1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * SCSI disk target driver. 30 */ 31 #include <sys/scsi/scsi.h> 32 #include <sys/dkbad.h> 33 #include <sys/dklabel.h> 34 #include <sys/dkio.h> 35 #include <sys/fdio.h> 36 #include <sys/cdio.h> 37 #include <sys/mhd.h> 38 #include <sys/vtoc.h> 39 #include <sys/dktp/fdisk.h> 40 #include <sys/kstat.h> 41 #include <sys/vtrace.h> 42 #include <sys/note.h> 43 #include <sys/thread.h> 44 #include <sys/proc.h> 45 #include <sys/efi_partition.h> 46 #include <sys/var.h> 47 #include <sys/aio_req.h> 48 49 #ifdef __lock_lint 50 #define _LP64 51 #define __amd64 52 #endif 53 54 #if (defined(__fibre)) 55 /* Note: is there a leadville version of the following? */ 56 #include <sys/fc4/fcal_linkapp.h> 57 #endif 58 #include <sys/taskq.h> 59 #include <sys/uuid.h> 60 #include <sys/byteorder.h> 61 #include <sys/sdt.h> 62 63 #include "sd_xbuf.h" 64 65 #include <sys/scsi/targets/sddef.h> 66 67 68 /* 69 * Loadable module info. 70 */ 71 #if (defined(__fibre)) 72 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 73 char _depends_on[] = "misc/scsi drv/fcp"; 74 #else 75 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 76 char _depends_on[] = "misc/scsi"; 77 #endif 78 79 /* 80 * Define the interconnect type, to allow the driver to distinguish 81 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 82 * 83 * This is really for backward compatability. In the future, the driver 84 * should actually check the "interconnect-type" property as reported by 85 * the HBA; however at present this property is not defined by all HBAs, 86 * so we will use this #define (1) to permit the driver to run in 87 * backward-compatability mode; and (2) to print a notification message 88 * if an FC HBA does not support the "interconnect-type" property. The 89 * behavior of the driver will be to assume parallel SCSI behaviors unless 90 * the "interconnect-type" property is defined by the HBA **AND** has a 91 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 92 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 93 * Channel behaviors (as per the old ssd). (Note that the 94 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 95 * will result in the driver assuming parallel SCSI behaviors.) 96 * 97 * (see common/sys/scsi/impl/services.h) 98 * 99 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 100 * since some FC HBAs may already support that, and there is some code in 101 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 102 * default would confuse that code, and besides things should work fine 103 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 104 * "interconnect_type" property. 105 * 106 * Notes for off-by-1 workaround: 107 * ----------------------------- 108 * 109 * SCSI READ_CAPACITY command returns the LBA number of the 110 * last logical block, but sd once treated this number as 111 * disks' capacity on x86 platform. And LBAs are addressed 112 * based 0. So the last block was lost on x86 platform. 113 * 114 * Now, we remove this workaround. In order for present sd 115 * driver to work with disks which are labeled/partitioned 116 * via previous sd, we add workaround as follows: 117 * 118 * 1) Locate backup EFI label: sd searches the next to last 119 * block for legacy backup EFI label. If fails, it will 120 * turn to the last block for backup EFI label; 121 * 2) Clear backup EFI label: sd first search the last block 122 * for backup EFI label, and will search the next to last 123 * block only if failed for the last block. 124 * 3) Calculate geometry: refer to sd_convert_geometry(), If 125 * capacity increasing by 1 causes disks' capacity to cross 126 * over the limits in table CHS_values, geometry info will 127 * change. This will raise an issue: In case that primary 128 * VTOC label is destroyed, format commandline can restore 129 * it via backup VTOC labels. And format locates backup VTOC 130 * labels by use of geometry from sd driver. So changing 131 * geometry will prevent format from finding backup VTOC 132 * labels. To eliminate this side effect for compatibility, 133 * sd uses (capacity -1) to calculate geometry; 134 * 4) 1TB disks: some important data structures use 32-bit 135 * signed long/int (for example, daddr_t), so that sd doesn't 136 * support a disk with capacity larger than 1TB on 32-bit 137 * platform. However, for exactly 1TB disk, it was treated as 138 * (1T - 512)B in the past, and could have valid solaris 139 * partitions. To workaround this, if an exactly 1TB disk has 140 * solaris fdisk partition, it will be allowed to work with sd. 141 */ 142 #if (defined(__fibre)) 143 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 144 #else 145 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 146 #endif 147 148 /* 149 * The name of the driver, established from the module name in _init. 150 */ 151 static char *sd_label = NULL; 152 153 /* 154 * Driver name is unfortunately prefixed on some driver.conf properties. 155 */ 156 #if (defined(__fibre)) 157 #define sd_max_xfer_size ssd_max_xfer_size 158 #define sd_config_list ssd_config_list 159 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 160 static char *sd_config_list = "ssd-config-list"; 161 #else 162 static char *sd_max_xfer_size = "sd_max_xfer_size"; 163 static char *sd_config_list = "sd-config-list"; 164 #endif 165 166 /* 167 * Driver global variables 168 */ 169 170 #if (defined(__fibre)) 171 /* 172 * These #defines are to avoid namespace collisions that occur because this 173 * code is currently used to compile two seperate driver modules: sd and ssd. 174 * All global variables need to be treated this way (even if declared static) 175 * in order to allow the debugger to resolve the names properly. 176 * It is anticipated that in the near future the ssd module will be obsoleted, 177 * at which time this namespace issue should go away. 178 */ 179 #define sd_state ssd_state 180 #define sd_io_time ssd_io_time 181 #define sd_failfast_enable ssd_failfast_enable 182 #define sd_ua_retry_count ssd_ua_retry_count 183 #define sd_report_pfa ssd_report_pfa 184 #define sd_max_throttle ssd_max_throttle 185 #define sd_min_throttle ssd_min_throttle 186 #define sd_rot_delay ssd_rot_delay 187 188 #define sd_retry_on_reservation_conflict \ 189 ssd_retry_on_reservation_conflict 190 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 191 #define sd_resv_conflict_name ssd_resv_conflict_name 192 193 #define sd_component_mask ssd_component_mask 194 #define sd_level_mask ssd_level_mask 195 #define sd_debug_un ssd_debug_un 196 #define sd_error_level ssd_error_level 197 198 #define sd_xbuf_active_limit ssd_xbuf_active_limit 199 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 200 201 #define sd_tr ssd_tr 202 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 203 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 204 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 205 #define sd_check_media_time ssd_check_media_time 206 #define sd_wait_cmds_complete ssd_wait_cmds_complete 207 #define sd_label_mutex ssd_label_mutex 208 #define sd_detach_mutex ssd_detach_mutex 209 #define sd_log_buf ssd_log_buf 210 #define sd_log_mutex ssd_log_mutex 211 212 #define sd_disk_table ssd_disk_table 213 #define sd_disk_table_size ssd_disk_table_size 214 #define sd_sense_mutex ssd_sense_mutex 215 #define sd_cdbtab ssd_cdbtab 216 217 #define sd_cb_ops ssd_cb_ops 218 #define sd_ops ssd_ops 219 #define sd_additional_codes ssd_additional_codes 220 221 #define sd_minor_data ssd_minor_data 222 #define sd_minor_data_efi ssd_minor_data_efi 223 224 #define sd_tq ssd_tq 225 #define sd_wmr_tq ssd_wmr_tq 226 #define sd_taskq_name ssd_taskq_name 227 #define sd_wmr_taskq_name ssd_wmr_taskq_name 228 #define sd_taskq_minalloc ssd_taskq_minalloc 229 #define sd_taskq_maxalloc ssd_taskq_maxalloc 230 231 #define sd_dump_format_string ssd_dump_format_string 232 233 #define sd_iostart_chain ssd_iostart_chain 234 #define sd_iodone_chain ssd_iodone_chain 235 236 #define sd_pm_idletime ssd_pm_idletime 237 238 #define sd_force_pm_supported ssd_force_pm_supported 239 240 #define sd_dtype_optical_bind ssd_dtype_optical_bind 241 242 #endif 243 244 245 #ifdef SDDEBUG 246 int sd_force_pm_supported = 0; 247 #endif /* SDDEBUG */ 248 249 void *sd_state = NULL; 250 int sd_io_time = SD_IO_TIME; 251 int sd_failfast_enable = 1; 252 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 253 int sd_report_pfa = 1; 254 int sd_max_throttle = SD_MAX_THROTTLE; 255 int sd_min_throttle = SD_MIN_THROTTLE; 256 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 257 int sd_qfull_throttle_enable = TRUE; 258 259 int sd_retry_on_reservation_conflict = 1; 260 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 261 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 262 263 static int sd_dtype_optical_bind = -1; 264 265 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 266 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 267 268 /* 269 * Global data for debug logging. To enable debug printing, sd_component_mask 270 * and sd_level_mask should be set to the desired bit patterns as outlined in 271 * sddef.h. 272 */ 273 uint_t sd_component_mask = 0x0; 274 uint_t sd_level_mask = 0x0; 275 struct sd_lun *sd_debug_un = NULL; 276 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 277 278 /* Note: these may go away in the future... */ 279 static uint32_t sd_xbuf_active_limit = 512; 280 static uint32_t sd_xbuf_reserve_limit = 16; 281 282 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 283 284 /* 285 * Timer value used to reset the throttle after it has been reduced 286 * (typically in response to TRAN_BUSY or STATUS_QFULL) 287 */ 288 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 289 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 290 291 /* 292 * Interval value associated with the media change scsi watch. 293 */ 294 static int sd_check_media_time = 3000000; 295 296 /* 297 * Wait value used for in progress operations during a DDI_SUSPEND 298 */ 299 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 300 301 /* 302 * sd_label_mutex protects a static buffer used in the disk label 303 * component of the driver 304 */ 305 static kmutex_t sd_label_mutex; 306 307 /* 308 * sd_detach_mutex protects un_layer_count, un_detach_count, and 309 * un_opens_in_progress in the sd_lun structure. 310 */ 311 static kmutex_t sd_detach_mutex; 312 313 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 314 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 315 316 /* 317 * Global buffer and mutex for debug logging 318 */ 319 static char sd_log_buf[1024]; 320 static kmutex_t sd_log_mutex; 321 322 /* 323 * Structs and globals for recording attached lun information. 324 * This maintains a chain. Each node in the chain represents a SCSI controller. 325 * The structure records the number of luns attached to each target connected 326 * with the controller. 327 * For parallel scsi device only. 328 */ 329 struct sd_scsi_hba_tgt_lun { 330 struct sd_scsi_hba_tgt_lun *next; 331 dev_info_t *pdip; 332 int nlun[NTARGETS_WIDE]; 333 }; 334 335 /* 336 * Flag to indicate the lun is attached or detached 337 */ 338 #define SD_SCSI_LUN_ATTACH 0 339 #define SD_SCSI_LUN_DETACH 1 340 341 static kmutex_t sd_scsi_target_lun_mutex; 342 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 343 344 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 345 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 346 347 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 348 sd_scsi_target_lun_head)) 349 350 /* 351 * "Smart" Probe Caching structs, globals, #defines, etc. 352 * For parallel scsi and non-self-identify device only. 353 */ 354 355 /* 356 * The following resources and routines are implemented to support 357 * "smart" probing, which caches the scsi_probe() results in an array, 358 * in order to help avoid long probe times. 359 */ 360 struct sd_scsi_probe_cache { 361 struct sd_scsi_probe_cache *next; 362 dev_info_t *pdip; 363 int cache[NTARGETS_WIDE]; 364 }; 365 366 static kmutex_t sd_scsi_probe_cache_mutex; 367 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 368 369 /* 370 * Really we only need protection on the head of the linked list, but 371 * better safe than sorry. 372 */ 373 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 374 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 375 376 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 377 sd_scsi_probe_cache_head)) 378 379 380 /* 381 * Vendor specific data name property declarations 382 */ 383 384 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 385 386 static sd_tunables seagate_properties = { 387 SEAGATE_THROTTLE_VALUE, 388 0, 389 0, 390 0, 391 0, 392 0, 393 0, 394 0, 395 0 396 }; 397 398 399 static sd_tunables fujitsu_properties = { 400 FUJITSU_THROTTLE_VALUE, 401 0, 402 0, 403 0, 404 0, 405 0, 406 0, 407 0, 408 0 409 }; 410 411 static sd_tunables ibm_properties = { 412 IBM_THROTTLE_VALUE, 413 0, 414 0, 415 0, 416 0, 417 0, 418 0, 419 0, 420 0 421 }; 422 423 static sd_tunables purple_properties = { 424 PURPLE_THROTTLE_VALUE, 425 0, 426 0, 427 PURPLE_BUSY_RETRIES, 428 PURPLE_RESET_RETRY_COUNT, 429 PURPLE_RESERVE_RELEASE_TIME, 430 0, 431 0, 432 0 433 }; 434 435 static sd_tunables sve_properties = { 436 SVE_THROTTLE_VALUE, 437 0, 438 0, 439 SVE_BUSY_RETRIES, 440 SVE_RESET_RETRY_COUNT, 441 SVE_RESERVE_RELEASE_TIME, 442 SVE_MIN_THROTTLE_VALUE, 443 SVE_DISKSORT_DISABLED_FLAG, 444 0 445 }; 446 447 static sd_tunables maserati_properties = { 448 0, 449 0, 450 0, 451 0, 452 0, 453 0, 454 0, 455 MASERATI_DISKSORT_DISABLED_FLAG, 456 MASERATI_LUN_RESET_ENABLED_FLAG 457 }; 458 459 static sd_tunables pirus_properties = { 460 PIRUS_THROTTLE_VALUE, 461 0, 462 PIRUS_NRR_COUNT, 463 PIRUS_BUSY_RETRIES, 464 PIRUS_RESET_RETRY_COUNT, 465 0, 466 PIRUS_MIN_THROTTLE_VALUE, 467 PIRUS_DISKSORT_DISABLED_FLAG, 468 PIRUS_LUN_RESET_ENABLED_FLAG 469 }; 470 471 #endif 472 473 #if (defined(__sparc) && !defined(__fibre)) || \ 474 (defined(__i386) || defined(__amd64)) 475 476 477 static sd_tunables elite_properties = { 478 ELITE_THROTTLE_VALUE, 479 0, 480 0, 481 0, 482 0, 483 0, 484 0, 485 0, 486 0 487 }; 488 489 static sd_tunables st31200n_properties = { 490 ST31200N_THROTTLE_VALUE, 491 0, 492 0, 493 0, 494 0, 495 0, 496 0, 497 0, 498 0 499 }; 500 501 #endif /* Fibre or not */ 502 503 static sd_tunables lsi_properties_scsi = { 504 LSI_THROTTLE_VALUE, 505 0, 506 LSI_NOTREADY_RETRIES, 507 0, 508 0, 509 0, 510 0, 511 0, 512 0 513 }; 514 515 static sd_tunables symbios_properties = { 516 SYMBIOS_THROTTLE_VALUE, 517 0, 518 SYMBIOS_NOTREADY_RETRIES, 519 0, 520 0, 521 0, 522 0, 523 0, 524 0 525 }; 526 527 static sd_tunables lsi_properties = { 528 0, 529 0, 530 LSI_NOTREADY_RETRIES, 531 0, 532 0, 533 0, 534 0, 535 0, 536 0 537 }; 538 539 static sd_tunables lsi_oem_properties = { 540 0, 541 0, 542 LSI_OEM_NOTREADY_RETRIES, 543 0, 544 0, 545 0, 546 0, 547 0, 548 0 549 }; 550 551 552 553 #if (defined(SD_PROP_TST)) 554 555 #define SD_TST_CTYPE_VAL CTYPE_CDROM 556 #define SD_TST_THROTTLE_VAL 16 557 #define SD_TST_NOTREADY_VAL 12 558 #define SD_TST_BUSY_VAL 60 559 #define SD_TST_RST_RETRY_VAL 36 560 #define SD_TST_RSV_REL_TIME 60 561 562 static sd_tunables tst_properties = { 563 SD_TST_THROTTLE_VAL, 564 SD_TST_CTYPE_VAL, 565 SD_TST_NOTREADY_VAL, 566 SD_TST_BUSY_VAL, 567 SD_TST_RST_RETRY_VAL, 568 SD_TST_RSV_REL_TIME, 569 0, 570 0, 571 0 572 }; 573 #endif 574 575 /* This is similiar to the ANSI toupper implementation */ 576 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 577 578 /* 579 * Static Driver Configuration Table 580 * 581 * This is the table of disks which need throttle adjustment (or, perhaps 582 * something else as defined by the flags at a future time.) device_id 583 * is a string consisting of concatenated vid (vendor), pid (product/model) 584 * and revision strings as defined in the scsi_inquiry structure. Offsets of 585 * the parts of the string are as defined by the sizes in the scsi_inquiry 586 * structure. Device type is searched as far as the device_id string is 587 * defined. Flags defines which values are to be set in the driver from the 588 * properties list. 589 * 590 * Entries below which begin and end with a "*" are a special case. 591 * These do not have a specific vendor, and the string which follows 592 * can appear anywhere in the 16 byte PID portion of the inquiry data. 593 * 594 * Entries below which begin and end with a " " (blank) are a special 595 * case. The comparison function will treat multiple consecutive blanks 596 * as equivalent to a single blank. For example, this causes a 597 * sd_disk_table entry of " NEC CDROM " to match a device's id string 598 * of "NEC CDROM". 599 * 600 * Note: The MD21 controller type has been obsoleted. 601 * ST318202F is a Legacy device 602 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 603 * made with an FC connection. The entries here are a legacy. 604 */ 605 static sd_disk_config_t sd_disk_table[] = { 606 #if defined(__fibre) || defined(__i386) || defined(__amd64) 607 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 608 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 609 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 610 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 611 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 612 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 613 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 614 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 615 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 616 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 617 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 618 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 620 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 621 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 622 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 623 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 624 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 625 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 626 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 627 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 628 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 629 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 630 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 631 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 632 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 633 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 634 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 635 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 636 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 637 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 638 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 639 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 640 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 641 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 642 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 653 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 654 { "SUN T3", SD_CONF_BSET_THROTTLE | 655 SD_CONF_BSET_BSY_RETRY_COUNT| 656 SD_CONF_BSET_RST_RETRIES| 657 SD_CONF_BSET_RSV_REL_TIME, 658 &purple_properties }, 659 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 660 SD_CONF_BSET_BSY_RETRY_COUNT| 661 SD_CONF_BSET_RST_RETRIES| 662 SD_CONF_BSET_RSV_REL_TIME| 663 SD_CONF_BSET_MIN_THROTTLE| 664 SD_CONF_BSET_DISKSORT_DISABLED, 665 &sve_properties }, 666 { "SUN T4", SD_CONF_BSET_THROTTLE | 667 SD_CONF_BSET_BSY_RETRY_COUNT| 668 SD_CONF_BSET_RST_RETRIES| 669 SD_CONF_BSET_RSV_REL_TIME, 670 &purple_properties }, 671 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 672 SD_CONF_BSET_LUN_RESET_ENABLED, 673 &maserati_properties }, 674 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 675 SD_CONF_BSET_NRR_COUNT| 676 SD_CONF_BSET_BSY_RETRY_COUNT| 677 SD_CONF_BSET_RST_RETRIES| 678 SD_CONF_BSET_MIN_THROTTLE| 679 SD_CONF_BSET_DISKSORT_DISABLED| 680 SD_CONF_BSET_LUN_RESET_ENABLED, 681 &pirus_properties }, 682 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 683 SD_CONF_BSET_NRR_COUNT| 684 SD_CONF_BSET_BSY_RETRY_COUNT| 685 SD_CONF_BSET_RST_RETRIES| 686 SD_CONF_BSET_MIN_THROTTLE| 687 SD_CONF_BSET_DISKSORT_DISABLED| 688 SD_CONF_BSET_LUN_RESET_ENABLED, 689 &pirus_properties }, 690 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 691 SD_CONF_BSET_NRR_COUNT| 692 SD_CONF_BSET_BSY_RETRY_COUNT| 693 SD_CONF_BSET_RST_RETRIES| 694 SD_CONF_BSET_MIN_THROTTLE| 695 SD_CONF_BSET_DISKSORT_DISABLED| 696 SD_CONF_BSET_LUN_RESET_ENABLED, 697 &pirus_properties }, 698 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 699 SD_CONF_BSET_NRR_COUNT| 700 SD_CONF_BSET_BSY_RETRY_COUNT| 701 SD_CONF_BSET_RST_RETRIES| 702 SD_CONF_BSET_MIN_THROTTLE| 703 SD_CONF_BSET_DISKSORT_DISABLED| 704 SD_CONF_BSET_LUN_RESET_ENABLED, 705 &pirus_properties }, 706 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 707 SD_CONF_BSET_NRR_COUNT| 708 SD_CONF_BSET_BSY_RETRY_COUNT| 709 SD_CONF_BSET_RST_RETRIES| 710 SD_CONF_BSET_MIN_THROTTLE| 711 SD_CONF_BSET_DISKSORT_DISABLED| 712 SD_CONF_BSET_LUN_RESET_ENABLED, 713 &pirus_properties }, 714 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 715 SD_CONF_BSET_NRR_COUNT| 716 SD_CONF_BSET_BSY_RETRY_COUNT| 717 SD_CONF_BSET_RST_RETRIES| 718 SD_CONF_BSET_MIN_THROTTLE| 719 SD_CONF_BSET_DISKSORT_DISABLED| 720 SD_CONF_BSET_LUN_RESET_ENABLED, 721 &pirus_properties }, 722 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 723 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 724 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 725 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 726 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 727 #endif /* fibre or NON-sparc platforms */ 728 #if ((defined(__sparc) && !defined(__fibre)) ||\ 729 (defined(__i386) || defined(__amd64))) 730 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 731 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 732 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 733 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 734 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 735 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 736 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 737 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 738 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 739 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 740 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 741 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 742 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 743 &symbios_properties }, 744 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 745 &lsi_properties_scsi }, 746 #if defined(__i386) || defined(__amd64) 747 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 748 | SD_CONF_BSET_READSUB_BCD 749 | SD_CONF_BSET_READ_TOC_ADDR_BCD 750 | SD_CONF_BSET_NO_READ_HEADER 751 | SD_CONF_BSET_READ_CD_XD4), NULL }, 752 753 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 754 | SD_CONF_BSET_READSUB_BCD 755 | SD_CONF_BSET_READ_TOC_ADDR_BCD 756 | SD_CONF_BSET_NO_READ_HEADER 757 | SD_CONF_BSET_READ_CD_XD4), NULL }, 758 #endif /* __i386 || __amd64 */ 759 #endif /* sparc NON-fibre or NON-sparc platforms */ 760 761 #if (defined(SD_PROP_TST)) 762 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 763 | SD_CONF_BSET_CTYPE 764 | SD_CONF_BSET_NRR_COUNT 765 | SD_CONF_BSET_FAB_DEVID 766 | SD_CONF_BSET_NOCACHE 767 | SD_CONF_BSET_BSY_RETRY_COUNT 768 | SD_CONF_BSET_PLAYMSF_BCD 769 | SD_CONF_BSET_READSUB_BCD 770 | SD_CONF_BSET_READ_TOC_TRK_BCD 771 | SD_CONF_BSET_READ_TOC_ADDR_BCD 772 | SD_CONF_BSET_NO_READ_HEADER 773 | SD_CONF_BSET_READ_CD_XD4 774 | SD_CONF_BSET_RST_RETRIES 775 | SD_CONF_BSET_RSV_REL_TIME 776 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 777 #endif 778 }; 779 780 static const int sd_disk_table_size = 781 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 782 783 784 /* 785 * Return codes of sd_uselabel(). 786 */ 787 #define SD_LABEL_IS_VALID 0 788 #define SD_LABEL_IS_INVALID 1 789 790 #define SD_INTERCONNECT_PARALLEL 0 791 #define SD_INTERCONNECT_FABRIC 1 792 #define SD_INTERCONNECT_FIBRE 2 793 #define SD_INTERCONNECT_SSA 3 794 #define SD_INTERCONNECT_SATA 4 795 #define SD_IS_PARALLEL_SCSI(un) \ 796 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 797 #define SD_IS_SERIAL(un) \ 798 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 799 800 /* 801 * Definitions used by device id registration routines 802 */ 803 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 804 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 805 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 806 #define WD_NODE 7 /* the whole disk minor */ 807 808 static kmutex_t sd_sense_mutex = {0}; 809 810 /* 811 * Macros for updates of the driver state 812 */ 813 #define New_state(un, s) \ 814 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 815 #define Restore_state(un) \ 816 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 817 818 static struct sd_cdbinfo sd_cdbtab[] = { 819 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 820 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 821 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 822 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 823 }; 824 825 /* 826 * Specifies the number of seconds that must have elapsed since the last 827 * cmd. has completed for a device to be declared idle to the PM framework. 828 */ 829 static int sd_pm_idletime = 1; 830 831 /* 832 * Internal function prototypes 833 */ 834 835 #if (defined(__fibre)) 836 /* 837 * These #defines are to avoid namespace collisions that occur because this 838 * code is currently used to compile two seperate driver modules: sd and ssd. 839 * All function names need to be treated this way (even if declared static) 840 * in order to allow the debugger to resolve the names properly. 841 * It is anticipated that in the near future the ssd module will be obsoleted, 842 * at which time this ugliness should go away. 843 */ 844 #define sd_log_trace ssd_log_trace 845 #define sd_log_info ssd_log_info 846 #define sd_log_err ssd_log_err 847 #define sdprobe ssdprobe 848 #define sdinfo ssdinfo 849 #define sd_prop_op ssd_prop_op 850 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 851 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 852 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 853 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 854 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 855 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 856 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 857 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 858 #define sd_spin_up_unit ssd_spin_up_unit 859 #define sd_enable_descr_sense ssd_enable_descr_sense 860 #define sd_reenable_dsense_task ssd_reenable_dsense_task 861 #define sd_set_mmc_caps ssd_set_mmc_caps 862 #define sd_read_unit_properties ssd_read_unit_properties 863 #define sd_process_sdconf_file ssd_process_sdconf_file 864 #define sd_process_sdconf_table ssd_process_sdconf_table 865 #define sd_sdconf_id_match ssd_sdconf_id_match 866 #define sd_blank_cmp ssd_blank_cmp 867 #define sd_chk_vers1_data ssd_chk_vers1_data 868 #define sd_set_vers1_properties ssd_set_vers1_properties 869 #define sd_validate_geometry ssd_validate_geometry 870 871 #if defined(_SUNOS_VTOC_16) 872 #define sd_convert_geometry ssd_convert_geometry 873 #endif 874 875 #define sd_resync_geom_caches ssd_resync_geom_caches 876 #define sd_read_fdisk ssd_read_fdisk 877 #define sd_get_physical_geometry ssd_get_physical_geometry 878 #define sd_get_virtual_geometry ssd_get_virtual_geometry 879 #define sd_update_block_info ssd_update_block_info 880 #define sd_swap_efi_gpt ssd_swap_efi_gpt 881 #define sd_swap_efi_gpe ssd_swap_efi_gpe 882 #define sd_validate_efi ssd_validate_efi 883 #define sd_use_efi ssd_use_efi 884 #define sd_uselabel ssd_uselabel 885 #define sd_build_default_label ssd_build_default_label 886 #define sd_has_max_chs_vals ssd_has_max_chs_vals 887 #define sd_inq_fill ssd_inq_fill 888 #define sd_register_devid ssd_register_devid 889 #define sd_get_devid_block ssd_get_devid_block 890 #define sd_get_devid ssd_get_devid 891 #define sd_create_devid ssd_create_devid 892 #define sd_write_deviceid ssd_write_deviceid 893 #define sd_check_vpd_page_support ssd_check_vpd_page_support 894 #define sd_setup_pm ssd_setup_pm 895 #define sd_create_pm_components ssd_create_pm_components 896 #define sd_ddi_suspend ssd_ddi_suspend 897 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 898 #define sd_ddi_resume ssd_ddi_resume 899 #define sd_ddi_pm_resume ssd_ddi_pm_resume 900 #define sdpower ssdpower 901 #define sdattach ssdattach 902 #define sddetach ssddetach 903 #define sd_unit_attach ssd_unit_attach 904 #define sd_unit_detach ssd_unit_detach 905 #define sd_set_unit_attributes ssd_set_unit_attributes 906 #define sd_create_minor_nodes ssd_create_minor_nodes 907 #define sd_create_errstats ssd_create_errstats 908 #define sd_set_errstats ssd_set_errstats 909 #define sd_set_pstats ssd_set_pstats 910 #define sddump ssddump 911 #define sd_scsi_poll ssd_scsi_poll 912 #define sd_send_polled_RQS ssd_send_polled_RQS 913 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 914 #define sd_init_event_callbacks ssd_init_event_callbacks 915 #define sd_event_callback ssd_event_callback 916 #define sd_cache_control ssd_cache_control 917 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 918 #define sd_make_device ssd_make_device 919 #define sdopen ssdopen 920 #define sdclose ssdclose 921 #define sd_ready_and_valid ssd_ready_and_valid 922 #define sdmin ssdmin 923 #define sdread ssdread 924 #define sdwrite ssdwrite 925 #define sdaread ssdaread 926 #define sdawrite ssdawrite 927 #define sdstrategy ssdstrategy 928 #define sdioctl ssdioctl 929 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 930 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 931 #define sd_checksum_iostart ssd_checksum_iostart 932 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 933 #define sd_pm_iostart ssd_pm_iostart 934 #define sd_core_iostart ssd_core_iostart 935 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 936 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 937 #define sd_checksum_iodone ssd_checksum_iodone 938 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 939 #define sd_pm_iodone ssd_pm_iodone 940 #define sd_initpkt_for_buf ssd_initpkt_for_buf 941 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 942 #define sd_setup_rw_pkt ssd_setup_rw_pkt 943 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 944 #define sd_buf_iodone ssd_buf_iodone 945 #define sd_uscsi_strategy ssd_uscsi_strategy 946 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 947 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 948 #define sd_uscsi_iodone ssd_uscsi_iodone 949 #define sd_xbuf_strategy ssd_xbuf_strategy 950 #define sd_xbuf_init ssd_xbuf_init 951 #define sd_pm_entry ssd_pm_entry 952 #define sd_pm_exit ssd_pm_exit 953 954 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 955 #define sd_pm_timeout_handler ssd_pm_timeout_handler 956 957 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 958 #define sdintr ssdintr 959 #define sd_start_cmds ssd_start_cmds 960 #define sd_send_scsi_cmd ssd_send_scsi_cmd 961 #define sd_bioclone_alloc ssd_bioclone_alloc 962 #define sd_bioclone_free ssd_bioclone_free 963 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 964 #define sd_shadow_buf_free ssd_shadow_buf_free 965 #define sd_print_transport_rejected_message \ 966 ssd_print_transport_rejected_message 967 #define sd_retry_command ssd_retry_command 968 #define sd_set_retry_bp ssd_set_retry_bp 969 #define sd_send_request_sense_command ssd_send_request_sense_command 970 #define sd_start_retry_command ssd_start_retry_command 971 #define sd_start_direct_priority_command \ 972 ssd_start_direct_priority_command 973 #define sd_return_failed_command ssd_return_failed_command 974 #define sd_return_failed_command_no_restart \ 975 ssd_return_failed_command_no_restart 976 #define sd_return_command ssd_return_command 977 #define sd_sync_with_callback ssd_sync_with_callback 978 #define sdrunout ssdrunout 979 #define sd_mark_rqs_busy ssd_mark_rqs_busy 980 #define sd_mark_rqs_idle ssd_mark_rqs_idle 981 #define sd_reduce_throttle ssd_reduce_throttle 982 #define sd_restore_throttle ssd_restore_throttle 983 #define sd_print_incomplete_msg ssd_print_incomplete_msg 984 #define sd_init_cdb_limits ssd_init_cdb_limits 985 #define sd_pkt_status_good ssd_pkt_status_good 986 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 987 #define sd_pkt_status_busy ssd_pkt_status_busy 988 #define sd_pkt_status_reservation_conflict \ 989 ssd_pkt_status_reservation_conflict 990 #define sd_pkt_status_qfull ssd_pkt_status_qfull 991 #define sd_handle_request_sense ssd_handle_request_sense 992 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 993 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 994 #define sd_validate_sense_data ssd_validate_sense_data 995 #define sd_decode_sense ssd_decode_sense 996 #define sd_print_sense_msg ssd_print_sense_msg 997 #define sd_sense_key_no_sense ssd_sense_key_no_sense 998 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 999 #define sd_sense_key_not_ready ssd_sense_key_not_ready 1000 #define sd_sense_key_medium_or_hardware_error \ 1001 ssd_sense_key_medium_or_hardware_error 1002 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1003 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1004 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1005 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1006 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1007 #define sd_sense_key_default ssd_sense_key_default 1008 #define sd_print_retry_msg ssd_print_retry_msg 1009 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1010 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1011 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1012 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1013 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1014 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1015 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1016 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1017 #define sd_pkt_reason_default ssd_pkt_reason_default 1018 #define sd_reset_target ssd_reset_target 1019 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1020 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1021 #define sd_taskq_create ssd_taskq_create 1022 #define sd_taskq_delete ssd_taskq_delete 1023 #define sd_media_change_task ssd_media_change_task 1024 #define sd_handle_mchange ssd_handle_mchange 1025 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1026 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1027 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1028 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1029 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1030 sd_send_scsi_feature_GET_CONFIGURATION 1031 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1032 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1033 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1034 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1035 ssd_send_scsi_PERSISTENT_RESERVE_IN 1036 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1037 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1038 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1039 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1040 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1041 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1042 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1043 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1044 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1045 #define sd_alloc_rqs ssd_alloc_rqs 1046 #define sd_free_rqs ssd_free_rqs 1047 #define sd_dump_memory ssd_dump_memory 1048 #define sd_get_media_info ssd_get_media_info 1049 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1050 #define sd_dkio_get_geometry ssd_dkio_get_geometry 1051 #define sd_dkio_set_geometry ssd_dkio_set_geometry 1052 #define sd_dkio_get_partition ssd_dkio_get_partition 1053 #define sd_dkio_set_partition ssd_dkio_set_partition 1054 #define sd_dkio_partition ssd_dkio_partition 1055 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 1056 #define sd_dkio_get_efi ssd_dkio_get_efi 1057 #define sd_build_user_vtoc ssd_build_user_vtoc 1058 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 1059 #define sd_dkio_set_efi ssd_dkio_set_efi 1060 #define sd_build_label_vtoc ssd_build_label_vtoc 1061 #define sd_write_label ssd_write_label 1062 #define sd_clear_vtoc ssd_clear_vtoc 1063 #define sd_clear_efi ssd_clear_efi 1064 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1065 #define sd_setup_next_xfer ssd_setup_next_xfer 1066 #define sd_dkio_get_temp ssd_dkio_get_temp 1067 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1068 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1069 #define sd_setup_default_geometry ssd_setup_default_geometry 1070 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1071 #define sd_check_mhd ssd_check_mhd 1072 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1073 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1074 #define sd_sname ssd_sname 1075 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1076 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1077 #define sd_take_ownership ssd_take_ownership 1078 #define sd_reserve_release ssd_reserve_release 1079 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1080 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1081 #define sd_persistent_reservation_in_read_keys \ 1082 ssd_persistent_reservation_in_read_keys 1083 #define sd_persistent_reservation_in_read_resv \ 1084 ssd_persistent_reservation_in_read_resv 1085 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1086 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1087 #define sd_mhdioc_release ssd_mhdioc_release 1088 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1089 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1090 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1091 #define sr_change_blkmode ssr_change_blkmode 1092 #define sr_change_speed ssr_change_speed 1093 #define sr_atapi_change_speed ssr_atapi_change_speed 1094 #define sr_pause_resume ssr_pause_resume 1095 #define sr_play_msf ssr_play_msf 1096 #define sr_play_trkind ssr_play_trkind 1097 #define sr_read_all_subcodes ssr_read_all_subcodes 1098 #define sr_read_subchannel ssr_read_subchannel 1099 #define sr_read_tocentry ssr_read_tocentry 1100 #define sr_read_tochdr ssr_read_tochdr 1101 #define sr_read_cdda ssr_read_cdda 1102 #define sr_read_cdxa ssr_read_cdxa 1103 #define sr_read_mode1 ssr_read_mode1 1104 #define sr_read_mode2 ssr_read_mode2 1105 #define sr_read_cd_mode2 ssr_read_cd_mode2 1106 #define sr_sector_mode ssr_sector_mode 1107 #define sr_eject ssr_eject 1108 #define sr_ejected ssr_ejected 1109 #define sr_check_wp ssr_check_wp 1110 #define sd_check_media ssd_check_media 1111 #define sd_media_watch_cb ssd_media_watch_cb 1112 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1113 #define sr_volume_ctrl ssr_volume_ctrl 1114 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1115 #define sd_log_page_supported ssd_log_page_supported 1116 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1117 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1118 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1119 #define sd_range_lock ssd_range_lock 1120 #define sd_get_range ssd_get_range 1121 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1122 #define sd_range_unlock ssd_range_unlock 1123 #define sd_read_modify_write_task ssd_read_modify_write_task 1124 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1125 1126 #define sd_iostart_chain ssd_iostart_chain 1127 #define sd_iodone_chain ssd_iodone_chain 1128 #define sd_initpkt_map ssd_initpkt_map 1129 #define sd_destroypkt_map ssd_destroypkt_map 1130 #define sd_chain_type_map ssd_chain_type_map 1131 #define sd_chain_index_map ssd_chain_index_map 1132 1133 #define sd_failfast_flushctl ssd_failfast_flushctl 1134 #define sd_failfast_flushq ssd_failfast_flushq 1135 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1136 1137 #define sd_is_lsi ssd_is_lsi 1138 1139 #endif /* #if (defined(__fibre)) */ 1140 1141 1142 int _init(void); 1143 int _fini(void); 1144 int _info(struct modinfo *modinfop); 1145 1146 /*PRINTFLIKE3*/ 1147 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1148 /*PRINTFLIKE3*/ 1149 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1150 /*PRINTFLIKE3*/ 1151 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1152 1153 static int sdprobe(dev_info_t *devi); 1154 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1155 void **result); 1156 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1157 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1158 1159 /* 1160 * Smart probe for parallel scsi 1161 */ 1162 static void sd_scsi_probe_cache_init(void); 1163 static void sd_scsi_probe_cache_fini(void); 1164 static void sd_scsi_clear_probe_cache(void); 1165 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1166 1167 /* 1168 * Attached luns on target for parallel scsi 1169 */ 1170 static void sd_scsi_target_lun_init(void); 1171 static void sd_scsi_target_lun_fini(void); 1172 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1173 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1174 1175 static int sd_spin_up_unit(struct sd_lun *un); 1176 #ifdef _LP64 1177 static void sd_enable_descr_sense(struct sd_lun *un); 1178 static void sd_reenable_dsense_task(void *arg); 1179 #endif /* _LP64 */ 1180 1181 static void sd_set_mmc_caps(struct sd_lun *un); 1182 1183 static void sd_read_unit_properties(struct sd_lun *un); 1184 static int sd_process_sdconf_file(struct sd_lun *un); 1185 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1186 int *data_list, sd_tunables *values); 1187 static void sd_process_sdconf_table(struct sd_lun *un); 1188 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1189 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1190 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1191 int list_len, char *dataname_ptr); 1192 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1193 sd_tunables *prop_list); 1194 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1195 1196 #if defined(_SUNOS_VTOC_16) 1197 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1198 #endif 1199 1200 static void sd_resync_geom_caches(struct sd_lun *un, uint64_t capacity, 1201 int lbasize, int path_flag); 1202 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1203 int path_flag); 1204 static void sd_get_physical_geometry(struct sd_lun *un, 1205 struct geom_cache *pgeom_p, uint64_t capacity, int lbasize, 1206 int path_flag); 1207 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1208 int lbasize); 1209 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1210 static void sd_swap_efi_gpt(efi_gpt_t *); 1211 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1212 static int sd_validate_efi(efi_gpt_t *); 1213 static int sd_use_efi(struct sd_lun *, int); 1214 static void sd_build_default_label(struct sd_lun *un); 1215 1216 #if defined(_FIRMWARE_NEEDS_FDISK) 1217 static int sd_has_max_chs_vals(struct ipart *fdp); 1218 #endif 1219 static void sd_inq_fill(char *p, int l, char *s); 1220 1221 1222 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1223 int reservation_flag); 1224 static daddr_t sd_get_devid_block(struct sd_lun *un); 1225 static int sd_get_devid(struct sd_lun *un); 1226 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1227 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1228 static int sd_write_deviceid(struct sd_lun *un); 1229 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1230 static int sd_check_vpd_page_support(struct sd_lun *un); 1231 1232 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1233 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1234 1235 static int sd_ddi_suspend(dev_info_t *devi); 1236 static int sd_ddi_pm_suspend(struct sd_lun *un); 1237 static int sd_ddi_resume(dev_info_t *devi); 1238 static int sd_ddi_pm_resume(struct sd_lun *un); 1239 static int sdpower(dev_info_t *devi, int component, int level); 1240 1241 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1242 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1243 static int sd_unit_attach(dev_info_t *devi); 1244 static int sd_unit_detach(dev_info_t *devi); 1245 1246 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1247 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1248 static void sd_create_errstats(struct sd_lun *un, int instance); 1249 static void sd_set_errstats(struct sd_lun *un); 1250 static void sd_set_pstats(struct sd_lun *un); 1251 1252 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1253 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1254 static int sd_send_polled_RQS(struct sd_lun *un); 1255 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1256 1257 #if (defined(__fibre)) 1258 /* 1259 * Event callbacks (photon) 1260 */ 1261 static void sd_init_event_callbacks(struct sd_lun *un); 1262 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1263 #endif 1264 1265 /* 1266 * Defines for sd_cache_control 1267 */ 1268 1269 #define SD_CACHE_ENABLE 1 1270 #define SD_CACHE_DISABLE 0 1271 #define SD_CACHE_NOCHANGE -1 1272 1273 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1274 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1275 static dev_t sd_make_device(dev_info_t *devi); 1276 1277 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1278 uint64_t capacity); 1279 1280 /* 1281 * Driver entry point functions. 1282 */ 1283 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1284 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1285 static int sd_ready_and_valid(struct sd_lun *un); 1286 1287 static void sdmin(struct buf *bp); 1288 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1289 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1290 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1291 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1292 1293 static int sdstrategy(struct buf *bp); 1294 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1295 1296 /* 1297 * Function prototypes for layering functions in the iostart chain. 1298 */ 1299 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1300 struct buf *bp); 1301 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1302 struct buf *bp); 1303 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1304 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1305 struct buf *bp); 1306 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1307 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1308 1309 /* 1310 * Function prototypes for layering functions in the iodone chain. 1311 */ 1312 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1313 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1314 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1315 struct buf *bp); 1316 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1317 struct buf *bp); 1318 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1319 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1320 struct buf *bp); 1321 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1322 1323 /* 1324 * Prototypes for functions to support buf(9S) based IO. 1325 */ 1326 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1327 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1328 static void sd_destroypkt_for_buf(struct buf *); 1329 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1330 struct buf *bp, int flags, 1331 int (*callback)(caddr_t), caddr_t callback_arg, 1332 diskaddr_t lba, uint32_t blockcount); 1333 #if defined(__i386) || defined(__amd64) 1334 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1335 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1336 #endif /* defined(__i386) || defined(__amd64) */ 1337 1338 /* 1339 * Prototypes for functions to support USCSI IO. 1340 */ 1341 static int sd_uscsi_strategy(struct buf *bp); 1342 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1343 static void sd_destroypkt_for_uscsi(struct buf *); 1344 1345 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1346 uchar_t chain_type, void *pktinfop); 1347 1348 static int sd_pm_entry(struct sd_lun *un); 1349 static void sd_pm_exit(struct sd_lun *un); 1350 1351 static void sd_pm_idletimeout_handler(void *arg); 1352 1353 /* 1354 * sd_core internal functions (used at the sd_core_io layer). 1355 */ 1356 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1357 static void sdintr(struct scsi_pkt *pktp); 1358 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1359 1360 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1361 enum uio_seg dataspace, int path_flag); 1362 1363 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1364 daddr_t blkno, int (*func)(struct buf *)); 1365 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1366 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1367 static void sd_bioclone_free(struct buf *bp); 1368 static void sd_shadow_buf_free(struct buf *bp); 1369 1370 static void sd_print_transport_rejected_message(struct sd_lun *un, 1371 struct sd_xbuf *xp, int code); 1372 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1373 void *arg, int code); 1374 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1375 void *arg, int code); 1376 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1377 void *arg, int code); 1378 1379 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1380 int retry_check_flag, 1381 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1382 int c), 1383 void *user_arg, int failure_code, clock_t retry_delay, 1384 void (*statp)(kstat_io_t *)); 1385 1386 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1387 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1388 1389 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1390 struct scsi_pkt *pktp); 1391 static void sd_start_retry_command(void *arg); 1392 static void sd_start_direct_priority_command(void *arg); 1393 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1394 int errcode); 1395 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1396 struct buf *bp, int errcode); 1397 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1398 static void sd_sync_with_callback(struct sd_lun *un); 1399 static int sdrunout(caddr_t arg); 1400 1401 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1402 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1403 1404 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1405 static void sd_restore_throttle(void *arg); 1406 1407 static void sd_init_cdb_limits(struct sd_lun *un); 1408 1409 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1410 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1411 1412 /* 1413 * Error handling functions 1414 */ 1415 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1416 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1417 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1418 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1419 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1420 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1421 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1422 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1423 1424 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1425 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1426 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1427 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1428 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1429 struct sd_xbuf *xp); 1430 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1431 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1432 1433 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1434 void *arg, int code); 1435 1436 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1437 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1438 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1439 uint8_t *sense_datap, 1440 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1441 static void sd_sense_key_not_ready(struct sd_lun *un, 1442 uint8_t *sense_datap, 1443 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1444 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1445 uint8_t *sense_datap, 1446 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1447 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1448 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1449 static void sd_sense_key_unit_attention(struct sd_lun *un, 1450 uint8_t *sense_datap, 1451 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1452 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1453 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1454 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1455 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1456 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1457 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1458 static void sd_sense_key_default(struct sd_lun *un, 1459 uint8_t *sense_datap, 1460 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1461 1462 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1463 void *arg, int flag); 1464 1465 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1466 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1467 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1468 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1469 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1470 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1471 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1472 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1473 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1474 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1475 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1476 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1477 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1478 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1479 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1480 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1481 1482 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1483 1484 static void sd_start_stop_unit_callback(void *arg); 1485 static void sd_start_stop_unit_task(void *arg); 1486 1487 static void sd_taskq_create(void); 1488 static void sd_taskq_delete(void); 1489 static void sd_media_change_task(void *arg); 1490 1491 static int sd_handle_mchange(struct sd_lun *un); 1492 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1493 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1494 uint32_t *lbap, int path_flag); 1495 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1496 uint32_t *lbap, int path_flag); 1497 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1498 int path_flag); 1499 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1500 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1501 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1502 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1503 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1504 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1505 uchar_t usr_cmd, uchar_t *usr_bufp); 1506 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1507 struct dk_callback *dkc); 1508 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1509 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1510 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1511 uchar_t *bufaddr, uint_t buflen); 1512 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1513 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1514 uchar_t *bufaddr, uint_t buflen, char feature); 1515 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1516 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1517 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1518 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1519 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1520 size_t buflen, daddr_t start_block, int path_flag); 1521 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1522 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1523 path_flag) 1524 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1525 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1526 path_flag) 1527 1528 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1529 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1530 uint16_t param_ptr, int path_flag); 1531 1532 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1533 static void sd_free_rqs(struct sd_lun *un); 1534 1535 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1536 uchar_t *data, int len, int fmt); 1537 static void sd_panic_for_res_conflict(struct sd_lun *un); 1538 1539 /* 1540 * Disk Ioctl Function Prototypes 1541 */ 1542 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1543 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1544 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1545 int geom_validated); 1546 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1547 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1548 int geom_validated); 1549 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1550 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1551 int geom_validated); 1552 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1553 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1554 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1555 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1556 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1557 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1558 static int sd_write_label(dev_t dev); 1559 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1560 static void sd_clear_vtoc(struct sd_lun *un); 1561 static void sd_clear_efi(struct sd_lun *un); 1562 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1563 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1564 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1565 static void sd_setup_default_geometry(struct sd_lun *un); 1566 #if defined(__i386) || defined(__amd64) 1567 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1568 #endif 1569 1570 /* 1571 * Multi-host Ioctl Prototypes 1572 */ 1573 static int sd_check_mhd(dev_t dev, int interval); 1574 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1575 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1576 static char *sd_sname(uchar_t status); 1577 static void sd_mhd_resvd_recover(void *arg); 1578 static void sd_resv_reclaim_thread(); 1579 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1580 static int sd_reserve_release(dev_t dev, int cmd); 1581 static void sd_rmv_resv_reclaim_req(dev_t dev); 1582 static void sd_mhd_reset_notify_cb(caddr_t arg); 1583 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1584 mhioc_inkeys_t *usrp, int flag); 1585 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1586 mhioc_inresvs_t *usrp, int flag); 1587 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1588 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1589 static int sd_mhdioc_release(dev_t dev); 1590 static int sd_mhdioc_register_devid(dev_t dev); 1591 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1592 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1593 1594 /* 1595 * SCSI removable prototypes 1596 */ 1597 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1598 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1599 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1600 static int sr_pause_resume(dev_t dev, int mode); 1601 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1602 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1603 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1604 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1605 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1606 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1610 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1611 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1612 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1613 static int sr_eject(dev_t dev); 1614 static void sr_ejected(register struct sd_lun *un); 1615 static int sr_check_wp(dev_t dev); 1616 static int sd_check_media(dev_t dev, enum dkio_state state); 1617 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1618 static void sd_delayed_cv_broadcast(void *arg); 1619 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1620 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1621 1622 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1623 1624 /* 1625 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1626 */ 1627 static void sd_check_for_writable_cd(struct sd_lun *un); 1628 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1629 static void sd_wm_cache_destructor(void *wm, void *un); 1630 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1631 daddr_t endb, ushort_t typ); 1632 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1633 daddr_t endb); 1634 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1635 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1636 static void sd_read_modify_write_task(void * arg); 1637 static int 1638 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1639 struct buf **bpp); 1640 1641 1642 /* 1643 * Function prototypes for failfast support. 1644 */ 1645 static void sd_failfast_flushq(struct sd_lun *un); 1646 static int sd_failfast_flushq_callback(struct buf *bp); 1647 1648 /* 1649 * Function prototypes to check for lsi devices 1650 */ 1651 static void sd_is_lsi(struct sd_lun *un); 1652 1653 /* 1654 * Function prototypes for x86 support 1655 */ 1656 #if defined(__i386) || defined(__amd64) 1657 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1658 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1659 #endif 1660 1661 /* 1662 * Constants for failfast support: 1663 * 1664 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1665 * failfast processing being performed. 1666 * 1667 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1668 * failfast processing on all bufs with B_FAILFAST set. 1669 */ 1670 1671 #define SD_FAILFAST_INACTIVE 0 1672 #define SD_FAILFAST_ACTIVE 1 1673 1674 /* 1675 * Bitmask to control behavior of buf(9S) flushes when a transition to 1676 * the failfast state occurs. Optional bits include: 1677 * 1678 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1679 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1680 * be flushed. 1681 * 1682 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1683 * driver, in addition to the regular wait queue. This includes the xbuf 1684 * queues. When clear, only the driver's wait queue will be flushed. 1685 */ 1686 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1687 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1688 1689 /* 1690 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1691 * to flush all queues within the driver. 1692 */ 1693 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1694 1695 1696 /* 1697 * SD Testing Fault Injection 1698 */ 1699 #ifdef SD_FAULT_INJECTION 1700 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1701 static void sd_faultinjection(struct scsi_pkt *pktp); 1702 static void sd_injection_log(char *buf, struct sd_lun *un); 1703 #endif 1704 1705 /* 1706 * Device driver ops vector 1707 */ 1708 static struct cb_ops sd_cb_ops = { 1709 sdopen, /* open */ 1710 sdclose, /* close */ 1711 sdstrategy, /* strategy */ 1712 nodev, /* print */ 1713 sddump, /* dump */ 1714 sdread, /* read */ 1715 sdwrite, /* write */ 1716 sdioctl, /* ioctl */ 1717 nodev, /* devmap */ 1718 nodev, /* mmap */ 1719 nodev, /* segmap */ 1720 nochpoll, /* poll */ 1721 sd_prop_op, /* cb_prop_op */ 1722 0, /* streamtab */ 1723 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1724 CB_REV, /* cb_rev */ 1725 sdaread, /* async I/O read entry point */ 1726 sdawrite /* async I/O write entry point */ 1727 }; 1728 1729 static struct dev_ops sd_ops = { 1730 DEVO_REV, /* devo_rev, */ 1731 0, /* refcnt */ 1732 sdinfo, /* info */ 1733 nulldev, /* identify */ 1734 sdprobe, /* probe */ 1735 sdattach, /* attach */ 1736 sddetach, /* detach */ 1737 nodev, /* reset */ 1738 &sd_cb_ops, /* driver operations */ 1739 NULL, /* bus operations */ 1740 sdpower /* power */ 1741 }; 1742 1743 1744 /* 1745 * This is the loadable module wrapper. 1746 */ 1747 #include <sys/modctl.h> 1748 1749 static struct modldrv modldrv = { 1750 &mod_driverops, /* Type of module. This one is a driver */ 1751 SD_MODULE_NAME, /* Module name. */ 1752 &sd_ops /* driver ops */ 1753 }; 1754 1755 1756 static struct modlinkage modlinkage = { 1757 MODREV_1, 1758 &modldrv, 1759 NULL 1760 }; 1761 1762 1763 static struct scsi_asq_key_strings sd_additional_codes[] = { 1764 0x81, 0, "Logical Unit is Reserved", 1765 0x85, 0, "Audio Address Not Valid", 1766 0xb6, 0, "Media Load Mechanism Failed", 1767 0xB9, 0, "Audio Play Operation Aborted", 1768 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1769 0x53, 2, "Medium removal prevented", 1770 0x6f, 0, "Authentication failed during key exchange", 1771 0x6f, 1, "Key not present", 1772 0x6f, 2, "Key not established", 1773 0x6f, 3, "Read without proper authentication", 1774 0x6f, 4, "Mismatched region to this logical unit", 1775 0x6f, 5, "Region reset count error", 1776 0xffff, 0x0, NULL 1777 }; 1778 1779 1780 /* 1781 * Struct for passing printing information for sense data messages 1782 */ 1783 struct sd_sense_info { 1784 int ssi_severity; 1785 int ssi_pfa_flag; 1786 }; 1787 1788 /* 1789 * Table of function pointers for iostart-side routines. Seperate "chains" 1790 * of layered function calls are formed by placing the function pointers 1791 * sequentially in the desired order. Functions are called according to an 1792 * incrementing table index ordering. The last function in each chain must 1793 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1794 * in the sd_iodone_chain[] array. 1795 * 1796 * Note: It may seem more natural to organize both the iostart and iodone 1797 * functions together, into an array of structures (or some similar 1798 * organization) with a common index, rather than two seperate arrays which 1799 * must be maintained in synchronization. The purpose of this division is 1800 * to achiece improved performance: individual arrays allows for more 1801 * effective cache line utilization on certain platforms. 1802 */ 1803 1804 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1805 1806 1807 static sd_chain_t sd_iostart_chain[] = { 1808 1809 /* Chain for buf IO for disk drive targets (PM enabled) */ 1810 sd_mapblockaddr_iostart, /* Index: 0 */ 1811 sd_pm_iostart, /* Index: 1 */ 1812 sd_core_iostart, /* Index: 2 */ 1813 1814 /* Chain for buf IO for disk drive targets (PM disabled) */ 1815 sd_mapblockaddr_iostart, /* Index: 3 */ 1816 sd_core_iostart, /* Index: 4 */ 1817 1818 /* Chain for buf IO for removable-media targets (PM enabled) */ 1819 sd_mapblockaddr_iostart, /* Index: 5 */ 1820 sd_mapblocksize_iostart, /* Index: 6 */ 1821 sd_pm_iostart, /* Index: 7 */ 1822 sd_core_iostart, /* Index: 8 */ 1823 1824 /* Chain for buf IO for removable-media targets (PM disabled) */ 1825 sd_mapblockaddr_iostart, /* Index: 9 */ 1826 sd_mapblocksize_iostart, /* Index: 10 */ 1827 sd_core_iostart, /* Index: 11 */ 1828 1829 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1830 sd_mapblockaddr_iostart, /* Index: 12 */ 1831 sd_checksum_iostart, /* Index: 13 */ 1832 sd_pm_iostart, /* Index: 14 */ 1833 sd_core_iostart, /* Index: 15 */ 1834 1835 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1836 sd_mapblockaddr_iostart, /* Index: 16 */ 1837 sd_checksum_iostart, /* Index: 17 */ 1838 sd_core_iostart, /* Index: 18 */ 1839 1840 /* Chain for USCSI commands (all targets) */ 1841 sd_pm_iostart, /* Index: 19 */ 1842 sd_core_iostart, /* Index: 20 */ 1843 1844 /* Chain for checksumming USCSI commands (all targets) */ 1845 sd_checksum_uscsi_iostart, /* Index: 21 */ 1846 sd_pm_iostart, /* Index: 22 */ 1847 sd_core_iostart, /* Index: 23 */ 1848 1849 /* Chain for "direct" USCSI commands (all targets) */ 1850 sd_core_iostart, /* Index: 24 */ 1851 1852 /* Chain for "direct priority" USCSI commands (all targets) */ 1853 sd_core_iostart, /* Index: 25 */ 1854 }; 1855 1856 /* 1857 * Macros to locate the first function of each iostart chain in the 1858 * sd_iostart_chain[] array. These are located by the index in the array. 1859 */ 1860 #define SD_CHAIN_DISK_IOSTART 0 1861 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1862 #define SD_CHAIN_RMMEDIA_IOSTART 5 1863 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1864 #define SD_CHAIN_CHKSUM_IOSTART 12 1865 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1866 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1867 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1868 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1869 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1870 1871 1872 /* 1873 * Table of function pointers for the iodone-side routines for the driver- 1874 * internal layering mechanism. The calling sequence for iodone routines 1875 * uses a decrementing table index, so the last routine called in a chain 1876 * must be at the lowest array index location for that chain. The last 1877 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1878 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1879 * of the functions in an iodone side chain must correspond to the ordering 1880 * of the iostart routines for that chain. Note that there is no iodone 1881 * side routine that corresponds to sd_core_iostart(), so there is no 1882 * entry in the table for this. 1883 */ 1884 1885 static sd_chain_t sd_iodone_chain[] = { 1886 1887 /* Chain for buf IO for disk drive targets (PM enabled) */ 1888 sd_buf_iodone, /* Index: 0 */ 1889 sd_mapblockaddr_iodone, /* Index: 1 */ 1890 sd_pm_iodone, /* Index: 2 */ 1891 1892 /* Chain for buf IO for disk drive targets (PM disabled) */ 1893 sd_buf_iodone, /* Index: 3 */ 1894 sd_mapblockaddr_iodone, /* Index: 4 */ 1895 1896 /* Chain for buf IO for removable-media targets (PM enabled) */ 1897 sd_buf_iodone, /* Index: 5 */ 1898 sd_mapblockaddr_iodone, /* Index: 6 */ 1899 sd_mapblocksize_iodone, /* Index: 7 */ 1900 sd_pm_iodone, /* Index: 8 */ 1901 1902 /* Chain for buf IO for removable-media targets (PM disabled) */ 1903 sd_buf_iodone, /* Index: 9 */ 1904 sd_mapblockaddr_iodone, /* Index: 10 */ 1905 sd_mapblocksize_iodone, /* Index: 11 */ 1906 1907 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1908 sd_buf_iodone, /* Index: 12 */ 1909 sd_mapblockaddr_iodone, /* Index: 13 */ 1910 sd_checksum_iodone, /* Index: 14 */ 1911 sd_pm_iodone, /* Index: 15 */ 1912 1913 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1914 sd_buf_iodone, /* Index: 16 */ 1915 sd_mapblockaddr_iodone, /* Index: 17 */ 1916 sd_checksum_iodone, /* Index: 18 */ 1917 1918 /* Chain for USCSI commands (non-checksum targets) */ 1919 sd_uscsi_iodone, /* Index: 19 */ 1920 sd_pm_iodone, /* Index: 20 */ 1921 1922 /* Chain for USCSI commands (checksum targets) */ 1923 sd_uscsi_iodone, /* Index: 21 */ 1924 sd_checksum_uscsi_iodone, /* Index: 22 */ 1925 sd_pm_iodone, /* Index: 22 */ 1926 1927 /* Chain for "direct" USCSI commands (all targets) */ 1928 sd_uscsi_iodone, /* Index: 24 */ 1929 1930 /* Chain for "direct priority" USCSI commands (all targets) */ 1931 sd_uscsi_iodone, /* Index: 25 */ 1932 }; 1933 1934 1935 /* 1936 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1937 * each iodone-side chain. These are located by the array index, but as the 1938 * iodone side functions are called in a decrementing-index order, the 1939 * highest index number in each chain must be specified (as these correspond 1940 * to the first function in the iodone chain that will be called by the core 1941 * at IO completion time). 1942 */ 1943 1944 #define SD_CHAIN_DISK_IODONE 2 1945 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1946 #define SD_CHAIN_RMMEDIA_IODONE 8 1947 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1948 #define SD_CHAIN_CHKSUM_IODONE 15 1949 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1950 #define SD_CHAIN_USCSI_CMD_IODONE 20 1951 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1952 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1953 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1954 1955 1956 1957 1958 /* 1959 * Array to map a layering chain index to the appropriate initpkt routine. 1960 * The redundant entries are present so that the index used for accessing 1961 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1962 * with this table as well. 1963 */ 1964 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1965 1966 static sd_initpkt_t sd_initpkt_map[] = { 1967 1968 /* Chain for buf IO for disk drive targets (PM enabled) */ 1969 sd_initpkt_for_buf, /* Index: 0 */ 1970 sd_initpkt_for_buf, /* Index: 1 */ 1971 sd_initpkt_for_buf, /* Index: 2 */ 1972 1973 /* Chain for buf IO for disk drive targets (PM disabled) */ 1974 sd_initpkt_for_buf, /* Index: 3 */ 1975 sd_initpkt_for_buf, /* Index: 4 */ 1976 1977 /* Chain for buf IO for removable-media targets (PM enabled) */ 1978 sd_initpkt_for_buf, /* Index: 5 */ 1979 sd_initpkt_for_buf, /* Index: 6 */ 1980 sd_initpkt_for_buf, /* Index: 7 */ 1981 sd_initpkt_for_buf, /* Index: 8 */ 1982 1983 /* Chain for buf IO for removable-media targets (PM disabled) */ 1984 sd_initpkt_for_buf, /* Index: 9 */ 1985 sd_initpkt_for_buf, /* Index: 10 */ 1986 sd_initpkt_for_buf, /* Index: 11 */ 1987 1988 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1989 sd_initpkt_for_buf, /* Index: 12 */ 1990 sd_initpkt_for_buf, /* Index: 13 */ 1991 sd_initpkt_for_buf, /* Index: 14 */ 1992 sd_initpkt_for_buf, /* Index: 15 */ 1993 1994 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1995 sd_initpkt_for_buf, /* Index: 16 */ 1996 sd_initpkt_for_buf, /* Index: 17 */ 1997 sd_initpkt_for_buf, /* Index: 18 */ 1998 1999 /* Chain for USCSI commands (non-checksum targets) */ 2000 sd_initpkt_for_uscsi, /* Index: 19 */ 2001 sd_initpkt_for_uscsi, /* Index: 20 */ 2002 2003 /* Chain for USCSI commands (checksum targets) */ 2004 sd_initpkt_for_uscsi, /* Index: 21 */ 2005 sd_initpkt_for_uscsi, /* Index: 22 */ 2006 sd_initpkt_for_uscsi, /* Index: 22 */ 2007 2008 /* Chain for "direct" USCSI commands (all targets) */ 2009 sd_initpkt_for_uscsi, /* Index: 24 */ 2010 2011 /* Chain for "direct priority" USCSI commands (all targets) */ 2012 sd_initpkt_for_uscsi, /* Index: 25 */ 2013 2014 }; 2015 2016 2017 /* 2018 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2019 * The redundant entries are present so that the index used for accessing 2020 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2021 * with this table as well. 2022 */ 2023 typedef void (*sd_destroypkt_t)(struct buf *); 2024 2025 static sd_destroypkt_t sd_destroypkt_map[] = { 2026 2027 /* Chain for buf IO for disk drive targets (PM enabled) */ 2028 sd_destroypkt_for_buf, /* Index: 0 */ 2029 sd_destroypkt_for_buf, /* Index: 1 */ 2030 sd_destroypkt_for_buf, /* Index: 2 */ 2031 2032 /* Chain for buf IO for disk drive targets (PM disabled) */ 2033 sd_destroypkt_for_buf, /* Index: 3 */ 2034 sd_destroypkt_for_buf, /* Index: 4 */ 2035 2036 /* Chain for buf IO for removable-media targets (PM enabled) */ 2037 sd_destroypkt_for_buf, /* Index: 5 */ 2038 sd_destroypkt_for_buf, /* Index: 6 */ 2039 sd_destroypkt_for_buf, /* Index: 7 */ 2040 sd_destroypkt_for_buf, /* Index: 8 */ 2041 2042 /* Chain for buf IO for removable-media targets (PM disabled) */ 2043 sd_destroypkt_for_buf, /* Index: 9 */ 2044 sd_destroypkt_for_buf, /* Index: 10 */ 2045 sd_destroypkt_for_buf, /* Index: 11 */ 2046 2047 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2048 sd_destroypkt_for_buf, /* Index: 12 */ 2049 sd_destroypkt_for_buf, /* Index: 13 */ 2050 sd_destroypkt_for_buf, /* Index: 14 */ 2051 sd_destroypkt_for_buf, /* Index: 15 */ 2052 2053 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2054 sd_destroypkt_for_buf, /* Index: 16 */ 2055 sd_destroypkt_for_buf, /* Index: 17 */ 2056 sd_destroypkt_for_buf, /* Index: 18 */ 2057 2058 /* Chain for USCSI commands (non-checksum targets) */ 2059 sd_destroypkt_for_uscsi, /* Index: 19 */ 2060 sd_destroypkt_for_uscsi, /* Index: 20 */ 2061 2062 /* Chain for USCSI commands (checksum targets) */ 2063 sd_destroypkt_for_uscsi, /* Index: 21 */ 2064 sd_destroypkt_for_uscsi, /* Index: 22 */ 2065 sd_destroypkt_for_uscsi, /* Index: 22 */ 2066 2067 /* Chain for "direct" USCSI commands (all targets) */ 2068 sd_destroypkt_for_uscsi, /* Index: 24 */ 2069 2070 /* Chain for "direct priority" USCSI commands (all targets) */ 2071 sd_destroypkt_for_uscsi, /* Index: 25 */ 2072 2073 }; 2074 2075 2076 2077 /* 2078 * Array to map a layering chain index to the appropriate chain "type". 2079 * The chain type indicates a specific property/usage of the chain. 2080 * The redundant entries are present so that the index used for accessing 2081 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2082 * with this table as well. 2083 */ 2084 2085 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2086 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2087 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2088 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2089 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2090 /* (for error recovery) */ 2091 2092 static int sd_chain_type_map[] = { 2093 2094 /* Chain for buf IO for disk drive targets (PM enabled) */ 2095 SD_CHAIN_BUFIO, /* Index: 0 */ 2096 SD_CHAIN_BUFIO, /* Index: 1 */ 2097 SD_CHAIN_BUFIO, /* Index: 2 */ 2098 2099 /* Chain for buf IO for disk drive targets (PM disabled) */ 2100 SD_CHAIN_BUFIO, /* Index: 3 */ 2101 SD_CHAIN_BUFIO, /* Index: 4 */ 2102 2103 /* Chain for buf IO for removable-media targets (PM enabled) */ 2104 SD_CHAIN_BUFIO, /* Index: 5 */ 2105 SD_CHAIN_BUFIO, /* Index: 6 */ 2106 SD_CHAIN_BUFIO, /* Index: 7 */ 2107 SD_CHAIN_BUFIO, /* Index: 8 */ 2108 2109 /* Chain for buf IO for removable-media targets (PM disabled) */ 2110 SD_CHAIN_BUFIO, /* Index: 9 */ 2111 SD_CHAIN_BUFIO, /* Index: 10 */ 2112 SD_CHAIN_BUFIO, /* Index: 11 */ 2113 2114 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2115 SD_CHAIN_BUFIO, /* Index: 12 */ 2116 SD_CHAIN_BUFIO, /* Index: 13 */ 2117 SD_CHAIN_BUFIO, /* Index: 14 */ 2118 SD_CHAIN_BUFIO, /* Index: 15 */ 2119 2120 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2121 SD_CHAIN_BUFIO, /* Index: 16 */ 2122 SD_CHAIN_BUFIO, /* Index: 17 */ 2123 SD_CHAIN_BUFIO, /* Index: 18 */ 2124 2125 /* Chain for USCSI commands (non-checksum targets) */ 2126 SD_CHAIN_USCSI, /* Index: 19 */ 2127 SD_CHAIN_USCSI, /* Index: 20 */ 2128 2129 /* Chain for USCSI commands (checksum targets) */ 2130 SD_CHAIN_USCSI, /* Index: 21 */ 2131 SD_CHAIN_USCSI, /* Index: 22 */ 2132 SD_CHAIN_USCSI, /* Index: 22 */ 2133 2134 /* Chain for "direct" USCSI commands (all targets) */ 2135 SD_CHAIN_DIRECT, /* Index: 24 */ 2136 2137 /* Chain for "direct priority" USCSI commands (all targets) */ 2138 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2139 }; 2140 2141 2142 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2143 #define SD_IS_BUFIO(xp) \ 2144 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2145 2146 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2147 #define SD_IS_DIRECT_PRIORITY(xp) \ 2148 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2149 2150 2151 2152 /* 2153 * Struct, array, and macros to map a specific chain to the appropriate 2154 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2155 * 2156 * The sd_chain_index_map[] array is used at attach time to set the various 2157 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2158 * chain to be used with the instance. This allows different instances to use 2159 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2160 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2161 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2162 * dynamically & without the use of locking; and (2) a layer may update the 2163 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2164 * to allow for deferred processing of an IO within the same chain from a 2165 * different execution context. 2166 */ 2167 2168 struct sd_chain_index { 2169 int sci_iostart_index; 2170 int sci_iodone_index; 2171 }; 2172 2173 static struct sd_chain_index sd_chain_index_map[] = { 2174 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2175 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2176 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2177 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2178 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2179 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2180 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2181 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2182 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2183 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2184 }; 2185 2186 2187 /* 2188 * The following are indexes into the sd_chain_index_map[] array. 2189 */ 2190 2191 /* un->un_buf_chain_type must be set to one of these */ 2192 #define SD_CHAIN_INFO_DISK 0 2193 #define SD_CHAIN_INFO_DISK_NO_PM 1 2194 #define SD_CHAIN_INFO_RMMEDIA 2 2195 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2196 #define SD_CHAIN_INFO_CHKSUM 4 2197 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2198 2199 /* un->un_uscsi_chain_type must be set to one of these */ 2200 #define SD_CHAIN_INFO_USCSI_CMD 6 2201 /* USCSI with PM disabled is the same as DIRECT */ 2202 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2203 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2204 2205 /* un->un_direct_chain_type must be set to one of these */ 2206 #define SD_CHAIN_INFO_DIRECT_CMD 8 2207 2208 /* un->un_priority_chain_type must be set to one of these */ 2209 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2210 2211 /* size for devid inquiries */ 2212 #define MAX_INQUIRY_SIZE 0xF0 2213 2214 /* 2215 * Macros used by functions to pass a given buf(9S) struct along to the 2216 * next function in the layering chain for further processing. 2217 * 2218 * In the following macros, passing more than three arguments to the called 2219 * routines causes the optimizer for the SPARC compiler to stop doing tail 2220 * call elimination which results in significant performance degradation. 2221 */ 2222 #define SD_BEGIN_IOSTART(index, un, bp) \ 2223 ((*(sd_iostart_chain[index]))(index, un, bp)) 2224 2225 #define SD_BEGIN_IODONE(index, un, bp) \ 2226 ((*(sd_iodone_chain[index]))(index, un, bp)) 2227 2228 #define SD_NEXT_IOSTART(index, un, bp) \ 2229 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2230 2231 #define SD_NEXT_IODONE(index, un, bp) \ 2232 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2233 2234 /* 2235 * Function: _init 2236 * 2237 * Description: This is the driver _init(9E) entry point. 2238 * 2239 * Return Code: Returns the value from mod_install(9F) or 2240 * ddi_soft_state_init(9F) as appropriate. 2241 * 2242 * Context: Called when driver module loaded. 2243 */ 2244 2245 int 2246 _init(void) 2247 { 2248 int err; 2249 2250 /* establish driver name from module name */ 2251 sd_label = mod_modname(&modlinkage); 2252 2253 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2254 SD_MAXUNIT); 2255 2256 if (err != 0) { 2257 return (err); 2258 } 2259 2260 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2261 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2262 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2263 2264 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2265 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2266 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2267 2268 /* 2269 * it's ok to init here even for fibre device 2270 */ 2271 sd_scsi_probe_cache_init(); 2272 2273 sd_scsi_target_lun_init(); 2274 2275 /* 2276 * Creating taskq before mod_install ensures that all callers (threads) 2277 * that enter the module after a successfull mod_install encounter 2278 * a valid taskq. 2279 */ 2280 sd_taskq_create(); 2281 2282 err = mod_install(&modlinkage); 2283 if (err != 0) { 2284 /* delete taskq if install fails */ 2285 sd_taskq_delete(); 2286 2287 mutex_destroy(&sd_detach_mutex); 2288 mutex_destroy(&sd_log_mutex); 2289 mutex_destroy(&sd_label_mutex); 2290 2291 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2292 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2293 cv_destroy(&sd_tr.srq_inprocess_cv); 2294 2295 sd_scsi_probe_cache_fini(); 2296 2297 sd_scsi_target_lun_fini(); 2298 2299 ddi_soft_state_fini(&sd_state); 2300 return (err); 2301 } 2302 2303 return (err); 2304 } 2305 2306 2307 /* 2308 * Function: _fini 2309 * 2310 * Description: This is the driver _fini(9E) entry point. 2311 * 2312 * Return Code: Returns the value from mod_remove(9F) 2313 * 2314 * Context: Called when driver module is unloaded. 2315 */ 2316 2317 int 2318 _fini(void) 2319 { 2320 int err; 2321 2322 if ((err = mod_remove(&modlinkage)) != 0) { 2323 return (err); 2324 } 2325 2326 sd_taskq_delete(); 2327 2328 mutex_destroy(&sd_detach_mutex); 2329 mutex_destroy(&sd_log_mutex); 2330 mutex_destroy(&sd_label_mutex); 2331 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2332 2333 sd_scsi_probe_cache_fini(); 2334 2335 sd_scsi_target_lun_fini(); 2336 2337 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2338 cv_destroy(&sd_tr.srq_inprocess_cv); 2339 2340 ddi_soft_state_fini(&sd_state); 2341 2342 return (err); 2343 } 2344 2345 2346 /* 2347 * Function: _info 2348 * 2349 * Description: This is the driver _info(9E) entry point. 2350 * 2351 * Arguments: modinfop - pointer to the driver modinfo structure 2352 * 2353 * Return Code: Returns the value from mod_info(9F). 2354 * 2355 * Context: Kernel thread context 2356 */ 2357 2358 int 2359 _info(struct modinfo *modinfop) 2360 { 2361 return (mod_info(&modlinkage, modinfop)); 2362 } 2363 2364 2365 /* 2366 * The following routines implement the driver message logging facility. 2367 * They provide component- and level- based debug output filtering. 2368 * Output may also be restricted to messages for a single instance by 2369 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2370 * to NULL, then messages for all instances are printed. 2371 * 2372 * These routines have been cloned from each other due to the language 2373 * constraints of macros and variable argument list processing. 2374 */ 2375 2376 2377 /* 2378 * Function: sd_log_err 2379 * 2380 * Description: This routine is called by the SD_ERROR macro for debug 2381 * logging of error conditions. 2382 * 2383 * Arguments: comp - driver component being logged 2384 * dev - pointer to driver info structure 2385 * fmt - error string and format to be logged 2386 */ 2387 2388 static void 2389 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2390 { 2391 va_list ap; 2392 dev_info_t *dev; 2393 2394 ASSERT(un != NULL); 2395 dev = SD_DEVINFO(un); 2396 ASSERT(dev != NULL); 2397 2398 /* 2399 * Filter messages based on the global component and level masks. 2400 * Also print if un matches the value of sd_debug_un, or if 2401 * sd_debug_un is set to NULL. 2402 */ 2403 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2404 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2405 mutex_enter(&sd_log_mutex); 2406 va_start(ap, fmt); 2407 (void) vsprintf(sd_log_buf, fmt, ap); 2408 va_end(ap); 2409 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2410 mutex_exit(&sd_log_mutex); 2411 } 2412 #ifdef SD_FAULT_INJECTION 2413 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2414 if (un->sd_injection_mask & comp) { 2415 mutex_enter(&sd_log_mutex); 2416 va_start(ap, fmt); 2417 (void) vsprintf(sd_log_buf, fmt, ap); 2418 va_end(ap); 2419 sd_injection_log(sd_log_buf, un); 2420 mutex_exit(&sd_log_mutex); 2421 } 2422 #endif 2423 } 2424 2425 2426 /* 2427 * Function: sd_log_info 2428 * 2429 * Description: This routine is called by the SD_INFO macro for debug 2430 * logging of general purpose informational conditions. 2431 * 2432 * Arguments: comp - driver component being logged 2433 * dev - pointer to driver info structure 2434 * fmt - info string and format to be logged 2435 */ 2436 2437 static void 2438 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2439 { 2440 va_list ap; 2441 dev_info_t *dev; 2442 2443 ASSERT(un != NULL); 2444 dev = SD_DEVINFO(un); 2445 ASSERT(dev != NULL); 2446 2447 /* 2448 * Filter messages based on the global component and level masks. 2449 * Also print if un matches the value of sd_debug_un, or if 2450 * sd_debug_un is set to NULL. 2451 */ 2452 if ((sd_component_mask & component) && 2453 (sd_level_mask & SD_LOGMASK_INFO) && 2454 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2455 mutex_enter(&sd_log_mutex); 2456 va_start(ap, fmt); 2457 (void) vsprintf(sd_log_buf, fmt, ap); 2458 va_end(ap); 2459 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2460 mutex_exit(&sd_log_mutex); 2461 } 2462 #ifdef SD_FAULT_INJECTION 2463 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2464 if (un->sd_injection_mask & component) { 2465 mutex_enter(&sd_log_mutex); 2466 va_start(ap, fmt); 2467 (void) vsprintf(sd_log_buf, fmt, ap); 2468 va_end(ap); 2469 sd_injection_log(sd_log_buf, un); 2470 mutex_exit(&sd_log_mutex); 2471 } 2472 #endif 2473 } 2474 2475 2476 /* 2477 * Function: sd_log_trace 2478 * 2479 * Description: This routine is called by the SD_TRACE macro for debug 2480 * logging of trace conditions (i.e. function entry/exit). 2481 * 2482 * Arguments: comp - driver component being logged 2483 * dev - pointer to driver info structure 2484 * fmt - trace string and format to be logged 2485 */ 2486 2487 static void 2488 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2489 { 2490 va_list ap; 2491 dev_info_t *dev; 2492 2493 ASSERT(un != NULL); 2494 dev = SD_DEVINFO(un); 2495 ASSERT(dev != NULL); 2496 2497 /* 2498 * Filter messages based on the global component and level masks. 2499 * Also print if un matches the value of sd_debug_un, or if 2500 * sd_debug_un is set to NULL. 2501 */ 2502 if ((sd_component_mask & component) && 2503 (sd_level_mask & SD_LOGMASK_TRACE) && 2504 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2505 mutex_enter(&sd_log_mutex); 2506 va_start(ap, fmt); 2507 (void) vsprintf(sd_log_buf, fmt, ap); 2508 va_end(ap); 2509 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2510 mutex_exit(&sd_log_mutex); 2511 } 2512 #ifdef SD_FAULT_INJECTION 2513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2514 if (un->sd_injection_mask & component) { 2515 mutex_enter(&sd_log_mutex); 2516 va_start(ap, fmt); 2517 (void) vsprintf(sd_log_buf, fmt, ap); 2518 va_end(ap); 2519 sd_injection_log(sd_log_buf, un); 2520 mutex_exit(&sd_log_mutex); 2521 } 2522 #endif 2523 } 2524 2525 2526 /* 2527 * Function: sdprobe 2528 * 2529 * Description: This is the driver probe(9e) entry point function. 2530 * 2531 * Arguments: devi - opaque device info handle 2532 * 2533 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2534 * DDI_PROBE_FAILURE: If the probe failed. 2535 * DDI_PROBE_PARTIAL: If the instance is not present now, 2536 * but may be present in the future. 2537 */ 2538 2539 static int 2540 sdprobe(dev_info_t *devi) 2541 { 2542 struct scsi_device *devp; 2543 int rval; 2544 int instance; 2545 2546 /* 2547 * if it wasn't for pln, sdprobe could actually be nulldev 2548 * in the "__fibre" case. 2549 */ 2550 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2551 return (DDI_PROBE_DONTCARE); 2552 } 2553 2554 devp = ddi_get_driver_private(devi); 2555 2556 if (devp == NULL) { 2557 /* Ooops... nexus driver is mis-configured... */ 2558 return (DDI_PROBE_FAILURE); 2559 } 2560 2561 instance = ddi_get_instance(devi); 2562 2563 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2564 return (DDI_PROBE_PARTIAL); 2565 } 2566 2567 /* 2568 * Call the SCSA utility probe routine to see if we actually 2569 * have a target at this SCSI nexus. 2570 */ 2571 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2572 case SCSIPROBE_EXISTS: 2573 switch (devp->sd_inq->inq_dtype) { 2574 case DTYPE_DIRECT: 2575 rval = DDI_PROBE_SUCCESS; 2576 break; 2577 case DTYPE_RODIRECT: 2578 /* CDs etc. Can be removable media */ 2579 rval = DDI_PROBE_SUCCESS; 2580 break; 2581 case DTYPE_OPTICAL: 2582 /* 2583 * Rewritable optical driver HP115AA 2584 * Can also be removable media 2585 */ 2586 2587 /* 2588 * Do not attempt to bind to DTYPE_OPTICAL if 2589 * pre solaris 9 sparc sd behavior is required 2590 * 2591 * If first time through and sd_dtype_optical_bind 2592 * has not been set in /etc/system check properties 2593 */ 2594 2595 if (sd_dtype_optical_bind < 0) { 2596 sd_dtype_optical_bind = ddi_prop_get_int 2597 (DDI_DEV_T_ANY, devi, 0, 2598 "optical-device-bind", 1); 2599 } 2600 2601 if (sd_dtype_optical_bind == 0) { 2602 rval = DDI_PROBE_FAILURE; 2603 } else { 2604 rval = DDI_PROBE_SUCCESS; 2605 } 2606 break; 2607 2608 case DTYPE_NOTPRESENT: 2609 default: 2610 rval = DDI_PROBE_FAILURE; 2611 break; 2612 } 2613 break; 2614 default: 2615 rval = DDI_PROBE_PARTIAL; 2616 break; 2617 } 2618 2619 /* 2620 * This routine checks for resource allocation prior to freeing, 2621 * so it will take care of the "smart probing" case where a 2622 * scsi_probe() may or may not have been issued and will *not* 2623 * free previously-freed resources. 2624 */ 2625 scsi_unprobe(devp); 2626 return (rval); 2627 } 2628 2629 2630 /* 2631 * Function: sdinfo 2632 * 2633 * Description: This is the driver getinfo(9e) entry point function. 2634 * Given the device number, return the devinfo pointer from 2635 * the scsi_device structure or the instance number 2636 * associated with the dev_t. 2637 * 2638 * Arguments: dip - pointer to device info structure 2639 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2640 * DDI_INFO_DEVT2INSTANCE) 2641 * arg - driver dev_t 2642 * resultp - user buffer for request response 2643 * 2644 * Return Code: DDI_SUCCESS 2645 * DDI_FAILURE 2646 */ 2647 /* ARGSUSED */ 2648 static int 2649 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2650 { 2651 struct sd_lun *un; 2652 dev_t dev; 2653 int instance; 2654 int error; 2655 2656 switch (infocmd) { 2657 case DDI_INFO_DEVT2DEVINFO: 2658 dev = (dev_t)arg; 2659 instance = SDUNIT(dev); 2660 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2661 return (DDI_FAILURE); 2662 } 2663 *result = (void *) SD_DEVINFO(un); 2664 error = DDI_SUCCESS; 2665 break; 2666 case DDI_INFO_DEVT2INSTANCE: 2667 dev = (dev_t)arg; 2668 instance = SDUNIT(dev); 2669 *result = (void *)(uintptr_t)instance; 2670 error = DDI_SUCCESS; 2671 break; 2672 default: 2673 error = DDI_FAILURE; 2674 } 2675 return (error); 2676 } 2677 2678 /* 2679 * Function: sd_prop_op 2680 * 2681 * Description: This is the driver prop_op(9e) entry point function. 2682 * Return the number of blocks for the partition in question 2683 * or forward the request to the property facilities. 2684 * 2685 * Arguments: dev - device number 2686 * dip - pointer to device info structure 2687 * prop_op - property operator 2688 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2689 * name - pointer to property name 2690 * valuep - pointer or address of the user buffer 2691 * lengthp - property length 2692 * 2693 * Return Code: DDI_PROP_SUCCESS 2694 * DDI_PROP_NOT_FOUND 2695 * DDI_PROP_UNDEFINED 2696 * DDI_PROP_NO_MEMORY 2697 * DDI_PROP_BUF_TOO_SMALL 2698 */ 2699 2700 static int 2701 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2702 char *name, caddr_t valuep, int *lengthp) 2703 { 2704 int instance = ddi_get_instance(dip); 2705 struct sd_lun *un; 2706 uint64_t nblocks64; 2707 2708 /* 2709 * Our dynamic properties are all device specific and size oriented. 2710 * Requests issued under conditions where size is valid are passed 2711 * to ddi_prop_op_nblocks with the size information, otherwise the 2712 * request is passed to ddi_prop_op. Size depends on valid geometry. 2713 */ 2714 un = ddi_get_soft_state(sd_state, instance); 2715 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2716 (un->un_f_geometry_is_valid == FALSE)) { 2717 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2718 name, valuep, lengthp)); 2719 } else { 2720 /* get nblocks value */ 2721 ASSERT(!mutex_owned(SD_MUTEX(un))); 2722 mutex_enter(SD_MUTEX(un)); 2723 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2724 mutex_exit(SD_MUTEX(un)); 2725 2726 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2727 name, valuep, lengthp, nblocks64)); 2728 } 2729 } 2730 2731 /* 2732 * The following functions are for smart probing: 2733 * sd_scsi_probe_cache_init() 2734 * sd_scsi_probe_cache_fini() 2735 * sd_scsi_clear_probe_cache() 2736 * sd_scsi_probe_with_cache() 2737 */ 2738 2739 /* 2740 * Function: sd_scsi_probe_cache_init 2741 * 2742 * Description: Initializes the probe response cache mutex and head pointer. 2743 * 2744 * Context: Kernel thread context 2745 */ 2746 2747 static void 2748 sd_scsi_probe_cache_init(void) 2749 { 2750 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2751 sd_scsi_probe_cache_head = NULL; 2752 } 2753 2754 2755 /* 2756 * Function: sd_scsi_probe_cache_fini 2757 * 2758 * Description: Frees all resources associated with the probe response cache. 2759 * 2760 * Context: Kernel thread context 2761 */ 2762 2763 static void 2764 sd_scsi_probe_cache_fini(void) 2765 { 2766 struct sd_scsi_probe_cache *cp; 2767 struct sd_scsi_probe_cache *ncp; 2768 2769 /* Clean up our smart probing linked list */ 2770 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2771 ncp = cp->next; 2772 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2773 } 2774 sd_scsi_probe_cache_head = NULL; 2775 mutex_destroy(&sd_scsi_probe_cache_mutex); 2776 } 2777 2778 2779 /* 2780 * Function: sd_scsi_clear_probe_cache 2781 * 2782 * Description: This routine clears the probe response cache. This is 2783 * done when open() returns ENXIO so that when deferred 2784 * attach is attempted (possibly after a device has been 2785 * turned on) we will retry the probe. Since we don't know 2786 * which target we failed to open, we just clear the 2787 * entire cache. 2788 * 2789 * Context: Kernel thread context 2790 */ 2791 2792 static void 2793 sd_scsi_clear_probe_cache(void) 2794 { 2795 struct sd_scsi_probe_cache *cp; 2796 int i; 2797 2798 mutex_enter(&sd_scsi_probe_cache_mutex); 2799 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2800 /* 2801 * Reset all entries to SCSIPROBE_EXISTS. This will 2802 * force probing to be performed the next time 2803 * sd_scsi_probe_with_cache is called. 2804 */ 2805 for (i = 0; i < NTARGETS_WIDE; i++) { 2806 cp->cache[i] = SCSIPROBE_EXISTS; 2807 } 2808 } 2809 mutex_exit(&sd_scsi_probe_cache_mutex); 2810 } 2811 2812 2813 /* 2814 * Function: sd_scsi_probe_with_cache 2815 * 2816 * Description: This routine implements support for a scsi device probe 2817 * with cache. The driver maintains a cache of the target 2818 * responses to scsi probes. If we get no response from a 2819 * target during a probe inquiry, we remember that, and we 2820 * avoid additional calls to scsi_probe on non-zero LUNs 2821 * on the same target until the cache is cleared. By doing 2822 * so we avoid the 1/4 sec selection timeout for nonzero 2823 * LUNs. lun0 of a target is always probed. 2824 * 2825 * Arguments: devp - Pointer to a scsi_device(9S) structure 2826 * waitfunc - indicates what the allocator routines should 2827 * do when resources are not available. This value 2828 * is passed on to scsi_probe() when that routine 2829 * is called. 2830 * 2831 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2832 * otherwise the value returned by scsi_probe(9F). 2833 * 2834 * Context: Kernel thread context 2835 */ 2836 2837 static int 2838 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2839 { 2840 struct sd_scsi_probe_cache *cp; 2841 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2842 int lun, tgt; 2843 2844 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2845 SCSI_ADDR_PROP_LUN, 0); 2846 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2847 SCSI_ADDR_PROP_TARGET, -1); 2848 2849 /* Make sure caching enabled and target in range */ 2850 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2851 /* do it the old way (no cache) */ 2852 return (scsi_probe(devp, waitfn)); 2853 } 2854 2855 mutex_enter(&sd_scsi_probe_cache_mutex); 2856 2857 /* Find the cache for this scsi bus instance */ 2858 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2859 if (cp->pdip == pdip) { 2860 break; 2861 } 2862 } 2863 2864 /* If we can't find a cache for this pdip, create one */ 2865 if (cp == NULL) { 2866 int i; 2867 2868 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2869 KM_SLEEP); 2870 cp->pdip = pdip; 2871 cp->next = sd_scsi_probe_cache_head; 2872 sd_scsi_probe_cache_head = cp; 2873 for (i = 0; i < NTARGETS_WIDE; i++) { 2874 cp->cache[i] = SCSIPROBE_EXISTS; 2875 } 2876 } 2877 2878 mutex_exit(&sd_scsi_probe_cache_mutex); 2879 2880 /* Recompute the cache for this target if LUN zero */ 2881 if (lun == 0) { 2882 cp->cache[tgt] = SCSIPROBE_EXISTS; 2883 } 2884 2885 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2886 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2887 return (SCSIPROBE_NORESP); 2888 } 2889 2890 /* Do the actual probe; save & return the result */ 2891 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2892 } 2893 2894 2895 /* 2896 * Function: sd_scsi_target_lun_init 2897 * 2898 * Description: Initializes the attached lun chain mutex and head pointer. 2899 * 2900 * Context: Kernel thread context 2901 */ 2902 2903 static void 2904 sd_scsi_target_lun_init(void) 2905 { 2906 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2907 sd_scsi_target_lun_head = NULL; 2908 } 2909 2910 2911 /* 2912 * Function: sd_scsi_target_lun_fini 2913 * 2914 * Description: Frees all resources associated with the attached lun 2915 * chain 2916 * 2917 * Context: Kernel thread context 2918 */ 2919 2920 static void 2921 sd_scsi_target_lun_fini(void) 2922 { 2923 struct sd_scsi_hba_tgt_lun *cp; 2924 struct sd_scsi_hba_tgt_lun *ncp; 2925 2926 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2927 ncp = cp->next; 2928 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2929 } 2930 sd_scsi_target_lun_head = NULL; 2931 mutex_destroy(&sd_scsi_target_lun_mutex); 2932 } 2933 2934 2935 /* 2936 * Function: sd_scsi_get_target_lun_count 2937 * 2938 * Description: This routine will check in the attached lun chain to see 2939 * how many luns are attached on the required SCSI controller 2940 * and target. Currently, some capabilities like tagged queue 2941 * are supported per target based by HBA. So all luns in a 2942 * target have the same capabilities. Based on this assumption, 2943 * sd should only set these capabilities once per target. This 2944 * function is called when sd needs to decide how many luns 2945 * already attached on a target. 2946 * 2947 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2948 * controller device. 2949 * target - The target ID on the controller's SCSI bus. 2950 * 2951 * Return Code: The number of luns attached on the required target and 2952 * controller. 2953 * -1 if target ID is not in parallel SCSI scope or the given 2954 * dip is not in the chain. 2955 * 2956 * Context: Kernel thread context 2957 */ 2958 2959 static int 2960 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2961 { 2962 struct sd_scsi_hba_tgt_lun *cp; 2963 2964 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2965 return (-1); 2966 } 2967 2968 mutex_enter(&sd_scsi_target_lun_mutex); 2969 2970 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2971 if (cp->pdip == dip) { 2972 break; 2973 } 2974 } 2975 2976 mutex_exit(&sd_scsi_target_lun_mutex); 2977 2978 if (cp == NULL) { 2979 return (-1); 2980 } 2981 2982 return (cp->nlun[target]); 2983 } 2984 2985 2986 /* 2987 * Function: sd_scsi_update_lun_on_target 2988 * 2989 * Description: This routine is used to update the attached lun chain when a 2990 * lun is attached or detached on a target. 2991 * 2992 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2993 * controller device. 2994 * target - The target ID on the controller's SCSI bus. 2995 * flag - Indicate the lun is attached or detached. 2996 * 2997 * Context: Kernel thread context 2998 */ 2999 3000 static void 3001 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3002 { 3003 struct sd_scsi_hba_tgt_lun *cp; 3004 3005 mutex_enter(&sd_scsi_target_lun_mutex); 3006 3007 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3008 if (cp->pdip == dip) { 3009 break; 3010 } 3011 } 3012 3013 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3014 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3015 KM_SLEEP); 3016 cp->pdip = dip; 3017 cp->next = sd_scsi_target_lun_head; 3018 sd_scsi_target_lun_head = cp; 3019 } 3020 3021 mutex_exit(&sd_scsi_target_lun_mutex); 3022 3023 if (cp != NULL) { 3024 if (flag == SD_SCSI_LUN_ATTACH) { 3025 cp->nlun[target] ++; 3026 } else { 3027 cp->nlun[target] --; 3028 } 3029 } 3030 } 3031 3032 3033 /* 3034 * Function: sd_spin_up_unit 3035 * 3036 * Description: Issues the following commands to spin-up the device: 3037 * START STOP UNIT, and INQUIRY. 3038 * 3039 * Arguments: un - driver soft state (unit) structure 3040 * 3041 * Return Code: 0 - success 3042 * EIO - failure 3043 * EACCES - reservation conflict 3044 * 3045 * Context: Kernel thread context 3046 */ 3047 3048 static int 3049 sd_spin_up_unit(struct sd_lun *un) 3050 { 3051 size_t resid = 0; 3052 int has_conflict = FALSE; 3053 uchar_t *bufaddr; 3054 3055 ASSERT(un != NULL); 3056 3057 /* 3058 * Send a throwaway START UNIT command. 3059 * 3060 * If we fail on this, we don't care presently what precisely 3061 * is wrong. EMC's arrays will also fail this with a check 3062 * condition (0x2/0x4/0x3) if the device is "inactive," but 3063 * we don't want to fail the attach because it may become 3064 * "active" later. 3065 */ 3066 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 3067 == EACCES) 3068 has_conflict = TRUE; 3069 3070 /* 3071 * Send another INQUIRY command to the target. This is necessary for 3072 * non-removable media direct access devices because their INQUIRY data 3073 * may not be fully qualified until they are spun up (perhaps via the 3074 * START command above). Note: This seems to be needed for some 3075 * legacy devices only.) The INQUIRY command should succeed even if a 3076 * Reservation Conflict is present. 3077 */ 3078 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3079 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 3080 kmem_free(bufaddr, SUN_INQSIZE); 3081 return (EIO); 3082 } 3083 3084 /* 3085 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3086 * Note that this routine does not return a failure here even if the 3087 * INQUIRY command did not return any data. This is a legacy behavior. 3088 */ 3089 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3090 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3091 } 3092 3093 kmem_free(bufaddr, SUN_INQSIZE); 3094 3095 /* If we hit a reservation conflict above, tell the caller. */ 3096 if (has_conflict == TRUE) { 3097 return (EACCES); 3098 } 3099 3100 return (0); 3101 } 3102 3103 #ifdef _LP64 3104 /* 3105 * Function: sd_enable_descr_sense 3106 * 3107 * Description: This routine attempts to select descriptor sense format 3108 * using the Control mode page. Devices that support 64 bit 3109 * LBAs (for >2TB luns) should also implement descriptor 3110 * sense data so we will call this function whenever we see 3111 * a lun larger than 2TB. If for some reason the device 3112 * supports 64 bit LBAs but doesn't support descriptor sense 3113 * presumably the mode select will fail. Everything will 3114 * continue to work normally except that we will not get 3115 * complete sense data for commands that fail with an LBA 3116 * larger than 32 bits. 3117 * 3118 * Arguments: un - driver soft state (unit) structure 3119 * 3120 * Context: Kernel thread context only 3121 */ 3122 3123 static void 3124 sd_enable_descr_sense(struct sd_lun *un) 3125 { 3126 uchar_t *header; 3127 struct mode_control_scsi3 *ctrl_bufp; 3128 size_t buflen; 3129 size_t bd_len; 3130 3131 /* 3132 * Read MODE SENSE page 0xA, Control Mode Page 3133 */ 3134 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3135 sizeof (struct mode_control_scsi3); 3136 header = kmem_zalloc(buflen, KM_SLEEP); 3137 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3138 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3139 SD_ERROR(SD_LOG_COMMON, un, 3140 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3141 goto eds_exit; 3142 } 3143 3144 /* 3145 * Determine size of Block Descriptors in order to locate 3146 * the mode page data. ATAPI devices return 0, SCSI devices 3147 * should return MODE_BLK_DESC_LENGTH. 3148 */ 3149 bd_len = ((struct mode_header *)header)->bdesc_length; 3150 3151 ctrl_bufp = (struct mode_control_scsi3 *) 3152 (header + MODE_HEADER_LENGTH + bd_len); 3153 3154 /* 3155 * Clear PS bit for MODE SELECT 3156 */ 3157 ctrl_bufp->mode_page.ps = 0; 3158 3159 /* 3160 * Set D_SENSE to enable descriptor sense format. 3161 */ 3162 ctrl_bufp->d_sense = 1; 3163 3164 /* 3165 * Use MODE SELECT to commit the change to the D_SENSE bit 3166 */ 3167 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3168 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3169 SD_INFO(SD_LOG_COMMON, un, 3170 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3171 goto eds_exit; 3172 } 3173 3174 eds_exit: 3175 kmem_free(header, buflen); 3176 } 3177 3178 /* 3179 * Function: sd_reenable_dsense_task 3180 * 3181 * Description: Re-enable descriptor sense after device or bus reset 3182 * 3183 * Context: Executes in a taskq() thread context 3184 */ 3185 static void 3186 sd_reenable_dsense_task(void *arg) 3187 { 3188 struct sd_lun *un = arg; 3189 3190 ASSERT(un != NULL); 3191 sd_enable_descr_sense(un); 3192 } 3193 #endif /* _LP64 */ 3194 3195 /* 3196 * Function: sd_set_mmc_caps 3197 * 3198 * Description: This routine determines if the device is MMC compliant and if 3199 * the device supports CDDA via a mode sense of the CDVD 3200 * capabilities mode page. Also checks if the device is a 3201 * dvdram writable device. 3202 * 3203 * Arguments: un - driver soft state (unit) structure 3204 * 3205 * Context: Kernel thread context only 3206 */ 3207 3208 static void 3209 sd_set_mmc_caps(struct sd_lun *un) 3210 { 3211 struct mode_header_grp2 *sense_mhp; 3212 uchar_t *sense_page; 3213 caddr_t buf; 3214 int bd_len; 3215 int status; 3216 struct uscsi_cmd com; 3217 int rtn; 3218 uchar_t *out_data_rw, *out_data_hd; 3219 uchar_t *rqbuf_rw, *rqbuf_hd; 3220 3221 ASSERT(un != NULL); 3222 3223 /* 3224 * The flags which will be set in this function are - mmc compliant, 3225 * dvdram writable device, cdda support. Initialize them to FALSE 3226 * and if a capability is detected - it will be set to TRUE. 3227 */ 3228 un->un_f_mmc_cap = FALSE; 3229 un->un_f_dvdram_writable_device = FALSE; 3230 un->un_f_cfg_cdda = FALSE; 3231 3232 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3233 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3234 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3235 3236 if (status != 0) { 3237 /* command failed; just return */ 3238 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3239 return; 3240 } 3241 /* 3242 * If the mode sense request for the CDROM CAPABILITIES 3243 * page (0x2A) succeeds the device is assumed to be MMC. 3244 */ 3245 un->un_f_mmc_cap = TRUE; 3246 3247 /* Get to the page data */ 3248 sense_mhp = (struct mode_header_grp2 *)buf; 3249 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3250 sense_mhp->bdesc_length_lo; 3251 if (bd_len > MODE_BLK_DESC_LENGTH) { 3252 /* 3253 * We did not get back the expected block descriptor 3254 * length so we cannot determine if the device supports 3255 * CDDA. However, we still indicate the device is MMC 3256 * according to the successful response to the page 3257 * 0x2A mode sense request. 3258 */ 3259 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3260 "sd_set_mmc_caps: Mode Sense returned " 3261 "invalid block descriptor length\n"); 3262 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3263 return; 3264 } 3265 3266 /* See if read CDDA is supported */ 3267 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3268 bd_len); 3269 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3270 3271 /* See if writing DVD RAM is supported. */ 3272 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3273 if (un->un_f_dvdram_writable_device == TRUE) { 3274 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3275 return; 3276 } 3277 3278 /* 3279 * If the device presents DVD or CD capabilities in the mode 3280 * page, we can return here since a RRD will not have 3281 * these capabilities. 3282 */ 3283 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3284 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3285 return; 3286 } 3287 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3288 3289 /* 3290 * If un->un_f_dvdram_writable_device is still FALSE, 3291 * check for a Removable Rigid Disk (RRD). A RRD 3292 * device is identified by the features RANDOM_WRITABLE and 3293 * HARDWARE_DEFECT_MANAGEMENT. 3294 */ 3295 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3296 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3297 3298 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3299 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3300 RANDOM_WRITABLE); 3301 if (rtn != 0) { 3302 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3303 kmem_free(rqbuf_rw, SENSE_LENGTH); 3304 return; 3305 } 3306 3307 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3308 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3309 3310 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3311 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3312 HARDWARE_DEFECT_MANAGEMENT); 3313 if (rtn == 0) { 3314 /* 3315 * We have good information, check for random writable 3316 * and hardware defect features. 3317 */ 3318 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3319 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3320 un->un_f_dvdram_writable_device = TRUE; 3321 } 3322 } 3323 3324 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3325 kmem_free(rqbuf_rw, SENSE_LENGTH); 3326 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3327 kmem_free(rqbuf_hd, SENSE_LENGTH); 3328 } 3329 3330 /* 3331 * Function: sd_check_for_writable_cd 3332 * 3333 * Description: This routine determines if the media in the device is 3334 * writable or not. It uses the get configuration command (0x46) 3335 * to determine if the media is writable 3336 * 3337 * Arguments: un - driver soft state (unit) structure 3338 * 3339 * Context: Never called at interrupt context. 3340 */ 3341 3342 static void 3343 sd_check_for_writable_cd(struct sd_lun *un) 3344 { 3345 struct uscsi_cmd com; 3346 uchar_t *out_data; 3347 uchar_t *rqbuf; 3348 int rtn; 3349 uchar_t *out_data_rw, *out_data_hd; 3350 uchar_t *rqbuf_rw, *rqbuf_hd; 3351 struct mode_header_grp2 *sense_mhp; 3352 uchar_t *sense_page; 3353 caddr_t buf; 3354 int bd_len; 3355 int status; 3356 3357 ASSERT(un != NULL); 3358 ASSERT(mutex_owned(SD_MUTEX(un))); 3359 3360 /* 3361 * Initialize the writable media to false, if configuration info. 3362 * tells us otherwise then only we will set it. 3363 */ 3364 un->un_f_mmc_writable_media = FALSE; 3365 mutex_exit(SD_MUTEX(un)); 3366 3367 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3368 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3369 3370 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3371 out_data, SD_PROFILE_HEADER_LEN); 3372 3373 mutex_enter(SD_MUTEX(un)); 3374 if (rtn == 0) { 3375 /* 3376 * We have good information, check for writable DVD. 3377 */ 3378 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3379 un->un_f_mmc_writable_media = TRUE; 3380 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3381 kmem_free(rqbuf, SENSE_LENGTH); 3382 return; 3383 } 3384 } 3385 3386 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3387 kmem_free(rqbuf, SENSE_LENGTH); 3388 3389 /* 3390 * Determine if this is a RRD type device. 3391 */ 3392 mutex_exit(SD_MUTEX(un)); 3393 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3394 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3395 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3396 mutex_enter(SD_MUTEX(un)); 3397 if (status != 0) { 3398 /* command failed; just return */ 3399 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3400 return; 3401 } 3402 3403 /* Get to the page data */ 3404 sense_mhp = (struct mode_header_grp2 *)buf; 3405 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3406 if (bd_len > MODE_BLK_DESC_LENGTH) { 3407 /* 3408 * We did not get back the expected block descriptor length so 3409 * we cannot check the mode page. 3410 */ 3411 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3412 "sd_check_for_writable_cd: Mode Sense returned " 3413 "invalid block descriptor length\n"); 3414 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3415 return; 3416 } 3417 3418 /* 3419 * If the device presents DVD or CD capabilities in the mode 3420 * page, we can return here since a RRD device will not have 3421 * these capabilities. 3422 */ 3423 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3424 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3425 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3426 return; 3427 } 3428 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3429 3430 /* 3431 * If un->un_f_mmc_writable_media is still FALSE, 3432 * check for RRD type media. A RRD device is identified 3433 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3434 */ 3435 mutex_exit(SD_MUTEX(un)); 3436 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3437 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3438 3439 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3440 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3441 RANDOM_WRITABLE); 3442 if (rtn != 0) { 3443 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3444 kmem_free(rqbuf_rw, SENSE_LENGTH); 3445 mutex_enter(SD_MUTEX(un)); 3446 return; 3447 } 3448 3449 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3450 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3451 3452 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3453 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3454 HARDWARE_DEFECT_MANAGEMENT); 3455 mutex_enter(SD_MUTEX(un)); 3456 if (rtn == 0) { 3457 /* 3458 * We have good information, check for random writable 3459 * and hardware defect features as current. 3460 */ 3461 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3462 (out_data_rw[10] & 0x1) && 3463 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3464 (out_data_hd[10] & 0x1)) { 3465 un->un_f_mmc_writable_media = TRUE; 3466 } 3467 } 3468 3469 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3470 kmem_free(rqbuf_rw, SENSE_LENGTH); 3471 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3472 kmem_free(rqbuf_hd, SENSE_LENGTH); 3473 } 3474 3475 /* 3476 * Function: sd_read_unit_properties 3477 * 3478 * Description: The following implements a property lookup mechanism. 3479 * Properties for particular disks (keyed on vendor, model 3480 * and rev numbers) are sought in the sd.conf file via 3481 * sd_process_sdconf_file(), and if not found there, are 3482 * looked for in a list hardcoded in this driver via 3483 * sd_process_sdconf_table() Once located the properties 3484 * are used to update the driver unit structure. 3485 * 3486 * Arguments: un - driver soft state (unit) structure 3487 */ 3488 3489 static void 3490 sd_read_unit_properties(struct sd_lun *un) 3491 { 3492 /* 3493 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3494 * the "sd-config-list" property (from the sd.conf file) or if 3495 * there was not a match for the inquiry vid/pid. If this event 3496 * occurs the static driver configuration table is searched for 3497 * a match. 3498 */ 3499 ASSERT(un != NULL); 3500 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3501 sd_process_sdconf_table(un); 3502 } 3503 3504 /* check for LSI device */ 3505 sd_is_lsi(un); 3506 3507 3508 } 3509 3510 3511 /* 3512 * Function: sd_process_sdconf_file 3513 * 3514 * Description: Use ddi_getlongprop to obtain the properties from the 3515 * driver's config file (ie, sd.conf) and update the driver 3516 * soft state structure accordingly. 3517 * 3518 * Arguments: un - driver soft state (unit) structure 3519 * 3520 * Return Code: SD_SUCCESS - The properties were successfully set according 3521 * to the driver configuration file. 3522 * SD_FAILURE - The driver config list was not obtained or 3523 * there was no vid/pid match. This indicates that 3524 * the static config table should be used. 3525 * 3526 * The config file has a property, "sd-config-list", which consists of 3527 * one or more duplets as follows: 3528 * 3529 * sd-config-list= 3530 * <duplet>, 3531 * [<duplet>,] 3532 * [<duplet>]; 3533 * 3534 * The structure of each duplet is as follows: 3535 * 3536 * <duplet>:= <vid+pid>,<data-property-name_list> 3537 * 3538 * The first entry of the duplet is the device ID string (the concatenated 3539 * vid & pid; not to be confused with a device_id). This is defined in 3540 * the same way as in the sd_disk_table. 3541 * 3542 * The second part of the duplet is a string that identifies a 3543 * data-property-name-list. The data-property-name-list is defined as 3544 * follows: 3545 * 3546 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3547 * 3548 * The syntax of <data-property-name> depends on the <version> field. 3549 * 3550 * If version = SD_CONF_VERSION_1 we have the following syntax: 3551 * 3552 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3553 * 3554 * where the prop0 value will be used to set prop0 if bit0 set in the 3555 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3556 * 3557 */ 3558 3559 static int 3560 sd_process_sdconf_file(struct sd_lun *un) 3561 { 3562 char *config_list = NULL; 3563 int config_list_len; 3564 int len; 3565 int dupletlen = 0; 3566 char *vidptr; 3567 int vidlen; 3568 char *dnlist_ptr; 3569 char *dataname_ptr; 3570 int dnlist_len; 3571 int dataname_len; 3572 int *data_list; 3573 int data_list_len; 3574 int rval = SD_FAILURE; 3575 int i; 3576 3577 ASSERT(un != NULL); 3578 3579 /* Obtain the configuration list associated with the .conf file */ 3580 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3581 sd_config_list, (caddr_t)&config_list, &config_list_len) 3582 != DDI_PROP_SUCCESS) { 3583 return (SD_FAILURE); 3584 } 3585 3586 /* 3587 * Compare vids in each duplet to the inquiry vid - if a match is 3588 * made, get the data value and update the soft state structure 3589 * accordingly. 3590 * 3591 * Note: This algorithm is complex and difficult to maintain. It should 3592 * be replaced with a more robust implementation. 3593 */ 3594 for (len = config_list_len, vidptr = config_list; len > 0; 3595 vidptr += dupletlen, len -= dupletlen) { 3596 /* 3597 * Note: The assumption here is that each vid entry is on 3598 * a unique line from its associated duplet. 3599 */ 3600 vidlen = dupletlen = (int)strlen(vidptr); 3601 if ((vidlen == 0) || 3602 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3603 dupletlen++; 3604 continue; 3605 } 3606 3607 /* 3608 * dnlist contains 1 or more blank separated 3609 * data-property-name entries 3610 */ 3611 dnlist_ptr = vidptr + vidlen + 1; 3612 dnlist_len = (int)strlen(dnlist_ptr); 3613 dupletlen += dnlist_len + 2; 3614 3615 /* 3616 * Set a pointer for the first data-property-name 3617 * entry in the list 3618 */ 3619 dataname_ptr = dnlist_ptr; 3620 dataname_len = 0; 3621 3622 /* 3623 * Loop through all data-property-name entries in the 3624 * data-property-name-list setting the properties for each. 3625 */ 3626 while (dataname_len < dnlist_len) { 3627 int version; 3628 3629 /* 3630 * Determine the length of the current 3631 * data-property-name entry by indexing until a 3632 * blank or NULL is encountered. When the space is 3633 * encountered reset it to a NULL for compliance 3634 * with ddi_getlongprop(). 3635 */ 3636 for (i = 0; ((dataname_ptr[i] != ' ') && 3637 (dataname_ptr[i] != '\0')); i++) { 3638 ; 3639 } 3640 3641 dataname_len += i; 3642 /* If not null terminated, Make it so */ 3643 if (dataname_ptr[i] == ' ') { 3644 dataname_ptr[i] = '\0'; 3645 } 3646 dataname_len++; 3647 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3648 "sd_process_sdconf_file: disk:%s, data:%s\n", 3649 vidptr, dataname_ptr); 3650 3651 /* Get the data list */ 3652 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3653 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3654 != DDI_PROP_SUCCESS) { 3655 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3656 "sd_process_sdconf_file: data property (%s)" 3657 " has no value\n", dataname_ptr); 3658 dataname_ptr = dnlist_ptr + dataname_len; 3659 continue; 3660 } 3661 3662 version = data_list[0]; 3663 3664 if (version == SD_CONF_VERSION_1) { 3665 sd_tunables values; 3666 3667 /* Set the properties */ 3668 if (sd_chk_vers1_data(un, data_list[1], 3669 &data_list[2], data_list_len, dataname_ptr) 3670 == SD_SUCCESS) { 3671 sd_get_tunables_from_conf(un, 3672 data_list[1], &data_list[2], 3673 &values); 3674 sd_set_vers1_properties(un, 3675 data_list[1], &values); 3676 rval = SD_SUCCESS; 3677 } else { 3678 rval = SD_FAILURE; 3679 } 3680 } else { 3681 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3682 "data property %s version 0x%x is invalid.", 3683 dataname_ptr, version); 3684 rval = SD_FAILURE; 3685 } 3686 kmem_free(data_list, data_list_len); 3687 dataname_ptr = dnlist_ptr + dataname_len; 3688 } 3689 } 3690 3691 /* free up the memory allocated by ddi_getlongprop */ 3692 if (config_list) { 3693 kmem_free(config_list, config_list_len); 3694 } 3695 3696 return (rval); 3697 } 3698 3699 /* 3700 * Function: sd_get_tunables_from_conf() 3701 * 3702 * 3703 * This function reads the data list from the sd.conf file and pulls 3704 * the values that can have numeric values as arguments and places 3705 * the values in the apropriate sd_tunables member. 3706 * Since the order of the data list members varies across platforms 3707 * This function reads them from the data list in a platform specific 3708 * order and places them into the correct sd_tunable member that is 3709 * a consistant across all platforms. 3710 */ 3711 static void 3712 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3713 sd_tunables *values) 3714 { 3715 int i; 3716 int mask; 3717 3718 bzero(values, sizeof (sd_tunables)); 3719 3720 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3721 3722 mask = 1 << i; 3723 if (mask > flags) { 3724 break; 3725 } 3726 3727 switch (mask & flags) { 3728 case 0: /* This mask bit not set in flags */ 3729 continue; 3730 case SD_CONF_BSET_THROTTLE: 3731 values->sdt_throttle = data_list[i]; 3732 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3733 "sd_get_tunables_from_conf: throttle = %d\n", 3734 values->sdt_throttle); 3735 break; 3736 case SD_CONF_BSET_CTYPE: 3737 values->sdt_ctype = data_list[i]; 3738 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3739 "sd_get_tunables_from_conf: ctype = %d\n", 3740 values->sdt_ctype); 3741 break; 3742 case SD_CONF_BSET_NRR_COUNT: 3743 values->sdt_not_rdy_retries = data_list[i]; 3744 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3745 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3746 values->sdt_not_rdy_retries); 3747 break; 3748 case SD_CONF_BSET_BSY_RETRY_COUNT: 3749 values->sdt_busy_retries = data_list[i]; 3750 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3751 "sd_get_tunables_from_conf: busy_retries = %d\n", 3752 values->sdt_busy_retries); 3753 break; 3754 case SD_CONF_BSET_RST_RETRIES: 3755 values->sdt_reset_retries = data_list[i]; 3756 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3757 "sd_get_tunables_from_conf: reset_retries = %d\n", 3758 values->sdt_reset_retries); 3759 break; 3760 case SD_CONF_BSET_RSV_REL_TIME: 3761 values->sdt_reserv_rel_time = data_list[i]; 3762 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3763 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3764 values->sdt_reserv_rel_time); 3765 break; 3766 case SD_CONF_BSET_MIN_THROTTLE: 3767 values->sdt_min_throttle = data_list[i]; 3768 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3769 "sd_get_tunables_from_conf: min_throttle = %d\n", 3770 values->sdt_min_throttle); 3771 break; 3772 case SD_CONF_BSET_DISKSORT_DISABLED: 3773 values->sdt_disk_sort_dis = data_list[i]; 3774 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3775 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3776 values->sdt_disk_sort_dis); 3777 break; 3778 case SD_CONF_BSET_LUN_RESET_ENABLED: 3779 values->sdt_lun_reset_enable = data_list[i]; 3780 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3781 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3782 "\n", values->sdt_lun_reset_enable); 3783 break; 3784 } 3785 } 3786 } 3787 3788 /* 3789 * Function: sd_process_sdconf_table 3790 * 3791 * Description: Search the static configuration table for a match on the 3792 * inquiry vid/pid and update the driver soft state structure 3793 * according to the table property values for the device. 3794 * 3795 * The form of a configuration table entry is: 3796 * <vid+pid>,<flags>,<property-data> 3797 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3798 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3799 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3800 * 3801 * Arguments: un - driver soft state (unit) structure 3802 */ 3803 3804 static void 3805 sd_process_sdconf_table(struct sd_lun *un) 3806 { 3807 char *id = NULL; 3808 int table_index; 3809 int idlen; 3810 3811 ASSERT(un != NULL); 3812 for (table_index = 0; table_index < sd_disk_table_size; 3813 table_index++) { 3814 id = sd_disk_table[table_index].device_id; 3815 idlen = strlen(id); 3816 if (idlen == 0) { 3817 continue; 3818 } 3819 3820 /* 3821 * The static configuration table currently does not 3822 * implement version 10 properties. Additionally, 3823 * multiple data-property-name entries are not 3824 * implemented in the static configuration table. 3825 */ 3826 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3827 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3828 "sd_process_sdconf_table: disk %s\n", id); 3829 sd_set_vers1_properties(un, 3830 sd_disk_table[table_index].flags, 3831 sd_disk_table[table_index].properties); 3832 break; 3833 } 3834 } 3835 } 3836 3837 3838 /* 3839 * Function: sd_sdconf_id_match 3840 * 3841 * Description: This local function implements a case sensitive vid/pid 3842 * comparison as well as the boundary cases of wild card and 3843 * multiple blanks. 3844 * 3845 * Note: An implicit assumption made here is that the scsi 3846 * inquiry structure will always keep the vid, pid and 3847 * revision strings in consecutive sequence, so they can be 3848 * read as a single string. If this assumption is not the 3849 * case, a separate string, to be used for the check, needs 3850 * to be built with these strings concatenated. 3851 * 3852 * Arguments: un - driver soft state (unit) structure 3853 * id - table or config file vid/pid 3854 * idlen - length of the vid/pid (bytes) 3855 * 3856 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3857 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3858 */ 3859 3860 static int 3861 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3862 { 3863 struct scsi_inquiry *sd_inq; 3864 int rval = SD_SUCCESS; 3865 3866 ASSERT(un != NULL); 3867 sd_inq = un->un_sd->sd_inq; 3868 ASSERT(id != NULL); 3869 3870 /* 3871 * We use the inq_vid as a pointer to a buffer containing the 3872 * vid and pid and use the entire vid/pid length of the table 3873 * entry for the comparison. This works because the inq_pid 3874 * data member follows inq_vid in the scsi_inquiry structure. 3875 */ 3876 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3877 /* 3878 * The user id string is compared to the inquiry vid/pid 3879 * using a case insensitive comparison and ignoring 3880 * multiple spaces. 3881 */ 3882 rval = sd_blank_cmp(un, id, idlen); 3883 if (rval != SD_SUCCESS) { 3884 /* 3885 * User id strings that start and end with a "*" 3886 * are a special case. These do not have a 3887 * specific vendor, and the product string can 3888 * appear anywhere in the 16 byte PID portion of 3889 * the inquiry data. This is a simple strstr() 3890 * type search for the user id in the inquiry data. 3891 */ 3892 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3893 char *pidptr = &id[1]; 3894 int i; 3895 int j; 3896 int pidstrlen = idlen - 2; 3897 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3898 pidstrlen; 3899 3900 if (j < 0) { 3901 return (SD_FAILURE); 3902 } 3903 for (i = 0; i < j; i++) { 3904 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3905 pidptr, pidstrlen) == 0) { 3906 rval = SD_SUCCESS; 3907 break; 3908 } 3909 } 3910 } 3911 } 3912 } 3913 return (rval); 3914 } 3915 3916 3917 /* 3918 * Function: sd_blank_cmp 3919 * 3920 * Description: If the id string starts and ends with a space, treat 3921 * multiple consecutive spaces as equivalent to a single 3922 * space. For example, this causes a sd_disk_table entry 3923 * of " NEC CDROM " to match a device's id string of 3924 * "NEC CDROM". 3925 * 3926 * Note: The success exit condition for this routine is if 3927 * the pointer to the table entry is '\0' and the cnt of 3928 * the inquiry length is zero. This will happen if the inquiry 3929 * string returned by the device is padded with spaces to be 3930 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3931 * SCSI spec states that the inquiry string is to be padded with 3932 * spaces. 3933 * 3934 * Arguments: un - driver soft state (unit) structure 3935 * id - table or config file vid/pid 3936 * idlen - length of the vid/pid (bytes) 3937 * 3938 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3939 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3940 */ 3941 3942 static int 3943 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3944 { 3945 char *p1; 3946 char *p2; 3947 int cnt; 3948 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3949 sizeof (SD_INQUIRY(un)->inq_pid); 3950 3951 ASSERT(un != NULL); 3952 p2 = un->un_sd->sd_inq->inq_vid; 3953 ASSERT(id != NULL); 3954 p1 = id; 3955 3956 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3957 /* 3958 * Note: string p1 is terminated by a NUL but string p2 3959 * isn't. The end of p2 is determined by cnt. 3960 */ 3961 for (;;) { 3962 /* skip over any extra blanks in both strings */ 3963 while ((*p1 != '\0') && (*p1 == ' ')) { 3964 p1++; 3965 } 3966 while ((cnt != 0) && (*p2 == ' ')) { 3967 p2++; 3968 cnt--; 3969 } 3970 3971 /* compare the two strings */ 3972 if ((cnt == 0) || 3973 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3974 break; 3975 } 3976 while ((cnt > 0) && 3977 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3978 p1++; 3979 p2++; 3980 cnt--; 3981 } 3982 } 3983 } 3984 3985 /* return SD_SUCCESS if both strings match */ 3986 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3987 } 3988 3989 3990 /* 3991 * Function: sd_chk_vers1_data 3992 * 3993 * Description: Verify the version 1 device properties provided by the 3994 * user via the configuration file 3995 * 3996 * Arguments: un - driver soft state (unit) structure 3997 * flags - integer mask indicating properties to be set 3998 * prop_list - integer list of property values 3999 * list_len - length of user provided data 4000 * 4001 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4002 * SD_FAILURE - Indicates the user provided data is invalid 4003 */ 4004 4005 static int 4006 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4007 int list_len, char *dataname_ptr) 4008 { 4009 int i; 4010 int mask = 1; 4011 int index = 0; 4012 4013 ASSERT(un != NULL); 4014 4015 /* Check for a NULL property name and list */ 4016 if (dataname_ptr == NULL) { 4017 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4018 "sd_chk_vers1_data: NULL data property name."); 4019 return (SD_FAILURE); 4020 } 4021 if (prop_list == NULL) { 4022 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4023 "sd_chk_vers1_data: %s NULL data property list.", 4024 dataname_ptr); 4025 return (SD_FAILURE); 4026 } 4027 4028 /* Display a warning if undefined bits are set in the flags */ 4029 if (flags & ~SD_CONF_BIT_MASK) { 4030 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4031 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4032 "Properties not set.", 4033 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4034 return (SD_FAILURE); 4035 } 4036 4037 /* 4038 * Verify the length of the list by identifying the highest bit set 4039 * in the flags and validating that the property list has a length 4040 * up to the index of this bit. 4041 */ 4042 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4043 if (flags & mask) { 4044 index++; 4045 } 4046 mask = 1 << i; 4047 } 4048 if ((list_len / sizeof (int)) < (index + 2)) { 4049 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4050 "sd_chk_vers1_data: " 4051 "Data property list %s size is incorrect. " 4052 "Properties not set.", dataname_ptr); 4053 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4054 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4055 return (SD_FAILURE); 4056 } 4057 return (SD_SUCCESS); 4058 } 4059 4060 4061 /* 4062 * Function: sd_set_vers1_properties 4063 * 4064 * Description: Set version 1 device properties based on a property list 4065 * retrieved from the driver configuration file or static 4066 * configuration table. Version 1 properties have the format: 4067 * 4068 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4069 * 4070 * where the prop0 value will be used to set prop0 if bit0 4071 * is set in the flags 4072 * 4073 * Arguments: un - driver soft state (unit) structure 4074 * flags - integer mask indicating properties to be set 4075 * prop_list - integer list of property values 4076 */ 4077 4078 static void 4079 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4080 { 4081 ASSERT(un != NULL); 4082 4083 /* 4084 * Set the flag to indicate cache is to be disabled. An attempt 4085 * to disable the cache via sd_cache_control() will be made 4086 * later during attach once the basic initialization is complete. 4087 */ 4088 if (flags & SD_CONF_BSET_NOCACHE) { 4089 un->un_f_opt_disable_cache = TRUE; 4090 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4091 "sd_set_vers1_properties: caching disabled flag set\n"); 4092 } 4093 4094 /* CD-specific configuration parameters */ 4095 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4096 un->un_f_cfg_playmsf_bcd = TRUE; 4097 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4098 "sd_set_vers1_properties: playmsf_bcd set\n"); 4099 } 4100 if (flags & SD_CONF_BSET_READSUB_BCD) { 4101 un->un_f_cfg_readsub_bcd = TRUE; 4102 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4103 "sd_set_vers1_properties: readsub_bcd set\n"); 4104 } 4105 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4106 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4107 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4108 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4109 } 4110 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4111 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4112 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4113 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4114 } 4115 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4116 un->un_f_cfg_no_read_header = TRUE; 4117 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4118 "sd_set_vers1_properties: no_read_header set\n"); 4119 } 4120 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4121 un->un_f_cfg_read_cd_xd4 = TRUE; 4122 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4123 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4124 } 4125 4126 /* Support for devices which do not have valid/unique serial numbers */ 4127 if (flags & SD_CONF_BSET_FAB_DEVID) { 4128 un->un_f_opt_fab_devid = TRUE; 4129 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4130 "sd_set_vers1_properties: fab_devid bit set\n"); 4131 } 4132 4133 /* Support for user throttle configuration */ 4134 if (flags & SD_CONF_BSET_THROTTLE) { 4135 ASSERT(prop_list != NULL); 4136 un->un_saved_throttle = un->un_throttle = 4137 prop_list->sdt_throttle; 4138 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4139 "sd_set_vers1_properties: throttle set to %d\n", 4140 prop_list->sdt_throttle); 4141 } 4142 4143 /* Set the per disk retry count according to the conf file or table. */ 4144 if (flags & SD_CONF_BSET_NRR_COUNT) { 4145 ASSERT(prop_list != NULL); 4146 if (prop_list->sdt_not_rdy_retries) { 4147 un->un_notready_retry_count = 4148 prop_list->sdt_not_rdy_retries; 4149 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4150 "sd_set_vers1_properties: not ready retry count" 4151 " set to %d\n", un->un_notready_retry_count); 4152 } 4153 } 4154 4155 /* The controller type is reported for generic disk driver ioctls */ 4156 if (flags & SD_CONF_BSET_CTYPE) { 4157 ASSERT(prop_list != NULL); 4158 switch (prop_list->sdt_ctype) { 4159 case CTYPE_CDROM: 4160 un->un_ctype = prop_list->sdt_ctype; 4161 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4162 "sd_set_vers1_properties: ctype set to " 4163 "CTYPE_CDROM\n"); 4164 break; 4165 case CTYPE_CCS: 4166 un->un_ctype = prop_list->sdt_ctype; 4167 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4168 "sd_set_vers1_properties: ctype set to " 4169 "CTYPE_CCS\n"); 4170 break; 4171 case CTYPE_ROD: /* RW optical */ 4172 un->un_ctype = prop_list->sdt_ctype; 4173 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4174 "sd_set_vers1_properties: ctype set to " 4175 "CTYPE_ROD\n"); 4176 break; 4177 default: 4178 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4179 "sd_set_vers1_properties: Could not set " 4180 "invalid ctype value (%d)", 4181 prop_list->sdt_ctype); 4182 } 4183 } 4184 4185 /* Purple failover timeout */ 4186 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4187 ASSERT(prop_list != NULL); 4188 un->un_busy_retry_count = 4189 prop_list->sdt_busy_retries; 4190 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4191 "sd_set_vers1_properties: " 4192 "busy retry count set to %d\n", 4193 un->un_busy_retry_count); 4194 } 4195 4196 /* Purple reset retry count */ 4197 if (flags & SD_CONF_BSET_RST_RETRIES) { 4198 ASSERT(prop_list != NULL); 4199 un->un_reset_retry_count = 4200 prop_list->sdt_reset_retries; 4201 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4202 "sd_set_vers1_properties: " 4203 "reset retry count set to %d\n", 4204 un->un_reset_retry_count); 4205 } 4206 4207 /* Purple reservation release timeout */ 4208 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4209 ASSERT(prop_list != NULL); 4210 un->un_reserve_release_time = 4211 prop_list->sdt_reserv_rel_time; 4212 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4213 "sd_set_vers1_properties: " 4214 "reservation release timeout set to %d\n", 4215 un->un_reserve_release_time); 4216 } 4217 4218 /* 4219 * Driver flag telling the driver to verify that no commands are pending 4220 * for a device before issuing a Test Unit Ready. This is a workaround 4221 * for a firmware bug in some Seagate eliteI drives. 4222 */ 4223 if (flags & SD_CONF_BSET_TUR_CHECK) { 4224 un->un_f_cfg_tur_check = TRUE; 4225 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4226 "sd_set_vers1_properties: tur queue check set\n"); 4227 } 4228 4229 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4230 un->un_min_throttle = prop_list->sdt_min_throttle; 4231 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4232 "sd_set_vers1_properties: min throttle set to %d\n", 4233 un->un_min_throttle); 4234 } 4235 4236 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4237 un->un_f_disksort_disabled = 4238 (prop_list->sdt_disk_sort_dis != 0) ? 4239 TRUE : FALSE; 4240 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4241 "sd_set_vers1_properties: disksort disabled " 4242 "flag set to %d\n", 4243 prop_list->sdt_disk_sort_dis); 4244 } 4245 4246 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4247 un->un_f_lun_reset_enabled = 4248 (prop_list->sdt_lun_reset_enable != 0) ? 4249 TRUE : FALSE; 4250 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4251 "sd_set_vers1_properties: lun reset enabled " 4252 "flag set to %d\n", 4253 prop_list->sdt_lun_reset_enable); 4254 } 4255 4256 /* 4257 * Validate the throttle values. 4258 * If any of the numbers are invalid, set everything to defaults. 4259 */ 4260 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4261 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4262 (un->un_min_throttle > un->un_throttle)) { 4263 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4264 un->un_min_throttle = sd_min_throttle; 4265 } 4266 } 4267 4268 /* 4269 * Function: sd_is_lsi() 4270 * 4271 * Description: Check for lsi devices, step throught the static device 4272 * table to match vid/pid. 4273 * 4274 * Args: un - ptr to sd_lun 4275 * 4276 * Notes: When creating new LSI property, need to add the new LSI property 4277 * to this function. 4278 */ 4279 static void 4280 sd_is_lsi(struct sd_lun *un) 4281 { 4282 char *id = NULL; 4283 int table_index; 4284 int idlen; 4285 void *prop; 4286 4287 ASSERT(un != NULL); 4288 for (table_index = 0; table_index < sd_disk_table_size; 4289 table_index++) { 4290 id = sd_disk_table[table_index].device_id; 4291 idlen = strlen(id); 4292 if (idlen == 0) { 4293 continue; 4294 } 4295 4296 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4297 prop = sd_disk_table[table_index].properties; 4298 if (prop == &lsi_properties || 4299 prop == &lsi_oem_properties || 4300 prop == &lsi_properties_scsi || 4301 prop == &symbios_properties) { 4302 un->un_f_cfg_is_lsi = TRUE; 4303 } 4304 break; 4305 } 4306 } 4307 } 4308 4309 4310 /* 4311 * The following routines support reading and interpretation of disk labels, 4312 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4313 * fdisk tables. 4314 */ 4315 4316 /* 4317 * Function: sd_validate_geometry 4318 * 4319 * Description: Read the label from the disk (if present). Update the unit's 4320 * geometry and vtoc information from the data in the label. 4321 * Verify that the label is valid. 4322 * 4323 * Arguments: un - driver soft state (unit) structure 4324 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4325 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4326 * to use the USCSI "direct" chain and bypass the normal 4327 * command waitq. 4328 * 4329 * Return Code: 0 - Successful completion 4330 * EINVAL - Invalid value in un->un_tgt_blocksize or 4331 * un->un_blockcount; or label on disk is corrupted 4332 * or unreadable. 4333 * EACCES - Reservation conflict at the device. 4334 * ENOMEM - Resource allocation error 4335 * ENOTSUP - geometry not applicable 4336 * 4337 * Context: Kernel thread only (can sleep). 4338 */ 4339 4340 static int 4341 sd_validate_geometry(struct sd_lun *un, int path_flag) 4342 { 4343 static char labelstring[128]; 4344 static char buf[256]; 4345 char *label = NULL; 4346 int label_error = 0; 4347 int gvalid = un->un_f_geometry_is_valid; 4348 int lbasize; 4349 uint64_t capacity; 4350 int count; 4351 #if defined(__i386) || defined(__amd64) 4352 int forced_under_1t = 0; 4353 #endif 4354 4355 ASSERT(un != NULL); 4356 ASSERT(mutex_owned(SD_MUTEX(un))); 4357 4358 /* 4359 * If the required values are not valid, then try getting them 4360 * once via read capacity. If that fails, then fail this call. 4361 * This is necessary with the new mpxio failover behavior in 4362 * the T300 where we can get an attach for the inactive path 4363 * before the active path. The inactive path fails commands with 4364 * sense data of 02,04,88 which happens to the read capacity 4365 * before mpxio has had sufficient knowledge to know if it should 4366 * force a fail over or not. (Which it won't do at attach anyhow). 4367 * If the read capacity at attach time fails, un_tgt_blocksize and 4368 * un_blockcount won't be valid. 4369 */ 4370 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4371 (un->un_f_blockcount_is_valid != TRUE)) { 4372 uint64_t cap; 4373 uint32_t lbasz; 4374 int rval; 4375 4376 mutex_exit(SD_MUTEX(un)); 4377 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4378 &lbasz, SD_PATH_DIRECT); 4379 mutex_enter(SD_MUTEX(un)); 4380 if (rval == 0) { 4381 /* 4382 * The following relies on 4383 * sd_send_scsi_READ_CAPACITY never 4384 * returning 0 for capacity and/or lbasize. 4385 */ 4386 sd_update_block_info(un, lbasz, cap); 4387 } 4388 4389 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4390 (un->un_f_blockcount_is_valid != TRUE)) { 4391 return (EINVAL); 4392 } 4393 } 4394 4395 /* 4396 * Copy the lbasize and capacity so that if they're reset while we're 4397 * not holding the SD_MUTEX, we will continue to use valid values 4398 * after the SD_MUTEX is reacquired. (4119659) 4399 */ 4400 lbasize = un->un_tgt_blocksize; 4401 capacity = un->un_blockcount; 4402 4403 #if defined(_SUNOS_VTOC_16) 4404 /* 4405 * Set up the "whole disk" fdisk partition; this should always 4406 * exist, regardless of whether the disk contains an fdisk table 4407 * or vtoc. 4408 */ 4409 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4410 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4411 #endif 4412 4413 /* 4414 * Refresh the logical and physical geometry caches. 4415 * (data from MODE SENSE format/rigid disk geometry pages, 4416 * and scsi_ifgetcap("geometry"). 4417 */ 4418 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4419 4420 label_error = sd_use_efi(un, path_flag); 4421 if (label_error == 0) { 4422 /* found a valid EFI label */ 4423 SD_TRACE(SD_LOG_IO_PARTITION, un, 4424 "sd_validate_geometry: found EFI label\n"); 4425 un->un_solaris_offset = 0; 4426 un->un_solaris_size = capacity; 4427 return (ENOTSUP); 4428 } 4429 if (un->un_blockcount > DK_MAX_BLOCKS) { 4430 if (label_error == ESRCH) { 4431 /* 4432 * they've configured a LUN over 1TB, but used 4433 * format.dat to restrict format's view of the 4434 * capacity to be under 1TB 4435 */ 4436 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4437 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4438 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4439 "size to be < 1TB or relabel the disk with an EFI label"); 4440 #if defined(__i386) || defined(__amd64) 4441 forced_under_1t = 1; 4442 #endif 4443 } else { 4444 /* unlabeled disk over 1TB */ 4445 #if defined(__i386) || defined(__amd64) 4446 /* 4447 * Refer to comments on off-by-1 at the head of the file 4448 * A 1TB disk was treated as (1T - 512)B in the past, 4449 * thus, it might have valid solaris partition. We 4450 * will return ENOTSUP later only if this disk has no 4451 * valid solaris partition. 4452 */ 4453 if ((un->un_tgt_blocksize != un->un_sys_blocksize) || 4454 (un->un_blockcount - 1 > DK_MAX_BLOCKS) || 4455 un->un_f_has_removable_media || 4456 un->un_f_is_hotpluggable) 4457 #endif 4458 return (ENOTSUP); 4459 } 4460 } 4461 label_error = 0; 4462 4463 /* 4464 * at this point it is either labeled with a VTOC or it is 4465 * under 1TB (<= 1TB actually for off-by-1) 4466 */ 4467 if (un->un_f_vtoc_label_supported) { 4468 struct dk_label *dkl; 4469 offset_t dkl1; 4470 offset_t label_addr, real_addr; 4471 int rval; 4472 size_t buffer_size; 4473 4474 /* 4475 * Note: This will set up un->un_solaris_size and 4476 * un->un_solaris_offset. 4477 */ 4478 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4479 case SD_CMD_RESERVATION_CONFLICT: 4480 ASSERT(mutex_owned(SD_MUTEX(un))); 4481 return (EACCES); 4482 case SD_CMD_FAILURE: 4483 ASSERT(mutex_owned(SD_MUTEX(un))); 4484 /* 4485 * A multisession audio cd can have an unreadable 4486 * fdisk sector, but there could be readable data 4487 * in a separate session. Accept this and let 4488 * the code build a default disk label later on. 4489 */ 4490 if (ISCD(un)) 4491 break; 4492 return (ENOMEM); 4493 } 4494 4495 if (un->un_solaris_size <= DK_LABEL_LOC) { 4496 4497 #if defined(__i386) || defined(__amd64) 4498 /* 4499 * Refer to comments on off-by-1 at the head of the file 4500 * This is for 1TB disk only. Since that there is no 4501 * solaris partitions, return ENOTSUP as we do for 4502 * >1TB disk. 4503 */ 4504 if (un->un_blockcount > DK_MAX_BLOCKS) 4505 return (ENOTSUP); 4506 #endif 4507 /* 4508 * Found fdisk table but no Solaris partition entry, 4509 * so don't call sd_uselabel() and don't create 4510 * a default label. 4511 */ 4512 label_error = 0; 4513 un->un_f_geometry_is_valid = TRUE; 4514 goto no_solaris_partition; 4515 } 4516 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4517 4518 #if defined(__i386) || defined(__amd64) 4519 /* 4520 * Refer to comments on off-by-1 at the head of the file 4521 * Now, this 1TB disk has valid solaris partition. It 4522 * must be created by previous sd driver, we have to 4523 * treat it as (1T-512)B. 4524 */ 4525 if ((un->un_blockcount > DK_MAX_BLOCKS) && 4526 (forced_under_1t != 1)) { 4527 un->un_f_capacity_adjusted = 1; 4528 un->un_blockcount = DK_MAX_BLOCKS; 4529 un->un_map[P0_RAW_DISK].dkl_nblk = DK_MAX_BLOCKS; 4530 4531 /* 4532 * Refer to sd_read_fdisk, when there is no 4533 * fdisk partition table, un_solaris_size is 4534 * set to disk's capacity. In this case, we 4535 * need to adjust it 4536 */ 4537 if (un->un_solaris_size > DK_MAX_BLOCKS) 4538 un->un_solaris_size = DK_MAX_BLOCKS; 4539 sd_resync_geom_caches(un, DK_MAX_BLOCKS, 4540 lbasize, path_flag); 4541 } 4542 #endif 4543 4544 /* 4545 * sys_blocksize != tgt_blocksize, need to re-adjust 4546 * blkno and save the index to beginning of dk_label 4547 */ 4548 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4549 buffer_size = SD_REQBYTES2TGTBYTES(un, 4550 sizeof (struct dk_label)); 4551 4552 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4553 "label_addr: 0x%x allocation size: 0x%x\n", 4554 label_addr, buffer_size); 4555 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4556 if (dkl == NULL) { 4557 return (ENOMEM); 4558 } 4559 4560 mutex_exit(SD_MUTEX(un)); 4561 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4562 path_flag); 4563 mutex_enter(SD_MUTEX(un)); 4564 4565 switch (rval) { 4566 case 0: 4567 /* 4568 * sd_uselabel will establish that the geometry 4569 * is valid. 4570 * For sys_blocksize != tgt_blocksize, need 4571 * to index into the beginning of dk_label 4572 */ 4573 dkl1 = (daddr_t)dkl 4574 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4575 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4576 path_flag) != SD_LABEL_IS_VALID) { 4577 label_error = EINVAL; 4578 } 4579 break; 4580 case EACCES: 4581 label_error = EACCES; 4582 break; 4583 default: 4584 label_error = EINVAL; 4585 break; 4586 } 4587 4588 kmem_free(dkl, buffer_size); 4589 4590 #if defined(_SUNOS_VTOC_8) 4591 label = (char *)un->un_asciilabel; 4592 #elif defined(_SUNOS_VTOC_16) 4593 label = (char *)un->un_vtoc.v_asciilabel; 4594 #else 4595 #error "No VTOC format defined." 4596 #endif 4597 } 4598 4599 /* 4600 * If a valid label was not found, AND if no reservation conflict 4601 * was detected, then go ahead and create a default label (4069506). 4602 */ 4603 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4604 if (un->un_f_geometry_is_valid == FALSE) { 4605 sd_build_default_label(un); 4606 } 4607 label_error = 0; 4608 } 4609 4610 no_solaris_partition: 4611 if ((!un->un_f_has_removable_media || 4612 (un->un_f_has_removable_media && 4613 un->un_mediastate == DKIO_EJECTED)) && 4614 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4615 /* 4616 * Print out a message indicating who and what we are. 4617 * We do this only when we happen to really validate the 4618 * geometry. We may call sd_validate_geometry() at other 4619 * times, e.g., ioctl()'s like Get VTOC in which case we 4620 * don't want to print the label. 4621 * If the geometry is valid, print the label string, 4622 * else print vendor and product info, if available 4623 */ 4624 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4625 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4626 } else { 4627 mutex_enter(&sd_label_mutex); 4628 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4629 labelstring); 4630 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4631 &labelstring[64]); 4632 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4633 labelstring, &labelstring[64]); 4634 if (un->un_f_blockcount_is_valid == TRUE) { 4635 (void) sprintf(&buf[strlen(buf)], 4636 ", %llu %u byte blocks\n", 4637 (longlong_t)un->un_blockcount, 4638 un->un_tgt_blocksize); 4639 } else { 4640 (void) sprintf(&buf[strlen(buf)], 4641 ", (unknown capacity)\n"); 4642 } 4643 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4644 mutex_exit(&sd_label_mutex); 4645 } 4646 } 4647 4648 #if defined(_SUNOS_VTOC_16) 4649 /* 4650 * If we have valid geometry, set up the remaining fdisk partitions. 4651 * Note that dkl_cylno is not used for the fdisk map entries, so 4652 * we set it to an entirely bogus value. 4653 */ 4654 for (count = 0; count < FD_NUMPART; count++) { 4655 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4656 un->un_map[FDISK_P1 + count].dkl_nblk = 4657 un->un_fmap[count].fmap_nblk; 4658 4659 un->un_offset[FDISK_P1 + count] = 4660 un->un_fmap[count].fmap_start; 4661 } 4662 #endif 4663 4664 for (count = 0; count < NDKMAP; count++) { 4665 #if defined(_SUNOS_VTOC_8) 4666 struct dk_map *lp = &un->un_map[count]; 4667 un->un_offset[count] = 4668 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4669 #elif defined(_SUNOS_VTOC_16) 4670 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4671 4672 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4673 #else 4674 #error "No VTOC format defined." 4675 #endif 4676 } 4677 4678 /* 4679 * For VTOC labeled disk, create and set the partition stats 4680 * at attach time, update the stats according to dynamic 4681 * partition changes during running time. 4682 */ 4683 if (label_error == 0 && un->un_f_pkstats_enabled) { 4684 sd_set_pstats(un); 4685 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4686 "un:0x%p pstats created and set, or updated\n", un); 4687 } 4688 4689 return (label_error); 4690 } 4691 4692 4693 #if defined(_SUNOS_VTOC_16) 4694 /* 4695 * Macro: MAX_BLKS 4696 * 4697 * This macro is used for table entries where we need to have the largest 4698 * possible sector value for that head & SPT (sectors per track) 4699 * combination. Other entries for some smaller disk sizes are set by 4700 * convention to match those used by X86 BIOS usage. 4701 */ 4702 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4703 4704 /* 4705 * Function: sd_convert_geometry 4706 * 4707 * Description: Convert physical geometry into a dk_geom structure. In 4708 * other words, make sure we don't wrap 16-bit values. 4709 * e.g. converting from geom_cache to dk_geom 4710 * 4711 * Context: Kernel thread only 4712 */ 4713 static void 4714 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4715 { 4716 int i; 4717 static const struct chs_values { 4718 uint_t max_cap; /* Max Capacity for this HS. */ 4719 uint_t nhead; /* Heads to use. */ 4720 uint_t nsect; /* SPT to use. */ 4721 } CHS_values[] = { 4722 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4723 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4724 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4725 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4726 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4727 }; 4728 4729 /* Unlabeled SCSI floppy device */ 4730 if (capacity <= 0x1000) { 4731 un_g->dkg_nhead = 2; 4732 un_g->dkg_ncyl = 80; 4733 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4734 return; 4735 } 4736 4737 /* 4738 * For all devices we calculate cylinders using the 4739 * heads and sectors we assign based on capacity of the 4740 * device. The table is designed to be compatible with the 4741 * way other operating systems lay out fdisk tables for X86 4742 * and to insure that the cylinders never exceed 65535 to 4743 * prevent problems with X86 ioctls that report geometry. 4744 * We use SPT that are multiples of 63, since other OSes that 4745 * are not limited to 16-bits for cylinders stop at 63 SPT 4746 * we make do by using multiples of 63 SPT. 4747 * 4748 * Note than capacities greater than or equal to 1TB will simply 4749 * get the largest geometry from the table. This should be okay 4750 * since disks this large shouldn't be using CHS values anyway. 4751 */ 4752 for (i = 0; CHS_values[i].max_cap < capacity && 4753 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4754 ; 4755 4756 un_g->dkg_nhead = CHS_values[i].nhead; 4757 un_g->dkg_nsect = CHS_values[i].nsect; 4758 } 4759 #endif 4760 4761 4762 /* 4763 * Function: sd_resync_geom_caches 4764 * 4765 * Description: (Re)initialize both geometry caches: the virtual geometry 4766 * information is extracted from the HBA (the "geometry" 4767 * capability), and the physical geometry cache data is 4768 * generated by issuing MODE SENSE commands. 4769 * 4770 * Arguments: un - driver soft state (unit) structure 4771 * capacity - disk capacity in #blocks 4772 * lbasize - disk block size in bytes 4773 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4774 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4775 * to use the USCSI "direct" chain and bypass the normal 4776 * command waitq. 4777 * 4778 * Context: Kernel thread only (can sleep). 4779 */ 4780 4781 static void 4782 sd_resync_geom_caches(struct sd_lun *un, uint64_t capacity, int lbasize, 4783 int path_flag) 4784 { 4785 struct geom_cache pgeom; 4786 struct geom_cache *pgeom_p = &pgeom; 4787 int spc; 4788 unsigned short nhead; 4789 unsigned short nsect; 4790 4791 ASSERT(un != NULL); 4792 ASSERT(mutex_owned(SD_MUTEX(un))); 4793 4794 /* 4795 * Ask the controller for its logical geometry. 4796 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4797 * then the lgeom cache will be invalid. 4798 */ 4799 sd_get_virtual_geometry(un, capacity, lbasize); 4800 4801 /* 4802 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4803 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4804 */ 4805 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4806 /* 4807 * Note: Perhaps this needs to be more adaptive? The rationale 4808 * is that, if there's no HBA geometry from the HBA driver, any 4809 * guess is good, since this is the physical geometry. If MODE 4810 * SENSE fails this gives a max cylinder size for non-LBA access 4811 */ 4812 nhead = 255; 4813 nsect = 63; 4814 } else { 4815 nhead = un->un_lgeom.g_nhead; 4816 nsect = un->un_lgeom.g_nsect; 4817 } 4818 4819 if (ISCD(un)) { 4820 pgeom_p->g_nhead = 1; 4821 pgeom_p->g_nsect = nsect * nhead; 4822 } else { 4823 pgeom_p->g_nhead = nhead; 4824 pgeom_p->g_nsect = nsect; 4825 } 4826 4827 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4828 pgeom_p->g_capacity = capacity; 4829 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4830 pgeom_p->g_acyl = 0; 4831 4832 /* 4833 * Retrieve fresh geometry data from the hardware, stash it 4834 * here temporarily before we rebuild the incore label. 4835 * 4836 * We want to use the MODE SENSE commands to derive the 4837 * physical geometry of the device, but if either command 4838 * fails, the logical geometry is used as the fallback for 4839 * disk label geometry. 4840 */ 4841 mutex_exit(SD_MUTEX(un)); 4842 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4843 mutex_enter(SD_MUTEX(un)); 4844 4845 /* 4846 * Now update the real copy while holding the mutex. This 4847 * way the global copy is never in an inconsistent state. 4848 */ 4849 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4850 4851 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4852 "(cached from lgeom)\n"); 4853 SD_INFO(SD_LOG_COMMON, un, 4854 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4855 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4856 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4857 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4858 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4859 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4860 un->un_pgeom.g_rpm); 4861 } 4862 4863 4864 /* 4865 * Function: sd_read_fdisk 4866 * 4867 * Description: utility routine to read the fdisk table. 4868 * 4869 * Arguments: un - driver soft state (unit) structure 4870 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4871 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4872 * to use the USCSI "direct" chain and bypass the normal 4873 * command waitq. 4874 * 4875 * Return Code: SD_CMD_SUCCESS 4876 * SD_CMD_FAILURE 4877 * 4878 * Context: Kernel thread only (can sleep). 4879 */ 4880 /* ARGSUSED */ 4881 static int 4882 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4883 { 4884 #if defined(_NO_FDISK_PRESENT) 4885 4886 un->un_solaris_offset = 0; 4887 un->un_solaris_size = capacity; 4888 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4889 return (SD_CMD_SUCCESS); 4890 4891 #elif defined(_FIRMWARE_NEEDS_FDISK) 4892 4893 struct ipart *fdp; 4894 struct mboot *mbp; 4895 struct ipart fdisk[FD_NUMPART]; 4896 int i; 4897 char sigbuf[2]; 4898 caddr_t bufp; 4899 int uidx; 4900 int rval; 4901 int lba = 0; 4902 uint_t solaris_offset; /* offset to solaris part. */ 4903 daddr_t solaris_size; /* size of solaris partition */ 4904 uint32_t blocksize; 4905 4906 ASSERT(un != NULL); 4907 ASSERT(mutex_owned(SD_MUTEX(un))); 4908 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4909 4910 blocksize = un->un_tgt_blocksize; 4911 4912 /* 4913 * Start off assuming no fdisk table 4914 */ 4915 solaris_offset = 0; 4916 solaris_size = capacity; 4917 4918 mutex_exit(SD_MUTEX(un)); 4919 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4920 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4921 mutex_enter(SD_MUTEX(un)); 4922 4923 if (rval != 0) { 4924 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4925 "sd_read_fdisk: fdisk read err\n"); 4926 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4927 rval = SD_CMD_FAILURE; 4928 goto done; 4929 } 4930 4931 mbp = (struct mboot *)bufp; 4932 4933 /* 4934 * The fdisk table does not begin on a 4-byte boundary within the 4935 * master boot record, so we copy it to an aligned structure to avoid 4936 * alignment exceptions on some processors. 4937 */ 4938 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4939 4940 /* 4941 * Check for lba support before verifying sig; sig might not be 4942 * there, say on a blank disk, but the max_chs mark may still 4943 * be present. 4944 * 4945 * Note: LBA support and BEFs are an x86-only concept but this 4946 * code should work OK on SPARC as well. 4947 */ 4948 4949 /* 4950 * First, check for lba-access-ok on root node (or prom root node) 4951 * if present there, don't need to search fdisk table. 4952 */ 4953 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4954 "lba-access-ok", 0) != 0) { 4955 /* All drives do LBA; don't search fdisk table */ 4956 lba = 1; 4957 } else { 4958 /* Okay, look for mark in fdisk table */ 4959 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4960 /* accumulate "lba" value from all partitions */ 4961 lba = (lba || sd_has_max_chs_vals(fdp)); 4962 } 4963 } 4964 4965 if (lba != 0) { 4966 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4967 4968 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4969 "lba-access-ok", 0) == 0) { 4970 /* not found; create it */ 4971 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4972 "lba-access-ok", (caddr_t)NULL, 0) != 4973 DDI_PROP_SUCCESS) { 4974 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4975 "sd_read_fdisk: Can't create lba property " 4976 "for instance %d\n", 4977 ddi_get_instance(SD_DEVINFO(un))); 4978 } 4979 } 4980 } 4981 4982 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4983 4984 /* 4985 * Endian-independent signature check 4986 */ 4987 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4988 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4989 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4990 "sd_read_fdisk: no fdisk\n"); 4991 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4992 rval = SD_CMD_SUCCESS; 4993 goto done; 4994 } 4995 4996 #ifdef SDDEBUG 4997 if (sd_level_mask & SD_LOGMASK_INFO) { 4998 fdp = fdisk; 4999 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 5000 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 5001 "numsect sysid bootid\n"); 5002 for (i = 0; i < FD_NUMPART; i++, fdp++) { 5003 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5004 " %d: %8d %8d 0x%08x 0x%08x\n", 5005 i, fdp->relsect, fdp->numsect, 5006 fdp->systid, fdp->bootid); 5007 } 5008 } 5009 #endif 5010 5011 /* 5012 * Try to find the unix partition 5013 */ 5014 uidx = -1; 5015 solaris_offset = 0; 5016 solaris_size = 0; 5017 5018 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 5019 int relsect; 5020 int numsect; 5021 5022 if (fdp->numsect == 0) { 5023 un->un_fmap[i].fmap_start = 0; 5024 un->un_fmap[i].fmap_nblk = 0; 5025 continue; 5026 } 5027 5028 /* 5029 * Data in the fdisk table is little-endian. 5030 */ 5031 relsect = LE_32(fdp->relsect); 5032 numsect = LE_32(fdp->numsect); 5033 5034 un->un_fmap[i].fmap_start = relsect; 5035 un->un_fmap[i].fmap_nblk = numsect; 5036 5037 if (fdp->systid != SUNIXOS && 5038 fdp->systid != SUNIXOS2 && 5039 fdp->systid != EFI_PMBR) { 5040 continue; 5041 } 5042 5043 /* 5044 * use the last active solaris partition id found 5045 * (there should only be 1 active partition id) 5046 * 5047 * if there are no active solaris partition id 5048 * then use the first inactive solaris partition id 5049 */ 5050 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 5051 uidx = i; 5052 solaris_offset = relsect; 5053 solaris_size = numsect; 5054 } 5055 } 5056 5057 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 5058 un->un_solaris_offset, un->un_solaris_size); 5059 5060 rval = SD_CMD_SUCCESS; 5061 5062 done: 5063 5064 /* 5065 * Clear the VTOC info, only if the Solaris partition entry 5066 * has moved, changed size, been deleted, or if the size of 5067 * the partition is too small to even fit the label sector. 5068 */ 5069 if ((un->un_solaris_offset != solaris_offset) || 5070 (un->un_solaris_size != solaris_size) || 5071 solaris_size <= DK_LABEL_LOC) { 5072 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 5073 solaris_offset, solaris_size); 5074 bzero(&un->un_g, sizeof (struct dk_geom)); 5075 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5076 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5077 un->un_f_geometry_is_valid = FALSE; 5078 } 5079 un->un_solaris_offset = solaris_offset; 5080 un->un_solaris_size = solaris_size; 5081 kmem_free(bufp, blocksize); 5082 return (rval); 5083 5084 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 5085 #error "fdisk table presence undetermined for this platform." 5086 #endif /* #if defined(_NO_FDISK_PRESENT) */ 5087 } 5088 5089 5090 /* 5091 * Function: sd_get_physical_geometry 5092 * 5093 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 5094 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 5095 * target, and use this information to initialize the physical 5096 * geometry cache specified by pgeom_p. 5097 * 5098 * MODE SENSE is an optional command, so failure in this case 5099 * does not necessarily denote an error. We want to use the 5100 * MODE SENSE commands to derive the physical geometry of the 5101 * device, but if either command fails, the logical geometry is 5102 * used as the fallback for disk label geometry. 5103 * 5104 * This requires that un->un_blockcount and un->un_tgt_blocksize 5105 * have already been initialized for the current target and 5106 * that the current values be passed as args so that we don't 5107 * end up ever trying to use -1 as a valid value. This could 5108 * happen if either value is reset while we're not holding 5109 * the mutex. 5110 * 5111 * Arguments: un - driver soft state (unit) structure 5112 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5113 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5114 * to use the USCSI "direct" chain and bypass the normal 5115 * command waitq. 5116 * 5117 * Context: Kernel thread only (can sleep). 5118 */ 5119 5120 static void 5121 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 5122 uint64_t capacity, int lbasize, int path_flag) 5123 { 5124 struct mode_format *page3p; 5125 struct mode_geometry *page4p; 5126 struct mode_header *headerp; 5127 int sector_size; 5128 int nsect; 5129 int nhead; 5130 int ncyl; 5131 int intrlv; 5132 int spc; 5133 int modesense_capacity; 5134 int rpm; 5135 int bd_len; 5136 int mode_header_length; 5137 uchar_t *p3bufp; 5138 uchar_t *p4bufp; 5139 int cdbsize; 5140 5141 ASSERT(un != NULL); 5142 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 5143 5144 if (un->un_f_blockcount_is_valid != TRUE) { 5145 return; 5146 } 5147 5148 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 5149 return; 5150 } 5151 5152 if (lbasize == 0) { 5153 if (ISCD(un)) { 5154 lbasize = 2048; 5155 } else { 5156 lbasize = un->un_sys_blocksize; 5157 } 5158 } 5159 pgeom_p->g_secsize = (unsigned short)lbasize; 5160 5161 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 5162 5163 /* 5164 * Retrieve MODE SENSE page 3 - Format Device Page 5165 */ 5166 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 5167 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 5168 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 5169 != 0) { 5170 SD_ERROR(SD_LOG_COMMON, un, 5171 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5172 goto page3_exit; 5173 } 5174 5175 /* 5176 * Determine size of Block Descriptors in order to locate the mode 5177 * page data. ATAPI devices return 0, SCSI devices should return 5178 * MODE_BLK_DESC_LENGTH. 5179 */ 5180 headerp = (struct mode_header *)p3bufp; 5181 if (un->un_f_cfg_is_atapi == TRUE) { 5182 struct mode_header_grp2 *mhp = 5183 (struct mode_header_grp2 *)headerp; 5184 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5185 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5186 } else { 5187 mode_header_length = MODE_HEADER_LENGTH; 5188 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5189 } 5190 5191 if (bd_len > MODE_BLK_DESC_LENGTH) { 5192 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5193 "received unexpected bd_len of %d, page3\n", bd_len); 5194 goto page3_exit; 5195 } 5196 5197 page3p = (struct mode_format *) 5198 ((caddr_t)headerp + mode_header_length + bd_len); 5199 5200 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5201 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5202 "mode sense pg3 code mismatch %d\n", 5203 page3p->mode_page.code); 5204 goto page3_exit; 5205 } 5206 5207 /* 5208 * Use this physical geometry data only if BOTH MODE SENSE commands 5209 * complete successfully; otherwise, revert to the logical geometry. 5210 * So, we need to save everything in temporary variables. 5211 */ 5212 sector_size = BE_16(page3p->data_bytes_sect); 5213 5214 /* 5215 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5216 */ 5217 if (sector_size == 0) { 5218 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5219 } else { 5220 sector_size &= ~(un->un_sys_blocksize - 1); 5221 } 5222 5223 nsect = BE_16(page3p->sect_track); 5224 intrlv = BE_16(page3p->interleave); 5225 5226 SD_INFO(SD_LOG_COMMON, un, 5227 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5228 SD_INFO(SD_LOG_COMMON, un, 5229 " mode page: %d; nsect: %d; sector size: %d;\n", 5230 page3p->mode_page.code, nsect, sector_size); 5231 SD_INFO(SD_LOG_COMMON, un, 5232 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5233 BE_16(page3p->track_skew), 5234 BE_16(page3p->cylinder_skew)); 5235 5236 5237 /* 5238 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5239 */ 5240 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5241 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5242 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5243 != 0) { 5244 SD_ERROR(SD_LOG_COMMON, un, 5245 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5246 goto page4_exit; 5247 } 5248 5249 /* 5250 * Determine size of Block Descriptors in order to locate the mode 5251 * page data. ATAPI devices return 0, SCSI devices should return 5252 * MODE_BLK_DESC_LENGTH. 5253 */ 5254 headerp = (struct mode_header *)p4bufp; 5255 if (un->un_f_cfg_is_atapi == TRUE) { 5256 struct mode_header_grp2 *mhp = 5257 (struct mode_header_grp2 *)headerp; 5258 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5259 } else { 5260 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5261 } 5262 5263 if (bd_len > MODE_BLK_DESC_LENGTH) { 5264 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5265 "received unexpected bd_len of %d, page4\n", bd_len); 5266 goto page4_exit; 5267 } 5268 5269 page4p = (struct mode_geometry *) 5270 ((caddr_t)headerp + mode_header_length + bd_len); 5271 5272 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5273 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5274 "mode sense pg4 code mismatch %d\n", 5275 page4p->mode_page.code); 5276 goto page4_exit; 5277 } 5278 5279 /* 5280 * Stash the data now, after we know that both commands completed. 5281 */ 5282 5283 mutex_enter(SD_MUTEX(un)); 5284 5285 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5286 spc = nhead * nsect; 5287 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5288 rpm = BE_16(page4p->rpm); 5289 5290 modesense_capacity = spc * ncyl; 5291 5292 SD_INFO(SD_LOG_COMMON, un, 5293 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5294 SD_INFO(SD_LOG_COMMON, un, 5295 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5296 SD_INFO(SD_LOG_COMMON, un, 5297 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5298 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5299 (void *)pgeom_p, capacity); 5300 5301 /* 5302 * Compensate if the drive's geometry is not rectangular, i.e., 5303 * the product of C * H * S returned by MODE SENSE >= that returned 5304 * by read capacity. This is an idiosyncrasy of the original x86 5305 * disk subsystem. 5306 */ 5307 if (modesense_capacity >= capacity) { 5308 SD_INFO(SD_LOG_COMMON, un, 5309 "sd_get_physical_geometry: adjusting acyl; " 5310 "old: %d; new: %d\n", pgeom_p->g_acyl, 5311 (modesense_capacity - capacity + spc - 1) / spc); 5312 if (sector_size != 0) { 5313 /* 1243403: NEC D38x7 drives don't support sec size */ 5314 pgeom_p->g_secsize = (unsigned short)sector_size; 5315 } 5316 pgeom_p->g_nsect = (unsigned short)nsect; 5317 pgeom_p->g_nhead = (unsigned short)nhead; 5318 pgeom_p->g_capacity = capacity; 5319 pgeom_p->g_acyl = 5320 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5321 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5322 } 5323 5324 pgeom_p->g_rpm = (unsigned short)rpm; 5325 pgeom_p->g_intrlv = (unsigned short)intrlv; 5326 5327 SD_INFO(SD_LOG_COMMON, un, 5328 "sd_get_physical_geometry: mode sense geometry:\n"); 5329 SD_INFO(SD_LOG_COMMON, un, 5330 " nsect: %d; sector size: %d; interlv: %d\n", 5331 nsect, sector_size, intrlv); 5332 SD_INFO(SD_LOG_COMMON, un, 5333 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5334 nhead, ncyl, rpm, modesense_capacity); 5335 SD_INFO(SD_LOG_COMMON, un, 5336 "sd_get_physical_geometry: (cached)\n"); 5337 SD_INFO(SD_LOG_COMMON, un, 5338 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5339 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5340 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5341 SD_INFO(SD_LOG_COMMON, un, 5342 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5343 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5344 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5345 5346 mutex_exit(SD_MUTEX(un)); 5347 5348 page4_exit: 5349 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5350 page3_exit: 5351 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5352 } 5353 5354 5355 /* 5356 * Function: sd_get_virtual_geometry 5357 * 5358 * Description: Ask the controller to tell us about the target device. 5359 * 5360 * Arguments: un - pointer to softstate 5361 * capacity - disk capacity in #blocks 5362 * lbasize - disk block size in bytes 5363 * 5364 * Context: Kernel thread only 5365 */ 5366 5367 static void 5368 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5369 { 5370 struct geom_cache *lgeom_p = &un->un_lgeom; 5371 uint_t geombuf; 5372 int spc; 5373 5374 ASSERT(un != NULL); 5375 ASSERT(mutex_owned(SD_MUTEX(un))); 5376 5377 mutex_exit(SD_MUTEX(un)); 5378 5379 /* Set sector size, and total number of sectors */ 5380 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5381 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5382 5383 /* Let the HBA tell us its geometry */ 5384 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5385 5386 mutex_enter(SD_MUTEX(un)); 5387 5388 /* A value of -1 indicates an undefined "geometry" property */ 5389 if (geombuf == (-1)) { 5390 return; 5391 } 5392 5393 /* Initialize the logical geometry cache. */ 5394 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5395 lgeom_p->g_nsect = geombuf & 0xffff; 5396 lgeom_p->g_secsize = un->un_sys_blocksize; 5397 5398 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5399 5400 /* 5401 * Note: The driver originally converted the capacity value from 5402 * target blocks to system blocks. However, the capacity value passed 5403 * to this routine is already in terms of system blocks (this scaling 5404 * is done when the READ CAPACITY command is issued and processed). 5405 * This 'error' may have gone undetected because the usage of g_ncyl 5406 * (which is based upon g_capacity) is very limited within the driver 5407 */ 5408 lgeom_p->g_capacity = capacity; 5409 5410 /* 5411 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5412 * hba may return zero values if the device has been removed. 5413 */ 5414 if (spc == 0) { 5415 lgeom_p->g_ncyl = 0; 5416 } else { 5417 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5418 } 5419 lgeom_p->g_acyl = 0; 5420 5421 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5422 SD_INFO(SD_LOG_COMMON, un, 5423 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5424 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5425 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5426 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5427 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5428 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5429 } 5430 5431 5432 /* 5433 * Function: sd_update_block_info 5434 * 5435 * Description: Calculate a byte count to sector count bitshift value 5436 * from sector size. 5437 * 5438 * Arguments: un: unit struct. 5439 * lbasize: new target sector size 5440 * capacity: new target capacity, ie. block count 5441 * 5442 * Context: Kernel thread context 5443 */ 5444 5445 static void 5446 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5447 { 5448 if (lbasize != 0) { 5449 un->un_tgt_blocksize = lbasize; 5450 un->un_f_tgt_blocksize_is_valid = TRUE; 5451 } 5452 5453 if (capacity != 0) { 5454 un->un_blockcount = capacity; 5455 un->un_f_blockcount_is_valid = TRUE; 5456 } 5457 } 5458 5459 5460 static void 5461 sd_swap_efi_gpt(efi_gpt_t *e) 5462 { 5463 _NOTE(ASSUMING_PROTECTED(*e)) 5464 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5465 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5466 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5467 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5468 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5469 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5470 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5471 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5472 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5473 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5474 e->efi_gpt_NumberOfPartitionEntries = 5475 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5476 e->efi_gpt_SizeOfPartitionEntry = 5477 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5478 e->efi_gpt_PartitionEntryArrayCRC32 = 5479 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5480 } 5481 5482 static void 5483 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5484 { 5485 int i; 5486 5487 _NOTE(ASSUMING_PROTECTED(*p)) 5488 for (i = 0; i < nparts; i++) { 5489 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5490 p[i].efi_gpe_PartitionTypeGUID); 5491 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5492 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5493 /* PartitionAttrs */ 5494 } 5495 } 5496 5497 static int 5498 sd_validate_efi(efi_gpt_t *labp) 5499 { 5500 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5501 return (EINVAL); 5502 /* at least 96 bytes in this version of the spec. */ 5503 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5504 labp->efi_gpt_HeaderSize) 5505 return (EINVAL); 5506 /* this should be 128 bytes */ 5507 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5508 return (EINVAL); 5509 return (0); 5510 } 5511 5512 static int 5513 sd_use_efi(struct sd_lun *un, int path_flag) 5514 { 5515 int i; 5516 int rval = 0; 5517 efi_gpe_t *partitions; 5518 uchar_t *buf; 5519 uint_t lbasize; 5520 uint64_t cap = 0; 5521 uint_t nparts; 5522 diskaddr_t gpe_lba; 5523 struct uuid uuid_type_reserved = EFI_RESERVED; 5524 5525 ASSERT(mutex_owned(SD_MUTEX(un))); 5526 lbasize = un->un_tgt_blocksize; 5527 un->un_reserved = -1; 5528 5529 mutex_exit(SD_MUTEX(un)); 5530 5531 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5532 5533 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5534 rval = EINVAL; 5535 goto done_err; 5536 } 5537 5538 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5539 if (rval) { 5540 goto done_err; 5541 } 5542 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5543 /* not ours */ 5544 rval = ESRCH; 5545 goto done_err; 5546 } 5547 5548 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5549 if (rval) { 5550 goto done_err; 5551 } 5552 sd_swap_efi_gpt((efi_gpt_t *)buf); 5553 5554 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5555 /* 5556 * Couldn't read the primary, try the backup. Our 5557 * capacity at this point could be based on CHS, so 5558 * check what the device reports. 5559 */ 5560 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5561 path_flag); 5562 if (rval) { 5563 goto done_err; 5564 } 5565 5566 /* 5567 * The MMC standard allows READ CAPACITY to be 5568 * inaccurate by a bounded amount (in the interest of 5569 * response latency). As a result, failed READs are 5570 * commonplace (due to the reading of metadata and not 5571 * data). Depending on the per-Vendor/drive Sense data, 5572 * the failed READ can cause many (unnecessary) retries. 5573 */ 5574 5575 /* 5576 * Refer to comments related to off-by-1 at the 5577 * header of this file. Search the next to last 5578 * block for backup EFI label. 5579 */ 5580 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5581 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5582 path_flag)) != 0) { 5583 goto done_err; 5584 } 5585 5586 sd_swap_efi_gpt((efi_gpt_t *)buf); 5587 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5588 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5589 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5590 path_flag)) != 0) { 5591 goto done_err; 5592 } 5593 sd_swap_efi_gpt((efi_gpt_t *)buf); 5594 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5595 goto done_err; 5596 } 5597 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5598 "primary label corrupt; using backup\n"); 5599 } 5600 5601 if (cap == 0) 5602 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5603 path_flag); 5604 5605 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5606 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5607 5608 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5609 path_flag); 5610 if (rval) { 5611 goto done_err; 5612 } 5613 partitions = (efi_gpe_t *)buf; 5614 5615 if (nparts > MAXPART) { 5616 nparts = MAXPART; 5617 } 5618 sd_swap_efi_gpe(nparts, partitions); 5619 5620 mutex_enter(SD_MUTEX(un)); 5621 5622 /* Fill in partition table. */ 5623 for (i = 0; i < nparts; i++) { 5624 if (partitions->efi_gpe_StartingLBA != 0 || 5625 partitions->efi_gpe_EndingLBA != 0) { 5626 un->un_map[i].dkl_cylno = 5627 partitions->efi_gpe_StartingLBA; 5628 un->un_map[i].dkl_nblk = 5629 partitions->efi_gpe_EndingLBA - 5630 partitions->efi_gpe_StartingLBA + 1; 5631 un->un_offset[i] = 5632 partitions->efi_gpe_StartingLBA; 5633 } 5634 if (un->un_reserved == -1) { 5635 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID, 5636 &uuid_type_reserved, sizeof (struct uuid)) == 0) { 5637 un->un_reserved = i; 5638 } 5639 } 5640 if (i == WD_NODE) { 5641 /* 5642 * minor number 7 corresponds to the whole disk 5643 */ 5644 un->un_map[i].dkl_cylno = 0; 5645 un->un_map[i].dkl_nblk = un->un_blockcount; 5646 un->un_offset[i] = 0; 5647 } 5648 partitions++; 5649 } 5650 un->un_solaris_offset = 0; 5651 un->un_solaris_size = cap; 5652 un->un_f_geometry_is_valid = TRUE; 5653 5654 /* clear the vtoc label */ 5655 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5656 5657 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5658 5659 /* 5660 * For EFI labeled disk, create and set the partition stats 5661 * at attach time, update the stats according to dynamic 5662 * partition changes during running time. 5663 */ 5664 if (un->un_f_pkstats_enabled) { 5665 sd_set_pstats(un); 5666 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_use_efi: " 5667 "un:0x%p pstats created and set, or updated\n", un); 5668 } 5669 return (0); 5670 5671 done_err: 5672 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5673 mutex_enter(SD_MUTEX(un)); 5674 /* 5675 * if we didn't find something that could look like a VTOC 5676 * and the disk is over 1TB, we know there isn't a valid label. 5677 * Otherwise let sd_uselabel decide what to do. We only 5678 * want to invalidate this if we're certain the label isn't 5679 * valid because sd_prop_op will now fail, which in turn 5680 * causes things like opens and stats on the partition to fail. 5681 */ 5682 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5683 un->un_f_geometry_is_valid = FALSE; 5684 } 5685 return (rval); 5686 } 5687 5688 5689 /* 5690 * Function: sd_uselabel 5691 * 5692 * Description: Validate the disk label and update the relevant data (geometry, 5693 * partition, vtoc, and capacity data) in the sd_lun struct. 5694 * Marks the geometry of the unit as being valid. 5695 * 5696 * Arguments: un: unit struct. 5697 * dk_label: disk label 5698 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5699 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5700 * to use the USCSI "direct" chain and bypass the normal 5701 * command waitq. 5702 * 5703 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5704 * partition, vtoc, and capacity data are good. 5705 * 5706 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5707 * label; or computed capacity does not jibe with capacity 5708 * reported from the READ CAPACITY command. 5709 * 5710 * Context: Kernel thread only (can sleep). 5711 */ 5712 5713 static int 5714 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5715 { 5716 short *sp; 5717 short sum; 5718 short count; 5719 int label_error = SD_LABEL_IS_VALID; 5720 int i; 5721 int capacity; 5722 int part_end; 5723 int track_capacity; 5724 int err; 5725 #if defined(_SUNOS_VTOC_16) 5726 struct dkl_partition *vpartp; 5727 #endif 5728 ASSERT(un != NULL); 5729 ASSERT(mutex_owned(SD_MUTEX(un))); 5730 5731 /* Validate the magic number of the label. */ 5732 if (labp->dkl_magic != DKL_MAGIC) { 5733 #if defined(__sparc) 5734 if ((un->un_state == SD_STATE_NORMAL) && 5735 un->un_f_vtoc_errlog_supported) { 5736 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5737 "Corrupt label; wrong magic number\n"); 5738 } 5739 #endif 5740 return (SD_LABEL_IS_INVALID); 5741 } 5742 5743 /* Validate the checksum of the label. */ 5744 sp = (short *)labp; 5745 sum = 0; 5746 count = sizeof (struct dk_label) / sizeof (short); 5747 while (count--) { 5748 sum ^= *sp++; 5749 } 5750 5751 if (sum != 0) { 5752 #if defined(_SUNOS_VTOC_16) 5753 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5754 #elif defined(_SUNOS_VTOC_8) 5755 if ((un->un_state == SD_STATE_NORMAL) && 5756 un->un_f_vtoc_errlog_supported) { 5757 #endif 5758 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5759 "Corrupt label - label checksum failed\n"); 5760 } 5761 return (SD_LABEL_IS_INVALID); 5762 } 5763 5764 5765 /* 5766 * Fill in geometry structure with data from label. 5767 */ 5768 bzero(&un->un_g, sizeof (struct dk_geom)); 5769 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5770 un->un_g.dkg_acyl = labp->dkl_acyl; 5771 un->un_g.dkg_bcyl = 0; 5772 un->un_g.dkg_nhead = labp->dkl_nhead; 5773 un->un_g.dkg_nsect = labp->dkl_nsect; 5774 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5775 5776 #if defined(_SUNOS_VTOC_8) 5777 un->un_g.dkg_gap1 = labp->dkl_gap1; 5778 un->un_g.dkg_gap2 = labp->dkl_gap2; 5779 un->un_g.dkg_bhead = labp->dkl_bhead; 5780 #endif 5781 #if defined(_SUNOS_VTOC_16) 5782 un->un_dkg_skew = labp->dkl_skew; 5783 #endif 5784 5785 #if defined(__i386) || defined(__amd64) 5786 un->un_g.dkg_apc = labp->dkl_apc; 5787 #endif 5788 5789 /* 5790 * Currently we rely on the values in the label being accurate. If 5791 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5792 * 5793 * Note: In the future a MODE SENSE may be used to retrieve this data, 5794 * although this command is optional in SCSI-2. 5795 */ 5796 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5797 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5798 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5799 5800 /* 5801 * The Read and Write reinstruct values may not be valid 5802 * for older disks. 5803 */ 5804 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5805 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5806 5807 /* Fill in partition table. */ 5808 #if defined(_SUNOS_VTOC_8) 5809 for (i = 0; i < NDKMAP; i++) { 5810 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5811 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5812 } 5813 #endif 5814 #if defined(_SUNOS_VTOC_16) 5815 vpartp = labp->dkl_vtoc.v_part; 5816 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5817 5818 /* Prevent divide by zero */ 5819 if (track_capacity == 0) { 5820 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5821 "Corrupt label - zero nhead or nsect value\n"); 5822 5823 return (SD_LABEL_IS_INVALID); 5824 } 5825 5826 for (i = 0; i < NDKMAP; i++, vpartp++) { 5827 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5828 un->un_map[i].dkl_nblk = vpartp->p_size; 5829 } 5830 #endif 5831 5832 /* Fill in VTOC Structure. */ 5833 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5834 #if defined(_SUNOS_VTOC_8) 5835 /* 5836 * The 8-slice vtoc does not include the ascii label; save it into 5837 * the device's soft state structure here. 5838 */ 5839 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5840 #endif 5841 5842 /* Now look for a valid capacity. */ 5843 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5844 capacity = (un->un_g.dkg_ncyl * track_capacity); 5845 5846 if (un->un_g.dkg_acyl) { 5847 #if defined(__i386) || defined(__amd64) 5848 /* we may have > 1 alts cylinder */ 5849 capacity += (track_capacity * un->un_g.dkg_acyl); 5850 #else 5851 capacity += track_capacity; 5852 #endif 5853 } 5854 5855 /* 5856 * Force check here to ensure the computed capacity is valid. 5857 * If capacity is zero, it indicates an invalid label and 5858 * we should abort updating the relevant data then. 5859 */ 5860 if (capacity == 0) { 5861 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5862 "Corrupt label - no valid capacity could be retrieved\n"); 5863 5864 return (SD_LABEL_IS_INVALID); 5865 } 5866 5867 /* Mark the geometry as valid. */ 5868 un->un_f_geometry_is_valid = TRUE; 5869 5870 /* 5871 * At this point, un->un_blockcount should contain valid data from 5872 * the READ CAPACITY command. 5873 */ 5874 if (un->un_f_blockcount_is_valid != TRUE) { 5875 /* 5876 * We have a situation where the target didn't give us a good 5877 * READ CAPACITY value, yet there appears to be a valid label. 5878 * In this case, we'll fake the capacity. 5879 */ 5880 un->un_blockcount = capacity; 5881 un->un_f_blockcount_is_valid = TRUE; 5882 goto done; 5883 } 5884 5885 5886 if ((capacity <= un->un_blockcount) || 5887 (un->un_state != SD_STATE_NORMAL)) { 5888 #if defined(_SUNOS_VTOC_8) 5889 /* 5890 * We can't let this happen on drives that are subdivided 5891 * into logical disks (i.e., that have an fdisk table). 5892 * The un_blockcount field should always hold the full media 5893 * size in sectors, period. This code would overwrite 5894 * un_blockcount with the size of the Solaris fdisk partition. 5895 */ 5896 SD_ERROR(SD_LOG_COMMON, un, 5897 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5898 capacity, un->un_blockcount); 5899 un->un_blockcount = capacity; 5900 un->un_f_blockcount_is_valid = TRUE; 5901 #endif /* defined(_SUNOS_VTOC_8) */ 5902 goto done; 5903 } 5904 5905 if (ISCD(un)) { 5906 /* For CDROMs, we trust that the data in the label is OK. */ 5907 #if defined(_SUNOS_VTOC_8) 5908 for (i = 0; i < NDKMAP; i++) { 5909 part_end = labp->dkl_nhead * labp->dkl_nsect * 5910 labp->dkl_map[i].dkl_cylno + 5911 labp->dkl_map[i].dkl_nblk - 1; 5912 5913 if ((labp->dkl_map[i].dkl_nblk) && 5914 (part_end > un->un_blockcount)) { 5915 un->un_f_geometry_is_valid = FALSE; 5916 break; 5917 } 5918 } 5919 #endif 5920 #if defined(_SUNOS_VTOC_16) 5921 vpartp = &(labp->dkl_vtoc.v_part[0]); 5922 for (i = 0; i < NDKMAP; i++, vpartp++) { 5923 part_end = vpartp->p_start + vpartp->p_size; 5924 if ((vpartp->p_size > 0) && 5925 (part_end > un->un_blockcount)) { 5926 un->un_f_geometry_is_valid = FALSE; 5927 break; 5928 } 5929 } 5930 #endif 5931 } else { 5932 uint64_t t_capacity; 5933 uint32_t t_lbasize; 5934 5935 mutex_exit(SD_MUTEX(un)); 5936 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5937 path_flag); 5938 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5939 mutex_enter(SD_MUTEX(un)); 5940 5941 if (err == 0) { 5942 sd_update_block_info(un, t_lbasize, t_capacity); 5943 } 5944 5945 if (capacity > un->un_blockcount) { 5946 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5947 "Corrupt label - bad geometry\n"); 5948 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5949 "Label says %u blocks; Drive says %llu blocks\n", 5950 capacity, (unsigned long long)un->un_blockcount); 5951 un->un_f_geometry_is_valid = FALSE; 5952 label_error = SD_LABEL_IS_INVALID; 5953 } 5954 } 5955 5956 done: 5957 5958 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5959 SD_INFO(SD_LOG_COMMON, un, 5960 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5961 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5962 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5963 SD_INFO(SD_LOG_COMMON, un, 5964 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5965 un->un_tgt_blocksize, un->un_blockcount, 5966 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5967 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5968 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5969 5970 ASSERT(mutex_owned(SD_MUTEX(un))); 5971 5972 return (label_error); 5973 } 5974 5975 5976 /* 5977 * Function: sd_build_default_label 5978 * 5979 * Description: Generate a default label for those devices that do not have 5980 * one, e.g., new media, removable cartridges, etc.. 5981 * 5982 * Context: Kernel thread only 5983 */ 5984 5985 static void 5986 sd_build_default_label(struct sd_lun *un) 5987 { 5988 #if defined(_SUNOS_VTOC_16) 5989 uint_t phys_spc; 5990 uint_t disksize; 5991 struct dk_geom un_g; 5992 uint64_t capacity; 5993 #endif 5994 5995 ASSERT(un != NULL); 5996 ASSERT(mutex_owned(SD_MUTEX(un))); 5997 5998 #if defined(_SUNOS_VTOC_8) 5999 /* 6000 * Note: This is a legacy check for non-removable devices on VTOC_8 6001 * only. This may be a valid check for VTOC_16 as well. 6002 * Once we understand why there is this difference between SPARC and 6003 * x86 platform, we could remove this legacy check. 6004 */ 6005 ASSERT(un->un_f_default_vtoc_supported); 6006 #endif 6007 6008 bzero(&un->un_g, sizeof (struct dk_geom)); 6009 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 6010 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 6011 6012 #if defined(_SUNOS_VTOC_8) 6013 6014 /* 6015 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 6016 * But it is still necessary to set up various geometry information, 6017 * and we are doing this here. 6018 */ 6019 6020 /* 6021 * For the rpm, we use the minimum for the disk. For the head, cyl, 6022 * and number of sector per track, if the capacity <= 1GB, head = 64, 6023 * sect = 32. else head = 255, sect 63 Note: the capacity should be 6024 * equal to C*H*S values. This will cause some truncation of size due 6025 * to round off errors. For CD-ROMs, this truncation can have adverse 6026 * side effects, so returning ncyl and nhead as 1. The nsect will 6027 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 6028 */ 6029 if (ISCD(un)) { 6030 /* 6031 * Preserve the old behavior for non-writable 6032 * medias. Since dkg_nsect is a ushort, it 6033 * will lose bits as cdroms have more than 6034 * 65536 sectors. So if we recalculate 6035 * capacity, it will become much shorter. 6036 * But the dkg_* information is not 6037 * used for CDROMs so it is OK. But for 6038 * Writable CDs we need this information 6039 * to be valid (for newfs say). So we 6040 * make nsect and nhead > 1 that way 6041 * nsect can still stay within ushort limit 6042 * without losing any bits. 6043 */ 6044 if (un->un_f_mmc_writable_media == TRUE) { 6045 un->un_g.dkg_nhead = 64; 6046 un->un_g.dkg_nsect = 32; 6047 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6048 un->un_blockcount = un->un_g.dkg_ncyl * 6049 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6050 } else { 6051 un->un_g.dkg_ncyl = 1; 6052 un->un_g.dkg_nhead = 1; 6053 un->un_g.dkg_nsect = un->un_blockcount; 6054 } 6055 } else { 6056 if (un->un_blockcount <= 0x1000) { 6057 /* unlabeled SCSI floppy device */ 6058 un->un_g.dkg_nhead = 2; 6059 un->un_g.dkg_ncyl = 80; 6060 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 6061 } else if (un->un_blockcount <= 0x200000) { 6062 un->un_g.dkg_nhead = 64; 6063 un->un_g.dkg_nsect = 32; 6064 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6065 } else { 6066 un->un_g.dkg_nhead = 255; 6067 un->un_g.dkg_nsect = 63; 6068 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 6069 } 6070 un->un_blockcount = 6071 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6072 } 6073 6074 un->un_g.dkg_acyl = 0; 6075 un->un_g.dkg_bcyl = 0; 6076 un->un_g.dkg_rpm = 200; 6077 un->un_asciilabel[0] = '\0'; 6078 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 6079 6080 un->un_map[0].dkl_cylno = 0; 6081 un->un_map[0].dkl_nblk = un->un_blockcount; 6082 un->un_map[2].dkl_cylno = 0; 6083 un->un_map[2].dkl_nblk = un->un_blockcount; 6084 6085 #elif defined(_SUNOS_VTOC_16) 6086 6087 if (un->un_solaris_size == 0) { 6088 /* 6089 * Got fdisk table but no solaris entry therefore 6090 * don't create a default label 6091 */ 6092 un->un_f_geometry_is_valid = TRUE; 6093 return; 6094 } 6095 6096 /* 6097 * For CDs we continue to use the physical geometry to calculate 6098 * number of cylinders. All other devices must convert the 6099 * physical geometry (geom_cache) to values that will fit 6100 * in a dk_geom structure. 6101 */ 6102 if (ISCD(un)) { 6103 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 6104 } else { 6105 /* Convert physical geometry to disk geometry */ 6106 bzero(&un_g, sizeof (struct dk_geom)); 6107 6108 /* 6109 * Refer to comments related to off-by-1 at the 6110 * header of this file. 6111 * Before caculating geometry, capacity should be 6112 * decreased by 1. That un_f_capacity_adjusted is 6113 * TRUE means that we are treating a 1TB disk as 6114 * (1T - 512)B. And the capacity of disks is already 6115 * decreased by 1. 6116 */ 6117 if (!un->un_f_capacity_adjusted && 6118 !un->un_f_has_removable_media && 6119 !un->un_f_is_hotpluggable && 6120 un->un_tgt_blocksize == un->un_sys_blocksize) 6121 capacity = un->un_blockcount - 1; 6122 else 6123 capacity = un->un_blockcount; 6124 6125 sd_convert_geometry(capacity, &un_g); 6126 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 6127 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6128 } 6129 6130 ASSERT(phys_spc != 0); 6131 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 6132 un->un_g.dkg_acyl = DK_ACYL; 6133 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 6134 disksize = un->un_g.dkg_ncyl * phys_spc; 6135 6136 if (ISCD(un)) { 6137 /* 6138 * CD's don't use the "heads * sectors * cyls"-type of 6139 * geometry, but instead use the entire capacity of the media. 6140 */ 6141 disksize = un->un_solaris_size; 6142 un->un_g.dkg_nhead = 1; 6143 un->un_g.dkg_nsect = 1; 6144 un->un_g.dkg_rpm = 6145 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 6146 6147 un->un_vtoc.v_part[0].p_start = 0; 6148 un->un_vtoc.v_part[0].p_size = disksize; 6149 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 6150 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 6151 6152 un->un_map[0].dkl_cylno = 0; 6153 un->un_map[0].dkl_nblk = disksize; 6154 un->un_offset[0] = 0; 6155 6156 } else { 6157 /* 6158 * Hard disks and removable media cartridges 6159 */ 6160 un->un_g.dkg_rpm = 6161 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 6162 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 6163 6164 /* Add boot slice */ 6165 un->un_vtoc.v_part[8].p_start = 0; 6166 un->un_vtoc.v_part[8].p_size = phys_spc; 6167 un->un_vtoc.v_part[8].p_tag = V_BOOT; 6168 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 6169 6170 un->un_map[8].dkl_cylno = 0; 6171 un->un_map[8].dkl_nblk = phys_spc; 6172 un->un_offset[8] = 0; 6173 } 6174 6175 un->un_g.dkg_apc = 0; 6176 un->un_vtoc.v_nparts = V_NUMPAR; 6177 un->un_vtoc.v_version = V_VERSION; 6178 6179 /* Add backup slice */ 6180 un->un_vtoc.v_part[2].p_start = 0; 6181 un->un_vtoc.v_part[2].p_size = disksize; 6182 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 6183 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 6184 6185 un->un_map[2].dkl_cylno = 0; 6186 un->un_map[2].dkl_nblk = disksize; 6187 un->un_offset[2] = 0; 6188 6189 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 6190 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 6191 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 6192 6193 #else 6194 #error "No VTOC format defined." 6195 #endif 6196 6197 un->un_g.dkg_read_reinstruct = 0; 6198 un->un_g.dkg_write_reinstruct = 0; 6199 6200 un->un_g.dkg_intrlv = 1; 6201 6202 un->un_vtoc.v_sanity = VTOC_SANE; 6203 6204 un->un_f_geometry_is_valid = TRUE; 6205 6206 SD_INFO(SD_LOG_COMMON, un, 6207 "sd_build_default_label: Default label created: " 6208 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 6209 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 6210 un->un_g.dkg_nsect, un->un_blockcount); 6211 } 6212 6213 6214 #if defined(_FIRMWARE_NEEDS_FDISK) 6215 /* 6216 * Max CHS values, as they are encoded into bytes, for 1022/254/63 6217 */ 6218 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 6219 #define LBA_MAX_CYL (1022 & 0xFF) 6220 #define LBA_MAX_HEAD (254) 6221 6222 6223 /* 6224 * Function: sd_has_max_chs_vals 6225 * 6226 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 6227 * 6228 * Arguments: fdp - ptr to CHS info 6229 * 6230 * Return Code: True or false 6231 * 6232 * Context: Any. 6233 */ 6234 6235 static int 6236 sd_has_max_chs_vals(struct ipart *fdp) 6237 { 6238 return ((fdp->begcyl == LBA_MAX_CYL) && 6239 (fdp->beghead == LBA_MAX_HEAD) && 6240 (fdp->begsect == LBA_MAX_SECT) && 6241 (fdp->endcyl == LBA_MAX_CYL) && 6242 (fdp->endhead == LBA_MAX_HEAD) && 6243 (fdp->endsect == LBA_MAX_SECT)); 6244 } 6245 #endif 6246 6247 6248 /* 6249 * Function: sd_inq_fill 6250 * 6251 * Description: Print a piece of inquiry data, cleaned up for non-printable 6252 * characters and stopping at the first space character after 6253 * the beginning of the passed string; 6254 * 6255 * Arguments: p - source string 6256 * l - maximum length to copy 6257 * s - destination string 6258 * 6259 * Context: Any. 6260 */ 6261 6262 static void 6263 sd_inq_fill(char *p, int l, char *s) 6264 { 6265 unsigned i = 0; 6266 char c; 6267 6268 while (i++ < l) { 6269 if ((c = *p++) < ' ' || c >= 0x7F) { 6270 c = '*'; 6271 } else if (i != 1 && c == ' ') { 6272 break; 6273 } 6274 *s++ = c; 6275 } 6276 *s++ = 0; 6277 } 6278 6279 6280 /* 6281 * Function: sd_register_devid 6282 * 6283 * Description: This routine will obtain the device id information from the 6284 * target, obtain the serial number, and register the device 6285 * id with the ddi framework. 6286 * 6287 * Arguments: devi - the system's dev_info_t for the device. 6288 * un - driver soft state (unit) structure 6289 * reservation_flag - indicates if a reservation conflict 6290 * occurred during attach 6291 * 6292 * Context: Kernel Thread 6293 */ 6294 static void 6295 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6296 { 6297 int rval = 0; 6298 uchar_t *inq80 = NULL; 6299 size_t inq80_len = MAX_INQUIRY_SIZE; 6300 size_t inq80_resid = 0; 6301 uchar_t *inq83 = NULL; 6302 size_t inq83_len = MAX_INQUIRY_SIZE; 6303 size_t inq83_resid = 0; 6304 6305 ASSERT(un != NULL); 6306 ASSERT(mutex_owned(SD_MUTEX(un))); 6307 ASSERT((SD_DEVINFO(un)) == devi); 6308 6309 /* 6310 * This is the case of antiquated Sun disk drives that have the 6311 * FAB_DEVID property set in the disk_table. These drives 6312 * manage the devid's by storing them in last 2 available sectors 6313 * on the drive and have them fabricated by the ddi layer by calling 6314 * ddi_devid_init and passing the DEVID_FAB flag. 6315 */ 6316 if (un->un_f_opt_fab_devid == TRUE) { 6317 /* 6318 * Depending on EINVAL isn't reliable, since a reserved disk 6319 * may result in invalid geometry, so check to make sure a 6320 * reservation conflict did not occur during attach. 6321 */ 6322 if ((sd_get_devid(un) == EINVAL) && 6323 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6324 /* 6325 * The devid is invalid AND there is no reservation 6326 * conflict. Fabricate a new devid. 6327 */ 6328 (void) sd_create_devid(un); 6329 } 6330 6331 /* Register the devid if it exists */ 6332 if (un->un_devid != NULL) { 6333 (void) ddi_devid_register(SD_DEVINFO(un), 6334 un->un_devid); 6335 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6336 "sd_register_devid: Devid Fabricated\n"); 6337 } 6338 return; 6339 } 6340 6341 /* 6342 * We check the availibility of the World Wide Name (0x83) and Unit 6343 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6344 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6345 * 0x83 is availible, that is the best choice. Our next choice is 6346 * 0x80. If neither are availible, we munge the devid from the device 6347 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6348 * to fabricate a devid for non-Sun qualified disks. 6349 */ 6350 if (sd_check_vpd_page_support(un) == 0) { 6351 /* collect page 80 data if available */ 6352 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6353 6354 mutex_exit(SD_MUTEX(un)); 6355 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6356 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6357 0x01, 0x80, &inq80_resid); 6358 6359 if (rval != 0) { 6360 kmem_free(inq80, inq80_len); 6361 inq80 = NULL; 6362 inq80_len = 0; 6363 } 6364 mutex_enter(SD_MUTEX(un)); 6365 } 6366 6367 /* collect page 83 data if available */ 6368 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6369 mutex_exit(SD_MUTEX(un)); 6370 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6371 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6372 0x01, 0x83, &inq83_resid); 6373 6374 if (rval != 0) { 6375 kmem_free(inq83, inq83_len); 6376 inq83 = NULL; 6377 inq83_len = 0; 6378 } 6379 mutex_enter(SD_MUTEX(un)); 6380 } 6381 } 6382 6383 /* encode best devid possible based on data available */ 6384 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6385 (char *)ddi_driver_name(SD_DEVINFO(un)), 6386 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6387 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6388 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6389 6390 /* devid successfully encoded, register devid */ 6391 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6392 6393 } else { 6394 /* 6395 * Unable to encode a devid based on data available. 6396 * This is not a Sun qualified disk. Older Sun disk 6397 * drives that have the SD_FAB_DEVID property 6398 * set in the disk_table and non Sun qualified 6399 * disks are treated in the same manner. These 6400 * drives manage the devid's by storing them in 6401 * last 2 available sectors on the drive and 6402 * have them fabricated by the ddi layer by 6403 * calling ddi_devid_init and passing the 6404 * DEVID_FAB flag. 6405 * Create a fabricate devid only if there's no 6406 * fabricate devid existed. 6407 */ 6408 if (sd_get_devid(un) == EINVAL) { 6409 (void) sd_create_devid(un); 6410 } 6411 un->un_f_opt_fab_devid = TRUE; 6412 6413 /* Register the devid if it exists */ 6414 if (un->un_devid != NULL) { 6415 (void) ddi_devid_register(SD_DEVINFO(un), 6416 un->un_devid); 6417 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6418 "sd_register_devid: devid fabricated using " 6419 "ddi framework\n"); 6420 } 6421 } 6422 6423 /* clean up resources */ 6424 if (inq80 != NULL) { 6425 kmem_free(inq80, inq80_len); 6426 } 6427 if (inq83 != NULL) { 6428 kmem_free(inq83, inq83_len); 6429 } 6430 } 6431 6432 static daddr_t 6433 sd_get_devid_block(struct sd_lun *un) 6434 { 6435 daddr_t spc, blk, head, cyl; 6436 6437 if ((un->un_f_geometry_is_valid == FALSE) || 6438 (un->un_solaris_size < DK_LABEL_LOC)) 6439 return (-1); 6440 6441 if (un->un_vtoc.v_sanity != VTOC_SANE) { 6442 /* EFI labeled */ 6443 if (un->un_reserved != -1) { 6444 blk = un->un_map[un->un_reserved].dkl_cylno; 6445 } else { 6446 return (-1); 6447 } 6448 } else { 6449 /* SMI labeled */ 6450 /* this geometry doesn't allow us to write a devid */ 6451 if (un->un_g.dkg_acyl < 2) { 6452 return (-1); 6453 } 6454 6455 /* 6456 * Subtract 2 guarantees that the next to last cylinder 6457 * is used 6458 */ 6459 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6460 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6461 head = un->un_g.dkg_nhead - 1; 6462 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6463 (head * un->un_g.dkg_nsect) + 1; 6464 } 6465 return (blk); 6466 } 6467 6468 /* 6469 * Function: sd_get_devid 6470 * 6471 * Description: This routine will return 0 if a valid device id has been 6472 * obtained from the target and stored in the soft state. If a 6473 * valid device id has not been previously read and stored, a 6474 * read attempt will be made. 6475 * 6476 * Arguments: un - driver soft state (unit) structure 6477 * 6478 * Return Code: 0 if we successfully get the device id 6479 * 6480 * Context: Kernel Thread 6481 */ 6482 6483 static int 6484 sd_get_devid(struct sd_lun *un) 6485 { 6486 struct dk_devid *dkdevid; 6487 ddi_devid_t tmpid; 6488 uint_t *ip; 6489 size_t sz; 6490 daddr_t blk; 6491 int status; 6492 int chksum; 6493 int i; 6494 size_t buffer_size; 6495 6496 ASSERT(un != NULL); 6497 ASSERT(mutex_owned(SD_MUTEX(un))); 6498 6499 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6500 un); 6501 6502 if (un->un_devid != NULL) { 6503 return (0); 6504 } 6505 6506 blk = sd_get_devid_block(un); 6507 if (blk < 0) 6508 return (EINVAL); 6509 6510 /* 6511 * Read and verify device id, stored in the reserved cylinders at the 6512 * end of the disk. Backup label is on the odd sectors of the last 6513 * track of the last cylinder. Device id will be on track of the next 6514 * to last cylinder. 6515 */ 6516 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6517 mutex_exit(SD_MUTEX(un)); 6518 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6519 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6520 SD_PATH_DIRECT); 6521 if (status != 0) { 6522 goto error; 6523 } 6524 6525 /* Validate the revision */ 6526 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6527 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6528 status = EINVAL; 6529 goto error; 6530 } 6531 6532 /* Calculate the checksum */ 6533 chksum = 0; 6534 ip = (uint_t *)dkdevid; 6535 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6536 i++) { 6537 chksum ^= ip[i]; 6538 } 6539 6540 /* Compare the checksums */ 6541 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6542 status = EINVAL; 6543 goto error; 6544 } 6545 6546 /* Validate the device id */ 6547 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6548 status = EINVAL; 6549 goto error; 6550 } 6551 6552 /* 6553 * Store the device id in the driver soft state 6554 */ 6555 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6556 tmpid = kmem_alloc(sz, KM_SLEEP); 6557 6558 mutex_enter(SD_MUTEX(un)); 6559 6560 un->un_devid = tmpid; 6561 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6562 6563 kmem_free(dkdevid, buffer_size); 6564 6565 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6566 6567 return (status); 6568 error: 6569 mutex_enter(SD_MUTEX(un)); 6570 kmem_free(dkdevid, buffer_size); 6571 return (status); 6572 } 6573 6574 6575 /* 6576 * Function: sd_create_devid 6577 * 6578 * Description: This routine will fabricate the device id and write it 6579 * to the disk. 6580 * 6581 * Arguments: un - driver soft state (unit) structure 6582 * 6583 * Return Code: value of the fabricated device id 6584 * 6585 * Context: Kernel Thread 6586 */ 6587 6588 static ddi_devid_t 6589 sd_create_devid(struct sd_lun *un) 6590 { 6591 ASSERT(un != NULL); 6592 6593 /* Fabricate the devid */ 6594 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6595 == DDI_FAILURE) { 6596 return (NULL); 6597 } 6598 6599 /* Write the devid to disk */ 6600 if (sd_write_deviceid(un) != 0) { 6601 ddi_devid_free(un->un_devid); 6602 un->un_devid = NULL; 6603 } 6604 6605 return (un->un_devid); 6606 } 6607 6608 6609 /* 6610 * Function: sd_write_deviceid 6611 * 6612 * Description: This routine will write the device id to the disk 6613 * reserved sector. 6614 * 6615 * Arguments: un - driver soft state (unit) structure 6616 * 6617 * Return Code: EINVAL 6618 * value returned by sd_send_scsi_cmd 6619 * 6620 * Context: Kernel Thread 6621 */ 6622 6623 static int 6624 sd_write_deviceid(struct sd_lun *un) 6625 { 6626 struct dk_devid *dkdevid; 6627 daddr_t blk; 6628 uint_t *ip, chksum; 6629 int status; 6630 int i; 6631 6632 ASSERT(mutex_owned(SD_MUTEX(un))); 6633 6634 blk = sd_get_devid_block(un); 6635 if (blk < 0) 6636 return (-1); 6637 mutex_exit(SD_MUTEX(un)); 6638 6639 /* Allocate the buffer */ 6640 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6641 6642 /* Fill in the revision */ 6643 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6644 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6645 6646 /* Copy in the device id */ 6647 mutex_enter(SD_MUTEX(un)); 6648 bcopy(un->un_devid, &dkdevid->dkd_devid, 6649 ddi_devid_sizeof(un->un_devid)); 6650 mutex_exit(SD_MUTEX(un)); 6651 6652 /* Calculate the checksum */ 6653 chksum = 0; 6654 ip = (uint_t *)dkdevid; 6655 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6656 i++) { 6657 chksum ^= ip[i]; 6658 } 6659 6660 /* Fill-in checksum */ 6661 DKD_FORMCHKSUM(chksum, dkdevid); 6662 6663 /* Write the reserved sector */ 6664 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6665 SD_PATH_DIRECT); 6666 6667 kmem_free(dkdevid, un->un_sys_blocksize); 6668 6669 mutex_enter(SD_MUTEX(un)); 6670 return (status); 6671 } 6672 6673 6674 /* 6675 * Function: sd_check_vpd_page_support 6676 * 6677 * Description: This routine sends an inquiry command with the EVPD bit set and 6678 * a page code of 0x00 to the device. It is used to determine which 6679 * vital product pages are availible to find the devid. We are 6680 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6681 * device does not support that command. 6682 * 6683 * Arguments: un - driver soft state (unit) structure 6684 * 6685 * Return Code: 0 - success 6686 * 1 - check condition 6687 * 6688 * Context: This routine can sleep. 6689 */ 6690 6691 static int 6692 sd_check_vpd_page_support(struct sd_lun *un) 6693 { 6694 uchar_t *page_list = NULL; 6695 uchar_t page_length = 0xff; /* Use max possible length */ 6696 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6697 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6698 int rval = 0; 6699 int counter; 6700 6701 ASSERT(un != NULL); 6702 ASSERT(mutex_owned(SD_MUTEX(un))); 6703 6704 mutex_exit(SD_MUTEX(un)); 6705 6706 /* 6707 * We'll set the page length to the maximum to save figuring it out 6708 * with an additional call. 6709 */ 6710 page_list = kmem_zalloc(page_length, KM_SLEEP); 6711 6712 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6713 page_code, NULL); 6714 6715 mutex_enter(SD_MUTEX(un)); 6716 6717 /* 6718 * Now we must validate that the device accepted the command, as some 6719 * drives do not support it. If the drive does support it, we will 6720 * return 0, and the supported pages will be in un_vpd_page_mask. If 6721 * not, we return -1. 6722 */ 6723 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6724 /* Loop to find one of the 2 pages we need */ 6725 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6726 6727 /* 6728 * Pages are returned in ascending order, and 0x83 is what we 6729 * are hoping for. 6730 */ 6731 while ((page_list[counter] <= 0x83) && 6732 (counter <= (page_list[VPD_PAGE_LENGTH] + 6733 VPD_HEAD_OFFSET))) { 6734 /* 6735 * Add 3 because page_list[3] is the number of 6736 * pages minus 3 6737 */ 6738 6739 switch (page_list[counter]) { 6740 case 0x00: 6741 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6742 break; 6743 case 0x80: 6744 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6745 break; 6746 case 0x81: 6747 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6748 break; 6749 case 0x82: 6750 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6751 break; 6752 case 0x83: 6753 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6754 break; 6755 } 6756 counter++; 6757 } 6758 6759 } else { 6760 rval = -1; 6761 6762 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6763 "sd_check_vpd_page_support: This drive does not implement " 6764 "VPD pages.\n"); 6765 } 6766 6767 kmem_free(page_list, page_length); 6768 6769 return (rval); 6770 } 6771 6772 6773 /* 6774 * Function: sd_setup_pm 6775 * 6776 * Description: Initialize Power Management on the device 6777 * 6778 * Context: Kernel Thread 6779 */ 6780 6781 static void 6782 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6783 { 6784 uint_t log_page_size; 6785 uchar_t *log_page_data; 6786 int rval; 6787 6788 /* 6789 * Since we are called from attach, holding a mutex for 6790 * un is unnecessary. Because some of the routines called 6791 * from here require SD_MUTEX to not be held, assert this 6792 * right up front. 6793 */ 6794 ASSERT(!mutex_owned(SD_MUTEX(un))); 6795 /* 6796 * Since the sd device does not have the 'reg' property, 6797 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6798 * The following code is to tell cpr that this device 6799 * DOES need to be suspended and resumed. 6800 */ 6801 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6802 "pm-hardware-state", "needs-suspend-resume"); 6803 6804 /* 6805 * This complies with the new power management framework 6806 * for certain desktop machines. Create the pm_components 6807 * property as a string array property. 6808 */ 6809 if (un->un_f_pm_supported) { 6810 /* 6811 * not all devices have a motor, try it first. 6812 * some devices may return ILLEGAL REQUEST, some 6813 * will hang 6814 * The following START_STOP_UNIT is used to check if target 6815 * device has a motor. 6816 */ 6817 un->un_f_start_stop_supported = TRUE; 6818 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6819 SD_PATH_DIRECT) != 0) { 6820 un->un_f_start_stop_supported = FALSE; 6821 } 6822 6823 /* 6824 * create pm properties anyways otherwise the parent can't 6825 * go to sleep 6826 */ 6827 (void) sd_create_pm_components(devi, un); 6828 un->un_f_pm_is_enabled = TRUE; 6829 return; 6830 } 6831 6832 if (!un->un_f_log_sense_supported) { 6833 un->un_power_level = SD_SPINDLE_ON; 6834 un->un_f_pm_is_enabled = FALSE; 6835 return; 6836 } 6837 6838 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6839 6840 #ifdef SDDEBUG 6841 if (sd_force_pm_supported) { 6842 /* Force a successful result */ 6843 rval = 1; 6844 } 6845 #endif 6846 6847 /* 6848 * If the start-stop cycle counter log page is not supported 6849 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6850 * then we should not create the pm_components property. 6851 */ 6852 if (rval == -1) { 6853 /* 6854 * Error. 6855 * Reading log sense failed, most likely this is 6856 * an older drive that does not support log sense. 6857 * If this fails auto-pm is not supported. 6858 */ 6859 un->un_power_level = SD_SPINDLE_ON; 6860 un->un_f_pm_is_enabled = FALSE; 6861 6862 } else if (rval == 0) { 6863 /* 6864 * Page not found. 6865 * The start stop cycle counter is implemented as page 6866 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6867 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6868 */ 6869 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6870 /* 6871 * Page found, use this one. 6872 */ 6873 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6874 un->un_f_pm_is_enabled = TRUE; 6875 } else { 6876 /* 6877 * Error or page not found. 6878 * auto-pm is not supported for this device. 6879 */ 6880 un->un_power_level = SD_SPINDLE_ON; 6881 un->un_f_pm_is_enabled = FALSE; 6882 } 6883 } else { 6884 /* 6885 * Page found, use it. 6886 */ 6887 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6888 un->un_f_pm_is_enabled = TRUE; 6889 } 6890 6891 6892 if (un->un_f_pm_is_enabled == TRUE) { 6893 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6894 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6895 6896 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6897 log_page_size, un->un_start_stop_cycle_page, 6898 0x01, 0, SD_PATH_DIRECT); 6899 #ifdef SDDEBUG 6900 if (sd_force_pm_supported) { 6901 /* Force a successful result */ 6902 rval = 0; 6903 } 6904 #endif 6905 6906 /* 6907 * If the Log sense for Page( Start/stop cycle counter page) 6908 * succeeds, then power managment is supported and we can 6909 * enable auto-pm. 6910 */ 6911 if (rval == 0) { 6912 (void) sd_create_pm_components(devi, un); 6913 } else { 6914 un->un_power_level = SD_SPINDLE_ON; 6915 un->un_f_pm_is_enabled = FALSE; 6916 } 6917 6918 kmem_free(log_page_data, log_page_size); 6919 } 6920 } 6921 6922 6923 /* 6924 * Function: sd_create_pm_components 6925 * 6926 * Description: Initialize PM property. 6927 * 6928 * Context: Kernel thread context 6929 */ 6930 6931 static void 6932 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6933 { 6934 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6935 6936 ASSERT(!mutex_owned(SD_MUTEX(un))); 6937 6938 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6939 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6940 /* 6941 * When components are initially created they are idle, 6942 * power up any non-removables. 6943 * Note: the return value of pm_raise_power can't be used 6944 * for determining if PM should be enabled for this device. 6945 * Even if you check the return values and remove this 6946 * property created above, the PM framework will not honor the 6947 * change after the first call to pm_raise_power. Hence, 6948 * removal of that property does not help if pm_raise_power 6949 * fails. In the case of removable media, the start/stop 6950 * will fail if the media is not present. 6951 */ 6952 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6953 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6954 mutex_enter(SD_MUTEX(un)); 6955 un->un_power_level = SD_SPINDLE_ON; 6956 mutex_enter(&un->un_pm_mutex); 6957 /* Set to on and not busy. */ 6958 un->un_pm_count = 0; 6959 } else { 6960 mutex_enter(SD_MUTEX(un)); 6961 un->un_power_level = SD_SPINDLE_OFF; 6962 mutex_enter(&un->un_pm_mutex); 6963 /* Set to off. */ 6964 un->un_pm_count = -1; 6965 } 6966 mutex_exit(&un->un_pm_mutex); 6967 mutex_exit(SD_MUTEX(un)); 6968 } else { 6969 un->un_power_level = SD_SPINDLE_ON; 6970 un->un_f_pm_is_enabled = FALSE; 6971 } 6972 } 6973 6974 6975 /* 6976 * Function: sd_ddi_suspend 6977 * 6978 * Description: Performs system power-down operations. This includes 6979 * setting the drive state to indicate its suspended so 6980 * that no new commands will be accepted. Also, wait for 6981 * all commands that are in transport or queued to a timer 6982 * for retry to complete. All timeout threads are cancelled. 6983 * 6984 * Return Code: DDI_FAILURE or DDI_SUCCESS 6985 * 6986 * Context: Kernel thread context 6987 */ 6988 6989 static int 6990 sd_ddi_suspend(dev_info_t *devi) 6991 { 6992 struct sd_lun *un; 6993 clock_t wait_cmds_complete; 6994 6995 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6996 if (un == NULL) { 6997 return (DDI_FAILURE); 6998 } 6999 7000 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 7001 7002 mutex_enter(SD_MUTEX(un)); 7003 7004 /* Return success if the device is already suspended. */ 7005 if (un->un_state == SD_STATE_SUSPENDED) { 7006 mutex_exit(SD_MUTEX(un)); 7007 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7008 "device already suspended, exiting\n"); 7009 return (DDI_SUCCESS); 7010 } 7011 7012 /* Return failure if the device is being used by HA */ 7013 if (un->un_resvd_status & 7014 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 7015 mutex_exit(SD_MUTEX(un)); 7016 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7017 "device in use by HA, exiting\n"); 7018 return (DDI_FAILURE); 7019 } 7020 7021 /* 7022 * Return failure if the device is in a resource wait 7023 * or power changing state. 7024 */ 7025 if ((un->un_state == SD_STATE_RWAIT) || 7026 (un->un_state == SD_STATE_PM_CHANGING)) { 7027 mutex_exit(SD_MUTEX(un)); 7028 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7029 "device in resource wait state, exiting\n"); 7030 return (DDI_FAILURE); 7031 } 7032 7033 7034 un->un_save_state = un->un_last_state; 7035 New_state(un, SD_STATE_SUSPENDED); 7036 7037 /* 7038 * Wait for all commands that are in transport or queued to a timer 7039 * for retry to complete. 7040 * 7041 * While waiting, no new commands will be accepted or sent because of 7042 * the new state we set above. 7043 * 7044 * Wait till current operation has completed. If we are in the resource 7045 * wait state (with an intr outstanding) then we need to wait till the 7046 * intr completes and starts the next cmd. We want to wait for 7047 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 7048 */ 7049 wait_cmds_complete = ddi_get_lbolt() + 7050 (sd_wait_cmds_complete * drv_usectohz(1000000)); 7051 7052 while (un->un_ncmds_in_transport != 0) { 7053 /* 7054 * Fail if commands do not finish in the specified time. 7055 */ 7056 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 7057 wait_cmds_complete) == -1) { 7058 /* 7059 * Undo the state changes made above. Everything 7060 * must go back to it's original value. 7061 */ 7062 Restore_state(un); 7063 un->un_last_state = un->un_save_state; 7064 /* Wake up any threads that might be waiting. */ 7065 cv_broadcast(&un->un_suspend_cv); 7066 mutex_exit(SD_MUTEX(un)); 7067 SD_ERROR(SD_LOG_IO_PM, un, 7068 "sd_ddi_suspend: failed due to outstanding cmds\n"); 7069 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 7070 return (DDI_FAILURE); 7071 } 7072 } 7073 7074 /* 7075 * Cancel SCSI watch thread and timeouts, if any are active 7076 */ 7077 7078 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 7079 opaque_t temp_token = un->un_swr_token; 7080 mutex_exit(SD_MUTEX(un)); 7081 scsi_watch_suspend(temp_token); 7082 mutex_enter(SD_MUTEX(un)); 7083 } 7084 7085 if (un->un_reset_throttle_timeid != NULL) { 7086 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7087 un->un_reset_throttle_timeid = NULL; 7088 mutex_exit(SD_MUTEX(un)); 7089 (void) untimeout(temp_id); 7090 mutex_enter(SD_MUTEX(un)); 7091 } 7092 7093 if (un->un_dcvb_timeid != NULL) { 7094 timeout_id_t temp_id = un->un_dcvb_timeid; 7095 un->un_dcvb_timeid = NULL; 7096 mutex_exit(SD_MUTEX(un)); 7097 (void) untimeout(temp_id); 7098 mutex_enter(SD_MUTEX(un)); 7099 } 7100 7101 mutex_enter(&un->un_pm_mutex); 7102 if (un->un_pm_timeid != NULL) { 7103 timeout_id_t temp_id = un->un_pm_timeid; 7104 un->un_pm_timeid = NULL; 7105 mutex_exit(&un->un_pm_mutex); 7106 mutex_exit(SD_MUTEX(un)); 7107 (void) untimeout(temp_id); 7108 mutex_enter(SD_MUTEX(un)); 7109 } else { 7110 mutex_exit(&un->un_pm_mutex); 7111 } 7112 7113 if (un->un_retry_timeid != NULL) { 7114 timeout_id_t temp_id = un->un_retry_timeid; 7115 un->un_retry_timeid = NULL; 7116 mutex_exit(SD_MUTEX(un)); 7117 (void) untimeout(temp_id); 7118 mutex_enter(SD_MUTEX(un)); 7119 } 7120 7121 if (un->un_direct_priority_timeid != NULL) { 7122 timeout_id_t temp_id = un->un_direct_priority_timeid; 7123 un->un_direct_priority_timeid = NULL; 7124 mutex_exit(SD_MUTEX(un)); 7125 (void) untimeout(temp_id); 7126 mutex_enter(SD_MUTEX(un)); 7127 } 7128 7129 if (un->un_f_is_fibre == TRUE) { 7130 /* 7131 * Remove callbacks for insert and remove events 7132 */ 7133 if (un->un_insert_event != NULL) { 7134 mutex_exit(SD_MUTEX(un)); 7135 (void) ddi_remove_event_handler(un->un_insert_cb_id); 7136 mutex_enter(SD_MUTEX(un)); 7137 un->un_insert_event = NULL; 7138 } 7139 7140 if (un->un_remove_event != NULL) { 7141 mutex_exit(SD_MUTEX(un)); 7142 (void) ddi_remove_event_handler(un->un_remove_cb_id); 7143 mutex_enter(SD_MUTEX(un)); 7144 un->un_remove_event = NULL; 7145 } 7146 } 7147 7148 mutex_exit(SD_MUTEX(un)); 7149 7150 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 7151 7152 return (DDI_SUCCESS); 7153 } 7154 7155 7156 /* 7157 * Function: sd_ddi_pm_suspend 7158 * 7159 * Description: Set the drive state to low power. 7160 * Someone else is required to actually change the drive 7161 * power level. 7162 * 7163 * Arguments: un - driver soft state (unit) structure 7164 * 7165 * Return Code: DDI_FAILURE or DDI_SUCCESS 7166 * 7167 * Context: Kernel thread context 7168 */ 7169 7170 static int 7171 sd_ddi_pm_suspend(struct sd_lun *un) 7172 { 7173 ASSERT(un != NULL); 7174 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 7175 7176 ASSERT(!mutex_owned(SD_MUTEX(un))); 7177 mutex_enter(SD_MUTEX(un)); 7178 7179 /* 7180 * Exit if power management is not enabled for this device, or if 7181 * the device is being used by HA. 7182 */ 7183 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 7184 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 7185 mutex_exit(SD_MUTEX(un)); 7186 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 7187 return (DDI_SUCCESS); 7188 } 7189 7190 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 7191 un->un_ncmds_in_driver); 7192 7193 /* 7194 * See if the device is not busy, ie.: 7195 * - we have no commands in the driver for this device 7196 * - not waiting for resources 7197 */ 7198 if ((un->un_ncmds_in_driver == 0) && 7199 (un->un_state != SD_STATE_RWAIT)) { 7200 /* 7201 * The device is not busy, so it is OK to go to low power state. 7202 * Indicate low power, but rely on someone else to actually 7203 * change it. 7204 */ 7205 mutex_enter(&un->un_pm_mutex); 7206 un->un_pm_count = -1; 7207 mutex_exit(&un->un_pm_mutex); 7208 un->un_power_level = SD_SPINDLE_OFF; 7209 } 7210 7211 mutex_exit(SD_MUTEX(un)); 7212 7213 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 7214 7215 return (DDI_SUCCESS); 7216 } 7217 7218 7219 /* 7220 * Function: sd_ddi_resume 7221 * 7222 * Description: Performs system power-up operations.. 7223 * 7224 * Return Code: DDI_SUCCESS 7225 * DDI_FAILURE 7226 * 7227 * Context: Kernel thread context 7228 */ 7229 7230 static int 7231 sd_ddi_resume(dev_info_t *devi) 7232 { 7233 struct sd_lun *un; 7234 7235 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 7236 if (un == NULL) { 7237 return (DDI_FAILURE); 7238 } 7239 7240 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 7241 7242 mutex_enter(SD_MUTEX(un)); 7243 Restore_state(un); 7244 7245 /* 7246 * Restore the state which was saved to give the 7247 * the right state in un_last_state 7248 */ 7249 un->un_last_state = un->un_save_state; 7250 /* 7251 * Note: throttle comes back at full. 7252 * Also note: this MUST be done before calling pm_raise_power 7253 * otherwise the system can get hung in biowait. The scenario where 7254 * this'll happen is under cpr suspend. Writing of the system 7255 * state goes through sddump, which writes 0 to un_throttle. If 7256 * writing the system state then fails, example if the partition is 7257 * too small, then cpr attempts a resume. If throttle isn't restored 7258 * from the saved value until after calling pm_raise_power then 7259 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7260 * in biowait. 7261 */ 7262 un->un_throttle = un->un_saved_throttle; 7263 7264 /* 7265 * The chance of failure is very rare as the only command done in power 7266 * entry point is START command when you transition from 0->1 or 7267 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7268 * which suspend was done. Ignore the return value as the resume should 7269 * not be failed. In the case of removable media the media need not be 7270 * inserted and hence there is a chance that raise power will fail with 7271 * media not present. 7272 */ 7273 if (un->un_f_attach_spinup) { 7274 mutex_exit(SD_MUTEX(un)); 7275 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7276 mutex_enter(SD_MUTEX(un)); 7277 } 7278 7279 /* 7280 * Don't broadcast to the suspend cv and therefore possibly 7281 * start I/O until after power has been restored. 7282 */ 7283 cv_broadcast(&un->un_suspend_cv); 7284 cv_broadcast(&un->un_state_cv); 7285 7286 /* restart thread */ 7287 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7288 scsi_watch_resume(un->un_swr_token); 7289 } 7290 7291 #if (defined(__fibre)) 7292 if (un->un_f_is_fibre == TRUE) { 7293 /* 7294 * Add callbacks for insert and remove events 7295 */ 7296 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7297 sd_init_event_callbacks(un); 7298 } 7299 } 7300 #endif 7301 7302 /* 7303 * Transport any pending commands to the target. 7304 * 7305 * If this is a low-activity device commands in queue will have to wait 7306 * until new commands come in, which may take awhile. Also, we 7307 * specifically don't check un_ncmds_in_transport because we know that 7308 * there really are no commands in progress after the unit was 7309 * suspended and we could have reached the throttle level, been 7310 * suspended, and have no new commands coming in for awhile. Highly 7311 * unlikely, but so is the low-activity disk scenario. 7312 */ 7313 ddi_xbuf_dispatch(un->un_xbuf_attr); 7314 7315 sd_start_cmds(un, NULL); 7316 mutex_exit(SD_MUTEX(un)); 7317 7318 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7319 7320 return (DDI_SUCCESS); 7321 } 7322 7323 7324 /* 7325 * Function: sd_ddi_pm_resume 7326 * 7327 * Description: Set the drive state to powered on. 7328 * Someone else is required to actually change the drive 7329 * power level. 7330 * 7331 * Arguments: un - driver soft state (unit) structure 7332 * 7333 * Return Code: DDI_SUCCESS 7334 * 7335 * Context: Kernel thread context 7336 */ 7337 7338 static int 7339 sd_ddi_pm_resume(struct sd_lun *un) 7340 { 7341 ASSERT(un != NULL); 7342 7343 ASSERT(!mutex_owned(SD_MUTEX(un))); 7344 mutex_enter(SD_MUTEX(un)); 7345 un->un_power_level = SD_SPINDLE_ON; 7346 7347 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7348 mutex_enter(&un->un_pm_mutex); 7349 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7350 un->un_pm_count++; 7351 ASSERT(un->un_pm_count == 0); 7352 /* 7353 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7354 * un_suspend_cv is for a system resume, not a power management 7355 * device resume. (4297749) 7356 * cv_broadcast(&un->un_suspend_cv); 7357 */ 7358 } 7359 mutex_exit(&un->un_pm_mutex); 7360 mutex_exit(SD_MUTEX(un)); 7361 7362 return (DDI_SUCCESS); 7363 } 7364 7365 7366 /* 7367 * Function: sd_pm_idletimeout_handler 7368 * 7369 * Description: A timer routine that's active only while a device is busy. 7370 * The purpose is to extend slightly the pm framework's busy 7371 * view of the device to prevent busy/idle thrashing for 7372 * back-to-back commands. Do this by comparing the current time 7373 * to the time at which the last command completed and when the 7374 * difference is greater than sd_pm_idletime, call 7375 * pm_idle_component. In addition to indicating idle to the pm 7376 * framework, update the chain type to again use the internal pm 7377 * layers of the driver. 7378 * 7379 * Arguments: arg - driver soft state (unit) structure 7380 * 7381 * Context: Executes in a timeout(9F) thread context 7382 */ 7383 7384 static void 7385 sd_pm_idletimeout_handler(void *arg) 7386 { 7387 struct sd_lun *un = arg; 7388 7389 time_t now; 7390 7391 mutex_enter(&sd_detach_mutex); 7392 if (un->un_detach_count != 0) { 7393 /* Abort if the instance is detaching */ 7394 mutex_exit(&sd_detach_mutex); 7395 return; 7396 } 7397 mutex_exit(&sd_detach_mutex); 7398 7399 now = ddi_get_time(); 7400 /* 7401 * Grab both mutexes, in the proper order, since we're accessing 7402 * both PM and softstate variables. 7403 */ 7404 mutex_enter(SD_MUTEX(un)); 7405 mutex_enter(&un->un_pm_mutex); 7406 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7407 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7408 /* 7409 * Update the chain types. 7410 * This takes affect on the next new command received. 7411 */ 7412 if (un->un_f_non_devbsize_supported) { 7413 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7414 } else { 7415 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7416 } 7417 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7418 7419 SD_TRACE(SD_LOG_IO_PM, un, 7420 "sd_pm_idletimeout_handler: idling device\n"); 7421 (void) pm_idle_component(SD_DEVINFO(un), 0); 7422 un->un_pm_idle_timeid = NULL; 7423 } else { 7424 un->un_pm_idle_timeid = 7425 timeout(sd_pm_idletimeout_handler, un, 7426 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7427 } 7428 mutex_exit(&un->un_pm_mutex); 7429 mutex_exit(SD_MUTEX(un)); 7430 } 7431 7432 7433 /* 7434 * Function: sd_pm_timeout_handler 7435 * 7436 * Description: Callback to tell framework we are idle. 7437 * 7438 * Context: timeout(9f) thread context. 7439 */ 7440 7441 static void 7442 sd_pm_timeout_handler(void *arg) 7443 { 7444 struct sd_lun *un = arg; 7445 7446 (void) pm_idle_component(SD_DEVINFO(un), 0); 7447 mutex_enter(&un->un_pm_mutex); 7448 un->un_pm_timeid = NULL; 7449 mutex_exit(&un->un_pm_mutex); 7450 } 7451 7452 7453 /* 7454 * Function: sdpower 7455 * 7456 * Description: PM entry point. 7457 * 7458 * Return Code: DDI_SUCCESS 7459 * DDI_FAILURE 7460 * 7461 * Context: Kernel thread context 7462 */ 7463 7464 static int 7465 sdpower(dev_info_t *devi, int component, int level) 7466 { 7467 struct sd_lun *un; 7468 int instance; 7469 int rval = DDI_SUCCESS; 7470 uint_t i, log_page_size, maxcycles, ncycles; 7471 uchar_t *log_page_data; 7472 int log_sense_page; 7473 int medium_present; 7474 time_t intvlp; 7475 dev_t dev; 7476 struct pm_trans_data sd_pm_tran_data; 7477 uchar_t save_state; 7478 int sval; 7479 uchar_t state_before_pm; 7480 int got_semaphore_here; 7481 7482 instance = ddi_get_instance(devi); 7483 7484 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7485 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7486 component != 0) { 7487 return (DDI_FAILURE); 7488 } 7489 7490 dev = sd_make_device(SD_DEVINFO(un)); 7491 7492 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7493 7494 /* 7495 * Must synchronize power down with close. 7496 * Attempt to decrement/acquire the open/close semaphore, 7497 * but do NOT wait on it. If it's not greater than zero, 7498 * ie. it can't be decremented without waiting, then 7499 * someone else, either open or close, already has it 7500 * and the try returns 0. Use that knowledge here to determine 7501 * if it's OK to change the device power level. 7502 * Also, only increment it on exit if it was decremented, ie. gotten, 7503 * here. 7504 */ 7505 got_semaphore_here = sema_tryp(&un->un_semoclose); 7506 7507 mutex_enter(SD_MUTEX(un)); 7508 7509 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7510 un->un_ncmds_in_driver); 7511 7512 /* 7513 * If un_ncmds_in_driver is non-zero it indicates commands are 7514 * already being processed in the driver, or if the semaphore was 7515 * not gotten here it indicates an open or close is being processed. 7516 * At the same time somebody is requesting to go low power which 7517 * can't happen, therefore we need to return failure. 7518 */ 7519 if ((level == SD_SPINDLE_OFF) && 7520 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7521 mutex_exit(SD_MUTEX(un)); 7522 7523 if (got_semaphore_here != 0) { 7524 sema_v(&un->un_semoclose); 7525 } 7526 SD_TRACE(SD_LOG_IO_PM, un, 7527 "sdpower: exit, device has queued cmds.\n"); 7528 return (DDI_FAILURE); 7529 } 7530 7531 /* 7532 * if it is OFFLINE that means the disk is completely dead 7533 * in our case we have to put the disk in on or off by sending commands 7534 * Of course that will fail anyway so return back here. 7535 * 7536 * Power changes to a device that's OFFLINE or SUSPENDED 7537 * are not allowed. 7538 */ 7539 if ((un->un_state == SD_STATE_OFFLINE) || 7540 (un->un_state == SD_STATE_SUSPENDED)) { 7541 mutex_exit(SD_MUTEX(un)); 7542 7543 if (got_semaphore_here != 0) { 7544 sema_v(&un->un_semoclose); 7545 } 7546 SD_TRACE(SD_LOG_IO_PM, un, 7547 "sdpower: exit, device is off-line.\n"); 7548 return (DDI_FAILURE); 7549 } 7550 7551 /* 7552 * Change the device's state to indicate it's power level 7553 * is being changed. Do this to prevent a power off in the 7554 * middle of commands, which is especially bad on devices 7555 * that are really powered off instead of just spun down. 7556 */ 7557 state_before_pm = un->un_state; 7558 un->un_state = SD_STATE_PM_CHANGING; 7559 7560 mutex_exit(SD_MUTEX(un)); 7561 7562 /* 7563 * If "pm-capable" property is set to TRUE by HBA drivers, 7564 * bypass the following checking, otherwise, check the log 7565 * sense information for this device 7566 */ 7567 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7568 /* 7569 * Get the log sense information to understand whether the 7570 * the powercycle counts have gone beyond the threshhold. 7571 */ 7572 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7573 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7574 7575 mutex_enter(SD_MUTEX(un)); 7576 log_sense_page = un->un_start_stop_cycle_page; 7577 mutex_exit(SD_MUTEX(un)); 7578 7579 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7580 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7581 #ifdef SDDEBUG 7582 if (sd_force_pm_supported) { 7583 /* Force a successful result */ 7584 rval = 0; 7585 } 7586 #endif 7587 if (rval != 0) { 7588 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7589 "Log Sense Failed\n"); 7590 kmem_free(log_page_data, log_page_size); 7591 /* Cannot support power management on those drives */ 7592 7593 if (got_semaphore_here != 0) { 7594 sema_v(&un->un_semoclose); 7595 } 7596 /* 7597 * On exit put the state back to it's original value 7598 * and broadcast to anyone waiting for the power 7599 * change completion. 7600 */ 7601 mutex_enter(SD_MUTEX(un)); 7602 un->un_state = state_before_pm; 7603 cv_broadcast(&un->un_suspend_cv); 7604 mutex_exit(SD_MUTEX(un)); 7605 SD_TRACE(SD_LOG_IO_PM, un, 7606 "sdpower: exit, Log Sense Failed.\n"); 7607 return (DDI_FAILURE); 7608 } 7609 7610 /* 7611 * From the page data - Convert the essential information to 7612 * pm_trans_data 7613 */ 7614 maxcycles = 7615 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7616 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7617 7618 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7619 7620 ncycles = 7621 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7622 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7623 7624 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7625 7626 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7627 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7628 log_page_data[8+i]; 7629 } 7630 7631 kmem_free(log_page_data, log_page_size); 7632 7633 /* 7634 * Call pm_trans_check routine to get the Ok from 7635 * the global policy 7636 */ 7637 7638 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7639 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7640 7641 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7642 #ifdef SDDEBUG 7643 if (sd_force_pm_supported) { 7644 /* Force a successful result */ 7645 rval = 1; 7646 } 7647 #endif 7648 switch (rval) { 7649 case 0: 7650 /* 7651 * Not Ok to Power cycle or error in parameters passed 7652 * Would have given the advised time to consider power 7653 * cycle. Based on the new intvlp parameter we are 7654 * supposed to pretend we are busy so that pm framework 7655 * will never call our power entry point. Because of 7656 * that install a timeout handler and wait for the 7657 * recommended time to elapse so that power management 7658 * can be effective again. 7659 * 7660 * To effect this behavior, call pm_busy_component to 7661 * indicate to the framework this device is busy. 7662 * By not adjusting un_pm_count the rest of PM in 7663 * the driver will function normally, and independant 7664 * of this but because the framework is told the device 7665 * is busy it won't attempt powering down until it gets 7666 * a matching idle. The timeout handler sends this. 7667 * Note: sd_pm_entry can't be called here to do this 7668 * because sdpower may have been called as a result 7669 * of a call to pm_raise_power from within sd_pm_entry. 7670 * 7671 * If a timeout handler is already active then 7672 * don't install another. 7673 */ 7674 mutex_enter(&un->un_pm_mutex); 7675 if (un->un_pm_timeid == NULL) { 7676 un->un_pm_timeid = 7677 timeout(sd_pm_timeout_handler, 7678 un, intvlp * drv_usectohz(1000000)); 7679 mutex_exit(&un->un_pm_mutex); 7680 (void) pm_busy_component(SD_DEVINFO(un), 0); 7681 } else { 7682 mutex_exit(&un->un_pm_mutex); 7683 } 7684 if (got_semaphore_here != 0) { 7685 sema_v(&un->un_semoclose); 7686 } 7687 /* 7688 * On exit put the state back to it's original value 7689 * and broadcast to anyone waiting for the power 7690 * change completion. 7691 */ 7692 mutex_enter(SD_MUTEX(un)); 7693 un->un_state = state_before_pm; 7694 cv_broadcast(&un->un_suspend_cv); 7695 mutex_exit(SD_MUTEX(un)); 7696 7697 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7698 "trans check Failed, not ok to power cycle.\n"); 7699 return (DDI_FAILURE); 7700 7701 case -1: 7702 if (got_semaphore_here != 0) { 7703 sema_v(&un->un_semoclose); 7704 } 7705 /* 7706 * On exit put the state back to it's original value 7707 * and broadcast to anyone waiting for the power 7708 * change completion. 7709 */ 7710 mutex_enter(SD_MUTEX(un)); 7711 un->un_state = state_before_pm; 7712 cv_broadcast(&un->un_suspend_cv); 7713 mutex_exit(SD_MUTEX(un)); 7714 SD_TRACE(SD_LOG_IO_PM, un, 7715 "sdpower: exit, trans check command Failed.\n"); 7716 return (DDI_FAILURE); 7717 } 7718 } 7719 7720 if (level == SD_SPINDLE_OFF) { 7721 /* 7722 * Save the last state... if the STOP FAILS we need it 7723 * for restoring 7724 */ 7725 mutex_enter(SD_MUTEX(un)); 7726 save_state = un->un_last_state; 7727 /* 7728 * There must not be any cmds. getting processed 7729 * in the driver when we get here. Power to the 7730 * device is potentially going off. 7731 */ 7732 ASSERT(un->un_ncmds_in_driver == 0); 7733 mutex_exit(SD_MUTEX(un)); 7734 7735 /* 7736 * For now suspend the device completely before spindle is 7737 * turned off 7738 */ 7739 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7740 if (got_semaphore_here != 0) { 7741 sema_v(&un->un_semoclose); 7742 } 7743 /* 7744 * On exit put the state back to it's original value 7745 * and broadcast to anyone waiting for the power 7746 * change completion. 7747 */ 7748 mutex_enter(SD_MUTEX(un)); 7749 un->un_state = state_before_pm; 7750 cv_broadcast(&un->un_suspend_cv); 7751 mutex_exit(SD_MUTEX(un)); 7752 SD_TRACE(SD_LOG_IO_PM, un, 7753 "sdpower: exit, PM suspend Failed.\n"); 7754 return (DDI_FAILURE); 7755 } 7756 } 7757 7758 /* 7759 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7760 * close, or strategy. Dump no long uses this routine, it uses it's 7761 * own code so it can be done in polled mode. 7762 */ 7763 7764 medium_present = TRUE; 7765 7766 /* 7767 * When powering up, issue a TUR in case the device is at unit 7768 * attention. Don't do retries. Bypass the PM layer, otherwise 7769 * a deadlock on un_pm_busy_cv will occur. 7770 */ 7771 if (level == SD_SPINDLE_ON) { 7772 (void) sd_send_scsi_TEST_UNIT_READY(un, 7773 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7774 } 7775 7776 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7777 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7778 7779 sval = sd_send_scsi_START_STOP_UNIT(un, 7780 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7781 SD_PATH_DIRECT); 7782 /* Command failed, check for media present. */ 7783 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7784 medium_present = FALSE; 7785 } 7786 7787 /* 7788 * The conditions of interest here are: 7789 * if a spindle off with media present fails, 7790 * then restore the state and return an error. 7791 * else if a spindle on fails, 7792 * then return an error (there's no state to restore). 7793 * In all other cases we setup for the new state 7794 * and return success. 7795 */ 7796 switch (level) { 7797 case SD_SPINDLE_OFF: 7798 if ((medium_present == TRUE) && (sval != 0)) { 7799 /* The stop command from above failed */ 7800 rval = DDI_FAILURE; 7801 /* 7802 * The stop command failed, and we have media 7803 * present. Put the level back by calling the 7804 * sd_pm_resume() and set the state back to 7805 * it's previous value. 7806 */ 7807 (void) sd_ddi_pm_resume(un); 7808 mutex_enter(SD_MUTEX(un)); 7809 un->un_last_state = save_state; 7810 mutex_exit(SD_MUTEX(un)); 7811 break; 7812 } 7813 /* 7814 * The stop command from above succeeded. 7815 */ 7816 if (un->un_f_monitor_media_state) { 7817 /* 7818 * Terminate watch thread in case of removable media 7819 * devices going into low power state. This is as per 7820 * the requirements of pm framework, otherwise commands 7821 * will be generated for the device (through watch 7822 * thread), even when the device is in low power state. 7823 */ 7824 mutex_enter(SD_MUTEX(un)); 7825 un->un_f_watcht_stopped = FALSE; 7826 if (un->un_swr_token != NULL) { 7827 opaque_t temp_token = un->un_swr_token; 7828 un->un_f_watcht_stopped = TRUE; 7829 un->un_swr_token = NULL; 7830 mutex_exit(SD_MUTEX(un)); 7831 (void) scsi_watch_request_terminate(temp_token, 7832 SCSI_WATCH_TERMINATE_WAIT); 7833 } else { 7834 mutex_exit(SD_MUTEX(un)); 7835 } 7836 } 7837 break; 7838 7839 default: /* The level requested is spindle on... */ 7840 /* 7841 * Legacy behavior: return success on a failed spinup 7842 * if there is no media in the drive. 7843 * Do this by looking at medium_present here. 7844 */ 7845 if ((sval != 0) && medium_present) { 7846 /* The start command from above failed */ 7847 rval = DDI_FAILURE; 7848 break; 7849 } 7850 /* 7851 * The start command from above succeeded 7852 * Resume the devices now that we have 7853 * started the disks 7854 */ 7855 (void) sd_ddi_pm_resume(un); 7856 7857 /* 7858 * Resume the watch thread since it was suspended 7859 * when the device went into low power mode. 7860 */ 7861 if (un->un_f_monitor_media_state) { 7862 mutex_enter(SD_MUTEX(un)); 7863 if (un->un_f_watcht_stopped == TRUE) { 7864 opaque_t temp_token; 7865 7866 un->un_f_watcht_stopped = FALSE; 7867 mutex_exit(SD_MUTEX(un)); 7868 temp_token = scsi_watch_request_submit( 7869 SD_SCSI_DEVP(un), 7870 sd_check_media_time, 7871 SENSE_LENGTH, sd_media_watch_cb, 7872 (caddr_t)dev); 7873 mutex_enter(SD_MUTEX(un)); 7874 un->un_swr_token = temp_token; 7875 } 7876 mutex_exit(SD_MUTEX(un)); 7877 } 7878 } 7879 if (got_semaphore_here != 0) { 7880 sema_v(&un->un_semoclose); 7881 } 7882 /* 7883 * On exit put the state back to it's original value 7884 * and broadcast to anyone waiting for the power 7885 * change completion. 7886 */ 7887 mutex_enter(SD_MUTEX(un)); 7888 un->un_state = state_before_pm; 7889 cv_broadcast(&un->un_suspend_cv); 7890 mutex_exit(SD_MUTEX(un)); 7891 7892 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7893 7894 return (rval); 7895 } 7896 7897 7898 7899 /* 7900 * Function: sdattach 7901 * 7902 * Description: Driver's attach(9e) entry point function. 7903 * 7904 * Arguments: devi - opaque device info handle 7905 * cmd - attach type 7906 * 7907 * Return Code: DDI_SUCCESS 7908 * DDI_FAILURE 7909 * 7910 * Context: Kernel thread context 7911 */ 7912 7913 static int 7914 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7915 { 7916 switch (cmd) { 7917 case DDI_ATTACH: 7918 return (sd_unit_attach(devi)); 7919 case DDI_RESUME: 7920 return (sd_ddi_resume(devi)); 7921 default: 7922 break; 7923 } 7924 return (DDI_FAILURE); 7925 } 7926 7927 7928 /* 7929 * Function: sddetach 7930 * 7931 * Description: Driver's detach(9E) entry point function. 7932 * 7933 * Arguments: devi - opaque device info handle 7934 * cmd - detach type 7935 * 7936 * Return Code: DDI_SUCCESS 7937 * DDI_FAILURE 7938 * 7939 * Context: Kernel thread context 7940 */ 7941 7942 static int 7943 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7944 { 7945 switch (cmd) { 7946 case DDI_DETACH: 7947 return (sd_unit_detach(devi)); 7948 case DDI_SUSPEND: 7949 return (sd_ddi_suspend(devi)); 7950 default: 7951 break; 7952 } 7953 return (DDI_FAILURE); 7954 } 7955 7956 7957 /* 7958 * Function: sd_sync_with_callback 7959 * 7960 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7961 * state while the callback routine is active. 7962 * 7963 * Arguments: un: softstate structure for the instance 7964 * 7965 * Context: Kernel thread context 7966 */ 7967 7968 static void 7969 sd_sync_with_callback(struct sd_lun *un) 7970 { 7971 ASSERT(un != NULL); 7972 7973 mutex_enter(SD_MUTEX(un)); 7974 7975 ASSERT(un->un_in_callback >= 0); 7976 7977 while (un->un_in_callback > 0) { 7978 mutex_exit(SD_MUTEX(un)); 7979 delay(2); 7980 mutex_enter(SD_MUTEX(un)); 7981 } 7982 7983 mutex_exit(SD_MUTEX(un)); 7984 } 7985 7986 /* 7987 * Function: sd_unit_attach 7988 * 7989 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7990 * the soft state structure for the device and performs 7991 * all necessary structure and device initializations. 7992 * 7993 * Arguments: devi: the system's dev_info_t for the device. 7994 * 7995 * Return Code: DDI_SUCCESS if attach is successful. 7996 * DDI_FAILURE if any part of the attach fails. 7997 * 7998 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7999 * Kernel thread context only. Can sleep. 8000 */ 8001 8002 static int 8003 sd_unit_attach(dev_info_t *devi) 8004 { 8005 struct scsi_device *devp; 8006 struct sd_lun *un; 8007 char *variantp; 8008 int reservation_flag = SD_TARGET_IS_UNRESERVED; 8009 int instance; 8010 int rval; 8011 int wc_enabled; 8012 int tgt; 8013 uint64_t capacity; 8014 uint_t lbasize; 8015 dev_info_t *pdip = ddi_get_parent(devi); 8016 8017 /* 8018 * Retrieve the target driver's private data area. This was set 8019 * up by the HBA. 8020 */ 8021 devp = ddi_get_driver_private(devi); 8022 8023 /* 8024 * Retrieve the target ID of the device. 8025 */ 8026 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8027 SCSI_ADDR_PROP_TARGET, -1); 8028 8029 /* 8030 * Since we have no idea what state things were left in by the last 8031 * user of the device, set up some 'default' settings, ie. turn 'em 8032 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 8033 * Do this before the scsi_probe, which sends an inquiry. 8034 * This is a fix for bug (4430280). 8035 * Of special importance is wide-xfer. The drive could have been left 8036 * in wide transfer mode by the last driver to communicate with it, 8037 * this includes us. If that's the case, and if the following is not 8038 * setup properly or we don't re-negotiate with the drive prior to 8039 * transferring data to/from the drive, it causes bus parity errors, 8040 * data overruns, and unexpected interrupts. This first occurred when 8041 * the fix for bug (4378686) was made. 8042 */ 8043 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 8044 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 8045 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 8046 8047 /* 8048 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 8049 * on a target. Setting it per lun instance actually sets the 8050 * capability of this target, which affects those luns already 8051 * attached on the same target. So during attach, we can only disable 8052 * this capability only when no other lun has been attached on this 8053 * target. By doing this, we assume a target has the same tagged-qing 8054 * capability for every lun. The condition can be removed when HBA 8055 * is changed to support per lun based tagged-qing capability. 8056 */ 8057 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8058 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 8059 } 8060 8061 /* 8062 * Use scsi_probe() to issue an INQUIRY command to the device. 8063 * This call will allocate and fill in the scsi_inquiry structure 8064 * and point the sd_inq member of the scsi_device structure to it. 8065 * If the attach succeeds, then this memory will not be de-allocated 8066 * (via scsi_unprobe()) until the instance is detached. 8067 */ 8068 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 8069 goto probe_failed; 8070 } 8071 8072 /* 8073 * Check the device type as specified in the inquiry data and 8074 * claim it if it is of a type that we support. 8075 */ 8076 switch (devp->sd_inq->inq_dtype) { 8077 case DTYPE_DIRECT: 8078 break; 8079 case DTYPE_RODIRECT: 8080 break; 8081 case DTYPE_OPTICAL: 8082 break; 8083 case DTYPE_NOTPRESENT: 8084 default: 8085 /* Unsupported device type; fail the attach. */ 8086 goto probe_failed; 8087 } 8088 8089 /* 8090 * Allocate the soft state structure for this unit. 8091 * 8092 * We rely upon this memory being set to all zeroes by 8093 * ddi_soft_state_zalloc(). We assume that any member of the 8094 * soft state structure that is not explicitly initialized by 8095 * this routine will have a value of zero. 8096 */ 8097 instance = ddi_get_instance(devp->sd_dev); 8098 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 8099 goto probe_failed; 8100 } 8101 8102 /* 8103 * Retrieve a pointer to the newly-allocated soft state. 8104 * 8105 * This should NEVER fail if the ddi_soft_state_zalloc() call above 8106 * was successful, unless something has gone horribly wrong and the 8107 * ddi's soft state internals are corrupt (in which case it is 8108 * probably better to halt here than just fail the attach....) 8109 */ 8110 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 8111 panic("sd_unit_attach: NULL soft state on instance:0x%x", 8112 instance); 8113 /*NOTREACHED*/ 8114 } 8115 8116 /* 8117 * Link the back ptr of the driver soft state to the scsi_device 8118 * struct for this lun. 8119 * Save a pointer to the softstate in the driver-private area of 8120 * the scsi_device struct. 8121 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 8122 * we first set un->un_sd below. 8123 */ 8124 un->un_sd = devp; 8125 devp->sd_private = (opaque_t)un; 8126 8127 /* 8128 * The following must be after devp is stored in the soft state struct. 8129 */ 8130 #ifdef SDDEBUG 8131 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8132 "%s_unit_attach: un:0x%p instance:%d\n", 8133 ddi_driver_name(devi), un, instance); 8134 #endif 8135 8136 /* 8137 * Set up the device type and node type (for the minor nodes). 8138 * By default we assume that the device can at least support the 8139 * Common Command Set. Call it a CD-ROM if it reports itself 8140 * as a RODIRECT device. 8141 */ 8142 switch (devp->sd_inq->inq_dtype) { 8143 case DTYPE_RODIRECT: 8144 un->un_node_type = DDI_NT_CD_CHAN; 8145 un->un_ctype = CTYPE_CDROM; 8146 break; 8147 case DTYPE_OPTICAL: 8148 un->un_node_type = DDI_NT_BLOCK_CHAN; 8149 un->un_ctype = CTYPE_ROD; 8150 break; 8151 default: 8152 un->un_node_type = DDI_NT_BLOCK_CHAN; 8153 un->un_ctype = CTYPE_CCS; 8154 break; 8155 } 8156 8157 /* 8158 * Try to read the interconnect type from the HBA. 8159 * 8160 * Note: This driver is currently compiled as two binaries, a parallel 8161 * scsi version (sd) and a fibre channel version (ssd). All functional 8162 * differences are determined at compile time. In the future a single 8163 * binary will be provided and the inteconnect type will be used to 8164 * differentiate between fibre and parallel scsi behaviors. At that time 8165 * it will be necessary for all fibre channel HBAs to support this 8166 * property. 8167 * 8168 * set un_f_is_fiber to TRUE ( default fiber ) 8169 */ 8170 un->un_f_is_fibre = TRUE; 8171 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 8172 case INTERCONNECT_SSA: 8173 un->un_interconnect_type = SD_INTERCONNECT_SSA; 8174 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8175 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 8176 break; 8177 case INTERCONNECT_PARALLEL: 8178 un->un_f_is_fibre = FALSE; 8179 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8180 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8181 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 8182 break; 8183 case INTERCONNECT_SATA: 8184 un->un_f_is_fibre = FALSE; 8185 un->un_interconnect_type = SD_INTERCONNECT_SATA; 8186 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8187 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 8188 break; 8189 case INTERCONNECT_FIBRE: 8190 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 8191 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8192 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 8193 break; 8194 case INTERCONNECT_FABRIC: 8195 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 8196 un->un_node_type = DDI_NT_BLOCK_FABRIC; 8197 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8198 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 8199 break; 8200 default: 8201 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 8202 /* 8203 * The HBA does not support the "interconnect-type" property 8204 * (or did not provide a recognized type). 8205 * 8206 * Note: This will be obsoleted when a single fibre channel 8207 * and parallel scsi driver is delivered. In the meantime the 8208 * interconnect type will be set to the platform default.If that 8209 * type is not parallel SCSI, it means that we should be 8210 * assuming "ssd" semantics. However, here this also means that 8211 * the FC HBA is not supporting the "interconnect-type" property 8212 * like we expect it to, so log this occurrence. 8213 */ 8214 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 8215 if (!SD_IS_PARALLEL_SCSI(un)) { 8216 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8217 "sd_unit_attach: un:0x%p Assuming " 8218 "INTERCONNECT_FIBRE\n", un); 8219 } else { 8220 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8221 "sd_unit_attach: un:0x%p Assuming " 8222 "INTERCONNECT_PARALLEL\n", un); 8223 un->un_f_is_fibre = FALSE; 8224 } 8225 #else 8226 /* 8227 * Note: This source will be implemented when a single fibre 8228 * channel and parallel scsi driver is delivered. The default 8229 * will be to assume that if a device does not support the 8230 * "interconnect-type" property it is a parallel SCSI HBA and 8231 * we will set the interconnect type for parallel scsi. 8232 */ 8233 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8234 un->un_f_is_fibre = FALSE; 8235 #endif 8236 break; 8237 } 8238 8239 if (un->un_f_is_fibre == TRUE) { 8240 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 8241 SCSI_VERSION_3) { 8242 switch (un->un_interconnect_type) { 8243 case SD_INTERCONNECT_FIBRE: 8244 case SD_INTERCONNECT_SSA: 8245 un->un_node_type = DDI_NT_BLOCK_WWN; 8246 break; 8247 default: 8248 break; 8249 } 8250 } 8251 } 8252 8253 /* 8254 * Initialize the Request Sense command for the target 8255 */ 8256 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 8257 goto alloc_rqs_failed; 8258 } 8259 8260 /* 8261 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 8262 * with seperate binary for sd and ssd. 8263 * 8264 * x86 has 1 binary, un_retry_count is set base on connection type. 8265 * The hardcoded values will go away when Sparc uses 1 binary 8266 * for sd and ssd. This hardcoded values need to match 8267 * SD_RETRY_COUNT in sddef.h 8268 * The value used is base on interconnect type. 8269 * fibre = 3, parallel = 5 8270 */ 8271 #if defined(__i386) || defined(__amd64) 8272 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8273 #else 8274 un->un_retry_count = SD_RETRY_COUNT; 8275 #endif 8276 8277 /* 8278 * Set the per disk retry count to the default number of retries 8279 * for disks and CDROMs. This value can be overridden by the 8280 * disk property list or an entry in sd.conf. 8281 */ 8282 un->un_notready_retry_count = 8283 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8284 : DISK_NOT_READY_RETRY_COUNT(un); 8285 8286 /* 8287 * Set the busy retry count to the default value of un_retry_count. 8288 * This can be overridden by entries in sd.conf or the device 8289 * config table. 8290 */ 8291 un->un_busy_retry_count = un->un_retry_count; 8292 8293 /* 8294 * Init the reset threshold for retries. This number determines 8295 * how many retries must be performed before a reset can be issued 8296 * (for certain error conditions). This can be overridden by entries 8297 * in sd.conf or the device config table. 8298 */ 8299 un->un_reset_retry_count = (un->un_retry_count / 2); 8300 8301 /* 8302 * Set the victim_retry_count to the default un_retry_count 8303 */ 8304 un->un_victim_retry_count = (2 * un->un_retry_count); 8305 8306 /* 8307 * Set the reservation release timeout to the default value of 8308 * 5 seconds. This can be overridden by entries in ssd.conf or the 8309 * device config table. 8310 */ 8311 un->un_reserve_release_time = 5; 8312 8313 /* 8314 * Set up the default maximum transfer size. Note that this may 8315 * get updated later in the attach, when setting up default wide 8316 * operations for disks. 8317 */ 8318 #if defined(__i386) || defined(__amd64) 8319 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8320 #else 8321 un->un_max_xfer_size = (uint_t)maxphys; 8322 #endif 8323 8324 /* 8325 * Get "allow bus device reset" property (defaults to "enabled" if 8326 * the property was not defined). This is to disable bus resets for 8327 * certain kinds of error recovery. Note: In the future when a run-time 8328 * fibre check is available the soft state flag should default to 8329 * enabled. 8330 */ 8331 if (un->un_f_is_fibre == TRUE) { 8332 un->un_f_allow_bus_device_reset = TRUE; 8333 } else { 8334 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8335 "allow-bus-device-reset", 1) != 0) { 8336 un->un_f_allow_bus_device_reset = TRUE; 8337 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8338 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8339 un); 8340 } else { 8341 un->un_f_allow_bus_device_reset = FALSE; 8342 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8343 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8344 un); 8345 } 8346 } 8347 8348 /* 8349 * Check if this is an ATAPI device. ATAPI devices use Group 1 8350 * Read/Write commands and Group 2 Mode Sense/Select commands. 8351 * 8352 * Note: The "obsolete" way of doing this is to check for the "atapi" 8353 * property. The new "variant" property with a value of "atapi" has been 8354 * introduced so that future 'variants' of standard SCSI behavior (like 8355 * atapi) could be specified by the underlying HBA drivers by supplying 8356 * a new value for the "variant" property, instead of having to define a 8357 * new property. 8358 */ 8359 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8360 un->un_f_cfg_is_atapi = TRUE; 8361 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8362 "sd_unit_attach: un:0x%p Atapi device\n", un); 8363 } 8364 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8365 &variantp) == DDI_PROP_SUCCESS) { 8366 if (strcmp(variantp, "atapi") == 0) { 8367 un->un_f_cfg_is_atapi = TRUE; 8368 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8369 "sd_unit_attach: un:0x%p Atapi device\n", un); 8370 } 8371 ddi_prop_free(variantp); 8372 } 8373 8374 un->un_cmd_timeout = SD_IO_TIME; 8375 8376 /* Info on current states, statuses, etc. (Updated frequently) */ 8377 un->un_state = SD_STATE_NORMAL; 8378 un->un_last_state = SD_STATE_NORMAL; 8379 8380 /* Control & status info for command throttling */ 8381 un->un_throttle = sd_max_throttle; 8382 un->un_saved_throttle = sd_max_throttle; 8383 un->un_min_throttle = sd_min_throttle; 8384 8385 if (un->un_f_is_fibre == TRUE) { 8386 un->un_f_use_adaptive_throttle = TRUE; 8387 } else { 8388 un->un_f_use_adaptive_throttle = FALSE; 8389 } 8390 8391 /* Removable media support. */ 8392 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8393 un->un_mediastate = DKIO_NONE; 8394 un->un_specified_mediastate = DKIO_NONE; 8395 8396 /* CVs for suspend/resume (PM or DR) */ 8397 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8398 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8399 8400 /* Power management support. */ 8401 un->un_power_level = SD_SPINDLE_UNINIT; 8402 8403 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8404 un->un_f_wcc_inprog = 0; 8405 8406 /* 8407 * The open/close semaphore is used to serialize threads executing 8408 * in the driver's open & close entry point routines for a given 8409 * instance. 8410 */ 8411 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8412 8413 /* 8414 * The conf file entry and softstate variable is a forceful override, 8415 * meaning a non-zero value must be entered to change the default. 8416 */ 8417 un->un_f_disksort_disabled = FALSE; 8418 8419 /* 8420 * Retrieve the properties from the static driver table or the driver 8421 * configuration file (.conf) for this unit and update the soft state 8422 * for the device as needed for the indicated properties. 8423 * Note: the property configuration needs to occur here as some of the 8424 * following routines may have dependancies on soft state flags set 8425 * as part of the driver property configuration. 8426 */ 8427 sd_read_unit_properties(un); 8428 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8429 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8430 8431 /* 8432 * Only if a device has "hotpluggable" property, it is 8433 * treated as hotpluggable device. Otherwise, it is 8434 * regarded as non-hotpluggable one. 8435 */ 8436 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8437 -1) != -1) { 8438 un->un_f_is_hotpluggable = TRUE; 8439 } 8440 8441 /* 8442 * set unit's attributes(flags) according to "hotpluggable" and 8443 * RMB bit in INQUIRY data. 8444 */ 8445 sd_set_unit_attributes(un, devi); 8446 8447 /* 8448 * By default, we mark the capacity, lbasize, and geometry 8449 * as invalid. Only if we successfully read a valid capacity 8450 * will we update the un_blockcount and un_tgt_blocksize with the 8451 * valid values (the geometry will be validated later). 8452 */ 8453 un->un_f_blockcount_is_valid = FALSE; 8454 un->un_f_tgt_blocksize_is_valid = FALSE; 8455 un->un_f_geometry_is_valid = FALSE; 8456 8457 /* 8458 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8459 * otherwise. 8460 */ 8461 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8462 un->un_blockcount = 0; 8463 8464 /* 8465 * Set up the per-instance info needed to determine the correct 8466 * CDBs and other info for issuing commands to the target. 8467 */ 8468 sd_init_cdb_limits(un); 8469 8470 /* 8471 * Set up the IO chains to use, based upon the target type. 8472 */ 8473 if (un->un_f_non_devbsize_supported) { 8474 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8475 } else { 8476 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8477 } 8478 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8479 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8480 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8481 8482 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8483 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8484 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8485 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8486 8487 8488 if (ISCD(un)) { 8489 un->un_additional_codes = sd_additional_codes; 8490 } else { 8491 un->un_additional_codes = NULL; 8492 } 8493 8494 /* 8495 * Create the kstats here so they can be available for attach-time 8496 * routines that send commands to the unit (either polled or via 8497 * sd_send_scsi_cmd). 8498 * 8499 * Note: This is a critical sequence that needs to be maintained: 8500 * 1) Instantiate the kstats here, before any routines using the 8501 * iopath (i.e. sd_send_scsi_cmd). 8502 * 2) Instantiate and initialize the partition stats 8503 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 8504 * see detailed comments there. 8505 * 3) Initialize the error stats (sd_set_errstats), following 8506 * sd_validate_geometry(),sd_register_devid(), 8507 * and sd_cache_control(). 8508 */ 8509 8510 un->un_stats = kstat_create(sd_label, instance, 8511 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8512 if (un->un_stats != NULL) { 8513 un->un_stats->ks_lock = SD_MUTEX(un); 8514 kstat_install(un->un_stats); 8515 } 8516 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8517 "sd_unit_attach: un:0x%p un_stats created\n", un); 8518 8519 sd_create_errstats(un, instance); 8520 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8521 "sd_unit_attach: un:0x%p errstats created\n", un); 8522 8523 /* 8524 * The following if/else code was relocated here from below as part 8525 * of the fix for bug (4430280). However with the default setup added 8526 * on entry to this routine, it's no longer absolutely necessary for 8527 * this to be before the call to sd_spin_up_unit. 8528 */ 8529 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 8530 /* 8531 * If SCSI-2 tagged queueing is supported by the target 8532 * and by the host adapter then we will enable it. 8533 */ 8534 un->un_tagflags = 0; 8535 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8536 (devp->sd_inq->inq_cmdque) && 8537 (un->un_f_arq_enabled == TRUE)) { 8538 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8539 1, 1) == 1) { 8540 un->un_tagflags = FLAG_STAG; 8541 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8542 "sd_unit_attach: un:0x%p tag queueing " 8543 "enabled\n", un); 8544 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8545 "untagged-qing", 0) == 1) { 8546 un->un_f_opt_queueing = TRUE; 8547 un->un_saved_throttle = un->un_throttle = 8548 min(un->un_throttle, 3); 8549 } else { 8550 un->un_f_opt_queueing = FALSE; 8551 un->un_saved_throttle = un->un_throttle = 1; 8552 } 8553 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8554 == 1) && (un->un_f_arq_enabled == TRUE)) { 8555 /* The Host Adapter supports internal queueing. */ 8556 un->un_f_opt_queueing = TRUE; 8557 un->un_saved_throttle = un->un_throttle = 8558 min(un->un_throttle, 3); 8559 } else { 8560 un->un_f_opt_queueing = FALSE; 8561 un->un_saved_throttle = un->un_throttle = 1; 8562 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8563 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8564 } 8565 8566 /* 8567 * Enable large transfers for SATA/SAS drives 8568 */ 8569 if (SD_IS_SERIAL(un)) { 8570 un->un_max_xfer_size = 8571 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8572 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8573 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8574 "sd_unit_attach: un:0x%p max transfer " 8575 "size=0x%x\n", un, un->un_max_xfer_size); 8576 8577 } 8578 8579 /* Setup or tear down default wide operations for disks */ 8580 8581 /* 8582 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8583 * and "ssd_max_xfer_size" to exist simultaneously on the same 8584 * system and be set to different values. In the future this 8585 * code may need to be updated when the ssd module is 8586 * obsoleted and removed from the system. (4299588) 8587 */ 8588 if (SD_IS_PARALLEL_SCSI(un) && 8589 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 8590 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8591 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8592 1, 1) == 1) { 8593 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8594 "sd_unit_attach: un:0x%p Wide Transfer " 8595 "enabled\n", un); 8596 } 8597 8598 /* 8599 * If tagged queuing has also been enabled, then 8600 * enable large xfers 8601 */ 8602 if (un->un_saved_throttle == sd_max_throttle) { 8603 un->un_max_xfer_size = 8604 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8605 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8606 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8607 "sd_unit_attach: un:0x%p max transfer " 8608 "size=0x%x\n", un, un->un_max_xfer_size); 8609 } 8610 } else { 8611 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8612 0, 1) == 1) { 8613 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8614 "sd_unit_attach: un:0x%p " 8615 "Wide Transfer disabled\n", un); 8616 } 8617 } 8618 } else { 8619 un->un_tagflags = FLAG_STAG; 8620 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8621 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8622 } 8623 8624 /* 8625 * If this target supports LUN reset, try to enable it. 8626 */ 8627 if (un->un_f_lun_reset_enabled) { 8628 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8629 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8630 "un:0x%p lun_reset capability set\n", un); 8631 } else { 8632 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8633 "un:0x%p lun-reset capability not set\n", un); 8634 } 8635 } 8636 8637 /* 8638 * At this point in the attach, we have enough info in the 8639 * soft state to be able to issue commands to the target. 8640 * 8641 * All command paths used below MUST issue their commands as 8642 * SD_PATH_DIRECT. This is important as intermediate layers 8643 * are not all initialized yet (such as PM). 8644 */ 8645 8646 /* 8647 * Send a TEST UNIT READY command to the device. This should clear 8648 * any outstanding UNIT ATTENTION that may be present. 8649 * 8650 * Note: Don't check for success, just track if there is a reservation, 8651 * this is a throw away command to clear any unit attentions. 8652 * 8653 * Note: This MUST be the first command issued to the target during 8654 * attach to ensure power on UNIT ATTENTIONS are cleared. 8655 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8656 * with attempts at spinning up a device with no media. 8657 */ 8658 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8659 reservation_flag = SD_TARGET_IS_RESERVED; 8660 } 8661 8662 /* 8663 * If the device is NOT a removable media device, attempt to spin 8664 * it up (using the START_STOP_UNIT command) and read its capacity 8665 * (using the READ CAPACITY command). Note, however, that either 8666 * of these could fail and in some cases we would continue with 8667 * the attach despite the failure (see below). 8668 */ 8669 if (un->un_f_descr_format_supported) { 8670 switch (sd_spin_up_unit(un)) { 8671 case 0: 8672 /* 8673 * Spin-up was successful; now try to read the 8674 * capacity. If successful then save the results 8675 * and mark the capacity & lbasize as valid. 8676 */ 8677 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8678 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8679 8680 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8681 &lbasize, SD_PATH_DIRECT)) { 8682 case 0: { 8683 if (capacity > DK_MAX_BLOCKS) { 8684 #ifdef _LP64 8685 if (capacity + 1 > 8686 SD_GROUP1_MAX_ADDRESS) { 8687 /* 8688 * Enable descriptor format 8689 * sense data so that we can 8690 * get 64 bit sense data 8691 * fields. 8692 */ 8693 sd_enable_descr_sense(un); 8694 } 8695 #else 8696 /* 32-bit kernels can't handle this */ 8697 scsi_log(SD_DEVINFO(un), 8698 sd_label, CE_WARN, 8699 "disk has %llu blocks, which " 8700 "is too large for a 32-bit " 8701 "kernel", capacity); 8702 8703 #if defined(__i386) || defined(__amd64) 8704 /* 8705 * Refer to comments related to off-by-1 8706 * at the header of this file. 8707 * 1TB disk was treated as (1T - 512)B 8708 * in the past, so that it might has 8709 * valid VTOC and solaris partitions, 8710 * we have to allow it to continue to 8711 * work. 8712 */ 8713 if (capacity -1 > DK_MAX_BLOCKS) 8714 #endif 8715 goto spinup_failed; 8716 #endif 8717 } 8718 8719 /* 8720 * Here it's not necessary to check the case: 8721 * the capacity of the device is bigger than 8722 * what the max hba cdb can support. Because 8723 * sd_send_scsi_READ_CAPACITY will retrieve 8724 * the capacity by sending USCSI command, which 8725 * is constrained by the max hba cdb. Actually, 8726 * sd_send_scsi_READ_CAPACITY will return 8727 * EINVAL when using bigger cdb than required 8728 * cdb length. Will handle this case in 8729 * "case EINVAL". 8730 */ 8731 8732 /* 8733 * The following relies on 8734 * sd_send_scsi_READ_CAPACITY never 8735 * returning 0 for capacity and/or lbasize. 8736 */ 8737 sd_update_block_info(un, lbasize, capacity); 8738 8739 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8740 "sd_unit_attach: un:0x%p capacity = %ld " 8741 "blocks; lbasize= %ld.\n", un, 8742 un->un_blockcount, un->un_tgt_blocksize); 8743 8744 break; 8745 } 8746 case EINVAL: 8747 /* 8748 * In the case where the max-cdb-length property 8749 * is smaller than the required CDB length for 8750 * a SCSI device, a target driver can fail to 8751 * attach to that device. 8752 */ 8753 scsi_log(SD_DEVINFO(un), 8754 sd_label, CE_WARN, 8755 "disk capacity is too large " 8756 "for current cdb length"); 8757 goto spinup_failed; 8758 case EACCES: 8759 /* 8760 * Should never get here if the spin-up 8761 * succeeded, but code it in anyway. 8762 * From here, just continue with the attach... 8763 */ 8764 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8765 "sd_unit_attach: un:0x%p " 8766 "sd_send_scsi_READ_CAPACITY " 8767 "returned reservation conflict\n", un); 8768 reservation_flag = SD_TARGET_IS_RESERVED; 8769 break; 8770 default: 8771 /* 8772 * Likewise, should never get here if the 8773 * spin-up succeeded. Just continue with 8774 * the attach... 8775 */ 8776 break; 8777 } 8778 break; 8779 case EACCES: 8780 /* 8781 * Device is reserved by another host. In this case 8782 * we could not spin it up or read the capacity, but 8783 * we continue with the attach anyway. 8784 */ 8785 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8786 "sd_unit_attach: un:0x%p spin-up reservation " 8787 "conflict.\n", un); 8788 reservation_flag = SD_TARGET_IS_RESERVED; 8789 break; 8790 default: 8791 /* Fail the attach if the spin-up failed. */ 8792 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8793 "sd_unit_attach: un:0x%p spin-up failed.", un); 8794 goto spinup_failed; 8795 } 8796 } 8797 8798 /* 8799 * Check to see if this is a MMC drive 8800 */ 8801 if (ISCD(un)) { 8802 sd_set_mmc_caps(un); 8803 } 8804 8805 /* 8806 * Create the minor nodes for the device. 8807 * Note: If we want to support fdisk on both sparc and intel, this will 8808 * have to separate out the notion that VTOC8 is always sparc, and 8809 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8810 * type will have to be determined at run-time, and the fdisk 8811 * partitioning will have to have been read & set up before we 8812 * create the minor nodes. (any other inits (such as kstats) that 8813 * also ought to be done before creating the minor nodes?) (Doesn't 8814 * setting up the minor nodes kind of imply that we're ready to 8815 * handle an open from userland?) 8816 */ 8817 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8818 goto create_minor_nodes_failed; 8819 } 8820 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8821 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8822 8823 /* 8824 * Add a zero-length attribute to tell the world we support 8825 * kernel ioctls (for layered drivers) 8826 */ 8827 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8828 DDI_KERNEL_IOCTL, NULL, 0); 8829 8830 /* 8831 * Add a boolean property to tell the world we support 8832 * the B_FAILFAST flag (for layered drivers) 8833 */ 8834 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8835 "ddi-failfast-supported", NULL, 0); 8836 8837 /* 8838 * Initialize power management 8839 */ 8840 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8841 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8842 sd_setup_pm(un, devi); 8843 if (un->un_f_pm_is_enabled == FALSE) { 8844 /* 8845 * For performance, point to a jump table that does 8846 * not include pm. 8847 * The direct and priority chains don't change with PM. 8848 * 8849 * Note: this is currently done based on individual device 8850 * capabilities. When an interface for determining system 8851 * power enabled state becomes available, or when additional 8852 * layers are added to the command chain, these values will 8853 * have to be re-evaluated for correctness. 8854 */ 8855 if (un->un_f_non_devbsize_supported) { 8856 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8857 } else { 8858 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8859 } 8860 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8861 } 8862 8863 /* 8864 * This property is set to 0 by HA software to avoid retries 8865 * on a reserved disk. (The preferred property name is 8866 * "retry-on-reservation-conflict") (1189689) 8867 * 8868 * Note: The use of a global here can have unintended consequences. A 8869 * per instance variable is preferrable to match the capabilities of 8870 * different underlying hba's (4402600) 8871 */ 8872 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8873 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8874 sd_retry_on_reservation_conflict); 8875 if (sd_retry_on_reservation_conflict != 0) { 8876 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8877 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8878 sd_retry_on_reservation_conflict); 8879 } 8880 8881 /* Set up options for QFULL handling. */ 8882 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8883 "qfull-retries", -1)) != -1) { 8884 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8885 rval, 1); 8886 } 8887 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8888 "qfull-retry-interval", -1)) != -1) { 8889 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8890 rval, 1); 8891 } 8892 8893 /* 8894 * This just prints a message that announces the existence of the 8895 * device. The message is always printed in the system logfile, but 8896 * only appears on the console if the system is booted with the 8897 * -v (verbose) argument. 8898 */ 8899 ddi_report_dev(devi); 8900 8901 /* 8902 * The framework calls driver attach routines single-threaded 8903 * for a given instance. However we still acquire SD_MUTEX here 8904 * because this required for calling the sd_validate_geometry() 8905 * and sd_register_devid() functions. 8906 */ 8907 mutex_enter(SD_MUTEX(un)); 8908 un->un_f_geometry_is_valid = FALSE; 8909 un->un_mediastate = DKIO_NONE; 8910 un->un_reserved = -1; 8911 8912 /* 8913 * Read and validate the device's geometry (ie, disk label) 8914 * A new unformatted drive will not have a valid geometry, but 8915 * the driver needs to successfully attach to this device so 8916 * the drive can be formatted via ioctls. 8917 */ 8918 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8919 ENOTSUP)) && 8920 (un->un_blockcount < DK_MAX_BLOCKS)) { 8921 /* 8922 * We found a small disk with an EFI label on it; 8923 * we need to fix up the minor nodes accordingly. 8924 */ 8925 ddi_remove_minor_node(devi, "h"); 8926 ddi_remove_minor_node(devi, "h,raw"); 8927 (void) ddi_create_minor_node(devi, "wd", 8928 S_IFBLK, 8929 (instance << SDUNIT_SHIFT) | WD_NODE, 8930 un->un_node_type, NULL); 8931 (void) ddi_create_minor_node(devi, "wd,raw", 8932 S_IFCHR, 8933 (instance << SDUNIT_SHIFT) | WD_NODE, 8934 un->un_node_type, NULL); 8935 } 8936 #if defined(__i386) || defined(__amd64) 8937 else if (un->un_f_capacity_adjusted == 1) { 8938 /* 8939 * Refer to comments related to off-by-1 at the 8940 * header of this file. 8941 * Adjust minor node for 1TB disk. 8942 */ 8943 ddi_remove_minor_node(devi, "wd"); 8944 ddi_remove_minor_node(devi, "wd,raw"); 8945 (void) ddi_create_minor_node(devi, "h", 8946 S_IFBLK, 8947 (instance << SDUNIT_SHIFT) | WD_NODE, 8948 un->un_node_type, NULL); 8949 (void) ddi_create_minor_node(devi, "h,raw", 8950 S_IFCHR, 8951 (instance << SDUNIT_SHIFT) | WD_NODE, 8952 un->un_node_type, NULL); 8953 } 8954 #endif 8955 /* 8956 * Read and initialize the devid for the unit. 8957 */ 8958 ASSERT(un->un_errstats != NULL); 8959 if (un->un_f_devid_supported) { 8960 sd_register_devid(un, devi, reservation_flag); 8961 } 8962 mutex_exit(SD_MUTEX(un)); 8963 8964 #if (defined(__fibre)) 8965 /* 8966 * Register callbacks for fibre only. You can't do this soley 8967 * on the basis of the devid_type because this is hba specific. 8968 * We need to query our hba capabilities to find out whether to 8969 * register or not. 8970 */ 8971 if (un->un_f_is_fibre) { 8972 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8973 sd_init_event_callbacks(un); 8974 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8975 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8976 } 8977 } 8978 #endif 8979 8980 if (un->un_f_opt_disable_cache == TRUE) { 8981 /* 8982 * Disable both read cache and write cache. This is 8983 * the historic behavior of the keywords in the config file. 8984 */ 8985 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8986 0) { 8987 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8988 "sd_unit_attach: un:0x%p Could not disable " 8989 "caching", un); 8990 goto devid_failed; 8991 } 8992 } 8993 8994 /* 8995 * Check the value of the WCE bit now and 8996 * set un_f_write_cache_enabled accordingly. 8997 */ 8998 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8999 mutex_enter(SD_MUTEX(un)); 9000 un->un_f_write_cache_enabled = (wc_enabled != 0); 9001 mutex_exit(SD_MUTEX(un)); 9002 9003 /* 9004 * Set the pstat and error stat values here, so data obtained during the 9005 * previous attach-time routines is available. 9006 * 9007 * Note: This is a critical sequence that needs to be maintained: 9008 * 1) Instantiate the kstats before any routines using the iopath 9009 * (i.e. sd_send_scsi_cmd). 9010 * 2) Instantiate and initialize the partition stats 9011 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 9012 * see detailed comments there. 9013 * 3) Initialize the error stats (sd_set_errstats), following 9014 * sd_validate_geometry(),sd_register_devid(), 9015 * and sd_cache_control(). 9016 */ 9017 sd_set_errstats(un); 9018 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9019 "sd_unit_attach: un:0x%p errstats set\n", un); 9020 9021 /* 9022 * Find out what type of reservation this disk supports. 9023 */ 9024 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 9025 case 0: 9026 /* 9027 * SCSI-3 reservations are supported. 9028 */ 9029 un->un_reservation_type = SD_SCSI3_RESERVATION; 9030 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9031 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 9032 break; 9033 case ENOTSUP: 9034 /* 9035 * The PERSISTENT RESERVE IN command would not be recognized by 9036 * a SCSI-2 device, so assume the reservation type is SCSI-2. 9037 */ 9038 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9039 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 9040 un->un_reservation_type = SD_SCSI2_RESERVATION; 9041 break; 9042 default: 9043 /* 9044 * default to SCSI-3 reservations 9045 */ 9046 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9047 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 9048 un->un_reservation_type = SD_SCSI3_RESERVATION; 9049 break; 9050 } 9051 9052 /* 9053 * After successfully attaching an instance, we record the information 9054 * of how many luns have been attached on the relative target and 9055 * controller for parallel SCSI. This information is used when sd tries 9056 * to set the tagged queuing capability in HBA. 9057 */ 9058 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9059 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 9060 } 9061 9062 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9063 "sd_unit_attach: un:0x%p exit success\n", un); 9064 9065 return (DDI_SUCCESS); 9066 9067 /* 9068 * An error occurred during the attach; clean up & return failure. 9069 */ 9070 9071 devid_failed: 9072 9073 setup_pm_failed: 9074 ddi_remove_minor_node(devi, NULL); 9075 9076 create_minor_nodes_failed: 9077 /* 9078 * Cleanup from the scsi_ifsetcap() calls (437868) 9079 */ 9080 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9081 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9082 9083 /* 9084 * Refer to the comments of setting tagged-qing in the beginning of 9085 * sd_unit_attach. We can only disable tagged queuing when there is 9086 * no lun attached on the target. 9087 */ 9088 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 9089 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9090 } 9091 9092 if (un->un_f_is_fibre == FALSE) { 9093 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9094 } 9095 9096 spinup_failed: 9097 9098 mutex_enter(SD_MUTEX(un)); 9099 9100 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 9101 if (un->un_direct_priority_timeid != NULL) { 9102 timeout_id_t temp_id = un->un_direct_priority_timeid; 9103 un->un_direct_priority_timeid = NULL; 9104 mutex_exit(SD_MUTEX(un)); 9105 (void) untimeout(temp_id); 9106 mutex_enter(SD_MUTEX(un)); 9107 } 9108 9109 /* Cancel any pending start/stop timeouts */ 9110 if (un->un_startstop_timeid != NULL) { 9111 timeout_id_t temp_id = un->un_startstop_timeid; 9112 un->un_startstop_timeid = NULL; 9113 mutex_exit(SD_MUTEX(un)); 9114 (void) untimeout(temp_id); 9115 mutex_enter(SD_MUTEX(un)); 9116 } 9117 9118 /* Cancel any pending reset-throttle timeouts */ 9119 if (un->un_reset_throttle_timeid != NULL) { 9120 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9121 un->un_reset_throttle_timeid = NULL; 9122 mutex_exit(SD_MUTEX(un)); 9123 (void) untimeout(temp_id); 9124 mutex_enter(SD_MUTEX(un)); 9125 } 9126 9127 /* Cancel any pending retry timeouts */ 9128 if (un->un_retry_timeid != NULL) { 9129 timeout_id_t temp_id = un->un_retry_timeid; 9130 un->un_retry_timeid = NULL; 9131 mutex_exit(SD_MUTEX(un)); 9132 (void) untimeout(temp_id); 9133 mutex_enter(SD_MUTEX(un)); 9134 } 9135 9136 /* Cancel any pending delayed cv broadcast timeouts */ 9137 if (un->un_dcvb_timeid != NULL) { 9138 timeout_id_t temp_id = un->un_dcvb_timeid; 9139 un->un_dcvb_timeid = NULL; 9140 mutex_exit(SD_MUTEX(un)); 9141 (void) untimeout(temp_id); 9142 mutex_enter(SD_MUTEX(un)); 9143 } 9144 9145 mutex_exit(SD_MUTEX(un)); 9146 9147 /* There should not be any in-progress I/O so ASSERT this check */ 9148 ASSERT(un->un_ncmds_in_transport == 0); 9149 ASSERT(un->un_ncmds_in_driver == 0); 9150 9151 /* Do not free the softstate if the callback routine is active */ 9152 sd_sync_with_callback(un); 9153 9154 /* 9155 * Partition stats apparently are not used with removables. These would 9156 * not have been created during attach, so no need to clean them up... 9157 */ 9158 if (un->un_stats != NULL) { 9159 kstat_delete(un->un_stats); 9160 un->un_stats = NULL; 9161 } 9162 if (un->un_errstats != NULL) { 9163 kstat_delete(un->un_errstats); 9164 un->un_errstats = NULL; 9165 } 9166 9167 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9168 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9169 9170 ddi_prop_remove_all(devi); 9171 sema_destroy(&un->un_semoclose); 9172 cv_destroy(&un->un_state_cv); 9173 9174 getrbuf_failed: 9175 9176 sd_free_rqs(un); 9177 9178 alloc_rqs_failed: 9179 9180 devp->sd_private = NULL; 9181 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 9182 9183 get_softstate_failed: 9184 /* 9185 * Note: the man pages are unclear as to whether or not doing a 9186 * ddi_soft_state_free(sd_state, instance) is the right way to 9187 * clean up after the ddi_soft_state_zalloc() if the subsequent 9188 * ddi_get_soft_state() fails. The implication seems to be 9189 * that the get_soft_state cannot fail if the zalloc succeeds. 9190 */ 9191 ddi_soft_state_free(sd_state, instance); 9192 9193 probe_failed: 9194 scsi_unprobe(devp); 9195 #ifdef SDDEBUG 9196 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 9197 (sd_level_mask & SD_LOGMASK_TRACE)) { 9198 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 9199 (void *)un); 9200 } 9201 #endif 9202 return (DDI_FAILURE); 9203 } 9204 9205 9206 /* 9207 * Function: sd_unit_detach 9208 * 9209 * Description: Performs DDI_DETACH processing for sddetach(). 9210 * 9211 * Return Code: DDI_SUCCESS 9212 * DDI_FAILURE 9213 * 9214 * Context: Kernel thread context 9215 */ 9216 9217 static int 9218 sd_unit_detach(dev_info_t *devi) 9219 { 9220 struct scsi_device *devp; 9221 struct sd_lun *un; 9222 int i; 9223 int tgt; 9224 dev_t dev; 9225 dev_info_t *pdip = ddi_get_parent(devi); 9226 int instance = ddi_get_instance(devi); 9227 9228 mutex_enter(&sd_detach_mutex); 9229 9230 /* 9231 * Fail the detach for any of the following: 9232 * - Unable to get the sd_lun struct for the instance 9233 * - A layered driver has an outstanding open on the instance 9234 * - Another thread is already detaching this instance 9235 * - Another thread is currently performing an open 9236 */ 9237 devp = ddi_get_driver_private(devi); 9238 if ((devp == NULL) || 9239 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 9240 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 9241 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 9242 mutex_exit(&sd_detach_mutex); 9243 return (DDI_FAILURE); 9244 } 9245 9246 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 9247 9248 /* 9249 * Mark this instance as currently in a detach, to inhibit any 9250 * opens from a layered driver. 9251 */ 9252 un->un_detach_count++; 9253 mutex_exit(&sd_detach_mutex); 9254 9255 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 9256 SCSI_ADDR_PROP_TARGET, -1); 9257 9258 dev = sd_make_device(SD_DEVINFO(un)); 9259 9260 #ifndef lint 9261 _NOTE(COMPETING_THREADS_NOW); 9262 #endif 9263 9264 mutex_enter(SD_MUTEX(un)); 9265 9266 /* 9267 * Fail the detach if there are any outstanding layered 9268 * opens on this device. 9269 */ 9270 for (i = 0; i < NDKMAP; i++) { 9271 if (un->un_ocmap.lyropen[i] != 0) { 9272 goto err_notclosed; 9273 } 9274 } 9275 9276 /* 9277 * Verify there are NO outstanding commands issued to this device. 9278 * ie, un_ncmds_in_transport == 0. 9279 * It's possible to have outstanding commands through the physio 9280 * code path, even though everything's closed. 9281 */ 9282 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 9283 (un->un_direct_priority_timeid != NULL) || 9284 (un->un_state == SD_STATE_RWAIT)) { 9285 mutex_exit(SD_MUTEX(un)); 9286 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9287 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 9288 goto err_stillbusy; 9289 } 9290 9291 /* 9292 * If we have the device reserved, release the reservation. 9293 */ 9294 if ((un->un_resvd_status & SD_RESERVE) && 9295 !(un->un_resvd_status & SD_LOST_RESERVE)) { 9296 mutex_exit(SD_MUTEX(un)); 9297 /* 9298 * Note: sd_reserve_release sends a command to the device 9299 * via the sd_ioctlcmd() path, and can sleep. 9300 */ 9301 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 9302 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9303 "sd_dr_detach: Cannot release reservation \n"); 9304 } 9305 } else { 9306 mutex_exit(SD_MUTEX(un)); 9307 } 9308 9309 /* 9310 * Untimeout any reserve recover, throttle reset, restart unit 9311 * and delayed broadcast timeout threads. Protect the timeout pointer 9312 * from getting nulled by their callback functions. 9313 */ 9314 mutex_enter(SD_MUTEX(un)); 9315 if (un->un_resvd_timeid != NULL) { 9316 timeout_id_t temp_id = un->un_resvd_timeid; 9317 un->un_resvd_timeid = NULL; 9318 mutex_exit(SD_MUTEX(un)); 9319 (void) untimeout(temp_id); 9320 mutex_enter(SD_MUTEX(un)); 9321 } 9322 9323 if (un->un_reset_throttle_timeid != NULL) { 9324 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9325 un->un_reset_throttle_timeid = NULL; 9326 mutex_exit(SD_MUTEX(un)); 9327 (void) untimeout(temp_id); 9328 mutex_enter(SD_MUTEX(un)); 9329 } 9330 9331 if (un->un_startstop_timeid != NULL) { 9332 timeout_id_t temp_id = un->un_startstop_timeid; 9333 un->un_startstop_timeid = NULL; 9334 mutex_exit(SD_MUTEX(un)); 9335 (void) untimeout(temp_id); 9336 mutex_enter(SD_MUTEX(un)); 9337 } 9338 9339 if (un->un_dcvb_timeid != NULL) { 9340 timeout_id_t temp_id = un->un_dcvb_timeid; 9341 un->un_dcvb_timeid = NULL; 9342 mutex_exit(SD_MUTEX(un)); 9343 (void) untimeout(temp_id); 9344 } else { 9345 mutex_exit(SD_MUTEX(un)); 9346 } 9347 9348 /* Remove any pending reservation reclaim requests for this device */ 9349 sd_rmv_resv_reclaim_req(dev); 9350 9351 mutex_enter(SD_MUTEX(un)); 9352 9353 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9354 if (un->un_direct_priority_timeid != NULL) { 9355 timeout_id_t temp_id = un->un_direct_priority_timeid; 9356 un->un_direct_priority_timeid = NULL; 9357 mutex_exit(SD_MUTEX(un)); 9358 (void) untimeout(temp_id); 9359 mutex_enter(SD_MUTEX(un)); 9360 } 9361 9362 /* Cancel any active multi-host disk watch thread requests */ 9363 if (un->un_mhd_token != NULL) { 9364 mutex_exit(SD_MUTEX(un)); 9365 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9366 if (scsi_watch_request_terminate(un->un_mhd_token, 9367 SCSI_WATCH_TERMINATE_NOWAIT)) { 9368 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9369 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9370 /* 9371 * Note: We are returning here after having removed 9372 * some driver timeouts above. This is consistent with 9373 * the legacy implementation but perhaps the watch 9374 * terminate call should be made with the wait flag set. 9375 */ 9376 goto err_stillbusy; 9377 } 9378 mutex_enter(SD_MUTEX(un)); 9379 un->un_mhd_token = NULL; 9380 } 9381 9382 if (un->un_swr_token != NULL) { 9383 mutex_exit(SD_MUTEX(un)); 9384 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9385 if (scsi_watch_request_terminate(un->un_swr_token, 9386 SCSI_WATCH_TERMINATE_NOWAIT)) { 9387 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9388 "sd_dr_detach: Cannot cancel swr watch request\n"); 9389 /* 9390 * Note: We are returning here after having removed 9391 * some driver timeouts above. This is consistent with 9392 * the legacy implementation but perhaps the watch 9393 * terminate call should be made with the wait flag set. 9394 */ 9395 goto err_stillbusy; 9396 } 9397 mutex_enter(SD_MUTEX(un)); 9398 un->un_swr_token = NULL; 9399 } 9400 9401 mutex_exit(SD_MUTEX(un)); 9402 9403 /* 9404 * Clear any scsi_reset_notifies. We clear the reset notifies 9405 * if we have not registered one. 9406 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9407 */ 9408 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9409 sd_mhd_reset_notify_cb, (caddr_t)un); 9410 9411 /* 9412 * protect the timeout pointers from getting nulled by 9413 * their callback functions during the cancellation process. 9414 * In such a scenario untimeout can be invoked with a null value. 9415 */ 9416 _NOTE(NO_COMPETING_THREADS_NOW); 9417 9418 mutex_enter(&un->un_pm_mutex); 9419 if (un->un_pm_idle_timeid != NULL) { 9420 timeout_id_t temp_id = un->un_pm_idle_timeid; 9421 un->un_pm_idle_timeid = NULL; 9422 mutex_exit(&un->un_pm_mutex); 9423 9424 /* 9425 * Timeout is active; cancel it. 9426 * Note that it'll never be active on a device 9427 * that does not support PM therefore we don't 9428 * have to check before calling pm_idle_component. 9429 */ 9430 (void) untimeout(temp_id); 9431 (void) pm_idle_component(SD_DEVINFO(un), 0); 9432 mutex_enter(&un->un_pm_mutex); 9433 } 9434 9435 /* 9436 * Check whether there is already a timeout scheduled for power 9437 * management. If yes then don't lower the power here, that's. 9438 * the timeout handler's job. 9439 */ 9440 if (un->un_pm_timeid != NULL) { 9441 timeout_id_t temp_id = un->un_pm_timeid; 9442 un->un_pm_timeid = NULL; 9443 mutex_exit(&un->un_pm_mutex); 9444 /* 9445 * Timeout is active; cancel it. 9446 * Note that it'll never be active on a device 9447 * that does not support PM therefore we don't 9448 * have to check before calling pm_idle_component. 9449 */ 9450 (void) untimeout(temp_id); 9451 (void) pm_idle_component(SD_DEVINFO(un), 0); 9452 9453 } else { 9454 mutex_exit(&un->un_pm_mutex); 9455 if ((un->un_f_pm_is_enabled == TRUE) && 9456 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9457 DDI_SUCCESS)) { 9458 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9459 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9460 /* 9461 * Fix for bug: 4297749, item # 13 9462 * The above test now includes a check to see if PM is 9463 * supported by this device before call 9464 * pm_lower_power(). 9465 * Note, the following is not dead code. The call to 9466 * pm_lower_power above will generate a call back into 9467 * our sdpower routine which might result in a timeout 9468 * handler getting activated. Therefore the following 9469 * code is valid and necessary. 9470 */ 9471 mutex_enter(&un->un_pm_mutex); 9472 if (un->un_pm_timeid != NULL) { 9473 timeout_id_t temp_id = un->un_pm_timeid; 9474 un->un_pm_timeid = NULL; 9475 mutex_exit(&un->un_pm_mutex); 9476 (void) untimeout(temp_id); 9477 (void) pm_idle_component(SD_DEVINFO(un), 0); 9478 } else { 9479 mutex_exit(&un->un_pm_mutex); 9480 } 9481 } 9482 } 9483 9484 /* 9485 * Cleanup from the scsi_ifsetcap() calls (437868) 9486 * Relocated here from above to be after the call to 9487 * pm_lower_power, which was getting errors. 9488 */ 9489 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9490 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9491 9492 /* 9493 * Currently, tagged queuing is supported per target based by HBA. 9494 * Setting this per lun instance actually sets the capability of this 9495 * target in HBA, which affects those luns already attached on the 9496 * same target. So during detach, we can only disable this capability 9497 * only when this is the only lun left on this target. By doing 9498 * this, we assume a target has the same tagged queuing capability 9499 * for every lun. The condition can be removed when HBA is changed to 9500 * support per lun based tagged queuing capability. 9501 */ 9502 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 9503 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9504 } 9505 9506 if (un->un_f_is_fibre == FALSE) { 9507 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9508 } 9509 9510 /* 9511 * Remove any event callbacks, fibre only 9512 */ 9513 if (un->un_f_is_fibre == TRUE) { 9514 if ((un->un_insert_event != NULL) && 9515 (ddi_remove_event_handler(un->un_insert_cb_id) != 9516 DDI_SUCCESS)) { 9517 /* 9518 * Note: We are returning here after having done 9519 * substantial cleanup above. This is consistent 9520 * with the legacy implementation but this may not 9521 * be the right thing to do. 9522 */ 9523 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9524 "sd_dr_detach: Cannot cancel insert event\n"); 9525 goto err_remove_event; 9526 } 9527 un->un_insert_event = NULL; 9528 9529 if ((un->un_remove_event != NULL) && 9530 (ddi_remove_event_handler(un->un_remove_cb_id) != 9531 DDI_SUCCESS)) { 9532 /* 9533 * Note: We are returning here after having done 9534 * substantial cleanup above. This is consistent 9535 * with the legacy implementation but this may not 9536 * be the right thing to do. 9537 */ 9538 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9539 "sd_dr_detach: Cannot cancel remove event\n"); 9540 goto err_remove_event; 9541 } 9542 un->un_remove_event = NULL; 9543 } 9544 9545 /* Do not free the softstate if the callback routine is active */ 9546 sd_sync_with_callback(un); 9547 9548 /* 9549 * Hold the detach mutex here, to make sure that no other threads ever 9550 * can access a (partially) freed soft state structure. 9551 */ 9552 mutex_enter(&sd_detach_mutex); 9553 9554 /* 9555 * Clean up the soft state struct. 9556 * Cleanup is done in reverse order of allocs/inits. 9557 * At this point there should be no competing threads anymore. 9558 */ 9559 9560 /* Unregister and free device id. */ 9561 ddi_devid_unregister(devi); 9562 if (un->un_devid) { 9563 ddi_devid_free(un->un_devid); 9564 un->un_devid = NULL; 9565 } 9566 9567 /* 9568 * Destroy wmap cache if it exists. 9569 */ 9570 if (un->un_wm_cache != NULL) { 9571 kmem_cache_destroy(un->un_wm_cache); 9572 un->un_wm_cache = NULL; 9573 } 9574 9575 /* Remove minor nodes */ 9576 ddi_remove_minor_node(devi, NULL); 9577 9578 /* 9579 * kstat cleanup is done in detach for all device types (4363169). 9580 * We do not want to fail detach if the device kstats are not deleted 9581 * since there is a confusion about the devo_refcnt for the device. 9582 * We just delete the kstats and let detach complete successfully. 9583 */ 9584 if (un->un_stats != NULL) { 9585 kstat_delete(un->un_stats); 9586 un->un_stats = NULL; 9587 } 9588 if (un->un_errstats != NULL) { 9589 kstat_delete(un->un_errstats); 9590 un->un_errstats = NULL; 9591 } 9592 9593 /* Remove partition stats */ 9594 if (un->un_f_pkstats_enabled) { 9595 for (i = 0; i < NSDMAP; i++) { 9596 if (un->un_pstats[i] != NULL) { 9597 kstat_delete(un->un_pstats[i]); 9598 un->un_pstats[i] = NULL; 9599 } 9600 } 9601 } 9602 9603 /* Remove xbuf registration */ 9604 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9605 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9606 9607 /* Remove driver properties */ 9608 ddi_prop_remove_all(devi); 9609 9610 mutex_destroy(&un->un_pm_mutex); 9611 cv_destroy(&un->un_pm_busy_cv); 9612 9613 cv_destroy(&un->un_wcc_cv); 9614 9615 /* Open/close semaphore */ 9616 sema_destroy(&un->un_semoclose); 9617 9618 /* Removable media condvar. */ 9619 cv_destroy(&un->un_state_cv); 9620 9621 /* Suspend/resume condvar. */ 9622 cv_destroy(&un->un_suspend_cv); 9623 cv_destroy(&un->un_disk_busy_cv); 9624 9625 sd_free_rqs(un); 9626 9627 /* Free up soft state */ 9628 devp->sd_private = NULL; 9629 bzero(un, sizeof (struct sd_lun)); 9630 ddi_soft_state_free(sd_state, instance); 9631 9632 mutex_exit(&sd_detach_mutex); 9633 9634 /* This frees up the INQUIRY data associated with the device. */ 9635 scsi_unprobe(devp); 9636 9637 /* 9638 * After successfully detaching an instance, we update the information 9639 * of how many luns have been attached in the relative target and 9640 * controller for parallel SCSI. This information is used when sd tries 9641 * to set the tagged queuing capability in HBA. 9642 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9643 * check if the device is parallel SCSI. However, we don't need to 9644 * check here because we've already checked during attach. No device 9645 * that is not parallel SCSI is in the chain. 9646 */ 9647 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9648 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9649 } 9650 9651 return (DDI_SUCCESS); 9652 9653 err_notclosed: 9654 mutex_exit(SD_MUTEX(un)); 9655 9656 err_stillbusy: 9657 _NOTE(NO_COMPETING_THREADS_NOW); 9658 9659 err_remove_event: 9660 mutex_enter(&sd_detach_mutex); 9661 un->un_detach_count--; 9662 mutex_exit(&sd_detach_mutex); 9663 9664 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9665 return (DDI_FAILURE); 9666 } 9667 9668 9669 /* 9670 * Driver minor node structure and data table 9671 */ 9672 struct driver_minor_data { 9673 char *name; 9674 minor_t minor; 9675 int type; 9676 }; 9677 9678 static struct driver_minor_data sd_minor_data[] = { 9679 {"a", 0, S_IFBLK}, 9680 {"b", 1, S_IFBLK}, 9681 {"c", 2, S_IFBLK}, 9682 {"d", 3, S_IFBLK}, 9683 {"e", 4, S_IFBLK}, 9684 {"f", 5, S_IFBLK}, 9685 {"g", 6, S_IFBLK}, 9686 {"h", 7, S_IFBLK}, 9687 #if defined(_SUNOS_VTOC_16) 9688 {"i", 8, S_IFBLK}, 9689 {"j", 9, S_IFBLK}, 9690 {"k", 10, S_IFBLK}, 9691 {"l", 11, S_IFBLK}, 9692 {"m", 12, S_IFBLK}, 9693 {"n", 13, S_IFBLK}, 9694 {"o", 14, S_IFBLK}, 9695 {"p", 15, S_IFBLK}, 9696 #endif /* defined(_SUNOS_VTOC_16) */ 9697 #if defined(_FIRMWARE_NEEDS_FDISK) 9698 {"q", 16, S_IFBLK}, 9699 {"r", 17, S_IFBLK}, 9700 {"s", 18, S_IFBLK}, 9701 {"t", 19, S_IFBLK}, 9702 {"u", 20, S_IFBLK}, 9703 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9704 {"a,raw", 0, S_IFCHR}, 9705 {"b,raw", 1, S_IFCHR}, 9706 {"c,raw", 2, S_IFCHR}, 9707 {"d,raw", 3, S_IFCHR}, 9708 {"e,raw", 4, S_IFCHR}, 9709 {"f,raw", 5, S_IFCHR}, 9710 {"g,raw", 6, S_IFCHR}, 9711 {"h,raw", 7, S_IFCHR}, 9712 #if defined(_SUNOS_VTOC_16) 9713 {"i,raw", 8, S_IFCHR}, 9714 {"j,raw", 9, S_IFCHR}, 9715 {"k,raw", 10, S_IFCHR}, 9716 {"l,raw", 11, S_IFCHR}, 9717 {"m,raw", 12, S_IFCHR}, 9718 {"n,raw", 13, S_IFCHR}, 9719 {"o,raw", 14, S_IFCHR}, 9720 {"p,raw", 15, S_IFCHR}, 9721 #endif /* defined(_SUNOS_VTOC_16) */ 9722 #if defined(_FIRMWARE_NEEDS_FDISK) 9723 {"q,raw", 16, S_IFCHR}, 9724 {"r,raw", 17, S_IFCHR}, 9725 {"s,raw", 18, S_IFCHR}, 9726 {"t,raw", 19, S_IFCHR}, 9727 {"u,raw", 20, S_IFCHR}, 9728 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9729 {0} 9730 }; 9731 9732 static struct driver_minor_data sd_minor_data_efi[] = { 9733 {"a", 0, S_IFBLK}, 9734 {"b", 1, S_IFBLK}, 9735 {"c", 2, S_IFBLK}, 9736 {"d", 3, S_IFBLK}, 9737 {"e", 4, S_IFBLK}, 9738 {"f", 5, S_IFBLK}, 9739 {"g", 6, S_IFBLK}, 9740 {"wd", 7, S_IFBLK}, 9741 #if defined(_FIRMWARE_NEEDS_FDISK) 9742 {"q", 16, S_IFBLK}, 9743 {"r", 17, S_IFBLK}, 9744 {"s", 18, S_IFBLK}, 9745 {"t", 19, S_IFBLK}, 9746 {"u", 20, S_IFBLK}, 9747 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9748 {"a,raw", 0, S_IFCHR}, 9749 {"b,raw", 1, S_IFCHR}, 9750 {"c,raw", 2, S_IFCHR}, 9751 {"d,raw", 3, S_IFCHR}, 9752 {"e,raw", 4, S_IFCHR}, 9753 {"f,raw", 5, S_IFCHR}, 9754 {"g,raw", 6, S_IFCHR}, 9755 {"wd,raw", 7, S_IFCHR}, 9756 #if defined(_FIRMWARE_NEEDS_FDISK) 9757 {"q,raw", 16, S_IFCHR}, 9758 {"r,raw", 17, S_IFCHR}, 9759 {"s,raw", 18, S_IFCHR}, 9760 {"t,raw", 19, S_IFCHR}, 9761 {"u,raw", 20, S_IFCHR}, 9762 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9763 {0} 9764 }; 9765 9766 9767 /* 9768 * Function: sd_create_minor_nodes 9769 * 9770 * Description: Create the minor device nodes for the instance. 9771 * 9772 * Arguments: un - driver soft state (unit) structure 9773 * devi - pointer to device info structure 9774 * 9775 * Return Code: DDI_SUCCESS 9776 * DDI_FAILURE 9777 * 9778 * Context: Kernel thread context 9779 */ 9780 9781 static int 9782 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9783 { 9784 struct driver_minor_data *dmdp; 9785 struct scsi_device *devp; 9786 int instance; 9787 char name[48]; 9788 9789 ASSERT(un != NULL); 9790 devp = ddi_get_driver_private(devi); 9791 instance = ddi_get_instance(devp->sd_dev); 9792 9793 /* 9794 * Create all the minor nodes for this target. 9795 */ 9796 if (un->un_blockcount > DK_MAX_BLOCKS) 9797 dmdp = sd_minor_data_efi; 9798 else 9799 dmdp = sd_minor_data; 9800 while (dmdp->name != NULL) { 9801 9802 (void) sprintf(name, "%s", dmdp->name); 9803 9804 if (ddi_create_minor_node(devi, name, dmdp->type, 9805 (instance << SDUNIT_SHIFT) | dmdp->minor, 9806 un->un_node_type, NULL) == DDI_FAILURE) { 9807 /* 9808 * Clean up any nodes that may have been created, in 9809 * case this fails in the middle of the loop. 9810 */ 9811 ddi_remove_minor_node(devi, NULL); 9812 return (DDI_FAILURE); 9813 } 9814 dmdp++; 9815 } 9816 9817 return (DDI_SUCCESS); 9818 } 9819 9820 9821 /* 9822 * Function: sd_create_errstats 9823 * 9824 * Description: This routine instantiates the device error stats. 9825 * 9826 * Note: During attach the stats are instantiated first so they are 9827 * available for attach-time routines that utilize the driver 9828 * iopath to send commands to the device. The stats are initialized 9829 * separately so data obtained during some attach-time routines is 9830 * available. (4362483) 9831 * 9832 * Arguments: un - driver soft state (unit) structure 9833 * instance - driver instance 9834 * 9835 * Context: Kernel thread context 9836 */ 9837 9838 static void 9839 sd_create_errstats(struct sd_lun *un, int instance) 9840 { 9841 struct sd_errstats *stp; 9842 char kstatmodule_err[KSTAT_STRLEN]; 9843 char kstatname[KSTAT_STRLEN]; 9844 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9845 9846 ASSERT(un != NULL); 9847 9848 if (un->un_errstats != NULL) { 9849 return; 9850 } 9851 9852 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9853 "%serr", sd_label); 9854 (void) snprintf(kstatname, sizeof (kstatname), 9855 "%s%d,err", sd_label, instance); 9856 9857 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9858 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9859 9860 if (un->un_errstats == NULL) { 9861 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9862 "sd_create_errstats: Failed kstat_create\n"); 9863 return; 9864 } 9865 9866 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9867 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9868 KSTAT_DATA_UINT32); 9869 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9870 KSTAT_DATA_UINT32); 9871 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9872 KSTAT_DATA_UINT32); 9873 kstat_named_init(&stp->sd_vid, "Vendor", 9874 KSTAT_DATA_CHAR); 9875 kstat_named_init(&stp->sd_pid, "Product", 9876 KSTAT_DATA_CHAR); 9877 kstat_named_init(&stp->sd_revision, "Revision", 9878 KSTAT_DATA_CHAR); 9879 kstat_named_init(&stp->sd_serial, "Serial No", 9880 KSTAT_DATA_CHAR); 9881 kstat_named_init(&stp->sd_capacity, "Size", 9882 KSTAT_DATA_ULONGLONG); 9883 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9884 KSTAT_DATA_UINT32); 9885 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9886 KSTAT_DATA_UINT32); 9887 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9888 KSTAT_DATA_UINT32); 9889 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9890 KSTAT_DATA_UINT32); 9891 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9892 KSTAT_DATA_UINT32); 9893 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9894 KSTAT_DATA_UINT32); 9895 9896 un->un_errstats->ks_private = un; 9897 un->un_errstats->ks_update = nulldev; 9898 9899 kstat_install(un->un_errstats); 9900 } 9901 9902 9903 /* 9904 * Function: sd_set_errstats 9905 * 9906 * Description: This routine sets the value of the vendor id, product id, 9907 * revision, serial number, and capacity device error stats. 9908 * 9909 * Note: During attach the stats are instantiated first so they are 9910 * available for attach-time routines that utilize the driver 9911 * iopath to send commands to the device. The stats are initialized 9912 * separately so data obtained during some attach-time routines is 9913 * available. (4362483) 9914 * 9915 * Arguments: un - driver soft state (unit) structure 9916 * 9917 * Context: Kernel thread context 9918 */ 9919 9920 static void 9921 sd_set_errstats(struct sd_lun *un) 9922 { 9923 struct sd_errstats *stp; 9924 9925 ASSERT(un != NULL); 9926 ASSERT(un->un_errstats != NULL); 9927 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9928 ASSERT(stp != NULL); 9929 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9930 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9931 (void) strncpy(stp->sd_revision.value.c, 9932 un->un_sd->sd_inq->inq_revision, 4); 9933 9934 /* 9935 * All the errstats are persistent across detach/attach, 9936 * so reset all the errstats here in case of the hot 9937 * replacement of disk drives, except for not changed 9938 * Sun qualified drives. 9939 */ 9940 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9941 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9942 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9943 stp->sd_softerrs.value.ui32 = 0; 9944 stp->sd_harderrs.value.ui32 = 0; 9945 stp->sd_transerrs.value.ui32 = 0; 9946 stp->sd_rq_media_err.value.ui32 = 0; 9947 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9948 stp->sd_rq_nodev_err.value.ui32 = 0; 9949 stp->sd_rq_recov_err.value.ui32 = 0; 9950 stp->sd_rq_illrq_err.value.ui32 = 0; 9951 stp->sd_rq_pfa_err.value.ui32 = 0; 9952 } 9953 9954 /* 9955 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9956 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9957 * (4376302)) 9958 */ 9959 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9960 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9961 sizeof (SD_INQUIRY(un)->inq_serial)); 9962 } 9963 9964 if (un->un_f_blockcount_is_valid != TRUE) { 9965 /* 9966 * Set capacity error stat to 0 for no media. This ensures 9967 * a valid capacity is displayed in response to 'iostat -E' 9968 * when no media is present in the device. 9969 */ 9970 stp->sd_capacity.value.ui64 = 0; 9971 } else { 9972 /* 9973 * Multiply un_blockcount by un->un_sys_blocksize to get 9974 * capacity. 9975 * 9976 * Note: for non-512 blocksize devices "un_blockcount" has been 9977 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9978 * (un_tgt_blocksize / un->un_sys_blocksize). 9979 */ 9980 stp->sd_capacity.value.ui64 = (uint64_t) 9981 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9982 } 9983 } 9984 9985 9986 /* 9987 * Function: sd_set_pstats 9988 * 9989 * Description: This routine instantiates and initializes the partition 9990 * stats for each partition with more than zero blocks. 9991 * (4363169) 9992 * 9993 * Arguments: un - driver soft state (unit) structure 9994 * 9995 * Context: Kernel thread context 9996 */ 9997 9998 static void 9999 sd_set_pstats(struct sd_lun *un) 10000 { 10001 char kstatname[KSTAT_STRLEN]; 10002 int instance; 10003 int i; 10004 10005 ASSERT(un != NULL); 10006 10007 instance = ddi_get_instance(SD_DEVINFO(un)); 10008 10009 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 10010 for (i = 0; i < NSDMAP; i++) { 10011 if ((un->un_pstats[i] == NULL) && 10012 (un->un_map[i].dkl_nblk != 0)) { 10013 (void) snprintf(kstatname, sizeof (kstatname), 10014 "%s%d,%s", sd_label, instance, 10015 sd_minor_data[i].name); 10016 un->un_pstats[i] = kstat_create(sd_label, 10017 instance, kstatname, "partition", KSTAT_TYPE_IO, 10018 1, KSTAT_FLAG_PERSISTENT); 10019 if (un->un_pstats[i] != NULL) { 10020 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 10021 kstat_install(un->un_pstats[i]); 10022 } 10023 } 10024 } 10025 } 10026 10027 10028 #if (defined(__fibre)) 10029 /* 10030 * Function: sd_init_event_callbacks 10031 * 10032 * Description: This routine initializes the insertion and removal event 10033 * callbacks. (fibre only) 10034 * 10035 * Arguments: un - driver soft state (unit) structure 10036 * 10037 * Context: Kernel thread context 10038 */ 10039 10040 static void 10041 sd_init_event_callbacks(struct sd_lun *un) 10042 { 10043 ASSERT(un != NULL); 10044 10045 if ((un->un_insert_event == NULL) && 10046 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 10047 &un->un_insert_event) == DDI_SUCCESS)) { 10048 /* 10049 * Add the callback for an insertion event 10050 */ 10051 (void) ddi_add_event_handler(SD_DEVINFO(un), 10052 un->un_insert_event, sd_event_callback, (void *)un, 10053 &(un->un_insert_cb_id)); 10054 } 10055 10056 if ((un->un_remove_event == NULL) && 10057 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 10058 &un->un_remove_event) == DDI_SUCCESS)) { 10059 /* 10060 * Add the callback for a removal event 10061 */ 10062 (void) ddi_add_event_handler(SD_DEVINFO(un), 10063 un->un_remove_event, sd_event_callback, (void *)un, 10064 &(un->un_remove_cb_id)); 10065 } 10066 } 10067 10068 10069 /* 10070 * Function: sd_event_callback 10071 * 10072 * Description: This routine handles insert/remove events (photon). The 10073 * state is changed to OFFLINE which can be used to supress 10074 * error msgs. (fibre only) 10075 * 10076 * Arguments: un - driver soft state (unit) structure 10077 * 10078 * Context: Callout thread context 10079 */ 10080 /* ARGSUSED */ 10081 static void 10082 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 10083 void *bus_impldata) 10084 { 10085 struct sd_lun *un = (struct sd_lun *)arg; 10086 10087 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 10088 if (event == un->un_insert_event) { 10089 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 10090 mutex_enter(SD_MUTEX(un)); 10091 if (un->un_state == SD_STATE_OFFLINE) { 10092 if (un->un_last_state != SD_STATE_SUSPENDED) { 10093 un->un_state = un->un_last_state; 10094 } else { 10095 /* 10096 * We have gone through SUSPEND/RESUME while 10097 * we were offline. Restore the last state 10098 */ 10099 un->un_state = un->un_save_state; 10100 } 10101 } 10102 mutex_exit(SD_MUTEX(un)); 10103 10104 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 10105 } else if (event == un->un_remove_event) { 10106 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 10107 mutex_enter(SD_MUTEX(un)); 10108 /* 10109 * We need to handle an event callback that occurs during 10110 * the suspend operation, since we don't prevent it. 10111 */ 10112 if (un->un_state != SD_STATE_OFFLINE) { 10113 if (un->un_state != SD_STATE_SUSPENDED) { 10114 New_state(un, SD_STATE_OFFLINE); 10115 } else { 10116 un->un_last_state = SD_STATE_OFFLINE; 10117 } 10118 } 10119 mutex_exit(SD_MUTEX(un)); 10120 } else { 10121 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 10122 "!Unknown event\n"); 10123 } 10124 10125 } 10126 #endif 10127 10128 /* 10129 * Function: sd_cache_control() 10130 * 10131 * Description: This routine is the driver entry point for setting 10132 * read and write caching by modifying the WCE (write cache 10133 * enable) and RCD (read cache disable) bits of mode 10134 * page 8 (MODEPAGE_CACHING). 10135 * 10136 * Arguments: un - driver soft state (unit) structure 10137 * rcd_flag - flag for controlling the read cache 10138 * wce_flag - flag for controlling the write cache 10139 * 10140 * Return Code: EIO 10141 * code returned by sd_send_scsi_MODE_SENSE and 10142 * sd_send_scsi_MODE_SELECT 10143 * 10144 * Context: Kernel Thread 10145 */ 10146 10147 static int 10148 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 10149 { 10150 struct mode_caching *mode_caching_page; 10151 uchar_t *header; 10152 size_t buflen; 10153 int hdrlen; 10154 int bd_len; 10155 int rval = 0; 10156 struct mode_header_grp2 *mhp; 10157 10158 ASSERT(un != NULL); 10159 10160 /* 10161 * Do a test unit ready, otherwise a mode sense may not work if this 10162 * is the first command sent to the device after boot. 10163 */ 10164 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10165 10166 if (un->un_f_cfg_is_atapi == TRUE) { 10167 hdrlen = MODE_HEADER_LENGTH_GRP2; 10168 } else { 10169 hdrlen = MODE_HEADER_LENGTH; 10170 } 10171 10172 /* 10173 * Allocate memory for the retrieved mode page and its headers. Set 10174 * a pointer to the page itself. Use mode_cache_scsi3 to insure 10175 * we get all of the mode sense data otherwise, the mode select 10176 * will fail. mode_cache_scsi3 is a superset of mode_caching. 10177 */ 10178 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 10179 sizeof (struct mode_cache_scsi3); 10180 10181 header = kmem_zalloc(buflen, KM_SLEEP); 10182 10183 /* Get the information from the device. */ 10184 if (un->un_f_cfg_is_atapi == TRUE) { 10185 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10186 MODEPAGE_CACHING, SD_PATH_DIRECT); 10187 } else { 10188 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10189 MODEPAGE_CACHING, SD_PATH_DIRECT); 10190 } 10191 if (rval != 0) { 10192 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10193 "sd_cache_control: Mode Sense Failed\n"); 10194 kmem_free(header, buflen); 10195 return (rval); 10196 } 10197 10198 /* 10199 * Determine size of Block Descriptors in order to locate 10200 * the mode page data. ATAPI devices return 0, SCSI devices 10201 * should return MODE_BLK_DESC_LENGTH. 10202 */ 10203 if (un->un_f_cfg_is_atapi == TRUE) { 10204 mhp = (struct mode_header_grp2 *)header; 10205 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10206 } else { 10207 bd_len = ((struct mode_header *)header)->bdesc_length; 10208 } 10209 10210 if (bd_len > MODE_BLK_DESC_LENGTH) { 10211 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10212 "sd_cache_control: Mode Sense returned invalid " 10213 "block descriptor length\n"); 10214 kmem_free(header, buflen); 10215 return (EIO); 10216 } 10217 10218 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10219 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10220 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10221 " caching page code mismatch %d\n", 10222 mode_caching_page->mode_page.code); 10223 kmem_free(header, buflen); 10224 return (EIO); 10225 } 10226 10227 /* Check the relevant bits on successful mode sense. */ 10228 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 10229 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 10230 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 10231 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 10232 10233 size_t sbuflen; 10234 uchar_t save_pg; 10235 10236 /* 10237 * Construct select buffer length based on the 10238 * length of the sense data returned. 10239 */ 10240 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 10241 sizeof (struct mode_page) + 10242 (int)mode_caching_page->mode_page.length; 10243 10244 /* 10245 * Set the caching bits as requested. 10246 */ 10247 if (rcd_flag == SD_CACHE_ENABLE) 10248 mode_caching_page->rcd = 0; 10249 else if (rcd_flag == SD_CACHE_DISABLE) 10250 mode_caching_page->rcd = 1; 10251 10252 if (wce_flag == SD_CACHE_ENABLE) 10253 mode_caching_page->wce = 1; 10254 else if (wce_flag == SD_CACHE_DISABLE) 10255 mode_caching_page->wce = 0; 10256 10257 /* 10258 * Save the page if the mode sense says the 10259 * drive supports it. 10260 */ 10261 save_pg = mode_caching_page->mode_page.ps ? 10262 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 10263 10264 /* Clear reserved bits before mode select. */ 10265 mode_caching_page->mode_page.ps = 0; 10266 10267 /* 10268 * Clear out mode header for mode select. 10269 * The rest of the retrieved page will be reused. 10270 */ 10271 bzero(header, hdrlen); 10272 10273 if (un->un_f_cfg_is_atapi == TRUE) { 10274 mhp = (struct mode_header_grp2 *)header; 10275 mhp->bdesc_length_hi = bd_len >> 8; 10276 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 10277 } else { 10278 ((struct mode_header *)header)->bdesc_length = bd_len; 10279 } 10280 10281 /* Issue mode select to change the cache settings */ 10282 if (un->un_f_cfg_is_atapi == TRUE) { 10283 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 10284 sbuflen, save_pg, SD_PATH_DIRECT); 10285 } else { 10286 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 10287 sbuflen, save_pg, SD_PATH_DIRECT); 10288 } 10289 } 10290 10291 kmem_free(header, buflen); 10292 return (rval); 10293 } 10294 10295 10296 /* 10297 * Function: sd_get_write_cache_enabled() 10298 * 10299 * Description: This routine is the driver entry point for determining if 10300 * write caching is enabled. It examines the WCE (write cache 10301 * enable) bits of mode page 8 (MODEPAGE_CACHING). 10302 * 10303 * Arguments: un - driver soft state (unit) structure 10304 * is_enabled - pointer to int where write cache enabled state 10305 * is returned (non-zero -> write cache enabled) 10306 * 10307 * 10308 * Return Code: EIO 10309 * code returned by sd_send_scsi_MODE_SENSE 10310 * 10311 * Context: Kernel Thread 10312 * 10313 * NOTE: If ioctl is added to disable write cache, this sequence should 10314 * be followed so that no locking is required for accesses to 10315 * un->un_f_write_cache_enabled: 10316 * do mode select to clear wce 10317 * do synchronize cache to flush cache 10318 * set un->un_f_write_cache_enabled = FALSE 10319 * 10320 * Conversely, an ioctl to enable the write cache should be done 10321 * in this order: 10322 * set un->un_f_write_cache_enabled = TRUE 10323 * do mode select to set wce 10324 */ 10325 10326 static int 10327 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 10328 { 10329 struct mode_caching *mode_caching_page; 10330 uchar_t *header; 10331 size_t buflen; 10332 int hdrlen; 10333 int bd_len; 10334 int rval = 0; 10335 10336 ASSERT(un != NULL); 10337 ASSERT(is_enabled != NULL); 10338 10339 /* in case of error, flag as enabled */ 10340 *is_enabled = TRUE; 10341 10342 /* 10343 * Do a test unit ready, otherwise a mode sense may not work if this 10344 * is the first command sent to the device after boot. 10345 */ 10346 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10347 10348 if (un->un_f_cfg_is_atapi == TRUE) { 10349 hdrlen = MODE_HEADER_LENGTH_GRP2; 10350 } else { 10351 hdrlen = MODE_HEADER_LENGTH; 10352 } 10353 10354 /* 10355 * Allocate memory for the retrieved mode page and its headers. Set 10356 * a pointer to the page itself. 10357 */ 10358 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 10359 header = kmem_zalloc(buflen, KM_SLEEP); 10360 10361 /* Get the information from the device. */ 10362 if (un->un_f_cfg_is_atapi == TRUE) { 10363 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10364 MODEPAGE_CACHING, SD_PATH_DIRECT); 10365 } else { 10366 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10367 MODEPAGE_CACHING, SD_PATH_DIRECT); 10368 } 10369 if (rval != 0) { 10370 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10371 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10372 kmem_free(header, buflen); 10373 return (rval); 10374 } 10375 10376 /* 10377 * Determine size of Block Descriptors in order to locate 10378 * the mode page data. ATAPI devices return 0, SCSI devices 10379 * should return MODE_BLK_DESC_LENGTH. 10380 */ 10381 if (un->un_f_cfg_is_atapi == TRUE) { 10382 struct mode_header_grp2 *mhp; 10383 mhp = (struct mode_header_grp2 *)header; 10384 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10385 } else { 10386 bd_len = ((struct mode_header *)header)->bdesc_length; 10387 } 10388 10389 if (bd_len > MODE_BLK_DESC_LENGTH) { 10390 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10391 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10392 "block descriptor length\n"); 10393 kmem_free(header, buflen); 10394 return (EIO); 10395 } 10396 10397 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10398 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10399 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10400 " caching page code mismatch %d\n", 10401 mode_caching_page->mode_page.code); 10402 kmem_free(header, buflen); 10403 return (EIO); 10404 } 10405 *is_enabled = mode_caching_page->wce; 10406 10407 kmem_free(header, buflen); 10408 return (0); 10409 } 10410 10411 10412 /* 10413 * Function: sd_make_device 10414 * 10415 * Description: Utility routine to return the Solaris device number from 10416 * the data in the device's dev_info structure. 10417 * 10418 * Return Code: The Solaris device number 10419 * 10420 * Context: Any 10421 */ 10422 10423 static dev_t 10424 sd_make_device(dev_info_t *devi) 10425 { 10426 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10427 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10428 } 10429 10430 10431 /* 10432 * Function: sd_pm_entry 10433 * 10434 * Description: Called at the start of a new command to manage power 10435 * and busy status of a device. This includes determining whether 10436 * the current power state of the device is sufficient for 10437 * performing the command or whether it must be changed. 10438 * The PM framework is notified appropriately. 10439 * Only with a return status of DDI_SUCCESS will the 10440 * component be busy to the framework. 10441 * 10442 * All callers of sd_pm_entry must check the return status 10443 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10444 * of DDI_FAILURE indicates the device failed to power up. 10445 * In this case un_pm_count has been adjusted so the result 10446 * on exit is still powered down, ie. count is less than 0. 10447 * Calling sd_pm_exit with this count value hits an ASSERT. 10448 * 10449 * Return Code: DDI_SUCCESS or DDI_FAILURE 10450 * 10451 * Context: Kernel thread context. 10452 */ 10453 10454 static int 10455 sd_pm_entry(struct sd_lun *un) 10456 { 10457 int return_status = DDI_SUCCESS; 10458 10459 ASSERT(!mutex_owned(SD_MUTEX(un))); 10460 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10461 10462 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10463 10464 if (un->un_f_pm_is_enabled == FALSE) { 10465 SD_TRACE(SD_LOG_IO_PM, un, 10466 "sd_pm_entry: exiting, PM not enabled\n"); 10467 return (return_status); 10468 } 10469 10470 /* 10471 * Just increment a counter if PM is enabled. On the transition from 10472 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10473 * the count with each IO and mark the device as idle when the count 10474 * hits 0. 10475 * 10476 * If the count is less than 0 the device is powered down. If a powered 10477 * down device is successfully powered up then the count must be 10478 * incremented to reflect the power up. Note that it'll get incremented 10479 * a second time to become busy. 10480 * 10481 * Because the following has the potential to change the device state 10482 * and must release the un_pm_mutex to do so, only one thread can be 10483 * allowed through at a time. 10484 */ 10485 10486 mutex_enter(&un->un_pm_mutex); 10487 while (un->un_pm_busy == TRUE) { 10488 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10489 } 10490 un->un_pm_busy = TRUE; 10491 10492 if (un->un_pm_count < 1) { 10493 10494 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10495 10496 /* 10497 * Indicate we are now busy so the framework won't attempt to 10498 * power down the device. This call will only fail if either 10499 * we passed a bad component number or the device has no 10500 * components. Neither of these should ever happen. 10501 */ 10502 mutex_exit(&un->un_pm_mutex); 10503 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10504 ASSERT(return_status == DDI_SUCCESS); 10505 10506 mutex_enter(&un->un_pm_mutex); 10507 10508 if (un->un_pm_count < 0) { 10509 mutex_exit(&un->un_pm_mutex); 10510 10511 SD_TRACE(SD_LOG_IO_PM, un, 10512 "sd_pm_entry: power up component\n"); 10513 10514 /* 10515 * pm_raise_power will cause sdpower to be called 10516 * which brings the device power level to the 10517 * desired state, ON in this case. If successful, 10518 * un_pm_count and un_power_level will be updated 10519 * appropriately. 10520 */ 10521 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10522 SD_SPINDLE_ON); 10523 10524 mutex_enter(&un->un_pm_mutex); 10525 10526 if (return_status != DDI_SUCCESS) { 10527 /* 10528 * Power up failed. 10529 * Idle the device and adjust the count 10530 * so the result on exit is that we're 10531 * still powered down, ie. count is less than 0. 10532 */ 10533 SD_TRACE(SD_LOG_IO_PM, un, 10534 "sd_pm_entry: power up failed," 10535 " idle the component\n"); 10536 10537 (void) pm_idle_component(SD_DEVINFO(un), 0); 10538 un->un_pm_count--; 10539 } else { 10540 /* 10541 * Device is powered up, verify the 10542 * count is non-negative. 10543 * This is debug only. 10544 */ 10545 ASSERT(un->un_pm_count == 0); 10546 } 10547 } 10548 10549 if (return_status == DDI_SUCCESS) { 10550 /* 10551 * For performance, now that the device has been tagged 10552 * as busy, and it's known to be powered up, update the 10553 * chain types to use jump tables that do not include 10554 * pm. This significantly lowers the overhead and 10555 * therefore improves performance. 10556 */ 10557 10558 mutex_exit(&un->un_pm_mutex); 10559 mutex_enter(SD_MUTEX(un)); 10560 SD_TRACE(SD_LOG_IO_PM, un, 10561 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10562 un->un_uscsi_chain_type); 10563 10564 if (un->un_f_non_devbsize_supported) { 10565 un->un_buf_chain_type = 10566 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10567 } else { 10568 un->un_buf_chain_type = 10569 SD_CHAIN_INFO_DISK_NO_PM; 10570 } 10571 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10572 10573 SD_TRACE(SD_LOG_IO_PM, un, 10574 " changed uscsi_chain_type to %d\n", 10575 un->un_uscsi_chain_type); 10576 mutex_exit(SD_MUTEX(un)); 10577 mutex_enter(&un->un_pm_mutex); 10578 10579 if (un->un_pm_idle_timeid == NULL) { 10580 /* 300 ms. */ 10581 un->un_pm_idle_timeid = 10582 timeout(sd_pm_idletimeout_handler, un, 10583 (drv_usectohz((clock_t)300000))); 10584 /* 10585 * Include an extra call to busy which keeps the 10586 * device busy with-respect-to the PM layer 10587 * until the timer fires, at which time it'll 10588 * get the extra idle call. 10589 */ 10590 (void) pm_busy_component(SD_DEVINFO(un), 0); 10591 } 10592 } 10593 } 10594 un->un_pm_busy = FALSE; 10595 /* Next... */ 10596 cv_signal(&un->un_pm_busy_cv); 10597 10598 un->un_pm_count++; 10599 10600 SD_TRACE(SD_LOG_IO_PM, un, 10601 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10602 10603 mutex_exit(&un->un_pm_mutex); 10604 10605 return (return_status); 10606 } 10607 10608 10609 /* 10610 * Function: sd_pm_exit 10611 * 10612 * Description: Called at the completion of a command to manage busy 10613 * status for the device. If the device becomes idle the 10614 * PM framework is notified. 10615 * 10616 * Context: Kernel thread context 10617 */ 10618 10619 static void 10620 sd_pm_exit(struct sd_lun *un) 10621 { 10622 ASSERT(!mutex_owned(SD_MUTEX(un))); 10623 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10624 10625 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10626 10627 /* 10628 * After attach the following flag is only read, so don't 10629 * take the penalty of acquiring a mutex for it. 10630 */ 10631 if (un->un_f_pm_is_enabled == TRUE) { 10632 10633 mutex_enter(&un->un_pm_mutex); 10634 un->un_pm_count--; 10635 10636 SD_TRACE(SD_LOG_IO_PM, un, 10637 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10638 10639 ASSERT(un->un_pm_count >= 0); 10640 if (un->un_pm_count == 0) { 10641 mutex_exit(&un->un_pm_mutex); 10642 10643 SD_TRACE(SD_LOG_IO_PM, un, 10644 "sd_pm_exit: idle component\n"); 10645 10646 (void) pm_idle_component(SD_DEVINFO(un), 0); 10647 10648 } else { 10649 mutex_exit(&un->un_pm_mutex); 10650 } 10651 } 10652 10653 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10654 } 10655 10656 10657 /* 10658 * Function: sdopen 10659 * 10660 * Description: Driver's open(9e) entry point function. 10661 * 10662 * Arguments: dev_i - pointer to device number 10663 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10664 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10665 * cred_p - user credential pointer 10666 * 10667 * Return Code: EINVAL 10668 * ENXIO 10669 * EIO 10670 * EROFS 10671 * EBUSY 10672 * 10673 * Context: Kernel thread context 10674 */ 10675 /* ARGSUSED */ 10676 static int 10677 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10678 { 10679 struct sd_lun *un; 10680 int nodelay; 10681 int part; 10682 uint64_t partmask; 10683 int instance; 10684 dev_t dev; 10685 int rval = EIO; 10686 10687 /* Validate the open type */ 10688 if (otyp >= OTYPCNT) { 10689 return (EINVAL); 10690 } 10691 10692 dev = *dev_p; 10693 instance = SDUNIT(dev); 10694 mutex_enter(&sd_detach_mutex); 10695 10696 /* 10697 * Fail the open if there is no softstate for the instance, or 10698 * if another thread somewhere is trying to detach the instance. 10699 */ 10700 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10701 (un->un_detach_count != 0)) { 10702 mutex_exit(&sd_detach_mutex); 10703 /* 10704 * The probe cache only needs to be cleared when open (9e) fails 10705 * with ENXIO (4238046). 10706 */ 10707 /* 10708 * un-conditionally clearing probe cache is ok with 10709 * separate sd/ssd binaries 10710 * x86 platform can be an issue with both parallel 10711 * and fibre in 1 binary 10712 */ 10713 sd_scsi_clear_probe_cache(); 10714 return (ENXIO); 10715 } 10716 10717 /* 10718 * The un_layer_count is to prevent another thread in specfs from 10719 * trying to detach the instance, which can happen when we are 10720 * called from a higher-layer driver instead of thru specfs. 10721 * This will not be needed when DDI provides a layered driver 10722 * interface that allows specfs to know that an instance is in 10723 * use by a layered driver & should not be detached. 10724 * 10725 * Note: the semantics for layered driver opens are exactly one 10726 * close for every open. 10727 */ 10728 if (otyp == OTYP_LYR) { 10729 un->un_layer_count++; 10730 } 10731 10732 /* 10733 * Keep a count of the current # of opens in progress. This is because 10734 * some layered drivers try to call us as a regular open. This can 10735 * cause problems that we cannot prevent, however by keeping this count 10736 * we can at least keep our open and detach routines from racing against 10737 * each other under such conditions. 10738 */ 10739 un->un_opens_in_progress++; 10740 mutex_exit(&sd_detach_mutex); 10741 10742 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10743 part = SDPART(dev); 10744 partmask = 1 << part; 10745 10746 /* 10747 * We use a semaphore here in order to serialize 10748 * open and close requests on the device. 10749 */ 10750 sema_p(&un->un_semoclose); 10751 10752 mutex_enter(SD_MUTEX(un)); 10753 10754 /* 10755 * All device accesses go thru sdstrategy() where we check 10756 * on suspend status but there could be a scsi_poll command, 10757 * which bypasses sdstrategy(), so we need to check pm 10758 * status. 10759 */ 10760 10761 if (!nodelay) { 10762 while ((un->un_state == SD_STATE_SUSPENDED) || 10763 (un->un_state == SD_STATE_PM_CHANGING)) { 10764 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10765 } 10766 10767 mutex_exit(SD_MUTEX(un)); 10768 if (sd_pm_entry(un) != DDI_SUCCESS) { 10769 rval = EIO; 10770 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10771 "sdopen: sd_pm_entry failed\n"); 10772 goto open_failed_with_pm; 10773 } 10774 mutex_enter(SD_MUTEX(un)); 10775 } 10776 10777 /* check for previous exclusive open */ 10778 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10779 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10780 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10781 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10782 10783 if (un->un_exclopen & (partmask)) { 10784 goto excl_open_fail; 10785 } 10786 10787 if (flag & FEXCL) { 10788 int i; 10789 if (un->un_ocmap.lyropen[part]) { 10790 goto excl_open_fail; 10791 } 10792 for (i = 0; i < (OTYPCNT - 1); i++) { 10793 if (un->un_ocmap.regopen[i] & (partmask)) { 10794 goto excl_open_fail; 10795 } 10796 } 10797 } 10798 10799 /* 10800 * Check the write permission if this is a removable media device, 10801 * NDELAY has not been set, and writable permission is requested. 10802 * 10803 * Note: If NDELAY was set and this is write-protected media the WRITE 10804 * attempt will fail with EIO as part of the I/O processing. This is a 10805 * more permissive implementation that allows the open to succeed and 10806 * WRITE attempts to fail when appropriate. 10807 */ 10808 if (un->un_f_chk_wp_open) { 10809 if ((flag & FWRITE) && (!nodelay)) { 10810 mutex_exit(SD_MUTEX(un)); 10811 /* 10812 * Defer the check for write permission on writable 10813 * DVD drive till sdstrategy and will not fail open even 10814 * if FWRITE is set as the device can be writable 10815 * depending upon the media and the media can change 10816 * after the call to open(). 10817 */ 10818 if (un->un_f_dvdram_writable_device == FALSE) { 10819 if (ISCD(un) || sr_check_wp(dev)) { 10820 rval = EROFS; 10821 mutex_enter(SD_MUTEX(un)); 10822 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10823 "write to cd or write protected media\n"); 10824 goto open_fail; 10825 } 10826 } 10827 mutex_enter(SD_MUTEX(un)); 10828 } 10829 } 10830 10831 /* 10832 * If opening in NDELAY/NONBLOCK mode, just return. 10833 * Check if disk is ready and has a valid geometry later. 10834 */ 10835 if (!nodelay) { 10836 mutex_exit(SD_MUTEX(un)); 10837 rval = sd_ready_and_valid(un); 10838 mutex_enter(SD_MUTEX(un)); 10839 /* 10840 * Fail if device is not ready or if the number of disk 10841 * blocks is zero or negative for non CD devices. 10842 */ 10843 if ((rval != SD_READY_VALID) || 10844 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10845 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10846 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10847 "device not ready or invalid disk block value\n"); 10848 goto open_fail; 10849 } 10850 #if defined(__i386) || defined(__amd64) 10851 } else { 10852 uchar_t *cp; 10853 /* 10854 * x86 requires special nodelay handling, so that p0 is 10855 * always defined and accessible. 10856 * Invalidate geometry only if device is not already open. 10857 */ 10858 cp = &un->un_ocmap.chkd[0]; 10859 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10860 if (*cp != (uchar_t)0) { 10861 break; 10862 } 10863 cp++; 10864 } 10865 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10866 un->un_f_geometry_is_valid = FALSE; 10867 } 10868 10869 #endif 10870 } 10871 10872 if (otyp == OTYP_LYR) { 10873 un->un_ocmap.lyropen[part]++; 10874 } else { 10875 un->un_ocmap.regopen[otyp] |= partmask; 10876 } 10877 10878 /* Set up open and exclusive open flags */ 10879 if (flag & FEXCL) { 10880 un->un_exclopen |= (partmask); 10881 } 10882 10883 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10884 "open of part %d type %d\n", part, otyp); 10885 10886 mutex_exit(SD_MUTEX(un)); 10887 if (!nodelay) { 10888 sd_pm_exit(un); 10889 } 10890 10891 sema_v(&un->un_semoclose); 10892 10893 mutex_enter(&sd_detach_mutex); 10894 un->un_opens_in_progress--; 10895 mutex_exit(&sd_detach_mutex); 10896 10897 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10898 return (DDI_SUCCESS); 10899 10900 excl_open_fail: 10901 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10902 rval = EBUSY; 10903 10904 open_fail: 10905 mutex_exit(SD_MUTEX(un)); 10906 10907 /* 10908 * On a failed open we must exit the pm management. 10909 */ 10910 if (!nodelay) { 10911 sd_pm_exit(un); 10912 } 10913 open_failed_with_pm: 10914 sema_v(&un->un_semoclose); 10915 10916 mutex_enter(&sd_detach_mutex); 10917 un->un_opens_in_progress--; 10918 if (otyp == OTYP_LYR) { 10919 un->un_layer_count--; 10920 } 10921 mutex_exit(&sd_detach_mutex); 10922 10923 return (rval); 10924 } 10925 10926 10927 /* 10928 * Function: sdclose 10929 * 10930 * Description: Driver's close(9e) entry point function. 10931 * 10932 * Arguments: dev - device number 10933 * flag - file status flag, informational only 10934 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10935 * cred_p - user credential pointer 10936 * 10937 * Return Code: ENXIO 10938 * 10939 * Context: Kernel thread context 10940 */ 10941 /* ARGSUSED */ 10942 static int 10943 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10944 { 10945 struct sd_lun *un; 10946 uchar_t *cp; 10947 int part; 10948 int nodelay; 10949 int rval = 0; 10950 10951 /* Validate the open type */ 10952 if (otyp >= OTYPCNT) { 10953 return (ENXIO); 10954 } 10955 10956 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10957 return (ENXIO); 10958 } 10959 10960 part = SDPART(dev); 10961 nodelay = flag & (FNDELAY | FNONBLOCK); 10962 10963 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10964 "sdclose: close of part %d type %d\n", part, otyp); 10965 10966 /* 10967 * We use a semaphore here in order to serialize 10968 * open and close requests on the device. 10969 */ 10970 sema_p(&un->un_semoclose); 10971 10972 mutex_enter(SD_MUTEX(un)); 10973 10974 /* Don't proceed if power is being changed. */ 10975 while (un->un_state == SD_STATE_PM_CHANGING) { 10976 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10977 } 10978 10979 if (un->un_exclopen & (1 << part)) { 10980 un->un_exclopen &= ~(1 << part); 10981 } 10982 10983 /* Update the open partition map */ 10984 if (otyp == OTYP_LYR) { 10985 un->un_ocmap.lyropen[part] -= 1; 10986 } else { 10987 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10988 } 10989 10990 cp = &un->un_ocmap.chkd[0]; 10991 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10992 if (*cp != NULL) { 10993 break; 10994 } 10995 cp++; 10996 } 10997 10998 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10999 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 11000 11001 /* 11002 * We avoid persistance upon the last close, and set 11003 * the throttle back to the maximum. 11004 */ 11005 un->un_throttle = un->un_saved_throttle; 11006 11007 if (un->un_state == SD_STATE_OFFLINE) { 11008 if (un->un_f_is_fibre == FALSE) { 11009 scsi_log(SD_DEVINFO(un), sd_label, 11010 CE_WARN, "offline\n"); 11011 } 11012 un->un_f_geometry_is_valid = FALSE; 11013 11014 } else { 11015 /* 11016 * Flush any outstanding writes in NVRAM cache. 11017 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 11018 * cmd, it may not work for non-Pluto devices. 11019 * SYNCHRONIZE CACHE is not required for removables, 11020 * except DVD-RAM drives. 11021 * 11022 * Also note: because SYNCHRONIZE CACHE is currently 11023 * the only command issued here that requires the 11024 * drive be powered up, only do the power up before 11025 * sending the Sync Cache command. If additional 11026 * commands are added which require a powered up 11027 * drive, the following sequence may have to change. 11028 * 11029 * And finally, note that parallel SCSI on SPARC 11030 * only issues a Sync Cache to DVD-RAM, a newly 11031 * supported device. 11032 */ 11033 #if defined(__i386) || defined(__amd64) 11034 if (un->un_f_sync_cache_supported || 11035 un->un_f_dvdram_writable_device == TRUE) { 11036 #else 11037 if (un->un_f_dvdram_writable_device == TRUE) { 11038 #endif 11039 mutex_exit(SD_MUTEX(un)); 11040 if (sd_pm_entry(un) == DDI_SUCCESS) { 11041 rval = 11042 sd_send_scsi_SYNCHRONIZE_CACHE(un, 11043 NULL); 11044 /* ignore error if not supported */ 11045 if (rval == ENOTSUP) { 11046 rval = 0; 11047 } else if (rval != 0) { 11048 rval = EIO; 11049 } 11050 sd_pm_exit(un); 11051 } else { 11052 rval = EIO; 11053 } 11054 mutex_enter(SD_MUTEX(un)); 11055 } 11056 11057 /* 11058 * For devices which supports DOOR_LOCK, send an ALLOW 11059 * MEDIA REMOVAL command, but don't get upset if it 11060 * fails. We need to raise the power of the drive before 11061 * we can call sd_send_scsi_DOORLOCK() 11062 */ 11063 if (un->un_f_doorlock_supported) { 11064 mutex_exit(SD_MUTEX(un)); 11065 if (sd_pm_entry(un) == DDI_SUCCESS) { 11066 rval = sd_send_scsi_DOORLOCK(un, 11067 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 11068 11069 sd_pm_exit(un); 11070 if (ISCD(un) && (rval != 0) && 11071 (nodelay != 0)) { 11072 rval = ENXIO; 11073 } 11074 } else { 11075 rval = EIO; 11076 } 11077 mutex_enter(SD_MUTEX(un)); 11078 } 11079 11080 /* 11081 * If a device has removable media, invalidate all 11082 * parameters related to media, such as geometry, 11083 * blocksize, and blockcount. 11084 */ 11085 if (un->un_f_has_removable_media) { 11086 sr_ejected(un); 11087 } 11088 11089 /* 11090 * Destroy the cache (if it exists) which was 11091 * allocated for the write maps since this is 11092 * the last close for this media. 11093 */ 11094 if (un->un_wm_cache) { 11095 /* 11096 * Check if there are pending commands. 11097 * and if there are give a warning and 11098 * do not destroy the cache. 11099 */ 11100 if (un->un_ncmds_in_driver > 0) { 11101 scsi_log(SD_DEVINFO(un), 11102 sd_label, CE_WARN, 11103 "Unable to clean up memory " 11104 "because of pending I/O\n"); 11105 } else { 11106 kmem_cache_destroy( 11107 un->un_wm_cache); 11108 un->un_wm_cache = NULL; 11109 } 11110 } 11111 } 11112 } 11113 11114 mutex_exit(SD_MUTEX(un)); 11115 sema_v(&un->un_semoclose); 11116 11117 if (otyp == OTYP_LYR) { 11118 mutex_enter(&sd_detach_mutex); 11119 /* 11120 * The detach routine may run when the layer count 11121 * drops to zero. 11122 */ 11123 un->un_layer_count--; 11124 mutex_exit(&sd_detach_mutex); 11125 } 11126 11127 return (rval); 11128 } 11129 11130 11131 /* 11132 * Function: sd_ready_and_valid 11133 * 11134 * Description: Test if device is ready and has a valid geometry. 11135 * 11136 * Arguments: dev - device number 11137 * un - driver soft state (unit) structure 11138 * 11139 * Return Code: SD_READY_VALID ready and valid label 11140 * SD_READY_NOT_VALID ready, geom ops never applicable 11141 * SD_NOT_READY_VALID not ready, no label 11142 * SD_RESERVED_BY_OTHERS reservation conflict 11143 * 11144 * Context: Never called at interrupt context. 11145 */ 11146 11147 static int 11148 sd_ready_and_valid(struct sd_lun *un) 11149 { 11150 struct sd_errstats *stp; 11151 uint64_t capacity; 11152 uint_t lbasize; 11153 int rval = SD_READY_VALID; 11154 char name_str[48]; 11155 11156 ASSERT(un != NULL); 11157 ASSERT(!mutex_owned(SD_MUTEX(un))); 11158 11159 mutex_enter(SD_MUTEX(un)); 11160 /* 11161 * If a device has removable media, we must check if media is 11162 * ready when checking if this device is ready and valid. 11163 */ 11164 if (un->un_f_has_removable_media) { 11165 mutex_exit(SD_MUTEX(un)); 11166 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 11167 rval = SD_NOT_READY_VALID; 11168 mutex_enter(SD_MUTEX(un)); 11169 goto done; 11170 } 11171 11172 mutex_enter(SD_MUTEX(un)); 11173 if ((un->un_f_geometry_is_valid == FALSE) || 11174 (un->un_f_blockcount_is_valid == FALSE) || 11175 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 11176 11177 /* capacity has to be read every open. */ 11178 mutex_exit(SD_MUTEX(un)); 11179 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 11180 &lbasize, SD_PATH_DIRECT) != 0) { 11181 mutex_enter(SD_MUTEX(un)); 11182 un->un_f_geometry_is_valid = FALSE; 11183 rval = SD_NOT_READY_VALID; 11184 goto done; 11185 } else { 11186 mutex_enter(SD_MUTEX(un)); 11187 sd_update_block_info(un, lbasize, capacity); 11188 } 11189 } 11190 11191 /* 11192 * Check if the media in the device is writable or not. 11193 */ 11194 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 11195 sd_check_for_writable_cd(un); 11196 } 11197 11198 } else { 11199 /* 11200 * Do a test unit ready to clear any unit attention from non-cd 11201 * devices. 11202 */ 11203 mutex_exit(SD_MUTEX(un)); 11204 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 11205 mutex_enter(SD_MUTEX(un)); 11206 } 11207 11208 11209 /* 11210 * If this is a non 512 block device, allocate space for 11211 * the wmap cache. This is being done here since every time 11212 * a media is changed this routine will be called and the 11213 * block size is a function of media rather than device. 11214 */ 11215 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 11216 if (!(un->un_wm_cache)) { 11217 (void) snprintf(name_str, sizeof (name_str), 11218 "%s%d_cache", 11219 ddi_driver_name(SD_DEVINFO(un)), 11220 ddi_get_instance(SD_DEVINFO(un))); 11221 un->un_wm_cache = kmem_cache_create( 11222 name_str, sizeof (struct sd_w_map), 11223 8, sd_wm_cache_constructor, 11224 sd_wm_cache_destructor, NULL, 11225 (void *)un, NULL, 0); 11226 if (!(un->un_wm_cache)) { 11227 rval = ENOMEM; 11228 goto done; 11229 } 11230 } 11231 } 11232 11233 if (un->un_state == SD_STATE_NORMAL) { 11234 /* 11235 * If the target is not yet ready here (defined by a TUR 11236 * failure), invalidate the geometry and print an 'offline' 11237 * message. This is a legacy message, as the state of the 11238 * target is not actually changed to SD_STATE_OFFLINE. 11239 * 11240 * If the TUR fails for EACCES (Reservation Conflict), 11241 * SD_RESERVED_BY_OTHERS will be returned to indicate 11242 * reservation conflict. If the TUR fails for other 11243 * reasons, SD_NOT_READY_VALID will be returned. 11244 */ 11245 int err; 11246 11247 mutex_exit(SD_MUTEX(un)); 11248 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 11249 mutex_enter(SD_MUTEX(un)); 11250 11251 if (err != 0) { 11252 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11253 "offline or reservation conflict\n"); 11254 un->un_f_geometry_is_valid = FALSE; 11255 if (err == EACCES) { 11256 rval = SD_RESERVED_BY_OTHERS; 11257 } else { 11258 rval = SD_NOT_READY_VALID; 11259 } 11260 goto done; 11261 } 11262 } 11263 11264 if (un->un_f_format_in_progress == FALSE) { 11265 /* 11266 * Note: sd_validate_geometry may return TRUE, but that does 11267 * not necessarily mean un_f_geometry_is_valid == TRUE! 11268 */ 11269 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 11270 if (rval == ENOTSUP) { 11271 if (un->un_f_geometry_is_valid == TRUE) 11272 rval = 0; 11273 else { 11274 rval = SD_READY_NOT_VALID; 11275 goto done; 11276 } 11277 } 11278 if (rval != 0) { 11279 /* 11280 * We don't check the validity of geometry for 11281 * CDROMs. Also we assume we have a good label 11282 * even if sd_validate_geometry returned ENOMEM. 11283 */ 11284 if (!ISCD(un) && rval != ENOMEM) { 11285 rval = SD_NOT_READY_VALID; 11286 goto done; 11287 } 11288 } 11289 } 11290 11291 /* 11292 * If this device supports DOOR_LOCK command, try and send 11293 * this command to PREVENT MEDIA REMOVAL, but don't get upset 11294 * if it fails. For a CD, however, it is an error 11295 */ 11296 if (un->un_f_doorlock_supported) { 11297 mutex_exit(SD_MUTEX(un)); 11298 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 11299 SD_PATH_DIRECT) != 0) && ISCD(un)) { 11300 rval = SD_NOT_READY_VALID; 11301 mutex_enter(SD_MUTEX(un)); 11302 goto done; 11303 } 11304 mutex_enter(SD_MUTEX(un)); 11305 } 11306 11307 /* The state has changed, inform the media watch routines */ 11308 un->un_mediastate = DKIO_INSERTED; 11309 cv_broadcast(&un->un_state_cv); 11310 rval = SD_READY_VALID; 11311 11312 done: 11313 11314 /* 11315 * Initialize the capacity kstat value, if no media previously 11316 * (capacity kstat is 0) and a media has been inserted 11317 * (un_blockcount > 0). 11318 */ 11319 if (un->un_errstats != NULL) { 11320 stp = (struct sd_errstats *)un->un_errstats->ks_data; 11321 if ((stp->sd_capacity.value.ui64 == 0) && 11322 (un->un_f_blockcount_is_valid == TRUE)) { 11323 stp->sd_capacity.value.ui64 = 11324 (uint64_t)((uint64_t)un->un_blockcount * 11325 un->un_sys_blocksize); 11326 } 11327 } 11328 11329 mutex_exit(SD_MUTEX(un)); 11330 return (rval); 11331 } 11332 11333 11334 /* 11335 * Function: sdmin 11336 * 11337 * Description: Routine to limit the size of a data transfer. Used in 11338 * conjunction with physio(9F). 11339 * 11340 * Arguments: bp - pointer to the indicated buf(9S) struct. 11341 * 11342 * Context: Kernel thread context. 11343 */ 11344 11345 static void 11346 sdmin(struct buf *bp) 11347 { 11348 struct sd_lun *un; 11349 int instance; 11350 11351 instance = SDUNIT(bp->b_edev); 11352 11353 un = ddi_get_soft_state(sd_state, instance); 11354 ASSERT(un != NULL); 11355 11356 if (bp->b_bcount > un->un_max_xfer_size) { 11357 bp->b_bcount = un->un_max_xfer_size; 11358 } 11359 } 11360 11361 11362 /* 11363 * Function: sdread 11364 * 11365 * Description: Driver's read(9e) entry point function. 11366 * 11367 * Arguments: dev - device number 11368 * uio - structure pointer describing where data is to be stored 11369 * in user's space 11370 * cred_p - user credential pointer 11371 * 11372 * Return Code: ENXIO 11373 * EIO 11374 * EINVAL 11375 * value returned by physio 11376 * 11377 * Context: Kernel thread context. 11378 */ 11379 /* ARGSUSED */ 11380 static int 11381 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11382 { 11383 struct sd_lun *un = NULL; 11384 int secmask; 11385 int err; 11386 11387 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11388 return (ENXIO); 11389 } 11390 11391 ASSERT(!mutex_owned(SD_MUTEX(un))); 11392 11393 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11394 mutex_enter(SD_MUTEX(un)); 11395 /* 11396 * Because the call to sd_ready_and_valid will issue I/O we 11397 * must wait here if either the device is suspended or 11398 * if it's power level is changing. 11399 */ 11400 while ((un->un_state == SD_STATE_SUSPENDED) || 11401 (un->un_state == SD_STATE_PM_CHANGING)) { 11402 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11403 } 11404 un->un_ncmds_in_driver++; 11405 mutex_exit(SD_MUTEX(un)); 11406 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11407 mutex_enter(SD_MUTEX(un)); 11408 un->un_ncmds_in_driver--; 11409 ASSERT(un->un_ncmds_in_driver >= 0); 11410 mutex_exit(SD_MUTEX(un)); 11411 return (EIO); 11412 } 11413 mutex_enter(SD_MUTEX(un)); 11414 un->un_ncmds_in_driver--; 11415 ASSERT(un->un_ncmds_in_driver >= 0); 11416 mutex_exit(SD_MUTEX(un)); 11417 } 11418 11419 /* 11420 * Read requests are restricted to multiples of the system block size. 11421 */ 11422 secmask = un->un_sys_blocksize - 1; 11423 11424 if (uio->uio_loffset & ((offset_t)(secmask))) { 11425 SD_ERROR(SD_LOG_READ_WRITE, un, 11426 "sdread: file offset not modulo %d\n", 11427 un->un_sys_blocksize); 11428 err = EINVAL; 11429 } else if (uio->uio_iov->iov_len & (secmask)) { 11430 SD_ERROR(SD_LOG_READ_WRITE, un, 11431 "sdread: transfer length not modulo %d\n", 11432 un->un_sys_blocksize); 11433 err = EINVAL; 11434 } else { 11435 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11436 } 11437 return (err); 11438 } 11439 11440 11441 /* 11442 * Function: sdwrite 11443 * 11444 * Description: Driver's write(9e) entry point function. 11445 * 11446 * Arguments: dev - device number 11447 * uio - structure pointer describing where data is stored in 11448 * user's space 11449 * cred_p - user credential pointer 11450 * 11451 * Return Code: ENXIO 11452 * EIO 11453 * EINVAL 11454 * value returned by physio 11455 * 11456 * Context: Kernel thread context. 11457 */ 11458 /* ARGSUSED */ 11459 static int 11460 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11461 { 11462 struct sd_lun *un = NULL; 11463 int secmask; 11464 int err; 11465 11466 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11467 return (ENXIO); 11468 } 11469 11470 ASSERT(!mutex_owned(SD_MUTEX(un))); 11471 11472 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11473 mutex_enter(SD_MUTEX(un)); 11474 /* 11475 * Because the call to sd_ready_and_valid will issue I/O we 11476 * must wait here if either the device is suspended or 11477 * if it's power level is changing. 11478 */ 11479 while ((un->un_state == SD_STATE_SUSPENDED) || 11480 (un->un_state == SD_STATE_PM_CHANGING)) { 11481 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11482 } 11483 un->un_ncmds_in_driver++; 11484 mutex_exit(SD_MUTEX(un)); 11485 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11486 mutex_enter(SD_MUTEX(un)); 11487 un->un_ncmds_in_driver--; 11488 ASSERT(un->un_ncmds_in_driver >= 0); 11489 mutex_exit(SD_MUTEX(un)); 11490 return (EIO); 11491 } 11492 mutex_enter(SD_MUTEX(un)); 11493 un->un_ncmds_in_driver--; 11494 ASSERT(un->un_ncmds_in_driver >= 0); 11495 mutex_exit(SD_MUTEX(un)); 11496 } 11497 11498 /* 11499 * Write requests are restricted to multiples of the system block size. 11500 */ 11501 secmask = un->un_sys_blocksize - 1; 11502 11503 if (uio->uio_loffset & ((offset_t)(secmask))) { 11504 SD_ERROR(SD_LOG_READ_WRITE, un, 11505 "sdwrite: file offset not modulo %d\n", 11506 un->un_sys_blocksize); 11507 err = EINVAL; 11508 } else if (uio->uio_iov->iov_len & (secmask)) { 11509 SD_ERROR(SD_LOG_READ_WRITE, un, 11510 "sdwrite: transfer length not modulo %d\n", 11511 un->un_sys_blocksize); 11512 err = EINVAL; 11513 } else { 11514 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11515 } 11516 return (err); 11517 } 11518 11519 11520 /* 11521 * Function: sdaread 11522 * 11523 * Description: Driver's aread(9e) entry point function. 11524 * 11525 * Arguments: dev - device number 11526 * aio - structure pointer describing where data is to be stored 11527 * cred_p - user credential pointer 11528 * 11529 * Return Code: ENXIO 11530 * EIO 11531 * EINVAL 11532 * value returned by aphysio 11533 * 11534 * Context: Kernel thread context. 11535 */ 11536 /* ARGSUSED */ 11537 static int 11538 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11539 { 11540 struct sd_lun *un = NULL; 11541 struct uio *uio = aio->aio_uio; 11542 int secmask; 11543 int err; 11544 11545 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11546 return (ENXIO); 11547 } 11548 11549 ASSERT(!mutex_owned(SD_MUTEX(un))); 11550 11551 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11552 mutex_enter(SD_MUTEX(un)); 11553 /* 11554 * Because the call to sd_ready_and_valid will issue I/O we 11555 * must wait here if either the device is suspended or 11556 * if it's power level is changing. 11557 */ 11558 while ((un->un_state == SD_STATE_SUSPENDED) || 11559 (un->un_state == SD_STATE_PM_CHANGING)) { 11560 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11561 } 11562 un->un_ncmds_in_driver++; 11563 mutex_exit(SD_MUTEX(un)); 11564 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11565 mutex_enter(SD_MUTEX(un)); 11566 un->un_ncmds_in_driver--; 11567 ASSERT(un->un_ncmds_in_driver >= 0); 11568 mutex_exit(SD_MUTEX(un)); 11569 return (EIO); 11570 } 11571 mutex_enter(SD_MUTEX(un)); 11572 un->un_ncmds_in_driver--; 11573 ASSERT(un->un_ncmds_in_driver >= 0); 11574 mutex_exit(SD_MUTEX(un)); 11575 } 11576 11577 /* 11578 * Read requests are restricted to multiples of the system block size. 11579 */ 11580 secmask = un->un_sys_blocksize - 1; 11581 11582 if (uio->uio_loffset & ((offset_t)(secmask))) { 11583 SD_ERROR(SD_LOG_READ_WRITE, un, 11584 "sdaread: file offset not modulo %d\n", 11585 un->un_sys_blocksize); 11586 err = EINVAL; 11587 } else if (uio->uio_iov->iov_len & (secmask)) { 11588 SD_ERROR(SD_LOG_READ_WRITE, un, 11589 "sdaread: transfer length not modulo %d\n", 11590 un->un_sys_blocksize); 11591 err = EINVAL; 11592 } else { 11593 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11594 } 11595 return (err); 11596 } 11597 11598 11599 /* 11600 * Function: sdawrite 11601 * 11602 * Description: Driver's awrite(9e) entry point function. 11603 * 11604 * Arguments: dev - device number 11605 * aio - structure pointer describing where data is stored 11606 * cred_p - user credential pointer 11607 * 11608 * Return Code: ENXIO 11609 * EIO 11610 * EINVAL 11611 * value returned by aphysio 11612 * 11613 * Context: Kernel thread context. 11614 */ 11615 /* ARGSUSED */ 11616 static int 11617 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11618 { 11619 struct sd_lun *un = NULL; 11620 struct uio *uio = aio->aio_uio; 11621 int secmask; 11622 int err; 11623 11624 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11625 return (ENXIO); 11626 } 11627 11628 ASSERT(!mutex_owned(SD_MUTEX(un))); 11629 11630 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11631 mutex_enter(SD_MUTEX(un)); 11632 /* 11633 * Because the call to sd_ready_and_valid will issue I/O we 11634 * must wait here if either the device is suspended or 11635 * if it's power level is changing. 11636 */ 11637 while ((un->un_state == SD_STATE_SUSPENDED) || 11638 (un->un_state == SD_STATE_PM_CHANGING)) { 11639 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11640 } 11641 un->un_ncmds_in_driver++; 11642 mutex_exit(SD_MUTEX(un)); 11643 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11644 mutex_enter(SD_MUTEX(un)); 11645 un->un_ncmds_in_driver--; 11646 ASSERT(un->un_ncmds_in_driver >= 0); 11647 mutex_exit(SD_MUTEX(un)); 11648 return (EIO); 11649 } 11650 mutex_enter(SD_MUTEX(un)); 11651 un->un_ncmds_in_driver--; 11652 ASSERT(un->un_ncmds_in_driver >= 0); 11653 mutex_exit(SD_MUTEX(un)); 11654 } 11655 11656 /* 11657 * Write requests are restricted to multiples of the system block size. 11658 */ 11659 secmask = un->un_sys_blocksize - 1; 11660 11661 if (uio->uio_loffset & ((offset_t)(secmask))) { 11662 SD_ERROR(SD_LOG_READ_WRITE, un, 11663 "sdawrite: file offset not modulo %d\n", 11664 un->un_sys_blocksize); 11665 err = EINVAL; 11666 } else if (uio->uio_iov->iov_len & (secmask)) { 11667 SD_ERROR(SD_LOG_READ_WRITE, un, 11668 "sdawrite: transfer length not modulo %d\n", 11669 un->un_sys_blocksize); 11670 err = EINVAL; 11671 } else { 11672 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11673 } 11674 return (err); 11675 } 11676 11677 11678 11679 11680 11681 /* 11682 * Driver IO processing follows the following sequence: 11683 * 11684 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11685 * | | ^ 11686 * v v | 11687 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11688 * | | | | 11689 * v | | | 11690 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11691 * | | ^ ^ 11692 * v v | | 11693 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11694 * | | | | 11695 * +---+ | +------------+ +-------+ 11696 * | | | | 11697 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11698 * | v | | 11699 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11700 * | | ^ | 11701 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11702 * | v | | 11703 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11704 * | | ^ | 11705 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11706 * | v | | 11707 * | sd_checksum_iostart() sd_checksum_iodone() | 11708 * | | ^ | 11709 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11710 * | v | | 11711 * | sd_pm_iostart() sd_pm_iodone() | 11712 * | | ^ | 11713 * | | | | 11714 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11715 * | ^ 11716 * v | 11717 * sd_core_iostart() | 11718 * | | 11719 * | +------>(*destroypkt)() 11720 * +-> sd_start_cmds() <-+ | | 11721 * | | | v 11722 * | | | scsi_destroy_pkt(9F) 11723 * | | | 11724 * +->(*initpkt)() +- sdintr() 11725 * | | | | 11726 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11727 * | +-> scsi_setup_cdb(9F) | 11728 * | | 11729 * +--> scsi_transport(9F) | 11730 * | | 11731 * +----> SCSA ---->+ 11732 * 11733 * 11734 * This code is based upon the following presumtions: 11735 * 11736 * - iostart and iodone functions operate on buf(9S) structures. These 11737 * functions perform the necessary operations on the buf(9S) and pass 11738 * them along to the next function in the chain by using the macros 11739 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11740 * (for iodone side functions). 11741 * 11742 * - The iostart side functions may sleep. The iodone side functions 11743 * are called under interrupt context and may NOT sleep. Therefore 11744 * iodone side functions also may not call iostart side functions. 11745 * (NOTE: iostart side functions should NOT sleep for memory, as 11746 * this could result in deadlock.) 11747 * 11748 * - An iostart side function may call its corresponding iodone side 11749 * function directly (if necessary). 11750 * 11751 * - In the event of an error, an iostart side function can return a buf(9S) 11752 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11753 * b_error in the usual way of course). 11754 * 11755 * - The taskq mechanism may be used by the iodone side functions to dispatch 11756 * requests to the iostart side functions. The iostart side functions in 11757 * this case would be called under the context of a taskq thread, so it's 11758 * OK for them to block/sleep/spin in this case. 11759 * 11760 * - iostart side functions may allocate "shadow" buf(9S) structs and 11761 * pass them along to the next function in the chain. The corresponding 11762 * iodone side functions must coalesce the "shadow" bufs and return 11763 * the "original" buf to the next higher layer. 11764 * 11765 * - The b_private field of the buf(9S) struct holds a pointer to 11766 * an sd_xbuf struct, which contains information needed to 11767 * construct the scsi_pkt for the command. 11768 * 11769 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11770 * layer must acquire & release the SD_MUTEX(un) as needed. 11771 */ 11772 11773 11774 /* 11775 * Create taskq for all targets in the system. This is created at 11776 * _init(9E) and destroyed at _fini(9E). 11777 * 11778 * Note: here we set the minalloc to a reasonably high number to ensure that 11779 * we will have an adequate supply of task entries available at interrupt time. 11780 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11781 * sd_create_taskq(). Since we do not want to sleep for allocations at 11782 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11783 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11784 * requests any one instant in time. 11785 */ 11786 #define SD_TASKQ_NUMTHREADS 8 11787 #define SD_TASKQ_MINALLOC 256 11788 #define SD_TASKQ_MAXALLOC 256 11789 11790 static taskq_t *sd_tq = NULL; 11791 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11792 11793 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11794 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11795 11796 /* 11797 * The following task queue is being created for the write part of 11798 * read-modify-write of non-512 block size devices. 11799 * Limit the number of threads to 1 for now. This number has been choosen 11800 * considering the fact that it applies only to dvd ram drives/MO drives 11801 * currently. Performance for which is not main criteria at this stage. 11802 * Note: It needs to be explored if we can use a single taskq in future 11803 */ 11804 #define SD_WMR_TASKQ_NUMTHREADS 1 11805 static taskq_t *sd_wmr_tq = NULL; 11806 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11807 11808 /* 11809 * Function: sd_taskq_create 11810 * 11811 * Description: Create taskq thread(s) and preallocate task entries 11812 * 11813 * Return Code: Returns a pointer to the allocated taskq_t. 11814 * 11815 * Context: Can sleep. Requires blockable context. 11816 * 11817 * Notes: - The taskq() facility currently is NOT part of the DDI. 11818 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11819 * - taskq_create() will block for memory, also it will panic 11820 * if it cannot create the requested number of threads. 11821 * - Currently taskq_create() creates threads that cannot be 11822 * swapped. 11823 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11824 * supply of taskq entries at interrupt time (ie, so that we 11825 * do not have to sleep for memory) 11826 */ 11827 11828 static void 11829 sd_taskq_create(void) 11830 { 11831 char taskq_name[TASKQ_NAMELEN]; 11832 11833 ASSERT(sd_tq == NULL); 11834 ASSERT(sd_wmr_tq == NULL); 11835 11836 (void) snprintf(taskq_name, sizeof (taskq_name), 11837 "%s_drv_taskq", sd_label); 11838 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11839 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11840 TASKQ_PREPOPULATE)); 11841 11842 (void) snprintf(taskq_name, sizeof (taskq_name), 11843 "%s_rmw_taskq", sd_label); 11844 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11845 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11846 TASKQ_PREPOPULATE)); 11847 } 11848 11849 11850 /* 11851 * Function: sd_taskq_delete 11852 * 11853 * Description: Complementary cleanup routine for sd_taskq_create(). 11854 * 11855 * Context: Kernel thread context. 11856 */ 11857 11858 static void 11859 sd_taskq_delete(void) 11860 { 11861 ASSERT(sd_tq != NULL); 11862 ASSERT(sd_wmr_tq != NULL); 11863 taskq_destroy(sd_tq); 11864 taskq_destroy(sd_wmr_tq); 11865 sd_tq = NULL; 11866 sd_wmr_tq = NULL; 11867 } 11868 11869 11870 /* 11871 * Function: sdstrategy 11872 * 11873 * Description: Driver's strategy (9E) entry point function. 11874 * 11875 * Arguments: bp - pointer to buf(9S) 11876 * 11877 * Return Code: Always returns zero 11878 * 11879 * Context: Kernel thread context. 11880 */ 11881 11882 static int 11883 sdstrategy(struct buf *bp) 11884 { 11885 struct sd_lun *un; 11886 11887 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11888 if (un == NULL) { 11889 bioerror(bp, EIO); 11890 bp->b_resid = bp->b_bcount; 11891 biodone(bp); 11892 return (0); 11893 } 11894 /* As was done in the past, fail new cmds. if state is dumping. */ 11895 if (un->un_state == SD_STATE_DUMPING) { 11896 bioerror(bp, ENXIO); 11897 bp->b_resid = bp->b_bcount; 11898 biodone(bp); 11899 return (0); 11900 } 11901 11902 ASSERT(!mutex_owned(SD_MUTEX(un))); 11903 11904 /* 11905 * Commands may sneak in while we released the mutex in 11906 * DDI_SUSPEND, we should block new commands. However, old 11907 * commands that are still in the driver at this point should 11908 * still be allowed to drain. 11909 */ 11910 mutex_enter(SD_MUTEX(un)); 11911 /* 11912 * Must wait here if either the device is suspended or 11913 * if it's power level is changing. 11914 */ 11915 while ((un->un_state == SD_STATE_SUSPENDED) || 11916 (un->un_state == SD_STATE_PM_CHANGING)) { 11917 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11918 } 11919 11920 un->un_ncmds_in_driver++; 11921 11922 /* 11923 * atapi: Since we are running the CD for now in PIO mode we need to 11924 * call bp_mapin here to avoid bp_mapin called interrupt context under 11925 * the HBA's init_pkt routine. 11926 */ 11927 if (un->un_f_cfg_is_atapi == TRUE) { 11928 mutex_exit(SD_MUTEX(un)); 11929 bp_mapin(bp); 11930 mutex_enter(SD_MUTEX(un)); 11931 } 11932 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11933 un->un_ncmds_in_driver); 11934 11935 mutex_exit(SD_MUTEX(un)); 11936 11937 /* 11938 * This will (eventually) allocate the sd_xbuf area and 11939 * call sd_xbuf_strategy(). We just want to return the 11940 * result of ddi_xbuf_qstrategy so that we have an opt- 11941 * imized tail call which saves us a stack frame. 11942 */ 11943 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11944 } 11945 11946 11947 /* 11948 * Function: sd_xbuf_strategy 11949 * 11950 * Description: Function for initiating IO operations via the 11951 * ddi_xbuf_qstrategy() mechanism. 11952 * 11953 * Context: Kernel thread context. 11954 */ 11955 11956 static void 11957 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11958 { 11959 struct sd_lun *un = arg; 11960 11961 ASSERT(bp != NULL); 11962 ASSERT(xp != NULL); 11963 ASSERT(un != NULL); 11964 ASSERT(!mutex_owned(SD_MUTEX(un))); 11965 11966 /* 11967 * Initialize the fields in the xbuf and save a pointer to the 11968 * xbuf in bp->b_private. 11969 */ 11970 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11971 11972 /* Send the buf down the iostart chain */ 11973 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11974 } 11975 11976 11977 /* 11978 * Function: sd_xbuf_init 11979 * 11980 * Description: Prepare the given sd_xbuf struct for use. 11981 * 11982 * Arguments: un - ptr to softstate 11983 * bp - ptr to associated buf(9S) 11984 * xp - ptr to associated sd_xbuf 11985 * chain_type - IO chain type to use: 11986 * SD_CHAIN_NULL 11987 * SD_CHAIN_BUFIO 11988 * SD_CHAIN_USCSI 11989 * SD_CHAIN_DIRECT 11990 * SD_CHAIN_DIRECT_PRIORITY 11991 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11992 * initialization; may be NULL if none. 11993 * 11994 * Context: Kernel thread context 11995 */ 11996 11997 static void 11998 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11999 uchar_t chain_type, void *pktinfop) 12000 { 12001 int index; 12002 12003 ASSERT(un != NULL); 12004 ASSERT(bp != NULL); 12005 ASSERT(xp != NULL); 12006 12007 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 12008 bp, chain_type); 12009 12010 xp->xb_un = un; 12011 xp->xb_pktp = NULL; 12012 xp->xb_pktinfo = pktinfop; 12013 xp->xb_private = bp->b_private; 12014 xp->xb_blkno = (daddr_t)bp->b_blkno; 12015 12016 /* 12017 * Set up the iostart and iodone chain indexes in the xbuf, based 12018 * upon the specified chain type to use. 12019 */ 12020 switch (chain_type) { 12021 case SD_CHAIN_NULL: 12022 /* 12023 * Fall thru to just use the values for the buf type, even 12024 * tho for the NULL chain these values will never be used. 12025 */ 12026 /* FALLTHRU */ 12027 case SD_CHAIN_BUFIO: 12028 index = un->un_buf_chain_type; 12029 break; 12030 case SD_CHAIN_USCSI: 12031 index = un->un_uscsi_chain_type; 12032 break; 12033 case SD_CHAIN_DIRECT: 12034 index = un->un_direct_chain_type; 12035 break; 12036 case SD_CHAIN_DIRECT_PRIORITY: 12037 index = un->un_priority_chain_type; 12038 break; 12039 default: 12040 /* We're really broken if we ever get here... */ 12041 panic("sd_xbuf_init: illegal chain type!"); 12042 /*NOTREACHED*/ 12043 } 12044 12045 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 12046 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 12047 12048 /* 12049 * It might be a bit easier to simply bzero the entire xbuf above, 12050 * but it turns out that since we init a fair number of members anyway, 12051 * we save a fair number cycles by doing explicit assignment of zero. 12052 */ 12053 xp->xb_pkt_flags = 0; 12054 xp->xb_dma_resid = 0; 12055 xp->xb_retry_count = 0; 12056 xp->xb_victim_retry_count = 0; 12057 xp->xb_ua_retry_count = 0; 12058 xp->xb_sense_bp = NULL; 12059 xp->xb_sense_status = 0; 12060 xp->xb_sense_state = 0; 12061 xp->xb_sense_resid = 0; 12062 12063 bp->b_private = xp; 12064 bp->b_flags &= ~(B_DONE | B_ERROR); 12065 bp->b_resid = 0; 12066 bp->av_forw = NULL; 12067 bp->av_back = NULL; 12068 bioerror(bp, 0); 12069 12070 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 12071 } 12072 12073 12074 /* 12075 * Function: sd_uscsi_strategy 12076 * 12077 * Description: Wrapper for calling into the USCSI chain via physio(9F) 12078 * 12079 * Arguments: bp - buf struct ptr 12080 * 12081 * Return Code: Always returns 0 12082 * 12083 * Context: Kernel thread context 12084 */ 12085 12086 static int 12087 sd_uscsi_strategy(struct buf *bp) 12088 { 12089 struct sd_lun *un; 12090 struct sd_uscsi_info *uip; 12091 struct sd_xbuf *xp; 12092 uchar_t chain_type; 12093 12094 ASSERT(bp != NULL); 12095 12096 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 12097 if (un == NULL) { 12098 bioerror(bp, EIO); 12099 bp->b_resid = bp->b_bcount; 12100 biodone(bp); 12101 return (0); 12102 } 12103 12104 ASSERT(!mutex_owned(SD_MUTEX(un))); 12105 12106 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 12107 12108 mutex_enter(SD_MUTEX(un)); 12109 /* 12110 * atapi: Since we are running the CD for now in PIO mode we need to 12111 * call bp_mapin here to avoid bp_mapin called interrupt context under 12112 * the HBA's init_pkt routine. 12113 */ 12114 if (un->un_f_cfg_is_atapi == TRUE) { 12115 mutex_exit(SD_MUTEX(un)); 12116 bp_mapin(bp); 12117 mutex_enter(SD_MUTEX(un)); 12118 } 12119 un->un_ncmds_in_driver++; 12120 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 12121 un->un_ncmds_in_driver); 12122 mutex_exit(SD_MUTEX(un)); 12123 12124 /* 12125 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 12126 */ 12127 ASSERT(bp->b_private != NULL); 12128 uip = (struct sd_uscsi_info *)bp->b_private; 12129 12130 switch (uip->ui_flags) { 12131 case SD_PATH_DIRECT: 12132 chain_type = SD_CHAIN_DIRECT; 12133 break; 12134 case SD_PATH_DIRECT_PRIORITY: 12135 chain_type = SD_CHAIN_DIRECT_PRIORITY; 12136 break; 12137 default: 12138 chain_type = SD_CHAIN_USCSI; 12139 break; 12140 } 12141 12142 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12143 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 12144 12145 /* Use the index obtained within xbuf_init */ 12146 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 12147 12148 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 12149 12150 return (0); 12151 } 12152 12153 /* 12154 * Function: sd_send_scsi_cmd 12155 * 12156 * Description: Runs a USCSI command for user (when called thru sdioctl), 12157 * or for the driver 12158 * 12159 * Arguments: dev - the dev_t for the device 12160 * incmd - ptr to a valid uscsi_cmd struct 12161 * flag - bit flag, indicating open settings, 32/64 bit type 12162 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 12163 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 12164 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 12165 * to use the USCSI "direct" chain and bypass the normal 12166 * command waitq. 12167 * 12168 * Return Code: 0 - successful completion of the given command 12169 * EIO - scsi_uscsi_handle_command() failed 12170 * ENXIO - soft state not found for specified dev 12171 * EINVAL 12172 * EFAULT - copyin/copyout error 12173 * return code of scsi_uscsi_handle_command(): 12174 * EIO 12175 * ENXIO 12176 * EACCES 12177 * 12178 * Context: Waits for command to complete. Can sleep. 12179 */ 12180 12181 static int 12182 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 12183 enum uio_seg dataspace, int path_flag) 12184 { 12185 struct sd_uscsi_info *uip; 12186 struct uscsi_cmd *uscmd; 12187 struct sd_lun *un; 12188 int format = 0; 12189 int rval; 12190 12191 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 12192 if (un == NULL) { 12193 return (ENXIO); 12194 } 12195 12196 ASSERT(!mutex_owned(SD_MUTEX(un))); 12197 12198 #ifdef SDDEBUG 12199 switch (dataspace) { 12200 case UIO_USERSPACE: 12201 SD_TRACE(SD_LOG_IO, un, 12202 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 12203 break; 12204 case UIO_SYSSPACE: 12205 SD_TRACE(SD_LOG_IO, un, 12206 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 12207 break; 12208 default: 12209 SD_TRACE(SD_LOG_IO, un, 12210 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 12211 break; 12212 } 12213 #endif 12214 12215 rval = scsi_uscsi_alloc_and_copyin((intptr_t)incmd, flag, 12216 SD_ADDRESS(un), &uscmd); 12217 if (rval != 0) { 12218 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 12219 "scsi_uscsi_alloc_and_copyin failed\n", un); 12220 return (rval); 12221 } 12222 12223 if ((uscmd->uscsi_cdb != NULL) && 12224 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 12225 mutex_enter(SD_MUTEX(un)); 12226 un->un_f_format_in_progress = TRUE; 12227 mutex_exit(SD_MUTEX(un)); 12228 format = 1; 12229 } 12230 12231 /* 12232 * Allocate an sd_uscsi_info struct and fill it with the info 12233 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12234 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12235 * since we allocate the buf here in this function, we do not 12236 * need to preserve the prior contents of b_private. 12237 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12238 */ 12239 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 12240 uip->ui_flags = path_flag; 12241 uip->ui_cmdp = uscmd; 12242 12243 /* 12244 * Commands sent with priority are intended for error recovery 12245 * situations, and do not have retries performed. 12246 */ 12247 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12248 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12249 } 12250 uscmd->uscsi_flags &= ~USCSI_NOINTR; 12251 12252 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 12253 sd_uscsi_strategy, NULL, uip); 12254 12255 #ifdef SDDEBUG 12256 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12257 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12258 uscmd->uscsi_status, uscmd->uscsi_resid); 12259 if (uscmd->uscsi_bufaddr != NULL) { 12260 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12261 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12262 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12263 if (dataspace == UIO_SYSSPACE) { 12264 SD_DUMP_MEMORY(un, SD_LOG_IO, 12265 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12266 uscmd->uscsi_buflen, SD_LOG_HEX); 12267 } 12268 } 12269 #endif 12270 12271 if (format == 1) { 12272 mutex_enter(SD_MUTEX(un)); 12273 un->un_f_format_in_progress = FALSE; 12274 mutex_exit(SD_MUTEX(un)); 12275 } 12276 12277 (void) scsi_uscsi_copyout_and_free((intptr_t)incmd, uscmd); 12278 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12279 12280 return (rval); 12281 } 12282 12283 12284 /* 12285 * Function: sd_buf_iodone 12286 * 12287 * Description: Frees the sd_xbuf & returns the buf to its originator. 12288 * 12289 * Context: May be called from interrupt context. 12290 */ 12291 /* ARGSUSED */ 12292 static void 12293 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12294 { 12295 struct sd_xbuf *xp; 12296 12297 ASSERT(un != NULL); 12298 ASSERT(bp != NULL); 12299 ASSERT(!mutex_owned(SD_MUTEX(un))); 12300 12301 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12302 12303 xp = SD_GET_XBUF(bp); 12304 ASSERT(xp != NULL); 12305 12306 mutex_enter(SD_MUTEX(un)); 12307 12308 /* 12309 * Grab time when the cmd completed. 12310 * This is used for determining if the system has been 12311 * idle long enough to make it idle to the PM framework. 12312 * This is for lowering the overhead, and therefore improving 12313 * performance per I/O operation. 12314 */ 12315 un->un_pm_idle_time = ddi_get_time(); 12316 12317 un->un_ncmds_in_driver--; 12318 ASSERT(un->un_ncmds_in_driver >= 0); 12319 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12320 un->un_ncmds_in_driver); 12321 12322 mutex_exit(SD_MUTEX(un)); 12323 12324 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12325 biodone(bp); /* bp is gone after this */ 12326 12327 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12328 } 12329 12330 12331 /* 12332 * Function: sd_uscsi_iodone 12333 * 12334 * Description: Frees the sd_xbuf & returns the buf to its originator. 12335 * 12336 * Context: May be called from interrupt context. 12337 */ 12338 /* ARGSUSED */ 12339 static void 12340 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12341 { 12342 struct sd_xbuf *xp; 12343 12344 ASSERT(un != NULL); 12345 ASSERT(bp != NULL); 12346 12347 xp = SD_GET_XBUF(bp); 12348 ASSERT(xp != NULL); 12349 ASSERT(!mutex_owned(SD_MUTEX(un))); 12350 12351 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12352 12353 bp->b_private = xp->xb_private; 12354 12355 mutex_enter(SD_MUTEX(un)); 12356 12357 /* 12358 * Grab time when the cmd completed. 12359 * This is used for determining if the system has been 12360 * idle long enough to make it idle to the PM framework. 12361 * This is for lowering the overhead, and therefore improving 12362 * performance per I/O operation. 12363 */ 12364 un->un_pm_idle_time = ddi_get_time(); 12365 12366 un->un_ncmds_in_driver--; 12367 ASSERT(un->un_ncmds_in_driver >= 0); 12368 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12369 un->un_ncmds_in_driver); 12370 12371 mutex_exit(SD_MUTEX(un)); 12372 12373 kmem_free(xp, sizeof (struct sd_xbuf)); 12374 biodone(bp); 12375 12376 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12377 } 12378 12379 12380 /* 12381 * Function: sd_mapblockaddr_iostart 12382 * 12383 * Description: Verify request lies withing the partition limits for 12384 * the indicated minor device. Issue "overrun" buf if 12385 * request would exceed partition range. Converts 12386 * partition-relative block address to absolute. 12387 * 12388 * Context: Can sleep 12389 * 12390 * Issues: This follows what the old code did, in terms of accessing 12391 * some of the partition info in the unit struct without holding 12392 * the mutext. This is a general issue, if the partition info 12393 * can be altered while IO is in progress... as soon as we send 12394 * a buf, its partitioning can be invalid before it gets to the 12395 * device. Probably the right fix is to move partitioning out 12396 * of the driver entirely. 12397 */ 12398 12399 static void 12400 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12401 { 12402 daddr_t nblocks; /* #blocks in the given partition */ 12403 daddr_t blocknum; /* Block number specified by the buf */ 12404 size_t requested_nblocks; 12405 size_t available_nblocks; 12406 int partition; 12407 diskaddr_t partition_offset; 12408 struct sd_xbuf *xp; 12409 12410 12411 ASSERT(un != NULL); 12412 ASSERT(bp != NULL); 12413 ASSERT(!mutex_owned(SD_MUTEX(un))); 12414 12415 SD_TRACE(SD_LOG_IO_PARTITION, un, 12416 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12417 12418 xp = SD_GET_XBUF(bp); 12419 ASSERT(xp != NULL); 12420 12421 /* 12422 * If the geometry is not indicated as valid, attempt to access 12423 * the unit & verify the geometry/label. This can be the case for 12424 * removable-media devices, of if the device was opened in 12425 * NDELAY/NONBLOCK mode. 12426 */ 12427 if ((un->un_f_geometry_is_valid != TRUE) && 12428 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12429 /* 12430 * For removable devices it is possible to start an I/O 12431 * without a media by opening the device in nodelay mode. 12432 * Also for writable CDs there can be many scenarios where 12433 * there is no geometry yet but volume manager is trying to 12434 * issue a read() just because it can see TOC on the CD. So 12435 * do not print a message for removables. 12436 */ 12437 if (!un->un_f_has_removable_media) { 12438 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12439 "i/o to invalid geometry\n"); 12440 } 12441 bioerror(bp, EIO); 12442 bp->b_resid = bp->b_bcount; 12443 SD_BEGIN_IODONE(index, un, bp); 12444 return; 12445 } 12446 12447 partition = SDPART(bp->b_edev); 12448 12449 /* #blocks in partition */ 12450 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12451 12452 /* Use of a local variable potentially improves performance slightly */ 12453 partition_offset = un->un_offset[partition]; 12454 12455 /* 12456 * blocknum is the starting block number of the request. At this 12457 * point it is still relative to the start of the minor device. 12458 */ 12459 blocknum = xp->xb_blkno; 12460 12461 /* 12462 * Legacy: If the starting block number is one past the last block 12463 * in the partition, do not set B_ERROR in the buf. 12464 */ 12465 if (blocknum == nblocks) { 12466 goto error_exit; 12467 } 12468 12469 /* 12470 * Confirm that the first block of the request lies within the 12471 * partition limits. Also the requested number of bytes must be 12472 * a multiple of the system block size. 12473 */ 12474 if ((blocknum < 0) || (blocknum >= nblocks) || 12475 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12476 bp->b_flags |= B_ERROR; 12477 goto error_exit; 12478 } 12479 12480 /* 12481 * If the requsted # blocks exceeds the available # blocks, that 12482 * is an overrun of the partition. 12483 */ 12484 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12485 available_nblocks = (size_t)(nblocks - blocknum); 12486 ASSERT(nblocks >= blocknum); 12487 12488 if (requested_nblocks > available_nblocks) { 12489 /* 12490 * Allocate an "overrun" buf to allow the request to proceed 12491 * for the amount of space available in the partition. The 12492 * amount not transferred will be added into the b_resid 12493 * when the operation is complete. The overrun buf 12494 * replaces the original buf here, and the original buf 12495 * is saved inside the overrun buf, for later use. 12496 */ 12497 size_t resid = SD_SYSBLOCKS2BYTES(un, 12498 (offset_t)(requested_nblocks - available_nblocks)); 12499 size_t count = bp->b_bcount - resid; 12500 /* 12501 * Note: count is an unsigned entity thus it'll NEVER 12502 * be less than 0 so ASSERT the original values are 12503 * correct. 12504 */ 12505 ASSERT(bp->b_bcount >= resid); 12506 12507 bp = sd_bioclone_alloc(bp, count, blocknum, 12508 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12509 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12510 ASSERT(xp != NULL); 12511 } 12512 12513 /* At this point there should be no residual for this buf. */ 12514 ASSERT(bp->b_resid == 0); 12515 12516 /* Convert the block number to an absolute address. */ 12517 xp->xb_blkno += partition_offset; 12518 12519 SD_NEXT_IOSTART(index, un, bp); 12520 12521 SD_TRACE(SD_LOG_IO_PARTITION, un, 12522 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12523 12524 return; 12525 12526 error_exit: 12527 bp->b_resid = bp->b_bcount; 12528 SD_BEGIN_IODONE(index, un, bp); 12529 SD_TRACE(SD_LOG_IO_PARTITION, un, 12530 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12531 } 12532 12533 12534 /* 12535 * Function: sd_mapblockaddr_iodone 12536 * 12537 * Description: Completion-side processing for partition management. 12538 * 12539 * Context: May be called under interrupt context 12540 */ 12541 12542 static void 12543 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12544 { 12545 /* int partition; */ /* Not used, see below. */ 12546 ASSERT(un != NULL); 12547 ASSERT(bp != NULL); 12548 ASSERT(!mutex_owned(SD_MUTEX(un))); 12549 12550 SD_TRACE(SD_LOG_IO_PARTITION, un, 12551 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12552 12553 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12554 /* 12555 * We have an "overrun" buf to deal with... 12556 */ 12557 struct sd_xbuf *xp; 12558 struct buf *obp; /* ptr to the original buf */ 12559 12560 xp = SD_GET_XBUF(bp); 12561 ASSERT(xp != NULL); 12562 12563 /* Retrieve the pointer to the original buf */ 12564 obp = (struct buf *)xp->xb_private; 12565 ASSERT(obp != NULL); 12566 12567 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12568 bioerror(obp, bp->b_error); 12569 12570 sd_bioclone_free(bp); 12571 12572 /* 12573 * Get back the original buf. 12574 * Note that since the restoration of xb_blkno below 12575 * was removed, the sd_xbuf is not needed. 12576 */ 12577 bp = obp; 12578 /* 12579 * xp = SD_GET_XBUF(bp); 12580 * ASSERT(xp != NULL); 12581 */ 12582 } 12583 12584 /* 12585 * Convert sd->xb_blkno back to a minor-device relative value. 12586 * Note: this has been commented out, as it is not needed in the 12587 * current implementation of the driver (ie, since this function 12588 * is at the top of the layering chains, so the info will be 12589 * discarded) and it is in the "hot" IO path. 12590 * 12591 * partition = getminor(bp->b_edev) & SDPART_MASK; 12592 * xp->xb_blkno -= un->un_offset[partition]; 12593 */ 12594 12595 SD_NEXT_IODONE(index, un, bp); 12596 12597 SD_TRACE(SD_LOG_IO_PARTITION, un, 12598 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12599 } 12600 12601 12602 /* 12603 * Function: sd_mapblocksize_iostart 12604 * 12605 * Description: Convert between system block size (un->un_sys_blocksize) 12606 * and target block size (un->un_tgt_blocksize). 12607 * 12608 * Context: Can sleep to allocate resources. 12609 * 12610 * Assumptions: A higher layer has already performed any partition validation, 12611 * and converted the xp->xb_blkno to an absolute value relative 12612 * to the start of the device. 12613 * 12614 * It is also assumed that the higher layer has implemented 12615 * an "overrun" mechanism for the case where the request would 12616 * read/write beyond the end of a partition. In this case we 12617 * assume (and ASSERT) that bp->b_resid == 0. 12618 * 12619 * Note: The implementation for this routine assumes the target 12620 * block size remains constant between allocation and transport. 12621 */ 12622 12623 static void 12624 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12625 { 12626 struct sd_mapblocksize_info *bsp; 12627 struct sd_xbuf *xp; 12628 offset_t first_byte; 12629 daddr_t start_block, end_block; 12630 daddr_t request_bytes; 12631 ushort_t is_aligned = FALSE; 12632 12633 ASSERT(un != NULL); 12634 ASSERT(bp != NULL); 12635 ASSERT(!mutex_owned(SD_MUTEX(un))); 12636 ASSERT(bp->b_resid == 0); 12637 12638 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12639 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12640 12641 /* 12642 * For a non-writable CD, a write request is an error 12643 */ 12644 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12645 (un->un_f_mmc_writable_media == FALSE)) { 12646 bioerror(bp, EIO); 12647 bp->b_resid = bp->b_bcount; 12648 SD_BEGIN_IODONE(index, un, bp); 12649 return; 12650 } 12651 12652 /* 12653 * We do not need a shadow buf if the device is using 12654 * un->un_sys_blocksize as its block size or if bcount == 0. 12655 * In this case there is no layer-private data block allocated. 12656 */ 12657 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12658 (bp->b_bcount == 0)) { 12659 goto done; 12660 } 12661 12662 #if defined(__i386) || defined(__amd64) 12663 /* We do not support non-block-aligned transfers for ROD devices */ 12664 ASSERT(!ISROD(un)); 12665 #endif 12666 12667 xp = SD_GET_XBUF(bp); 12668 ASSERT(xp != NULL); 12669 12670 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12671 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12672 un->un_tgt_blocksize, un->un_sys_blocksize); 12673 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12674 "request start block:0x%x\n", xp->xb_blkno); 12675 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12676 "request len:0x%x\n", bp->b_bcount); 12677 12678 /* 12679 * Allocate the layer-private data area for the mapblocksize layer. 12680 * Layers are allowed to use the xp_private member of the sd_xbuf 12681 * struct to store the pointer to their layer-private data block, but 12682 * each layer also has the responsibility of restoring the prior 12683 * contents of xb_private before returning the buf/xbuf to the 12684 * higher layer that sent it. 12685 * 12686 * Here we save the prior contents of xp->xb_private into the 12687 * bsp->mbs_oprivate field of our layer-private data area. This value 12688 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12689 * the layer-private area and returning the buf/xbuf to the layer 12690 * that sent it. 12691 * 12692 * Note that here we use kmem_zalloc for the allocation as there are 12693 * parts of the mapblocksize code that expect certain fields to be 12694 * zero unless explicitly set to a required value. 12695 */ 12696 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12697 bsp->mbs_oprivate = xp->xb_private; 12698 xp->xb_private = bsp; 12699 12700 /* 12701 * This treats the data on the disk (target) as an array of bytes. 12702 * first_byte is the byte offset, from the beginning of the device, 12703 * to the location of the request. This is converted from a 12704 * un->un_sys_blocksize block address to a byte offset, and then back 12705 * to a block address based upon a un->un_tgt_blocksize block size. 12706 * 12707 * xp->xb_blkno should be absolute upon entry into this function, 12708 * but, but it is based upon partitions that use the "system" 12709 * block size. It must be adjusted to reflect the block size of 12710 * the target. 12711 * 12712 * Note that end_block is actually the block that follows the last 12713 * block of the request, but that's what is needed for the computation. 12714 */ 12715 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12716 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12717 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12718 un->un_tgt_blocksize; 12719 12720 /* request_bytes is rounded up to a multiple of the target block size */ 12721 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12722 12723 /* 12724 * See if the starting address of the request and the request 12725 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12726 * then we do not need to allocate a shadow buf to handle the request. 12727 */ 12728 if (((first_byte % un->un_tgt_blocksize) == 0) && 12729 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12730 is_aligned = TRUE; 12731 } 12732 12733 if ((bp->b_flags & B_READ) == 0) { 12734 /* 12735 * Lock the range for a write operation. An aligned request is 12736 * considered a simple write; otherwise the request must be a 12737 * read-modify-write. 12738 */ 12739 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12740 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12741 } 12742 12743 /* 12744 * Alloc a shadow buf if the request is not aligned. Also, this is 12745 * where the READ command is generated for a read-modify-write. (The 12746 * write phase is deferred until after the read completes.) 12747 */ 12748 if (is_aligned == FALSE) { 12749 12750 struct sd_mapblocksize_info *shadow_bsp; 12751 struct sd_xbuf *shadow_xp; 12752 struct buf *shadow_bp; 12753 12754 /* 12755 * Allocate the shadow buf and it associated xbuf. Note that 12756 * after this call the xb_blkno value in both the original 12757 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12758 * same: absolute relative to the start of the device, and 12759 * adjusted for the target block size. The b_blkno in the 12760 * shadow buf will also be set to this value. We should never 12761 * change b_blkno in the original bp however. 12762 * 12763 * Note also that the shadow buf will always need to be a 12764 * READ command, regardless of whether the incoming command 12765 * is a READ or a WRITE. 12766 */ 12767 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12768 xp->xb_blkno, 12769 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12770 12771 shadow_xp = SD_GET_XBUF(shadow_bp); 12772 12773 /* 12774 * Allocate the layer-private data for the shadow buf. 12775 * (No need to preserve xb_private in the shadow xbuf.) 12776 */ 12777 shadow_xp->xb_private = shadow_bsp = 12778 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12779 12780 /* 12781 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12782 * to figure out where the start of the user data is (based upon 12783 * the system block size) in the data returned by the READ 12784 * command (which will be based upon the target blocksize). Note 12785 * that this is only really used if the request is unaligned. 12786 */ 12787 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12788 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12789 ASSERT((bsp->mbs_copy_offset >= 0) && 12790 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12791 12792 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12793 12794 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12795 12796 /* Transfer the wmap (if any) to the shadow buf */ 12797 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12798 bsp->mbs_wmp = NULL; 12799 12800 /* 12801 * The shadow buf goes on from here in place of the 12802 * original buf. 12803 */ 12804 shadow_bsp->mbs_orig_bp = bp; 12805 bp = shadow_bp; 12806 } 12807 12808 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12809 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12810 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12811 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12812 request_bytes); 12813 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12814 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12815 12816 done: 12817 SD_NEXT_IOSTART(index, un, bp); 12818 12819 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12820 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12821 } 12822 12823 12824 /* 12825 * Function: sd_mapblocksize_iodone 12826 * 12827 * Description: Completion side processing for block-size mapping. 12828 * 12829 * Context: May be called under interrupt context 12830 */ 12831 12832 static void 12833 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12834 { 12835 struct sd_mapblocksize_info *bsp; 12836 struct sd_xbuf *xp; 12837 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12838 struct buf *orig_bp; /* ptr to the original buf */ 12839 offset_t shadow_end; 12840 offset_t request_end; 12841 offset_t shadow_start; 12842 ssize_t copy_offset; 12843 size_t copy_length; 12844 size_t shortfall; 12845 uint_t is_write; /* TRUE if this bp is a WRITE */ 12846 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12847 12848 ASSERT(un != NULL); 12849 ASSERT(bp != NULL); 12850 12851 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12852 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12853 12854 /* 12855 * There is no shadow buf or layer-private data if the target is 12856 * using un->un_sys_blocksize as its block size or if bcount == 0. 12857 */ 12858 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12859 (bp->b_bcount == 0)) { 12860 goto exit; 12861 } 12862 12863 xp = SD_GET_XBUF(bp); 12864 ASSERT(xp != NULL); 12865 12866 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12867 bsp = xp->xb_private; 12868 12869 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12870 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12871 12872 if (is_write) { 12873 /* 12874 * For a WRITE request we must free up the block range that 12875 * we have locked up. This holds regardless of whether this is 12876 * an aligned write request or a read-modify-write request. 12877 */ 12878 sd_range_unlock(un, bsp->mbs_wmp); 12879 bsp->mbs_wmp = NULL; 12880 } 12881 12882 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12883 /* 12884 * An aligned read or write command will have no shadow buf; 12885 * there is not much else to do with it. 12886 */ 12887 goto done; 12888 } 12889 12890 orig_bp = bsp->mbs_orig_bp; 12891 ASSERT(orig_bp != NULL); 12892 orig_xp = SD_GET_XBUF(orig_bp); 12893 ASSERT(orig_xp != NULL); 12894 ASSERT(!mutex_owned(SD_MUTEX(un))); 12895 12896 if (!is_write && has_wmap) { 12897 /* 12898 * A READ with a wmap means this is the READ phase of a 12899 * read-modify-write. If an error occurred on the READ then 12900 * we do not proceed with the WRITE phase or copy any data. 12901 * Just release the write maps and return with an error. 12902 */ 12903 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12904 orig_bp->b_resid = orig_bp->b_bcount; 12905 bioerror(orig_bp, bp->b_error); 12906 sd_range_unlock(un, bsp->mbs_wmp); 12907 goto freebuf_done; 12908 } 12909 } 12910 12911 /* 12912 * Here is where we set up to copy the data from the shadow buf 12913 * into the space associated with the original buf. 12914 * 12915 * To deal with the conversion between block sizes, these 12916 * computations treat the data as an array of bytes, with the 12917 * first byte (byte 0) corresponding to the first byte in the 12918 * first block on the disk. 12919 */ 12920 12921 /* 12922 * shadow_start and shadow_len indicate the location and size of 12923 * the data returned with the shadow IO request. 12924 */ 12925 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12926 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12927 12928 /* 12929 * copy_offset gives the offset (in bytes) from the start of the first 12930 * block of the READ request to the beginning of the data. We retrieve 12931 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12932 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12933 * data to be copied (in bytes). 12934 */ 12935 copy_offset = bsp->mbs_copy_offset; 12936 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12937 copy_length = orig_bp->b_bcount; 12938 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12939 12940 /* 12941 * Set up the resid and error fields of orig_bp as appropriate. 12942 */ 12943 if (shadow_end >= request_end) { 12944 /* We got all the requested data; set resid to zero */ 12945 orig_bp->b_resid = 0; 12946 } else { 12947 /* 12948 * We failed to get enough data to fully satisfy the original 12949 * request. Just copy back whatever data we got and set 12950 * up the residual and error code as required. 12951 * 12952 * 'shortfall' is the amount by which the data received with the 12953 * shadow buf has "fallen short" of the requested amount. 12954 */ 12955 shortfall = (size_t)(request_end - shadow_end); 12956 12957 if (shortfall > orig_bp->b_bcount) { 12958 /* 12959 * We did not get enough data to even partially 12960 * fulfill the original request. The residual is 12961 * equal to the amount requested. 12962 */ 12963 orig_bp->b_resid = orig_bp->b_bcount; 12964 } else { 12965 /* 12966 * We did not get all the data that we requested 12967 * from the device, but we will try to return what 12968 * portion we did get. 12969 */ 12970 orig_bp->b_resid = shortfall; 12971 } 12972 ASSERT(copy_length >= orig_bp->b_resid); 12973 copy_length -= orig_bp->b_resid; 12974 } 12975 12976 /* Propagate the error code from the shadow buf to the original buf */ 12977 bioerror(orig_bp, bp->b_error); 12978 12979 if (is_write) { 12980 goto freebuf_done; /* No data copying for a WRITE */ 12981 } 12982 12983 if (has_wmap) { 12984 /* 12985 * This is a READ command from the READ phase of a 12986 * read-modify-write request. We have to copy the data given 12987 * by the user OVER the data returned by the READ command, 12988 * then convert the command from a READ to a WRITE and send 12989 * it back to the target. 12990 */ 12991 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12992 copy_length); 12993 12994 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12995 12996 /* 12997 * Dispatch the WRITE command to the taskq thread, which 12998 * will in turn send the command to the target. When the 12999 * WRITE command completes, we (sd_mapblocksize_iodone()) 13000 * will get called again as part of the iodone chain 13001 * processing for it. Note that we will still be dealing 13002 * with the shadow buf at that point. 13003 */ 13004 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13005 KM_NOSLEEP) != 0) { 13006 /* 13007 * Dispatch was successful so we are done. Return 13008 * without going any higher up the iodone chain. Do 13009 * not free up any layer-private data until after the 13010 * WRITE completes. 13011 */ 13012 return; 13013 } 13014 13015 /* 13016 * Dispatch of the WRITE command failed; set up the error 13017 * condition and send this IO back up the iodone chain. 13018 */ 13019 bioerror(orig_bp, EIO); 13020 orig_bp->b_resid = orig_bp->b_bcount; 13021 13022 } else { 13023 /* 13024 * This is a regular READ request (ie, not a RMW). Copy the 13025 * data from the shadow buf into the original buf. The 13026 * copy_offset compensates for any "misalignment" between the 13027 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13028 * original buf (with its un->un_sys_blocksize blocks). 13029 */ 13030 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13031 copy_length); 13032 } 13033 13034 freebuf_done: 13035 13036 /* 13037 * At this point we still have both the shadow buf AND the original 13038 * buf to deal with, as well as the layer-private data area in each. 13039 * Local variables are as follows: 13040 * 13041 * bp -- points to shadow buf 13042 * xp -- points to xbuf of shadow buf 13043 * bsp -- points to layer-private data area of shadow buf 13044 * orig_bp -- points to original buf 13045 * 13046 * First free the shadow buf and its associated xbuf, then free the 13047 * layer-private data area from the shadow buf. There is no need to 13048 * restore xb_private in the shadow xbuf. 13049 */ 13050 sd_shadow_buf_free(bp); 13051 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13052 13053 /* 13054 * Now update the local variables to point to the original buf, xbuf, 13055 * and layer-private area. 13056 */ 13057 bp = orig_bp; 13058 xp = SD_GET_XBUF(bp); 13059 ASSERT(xp != NULL); 13060 ASSERT(xp == orig_xp); 13061 bsp = xp->xb_private; 13062 ASSERT(bsp != NULL); 13063 13064 done: 13065 /* 13066 * Restore xb_private to whatever it was set to by the next higher 13067 * layer in the chain, then free the layer-private data area. 13068 */ 13069 xp->xb_private = bsp->mbs_oprivate; 13070 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13071 13072 exit: 13073 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13074 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13075 13076 SD_NEXT_IODONE(index, un, bp); 13077 } 13078 13079 13080 /* 13081 * Function: sd_checksum_iostart 13082 * 13083 * Description: A stub function for a layer that's currently not used. 13084 * For now just a placeholder. 13085 * 13086 * Context: Kernel thread context 13087 */ 13088 13089 static void 13090 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13091 { 13092 ASSERT(un != NULL); 13093 ASSERT(bp != NULL); 13094 ASSERT(!mutex_owned(SD_MUTEX(un))); 13095 SD_NEXT_IOSTART(index, un, bp); 13096 } 13097 13098 13099 /* 13100 * Function: sd_checksum_iodone 13101 * 13102 * Description: A stub function for a layer that's currently not used. 13103 * For now just a placeholder. 13104 * 13105 * Context: May be called under interrupt context 13106 */ 13107 13108 static void 13109 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13110 { 13111 ASSERT(un != NULL); 13112 ASSERT(bp != NULL); 13113 ASSERT(!mutex_owned(SD_MUTEX(un))); 13114 SD_NEXT_IODONE(index, un, bp); 13115 } 13116 13117 13118 /* 13119 * Function: sd_checksum_uscsi_iostart 13120 * 13121 * Description: A stub function for a layer that's currently not used. 13122 * For now just a placeholder. 13123 * 13124 * Context: Kernel thread context 13125 */ 13126 13127 static void 13128 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13129 { 13130 ASSERT(un != NULL); 13131 ASSERT(bp != NULL); 13132 ASSERT(!mutex_owned(SD_MUTEX(un))); 13133 SD_NEXT_IOSTART(index, un, bp); 13134 } 13135 13136 13137 /* 13138 * Function: sd_checksum_uscsi_iodone 13139 * 13140 * Description: A stub function for a layer that's currently not used. 13141 * For now just a placeholder. 13142 * 13143 * Context: May be called under interrupt context 13144 */ 13145 13146 static void 13147 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13148 { 13149 ASSERT(un != NULL); 13150 ASSERT(bp != NULL); 13151 ASSERT(!mutex_owned(SD_MUTEX(un))); 13152 SD_NEXT_IODONE(index, un, bp); 13153 } 13154 13155 13156 /* 13157 * Function: sd_pm_iostart 13158 * 13159 * Description: iostart-side routine for Power mangement. 13160 * 13161 * Context: Kernel thread context 13162 */ 13163 13164 static void 13165 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13166 { 13167 ASSERT(un != NULL); 13168 ASSERT(bp != NULL); 13169 ASSERT(!mutex_owned(SD_MUTEX(un))); 13170 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13171 13172 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13173 13174 if (sd_pm_entry(un) != DDI_SUCCESS) { 13175 /* 13176 * Set up to return the failed buf back up the 'iodone' 13177 * side of the calling chain. 13178 */ 13179 bioerror(bp, EIO); 13180 bp->b_resid = bp->b_bcount; 13181 13182 SD_BEGIN_IODONE(index, un, bp); 13183 13184 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13185 return; 13186 } 13187 13188 SD_NEXT_IOSTART(index, un, bp); 13189 13190 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13191 } 13192 13193 13194 /* 13195 * Function: sd_pm_iodone 13196 * 13197 * Description: iodone-side routine for power mangement. 13198 * 13199 * Context: may be called from interrupt context 13200 */ 13201 13202 static void 13203 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13204 { 13205 ASSERT(un != NULL); 13206 ASSERT(bp != NULL); 13207 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13208 13209 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13210 13211 /* 13212 * After attach the following flag is only read, so don't 13213 * take the penalty of acquiring a mutex for it. 13214 */ 13215 if (un->un_f_pm_is_enabled == TRUE) { 13216 sd_pm_exit(un); 13217 } 13218 13219 SD_NEXT_IODONE(index, un, bp); 13220 13221 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13222 } 13223 13224 13225 /* 13226 * Function: sd_core_iostart 13227 * 13228 * Description: Primary driver function for enqueuing buf(9S) structs from 13229 * the system and initiating IO to the target device 13230 * 13231 * Context: Kernel thread context. Can sleep. 13232 * 13233 * Assumptions: - The given xp->xb_blkno is absolute 13234 * (ie, relative to the start of the device). 13235 * - The IO is to be done using the native blocksize of 13236 * the device, as specified in un->un_tgt_blocksize. 13237 */ 13238 /* ARGSUSED */ 13239 static void 13240 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13241 { 13242 struct sd_xbuf *xp; 13243 13244 ASSERT(un != NULL); 13245 ASSERT(bp != NULL); 13246 ASSERT(!mutex_owned(SD_MUTEX(un))); 13247 ASSERT(bp->b_resid == 0); 13248 13249 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13250 13251 xp = SD_GET_XBUF(bp); 13252 ASSERT(xp != NULL); 13253 13254 mutex_enter(SD_MUTEX(un)); 13255 13256 /* 13257 * If we are currently in the failfast state, fail any new IO 13258 * that has B_FAILFAST set, then return. 13259 */ 13260 if ((bp->b_flags & B_FAILFAST) && 13261 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13262 mutex_exit(SD_MUTEX(un)); 13263 bioerror(bp, EIO); 13264 bp->b_resid = bp->b_bcount; 13265 SD_BEGIN_IODONE(index, un, bp); 13266 return; 13267 } 13268 13269 if (SD_IS_DIRECT_PRIORITY(xp)) { 13270 /* 13271 * Priority command -- transport it immediately. 13272 * 13273 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13274 * because all direct priority commands should be associated 13275 * with error recovery actions which we don't want to retry. 13276 */ 13277 sd_start_cmds(un, bp); 13278 } else { 13279 /* 13280 * Normal command -- add it to the wait queue, then start 13281 * transporting commands from the wait queue. 13282 */ 13283 sd_add_buf_to_waitq(un, bp); 13284 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13285 sd_start_cmds(un, NULL); 13286 } 13287 13288 mutex_exit(SD_MUTEX(un)); 13289 13290 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13291 } 13292 13293 13294 /* 13295 * Function: sd_init_cdb_limits 13296 * 13297 * Description: This is to handle scsi_pkt initialization differences 13298 * between the driver platforms. 13299 * 13300 * Legacy behaviors: 13301 * 13302 * If the block number or the sector count exceeds the 13303 * capabilities of a Group 0 command, shift over to a 13304 * Group 1 command. We don't blindly use Group 1 13305 * commands because a) some drives (CDC Wren IVs) get a 13306 * bit confused, and b) there is probably a fair amount 13307 * of speed difference for a target to receive and decode 13308 * a 10 byte command instead of a 6 byte command. 13309 * 13310 * The xfer time difference of 6 vs 10 byte CDBs is 13311 * still significant so this code is still worthwhile. 13312 * 10 byte CDBs are very inefficient with the fas HBA driver 13313 * and older disks. Each CDB byte took 1 usec with some 13314 * popular disks. 13315 * 13316 * Context: Must be called at attach time 13317 */ 13318 13319 static void 13320 sd_init_cdb_limits(struct sd_lun *un) 13321 { 13322 int hba_cdb_limit; 13323 13324 /* 13325 * Use CDB_GROUP1 commands for most devices except for 13326 * parallel SCSI fixed drives in which case we get better 13327 * performance using CDB_GROUP0 commands (where applicable). 13328 */ 13329 un->un_mincdb = SD_CDB_GROUP1; 13330 #if !defined(__fibre) 13331 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13332 !un->un_f_has_removable_media) { 13333 un->un_mincdb = SD_CDB_GROUP0; 13334 } 13335 #endif 13336 13337 /* 13338 * Try to read the max-cdb-length supported by HBA. 13339 */ 13340 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13341 if (0 >= un->un_max_hba_cdb) { 13342 un->un_max_hba_cdb = CDB_GROUP4; 13343 hba_cdb_limit = SD_CDB_GROUP4; 13344 } else if (0 < un->un_max_hba_cdb && 13345 un->un_max_hba_cdb < CDB_GROUP1) { 13346 hba_cdb_limit = SD_CDB_GROUP0; 13347 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13348 un->un_max_hba_cdb < CDB_GROUP5) { 13349 hba_cdb_limit = SD_CDB_GROUP1; 13350 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13351 un->un_max_hba_cdb < CDB_GROUP4) { 13352 hba_cdb_limit = SD_CDB_GROUP5; 13353 } else { 13354 hba_cdb_limit = SD_CDB_GROUP4; 13355 } 13356 13357 /* 13358 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13359 * commands for fixed disks unless we are building for a 32 bit 13360 * kernel. 13361 */ 13362 #ifdef _LP64 13363 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13364 min(hba_cdb_limit, SD_CDB_GROUP4); 13365 #else 13366 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13367 min(hba_cdb_limit, SD_CDB_GROUP1); 13368 #endif 13369 13370 /* 13371 * x86 systems require the PKT_DMA_PARTIAL flag 13372 */ 13373 #if defined(__x86) 13374 un->un_pkt_flags = PKT_DMA_PARTIAL; 13375 #else 13376 un->un_pkt_flags = 0; 13377 #endif 13378 13379 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13380 ? sizeof (struct scsi_arq_status) : 1); 13381 un->un_cmd_timeout = (ushort_t)sd_io_time; 13382 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13383 } 13384 13385 13386 /* 13387 * Function: sd_initpkt_for_buf 13388 * 13389 * Description: Allocate and initialize for transport a scsi_pkt struct, 13390 * based upon the info specified in the given buf struct. 13391 * 13392 * Assumes the xb_blkno in the request is absolute (ie, 13393 * relative to the start of the device (NOT partition!). 13394 * Also assumes that the request is using the native block 13395 * size of the device (as returned by the READ CAPACITY 13396 * command). 13397 * 13398 * Return Code: SD_PKT_ALLOC_SUCCESS 13399 * SD_PKT_ALLOC_FAILURE 13400 * SD_PKT_ALLOC_FAILURE_NO_DMA 13401 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13402 * 13403 * Context: Kernel thread and may be called from software interrupt context 13404 * as part of a sdrunout callback. This function may not block or 13405 * call routines that block 13406 */ 13407 13408 static int 13409 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13410 { 13411 struct sd_xbuf *xp; 13412 struct scsi_pkt *pktp = NULL; 13413 struct sd_lun *un; 13414 size_t blockcount; 13415 daddr_t startblock; 13416 int rval; 13417 int cmd_flags; 13418 13419 ASSERT(bp != NULL); 13420 ASSERT(pktpp != NULL); 13421 xp = SD_GET_XBUF(bp); 13422 ASSERT(xp != NULL); 13423 un = SD_GET_UN(bp); 13424 ASSERT(un != NULL); 13425 ASSERT(mutex_owned(SD_MUTEX(un))); 13426 ASSERT(bp->b_resid == 0); 13427 13428 SD_TRACE(SD_LOG_IO_CORE, un, 13429 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13430 13431 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13432 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13433 /* 13434 * Already have a scsi_pkt -- just need DMA resources. 13435 * We must recompute the CDB in case the mapping returns 13436 * a nonzero pkt_resid. 13437 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13438 * that is being retried, the unmap/remap of the DMA resouces 13439 * will result in the entire transfer starting over again 13440 * from the very first block. 13441 */ 13442 ASSERT(xp->xb_pktp != NULL); 13443 pktp = xp->xb_pktp; 13444 } else { 13445 pktp = NULL; 13446 } 13447 #endif /* __i386 || __amd64 */ 13448 13449 startblock = xp->xb_blkno; /* Absolute block num. */ 13450 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13451 13452 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13453 13454 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13455 13456 #else 13457 13458 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13459 13460 #endif 13461 13462 /* 13463 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13464 * call scsi_init_pkt, and build the CDB. 13465 */ 13466 rval = sd_setup_rw_pkt(un, &pktp, bp, 13467 cmd_flags, sdrunout, (caddr_t)un, 13468 startblock, blockcount); 13469 13470 if (rval == 0) { 13471 /* 13472 * Success. 13473 * 13474 * If partial DMA is being used and required for this transfer. 13475 * set it up here. 13476 */ 13477 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13478 (pktp->pkt_resid != 0)) { 13479 13480 /* 13481 * Save the CDB length and pkt_resid for the 13482 * next xfer 13483 */ 13484 xp->xb_dma_resid = pktp->pkt_resid; 13485 13486 /* rezero resid */ 13487 pktp->pkt_resid = 0; 13488 13489 } else { 13490 xp->xb_dma_resid = 0; 13491 } 13492 13493 pktp->pkt_flags = un->un_tagflags; 13494 pktp->pkt_time = un->un_cmd_timeout; 13495 pktp->pkt_comp = sdintr; 13496 13497 pktp->pkt_private = bp; 13498 *pktpp = pktp; 13499 13500 SD_TRACE(SD_LOG_IO_CORE, un, 13501 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13502 13503 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13504 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13505 #endif 13506 13507 return (SD_PKT_ALLOC_SUCCESS); 13508 13509 } 13510 13511 /* 13512 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13513 * from sd_setup_rw_pkt. 13514 */ 13515 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13516 13517 if (rval == SD_PKT_ALLOC_FAILURE) { 13518 *pktpp = NULL; 13519 /* 13520 * Set the driver state to RWAIT to indicate the driver 13521 * is waiting on resource allocations. The driver will not 13522 * suspend, pm_suspend, or detatch while the state is RWAIT. 13523 */ 13524 New_state(un, SD_STATE_RWAIT); 13525 13526 SD_ERROR(SD_LOG_IO_CORE, un, 13527 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13528 13529 if ((bp->b_flags & B_ERROR) != 0) { 13530 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13531 } 13532 return (SD_PKT_ALLOC_FAILURE); 13533 } else { 13534 /* 13535 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13536 * 13537 * This should never happen. Maybe someone messed with the 13538 * kernel's minphys? 13539 */ 13540 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13541 "Request rejected: too large for CDB: " 13542 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13543 SD_ERROR(SD_LOG_IO_CORE, un, 13544 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13545 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13546 13547 } 13548 } 13549 13550 13551 /* 13552 * Function: sd_destroypkt_for_buf 13553 * 13554 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13555 * 13556 * Context: Kernel thread or interrupt context 13557 */ 13558 13559 static void 13560 sd_destroypkt_for_buf(struct buf *bp) 13561 { 13562 ASSERT(bp != NULL); 13563 ASSERT(SD_GET_UN(bp) != NULL); 13564 13565 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13566 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13567 13568 ASSERT(SD_GET_PKTP(bp) != NULL); 13569 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13570 13571 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13572 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13573 } 13574 13575 /* 13576 * Function: sd_setup_rw_pkt 13577 * 13578 * Description: Determines appropriate CDB group for the requested LBA 13579 * and transfer length, calls scsi_init_pkt, and builds 13580 * the CDB. Do not use for partial DMA transfers except 13581 * for the initial transfer since the CDB size must 13582 * remain constant. 13583 * 13584 * Context: Kernel thread and may be called from software interrupt 13585 * context as part of a sdrunout callback. This function may not 13586 * block or call routines that block 13587 */ 13588 13589 13590 int 13591 sd_setup_rw_pkt(struct sd_lun *un, 13592 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13593 int (*callback)(caddr_t), caddr_t callback_arg, 13594 diskaddr_t lba, uint32_t blockcount) 13595 { 13596 struct scsi_pkt *return_pktp; 13597 union scsi_cdb *cdbp; 13598 struct sd_cdbinfo *cp = NULL; 13599 int i; 13600 13601 /* 13602 * See which size CDB to use, based upon the request. 13603 */ 13604 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13605 13606 /* 13607 * Check lba and block count against sd_cdbtab limits. 13608 * In the partial DMA case, we have to use the same size 13609 * CDB for all the transfers. Check lba + blockcount 13610 * against the max LBA so we know that segment of the 13611 * transfer can use the CDB we select. 13612 */ 13613 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13614 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13615 13616 /* 13617 * The command will fit into the CDB type 13618 * specified by sd_cdbtab[i]. 13619 */ 13620 cp = sd_cdbtab + i; 13621 13622 /* 13623 * Call scsi_init_pkt so we can fill in the 13624 * CDB. 13625 */ 13626 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13627 bp, cp->sc_grpcode, un->un_status_len, 0, 13628 flags, callback, callback_arg); 13629 13630 if (return_pktp != NULL) { 13631 13632 /* 13633 * Return new value of pkt 13634 */ 13635 *pktpp = return_pktp; 13636 13637 /* 13638 * To be safe, zero the CDB insuring there is 13639 * no leftover data from a previous command. 13640 */ 13641 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13642 13643 /* 13644 * Handle partial DMA mapping 13645 */ 13646 if (return_pktp->pkt_resid != 0) { 13647 13648 /* 13649 * Not going to xfer as many blocks as 13650 * originally expected 13651 */ 13652 blockcount -= 13653 SD_BYTES2TGTBLOCKS(un, 13654 return_pktp->pkt_resid); 13655 } 13656 13657 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13658 13659 /* 13660 * Set command byte based on the CDB 13661 * type we matched. 13662 */ 13663 cdbp->scc_cmd = cp->sc_grpmask | 13664 ((bp->b_flags & B_READ) ? 13665 SCMD_READ : SCMD_WRITE); 13666 13667 SD_FILL_SCSI1_LUN(un, return_pktp); 13668 13669 /* 13670 * Fill in LBA and length 13671 */ 13672 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13673 (cp->sc_grpcode == CDB_GROUP4) || 13674 (cp->sc_grpcode == CDB_GROUP0) || 13675 (cp->sc_grpcode == CDB_GROUP5)); 13676 13677 if (cp->sc_grpcode == CDB_GROUP1) { 13678 FORMG1ADDR(cdbp, lba); 13679 FORMG1COUNT(cdbp, blockcount); 13680 return (0); 13681 } else if (cp->sc_grpcode == CDB_GROUP4) { 13682 FORMG4LONGADDR(cdbp, lba); 13683 FORMG4COUNT(cdbp, blockcount); 13684 return (0); 13685 } else if (cp->sc_grpcode == CDB_GROUP0) { 13686 FORMG0ADDR(cdbp, lba); 13687 FORMG0COUNT(cdbp, blockcount); 13688 return (0); 13689 } else if (cp->sc_grpcode == CDB_GROUP5) { 13690 FORMG5ADDR(cdbp, lba); 13691 FORMG5COUNT(cdbp, blockcount); 13692 return (0); 13693 } 13694 13695 /* 13696 * It should be impossible to not match one 13697 * of the CDB types above, so we should never 13698 * reach this point. Set the CDB command byte 13699 * to test-unit-ready to avoid writing 13700 * to somewhere we don't intend. 13701 */ 13702 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13703 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13704 } else { 13705 /* 13706 * Couldn't get scsi_pkt 13707 */ 13708 return (SD_PKT_ALLOC_FAILURE); 13709 } 13710 } 13711 } 13712 13713 /* 13714 * None of the available CDB types were suitable. This really 13715 * should never happen: on a 64 bit system we support 13716 * READ16/WRITE16 which will hold an entire 64 bit disk address 13717 * and on a 32 bit system we will refuse to bind to a device 13718 * larger than 2TB so addresses will never be larger than 32 bits. 13719 */ 13720 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13721 } 13722 13723 #if defined(__i386) || defined(__amd64) 13724 /* 13725 * Function: sd_setup_next_rw_pkt 13726 * 13727 * Description: Setup packet for partial DMA transfers, except for the 13728 * initial transfer. sd_setup_rw_pkt should be used for 13729 * the initial transfer. 13730 * 13731 * Context: Kernel thread and may be called from interrupt context. 13732 */ 13733 13734 int 13735 sd_setup_next_rw_pkt(struct sd_lun *un, 13736 struct scsi_pkt *pktp, struct buf *bp, 13737 diskaddr_t lba, uint32_t blockcount) 13738 { 13739 uchar_t com; 13740 union scsi_cdb *cdbp; 13741 uchar_t cdb_group_id; 13742 13743 ASSERT(pktp != NULL); 13744 ASSERT(pktp->pkt_cdbp != NULL); 13745 13746 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13747 com = cdbp->scc_cmd; 13748 cdb_group_id = CDB_GROUPID(com); 13749 13750 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13751 (cdb_group_id == CDB_GROUPID_1) || 13752 (cdb_group_id == CDB_GROUPID_4) || 13753 (cdb_group_id == CDB_GROUPID_5)); 13754 13755 /* 13756 * Move pkt to the next portion of the xfer. 13757 * func is NULL_FUNC so we do not have to release 13758 * the disk mutex here. 13759 */ 13760 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13761 NULL_FUNC, NULL) == pktp) { 13762 /* Success. Handle partial DMA */ 13763 if (pktp->pkt_resid != 0) { 13764 blockcount -= 13765 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13766 } 13767 13768 cdbp->scc_cmd = com; 13769 SD_FILL_SCSI1_LUN(un, pktp); 13770 if (cdb_group_id == CDB_GROUPID_1) { 13771 FORMG1ADDR(cdbp, lba); 13772 FORMG1COUNT(cdbp, blockcount); 13773 return (0); 13774 } else if (cdb_group_id == CDB_GROUPID_4) { 13775 FORMG4LONGADDR(cdbp, lba); 13776 FORMG4COUNT(cdbp, blockcount); 13777 return (0); 13778 } else if (cdb_group_id == CDB_GROUPID_0) { 13779 FORMG0ADDR(cdbp, lba); 13780 FORMG0COUNT(cdbp, blockcount); 13781 return (0); 13782 } else if (cdb_group_id == CDB_GROUPID_5) { 13783 FORMG5ADDR(cdbp, lba); 13784 FORMG5COUNT(cdbp, blockcount); 13785 return (0); 13786 } 13787 13788 /* Unreachable */ 13789 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13790 } 13791 13792 /* 13793 * Error setting up next portion of cmd transfer. 13794 * Something is definitely very wrong and this 13795 * should not happen. 13796 */ 13797 return (SD_PKT_ALLOC_FAILURE); 13798 } 13799 #endif /* defined(__i386) || defined(__amd64) */ 13800 13801 /* 13802 * Function: sd_initpkt_for_uscsi 13803 * 13804 * Description: Allocate and initialize for transport a scsi_pkt struct, 13805 * based upon the info specified in the given uscsi_cmd struct. 13806 * 13807 * Return Code: SD_PKT_ALLOC_SUCCESS 13808 * SD_PKT_ALLOC_FAILURE 13809 * SD_PKT_ALLOC_FAILURE_NO_DMA 13810 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13811 * 13812 * Context: Kernel thread and may be called from software interrupt context 13813 * as part of a sdrunout callback. This function may not block or 13814 * call routines that block 13815 */ 13816 13817 static int 13818 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13819 { 13820 struct uscsi_cmd *uscmd; 13821 struct sd_xbuf *xp; 13822 struct scsi_pkt *pktp; 13823 struct sd_lun *un; 13824 uint32_t flags = 0; 13825 13826 ASSERT(bp != NULL); 13827 ASSERT(pktpp != NULL); 13828 xp = SD_GET_XBUF(bp); 13829 ASSERT(xp != NULL); 13830 un = SD_GET_UN(bp); 13831 ASSERT(un != NULL); 13832 ASSERT(mutex_owned(SD_MUTEX(un))); 13833 13834 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13835 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13836 ASSERT(uscmd != NULL); 13837 13838 SD_TRACE(SD_LOG_IO_CORE, un, 13839 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13840 13841 /* 13842 * Allocate the scsi_pkt for the command. 13843 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13844 * during scsi_init_pkt time and will continue to use the 13845 * same path as long as the same scsi_pkt is used without 13846 * intervening scsi_dma_free(). Since uscsi command does 13847 * not call scsi_dmafree() before retry failed command, it 13848 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13849 * set such that scsi_vhci can use other available path for 13850 * retry. Besides, ucsci command does not allow DMA breakup, 13851 * so there is no need to set PKT_DMA_PARTIAL flag. 13852 */ 13853 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13854 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13855 sizeof (struct scsi_arq_status), 0, 13856 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13857 sdrunout, (caddr_t)un); 13858 13859 if (pktp == NULL) { 13860 *pktpp = NULL; 13861 /* 13862 * Set the driver state to RWAIT to indicate the driver 13863 * is waiting on resource allocations. The driver will not 13864 * suspend, pm_suspend, or detatch while the state is RWAIT. 13865 */ 13866 New_state(un, SD_STATE_RWAIT); 13867 13868 SD_ERROR(SD_LOG_IO_CORE, un, 13869 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13870 13871 if ((bp->b_flags & B_ERROR) != 0) { 13872 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13873 } 13874 return (SD_PKT_ALLOC_FAILURE); 13875 } 13876 13877 /* 13878 * We do not do DMA breakup for USCSI commands, so return failure 13879 * here if all the needed DMA resources were not allocated. 13880 */ 13881 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13882 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13883 scsi_destroy_pkt(pktp); 13884 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13885 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13886 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13887 } 13888 13889 /* Init the cdb from the given uscsi struct */ 13890 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13891 uscmd->uscsi_cdb[0], 0, 0, 0); 13892 13893 SD_FILL_SCSI1_LUN(un, pktp); 13894 13895 /* 13896 * Set up the optional USCSI flags. See the uscsi (7I) man page 13897 * for listing of the supported flags. 13898 */ 13899 13900 if (uscmd->uscsi_flags & USCSI_SILENT) { 13901 flags |= FLAG_SILENT; 13902 } 13903 13904 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13905 flags |= FLAG_DIAGNOSE; 13906 } 13907 13908 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13909 flags |= FLAG_ISOLATE; 13910 } 13911 13912 if (un->un_f_is_fibre == FALSE) { 13913 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13914 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13915 } 13916 } 13917 13918 /* 13919 * Set the pkt flags here so we save time later. 13920 * Note: These flags are NOT in the uscsi man page!!! 13921 */ 13922 if (uscmd->uscsi_flags & USCSI_HEAD) { 13923 flags |= FLAG_HEAD; 13924 } 13925 13926 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13927 flags |= FLAG_NOINTR; 13928 } 13929 13930 /* 13931 * For tagged queueing, things get a bit complicated. 13932 * Check first for head of queue and last for ordered queue. 13933 * If neither head nor order, use the default driver tag flags. 13934 */ 13935 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13936 if (uscmd->uscsi_flags & USCSI_HTAG) { 13937 flags |= FLAG_HTAG; 13938 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13939 flags |= FLAG_OTAG; 13940 } else { 13941 flags |= un->un_tagflags & FLAG_TAGMASK; 13942 } 13943 } 13944 13945 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13946 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13947 } 13948 13949 pktp->pkt_flags = flags; 13950 13951 /* Copy the caller's CDB into the pkt... */ 13952 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13953 13954 if (uscmd->uscsi_timeout == 0) { 13955 pktp->pkt_time = un->un_uscsi_timeout; 13956 } else { 13957 pktp->pkt_time = uscmd->uscsi_timeout; 13958 } 13959 13960 /* need it later to identify USCSI request in sdintr */ 13961 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13962 13963 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13964 13965 pktp->pkt_private = bp; 13966 pktp->pkt_comp = sdintr; 13967 *pktpp = pktp; 13968 13969 SD_TRACE(SD_LOG_IO_CORE, un, 13970 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13971 13972 return (SD_PKT_ALLOC_SUCCESS); 13973 } 13974 13975 13976 /* 13977 * Function: sd_destroypkt_for_uscsi 13978 * 13979 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13980 * IOs.. Also saves relevant info into the associated uscsi_cmd 13981 * struct. 13982 * 13983 * Context: May be called under interrupt context 13984 */ 13985 13986 static void 13987 sd_destroypkt_for_uscsi(struct buf *bp) 13988 { 13989 struct uscsi_cmd *uscmd; 13990 struct sd_xbuf *xp; 13991 struct scsi_pkt *pktp; 13992 struct sd_lun *un; 13993 13994 ASSERT(bp != NULL); 13995 xp = SD_GET_XBUF(bp); 13996 ASSERT(xp != NULL); 13997 un = SD_GET_UN(bp); 13998 ASSERT(un != NULL); 13999 ASSERT(!mutex_owned(SD_MUTEX(un))); 14000 pktp = SD_GET_PKTP(bp); 14001 ASSERT(pktp != NULL); 14002 14003 SD_TRACE(SD_LOG_IO_CORE, un, 14004 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14005 14006 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14007 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14008 ASSERT(uscmd != NULL); 14009 14010 /* Save the status and the residual into the uscsi_cmd struct */ 14011 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14012 uscmd->uscsi_resid = bp->b_resid; 14013 14014 /* 14015 * If enabled, copy any saved sense data into the area specified 14016 * by the uscsi command. 14017 */ 14018 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14019 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14020 /* 14021 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14022 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14023 */ 14024 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14025 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14026 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 14027 } 14028 14029 /* We are done with the scsi_pkt; free it now */ 14030 ASSERT(SD_GET_PKTP(bp) != NULL); 14031 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14032 14033 SD_TRACE(SD_LOG_IO_CORE, un, 14034 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14035 } 14036 14037 14038 /* 14039 * Function: sd_bioclone_alloc 14040 * 14041 * Description: Allocate a buf(9S) and init it as per the given buf 14042 * and the various arguments. The associated sd_xbuf 14043 * struct is (nearly) duplicated. The struct buf *bp 14044 * argument is saved in new_xp->xb_private. 14045 * 14046 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14047 * datalen - size of data area for the shadow bp 14048 * blkno - starting LBA 14049 * func - function pointer for b_iodone in the shadow buf. (May 14050 * be NULL if none.) 14051 * 14052 * Return Code: Pointer to allocates buf(9S) struct 14053 * 14054 * Context: Can sleep. 14055 */ 14056 14057 static struct buf * 14058 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14059 daddr_t blkno, int (*func)(struct buf *)) 14060 { 14061 struct sd_lun *un; 14062 struct sd_xbuf *xp; 14063 struct sd_xbuf *new_xp; 14064 struct buf *new_bp; 14065 14066 ASSERT(bp != NULL); 14067 xp = SD_GET_XBUF(bp); 14068 ASSERT(xp != NULL); 14069 un = SD_GET_UN(bp); 14070 ASSERT(un != NULL); 14071 ASSERT(!mutex_owned(SD_MUTEX(un))); 14072 14073 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14074 NULL, KM_SLEEP); 14075 14076 new_bp->b_lblkno = blkno; 14077 14078 /* 14079 * Allocate an xbuf for the shadow bp and copy the contents of the 14080 * original xbuf into it. 14081 */ 14082 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14083 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14084 14085 /* 14086 * The given bp is automatically saved in the xb_private member 14087 * of the new xbuf. Callers are allowed to depend on this. 14088 */ 14089 new_xp->xb_private = bp; 14090 14091 new_bp->b_private = new_xp; 14092 14093 return (new_bp); 14094 } 14095 14096 /* 14097 * Function: sd_shadow_buf_alloc 14098 * 14099 * Description: Allocate a buf(9S) and init it as per the given buf 14100 * and the various arguments. The associated sd_xbuf 14101 * struct is (nearly) duplicated. The struct buf *bp 14102 * argument is saved in new_xp->xb_private. 14103 * 14104 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14105 * datalen - size of data area for the shadow bp 14106 * bflags - B_READ or B_WRITE (pseudo flag) 14107 * blkno - starting LBA 14108 * func - function pointer for b_iodone in the shadow buf. (May 14109 * be NULL if none.) 14110 * 14111 * Return Code: Pointer to allocates buf(9S) struct 14112 * 14113 * Context: Can sleep. 14114 */ 14115 14116 static struct buf * 14117 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14118 daddr_t blkno, int (*func)(struct buf *)) 14119 { 14120 struct sd_lun *un; 14121 struct sd_xbuf *xp; 14122 struct sd_xbuf *new_xp; 14123 struct buf *new_bp; 14124 14125 ASSERT(bp != NULL); 14126 xp = SD_GET_XBUF(bp); 14127 ASSERT(xp != NULL); 14128 un = SD_GET_UN(bp); 14129 ASSERT(un != NULL); 14130 ASSERT(!mutex_owned(SD_MUTEX(un))); 14131 14132 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14133 bp_mapin(bp); 14134 } 14135 14136 bflags &= (B_READ | B_WRITE); 14137 #if defined(__i386) || defined(__amd64) 14138 new_bp = getrbuf(KM_SLEEP); 14139 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14140 new_bp->b_bcount = datalen; 14141 new_bp->b_flags = bflags | 14142 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14143 #else 14144 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14145 datalen, bflags, SLEEP_FUNC, NULL); 14146 #endif 14147 new_bp->av_forw = NULL; 14148 new_bp->av_back = NULL; 14149 new_bp->b_dev = bp->b_dev; 14150 new_bp->b_blkno = blkno; 14151 new_bp->b_iodone = func; 14152 new_bp->b_edev = bp->b_edev; 14153 new_bp->b_resid = 0; 14154 14155 /* We need to preserve the B_FAILFAST flag */ 14156 if (bp->b_flags & B_FAILFAST) { 14157 new_bp->b_flags |= B_FAILFAST; 14158 } 14159 14160 /* 14161 * Allocate an xbuf for the shadow bp and copy the contents of the 14162 * original xbuf into it. 14163 */ 14164 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14165 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14166 14167 /* Need later to copy data between the shadow buf & original buf! */ 14168 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14169 14170 /* 14171 * The given bp is automatically saved in the xb_private member 14172 * of the new xbuf. Callers are allowed to depend on this. 14173 */ 14174 new_xp->xb_private = bp; 14175 14176 new_bp->b_private = new_xp; 14177 14178 return (new_bp); 14179 } 14180 14181 /* 14182 * Function: sd_bioclone_free 14183 * 14184 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14185 * in the larger than partition operation. 14186 * 14187 * Context: May be called under interrupt context 14188 */ 14189 14190 static void 14191 sd_bioclone_free(struct buf *bp) 14192 { 14193 struct sd_xbuf *xp; 14194 14195 ASSERT(bp != NULL); 14196 xp = SD_GET_XBUF(bp); 14197 ASSERT(xp != NULL); 14198 14199 /* 14200 * Call bp_mapout() before freeing the buf, in case a lower 14201 * layer or HBA had done a bp_mapin(). we must do this here 14202 * as we are the "originator" of the shadow buf. 14203 */ 14204 bp_mapout(bp); 14205 14206 /* 14207 * Null out b_iodone before freeing the bp, to ensure that the driver 14208 * never gets confused by a stale value in this field. (Just a little 14209 * extra defensiveness here.) 14210 */ 14211 bp->b_iodone = NULL; 14212 14213 freerbuf(bp); 14214 14215 kmem_free(xp, sizeof (struct sd_xbuf)); 14216 } 14217 14218 /* 14219 * Function: sd_shadow_buf_free 14220 * 14221 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14222 * 14223 * Context: May be called under interrupt context 14224 */ 14225 14226 static void 14227 sd_shadow_buf_free(struct buf *bp) 14228 { 14229 struct sd_xbuf *xp; 14230 14231 ASSERT(bp != NULL); 14232 xp = SD_GET_XBUF(bp); 14233 ASSERT(xp != NULL); 14234 14235 #if defined(__sparc) 14236 /* 14237 * Call bp_mapout() before freeing the buf, in case a lower 14238 * layer or HBA had done a bp_mapin(). we must do this here 14239 * as we are the "originator" of the shadow buf. 14240 */ 14241 bp_mapout(bp); 14242 #endif 14243 14244 /* 14245 * Null out b_iodone before freeing the bp, to ensure that the driver 14246 * never gets confused by a stale value in this field. (Just a little 14247 * extra defensiveness here.) 14248 */ 14249 bp->b_iodone = NULL; 14250 14251 #if defined(__i386) || defined(__amd64) 14252 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14253 freerbuf(bp); 14254 #else 14255 scsi_free_consistent_buf(bp); 14256 #endif 14257 14258 kmem_free(xp, sizeof (struct sd_xbuf)); 14259 } 14260 14261 14262 /* 14263 * Function: sd_print_transport_rejected_message 14264 * 14265 * Description: This implements the ludicrously complex rules for printing 14266 * a "transport rejected" message. This is to address the 14267 * specific problem of having a flood of this error message 14268 * produced when a failover occurs. 14269 * 14270 * Context: Any. 14271 */ 14272 14273 static void 14274 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14275 int code) 14276 { 14277 ASSERT(un != NULL); 14278 ASSERT(mutex_owned(SD_MUTEX(un))); 14279 ASSERT(xp != NULL); 14280 14281 /* 14282 * Print the "transport rejected" message under the following 14283 * conditions: 14284 * 14285 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14286 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14287 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14288 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14289 * scsi_transport(9F) (which indicates that the target might have 14290 * gone off-line). This uses the un->un_tran_fatal_count 14291 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14292 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14293 * from scsi_transport(). 14294 * 14295 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14296 * the preceeding cases in order for the message to be printed. 14297 */ 14298 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14299 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14300 (code != TRAN_FATAL_ERROR) || 14301 (un->un_tran_fatal_count == 1)) { 14302 switch (code) { 14303 case TRAN_BADPKT: 14304 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14305 "transport rejected bad packet\n"); 14306 break; 14307 case TRAN_FATAL_ERROR: 14308 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14309 "transport rejected fatal error\n"); 14310 break; 14311 default: 14312 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14313 "transport rejected (%d)\n", code); 14314 break; 14315 } 14316 } 14317 } 14318 } 14319 14320 14321 /* 14322 * Function: sd_add_buf_to_waitq 14323 * 14324 * Description: Add the given buf(9S) struct to the wait queue for the 14325 * instance. If sorting is enabled, then the buf is added 14326 * to the queue via an elevator sort algorithm (a la 14327 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14328 * If sorting is not enabled, then the buf is just added 14329 * to the end of the wait queue. 14330 * 14331 * Return Code: void 14332 * 14333 * Context: Does not sleep/block, therefore technically can be called 14334 * from any context. However if sorting is enabled then the 14335 * execution time is indeterminate, and may take long if 14336 * the wait queue grows large. 14337 */ 14338 14339 static void 14340 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14341 { 14342 struct buf *ap; 14343 14344 ASSERT(bp != NULL); 14345 ASSERT(un != NULL); 14346 ASSERT(mutex_owned(SD_MUTEX(un))); 14347 14348 /* If the queue is empty, add the buf as the only entry & return. */ 14349 if (un->un_waitq_headp == NULL) { 14350 ASSERT(un->un_waitq_tailp == NULL); 14351 un->un_waitq_headp = un->un_waitq_tailp = bp; 14352 bp->av_forw = NULL; 14353 return; 14354 } 14355 14356 ASSERT(un->un_waitq_tailp != NULL); 14357 14358 /* 14359 * If sorting is disabled, just add the buf to the tail end of 14360 * the wait queue and return. 14361 */ 14362 if (un->un_f_disksort_disabled) { 14363 un->un_waitq_tailp->av_forw = bp; 14364 un->un_waitq_tailp = bp; 14365 bp->av_forw = NULL; 14366 return; 14367 } 14368 14369 /* 14370 * Sort thru the list of requests currently on the wait queue 14371 * and add the new buf request at the appropriate position. 14372 * 14373 * The un->un_waitq_headp is an activity chain pointer on which 14374 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14375 * first queue holds those requests which are positioned after 14376 * the current SD_GET_BLKNO() (in the first request); the second holds 14377 * requests which came in after their SD_GET_BLKNO() number was passed. 14378 * Thus we implement a one way scan, retracting after reaching 14379 * the end of the drive to the first request on the second 14380 * queue, at which time it becomes the first queue. 14381 * A one-way scan is natural because of the way UNIX read-ahead 14382 * blocks are allocated. 14383 * 14384 * If we lie after the first request, then we must locate the 14385 * second request list and add ourselves to it. 14386 */ 14387 ap = un->un_waitq_headp; 14388 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14389 while (ap->av_forw != NULL) { 14390 /* 14391 * Look for an "inversion" in the (normally 14392 * ascending) block numbers. This indicates 14393 * the start of the second request list. 14394 */ 14395 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14396 /* 14397 * Search the second request list for the 14398 * first request at a larger block number. 14399 * We go before that; however if there is 14400 * no such request, we go at the end. 14401 */ 14402 do { 14403 if (SD_GET_BLKNO(bp) < 14404 SD_GET_BLKNO(ap->av_forw)) { 14405 goto insert; 14406 } 14407 ap = ap->av_forw; 14408 } while (ap->av_forw != NULL); 14409 goto insert; /* after last */ 14410 } 14411 ap = ap->av_forw; 14412 } 14413 14414 /* 14415 * No inversions... we will go after the last, and 14416 * be the first request in the second request list. 14417 */ 14418 goto insert; 14419 } 14420 14421 /* 14422 * Request is at/after the current request... 14423 * sort in the first request list. 14424 */ 14425 while (ap->av_forw != NULL) { 14426 /* 14427 * We want to go after the current request (1) if 14428 * there is an inversion after it (i.e. it is the end 14429 * of the first request list), or (2) if the next 14430 * request is a larger block no. than our request. 14431 */ 14432 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14433 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14434 goto insert; 14435 } 14436 ap = ap->av_forw; 14437 } 14438 14439 /* 14440 * Neither a second list nor a larger request, therefore 14441 * we go at the end of the first list (which is the same 14442 * as the end of the whole schebang). 14443 */ 14444 insert: 14445 bp->av_forw = ap->av_forw; 14446 ap->av_forw = bp; 14447 14448 /* 14449 * If we inserted onto the tail end of the waitq, make sure the 14450 * tail pointer is updated. 14451 */ 14452 if (ap == un->un_waitq_tailp) { 14453 un->un_waitq_tailp = bp; 14454 } 14455 } 14456 14457 14458 /* 14459 * Function: sd_start_cmds 14460 * 14461 * Description: Remove and transport cmds from the driver queues. 14462 * 14463 * Arguments: un - pointer to the unit (soft state) struct for the target. 14464 * 14465 * immed_bp - ptr to a buf to be transported immediately. Only 14466 * the immed_bp is transported; bufs on the waitq are not 14467 * processed and the un_retry_bp is not checked. If immed_bp is 14468 * NULL, then normal queue processing is performed. 14469 * 14470 * Context: May be called from kernel thread context, interrupt context, 14471 * or runout callback context. This function may not block or 14472 * call routines that block. 14473 */ 14474 14475 static void 14476 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14477 { 14478 struct sd_xbuf *xp; 14479 struct buf *bp; 14480 void (*statp)(kstat_io_t *); 14481 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14482 void (*saved_statp)(kstat_io_t *); 14483 #endif 14484 int rval; 14485 14486 ASSERT(un != NULL); 14487 ASSERT(mutex_owned(SD_MUTEX(un))); 14488 ASSERT(un->un_ncmds_in_transport >= 0); 14489 ASSERT(un->un_throttle >= 0); 14490 14491 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14492 14493 do { 14494 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14495 saved_statp = NULL; 14496 #endif 14497 14498 /* 14499 * If we are syncing or dumping, fail the command to 14500 * avoid recursively calling back into scsi_transport(). 14501 * The dump I/O itself uses a separate code path so this 14502 * only prevents non-dump I/O from being sent while dumping. 14503 * File system sync takes place before dumping begins. 14504 * During panic, filesystem I/O is allowed provided 14505 * un_in_callback is <= 1. This is to prevent recursion 14506 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14507 * sd_start_cmds and so on. See panic.c for more information 14508 * about the states the system can be in during panic. 14509 */ 14510 if ((un->un_state == SD_STATE_DUMPING) || 14511 (ddi_in_panic() && (un->un_in_callback > 1))) { 14512 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14513 "sd_start_cmds: panicking\n"); 14514 goto exit; 14515 } 14516 14517 if ((bp = immed_bp) != NULL) { 14518 /* 14519 * We have a bp that must be transported immediately. 14520 * It's OK to transport the immed_bp here without doing 14521 * the throttle limit check because the immed_bp is 14522 * always used in a retry/recovery case. This means 14523 * that we know we are not at the throttle limit by 14524 * virtue of the fact that to get here we must have 14525 * already gotten a command back via sdintr(). This also 14526 * relies on (1) the command on un_retry_bp preventing 14527 * further commands from the waitq from being issued; 14528 * and (2) the code in sd_retry_command checking the 14529 * throttle limit before issuing a delayed or immediate 14530 * retry. This holds even if the throttle limit is 14531 * currently ratcheted down from its maximum value. 14532 */ 14533 statp = kstat_runq_enter; 14534 if (bp == un->un_retry_bp) { 14535 ASSERT((un->un_retry_statp == NULL) || 14536 (un->un_retry_statp == kstat_waitq_enter) || 14537 (un->un_retry_statp == 14538 kstat_runq_back_to_waitq)); 14539 /* 14540 * If the waitq kstat was incremented when 14541 * sd_set_retry_bp() queued this bp for a retry, 14542 * then we must set up statp so that the waitq 14543 * count will get decremented correctly below. 14544 * Also we must clear un->un_retry_statp to 14545 * ensure that we do not act on a stale value 14546 * in this field. 14547 */ 14548 if ((un->un_retry_statp == kstat_waitq_enter) || 14549 (un->un_retry_statp == 14550 kstat_runq_back_to_waitq)) { 14551 statp = kstat_waitq_to_runq; 14552 } 14553 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14554 saved_statp = un->un_retry_statp; 14555 #endif 14556 un->un_retry_statp = NULL; 14557 14558 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14559 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14560 "un_throttle:%d un_ncmds_in_transport:%d\n", 14561 un, un->un_retry_bp, un->un_throttle, 14562 un->un_ncmds_in_transport); 14563 } else { 14564 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14565 "processing priority bp:0x%p\n", bp); 14566 } 14567 14568 } else if ((bp = un->un_waitq_headp) != NULL) { 14569 /* 14570 * A command on the waitq is ready to go, but do not 14571 * send it if: 14572 * 14573 * (1) the throttle limit has been reached, or 14574 * (2) a retry is pending, or 14575 * (3) a START_STOP_UNIT callback pending, or 14576 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14577 * command is pending. 14578 * 14579 * For all of these conditions, IO processing will 14580 * restart after the condition is cleared. 14581 */ 14582 if (un->un_ncmds_in_transport >= un->un_throttle) { 14583 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14584 "sd_start_cmds: exiting, " 14585 "throttle limit reached!\n"); 14586 goto exit; 14587 } 14588 if (un->un_retry_bp != NULL) { 14589 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14590 "sd_start_cmds: exiting, retry pending!\n"); 14591 goto exit; 14592 } 14593 if (un->un_startstop_timeid != NULL) { 14594 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14595 "sd_start_cmds: exiting, " 14596 "START_STOP pending!\n"); 14597 goto exit; 14598 } 14599 if (un->un_direct_priority_timeid != NULL) { 14600 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14601 "sd_start_cmds: exiting, " 14602 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14603 goto exit; 14604 } 14605 14606 /* Dequeue the command */ 14607 un->un_waitq_headp = bp->av_forw; 14608 if (un->un_waitq_headp == NULL) { 14609 un->un_waitq_tailp = NULL; 14610 } 14611 bp->av_forw = NULL; 14612 statp = kstat_waitq_to_runq; 14613 SD_TRACE(SD_LOG_IO_CORE, un, 14614 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14615 14616 } else { 14617 /* No work to do so bail out now */ 14618 SD_TRACE(SD_LOG_IO_CORE, un, 14619 "sd_start_cmds: no more work, exiting!\n"); 14620 goto exit; 14621 } 14622 14623 /* 14624 * Reset the state to normal. This is the mechanism by which 14625 * the state transitions from either SD_STATE_RWAIT or 14626 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14627 * If state is SD_STATE_PM_CHANGING then this command is 14628 * part of the device power control and the state must 14629 * not be put back to normal. Doing so would would 14630 * allow new commands to proceed when they shouldn't, 14631 * the device may be going off. 14632 */ 14633 if ((un->un_state != SD_STATE_SUSPENDED) && 14634 (un->un_state != SD_STATE_PM_CHANGING)) { 14635 New_state(un, SD_STATE_NORMAL); 14636 } 14637 14638 xp = SD_GET_XBUF(bp); 14639 ASSERT(xp != NULL); 14640 14641 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14642 /* 14643 * Allocate the scsi_pkt if we need one, or attach DMA 14644 * resources if we have a scsi_pkt that needs them. The 14645 * latter should only occur for commands that are being 14646 * retried. 14647 */ 14648 if ((xp->xb_pktp == NULL) || 14649 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14650 #else 14651 if (xp->xb_pktp == NULL) { 14652 #endif 14653 /* 14654 * There is no scsi_pkt allocated for this buf. Call 14655 * the initpkt function to allocate & init one. 14656 * 14657 * The scsi_init_pkt runout callback functionality is 14658 * implemented as follows: 14659 * 14660 * 1) The initpkt function always calls 14661 * scsi_init_pkt(9F) with sdrunout specified as the 14662 * callback routine. 14663 * 2) A successful packet allocation is initialized and 14664 * the I/O is transported. 14665 * 3) The I/O associated with an allocation resource 14666 * failure is left on its queue to be retried via 14667 * runout or the next I/O. 14668 * 4) The I/O associated with a DMA error is removed 14669 * from the queue and failed with EIO. Processing of 14670 * the transport queues is also halted to be 14671 * restarted via runout or the next I/O. 14672 * 5) The I/O associated with a CDB size or packet 14673 * size error is removed from the queue and failed 14674 * with EIO. Processing of the transport queues is 14675 * continued. 14676 * 14677 * Note: there is no interface for canceling a runout 14678 * callback. To prevent the driver from detaching or 14679 * suspending while a runout is pending the driver 14680 * state is set to SD_STATE_RWAIT 14681 * 14682 * Note: using the scsi_init_pkt callback facility can 14683 * result in an I/O request persisting at the head of 14684 * the list which cannot be satisfied even after 14685 * multiple retries. In the future the driver may 14686 * implement some kind of maximum runout count before 14687 * failing an I/O. 14688 * 14689 * Note: the use of funcp below may seem superfluous, 14690 * but it helps warlock figure out the correct 14691 * initpkt function calls (see [s]sd.wlcmd). 14692 */ 14693 struct scsi_pkt *pktp; 14694 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14695 14696 ASSERT(bp != un->un_rqs_bp); 14697 14698 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14699 switch ((*funcp)(bp, &pktp)) { 14700 case SD_PKT_ALLOC_SUCCESS: 14701 xp->xb_pktp = pktp; 14702 SD_TRACE(SD_LOG_IO_CORE, un, 14703 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14704 pktp); 14705 goto got_pkt; 14706 14707 case SD_PKT_ALLOC_FAILURE: 14708 /* 14709 * Temporary (hopefully) resource depletion. 14710 * Since retries and RQS commands always have a 14711 * scsi_pkt allocated, these cases should never 14712 * get here. So the only cases this needs to 14713 * handle is a bp from the waitq (which we put 14714 * back onto the waitq for sdrunout), or a bp 14715 * sent as an immed_bp (which we just fail). 14716 */ 14717 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14718 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14719 14720 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14721 14722 if (bp == immed_bp) { 14723 /* 14724 * If SD_XB_DMA_FREED is clear, then 14725 * this is a failure to allocate a 14726 * scsi_pkt, and we must fail the 14727 * command. 14728 */ 14729 if ((xp->xb_pkt_flags & 14730 SD_XB_DMA_FREED) == 0) { 14731 break; 14732 } 14733 14734 /* 14735 * If this immediate command is NOT our 14736 * un_retry_bp, then we must fail it. 14737 */ 14738 if (bp != un->un_retry_bp) { 14739 break; 14740 } 14741 14742 /* 14743 * We get here if this cmd is our 14744 * un_retry_bp that was DMAFREED, but 14745 * scsi_init_pkt() failed to reallocate 14746 * DMA resources when we attempted to 14747 * retry it. This can happen when an 14748 * mpxio failover is in progress, but 14749 * we don't want to just fail the 14750 * command in this case. 14751 * 14752 * Use timeout(9F) to restart it after 14753 * a 100ms delay. We don't want to 14754 * let sdrunout() restart it, because 14755 * sdrunout() is just supposed to start 14756 * commands that are sitting on the 14757 * wait queue. The un_retry_bp stays 14758 * set until the command completes, but 14759 * sdrunout can be called many times 14760 * before that happens. Since sdrunout 14761 * cannot tell if the un_retry_bp is 14762 * already in the transport, it could 14763 * end up calling scsi_transport() for 14764 * the un_retry_bp multiple times. 14765 * 14766 * Also: don't schedule the callback 14767 * if some other callback is already 14768 * pending. 14769 */ 14770 if (un->un_retry_statp == NULL) { 14771 /* 14772 * restore the kstat pointer to 14773 * keep kstat counts coherent 14774 * when we do retry the command. 14775 */ 14776 un->un_retry_statp = 14777 saved_statp; 14778 } 14779 14780 if ((un->un_startstop_timeid == NULL) && 14781 (un->un_retry_timeid == NULL) && 14782 (un->un_direct_priority_timeid == 14783 NULL)) { 14784 14785 un->un_retry_timeid = 14786 timeout( 14787 sd_start_retry_command, 14788 un, SD_RESTART_TIMEOUT); 14789 } 14790 goto exit; 14791 } 14792 14793 #else 14794 if (bp == immed_bp) { 14795 break; /* Just fail the command */ 14796 } 14797 #endif 14798 14799 /* Add the buf back to the head of the waitq */ 14800 bp->av_forw = un->un_waitq_headp; 14801 un->un_waitq_headp = bp; 14802 if (un->un_waitq_tailp == NULL) { 14803 un->un_waitq_tailp = bp; 14804 } 14805 goto exit; 14806 14807 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14808 /* 14809 * HBA DMA resource failure. Fail the command 14810 * and continue processing of the queues. 14811 */ 14812 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14813 "sd_start_cmds: " 14814 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14815 break; 14816 14817 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14818 /* 14819 * Note:x86: Partial DMA mapping not supported 14820 * for USCSI commands, and all the needed DMA 14821 * resources were not allocated. 14822 */ 14823 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14824 "sd_start_cmds: " 14825 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14826 break; 14827 14828 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14829 /* 14830 * Note:x86: Request cannot fit into CDB based 14831 * on lba and len. 14832 */ 14833 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14834 "sd_start_cmds: " 14835 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14836 break; 14837 14838 default: 14839 /* Should NEVER get here! */ 14840 panic("scsi_initpkt error"); 14841 /*NOTREACHED*/ 14842 } 14843 14844 /* 14845 * Fatal error in allocating a scsi_pkt for this buf. 14846 * Update kstats & return the buf with an error code. 14847 * We must use sd_return_failed_command_no_restart() to 14848 * avoid a recursive call back into sd_start_cmds(). 14849 * However this also means that we must keep processing 14850 * the waitq here in order to avoid stalling. 14851 */ 14852 if (statp == kstat_waitq_to_runq) { 14853 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14854 } 14855 sd_return_failed_command_no_restart(un, bp, EIO); 14856 if (bp == immed_bp) { 14857 /* immed_bp is gone by now, so clear this */ 14858 immed_bp = NULL; 14859 } 14860 continue; 14861 } 14862 got_pkt: 14863 if (bp == immed_bp) { 14864 /* goto the head of the class.... */ 14865 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14866 } 14867 14868 un->un_ncmds_in_transport++; 14869 SD_UPDATE_KSTATS(un, statp, bp); 14870 14871 /* 14872 * Call scsi_transport() to send the command to the target. 14873 * According to SCSA architecture, we must drop the mutex here 14874 * before calling scsi_transport() in order to avoid deadlock. 14875 * Note that the scsi_pkt's completion routine can be executed 14876 * (from interrupt context) even before the call to 14877 * scsi_transport() returns. 14878 */ 14879 SD_TRACE(SD_LOG_IO_CORE, un, 14880 "sd_start_cmds: calling scsi_transport()\n"); 14881 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14882 14883 mutex_exit(SD_MUTEX(un)); 14884 rval = scsi_transport(xp->xb_pktp); 14885 mutex_enter(SD_MUTEX(un)); 14886 14887 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14888 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14889 14890 switch (rval) { 14891 case TRAN_ACCEPT: 14892 /* Clear this with every pkt accepted by the HBA */ 14893 un->un_tran_fatal_count = 0; 14894 break; /* Success; try the next cmd (if any) */ 14895 14896 case TRAN_BUSY: 14897 un->un_ncmds_in_transport--; 14898 ASSERT(un->un_ncmds_in_transport >= 0); 14899 14900 /* 14901 * Don't retry request sense, the sense data 14902 * is lost when another request is sent. 14903 * Free up the rqs buf and retry 14904 * the original failed cmd. Update kstat. 14905 */ 14906 if (bp == un->un_rqs_bp) { 14907 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14908 bp = sd_mark_rqs_idle(un, xp); 14909 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14910 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14911 kstat_waitq_enter); 14912 goto exit; 14913 } 14914 14915 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14916 /* 14917 * Free the DMA resources for the scsi_pkt. This will 14918 * allow mpxio to select another path the next time 14919 * we call scsi_transport() with this scsi_pkt. 14920 * See sdintr() for the rationalization behind this. 14921 */ 14922 if ((un->un_f_is_fibre == TRUE) && 14923 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14924 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14925 scsi_dmafree(xp->xb_pktp); 14926 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14927 } 14928 #endif 14929 14930 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14931 /* 14932 * Commands that are SD_PATH_DIRECT_PRIORITY 14933 * are for error recovery situations. These do 14934 * not use the normal command waitq, so if they 14935 * get a TRAN_BUSY we cannot put them back onto 14936 * the waitq for later retry. One possible 14937 * problem is that there could already be some 14938 * other command on un_retry_bp that is waiting 14939 * for this one to complete, so we would be 14940 * deadlocked if we put this command back onto 14941 * the waitq for later retry (since un_retry_bp 14942 * must complete before the driver gets back to 14943 * commands on the waitq). 14944 * 14945 * To avoid deadlock we must schedule a callback 14946 * that will restart this command after a set 14947 * interval. This should keep retrying for as 14948 * long as the underlying transport keeps 14949 * returning TRAN_BUSY (just like for other 14950 * commands). Use the same timeout interval as 14951 * for the ordinary TRAN_BUSY retry. 14952 */ 14953 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14954 "sd_start_cmds: scsi_transport() returned " 14955 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14956 14957 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14958 un->un_direct_priority_timeid = 14959 timeout(sd_start_direct_priority_command, 14960 bp, SD_BSY_TIMEOUT / 500); 14961 14962 goto exit; 14963 } 14964 14965 /* 14966 * For TRAN_BUSY, we want to reduce the throttle value, 14967 * unless we are retrying a command. 14968 */ 14969 if (bp != un->un_retry_bp) { 14970 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14971 } 14972 14973 /* 14974 * Set up the bp to be tried again 10 ms later. 14975 * Note:x86: Is there a timeout value in the sd_lun 14976 * for this condition? 14977 */ 14978 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14979 kstat_runq_back_to_waitq); 14980 goto exit; 14981 14982 case TRAN_FATAL_ERROR: 14983 un->un_tran_fatal_count++; 14984 /* FALLTHRU */ 14985 14986 case TRAN_BADPKT: 14987 default: 14988 un->un_ncmds_in_transport--; 14989 ASSERT(un->un_ncmds_in_transport >= 0); 14990 14991 /* 14992 * If this is our REQUEST SENSE command with a 14993 * transport error, we must get back the pointers 14994 * to the original buf, and mark the REQUEST 14995 * SENSE command as "available". 14996 */ 14997 if (bp == un->un_rqs_bp) { 14998 bp = sd_mark_rqs_idle(un, xp); 14999 xp = SD_GET_XBUF(bp); 15000 } else { 15001 /* 15002 * Legacy behavior: do not update transport 15003 * error count for request sense commands. 15004 */ 15005 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15006 } 15007 15008 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15009 sd_print_transport_rejected_message(un, xp, rval); 15010 15011 /* 15012 * We must use sd_return_failed_command_no_restart() to 15013 * avoid a recursive call back into sd_start_cmds(). 15014 * However this also means that we must keep processing 15015 * the waitq here in order to avoid stalling. 15016 */ 15017 sd_return_failed_command_no_restart(un, bp, EIO); 15018 15019 /* 15020 * Notify any threads waiting in sd_ddi_suspend() that 15021 * a command completion has occurred. 15022 */ 15023 if (un->un_state == SD_STATE_SUSPENDED) { 15024 cv_broadcast(&un->un_disk_busy_cv); 15025 } 15026 15027 if (bp == immed_bp) { 15028 /* immed_bp is gone by now, so clear this */ 15029 immed_bp = NULL; 15030 } 15031 break; 15032 } 15033 15034 } while (immed_bp == NULL); 15035 15036 exit: 15037 ASSERT(mutex_owned(SD_MUTEX(un))); 15038 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15039 } 15040 15041 15042 /* 15043 * Function: sd_return_command 15044 * 15045 * Description: Returns a command to its originator (with or without an 15046 * error). Also starts commands waiting to be transported 15047 * to the target. 15048 * 15049 * Context: May be called from interrupt, kernel, or timeout context 15050 */ 15051 15052 static void 15053 sd_return_command(struct sd_lun *un, struct buf *bp) 15054 { 15055 struct sd_xbuf *xp; 15056 #if defined(__i386) || defined(__amd64) 15057 struct scsi_pkt *pktp; 15058 #endif 15059 15060 ASSERT(bp != NULL); 15061 ASSERT(un != NULL); 15062 ASSERT(mutex_owned(SD_MUTEX(un))); 15063 ASSERT(bp != un->un_rqs_bp); 15064 xp = SD_GET_XBUF(bp); 15065 ASSERT(xp != NULL); 15066 15067 #if defined(__i386) || defined(__amd64) 15068 pktp = SD_GET_PKTP(bp); 15069 #endif 15070 15071 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15072 15073 #if defined(__i386) || defined(__amd64) 15074 /* 15075 * Note:x86: check for the "sdrestart failed" case. 15076 */ 15077 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15078 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15079 (xp->xb_pktp->pkt_resid == 0)) { 15080 15081 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15082 /* 15083 * Successfully set up next portion of cmd 15084 * transfer, try sending it 15085 */ 15086 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15087 NULL, NULL, 0, (clock_t)0, NULL); 15088 sd_start_cmds(un, NULL); 15089 return; /* Note:x86: need a return here? */ 15090 } 15091 } 15092 #endif 15093 15094 /* 15095 * If this is the failfast bp, clear it from un_failfast_bp. This 15096 * can happen if upon being re-tried the failfast bp either 15097 * succeeded or encountered another error (possibly even a different 15098 * error than the one that precipitated the failfast state, but in 15099 * that case it would have had to exhaust retries as well). Regardless, 15100 * this should not occur whenever the instance is in the active 15101 * failfast state. 15102 */ 15103 if (bp == un->un_failfast_bp) { 15104 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15105 un->un_failfast_bp = NULL; 15106 } 15107 15108 /* 15109 * Clear the failfast state upon successful completion of ANY cmd. 15110 */ 15111 if (bp->b_error == 0) { 15112 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15113 } 15114 15115 /* 15116 * This is used if the command was retried one or more times. Show that 15117 * we are done with it, and allow processing of the waitq to resume. 15118 */ 15119 if (bp == un->un_retry_bp) { 15120 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15121 "sd_return_command: un:0x%p: " 15122 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15123 un->un_retry_bp = NULL; 15124 un->un_retry_statp = NULL; 15125 } 15126 15127 SD_UPDATE_RDWR_STATS(un, bp); 15128 SD_UPDATE_PARTITION_STATS(un, bp); 15129 15130 switch (un->un_state) { 15131 case SD_STATE_SUSPENDED: 15132 /* 15133 * Notify any threads waiting in sd_ddi_suspend() that 15134 * a command completion has occurred. 15135 */ 15136 cv_broadcast(&un->un_disk_busy_cv); 15137 break; 15138 default: 15139 sd_start_cmds(un, NULL); 15140 break; 15141 } 15142 15143 /* Return this command up the iodone chain to its originator. */ 15144 mutex_exit(SD_MUTEX(un)); 15145 15146 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15147 xp->xb_pktp = NULL; 15148 15149 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15150 15151 ASSERT(!mutex_owned(SD_MUTEX(un))); 15152 mutex_enter(SD_MUTEX(un)); 15153 15154 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15155 } 15156 15157 15158 /* 15159 * Function: sd_return_failed_command 15160 * 15161 * Description: Command completion when an error occurred. 15162 * 15163 * Context: May be called from interrupt context 15164 */ 15165 15166 static void 15167 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15168 { 15169 ASSERT(bp != NULL); 15170 ASSERT(un != NULL); 15171 ASSERT(mutex_owned(SD_MUTEX(un))); 15172 15173 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15174 "sd_return_failed_command: entry\n"); 15175 15176 /* 15177 * b_resid could already be nonzero due to a partial data 15178 * transfer, so do not change it here. 15179 */ 15180 SD_BIOERROR(bp, errcode); 15181 15182 sd_return_command(un, bp); 15183 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15184 "sd_return_failed_command: exit\n"); 15185 } 15186 15187 15188 /* 15189 * Function: sd_return_failed_command_no_restart 15190 * 15191 * Description: Same as sd_return_failed_command, but ensures that no 15192 * call back into sd_start_cmds will be issued. 15193 * 15194 * Context: May be called from interrupt context 15195 */ 15196 15197 static void 15198 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15199 int errcode) 15200 { 15201 struct sd_xbuf *xp; 15202 15203 ASSERT(bp != NULL); 15204 ASSERT(un != NULL); 15205 ASSERT(mutex_owned(SD_MUTEX(un))); 15206 xp = SD_GET_XBUF(bp); 15207 ASSERT(xp != NULL); 15208 ASSERT(errcode != 0); 15209 15210 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15211 "sd_return_failed_command_no_restart: entry\n"); 15212 15213 /* 15214 * b_resid could already be nonzero due to a partial data 15215 * transfer, so do not change it here. 15216 */ 15217 SD_BIOERROR(bp, errcode); 15218 15219 /* 15220 * If this is the failfast bp, clear it. This can happen if the 15221 * failfast bp encounterd a fatal error when we attempted to 15222 * re-try it (such as a scsi_transport(9F) failure). However 15223 * we should NOT be in an active failfast state if the failfast 15224 * bp is not NULL. 15225 */ 15226 if (bp == un->un_failfast_bp) { 15227 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15228 un->un_failfast_bp = NULL; 15229 } 15230 15231 if (bp == un->un_retry_bp) { 15232 /* 15233 * This command was retried one or more times. Show that we are 15234 * done with it, and allow processing of the waitq to resume. 15235 */ 15236 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15237 "sd_return_failed_command_no_restart: " 15238 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15239 un->un_retry_bp = NULL; 15240 un->un_retry_statp = NULL; 15241 } 15242 15243 SD_UPDATE_RDWR_STATS(un, bp); 15244 SD_UPDATE_PARTITION_STATS(un, bp); 15245 15246 mutex_exit(SD_MUTEX(un)); 15247 15248 if (xp->xb_pktp != NULL) { 15249 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15250 xp->xb_pktp = NULL; 15251 } 15252 15253 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15254 15255 mutex_enter(SD_MUTEX(un)); 15256 15257 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15258 "sd_return_failed_command_no_restart: exit\n"); 15259 } 15260 15261 15262 /* 15263 * Function: sd_retry_command 15264 * 15265 * Description: queue up a command for retry, or (optionally) fail it 15266 * if retry counts are exhausted. 15267 * 15268 * Arguments: un - Pointer to the sd_lun struct for the target. 15269 * 15270 * bp - Pointer to the buf for the command to be retried. 15271 * 15272 * retry_check_flag - Flag to see which (if any) of the retry 15273 * counts should be decremented/checked. If the indicated 15274 * retry count is exhausted, then the command will not be 15275 * retried; it will be failed instead. This should use a 15276 * value equal to one of the following: 15277 * 15278 * SD_RETRIES_NOCHECK 15279 * SD_RESD_RETRIES_STANDARD 15280 * SD_RETRIES_VICTIM 15281 * 15282 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15283 * if the check should be made to see of FLAG_ISOLATE is set 15284 * in the pkt. If FLAG_ISOLATE is set, then the command is 15285 * not retried, it is simply failed. 15286 * 15287 * user_funcp - Ptr to function to call before dispatching the 15288 * command. May be NULL if no action needs to be performed. 15289 * (Primarily intended for printing messages.) 15290 * 15291 * user_arg - Optional argument to be passed along to 15292 * the user_funcp call. 15293 * 15294 * failure_code - errno return code to set in the bp if the 15295 * command is going to be failed. 15296 * 15297 * retry_delay - Retry delay interval in (clock_t) units. May 15298 * be zero which indicates that the retry should be retried 15299 * immediately (ie, without an intervening delay). 15300 * 15301 * statp - Ptr to kstat function to be updated if the command 15302 * is queued for a delayed retry. May be NULL if no kstat 15303 * update is desired. 15304 * 15305 * Context: May be called from interupt context. 15306 */ 15307 15308 static void 15309 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15310 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15311 code), void *user_arg, int failure_code, clock_t retry_delay, 15312 void (*statp)(kstat_io_t *)) 15313 { 15314 struct sd_xbuf *xp; 15315 struct scsi_pkt *pktp; 15316 15317 ASSERT(un != NULL); 15318 ASSERT(mutex_owned(SD_MUTEX(un))); 15319 ASSERT(bp != NULL); 15320 xp = SD_GET_XBUF(bp); 15321 ASSERT(xp != NULL); 15322 pktp = SD_GET_PKTP(bp); 15323 ASSERT(pktp != NULL); 15324 15325 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15326 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15327 15328 /* 15329 * If we are syncing or dumping, fail the command to avoid 15330 * recursively calling back into scsi_transport(). 15331 */ 15332 if (ddi_in_panic()) { 15333 goto fail_command_no_log; 15334 } 15335 15336 /* 15337 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15338 * log an error and fail the command. 15339 */ 15340 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15341 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15342 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15343 sd_dump_memory(un, SD_LOG_IO, "CDB", 15344 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15345 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15346 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15347 goto fail_command; 15348 } 15349 15350 /* 15351 * If we are suspended, then put the command onto head of the 15352 * wait queue since we don't want to start more commands. 15353 */ 15354 switch (un->un_state) { 15355 case SD_STATE_SUSPENDED: 15356 case SD_STATE_DUMPING: 15357 bp->av_forw = un->un_waitq_headp; 15358 un->un_waitq_headp = bp; 15359 if (un->un_waitq_tailp == NULL) { 15360 un->un_waitq_tailp = bp; 15361 } 15362 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15363 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15364 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15365 return; 15366 default: 15367 break; 15368 } 15369 15370 /* 15371 * If the caller wants us to check FLAG_ISOLATE, then see if that 15372 * is set; if it is then we do not want to retry the command. 15373 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15374 */ 15375 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15376 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15377 goto fail_command; 15378 } 15379 } 15380 15381 15382 /* 15383 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15384 * command timeout or a selection timeout has occurred. This means 15385 * that we were unable to establish an kind of communication with 15386 * the target, and subsequent retries and/or commands are likely 15387 * to encounter similar results and take a long time to complete. 15388 * 15389 * If this is a failfast error condition, we need to update the 15390 * failfast state, even if this bp does not have B_FAILFAST set. 15391 */ 15392 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15393 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15394 ASSERT(un->un_failfast_bp == NULL); 15395 /* 15396 * If we are already in the active failfast state, and 15397 * another failfast error condition has been detected, 15398 * then fail this command if it has B_FAILFAST set. 15399 * If B_FAILFAST is clear, then maintain the legacy 15400 * behavior of retrying heroically, even tho this will 15401 * take a lot more time to fail the command. 15402 */ 15403 if (bp->b_flags & B_FAILFAST) { 15404 goto fail_command; 15405 } 15406 } else { 15407 /* 15408 * We're not in the active failfast state, but we 15409 * have a failfast error condition, so we must begin 15410 * transition to the next state. We do this regardless 15411 * of whether or not this bp has B_FAILFAST set. 15412 */ 15413 if (un->un_failfast_bp == NULL) { 15414 /* 15415 * This is the first bp to meet a failfast 15416 * condition so save it on un_failfast_bp & 15417 * do normal retry processing. Do not enter 15418 * active failfast state yet. This marks 15419 * entry into the "failfast pending" state. 15420 */ 15421 un->un_failfast_bp = bp; 15422 15423 } else if (un->un_failfast_bp == bp) { 15424 /* 15425 * This is the second time *this* bp has 15426 * encountered a failfast error condition, 15427 * so enter active failfast state & flush 15428 * queues as appropriate. 15429 */ 15430 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15431 un->un_failfast_bp = NULL; 15432 sd_failfast_flushq(un); 15433 15434 /* 15435 * Fail this bp now if B_FAILFAST set; 15436 * otherwise continue with retries. (It would 15437 * be pretty ironic if this bp succeeded on a 15438 * subsequent retry after we just flushed all 15439 * the queues). 15440 */ 15441 if (bp->b_flags & B_FAILFAST) { 15442 goto fail_command; 15443 } 15444 15445 #if !defined(lint) && !defined(__lint) 15446 } else { 15447 /* 15448 * If neither of the preceeding conditionals 15449 * was true, it means that there is some 15450 * *other* bp that has met an inital failfast 15451 * condition and is currently either being 15452 * retried or is waiting to be retried. In 15453 * that case we should perform normal retry 15454 * processing on *this* bp, since there is a 15455 * chance that the current failfast condition 15456 * is transient and recoverable. If that does 15457 * not turn out to be the case, then retries 15458 * will be cleared when the wait queue is 15459 * flushed anyway. 15460 */ 15461 #endif 15462 } 15463 } 15464 } else { 15465 /* 15466 * SD_RETRIES_FAILFAST is clear, which indicates that we 15467 * likely were able to at least establish some level of 15468 * communication with the target and subsequent commands 15469 * and/or retries are likely to get through to the target, 15470 * In this case we want to be aggressive about clearing 15471 * the failfast state. Note that this does not affect 15472 * the "failfast pending" condition. 15473 */ 15474 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15475 } 15476 15477 15478 /* 15479 * Check the specified retry count to see if we can still do 15480 * any retries with this pkt before we should fail it. 15481 */ 15482 switch (retry_check_flag & SD_RETRIES_MASK) { 15483 case SD_RETRIES_VICTIM: 15484 /* 15485 * Check the victim retry count. If exhausted, then fall 15486 * thru & check against the standard retry count. 15487 */ 15488 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15489 /* Increment count & proceed with the retry */ 15490 xp->xb_victim_retry_count++; 15491 break; 15492 } 15493 /* Victim retries exhausted, fall back to std. retries... */ 15494 /* FALLTHRU */ 15495 15496 case SD_RETRIES_STANDARD: 15497 if (xp->xb_retry_count >= un->un_retry_count) { 15498 /* Retries exhausted, fail the command */ 15499 SD_TRACE(SD_LOG_IO_CORE, un, 15500 "sd_retry_command: retries exhausted!\n"); 15501 /* 15502 * update b_resid for failed SCMD_READ & SCMD_WRITE 15503 * commands with nonzero pkt_resid. 15504 */ 15505 if ((pktp->pkt_reason == CMD_CMPLT) && 15506 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15507 (pktp->pkt_resid != 0)) { 15508 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15509 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15510 SD_UPDATE_B_RESID(bp, pktp); 15511 } 15512 } 15513 goto fail_command; 15514 } 15515 xp->xb_retry_count++; 15516 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15517 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15518 break; 15519 15520 case SD_RETRIES_UA: 15521 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15522 /* Retries exhausted, fail the command */ 15523 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15524 "Unit Attention retries exhausted. " 15525 "Check the target.\n"); 15526 goto fail_command; 15527 } 15528 xp->xb_ua_retry_count++; 15529 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15530 "sd_retry_command: retry count:%d\n", 15531 xp->xb_ua_retry_count); 15532 break; 15533 15534 case SD_RETRIES_BUSY: 15535 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15536 /* Retries exhausted, fail the command */ 15537 SD_TRACE(SD_LOG_IO_CORE, un, 15538 "sd_retry_command: retries exhausted!\n"); 15539 goto fail_command; 15540 } 15541 xp->xb_retry_count++; 15542 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15543 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15544 break; 15545 15546 case SD_RETRIES_NOCHECK: 15547 default: 15548 /* No retry count to check. Just proceed with the retry */ 15549 break; 15550 } 15551 15552 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15553 15554 /* 15555 * If we were given a zero timeout, we must attempt to retry the 15556 * command immediately (ie, without a delay). 15557 */ 15558 if (retry_delay == 0) { 15559 /* 15560 * Check some limiting conditions to see if we can actually 15561 * do the immediate retry. If we cannot, then we must 15562 * fall back to queueing up a delayed retry. 15563 */ 15564 if (un->un_ncmds_in_transport >= un->un_throttle) { 15565 /* 15566 * We are at the throttle limit for the target, 15567 * fall back to delayed retry. 15568 */ 15569 retry_delay = SD_BSY_TIMEOUT; 15570 statp = kstat_waitq_enter; 15571 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15572 "sd_retry_command: immed. retry hit " 15573 "throttle!\n"); 15574 } else { 15575 /* 15576 * We're clear to proceed with the immediate retry. 15577 * First call the user-provided function (if any) 15578 */ 15579 if (user_funcp != NULL) { 15580 (*user_funcp)(un, bp, user_arg, 15581 SD_IMMEDIATE_RETRY_ISSUED); 15582 #ifdef __lock_lint 15583 sd_print_incomplete_msg(un, bp, user_arg, 15584 SD_IMMEDIATE_RETRY_ISSUED); 15585 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15586 SD_IMMEDIATE_RETRY_ISSUED); 15587 sd_print_sense_failed_msg(un, bp, user_arg, 15588 SD_IMMEDIATE_RETRY_ISSUED); 15589 #endif 15590 } 15591 15592 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15593 "sd_retry_command: issuing immediate retry\n"); 15594 15595 /* 15596 * Call sd_start_cmds() to transport the command to 15597 * the target. 15598 */ 15599 sd_start_cmds(un, bp); 15600 15601 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15602 "sd_retry_command exit\n"); 15603 return; 15604 } 15605 } 15606 15607 /* 15608 * Set up to retry the command after a delay. 15609 * First call the user-provided function (if any) 15610 */ 15611 if (user_funcp != NULL) { 15612 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15613 } 15614 15615 sd_set_retry_bp(un, bp, retry_delay, statp); 15616 15617 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15618 return; 15619 15620 fail_command: 15621 15622 if (user_funcp != NULL) { 15623 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15624 } 15625 15626 fail_command_no_log: 15627 15628 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15629 "sd_retry_command: returning failed command\n"); 15630 15631 sd_return_failed_command(un, bp, failure_code); 15632 15633 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15634 } 15635 15636 15637 /* 15638 * Function: sd_set_retry_bp 15639 * 15640 * Description: Set up the given bp for retry. 15641 * 15642 * Arguments: un - ptr to associated softstate 15643 * bp - ptr to buf(9S) for the command 15644 * retry_delay - time interval before issuing retry (may be 0) 15645 * statp - optional pointer to kstat function 15646 * 15647 * Context: May be called under interrupt context 15648 */ 15649 15650 static void 15651 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15652 void (*statp)(kstat_io_t *)) 15653 { 15654 ASSERT(un != NULL); 15655 ASSERT(mutex_owned(SD_MUTEX(un))); 15656 ASSERT(bp != NULL); 15657 15658 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15659 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15660 15661 /* 15662 * Indicate that the command is being retried. This will not allow any 15663 * other commands on the wait queue to be transported to the target 15664 * until this command has been completed (success or failure). The 15665 * "retry command" is not transported to the target until the given 15666 * time delay expires, unless the user specified a 0 retry_delay. 15667 * 15668 * Note: the timeout(9F) callback routine is what actually calls 15669 * sd_start_cmds() to transport the command, with the exception of a 15670 * zero retry_delay. The only current implementor of a zero retry delay 15671 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15672 */ 15673 if (un->un_retry_bp == NULL) { 15674 ASSERT(un->un_retry_statp == NULL); 15675 un->un_retry_bp = bp; 15676 15677 /* 15678 * If the user has not specified a delay the command should 15679 * be queued and no timeout should be scheduled. 15680 */ 15681 if (retry_delay == 0) { 15682 /* 15683 * Save the kstat pointer that will be used in the 15684 * call to SD_UPDATE_KSTATS() below, so that 15685 * sd_start_cmds() can correctly decrement the waitq 15686 * count when it is time to transport this command. 15687 */ 15688 un->un_retry_statp = statp; 15689 goto done; 15690 } 15691 } 15692 15693 if (un->un_retry_bp == bp) { 15694 /* 15695 * Save the kstat pointer that will be used in the call to 15696 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15697 * correctly decrement the waitq count when it is time to 15698 * transport this command. 15699 */ 15700 un->un_retry_statp = statp; 15701 15702 /* 15703 * Schedule a timeout if: 15704 * 1) The user has specified a delay. 15705 * 2) There is not a START_STOP_UNIT callback pending. 15706 * 15707 * If no delay has been specified, then it is up to the caller 15708 * to ensure that IO processing continues without stalling. 15709 * Effectively, this means that the caller will issue the 15710 * required call to sd_start_cmds(). The START_STOP_UNIT 15711 * callback does this after the START STOP UNIT command has 15712 * completed. In either of these cases we should not schedule 15713 * a timeout callback here. Also don't schedule the timeout if 15714 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15715 */ 15716 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15717 (un->un_direct_priority_timeid == NULL)) { 15718 un->un_retry_timeid = 15719 timeout(sd_start_retry_command, un, retry_delay); 15720 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15721 "sd_set_retry_bp: setting timeout: un: 0x%p" 15722 " bp:0x%p un_retry_timeid:0x%p\n", 15723 un, bp, un->un_retry_timeid); 15724 } 15725 } else { 15726 /* 15727 * We only get in here if there is already another command 15728 * waiting to be retried. In this case, we just put the 15729 * given command onto the wait queue, so it can be transported 15730 * after the current retry command has completed. 15731 * 15732 * Also we have to make sure that if the command at the head 15733 * of the wait queue is the un_failfast_bp, that we do not 15734 * put ahead of it any other commands that are to be retried. 15735 */ 15736 if ((un->un_failfast_bp != NULL) && 15737 (un->un_failfast_bp == un->un_waitq_headp)) { 15738 /* 15739 * Enqueue this command AFTER the first command on 15740 * the wait queue (which is also un_failfast_bp). 15741 */ 15742 bp->av_forw = un->un_waitq_headp->av_forw; 15743 un->un_waitq_headp->av_forw = bp; 15744 if (un->un_waitq_headp == un->un_waitq_tailp) { 15745 un->un_waitq_tailp = bp; 15746 } 15747 } else { 15748 /* Enqueue this command at the head of the waitq. */ 15749 bp->av_forw = un->un_waitq_headp; 15750 un->un_waitq_headp = bp; 15751 if (un->un_waitq_tailp == NULL) { 15752 un->un_waitq_tailp = bp; 15753 } 15754 } 15755 15756 if (statp == NULL) { 15757 statp = kstat_waitq_enter; 15758 } 15759 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15760 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15761 } 15762 15763 done: 15764 if (statp != NULL) { 15765 SD_UPDATE_KSTATS(un, statp, bp); 15766 } 15767 15768 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15769 "sd_set_retry_bp: exit un:0x%p\n", un); 15770 } 15771 15772 15773 /* 15774 * Function: sd_start_retry_command 15775 * 15776 * Description: Start the command that has been waiting on the target's 15777 * retry queue. Called from timeout(9F) context after the 15778 * retry delay interval has expired. 15779 * 15780 * Arguments: arg - pointer to associated softstate for the device. 15781 * 15782 * Context: timeout(9F) thread context. May not sleep. 15783 */ 15784 15785 static void 15786 sd_start_retry_command(void *arg) 15787 { 15788 struct sd_lun *un = arg; 15789 15790 ASSERT(un != NULL); 15791 ASSERT(!mutex_owned(SD_MUTEX(un))); 15792 15793 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15794 "sd_start_retry_command: entry\n"); 15795 15796 mutex_enter(SD_MUTEX(un)); 15797 15798 un->un_retry_timeid = NULL; 15799 15800 if (un->un_retry_bp != NULL) { 15801 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15802 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15803 un, un->un_retry_bp); 15804 sd_start_cmds(un, un->un_retry_bp); 15805 } 15806 15807 mutex_exit(SD_MUTEX(un)); 15808 15809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15810 "sd_start_retry_command: exit\n"); 15811 } 15812 15813 15814 /* 15815 * Function: sd_start_direct_priority_command 15816 * 15817 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15818 * received TRAN_BUSY when we called scsi_transport() to send it 15819 * to the underlying HBA. This function is called from timeout(9F) 15820 * context after the delay interval has expired. 15821 * 15822 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15823 * 15824 * Context: timeout(9F) thread context. May not sleep. 15825 */ 15826 15827 static void 15828 sd_start_direct_priority_command(void *arg) 15829 { 15830 struct buf *priority_bp = arg; 15831 struct sd_lun *un; 15832 15833 ASSERT(priority_bp != NULL); 15834 un = SD_GET_UN(priority_bp); 15835 ASSERT(un != NULL); 15836 ASSERT(!mutex_owned(SD_MUTEX(un))); 15837 15838 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15839 "sd_start_direct_priority_command: entry\n"); 15840 15841 mutex_enter(SD_MUTEX(un)); 15842 un->un_direct_priority_timeid = NULL; 15843 sd_start_cmds(un, priority_bp); 15844 mutex_exit(SD_MUTEX(un)); 15845 15846 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15847 "sd_start_direct_priority_command: exit\n"); 15848 } 15849 15850 15851 /* 15852 * Function: sd_send_request_sense_command 15853 * 15854 * Description: Sends a REQUEST SENSE command to the target 15855 * 15856 * Context: May be called from interrupt context. 15857 */ 15858 15859 static void 15860 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15861 struct scsi_pkt *pktp) 15862 { 15863 ASSERT(bp != NULL); 15864 ASSERT(un != NULL); 15865 ASSERT(mutex_owned(SD_MUTEX(un))); 15866 15867 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15868 "entry: buf:0x%p\n", bp); 15869 15870 /* 15871 * If we are syncing or dumping, then fail the command to avoid a 15872 * recursive callback into scsi_transport(). Also fail the command 15873 * if we are suspended (legacy behavior). 15874 */ 15875 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15876 (un->un_state == SD_STATE_DUMPING)) { 15877 sd_return_failed_command(un, bp, EIO); 15878 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15879 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15880 return; 15881 } 15882 15883 /* 15884 * Retry the failed command and don't issue the request sense if: 15885 * 1) the sense buf is busy 15886 * 2) we have 1 or more outstanding commands on the target 15887 * (the sense data will be cleared or invalidated any way) 15888 * 15889 * Note: There could be an issue with not checking a retry limit here, 15890 * the problem is determining which retry limit to check. 15891 */ 15892 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15893 /* Don't retry if the command is flagged as non-retryable */ 15894 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15895 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15896 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15897 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15898 "sd_send_request_sense_command: " 15899 "at full throttle, retrying exit\n"); 15900 } else { 15901 sd_return_failed_command(un, bp, EIO); 15902 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15903 "sd_send_request_sense_command: " 15904 "at full throttle, non-retryable exit\n"); 15905 } 15906 return; 15907 } 15908 15909 sd_mark_rqs_busy(un, bp); 15910 sd_start_cmds(un, un->un_rqs_bp); 15911 15912 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15913 "sd_send_request_sense_command: exit\n"); 15914 } 15915 15916 15917 /* 15918 * Function: sd_mark_rqs_busy 15919 * 15920 * Description: Indicate that the request sense bp for this instance is 15921 * in use. 15922 * 15923 * Context: May be called under interrupt context 15924 */ 15925 15926 static void 15927 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15928 { 15929 struct sd_xbuf *sense_xp; 15930 15931 ASSERT(un != NULL); 15932 ASSERT(bp != NULL); 15933 ASSERT(mutex_owned(SD_MUTEX(un))); 15934 ASSERT(un->un_sense_isbusy == 0); 15935 15936 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15937 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15938 15939 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15940 ASSERT(sense_xp != NULL); 15941 15942 SD_INFO(SD_LOG_IO, un, 15943 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15944 15945 ASSERT(sense_xp->xb_pktp != NULL); 15946 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15947 == (FLAG_SENSING | FLAG_HEAD)); 15948 15949 un->un_sense_isbusy = 1; 15950 un->un_rqs_bp->b_resid = 0; 15951 sense_xp->xb_pktp->pkt_resid = 0; 15952 sense_xp->xb_pktp->pkt_reason = 0; 15953 15954 /* So we can get back the bp at interrupt time! */ 15955 sense_xp->xb_sense_bp = bp; 15956 15957 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15958 15959 /* 15960 * Mark this buf as awaiting sense data. (This is already set in 15961 * the pkt_flags for the RQS packet.) 15962 */ 15963 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15964 15965 sense_xp->xb_retry_count = 0; 15966 sense_xp->xb_victim_retry_count = 0; 15967 sense_xp->xb_ua_retry_count = 0; 15968 sense_xp->xb_dma_resid = 0; 15969 15970 /* Clean up the fields for auto-request sense */ 15971 sense_xp->xb_sense_status = 0; 15972 sense_xp->xb_sense_state = 0; 15973 sense_xp->xb_sense_resid = 0; 15974 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15975 15976 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15977 } 15978 15979 15980 /* 15981 * Function: sd_mark_rqs_idle 15982 * 15983 * Description: SD_MUTEX must be held continuously through this routine 15984 * to prevent reuse of the rqs struct before the caller can 15985 * complete it's processing. 15986 * 15987 * Return Code: Pointer to the RQS buf 15988 * 15989 * Context: May be called under interrupt context 15990 */ 15991 15992 static struct buf * 15993 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15994 { 15995 struct buf *bp; 15996 ASSERT(un != NULL); 15997 ASSERT(sense_xp != NULL); 15998 ASSERT(mutex_owned(SD_MUTEX(un))); 15999 ASSERT(un->un_sense_isbusy != 0); 16000 16001 un->un_sense_isbusy = 0; 16002 bp = sense_xp->xb_sense_bp; 16003 sense_xp->xb_sense_bp = NULL; 16004 16005 /* This pkt is no longer interested in getting sense data */ 16006 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16007 16008 return (bp); 16009 } 16010 16011 16012 16013 /* 16014 * Function: sd_alloc_rqs 16015 * 16016 * Description: Set up the unit to receive auto request sense data 16017 * 16018 * Return Code: DDI_SUCCESS or DDI_FAILURE 16019 * 16020 * Context: Called under attach(9E) context 16021 */ 16022 16023 static int 16024 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16025 { 16026 struct sd_xbuf *xp; 16027 16028 ASSERT(un != NULL); 16029 ASSERT(!mutex_owned(SD_MUTEX(un))); 16030 ASSERT(un->un_rqs_bp == NULL); 16031 ASSERT(un->un_rqs_pktp == NULL); 16032 16033 /* 16034 * First allocate the required buf and scsi_pkt structs, then set up 16035 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16036 */ 16037 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16038 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16039 if (un->un_rqs_bp == NULL) { 16040 return (DDI_FAILURE); 16041 } 16042 16043 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16044 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16045 16046 if (un->un_rqs_pktp == NULL) { 16047 sd_free_rqs(un); 16048 return (DDI_FAILURE); 16049 } 16050 16051 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16052 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16053 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 16054 16055 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16056 16057 /* Set up the other needed members in the ARQ scsi_pkt. */ 16058 un->un_rqs_pktp->pkt_comp = sdintr; 16059 un->un_rqs_pktp->pkt_time = sd_io_time; 16060 un->un_rqs_pktp->pkt_flags |= 16061 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16062 16063 /* 16064 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16065 * provide any intpkt, destroypkt routines as we take care of 16066 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16067 */ 16068 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16069 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16070 xp->xb_pktp = un->un_rqs_pktp; 16071 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16072 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16073 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16074 16075 /* 16076 * Save the pointer to the request sense private bp so it can 16077 * be retrieved in sdintr. 16078 */ 16079 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16080 ASSERT(un->un_rqs_bp->b_private == xp); 16081 16082 /* 16083 * See if the HBA supports auto-request sense for the specified 16084 * target/lun. If it does, then try to enable it (if not already 16085 * enabled). 16086 * 16087 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16088 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16089 * return success. However, in both of these cases ARQ is always 16090 * enabled and scsi_ifgetcap will always return true. The best approach 16091 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16092 * 16093 * The 3rd case is the HBA (adp) always return enabled on 16094 * scsi_ifgetgetcap even when it's not enable, the best approach 16095 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16096 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16097 */ 16098 16099 if (un->un_f_is_fibre == TRUE) { 16100 un->un_f_arq_enabled = TRUE; 16101 } else { 16102 #if defined(__i386) || defined(__amd64) 16103 /* 16104 * Circumvent the Adaptec bug, remove this code when 16105 * the bug is fixed 16106 */ 16107 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16108 #endif 16109 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16110 case 0: 16111 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16112 "sd_alloc_rqs: HBA supports ARQ\n"); 16113 /* 16114 * ARQ is supported by this HBA but currently is not 16115 * enabled. Attempt to enable it and if successful then 16116 * mark this instance as ARQ enabled. 16117 */ 16118 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16119 == 1) { 16120 /* Successfully enabled ARQ in the HBA */ 16121 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16122 "sd_alloc_rqs: ARQ enabled\n"); 16123 un->un_f_arq_enabled = TRUE; 16124 } else { 16125 /* Could not enable ARQ in the HBA */ 16126 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16127 "sd_alloc_rqs: failed ARQ enable\n"); 16128 un->un_f_arq_enabled = FALSE; 16129 } 16130 break; 16131 case 1: 16132 /* 16133 * ARQ is supported by this HBA and is already enabled. 16134 * Just mark ARQ as enabled for this instance. 16135 */ 16136 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16137 "sd_alloc_rqs: ARQ already enabled\n"); 16138 un->un_f_arq_enabled = TRUE; 16139 break; 16140 default: 16141 /* 16142 * ARQ is not supported by this HBA; disable it for this 16143 * instance. 16144 */ 16145 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16146 "sd_alloc_rqs: HBA does not support ARQ\n"); 16147 un->un_f_arq_enabled = FALSE; 16148 break; 16149 } 16150 } 16151 16152 return (DDI_SUCCESS); 16153 } 16154 16155 16156 /* 16157 * Function: sd_free_rqs 16158 * 16159 * Description: Cleanup for the pre-instance RQS command. 16160 * 16161 * Context: Kernel thread context 16162 */ 16163 16164 static void 16165 sd_free_rqs(struct sd_lun *un) 16166 { 16167 ASSERT(un != NULL); 16168 16169 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16170 16171 /* 16172 * If consistent memory is bound to a scsi_pkt, the pkt 16173 * has to be destroyed *before* freeing the consistent memory. 16174 * Don't change the sequence of this operations. 16175 * scsi_destroy_pkt() might access memory, which isn't allowed, 16176 * after it was freed in scsi_free_consistent_buf(). 16177 */ 16178 if (un->un_rqs_pktp != NULL) { 16179 scsi_destroy_pkt(un->un_rqs_pktp); 16180 un->un_rqs_pktp = NULL; 16181 } 16182 16183 if (un->un_rqs_bp != NULL) { 16184 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16185 scsi_free_consistent_buf(un->un_rqs_bp); 16186 un->un_rqs_bp = NULL; 16187 } 16188 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16189 } 16190 16191 16192 16193 /* 16194 * Function: sd_reduce_throttle 16195 * 16196 * Description: Reduces the maximun # of outstanding commands on a 16197 * target to the current number of outstanding commands. 16198 * Queues a tiemout(9F) callback to restore the limit 16199 * after a specified interval has elapsed. 16200 * Typically used when we get a TRAN_BUSY return code 16201 * back from scsi_transport(). 16202 * 16203 * Arguments: un - ptr to the sd_lun softstate struct 16204 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16205 * 16206 * Context: May be called from interrupt context 16207 */ 16208 16209 static void 16210 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16211 { 16212 ASSERT(un != NULL); 16213 ASSERT(mutex_owned(SD_MUTEX(un))); 16214 ASSERT(un->un_ncmds_in_transport >= 0); 16215 16216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16217 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16218 un, un->un_throttle, un->un_ncmds_in_transport); 16219 16220 if (un->un_throttle > 1) { 16221 if (un->un_f_use_adaptive_throttle == TRUE) { 16222 switch (throttle_type) { 16223 case SD_THROTTLE_TRAN_BUSY: 16224 if (un->un_busy_throttle == 0) { 16225 un->un_busy_throttle = un->un_throttle; 16226 } 16227 break; 16228 case SD_THROTTLE_QFULL: 16229 un->un_busy_throttle = 0; 16230 break; 16231 default: 16232 ASSERT(FALSE); 16233 } 16234 16235 if (un->un_ncmds_in_transport > 0) { 16236 un->un_throttle = un->un_ncmds_in_transport; 16237 } 16238 16239 } else { 16240 if (un->un_ncmds_in_transport == 0) { 16241 un->un_throttle = 1; 16242 } else { 16243 un->un_throttle = un->un_ncmds_in_transport; 16244 } 16245 } 16246 } 16247 16248 /* Reschedule the timeout if none is currently active */ 16249 if (un->un_reset_throttle_timeid == NULL) { 16250 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16251 un, SD_THROTTLE_RESET_INTERVAL); 16252 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16253 "sd_reduce_throttle: timeout scheduled!\n"); 16254 } 16255 16256 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16257 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16258 } 16259 16260 16261 16262 /* 16263 * Function: sd_restore_throttle 16264 * 16265 * Description: Callback function for timeout(9F). Resets the current 16266 * value of un->un_throttle to its default. 16267 * 16268 * Arguments: arg - pointer to associated softstate for the device. 16269 * 16270 * Context: May be called from interrupt context 16271 */ 16272 16273 static void 16274 sd_restore_throttle(void *arg) 16275 { 16276 struct sd_lun *un = arg; 16277 16278 ASSERT(un != NULL); 16279 ASSERT(!mutex_owned(SD_MUTEX(un))); 16280 16281 mutex_enter(SD_MUTEX(un)); 16282 16283 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16284 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16285 16286 un->un_reset_throttle_timeid = NULL; 16287 16288 if (un->un_f_use_adaptive_throttle == TRUE) { 16289 /* 16290 * If un_busy_throttle is nonzero, then it contains the 16291 * value that un_throttle was when we got a TRAN_BUSY back 16292 * from scsi_transport(). We want to revert back to this 16293 * value. 16294 * 16295 * In the QFULL case, the throttle limit will incrementally 16296 * increase until it reaches max throttle. 16297 */ 16298 if (un->un_busy_throttle > 0) { 16299 un->un_throttle = un->un_busy_throttle; 16300 un->un_busy_throttle = 0; 16301 } else { 16302 /* 16303 * increase throttle by 10% open gate slowly, schedule 16304 * another restore if saved throttle has not been 16305 * reached 16306 */ 16307 short throttle; 16308 if (sd_qfull_throttle_enable) { 16309 throttle = un->un_throttle + 16310 max((un->un_throttle / 10), 1); 16311 un->un_throttle = 16312 (throttle < un->un_saved_throttle) ? 16313 throttle : un->un_saved_throttle; 16314 if (un->un_throttle < un->un_saved_throttle) { 16315 un->un_reset_throttle_timeid = 16316 timeout(sd_restore_throttle, 16317 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16318 } 16319 } 16320 } 16321 16322 /* 16323 * If un_throttle has fallen below the low-water mark, we 16324 * restore the maximum value here (and allow it to ratchet 16325 * down again if necessary). 16326 */ 16327 if (un->un_throttle < un->un_min_throttle) { 16328 un->un_throttle = un->un_saved_throttle; 16329 } 16330 } else { 16331 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16332 "restoring limit from 0x%x to 0x%x\n", 16333 un->un_throttle, un->un_saved_throttle); 16334 un->un_throttle = un->un_saved_throttle; 16335 } 16336 16337 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16338 "sd_restore_throttle: calling sd_start_cmds!\n"); 16339 16340 sd_start_cmds(un, NULL); 16341 16342 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16343 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16344 un, un->un_throttle); 16345 16346 mutex_exit(SD_MUTEX(un)); 16347 16348 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16349 } 16350 16351 /* 16352 * Function: sdrunout 16353 * 16354 * Description: Callback routine for scsi_init_pkt when a resource allocation 16355 * fails. 16356 * 16357 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16358 * soft state instance. 16359 * 16360 * Return Code: The scsi_init_pkt routine allows for the callback function to 16361 * return a 0 indicating the callback should be rescheduled or a 1 16362 * indicating not to reschedule. This routine always returns 1 16363 * because the driver always provides a callback function to 16364 * scsi_init_pkt. This results in a callback always being scheduled 16365 * (via the scsi_init_pkt callback implementation) if a resource 16366 * failure occurs. 16367 * 16368 * Context: This callback function may not block or call routines that block 16369 * 16370 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16371 * request persisting at the head of the list which cannot be 16372 * satisfied even after multiple retries. In the future the driver 16373 * may implement some time of maximum runout count before failing 16374 * an I/O. 16375 */ 16376 16377 static int 16378 sdrunout(caddr_t arg) 16379 { 16380 struct sd_lun *un = (struct sd_lun *)arg; 16381 16382 ASSERT(un != NULL); 16383 ASSERT(!mutex_owned(SD_MUTEX(un))); 16384 16385 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16386 16387 mutex_enter(SD_MUTEX(un)); 16388 sd_start_cmds(un, NULL); 16389 mutex_exit(SD_MUTEX(un)); 16390 /* 16391 * This callback routine always returns 1 (i.e. do not reschedule) 16392 * because we always specify sdrunout as the callback handler for 16393 * scsi_init_pkt inside the call to sd_start_cmds. 16394 */ 16395 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16396 return (1); 16397 } 16398 16399 16400 /* 16401 * Function: sdintr 16402 * 16403 * Description: Completion callback routine for scsi_pkt(9S) structs 16404 * sent to the HBA driver via scsi_transport(9F). 16405 * 16406 * Context: Interrupt context 16407 */ 16408 16409 static void 16410 sdintr(struct scsi_pkt *pktp) 16411 { 16412 struct buf *bp; 16413 struct sd_xbuf *xp; 16414 struct sd_lun *un; 16415 16416 ASSERT(pktp != NULL); 16417 bp = (struct buf *)pktp->pkt_private; 16418 ASSERT(bp != NULL); 16419 xp = SD_GET_XBUF(bp); 16420 ASSERT(xp != NULL); 16421 ASSERT(xp->xb_pktp != NULL); 16422 un = SD_GET_UN(bp); 16423 ASSERT(un != NULL); 16424 ASSERT(!mutex_owned(SD_MUTEX(un))); 16425 16426 #ifdef SD_FAULT_INJECTION 16427 16428 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16429 /* SD FaultInjection */ 16430 sd_faultinjection(pktp); 16431 16432 #endif /* SD_FAULT_INJECTION */ 16433 16434 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16435 " xp:0x%p, un:0x%p\n", bp, xp, un); 16436 16437 mutex_enter(SD_MUTEX(un)); 16438 16439 /* Reduce the count of the #commands currently in transport */ 16440 un->un_ncmds_in_transport--; 16441 ASSERT(un->un_ncmds_in_transport >= 0); 16442 16443 /* Increment counter to indicate that the callback routine is active */ 16444 un->un_in_callback++; 16445 16446 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16447 16448 #ifdef SDDEBUG 16449 if (bp == un->un_retry_bp) { 16450 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16451 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16452 un, un->un_retry_bp, un->un_ncmds_in_transport); 16453 } 16454 #endif 16455 16456 /* 16457 * If pkt_reason is CMD_DEV_GONE, just fail the command 16458 */ 16459 if (pktp->pkt_reason == CMD_DEV_GONE) { 16460 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16461 "Device is gone\n"); 16462 sd_return_failed_command(un, bp, EIO); 16463 goto exit; 16464 } 16465 16466 /* 16467 * First see if the pkt has auto-request sense data with it.... 16468 * Look at the packet state first so we don't take a performance 16469 * hit looking at the arq enabled flag unless absolutely necessary. 16470 */ 16471 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16472 (un->un_f_arq_enabled == TRUE)) { 16473 /* 16474 * The HBA did an auto request sense for this command so check 16475 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16476 * driver command that should not be retried. 16477 */ 16478 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16479 /* 16480 * Save the relevant sense info into the xp for the 16481 * original cmd. 16482 */ 16483 struct scsi_arq_status *asp; 16484 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16485 xp->xb_sense_status = 16486 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16487 xp->xb_sense_state = asp->sts_rqpkt_state; 16488 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16489 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16490 min(sizeof (struct scsi_extended_sense), 16491 SENSE_LENGTH)); 16492 16493 /* fail the command */ 16494 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16495 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16496 sd_return_failed_command(un, bp, EIO); 16497 goto exit; 16498 } 16499 16500 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16501 /* 16502 * We want to either retry or fail this command, so free 16503 * the DMA resources here. If we retry the command then 16504 * the DMA resources will be reallocated in sd_start_cmds(). 16505 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16506 * causes the *entire* transfer to start over again from the 16507 * beginning of the request, even for PARTIAL chunks that 16508 * have already transferred successfully. 16509 */ 16510 if ((un->un_f_is_fibre == TRUE) && 16511 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16512 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16513 scsi_dmafree(pktp); 16514 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16515 } 16516 #endif 16517 16518 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16519 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16520 16521 sd_handle_auto_request_sense(un, bp, xp, pktp); 16522 goto exit; 16523 } 16524 16525 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16526 if (pktp->pkt_flags & FLAG_SENSING) { 16527 /* This pktp is from the unit's REQUEST_SENSE command */ 16528 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16529 "sdintr: sd_handle_request_sense\n"); 16530 sd_handle_request_sense(un, bp, xp, pktp); 16531 goto exit; 16532 } 16533 16534 /* 16535 * Check to see if the command successfully completed as requested; 16536 * this is the most common case (and also the hot performance path). 16537 * 16538 * Requirements for successful completion are: 16539 * pkt_reason is CMD_CMPLT and packet status is status good. 16540 * In addition: 16541 * - A residual of zero indicates successful completion no matter what 16542 * the command is. 16543 * - If the residual is not zero and the command is not a read or 16544 * write, then it's still defined as successful completion. In other 16545 * words, if the command is a read or write the residual must be 16546 * zero for successful completion. 16547 * - If the residual is not zero and the command is a read or 16548 * write, and it's a USCSICMD, then it's still defined as 16549 * successful completion. 16550 */ 16551 if ((pktp->pkt_reason == CMD_CMPLT) && 16552 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16553 16554 /* 16555 * Since this command is returned with a good status, we 16556 * can reset the count for Sonoma failover. 16557 */ 16558 un->un_sonoma_failure_count = 0; 16559 16560 /* 16561 * Return all USCSI commands on good status 16562 */ 16563 if (pktp->pkt_resid == 0) { 16564 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16565 "sdintr: returning command for resid == 0\n"); 16566 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16567 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16568 SD_UPDATE_B_RESID(bp, pktp); 16569 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16570 "sdintr: returning command for resid != 0\n"); 16571 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16572 SD_UPDATE_B_RESID(bp, pktp); 16573 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16574 "sdintr: returning uscsi command\n"); 16575 } else { 16576 goto not_successful; 16577 } 16578 sd_return_command(un, bp); 16579 16580 /* 16581 * Decrement counter to indicate that the callback routine 16582 * is done. 16583 */ 16584 un->un_in_callback--; 16585 ASSERT(un->un_in_callback >= 0); 16586 mutex_exit(SD_MUTEX(un)); 16587 16588 return; 16589 } 16590 16591 not_successful: 16592 16593 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16594 /* 16595 * The following is based upon knowledge of the underlying transport 16596 * and its use of DMA resources. This code should be removed when 16597 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16598 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16599 * and sd_start_cmds(). 16600 * 16601 * Free any DMA resources associated with this command if there 16602 * is a chance it could be retried or enqueued for later retry. 16603 * If we keep the DMA binding then mpxio cannot reissue the 16604 * command on another path whenever a path failure occurs. 16605 * 16606 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16607 * causes the *entire* transfer to start over again from the 16608 * beginning of the request, even for PARTIAL chunks that 16609 * have already transferred successfully. 16610 * 16611 * This is only done for non-uscsi commands (and also skipped for the 16612 * driver's internal RQS command). Also just do this for Fibre Channel 16613 * devices as these are the only ones that support mpxio. 16614 */ 16615 if ((un->un_f_is_fibre == TRUE) && 16616 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16617 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16618 scsi_dmafree(pktp); 16619 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16620 } 16621 #endif 16622 16623 /* 16624 * The command did not successfully complete as requested so check 16625 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16626 * driver command that should not be retried so just return. If 16627 * FLAG_DIAGNOSE is not set the error will be processed below. 16628 */ 16629 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16630 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16631 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16632 /* 16633 * Issue a request sense if a check condition caused the error 16634 * (we handle the auto request sense case above), otherwise 16635 * just fail the command. 16636 */ 16637 if ((pktp->pkt_reason == CMD_CMPLT) && 16638 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16639 sd_send_request_sense_command(un, bp, pktp); 16640 } else { 16641 sd_return_failed_command(un, bp, EIO); 16642 } 16643 goto exit; 16644 } 16645 16646 /* 16647 * The command did not successfully complete as requested so process 16648 * the error, retry, and/or attempt recovery. 16649 */ 16650 switch (pktp->pkt_reason) { 16651 case CMD_CMPLT: 16652 switch (SD_GET_PKT_STATUS(pktp)) { 16653 case STATUS_GOOD: 16654 /* 16655 * The command completed successfully with a non-zero 16656 * residual 16657 */ 16658 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16659 "sdintr: STATUS_GOOD \n"); 16660 sd_pkt_status_good(un, bp, xp, pktp); 16661 break; 16662 16663 case STATUS_CHECK: 16664 case STATUS_TERMINATED: 16665 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16666 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16667 sd_pkt_status_check_condition(un, bp, xp, pktp); 16668 break; 16669 16670 case STATUS_BUSY: 16671 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16672 "sdintr: STATUS_BUSY\n"); 16673 sd_pkt_status_busy(un, bp, xp, pktp); 16674 break; 16675 16676 case STATUS_RESERVATION_CONFLICT: 16677 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16678 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16679 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16680 break; 16681 16682 case STATUS_QFULL: 16683 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16684 "sdintr: STATUS_QFULL\n"); 16685 sd_pkt_status_qfull(un, bp, xp, pktp); 16686 break; 16687 16688 case STATUS_MET: 16689 case STATUS_INTERMEDIATE: 16690 case STATUS_SCSI2: 16691 case STATUS_INTERMEDIATE_MET: 16692 case STATUS_ACA_ACTIVE: 16693 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16694 "Unexpected SCSI status received: 0x%x\n", 16695 SD_GET_PKT_STATUS(pktp)); 16696 sd_return_failed_command(un, bp, EIO); 16697 break; 16698 16699 default: 16700 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16701 "Invalid SCSI status received: 0x%x\n", 16702 SD_GET_PKT_STATUS(pktp)); 16703 sd_return_failed_command(un, bp, EIO); 16704 break; 16705 16706 } 16707 break; 16708 16709 case CMD_INCOMPLETE: 16710 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16711 "sdintr: CMD_INCOMPLETE\n"); 16712 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16713 break; 16714 case CMD_TRAN_ERR: 16715 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16716 "sdintr: CMD_TRAN_ERR\n"); 16717 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16718 break; 16719 case CMD_RESET: 16720 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16721 "sdintr: CMD_RESET \n"); 16722 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16723 break; 16724 case CMD_ABORTED: 16725 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16726 "sdintr: CMD_ABORTED \n"); 16727 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16728 break; 16729 case CMD_TIMEOUT: 16730 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16731 "sdintr: CMD_TIMEOUT\n"); 16732 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16733 break; 16734 case CMD_UNX_BUS_FREE: 16735 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16736 "sdintr: CMD_UNX_BUS_FREE \n"); 16737 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16738 break; 16739 case CMD_TAG_REJECT: 16740 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16741 "sdintr: CMD_TAG_REJECT\n"); 16742 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16743 break; 16744 default: 16745 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16746 "sdintr: default\n"); 16747 sd_pkt_reason_default(un, bp, xp, pktp); 16748 break; 16749 } 16750 16751 exit: 16752 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16753 16754 /* Decrement counter to indicate that the callback routine is done. */ 16755 un->un_in_callback--; 16756 ASSERT(un->un_in_callback >= 0); 16757 16758 /* 16759 * At this point, the pkt has been dispatched, ie, it is either 16760 * being re-tried or has been returned to its caller and should 16761 * not be referenced. 16762 */ 16763 16764 mutex_exit(SD_MUTEX(un)); 16765 } 16766 16767 16768 /* 16769 * Function: sd_print_incomplete_msg 16770 * 16771 * Description: Prints the error message for a CMD_INCOMPLETE error. 16772 * 16773 * Arguments: un - ptr to associated softstate for the device. 16774 * bp - ptr to the buf(9S) for the command. 16775 * arg - message string ptr 16776 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16777 * or SD_NO_RETRY_ISSUED. 16778 * 16779 * Context: May be called under interrupt context 16780 */ 16781 16782 static void 16783 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16784 { 16785 struct scsi_pkt *pktp; 16786 char *msgp; 16787 char *cmdp = arg; 16788 16789 ASSERT(un != NULL); 16790 ASSERT(mutex_owned(SD_MUTEX(un))); 16791 ASSERT(bp != NULL); 16792 ASSERT(arg != NULL); 16793 pktp = SD_GET_PKTP(bp); 16794 ASSERT(pktp != NULL); 16795 16796 switch (code) { 16797 case SD_DELAYED_RETRY_ISSUED: 16798 case SD_IMMEDIATE_RETRY_ISSUED: 16799 msgp = "retrying"; 16800 break; 16801 case SD_NO_RETRY_ISSUED: 16802 default: 16803 msgp = "giving up"; 16804 break; 16805 } 16806 16807 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16808 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16809 "incomplete %s- %s\n", cmdp, msgp); 16810 } 16811 } 16812 16813 16814 16815 /* 16816 * Function: sd_pkt_status_good 16817 * 16818 * Description: Processing for a STATUS_GOOD code in pkt_status. 16819 * 16820 * Context: May be called under interrupt context 16821 */ 16822 16823 static void 16824 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16825 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16826 { 16827 char *cmdp; 16828 16829 ASSERT(un != NULL); 16830 ASSERT(mutex_owned(SD_MUTEX(un))); 16831 ASSERT(bp != NULL); 16832 ASSERT(xp != NULL); 16833 ASSERT(pktp != NULL); 16834 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16835 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16836 ASSERT(pktp->pkt_resid != 0); 16837 16838 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16839 16840 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16841 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16842 case SCMD_READ: 16843 cmdp = "read"; 16844 break; 16845 case SCMD_WRITE: 16846 cmdp = "write"; 16847 break; 16848 default: 16849 SD_UPDATE_B_RESID(bp, pktp); 16850 sd_return_command(un, bp); 16851 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16852 return; 16853 } 16854 16855 /* 16856 * See if we can retry the read/write, preferrably immediately. 16857 * If retries are exhaused, then sd_retry_command() will update 16858 * the b_resid count. 16859 */ 16860 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16861 cmdp, EIO, (clock_t)0, NULL); 16862 16863 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16864 } 16865 16866 16867 16868 16869 16870 /* 16871 * Function: sd_handle_request_sense 16872 * 16873 * Description: Processing for non-auto Request Sense command. 16874 * 16875 * Arguments: un - ptr to associated softstate 16876 * sense_bp - ptr to buf(9S) for the RQS command 16877 * sense_xp - ptr to the sd_xbuf for the RQS command 16878 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16879 * 16880 * Context: May be called under interrupt context 16881 */ 16882 16883 static void 16884 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16885 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16886 { 16887 struct buf *cmd_bp; /* buf for the original command */ 16888 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16889 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16890 16891 ASSERT(un != NULL); 16892 ASSERT(mutex_owned(SD_MUTEX(un))); 16893 ASSERT(sense_bp != NULL); 16894 ASSERT(sense_xp != NULL); 16895 ASSERT(sense_pktp != NULL); 16896 16897 /* 16898 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16899 * RQS command and not the original command. 16900 */ 16901 ASSERT(sense_pktp == un->un_rqs_pktp); 16902 ASSERT(sense_bp == un->un_rqs_bp); 16903 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16904 (FLAG_SENSING | FLAG_HEAD)); 16905 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16906 FLAG_SENSING) == FLAG_SENSING); 16907 16908 /* These are the bp, xp, and pktp for the original command */ 16909 cmd_bp = sense_xp->xb_sense_bp; 16910 cmd_xp = SD_GET_XBUF(cmd_bp); 16911 cmd_pktp = SD_GET_PKTP(cmd_bp); 16912 16913 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16914 /* 16915 * The REQUEST SENSE command failed. Release the REQUEST 16916 * SENSE command for re-use, get back the bp for the original 16917 * command, and attempt to re-try the original command if 16918 * FLAG_DIAGNOSE is not set in the original packet. 16919 */ 16920 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16921 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16922 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16923 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16924 NULL, NULL, EIO, (clock_t)0, NULL); 16925 return; 16926 } 16927 } 16928 16929 /* 16930 * Save the relevant sense info into the xp for the original cmd. 16931 * 16932 * Note: if the request sense failed the state info will be zero 16933 * as set in sd_mark_rqs_busy() 16934 */ 16935 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16936 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16937 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16938 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16939 16940 /* 16941 * Free up the RQS command.... 16942 * NOTE: 16943 * Must do this BEFORE calling sd_validate_sense_data! 16944 * sd_validate_sense_data may return the original command in 16945 * which case the pkt will be freed and the flags can no 16946 * longer be touched. 16947 * SD_MUTEX is held through this process until the command 16948 * is dispatched based upon the sense data, so there are 16949 * no race conditions. 16950 */ 16951 (void) sd_mark_rqs_idle(un, sense_xp); 16952 16953 /* 16954 * For a retryable command see if we have valid sense data, if so then 16955 * turn it over to sd_decode_sense() to figure out the right course of 16956 * action. Just fail a non-retryable command. 16957 */ 16958 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16959 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16960 SD_SENSE_DATA_IS_VALID) { 16961 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16962 } 16963 } else { 16964 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16965 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16966 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16967 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16968 sd_return_failed_command(un, cmd_bp, EIO); 16969 } 16970 } 16971 16972 16973 16974 16975 /* 16976 * Function: sd_handle_auto_request_sense 16977 * 16978 * Description: Processing for auto-request sense information. 16979 * 16980 * Arguments: un - ptr to associated softstate 16981 * bp - ptr to buf(9S) for the command 16982 * xp - ptr to the sd_xbuf for the command 16983 * pktp - ptr to the scsi_pkt(9S) for the command 16984 * 16985 * Context: May be called under interrupt context 16986 */ 16987 16988 static void 16989 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16990 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16991 { 16992 struct scsi_arq_status *asp; 16993 16994 ASSERT(un != NULL); 16995 ASSERT(mutex_owned(SD_MUTEX(un))); 16996 ASSERT(bp != NULL); 16997 ASSERT(xp != NULL); 16998 ASSERT(pktp != NULL); 16999 ASSERT(pktp != un->un_rqs_pktp); 17000 ASSERT(bp != un->un_rqs_bp); 17001 17002 /* 17003 * For auto-request sense, we get a scsi_arq_status back from 17004 * the HBA, with the sense data in the sts_sensedata member. 17005 * The pkt_scbp of the packet points to this scsi_arq_status. 17006 */ 17007 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17008 17009 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17010 /* 17011 * The auto REQUEST SENSE failed; see if we can re-try 17012 * the original command. 17013 */ 17014 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17015 "auto request sense failed (reason=%s)\n", 17016 scsi_rname(asp->sts_rqpkt_reason)); 17017 17018 sd_reset_target(un, pktp); 17019 17020 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17021 NULL, NULL, EIO, (clock_t)0, NULL); 17022 return; 17023 } 17024 17025 /* Save the relevant sense info into the xp for the original cmd. */ 17026 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17027 xp->xb_sense_state = asp->sts_rqpkt_state; 17028 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17029 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17030 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 17031 17032 /* 17033 * See if we have valid sense data, if so then turn it over to 17034 * sd_decode_sense() to figure out the right course of action. 17035 */ 17036 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 17037 sd_decode_sense(un, bp, xp, pktp); 17038 } 17039 } 17040 17041 17042 /* 17043 * Function: sd_print_sense_failed_msg 17044 * 17045 * Description: Print log message when RQS has failed. 17046 * 17047 * Arguments: un - ptr to associated softstate 17048 * bp - ptr to buf(9S) for the command 17049 * arg - generic message string ptr 17050 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17051 * or SD_NO_RETRY_ISSUED 17052 * 17053 * Context: May be called from interrupt context 17054 */ 17055 17056 static void 17057 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17058 int code) 17059 { 17060 char *msgp = arg; 17061 17062 ASSERT(un != NULL); 17063 ASSERT(mutex_owned(SD_MUTEX(un))); 17064 ASSERT(bp != NULL); 17065 17066 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17067 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17068 } 17069 } 17070 17071 17072 /* 17073 * Function: sd_validate_sense_data 17074 * 17075 * Description: Check the given sense data for validity. 17076 * If the sense data is not valid, the command will 17077 * be either failed or retried! 17078 * 17079 * Return Code: SD_SENSE_DATA_IS_INVALID 17080 * SD_SENSE_DATA_IS_VALID 17081 * 17082 * Context: May be called from interrupt context 17083 */ 17084 17085 static int 17086 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 17087 { 17088 struct scsi_extended_sense *esp; 17089 struct scsi_pkt *pktp; 17090 size_t actual_len; 17091 char *msgp = NULL; 17092 17093 ASSERT(un != NULL); 17094 ASSERT(mutex_owned(SD_MUTEX(un))); 17095 ASSERT(bp != NULL); 17096 ASSERT(bp != un->un_rqs_bp); 17097 ASSERT(xp != NULL); 17098 17099 pktp = SD_GET_PKTP(bp); 17100 ASSERT(pktp != NULL); 17101 17102 /* 17103 * Check the status of the RQS command (auto or manual). 17104 */ 17105 switch (xp->xb_sense_status & STATUS_MASK) { 17106 case STATUS_GOOD: 17107 break; 17108 17109 case STATUS_RESERVATION_CONFLICT: 17110 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17111 return (SD_SENSE_DATA_IS_INVALID); 17112 17113 case STATUS_BUSY: 17114 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17115 "Busy Status on REQUEST SENSE\n"); 17116 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17117 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17118 return (SD_SENSE_DATA_IS_INVALID); 17119 17120 case STATUS_QFULL: 17121 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17122 "QFULL Status on REQUEST SENSE\n"); 17123 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17124 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17125 return (SD_SENSE_DATA_IS_INVALID); 17126 17127 case STATUS_CHECK: 17128 case STATUS_TERMINATED: 17129 msgp = "Check Condition on REQUEST SENSE\n"; 17130 goto sense_failed; 17131 17132 default: 17133 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17134 goto sense_failed; 17135 } 17136 17137 /* 17138 * See if we got the minimum required amount of sense data. 17139 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17140 * or less. 17141 */ 17142 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17143 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17144 (actual_len == 0)) { 17145 msgp = "Request Sense couldn't get sense data\n"; 17146 goto sense_failed; 17147 } 17148 17149 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17150 msgp = "Not enough sense information\n"; 17151 goto sense_failed; 17152 } 17153 17154 /* 17155 * We require the extended sense data 17156 */ 17157 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17158 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17159 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17160 static char tmp[8]; 17161 static char buf[148]; 17162 char *p = (char *)(xp->xb_sense_data); 17163 int i; 17164 17165 mutex_enter(&sd_sense_mutex); 17166 (void) strcpy(buf, "undecodable sense information:"); 17167 for (i = 0; i < actual_len; i++) { 17168 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17169 (void) strcpy(&buf[strlen(buf)], tmp); 17170 } 17171 i = strlen(buf); 17172 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17173 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17174 mutex_exit(&sd_sense_mutex); 17175 } 17176 /* Note: Legacy behavior, fail the command with no retry */ 17177 sd_return_failed_command(un, bp, EIO); 17178 return (SD_SENSE_DATA_IS_INVALID); 17179 } 17180 17181 /* 17182 * Check that es_code is valid (es_class concatenated with es_code 17183 * make up the "response code" field. es_class will always be 7, so 17184 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17185 * format. 17186 */ 17187 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17188 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17189 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17190 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17191 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17192 goto sense_failed; 17193 } 17194 17195 return (SD_SENSE_DATA_IS_VALID); 17196 17197 sense_failed: 17198 /* 17199 * If the request sense failed (for whatever reason), attempt 17200 * to retry the original command. 17201 */ 17202 #if defined(__i386) || defined(__amd64) 17203 /* 17204 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17205 * sddef.h for Sparc platform, and x86 uses 1 binary 17206 * for both SCSI/FC. 17207 * The SD_RETRY_DELAY value need to be adjusted here 17208 * when SD_RETRY_DELAY change in sddef.h 17209 */ 17210 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17211 sd_print_sense_failed_msg, msgp, EIO, 17212 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17213 #else 17214 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17215 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17216 #endif 17217 17218 return (SD_SENSE_DATA_IS_INVALID); 17219 } 17220 17221 17222 17223 /* 17224 * Function: sd_decode_sense 17225 * 17226 * Description: Take recovery action(s) when SCSI Sense Data is received. 17227 * 17228 * Context: Interrupt context. 17229 */ 17230 17231 static void 17232 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17233 struct scsi_pkt *pktp) 17234 { 17235 uint8_t sense_key; 17236 17237 ASSERT(un != NULL); 17238 ASSERT(mutex_owned(SD_MUTEX(un))); 17239 ASSERT(bp != NULL); 17240 ASSERT(bp != un->un_rqs_bp); 17241 ASSERT(xp != NULL); 17242 ASSERT(pktp != NULL); 17243 17244 sense_key = scsi_sense_key(xp->xb_sense_data); 17245 17246 switch (sense_key) { 17247 case KEY_NO_SENSE: 17248 sd_sense_key_no_sense(un, bp, xp, pktp); 17249 break; 17250 case KEY_RECOVERABLE_ERROR: 17251 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17252 bp, xp, pktp); 17253 break; 17254 case KEY_NOT_READY: 17255 sd_sense_key_not_ready(un, xp->xb_sense_data, 17256 bp, xp, pktp); 17257 break; 17258 case KEY_MEDIUM_ERROR: 17259 case KEY_HARDWARE_ERROR: 17260 sd_sense_key_medium_or_hardware_error(un, 17261 xp->xb_sense_data, bp, xp, pktp); 17262 break; 17263 case KEY_ILLEGAL_REQUEST: 17264 sd_sense_key_illegal_request(un, bp, xp, pktp); 17265 break; 17266 case KEY_UNIT_ATTENTION: 17267 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17268 bp, xp, pktp); 17269 break; 17270 case KEY_WRITE_PROTECT: 17271 case KEY_VOLUME_OVERFLOW: 17272 case KEY_MISCOMPARE: 17273 sd_sense_key_fail_command(un, bp, xp, pktp); 17274 break; 17275 case KEY_BLANK_CHECK: 17276 sd_sense_key_blank_check(un, bp, xp, pktp); 17277 break; 17278 case KEY_ABORTED_COMMAND: 17279 sd_sense_key_aborted_command(un, bp, xp, pktp); 17280 break; 17281 case KEY_VENDOR_UNIQUE: 17282 case KEY_COPY_ABORTED: 17283 case KEY_EQUAL: 17284 case KEY_RESERVED: 17285 default: 17286 sd_sense_key_default(un, xp->xb_sense_data, 17287 bp, xp, pktp); 17288 break; 17289 } 17290 } 17291 17292 17293 /* 17294 * Function: sd_dump_memory 17295 * 17296 * Description: Debug logging routine to print the contents of a user provided 17297 * buffer. The output of the buffer is broken up into 256 byte 17298 * segments due to a size constraint of the scsi_log. 17299 * implementation. 17300 * 17301 * Arguments: un - ptr to softstate 17302 * comp - component mask 17303 * title - "title" string to preceed data when printed 17304 * data - ptr to data block to be printed 17305 * len - size of data block to be printed 17306 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17307 * 17308 * Context: May be called from interrupt context 17309 */ 17310 17311 #define SD_DUMP_MEMORY_BUF_SIZE 256 17312 17313 static char *sd_dump_format_string[] = { 17314 " 0x%02x", 17315 " %c" 17316 }; 17317 17318 static void 17319 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17320 int len, int fmt) 17321 { 17322 int i, j; 17323 int avail_count; 17324 int start_offset; 17325 int end_offset; 17326 size_t entry_len; 17327 char *bufp; 17328 char *local_buf; 17329 char *format_string; 17330 17331 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17332 17333 /* 17334 * In the debug version of the driver, this function is called from a 17335 * number of places which are NOPs in the release driver. 17336 * The debug driver therefore has additional methods of filtering 17337 * debug output. 17338 */ 17339 #ifdef SDDEBUG 17340 /* 17341 * In the debug version of the driver we can reduce the amount of debug 17342 * messages by setting sd_error_level to something other than 17343 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17344 * sd_component_mask. 17345 */ 17346 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17347 (sd_error_level != SCSI_ERR_ALL)) { 17348 return; 17349 } 17350 if (((sd_component_mask & comp) == 0) || 17351 (sd_error_level != SCSI_ERR_ALL)) { 17352 return; 17353 } 17354 #else 17355 if (sd_error_level != SCSI_ERR_ALL) { 17356 return; 17357 } 17358 #endif 17359 17360 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17361 bufp = local_buf; 17362 /* 17363 * Available length is the length of local_buf[], minus the 17364 * length of the title string, minus one for the ":", minus 17365 * one for the newline, minus one for the NULL terminator. 17366 * This gives the #bytes available for holding the printed 17367 * values from the given data buffer. 17368 */ 17369 if (fmt == SD_LOG_HEX) { 17370 format_string = sd_dump_format_string[0]; 17371 } else /* SD_LOG_CHAR */ { 17372 format_string = sd_dump_format_string[1]; 17373 } 17374 /* 17375 * Available count is the number of elements from the given 17376 * data buffer that we can fit into the available length. 17377 * This is based upon the size of the format string used. 17378 * Make one entry and find it's size. 17379 */ 17380 (void) sprintf(bufp, format_string, data[0]); 17381 entry_len = strlen(bufp); 17382 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17383 17384 j = 0; 17385 while (j < len) { 17386 bufp = local_buf; 17387 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17388 start_offset = j; 17389 17390 end_offset = start_offset + avail_count; 17391 17392 (void) sprintf(bufp, "%s:", title); 17393 bufp += strlen(bufp); 17394 for (i = start_offset; ((i < end_offset) && (j < len)); 17395 i++, j++) { 17396 (void) sprintf(bufp, format_string, data[i]); 17397 bufp += entry_len; 17398 } 17399 (void) sprintf(bufp, "\n"); 17400 17401 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17402 } 17403 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17404 } 17405 17406 /* 17407 * Function: sd_print_sense_msg 17408 * 17409 * Description: Log a message based upon the given sense data. 17410 * 17411 * Arguments: un - ptr to associated softstate 17412 * bp - ptr to buf(9S) for the command 17413 * arg - ptr to associate sd_sense_info struct 17414 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17415 * or SD_NO_RETRY_ISSUED 17416 * 17417 * Context: May be called from interrupt context 17418 */ 17419 17420 static void 17421 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17422 { 17423 struct sd_xbuf *xp; 17424 struct scsi_pkt *pktp; 17425 uint8_t *sensep; 17426 daddr_t request_blkno; 17427 diskaddr_t err_blkno; 17428 int severity; 17429 int pfa_flag; 17430 extern struct scsi_key_strings scsi_cmds[]; 17431 17432 ASSERT(un != NULL); 17433 ASSERT(mutex_owned(SD_MUTEX(un))); 17434 ASSERT(bp != NULL); 17435 xp = SD_GET_XBUF(bp); 17436 ASSERT(xp != NULL); 17437 pktp = SD_GET_PKTP(bp); 17438 ASSERT(pktp != NULL); 17439 ASSERT(arg != NULL); 17440 17441 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17442 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17443 17444 if ((code == SD_DELAYED_RETRY_ISSUED) || 17445 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17446 severity = SCSI_ERR_RETRYABLE; 17447 } 17448 17449 /* Use absolute block number for the request block number */ 17450 request_blkno = xp->xb_blkno; 17451 17452 /* 17453 * Now try to get the error block number from the sense data 17454 */ 17455 sensep = xp->xb_sense_data; 17456 17457 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17458 (uint64_t *)&err_blkno)) { 17459 /* 17460 * We retrieved the error block number from the information 17461 * portion of the sense data. 17462 * 17463 * For USCSI commands we are better off using the error 17464 * block no. as the requested block no. (This is the best 17465 * we can estimate.) 17466 */ 17467 if ((SD_IS_BUFIO(xp) == FALSE) && 17468 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17469 request_blkno = err_blkno; 17470 } 17471 } else { 17472 /* 17473 * Without the es_valid bit set (for fixed format) or an 17474 * information descriptor (for descriptor format) we cannot 17475 * be certain of the error blkno, so just use the 17476 * request_blkno. 17477 */ 17478 err_blkno = (diskaddr_t)request_blkno; 17479 } 17480 17481 /* 17482 * The following will log the buffer contents for the release driver 17483 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17484 * level is set to verbose. 17485 */ 17486 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17487 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17488 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17489 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17490 17491 if (pfa_flag == FALSE) { 17492 /* This is normally only set for USCSI */ 17493 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17494 return; 17495 } 17496 17497 if ((SD_IS_BUFIO(xp) == TRUE) && 17498 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17499 (severity < sd_error_level))) { 17500 return; 17501 } 17502 } 17503 17504 /* 17505 * Check for Sonoma Failover and keep a count of how many failed I/O's 17506 */ 17507 if ((SD_IS_LSI(un)) && 17508 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17509 (scsi_sense_asc(sensep) == 0x94) && 17510 (scsi_sense_ascq(sensep) == 0x01)) { 17511 un->un_sonoma_failure_count++; 17512 if (un->un_sonoma_failure_count > 1) { 17513 return; 17514 } 17515 } 17516 17517 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17518 request_blkno, err_blkno, scsi_cmds, 17519 (struct scsi_extended_sense *)sensep, 17520 un->un_additional_codes, NULL); 17521 } 17522 17523 /* 17524 * Function: sd_sense_key_no_sense 17525 * 17526 * Description: Recovery action when sense data was not received. 17527 * 17528 * Context: May be called from interrupt context 17529 */ 17530 17531 static void 17532 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17533 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17534 { 17535 struct sd_sense_info si; 17536 17537 ASSERT(un != NULL); 17538 ASSERT(mutex_owned(SD_MUTEX(un))); 17539 ASSERT(bp != NULL); 17540 ASSERT(xp != NULL); 17541 ASSERT(pktp != NULL); 17542 17543 si.ssi_severity = SCSI_ERR_FATAL; 17544 si.ssi_pfa_flag = FALSE; 17545 17546 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17547 17548 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17549 &si, EIO, (clock_t)0, NULL); 17550 } 17551 17552 17553 /* 17554 * Function: sd_sense_key_recoverable_error 17555 * 17556 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17557 * 17558 * Context: May be called from interrupt context 17559 */ 17560 17561 static void 17562 sd_sense_key_recoverable_error(struct sd_lun *un, 17563 uint8_t *sense_datap, 17564 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17565 { 17566 struct sd_sense_info si; 17567 uint8_t asc = scsi_sense_asc(sense_datap); 17568 17569 ASSERT(un != NULL); 17570 ASSERT(mutex_owned(SD_MUTEX(un))); 17571 ASSERT(bp != NULL); 17572 ASSERT(xp != NULL); 17573 ASSERT(pktp != NULL); 17574 17575 /* 17576 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17577 */ 17578 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17579 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17580 si.ssi_severity = SCSI_ERR_INFO; 17581 si.ssi_pfa_flag = TRUE; 17582 } else { 17583 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17584 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17585 si.ssi_severity = SCSI_ERR_RECOVERED; 17586 si.ssi_pfa_flag = FALSE; 17587 } 17588 17589 if (pktp->pkt_resid == 0) { 17590 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17591 sd_return_command(un, bp); 17592 return; 17593 } 17594 17595 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17596 &si, EIO, (clock_t)0, NULL); 17597 } 17598 17599 17600 17601 17602 /* 17603 * Function: sd_sense_key_not_ready 17604 * 17605 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17606 * 17607 * Context: May be called from interrupt context 17608 */ 17609 17610 static void 17611 sd_sense_key_not_ready(struct sd_lun *un, 17612 uint8_t *sense_datap, 17613 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17614 { 17615 struct sd_sense_info si; 17616 uint8_t asc = scsi_sense_asc(sense_datap); 17617 uint8_t ascq = scsi_sense_ascq(sense_datap); 17618 17619 ASSERT(un != NULL); 17620 ASSERT(mutex_owned(SD_MUTEX(un))); 17621 ASSERT(bp != NULL); 17622 ASSERT(xp != NULL); 17623 ASSERT(pktp != NULL); 17624 17625 si.ssi_severity = SCSI_ERR_FATAL; 17626 si.ssi_pfa_flag = FALSE; 17627 17628 /* 17629 * Update error stats after first NOT READY error. Disks may have 17630 * been powered down and may need to be restarted. For CDROMs, 17631 * report NOT READY errors only if media is present. 17632 */ 17633 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17634 (xp->xb_retry_count > 0)) { 17635 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17636 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17637 } 17638 17639 /* 17640 * Just fail if the "not ready" retry limit has been reached. 17641 */ 17642 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17643 /* Special check for error message printing for removables. */ 17644 if (un->un_f_has_removable_media && (asc == 0x04) && 17645 (ascq >= 0x04)) { 17646 si.ssi_severity = SCSI_ERR_ALL; 17647 } 17648 goto fail_command; 17649 } 17650 17651 /* 17652 * Check the ASC and ASCQ in the sense data as needed, to determine 17653 * what to do. 17654 */ 17655 switch (asc) { 17656 case 0x04: /* LOGICAL UNIT NOT READY */ 17657 /* 17658 * disk drives that don't spin up result in a very long delay 17659 * in format without warning messages. We will log a message 17660 * if the error level is set to verbose. 17661 */ 17662 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17664 "logical unit not ready, resetting disk\n"); 17665 } 17666 17667 /* 17668 * There are different requirements for CDROMs and disks for 17669 * the number of retries. If a CD-ROM is giving this, it is 17670 * probably reading TOC and is in the process of getting 17671 * ready, so we should keep on trying for a long time to make 17672 * sure that all types of media are taken in account (for 17673 * some media the drive takes a long time to read TOC). For 17674 * disks we do not want to retry this too many times as this 17675 * can cause a long hang in format when the drive refuses to 17676 * spin up (a very common failure). 17677 */ 17678 switch (ascq) { 17679 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17680 /* 17681 * Disk drives frequently refuse to spin up which 17682 * results in a very long hang in format without 17683 * warning messages. 17684 * 17685 * Note: This code preserves the legacy behavior of 17686 * comparing xb_retry_count against zero for fibre 17687 * channel targets instead of comparing against the 17688 * un_reset_retry_count value. The reason for this 17689 * discrepancy has been so utterly lost beneath the 17690 * Sands of Time that even Indiana Jones could not 17691 * find it. 17692 */ 17693 if (un->un_f_is_fibre == TRUE) { 17694 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17695 (xp->xb_retry_count > 0)) && 17696 (un->un_startstop_timeid == NULL)) { 17697 scsi_log(SD_DEVINFO(un), sd_label, 17698 CE_WARN, "logical unit not ready, " 17699 "resetting disk\n"); 17700 sd_reset_target(un, pktp); 17701 } 17702 } else { 17703 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17704 (xp->xb_retry_count > 17705 un->un_reset_retry_count)) && 17706 (un->un_startstop_timeid == NULL)) { 17707 scsi_log(SD_DEVINFO(un), sd_label, 17708 CE_WARN, "logical unit not ready, " 17709 "resetting disk\n"); 17710 sd_reset_target(un, pktp); 17711 } 17712 } 17713 break; 17714 17715 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17716 /* 17717 * If the target is in the process of becoming 17718 * ready, just proceed with the retry. This can 17719 * happen with CD-ROMs that take a long time to 17720 * read TOC after a power cycle or reset. 17721 */ 17722 goto do_retry; 17723 17724 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17725 break; 17726 17727 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17728 /* 17729 * Retries cannot help here so just fail right away. 17730 */ 17731 goto fail_command; 17732 17733 case 0x88: 17734 /* 17735 * Vendor-unique code for T3/T4: it indicates a 17736 * path problem in a mutipathed config, but as far as 17737 * the target driver is concerned it equates to a fatal 17738 * error, so we should just fail the command right away 17739 * (without printing anything to the console). If this 17740 * is not a T3/T4, fall thru to the default recovery 17741 * action. 17742 * T3/T4 is FC only, don't need to check is_fibre 17743 */ 17744 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17745 sd_return_failed_command(un, bp, EIO); 17746 return; 17747 } 17748 /* FALLTHRU */ 17749 17750 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17751 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17752 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17753 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17754 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17755 default: /* Possible future codes in SCSI spec? */ 17756 /* 17757 * For removable-media devices, do not retry if 17758 * ASCQ > 2 as these result mostly from USCSI commands 17759 * on MMC devices issued to check status of an 17760 * operation initiated in immediate mode. Also for 17761 * ASCQ >= 4 do not print console messages as these 17762 * mainly represent a user-initiated operation 17763 * instead of a system failure. 17764 */ 17765 if (un->un_f_has_removable_media) { 17766 si.ssi_severity = SCSI_ERR_ALL; 17767 goto fail_command; 17768 } 17769 break; 17770 } 17771 17772 /* 17773 * As part of our recovery attempt for the NOT READY 17774 * condition, we issue a START STOP UNIT command. However 17775 * we want to wait for a short delay before attempting this 17776 * as there may still be more commands coming back from the 17777 * target with the check condition. To do this we use 17778 * timeout(9F) to call sd_start_stop_unit_callback() after 17779 * the delay interval expires. (sd_start_stop_unit_callback() 17780 * dispatches sd_start_stop_unit_task(), which will issue 17781 * the actual START STOP UNIT command. The delay interval 17782 * is one-half of the delay that we will use to retry the 17783 * command that generated the NOT READY condition. 17784 * 17785 * Note that we could just dispatch sd_start_stop_unit_task() 17786 * from here and allow it to sleep for the delay interval, 17787 * but then we would be tying up the taskq thread 17788 * uncesessarily for the duration of the delay. 17789 * 17790 * Do not issue the START STOP UNIT if the current command 17791 * is already a START STOP UNIT. 17792 */ 17793 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17794 break; 17795 } 17796 17797 /* 17798 * Do not schedule the timeout if one is already pending. 17799 */ 17800 if (un->un_startstop_timeid != NULL) { 17801 SD_INFO(SD_LOG_ERROR, un, 17802 "sd_sense_key_not_ready: restart already issued to" 17803 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17804 ddi_get_instance(SD_DEVINFO(un))); 17805 break; 17806 } 17807 17808 /* 17809 * Schedule the START STOP UNIT command, then queue the command 17810 * for a retry. 17811 * 17812 * Note: A timeout is not scheduled for this retry because we 17813 * want the retry to be serial with the START_STOP_UNIT. The 17814 * retry will be started when the START_STOP_UNIT is completed 17815 * in sd_start_stop_unit_task. 17816 */ 17817 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17818 un, SD_BSY_TIMEOUT / 2); 17819 xp->xb_retry_count++; 17820 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17821 return; 17822 17823 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17824 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17825 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17826 "unit does not respond to selection\n"); 17827 } 17828 break; 17829 17830 case 0x3A: /* MEDIUM NOT PRESENT */ 17831 if (sd_error_level >= SCSI_ERR_FATAL) { 17832 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17833 "Caddy not inserted in drive\n"); 17834 } 17835 17836 sr_ejected(un); 17837 un->un_mediastate = DKIO_EJECTED; 17838 /* The state has changed, inform the media watch routines */ 17839 cv_broadcast(&un->un_state_cv); 17840 /* Just fail if no media is present in the drive. */ 17841 goto fail_command; 17842 17843 default: 17844 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17845 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17846 "Unit not Ready. Additional sense code 0x%x\n", 17847 asc); 17848 } 17849 break; 17850 } 17851 17852 do_retry: 17853 17854 /* 17855 * Retry the command, as some targets may report NOT READY for 17856 * several seconds after being reset. 17857 */ 17858 xp->xb_retry_count++; 17859 si.ssi_severity = SCSI_ERR_RETRYABLE; 17860 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17861 &si, EIO, SD_BSY_TIMEOUT, NULL); 17862 17863 return; 17864 17865 fail_command: 17866 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17867 sd_return_failed_command(un, bp, EIO); 17868 } 17869 17870 17871 17872 /* 17873 * Function: sd_sense_key_medium_or_hardware_error 17874 * 17875 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17876 * sense key. 17877 * 17878 * Context: May be called from interrupt context 17879 */ 17880 17881 static void 17882 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17883 uint8_t *sense_datap, 17884 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17885 { 17886 struct sd_sense_info si; 17887 uint8_t sense_key = scsi_sense_key(sense_datap); 17888 uint8_t asc = scsi_sense_asc(sense_datap); 17889 17890 ASSERT(un != NULL); 17891 ASSERT(mutex_owned(SD_MUTEX(un))); 17892 ASSERT(bp != NULL); 17893 ASSERT(xp != NULL); 17894 ASSERT(pktp != NULL); 17895 17896 si.ssi_severity = SCSI_ERR_FATAL; 17897 si.ssi_pfa_flag = FALSE; 17898 17899 if (sense_key == KEY_MEDIUM_ERROR) { 17900 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17901 } 17902 17903 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17904 17905 if ((un->un_reset_retry_count != 0) && 17906 (xp->xb_retry_count == un->un_reset_retry_count)) { 17907 mutex_exit(SD_MUTEX(un)); 17908 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17909 if (un->un_f_allow_bus_device_reset == TRUE) { 17910 17911 boolean_t try_resetting_target = B_TRUE; 17912 17913 /* 17914 * We need to be able to handle specific ASC when we are 17915 * handling a KEY_HARDWARE_ERROR. In particular 17916 * taking the default action of resetting the target may 17917 * not be the appropriate way to attempt recovery. 17918 * Resetting a target because of a single LUN failure 17919 * victimizes all LUNs on that target. 17920 * 17921 * This is true for the LSI arrays, if an LSI 17922 * array controller returns an ASC of 0x84 (LUN Dead) we 17923 * should trust it. 17924 */ 17925 17926 if (sense_key == KEY_HARDWARE_ERROR) { 17927 switch (asc) { 17928 case 0x84: 17929 if (SD_IS_LSI(un)) { 17930 try_resetting_target = B_FALSE; 17931 } 17932 break; 17933 default: 17934 break; 17935 } 17936 } 17937 17938 if (try_resetting_target == B_TRUE) { 17939 int reset_retval = 0; 17940 if (un->un_f_lun_reset_enabled == TRUE) { 17941 SD_TRACE(SD_LOG_IO_CORE, un, 17942 "sd_sense_key_medium_or_hardware_" 17943 "error: issuing RESET_LUN\n"); 17944 reset_retval = 17945 scsi_reset(SD_ADDRESS(un), 17946 RESET_LUN); 17947 } 17948 if (reset_retval == 0) { 17949 SD_TRACE(SD_LOG_IO_CORE, un, 17950 "sd_sense_key_medium_or_hardware_" 17951 "error: issuing RESET_TARGET\n"); 17952 (void) scsi_reset(SD_ADDRESS(un), 17953 RESET_TARGET); 17954 } 17955 } 17956 } 17957 mutex_enter(SD_MUTEX(un)); 17958 } 17959 17960 /* 17961 * This really ought to be a fatal error, but we will retry anyway 17962 * as some drives report this as a spurious error. 17963 */ 17964 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17965 &si, EIO, (clock_t)0, NULL); 17966 } 17967 17968 17969 17970 /* 17971 * Function: sd_sense_key_illegal_request 17972 * 17973 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17974 * 17975 * Context: May be called from interrupt context 17976 */ 17977 17978 static void 17979 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17980 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17981 { 17982 struct sd_sense_info si; 17983 17984 ASSERT(un != NULL); 17985 ASSERT(mutex_owned(SD_MUTEX(un))); 17986 ASSERT(bp != NULL); 17987 ASSERT(xp != NULL); 17988 ASSERT(pktp != NULL); 17989 17990 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17991 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17992 17993 si.ssi_severity = SCSI_ERR_INFO; 17994 si.ssi_pfa_flag = FALSE; 17995 17996 /* Pointless to retry if the target thinks it's an illegal request */ 17997 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17998 sd_return_failed_command(un, bp, EIO); 17999 } 18000 18001 18002 18003 18004 /* 18005 * Function: sd_sense_key_unit_attention 18006 * 18007 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18008 * 18009 * Context: May be called from interrupt context 18010 */ 18011 18012 static void 18013 sd_sense_key_unit_attention(struct sd_lun *un, 18014 uint8_t *sense_datap, 18015 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18016 { 18017 /* 18018 * For UNIT ATTENTION we allow retries for one minute. Devices 18019 * like Sonoma can return UNIT ATTENTION close to a minute 18020 * under certain conditions. 18021 */ 18022 int retry_check_flag = SD_RETRIES_UA; 18023 boolean_t kstat_updated = B_FALSE; 18024 struct sd_sense_info si; 18025 uint8_t asc = scsi_sense_asc(sense_datap); 18026 18027 ASSERT(un != NULL); 18028 ASSERT(mutex_owned(SD_MUTEX(un))); 18029 ASSERT(bp != NULL); 18030 ASSERT(xp != NULL); 18031 ASSERT(pktp != NULL); 18032 18033 si.ssi_severity = SCSI_ERR_INFO; 18034 si.ssi_pfa_flag = FALSE; 18035 18036 18037 switch (asc) { 18038 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18039 if (sd_report_pfa != 0) { 18040 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18041 si.ssi_pfa_flag = TRUE; 18042 retry_check_flag = SD_RETRIES_STANDARD; 18043 goto do_retry; 18044 } 18045 18046 break; 18047 18048 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18049 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18050 un->un_resvd_status |= 18051 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18052 } 18053 #ifdef _LP64 18054 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18055 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18056 un, KM_NOSLEEP) == 0) { 18057 /* 18058 * If we can't dispatch the task we'll just 18059 * live without descriptor sense. We can 18060 * try again on the next "unit attention" 18061 */ 18062 SD_ERROR(SD_LOG_ERROR, un, 18063 "sd_sense_key_unit_attention: " 18064 "Could not dispatch " 18065 "sd_reenable_dsense_task\n"); 18066 } 18067 } 18068 #endif /* _LP64 */ 18069 /* FALLTHRU */ 18070 18071 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18072 if (!un->un_f_has_removable_media) { 18073 break; 18074 } 18075 18076 /* 18077 * When we get a unit attention from a removable-media device, 18078 * it may be in a state that will take a long time to recover 18079 * (e.g., from a reset). Since we are executing in interrupt 18080 * context here, we cannot wait around for the device to come 18081 * back. So hand this command off to sd_media_change_task() 18082 * for deferred processing under taskq thread context. (Note 18083 * that the command still may be failed if a problem is 18084 * encountered at a later time.) 18085 */ 18086 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18087 KM_NOSLEEP) == 0) { 18088 /* 18089 * Cannot dispatch the request so fail the command. 18090 */ 18091 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18092 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18093 si.ssi_severity = SCSI_ERR_FATAL; 18094 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18095 sd_return_failed_command(un, bp, EIO); 18096 } 18097 18098 /* 18099 * If failed to dispatch sd_media_change_task(), we already 18100 * updated kstat. If succeed to dispatch sd_media_change_task(), 18101 * we should update kstat later if it encounters an error. So, 18102 * we update kstat_updated flag here. 18103 */ 18104 kstat_updated = B_TRUE; 18105 18106 /* 18107 * Either the command has been successfully dispatched to a 18108 * task Q for retrying, or the dispatch failed. In either case 18109 * do NOT retry again by calling sd_retry_command. This sets up 18110 * two retries of the same command and when one completes and 18111 * frees the resources the other will access freed memory, 18112 * a bad thing. 18113 */ 18114 return; 18115 18116 default: 18117 break; 18118 } 18119 18120 /* 18121 * Update kstat if we haven't done that. 18122 */ 18123 if (!kstat_updated) { 18124 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18125 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18126 } 18127 18128 do_retry: 18129 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18130 EIO, SD_UA_RETRY_DELAY, NULL); 18131 } 18132 18133 18134 18135 /* 18136 * Function: sd_sense_key_fail_command 18137 * 18138 * Description: Use to fail a command when we don't like the sense key that 18139 * was returned. 18140 * 18141 * Context: May be called from interrupt context 18142 */ 18143 18144 static void 18145 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18146 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18147 { 18148 struct sd_sense_info si; 18149 18150 ASSERT(un != NULL); 18151 ASSERT(mutex_owned(SD_MUTEX(un))); 18152 ASSERT(bp != NULL); 18153 ASSERT(xp != NULL); 18154 ASSERT(pktp != NULL); 18155 18156 si.ssi_severity = SCSI_ERR_FATAL; 18157 si.ssi_pfa_flag = FALSE; 18158 18159 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18160 sd_return_failed_command(un, bp, EIO); 18161 } 18162 18163 18164 18165 /* 18166 * Function: sd_sense_key_blank_check 18167 * 18168 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18169 * Has no monetary connotation. 18170 * 18171 * Context: May be called from interrupt context 18172 */ 18173 18174 static void 18175 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18176 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18177 { 18178 struct sd_sense_info si; 18179 18180 ASSERT(un != NULL); 18181 ASSERT(mutex_owned(SD_MUTEX(un))); 18182 ASSERT(bp != NULL); 18183 ASSERT(xp != NULL); 18184 ASSERT(pktp != NULL); 18185 18186 /* 18187 * Blank check is not fatal for removable devices, therefore 18188 * it does not require a console message. 18189 */ 18190 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18191 SCSI_ERR_FATAL; 18192 si.ssi_pfa_flag = FALSE; 18193 18194 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18195 sd_return_failed_command(un, bp, EIO); 18196 } 18197 18198 18199 18200 18201 /* 18202 * Function: sd_sense_key_aborted_command 18203 * 18204 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18205 * 18206 * Context: May be called from interrupt context 18207 */ 18208 18209 static void 18210 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18211 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18212 { 18213 struct sd_sense_info si; 18214 18215 ASSERT(un != NULL); 18216 ASSERT(mutex_owned(SD_MUTEX(un))); 18217 ASSERT(bp != NULL); 18218 ASSERT(xp != NULL); 18219 ASSERT(pktp != NULL); 18220 18221 si.ssi_severity = SCSI_ERR_FATAL; 18222 si.ssi_pfa_flag = FALSE; 18223 18224 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18225 18226 /* 18227 * This really ought to be a fatal error, but we will retry anyway 18228 * as some drives report this as a spurious error. 18229 */ 18230 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18231 &si, EIO, (clock_t)0, NULL); 18232 } 18233 18234 18235 18236 /* 18237 * Function: sd_sense_key_default 18238 * 18239 * Description: Default recovery action for several SCSI sense keys (basically 18240 * attempts a retry). 18241 * 18242 * Context: May be called from interrupt context 18243 */ 18244 18245 static void 18246 sd_sense_key_default(struct sd_lun *un, 18247 uint8_t *sense_datap, 18248 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18249 { 18250 struct sd_sense_info si; 18251 uint8_t sense_key = scsi_sense_key(sense_datap); 18252 18253 ASSERT(un != NULL); 18254 ASSERT(mutex_owned(SD_MUTEX(un))); 18255 ASSERT(bp != NULL); 18256 ASSERT(xp != NULL); 18257 ASSERT(pktp != NULL); 18258 18259 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18260 18261 /* 18262 * Undecoded sense key. Attempt retries and hope that will fix 18263 * the problem. Otherwise, we're dead. 18264 */ 18265 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18266 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18267 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18268 } 18269 18270 si.ssi_severity = SCSI_ERR_FATAL; 18271 si.ssi_pfa_flag = FALSE; 18272 18273 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18274 &si, EIO, (clock_t)0, NULL); 18275 } 18276 18277 18278 18279 /* 18280 * Function: sd_print_retry_msg 18281 * 18282 * Description: Print a message indicating the retry action being taken. 18283 * 18284 * Arguments: un - ptr to associated softstate 18285 * bp - ptr to buf(9S) for the command 18286 * arg - not used. 18287 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18288 * or SD_NO_RETRY_ISSUED 18289 * 18290 * Context: May be called from interrupt context 18291 */ 18292 /* ARGSUSED */ 18293 static void 18294 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18295 { 18296 struct sd_xbuf *xp; 18297 struct scsi_pkt *pktp; 18298 char *reasonp; 18299 char *msgp; 18300 18301 ASSERT(un != NULL); 18302 ASSERT(mutex_owned(SD_MUTEX(un))); 18303 ASSERT(bp != NULL); 18304 pktp = SD_GET_PKTP(bp); 18305 ASSERT(pktp != NULL); 18306 xp = SD_GET_XBUF(bp); 18307 ASSERT(xp != NULL); 18308 18309 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18310 mutex_enter(&un->un_pm_mutex); 18311 if ((un->un_state == SD_STATE_SUSPENDED) || 18312 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18313 (pktp->pkt_flags & FLAG_SILENT)) { 18314 mutex_exit(&un->un_pm_mutex); 18315 goto update_pkt_reason; 18316 } 18317 mutex_exit(&un->un_pm_mutex); 18318 18319 /* 18320 * Suppress messages if they are all the same pkt_reason; with 18321 * TQ, many (up to 256) are returned with the same pkt_reason. 18322 * If we are in panic, then suppress the retry messages. 18323 */ 18324 switch (flag) { 18325 case SD_NO_RETRY_ISSUED: 18326 msgp = "giving up"; 18327 break; 18328 case SD_IMMEDIATE_RETRY_ISSUED: 18329 case SD_DELAYED_RETRY_ISSUED: 18330 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18331 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18332 (sd_error_level != SCSI_ERR_ALL))) { 18333 return; 18334 } 18335 msgp = "retrying command"; 18336 break; 18337 default: 18338 goto update_pkt_reason; 18339 } 18340 18341 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18342 scsi_rname(pktp->pkt_reason)); 18343 18344 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18345 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18346 18347 update_pkt_reason: 18348 /* 18349 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18350 * This is to prevent multiple console messages for the same failure 18351 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18352 * when the command is retried successfully because there still may be 18353 * more commands coming back with the same value of pktp->pkt_reason. 18354 */ 18355 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18356 un->un_last_pkt_reason = pktp->pkt_reason; 18357 } 18358 } 18359 18360 18361 /* 18362 * Function: sd_print_cmd_incomplete_msg 18363 * 18364 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18365 * 18366 * Arguments: un - ptr to associated softstate 18367 * bp - ptr to buf(9S) for the command 18368 * arg - passed to sd_print_retry_msg() 18369 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18370 * or SD_NO_RETRY_ISSUED 18371 * 18372 * Context: May be called from interrupt context 18373 */ 18374 18375 static void 18376 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18377 int code) 18378 { 18379 dev_info_t *dip; 18380 18381 ASSERT(un != NULL); 18382 ASSERT(mutex_owned(SD_MUTEX(un))); 18383 ASSERT(bp != NULL); 18384 18385 switch (code) { 18386 case SD_NO_RETRY_ISSUED: 18387 /* Command was failed. Someone turned off this target? */ 18388 if (un->un_state != SD_STATE_OFFLINE) { 18389 /* 18390 * Suppress message if we are detaching and 18391 * device has been disconnected 18392 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18393 * private interface and not part of the DDI 18394 */ 18395 dip = un->un_sd->sd_dev; 18396 if (!(DEVI_IS_DETACHING(dip) && 18397 DEVI_IS_DEVICE_REMOVED(dip))) { 18398 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18399 "disk not responding to selection\n"); 18400 } 18401 New_state(un, SD_STATE_OFFLINE); 18402 } 18403 break; 18404 18405 case SD_DELAYED_RETRY_ISSUED: 18406 case SD_IMMEDIATE_RETRY_ISSUED: 18407 default: 18408 /* Command was successfully queued for retry */ 18409 sd_print_retry_msg(un, bp, arg, code); 18410 break; 18411 } 18412 } 18413 18414 18415 /* 18416 * Function: sd_pkt_reason_cmd_incomplete 18417 * 18418 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18419 * 18420 * Context: May be called from interrupt context 18421 */ 18422 18423 static void 18424 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18425 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18426 { 18427 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18428 18429 ASSERT(un != NULL); 18430 ASSERT(mutex_owned(SD_MUTEX(un))); 18431 ASSERT(bp != NULL); 18432 ASSERT(xp != NULL); 18433 ASSERT(pktp != NULL); 18434 18435 /* Do not do a reset if selection did not complete */ 18436 /* Note: Should this not just check the bit? */ 18437 if (pktp->pkt_state != STATE_GOT_BUS) { 18438 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18439 sd_reset_target(un, pktp); 18440 } 18441 18442 /* 18443 * If the target was not successfully selected, then set 18444 * SD_RETRIES_FAILFAST to indicate that we lost communication 18445 * with the target, and further retries and/or commands are 18446 * likely to take a long time. 18447 */ 18448 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18449 flag |= SD_RETRIES_FAILFAST; 18450 } 18451 18452 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18453 18454 sd_retry_command(un, bp, flag, 18455 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18456 } 18457 18458 18459 18460 /* 18461 * Function: sd_pkt_reason_cmd_tran_err 18462 * 18463 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18464 * 18465 * Context: May be called from interrupt context 18466 */ 18467 18468 static void 18469 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18470 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18471 { 18472 ASSERT(un != NULL); 18473 ASSERT(mutex_owned(SD_MUTEX(un))); 18474 ASSERT(bp != NULL); 18475 ASSERT(xp != NULL); 18476 ASSERT(pktp != NULL); 18477 18478 /* 18479 * Do not reset if we got a parity error, or if 18480 * selection did not complete. 18481 */ 18482 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18483 /* Note: Should this not just check the bit for pkt_state? */ 18484 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18485 (pktp->pkt_state != STATE_GOT_BUS)) { 18486 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18487 sd_reset_target(un, pktp); 18488 } 18489 18490 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18491 18492 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18493 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18494 } 18495 18496 18497 18498 /* 18499 * Function: sd_pkt_reason_cmd_reset 18500 * 18501 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18502 * 18503 * Context: May be called from interrupt context 18504 */ 18505 18506 static void 18507 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18508 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18509 { 18510 ASSERT(un != NULL); 18511 ASSERT(mutex_owned(SD_MUTEX(un))); 18512 ASSERT(bp != NULL); 18513 ASSERT(xp != NULL); 18514 ASSERT(pktp != NULL); 18515 18516 /* The target may still be running the command, so try to reset. */ 18517 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18518 sd_reset_target(un, pktp); 18519 18520 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18521 18522 /* 18523 * If pkt_reason is CMD_RESET chances are that this pkt got 18524 * reset because another target on this bus caused it. The target 18525 * that caused it should get CMD_TIMEOUT with pkt_statistics 18526 * of STAT_TIMEOUT/STAT_DEV_RESET. 18527 */ 18528 18529 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18530 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18531 } 18532 18533 18534 18535 18536 /* 18537 * Function: sd_pkt_reason_cmd_aborted 18538 * 18539 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18540 * 18541 * Context: May be called from interrupt context 18542 */ 18543 18544 static void 18545 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18546 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18547 { 18548 ASSERT(un != NULL); 18549 ASSERT(mutex_owned(SD_MUTEX(un))); 18550 ASSERT(bp != NULL); 18551 ASSERT(xp != NULL); 18552 ASSERT(pktp != NULL); 18553 18554 /* The target may still be running the command, so try to reset. */ 18555 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18556 sd_reset_target(un, pktp); 18557 18558 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18559 18560 /* 18561 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18562 * aborted because another target on this bus caused it. The target 18563 * that caused it should get CMD_TIMEOUT with pkt_statistics 18564 * of STAT_TIMEOUT/STAT_DEV_RESET. 18565 */ 18566 18567 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18568 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18569 } 18570 18571 18572 18573 /* 18574 * Function: sd_pkt_reason_cmd_timeout 18575 * 18576 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18577 * 18578 * Context: May be called from interrupt context 18579 */ 18580 18581 static void 18582 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18583 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18584 { 18585 ASSERT(un != NULL); 18586 ASSERT(mutex_owned(SD_MUTEX(un))); 18587 ASSERT(bp != NULL); 18588 ASSERT(xp != NULL); 18589 ASSERT(pktp != NULL); 18590 18591 18592 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18593 sd_reset_target(un, pktp); 18594 18595 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18596 18597 /* 18598 * A command timeout indicates that we could not establish 18599 * communication with the target, so set SD_RETRIES_FAILFAST 18600 * as further retries/commands are likely to take a long time. 18601 */ 18602 sd_retry_command(un, bp, 18603 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18604 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18605 } 18606 18607 18608 18609 /* 18610 * Function: sd_pkt_reason_cmd_unx_bus_free 18611 * 18612 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18613 * 18614 * Context: May be called from interrupt context 18615 */ 18616 18617 static void 18618 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18619 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18620 { 18621 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18622 18623 ASSERT(un != NULL); 18624 ASSERT(mutex_owned(SD_MUTEX(un))); 18625 ASSERT(bp != NULL); 18626 ASSERT(xp != NULL); 18627 ASSERT(pktp != NULL); 18628 18629 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18630 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18631 18632 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18633 sd_print_retry_msg : NULL; 18634 18635 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18636 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18637 } 18638 18639 18640 /* 18641 * Function: sd_pkt_reason_cmd_tag_reject 18642 * 18643 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18644 * 18645 * Context: May be called from interrupt context 18646 */ 18647 18648 static void 18649 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18650 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18651 { 18652 ASSERT(un != NULL); 18653 ASSERT(mutex_owned(SD_MUTEX(un))); 18654 ASSERT(bp != NULL); 18655 ASSERT(xp != NULL); 18656 ASSERT(pktp != NULL); 18657 18658 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18659 pktp->pkt_flags = 0; 18660 un->un_tagflags = 0; 18661 if (un->un_f_opt_queueing == TRUE) { 18662 un->un_throttle = min(un->un_throttle, 3); 18663 } else { 18664 un->un_throttle = 1; 18665 } 18666 mutex_exit(SD_MUTEX(un)); 18667 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18668 mutex_enter(SD_MUTEX(un)); 18669 18670 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18671 18672 /* Legacy behavior not to check retry counts here. */ 18673 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18674 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18675 } 18676 18677 18678 /* 18679 * Function: sd_pkt_reason_default 18680 * 18681 * Description: Default recovery actions for SCSA pkt_reason values that 18682 * do not have more explicit recovery actions. 18683 * 18684 * Context: May be called from interrupt context 18685 */ 18686 18687 static void 18688 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18689 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18690 { 18691 ASSERT(un != NULL); 18692 ASSERT(mutex_owned(SD_MUTEX(un))); 18693 ASSERT(bp != NULL); 18694 ASSERT(xp != NULL); 18695 ASSERT(pktp != NULL); 18696 18697 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18698 sd_reset_target(un, pktp); 18699 18700 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18701 18702 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18703 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18704 } 18705 18706 18707 18708 /* 18709 * Function: sd_pkt_status_check_condition 18710 * 18711 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18712 * 18713 * Context: May be called from interrupt context 18714 */ 18715 18716 static void 18717 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18718 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18719 { 18720 ASSERT(un != NULL); 18721 ASSERT(mutex_owned(SD_MUTEX(un))); 18722 ASSERT(bp != NULL); 18723 ASSERT(xp != NULL); 18724 ASSERT(pktp != NULL); 18725 18726 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18727 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18728 18729 /* 18730 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18731 * command will be retried after the request sense). Otherwise, retry 18732 * the command. Note: we are issuing the request sense even though the 18733 * retry limit may have been reached for the failed command. 18734 */ 18735 if (un->un_f_arq_enabled == FALSE) { 18736 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18737 "no ARQ, sending request sense command\n"); 18738 sd_send_request_sense_command(un, bp, pktp); 18739 } else { 18740 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18741 "ARQ,retrying request sense command\n"); 18742 #if defined(__i386) || defined(__amd64) 18743 /* 18744 * The SD_RETRY_DELAY value need to be adjusted here 18745 * when SD_RETRY_DELAY change in sddef.h 18746 */ 18747 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18748 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18749 NULL); 18750 #else 18751 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18752 EIO, SD_RETRY_DELAY, NULL); 18753 #endif 18754 } 18755 18756 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18757 } 18758 18759 18760 /* 18761 * Function: sd_pkt_status_busy 18762 * 18763 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18764 * 18765 * Context: May be called from interrupt context 18766 */ 18767 18768 static void 18769 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18770 struct scsi_pkt *pktp) 18771 { 18772 ASSERT(un != NULL); 18773 ASSERT(mutex_owned(SD_MUTEX(un))); 18774 ASSERT(bp != NULL); 18775 ASSERT(xp != NULL); 18776 ASSERT(pktp != NULL); 18777 18778 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18779 "sd_pkt_status_busy: entry\n"); 18780 18781 /* If retries are exhausted, just fail the command. */ 18782 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18783 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18784 "device busy too long\n"); 18785 sd_return_failed_command(un, bp, EIO); 18786 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18787 "sd_pkt_status_busy: exit\n"); 18788 return; 18789 } 18790 xp->xb_retry_count++; 18791 18792 /* 18793 * Try to reset the target. However, we do not want to perform 18794 * more than one reset if the device continues to fail. The reset 18795 * will be performed when the retry count reaches the reset 18796 * threshold. This threshold should be set such that at least 18797 * one retry is issued before the reset is performed. 18798 */ 18799 if (xp->xb_retry_count == 18800 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18801 int rval = 0; 18802 mutex_exit(SD_MUTEX(un)); 18803 if (un->un_f_allow_bus_device_reset == TRUE) { 18804 /* 18805 * First try to reset the LUN; if we cannot then 18806 * try to reset the target. 18807 */ 18808 if (un->un_f_lun_reset_enabled == TRUE) { 18809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18810 "sd_pkt_status_busy: RESET_LUN\n"); 18811 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18812 } 18813 if (rval == 0) { 18814 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18815 "sd_pkt_status_busy: RESET_TARGET\n"); 18816 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18817 } 18818 } 18819 if (rval == 0) { 18820 /* 18821 * If the RESET_LUN and/or RESET_TARGET failed, 18822 * try RESET_ALL 18823 */ 18824 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18825 "sd_pkt_status_busy: RESET_ALL\n"); 18826 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18827 } 18828 mutex_enter(SD_MUTEX(un)); 18829 if (rval == 0) { 18830 /* 18831 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18832 * At this point we give up & fail the command. 18833 */ 18834 sd_return_failed_command(un, bp, EIO); 18835 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18836 "sd_pkt_status_busy: exit (failed cmd)\n"); 18837 return; 18838 } 18839 } 18840 18841 /* 18842 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18843 * we have already checked the retry counts above. 18844 */ 18845 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18846 EIO, SD_BSY_TIMEOUT, NULL); 18847 18848 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18849 "sd_pkt_status_busy: exit\n"); 18850 } 18851 18852 18853 /* 18854 * Function: sd_pkt_status_reservation_conflict 18855 * 18856 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18857 * command status. 18858 * 18859 * Context: May be called from interrupt context 18860 */ 18861 18862 static void 18863 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18864 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18865 { 18866 ASSERT(un != NULL); 18867 ASSERT(mutex_owned(SD_MUTEX(un))); 18868 ASSERT(bp != NULL); 18869 ASSERT(xp != NULL); 18870 ASSERT(pktp != NULL); 18871 18872 /* 18873 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18874 * conflict could be due to various reasons like incorrect keys, not 18875 * registered or not reserved etc. So, we return EACCES to the caller. 18876 */ 18877 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18878 int cmd = SD_GET_PKT_OPCODE(pktp); 18879 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18880 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18881 sd_return_failed_command(un, bp, EACCES); 18882 return; 18883 } 18884 } 18885 18886 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18887 18888 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18889 if (sd_failfast_enable != 0) { 18890 /* By definition, we must panic here.... */ 18891 sd_panic_for_res_conflict(un); 18892 /*NOTREACHED*/ 18893 } 18894 SD_ERROR(SD_LOG_IO, un, 18895 "sd_handle_resv_conflict: Disk Reserved\n"); 18896 sd_return_failed_command(un, bp, EACCES); 18897 return; 18898 } 18899 18900 /* 18901 * 1147670: retry only if sd_retry_on_reservation_conflict 18902 * property is set (default is 1). Retries will not succeed 18903 * on a disk reserved by another initiator. HA systems 18904 * may reset this via sd.conf to avoid these retries. 18905 * 18906 * Note: The legacy return code for this failure is EIO, however EACCES 18907 * seems more appropriate for a reservation conflict. 18908 */ 18909 if (sd_retry_on_reservation_conflict == 0) { 18910 SD_ERROR(SD_LOG_IO, un, 18911 "sd_handle_resv_conflict: Device Reserved\n"); 18912 sd_return_failed_command(un, bp, EIO); 18913 return; 18914 } 18915 18916 /* 18917 * Retry the command if we can. 18918 * 18919 * Note: The legacy return code for this failure is EIO, however EACCES 18920 * seems more appropriate for a reservation conflict. 18921 */ 18922 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18923 (clock_t)2, NULL); 18924 } 18925 18926 18927 18928 /* 18929 * Function: sd_pkt_status_qfull 18930 * 18931 * Description: Handle a QUEUE FULL condition from the target. This can 18932 * occur if the HBA does not handle the queue full condition. 18933 * (Basically this means third-party HBAs as Sun HBAs will 18934 * handle the queue full condition.) Note that if there are 18935 * some commands already in the transport, then the queue full 18936 * has occurred because the queue for this nexus is actually 18937 * full. If there are no commands in the transport, then the 18938 * queue full is resulting from some other initiator or lun 18939 * consuming all the resources at the target. 18940 * 18941 * Context: May be called from interrupt context 18942 */ 18943 18944 static void 18945 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18946 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18947 { 18948 ASSERT(un != NULL); 18949 ASSERT(mutex_owned(SD_MUTEX(un))); 18950 ASSERT(bp != NULL); 18951 ASSERT(xp != NULL); 18952 ASSERT(pktp != NULL); 18953 18954 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18955 "sd_pkt_status_qfull: entry\n"); 18956 18957 /* 18958 * Just lower the QFULL throttle and retry the command. Note that 18959 * we do not limit the number of retries here. 18960 */ 18961 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18962 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18963 SD_RESTART_TIMEOUT, NULL); 18964 18965 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18966 "sd_pkt_status_qfull: exit\n"); 18967 } 18968 18969 18970 /* 18971 * Function: sd_reset_target 18972 * 18973 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18974 * RESET_TARGET, or RESET_ALL. 18975 * 18976 * Context: May be called under interrupt context. 18977 */ 18978 18979 static void 18980 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18981 { 18982 int rval = 0; 18983 18984 ASSERT(un != NULL); 18985 ASSERT(mutex_owned(SD_MUTEX(un))); 18986 ASSERT(pktp != NULL); 18987 18988 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18989 18990 /* 18991 * No need to reset if the transport layer has already done so. 18992 */ 18993 if ((pktp->pkt_statistics & 18994 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18995 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18996 "sd_reset_target: no reset\n"); 18997 return; 18998 } 18999 19000 mutex_exit(SD_MUTEX(un)); 19001 19002 if (un->un_f_allow_bus_device_reset == TRUE) { 19003 if (un->un_f_lun_reset_enabled == TRUE) { 19004 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19005 "sd_reset_target: RESET_LUN\n"); 19006 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19007 } 19008 if (rval == 0) { 19009 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19010 "sd_reset_target: RESET_TARGET\n"); 19011 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19012 } 19013 } 19014 19015 if (rval == 0) { 19016 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19017 "sd_reset_target: RESET_ALL\n"); 19018 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19019 } 19020 19021 mutex_enter(SD_MUTEX(un)); 19022 19023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19024 } 19025 19026 19027 /* 19028 * Function: sd_media_change_task 19029 * 19030 * Description: Recovery action for CDROM to become available. 19031 * 19032 * Context: Executes in a taskq() thread context 19033 */ 19034 19035 static void 19036 sd_media_change_task(void *arg) 19037 { 19038 struct scsi_pkt *pktp = arg; 19039 struct sd_lun *un; 19040 struct buf *bp; 19041 struct sd_xbuf *xp; 19042 int err = 0; 19043 int retry_count = 0; 19044 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19045 struct sd_sense_info si; 19046 19047 ASSERT(pktp != NULL); 19048 bp = (struct buf *)pktp->pkt_private; 19049 ASSERT(bp != NULL); 19050 xp = SD_GET_XBUF(bp); 19051 ASSERT(xp != NULL); 19052 un = SD_GET_UN(bp); 19053 ASSERT(un != NULL); 19054 ASSERT(!mutex_owned(SD_MUTEX(un))); 19055 ASSERT(un->un_f_monitor_media_state); 19056 19057 si.ssi_severity = SCSI_ERR_INFO; 19058 si.ssi_pfa_flag = FALSE; 19059 19060 /* 19061 * When a reset is issued on a CDROM, it takes a long time to 19062 * recover. First few attempts to read capacity and other things 19063 * related to handling unit attention fail (with a ASC 0x4 and 19064 * ASCQ 0x1). In that case we want to do enough retries and we want 19065 * to limit the retries in other cases of genuine failures like 19066 * no media in drive. 19067 */ 19068 while (retry_count++ < retry_limit) { 19069 if ((err = sd_handle_mchange(un)) == 0) { 19070 break; 19071 } 19072 if (err == EAGAIN) { 19073 retry_limit = SD_UNIT_ATTENTION_RETRY; 19074 } 19075 /* Sleep for 0.5 sec. & try again */ 19076 delay(drv_usectohz(500000)); 19077 } 19078 19079 /* 19080 * Dispatch (retry or fail) the original command here, 19081 * along with appropriate console messages.... 19082 * 19083 * Must grab the mutex before calling sd_retry_command, 19084 * sd_print_sense_msg and sd_return_failed_command. 19085 */ 19086 mutex_enter(SD_MUTEX(un)); 19087 if (err != SD_CMD_SUCCESS) { 19088 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19089 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19090 si.ssi_severity = SCSI_ERR_FATAL; 19091 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19092 sd_return_failed_command(un, bp, EIO); 19093 } else { 19094 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19095 &si, EIO, (clock_t)0, NULL); 19096 } 19097 mutex_exit(SD_MUTEX(un)); 19098 } 19099 19100 19101 19102 /* 19103 * Function: sd_handle_mchange 19104 * 19105 * Description: Perform geometry validation & other recovery when CDROM 19106 * has been removed from drive. 19107 * 19108 * Return Code: 0 for success 19109 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19110 * sd_send_scsi_READ_CAPACITY() 19111 * 19112 * Context: Executes in a taskq() thread context 19113 */ 19114 19115 static int 19116 sd_handle_mchange(struct sd_lun *un) 19117 { 19118 uint64_t capacity; 19119 uint32_t lbasize; 19120 int rval; 19121 19122 ASSERT(!mutex_owned(SD_MUTEX(un))); 19123 ASSERT(un->un_f_monitor_media_state); 19124 19125 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 19126 SD_PATH_DIRECT_PRIORITY)) != 0) { 19127 return (rval); 19128 } 19129 19130 mutex_enter(SD_MUTEX(un)); 19131 sd_update_block_info(un, lbasize, capacity); 19132 19133 if (un->un_errstats != NULL) { 19134 struct sd_errstats *stp = 19135 (struct sd_errstats *)un->un_errstats->ks_data; 19136 stp->sd_capacity.value.ui64 = (uint64_t) 19137 ((uint64_t)un->un_blockcount * 19138 (uint64_t)un->un_tgt_blocksize); 19139 } 19140 19141 /* 19142 * Note: Maybe let the strategy/partitioning chain worry about getting 19143 * valid geometry. 19144 */ 19145 un->un_f_geometry_is_valid = FALSE; 19146 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19147 if (un->un_f_geometry_is_valid == FALSE) { 19148 mutex_exit(SD_MUTEX(un)); 19149 return (EIO); 19150 } 19151 19152 mutex_exit(SD_MUTEX(un)); 19153 19154 /* 19155 * Try to lock the door 19156 */ 19157 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19158 SD_PATH_DIRECT_PRIORITY)); 19159 } 19160 19161 19162 /* 19163 * Function: sd_send_scsi_DOORLOCK 19164 * 19165 * Description: Issue the scsi DOOR LOCK command 19166 * 19167 * Arguments: un - pointer to driver soft state (unit) structure for 19168 * this target. 19169 * flag - SD_REMOVAL_ALLOW 19170 * SD_REMOVAL_PREVENT 19171 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19172 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19173 * to use the USCSI "direct" chain and bypass the normal 19174 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19175 * command is issued as part of an error recovery action. 19176 * 19177 * Return Code: 0 - Success 19178 * errno return code from sd_send_scsi_cmd() 19179 * 19180 * Context: Can sleep. 19181 */ 19182 19183 static int 19184 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19185 { 19186 union scsi_cdb cdb; 19187 struct uscsi_cmd ucmd_buf; 19188 struct scsi_extended_sense sense_buf; 19189 int status; 19190 19191 ASSERT(un != NULL); 19192 ASSERT(!mutex_owned(SD_MUTEX(un))); 19193 19194 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19195 19196 /* already determined doorlock is not supported, fake success */ 19197 if (un->un_f_doorlock_supported == FALSE) { 19198 return (0); 19199 } 19200 19201 /* 19202 * If we are ejecting and see an SD_REMOVAL_PREVENT 19203 * ignore the command so we can complete the eject 19204 * operation. 19205 */ 19206 if (flag == SD_REMOVAL_PREVENT) { 19207 mutex_enter(SD_MUTEX(un)); 19208 if (un->un_f_ejecting == TRUE) { 19209 mutex_exit(SD_MUTEX(un)); 19210 return (EAGAIN); 19211 } 19212 mutex_exit(SD_MUTEX(un)); 19213 } 19214 19215 bzero(&cdb, sizeof (cdb)); 19216 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19217 19218 cdb.scc_cmd = SCMD_DOORLOCK; 19219 cdb.cdb_opaque[4] = (uchar_t)flag; 19220 19221 ucmd_buf.uscsi_cdb = (char *)&cdb; 19222 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19223 ucmd_buf.uscsi_bufaddr = NULL; 19224 ucmd_buf.uscsi_buflen = 0; 19225 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19226 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19227 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19228 ucmd_buf.uscsi_timeout = 15; 19229 19230 SD_TRACE(SD_LOG_IO, un, 19231 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19232 19233 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19234 UIO_SYSSPACE, path_flag); 19235 19236 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19237 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19238 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19239 /* fake success and skip subsequent doorlock commands */ 19240 un->un_f_doorlock_supported = FALSE; 19241 return (0); 19242 } 19243 19244 return (status); 19245 } 19246 19247 /* 19248 * Function: sd_send_scsi_READ_CAPACITY 19249 * 19250 * Description: This routine uses the scsi READ CAPACITY command to determine 19251 * the device capacity in number of blocks and the device native 19252 * block size. If this function returns a failure, then the 19253 * values in *capp and *lbap are undefined. If the capacity 19254 * returned is 0xffffffff then the lun is too large for a 19255 * normal READ CAPACITY command and the results of a 19256 * READ CAPACITY 16 will be used instead. 19257 * 19258 * Arguments: un - ptr to soft state struct for the target 19259 * capp - ptr to unsigned 64-bit variable to receive the 19260 * capacity value from the command. 19261 * lbap - ptr to unsigned 32-bit varaible to receive the 19262 * block size value from the command 19263 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19264 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19265 * to use the USCSI "direct" chain and bypass the normal 19266 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19267 * command is issued as part of an error recovery action. 19268 * 19269 * Return Code: 0 - Success 19270 * EIO - IO error 19271 * EACCES - Reservation conflict detected 19272 * EAGAIN - Device is becoming ready 19273 * errno return code from sd_send_scsi_cmd() 19274 * 19275 * Context: Can sleep. Blocks until command completes. 19276 */ 19277 19278 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19279 19280 static int 19281 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19282 int path_flag) 19283 { 19284 struct scsi_extended_sense sense_buf; 19285 struct uscsi_cmd ucmd_buf; 19286 union scsi_cdb cdb; 19287 uint32_t *capacity_buf; 19288 uint64_t capacity; 19289 uint32_t lbasize; 19290 int status; 19291 19292 ASSERT(un != NULL); 19293 ASSERT(!mutex_owned(SD_MUTEX(un))); 19294 ASSERT(capp != NULL); 19295 ASSERT(lbap != NULL); 19296 19297 SD_TRACE(SD_LOG_IO, un, 19298 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19299 19300 /* 19301 * First send a READ_CAPACITY command to the target. 19302 * (This command is mandatory under SCSI-2.) 19303 * 19304 * Set up the CDB for the READ_CAPACITY command. The Partial 19305 * Medium Indicator bit is cleared. The address field must be 19306 * zero if the PMI bit is zero. 19307 */ 19308 bzero(&cdb, sizeof (cdb)); 19309 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19310 19311 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19312 19313 cdb.scc_cmd = SCMD_READ_CAPACITY; 19314 19315 ucmd_buf.uscsi_cdb = (char *)&cdb; 19316 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19317 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19318 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19319 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19320 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19321 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19322 ucmd_buf.uscsi_timeout = 60; 19323 19324 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19325 UIO_SYSSPACE, path_flag); 19326 19327 switch (status) { 19328 case 0: 19329 /* Return failure if we did not get valid capacity data. */ 19330 if (ucmd_buf.uscsi_resid != 0) { 19331 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19332 return (EIO); 19333 } 19334 19335 /* 19336 * Read capacity and block size from the READ CAPACITY 10 data. 19337 * This data may be adjusted later due to device specific 19338 * issues. 19339 * 19340 * According to the SCSI spec, the READ CAPACITY 10 19341 * command returns the following: 19342 * 19343 * bytes 0-3: Maximum logical block address available. 19344 * (MSB in byte:0 & LSB in byte:3) 19345 * 19346 * bytes 4-7: Block length in bytes 19347 * (MSB in byte:4 & LSB in byte:7) 19348 * 19349 */ 19350 capacity = BE_32(capacity_buf[0]); 19351 lbasize = BE_32(capacity_buf[1]); 19352 19353 /* 19354 * Done with capacity_buf 19355 */ 19356 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19357 19358 /* 19359 * if the reported capacity is set to all 0xf's, then 19360 * this disk is too large and requires SBC-2 commands. 19361 * Reissue the request using READ CAPACITY 16. 19362 */ 19363 if (capacity == 0xffffffff) { 19364 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19365 &lbasize, path_flag); 19366 if (status != 0) { 19367 return (status); 19368 } 19369 } 19370 break; /* Success! */ 19371 case EIO: 19372 switch (ucmd_buf.uscsi_status) { 19373 case STATUS_RESERVATION_CONFLICT: 19374 status = EACCES; 19375 break; 19376 case STATUS_CHECK: 19377 /* 19378 * Check condition; look for ASC/ASCQ of 0x04/0x01 19379 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19380 */ 19381 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19382 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19383 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19384 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19385 return (EAGAIN); 19386 } 19387 break; 19388 default: 19389 break; 19390 } 19391 /* FALLTHRU */ 19392 default: 19393 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19394 return (status); 19395 } 19396 19397 /* 19398 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19399 * (2352 and 0 are common) so for these devices always force the value 19400 * to 2048 as required by the ATAPI specs. 19401 */ 19402 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19403 lbasize = 2048; 19404 } 19405 19406 /* 19407 * Get the maximum LBA value from the READ CAPACITY data. 19408 * Here we assume that the Partial Medium Indicator (PMI) bit 19409 * was cleared when issuing the command. This means that the LBA 19410 * returned from the device is the LBA of the last logical block 19411 * on the logical unit. The actual logical block count will be 19412 * this value plus one. 19413 * 19414 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19415 * so scale the capacity value to reflect this. 19416 */ 19417 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19418 19419 #if defined(__i386) || defined(__amd64) 19420 /* 19421 * Refer to comments related to off-by-1 at the 19422 * header of this file. 19423 * Treat 1TB disk as (1T - 512)B. 19424 */ 19425 if (un->un_f_capacity_adjusted == 1) 19426 capacity = DK_MAX_BLOCKS; 19427 #endif 19428 19429 /* 19430 * Copy the values from the READ CAPACITY command into the space 19431 * provided by the caller. 19432 */ 19433 *capp = capacity; 19434 *lbap = lbasize; 19435 19436 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19437 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19438 19439 /* 19440 * Both the lbasize and capacity from the device must be nonzero, 19441 * otherwise we assume that the values are not valid and return 19442 * failure to the caller. (4203735) 19443 */ 19444 if ((capacity == 0) || (lbasize == 0)) { 19445 return (EIO); 19446 } 19447 19448 return (0); 19449 } 19450 19451 /* 19452 * Function: sd_send_scsi_READ_CAPACITY_16 19453 * 19454 * Description: This routine uses the scsi READ CAPACITY 16 command to 19455 * determine the device capacity in number of blocks and the 19456 * device native block size. If this function returns a failure, 19457 * then the values in *capp and *lbap are undefined. 19458 * This routine should always be called by 19459 * sd_send_scsi_READ_CAPACITY which will appy any device 19460 * specific adjustments to capacity and lbasize. 19461 * 19462 * Arguments: un - ptr to soft state struct for the target 19463 * capp - ptr to unsigned 64-bit variable to receive the 19464 * capacity value from the command. 19465 * lbap - ptr to unsigned 32-bit varaible to receive the 19466 * block size value from the command 19467 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19468 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19469 * to use the USCSI "direct" chain and bypass the normal 19470 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19471 * this command is issued as part of an error recovery 19472 * action. 19473 * 19474 * Return Code: 0 - Success 19475 * EIO - IO error 19476 * EACCES - Reservation conflict detected 19477 * EAGAIN - Device is becoming ready 19478 * errno return code from sd_send_scsi_cmd() 19479 * 19480 * Context: Can sleep. Blocks until command completes. 19481 */ 19482 19483 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19484 19485 static int 19486 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19487 uint32_t *lbap, int path_flag) 19488 { 19489 struct scsi_extended_sense sense_buf; 19490 struct uscsi_cmd ucmd_buf; 19491 union scsi_cdb cdb; 19492 uint64_t *capacity16_buf; 19493 uint64_t capacity; 19494 uint32_t lbasize; 19495 int status; 19496 19497 ASSERT(un != NULL); 19498 ASSERT(!mutex_owned(SD_MUTEX(un))); 19499 ASSERT(capp != NULL); 19500 ASSERT(lbap != NULL); 19501 19502 SD_TRACE(SD_LOG_IO, un, 19503 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19504 19505 /* 19506 * First send a READ_CAPACITY_16 command to the target. 19507 * 19508 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19509 * Medium Indicator bit is cleared. The address field must be 19510 * zero if the PMI bit is zero. 19511 */ 19512 bzero(&cdb, sizeof (cdb)); 19513 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19514 19515 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19516 19517 ucmd_buf.uscsi_cdb = (char *)&cdb; 19518 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19519 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19520 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19521 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19522 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19523 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19524 ucmd_buf.uscsi_timeout = 60; 19525 19526 /* 19527 * Read Capacity (16) is a Service Action In command. One 19528 * command byte (0x9E) is overloaded for multiple operations, 19529 * with the second CDB byte specifying the desired operation 19530 */ 19531 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19532 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19533 19534 /* 19535 * Fill in allocation length field 19536 */ 19537 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19538 19539 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19540 UIO_SYSSPACE, path_flag); 19541 19542 switch (status) { 19543 case 0: 19544 /* Return failure if we did not get valid capacity data. */ 19545 if (ucmd_buf.uscsi_resid > 20) { 19546 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19547 return (EIO); 19548 } 19549 19550 /* 19551 * Read capacity and block size from the READ CAPACITY 10 data. 19552 * This data may be adjusted later due to device specific 19553 * issues. 19554 * 19555 * According to the SCSI spec, the READ CAPACITY 10 19556 * command returns the following: 19557 * 19558 * bytes 0-7: Maximum logical block address available. 19559 * (MSB in byte:0 & LSB in byte:7) 19560 * 19561 * bytes 8-11: Block length in bytes 19562 * (MSB in byte:8 & LSB in byte:11) 19563 * 19564 */ 19565 capacity = BE_64(capacity16_buf[0]); 19566 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19567 19568 /* 19569 * Done with capacity16_buf 19570 */ 19571 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19572 19573 /* 19574 * if the reported capacity is set to all 0xf's, then 19575 * this disk is too large. This could only happen with 19576 * a device that supports LBAs larger than 64 bits which 19577 * are not defined by any current T10 standards. 19578 */ 19579 if (capacity == 0xffffffffffffffff) { 19580 return (EIO); 19581 } 19582 break; /* Success! */ 19583 case EIO: 19584 switch (ucmd_buf.uscsi_status) { 19585 case STATUS_RESERVATION_CONFLICT: 19586 status = EACCES; 19587 break; 19588 case STATUS_CHECK: 19589 /* 19590 * Check condition; look for ASC/ASCQ of 0x04/0x01 19591 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19592 */ 19593 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19594 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19595 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19596 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19597 return (EAGAIN); 19598 } 19599 break; 19600 default: 19601 break; 19602 } 19603 /* FALLTHRU */ 19604 default: 19605 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19606 return (status); 19607 } 19608 19609 *capp = capacity; 19610 *lbap = lbasize; 19611 19612 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19613 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19614 19615 return (0); 19616 } 19617 19618 19619 /* 19620 * Function: sd_send_scsi_START_STOP_UNIT 19621 * 19622 * Description: Issue a scsi START STOP UNIT command to the target. 19623 * 19624 * Arguments: un - pointer to driver soft state (unit) structure for 19625 * this target. 19626 * flag - SD_TARGET_START 19627 * SD_TARGET_STOP 19628 * SD_TARGET_EJECT 19629 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19630 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19631 * to use the USCSI "direct" chain and bypass the normal 19632 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19633 * command is issued as part of an error recovery action. 19634 * 19635 * Return Code: 0 - Success 19636 * EIO - IO error 19637 * EACCES - Reservation conflict detected 19638 * ENXIO - Not Ready, medium not present 19639 * errno return code from sd_send_scsi_cmd() 19640 * 19641 * Context: Can sleep. 19642 */ 19643 19644 static int 19645 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19646 { 19647 struct scsi_extended_sense sense_buf; 19648 union scsi_cdb cdb; 19649 struct uscsi_cmd ucmd_buf; 19650 int status; 19651 19652 ASSERT(un != NULL); 19653 ASSERT(!mutex_owned(SD_MUTEX(un))); 19654 19655 SD_TRACE(SD_LOG_IO, un, 19656 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19657 19658 if (un->un_f_check_start_stop && 19659 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19660 (un->un_f_start_stop_supported != TRUE)) { 19661 return (0); 19662 } 19663 19664 /* 19665 * If we are performing an eject operation and 19666 * we receive any command other than SD_TARGET_EJECT 19667 * we should immediately return. 19668 */ 19669 if (flag != SD_TARGET_EJECT) { 19670 mutex_enter(SD_MUTEX(un)); 19671 if (un->un_f_ejecting == TRUE) { 19672 mutex_exit(SD_MUTEX(un)); 19673 return (EAGAIN); 19674 } 19675 mutex_exit(SD_MUTEX(un)); 19676 } 19677 19678 bzero(&cdb, sizeof (cdb)); 19679 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19680 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19681 19682 cdb.scc_cmd = SCMD_START_STOP; 19683 cdb.cdb_opaque[4] = (uchar_t)flag; 19684 19685 ucmd_buf.uscsi_cdb = (char *)&cdb; 19686 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19687 ucmd_buf.uscsi_bufaddr = NULL; 19688 ucmd_buf.uscsi_buflen = 0; 19689 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19690 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19691 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19692 ucmd_buf.uscsi_timeout = 200; 19693 19694 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19695 UIO_SYSSPACE, path_flag); 19696 19697 switch (status) { 19698 case 0: 19699 break; /* Success! */ 19700 case EIO: 19701 switch (ucmd_buf.uscsi_status) { 19702 case STATUS_RESERVATION_CONFLICT: 19703 status = EACCES; 19704 break; 19705 case STATUS_CHECK: 19706 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19707 switch (scsi_sense_key( 19708 (uint8_t *)&sense_buf)) { 19709 case KEY_ILLEGAL_REQUEST: 19710 status = ENOTSUP; 19711 break; 19712 case KEY_NOT_READY: 19713 if (scsi_sense_asc( 19714 (uint8_t *)&sense_buf) 19715 == 0x3A) { 19716 status = ENXIO; 19717 } 19718 break; 19719 default: 19720 break; 19721 } 19722 } 19723 break; 19724 default: 19725 break; 19726 } 19727 break; 19728 default: 19729 break; 19730 } 19731 19732 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19733 19734 return (status); 19735 } 19736 19737 19738 /* 19739 * Function: sd_start_stop_unit_callback 19740 * 19741 * Description: timeout(9F) callback to begin recovery process for a 19742 * device that has spun down. 19743 * 19744 * Arguments: arg - pointer to associated softstate struct. 19745 * 19746 * Context: Executes in a timeout(9F) thread context 19747 */ 19748 19749 static void 19750 sd_start_stop_unit_callback(void *arg) 19751 { 19752 struct sd_lun *un = arg; 19753 ASSERT(un != NULL); 19754 ASSERT(!mutex_owned(SD_MUTEX(un))); 19755 19756 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19757 19758 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19759 } 19760 19761 19762 /* 19763 * Function: sd_start_stop_unit_task 19764 * 19765 * Description: Recovery procedure when a drive is spun down. 19766 * 19767 * Arguments: arg - pointer to associated softstate struct. 19768 * 19769 * Context: Executes in a taskq() thread context 19770 */ 19771 19772 static void 19773 sd_start_stop_unit_task(void *arg) 19774 { 19775 struct sd_lun *un = arg; 19776 19777 ASSERT(un != NULL); 19778 ASSERT(!mutex_owned(SD_MUTEX(un))); 19779 19780 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19781 19782 /* 19783 * Some unformatted drives report not ready error, no need to 19784 * restart if format has been initiated. 19785 */ 19786 mutex_enter(SD_MUTEX(un)); 19787 if (un->un_f_format_in_progress == TRUE) { 19788 mutex_exit(SD_MUTEX(un)); 19789 return; 19790 } 19791 mutex_exit(SD_MUTEX(un)); 19792 19793 /* 19794 * When a START STOP command is issued from here, it is part of a 19795 * failure recovery operation and must be issued before any other 19796 * commands, including any pending retries. Thus it must be sent 19797 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19798 * succeeds or not, we will start I/O after the attempt. 19799 */ 19800 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19801 SD_PATH_DIRECT_PRIORITY); 19802 19803 /* 19804 * The above call blocks until the START_STOP_UNIT command completes. 19805 * Now that it has completed, we must re-try the original IO that 19806 * received the NOT READY condition in the first place. There are 19807 * three possible conditions here: 19808 * 19809 * (1) The original IO is on un_retry_bp. 19810 * (2) The original IO is on the regular wait queue, and un_retry_bp 19811 * is NULL. 19812 * (3) The original IO is on the regular wait queue, and un_retry_bp 19813 * points to some other, unrelated bp. 19814 * 19815 * For each case, we must call sd_start_cmds() with un_retry_bp 19816 * as the argument. If un_retry_bp is NULL, this will initiate 19817 * processing of the regular wait queue. If un_retry_bp is not NULL, 19818 * then this will process the bp on un_retry_bp. That may or may not 19819 * be the original IO, but that does not matter: the important thing 19820 * is to keep the IO processing going at this point. 19821 * 19822 * Note: This is a very specific error recovery sequence associated 19823 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19824 * serialize the I/O with completion of the spin-up. 19825 */ 19826 mutex_enter(SD_MUTEX(un)); 19827 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19828 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19829 un, un->un_retry_bp); 19830 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19831 sd_start_cmds(un, un->un_retry_bp); 19832 mutex_exit(SD_MUTEX(un)); 19833 19834 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19835 } 19836 19837 19838 /* 19839 * Function: sd_send_scsi_INQUIRY 19840 * 19841 * Description: Issue the scsi INQUIRY command. 19842 * 19843 * Arguments: un 19844 * bufaddr 19845 * buflen 19846 * evpd 19847 * page_code 19848 * page_length 19849 * 19850 * Return Code: 0 - Success 19851 * errno return code from sd_send_scsi_cmd() 19852 * 19853 * Context: Can sleep. Does not return until command is completed. 19854 */ 19855 19856 static int 19857 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19858 uchar_t evpd, uchar_t page_code, size_t *residp) 19859 { 19860 union scsi_cdb cdb; 19861 struct uscsi_cmd ucmd_buf; 19862 int status; 19863 19864 ASSERT(un != NULL); 19865 ASSERT(!mutex_owned(SD_MUTEX(un))); 19866 ASSERT(bufaddr != NULL); 19867 19868 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19869 19870 bzero(&cdb, sizeof (cdb)); 19871 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19872 bzero(bufaddr, buflen); 19873 19874 cdb.scc_cmd = SCMD_INQUIRY; 19875 cdb.cdb_opaque[1] = evpd; 19876 cdb.cdb_opaque[2] = page_code; 19877 FORMG0COUNT(&cdb, buflen); 19878 19879 ucmd_buf.uscsi_cdb = (char *)&cdb; 19880 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19881 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19882 ucmd_buf.uscsi_buflen = buflen; 19883 ucmd_buf.uscsi_rqbuf = NULL; 19884 ucmd_buf.uscsi_rqlen = 0; 19885 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19886 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19887 19888 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19889 UIO_SYSSPACE, SD_PATH_DIRECT); 19890 19891 if ((status == 0) && (residp != NULL)) { 19892 *residp = ucmd_buf.uscsi_resid; 19893 } 19894 19895 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19896 19897 return (status); 19898 } 19899 19900 19901 /* 19902 * Function: sd_send_scsi_TEST_UNIT_READY 19903 * 19904 * Description: Issue the scsi TEST UNIT READY command. 19905 * This routine can be told to set the flag USCSI_DIAGNOSE to 19906 * prevent retrying failed commands. Use this when the intent 19907 * is either to check for device readiness, to clear a Unit 19908 * Attention, or to clear any outstanding sense data. 19909 * However under specific conditions the expected behavior 19910 * is for retries to bring a device ready, so use the flag 19911 * with caution. 19912 * 19913 * Arguments: un 19914 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19915 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19916 * 0: dont check for media present, do retries on cmd. 19917 * 19918 * Return Code: 0 - Success 19919 * EIO - IO error 19920 * EACCES - Reservation conflict detected 19921 * ENXIO - Not Ready, medium not present 19922 * errno return code from sd_send_scsi_cmd() 19923 * 19924 * Context: Can sleep. Does not return until command is completed. 19925 */ 19926 19927 static int 19928 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19929 { 19930 struct scsi_extended_sense sense_buf; 19931 union scsi_cdb cdb; 19932 struct uscsi_cmd ucmd_buf; 19933 int status; 19934 19935 ASSERT(un != NULL); 19936 ASSERT(!mutex_owned(SD_MUTEX(un))); 19937 19938 SD_TRACE(SD_LOG_IO, un, 19939 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19940 19941 /* 19942 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19943 * timeouts when they receive a TUR and the queue is not empty. Check 19944 * the configuration flag set during attach (indicating the drive has 19945 * this firmware bug) and un_ncmds_in_transport before issuing the 19946 * TUR. If there are 19947 * pending commands return success, this is a bit arbitrary but is ok 19948 * for non-removables (i.e. the eliteI disks) and non-clustering 19949 * configurations. 19950 */ 19951 if (un->un_f_cfg_tur_check == TRUE) { 19952 mutex_enter(SD_MUTEX(un)); 19953 if (un->un_ncmds_in_transport != 0) { 19954 mutex_exit(SD_MUTEX(un)); 19955 return (0); 19956 } 19957 mutex_exit(SD_MUTEX(un)); 19958 } 19959 19960 bzero(&cdb, sizeof (cdb)); 19961 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19962 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19963 19964 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19965 19966 ucmd_buf.uscsi_cdb = (char *)&cdb; 19967 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19968 ucmd_buf.uscsi_bufaddr = NULL; 19969 ucmd_buf.uscsi_buflen = 0; 19970 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19971 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19972 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19973 19974 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19975 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19976 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19977 } 19978 ucmd_buf.uscsi_timeout = 60; 19979 19980 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19981 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 19982 SD_PATH_STANDARD)); 19983 19984 switch (status) { 19985 case 0: 19986 break; /* Success! */ 19987 case EIO: 19988 switch (ucmd_buf.uscsi_status) { 19989 case STATUS_RESERVATION_CONFLICT: 19990 status = EACCES; 19991 break; 19992 case STATUS_CHECK: 19993 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19994 break; 19995 } 19996 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19997 (scsi_sense_key((uint8_t *)&sense_buf) == 19998 KEY_NOT_READY) && 19999 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20000 status = ENXIO; 20001 } 20002 break; 20003 default: 20004 break; 20005 } 20006 break; 20007 default: 20008 break; 20009 } 20010 20011 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20012 20013 return (status); 20014 } 20015 20016 20017 /* 20018 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20019 * 20020 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20021 * 20022 * Arguments: un 20023 * 20024 * Return Code: 0 - Success 20025 * EACCES 20026 * ENOTSUP 20027 * errno return code from sd_send_scsi_cmd() 20028 * 20029 * Context: Can sleep. Does not return until command is completed. 20030 */ 20031 20032 static int 20033 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 20034 uint16_t data_len, uchar_t *data_bufp) 20035 { 20036 struct scsi_extended_sense sense_buf; 20037 union scsi_cdb cdb; 20038 struct uscsi_cmd ucmd_buf; 20039 int status; 20040 int no_caller_buf = FALSE; 20041 20042 ASSERT(un != NULL); 20043 ASSERT(!mutex_owned(SD_MUTEX(un))); 20044 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20045 20046 SD_TRACE(SD_LOG_IO, un, 20047 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20048 20049 bzero(&cdb, sizeof (cdb)); 20050 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20051 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20052 if (data_bufp == NULL) { 20053 /* Allocate a default buf if the caller did not give one */ 20054 ASSERT(data_len == 0); 20055 data_len = MHIOC_RESV_KEY_SIZE; 20056 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20057 no_caller_buf = TRUE; 20058 } 20059 20060 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20061 cdb.cdb_opaque[1] = usr_cmd; 20062 FORMG1COUNT(&cdb, data_len); 20063 20064 ucmd_buf.uscsi_cdb = (char *)&cdb; 20065 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20066 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20067 ucmd_buf.uscsi_buflen = data_len; 20068 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20069 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20070 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20071 ucmd_buf.uscsi_timeout = 60; 20072 20073 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20074 UIO_SYSSPACE, SD_PATH_STANDARD); 20075 20076 switch (status) { 20077 case 0: 20078 break; /* Success! */ 20079 case EIO: 20080 switch (ucmd_buf.uscsi_status) { 20081 case STATUS_RESERVATION_CONFLICT: 20082 status = EACCES; 20083 break; 20084 case STATUS_CHECK: 20085 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20086 (scsi_sense_key((uint8_t *)&sense_buf) == 20087 KEY_ILLEGAL_REQUEST)) { 20088 status = ENOTSUP; 20089 } 20090 break; 20091 default: 20092 break; 20093 } 20094 break; 20095 default: 20096 break; 20097 } 20098 20099 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20100 20101 if (no_caller_buf == TRUE) { 20102 kmem_free(data_bufp, data_len); 20103 } 20104 20105 return (status); 20106 } 20107 20108 20109 /* 20110 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20111 * 20112 * Description: This routine is the driver entry point for handling CD-ROM 20113 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20114 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20115 * device. 20116 * 20117 * Arguments: un - Pointer to soft state struct for the target. 20118 * usr_cmd SCSI-3 reservation facility command (one of 20119 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20120 * SD_SCSI3_PREEMPTANDABORT) 20121 * usr_bufp - user provided pointer register, reserve descriptor or 20122 * preempt and abort structure (mhioc_register_t, 20123 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20124 * 20125 * Return Code: 0 - Success 20126 * EACCES 20127 * ENOTSUP 20128 * errno return code from sd_send_scsi_cmd() 20129 * 20130 * Context: Can sleep. Does not return until command is completed. 20131 */ 20132 20133 static int 20134 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 20135 uchar_t *usr_bufp) 20136 { 20137 struct scsi_extended_sense sense_buf; 20138 union scsi_cdb cdb; 20139 struct uscsi_cmd ucmd_buf; 20140 int status; 20141 uchar_t data_len = sizeof (sd_prout_t); 20142 sd_prout_t *prp; 20143 20144 ASSERT(un != NULL); 20145 ASSERT(!mutex_owned(SD_MUTEX(un))); 20146 ASSERT(data_len == 24); /* required by scsi spec */ 20147 20148 SD_TRACE(SD_LOG_IO, un, 20149 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20150 20151 if (usr_bufp == NULL) { 20152 return (EINVAL); 20153 } 20154 20155 bzero(&cdb, sizeof (cdb)); 20156 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20157 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20158 prp = kmem_zalloc(data_len, KM_SLEEP); 20159 20160 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20161 cdb.cdb_opaque[1] = usr_cmd; 20162 FORMG1COUNT(&cdb, data_len); 20163 20164 ucmd_buf.uscsi_cdb = (char *)&cdb; 20165 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20166 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20167 ucmd_buf.uscsi_buflen = data_len; 20168 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20169 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20170 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20171 ucmd_buf.uscsi_timeout = 60; 20172 20173 switch (usr_cmd) { 20174 case SD_SCSI3_REGISTER: { 20175 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20176 20177 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20178 bcopy(ptr->newkey.key, prp->service_key, 20179 MHIOC_RESV_KEY_SIZE); 20180 prp->aptpl = ptr->aptpl; 20181 break; 20182 } 20183 case SD_SCSI3_RESERVE: 20184 case SD_SCSI3_RELEASE: { 20185 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20186 20187 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20188 prp->scope_address = BE_32(ptr->scope_specific_addr); 20189 cdb.cdb_opaque[2] = ptr->type; 20190 break; 20191 } 20192 case SD_SCSI3_PREEMPTANDABORT: { 20193 mhioc_preemptandabort_t *ptr = 20194 (mhioc_preemptandabort_t *)usr_bufp; 20195 20196 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20197 bcopy(ptr->victim_key.key, prp->service_key, 20198 MHIOC_RESV_KEY_SIZE); 20199 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20200 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20201 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20202 break; 20203 } 20204 case SD_SCSI3_REGISTERANDIGNOREKEY: 20205 { 20206 mhioc_registerandignorekey_t *ptr; 20207 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20208 bcopy(ptr->newkey.key, 20209 prp->service_key, MHIOC_RESV_KEY_SIZE); 20210 prp->aptpl = ptr->aptpl; 20211 break; 20212 } 20213 default: 20214 ASSERT(FALSE); 20215 break; 20216 } 20217 20218 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20219 UIO_SYSSPACE, SD_PATH_STANDARD); 20220 20221 switch (status) { 20222 case 0: 20223 break; /* Success! */ 20224 case EIO: 20225 switch (ucmd_buf.uscsi_status) { 20226 case STATUS_RESERVATION_CONFLICT: 20227 status = EACCES; 20228 break; 20229 case STATUS_CHECK: 20230 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20231 (scsi_sense_key((uint8_t *)&sense_buf) == 20232 KEY_ILLEGAL_REQUEST)) { 20233 status = ENOTSUP; 20234 } 20235 break; 20236 default: 20237 break; 20238 } 20239 break; 20240 default: 20241 break; 20242 } 20243 20244 kmem_free(prp, data_len); 20245 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20246 return (status); 20247 } 20248 20249 20250 /* 20251 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20252 * 20253 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20254 * 20255 * Arguments: un - pointer to the target's soft state struct 20256 * 20257 * Return Code: 0 - success 20258 * errno-type error code 20259 * 20260 * Context: kernel thread context only. 20261 */ 20262 20263 static int 20264 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20265 { 20266 struct sd_uscsi_info *uip; 20267 struct uscsi_cmd *uscmd; 20268 union scsi_cdb *cdb; 20269 struct buf *bp; 20270 int rval = 0; 20271 20272 SD_TRACE(SD_LOG_IO, un, 20273 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20274 20275 ASSERT(un != NULL); 20276 ASSERT(!mutex_owned(SD_MUTEX(un))); 20277 20278 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20279 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20280 20281 /* 20282 * First get some memory for the uscsi_cmd struct and cdb 20283 * and initialize for SYNCHRONIZE_CACHE cmd. 20284 */ 20285 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20286 uscmd->uscsi_cdblen = CDB_GROUP1; 20287 uscmd->uscsi_cdb = (caddr_t)cdb; 20288 uscmd->uscsi_bufaddr = NULL; 20289 uscmd->uscsi_buflen = 0; 20290 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20291 uscmd->uscsi_rqlen = SENSE_LENGTH; 20292 uscmd->uscsi_rqresid = SENSE_LENGTH; 20293 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20294 uscmd->uscsi_timeout = sd_io_time; 20295 20296 /* 20297 * Allocate an sd_uscsi_info struct and fill it with the info 20298 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20299 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20300 * since we allocate the buf here in this function, we do not 20301 * need to preserve the prior contents of b_private. 20302 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20303 */ 20304 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20305 uip->ui_flags = SD_PATH_DIRECT; 20306 uip->ui_cmdp = uscmd; 20307 20308 bp = getrbuf(KM_SLEEP); 20309 bp->b_private = uip; 20310 20311 /* 20312 * Setup buffer to carry uscsi request. 20313 */ 20314 bp->b_flags = B_BUSY; 20315 bp->b_bcount = 0; 20316 bp->b_blkno = 0; 20317 20318 if (dkc != NULL) { 20319 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20320 uip->ui_dkc = *dkc; 20321 } 20322 20323 bp->b_edev = SD_GET_DEV(un); 20324 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20325 20326 (void) sd_uscsi_strategy(bp); 20327 20328 /* 20329 * If synchronous request, wait for completion 20330 * If async just return and let b_iodone callback 20331 * cleanup. 20332 * NOTE: On return, u_ncmds_in_driver will be decremented, 20333 * but it was also incremented in sd_uscsi_strategy(), so 20334 * we should be ok. 20335 */ 20336 if (dkc == NULL) { 20337 (void) biowait(bp); 20338 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20339 } 20340 20341 return (rval); 20342 } 20343 20344 20345 static int 20346 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20347 { 20348 struct sd_uscsi_info *uip; 20349 struct uscsi_cmd *uscmd; 20350 uint8_t *sense_buf; 20351 struct sd_lun *un; 20352 int status; 20353 20354 uip = (struct sd_uscsi_info *)(bp->b_private); 20355 ASSERT(uip != NULL); 20356 20357 uscmd = uip->ui_cmdp; 20358 ASSERT(uscmd != NULL); 20359 20360 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20361 ASSERT(sense_buf != NULL); 20362 20363 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20364 ASSERT(un != NULL); 20365 20366 status = geterror(bp); 20367 switch (status) { 20368 case 0: 20369 break; /* Success! */ 20370 case EIO: 20371 switch (uscmd->uscsi_status) { 20372 case STATUS_RESERVATION_CONFLICT: 20373 /* Ignore reservation conflict */ 20374 status = 0; 20375 goto done; 20376 20377 case STATUS_CHECK: 20378 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20379 (scsi_sense_key(sense_buf) == 20380 KEY_ILLEGAL_REQUEST)) { 20381 /* Ignore Illegal Request error */ 20382 mutex_enter(SD_MUTEX(un)); 20383 un->un_f_sync_cache_supported = FALSE; 20384 mutex_exit(SD_MUTEX(un)); 20385 status = ENOTSUP; 20386 goto done; 20387 } 20388 break; 20389 default: 20390 break; 20391 } 20392 /* FALLTHRU */ 20393 default: 20394 /* 20395 * Don't log an error message if this device 20396 * has removable media. 20397 */ 20398 if (!un->un_f_has_removable_media) { 20399 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20400 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20401 } 20402 break; 20403 } 20404 20405 done: 20406 if (uip->ui_dkc.dkc_callback != NULL) { 20407 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20408 } 20409 20410 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20411 freerbuf(bp); 20412 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20413 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20414 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20415 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20416 20417 return (status); 20418 } 20419 20420 20421 /* 20422 * Function: sd_send_scsi_GET_CONFIGURATION 20423 * 20424 * Description: Issues the get configuration command to the device. 20425 * Called from sd_check_for_writable_cd & sd_get_media_info 20426 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20427 * Arguments: un 20428 * ucmdbuf 20429 * rqbuf 20430 * rqbuflen 20431 * bufaddr 20432 * buflen 20433 * 20434 * Return Code: 0 - Success 20435 * errno return code from sd_send_scsi_cmd() 20436 * 20437 * Context: Can sleep. Does not return until command is completed. 20438 * 20439 */ 20440 20441 static int 20442 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20443 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20444 { 20445 char cdb[CDB_GROUP1]; 20446 int status; 20447 20448 ASSERT(un != NULL); 20449 ASSERT(!mutex_owned(SD_MUTEX(un))); 20450 ASSERT(bufaddr != NULL); 20451 ASSERT(ucmdbuf != NULL); 20452 ASSERT(rqbuf != NULL); 20453 20454 SD_TRACE(SD_LOG_IO, un, 20455 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20456 20457 bzero(cdb, sizeof (cdb)); 20458 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20459 bzero(rqbuf, rqbuflen); 20460 bzero(bufaddr, buflen); 20461 20462 /* 20463 * Set up cdb field for the get configuration command. 20464 */ 20465 cdb[0] = SCMD_GET_CONFIGURATION; 20466 cdb[1] = 0x02; /* Requested Type */ 20467 cdb[8] = SD_PROFILE_HEADER_LEN; 20468 ucmdbuf->uscsi_cdb = cdb; 20469 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20470 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20471 ucmdbuf->uscsi_buflen = buflen; 20472 ucmdbuf->uscsi_timeout = sd_io_time; 20473 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20474 ucmdbuf->uscsi_rqlen = rqbuflen; 20475 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20476 20477 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 20478 UIO_SYSSPACE, SD_PATH_STANDARD); 20479 20480 switch (status) { 20481 case 0: 20482 break; /* Success! */ 20483 case EIO: 20484 switch (ucmdbuf->uscsi_status) { 20485 case STATUS_RESERVATION_CONFLICT: 20486 status = EACCES; 20487 break; 20488 default: 20489 break; 20490 } 20491 break; 20492 default: 20493 break; 20494 } 20495 20496 if (status == 0) { 20497 SD_DUMP_MEMORY(un, SD_LOG_IO, 20498 "sd_send_scsi_GET_CONFIGURATION: data", 20499 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20500 } 20501 20502 SD_TRACE(SD_LOG_IO, un, 20503 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20504 20505 return (status); 20506 } 20507 20508 /* 20509 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20510 * 20511 * Description: Issues the get configuration command to the device to 20512 * retrieve a specfic feature. Called from 20513 * sd_check_for_writable_cd & sd_set_mmc_caps. 20514 * Arguments: un 20515 * ucmdbuf 20516 * rqbuf 20517 * rqbuflen 20518 * bufaddr 20519 * buflen 20520 * feature 20521 * 20522 * Return Code: 0 - Success 20523 * errno return code from sd_send_scsi_cmd() 20524 * 20525 * Context: Can sleep. Does not return until command is completed. 20526 * 20527 */ 20528 static int 20529 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20530 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20531 uchar_t *bufaddr, uint_t buflen, char feature) 20532 { 20533 char cdb[CDB_GROUP1]; 20534 int status; 20535 20536 ASSERT(un != NULL); 20537 ASSERT(!mutex_owned(SD_MUTEX(un))); 20538 ASSERT(bufaddr != NULL); 20539 ASSERT(ucmdbuf != NULL); 20540 ASSERT(rqbuf != NULL); 20541 20542 SD_TRACE(SD_LOG_IO, un, 20543 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20544 20545 bzero(cdb, sizeof (cdb)); 20546 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20547 bzero(rqbuf, rqbuflen); 20548 bzero(bufaddr, buflen); 20549 20550 /* 20551 * Set up cdb field for the get configuration command. 20552 */ 20553 cdb[0] = SCMD_GET_CONFIGURATION; 20554 cdb[1] = 0x02; /* Requested Type */ 20555 cdb[3] = feature; 20556 cdb[8] = buflen; 20557 ucmdbuf->uscsi_cdb = cdb; 20558 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20559 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20560 ucmdbuf->uscsi_buflen = buflen; 20561 ucmdbuf->uscsi_timeout = sd_io_time; 20562 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20563 ucmdbuf->uscsi_rqlen = rqbuflen; 20564 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20565 20566 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 20567 UIO_SYSSPACE, SD_PATH_STANDARD); 20568 20569 switch (status) { 20570 case 0: 20571 break; /* Success! */ 20572 case EIO: 20573 switch (ucmdbuf->uscsi_status) { 20574 case STATUS_RESERVATION_CONFLICT: 20575 status = EACCES; 20576 break; 20577 default: 20578 break; 20579 } 20580 break; 20581 default: 20582 break; 20583 } 20584 20585 if (status == 0) { 20586 SD_DUMP_MEMORY(un, SD_LOG_IO, 20587 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20588 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20589 } 20590 20591 SD_TRACE(SD_LOG_IO, un, 20592 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20593 20594 return (status); 20595 } 20596 20597 20598 /* 20599 * Function: sd_send_scsi_MODE_SENSE 20600 * 20601 * Description: Utility function for issuing a scsi MODE SENSE command. 20602 * Note: This routine uses a consistent implementation for Group0, 20603 * Group1, and Group2 commands across all platforms. ATAPI devices 20604 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20605 * 20606 * Arguments: un - pointer to the softstate struct for the target. 20607 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20608 * CDB_GROUP[1|2] (10 byte). 20609 * bufaddr - buffer for page data retrieved from the target. 20610 * buflen - size of page to be retrieved. 20611 * page_code - page code of data to be retrieved from the target. 20612 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20613 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20614 * to use the USCSI "direct" chain and bypass the normal 20615 * command waitq. 20616 * 20617 * Return Code: 0 - Success 20618 * errno return code from sd_send_scsi_cmd() 20619 * 20620 * Context: Can sleep. Does not return until command is completed. 20621 */ 20622 20623 static int 20624 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20625 size_t buflen, uchar_t page_code, int path_flag) 20626 { 20627 struct scsi_extended_sense sense_buf; 20628 union scsi_cdb cdb; 20629 struct uscsi_cmd ucmd_buf; 20630 int status; 20631 int headlen; 20632 20633 ASSERT(un != NULL); 20634 ASSERT(!mutex_owned(SD_MUTEX(un))); 20635 ASSERT(bufaddr != NULL); 20636 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20637 (cdbsize == CDB_GROUP2)); 20638 20639 SD_TRACE(SD_LOG_IO, un, 20640 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20641 20642 bzero(&cdb, sizeof (cdb)); 20643 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20644 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20645 bzero(bufaddr, buflen); 20646 20647 if (cdbsize == CDB_GROUP0) { 20648 cdb.scc_cmd = SCMD_MODE_SENSE; 20649 cdb.cdb_opaque[2] = page_code; 20650 FORMG0COUNT(&cdb, buflen); 20651 headlen = MODE_HEADER_LENGTH; 20652 } else { 20653 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20654 cdb.cdb_opaque[2] = page_code; 20655 FORMG1COUNT(&cdb, buflen); 20656 headlen = MODE_HEADER_LENGTH_GRP2; 20657 } 20658 20659 ASSERT(headlen <= buflen); 20660 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20661 20662 ucmd_buf.uscsi_cdb = (char *)&cdb; 20663 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20664 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20665 ucmd_buf.uscsi_buflen = buflen; 20666 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20667 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20668 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20669 ucmd_buf.uscsi_timeout = 60; 20670 20671 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20672 UIO_SYSSPACE, path_flag); 20673 20674 switch (status) { 20675 case 0: 20676 /* 20677 * sr_check_wp() uses 0x3f page code and check the header of 20678 * mode page to determine if target device is write-protected. 20679 * But some USB devices return 0 bytes for 0x3f page code. For 20680 * this case, make sure that mode page header is returned at 20681 * least. 20682 */ 20683 if (buflen - ucmd_buf.uscsi_resid < headlen) 20684 status = EIO; 20685 break; /* Success! */ 20686 case EIO: 20687 switch (ucmd_buf.uscsi_status) { 20688 case STATUS_RESERVATION_CONFLICT: 20689 status = EACCES; 20690 break; 20691 default: 20692 break; 20693 } 20694 break; 20695 default: 20696 break; 20697 } 20698 20699 if (status == 0) { 20700 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20701 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20702 } 20703 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20704 20705 return (status); 20706 } 20707 20708 20709 /* 20710 * Function: sd_send_scsi_MODE_SELECT 20711 * 20712 * Description: Utility function for issuing a scsi MODE SELECT command. 20713 * Note: This routine uses a consistent implementation for Group0, 20714 * Group1, and Group2 commands across all platforms. ATAPI devices 20715 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20716 * 20717 * Arguments: un - pointer to the softstate struct for the target. 20718 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20719 * CDB_GROUP[1|2] (10 byte). 20720 * bufaddr - buffer for page data retrieved from the target. 20721 * buflen - size of page to be retrieved. 20722 * save_page - boolean to determin if SP bit should be set. 20723 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20724 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20725 * to use the USCSI "direct" chain and bypass the normal 20726 * command waitq. 20727 * 20728 * Return Code: 0 - Success 20729 * errno return code from sd_send_scsi_cmd() 20730 * 20731 * Context: Can sleep. Does not return until command is completed. 20732 */ 20733 20734 static int 20735 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20736 size_t buflen, uchar_t save_page, int path_flag) 20737 { 20738 struct scsi_extended_sense sense_buf; 20739 union scsi_cdb cdb; 20740 struct uscsi_cmd ucmd_buf; 20741 int status; 20742 20743 ASSERT(un != NULL); 20744 ASSERT(!mutex_owned(SD_MUTEX(un))); 20745 ASSERT(bufaddr != NULL); 20746 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20747 (cdbsize == CDB_GROUP2)); 20748 20749 SD_TRACE(SD_LOG_IO, un, 20750 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20751 20752 bzero(&cdb, sizeof (cdb)); 20753 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20754 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20755 20756 /* Set the PF bit for many third party drives */ 20757 cdb.cdb_opaque[1] = 0x10; 20758 20759 /* Set the savepage(SP) bit if given */ 20760 if (save_page == SD_SAVE_PAGE) { 20761 cdb.cdb_opaque[1] |= 0x01; 20762 } 20763 20764 if (cdbsize == CDB_GROUP0) { 20765 cdb.scc_cmd = SCMD_MODE_SELECT; 20766 FORMG0COUNT(&cdb, buflen); 20767 } else { 20768 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20769 FORMG1COUNT(&cdb, buflen); 20770 } 20771 20772 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20773 20774 ucmd_buf.uscsi_cdb = (char *)&cdb; 20775 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20776 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20777 ucmd_buf.uscsi_buflen = buflen; 20778 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20779 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20780 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20781 ucmd_buf.uscsi_timeout = 60; 20782 20783 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20784 UIO_SYSSPACE, path_flag); 20785 20786 switch (status) { 20787 case 0: 20788 break; /* Success! */ 20789 case EIO: 20790 switch (ucmd_buf.uscsi_status) { 20791 case STATUS_RESERVATION_CONFLICT: 20792 status = EACCES; 20793 break; 20794 default: 20795 break; 20796 } 20797 break; 20798 default: 20799 break; 20800 } 20801 20802 if (status == 0) { 20803 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20804 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20805 } 20806 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20807 20808 return (status); 20809 } 20810 20811 20812 /* 20813 * Function: sd_send_scsi_RDWR 20814 * 20815 * Description: Issue a scsi READ or WRITE command with the given parameters. 20816 * 20817 * Arguments: un: Pointer to the sd_lun struct for the target. 20818 * cmd: SCMD_READ or SCMD_WRITE 20819 * bufaddr: Address of caller's buffer to receive the RDWR data 20820 * buflen: Length of caller's buffer receive the RDWR data. 20821 * start_block: Block number for the start of the RDWR operation. 20822 * (Assumes target-native block size.) 20823 * residp: Pointer to variable to receive the redisual of the 20824 * RDWR operation (may be NULL of no residual requested). 20825 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20826 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20827 * to use the USCSI "direct" chain and bypass the normal 20828 * command waitq. 20829 * 20830 * Return Code: 0 - Success 20831 * errno return code from sd_send_scsi_cmd() 20832 * 20833 * Context: Can sleep. Does not return until command is completed. 20834 */ 20835 20836 static int 20837 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20838 size_t buflen, daddr_t start_block, int path_flag) 20839 { 20840 struct scsi_extended_sense sense_buf; 20841 union scsi_cdb cdb; 20842 struct uscsi_cmd ucmd_buf; 20843 uint32_t block_count; 20844 int status; 20845 int cdbsize; 20846 uchar_t flag; 20847 20848 ASSERT(un != NULL); 20849 ASSERT(!mutex_owned(SD_MUTEX(un))); 20850 ASSERT(bufaddr != NULL); 20851 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20852 20853 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20854 20855 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20856 return (EINVAL); 20857 } 20858 20859 mutex_enter(SD_MUTEX(un)); 20860 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20861 mutex_exit(SD_MUTEX(un)); 20862 20863 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20864 20865 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20866 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20867 bufaddr, buflen, start_block, block_count); 20868 20869 bzero(&cdb, sizeof (cdb)); 20870 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20871 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20872 20873 /* Compute CDB size to use */ 20874 if (start_block > 0xffffffff) 20875 cdbsize = CDB_GROUP4; 20876 else if ((start_block & 0xFFE00000) || 20877 (un->un_f_cfg_is_atapi == TRUE)) 20878 cdbsize = CDB_GROUP1; 20879 else 20880 cdbsize = CDB_GROUP0; 20881 20882 switch (cdbsize) { 20883 case CDB_GROUP0: /* 6-byte CDBs */ 20884 cdb.scc_cmd = cmd; 20885 FORMG0ADDR(&cdb, start_block); 20886 FORMG0COUNT(&cdb, block_count); 20887 break; 20888 case CDB_GROUP1: /* 10-byte CDBs */ 20889 cdb.scc_cmd = cmd | SCMD_GROUP1; 20890 FORMG1ADDR(&cdb, start_block); 20891 FORMG1COUNT(&cdb, block_count); 20892 break; 20893 case CDB_GROUP4: /* 16-byte CDBs */ 20894 cdb.scc_cmd = cmd | SCMD_GROUP4; 20895 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20896 FORMG4COUNT(&cdb, block_count); 20897 break; 20898 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20899 default: 20900 /* All others reserved */ 20901 return (EINVAL); 20902 } 20903 20904 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20905 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20906 20907 ucmd_buf.uscsi_cdb = (char *)&cdb; 20908 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20909 ucmd_buf.uscsi_bufaddr = bufaddr; 20910 ucmd_buf.uscsi_buflen = buflen; 20911 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20912 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20913 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20914 ucmd_buf.uscsi_timeout = 60; 20915 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20916 UIO_SYSSPACE, path_flag); 20917 switch (status) { 20918 case 0: 20919 break; /* Success! */ 20920 case EIO: 20921 switch (ucmd_buf.uscsi_status) { 20922 case STATUS_RESERVATION_CONFLICT: 20923 status = EACCES; 20924 break; 20925 default: 20926 break; 20927 } 20928 break; 20929 default: 20930 break; 20931 } 20932 20933 if (status == 0) { 20934 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20935 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20936 } 20937 20938 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20939 20940 return (status); 20941 } 20942 20943 20944 /* 20945 * Function: sd_send_scsi_LOG_SENSE 20946 * 20947 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20948 * 20949 * Arguments: un: Pointer to the sd_lun struct for the target. 20950 * 20951 * Return Code: 0 - Success 20952 * errno return code from sd_send_scsi_cmd() 20953 * 20954 * Context: Can sleep. Does not return until command is completed. 20955 */ 20956 20957 static int 20958 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20959 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20960 int path_flag) 20961 20962 { 20963 struct scsi_extended_sense sense_buf; 20964 union scsi_cdb cdb; 20965 struct uscsi_cmd ucmd_buf; 20966 int status; 20967 20968 ASSERT(un != NULL); 20969 ASSERT(!mutex_owned(SD_MUTEX(un))); 20970 20971 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20972 20973 bzero(&cdb, sizeof (cdb)); 20974 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20975 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20976 20977 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20978 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20979 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20980 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20981 FORMG1COUNT(&cdb, buflen); 20982 20983 ucmd_buf.uscsi_cdb = (char *)&cdb; 20984 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20985 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20986 ucmd_buf.uscsi_buflen = buflen; 20987 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20988 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20989 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20990 ucmd_buf.uscsi_timeout = 60; 20991 20992 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 20993 UIO_SYSSPACE, path_flag); 20994 20995 switch (status) { 20996 case 0: 20997 break; 20998 case EIO: 20999 switch (ucmd_buf.uscsi_status) { 21000 case STATUS_RESERVATION_CONFLICT: 21001 status = EACCES; 21002 break; 21003 case STATUS_CHECK: 21004 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21005 (scsi_sense_key((uint8_t *)&sense_buf) == 21006 KEY_ILLEGAL_REQUEST) && 21007 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21008 /* 21009 * ASC 0x24: INVALID FIELD IN CDB 21010 */ 21011 switch (page_code) { 21012 case START_STOP_CYCLE_PAGE: 21013 /* 21014 * The start stop cycle counter is 21015 * implemented as page 0x31 in earlier 21016 * generation disks. In new generation 21017 * disks the start stop cycle counter is 21018 * implemented as page 0xE. To properly 21019 * handle this case if an attempt for 21020 * log page 0xE is made and fails we 21021 * will try again using page 0x31. 21022 * 21023 * Network storage BU committed to 21024 * maintain the page 0x31 for this 21025 * purpose and will not have any other 21026 * page implemented with page code 0x31 21027 * until all disks transition to the 21028 * standard page. 21029 */ 21030 mutex_enter(SD_MUTEX(un)); 21031 un->un_start_stop_cycle_page = 21032 START_STOP_CYCLE_VU_PAGE; 21033 cdb.cdb_opaque[2] = 21034 (char)(page_control << 6) | 21035 un->un_start_stop_cycle_page; 21036 mutex_exit(SD_MUTEX(un)); 21037 status = sd_send_scsi_cmd( 21038 SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 21039 UIO_SYSSPACE, path_flag); 21040 21041 break; 21042 case TEMPERATURE_PAGE: 21043 status = ENOTTY; 21044 break; 21045 default: 21046 break; 21047 } 21048 } 21049 break; 21050 default: 21051 break; 21052 } 21053 break; 21054 default: 21055 break; 21056 } 21057 21058 if (status == 0) { 21059 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21060 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21061 } 21062 21063 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21064 21065 return (status); 21066 } 21067 21068 21069 /* 21070 * Function: sdioctl 21071 * 21072 * Description: Driver's ioctl(9e) entry point function. 21073 * 21074 * Arguments: dev - device number 21075 * cmd - ioctl operation to be performed 21076 * arg - user argument, contains data to be set or reference 21077 * parameter for get 21078 * flag - bit flag, indicating open settings, 32/64 bit type 21079 * cred_p - user credential pointer 21080 * rval_p - calling process return value (OPT) 21081 * 21082 * Return Code: EINVAL 21083 * ENOTTY 21084 * ENXIO 21085 * EIO 21086 * EFAULT 21087 * ENOTSUP 21088 * EPERM 21089 * 21090 * Context: Called from the device switch at normal priority. 21091 */ 21092 21093 static int 21094 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21095 { 21096 struct sd_lun *un = NULL; 21097 int geom_validated = FALSE; 21098 int err = 0; 21099 int i = 0; 21100 cred_t *cr; 21101 21102 /* 21103 * All device accesses go thru sdstrategy where we check on suspend 21104 * status 21105 */ 21106 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21107 return (ENXIO); 21108 } 21109 21110 ASSERT(!mutex_owned(SD_MUTEX(un))); 21111 21112 /* 21113 * Moved this wait from sd_uscsi_strategy to here for 21114 * reasons of deadlock prevention. Internal driver commands, 21115 * specifically those to change a devices power level, result 21116 * in a call to sd_uscsi_strategy. 21117 */ 21118 mutex_enter(SD_MUTEX(un)); 21119 while ((un->un_state == SD_STATE_SUSPENDED) || 21120 (un->un_state == SD_STATE_PM_CHANGING)) { 21121 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21122 } 21123 /* 21124 * Twiddling the counter here protects commands from now 21125 * through to the top of sd_uscsi_strategy. Without the 21126 * counter inc. a power down, for example, could get in 21127 * after the above check for state is made and before 21128 * execution gets to the top of sd_uscsi_strategy. 21129 * That would cause problems. 21130 */ 21131 un->un_ncmds_in_driver++; 21132 21133 if ((un->un_f_geometry_is_valid == FALSE) && 21134 (flag & (FNDELAY | FNONBLOCK))) { 21135 switch (cmd) { 21136 case CDROMPAUSE: 21137 case CDROMRESUME: 21138 case CDROMPLAYMSF: 21139 case CDROMPLAYTRKIND: 21140 case CDROMREADTOCHDR: 21141 case CDROMREADTOCENTRY: 21142 case CDROMSTOP: 21143 case CDROMSTART: 21144 case CDROMVOLCTRL: 21145 case CDROMSUBCHNL: 21146 case CDROMREADMODE2: 21147 case CDROMREADMODE1: 21148 case CDROMREADOFFSET: 21149 case CDROMSBLKMODE: 21150 case CDROMGBLKMODE: 21151 case CDROMGDRVSPEED: 21152 case CDROMSDRVSPEED: 21153 case CDROMCDDA: 21154 case CDROMCDXA: 21155 case CDROMSUBCODE: 21156 if (!ISCD(un)) { 21157 un->un_ncmds_in_driver--; 21158 ASSERT(un->un_ncmds_in_driver >= 0); 21159 mutex_exit(SD_MUTEX(un)); 21160 return (ENOTTY); 21161 } 21162 break; 21163 case FDEJECT: 21164 case DKIOCEJECT: 21165 case CDROMEJECT: 21166 if (!un->un_f_eject_media_supported) { 21167 un->un_ncmds_in_driver--; 21168 ASSERT(un->un_ncmds_in_driver >= 0); 21169 mutex_exit(SD_MUTEX(un)); 21170 return (ENOTTY); 21171 } 21172 break; 21173 case DKIOCSVTOC: 21174 case DKIOCSETEFI: 21175 case DKIOCSMBOOT: 21176 case DKIOCFLUSHWRITECACHE: 21177 mutex_exit(SD_MUTEX(un)); 21178 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21179 if (err != 0) { 21180 mutex_enter(SD_MUTEX(un)); 21181 un->un_ncmds_in_driver--; 21182 ASSERT(un->un_ncmds_in_driver >= 0); 21183 mutex_exit(SD_MUTEX(un)); 21184 return (EIO); 21185 } 21186 mutex_enter(SD_MUTEX(un)); 21187 /* FALLTHROUGH */ 21188 case DKIOCREMOVABLE: 21189 case DKIOCHOTPLUGGABLE: 21190 case DKIOCINFO: 21191 case DKIOCGMEDIAINFO: 21192 case MHIOCENFAILFAST: 21193 case MHIOCSTATUS: 21194 case MHIOCTKOWN: 21195 case MHIOCRELEASE: 21196 case MHIOCGRP_INKEYS: 21197 case MHIOCGRP_INRESV: 21198 case MHIOCGRP_REGISTER: 21199 case MHIOCGRP_RESERVE: 21200 case MHIOCGRP_PREEMPTANDABORT: 21201 case MHIOCGRP_REGISTERANDIGNOREKEY: 21202 case CDROMCLOSETRAY: 21203 case USCSICMD: 21204 goto skip_ready_valid; 21205 default: 21206 break; 21207 } 21208 21209 mutex_exit(SD_MUTEX(un)); 21210 err = sd_ready_and_valid(un); 21211 mutex_enter(SD_MUTEX(un)); 21212 if (err == SD_READY_NOT_VALID) { 21213 switch (cmd) { 21214 case DKIOCGAPART: 21215 case DKIOCGGEOM: 21216 case DKIOCSGEOM: 21217 case DKIOCGVTOC: 21218 case DKIOCSVTOC: 21219 case DKIOCSAPART: 21220 case DKIOCG_PHYGEOM: 21221 case DKIOCG_VIRTGEOM: 21222 err = ENOTSUP; 21223 un->un_ncmds_in_driver--; 21224 ASSERT(un->un_ncmds_in_driver >= 0); 21225 mutex_exit(SD_MUTEX(un)); 21226 return (err); 21227 } 21228 } 21229 if (err != SD_READY_VALID) { 21230 switch (cmd) { 21231 case DKIOCSTATE: 21232 case CDROMGDRVSPEED: 21233 case CDROMSDRVSPEED: 21234 case FDEJECT: /* for eject command */ 21235 case DKIOCEJECT: 21236 case CDROMEJECT: 21237 case DKIOCGETEFI: 21238 case DKIOCSGEOM: 21239 case DKIOCREMOVABLE: 21240 case DKIOCHOTPLUGGABLE: 21241 case DKIOCSAPART: 21242 case DKIOCSETEFI: 21243 break; 21244 default: 21245 if (un->un_f_has_removable_media) { 21246 err = ENXIO; 21247 } else { 21248 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21249 if (err == SD_RESERVED_BY_OTHERS) { 21250 err = EACCES; 21251 } else { 21252 err = EIO; 21253 } 21254 } 21255 un->un_ncmds_in_driver--; 21256 ASSERT(un->un_ncmds_in_driver >= 0); 21257 mutex_exit(SD_MUTEX(un)); 21258 return (err); 21259 } 21260 } 21261 geom_validated = TRUE; 21262 } 21263 if ((un->un_f_geometry_is_valid == TRUE) && 21264 (un->un_solaris_size > 0)) { 21265 /* 21266 * the "geometry_is_valid" flag could be true if we 21267 * have an fdisk table but no Solaris partition 21268 */ 21269 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21270 /* it is EFI, so return ENOTSUP for these */ 21271 switch (cmd) { 21272 case DKIOCGAPART: 21273 case DKIOCGGEOM: 21274 case DKIOCGVTOC: 21275 case DKIOCSVTOC: 21276 case DKIOCSAPART: 21277 err = ENOTSUP; 21278 un->un_ncmds_in_driver--; 21279 ASSERT(un->un_ncmds_in_driver >= 0); 21280 mutex_exit(SD_MUTEX(un)); 21281 return (err); 21282 } 21283 } 21284 } 21285 21286 skip_ready_valid: 21287 mutex_exit(SD_MUTEX(un)); 21288 21289 switch (cmd) { 21290 case DKIOCINFO: 21291 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21292 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21293 break; 21294 21295 case DKIOCGMEDIAINFO: 21296 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21297 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21298 break; 21299 21300 case DKIOCGGEOM: 21301 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21302 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21303 geom_validated); 21304 break; 21305 21306 case DKIOCSGEOM: 21307 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21308 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21309 break; 21310 21311 case DKIOCGAPART: 21312 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21313 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21314 geom_validated); 21315 break; 21316 21317 case DKIOCSAPART: 21318 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21319 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21320 break; 21321 21322 case DKIOCGVTOC: 21323 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21324 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21325 geom_validated); 21326 break; 21327 21328 case DKIOCGETEFI: 21329 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21330 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21331 break; 21332 21333 case DKIOCPARTITION: 21334 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21335 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21336 break; 21337 21338 case DKIOCSVTOC: 21339 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21340 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21341 break; 21342 21343 case DKIOCSETEFI: 21344 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21345 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21346 break; 21347 21348 case DKIOCGMBOOT: 21349 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21350 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21351 break; 21352 21353 case DKIOCSMBOOT: 21354 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21355 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21356 break; 21357 21358 case DKIOCLOCK: 21359 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21360 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21361 SD_PATH_STANDARD); 21362 break; 21363 21364 case DKIOCUNLOCK: 21365 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21366 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21367 SD_PATH_STANDARD); 21368 break; 21369 21370 case DKIOCSTATE: { 21371 enum dkio_state state; 21372 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21373 21374 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21375 err = EFAULT; 21376 } else { 21377 err = sd_check_media(dev, state); 21378 if (err == 0) { 21379 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21380 sizeof (int), flag) != 0) 21381 err = EFAULT; 21382 } 21383 } 21384 break; 21385 } 21386 21387 case DKIOCREMOVABLE: 21388 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21389 i = un->un_f_has_removable_media ? 1 : 0; 21390 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21391 err = EFAULT; 21392 } else { 21393 err = 0; 21394 } 21395 break; 21396 21397 case DKIOCHOTPLUGGABLE: 21398 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21399 i = un->un_f_is_hotpluggable ? 1 : 0; 21400 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21401 err = EFAULT; 21402 } else { 21403 err = 0; 21404 } 21405 break; 21406 21407 case DKIOCGTEMPERATURE: 21408 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21409 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21410 break; 21411 21412 case MHIOCENFAILFAST: 21413 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21414 if ((err = drv_priv(cred_p)) == 0) { 21415 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21416 } 21417 break; 21418 21419 case MHIOCTKOWN: 21420 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21421 if ((err = drv_priv(cred_p)) == 0) { 21422 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21423 } 21424 break; 21425 21426 case MHIOCRELEASE: 21427 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21428 if ((err = drv_priv(cred_p)) == 0) { 21429 err = sd_mhdioc_release(dev); 21430 } 21431 break; 21432 21433 case MHIOCSTATUS: 21434 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21435 if ((err = drv_priv(cred_p)) == 0) { 21436 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21437 case 0: 21438 err = 0; 21439 break; 21440 case EACCES: 21441 *rval_p = 1; 21442 err = 0; 21443 break; 21444 default: 21445 err = EIO; 21446 break; 21447 } 21448 } 21449 break; 21450 21451 case MHIOCQRESERVE: 21452 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21453 if ((err = drv_priv(cred_p)) == 0) { 21454 err = sd_reserve_release(dev, SD_RESERVE); 21455 } 21456 break; 21457 21458 case MHIOCREREGISTERDEVID: 21459 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21460 if (drv_priv(cred_p) == EPERM) { 21461 err = EPERM; 21462 } else if (!un->un_f_devid_supported) { 21463 err = ENOTTY; 21464 } else { 21465 err = sd_mhdioc_register_devid(dev); 21466 } 21467 break; 21468 21469 case MHIOCGRP_INKEYS: 21470 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21471 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21472 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21473 err = ENOTSUP; 21474 } else { 21475 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21476 flag); 21477 } 21478 } 21479 break; 21480 21481 case MHIOCGRP_INRESV: 21482 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21483 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21484 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21485 err = ENOTSUP; 21486 } else { 21487 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21488 } 21489 } 21490 break; 21491 21492 case MHIOCGRP_REGISTER: 21493 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21494 if ((err = drv_priv(cred_p)) != EPERM) { 21495 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21496 err = ENOTSUP; 21497 } else if (arg != NULL) { 21498 mhioc_register_t reg; 21499 if (ddi_copyin((void *)arg, ®, 21500 sizeof (mhioc_register_t), flag) != 0) { 21501 err = EFAULT; 21502 } else { 21503 err = 21504 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21505 un, SD_SCSI3_REGISTER, 21506 (uchar_t *)®); 21507 } 21508 } 21509 } 21510 break; 21511 21512 case MHIOCGRP_RESERVE: 21513 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21514 if ((err = drv_priv(cred_p)) != EPERM) { 21515 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21516 err = ENOTSUP; 21517 } else if (arg != NULL) { 21518 mhioc_resv_desc_t resv_desc; 21519 if (ddi_copyin((void *)arg, &resv_desc, 21520 sizeof (mhioc_resv_desc_t), flag) != 0) { 21521 err = EFAULT; 21522 } else { 21523 err = 21524 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21525 un, SD_SCSI3_RESERVE, 21526 (uchar_t *)&resv_desc); 21527 } 21528 } 21529 } 21530 break; 21531 21532 case MHIOCGRP_PREEMPTANDABORT: 21533 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21534 if ((err = drv_priv(cred_p)) != EPERM) { 21535 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21536 err = ENOTSUP; 21537 } else if (arg != NULL) { 21538 mhioc_preemptandabort_t preempt_abort; 21539 if (ddi_copyin((void *)arg, &preempt_abort, 21540 sizeof (mhioc_preemptandabort_t), 21541 flag) != 0) { 21542 err = EFAULT; 21543 } else { 21544 err = 21545 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21546 un, SD_SCSI3_PREEMPTANDABORT, 21547 (uchar_t *)&preempt_abort); 21548 } 21549 } 21550 } 21551 break; 21552 21553 case MHIOCGRP_REGISTERANDIGNOREKEY: 21554 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21555 if ((err = drv_priv(cred_p)) != EPERM) { 21556 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21557 err = ENOTSUP; 21558 } else if (arg != NULL) { 21559 mhioc_registerandignorekey_t r_and_i; 21560 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21561 sizeof (mhioc_registerandignorekey_t), 21562 flag) != 0) { 21563 err = EFAULT; 21564 } else { 21565 err = 21566 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21567 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21568 (uchar_t *)&r_and_i); 21569 } 21570 } 21571 } 21572 break; 21573 21574 case USCSICMD: 21575 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21576 cr = ddi_get_cred(); 21577 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21578 err = EPERM; 21579 } else { 21580 enum uio_seg uioseg; 21581 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 21582 UIO_USERSPACE; 21583 if (un->un_f_format_in_progress == TRUE) { 21584 err = EAGAIN; 21585 break; 21586 } 21587 err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg, 21588 flag, uioseg, SD_PATH_STANDARD); 21589 } 21590 break; 21591 21592 case CDROMPAUSE: 21593 case CDROMRESUME: 21594 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21595 if (!ISCD(un)) { 21596 err = ENOTTY; 21597 } else { 21598 err = sr_pause_resume(dev, cmd); 21599 } 21600 break; 21601 21602 case CDROMPLAYMSF: 21603 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21604 if (!ISCD(un)) { 21605 err = ENOTTY; 21606 } else { 21607 err = sr_play_msf(dev, (caddr_t)arg, flag); 21608 } 21609 break; 21610 21611 case CDROMPLAYTRKIND: 21612 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21613 #if defined(__i386) || defined(__amd64) 21614 /* 21615 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21616 */ 21617 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21618 #else 21619 if (!ISCD(un)) { 21620 #endif 21621 err = ENOTTY; 21622 } else { 21623 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21624 } 21625 break; 21626 21627 case CDROMREADTOCHDR: 21628 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21629 if (!ISCD(un)) { 21630 err = ENOTTY; 21631 } else { 21632 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21633 } 21634 break; 21635 21636 case CDROMREADTOCENTRY: 21637 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21638 if (!ISCD(un)) { 21639 err = ENOTTY; 21640 } else { 21641 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21642 } 21643 break; 21644 21645 case CDROMSTOP: 21646 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21647 if (!ISCD(un)) { 21648 err = ENOTTY; 21649 } else { 21650 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21651 SD_PATH_STANDARD); 21652 } 21653 break; 21654 21655 case CDROMSTART: 21656 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21657 if (!ISCD(un)) { 21658 err = ENOTTY; 21659 } else { 21660 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21661 SD_PATH_STANDARD); 21662 } 21663 break; 21664 21665 case CDROMCLOSETRAY: 21666 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21667 if (!ISCD(un)) { 21668 err = ENOTTY; 21669 } else { 21670 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21671 SD_PATH_STANDARD); 21672 } 21673 break; 21674 21675 case FDEJECT: /* for eject command */ 21676 case DKIOCEJECT: 21677 case CDROMEJECT: 21678 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21679 if (!un->un_f_eject_media_supported) { 21680 err = ENOTTY; 21681 } else { 21682 err = sr_eject(dev); 21683 } 21684 break; 21685 21686 case CDROMVOLCTRL: 21687 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21688 if (!ISCD(un)) { 21689 err = ENOTTY; 21690 } else { 21691 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21692 } 21693 break; 21694 21695 case CDROMSUBCHNL: 21696 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21697 if (!ISCD(un)) { 21698 err = ENOTTY; 21699 } else { 21700 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21701 } 21702 break; 21703 21704 case CDROMREADMODE2: 21705 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21706 if (!ISCD(un)) { 21707 err = ENOTTY; 21708 } else if (un->un_f_cfg_is_atapi == TRUE) { 21709 /* 21710 * If the drive supports READ CD, use that instead of 21711 * switching the LBA size via a MODE SELECT 21712 * Block Descriptor 21713 */ 21714 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21715 } else { 21716 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21717 } 21718 break; 21719 21720 case CDROMREADMODE1: 21721 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21722 if (!ISCD(un)) { 21723 err = ENOTTY; 21724 } else { 21725 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21726 } 21727 break; 21728 21729 case CDROMREADOFFSET: 21730 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21731 if (!ISCD(un)) { 21732 err = ENOTTY; 21733 } else { 21734 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21735 flag); 21736 } 21737 break; 21738 21739 case CDROMSBLKMODE: 21740 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21741 /* 21742 * There is no means of changing block size in case of atapi 21743 * drives, thus return ENOTTY if drive type is atapi 21744 */ 21745 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21746 err = ENOTTY; 21747 } else if (un->un_f_mmc_cap == TRUE) { 21748 21749 /* 21750 * MMC Devices do not support changing the 21751 * logical block size 21752 * 21753 * Note: EINVAL is being returned instead of ENOTTY to 21754 * maintain consistancy with the original mmc 21755 * driver update. 21756 */ 21757 err = EINVAL; 21758 } else { 21759 mutex_enter(SD_MUTEX(un)); 21760 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21761 (un->un_ncmds_in_transport > 0)) { 21762 mutex_exit(SD_MUTEX(un)); 21763 err = EINVAL; 21764 } else { 21765 mutex_exit(SD_MUTEX(un)); 21766 err = sr_change_blkmode(dev, cmd, arg, flag); 21767 } 21768 } 21769 break; 21770 21771 case CDROMGBLKMODE: 21772 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21773 if (!ISCD(un)) { 21774 err = ENOTTY; 21775 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21776 (un->un_f_blockcount_is_valid != FALSE)) { 21777 /* 21778 * Drive is an ATAPI drive so return target block 21779 * size for ATAPI drives since we cannot change the 21780 * blocksize on ATAPI drives. Used primarily to detect 21781 * if an ATAPI cdrom is present. 21782 */ 21783 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21784 sizeof (int), flag) != 0) { 21785 err = EFAULT; 21786 } else { 21787 err = 0; 21788 } 21789 21790 } else { 21791 /* 21792 * Drive supports changing block sizes via a Mode 21793 * Select. 21794 */ 21795 err = sr_change_blkmode(dev, cmd, arg, flag); 21796 } 21797 break; 21798 21799 case CDROMGDRVSPEED: 21800 case CDROMSDRVSPEED: 21801 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21802 if (!ISCD(un)) { 21803 err = ENOTTY; 21804 } else if (un->un_f_mmc_cap == TRUE) { 21805 /* 21806 * Note: In the future the driver implementation 21807 * for getting and 21808 * setting cd speed should entail: 21809 * 1) If non-mmc try the Toshiba mode page 21810 * (sr_change_speed) 21811 * 2) If mmc but no support for Real Time Streaming try 21812 * the SET CD SPEED (0xBB) command 21813 * (sr_atapi_change_speed) 21814 * 3) If mmc and support for Real Time Streaming 21815 * try the GET PERFORMANCE and SET STREAMING 21816 * commands (not yet implemented, 4380808) 21817 */ 21818 /* 21819 * As per recent MMC spec, CD-ROM speed is variable 21820 * and changes with LBA. Since there is no such 21821 * things as drive speed now, fail this ioctl. 21822 * 21823 * Note: EINVAL is returned for consistancy of original 21824 * implementation which included support for getting 21825 * the drive speed of mmc devices but not setting 21826 * the drive speed. Thus EINVAL would be returned 21827 * if a set request was made for an mmc device. 21828 * We no longer support get or set speed for 21829 * mmc but need to remain consistant with regard 21830 * to the error code returned. 21831 */ 21832 err = EINVAL; 21833 } else if (un->un_f_cfg_is_atapi == TRUE) { 21834 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21835 } else { 21836 err = sr_change_speed(dev, cmd, arg, flag); 21837 } 21838 break; 21839 21840 case CDROMCDDA: 21841 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21842 if (!ISCD(un)) { 21843 err = ENOTTY; 21844 } else { 21845 err = sr_read_cdda(dev, (void *)arg, flag); 21846 } 21847 break; 21848 21849 case CDROMCDXA: 21850 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21851 if (!ISCD(un)) { 21852 err = ENOTTY; 21853 } else { 21854 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21855 } 21856 break; 21857 21858 case CDROMSUBCODE: 21859 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21860 if (!ISCD(un)) { 21861 err = ENOTTY; 21862 } else { 21863 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21864 } 21865 break; 21866 21867 case DKIOCPARTINFO: { 21868 /* 21869 * Return parameters describing the selected disk slice. 21870 * Note: this ioctl is for the intel platform only 21871 */ 21872 #if defined(__i386) || defined(__amd64) 21873 int part; 21874 21875 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21876 part = SDPART(dev); 21877 21878 /* don't check un_solaris_size for pN */ 21879 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21880 err = EIO; 21881 } else { 21882 struct part_info p; 21883 21884 p.p_start = (daddr_t)un->un_offset[part]; 21885 p.p_length = (int)un->un_map[part].dkl_nblk; 21886 #ifdef _MULTI_DATAMODEL 21887 switch (ddi_model_convert_from(flag & FMODELS)) { 21888 case DDI_MODEL_ILP32: 21889 { 21890 struct part_info32 p32; 21891 21892 p32.p_start = (daddr32_t)p.p_start; 21893 p32.p_length = p.p_length; 21894 if (ddi_copyout(&p32, (void *)arg, 21895 sizeof (p32), flag)) 21896 err = EFAULT; 21897 break; 21898 } 21899 21900 case DDI_MODEL_NONE: 21901 { 21902 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21903 flag)) 21904 err = EFAULT; 21905 break; 21906 } 21907 } 21908 #else /* ! _MULTI_DATAMODEL */ 21909 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21910 err = EFAULT; 21911 #endif /* _MULTI_DATAMODEL */ 21912 } 21913 #else 21914 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21915 err = ENOTTY; 21916 #endif 21917 break; 21918 } 21919 21920 case DKIOCG_PHYGEOM: { 21921 /* Return the driver's notion of the media physical geometry */ 21922 #if defined(__i386) || defined(__amd64) 21923 uint64_t capacity; 21924 struct dk_geom disk_geom; 21925 struct dk_geom *dkgp = &disk_geom; 21926 21927 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21928 mutex_enter(SD_MUTEX(un)); 21929 21930 if (un->un_g.dkg_nhead != 0 && 21931 un->un_g.dkg_nsect != 0) { 21932 /* 21933 * We succeeded in getting a geometry, but 21934 * right now it is being reported as just the 21935 * Solaris fdisk partition, just like for 21936 * DKIOCGGEOM. We need to change that to be 21937 * correct for the entire disk now. 21938 */ 21939 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21940 dkgp->dkg_acyl = 0; 21941 dkgp->dkg_ncyl = un->un_blockcount / 21942 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21943 } else { 21944 bzero(dkgp, sizeof (struct dk_geom)); 21945 /* 21946 * This disk does not have a Solaris VTOC 21947 * so we must present a physical geometry 21948 * that will remain consistent regardless 21949 * of how the disk is used. This will ensure 21950 * that the geometry does not change regardless 21951 * of the fdisk partition type (ie. EFI, FAT32, 21952 * Solaris, etc). 21953 */ 21954 if (ISCD(un)) { 21955 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21956 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21957 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21958 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21959 } else { 21960 /* 21961 * Invalid un_blockcount can generate invalid 21962 * dk_geom and may result in division by zero 21963 * system failure. Should make sure blockcount 21964 * is valid before using it here. 21965 */ 21966 if (un->un_f_blockcount_is_valid == FALSE) { 21967 mutex_exit(SD_MUTEX(un)); 21968 err = EIO; 21969 21970 break; 21971 } 21972 21973 /* 21974 * Refer to comments related to off-by-1 at the 21975 * header of this file 21976 */ 21977 if (!un->un_f_capacity_adjusted && 21978 !un->un_f_has_removable_media && 21979 !un->un_f_is_hotpluggable && 21980 (un->un_tgt_blocksize == 21981 un->un_sys_blocksize)) 21982 capacity = un->un_blockcount - 1; 21983 else 21984 capacity = un->un_blockcount; 21985 21986 sd_convert_geometry(capacity, dkgp); 21987 dkgp->dkg_acyl = 0; 21988 dkgp->dkg_ncyl = capacity / 21989 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21990 } 21991 } 21992 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21993 21994 if (ddi_copyout(dkgp, (void *)arg, 21995 sizeof (struct dk_geom), flag)) { 21996 mutex_exit(SD_MUTEX(un)); 21997 err = EFAULT; 21998 } else { 21999 mutex_exit(SD_MUTEX(un)); 22000 err = 0; 22001 } 22002 #else 22003 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22004 err = ENOTTY; 22005 #endif 22006 break; 22007 } 22008 22009 case DKIOCG_VIRTGEOM: { 22010 /* Return the driver's notion of the media's logical geometry */ 22011 #if defined(__i386) || defined(__amd64) 22012 struct dk_geom disk_geom; 22013 struct dk_geom *dkgp = &disk_geom; 22014 22015 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22016 mutex_enter(SD_MUTEX(un)); 22017 /* 22018 * If there is no HBA geometry available, or 22019 * if the HBA returned us something that doesn't 22020 * really fit into an Int 13/function 8 geometry 22021 * result, just fail the ioctl. See PSARC 1998/313. 22022 */ 22023 if (un->un_lgeom.g_nhead == 0 || 22024 un->un_lgeom.g_nsect == 0 || 22025 un->un_lgeom.g_ncyl > 1024) { 22026 mutex_exit(SD_MUTEX(un)); 22027 err = EINVAL; 22028 } else { 22029 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 22030 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 22031 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22032 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 22033 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 22034 22035 if (ddi_copyout(dkgp, (void *)arg, 22036 sizeof (struct dk_geom), flag)) { 22037 mutex_exit(SD_MUTEX(un)); 22038 err = EFAULT; 22039 } else { 22040 mutex_exit(SD_MUTEX(un)); 22041 err = 0; 22042 } 22043 } 22044 #else 22045 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22046 err = ENOTTY; 22047 #endif 22048 break; 22049 } 22050 #ifdef SDDEBUG 22051 /* RESET/ABORTS testing ioctls */ 22052 case DKIOCRESET: { 22053 int reset_level; 22054 22055 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22056 err = EFAULT; 22057 } else { 22058 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22059 "reset_level = 0x%lx\n", reset_level); 22060 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22061 err = 0; 22062 } else { 22063 err = EIO; 22064 } 22065 } 22066 break; 22067 } 22068 22069 case DKIOCABORT: 22070 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22071 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22072 err = 0; 22073 } else { 22074 err = EIO; 22075 } 22076 break; 22077 #endif 22078 22079 #ifdef SD_FAULT_INJECTION 22080 /* SDIOC FaultInjection testing ioctls */ 22081 case SDIOCSTART: 22082 case SDIOCSTOP: 22083 case SDIOCINSERTPKT: 22084 case SDIOCINSERTXB: 22085 case SDIOCINSERTUN: 22086 case SDIOCINSERTARQ: 22087 case SDIOCPUSH: 22088 case SDIOCRETRIEVE: 22089 case SDIOCRUN: 22090 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22091 "SDIOC detected cmd:0x%X:\n", cmd); 22092 /* call error generator */ 22093 sd_faultinjection_ioctl(cmd, arg, un); 22094 err = 0; 22095 break; 22096 22097 #endif /* SD_FAULT_INJECTION */ 22098 22099 case DKIOCFLUSHWRITECACHE: 22100 { 22101 struct dk_callback *dkc = (struct dk_callback *)arg; 22102 22103 mutex_enter(SD_MUTEX(un)); 22104 if (!un->un_f_sync_cache_supported || 22105 !un->un_f_write_cache_enabled) { 22106 err = un->un_f_sync_cache_supported ? 22107 0 : ENOTSUP; 22108 mutex_exit(SD_MUTEX(un)); 22109 if ((flag & FKIOCTL) && dkc != NULL && 22110 dkc->dkc_callback != NULL) { 22111 (*dkc->dkc_callback)(dkc->dkc_cookie, 22112 err); 22113 /* 22114 * Did callback and reported error. 22115 * Since we did a callback, ioctl 22116 * should return 0. 22117 */ 22118 err = 0; 22119 } 22120 break; 22121 } 22122 mutex_exit(SD_MUTEX(un)); 22123 22124 if ((flag & FKIOCTL) && dkc != NULL && 22125 dkc->dkc_callback != NULL) { 22126 /* async SYNC CACHE request */ 22127 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22128 } else { 22129 /* synchronous SYNC CACHE request */ 22130 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22131 } 22132 } 22133 break; 22134 22135 case DKIOCGETWCE: { 22136 22137 int wce; 22138 22139 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 22140 break; 22141 } 22142 22143 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22144 err = EFAULT; 22145 } 22146 break; 22147 } 22148 22149 case DKIOCSETWCE: { 22150 22151 int wce, sync_supported; 22152 22153 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22154 err = EFAULT; 22155 break; 22156 } 22157 22158 /* 22159 * Synchronize multiple threads trying to enable 22160 * or disable the cache via the un_f_wcc_cv 22161 * condition variable. 22162 */ 22163 mutex_enter(SD_MUTEX(un)); 22164 22165 /* 22166 * Don't allow the cache to be enabled if the 22167 * config file has it disabled. 22168 */ 22169 if (un->un_f_opt_disable_cache && wce) { 22170 mutex_exit(SD_MUTEX(un)); 22171 err = EINVAL; 22172 break; 22173 } 22174 22175 /* 22176 * Wait for write cache change in progress 22177 * bit to be clear before proceeding. 22178 */ 22179 while (un->un_f_wcc_inprog) 22180 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22181 22182 un->un_f_wcc_inprog = 1; 22183 22184 if (un->un_f_write_cache_enabled && wce == 0) { 22185 /* 22186 * Disable the write cache. Don't clear 22187 * un_f_write_cache_enabled until after 22188 * the mode select and flush are complete. 22189 */ 22190 sync_supported = un->un_f_sync_cache_supported; 22191 mutex_exit(SD_MUTEX(un)); 22192 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22193 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22194 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22195 } 22196 22197 mutex_enter(SD_MUTEX(un)); 22198 if (err == 0) { 22199 un->un_f_write_cache_enabled = 0; 22200 } 22201 22202 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22203 /* 22204 * Set un_f_write_cache_enabled first, so there is 22205 * no window where the cache is enabled, but the 22206 * bit says it isn't. 22207 */ 22208 un->un_f_write_cache_enabled = 1; 22209 mutex_exit(SD_MUTEX(un)); 22210 22211 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22212 SD_CACHE_ENABLE); 22213 22214 mutex_enter(SD_MUTEX(un)); 22215 22216 if (err) { 22217 un->un_f_write_cache_enabled = 0; 22218 } 22219 } 22220 22221 un->un_f_wcc_inprog = 0; 22222 cv_broadcast(&un->un_wcc_cv); 22223 mutex_exit(SD_MUTEX(un)); 22224 break; 22225 } 22226 22227 default: 22228 err = ENOTTY; 22229 break; 22230 } 22231 mutex_enter(SD_MUTEX(un)); 22232 un->un_ncmds_in_driver--; 22233 ASSERT(un->un_ncmds_in_driver >= 0); 22234 mutex_exit(SD_MUTEX(un)); 22235 22236 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22237 return (err); 22238 } 22239 22240 22241 /* 22242 * Function: sd_dkio_ctrl_info 22243 * 22244 * Description: This routine is the driver entry point for handling controller 22245 * information ioctl requests (DKIOCINFO). 22246 * 22247 * Arguments: dev - the device number 22248 * arg - pointer to user provided dk_cinfo structure 22249 * specifying the controller type and attributes. 22250 * flag - this argument is a pass through to ddi_copyxxx() 22251 * directly from the mode argument of ioctl(). 22252 * 22253 * Return Code: 0 22254 * EFAULT 22255 * ENXIO 22256 */ 22257 22258 static int 22259 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22260 { 22261 struct sd_lun *un = NULL; 22262 struct dk_cinfo *info; 22263 dev_info_t *pdip; 22264 int lun, tgt; 22265 22266 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22267 return (ENXIO); 22268 } 22269 22270 info = (struct dk_cinfo *) 22271 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22272 22273 switch (un->un_ctype) { 22274 case CTYPE_CDROM: 22275 info->dki_ctype = DKC_CDROM; 22276 break; 22277 default: 22278 info->dki_ctype = DKC_SCSI_CCS; 22279 break; 22280 } 22281 pdip = ddi_get_parent(SD_DEVINFO(un)); 22282 info->dki_cnum = ddi_get_instance(pdip); 22283 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22284 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22285 } else { 22286 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22287 DK_DEVLEN - 1); 22288 } 22289 22290 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22291 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22292 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22293 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22294 22295 /* Unit Information */ 22296 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22297 info->dki_slave = ((tgt << 3) | lun); 22298 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22299 DK_DEVLEN - 1); 22300 info->dki_flags = DKI_FMTVOL; 22301 info->dki_partition = SDPART(dev); 22302 22303 /* Max Transfer size of this device in blocks */ 22304 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22305 info->dki_addr = 0; 22306 info->dki_space = 0; 22307 info->dki_prio = 0; 22308 info->dki_vec = 0; 22309 22310 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22311 kmem_free(info, sizeof (struct dk_cinfo)); 22312 return (EFAULT); 22313 } else { 22314 kmem_free(info, sizeof (struct dk_cinfo)); 22315 return (0); 22316 } 22317 } 22318 22319 22320 /* 22321 * Function: sd_get_media_info 22322 * 22323 * Description: This routine is the driver entry point for handling ioctl 22324 * requests for the media type or command set profile used by the 22325 * drive to operate on the media (DKIOCGMEDIAINFO). 22326 * 22327 * Arguments: dev - the device number 22328 * arg - pointer to user provided dk_minfo structure 22329 * specifying the media type, logical block size and 22330 * drive capacity. 22331 * flag - this argument is a pass through to ddi_copyxxx() 22332 * directly from the mode argument of ioctl(). 22333 * 22334 * Return Code: 0 22335 * EACCESS 22336 * EFAULT 22337 * ENXIO 22338 * EIO 22339 */ 22340 22341 static int 22342 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22343 { 22344 struct sd_lun *un = NULL; 22345 struct uscsi_cmd com; 22346 struct scsi_inquiry *sinq; 22347 struct dk_minfo media_info; 22348 u_longlong_t media_capacity; 22349 uint64_t capacity; 22350 uint_t lbasize; 22351 uchar_t *out_data; 22352 uchar_t *rqbuf; 22353 int rval = 0; 22354 int rtn; 22355 22356 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22357 (un->un_state == SD_STATE_OFFLINE)) { 22358 return (ENXIO); 22359 } 22360 22361 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22362 22363 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22364 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22365 22366 /* Issue a TUR to determine if the drive is ready with media present */ 22367 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22368 if (rval == ENXIO) { 22369 goto done; 22370 } 22371 22372 /* Now get configuration data */ 22373 if (ISCD(un)) { 22374 media_info.dki_media_type = DK_CDROM; 22375 22376 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22377 if (un->un_f_mmc_cap == TRUE) { 22378 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22379 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22380 22381 if (rtn) { 22382 /* 22383 * Failed for other than an illegal request 22384 * or command not supported 22385 */ 22386 if ((com.uscsi_status == STATUS_CHECK) && 22387 (com.uscsi_rqstatus == STATUS_GOOD)) { 22388 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22389 (rqbuf[12] != 0x20)) { 22390 rval = EIO; 22391 goto done; 22392 } 22393 } 22394 } else { 22395 /* 22396 * The GET CONFIGURATION command succeeded 22397 * so set the media type according to the 22398 * returned data 22399 */ 22400 media_info.dki_media_type = out_data[6]; 22401 media_info.dki_media_type <<= 8; 22402 media_info.dki_media_type |= out_data[7]; 22403 } 22404 } 22405 } else { 22406 /* 22407 * The profile list is not available, so we attempt to identify 22408 * the media type based on the inquiry data 22409 */ 22410 sinq = un->un_sd->sd_inq; 22411 if (sinq->inq_qual == 0) { 22412 /* This is a direct access device */ 22413 media_info.dki_media_type = DK_FIXED_DISK; 22414 22415 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22416 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22417 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22418 media_info.dki_media_type = DK_ZIP; 22419 } else if ( 22420 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22421 media_info.dki_media_type = DK_JAZ; 22422 } 22423 } 22424 } else { 22425 /* Not a CD or direct access so return unknown media */ 22426 media_info.dki_media_type = DK_UNKNOWN; 22427 } 22428 } 22429 22430 /* Now read the capacity so we can provide the lbasize and capacity */ 22431 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22432 SD_PATH_DIRECT)) { 22433 case 0: 22434 break; 22435 case EACCES: 22436 rval = EACCES; 22437 goto done; 22438 default: 22439 rval = EIO; 22440 goto done; 22441 } 22442 22443 media_info.dki_lbsize = lbasize; 22444 media_capacity = capacity; 22445 22446 /* 22447 * sd_send_scsi_READ_CAPACITY() reports capacity in 22448 * un->un_sys_blocksize chunks. So we need to convert it into 22449 * cap.lbasize chunks. 22450 */ 22451 media_capacity *= un->un_sys_blocksize; 22452 media_capacity /= lbasize; 22453 media_info.dki_capacity = media_capacity; 22454 22455 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22456 rval = EFAULT; 22457 /* Put goto. Anybody might add some code below in future */ 22458 goto done; 22459 } 22460 done: 22461 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22462 kmem_free(rqbuf, SENSE_LENGTH); 22463 return (rval); 22464 } 22465 22466 22467 /* 22468 * Function: sd_dkio_get_geometry 22469 * 22470 * Description: This routine is the driver entry point for handling user 22471 * requests to get the device geometry (DKIOCGGEOM). 22472 * 22473 * Arguments: dev - the device number 22474 * arg - pointer to user provided dk_geom structure specifying 22475 * the controller's notion of the current geometry. 22476 * flag - this argument is a pass through to ddi_copyxxx() 22477 * directly from the mode argument of ioctl(). 22478 * geom_validated - flag indicating if the device geometry has been 22479 * previously validated in the sdioctl routine. 22480 * 22481 * Return Code: 0 22482 * EFAULT 22483 * ENXIO 22484 * EIO 22485 */ 22486 22487 static int 22488 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22489 { 22490 struct sd_lun *un = NULL; 22491 struct dk_geom *tmp_geom = NULL; 22492 int rval = 0; 22493 22494 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22495 return (ENXIO); 22496 } 22497 22498 if (geom_validated == FALSE) { 22499 /* 22500 * sd_validate_geometry does not spin a disk up 22501 * if it was spun down. We need to make sure it 22502 * is ready. 22503 */ 22504 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22505 return (rval); 22506 } 22507 mutex_enter(SD_MUTEX(un)); 22508 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22509 mutex_exit(SD_MUTEX(un)); 22510 } 22511 if (rval) 22512 return (rval); 22513 22514 /* 22515 * It is possible that un_solaris_size is 0(uninitialized) 22516 * after sd_unit_attach. Reservation conflict may cause the 22517 * above situation. Thus, the zero check of un_solaris_size 22518 * should occur after the sd_validate_geometry() call. 22519 */ 22520 #if defined(__i386) || defined(__amd64) 22521 if (un->un_solaris_size == 0) { 22522 return (EIO); 22523 } 22524 #endif 22525 22526 /* 22527 * Make a local copy of the soft state geometry to avoid some potential 22528 * race conditions associated with holding the mutex and updating the 22529 * write_reinstruct value 22530 */ 22531 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22532 mutex_enter(SD_MUTEX(un)); 22533 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22534 mutex_exit(SD_MUTEX(un)); 22535 22536 if (tmp_geom->dkg_write_reinstruct == 0) { 22537 tmp_geom->dkg_write_reinstruct = 22538 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22539 sd_rot_delay) / (int)60000); 22540 } 22541 22542 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22543 flag); 22544 if (rval != 0) { 22545 rval = EFAULT; 22546 } 22547 22548 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22549 return (rval); 22550 22551 } 22552 22553 22554 /* 22555 * Function: sd_dkio_set_geometry 22556 * 22557 * Description: This routine is the driver entry point for handling user 22558 * requests to set the device geometry (DKIOCSGEOM). The actual 22559 * device geometry is not updated, just the driver "notion" of it. 22560 * 22561 * Arguments: dev - the device number 22562 * arg - pointer to user provided dk_geom structure used to set 22563 * the controller's notion of the current geometry. 22564 * flag - this argument is a pass through to ddi_copyxxx() 22565 * directly from the mode argument of ioctl(). 22566 * 22567 * Return Code: 0 22568 * EFAULT 22569 * ENXIO 22570 * EIO 22571 */ 22572 22573 static int 22574 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22575 { 22576 struct sd_lun *un = NULL; 22577 struct dk_geom *tmp_geom; 22578 struct dk_map *lp; 22579 int rval = 0; 22580 int i; 22581 22582 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22583 return (ENXIO); 22584 } 22585 22586 /* 22587 * Make sure there is no reservation conflict on the lun. 22588 */ 22589 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22590 return (EACCES); 22591 } 22592 22593 #if defined(__i386) || defined(__amd64) 22594 if (un->un_solaris_size == 0) { 22595 return (EIO); 22596 } 22597 #endif 22598 22599 /* 22600 * We need to copy the user specified geometry into local 22601 * storage and then update the softstate. We don't want to hold 22602 * the mutex and copyin directly from the user to the soft state 22603 */ 22604 tmp_geom = (struct dk_geom *) 22605 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22606 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22607 if (rval != 0) { 22608 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22609 return (EFAULT); 22610 } 22611 22612 mutex_enter(SD_MUTEX(un)); 22613 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22614 for (i = 0; i < NDKMAP; i++) { 22615 lp = &un->un_map[i]; 22616 un->un_offset[i] = 22617 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22618 #if defined(__i386) || defined(__amd64) 22619 un->un_offset[i] += un->un_solaris_offset; 22620 #endif 22621 } 22622 un->un_f_geometry_is_valid = FALSE; 22623 mutex_exit(SD_MUTEX(un)); 22624 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22625 22626 return (rval); 22627 } 22628 22629 22630 /* 22631 * Function: sd_dkio_get_partition 22632 * 22633 * Description: This routine is the driver entry point for handling user 22634 * requests to get the partition table (DKIOCGAPART). 22635 * 22636 * Arguments: dev - the device number 22637 * arg - pointer to user provided dk_allmap structure specifying 22638 * the controller's notion of the current partition table. 22639 * flag - this argument is a pass through to ddi_copyxxx() 22640 * directly from the mode argument of ioctl(). 22641 * geom_validated - flag indicating if the device geometry has been 22642 * previously validated in the sdioctl routine. 22643 * 22644 * Return Code: 0 22645 * EFAULT 22646 * ENXIO 22647 * EIO 22648 */ 22649 22650 static int 22651 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22652 { 22653 struct sd_lun *un = NULL; 22654 int rval = 0; 22655 int size; 22656 22657 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22658 return (ENXIO); 22659 } 22660 22661 /* 22662 * Make sure the geometry is valid before getting the partition 22663 * information. 22664 */ 22665 mutex_enter(SD_MUTEX(un)); 22666 if (geom_validated == FALSE) { 22667 /* 22668 * sd_validate_geometry does not spin a disk up 22669 * if it was spun down. We need to make sure it 22670 * is ready before validating the geometry. 22671 */ 22672 mutex_exit(SD_MUTEX(un)); 22673 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22674 return (rval); 22675 } 22676 mutex_enter(SD_MUTEX(un)); 22677 22678 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22679 mutex_exit(SD_MUTEX(un)); 22680 return (rval); 22681 } 22682 } 22683 mutex_exit(SD_MUTEX(un)); 22684 22685 /* 22686 * It is possible that un_solaris_size is 0(uninitialized) 22687 * after sd_unit_attach. Reservation conflict may cause the 22688 * above situation. Thus, the zero check of un_solaris_size 22689 * should occur after the sd_validate_geometry() call. 22690 */ 22691 #if defined(__i386) || defined(__amd64) 22692 if (un->un_solaris_size == 0) { 22693 return (EIO); 22694 } 22695 #endif 22696 22697 #ifdef _MULTI_DATAMODEL 22698 switch (ddi_model_convert_from(flag & FMODELS)) { 22699 case DDI_MODEL_ILP32: { 22700 struct dk_map32 dk_map32[NDKMAP]; 22701 int i; 22702 22703 for (i = 0; i < NDKMAP; i++) { 22704 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22705 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22706 } 22707 size = NDKMAP * sizeof (struct dk_map32); 22708 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22709 if (rval != 0) { 22710 rval = EFAULT; 22711 } 22712 break; 22713 } 22714 case DDI_MODEL_NONE: 22715 size = NDKMAP * sizeof (struct dk_map); 22716 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22717 if (rval != 0) { 22718 rval = EFAULT; 22719 } 22720 break; 22721 } 22722 #else /* ! _MULTI_DATAMODEL */ 22723 size = NDKMAP * sizeof (struct dk_map); 22724 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22725 if (rval != 0) { 22726 rval = EFAULT; 22727 } 22728 #endif /* _MULTI_DATAMODEL */ 22729 return (rval); 22730 } 22731 22732 22733 /* 22734 * Function: sd_dkio_set_partition 22735 * 22736 * Description: This routine is the driver entry point for handling user 22737 * requests to set the partition table (DKIOCSAPART). The actual 22738 * device partition is not updated. 22739 * 22740 * Arguments: dev - the device number 22741 * arg - pointer to user provided dk_allmap structure used to set 22742 * the controller's notion of the partition table. 22743 * flag - this argument is a pass through to ddi_copyxxx() 22744 * directly from the mode argument of ioctl(). 22745 * 22746 * Return Code: 0 22747 * EINVAL 22748 * EFAULT 22749 * ENXIO 22750 * EIO 22751 */ 22752 22753 static int 22754 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22755 { 22756 struct sd_lun *un = NULL; 22757 struct dk_map dk_map[NDKMAP]; 22758 struct dk_map *lp; 22759 int rval = 0; 22760 int size; 22761 int i; 22762 #if defined(_SUNOS_VTOC_16) 22763 struct dkl_partition *vp; 22764 #endif 22765 22766 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22767 return (ENXIO); 22768 } 22769 22770 /* 22771 * Set the map for all logical partitions. We lock 22772 * the priority just to make sure an interrupt doesn't 22773 * come in while the map is half updated. 22774 */ 22775 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22776 mutex_enter(SD_MUTEX(un)); 22777 if (un->un_blockcount > DK_MAX_BLOCKS) { 22778 mutex_exit(SD_MUTEX(un)); 22779 return (ENOTSUP); 22780 } 22781 mutex_exit(SD_MUTEX(un)); 22782 22783 /* 22784 * Make sure there is no reservation conflict on the lun. 22785 */ 22786 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22787 return (EACCES); 22788 } 22789 22790 #if defined(__i386) || defined(__amd64) 22791 if (un->un_solaris_size == 0) { 22792 return (EIO); 22793 } 22794 #endif 22795 22796 #ifdef _MULTI_DATAMODEL 22797 switch (ddi_model_convert_from(flag & FMODELS)) { 22798 case DDI_MODEL_ILP32: { 22799 struct dk_map32 dk_map32[NDKMAP]; 22800 22801 size = NDKMAP * sizeof (struct dk_map32); 22802 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22803 if (rval != 0) { 22804 return (EFAULT); 22805 } 22806 for (i = 0; i < NDKMAP; i++) { 22807 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22808 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22809 } 22810 break; 22811 } 22812 case DDI_MODEL_NONE: 22813 size = NDKMAP * sizeof (struct dk_map); 22814 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22815 if (rval != 0) { 22816 return (EFAULT); 22817 } 22818 break; 22819 } 22820 #else /* ! _MULTI_DATAMODEL */ 22821 size = NDKMAP * sizeof (struct dk_map); 22822 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22823 if (rval != 0) { 22824 return (EFAULT); 22825 } 22826 #endif /* _MULTI_DATAMODEL */ 22827 22828 mutex_enter(SD_MUTEX(un)); 22829 /* Note: The size used in this bcopy is set based upon the data model */ 22830 bcopy(dk_map, un->un_map, size); 22831 #if defined(_SUNOS_VTOC_16) 22832 vp = (struct dkl_partition *)&(un->un_vtoc); 22833 #endif /* defined(_SUNOS_VTOC_16) */ 22834 for (i = 0; i < NDKMAP; i++) { 22835 lp = &un->un_map[i]; 22836 un->un_offset[i] = 22837 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22838 #if defined(_SUNOS_VTOC_16) 22839 vp->p_start = un->un_offset[i]; 22840 vp->p_size = lp->dkl_nblk; 22841 vp++; 22842 #endif /* defined(_SUNOS_VTOC_16) */ 22843 #if defined(__i386) || defined(__amd64) 22844 un->un_offset[i] += un->un_solaris_offset; 22845 #endif 22846 } 22847 mutex_exit(SD_MUTEX(un)); 22848 return (rval); 22849 } 22850 22851 22852 /* 22853 * Function: sd_dkio_get_vtoc 22854 * 22855 * Description: This routine is the driver entry point for handling user 22856 * requests to get the current volume table of contents 22857 * (DKIOCGVTOC). 22858 * 22859 * Arguments: dev - the device number 22860 * arg - pointer to user provided vtoc structure specifying 22861 * the current vtoc. 22862 * flag - this argument is a pass through to ddi_copyxxx() 22863 * directly from the mode argument of ioctl(). 22864 * geom_validated - flag indicating if the device geometry has been 22865 * previously validated in the sdioctl routine. 22866 * 22867 * Return Code: 0 22868 * EFAULT 22869 * ENXIO 22870 * EIO 22871 */ 22872 22873 static int 22874 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22875 { 22876 struct sd_lun *un = NULL; 22877 #if defined(_SUNOS_VTOC_8) 22878 struct vtoc user_vtoc; 22879 #endif /* defined(_SUNOS_VTOC_8) */ 22880 int rval = 0; 22881 22882 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22883 return (ENXIO); 22884 } 22885 22886 mutex_enter(SD_MUTEX(un)); 22887 if (geom_validated == FALSE) { 22888 /* 22889 * sd_validate_geometry does not spin a disk up 22890 * if it was spun down. We need to make sure it 22891 * is ready. 22892 */ 22893 mutex_exit(SD_MUTEX(un)); 22894 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22895 return (rval); 22896 } 22897 mutex_enter(SD_MUTEX(un)); 22898 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22899 mutex_exit(SD_MUTEX(un)); 22900 return (rval); 22901 } 22902 } 22903 22904 #if defined(_SUNOS_VTOC_8) 22905 sd_build_user_vtoc(un, &user_vtoc); 22906 mutex_exit(SD_MUTEX(un)); 22907 22908 #ifdef _MULTI_DATAMODEL 22909 switch (ddi_model_convert_from(flag & FMODELS)) { 22910 case DDI_MODEL_ILP32: { 22911 struct vtoc32 user_vtoc32; 22912 22913 vtoctovtoc32(user_vtoc, user_vtoc32); 22914 if (ddi_copyout(&user_vtoc32, (void *)arg, 22915 sizeof (struct vtoc32), flag)) { 22916 return (EFAULT); 22917 } 22918 break; 22919 } 22920 22921 case DDI_MODEL_NONE: 22922 if (ddi_copyout(&user_vtoc, (void *)arg, 22923 sizeof (struct vtoc), flag)) { 22924 return (EFAULT); 22925 } 22926 break; 22927 } 22928 #else /* ! _MULTI_DATAMODEL */ 22929 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22930 return (EFAULT); 22931 } 22932 #endif /* _MULTI_DATAMODEL */ 22933 22934 #elif defined(_SUNOS_VTOC_16) 22935 mutex_exit(SD_MUTEX(un)); 22936 22937 #ifdef _MULTI_DATAMODEL 22938 /* 22939 * The un_vtoc structure is a "struct dk_vtoc" which is always 22940 * 32-bit to maintain compatibility with existing on-disk 22941 * structures. Thus, we need to convert the structure when copying 22942 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22943 * program. If the target is a 32-bit program, then no conversion 22944 * is necessary. 22945 */ 22946 /* LINTED: logical expression always true: op "||" */ 22947 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22948 switch (ddi_model_convert_from(flag & FMODELS)) { 22949 case DDI_MODEL_ILP32: 22950 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22951 sizeof (un->un_vtoc), flag)) { 22952 return (EFAULT); 22953 } 22954 break; 22955 22956 case DDI_MODEL_NONE: { 22957 struct vtoc user_vtoc; 22958 22959 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22960 if (ddi_copyout(&user_vtoc, (void *)arg, 22961 sizeof (struct vtoc), flag)) { 22962 return (EFAULT); 22963 } 22964 break; 22965 } 22966 } 22967 #else /* ! _MULTI_DATAMODEL */ 22968 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22969 flag)) { 22970 return (EFAULT); 22971 } 22972 #endif /* _MULTI_DATAMODEL */ 22973 #else 22974 #error "No VTOC format defined." 22975 #endif 22976 22977 return (rval); 22978 } 22979 22980 static int 22981 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22982 { 22983 struct sd_lun *un = NULL; 22984 dk_efi_t user_efi; 22985 int rval = 0; 22986 void *buffer; 22987 22988 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22989 return (ENXIO); 22990 22991 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22992 return (EFAULT); 22993 22994 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22995 22996 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22997 (user_efi.dki_length > un->un_max_xfer_size)) 22998 return (EINVAL); 22999 23000 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23001 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 23002 user_efi.dki_lba, SD_PATH_DIRECT); 23003 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 23004 user_efi.dki_length, flag) != 0) 23005 rval = EFAULT; 23006 23007 kmem_free(buffer, user_efi.dki_length); 23008 return (rval); 23009 } 23010 23011 #if defined(_SUNOS_VTOC_8) 23012 /* 23013 * Function: sd_build_user_vtoc 23014 * 23015 * Description: This routine populates a pass by reference variable with the 23016 * current volume table of contents. 23017 * 23018 * Arguments: un - driver soft state (unit) structure 23019 * user_vtoc - pointer to vtoc structure to be populated 23020 */ 23021 23022 static void 23023 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23024 { 23025 struct dk_map2 *lpart; 23026 struct dk_map *lmap; 23027 struct partition *vpart; 23028 int nblks; 23029 int i; 23030 23031 ASSERT(mutex_owned(SD_MUTEX(un))); 23032 23033 /* 23034 * Return vtoc structure fields in the provided VTOC area, addressed 23035 * by *vtoc. 23036 */ 23037 bzero(user_vtoc, sizeof (struct vtoc)); 23038 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23039 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23040 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23041 user_vtoc->v_sanity = VTOC_SANE; 23042 user_vtoc->v_version = un->un_vtoc.v_version; 23043 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23044 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23045 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23046 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23047 sizeof (un->un_vtoc.v_reserved)); 23048 /* 23049 * Convert partitioning information. 23050 * 23051 * Note the conversion from starting cylinder number 23052 * to starting sector number. 23053 */ 23054 lmap = un->un_map; 23055 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23056 vpart = user_vtoc->v_part; 23057 23058 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23059 23060 for (i = 0; i < V_NUMPAR; i++) { 23061 vpart->p_tag = lpart->p_tag; 23062 vpart->p_flag = lpart->p_flag; 23063 vpart->p_start = lmap->dkl_cylno * nblks; 23064 vpart->p_size = lmap->dkl_nblk; 23065 lmap++; 23066 lpart++; 23067 vpart++; 23068 23069 /* (4364927) */ 23070 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23071 } 23072 23073 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23074 } 23075 #endif 23076 23077 static int 23078 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23079 { 23080 struct sd_lun *un = NULL; 23081 struct partition64 p64; 23082 int rval = 0; 23083 uint_t nparts; 23084 efi_gpe_t *partitions; 23085 efi_gpt_t *buffer; 23086 diskaddr_t gpe_lba; 23087 23088 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23089 return (ENXIO); 23090 } 23091 23092 if (ddi_copyin((const void *)arg, &p64, 23093 sizeof (struct partition64), flag)) { 23094 return (EFAULT); 23095 } 23096 23097 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23098 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23099 1, SD_PATH_DIRECT); 23100 if (rval != 0) 23101 goto done_error; 23102 23103 sd_swap_efi_gpt(buffer); 23104 23105 if ((rval = sd_validate_efi(buffer)) != 0) 23106 goto done_error; 23107 23108 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23109 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23110 if (p64.p_partno > nparts) { 23111 /* couldn't find it */ 23112 rval = ESRCH; 23113 goto done_error; 23114 } 23115 /* 23116 * if we're dealing with a partition that's out of the normal 23117 * 16K block, adjust accordingly 23118 */ 23119 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23120 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23121 gpe_lba, SD_PATH_DIRECT); 23122 if (rval) { 23123 goto done_error; 23124 } 23125 partitions = (efi_gpe_t *)buffer; 23126 23127 sd_swap_efi_gpe(nparts, partitions); 23128 23129 partitions += p64.p_partno; 23130 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23131 sizeof (struct uuid)); 23132 p64.p_start = partitions->efi_gpe_StartingLBA; 23133 p64.p_size = partitions->efi_gpe_EndingLBA - 23134 p64.p_start + 1; 23135 23136 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23137 rval = EFAULT; 23138 23139 done_error: 23140 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23141 return (rval); 23142 } 23143 23144 23145 /* 23146 * Function: sd_dkio_set_vtoc 23147 * 23148 * Description: This routine is the driver entry point for handling user 23149 * requests to set the current volume table of contents 23150 * (DKIOCSVTOC). 23151 * 23152 * Arguments: dev - the device number 23153 * arg - pointer to user provided vtoc structure used to set the 23154 * current vtoc. 23155 * flag - this argument is a pass through to ddi_copyxxx() 23156 * directly from the mode argument of ioctl(). 23157 * 23158 * Return Code: 0 23159 * EFAULT 23160 * ENXIO 23161 * EINVAL 23162 * ENOTSUP 23163 */ 23164 23165 static int 23166 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23167 { 23168 struct sd_lun *un = NULL; 23169 struct vtoc user_vtoc; 23170 int rval = 0; 23171 23172 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23173 return (ENXIO); 23174 } 23175 23176 #if defined(__i386) || defined(__amd64) 23177 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23178 return (EINVAL); 23179 } 23180 #endif 23181 23182 #ifdef _MULTI_DATAMODEL 23183 switch (ddi_model_convert_from(flag & FMODELS)) { 23184 case DDI_MODEL_ILP32: { 23185 struct vtoc32 user_vtoc32; 23186 23187 if (ddi_copyin((const void *)arg, &user_vtoc32, 23188 sizeof (struct vtoc32), flag)) { 23189 return (EFAULT); 23190 } 23191 vtoc32tovtoc(user_vtoc32, user_vtoc); 23192 break; 23193 } 23194 23195 case DDI_MODEL_NONE: 23196 if (ddi_copyin((const void *)arg, &user_vtoc, 23197 sizeof (struct vtoc), flag)) { 23198 return (EFAULT); 23199 } 23200 break; 23201 } 23202 #else /* ! _MULTI_DATAMODEL */ 23203 if (ddi_copyin((const void *)arg, &user_vtoc, 23204 sizeof (struct vtoc), flag)) { 23205 return (EFAULT); 23206 } 23207 #endif /* _MULTI_DATAMODEL */ 23208 23209 mutex_enter(SD_MUTEX(un)); 23210 if (un->un_blockcount > DK_MAX_BLOCKS) { 23211 mutex_exit(SD_MUTEX(un)); 23212 return (ENOTSUP); 23213 } 23214 if (un->un_g.dkg_ncyl == 0) { 23215 mutex_exit(SD_MUTEX(un)); 23216 return (EINVAL); 23217 } 23218 23219 mutex_exit(SD_MUTEX(un)); 23220 sd_clear_efi(un); 23221 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23222 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23223 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23224 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23225 un->un_node_type, NULL); 23226 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23227 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23228 un->un_node_type, NULL); 23229 mutex_enter(SD_MUTEX(un)); 23230 23231 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23232 if ((rval = sd_write_label(dev)) == 0) { 23233 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23234 != 0) { 23235 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23236 "sd_dkio_set_vtoc: " 23237 "Failed validate geometry\n"); 23238 } 23239 } 23240 } 23241 23242 /* 23243 * If sd_build_label_vtoc, or sd_write_label failed above write the 23244 * devid anyway, what can it hurt? Also preserve the device id by 23245 * writing to the disk acyl for the case where a devid has been 23246 * fabricated. 23247 */ 23248 if (un->un_f_devid_supported && 23249 (un->un_f_opt_fab_devid == TRUE)) { 23250 if (un->un_devid == NULL) { 23251 sd_register_devid(un, SD_DEVINFO(un), 23252 SD_TARGET_IS_UNRESERVED); 23253 } else { 23254 /* 23255 * The device id for this disk has been 23256 * fabricated. Fabricated device id's are 23257 * managed by storing them in the last 2 23258 * available sectors on the drive. The device 23259 * id must be preserved by writing it back out 23260 * to this location. 23261 */ 23262 if (sd_write_deviceid(un) != 0) { 23263 ddi_devid_free(un->un_devid); 23264 un->un_devid = NULL; 23265 } 23266 } 23267 } 23268 mutex_exit(SD_MUTEX(un)); 23269 return (rval); 23270 } 23271 23272 23273 /* 23274 * Function: sd_build_label_vtoc 23275 * 23276 * Description: This routine updates the driver soft state current volume table 23277 * of contents based on a user specified vtoc. 23278 * 23279 * Arguments: un - driver soft state (unit) structure 23280 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23281 * to update the driver soft state. 23282 * 23283 * Return Code: 0 23284 * EINVAL 23285 */ 23286 23287 static int 23288 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23289 { 23290 struct dk_map *lmap; 23291 struct partition *vpart; 23292 int nblks; 23293 #if defined(_SUNOS_VTOC_8) 23294 int ncyl; 23295 struct dk_map2 *lpart; 23296 #endif /* defined(_SUNOS_VTOC_8) */ 23297 int i; 23298 23299 ASSERT(mutex_owned(SD_MUTEX(un))); 23300 23301 /* Sanity-check the vtoc */ 23302 if (user_vtoc->v_sanity != VTOC_SANE || 23303 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23304 user_vtoc->v_nparts != V_NUMPAR) { 23305 return (EINVAL); 23306 } 23307 23308 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23309 if (nblks == 0) { 23310 return (EINVAL); 23311 } 23312 23313 #if defined(_SUNOS_VTOC_8) 23314 vpart = user_vtoc->v_part; 23315 for (i = 0; i < V_NUMPAR; i++) { 23316 if ((vpart->p_start % nblks) != 0) { 23317 return (EINVAL); 23318 } 23319 ncyl = vpart->p_start / nblks; 23320 ncyl += vpart->p_size / nblks; 23321 if ((vpart->p_size % nblks) != 0) { 23322 ncyl++; 23323 } 23324 if (ncyl > (int)un->un_g.dkg_ncyl) { 23325 return (EINVAL); 23326 } 23327 vpart++; 23328 } 23329 #endif /* defined(_SUNOS_VTOC_8) */ 23330 23331 /* Put appropriate vtoc structure fields into the disk label */ 23332 #if defined(_SUNOS_VTOC_16) 23333 /* 23334 * The vtoc is always a 32bit data structure to maintain the 23335 * on-disk format. Convert "in place" instead of bcopying it. 23336 */ 23337 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23338 23339 /* 23340 * in the 16-slice vtoc, starting sectors are expressed in 23341 * numbers *relative* to the start of the Solaris fdisk partition. 23342 */ 23343 lmap = un->un_map; 23344 vpart = user_vtoc->v_part; 23345 23346 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23347 lmap->dkl_cylno = vpart->p_start / nblks; 23348 lmap->dkl_nblk = vpart->p_size; 23349 } 23350 23351 #elif defined(_SUNOS_VTOC_8) 23352 23353 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23354 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23355 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23356 23357 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23358 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23359 23360 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23361 23362 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23363 23364 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23365 sizeof (un->un_vtoc.v_reserved)); 23366 23367 /* 23368 * Note the conversion from starting sector number 23369 * to starting cylinder number. 23370 * Return error if division results in a remainder. 23371 */ 23372 lmap = un->un_map; 23373 lpart = un->un_vtoc.v_part; 23374 vpart = user_vtoc->v_part; 23375 23376 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23377 lpart->p_tag = vpart->p_tag; 23378 lpart->p_flag = vpart->p_flag; 23379 lmap->dkl_cylno = vpart->p_start / nblks; 23380 lmap->dkl_nblk = vpart->p_size; 23381 23382 lmap++; 23383 lpart++; 23384 vpart++; 23385 23386 /* (4387723) */ 23387 #ifdef _LP64 23388 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23389 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23390 } else { 23391 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23392 } 23393 #else 23394 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23395 #endif 23396 } 23397 23398 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23399 #else 23400 #error "No VTOC format defined." 23401 #endif 23402 return (0); 23403 } 23404 23405 /* 23406 * Function: sd_clear_efi 23407 * 23408 * Description: This routine clears all EFI labels. 23409 * 23410 * Arguments: un - driver soft state (unit) structure 23411 * 23412 * Return Code: void 23413 */ 23414 23415 static void 23416 sd_clear_efi(struct sd_lun *un) 23417 { 23418 efi_gpt_t *gpt; 23419 uint_t lbasize; 23420 uint64_t cap; 23421 int rval; 23422 23423 ASSERT(!mutex_owned(SD_MUTEX(un))); 23424 23425 mutex_enter(SD_MUTEX(un)); 23426 un->un_reserved = -1; 23427 mutex_exit(SD_MUTEX(un)); 23428 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23429 23430 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23431 goto done; 23432 } 23433 23434 sd_swap_efi_gpt(gpt); 23435 rval = sd_validate_efi(gpt); 23436 if (rval == 0) { 23437 /* clear primary */ 23438 bzero(gpt, sizeof (efi_gpt_t)); 23439 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23440 SD_PATH_DIRECT))) { 23441 SD_INFO(SD_LOG_IO_PARTITION, un, 23442 "sd_clear_efi: clear primary label failed\n"); 23443 } 23444 } 23445 /* the backup */ 23446 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23447 SD_PATH_DIRECT); 23448 if (rval) { 23449 goto done; 23450 } 23451 /* 23452 * The MMC standard allows READ CAPACITY to be 23453 * inaccurate by a bounded amount (in the interest of 23454 * response latency). As a result, failed READs are 23455 * commonplace (due to the reading of metadata and not 23456 * data). Depending on the per-Vendor/drive Sense data, 23457 * the failed READ can cause many (unnecessary) retries. 23458 */ 23459 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23460 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23461 SD_PATH_DIRECT)) != 0) { 23462 goto done; 23463 } 23464 sd_swap_efi_gpt(gpt); 23465 rval = sd_validate_efi(gpt); 23466 if (rval == 0) { 23467 /* clear backup */ 23468 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23469 cap-1); 23470 bzero(gpt, sizeof (efi_gpt_t)); 23471 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23472 cap-1, SD_PATH_DIRECT))) { 23473 SD_INFO(SD_LOG_IO_PARTITION, un, 23474 "sd_clear_efi: clear backup label failed\n"); 23475 } 23476 } else { 23477 /* 23478 * Refer to comments related to off-by-1 at the 23479 * header of this file 23480 */ 23481 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23482 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23483 SD_PATH_DIRECT)) != 0) { 23484 goto done; 23485 } 23486 sd_swap_efi_gpt(gpt); 23487 rval = sd_validate_efi(gpt); 23488 if (rval == 0) { 23489 /* clear legacy backup EFI label */ 23490 SD_TRACE(SD_LOG_IOCTL, un, 23491 "sd_clear_efi clear backup@%lu\n", cap-2); 23492 bzero(gpt, sizeof (efi_gpt_t)); 23493 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23494 cap-2, SD_PATH_DIRECT))) { 23495 SD_INFO(SD_LOG_IO_PARTITION, 23496 un, "sd_clear_efi: " 23497 " clear legacy backup label failed\n"); 23498 } 23499 } 23500 } 23501 23502 done: 23503 kmem_free(gpt, sizeof (efi_gpt_t)); 23504 } 23505 23506 /* 23507 * Function: sd_set_vtoc 23508 * 23509 * Description: This routine writes data to the appropriate positions 23510 * 23511 * Arguments: un - driver soft state (unit) structure 23512 * dkl - the data to be written 23513 * 23514 * Return: void 23515 */ 23516 23517 static int 23518 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23519 { 23520 void *shadow_buf; 23521 uint_t label_addr; 23522 int sec; 23523 int blk; 23524 int head; 23525 int cyl; 23526 int rval; 23527 23528 #if defined(__i386) || defined(__amd64) 23529 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23530 #else 23531 /* Write the primary label at block 0 of the solaris partition. */ 23532 label_addr = 0; 23533 #endif 23534 23535 if (NOT_DEVBSIZE(un)) { 23536 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23537 /* 23538 * Read the target's first block. 23539 */ 23540 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23541 un->un_tgt_blocksize, label_addr, 23542 SD_PATH_STANDARD)) != 0) { 23543 goto exit; 23544 } 23545 /* 23546 * Copy the contents of the label into the shadow buffer 23547 * which is of the size of target block size. 23548 */ 23549 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23550 } 23551 23552 /* Write the primary label */ 23553 if (NOT_DEVBSIZE(un)) { 23554 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23555 label_addr, SD_PATH_STANDARD); 23556 } else { 23557 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23558 label_addr, SD_PATH_STANDARD); 23559 } 23560 if (rval != 0) { 23561 return (rval); 23562 } 23563 23564 /* 23565 * Calculate where the backup labels go. They are always on 23566 * the last alternate cylinder, but some older drives put them 23567 * on head 2 instead of the last head. They are always on the 23568 * first 5 odd sectors of the appropriate track. 23569 * 23570 * We have no choice at this point, but to believe that the 23571 * disk label is valid. Use the geometry of the disk 23572 * as described in the label. 23573 */ 23574 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23575 head = dkl->dkl_nhead - 1; 23576 23577 /* 23578 * Write and verify the backup labels. Make sure we don't try to 23579 * write past the last cylinder. 23580 */ 23581 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23582 blk = (daddr_t)( 23583 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23584 (head * dkl->dkl_nsect) + sec); 23585 #if defined(__i386) || defined(__amd64) 23586 blk += un->un_solaris_offset; 23587 #endif 23588 if (NOT_DEVBSIZE(un)) { 23589 uint64_t tblk; 23590 /* 23591 * Need to read the block first for read modify write. 23592 */ 23593 tblk = (uint64_t)blk; 23594 blk = (int)((tblk * un->un_sys_blocksize) / 23595 un->un_tgt_blocksize); 23596 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23597 un->un_tgt_blocksize, blk, 23598 SD_PATH_STANDARD)) != 0) { 23599 goto exit; 23600 } 23601 /* 23602 * Modify the shadow buffer with the label. 23603 */ 23604 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23605 rval = sd_send_scsi_WRITE(un, shadow_buf, 23606 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23607 } else { 23608 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23609 blk, SD_PATH_STANDARD); 23610 SD_INFO(SD_LOG_IO_PARTITION, un, 23611 "sd_set_vtoc: wrote backup label %d\n", blk); 23612 } 23613 if (rval != 0) { 23614 goto exit; 23615 } 23616 } 23617 exit: 23618 if (NOT_DEVBSIZE(un)) { 23619 kmem_free(shadow_buf, un->un_tgt_blocksize); 23620 } 23621 return (rval); 23622 } 23623 23624 /* 23625 * Function: sd_clear_vtoc 23626 * 23627 * Description: This routine clears out the VTOC labels. 23628 * 23629 * Arguments: un - driver soft state (unit) structure 23630 * 23631 * Return: void 23632 */ 23633 23634 static void 23635 sd_clear_vtoc(struct sd_lun *un) 23636 { 23637 struct dk_label *dkl; 23638 23639 mutex_exit(SD_MUTEX(un)); 23640 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23641 mutex_enter(SD_MUTEX(un)); 23642 /* 23643 * sd_set_vtoc uses these fields in order to figure out 23644 * where to overwrite the backup labels 23645 */ 23646 dkl->dkl_apc = un->un_g.dkg_apc; 23647 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23648 dkl->dkl_acyl = un->un_g.dkg_acyl; 23649 dkl->dkl_nhead = un->un_g.dkg_nhead; 23650 dkl->dkl_nsect = un->un_g.dkg_nsect; 23651 mutex_exit(SD_MUTEX(un)); 23652 (void) sd_set_vtoc(un, dkl); 23653 kmem_free(dkl, sizeof (struct dk_label)); 23654 23655 mutex_enter(SD_MUTEX(un)); 23656 } 23657 23658 /* 23659 * Function: sd_write_label 23660 * 23661 * Description: This routine will validate and write the driver soft state vtoc 23662 * contents to the device. 23663 * 23664 * Arguments: dev - the device number 23665 * 23666 * Return Code: the code returned by sd_send_scsi_cmd() 23667 * 0 23668 * EINVAL 23669 * ENXIO 23670 * ENOMEM 23671 */ 23672 23673 static int 23674 sd_write_label(dev_t dev) 23675 { 23676 struct sd_lun *un; 23677 struct dk_label *dkl; 23678 short sum; 23679 short *sp; 23680 int i; 23681 int rval; 23682 23683 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23684 (un->un_state == SD_STATE_OFFLINE)) { 23685 return (ENXIO); 23686 } 23687 ASSERT(mutex_owned(SD_MUTEX(un))); 23688 mutex_exit(SD_MUTEX(un)); 23689 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23690 mutex_enter(SD_MUTEX(un)); 23691 23692 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23693 dkl->dkl_rpm = un->un_g.dkg_rpm; 23694 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23695 dkl->dkl_apc = un->un_g.dkg_apc; 23696 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23697 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23698 dkl->dkl_acyl = un->un_g.dkg_acyl; 23699 dkl->dkl_nhead = un->un_g.dkg_nhead; 23700 dkl->dkl_nsect = un->un_g.dkg_nsect; 23701 23702 #if defined(_SUNOS_VTOC_8) 23703 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23704 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23705 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23706 for (i = 0; i < NDKMAP; i++) { 23707 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23708 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23709 } 23710 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23711 #elif defined(_SUNOS_VTOC_16) 23712 dkl->dkl_skew = un->un_dkg_skew; 23713 #else 23714 #error "No VTOC format defined." 23715 #endif 23716 23717 dkl->dkl_magic = DKL_MAGIC; 23718 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23719 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23720 23721 /* Construct checksum for the new disk label */ 23722 sum = 0; 23723 sp = (short *)dkl; 23724 i = sizeof (struct dk_label) / sizeof (short); 23725 while (i--) { 23726 sum ^= *sp++; 23727 } 23728 dkl->dkl_cksum = sum; 23729 23730 mutex_exit(SD_MUTEX(un)); 23731 23732 rval = sd_set_vtoc(un, dkl); 23733 exit: 23734 kmem_free(dkl, sizeof (struct dk_label)); 23735 mutex_enter(SD_MUTEX(un)); 23736 return (rval); 23737 } 23738 23739 static int 23740 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23741 { 23742 struct sd_lun *un = NULL; 23743 dk_efi_t user_efi; 23744 int rval = 0; 23745 void *buffer; 23746 int valid_efi; 23747 23748 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23749 return (ENXIO); 23750 23751 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23752 return (EFAULT); 23753 23754 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23755 23756 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23757 (user_efi.dki_length > un->un_max_xfer_size)) 23758 return (EINVAL); 23759 23760 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23761 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23762 rval = EFAULT; 23763 } else { 23764 /* 23765 * let's clear the vtoc labels and clear the softstate 23766 * vtoc. 23767 */ 23768 mutex_enter(SD_MUTEX(un)); 23769 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23770 SD_TRACE(SD_LOG_IO_PARTITION, un, 23771 "sd_dkio_set_efi: CLEAR VTOC\n"); 23772 sd_clear_vtoc(un); 23773 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23774 mutex_exit(SD_MUTEX(un)); 23775 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23776 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23777 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23778 S_IFBLK, 23779 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23780 un->un_node_type, NULL); 23781 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23782 S_IFCHR, 23783 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23784 un->un_node_type, NULL); 23785 } else 23786 mutex_exit(SD_MUTEX(un)); 23787 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23788 user_efi.dki_lba, SD_PATH_DIRECT); 23789 if (rval == 0) { 23790 mutex_enter(SD_MUTEX(un)); 23791 23792 /* 23793 * Set the un_reserved for valid efi label. 23794 * Function clear_efi in fdisk and efi_write in 23795 * libefi both change efi label on disk in 3 steps 23796 * 1. Change primary gpt and gpe 23797 * 2. Change backup gpe 23798 * 3. Change backup gpt, which is one block 23799 * We only reread the efi label after the 3rd step, 23800 * or there will be warning "primary label corrupt". 23801 */ 23802 if (user_efi.dki_length == un->un_tgt_blocksize) { 23803 un->un_f_geometry_is_valid = FALSE; 23804 valid_efi = sd_use_efi(un, SD_PATH_DIRECT); 23805 if ((valid_efi == 0) && 23806 un->un_f_devid_supported && 23807 (un->un_f_opt_fab_devid == TRUE)) { 23808 if (un->un_devid == NULL) { 23809 sd_register_devid(un, 23810 SD_DEVINFO(un), 23811 SD_TARGET_IS_UNRESERVED); 23812 } else { 23813 /* 23814 * The device id for this disk 23815 * has been fabricated. The 23816 * device id must be preserved 23817 * by writing it back out to 23818 * disk. 23819 */ 23820 if (sd_write_deviceid(un) 23821 != 0) { 23822 ddi_devid_free( 23823 un->un_devid); 23824 un->un_devid = NULL; 23825 } 23826 } 23827 } 23828 } 23829 23830 mutex_exit(SD_MUTEX(un)); 23831 } 23832 } 23833 kmem_free(buffer, user_efi.dki_length); 23834 return (rval); 23835 } 23836 23837 /* 23838 * Function: sd_dkio_get_mboot 23839 * 23840 * Description: This routine is the driver entry point for handling user 23841 * requests to get the current device mboot (DKIOCGMBOOT) 23842 * 23843 * Arguments: dev - the device number 23844 * arg - pointer to user provided mboot structure specifying 23845 * the current mboot. 23846 * flag - this argument is a pass through to ddi_copyxxx() 23847 * directly from the mode argument of ioctl(). 23848 * 23849 * Return Code: 0 23850 * EINVAL 23851 * EFAULT 23852 * ENXIO 23853 */ 23854 23855 static int 23856 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23857 { 23858 struct sd_lun *un; 23859 struct mboot *mboot; 23860 int rval; 23861 size_t buffer_size; 23862 23863 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23864 (un->un_state == SD_STATE_OFFLINE)) { 23865 return (ENXIO); 23866 } 23867 23868 if (!un->un_f_mboot_supported || arg == NULL) { 23869 return (EINVAL); 23870 } 23871 23872 /* 23873 * Read the mboot block, located at absolute block 0 on the target. 23874 */ 23875 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23876 23877 SD_TRACE(SD_LOG_IO_PARTITION, un, 23878 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23879 23880 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23881 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23882 SD_PATH_STANDARD)) == 0) { 23883 if (ddi_copyout(mboot, (void *)arg, 23884 sizeof (struct mboot), flag) != 0) { 23885 rval = EFAULT; 23886 } 23887 } 23888 kmem_free(mboot, buffer_size); 23889 return (rval); 23890 } 23891 23892 23893 /* 23894 * Function: sd_dkio_set_mboot 23895 * 23896 * Description: This routine is the driver entry point for handling user 23897 * requests to validate and set the device master boot 23898 * (DKIOCSMBOOT). 23899 * 23900 * Arguments: dev - the device number 23901 * arg - pointer to user provided mboot structure used to set the 23902 * master boot. 23903 * flag - this argument is a pass through to ddi_copyxxx() 23904 * directly from the mode argument of ioctl(). 23905 * 23906 * Return Code: 0 23907 * EINVAL 23908 * EFAULT 23909 * ENXIO 23910 */ 23911 23912 static int 23913 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23914 { 23915 struct sd_lun *un = NULL; 23916 struct mboot *mboot = NULL; 23917 int rval; 23918 ushort_t magic; 23919 23920 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23921 return (ENXIO); 23922 } 23923 23924 ASSERT(!mutex_owned(SD_MUTEX(un))); 23925 23926 if (!un->un_f_mboot_supported) { 23927 return (EINVAL); 23928 } 23929 23930 if (arg == NULL) { 23931 return (EINVAL); 23932 } 23933 23934 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23935 23936 if (ddi_copyin((const void *)arg, mboot, 23937 sizeof (struct mboot), flag) != 0) { 23938 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23939 return (EFAULT); 23940 } 23941 23942 /* Is this really a master boot record? */ 23943 magic = LE_16(mboot->signature); 23944 if (magic != MBB_MAGIC) { 23945 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23946 return (EINVAL); 23947 } 23948 23949 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23950 SD_PATH_STANDARD); 23951 23952 mutex_enter(SD_MUTEX(un)); 23953 #if defined(__i386) || defined(__amd64) 23954 if (rval == 0) { 23955 /* 23956 * mboot has been written successfully. 23957 * update the fdisk and vtoc tables in memory 23958 */ 23959 rval = sd_update_fdisk_and_vtoc(un); 23960 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23961 mutex_exit(SD_MUTEX(un)); 23962 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23963 return (rval); 23964 } 23965 } 23966 23967 #ifdef __lock_lint 23968 sd_setup_default_geometry(un); 23969 #endif 23970 23971 #else 23972 if (rval == 0) { 23973 /* 23974 * mboot has been written successfully. 23975 * set up the default geometry and VTOC 23976 */ 23977 if (un->un_blockcount <= DK_MAX_BLOCKS) 23978 sd_setup_default_geometry(un); 23979 } 23980 #endif 23981 mutex_exit(SD_MUTEX(un)); 23982 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23983 return (rval); 23984 } 23985 23986 23987 /* 23988 * Function: sd_setup_default_geometry 23989 * 23990 * Description: This local utility routine sets the default geometry as part of 23991 * setting the device mboot. 23992 * 23993 * Arguments: un - driver soft state (unit) structure 23994 * 23995 * Note: This may be redundant with sd_build_default_label. 23996 */ 23997 23998 static void 23999 sd_setup_default_geometry(struct sd_lun *un) 24000 { 24001 /* zero out the soft state geometry and partition table. */ 24002 bzero(&un->un_g, sizeof (struct dk_geom)); 24003 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24004 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 24005 un->un_asciilabel[0] = '\0'; 24006 24007 /* 24008 * For the rpm, we use the minimum for the disk. 24009 * For the head, cyl and number of sector per track, 24010 * if the capacity <= 1GB, head = 64, sect = 32. 24011 * else head = 255, sect 63 24012 * Note: the capacity should be equal to C*H*S values. 24013 * This will cause some truncation of size due to 24014 * round off errors. For CD-ROMs, this truncation can 24015 * have adverse side effects, so returning ncyl and 24016 * nhead as 1. The nsect will overflow for most of 24017 * CD-ROMs as nsect is of type ushort. 24018 */ 24019 if (ISCD(un)) { 24020 un->un_g.dkg_ncyl = 1; 24021 un->un_g.dkg_nhead = 1; 24022 un->un_g.dkg_nsect = un->un_blockcount; 24023 } else { 24024 if (un->un_blockcount <= 0x1000) { 24025 /* Needed for unlabeled SCSI floppies. */ 24026 un->un_g.dkg_nhead = 2; 24027 un->un_g.dkg_ncyl = 80; 24028 un->un_g.dkg_pcyl = 80; 24029 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24030 } else if (un->un_blockcount <= 0x200000) { 24031 un->un_g.dkg_nhead = 64; 24032 un->un_g.dkg_nsect = 32; 24033 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24034 } else { 24035 un->un_g.dkg_nhead = 255; 24036 un->un_g.dkg_nsect = 63; 24037 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24038 } 24039 un->un_blockcount = un->un_g.dkg_ncyl * 24040 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24041 } 24042 un->un_g.dkg_acyl = 0; 24043 un->un_g.dkg_bcyl = 0; 24044 un->un_g.dkg_intrlv = 1; 24045 un->un_g.dkg_rpm = 200; 24046 un->un_g.dkg_read_reinstruct = 0; 24047 un->un_g.dkg_write_reinstruct = 0; 24048 if (un->un_g.dkg_pcyl == 0) { 24049 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24050 } 24051 24052 un->un_map['a'-'a'].dkl_cylno = 0; 24053 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24054 un->un_map['c'-'a'].dkl_cylno = 0; 24055 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24056 un->un_f_geometry_is_valid = FALSE; 24057 } 24058 24059 24060 #if defined(__i386) || defined(__amd64) 24061 /* 24062 * Function: sd_update_fdisk_and_vtoc 24063 * 24064 * Description: This local utility routine updates the device fdisk and vtoc 24065 * as part of setting the device mboot. 24066 * 24067 * Arguments: un - driver soft state (unit) structure 24068 * 24069 * Return Code: 0 for success or errno-type return code. 24070 * 24071 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24072 * these did exist seperately in x86 sd.c!!! 24073 */ 24074 24075 static int 24076 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24077 { 24078 static char labelstring[128]; 24079 static char buf[256]; 24080 char *label = 0; 24081 int count; 24082 int label_rc = 0; 24083 int gvalid = un->un_f_geometry_is_valid; 24084 int fdisk_rval; 24085 int lbasize; 24086 int capacity; 24087 24088 ASSERT(mutex_owned(SD_MUTEX(un))); 24089 24090 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24091 return (EINVAL); 24092 } 24093 24094 if (un->un_f_blockcount_is_valid == FALSE) { 24095 return (EINVAL); 24096 } 24097 24098 #if defined(_SUNOS_VTOC_16) 24099 /* 24100 * Set up the "whole disk" fdisk partition; this should always 24101 * exist, regardless of whether the disk contains an fdisk table 24102 * or vtoc. 24103 */ 24104 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24105 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24106 #endif /* defined(_SUNOS_VTOC_16) */ 24107 24108 /* 24109 * copy the lbasize and capacity so that if they're 24110 * reset while we're not holding the SD_MUTEX(un), we will 24111 * continue to use valid values after the SD_MUTEX(un) is 24112 * reacquired. 24113 */ 24114 lbasize = un->un_tgt_blocksize; 24115 capacity = un->un_blockcount; 24116 24117 /* 24118 * refresh the logical and physical geometry caches. 24119 * (data from mode sense format/rigid disk geometry pages, 24120 * and scsi_ifgetcap("geometry"). 24121 */ 24122 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24123 24124 /* 24125 * Only DIRECT ACCESS devices will have Sun labels. 24126 * CD's supposedly have a Sun label, too 24127 */ 24128 if (un->un_f_vtoc_label_supported) { 24129 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24130 SD_PATH_DIRECT); 24131 if (fdisk_rval == SD_CMD_FAILURE) { 24132 ASSERT(mutex_owned(SD_MUTEX(un))); 24133 return (EIO); 24134 } 24135 24136 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24137 ASSERT(mutex_owned(SD_MUTEX(un))); 24138 return (EACCES); 24139 } 24140 24141 if (un->un_solaris_size <= DK_LABEL_LOC) { 24142 /* 24143 * Found fdisk table but no Solaris partition entry, 24144 * so don't call sd_uselabel() and don't create 24145 * a default label. 24146 */ 24147 label_rc = 0; 24148 un->un_f_geometry_is_valid = TRUE; 24149 goto no_solaris_partition; 24150 } 24151 24152 #if defined(_SUNOS_VTOC_8) 24153 label = (char *)un->un_asciilabel; 24154 #elif defined(_SUNOS_VTOC_16) 24155 label = (char *)un->un_vtoc.v_asciilabel; 24156 #else 24157 #error "No VTOC format defined." 24158 #endif 24159 } else if (capacity < 0) { 24160 ASSERT(mutex_owned(SD_MUTEX(un))); 24161 return (EINVAL); 24162 } 24163 24164 /* 24165 * For Removable media We reach here if we have found a 24166 * SOLARIS PARTITION. 24167 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24168 * PARTITION has changed from the previous one, hence we will setup a 24169 * default VTOC in this case. 24170 */ 24171 if (un->un_f_geometry_is_valid == FALSE) { 24172 sd_build_default_label(un); 24173 label_rc = 0; 24174 } 24175 24176 no_solaris_partition: 24177 if ((!un->un_f_has_removable_media || 24178 (un->un_f_has_removable_media && 24179 un->un_mediastate == DKIO_EJECTED)) && 24180 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24181 /* 24182 * Print out a message indicating who and what we are. 24183 * We do this only when we happen to really validate the 24184 * geometry. We may call sd_validate_geometry() at other 24185 * times, ioctl()'s like Get VTOC in which case we 24186 * don't want to print the label. 24187 * If the geometry is valid, print the label string, 24188 * else print vendor and product info, if available 24189 */ 24190 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24191 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24192 } else { 24193 mutex_enter(&sd_label_mutex); 24194 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24195 labelstring); 24196 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24197 &labelstring[64]); 24198 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24199 labelstring, &labelstring[64]); 24200 if (un->un_f_blockcount_is_valid == TRUE) { 24201 (void) sprintf(&buf[strlen(buf)], 24202 ", %" PRIu64 " %u byte blocks\n", 24203 un->un_blockcount, 24204 un->un_tgt_blocksize); 24205 } else { 24206 (void) sprintf(&buf[strlen(buf)], 24207 ", (unknown capacity)\n"); 24208 } 24209 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24210 mutex_exit(&sd_label_mutex); 24211 } 24212 } 24213 24214 #if defined(_SUNOS_VTOC_16) 24215 /* 24216 * If we have valid geometry, set up the remaining fdisk partitions. 24217 * Note that dkl_cylno is not used for the fdisk map entries, so 24218 * we set it to an entirely bogus value. 24219 */ 24220 for (count = 0; count < FD_NUMPART; count++) { 24221 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24222 un->un_map[FDISK_P1 + count].dkl_nblk = 24223 un->un_fmap[count].fmap_nblk; 24224 un->un_offset[FDISK_P1 + count] = 24225 un->un_fmap[count].fmap_start; 24226 } 24227 #endif 24228 24229 for (count = 0; count < NDKMAP; count++) { 24230 #if defined(_SUNOS_VTOC_8) 24231 struct dk_map *lp = &un->un_map[count]; 24232 un->un_offset[count] = 24233 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24234 #elif defined(_SUNOS_VTOC_16) 24235 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24236 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24237 #else 24238 #error "No VTOC format defined." 24239 #endif 24240 } 24241 24242 ASSERT(mutex_owned(SD_MUTEX(un))); 24243 return (label_rc); 24244 } 24245 #endif 24246 24247 24248 /* 24249 * Function: sd_check_media 24250 * 24251 * Description: This utility routine implements the functionality for the 24252 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24253 * driver state changes from that specified by the user 24254 * (inserted or ejected). For example, if the user specifies 24255 * DKIO_EJECTED and the current media state is inserted this 24256 * routine will immediately return DKIO_INSERTED. However, if the 24257 * current media state is not inserted the user thread will be 24258 * blocked until the drive state changes. If DKIO_NONE is specified 24259 * the user thread will block until a drive state change occurs. 24260 * 24261 * Arguments: dev - the device number 24262 * state - user pointer to a dkio_state, updated with the current 24263 * drive state at return. 24264 * 24265 * Return Code: ENXIO 24266 * EIO 24267 * EAGAIN 24268 * EINTR 24269 */ 24270 24271 static int 24272 sd_check_media(dev_t dev, enum dkio_state state) 24273 { 24274 struct sd_lun *un = NULL; 24275 enum dkio_state prev_state; 24276 opaque_t token = NULL; 24277 int rval = 0; 24278 24279 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24280 return (ENXIO); 24281 } 24282 24283 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24284 24285 mutex_enter(SD_MUTEX(un)); 24286 24287 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24288 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24289 24290 prev_state = un->un_mediastate; 24291 24292 /* is there anything to do? */ 24293 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24294 /* 24295 * submit the request to the scsi_watch service; 24296 * scsi_media_watch_cb() does the real work 24297 */ 24298 mutex_exit(SD_MUTEX(un)); 24299 24300 /* 24301 * This change handles the case where a scsi watch request is 24302 * added to a device that is powered down. To accomplish this 24303 * we power up the device before adding the scsi watch request, 24304 * since the scsi watch sends a TUR directly to the device 24305 * which the device cannot handle if it is powered down. 24306 */ 24307 if (sd_pm_entry(un) != DDI_SUCCESS) { 24308 mutex_enter(SD_MUTEX(un)); 24309 goto done; 24310 } 24311 24312 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24313 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24314 (caddr_t)dev); 24315 24316 sd_pm_exit(un); 24317 24318 mutex_enter(SD_MUTEX(un)); 24319 if (token == NULL) { 24320 rval = EAGAIN; 24321 goto done; 24322 } 24323 24324 /* 24325 * This is a special case IOCTL that doesn't return 24326 * until the media state changes. Routine sdpower 24327 * knows about and handles this so don't count it 24328 * as an active cmd in the driver, which would 24329 * keep the device busy to the pm framework. 24330 * If the count isn't decremented the device can't 24331 * be powered down. 24332 */ 24333 un->un_ncmds_in_driver--; 24334 ASSERT(un->un_ncmds_in_driver >= 0); 24335 24336 /* 24337 * if a prior request had been made, this will be the same 24338 * token, as scsi_watch was designed that way. 24339 */ 24340 un->un_swr_token = token; 24341 un->un_specified_mediastate = state; 24342 24343 /* 24344 * now wait for media change 24345 * we will not be signalled unless mediastate == state but it is 24346 * still better to test for this condition, since there is a 24347 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24348 */ 24349 SD_TRACE(SD_LOG_COMMON, un, 24350 "sd_check_media: waiting for media state change\n"); 24351 while (un->un_mediastate == state) { 24352 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24353 SD_TRACE(SD_LOG_COMMON, un, 24354 "sd_check_media: waiting for media state " 24355 "was interrupted\n"); 24356 un->un_ncmds_in_driver++; 24357 rval = EINTR; 24358 goto done; 24359 } 24360 SD_TRACE(SD_LOG_COMMON, un, 24361 "sd_check_media: received signal, state=%x\n", 24362 un->un_mediastate); 24363 } 24364 /* 24365 * Inc the counter to indicate the device once again 24366 * has an active outstanding cmd. 24367 */ 24368 un->un_ncmds_in_driver++; 24369 } 24370 24371 /* invalidate geometry */ 24372 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24373 sr_ejected(un); 24374 } 24375 24376 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24377 uint64_t capacity; 24378 uint_t lbasize; 24379 24380 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24381 mutex_exit(SD_MUTEX(un)); 24382 /* 24383 * Since the following routines use SD_PATH_DIRECT, we must 24384 * call PM directly before the upcoming disk accesses. This 24385 * may cause the disk to be power/spin up. 24386 */ 24387 24388 if (sd_pm_entry(un) == DDI_SUCCESS) { 24389 rval = sd_send_scsi_READ_CAPACITY(un, 24390 &capacity, 24391 &lbasize, SD_PATH_DIRECT); 24392 if (rval != 0) { 24393 sd_pm_exit(un); 24394 mutex_enter(SD_MUTEX(un)); 24395 goto done; 24396 } 24397 } else { 24398 rval = EIO; 24399 mutex_enter(SD_MUTEX(un)); 24400 goto done; 24401 } 24402 mutex_enter(SD_MUTEX(un)); 24403 24404 sd_update_block_info(un, lbasize, capacity); 24405 24406 un->un_f_geometry_is_valid = FALSE; 24407 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24408 24409 mutex_exit(SD_MUTEX(un)); 24410 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24411 SD_PATH_DIRECT); 24412 sd_pm_exit(un); 24413 24414 mutex_enter(SD_MUTEX(un)); 24415 } 24416 done: 24417 un->un_f_watcht_stopped = FALSE; 24418 if (un->un_swr_token) { 24419 /* 24420 * Use of this local token and the mutex ensures that we avoid 24421 * some race conditions associated with terminating the 24422 * scsi watch. 24423 */ 24424 token = un->un_swr_token; 24425 un->un_swr_token = (opaque_t)NULL; 24426 mutex_exit(SD_MUTEX(un)); 24427 (void) scsi_watch_request_terminate(token, 24428 SCSI_WATCH_TERMINATE_WAIT); 24429 mutex_enter(SD_MUTEX(un)); 24430 } 24431 24432 /* 24433 * Update the capacity kstat value, if no media previously 24434 * (capacity kstat is 0) and a media has been inserted 24435 * (un_f_blockcount_is_valid == TRUE) 24436 */ 24437 if (un->un_errstats) { 24438 struct sd_errstats *stp = NULL; 24439 24440 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24441 if ((stp->sd_capacity.value.ui64 == 0) && 24442 (un->un_f_blockcount_is_valid == TRUE)) { 24443 stp->sd_capacity.value.ui64 = 24444 (uint64_t)((uint64_t)un->un_blockcount * 24445 un->un_sys_blocksize); 24446 } 24447 } 24448 mutex_exit(SD_MUTEX(un)); 24449 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24450 return (rval); 24451 } 24452 24453 24454 /* 24455 * Function: sd_delayed_cv_broadcast 24456 * 24457 * Description: Delayed cv_broadcast to allow for target to recover from media 24458 * insertion. 24459 * 24460 * Arguments: arg - driver soft state (unit) structure 24461 */ 24462 24463 static void 24464 sd_delayed_cv_broadcast(void *arg) 24465 { 24466 struct sd_lun *un = arg; 24467 24468 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24469 24470 mutex_enter(SD_MUTEX(un)); 24471 un->un_dcvb_timeid = NULL; 24472 cv_broadcast(&un->un_state_cv); 24473 mutex_exit(SD_MUTEX(un)); 24474 } 24475 24476 24477 /* 24478 * Function: sd_media_watch_cb 24479 * 24480 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24481 * routine processes the TUR sense data and updates the driver 24482 * state if a transition has occurred. The user thread 24483 * (sd_check_media) is then signalled. 24484 * 24485 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24486 * among multiple watches that share this callback function 24487 * resultp - scsi watch facility result packet containing scsi 24488 * packet, status byte and sense data 24489 * 24490 * Return Code: 0 for success, -1 for failure 24491 */ 24492 24493 static int 24494 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24495 { 24496 struct sd_lun *un; 24497 struct scsi_status *statusp = resultp->statusp; 24498 uint8_t *sensep = (uint8_t *)resultp->sensep; 24499 enum dkio_state state = DKIO_NONE; 24500 dev_t dev = (dev_t)arg; 24501 uchar_t actual_sense_length; 24502 uint8_t skey, asc, ascq; 24503 24504 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24505 return (-1); 24506 } 24507 actual_sense_length = resultp->actual_sense_length; 24508 24509 mutex_enter(SD_MUTEX(un)); 24510 SD_TRACE(SD_LOG_COMMON, un, 24511 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24512 *((char *)statusp), (void *)sensep, actual_sense_length); 24513 24514 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24515 un->un_mediastate = DKIO_DEV_GONE; 24516 cv_broadcast(&un->un_state_cv); 24517 mutex_exit(SD_MUTEX(un)); 24518 24519 return (0); 24520 } 24521 24522 /* 24523 * If there was a check condition then sensep points to valid sense data 24524 * If status was not a check condition but a reservation or busy status 24525 * then the new state is DKIO_NONE 24526 */ 24527 if (sensep != NULL) { 24528 skey = scsi_sense_key(sensep); 24529 asc = scsi_sense_asc(sensep); 24530 ascq = scsi_sense_ascq(sensep); 24531 24532 SD_INFO(SD_LOG_COMMON, un, 24533 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24534 skey, asc, ascq); 24535 /* This routine only uses up to 13 bytes of sense data. */ 24536 if (actual_sense_length >= 13) { 24537 if (skey == KEY_UNIT_ATTENTION) { 24538 if (asc == 0x28) { 24539 state = DKIO_INSERTED; 24540 } 24541 } else { 24542 /* 24543 * if 02/04/02 means that the host 24544 * should send start command. Explicitly 24545 * leave the media state as is 24546 * (inserted) as the media is inserted 24547 * and host has stopped device for PM 24548 * reasons. Upon next true read/write 24549 * to this media will bring the 24550 * device to the right state good for 24551 * media access. 24552 */ 24553 if ((skey == KEY_NOT_READY) && 24554 (asc == 0x3a)) { 24555 state = DKIO_EJECTED; 24556 } 24557 24558 /* 24559 * If the drivge is busy with an operation 24560 * or long write, keep the media in an 24561 * inserted state. 24562 */ 24563 24564 if ((skey == KEY_NOT_READY) && 24565 (asc == 0x04) && 24566 ((ascq == 0x02) || 24567 (ascq == 0x07) || 24568 (ascq == 0x08))) { 24569 state = DKIO_INSERTED; 24570 } 24571 } 24572 } 24573 } else if ((*((char *)statusp) == STATUS_GOOD) && 24574 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24575 state = DKIO_INSERTED; 24576 } 24577 24578 SD_TRACE(SD_LOG_COMMON, un, 24579 "sd_media_watch_cb: state=%x, specified=%x\n", 24580 state, un->un_specified_mediastate); 24581 24582 /* 24583 * now signal the waiting thread if this is *not* the specified state; 24584 * delay the signal if the state is DKIO_INSERTED to allow the target 24585 * to recover 24586 */ 24587 if (state != un->un_specified_mediastate) { 24588 un->un_mediastate = state; 24589 if (state == DKIO_INSERTED) { 24590 /* 24591 * delay the signal to give the drive a chance 24592 * to do what it apparently needs to do 24593 */ 24594 SD_TRACE(SD_LOG_COMMON, un, 24595 "sd_media_watch_cb: delayed cv_broadcast\n"); 24596 if (un->un_dcvb_timeid == NULL) { 24597 un->un_dcvb_timeid = 24598 timeout(sd_delayed_cv_broadcast, un, 24599 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24600 } 24601 } else { 24602 SD_TRACE(SD_LOG_COMMON, un, 24603 "sd_media_watch_cb: immediate cv_broadcast\n"); 24604 cv_broadcast(&un->un_state_cv); 24605 } 24606 } 24607 mutex_exit(SD_MUTEX(un)); 24608 return (0); 24609 } 24610 24611 24612 /* 24613 * Function: sd_dkio_get_temp 24614 * 24615 * Description: This routine is the driver entry point for handling ioctl 24616 * requests to get the disk temperature. 24617 * 24618 * Arguments: dev - the device number 24619 * arg - pointer to user provided dk_temperature structure. 24620 * flag - this argument is a pass through to ddi_copyxxx() 24621 * directly from the mode argument of ioctl(). 24622 * 24623 * Return Code: 0 24624 * EFAULT 24625 * ENXIO 24626 * EAGAIN 24627 */ 24628 24629 static int 24630 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24631 { 24632 struct sd_lun *un = NULL; 24633 struct dk_temperature *dktemp = NULL; 24634 uchar_t *temperature_page; 24635 int rval = 0; 24636 int path_flag = SD_PATH_STANDARD; 24637 24638 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24639 return (ENXIO); 24640 } 24641 24642 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24643 24644 /* copyin the disk temp argument to get the user flags */ 24645 if (ddi_copyin((void *)arg, dktemp, 24646 sizeof (struct dk_temperature), flag) != 0) { 24647 rval = EFAULT; 24648 goto done; 24649 } 24650 24651 /* Initialize the temperature to invalid. */ 24652 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24653 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24654 24655 /* 24656 * Note: Investigate removing the "bypass pm" semantic. 24657 * Can we just bypass PM always? 24658 */ 24659 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24660 path_flag = SD_PATH_DIRECT; 24661 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24662 mutex_enter(&un->un_pm_mutex); 24663 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24664 /* 24665 * If DKT_BYPASS_PM is set, and the drive happens to be 24666 * in low power mode, we can not wake it up, Need to 24667 * return EAGAIN. 24668 */ 24669 mutex_exit(&un->un_pm_mutex); 24670 rval = EAGAIN; 24671 goto done; 24672 } else { 24673 /* 24674 * Indicate to PM the device is busy. This is required 24675 * to avoid a race - i.e. the ioctl is issuing a 24676 * command and the pm framework brings down the device 24677 * to low power mode (possible power cut-off on some 24678 * platforms). 24679 */ 24680 mutex_exit(&un->un_pm_mutex); 24681 if (sd_pm_entry(un) != DDI_SUCCESS) { 24682 rval = EAGAIN; 24683 goto done; 24684 } 24685 } 24686 } 24687 24688 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24689 24690 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24691 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24692 goto done2; 24693 } 24694 24695 /* 24696 * For the current temperature verify that the parameter length is 0x02 24697 * and the parameter code is 0x00 24698 */ 24699 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24700 (temperature_page[5] == 0x00)) { 24701 if (temperature_page[9] == 0xFF) { 24702 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24703 } else { 24704 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24705 } 24706 } 24707 24708 /* 24709 * For the reference temperature verify that the parameter 24710 * length is 0x02 and the parameter code is 0x01 24711 */ 24712 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24713 (temperature_page[11] == 0x01)) { 24714 if (temperature_page[15] == 0xFF) { 24715 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24716 } else { 24717 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24718 } 24719 } 24720 24721 /* Do the copyout regardless of the temperature commands status. */ 24722 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24723 flag) != 0) { 24724 rval = EFAULT; 24725 } 24726 24727 done2: 24728 if (path_flag == SD_PATH_DIRECT) { 24729 sd_pm_exit(un); 24730 } 24731 24732 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24733 done: 24734 if (dktemp != NULL) { 24735 kmem_free(dktemp, sizeof (struct dk_temperature)); 24736 } 24737 24738 return (rval); 24739 } 24740 24741 24742 /* 24743 * Function: sd_log_page_supported 24744 * 24745 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24746 * supported log pages. 24747 * 24748 * Arguments: un - 24749 * log_page - 24750 * 24751 * Return Code: -1 - on error (log sense is optional and may not be supported). 24752 * 0 - log page not found. 24753 * 1 - log page found. 24754 */ 24755 24756 static int 24757 sd_log_page_supported(struct sd_lun *un, int log_page) 24758 { 24759 uchar_t *log_page_data; 24760 int i; 24761 int match = 0; 24762 int log_size; 24763 24764 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24765 24766 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24767 SD_PATH_DIRECT) != 0) { 24768 SD_ERROR(SD_LOG_COMMON, un, 24769 "sd_log_page_supported: failed log page retrieval\n"); 24770 kmem_free(log_page_data, 0xFF); 24771 return (-1); 24772 } 24773 log_size = log_page_data[3]; 24774 24775 /* 24776 * The list of supported log pages start from the fourth byte. Check 24777 * until we run out of log pages or a match is found. 24778 */ 24779 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24780 if (log_page_data[i] == log_page) { 24781 match++; 24782 } 24783 } 24784 kmem_free(log_page_data, 0xFF); 24785 return (match); 24786 } 24787 24788 24789 /* 24790 * Function: sd_mhdioc_failfast 24791 * 24792 * Description: This routine is the driver entry point for handling ioctl 24793 * requests to enable/disable the multihost failfast option. 24794 * (MHIOCENFAILFAST) 24795 * 24796 * Arguments: dev - the device number 24797 * arg - user specified probing interval. 24798 * flag - this argument is a pass through to ddi_copyxxx() 24799 * directly from the mode argument of ioctl(). 24800 * 24801 * Return Code: 0 24802 * EFAULT 24803 * ENXIO 24804 */ 24805 24806 static int 24807 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24808 { 24809 struct sd_lun *un = NULL; 24810 int mh_time; 24811 int rval = 0; 24812 24813 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24814 return (ENXIO); 24815 } 24816 24817 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24818 return (EFAULT); 24819 24820 if (mh_time) { 24821 mutex_enter(SD_MUTEX(un)); 24822 un->un_resvd_status |= SD_FAILFAST; 24823 mutex_exit(SD_MUTEX(un)); 24824 /* 24825 * If mh_time is INT_MAX, then this ioctl is being used for 24826 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24827 */ 24828 if (mh_time != INT_MAX) { 24829 rval = sd_check_mhd(dev, mh_time); 24830 } 24831 } else { 24832 (void) sd_check_mhd(dev, 0); 24833 mutex_enter(SD_MUTEX(un)); 24834 un->un_resvd_status &= ~SD_FAILFAST; 24835 mutex_exit(SD_MUTEX(un)); 24836 } 24837 return (rval); 24838 } 24839 24840 24841 /* 24842 * Function: sd_mhdioc_takeown 24843 * 24844 * Description: This routine is the driver entry point for handling ioctl 24845 * requests to forcefully acquire exclusive access rights to the 24846 * multihost disk (MHIOCTKOWN). 24847 * 24848 * Arguments: dev - the device number 24849 * arg - user provided structure specifying the delay 24850 * parameters in milliseconds 24851 * flag - this argument is a pass through to ddi_copyxxx() 24852 * directly from the mode argument of ioctl(). 24853 * 24854 * Return Code: 0 24855 * EFAULT 24856 * ENXIO 24857 */ 24858 24859 static int 24860 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24861 { 24862 struct sd_lun *un = NULL; 24863 struct mhioctkown *tkown = NULL; 24864 int rval = 0; 24865 24866 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24867 return (ENXIO); 24868 } 24869 24870 if (arg != NULL) { 24871 tkown = (struct mhioctkown *) 24872 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24873 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24874 if (rval != 0) { 24875 rval = EFAULT; 24876 goto error; 24877 } 24878 } 24879 24880 rval = sd_take_ownership(dev, tkown); 24881 mutex_enter(SD_MUTEX(un)); 24882 if (rval == 0) { 24883 un->un_resvd_status |= SD_RESERVE; 24884 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24885 sd_reinstate_resv_delay = 24886 tkown->reinstate_resv_delay * 1000; 24887 } else { 24888 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24889 } 24890 /* 24891 * Give the scsi_watch routine interval set by 24892 * the MHIOCENFAILFAST ioctl precedence here. 24893 */ 24894 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24895 mutex_exit(SD_MUTEX(un)); 24896 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24897 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24898 "sd_mhdioc_takeown : %d\n", 24899 sd_reinstate_resv_delay); 24900 } else { 24901 mutex_exit(SD_MUTEX(un)); 24902 } 24903 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24904 sd_mhd_reset_notify_cb, (caddr_t)un); 24905 } else { 24906 un->un_resvd_status &= ~SD_RESERVE; 24907 mutex_exit(SD_MUTEX(un)); 24908 } 24909 24910 error: 24911 if (tkown != NULL) { 24912 kmem_free(tkown, sizeof (struct mhioctkown)); 24913 } 24914 return (rval); 24915 } 24916 24917 24918 /* 24919 * Function: sd_mhdioc_release 24920 * 24921 * Description: This routine is the driver entry point for handling ioctl 24922 * requests to release exclusive access rights to the multihost 24923 * disk (MHIOCRELEASE). 24924 * 24925 * Arguments: dev - the device number 24926 * 24927 * Return Code: 0 24928 * ENXIO 24929 */ 24930 24931 static int 24932 sd_mhdioc_release(dev_t dev) 24933 { 24934 struct sd_lun *un = NULL; 24935 timeout_id_t resvd_timeid_save; 24936 int resvd_status_save; 24937 int rval = 0; 24938 24939 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24940 return (ENXIO); 24941 } 24942 24943 mutex_enter(SD_MUTEX(un)); 24944 resvd_status_save = un->un_resvd_status; 24945 un->un_resvd_status &= 24946 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24947 if (un->un_resvd_timeid) { 24948 resvd_timeid_save = un->un_resvd_timeid; 24949 un->un_resvd_timeid = NULL; 24950 mutex_exit(SD_MUTEX(un)); 24951 (void) untimeout(resvd_timeid_save); 24952 } else { 24953 mutex_exit(SD_MUTEX(un)); 24954 } 24955 24956 /* 24957 * destroy any pending timeout thread that may be attempting to 24958 * reinstate reservation on this device. 24959 */ 24960 sd_rmv_resv_reclaim_req(dev); 24961 24962 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24963 mutex_enter(SD_MUTEX(un)); 24964 if ((un->un_mhd_token) && 24965 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24966 mutex_exit(SD_MUTEX(un)); 24967 (void) sd_check_mhd(dev, 0); 24968 } else { 24969 mutex_exit(SD_MUTEX(un)); 24970 } 24971 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24972 sd_mhd_reset_notify_cb, (caddr_t)un); 24973 } else { 24974 /* 24975 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24976 */ 24977 mutex_enter(SD_MUTEX(un)); 24978 un->un_resvd_status = resvd_status_save; 24979 mutex_exit(SD_MUTEX(un)); 24980 } 24981 return (rval); 24982 } 24983 24984 24985 /* 24986 * Function: sd_mhdioc_register_devid 24987 * 24988 * Description: This routine is the driver entry point for handling ioctl 24989 * requests to register the device id (MHIOCREREGISTERDEVID). 24990 * 24991 * Note: The implementation for this ioctl has been updated to 24992 * be consistent with the original PSARC case (1999/357) 24993 * (4375899, 4241671, 4220005) 24994 * 24995 * Arguments: dev - the device number 24996 * 24997 * Return Code: 0 24998 * ENXIO 24999 */ 25000 25001 static int 25002 sd_mhdioc_register_devid(dev_t dev) 25003 { 25004 struct sd_lun *un = NULL; 25005 int rval = 0; 25006 25007 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25008 return (ENXIO); 25009 } 25010 25011 ASSERT(!mutex_owned(SD_MUTEX(un))); 25012 25013 mutex_enter(SD_MUTEX(un)); 25014 25015 /* If a devid already exists, de-register it */ 25016 if (un->un_devid != NULL) { 25017 ddi_devid_unregister(SD_DEVINFO(un)); 25018 /* 25019 * After unregister devid, needs to free devid memory 25020 */ 25021 ddi_devid_free(un->un_devid); 25022 un->un_devid = NULL; 25023 } 25024 25025 /* Check for reservation conflict */ 25026 mutex_exit(SD_MUTEX(un)); 25027 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 25028 mutex_enter(SD_MUTEX(un)); 25029 25030 switch (rval) { 25031 case 0: 25032 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25033 break; 25034 case EACCES: 25035 break; 25036 default: 25037 rval = EIO; 25038 } 25039 25040 mutex_exit(SD_MUTEX(un)); 25041 return (rval); 25042 } 25043 25044 25045 /* 25046 * Function: sd_mhdioc_inkeys 25047 * 25048 * Description: This routine is the driver entry point for handling ioctl 25049 * requests to issue the SCSI-3 Persistent In Read Keys command 25050 * to the device (MHIOCGRP_INKEYS). 25051 * 25052 * Arguments: dev - the device number 25053 * arg - user provided in_keys structure 25054 * flag - this argument is a pass through to ddi_copyxxx() 25055 * directly from the mode argument of ioctl(). 25056 * 25057 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25058 * ENXIO 25059 * EFAULT 25060 */ 25061 25062 static int 25063 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25064 { 25065 struct sd_lun *un; 25066 mhioc_inkeys_t inkeys; 25067 int rval = 0; 25068 25069 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25070 return (ENXIO); 25071 } 25072 25073 #ifdef _MULTI_DATAMODEL 25074 switch (ddi_model_convert_from(flag & FMODELS)) { 25075 case DDI_MODEL_ILP32: { 25076 struct mhioc_inkeys32 inkeys32; 25077 25078 if (ddi_copyin(arg, &inkeys32, 25079 sizeof (struct mhioc_inkeys32), flag) != 0) { 25080 return (EFAULT); 25081 } 25082 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25083 if ((rval = sd_persistent_reservation_in_read_keys(un, 25084 &inkeys, flag)) != 0) { 25085 return (rval); 25086 } 25087 inkeys32.generation = inkeys.generation; 25088 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25089 flag) != 0) { 25090 return (EFAULT); 25091 } 25092 break; 25093 } 25094 case DDI_MODEL_NONE: 25095 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25096 flag) != 0) { 25097 return (EFAULT); 25098 } 25099 if ((rval = sd_persistent_reservation_in_read_keys(un, 25100 &inkeys, flag)) != 0) { 25101 return (rval); 25102 } 25103 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25104 flag) != 0) { 25105 return (EFAULT); 25106 } 25107 break; 25108 } 25109 25110 #else /* ! _MULTI_DATAMODEL */ 25111 25112 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25113 return (EFAULT); 25114 } 25115 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25116 if (rval != 0) { 25117 return (rval); 25118 } 25119 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25120 return (EFAULT); 25121 } 25122 25123 #endif /* _MULTI_DATAMODEL */ 25124 25125 return (rval); 25126 } 25127 25128 25129 /* 25130 * Function: sd_mhdioc_inresv 25131 * 25132 * Description: This routine is the driver entry point for handling ioctl 25133 * requests to issue the SCSI-3 Persistent In Read Reservations 25134 * command to the device (MHIOCGRP_INKEYS). 25135 * 25136 * Arguments: dev - the device number 25137 * arg - user provided in_resv structure 25138 * flag - this argument is a pass through to ddi_copyxxx() 25139 * directly from the mode argument of ioctl(). 25140 * 25141 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25142 * ENXIO 25143 * EFAULT 25144 */ 25145 25146 static int 25147 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25148 { 25149 struct sd_lun *un; 25150 mhioc_inresvs_t inresvs; 25151 int rval = 0; 25152 25153 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25154 return (ENXIO); 25155 } 25156 25157 #ifdef _MULTI_DATAMODEL 25158 25159 switch (ddi_model_convert_from(flag & FMODELS)) { 25160 case DDI_MODEL_ILP32: { 25161 struct mhioc_inresvs32 inresvs32; 25162 25163 if (ddi_copyin(arg, &inresvs32, 25164 sizeof (struct mhioc_inresvs32), flag) != 0) { 25165 return (EFAULT); 25166 } 25167 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25168 if ((rval = sd_persistent_reservation_in_read_resv(un, 25169 &inresvs, flag)) != 0) { 25170 return (rval); 25171 } 25172 inresvs32.generation = inresvs.generation; 25173 if (ddi_copyout(&inresvs32, arg, 25174 sizeof (struct mhioc_inresvs32), flag) != 0) { 25175 return (EFAULT); 25176 } 25177 break; 25178 } 25179 case DDI_MODEL_NONE: 25180 if (ddi_copyin(arg, &inresvs, 25181 sizeof (mhioc_inresvs_t), flag) != 0) { 25182 return (EFAULT); 25183 } 25184 if ((rval = sd_persistent_reservation_in_read_resv(un, 25185 &inresvs, flag)) != 0) { 25186 return (rval); 25187 } 25188 if (ddi_copyout(&inresvs, arg, 25189 sizeof (mhioc_inresvs_t), flag) != 0) { 25190 return (EFAULT); 25191 } 25192 break; 25193 } 25194 25195 #else /* ! _MULTI_DATAMODEL */ 25196 25197 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25198 return (EFAULT); 25199 } 25200 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25201 if (rval != 0) { 25202 return (rval); 25203 } 25204 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25205 return (EFAULT); 25206 } 25207 25208 #endif /* ! _MULTI_DATAMODEL */ 25209 25210 return (rval); 25211 } 25212 25213 25214 /* 25215 * The following routines support the clustering functionality described below 25216 * and implement lost reservation reclaim functionality. 25217 * 25218 * Clustering 25219 * ---------- 25220 * The clustering code uses two different, independent forms of SCSI 25221 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25222 * Persistent Group Reservations. For any particular disk, it will use either 25223 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25224 * 25225 * SCSI-2 25226 * The cluster software takes ownership of a multi-hosted disk by issuing the 25227 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25228 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25229 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25230 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25231 * meaning of failfast is that if the driver (on this host) ever encounters the 25232 * scsi error return code RESERVATION_CONFLICT from the device, it should 25233 * immediately panic the host. The motivation for this ioctl is that if this 25234 * host does encounter reservation conflict, the underlying cause is that some 25235 * other host of the cluster has decided that this host is no longer in the 25236 * cluster and has seized control of the disks for itself. Since this host is no 25237 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25238 * does two things: 25239 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25240 * error to panic the host 25241 * (b) it sets up a periodic timer to test whether this host still has 25242 * "access" (in that no other host has reserved the device): if the 25243 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25244 * purpose of that periodic timer is to handle scenarios where the host is 25245 * otherwise temporarily quiescent, temporarily doing no real i/o. 25246 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25247 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25248 * the device itself. 25249 * 25250 * SCSI-3 PGR 25251 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25252 * facility is supported through the shared multihost disk ioctls 25253 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25254 * MHIOCGRP_PREEMPTANDABORT) 25255 * 25256 * Reservation Reclaim: 25257 * -------------------- 25258 * To support the lost reservation reclaim operations this driver creates a 25259 * single thread to handle reinstating reservations on all devices that have 25260 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25261 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25262 * and the reservation reclaim thread loops through the requests to regain the 25263 * lost reservations. 25264 */ 25265 25266 /* 25267 * Function: sd_check_mhd() 25268 * 25269 * Description: This function sets up and submits a scsi watch request or 25270 * terminates an existing watch request. This routine is used in 25271 * support of reservation reclaim. 25272 * 25273 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25274 * among multiple watches that share the callback function 25275 * interval - the number of microseconds specifying the watch 25276 * interval for issuing TEST UNIT READY commands. If 25277 * set to 0 the watch should be terminated. If the 25278 * interval is set to 0 and if the device is required 25279 * to hold reservation while disabling failfast, the 25280 * watch is restarted with an interval of 25281 * reinstate_resv_delay. 25282 * 25283 * Return Code: 0 - Successful submit/terminate of scsi watch request 25284 * ENXIO - Indicates an invalid device was specified 25285 * EAGAIN - Unable to submit the scsi watch request 25286 */ 25287 25288 static int 25289 sd_check_mhd(dev_t dev, int interval) 25290 { 25291 struct sd_lun *un; 25292 opaque_t token; 25293 25294 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25295 return (ENXIO); 25296 } 25297 25298 /* is this a watch termination request? */ 25299 if (interval == 0) { 25300 mutex_enter(SD_MUTEX(un)); 25301 /* if there is an existing watch task then terminate it */ 25302 if (un->un_mhd_token) { 25303 token = un->un_mhd_token; 25304 un->un_mhd_token = NULL; 25305 mutex_exit(SD_MUTEX(un)); 25306 (void) scsi_watch_request_terminate(token, 25307 SCSI_WATCH_TERMINATE_WAIT); 25308 mutex_enter(SD_MUTEX(un)); 25309 } else { 25310 mutex_exit(SD_MUTEX(un)); 25311 /* 25312 * Note: If we return here we don't check for the 25313 * failfast case. This is the original legacy 25314 * implementation but perhaps we should be checking 25315 * the failfast case. 25316 */ 25317 return (0); 25318 } 25319 /* 25320 * If the device is required to hold reservation while 25321 * disabling failfast, we need to restart the scsi_watch 25322 * routine with an interval of reinstate_resv_delay. 25323 */ 25324 if (un->un_resvd_status & SD_RESERVE) { 25325 interval = sd_reinstate_resv_delay/1000; 25326 } else { 25327 /* no failfast so bail */ 25328 mutex_exit(SD_MUTEX(un)); 25329 return (0); 25330 } 25331 mutex_exit(SD_MUTEX(un)); 25332 } 25333 25334 /* 25335 * adjust minimum time interval to 1 second, 25336 * and convert from msecs to usecs 25337 */ 25338 if (interval > 0 && interval < 1000) { 25339 interval = 1000; 25340 } 25341 interval *= 1000; 25342 25343 /* 25344 * submit the request to the scsi_watch service 25345 */ 25346 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25347 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25348 if (token == NULL) { 25349 return (EAGAIN); 25350 } 25351 25352 /* 25353 * save token for termination later on 25354 */ 25355 mutex_enter(SD_MUTEX(un)); 25356 un->un_mhd_token = token; 25357 mutex_exit(SD_MUTEX(un)); 25358 return (0); 25359 } 25360 25361 25362 /* 25363 * Function: sd_mhd_watch_cb() 25364 * 25365 * Description: This function is the call back function used by the scsi watch 25366 * facility. The scsi watch facility sends the "Test Unit Ready" 25367 * and processes the status. If applicable (i.e. a "Unit Attention" 25368 * status and automatic "Request Sense" not used) the scsi watch 25369 * facility will send a "Request Sense" and retrieve the sense data 25370 * to be passed to this callback function. In either case the 25371 * automatic "Request Sense" or the facility submitting one, this 25372 * callback is passed the status and sense data. 25373 * 25374 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25375 * among multiple watches that share this callback function 25376 * resultp - scsi watch facility result packet containing scsi 25377 * packet, status byte and sense data 25378 * 25379 * Return Code: 0 - continue the watch task 25380 * non-zero - terminate the watch task 25381 */ 25382 25383 static int 25384 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25385 { 25386 struct sd_lun *un; 25387 struct scsi_status *statusp; 25388 uint8_t *sensep; 25389 struct scsi_pkt *pkt; 25390 uchar_t actual_sense_length; 25391 dev_t dev = (dev_t)arg; 25392 25393 ASSERT(resultp != NULL); 25394 statusp = resultp->statusp; 25395 sensep = (uint8_t *)resultp->sensep; 25396 pkt = resultp->pkt; 25397 actual_sense_length = resultp->actual_sense_length; 25398 25399 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25400 return (ENXIO); 25401 } 25402 25403 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25404 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25405 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25406 25407 /* Begin processing of the status and/or sense data */ 25408 if (pkt->pkt_reason != CMD_CMPLT) { 25409 /* Handle the incomplete packet */ 25410 sd_mhd_watch_incomplete(un, pkt); 25411 return (0); 25412 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25413 if (*((unsigned char *)statusp) 25414 == STATUS_RESERVATION_CONFLICT) { 25415 /* 25416 * Handle a reservation conflict by panicking if 25417 * configured for failfast or by logging the conflict 25418 * and updating the reservation status 25419 */ 25420 mutex_enter(SD_MUTEX(un)); 25421 if ((un->un_resvd_status & SD_FAILFAST) && 25422 (sd_failfast_enable)) { 25423 sd_panic_for_res_conflict(un); 25424 /*NOTREACHED*/ 25425 } 25426 SD_INFO(SD_LOG_IOCTL_MHD, un, 25427 "sd_mhd_watch_cb: Reservation Conflict\n"); 25428 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25429 mutex_exit(SD_MUTEX(un)); 25430 } 25431 } 25432 25433 if (sensep != NULL) { 25434 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25435 mutex_enter(SD_MUTEX(un)); 25436 if ((scsi_sense_asc(sensep) == 25437 SD_SCSI_RESET_SENSE_CODE) && 25438 (un->un_resvd_status & SD_RESERVE)) { 25439 /* 25440 * The additional sense code indicates a power 25441 * on or bus device reset has occurred; update 25442 * the reservation status. 25443 */ 25444 un->un_resvd_status |= 25445 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25446 SD_INFO(SD_LOG_IOCTL_MHD, un, 25447 "sd_mhd_watch_cb: Lost Reservation\n"); 25448 } 25449 } else { 25450 return (0); 25451 } 25452 } else { 25453 mutex_enter(SD_MUTEX(un)); 25454 } 25455 25456 if ((un->un_resvd_status & SD_RESERVE) && 25457 (un->un_resvd_status & SD_LOST_RESERVE)) { 25458 if (un->un_resvd_status & SD_WANT_RESERVE) { 25459 /* 25460 * A reset occurred in between the last probe and this 25461 * one so if a timeout is pending cancel it. 25462 */ 25463 if (un->un_resvd_timeid) { 25464 timeout_id_t temp_id = un->un_resvd_timeid; 25465 un->un_resvd_timeid = NULL; 25466 mutex_exit(SD_MUTEX(un)); 25467 (void) untimeout(temp_id); 25468 mutex_enter(SD_MUTEX(un)); 25469 } 25470 un->un_resvd_status &= ~SD_WANT_RESERVE; 25471 } 25472 if (un->un_resvd_timeid == 0) { 25473 /* Schedule a timeout to handle the lost reservation */ 25474 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25475 (void *)dev, 25476 drv_usectohz(sd_reinstate_resv_delay)); 25477 } 25478 } 25479 mutex_exit(SD_MUTEX(un)); 25480 return (0); 25481 } 25482 25483 25484 /* 25485 * Function: sd_mhd_watch_incomplete() 25486 * 25487 * Description: This function is used to find out why a scsi pkt sent by the 25488 * scsi watch facility was not completed. Under some scenarios this 25489 * routine will return. Otherwise it will send a bus reset to see 25490 * if the drive is still online. 25491 * 25492 * Arguments: un - driver soft state (unit) structure 25493 * pkt - incomplete scsi pkt 25494 */ 25495 25496 static void 25497 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25498 { 25499 int be_chatty; 25500 int perr; 25501 25502 ASSERT(pkt != NULL); 25503 ASSERT(un != NULL); 25504 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25505 perr = (pkt->pkt_statistics & STAT_PERR); 25506 25507 mutex_enter(SD_MUTEX(un)); 25508 if (un->un_state == SD_STATE_DUMPING) { 25509 mutex_exit(SD_MUTEX(un)); 25510 return; 25511 } 25512 25513 switch (pkt->pkt_reason) { 25514 case CMD_UNX_BUS_FREE: 25515 /* 25516 * If we had a parity error that caused the target to drop BSY*, 25517 * don't be chatty about it. 25518 */ 25519 if (perr && be_chatty) { 25520 be_chatty = 0; 25521 } 25522 break; 25523 case CMD_TAG_REJECT: 25524 /* 25525 * The SCSI-2 spec states that a tag reject will be sent by the 25526 * target if tagged queuing is not supported. A tag reject may 25527 * also be sent during certain initialization periods or to 25528 * control internal resources. For the latter case the target 25529 * may also return Queue Full. 25530 * 25531 * If this driver receives a tag reject from a target that is 25532 * going through an init period or controlling internal 25533 * resources tagged queuing will be disabled. This is a less 25534 * than optimal behavior but the driver is unable to determine 25535 * the target state and assumes tagged queueing is not supported 25536 */ 25537 pkt->pkt_flags = 0; 25538 un->un_tagflags = 0; 25539 25540 if (un->un_f_opt_queueing == TRUE) { 25541 un->un_throttle = min(un->un_throttle, 3); 25542 } else { 25543 un->un_throttle = 1; 25544 } 25545 mutex_exit(SD_MUTEX(un)); 25546 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25547 mutex_enter(SD_MUTEX(un)); 25548 break; 25549 case CMD_INCOMPLETE: 25550 /* 25551 * The transport stopped with an abnormal state, fallthrough and 25552 * reset the target and/or bus unless selection did not complete 25553 * (indicated by STATE_GOT_BUS) in which case we don't want to 25554 * go through a target/bus reset 25555 */ 25556 if (pkt->pkt_state == STATE_GOT_BUS) { 25557 break; 25558 } 25559 /*FALLTHROUGH*/ 25560 25561 case CMD_TIMEOUT: 25562 default: 25563 /* 25564 * The lun may still be running the command, so a lun reset 25565 * should be attempted. If the lun reset fails or cannot be 25566 * issued, than try a target reset. Lastly try a bus reset. 25567 */ 25568 if ((pkt->pkt_statistics & 25569 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25570 int reset_retval = 0; 25571 mutex_exit(SD_MUTEX(un)); 25572 if (un->un_f_allow_bus_device_reset == TRUE) { 25573 if (un->un_f_lun_reset_enabled == TRUE) { 25574 reset_retval = 25575 scsi_reset(SD_ADDRESS(un), 25576 RESET_LUN); 25577 } 25578 if (reset_retval == 0) { 25579 reset_retval = 25580 scsi_reset(SD_ADDRESS(un), 25581 RESET_TARGET); 25582 } 25583 } 25584 if (reset_retval == 0) { 25585 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25586 } 25587 mutex_enter(SD_MUTEX(un)); 25588 } 25589 break; 25590 } 25591 25592 /* A device/bus reset has occurred; update the reservation status. */ 25593 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25594 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25595 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25596 un->un_resvd_status |= 25597 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25598 SD_INFO(SD_LOG_IOCTL_MHD, un, 25599 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25600 } 25601 } 25602 25603 /* 25604 * The disk has been turned off; Update the device state. 25605 * 25606 * Note: Should we be offlining the disk here? 25607 */ 25608 if (pkt->pkt_state == STATE_GOT_BUS) { 25609 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25610 "Disk not responding to selection\n"); 25611 if (un->un_state != SD_STATE_OFFLINE) { 25612 New_state(un, SD_STATE_OFFLINE); 25613 } 25614 } else if (be_chatty) { 25615 /* 25616 * suppress messages if they are all the same pkt reason; 25617 * with TQ, many (up to 256) are returned with the same 25618 * pkt_reason 25619 */ 25620 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25621 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25622 "sd_mhd_watch_incomplete: " 25623 "SCSI transport failed: reason '%s'\n", 25624 scsi_rname(pkt->pkt_reason)); 25625 } 25626 } 25627 un->un_last_pkt_reason = pkt->pkt_reason; 25628 mutex_exit(SD_MUTEX(un)); 25629 } 25630 25631 25632 /* 25633 * Function: sd_sname() 25634 * 25635 * Description: This is a simple little routine to return a string containing 25636 * a printable description of command status byte for use in 25637 * logging. 25638 * 25639 * Arguments: status - pointer to a status byte 25640 * 25641 * Return Code: char * - string containing status description. 25642 */ 25643 25644 static char * 25645 sd_sname(uchar_t status) 25646 { 25647 switch (status & STATUS_MASK) { 25648 case STATUS_GOOD: 25649 return ("good status"); 25650 case STATUS_CHECK: 25651 return ("check condition"); 25652 case STATUS_MET: 25653 return ("condition met"); 25654 case STATUS_BUSY: 25655 return ("busy"); 25656 case STATUS_INTERMEDIATE: 25657 return ("intermediate"); 25658 case STATUS_INTERMEDIATE_MET: 25659 return ("intermediate - condition met"); 25660 case STATUS_RESERVATION_CONFLICT: 25661 return ("reservation_conflict"); 25662 case STATUS_TERMINATED: 25663 return ("command terminated"); 25664 case STATUS_QFULL: 25665 return ("queue full"); 25666 default: 25667 return ("<unknown status>"); 25668 } 25669 } 25670 25671 25672 /* 25673 * Function: sd_mhd_resvd_recover() 25674 * 25675 * Description: This function adds a reservation entry to the 25676 * sd_resv_reclaim_request list and signals the reservation 25677 * reclaim thread that there is work pending. If the reservation 25678 * reclaim thread has not been previously created this function 25679 * will kick it off. 25680 * 25681 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25682 * among multiple watches that share this callback function 25683 * 25684 * Context: This routine is called by timeout() and is run in interrupt 25685 * context. It must not sleep or call other functions which may 25686 * sleep. 25687 */ 25688 25689 static void 25690 sd_mhd_resvd_recover(void *arg) 25691 { 25692 dev_t dev = (dev_t)arg; 25693 struct sd_lun *un; 25694 struct sd_thr_request *sd_treq = NULL; 25695 struct sd_thr_request *sd_cur = NULL; 25696 struct sd_thr_request *sd_prev = NULL; 25697 int already_there = 0; 25698 25699 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25700 return; 25701 } 25702 25703 mutex_enter(SD_MUTEX(un)); 25704 un->un_resvd_timeid = NULL; 25705 if (un->un_resvd_status & SD_WANT_RESERVE) { 25706 /* 25707 * There was a reset so don't issue the reserve, allow the 25708 * sd_mhd_watch_cb callback function to notice this and 25709 * reschedule the timeout for reservation. 25710 */ 25711 mutex_exit(SD_MUTEX(un)); 25712 return; 25713 } 25714 mutex_exit(SD_MUTEX(un)); 25715 25716 /* 25717 * Add this device to the sd_resv_reclaim_request list and the 25718 * sd_resv_reclaim_thread should take care of the rest. 25719 * 25720 * Note: We can't sleep in this context so if the memory allocation 25721 * fails allow the sd_mhd_watch_cb callback function to notice this and 25722 * reschedule the timeout for reservation. (4378460) 25723 */ 25724 sd_treq = (struct sd_thr_request *) 25725 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25726 if (sd_treq == NULL) { 25727 return; 25728 } 25729 25730 sd_treq->sd_thr_req_next = NULL; 25731 sd_treq->dev = dev; 25732 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25733 if (sd_tr.srq_thr_req_head == NULL) { 25734 sd_tr.srq_thr_req_head = sd_treq; 25735 } else { 25736 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25737 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25738 if (sd_cur->dev == dev) { 25739 /* 25740 * already in Queue so don't log 25741 * another request for the device 25742 */ 25743 already_there = 1; 25744 break; 25745 } 25746 sd_prev = sd_cur; 25747 } 25748 if (!already_there) { 25749 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25750 "logging request for %lx\n", dev); 25751 sd_prev->sd_thr_req_next = sd_treq; 25752 } else { 25753 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25754 } 25755 } 25756 25757 /* 25758 * Create a kernel thread to do the reservation reclaim and free up this 25759 * thread. We cannot block this thread while we go away to do the 25760 * reservation reclaim 25761 */ 25762 if (sd_tr.srq_resv_reclaim_thread == NULL) 25763 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25764 sd_resv_reclaim_thread, NULL, 25765 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25766 25767 /* Tell the reservation reclaim thread that it has work to do */ 25768 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25769 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25770 } 25771 25772 /* 25773 * Function: sd_resv_reclaim_thread() 25774 * 25775 * Description: This function implements the reservation reclaim operations 25776 * 25777 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25778 * among multiple watches that share this callback function 25779 */ 25780 25781 static void 25782 sd_resv_reclaim_thread() 25783 { 25784 struct sd_lun *un; 25785 struct sd_thr_request *sd_mhreq; 25786 25787 /* Wait for work */ 25788 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25789 if (sd_tr.srq_thr_req_head == NULL) { 25790 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25791 &sd_tr.srq_resv_reclaim_mutex); 25792 } 25793 25794 /* Loop while we have work */ 25795 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25796 un = ddi_get_soft_state(sd_state, 25797 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25798 if (un == NULL) { 25799 /* 25800 * softstate structure is NULL so just 25801 * dequeue the request and continue 25802 */ 25803 sd_tr.srq_thr_req_head = 25804 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25805 kmem_free(sd_tr.srq_thr_cur_req, 25806 sizeof (struct sd_thr_request)); 25807 continue; 25808 } 25809 25810 /* dequeue the request */ 25811 sd_mhreq = sd_tr.srq_thr_cur_req; 25812 sd_tr.srq_thr_req_head = 25813 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25814 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25815 25816 /* 25817 * Reclaim reservation only if SD_RESERVE is still set. There 25818 * may have been a call to MHIOCRELEASE before we got here. 25819 */ 25820 mutex_enter(SD_MUTEX(un)); 25821 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25822 /* 25823 * Note: The SD_LOST_RESERVE flag is cleared before 25824 * reclaiming the reservation. If this is done after the 25825 * call to sd_reserve_release a reservation loss in the 25826 * window between pkt completion of reserve cmd and 25827 * mutex_enter below may not be recognized 25828 */ 25829 un->un_resvd_status &= ~SD_LOST_RESERVE; 25830 mutex_exit(SD_MUTEX(un)); 25831 25832 if (sd_reserve_release(sd_mhreq->dev, 25833 SD_RESERVE) == 0) { 25834 mutex_enter(SD_MUTEX(un)); 25835 un->un_resvd_status |= SD_RESERVE; 25836 mutex_exit(SD_MUTEX(un)); 25837 SD_INFO(SD_LOG_IOCTL_MHD, un, 25838 "sd_resv_reclaim_thread: " 25839 "Reservation Recovered\n"); 25840 } else { 25841 mutex_enter(SD_MUTEX(un)); 25842 un->un_resvd_status |= SD_LOST_RESERVE; 25843 mutex_exit(SD_MUTEX(un)); 25844 SD_INFO(SD_LOG_IOCTL_MHD, un, 25845 "sd_resv_reclaim_thread: Failed " 25846 "Reservation Recovery\n"); 25847 } 25848 } else { 25849 mutex_exit(SD_MUTEX(un)); 25850 } 25851 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25852 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25853 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25854 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25855 /* 25856 * wakeup the destroy thread if anyone is waiting on 25857 * us to complete. 25858 */ 25859 cv_signal(&sd_tr.srq_inprocess_cv); 25860 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25861 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25862 } 25863 25864 /* 25865 * cleanup the sd_tr structure now that this thread will not exist 25866 */ 25867 ASSERT(sd_tr.srq_thr_req_head == NULL); 25868 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25869 sd_tr.srq_resv_reclaim_thread = NULL; 25870 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25871 thread_exit(); 25872 } 25873 25874 25875 /* 25876 * Function: sd_rmv_resv_reclaim_req() 25877 * 25878 * Description: This function removes any pending reservation reclaim requests 25879 * for the specified device. 25880 * 25881 * Arguments: dev - the device 'dev_t' 25882 */ 25883 25884 static void 25885 sd_rmv_resv_reclaim_req(dev_t dev) 25886 { 25887 struct sd_thr_request *sd_mhreq; 25888 struct sd_thr_request *sd_prev; 25889 25890 /* Remove a reservation reclaim request from the list */ 25891 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25892 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25893 /* 25894 * We are attempting to reinstate reservation for 25895 * this device. We wait for sd_reserve_release() 25896 * to return before we return. 25897 */ 25898 cv_wait(&sd_tr.srq_inprocess_cv, 25899 &sd_tr.srq_resv_reclaim_mutex); 25900 } else { 25901 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25902 if (sd_mhreq && sd_mhreq->dev == dev) { 25903 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25904 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25905 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25906 return; 25907 } 25908 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25909 if (sd_mhreq && sd_mhreq->dev == dev) { 25910 break; 25911 } 25912 sd_prev = sd_mhreq; 25913 } 25914 if (sd_mhreq != NULL) { 25915 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25916 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25917 } 25918 } 25919 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25920 } 25921 25922 25923 /* 25924 * Function: sd_mhd_reset_notify_cb() 25925 * 25926 * Description: This is a call back function for scsi_reset_notify. This 25927 * function updates the softstate reserved status and logs the 25928 * reset. The driver scsi watch facility callback function 25929 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25930 * will reclaim the reservation. 25931 * 25932 * Arguments: arg - driver soft state (unit) structure 25933 */ 25934 25935 static void 25936 sd_mhd_reset_notify_cb(caddr_t arg) 25937 { 25938 struct sd_lun *un = (struct sd_lun *)arg; 25939 25940 mutex_enter(SD_MUTEX(un)); 25941 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25942 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25943 SD_INFO(SD_LOG_IOCTL_MHD, un, 25944 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25945 } 25946 mutex_exit(SD_MUTEX(un)); 25947 } 25948 25949 25950 /* 25951 * Function: sd_take_ownership() 25952 * 25953 * Description: This routine implements an algorithm to achieve a stable 25954 * reservation on disks which don't implement priority reserve, 25955 * and makes sure that other host lose re-reservation attempts. 25956 * This algorithm contains of a loop that keeps issuing the RESERVE 25957 * for some period of time (min_ownership_delay, default 6 seconds) 25958 * During that loop, it looks to see if there has been a bus device 25959 * reset or bus reset (both of which cause an existing reservation 25960 * to be lost). If the reservation is lost issue RESERVE until a 25961 * period of min_ownership_delay with no resets has gone by, or 25962 * until max_ownership_delay has expired. This loop ensures that 25963 * the host really did manage to reserve the device, in spite of 25964 * resets. The looping for min_ownership_delay (default six 25965 * seconds) is important to early generation clustering products, 25966 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25967 * MHIOCENFAILFAST periodic timer of two seconds. By having 25968 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25969 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25970 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25971 * have already noticed, via the MHIOCENFAILFAST polling, that it 25972 * no longer "owns" the disk and will have panicked itself. Thus, 25973 * the host issuing the MHIOCTKOWN is assured (with timing 25974 * dependencies) that by the time it actually starts to use the 25975 * disk for real work, the old owner is no longer accessing it. 25976 * 25977 * min_ownership_delay is the minimum amount of time for which the 25978 * disk must be reserved continuously devoid of resets before the 25979 * MHIOCTKOWN ioctl will return success. 25980 * 25981 * max_ownership_delay indicates the amount of time by which the 25982 * take ownership should succeed or timeout with an error. 25983 * 25984 * Arguments: dev - the device 'dev_t' 25985 * *p - struct containing timing info. 25986 * 25987 * Return Code: 0 for success or error code 25988 */ 25989 25990 static int 25991 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25992 { 25993 struct sd_lun *un; 25994 int rval; 25995 int err; 25996 int reservation_count = 0; 25997 int min_ownership_delay = 6000000; /* in usec */ 25998 int max_ownership_delay = 30000000; /* in usec */ 25999 clock_t start_time; /* starting time of this algorithm */ 26000 clock_t end_time; /* time limit for giving up */ 26001 clock_t ownership_time; /* time limit for stable ownership */ 26002 clock_t current_time; 26003 clock_t previous_current_time; 26004 26005 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26006 return (ENXIO); 26007 } 26008 26009 /* 26010 * Attempt a device reservation. A priority reservation is requested. 26011 */ 26012 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 26013 != SD_SUCCESS) { 26014 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26015 "sd_take_ownership: return(1)=%d\n", rval); 26016 return (rval); 26017 } 26018 26019 /* Update the softstate reserved status to indicate the reservation */ 26020 mutex_enter(SD_MUTEX(un)); 26021 un->un_resvd_status |= SD_RESERVE; 26022 un->un_resvd_status &= 26023 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 26024 mutex_exit(SD_MUTEX(un)); 26025 26026 if (p != NULL) { 26027 if (p->min_ownership_delay != 0) { 26028 min_ownership_delay = p->min_ownership_delay * 1000; 26029 } 26030 if (p->max_ownership_delay != 0) { 26031 max_ownership_delay = p->max_ownership_delay * 1000; 26032 } 26033 } 26034 SD_INFO(SD_LOG_IOCTL_MHD, un, 26035 "sd_take_ownership: min, max delays: %d, %d\n", 26036 min_ownership_delay, max_ownership_delay); 26037 26038 start_time = ddi_get_lbolt(); 26039 current_time = start_time; 26040 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26041 end_time = start_time + drv_usectohz(max_ownership_delay); 26042 26043 while (current_time - end_time < 0) { 26044 delay(drv_usectohz(500000)); 26045 26046 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26047 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26048 mutex_enter(SD_MUTEX(un)); 26049 rval = (un->un_resvd_status & 26050 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26051 mutex_exit(SD_MUTEX(un)); 26052 break; 26053 } 26054 } 26055 previous_current_time = current_time; 26056 current_time = ddi_get_lbolt(); 26057 mutex_enter(SD_MUTEX(un)); 26058 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26059 ownership_time = ddi_get_lbolt() + 26060 drv_usectohz(min_ownership_delay); 26061 reservation_count = 0; 26062 } else { 26063 reservation_count++; 26064 } 26065 un->un_resvd_status |= SD_RESERVE; 26066 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26067 mutex_exit(SD_MUTEX(un)); 26068 26069 SD_INFO(SD_LOG_IOCTL_MHD, un, 26070 "sd_take_ownership: ticks for loop iteration=%ld, " 26071 "reservation=%s\n", (current_time - previous_current_time), 26072 reservation_count ? "ok" : "reclaimed"); 26073 26074 if (current_time - ownership_time >= 0 && 26075 reservation_count >= 4) { 26076 rval = 0; /* Achieved a stable ownership */ 26077 break; 26078 } 26079 if (current_time - end_time >= 0) { 26080 rval = EACCES; /* No ownership in max possible time */ 26081 break; 26082 } 26083 } 26084 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26085 "sd_take_ownership: return(2)=%d\n", rval); 26086 return (rval); 26087 } 26088 26089 26090 /* 26091 * Function: sd_reserve_release() 26092 * 26093 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26094 * PRIORITY RESERVE commands based on a user specified command type 26095 * 26096 * Arguments: dev - the device 'dev_t' 26097 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26098 * SD_RESERVE, SD_RELEASE 26099 * 26100 * Return Code: 0 or Error Code 26101 */ 26102 26103 static int 26104 sd_reserve_release(dev_t dev, int cmd) 26105 { 26106 struct uscsi_cmd *com = NULL; 26107 struct sd_lun *un = NULL; 26108 char cdb[CDB_GROUP0]; 26109 int rval; 26110 26111 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26112 (cmd == SD_PRIORITY_RESERVE)); 26113 26114 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26115 return (ENXIO); 26116 } 26117 26118 /* instantiate and initialize the command and cdb */ 26119 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26120 bzero(cdb, CDB_GROUP0); 26121 com->uscsi_flags = USCSI_SILENT; 26122 com->uscsi_timeout = un->un_reserve_release_time; 26123 com->uscsi_cdblen = CDB_GROUP0; 26124 com->uscsi_cdb = cdb; 26125 if (cmd == SD_RELEASE) { 26126 cdb[0] = SCMD_RELEASE; 26127 } else { 26128 cdb[0] = SCMD_RESERVE; 26129 } 26130 26131 /* Send the command. */ 26132 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26133 SD_PATH_STANDARD); 26134 26135 /* 26136 * "break" a reservation that is held by another host, by issuing a 26137 * reset if priority reserve is desired, and we could not get the 26138 * device. 26139 */ 26140 if ((cmd == SD_PRIORITY_RESERVE) && 26141 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26142 /* 26143 * First try to reset the LUN. If we cannot, then try a target 26144 * reset, followed by a bus reset if the target reset fails. 26145 */ 26146 int reset_retval = 0; 26147 if (un->un_f_lun_reset_enabled == TRUE) { 26148 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26149 } 26150 if (reset_retval == 0) { 26151 /* The LUN reset either failed or was not issued */ 26152 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26153 } 26154 if ((reset_retval == 0) && 26155 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26156 rval = EIO; 26157 kmem_free(com, sizeof (*com)); 26158 return (rval); 26159 } 26160 26161 bzero(com, sizeof (struct uscsi_cmd)); 26162 com->uscsi_flags = USCSI_SILENT; 26163 com->uscsi_cdb = cdb; 26164 com->uscsi_cdblen = CDB_GROUP0; 26165 com->uscsi_timeout = 5; 26166 26167 /* 26168 * Reissue the last reserve command, this time without request 26169 * sense. Assume that it is just a regular reserve command. 26170 */ 26171 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26172 SD_PATH_STANDARD); 26173 } 26174 26175 /* Return an error if still getting a reservation conflict. */ 26176 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26177 rval = EACCES; 26178 } 26179 26180 kmem_free(com, sizeof (*com)); 26181 return (rval); 26182 } 26183 26184 26185 #define SD_NDUMP_RETRIES 12 26186 /* 26187 * System Crash Dump routine 26188 */ 26189 26190 static int 26191 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26192 { 26193 int instance; 26194 int partition; 26195 int i; 26196 int err; 26197 struct sd_lun *un; 26198 struct dk_map *lp; 26199 struct scsi_pkt *wr_pktp; 26200 struct buf *wr_bp; 26201 struct buf wr_buf; 26202 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26203 daddr_t tgt_blkno; /* rmw - blkno for target */ 26204 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26205 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26206 size_t io_start_offset; 26207 int doing_rmw = FALSE; 26208 int rval; 26209 #if defined(__i386) || defined(__amd64) 26210 ssize_t dma_resid; 26211 daddr_t oblkno; 26212 #endif 26213 26214 instance = SDUNIT(dev); 26215 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26216 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26217 return (ENXIO); 26218 } 26219 26220 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26221 26222 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26223 26224 partition = SDPART(dev); 26225 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26226 26227 /* Validate blocks to dump at against partition size. */ 26228 lp = &un->un_map[partition]; 26229 if ((blkno + nblk) > lp->dkl_nblk) { 26230 SD_TRACE(SD_LOG_DUMP, un, 26231 "sddump: dump range larger than partition: " 26232 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26233 blkno, nblk, lp->dkl_nblk); 26234 return (EINVAL); 26235 } 26236 26237 mutex_enter(&un->un_pm_mutex); 26238 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26239 struct scsi_pkt *start_pktp; 26240 26241 mutex_exit(&un->un_pm_mutex); 26242 26243 /* 26244 * use pm framework to power on HBA 1st 26245 */ 26246 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26247 26248 /* 26249 * Dump no long uses sdpower to power on a device, it's 26250 * in-line here so it can be done in polled mode. 26251 */ 26252 26253 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26254 26255 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26256 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26257 26258 if (start_pktp == NULL) { 26259 /* We were not given a SCSI packet, fail. */ 26260 return (EIO); 26261 } 26262 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26263 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26264 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26265 start_pktp->pkt_flags = FLAG_NOINTR; 26266 26267 mutex_enter(SD_MUTEX(un)); 26268 SD_FILL_SCSI1_LUN(un, start_pktp); 26269 mutex_exit(SD_MUTEX(un)); 26270 /* 26271 * Scsi_poll returns 0 (success) if the command completes and 26272 * the status block is STATUS_GOOD. 26273 */ 26274 if (sd_scsi_poll(un, start_pktp) != 0) { 26275 scsi_destroy_pkt(start_pktp); 26276 return (EIO); 26277 } 26278 scsi_destroy_pkt(start_pktp); 26279 (void) sd_ddi_pm_resume(un); 26280 } else { 26281 mutex_exit(&un->un_pm_mutex); 26282 } 26283 26284 mutex_enter(SD_MUTEX(un)); 26285 un->un_throttle = 0; 26286 26287 /* 26288 * The first time through, reset the specific target device. 26289 * However, when cpr calls sddump we know that sd is in a 26290 * a good state so no bus reset is required. 26291 * Clear sense data via Request Sense cmd. 26292 * In sddump we don't care about allow_bus_device_reset anymore 26293 */ 26294 26295 if ((un->un_state != SD_STATE_SUSPENDED) && 26296 (un->un_state != SD_STATE_DUMPING)) { 26297 26298 New_state(un, SD_STATE_DUMPING); 26299 26300 if (un->un_f_is_fibre == FALSE) { 26301 mutex_exit(SD_MUTEX(un)); 26302 /* 26303 * Attempt a bus reset for parallel scsi. 26304 * 26305 * Note: A bus reset is required because on some host 26306 * systems (i.e. E420R) a bus device reset is 26307 * insufficient to reset the state of the target. 26308 * 26309 * Note: Don't issue the reset for fibre-channel, 26310 * because this tends to hang the bus (loop) for 26311 * too long while everyone is logging out and in 26312 * and the deadman timer for dumping will fire 26313 * before the dump is complete. 26314 */ 26315 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26316 mutex_enter(SD_MUTEX(un)); 26317 Restore_state(un); 26318 mutex_exit(SD_MUTEX(un)); 26319 return (EIO); 26320 } 26321 26322 /* Delay to give the device some recovery time. */ 26323 drv_usecwait(10000); 26324 26325 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26326 SD_INFO(SD_LOG_DUMP, un, 26327 "sddump: sd_send_polled_RQS failed\n"); 26328 } 26329 mutex_enter(SD_MUTEX(un)); 26330 } 26331 } 26332 26333 /* 26334 * Convert the partition-relative block number to a 26335 * disk physical block number. 26336 */ 26337 blkno += un->un_offset[partition]; 26338 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26339 26340 26341 /* 26342 * Check if the device has a non-512 block size. 26343 */ 26344 wr_bp = NULL; 26345 if (NOT_DEVBSIZE(un)) { 26346 tgt_byte_offset = blkno * un->un_sys_blocksize; 26347 tgt_byte_count = nblk * un->un_sys_blocksize; 26348 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26349 (tgt_byte_count % un->un_tgt_blocksize)) { 26350 doing_rmw = TRUE; 26351 /* 26352 * Calculate the block number and number of block 26353 * in terms of the media block size. 26354 */ 26355 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26356 tgt_nblk = 26357 ((tgt_byte_offset + tgt_byte_count + 26358 (un->un_tgt_blocksize - 1)) / 26359 un->un_tgt_blocksize) - tgt_blkno; 26360 26361 /* 26362 * Invoke the routine which is going to do read part 26363 * of read-modify-write. 26364 * Note that this routine returns a pointer to 26365 * a valid bp in wr_bp. 26366 */ 26367 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26368 &wr_bp); 26369 if (err) { 26370 mutex_exit(SD_MUTEX(un)); 26371 return (err); 26372 } 26373 /* 26374 * Offset is being calculated as - 26375 * (original block # * system block size) - 26376 * (new block # * target block size) 26377 */ 26378 io_start_offset = 26379 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26380 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26381 26382 ASSERT((io_start_offset >= 0) && 26383 (io_start_offset < un->un_tgt_blocksize)); 26384 /* 26385 * Do the modify portion of read modify write. 26386 */ 26387 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26388 (size_t)nblk * un->un_sys_blocksize); 26389 } else { 26390 doing_rmw = FALSE; 26391 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26392 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26393 } 26394 26395 /* Convert blkno and nblk to target blocks */ 26396 blkno = tgt_blkno; 26397 nblk = tgt_nblk; 26398 } else { 26399 wr_bp = &wr_buf; 26400 bzero(wr_bp, sizeof (struct buf)); 26401 wr_bp->b_flags = B_BUSY; 26402 wr_bp->b_un.b_addr = addr; 26403 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26404 wr_bp->b_resid = 0; 26405 } 26406 26407 mutex_exit(SD_MUTEX(un)); 26408 26409 /* 26410 * Obtain a SCSI packet for the write command. 26411 * It should be safe to call the allocator here without 26412 * worrying about being locked for DVMA mapping because 26413 * the address we're passed is already a DVMA mapping 26414 * 26415 * We are also not going to worry about semaphore ownership 26416 * in the dump buffer. Dumping is single threaded at present. 26417 */ 26418 26419 wr_pktp = NULL; 26420 26421 #if defined(__i386) || defined(__amd64) 26422 dma_resid = wr_bp->b_bcount; 26423 oblkno = blkno; 26424 while (dma_resid != 0) { 26425 #endif 26426 26427 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26428 wr_bp->b_flags &= ~B_ERROR; 26429 26430 #if defined(__i386) || defined(__amd64) 26431 blkno = oblkno + 26432 ((wr_bp->b_bcount - dma_resid) / 26433 un->un_tgt_blocksize); 26434 nblk = dma_resid / un->un_tgt_blocksize; 26435 26436 if (wr_pktp) { 26437 /* Partial DMA transfers after initial transfer */ 26438 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26439 blkno, nblk); 26440 } else { 26441 /* Initial transfer */ 26442 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26443 un->un_pkt_flags, NULL_FUNC, NULL, 26444 blkno, nblk); 26445 } 26446 #else 26447 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26448 0, NULL_FUNC, NULL, blkno, nblk); 26449 #endif 26450 26451 if (rval == 0) { 26452 /* We were given a SCSI packet, continue. */ 26453 break; 26454 } 26455 26456 if (i == 0) { 26457 if (wr_bp->b_flags & B_ERROR) { 26458 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26459 "no resources for dumping; " 26460 "error code: 0x%x, retrying", 26461 geterror(wr_bp)); 26462 } else { 26463 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26464 "no resources for dumping; retrying"); 26465 } 26466 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26467 if (wr_bp->b_flags & B_ERROR) { 26468 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26469 "no resources for dumping; error code: " 26470 "0x%x, retrying\n", geterror(wr_bp)); 26471 } 26472 } else { 26473 if (wr_bp->b_flags & B_ERROR) { 26474 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26475 "no resources for dumping; " 26476 "error code: 0x%x, retries failed, " 26477 "giving up.\n", geterror(wr_bp)); 26478 } else { 26479 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26480 "no resources for dumping; " 26481 "retries failed, giving up.\n"); 26482 } 26483 mutex_enter(SD_MUTEX(un)); 26484 Restore_state(un); 26485 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26486 mutex_exit(SD_MUTEX(un)); 26487 scsi_free_consistent_buf(wr_bp); 26488 } else { 26489 mutex_exit(SD_MUTEX(un)); 26490 } 26491 return (EIO); 26492 } 26493 drv_usecwait(10000); 26494 } 26495 26496 #if defined(__i386) || defined(__amd64) 26497 /* 26498 * save the resid from PARTIAL_DMA 26499 */ 26500 dma_resid = wr_pktp->pkt_resid; 26501 if (dma_resid != 0) 26502 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26503 wr_pktp->pkt_resid = 0; 26504 #endif 26505 26506 /* SunBug 1222170 */ 26507 wr_pktp->pkt_flags = FLAG_NOINTR; 26508 26509 err = EIO; 26510 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26511 26512 /* 26513 * Scsi_poll returns 0 (success) if the command completes and 26514 * the status block is STATUS_GOOD. We should only check 26515 * errors if this condition is not true. Even then we should 26516 * send our own request sense packet only if we have a check 26517 * condition and auto request sense has not been performed by 26518 * the hba. 26519 */ 26520 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26521 26522 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26523 (wr_pktp->pkt_resid == 0)) { 26524 err = SD_SUCCESS; 26525 break; 26526 } 26527 26528 /* 26529 * Check CMD_DEV_GONE 1st, give up if device is gone. 26530 */ 26531 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26532 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26533 "Device is gone\n"); 26534 break; 26535 } 26536 26537 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26538 SD_INFO(SD_LOG_DUMP, un, 26539 "sddump: write failed with CHECK, try # %d\n", i); 26540 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26541 (void) sd_send_polled_RQS(un); 26542 } 26543 26544 continue; 26545 } 26546 26547 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26548 int reset_retval = 0; 26549 26550 SD_INFO(SD_LOG_DUMP, un, 26551 "sddump: write failed with BUSY, try # %d\n", i); 26552 26553 if (un->un_f_lun_reset_enabled == TRUE) { 26554 reset_retval = scsi_reset(SD_ADDRESS(un), 26555 RESET_LUN); 26556 } 26557 if (reset_retval == 0) { 26558 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26559 } 26560 (void) sd_send_polled_RQS(un); 26561 26562 } else { 26563 SD_INFO(SD_LOG_DUMP, un, 26564 "sddump: write failed with 0x%x, try # %d\n", 26565 SD_GET_PKT_STATUS(wr_pktp), i); 26566 mutex_enter(SD_MUTEX(un)); 26567 sd_reset_target(un, wr_pktp); 26568 mutex_exit(SD_MUTEX(un)); 26569 } 26570 26571 /* 26572 * If we are not getting anywhere with lun/target resets, 26573 * let's reset the bus. 26574 */ 26575 if (i == SD_NDUMP_RETRIES/2) { 26576 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26577 (void) sd_send_polled_RQS(un); 26578 } 26579 26580 } 26581 #if defined(__i386) || defined(__amd64) 26582 } /* dma_resid */ 26583 #endif 26584 26585 scsi_destroy_pkt(wr_pktp); 26586 mutex_enter(SD_MUTEX(un)); 26587 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26588 mutex_exit(SD_MUTEX(un)); 26589 scsi_free_consistent_buf(wr_bp); 26590 } else { 26591 mutex_exit(SD_MUTEX(un)); 26592 } 26593 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26594 return (err); 26595 } 26596 26597 /* 26598 * Function: sd_scsi_poll() 26599 * 26600 * Description: This is a wrapper for the scsi_poll call. 26601 * 26602 * Arguments: sd_lun - The unit structure 26603 * scsi_pkt - The scsi packet being sent to the device. 26604 * 26605 * Return Code: 0 - Command completed successfully with good status 26606 * -1 - Command failed. This could indicate a check condition 26607 * or other status value requiring recovery action. 26608 * 26609 */ 26610 26611 static int 26612 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26613 { 26614 int status; 26615 26616 ASSERT(un != NULL); 26617 ASSERT(!mutex_owned(SD_MUTEX(un))); 26618 ASSERT(pktp != NULL); 26619 26620 status = SD_SUCCESS; 26621 26622 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26623 pktp->pkt_flags |= un->un_tagflags; 26624 pktp->pkt_flags &= ~FLAG_NODISCON; 26625 } 26626 26627 status = sd_ddi_scsi_poll(pktp); 26628 /* 26629 * Scsi_poll returns 0 (success) if the command completes and the 26630 * status block is STATUS_GOOD. We should only check errors if this 26631 * condition is not true. Even then we should send our own request 26632 * sense packet only if we have a check condition and auto 26633 * request sense has not been performed by the hba. 26634 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26635 */ 26636 if ((status != SD_SUCCESS) && 26637 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26638 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26639 (pktp->pkt_reason != CMD_DEV_GONE)) 26640 (void) sd_send_polled_RQS(un); 26641 26642 return (status); 26643 } 26644 26645 /* 26646 * Function: sd_send_polled_RQS() 26647 * 26648 * Description: This sends the request sense command to a device. 26649 * 26650 * Arguments: sd_lun - The unit structure 26651 * 26652 * Return Code: 0 - Command completed successfully with good status 26653 * -1 - Command failed. 26654 * 26655 */ 26656 26657 static int 26658 sd_send_polled_RQS(struct sd_lun *un) 26659 { 26660 int ret_val; 26661 struct scsi_pkt *rqs_pktp; 26662 struct buf *rqs_bp; 26663 26664 ASSERT(un != NULL); 26665 ASSERT(!mutex_owned(SD_MUTEX(un))); 26666 26667 ret_val = SD_SUCCESS; 26668 26669 rqs_pktp = un->un_rqs_pktp; 26670 rqs_bp = un->un_rqs_bp; 26671 26672 mutex_enter(SD_MUTEX(un)); 26673 26674 if (un->un_sense_isbusy) { 26675 ret_val = SD_FAILURE; 26676 mutex_exit(SD_MUTEX(un)); 26677 return (ret_val); 26678 } 26679 26680 /* 26681 * If the request sense buffer (and packet) is not in use, 26682 * let's set the un_sense_isbusy and send our packet 26683 */ 26684 un->un_sense_isbusy = 1; 26685 rqs_pktp->pkt_resid = 0; 26686 rqs_pktp->pkt_reason = 0; 26687 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26688 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26689 26690 mutex_exit(SD_MUTEX(un)); 26691 26692 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26693 " 0x%p\n", rqs_bp->b_un.b_addr); 26694 26695 /* 26696 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26697 * axle - it has a call into us! 26698 */ 26699 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26700 SD_INFO(SD_LOG_COMMON, un, 26701 "sd_send_polled_RQS: RQS failed\n"); 26702 } 26703 26704 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26705 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26706 26707 mutex_enter(SD_MUTEX(un)); 26708 un->un_sense_isbusy = 0; 26709 mutex_exit(SD_MUTEX(un)); 26710 26711 return (ret_val); 26712 } 26713 26714 /* 26715 * Defines needed for localized version of the scsi_poll routine. 26716 */ 26717 #define SD_CSEC 10000 /* usecs */ 26718 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26719 26720 26721 /* 26722 * Function: sd_ddi_scsi_poll() 26723 * 26724 * Description: Localized version of the scsi_poll routine. The purpose is to 26725 * send a scsi_pkt to a device as a polled command. This version 26726 * is to ensure more robust handling of transport errors. 26727 * Specifically this routine cures not ready, coming ready 26728 * transition for power up and reset of sonoma's. This can take 26729 * up to 45 seconds for power-on and 20 seconds for reset of a 26730 * sonoma lun. 26731 * 26732 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26733 * 26734 * Return Code: 0 - Command completed successfully with good status 26735 * -1 - Command failed. 26736 * 26737 */ 26738 26739 static int 26740 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26741 { 26742 int busy_count; 26743 int timeout; 26744 int rval = SD_FAILURE; 26745 int savef; 26746 uint8_t *sensep; 26747 long savet; 26748 void (*savec)(); 26749 /* 26750 * The following is defined in machdep.c and is used in determining if 26751 * the scsi transport system will do polled I/O instead of interrupt 26752 * I/O when called from xx_dump(). 26753 */ 26754 extern int do_polled_io; 26755 26756 /* 26757 * save old flags in pkt, to restore at end 26758 */ 26759 savef = pkt->pkt_flags; 26760 savec = pkt->pkt_comp; 26761 savet = pkt->pkt_time; 26762 26763 pkt->pkt_flags |= FLAG_NOINTR; 26764 26765 /* 26766 * XXX there is nothing in the SCSA spec that states that we should not 26767 * do a callback for polled cmds; however, removing this will break sd 26768 * and probably other target drivers 26769 */ 26770 pkt->pkt_comp = NULL; 26771 26772 /* 26773 * we don't like a polled command without timeout. 26774 * 60 seconds seems long enough. 26775 */ 26776 if (pkt->pkt_time == 0) { 26777 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26778 } 26779 26780 /* 26781 * Send polled cmd. 26782 * 26783 * We do some error recovery for various errors. Tran_busy, 26784 * queue full, and non-dispatched commands are retried every 10 msec. 26785 * as they are typically transient failures. Busy status and Not 26786 * Ready are retried every second as this status takes a while to 26787 * change. Unit attention is retried for pkt_time (60) times 26788 * with no delay. 26789 */ 26790 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26791 26792 for (busy_count = 0; busy_count < timeout; busy_count++) { 26793 int rc; 26794 int poll_delay; 26795 26796 /* 26797 * Initialize pkt status variables. 26798 */ 26799 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26800 26801 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26802 if (rc != TRAN_BUSY) { 26803 /* Transport failed - give up. */ 26804 break; 26805 } else { 26806 /* Transport busy - try again. */ 26807 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26808 } 26809 } else { 26810 /* 26811 * Transport accepted - check pkt status. 26812 */ 26813 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26814 if (pkt->pkt_reason == CMD_CMPLT && 26815 rc == STATUS_CHECK && 26816 pkt->pkt_state & STATE_ARQ_DONE) { 26817 struct scsi_arq_status *arqstat = 26818 (struct scsi_arq_status *)(pkt->pkt_scbp); 26819 26820 sensep = (uint8_t *)&arqstat->sts_sensedata; 26821 } else { 26822 sensep = NULL; 26823 } 26824 26825 if ((pkt->pkt_reason == CMD_CMPLT) && 26826 (rc == STATUS_GOOD)) { 26827 /* No error - we're done */ 26828 rval = SD_SUCCESS; 26829 break; 26830 26831 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26832 /* Lost connection - give up */ 26833 break; 26834 26835 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26836 (pkt->pkt_state == 0)) { 26837 /* Pkt not dispatched - try again. */ 26838 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26839 26840 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26841 (rc == STATUS_QFULL)) { 26842 /* Queue full - try again. */ 26843 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26844 26845 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26846 (rc == STATUS_BUSY)) { 26847 /* Busy - try again. */ 26848 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26849 busy_count += (SD_SEC_TO_CSEC - 1); 26850 26851 } else if ((sensep != NULL) && 26852 (scsi_sense_key(sensep) == 26853 KEY_UNIT_ATTENTION)) { 26854 /* Unit Attention - try again */ 26855 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26856 continue; 26857 26858 } else if ((sensep != NULL) && 26859 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26860 (scsi_sense_asc(sensep) == 0x04) && 26861 (scsi_sense_ascq(sensep) == 0x01)) { 26862 /* Not ready -> ready - try again. */ 26863 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26864 busy_count += (SD_SEC_TO_CSEC - 1); 26865 26866 } else { 26867 /* BAD status - give up. */ 26868 break; 26869 } 26870 } 26871 26872 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26873 !do_polled_io) { 26874 delay(drv_usectohz(poll_delay)); 26875 } else { 26876 /* we busy wait during cpr_dump or interrupt threads */ 26877 drv_usecwait(poll_delay); 26878 } 26879 } 26880 26881 pkt->pkt_flags = savef; 26882 pkt->pkt_comp = savec; 26883 pkt->pkt_time = savet; 26884 return (rval); 26885 } 26886 26887 26888 /* 26889 * Function: sd_persistent_reservation_in_read_keys 26890 * 26891 * Description: This routine is the driver entry point for handling CD-ROM 26892 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26893 * by sending the SCSI-3 PRIN commands to the device. 26894 * Processes the read keys command response by copying the 26895 * reservation key information into the user provided buffer. 26896 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26897 * 26898 * Arguments: un - Pointer to soft state struct for the target. 26899 * usrp - user provided pointer to multihost Persistent In Read 26900 * Keys structure (mhioc_inkeys_t) 26901 * flag - this argument is a pass through to ddi_copyxxx() 26902 * directly from the mode argument of ioctl(). 26903 * 26904 * Return Code: 0 - Success 26905 * EACCES 26906 * ENOTSUP 26907 * errno return code from sd_send_scsi_cmd() 26908 * 26909 * Context: Can sleep. Does not return until command is completed. 26910 */ 26911 26912 static int 26913 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26914 mhioc_inkeys_t *usrp, int flag) 26915 { 26916 #ifdef _MULTI_DATAMODEL 26917 struct mhioc_key_list32 li32; 26918 #endif 26919 sd_prin_readkeys_t *in; 26920 mhioc_inkeys_t *ptr; 26921 mhioc_key_list_t li; 26922 uchar_t *data_bufp; 26923 int data_len; 26924 int rval; 26925 size_t copysz; 26926 26927 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26928 return (EINVAL); 26929 } 26930 bzero(&li, sizeof (mhioc_key_list_t)); 26931 26932 /* 26933 * Get the listsize from user 26934 */ 26935 #ifdef _MULTI_DATAMODEL 26936 26937 switch (ddi_model_convert_from(flag & FMODELS)) { 26938 case DDI_MODEL_ILP32: 26939 copysz = sizeof (struct mhioc_key_list32); 26940 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26941 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26942 "sd_persistent_reservation_in_read_keys: " 26943 "failed ddi_copyin: mhioc_key_list32_t\n"); 26944 rval = EFAULT; 26945 goto done; 26946 } 26947 li.listsize = li32.listsize; 26948 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26949 break; 26950 26951 case DDI_MODEL_NONE: 26952 copysz = sizeof (mhioc_key_list_t); 26953 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26954 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26955 "sd_persistent_reservation_in_read_keys: " 26956 "failed ddi_copyin: mhioc_key_list_t\n"); 26957 rval = EFAULT; 26958 goto done; 26959 } 26960 break; 26961 } 26962 26963 #else /* ! _MULTI_DATAMODEL */ 26964 copysz = sizeof (mhioc_key_list_t); 26965 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26966 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26967 "sd_persistent_reservation_in_read_keys: " 26968 "failed ddi_copyin: mhioc_key_list_t\n"); 26969 rval = EFAULT; 26970 goto done; 26971 } 26972 #endif 26973 26974 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26975 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26976 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26977 26978 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26979 data_len, data_bufp)) != 0) { 26980 goto done; 26981 } 26982 in = (sd_prin_readkeys_t *)data_bufp; 26983 ptr->generation = BE_32(in->generation); 26984 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26985 26986 /* 26987 * Return the min(listsize, listlen) keys 26988 */ 26989 #ifdef _MULTI_DATAMODEL 26990 26991 switch (ddi_model_convert_from(flag & FMODELS)) { 26992 case DDI_MODEL_ILP32: 26993 li32.listlen = li.listlen; 26994 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26995 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26996 "sd_persistent_reservation_in_read_keys: " 26997 "failed ddi_copyout: mhioc_key_list32_t\n"); 26998 rval = EFAULT; 26999 goto done; 27000 } 27001 break; 27002 27003 case DDI_MODEL_NONE: 27004 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27005 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27006 "sd_persistent_reservation_in_read_keys: " 27007 "failed ddi_copyout: mhioc_key_list_t\n"); 27008 rval = EFAULT; 27009 goto done; 27010 } 27011 break; 27012 } 27013 27014 #else /* ! _MULTI_DATAMODEL */ 27015 27016 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27017 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27018 "sd_persistent_reservation_in_read_keys: " 27019 "failed ddi_copyout: mhioc_key_list_t\n"); 27020 rval = EFAULT; 27021 goto done; 27022 } 27023 27024 #endif /* _MULTI_DATAMODEL */ 27025 27026 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 27027 li.listsize * MHIOC_RESV_KEY_SIZE); 27028 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27029 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27030 "sd_persistent_reservation_in_read_keys: " 27031 "failed ddi_copyout: keylist\n"); 27032 rval = EFAULT; 27033 } 27034 done: 27035 kmem_free(data_bufp, data_len); 27036 return (rval); 27037 } 27038 27039 27040 /* 27041 * Function: sd_persistent_reservation_in_read_resv 27042 * 27043 * Description: This routine is the driver entry point for handling CD-ROM 27044 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27045 * by sending the SCSI-3 PRIN commands to the device. 27046 * Process the read persistent reservations command response by 27047 * copying the reservation information into the user provided 27048 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27049 * 27050 * Arguments: un - Pointer to soft state struct for the target. 27051 * usrp - user provided pointer to multihost Persistent In Read 27052 * Keys structure (mhioc_inkeys_t) 27053 * flag - this argument is a pass through to ddi_copyxxx() 27054 * directly from the mode argument of ioctl(). 27055 * 27056 * Return Code: 0 - Success 27057 * EACCES 27058 * ENOTSUP 27059 * errno return code from sd_send_scsi_cmd() 27060 * 27061 * Context: Can sleep. Does not return until command is completed. 27062 */ 27063 27064 static int 27065 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27066 mhioc_inresvs_t *usrp, int flag) 27067 { 27068 #ifdef _MULTI_DATAMODEL 27069 struct mhioc_resv_desc_list32 resvlist32; 27070 #endif 27071 sd_prin_readresv_t *in; 27072 mhioc_inresvs_t *ptr; 27073 sd_readresv_desc_t *readresv_ptr; 27074 mhioc_resv_desc_list_t resvlist; 27075 mhioc_resv_desc_t resvdesc; 27076 uchar_t *data_bufp; 27077 int data_len; 27078 int rval; 27079 int i; 27080 size_t copysz; 27081 mhioc_resv_desc_t *bufp; 27082 27083 if ((ptr = usrp) == NULL) { 27084 return (EINVAL); 27085 } 27086 27087 /* 27088 * Get the listsize from user 27089 */ 27090 #ifdef _MULTI_DATAMODEL 27091 switch (ddi_model_convert_from(flag & FMODELS)) { 27092 case DDI_MODEL_ILP32: 27093 copysz = sizeof (struct mhioc_resv_desc_list32); 27094 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27095 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27096 "sd_persistent_reservation_in_read_resv: " 27097 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27098 rval = EFAULT; 27099 goto done; 27100 } 27101 resvlist.listsize = resvlist32.listsize; 27102 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27103 break; 27104 27105 case DDI_MODEL_NONE: 27106 copysz = sizeof (mhioc_resv_desc_list_t); 27107 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27108 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27109 "sd_persistent_reservation_in_read_resv: " 27110 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27111 rval = EFAULT; 27112 goto done; 27113 } 27114 break; 27115 } 27116 #else /* ! _MULTI_DATAMODEL */ 27117 copysz = sizeof (mhioc_resv_desc_list_t); 27118 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27119 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27120 "sd_persistent_reservation_in_read_resv: " 27121 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27122 rval = EFAULT; 27123 goto done; 27124 } 27125 #endif /* ! _MULTI_DATAMODEL */ 27126 27127 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27128 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27129 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27130 27131 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27132 data_len, data_bufp)) != 0) { 27133 goto done; 27134 } 27135 in = (sd_prin_readresv_t *)data_bufp; 27136 ptr->generation = BE_32(in->generation); 27137 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27138 27139 /* 27140 * Return the min(listsize, listlen( keys 27141 */ 27142 #ifdef _MULTI_DATAMODEL 27143 27144 switch (ddi_model_convert_from(flag & FMODELS)) { 27145 case DDI_MODEL_ILP32: 27146 resvlist32.listlen = resvlist.listlen; 27147 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27148 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27149 "sd_persistent_reservation_in_read_resv: " 27150 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27151 rval = EFAULT; 27152 goto done; 27153 } 27154 break; 27155 27156 case DDI_MODEL_NONE: 27157 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27158 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27159 "sd_persistent_reservation_in_read_resv: " 27160 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27161 rval = EFAULT; 27162 goto done; 27163 } 27164 break; 27165 } 27166 27167 #else /* ! _MULTI_DATAMODEL */ 27168 27169 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27170 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27171 "sd_persistent_reservation_in_read_resv: " 27172 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27173 rval = EFAULT; 27174 goto done; 27175 } 27176 27177 #endif /* ! _MULTI_DATAMODEL */ 27178 27179 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27180 bufp = resvlist.list; 27181 copysz = sizeof (mhioc_resv_desc_t); 27182 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27183 i++, readresv_ptr++, bufp++) { 27184 27185 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27186 MHIOC_RESV_KEY_SIZE); 27187 resvdesc.type = readresv_ptr->type; 27188 resvdesc.scope = readresv_ptr->scope; 27189 resvdesc.scope_specific_addr = 27190 BE_32(readresv_ptr->scope_specific_addr); 27191 27192 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27193 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27194 "sd_persistent_reservation_in_read_resv: " 27195 "failed ddi_copyout: resvlist\n"); 27196 rval = EFAULT; 27197 goto done; 27198 } 27199 } 27200 done: 27201 kmem_free(data_bufp, data_len); 27202 return (rval); 27203 } 27204 27205 27206 /* 27207 * Function: sr_change_blkmode() 27208 * 27209 * Description: This routine is the driver entry point for handling CD-ROM 27210 * block mode ioctl requests. Support for returning and changing 27211 * the current block size in use by the device is implemented. The 27212 * LBA size is changed via a MODE SELECT Block Descriptor. 27213 * 27214 * This routine issues a mode sense with an allocation length of 27215 * 12 bytes for the mode page header and a single block descriptor. 27216 * 27217 * Arguments: dev - the device 'dev_t' 27218 * cmd - the request type; one of CDROMGBLKMODE (get) or 27219 * CDROMSBLKMODE (set) 27220 * data - current block size or requested block size 27221 * flag - this argument is a pass through to ddi_copyxxx() directly 27222 * from the mode argument of ioctl(). 27223 * 27224 * Return Code: the code returned by sd_send_scsi_cmd() 27225 * EINVAL if invalid arguments are provided 27226 * EFAULT if ddi_copyxxx() fails 27227 * ENXIO if fail ddi_get_soft_state 27228 * EIO if invalid mode sense block descriptor length 27229 * 27230 */ 27231 27232 static int 27233 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27234 { 27235 struct sd_lun *un = NULL; 27236 struct mode_header *sense_mhp, *select_mhp; 27237 struct block_descriptor *sense_desc, *select_desc; 27238 int current_bsize; 27239 int rval = EINVAL; 27240 uchar_t *sense = NULL; 27241 uchar_t *select = NULL; 27242 27243 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27244 27245 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27246 return (ENXIO); 27247 } 27248 27249 /* 27250 * The block length is changed via the Mode Select block descriptor, the 27251 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27252 * required as part of this routine. Therefore the mode sense allocation 27253 * length is specified to be the length of a mode page header and a 27254 * block descriptor. 27255 */ 27256 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27257 27258 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27259 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27260 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27261 "sr_change_blkmode: Mode Sense Failed\n"); 27262 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27263 return (rval); 27264 } 27265 27266 /* Check the block descriptor len to handle only 1 block descriptor */ 27267 sense_mhp = (struct mode_header *)sense; 27268 if ((sense_mhp->bdesc_length == 0) || 27269 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27270 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27271 "sr_change_blkmode: Mode Sense returned invalid block" 27272 " descriptor length\n"); 27273 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27274 return (EIO); 27275 } 27276 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27277 current_bsize = ((sense_desc->blksize_hi << 16) | 27278 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27279 27280 /* Process command */ 27281 switch (cmd) { 27282 case CDROMGBLKMODE: 27283 /* Return the block size obtained during the mode sense */ 27284 if (ddi_copyout(¤t_bsize, (void *)data, 27285 sizeof (int), flag) != 0) 27286 rval = EFAULT; 27287 break; 27288 case CDROMSBLKMODE: 27289 /* Validate the requested block size */ 27290 switch (data) { 27291 case CDROM_BLK_512: 27292 case CDROM_BLK_1024: 27293 case CDROM_BLK_2048: 27294 case CDROM_BLK_2056: 27295 case CDROM_BLK_2336: 27296 case CDROM_BLK_2340: 27297 case CDROM_BLK_2352: 27298 case CDROM_BLK_2368: 27299 case CDROM_BLK_2448: 27300 case CDROM_BLK_2646: 27301 case CDROM_BLK_2647: 27302 break; 27303 default: 27304 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27305 "sr_change_blkmode: " 27306 "Block Size '%ld' Not Supported\n", data); 27307 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27308 return (EINVAL); 27309 } 27310 27311 /* 27312 * The current block size matches the requested block size so 27313 * there is no need to send the mode select to change the size 27314 */ 27315 if (current_bsize == data) { 27316 break; 27317 } 27318 27319 /* Build the select data for the requested block size */ 27320 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27321 select_mhp = (struct mode_header *)select; 27322 select_desc = 27323 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27324 /* 27325 * The LBA size is changed via the block descriptor, so the 27326 * descriptor is built according to the user data 27327 */ 27328 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27329 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27330 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27331 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27332 27333 /* Send the mode select for the requested block size */ 27334 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27335 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27336 SD_PATH_STANDARD)) != 0) { 27337 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27338 "sr_change_blkmode: Mode Select Failed\n"); 27339 /* 27340 * The mode select failed for the requested block size, 27341 * so reset the data for the original block size and 27342 * send it to the target. The error is indicated by the 27343 * return value for the failed mode select. 27344 */ 27345 select_desc->blksize_hi = sense_desc->blksize_hi; 27346 select_desc->blksize_mid = sense_desc->blksize_mid; 27347 select_desc->blksize_lo = sense_desc->blksize_lo; 27348 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27349 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27350 SD_PATH_STANDARD); 27351 } else { 27352 ASSERT(!mutex_owned(SD_MUTEX(un))); 27353 mutex_enter(SD_MUTEX(un)); 27354 sd_update_block_info(un, (uint32_t)data, 0); 27355 27356 mutex_exit(SD_MUTEX(un)); 27357 } 27358 break; 27359 default: 27360 /* should not reach here, but check anyway */ 27361 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27362 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27363 rval = EINVAL; 27364 break; 27365 } 27366 27367 if (select) { 27368 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27369 } 27370 if (sense) { 27371 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27372 } 27373 return (rval); 27374 } 27375 27376 27377 /* 27378 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27379 * implement driver support for getting and setting the CD speed. The command 27380 * set used will be based on the device type. If the device has not been 27381 * identified as MMC the Toshiba vendor specific mode page will be used. If 27382 * the device is MMC but does not support the Real Time Streaming feature 27383 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27384 * be used to read the speed. 27385 */ 27386 27387 /* 27388 * Function: sr_change_speed() 27389 * 27390 * Description: This routine is the driver entry point for handling CD-ROM 27391 * drive speed ioctl requests for devices supporting the Toshiba 27392 * vendor specific drive speed mode page. Support for returning 27393 * and changing the current drive speed in use by the device is 27394 * implemented. 27395 * 27396 * Arguments: dev - the device 'dev_t' 27397 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27398 * CDROMSDRVSPEED (set) 27399 * data - current drive speed or requested drive speed 27400 * flag - this argument is a pass through to ddi_copyxxx() directly 27401 * from the mode argument of ioctl(). 27402 * 27403 * Return Code: the code returned by sd_send_scsi_cmd() 27404 * EINVAL if invalid arguments are provided 27405 * EFAULT if ddi_copyxxx() fails 27406 * ENXIO if fail ddi_get_soft_state 27407 * EIO if invalid mode sense block descriptor length 27408 */ 27409 27410 static int 27411 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27412 { 27413 struct sd_lun *un = NULL; 27414 struct mode_header *sense_mhp, *select_mhp; 27415 struct mode_speed *sense_page, *select_page; 27416 int current_speed; 27417 int rval = EINVAL; 27418 int bd_len; 27419 uchar_t *sense = NULL; 27420 uchar_t *select = NULL; 27421 27422 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27423 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27424 return (ENXIO); 27425 } 27426 27427 /* 27428 * Note: The drive speed is being modified here according to a Toshiba 27429 * vendor specific mode page (0x31). 27430 */ 27431 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27432 27433 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27434 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27435 SD_PATH_STANDARD)) != 0) { 27436 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27437 "sr_change_speed: Mode Sense Failed\n"); 27438 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27439 return (rval); 27440 } 27441 sense_mhp = (struct mode_header *)sense; 27442 27443 /* Check the block descriptor len to handle only 1 block descriptor */ 27444 bd_len = sense_mhp->bdesc_length; 27445 if (bd_len > MODE_BLK_DESC_LENGTH) { 27446 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27447 "sr_change_speed: Mode Sense returned invalid block " 27448 "descriptor length\n"); 27449 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27450 return (EIO); 27451 } 27452 27453 sense_page = (struct mode_speed *) 27454 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27455 current_speed = sense_page->speed; 27456 27457 /* Process command */ 27458 switch (cmd) { 27459 case CDROMGDRVSPEED: 27460 /* Return the drive speed obtained during the mode sense */ 27461 if (current_speed == 0x2) { 27462 current_speed = CDROM_TWELVE_SPEED; 27463 } 27464 if (ddi_copyout(¤t_speed, (void *)data, 27465 sizeof (int), flag) != 0) { 27466 rval = EFAULT; 27467 } 27468 break; 27469 case CDROMSDRVSPEED: 27470 /* Validate the requested drive speed */ 27471 switch ((uchar_t)data) { 27472 case CDROM_TWELVE_SPEED: 27473 data = 0x2; 27474 /*FALLTHROUGH*/ 27475 case CDROM_NORMAL_SPEED: 27476 case CDROM_DOUBLE_SPEED: 27477 case CDROM_QUAD_SPEED: 27478 case CDROM_MAXIMUM_SPEED: 27479 break; 27480 default: 27481 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27482 "sr_change_speed: " 27483 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27484 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27485 return (EINVAL); 27486 } 27487 27488 /* 27489 * The current drive speed matches the requested drive speed so 27490 * there is no need to send the mode select to change the speed 27491 */ 27492 if (current_speed == data) { 27493 break; 27494 } 27495 27496 /* Build the select data for the requested drive speed */ 27497 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27498 select_mhp = (struct mode_header *)select; 27499 select_mhp->bdesc_length = 0; 27500 select_page = 27501 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27502 select_page = 27503 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27504 select_page->mode_page.code = CDROM_MODE_SPEED; 27505 select_page->mode_page.length = 2; 27506 select_page->speed = (uchar_t)data; 27507 27508 /* Send the mode select for the requested block size */ 27509 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27510 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27511 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27512 /* 27513 * The mode select failed for the requested drive speed, 27514 * so reset the data for the original drive speed and 27515 * send it to the target. The error is indicated by the 27516 * return value for the failed mode select. 27517 */ 27518 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27519 "sr_drive_speed: Mode Select Failed\n"); 27520 select_page->speed = sense_page->speed; 27521 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27522 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27523 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27524 } 27525 break; 27526 default: 27527 /* should not reach here, but check anyway */ 27528 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27529 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27530 rval = EINVAL; 27531 break; 27532 } 27533 27534 if (select) { 27535 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27536 } 27537 if (sense) { 27538 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27539 } 27540 27541 return (rval); 27542 } 27543 27544 27545 /* 27546 * Function: sr_atapi_change_speed() 27547 * 27548 * Description: This routine is the driver entry point for handling CD-ROM 27549 * drive speed ioctl requests for MMC devices that do not support 27550 * the Real Time Streaming feature (0x107). 27551 * 27552 * Note: This routine will use the SET SPEED command which may not 27553 * be supported by all devices. 27554 * 27555 * Arguments: dev- the device 'dev_t' 27556 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27557 * CDROMSDRVSPEED (set) 27558 * data- current drive speed or requested drive speed 27559 * flag- this argument is a pass through to ddi_copyxxx() directly 27560 * from the mode argument of ioctl(). 27561 * 27562 * Return Code: the code returned by sd_send_scsi_cmd() 27563 * EINVAL if invalid arguments are provided 27564 * EFAULT if ddi_copyxxx() fails 27565 * ENXIO if fail ddi_get_soft_state 27566 * EIO if invalid mode sense block descriptor length 27567 */ 27568 27569 static int 27570 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27571 { 27572 struct sd_lun *un; 27573 struct uscsi_cmd *com = NULL; 27574 struct mode_header_grp2 *sense_mhp; 27575 uchar_t *sense_page; 27576 uchar_t *sense = NULL; 27577 char cdb[CDB_GROUP5]; 27578 int bd_len; 27579 int current_speed = 0; 27580 int max_speed = 0; 27581 int rval; 27582 27583 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27584 27585 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27586 return (ENXIO); 27587 } 27588 27589 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27590 27591 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27592 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27593 SD_PATH_STANDARD)) != 0) { 27594 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27595 "sr_atapi_change_speed: Mode Sense Failed\n"); 27596 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27597 return (rval); 27598 } 27599 27600 /* Check the block descriptor len to handle only 1 block descriptor */ 27601 sense_mhp = (struct mode_header_grp2 *)sense; 27602 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27603 if (bd_len > MODE_BLK_DESC_LENGTH) { 27604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27605 "sr_atapi_change_speed: Mode Sense returned invalid " 27606 "block descriptor length\n"); 27607 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27608 return (EIO); 27609 } 27610 27611 /* Calculate the current and maximum drive speeds */ 27612 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27613 current_speed = (sense_page[14] << 8) | sense_page[15]; 27614 max_speed = (sense_page[8] << 8) | sense_page[9]; 27615 27616 /* Process the command */ 27617 switch (cmd) { 27618 case CDROMGDRVSPEED: 27619 current_speed /= SD_SPEED_1X; 27620 if (ddi_copyout(¤t_speed, (void *)data, 27621 sizeof (int), flag) != 0) 27622 rval = EFAULT; 27623 break; 27624 case CDROMSDRVSPEED: 27625 /* Convert the speed code to KB/sec */ 27626 switch ((uchar_t)data) { 27627 case CDROM_NORMAL_SPEED: 27628 current_speed = SD_SPEED_1X; 27629 break; 27630 case CDROM_DOUBLE_SPEED: 27631 current_speed = 2 * SD_SPEED_1X; 27632 break; 27633 case CDROM_QUAD_SPEED: 27634 current_speed = 4 * SD_SPEED_1X; 27635 break; 27636 case CDROM_TWELVE_SPEED: 27637 current_speed = 12 * SD_SPEED_1X; 27638 break; 27639 case CDROM_MAXIMUM_SPEED: 27640 current_speed = 0xffff; 27641 break; 27642 default: 27643 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27644 "sr_atapi_change_speed: invalid drive speed %d\n", 27645 (uchar_t)data); 27646 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27647 return (EINVAL); 27648 } 27649 27650 /* Check the request against the drive's max speed. */ 27651 if (current_speed != 0xffff) { 27652 if (current_speed > max_speed) { 27653 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27654 return (EINVAL); 27655 } 27656 } 27657 27658 /* 27659 * Build and send the SET SPEED command 27660 * 27661 * Note: The SET SPEED (0xBB) command used in this routine is 27662 * obsolete per the SCSI MMC spec but still supported in the 27663 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27664 * therefore the command is still implemented in this routine. 27665 */ 27666 bzero(cdb, sizeof (cdb)); 27667 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27668 cdb[2] = (uchar_t)(current_speed >> 8); 27669 cdb[3] = (uchar_t)current_speed; 27670 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27671 com->uscsi_cdb = (caddr_t)cdb; 27672 com->uscsi_cdblen = CDB_GROUP5; 27673 com->uscsi_bufaddr = NULL; 27674 com->uscsi_buflen = 0; 27675 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27676 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 27677 break; 27678 default: 27679 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27680 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27681 rval = EINVAL; 27682 } 27683 27684 if (sense) { 27685 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27686 } 27687 if (com) { 27688 kmem_free(com, sizeof (*com)); 27689 } 27690 return (rval); 27691 } 27692 27693 27694 /* 27695 * Function: sr_pause_resume() 27696 * 27697 * Description: This routine is the driver entry point for handling CD-ROM 27698 * pause/resume ioctl requests. This only affects the audio play 27699 * operation. 27700 * 27701 * Arguments: dev - the device 'dev_t' 27702 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27703 * for setting the resume bit of the cdb. 27704 * 27705 * Return Code: the code returned by sd_send_scsi_cmd() 27706 * EINVAL if invalid mode specified 27707 * 27708 */ 27709 27710 static int 27711 sr_pause_resume(dev_t dev, int cmd) 27712 { 27713 struct sd_lun *un; 27714 struct uscsi_cmd *com; 27715 char cdb[CDB_GROUP1]; 27716 int rval; 27717 27718 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27719 return (ENXIO); 27720 } 27721 27722 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27723 bzero(cdb, CDB_GROUP1); 27724 cdb[0] = SCMD_PAUSE_RESUME; 27725 switch (cmd) { 27726 case CDROMRESUME: 27727 cdb[8] = 1; 27728 break; 27729 case CDROMPAUSE: 27730 cdb[8] = 0; 27731 break; 27732 default: 27733 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27734 " Command '%x' Not Supported\n", cmd); 27735 rval = EINVAL; 27736 goto done; 27737 } 27738 27739 com->uscsi_cdb = cdb; 27740 com->uscsi_cdblen = CDB_GROUP1; 27741 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27742 27743 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27744 SD_PATH_STANDARD); 27745 27746 done: 27747 kmem_free(com, sizeof (*com)); 27748 return (rval); 27749 } 27750 27751 27752 /* 27753 * Function: sr_play_msf() 27754 * 27755 * Description: This routine is the driver entry point for handling CD-ROM 27756 * ioctl requests to output the audio signals at the specified 27757 * starting address and continue the audio play until the specified 27758 * ending address (CDROMPLAYMSF) The address is in Minute Second 27759 * Frame (MSF) format. 27760 * 27761 * Arguments: dev - the device 'dev_t' 27762 * data - pointer to user provided audio msf structure, 27763 * specifying start/end addresses. 27764 * flag - this argument is a pass through to ddi_copyxxx() 27765 * directly from the mode argument of ioctl(). 27766 * 27767 * Return Code: the code returned by sd_send_scsi_cmd() 27768 * EFAULT if ddi_copyxxx() fails 27769 * ENXIO if fail ddi_get_soft_state 27770 * EINVAL if data pointer is NULL 27771 */ 27772 27773 static int 27774 sr_play_msf(dev_t dev, caddr_t data, int flag) 27775 { 27776 struct sd_lun *un; 27777 struct uscsi_cmd *com; 27778 struct cdrom_msf msf_struct; 27779 struct cdrom_msf *msf = &msf_struct; 27780 char cdb[CDB_GROUP1]; 27781 int rval; 27782 27783 if (data == NULL) { 27784 return (EINVAL); 27785 } 27786 27787 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27788 return (ENXIO); 27789 } 27790 27791 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27792 return (EFAULT); 27793 } 27794 27795 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27796 bzero(cdb, CDB_GROUP1); 27797 cdb[0] = SCMD_PLAYAUDIO_MSF; 27798 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27799 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27800 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27801 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27802 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27803 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27804 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27805 } else { 27806 cdb[3] = msf->cdmsf_min0; 27807 cdb[4] = msf->cdmsf_sec0; 27808 cdb[5] = msf->cdmsf_frame0; 27809 cdb[6] = msf->cdmsf_min1; 27810 cdb[7] = msf->cdmsf_sec1; 27811 cdb[8] = msf->cdmsf_frame1; 27812 } 27813 com->uscsi_cdb = cdb; 27814 com->uscsi_cdblen = CDB_GROUP1; 27815 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27816 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27817 SD_PATH_STANDARD); 27818 kmem_free(com, sizeof (*com)); 27819 return (rval); 27820 } 27821 27822 27823 /* 27824 * Function: sr_play_trkind() 27825 * 27826 * Description: This routine is the driver entry point for handling CD-ROM 27827 * ioctl requests to output the audio signals at the specified 27828 * starting address and continue the audio play until the specified 27829 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27830 * format. 27831 * 27832 * Arguments: dev - the device 'dev_t' 27833 * data - pointer to user provided audio track/index structure, 27834 * specifying start/end addresses. 27835 * flag - this argument is a pass through to ddi_copyxxx() 27836 * directly from the mode argument of ioctl(). 27837 * 27838 * Return Code: the code returned by sd_send_scsi_cmd() 27839 * EFAULT if ddi_copyxxx() fails 27840 * ENXIO if fail ddi_get_soft_state 27841 * EINVAL if data pointer is NULL 27842 */ 27843 27844 static int 27845 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27846 { 27847 struct cdrom_ti ti_struct; 27848 struct cdrom_ti *ti = &ti_struct; 27849 struct uscsi_cmd *com = NULL; 27850 char cdb[CDB_GROUP1]; 27851 int rval; 27852 27853 if (data == NULL) { 27854 return (EINVAL); 27855 } 27856 27857 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27858 return (EFAULT); 27859 } 27860 27861 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27862 bzero(cdb, CDB_GROUP1); 27863 cdb[0] = SCMD_PLAYAUDIO_TI; 27864 cdb[4] = ti->cdti_trk0; 27865 cdb[5] = ti->cdti_ind0; 27866 cdb[7] = ti->cdti_trk1; 27867 cdb[8] = ti->cdti_ind1; 27868 com->uscsi_cdb = cdb; 27869 com->uscsi_cdblen = CDB_GROUP1; 27870 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27871 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27872 SD_PATH_STANDARD); 27873 kmem_free(com, sizeof (*com)); 27874 return (rval); 27875 } 27876 27877 27878 /* 27879 * Function: sr_read_all_subcodes() 27880 * 27881 * Description: This routine is the driver entry point for handling CD-ROM 27882 * ioctl requests to return raw subcode data while the target is 27883 * playing audio (CDROMSUBCODE). 27884 * 27885 * Arguments: dev - the device 'dev_t' 27886 * data - pointer to user provided cdrom subcode structure, 27887 * specifying the transfer length and address. 27888 * flag - this argument is a pass through to ddi_copyxxx() 27889 * directly from the mode argument of ioctl(). 27890 * 27891 * Return Code: the code returned by sd_send_scsi_cmd() 27892 * EFAULT if ddi_copyxxx() fails 27893 * ENXIO if fail ddi_get_soft_state 27894 * EINVAL if data pointer is NULL 27895 */ 27896 27897 static int 27898 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27899 { 27900 struct sd_lun *un = NULL; 27901 struct uscsi_cmd *com = NULL; 27902 struct cdrom_subcode *subcode = NULL; 27903 int rval; 27904 size_t buflen; 27905 char cdb[CDB_GROUP5]; 27906 27907 #ifdef _MULTI_DATAMODEL 27908 /* To support ILP32 applications in an LP64 world */ 27909 struct cdrom_subcode32 cdrom_subcode32; 27910 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27911 #endif 27912 if (data == NULL) { 27913 return (EINVAL); 27914 } 27915 27916 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27917 return (ENXIO); 27918 } 27919 27920 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27921 27922 #ifdef _MULTI_DATAMODEL 27923 switch (ddi_model_convert_from(flag & FMODELS)) { 27924 case DDI_MODEL_ILP32: 27925 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27926 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27927 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27928 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27929 return (EFAULT); 27930 } 27931 /* Convert the ILP32 uscsi data from the application to LP64 */ 27932 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27933 break; 27934 case DDI_MODEL_NONE: 27935 if (ddi_copyin(data, subcode, 27936 sizeof (struct cdrom_subcode), flag)) { 27937 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27938 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27939 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27940 return (EFAULT); 27941 } 27942 break; 27943 } 27944 #else /* ! _MULTI_DATAMODEL */ 27945 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27946 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27947 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27948 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27949 return (EFAULT); 27950 } 27951 #endif /* _MULTI_DATAMODEL */ 27952 27953 /* 27954 * Since MMC-2 expects max 3 bytes for length, check if the 27955 * length input is greater than 3 bytes 27956 */ 27957 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27958 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27959 "sr_read_all_subcodes: " 27960 "cdrom transfer length too large: %d (limit %d)\n", 27961 subcode->cdsc_length, 0xFFFFFF); 27962 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27963 return (EINVAL); 27964 } 27965 27966 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27967 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27968 bzero(cdb, CDB_GROUP5); 27969 27970 if (un->un_f_mmc_cap == TRUE) { 27971 cdb[0] = (char)SCMD_READ_CD; 27972 cdb[2] = (char)0xff; 27973 cdb[3] = (char)0xff; 27974 cdb[4] = (char)0xff; 27975 cdb[5] = (char)0xff; 27976 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27977 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27978 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27979 cdb[10] = 1; 27980 } else { 27981 /* 27982 * Note: A vendor specific command (0xDF) is being used her to 27983 * request a read of all subcodes. 27984 */ 27985 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27986 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27987 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27988 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27989 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27990 } 27991 com->uscsi_cdb = cdb; 27992 com->uscsi_cdblen = CDB_GROUP5; 27993 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27994 com->uscsi_buflen = buflen; 27995 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27996 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27997 SD_PATH_STANDARD); 27998 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27999 kmem_free(com, sizeof (*com)); 28000 return (rval); 28001 } 28002 28003 28004 /* 28005 * Function: sr_read_subchannel() 28006 * 28007 * Description: This routine is the driver entry point for handling CD-ROM 28008 * ioctl requests to return the Q sub-channel data of the CD 28009 * current position block. (CDROMSUBCHNL) The data includes the 28010 * track number, index number, absolute CD-ROM address (LBA or MSF 28011 * format per the user) , track relative CD-ROM address (LBA or MSF 28012 * format per the user), control data and audio status. 28013 * 28014 * Arguments: dev - the device 'dev_t' 28015 * data - pointer to user provided cdrom sub-channel structure 28016 * flag - this argument is a pass through to ddi_copyxxx() 28017 * directly from the mode argument of ioctl(). 28018 * 28019 * Return Code: the code returned by sd_send_scsi_cmd() 28020 * EFAULT if ddi_copyxxx() fails 28021 * ENXIO if fail ddi_get_soft_state 28022 * EINVAL if data pointer is NULL 28023 */ 28024 28025 static int 28026 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 28027 { 28028 struct sd_lun *un; 28029 struct uscsi_cmd *com; 28030 struct cdrom_subchnl subchanel; 28031 struct cdrom_subchnl *subchnl = &subchanel; 28032 char cdb[CDB_GROUP1]; 28033 caddr_t buffer; 28034 int rval; 28035 28036 if (data == NULL) { 28037 return (EINVAL); 28038 } 28039 28040 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28041 (un->un_state == SD_STATE_OFFLINE)) { 28042 return (ENXIO); 28043 } 28044 28045 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28046 return (EFAULT); 28047 } 28048 28049 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28050 bzero(cdb, CDB_GROUP1); 28051 cdb[0] = SCMD_READ_SUBCHANNEL; 28052 /* Set the MSF bit based on the user requested address format */ 28053 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28054 /* 28055 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28056 * returned 28057 */ 28058 cdb[2] = 0x40; 28059 /* 28060 * Set byte 3 to specify the return data format. A value of 0x01 28061 * indicates that the CD-ROM current position should be returned. 28062 */ 28063 cdb[3] = 0x01; 28064 cdb[8] = 0x10; 28065 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28066 com->uscsi_cdb = cdb; 28067 com->uscsi_cdblen = CDB_GROUP1; 28068 com->uscsi_bufaddr = buffer; 28069 com->uscsi_buflen = 16; 28070 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28071 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28072 SD_PATH_STANDARD); 28073 if (rval != 0) { 28074 kmem_free(buffer, 16); 28075 kmem_free(com, sizeof (*com)); 28076 return (rval); 28077 } 28078 28079 /* Process the returned Q sub-channel data */ 28080 subchnl->cdsc_audiostatus = buffer[1]; 28081 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28082 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28083 subchnl->cdsc_trk = buffer[6]; 28084 subchnl->cdsc_ind = buffer[7]; 28085 if (subchnl->cdsc_format & CDROM_LBA) { 28086 subchnl->cdsc_absaddr.lba = 28087 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28088 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28089 subchnl->cdsc_reladdr.lba = 28090 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28091 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28092 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28093 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28094 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28095 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28096 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28097 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28098 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28099 } else { 28100 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28101 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28102 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28103 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28104 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28105 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28106 } 28107 kmem_free(buffer, 16); 28108 kmem_free(com, sizeof (*com)); 28109 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28110 != 0) { 28111 return (EFAULT); 28112 } 28113 return (rval); 28114 } 28115 28116 28117 /* 28118 * Function: sr_read_tocentry() 28119 * 28120 * Description: This routine is the driver entry point for handling CD-ROM 28121 * ioctl requests to read from the Table of Contents (TOC) 28122 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28123 * fields, the starting address (LBA or MSF format per the user) 28124 * and the data mode if the user specified track is a data track. 28125 * 28126 * Note: The READ HEADER (0x44) command used in this routine is 28127 * obsolete per the SCSI MMC spec but still supported in the 28128 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28129 * therefore the command is still implemented in this routine. 28130 * 28131 * Arguments: dev - the device 'dev_t' 28132 * data - pointer to user provided toc entry structure, 28133 * specifying the track # and the address format 28134 * (LBA or MSF). 28135 * flag - this argument is a pass through to ddi_copyxxx() 28136 * directly from the mode argument of ioctl(). 28137 * 28138 * Return Code: the code returned by sd_send_scsi_cmd() 28139 * EFAULT if ddi_copyxxx() fails 28140 * ENXIO if fail ddi_get_soft_state 28141 * EINVAL if data pointer is NULL 28142 */ 28143 28144 static int 28145 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28146 { 28147 struct sd_lun *un = NULL; 28148 struct uscsi_cmd *com; 28149 struct cdrom_tocentry toc_entry; 28150 struct cdrom_tocentry *entry = &toc_entry; 28151 caddr_t buffer; 28152 int rval; 28153 char cdb[CDB_GROUP1]; 28154 28155 if (data == NULL) { 28156 return (EINVAL); 28157 } 28158 28159 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28160 (un->un_state == SD_STATE_OFFLINE)) { 28161 return (ENXIO); 28162 } 28163 28164 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28165 return (EFAULT); 28166 } 28167 28168 /* Validate the requested track and address format */ 28169 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28170 return (EINVAL); 28171 } 28172 28173 if (entry->cdte_track == 0) { 28174 return (EINVAL); 28175 } 28176 28177 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28178 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28179 bzero(cdb, CDB_GROUP1); 28180 28181 cdb[0] = SCMD_READ_TOC; 28182 /* Set the MSF bit based on the user requested address format */ 28183 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28184 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28185 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28186 } else { 28187 cdb[6] = entry->cdte_track; 28188 } 28189 28190 /* 28191 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28192 * (4 byte TOC response header + 8 byte track descriptor) 28193 */ 28194 cdb[8] = 12; 28195 com->uscsi_cdb = cdb; 28196 com->uscsi_cdblen = CDB_GROUP1; 28197 com->uscsi_bufaddr = buffer; 28198 com->uscsi_buflen = 0x0C; 28199 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28200 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28201 SD_PATH_STANDARD); 28202 if (rval != 0) { 28203 kmem_free(buffer, 12); 28204 kmem_free(com, sizeof (*com)); 28205 return (rval); 28206 } 28207 28208 /* Process the toc entry */ 28209 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28210 entry->cdte_ctrl = (buffer[5] & 0x0F); 28211 if (entry->cdte_format & CDROM_LBA) { 28212 entry->cdte_addr.lba = 28213 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28214 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28215 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28216 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28217 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28218 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28219 /* 28220 * Send a READ TOC command using the LBA address format to get 28221 * the LBA for the track requested so it can be used in the 28222 * READ HEADER request 28223 * 28224 * Note: The MSF bit of the READ HEADER command specifies the 28225 * output format. The block address specified in that command 28226 * must be in LBA format. 28227 */ 28228 cdb[1] = 0; 28229 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28230 SD_PATH_STANDARD); 28231 if (rval != 0) { 28232 kmem_free(buffer, 12); 28233 kmem_free(com, sizeof (*com)); 28234 return (rval); 28235 } 28236 } else { 28237 entry->cdte_addr.msf.minute = buffer[9]; 28238 entry->cdte_addr.msf.second = buffer[10]; 28239 entry->cdte_addr.msf.frame = buffer[11]; 28240 /* 28241 * Send a READ TOC command using the LBA address format to get 28242 * the LBA for the track requested so it can be used in the 28243 * READ HEADER request 28244 * 28245 * Note: The MSF bit of the READ HEADER command specifies the 28246 * output format. The block address specified in that command 28247 * must be in LBA format. 28248 */ 28249 cdb[1] = 0; 28250 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28251 SD_PATH_STANDARD); 28252 if (rval != 0) { 28253 kmem_free(buffer, 12); 28254 kmem_free(com, sizeof (*com)); 28255 return (rval); 28256 } 28257 } 28258 28259 /* 28260 * Build and send the READ HEADER command to determine the data mode of 28261 * the user specified track. 28262 */ 28263 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28264 (entry->cdte_track != CDROM_LEADOUT)) { 28265 bzero(cdb, CDB_GROUP1); 28266 cdb[0] = SCMD_READ_HEADER; 28267 cdb[2] = buffer[8]; 28268 cdb[3] = buffer[9]; 28269 cdb[4] = buffer[10]; 28270 cdb[5] = buffer[11]; 28271 cdb[8] = 0x08; 28272 com->uscsi_buflen = 0x08; 28273 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28274 SD_PATH_STANDARD); 28275 if (rval == 0) { 28276 entry->cdte_datamode = buffer[0]; 28277 } else { 28278 /* 28279 * READ HEADER command failed, since this is 28280 * obsoleted in one spec, its better to return 28281 * -1 for an invlid track so that we can still 28282 * recieve the rest of the TOC data. 28283 */ 28284 entry->cdte_datamode = (uchar_t)-1; 28285 } 28286 } else { 28287 entry->cdte_datamode = (uchar_t)-1; 28288 } 28289 28290 kmem_free(buffer, 12); 28291 kmem_free(com, sizeof (*com)); 28292 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28293 return (EFAULT); 28294 28295 return (rval); 28296 } 28297 28298 28299 /* 28300 * Function: sr_read_tochdr() 28301 * 28302 * Description: This routine is the driver entry point for handling CD-ROM 28303 * ioctl requests to read the Table of Contents (TOC) header 28304 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28305 * and ending track numbers 28306 * 28307 * Arguments: dev - the device 'dev_t' 28308 * data - pointer to user provided toc header structure, 28309 * specifying the starting and ending track numbers. 28310 * flag - this argument is a pass through to ddi_copyxxx() 28311 * directly from the mode argument of ioctl(). 28312 * 28313 * Return Code: the code returned by sd_send_scsi_cmd() 28314 * EFAULT if ddi_copyxxx() fails 28315 * ENXIO if fail ddi_get_soft_state 28316 * EINVAL if data pointer is NULL 28317 */ 28318 28319 static int 28320 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28321 { 28322 struct sd_lun *un; 28323 struct uscsi_cmd *com; 28324 struct cdrom_tochdr toc_header; 28325 struct cdrom_tochdr *hdr = &toc_header; 28326 char cdb[CDB_GROUP1]; 28327 int rval; 28328 caddr_t buffer; 28329 28330 if (data == NULL) { 28331 return (EINVAL); 28332 } 28333 28334 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28335 (un->un_state == SD_STATE_OFFLINE)) { 28336 return (ENXIO); 28337 } 28338 28339 buffer = kmem_zalloc(4, KM_SLEEP); 28340 bzero(cdb, CDB_GROUP1); 28341 cdb[0] = SCMD_READ_TOC; 28342 /* 28343 * Specifying a track number of 0x00 in the READ TOC command indicates 28344 * that the TOC header should be returned 28345 */ 28346 cdb[6] = 0x00; 28347 /* 28348 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28349 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28350 */ 28351 cdb[8] = 0x04; 28352 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28353 com->uscsi_cdb = cdb; 28354 com->uscsi_cdblen = CDB_GROUP1; 28355 com->uscsi_bufaddr = buffer; 28356 com->uscsi_buflen = 0x04; 28357 com->uscsi_timeout = 300; 28358 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28359 28360 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28361 SD_PATH_STANDARD); 28362 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28363 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28364 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28365 } else { 28366 hdr->cdth_trk0 = buffer[2]; 28367 hdr->cdth_trk1 = buffer[3]; 28368 } 28369 kmem_free(buffer, 4); 28370 kmem_free(com, sizeof (*com)); 28371 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28372 return (EFAULT); 28373 } 28374 return (rval); 28375 } 28376 28377 28378 /* 28379 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28380 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28381 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28382 * digital audio and extended architecture digital audio. These modes are 28383 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28384 * MMC specs. 28385 * 28386 * In addition to support for the various data formats these routines also 28387 * include support for devices that implement only the direct access READ 28388 * commands (0x08, 0x28), devices that implement the READ_CD commands 28389 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28390 * READ CDXA commands (0xD8, 0xDB) 28391 */ 28392 28393 /* 28394 * Function: sr_read_mode1() 28395 * 28396 * Description: This routine is the driver entry point for handling CD-ROM 28397 * ioctl read mode1 requests (CDROMREADMODE1). 28398 * 28399 * Arguments: dev - the device 'dev_t' 28400 * data - pointer to user provided cd read structure specifying 28401 * the lba buffer address and length. 28402 * flag - this argument is a pass through to ddi_copyxxx() 28403 * directly from the mode argument of ioctl(). 28404 * 28405 * Return Code: the code returned by sd_send_scsi_cmd() 28406 * EFAULT if ddi_copyxxx() fails 28407 * ENXIO if fail ddi_get_soft_state 28408 * EINVAL if data pointer is NULL 28409 */ 28410 28411 static int 28412 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28413 { 28414 struct sd_lun *un; 28415 struct cdrom_read mode1_struct; 28416 struct cdrom_read *mode1 = &mode1_struct; 28417 int rval; 28418 #ifdef _MULTI_DATAMODEL 28419 /* To support ILP32 applications in an LP64 world */ 28420 struct cdrom_read32 cdrom_read32; 28421 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28422 #endif /* _MULTI_DATAMODEL */ 28423 28424 if (data == NULL) { 28425 return (EINVAL); 28426 } 28427 28428 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28429 (un->un_state == SD_STATE_OFFLINE)) { 28430 return (ENXIO); 28431 } 28432 28433 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28434 "sd_read_mode1: entry: un:0x%p\n", un); 28435 28436 #ifdef _MULTI_DATAMODEL 28437 switch (ddi_model_convert_from(flag & FMODELS)) { 28438 case DDI_MODEL_ILP32: 28439 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28440 return (EFAULT); 28441 } 28442 /* Convert the ILP32 uscsi data from the application to LP64 */ 28443 cdrom_read32tocdrom_read(cdrd32, mode1); 28444 break; 28445 case DDI_MODEL_NONE: 28446 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28447 return (EFAULT); 28448 } 28449 } 28450 #else /* ! _MULTI_DATAMODEL */ 28451 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28452 return (EFAULT); 28453 } 28454 #endif /* _MULTI_DATAMODEL */ 28455 28456 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28457 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28458 28459 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28460 "sd_read_mode1: exit: un:0x%p\n", un); 28461 28462 return (rval); 28463 } 28464 28465 28466 /* 28467 * Function: sr_read_cd_mode2() 28468 * 28469 * Description: This routine is the driver entry point for handling CD-ROM 28470 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28471 * support the READ CD (0xBE) command or the 1st generation 28472 * READ CD (0xD4) command. 28473 * 28474 * Arguments: dev - the device 'dev_t' 28475 * data - pointer to user provided cd read structure specifying 28476 * the lba buffer address and length. 28477 * flag - this argument is a pass through to ddi_copyxxx() 28478 * directly from the mode argument of ioctl(). 28479 * 28480 * Return Code: the code returned by sd_send_scsi_cmd() 28481 * EFAULT if ddi_copyxxx() fails 28482 * ENXIO if fail ddi_get_soft_state 28483 * EINVAL if data pointer is NULL 28484 */ 28485 28486 static int 28487 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28488 { 28489 struct sd_lun *un; 28490 struct uscsi_cmd *com; 28491 struct cdrom_read mode2_struct; 28492 struct cdrom_read *mode2 = &mode2_struct; 28493 uchar_t cdb[CDB_GROUP5]; 28494 int nblocks; 28495 int rval; 28496 #ifdef _MULTI_DATAMODEL 28497 /* To support ILP32 applications in an LP64 world */ 28498 struct cdrom_read32 cdrom_read32; 28499 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28500 #endif /* _MULTI_DATAMODEL */ 28501 28502 if (data == NULL) { 28503 return (EINVAL); 28504 } 28505 28506 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28507 (un->un_state == SD_STATE_OFFLINE)) { 28508 return (ENXIO); 28509 } 28510 28511 #ifdef _MULTI_DATAMODEL 28512 switch (ddi_model_convert_from(flag & FMODELS)) { 28513 case DDI_MODEL_ILP32: 28514 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28515 return (EFAULT); 28516 } 28517 /* Convert the ILP32 uscsi data from the application to LP64 */ 28518 cdrom_read32tocdrom_read(cdrd32, mode2); 28519 break; 28520 case DDI_MODEL_NONE: 28521 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28522 return (EFAULT); 28523 } 28524 break; 28525 } 28526 28527 #else /* ! _MULTI_DATAMODEL */ 28528 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28529 return (EFAULT); 28530 } 28531 #endif /* _MULTI_DATAMODEL */ 28532 28533 bzero(cdb, sizeof (cdb)); 28534 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28535 /* Read command supported by 1st generation atapi drives */ 28536 cdb[0] = SCMD_READ_CDD4; 28537 } else { 28538 /* Universal CD Access Command */ 28539 cdb[0] = SCMD_READ_CD; 28540 } 28541 28542 /* 28543 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28544 */ 28545 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28546 28547 /* set the start address */ 28548 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28549 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28550 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28551 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28552 28553 /* set the transfer length */ 28554 nblocks = mode2->cdread_buflen / 2336; 28555 cdb[6] = (uchar_t)(nblocks >> 16); 28556 cdb[7] = (uchar_t)(nblocks >> 8); 28557 cdb[8] = (uchar_t)nblocks; 28558 28559 /* set the filter bits */ 28560 cdb[9] = CDROM_READ_CD_USERDATA; 28561 28562 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28563 com->uscsi_cdb = (caddr_t)cdb; 28564 com->uscsi_cdblen = sizeof (cdb); 28565 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28566 com->uscsi_buflen = mode2->cdread_buflen; 28567 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28568 28569 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28570 SD_PATH_STANDARD); 28571 kmem_free(com, sizeof (*com)); 28572 return (rval); 28573 } 28574 28575 28576 /* 28577 * Function: sr_read_mode2() 28578 * 28579 * Description: This routine is the driver entry point for handling CD-ROM 28580 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28581 * do not support the READ CD (0xBE) command. 28582 * 28583 * Arguments: dev - the device 'dev_t' 28584 * data - pointer to user provided cd read structure specifying 28585 * the lba buffer address and length. 28586 * flag - this argument is a pass through to ddi_copyxxx() 28587 * directly from the mode argument of ioctl(). 28588 * 28589 * Return Code: the code returned by sd_send_scsi_cmd() 28590 * EFAULT if ddi_copyxxx() fails 28591 * ENXIO if fail ddi_get_soft_state 28592 * EINVAL if data pointer is NULL 28593 * EIO if fail to reset block size 28594 * EAGAIN if commands are in progress in the driver 28595 */ 28596 28597 static int 28598 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28599 { 28600 struct sd_lun *un; 28601 struct cdrom_read mode2_struct; 28602 struct cdrom_read *mode2 = &mode2_struct; 28603 int rval; 28604 uint32_t restore_blksize; 28605 struct uscsi_cmd *com; 28606 uchar_t cdb[CDB_GROUP0]; 28607 int nblocks; 28608 28609 #ifdef _MULTI_DATAMODEL 28610 /* To support ILP32 applications in an LP64 world */ 28611 struct cdrom_read32 cdrom_read32; 28612 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28613 #endif /* _MULTI_DATAMODEL */ 28614 28615 if (data == NULL) { 28616 return (EINVAL); 28617 } 28618 28619 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28620 (un->un_state == SD_STATE_OFFLINE)) { 28621 return (ENXIO); 28622 } 28623 28624 /* 28625 * Because this routine will update the device and driver block size 28626 * being used we want to make sure there are no commands in progress. 28627 * If commands are in progress the user will have to try again. 28628 * 28629 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28630 * in sdioctl to protect commands from sdioctl through to the top of 28631 * sd_uscsi_strategy. See sdioctl for details. 28632 */ 28633 mutex_enter(SD_MUTEX(un)); 28634 if (un->un_ncmds_in_driver != 1) { 28635 mutex_exit(SD_MUTEX(un)); 28636 return (EAGAIN); 28637 } 28638 mutex_exit(SD_MUTEX(un)); 28639 28640 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28641 "sd_read_mode2: entry: un:0x%p\n", un); 28642 28643 #ifdef _MULTI_DATAMODEL 28644 switch (ddi_model_convert_from(flag & FMODELS)) { 28645 case DDI_MODEL_ILP32: 28646 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28647 return (EFAULT); 28648 } 28649 /* Convert the ILP32 uscsi data from the application to LP64 */ 28650 cdrom_read32tocdrom_read(cdrd32, mode2); 28651 break; 28652 case DDI_MODEL_NONE: 28653 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28654 return (EFAULT); 28655 } 28656 break; 28657 } 28658 #else /* ! _MULTI_DATAMODEL */ 28659 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28660 return (EFAULT); 28661 } 28662 #endif /* _MULTI_DATAMODEL */ 28663 28664 /* Store the current target block size for restoration later */ 28665 restore_blksize = un->un_tgt_blocksize; 28666 28667 /* Change the device and soft state target block size to 2336 */ 28668 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28669 rval = EIO; 28670 goto done; 28671 } 28672 28673 28674 bzero(cdb, sizeof (cdb)); 28675 28676 /* set READ operation */ 28677 cdb[0] = SCMD_READ; 28678 28679 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28680 mode2->cdread_lba >>= 2; 28681 28682 /* set the start address */ 28683 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28684 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28685 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28686 28687 /* set the transfer length */ 28688 nblocks = mode2->cdread_buflen / 2336; 28689 cdb[4] = (uchar_t)nblocks & 0xFF; 28690 28691 /* build command */ 28692 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28693 com->uscsi_cdb = (caddr_t)cdb; 28694 com->uscsi_cdblen = sizeof (cdb); 28695 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28696 com->uscsi_buflen = mode2->cdread_buflen; 28697 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28698 28699 /* 28700 * Issue SCSI command with user space address for read buffer. 28701 * 28702 * This sends the command through main channel in the driver. 28703 * 28704 * Since this is accessed via an IOCTL call, we go through the 28705 * standard path, so that if the device was powered down, then 28706 * it would be 'awakened' to handle the command. 28707 */ 28708 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28709 SD_PATH_STANDARD); 28710 28711 kmem_free(com, sizeof (*com)); 28712 28713 /* Restore the device and soft state target block size */ 28714 if (sr_sector_mode(dev, restore_blksize) != 0) { 28715 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28716 "can't do switch back to mode 1\n"); 28717 /* 28718 * If sd_send_scsi_READ succeeded we still need to report 28719 * an error because we failed to reset the block size 28720 */ 28721 if (rval == 0) { 28722 rval = EIO; 28723 } 28724 } 28725 28726 done: 28727 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28728 "sd_read_mode2: exit: un:0x%p\n", un); 28729 28730 return (rval); 28731 } 28732 28733 28734 /* 28735 * Function: sr_sector_mode() 28736 * 28737 * Description: This utility function is used by sr_read_mode2 to set the target 28738 * block size based on the user specified size. This is a legacy 28739 * implementation based upon a vendor specific mode page 28740 * 28741 * Arguments: dev - the device 'dev_t' 28742 * data - flag indicating if block size is being set to 2336 or 28743 * 512. 28744 * 28745 * Return Code: the code returned by sd_send_scsi_cmd() 28746 * EFAULT if ddi_copyxxx() fails 28747 * ENXIO if fail ddi_get_soft_state 28748 * EINVAL if data pointer is NULL 28749 */ 28750 28751 static int 28752 sr_sector_mode(dev_t dev, uint32_t blksize) 28753 { 28754 struct sd_lun *un; 28755 uchar_t *sense; 28756 uchar_t *select; 28757 int rval; 28758 28759 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28760 (un->un_state == SD_STATE_OFFLINE)) { 28761 return (ENXIO); 28762 } 28763 28764 sense = kmem_zalloc(20, KM_SLEEP); 28765 28766 /* Note: This is a vendor specific mode page (0x81) */ 28767 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28768 SD_PATH_STANDARD)) != 0) { 28769 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28770 "sr_sector_mode: Mode Sense failed\n"); 28771 kmem_free(sense, 20); 28772 return (rval); 28773 } 28774 select = kmem_zalloc(20, KM_SLEEP); 28775 select[3] = 0x08; 28776 select[10] = ((blksize >> 8) & 0xff); 28777 select[11] = (blksize & 0xff); 28778 select[12] = 0x01; 28779 select[13] = 0x06; 28780 select[14] = sense[14]; 28781 select[15] = sense[15]; 28782 if (blksize == SD_MODE2_BLKSIZE) { 28783 select[14] |= 0x01; 28784 } 28785 28786 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28787 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28788 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28789 "sr_sector_mode: Mode Select failed\n"); 28790 } else { 28791 /* 28792 * Only update the softstate block size if we successfully 28793 * changed the device block mode. 28794 */ 28795 mutex_enter(SD_MUTEX(un)); 28796 sd_update_block_info(un, blksize, 0); 28797 mutex_exit(SD_MUTEX(un)); 28798 } 28799 kmem_free(sense, 20); 28800 kmem_free(select, 20); 28801 return (rval); 28802 } 28803 28804 28805 /* 28806 * Function: sr_read_cdda() 28807 * 28808 * Description: This routine is the driver entry point for handling CD-ROM 28809 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28810 * the target supports CDDA these requests are handled via a vendor 28811 * specific command (0xD8) If the target does not support CDDA 28812 * these requests are handled via the READ CD command (0xBE). 28813 * 28814 * Arguments: dev - the device 'dev_t' 28815 * data - pointer to user provided CD-DA structure specifying 28816 * the track starting address, transfer length, and 28817 * subcode options. 28818 * flag - this argument is a pass through to ddi_copyxxx() 28819 * directly from the mode argument of ioctl(). 28820 * 28821 * Return Code: the code returned by sd_send_scsi_cmd() 28822 * EFAULT if ddi_copyxxx() fails 28823 * ENXIO if fail ddi_get_soft_state 28824 * EINVAL if invalid arguments are provided 28825 * ENOTTY 28826 */ 28827 28828 static int 28829 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28830 { 28831 struct sd_lun *un; 28832 struct uscsi_cmd *com; 28833 struct cdrom_cdda *cdda; 28834 int rval; 28835 size_t buflen; 28836 char cdb[CDB_GROUP5]; 28837 28838 #ifdef _MULTI_DATAMODEL 28839 /* To support ILP32 applications in an LP64 world */ 28840 struct cdrom_cdda32 cdrom_cdda32; 28841 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28842 #endif /* _MULTI_DATAMODEL */ 28843 28844 if (data == NULL) { 28845 return (EINVAL); 28846 } 28847 28848 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28849 return (ENXIO); 28850 } 28851 28852 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28853 28854 #ifdef _MULTI_DATAMODEL 28855 switch (ddi_model_convert_from(flag & FMODELS)) { 28856 case DDI_MODEL_ILP32: 28857 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28858 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28859 "sr_read_cdda: ddi_copyin Failed\n"); 28860 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28861 return (EFAULT); 28862 } 28863 /* Convert the ILP32 uscsi data from the application to LP64 */ 28864 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28865 break; 28866 case DDI_MODEL_NONE: 28867 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28868 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28869 "sr_read_cdda: ddi_copyin Failed\n"); 28870 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28871 return (EFAULT); 28872 } 28873 break; 28874 } 28875 #else /* ! _MULTI_DATAMODEL */ 28876 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28878 "sr_read_cdda: ddi_copyin Failed\n"); 28879 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28880 return (EFAULT); 28881 } 28882 #endif /* _MULTI_DATAMODEL */ 28883 28884 /* 28885 * Since MMC-2 expects max 3 bytes for length, check if the 28886 * length input is greater than 3 bytes 28887 */ 28888 if ((cdda->cdda_length & 0xFF000000) != 0) { 28889 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28890 "cdrom transfer length too large: %d (limit %d)\n", 28891 cdda->cdda_length, 0xFFFFFF); 28892 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28893 return (EINVAL); 28894 } 28895 28896 switch (cdda->cdda_subcode) { 28897 case CDROM_DA_NO_SUBCODE: 28898 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28899 break; 28900 case CDROM_DA_SUBQ: 28901 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28902 break; 28903 case CDROM_DA_ALL_SUBCODE: 28904 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28905 break; 28906 case CDROM_DA_SUBCODE_ONLY: 28907 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28908 break; 28909 default: 28910 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28911 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28912 cdda->cdda_subcode); 28913 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28914 return (EINVAL); 28915 } 28916 28917 /* Build and send the command */ 28918 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28919 bzero(cdb, CDB_GROUP5); 28920 28921 if (un->un_f_cfg_cdda == TRUE) { 28922 cdb[0] = (char)SCMD_READ_CD; 28923 cdb[1] = 0x04; 28924 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28925 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28926 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28927 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28928 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28929 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28930 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28931 cdb[9] = 0x10; 28932 switch (cdda->cdda_subcode) { 28933 case CDROM_DA_NO_SUBCODE : 28934 cdb[10] = 0x0; 28935 break; 28936 case CDROM_DA_SUBQ : 28937 cdb[10] = 0x2; 28938 break; 28939 case CDROM_DA_ALL_SUBCODE : 28940 cdb[10] = 0x1; 28941 break; 28942 case CDROM_DA_SUBCODE_ONLY : 28943 /* FALLTHROUGH */ 28944 default : 28945 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28946 kmem_free(com, sizeof (*com)); 28947 return (ENOTTY); 28948 } 28949 } else { 28950 cdb[0] = (char)SCMD_READ_CDDA; 28951 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28952 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28953 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28954 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28955 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28956 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28957 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28958 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28959 cdb[10] = cdda->cdda_subcode; 28960 } 28961 28962 com->uscsi_cdb = cdb; 28963 com->uscsi_cdblen = CDB_GROUP5; 28964 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28965 com->uscsi_buflen = buflen; 28966 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28967 28968 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28969 SD_PATH_STANDARD); 28970 28971 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28972 kmem_free(com, sizeof (*com)); 28973 return (rval); 28974 } 28975 28976 28977 /* 28978 * Function: sr_read_cdxa() 28979 * 28980 * Description: This routine is the driver entry point for handling CD-ROM 28981 * ioctl requests to return CD-XA (Extended Architecture) data. 28982 * (CDROMCDXA). 28983 * 28984 * Arguments: dev - the device 'dev_t' 28985 * data - pointer to user provided CD-XA structure specifying 28986 * the data starting address, transfer length, and format 28987 * flag - this argument is a pass through to ddi_copyxxx() 28988 * directly from the mode argument of ioctl(). 28989 * 28990 * Return Code: the code returned by sd_send_scsi_cmd() 28991 * EFAULT if ddi_copyxxx() fails 28992 * ENXIO if fail ddi_get_soft_state 28993 * EINVAL if data pointer is NULL 28994 */ 28995 28996 static int 28997 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28998 { 28999 struct sd_lun *un; 29000 struct uscsi_cmd *com; 29001 struct cdrom_cdxa *cdxa; 29002 int rval; 29003 size_t buflen; 29004 char cdb[CDB_GROUP5]; 29005 uchar_t read_flags; 29006 29007 #ifdef _MULTI_DATAMODEL 29008 /* To support ILP32 applications in an LP64 world */ 29009 struct cdrom_cdxa32 cdrom_cdxa32; 29010 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 29011 #endif /* _MULTI_DATAMODEL */ 29012 29013 if (data == NULL) { 29014 return (EINVAL); 29015 } 29016 29017 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29018 return (ENXIO); 29019 } 29020 29021 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 29022 29023 #ifdef _MULTI_DATAMODEL 29024 switch (ddi_model_convert_from(flag & FMODELS)) { 29025 case DDI_MODEL_ILP32: 29026 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 29027 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29028 return (EFAULT); 29029 } 29030 /* 29031 * Convert the ILP32 uscsi data from the 29032 * application to LP64 for internal use. 29033 */ 29034 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29035 break; 29036 case DDI_MODEL_NONE: 29037 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29038 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29039 return (EFAULT); 29040 } 29041 break; 29042 } 29043 #else /* ! _MULTI_DATAMODEL */ 29044 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29045 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29046 return (EFAULT); 29047 } 29048 #endif /* _MULTI_DATAMODEL */ 29049 29050 /* 29051 * Since MMC-2 expects max 3 bytes for length, check if the 29052 * length input is greater than 3 bytes 29053 */ 29054 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29055 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29056 "cdrom transfer length too large: %d (limit %d)\n", 29057 cdxa->cdxa_length, 0xFFFFFF); 29058 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29059 return (EINVAL); 29060 } 29061 29062 switch (cdxa->cdxa_format) { 29063 case CDROM_XA_DATA: 29064 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29065 read_flags = 0x10; 29066 break; 29067 case CDROM_XA_SECTOR_DATA: 29068 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29069 read_flags = 0xf8; 29070 break; 29071 case CDROM_XA_DATA_W_ERROR: 29072 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29073 read_flags = 0xfc; 29074 break; 29075 default: 29076 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29077 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29078 cdxa->cdxa_format); 29079 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29080 return (EINVAL); 29081 } 29082 29083 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29084 bzero(cdb, CDB_GROUP5); 29085 if (un->un_f_mmc_cap == TRUE) { 29086 cdb[0] = (char)SCMD_READ_CD; 29087 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29088 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29089 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29090 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29091 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29092 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29093 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29094 cdb[9] = (char)read_flags; 29095 } else { 29096 /* 29097 * Note: A vendor specific command (0xDB) is being used her to 29098 * request a read of all subcodes. 29099 */ 29100 cdb[0] = (char)SCMD_READ_CDXA; 29101 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29102 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29103 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29104 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29105 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29106 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29107 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29108 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29109 cdb[10] = cdxa->cdxa_format; 29110 } 29111 com->uscsi_cdb = cdb; 29112 com->uscsi_cdblen = CDB_GROUP5; 29113 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29114 com->uscsi_buflen = buflen; 29115 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29116 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 29117 SD_PATH_STANDARD); 29118 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29119 kmem_free(com, sizeof (*com)); 29120 return (rval); 29121 } 29122 29123 29124 /* 29125 * Function: sr_eject() 29126 * 29127 * Description: This routine is the driver entry point for handling CD-ROM 29128 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29129 * 29130 * Arguments: dev - the device 'dev_t' 29131 * 29132 * Return Code: the code returned by sd_send_scsi_cmd() 29133 */ 29134 29135 static int 29136 sr_eject(dev_t dev) 29137 { 29138 struct sd_lun *un; 29139 int rval; 29140 29141 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29142 (un->un_state == SD_STATE_OFFLINE)) { 29143 return (ENXIO); 29144 } 29145 29146 /* 29147 * To prevent race conditions with the eject 29148 * command, keep track of an eject command as 29149 * it progresses. If we are already handling 29150 * an eject command in the driver for the given 29151 * unit and another request to eject is received 29152 * immediately return EAGAIN so we don't lose 29153 * the command if the current eject command fails. 29154 */ 29155 mutex_enter(SD_MUTEX(un)); 29156 if (un->un_f_ejecting == TRUE) { 29157 mutex_exit(SD_MUTEX(un)); 29158 return (EAGAIN); 29159 } 29160 un->un_f_ejecting = TRUE; 29161 mutex_exit(SD_MUTEX(un)); 29162 29163 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29164 SD_PATH_STANDARD)) != 0) { 29165 mutex_enter(SD_MUTEX(un)); 29166 un->un_f_ejecting = FALSE; 29167 mutex_exit(SD_MUTEX(un)); 29168 return (rval); 29169 } 29170 29171 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29172 SD_PATH_STANDARD); 29173 29174 if (rval == 0) { 29175 mutex_enter(SD_MUTEX(un)); 29176 sr_ejected(un); 29177 un->un_mediastate = DKIO_EJECTED; 29178 un->un_f_ejecting = FALSE; 29179 cv_broadcast(&un->un_state_cv); 29180 mutex_exit(SD_MUTEX(un)); 29181 } else { 29182 mutex_enter(SD_MUTEX(un)); 29183 un->un_f_ejecting = FALSE; 29184 mutex_exit(SD_MUTEX(un)); 29185 } 29186 return (rval); 29187 } 29188 29189 29190 /* 29191 * Function: sr_ejected() 29192 * 29193 * Description: This routine updates the soft state structure to invalidate the 29194 * geometry information after the media has been ejected or a 29195 * media eject has been detected. 29196 * 29197 * Arguments: un - driver soft state (unit) structure 29198 */ 29199 29200 static void 29201 sr_ejected(struct sd_lun *un) 29202 { 29203 struct sd_errstats *stp; 29204 29205 ASSERT(un != NULL); 29206 ASSERT(mutex_owned(SD_MUTEX(un))); 29207 29208 un->un_f_blockcount_is_valid = FALSE; 29209 un->un_f_tgt_blocksize_is_valid = FALSE; 29210 un->un_f_geometry_is_valid = FALSE; 29211 29212 if (un->un_errstats != NULL) { 29213 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29214 stp->sd_capacity.value.ui64 = 0; 29215 } 29216 } 29217 29218 29219 /* 29220 * Function: sr_check_wp() 29221 * 29222 * Description: This routine checks the write protection of a removable 29223 * media disk and hotpluggable devices via the write protect bit of 29224 * the Mode Page Header device specific field. Some devices choke 29225 * on unsupported mode page. In order to workaround this issue, 29226 * this routine has been implemented to use 0x3f mode page(request 29227 * for all pages) for all device types. 29228 * 29229 * Arguments: dev - the device 'dev_t' 29230 * 29231 * Return Code: int indicating if the device is write protected (1) or not (0) 29232 * 29233 * Context: Kernel thread. 29234 * 29235 */ 29236 29237 static int 29238 sr_check_wp(dev_t dev) 29239 { 29240 struct sd_lun *un; 29241 uchar_t device_specific; 29242 uchar_t *sense; 29243 int hdrlen; 29244 int rval = FALSE; 29245 29246 /* 29247 * Note: The return codes for this routine should be reworked to 29248 * properly handle the case of a NULL softstate. 29249 */ 29250 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29251 return (FALSE); 29252 } 29253 29254 if (un->un_f_cfg_is_atapi == TRUE) { 29255 /* 29256 * The mode page contents are not required; set the allocation 29257 * length for the mode page header only 29258 */ 29259 hdrlen = MODE_HEADER_LENGTH_GRP2; 29260 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29261 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29262 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29263 goto err_exit; 29264 device_specific = 29265 ((struct mode_header_grp2 *)sense)->device_specific; 29266 } else { 29267 hdrlen = MODE_HEADER_LENGTH; 29268 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29269 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29270 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29271 goto err_exit; 29272 device_specific = 29273 ((struct mode_header *)sense)->device_specific; 29274 } 29275 29276 /* 29277 * Write protect mode sense failed; not all disks 29278 * understand this query. Return FALSE assuming that 29279 * these devices are not writable. 29280 */ 29281 if (device_specific & WRITE_PROTECT) { 29282 rval = TRUE; 29283 } 29284 29285 err_exit: 29286 kmem_free(sense, hdrlen); 29287 return (rval); 29288 } 29289 29290 /* 29291 * Function: sr_volume_ctrl() 29292 * 29293 * Description: This routine is the driver entry point for handling CD-ROM 29294 * audio output volume ioctl requests. (CDROMVOLCTRL) 29295 * 29296 * Arguments: dev - the device 'dev_t' 29297 * data - pointer to user audio volume control structure 29298 * flag - this argument is a pass through to ddi_copyxxx() 29299 * directly from the mode argument of ioctl(). 29300 * 29301 * Return Code: the code returned by sd_send_scsi_cmd() 29302 * EFAULT if ddi_copyxxx() fails 29303 * ENXIO if fail ddi_get_soft_state 29304 * EINVAL if data pointer is NULL 29305 * 29306 */ 29307 29308 static int 29309 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29310 { 29311 struct sd_lun *un; 29312 struct cdrom_volctrl volume; 29313 struct cdrom_volctrl *vol = &volume; 29314 uchar_t *sense_page; 29315 uchar_t *select_page; 29316 uchar_t *sense; 29317 uchar_t *select; 29318 int sense_buflen; 29319 int select_buflen; 29320 int rval; 29321 29322 if (data == NULL) { 29323 return (EINVAL); 29324 } 29325 29326 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29327 (un->un_state == SD_STATE_OFFLINE)) { 29328 return (ENXIO); 29329 } 29330 29331 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29332 return (EFAULT); 29333 } 29334 29335 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29336 struct mode_header_grp2 *sense_mhp; 29337 struct mode_header_grp2 *select_mhp; 29338 int bd_len; 29339 29340 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29341 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29342 MODEPAGE_AUDIO_CTRL_LEN; 29343 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29344 select = kmem_zalloc(select_buflen, KM_SLEEP); 29345 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29346 sense_buflen, MODEPAGE_AUDIO_CTRL, 29347 SD_PATH_STANDARD)) != 0) { 29348 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29349 "sr_volume_ctrl: Mode Sense Failed\n"); 29350 kmem_free(sense, sense_buflen); 29351 kmem_free(select, select_buflen); 29352 return (rval); 29353 } 29354 sense_mhp = (struct mode_header_grp2 *)sense; 29355 select_mhp = (struct mode_header_grp2 *)select; 29356 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29357 sense_mhp->bdesc_length_lo; 29358 if (bd_len > MODE_BLK_DESC_LENGTH) { 29359 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29360 "sr_volume_ctrl: Mode Sense returned invalid " 29361 "block descriptor length\n"); 29362 kmem_free(sense, sense_buflen); 29363 kmem_free(select, select_buflen); 29364 return (EIO); 29365 } 29366 sense_page = (uchar_t *) 29367 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29368 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29369 select_mhp->length_msb = 0; 29370 select_mhp->length_lsb = 0; 29371 select_mhp->bdesc_length_hi = 0; 29372 select_mhp->bdesc_length_lo = 0; 29373 } else { 29374 struct mode_header *sense_mhp, *select_mhp; 29375 29376 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29377 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29378 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29379 select = kmem_zalloc(select_buflen, KM_SLEEP); 29380 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29381 sense_buflen, MODEPAGE_AUDIO_CTRL, 29382 SD_PATH_STANDARD)) != 0) { 29383 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29384 "sr_volume_ctrl: Mode Sense Failed\n"); 29385 kmem_free(sense, sense_buflen); 29386 kmem_free(select, select_buflen); 29387 return (rval); 29388 } 29389 sense_mhp = (struct mode_header *)sense; 29390 select_mhp = (struct mode_header *)select; 29391 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29392 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29393 "sr_volume_ctrl: Mode Sense returned invalid " 29394 "block descriptor length\n"); 29395 kmem_free(sense, sense_buflen); 29396 kmem_free(select, select_buflen); 29397 return (EIO); 29398 } 29399 sense_page = (uchar_t *) 29400 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29401 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29402 select_mhp->length = 0; 29403 select_mhp->bdesc_length = 0; 29404 } 29405 /* 29406 * Note: An audio control data structure could be created and overlayed 29407 * on the following in place of the array indexing method implemented. 29408 */ 29409 29410 /* Build the select data for the user volume data */ 29411 select_page[0] = MODEPAGE_AUDIO_CTRL; 29412 select_page[1] = 0xE; 29413 /* Set the immediate bit */ 29414 select_page[2] = 0x04; 29415 /* Zero out reserved fields */ 29416 select_page[3] = 0x00; 29417 select_page[4] = 0x00; 29418 /* Return sense data for fields not to be modified */ 29419 select_page[5] = sense_page[5]; 29420 select_page[6] = sense_page[6]; 29421 select_page[7] = sense_page[7]; 29422 /* Set the user specified volume levels for channel 0 and 1 */ 29423 select_page[8] = 0x01; 29424 select_page[9] = vol->channel0; 29425 select_page[10] = 0x02; 29426 select_page[11] = vol->channel1; 29427 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29428 select_page[12] = sense_page[12]; 29429 select_page[13] = sense_page[13]; 29430 select_page[14] = sense_page[14]; 29431 select_page[15] = sense_page[15]; 29432 29433 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29434 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29435 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29436 } else { 29437 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29438 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29439 } 29440 29441 kmem_free(sense, sense_buflen); 29442 kmem_free(select, select_buflen); 29443 return (rval); 29444 } 29445 29446 29447 /* 29448 * Function: sr_read_sony_session_offset() 29449 * 29450 * Description: This routine is the driver entry point for handling CD-ROM 29451 * ioctl requests for session offset information. (CDROMREADOFFSET) 29452 * The address of the first track in the last session of a 29453 * multi-session CD-ROM is returned 29454 * 29455 * Note: This routine uses a vendor specific key value in the 29456 * command control field without implementing any vendor check here 29457 * or in the ioctl routine. 29458 * 29459 * Arguments: dev - the device 'dev_t' 29460 * data - pointer to an int to hold the requested address 29461 * flag - this argument is a pass through to ddi_copyxxx() 29462 * directly from the mode argument of ioctl(). 29463 * 29464 * Return Code: the code returned by sd_send_scsi_cmd() 29465 * EFAULT if ddi_copyxxx() fails 29466 * ENXIO if fail ddi_get_soft_state 29467 * EINVAL if data pointer is NULL 29468 */ 29469 29470 static int 29471 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29472 { 29473 struct sd_lun *un; 29474 struct uscsi_cmd *com; 29475 caddr_t buffer; 29476 char cdb[CDB_GROUP1]; 29477 int session_offset = 0; 29478 int rval; 29479 29480 if (data == NULL) { 29481 return (EINVAL); 29482 } 29483 29484 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29485 (un->un_state == SD_STATE_OFFLINE)) { 29486 return (ENXIO); 29487 } 29488 29489 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29490 bzero(cdb, CDB_GROUP1); 29491 cdb[0] = SCMD_READ_TOC; 29492 /* 29493 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29494 * (4 byte TOC response header + 8 byte response data) 29495 */ 29496 cdb[8] = SONY_SESSION_OFFSET_LEN; 29497 /* Byte 9 is the control byte. A vendor specific value is used */ 29498 cdb[9] = SONY_SESSION_OFFSET_KEY; 29499 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29500 com->uscsi_cdb = cdb; 29501 com->uscsi_cdblen = CDB_GROUP1; 29502 com->uscsi_bufaddr = buffer; 29503 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29504 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29505 29506 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 29507 SD_PATH_STANDARD); 29508 if (rval != 0) { 29509 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29510 kmem_free(com, sizeof (*com)); 29511 return (rval); 29512 } 29513 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29514 session_offset = 29515 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29516 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29517 /* 29518 * Offset returned offset in current lbasize block's. Convert to 29519 * 2k block's to return to the user 29520 */ 29521 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29522 session_offset >>= 2; 29523 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29524 session_offset >>= 1; 29525 } 29526 } 29527 29528 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29529 rval = EFAULT; 29530 } 29531 29532 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29533 kmem_free(com, sizeof (*com)); 29534 return (rval); 29535 } 29536 29537 29538 /* 29539 * Function: sd_wm_cache_constructor() 29540 * 29541 * Description: Cache Constructor for the wmap cache for the read/modify/write 29542 * devices. 29543 * 29544 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29545 * un - sd_lun structure for the device. 29546 * flag - the km flags passed to constructor 29547 * 29548 * Return Code: 0 on success. 29549 * -1 on failure. 29550 */ 29551 29552 /*ARGSUSED*/ 29553 static int 29554 sd_wm_cache_constructor(void *wm, void *un, int flags) 29555 { 29556 bzero(wm, sizeof (struct sd_w_map)); 29557 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29558 return (0); 29559 } 29560 29561 29562 /* 29563 * Function: sd_wm_cache_destructor() 29564 * 29565 * Description: Cache destructor for the wmap cache for the read/modify/write 29566 * devices. 29567 * 29568 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29569 * un - sd_lun structure for the device. 29570 */ 29571 /*ARGSUSED*/ 29572 static void 29573 sd_wm_cache_destructor(void *wm, void *un) 29574 { 29575 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29576 } 29577 29578 29579 /* 29580 * Function: sd_range_lock() 29581 * 29582 * Description: Lock the range of blocks specified as parameter to ensure 29583 * that read, modify write is atomic and no other i/o writes 29584 * to the same location. The range is specified in terms 29585 * of start and end blocks. Block numbers are the actual 29586 * media block numbers and not system. 29587 * 29588 * Arguments: un - sd_lun structure for the device. 29589 * startb - The starting block number 29590 * endb - The end block number 29591 * typ - type of i/o - simple/read_modify_write 29592 * 29593 * Return Code: wm - pointer to the wmap structure. 29594 * 29595 * Context: This routine can sleep. 29596 */ 29597 29598 static struct sd_w_map * 29599 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29600 { 29601 struct sd_w_map *wmp = NULL; 29602 struct sd_w_map *sl_wmp = NULL; 29603 struct sd_w_map *tmp_wmp; 29604 wm_state state = SD_WM_CHK_LIST; 29605 29606 29607 ASSERT(un != NULL); 29608 ASSERT(!mutex_owned(SD_MUTEX(un))); 29609 29610 mutex_enter(SD_MUTEX(un)); 29611 29612 while (state != SD_WM_DONE) { 29613 29614 switch (state) { 29615 case SD_WM_CHK_LIST: 29616 /* 29617 * This is the starting state. Check the wmap list 29618 * to see if the range is currently available. 29619 */ 29620 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29621 /* 29622 * If this is a simple write and no rmw 29623 * i/o is pending then try to lock the 29624 * range as the range should be available. 29625 */ 29626 state = SD_WM_LOCK_RANGE; 29627 } else { 29628 tmp_wmp = sd_get_range(un, startb, endb); 29629 if (tmp_wmp != NULL) { 29630 if ((wmp != NULL) && ONLIST(un, wmp)) { 29631 /* 29632 * Should not keep onlist wmps 29633 * while waiting this macro 29634 * will also do wmp = NULL; 29635 */ 29636 FREE_ONLIST_WMAP(un, wmp); 29637 } 29638 /* 29639 * sl_wmp is the wmap on which wait 29640 * is done, since the tmp_wmp points 29641 * to the inuse wmap, set sl_wmp to 29642 * tmp_wmp and change the state to sleep 29643 */ 29644 sl_wmp = tmp_wmp; 29645 state = SD_WM_WAIT_MAP; 29646 } else { 29647 state = SD_WM_LOCK_RANGE; 29648 } 29649 29650 } 29651 break; 29652 29653 case SD_WM_LOCK_RANGE: 29654 ASSERT(un->un_wm_cache); 29655 /* 29656 * The range need to be locked, try to get a wmap. 29657 * First attempt it with NO_SLEEP, want to avoid a sleep 29658 * if possible as we will have to release the sd mutex 29659 * if we have to sleep. 29660 */ 29661 if (wmp == NULL) 29662 wmp = kmem_cache_alloc(un->un_wm_cache, 29663 KM_NOSLEEP); 29664 if (wmp == NULL) { 29665 mutex_exit(SD_MUTEX(un)); 29666 _NOTE(DATA_READABLE_WITHOUT_LOCK 29667 (sd_lun::un_wm_cache)) 29668 wmp = kmem_cache_alloc(un->un_wm_cache, 29669 KM_SLEEP); 29670 mutex_enter(SD_MUTEX(un)); 29671 /* 29672 * we released the mutex so recheck and go to 29673 * check list state. 29674 */ 29675 state = SD_WM_CHK_LIST; 29676 } else { 29677 /* 29678 * We exit out of state machine since we 29679 * have the wmap. Do the housekeeping first. 29680 * place the wmap on the wmap list if it is not 29681 * on it already and then set the state to done. 29682 */ 29683 wmp->wm_start = startb; 29684 wmp->wm_end = endb; 29685 wmp->wm_flags = typ | SD_WM_BUSY; 29686 if (typ & SD_WTYPE_RMW) { 29687 un->un_rmw_count++; 29688 } 29689 /* 29690 * If not already on the list then link 29691 */ 29692 if (!ONLIST(un, wmp)) { 29693 wmp->wm_next = un->un_wm; 29694 wmp->wm_prev = NULL; 29695 if (wmp->wm_next) 29696 wmp->wm_next->wm_prev = wmp; 29697 un->un_wm = wmp; 29698 } 29699 state = SD_WM_DONE; 29700 } 29701 break; 29702 29703 case SD_WM_WAIT_MAP: 29704 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29705 /* 29706 * Wait is done on sl_wmp, which is set in the 29707 * check_list state. 29708 */ 29709 sl_wmp->wm_wanted_count++; 29710 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29711 sl_wmp->wm_wanted_count--; 29712 /* 29713 * We can reuse the memory from the completed sl_wmp 29714 * lock range for our new lock, but only if noone is 29715 * waiting for it. 29716 */ 29717 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29718 if (sl_wmp->wm_wanted_count == 0) { 29719 if (wmp != NULL) 29720 CHK_N_FREEWMP(un, wmp); 29721 wmp = sl_wmp; 29722 } 29723 sl_wmp = NULL; 29724 /* 29725 * After waking up, need to recheck for availability of 29726 * range. 29727 */ 29728 state = SD_WM_CHK_LIST; 29729 break; 29730 29731 default: 29732 panic("sd_range_lock: " 29733 "Unknown state %d in sd_range_lock", state); 29734 /*NOTREACHED*/ 29735 } /* switch(state) */ 29736 29737 } /* while(state != SD_WM_DONE) */ 29738 29739 mutex_exit(SD_MUTEX(un)); 29740 29741 ASSERT(wmp != NULL); 29742 29743 return (wmp); 29744 } 29745 29746 29747 /* 29748 * Function: sd_get_range() 29749 * 29750 * Description: Find if there any overlapping I/O to this one 29751 * Returns the write-map of 1st such I/O, NULL otherwise. 29752 * 29753 * Arguments: un - sd_lun structure for the device. 29754 * startb - The starting block number 29755 * endb - The end block number 29756 * 29757 * Return Code: wm - pointer to the wmap structure. 29758 */ 29759 29760 static struct sd_w_map * 29761 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29762 { 29763 struct sd_w_map *wmp; 29764 29765 ASSERT(un != NULL); 29766 29767 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29768 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29769 continue; 29770 } 29771 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29772 break; 29773 } 29774 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29775 break; 29776 } 29777 } 29778 29779 return (wmp); 29780 } 29781 29782 29783 /* 29784 * Function: sd_free_inlist_wmap() 29785 * 29786 * Description: Unlink and free a write map struct. 29787 * 29788 * Arguments: un - sd_lun structure for the device. 29789 * wmp - sd_w_map which needs to be unlinked. 29790 */ 29791 29792 static void 29793 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29794 { 29795 ASSERT(un != NULL); 29796 29797 if (un->un_wm == wmp) { 29798 un->un_wm = wmp->wm_next; 29799 } else { 29800 wmp->wm_prev->wm_next = wmp->wm_next; 29801 } 29802 29803 if (wmp->wm_next) { 29804 wmp->wm_next->wm_prev = wmp->wm_prev; 29805 } 29806 29807 wmp->wm_next = wmp->wm_prev = NULL; 29808 29809 kmem_cache_free(un->un_wm_cache, wmp); 29810 } 29811 29812 29813 /* 29814 * Function: sd_range_unlock() 29815 * 29816 * Description: Unlock the range locked by wm. 29817 * Free write map if nobody else is waiting on it. 29818 * 29819 * Arguments: un - sd_lun structure for the device. 29820 * wmp - sd_w_map which needs to be unlinked. 29821 */ 29822 29823 static void 29824 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29825 { 29826 ASSERT(un != NULL); 29827 ASSERT(wm != NULL); 29828 ASSERT(!mutex_owned(SD_MUTEX(un))); 29829 29830 mutex_enter(SD_MUTEX(un)); 29831 29832 if (wm->wm_flags & SD_WTYPE_RMW) { 29833 un->un_rmw_count--; 29834 } 29835 29836 if (wm->wm_wanted_count) { 29837 wm->wm_flags = 0; 29838 /* 29839 * Broadcast that the wmap is available now. 29840 */ 29841 cv_broadcast(&wm->wm_avail); 29842 } else { 29843 /* 29844 * If no one is waiting on the map, it should be free'ed. 29845 */ 29846 sd_free_inlist_wmap(un, wm); 29847 } 29848 29849 mutex_exit(SD_MUTEX(un)); 29850 } 29851 29852 29853 /* 29854 * Function: sd_read_modify_write_task 29855 * 29856 * Description: Called from a taskq thread to initiate the write phase of 29857 * a read-modify-write request. This is used for targets where 29858 * un->un_sys_blocksize != un->un_tgt_blocksize. 29859 * 29860 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29861 * 29862 * Context: Called under taskq thread context. 29863 */ 29864 29865 static void 29866 sd_read_modify_write_task(void *arg) 29867 { 29868 struct sd_mapblocksize_info *bsp; 29869 struct buf *bp; 29870 struct sd_xbuf *xp; 29871 struct sd_lun *un; 29872 29873 bp = arg; /* The bp is given in arg */ 29874 ASSERT(bp != NULL); 29875 29876 /* Get the pointer to the layer-private data struct */ 29877 xp = SD_GET_XBUF(bp); 29878 ASSERT(xp != NULL); 29879 bsp = xp->xb_private; 29880 ASSERT(bsp != NULL); 29881 29882 un = SD_GET_UN(bp); 29883 ASSERT(un != NULL); 29884 ASSERT(!mutex_owned(SD_MUTEX(un))); 29885 29886 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29887 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29888 29889 /* 29890 * This is the write phase of a read-modify-write request, called 29891 * under the context of a taskq thread in response to the completion 29892 * of the read portion of the rmw request completing under interrupt 29893 * context. The write request must be sent from here down the iostart 29894 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29895 * we use the layer index saved in the layer-private data area. 29896 */ 29897 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29898 29899 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29900 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29901 } 29902 29903 29904 /* 29905 * Function: sddump_do_read_of_rmw() 29906 * 29907 * Description: This routine will be called from sddump, If sddump is called 29908 * with an I/O which not aligned on device blocksize boundary 29909 * then the write has to be converted to read-modify-write. 29910 * Do the read part here in order to keep sddump simple. 29911 * Note - That the sd_mutex is held across the call to this 29912 * routine. 29913 * 29914 * Arguments: un - sd_lun 29915 * blkno - block number in terms of media block size. 29916 * nblk - number of blocks. 29917 * bpp - pointer to pointer to the buf structure. On return 29918 * from this function, *bpp points to the valid buffer 29919 * to which the write has to be done. 29920 * 29921 * Return Code: 0 for success or errno-type return code 29922 */ 29923 29924 static int 29925 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29926 struct buf **bpp) 29927 { 29928 int err; 29929 int i; 29930 int rval; 29931 struct buf *bp; 29932 struct scsi_pkt *pkt = NULL; 29933 uint32_t target_blocksize; 29934 29935 ASSERT(un != NULL); 29936 ASSERT(mutex_owned(SD_MUTEX(un))); 29937 29938 target_blocksize = un->un_tgt_blocksize; 29939 29940 mutex_exit(SD_MUTEX(un)); 29941 29942 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29943 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29944 if (bp == NULL) { 29945 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29946 "no resources for dumping; giving up"); 29947 err = ENOMEM; 29948 goto done; 29949 } 29950 29951 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29952 blkno, nblk); 29953 if (rval != 0) { 29954 scsi_free_consistent_buf(bp); 29955 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29956 "no resources for dumping; giving up"); 29957 err = ENOMEM; 29958 goto done; 29959 } 29960 29961 pkt->pkt_flags |= FLAG_NOINTR; 29962 29963 err = EIO; 29964 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29965 29966 /* 29967 * Scsi_poll returns 0 (success) if the command completes and 29968 * the status block is STATUS_GOOD. We should only check 29969 * errors if this condition is not true. Even then we should 29970 * send our own request sense packet only if we have a check 29971 * condition and auto request sense has not been performed by 29972 * the hba. 29973 */ 29974 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29975 29976 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29977 err = 0; 29978 break; 29979 } 29980 29981 /* 29982 * Check CMD_DEV_GONE 1st, give up if device is gone, 29983 * no need to read RQS data. 29984 */ 29985 if (pkt->pkt_reason == CMD_DEV_GONE) { 29986 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29987 "Device is gone\n"); 29988 break; 29989 } 29990 29991 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29992 SD_INFO(SD_LOG_DUMP, un, 29993 "sddump: read failed with CHECK, try # %d\n", i); 29994 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29995 (void) sd_send_polled_RQS(un); 29996 } 29997 29998 continue; 29999 } 30000 30001 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 30002 int reset_retval = 0; 30003 30004 SD_INFO(SD_LOG_DUMP, un, 30005 "sddump: read failed with BUSY, try # %d\n", i); 30006 30007 if (un->un_f_lun_reset_enabled == TRUE) { 30008 reset_retval = scsi_reset(SD_ADDRESS(un), 30009 RESET_LUN); 30010 } 30011 if (reset_retval == 0) { 30012 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 30013 } 30014 (void) sd_send_polled_RQS(un); 30015 30016 } else { 30017 SD_INFO(SD_LOG_DUMP, un, 30018 "sddump: read failed with 0x%x, try # %d\n", 30019 SD_GET_PKT_STATUS(pkt), i); 30020 mutex_enter(SD_MUTEX(un)); 30021 sd_reset_target(un, pkt); 30022 mutex_exit(SD_MUTEX(un)); 30023 } 30024 30025 /* 30026 * If we are not getting anywhere with lun/target resets, 30027 * let's reset the bus. 30028 */ 30029 if (i > SD_NDUMP_RETRIES/2) { 30030 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 30031 (void) sd_send_polled_RQS(un); 30032 } 30033 30034 } 30035 scsi_destroy_pkt(pkt); 30036 30037 if (err != 0) { 30038 scsi_free_consistent_buf(bp); 30039 *bpp = NULL; 30040 } else { 30041 *bpp = bp; 30042 } 30043 30044 done: 30045 mutex_enter(SD_MUTEX(un)); 30046 return (err); 30047 } 30048 30049 30050 /* 30051 * Function: sd_failfast_flushq 30052 * 30053 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30054 * in b_flags and move them onto the failfast queue, then kick 30055 * off a thread to return all bp's on the failfast queue to 30056 * their owners with an error set. 30057 * 30058 * Arguments: un - pointer to the soft state struct for the instance. 30059 * 30060 * Context: may execute in interrupt context. 30061 */ 30062 30063 static void 30064 sd_failfast_flushq(struct sd_lun *un) 30065 { 30066 struct buf *bp; 30067 struct buf *next_waitq_bp; 30068 struct buf *prev_waitq_bp = NULL; 30069 30070 ASSERT(un != NULL); 30071 ASSERT(mutex_owned(SD_MUTEX(un))); 30072 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30073 ASSERT(un->un_failfast_bp == NULL); 30074 30075 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30076 "sd_failfast_flushq: entry: un:0x%p\n", un); 30077 30078 /* 30079 * Check if we should flush all bufs when entering failfast state, or 30080 * just those with B_FAILFAST set. 30081 */ 30082 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30083 /* 30084 * Move *all* bp's on the wait queue to the failfast flush 30085 * queue, including those that do NOT have B_FAILFAST set. 30086 */ 30087 if (un->un_failfast_headp == NULL) { 30088 ASSERT(un->un_failfast_tailp == NULL); 30089 un->un_failfast_headp = un->un_waitq_headp; 30090 } else { 30091 ASSERT(un->un_failfast_tailp != NULL); 30092 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30093 } 30094 30095 un->un_failfast_tailp = un->un_waitq_tailp; 30096 30097 /* update kstat for each bp moved out of the waitq */ 30098 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30099 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30100 } 30101 30102 /* empty the waitq */ 30103 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30104 30105 } else { 30106 /* 30107 * Go thru the wait queue, pick off all entries with 30108 * B_FAILFAST set, and move these onto the failfast queue. 30109 */ 30110 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30111 /* 30112 * Save the pointer to the next bp on the wait queue, 30113 * so we get to it on the next iteration of this loop. 30114 */ 30115 next_waitq_bp = bp->av_forw; 30116 30117 /* 30118 * If this bp from the wait queue does NOT have 30119 * B_FAILFAST set, just move on to the next element 30120 * in the wait queue. Note, this is the only place 30121 * where it is correct to set prev_waitq_bp. 30122 */ 30123 if ((bp->b_flags & B_FAILFAST) == 0) { 30124 prev_waitq_bp = bp; 30125 continue; 30126 } 30127 30128 /* 30129 * Remove the bp from the wait queue. 30130 */ 30131 if (bp == un->un_waitq_headp) { 30132 /* The bp is the first element of the waitq. */ 30133 un->un_waitq_headp = next_waitq_bp; 30134 if (un->un_waitq_headp == NULL) { 30135 /* The wait queue is now empty */ 30136 un->un_waitq_tailp = NULL; 30137 } 30138 } else { 30139 /* 30140 * The bp is either somewhere in the middle 30141 * or at the end of the wait queue. 30142 */ 30143 ASSERT(un->un_waitq_headp != NULL); 30144 ASSERT(prev_waitq_bp != NULL); 30145 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30146 == 0); 30147 if (bp == un->un_waitq_tailp) { 30148 /* bp is the last entry on the waitq. */ 30149 ASSERT(next_waitq_bp == NULL); 30150 un->un_waitq_tailp = prev_waitq_bp; 30151 } 30152 prev_waitq_bp->av_forw = next_waitq_bp; 30153 } 30154 bp->av_forw = NULL; 30155 30156 /* 30157 * update kstat since the bp is moved out of 30158 * the waitq 30159 */ 30160 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30161 30162 /* 30163 * Now put the bp onto the failfast queue. 30164 */ 30165 if (un->un_failfast_headp == NULL) { 30166 /* failfast queue is currently empty */ 30167 ASSERT(un->un_failfast_tailp == NULL); 30168 un->un_failfast_headp = 30169 un->un_failfast_tailp = bp; 30170 } else { 30171 /* Add the bp to the end of the failfast q */ 30172 ASSERT(un->un_failfast_tailp != NULL); 30173 ASSERT(un->un_failfast_tailp->b_flags & 30174 B_FAILFAST); 30175 un->un_failfast_tailp->av_forw = bp; 30176 un->un_failfast_tailp = bp; 30177 } 30178 } 30179 } 30180 30181 /* 30182 * Now return all bp's on the failfast queue to their owners. 30183 */ 30184 while ((bp = un->un_failfast_headp) != NULL) { 30185 30186 un->un_failfast_headp = bp->av_forw; 30187 if (un->un_failfast_headp == NULL) { 30188 un->un_failfast_tailp = NULL; 30189 } 30190 30191 /* 30192 * We want to return the bp with a failure error code, but 30193 * we do not want a call to sd_start_cmds() to occur here, 30194 * so use sd_return_failed_command_no_restart() instead of 30195 * sd_return_failed_command(). 30196 */ 30197 sd_return_failed_command_no_restart(un, bp, EIO); 30198 } 30199 30200 /* Flush the xbuf queues if required. */ 30201 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30202 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30203 } 30204 30205 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30206 "sd_failfast_flushq: exit: un:0x%p\n", un); 30207 } 30208 30209 30210 /* 30211 * Function: sd_failfast_flushq_callback 30212 * 30213 * Description: Return TRUE if the given bp meets the criteria for failfast 30214 * flushing. Used with ddi_xbuf_flushq(9F). 30215 * 30216 * Arguments: bp - ptr to buf struct to be examined. 30217 * 30218 * Context: Any 30219 */ 30220 30221 static int 30222 sd_failfast_flushq_callback(struct buf *bp) 30223 { 30224 /* 30225 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30226 * state is entered; OR (2) the given bp has B_FAILFAST set. 30227 */ 30228 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30229 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30230 } 30231 30232 30233 30234 #if defined(__i386) || defined(__amd64) 30235 /* 30236 * Function: sd_setup_next_xfer 30237 * 30238 * Description: Prepare next I/O operation using DMA_PARTIAL 30239 * 30240 */ 30241 30242 static int 30243 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30244 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30245 { 30246 ssize_t num_blks_not_xfered; 30247 daddr_t strt_blk_num; 30248 ssize_t bytes_not_xfered; 30249 int rval; 30250 30251 ASSERT(pkt->pkt_resid == 0); 30252 30253 /* 30254 * Calculate next block number and amount to be transferred. 30255 * 30256 * How much data NOT transfered to the HBA yet. 30257 */ 30258 bytes_not_xfered = xp->xb_dma_resid; 30259 30260 /* 30261 * figure how many blocks NOT transfered to the HBA yet. 30262 */ 30263 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30264 30265 /* 30266 * set starting block number to the end of what WAS transfered. 30267 */ 30268 strt_blk_num = xp->xb_blkno + 30269 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30270 30271 /* 30272 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30273 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30274 * the disk mutex here. 30275 */ 30276 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30277 strt_blk_num, num_blks_not_xfered); 30278 30279 if (rval == 0) { 30280 30281 /* 30282 * Success. 30283 * 30284 * Adjust things if there are still more blocks to be 30285 * transfered. 30286 */ 30287 xp->xb_dma_resid = pkt->pkt_resid; 30288 pkt->pkt_resid = 0; 30289 30290 return (1); 30291 } 30292 30293 /* 30294 * There's really only one possible return value from 30295 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30296 * returns NULL. 30297 */ 30298 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30299 30300 bp->b_resid = bp->b_bcount; 30301 bp->b_flags |= B_ERROR; 30302 30303 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30304 "Error setting up next portion of DMA transfer\n"); 30305 30306 return (0); 30307 } 30308 #endif 30309 30310 /* 30311 * Function: sd_panic_for_res_conflict 30312 * 30313 * Description: Call panic with a string formated with "Reservation Conflict" 30314 * and a human readable identifier indicating the SD instance 30315 * that experienced the reservation conflict. 30316 * 30317 * Arguments: un - pointer to the soft state struct for the instance. 30318 * 30319 * Context: may execute in interrupt context. 30320 */ 30321 30322 #define SD_RESV_CONFLICT_FMT_LEN 40 30323 void 30324 sd_panic_for_res_conflict(struct sd_lun *un) 30325 { 30326 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30327 char path_str[MAXPATHLEN]; 30328 30329 (void) snprintf(panic_str, sizeof (panic_str), 30330 "Reservation Conflict\nDisk: %s", 30331 ddi_pathname(SD_DEVINFO(un), path_str)); 30332 30333 panic(panic_str); 30334 } 30335 30336 /* 30337 * Note: The following sd_faultinjection_ioctl( ) routines implement 30338 * driver support for handling fault injection for error analysis 30339 * causing faults in multiple layers of the driver. 30340 * 30341 */ 30342 30343 #ifdef SD_FAULT_INJECTION 30344 static uint_t sd_fault_injection_on = 0; 30345 30346 /* 30347 * Function: sd_faultinjection_ioctl() 30348 * 30349 * Description: This routine is the driver entry point for handling 30350 * faultinjection ioctls to inject errors into the 30351 * layer model 30352 * 30353 * Arguments: cmd - the ioctl cmd recieved 30354 * arg - the arguments from user and returns 30355 */ 30356 30357 static void 30358 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30359 30360 uint_t i; 30361 uint_t rval; 30362 30363 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30364 30365 mutex_enter(SD_MUTEX(un)); 30366 30367 switch (cmd) { 30368 case SDIOCRUN: 30369 /* Allow pushed faults to be injected */ 30370 SD_INFO(SD_LOG_SDTEST, un, 30371 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30372 30373 sd_fault_injection_on = 1; 30374 30375 SD_INFO(SD_LOG_IOERR, un, 30376 "sd_faultinjection_ioctl: run finished\n"); 30377 break; 30378 30379 case SDIOCSTART: 30380 /* Start Injection Session */ 30381 SD_INFO(SD_LOG_SDTEST, un, 30382 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30383 30384 sd_fault_injection_on = 0; 30385 un->sd_injection_mask = 0xFFFFFFFF; 30386 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30387 un->sd_fi_fifo_pkt[i] = NULL; 30388 un->sd_fi_fifo_xb[i] = NULL; 30389 un->sd_fi_fifo_un[i] = NULL; 30390 un->sd_fi_fifo_arq[i] = NULL; 30391 } 30392 un->sd_fi_fifo_start = 0; 30393 un->sd_fi_fifo_end = 0; 30394 30395 mutex_enter(&(un->un_fi_mutex)); 30396 un->sd_fi_log[0] = '\0'; 30397 un->sd_fi_buf_len = 0; 30398 mutex_exit(&(un->un_fi_mutex)); 30399 30400 SD_INFO(SD_LOG_IOERR, un, 30401 "sd_faultinjection_ioctl: start finished\n"); 30402 break; 30403 30404 case SDIOCSTOP: 30405 /* Stop Injection Session */ 30406 SD_INFO(SD_LOG_SDTEST, un, 30407 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30408 sd_fault_injection_on = 0; 30409 un->sd_injection_mask = 0x0; 30410 30411 /* Empty stray or unuseds structs from fifo */ 30412 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30413 if (un->sd_fi_fifo_pkt[i] != NULL) { 30414 kmem_free(un->sd_fi_fifo_pkt[i], 30415 sizeof (struct sd_fi_pkt)); 30416 } 30417 if (un->sd_fi_fifo_xb[i] != NULL) { 30418 kmem_free(un->sd_fi_fifo_xb[i], 30419 sizeof (struct sd_fi_xb)); 30420 } 30421 if (un->sd_fi_fifo_un[i] != NULL) { 30422 kmem_free(un->sd_fi_fifo_un[i], 30423 sizeof (struct sd_fi_un)); 30424 } 30425 if (un->sd_fi_fifo_arq[i] != NULL) { 30426 kmem_free(un->sd_fi_fifo_arq[i], 30427 sizeof (struct sd_fi_arq)); 30428 } 30429 un->sd_fi_fifo_pkt[i] = NULL; 30430 un->sd_fi_fifo_un[i] = NULL; 30431 un->sd_fi_fifo_xb[i] = NULL; 30432 un->sd_fi_fifo_arq[i] = NULL; 30433 } 30434 un->sd_fi_fifo_start = 0; 30435 un->sd_fi_fifo_end = 0; 30436 30437 SD_INFO(SD_LOG_IOERR, un, 30438 "sd_faultinjection_ioctl: stop finished\n"); 30439 break; 30440 30441 case SDIOCINSERTPKT: 30442 /* Store a packet struct to be pushed onto fifo */ 30443 SD_INFO(SD_LOG_SDTEST, un, 30444 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30445 30446 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30447 30448 sd_fault_injection_on = 0; 30449 30450 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30451 if (un->sd_fi_fifo_pkt[i] != NULL) { 30452 kmem_free(un->sd_fi_fifo_pkt[i], 30453 sizeof (struct sd_fi_pkt)); 30454 } 30455 if (arg != NULL) { 30456 un->sd_fi_fifo_pkt[i] = 30457 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30458 if (un->sd_fi_fifo_pkt[i] == NULL) { 30459 /* Alloc failed don't store anything */ 30460 break; 30461 } 30462 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30463 sizeof (struct sd_fi_pkt), 0); 30464 if (rval == -1) { 30465 kmem_free(un->sd_fi_fifo_pkt[i], 30466 sizeof (struct sd_fi_pkt)); 30467 un->sd_fi_fifo_pkt[i] = NULL; 30468 } 30469 } else { 30470 SD_INFO(SD_LOG_IOERR, un, 30471 "sd_faultinjection_ioctl: pkt null\n"); 30472 } 30473 break; 30474 30475 case SDIOCINSERTXB: 30476 /* Store a xb struct to be pushed onto fifo */ 30477 SD_INFO(SD_LOG_SDTEST, un, 30478 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30479 30480 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30481 30482 sd_fault_injection_on = 0; 30483 30484 if (un->sd_fi_fifo_xb[i] != NULL) { 30485 kmem_free(un->sd_fi_fifo_xb[i], 30486 sizeof (struct sd_fi_xb)); 30487 un->sd_fi_fifo_xb[i] = NULL; 30488 } 30489 if (arg != NULL) { 30490 un->sd_fi_fifo_xb[i] = 30491 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30492 if (un->sd_fi_fifo_xb[i] == NULL) { 30493 /* Alloc failed don't store anything */ 30494 break; 30495 } 30496 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30497 sizeof (struct sd_fi_xb), 0); 30498 30499 if (rval == -1) { 30500 kmem_free(un->sd_fi_fifo_xb[i], 30501 sizeof (struct sd_fi_xb)); 30502 un->sd_fi_fifo_xb[i] = NULL; 30503 } 30504 } else { 30505 SD_INFO(SD_LOG_IOERR, un, 30506 "sd_faultinjection_ioctl: xb null\n"); 30507 } 30508 break; 30509 30510 case SDIOCINSERTUN: 30511 /* Store a un struct to be pushed onto fifo */ 30512 SD_INFO(SD_LOG_SDTEST, un, 30513 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30514 30515 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30516 30517 sd_fault_injection_on = 0; 30518 30519 if (un->sd_fi_fifo_un[i] != NULL) { 30520 kmem_free(un->sd_fi_fifo_un[i], 30521 sizeof (struct sd_fi_un)); 30522 un->sd_fi_fifo_un[i] = NULL; 30523 } 30524 if (arg != NULL) { 30525 un->sd_fi_fifo_un[i] = 30526 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30527 if (un->sd_fi_fifo_un[i] == NULL) { 30528 /* Alloc failed don't store anything */ 30529 break; 30530 } 30531 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30532 sizeof (struct sd_fi_un), 0); 30533 if (rval == -1) { 30534 kmem_free(un->sd_fi_fifo_un[i], 30535 sizeof (struct sd_fi_un)); 30536 un->sd_fi_fifo_un[i] = NULL; 30537 } 30538 30539 } else { 30540 SD_INFO(SD_LOG_IOERR, un, 30541 "sd_faultinjection_ioctl: un null\n"); 30542 } 30543 30544 break; 30545 30546 case SDIOCINSERTARQ: 30547 /* Store a arq struct to be pushed onto fifo */ 30548 SD_INFO(SD_LOG_SDTEST, un, 30549 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30550 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30551 30552 sd_fault_injection_on = 0; 30553 30554 if (un->sd_fi_fifo_arq[i] != NULL) { 30555 kmem_free(un->sd_fi_fifo_arq[i], 30556 sizeof (struct sd_fi_arq)); 30557 un->sd_fi_fifo_arq[i] = NULL; 30558 } 30559 if (arg != NULL) { 30560 un->sd_fi_fifo_arq[i] = 30561 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30562 if (un->sd_fi_fifo_arq[i] == NULL) { 30563 /* Alloc failed don't store anything */ 30564 break; 30565 } 30566 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30567 sizeof (struct sd_fi_arq), 0); 30568 if (rval == -1) { 30569 kmem_free(un->sd_fi_fifo_arq[i], 30570 sizeof (struct sd_fi_arq)); 30571 un->sd_fi_fifo_arq[i] = NULL; 30572 } 30573 30574 } else { 30575 SD_INFO(SD_LOG_IOERR, un, 30576 "sd_faultinjection_ioctl: arq null\n"); 30577 } 30578 30579 break; 30580 30581 case SDIOCPUSH: 30582 /* Push stored xb, pkt, un, and arq onto fifo */ 30583 sd_fault_injection_on = 0; 30584 30585 if (arg != NULL) { 30586 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30587 if (rval != -1 && 30588 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30589 un->sd_fi_fifo_end += i; 30590 } 30591 } else { 30592 SD_INFO(SD_LOG_IOERR, un, 30593 "sd_faultinjection_ioctl: push arg null\n"); 30594 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30595 un->sd_fi_fifo_end++; 30596 } 30597 } 30598 SD_INFO(SD_LOG_IOERR, un, 30599 "sd_faultinjection_ioctl: push to end=%d\n", 30600 un->sd_fi_fifo_end); 30601 break; 30602 30603 case SDIOCRETRIEVE: 30604 /* Return buffer of log from Injection session */ 30605 SD_INFO(SD_LOG_SDTEST, un, 30606 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30607 30608 sd_fault_injection_on = 0; 30609 30610 mutex_enter(&(un->un_fi_mutex)); 30611 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30612 un->sd_fi_buf_len+1, 0); 30613 mutex_exit(&(un->un_fi_mutex)); 30614 30615 if (rval == -1) { 30616 /* 30617 * arg is possibly invalid setting 30618 * it to NULL for return 30619 */ 30620 arg = NULL; 30621 } 30622 break; 30623 } 30624 30625 mutex_exit(SD_MUTEX(un)); 30626 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30627 " exit\n"); 30628 } 30629 30630 30631 /* 30632 * Function: sd_injection_log() 30633 * 30634 * Description: This routine adds buff to the already existing injection log 30635 * for retrieval via faultinjection_ioctl for use in fault 30636 * detection and recovery 30637 * 30638 * Arguments: buf - the string to add to the log 30639 */ 30640 30641 static void 30642 sd_injection_log(char *buf, struct sd_lun *un) 30643 { 30644 uint_t len; 30645 30646 ASSERT(un != NULL); 30647 ASSERT(buf != NULL); 30648 30649 mutex_enter(&(un->un_fi_mutex)); 30650 30651 len = min(strlen(buf), 255); 30652 /* Add logged value to Injection log to be returned later */ 30653 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30654 uint_t offset = strlen((char *)un->sd_fi_log); 30655 char *destp = (char *)un->sd_fi_log + offset; 30656 int i; 30657 for (i = 0; i < len; i++) { 30658 *destp++ = *buf++; 30659 } 30660 un->sd_fi_buf_len += len; 30661 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30662 } 30663 30664 mutex_exit(&(un->un_fi_mutex)); 30665 } 30666 30667 30668 /* 30669 * Function: sd_faultinjection() 30670 * 30671 * Description: This routine takes the pkt and changes its 30672 * content based on error injection scenerio. 30673 * 30674 * Arguments: pktp - packet to be changed 30675 */ 30676 30677 static void 30678 sd_faultinjection(struct scsi_pkt *pktp) 30679 { 30680 uint_t i; 30681 struct sd_fi_pkt *fi_pkt; 30682 struct sd_fi_xb *fi_xb; 30683 struct sd_fi_un *fi_un; 30684 struct sd_fi_arq *fi_arq; 30685 struct buf *bp; 30686 struct sd_xbuf *xb; 30687 struct sd_lun *un; 30688 30689 ASSERT(pktp != NULL); 30690 30691 /* pull bp xb and un from pktp */ 30692 bp = (struct buf *)pktp->pkt_private; 30693 xb = SD_GET_XBUF(bp); 30694 un = SD_GET_UN(bp); 30695 30696 ASSERT(un != NULL); 30697 30698 mutex_enter(SD_MUTEX(un)); 30699 30700 SD_TRACE(SD_LOG_SDTEST, un, 30701 "sd_faultinjection: entry Injection from sdintr\n"); 30702 30703 /* if injection is off return */ 30704 if (sd_fault_injection_on == 0 || 30705 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30706 mutex_exit(SD_MUTEX(un)); 30707 return; 30708 } 30709 30710 30711 /* take next set off fifo */ 30712 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30713 30714 fi_pkt = un->sd_fi_fifo_pkt[i]; 30715 fi_xb = un->sd_fi_fifo_xb[i]; 30716 fi_un = un->sd_fi_fifo_un[i]; 30717 fi_arq = un->sd_fi_fifo_arq[i]; 30718 30719 30720 /* set variables accordingly */ 30721 /* set pkt if it was on fifo */ 30722 if (fi_pkt != NULL) { 30723 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30724 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30725 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30726 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30727 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30728 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30729 30730 } 30731 30732 /* set xb if it was on fifo */ 30733 if (fi_xb != NULL) { 30734 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30735 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30736 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30737 SD_CONDSET(xb, xb, xb_victim_retry_count, 30738 "xb_victim_retry_count"); 30739 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30740 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30741 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30742 30743 /* copy in block data from sense */ 30744 if (fi_xb->xb_sense_data[0] != -1) { 30745 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30746 SENSE_LENGTH); 30747 } 30748 30749 /* copy in extended sense codes */ 30750 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30751 "es_code"); 30752 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30753 "es_key"); 30754 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30755 "es_add_code"); 30756 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30757 es_qual_code, "es_qual_code"); 30758 } 30759 30760 /* set un if it was on fifo */ 30761 if (fi_un != NULL) { 30762 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30763 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30764 SD_CONDSET(un, un, un_reset_retry_count, 30765 "un_reset_retry_count"); 30766 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30767 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30768 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30769 SD_CONDSET(un, un, un_f_geometry_is_valid, 30770 "un_f_geometry_is_valid"); 30771 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30772 "un_f_allow_bus_device_reset"); 30773 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30774 30775 } 30776 30777 /* copy in auto request sense if it was on fifo */ 30778 if (fi_arq != NULL) { 30779 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30780 } 30781 30782 /* free structs */ 30783 if (un->sd_fi_fifo_pkt[i] != NULL) { 30784 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30785 } 30786 if (un->sd_fi_fifo_xb[i] != NULL) { 30787 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30788 } 30789 if (un->sd_fi_fifo_un[i] != NULL) { 30790 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30791 } 30792 if (un->sd_fi_fifo_arq[i] != NULL) { 30793 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30794 } 30795 30796 /* 30797 * kmem_free does not gurantee to set to NULL 30798 * since we uses these to determine if we set 30799 * values or not lets confirm they are always 30800 * NULL after free 30801 */ 30802 un->sd_fi_fifo_pkt[i] = NULL; 30803 un->sd_fi_fifo_un[i] = NULL; 30804 un->sd_fi_fifo_xb[i] = NULL; 30805 un->sd_fi_fifo_arq[i] = NULL; 30806 30807 un->sd_fi_fifo_start++; 30808 30809 mutex_exit(SD_MUTEX(un)); 30810 30811 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30812 } 30813 30814 #endif /* SD_FAULT_INJECTION */ 30815 30816 /* 30817 * This routine is invoked in sd_unit_attach(). Before calling it, the 30818 * properties in conf file should be processed already, and "hotpluggable" 30819 * property was processed also. 30820 * 30821 * The sd driver distinguishes 3 different type of devices: removable media, 30822 * non-removable media, and hotpluggable. Below the differences are defined: 30823 * 30824 * 1. Device ID 30825 * 30826 * The device ID of a device is used to identify this device. Refer to 30827 * ddi_devid_register(9F). 30828 * 30829 * For a non-removable media disk device which can provide 0x80 or 0x83 30830 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30831 * device ID is created to identify this device. For other non-removable 30832 * media devices, a default device ID is created only if this device has 30833 * at least 2 alter cylinders. Otherwise, this device has no devid. 30834 * 30835 * ------------------------------------------------------- 30836 * removable media hotpluggable | Can Have Device ID 30837 * ------------------------------------------------------- 30838 * false false | Yes 30839 * false true | Yes 30840 * true x | No 30841 * ------------------------------------------------------ 30842 * 30843 * 30844 * 2. SCSI group 4 commands 30845 * 30846 * In SCSI specs, only some commands in group 4 command set can use 30847 * 8-byte addresses that can be used to access >2TB storage spaces. 30848 * Other commands have no such capability. Without supporting group4, 30849 * it is impossible to make full use of storage spaces of a disk with 30850 * capacity larger than 2TB. 30851 * 30852 * ----------------------------------------------- 30853 * removable media hotpluggable LP64 | Group 30854 * ----------------------------------------------- 30855 * false false false | 1 30856 * false false true | 4 30857 * false true false | 1 30858 * false true true | 4 30859 * true x x | 5 30860 * ----------------------------------------------- 30861 * 30862 * 30863 * 3. Check for VTOC Label 30864 * 30865 * If a direct-access disk has no EFI label, sd will check if it has a 30866 * valid VTOC label. Now, sd also does that check for removable media 30867 * and hotpluggable devices. 30868 * 30869 * -------------------------------------------------------------- 30870 * Direct-Access removable media hotpluggable | Check Label 30871 * ------------------------------------------------------------- 30872 * false false false | No 30873 * false false true | No 30874 * false true false | Yes 30875 * false true true | Yes 30876 * true x x | Yes 30877 * -------------------------------------------------------------- 30878 * 30879 * 30880 * 4. Building default VTOC label 30881 * 30882 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30883 * If those devices have no valid VTOC label, sd(7d) will attempt to 30884 * create default VTOC for them. Currently sd creates default VTOC label 30885 * for all devices on x86 platform (VTOC_16), but only for removable 30886 * media devices on SPARC (VTOC_8). 30887 * 30888 * ----------------------------------------------------------- 30889 * removable media hotpluggable platform | Default Label 30890 * ----------------------------------------------------------- 30891 * false false sparc | No 30892 * false true x86 | Yes 30893 * false true sparc | Yes 30894 * true x x | Yes 30895 * ---------------------------------------------------------- 30896 * 30897 * 30898 * 5. Supported blocksizes of target devices 30899 * 30900 * Sd supports non-512-byte blocksize for removable media devices only. 30901 * For other devices, only 512-byte blocksize is supported. This may be 30902 * changed in near future because some RAID devices require non-512-byte 30903 * blocksize 30904 * 30905 * ----------------------------------------------------------- 30906 * removable media hotpluggable | non-512-byte blocksize 30907 * ----------------------------------------------------------- 30908 * false false | No 30909 * false true | No 30910 * true x | Yes 30911 * ----------------------------------------------------------- 30912 * 30913 * 30914 * 6. Automatic mount & unmount 30915 * 30916 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30917 * if a device is removable media device. It return 1 for removable media 30918 * devices, and 0 for others. 30919 * 30920 * The automatic mounting subsystem should distinguish between the types 30921 * of devices and apply automounting policies to each. 30922 * 30923 * 30924 * 7. fdisk partition management 30925 * 30926 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30927 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30928 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30929 * fdisk partitions on both x86 and SPARC platform. 30930 * 30931 * ----------------------------------------------------------- 30932 * platform removable media USB/1394 | fdisk supported 30933 * ----------------------------------------------------------- 30934 * x86 X X | true 30935 * ------------------------------------------------------------ 30936 * sparc X X | false 30937 * ------------------------------------------------------------ 30938 * 30939 * 30940 * 8. MBOOT/MBR 30941 * 30942 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30943 * read/write mboot for removable media devices on sparc platform. 30944 * 30945 * ----------------------------------------------------------- 30946 * platform removable media USB/1394 | mboot supported 30947 * ----------------------------------------------------------- 30948 * x86 X X | true 30949 * ------------------------------------------------------------ 30950 * sparc false false | false 30951 * sparc false true | true 30952 * sparc true false | true 30953 * sparc true true | true 30954 * ------------------------------------------------------------ 30955 * 30956 * 30957 * 9. error handling during opening device 30958 * 30959 * If failed to open a disk device, an errno is returned. For some kinds 30960 * of errors, different errno is returned depending on if this device is 30961 * a removable media device. This brings USB/1394 hard disks in line with 30962 * expected hard disk behavior. It is not expected that this breaks any 30963 * application. 30964 * 30965 * ------------------------------------------------------ 30966 * removable media hotpluggable | errno 30967 * ------------------------------------------------------ 30968 * false false | EIO 30969 * false true | EIO 30970 * true x | ENXIO 30971 * ------------------------------------------------------ 30972 * 30973 * 30974 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30975 * 30976 * These IOCTLs are applicable only to removable media devices. 30977 * 30978 * ----------------------------------------------------------- 30979 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30980 * ----------------------------------------------------------- 30981 * false false | No 30982 * false true | No 30983 * true x | Yes 30984 * ----------------------------------------------------------- 30985 * 30986 * 30987 * 12. Kstats for partitions 30988 * 30989 * sd creates partition kstat for non-removable media devices. USB and 30990 * Firewire hard disks now have partition kstats 30991 * 30992 * ------------------------------------------------------ 30993 * removable media hotplugable | kstat 30994 * ------------------------------------------------------ 30995 * false false | Yes 30996 * false true | Yes 30997 * true x | No 30998 * ------------------------------------------------------ 30999 * 31000 * 31001 * 13. Removable media & hotpluggable properties 31002 * 31003 * Sd driver creates a "removable-media" property for removable media 31004 * devices. Parent nexus drivers create a "hotpluggable" property if 31005 * it supports hotplugging. 31006 * 31007 * --------------------------------------------------------------------- 31008 * removable media hotpluggable | "removable-media" " hotpluggable" 31009 * --------------------------------------------------------------------- 31010 * false false | No No 31011 * false true | No Yes 31012 * true false | Yes No 31013 * true true | Yes Yes 31014 * --------------------------------------------------------------------- 31015 * 31016 * 31017 * 14. Power Management 31018 * 31019 * sd only power manages removable media devices or devices that support 31020 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 31021 * 31022 * A parent nexus that supports hotplugging can also set "pm-capable" 31023 * if the disk can be power managed. 31024 * 31025 * ------------------------------------------------------------ 31026 * removable media hotpluggable pm-capable | power manage 31027 * ------------------------------------------------------------ 31028 * false false false | No 31029 * false false true | Yes 31030 * false true false | No 31031 * false true true | Yes 31032 * true x x | Yes 31033 * ------------------------------------------------------------ 31034 * 31035 * USB and firewire hard disks can now be power managed independently 31036 * of the framebuffer 31037 * 31038 * 31039 * 15. Support for USB disks with capacity larger than 1TB 31040 * 31041 * Currently, sd doesn't permit a fixed disk device with capacity 31042 * larger than 1TB to be used in a 32-bit operating system environment. 31043 * However, sd doesn't do that for removable media devices. Instead, it 31044 * assumes that removable media devices cannot have a capacity larger 31045 * than 1TB. Therefore, using those devices on 32-bit system is partially 31046 * supported, which can cause some unexpected results. 31047 * 31048 * --------------------------------------------------------------------- 31049 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31050 * --------------------------------------------------------------------- 31051 * false false | true | no 31052 * false true | true | no 31053 * true false | true | Yes 31054 * true true | true | Yes 31055 * --------------------------------------------------------------------- 31056 * 31057 * 31058 * 16. Check write-protection at open time 31059 * 31060 * When a removable media device is being opened for writing without NDELAY 31061 * flag, sd will check if this device is writable. If attempting to open 31062 * without NDELAY flag a write-protected device, this operation will abort. 31063 * 31064 * ------------------------------------------------------------ 31065 * removable media USB/1394 | WP Check 31066 * ------------------------------------------------------------ 31067 * false false | No 31068 * false true | No 31069 * true false | Yes 31070 * true true | Yes 31071 * ------------------------------------------------------------ 31072 * 31073 * 31074 * 17. syslog when corrupted VTOC is encountered 31075 * 31076 * Currently, if an invalid VTOC is encountered, sd only print syslog 31077 * for fixed SCSI disks. 31078 * ------------------------------------------------------------ 31079 * removable media USB/1394 | print syslog 31080 * ------------------------------------------------------------ 31081 * false false | Yes 31082 * false true | No 31083 * true false | No 31084 * true true | No 31085 * ------------------------------------------------------------ 31086 */ 31087 static void 31088 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31089 { 31090 int pm_capable_prop; 31091 31092 ASSERT(un->un_sd); 31093 ASSERT(un->un_sd->sd_inq); 31094 31095 #if defined(_SUNOS_VTOC_16) 31096 /* 31097 * For VTOC_16 devices, the default label will be created for all 31098 * devices. (see sd_build_default_label) 31099 */ 31100 un->un_f_default_vtoc_supported = TRUE; 31101 #endif 31102 31103 /* 31104 * Enable SYNC CACHE support for all devices. 31105 */ 31106 un->un_f_sync_cache_supported = TRUE; 31107 31108 if (un->un_sd->sd_inq->inq_rmb) { 31109 /* 31110 * The media of this device is removable. And for this kind 31111 * of devices, it is possible to change medium after opening 31112 * devices. Thus we should support this operation. 31113 */ 31114 un->un_f_has_removable_media = TRUE; 31115 31116 #if defined(_SUNOS_VTOC_8) 31117 /* 31118 * Note: currently, for VTOC_8 devices, default label is 31119 * created for removable and hotpluggable devices only. 31120 */ 31121 un->un_f_default_vtoc_supported = TRUE; 31122 #endif 31123 /* 31124 * support non-512-byte blocksize of removable media devices 31125 */ 31126 un->un_f_non_devbsize_supported = TRUE; 31127 31128 /* 31129 * Assume that all removable media devices support DOOR_LOCK 31130 */ 31131 un->un_f_doorlock_supported = TRUE; 31132 31133 /* 31134 * For a removable media device, it is possible to be opened 31135 * with NDELAY flag when there is no media in drive, in this 31136 * case we don't care if device is writable. But if without 31137 * NDELAY flag, we need to check if media is write-protected. 31138 */ 31139 un->un_f_chk_wp_open = TRUE; 31140 31141 /* 31142 * need to start a SCSI watch thread to monitor media state, 31143 * when media is being inserted or ejected, notify syseventd. 31144 */ 31145 un->un_f_monitor_media_state = TRUE; 31146 31147 /* 31148 * Some devices don't support START_STOP_UNIT command. 31149 * Therefore, we'd better check if a device supports it 31150 * before sending it. 31151 */ 31152 un->un_f_check_start_stop = TRUE; 31153 31154 /* 31155 * support eject media ioctl: 31156 * FDEJECT, DKIOCEJECT, CDROMEJECT 31157 */ 31158 un->un_f_eject_media_supported = TRUE; 31159 31160 /* 31161 * Because many removable-media devices don't support 31162 * LOG_SENSE, we couldn't use this command to check if 31163 * a removable media device support power-management. 31164 * We assume that they support power-management via 31165 * START_STOP_UNIT command and can be spun up and down 31166 * without limitations. 31167 */ 31168 un->un_f_pm_supported = TRUE; 31169 31170 /* 31171 * Need to create a zero length (Boolean) property 31172 * removable-media for the removable media devices. 31173 * Note that the return value of the property is not being 31174 * checked, since if unable to create the property 31175 * then do not want the attach to fail altogether. Consistent 31176 * with other property creation in attach. 31177 */ 31178 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31179 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31180 31181 } else { 31182 /* 31183 * create device ID for device 31184 */ 31185 un->un_f_devid_supported = TRUE; 31186 31187 /* 31188 * Spin up non-removable-media devices once it is attached 31189 */ 31190 un->un_f_attach_spinup = TRUE; 31191 31192 /* 31193 * According to SCSI specification, Sense data has two kinds of 31194 * format: fixed format, and descriptor format. At present, we 31195 * don't support descriptor format sense data for removable 31196 * media. 31197 */ 31198 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31199 un->un_f_descr_format_supported = TRUE; 31200 } 31201 31202 /* 31203 * kstats are created only for non-removable media devices. 31204 * 31205 * Set this in sd.conf to 0 in order to disable kstats. The 31206 * default is 1, so they are enabled by default. 31207 */ 31208 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31209 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31210 "enable-partition-kstats", 1)); 31211 31212 /* 31213 * Check if HBA has set the "pm-capable" property. 31214 * If "pm-capable" exists and is non-zero then we can 31215 * power manage the device without checking the start/stop 31216 * cycle count log sense page. 31217 * 31218 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31219 * then we should not power manage the device. 31220 * 31221 * If "pm-capable" doesn't exist then pm_capable_prop will 31222 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31223 * sd will check the start/stop cycle count log sense page 31224 * and power manage the device if the cycle count limit has 31225 * not been exceeded. 31226 */ 31227 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31228 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31229 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31230 un->un_f_log_sense_supported = TRUE; 31231 } else { 31232 /* 31233 * pm-capable property exists. 31234 * 31235 * Convert "TRUE" values for pm_capable_prop to 31236 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31237 * later. "TRUE" values are any values except 31238 * SD_PM_CAPABLE_FALSE (0) and 31239 * SD_PM_CAPABLE_UNDEFINED (-1) 31240 */ 31241 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31242 un->un_f_log_sense_supported = FALSE; 31243 } else { 31244 un->un_f_pm_supported = TRUE; 31245 } 31246 31247 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31248 "sd_unit_attach: un:0x%p pm-capable " 31249 "property set to %d.\n", un, un->un_f_pm_supported); 31250 } 31251 } 31252 31253 if (un->un_f_is_hotpluggable) { 31254 #if defined(_SUNOS_VTOC_8) 31255 /* 31256 * Note: currently, for VTOC_8 devices, default label is 31257 * created for removable and hotpluggable devices only. 31258 */ 31259 un->un_f_default_vtoc_supported = TRUE; 31260 #endif 31261 31262 /* 31263 * Have to watch hotpluggable devices as well, since 31264 * that's the only way for userland applications to 31265 * detect hot removal while device is busy/mounted. 31266 */ 31267 un->un_f_monitor_media_state = TRUE; 31268 31269 un->un_f_check_start_stop = TRUE; 31270 31271 } 31272 31273 /* 31274 * By default, only DIRECT ACCESS devices and CDs will have Sun 31275 * labels. 31276 */ 31277 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31278 (un->un_sd->sd_inq->inq_rmb)) { 31279 /* 31280 * Direct access devices have disk label 31281 */ 31282 un->un_f_vtoc_label_supported = TRUE; 31283 } 31284 31285 /* 31286 * Fdisk partitions are supported for all direct access devices on 31287 * x86 platform, and just for removable media and hotpluggable 31288 * devices on SPARC platform. Later, we will set the following flag 31289 * to FALSE if current device is not removable media or hotpluggable 31290 * device and if sd works on SAPRC platform. 31291 */ 31292 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31293 un->un_f_mboot_supported = TRUE; 31294 } 31295 31296 if (!un->un_f_is_hotpluggable && 31297 !un->un_sd->sd_inq->inq_rmb) { 31298 31299 #if defined(_SUNOS_VTOC_8) 31300 /* 31301 * Don't support fdisk on fixed disk 31302 */ 31303 un->un_f_mboot_supported = FALSE; 31304 #endif 31305 31306 /* 31307 * For fixed disk, if its VTOC is not valid, we will write 31308 * errlog into system log 31309 */ 31310 if (un->un_f_vtoc_label_supported) 31311 un->un_f_vtoc_errlog_supported = TRUE; 31312 } 31313 } 31314