1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * SCSI disk target driver. 30 */ 31 #include <sys/scsi/scsi.h> 32 #include <sys/dkbad.h> 33 #include <sys/dklabel.h> 34 #include <sys/dkio.h> 35 #include <sys/fdio.h> 36 #include <sys/cdio.h> 37 #include <sys/mhd.h> 38 #include <sys/vtoc.h> 39 #include <sys/dktp/fdisk.h> 40 #include <sys/file.h> 41 #include <sys/stat.h> 42 #include <sys/kstat.h> 43 #include <sys/vtrace.h> 44 #include <sys/note.h> 45 #include <sys/thread.h> 46 #include <sys/proc.h> 47 #include <sys/efi_partition.h> 48 #include <sys/var.h> 49 #include <sys/aio_req.h> 50 51 #ifdef __lock_lint 52 #define _LP64 53 #define __amd64 54 #endif 55 56 #if (defined(__fibre)) 57 /* Note: is there a leadville version of the following? */ 58 #include <sys/fc4/fcal_linkapp.h> 59 #endif 60 #include <sys/taskq.h> 61 #include <sys/uuid.h> 62 #include <sys/byteorder.h> 63 #include <sys/sdt.h> 64 65 #include "sd_xbuf.h" 66 67 #include <sys/scsi/targets/sddef.h> 68 69 70 /* 71 * Loadable module info. 72 */ 73 #if (defined(__fibre)) 74 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 75 char _depends_on[] = "misc/scsi drv/fcp"; 76 #else 77 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 78 char _depends_on[] = "misc/scsi"; 79 #endif 80 81 /* 82 * Define the interconnect type, to allow the driver to distinguish 83 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 84 * 85 * This is really for backward compatability. In the future, the driver 86 * should actually check the "interconnect-type" property as reported by 87 * the HBA; however at present this property is not defined by all HBAs, 88 * so we will use this #define (1) to permit the driver to run in 89 * backward-compatability mode; and (2) to print a notification message 90 * if an FC HBA does not support the "interconnect-type" property. The 91 * behavior of the driver will be to assume parallel SCSI behaviors unless 92 * the "interconnect-type" property is defined by the HBA **AND** has a 93 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 94 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 95 * Channel behaviors (as per the old ssd). (Note that the 96 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 97 * will result in the driver assuming parallel SCSI behaviors.) 98 * 99 * (see common/sys/scsi/impl/services.h) 100 * 101 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 102 * since some FC HBAs may already support that, and there is some code in 103 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 104 * default would confuse that code, and besides things should work fine 105 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 106 * "interconnect_type" property. 107 * 108 * Notes for off-by-1 workaround: 109 * ----------------------------- 110 * 111 * SCSI READ_CAPACITY command returns the LBA number of the 112 * last logical block, but sd once treated this number as 113 * disks' capacity on x86 platform. And LBAs are addressed 114 * based 0. So the last block was lost on x86 platform. 115 * 116 * Now, we remove this workaround. In order for present sd 117 * driver to work with disks which are labeled/partitioned 118 * via previous sd, we add workaround as follows: 119 * 120 * 1) Locate backup EFI label: sd searchs the next to last 121 * block for backup EFI label if it can't find it on the 122 * last block; 123 * 2) Calculate geometry: refer to sd_convert_geometry(), If 124 * capacity increasing by 1 causes disks' capacity to cross 125 * over the limits in table CHS_values, geometry info will 126 * change. This will raise an issue: In case that primary 127 * VTOC label is destroyed, format commandline can restore 128 * it via backup VTOC labels. And format locates backup VTOC 129 * labels by use of geometry from sd driver. So changing 130 * geometry will prevent format from finding backup VTOC 131 * labels. To eliminate this side effect for compatibility, 132 * sd uses (capacity -1) to calculate geometry; 133 * 3) 1TB disks: VTOC uses 32-bit signed int, thus sd doesn't 134 * support VTOC for a disk which has more than DK_MAX_BLOCKS 135 * LBAs. However, for exactly 1TB disk, it was treated as 136 * (1T - 512)B in the past, and could have VTOC. To overcome 137 * this, if an exactly 1TB disk has solaris fdisk partition, 138 * it will be allowed to work with sd. 139 */ 140 #if (defined(__fibre)) 141 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 142 #else 143 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 144 #endif 145 146 /* 147 * The name of the driver, established from the module name in _init. 148 */ 149 static char *sd_label = NULL; 150 151 /* 152 * Driver name is unfortunately prefixed on some driver.conf properties. 153 */ 154 #if (defined(__fibre)) 155 #define sd_max_xfer_size ssd_max_xfer_size 156 #define sd_config_list ssd_config_list 157 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 158 static char *sd_config_list = "ssd-config-list"; 159 #else 160 static char *sd_max_xfer_size = "sd_max_xfer_size"; 161 static char *sd_config_list = "sd-config-list"; 162 #endif 163 164 /* 165 * Driver global variables 166 */ 167 168 #if (defined(__fibre)) 169 /* 170 * These #defines are to avoid namespace collisions that occur because this 171 * code is currently used to compile two seperate driver modules: sd and ssd. 172 * All global variables need to be treated this way (even if declared static) 173 * in order to allow the debugger to resolve the names properly. 174 * It is anticipated that in the near future the ssd module will be obsoleted, 175 * at which time this namespace issue should go away. 176 */ 177 #define sd_state ssd_state 178 #define sd_io_time ssd_io_time 179 #define sd_failfast_enable ssd_failfast_enable 180 #define sd_ua_retry_count ssd_ua_retry_count 181 #define sd_report_pfa ssd_report_pfa 182 #define sd_max_throttle ssd_max_throttle 183 #define sd_min_throttle ssd_min_throttle 184 #define sd_rot_delay ssd_rot_delay 185 186 #define sd_retry_on_reservation_conflict \ 187 ssd_retry_on_reservation_conflict 188 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 189 #define sd_resv_conflict_name ssd_resv_conflict_name 190 191 #define sd_component_mask ssd_component_mask 192 #define sd_level_mask ssd_level_mask 193 #define sd_debug_un ssd_debug_un 194 #define sd_error_level ssd_error_level 195 196 #define sd_xbuf_active_limit ssd_xbuf_active_limit 197 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 198 199 #define sd_tr ssd_tr 200 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 201 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 202 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 203 #define sd_check_media_time ssd_check_media_time 204 #define sd_wait_cmds_complete ssd_wait_cmds_complete 205 #define sd_label_mutex ssd_label_mutex 206 #define sd_detach_mutex ssd_detach_mutex 207 #define sd_log_buf ssd_log_buf 208 #define sd_log_mutex ssd_log_mutex 209 210 #define sd_disk_table ssd_disk_table 211 #define sd_disk_table_size ssd_disk_table_size 212 #define sd_sense_mutex ssd_sense_mutex 213 #define sd_cdbtab ssd_cdbtab 214 215 #define sd_cb_ops ssd_cb_ops 216 #define sd_ops ssd_ops 217 #define sd_additional_codes ssd_additional_codes 218 219 #define sd_minor_data ssd_minor_data 220 #define sd_minor_data_efi ssd_minor_data_efi 221 222 #define sd_tq ssd_tq 223 #define sd_wmr_tq ssd_wmr_tq 224 #define sd_taskq_name ssd_taskq_name 225 #define sd_wmr_taskq_name ssd_wmr_taskq_name 226 #define sd_taskq_minalloc ssd_taskq_minalloc 227 #define sd_taskq_maxalloc ssd_taskq_maxalloc 228 229 #define sd_dump_format_string ssd_dump_format_string 230 231 #define sd_iostart_chain ssd_iostart_chain 232 #define sd_iodone_chain ssd_iodone_chain 233 234 #define sd_pm_idletime ssd_pm_idletime 235 236 #define sd_force_pm_supported ssd_force_pm_supported 237 238 #define sd_dtype_optical_bind ssd_dtype_optical_bind 239 240 #endif 241 242 243 #ifdef SDDEBUG 244 int sd_force_pm_supported = 0; 245 #endif /* SDDEBUG */ 246 247 void *sd_state = NULL; 248 int sd_io_time = SD_IO_TIME; 249 int sd_failfast_enable = 1; 250 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 251 int sd_report_pfa = 1; 252 int sd_max_throttle = SD_MAX_THROTTLE; 253 int sd_min_throttle = SD_MIN_THROTTLE; 254 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 255 int sd_qfull_throttle_enable = TRUE; 256 257 int sd_retry_on_reservation_conflict = 1; 258 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 259 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 260 261 static int sd_dtype_optical_bind = -1; 262 263 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 264 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 265 266 /* 267 * Global data for debug logging. To enable debug printing, sd_component_mask 268 * and sd_level_mask should be set to the desired bit patterns as outlined in 269 * sddef.h. 270 */ 271 uint_t sd_component_mask = 0x0; 272 uint_t sd_level_mask = 0x0; 273 struct sd_lun *sd_debug_un = NULL; 274 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 275 276 /* Note: these may go away in the future... */ 277 static uint32_t sd_xbuf_active_limit = 512; 278 static uint32_t sd_xbuf_reserve_limit = 16; 279 280 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 281 282 /* 283 * Timer value used to reset the throttle after it has been reduced 284 * (typically in response to TRAN_BUSY or STATUS_QFULL) 285 */ 286 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 287 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 288 289 /* 290 * Interval value associated with the media change scsi watch. 291 */ 292 static int sd_check_media_time = 3000000; 293 294 /* 295 * Wait value used for in progress operations during a DDI_SUSPEND 296 */ 297 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 298 299 /* 300 * sd_label_mutex protects a static buffer used in the disk label 301 * component of the driver 302 */ 303 static kmutex_t sd_label_mutex; 304 305 /* 306 * sd_detach_mutex protects un_layer_count, un_detach_count, and 307 * un_opens_in_progress in the sd_lun structure. 308 */ 309 static kmutex_t sd_detach_mutex; 310 311 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 312 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 313 314 /* 315 * Global buffer and mutex for debug logging 316 */ 317 static char sd_log_buf[1024]; 318 static kmutex_t sd_log_mutex; 319 320 321 /* 322 * "Smart" Probe Caching structs, globals, #defines, etc. 323 * For parallel scsi and non-self-identify device only. 324 */ 325 326 /* 327 * The following resources and routines are implemented to support 328 * "smart" probing, which caches the scsi_probe() results in an array, 329 * in order to help avoid long probe times. 330 */ 331 struct sd_scsi_probe_cache { 332 struct sd_scsi_probe_cache *next; 333 dev_info_t *pdip; 334 int cache[NTARGETS_WIDE]; 335 }; 336 337 static kmutex_t sd_scsi_probe_cache_mutex; 338 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 339 340 /* 341 * Really we only need protection on the head of the linked list, but 342 * better safe than sorry. 343 */ 344 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 345 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 346 347 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 348 sd_scsi_probe_cache_head)) 349 350 351 /* 352 * Vendor specific data name property declarations 353 */ 354 355 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 356 357 static sd_tunables seagate_properties = { 358 SEAGATE_THROTTLE_VALUE, 359 0, 360 0, 361 0, 362 0, 363 0, 364 0, 365 0, 366 0 367 }; 368 369 370 static sd_tunables fujitsu_properties = { 371 FUJITSU_THROTTLE_VALUE, 372 0, 373 0, 374 0, 375 0, 376 0, 377 0, 378 0, 379 0 380 }; 381 382 static sd_tunables ibm_properties = { 383 IBM_THROTTLE_VALUE, 384 0, 385 0, 386 0, 387 0, 388 0, 389 0, 390 0, 391 0 392 }; 393 394 static sd_tunables purple_properties = { 395 PURPLE_THROTTLE_VALUE, 396 0, 397 0, 398 PURPLE_BUSY_RETRIES, 399 PURPLE_RESET_RETRY_COUNT, 400 PURPLE_RESERVE_RELEASE_TIME, 401 0, 402 0, 403 0 404 }; 405 406 static sd_tunables sve_properties = { 407 SVE_THROTTLE_VALUE, 408 0, 409 0, 410 SVE_BUSY_RETRIES, 411 SVE_RESET_RETRY_COUNT, 412 SVE_RESERVE_RELEASE_TIME, 413 SVE_MIN_THROTTLE_VALUE, 414 SVE_DISKSORT_DISABLED_FLAG, 415 0 416 }; 417 418 static sd_tunables maserati_properties = { 419 0, 420 0, 421 0, 422 0, 423 0, 424 0, 425 0, 426 MASERATI_DISKSORT_DISABLED_FLAG, 427 MASERATI_LUN_RESET_ENABLED_FLAG 428 }; 429 430 static sd_tunables pirus_properties = { 431 PIRUS_THROTTLE_VALUE, 432 0, 433 PIRUS_NRR_COUNT, 434 PIRUS_BUSY_RETRIES, 435 PIRUS_RESET_RETRY_COUNT, 436 0, 437 PIRUS_MIN_THROTTLE_VALUE, 438 PIRUS_DISKSORT_DISABLED_FLAG, 439 PIRUS_LUN_RESET_ENABLED_FLAG 440 }; 441 442 #endif 443 444 #if (defined(__sparc) && !defined(__fibre)) || \ 445 (defined(__i386) || defined(__amd64)) 446 447 448 static sd_tunables elite_properties = { 449 ELITE_THROTTLE_VALUE, 450 0, 451 0, 452 0, 453 0, 454 0, 455 0, 456 0, 457 0 458 }; 459 460 static sd_tunables st31200n_properties = { 461 ST31200N_THROTTLE_VALUE, 462 0, 463 0, 464 0, 465 0, 466 0, 467 0, 468 0, 469 0 470 }; 471 472 #endif /* Fibre or not */ 473 474 static sd_tunables lsi_properties_scsi = { 475 LSI_THROTTLE_VALUE, 476 0, 477 LSI_NOTREADY_RETRIES, 478 0, 479 0, 480 0, 481 0, 482 0, 483 0 484 }; 485 486 static sd_tunables symbios_properties = { 487 SYMBIOS_THROTTLE_VALUE, 488 0, 489 SYMBIOS_NOTREADY_RETRIES, 490 0, 491 0, 492 0, 493 0, 494 0, 495 0 496 }; 497 498 static sd_tunables lsi_properties = { 499 0, 500 0, 501 LSI_NOTREADY_RETRIES, 502 0, 503 0, 504 0, 505 0, 506 0, 507 0 508 }; 509 510 static sd_tunables lsi_oem_properties = { 511 0, 512 0, 513 LSI_OEM_NOTREADY_RETRIES, 514 0, 515 0, 516 0, 517 0, 518 0, 519 0 520 }; 521 522 523 524 #if (defined(SD_PROP_TST)) 525 526 #define SD_TST_CTYPE_VAL CTYPE_CDROM 527 #define SD_TST_THROTTLE_VAL 16 528 #define SD_TST_NOTREADY_VAL 12 529 #define SD_TST_BUSY_VAL 60 530 #define SD_TST_RST_RETRY_VAL 36 531 #define SD_TST_RSV_REL_TIME 60 532 533 static sd_tunables tst_properties = { 534 SD_TST_THROTTLE_VAL, 535 SD_TST_CTYPE_VAL, 536 SD_TST_NOTREADY_VAL, 537 SD_TST_BUSY_VAL, 538 SD_TST_RST_RETRY_VAL, 539 SD_TST_RSV_REL_TIME, 540 0, 541 0, 542 0 543 }; 544 #endif 545 546 /* This is similiar to the ANSI toupper implementation */ 547 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 548 549 /* 550 * Static Driver Configuration Table 551 * 552 * This is the table of disks which need throttle adjustment (or, perhaps 553 * something else as defined by the flags at a future time.) device_id 554 * is a string consisting of concatenated vid (vendor), pid (product/model) 555 * and revision strings as defined in the scsi_inquiry structure. Offsets of 556 * the parts of the string are as defined by the sizes in the scsi_inquiry 557 * structure. Device type is searched as far as the device_id string is 558 * defined. Flags defines which values are to be set in the driver from the 559 * properties list. 560 * 561 * Entries below which begin and end with a "*" are a special case. 562 * These do not have a specific vendor, and the string which follows 563 * can appear anywhere in the 16 byte PID portion of the inquiry data. 564 * 565 * Entries below which begin and end with a " " (blank) are a special 566 * case. The comparison function will treat multiple consecutive blanks 567 * as equivalent to a single blank. For example, this causes a 568 * sd_disk_table entry of " NEC CDROM " to match a device's id string 569 * of "NEC CDROM". 570 * 571 * Note: The MD21 controller type has been obsoleted. 572 * ST318202F is a Legacy device 573 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 574 * made with an FC connection. The entries here are a legacy. 575 */ 576 static sd_disk_config_t sd_disk_table[] = { 577 #if defined(__fibre) || defined(__i386) || defined(__amd64) 578 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 579 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 580 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 581 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 582 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 583 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 584 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 585 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 586 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 587 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 588 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 589 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 590 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 591 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 592 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 593 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 594 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 595 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 596 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 597 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 598 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 599 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 600 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 601 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 602 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 603 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 604 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 605 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 606 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 607 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 608 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 609 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 610 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 611 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 612 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 613 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 614 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 615 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 616 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 617 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 618 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 619 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 620 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 621 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 622 { "SUN T3", SD_CONF_BSET_THROTTLE | 623 SD_CONF_BSET_BSY_RETRY_COUNT| 624 SD_CONF_BSET_RST_RETRIES| 625 SD_CONF_BSET_RSV_REL_TIME, 626 &purple_properties }, 627 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 628 SD_CONF_BSET_BSY_RETRY_COUNT| 629 SD_CONF_BSET_RST_RETRIES| 630 SD_CONF_BSET_RSV_REL_TIME| 631 SD_CONF_BSET_MIN_THROTTLE| 632 SD_CONF_BSET_DISKSORT_DISABLED, 633 &sve_properties }, 634 { "SUN T4", SD_CONF_BSET_THROTTLE | 635 SD_CONF_BSET_BSY_RETRY_COUNT| 636 SD_CONF_BSET_RST_RETRIES| 637 SD_CONF_BSET_RSV_REL_TIME, 638 &purple_properties }, 639 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 640 SD_CONF_BSET_LUN_RESET_ENABLED, 641 &maserati_properties }, 642 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 643 SD_CONF_BSET_NRR_COUNT| 644 SD_CONF_BSET_BSY_RETRY_COUNT| 645 SD_CONF_BSET_RST_RETRIES| 646 SD_CONF_BSET_MIN_THROTTLE| 647 SD_CONF_BSET_DISKSORT_DISABLED| 648 SD_CONF_BSET_LUN_RESET_ENABLED, 649 &pirus_properties }, 650 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 651 SD_CONF_BSET_NRR_COUNT| 652 SD_CONF_BSET_BSY_RETRY_COUNT| 653 SD_CONF_BSET_RST_RETRIES| 654 SD_CONF_BSET_MIN_THROTTLE| 655 SD_CONF_BSET_DISKSORT_DISABLED| 656 SD_CONF_BSET_LUN_RESET_ENABLED, 657 &pirus_properties }, 658 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 659 SD_CONF_BSET_NRR_COUNT| 660 SD_CONF_BSET_BSY_RETRY_COUNT| 661 SD_CONF_BSET_RST_RETRIES| 662 SD_CONF_BSET_MIN_THROTTLE| 663 SD_CONF_BSET_DISKSORT_DISABLED| 664 SD_CONF_BSET_LUN_RESET_ENABLED, 665 &pirus_properties }, 666 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 667 SD_CONF_BSET_NRR_COUNT| 668 SD_CONF_BSET_BSY_RETRY_COUNT| 669 SD_CONF_BSET_RST_RETRIES| 670 SD_CONF_BSET_MIN_THROTTLE| 671 SD_CONF_BSET_DISKSORT_DISABLED| 672 SD_CONF_BSET_LUN_RESET_ENABLED, 673 &pirus_properties }, 674 { "SUN PSX1000", 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 SE6330", 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 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 691 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 692 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 693 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 694 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 695 #endif /* fibre or NON-sparc platforms */ 696 #if ((defined(__sparc) && !defined(__fibre)) ||\ 697 (defined(__i386) || defined(__amd64))) 698 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 699 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 700 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 701 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 702 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 703 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 704 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 705 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 706 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 707 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 708 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 709 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 710 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 711 &symbios_properties }, 712 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 713 &lsi_properties_scsi }, 714 #if defined(__i386) || defined(__amd64) 715 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 716 | SD_CONF_BSET_READSUB_BCD 717 | SD_CONF_BSET_READ_TOC_ADDR_BCD 718 | SD_CONF_BSET_NO_READ_HEADER 719 | SD_CONF_BSET_READ_CD_XD4), NULL }, 720 721 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 722 | SD_CONF_BSET_READSUB_BCD 723 | SD_CONF_BSET_READ_TOC_ADDR_BCD 724 | SD_CONF_BSET_NO_READ_HEADER 725 | SD_CONF_BSET_READ_CD_XD4), NULL }, 726 #endif /* __i386 || __amd64 */ 727 #endif /* sparc NON-fibre or NON-sparc platforms */ 728 729 #if (defined(SD_PROP_TST)) 730 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 731 | SD_CONF_BSET_CTYPE 732 | SD_CONF_BSET_NRR_COUNT 733 | SD_CONF_BSET_FAB_DEVID 734 | SD_CONF_BSET_NOCACHE 735 | SD_CONF_BSET_BSY_RETRY_COUNT 736 | SD_CONF_BSET_PLAYMSF_BCD 737 | SD_CONF_BSET_READSUB_BCD 738 | SD_CONF_BSET_READ_TOC_TRK_BCD 739 | SD_CONF_BSET_READ_TOC_ADDR_BCD 740 | SD_CONF_BSET_NO_READ_HEADER 741 | SD_CONF_BSET_READ_CD_XD4 742 | SD_CONF_BSET_RST_RETRIES 743 | SD_CONF_BSET_RSV_REL_TIME 744 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 745 #endif 746 }; 747 748 static const int sd_disk_table_size = 749 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 750 751 752 /* 753 * Return codes of sd_uselabel(). 754 */ 755 #define SD_LABEL_IS_VALID 0 756 #define SD_LABEL_IS_INVALID 1 757 758 #define SD_INTERCONNECT_PARALLEL 0 759 #define SD_INTERCONNECT_FABRIC 1 760 #define SD_INTERCONNECT_FIBRE 2 761 #define SD_INTERCONNECT_SSA 3 762 #define SD_IS_PARALLEL_SCSI(un) \ 763 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 764 765 /* 766 * Definitions used by device id registration routines 767 */ 768 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 769 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 770 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 771 #define WD_NODE 7 /* the whole disk minor */ 772 773 static kmutex_t sd_sense_mutex = {0}; 774 775 /* 776 * Macros for updates of the driver state 777 */ 778 #define New_state(un, s) \ 779 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 780 #define Restore_state(un) \ 781 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 782 783 static struct sd_cdbinfo sd_cdbtab[] = { 784 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 785 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 786 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 787 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 788 }; 789 790 /* 791 * Specifies the number of seconds that must have elapsed since the last 792 * cmd. has completed for a device to be declared idle to the PM framework. 793 */ 794 static int sd_pm_idletime = 1; 795 796 /* 797 * Internal function prototypes 798 */ 799 800 #if (defined(__fibre)) 801 /* 802 * These #defines are to avoid namespace collisions that occur because this 803 * code is currently used to compile two seperate driver modules: sd and ssd. 804 * All function names need to be treated this way (even if declared static) 805 * in order to allow the debugger to resolve the names properly. 806 * It is anticipated that in the near future the ssd module will be obsoleted, 807 * at which time this ugliness should go away. 808 */ 809 #define sd_log_trace ssd_log_trace 810 #define sd_log_info ssd_log_info 811 #define sd_log_err ssd_log_err 812 #define sdprobe ssdprobe 813 #define sdinfo ssdinfo 814 #define sd_prop_op ssd_prop_op 815 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 816 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 817 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 818 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 819 #define sd_spin_up_unit ssd_spin_up_unit 820 #define sd_enable_descr_sense ssd_enable_descr_sense 821 #define sd_reenable_dsense_task ssd_reenable_dsense_task 822 #define sd_set_mmc_caps ssd_set_mmc_caps 823 #define sd_read_unit_properties ssd_read_unit_properties 824 #define sd_process_sdconf_file ssd_process_sdconf_file 825 #define sd_process_sdconf_table ssd_process_sdconf_table 826 #define sd_sdconf_id_match ssd_sdconf_id_match 827 #define sd_blank_cmp ssd_blank_cmp 828 #define sd_chk_vers1_data ssd_chk_vers1_data 829 #define sd_set_vers1_properties ssd_set_vers1_properties 830 #define sd_validate_geometry ssd_validate_geometry 831 832 #if defined(_SUNOS_VTOC_16) 833 #define sd_convert_geometry ssd_convert_geometry 834 #endif 835 836 #define sd_resync_geom_caches ssd_resync_geom_caches 837 #define sd_read_fdisk ssd_read_fdisk 838 #define sd_get_physical_geometry ssd_get_physical_geometry 839 #define sd_get_virtual_geometry ssd_get_virtual_geometry 840 #define sd_update_block_info ssd_update_block_info 841 #define sd_swap_efi_gpt ssd_swap_efi_gpt 842 #define sd_swap_efi_gpe ssd_swap_efi_gpe 843 #define sd_validate_efi ssd_validate_efi 844 #define sd_use_efi ssd_use_efi 845 #define sd_uselabel ssd_uselabel 846 #define sd_build_default_label ssd_build_default_label 847 #define sd_has_max_chs_vals ssd_has_max_chs_vals 848 #define sd_inq_fill ssd_inq_fill 849 #define sd_register_devid ssd_register_devid 850 #define sd_get_devid_block ssd_get_devid_block 851 #define sd_get_devid ssd_get_devid 852 #define sd_create_devid ssd_create_devid 853 #define sd_write_deviceid ssd_write_deviceid 854 #define sd_check_vpd_page_support ssd_check_vpd_page_support 855 #define sd_setup_pm ssd_setup_pm 856 #define sd_create_pm_components ssd_create_pm_components 857 #define sd_ddi_suspend ssd_ddi_suspend 858 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 859 #define sd_ddi_resume ssd_ddi_resume 860 #define sd_ddi_pm_resume ssd_ddi_pm_resume 861 #define sdpower ssdpower 862 #define sdattach ssdattach 863 #define sddetach ssddetach 864 #define sd_unit_attach ssd_unit_attach 865 #define sd_unit_detach ssd_unit_detach 866 #define sd_set_unit_attributes ssd_set_unit_attributes 867 #define sd_create_minor_nodes ssd_create_minor_nodes 868 #define sd_create_errstats ssd_create_errstats 869 #define sd_set_errstats ssd_set_errstats 870 #define sd_set_pstats ssd_set_pstats 871 #define sddump ssddump 872 #define sd_scsi_poll ssd_scsi_poll 873 #define sd_send_polled_RQS ssd_send_polled_RQS 874 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 875 #define sd_init_event_callbacks ssd_init_event_callbacks 876 #define sd_event_callback ssd_event_callback 877 #define sd_cache_control ssd_cache_control 878 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 879 #define sd_make_device ssd_make_device 880 #define sdopen ssdopen 881 #define sdclose ssdclose 882 #define sd_ready_and_valid ssd_ready_and_valid 883 #define sdmin ssdmin 884 #define sdread ssdread 885 #define sdwrite ssdwrite 886 #define sdaread ssdaread 887 #define sdawrite ssdawrite 888 #define sdstrategy ssdstrategy 889 #define sdioctl ssdioctl 890 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 891 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 892 #define sd_checksum_iostart ssd_checksum_iostart 893 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 894 #define sd_pm_iostart ssd_pm_iostart 895 #define sd_core_iostart ssd_core_iostart 896 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 897 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 898 #define sd_checksum_iodone ssd_checksum_iodone 899 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 900 #define sd_pm_iodone ssd_pm_iodone 901 #define sd_initpkt_for_buf ssd_initpkt_for_buf 902 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 903 #define sd_setup_rw_pkt ssd_setup_rw_pkt 904 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 905 #define sd_buf_iodone ssd_buf_iodone 906 #define sd_uscsi_strategy ssd_uscsi_strategy 907 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 908 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 909 #define sd_uscsi_iodone ssd_uscsi_iodone 910 #define sd_xbuf_strategy ssd_xbuf_strategy 911 #define sd_xbuf_init ssd_xbuf_init 912 #define sd_pm_entry ssd_pm_entry 913 #define sd_pm_exit ssd_pm_exit 914 915 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 916 #define sd_pm_timeout_handler ssd_pm_timeout_handler 917 918 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 919 #define sdintr ssdintr 920 #define sd_start_cmds ssd_start_cmds 921 #define sd_send_scsi_cmd ssd_send_scsi_cmd 922 #define sd_bioclone_alloc ssd_bioclone_alloc 923 #define sd_bioclone_free ssd_bioclone_free 924 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 925 #define sd_shadow_buf_free ssd_shadow_buf_free 926 #define sd_print_transport_rejected_message \ 927 ssd_print_transport_rejected_message 928 #define sd_retry_command ssd_retry_command 929 #define sd_set_retry_bp ssd_set_retry_bp 930 #define sd_send_request_sense_command ssd_send_request_sense_command 931 #define sd_start_retry_command ssd_start_retry_command 932 #define sd_start_direct_priority_command \ 933 ssd_start_direct_priority_command 934 #define sd_return_failed_command ssd_return_failed_command 935 #define sd_return_failed_command_no_restart \ 936 ssd_return_failed_command_no_restart 937 #define sd_return_command ssd_return_command 938 #define sd_sync_with_callback ssd_sync_with_callback 939 #define sdrunout ssdrunout 940 #define sd_mark_rqs_busy ssd_mark_rqs_busy 941 #define sd_mark_rqs_idle ssd_mark_rqs_idle 942 #define sd_reduce_throttle ssd_reduce_throttle 943 #define sd_restore_throttle ssd_restore_throttle 944 #define sd_print_incomplete_msg ssd_print_incomplete_msg 945 #define sd_init_cdb_limits ssd_init_cdb_limits 946 #define sd_pkt_status_good ssd_pkt_status_good 947 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 948 #define sd_pkt_status_busy ssd_pkt_status_busy 949 #define sd_pkt_status_reservation_conflict \ 950 ssd_pkt_status_reservation_conflict 951 #define sd_pkt_status_qfull ssd_pkt_status_qfull 952 #define sd_handle_request_sense ssd_handle_request_sense 953 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 954 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 955 #define sd_validate_sense_data ssd_validate_sense_data 956 #define sd_decode_sense ssd_decode_sense 957 #define sd_print_sense_msg ssd_print_sense_msg 958 #define sd_sense_key_no_sense ssd_sense_key_no_sense 959 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 960 #define sd_sense_key_not_ready ssd_sense_key_not_ready 961 #define sd_sense_key_medium_or_hardware_error \ 962 ssd_sense_key_medium_or_hardware_error 963 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 964 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 965 #define sd_sense_key_fail_command ssd_sense_key_fail_command 966 #define sd_sense_key_blank_check ssd_sense_key_blank_check 967 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 968 #define sd_sense_key_default ssd_sense_key_default 969 #define sd_print_retry_msg ssd_print_retry_msg 970 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 971 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 972 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 973 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 974 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 975 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 976 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 977 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 978 #define sd_pkt_reason_default ssd_pkt_reason_default 979 #define sd_reset_target ssd_reset_target 980 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 981 #define sd_start_stop_unit_task ssd_start_stop_unit_task 982 #define sd_taskq_create ssd_taskq_create 983 #define sd_taskq_delete ssd_taskq_delete 984 #define sd_media_change_task ssd_media_change_task 985 #define sd_handle_mchange ssd_handle_mchange 986 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 987 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 988 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 989 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 990 #define sd_send_scsi_feature_GET_CONFIGURATION \ 991 sd_send_scsi_feature_GET_CONFIGURATION 992 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 993 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 994 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 995 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 996 ssd_send_scsi_PERSISTENT_RESERVE_IN 997 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 998 ssd_send_scsi_PERSISTENT_RESERVE_OUT 999 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1000 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1001 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1002 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1003 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1004 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1005 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1006 #define sd_alloc_rqs ssd_alloc_rqs 1007 #define sd_free_rqs ssd_free_rqs 1008 #define sd_dump_memory ssd_dump_memory 1009 #define sd_uscsi_ioctl ssd_uscsi_ioctl 1010 #define sd_get_media_info ssd_get_media_info 1011 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1012 #define sd_dkio_get_geometry ssd_dkio_get_geometry 1013 #define sd_dkio_set_geometry ssd_dkio_set_geometry 1014 #define sd_dkio_get_partition ssd_dkio_get_partition 1015 #define sd_dkio_set_partition ssd_dkio_set_partition 1016 #define sd_dkio_partition ssd_dkio_partition 1017 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 1018 #define sd_dkio_get_efi ssd_dkio_get_efi 1019 #define sd_build_user_vtoc ssd_build_user_vtoc 1020 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 1021 #define sd_dkio_set_efi ssd_dkio_set_efi 1022 #define sd_build_label_vtoc ssd_build_label_vtoc 1023 #define sd_write_label ssd_write_label 1024 #define sd_clear_vtoc ssd_clear_vtoc 1025 #define sd_clear_efi ssd_clear_efi 1026 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1027 #define sd_setup_next_xfer ssd_setup_next_xfer 1028 #define sd_dkio_get_temp ssd_dkio_get_temp 1029 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1030 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1031 #define sd_setup_default_geometry ssd_setup_default_geometry 1032 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1033 #define sd_check_mhd ssd_check_mhd 1034 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1035 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1036 #define sd_sname ssd_sname 1037 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1038 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1039 #define sd_take_ownership ssd_take_ownership 1040 #define sd_reserve_release ssd_reserve_release 1041 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1042 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1043 #define sd_persistent_reservation_in_read_keys \ 1044 ssd_persistent_reservation_in_read_keys 1045 #define sd_persistent_reservation_in_read_resv \ 1046 ssd_persistent_reservation_in_read_resv 1047 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1048 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1049 #define sd_mhdioc_release ssd_mhdioc_release 1050 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1051 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1052 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1053 #define sr_change_blkmode ssr_change_blkmode 1054 #define sr_change_speed ssr_change_speed 1055 #define sr_atapi_change_speed ssr_atapi_change_speed 1056 #define sr_pause_resume ssr_pause_resume 1057 #define sr_play_msf ssr_play_msf 1058 #define sr_play_trkind ssr_play_trkind 1059 #define sr_read_all_subcodes ssr_read_all_subcodes 1060 #define sr_read_subchannel ssr_read_subchannel 1061 #define sr_read_tocentry ssr_read_tocentry 1062 #define sr_read_tochdr ssr_read_tochdr 1063 #define sr_read_cdda ssr_read_cdda 1064 #define sr_read_cdxa ssr_read_cdxa 1065 #define sr_read_mode1 ssr_read_mode1 1066 #define sr_read_mode2 ssr_read_mode2 1067 #define sr_read_cd_mode2 ssr_read_cd_mode2 1068 #define sr_sector_mode ssr_sector_mode 1069 #define sr_eject ssr_eject 1070 #define sr_ejected ssr_ejected 1071 #define sr_check_wp ssr_check_wp 1072 #define sd_check_media ssd_check_media 1073 #define sd_media_watch_cb ssd_media_watch_cb 1074 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1075 #define sr_volume_ctrl ssr_volume_ctrl 1076 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1077 #define sd_log_page_supported ssd_log_page_supported 1078 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1079 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1080 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1081 #define sd_range_lock ssd_range_lock 1082 #define sd_get_range ssd_get_range 1083 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1084 #define sd_range_unlock ssd_range_unlock 1085 #define sd_read_modify_write_task ssd_read_modify_write_task 1086 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1087 1088 #define sd_iostart_chain ssd_iostart_chain 1089 #define sd_iodone_chain ssd_iodone_chain 1090 #define sd_initpkt_map ssd_initpkt_map 1091 #define sd_destroypkt_map ssd_destroypkt_map 1092 #define sd_chain_type_map ssd_chain_type_map 1093 #define sd_chain_index_map ssd_chain_index_map 1094 1095 #define sd_failfast_flushctl ssd_failfast_flushctl 1096 #define sd_failfast_flushq ssd_failfast_flushq 1097 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1098 1099 #define sd_is_lsi ssd_is_lsi 1100 1101 #endif /* #if (defined(__fibre)) */ 1102 1103 1104 int _init(void); 1105 int _fini(void); 1106 int _info(struct modinfo *modinfop); 1107 1108 /*PRINTFLIKE3*/ 1109 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1110 /*PRINTFLIKE3*/ 1111 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1112 /*PRINTFLIKE3*/ 1113 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1114 1115 static int sdprobe(dev_info_t *devi); 1116 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1117 void **result); 1118 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1119 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1120 1121 /* 1122 * Smart probe for parallel scsi 1123 */ 1124 static void sd_scsi_probe_cache_init(void); 1125 static void sd_scsi_probe_cache_fini(void); 1126 static void sd_scsi_clear_probe_cache(void); 1127 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1128 1129 static int sd_spin_up_unit(struct sd_lun *un); 1130 #ifdef _LP64 1131 static void sd_enable_descr_sense(struct sd_lun *un); 1132 static void sd_reenable_dsense_task(void *arg); 1133 #endif /* _LP64 */ 1134 1135 static void sd_set_mmc_caps(struct sd_lun *un); 1136 1137 static void sd_read_unit_properties(struct sd_lun *un); 1138 static int sd_process_sdconf_file(struct sd_lun *un); 1139 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1140 int *data_list, sd_tunables *values); 1141 static void sd_process_sdconf_table(struct sd_lun *un); 1142 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1143 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1144 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1145 int list_len, char *dataname_ptr); 1146 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1147 sd_tunables *prop_list); 1148 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1149 1150 #if defined(_SUNOS_VTOC_16) 1151 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1152 #endif 1153 1154 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1155 int path_flag); 1156 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1157 int path_flag); 1158 static void sd_get_physical_geometry(struct sd_lun *un, 1159 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1160 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1161 int lbasize); 1162 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1163 static void sd_swap_efi_gpt(efi_gpt_t *); 1164 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1165 static int sd_validate_efi(efi_gpt_t *); 1166 static int sd_use_efi(struct sd_lun *, int); 1167 static void sd_build_default_label(struct sd_lun *un); 1168 1169 #if defined(_FIRMWARE_NEEDS_FDISK) 1170 static int sd_has_max_chs_vals(struct ipart *fdp); 1171 #endif 1172 static void sd_inq_fill(char *p, int l, char *s); 1173 1174 1175 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1176 int reservation_flag); 1177 static daddr_t sd_get_devid_block(struct sd_lun *un); 1178 static int sd_get_devid(struct sd_lun *un); 1179 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1180 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1181 static int sd_write_deviceid(struct sd_lun *un); 1182 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1183 static int sd_check_vpd_page_support(struct sd_lun *un); 1184 1185 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1186 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1187 1188 static int sd_ddi_suspend(dev_info_t *devi); 1189 static int sd_ddi_pm_suspend(struct sd_lun *un); 1190 static int sd_ddi_resume(dev_info_t *devi); 1191 static int sd_ddi_pm_resume(struct sd_lun *un); 1192 static int sdpower(dev_info_t *devi, int component, int level); 1193 1194 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1195 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1196 static int sd_unit_attach(dev_info_t *devi); 1197 static int sd_unit_detach(dev_info_t *devi); 1198 1199 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1200 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1201 static void sd_create_errstats(struct sd_lun *un, int instance); 1202 static void sd_set_errstats(struct sd_lun *un); 1203 static void sd_set_pstats(struct sd_lun *un); 1204 1205 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1206 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1207 static int sd_send_polled_RQS(struct sd_lun *un); 1208 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1209 1210 #if (defined(__fibre)) 1211 /* 1212 * Event callbacks (photon) 1213 */ 1214 static void sd_init_event_callbacks(struct sd_lun *un); 1215 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1216 #endif 1217 1218 /* 1219 * Defines for sd_cache_control 1220 */ 1221 1222 #define SD_CACHE_ENABLE 1 1223 #define SD_CACHE_DISABLE 0 1224 #define SD_CACHE_NOCHANGE -1 1225 1226 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1227 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1228 static dev_t sd_make_device(dev_info_t *devi); 1229 1230 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1231 uint64_t capacity); 1232 1233 /* 1234 * Driver entry point functions. 1235 */ 1236 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1237 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1238 static int sd_ready_and_valid(struct sd_lun *un); 1239 1240 static void sdmin(struct buf *bp); 1241 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1242 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1243 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1244 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1245 1246 static int sdstrategy(struct buf *bp); 1247 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1248 1249 /* 1250 * Function prototypes for layering functions in the iostart chain. 1251 */ 1252 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1253 struct buf *bp); 1254 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1255 struct buf *bp); 1256 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1257 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1258 struct buf *bp); 1259 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1260 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1261 1262 /* 1263 * Function prototypes for layering functions in the iodone chain. 1264 */ 1265 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1266 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1267 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1268 struct buf *bp); 1269 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1270 struct buf *bp); 1271 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1272 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1273 struct buf *bp); 1274 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1275 1276 /* 1277 * Prototypes for functions to support buf(9S) based IO. 1278 */ 1279 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1280 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1281 static void sd_destroypkt_for_buf(struct buf *); 1282 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1283 struct buf *bp, int flags, 1284 int (*callback)(caddr_t), caddr_t callback_arg, 1285 diskaddr_t lba, uint32_t blockcount); 1286 #if defined(__i386) || defined(__amd64) 1287 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1288 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1289 #endif /* defined(__i386) || defined(__amd64) */ 1290 1291 /* 1292 * Prototypes for functions to support USCSI IO. 1293 */ 1294 static int sd_uscsi_strategy(struct buf *bp); 1295 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1296 static void sd_destroypkt_for_uscsi(struct buf *); 1297 1298 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1299 uchar_t chain_type, void *pktinfop); 1300 1301 static int sd_pm_entry(struct sd_lun *un); 1302 static void sd_pm_exit(struct sd_lun *un); 1303 1304 static void sd_pm_idletimeout_handler(void *arg); 1305 1306 /* 1307 * sd_core internal functions (used at the sd_core_io layer). 1308 */ 1309 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1310 static void sdintr(struct scsi_pkt *pktp); 1311 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1312 1313 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1314 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1315 int path_flag); 1316 1317 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1318 daddr_t blkno, int (*func)(struct buf *)); 1319 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1320 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1321 static void sd_bioclone_free(struct buf *bp); 1322 static void sd_shadow_buf_free(struct buf *bp); 1323 1324 static void sd_print_transport_rejected_message(struct sd_lun *un, 1325 struct sd_xbuf *xp, int code); 1326 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1327 void *arg, int code); 1328 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1329 void *arg, int code); 1330 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1331 void *arg, int code); 1332 1333 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1334 int retry_check_flag, 1335 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1336 int c), 1337 void *user_arg, int failure_code, clock_t retry_delay, 1338 void (*statp)(kstat_io_t *)); 1339 1340 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1341 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1342 1343 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1344 struct scsi_pkt *pktp); 1345 static void sd_start_retry_command(void *arg); 1346 static void sd_start_direct_priority_command(void *arg); 1347 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1348 int errcode); 1349 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1350 struct buf *bp, int errcode); 1351 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1352 static void sd_sync_with_callback(struct sd_lun *un); 1353 static int sdrunout(caddr_t arg); 1354 1355 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1356 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1357 1358 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1359 static void sd_restore_throttle(void *arg); 1360 1361 static void sd_init_cdb_limits(struct sd_lun *un); 1362 1363 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1364 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1365 1366 /* 1367 * Error handling functions 1368 */ 1369 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1370 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1371 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1372 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1373 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1374 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1375 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1376 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1377 1378 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1379 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1380 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1381 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1382 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1383 struct sd_xbuf *xp); 1384 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1385 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1386 1387 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1388 void *arg, int code); 1389 1390 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1391 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1392 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1393 uint8_t *sense_datap, 1394 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1395 static void sd_sense_key_not_ready(struct sd_lun *un, 1396 uint8_t *sense_datap, 1397 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1398 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1399 uint8_t *sense_datap, 1400 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1401 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1402 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1403 static void sd_sense_key_unit_attention(struct sd_lun *un, 1404 uint8_t *sense_datap, 1405 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1406 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1407 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1408 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1409 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1410 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1411 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1412 static void sd_sense_key_default(struct sd_lun *un, 1413 uint8_t *sense_datap, 1414 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1415 1416 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1417 void *arg, int flag); 1418 1419 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1420 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1421 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1422 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1423 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1424 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1425 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1426 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1427 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1428 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1429 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1430 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1431 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1432 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1433 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1434 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1435 1436 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1437 1438 static void sd_start_stop_unit_callback(void *arg); 1439 static void sd_start_stop_unit_task(void *arg); 1440 1441 static void sd_taskq_create(void); 1442 static void sd_taskq_delete(void); 1443 static void sd_media_change_task(void *arg); 1444 1445 static int sd_handle_mchange(struct sd_lun *un); 1446 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1447 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1448 uint32_t *lbap, int path_flag); 1449 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1450 uint32_t *lbap, int path_flag); 1451 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1452 int path_flag); 1453 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1454 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1455 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1456 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1457 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1458 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1459 uchar_t usr_cmd, uchar_t *usr_bufp); 1460 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1461 struct dk_callback *dkc); 1462 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1463 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1464 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1465 uchar_t *bufaddr, uint_t buflen); 1466 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1467 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1468 uchar_t *bufaddr, uint_t buflen, char feature); 1469 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1470 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1471 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1472 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1473 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1474 size_t buflen, daddr_t start_block, int path_flag); 1475 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1476 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1477 path_flag) 1478 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1479 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1480 path_flag) 1481 1482 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1483 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1484 uint16_t param_ptr, int path_flag); 1485 1486 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1487 static void sd_free_rqs(struct sd_lun *un); 1488 1489 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1490 uchar_t *data, int len, int fmt); 1491 static void sd_panic_for_res_conflict(struct sd_lun *un); 1492 1493 /* 1494 * Disk Ioctl Function Prototypes 1495 */ 1496 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1497 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1498 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1499 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1500 int geom_validated); 1501 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1502 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1503 int geom_validated); 1504 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1505 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1506 int geom_validated); 1507 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1508 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1509 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1510 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1511 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1512 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1513 static int sd_write_label(dev_t dev); 1514 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1515 static void sd_clear_vtoc(struct sd_lun *un); 1516 static void sd_clear_efi(struct sd_lun *un); 1517 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1518 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1519 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1520 static void sd_setup_default_geometry(struct sd_lun *un); 1521 #if defined(__i386) || defined(__amd64) 1522 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1523 #endif 1524 1525 /* 1526 * Multi-host Ioctl Prototypes 1527 */ 1528 static int sd_check_mhd(dev_t dev, int interval); 1529 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1530 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1531 static char *sd_sname(uchar_t status); 1532 static void sd_mhd_resvd_recover(void *arg); 1533 static void sd_resv_reclaim_thread(); 1534 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1535 static int sd_reserve_release(dev_t dev, int cmd); 1536 static void sd_rmv_resv_reclaim_req(dev_t dev); 1537 static void sd_mhd_reset_notify_cb(caddr_t arg); 1538 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1539 mhioc_inkeys_t *usrp, int flag); 1540 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1541 mhioc_inresvs_t *usrp, int flag); 1542 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1543 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1544 static int sd_mhdioc_release(dev_t dev); 1545 static int sd_mhdioc_register_devid(dev_t dev); 1546 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1547 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1548 1549 /* 1550 * SCSI removable prototypes 1551 */ 1552 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1553 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1554 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1555 static int sr_pause_resume(dev_t dev, int mode); 1556 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1557 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1558 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1559 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1560 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1561 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1562 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1563 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1564 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1565 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1566 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1567 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1568 static int sr_eject(dev_t dev); 1569 static void sr_ejected(register struct sd_lun *un); 1570 static int sr_check_wp(dev_t dev); 1571 static int sd_check_media(dev_t dev, enum dkio_state state); 1572 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1573 static void sd_delayed_cv_broadcast(void *arg); 1574 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1575 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1576 1577 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1578 1579 /* 1580 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1581 */ 1582 static void sd_check_for_writable_cd(struct sd_lun *un); 1583 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1584 static void sd_wm_cache_destructor(void *wm, void *un); 1585 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1586 daddr_t endb, ushort_t typ); 1587 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1588 daddr_t endb); 1589 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1590 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1591 static void sd_read_modify_write_task(void * arg); 1592 static int 1593 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1594 struct buf **bpp); 1595 1596 1597 /* 1598 * Function prototypes for failfast support. 1599 */ 1600 static void sd_failfast_flushq(struct sd_lun *un); 1601 static int sd_failfast_flushq_callback(struct buf *bp); 1602 1603 /* 1604 * Function prototypes to check for lsi devices 1605 */ 1606 static void sd_is_lsi(struct sd_lun *un); 1607 1608 /* 1609 * Function prototypes for x86 support 1610 */ 1611 #if defined(__i386) || defined(__amd64) 1612 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1613 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1614 #endif 1615 1616 /* 1617 * Constants for failfast support: 1618 * 1619 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1620 * failfast processing being performed. 1621 * 1622 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1623 * failfast processing on all bufs with B_FAILFAST set. 1624 */ 1625 1626 #define SD_FAILFAST_INACTIVE 0 1627 #define SD_FAILFAST_ACTIVE 1 1628 1629 /* 1630 * Bitmask to control behavior of buf(9S) flushes when a transition to 1631 * the failfast state occurs. Optional bits include: 1632 * 1633 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1634 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1635 * be flushed. 1636 * 1637 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1638 * driver, in addition to the regular wait queue. This includes the xbuf 1639 * queues. When clear, only the driver's wait queue will be flushed. 1640 */ 1641 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1642 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1643 1644 /* 1645 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1646 * to flush all queues within the driver. 1647 */ 1648 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1649 1650 1651 /* 1652 * SD Testing Fault Injection 1653 */ 1654 #ifdef SD_FAULT_INJECTION 1655 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1656 static void sd_faultinjection(struct scsi_pkt *pktp); 1657 static void sd_injection_log(char *buf, struct sd_lun *un); 1658 #endif 1659 1660 /* 1661 * Device driver ops vector 1662 */ 1663 static struct cb_ops sd_cb_ops = { 1664 sdopen, /* open */ 1665 sdclose, /* close */ 1666 sdstrategy, /* strategy */ 1667 nodev, /* print */ 1668 sddump, /* dump */ 1669 sdread, /* read */ 1670 sdwrite, /* write */ 1671 sdioctl, /* ioctl */ 1672 nodev, /* devmap */ 1673 nodev, /* mmap */ 1674 nodev, /* segmap */ 1675 nochpoll, /* poll */ 1676 sd_prop_op, /* cb_prop_op */ 1677 0, /* streamtab */ 1678 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1679 CB_REV, /* cb_rev */ 1680 sdaread, /* async I/O read entry point */ 1681 sdawrite /* async I/O write entry point */ 1682 }; 1683 1684 static struct dev_ops sd_ops = { 1685 DEVO_REV, /* devo_rev, */ 1686 0, /* refcnt */ 1687 sdinfo, /* info */ 1688 nulldev, /* identify */ 1689 sdprobe, /* probe */ 1690 sdattach, /* attach */ 1691 sddetach, /* detach */ 1692 nodev, /* reset */ 1693 &sd_cb_ops, /* driver operations */ 1694 NULL, /* bus operations */ 1695 sdpower /* power */ 1696 }; 1697 1698 1699 /* 1700 * This is the loadable module wrapper. 1701 */ 1702 #include <sys/modctl.h> 1703 1704 static struct modldrv modldrv = { 1705 &mod_driverops, /* Type of module. This one is a driver */ 1706 SD_MODULE_NAME, /* Module name. */ 1707 &sd_ops /* driver ops */ 1708 }; 1709 1710 1711 static struct modlinkage modlinkage = { 1712 MODREV_1, 1713 &modldrv, 1714 NULL 1715 }; 1716 1717 1718 static struct scsi_asq_key_strings sd_additional_codes[] = { 1719 0x81, 0, "Logical Unit is Reserved", 1720 0x85, 0, "Audio Address Not Valid", 1721 0xb6, 0, "Media Load Mechanism Failed", 1722 0xB9, 0, "Audio Play Operation Aborted", 1723 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1724 0x53, 2, "Medium removal prevented", 1725 0x6f, 0, "Authentication failed during key exchange", 1726 0x6f, 1, "Key not present", 1727 0x6f, 2, "Key not established", 1728 0x6f, 3, "Read without proper authentication", 1729 0x6f, 4, "Mismatched region to this logical unit", 1730 0x6f, 5, "Region reset count error", 1731 0xffff, 0x0, NULL 1732 }; 1733 1734 1735 /* 1736 * Struct for passing printing information for sense data messages 1737 */ 1738 struct sd_sense_info { 1739 int ssi_severity; 1740 int ssi_pfa_flag; 1741 }; 1742 1743 /* 1744 * Table of function pointers for iostart-side routines. Seperate "chains" 1745 * of layered function calls are formed by placing the function pointers 1746 * sequentially in the desired order. Functions are called according to an 1747 * incrementing table index ordering. The last function in each chain must 1748 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1749 * in the sd_iodone_chain[] array. 1750 * 1751 * Note: It may seem more natural to organize both the iostart and iodone 1752 * functions together, into an array of structures (or some similar 1753 * organization) with a common index, rather than two seperate arrays which 1754 * must be maintained in synchronization. The purpose of this division is 1755 * to achiece improved performance: individual arrays allows for more 1756 * effective cache line utilization on certain platforms. 1757 */ 1758 1759 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1760 1761 1762 static sd_chain_t sd_iostart_chain[] = { 1763 1764 /* Chain for buf IO for disk drive targets (PM enabled) */ 1765 sd_mapblockaddr_iostart, /* Index: 0 */ 1766 sd_pm_iostart, /* Index: 1 */ 1767 sd_core_iostart, /* Index: 2 */ 1768 1769 /* Chain for buf IO for disk drive targets (PM disabled) */ 1770 sd_mapblockaddr_iostart, /* Index: 3 */ 1771 sd_core_iostart, /* Index: 4 */ 1772 1773 /* Chain for buf IO for removable-media targets (PM enabled) */ 1774 sd_mapblockaddr_iostart, /* Index: 5 */ 1775 sd_mapblocksize_iostart, /* Index: 6 */ 1776 sd_pm_iostart, /* Index: 7 */ 1777 sd_core_iostart, /* Index: 8 */ 1778 1779 /* Chain for buf IO for removable-media targets (PM disabled) */ 1780 sd_mapblockaddr_iostart, /* Index: 9 */ 1781 sd_mapblocksize_iostart, /* Index: 10 */ 1782 sd_core_iostart, /* Index: 11 */ 1783 1784 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1785 sd_mapblockaddr_iostart, /* Index: 12 */ 1786 sd_checksum_iostart, /* Index: 13 */ 1787 sd_pm_iostart, /* Index: 14 */ 1788 sd_core_iostart, /* Index: 15 */ 1789 1790 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1791 sd_mapblockaddr_iostart, /* Index: 16 */ 1792 sd_checksum_iostart, /* Index: 17 */ 1793 sd_core_iostart, /* Index: 18 */ 1794 1795 /* Chain for USCSI commands (all targets) */ 1796 sd_pm_iostart, /* Index: 19 */ 1797 sd_core_iostart, /* Index: 20 */ 1798 1799 /* Chain for checksumming USCSI commands (all targets) */ 1800 sd_checksum_uscsi_iostart, /* Index: 21 */ 1801 sd_pm_iostart, /* Index: 22 */ 1802 sd_core_iostart, /* Index: 23 */ 1803 1804 /* Chain for "direct" USCSI commands (all targets) */ 1805 sd_core_iostart, /* Index: 24 */ 1806 1807 /* Chain for "direct priority" USCSI commands (all targets) */ 1808 sd_core_iostart, /* Index: 25 */ 1809 }; 1810 1811 /* 1812 * Macros to locate the first function of each iostart chain in the 1813 * sd_iostart_chain[] array. These are located by the index in the array. 1814 */ 1815 #define SD_CHAIN_DISK_IOSTART 0 1816 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1817 #define SD_CHAIN_RMMEDIA_IOSTART 5 1818 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1819 #define SD_CHAIN_CHKSUM_IOSTART 12 1820 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1821 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1822 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1823 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1824 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1825 1826 1827 /* 1828 * Table of function pointers for the iodone-side routines for the driver- 1829 * internal layering mechanism. The calling sequence for iodone routines 1830 * uses a decrementing table index, so the last routine called in a chain 1831 * must be at the lowest array index location for that chain. The last 1832 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1833 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1834 * of the functions in an iodone side chain must correspond to the ordering 1835 * of the iostart routines for that chain. Note that there is no iodone 1836 * side routine that corresponds to sd_core_iostart(), so there is no 1837 * entry in the table for this. 1838 */ 1839 1840 static sd_chain_t sd_iodone_chain[] = { 1841 1842 /* Chain for buf IO for disk drive targets (PM enabled) */ 1843 sd_buf_iodone, /* Index: 0 */ 1844 sd_mapblockaddr_iodone, /* Index: 1 */ 1845 sd_pm_iodone, /* Index: 2 */ 1846 1847 /* Chain for buf IO for disk drive targets (PM disabled) */ 1848 sd_buf_iodone, /* Index: 3 */ 1849 sd_mapblockaddr_iodone, /* Index: 4 */ 1850 1851 /* Chain for buf IO for removable-media targets (PM enabled) */ 1852 sd_buf_iodone, /* Index: 5 */ 1853 sd_mapblockaddr_iodone, /* Index: 6 */ 1854 sd_mapblocksize_iodone, /* Index: 7 */ 1855 sd_pm_iodone, /* Index: 8 */ 1856 1857 /* Chain for buf IO for removable-media targets (PM disabled) */ 1858 sd_buf_iodone, /* Index: 9 */ 1859 sd_mapblockaddr_iodone, /* Index: 10 */ 1860 sd_mapblocksize_iodone, /* Index: 11 */ 1861 1862 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1863 sd_buf_iodone, /* Index: 12 */ 1864 sd_mapblockaddr_iodone, /* Index: 13 */ 1865 sd_checksum_iodone, /* Index: 14 */ 1866 sd_pm_iodone, /* Index: 15 */ 1867 1868 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1869 sd_buf_iodone, /* Index: 16 */ 1870 sd_mapblockaddr_iodone, /* Index: 17 */ 1871 sd_checksum_iodone, /* Index: 18 */ 1872 1873 /* Chain for USCSI commands (non-checksum targets) */ 1874 sd_uscsi_iodone, /* Index: 19 */ 1875 sd_pm_iodone, /* Index: 20 */ 1876 1877 /* Chain for USCSI commands (checksum targets) */ 1878 sd_uscsi_iodone, /* Index: 21 */ 1879 sd_checksum_uscsi_iodone, /* Index: 22 */ 1880 sd_pm_iodone, /* Index: 22 */ 1881 1882 /* Chain for "direct" USCSI commands (all targets) */ 1883 sd_uscsi_iodone, /* Index: 24 */ 1884 1885 /* Chain for "direct priority" USCSI commands (all targets) */ 1886 sd_uscsi_iodone, /* Index: 25 */ 1887 }; 1888 1889 1890 /* 1891 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1892 * each iodone-side chain. These are located by the array index, but as the 1893 * iodone side functions are called in a decrementing-index order, the 1894 * highest index number in each chain must be specified (as these correspond 1895 * to the first function in the iodone chain that will be called by the core 1896 * at IO completion time). 1897 */ 1898 1899 #define SD_CHAIN_DISK_IODONE 2 1900 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1901 #define SD_CHAIN_RMMEDIA_IODONE 8 1902 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1903 #define SD_CHAIN_CHKSUM_IODONE 15 1904 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1905 #define SD_CHAIN_USCSI_CMD_IODONE 20 1906 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1907 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1908 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1909 1910 1911 1912 1913 /* 1914 * Array to map a layering chain index to the appropriate initpkt routine. 1915 * The redundant entries are present so that the index used for accessing 1916 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1917 * with this table as well. 1918 */ 1919 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1920 1921 static sd_initpkt_t sd_initpkt_map[] = { 1922 1923 /* Chain for buf IO for disk drive targets (PM enabled) */ 1924 sd_initpkt_for_buf, /* Index: 0 */ 1925 sd_initpkt_for_buf, /* Index: 1 */ 1926 sd_initpkt_for_buf, /* Index: 2 */ 1927 1928 /* Chain for buf IO for disk drive targets (PM disabled) */ 1929 sd_initpkt_for_buf, /* Index: 3 */ 1930 sd_initpkt_for_buf, /* Index: 4 */ 1931 1932 /* Chain for buf IO for removable-media targets (PM enabled) */ 1933 sd_initpkt_for_buf, /* Index: 5 */ 1934 sd_initpkt_for_buf, /* Index: 6 */ 1935 sd_initpkt_for_buf, /* Index: 7 */ 1936 sd_initpkt_for_buf, /* Index: 8 */ 1937 1938 /* Chain for buf IO for removable-media targets (PM disabled) */ 1939 sd_initpkt_for_buf, /* Index: 9 */ 1940 sd_initpkt_for_buf, /* Index: 10 */ 1941 sd_initpkt_for_buf, /* Index: 11 */ 1942 1943 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1944 sd_initpkt_for_buf, /* Index: 12 */ 1945 sd_initpkt_for_buf, /* Index: 13 */ 1946 sd_initpkt_for_buf, /* Index: 14 */ 1947 sd_initpkt_for_buf, /* Index: 15 */ 1948 1949 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1950 sd_initpkt_for_buf, /* Index: 16 */ 1951 sd_initpkt_for_buf, /* Index: 17 */ 1952 sd_initpkt_for_buf, /* Index: 18 */ 1953 1954 /* Chain for USCSI commands (non-checksum targets) */ 1955 sd_initpkt_for_uscsi, /* Index: 19 */ 1956 sd_initpkt_for_uscsi, /* Index: 20 */ 1957 1958 /* Chain for USCSI commands (checksum targets) */ 1959 sd_initpkt_for_uscsi, /* Index: 21 */ 1960 sd_initpkt_for_uscsi, /* Index: 22 */ 1961 sd_initpkt_for_uscsi, /* Index: 22 */ 1962 1963 /* Chain for "direct" USCSI commands (all targets) */ 1964 sd_initpkt_for_uscsi, /* Index: 24 */ 1965 1966 /* Chain for "direct priority" USCSI commands (all targets) */ 1967 sd_initpkt_for_uscsi, /* Index: 25 */ 1968 1969 }; 1970 1971 1972 /* 1973 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1974 * The redundant entries are present so that the index used for accessing 1975 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1976 * with this table as well. 1977 */ 1978 typedef void (*sd_destroypkt_t)(struct buf *); 1979 1980 static sd_destroypkt_t sd_destroypkt_map[] = { 1981 1982 /* Chain for buf IO for disk drive targets (PM enabled) */ 1983 sd_destroypkt_for_buf, /* Index: 0 */ 1984 sd_destroypkt_for_buf, /* Index: 1 */ 1985 sd_destroypkt_for_buf, /* Index: 2 */ 1986 1987 /* Chain for buf IO for disk drive targets (PM disabled) */ 1988 sd_destroypkt_for_buf, /* Index: 3 */ 1989 sd_destroypkt_for_buf, /* Index: 4 */ 1990 1991 /* Chain for buf IO for removable-media targets (PM enabled) */ 1992 sd_destroypkt_for_buf, /* Index: 5 */ 1993 sd_destroypkt_for_buf, /* Index: 6 */ 1994 sd_destroypkt_for_buf, /* Index: 7 */ 1995 sd_destroypkt_for_buf, /* Index: 8 */ 1996 1997 /* Chain for buf IO for removable-media targets (PM disabled) */ 1998 sd_destroypkt_for_buf, /* Index: 9 */ 1999 sd_destroypkt_for_buf, /* Index: 10 */ 2000 sd_destroypkt_for_buf, /* Index: 11 */ 2001 2002 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2003 sd_destroypkt_for_buf, /* Index: 12 */ 2004 sd_destroypkt_for_buf, /* Index: 13 */ 2005 sd_destroypkt_for_buf, /* Index: 14 */ 2006 sd_destroypkt_for_buf, /* Index: 15 */ 2007 2008 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2009 sd_destroypkt_for_buf, /* Index: 16 */ 2010 sd_destroypkt_for_buf, /* Index: 17 */ 2011 sd_destroypkt_for_buf, /* Index: 18 */ 2012 2013 /* Chain for USCSI commands (non-checksum targets) */ 2014 sd_destroypkt_for_uscsi, /* Index: 19 */ 2015 sd_destroypkt_for_uscsi, /* Index: 20 */ 2016 2017 /* Chain for USCSI commands (checksum targets) */ 2018 sd_destroypkt_for_uscsi, /* Index: 21 */ 2019 sd_destroypkt_for_uscsi, /* Index: 22 */ 2020 sd_destroypkt_for_uscsi, /* Index: 22 */ 2021 2022 /* Chain for "direct" USCSI commands (all targets) */ 2023 sd_destroypkt_for_uscsi, /* Index: 24 */ 2024 2025 /* Chain for "direct priority" USCSI commands (all targets) */ 2026 sd_destroypkt_for_uscsi, /* Index: 25 */ 2027 2028 }; 2029 2030 2031 2032 /* 2033 * Array to map a layering chain index to the appropriate chain "type". 2034 * The chain type indicates a specific property/usage of the chain. 2035 * The redundant entries are present so that the index used for accessing 2036 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2037 * with this table as well. 2038 */ 2039 2040 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2041 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2042 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2043 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2044 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2045 /* (for error recovery) */ 2046 2047 static int sd_chain_type_map[] = { 2048 2049 /* Chain for buf IO for disk drive targets (PM enabled) */ 2050 SD_CHAIN_BUFIO, /* Index: 0 */ 2051 SD_CHAIN_BUFIO, /* Index: 1 */ 2052 SD_CHAIN_BUFIO, /* Index: 2 */ 2053 2054 /* Chain for buf IO for disk drive targets (PM disabled) */ 2055 SD_CHAIN_BUFIO, /* Index: 3 */ 2056 SD_CHAIN_BUFIO, /* Index: 4 */ 2057 2058 /* Chain for buf IO for removable-media targets (PM enabled) */ 2059 SD_CHAIN_BUFIO, /* Index: 5 */ 2060 SD_CHAIN_BUFIO, /* Index: 6 */ 2061 SD_CHAIN_BUFIO, /* Index: 7 */ 2062 SD_CHAIN_BUFIO, /* Index: 8 */ 2063 2064 /* Chain for buf IO for removable-media targets (PM disabled) */ 2065 SD_CHAIN_BUFIO, /* Index: 9 */ 2066 SD_CHAIN_BUFIO, /* Index: 10 */ 2067 SD_CHAIN_BUFIO, /* Index: 11 */ 2068 2069 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2070 SD_CHAIN_BUFIO, /* Index: 12 */ 2071 SD_CHAIN_BUFIO, /* Index: 13 */ 2072 SD_CHAIN_BUFIO, /* Index: 14 */ 2073 SD_CHAIN_BUFIO, /* Index: 15 */ 2074 2075 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2076 SD_CHAIN_BUFIO, /* Index: 16 */ 2077 SD_CHAIN_BUFIO, /* Index: 17 */ 2078 SD_CHAIN_BUFIO, /* Index: 18 */ 2079 2080 /* Chain for USCSI commands (non-checksum targets) */ 2081 SD_CHAIN_USCSI, /* Index: 19 */ 2082 SD_CHAIN_USCSI, /* Index: 20 */ 2083 2084 /* Chain for USCSI commands (checksum targets) */ 2085 SD_CHAIN_USCSI, /* Index: 21 */ 2086 SD_CHAIN_USCSI, /* Index: 22 */ 2087 SD_CHAIN_USCSI, /* Index: 22 */ 2088 2089 /* Chain for "direct" USCSI commands (all targets) */ 2090 SD_CHAIN_DIRECT, /* Index: 24 */ 2091 2092 /* Chain for "direct priority" USCSI commands (all targets) */ 2093 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2094 }; 2095 2096 2097 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2098 #define SD_IS_BUFIO(xp) \ 2099 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2100 2101 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2102 #define SD_IS_DIRECT_PRIORITY(xp) \ 2103 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2104 2105 2106 2107 /* 2108 * Struct, array, and macros to map a specific chain to the appropriate 2109 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2110 * 2111 * The sd_chain_index_map[] array is used at attach time to set the various 2112 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2113 * chain to be used with the instance. This allows different instances to use 2114 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2115 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2116 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2117 * dynamically & without the use of locking; and (2) a layer may update the 2118 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2119 * to allow for deferred processing of an IO within the same chain from a 2120 * different execution context. 2121 */ 2122 2123 struct sd_chain_index { 2124 int sci_iostart_index; 2125 int sci_iodone_index; 2126 }; 2127 2128 static struct sd_chain_index sd_chain_index_map[] = { 2129 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2130 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2131 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2132 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2133 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2134 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2135 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2136 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2137 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2138 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2139 }; 2140 2141 2142 /* 2143 * The following are indexes into the sd_chain_index_map[] array. 2144 */ 2145 2146 /* un->un_buf_chain_type must be set to one of these */ 2147 #define SD_CHAIN_INFO_DISK 0 2148 #define SD_CHAIN_INFO_DISK_NO_PM 1 2149 #define SD_CHAIN_INFO_RMMEDIA 2 2150 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2151 #define SD_CHAIN_INFO_CHKSUM 4 2152 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2153 2154 /* un->un_uscsi_chain_type must be set to one of these */ 2155 #define SD_CHAIN_INFO_USCSI_CMD 6 2156 /* USCSI with PM disabled is the same as DIRECT */ 2157 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2158 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2159 2160 /* un->un_direct_chain_type must be set to one of these */ 2161 #define SD_CHAIN_INFO_DIRECT_CMD 8 2162 2163 /* un->un_priority_chain_type must be set to one of these */ 2164 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2165 2166 /* size for devid inquiries */ 2167 #define MAX_INQUIRY_SIZE 0xF0 2168 2169 /* 2170 * Macros used by functions to pass a given buf(9S) struct along to the 2171 * next function in the layering chain for further processing. 2172 * 2173 * In the following macros, passing more than three arguments to the called 2174 * routines causes the optimizer for the SPARC compiler to stop doing tail 2175 * call elimination which results in significant performance degradation. 2176 */ 2177 #define SD_BEGIN_IOSTART(index, un, bp) \ 2178 ((*(sd_iostart_chain[index]))(index, un, bp)) 2179 2180 #define SD_BEGIN_IODONE(index, un, bp) \ 2181 ((*(sd_iodone_chain[index]))(index, un, bp)) 2182 2183 #define SD_NEXT_IOSTART(index, un, bp) \ 2184 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2185 2186 #define SD_NEXT_IODONE(index, un, bp) \ 2187 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2188 2189 /* 2190 * Function: _init 2191 * 2192 * Description: This is the driver _init(9E) entry point. 2193 * 2194 * Return Code: Returns the value from mod_install(9F) or 2195 * ddi_soft_state_init(9F) as appropriate. 2196 * 2197 * Context: Called when driver module loaded. 2198 */ 2199 2200 int 2201 _init(void) 2202 { 2203 int err; 2204 2205 /* establish driver name from module name */ 2206 sd_label = mod_modname(&modlinkage); 2207 2208 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2209 SD_MAXUNIT); 2210 2211 if (err != 0) { 2212 return (err); 2213 } 2214 2215 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2216 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2217 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2218 2219 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2220 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2221 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2222 2223 /* 2224 * it's ok to init here even for fibre device 2225 */ 2226 sd_scsi_probe_cache_init(); 2227 2228 /* 2229 * Creating taskq before mod_install ensures that all callers (threads) 2230 * that enter the module after a successfull mod_install encounter 2231 * a valid taskq. 2232 */ 2233 sd_taskq_create(); 2234 2235 err = mod_install(&modlinkage); 2236 if (err != 0) { 2237 /* delete taskq if install fails */ 2238 sd_taskq_delete(); 2239 2240 mutex_destroy(&sd_detach_mutex); 2241 mutex_destroy(&sd_log_mutex); 2242 mutex_destroy(&sd_label_mutex); 2243 2244 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2245 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2246 cv_destroy(&sd_tr.srq_inprocess_cv); 2247 2248 sd_scsi_probe_cache_fini(); 2249 2250 ddi_soft_state_fini(&sd_state); 2251 return (err); 2252 } 2253 2254 return (err); 2255 } 2256 2257 2258 /* 2259 * Function: _fini 2260 * 2261 * Description: This is the driver _fini(9E) entry point. 2262 * 2263 * Return Code: Returns the value from mod_remove(9F) 2264 * 2265 * Context: Called when driver module is unloaded. 2266 */ 2267 2268 int 2269 _fini(void) 2270 { 2271 int err; 2272 2273 if ((err = mod_remove(&modlinkage)) != 0) { 2274 return (err); 2275 } 2276 2277 sd_taskq_delete(); 2278 2279 mutex_destroy(&sd_detach_mutex); 2280 mutex_destroy(&sd_log_mutex); 2281 mutex_destroy(&sd_label_mutex); 2282 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2283 2284 sd_scsi_probe_cache_fini(); 2285 2286 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2287 cv_destroy(&sd_tr.srq_inprocess_cv); 2288 2289 ddi_soft_state_fini(&sd_state); 2290 2291 return (err); 2292 } 2293 2294 2295 /* 2296 * Function: _info 2297 * 2298 * Description: This is the driver _info(9E) entry point. 2299 * 2300 * Arguments: modinfop - pointer to the driver modinfo structure 2301 * 2302 * Return Code: Returns the value from mod_info(9F). 2303 * 2304 * Context: Kernel thread context 2305 */ 2306 2307 int 2308 _info(struct modinfo *modinfop) 2309 { 2310 return (mod_info(&modlinkage, modinfop)); 2311 } 2312 2313 2314 /* 2315 * The following routines implement the driver message logging facility. 2316 * They provide component- and level- based debug output filtering. 2317 * Output may also be restricted to messages for a single instance by 2318 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2319 * to NULL, then messages for all instances are printed. 2320 * 2321 * These routines have been cloned from each other due to the language 2322 * constraints of macros and variable argument list processing. 2323 */ 2324 2325 2326 /* 2327 * Function: sd_log_err 2328 * 2329 * Description: This routine is called by the SD_ERROR macro for debug 2330 * logging of error conditions. 2331 * 2332 * Arguments: comp - driver component being logged 2333 * dev - pointer to driver info structure 2334 * fmt - error string and format to be logged 2335 */ 2336 2337 static void 2338 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2339 { 2340 va_list ap; 2341 dev_info_t *dev; 2342 2343 ASSERT(un != NULL); 2344 dev = SD_DEVINFO(un); 2345 ASSERT(dev != NULL); 2346 2347 /* 2348 * Filter messages based on the global component and level masks. 2349 * Also print if un matches the value of sd_debug_un, or if 2350 * sd_debug_un is set to NULL. 2351 */ 2352 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2353 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2354 mutex_enter(&sd_log_mutex); 2355 va_start(ap, fmt); 2356 (void) vsprintf(sd_log_buf, fmt, ap); 2357 va_end(ap); 2358 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2359 mutex_exit(&sd_log_mutex); 2360 } 2361 #ifdef SD_FAULT_INJECTION 2362 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2363 if (un->sd_injection_mask & comp) { 2364 mutex_enter(&sd_log_mutex); 2365 va_start(ap, fmt); 2366 (void) vsprintf(sd_log_buf, fmt, ap); 2367 va_end(ap); 2368 sd_injection_log(sd_log_buf, un); 2369 mutex_exit(&sd_log_mutex); 2370 } 2371 #endif 2372 } 2373 2374 2375 /* 2376 * Function: sd_log_info 2377 * 2378 * Description: This routine is called by the SD_INFO macro for debug 2379 * logging of general purpose informational conditions. 2380 * 2381 * Arguments: comp - driver component being logged 2382 * dev - pointer to driver info structure 2383 * fmt - info string and format to be logged 2384 */ 2385 2386 static void 2387 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2388 { 2389 va_list ap; 2390 dev_info_t *dev; 2391 2392 ASSERT(un != NULL); 2393 dev = SD_DEVINFO(un); 2394 ASSERT(dev != NULL); 2395 2396 /* 2397 * Filter messages based on the global component and level masks. 2398 * Also print if un matches the value of sd_debug_un, or if 2399 * sd_debug_un is set to NULL. 2400 */ 2401 if ((sd_component_mask & component) && 2402 (sd_level_mask & SD_LOGMASK_INFO) && 2403 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2404 mutex_enter(&sd_log_mutex); 2405 va_start(ap, fmt); 2406 (void) vsprintf(sd_log_buf, fmt, ap); 2407 va_end(ap); 2408 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2409 mutex_exit(&sd_log_mutex); 2410 } 2411 #ifdef SD_FAULT_INJECTION 2412 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2413 if (un->sd_injection_mask & component) { 2414 mutex_enter(&sd_log_mutex); 2415 va_start(ap, fmt); 2416 (void) vsprintf(sd_log_buf, fmt, ap); 2417 va_end(ap); 2418 sd_injection_log(sd_log_buf, un); 2419 mutex_exit(&sd_log_mutex); 2420 } 2421 #endif 2422 } 2423 2424 2425 /* 2426 * Function: sd_log_trace 2427 * 2428 * Description: This routine is called by the SD_TRACE macro for debug 2429 * logging of trace conditions (i.e. function entry/exit). 2430 * 2431 * Arguments: comp - driver component being logged 2432 * dev - pointer to driver info structure 2433 * fmt - trace string and format to be logged 2434 */ 2435 2436 static void 2437 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2438 { 2439 va_list ap; 2440 dev_info_t *dev; 2441 2442 ASSERT(un != NULL); 2443 dev = SD_DEVINFO(un); 2444 ASSERT(dev != NULL); 2445 2446 /* 2447 * Filter messages based on the global component and level masks. 2448 * Also print if un matches the value of sd_debug_un, or if 2449 * sd_debug_un is set to NULL. 2450 */ 2451 if ((sd_component_mask & component) && 2452 (sd_level_mask & SD_LOGMASK_TRACE) && 2453 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2454 mutex_enter(&sd_log_mutex); 2455 va_start(ap, fmt); 2456 (void) vsprintf(sd_log_buf, fmt, ap); 2457 va_end(ap); 2458 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2459 mutex_exit(&sd_log_mutex); 2460 } 2461 #ifdef SD_FAULT_INJECTION 2462 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2463 if (un->sd_injection_mask & component) { 2464 mutex_enter(&sd_log_mutex); 2465 va_start(ap, fmt); 2466 (void) vsprintf(sd_log_buf, fmt, ap); 2467 va_end(ap); 2468 sd_injection_log(sd_log_buf, un); 2469 mutex_exit(&sd_log_mutex); 2470 } 2471 #endif 2472 } 2473 2474 2475 /* 2476 * Function: sdprobe 2477 * 2478 * Description: This is the driver probe(9e) entry point function. 2479 * 2480 * Arguments: devi - opaque device info handle 2481 * 2482 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2483 * DDI_PROBE_FAILURE: If the probe failed. 2484 * DDI_PROBE_PARTIAL: If the instance is not present now, 2485 * but may be present in the future. 2486 */ 2487 2488 static int 2489 sdprobe(dev_info_t *devi) 2490 { 2491 struct scsi_device *devp; 2492 int rval; 2493 int instance; 2494 2495 /* 2496 * if it wasn't for pln, sdprobe could actually be nulldev 2497 * in the "__fibre" case. 2498 */ 2499 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2500 return (DDI_PROBE_DONTCARE); 2501 } 2502 2503 devp = ddi_get_driver_private(devi); 2504 2505 if (devp == NULL) { 2506 /* Ooops... nexus driver is mis-configured... */ 2507 return (DDI_PROBE_FAILURE); 2508 } 2509 2510 instance = ddi_get_instance(devi); 2511 2512 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2513 return (DDI_PROBE_PARTIAL); 2514 } 2515 2516 /* 2517 * Call the SCSA utility probe routine to see if we actually 2518 * have a target at this SCSI nexus. 2519 */ 2520 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2521 case SCSIPROBE_EXISTS: 2522 switch (devp->sd_inq->inq_dtype) { 2523 case DTYPE_DIRECT: 2524 rval = DDI_PROBE_SUCCESS; 2525 break; 2526 case DTYPE_RODIRECT: 2527 /* CDs etc. Can be removable media */ 2528 rval = DDI_PROBE_SUCCESS; 2529 break; 2530 case DTYPE_OPTICAL: 2531 /* 2532 * Rewritable optical driver HP115AA 2533 * Can also be removable media 2534 */ 2535 2536 /* 2537 * Do not attempt to bind to DTYPE_OPTICAL if 2538 * pre solaris 9 sparc sd behavior is required 2539 * 2540 * If first time through and sd_dtype_optical_bind 2541 * has not been set in /etc/system check properties 2542 */ 2543 2544 if (sd_dtype_optical_bind < 0) { 2545 sd_dtype_optical_bind = ddi_prop_get_int 2546 (DDI_DEV_T_ANY, devi, 0, 2547 "optical-device-bind", 1); 2548 } 2549 2550 if (sd_dtype_optical_bind == 0) { 2551 rval = DDI_PROBE_FAILURE; 2552 } else { 2553 rval = DDI_PROBE_SUCCESS; 2554 } 2555 break; 2556 2557 case DTYPE_NOTPRESENT: 2558 default: 2559 rval = DDI_PROBE_FAILURE; 2560 break; 2561 } 2562 break; 2563 default: 2564 rval = DDI_PROBE_PARTIAL; 2565 break; 2566 } 2567 2568 /* 2569 * This routine checks for resource allocation prior to freeing, 2570 * so it will take care of the "smart probing" case where a 2571 * scsi_probe() may or may not have been issued and will *not* 2572 * free previously-freed resources. 2573 */ 2574 scsi_unprobe(devp); 2575 return (rval); 2576 } 2577 2578 2579 /* 2580 * Function: sdinfo 2581 * 2582 * Description: This is the driver getinfo(9e) entry point function. 2583 * Given the device number, return the devinfo pointer from 2584 * the scsi_device structure or the instance number 2585 * associated with the dev_t. 2586 * 2587 * Arguments: dip - pointer to device info structure 2588 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2589 * DDI_INFO_DEVT2INSTANCE) 2590 * arg - driver dev_t 2591 * resultp - user buffer for request response 2592 * 2593 * Return Code: DDI_SUCCESS 2594 * DDI_FAILURE 2595 */ 2596 /* ARGSUSED */ 2597 static int 2598 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2599 { 2600 struct sd_lun *un; 2601 dev_t dev; 2602 int instance; 2603 int error; 2604 2605 switch (infocmd) { 2606 case DDI_INFO_DEVT2DEVINFO: 2607 dev = (dev_t)arg; 2608 instance = SDUNIT(dev); 2609 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2610 return (DDI_FAILURE); 2611 } 2612 *result = (void *) SD_DEVINFO(un); 2613 error = DDI_SUCCESS; 2614 break; 2615 case DDI_INFO_DEVT2INSTANCE: 2616 dev = (dev_t)arg; 2617 instance = SDUNIT(dev); 2618 *result = (void *)(uintptr_t)instance; 2619 error = DDI_SUCCESS; 2620 break; 2621 default: 2622 error = DDI_FAILURE; 2623 } 2624 return (error); 2625 } 2626 2627 /* 2628 * Function: sd_prop_op 2629 * 2630 * Description: This is the driver prop_op(9e) entry point function. 2631 * Return the number of blocks for the partition in question 2632 * or forward the request to the property facilities. 2633 * 2634 * Arguments: dev - device number 2635 * dip - pointer to device info structure 2636 * prop_op - property operator 2637 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2638 * name - pointer to property name 2639 * valuep - pointer or address of the user buffer 2640 * lengthp - property length 2641 * 2642 * Return Code: DDI_PROP_SUCCESS 2643 * DDI_PROP_NOT_FOUND 2644 * DDI_PROP_UNDEFINED 2645 * DDI_PROP_NO_MEMORY 2646 * DDI_PROP_BUF_TOO_SMALL 2647 */ 2648 2649 static int 2650 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2651 char *name, caddr_t valuep, int *lengthp) 2652 { 2653 int instance = ddi_get_instance(dip); 2654 struct sd_lun *un; 2655 uint64_t nblocks64; 2656 2657 /* 2658 * Our dynamic properties are all device specific and size oriented. 2659 * Requests issued under conditions where size is valid are passed 2660 * to ddi_prop_op_nblocks with the size information, otherwise the 2661 * request is passed to ddi_prop_op. Size depends on valid geometry. 2662 */ 2663 un = ddi_get_soft_state(sd_state, instance); 2664 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2665 (un->un_f_geometry_is_valid == FALSE)) { 2666 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2667 name, valuep, lengthp)); 2668 } else { 2669 /* get nblocks value */ 2670 ASSERT(!mutex_owned(SD_MUTEX(un))); 2671 mutex_enter(SD_MUTEX(un)); 2672 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2673 mutex_exit(SD_MUTEX(un)); 2674 2675 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2676 name, valuep, lengthp, nblocks64)); 2677 } 2678 } 2679 2680 /* 2681 * The following functions are for smart probing: 2682 * sd_scsi_probe_cache_init() 2683 * sd_scsi_probe_cache_fini() 2684 * sd_scsi_clear_probe_cache() 2685 * sd_scsi_probe_with_cache() 2686 */ 2687 2688 /* 2689 * Function: sd_scsi_probe_cache_init 2690 * 2691 * Description: Initializes the probe response cache mutex and head pointer. 2692 * 2693 * Context: Kernel thread context 2694 */ 2695 2696 static void 2697 sd_scsi_probe_cache_init(void) 2698 { 2699 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2700 sd_scsi_probe_cache_head = NULL; 2701 } 2702 2703 2704 /* 2705 * Function: sd_scsi_probe_cache_fini 2706 * 2707 * Description: Frees all resources associated with the probe response cache. 2708 * 2709 * Context: Kernel thread context 2710 */ 2711 2712 static void 2713 sd_scsi_probe_cache_fini(void) 2714 { 2715 struct sd_scsi_probe_cache *cp; 2716 struct sd_scsi_probe_cache *ncp; 2717 2718 /* Clean up our smart probing linked list */ 2719 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2720 ncp = cp->next; 2721 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2722 } 2723 sd_scsi_probe_cache_head = NULL; 2724 mutex_destroy(&sd_scsi_probe_cache_mutex); 2725 } 2726 2727 2728 /* 2729 * Function: sd_scsi_clear_probe_cache 2730 * 2731 * Description: This routine clears the probe response cache. This is 2732 * done when open() returns ENXIO so that when deferred 2733 * attach is attempted (possibly after a device has been 2734 * turned on) we will retry the probe. Since we don't know 2735 * which target we failed to open, we just clear the 2736 * entire cache. 2737 * 2738 * Context: Kernel thread context 2739 */ 2740 2741 static void 2742 sd_scsi_clear_probe_cache(void) 2743 { 2744 struct sd_scsi_probe_cache *cp; 2745 int i; 2746 2747 mutex_enter(&sd_scsi_probe_cache_mutex); 2748 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2749 /* 2750 * Reset all entries to SCSIPROBE_EXISTS. This will 2751 * force probing to be performed the next time 2752 * sd_scsi_probe_with_cache is called. 2753 */ 2754 for (i = 0; i < NTARGETS_WIDE; i++) { 2755 cp->cache[i] = SCSIPROBE_EXISTS; 2756 } 2757 } 2758 mutex_exit(&sd_scsi_probe_cache_mutex); 2759 } 2760 2761 2762 /* 2763 * Function: sd_scsi_probe_with_cache 2764 * 2765 * Description: This routine implements support for a scsi device probe 2766 * with cache. The driver maintains a cache of the target 2767 * responses to scsi probes. If we get no response from a 2768 * target during a probe inquiry, we remember that, and we 2769 * avoid additional calls to scsi_probe on non-zero LUNs 2770 * on the same target until the cache is cleared. By doing 2771 * so we avoid the 1/4 sec selection timeout for nonzero 2772 * LUNs. lun0 of a target is always probed. 2773 * 2774 * Arguments: devp - Pointer to a scsi_device(9S) structure 2775 * waitfunc - indicates what the allocator routines should 2776 * do when resources are not available. This value 2777 * is passed on to scsi_probe() when that routine 2778 * is called. 2779 * 2780 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2781 * otherwise the value returned by scsi_probe(9F). 2782 * 2783 * Context: Kernel thread context 2784 */ 2785 2786 static int 2787 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2788 { 2789 struct sd_scsi_probe_cache *cp; 2790 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2791 int lun, tgt; 2792 2793 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2794 SCSI_ADDR_PROP_LUN, 0); 2795 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2796 SCSI_ADDR_PROP_TARGET, -1); 2797 2798 /* Make sure caching enabled and target in range */ 2799 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2800 /* do it the old way (no cache) */ 2801 return (scsi_probe(devp, waitfn)); 2802 } 2803 2804 mutex_enter(&sd_scsi_probe_cache_mutex); 2805 2806 /* Find the cache for this scsi bus instance */ 2807 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2808 if (cp->pdip == pdip) { 2809 break; 2810 } 2811 } 2812 2813 /* If we can't find a cache for this pdip, create one */ 2814 if (cp == NULL) { 2815 int i; 2816 2817 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2818 KM_SLEEP); 2819 cp->pdip = pdip; 2820 cp->next = sd_scsi_probe_cache_head; 2821 sd_scsi_probe_cache_head = cp; 2822 for (i = 0; i < NTARGETS_WIDE; i++) { 2823 cp->cache[i] = SCSIPROBE_EXISTS; 2824 } 2825 } 2826 2827 mutex_exit(&sd_scsi_probe_cache_mutex); 2828 2829 /* Recompute the cache for this target if LUN zero */ 2830 if (lun == 0) { 2831 cp->cache[tgt] = SCSIPROBE_EXISTS; 2832 } 2833 2834 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2835 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2836 return (SCSIPROBE_NORESP); 2837 } 2838 2839 /* Do the actual probe; save & return the result */ 2840 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2841 } 2842 2843 2844 /* 2845 * Function: sd_spin_up_unit 2846 * 2847 * Description: Issues the following commands to spin-up the device: 2848 * START STOP UNIT, and INQUIRY. 2849 * 2850 * Arguments: un - driver soft state (unit) structure 2851 * 2852 * Return Code: 0 - success 2853 * EIO - failure 2854 * EACCES - reservation conflict 2855 * 2856 * Context: Kernel thread context 2857 */ 2858 2859 static int 2860 sd_spin_up_unit(struct sd_lun *un) 2861 { 2862 size_t resid = 0; 2863 int has_conflict = FALSE; 2864 uchar_t *bufaddr; 2865 2866 ASSERT(un != NULL); 2867 2868 /* 2869 * Send a throwaway START UNIT command. 2870 * 2871 * If we fail on this, we don't care presently what precisely 2872 * is wrong. EMC's arrays will also fail this with a check 2873 * condition (0x2/0x4/0x3) if the device is "inactive," but 2874 * we don't want to fail the attach because it may become 2875 * "active" later. 2876 */ 2877 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2878 == EACCES) 2879 has_conflict = TRUE; 2880 2881 /* 2882 * Send another INQUIRY command to the target. This is necessary for 2883 * non-removable media direct access devices because their INQUIRY data 2884 * may not be fully qualified until they are spun up (perhaps via the 2885 * START command above). Note: This seems to be needed for some 2886 * legacy devices only.) The INQUIRY command should succeed even if a 2887 * Reservation Conflict is present. 2888 */ 2889 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2890 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2891 kmem_free(bufaddr, SUN_INQSIZE); 2892 return (EIO); 2893 } 2894 2895 /* 2896 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2897 * Note that this routine does not return a failure here even if the 2898 * INQUIRY command did not return any data. This is a legacy behavior. 2899 */ 2900 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2901 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2902 } 2903 2904 kmem_free(bufaddr, SUN_INQSIZE); 2905 2906 /* If we hit a reservation conflict above, tell the caller. */ 2907 if (has_conflict == TRUE) { 2908 return (EACCES); 2909 } 2910 2911 return (0); 2912 } 2913 2914 #ifdef _LP64 2915 /* 2916 * Function: sd_enable_descr_sense 2917 * 2918 * Description: This routine attempts to select descriptor sense format 2919 * using the Control mode page. Devices that support 64 bit 2920 * LBAs (for >2TB luns) should also implement descriptor 2921 * sense data so we will call this function whenever we see 2922 * a lun larger than 2TB. If for some reason the device 2923 * supports 64 bit LBAs but doesn't support descriptor sense 2924 * presumably the mode select will fail. Everything will 2925 * continue to work normally except that we will not get 2926 * complete sense data for commands that fail with an LBA 2927 * larger than 32 bits. 2928 * 2929 * Arguments: un - driver soft state (unit) structure 2930 * 2931 * Context: Kernel thread context only 2932 */ 2933 2934 static void 2935 sd_enable_descr_sense(struct sd_lun *un) 2936 { 2937 uchar_t *header; 2938 struct mode_control_scsi3 *ctrl_bufp; 2939 size_t buflen; 2940 size_t bd_len; 2941 2942 /* 2943 * Read MODE SENSE page 0xA, Control Mode Page 2944 */ 2945 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2946 sizeof (struct mode_control_scsi3); 2947 header = kmem_zalloc(buflen, KM_SLEEP); 2948 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2949 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2950 SD_ERROR(SD_LOG_COMMON, un, 2951 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2952 goto eds_exit; 2953 } 2954 2955 /* 2956 * Determine size of Block Descriptors in order to locate 2957 * the mode page data. ATAPI devices return 0, SCSI devices 2958 * should return MODE_BLK_DESC_LENGTH. 2959 */ 2960 bd_len = ((struct mode_header *)header)->bdesc_length; 2961 2962 ctrl_bufp = (struct mode_control_scsi3 *) 2963 (header + MODE_HEADER_LENGTH + bd_len); 2964 2965 /* 2966 * Clear PS bit for MODE SELECT 2967 */ 2968 ctrl_bufp->mode_page.ps = 0; 2969 2970 /* 2971 * Set D_SENSE to enable descriptor sense format. 2972 */ 2973 ctrl_bufp->d_sense = 1; 2974 2975 /* 2976 * Use MODE SELECT to commit the change to the D_SENSE bit 2977 */ 2978 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2979 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2980 SD_INFO(SD_LOG_COMMON, un, 2981 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2982 goto eds_exit; 2983 } 2984 2985 eds_exit: 2986 kmem_free(header, buflen); 2987 } 2988 2989 /* 2990 * Function: sd_reenable_dsense_task 2991 * 2992 * Description: Re-enable descriptor sense after device or bus reset 2993 * 2994 * Context: Executes in a taskq() thread context 2995 */ 2996 static void 2997 sd_reenable_dsense_task(void *arg) 2998 { 2999 struct sd_lun *un = arg; 3000 3001 ASSERT(un != NULL); 3002 sd_enable_descr_sense(un); 3003 } 3004 #endif /* _LP64 */ 3005 3006 /* 3007 * Function: sd_set_mmc_caps 3008 * 3009 * Description: This routine determines if the device is MMC compliant and if 3010 * the device supports CDDA via a mode sense of the CDVD 3011 * capabilities mode page. Also checks if the device is a 3012 * dvdram writable device. 3013 * 3014 * Arguments: un - driver soft state (unit) structure 3015 * 3016 * Context: Kernel thread context only 3017 */ 3018 3019 static void 3020 sd_set_mmc_caps(struct sd_lun *un) 3021 { 3022 struct mode_header_grp2 *sense_mhp; 3023 uchar_t *sense_page; 3024 caddr_t buf; 3025 int bd_len; 3026 int status; 3027 struct uscsi_cmd com; 3028 int rtn; 3029 uchar_t *out_data_rw, *out_data_hd; 3030 uchar_t *rqbuf_rw, *rqbuf_hd; 3031 3032 ASSERT(un != NULL); 3033 3034 /* 3035 * The flags which will be set in this function are - mmc compliant, 3036 * dvdram writable device, cdda support. Initialize them to FALSE 3037 * and if a capability is detected - it will be set to TRUE. 3038 */ 3039 un->un_f_mmc_cap = FALSE; 3040 un->un_f_dvdram_writable_device = FALSE; 3041 un->un_f_cfg_cdda = FALSE; 3042 3043 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3044 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3045 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3046 3047 if (status != 0) { 3048 /* command failed; just return */ 3049 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3050 return; 3051 } 3052 /* 3053 * If the mode sense request for the CDROM CAPABILITIES 3054 * page (0x2A) succeeds the device is assumed to be MMC. 3055 */ 3056 un->un_f_mmc_cap = TRUE; 3057 3058 /* Get to the page data */ 3059 sense_mhp = (struct mode_header_grp2 *)buf; 3060 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3061 sense_mhp->bdesc_length_lo; 3062 if (bd_len > MODE_BLK_DESC_LENGTH) { 3063 /* 3064 * We did not get back the expected block descriptor 3065 * length so we cannot determine if the device supports 3066 * CDDA. However, we still indicate the device is MMC 3067 * according to the successful response to the page 3068 * 0x2A mode sense request. 3069 */ 3070 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3071 "sd_set_mmc_caps: Mode Sense returned " 3072 "invalid block descriptor length\n"); 3073 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3074 return; 3075 } 3076 3077 /* See if read CDDA is supported */ 3078 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3079 bd_len); 3080 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3081 3082 /* See if writing DVD RAM is supported. */ 3083 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3084 if (un->un_f_dvdram_writable_device == TRUE) { 3085 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3086 return; 3087 } 3088 3089 /* 3090 * If the device presents DVD or CD capabilities in the mode 3091 * page, we can return here since a RRD will not have 3092 * these capabilities. 3093 */ 3094 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3095 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3096 return; 3097 } 3098 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3099 3100 /* 3101 * If un->un_f_dvdram_writable_device is still FALSE, 3102 * check for a Removable Rigid Disk (RRD). A RRD 3103 * device is identified by the features RANDOM_WRITABLE and 3104 * HARDWARE_DEFECT_MANAGEMENT. 3105 */ 3106 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3107 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3108 3109 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3110 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3111 RANDOM_WRITABLE); 3112 if (rtn != 0) { 3113 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3114 kmem_free(rqbuf_rw, SENSE_LENGTH); 3115 return; 3116 } 3117 3118 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3119 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3120 3121 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3122 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3123 HARDWARE_DEFECT_MANAGEMENT); 3124 if (rtn == 0) { 3125 /* 3126 * We have good information, check for random writable 3127 * and hardware defect features. 3128 */ 3129 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3130 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3131 un->un_f_dvdram_writable_device = TRUE; 3132 } 3133 } 3134 3135 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3136 kmem_free(rqbuf_rw, SENSE_LENGTH); 3137 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3138 kmem_free(rqbuf_hd, SENSE_LENGTH); 3139 } 3140 3141 /* 3142 * Function: sd_check_for_writable_cd 3143 * 3144 * Description: This routine determines if the media in the device is 3145 * writable or not. It uses the get configuration command (0x46) 3146 * to determine if the media is writable 3147 * 3148 * Arguments: un - driver soft state (unit) structure 3149 * 3150 * Context: Never called at interrupt context. 3151 */ 3152 3153 static void 3154 sd_check_for_writable_cd(struct sd_lun *un) 3155 { 3156 struct uscsi_cmd com; 3157 uchar_t *out_data; 3158 uchar_t *rqbuf; 3159 int rtn; 3160 uchar_t *out_data_rw, *out_data_hd; 3161 uchar_t *rqbuf_rw, *rqbuf_hd; 3162 struct mode_header_grp2 *sense_mhp; 3163 uchar_t *sense_page; 3164 caddr_t buf; 3165 int bd_len; 3166 int status; 3167 3168 ASSERT(un != NULL); 3169 ASSERT(mutex_owned(SD_MUTEX(un))); 3170 3171 /* 3172 * Initialize the writable media to false, if configuration info. 3173 * tells us otherwise then only we will set it. 3174 */ 3175 un->un_f_mmc_writable_media = FALSE; 3176 mutex_exit(SD_MUTEX(un)); 3177 3178 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3179 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3180 3181 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3182 out_data, SD_PROFILE_HEADER_LEN); 3183 3184 mutex_enter(SD_MUTEX(un)); 3185 if (rtn == 0) { 3186 /* 3187 * We have good information, check for writable DVD. 3188 */ 3189 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3190 un->un_f_mmc_writable_media = TRUE; 3191 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3192 kmem_free(rqbuf, SENSE_LENGTH); 3193 return; 3194 } 3195 } 3196 3197 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3198 kmem_free(rqbuf, SENSE_LENGTH); 3199 3200 /* 3201 * Determine if this is a RRD type device. 3202 */ 3203 mutex_exit(SD_MUTEX(un)); 3204 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3205 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3206 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3207 mutex_enter(SD_MUTEX(un)); 3208 if (status != 0) { 3209 /* command failed; just return */ 3210 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3211 return; 3212 } 3213 3214 /* Get to the page data */ 3215 sense_mhp = (struct mode_header_grp2 *)buf; 3216 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3217 if (bd_len > MODE_BLK_DESC_LENGTH) { 3218 /* 3219 * We did not get back the expected block descriptor length so 3220 * we cannot check the mode page. 3221 */ 3222 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3223 "sd_check_for_writable_cd: Mode Sense returned " 3224 "invalid block descriptor length\n"); 3225 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3226 return; 3227 } 3228 3229 /* 3230 * If the device presents DVD or CD capabilities in the mode 3231 * page, we can return here since a RRD device will not have 3232 * these capabilities. 3233 */ 3234 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3235 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3236 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3237 return; 3238 } 3239 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3240 3241 /* 3242 * If un->un_f_mmc_writable_media is still FALSE, 3243 * check for RRD type media. A RRD device is identified 3244 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3245 */ 3246 mutex_exit(SD_MUTEX(un)); 3247 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3248 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3249 3250 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3251 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3252 RANDOM_WRITABLE); 3253 if (rtn != 0) { 3254 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3255 kmem_free(rqbuf_rw, SENSE_LENGTH); 3256 mutex_enter(SD_MUTEX(un)); 3257 return; 3258 } 3259 3260 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3261 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3262 3263 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3264 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3265 HARDWARE_DEFECT_MANAGEMENT); 3266 mutex_enter(SD_MUTEX(un)); 3267 if (rtn == 0) { 3268 /* 3269 * We have good information, check for random writable 3270 * and hardware defect features as current. 3271 */ 3272 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3273 (out_data_rw[10] & 0x1) && 3274 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3275 (out_data_hd[10] & 0x1)) { 3276 un->un_f_mmc_writable_media = TRUE; 3277 } 3278 } 3279 3280 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3281 kmem_free(rqbuf_rw, SENSE_LENGTH); 3282 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3283 kmem_free(rqbuf_hd, SENSE_LENGTH); 3284 } 3285 3286 /* 3287 * Function: sd_read_unit_properties 3288 * 3289 * Description: The following implements a property lookup mechanism. 3290 * Properties for particular disks (keyed on vendor, model 3291 * and rev numbers) are sought in the sd.conf file via 3292 * sd_process_sdconf_file(), and if not found there, are 3293 * looked for in a list hardcoded in this driver via 3294 * sd_process_sdconf_table() Once located the properties 3295 * are used to update the driver unit structure. 3296 * 3297 * Arguments: un - driver soft state (unit) structure 3298 */ 3299 3300 static void 3301 sd_read_unit_properties(struct sd_lun *un) 3302 { 3303 /* 3304 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3305 * the "sd-config-list" property (from the sd.conf file) or if 3306 * there was not a match for the inquiry vid/pid. If this event 3307 * occurs the static driver configuration table is searched for 3308 * a match. 3309 */ 3310 ASSERT(un != NULL); 3311 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3312 sd_process_sdconf_table(un); 3313 } 3314 3315 /* check for LSI device */ 3316 sd_is_lsi(un); 3317 3318 3319 } 3320 3321 3322 /* 3323 * Function: sd_process_sdconf_file 3324 * 3325 * Description: Use ddi_getlongprop to obtain the properties from the 3326 * driver's config file (ie, sd.conf) and update the driver 3327 * soft state structure accordingly. 3328 * 3329 * Arguments: un - driver soft state (unit) structure 3330 * 3331 * Return Code: SD_SUCCESS - The properties were successfully set according 3332 * to the driver configuration file. 3333 * SD_FAILURE - The driver config list was not obtained or 3334 * there was no vid/pid match. This indicates that 3335 * the static config table should be used. 3336 * 3337 * The config file has a property, "sd-config-list", which consists of 3338 * one or more duplets as follows: 3339 * 3340 * sd-config-list= 3341 * <duplet>, 3342 * [<duplet>,] 3343 * [<duplet>]; 3344 * 3345 * The structure of each duplet is as follows: 3346 * 3347 * <duplet>:= <vid+pid>,<data-property-name_list> 3348 * 3349 * The first entry of the duplet is the device ID string (the concatenated 3350 * vid & pid; not to be confused with a device_id). This is defined in 3351 * the same way as in the sd_disk_table. 3352 * 3353 * The second part of the duplet is a string that identifies a 3354 * data-property-name-list. The data-property-name-list is defined as 3355 * follows: 3356 * 3357 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3358 * 3359 * The syntax of <data-property-name> depends on the <version> field. 3360 * 3361 * If version = SD_CONF_VERSION_1 we have the following syntax: 3362 * 3363 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3364 * 3365 * where the prop0 value will be used to set prop0 if bit0 set in the 3366 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3367 * 3368 */ 3369 3370 static int 3371 sd_process_sdconf_file(struct sd_lun *un) 3372 { 3373 char *config_list = NULL; 3374 int config_list_len; 3375 int len; 3376 int dupletlen = 0; 3377 char *vidptr; 3378 int vidlen; 3379 char *dnlist_ptr; 3380 char *dataname_ptr; 3381 int dnlist_len; 3382 int dataname_len; 3383 int *data_list; 3384 int data_list_len; 3385 int rval = SD_FAILURE; 3386 int i; 3387 3388 ASSERT(un != NULL); 3389 3390 /* Obtain the configuration list associated with the .conf file */ 3391 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3392 sd_config_list, (caddr_t)&config_list, &config_list_len) 3393 != DDI_PROP_SUCCESS) { 3394 return (SD_FAILURE); 3395 } 3396 3397 /* 3398 * Compare vids in each duplet to the inquiry vid - if a match is 3399 * made, get the data value and update the soft state structure 3400 * accordingly. 3401 * 3402 * Note: This algorithm is complex and difficult to maintain. It should 3403 * be replaced with a more robust implementation. 3404 */ 3405 for (len = config_list_len, vidptr = config_list; len > 0; 3406 vidptr += dupletlen, len -= dupletlen) { 3407 /* 3408 * Note: The assumption here is that each vid entry is on 3409 * a unique line from its associated duplet. 3410 */ 3411 vidlen = dupletlen = (int)strlen(vidptr); 3412 if ((vidlen == 0) || 3413 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3414 dupletlen++; 3415 continue; 3416 } 3417 3418 /* 3419 * dnlist contains 1 or more blank separated 3420 * data-property-name entries 3421 */ 3422 dnlist_ptr = vidptr + vidlen + 1; 3423 dnlist_len = (int)strlen(dnlist_ptr); 3424 dupletlen += dnlist_len + 2; 3425 3426 /* 3427 * Set a pointer for the first data-property-name 3428 * entry in the list 3429 */ 3430 dataname_ptr = dnlist_ptr; 3431 dataname_len = 0; 3432 3433 /* 3434 * Loop through all data-property-name entries in the 3435 * data-property-name-list setting the properties for each. 3436 */ 3437 while (dataname_len < dnlist_len) { 3438 int version; 3439 3440 /* 3441 * Determine the length of the current 3442 * data-property-name entry by indexing until a 3443 * blank or NULL is encountered. When the space is 3444 * encountered reset it to a NULL for compliance 3445 * with ddi_getlongprop(). 3446 */ 3447 for (i = 0; ((dataname_ptr[i] != ' ') && 3448 (dataname_ptr[i] != '\0')); i++) { 3449 ; 3450 } 3451 3452 dataname_len += i; 3453 /* If not null terminated, Make it so */ 3454 if (dataname_ptr[i] == ' ') { 3455 dataname_ptr[i] = '\0'; 3456 } 3457 dataname_len++; 3458 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3459 "sd_process_sdconf_file: disk:%s, data:%s\n", 3460 vidptr, dataname_ptr); 3461 3462 /* Get the data list */ 3463 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3464 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3465 != DDI_PROP_SUCCESS) { 3466 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3467 "sd_process_sdconf_file: data property (%s)" 3468 " has no value\n", dataname_ptr); 3469 dataname_ptr = dnlist_ptr + dataname_len; 3470 continue; 3471 } 3472 3473 version = data_list[0]; 3474 3475 if (version == SD_CONF_VERSION_1) { 3476 sd_tunables values; 3477 3478 /* Set the properties */ 3479 if (sd_chk_vers1_data(un, data_list[1], 3480 &data_list[2], data_list_len, dataname_ptr) 3481 == SD_SUCCESS) { 3482 sd_get_tunables_from_conf(un, 3483 data_list[1], &data_list[2], 3484 &values); 3485 sd_set_vers1_properties(un, 3486 data_list[1], &values); 3487 rval = SD_SUCCESS; 3488 } else { 3489 rval = SD_FAILURE; 3490 } 3491 } else { 3492 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3493 "data property %s version 0x%x is invalid.", 3494 dataname_ptr, version); 3495 rval = SD_FAILURE; 3496 } 3497 kmem_free(data_list, data_list_len); 3498 dataname_ptr = dnlist_ptr + dataname_len; 3499 } 3500 } 3501 3502 /* free up the memory allocated by ddi_getlongprop */ 3503 if (config_list) { 3504 kmem_free(config_list, config_list_len); 3505 } 3506 3507 return (rval); 3508 } 3509 3510 /* 3511 * Function: sd_get_tunables_from_conf() 3512 * 3513 * 3514 * This function reads the data list from the sd.conf file and pulls 3515 * the values that can have numeric values as arguments and places 3516 * the values in the apropriate sd_tunables member. 3517 * Since the order of the data list members varies across platforms 3518 * This function reads them from the data list in a platform specific 3519 * order and places them into the correct sd_tunable member that is 3520 * a consistant across all platforms. 3521 */ 3522 static void 3523 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3524 sd_tunables *values) 3525 { 3526 int i; 3527 int mask; 3528 3529 bzero(values, sizeof (sd_tunables)); 3530 3531 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3532 3533 mask = 1 << i; 3534 if (mask > flags) { 3535 break; 3536 } 3537 3538 switch (mask & flags) { 3539 case 0: /* This mask bit not set in flags */ 3540 continue; 3541 case SD_CONF_BSET_THROTTLE: 3542 values->sdt_throttle = data_list[i]; 3543 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3544 "sd_get_tunables_from_conf: throttle = %d\n", 3545 values->sdt_throttle); 3546 break; 3547 case SD_CONF_BSET_CTYPE: 3548 values->sdt_ctype = data_list[i]; 3549 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3550 "sd_get_tunables_from_conf: ctype = %d\n", 3551 values->sdt_ctype); 3552 break; 3553 case SD_CONF_BSET_NRR_COUNT: 3554 values->sdt_not_rdy_retries = data_list[i]; 3555 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3556 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3557 values->sdt_not_rdy_retries); 3558 break; 3559 case SD_CONF_BSET_BSY_RETRY_COUNT: 3560 values->sdt_busy_retries = data_list[i]; 3561 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3562 "sd_get_tunables_from_conf: busy_retries = %d\n", 3563 values->sdt_busy_retries); 3564 break; 3565 case SD_CONF_BSET_RST_RETRIES: 3566 values->sdt_reset_retries = data_list[i]; 3567 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3568 "sd_get_tunables_from_conf: reset_retries = %d\n", 3569 values->sdt_reset_retries); 3570 break; 3571 case SD_CONF_BSET_RSV_REL_TIME: 3572 values->sdt_reserv_rel_time = data_list[i]; 3573 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3574 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3575 values->sdt_reserv_rel_time); 3576 break; 3577 case SD_CONF_BSET_MIN_THROTTLE: 3578 values->sdt_min_throttle = data_list[i]; 3579 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3580 "sd_get_tunables_from_conf: min_throttle = %d\n", 3581 values->sdt_min_throttle); 3582 break; 3583 case SD_CONF_BSET_DISKSORT_DISABLED: 3584 values->sdt_disk_sort_dis = data_list[i]; 3585 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3586 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3587 values->sdt_disk_sort_dis); 3588 break; 3589 case SD_CONF_BSET_LUN_RESET_ENABLED: 3590 values->sdt_lun_reset_enable = data_list[i]; 3591 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3592 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3593 "\n", values->sdt_lun_reset_enable); 3594 break; 3595 } 3596 } 3597 } 3598 3599 /* 3600 * Function: sd_process_sdconf_table 3601 * 3602 * Description: Search the static configuration table for a match on the 3603 * inquiry vid/pid and update the driver soft state structure 3604 * according to the table property values for the device. 3605 * 3606 * The form of a configuration table entry is: 3607 * <vid+pid>,<flags>,<property-data> 3608 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3609 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3610 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3611 * 3612 * Arguments: un - driver soft state (unit) structure 3613 */ 3614 3615 static void 3616 sd_process_sdconf_table(struct sd_lun *un) 3617 { 3618 char *id = NULL; 3619 int table_index; 3620 int idlen; 3621 3622 ASSERT(un != NULL); 3623 for (table_index = 0; table_index < sd_disk_table_size; 3624 table_index++) { 3625 id = sd_disk_table[table_index].device_id; 3626 idlen = strlen(id); 3627 if (idlen == 0) { 3628 continue; 3629 } 3630 3631 /* 3632 * The static configuration table currently does not 3633 * implement version 10 properties. Additionally, 3634 * multiple data-property-name entries are not 3635 * implemented in the static configuration table. 3636 */ 3637 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3638 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3639 "sd_process_sdconf_table: disk %s\n", id); 3640 sd_set_vers1_properties(un, 3641 sd_disk_table[table_index].flags, 3642 sd_disk_table[table_index].properties); 3643 break; 3644 } 3645 } 3646 } 3647 3648 3649 /* 3650 * Function: sd_sdconf_id_match 3651 * 3652 * Description: This local function implements a case sensitive vid/pid 3653 * comparison as well as the boundary cases of wild card and 3654 * multiple blanks. 3655 * 3656 * Note: An implicit assumption made here is that the scsi 3657 * inquiry structure will always keep the vid, pid and 3658 * revision strings in consecutive sequence, so they can be 3659 * read as a single string. If this assumption is not the 3660 * case, a separate string, to be used for the check, needs 3661 * to be built with these strings concatenated. 3662 * 3663 * Arguments: un - driver soft state (unit) structure 3664 * id - table or config file vid/pid 3665 * idlen - length of the vid/pid (bytes) 3666 * 3667 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3668 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3669 */ 3670 3671 static int 3672 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3673 { 3674 struct scsi_inquiry *sd_inq; 3675 int rval = SD_SUCCESS; 3676 3677 ASSERT(un != NULL); 3678 sd_inq = un->un_sd->sd_inq; 3679 ASSERT(id != NULL); 3680 3681 /* 3682 * We use the inq_vid as a pointer to a buffer containing the 3683 * vid and pid and use the entire vid/pid length of the table 3684 * entry for the comparison. This works because the inq_pid 3685 * data member follows inq_vid in the scsi_inquiry structure. 3686 */ 3687 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3688 /* 3689 * The user id string is compared to the inquiry vid/pid 3690 * using a case insensitive comparison and ignoring 3691 * multiple spaces. 3692 */ 3693 rval = sd_blank_cmp(un, id, idlen); 3694 if (rval != SD_SUCCESS) { 3695 /* 3696 * User id strings that start and end with a "*" 3697 * are a special case. These do not have a 3698 * specific vendor, and the product string can 3699 * appear anywhere in the 16 byte PID portion of 3700 * the inquiry data. This is a simple strstr() 3701 * type search for the user id in the inquiry data. 3702 */ 3703 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3704 char *pidptr = &id[1]; 3705 int i; 3706 int j; 3707 int pidstrlen = idlen - 2; 3708 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3709 pidstrlen; 3710 3711 if (j < 0) { 3712 return (SD_FAILURE); 3713 } 3714 for (i = 0; i < j; i++) { 3715 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3716 pidptr, pidstrlen) == 0) { 3717 rval = SD_SUCCESS; 3718 break; 3719 } 3720 } 3721 } 3722 } 3723 } 3724 return (rval); 3725 } 3726 3727 3728 /* 3729 * Function: sd_blank_cmp 3730 * 3731 * Description: If the id string starts and ends with a space, treat 3732 * multiple consecutive spaces as equivalent to a single 3733 * space. For example, this causes a sd_disk_table entry 3734 * of " NEC CDROM " to match a device's id string of 3735 * "NEC CDROM". 3736 * 3737 * Note: The success exit condition for this routine is if 3738 * the pointer to the table entry is '\0' and the cnt of 3739 * the inquiry length is zero. This will happen if the inquiry 3740 * string returned by the device is padded with spaces to be 3741 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3742 * SCSI spec states that the inquiry string is to be padded with 3743 * spaces. 3744 * 3745 * Arguments: un - driver soft state (unit) structure 3746 * id - table or config file vid/pid 3747 * idlen - length of the vid/pid (bytes) 3748 * 3749 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3750 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3751 */ 3752 3753 static int 3754 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3755 { 3756 char *p1; 3757 char *p2; 3758 int cnt; 3759 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3760 sizeof (SD_INQUIRY(un)->inq_pid); 3761 3762 ASSERT(un != NULL); 3763 p2 = un->un_sd->sd_inq->inq_vid; 3764 ASSERT(id != NULL); 3765 p1 = id; 3766 3767 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3768 /* 3769 * Note: string p1 is terminated by a NUL but string p2 3770 * isn't. The end of p2 is determined by cnt. 3771 */ 3772 for (;;) { 3773 /* skip over any extra blanks in both strings */ 3774 while ((*p1 != '\0') && (*p1 == ' ')) { 3775 p1++; 3776 } 3777 while ((cnt != 0) && (*p2 == ' ')) { 3778 p2++; 3779 cnt--; 3780 } 3781 3782 /* compare the two strings */ 3783 if ((cnt == 0) || 3784 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3785 break; 3786 } 3787 while ((cnt > 0) && 3788 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3789 p1++; 3790 p2++; 3791 cnt--; 3792 } 3793 } 3794 } 3795 3796 /* return SD_SUCCESS if both strings match */ 3797 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3798 } 3799 3800 3801 /* 3802 * Function: sd_chk_vers1_data 3803 * 3804 * Description: Verify the version 1 device properties provided by the 3805 * user via the configuration file 3806 * 3807 * Arguments: un - driver soft state (unit) structure 3808 * flags - integer mask indicating properties to be set 3809 * prop_list - integer list of property values 3810 * list_len - length of user provided data 3811 * 3812 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3813 * SD_FAILURE - Indicates the user provided data is invalid 3814 */ 3815 3816 static int 3817 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3818 int list_len, char *dataname_ptr) 3819 { 3820 int i; 3821 int mask = 1; 3822 int index = 0; 3823 3824 ASSERT(un != NULL); 3825 3826 /* Check for a NULL property name and list */ 3827 if (dataname_ptr == NULL) { 3828 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3829 "sd_chk_vers1_data: NULL data property name."); 3830 return (SD_FAILURE); 3831 } 3832 if (prop_list == NULL) { 3833 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3834 "sd_chk_vers1_data: %s NULL data property list.", 3835 dataname_ptr); 3836 return (SD_FAILURE); 3837 } 3838 3839 /* Display a warning if undefined bits are set in the flags */ 3840 if (flags & ~SD_CONF_BIT_MASK) { 3841 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3842 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3843 "Properties not set.", 3844 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3845 return (SD_FAILURE); 3846 } 3847 3848 /* 3849 * Verify the length of the list by identifying the highest bit set 3850 * in the flags and validating that the property list has a length 3851 * up to the index of this bit. 3852 */ 3853 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3854 if (flags & mask) { 3855 index++; 3856 } 3857 mask = 1 << i; 3858 } 3859 if ((list_len / sizeof (int)) < (index + 2)) { 3860 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3861 "sd_chk_vers1_data: " 3862 "Data property list %s size is incorrect. " 3863 "Properties not set.", dataname_ptr); 3864 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3865 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3866 return (SD_FAILURE); 3867 } 3868 return (SD_SUCCESS); 3869 } 3870 3871 3872 /* 3873 * Function: sd_set_vers1_properties 3874 * 3875 * Description: Set version 1 device properties based on a property list 3876 * retrieved from the driver configuration file or static 3877 * configuration table. Version 1 properties have the format: 3878 * 3879 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3880 * 3881 * where the prop0 value will be used to set prop0 if bit0 3882 * is set in the flags 3883 * 3884 * Arguments: un - driver soft state (unit) structure 3885 * flags - integer mask indicating properties to be set 3886 * prop_list - integer list of property values 3887 */ 3888 3889 static void 3890 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3891 { 3892 ASSERT(un != NULL); 3893 3894 /* 3895 * Set the flag to indicate cache is to be disabled. An attempt 3896 * to disable the cache via sd_cache_control() will be made 3897 * later during attach once the basic initialization is complete. 3898 */ 3899 if (flags & SD_CONF_BSET_NOCACHE) { 3900 un->un_f_opt_disable_cache = TRUE; 3901 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3902 "sd_set_vers1_properties: caching disabled flag set\n"); 3903 } 3904 3905 /* CD-specific configuration parameters */ 3906 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3907 un->un_f_cfg_playmsf_bcd = TRUE; 3908 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3909 "sd_set_vers1_properties: playmsf_bcd set\n"); 3910 } 3911 if (flags & SD_CONF_BSET_READSUB_BCD) { 3912 un->un_f_cfg_readsub_bcd = TRUE; 3913 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3914 "sd_set_vers1_properties: readsub_bcd set\n"); 3915 } 3916 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3917 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3918 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3919 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3920 } 3921 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3922 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3923 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3924 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3925 } 3926 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3927 un->un_f_cfg_no_read_header = TRUE; 3928 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3929 "sd_set_vers1_properties: no_read_header set\n"); 3930 } 3931 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3932 un->un_f_cfg_read_cd_xd4 = TRUE; 3933 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3934 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3935 } 3936 3937 /* Support for devices which do not have valid/unique serial numbers */ 3938 if (flags & SD_CONF_BSET_FAB_DEVID) { 3939 un->un_f_opt_fab_devid = TRUE; 3940 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3941 "sd_set_vers1_properties: fab_devid bit set\n"); 3942 } 3943 3944 /* Support for user throttle configuration */ 3945 if (flags & SD_CONF_BSET_THROTTLE) { 3946 ASSERT(prop_list != NULL); 3947 un->un_saved_throttle = un->un_throttle = 3948 prop_list->sdt_throttle; 3949 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3950 "sd_set_vers1_properties: throttle set to %d\n", 3951 prop_list->sdt_throttle); 3952 } 3953 3954 /* Set the per disk retry count according to the conf file or table. */ 3955 if (flags & SD_CONF_BSET_NRR_COUNT) { 3956 ASSERT(prop_list != NULL); 3957 if (prop_list->sdt_not_rdy_retries) { 3958 un->un_notready_retry_count = 3959 prop_list->sdt_not_rdy_retries; 3960 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3961 "sd_set_vers1_properties: not ready retry count" 3962 " set to %d\n", un->un_notready_retry_count); 3963 } 3964 } 3965 3966 /* The controller type is reported for generic disk driver ioctls */ 3967 if (flags & SD_CONF_BSET_CTYPE) { 3968 ASSERT(prop_list != NULL); 3969 switch (prop_list->sdt_ctype) { 3970 case CTYPE_CDROM: 3971 un->un_ctype = prop_list->sdt_ctype; 3972 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3973 "sd_set_vers1_properties: ctype set to " 3974 "CTYPE_CDROM\n"); 3975 break; 3976 case CTYPE_CCS: 3977 un->un_ctype = prop_list->sdt_ctype; 3978 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3979 "sd_set_vers1_properties: ctype set to " 3980 "CTYPE_CCS\n"); 3981 break; 3982 case CTYPE_ROD: /* RW optical */ 3983 un->un_ctype = prop_list->sdt_ctype; 3984 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3985 "sd_set_vers1_properties: ctype set to " 3986 "CTYPE_ROD\n"); 3987 break; 3988 default: 3989 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3990 "sd_set_vers1_properties: Could not set " 3991 "invalid ctype value (%d)", 3992 prop_list->sdt_ctype); 3993 } 3994 } 3995 3996 /* Purple failover timeout */ 3997 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3998 ASSERT(prop_list != NULL); 3999 un->un_busy_retry_count = 4000 prop_list->sdt_busy_retries; 4001 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4002 "sd_set_vers1_properties: " 4003 "busy retry count set to %d\n", 4004 un->un_busy_retry_count); 4005 } 4006 4007 /* Purple reset retry count */ 4008 if (flags & SD_CONF_BSET_RST_RETRIES) { 4009 ASSERT(prop_list != NULL); 4010 un->un_reset_retry_count = 4011 prop_list->sdt_reset_retries; 4012 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4013 "sd_set_vers1_properties: " 4014 "reset retry count set to %d\n", 4015 un->un_reset_retry_count); 4016 } 4017 4018 /* Purple reservation release timeout */ 4019 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4020 ASSERT(prop_list != NULL); 4021 un->un_reserve_release_time = 4022 prop_list->sdt_reserv_rel_time; 4023 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4024 "sd_set_vers1_properties: " 4025 "reservation release timeout set to %d\n", 4026 un->un_reserve_release_time); 4027 } 4028 4029 /* 4030 * Driver flag telling the driver to verify that no commands are pending 4031 * for a device before issuing a Test Unit Ready. This is a workaround 4032 * for a firmware bug in some Seagate eliteI drives. 4033 */ 4034 if (flags & SD_CONF_BSET_TUR_CHECK) { 4035 un->un_f_cfg_tur_check = TRUE; 4036 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4037 "sd_set_vers1_properties: tur queue check set\n"); 4038 } 4039 4040 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4041 un->un_min_throttle = prop_list->sdt_min_throttle; 4042 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4043 "sd_set_vers1_properties: min throttle set to %d\n", 4044 un->un_min_throttle); 4045 } 4046 4047 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4048 un->un_f_disksort_disabled = 4049 (prop_list->sdt_disk_sort_dis != 0) ? 4050 TRUE : FALSE; 4051 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4052 "sd_set_vers1_properties: disksort disabled " 4053 "flag set to %d\n", 4054 prop_list->sdt_disk_sort_dis); 4055 } 4056 4057 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4058 un->un_f_lun_reset_enabled = 4059 (prop_list->sdt_lun_reset_enable != 0) ? 4060 TRUE : FALSE; 4061 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4062 "sd_set_vers1_properties: lun reset enabled " 4063 "flag set to %d\n", 4064 prop_list->sdt_lun_reset_enable); 4065 } 4066 4067 /* 4068 * Validate the throttle values. 4069 * If any of the numbers are invalid, set everything to defaults. 4070 */ 4071 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4072 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4073 (un->un_min_throttle > un->un_throttle)) { 4074 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4075 un->un_min_throttle = sd_min_throttle; 4076 } 4077 } 4078 4079 /* 4080 * Function: sd_is_lsi() 4081 * 4082 * Description: Check for lsi devices, step throught the static device 4083 * table to match vid/pid. 4084 * 4085 * Args: un - ptr to sd_lun 4086 * 4087 * Notes: When creating new LSI property, need to add the new LSI property 4088 * to this function. 4089 */ 4090 static void 4091 sd_is_lsi(struct sd_lun *un) 4092 { 4093 char *id = NULL; 4094 int table_index; 4095 int idlen; 4096 void *prop; 4097 4098 ASSERT(un != NULL); 4099 for (table_index = 0; table_index < sd_disk_table_size; 4100 table_index++) { 4101 id = sd_disk_table[table_index].device_id; 4102 idlen = strlen(id); 4103 if (idlen == 0) { 4104 continue; 4105 } 4106 4107 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4108 prop = sd_disk_table[table_index].properties; 4109 if (prop == &lsi_properties || 4110 prop == &lsi_oem_properties || 4111 prop == &lsi_properties_scsi || 4112 prop == &symbios_properties) { 4113 un->un_f_cfg_is_lsi = TRUE; 4114 } 4115 break; 4116 } 4117 } 4118 } 4119 4120 4121 /* 4122 * The following routines support reading and interpretation of disk labels, 4123 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4124 * fdisk tables. 4125 */ 4126 4127 /* 4128 * Function: sd_validate_geometry 4129 * 4130 * Description: Read the label from the disk (if present). Update the unit's 4131 * geometry and vtoc information from the data in the label. 4132 * Verify that the label is valid. 4133 * 4134 * Arguments: un - driver soft state (unit) structure 4135 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4136 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4137 * to use the USCSI "direct" chain and bypass the normal 4138 * command waitq. 4139 * 4140 * Return Code: 0 - Successful completion 4141 * EINVAL - Invalid value in un->un_tgt_blocksize or 4142 * un->un_blockcount; or label on disk is corrupted 4143 * or unreadable. 4144 * EACCES - Reservation conflict at the device. 4145 * ENOMEM - Resource allocation error 4146 * ENOTSUP - geometry not applicable 4147 * 4148 * Context: Kernel thread only (can sleep). 4149 */ 4150 4151 static int 4152 sd_validate_geometry(struct sd_lun *un, int path_flag) 4153 { 4154 static char labelstring[128]; 4155 static char buf[256]; 4156 char *label = NULL; 4157 int label_error = 0; 4158 int gvalid = un->un_f_geometry_is_valid; 4159 int lbasize; 4160 uint_t capacity; 4161 int count; 4162 4163 ASSERT(un != NULL); 4164 ASSERT(mutex_owned(SD_MUTEX(un))); 4165 4166 /* 4167 * If the required values are not valid, then try getting them 4168 * once via read capacity. If that fails, then fail this call. 4169 * This is necessary with the new mpxio failover behavior in 4170 * the T300 where we can get an attach for the inactive path 4171 * before the active path. The inactive path fails commands with 4172 * sense data of 02,04,88 which happens to the read capacity 4173 * before mpxio has had sufficient knowledge to know if it should 4174 * force a fail over or not. (Which it won't do at attach anyhow). 4175 * If the read capacity at attach time fails, un_tgt_blocksize and 4176 * un_blockcount won't be valid. 4177 */ 4178 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4179 (un->un_f_blockcount_is_valid != TRUE)) { 4180 uint64_t cap; 4181 uint32_t lbasz; 4182 int rval; 4183 4184 mutex_exit(SD_MUTEX(un)); 4185 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4186 &lbasz, SD_PATH_DIRECT); 4187 mutex_enter(SD_MUTEX(un)); 4188 if (rval == 0) { 4189 /* 4190 * The following relies on 4191 * sd_send_scsi_READ_CAPACITY never 4192 * returning 0 for capacity and/or lbasize. 4193 */ 4194 sd_update_block_info(un, lbasz, cap); 4195 } 4196 4197 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4198 (un->un_f_blockcount_is_valid != TRUE)) { 4199 return (EINVAL); 4200 } 4201 } 4202 4203 /* 4204 * Copy the lbasize and capacity so that if they're reset while we're 4205 * not holding the SD_MUTEX, we will continue to use valid values 4206 * after the SD_MUTEX is reacquired. (4119659) 4207 */ 4208 lbasize = un->un_tgt_blocksize; 4209 capacity = un->un_blockcount; 4210 4211 #if defined(_SUNOS_VTOC_16) 4212 /* 4213 * Set up the "whole disk" fdisk partition; this should always 4214 * exist, regardless of whether the disk contains an fdisk table 4215 * or vtoc. 4216 */ 4217 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4218 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4219 #endif 4220 4221 /* 4222 * Refresh the logical and physical geometry caches. 4223 * (data from MODE SENSE format/rigid disk geometry pages, 4224 * and scsi_ifgetcap("geometry"). 4225 */ 4226 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4227 4228 label_error = sd_use_efi(un, path_flag); 4229 if (label_error == 0) { 4230 /* found a valid EFI label */ 4231 SD_TRACE(SD_LOG_IO_PARTITION, un, 4232 "sd_validate_geometry: found EFI label\n"); 4233 un->un_solaris_offset = 0; 4234 un->un_solaris_size = capacity; 4235 return (ENOTSUP); 4236 } 4237 if (un->un_blockcount > DK_MAX_BLOCKS) { 4238 if (label_error == ESRCH) { 4239 /* 4240 * they've configured a LUN over 1TB, but used 4241 * format.dat to restrict format's view of the 4242 * capacity to be under 1TB 4243 */ 4244 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4245 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4246 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4247 "size to be < 1TB or relabel the disk with an EFI label"); 4248 } else { 4249 /* unlabeled disk over 1TB */ 4250 #if defined(__i386) || defined(__amd64) 4251 /* 4252 * Refer to comments on off-by-1 at the head of the file 4253 * A 1TB disk was treated as (1T - 512)B in the past, 4254 * thus, it might have valid solaris partition. We 4255 * will return ENOTSUP later only if this disk has no 4256 * valid solaris partition. 4257 */ 4258 if ((un->un_tgt_blocksize != un->un_sys_blocksize) || 4259 (un->un_blockcount - 1 > DK_MAX_BLOCKS) || 4260 un->un_f_has_removable_media || 4261 un->un_f_is_hotpluggable) 4262 #endif 4263 return (ENOTSUP); 4264 } 4265 } 4266 label_error = 0; 4267 4268 /* 4269 * at this point it is either labeled with a VTOC or it is 4270 * under 1TB (<= 1TB actually for off-by-1) 4271 */ 4272 if (un->un_f_vtoc_label_supported) { 4273 struct dk_label *dkl; 4274 offset_t dkl1; 4275 offset_t label_addr, real_addr; 4276 int rval; 4277 size_t buffer_size; 4278 4279 /* 4280 * Note: This will set up un->un_solaris_size and 4281 * un->un_solaris_offset. 4282 */ 4283 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4284 case SD_CMD_RESERVATION_CONFLICT: 4285 ASSERT(mutex_owned(SD_MUTEX(un))); 4286 return (EACCES); 4287 case SD_CMD_FAILURE: 4288 ASSERT(mutex_owned(SD_MUTEX(un))); 4289 return (ENOMEM); 4290 } 4291 4292 if (un->un_solaris_size <= DK_LABEL_LOC) { 4293 4294 #if defined(__i386) || defined(__amd64) 4295 /* 4296 * Refer to comments on off-by-1 at the head of the file 4297 * This is for 1TB disk only. Since that there is no 4298 * solaris partitions, return ENOTSUP as we do for 4299 * >1TB disk. 4300 */ 4301 if (un->un_blockcount > DK_MAX_BLOCKS) 4302 return (ENOTSUP); 4303 #endif 4304 /* 4305 * Found fdisk table but no Solaris partition entry, 4306 * so don't call sd_uselabel() and don't create 4307 * a default label. 4308 */ 4309 label_error = 0; 4310 un->un_f_geometry_is_valid = TRUE; 4311 goto no_solaris_partition; 4312 } 4313 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4314 4315 #if defined(__i386) || defined(__amd64) 4316 /* 4317 * Refer to comments on off-by-1 at the head of the file 4318 * Now, this 1TB disk has valid solaris partition. It 4319 * must be created by previous sd driver, we have to 4320 * treat it as (1T-512)B. 4321 */ 4322 if (un->un_blockcount > DK_MAX_BLOCKS) { 4323 un->un_f_capacity_adjusted = 1; 4324 un->un_blockcount = DK_MAX_BLOCKS; 4325 un->un_map[P0_RAW_DISK].dkl_nblk = DK_MAX_BLOCKS; 4326 4327 /* 4328 * Refer to sd_read_fdisk, when there is no 4329 * fdisk partition table, un_solaris_size is 4330 * set to disk's capacity. In this case, we 4331 * need to adjust it 4332 */ 4333 if (un->un_solaris_size > DK_MAX_BLOCKS) 4334 un->un_solaris_size = DK_MAX_BLOCKS; 4335 sd_resync_geom_caches(un, DK_MAX_BLOCKS, 4336 lbasize, path_flag); 4337 } 4338 #endif 4339 4340 /* 4341 * sys_blocksize != tgt_blocksize, need to re-adjust 4342 * blkno and save the index to beginning of dk_label 4343 */ 4344 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4345 buffer_size = SD_REQBYTES2TGTBYTES(un, 4346 sizeof (struct dk_label)); 4347 4348 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4349 "label_addr: 0x%x allocation size: 0x%x\n", 4350 label_addr, buffer_size); 4351 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4352 if (dkl == NULL) { 4353 return (ENOMEM); 4354 } 4355 4356 mutex_exit(SD_MUTEX(un)); 4357 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4358 path_flag); 4359 mutex_enter(SD_MUTEX(un)); 4360 4361 switch (rval) { 4362 case 0: 4363 /* 4364 * sd_uselabel will establish that the geometry 4365 * is valid. 4366 * For sys_blocksize != tgt_blocksize, need 4367 * to index into the beginning of dk_label 4368 */ 4369 dkl1 = (daddr_t)dkl 4370 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4371 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4372 path_flag) != SD_LABEL_IS_VALID) { 4373 label_error = EINVAL; 4374 } 4375 break; 4376 case EACCES: 4377 label_error = EACCES; 4378 break; 4379 default: 4380 label_error = EINVAL; 4381 break; 4382 } 4383 4384 kmem_free(dkl, buffer_size); 4385 4386 #if defined(_SUNOS_VTOC_8) 4387 label = (char *)un->un_asciilabel; 4388 #elif defined(_SUNOS_VTOC_16) 4389 label = (char *)un->un_vtoc.v_asciilabel; 4390 #else 4391 #error "No VTOC format defined." 4392 #endif 4393 } 4394 4395 /* 4396 * If a valid label was not found, AND if no reservation conflict 4397 * was detected, then go ahead and create a default label (4069506). 4398 */ 4399 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4400 if (un->un_f_geometry_is_valid == FALSE) { 4401 sd_build_default_label(un); 4402 } 4403 label_error = 0; 4404 } 4405 4406 no_solaris_partition: 4407 if ((!un->un_f_has_removable_media || 4408 (un->un_f_has_removable_media && 4409 un->un_mediastate == DKIO_EJECTED)) && 4410 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4411 /* 4412 * Print out a message indicating who and what we are. 4413 * We do this only when we happen to really validate the 4414 * geometry. We may call sd_validate_geometry() at other 4415 * times, e.g., ioctl()'s like Get VTOC in which case we 4416 * don't want to print the label. 4417 * If the geometry is valid, print the label string, 4418 * else print vendor and product info, if available 4419 */ 4420 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4421 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4422 } else { 4423 mutex_enter(&sd_label_mutex); 4424 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4425 labelstring); 4426 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4427 &labelstring[64]); 4428 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4429 labelstring, &labelstring[64]); 4430 if (un->un_f_blockcount_is_valid == TRUE) { 4431 (void) sprintf(&buf[strlen(buf)], 4432 ", %llu %u byte blocks\n", 4433 (longlong_t)un->un_blockcount, 4434 un->un_tgt_blocksize); 4435 } else { 4436 (void) sprintf(&buf[strlen(buf)], 4437 ", (unknown capacity)\n"); 4438 } 4439 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4440 mutex_exit(&sd_label_mutex); 4441 } 4442 } 4443 4444 #if defined(_SUNOS_VTOC_16) 4445 /* 4446 * If we have valid geometry, set up the remaining fdisk partitions. 4447 * Note that dkl_cylno is not used for the fdisk map entries, so 4448 * we set it to an entirely bogus value. 4449 */ 4450 for (count = 0; count < FD_NUMPART; count++) { 4451 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4452 un->un_map[FDISK_P1 + count].dkl_nblk = 4453 un->un_fmap[count].fmap_nblk; 4454 4455 un->un_offset[FDISK_P1 + count] = 4456 un->un_fmap[count].fmap_start; 4457 } 4458 #endif 4459 4460 for (count = 0; count < NDKMAP; count++) { 4461 #if defined(_SUNOS_VTOC_8) 4462 struct dk_map *lp = &un->un_map[count]; 4463 un->un_offset[count] = 4464 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4465 #elif defined(_SUNOS_VTOC_16) 4466 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4467 4468 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4469 #else 4470 #error "No VTOC format defined." 4471 #endif 4472 } 4473 4474 return (label_error); 4475 } 4476 4477 4478 #if defined(_SUNOS_VTOC_16) 4479 /* 4480 * Macro: MAX_BLKS 4481 * 4482 * This macro is used for table entries where we need to have the largest 4483 * possible sector value for that head & SPT (sectors per track) 4484 * combination. Other entries for some smaller disk sizes are set by 4485 * convention to match those used by X86 BIOS usage. 4486 */ 4487 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4488 4489 /* 4490 * Function: sd_convert_geometry 4491 * 4492 * Description: Convert physical geometry into a dk_geom structure. In 4493 * other words, make sure we don't wrap 16-bit values. 4494 * e.g. converting from geom_cache to dk_geom 4495 * 4496 * Context: Kernel thread only 4497 */ 4498 static void 4499 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4500 { 4501 int i; 4502 static const struct chs_values { 4503 uint_t max_cap; /* Max Capacity for this HS. */ 4504 uint_t nhead; /* Heads to use. */ 4505 uint_t nsect; /* SPT to use. */ 4506 } CHS_values[] = { 4507 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4508 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4509 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4510 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4511 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4512 }; 4513 4514 /* Unlabeled SCSI floppy device */ 4515 if (capacity <= 0x1000) { 4516 un_g->dkg_nhead = 2; 4517 un_g->dkg_ncyl = 80; 4518 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4519 return; 4520 } 4521 4522 /* 4523 * For all devices we calculate cylinders using the 4524 * heads and sectors we assign based on capacity of the 4525 * device. The table is designed to be compatible with the 4526 * way other operating systems lay out fdisk tables for X86 4527 * and to insure that the cylinders never exceed 65535 to 4528 * prevent problems with X86 ioctls that report geometry. 4529 * We use SPT that are multiples of 63, since other OSes that 4530 * are not limited to 16-bits for cylinders stop at 63 SPT 4531 * we make do by using multiples of 63 SPT. 4532 * 4533 * Note than capacities greater than or equal to 1TB will simply 4534 * get the largest geometry from the table. This should be okay 4535 * since disks this large shouldn't be using CHS values anyway. 4536 */ 4537 for (i = 0; CHS_values[i].max_cap < capacity && 4538 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4539 ; 4540 4541 un_g->dkg_nhead = CHS_values[i].nhead; 4542 un_g->dkg_nsect = CHS_values[i].nsect; 4543 } 4544 #endif 4545 4546 4547 /* 4548 * Function: sd_resync_geom_caches 4549 * 4550 * Description: (Re)initialize both geometry caches: the virtual geometry 4551 * information is extracted from the HBA (the "geometry" 4552 * capability), and the physical geometry cache data is 4553 * generated by issuing MODE SENSE commands. 4554 * 4555 * Arguments: un - driver soft state (unit) structure 4556 * capacity - disk capacity in #blocks 4557 * lbasize - disk block size in bytes 4558 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4559 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4560 * to use the USCSI "direct" chain and bypass the normal 4561 * command waitq. 4562 * 4563 * Context: Kernel thread only (can sleep). 4564 */ 4565 4566 static void 4567 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4568 int path_flag) 4569 { 4570 struct geom_cache pgeom; 4571 struct geom_cache *pgeom_p = &pgeom; 4572 int spc; 4573 unsigned short nhead; 4574 unsigned short nsect; 4575 4576 ASSERT(un != NULL); 4577 ASSERT(mutex_owned(SD_MUTEX(un))); 4578 4579 /* 4580 * Ask the controller for its logical geometry. 4581 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4582 * then the lgeom cache will be invalid. 4583 */ 4584 sd_get_virtual_geometry(un, capacity, lbasize); 4585 4586 /* 4587 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4588 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4589 */ 4590 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4591 /* 4592 * Note: Perhaps this needs to be more adaptive? The rationale 4593 * is that, if there's no HBA geometry from the HBA driver, any 4594 * guess is good, since this is the physical geometry. If MODE 4595 * SENSE fails this gives a max cylinder size for non-LBA access 4596 */ 4597 nhead = 255; 4598 nsect = 63; 4599 } else { 4600 nhead = un->un_lgeom.g_nhead; 4601 nsect = un->un_lgeom.g_nsect; 4602 } 4603 4604 if (ISCD(un)) { 4605 pgeom_p->g_nhead = 1; 4606 pgeom_p->g_nsect = nsect * nhead; 4607 } else { 4608 pgeom_p->g_nhead = nhead; 4609 pgeom_p->g_nsect = nsect; 4610 } 4611 4612 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4613 pgeom_p->g_capacity = capacity; 4614 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4615 pgeom_p->g_acyl = 0; 4616 4617 /* 4618 * Retrieve fresh geometry data from the hardware, stash it 4619 * here temporarily before we rebuild the incore label. 4620 * 4621 * We want to use the MODE SENSE commands to derive the 4622 * physical geometry of the device, but if either command 4623 * fails, the logical geometry is used as the fallback for 4624 * disk label geometry. 4625 */ 4626 mutex_exit(SD_MUTEX(un)); 4627 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4628 mutex_enter(SD_MUTEX(un)); 4629 4630 /* 4631 * Now update the real copy while holding the mutex. This 4632 * way the global copy is never in an inconsistent state. 4633 */ 4634 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4635 4636 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4637 "(cached from lgeom)\n"); 4638 SD_INFO(SD_LOG_COMMON, un, 4639 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4640 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4641 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4642 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4643 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4644 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4645 un->un_pgeom.g_rpm); 4646 } 4647 4648 4649 /* 4650 * Function: sd_read_fdisk 4651 * 4652 * Description: utility routine to read the fdisk table. 4653 * 4654 * Arguments: un - driver soft state (unit) structure 4655 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4656 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4657 * to use the USCSI "direct" chain and bypass the normal 4658 * command waitq. 4659 * 4660 * Return Code: SD_CMD_SUCCESS 4661 * SD_CMD_FAILURE 4662 * 4663 * Context: Kernel thread only (can sleep). 4664 */ 4665 /* ARGSUSED */ 4666 static int 4667 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4668 { 4669 #if defined(_NO_FDISK_PRESENT) 4670 4671 un->un_solaris_offset = 0; 4672 un->un_solaris_size = capacity; 4673 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4674 return (SD_CMD_SUCCESS); 4675 4676 #elif defined(_FIRMWARE_NEEDS_FDISK) 4677 4678 struct ipart *fdp; 4679 struct mboot *mbp; 4680 struct ipart fdisk[FD_NUMPART]; 4681 int i; 4682 char sigbuf[2]; 4683 caddr_t bufp; 4684 int uidx; 4685 int rval; 4686 int lba = 0; 4687 uint_t solaris_offset; /* offset to solaris part. */ 4688 daddr_t solaris_size; /* size of solaris partition */ 4689 uint32_t blocksize; 4690 4691 ASSERT(un != NULL); 4692 ASSERT(mutex_owned(SD_MUTEX(un))); 4693 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4694 4695 blocksize = un->un_tgt_blocksize; 4696 4697 /* 4698 * Start off assuming no fdisk table 4699 */ 4700 solaris_offset = 0; 4701 solaris_size = capacity; 4702 4703 mutex_exit(SD_MUTEX(un)); 4704 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4705 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4706 mutex_enter(SD_MUTEX(un)); 4707 4708 if (rval != 0) { 4709 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4710 "sd_read_fdisk: fdisk read err\n"); 4711 kmem_free(bufp, blocksize); 4712 return (SD_CMD_FAILURE); 4713 } 4714 4715 mbp = (struct mboot *)bufp; 4716 4717 /* 4718 * The fdisk table does not begin on a 4-byte boundary within the 4719 * master boot record, so we copy it to an aligned structure to avoid 4720 * alignment exceptions on some processors. 4721 */ 4722 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4723 4724 /* 4725 * Check for lba support before verifying sig; sig might not be 4726 * there, say on a blank disk, but the max_chs mark may still 4727 * be present. 4728 * 4729 * Note: LBA support and BEFs are an x86-only concept but this 4730 * code should work OK on SPARC as well. 4731 */ 4732 4733 /* 4734 * First, check for lba-access-ok on root node (or prom root node) 4735 * if present there, don't need to search fdisk table. 4736 */ 4737 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4738 "lba-access-ok", 0) != 0) { 4739 /* All drives do LBA; don't search fdisk table */ 4740 lba = 1; 4741 } else { 4742 /* Okay, look for mark in fdisk table */ 4743 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4744 /* accumulate "lba" value from all partitions */ 4745 lba = (lba || sd_has_max_chs_vals(fdp)); 4746 } 4747 } 4748 4749 if (lba != 0) { 4750 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4751 4752 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4753 "lba-access-ok", 0) == 0) { 4754 /* not found; create it */ 4755 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4756 "lba-access-ok", (caddr_t)NULL, 0) != 4757 DDI_PROP_SUCCESS) { 4758 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4759 "sd_read_fdisk: Can't create lba property " 4760 "for instance %d\n", 4761 ddi_get_instance(SD_DEVINFO(un))); 4762 } 4763 } 4764 } 4765 4766 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4767 4768 /* 4769 * Endian-independent signature check 4770 */ 4771 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4772 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4773 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4774 "sd_read_fdisk: no fdisk\n"); 4775 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4776 rval = SD_CMD_SUCCESS; 4777 goto done; 4778 } 4779 4780 #ifdef SDDEBUG 4781 if (sd_level_mask & SD_LOGMASK_INFO) { 4782 fdp = fdisk; 4783 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4784 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4785 "numsect sysid bootid\n"); 4786 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4787 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4788 " %d: %8d %8d 0x%08x 0x%08x\n", 4789 i, fdp->relsect, fdp->numsect, 4790 fdp->systid, fdp->bootid); 4791 } 4792 } 4793 #endif 4794 4795 /* 4796 * Try to find the unix partition 4797 */ 4798 uidx = -1; 4799 solaris_offset = 0; 4800 solaris_size = 0; 4801 4802 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4803 int relsect; 4804 int numsect; 4805 4806 if (fdp->numsect == 0) { 4807 un->un_fmap[i].fmap_start = 0; 4808 un->un_fmap[i].fmap_nblk = 0; 4809 continue; 4810 } 4811 4812 /* 4813 * Data in the fdisk table is little-endian. 4814 */ 4815 relsect = LE_32(fdp->relsect); 4816 numsect = LE_32(fdp->numsect); 4817 4818 un->un_fmap[i].fmap_start = relsect; 4819 un->un_fmap[i].fmap_nblk = numsect; 4820 4821 if (fdp->systid != SUNIXOS && 4822 fdp->systid != SUNIXOS2 && 4823 fdp->systid != EFI_PMBR) { 4824 continue; 4825 } 4826 4827 /* 4828 * use the last active solaris partition id found 4829 * (there should only be 1 active partition id) 4830 * 4831 * if there are no active solaris partition id 4832 * then use the first inactive solaris partition id 4833 */ 4834 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4835 uidx = i; 4836 solaris_offset = relsect; 4837 solaris_size = numsect; 4838 } 4839 } 4840 4841 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4842 un->un_solaris_offset, un->un_solaris_size); 4843 4844 rval = SD_CMD_SUCCESS; 4845 4846 done: 4847 4848 /* 4849 * Clear the VTOC info, only if the Solaris partition entry 4850 * has moved, changed size, been deleted, or if the size of 4851 * the partition is too small to even fit the label sector. 4852 */ 4853 if ((un->un_solaris_offset != solaris_offset) || 4854 (un->un_solaris_size != solaris_size) || 4855 solaris_size <= DK_LABEL_LOC) { 4856 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4857 solaris_offset, solaris_size); 4858 bzero(&un->un_g, sizeof (struct dk_geom)); 4859 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4860 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4861 un->un_f_geometry_is_valid = FALSE; 4862 } 4863 un->un_solaris_offset = solaris_offset; 4864 un->un_solaris_size = solaris_size; 4865 kmem_free(bufp, blocksize); 4866 return (rval); 4867 4868 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4869 #error "fdisk table presence undetermined for this platform." 4870 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4871 } 4872 4873 4874 /* 4875 * Function: sd_get_physical_geometry 4876 * 4877 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4878 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4879 * target, and use this information to initialize the physical 4880 * geometry cache specified by pgeom_p. 4881 * 4882 * MODE SENSE is an optional command, so failure in this case 4883 * does not necessarily denote an error. We want to use the 4884 * MODE SENSE commands to derive the physical geometry of the 4885 * device, but if either command fails, the logical geometry is 4886 * used as the fallback for disk label geometry. 4887 * 4888 * This requires that un->un_blockcount and un->un_tgt_blocksize 4889 * have already been initialized for the current target and 4890 * that the current values be passed as args so that we don't 4891 * end up ever trying to use -1 as a valid value. This could 4892 * happen if either value is reset while we're not holding 4893 * the mutex. 4894 * 4895 * Arguments: un - driver soft state (unit) structure 4896 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4897 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4898 * to use the USCSI "direct" chain and bypass the normal 4899 * command waitq. 4900 * 4901 * Context: Kernel thread only (can sleep). 4902 */ 4903 4904 static void 4905 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4906 int capacity, int lbasize, int path_flag) 4907 { 4908 struct mode_format *page3p; 4909 struct mode_geometry *page4p; 4910 struct mode_header *headerp; 4911 int sector_size; 4912 int nsect; 4913 int nhead; 4914 int ncyl; 4915 int intrlv; 4916 int spc; 4917 int modesense_capacity; 4918 int rpm; 4919 int bd_len; 4920 int mode_header_length; 4921 uchar_t *p3bufp; 4922 uchar_t *p4bufp; 4923 int cdbsize; 4924 4925 ASSERT(un != NULL); 4926 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4927 4928 if (un->un_f_blockcount_is_valid != TRUE) { 4929 return; 4930 } 4931 4932 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4933 return; 4934 } 4935 4936 if (lbasize == 0) { 4937 if (ISCD(un)) { 4938 lbasize = 2048; 4939 } else { 4940 lbasize = un->un_sys_blocksize; 4941 } 4942 } 4943 pgeom_p->g_secsize = (unsigned short)lbasize; 4944 4945 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4946 4947 /* 4948 * Retrieve MODE SENSE page 3 - Format Device Page 4949 */ 4950 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4951 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4952 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4953 != 0) { 4954 SD_ERROR(SD_LOG_COMMON, un, 4955 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4956 goto page3_exit; 4957 } 4958 4959 /* 4960 * Determine size of Block Descriptors in order to locate the mode 4961 * page data. ATAPI devices return 0, SCSI devices should return 4962 * MODE_BLK_DESC_LENGTH. 4963 */ 4964 headerp = (struct mode_header *)p3bufp; 4965 if (un->un_f_cfg_is_atapi == TRUE) { 4966 struct mode_header_grp2 *mhp = 4967 (struct mode_header_grp2 *)headerp; 4968 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4969 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4970 } else { 4971 mode_header_length = MODE_HEADER_LENGTH; 4972 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4973 } 4974 4975 if (bd_len > MODE_BLK_DESC_LENGTH) { 4976 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4977 "received unexpected bd_len of %d, page3\n", bd_len); 4978 goto page3_exit; 4979 } 4980 4981 page3p = (struct mode_format *) 4982 ((caddr_t)headerp + mode_header_length + bd_len); 4983 4984 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4985 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4986 "mode sense pg3 code mismatch %d\n", 4987 page3p->mode_page.code); 4988 goto page3_exit; 4989 } 4990 4991 /* 4992 * Use this physical geometry data only if BOTH MODE SENSE commands 4993 * complete successfully; otherwise, revert to the logical geometry. 4994 * So, we need to save everything in temporary variables. 4995 */ 4996 sector_size = BE_16(page3p->data_bytes_sect); 4997 4998 /* 4999 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5000 */ 5001 if (sector_size == 0) { 5002 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5003 } else { 5004 sector_size &= ~(un->un_sys_blocksize - 1); 5005 } 5006 5007 nsect = BE_16(page3p->sect_track); 5008 intrlv = BE_16(page3p->interleave); 5009 5010 SD_INFO(SD_LOG_COMMON, un, 5011 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5012 SD_INFO(SD_LOG_COMMON, un, 5013 " mode page: %d; nsect: %d; sector size: %d;\n", 5014 page3p->mode_page.code, nsect, sector_size); 5015 SD_INFO(SD_LOG_COMMON, un, 5016 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5017 BE_16(page3p->track_skew), 5018 BE_16(page3p->cylinder_skew)); 5019 5020 5021 /* 5022 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5023 */ 5024 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5025 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5026 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5027 != 0) { 5028 SD_ERROR(SD_LOG_COMMON, un, 5029 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5030 goto page4_exit; 5031 } 5032 5033 /* 5034 * Determine size of Block Descriptors in order to locate the mode 5035 * page data. ATAPI devices return 0, SCSI devices should return 5036 * MODE_BLK_DESC_LENGTH. 5037 */ 5038 headerp = (struct mode_header *)p4bufp; 5039 if (un->un_f_cfg_is_atapi == TRUE) { 5040 struct mode_header_grp2 *mhp = 5041 (struct mode_header_grp2 *)headerp; 5042 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5043 } else { 5044 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5045 } 5046 5047 if (bd_len > MODE_BLK_DESC_LENGTH) { 5048 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5049 "received unexpected bd_len of %d, page4\n", bd_len); 5050 goto page4_exit; 5051 } 5052 5053 page4p = (struct mode_geometry *) 5054 ((caddr_t)headerp + mode_header_length + bd_len); 5055 5056 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5057 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5058 "mode sense pg4 code mismatch %d\n", 5059 page4p->mode_page.code); 5060 goto page4_exit; 5061 } 5062 5063 /* 5064 * Stash the data now, after we know that both commands completed. 5065 */ 5066 5067 mutex_enter(SD_MUTEX(un)); 5068 5069 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5070 spc = nhead * nsect; 5071 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5072 rpm = BE_16(page4p->rpm); 5073 5074 modesense_capacity = spc * ncyl; 5075 5076 SD_INFO(SD_LOG_COMMON, un, 5077 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5078 SD_INFO(SD_LOG_COMMON, un, 5079 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5080 SD_INFO(SD_LOG_COMMON, un, 5081 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5082 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5083 (void *)pgeom_p, capacity); 5084 5085 /* 5086 * Compensate if the drive's geometry is not rectangular, i.e., 5087 * the product of C * H * S returned by MODE SENSE >= that returned 5088 * by read capacity. This is an idiosyncrasy of the original x86 5089 * disk subsystem. 5090 */ 5091 if (modesense_capacity >= capacity) { 5092 SD_INFO(SD_LOG_COMMON, un, 5093 "sd_get_physical_geometry: adjusting acyl; " 5094 "old: %d; new: %d\n", pgeom_p->g_acyl, 5095 (modesense_capacity - capacity + spc - 1) / spc); 5096 if (sector_size != 0) { 5097 /* 1243403: NEC D38x7 drives don't support sec size */ 5098 pgeom_p->g_secsize = (unsigned short)sector_size; 5099 } 5100 pgeom_p->g_nsect = (unsigned short)nsect; 5101 pgeom_p->g_nhead = (unsigned short)nhead; 5102 pgeom_p->g_capacity = capacity; 5103 pgeom_p->g_acyl = 5104 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5105 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5106 } 5107 5108 pgeom_p->g_rpm = (unsigned short)rpm; 5109 pgeom_p->g_intrlv = (unsigned short)intrlv; 5110 5111 SD_INFO(SD_LOG_COMMON, un, 5112 "sd_get_physical_geometry: mode sense geometry:\n"); 5113 SD_INFO(SD_LOG_COMMON, un, 5114 " nsect: %d; sector size: %d; interlv: %d\n", 5115 nsect, sector_size, intrlv); 5116 SD_INFO(SD_LOG_COMMON, un, 5117 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5118 nhead, ncyl, rpm, modesense_capacity); 5119 SD_INFO(SD_LOG_COMMON, un, 5120 "sd_get_physical_geometry: (cached)\n"); 5121 SD_INFO(SD_LOG_COMMON, un, 5122 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5123 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5124 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5125 SD_INFO(SD_LOG_COMMON, un, 5126 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5127 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5128 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5129 5130 mutex_exit(SD_MUTEX(un)); 5131 5132 page4_exit: 5133 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5134 page3_exit: 5135 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5136 } 5137 5138 5139 /* 5140 * Function: sd_get_virtual_geometry 5141 * 5142 * Description: Ask the controller to tell us about the target device. 5143 * 5144 * Arguments: un - pointer to softstate 5145 * capacity - disk capacity in #blocks 5146 * lbasize - disk block size in bytes 5147 * 5148 * Context: Kernel thread only 5149 */ 5150 5151 static void 5152 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5153 { 5154 struct geom_cache *lgeom_p = &un->un_lgeom; 5155 uint_t geombuf; 5156 int spc; 5157 5158 ASSERT(un != NULL); 5159 ASSERT(mutex_owned(SD_MUTEX(un))); 5160 5161 mutex_exit(SD_MUTEX(un)); 5162 5163 /* Set sector size, and total number of sectors */ 5164 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5165 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5166 5167 /* Let the HBA tell us its geometry */ 5168 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5169 5170 mutex_enter(SD_MUTEX(un)); 5171 5172 /* A value of -1 indicates an undefined "geometry" property */ 5173 if (geombuf == (-1)) { 5174 return; 5175 } 5176 5177 /* Initialize the logical geometry cache. */ 5178 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5179 lgeom_p->g_nsect = geombuf & 0xffff; 5180 lgeom_p->g_secsize = un->un_sys_blocksize; 5181 5182 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5183 5184 /* 5185 * Note: The driver originally converted the capacity value from 5186 * target blocks to system blocks. However, the capacity value passed 5187 * to this routine is already in terms of system blocks (this scaling 5188 * is done when the READ CAPACITY command is issued and processed). 5189 * This 'error' may have gone undetected because the usage of g_ncyl 5190 * (which is based upon g_capacity) is very limited within the driver 5191 */ 5192 lgeom_p->g_capacity = capacity; 5193 5194 /* 5195 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5196 * hba may return zero values if the device has been removed. 5197 */ 5198 if (spc == 0) { 5199 lgeom_p->g_ncyl = 0; 5200 } else { 5201 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5202 } 5203 lgeom_p->g_acyl = 0; 5204 5205 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5206 SD_INFO(SD_LOG_COMMON, un, 5207 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5208 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5209 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5210 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5211 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5212 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5213 } 5214 5215 5216 /* 5217 * Function: sd_update_block_info 5218 * 5219 * Description: Calculate a byte count to sector count bitshift value 5220 * from sector size. 5221 * 5222 * Arguments: un: unit struct. 5223 * lbasize: new target sector size 5224 * capacity: new target capacity, ie. block count 5225 * 5226 * Context: Kernel thread context 5227 */ 5228 5229 static void 5230 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5231 { 5232 if (lbasize != 0) { 5233 un->un_tgt_blocksize = lbasize; 5234 un->un_f_tgt_blocksize_is_valid = TRUE; 5235 } 5236 5237 if (capacity != 0) { 5238 un->un_blockcount = capacity; 5239 un->un_f_blockcount_is_valid = TRUE; 5240 } 5241 } 5242 5243 5244 static void 5245 sd_swap_efi_gpt(efi_gpt_t *e) 5246 { 5247 _NOTE(ASSUMING_PROTECTED(*e)) 5248 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5249 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5250 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5251 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5252 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5253 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5254 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5255 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5256 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5257 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5258 e->efi_gpt_NumberOfPartitionEntries = 5259 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5260 e->efi_gpt_SizeOfPartitionEntry = 5261 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5262 e->efi_gpt_PartitionEntryArrayCRC32 = 5263 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5264 } 5265 5266 static void 5267 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5268 { 5269 int i; 5270 5271 _NOTE(ASSUMING_PROTECTED(*p)) 5272 for (i = 0; i < nparts; i++) { 5273 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5274 p[i].efi_gpe_PartitionTypeGUID); 5275 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5276 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5277 /* PartitionAttrs */ 5278 } 5279 } 5280 5281 static int 5282 sd_validate_efi(efi_gpt_t *labp) 5283 { 5284 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5285 return (EINVAL); 5286 /* at least 96 bytes in this version of the spec. */ 5287 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5288 labp->efi_gpt_HeaderSize) 5289 return (EINVAL); 5290 /* this should be 128 bytes */ 5291 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5292 return (EINVAL); 5293 return (0); 5294 } 5295 5296 static int 5297 sd_use_efi(struct sd_lun *un, int path_flag) 5298 { 5299 int i; 5300 int rval = 0; 5301 efi_gpe_t *partitions; 5302 uchar_t *buf; 5303 uint_t lbasize; 5304 uint64_t cap; 5305 uint_t nparts; 5306 diskaddr_t gpe_lba; 5307 5308 ASSERT(mutex_owned(SD_MUTEX(un))); 5309 lbasize = un->un_tgt_blocksize; 5310 5311 mutex_exit(SD_MUTEX(un)); 5312 5313 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5314 5315 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5316 rval = EINVAL; 5317 goto done_err; 5318 } 5319 5320 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5321 if (rval) { 5322 goto done_err; 5323 } 5324 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5325 /* not ours */ 5326 rval = ESRCH; 5327 goto done_err; 5328 } 5329 5330 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5331 if (rval) { 5332 goto done_err; 5333 } 5334 sd_swap_efi_gpt((efi_gpt_t *)buf); 5335 5336 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5337 /* 5338 * Couldn't read the primary, try the backup. Our 5339 * capacity at this point could be based on CHS, so 5340 * check what the device reports. 5341 */ 5342 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5343 path_flag); 5344 if (rval) { 5345 goto done_err; 5346 } 5347 5348 /* 5349 * The MMC standard allows READ CAPACITY to be 5350 * inaccurate by a bounded amount (in the interest of 5351 * response latency). As a result, failed READs are 5352 * commonplace (due to the reading of metadata and not 5353 * data). Depending on the per-Vendor/drive Sense data, 5354 * the failed READ can cause many (unnecessary) retries. 5355 */ 5356 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5357 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5358 path_flag)) != 0) { 5359 goto done_err; 5360 } 5361 5362 sd_swap_efi_gpt((efi_gpt_t *)buf); 5363 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5364 5365 /* 5366 * Refer to comments related to off-by-1 at the 5367 * header of this file. Search the next to last 5368 * block for backup EFI label. 5369 */ 5370 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5371 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5372 path_flag)) != 0) { 5373 goto done_err; 5374 } 5375 sd_swap_efi_gpt((efi_gpt_t *)buf); 5376 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5377 goto done_err; 5378 } 5379 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5380 "primary label corrupt; using backup\n"); 5381 } 5382 5383 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5384 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5385 5386 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5387 path_flag); 5388 if (rval) { 5389 goto done_err; 5390 } 5391 partitions = (efi_gpe_t *)buf; 5392 5393 if (nparts > MAXPART) { 5394 nparts = MAXPART; 5395 } 5396 sd_swap_efi_gpe(nparts, partitions); 5397 5398 mutex_enter(SD_MUTEX(un)); 5399 5400 /* Fill in partition table. */ 5401 for (i = 0; i < nparts; i++) { 5402 if (partitions->efi_gpe_StartingLBA != 0 || 5403 partitions->efi_gpe_EndingLBA != 0) { 5404 un->un_map[i].dkl_cylno = 5405 partitions->efi_gpe_StartingLBA; 5406 un->un_map[i].dkl_nblk = 5407 partitions->efi_gpe_EndingLBA - 5408 partitions->efi_gpe_StartingLBA + 1; 5409 un->un_offset[i] = 5410 partitions->efi_gpe_StartingLBA; 5411 } 5412 if (i == WD_NODE) { 5413 /* 5414 * minor number 7 corresponds to the whole disk 5415 */ 5416 un->un_map[i].dkl_cylno = 0; 5417 un->un_map[i].dkl_nblk = un->un_blockcount; 5418 un->un_offset[i] = 0; 5419 } 5420 partitions++; 5421 } 5422 un->un_solaris_offset = 0; 5423 un->un_solaris_size = cap; 5424 un->un_f_geometry_is_valid = TRUE; 5425 5426 /* clear the vtoc label */ 5427 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5428 5429 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5430 return (0); 5431 5432 done_err: 5433 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5434 mutex_enter(SD_MUTEX(un)); 5435 /* 5436 * if we didn't find something that could look like a VTOC 5437 * and the disk is over 1TB, we know there isn't a valid label. 5438 * Otherwise let sd_uselabel decide what to do. We only 5439 * want to invalidate this if we're certain the label isn't 5440 * valid because sd_prop_op will now fail, which in turn 5441 * causes things like opens and stats on the partition to fail. 5442 */ 5443 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5444 un->un_f_geometry_is_valid = FALSE; 5445 } 5446 return (rval); 5447 } 5448 5449 5450 /* 5451 * Function: sd_uselabel 5452 * 5453 * Description: Validate the disk label and update the relevant data (geometry, 5454 * partition, vtoc, and capacity data) in the sd_lun struct. 5455 * Marks the geometry of the unit as being valid. 5456 * 5457 * Arguments: un: unit struct. 5458 * dk_label: disk label 5459 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5460 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5461 * to use the USCSI "direct" chain and bypass the normal 5462 * command waitq. 5463 * 5464 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5465 * partition, vtoc, and capacity data are good. 5466 * 5467 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5468 * label; or computed capacity does not jibe with capacity 5469 * reported from the READ CAPACITY command. 5470 * 5471 * Context: Kernel thread only (can sleep). 5472 */ 5473 5474 static int 5475 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5476 { 5477 short *sp; 5478 short sum; 5479 short count; 5480 int label_error = SD_LABEL_IS_VALID; 5481 int i; 5482 int capacity; 5483 int part_end; 5484 int track_capacity; 5485 int err; 5486 #if defined(_SUNOS_VTOC_16) 5487 struct dkl_partition *vpartp; 5488 #endif 5489 ASSERT(un != NULL); 5490 ASSERT(mutex_owned(SD_MUTEX(un))); 5491 5492 /* Validate the magic number of the label. */ 5493 if (labp->dkl_magic != DKL_MAGIC) { 5494 #if defined(__sparc) 5495 if ((un->un_state == SD_STATE_NORMAL) && 5496 un->un_f_vtoc_errlog_supported) { 5497 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5498 "Corrupt label; wrong magic number\n"); 5499 } 5500 #endif 5501 return (SD_LABEL_IS_INVALID); 5502 } 5503 5504 /* Validate the checksum of the label. */ 5505 sp = (short *)labp; 5506 sum = 0; 5507 count = sizeof (struct dk_label) / sizeof (short); 5508 while (count--) { 5509 sum ^= *sp++; 5510 } 5511 5512 if (sum != 0) { 5513 #if defined(_SUNOS_VTOC_16) 5514 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5515 #elif defined(_SUNOS_VTOC_8) 5516 if ((un->un_state == SD_STATE_NORMAL) && 5517 un->un_f_vtoc_errlog_supported) { 5518 #endif 5519 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5520 "Corrupt label - label checksum failed\n"); 5521 } 5522 return (SD_LABEL_IS_INVALID); 5523 } 5524 5525 5526 /* 5527 * Fill in geometry structure with data from label. 5528 */ 5529 bzero(&un->un_g, sizeof (struct dk_geom)); 5530 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5531 un->un_g.dkg_acyl = labp->dkl_acyl; 5532 un->un_g.dkg_bcyl = 0; 5533 un->un_g.dkg_nhead = labp->dkl_nhead; 5534 un->un_g.dkg_nsect = labp->dkl_nsect; 5535 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5536 5537 #if defined(_SUNOS_VTOC_8) 5538 un->un_g.dkg_gap1 = labp->dkl_gap1; 5539 un->un_g.dkg_gap2 = labp->dkl_gap2; 5540 un->un_g.dkg_bhead = labp->dkl_bhead; 5541 #endif 5542 #if defined(_SUNOS_VTOC_16) 5543 un->un_dkg_skew = labp->dkl_skew; 5544 #endif 5545 5546 #if defined(__i386) || defined(__amd64) 5547 un->un_g.dkg_apc = labp->dkl_apc; 5548 #endif 5549 5550 /* 5551 * Currently we rely on the values in the label being accurate. If 5552 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5553 * 5554 * Note: In the future a MODE SENSE may be used to retrieve this data, 5555 * although this command is optional in SCSI-2. 5556 */ 5557 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5558 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5559 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5560 5561 /* 5562 * The Read and Write reinstruct values may not be valid 5563 * for older disks. 5564 */ 5565 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5566 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5567 5568 /* Fill in partition table. */ 5569 #if defined(_SUNOS_VTOC_8) 5570 for (i = 0; i < NDKMAP; i++) { 5571 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5572 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5573 } 5574 #endif 5575 #if defined(_SUNOS_VTOC_16) 5576 vpartp = labp->dkl_vtoc.v_part; 5577 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5578 5579 /* Prevent divide by zero */ 5580 if (track_capacity == 0) { 5581 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5582 "Corrupt label - zero nhead or nsect value\n"); 5583 5584 return (SD_LABEL_IS_INVALID); 5585 } 5586 5587 for (i = 0; i < NDKMAP; i++, vpartp++) { 5588 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5589 un->un_map[i].dkl_nblk = vpartp->p_size; 5590 } 5591 #endif 5592 5593 /* Fill in VTOC Structure. */ 5594 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5595 #if defined(_SUNOS_VTOC_8) 5596 /* 5597 * The 8-slice vtoc does not include the ascii label; save it into 5598 * the device's soft state structure here. 5599 */ 5600 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5601 #endif 5602 5603 /* Now look for a valid capacity. */ 5604 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5605 capacity = (un->un_g.dkg_ncyl * track_capacity); 5606 5607 if (un->un_g.dkg_acyl) { 5608 #if defined(__i386) || defined(__amd64) 5609 /* we may have > 1 alts cylinder */ 5610 capacity += (track_capacity * un->un_g.dkg_acyl); 5611 #else 5612 capacity += track_capacity; 5613 #endif 5614 } 5615 5616 /* 5617 * Force check here to ensure the computed capacity is valid. 5618 * If capacity is zero, it indicates an invalid label and 5619 * we should abort updating the relevant data then. 5620 */ 5621 if (capacity == 0) { 5622 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5623 "Corrupt label - no valid capacity could be retrieved\n"); 5624 5625 return (SD_LABEL_IS_INVALID); 5626 } 5627 5628 /* Mark the geometry as valid. */ 5629 un->un_f_geometry_is_valid = TRUE; 5630 5631 /* 5632 * At this point, un->un_blockcount should contain valid data from 5633 * the READ CAPACITY command. 5634 */ 5635 if (un->un_f_blockcount_is_valid != TRUE) { 5636 /* 5637 * We have a situation where the target didn't give us a good 5638 * READ CAPACITY value, yet there appears to be a valid label. 5639 * In this case, we'll fake the capacity. 5640 */ 5641 un->un_blockcount = capacity; 5642 un->un_f_blockcount_is_valid = TRUE; 5643 goto done; 5644 } 5645 5646 5647 if ((capacity <= un->un_blockcount) || 5648 (un->un_state != SD_STATE_NORMAL)) { 5649 #if defined(_SUNOS_VTOC_8) 5650 /* 5651 * We can't let this happen on drives that are subdivided 5652 * into logical disks (i.e., that have an fdisk table). 5653 * The un_blockcount field should always hold the full media 5654 * size in sectors, period. This code would overwrite 5655 * un_blockcount with the size of the Solaris fdisk partition. 5656 */ 5657 SD_ERROR(SD_LOG_COMMON, un, 5658 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5659 capacity, un->un_blockcount); 5660 un->un_blockcount = capacity; 5661 un->un_f_blockcount_is_valid = TRUE; 5662 #endif /* defined(_SUNOS_VTOC_8) */ 5663 goto done; 5664 } 5665 5666 if (ISCD(un)) { 5667 /* For CDROMs, we trust that the data in the label is OK. */ 5668 #if defined(_SUNOS_VTOC_8) 5669 for (i = 0; i < NDKMAP; i++) { 5670 part_end = labp->dkl_nhead * labp->dkl_nsect * 5671 labp->dkl_map[i].dkl_cylno + 5672 labp->dkl_map[i].dkl_nblk - 1; 5673 5674 if ((labp->dkl_map[i].dkl_nblk) && 5675 (part_end > un->un_blockcount)) { 5676 un->un_f_geometry_is_valid = FALSE; 5677 break; 5678 } 5679 } 5680 #endif 5681 #if defined(_SUNOS_VTOC_16) 5682 vpartp = &(labp->dkl_vtoc.v_part[0]); 5683 for (i = 0; i < NDKMAP; i++, vpartp++) { 5684 part_end = vpartp->p_start + vpartp->p_size; 5685 if ((vpartp->p_size > 0) && 5686 (part_end > un->un_blockcount)) { 5687 un->un_f_geometry_is_valid = FALSE; 5688 break; 5689 } 5690 } 5691 #endif 5692 } else { 5693 uint64_t t_capacity; 5694 uint32_t t_lbasize; 5695 5696 mutex_exit(SD_MUTEX(un)); 5697 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5698 path_flag); 5699 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5700 mutex_enter(SD_MUTEX(un)); 5701 5702 if (err == 0) { 5703 sd_update_block_info(un, t_lbasize, t_capacity); 5704 } 5705 5706 if (capacity > un->un_blockcount) { 5707 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5708 "Corrupt label - bad geometry\n"); 5709 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5710 "Label says %u blocks; Drive says %llu blocks\n", 5711 capacity, (unsigned long long)un->un_blockcount); 5712 un->un_f_geometry_is_valid = FALSE; 5713 label_error = SD_LABEL_IS_INVALID; 5714 } 5715 } 5716 5717 done: 5718 5719 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5720 SD_INFO(SD_LOG_COMMON, un, 5721 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5722 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5723 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5724 SD_INFO(SD_LOG_COMMON, un, 5725 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5726 un->un_tgt_blocksize, un->un_blockcount, 5727 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5728 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5729 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5730 5731 ASSERT(mutex_owned(SD_MUTEX(un))); 5732 5733 return (label_error); 5734 } 5735 5736 5737 /* 5738 * Function: sd_build_default_label 5739 * 5740 * Description: Generate a default label for those devices that do not have 5741 * one, e.g., new media, removable cartridges, etc.. 5742 * 5743 * Context: Kernel thread only 5744 */ 5745 5746 static void 5747 sd_build_default_label(struct sd_lun *un) 5748 { 5749 #if defined(_SUNOS_VTOC_16) 5750 uint_t phys_spc; 5751 uint_t disksize; 5752 struct dk_geom un_g; 5753 uint64_t capacity; 5754 #endif 5755 5756 ASSERT(un != NULL); 5757 ASSERT(mutex_owned(SD_MUTEX(un))); 5758 5759 #if defined(_SUNOS_VTOC_8) 5760 /* 5761 * Note: This is a legacy check for non-removable devices on VTOC_8 5762 * only. This may be a valid check for VTOC_16 as well. 5763 * Once we understand why there is this difference between SPARC and 5764 * x86 platform, we could remove this legacy check. 5765 */ 5766 ASSERT(un->un_f_default_vtoc_supported); 5767 #endif 5768 5769 bzero(&un->un_g, sizeof (struct dk_geom)); 5770 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5771 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5772 5773 #if defined(_SUNOS_VTOC_8) 5774 5775 /* 5776 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5777 * But it is still necessary to set up various geometry information, 5778 * and we are doing this here. 5779 */ 5780 5781 /* 5782 * For the rpm, we use the minimum for the disk. For the head, cyl, 5783 * and number of sector per track, if the capacity <= 1GB, head = 64, 5784 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5785 * equal to C*H*S values. This will cause some truncation of size due 5786 * to round off errors. For CD-ROMs, this truncation can have adverse 5787 * side effects, so returning ncyl and nhead as 1. The nsect will 5788 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5789 */ 5790 if (ISCD(un)) { 5791 /* 5792 * Preserve the old behavior for non-writable 5793 * medias. Since dkg_nsect is a ushort, it 5794 * will lose bits as cdroms have more than 5795 * 65536 sectors. So if we recalculate 5796 * capacity, it will become much shorter. 5797 * But the dkg_* information is not 5798 * used for CDROMs so it is OK. But for 5799 * Writable CDs we need this information 5800 * to be valid (for newfs say). So we 5801 * make nsect and nhead > 1 that way 5802 * nsect can still stay within ushort limit 5803 * without losing any bits. 5804 */ 5805 if (un->un_f_mmc_writable_media == TRUE) { 5806 un->un_g.dkg_nhead = 64; 5807 un->un_g.dkg_nsect = 32; 5808 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5809 un->un_blockcount = un->un_g.dkg_ncyl * 5810 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5811 } else { 5812 un->un_g.dkg_ncyl = 1; 5813 un->un_g.dkg_nhead = 1; 5814 un->un_g.dkg_nsect = un->un_blockcount; 5815 } 5816 } else { 5817 if (un->un_blockcount <= 0x1000) { 5818 /* unlabeled SCSI floppy device */ 5819 un->un_g.dkg_nhead = 2; 5820 un->un_g.dkg_ncyl = 80; 5821 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 5822 } else if (un->un_blockcount <= 0x200000) { 5823 un->un_g.dkg_nhead = 64; 5824 un->un_g.dkg_nsect = 32; 5825 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5826 } else { 5827 un->un_g.dkg_nhead = 255; 5828 un->un_g.dkg_nsect = 63; 5829 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 5830 } 5831 un->un_blockcount = 5832 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5833 } 5834 5835 un->un_g.dkg_acyl = 0; 5836 un->un_g.dkg_bcyl = 0; 5837 un->un_g.dkg_rpm = 200; 5838 un->un_asciilabel[0] = '\0'; 5839 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 5840 5841 un->un_map[0].dkl_cylno = 0; 5842 un->un_map[0].dkl_nblk = un->un_blockcount; 5843 un->un_map[2].dkl_cylno = 0; 5844 un->un_map[2].dkl_nblk = un->un_blockcount; 5845 5846 #elif defined(_SUNOS_VTOC_16) 5847 5848 if (un->un_solaris_size == 0) { 5849 /* 5850 * Got fdisk table but no solaris entry therefore 5851 * don't create a default label 5852 */ 5853 un->un_f_geometry_is_valid = TRUE; 5854 return; 5855 } 5856 5857 /* 5858 * For CDs we continue to use the physical geometry to calculate 5859 * number of cylinders. All other devices must convert the 5860 * physical geometry (geom_cache) to values that will fit 5861 * in a dk_geom structure. 5862 */ 5863 if (ISCD(un)) { 5864 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 5865 } else { 5866 /* Convert physical geometry to disk geometry */ 5867 bzero(&un_g, sizeof (struct dk_geom)); 5868 5869 /* 5870 * Refer to comments related to off-by-1 at the 5871 * header of this file. 5872 * Before caculating geometry, capacity should be 5873 * decreased by 1. That un_f_capacity_adjusted is 5874 * TRUE means that we are treating a 1TB disk as 5875 * (1T - 512)B. And the capacity of disks is already 5876 * decreased by 1. 5877 */ 5878 if (!un->un_f_capacity_adjusted && 5879 !un->un_f_has_removable_media && 5880 !un->un_f_is_hotpluggable && 5881 un->un_tgt_blocksize == un->un_sys_blocksize) 5882 capacity = un->un_blockcount - 1; 5883 else 5884 capacity = un->un_blockcount; 5885 5886 sd_convert_geometry(capacity, &un_g); 5887 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 5888 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5889 } 5890 5891 ASSERT(phys_spc != 0); 5892 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 5893 un->un_g.dkg_acyl = DK_ACYL; 5894 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 5895 disksize = un->un_g.dkg_ncyl * phys_spc; 5896 5897 if (ISCD(un)) { 5898 /* 5899 * CD's don't use the "heads * sectors * cyls"-type of 5900 * geometry, but instead use the entire capacity of the media. 5901 */ 5902 disksize = un->un_solaris_size; 5903 un->un_g.dkg_nhead = 1; 5904 un->un_g.dkg_nsect = 1; 5905 un->un_g.dkg_rpm = 5906 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 5907 5908 un->un_vtoc.v_part[0].p_start = 0; 5909 un->un_vtoc.v_part[0].p_size = disksize; 5910 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 5911 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 5912 5913 un->un_map[0].dkl_cylno = 0; 5914 un->un_map[0].dkl_nblk = disksize; 5915 un->un_offset[0] = 0; 5916 5917 } else { 5918 /* 5919 * Hard disks and removable media cartridges 5920 */ 5921 un->un_g.dkg_rpm = 5922 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 5923 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 5924 5925 /* Add boot slice */ 5926 un->un_vtoc.v_part[8].p_start = 0; 5927 un->un_vtoc.v_part[8].p_size = phys_spc; 5928 un->un_vtoc.v_part[8].p_tag = V_BOOT; 5929 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 5930 5931 un->un_map[8].dkl_cylno = 0; 5932 un->un_map[8].dkl_nblk = phys_spc; 5933 un->un_offset[8] = 0; 5934 } 5935 5936 un->un_g.dkg_apc = 0; 5937 un->un_vtoc.v_nparts = V_NUMPAR; 5938 un->un_vtoc.v_version = V_VERSION; 5939 5940 /* Add backup slice */ 5941 un->un_vtoc.v_part[2].p_start = 0; 5942 un->un_vtoc.v_part[2].p_size = disksize; 5943 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 5944 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 5945 5946 un->un_map[2].dkl_cylno = 0; 5947 un->un_map[2].dkl_nblk = disksize; 5948 un->un_offset[2] = 0; 5949 5950 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 5951 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5952 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5953 5954 #else 5955 #error "No VTOC format defined." 5956 #endif 5957 5958 un->un_g.dkg_read_reinstruct = 0; 5959 un->un_g.dkg_write_reinstruct = 0; 5960 5961 un->un_g.dkg_intrlv = 1; 5962 5963 un->un_vtoc.v_sanity = VTOC_SANE; 5964 5965 un->un_f_geometry_is_valid = TRUE; 5966 5967 SD_INFO(SD_LOG_COMMON, un, 5968 "sd_build_default_label: Default label created: " 5969 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 5970 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 5971 un->un_g.dkg_nsect, un->un_blockcount); 5972 } 5973 5974 5975 #if defined(_FIRMWARE_NEEDS_FDISK) 5976 /* 5977 * Max CHS values, as they are encoded into bytes, for 1022/254/63 5978 */ 5979 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 5980 #define LBA_MAX_CYL (1022 & 0xFF) 5981 #define LBA_MAX_HEAD (254) 5982 5983 5984 /* 5985 * Function: sd_has_max_chs_vals 5986 * 5987 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 5988 * 5989 * Arguments: fdp - ptr to CHS info 5990 * 5991 * Return Code: True or false 5992 * 5993 * Context: Any. 5994 */ 5995 5996 static int 5997 sd_has_max_chs_vals(struct ipart *fdp) 5998 { 5999 return ((fdp->begcyl == LBA_MAX_CYL) && 6000 (fdp->beghead == LBA_MAX_HEAD) && 6001 (fdp->begsect == LBA_MAX_SECT) && 6002 (fdp->endcyl == LBA_MAX_CYL) && 6003 (fdp->endhead == LBA_MAX_HEAD) && 6004 (fdp->endsect == LBA_MAX_SECT)); 6005 } 6006 #endif 6007 6008 6009 /* 6010 * Function: sd_inq_fill 6011 * 6012 * Description: Print a piece of inquiry data, cleaned up for non-printable 6013 * characters and stopping at the first space character after 6014 * the beginning of the passed string; 6015 * 6016 * Arguments: p - source string 6017 * l - maximum length to copy 6018 * s - destination string 6019 * 6020 * Context: Any. 6021 */ 6022 6023 static void 6024 sd_inq_fill(char *p, int l, char *s) 6025 { 6026 unsigned i = 0; 6027 char c; 6028 6029 while (i++ < l) { 6030 if ((c = *p++) < ' ' || c >= 0x7F) { 6031 c = '*'; 6032 } else if (i != 1 && c == ' ') { 6033 break; 6034 } 6035 *s++ = c; 6036 } 6037 *s++ = 0; 6038 } 6039 6040 6041 /* 6042 * Function: sd_register_devid 6043 * 6044 * Description: This routine will obtain the device id information from the 6045 * target, obtain the serial number, and register the device 6046 * id with the ddi framework. 6047 * 6048 * Arguments: devi - the system's dev_info_t for the device. 6049 * un - driver soft state (unit) structure 6050 * reservation_flag - indicates if a reservation conflict 6051 * occurred during attach 6052 * 6053 * Context: Kernel Thread 6054 */ 6055 static void 6056 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6057 { 6058 int rval = 0; 6059 uchar_t *inq80 = NULL; 6060 size_t inq80_len = MAX_INQUIRY_SIZE; 6061 size_t inq80_resid = 0; 6062 uchar_t *inq83 = NULL; 6063 size_t inq83_len = MAX_INQUIRY_SIZE; 6064 size_t inq83_resid = 0; 6065 6066 ASSERT(un != NULL); 6067 ASSERT(mutex_owned(SD_MUTEX(un))); 6068 ASSERT((SD_DEVINFO(un)) == devi); 6069 6070 /* 6071 * This is the case of antiquated Sun disk drives that have the 6072 * FAB_DEVID property set in the disk_table. These drives 6073 * manage the devid's by storing them in last 2 available sectors 6074 * on the drive and have them fabricated by the ddi layer by calling 6075 * ddi_devid_init and passing the DEVID_FAB flag. 6076 */ 6077 if (un->un_f_opt_fab_devid == TRUE) { 6078 /* 6079 * Depending on EINVAL isn't reliable, since a reserved disk 6080 * may result in invalid geometry, so check to make sure a 6081 * reservation conflict did not occur during attach. 6082 */ 6083 if ((sd_get_devid(un) == EINVAL) && 6084 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6085 /* 6086 * The devid is invalid AND there is no reservation 6087 * conflict. Fabricate a new devid. 6088 */ 6089 (void) sd_create_devid(un); 6090 } 6091 6092 /* Register the devid if it exists */ 6093 if (un->un_devid != NULL) { 6094 (void) ddi_devid_register(SD_DEVINFO(un), 6095 un->un_devid); 6096 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6097 "sd_register_devid: Devid Fabricated\n"); 6098 } 6099 return; 6100 } 6101 6102 /* 6103 * We check the availibility of the World Wide Name (0x83) and Unit 6104 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6105 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6106 * 0x83 is availible, that is the best choice. Our next choice is 6107 * 0x80. If neither are availible, we munge the devid from the device 6108 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6109 * to fabricate a devid for non-Sun qualified disks. 6110 */ 6111 if (sd_check_vpd_page_support(un) == 0) { 6112 /* collect page 80 data if available */ 6113 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6114 6115 mutex_exit(SD_MUTEX(un)); 6116 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6117 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6118 0x01, 0x80, &inq80_resid); 6119 6120 if (rval != 0) { 6121 kmem_free(inq80, inq80_len); 6122 inq80 = NULL; 6123 inq80_len = 0; 6124 } 6125 mutex_enter(SD_MUTEX(un)); 6126 } 6127 6128 /* collect page 83 data if available */ 6129 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6130 mutex_exit(SD_MUTEX(un)); 6131 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6132 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6133 0x01, 0x83, &inq83_resid); 6134 6135 if (rval != 0) { 6136 kmem_free(inq83, inq83_len); 6137 inq83 = NULL; 6138 inq83_len = 0; 6139 } 6140 mutex_enter(SD_MUTEX(un)); 6141 } 6142 } 6143 6144 /* encode best devid possible based on data available */ 6145 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6146 (char *)ddi_driver_name(SD_DEVINFO(un)), 6147 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6148 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6149 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6150 6151 /* devid successfully encoded, register devid */ 6152 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6153 6154 } else { 6155 /* 6156 * Unable to encode a devid based on data available. 6157 * This is not a Sun qualified disk. Older Sun disk 6158 * drives that have the SD_FAB_DEVID property 6159 * set in the disk_table and non Sun qualified 6160 * disks are treated in the same manner. These 6161 * drives manage the devid's by storing them in 6162 * last 2 available sectors on the drive and 6163 * have them fabricated by the ddi layer by 6164 * calling ddi_devid_init and passing the 6165 * DEVID_FAB flag. 6166 * Create a fabricate devid only if there's no 6167 * fabricate devid existed. 6168 */ 6169 if (sd_get_devid(un) == EINVAL) { 6170 (void) sd_create_devid(un); 6171 un->un_f_opt_fab_devid = TRUE; 6172 } 6173 6174 /* Register the devid if it exists */ 6175 if (un->un_devid != NULL) { 6176 (void) ddi_devid_register(SD_DEVINFO(un), 6177 un->un_devid); 6178 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6179 "sd_register_devid: devid fabricated using " 6180 "ddi framework\n"); 6181 } 6182 } 6183 6184 /* clean up resources */ 6185 if (inq80 != NULL) { 6186 kmem_free(inq80, inq80_len); 6187 } 6188 if (inq83 != NULL) { 6189 kmem_free(inq83, inq83_len); 6190 } 6191 } 6192 6193 static daddr_t 6194 sd_get_devid_block(struct sd_lun *un) 6195 { 6196 daddr_t spc, blk, head, cyl; 6197 6198 if (un->un_blockcount <= DK_MAX_BLOCKS) { 6199 /* this geometry doesn't allow us to write a devid */ 6200 if (un->un_g.dkg_acyl < 2) { 6201 return (-1); 6202 } 6203 6204 /* 6205 * Subtract 2 guarantees that the next to last cylinder 6206 * is used 6207 */ 6208 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6209 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6210 head = un->un_g.dkg_nhead - 1; 6211 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6212 (head * un->un_g.dkg_nsect) + 1; 6213 } else { 6214 if (un->un_reserved != -1) { 6215 blk = un->un_map[un->un_reserved].dkl_cylno + 1; 6216 } else { 6217 return (-1); 6218 } 6219 } 6220 return (blk); 6221 } 6222 6223 /* 6224 * Function: sd_get_devid 6225 * 6226 * Description: This routine will return 0 if a valid device id has been 6227 * obtained from the target and stored in the soft state. If a 6228 * valid device id has not been previously read and stored, a 6229 * read attempt will be made. 6230 * 6231 * Arguments: un - driver soft state (unit) structure 6232 * 6233 * Return Code: 0 if we successfully get the device id 6234 * 6235 * Context: Kernel Thread 6236 */ 6237 6238 static int 6239 sd_get_devid(struct sd_lun *un) 6240 { 6241 struct dk_devid *dkdevid; 6242 ddi_devid_t tmpid; 6243 uint_t *ip; 6244 size_t sz; 6245 daddr_t blk; 6246 int status; 6247 int chksum; 6248 int i; 6249 size_t buffer_size; 6250 6251 ASSERT(un != NULL); 6252 ASSERT(mutex_owned(SD_MUTEX(un))); 6253 6254 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6255 un); 6256 6257 if (un->un_devid != NULL) { 6258 return (0); 6259 } 6260 6261 blk = sd_get_devid_block(un); 6262 if (blk < 0) 6263 return (EINVAL); 6264 6265 /* 6266 * Read and verify device id, stored in the reserved cylinders at the 6267 * end of the disk. Backup label is on the odd sectors of the last 6268 * track of the last cylinder. Device id will be on track of the next 6269 * to last cylinder. 6270 */ 6271 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6272 mutex_exit(SD_MUTEX(un)); 6273 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6274 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6275 SD_PATH_DIRECT); 6276 if (status != 0) { 6277 goto error; 6278 } 6279 6280 /* Validate the revision */ 6281 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6282 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6283 status = EINVAL; 6284 goto error; 6285 } 6286 6287 /* Calculate the checksum */ 6288 chksum = 0; 6289 ip = (uint_t *)dkdevid; 6290 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6291 i++) { 6292 chksum ^= ip[i]; 6293 } 6294 6295 /* Compare the checksums */ 6296 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6297 status = EINVAL; 6298 goto error; 6299 } 6300 6301 /* Validate the device id */ 6302 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6303 status = EINVAL; 6304 goto error; 6305 } 6306 6307 /* 6308 * Store the device id in the driver soft state 6309 */ 6310 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6311 tmpid = kmem_alloc(sz, KM_SLEEP); 6312 6313 mutex_enter(SD_MUTEX(un)); 6314 6315 un->un_devid = tmpid; 6316 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6317 6318 kmem_free(dkdevid, buffer_size); 6319 6320 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6321 6322 return (status); 6323 error: 6324 mutex_enter(SD_MUTEX(un)); 6325 kmem_free(dkdevid, buffer_size); 6326 return (status); 6327 } 6328 6329 6330 /* 6331 * Function: sd_create_devid 6332 * 6333 * Description: This routine will fabricate the device id and write it 6334 * to the disk. 6335 * 6336 * Arguments: un - driver soft state (unit) structure 6337 * 6338 * Return Code: value of the fabricated device id 6339 * 6340 * Context: Kernel Thread 6341 */ 6342 6343 static ddi_devid_t 6344 sd_create_devid(struct sd_lun *un) 6345 { 6346 ASSERT(un != NULL); 6347 6348 /* Fabricate the devid */ 6349 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6350 == DDI_FAILURE) { 6351 return (NULL); 6352 } 6353 6354 /* Write the devid to disk */ 6355 if (sd_write_deviceid(un) != 0) { 6356 ddi_devid_free(un->un_devid); 6357 un->un_devid = NULL; 6358 } 6359 6360 return (un->un_devid); 6361 } 6362 6363 6364 /* 6365 * Function: sd_write_deviceid 6366 * 6367 * Description: This routine will write the device id to the disk 6368 * reserved sector. 6369 * 6370 * Arguments: un - driver soft state (unit) structure 6371 * 6372 * Return Code: EINVAL 6373 * value returned by sd_send_scsi_cmd 6374 * 6375 * Context: Kernel Thread 6376 */ 6377 6378 static int 6379 sd_write_deviceid(struct sd_lun *un) 6380 { 6381 struct dk_devid *dkdevid; 6382 daddr_t blk; 6383 uint_t *ip, chksum; 6384 int status; 6385 int i; 6386 6387 ASSERT(mutex_owned(SD_MUTEX(un))); 6388 6389 blk = sd_get_devid_block(un); 6390 if (blk < 0) 6391 return (-1); 6392 mutex_exit(SD_MUTEX(un)); 6393 6394 /* Allocate the buffer */ 6395 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6396 6397 /* Fill in the revision */ 6398 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6399 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6400 6401 /* Copy in the device id */ 6402 mutex_enter(SD_MUTEX(un)); 6403 bcopy(un->un_devid, &dkdevid->dkd_devid, 6404 ddi_devid_sizeof(un->un_devid)); 6405 mutex_exit(SD_MUTEX(un)); 6406 6407 /* Calculate the checksum */ 6408 chksum = 0; 6409 ip = (uint_t *)dkdevid; 6410 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6411 i++) { 6412 chksum ^= ip[i]; 6413 } 6414 6415 /* Fill-in checksum */ 6416 DKD_FORMCHKSUM(chksum, dkdevid); 6417 6418 /* Write the reserved sector */ 6419 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6420 SD_PATH_DIRECT); 6421 6422 kmem_free(dkdevid, un->un_sys_blocksize); 6423 6424 mutex_enter(SD_MUTEX(un)); 6425 return (status); 6426 } 6427 6428 6429 /* 6430 * Function: sd_check_vpd_page_support 6431 * 6432 * Description: This routine sends an inquiry command with the EVPD bit set and 6433 * a page code of 0x00 to the device. It is used to determine which 6434 * vital product pages are availible to find the devid. We are 6435 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6436 * device does not support that command. 6437 * 6438 * Arguments: un - driver soft state (unit) structure 6439 * 6440 * Return Code: 0 - success 6441 * 1 - check condition 6442 * 6443 * Context: This routine can sleep. 6444 */ 6445 6446 static int 6447 sd_check_vpd_page_support(struct sd_lun *un) 6448 { 6449 uchar_t *page_list = NULL; 6450 uchar_t page_length = 0xff; /* Use max possible length */ 6451 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6452 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6453 int rval = 0; 6454 int counter; 6455 6456 ASSERT(un != NULL); 6457 ASSERT(mutex_owned(SD_MUTEX(un))); 6458 6459 mutex_exit(SD_MUTEX(un)); 6460 6461 /* 6462 * We'll set the page length to the maximum to save figuring it out 6463 * with an additional call. 6464 */ 6465 page_list = kmem_zalloc(page_length, KM_SLEEP); 6466 6467 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6468 page_code, NULL); 6469 6470 mutex_enter(SD_MUTEX(un)); 6471 6472 /* 6473 * Now we must validate that the device accepted the command, as some 6474 * drives do not support it. If the drive does support it, we will 6475 * return 0, and the supported pages will be in un_vpd_page_mask. If 6476 * not, we return -1. 6477 */ 6478 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6479 /* Loop to find one of the 2 pages we need */ 6480 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6481 6482 /* 6483 * Pages are returned in ascending order, and 0x83 is what we 6484 * are hoping for. 6485 */ 6486 while ((page_list[counter] <= 0x83) && 6487 (counter <= (page_list[VPD_PAGE_LENGTH] + 6488 VPD_HEAD_OFFSET))) { 6489 /* 6490 * Add 3 because page_list[3] is the number of 6491 * pages minus 3 6492 */ 6493 6494 switch (page_list[counter]) { 6495 case 0x00: 6496 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6497 break; 6498 case 0x80: 6499 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6500 break; 6501 case 0x81: 6502 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6503 break; 6504 case 0x82: 6505 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6506 break; 6507 case 0x83: 6508 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6509 break; 6510 } 6511 counter++; 6512 } 6513 6514 } else { 6515 rval = -1; 6516 6517 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6518 "sd_check_vpd_page_support: This drive does not implement " 6519 "VPD pages.\n"); 6520 } 6521 6522 kmem_free(page_list, page_length); 6523 6524 return (rval); 6525 } 6526 6527 6528 /* 6529 * Function: sd_setup_pm 6530 * 6531 * Description: Initialize Power Management on the device 6532 * 6533 * Context: Kernel Thread 6534 */ 6535 6536 static void 6537 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6538 { 6539 uint_t log_page_size; 6540 uchar_t *log_page_data; 6541 int rval; 6542 6543 /* 6544 * Since we are called from attach, holding a mutex for 6545 * un is unnecessary. Because some of the routines called 6546 * from here require SD_MUTEX to not be held, assert this 6547 * right up front. 6548 */ 6549 ASSERT(!mutex_owned(SD_MUTEX(un))); 6550 /* 6551 * Since the sd device does not have the 'reg' property, 6552 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6553 * The following code is to tell cpr that this device 6554 * DOES need to be suspended and resumed. 6555 */ 6556 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6557 "pm-hardware-state", "needs-suspend-resume"); 6558 6559 /* 6560 * This complies with the new power management framework 6561 * for certain desktop machines. Create the pm_components 6562 * property as a string array property. 6563 */ 6564 if (un->un_f_pm_supported) { 6565 /* 6566 * not all devices have a motor, try it first. 6567 * some devices may return ILLEGAL REQUEST, some 6568 * will hang 6569 * The following START_STOP_UNIT is used to check if target 6570 * device has a motor. 6571 */ 6572 un->un_f_start_stop_supported = TRUE; 6573 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6574 SD_PATH_DIRECT) != 0) { 6575 un->un_f_start_stop_supported = FALSE; 6576 } 6577 6578 /* 6579 * create pm properties anyways otherwise the parent can't 6580 * go to sleep 6581 */ 6582 (void) sd_create_pm_components(devi, un); 6583 un->un_f_pm_is_enabled = TRUE; 6584 return; 6585 } 6586 6587 if (!un->un_f_log_sense_supported) { 6588 un->un_power_level = SD_SPINDLE_ON; 6589 un->un_f_pm_is_enabled = FALSE; 6590 return; 6591 } 6592 6593 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6594 6595 #ifdef SDDEBUG 6596 if (sd_force_pm_supported) { 6597 /* Force a successful result */ 6598 rval = 1; 6599 } 6600 #endif 6601 6602 /* 6603 * If the start-stop cycle counter log page is not supported 6604 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6605 * then we should not create the pm_components property. 6606 */ 6607 if (rval == -1) { 6608 /* 6609 * Error. 6610 * Reading log sense failed, most likely this is 6611 * an older drive that does not support log sense. 6612 * If this fails auto-pm is not supported. 6613 */ 6614 un->un_power_level = SD_SPINDLE_ON; 6615 un->un_f_pm_is_enabled = FALSE; 6616 6617 } else if (rval == 0) { 6618 /* 6619 * Page not found. 6620 * The start stop cycle counter is implemented as page 6621 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6622 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6623 */ 6624 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6625 /* 6626 * Page found, use this one. 6627 */ 6628 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6629 un->un_f_pm_is_enabled = TRUE; 6630 } else { 6631 /* 6632 * Error or page not found. 6633 * auto-pm is not supported for this device. 6634 */ 6635 un->un_power_level = SD_SPINDLE_ON; 6636 un->un_f_pm_is_enabled = FALSE; 6637 } 6638 } else { 6639 /* 6640 * Page found, use it. 6641 */ 6642 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6643 un->un_f_pm_is_enabled = TRUE; 6644 } 6645 6646 6647 if (un->un_f_pm_is_enabled == TRUE) { 6648 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6649 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6650 6651 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6652 log_page_size, un->un_start_stop_cycle_page, 6653 0x01, 0, SD_PATH_DIRECT); 6654 #ifdef SDDEBUG 6655 if (sd_force_pm_supported) { 6656 /* Force a successful result */ 6657 rval = 0; 6658 } 6659 #endif 6660 6661 /* 6662 * If the Log sense for Page( Start/stop cycle counter page) 6663 * succeeds, then power managment is supported and we can 6664 * enable auto-pm. 6665 */ 6666 if (rval == 0) { 6667 (void) sd_create_pm_components(devi, un); 6668 } else { 6669 un->un_power_level = SD_SPINDLE_ON; 6670 un->un_f_pm_is_enabled = FALSE; 6671 } 6672 6673 kmem_free(log_page_data, log_page_size); 6674 } 6675 } 6676 6677 6678 /* 6679 * Function: sd_create_pm_components 6680 * 6681 * Description: Initialize PM property. 6682 * 6683 * Context: Kernel thread context 6684 */ 6685 6686 static void 6687 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6688 { 6689 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6690 6691 ASSERT(!mutex_owned(SD_MUTEX(un))); 6692 6693 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6694 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6695 /* 6696 * When components are initially created they are idle, 6697 * power up any non-removables. 6698 * Note: the return value of pm_raise_power can't be used 6699 * for determining if PM should be enabled for this device. 6700 * Even if you check the return values and remove this 6701 * property created above, the PM framework will not honor the 6702 * change after the first call to pm_raise_power. Hence, 6703 * removal of that property does not help if pm_raise_power 6704 * fails. In the case of removable media, the start/stop 6705 * will fail if the media is not present. 6706 */ 6707 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6708 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6709 mutex_enter(SD_MUTEX(un)); 6710 un->un_power_level = SD_SPINDLE_ON; 6711 mutex_enter(&un->un_pm_mutex); 6712 /* Set to on and not busy. */ 6713 un->un_pm_count = 0; 6714 } else { 6715 mutex_enter(SD_MUTEX(un)); 6716 un->un_power_level = SD_SPINDLE_OFF; 6717 mutex_enter(&un->un_pm_mutex); 6718 /* Set to off. */ 6719 un->un_pm_count = -1; 6720 } 6721 mutex_exit(&un->un_pm_mutex); 6722 mutex_exit(SD_MUTEX(un)); 6723 } else { 6724 un->un_power_level = SD_SPINDLE_ON; 6725 un->un_f_pm_is_enabled = FALSE; 6726 } 6727 } 6728 6729 6730 /* 6731 * Function: sd_ddi_suspend 6732 * 6733 * Description: Performs system power-down operations. This includes 6734 * setting the drive state to indicate its suspended so 6735 * that no new commands will be accepted. Also, wait for 6736 * all commands that are in transport or queued to a timer 6737 * for retry to complete. All timeout threads are cancelled. 6738 * 6739 * Return Code: DDI_FAILURE or DDI_SUCCESS 6740 * 6741 * Context: Kernel thread context 6742 */ 6743 6744 static int 6745 sd_ddi_suspend(dev_info_t *devi) 6746 { 6747 struct sd_lun *un; 6748 clock_t wait_cmds_complete; 6749 6750 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6751 if (un == NULL) { 6752 return (DDI_FAILURE); 6753 } 6754 6755 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6756 6757 mutex_enter(SD_MUTEX(un)); 6758 6759 /* Return success if the device is already suspended. */ 6760 if (un->un_state == SD_STATE_SUSPENDED) { 6761 mutex_exit(SD_MUTEX(un)); 6762 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6763 "device already suspended, exiting\n"); 6764 return (DDI_SUCCESS); 6765 } 6766 6767 /* Return failure if the device is being used by HA */ 6768 if (un->un_resvd_status & 6769 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6770 mutex_exit(SD_MUTEX(un)); 6771 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6772 "device in use by HA, exiting\n"); 6773 return (DDI_FAILURE); 6774 } 6775 6776 /* 6777 * Return failure if the device is in a resource wait 6778 * or power changing state. 6779 */ 6780 if ((un->un_state == SD_STATE_RWAIT) || 6781 (un->un_state == SD_STATE_PM_CHANGING)) { 6782 mutex_exit(SD_MUTEX(un)); 6783 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6784 "device in resource wait state, exiting\n"); 6785 return (DDI_FAILURE); 6786 } 6787 6788 6789 un->un_save_state = un->un_last_state; 6790 New_state(un, SD_STATE_SUSPENDED); 6791 6792 /* 6793 * Wait for all commands that are in transport or queued to a timer 6794 * for retry to complete. 6795 * 6796 * While waiting, no new commands will be accepted or sent because of 6797 * the new state we set above. 6798 * 6799 * Wait till current operation has completed. If we are in the resource 6800 * wait state (with an intr outstanding) then we need to wait till the 6801 * intr completes and starts the next cmd. We want to wait for 6802 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6803 */ 6804 wait_cmds_complete = ddi_get_lbolt() + 6805 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6806 6807 while (un->un_ncmds_in_transport != 0) { 6808 /* 6809 * Fail if commands do not finish in the specified time. 6810 */ 6811 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6812 wait_cmds_complete) == -1) { 6813 /* 6814 * Undo the state changes made above. Everything 6815 * must go back to it's original value. 6816 */ 6817 Restore_state(un); 6818 un->un_last_state = un->un_save_state; 6819 /* Wake up any threads that might be waiting. */ 6820 cv_broadcast(&un->un_suspend_cv); 6821 mutex_exit(SD_MUTEX(un)); 6822 SD_ERROR(SD_LOG_IO_PM, un, 6823 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6824 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6825 return (DDI_FAILURE); 6826 } 6827 } 6828 6829 /* 6830 * Cancel SCSI watch thread and timeouts, if any are active 6831 */ 6832 6833 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6834 opaque_t temp_token = un->un_swr_token; 6835 mutex_exit(SD_MUTEX(un)); 6836 scsi_watch_suspend(temp_token); 6837 mutex_enter(SD_MUTEX(un)); 6838 } 6839 6840 if (un->un_reset_throttle_timeid != NULL) { 6841 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6842 un->un_reset_throttle_timeid = NULL; 6843 mutex_exit(SD_MUTEX(un)); 6844 (void) untimeout(temp_id); 6845 mutex_enter(SD_MUTEX(un)); 6846 } 6847 6848 if (un->un_dcvb_timeid != NULL) { 6849 timeout_id_t temp_id = un->un_dcvb_timeid; 6850 un->un_dcvb_timeid = NULL; 6851 mutex_exit(SD_MUTEX(un)); 6852 (void) untimeout(temp_id); 6853 mutex_enter(SD_MUTEX(un)); 6854 } 6855 6856 mutex_enter(&un->un_pm_mutex); 6857 if (un->un_pm_timeid != NULL) { 6858 timeout_id_t temp_id = un->un_pm_timeid; 6859 un->un_pm_timeid = NULL; 6860 mutex_exit(&un->un_pm_mutex); 6861 mutex_exit(SD_MUTEX(un)); 6862 (void) untimeout(temp_id); 6863 mutex_enter(SD_MUTEX(un)); 6864 } else { 6865 mutex_exit(&un->un_pm_mutex); 6866 } 6867 6868 if (un->un_retry_timeid != NULL) { 6869 timeout_id_t temp_id = un->un_retry_timeid; 6870 un->un_retry_timeid = NULL; 6871 mutex_exit(SD_MUTEX(un)); 6872 (void) untimeout(temp_id); 6873 mutex_enter(SD_MUTEX(un)); 6874 } 6875 6876 if (un->un_direct_priority_timeid != NULL) { 6877 timeout_id_t temp_id = un->un_direct_priority_timeid; 6878 un->un_direct_priority_timeid = NULL; 6879 mutex_exit(SD_MUTEX(un)); 6880 (void) untimeout(temp_id); 6881 mutex_enter(SD_MUTEX(un)); 6882 } 6883 6884 if (un->un_f_is_fibre == TRUE) { 6885 /* 6886 * Remove callbacks for insert and remove events 6887 */ 6888 if (un->un_insert_event != NULL) { 6889 mutex_exit(SD_MUTEX(un)); 6890 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6891 mutex_enter(SD_MUTEX(un)); 6892 un->un_insert_event = NULL; 6893 } 6894 6895 if (un->un_remove_event != NULL) { 6896 mutex_exit(SD_MUTEX(un)); 6897 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6898 mutex_enter(SD_MUTEX(un)); 6899 un->un_remove_event = NULL; 6900 } 6901 } 6902 6903 mutex_exit(SD_MUTEX(un)); 6904 6905 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6906 6907 return (DDI_SUCCESS); 6908 } 6909 6910 6911 /* 6912 * Function: sd_ddi_pm_suspend 6913 * 6914 * Description: Set the drive state to low power. 6915 * Someone else is required to actually change the drive 6916 * power level. 6917 * 6918 * Arguments: un - driver soft state (unit) structure 6919 * 6920 * Return Code: DDI_FAILURE or DDI_SUCCESS 6921 * 6922 * Context: Kernel thread context 6923 */ 6924 6925 static int 6926 sd_ddi_pm_suspend(struct sd_lun *un) 6927 { 6928 ASSERT(un != NULL); 6929 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 6930 6931 ASSERT(!mutex_owned(SD_MUTEX(un))); 6932 mutex_enter(SD_MUTEX(un)); 6933 6934 /* 6935 * Exit if power management is not enabled for this device, or if 6936 * the device is being used by HA. 6937 */ 6938 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6939 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6940 mutex_exit(SD_MUTEX(un)); 6941 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 6942 return (DDI_SUCCESS); 6943 } 6944 6945 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 6946 un->un_ncmds_in_driver); 6947 6948 /* 6949 * See if the device is not busy, ie.: 6950 * - we have no commands in the driver for this device 6951 * - not waiting for resources 6952 */ 6953 if ((un->un_ncmds_in_driver == 0) && 6954 (un->un_state != SD_STATE_RWAIT)) { 6955 /* 6956 * The device is not busy, so it is OK to go to low power state. 6957 * Indicate low power, but rely on someone else to actually 6958 * change it. 6959 */ 6960 mutex_enter(&un->un_pm_mutex); 6961 un->un_pm_count = -1; 6962 mutex_exit(&un->un_pm_mutex); 6963 un->un_power_level = SD_SPINDLE_OFF; 6964 } 6965 6966 mutex_exit(SD_MUTEX(un)); 6967 6968 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 6969 6970 return (DDI_SUCCESS); 6971 } 6972 6973 6974 /* 6975 * Function: sd_ddi_resume 6976 * 6977 * Description: Performs system power-up operations.. 6978 * 6979 * Return Code: DDI_SUCCESS 6980 * DDI_FAILURE 6981 * 6982 * Context: Kernel thread context 6983 */ 6984 6985 static int 6986 sd_ddi_resume(dev_info_t *devi) 6987 { 6988 struct sd_lun *un; 6989 6990 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6991 if (un == NULL) { 6992 return (DDI_FAILURE); 6993 } 6994 6995 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6996 6997 mutex_enter(SD_MUTEX(un)); 6998 Restore_state(un); 6999 7000 /* 7001 * Restore the state which was saved to give the 7002 * the right state in un_last_state 7003 */ 7004 un->un_last_state = un->un_save_state; 7005 /* 7006 * Note: throttle comes back at full. 7007 * Also note: this MUST be done before calling pm_raise_power 7008 * otherwise the system can get hung in biowait. The scenario where 7009 * this'll happen is under cpr suspend. Writing of the system 7010 * state goes through sddump, which writes 0 to un_throttle. If 7011 * writing the system state then fails, example if the partition is 7012 * too small, then cpr attempts a resume. If throttle isn't restored 7013 * from the saved value until after calling pm_raise_power then 7014 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7015 * in biowait. 7016 */ 7017 un->un_throttle = un->un_saved_throttle; 7018 7019 /* 7020 * The chance of failure is very rare as the only command done in power 7021 * entry point is START command when you transition from 0->1 or 7022 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7023 * which suspend was done. Ignore the return value as the resume should 7024 * not be failed. In the case of removable media the media need not be 7025 * inserted and hence there is a chance that raise power will fail with 7026 * media not present. 7027 */ 7028 if (un->un_f_attach_spinup) { 7029 mutex_exit(SD_MUTEX(un)); 7030 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7031 mutex_enter(SD_MUTEX(un)); 7032 } 7033 7034 /* 7035 * Don't broadcast to the suspend cv and therefore possibly 7036 * start I/O until after power has been restored. 7037 */ 7038 cv_broadcast(&un->un_suspend_cv); 7039 cv_broadcast(&un->un_state_cv); 7040 7041 /* restart thread */ 7042 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7043 scsi_watch_resume(un->un_swr_token); 7044 } 7045 7046 #if (defined(__fibre)) 7047 if (un->un_f_is_fibre == TRUE) { 7048 /* 7049 * Add callbacks for insert and remove events 7050 */ 7051 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7052 sd_init_event_callbacks(un); 7053 } 7054 } 7055 #endif 7056 7057 /* 7058 * Transport any pending commands to the target. 7059 * 7060 * If this is a low-activity device commands in queue will have to wait 7061 * until new commands come in, which may take awhile. Also, we 7062 * specifically don't check un_ncmds_in_transport because we know that 7063 * there really are no commands in progress after the unit was 7064 * suspended and we could have reached the throttle level, been 7065 * suspended, and have no new commands coming in for awhile. Highly 7066 * unlikely, but so is the low-activity disk scenario. 7067 */ 7068 ddi_xbuf_dispatch(un->un_xbuf_attr); 7069 7070 sd_start_cmds(un, NULL); 7071 mutex_exit(SD_MUTEX(un)); 7072 7073 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7074 7075 return (DDI_SUCCESS); 7076 } 7077 7078 7079 /* 7080 * Function: sd_ddi_pm_resume 7081 * 7082 * Description: Set the drive state to powered on. 7083 * Someone else is required to actually change the drive 7084 * power level. 7085 * 7086 * Arguments: un - driver soft state (unit) structure 7087 * 7088 * Return Code: DDI_SUCCESS 7089 * 7090 * Context: Kernel thread context 7091 */ 7092 7093 static int 7094 sd_ddi_pm_resume(struct sd_lun *un) 7095 { 7096 ASSERT(un != NULL); 7097 7098 ASSERT(!mutex_owned(SD_MUTEX(un))); 7099 mutex_enter(SD_MUTEX(un)); 7100 un->un_power_level = SD_SPINDLE_ON; 7101 7102 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7103 mutex_enter(&un->un_pm_mutex); 7104 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7105 un->un_pm_count++; 7106 ASSERT(un->un_pm_count == 0); 7107 /* 7108 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7109 * un_suspend_cv is for a system resume, not a power management 7110 * device resume. (4297749) 7111 * cv_broadcast(&un->un_suspend_cv); 7112 */ 7113 } 7114 mutex_exit(&un->un_pm_mutex); 7115 mutex_exit(SD_MUTEX(un)); 7116 7117 return (DDI_SUCCESS); 7118 } 7119 7120 7121 /* 7122 * Function: sd_pm_idletimeout_handler 7123 * 7124 * Description: A timer routine that's active only while a device is busy. 7125 * The purpose is to extend slightly the pm framework's busy 7126 * view of the device to prevent busy/idle thrashing for 7127 * back-to-back commands. Do this by comparing the current time 7128 * to the time at which the last command completed and when the 7129 * difference is greater than sd_pm_idletime, call 7130 * pm_idle_component. In addition to indicating idle to the pm 7131 * framework, update the chain type to again use the internal pm 7132 * layers of the driver. 7133 * 7134 * Arguments: arg - driver soft state (unit) structure 7135 * 7136 * Context: Executes in a timeout(9F) thread context 7137 */ 7138 7139 static void 7140 sd_pm_idletimeout_handler(void *arg) 7141 { 7142 struct sd_lun *un = arg; 7143 7144 time_t now; 7145 7146 mutex_enter(&sd_detach_mutex); 7147 if (un->un_detach_count != 0) { 7148 /* Abort if the instance is detaching */ 7149 mutex_exit(&sd_detach_mutex); 7150 return; 7151 } 7152 mutex_exit(&sd_detach_mutex); 7153 7154 now = ddi_get_time(); 7155 /* 7156 * Grab both mutexes, in the proper order, since we're accessing 7157 * both PM and softstate variables. 7158 */ 7159 mutex_enter(SD_MUTEX(un)); 7160 mutex_enter(&un->un_pm_mutex); 7161 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7162 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7163 /* 7164 * Update the chain types. 7165 * This takes affect on the next new command received. 7166 */ 7167 if (un->un_f_non_devbsize_supported) { 7168 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7169 } else { 7170 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7171 } 7172 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7173 7174 SD_TRACE(SD_LOG_IO_PM, un, 7175 "sd_pm_idletimeout_handler: idling device\n"); 7176 (void) pm_idle_component(SD_DEVINFO(un), 0); 7177 un->un_pm_idle_timeid = NULL; 7178 } else { 7179 un->un_pm_idle_timeid = 7180 timeout(sd_pm_idletimeout_handler, un, 7181 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7182 } 7183 mutex_exit(&un->un_pm_mutex); 7184 mutex_exit(SD_MUTEX(un)); 7185 } 7186 7187 7188 /* 7189 * Function: sd_pm_timeout_handler 7190 * 7191 * Description: Callback to tell framework we are idle. 7192 * 7193 * Context: timeout(9f) thread context. 7194 */ 7195 7196 static void 7197 sd_pm_timeout_handler(void *arg) 7198 { 7199 struct sd_lun *un = arg; 7200 7201 (void) pm_idle_component(SD_DEVINFO(un), 0); 7202 mutex_enter(&un->un_pm_mutex); 7203 un->un_pm_timeid = NULL; 7204 mutex_exit(&un->un_pm_mutex); 7205 } 7206 7207 7208 /* 7209 * Function: sdpower 7210 * 7211 * Description: PM entry point. 7212 * 7213 * Return Code: DDI_SUCCESS 7214 * DDI_FAILURE 7215 * 7216 * Context: Kernel thread context 7217 */ 7218 7219 static int 7220 sdpower(dev_info_t *devi, int component, int level) 7221 { 7222 struct sd_lun *un; 7223 int instance; 7224 int rval = DDI_SUCCESS; 7225 uint_t i, log_page_size, maxcycles, ncycles; 7226 uchar_t *log_page_data; 7227 int log_sense_page; 7228 int medium_present; 7229 time_t intvlp; 7230 dev_t dev; 7231 struct pm_trans_data sd_pm_tran_data; 7232 uchar_t save_state; 7233 int sval; 7234 uchar_t state_before_pm; 7235 int got_semaphore_here; 7236 7237 instance = ddi_get_instance(devi); 7238 7239 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7240 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7241 component != 0) { 7242 return (DDI_FAILURE); 7243 } 7244 7245 dev = sd_make_device(SD_DEVINFO(un)); 7246 7247 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7248 7249 /* 7250 * Must synchronize power down with close. 7251 * Attempt to decrement/acquire the open/close semaphore, 7252 * but do NOT wait on it. If it's not greater than zero, 7253 * ie. it can't be decremented without waiting, then 7254 * someone else, either open or close, already has it 7255 * and the try returns 0. Use that knowledge here to determine 7256 * if it's OK to change the device power level. 7257 * Also, only increment it on exit if it was decremented, ie. gotten, 7258 * here. 7259 */ 7260 got_semaphore_here = sema_tryp(&un->un_semoclose); 7261 7262 mutex_enter(SD_MUTEX(un)); 7263 7264 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7265 un->un_ncmds_in_driver); 7266 7267 /* 7268 * If un_ncmds_in_driver is non-zero it indicates commands are 7269 * already being processed in the driver, or if the semaphore was 7270 * not gotten here it indicates an open or close is being processed. 7271 * At the same time somebody is requesting to go low power which 7272 * can't happen, therefore we need to return failure. 7273 */ 7274 if ((level == SD_SPINDLE_OFF) && 7275 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7276 mutex_exit(SD_MUTEX(un)); 7277 7278 if (got_semaphore_here != 0) { 7279 sema_v(&un->un_semoclose); 7280 } 7281 SD_TRACE(SD_LOG_IO_PM, un, 7282 "sdpower: exit, device has queued cmds.\n"); 7283 return (DDI_FAILURE); 7284 } 7285 7286 /* 7287 * if it is OFFLINE that means the disk is completely dead 7288 * in our case we have to put the disk in on or off by sending commands 7289 * Of course that will fail anyway so return back here. 7290 * 7291 * Power changes to a device that's OFFLINE or SUSPENDED 7292 * are not allowed. 7293 */ 7294 if ((un->un_state == SD_STATE_OFFLINE) || 7295 (un->un_state == SD_STATE_SUSPENDED)) { 7296 mutex_exit(SD_MUTEX(un)); 7297 7298 if (got_semaphore_here != 0) { 7299 sema_v(&un->un_semoclose); 7300 } 7301 SD_TRACE(SD_LOG_IO_PM, un, 7302 "sdpower: exit, device is off-line.\n"); 7303 return (DDI_FAILURE); 7304 } 7305 7306 /* 7307 * Change the device's state to indicate it's power level 7308 * is being changed. Do this to prevent a power off in the 7309 * middle of commands, which is especially bad on devices 7310 * that are really powered off instead of just spun down. 7311 */ 7312 state_before_pm = un->un_state; 7313 un->un_state = SD_STATE_PM_CHANGING; 7314 7315 mutex_exit(SD_MUTEX(un)); 7316 7317 /* 7318 * If "pm-capable" property is set to TRUE by HBA drivers, 7319 * bypass the following checking, otherwise, check the log 7320 * sense information for this device 7321 */ 7322 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7323 /* 7324 * Get the log sense information to understand whether the 7325 * the powercycle counts have gone beyond the threshhold. 7326 */ 7327 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7328 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7329 7330 mutex_enter(SD_MUTEX(un)); 7331 log_sense_page = un->un_start_stop_cycle_page; 7332 mutex_exit(SD_MUTEX(un)); 7333 7334 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7335 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7336 #ifdef SDDEBUG 7337 if (sd_force_pm_supported) { 7338 /* Force a successful result */ 7339 rval = 0; 7340 } 7341 #endif 7342 if (rval != 0) { 7343 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7344 "Log Sense Failed\n"); 7345 kmem_free(log_page_data, log_page_size); 7346 /* Cannot support power management on those drives */ 7347 7348 if (got_semaphore_here != 0) { 7349 sema_v(&un->un_semoclose); 7350 } 7351 /* 7352 * On exit put the state back to it's original value 7353 * and broadcast to anyone waiting for the power 7354 * change completion. 7355 */ 7356 mutex_enter(SD_MUTEX(un)); 7357 un->un_state = state_before_pm; 7358 cv_broadcast(&un->un_suspend_cv); 7359 mutex_exit(SD_MUTEX(un)); 7360 SD_TRACE(SD_LOG_IO_PM, un, 7361 "sdpower: exit, Log Sense Failed.\n"); 7362 return (DDI_FAILURE); 7363 } 7364 7365 /* 7366 * From the page data - Convert the essential information to 7367 * pm_trans_data 7368 */ 7369 maxcycles = 7370 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7371 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7372 7373 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7374 7375 ncycles = 7376 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7377 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7378 7379 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7380 7381 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7382 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7383 log_page_data[8+i]; 7384 } 7385 7386 kmem_free(log_page_data, log_page_size); 7387 7388 /* 7389 * Call pm_trans_check routine to get the Ok from 7390 * the global policy 7391 */ 7392 7393 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7394 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7395 7396 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7397 #ifdef SDDEBUG 7398 if (sd_force_pm_supported) { 7399 /* Force a successful result */ 7400 rval = 1; 7401 } 7402 #endif 7403 switch (rval) { 7404 case 0: 7405 /* 7406 * Not Ok to Power cycle or error in parameters passed 7407 * Would have given the advised time to consider power 7408 * cycle. Based on the new intvlp parameter we are 7409 * supposed to pretend we are busy so that pm framework 7410 * will never call our power entry point. Because of 7411 * that install a timeout handler and wait for the 7412 * recommended time to elapse so that power management 7413 * can be effective again. 7414 * 7415 * To effect this behavior, call pm_busy_component to 7416 * indicate to the framework this device is busy. 7417 * By not adjusting un_pm_count the rest of PM in 7418 * the driver will function normally, and independant 7419 * of this but because the framework is told the device 7420 * is busy it won't attempt powering down until it gets 7421 * a matching idle. The timeout handler sends this. 7422 * Note: sd_pm_entry can't be called here to do this 7423 * because sdpower may have been called as a result 7424 * of a call to pm_raise_power from within sd_pm_entry. 7425 * 7426 * If a timeout handler is already active then 7427 * don't install another. 7428 */ 7429 mutex_enter(&un->un_pm_mutex); 7430 if (un->un_pm_timeid == NULL) { 7431 un->un_pm_timeid = 7432 timeout(sd_pm_timeout_handler, 7433 un, intvlp * drv_usectohz(1000000)); 7434 mutex_exit(&un->un_pm_mutex); 7435 (void) pm_busy_component(SD_DEVINFO(un), 0); 7436 } else { 7437 mutex_exit(&un->un_pm_mutex); 7438 } 7439 if (got_semaphore_here != 0) { 7440 sema_v(&un->un_semoclose); 7441 } 7442 /* 7443 * On exit put the state back to it's original value 7444 * and broadcast to anyone waiting for the power 7445 * change completion. 7446 */ 7447 mutex_enter(SD_MUTEX(un)); 7448 un->un_state = state_before_pm; 7449 cv_broadcast(&un->un_suspend_cv); 7450 mutex_exit(SD_MUTEX(un)); 7451 7452 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7453 "trans check Failed, not ok to power cycle.\n"); 7454 return (DDI_FAILURE); 7455 7456 case -1: 7457 if (got_semaphore_here != 0) { 7458 sema_v(&un->un_semoclose); 7459 } 7460 /* 7461 * On exit put the state back to it's original value 7462 * and broadcast to anyone waiting for the power 7463 * change completion. 7464 */ 7465 mutex_enter(SD_MUTEX(un)); 7466 un->un_state = state_before_pm; 7467 cv_broadcast(&un->un_suspend_cv); 7468 mutex_exit(SD_MUTEX(un)); 7469 SD_TRACE(SD_LOG_IO_PM, un, 7470 "sdpower: exit, trans check command Failed.\n"); 7471 return (DDI_FAILURE); 7472 } 7473 } 7474 7475 if (level == SD_SPINDLE_OFF) { 7476 /* 7477 * Save the last state... if the STOP FAILS we need it 7478 * for restoring 7479 */ 7480 mutex_enter(SD_MUTEX(un)); 7481 save_state = un->un_last_state; 7482 /* 7483 * There must not be any cmds. getting processed 7484 * in the driver when we get here. Power to the 7485 * device is potentially going off. 7486 */ 7487 ASSERT(un->un_ncmds_in_driver == 0); 7488 mutex_exit(SD_MUTEX(un)); 7489 7490 /* 7491 * For now suspend the device completely before spindle is 7492 * turned off 7493 */ 7494 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7495 if (got_semaphore_here != 0) { 7496 sema_v(&un->un_semoclose); 7497 } 7498 /* 7499 * On exit put the state back to it's original value 7500 * and broadcast to anyone waiting for the power 7501 * change completion. 7502 */ 7503 mutex_enter(SD_MUTEX(un)); 7504 un->un_state = state_before_pm; 7505 cv_broadcast(&un->un_suspend_cv); 7506 mutex_exit(SD_MUTEX(un)); 7507 SD_TRACE(SD_LOG_IO_PM, un, 7508 "sdpower: exit, PM suspend Failed.\n"); 7509 return (DDI_FAILURE); 7510 } 7511 } 7512 7513 /* 7514 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7515 * close, or strategy. Dump no long uses this routine, it uses it's 7516 * own code so it can be done in polled mode. 7517 */ 7518 7519 medium_present = TRUE; 7520 7521 /* 7522 * When powering up, issue a TUR in case the device is at unit 7523 * attention. Don't do retries. Bypass the PM layer, otherwise 7524 * a deadlock on un_pm_busy_cv will occur. 7525 */ 7526 if (level == SD_SPINDLE_ON) { 7527 (void) sd_send_scsi_TEST_UNIT_READY(un, 7528 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7529 } 7530 7531 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7532 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7533 7534 sval = sd_send_scsi_START_STOP_UNIT(un, 7535 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7536 SD_PATH_DIRECT); 7537 /* Command failed, check for media present. */ 7538 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7539 medium_present = FALSE; 7540 } 7541 7542 /* 7543 * The conditions of interest here are: 7544 * if a spindle off with media present fails, 7545 * then restore the state and return an error. 7546 * else if a spindle on fails, 7547 * then return an error (there's no state to restore). 7548 * In all other cases we setup for the new state 7549 * and return success. 7550 */ 7551 switch (level) { 7552 case SD_SPINDLE_OFF: 7553 if ((medium_present == TRUE) && (sval != 0)) { 7554 /* The stop command from above failed */ 7555 rval = DDI_FAILURE; 7556 /* 7557 * The stop command failed, and we have media 7558 * present. Put the level back by calling the 7559 * sd_pm_resume() and set the state back to 7560 * it's previous value. 7561 */ 7562 (void) sd_ddi_pm_resume(un); 7563 mutex_enter(SD_MUTEX(un)); 7564 un->un_last_state = save_state; 7565 mutex_exit(SD_MUTEX(un)); 7566 break; 7567 } 7568 /* 7569 * The stop command from above succeeded. 7570 */ 7571 if (un->un_f_monitor_media_state) { 7572 /* 7573 * Terminate watch thread in case of removable media 7574 * devices going into low power state. This is as per 7575 * the requirements of pm framework, otherwise commands 7576 * will be generated for the device (through watch 7577 * thread), even when the device is in low power state. 7578 */ 7579 mutex_enter(SD_MUTEX(un)); 7580 un->un_f_watcht_stopped = FALSE; 7581 if (un->un_swr_token != NULL) { 7582 opaque_t temp_token = un->un_swr_token; 7583 un->un_f_watcht_stopped = TRUE; 7584 un->un_swr_token = NULL; 7585 mutex_exit(SD_MUTEX(un)); 7586 (void) scsi_watch_request_terminate(temp_token, 7587 SCSI_WATCH_TERMINATE_WAIT); 7588 } else { 7589 mutex_exit(SD_MUTEX(un)); 7590 } 7591 } 7592 break; 7593 7594 default: /* The level requested is spindle on... */ 7595 /* 7596 * Legacy behavior: return success on a failed spinup 7597 * if there is no media in the drive. 7598 * Do this by looking at medium_present here. 7599 */ 7600 if ((sval != 0) && medium_present) { 7601 /* The start command from above failed */ 7602 rval = DDI_FAILURE; 7603 break; 7604 } 7605 /* 7606 * The start command from above succeeded 7607 * Resume the devices now that we have 7608 * started the disks 7609 */ 7610 (void) sd_ddi_pm_resume(un); 7611 7612 /* 7613 * Resume the watch thread since it was suspended 7614 * when the device went into low power mode. 7615 */ 7616 if (un->un_f_monitor_media_state) { 7617 mutex_enter(SD_MUTEX(un)); 7618 if (un->un_f_watcht_stopped == TRUE) { 7619 opaque_t temp_token; 7620 7621 un->un_f_watcht_stopped = FALSE; 7622 mutex_exit(SD_MUTEX(un)); 7623 temp_token = scsi_watch_request_submit( 7624 SD_SCSI_DEVP(un), 7625 sd_check_media_time, 7626 SENSE_LENGTH, sd_media_watch_cb, 7627 (caddr_t)dev); 7628 mutex_enter(SD_MUTEX(un)); 7629 un->un_swr_token = temp_token; 7630 } 7631 mutex_exit(SD_MUTEX(un)); 7632 } 7633 } 7634 if (got_semaphore_here != 0) { 7635 sema_v(&un->un_semoclose); 7636 } 7637 /* 7638 * On exit put the state back to it's original value 7639 * and broadcast to anyone waiting for the power 7640 * change completion. 7641 */ 7642 mutex_enter(SD_MUTEX(un)); 7643 un->un_state = state_before_pm; 7644 cv_broadcast(&un->un_suspend_cv); 7645 mutex_exit(SD_MUTEX(un)); 7646 7647 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7648 7649 return (rval); 7650 } 7651 7652 7653 7654 /* 7655 * Function: sdattach 7656 * 7657 * Description: Driver's attach(9e) entry point function. 7658 * 7659 * Arguments: devi - opaque device info handle 7660 * cmd - attach type 7661 * 7662 * Return Code: DDI_SUCCESS 7663 * DDI_FAILURE 7664 * 7665 * Context: Kernel thread context 7666 */ 7667 7668 static int 7669 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7670 { 7671 switch (cmd) { 7672 case DDI_ATTACH: 7673 return (sd_unit_attach(devi)); 7674 case DDI_RESUME: 7675 return (sd_ddi_resume(devi)); 7676 default: 7677 break; 7678 } 7679 return (DDI_FAILURE); 7680 } 7681 7682 7683 /* 7684 * Function: sddetach 7685 * 7686 * Description: Driver's detach(9E) entry point function. 7687 * 7688 * Arguments: devi - opaque device info handle 7689 * cmd - detach type 7690 * 7691 * Return Code: DDI_SUCCESS 7692 * DDI_FAILURE 7693 * 7694 * Context: Kernel thread context 7695 */ 7696 7697 static int 7698 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7699 { 7700 switch (cmd) { 7701 case DDI_DETACH: 7702 return (sd_unit_detach(devi)); 7703 case DDI_SUSPEND: 7704 return (sd_ddi_suspend(devi)); 7705 default: 7706 break; 7707 } 7708 return (DDI_FAILURE); 7709 } 7710 7711 7712 /* 7713 * Function: sd_sync_with_callback 7714 * 7715 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7716 * state while the callback routine is active. 7717 * 7718 * Arguments: un: softstate structure for the instance 7719 * 7720 * Context: Kernel thread context 7721 */ 7722 7723 static void 7724 sd_sync_with_callback(struct sd_lun *un) 7725 { 7726 ASSERT(un != NULL); 7727 7728 mutex_enter(SD_MUTEX(un)); 7729 7730 ASSERT(un->un_in_callback >= 0); 7731 7732 while (un->un_in_callback > 0) { 7733 mutex_exit(SD_MUTEX(un)); 7734 delay(2); 7735 mutex_enter(SD_MUTEX(un)); 7736 } 7737 7738 mutex_exit(SD_MUTEX(un)); 7739 } 7740 7741 /* 7742 * Function: sd_unit_attach 7743 * 7744 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7745 * the soft state structure for the device and performs 7746 * all necessary structure and device initializations. 7747 * 7748 * Arguments: devi: the system's dev_info_t for the device. 7749 * 7750 * Return Code: DDI_SUCCESS if attach is successful. 7751 * DDI_FAILURE if any part of the attach fails. 7752 * 7753 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7754 * Kernel thread context only. Can sleep. 7755 */ 7756 7757 static int 7758 sd_unit_attach(dev_info_t *devi) 7759 { 7760 struct scsi_device *devp; 7761 struct sd_lun *un; 7762 char *variantp; 7763 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7764 int instance; 7765 int rval; 7766 int wc_enabled; 7767 uint64_t capacity; 7768 uint_t lbasize; 7769 7770 /* 7771 * Retrieve the target driver's private data area. This was set 7772 * up by the HBA. 7773 */ 7774 devp = ddi_get_driver_private(devi); 7775 7776 /* 7777 * Since we have no idea what state things were left in by the last 7778 * user of the device, set up some 'default' settings, ie. turn 'em 7779 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7780 * Do this before the scsi_probe, which sends an inquiry. 7781 * This is a fix for bug (4430280). 7782 * Of special importance is wide-xfer. The drive could have been left 7783 * in wide transfer mode by the last driver to communicate with it, 7784 * this includes us. If that's the case, and if the following is not 7785 * setup properly or we don't re-negotiate with the drive prior to 7786 * transferring data to/from the drive, it causes bus parity errors, 7787 * data overruns, and unexpected interrupts. This first occurred when 7788 * the fix for bug (4378686) was made. 7789 */ 7790 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7791 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7792 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7793 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7794 7795 /* 7796 * Use scsi_probe() to issue an INQUIRY command to the device. 7797 * This call will allocate and fill in the scsi_inquiry structure 7798 * and point the sd_inq member of the scsi_device structure to it. 7799 * If the attach succeeds, then this memory will not be de-allocated 7800 * (via scsi_unprobe()) until the instance is detached. 7801 */ 7802 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7803 goto probe_failed; 7804 } 7805 7806 /* 7807 * Check the device type as specified in the inquiry data and 7808 * claim it if it is of a type that we support. 7809 */ 7810 switch (devp->sd_inq->inq_dtype) { 7811 case DTYPE_DIRECT: 7812 break; 7813 case DTYPE_RODIRECT: 7814 break; 7815 case DTYPE_OPTICAL: 7816 break; 7817 case DTYPE_NOTPRESENT: 7818 default: 7819 /* Unsupported device type; fail the attach. */ 7820 goto probe_failed; 7821 } 7822 7823 /* 7824 * Allocate the soft state structure for this unit. 7825 * 7826 * We rely upon this memory being set to all zeroes by 7827 * ddi_soft_state_zalloc(). We assume that any member of the 7828 * soft state structure that is not explicitly initialized by 7829 * this routine will have a value of zero. 7830 */ 7831 instance = ddi_get_instance(devp->sd_dev); 7832 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7833 goto probe_failed; 7834 } 7835 7836 /* 7837 * Retrieve a pointer to the newly-allocated soft state. 7838 * 7839 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7840 * was successful, unless something has gone horribly wrong and the 7841 * ddi's soft state internals are corrupt (in which case it is 7842 * probably better to halt here than just fail the attach....) 7843 */ 7844 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7845 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7846 instance); 7847 /*NOTREACHED*/ 7848 } 7849 7850 /* 7851 * Link the back ptr of the driver soft state to the scsi_device 7852 * struct for this lun. 7853 * Save a pointer to the softstate in the driver-private area of 7854 * the scsi_device struct. 7855 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7856 * we first set un->un_sd below. 7857 */ 7858 un->un_sd = devp; 7859 devp->sd_private = (opaque_t)un; 7860 7861 /* 7862 * The following must be after devp is stored in the soft state struct. 7863 */ 7864 #ifdef SDDEBUG 7865 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7866 "%s_unit_attach: un:0x%p instance:%d\n", 7867 ddi_driver_name(devi), un, instance); 7868 #endif 7869 7870 /* 7871 * Set up the device type and node type (for the minor nodes). 7872 * By default we assume that the device can at least support the 7873 * Common Command Set. Call it a CD-ROM if it reports itself 7874 * as a RODIRECT device. 7875 */ 7876 switch (devp->sd_inq->inq_dtype) { 7877 case DTYPE_RODIRECT: 7878 un->un_node_type = DDI_NT_CD_CHAN; 7879 un->un_ctype = CTYPE_CDROM; 7880 break; 7881 case DTYPE_OPTICAL: 7882 un->un_node_type = DDI_NT_BLOCK_CHAN; 7883 un->un_ctype = CTYPE_ROD; 7884 break; 7885 default: 7886 un->un_node_type = DDI_NT_BLOCK_CHAN; 7887 un->un_ctype = CTYPE_CCS; 7888 break; 7889 } 7890 7891 /* 7892 * Try to read the interconnect type from the HBA. 7893 * 7894 * Note: This driver is currently compiled as two binaries, a parallel 7895 * scsi version (sd) and a fibre channel version (ssd). All functional 7896 * differences are determined at compile time. In the future a single 7897 * binary will be provided and the inteconnect type will be used to 7898 * differentiate between fibre and parallel scsi behaviors. At that time 7899 * it will be necessary for all fibre channel HBAs to support this 7900 * property. 7901 * 7902 * set un_f_is_fiber to TRUE ( default fiber ) 7903 */ 7904 un->un_f_is_fibre = TRUE; 7905 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7906 case INTERCONNECT_SSA: 7907 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7908 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7909 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7910 break; 7911 case INTERCONNECT_PARALLEL: 7912 un->un_f_is_fibre = FALSE; 7913 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7914 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7915 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7916 break; 7917 case INTERCONNECT_FIBRE: 7918 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7919 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7920 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7921 break; 7922 case INTERCONNECT_FABRIC: 7923 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7924 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7925 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7926 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7927 break; 7928 default: 7929 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7930 /* 7931 * The HBA does not support the "interconnect-type" property 7932 * (or did not provide a recognized type). 7933 * 7934 * Note: This will be obsoleted when a single fibre channel 7935 * and parallel scsi driver is delivered. In the meantime the 7936 * interconnect type will be set to the platform default.If that 7937 * type is not parallel SCSI, it means that we should be 7938 * assuming "ssd" semantics. However, here this also means that 7939 * the FC HBA is not supporting the "interconnect-type" property 7940 * like we expect it to, so log this occurrence. 7941 */ 7942 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7943 if (!SD_IS_PARALLEL_SCSI(un)) { 7944 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7945 "sd_unit_attach: un:0x%p Assuming " 7946 "INTERCONNECT_FIBRE\n", un); 7947 } else { 7948 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7949 "sd_unit_attach: un:0x%p Assuming " 7950 "INTERCONNECT_PARALLEL\n", un); 7951 un->un_f_is_fibre = FALSE; 7952 } 7953 #else 7954 /* 7955 * Note: This source will be implemented when a single fibre 7956 * channel and parallel scsi driver is delivered. The default 7957 * will be to assume that if a device does not support the 7958 * "interconnect-type" property it is a parallel SCSI HBA and 7959 * we will set the interconnect type for parallel scsi. 7960 */ 7961 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7962 un->un_f_is_fibre = FALSE; 7963 #endif 7964 break; 7965 } 7966 7967 if (un->un_f_is_fibre == TRUE) { 7968 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7969 SCSI_VERSION_3) { 7970 switch (un->un_interconnect_type) { 7971 case SD_INTERCONNECT_FIBRE: 7972 case SD_INTERCONNECT_SSA: 7973 un->un_node_type = DDI_NT_BLOCK_WWN; 7974 break; 7975 default: 7976 break; 7977 } 7978 } 7979 } 7980 7981 /* 7982 * Initialize the Request Sense command for the target 7983 */ 7984 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7985 goto alloc_rqs_failed; 7986 } 7987 7988 /* 7989 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7990 * with seperate binary for sd and ssd. 7991 * 7992 * x86 has 1 binary, un_retry_count is set base on connection type. 7993 * The hardcoded values will go away when Sparc uses 1 binary 7994 * for sd and ssd. This hardcoded values need to match 7995 * SD_RETRY_COUNT in sddef.h 7996 * The value used is base on interconnect type. 7997 * fibre = 3, parallel = 5 7998 */ 7999 #if defined(__i386) || defined(__amd64) 8000 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8001 #else 8002 un->un_retry_count = SD_RETRY_COUNT; 8003 #endif 8004 8005 /* 8006 * Set the per disk retry count to the default number of retries 8007 * for disks and CDROMs. This value can be overridden by the 8008 * disk property list or an entry in sd.conf. 8009 */ 8010 un->un_notready_retry_count = 8011 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8012 : DISK_NOT_READY_RETRY_COUNT(un); 8013 8014 /* 8015 * Set the busy retry count to the default value of un_retry_count. 8016 * This can be overridden by entries in sd.conf or the device 8017 * config table. 8018 */ 8019 un->un_busy_retry_count = un->un_retry_count; 8020 8021 /* 8022 * Init the reset threshold for retries. This number determines 8023 * how many retries must be performed before a reset can be issued 8024 * (for certain error conditions). This can be overridden by entries 8025 * in sd.conf or the device config table. 8026 */ 8027 un->un_reset_retry_count = (un->un_retry_count / 2); 8028 8029 /* 8030 * Set the victim_retry_count to the default un_retry_count 8031 */ 8032 un->un_victim_retry_count = (2 * un->un_retry_count); 8033 8034 /* 8035 * Set the reservation release timeout to the default value of 8036 * 5 seconds. This can be overridden by entries in ssd.conf or the 8037 * device config table. 8038 */ 8039 un->un_reserve_release_time = 5; 8040 8041 /* 8042 * Set up the default maximum transfer size. Note that this may 8043 * get updated later in the attach, when setting up default wide 8044 * operations for disks. 8045 */ 8046 #if defined(__i386) || defined(__amd64) 8047 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8048 #else 8049 un->un_max_xfer_size = (uint_t)maxphys; 8050 #endif 8051 8052 /* 8053 * Get "allow bus device reset" property (defaults to "enabled" if 8054 * the property was not defined). This is to disable bus resets for 8055 * certain kinds of error recovery. Note: In the future when a run-time 8056 * fibre check is available the soft state flag should default to 8057 * enabled. 8058 */ 8059 if (un->un_f_is_fibre == TRUE) { 8060 un->un_f_allow_bus_device_reset = TRUE; 8061 } else { 8062 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8063 "allow-bus-device-reset", 1) != 0) { 8064 un->un_f_allow_bus_device_reset = TRUE; 8065 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8066 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8067 un); 8068 } else { 8069 un->un_f_allow_bus_device_reset = FALSE; 8070 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8071 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8072 un); 8073 } 8074 } 8075 8076 /* 8077 * Check if this is an ATAPI device. ATAPI devices use Group 1 8078 * Read/Write commands and Group 2 Mode Sense/Select commands. 8079 * 8080 * Note: The "obsolete" way of doing this is to check for the "atapi" 8081 * property. The new "variant" property with a value of "atapi" has been 8082 * introduced so that future 'variants' of standard SCSI behavior (like 8083 * atapi) could be specified by the underlying HBA drivers by supplying 8084 * a new value for the "variant" property, instead of having to define a 8085 * new property. 8086 */ 8087 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8088 un->un_f_cfg_is_atapi = TRUE; 8089 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8090 "sd_unit_attach: un:0x%p Atapi device\n", un); 8091 } 8092 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8093 &variantp) == DDI_PROP_SUCCESS) { 8094 if (strcmp(variantp, "atapi") == 0) { 8095 un->un_f_cfg_is_atapi = TRUE; 8096 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8097 "sd_unit_attach: un:0x%p Atapi device\n", un); 8098 } 8099 ddi_prop_free(variantp); 8100 } 8101 8102 un->un_cmd_timeout = SD_IO_TIME; 8103 8104 /* Info on current states, statuses, etc. (Updated frequently) */ 8105 un->un_state = SD_STATE_NORMAL; 8106 un->un_last_state = SD_STATE_NORMAL; 8107 8108 /* Control & status info for command throttling */ 8109 un->un_throttle = sd_max_throttle; 8110 un->un_saved_throttle = sd_max_throttle; 8111 un->un_min_throttle = sd_min_throttle; 8112 8113 if (un->un_f_is_fibre == TRUE) { 8114 un->un_f_use_adaptive_throttle = TRUE; 8115 } else { 8116 un->un_f_use_adaptive_throttle = FALSE; 8117 } 8118 8119 /* Removable media support. */ 8120 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8121 un->un_mediastate = DKIO_NONE; 8122 un->un_specified_mediastate = DKIO_NONE; 8123 8124 /* CVs for suspend/resume (PM or DR) */ 8125 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8126 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8127 8128 /* Power management support. */ 8129 un->un_power_level = SD_SPINDLE_UNINIT; 8130 8131 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8132 un->un_f_wcc_inprog = 0; 8133 8134 /* 8135 * The open/close semaphore is used to serialize threads executing 8136 * in the driver's open & close entry point routines for a given 8137 * instance. 8138 */ 8139 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8140 8141 /* 8142 * The conf file entry and softstate variable is a forceful override, 8143 * meaning a non-zero value must be entered to change the default. 8144 */ 8145 un->un_f_disksort_disabled = FALSE; 8146 8147 /* 8148 * Retrieve the properties from the static driver table or the driver 8149 * configuration file (.conf) for this unit and update the soft state 8150 * for the device as needed for the indicated properties. 8151 * Note: the property configuration needs to occur here as some of the 8152 * following routines may have dependancies on soft state flags set 8153 * as part of the driver property configuration. 8154 */ 8155 sd_read_unit_properties(un); 8156 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8157 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8158 8159 /* 8160 * Only if a device has "hotpluggable" property, it is 8161 * treated as hotpluggable device. Otherwise, it is 8162 * regarded as non-hotpluggable one. 8163 */ 8164 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8165 -1) != -1) { 8166 un->un_f_is_hotpluggable = TRUE; 8167 } 8168 8169 /* 8170 * set unit's attributes(flags) according to "hotpluggable" and 8171 * RMB bit in INQUIRY data. 8172 */ 8173 sd_set_unit_attributes(un, devi); 8174 8175 /* 8176 * By default, we mark the capacity, lbasize, and geometry 8177 * as invalid. Only if we successfully read a valid capacity 8178 * will we update the un_blockcount and un_tgt_blocksize with the 8179 * valid values (the geometry will be validated later). 8180 */ 8181 un->un_f_blockcount_is_valid = FALSE; 8182 un->un_f_tgt_blocksize_is_valid = FALSE; 8183 un->un_f_geometry_is_valid = FALSE; 8184 8185 /* 8186 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8187 * otherwise. 8188 */ 8189 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8190 un->un_blockcount = 0; 8191 8192 /* 8193 * Set up the per-instance info needed to determine the correct 8194 * CDBs and other info for issuing commands to the target. 8195 */ 8196 sd_init_cdb_limits(un); 8197 8198 /* 8199 * Set up the IO chains to use, based upon the target type. 8200 */ 8201 if (un->un_f_non_devbsize_supported) { 8202 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8203 } else { 8204 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8205 } 8206 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8207 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8208 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8209 8210 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8211 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8212 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8213 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8214 8215 8216 if (ISCD(un)) { 8217 un->un_additional_codes = sd_additional_codes; 8218 } else { 8219 un->un_additional_codes = NULL; 8220 } 8221 8222 /* 8223 * Create the kstats here so they can be available for attach-time 8224 * routines that send commands to the unit (either polled or via 8225 * sd_send_scsi_cmd). 8226 * 8227 * Note: This is a critical sequence that needs to be maintained: 8228 * 1) Instantiate the kstats here, before any routines using the 8229 * iopath (i.e. sd_send_scsi_cmd). 8230 * 2) Initialize the error stats (sd_set_errstats) and partition 8231 * stats (sd_set_pstats), following sd_validate_geometry(), 8232 * sd_register_devid(), and sd_cache_control(). 8233 */ 8234 8235 un->un_stats = kstat_create(sd_label, instance, 8236 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8237 if (un->un_stats != NULL) { 8238 un->un_stats->ks_lock = SD_MUTEX(un); 8239 kstat_install(un->un_stats); 8240 } 8241 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8242 "sd_unit_attach: un:0x%p un_stats created\n", un); 8243 8244 sd_create_errstats(un, instance); 8245 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8246 "sd_unit_attach: un:0x%p errstats created\n", un); 8247 8248 /* 8249 * The following if/else code was relocated here from below as part 8250 * of the fix for bug (4430280). However with the default setup added 8251 * on entry to this routine, it's no longer absolutely necessary for 8252 * this to be before the call to sd_spin_up_unit. 8253 */ 8254 if (SD_IS_PARALLEL_SCSI(un)) { 8255 /* 8256 * If SCSI-2 tagged queueing is supported by the target 8257 * and by the host adapter then we will enable it. 8258 */ 8259 un->un_tagflags = 0; 8260 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8261 (devp->sd_inq->inq_cmdque) && 8262 (un->un_f_arq_enabled == TRUE)) { 8263 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8264 1, 1) == 1) { 8265 un->un_tagflags = FLAG_STAG; 8266 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8267 "sd_unit_attach: un:0x%p tag queueing " 8268 "enabled\n", un); 8269 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8270 "untagged-qing", 0) == 1) { 8271 un->un_f_opt_queueing = TRUE; 8272 un->un_saved_throttle = un->un_throttle = 8273 min(un->un_throttle, 3); 8274 } else { 8275 un->un_f_opt_queueing = FALSE; 8276 un->un_saved_throttle = un->un_throttle = 1; 8277 } 8278 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8279 == 1) && (un->un_f_arq_enabled == TRUE)) { 8280 /* The Host Adapter supports internal queueing. */ 8281 un->un_f_opt_queueing = TRUE; 8282 un->un_saved_throttle = un->un_throttle = 8283 min(un->un_throttle, 3); 8284 } else { 8285 un->un_f_opt_queueing = FALSE; 8286 un->un_saved_throttle = un->un_throttle = 1; 8287 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8288 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8289 } 8290 8291 8292 /* Setup or tear down default wide operations for disks */ 8293 8294 /* 8295 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8296 * and "ssd_max_xfer_size" to exist simultaneously on the same 8297 * system and be set to different values. In the future this 8298 * code may need to be updated when the ssd module is 8299 * obsoleted and removed from the system. (4299588) 8300 */ 8301 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8302 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8303 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8304 1, 1) == 1) { 8305 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8306 "sd_unit_attach: un:0x%p Wide Transfer " 8307 "enabled\n", un); 8308 } 8309 8310 /* 8311 * If tagged queuing has also been enabled, then 8312 * enable large xfers 8313 */ 8314 if (un->un_saved_throttle == sd_max_throttle) { 8315 un->un_max_xfer_size = 8316 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8317 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8318 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8319 "sd_unit_attach: un:0x%p max transfer " 8320 "size=0x%x\n", un, un->un_max_xfer_size); 8321 } 8322 } else { 8323 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8324 0, 1) == 1) { 8325 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8326 "sd_unit_attach: un:0x%p " 8327 "Wide Transfer disabled\n", un); 8328 } 8329 } 8330 } else { 8331 un->un_tagflags = FLAG_STAG; 8332 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8333 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8334 } 8335 8336 /* 8337 * If this target supports LUN reset, try to enable it. 8338 */ 8339 if (un->un_f_lun_reset_enabled) { 8340 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8341 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8342 "un:0x%p lun_reset capability set\n", un); 8343 } else { 8344 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8345 "un:0x%p lun-reset capability not set\n", un); 8346 } 8347 } 8348 8349 /* 8350 * At this point in the attach, we have enough info in the 8351 * soft state to be able to issue commands to the target. 8352 * 8353 * All command paths used below MUST issue their commands as 8354 * SD_PATH_DIRECT. This is important as intermediate layers 8355 * are not all initialized yet (such as PM). 8356 */ 8357 8358 /* 8359 * Send a TEST UNIT READY command to the device. This should clear 8360 * any outstanding UNIT ATTENTION that may be present. 8361 * 8362 * Note: Don't check for success, just track if there is a reservation, 8363 * this is a throw away command to clear any unit attentions. 8364 * 8365 * Note: This MUST be the first command issued to the target during 8366 * attach to ensure power on UNIT ATTENTIONS are cleared. 8367 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8368 * with attempts at spinning up a device with no media. 8369 */ 8370 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8371 reservation_flag = SD_TARGET_IS_RESERVED; 8372 } 8373 8374 /* 8375 * If the device is NOT a removable media device, attempt to spin 8376 * it up (using the START_STOP_UNIT command) and read its capacity 8377 * (using the READ CAPACITY command). Note, however, that either 8378 * of these could fail and in some cases we would continue with 8379 * the attach despite the failure (see below). 8380 */ 8381 if (un->un_f_descr_format_supported) { 8382 switch (sd_spin_up_unit(un)) { 8383 case 0: 8384 /* 8385 * Spin-up was successful; now try to read the 8386 * capacity. If successful then save the results 8387 * and mark the capacity & lbasize as valid. 8388 */ 8389 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8390 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8391 8392 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8393 &lbasize, SD_PATH_DIRECT)) { 8394 case 0: { 8395 if (capacity > DK_MAX_BLOCKS) { 8396 #ifdef _LP64 8397 if (capacity + 1 > 8398 SD_GROUP1_MAX_ADDRESS) { 8399 /* 8400 * Enable descriptor format 8401 * sense data so that we can 8402 * get 64 bit sense data 8403 * fields. 8404 */ 8405 sd_enable_descr_sense(un); 8406 } 8407 #else 8408 /* 32-bit kernels can't handle this */ 8409 scsi_log(SD_DEVINFO(un), 8410 sd_label, CE_WARN, 8411 "disk has %llu blocks, which " 8412 "is too large for a 32-bit " 8413 "kernel", capacity); 8414 8415 #if defined(__i386) || defined(__amd64) 8416 /* 8417 * Refer to comments related to off-by-1 8418 * at the header of this file. 8419 * 1TB disk was treated as (1T - 512)B 8420 * in the past, so that it might has 8421 * valid VTOC and solaris partitions, 8422 * we have to allow it to continue to 8423 * work. 8424 */ 8425 if (capacity -1 > DK_MAX_BLOCKS) 8426 #endif 8427 goto spinup_failed; 8428 #endif 8429 } 8430 8431 /* 8432 * Here it's not necessary to check the case: 8433 * the capacity of the device is bigger than 8434 * what the max hba cdb can support. Because 8435 * sd_send_scsi_READ_CAPACITY will retrieve 8436 * the capacity by sending USCSI command, which 8437 * is constrained by the max hba cdb. Actually, 8438 * sd_send_scsi_READ_CAPACITY will return 8439 * EINVAL when using bigger cdb than required 8440 * cdb length. Will handle this case in 8441 * "case EINVAL". 8442 */ 8443 8444 /* 8445 * The following relies on 8446 * sd_send_scsi_READ_CAPACITY never 8447 * returning 0 for capacity and/or lbasize. 8448 */ 8449 sd_update_block_info(un, lbasize, capacity); 8450 8451 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8452 "sd_unit_attach: un:0x%p capacity = %ld " 8453 "blocks; lbasize= %ld.\n", un, 8454 un->un_blockcount, un->un_tgt_blocksize); 8455 8456 break; 8457 } 8458 case EINVAL: 8459 /* 8460 * In the case where the max-cdb-length property 8461 * is smaller than the required CDB length for 8462 * a SCSI device, a target driver can fail to 8463 * attach to that device. 8464 */ 8465 scsi_log(SD_DEVINFO(un), 8466 sd_label, CE_WARN, 8467 "disk capacity is too large " 8468 "for current cdb length"); 8469 goto spinup_failed; 8470 case EACCES: 8471 /* 8472 * Should never get here if the spin-up 8473 * succeeded, but code it in anyway. 8474 * From here, just continue with the attach... 8475 */ 8476 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8477 "sd_unit_attach: un:0x%p " 8478 "sd_send_scsi_READ_CAPACITY " 8479 "returned reservation conflict\n", un); 8480 reservation_flag = SD_TARGET_IS_RESERVED; 8481 break; 8482 default: 8483 /* 8484 * Likewise, should never get here if the 8485 * spin-up succeeded. Just continue with 8486 * the attach... 8487 */ 8488 break; 8489 } 8490 break; 8491 case EACCES: 8492 /* 8493 * Device is reserved by another host. In this case 8494 * we could not spin it up or read the capacity, but 8495 * we continue with the attach anyway. 8496 */ 8497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8498 "sd_unit_attach: un:0x%p spin-up reservation " 8499 "conflict.\n", un); 8500 reservation_flag = SD_TARGET_IS_RESERVED; 8501 break; 8502 default: 8503 /* Fail the attach if the spin-up failed. */ 8504 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8505 "sd_unit_attach: un:0x%p spin-up failed.", un); 8506 goto spinup_failed; 8507 } 8508 } 8509 8510 /* 8511 * Check to see if this is a MMC drive 8512 */ 8513 if (ISCD(un)) { 8514 sd_set_mmc_caps(un); 8515 } 8516 8517 /* 8518 * Create the minor nodes for the device. 8519 * Note: If we want to support fdisk on both sparc and intel, this will 8520 * have to separate out the notion that VTOC8 is always sparc, and 8521 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8522 * type will have to be determined at run-time, and the fdisk 8523 * partitioning will have to have been read & set up before we 8524 * create the minor nodes. (any other inits (such as kstats) that 8525 * also ought to be done before creating the minor nodes?) (Doesn't 8526 * setting up the minor nodes kind of imply that we're ready to 8527 * handle an open from userland?) 8528 */ 8529 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8530 goto create_minor_nodes_failed; 8531 } 8532 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8533 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8534 8535 /* 8536 * Add a zero-length attribute to tell the world we support 8537 * kernel ioctls (for layered drivers) 8538 */ 8539 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8540 DDI_KERNEL_IOCTL, NULL, 0); 8541 8542 /* 8543 * Add a boolean property to tell the world we support 8544 * the B_FAILFAST flag (for layered drivers) 8545 */ 8546 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8547 "ddi-failfast-supported", NULL, 0); 8548 8549 /* 8550 * Initialize power management 8551 */ 8552 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8553 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8554 sd_setup_pm(un, devi); 8555 if (un->un_f_pm_is_enabled == FALSE) { 8556 /* 8557 * For performance, point to a jump table that does 8558 * not include pm. 8559 * The direct and priority chains don't change with PM. 8560 * 8561 * Note: this is currently done based on individual device 8562 * capabilities. When an interface for determining system 8563 * power enabled state becomes available, or when additional 8564 * layers are added to the command chain, these values will 8565 * have to be re-evaluated for correctness. 8566 */ 8567 if (un->un_f_non_devbsize_supported) { 8568 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8569 } else { 8570 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8571 } 8572 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8573 } 8574 8575 /* 8576 * This property is set to 0 by HA software to avoid retries 8577 * on a reserved disk. (The preferred property name is 8578 * "retry-on-reservation-conflict") (1189689) 8579 * 8580 * Note: The use of a global here can have unintended consequences. A 8581 * per instance variable is preferrable to match the capabilities of 8582 * different underlying hba's (4402600) 8583 */ 8584 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8585 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8586 sd_retry_on_reservation_conflict); 8587 if (sd_retry_on_reservation_conflict != 0) { 8588 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8589 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8590 sd_retry_on_reservation_conflict); 8591 } 8592 8593 /* Set up options for QFULL handling. */ 8594 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8595 "qfull-retries", -1)) != -1) { 8596 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8597 rval, 1); 8598 } 8599 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8600 "qfull-retry-interval", -1)) != -1) { 8601 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8602 rval, 1); 8603 } 8604 8605 /* 8606 * This just prints a message that announces the existence of the 8607 * device. The message is always printed in the system logfile, but 8608 * only appears on the console if the system is booted with the 8609 * -v (verbose) argument. 8610 */ 8611 ddi_report_dev(devi); 8612 8613 /* 8614 * The framework calls driver attach routines single-threaded 8615 * for a given instance. However we still acquire SD_MUTEX here 8616 * because this required for calling the sd_validate_geometry() 8617 * and sd_register_devid() functions. 8618 */ 8619 mutex_enter(SD_MUTEX(un)); 8620 un->un_f_geometry_is_valid = FALSE; 8621 un->un_mediastate = DKIO_NONE; 8622 un->un_reserved = -1; 8623 8624 /* 8625 * Read and validate the device's geometry (ie, disk label) 8626 * A new unformatted drive will not have a valid geometry, but 8627 * the driver needs to successfully attach to this device so 8628 * the drive can be formatted via ioctls. 8629 */ 8630 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8631 ENOTSUP)) && 8632 (un->un_blockcount < DK_MAX_BLOCKS)) { 8633 /* 8634 * We found a small disk with an EFI label on it; 8635 * we need to fix up the minor nodes accordingly. 8636 */ 8637 ddi_remove_minor_node(devi, "h"); 8638 ddi_remove_minor_node(devi, "h,raw"); 8639 (void) ddi_create_minor_node(devi, "wd", 8640 S_IFBLK, 8641 (instance << SDUNIT_SHIFT) | WD_NODE, 8642 un->un_node_type, NULL); 8643 (void) ddi_create_minor_node(devi, "wd,raw", 8644 S_IFCHR, 8645 (instance << SDUNIT_SHIFT) | WD_NODE, 8646 un->un_node_type, NULL); 8647 } 8648 #if defined(__i386) || defined(__amd64) 8649 else if (un->un_f_capacity_adjusted == 1) { 8650 /* 8651 * Refer to comments related to off-by-1 at the 8652 * header of this file. 8653 * Adjust minor node for 1TB disk. 8654 */ 8655 ddi_remove_minor_node(devi, "wd"); 8656 ddi_remove_minor_node(devi, "wd,raw"); 8657 (void) ddi_create_minor_node(devi, "h", 8658 S_IFBLK, 8659 (instance << SDUNIT_SHIFT) | WD_NODE, 8660 un->un_node_type, NULL); 8661 (void) ddi_create_minor_node(devi, "h,raw", 8662 S_IFCHR, 8663 (instance << SDUNIT_SHIFT) | WD_NODE, 8664 un->un_node_type, NULL); 8665 } 8666 #endif 8667 /* 8668 * Read and initialize the devid for the unit. 8669 */ 8670 ASSERT(un->un_errstats != NULL); 8671 if (un->un_f_devid_supported) { 8672 sd_register_devid(un, devi, reservation_flag); 8673 } 8674 mutex_exit(SD_MUTEX(un)); 8675 8676 #if (defined(__fibre)) 8677 /* 8678 * Register callbacks for fibre only. You can't do this soley 8679 * on the basis of the devid_type because this is hba specific. 8680 * We need to query our hba capabilities to find out whether to 8681 * register or not. 8682 */ 8683 if (un->un_f_is_fibre) { 8684 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8685 sd_init_event_callbacks(un); 8686 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8687 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8688 } 8689 } 8690 #endif 8691 8692 if (un->un_f_opt_disable_cache == TRUE) { 8693 /* 8694 * Disable both read cache and write cache. This is 8695 * the historic behavior of the keywords in the config file. 8696 */ 8697 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8698 0) { 8699 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8700 "sd_unit_attach: un:0x%p Could not disable " 8701 "caching", un); 8702 goto devid_failed; 8703 } 8704 } 8705 8706 /* 8707 * Check the value of the WCE bit now and 8708 * set un_f_write_cache_enabled accordingly. 8709 */ 8710 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8711 mutex_enter(SD_MUTEX(un)); 8712 un->un_f_write_cache_enabled = (wc_enabled != 0); 8713 mutex_exit(SD_MUTEX(un)); 8714 8715 /* 8716 * Set the pstat and error stat values here, so data obtained during the 8717 * previous attach-time routines is available. 8718 * 8719 * Note: This is a critical sequence that needs to be maintained: 8720 * 1) Instantiate the kstats before any routines using the iopath 8721 * (i.e. sd_send_scsi_cmd). 8722 * 2) Initialize the error stats (sd_set_errstats) and partition 8723 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8724 * sd_register_devid(), and sd_cache_control(). 8725 */ 8726 if (un->un_f_pkstats_enabled) { 8727 sd_set_pstats(un); 8728 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8729 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8730 } 8731 8732 sd_set_errstats(un); 8733 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8734 "sd_unit_attach: un:0x%p errstats set\n", un); 8735 8736 /* 8737 * Find out what type of reservation this disk supports. 8738 */ 8739 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8740 case 0: 8741 /* 8742 * SCSI-3 reservations are supported. 8743 */ 8744 un->un_reservation_type = SD_SCSI3_RESERVATION; 8745 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8746 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8747 break; 8748 case ENOTSUP: 8749 /* 8750 * The PERSISTENT RESERVE IN command would not be recognized by 8751 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8752 */ 8753 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8754 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8755 un->un_reservation_type = SD_SCSI2_RESERVATION; 8756 break; 8757 default: 8758 /* 8759 * default to SCSI-3 reservations 8760 */ 8761 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8762 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8763 un->un_reservation_type = SD_SCSI3_RESERVATION; 8764 break; 8765 } 8766 8767 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8768 "sd_unit_attach: un:0x%p exit success\n", un); 8769 8770 return (DDI_SUCCESS); 8771 8772 /* 8773 * An error occurred during the attach; clean up & return failure. 8774 */ 8775 8776 devid_failed: 8777 8778 setup_pm_failed: 8779 ddi_remove_minor_node(devi, NULL); 8780 8781 create_minor_nodes_failed: 8782 /* 8783 * Cleanup from the scsi_ifsetcap() calls (437868) 8784 */ 8785 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8786 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8787 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8788 8789 if (un->un_f_is_fibre == FALSE) { 8790 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8791 } 8792 8793 spinup_failed: 8794 8795 mutex_enter(SD_MUTEX(un)); 8796 8797 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8798 if (un->un_direct_priority_timeid != NULL) { 8799 timeout_id_t temp_id = un->un_direct_priority_timeid; 8800 un->un_direct_priority_timeid = NULL; 8801 mutex_exit(SD_MUTEX(un)); 8802 (void) untimeout(temp_id); 8803 mutex_enter(SD_MUTEX(un)); 8804 } 8805 8806 /* Cancel any pending start/stop timeouts */ 8807 if (un->un_startstop_timeid != NULL) { 8808 timeout_id_t temp_id = un->un_startstop_timeid; 8809 un->un_startstop_timeid = NULL; 8810 mutex_exit(SD_MUTEX(un)); 8811 (void) untimeout(temp_id); 8812 mutex_enter(SD_MUTEX(un)); 8813 } 8814 8815 /* Cancel any pending reset-throttle timeouts */ 8816 if (un->un_reset_throttle_timeid != NULL) { 8817 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8818 un->un_reset_throttle_timeid = NULL; 8819 mutex_exit(SD_MUTEX(un)); 8820 (void) untimeout(temp_id); 8821 mutex_enter(SD_MUTEX(un)); 8822 } 8823 8824 /* Cancel any pending retry timeouts */ 8825 if (un->un_retry_timeid != NULL) { 8826 timeout_id_t temp_id = un->un_retry_timeid; 8827 un->un_retry_timeid = NULL; 8828 mutex_exit(SD_MUTEX(un)); 8829 (void) untimeout(temp_id); 8830 mutex_enter(SD_MUTEX(un)); 8831 } 8832 8833 /* Cancel any pending delayed cv broadcast timeouts */ 8834 if (un->un_dcvb_timeid != NULL) { 8835 timeout_id_t temp_id = un->un_dcvb_timeid; 8836 un->un_dcvb_timeid = NULL; 8837 mutex_exit(SD_MUTEX(un)); 8838 (void) untimeout(temp_id); 8839 mutex_enter(SD_MUTEX(un)); 8840 } 8841 8842 mutex_exit(SD_MUTEX(un)); 8843 8844 /* There should not be any in-progress I/O so ASSERT this check */ 8845 ASSERT(un->un_ncmds_in_transport == 0); 8846 ASSERT(un->un_ncmds_in_driver == 0); 8847 8848 /* Do not free the softstate if the callback routine is active */ 8849 sd_sync_with_callback(un); 8850 8851 /* 8852 * Partition stats apparently are not used with removables. These would 8853 * not have been created during attach, so no need to clean them up... 8854 */ 8855 if (un->un_stats != NULL) { 8856 kstat_delete(un->un_stats); 8857 un->un_stats = NULL; 8858 } 8859 if (un->un_errstats != NULL) { 8860 kstat_delete(un->un_errstats); 8861 un->un_errstats = NULL; 8862 } 8863 8864 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8865 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8866 8867 ddi_prop_remove_all(devi); 8868 sema_destroy(&un->un_semoclose); 8869 cv_destroy(&un->un_state_cv); 8870 8871 getrbuf_failed: 8872 8873 sd_free_rqs(un); 8874 8875 alloc_rqs_failed: 8876 8877 devp->sd_private = NULL; 8878 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8879 8880 get_softstate_failed: 8881 /* 8882 * Note: the man pages are unclear as to whether or not doing a 8883 * ddi_soft_state_free(sd_state, instance) is the right way to 8884 * clean up after the ddi_soft_state_zalloc() if the subsequent 8885 * ddi_get_soft_state() fails. The implication seems to be 8886 * that the get_soft_state cannot fail if the zalloc succeeds. 8887 */ 8888 ddi_soft_state_free(sd_state, instance); 8889 8890 probe_failed: 8891 scsi_unprobe(devp); 8892 #ifdef SDDEBUG 8893 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8894 (sd_level_mask & SD_LOGMASK_TRACE)) { 8895 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8896 (void *)un); 8897 } 8898 #endif 8899 return (DDI_FAILURE); 8900 } 8901 8902 8903 /* 8904 * Function: sd_unit_detach 8905 * 8906 * Description: Performs DDI_DETACH processing for sddetach(). 8907 * 8908 * Return Code: DDI_SUCCESS 8909 * DDI_FAILURE 8910 * 8911 * Context: Kernel thread context 8912 */ 8913 8914 static int 8915 sd_unit_detach(dev_info_t *devi) 8916 { 8917 struct scsi_device *devp; 8918 struct sd_lun *un; 8919 int i; 8920 dev_t dev; 8921 int instance = ddi_get_instance(devi); 8922 8923 mutex_enter(&sd_detach_mutex); 8924 8925 /* 8926 * Fail the detach for any of the following: 8927 * - Unable to get the sd_lun struct for the instance 8928 * - A layered driver has an outstanding open on the instance 8929 * - Another thread is already detaching this instance 8930 * - Another thread is currently performing an open 8931 */ 8932 devp = ddi_get_driver_private(devi); 8933 if ((devp == NULL) || 8934 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8935 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8936 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8937 mutex_exit(&sd_detach_mutex); 8938 return (DDI_FAILURE); 8939 } 8940 8941 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8942 8943 /* 8944 * Mark this instance as currently in a detach, to inhibit any 8945 * opens from a layered driver. 8946 */ 8947 un->un_detach_count++; 8948 mutex_exit(&sd_detach_mutex); 8949 8950 dev = sd_make_device(SD_DEVINFO(un)); 8951 8952 _NOTE(COMPETING_THREADS_NOW); 8953 8954 mutex_enter(SD_MUTEX(un)); 8955 8956 /* 8957 * Fail the detach if there are any outstanding layered 8958 * opens on this device. 8959 */ 8960 for (i = 0; i < NDKMAP; i++) { 8961 if (un->un_ocmap.lyropen[i] != 0) { 8962 goto err_notclosed; 8963 } 8964 } 8965 8966 /* 8967 * Verify there are NO outstanding commands issued to this device. 8968 * ie, un_ncmds_in_transport == 0. 8969 * It's possible to have outstanding commands through the physio 8970 * code path, even though everything's closed. 8971 */ 8972 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8973 (un->un_direct_priority_timeid != NULL) || 8974 (un->un_state == SD_STATE_RWAIT)) { 8975 mutex_exit(SD_MUTEX(un)); 8976 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8977 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8978 goto err_stillbusy; 8979 } 8980 8981 /* 8982 * If we have the device reserved, release the reservation. 8983 */ 8984 if ((un->un_resvd_status & SD_RESERVE) && 8985 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8986 mutex_exit(SD_MUTEX(un)); 8987 /* 8988 * Note: sd_reserve_release sends a command to the device 8989 * via the sd_ioctlcmd() path, and can sleep. 8990 */ 8991 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8992 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8993 "sd_dr_detach: Cannot release reservation \n"); 8994 } 8995 } else { 8996 mutex_exit(SD_MUTEX(un)); 8997 } 8998 8999 /* 9000 * Untimeout any reserve recover, throttle reset, restart unit 9001 * and delayed broadcast timeout threads. Protect the timeout pointer 9002 * from getting nulled by their callback functions. 9003 */ 9004 mutex_enter(SD_MUTEX(un)); 9005 if (un->un_resvd_timeid != NULL) { 9006 timeout_id_t temp_id = un->un_resvd_timeid; 9007 un->un_resvd_timeid = NULL; 9008 mutex_exit(SD_MUTEX(un)); 9009 (void) untimeout(temp_id); 9010 mutex_enter(SD_MUTEX(un)); 9011 } 9012 9013 if (un->un_reset_throttle_timeid != NULL) { 9014 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9015 un->un_reset_throttle_timeid = NULL; 9016 mutex_exit(SD_MUTEX(un)); 9017 (void) untimeout(temp_id); 9018 mutex_enter(SD_MUTEX(un)); 9019 } 9020 9021 if (un->un_startstop_timeid != NULL) { 9022 timeout_id_t temp_id = un->un_startstop_timeid; 9023 un->un_startstop_timeid = NULL; 9024 mutex_exit(SD_MUTEX(un)); 9025 (void) untimeout(temp_id); 9026 mutex_enter(SD_MUTEX(un)); 9027 } 9028 9029 if (un->un_dcvb_timeid != NULL) { 9030 timeout_id_t temp_id = un->un_dcvb_timeid; 9031 un->un_dcvb_timeid = NULL; 9032 mutex_exit(SD_MUTEX(un)); 9033 (void) untimeout(temp_id); 9034 } else { 9035 mutex_exit(SD_MUTEX(un)); 9036 } 9037 9038 /* Remove any pending reservation reclaim requests for this device */ 9039 sd_rmv_resv_reclaim_req(dev); 9040 9041 mutex_enter(SD_MUTEX(un)); 9042 9043 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9044 if (un->un_direct_priority_timeid != NULL) { 9045 timeout_id_t temp_id = un->un_direct_priority_timeid; 9046 un->un_direct_priority_timeid = NULL; 9047 mutex_exit(SD_MUTEX(un)); 9048 (void) untimeout(temp_id); 9049 mutex_enter(SD_MUTEX(un)); 9050 } 9051 9052 /* Cancel any active multi-host disk watch thread requests */ 9053 if (un->un_mhd_token != NULL) { 9054 mutex_exit(SD_MUTEX(un)); 9055 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9056 if (scsi_watch_request_terminate(un->un_mhd_token, 9057 SCSI_WATCH_TERMINATE_NOWAIT)) { 9058 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9059 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9060 /* 9061 * Note: We are returning here after having removed 9062 * some driver timeouts above. This is consistent with 9063 * the legacy implementation but perhaps the watch 9064 * terminate call should be made with the wait flag set. 9065 */ 9066 goto err_stillbusy; 9067 } 9068 mutex_enter(SD_MUTEX(un)); 9069 un->un_mhd_token = NULL; 9070 } 9071 9072 if (un->un_swr_token != NULL) { 9073 mutex_exit(SD_MUTEX(un)); 9074 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9075 if (scsi_watch_request_terminate(un->un_swr_token, 9076 SCSI_WATCH_TERMINATE_NOWAIT)) { 9077 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9078 "sd_dr_detach: Cannot cancel swr watch request\n"); 9079 /* 9080 * Note: We are returning here after having removed 9081 * some driver timeouts above. This is consistent with 9082 * the legacy implementation but perhaps the watch 9083 * terminate call should be made with the wait flag set. 9084 */ 9085 goto err_stillbusy; 9086 } 9087 mutex_enter(SD_MUTEX(un)); 9088 un->un_swr_token = NULL; 9089 } 9090 9091 mutex_exit(SD_MUTEX(un)); 9092 9093 /* 9094 * Clear any scsi_reset_notifies. We clear the reset notifies 9095 * if we have not registered one. 9096 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9097 */ 9098 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9099 sd_mhd_reset_notify_cb, (caddr_t)un); 9100 9101 /* 9102 * protect the timeout pointers from getting nulled by 9103 * their callback functions during the cancellation process. 9104 * In such a scenario untimeout can be invoked with a null value. 9105 */ 9106 _NOTE(NO_COMPETING_THREADS_NOW); 9107 9108 mutex_enter(&un->un_pm_mutex); 9109 if (un->un_pm_idle_timeid != NULL) { 9110 timeout_id_t temp_id = un->un_pm_idle_timeid; 9111 un->un_pm_idle_timeid = NULL; 9112 mutex_exit(&un->un_pm_mutex); 9113 9114 /* 9115 * Timeout is active; cancel it. 9116 * Note that it'll never be active on a device 9117 * that does not support PM therefore we don't 9118 * have to check before calling pm_idle_component. 9119 */ 9120 (void) untimeout(temp_id); 9121 (void) pm_idle_component(SD_DEVINFO(un), 0); 9122 mutex_enter(&un->un_pm_mutex); 9123 } 9124 9125 /* 9126 * Check whether there is already a timeout scheduled for power 9127 * management. If yes then don't lower the power here, that's. 9128 * the timeout handler's job. 9129 */ 9130 if (un->un_pm_timeid != NULL) { 9131 timeout_id_t temp_id = un->un_pm_timeid; 9132 un->un_pm_timeid = NULL; 9133 mutex_exit(&un->un_pm_mutex); 9134 /* 9135 * Timeout is active; cancel it. 9136 * Note that it'll never be active on a device 9137 * that does not support PM therefore we don't 9138 * have to check before calling pm_idle_component. 9139 */ 9140 (void) untimeout(temp_id); 9141 (void) pm_idle_component(SD_DEVINFO(un), 0); 9142 9143 } else { 9144 mutex_exit(&un->un_pm_mutex); 9145 if ((un->un_f_pm_is_enabled == TRUE) && 9146 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9147 DDI_SUCCESS)) { 9148 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9149 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9150 /* 9151 * Fix for bug: 4297749, item # 13 9152 * The above test now includes a check to see if PM is 9153 * supported by this device before call 9154 * pm_lower_power(). 9155 * Note, the following is not dead code. The call to 9156 * pm_lower_power above will generate a call back into 9157 * our sdpower routine which might result in a timeout 9158 * handler getting activated. Therefore the following 9159 * code is valid and necessary. 9160 */ 9161 mutex_enter(&un->un_pm_mutex); 9162 if (un->un_pm_timeid != NULL) { 9163 timeout_id_t temp_id = un->un_pm_timeid; 9164 un->un_pm_timeid = NULL; 9165 mutex_exit(&un->un_pm_mutex); 9166 (void) untimeout(temp_id); 9167 (void) pm_idle_component(SD_DEVINFO(un), 0); 9168 } else { 9169 mutex_exit(&un->un_pm_mutex); 9170 } 9171 } 9172 } 9173 9174 /* 9175 * Cleanup from the scsi_ifsetcap() calls (437868) 9176 * Relocated here from above to be after the call to 9177 * pm_lower_power, which was getting errors. 9178 */ 9179 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9180 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9181 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9182 9183 if (un->un_f_is_fibre == FALSE) { 9184 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9185 } 9186 9187 /* 9188 * Remove any event callbacks, fibre only 9189 */ 9190 if (un->un_f_is_fibre == TRUE) { 9191 if ((un->un_insert_event != NULL) && 9192 (ddi_remove_event_handler(un->un_insert_cb_id) != 9193 DDI_SUCCESS)) { 9194 /* 9195 * Note: We are returning here after having done 9196 * substantial cleanup above. This is consistent 9197 * with the legacy implementation but this may not 9198 * be the right thing to do. 9199 */ 9200 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9201 "sd_dr_detach: Cannot cancel insert event\n"); 9202 goto err_remove_event; 9203 } 9204 un->un_insert_event = NULL; 9205 9206 if ((un->un_remove_event != NULL) && 9207 (ddi_remove_event_handler(un->un_remove_cb_id) != 9208 DDI_SUCCESS)) { 9209 /* 9210 * Note: We are returning here after having done 9211 * substantial cleanup above. This is consistent 9212 * with the legacy implementation but this may not 9213 * be the right thing to do. 9214 */ 9215 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9216 "sd_dr_detach: Cannot cancel remove event\n"); 9217 goto err_remove_event; 9218 } 9219 un->un_remove_event = NULL; 9220 } 9221 9222 /* Do not free the softstate if the callback routine is active */ 9223 sd_sync_with_callback(un); 9224 9225 /* 9226 * Hold the detach mutex here, to make sure that no other threads ever 9227 * can access a (partially) freed soft state structure. 9228 */ 9229 mutex_enter(&sd_detach_mutex); 9230 9231 /* 9232 * Clean up the soft state struct. 9233 * Cleanup is done in reverse order of allocs/inits. 9234 * At this point there should be no competing threads anymore. 9235 */ 9236 9237 /* Unregister and free device id. */ 9238 ddi_devid_unregister(devi); 9239 if (un->un_devid) { 9240 ddi_devid_free(un->un_devid); 9241 un->un_devid = NULL; 9242 } 9243 9244 /* 9245 * Destroy wmap cache if it exists. 9246 */ 9247 if (un->un_wm_cache != NULL) { 9248 kmem_cache_destroy(un->un_wm_cache); 9249 un->un_wm_cache = NULL; 9250 } 9251 9252 /* Remove minor nodes */ 9253 ddi_remove_minor_node(devi, NULL); 9254 9255 /* 9256 * kstat cleanup is done in detach for all device types (4363169). 9257 * We do not want to fail detach if the device kstats are not deleted 9258 * since there is a confusion about the devo_refcnt for the device. 9259 * We just delete the kstats and let detach complete successfully. 9260 */ 9261 if (un->un_stats != NULL) { 9262 kstat_delete(un->un_stats); 9263 un->un_stats = NULL; 9264 } 9265 if (un->un_errstats != NULL) { 9266 kstat_delete(un->un_errstats); 9267 un->un_errstats = NULL; 9268 } 9269 9270 /* Remove partition stats */ 9271 if (un->un_f_pkstats_enabled) { 9272 for (i = 0; i < NSDMAP; i++) { 9273 if (un->un_pstats[i] != NULL) { 9274 kstat_delete(un->un_pstats[i]); 9275 un->un_pstats[i] = NULL; 9276 } 9277 } 9278 } 9279 9280 /* Remove xbuf registration */ 9281 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9282 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9283 9284 /* Remove driver properties */ 9285 ddi_prop_remove_all(devi); 9286 9287 mutex_destroy(&un->un_pm_mutex); 9288 cv_destroy(&un->un_pm_busy_cv); 9289 9290 cv_destroy(&un->un_wcc_cv); 9291 9292 /* Open/close semaphore */ 9293 sema_destroy(&un->un_semoclose); 9294 9295 /* Removable media condvar. */ 9296 cv_destroy(&un->un_state_cv); 9297 9298 /* Suspend/resume condvar. */ 9299 cv_destroy(&un->un_suspend_cv); 9300 cv_destroy(&un->un_disk_busy_cv); 9301 9302 sd_free_rqs(un); 9303 9304 /* Free up soft state */ 9305 devp->sd_private = NULL; 9306 bzero(un, sizeof (struct sd_lun)); 9307 ddi_soft_state_free(sd_state, instance); 9308 9309 mutex_exit(&sd_detach_mutex); 9310 9311 /* This frees up the INQUIRY data associated with the device. */ 9312 scsi_unprobe(devp); 9313 9314 return (DDI_SUCCESS); 9315 9316 err_notclosed: 9317 mutex_exit(SD_MUTEX(un)); 9318 9319 err_stillbusy: 9320 _NOTE(NO_COMPETING_THREADS_NOW); 9321 9322 err_remove_event: 9323 mutex_enter(&sd_detach_mutex); 9324 un->un_detach_count--; 9325 mutex_exit(&sd_detach_mutex); 9326 9327 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9328 return (DDI_FAILURE); 9329 } 9330 9331 9332 /* 9333 * Driver minor node structure and data table 9334 */ 9335 struct driver_minor_data { 9336 char *name; 9337 minor_t minor; 9338 int type; 9339 }; 9340 9341 static struct driver_minor_data sd_minor_data[] = { 9342 {"a", 0, S_IFBLK}, 9343 {"b", 1, S_IFBLK}, 9344 {"c", 2, S_IFBLK}, 9345 {"d", 3, S_IFBLK}, 9346 {"e", 4, S_IFBLK}, 9347 {"f", 5, S_IFBLK}, 9348 {"g", 6, S_IFBLK}, 9349 {"h", 7, S_IFBLK}, 9350 #if defined(_SUNOS_VTOC_16) 9351 {"i", 8, S_IFBLK}, 9352 {"j", 9, S_IFBLK}, 9353 {"k", 10, S_IFBLK}, 9354 {"l", 11, S_IFBLK}, 9355 {"m", 12, S_IFBLK}, 9356 {"n", 13, S_IFBLK}, 9357 {"o", 14, S_IFBLK}, 9358 {"p", 15, S_IFBLK}, 9359 #endif /* defined(_SUNOS_VTOC_16) */ 9360 #if defined(_FIRMWARE_NEEDS_FDISK) 9361 {"q", 16, S_IFBLK}, 9362 {"r", 17, S_IFBLK}, 9363 {"s", 18, S_IFBLK}, 9364 {"t", 19, S_IFBLK}, 9365 {"u", 20, S_IFBLK}, 9366 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9367 {"a,raw", 0, S_IFCHR}, 9368 {"b,raw", 1, S_IFCHR}, 9369 {"c,raw", 2, S_IFCHR}, 9370 {"d,raw", 3, S_IFCHR}, 9371 {"e,raw", 4, S_IFCHR}, 9372 {"f,raw", 5, S_IFCHR}, 9373 {"g,raw", 6, S_IFCHR}, 9374 {"h,raw", 7, S_IFCHR}, 9375 #if defined(_SUNOS_VTOC_16) 9376 {"i,raw", 8, S_IFCHR}, 9377 {"j,raw", 9, S_IFCHR}, 9378 {"k,raw", 10, S_IFCHR}, 9379 {"l,raw", 11, S_IFCHR}, 9380 {"m,raw", 12, S_IFCHR}, 9381 {"n,raw", 13, S_IFCHR}, 9382 {"o,raw", 14, S_IFCHR}, 9383 {"p,raw", 15, S_IFCHR}, 9384 #endif /* defined(_SUNOS_VTOC_16) */ 9385 #if defined(_FIRMWARE_NEEDS_FDISK) 9386 {"q,raw", 16, S_IFCHR}, 9387 {"r,raw", 17, S_IFCHR}, 9388 {"s,raw", 18, S_IFCHR}, 9389 {"t,raw", 19, S_IFCHR}, 9390 {"u,raw", 20, S_IFCHR}, 9391 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9392 {0} 9393 }; 9394 9395 static struct driver_minor_data sd_minor_data_efi[] = { 9396 {"a", 0, S_IFBLK}, 9397 {"b", 1, S_IFBLK}, 9398 {"c", 2, S_IFBLK}, 9399 {"d", 3, S_IFBLK}, 9400 {"e", 4, S_IFBLK}, 9401 {"f", 5, S_IFBLK}, 9402 {"g", 6, S_IFBLK}, 9403 {"wd", 7, S_IFBLK}, 9404 #if defined(_FIRMWARE_NEEDS_FDISK) 9405 {"q", 16, S_IFBLK}, 9406 {"r", 17, S_IFBLK}, 9407 {"s", 18, S_IFBLK}, 9408 {"t", 19, S_IFBLK}, 9409 {"u", 20, S_IFBLK}, 9410 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9411 {"a,raw", 0, S_IFCHR}, 9412 {"b,raw", 1, S_IFCHR}, 9413 {"c,raw", 2, S_IFCHR}, 9414 {"d,raw", 3, S_IFCHR}, 9415 {"e,raw", 4, S_IFCHR}, 9416 {"f,raw", 5, S_IFCHR}, 9417 {"g,raw", 6, S_IFCHR}, 9418 {"wd,raw", 7, S_IFCHR}, 9419 #if defined(_FIRMWARE_NEEDS_FDISK) 9420 {"q,raw", 16, S_IFCHR}, 9421 {"r,raw", 17, S_IFCHR}, 9422 {"s,raw", 18, S_IFCHR}, 9423 {"t,raw", 19, S_IFCHR}, 9424 {"u,raw", 20, S_IFCHR}, 9425 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9426 {0} 9427 }; 9428 9429 9430 /* 9431 * Function: sd_create_minor_nodes 9432 * 9433 * Description: Create the minor device nodes for the instance. 9434 * 9435 * Arguments: un - driver soft state (unit) structure 9436 * devi - pointer to device info structure 9437 * 9438 * Return Code: DDI_SUCCESS 9439 * DDI_FAILURE 9440 * 9441 * Context: Kernel thread context 9442 */ 9443 9444 static int 9445 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9446 { 9447 struct driver_minor_data *dmdp; 9448 struct scsi_device *devp; 9449 int instance; 9450 char name[48]; 9451 9452 ASSERT(un != NULL); 9453 devp = ddi_get_driver_private(devi); 9454 instance = ddi_get_instance(devp->sd_dev); 9455 9456 /* 9457 * Create all the minor nodes for this target. 9458 */ 9459 if (un->un_blockcount > DK_MAX_BLOCKS) 9460 dmdp = sd_minor_data_efi; 9461 else 9462 dmdp = sd_minor_data; 9463 while (dmdp->name != NULL) { 9464 9465 (void) sprintf(name, "%s", dmdp->name); 9466 9467 if (ddi_create_minor_node(devi, name, dmdp->type, 9468 (instance << SDUNIT_SHIFT) | dmdp->minor, 9469 un->un_node_type, NULL) == DDI_FAILURE) { 9470 /* 9471 * Clean up any nodes that may have been created, in 9472 * case this fails in the middle of the loop. 9473 */ 9474 ddi_remove_minor_node(devi, NULL); 9475 return (DDI_FAILURE); 9476 } 9477 dmdp++; 9478 } 9479 9480 return (DDI_SUCCESS); 9481 } 9482 9483 9484 /* 9485 * Function: sd_create_errstats 9486 * 9487 * Description: This routine instantiates the device error stats. 9488 * 9489 * Note: During attach the stats are instantiated first so they are 9490 * available for attach-time routines that utilize the driver 9491 * iopath to send commands to the device. The stats are initialized 9492 * separately so data obtained during some attach-time routines is 9493 * available. (4362483) 9494 * 9495 * Arguments: un - driver soft state (unit) structure 9496 * instance - driver instance 9497 * 9498 * Context: Kernel thread context 9499 */ 9500 9501 static void 9502 sd_create_errstats(struct sd_lun *un, int instance) 9503 { 9504 struct sd_errstats *stp; 9505 char kstatmodule_err[KSTAT_STRLEN]; 9506 char kstatname[KSTAT_STRLEN]; 9507 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9508 9509 ASSERT(un != NULL); 9510 9511 if (un->un_errstats != NULL) { 9512 return; 9513 } 9514 9515 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9516 "%serr", sd_label); 9517 (void) snprintf(kstatname, sizeof (kstatname), 9518 "%s%d,err", sd_label, instance); 9519 9520 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9521 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9522 9523 if (un->un_errstats == NULL) { 9524 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9525 "sd_create_errstats: Failed kstat_create\n"); 9526 return; 9527 } 9528 9529 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9530 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9531 KSTAT_DATA_UINT32); 9532 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9533 KSTAT_DATA_UINT32); 9534 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9535 KSTAT_DATA_UINT32); 9536 kstat_named_init(&stp->sd_vid, "Vendor", 9537 KSTAT_DATA_CHAR); 9538 kstat_named_init(&stp->sd_pid, "Product", 9539 KSTAT_DATA_CHAR); 9540 kstat_named_init(&stp->sd_revision, "Revision", 9541 KSTAT_DATA_CHAR); 9542 kstat_named_init(&stp->sd_serial, "Serial No", 9543 KSTAT_DATA_CHAR); 9544 kstat_named_init(&stp->sd_capacity, "Size", 9545 KSTAT_DATA_ULONGLONG); 9546 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9547 KSTAT_DATA_UINT32); 9548 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9549 KSTAT_DATA_UINT32); 9550 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9551 KSTAT_DATA_UINT32); 9552 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9553 KSTAT_DATA_UINT32); 9554 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9555 KSTAT_DATA_UINT32); 9556 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9557 KSTAT_DATA_UINT32); 9558 9559 un->un_errstats->ks_private = un; 9560 un->un_errstats->ks_update = nulldev; 9561 9562 kstat_install(un->un_errstats); 9563 } 9564 9565 9566 /* 9567 * Function: sd_set_errstats 9568 * 9569 * Description: This routine sets the value of the vendor id, product id, 9570 * revision, serial number, and capacity device error stats. 9571 * 9572 * Note: During attach the stats are instantiated first so they are 9573 * available for attach-time routines that utilize the driver 9574 * iopath to send commands to the device. The stats are initialized 9575 * separately so data obtained during some attach-time routines is 9576 * available. (4362483) 9577 * 9578 * Arguments: un - driver soft state (unit) structure 9579 * 9580 * Context: Kernel thread context 9581 */ 9582 9583 static void 9584 sd_set_errstats(struct sd_lun *un) 9585 { 9586 struct sd_errstats *stp; 9587 9588 ASSERT(un != NULL); 9589 ASSERT(un->un_errstats != NULL); 9590 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9591 ASSERT(stp != NULL); 9592 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9593 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9594 (void) strncpy(stp->sd_revision.value.c, 9595 un->un_sd->sd_inq->inq_revision, 4); 9596 9597 /* 9598 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9599 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9600 * (4376302)) 9601 */ 9602 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9603 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9604 sizeof (SD_INQUIRY(un)->inq_serial)); 9605 } 9606 9607 if (un->un_f_blockcount_is_valid != TRUE) { 9608 /* 9609 * Set capacity error stat to 0 for no media. This ensures 9610 * a valid capacity is displayed in response to 'iostat -E' 9611 * when no media is present in the device. 9612 */ 9613 stp->sd_capacity.value.ui64 = 0; 9614 } else { 9615 /* 9616 * Multiply un_blockcount by un->un_sys_blocksize to get 9617 * capacity. 9618 * 9619 * Note: for non-512 blocksize devices "un_blockcount" has been 9620 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9621 * (un_tgt_blocksize / un->un_sys_blocksize). 9622 */ 9623 stp->sd_capacity.value.ui64 = (uint64_t) 9624 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9625 } 9626 } 9627 9628 9629 /* 9630 * Function: sd_set_pstats 9631 * 9632 * Description: This routine instantiates and initializes the partition 9633 * stats for each partition with more than zero blocks. 9634 * (4363169) 9635 * 9636 * Arguments: un - driver soft state (unit) structure 9637 * 9638 * Context: Kernel thread context 9639 */ 9640 9641 static void 9642 sd_set_pstats(struct sd_lun *un) 9643 { 9644 char kstatname[KSTAT_STRLEN]; 9645 int instance; 9646 int i; 9647 9648 ASSERT(un != NULL); 9649 9650 instance = ddi_get_instance(SD_DEVINFO(un)); 9651 9652 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9653 for (i = 0; i < NSDMAP; i++) { 9654 if ((un->un_pstats[i] == NULL) && 9655 (un->un_map[i].dkl_nblk != 0)) { 9656 (void) snprintf(kstatname, sizeof (kstatname), 9657 "%s%d,%s", sd_label, instance, 9658 sd_minor_data[i].name); 9659 un->un_pstats[i] = kstat_create(sd_label, 9660 instance, kstatname, "partition", KSTAT_TYPE_IO, 9661 1, KSTAT_FLAG_PERSISTENT); 9662 if (un->un_pstats[i] != NULL) { 9663 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9664 kstat_install(un->un_pstats[i]); 9665 } 9666 } 9667 } 9668 } 9669 9670 9671 #if (defined(__fibre)) 9672 /* 9673 * Function: sd_init_event_callbacks 9674 * 9675 * Description: This routine initializes the insertion and removal event 9676 * callbacks. (fibre only) 9677 * 9678 * Arguments: un - driver soft state (unit) structure 9679 * 9680 * Context: Kernel thread context 9681 */ 9682 9683 static void 9684 sd_init_event_callbacks(struct sd_lun *un) 9685 { 9686 ASSERT(un != NULL); 9687 9688 if ((un->un_insert_event == NULL) && 9689 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9690 &un->un_insert_event) == DDI_SUCCESS)) { 9691 /* 9692 * Add the callback for an insertion event 9693 */ 9694 (void) ddi_add_event_handler(SD_DEVINFO(un), 9695 un->un_insert_event, sd_event_callback, (void *)un, 9696 &(un->un_insert_cb_id)); 9697 } 9698 9699 if ((un->un_remove_event == NULL) && 9700 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9701 &un->un_remove_event) == DDI_SUCCESS)) { 9702 /* 9703 * Add the callback for a removal event 9704 */ 9705 (void) ddi_add_event_handler(SD_DEVINFO(un), 9706 un->un_remove_event, sd_event_callback, (void *)un, 9707 &(un->un_remove_cb_id)); 9708 } 9709 } 9710 9711 9712 /* 9713 * Function: sd_event_callback 9714 * 9715 * Description: This routine handles insert/remove events (photon). The 9716 * state is changed to OFFLINE which can be used to supress 9717 * error msgs. (fibre only) 9718 * 9719 * Arguments: un - driver soft state (unit) structure 9720 * 9721 * Context: Callout thread context 9722 */ 9723 /* ARGSUSED */ 9724 static void 9725 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9726 void *bus_impldata) 9727 { 9728 struct sd_lun *un = (struct sd_lun *)arg; 9729 9730 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9731 if (event == un->un_insert_event) { 9732 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9733 mutex_enter(SD_MUTEX(un)); 9734 if (un->un_state == SD_STATE_OFFLINE) { 9735 if (un->un_last_state != SD_STATE_SUSPENDED) { 9736 un->un_state = un->un_last_state; 9737 } else { 9738 /* 9739 * We have gone through SUSPEND/RESUME while 9740 * we were offline. Restore the last state 9741 */ 9742 un->un_state = un->un_save_state; 9743 } 9744 } 9745 mutex_exit(SD_MUTEX(un)); 9746 9747 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9748 } else if (event == un->un_remove_event) { 9749 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9750 mutex_enter(SD_MUTEX(un)); 9751 /* 9752 * We need to handle an event callback that occurs during 9753 * the suspend operation, since we don't prevent it. 9754 */ 9755 if (un->un_state != SD_STATE_OFFLINE) { 9756 if (un->un_state != SD_STATE_SUSPENDED) { 9757 New_state(un, SD_STATE_OFFLINE); 9758 } else { 9759 un->un_last_state = SD_STATE_OFFLINE; 9760 } 9761 } 9762 mutex_exit(SD_MUTEX(un)); 9763 } else { 9764 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9765 "!Unknown event\n"); 9766 } 9767 9768 } 9769 #endif 9770 9771 /* 9772 * Function: sd_cache_control() 9773 * 9774 * Description: This routine is the driver entry point for setting 9775 * read and write caching by modifying the WCE (write cache 9776 * enable) and RCD (read cache disable) bits of mode 9777 * page 8 (MODEPAGE_CACHING). 9778 * 9779 * Arguments: un - driver soft state (unit) structure 9780 * rcd_flag - flag for controlling the read cache 9781 * wce_flag - flag for controlling the write cache 9782 * 9783 * Return Code: EIO 9784 * code returned by sd_send_scsi_MODE_SENSE and 9785 * sd_send_scsi_MODE_SELECT 9786 * 9787 * Context: Kernel Thread 9788 */ 9789 9790 static int 9791 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 9792 { 9793 struct mode_caching *mode_caching_page; 9794 uchar_t *header; 9795 size_t buflen; 9796 int hdrlen; 9797 int bd_len; 9798 int rval = 0; 9799 struct mode_header_grp2 *mhp; 9800 9801 ASSERT(un != NULL); 9802 9803 /* 9804 * Do a test unit ready, otherwise a mode sense may not work if this 9805 * is the first command sent to the device after boot. 9806 */ 9807 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9808 9809 if (un->un_f_cfg_is_atapi == TRUE) { 9810 hdrlen = MODE_HEADER_LENGTH_GRP2; 9811 } else { 9812 hdrlen = MODE_HEADER_LENGTH; 9813 } 9814 9815 /* 9816 * Allocate memory for the retrieved mode page and its headers. Set 9817 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9818 * we get all of the mode sense data otherwise, the mode select 9819 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9820 */ 9821 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9822 sizeof (struct mode_cache_scsi3); 9823 9824 header = kmem_zalloc(buflen, KM_SLEEP); 9825 9826 /* Get the information from the device. */ 9827 if (un->un_f_cfg_is_atapi == TRUE) { 9828 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9829 MODEPAGE_CACHING, SD_PATH_DIRECT); 9830 } else { 9831 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9832 MODEPAGE_CACHING, SD_PATH_DIRECT); 9833 } 9834 if (rval != 0) { 9835 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9836 "sd_cache_control: Mode Sense Failed\n"); 9837 kmem_free(header, buflen); 9838 return (rval); 9839 } 9840 9841 /* 9842 * Determine size of Block Descriptors in order to locate 9843 * the mode page data. ATAPI devices return 0, SCSI devices 9844 * should return MODE_BLK_DESC_LENGTH. 9845 */ 9846 if (un->un_f_cfg_is_atapi == TRUE) { 9847 mhp = (struct mode_header_grp2 *)header; 9848 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9849 } else { 9850 bd_len = ((struct mode_header *)header)->bdesc_length; 9851 } 9852 9853 if (bd_len > MODE_BLK_DESC_LENGTH) { 9854 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9855 "sd_cache_control: Mode Sense returned invalid " 9856 "block descriptor length\n"); 9857 kmem_free(header, buflen); 9858 return (EIO); 9859 } 9860 9861 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9862 9863 /* Check the relevant bits on successful mode sense. */ 9864 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9865 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9866 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9867 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9868 9869 size_t sbuflen; 9870 uchar_t save_pg; 9871 9872 /* 9873 * Construct select buffer length based on the 9874 * length of the sense data returned. 9875 */ 9876 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 9877 sizeof (struct mode_page) + 9878 (int)mode_caching_page->mode_page.length; 9879 9880 /* 9881 * Set the caching bits as requested. 9882 */ 9883 if (rcd_flag == SD_CACHE_ENABLE) 9884 mode_caching_page->rcd = 0; 9885 else if (rcd_flag == SD_CACHE_DISABLE) 9886 mode_caching_page->rcd = 1; 9887 9888 if (wce_flag == SD_CACHE_ENABLE) 9889 mode_caching_page->wce = 1; 9890 else if (wce_flag == SD_CACHE_DISABLE) 9891 mode_caching_page->wce = 0; 9892 9893 /* 9894 * Save the page if the mode sense says the 9895 * drive supports it. 9896 */ 9897 save_pg = mode_caching_page->mode_page.ps ? 9898 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9899 9900 /* Clear reserved bits before mode select. */ 9901 mode_caching_page->mode_page.ps = 0; 9902 9903 /* 9904 * Clear out mode header for mode select. 9905 * The rest of the retrieved page will be reused. 9906 */ 9907 bzero(header, hdrlen); 9908 9909 if (un->un_f_cfg_is_atapi == TRUE) { 9910 mhp = (struct mode_header_grp2 *)header; 9911 mhp->bdesc_length_hi = bd_len >> 8; 9912 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9913 } else { 9914 ((struct mode_header *)header)->bdesc_length = bd_len; 9915 } 9916 9917 /* Issue mode select to change the cache settings */ 9918 if (un->un_f_cfg_is_atapi == TRUE) { 9919 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9920 sbuflen, save_pg, SD_PATH_DIRECT); 9921 } else { 9922 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9923 sbuflen, save_pg, SD_PATH_DIRECT); 9924 } 9925 } 9926 9927 kmem_free(header, buflen); 9928 return (rval); 9929 } 9930 9931 9932 /* 9933 * Function: sd_get_write_cache_enabled() 9934 * 9935 * Description: This routine is the driver entry point for determining if 9936 * write caching is enabled. It examines the WCE (write cache 9937 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9938 * 9939 * Arguments: un - driver soft state (unit) structure 9940 * is_enabled - pointer to int where write cache enabled state 9941 * is returned (non-zero -> write cache enabled) 9942 * 9943 * 9944 * Return Code: EIO 9945 * code returned by sd_send_scsi_MODE_SENSE 9946 * 9947 * Context: Kernel Thread 9948 * 9949 * NOTE: If ioctl is added to disable write cache, this sequence should 9950 * be followed so that no locking is required for accesses to 9951 * un->un_f_write_cache_enabled: 9952 * do mode select to clear wce 9953 * do synchronize cache to flush cache 9954 * set un->un_f_write_cache_enabled = FALSE 9955 * 9956 * Conversely, an ioctl to enable the write cache should be done 9957 * in this order: 9958 * set un->un_f_write_cache_enabled = TRUE 9959 * do mode select to set wce 9960 */ 9961 9962 static int 9963 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 9964 { 9965 struct mode_caching *mode_caching_page; 9966 uchar_t *header; 9967 size_t buflen; 9968 int hdrlen; 9969 int bd_len; 9970 int rval = 0; 9971 9972 ASSERT(un != NULL); 9973 ASSERT(is_enabled != NULL); 9974 9975 /* in case of error, flag as enabled */ 9976 *is_enabled = TRUE; 9977 9978 /* 9979 * Do a test unit ready, otherwise a mode sense may not work if this 9980 * is the first command sent to the device after boot. 9981 */ 9982 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9983 9984 if (un->un_f_cfg_is_atapi == TRUE) { 9985 hdrlen = MODE_HEADER_LENGTH_GRP2; 9986 } else { 9987 hdrlen = MODE_HEADER_LENGTH; 9988 } 9989 9990 /* 9991 * Allocate memory for the retrieved mode page and its headers. Set 9992 * a pointer to the page itself. 9993 */ 9994 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9995 header = kmem_zalloc(buflen, KM_SLEEP); 9996 9997 /* Get the information from the device. */ 9998 if (un->un_f_cfg_is_atapi == TRUE) { 9999 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10000 MODEPAGE_CACHING, SD_PATH_DIRECT); 10001 } else { 10002 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10003 MODEPAGE_CACHING, SD_PATH_DIRECT); 10004 } 10005 if (rval != 0) { 10006 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10007 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10008 kmem_free(header, buflen); 10009 return (rval); 10010 } 10011 10012 /* 10013 * Determine size of Block Descriptors in order to locate 10014 * the mode page data. ATAPI devices return 0, SCSI devices 10015 * should return MODE_BLK_DESC_LENGTH. 10016 */ 10017 if (un->un_f_cfg_is_atapi == TRUE) { 10018 struct mode_header_grp2 *mhp; 10019 mhp = (struct mode_header_grp2 *)header; 10020 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10021 } else { 10022 bd_len = ((struct mode_header *)header)->bdesc_length; 10023 } 10024 10025 if (bd_len > MODE_BLK_DESC_LENGTH) { 10026 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10027 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10028 "block descriptor length\n"); 10029 kmem_free(header, buflen); 10030 return (EIO); 10031 } 10032 10033 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10034 *is_enabled = mode_caching_page->wce; 10035 10036 kmem_free(header, buflen); 10037 return (0); 10038 } 10039 10040 10041 /* 10042 * Function: sd_make_device 10043 * 10044 * Description: Utility routine to return the Solaris device number from 10045 * the data in the device's dev_info structure. 10046 * 10047 * Return Code: The Solaris device number 10048 * 10049 * Context: Any 10050 */ 10051 10052 static dev_t 10053 sd_make_device(dev_info_t *devi) 10054 { 10055 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10056 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10057 } 10058 10059 10060 /* 10061 * Function: sd_pm_entry 10062 * 10063 * Description: Called at the start of a new command to manage power 10064 * and busy status of a device. This includes determining whether 10065 * the current power state of the device is sufficient for 10066 * performing the command or whether it must be changed. 10067 * The PM framework is notified appropriately. 10068 * Only with a return status of DDI_SUCCESS will the 10069 * component be busy to the framework. 10070 * 10071 * All callers of sd_pm_entry must check the return status 10072 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10073 * of DDI_FAILURE indicates the device failed to power up. 10074 * In this case un_pm_count has been adjusted so the result 10075 * on exit is still powered down, ie. count is less than 0. 10076 * Calling sd_pm_exit with this count value hits an ASSERT. 10077 * 10078 * Return Code: DDI_SUCCESS or DDI_FAILURE 10079 * 10080 * Context: Kernel thread context. 10081 */ 10082 10083 static int 10084 sd_pm_entry(struct sd_lun *un) 10085 { 10086 int return_status = DDI_SUCCESS; 10087 10088 ASSERT(!mutex_owned(SD_MUTEX(un))); 10089 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10090 10091 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10092 10093 if (un->un_f_pm_is_enabled == FALSE) { 10094 SD_TRACE(SD_LOG_IO_PM, un, 10095 "sd_pm_entry: exiting, PM not enabled\n"); 10096 return (return_status); 10097 } 10098 10099 /* 10100 * Just increment a counter if PM is enabled. On the transition from 10101 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10102 * the count with each IO and mark the device as idle when the count 10103 * hits 0. 10104 * 10105 * If the count is less than 0 the device is powered down. If a powered 10106 * down device is successfully powered up then the count must be 10107 * incremented to reflect the power up. Note that it'll get incremented 10108 * a second time to become busy. 10109 * 10110 * Because the following has the potential to change the device state 10111 * and must release the un_pm_mutex to do so, only one thread can be 10112 * allowed through at a time. 10113 */ 10114 10115 mutex_enter(&un->un_pm_mutex); 10116 while (un->un_pm_busy == TRUE) { 10117 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10118 } 10119 un->un_pm_busy = TRUE; 10120 10121 if (un->un_pm_count < 1) { 10122 10123 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10124 10125 /* 10126 * Indicate we are now busy so the framework won't attempt to 10127 * power down the device. This call will only fail if either 10128 * we passed a bad component number or the device has no 10129 * components. Neither of these should ever happen. 10130 */ 10131 mutex_exit(&un->un_pm_mutex); 10132 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10133 ASSERT(return_status == DDI_SUCCESS); 10134 10135 mutex_enter(&un->un_pm_mutex); 10136 10137 if (un->un_pm_count < 0) { 10138 mutex_exit(&un->un_pm_mutex); 10139 10140 SD_TRACE(SD_LOG_IO_PM, un, 10141 "sd_pm_entry: power up component\n"); 10142 10143 /* 10144 * pm_raise_power will cause sdpower to be called 10145 * which brings the device power level to the 10146 * desired state, ON in this case. If successful, 10147 * un_pm_count and un_power_level will be updated 10148 * appropriately. 10149 */ 10150 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10151 SD_SPINDLE_ON); 10152 10153 mutex_enter(&un->un_pm_mutex); 10154 10155 if (return_status != DDI_SUCCESS) { 10156 /* 10157 * Power up failed. 10158 * Idle the device and adjust the count 10159 * so the result on exit is that we're 10160 * still powered down, ie. count is less than 0. 10161 */ 10162 SD_TRACE(SD_LOG_IO_PM, un, 10163 "sd_pm_entry: power up failed," 10164 " idle the component\n"); 10165 10166 (void) pm_idle_component(SD_DEVINFO(un), 0); 10167 un->un_pm_count--; 10168 } else { 10169 /* 10170 * Device is powered up, verify the 10171 * count is non-negative. 10172 * This is debug only. 10173 */ 10174 ASSERT(un->un_pm_count == 0); 10175 } 10176 } 10177 10178 if (return_status == DDI_SUCCESS) { 10179 /* 10180 * For performance, now that the device has been tagged 10181 * as busy, and it's known to be powered up, update the 10182 * chain types to use jump tables that do not include 10183 * pm. This significantly lowers the overhead and 10184 * therefore improves performance. 10185 */ 10186 10187 mutex_exit(&un->un_pm_mutex); 10188 mutex_enter(SD_MUTEX(un)); 10189 SD_TRACE(SD_LOG_IO_PM, un, 10190 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10191 un->un_uscsi_chain_type); 10192 10193 if (un->un_f_non_devbsize_supported) { 10194 un->un_buf_chain_type = 10195 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10196 } else { 10197 un->un_buf_chain_type = 10198 SD_CHAIN_INFO_DISK_NO_PM; 10199 } 10200 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10201 10202 SD_TRACE(SD_LOG_IO_PM, un, 10203 " changed uscsi_chain_type to %d\n", 10204 un->un_uscsi_chain_type); 10205 mutex_exit(SD_MUTEX(un)); 10206 mutex_enter(&un->un_pm_mutex); 10207 10208 if (un->un_pm_idle_timeid == NULL) { 10209 /* 300 ms. */ 10210 un->un_pm_idle_timeid = 10211 timeout(sd_pm_idletimeout_handler, un, 10212 (drv_usectohz((clock_t)300000))); 10213 /* 10214 * Include an extra call to busy which keeps the 10215 * device busy with-respect-to the PM layer 10216 * until the timer fires, at which time it'll 10217 * get the extra idle call. 10218 */ 10219 (void) pm_busy_component(SD_DEVINFO(un), 0); 10220 } 10221 } 10222 } 10223 un->un_pm_busy = FALSE; 10224 /* Next... */ 10225 cv_signal(&un->un_pm_busy_cv); 10226 10227 un->un_pm_count++; 10228 10229 SD_TRACE(SD_LOG_IO_PM, un, 10230 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10231 10232 mutex_exit(&un->un_pm_mutex); 10233 10234 return (return_status); 10235 } 10236 10237 10238 /* 10239 * Function: sd_pm_exit 10240 * 10241 * Description: Called at the completion of a command to manage busy 10242 * status for the device. If the device becomes idle the 10243 * PM framework is notified. 10244 * 10245 * Context: Kernel thread context 10246 */ 10247 10248 static void 10249 sd_pm_exit(struct sd_lun *un) 10250 { 10251 ASSERT(!mutex_owned(SD_MUTEX(un))); 10252 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10253 10254 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10255 10256 /* 10257 * After attach the following flag is only read, so don't 10258 * take the penalty of acquiring a mutex for it. 10259 */ 10260 if (un->un_f_pm_is_enabled == TRUE) { 10261 10262 mutex_enter(&un->un_pm_mutex); 10263 un->un_pm_count--; 10264 10265 SD_TRACE(SD_LOG_IO_PM, un, 10266 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10267 10268 ASSERT(un->un_pm_count >= 0); 10269 if (un->un_pm_count == 0) { 10270 mutex_exit(&un->un_pm_mutex); 10271 10272 SD_TRACE(SD_LOG_IO_PM, un, 10273 "sd_pm_exit: idle component\n"); 10274 10275 (void) pm_idle_component(SD_DEVINFO(un), 0); 10276 10277 } else { 10278 mutex_exit(&un->un_pm_mutex); 10279 } 10280 } 10281 10282 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10283 } 10284 10285 10286 /* 10287 * Function: sdopen 10288 * 10289 * Description: Driver's open(9e) entry point function. 10290 * 10291 * Arguments: dev_i - pointer to device number 10292 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10293 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10294 * cred_p - user credential pointer 10295 * 10296 * Return Code: EINVAL 10297 * ENXIO 10298 * EIO 10299 * EROFS 10300 * EBUSY 10301 * 10302 * Context: Kernel thread context 10303 */ 10304 /* ARGSUSED */ 10305 static int 10306 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10307 { 10308 struct sd_lun *un; 10309 int nodelay; 10310 int part; 10311 uint64_t partmask; 10312 int instance; 10313 dev_t dev; 10314 int rval = EIO; 10315 10316 /* Validate the open type */ 10317 if (otyp >= OTYPCNT) { 10318 return (EINVAL); 10319 } 10320 10321 dev = *dev_p; 10322 instance = SDUNIT(dev); 10323 mutex_enter(&sd_detach_mutex); 10324 10325 /* 10326 * Fail the open if there is no softstate for the instance, or 10327 * if another thread somewhere is trying to detach the instance. 10328 */ 10329 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10330 (un->un_detach_count != 0)) { 10331 mutex_exit(&sd_detach_mutex); 10332 /* 10333 * The probe cache only needs to be cleared when open (9e) fails 10334 * with ENXIO (4238046). 10335 */ 10336 /* 10337 * un-conditionally clearing probe cache is ok with 10338 * separate sd/ssd binaries 10339 * x86 platform can be an issue with both parallel 10340 * and fibre in 1 binary 10341 */ 10342 sd_scsi_clear_probe_cache(); 10343 return (ENXIO); 10344 } 10345 10346 /* 10347 * The un_layer_count is to prevent another thread in specfs from 10348 * trying to detach the instance, which can happen when we are 10349 * called from a higher-layer driver instead of thru specfs. 10350 * This will not be needed when DDI provides a layered driver 10351 * interface that allows specfs to know that an instance is in 10352 * use by a layered driver & should not be detached. 10353 * 10354 * Note: the semantics for layered driver opens are exactly one 10355 * close for every open. 10356 */ 10357 if (otyp == OTYP_LYR) { 10358 un->un_layer_count++; 10359 } 10360 10361 /* 10362 * Keep a count of the current # of opens in progress. This is because 10363 * some layered drivers try to call us as a regular open. This can 10364 * cause problems that we cannot prevent, however by keeping this count 10365 * we can at least keep our open and detach routines from racing against 10366 * each other under such conditions. 10367 */ 10368 un->un_opens_in_progress++; 10369 mutex_exit(&sd_detach_mutex); 10370 10371 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10372 part = SDPART(dev); 10373 partmask = 1 << part; 10374 10375 /* 10376 * We use a semaphore here in order to serialize 10377 * open and close requests on the device. 10378 */ 10379 sema_p(&un->un_semoclose); 10380 10381 mutex_enter(SD_MUTEX(un)); 10382 10383 /* 10384 * All device accesses go thru sdstrategy() where we check 10385 * on suspend status but there could be a scsi_poll command, 10386 * which bypasses sdstrategy(), so we need to check pm 10387 * status. 10388 */ 10389 10390 if (!nodelay) { 10391 while ((un->un_state == SD_STATE_SUSPENDED) || 10392 (un->un_state == SD_STATE_PM_CHANGING)) { 10393 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10394 } 10395 10396 mutex_exit(SD_MUTEX(un)); 10397 if (sd_pm_entry(un) != DDI_SUCCESS) { 10398 rval = EIO; 10399 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10400 "sdopen: sd_pm_entry failed\n"); 10401 goto open_failed_with_pm; 10402 } 10403 mutex_enter(SD_MUTEX(un)); 10404 } 10405 10406 /* check for previous exclusive open */ 10407 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10408 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10409 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10410 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10411 10412 if (un->un_exclopen & (partmask)) { 10413 goto excl_open_fail; 10414 } 10415 10416 if (flag & FEXCL) { 10417 int i; 10418 if (un->un_ocmap.lyropen[part]) { 10419 goto excl_open_fail; 10420 } 10421 for (i = 0; i < (OTYPCNT - 1); i++) { 10422 if (un->un_ocmap.regopen[i] & (partmask)) { 10423 goto excl_open_fail; 10424 } 10425 } 10426 } 10427 10428 /* 10429 * Check the write permission if this is a removable media device, 10430 * NDELAY has not been set, and writable permission is requested. 10431 * 10432 * Note: If NDELAY was set and this is write-protected media the WRITE 10433 * attempt will fail with EIO as part of the I/O processing. This is a 10434 * more permissive implementation that allows the open to succeed and 10435 * WRITE attempts to fail when appropriate. 10436 */ 10437 if (un->un_f_chk_wp_open) { 10438 if ((flag & FWRITE) && (!nodelay)) { 10439 mutex_exit(SD_MUTEX(un)); 10440 /* 10441 * Defer the check for write permission on writable 10442 * DVD drive till sdstrategy and will not fail open even 10443 * if FWRITE is set as the device can be writable 10444 * depending upon the media and the media can change 10445 * after the call to open(). 10446 */ 10447 if (un->un_f_dvdram_writable_device == FALSE) { 10448 if (ISCD(un) || sr_check_wp(dev)) { 10449 rval = EROFS; 10450 mutex_enter(SD_MUTEX(un)); 10451 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10452 "write to cd or write protected media\n"); 10453 goto open_fail; 10454 } 10455 } 10456 mutex_enter(SD_MUTEX(un)); 10457 } 10458 } 10459 10460 /* 10461 * If opening in NDELAY/NONBLOCK mode, just return. 10462 * Check if disk is ready and has a valid geometry later. 10463 */ 10464 if (!nodelay) { 10465 mutex_exit(SD_MUTEX(un)); 10466 rval = sd_ready_and_valid(un); 10467 mutex_enter(SD_MUTEX(un)); 10468 /* 10469 * Fail if device is not ready or if the number of disk 10470 * blocks is zero or negative for non CD devices. 10471 */ 10472 if ((rval != SD_READY_VALID) || 10473 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10474 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10475 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10476 "device not ready or invalid disk block value\n"); 10477 goto open_fail; 10478 } 10479 #if defined(__i386) || defined(__amd64) 10480 } else { 10481 uchar_t *cp; 10482 /* 10483 * x86 requires special nodelay handling, so that p0 is 10484 * always defined and accessible. 10485 * Invalidate geometry only if device is not already open. 10486 */ 10487 cp = &un->un_ocmap.chkd[0]; 10488 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10489 if (*cp != (uchar_t)0) { 10490 break; 10491 } 10492 cp++; 10493 } 10494 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10495 un->un_f_geometry_is_valid = FALSE; 10496 } 10497 10498 #endif 10499 } 10500 10501 if (otyp == OTYP_LYR) { 10502 un->un_ocmap.lyropen[part]++; 10503 } else { 10504 un->un_ocmap.regopen[otyp] |= partmask; 10505 } 10506 10507 /* Set up open and exclusive open flags */ 10508 if (flag & FEXCL) { 10509 un->un_exclopen |= (partmask); 10510 } 10511 10512 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10513 "open of part %d type %d\n", part, otyp); 10514 10515 mutex_exit(SD_MUTEX(un)); 10516 if (!nodelay) { 10517 sd_pm_exit(un); 10518 } 10519 10520 sema_v(&un->un_semoclose); 10521 10522 mutex_enter(&sd_detach_mutex); 10523 un->un_opens_in_progress--; 10524 mutex_exit(&sd_detach_mutex); 10525 10526 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10527 return (DDI_SUCCESS); 10528 10529 excl_open_fail: 10530 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10531 rval = EBUSY; 10532 10533 open_fail: 10534 mutex_exit(SD_MUTEX(un)); 10535 10536 /* 10537 * On a failed open we must exit the pm management. 10538 */ 10539 if (!nodelay) { 10540 sd_pm_exit(un); 10541 } 10542 open_failed_with_pm: 10543 sema_v(&un->un_semoclose); 10544 10545 mutex_enter(&sd_detach_mutex); 10546 un->un_opens_in_progress--; 10547 if (otyp == OTYP_LYR) { 10548 un->un_layer_count--; 10549 } 10550 mutex_exit(&sd_detach_mutex); 10551 10552 return (rval); 10553 } 10554 10555 10556 /* 10557 * Function: sdclose 10558 * 10559 * Description: Driver's close(9e) entry point function. 10560 * 10561 * Arguments: dev - device number 10562 * flag - file status flag, informational only 10563 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10564 * cred_p - user credential pointer 10565 * 10566 * Return Code: ENXIO 10567 * 10568 * Context: Kernel thread context 10569 */ 10570 /* ARGSUSED */ 10571 static int 10572 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10573 { 10574 struct sd_lun *un; 10575 uchar_t *cp; 10576 int part; 10577 int nodelay; 10578 int rval = 0; 10579 10580 /* Validate the open type */ 10581 if (otyp >= OTYPCNT) { 10582 return (ENXIO); 10583 } 10584 10585 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10586 return (ENXIO); 10587 } 10588 10589 part = SDPART(dev); 10590 nodelay = flag & (FNDELAY | FNONBLOCK); 10591 10592 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10593 "sdclose: close of part %d type %d\n", part, otyp); 10594 10595 /* 10596 * We use a semaphore here in order to serialize 10597 * open and close requests on the device. 10598 */ 10599 sema_p(&un->un_semoclose); 10600 10601 mutex_enter(SD_MUTEX(un)); 10602 10603 /* Don't proceed if power is being changed. */ 10604 while (un->un_state == SD_STATE_PM_CHANGING) { 10605 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10606 } 10607 10608 if (un->un_exclopen & (1 << part)) { 10609 un->un_exclopen &= ~(1 << part); 10610 } 10611 10612 /* Update the open partition map */ 10613 if (otyp == OTYP_LYR) { 10614 un->un_ocmap.lyropen[part] -= 1; 10615 } else { 10616 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10617 } 10618 10619 cp = &un->un_ocmap.chkd[0]; 10620 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10621 if (*cp != NULL) { 10622 break; 10623 } 10624 cp++; 10625 } 10626 10627 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10628 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10629 10630 /* 10631 * We avoid persistance upon the last close, and set 10632 * the throttle back to the maximum. 10633 */ 10634 un->un_throttle = un->un_saved_throttle; 10635 10636 if (un->un_state == SD_STATE_OFFLINE) { 10637 if (un->un_f_is_fibre == FALSE) { 10638 scsi_log(SD_DEVINFO(un), sd_label, 10639 CE_WARN, "offline\n"); 10640 } 10641 un->un_f_geometry_is_valid = FALSE; 10642 10643 } else { 10644 /* 10645 * Flush any outstanding writes in NVRAM cache. 10646 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10647 * cmd, it may not work for non-Pluto devices. 10648 * SYNCHRONIZE CACHE is not required for removables, 10649 * except DVD-RAM drives. 10650 * 10651 * Also note: because SYNCHRONIZE CACHE is currently 10652 * the only command issued here that requires the 10653 * drive be powered up, only do the power up before 10654 * sending the Sync Cache command. If additional 10655 * commands are added which require a powered up 10656 * drive, the following sequence may have to change. 10657 * 10658 * And finally, note that parallel SCSI on SPARC 10659 * only issues a Sync Cache to DVD-RAM, a newly 10660 * supported device. 10661 */ 10662 #if defined(__i386) || defined(__amd64) 10663 if (un->un_f_sync_cache_supported || 10664 un->un_f_dvdram_writable_device == TRUE) { 10665 #else 10666 if (un->un_f_dvdram_writable_device == TRUE) { 10667 #endif 10668 mutex_exit(SD_MUTEX(un)); 10669 if (sd_pm_entry(un) == DDI_SUCCESS) { 10670 rval = 10671 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10672 NULL); 10673 /* ignore error if not supported */ 10674 if (rval == ENOTSUP) { 10675 rval = 0; 10676 } else if (rval != 0) { 10677 rval = EIO; 10678 } 10679 sd_pm_exit(un); 10680 } else { 10681 rval = EIO; 10682 } 10683 mutex_enter(SD_MUTEX(un)); 10684 } 10685 10686 /* 10687 * For devices which supports DOOR_LOCK, send an ALLOW 10688 * MEDIA REMOVAL command, but don't get upset if it 10689 * fails. We need to raise the power of the drive before 10690 * we can call sd_send_scsi_DOORLOCK() 10691 */ 10692 if (un->un_f_doorlock_supported) { 10693 mutex_exit(SD_MUTEX(un)); 10694 if (sd_pm_entry(un) == DDI_SUCCESS) { 10695 rval = sd_send_scsi_DOORLOCK(un, 10696 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10697 10698 sd_pm_exit(un); 10699 if (ISCD(un) && (rval != 0) && 10700 (nodelay != 0)) { 10701 rval = ENXIO; 10702 } 10703 } else { 10704 rval = EIO; 10705 } 10706 mutex_enter(SD_MUTEX(un)); 10707 } 10708 10709 /* 10710 * If a device has removable media, invalidate all 10711 * parameters related to media, such as geometry, 10712 * blocksize, and blockcount. 10713 */ 10714 if (un->un_f_has_removable_media) { 10715 sr_ejected(un); 10716 } 10717 10718 /* 10719 * Destroy the cache (if it exists) which was 10720 * allocated for the write maps since this is 10721 * the last close for this media. 10722 */ 10723 if (un->un_wm_cache) { 10724 /* 10725 * Check if there are pending commands. 10726 * and if there are give a warning and 10727 * do not destroy the cache. 10728 */ 10729 if (un->un_ncmds_in_driver > 0) { 10730 scsi_log(SD_DEVINFO(un), 10731 sd_label, CE_WARN, 10732 "Unable to clean up memory " 10733 "because of pending I/O\n"); 10734 } else { 10735 kmem_cache_destroy( 10736 un->un_wm_cache); 10737 un->un_wm_cache = NULL; 10738 } 10739 } 10740 } 10741 } 10742 10743 mutex_exit(SD_MUTEX(un)); 10744 sema_v(&un->un_semoclose); 10745 10746 if (otyp == OTYP_LYR) { 10747 mutex_enter(&sd_detach_mutex); 10748 /* 10749 * The detach routine may run when the layer count 10750 * drops to zero. 10751 */ 10752 un->un_layer_count--; 10753 mutex_exit(&sd_detach_mutex); 10754 } 10755 10756 return (rval); 10757 } 10758 10759 10760 /* 10761 * Function: sd_ready_and_valid 10762 * 10763 * Description: Test if device is ready and has a valid geometry. 10764 * 10765 * Arguments: dev - device number 10766 * un - driver soft state (unit) structure 10767 * 10768 * Return Code: SD_READY_VALID ready and valid label 10769 * SD_READY_NOT_VALID ready, geom ops never applicable 10770 * SD_NOT_READY_VALID not ready, no label 10771 * 10772 * Context: Never called at interrupt context. 10773 */ 10774 10775 static int 10776 sd_ready_and_valid(struct sd_lun *un) 10777 { 10778 struct sd_errstats *stp; 10779 uint64_t capacity; 10780 uint_t lbasize; 10781 int rval = SD_READY_VALID; 10782 char name_str[48]; 10783 10784 ASSERT(un != NULL); 10785 ASSERT(!mutex_owned(SD_MUTEX(un))); 10786 10787 mutex_enter(SD_MUTEX(un)); 10788 /* 10789 * If a device has removable media, we must check if media is 10790 * ready when checking if this device is ready and valid. 10791 */ 10792 if (un->un_f_has_removable_media) { 10793 mutex_exit(SD_MUTEX(un)); 10794 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10795 rval = SD_NOT_READY_VALID; 10796 mutex_enter(SD_MUTEX(un)); 10797 goto done; 10798 } 10799 10800 mutex_enter(SD_MUTEX(un)); 10801 if ((un->un_f_geometry_is_valid == FALSE) || 10802 (un->un_f_blockcount_is_valid == FALSE) || 10803 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10804 10805 /* capacity has to be read every open. */ 10806 mutex_exit(SD_MUTEX(un)); 10807 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10808 &lbasize, SD_PATH_DIRECT) != 0) { 10809 mutex_enter(SD_MUTEX(un)); 10810 un->un_f_geometry_is_valid = FALSE; 10811 rval = SD_NOT_READY_VALID; 10812 goto done; 10813 } else { 10814 mutex_enter(SD_MUTEX(un)); 10815 sd_update_block_info(un, lbasize, capacity); 10816 } 10817 } 10818 10819 /* 10820 * Check if the media in the device is writable or not. 10821 */ 10822 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10823 sd_check_for_writable_cd(un); 10824 } 10825 10826 } else { 10827 /* 10828 * Do a test unit ready to clear any unit attention from non-cd 10829 * devices. 10830 */ 10831 mutex_exit(SD_MUTEX(un)); 10832 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10833 mutex_enter(SD_MUTEX(un)); 10834 } 10835 10836 10837 /* 10838 * If this is a non 512 block device, allocate space for 10839 * the wmap cache. This is being done here since every time 10840 * a media is changed this routine will be called and the 10841 * block size is a function of media rather than device. 10842 */ 10843 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 10844 if (!(un->un_wm_cache)) { 10845 (void) snprintf(name_str, sizeof (name_str), 10846 "%s%d_cache", 10847 ddi_driver_name(SD_DEVINFO(un)), 10848 ddi_get_instance(SD_DEVINFO(un))); 10849 un->un_wm_cache = kmem_cache_create( 10850 name_str, sizeof (struct sd_w_map), 10851 8, sd_wm_cache_constructor, 10852 sd_wm_cache_destructor, NULL, 10853 (void *)un, NULL, 0); 10854 if (!(un->un_wm_cache)) { 10855 rval = ENOMEM; 10856 goto done; 10857 } 10858 } 10859 } 10860 10861 if (un->un_state == SD_STATE_NORMAL) { 10862 /* 10863 * If the target is not yet ready here (defined by a TUR 10864 * failure), invalidate the geometry and print an 'offline' 10865 * message. This is a legacy message, as the state of the 10866 * target is not actually changed to SD_STATE_OFFLINE. 10867 * 10868 * If the TUR fails for EACCES (Reservation Conflict), it 10869 * means there actually is nothing wrong with the target that 10870 * would require invalidating the geometry, so continue in 10871 * that case as if the TUR was successful. 10872 */ 10873 int err; 10874 10875 mutex_exit(SD_MUTEX(un)); 10876 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10877 mutex_enter(SD_MUTEX(un)); 10878 10879 if ((err != 0) && (err != EACCES)) { 10880 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10881 "offline\n"); 10882 un->un_f_geometry_is_valid = FALSE; 10883 rval = SD_NOT_READY_VALID; 10884 goto done; 10885 } 10886 } 10887 10888 if (un->un_f_format_in_progress == FALSE) { 10889 /* 10890 * Note: sd_validate_geometry may return TRUE, but that does 10891 * not necessarily mean un_f_geometry_is_valid == TRUE! 10892 */ 10893 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10894 if (rval == ENOTSUP) { 10895 if (un->un_f_geometry_is_valid == TRUE) 10896 rval = 0; 10897 else { 10898 rval = SD_READY_NOT_VALID; 10899 goto done; 10900 } 10901 } 10902 if (rval != 0) { 10903 /* 10904 * We don't check the validity of geometry for 10905 * CDROMs. Also we assume we have a good label 10906 * even if sd_validate_geometry returned ENOMEM. 10907 */ 10908 if (!ISCD(un) && rval != ENOMEM) { 10909 rval = SD_NOT_READY_VALID; 10910 goto done; 10911 } 10912 } 10913 } 10914 10915 /* 10916 * If this device supports DOOR_LOCK command, try and send 10917 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10918 * if it fails. For a CD, however, it is an error 10919 */ 10920 if (un->un_f_doorlock_supported) { 10921 mutex_exit(SD_MUTEX(un)); 10922 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10923 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10924 rval = SD_NOT_READY_VALID; 10925 mutex_enter(SD_MUTEX(un)); 10926 goto done; 10927 } 10928 mutex_enter(SD_MUTEX(un)); 10929 } 10930 10931 /* The state has changed, inform the media watch routines */ 10932 un->un_mediastate = DKIO_INSERTED; 10933 cv_broadcast(&un->un_state_cv); 10934 rval = SD_READY_VALID; 10935 10936 done: 10937 10938 /* 10939 * Initialize the capacity kstat value, if no media previously 10940 * (capacity kstat is 0) and a media has been inserted 10941 * (un_blockcount > 0). 10942 */ 10943 if (un->un_errstats != NULL) { 10944 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10945 if ((stp->sd_capacity.value.ui64 == 0) && 10946 (un->un_f_blockcount_is_valid == TRUE)) { 10947 stp->sd_capacity.value.ui64 = 10948 (uint64_t)((uint64_t)un->un_blockcount * 10949 un->un_sys_blocksize); 10950 } 10951 } 10952 10953 mutex_exit(SD_MUTEX(un)); 10954 return (rval); 10955 } 10956 10957 10958 /* 10959 * Function: sdmin 10960 * 10961 * Description: Routine to limit the size of a data transfer. Used in 10962 * conjunction with physio(9F). 10963 * 10964 * Arguments: bp - pointer to the indicated buf(9S) struct. 10965 * 10966 * Context: Kernel thread context. 10967 */ 10968 10969 static void 10970 sdmin(struct buf *bp) 10971 { 10972 struct sd_lun *un; 10973 int instance; 10974 10975 instance = SDUNIT(bp->b_edev); 10976 10977 un = ddi_get_soft_state(sd_state, instance); 10978 ASSERT(un != NULL); 10979 10980 if (bp->b_bcount > un->un_max_xfer_size) { 10981 bp->b_bcount = un->un_max_xfer_size; 10982 } 10983 } 10984 10985 10986 /* 10987 * Function: sdread 10988 * 10989 * Description: Driver's read(9e) entry point function. 10990 * 10991 * Arguments: dev - device number 10992 * uio - structure pointer describing where data is to be stored 10993 * in user's space 10994 * cred_p - user credential pointer 10995 * 10996 * Return Code: ENXIO 10997 * EIO 10998 * EINVAL 10999 * value returned by physio 11000 * 11001 * Context: Kernel thread context. 11002 */ 11003 /* ARGSUSED */ 11004 static int 11005 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11006 { 11007 struct sd_lun *un = NULL; 11008 int secmask; 11009 int err; 11010 11011 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11012 return (ENXIO); 11013 } 11014 11015 ASSERT(!mutex_owned(SD_MUTEX(un))); 11016 11017 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11018 mutex_enter(SD_MUTEX(un)); 11019 /* 11020 * Because the call to sd_ready_and_valid will issue I/O we 11021 * must wait here if either the device is suspended or 11022 * if it's power level is changing. 11023 */ 11024 while ((un->un_state == SD_STATE_SUSPENDED) || 11025 (un->un_state == SD_STATE_PM_CHANGING)) { 11026 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11027 } 11028 un->un_ncmds_in_driver++; 11029 mutex_exit(SD_MUTEX(un)); 11030 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11031 mutex_enter(SD_MUTEX(un)); 11032 un->un_ncmds_in_driver--; 11033 ASSERT(un->un_ncmds_in_driver >= 0); 11034 mutex_exit(SD_MUTEX(un)); 11035 return (EIO); 11036 } 11037 mutex_enter(SD_MUTEX(un)); 11038 un->un_ncmds_in_driver--; 11039 ASSERT(un->un_ncmds_in_driver >= 0); 11040 mutex_exit(SD_MUTEX(un)); 11041 } 11042 11043 /* 11044 * Read requests are restricted to multiples of the system block size. 11045 */ 11046 secmask = un->un_sys_blocksize - 1; 11047 11048 if (uio->uio_loffset & ((offset_t)(secmask))) { 11049 SD_ERROR(SD_LOG_READ_WRITE, un, 11050 "sdread: file offset not modulo %d\n", 11051 un->un_sys_blocksize); 11052 err = EINVAL; 11053 } else if (uio->uio_iov->iov_len & (secmask)) { 11054 SD_ERROR(SD_LOG_READ_WRITE, un, 11055 "sdread: transfer length not modulo %d\n", 11056 un->un_sys_blocksize); 11057 err = EINVAL; 11058 } else { 11059 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11060 } 11061 return (err); 11062 } 11063 11064 11065 /* 11066 * Function: sdwrite 11067 * 11068 * Description: Driver's write(9e) entry point function. 11069 * 11070 * Arguments: dev - device number 11071 * uio - structure pointer describing where data is stored in 11072 * user's space 11073 * cred_p - user credential pointer 11074 * 11075 * Return Code: ENXIO 11076 * EIO 11077 * EINVAL 11078 * value returned by physio 11079 * 11080 * Context: Kernel thread context. 11081 */ 11082 /* ARGSUSED */ 11083 static int 11084 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11085 { 11086 struct sd_lun *un = NULL; 11087 int secmask; 11088 int err; 11089 11090 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11091 return (ENXIO); 11092 } 11093 11094 ASSERT(!mutex_owned(SD_MUTEX(un))); 11095 11096 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11097 mutex_enter(SD_MUTEX(un)); 11098 /* 11099 * Because the call to sd_ready_and_valid will issue I/O we 11100 * must wait here if either the device is suspended or 11101 * if it's power level is changing. 11102 */ 11103 while ((un->un_state == SD_STATE_SUSPENDED) || 11104 (un->un_state == SD_STATE_PM_CHANGING)) { 11105 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11106 } 11107 un->un_ncmds_in_driver++; 11108 mutex_exit(SD_MUTEX(un)); 11109 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11110 mutex_enter(SD_MUTEX(un)); 11111 un->un_ncmds_in_driver--; 11112 ASSERT(un->un_ncmds_in_driver >= 0); 11113 mutex_exit(SD_MUTEX(un)); 11114 return (EIO); 11115 } 11116 mutex_enter(SD_MUTEX(un)); 11117 un->un_ncmds_in_driver--; 11118 ASSERT(un->un_ncmds_in_driver >= 0); 11119 mutex_exit(SD_MUTEX(un)); 11120 } 11121 11122 /* 11123 * Write requests are restricted to multiples of the system block size. 11124 */ 11125 secmask = un->un_sys_blocksize - 1; 11126 11127 if (uio->uio_loffset & ((offset_t)(secmask))) { 11128 SD_ERROR(SD_LOG_READ_WRITE, un, 11129 "sdwrite: file offset not modulo %d\n", 11130 un->un_sys_blocksize); 11131 err = EINVAL; 11132 } else if (uio->uio_iov->iov_len & (secmask)) { 11133 SD_ERROR(SD_LOG_READ_WRITE, un, 11134 "sdwrite: transfer length not modulo %d\n", 11135 un->un_sys_blocksize); 11136 err = EINVAL; 11137 } else { 11138 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11139 } 11140 return (err); 11141 } 11142 11143 11144 /* 11145 * Function: sdaread 11146 * 11147 * Description: Driver's aread(9e) entry point function. 11148 * 11149 * Arguments: dev - device number 11150 * aio - structure pointer describing where data is to be stored 11151 * cred_p - user credential pointer 11152 * 11153 * Return Code: ENXIO 11154 * EIO 11155 * EINVAL 11156 * value returned by aphysio 11157 * 11158 * Context: Kernel thread context. 11159 */ 11160 /* ARGSUSED */ 11161 static int 11162 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11163 { 11164 struct sd_lun *un = NULL; 11165 struct uio *uio = aio->aio_uio; 11166 int secmask; 11167 int err; 11168 11169 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11170 return (ENXIO); 11171 } 11172 11173 ASSERT(!mutex_owned(SD_MUTEX(un))); 11174 11175 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11176 mutex_enter(SD_MUTEX(un)); 11177 /* 11178 * Because the call to sd_ready_and_valid will issue I/O we 11179 * must wait here if either the device is suspended or 11180 * if it's power level is changing. 11181 */ 11182 while ((un->un_state == SD_STATE_SUSPENDED) || 11183 (un->un_state == SD_STATE_PM_CHANGING)) { 11184 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11185 } 11186 un->un_ncmds_in_driver++; 11187 mutex_exit(SD_MUTEX(un)); 11188 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11189 mutex_enter(SD_MUTEX(un)); 11190 un->un_ncmds_in_driver--; 11191 ASSERT(un->un_ncmds_in_driver >= 0); 11192 mutex_exit(SD_MUTEX(un)); 11193 return (EIO); 11194 } 11195 mutex_enter(SD_MUTEX(un)); 11196 un->un_ncmds_in_driver--; 11197 ASSERT(un->un_ncmds_in_driver >= 0); 11198 mutex_exit(SD_MUTEX(un)); 11199 } 11200 11201 /* 11202 * Read requests are restricted to multiples of the system block size. 11203 */ 11204 secmask = un->un_sys_blocksize - 1; 11205 11206 if (uio->uio_loffset & ((offset_t)(secmask))) { 11207 SD_ERROR(SD_LOG_READ_WRITE, un, 11208 "sdaread: file offset not modulo %d\n", 11209 un->un_sys_blocksize); 11210 err = EINVAL; 11211 } else if (uio->uio_iov->iov_len & (secmask)) { 11212 SD_ERROR(SD_LOG_READ_WRITE, un, 11213 "sdaread: transfer length not modulo %d\n", 11214 un->un_sys_blocksize); 11215 err = EINVAL; 11216 } else { 11217 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11218 } 11219 return (err); 11220 } 11221 11222 11223 /* 11224 * Function: sdawrite 11225 * 11226 * Description: Driver's awrite(9e) entry point function. 11227 * 11228 * Arguments: dev - device number 11229 * aio - structure pointer describing where data is stored 11230 * cred_p - user credential pointer 11231 * 11232 * Return Code: ENXIO 11233 * EIO 11234 * EINVAL 11235 * value returned by aphysio 11236 * 11237 * Context: Kernel thread context. 11238 */ 11239 /* ARGSUSED */ 11240 static int 11241 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11242 { 11243 struct sd_lun *un = NULL; 11244 struct uio *uio = aio->aio_uio; 11245 int secmask; 11246 int err; 11247 11248 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11249 return (ENXIO); 11250 } 11251 11252 ASSERT(!mutex_owned(SD_MUTEX(un))); 11253 11254 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11255 mutex_enter(SD_MUTEX(un)); 11256 /* 11257 * Because the call to sd_ready_and_valid will issue I/O we 11258 * must wait here if either the device is suspended or 11259 * if it's power level is changing. 11260 */ 11261 while ((un->un_state == SD_STATE_SUSPENDED) || 11262 (un->un_state == SD_STATE_PM_CHANGING)) { 11263 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11264 } 11265 un->un_ncmds_in_driver++; 11266 mutex_exit(SD_MUTEX(un)); 11267 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11268 mutex_enter(SD_MUTEX(un)); 11269 un->un_ncmds_in_driver--; 11270 ASSERT(un->un_ncmds_in_driver >= 0); 11271 mutex_exit(SD_MUTEX(un)); 11272 return (EIO); 11273 } 11274 mutex_enter(SD_MUTEX(un)); 11275 un->un_ncmds_in_driver--; 11276 ASSERT(un->un_ncmds_in_driver >= 0); 11277 mutex_exit(SD_MUTEX(un)); 11278 } 11279 11280 /* 11281 * Write requests are restricted to multiples of the system block size. 11282 */ 11283 secmask = un->un_sys_blocksize - 1; 11284 11285 if (uio->uio_loffset & ((offset_t)(secmask))) { 11286 SD_ERROR(SD_LOG_READ_WRITE, un, 11287 "sdawrite: file offset not modulo %d\n", 11288 un->un_sys_blocksize); 11289 err = EINVAL; 11290 } else if (uio->uio_iov->iov_len & (secmask)) { 11291 SD_ERROR(SD_LOG_READ_WRITE, un, 11292 "sdawrite: transfer length not modulo %d\n", 11293 un->un_sys_blocksize); 11294 err = EINVAL; 11295 } else { 11296 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11297 } 11298 return (err); 11299 } 11300 11301 11302 11303 11304 11305 /* 11306 * Driver IO processing follows the following sequence: 11307 * 11308 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11309 * | | ^ 11310 * v v | 11311 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11312 * | | | | 11313 * v | | | 11314 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11315 * | | ^ ^ 11316 * v v | | 11317 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11318 * | | | | 11319 * +---+ | +------------+ +-------+ 11320 * | | | | 11321 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11322 * | v | | 11323 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11324 * | | ^ | 11325 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11326 * | v | | 11327 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11328 * | | ^ | 11329 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11330 * | v | | 11331 * | sd_checksum_iostart() sd_checksum_iodone() | 11332 * | | ^ | 11333 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11334 * | v | | 11335 * | sd_pm_iostart() sd_pm_iodone() | 11336 * | | ^ | 11337 * | | | | 11338 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11339 * | ^ 11340 * v | 11341 * sd_core_iostart() | 11342 * | | 11343 * | +------>(*destroypkt)() 11344 * +-> sd_start_cmds() <-+ | | 11345 * | | | v 11346 * | | | scsi_destroy_pkt(9F) 11347 * | | | 11348 * +->(*initpkt)() +- sdintr() 11349 * | | | | 11350 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11351 * | +-> scsi_setup_cdb(9F) | 11352 * | | 11353 * +--> scsi_transport(9F) | 11354 * | | 11355 * +----> SCSA ---->+ 11356 * 11357 * 11358 * This code is based upon the following presumtions: 11359 * 11360 * - iostart and iodone functions operate on buf(9S) structures. These 11361 * functions perform the necessary operations on the buf(9S) and pass 11362 * them along to the next function in the chain by using the macros 11363 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11364 * (for iodone side functions). 11365 * 11366 * - The iostart side functions may sleep. The iodone side functions 11367 * are called under interrupt context and may NOT sleep. Therefore 11368 * iodone side functions also may not call iostart side functions. 11369 * (NOTE: iostart side functions should NOT sleep for memory, as 11370 * this could result in deadlock.) 11371 * 11372 * - An iostart side function may call its corresponding iodone side 11373 * function directly (if necessary). 11374 * 11375 * - In the event of an error, an iostart side function can return a buf(9S) 11376 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11377 * b_error in the usual way of course). 11378 * 11379 * - The taskq mechanism may be used by the iodone side functions to dispatch 11380 * requests to the iostart side functions. The iostart side functions in 11381 * this case would be called under the context of a taskq thread, so it's 11382 * OK for them to block/sleep/spin in this case. 11383 * 11384 * - iostart side functions may allocate "shadow" buf(9S) structs and 11385 * pass them along to the next function in the chain. The corresponding 11386 * iodone side functions must coalesce the "shadow" bufs and return 11387 * the "original" buf to the next higher layer. 11388 * 11389 * - The b_private field of the buf(9S) struct holds a pointer to 11390 * an sd_xbuf struct, which contains information needed to 11391 * construct the scsi_pkt for the command. 11392 * 11393 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11394 * layer must acquire & release the SD_MUTEX(un) as needed. 11395 */ 11396 11397 11398 /* 11399 * Create taskq for all targets in the system. This is created at 11400 * _init(9E) and destroyed at _fini(9E). 11401 * 11402 * Note: here we set the minalloc to a reasonably high number to ensure that 11403 * we will have an adequate supply of task entries available at interrupt time. 11404 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11405 * sd_create_taskq(). Since we do not want to sleep for allocations at 11406 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11407 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11408 * requests any one instant in time. 11409 */ 11410 #define SD_TASKQ_NUMTHREADS 8 11411 #define SD_TASKQ_MINALLOC 256 11412 #define SD_TASKQ_MAXALLOC 256 11413 11414 static taskq_t *sd_tq = NULL; 11415 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11416 11417 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11418 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11419 11420 /* 11421 * The following task queue is being created for the write part of 11422 * read-modify-write of non-512 block size devices. 11423 * Limit the number of threads to 1 for now. This number has been choosen 11424 * considering the fact that it applies only to dvd ram drives/MO drives 11425 * currently. Performance for which is not main criteria at this stage. 11426 * Note: It needs to be explored if we can use a single taskq in future 11427 */ 11428 #define SD_WMR_TASKQ_NUMTHREADS 1 11429 static taskq_t *sd_wmr_tq = NULL; 11430 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11431 11432 /* 11433 * Function: sd_taskq_create 11434 * 11435 * Description: Create taskq thread(s) and preallocate task entries 11436 * 11437 * Return Code: Returns a pointer to the allocated taskq_t. 11438 * 11439 * Context: Can sleep. Requires blockable context. 11440 * 11441 * Notes: - The taskq() facility currently is NOT part of the DDI. 11442 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11443 * - taskq_create() will block for memory, also it will panic 11444 * if it cannot create the requested number of threads. 11445 * - Currently taskq_create() creates threads that cannot be 11446 * swapped. 11447 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11448 * supply of taskq entries at interrupt time (ie, so that we 11449 * do not have to sleep for memory) 11450 */ 11451 11452 static void 11453 sd_taskq_create(void) 11454 { 11455 char taskq_name[TASKQ_NAMELEN]; 11456 11457 ASSERT(sd_tq == NULL); 11458 ASSERT(sd_wmr_tq == NULL); 11459 11460 (void) snprintf(taskq_name, sizeof (taskq_name), 11461 "%s_drv_taskq", sd_label); 11462 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11463 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11464 TASKQ_PREPOPULATE)); 11465 11466 (void) snprintf(taskq_name, sizeof (taskq_name), 11467 "%s_rmw_taskq", sd_label); 11468 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11469 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11470 TASKQ_PREPOPULATE)); 11471 } 11472 11473 11474 /* 11475 * Function: sd_taskq_delete 11476 * 11477 * Description: Complementary cleanup routine for sd_taskq_create(). 11478 * 11479 * Context: Kernel thread context. 11480 */ 11481 11482 static void 11483 sd_taskq_delete(void) 11484 { 11485 ASSERT(sd_tq != NULL); 11486 ASSERT(sd_wmr_tq != NULL); 11487 taskq_destroy(sd_tq); 11488 taskq_destroy(sd_wmr_tq); 11489 sd_tq = NULL; 11490 sd_wmr_tq = NULL; 11491 } 11492 11493 11494 /* 11495 * Function: sdstrategy 11496 * 11497 * Description: Driver's strategy (9E) entry point function. 11498 * 11499 * Arguments: bp - pointer to buf(9S) 11500 * 11501 * Return Code: Always returns zero 11502 * 11503 * Context: Kernel thread context. 11504 */ 11505 11506 static int 11507 sdstrategy(struct buf *bp) 11508 { 11509 struct sd_lun *un; 11510 11511 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11512 if (un == NULL) { 11513 bioerror(bp, EIO); 11514 bp->b_resid = bp->b_bcount; 11515 biodone(bp); 11516 return (0); 11517 } 11518 /* As was done in the past, fail new cmds. if state is dumping. */ 11519 if (un->un_state == SD_STATE_DUMPING) { 11520 bioerror(bp, ENXIO); 11521 bp->b_resid = bp->b_bcount; 11522 biodone(bp); 11523 return (0); 11524 } 11525 11526 ASSERT(!mutex_owned(SD_MUTEX(un))); 11527 11528 /* 11529 * Commands may sneak in while we released the mutex in 11530 * DDI_SUSPEND, we should block new commands. However, old 11531 * commands that are still in the driver at this point should 11532 * still be allowed to drain. 11533 */ 11534 mutex_enter(SD_MUTEX(un)); 11535 /* 11536 * Must wait here if either the device is suspended or 11537 * if it's power level is changing. 11538 */ 11539 while ((un->un_state == SD_STATE_SUSPENDED) || 11540 (un->un_state == SD_STATE_PM_CHANGING)) { 11541 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11542 } 11543 11544 un->un_ncmds_in_driver++; 11545 11546 /* 11547 * atapi: Since we are running the CD for now in PIO mode we need to 11548 * call bp_mapin here to avoid bp_mapin called interrupt context under 11549 * the HBA's init_pkt routine. 11550 */ 11551 if (un->un_f_cfg_is_atapi == TRUE) { 11552 mutex_exit(SD_MUTEX(un)); 11553 bp_mapin(bp); 11554 mutex_enter(SD_MUTEX(un)); 11555 } 11556 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11557 un->un_ncmds_in_driver); 11558 11559 mutex_exit(SD_MUTEX(un)); 11560 11561 /* 11562 * This will (eventually) allocate the sd_xbuf area and 11563 * call sd_xbuf_strategy(). We just want to return the 11564 * result of ddi_xbuf_qstrategy so that we have an opt- 11565 * imized tail call which saves us a stack frame. 11566 */ 11567 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11568 } 11569 11570 11571 /* 11572 * Function: sd_xbuf_strategy 11573 * 11574 * Description: Function for initiating IO operations via the 11575 * ddi_xbuf_qstrategy() mechanism. 11576 * 11577 * Context: Kernel thread context. 11578 */ 11579 11580 static void 11581 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11582 { 11583 struct sd_lun *un = arg; 11584 11585 ASSERT(bp != NULL); 11586 ASSERT(xp != NULL); 11587 ASSERT(un != NULL); 11588 ASSERT(!mutex_owned(SD_MUTEX(un))); 11589 11590 /* 11591 * Initialize the fields in the xbuf and save a pointer to the 11592 * xbuf in bp->b_private. 11593 */ 11594 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11595 11596 /* Send the buf down the iostart chain */ 11597 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11598 } 11599 11600 11601 /* 11602 * Function: sd_xbuf_init 11603 * 11604 * Description: Prepare the given sd_xbuf struct for use. 11605 * 11606 * Arguments: un - ptr to softstate 11607 * bp - ptr to associated buf(9S) 11608 * xp - ptr to associated sd_xbuf 11609 * chain_type - IO chain type to use: 11610 * SD_CHAIN_NULL 11611 * SD_CHAIN_BUFIO 11612 * SD_CHAIN_USCSI 11613 * SD_CHAIN_DIRECT 11614 * SD_CHAIN_DIRECT_PRIORITY 11615 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11616 * initialization; may be NULL if none. 11617 * 11618 * Context: Kernel thread context 11619 */ 11620 11621 static void 11622 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11623 uchar_t chain_type, void *pktinfop) 11624 { 11625 int index; 11626 11627 ASSERT(un != NULL); 11628 ASSERT(bp != NULL); 11629 ASSERT(xp != NULL); 11630 11631 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11632 bp, chain_type); 11633 11634 xp->xb_un = un; 11635 xp->xb_pktp = NULL; 11636 xp->xb_pktinfo = pktinfop; 11637 xp->xb_private = bp->b_private; 11638 xp->xb_blkno = (daddr_t)bp->b_blkno; 11639 11640 /* 11641 * Set up the iostart and iodone chain indexes in the xbuf, based 11642 * upon the specified chain type to use. 11643 */ 11644 switch (chain_type) { 11645 case SD_CHAIN_NULL: 11646 /* 11647 * Fall thru to just use the values for the buf type, even 11648 * tho for the NULL chain these values will never be used. 11649 */ 11650 /* FALLTHRU */ 11651 case SD_CHAIN_BUFIO: 11652 index = un->un_buf_chain_type; 11653 break; 11654 case SD_CHAIN_USCSI: 11655 index = un->un_uscsi_chain_type; 11656 break; 11657 case SD_CHAIN_DIRECT: 11658 index = un->un_direct_chain_type; 11659 break; 11660 case SD_CHAIN_DIRECT_PRIORITY: 11661 index = un->un_priority_chain_type; 11662 break; 11663 default: 11664 /* We're really broken if we ever get here... */ 11665 panic("sd_xbuf_init: illegal chain type!"); 11666 /*NOTREACHED*/ 11667 } 11668 11669 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11670 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11671 11672 /* 11673 * It might be a bit easier to simply bzero the entire xbuf above, 11674 * but it turns out that since we init a fair number of members anyway, 11675 * we save a fair number cycles by doing explicit assignment of zero. 11676 */ 11677 xp->xb_pkt_flags = 0; 11678 xp->xb_dma_resid = 0; 11679 xp->xb_retry_count = 0; 11680 xp->xb_victim_retry_count = 0; 11681 xp->xb_ua_retry_count = 0; 11682 xp->xb_sense_bp = NULL; 11683 xp->xb_sense_status = 0; 11684 xp->xb_sense_state = 0; 11685 xp->xb_sense_resid = 0; 11686 11687 bp->b_private = xp; 11688 bp->b_flags &= ~(B_DONE | B_ERROR); 11689 bp->b_resid = 0; 11690 bp->av_forw = NULL; 11691 bp->av_back = NULL; 11692 bioerror(bp, 0); 11693 11694 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11695 } 11696 11697 11698 /* 11699 * Function: sd_uscsi_strategy 11700 * 11701 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11702 * 11703 * Arguments: bp - buf struct ptr 11704 * 11705 * Return Code: Always returns 0 11706 * 11707 * Context: Kernel thread context 11708 */ 11709 11710 static int 11711 sd_uscsi_strategy(struct buf *bp) 11712 { 11713 struct sd_lun *un; 11714 struct sd_uscsi_info *uip; 11715 struct sd_xbuf *xp; 11716 uchar_t chain_type; 11717 11718 ASSERT(bp != NULL); 11719 11720 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11721 if (un == NULL) { 11722 bioerror(bp, EIO); 11723 bp->b_resid = bp->b_bcount; 11724 biodone(bp); 11725 return (0); 11726 } 11727 11728 ASSERT(!mutex_owned(SD_MUTEX(un))); 11729 11730 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11731 11732 mutex_enter(SD_MUTEX(un)); 11733 /* 11734 * atapi: Since we are running the CD for now in PIO mode we need to 11735 * call bp_mapin here to avoid bp_mapin called interrupt context under 11736 * the HBA's init_pkt routine. 11737 */ 11738 if (un->un_f_cfg_is_atapi == TRUE) { 11739 mutex_exit(SD_MUTEX(un)); 11740 bp_mapin(bp); 11741 mutex_enter(SD_MUTEX(un)); 11742 } 11743 un->un_ncmds_in_driver++; 11744 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11745 un->un_ncmds_in_driver); 11746 mutex_exit(SD_MUTEX(un)); 11747 11748 /* 11749 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11750 */ 11751 ASSERT(bp->b_private != NULL); 11752 uip = (struct sd_uscsi_info *)bp->b_private; 11753 11754 switch (uip->ui_flags) { 11755 case SD_PATH_DIRECT: 11756 chain_type = SD_CHAIN_DIRECT; 11757 break; 11758 case SD_PATH_DIRECT_PRIORITY: 11759 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11760 break; 11761 default: 11762 chain_type = SD_CHAIN_USCSI; 11763 break; 11764 } 11765 11766 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11767 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11768 11769 /* Use the index obtained within xbuf_init */ 11770 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11771 11772 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11773 11774 return (0); 11775 } 11776 11777 11778 /* 11779 * These routines perform raw i/o operations. 11780 */ 11781 /*ARGSUSED*/ 11782 static void 11783 sduscsimin(struct buf *bp) 11784 { 11785 /* 11786 * do not break up because the CDB count would then 11787 * be incorrect and data underruns would result (incomplete 11788 * read/writes which would be retried and then failed, see 11789 * sdintr(). 11790 */ 11791 } 11792 11793 11794 11795 /* 11796 * Function: sd_send_scsi_cmd 11797 * 11798 * Description: Runs a USCSI command for user (when called thru sdioctl), 11799 * or for the driver 11800 * 11801 * Arguments: dev - the dev_t for the device 11802 * incmd - ptr to a valid uscsi_cmd struct 11803 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11804 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11805 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11806 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11807 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11808 * to use the USCSI "direct" chain and bypass the normal 11809 * command waitq. 11810 * 11811 * Return Code: 0 - successful completion of the given command 11812 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11813 * ENXIO - soft state not found for specified dev 11814 * EINVAL 11815 * EFAULT - copyin/copyout error 11816 * return code of biowait(9F) or physio(9F): 11817 * EIO - IO error, caller may check incmd->uscsi_status 11818 * ENXIO 11819 * EACCES - reservation conflict 11820 * 11821 * Context: Waits for command to complete. Can sleep. 11822 */ 11823 11824 static int 11825 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11826 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11827 int path_flag) 11828 { 11829 struct sd_uscsi_info *uip; 11830 struct uscsi_cmd *uscmd; 11831 struct sd_lun *un; 11832 struct buf *bp; 11833 int rval; 11834 int flags; 11835 11836 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11837 if (un == NULL) { 11838 return (ENXIO); 11839 } 11840 11841 ASSERT(!mutex_owned(SD_MUTEX(un))); 11842 11843 #ifdef SDDEBUG 11844 switch (dataspace) { 11845 case UIO_USERSPACE: 11846 SD_TRACE(SD_LOG_IO, un, 11847 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11848 break; 11849 case UIO_SYSSPACE: 11850 SD_TRACE(SD_LOG_IO, un, 11851 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11852 break; 11853 default: 11854 SD_TRACE(SD_LOG_IO, un, 11855 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11856 break; 11857 } 11858 #endif 11859 11860 /* 11861 * Perform resets directly; no need to generate a command to do it. 11862 */ 11863 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11864 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11865 RESET_ALL : RESET_TARGET; 11866 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11867 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11868 /* Reset attempt was unsuccessful */ 11869 SD_TRACE(SD_LOG_IO, un, 11870 "sd_send_scsi_cmd: reset: failure\n"); 11871 return (EIO); 11872 } 11873 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11874 return (0); 11875 } 11876 11877 /* Perfunctory sanity check... */ 11878 if (incmd->uscsi_cdblen <= 0) { 11879 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11880 "invalid uscsi_cdblen, returning EINVAL\n"); 11881 return (EINVAL); 11882 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 11883 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11884 "unsupported uscsi_cdblen, returning EINVAL\n"); 11885 return (EINVAL); 11886 } 11887 11888 /* 11889 * In order to not worry about where the uscsi structure came from 11890 * (or where the cdb it points to came from) we're going to make 11891 * kmem_alloc'd copies of them here. This will also allow reference 11892 * to the data they contain long after this process has gone to 11893 * sleep and its kernel stack has been unmapped, etc. 11894 * 11895 * First get some memory for the uscsi_cmd struct and copy the 11896 * contents of the given uscsi_cmd struct into it. 11897 */ 11898 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11899 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11900 11901 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11902 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11903 11904 /* 11905 * Now get some space for the CDB, and copy the given CDB into 11906 * it. Use ddi_copyin() in case the data is in user space. 11907 */ 11908 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11909 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11910 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11911 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11912 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11913 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11914 return (EFAULT); 11915 } 11916 11917 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11918 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11919 11920 bp = getrbuf(KM_SLEEP); 11921 11922 /* 11923 * Allocate an sd_uscsi_info struct and fill it with the info 11924 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11925 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11926 * since we allocate the buf here in this function, we do not 11927 * need to preserve the prior contents of b_private. 11928 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11929 */ 11930 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11931 uip->ui_flags = path_flag; 11932 uip->ui_cmdp = uscmd; 11933 bp->b_private = uip; 11934 11935 /* 11936 * Initialize Request Sense buffering, if requested. 11937 */ 11938 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11939 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11940 /* 11941 * Here uscmd->uscsi_rqbuf currently points to the caller's 11942 * buffer, but we replace this with a kernel buffer that 11943 * we allocate to use with the sense data. The sense data 11944 * (if present) gets copied into this new buffer before the 11945 * command is completed. Then we copy the sense data from 11946 * our allocated buf into the caller's buffer below. Note 11947 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11948 * below to perform the copy back to the caller's buf. 11949 */ 11950 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11951 if (rqbufspace == UIO_USERSPACE) { 11952 uscmd->uscsi_rqlen = SENSE_LENGTH; 11953 uscmd->uscsi_rqresid = SENSE_LENGTH; 11954 } else { 11955 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11956 uscmd->uscsi_rqlen = rlen; 11957 uscmd->uscsi_rqresid = rlen; 11958 } 11959 } else { 11960 uscmd->uscsi_rqbuf = NULL; 11961 uscmd->uscsi_rqlen = 0; 11962 uscmd->uscsi_rqresid = 0; 11963 } 11964 11965 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11966 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11967 11968 if (un->un_f_is_fibre == FALSE) { 11969 /* 11970 * Force asynchronous mode, if necessary. Doing this here 11971 * has the unfortunate effect of running other queued 11972 * commands async also, but since the main purpose of this 11973 * capability is downloading new drive firmware, we can 11974 * probably live with it. 11975 */ 11976 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11977 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11978 == 1) { 11979 if (scsi_ifsetcap(SD_ADDRESS(un), 11980 "synchronous", 0, 1) == 1) { 11981 SD_TRACE(SD_LOG_IO, un, 11982 "sd_send_scsi_cmd: forced async ok\n"); 11983 } else { 11984 SD_TRACE(SD_LOG_IO, un, 11985 "sd_send_scsi_cmd:\ 11986 forced async failed\n"); 11987 rval = EINVAL; 11988 goto done; 11989 } 11990 } 11991 } 11992 11993 /* 11994 * Re-enable synchronous mode, if requested 11995 */ 11996 if (uscmd->uscsi_flags & USCSI_SYNC) { 11997 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11998 == 0) { 11999 int i = scsi_ifsetcap(SD_ADDRESS(un), 12000 "synchronous", 1, 1); 12001 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12002 "re-enabled sync %s\n", 12003 (i == 1) ? "ok" : "failed"); 12004 } 12005 } 12006 } 12007 12008 /* 12009 * Commands sent with priority are intended for error recovery 12010 * situations, and do not have retries performed. 12011 */ 12012 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12013 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12014 } 12015 12016 /* 12017 * If we're going to do actual I/O, let physio do all the right things 12018 */ 12019 if (uscmd->uscsi_buflen != 0) { 12020 struct iovec aiov; 12021 struct uio auio; 12022 struct uio *uio = &auio; 12023 12024 bzero(&auio, sizeof (struct uio)); 12025 bzero(&aiov, sizeof (struct iovec)); 12026 aiov.iov_base = uscmd->uscsi_bufaddr; 12027 aiov.iov_len = uscmd->uscsi_buflen; 12028 uio->uio_iov = &aiov; 12029 12030 uio->uio_iovcnt = 1; 12031 uio->uio_resid = uscmd->uscsi_buflen; 12032 uio->uio_segflg = dataspace; 12033 12034 /* 12035 * physio() will block here until the command completes.... 12036 */ 12037 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 12038 12039 rval = physio(sd_uscsi_strategy, bp, dev, 12040 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 12041 sduscsimin, uio); 12042 12043 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12044 "returned from physio with 0x%x\n", rval); 12045 12046 } else { 12047 /* 12048 * We have to mimic what physio would do here! Argh! 12049 */ 12050 bp->b_flags = B_BUSY | 12051 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 12052 bp->b_edev = dev; 12053 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 12054 bp->b_bcount = 0; 12055 bp->b_blkno = 0; 12056 12057 SD_TRACE(SD_LOG_IO, un, 12058 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 12059 12060 (void) sd_uscsi_strategy(bp); 12061 12062 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 12063 12064 rval = biowait(bp); 12065 12066 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12067 "returned from biowait with 0x%x\n", rval); 12068 } 12069 12070 done: 12071 12072 #ifdef SDDEBUG 12073 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12074 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12075 uscmd->uscsi_status, uscmd->uscsi_resid); 12076 if (uscmd->uscsi_bufaddr != NULL) { 12077 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12078 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12079 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12080 if (dataspace == UIO_SYSSPACE) { 12081 SD_DUMP_MEMORY(un, SD_LOG_IO, 12082 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12083 uscmd->uscsi_buflen, SD_LOG_HEX); 12084 } 12085 } 12086 #endif 12087 12088 /* 12089 * Get the status and residual to return to the caller. 12090 */ 12091 incmd->uscsi_status = uscmd->uscsi_status; 12092 incmd->uscsi_resid = uscmd->uscsi_resid; 12093 12094 /* 12095 * If the caller wants sense data, copy back whatever sense data 12096 * we may have gotten, and update the relevant rqsense info. 12097 */ 12098 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12099 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12100 12101 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 12102 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 12103 12104 /* Update the Request Sense status and resid */ 12105 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 12106 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 12107 12108 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12109 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 12110 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 12111 12112 /* Copy out the sense data for user processes */ 12113 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 12114 int flags = 12115 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 12116 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 12117 rqlen, flags) != 0) { 12118 rval = EFAULT; 12119 } 12120 /* 12121 * Note: Can't touch incmd->uscsi_rqbuf so use 12122 * uscmd->uscsi_rqbuf instead. They're the same. 12123 */ 12124 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12125 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 12126 incmd->uscsi_rqbuf, rqlen); 12127 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 12128 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 12129 } 12130 } 12131 12132 /* 12133 * Free allocated resources and return; mapout the buf in case it was 12134 * mapped in by a lower layer. 12135 */ 12136 bp_mapout(bp); 12137 freerbuf(bp); 12138 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12139 if (uscmd->uscsi_rqbuf != NULL) { 12140 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 12141 } 12142 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 12143 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12144 12145 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 12146 12147 return (rval); 12148 } 12149 12150 12151 /* 12152 * Function: sd_buf_iodone 12153 * 12154 * Description: Frees the sd_xbuf & returns the buf to its originator. 12155 * 12156 * Context: May be called from interrupt context. 12157 */ 12158 /* ARGSUSED */ 12159 static void 12160 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12161 { 12162 struct sd_xbuf *xp; 12163 12164 ASSERT(un != NULL); 12165 ASSERT(bp != NULL); 12166 ASSERT(!mutex_owned(SD_MUTEX(un))); 12167 12168 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12169 12170 xp = SD_GET_XBUF(bp); 12171 ASSERT(xp != NULL); 12172 12173 mutex_enter(SD_MUTEX(un)); 12174 12175 /* 12176 * Grab time when the cmd completed. 12177 * This is used for determining if the system has been 12178 * idle long enough to make it idle to the PM framework. 12179 * This is for lowering the overhead, and therefore improving 12180 * performance per I/O operation. 12181 */ 12182 un->un_pm_idle_time = ddi_get_time(); 12183 12184 un->un_ncmds_in_driver--; 12185 ASSERT(un->un_ncmds_in_driver >= 0); 12186 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12187 un->un_ncmds_in_driver); 12188 12189 mutex_exit(SD_MUTEX(un)); 12190 12191 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12192 biodone(bp); /* bp is gone after this */ 12193 12194 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12195 } 12196 12197 12198 /* 12199 * Function: sd_uscsi_iodone 12200 * 12201 * Description: Frees the sd_xbuf & returns the buf to its originator. 12202 * 12203 * Context: May be called from interrupt context. 12204 */ 12205 /* ARGSUSED */ 12206 static void 12207 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12208 { 12209 struct sd_xbuf *xp; 12210 12211 ASSERT(un != NULL); 12212 ASSERT(bp != NULL); 12213 12214 xp = SD_GET_XBUF(bp); 12215 ASSERT(xp != NULL); 12216 ASSERT(!mutex_owned(SD_MUTEX(un))); 12217 12218 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12219 12220 bp->b_private = xp->xb_private; 12221 12222 mutex_enter(SD_MUTEX(un)); 12223 12224 /* 12225 * Grab time when the cmd completed. 12226 * This is used for determining if the system has been 12227 * idle long enough to make it idle to the PM framework. 12228 * This is for lowering the overhead, and therefore improving 12229 * performance per I/O operation. 12230 */ 12231 un->un_pm_idle_time = ddi_get_time(); 12232 12233 un->un_ncmds_in_driver--; 12234 ASSERT(un->un_ncmds_in_driver >= 0); 12235 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12236 un->un_ncmds_in_driver); 12237 12238 mutex_exit(SD_MUTEX(un)); 12239 12240 kmem_free(xp, sizeof (struct sd_xbuf)); 12241 biodone(bp); 12242 12243 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12244 } 12245 12246 12247 /* 12248 * Function: sd_mapblockaddr_iostart 12249 * 12250 * Description: Verify request lies withing the partition limits for 12251 * the indicated minor device. Issue "overrun" buf if 12252 * request would exceed partition range. Converts 12253 * partition-relative block address to absolute. 12254 * 12255 * Context: Can sleep 12256 * 12257 * Issues: This follows what the old code did, in terms of accessing 12258 * some of the partition info in the unit struct without holding 12259 * the mutext. This is a general issue, if the partition info 12260 * can be altered while IO is in progress... as soon as we send 12261 * a buf, its partitioning can be invalid before it gets to the 12262 * device. Probably the right fix is to move partitioning out 12263 * of the driver entirely. 12264 */ 12265 12266 static void 12267 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12268 { 12269 daddr_t nblocks; /* #blocks in the given partition */ 12270 daddr_t blocknum; /* Block number specified by the buf */ 12271 size_t requested_nblocks; 12272 size_t available_nblocks; 12273 int partition; 12274 diskaddr_t partition_offset; 12275 struct sd_xbuf *xp; 12276 12277 12278 ASSERT(un != NULL); 12279 ASSERT(bp != NULL); 12280 ASSERT(!mutex_owned(SD_MUTEX(un))); 12281 12282 SD_TRACE(SD_LOG_IO_PARTITION, un, 12283 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12284 12285 xp = SD_GET_XBUF(bp); 12286 ASSERT(xp != NULL); 12287 12288 /* 12289 * If the geometry is not indicated as valid, attempt to access 12290 * the unit & verify the geometry/label. This can be the case for 12291 * removable-media devices, of if the device was opened in 12292 * NDELAY/NONBLOCK mode. 12293 */ 12294 if ((un->un_f_geometry_is_valid != TRUE) && 12295 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12296 /* 12297 * For removable devices it is possible to start an I/O 12298 * without a media by opening the device in nodelay mode. 12299 * Also for writable CDs there can be many scenarios where 12300 * there is no geometry yet but volume manager is trying to 12301 * issue a read() just because it can see TOC on the CD. So 12302 * do not print a message for removables. 12303 */ 12304 if (!un->un_f_has_removable_media) { 12305 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12306 "i/o to invalid geometry\n"); 12307 } 12308 bioerror(bp, EIO); 12309 bp->b_resid = bp->b_bcount; 12310 SD_BEGIN_IODONE(index, un, bp); 12311 return; 12312 } 12313 12314 partition = SDPART(bp->b_edev); 12315 12316 /* #blocks in partition */ 12317 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12318 12319 /* Use of a local variable potentially improves performance slightly */ 12320 partition_offset = un->un_offset[partition]; 12321 12322 /* 12323 * blocknum is the starting block number of the request. At this 12324 * point it is still relative to the start of the minor device. 12325 */ 12326 blocknum = xp->xb_blkno; 12327 12328 /* 12329 * Legacy: If the starting block number is one past the last block 12330 * in the partition, do not set B_ERROR in the buf. 12331 */ 12332 if (blocknum == nblocks) { 12333 goto error_exit; 12334 } 12335 12336 /* 12337 * Confirm that the first block of the request lies within the 12338 * partition limits. Also the requested number of bytes must be 12339 * a multiple of the system block size. 12340 */ 12341 if ((blocknum < 0) || (blocknum >= nblocks) || 12342 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12343 bp->b_flags |= B_ERROR; 12344 goto error_exit; 12345 } 12346 12347 /* 12348 * If the requsted # blocks exceeds the available # blocks, that 12349 * is an overrun of the partition. 12350 */ 12351 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12352 available_nblocks = (size_t)(nblocks - blocknum); 12353 ASSERT(nblocks >= blocknum); 12354 12355 if (requested_nblocks > available_nblocks) { 12356 /* 12357 * Allocate an "overrun" buf to allow the request to proceed 12358 * for the amount of space available in the partition. The 12359 * amount not transferred will be added into the b_resid 12360 * when the operation is complete. The overrun buf 12361 * replaces the original buf here, and the original buf 12362 * is saved inside the overrun buf, for later use. 12363 */ 12364 size_t resid = SD_SYSBLOCKS2BYTES(un, 12365 (offset_t)(requested_nblocks - available_nblocks)); 12366 size_t count = bp->b_bcount - resid; 12367 /* 12368 * Note: count is an unsigned entity thus it'll NEVER 12369 * be less than 0 so ASSERT the original values are 12370 * correct. 12371 */ 12372 ASSERT(bp->b_bcount >= resid); 12373 12374 bp = sd_bioclone_alloc(bp, count, blocknum, 12375 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12376 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12377 ASSERT(xp != NULL); 12378 } 12379 12380 /* At this point there should be no residual for this buf. */ 12381 ASSERT(bp->b_resid == 0); 12382 12383 /* Convert the block number to an absolute address. */ 12384 xp->xb_blkno += partition_offset; 12385 12386 SD_NEXT_IOSTART(index, un, bp); 12387 12388 SD_TRACE(SD_LOG_IO_PARTITION, un, 12389 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12390 12391 return; 12392 12393 error_exit: 12394 bp->b_resid = bp->b_bcount; 12395 SD_BEGIN_IODONE(index, un, bp); 12396 SD_TRACE(SD_LOG_IO_PARTITION, un, 12397 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12398 } 12399 12400 12401 /* 12402 * Function: sd_mapblockaddr_iodone 12403 * 12404 * Description: Completion-side processing for partition management. 12405 * 12406 * Context: May be called under interrupt context 12407 */ 12408 12409 static void 12410 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12411 { 12412 /* int partition; */ /* Not used, see below. */ 12413 ASSERT(un != NULL); 12414 ASSERT(bp != NULL); 12415 ASSERT(!mutex_owned(SD_MUTEX(un))); 12416 12417 SD_TRACE(SD_LOG_IO_PARTITION, un, 12418 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12419 12420 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12421 /* 12422 * We have an "overrun" buf to deal with... 12423 */ 12424 struct sd_xbuf *xp; 12425 struct buf *obp; /* ptr to the original buf */ 12426 12427 xp = SD_GET_XBUF(bp); 12428 ASSERT(xp != NULL); 12429 12430 /* Retrieve the pointer to the original buf */ 12431 obp = (struct buf *)xp->xb_private; 12432 ASSERT(obp != NULL); 12433 12434 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12435 bioerror(obp, bp->b_error); 12436 12437 sd_bioclone_free(bp); 12438 12439 /* 12440 * Get back the original buf. 12441 * Note that since the restoration of xb_blkno below 12442 * was removed, the sd_xbuf is not needed. 12443 */ 12444 bp = obp; 12445 /* 12446 * xp = SD_GET_XBUF(bp); 12447 * ASSERT(xp != NULL); 12448 */ 12449 } 12450 12451 /* 12452 * Convert sd->xb_blkno back to a minor-device relative value. 12453 * Note: this has been commented out, as it is not needed in the 12454 * current implementation of the driver (ie, since this function 12455 * is at the top of the layering chains, so the info will be 12456 * discarded) and it is in the "hot" IO path. 12457 * 12458 * partition = getminor(bp->b_edev) & SDPART_MASK; 12459 * xp->xb_blkno -= un->un_offset[partition]; 12460 */ 12461 12462 SD_NEXT_IODONE(index, un, bp); 12463 12464 SD_TRACE(SD_LOG_IO_PARTITION, un, 12465 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12466 } 12467 12468 12469 /* 12470 * Function: sd_mapblocksize_iostart 12471 * 12472 * Description: Convert between system block size (un->un_sys_blocksize) 12473 * and target block size (un->un_tgt_blocksize). 12474 * 12475 * Context: Can sleep to allocate resources. 12476 * 12477 * Assumptions: A higher layer has already performed any partition validation, 12478 * and converted the xp->xb_blkno to an absolute value relative 12479 * to the start of the device. 12480 * 12481 * It is also assumed that the higher layer has implemented 12482 * an "overrun" mechanism for the case where the request would 12483 * read/write beyond the end of a partition. In this case we 12484 * assume (and ASSERT) that bp->b_resid == 0. 12485 * 12486 * Note: The implementation for this routine assumes the target 12487 * block size remains constant between allocation and transport. 12488 */ 12489 12490 static void 12491 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12492 { 12493 struct sd_mapblocksize_info *bsp; 12494 struct sd_xbuf *xp; 12495 offset_t first_byte; 12496 daddr_t start_block, end_block; 12497 daddr_t request_bytes; 12498 ushort_t is_aligned = FALSE; 12499 12500 ASSERT(un != NULL); 12501 ASSERT(bp != NULL); 12502 ASSERT(!mutex_owned(SD_MUTEX(un))); 12503 ASSERT(bp->b_resid == 0); 12504 12505 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12506 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12507 12508 /* 12509 * For a non-writable CD, a write request is an error 12510 */ 12511 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12512 (un->un_f_mmc_writable_media == FALSE)) { 12513 bioerror(bp, EIO); 12514 bp->b_resid = bp->b_bcount; 12515 SD_BEGIN_IODONE(index, un, bp); 12516 return; 12517 } 12518 12519 /* 12520 * We do not need a shadow buf if the device is using 12521 * un->un_sys_blocksize as its block size or if bcount == 0. 12522 * In this case there is no layer-private data block allocated. 12523 */ 12524 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12525 (bp->b_bcount == 0)) { 12526 goto done; 12527 } 12528 12529 #if defined(__i386) || defined(__amd64) 12530 /* We do not support non-block-aligned transfers for ROD devices */ 12531 ASSERT(!ISROD(un)); 12532 #endif 12533 12534 xp = SD_GET_XBUF(bp); 12535 ASSERT(xp != NULL); 12536 12537 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12538 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12539 un->un_tgt_blocksize, un->un_sys_blocksize); 12540 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12541 "request start block:0x%x\n", xp->xb_blkno); 12542 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12543 "request len:0x%x\n", bp->b_bcount); 12544 12545 /* 12546 * Allocate the layer-private data area for the mapblocksize layer. 12547 * Layers are allowed to use the xp_private member of the sd_xbuf 12548 * struct to store the pointer to their layer-private data block, but 12549 * each layer also has the responsibility of restoring the prior 12550 * contents of xb_private before returning the buf/xbuf to the 12551 * higher layer that sent it. 12552 * 12553 * Here we save the prior contents of xp->xb_private into the 12554 * bsp->mbs_oprivate field of our layer-private data area. This value 12555 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12556 * the layer-private area and returning the buf/xbuf to the layer 12557 * that sent it. 12558 * 12559 * Note that here we use kmem_zalloc for the allocation as there are 12560 * parts of the mapblocksize code that expect certain fields to be 12561 * zero unless explicitly set to a required value. 12562 */ 12563 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12564 bsp->mbs_oprivate = xp->xb_private; 12565 xp->xb_private = bsp; 12566 12567 /* 12568 * This treats the data on the disk (target) as an array of bytes. 12569 * first_byte is the byte offset, from the beginning of the device, 12570 * to the location of the request. This is converted from a 12571 * un->un_sys_blocksize block address to a byte offset, and then back 12572 * to a block address based upon a un->un_tgt_blocksize block size. 12573 * 12574 * xp->xb_blkno should be absolute upon entry into this function, 12575 * but, but it is based upon partitions that use the "system" 12576 * block size. It must be adjusted to reflect the block size of 12577 * the target. 12578 * 12579 * Note that end_block is actually the block that follows the last 12580 * block of the request, but that's what is needed for the computation. 12581 */ 12582 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12583 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12584 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12585 un->un_tgt_blocksize; 12586 12587 /* request_bytes is rounded up to a multiple of the target block size */ 12588 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12589 12590 /* 12591 * See if the starting address of the request and the request 12592 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12593 * then we do not need to allocate a shadow buf to handle the request. 12594 */ 12595 if (((first_byte % un->un_tgt_blocksize) == 0) && 12596 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12597 is_aligned = TRUE; 12598 } 12599 12600 if ((bp->b_flags & B_READ) == 0) { 12601 /* 12602 * Lock the range for a write operation. An aligned request is 12603 * considered a simple write; otherwise the request must be a 12604 * read-modify-write. 12605 */ 12606 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12607 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12608 } 12609 12610 /* 12611 * Alloc a shadow buf if the request is not aligned. Also, this is 12612 * where the READ command is generated for a read-modify-write. (The 12613 * write phase is deferred until after the read completes.) 12614 */ 12615 if (is_aligned == FALSE) { 12616 12617 struct sd_mapblocksize_info *shadow_bsp; 12618 struct sd_xbuf *shadow_xp; 12619 struct buf *shadow_bp; 12620 12621 /* 12622 * Allocate the shadow buf and it associated xbuf. Note that 12623 * after this call the xb_blkno value in both the original 12624 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12625 * same: absolute relative to the start of the device, and 12626 * adjusted for the target block size. The b_blkno in the 12627 * shadow buf will also be set to this value. We should never 12628 * change b_blkno in the original bp however. 12629 * 12630 * Note also that the shadow buf will always need to be a 12631 * READ command, regardless of whether the incoming command 12632 * is a READ or a WRITE. 12633 */ 12634 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12635 xp->xb_blkno, 12636 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12637 12638 shadow_xp = SD_GET_XBUF(shadow_bp); 12639 12640 /* 12641 * Allocate the layer-private data for the shadow buf. 12642 * (No need to preserve xb_private in the shadow xbuf.) 12643 */ 12644 shadow_xp->xb_private = shadow_bsp = 12645 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12646 12647 /* 12648 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12649 * to figure out where the start of the user data is (based upon 12650 * the system block size) in the data returned by the READ 12651 * command (which will be based upon the target blocksize). Note 12652 * that this is only really used if the request is unaligned. 12653 */ 12654 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12655 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12656 ASSERT((bsp->mbs_copy_offset >= 0) && 12657 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12658 12659 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12660 12661 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12662 12663 /* Transfer the wmap (if any) to the shadow buf */ 12664 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12665 bsp->mbs_wmp = NULL; 12666 12667 /* 12668 * The shadow buf goes on from here in place of the 12669 * original buf. 12670 */ 12671 shadow_bsp->mbs_orig_bp = bp; 12672 bp = shadow_bp; 12673 } 12674 12675 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12676 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12677 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12678 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12679 request_bytes); 12680 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12681 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12682 12683 done: 12684 SD_NEXT_IOSTART(index, un, bp); 12685 12686 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12687 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12688 } 12689 12690 12691 /* 12692 * Function: sd_mapblocksize_iodone 12693 * 12694 * Description: Completion side processing for block-size mapping. 12695 * 12696 * Context: May be called under interrupt context 12697 */ 12698 12699 static void 12700 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12701 { 12702 struct sd_mapblocksize_info *bsp; 12703 struct sd_xbuf *xp; 12704 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12705 struct buf *orig_bp; /* ptr to the original buf */ 12706 offset_t shadow_end; 12707 offset_t request_end; 12708 offset_t shadow_start; 12709 ssize_t copy_offset; 12710 size_t copy_length; 12711 size_t shortfall; 12712 uint_t is_write; /* TRUE if this bp is a WRITE */ 12713 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12714 12715 ASSERT(un != NULL); 12716 ASSERT(bp != NULL); 12717 12718 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12719 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12720 12721 /* 12722 * There is no shadow buf or layer-private data if the target is 12723 * using un->un_sys_blocksize as its block size or if bcount == 0. 12724 */ 12725 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12726 (bp->b_bcount == 0)) { 12727 goto exit; 12728 } 12729 12730 xp = SD_GET_XBUF(bp); 12731 ASSERT(xp != NULL); 12732 12733 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12734 bsp = xp->xb_private; 12735 12736 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12737 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12738 12739 if (is_write) { 12740 /* 12741 * For a WRITE request we must free up the block range that 12742 * we have locked up. This holds regardless of whether this is 12743 * an aligned write request or a read-modify-write request. 12744 */ 12745 sd_range_unlock(un, bsp->mbs_wmp); 12746 bsp->mbs_wmp = NULL; 12747 } 12748 12749 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12750 /* 12751 * An aligned read or write command will have no shadow buf; 12752 * there is not much else to do with it. 12753 */ 12754 goto done; 12755 } 12756 12757 orig_bp = bsp->mbs_orig_bp; 12758 ASSERT(orig_bp != NULL); 12759 orig_xp = SD_GET_XBUF(orig_bp); 12760 ASSERT(orig_xp != NULL); 12761 ASSERT(!mutex_owned(SD_MUTEX(un))); 12762 12763 if (!is_write && has_wmap) { 12764 /* 12765 * A READ with a wmap means this is the READ phase of a 12766 * read-modify-write. If an error occurred on the READ then 12767 * we do not proceed with the WRITE phase or copy any data. 12768 * Just release the write maps and return with an error. 12769 */ 12770 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12771 orig_bp->b_resid = orig_bp->b_bcount; 12772 bioerror(orig_bp, bp->b_error); 12773 sd_range_unlock(un, bsp->mbs_wmp); 12774 goto freebuf_done; 12775 } 12776 } 12777 12778 /* 12779 * Here is where we set up to copy the data from the shadow buf 12780 * into the space associated with the original buf. 12781 * 12782 * To deal with the conversion between block sizes, these 12783 * computations treat the data as an array of bytes, with the 12784 * first byte (byte 0) corresponding to the first byte in the 12785 * first block on the disk. 12786 */ 12787 12788 /* 12789 * shadow_start and shadow_len indicate the location and size of 12790 * the data returned with the shadow IO request. 12791 */ 12792 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12793 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12794 12795 /* 12796 * copy_offset gives the offset (in bytes) from the start of the first 12797 * block of the READ request to the beginning of the data. We retrieve 12798 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12799 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12800 * data to be copied (in bytes). 12801 */ 12802 copy_offset = bsp->mbs_copy_offset; 12803 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12804 copy_length = orig_bp->b_bcount; 12805 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12806 12807 /* 12808 * Set up the resid and error fields of orig_bp as appropriate. 12809 */ 12810 if (shadow_end >= request_end) { 12811 /* We got all the requested data; set resid to zero */ 12812 orig_bp->b_resid = 0; 12813 } else { 12814 /* 12815 * We failed to get enough data to fully satisfy the original 12816 * request. Just copy back whatever data we got and set 12817 * up the residual and error code as required. 12818 * 12819 * 'shortfall' is the amount by which the data received with the 12820 * shadow buf has "fallen short" of the requested amount. 12821 */ 12822 shortfall = (size_t)(request_end - shadow_end); 12823 12824 if (shortfall > orig_bp->b_bcount) { 12825 /* 12826 * We did not get enough data to even partially 12827 * fulfill the original request. The residual is 12828 * equal to the amount requested. 12829 */ 12830 orig_bp->b_resid = orig_bp->b_bcount; 12831 } else { 12832 /* 12833 * We did not get all the data that we requested 12834 * from the device, but we will try to return what 12835 * portion we did get. 12836 */ 12837 orig_bp->b_resid = shortfall; 12838 } 12839 ASSERT(copy_length >= orig_bp->b_resid); 12840 copy_length -= orig_bp->b_resid; 12841 } 12842 12843 /* Propagate the error code from the shadow buf to the original buf */ 12844 bioerror(orig_bp, bp->b_error); 12845 12846 if (is_write) { 12847 goto freebuf_done; /* No data copying for a WRITE */ 12848 } 12849 12850 if (has_wmap) { 12851 /* 12852 * This is a READ command from the READ phase of a 12853 * read-modify-write request. We have to copy the data given 12854 * by the user OVER the data returned by the READ command, 12855 * then convert the command from a READ to a WRITE and send 12856 * it back to the target. 12857 */ 12858 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12859 copy_length); 12860 12861 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12862 12863 /* 12864 * Dispatch the WRITE command to the taskq thread, which 12865 * will in turn send the command to the target. When the 12866 * WRITE command completes, we (sd_mapblocksize_iodone()) 12867 * will get called again as part of the iodone chain 12868 * processing for it. Note that we will still be dealing 12869 * with the shadow buf at that point. 12870 */ 12871 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12872 KM_NOSLEEP) != 0) { 12873 /* 12874 * Dispatch was successful so we are done. Return 12875 * without going any higher up the iodone chain. Do 12876 * not free up any layer-private data until after the 12877 * WRITE completes. 12878 */ 12879 return; 12880 } 12881 12882 /* 12883 * Dispatch of the WRITE command failed; set up the error 12884 * condition and send this IO back up the iodone chain. 12885 */ 12886 bioerror(orig_bp, EIO); 12887 orig_bp->b_resid = orig_bp->b_bcount; 12888 12889 } else { 12890 /* 12891 * This is a regular READ request (ie, not a RMW). Copy the 12892 * data from the shadow buf into the original buf. The 12893 * copy_offset compensates for any "misalignment" between the 12894 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12895 * original buf (with its un->un_sys_blocksize blocks). 12896 */ 12897 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12898 copy_length); 12899 } 12900 12901 freebuf_done: 12902 12903 /* 12904 * At this point we still have both the shadow buf AND the original 12905 * buf to deal with, as well as the layer-private data area in each. 12906 * Local variables are as follows: 12907 * 12908 * bp -- points to shadow buf 12909 * xp -- points to xbuf of shadow buf 12910 * bsp -- points to layer-private data area of shadow buf 12911 * orig_bp -- points to original buf 12912 * 12913 * First free the shadow buf and its associated xbuf, then free the 12914 * layer-private data area from the shadow buf. There is no need to 12915 * restore xb_private in the shadow xbuf. 12916 */ 12917 sd_shadow_buf_free(bp); 12918 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12919 12920 /* 12921 * Now update the local variables to point to the original buf, xbuf, 12922 * and layer-private area. 12923 */ 12924 bp = orig_bp; 12925 xp = SD_GET_XBUF(bp); 12926 ASSERT(xp != NULL); 12927 ASSERT(xp == orig_xp); 12928 bsp = xp->xb_private; 12929 ASSERT(bsp != NULL); 12930 12931 done: 12932 /* 12933 * Restore xb_private to whatever it was set to by the next higher 12934 * layer in the chain, then free the layer-private data area. 12935 */ 12936 xp->xb_private = bsp->mbs_oprivate; 12937 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12938 12939 exit: 12940 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12941 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12942 12943 SD_NEXT_IODONE(index, un, bp); 12944 } 12945 12946 12947 /* 12948 * Function: sd_checksum_iostart 12949 * 12950 * Description: A stub function for a layer that's currently not used. 12951 * For now just a placeholder. 12952 * 12953 * Context: Kernel thread context 12954 */ 12955 12956 static void 12957 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12958 { 12959 ASSERT(un != NULL); 12960 ASSERT(bp != NULL); 12961 ASSERT(!mutex_owned(SD_MUTEX(un))); 12962 SD_NEXT_IOSTART(index, un, bp); 12963 } 12964 12965 12966 /* 12967 * Function: sd_checksum_iodone 12968 * 12969 * Description: A stub function for a layer that's currently not used. 12970 * For now just a placeholder. 12971 * 12972 * Context: May be called under interrupt context 12973 */ 12974 12975 static void 12976 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12977 { 12978 ASSERT(un != NULL); 12979 ASSERT(bp != NULL); 12980 ASSERT(!mutex_owned(SD_MUTEX(un))); 12981 SD_NEXT_IODONE(index, un, bp); 12982 } 12983 12984 12985 /* 12986 * Function: sd_checksum_uscsi_iostart 12987 * 12988 * Description: A stub function for a layer that's currently not used. 12989 * For now just a placeholder. 12990 * 12991 * Context: Kernel thread context 12992 */ 12993 12994 static void 12995 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12996 { 12997 ASSERT(un != NULL); 12998 ASSERT(bp != NULL); 12999 ASSERT(!mutex_owned(SD_MUTEX(un))); 13000 SD_NEXT_IOSTART(index, un, bp); 13001 } 13002 13003 13004 /* 13005 * Function: sd_checksum_uscsi_iodone 13006 * 13007 * Description: A stub function for a layer that's currently not used. 13008 * For now just a placeholder. 13009 * 13010 * Context: May be called under interrupt context 13011 */ 13012 13013 static void 13014 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13015 { 13016 ASSERT(un != NULL); 13017 ASSERT(bp != NULL); 13018 ASSERT(!mutex_owned(SD_MUTEX(un))); 13019 SD_NEXT_IODONE(index, un, bp); 13020 } 13021 13022 13023 /* 13024 * Function: sd_pm_iostart 13025 * 13026 * Description: iostart-side routine for Power mangement. 13027 * 13028 * Context: Kernel thread context 13029 */ 13030 13031 static void 13032 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13033 { 13034 ASSERT(un != NULL); 13035 ASSERT(bp != NULL); 13036 ASSERT(!mutex_owned(SD_MUTEX(un))); 13037 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13038 13039 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13040 13041 if (sd_pm_entry(un) != DDI_SUCCESS) { 13042 /* 13043 * Set up to return the failed buf back up the 'iodone' 13044 * side of the calling chain. 13045 */ 13046 bioerror(bp, EIO); 13047 bp->b_resid = bp->b_bcount; 13048 13049 SD_BEGIN_IODONE(index, un, bp); 13050 13051 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13052 return; 13053 } 13054 13055 SD_NEXT_IOSTART(index, un, bp); 13056 13057 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13058 } 13059 13060 13061 /* 13062 * Function: sd_pm_iodone 13063 * 13064 * Description: iodone-side routine for power mangement. 13065 * 13066 * Context: may be called from interrupt context 13067 */ 13068 13069 static void 13070 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13071 { 13072 ASSERT(un != NULL); 13073 ASSERT(bp != NULL); 13074 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13075 13076 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13077 13078 /* 13079 * After attach the following flag is only read, so don't 13080 * take the penalty of acquiring a mutex for it. 13081 */ 13082 if (un->un_f_pm_is_enabled == TRUE) { 13083 sd_pm_exit(un); 13084 } 13085 13086 SD_NEXT_IODONE(index, un, bp); 13087 13088 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13089 } 13090 13091 13092 /* 13093 * Function: sd_core_iostart 13094 * 13095 * Description: Primary driver function for enqueuing buf(9S) structs from 13096 * the system and initiating IO to the target device 13097 * 13098 * Context: Kernel thread context. Can sleep. 13099 * 13100 * Assumptions: - The given xp->xb_blkno is absolute 13101 * (ie, relative to the start of the device). 13102 * - The IO is to be done using the native blocksize of 13103 * the device, as specified in un->un_tgt_blocksize. 13104 */ 13105 /* ARGSUSED */ 13106 static void 13107 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13108 { 13109 struct sd_xbuf *xp; 13110 13111 ASSERT(un != NULL); 13112 ASSERT(bp != NULL); 13113 ASSERT(!mutex_owned(SD_MUTEX(un))); 13114 ASSERT(bp->b_resid == 0); 13115 13116 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13117 13118 xp = SD_GET_XBUF(bp); 13119 ASSERT(xp != NULL); 13120 13121 mutex_enter(SD_MUTEX(un)); 13122 13123 /* 13124 * If we are currently in the failfast state, fail any new IO 13125 * that has B_FAILFAST set, then return. 13126 */ 13127 if ((bp->b_flags & B_FAILFAST) && 13128 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13129 mutex_exit(SD_MUTEX(un)); 13130 bioerror(bp, EIO); 13131 bp->b_resid = bp->b_bcount; 13132 SD_BEGIN_IODONE(index, un, bp); 13133 return; 13134 } 13135 13136 if (SD_IS_DIRECT_PRIORITY(xp)) { 13137 /* 13138 * Priority command -- transport it immediately. 13139 * 13140 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13141 * because all direct priority commands should be associated 13142 * with error recovery actions which we don't want to retry. 13143 */ 13144 sd_start_cmds(un, bp); 13145 } else { 13146 /* 13147 * Normal command -- add it to the wait queue, then start 13148 * transporting commands from the wait queue. 13149 */ 13150 sd_add_buf_to_waitq(un, bp); 13151 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13152 sd_start_cmds(un, NULL); 13153 } 13154 13155 mutex_exit(SD_MUTEX(un)); 13156 13157 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13158 } 13159 13160 13161 /* 13162 * Function: sd_init_cdb_limits 13163 * 13164 * Description: This is to handle scsi_pkt initialization differences 13165 * between the driver platforms. 13166 * 13167 * Legacy behaviors: 13168 * 13169 * If the block number or the sector count exceeds the 13170 * capabilities of a Group 0 command, shift over to a 13171 * Group 1 command. We don't blindly use Group 1 13172 * commands because a) some drives (CDC Wren IVs) get a 13173 * bit confused, and b) there is probably a fair amount 13174 * of speed difference for a target to receive and decode 13175 * a 10 byte command instead of a 6 byte command. 13176 * 13177 * The xfer time difference of 6 vs 10 byte CDBs is 13178 * still significant so this code is still worthwhile. 13179 * 10 byte CDBs are very inefficient with the fas HBA driver 13180 * and older disks. Each CDB byte took 1 usec with some 13181 * popular disks. 13182 * 13183 * Context: Must be called at attach time 13184 */ 13185 13186 static void 13187 sd_init_cdb_limits(struct sd_lun *un) 13188 { 13189 int hba_cdb_limit; 13190 13191 /* 13192 * Use CDB_GROUP1 commands for most devices except for 13193 * parallel SCSI fixed drives in which case we get better 13194 * performance using CDB_GROUP0 commands (where applicable). 13195 */ 13196 un->un_mincdb = SD_CDB_GROUP1; 13197 #if !defined(__fibre) 13198 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13199 !un->un_f_has_removable_media) { 13200 un->un_mincdb = SD_CDB_GROUP0; 13201 } 13202 #endif 13203 13204 /* 13205 * Try to read the max-cdb-length supported by HBA. 13206 */ 13207 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13208 if (0 >= un->un_max_hba_cdb) { 13209 un->un_max_hba_cdb = CDB_GROUP4; 13210 hba_cdb_limit = SD_CDB_GROUP4; 13211 } else if (0 < un->un_max_hba_cdb && 13212 un->un_max_hba_cdb < CDB_GROUP1) { 13213 hba_cdb_limit = SD_CDB_GROUP0; 13214 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13215 un->un_max_hba_cdb < CDB_GROUP5) { 13216 hba_cdb_limit = SD_CDB_GROUP1; 13217 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13218 un->un_max_hba_cdb < CDB_GROUP4) { 13219 hba_cdb_limit = SD_CDB_GROUP5; 13220 } else { 13221 hba_cdb_limit = SD_CDB_GROUP4; 13222 } 13223 13224 /* 13225 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13226 * commands for fixed disks unless we are building for a 32 bit 13227 * kernel. 13228 */ 13229 #ifdef _LP64 13230 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13231 min(hba_cdb_limit, SD_CDB_GROUP4); 13232 #else 13233 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13234 min(hba_cdb_limit, SD_CDB_GROUP1); 13235 #endif 13236 13237 /* 13238 * x86 systems require the PKT_DMA_PARTIAL flag 13239 */ 13240 #if defined(__x86) 13241 un->un_pkt_flags = PKT_DMA_PARTIAL; 13242 #else 13243 un->un_pkt_flags = 0; 13244 #endif 13245 13246 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13247 ? sizeof (struct scsi_arq_status) : 1); 13248 un->un_cmd_timeout = (ushort_t)sd_io_time; 13249 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13250 } 13251 13252 13253 /* 13254 * Function: sd_initpkt_for_buf 13255 * 13256 * Description: Allocate and initialize for transport a scsi_pkt struct, 13257 * based upon the info specified in the given buf struct. 13258 * 13259 * Assumes the xb_blkno in the request is absolute (ie, 13260 * relative to the start of the device (NOT partition!). 13261 * Also assumes that the request is using the native block 13262 * size of the device (as returned by the READ CAPACITY 13263 * command). 13264 * 13265 * Return Code: SD_PKT_ALLOC_SUCCESS 13266 * SD_PKT_ALLOC_FAILURE 13267 * SD_PKT_ALLOC_FAILURE_NO_DMA 13268 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13269 * 13270 * Context: Kernel thread and may be called from software interrupt context 13271 * as part of a sdrunout callback. This function may not block or 13272 * call routines that block 13273 */ 13274 13275 static int 13276 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13277 { 13278 struct sd_xbuf *xp; 13279 struct scsi_pkt *pktp = NULL; 13280 struct sd_lun *un; 13281 size_t blockcount; 13282 daddr_t startblock; 13283 int rval; 13284 int cmd_flags; 13285 13286 ASSERT(bp != NULL); 13287 ASSERT(pktpp != NULL); 13288 xp = SD_GET_XBUF(bp); 13289 ASSERT(xp != NULL); 13290 un = SD_GET_UN(bp); 13291 ASSERT(un != NULL); 13292 ASSERT(mutex_owned(SD_MUTEX(un))); 13293 ASSERT(bp->b_resid == 0); 13294 13295 SD_TRACE(SD_LOG_IO_CORE, un, 13296 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13297 13298 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13299 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13300 /* 13301 * Already have a scsi_pkt -- just need DMA resources. 13302 * We must recompute the CDB in case the mapping returns 13303 * a nonzero pkt_resid. 13304 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13305 * that is being retried, the unmap/remap of the DMA resouces 13306 * will result in the entire transfer starting over again 13307 * from the very first block. 13308 */ 13309 ASSERT(xp->xb_pktp != NULL); 13310 pktp = xp->xb_pktp; 13311 } else { 13312 pktp = NULL; 13313 } 13314 #endif /* __i386 || __amd64 */ 13315 13316 startblock = xp->xb_blkno; /* Absolute block num. */ 13317 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13318 13319 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13320 13321 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13322 13323 #else 13324 13325 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13326 13327 #endif 13328 13329 /* 13330 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13331 * call scsi_init_pkt, and build the CDB. 13332 */ 13333 rval = sd_setup_rw_pkt(un, &pktp, bp, 13334 cmd_flags, sdrunout, (caddr_t)un, 13335 startblock, blockcount); 13336 13337 if (rval == 0) { 13338 /* 13339 * Success. 13340 * 13341 * If partial DMA is being used and required for this transfer. 13342 * set it up here. 13343 */ 13344 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13345 (pktp->pkt_resid != 0)) { 13346 13347 /* 13348 * Save the CDB length and pkt_resid for the 13349 * next xfer 13350 */ 13351 xp->xb_dma_resid = pktp->pkt_resid; 13352 13353 /* rezero resid */ 13354 pktp->pkt_resid = 0; 13355 13356 } else { 13357 xp->xb_dma_resid = 0; 13358 } 13359 13360 pktp->pkt_flags = un->un_tagflags; 13361 pktp->pkt_time = un->un_cmd_timeout; 13362 pktp->pkt_comp = sdintr; 13363 13364 pktp->pkt_private = bp; 13365 *pktpp = pktp; 13366 13367 SD_TRACE(SD_LOG_IO_CORE, un, 13368 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13369 13370 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13371 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13372 #endif 13373 13374 return (SD_PKT_ALLOC_SUCCESS); 13375 13376 } 13377 13378 /* 13379 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13380 * from sd_setup_rw_pkt. 13381 */ 13382 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13383 13384 if (rval == SD_PKT_ALLOC_FAILURE) { 13385 *pktpp = NULL; 13386 /* 13387 * Set the driver state to RWAIT to indicate the driver 13388 * is waiting on resource allocations. The driver will not 13389 * suspend, pm_suspend, or detatch while the state is RWAIT. 13390 */ 13391 New_state(un, SD_STATE_RWAIT); 13392 13393 SD_ERROR(SD_LOG_IO_CORE, un, 13394 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13395 13396 if ((bp->b_flags & B_ERROR) != 0) { 13397 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13398 } 13399 return (SD_PKT_ALLOC_FAILURE); 13400 } else { 13401 /* 13402 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13403 * 13404 * This should never happen. Maybe someone messed with the 13405 * kernel's minphys? 13406 */ 13407 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13408 "Request rejected: too large for CDB: " 13409 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13410 SD_ERROR(SD_LOG_IO_CORE, un, 13411 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13412 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13413 13414 } 13415 } 13416 13417 13418 /* 13419 * Function: sd_destroypkt_for_buf 13420 * 13421 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13422 * 13423 * Context: Kernel thread or interrupt context 13424 */ 13425 13426 static void 13427 sd_destroypkt_for_buf(struct buf *bp) 13428 { 13429 ASSERT(bp != NULL); 13430 ASSERT(SD_GET_UN(bp) != NULL); 13431 13432 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13433 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13434 13435 ASSERT(SD_GET_PKTP(bp) != NULL); 13436 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13437 13438 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13439 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13440 } 13441 13442 /* 13443 * Function: sd_setup_rw_pkt 13444 * 13445 * Description: Determines appropriate CDB group for the requested LBA 13446 * and transfer length, calls scsi_init_pkt, and builds 13447 * the CDB. Do not use for partial DMA transfers except 13448 * for the initial transfer since the CDB size must 13449 * remain constant. 13450 * 13451 * Context: Kernel thread and may be called from software interrupt 13452 * context as part of a sdrunout callback. This function may not 13453 * block or call routines that block 13454 */ 13455 13456 13457 int 13458 sd_setup_rw_pkt(struct sd_lun *un, 13459 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13460 int (*callback)(caddr_t), caddr_t callback_arg, 13461 diskaddr_t lba, uint32_t blockcount) 13462 { 13463 struct scsi_pkt *return_pktp; 13464 union scsi_cdb *cdbp; 13465 struct sd_cdbinfo *cp = NULL; 13466 int i; 13467 13468 /* 13469 * See which size CDB to use, based upon the request. 13470 */ 13471 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13472 13473 /* 13474 * Check lba and block count against sd_cdbtab limits. 13475 * In the partial DMA case, we have to use the same size 13476 * CDB for all the transfers. Check lba + blockcount 13477 * against the max LBA so we know that segment of the 13478 * transfer can use the CDB we select. 13479 */ 13480 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13481 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13482 13483 /* 13484 * The command will fit into the CDB type 13485 * specified by sd_cdbtab[i]. 13486 */ 13487 cp = sd_cdbtab + i; 13488 13489 /* 13490 * Call scsi_init_pkt so we can fill in the 13491 * CDB. 13492 */ 13493 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13494 bp, cp->sc_grpcode, un->un_status_len, 0, 13495 flags, callback, callback_arg); 13496 13497 if (return_pktp != NULL) { 13498 13499 /* 13500 * Return new value of pkt 13501 */ 13502 *pktpp = return_pktp; 13503 13504 /* 13505 * To be safe, zero the CDB insuring there is 13506 * no leftover data from a previous command. 13507 */ 13508 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13509 13510 /* 13511 * Handle partial DMA mapping 13512 */ 13513 if (return_pktp->pkt_resid != 0) { 13514 13515 /* 13516 * Not going to xfer as many blocks as 13517 * originally expected 13518 */ 13519 blockcount -= 13520 SD_BYTES2TGTBLOCKS(un, 13521 return_pktp->pkt_resid); 13522 } 13523 13524 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13525 13526 /* 13527 * Set command byte based on the CDB 13528 * type we matched. 13529 */ 13530 cdbp->scc_cmd = cp->sc_grpmask | 13531 ((bp->b_flags & B_READ) ? 13532 SCMD_READ : SCMD_WRITE); 13533 13534 SD_FILL_SCSI1_LUN(un, return_pktp); 13535 13536 /* 13537 * Fill in LBA and length 13538 */ 13539 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13540 (cp->sc_grpcode == CDB_GROUP4) || 13541 (cp->sc_grpcode == CDB_GROUP0) || 13542 (cp->sc_grpcode == CDB_GROUP5)); 13543 13544 if (cp->sc_grpcode == CDB_GROUP1) { 13545 FORMG1ADDR(cdbp, lba); 13546 FORMG1COUNT(cdbp, blockcount); 13547 return (0); 13548 } else if (cp->sc_grpcode == CDB_GROUP4) { 13549 FORMG4LONGADDR(cdbp, lba); 13550 FORMG4COUNT(cdbp, blockcount); 13551 return (0); 13552 } else if (cp->sc_grpcode == CDB_GROUP0) { 13553 FORMG0ADDR(cdbp, lba); 13554 FORMG0COUNT(cdbp, blockcount); 13555 return (0); 13556 } else if (cp->sc_grpcode == CDB_GROUP5) { 13557 FORMG5ADDR(cdbp, lba); 13558 FORMG5COUNT(cdbp, blockcount); 13559 return (0); 13560 } 13561 13562 /* 13563 * It should be impossible to not match one 13564 * of the CDB types above, so we should never 13565 * reach this point. Set the CDB command byte 13566 * to test-unit-ready to avoid writing 13567 * to somewhere we don't intend. 13568 */ 13569 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13570 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13571 } else { 13572 /* 13573 * Couldn't get scsi_pkt 13574 */ 13575 return (SD_PKT_ALLOC_FAILURE); 13576 } 13577 } 13578 } 13579 13580 /* 13581 * None of the available CDB types were suitable. This really 13582 * should never happen: on a 64 bit system we support 13583 * READ16/WRITE16 which will hold an entire 64 bit disk address 13584 * and on a 32 bit system we will refuse to bind to a device 13585 * larger than 2TB so addresses will never be larger than 32 bits. 13586 */ 13587 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13588 } 13589 13590 #if defined(__i386) || defined(__amd64) 13591 /* 13592 * Function: sd_setup_next_rw_pkt 13593 * 13594 * Description: Setup packet for partial DMA transfers, except for the 13595 * initial transfer. sd_setup_rw_pkt should be used for 13596 * the initial transfer. 13597 * 13598 * Context: Kernel thread and may be called from interrupt context. 13599 */ 13600 13601 int 13602 sd_setup_next_rw_pkt(struct sd_lun *un, 13603 struct scsi_pkt *pktp, struct buf *bp, 13604 diskaddr_t lba, uint32_t blockcount) 13605 { 13606 uchar_t com; 13607 union scsi_cdb *cdbp; 13608 uchar_t cdb_group_id; 13609 13610 ASSERT(pktp != NULL); 13611 ASSERT(pktp->pkt_cdbp != NULL); 13612 13613 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13614 com = cdbp->scc_cmd; 13615 cdb_group_id = CDB_GROUPID(com); 13616 13617 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13618 (cdb_group_id == CDB_GROUPID_1) || 13619 (cdb_group_id == CDB_GROUPID_4) || 13620 (cdb_group_id == CDB_GROUPID_5)); 13621 13622 /* 13623 * Move pkt to the next portion of the xfer. 13624 * func is NULL_FUNC so we do not have to release 13625 * the disk mutex here. 13626 */ 13627 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13628 NULL_FUNC, NULL) == pktp) { 13629 /* Success. Handle partial DMA */ 13630 if (pktp->pkt_resid != 0) { 13631 blockcount -= 13632 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13633 } 13634 13635 cdbp->scc_cmd = com; 13636 SD_FILL_SCSI1_LUN(un, pktp); 13637 if (cdb_group_id == CDB_GROUPID_1) { 13638 FORMG1ADDR(cdbp, lba); 13639 FORMG1COUNT(cdbp, blockcount); 13640 return (0); 13641 } else if (cdb_group_id == CDB_GROUPID_4) { 13642 FORMG4LONGADDR(cdbp, lba); 13643 FORMG4COUNT(cdbp, blockcount); 13644 return (0); 13645 } else if (cdb_group_id == CDB_GROUPID_0) { 13646 FORMG0ADDR(cdbp, lba); 13647 FORMG0COUNT(cdbp, blockcount); 13648 return (0); 13649 } else if (cdb_group_id == CDB_GROUPID_5) { 13650 FORMG5ADDR(cdbp, lba); 13651 FORMG5COUNT(cdbp, blockcount); 13652 return (0); 13653 } 13654 13655 /* Unreachable */ 13656 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13657 } 13658 13659 /* 13660 * Error setting up next portion of cmd transfer. 13661 * Something is definitely very wrong and this 13662 * should not happen. 13663 */ 13664 return (SD_PKT_ALLOC_FAILURE); 13665 } 13666 #endif /* defined(__i386) || defined(__amd64) */ 13667 13668 /* 13669 * Function: sd_initpkt_for_uscsi 13670 * 13671 * Description: Allocate and initialize for transport a scsi_pkt struct, 13672 * based upon the info specified in the given uscsi_cmd struct. 13673 * 13674 * Return Code: SD_PKT_ALLOC_SUCCESS 13675 * SD_PKT_ALLOC_FAILURE 13676 * SD_PKT_ALLOC_FAILURE_NO_DMA 13677 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13678 * 13679 * Context: Kernel thread and may be called from software interrupt context 13680 * as part of a sdrunout callback. This function may not block or 13681 * call routines that block 13682 */ 13683 13684 static int 13685 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13686 { 13687 struct uscsi_cmd *uscmd; 13688 struct sd_xbuf *xp; 13689 struct scsi_pkt *pktp; 13690 struct sd_lun *un; 13691 uint32_t flags = 0; 13692 13693 ASSERT(bp != NULL); 13694 ASSERT(pktpp != NULL); 13695 xp = SD_GET_XBUF(bp); 13696 ASSERT(xp != NULL); 13697 un = SD_GET_UN(bp); 13698 ASSERT(un != NULL); 13699 ASSERT(mutex_owned(SD_MUTEX(un))); 13700 13701 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13702 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13703 ASSERT(uscmd != NULL); 13704 13705 SD_TRACE(SD_LOG_IO_CORE, un, 13706 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13707 13708 /* 13709 * Allocate the scsi_pkt for the command. 13710 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13711 * during scsi_init_pkt time and will continue to use the 13712 * same path as long as the same scsi_pkt is used without 13713 * intervening scsi_dma_free(). Since uscsi command does 13714 * not call scsi_dmafree() before retry failed command, it 13715 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13716 * set such that scsi_vhci can use other available path for 13717 * retry. Besides, ucsci command does not allow DMA breakup, 13718 * so there is no need to set PKT_DMA_PARTIAL flag. 13719 */ 13720 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13721 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13722 sizeof (struct scsi_arq_status), 0, 13723 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13724 sdrunout, (caddr_t)un); 13725 13726 if (pktp == NULL) { 13727 *pktpp = NULL; 13728 /* 13729 * Set the driver state to RWAIT to indicate the driver 13730 * is waiting on resource allocations. The driver will not 13731 * suspend, pm_suspend, or detatch while the state is RWAIT. 13732 */ 13733 New_state(un, SD_STATE_RWAIT); 13734 13735 SD_ERROR(SD_LOG_IO_CORE, un, 13736 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13737 13738 if ((bp->b_flags & B_ERROR) != 0) { 13739 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13740 } 13741 return (SD_PKT_ALLOC_FAILURE); 13742 } 13743 13744 /* 13745 * We do not do DMA breakup for USCSI commands, so return failure 13746 * here if all the needed DMA resources were not allocated. 13747 */ 13748 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13749 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13750 scsi_destroy_pkt(pktp); 13751 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13752 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13753 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13754 } 13755 13756 /* Init the cdb from the given uscsi struct */ 13757 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13758 uscmd->uscsi_cdb[0], 0, 0, 0); 13759 13760 SD_FILL_SCSI1_LUN(un, pktp); 13761 13762 /* 13763 * Set up the optional USCSI flags. See the uscsi (7I) man page 13764 * for listing of the supported flags. 13765 */ 13766 13767 if (uscmd->uscsi_flags & USCSI_SILENT) { 13768 flags |= FLAG_SILENT; 13769 } 13770 13771 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13772 flags |= FLAG_DIAGNOSE; 13773 } 13774 13775 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13776 flags |= FLAG_ISOLATE; 13777 } 13778 13779 if (un->un_f_is_fibre == FALSE) { 13780 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13781 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13782 } 13783 } 13784 13785 /* 13786 * Set the pkt flags here so we save time later. 13787 * Note: These flags are NOT in the uscsi man page!!! 13788 */ 13789 if (uscmd->uscsi_flags & USCSI_HEAD) { 13790 flags |= FLAG_HEAD; 13791 } 13792 13793 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13794 flags |= FLAG_NOINTR; 13795 } 13796 13797 /* 13798 * For tagged queueing, things get a bit complicated. 13799 * Check first for head of queue and last for ordered queue. 13800 * If neither head nor order, use the default driver tag flags. 13801 */ 13802 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13803 if (uscmd->uscsi_flags & USCSI_HTAG) { 13804 flags |= FLAG_HTAG; 13805 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13806 flags |= FLAG_OTAG; 13807 } else { 13808 flags |= un->un_tagflags & FLAG_TAGMASK; 13809 } 13810 } 13811 13812 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13813 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13814 } 13815 13816 pktp->pkt_flags = flags; 13817 13818 /* Copy the caller's CDB into the pkt... */ 13819 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13820 13821 if (uscmd->uscsi_timeout == 0) { 13822 pktp->pkt_time = un->un_uscsi_timeout; 13823 } else { 13824 pktp->pkt_time = uscmd->uscsi_timeout; 13825 } 13826 13827 /* need it later to identify USCSI request in sdintr */ 13828 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13829 13830 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13831 13832 pktp->pkt_private = bp; 13833 pktp->pkt_comp = sdintr; 13834 *pktpp = pktp; 13835 13836 SD_TRACE(SD_LOG_IO_CORE, un, 13837 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13838 13839 return (SD_PKT_ALLOC_SUCCESS); 13840 } 13841 13842 13843 /* 13844 * Function: sd_destroypkt_for_uscsi 13845 * 13846 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13847 * IOs.. Also saves relevant info into the associated uscsi_cmd 13848 * struct. 13849 * 13850 * Context: May be called under interrupt context 13851 */ 13852 13853 static void 13854 sd_destroypkt_for_uscsi(struct buf *bp) 13855 { 13856 struct uscsi_cmd *uscmd; 13857 struct sd_xbuf *xp; 13858 struct scsi_pkt *pktp; 13859 struct sd_lun *un; 13860 13861 ASSERT(bp != NULL); 13862 xp = SD_GET_XBUF(bp); 13863 ASSERT(xp != NULL); 13864 un = SD_GET_UN(bp); 13865 ASSERT(un != NULL); 13866 ASSERT(!mutex_owned(SD_MUTEX(un))); 13867 pktp = SD_GET_PKTP(bp); 13868 ASSERT(pktp != NULL); 13869 13870 SD_TRACE(SD_LOG_IO_CORE, un, 13871 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13872 13873 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13874 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13875 ASSERT(uscmd != NULL); 13876 13877 /* Save the status and the residual into the uscsi_cmd struct */ 13878 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13879 uscmd->uscsi_resid = bp->b_resid; 13880 13881 /* 13882 * If enabled, copy any saved sense data into the area specified 13883 * by the uscsi command. 13884 */ 13885 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13886 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13887 /* 13888 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13889 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13890 */ 13891 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13892 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13893 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13894 } 13895 13896 /* We are done with the scsi_pkt; free it now */ 13897 ASSERT(SD_GET_PKTP(bp) != NULL); 13898 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13899 13900 SD_TRACE(SD_LOG_IO_CORE, un, 13901 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13902 } 13903 13904 13905 /* 13906 * Function: sd_bioclone_alloc 13907 * 13908 * Description: Allocate a buf(9S) and init it as per the given buf 13909 * and the various arguments. The associated sd_xbuf 13910 * struct is (nearly) duplicated. The struct buf *bp 13911 * argument is saved in new_xp->xb_private. 13912 * 13913 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13914 * datalen - size of data area for the shadow bp 13915 * blkno - starting LBA 13916 * func - function pointer for b_iodone in the shadow buf. (May 13917 * be NULL if none.) 13918 * 13919 * Return Code: Pointer to allocates buf(9S) struct 13920 * 13921 * Context: Can sleep. 13922 */ 13923 13924 static struct buf * 13925 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13926 daddr_t blkno, int (*func)(struct buf *)) 13927 { 13928 struct sd_lun *un; 13929 struct sd_xbuf *xp; 13930 struct sd_xbuf *new_xp; 13931 struct buf *new_bp; 13932 13933 ASSERT(bp != NULL); 13934 xp = SD_GET_XBUF(bp); 13935 ASSERT(xp != NULL); 13936 un = SD_GET_UN(bp); 13937 ASSERT(un != NULL); 13938 ASSERT(!mutex_owned(SD_MUTEX(un))); 13939 13940 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13941 NULL, KM_SLEEP); 13942 13943 new_bp->b_lblkno = blkno; 13944 13945 /* 13946 * Allocate an xbuf for the shadow bp and copy the contents of the 13947 * original xbuf into it. 13948 */ 13949 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13950 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13951 13952 /* 13953 * The given bp is automatically saved in the xb_private member 13954 * of the new xbuf. Callers are allowed to depend on this. 13955 */ 13956 new_xp->xb_private = bp; 13957 13958 new_bp->b_private = new_xp; 13959 13960 return (new_bp); 13961 } 13962 13963 /* 13964 * Function: sd_shadow_buf_alloc 13965 * 13966 * Description: Allocate a buf(9S) and init it as per the given buf 13967 * and the various arguments. The associated sd_xbuf 13968 * struct is (nearly) duplicated. The struct buf *bp 13969 * argument is saved in new_xp->xb_private. 13970 * 13971 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13972 * datalen - size of data area for the shadow bp 13973 * bflags - B_READ or B_WRITE (pseudo flag) 13974 * blkno - starting LBA 13975 * func - function pointer for b_iodone in the shadow buf. (May 13976 * be NULL if none.) 13977 * 13978 * Return Code: Pointer to allocates buf(9S) struct 13979 * 13980 * Context: Can sleep. 13981 */ 13982 13983 static struct buf * 13984 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13985 daddr_t blkno, int (*func)(struct buf *)) 13986 { 13987 struct sd_lun *un; 13988 struct sd_xbuf *xp; 13989 struct sd_xbuf *new_xp; 13990 struct buf *new_bp; 13991 13992 ASSERT(bp != NULL); 13993 xp = SD_GET_XBUF(bp); 13994 ASSERT(xp != NULL); 13995 un = SD_GET_UN(bp); 13996 ASSERT(un != NULL); 13997 ASSERT(!mutex_owned(SD_MUTEX(un))); 13998 13999 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14000 bp_mapin(bp); 14001 } 14002 14003 bflags &= (B_READ | B_WRITE); 14004 #if defined(__i386) || defined(__amd64) 14005 new_bp = getrbuf(KM_SLEEP); 14006 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14007 new_bp->b_bcount = datalen; 14008 new_bp->b_flags = bflags | 14009 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14010 #else 14011 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14012 datalen, bflags, SLEEP_FUNC, NULL); 14013 #endif 14014 new_bp->av_forw = NULL; 14015 new_bp->av_back = NULL; 14016 new_bp->b_dev = bp->b_dev; 14017 new_bp->b_blkno = blkno; 14018 new_bp->b_iodone = func; 14019 new_bp->b_edev = bp->b_edev; 14020 new_bp->b_resid = 0; 14021 14022 /* We need to preserve the B_FAILFAST flag */ 14023 if (bp->b_flags & B_FAILFAST) { 14024 new_bp->b_flags |= B_FAILFAST; 14025 } 14026 14027 /* 14028 * Allocate an xbuf for the shadow bp and copy the contents of the 14029 * original xbuf into it. 14030 */ 14031 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14032 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14033 14034 /* Need later to copy data between the shadow buf & original buf! */ 14035 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14036 14037 /* 14038 * The given bp is automatically saved in the xb_private member 14039 * of the new xbuf. Callers are allowed to depend on this. 14040 */ 14041 new_xp->xb_private = bp; 14042 14043 new_bp->b_private = new_xp; 14044 14045 return (new_bp); 14046 } 14047 14048 /* 14049 * Function: sd_bioclone_free 14050 * 14051 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14052 * in the larger than partition operation. 14053 * 14054 * Context: May be called under interrupt context 14055 */ 14056 14057 static void 14058 sd_bioclone_free(struct buf *bp) 14059 { 14060 struct sd_xbuf *xp; 14061 14062 ASSERT(bp != NULL); 14063 xp = SD_GET_XBUF(bp); 14064 ASSERT(xp != NULL); 14065 14066 /* 14067 * Call bp_mapout() before freeing the buf, in case a lower 14068 * layer or HBA had done a bp_mapin(). we must do this here 14069 * as we are the "originator" of the shadow buf. 14070 */ 14071 bp_mapout(bp); 14072 14073 /* 14074 * Null out b_iodone before freeing the bp, to ensure that the driver 14075 * never gets confused by a stale value in this field. (Just a little 14076 * extra defensiveness here.) 14077 */ 14078 bp->b_iodone = NULL; 14079 14080 freerbuf(bp); 14081 14082 kmem_free(xp, sizeof (struct sd_xbuf)); 14083 } 14084 14085 /* 14086 * Function: sd_shadow_buf_free 14087 * 14088 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14089 * 14090 * Context: May be called under interrupt context 14091 */ 14092 14093 static void 14094 sd_shadow_buf_free(struct buf *bp) 14095 { 14096 struct sd_xbuf *xp; 14097 14098 ASSERT(bp != NULL); 14099 xp = SD_GET_XBUF(bp); 14100 ASSERT(xp != NULL); 14101 14102 #if defined(__sparc) 14103 /* 14104 * Call bp_mapout() before freeing the buf, in case a lower 14105 * layer or HBA had done a bp_mapin(). we must do this here 14106 * as we are the "originator" of the shadow buf. 14107 */ 14108 bp_mapout(bp); 14109 #endif 14110 14111 /* 14112 * Null out b_iodone before freeing the bp, to ensure that the driver 14113 * never gets confused by a stale value in this field. (Just a little 14114 * extra defensiveness here.) 14115 */ 14116 bp->b_iodone = NULL; 14117 14118 #if defined(__i386) || defined(__amd64) 14119 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14120 freerbuf(bp); 14121 #else 14122 scsi_free_consistent_buf(bp); 14123 #endif 14124 14125 kmem_free(xp, sizeof (struct sd_xbuf)); 14126 } 14127 14128 14129 /* 14130 * Function: sd_print_transport_rejected_message 14131 * 14132 * Description: This implements the ludicrously complex rules for printing 14133 * a "transport rejected" message. This is to address the 14134 * specific problem of having a flood of this error message 14135 * produced when a failover occurs. 14136 * 14137 * Context: Any. 14138 */ 14139 14140 static void 14141 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14142 int code) 14143 { 14144 ASSERT(un != NULL); 14145 ASSERT(mutex_owned(SD_MUTEX(un))); 14146 ASSERT(xp != NULL); 14147 14148 /* 14149 * Print the "transport rejected" message under the following 14150 * conditions: 14151 * 14152 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14153 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14154 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14155 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14156 * scsi_transport(9F) (which indicates that the target might have 14157 * gone off-line). This uses the un->un_tran_fatal_count 14158 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14159 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14160 * from scsi_transport(). 14161 * 14162 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14163 * the preceeding cases in order for the message to be printed. 14164 */ 14165 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14166 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14167 (code != TRAN_FATAL_ERROR) || 14168 (un->un_tran_fatal_count == 1)) { 14169 switch (code) { 14170 case TRAN_BADPKT: 14171 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14172 "transport rejected bad packet\n"); 14173 break; 14174 case TRAN_FATAL_ERROR: 14175 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14176 "transport rejected fatal error\n"); 14177 break; 14178 default: 14179 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14180 "transport rejected (%d)\n", code); 14181 break; 14182 } 14183 } 14184 } 14185 } 14186 14187 14188 /* 14189 * Function: sd_add_buf_to_waitq 14190 * 14191 * Description: Add the given buf(9S) struct to the wait queue for the 14192 * instance. If sorting is enabled, then the buf is added 14193 * to the queue via an elevator sort algorithm (a la 14194 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14195 * If sorting is not enabled, then the buf is just added 14196 * to the end of the wait queue. 14197 * 14198 * Return Code: void 14199 * 14200 * Context: Does not sleep/block, therefore technically can be called 14201 * from any context. However if sorting is enabled then the 14202 * execution time is indeterminate, and may take long if 14203 * the wait queue grows large. 14204 */ 14205 14206 static void 14207 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14208 { 14209 struct buf *ap; 14210 14211 ASSERT(bp != NULL); 14212 ASSERT(un != NULL); 14213 ASSERT(mutex_owned(SD_MUTEX(un))); 14214 14215 /* If the queue is empty, add the buf as the only entry & return. */ 14216 if (un->un_waitq_headp == NULL) { 14217 ASSERT(un->un_waitq_tailp == NULL); 14218 un->un_waitq_headp = un->un_waitq_tailp = bp; 14219 bp->av_forw = NULL; 14220 return; 14221 } 14222 14223 ASSERT(un->un_waitq_tailp != NULL); 14224 14225 /* 14226 * If sorting is disabled, just add the buf to the tail end of 14227 * the wait queue and return. 14228 */ 14229 if (un->un_f_disksort_disabled) { 14230 un->un_waitq_tailp->av_forw = bp; 14231 un->un_waitq_tailp = bp; 14232 bp->av_forw = NULL; 14233 return; 14234 } 14235 14236 /* 14237 * Sort thru the list of requests currently on the wait queue 14238 * and add the new buf request at the appropriate position. 14239 * 14240 * The un->un_waitq_headp is an activity chain pointer on which 14241 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14242 * first queue holds those requests which are positioned after 14243 * the current SD_GET_BLKNO() (in the first request); the second holds 14244 * requests which came in after their SD_GET_BLKNO() number was passed. 14245 * Thus we implement a one way scan, retracting after reaching 14246 * the end of the drive to the first request on the second 14247 * queue, at which time it becomes the first queue. 14248 * A one-way scan is natural because of the way UNIX read-ahead 14249 * blocks are allocated. 14250 * 14251 * If we lie after the first request, then we must locate the 14252 * second request list and add ourselves to it. 14253 */ 14254 ap = un->un_waitq_headp; 14255 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14256 while (ap->av_forw != NULL) { 14257 /* 14258 * Look for an "inversion" in the (normally 14259 * ascending) block numbers. This indicates 14260 * the start of the second request list. 14261 */ 14262 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14263 /* 14264 * Search the second request list for the 14265 * first request at a larger block number. 14266 * We go before that; however if there is 14267 * no such request, we go at the end. 14268 */ 14269 do { 14270 if (SD_GET_BLKNO(bp) < 14271 SD_GET_BLKNO(ap->av_forw)) { 14272 goto insert; 14273 } 14274 ap = ap->av_forw; 14275 } while (ap->av_forw != NULL); 14276 goto insert; /* after last */ 14277 } 14278 ap = ap->av_forw; 14279 } 14280 14281 /* 14282 * No inversions... we will go after the last, and 14283 * be the first request in the second request list. 14284 */ 14285 goto insert; 14286 } 14287 14288 /* 14289 * Request is at/after the current request... 14290 * sort in the first request list. 14291 */ 14292 while (ap->av_forw != NULL) { 14293 /* 14294 * We want to go after the current request (1) if 14295 * there is an inversion after it (i.e. it is the end 14296 * of the first request list), or (2) if the next 14297 * request is a larger block no. than our request. 14298 */ 14299 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14300 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14301 goto insert; 14302 } 14303 ap = ap->av_forw; 14304 } 14305 14306 /* 14307 * Neither a second list nor a larger request, therefore 14308 * we go at the end of the first list (which is the same 14309 * as the end of the whole schebang). 14310 */ 14311 insert: 14312 bp->av_forw = ap->av_forw; 14313 ap->av_forw = bp; 14314 14315 /* 14316 * If we inserted onto the tail end of the waitq, make sure the 14317 * tail pointer is updated. 14318 */ 14319 if (ap == un->un_waitq_tailp) { 14320 un->un_waitq_tailp = bp; 14321 } 14322 } 14323 14324 14325 /* 14326 * Function: sd_start_cmds 14327 * 14328 * Description: Remove and transport cmds from the driver queues. 14329 * 14330 * Arguments: un - pointer to the unit (soft state) struct for the target. 14331 * 14332 * immed_bp - ptr to a buf to be transported immediately. Only 14333 * the immed_bp is transported; bufs on the waitq are not 14334 * processed and the un_retry_bp is not checked. If immed_bp is 14335 * NULL, then normal queue processing is performed. 14336 * 14337 * Context: May be called from kernel thread context, interrupt context, 14338 * or runout callback context. This function may not block or 14339 * call routines that block. 14340 */ 14341 14342 static void 14343 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14344 { 14345 struct sd_xbuf *xp; 14346 struct buf *bp; 14347 void (*statp)(kstat_io_t *); 14348 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14349 void (*saved_statp)(kstat_io_t *); 14350 #endif 14351 int rval; 14352 14353 ASSERT(un != NULL); 14354 ASSERT(mutex_owned(SD_MUTEX(un))); 14355 ASSERT(un->un_ncmds_in_transport >= 0); 14356 ASSERT(un->un_throttle >= 0); 14357 14358 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14359 14360 do { 14361 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14362 saved_statp = NULL; 14363 #endif 14364 14365 /* 14366 * If we are syncing or dumping, fail the command to 14367 * avoid recursively calling back into scsi_transport(). 14368 * The dump I/O itself uses a separate code path so this 14369 * only prevents non-dump I/O from being sent while dumping. 14370 * File system sync takes place before dumping begins. 14371 * During panic, filesystem I/O is allowed provided 14372 * un_in_callback is <= 1. This is to prevent recursion 14373 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14374 * sd_start_cmds and so on. See panic.c for more information 14375 * about the states the system can be in during panic. 14376 */ 14377 if ((un->un_state == SD_STATE_DUMPING) || 14378 (ddi_in_panic() && (un->un_in_callback > 1))) { 14379 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14380 "sd_start_cmds: panicking\n"); 14381 goto exit; 14382 } 14383 14384 if ((bp = immed_bp) != NULL) { 14385 /* 14386 * We have a bp that must be transported immediately. 14387 * It's OK to transport the immed_bp here without doing 14388 * the throttle limit check because the immed_bp is 14389 * always used in a retry/recovery case. This means 14390 * that we know we are not at the throttle limit by 14391 * virtue of the fact that to get here we must have 14392 * already gotten a command back via sdintr(). This also 14393 * relies on (1) the command on un_retry_bp preventing 14394 * further commands from the waitq from being issued; 14395 * and (2) the code in sd_retry_command checking the 14396 * throttle limit before issuing a delayed or immediate 14397 * retry. This holds even if the throttle limit is 14398 * currently ratcheted down from its maximum value. 14399 */ 14400 statp = kstat_runq_enter; 14401 if (bp == un->un_retry_bp) { 14402 ASSERT((un->un_retry_statp == NULL) || 14403 (un->un_retry_statp == kstat_waitq_enter) || 14404 (un->un_retry_statp == 14405 kstat_runq_back_to_waitq)); 14406 /* 14407 * If the waitq kstat was incremented when 14408 * sd_set_retry_bp() queued this bp for a retry, 14409 * then we must set up statp so that the waitq 14410 * count will get decremented correctly below. 14411 * Also we must clear un->un_retry_statp to 14412 * ensure that we do not act on a stale value 14413 * in this field. 14414 */ 14415 if ((un->un_retry_statp == kstat_waitq_enter) || 14416 (un->un_retry_statp == 14417 kstat_runq_back_to_waitq)) { 14418 statp = kstat_waitq_to_runq; 14419 } 14420 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14421 saved_statp = un->un_retry_statp; 14422 #endif 14423 un->un_retry_statp = NULL; 14424 14425 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14426 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14427 "un_throttle:%d un_ncmds_in_transport:%d\n", 14428 un, un->un_retry_bp, un->un_throttle, 14429 un->un_ncmds_in_transport); 14430 } else { 14431 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14432 "processing priority bp:0x%p\n", bp); 14433 } 14434 14435 } else if ((bp = un->un_waitq_headp) != NULL) { 14436 /* 14437 * A command on the waitq is ready to go, but do not 14438 * send it if: 14439 * 14440 * (1) the throttle limit has been reached, or 14441 * (2) a retry is pending, or 14442 * (3) a START_STOP_UNIT callback pending, or 14443 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14444 * command is pending. 14445 * 14446 * For all of these conditions, IO processing will 14447 * restart after the condition is cleared. 14448 */ 14449 if (un->un_ncmds_in_transport >= un->un_throttle) { 14450 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14451 "sd_start_cmds: exiting, " 14452 "throttle limit reached!\n"); 14453 goto exit; 14454 } 14455 if (un->un_retry_bp != NULL) { 14456 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14457 "sd_start_cmds: exiting, retry pending!\n"); 14458 goto exit; 14459 } 14460 if (un->un_startstop_timeid != NULL) { 14461 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14462 "sd_start_cmds: exiting, " 14463 "START_STOP pending!\n"); 14464 goto exit; 14465 } 14466 if (un->un_direct_priority_timeid != NULL) { 14467 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14468 "sd_start_cmds: exiting, " 14469 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14470 goto exit; 14471 } 14472 14473 /* Dequeue the command */ 14474 un->un_waitq_headp = bp->av_forw; 14475 if (un->un_waitq_headp == NULL) { 14476 un->un_waitq_tailp = NULL; 14477 } 14478 bp->av_forw = NULL; 14479 statp = kstat_waitq_to_runq; 14480 SD_TRACE(SD_LOG_IO_CORE, un, 14481 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14482 14483 } else { 14484 /* No work to do so bail out now */ 14485 SD_TRACE(SD_LOG_IO_CORE, un, 14486 "sd_start_cmds: no more work, exiting!\n"); 14487 goto exit; 14488 } 14489 14490 /* 14491 * Reset the state to normal. This is the mechanism by which 14492 * the state transitions from either SD_STATE_RWAIT or 14493 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14494 * If state is SD_STATE_PM_CHANGING then this command is 14495 * part of the device power control and the state must 14496 * not be put back to normal. Doing so would would 14497 * allow new commands to proceed when they shouldn't, 14498 * the device may be going off. 14499 */ 14500 if ((un->un_state != SD_STATE_SUSPENDED) && 14501 (un->un_state != SD_STATE_PM_CHANGING)) { 14502 New_state(un, SD_STATE_NORMAL); 14503 } 14504 14505 xp = SD_GET_XBUF(bp); 14506 ASSERT(xp != NULL); 14507 14508 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14509 /* 14510 * Allocate the scsi_pkt if we need one, or attach DMA 14511 * resources if we have a scsi_pkt that needs them. The 14512 * latter should only occur for commands that are being 14513 * retried. 14514 */ 14515 if ((xp->xb_pktp == NULL) || 14516 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14517 #else 14518 if (xp->xb_pktp == NULL) { 14519 #endif 14520 /* 14521 * There is no scsi_pkt allocated for this buf. Call 14522 * the initpkt function to allocate & init one. 14523 * 14524 * The scsi_init_pkt runout callback functionality is 14525 * implemented as follows: 14526 * 14527 * 1) The initpkt function always calls 14528 * scsi_init_pkt(9F) with sdrunout specified as the 14529 * callback routine. 14530 * 2) A successful packet allocation is initialized and 14531 * the I/O is transported. 14532 * 3) The I/O associated with an allocation resource 14533 * failure is left on its queue to be retried via 14534 * runout or the next I/O. 14535 * 4) The I/O associated with a DMA error is removed 14536 * from the queue and failed with EIO. Processing of 14537 * the transport queues is also halted to be 14538 * restarted via runout or the next I/O. 14539 * 5) The I/O associated with a CDB size or packet 14540 * size error is removed from the queue and failed 14541 * with EIO. Processing of the transport queues is 14542 * continued. 14543 * 14544 * Note: there is no interface for canceling a runout 14545 * callback. To prevent the driver from detaching or 14546 * suspending while a runout is pending the driver 14547 * state is set to SD_STATE_RWAIT 14548 * 14549 * Note: using the scsi_init_pkt callback facility can 14550 * result in an I/O request persisting at the head of 14551 * the list which cannot be satisfied even after 14552 * multiple retries. In the future the driver may 14553 * implement some kind of maximum runout count before 14554 * failing an I/O. 14555 * 14556 * Note: the use of funcp below may seem superfluous, 14557 * but it helps warlock figure out the correct 14558 * initpkt function calls (see [s]sd.wlcmd). 14559 */ 14560 struct scsi_pkt *pktp; 14561 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14562 14563 ASSERT(bp != un->un_rqs_bp); 14564 14565 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14566 switch ((*funcp)(bp, &pktp)) { 14567 case SD_PKT_ALLOC_SUCCESS: 14568 xp->xb_pktp = pktp; 14569 SD_TRACE(SD_LOG_IO_CORE, un, 14570 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14571 pktp); 14572 goto got_pkt; 14573 14574 case SD_PKT_ALLOC_FAILURE: 14575 /* 14576 * Temporary (hopefully) resource depletion. 14577 * Since retries and RQS commands always have a 14578 * scsi_pkt allocated, these cases should never 14579 * get here. So the only cases this needs to 14580 * handle is a bp from the waitq (which we put 14581 * back onto the waitq for sdrunout), or a bp 14582 * sent as an immed_bp (which we just fail). 14583 */ 14584 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14585 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14586 14587 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14588 14589 if (bp == immed_bp) { 14590 /* 14591 * If SD_XB_DMA_FREED is clear, then 14592 * this is a failure to allocate a 14593 * scsi_pkt, and we must fail the 14594 * command. 14595 */ 14596 if ((xp->xb_pkt_flags & 14597 SD_XB_DMA_FREED) == 0) { 14598 break; 14599 } 14600 14601 /* 14602 * If this immediate command is NOT our 14603 * un_retry_bp, then we must fail it. 14604 */ 14605 if (bp != un->un_retry_bp) { 14606 break; 14607 } 14608 14609 /* 14610 * We get here if this cmd is our 14611 * un_retry_bp that was DMAFREED, but 14612 * scsi_init_pkt() failed to reallocate 14613 * DMA resources when we attempted to 14614 * retry it. This can happen when an 14615 * mpxio failover is in progress, but 14616 * we don't want to just fail the 14617 * command in this case. 14618 * 14619 * Use timeout(9F) to restart it after 14620 * a 100ms delay. We don't want to 14621 * let sdrunout() restart it, because 14622 * sdrunout() is just supposed to start 14623 * commands that are sitting on the 14624 * wait queue. The un_retry_bp stays 14625 * set until the command completes, but 14626 * sdrunout can be called many times 14627 * before that happens. Since sdrunout 14628 * cannot tell if the un_retry_bp is 14629 * already in the transport, it could 14630 * end up calling scsi_transport() for 14631 * the un_retry_bp multiple times. 14632 * 14633 * Also: don't schedule the callback 14634 * if some other callback is already 14635 * pending. 14636 */ 14637 if (un->un_retry_statp == NULL) { 14638 /* 14639 * restore the kstat pointer to 14640 * keep kstat counts coherent 14641 * when we do retry the command. 14642 */ 14643 un->un_retry_statp = 14644 saved_statp; 14645 } 14646 14647 if ((un->un_startstop_timeid == NULL) && 14648 (un->un_retry_timeid == NULL) && 14649 (un->un_direct_priority_timeid == 14650 NULL)) { 14651 14652 un->un_retry_timeid = 14653 timeout( 14654 sd_start_retry_command, 14655 un, SD_RESTART_TIMEOUT); 14656 } 14657 goto exit; 14658 } 14659 14660 #else 14661 if (bp == immed_bp) { 14662 break; /* Just fail the command */ 14663 } 14664 #endif 14665 14666 /* Add the buf back to the head of the waitq */ 14667 bp->av_forw = un->un_waitq_headp; 14668 un->un_waitq_headp = bp; 14669 if (un->un_waitq_tailp == NULL) { 14670 un->un_waitq_tailp = bp; 14671 } 14672 goto exit; 14673 14674 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14675 /* 14676 * HBA DMA resource failure. Fail the command 14677 * and continue processing of the queues. 14678 */ 14679 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14680 "sd_start_cmds: " 14681 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14682 break; 14683 14684 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14685 /* 14686 * Note:x86: Partial DMA mapping not supported 14687 * for USCSI commands, and all the needed DMA 14688 * resources were not allocated. 14689 */ 14690 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14691 "sd_start_cmds: " 14692 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14693 break; 14694 14695 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14696 /* 14697 * Note:x86: Request cannot fit into CDB based 14698 * on lba and len. 14699 */ 14700 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14701 "sd_start_cmds: " 14702 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14703 break; 14704 14705 default: 14706 /* Should NEVER get here! */ 14707 panic("scsi_initpkt error"); 14708 /*NOTREACHED*/ 14709 } 14710 14711 /* 14712 * Fatal error in allocating a scsi_pkt for this buf. 14713 * Update kstats & return the buf with an error code. 14714 * We must use sd_return_failed_command_no_restart() to 14715 * avoid a recursive call back into sd_start_cmds(). 14716 * However this also means that we must keep processing 14717 * the waitq here in order to avoid stalling. 14718 */ 14719 if (statp == kstat_waitq_to_runq) { 14720 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14721 } 14722 sd_return_failed_command_no_restart(un, bp, EIO); 14723 if (bp == immed_bp) { 14724 /* immed_bp is gone by now, so clear this */ 14725 immed_bp = NULL; 14726 } 14727 continue; 14728 } 14729 got_pkt: 14730 if (bp == immed_bp) { 14731 /* goto the head of the class.... */ 14732 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14733 } 14734 14735 un->un_ncmds_in_transport++; 14736 SD_UPDATE_KSTATS(un, statp, bp); 14737 14738 /* 14739 * Call scsi_transport() to send the command to the target. 14740 * According to SCSA architecture, we must drop the mutex here 14741 * before calling scsi_transport() in order to avoid deadlock. 14742 * Note that the scsi_pkt's completion routine can be executed 14743 * (from interrupt context) even before the call to 14744 * scsi_transport() returns. 14745 */ 14746 SD_TRACE(SD_LOG_IO_CORE, un, 14747 "sd_start_cmds: calling scsi_transport()\n"); 14748 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14749 14750 mutex_exit(SD_MUTEX(un)); 14751 rval = scsi_transport(xp->xb_pktp); 14752 mutex_enter(SD_MUTEX(un)); 14753 14754 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14755 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14756 14757 switch (rval) { 14758 case TRAN_ACCEPT: 14759 /* Clear this with every pkt accepted by the HBA */ 14760 un->un_tran_fatal_count = 0; 14761 break; /* Success; try the next cmd (if any) */ 14762 14763 case TRAN_BUSY: 14764 un->un_ncmds_in_transport--; 14765 ASSERT(un->un_ncmds_in_transport >= 0); 14766 14767 /* 14768 * Don't retry request sense, the sense data 14769 * is lost when another request is sent. 14770 * Free up the rqs buf and retry 14771 * the original failed cmd. Update kstat. 14772 */ 14773 if (bp == un->un_rqs_bp) { 14774 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14775 bp = sd_mark_rqs_idle(un, xp); 14776 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14777 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14778 kstat_waitq_enter); 14779 goto exit; 14780 } 14781 14782 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14783 /* 14784 * Free the DMA resources for the scsi_pkt. This will 14785 * allow mpxio to select another path the next time 14786 * we call scsi_transport() with this scsi_pkt. 14787 * See sdintr() for the rationalization behind this. 14788 */ 14789 if ((un->un_f_is_fibre == TRUE) && 14790 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14791 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14792 scsi_dmafree(xp->xb_pktp); 14793 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14794 } 14795 #endif 14796 14797 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14798 /* 14799 * Commands that are SD_PATH_DIRECT_PRIORITY 14800 * are for error recovery situations. These do 14801 * not use the normal command waitq, so if they 14802 * get a TRAN_BUSY we cannot put them back onto 14803 * the waitq for later retry. One possible 14804 * problem is that there could already be some 14805 * other command on un_retry_bp that is waiting 14806 * for this one to complete, so we would be 14807 * deadlocked if we put this command back onto 14808 * the waitq for later retry (since un_retry_bp 14809 * must complete before the driver gets back to 14810 * commands on the waitq). 14811 * 14812 * To avoid deadlock we must schedule a callback 14813 * that will restart this command after a set 14814 * interval. This should keep retrying for as 14815 * long as the underlying transport keeps 14816 * returning TRAN_BUSY (just like for other 14817 * commands). Use the same timeout interval as 14818 * for the ordinary TRAN_BUSY retry. 14819 */ 14820 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14821 "sd_start_cmds: scsi_transport() returned " 14822 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14823 14824 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14825 un->un_direct_priority_timeid = 14826 timeout(sd_start_direct_priority_command, 14827 bp, SD_BSY_TIMEOUT / 500); 14828 14829 goto exit; 14830 } 14831 14832 /* 14833 * For TRAN_BUSY, we want to reduce the throttle value, 14834 * unless we are retrying a command. 14835 */ 14836 if (bp != un->un_retry_bp) { 14837 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14838 } 14839 14840 /* 14841 * Set up the bp to be tried again 10 ms later. 14842 * Note:x86: Is there a timeout value in the sd_lun 14843 * for this condition? 14844 */ 14845 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14846 kstat_runq_back_to_waitq); 14847 goto exit; 14848 14849 case TRAN_FATAL_ERROR: 14850 un->un_tran_fatal_count++; 14851 /* FALLTHRU */ 14852 14853 case TRAN_BADPKT: 14854 default: 14855 un->un_ncmds_in_transport--; 14856 ASSERT(un->un_ncmds_in_transport >= 0); 14857 14858 /* 14859 * If this is our REQUEST SENSE command with a 14860 * transport error, we must get back the pointers 14861 * to the original buf, and mark the REQUEST 14862 * SENSE command as "available". 14863 */ 14864 if (bp == un->un_rqs_bp) { 14865 bp = sd_mark_rqs_idle(un, xp); 14866 xp = SD_GET_XBUF(bp); 14867 } else { 14868 /* 14869 * Legacy behavior: do not update transport 14870 * error count for request sense commands. 14871 */ 14872 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14873 } 14874 14875 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14876 sd_print_transport_rejected_message(un, xp, rval); 14877 14878 /* 14879 * We must use sd_return_failed_command_no_restart() to 14880 * avoid a recursive call back into sd_start_cmds(). 14881 * However this also means that we must keep processing 14882 * the waitq here in order to avoid stalling. 14883 */ 14884 sd_return_failed_command_no_restart(un, bp, EIO); 14885 14886 /* 14887 * Notify any threads waiting in sd_ddi_suspend() that 14888 * a command completion has occurred. 14889 */ 14890 if (un->un_state == SD_STATE_SUSPENDED) { 14891 cv_broadcast(&un->un_disk_busy_cv); 14892 } 14893 14894 if (bp == immed_bp) { 14895 /* immed_bp is gone by now, so clear this */ 14896 immed_bp = NULL; 14897 } 14898 break; 14899 } 14900 14901 } while (immed_bp == NULL); 14902 14903 exit: 14904 ASSERT(mutex_owned(SD_MUTEX(un))); 14905 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14906 } 14907 14908 14909 /* 14910 * Function: sd_return_command 14911 * 14912 * Description: Returns a command to its originator (with or without an 14913 * error). Also starts commands waiting to be transported 14914 * to the target. 14915 * 14916 * Context: May be called from interrupt, kernel, or timeout context 14917 */ 14918 14919 static void 14920 sd_return_command(struct sd_lun *un, struct buf *bp) 14921 { 14922 struct sd_xbuf *xp; 14923 #if defined(__i386) || defined(__amd64) 14924 struct scsi_pkt *pktp; 14925 #endif 14926 14927 ASSERT(bp != NULL); 14928 ASSERT(un != NULL); 14929 ASSERT(mutex_owned(SD_MUTEX(un))); 14930 ASSERT(bp != un->un_rqs_bp); 14931 xp = SD_GET_XBUF(bp); 14932 ASSERT(xp != NULL); 14933 14934 #if defined(__i386) || defined(__amd64) 14935 pktp = SD_GET_PKTP(bp); 14936 #endif 14937 14938 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14939 14940 #if defined(__i386) || defined(__amd64) 14941 /* 14942 * Note:x86: check for the "sdrestart failed" case. 14943 */ 14944 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14945 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14946 (xp->xb_pktp->pkt_resid == 0)) { 14947 14948 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14949 /* 14950 * Successfully set up next portion of cmd 14951 * transfer, try sending it 14952 */ 14953 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14954 NULL, NULL, 0, (clock_t)0, NULL); 14955 sd_start_cmds(un, NULL); 14956 return; /* Note:x86: need a return here? */ 14957 } 14958 } 14959 #endif 14960 14961 /* 14962 * If this is the failfast bp, clear it from un_failfast_bp. This 14963 * can happen if upon being re-tried the failfast bp either 14964 * succeeded or encountered another error (possibly even a different 14965 * error than the one that precipitated the failfast state, but in 14966 * that case it would have had to exhaust retries as well). Regardless, 14967 * this should not occur whenever the instance is in the active 14968 * failfast state. 14969 */ 14970 if (bp == un->un_failfast_bp) { 14971 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14972 un->un_failfast_bp = NULL; 14973 } 14974 14975 /* 14976 * Clear the failfast state upon successful completion of ANY cmd. 14977 */ 14978 if (bp->b_error == 0) { 14979 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14980 } 14981 14982 /* 14983 * This is used if the command was retried one or more times. Show that 14984 * we are done with it, and allow processing of the waitq to resume. 14985 */ 14986 if (bp == un->un_retry_bp) { 14987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14988 "sd_return_command: un:0x%p: " 14989 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14990 un->un_retry_bp = NULL; 14991 un->un_retry_statp = NULL; 14992 } 14993 14994 SD_UPDATE_RDWR_STATS(un, bp); 14995 SD_UPDATE_PARTITION_STATS(un, bp); 14996 14997 switch (un->un_state) { 14998 case SD_STATE_SUSPENDED: 14999 /* 15000 * Notify any threads waiting in sd_ddi_suspend() that 15001 * a command completion has occurred. 15002 */ 15003 cv_broadcast(&un->un_disk_busy_cv); 15004 break; 15005 default: 15006 sd_start_cmds(un, NULL); 15007 break; 15008 } 15009 15010 /* Return this command up the iodone chain to its originator. */ 15011 mutex_exit(SD_MUTEX(un)); 15012 15013 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15014 xp->xb_pktp = NULL; 15015 15016 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15017 15018 ASSERT(!mutex_owned(SD_MUTEX(un))); 15019 mutex_enter(SD_MUTEX(un)); 15020 15021 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15022 } 15023 15024 15025 /* 15026 * Function: sd_return_failed_command 15027 * 15028 * Description: Command completion when an error occurred. 15029 * 15030 * Context: May be called from interrupt context 15031 */ 15032 15033 static void 15034 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15035 { 15036 ASSERT(bp != NULL); 15037 ASSERT(un != NULL); 15038 ASSERT(mutex_owned(SD_MUTEX(un))); 15039 15040 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15041 "sd_return_failed_command: entry\n"); 15042 15043 /* 15044 * b_resid could already be nonzero due to a partial data 15045 * transfer, so do not change it here. 15046 */ 15047 SD_BIOERROR(bp, errcode); 15048 15049 sd_return_command(un, bp); 15050 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15051 "sd_return_failed_command: exit\n"); 15052 } 15053 15054 15055 /* 15056 * Function: sd_return_failed_command_no_restart 15057 * 15058 * Description: Same as sd_return_failed_command, but ensures that no 15059 * call back into sd_start_cmds will be issued. 15060 * 15061 * Context: May be called from interrupt context 15062 */ 15063 15064 static void 15065 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15066 int errcode) 15067 { 15068 struct sd_xbuf *xp; 15069 15070 ASSERT(bp != NULL); 15071 ASSERT(un != NULL); 15072 ASSERT(mutex_owned(SD_MUTEX(un))); 15073 xp = SD_GET_XBUF(bp); 15074 ASSERT(xp != NULL); 15075 ASSERT(errcode != 0); 15076 15077 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15078 "sd_return_failed_command_no_restart: entry\n"); 15079 15080 /* 15081 * b_resid could already be nonzero due to a partial data 15082 * transfer, so do not change it here. 15083 */ 15084 SD_BIOERROR(bp, errcode); 15085 15086 /* 15087 * If this is the failfast bp, clear it. This can happen if the 15088 * failfast bp encounterd a fatal error when we attempted to 15089 * re-try it (such as a scsi_transport(9F) failure). However 15090 * we should NOT be in an active failfast state if the failfast 15091 * bp is not NULL. 15092 */ 15093 if (bp == un->un_failfast_bp) { 15094 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15095 un->un_failfast_bp = NULL; 15096 } 15097 15098 if (bp == un->un_retry_bp) { 15099 /* 15100 * This command was retried one or more times. Show that we are 15101 * done with it, and allow processing of the waitq to resume. 15102 */ 15103 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15104 "sd_return_failed_command_no_restart: " 15105 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15106 un->un_retry_bp = NULL; 15107 un->un_retry_statp = NULL; 15108 } 15109 15110 SD_UPDATE_RDWR_STATS(un, bp); 15111 SD_UPDATE_PARTITION_STATS(un, bp); 15112 15113 mutex_exit(SD_MUTEX(un)); 15114 15115 if (xp->xb_pktp != NULL) { 15116 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15117 xp->xb_pktp = NULL; 15118 } 15119 15120 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15121 15122 mutex_enter(SD_MUTEX(un)); 15123 15124 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15125 "sd_return_failed_command_no_restart: exit\n"); 15126 } 15127 15128 15129 /* 15130 * Function: sd_retry_command 15131 * 15132 * Description: queue up a command for retry, or (optionally) fail it 15133 * if retry counts are exhausted. 15134 * 15135 * Arguments: un - Pointer to the sd_lun struct for the target. 15136 * 15137 * bp - Pointer to the buf for the command to be retried. 15138 * 15139 * retry_check_flag - Flag to see which (if any) of the retry 15140 * counts should be decremented/checked. If the indicated 15141 * retry count is exhausted, then the command will not be 15142 * retried; it will be failed instead. This should use a 15143 * value equal to one of the following: 15144 * 15145 * SD_RETRIES_NOCHECK 15146 * SD_RESD_RETRIES_STANDARD 15147 * SD_RETRIES_VICTIM 15148 * 15149 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15150 * if the check should be made to see of FLAG_ISOLATE is set 15151 * in the pkt. If FLAG_ISOLATE is set, then the command is 15152 * not retried, it is simply failed. 15153 * 15154 * user_funcp - Ptr to function to call before dispatching the 15155 * command. May be NULL if no action needs to be performed. 15156 * (Primarily intended for printing messages.) 15157 * 15158 * user_arg - Optional argument to be passed along to 15159 * the user_funcp call. 15160 * 15161 * failure_code - errno return code to set in the bp if the 15162 * command is going to be failed. 15163 * 15164 * retry_delay - Retry delay interval in (clock_t) units. May 15165 * be zero which indicates that the retry should be retried 15166 * immediately (ie, without an intervening delay). 15167 * 15168 * statp - Ptr to kstat function to be updated if the command 15169 * is queued for a delayed retry. May be NULL if no kstat 15170 * update is desired. 15171 * 15172 * Context: May be called from interupt context. 15173 */ 15174 15175 static void 15176 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15177 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15178 code), void *user_arg, int failure_code, clock_t retry_delay, 15179 void (*statp)(kstat_io_t *)) 15180 { 15181 struct sd_xbuf *xp; 15182 struct scsi_pkt *pktp; 15183 15184 ASSERT(un != NULL); 15185 ASSERT(mutex_owned(SD_MUTEX(un))); 15186 ASSERT(bp != NULL); 15187 xp = SD_GET_XBUF(bp); 15188 ASSERT(xp != NULL); 15189 pktp = SD_GET_PKTP(bp); 15190 ASSERT(pktp != NULL); 15191 15192 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15193 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15194 15195 /* 15196 * If we are syncing or dumping, fail the command to avoid 15197 * recursively calling back into scsi_transport(). 15198 */ 15199 if (ddi_in_panic()) { 15200 goto fail_command_no_log; 15201 } 15202 15203 /* 15204 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15205 * log an error and fail the command. 15206 */ 15207 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15208 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15209 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15210 sd_dump_memory(un, SD_LOG_IO, "CDB", 15211 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15212 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15213 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15214 goto fail_command; 15215 } 15216 15217 /* 15218 * If we are suspended, then put the command onto head of the 15219 * wait queue since we don't want to start more commands. 15220 */ 15221 switch (un->un_state) { 15222 case SD_STATE_SUSPENDED: 15223 case SD_STATE_DUMPING: 15224 bp->av_forw = un->un_waitq_headp; 15225 un->un_waitq_headp = bp; 15226 if (un->un_waitq_tailp == NULL) { 15227 un->un_waitq_tailp = bp; 15228 } 15229 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15231 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15232 return; 15233 default: 15234 break; 15235 } 15236 15237 /* 15238 * If the caller wants us to check FLAG_ISOLATE, then see if that 15239 * is set; if it is then we do not want to retry the command. 15240 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15241 */ 15242 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15243 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15244 goto fail_command; 15245 } 15246 } 15247 15248 15249 /* 15250 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15251 * command timeout or a selection timeout has occurred. This means 15252 * that we were unable to establish an kind of communication with 15253 * the target, and subsequent retries and/or commands are likely 15254 * to encounter similar results and take a long time to complete. 15255 * 15256 * If this is a failfast error condition, we need to update the 15257 * failfast state, even if this bp does not have B_FAILFAST set. 15258 */ 15259 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15260 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15261 ASSERT(un->un_failfast_bp == NULL); 15262 /* 15263 * If we are already in the active failfast state, and 15264 * another failfast error condition has been detected, 15265 * then fail this command if it has B_FAILFAST set. 15266 * If B_FAILFAST is clear, then maintain the legacy 15267 * behavior of retrying heroically, even tho this will 15268 * take a lot more time to fail the command. 15269 */ 15270 if (bp->b_flags & B_FAILFAST) { 15271 goto fail_command; 15272 } 15273 } else { 15274 /* 15275 * We're not in the active failfast state, but we 15276 * have a failfast error condition, so we must begin 15277 * transition to the next state. We do this regardless 15278 * of whether or not this bp has B_FAILFAST set. 15279 */ 15280 if (un->un_failfast_bp == NULL) { 15281 /* 15282 * This is the first bp to meet a failfast 15283 * condition so save it on un_failfast_bp & 15284 * do normal retry processing. Do not enter 15285 * active failfast state yet. This marks 15286 * entry into the "failfast pending" state. 15287 */ 15288 un->un_failfast_bp = bp; 15289 15290 } else if (un->un_failfast_bp == bp) { 15291 /* 15292 * This is the second time *this* bp has 15293 * encountered a failfast error condition, 15294 * so enter active failfast state & flush 15295 * queues as appropriate. 15296 */ 15297 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15298 un->un_failfast_bp = NULL; 15299 sd_failfast_flushq(un); 15300 15301 /* 15302 * Fail this bp now if B_FAILFAST set; 15303 * otherwise continue with retries. (It would 15304 * be pretty ironic if this bp succeeded on a 15305 * subsequent retry after we just flushed all 15306 * the queues). 15307 */ 15308 if (bp->b_flags & B_FAILFAST) { 15309 goto fail_command; 15310 } 15311 15312 #if !defined(lint) && !defined(__lint) 15313 } else { 15314 /* 15315 * If neither of the preceeding conditionals 15316 * was true, it means that there is some 15317 * *other* bp that has met an inital failfast 15318 * condition and is currently either being 15319 * retried or is waiting to be retried. In 15320 * that case we should perform normal retry 15321 * processing on *this* bp, since there is a 15322 * chance that the current failfast condition 15323 * is transient and recoverable. If that does 15324 * not turn out to be the case, then retries 15325 * will be cleared when the wait queue is 15326 * flushed anyway. 15327 */ 15328 #endif 15329 } 15330 } 15331 } else { 15332 /* 15333 * SD_RETRIES_FAILFAST is clear, which indicates that we 15334 * likely were able to at least establish some level of 15335 * communication with the target and subsequent commands 15336 * and/or retries are likely to get through to the target, 15337 * In this case we want to be aggressive about clearing 15338 * the failfast state. Note that this does not affect 15339 * the "failfast pending" condition. 15340 */ 15341 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15342 } 15343 15344 15345 /* 15346 * Check the specified retry count to see if we can still do 15347 * any retries with this pkt before we should fail it. 15348 */ 15349 switch (retry_check_flag & SD_RETRIES_MASK) { 15350 case SD_RETRIES_VICTIM: 15351 /* 15352 * Check the victim retry count. If exhausted, then fall 15353 * thru & check against the standard retry count. 15354 */ 15355 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15356 /* Increment count & proceed with the retry */ 15357 xp->xb_victim_retry_count++; 15358 break; 15359 } 15360 /* Victim retries exhausted, fall back to std. retries... */ 15361 /* FALLTHRU */ 15362 15363 case SD_RETRIES_STANDARD: 15364 if (xp->xb_retry_count >= un->un_retry_count) { 15365 /* Retries exhausted, fail the command */ 15366 SD_TRACE(SD_LOG_IO_CORE, un, 15367 "sd_retry_command: retries exhausted!\n"); 15368 /* 15369 * update b_resid for failed SCMD_READ & SCMD_WRITE 15370 * commands with nonzero pkt_resid. 15371 */ 15372 if ((pktp->pkt_reason == CMD_CMPLT) && 15373 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15374 (pktp->pkt_resid != 0)) { 15375 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15376 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15377 SD_UPDATE_B_RESID(bp, pktp); 15378 } 15379 } 15380 goto fail_command; 15381 } 15382 xp->xb_retry_count++; 15383 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15384 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15385 break; 15386 15387 case SD_RETRIES_UA: 15388 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15389 /* Retries exhausted, fail the command */ 15390 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15391 "Unit Attention retries exhausted. " 15392 "Check the target.\n"); 15393 goto fail_command; 15394 } 15395 xp->xb_ua_retry_count++; 15396 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15397 "sd_retry_command: retry count:%d\n", 15398 xp->xb_ua_retry_count); 15399 break; 15400 15401 case SD_RETRIES_BUSY: 15402 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15403 /* Retries exhausted, fail the command */ 15404 SD_TRACE(SD_LOG_IO_CORE, un, 15405 "sd_retry_command: retries exhausted!\n"); 15406 goto fail_command; 15407 } 15408 xp->xb_retry_count++; 15409 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15410 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15411 break; 15412 15413 case SD_RETRIES_NOCHECK: 15414 default: 15415 /* No retry count to check. Just proceed with the retry */ 15416 break; 15417 } 15418 15419 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15420 15421 /* 15422 * If we were given a zero timeout, we must attempt to retry the 15423 * command immediately (ie, without a delay). 15424 */ 15425 if (retry_delay == 0) { 15426 /* 15427 * Check some limiting conditions to see if we can actually 15428 * do the immediate retry. If we cannot, then we must 15429 * fall back to queueing up a delayed retry. 15430 */ 15431 if (un->un_ncmds_in_transport >= un->un_throttle) { 15432 /* 15433 * We are at the throttle limit for the target, 15434 * fall back to delayed retry. 15435 */ 15436 retry_delay = SD_BSY_TIMEOUT; 15437 statp = kstat_waitq_enter; 15438 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15439 "sd_retry_command: immed. retry hit " 15440 "throttle!\n"); 15441 } else { 15442 /* 15443 * We're clear to proceed with the immediate retry. 15444 * First call the user-provided function (if any) 15445 */ 15446 if (user_funcp != NULL) { 15447 (*user_funcp)(un, bp, user_arg, 15448 SD_IMMEDIATE_RETRY_ISSUED); 15449 #ifdef __lock_lint 15450 sd_print_incomplete_msg(un, bp, user_arg, 15451 SD_IMMEDIATE_RETRY_ISSUED); 15452 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15453 SD_IMMEDIATE_RETRY_ISSUED); 15454 sd_print_sense_failed_msg(un, bp, user_arg, 15455 SD_IMMEDIATE_RETRY_ISSUED); 15456 #endif 15457 } 15458 15459 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15460 "sd_retry_command: issuing immediate retry\n"); 15461 15462 /* 15463 * Call sd_start_cmds() to transport the command to 15464 * the target. 15465 */ 15466 sd_start_cmds(un, bp); 15467 15468 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15469 "sd_retry_command exit\n"); 15470 return; 15471 } 15472 } 15473 15474 /* 15475 * Set up to retry the command after a delay. 15476 * First call the user-provided function (if any) 15477 */ 15478 if (user_funcp != NULL) { 15479 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15480 } 15481 15482 sd_set_retry_bp(un, bp, retry_delay, statp); 15483 15484 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15485 return; 15486 15487 fail_command: 15488 15489 if (user_funcp != NULL) { 15490 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15491 } 15492 15493 fail_command_no_log: 15494 15495 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15496 "sd_retry_command: returning failed command\n"); 15497 15498 sd_return_failed_command(un, bp, failure_code); 15499 15500 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15501 } 15502 15503 15504 /* 15505 * Function: sd_set_retry_bp 15506 * 15507 * Description: Set up the given bp for retry. 15508 * 15509 * Arguments: un - ptr to associated softstate 15510 * bp - ptr to buf(9S) for the command 15511 * retry_delay - time interval before issuing retry (may be 0) 15512 * statp - optional pointer to kstat function 15513 * 15514 * Context: May be called under interrupt context 15515 */ 15516 15517 static void 15518 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15519 void (*statp)(kstat_io_t *)) 15520 { 15521 ASSERT(un != NULL); 15522 ASSERT(mutex_owned(SD_MUTEX(un))); 15523 ASSERT(bp != NULL); 15524 15525 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15526 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15527 15528 /* 15529 * Indicate that the command is being retried. This will not allow any 15530 * other commands on the wait queue to be transported to the target 15531 * until this command has been completed (success or failure). The 15532 * "retry command" is not transported to the target until the given 15533 * time delay expires, unless the user specified a 0 retry_delay. 15534 * 15535 * Note: the timeout(9F) callback routine is what actually calls 15536 * sd_start_cmds() to transport the command, with the exception of a 15537 * zero retry_delay. The only current implementor of a zero retry delay 15538 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15539 */ 15540 if (un->un_retry_bp == NULL) { 15541 ASSERT(un->un_retry_statp == NULL); 15542 un->un_retry_bp = bp; 15543 15544 /* 15545 * If the user has not specified a delay the command should 15546 * be queued and no timeout should be scheduled. 15547 */ 15548 if (retry_delay == 0) { 15549 /* 15550 * Save the kstat pointer that will be used in the 15551 * call to SD_UPDATE_KSTATS() below, so that 15552 * sd_start_cmds() can correctly decrement the waitq 15553 * count when it is time to transport this command. 15554 */ 15555 un->un_retry_statp = statp; 15556 goto done; 15557 } 15558 } 15559 15560 if (un->un_retry_bp == bp) { 15561 /* 15562 * Save the kstat pointer that will be used in the call to 15563 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15564 * correctly decrement the waitq count when it is time to 15565 * transport this command. 15566 */ 15567 un->un_retry_statp = statp; 15568 15569 /* 15570 * Schedule a timeout if: 15571 * 1) The user has specified a delay. 15572 * 2) There is not a START_STOP_UNIT callback pending. 15573 * 15574 * If no delay has been specified, then it is up to the caller 15575 * to ensure that IO processing continues without stalling. 15576 * Effectively, this means that the caller will issue the 15577 * required call to sd_start_cmds(). The START_STOP_UNIT 15578 * callback does this after the START STOP UNIT command has 15579 * completed. In either of these cases we should not schedule 15580 * a timeout callback here. Also don't schedule the timeout if 15581 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15582 */ 15583 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15584 (un->un_direct_priority_timeid == NULL)) { 15585 un->un_retry_timeid = 15586 timeout(sd_start_retry_command, un, retry_delay); 15587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15588 "sd_set_retry_bp: setting timeout: un: 0x%p" 15589 " bp:0x%p un_retry_timeid:0x%p\n", 15590 un, bp, un->un_retry_timeid); 15591 } 15592 } else { 15593 /* 15594 * We only get in here if there is already another command 15595 * waiting to be retried. In this case, we just put the 15596 * given command onto the wait queue, so it can be transported 15597 * after the current retry command has completed. 15598 * 15599 * Also we have to make sure that if the command at the head 15600 * of the wait queue is the un_failfast_bp, that we do not 15601 * put ahead of it any other commands that are to be retried. 15602 */ 15603 if ((un->un_failfast_bp != NULL) && 15604 (un->un_failfast_bp == un->un_waitq_headp)) { 15605 /* 15606 * Enqueue this command AFTER the first command on 15607 * the wait queue (which is also un_failfast_bp). 15608 */ 15609 bp->av_forw = un->un_waitq_headp->av_forw; 15610 un->un_waitq_headp->av_forw = bp; 15611 if (un->un_waitq_headp == un->un_waitq_tailp) { 15612 un->un_waitq_tailp = bp; 15613 } 15614 } else { 15615 /* Enqueue this command at the head of the waitq. */ 15616 bp->av_forw = un->un_waitq_headp; 15617 un->un_waitq_headp = bp; 15618 if (un->un_waitq_tailp == NULL) { 15619 un->un_waitq_tailp = bp; 15620 } 15621 } 15622 15623 if (statp == NULL) { 15624 statp = kstat_waitq_enter; 15625 } 15626 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15627 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15628 } 15629 15630 done: 15631 if (statp != NULL) { 15632 SD_UPDATE_KSTATS(un, statp, bp); 15633 } 15634 15635 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15636 "sd_set_retry_bp: exit un:0x%p\n", un); 15637 } 15638 15639 15640 /* 15641 * Function: sd_start_retry_command 15642 * 15643 * Description: Start the command that has been waiting on the target's 15644 * retry queue. Called from timeout(9F) context after the 15645 * retry delay interval has expired. 15646 * 15647 * Arguments: arg - pointer to associated softstate for the device. 15648 * 15649 * Context: timeout(9F) thread context. May not sleep. 15650 */ 15651 15652 static void 15653 sd_start_retry_command(void *arg) 15654 { 15655 struct sd_lun *un = arg; 15656 15657 ASSERT(un != NULL); 15658 ASSERT(!mutex_owned(SD_MUTEX(un))); 15659 15660 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15661 "sd_start_retry_command: entry\n"); 15662 15663 mutex_enter(SD_MUTEX(un)); 15664 15665 un->un_retry_timeid = NULL; 15666 15667 if (un->un_retry_bp != NULL) { 15668 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15669 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15670 un, un->un_retry_bp); 15671 sd_start_cmds(un, un->un_retry_bp); 15672 } 15673 15674 mutex_exit(SD_MUTEX(un)); 15675 15676 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15677 "sd_start_retry_command: exit\n"); 15678 } 15679 15680 15681 /* 15682 * Function: sd_start_direct_priority_command 15683 * 15684 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15685 * received TRAN_BUSY when we called scsi_transport() to send it 15686 * to the underlying HBA. This function is called from timeout(9F) 15687 * context after the delay interval has expired. 15688 * 15689 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15690 * 15691 * Context: timeout(9F) thread context. May not sleep. 15692 */ 15693 15694 static void 15695 sd_start_direct_priority_command(void *arg) 15696 { 15697 struct buf *priority_bp = arg; 15698 struct sd_lun *un; 15699 15700 ASSERT(priority_bp != NULL); 15701 un = SD_GET_UN(priority_bp); 15702 ASSERT(un != NULL); 15703 ASSERT(!mutex_owned(SD_MUTEX(un))); 15704 15705 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15706 "sd_start_direct_priority_command: entry\n"); 15707 15708 mutex_enter(SD_MUTEX(un)); 15709 un->un_direct_priority_timeid = NULL; 15710 sd_start_cmds(un, priority_bp); 15711 mutex_exit(SD_MUTEX(un)); 15712 15713 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15714 "sd_start_direct_priority_command: exit\n"); 15715 } 15716 15717 15718 /* 15719 * Function: sd_send_request_sense_command 15720 * 15721 * Description: Sends a REQUEST SENSE command to the target 15722 * 15723 * Context: May be called from interrupt context. 15724 */ 15725 15726 static void 15727 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15728 struct scsi_pkt *pktp) 15729 { 15730 ASSERT(bp != NULL); 15731 ASSERT(un != NULL); 15732 ASSERT(mutex_owned(SD_MUTEX(un))); 15733 15734 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15735 "entry: buf:0x%p\n", bp); 15736 15737 /* 15738 * If we are syncing or dumping, then fail the command to avoid a 15739 * recursive callback into scsi_transport(). Also fail the command 15740 * if we are suspended (legacy behavior). 15741 */ 15742 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15743 (un->un_state == SD_STATE_DUMPING)) { 15744 sd_return_failed_command(un, bp, EIO); 15745 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15746 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15747 return; 15748 } 15749 15750 /* 15751 * Retry the failed command and don't issue the request sense if: 15752 * 1) the sense buf is busy 15753 * 2) we have 1 or more outstanding commands on the target 15754 * (the sense data will be cleared or invalidated any way) 15755 * 15756 * Note: There could be an issue with not checking a retry limit here, 15757 * the problem is determining which retry limit to check. 15758 */ 15759 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15760 /* Don't retry if the command is flagged as non-retryable */ 15761 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15762 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15763 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15764 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15765 "sd_send_request_sense_command: " 15766 "at full throttle, retrying exit\n"); 15767 } else { 15768 sd_return_failed_command(un, bp, EIO); 15769 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15770 "sd_send_request_sense_command: " 15771 "at full throttle, non-retryable exit\n"); 15772 } 15773 return; 15774 } 15775 15776 sd_mark_rqs_busy(un, bp); 15777 sd_start_cmds(un, un->un_rqs_bp); 15778 15779 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15780 "sd_send_request_sense_command: exit\n"); 15781 } 15782 15783 15784 /* 15785 * Function: sd_mark_rqs_busy 15786 * 15787 * Description: Indicate that the request sense bp for this instance is 15788 * in use. 15789 * 15790 * Context: May be called under interrupt context 15791 */ 15792 15793 static void 15794 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15795 { 15796 struct sd_xbuf *sense_xp; 15797 15798 ASSERT(un != NULL); 15799 ASSERT(bp != NULL); 15800 ASSERT(mutex_owned(SD_MUTEX(un))); 15801 ASSERT(un->un_sense_isbusy == 0); 15802 15803 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15804 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15805 15806 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15807 ASSERT(sense_xp != NULL); 15808 15809 SD_INFO(SD_LOG_IO, un, 15810 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15811 15812 ASSERT(sense_xp->xb_pktp != NULL); 15813 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15814 == (FLAG_SENSING | FLAG_HEAD)); 15815 15816 un->un_sense_isbusy = 1; 15817 un->un_rqs_bp->b_resid = 0; 15818 sense_xp->xb_pktp->pkt_resid = 0; 15819 sense_xp->xb_pktp->pkt_reason = 0; 15820 15821 /* So we can get back the bp at interrupt time! */ 15822 sense_xp->xb_sense_bp = bp; 15823 15824 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15825 15826 /* 15827 * Mark this buf as awaiting sense data. (This is already set in 15828 * the pkt_flags for the RQS packet.) 15829 */ 15830 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15831 15832 sense_xp->xb_retry_count = 0; 15833 sense_xp->xb_victim_retry_count = 0; 15834 sense_xp->xb_ua_retry_count = 0; 15835 sense_xp->xb_dma_resid = 0; 15836 15837 /* Clean up the fields for auto-request sense */ 15838 sense_xp->xb_sense_status = 0; 15839 sense_xp->xb_sense_state = 0; 15840 sense_xp->xb_sense_resid = 0; 15841 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15842 15843 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15844 } 15845 15846 15847 /* 15848 * Function: sd_mark_rqs_idle 15849 * 15850 * Description: SD_MUTEX must be held continuously through this routine 15851 * to prevent reuse of the rqs struct before the caller can 15852 * complete it's processing. 15853 * 15854 * Return Code: Pointer to the RQS buf 15855 * 15856 * Context: May be called under interrupt context 15857 */ 15858 15859 static struct buf * 15860 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15861 { 15862 struct buf *bp; 15863 ASSERT(un != NULL); 15864 ASSERT(sense_xp != NULL); 15865 ASSERT(mutex_owned(SD_MUTEX(un))); 15866 ASSERT(un->un_sense_isbusy != 0); 15867 15868 un->un_sense_isbusy = 0; 15869 bp = sense_xp->xb_sense_bp; 15870 sense_xp->xb_sense_bp = NULL; 15871 15872 /* This pkt is no longer interested in getting sense data */ 15873 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15874 15875 return (bp); 15876 } 15877 15878 15879 15880 /* 15881 * Function: sd_alloc_rqs 15882 * 15883 * Description: Set up the unit to receive auto request sense data 15884 * 15885 * Return Code: DDI_SUCCESS or DDI_FAILURE 15886 * 15887 * Context: Called under attach(9E) context 15888 */ 15889 15890 static int 15891 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15892 { 15893 struct sd_xbuf *xp; 15894 15895 ASSERT(un != NULL); 15896 ASSERT(!mutex_owned(SD_MUTEX(un))); 15897 ASSERT(un->un_rqs_bp == NULL); 15898 ASSERT(un->un_rqs_pktp == NULL); 15899 15900 /* 15901 * First allocate the required buf and scsi_pkt structs, then set up 15902 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15903 */ 15904 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15905 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15906 if (un->un_rqs_bp == NULL) { 15907 return (DDI_FAILURE); 15908 } 15909 15910 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15911 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15912 15913 if (un->un_rqs_pktp == NULL) { 15914 sd_free_rqs(un); 15915 return (DDI_FAILURE); 15916 } 15917 15918 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15919 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15920 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15921 15922 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15923 15924 /* Set up the other needed members in the ARQ scsi_pkt. */ 15925 un->un_rqs_pktp->pkt_comp = sdintr; 15926 un->un_rqs_pktp->pkt_time = sd_io_time; 15927 un->un_rqs_pktp->pkt_flags |= 15928 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15929 15930 /* 15931 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15932 * provide any intpkt, destroypkt routines as we take care of 15933 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15934 */ 15935 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15936 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15937 xp->xb_pktp = un->un_rqs_pktp; 15938 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15939 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15940 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15941 15942 /* 15943 * Save the pointer to the request sense private bp so it can 15944 * be retrieved in sdintr. 15945 */ 15946 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15947 ASSERT(un->un_rqs_bp->b_private == xp); 15948 15949 /* 15950 * See if the HBA supports auto-request sense for the specified 15951 * target/lun. If it does, then try to enable it (if not already 15952 * enabled). 15953 * 15954 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15955 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15956 * return success. However, in both of these cases ARQ is always 15957 * enabled and scsi_ifgetcap will always return true. The best approach 15958 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15959 * 15960 * The 3rd case is the HBA (adp) always return enabled on 15961 * scsi_ifgetgetcap even when it's not enable, the best approach 15962 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15963 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15964 */ 15965 15966 if (un->un_f_is_fibre == TRUE) { 15967 un->un_f_arq_enabled = TRUE; 15968 } else { 15969 #if defined(__i386) || defined(__amd64) 15970 /* 15971 * Circumvent the Adaptec bug, remove this code when 15972 * the bug is fixed 15973 */ 15974 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15975 #endif 15976 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15977 case 0: 15978 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15979 "sd_alloc_rqs: HBA supports ARQ\n"); 15980 /* 15981 * ARQ is supported by this HBA but currently is not 15982 * enabled. Attempt to enable it and if successful then 15983 * mark this instance as ARQ enabled. 15984 */ 15985 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15986 == 1) { 15987 /* Successfully enabled ARQ in the HBA */ 15988 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15989 "sd_alloc_rqs: ARQ enabled\n"); 15990 un->un_f_arq_enabled = TRUE; 15991 } else { 15992 /* Could not enable ARQ in the HBA */ 15993 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15994 "sd_alloc_rqs: failed ARQ enable\n"); 15995 un->un_f_arq_enabled = FALSE; 15996 } 15997 break; 15998 case 1: 15999 /* 16000 * ARQ is supported by this HBA and is already enabled. 16001 * Just mark ARQ as enabled for this instance. 16002 */ 16003 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16004 "sd_alloc_rqs: ARQ already enabled\n"); 16005 un->un_f_arq_enabled = TRUE; 16006 break; 16007 default: 16008 /* 16009 * ARQ is not supported by this HBA; disable it for this 16010 * instance. 16011 */ 16012 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16013 "sd_alloc_rqs: HBA does not support ARQ\n"); 16014 un->un_f_arq_enabled = FALSE; 16015 break; 16016 } 16017 } 16018 16019 return (DDI_SUCCESS); 16020 } 16021 16022 16023 /* 16024 * Function: sd_free_rqs 16025 * 16026 * Description: Cleanup for the pre-instance RQS command. 16027 * 16028 * Context: Kernel thread context 16029 */ 16030 16031 static void 16032 sd_free_rqs(struct sd_lun *un) 16033 { 16034 ASSERT(un != NULL); 16035 16036 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16037 16038 /* 16039 * If consistent memory is bound to a scsi_pkt, the pkt 16040 * has to be destroyed *before* freeing the consistent memory. 16041 * Don't change the sequence of this operations. 16042 * scsi_destroy_pkt() might access memory, which isn't allowed, 16043 * after it was freed in scsi_free_consistent_buf(). 16044 */ 16045 if (un->un_rqs_pktp != NULL) { 16046 scsi_destroy_pkt(un->un_rqs_pktp); 16047 un->un_rqs_pktp = NULL; 16048 } 16049 16050 if (un->un_rqs_bp != NULL) { 16051 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16052 scsi_free_consistent_buf(un->un_rqs_bp); 16053 un->un_rqs_bp = NULL; 16054 } 16055 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16056 } 16057 16058 16059 16060 /* 16061 * Function: sd_reduce_throttle 16062 * 16063 * Description: Reduces the maximun # of outstanding commands on a 16064 * target to the current number of outstanding commands. 16065 * Queues a tiemout(9F) callback to restore the limit 16066 * after a specified interval has elapsed. 16067 * Typically used when we get a TRAN_BUSY return code 16068 * back from scsi_transport(). 16069 * 16070 * Arguments: un - ptr to the sd_lun softstate struct 16071 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16072 * 16073 * Context: May be called from interrupt context 16074 */ 16075 16076 static void 16077 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16078 { 16079 ASSERT(un != NULL); 16080 ASSERT(mutex_owned(SD_MUTEX(un))); 16081 ASSERT(un->un_ncmds_in_transport >= 0); 16082 16083 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16084 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16085 un, un->un_throttle, un->un_ncmds_in_transport); 16086 16087 if (un->un_throttle > 1) { 16088 if (un->un_f_use_adaptive_throttle == TRUE) { 16089 switch (throttle_type) { 16090 case SD_THROTTLE_TRAN_BUSY: 16091 if (un->un_busy_throttle == 0) { 16092 un->un_busy_throttle = un->un_throttle; 16093 } 16094 break; 16095 case SD_THROTTLE_QFULL: 16096 un->un_busy_throttle = 0; 16097 break; 16098 default: 16099 ASSERT(FALSE); 16100 } 16101 16102 if (un->un_ncmds_in_transport > 0) { 16103 un->un_throttle = un->un_ncmds_in_transport; 16104 } 16105 16106 } else { 16107 if (un->un_ncmds_in_transport == 0) { 16108 un->un_throttle = 1; 16109 } else { 16110 un->un_throttle = un->un_ncmds_in_transport; 16111 } 16112 } 16113 } 16114 16115 /* Reschedule the timeout if none is currently active */ 16116 if (un->un_reset_throttle_timeid == NULL) { 16117 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16118 un, SD_THROTTLE_RESET_INTERVAL); 16119 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16120 "sd_reduce_throttle: timeout scheduled!\n"); 16121 } 16122 16123 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16124 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16125 } 16126 16127 16128 16129 /* 16130 * Function: sd_restore_throttle 16131 * 16132 * Description: Callback function for timeout(9F). Resets the current 16133 * value of un->un_throttle to its default. 16134 * 16135 * Arguments: arg - pointer to associated softstate for the device. 16136 * 16137 * Context: May be called from interrupt context 16138 */ 16139 16140 static void 16141 sd_restore_throttle(void *arg) 16142 { 16143 struct sd_lun *un = arg; 16144 16145 ASSERT(un != NULL); 16146 ASSERT(!mutex_owned(SD_MUTEX(un))); 16147 16148 mutex_enter(SD_MUTEX(un)); 16149 16150 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16151 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16152 16153 un->un_reset_throttle_timeid = NULL; 16154 16155 if (un->un_f_use_adaptive_throttle == TRUE) { 16156 /* 16157 * If un_busy_throttle is nonzero, then it contains the 16158 * value that un_throttle was when we got a TRAN_BUSY back 16159 * from scsi_transport(). We want to revert back to this 16160 * value. 16161 * 16162 * In the QFULL case, the throttle limit will incrementally 16163 * increase until it reaches max throttle. 16164 */ 16165 if (un->un_busy_throttle > 0) { 16166 un->un_throttle = un->un_busy_throttle; 16167 un->un_busy_throttle = 0; 16168 } else { 16169 /* 16170 * increase throttle by 10% open gate slowly, schedule 16171 * another restore if saved throttle has not been 16172 * reached 16173 */ 16174 short throttle; 16175 if (sd_qfull_throttle_enable) { 16176 throttle = un->un_throttle + 16177 max((un->un_throttle / 10), 1); 16178 un->un_throttle = 16179 (throttle < un->un_saved_throttle) ? 16180 throttle : un->un_saved_throttle; 16181 if (un->un_throttle < un->un_saved_throttle) { 16182 un->un_reset_throttle_timeid = 16183 timeout(sd_restore_throttle, 16184 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16185 } 16186 } 16187 } 16188 16189 /* 16190 * If un_throttle has fallen below the low-water mark, we 16191 * restore the maximum value here (and allow it to ratchet 16192 * down again if necessary). 16193 */ 16194 if (un->un_throttle < un->un_min_throttle) { 16195 un->un_throttle = un->un_saved_throttle; 16196 } 16197 } else { 16198 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16199 "restoring limit from 0x%x to 0x%x\n", 16200 un->un_throttle, un->un_saved_throttle); 16201 un->un_throttle = un->un_saved_throttle; 16202 } 16203 16204 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16205 "sd_restore_throttle: calling sd_start_cmds!\n"); 16206 16207 sd_start_cmds(un, NULL); 16208 16209 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16210 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16211 un, un->un_throttle); 16212 16213 mutex_exit(SD_MUTEX(un)); 16214 16215 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16216 } 16217 16218 /* 16219 * Function: sdrunout 16220 * 16221 * Description: Callback routine for scsi_init_pkt when a resource allocation 16222 * fails. 16223 * 16224 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16225 * soft state instance. 16226 * 16227 * Return Code: The scsi_init_pkt routine allows for the callback function to 16228 * return a 0 indicating the callback should be rescheduled or a 1 16229 * indicating not to reschedule. This routine always returns 1 16230 * because the driver always provides a callback function to 16231 * scsi_init_pkt. This results in a callback always being scheduled 16232 * (via the scsi_init_pkt callback implementation) if a resource 16233 * failure occurs. 16234 * 16235 * Context: This callback function may not block or call routines that block 16236 * 16237 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16238 * request persisting at the head of the list which cannot be 16239 * satisfied even after multiple retries. In the future the driver 16240 * may implement some time of maximum runout count before failing 16241 * an I/O. 16242 */ 16243 16244 static int 16245 sdrunout(caddr_t arg) 16246 { 16247 struct sd_lun *un = (struct sd_lun *)arg; 16248 16249 ASSERT(un != NULL); 16250 ASSERT(!mutex_owned(SD_MUTEX(un))); 16251 16252 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16253 16254 mutex_enter(SD_MUTEX(un)); 16255 sd_start_cmds(un, NULL); 16256 mutex_exit(SD_MUTEX(un)); 16257 /* 16258 * This callback routine always returns 1 (i.e. do not reschedule) 16259 * because we always specify sdrunout as the callback handler for 16260 * scsi_init_pkt inside the call to sd_start_cmds. 16261 */ 16262 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16263 return (1); 16264 } 16265 16266 16267 /* 16268 * Function: sdintr 16269 * 16270 * Description: Completion callback routine for scsi_pkt(9S) structs 16271 * sent to the HBA driver via scsi_transport(9F). 16272 * 16273 * Context: Interrupt context 16274 */ 16275 16276 static void 16277 sdintr(struct scsi_pkt *pktp) 16278 { 16279 struct buf *bp; 16280 struct sd_xbuf *xp; 16281 struct sd_lun *un; 16282 16283 ASSERT(pktp != NULL); 16284 bp = (struct buf *)pktp->pkt_private; 16285 ASSERT(bp != NULL); 16286 xp = SD_GET_XBUF(bp); 16287 ASSERT(xp != NULL); 16288 ASSERT(xp->xb_pktp != NULL); 16289 un = SD_GET_UN(bp); 16290 ASSERT(un != NULL); 16291 ASSERT(!mutex_owned(SD_MUTEX(un))); 16292 16293 #ifdef SD_FAULT_INJECTION 16294 16295 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16296 /* SD FaultInjection */ 16297 sd_faultinjection(pktp); 16298 16299 #endif /* SD_FAULT_INJECTION */ 16300 16301 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16302 " xp:0x%p, un:0x%p\n", bp, xp, un); 16303 16304 mutex_enter(SD_MUTEX(un)); 16305 16306 /* Reduce the count of the #commands currently in transport */ 16307 un->un_ncmds_in_transport--; 16308 ASSERT(un->un_ncmds_in_transport >= 0); 16309 16310 /* Increment counter to indicate that the callback routine is active */ 16311 un->un_in_callback++; 16312 16313 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16314 16315 #ifdef SDDEBUG 16316 if (bp == un->un_retry_bp) { 16317 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16318 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16319 un, un->un_retry_bp, un->un_ncmds_in_transport); 16320 } 16321 #endif 16322 16323 /* 16324 * If pkt_reason is CMD_DEV_GONE, just fail the command 16325 */ 16326 if (pktp->pkt_reason == CMD_DEV_GONE) { 16327 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16328 "Device is gone\n"); 16329 sd_return_failed_command(un, bp, EIO); 16330 goto exit; 16331 } 16332 16333 /* 16334 * First see if the pkt has auto-request sense data with it.... 16335 * Look at the packet state first so we don't take a performance 16336 * hit looking at the arq enabled flag unless absolutely necessary. 16337 */ 16338 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16339 (un->un_f_arq_enabled == TRUE)) { 16340 /* 16341 * The HBA did an auto request sense for this command so check 16342 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16343 * driver command that should not be retried. 16344 */ 16345 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16346 /* 16347 * Save the relevant sense info into the xp for the 16348 * original cmd. 16349 */ 16350 struct scsi_arq_status *asp; 16351 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16352 xp->xb_sense_status = 16353 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16354 xp->xb_sense_state = asp->sts_rqpkt_state; 16355 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16356 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16357 min(sizeof (struct scsi_extended_sense), 16358 SENSE_LENGTH)); 16359 16360 /* fail the command */ 16361 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16362 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16363 sd_return_failed_command(un, bp, EIO); 16364 goto exit; 16365 } 16366 16367 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16368 /* 16369 * We want to either retry or fail this command, so free 16370 * the DMA resources here. If we retry the command then 16371 * the DMA resources will be reallocated in sd_start_cmds(). 16372 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16373 * causes the *entire* transfer to start over again from the 16374 * beginning of the request, even for PARTIAL chunks that 16375 * have already transferred successfully. 16376 */ 16377 if ((un->un_f_is_fibre == TRUE) && 16378 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16379 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16380 scsi_dmafree(pktp); 16381 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16382 } 16383 #endif 16384 16385 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16386 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16387 16388 sd_handle_auto_request_sense(un, bp, xp, pktp); 16389 goto exit; 16390 } 16391 16392 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16393 if (pktp->pkt_flags & FLAG_SENSING) { 16394 /* This pktp is from the unit's REQUEST_SENSE command */ 16395 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16396 "sdintr: sd_handle_request_sense\n"); 16397 sd_handle_request_sense(un, bp, xp, pktp); 16398 goto exit; 16399 } 16400 16401 /* 16402 * Check to see if the command successfully completed as requested; 16403 * this is the most common case (and also the hot performance path). 16404 * 16405 * Requirements for successful completion are: 16406 * pkt_reason is CMD_CMPLT and packet status is status good. 16407 * In addition: 16408 * - A residual of zero indicates successful completion no matter what 16409 * the command is. 16410 * - If the residual is not zero and the command is not a read or 16411 * write, then it's still defined as successful completion. In other 16412 * words, if the command is a read or write the residual must be 16413 * zero for successful completion. 16414 * - If the residual is not zero and the command is a read or 16415 * write, and it's a USCSICMD, then it's still defined as 16416 * successful completion. 16417 */ 16418 if ((pktp->pkt_reason == CMD_CMPLT) && 16419 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16420 16421 /* 16422 * Since this command is returned with a good status, we 16423 * can reset the count for Sonoma failover. 16424 */ 16425 un->un_sonoma_failure_count = 0; 16426 16427 /* 16428 * Return all USCSI commands on good status 16429 */ 16430 if (pktp->pkt_resid == 0) { 16431 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16432 "sdintr: returning command for resid == 0\n"); 16433 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16434 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16435 SD_UPDATE_B_RESID(bp, pktp); 16436 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16437 "sdintr: returning command for resid != 0\n"); 16438 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16439 SD_UPDATE_B_RESID(bp, pktp); 16440 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16441 "sdintr: returning uscsi command\n"); 16442 } else { 16443 goto not_successful; 16444 } 16445 sd_return_command(un, bp); 16446 16447 /* 16448 * Decrement counter to indicate that the callback routine 16449 * is done. 16450 */ 16451 un->un_in_callback--; 16452 ASSERT(un->un_in_callback >= 0); 16453 mutex_exit(SD_MUTEX(un)); 16454 16455 return; 16456 } 16457 16458 not_successful: 16459 16460 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16461 /* 16462 * The following is based upon knowledge of the underlying transport 16463 * and its use of DMA resources. This code should be removed when 16464 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16465 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16466 * and sd_start_cmds(). 16467 * 16468 * Free any DMA resources associated with this command if there 16469 * is a chance it could be retried or enqueued for later retry. 16470 * If we keep the DMA binding then mpxio cannot reissue the 16471 * command on another path whenever a path failure occurs. 16472 * 16473 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16474 * causes the *entire* transfer to start over again from the 16475 * beginning of the request, even for PARTIAL chunks that 16476 * have already transferred successfully. 16477 * 16478 * This is only done for non-uscsi commands (and also skipped for the 16479 * driver's internal RQS command). Also just do this for Fibre Channel 16480 * devices as these are the only ones that support mpxio. 16481 */ 16482 if ((un->un_f_is_fibre == TRUE) && 16483 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16484 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16485 scsi_dmafree(pktp); 16486 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16487 } 16488 #endif 16489 16490 /* 16491 * The command did not successfully complete as requested so check 16492 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16493 * driver command that should not be retried so just return. If 16494 * FLAG_DIAGNOSE is not set the error will be processed below. 16495 */ 16496 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16497 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16498 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16499 /* 16500 * Issue a request sense if a check condition caused the error 16501 * (we handle the auto request sense case above), otherwise 16502 * just fail the command. 16503 */ 16504 if ((pktp->pkt_reason == CMD_CMPLT) && 16505 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16506 sd_send_request_sense_command(un, bp, pktp); 16507 } else { 16508 sd_return_failed_command(un, bp, EIO); 16509 } 16510 goto exit; 16511 } 16512 16513 /* 16514 * The command did not successfully complete as requested so process 16515 * the error, retry, and/or attempt recovery. 16516 */ 16517 switch (pktp->pkt_reason) { 16518 case CMD_CMPLT: 16519 switch (SD_GET_PKT_STATUS(pktp)) { 16520 case STATUS_GOOD: 16521 /* 16522 * The command completed successfully with a non-zero 16523 * residual 16524 */ 16525 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16526 "sdintr: STATUS_GOOD \n"); 16527 sd_pkt_status_good(un, bp, xp, pktp); 16528 break; 16529 16530 case STATUS_CHECK: 16531 case STATUS_TERMINATED: 16532 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16533 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16534 sd_pkt_status_check_condition(un, bp, xp, pktp); 16535 break; 16536 16537 case STATUS_BUSY: 16538 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16539 "sdintr: STATUS_BUSY\n"); 16540 sd_pkt_status_busy(un, bp, xp, pktp); 16541 break; 16542 16543 case STATUS_RESERVATION_CONFLICT: 16544 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16545 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16546 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16547 break; 16548 16549 case STATUS_QFULL: 16550 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16551 "sdintr: STATUS_QFULL\n"); 16552 sd_pkt_status_qfull(un, bp, xp, pktp); 16553 break; 16554 16555 case STATUS_MET: 16556 case STATUS_INTERMEDIATE: 16557 case STATUS_SCSI2: 16558 case STATUS_INTERMEDIATE_MET: 16559 case STATUS_ACA_ACTIVE: 16560 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16561 "Unexpected SCSI status received: 0x%x\n", 16562 SD_GET_PKT_STATUS(pktp)); 16563 sd_return_failed_command(un, bp, EIO); 16564 break; 16565 16566 default: 16567 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16568 "Invalid SCSI status received: 0x%x\n", 16569 SD_GET_PKT_STATUS(pktp)); 16570 sd_return_failed_command(un, bp, EIO); 16571 break; 16572 16573 } 16574 break; 16575 16576 case CMD_INCOMPLETE: 16577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16578 "sdintr: CMD_INCOMPLETE\n"); 16579 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16580 break; 16581 case CMD_TRAN_ERR: 16582 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16583 "sdintr: CMD_TRAN_ERR\n"); 16584 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16585 break; 16586 case CMD_RESET: 16587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16588 "sdintr: CMD_RESET \n"); 16589 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16590 break; 16591 case CMD_ABORTED: 16592 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16593 "sdintr: CMD_ABORTED \n"); 16594 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16595 break; 16596 case CMD_TIMEOUT: 16597 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16598 "sdintr: CMD_TIMEOUT\n"); 16599 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16600 break; 16601 case CMD_UNX_BUS_FREE: 16602 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16603 "sdintr: CMD_UNX_BUS_FREE \n"); 16604 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16605 break; 16606 case CMD_TAG_REJECT: 16607 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16608 "sdintr: CMD_TAG_REJECT\n"); 16609 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16610 break; 16611 default: 16612 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16613 "sdintr: default\n"); 16614 sd_pkt_reason_default(un, bp, xp, pktp); 16615 break; 16616 } 16617 16618 exit: 16619 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16620 16621 /* Decrement counter to indicate that the callback routine is done. */ 16622 un->un_in_callback--; 16623 ASSERT(un->un_in_callback >= 0); 16624 16625 /* 16626 * At this point, the pkt has been dispatched, ie, it is either 16627 * being re-tried or has been returned to its caller and should 16628 * not be referenced. 16629 */ 16630 16631 mutex_exit(SD_MUTEX(un)); 16632 } 16633 16634 16635 /* 16636 * Function: sd_print_incomplete_msg 16637 * 16638 * Description: Prints the error message for a CMD_INCOMPLETE error. 16639 * 16640 * Arguments: un - ptr to associated softstate for the device. 16641 * bp - ptr to the buf(9S) for the command. 16642 * arg - message string ptr 16643 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16644 * or SD_NO_RETRY_ISSUED. 16645 * 16646 * Context: May be called under interrupt context 16647 */ 16648 16649 static void 16650 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16651 { 16652 struct scsi_pkt *pktp; 16653 char *msgp; 16654 char *cmdp = arg; 16655 16656 ASSERT(un != NULL); 16657 ASSERT(mutex_owned(SD_MUTEX(un))); 16658 ASSERT(bp != NULL); 16659 ASSERT(arg != NULL); 16660 pktp = SD_GET_PKTP(bp); 16661 ASSERT(pktp != NULL); 16662 16663 switch (code) { 16664 case SD_DELAYED_RETRY_ISSUED: 16665 case SD_IMMEDIATE_RETRY_ISSUED: 16666 msgp = "retrying"; 16667 break; 16668 case SD_NO_RETRY_ISSUED: 16669 default: 16670 msgp = "giving up"; 16671 break; 16672 } 16673 16674 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16675 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16676 "incomplete %s- %s\n", cmdp, msgp); 16677 } 16678 } 16679 16680 16681 16682 /* 16683 * Function: sd_pkt_status_good 16684 * 16685 * Description: Processing for a STATUS_GOOD code in pkt_status. 16686 * 16687 * Context: May be called under interrupt context 16688 */ 16689 16690 static void 16691 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16692 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16693 { 16694 char *cmdp; 16695 16696 ASSERT(un != NULL); 16697 ASSERT(mutex_owned(SD_MUTEX(un))); 16698 ASSERT(bp != NULL); 16699 ASSERT(xp != NULL); 16700 ASSERT(pktp != NULL); 16701 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16702 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16703 ASSERT(pktp->pkt_resid != 0); 16704 16705 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16706 16707 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16708 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16709 case SCMD_READ: 16710 cmdp = "read"; 16711 break; 16712 case SCMD_WRITE: 16713 cmdp = "write"; 16714 break; 16715 default: 16716 SD_UPDATE_B_RESID(bp, pktp); 16717 sd_return_command(un, bp); 16718 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16719 return; 16720 } 16721 16722 /* 16723 * See if we can retry the read/write, preferrably immediately. 16724 * If retries are exhaused, then sd_retry_command() will update 16725 * the b_resid count. 16726 */ 16727 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16728 cmdp, EIO, (clock_t)0, NULL); 16729 16730 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16731 } 16732 16733 16734 16735 16736 16737 /* 16738 * Function: sd_handle_request_sense 16739 * 16740 * Description: Processing for non-auto Request Sense command. 16741 * 16742 * Arguments: un - ptr to associated softstate 16743 * sense_bp - ptr to buf(9S) for the RQS command 16744 * sense_xp - ptr to the sd_xbuf for the RQS command 16745 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16746 * 16747 * Context: May be called under interrupt context 16748 */ 16749 16750 static void 16751 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16752 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16753 { 16754 struct buf *cmd_bp; /* buf for the original command */ 16755 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16756 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16757 16758 ASSERT(un != NULL); 16759 ASSERT(mutex_owned(SD_MUTEX(un))); 16760 ASSERT(sense_bp != NULL); 16761 ASSERT(sense_xp != NULL); 16762 ASSERT(sense_pktp != NULL); 16763 16764 /* 16765 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16766 * RQS command and not the original command. 16767 */ 16768 ASSERT(sense_pktp == un->un_rqs_pktp); 16769 ASSERT(sense_bp == un->un_rqs_bp); 16770 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16771 (FLAG_SENSING | FLAG_HEAD)); 16772 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16773 FLAG_SENSING) == FLAG_SENSING); 16774 16775 /* These are the bp, xp, and pktp for the original command */ 16776 cmd_bp = sense_xp->xb_sense_bp; 16777 cmd_xp = SD_GET_XBUF(cmd_bp); 16778 cmd_pktp = SD_GET_PKTP(cmd_bp); 16779 16780 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16781 /* 16782 * The REQUEST SENSE command failed. Release the REQUEST 16783 * SENSE command for re-use, get back the bp for the original 16784 * command, and attempt to re-try the original command if 16785 * FLAG_DIAGNOSE is not set in the original packet. 16786 */ 16787 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16788 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16789 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16790 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16791 NULL, NULL, EIO, (clock_t)0, NULL); 16792 return; 16793 } 16794 } 16795 16796 /* 16797 * Save the relevant sense info into the xp for the original cmd. 16798 * 16799 * Note: if the request sense failed the state info will be zero 16800 * as set in sd_mark_rqs_busy() 16801 */ 16802 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16803 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16804 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16805 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16806 16807 /* 16808 * Free up the RQS command.... 16809 * NOTE: 16810 * Must do this BEFORE calling sd_validate_sense_data! 16811 * sd_validate_sense_data may return the original command in 16812 * which case the pkt will be freed and the flags can no 16813 * longer be touched. 16814 * SD_MUTEX is held through this process until the command 16815 * is dispatched based upon the sense data, so there are 16816 * no race conditions. 16817 */ 16818 (void) sd_mark_rqs_idle(un, sense_xp); 16819 16820 /* 16821 * For a retryable command see if we have valid sense data, if so then 16822 * turn it over to sd_decode_sense() to figure out the right course of 16823 * action. Just fail a non-retryable command. 16824 */ 16825 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16826 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16827 SD_SENSE_DATA_IS_VALID) { 16828 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16829 } 16830 } else { 16831 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16832 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16833 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16834 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16835 sd_return_failed_command(un, cmd_bp, EIO); 16836 } 16837 } 16838 16839 16840 16841 16842 /* 16843 * Function: sd_handle_auto_request_sense 16844 * 16845 * Description: Processing for auto-request sense information. 16846 * 16847 * Arguments: un - ptr to associated softstate 16848 * bp - ptr to buf(9S) for the command 16849 * xp - ptr to the sd_xbuf for the command 16850 * pktp - ptr to the scsi_pkt(9S) for the command 16851 * 16852 * Context: May be called under interrupt context 16853 */ 16854 16855 static void 16856 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16857 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16858 { 16859 struct scsi_arq_status *asp; 16860 16861 ASSERT(un != NULL); 16862 ASSERT(mutex_owned(SD_MUTEX(un))); 16863 ASSERT(bp != NULL); 16864 ASSERT(xp != NULL); 16865 ASSERT(pktp != NULL); 16866 ASSERT(pktp != un->un_rqs_pktp); 16867 ASSERT(bp != un->un_rqs_bp); 16868 16869 /* 16870 * For auto-request sense, we get a scsi_arq_status back from 16871 * the HBA, with the sense data in the sts_sensedata member. 16872 * The pkt_scbp of the packet points to this scsi_arq_status. 16873 */ 16874 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16875 16876 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16877 /* 16878 * The auto REQUEST SENSE failed; see if we can re-try 16879 * the original command. 16880 */ 16881 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16882 "auto request sense failed (reason=%s)\n", 16883 scsi_rname(asp->sts_rqpkt_reason)); 16884 16885 sd_reset_target(un, pktp); 16886 16887 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16888 NULL, NULL, EIO, (clock_t)0, NULL); 16889 return; 16890 } 16891 16892 /* Save the relevant sense info into the xp for the original cmd. */ 16893 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16894 xp->xb_sense_state = asp->sts_rqpkt_state; 16895 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16896 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16897 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16898 16899 /* 16900 * See if we have valid sense data, if so then turn it over to 16901 * sd_decode_sense() to figure out the right course of action. 16902 */ 16903 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16904 sd_decode_sense(un, bp, xp, pktp); 16905 } 16906 } 16907 16908 16909 /* 16910 * Function: sd_print_sense_failed_msg 16911 * 16912 * Description: Print log message when RQS has failed. 16913 * 16914 * Arguments: un - ptr to associated softstate 16915 * bp - ptr to buf(9S) for the command 16916 * arg - generic message string ptr 16917 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16918 * or SD_NO_RETRY_ISSUED 16919 * 16920 * Context: May be called from interrupt context 16921 */ 16922 16923 static void 16924 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16925 int code) 16926 { 16927 char *msgp = arg; 16928 16929 ASSERT(un != NULL); 16930 ASSERT(mutex_owned(SD_MUTEX(un))); 16931 ASSERT(bp != NULL); 16932 16933 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16934 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16935 } 16936 } 16937 16938 16939 /* 16940 * Function: sd_validate_sense_data 16941 * 16942 * Description: Check the given sense data for validity. 16943 * If the sense data is not valid, the command will 16944 * be either failed or retried! 16945 * 16946 * Return Code: SD_SENSE_DATA_IS_INVALID 16947 * SD_SENSE_DATA_IS_VALID 16948 * 16949 * Context: May be called from interrupt context 16950 */ 16951 16952 static int 16953 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16954 { 16955 struct scsi_extended_sense *esp; 16956 struct scsi_pkt *pktp; 16957 size_t actual_len; 16958 char *msgp = NULL; 16959 16960 ASSERT(un != NULL); 16961 ASSERT(mutex_owned(SD_MUTEX(un))); 16962 ASSERT(bp != NULL); 16963 ASSERT(bp != un->un_rqs_bp); 16964 ASSERT(xp != NULL); 16965 16966 pktp = SD_GET_PKTP(bp); 16967 ASSERT(pktp != NULL); 16968 16969 /* 16970 * Check the status of the RQS command (auto or manual). 16971 */ 16972 switch (xp->xb_sense_status & STATUS_MASK) { 16973 case STATUS_GOOD: 16974 break; 16975 16976 case STATUS_RESERVATION_CONFLICT: 16977 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16978 return (SD_SENSE_DATA_IS_INVALID); 16979 16980 case STATUS_BUSY: 16981 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16982 "Busy Status on REQUEST SENSE\n"); 16983 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16984 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16985 return (SD_SENSE_DATA_IS_INVALID); 16986 16987 case STATUS_QFULL: 16988 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16989 "QFULL Status on REQUEST SENSE\n"); 16990 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16991 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16992 return (SD_SENSE_DATA_IS_INVALID); 16993 16994 case STATUS_CHECK: 16995 case STATUS_TERMINATED: 16996 msgp = "Check Condition on REQUEST SENSE\n"; 16997 goto sense_failed; 16998 16999 default: 17000 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17001 goto sense_failed; 17002 } 17003 17004 /* 17005 * See if we got the minimum required amount of sense data. 17006 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17007 * or less. 17008 */ 17009 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17010 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17011 (actual_len == 0)) { 17012 msgp = "Request Sense couldn't get sense data\n"; 17013 goto sense_failed; 17014 } 17015 17016 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17017 msgp = "Not enough sense information\n"; 17018 goto sense_failed; 17019 } 17020 17021 /* 17022 * We require the extended sense data 17023 */ 17024 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17025 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17026 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17027 static char tmp[8]; 17028 static char buf[148]; 17029 char *p = (char *)(xp->xb_sense_data); 17030 int i; 17031 17032 mutex_enter(&sd_sense_mutex); 17033 (void) strcpy(buf, "undecodable sense information:"); 17034 for (i = 0; i < actual_len; i++) { 17035 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17036 (void) strcpy(&buf[strlen(buf)], tmp); 17037 } 17038 i = strlen(buf); 17039 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17040 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17041 mutex_exit(&sd_sense_mutex); 17042 } 17043 /* Note: Legacy behavior, fail the command with no retry */ 17044 sd_return_failed_command(un, bp, EIO); 17045 return (SD_SENSE_DATA_IS_INVALID); 17046 } 17047 17048 /* 17049 * Check that es_code is valid (es_class concatenated with es_code 17050 * make up the "response code" field. es_class will always be 7, so 17051 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17052 * format. 17053 */ 17054 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17055 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17056 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17057 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17058 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17059 goto sense_failed; 17060 } 17061 17062 return (SD_SENSE_DATA_IS_VALID); 17063 17064 sense_failed: 17065 /* 17066 * If the request sense failed (for whatever reason), attempt 17067 * to retry the original command. 17068 */ 17069 #if defined(__i386) || defined(__amd64) 17070 /* 17071 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17072 * sddef.h for Sparc platform, and x86 uses 1 binary 17073 * for both SCSI/FC. 17074 * The SD_RETRY_DELAY value need to be adjusted here 17075 * when SD_RETRY_DELAY change in sddef.h 17076 */ 17077 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17078 sd_print_sense_failed_msg, msgp, EIO, 17079 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17080 #else 17081 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17082 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17083 #endif 17084 17085 return (SD_SENSE_DATA_IS_INVALID); 17086 } 17087 17088 17089 17090 /* 17091 * Function: sd_decode_sense 17092 * 17093 * Description: Take recovery action(s) when SCSI Sense Data is received. 17094 * 17095 * Context: Interrupt context. 17096 */ 17097 17098 static void 17099 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17100 struct scsi_pkt *pktp) 17101 { 17102 uint8_t sense_key; 17103 17104 ASSERT(un != NULL); 17105 ASSERT(mutex_owned(SD_MUTEX(un))); 17106 ASSERT(bp != NULL); 17107 ASSERT(bp != un->un_rqs_bp); 17108 ASSERT(xp != NULL); 17109 ASSERT(pktp != NULL); 17110 17111 sense_key = scsi_sense_key(xp->xb_sense_data); 17112 17113 switch (sense_key) { 17114 case KEY_NO_SENSE: 17115 sd_sense_key_no_sense(un, bp, xp, pktp); 17116 break; 17117 case KEY_RECOVERABLE_ERROR: 17118 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17119 bp, xp, pktp); 17120 break; 17121 case KEY_NOT_READY: 17122 sd_sense_key_not_ready(un, xp->xb_sense_data, 17123 bp, xp, pktp); 17124 break; 17125 case KEY_MEDIUM_ERROR: 17126 case KEY_HARDWARE_ERROR: 17127 sd_sense_key_medium_or_hardware_error(un, 17128 xp->xb_sense_data, bp, xp, pktp); 17129 break; 17130 case KEY_ILLEGAL_REQUEST: 17131 sd_sense_key_illegal_request(un, bp, xp, pktp); 17132 break; 17133 case KEY_UNIT_ATTENTION: 17134 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17135 bp, xp, pktp); 17136 break; 17137 case KEY_WRITE_PROTECT: 17138 case KEY_VOLUME_OVERFLOW: 17139 case KEY_MISCOMPARE: 17140 sd_sense_key_fail_command(un, bp, xp, pktp); 17141 break; 17142 case KEY_BLANK_CHECK: 17143 sd_sense_key_blank_check(un, bp, xp, pktp); 17144 break; 17145 case KEY_ABORTED_COMMAND: 17146 sd_sense_key_aborted_command(un, bp, xp, pktp); 17147 break; 17148 case KEY_VENDOR_UNIQUE: 17149 case KEY_COPY_ABORTED: 17150 case KEY_EQUAL: 17151 case KEY_RESERVED: 17152 default: 17153 sd_sense_key_default(un, xp->xb_sense_data, 17154 bp, xp, pktp); 17155 break; 17156 } 17157 } 17158 17159 17160 /* 17161 * Function: sd_dump_memory 17162 * 17163 * Description: Debug logging routine to print the contents of a user provided 17164 * buffer. The output of the buffer is broken up into 256 byte 17165 * segments due to a size constraint of the scsi_log. 17166 * implementation. 17167 * 17168 * Arguments: un - ptr to softstate 17169 * comp - component mask 17170 * title - "title" string to preceed data when printed 17171 * data - ptr to data block to be printed 17172 * len - size of data block to be printed 17173 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17174 * 17175 * Context: May be called from interrupt context 17176 */ 17177 17178 #define SD_DUMP_MEMORY_BUF_SIZE 256 17179 17180 static char *sd_dump_format_string[] = { 17181 " 0x%02x", 17182 " %c" 17183 }; 17184 17185 static void 17186 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17187 int len, int fmt) 17188 { 17189 int i, j; 17190 int avail_count; 17191 int start_offset; 17192 int end_offset; 17193 size_t entry_len; 17194 char *bufp; 17195 char *local_buf; 17196 char *format_string; 17197 17198 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17199 17200 /* 17201 * In the debug version of the driver, this function is called from a 17202 * number of places which are NOPs in the release driver. 17203 * The debug driver therefore has additional methods of filtering 17204 * debug output. 17205 */ 17206 #ifdef SDDEBUG 17207 /* 17208 * In the debug version of the driver we can reduce the amount of debug 17209 * messages by setting sd_error_level to something other than 17210 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17211 * sd_component_mask. 17212 */ 17213 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17214 (sd_error_level != SCSI_ERR_ALL)) { 17215 return; 17216 } 17217 if (((sd_component_mask & comp) == 0) || 17218 (sd_error_level != SCSI_ERR_ALL)) { 17219 return; 17220 } 17221 #else 17222 if (sd_error_level != SCSI_ERR_ALL) { 17223 return; 17224 } 17225 #endif 17226 17227 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17228 bufp = local_buf; 17229 /* 17230 * Available length is the length of local_buf[], minus the 17231 * length of the title string, minus one for the ":", minus 17232 * one for the newline, minus one for the NULL terminator. 17233 * This gives the #bytes available for holding the printed 17234 * values from the given data buffer. 17235 */ 17236 if (fmt == SD_LOG_HEX) { 17237 format_string = sd_dump_format_string[0]; 17238 } else /* SD_LOG_CHAR */ { 17239 format_string = sd_dump_format_string[1]; 17240 } 17241 /* 17242 * Available count is the number of elements from the given 17243 * data buffer that we can fit into the available length. 17244 * This is based upon the size of the format string used. 17245 * Make one entry and find it's size. 17246 */ 17247 (void) sprintf(bufp, format_string, data[0]); 17248 entry_len = strlen(bufp); 17249 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17250 17251 j = 0; 17252 while (j < len) { 17253 bufp = local_buf; 17254 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17255 start_offset = j; 17256 17257 end_offset = start_offset + avail_count; 17258 17259 (void) sprintf(bufp, "%s:", title); 17260 bufp += strlen(bufp); 17261 for (i = start_offset; ((i < end_offset) && (j < len)); 17262 i++, j++) { 17263 (void) sprintf(bufp, format_string, data[i]); 17264 bufp += entry_len; 17265 } 17266 (void) sprintf(bufp, "\n"); 17267 17268 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17269 } 17270 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17271 } 17272 17273 /* 17274 * Function: sd_print_sense_msg 17275 * 17276 * Description: Log a message based upon the given sense data. 17277 * 17278 * Arguments: un - ptr to associated softstate 17279 * bp - ptr to buf(9S) for the command 17280 * arg - ptr to associate sd_sense_info struct 17281 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17282 * or SD_NO_RETRY_ISSUED 17283 * 17284 * Context: May be called from interrupt context 17285 */ 17286 17287 static void 17288 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17289 { 17290 struct sd_xbuf *xp; 17291 struct scsi_pkt *pktp; 17292 uint8_t *sensep; 17293 daddr_t request_blkno; 17294 diskaddr_t err_blkno; 17295 int severity; 17296 int pfa_flag; 17297 extern struct scsi_key_strings scsi_cmds[]; 17298 17299 ASSERT(un != NULL); 17300 ASSERT(mutex_owned(SD_MUTEX(un))); 17301 ASSERT(bp != NULL); 17302 xp = SD_GET_XBUF(bp); 17303 ASSERT(xp != NULL); 17304 pktp = SD_GET_PKTP(bp); 17305 ASSERT(pktp != NULL); 17306 ASSERT(arg != NULL); 17307 17308 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17309 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17310 17311 if ((code == SD_DELAYED_RETRY_ISSUED) || 17312 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17313 severity = SCSI_ERR_RETRYABLE; 17314 } 17315 17316 /* Use absolute block number for the request block number */ 17317 request_blkno = xp->xb_blkno; 17318 17319 /* 17320 * Now try to get the error block number from the sense data 17321 */ 17322 sensep = xp->xb_sense_data; 17323 17324 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17325 (uint64_t *)&err_blkno)) { 17326 /* 17327 * We retrieved the error block number from the information 17328 * portion of the sense data. 17329 * 17330 * For USCSI commands we are better off using the error 17331 * block no. as the requested block no. (This is the best 17332 * we can estimate.) 17333 */ 17334 if ((SD_IS_BUFIO(xp) == FALSE) && 17335 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17336 request_blkno = err_blkno; 17337 } 17338 } else { 17339 /* 17340 * Without the es_valid bit set (for fixed format) or an 17341 * information descriptor (for descriptor format) we cannot 17342 * be certain of the error blkno, so just use the 17343 * request_blkno. 17344 */ 17345 err_blkno = (diskaddr_t)request_blkno; 17346 } 17347 17348 /* 17349 * The following will log the buffer contents for the release driver 17350 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17351 * level is set to verbose. 17352 */ 17353 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17354 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17355 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17356 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17357 17358 if (pfa_flag == FALSE) { 17359 /* This is normally only set for USCSI */ 17360 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17361 return; 17362 } 17363 17364 if ((SD_IS_BUFIO(xp) == TRUE) && 17365 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17366 (severity < sd_error_level))) { 17367 return; 17368 } 17369 } 17370 17371 /* 17372 * Check for Sonoma Failover and keep a count of how many failed I/O's 17373 */ 17374 if ((SD_IS_LSI(un)) && 17375 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17376 (scsi_sense_asc(sensep) == 0x94) && 17377 (scsi_sense_ascq(sensep) == 0x01)) { 17378 un->un_sonoma_failure_count++; 17379 if (un->un_sonoma_failure_count > 1) { 17380 return; 17381 } 17382 } 17383 17384 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17385 request_blkno, err_blkno, scsi_cmds, 17386 (struct scsi_extended_sense *)sensep, 17387 un->un_additional_codes, NULL); 17388 } 17389 17390 /* 17391 * Function: sd_sense_key_no_sense 17392 * 17393 * Description: Recovery action when sense data was not received. 17394 * 17395 * Context: May be called from interrupt context 17396 */ 17397 17398 static void 17399 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17400 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17401 { 17402 struct sd_sense_info si; 17403 17404 ASSERT(un != NULL); 17405 ASSERT(mutex_owned(SD_MUTEX(un))); 17406 ASSERT(bp != NULL); 17407 ASSERT(xp != NULL); 17408 ASSERT(pktp != NULL); 17409 17410 si.ssi_severity = SCSI_ERR_FATAL; 17411 si.ssi_pfa_flag = FALSE; 17412 17413 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17414 17415 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17416 &si, EIO, (clock_t)0, NULL); 17417 } 17418 17419 17420 /* 17421 * Function: sd_sense_key_recoverable_error 17422 * 17423 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17424 * 17425 * Context: May be called from interrupt context 17426 */ 17427 17428 static void 17429 sd_sense_key_recoverable_error(struct sd_lun *un, 17430 uint8_t *sense_datap, 17431 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17432 { 17433 struct sd_sense_info si; 17434 uint8_t asc = scsi_sense_asc(sense_datap); 17435 17436 ASSERT(un != NULL); 17437 ASSERT(mutex_owned(SD_MUTEX(un))); 17438 ASSERT(bp != NULL); 17439 ASSERT(xp != NULL); 17440 ASSERT(pktp != NULL); 17441 17442 /* 17443 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17444 */ 17445 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17446 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17447 si.ssi_severity = SCSI_ERR_INFO; 17448 si.ssi_pfa_flag = TRUE; 17449 } else { 17450 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17451 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17452 si.ssi_severity = SCSI_ERR_RECOVERED; 17453 si.ssi_pfa_flag = FALSE; 17454 } 17455 17456 if (pktp->pkt_resid == 0) { 17457 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17458 sd_return_command(un, bp); 17459 return; 17460 } 17461 17462 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17463 &si, EIO, (clock_t)0, NULL); 17464 } 17465 17466 17467 17468 17469 /* 17470 * Function: sd_sense_key_not_ready 17471 * 17472 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17473 * 17474 * Context: May be called from interrupt context 17475 */ 17476 17477 static void 17478 sd_sense_key_not_ready(struct sd_lun *un, 17479 uint8_t *sense_datap, 17480 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17481 { 17482 struct sd_sense_info si; 17483 uint8_t asc = scsi_sense_asc(sense_datap); 17484 uint8_t ascq = scsi_sense_ascq(sense_datap); 17485 17486 ASSERT(un != NULL); 17487 ASSERT(mutex_owned(SD_MUTEX(un))); 17488 ASSERT(bp != NULL); 17489 ASSERT(xp != NULL); 17490 ASSERT(pktp != NULL); 17491 17492 si.ssi_severity = SCSI_ERR_FATAL; 17493 si.ssi_pfa_flag = FALSE; 17494 17495 /* 17496 * Update error stats after first NOT READY error. Disks may have 17497 * been powered down and may need to be restarted. For CDROMs, 17498 * report NOT READY errors only if media is present. 17499 */ 17500 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17501 (xp->xb_retry_count > 0)) { 17502 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17503 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17504 } 17505 17506 /* 17507 * Just fail if the "not ready" retry limit has been reached. 17508 */ 17509 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17510 /* Special check for error message printing for removables. */ 17511 if (un->un_f_has_removable_media && (asc == 0x04) && 17512 (ascq >= 0x04)) { 17513 si.ssi_severity = SCSI_ERR_ALL; 17514 } 17515 goto fail_command; 17516 } 17517 17518 /* 17519 * Check the ASC and ASCQ in the sense data as needed, to determine 17520 * what to do. 17521 */ 17522 switch (asc) { 17523 case 0x04: /* LOGICAL UNIT NOT READY */ 17524 /* 17525 * disk drives that don't spin up result in a very long delay 17526 * in format without warning messages. We will log a message 17527 * if the error level is set to verbose. 17528 */ 17529 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17530 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17531 "logical unit not ready, resetting disk\n"); 17532 } 17533 17534 /* 17535 * There are different requirements for CDROMs and disks for 17536 * the number of retries. If a CD-ROM is giving this, it is 17537 * probably reading TOC and is in the process of getting 17538 * ready, so we should keep on trying for a long time to make 17539 * sure that all types of media are taken in account (for 17540 * some media the drive takes a long time to read TOC). For 17541 * disks we do not want to retry this too many times as this 17542 * can cause a long hang in format when the drive refuses to 17543 * spin up (a very common failure). 17544 */ 17545 switch (ascq) { 17546 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17547 /* 17548 * Disk drives frequently refuse to spin up which 17549 * results in a very long hang in format without 17550 * warning messages. 17551 * 17552 * Note: This code preserves the legacy behavior of 17553 * comparing xb_retry_count against zero for fibre 17554 * channel targets instead of comparing against the 17555 * un_reset_retry_count value. The reason for this 17556 * discrepancy has been so utterly lost beneath the 17557 * Sands of Time that even Indiana Jones could not 17558 * find it. 17559 */ 17560 if (un->un_f_is_fibre == TRUE) { 17561 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17562 (xp->xb_retry_count > 0)) && 17563 (un->un_startstop_timeid == NULL)) { 17564 scsi_log(SD_DEVINFO(un), sd_label, 17565 CE_WARN, "logical unit not ready, " 17566 "resetting disk\n"); 17567 sd_reset_target(un, pktp); 17568 } 17569 } else { 17570 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17571 (xp->xb_retry_count > 17572 un->un_reset_retry_count)) && 17573 (un->un_startstop_timeid == NULL)) { 17574 scsi_log(SD_DEVINFO(un), sd_label, 17575 CE_WARN, "logical unit not ready, " 17576 "resetting disk\n"); 17577 sd_reset_target(un, pktp); 17578 } 17579 } 17580 break; 17581 17582 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17583 /* 17584 * If the target is in the process of becoming 17585 * ready, just proceed with the retry. This can 17586 * happen with CD-ROMs that take a long time to 17587 * read TOC after a power cycle or reset. 17588 */ 17589 goto do_retry; 17590 17591 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17592 break; 17593 17594 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17595 /* 17596 * Retries cannot help here so just fail right away. 17597 */ 17598 goto fail_command; 17599 17600 case 0x88: 17601 /* 17602 * Vendor-unique code for T3/T4: it indicates a 17603 * path problem in a mutipathed config, but as far as 17604 * the target driver is concerned it equates to a fatal 17605 * error, so we should just fail the command right away 17606 * (without printing anything to the console). If this 17607 * is not a T3/T4, fall thru to the default recovery 17608 * action. 17609 * T3/T4 is FC only, don't need to check is_fibre 17610 */ 17611 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17612 sd_return_failed_command(un, bp, EIO); 17613 return; 17614 } 17615 /* FALLTHRU */ 17616 17617 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17618 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17619 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17620 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17621 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17622 default: /* Possible future codes in SCSI spec? */ 17623 /* 17624 * For removable-media devices, do not retry if 17625 * ASCQ > 2 as these result mostly from USCSI commands 17626 * on MMC devices issued to check status of an 17627 * operation initiated in immediate mode. Also for 17628 * ASCQ >= 4 do not print console messages as these 17629 * mainly represent a user-initiated operation 17630 * instead of a system failure. 17631 */ 17632 if (un->un_f_has_removable_media) { 17633 si.ssi_severity = SCSI_ERR_ALL; 17634 goto fail_command; 17635 } 17636 break; 17637 } 17638 17639 /* 17640 * As part of our recovery attempt for the NOT READY 17641 * condition, we issue a START STOP UNIT command. However 17642 * we want to wait for a short delay before attempting this 17643 * as there may still be more commands coming back from the 17644 * target with the check condition. To do this we use 17645 * timeout(9F) to call sd_start_stop_unit_callback() after 17646 * the delay interval expires. (sd_start_stop_unit_callback() 17647 * dispatches sd_start_stop_unit_task(), which will issue 17648 * the actual START STOP UNIT command. The delay interval 17649 * is one-half of the delay that we will use to retry the 17650 * command that generated the NOT READY condition. 17651 * 17652 * Note that we could just dispatch sd_start_stop_unit_task() 17653 * from here and allow it to sleep for the delay interval, 17654 * but then we would be tying up the taskq thread 17655 * uncesessarily for the duration of the delay. 17656 * 17657 * Do not issue the START STOP UNIT if the current command 17658 * is already a START STOP UNIT. 17659 */ 17660 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17661 break; 17662 } 17663 17664 /* 17665 * Do not schedule the timeout if one is already pending. 17666 */ 17667 if (un->un_startstop_timeid != NULL) { 17668 SD_INFO(SD_LOG_ERROR, un, 17669 "sd_sense_key_not_ready: restart already issued to" 17670 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17671 ddi_get_instance(SD_DEVINFO(un))); 17672 break; 17673 } 17674 17675 /* 17676 * Schedule the START STOP UNIT command, then queue the command 17677 * for a retry. 17678 * 17679 * Note: A timeout is not scheduled for this retry because we 17680 * want the retry to be serial with the START_STOP_UNIT. The 17681 * retry will be started when the START_STOP_UNIT is completed 17682 * in sd_start_stop_unit_task. 17683 */ 17684 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17685 un, SD_BSY_TIMEOUT / 2); 17686 xp->xb_retry_count++; 17687 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17688 return; 17689 17690 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17691 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17692 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17693 "unit does not respond to selection\n"); 17694 } 17695 break; 17696 17697 case 0x3A: /* MEDIUM NOT PRESENT */ 17698 if (sd_error_level >= SCSI_ERR_FATAL) { 17699 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17700 "Caddy not inserted in drive\n"); 17701 } 17702 17703 sr_ejected(un); 17704 un->un_mediastate = DKIO_EJECTED; 17705 /* The state has changed, inform the media watch routines */ 17706 cv_broadcast(&un->un_state_cv); 17707 /* Just fail if no media is present in the drive. */ 17708 goto fail_command; 17709 17710 default: 17711 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17712 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17713 "Unit not Ready. Additional sense code 0x%x\n", 17714 asc); 17715 } 17716 break; 17717 } 17718 17719 do_retry: 17720 17721 /* 17722 * Retry the command, as some targets may report NOT READY for 17723 * several seconds after being reset. 17724 */ 17725 xp->xb_retry_count++; 17726 si.ssi_severity = SCSI_ERR_RETRYABLE; 17727 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17728 &si, EIO, SD_BSY_TIMEOUT, NULL); 17729 17730 return; 17731 17732 fail_command: 17733 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17734 sd_return_failed_command(un, bp, EIO); 17735 } 17736 17737 17738 17739 /* 17740 * Function: sd_sense_key_medium_or_hardware_error 17741 * 17742 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17743 * sense key. 17744 * 17745 * Context: May be called from interrupt context 17746 */ 17747 17748 static void 17749 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17750 uint8_t *sense_datap, 17751 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17752 { 17753 struct sd_sense_info si; 17754 uint8_t sense_key = scsi_sense_key(sense_datap); 17755 uint8_t asc = scsi_sense_asc(sense_datap); 17756 17757 ASSERT(un != NULL); 17758 ASSERT(mutex_owned(SD_MUTEX(un))); 17759 ASSERT(bp != NULL); 17760 ASSERT(xp != NULL); 17761 ASSERT(pktp != NULL); 17762 17763 si.ssi_severity = SCSI_ERR_FATAL; 17764 si.ssi_pfa_flag = FALSE; 17765 17766 if (sense_key == KEY_MEDIUM_ERROR) { 17767 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17768 } 17769 17770 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17771 17772 if ((un->un_reset_retry_count != 0) && 17773 (xp->xb_retry_count == un->un_reset_retry_count)) { 17774 mutex_exit(SD_MUTEX(un)); 17775 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17776 if (un->un_f_allow_bus_device_reset == TRUE) { 17777 17778 boolean_t try_resetting_target = B_TRUE; 17779 17780 /* 17781 * We need to be able to handle specific ASC when we are 17782 * handling a KEY_HARDWARE_ERROR. In particular 17783 * taking the default action of resetting the target may 17784 * not be the appropriate way to attempt recovery. 17785 * Resetting a target because of a single LUN failure 17786 * victimizes all LUNs on that target. 17787 * 17788 * This is true for the LSI arrays, if an LSI 17789 * array controller returns an ASC of 0x84 (LUN Dead) we 17790 * should trust it. 17791 */ 17792 17793 if (sense_key == KEY_HARDWARE_ERROR) { 17794 switch (asc) { 17795 case 0x84: 17796 if (SD_IS_LSI(un)) { 17797 try_resetting_target = B_FALSE; 17798 } 17799 break; 17800 default: 17801 break; 17802 } 17803 } 17804 17805 if (try_resetting_target == B_TRUE) { 17806 int reset_retval = 0; 17807 if (un->un_f_lun_reset_enabled == TRUE) { 17808 SD_TRACE(SD_LOG_IO_CORE, un, 17809 "sd_sense_key_medium_or_hardware_" 17810 "error: issuing RESET_LUN\n"); 17811 reset_retval = 17812 scsi_reset(SD_ADDRESS(un), 17813 RESET_LUN); 17814 } 17815 if (reset_retval == 0) { 17816 SD_TRACE(SD_LOG_IO_CORE, un, 17817 "sd_sense_key_medium_or_hardware_" 17818 "error: issuing RESET_TARGET\n"); 17819 (void) scsi_reset(SD_ADDRESS(un), 17820 RESET_TARGET); 17821 } 17822 } 17823 } 17824 mutex_enter(SD_MUTEX(un)); 17825 } 17826 17827 /* 17828 * This really ought to be a fatal error, but we will retry anyway 17829 * as some drives report this as a spurious error. 17830 */ 17831 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17832 &si, EIO, (clock_t)0, NULL); 17833 } 17834 17835 17836 17837 /* 17838 * Function: sd_sense_key_illegal_request 17839 * 17840 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17841 * 17842 * Context: May be called from interrupt context 17843 */ 17844 17845 static void 17846 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17847 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17848 { 17849 struct sd_sense_info si; 17850 17851 ASSERT(un != NULL); 17852 ASSERT(mutex_owned(SD_MUTEX(un))); 17853 ASSERT(bp != NULL); 17854 ASSERT(xp != NULL); 17855 ASSERT(pktp != NULL); 17856 17857 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17858 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17859 17860 si.ssi_severity = SCSI_ERR_INFO; 17861 si.ssi_pfa_flag = FALSE; 17862 17863 /* Pointless to retry if the target thinks it's an illegal request */ 17864 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17865 sd_return_failed_command(un, bp, EIO); 17866 } 17867 17868 17869 17870 17871 /* 17872 * Function: sd_sense_key_unit_attention 17873 * 17874 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17875 * 17876 * Context: May be called from interrupt context 17877 */ 17878 17879 static void 17880 sd_sense_key_unit_attention(struct sd_lun *un, 17881 uint8_t *sense_datap, 17882 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17883 { 17884 /* 17885 * For UNIT ATTENTION we allow retries for one minute. Devices 17886 * like Sonoma can return UNIT ATTENTION close to a minute 17887 * under certain conditions. 17888 */ 17889 int retry_check_flag = SD_RETRIES_UA; 17890 boolean_t kstat_updated = B_FALSE; 17891 struct sd_sense_info si; 17892 uint8_t asc = scsi_sense_asc(sense_datap); 17893 17894 ASSERT(un != NULL); 17895 ASSERT(mutex_owned(SD_MUTEX(un))); 17896 ASSERT(bp != NULL); 17897 ASSERT(xp != NULL); 17898 ASSERT(pktp != NULL); 17899 17900 si.ssi_severity = SCSI_ERR_INFO; 17901 si.ssi_pfa_flag = FALSE; 17902 17903 17904 switch (asc) { 17905 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17906 if (sd_report_pfa != 0) { 17907 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17908 si.ssi_pfa_flag = TRUE; 17909 retry_check_flag = SD_RETRIES_STANDARD; 17910 goto do_retry; 17911 } 17912 17913 break; 17914 17915 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17916 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17917 un->un_resvd_status |= 17918 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17919 } 17920 #ifdef _LP64 17921 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 17922 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 17923 un, KM_NOSLEEP) == 0) { 17924 /* 17925 * If we can't dispatch the task we'll just 17926 * live without descriptor sense. We can 17927 * try again on the next "unit attention" 17928 */ 17929 SD_ERROR(SD_LOG_ERROR, un, 17930 "sd_sense_key_unit_attention: " 17931 "Could not dispatch " 17932 "sd_reenable_dsense_task\n"); 17933 } 17934 } 17935 #endif /* _LP64 */ 17936 /* FALLTHRU */ 17937 17938 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17939 if (!un->un_f_has_removable_media) { 17940 break; 17941 } 17942 17943 /* 17944 * When we get a unit attention from a removable-media device, 17945 * it may be in a state that will take a long time to recover 17946 * (e.g., from a reset). Since we are executing in interrupt 17947 * context here, we cannot wait around for the device to come 17948 * back. So hand this command off to sd_media_change_task() 17949 * for deferred processing under taskq thread context. (Note 17950 * that the command still may be failed if a problem is 17951 * encountered at a later time.) 17952 */ 17953 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17954 KM_NOSLEEP) == 0) { 17955 /* 17956 * Cannot dispatch the request so fail the command. 17957 */ 17958 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17959 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17960 si.ssi_severity = SCSI_ERR_FATAL; 17961 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17962 sd_return_failed_command(un, bp, EIO); 17963 } 17964 17965 /* 17966 * If failed to dispatch sd_media_change_task(), we already 17967 * updated kstat. If succeed to dispatch sd_media_change_task(), 17968 * we should update kstat later if it encounters an error. So, 17969 * we update kstat_updated flag here. 17970 */ 17971 kstat_updated = B_TRUE; 17972 17973 /* 17974 * Either the command has been successfully dispatched to a 17975 * task Q for retrying, or the dispatch failed. In either case 17976 * do NOT retry again by calling sd_retry_command. This sets up 17977 * two retries of the same command and when one completes and 17978 * frees the resources the other will access freed memory, 17979 * a bad thing. 17980 */ 17981 return; 17982 17983 default: 17984 break; 17985 } 17986 17987 /* 17988 * Update kstat if we haven't done that. 17989 */ 17990 if (!kstat_updated) { 17991 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17992 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17993 } 17994 17995 do_retry: 17996 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17997 EIO, SD_UA_RETRY_DELAY, NULL); 17998 } 17999 18000 18001 18002 /* 18003 * Function: sd_sense_key_fail_command 18004 * 18005 * Description: Use to fail a command when we don't like the sense key that 18006 * was returned. 18007 * 18008 * Context: May be called from interrupt context 18009 */ 18010 18011 static void 18012 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18013 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18014 { 18015 struct sd_sense_info si; 18016 18017 ASSERT(un != NULL); 18018 ASSERT(mutex_owned(SD_MUTEX(un))); 18019 ASSERT(bp != NULL); 18020 ASSERT(xp != NULL); 18021 ASSERT(pktp != NULL); 18022 18023 si.ssi_severity = SCSI_ERR_FATAL; 18024 si.ssi_pfa_flag = FALSE; 18025 18026 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18027 sd_return_failed_command(un, bp, EIO); 18028 } 18029 18030 18031 18032 /* 18033 * Function: sd_sense_key_blank_check 18034 * 18035 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18036 * Has no monetary connotation. 18037 * 18038 * Context: May be called from interrupt context 18039 */ 18040 18041 static void 18042 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18043 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18044 { 18045 struct sd_sense_info si; 18046 18047 ASSERT(un != NULL); 18048 ASSERT(mutex_owned(SD_MUTEX(un))); 18049 ASSERT(bp != NULL); 18050 ASSERT(xp != NULL); 18051 ASSERT(pktp != NULL); 18052 18053 /* 18054 * Blank check is not fatal for removable devices, therefore 18055 * it does not require a console message. 18056 */ 18057 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18058 SCSI_ERR_FATAL; 18059 si.ssi_pfa_flag = FALSE; 18060 18061 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18062 sd_return_failed_command(un, bp, EIO); 18063 } 18064 18065 18066 18067 18068 /* 18069 * Function: sd_sense_key_aborted_command 18070 * 18071 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18072 * 18073 * Context: May be called from interrupt context 18074 */ 18075 18076 static void 18077 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18078 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18079 { 18080 struct sd_sense_info si; 18081 18082 ASSERT(un != NULL); 18083 ASSERT(mutex_owned(SD_MUTEX(un))); 18084 ASSERT(bp != NULL); 18085 ASSERT(xp != NULL); 18086 ASSERT(pktp != NULL); 18087 18088 si.ssi_severity = SCSI_ERR_FATAL; 18089 si.ssi_pfa_flag = FALSE; 18090 18091 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18092 18093 /* 18094 * This really ought to be a fatal error, but we will retry anyway 18095 * as some drives report this as a spurious error. 18096 */ 18097 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18098 &si, EIO, (clock_t)0, NULL); 18099 } 18100 18101 18102 18103 /* 18104 * Function: sd_sense_key_default 18105 * 18106 * Description: Default recovery action for several SCSI sense keys (basically 18107 * attempts a retry). 18108 * 18109 * Context: May be called from interrupt context 18110 */ 18111 18112 static void 18113 sd_sense_key_default(struct sd_lun *un, 18114 uint8_t *sense_datap, 18115 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18116 { 18117 struct sd_sense_info si; 18118 uint8_t sense_key = scsi_sense_key(sense_datap); 18119 18120 ASSERT(un != NULL); 18121 ASSERT(mutex_owned(SD_MUTEX(un))); 18122 ASSERT(bp != NULL); 18123 ASSERT(xp != NULL); 18124 ASSERT(pktp != NULL); 18125 18126 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18127 18128 /* 18129 * Undecoded sense key. Attempt retries and hope that will fix 18130 * the problem. Otherwise, we're dead. 18131 */ 18132 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18133 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18134 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18135 } 18136 18137 si.ssi_severity = SCSI_ERR_FATAL; 18138 si.ssi_pfa_flag = FALSE; 18139 18140 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18141 &si, EIO, (clock_t)0, NULL); 18142 } 18143 18144 18145 18146 /* 18147 * Function: sd_print_retry_msg 18148 * 18149 * Description: Print a message indicating the retry action being taken. 18150 * 18151 * Arguments: un - ptr to associated softstate 18152 * bp - ptr to buf(9S) for the command 18153 * arg - not used. 18154 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18155 * or SD_NO_RETRY_ISSUED 18156 * 18157 * Context: May be called from interrupt context 18158 */ 18159 /* ARGSUSED */ 18160 static void 18161 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18162 { 18163 struct sd_xbuf *xp; 18164 struct scsi_pkt *pktp; 18165 char *reasonp; 18166 char *msgp; 18167 18168 ASSERT(un != NULL); 18169 ASSERT(mutex_owned(SD_MUTEX(un))); 18170 ASSERT(bp != NULL); 18171 pktp = SD_GET_PKTP(bp); 18172 ASSERT(pktp != NULL); 18173 xp = SD_GET_XBUF(bp); 18174 ASSERT(xp != NULL); 18175 18176 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18177 mutex_enter(&un->un_pm_mutex); 18178 if ((un->un_state == SD_STATE_SUSPENDED) || 18179 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18180 (pktp->pkt_flags & FLAG_SILENT)) { 18181 mutex_exit(&un->un_pm_mutex); 18182 goto update_pkt_reason; 18183 } 18184 mutex_exit(&un->un_pm_mutex); 18185 18186 /* 18187 * Suppress messages if they are all the same pkt_reason; with 18188 * TQ, many (up to 256) are returned with the same pkt_reason. 18189 * If we are in panic, then suppress the retry messages. 18190 */ 18191 switch (flag) { 18192 case SD_NO_RETRY_ISSUED: 18193 msgp = "giving up"; 18194 break; 18195 case SD_IMMEDIATE_RETRY_ISSUED: 18196 case SD_DELAYED_RETRY_ISSUED: 18197 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18198 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18199 (sd_error_level != SCSI_ERR_ALL))) { 18200 return; 18201 } 18202 msgp = "retrying command"; 18203 break; 18204 default: 18205 goto update_pkt_reason; 18206 } 18207 18208 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18209 scsi_rname(pktp->pkt_reason)); 18210 18211 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18212 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18213 18214 update_pkt_reason: 18215 /* 18216 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18217 * This is to prevent multiple console messages for the same failure 18218 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18219 * when the command is retried successfully because there still may be 18220 * more commands coming back with the same value of pktp->pkt_reason. 18221 */ 18222 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18223 un->un_last_pkt_reason = pktp->pkt_reason; 18224 } 18225 } 18226 18227 18228 /* 18229 * Function: sd_print_cmd_incomplete_msg 18230 * 18231 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18232 * 18233 * Arguments: un - ptr to associated softstate 18234 * bp - ptr to buf(9S) for the command 18235 * arg - passed to sd_print_retry_msg() 18236 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18237 * or SD_NO_RETRY_ISSUED 18238 * 18239 * Context: May be called from interrupt context 18240 */ 18241 18242 static void 18243 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18244 int code) 18245 { 18246 dev_info_t *dip; 18247 18248 ASSERT(un != NULL); 18249 ASSERT(mutex_owned(SD_MUTEX(un))); 18250 ASSERT(bp != NULL); 18251 18252 switch (code) { 18253 case SD_NO_RETRY_ISSUED: 18254 /* Command was failed. Someone turned off this target? */ 18255 if (un->un_state != SD_STATE_OFFLINE) { 18256 /* 18257 * Suppress message if we are detaching and 18258 * device has been disconnected 18259 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18260 * private interface and not part of the DDI 18261 */ 18262 dip = un->un_sd->sd_dev; 18263 if (!(DEVI_IS_DETACHING(dip) && 18264 DEVI_IS_DEVICE_REMOVED(dip))) { 18265 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18266 "disk not responding to selection\n"); 18267 } 18268 New_state(un, SD_STATE_OFFLINE); 18269 } 18270 break; 18271 18272 case SD_DELAYED_RETRY_ISSUED: 18273 case SD_IMMEDIATE_RETRY_ISSUED: 18274 default: 18275 /* Command was successfully queued for retry */ 18276 sd_print_retry_msg(un, bp, arg, code); 18277 break; 18278 } 18279 } 18280 18281 18282 /* 18283 * Function: sd_pkt_reason_cmd_incomplete 18284 * 18285 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18286 * 18287 * Context: May be called from interrupt context 18288 */ 18289 18290 static void 18291 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18292 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18293 { 18294 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18295 18296 ASSERT(un != NULL); 18297 ASSERT(mutex_owned(SD_MUTEX(un))); 18298 ASSERT(bp != NULL); 18299 ASSERT(xp != NULL); 18300 ASSERT(pktp != NULL); 18301 18302 /* Do not do a reset if selection did not complete */ 18303 /* Note: Should this not just check the bit? */ 18304 if (pktp->pkt_state != STATE_GOT_BUS) { 18305 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18306 sd_reset_target(un, pktp); 18307 } 18308 18309 /* 18310 * If the target was not successfully selected, then set 18311 * SD_RETRIES_FAILFAST to indicate that we lost communication 18312 * with the target, and further retries and/or commands are 18313 * likely to take a long time. 18314 */ 18315 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18316 flag |= SD_RETRIES_FAILFAST; 18317 } 18318 18319 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18320 18321 sd_retry_command(un, bp, flag, 18322 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18323 } 18324 18325 18326 18327 /* 18328 * Function: sd_pkt_reason_cmd_tran_err 18329 * 18330 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18331 * 18332 * Context: May be called from interrupt context 18333 */ 18334 18335 static void 18336 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18337 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18338 { 18339 ASSERT(un != NULL); 18340 ASSERT(mutex_owned(SD_MUTEX(un))); 18341 ASSERT(bp != NULL); 18342 ASSERT(xp != NULL); 18343 ASSERT(pktp != NULL); 18344 18345 /* 18346 * Do not reset if we got a parity error, or if 18347 * selection did not complete. 18348 */ 18349 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18350 /* Note: Should this not just check the bit for pkt_state? */ 18351 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18352 (pktp->pkt_state != STATE_GOT_BUS)) { 18353 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18354 sd_reset_target(un, pktp); 18355 } 18356 18357 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18358 18359 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18360 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18361 } 18362 18363 18364 18365 /* 18366 * Function: sd_pkt_reason_cmd_reset 18367 * 18368 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18369 * 18370 * Context: May be called from interrupt context 18371 */ 18372 18373 static void 18374 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18375 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18376 { 18377 ASSERT(un != NULL); 18378 ASSERT(mutex_owned(SD_MUTEX(un))); 18379 ASSERT(bp != NULL); 18380 ASSERT(xp != NULL); 18381 ASSERT(pktp != NULL); 18382 18383 /* The target may still be running the command, so try to reset. */ 18384 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18385 sd_reset_target(un, pktp); 18386 18387 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18388 18389 /* 18390 * If pkt_reason is CMD_RESET chances are that this pkt got 18391 * reset because another target on this bus caused it. The target 18392 * that caused it should get CMD_TIMEOUT with pkt_statistics 18393 * of STAT_TIMEOUT/STAT_DEV_RESET. 18394 */ 18395 18396 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18397 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18398 } 18399 18400 18401 18402 18403 /* 18404 * Function: sd_pkt_reason_cmd_aborted 18405 * 18406 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18407 * 18408 * Context: May be called from interrupt context 18409 */ 18410 18411 static void 18412 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18413 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18414 { 18415 ASSERT(un != NULL); 18416 ASSERT(mutex_owned(SD_MUTEX(un))); 18417 ASSERT(bp != NULL); 18418 ASSERT(xp != NULL); 18419 ASSERT(pktp != NULL); 18420 18421 /* The target may still be running the command, so try to reset. */ 18422 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18423 sd_reset_target(un, pktp); 18424 18425 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18426 18427 /* 18428 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18429 * aborted because another target on this bus caused it. The target 18430 * that caused it should get CMD_TIMEOUT with pkt_statistics 18431 * of STAT_TIMEOUT/STAT_DEV_RESET. 18432 */ 18433 18434 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18435 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18436 } 18437 18438 18439 18440 /* 18441 * Function: sd_pkt_reason_cmd_timeout 18442 * 18443 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18444 * 18445 * Context: May be called from interrupt context 18446 */ 18447 18448 static void 18449 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18450 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18451 { 18452 ASSERT(un != NULL); 18453 ASSERT(mutex_owned(SD_MUTEX(un))); 18454 ASSERT(bp != NULL); 18455 ASSERT(xp != NULL); 18456 ASSERT(pktp != NULL); 18457 18458 18459 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18460 sd_reset_target(un, pktp); 18461 18462 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18463 18464 /* 18465 * A command timeout indicates that we could not establish 18466 * communication with the target, so set SD_RETRIES_FAILFAST 18467 * as further retries/commands are likely to take a long time. 18468 */ 18469 sd_retry_command(un, bp, 18470 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18471 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18472 } 18473 18474 18475 18476 /* 18477 * Function: sd_pkt_reason_cmd_unx_bus_free 18478 * 18479 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18480 * 18481 * Context: May be called from interrupt context 18482 */ 18483 18484 static void 18485 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18486 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18487 { 18488 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18489 18490 ASSERT(un != NULL); 18491 ASSERT(mutex_owned(SD_MUTEX(un))); 18492 ASSERT(bp != NULL); 18493 ASSERT(xp != NULL); 18494 ASSERT(pktp != NULL); 18495 18496 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18497 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18498 18499 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18500 sd_print_retry_msg : NULL; 18501 18502 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18503 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18504 } 18505 18506 18507 /* 18508 * Function: sd_pkt_reason_cmd_tag_reject 18509 * 18510 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18511 * 18512 * Context: May be called from interrupt context 18513 */ 18514 18515 static void 18516 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18517 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18518 { 18519 ASSERT(un != NULL); 18520 ASSERT(mutex_owned(SD_MUTEX(un))); 18521 ASSERT(bp != NULL); 18522 ASSERT(xp != NULL); 18523 ASSERT(pktp != NULL); 18524 18525 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18526 pktp->pkt_flags = 0; 18527 un->un_tagflags = 0; 18528 if (un->un_f_opt_queueing == TRUE) { 18529 un->un_throttle = min(un->un_throttle, 3); 18530 } else { 18531 un->un_throttle = 1; 18532 } 18533 mutex_exit(SD_MUTEX(un)); 18534 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18535 mutex_enter(SD_MUTEX(un)); 18536 18537 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18538 18539 /* Legacy behavior not to check retry counts here. */ 18540 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18541 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18542 } 18543 18544 18545 /* 18546 * Function: sd_pkt_reason_default 18547 * 18548 * Description: Default recovery actions for SCSA pkt_reason values that 18549 * do not have more explicit recovery actions. 18550 * 18551 * Context: May be called from interrupt context 18552 */ 18553 18554 static void 18555 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18556 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18557 { 18558 ASSERT(un != NULL); 18559 ASSERT(mutex_owned(SD_MUTEX(un))); 18560 ASSERT(bp != NULL); 18561 ASSERT(xp != NULL); 18562 ASSERT(pktp != NULL); 18563 18564 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18565 sd_reset_target(un, pktp); 18566 18567 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18568 18569 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18570 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18571 } 18572 18573 18574 18575 /* 18576 * Function: sd_pkt_status_check_condition 18577 * 18578 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18579 * 18580 * Context: May be called from interrupt context 18581 */ 18582 18583 static void 18584 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18585 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18586 { 18587 ASSERT(un != NULL); 18588 ASSERT(mutex_owned(SD_MUTEX(un))); 18589 ASSERT(bp != NULL); 18590 ASSERT(xp != NULL); 18591 ASSERT(pktp != NULL); 18592 18593 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18594 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18595 18596 /* 18597 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18598 * command will be retried after the request sense). Otherwise, retry 18599 * the command. Note: we are issuing the request sense even though the 18600 * retry limit may have been reached for the failed command. 18601 */ 18602 if (un->un_f_arq_enabled == FALSE) { 18603 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18604 "no ARQ, sending request sense command\n"); 18605 sd_send_request_sense_command(un, bp, pktp); 18606 } else { 18607 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18608 "ARQ,retrying request sense command\n"); 18609 #if defined(__i386) || defined(__amd64) 18610 /* 18611 * The SD_RETRY_DELAY value need to be adjusted here 18612 * when SD_RETRY_DELAY change in sddef.h 18613 */ 18614 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18615 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18616 NULL); 18617 #else 18618 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18619 EIO, SD_RETRY_DELAY, NULL); 18620 #endif 18621 } 18622 18623 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18624 } 18625 18626 18627 /* 18628 * Function: sd_pkt_status_busy 18629 * 18630 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18631 * 18632 * Context: May be called from interrupt context 18633 */ 18634 18635 static void 18636 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18637 struct scsi_pkt *pktp) 18638 { 18639 ASSERT(un != NULL); 18640 ASSERT(mutex_owned(SD_MUTEX(un))); 18641 ASSERT(bp != NULL); 18642 ASSERT(xp != NULL); 18643 ASSERT(pktp != NULL); 18644 18645 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18646 "sd_pkt_status_busy: entry\n"); 18647 18648 /* If retries are exhausted, just fail the command. */ 18649 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18650 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18651 "device busy too long\n"); 18652 sd_return_failed_command(un, bp, EIO); 18653 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18654 "sd_pkt_status_busy: exit\n"); 18655 return; 18656 } 18657 xp->xb_retry_count++; 18658 18659 /* 18660 * Try to reset the target. However, we do not want to perform 18661 * more than one reset if the device continues to fail. The reset 18662 * will be performed when the retry count reaches the reset 18663 * threshold. This threshold should be set such that at least 18664 * one retry is issued before the reset is performed. 18665 */ 18666 if (xp->xb_retry_count == 18667 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18668 int rval = 0; 18669 mutex_exit(SD_MUTEX(un)); 18670 if (un->un_f_allow_bus_device_reset == TRUE) { 18671 /* 18672 * First try to reset the LUN; if we cannot then 18673 * try to reset the target. 18674 */ 18675 if (un->un_f_lun_reset_enabled == TRUE) { 18676 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18677 "sd_pkt_status_busy: RESET_LUN\n"); 18678 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18679 } 18680 if (rval == 0) { 18681 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18682 "sd_pkt_status_busy: RESET_TARGET\n"); 18683 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18684 } 18685 } 18686 if (rval == 0) { 18687 /* 18688 * If the RESET_LUN and/or RESET_TARGET failed, 18689 * try RESET_ALL 18690 */ 18691 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18692 "sd_pkt_status_busy: RESET_ALL\n"); 18693 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18694 } 18695 mutex_enter(SD_MUTEX(un)); 18696 if (rval == 0) { 18697 /* 18698 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18699 * At this point we give up & fail the command. 18700 */ 18701 sd_return_failed_command(un, bp, EIO); 18702 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18703 "sd_pkt_status_busy: exit (failed cmd)\n"); 18704 return; 18705 } 18706 } 18707 18708 /* 18709 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18710 * we have already checked the retry counts above. 18711 */ 18712 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18713 EIO, SD_BSY_TIMEOUT, NULL); 18714 18715 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18716 "sd_pkt_status_busy: exit\n"); 18717 } 18718 18719 18720 /* 18721 * Function: sd_pkt_status_reservation_conflict 18722 * 18723 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18724 * command status. 18725 * 18726 * Context: May be called from interrupt context 18727 */ 18728 18729 static void 18730 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18731 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18732 { 18733 ASSERT(un != NULL); 18734 ASSERT(mutex_owned(SD_MUTEX(un))); 18735 ASSERT(bp != NULL); 18736 ASSERT(xp != NULL); 18737 ASSERT(pktp != NULL); 18738 18739 /* 18740 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18741 * conflict could be due to various reasons like incorrect keys, not 18742 * registered or not reserved etc. So, we return EACCES to the caller. 18743 */ 18744 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18745 int cmd = SD_GET_PKT_OPCODE(pktp); 18746 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18747 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18748 sd_return_failed_command(un, bp, EACCES); 18749 return; 18750 } 18751 } 18752 18753 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18754 18755 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18756 if (sd_failfast_enable != 0) { 18757 /* By definition, we must panic here.... */ 18758 sd_panic_for_res_conflict(un); 18759 /*NOTREACHED*/ 18760 } 18761 SD_ERROR(SD_LOG_IO, un, 18762 "sd_handle_resv_conflict: Disk Reserved\n"); 18763 sd_return_failed_command(un, bp, EACCES); 18764 return; 18765 } 18766 18767 /* 18768 * 1147670: retry only if sd_retry_on_reservation_conflict 18769 * property is set (default is 1). Retries will not succeed 18770 * on a disk reserved by another initiator. HA systems 18771 * may reset this via sd.conf to avoid these retries. 18772 * 18773 * Note: The legacy return code for this failure is EIO, however EACCES 18774 * seems more appropriate for a reservation conflict. 18775 */ 18776 if (sd_retry_on_reservation_conflict == 0) { 18777 SD_ERROR(SD_LOG_IO, un, 18778 "sd_handle_resv_conflict: Device Reserved\n"); 18779 sd_return_failed_command(un, bp, EIO); 18780 return; 18781 } 18782 18783 /* 18784 * Retry the command if we can. 18785 * 18786 * Note: The legacy return code for this failure is EIO, however EACCES 18787 * seems more appropriate for a reservation conflict. 18788 */ 18789 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18790 (clock_t)2, NULL); 18791 } 18792 18793 18794 18795 /* 18796 * Function: sd_pkt_status_qfull 18797 * 18798 * Description: Handle a QUEUE FULL condition from the target. This can 18799 * occur if the HBA does not handle the queue full condition. 18800 * (Basically this means third-party HBAs as Sun HBAs will 18801 * handle the queue full condition.) Note that if there are 18802 * some commands already in the transport, then the queue full 18803 * has occurred because the queue for this nexus is actually 18804 * full. If there are no commands in the transport, then the 18805 * queue full is resulting from some other initiator or lun 18806 * consuming all the resources at the target. 18807 * 18808 * Context: May be called from interrupt context 18809 */ 18810 18811 static void 18812 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18813 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18814 { 18815 ASSERT(un != NULL); 18816 ASSERT(mutex_owned(SD_MUTEX(un))); 18817 ASSERT(bp != NULL); 18818 ASSERT(xp != NULL); 18819 ASSERT(pktp != NULL); 18820 18821 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18822 "sd_pkt_status_qfull: entry\n"); 18823 18824 /* 18825 * Just lower the QFULL throttle and retry the command. Note that 18826 * we do not limit the number of retries here. 18827 */ 18828 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18829 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18830 SD_RESTART_TIMEOUT, NULL); 18831 18832 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18833 "sd_pkt_status_qfull: exit\n"); 18834 } 18835 18836 18837 /* 18838 * Function: sd_reset_target 18839 * 18840 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18841 * RESET_TARGET, or RESET_ALL. 18842 * 18843 * Context: May be called under interrupt context. 18844 */ 18845 18846 static void 18847 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18848 { 18849 int rval = 0; 18850 18851 ASSERT(un != NULL); 18852 ASSERT(mutex_owned(SD_MUTEX(un))); 18853 ASSERT(pktp != NULL); 18854 18855 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18856 18857 /* 18858 * No need to reset if the transport layer has already done so. 18859 */ 18860 if ((pktp->pkt_statistics & 18861 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18862 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18863 "sd_reset_target: no reset\n"); 18864 return; 18865 } 18866 18867 mutex_exit(SD_MUTEX(un)); 18868 18869 if (un->un_f_allow_bus_device_reset == TRUE) { 18870 if (un->un_f_lun_reset_enabled == TRUE) { 18871 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18872 "sd_reset_target: RESET_LUN\n"); 18873 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18874 } 18875 if (rval == 0) { 18876 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18877 "sd_reset_target: RESET_TARGET\n"); 18878 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18879 } 18880 } 18881 18882 if (rval == 0) { 18883 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18884 "sd_reset_target: RESET_ALL\n"); 18885 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18886 } 18887 18888 mutex_enter(SD_MUTEX(un)); 18889 18890 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18891 } 18892 18893 18894 /* 18895 * Function: sd_media_change_task 18896 * 18897 * Description: Recovery action for CDROM to become available. 18898 * 18899 * Context: Executes in a taskq() thread context 18900 */ 18901 18902 static void 18903 sd_media_change_task(void *arg) 18904 { 18905 struct scsi_pkt *pktp = arg; 18906 struct sd_lun *un; 18907 struct buf *bp; 18908 struct sd_xbuf *xp; 18909 int err = 0; 18910 int retry_count = 0; 18911 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18912 struct sd_sense_info si; 18913 18914 ASSERT(pktp != NULL); 18915 bp = (struct buf *)pktp->pkt_private; 18916 ASSERT(bp != NULL); 18917 xp = SD_GET_XBUF(bp); 18918 ASSERT(xp != NULL); 18919 un = SD_GET_UN(bp); 18920 ASSERT(un != NULL); 18921 ASSERT(!mutex_owned(SD_MUTEX(un))); 18922 ASSERT(un->un_f_monitor_media_state); 18923 18924 si.ssi_severity = SCSI_ERR_INFO; 18925 si.ssi_pfa_flag = FALSE; 18926 18927 /* 18928 * When a reset is issued on a CDROM, it takes a long time to 18929 * recover. First few attempts to read capacity and other things 18930 * related to handling unit attention fail (with a ASC 0x4 and 18931 * ASCQ 0x1). In that case we want to do enough retries and we want 18932 * to limit the retries in other cases of genuine failures like 18933 * no media in drive. 18934 */ 18935 while (retry_count++ < retry_limit) { 18936 if ((err = sd_handle_mchange(un)) == 0) { 18937 break; 18938 } 18939 if (err == EAGAIN) { 18940 retry_limit = SD_UNIT_ATTENTION_RETRY; 18941 } 18942 /* Sleep for 0.5 sec. & try again */ 18943 delay(drv_usectohz(500000)); 18944 } 18945 18946 /* 18947 * Dispatch (retry or fail) the original command here, 18948 * along with appropriate console messages.... 18949 * 18950 * Must grab the mutex before calling sd_retry_command, 18951 * sd_print_sense_msg and sd_return_failed_command. 18952 */ 18953 mutex_enter(SD_MUTEX(un)); 18954 if (err != SD_CMD_SUCCESS) { 18955 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18956 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18957 si.ssi_severity = SCSI_ERR_FATAL; 18958 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18959 sd_return_failed_command(un, bp, EIO); 18960 } else { 18961 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18962 &si, EIO, (clock_t)0, NULL); 18963 } 18964 mutex_exit(SD_MUTEX(un)); 18965 } 18966 18967 18968 18969 /* 18970 * Function: sd_handle_mchange 18971 * 18972 * Description: Perform geometry validation & other recovery when CDROM 18973 * has been removed from drive. 18974 * 18975 * Return Code: 0 for success 18976 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18977 * sd_send_scsi_READ_CAPACITY() 18978 * 18979 * Context: Executes in a taskq() thread context 18980 */ 18981 18982 static int 18983 sd_handle_mchange(struct sd_lun *un) 18984 { 18985 uint64_t capacity; 18986 uint32_t lbasize; 18987 int rval; 18988 18989 ASSERT(!mutex_owned(SD_MUTEX(un))); 18990 ASSERT(un->un_f_monitor_media_state); 18991 18992 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18993 SD_PATH_DIRECT_PRIORITY)) != 0) { 18994 return (rval); 18995 } 18996 18997 mutex_enter(SD_MUTEX(un)); 18998 sd_update_block_info(un, lbasize, capacity); 18999 19000 if (un->un_errstats != NULL) { 19001 struct sd_errstats *stp = 19002 (struct sd_errstats *)un->un_errstats->ks_data; 19003 stp->sd_capacity.value.ui64 = (uint64_t) 19004 ((uint64_t)un->un_blockcount * 19005 (uint64_t)un->un_tgt_blocksize); 19006 } 19007 19008 /* 19009 * Note: Maybe let the strategy/partitioning chain worry about getting 19010 * valid geometry. 19011 */ 19012 un->un_f_geometry_is_valid = FALSE; 19013 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19014 if (un->un_f_geometry_is_valid == FALSE) { 19015 mutex_exit(SD_MUTEX(un)); 19016 return (EIO); 19017 } 19018 19019 mutex_exit(SD_MUTEX(un)); 19020 19021 /* 19022 * Try to lock the door 19023 */ 19024 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19025 SD_PATH_DIRECT_PRIORITY)); 19026 } 19027 19028 19029 /* 19030 * Function: sd_send_scsi_DOORLOCK 19031 * 19032 * Description: Issue the scsi DOOR LOCK command 19033 * 19034 * Arguments: un - pointer to driver soft state (unit) structure for 19035 * this target. 19036 * flag - SD_REMOVAL_ALLOW 19037 * SD_REMOVAL_PREVENT 19038 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19039 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19040 * to use the USCSI "direct" chain and bypass the normal 19041 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19042 * command is issued as part of an error recovery action. 19043 * 19044 * Return Code: 0 - Success 19045 * errno return code from sd_send_scsi_cmd() 19046 * 19047 * Context: Can sleep. 19048 */ 19049 19050 static int 19051 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19052 { 19053 union scsi_cdb cdb; 19054 struct uscsi_cmd ucmd_buf; 19055 struct scsi_extended_sense sense_buf; 19056 int status; 19057 19058 ASSERT(un != NULL); 19059 ASSERT(!mutex_owned(SD_MUTEX(un))); 19060 19061 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19062 19063 /* already determined doorlock is not supported, fake success */ 19064 if (un->un_f_doorlock_supported == FALSE) { 19065 return (0); 19066 } 19067 19068 bzero(&cdb, sizeof (cdb)); 19069 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19070 19071 cdb.scc_cmd = SCMD_DOORLOCK; 19072 cdb.cdb_opaque[4] = (uchar_t)flag; 19073 19074 ucmd_buf.uscsi_cdb = (char *)&cdb; 19075 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19076 ucmd_buf.uscsi_bufaddr = NULL; 19077 ucmd_buf.uscsi_buflen = 0; 19078 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19079 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19080 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19081 ucmd_buf.uscsi_timeout = 15; 19082 19083 SD_TRACE(SD_LOG_IO, un, 19084 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19085 19086 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19087 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19088 19089 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19090 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19091 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19092 /* fake success and skip subsequent doorlock commands */ 19093 un->un_f_doorlock_supported = FALSE; 19094 return (0); 19095 } 19096 19097 return (status); 19098 } 19099 19100 /* 19101 * Function: sd_send_scsi_READ_CAPACITY 19102 * 19103 * Description: This routine uses the scsi READ CAPACITY command to determine 19104 * the device capacity in number of blocks and the device native 19105 * block size. If this function returns a failure, then the 19106 * values in *capp and *lbap are undefined. If the capacity 19107 * returned is 0xffffffff then the lun is too large for a 19108 * normal READ CAPACITY command and the results of a 19109 * READ CAPACITY 16 will be used instead. 19110 * 19111 * Arguments: un - ptr to soft state struct for the target 19112 * capp - ptr to unsigned 64-bit variable to receive the 19113 * capacity value from the command. 19114 * lbap - ptr to unsigned 32-bit varaible to receive the 19115 * block size value from the command 19116 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19117 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19118 * to use the USCSI "direct" chain and bypass the normal 19119 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19120 * command is issued as part of an error recovery action. 19121 * 19122 * Return Code: 0 - Success 19123 * EIO - IO error 19124 * EACCES - Reservation conflict detected 19125 * EAGAIN - Device is becoming ready 19126 * errno return code from sd_send_scsi_cmd() 19127 * 19128 * Context: Can sleep. Blocks until command completes. 19129 */ 19130 19131 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19132 19133 static int 19134 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19135 int path_flag) 19136 { 19137 struct scsi_extended_sense sense_buf; 19138 struct uscsi_cmd ucmd_buf; 19139 union scsi_cdb cdb; 19140 uint32_t *capacity_buf; 19141 uint64_t capacity; 19142 uint32_t lbasize; 19143 int status; 19144 19145 ASSERT(un != NULL); 19146 ASSERT(!mutex_owned(SD_MUTEX(un))); 19147 ASSERT(capp != NULL); 19148 ASSERT(lbap != NULL); 19149 19150 SD_TRACE(SD_LOG_IO, un, 19151 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19152 19153 /* 19154 * First send a READ_CAPACITY command to the target. 19155 * (This command is mandatory under SCSI-2.) 19156 * 19157 * Set up the CDB for the READ_CAPACITY command. The Partial 19158 * Medium Indicator bit is cleared. The address field must be 19159 * zero if the PMI bit is zero. 19160 */ 19161 bzero(&cdb, sizeof (cdb)); 19162 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19163 19164 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19165 19166 cdb.scc_cmd = SCMD_READ_CAPACITY; 19167 19168 ucmd_buf.uscsi_cdb = (char *)&cdb; 19169 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19170 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19171 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19172 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19173 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19174 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19175 ucmd_buf.uscsi_timeout = 60; 19176 19177 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19178 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19179 19180 switch (status) { 19181 case 0: 19182 /* Return failure if we did not get valid capacity data. */ 19183 if (ucmd_buf.uscsi_resid != 0) { 19184 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19185 return (EIO); 19186 } 19187 19188 /* 19189 * Read capacity and block size from the READ CAPACITY 10 data. 19190 * This data may be adjusted later due to device specific 19191 * issues. 19192 * 19193 * According to the SCSI spec, the READ CAPACITY 10 19194 * command returns the following: 19195 * 19196 * bytes 0-3: Maximum logical block address available. 19197 * (MSB in byte:0 & LSB in byte:3) 19198 * 19199 * bytes 4-7: Block length in bytes 19200 * (MSB in byte:4 & LSB in byte:7) 19201 * 19202 */ 19203 capacity = BE_32(capacity_buf[0]); 19204 lbasize = BE_32(capacity_buf[1]); 19205 19206 /* 19207 * Done with capacity_buf 19208 */ 19209 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19210 19211 /* 19212 * if the reported capacity is set to all 0xf's, then 19213 * this disk is too large and requires SBC-2 commands. 19214 * Reissue the request using READ CAPACITY 16. 19215 */ 19216 if (capacity == 0xffffffff) { 19217 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19218 &lbasize, path_flag); 19219 if (status != 0) { 19220 return (status); 19221 } 19222 } 19223 break; /* Success! */ 19224 case EIO: 19225 switch (ucmd_buf.uscsi_status) { 19226 case STATUS_RESERVATION_CONFLICT: 19227 status = EACCES; 19228 break; 19229 case STATUS_CHECK: 19230 /* 19231 * Check condition; look for ASC/ASCQ of 0x04/0x01 19232 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19233 */ 19234 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19235 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19236 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19237 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19238 return (EAGAIN); 19239 } 19240 break; 19241 default: 19242 break; 19243 } 19244 /* FALLTHRU */ 19245 default: 19246 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19247 return (status); 19248 } 19249 19250 /* 19251 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19252 * (2352 and 0 are common) so for these devices always force the value 19253 * to 2048 as required by the ATAPI specs. 19254 */ 19255 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19256 lbasize = 2048; 19257 } 19258 19259 /* 19260 * Get the maximum LBA value from the READ CAPACITY data. 19261 * Here we assume that the Partial Medium Indicator (PMI) bit 19262 * was cleared when issuing the command. This means that the LBA 19263 * returned from the device is the LBA of the last logical block 19264 * on the logical unit. The actual logical block count will be 19265 * this value plus one. 19266 * 19267 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19268 * so scale the capacity value to reflect this. 19269 */ 19270 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19271 19272 #if defined(__i386) || defined(__amd64) 19273 /* 19274 * Refer to comments related to off-by-1 at the 19275 * header of this file. 19276 * Treat 1TB disk as (1T - 512)B. 19277 */ 19278 if (un->un_f_capacity_adjusted == 1) 19279 capacity = DK_MAX_BLOCKS; 19280 #endif 19281 19282 /* 19283 * Copy the values from the READ CAPACITY command into the space 19284 * provided by the caller. 19285 */ 19286 *capp = capacity; 19287 *lbap = lbasize; 19288 19289 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19290 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19291 19292 /* 19293 * Both the lbasize and capacity from the device must be nonzero, 19294 * otherwise we assume that the values are not valid and return 19295 * failure to the caller. (4203735) 19296 */ 19297 if ((capacity == 0) || (lbasize == 0)) { 19298 return (EIO); 19299 } 19300 19301 return (0); 19302 } 19303 19304 /* 19305 * Function: sd_send_scsi_READ_CAPACITY_16 19306 * 19307 * Description: This routine uses the scsi READ CAPACITY 16 command to 19308 * determine the device capacity in number of blocks and the 19309 * device native block size. If this function returns a failure, 19310 * then the values in *capp and *lbap are undefined. 19311 * This routine should always be called by 19312 * sd_send_scsi_READ_CAPACITY which will appy any device 19313 * specific adjustments to capacity and lbasize. 19314 * 19315 * Arguments: un - ptr to soft state struct for the target 19316 * capp - ptr to unsigned 64-bit variable to receive the 19317 * capacity value from the command. 19318 * lbap - ptr to unsigned 32-bit varaible to receive the 19319 * block size value from the command 19320 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19321 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19322 * to use the USCSI "direct" chain and bypass the normal 19323 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19324 * this command is issued as part of an error recovery 19325 * action. 19326 * 19327 * Return Code: 0 - Success 19328 * EIO - IO error 19329 * EACCES - Reservation conflict detected 19330 * EAGAIN - Device is becoming ready 19331 * errno return code from sd_send_scsi_cmd() 19332 * 19333 * Context: Can sleep. Blocks until command completes. 19334 */ 19335 19336 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19337 19338 static int 19339 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19340 uint32_t *lbap, int path_flag) 19341 { 19342 struct scsi_extended_sense sense_buf; 19343 struct uscsi_cmd ucmd_buf; 19344 union scsi_cdb cdb; 19345 uint64_t *capacity16_buf; 19346 uint64_t capacity; 19347 uint32_t lbasize; 19348 int status; 19349 19350 ASSERT(un != NULL); 19351 ASSERT(!mutex_owned(SD_MUTEX(un))); 19352 ASSERT(capp != NULL); 19353 ASSERT(lbap != NULL); 19354 19355 SD_TRACE(SD_LOG_IO, un, 19356 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19357 19358 /* 19359 * First send a READ_CAPACITY_16 command to the target. 19360 * 19361 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19362 * Medium Indicator bit is cleared. The address field must be 19363 * zero if the PMI bit is zero. 19364 */ 19365 bzero(&cdb, sizeof (cdb)); 19366 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19367 19368 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19369 19370 ucmd_buf.uscsi_cdb = (char *)&cdb; 19371 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19372 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19373 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19374 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19375 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19376 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19377 ucmd_buf.uscsi_timeout = 60; 19378 19379 /* 19380 * Read Capacity (16) is a Service Action In command. One 19381 * command byte (0x9E) is overloaded for multiple operations, 19382 * with the second CDB byte specifying the desired operation 19383 */ 19384 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19385 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19386 19387 /* 19388 * Fill in allocation length field 19389 */ 19390 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19391 19392 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19393 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19394 19395 switch (status) { 19396 case 0: 19397 /* Return failure if we did not get valid capacity data. */ 19398 if (ucmd_buf.uscsi_resid > 20) { 19399 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19400 return (EIO); 19401 } 19402 19403 /* 19404 * Read capacity and block size from the READ CAPACITY 10 data. 19405 * This data may be adjusted later due to device specific 19406 * issues. 19407 * 19408 * According to the SCSI spec, the READ CAPACITY 10 19409 * command returns the following: 19410 * 19411 * bytes 0-7: Maximum logical block address available. 19412 * (MSB in byte:0 & LSB in byte:7) 19413 * 19414 * bytes 8-11: Block length in bytes 19415 * (MSB in byte:8 & LSB in byte:11) 19416 * 19417 */ 19418 capacity = BE_64(capacity16_buf[0]); 19419 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19420 19421 /* 19422 * Done with capacity16_buf 19423 */ 19424 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19425 19426 /* 19427 * if the reported capacity is set to all 0xf's, then 19428 * this disk is too large. This could only happen with 19429 * a device that supports LBAs larger than 64 bits which 19430 * are not defined by any current T10 standards. 19431 */ 19432 if (capacity == 0xffffffffffffffff) { 19433 return (EIO); 19434 } 19435 break; /* Success! */ 19436 case EIO: 19437 switch (ucmd_buf.uscsi_status) { 19438 case STATUS_RESERVATION_CONFLICT: 19439 status = EACCES; 19440 break; 19441 case STATUS_CHECK: 19442 /* 19443 * Check condition; look for ASC/ASCQ of 0x04/0x01 19444 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19445 */ 19446 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19447 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19448 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19449 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19450 return (EAGAIN); 19451 } 19452 break; 19453 default: 19454 break; 19455 } 19456 /* FALLTHRU */ 19457 default: 19458 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19459 return (status); 19460 } 19461 19462 *capp = capacity; 19463 *lbap = lbasize; 19464 19465 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19466 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19467 19468 return (0); 19469 } 19470 19471 19472 /* 19473 * Function: sd_send_scsi_START_STOP_UNIT 19474 * 19475 * Description: Issue a scsi START STOP UNIT command to the target. 19476 * 19477 * Arguments: un - pointer to driver soft state (unit) structure for 19478 * this target. 19479 * flag - SD_TARGET_START 19480 * SD_TARGET_STOP 19481 * SD_TARGET_EJECT 19482 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19483 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19484 * to use the USCSI "direct" chain and bypass the normal 19485 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19486 * command is issued as part of an error recovery action. 19487 * 19488 * Return Code: 0 - Success 19489 * EIO - IO error 19490 * EACCES - Reservation conflict detected 19491 * ENXIO - Not Ready, medium not present 19492 * errno return code from sd_send_scsi_cmd() 19493 * 19494 * Context: Can sleep. 19495 */ 19496 19497 static int 19498 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19499 { 19500 struct scsi_extended_sense sense_buf; 19501 union scsi_cdb cdb; 19502 struct uscsi_cmd ucmd_buf; 19503 int status; 19504 19505 ASSERT(un != NULL); 19506 ASSERT(!mutex_owned(SD_MUTEX(un))); 19507 19508 SD_TRACE(SD_LOG_IO, un, 19509 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19510 19511 if (un->un_f_check_start_stop && 19512 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19513 (un->un_f_start_stop_supported != TRUE)) { 19514 return (0); 19515 } 19516 19517 bzero(&cdb, sizeof (cdb)); 19518 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19519 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19520 19521 cdb.scc_cmd = SCMD_START_STOP; 19522 cdb.cdb_opaque[4] = (uchar_t)flag; 19523 19524 ucmd_buf.uscsi_cdb = (char *)&cdb; 19525 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19526 ucmd_buf.uscsi_bufaddr = NULL; 19527 ucmd_buf.uscsi_buflen = 0; 19528 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19529 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19530 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19531 ucmd_buf.uscsi_timeout = 200; 19532 19533 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19534 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19535 19536 switch (status) { 19537 case 0: 19538 break; /* Success! */ 19539 case EIO: 19540 switch (ucmd_buf.uscsi_status) { 19541 case STATUS_RESERVATION_CONFLICT: 19542 status = EACCES; 19543 break; 19544 case STATUS_CHECK: 19545 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19546 switch (scsi_sense_key( 19547 (uint8_t *)&sense_buf)) { 19548 case KEY_ILLEGAL_REQUEST: 19549 status = ENOTSUP; 19550 break; 19551 case KEY_NOT_READY: 19552 if (scsi_sense_asc( 19553 (uint8_t *)&sense_buf) 19554 == 0x3A) { 19555 status = ENXIO; 19556 } 19557 break; 19558 default: 19559 break; 19560 } 19561 } 19562 break; 19563 default: 19564 break; 19565 } 19566 break; 19567 default: 19568 break; 19569 } 19570 19571 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19572 19573 return (status); 19574 } 19575 19576 19577 /* 19578 * Function: sd_start_stop_unit_callback 19579 * 19580 * Description: timeout(9F) callback to begin recovery process for a 19581 * device that has spun down. 19582 * 19583 * Arguments: arg - pointer to associated softstate struct. 19584 * 19585 * Context: Executes in a timeout(9F) thread context 19586 */ 19587 19588 static void 19589 sd_start_stop_unit_callback(void *arg) 19590 { 19591 struct sd_lun *un = arg; 19592 ASSERT(un != NULL); 19593 ASSERT(!mutex_owned(SD_MUTEX(un))); 19594 19595 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19596 19597 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19598 } 19599 19600 19601 /* 19602 * Function: sd_start_stop_unit_task 19603 * 19604 * Description: Recovery procedure when a drive is spun down. 19605 * 19606 * Arguments: arg - pointer to associated softstate struct. 19607 * 19608 * Context: Executes in a taskq() thread context 19609 */ 19610 19611 static void 19612 sd_start_stop_unit_task(void *arg) 19613 { 19614 struct sd_lun *un = arg; 19615 19616 ASSERT(un != NULL); 19617 ASSERT(!mutex_owned(SD_MUTEX(un))); 19618 19619 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19620 19621 /* 19622 * Some unformatted drives report not ready error, no need to 19623 * restart if format has been initiated. 19624 */ 19625 mutex_enter(SD_MUTEX(un)); 19626 if (un->un_f_format_in_progress == TRUE) { 19627 mutex_exit(SD_MUTEX(un)); 19628 return; 19629 } 19630 mutex_exit(SD_MUTEX(un)); 19631 19632 /* 19633 * When a START STOP command is issued from here, it is part of a 19634 * failure recovery operation and must be issued before any other 19635 * commands, including any pending retries. Thus it must be sent 19636 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19637 * succeeds or not, we will start I/O after the attempt. 19638 */ 19639 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19640 SD_PATH_DIRECT_PRIORITY); 19641 19642 /* 19643 * The above call blocks until the START_STOP_UNIT command completes. 19644 * Now that it has completed, we must re-try the original IO that 19645 * received the NOT READY condition in the first place. There are 19646 * three possible conditions here: 19647 * 19648 * (1) The original IO is on un_retry_bp. 19649 * (2) The original IO is on the regular wait queue, and un_retry_bp 19650 * is NULL. 19651 * (3) The original IO is on the regular wait queue, and un_retry_bp 19652 * points to some other, unrelated bp. 19653 * 19654 * For each case, we must call sd_start_cmds() with un_retry_bp 19655 * as the argument. If un_retry_bp is NULL, this will initiate 19656 * processing of the regular wait queue. If un_retry_bp is not NULL, 19657 * then this will process the bp on un_retry_bp. That may or may not 19658 * be the original IO, but that does not matter: the important thing 19659 * is to keep the IO processing going at this point. 19660 * 19661 * Note: This is a very specific error recovery sequence associated 19662 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19663 * serialize the I/O with completion of the spin-up. 19664 */ 19665 mutex_enter(SD_MUTEX(un)); 19666 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19667 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19668 un, un->un_retry_bp); 19669 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19670 sd_start_cmds(un, un->un_retry_bp); 19671 mutex_exit(SD_MUTEX(un)); 19672 19673 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19674 } 19675 19676 19677 /* 19678 * Function: sd_send_scsi_INQUIRY 19679 * 19680 * Description: Issue the scsi INQUIRY command. 19681 * 19682 * Arguments: un 19683 * bufaddr 19684 * buflen 19685 * evpd 19686 * page_code 19687 * page_length 19688 * 19689 * Return Code: 0 - Success 19690 * errno return code from sd_send_scsi_cmd() 19691 * 19692 * Context: Can sleep. Does not return until command is completed. 19693 */ 19694 19695 static int 19696 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19697 uchar_t evpd, uchar_t page_code, size_t *residp) 19698 { 19699 union scsi_cdb cdb; 19700 struct uscsi_cmd ucmd_buf; 19701 int status; 19702 19703 ASSERT(un != NULL); 19704 ASSERT(!mutex_owned(SD_MUTEX(un))); 19705 ASSERT(bufaddr != NULL); 19706 19707 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19708 19709 bzero(&cdb, sizeof (cdb)); 19710 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19711 bzero(bufaddr, buflen); 19712 19713 cdb.scc_cmd = SCMD_INQUIRY; 19714 cdb.cdb_opaque[1] = evpd; 19715 cdb.cdb_opaque[2] = page_code; 19716 FORMG0COUNT(&cdb, buflen); 19717 19718 ucmd_buf.uscsi_cdb = (char *)&cdb; 19719 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19720 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19721 ucmd_buf.uscsi_buflen = buflen; 19722 ucmd_buf.uscsi_rqbuf = NULL; 19723 ucmd_buf.uscsi_rqlen = 0; 19724 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19725 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19726 19727 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19728 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19729 19730 if ((status == 0) && (residp != NULL)) { 19731 *residp = ucmd_buf.uscsi_resid; 19732 } 19733 19734 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19735 19736 return (status); 19737 } 19738 19739 19740 /* 19741 * Function: sd_send_scsi_TEST_UNIT_READY 19742 * 19743 * Description: Issue the scsi TEST UNIT READY command. 19744 * This routine can be told to set the flag USCSI_DIAGNOSE to 19745 * prevent retrying failed commands. Use this when the intent 19746 * is either to check for device readiness, to clear a Unit 19747 * Attention, or to clear any outstanding sense data. 19748 * However under specific conditions the expected behavior 19749 * is for retries to bring a device ready, so use the flag 19750 * with caution. 19751 * 19752 * Arguments: un 19753 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19754 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19755 * 0: dont check for media present, do retries on cmd. 19756 * 19757 * Return Code: 0 - Success 19758 * EIO - IO error 19759 * EACCES - Reservation conflict detected 19760 * ENXIO - Not Ready, medium not present 19761 * errno return code from sd_send_scsi_cmd() 19762 * 19763 * Context: Can sleep. Does not return until command is completed. 19764 */ 19765 19766 static int 19767 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19768 { 19769 struct scsi_extended_sense sense_buf; 19770 union scsi_cdb cdb; 19771 struct uscsi_cmd ucmd_buf; 19772 int status; 19773 19774 ASSERT(un != NULL); 19775 ASSERT(!mutex_owned(SD_MUTEX(un))); 19776 19777 SD_TRACE(SD_LOG_IO, un, 19778 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19779 19780 /* 19781 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19782 * timeouts when they receive a TUR and the queue is not empty. Check 19783 * the configuration flag set during attach (indicating the drive has 19784 * this firmware bug) and un_ncmds_in_transport before issuing the 19785 * TUR. If there are 19786 * pending commands return success, this is a bit arbitrary but is ok 19787 * for non-removables (i.e. the eliteI disks) and non-clustering 19788 * configurations. 19789 */ 19790 if (un->un_f_cfg_tur_check == TRUE) { 19791 mutex_enter(SD_MUTEX(un)); 19792 if (un->un_ncmds_in_transport != 0) { 19793 mutex_exit(SD_MUTEX(un)); 19794 return (0); 19795 } 19796 mutex_exit(SD_MUTEX(un)); 19797 } 19798 19799 bzero(&cdb, sizeof (cdb)); 19800 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19801 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19802 19803 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19804 19805 ucmd_buf.uscsi_cdb = (char *)&cdb; 19806 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19807 ucmd_buf.uscsi_bufaddr = NULL; 19808 ucmd_buf.uscsi_buflen = 0; 19809 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19810 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19811 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19812 19813 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19814 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19815 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19816 } 19817 ucmd_buf.uscsi_timeout = 60; 19818 19819 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19820 UIO_SYSSPACE, UIO_SYSSPACE, 19821 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19822 19823 switch (status) { 19824 case 0: 19825 break; /* Success! */ 19826 case EIO: 19827 switch (ucmd_buf.uscsi_status) { 19828 case STATUS_RESERVATION_CONFLICT: 19829 status = EACCES; 19830 break; 19831 case STATUS_CHECK: 19832 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19833 break; 19834 } 19835 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19836 (scsi_sense_key((uint8_t *)&sense_buf) == 19837 KEY_NOT_READY) && 19838 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 19839 status = ENXIO; 19840 } 19841 break; 19842 default: 19843 break; 19844 } 19845 break; 19846 default: 19847 break; 19848 } 19849 19850 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19851 19852 return (status); 19853 } 19854 19855 19856 /* 19857 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19858 * 19859 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19860 * 19861 * Arguments: un 19862 * 19863 * Return Code: 0 - Success 19864 * EACCES 19865 * ENOTSUP 19866 * errno return code from sd_send_scsi_cmd() 19867 * 19868 * Context: Can sleep. Does not return until command is completed. 19869 */ 19870 19871 static int 19872 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19873 uint16_t data_len, uchar_t *data_bufp) 19874 { 19875 struct scsi_extended_sense sense_buf; 19876 union scsi_cdb cdb; 19877 struct uscsi_cmd ucmd_buf; 19878 int status; 19879 int no_caller_buf = FALSE; 19880 19881 ASSERT(un != NULL); 19882 ASSERT(!mutex_owned(SD_MUTEX(un))); 19883 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19884 19885 SD_TRACE(SD_LOG_IO, un, 19886 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19887 19888 bzero(&cdb, sizeof (cdb)); 19889 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19890 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19891 if (data_bufp == NULL) { 19892 /* Allocate a default buf if the caller did not give one */ 19893 ASSERT(data_len == 0); 19894 data_len = MHIOC_RESV_KEY_SIZE; 19895 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19896 no_caller_buf = TRUE; 19897 } 19898 19899 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19900 cdb.cdb_opaque[1] = usr_cmd; 19901 FORMG1COUNT(&cdb, data_len); 19902 19903 ucmd_buf.uscsi_cdb = (char *)&cdb; 19904 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19905 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19906 ucmd_buf.uscsi_buflen = data_len; 19907 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19908 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19909 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19910 ucmd_buf.uscsi_timeout = 60; 19911 19912 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19913 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19914 19915 switch (status) { 19916 case 0: 19917 break; /* Success! */ 19918 case EIO: 19919 switch (ucmd_buf.uscsi_status) { 19920 case STATUS_RESERVATION_CONFLICT: 19921 status = EACCES; 19922 break; 19923 case STATUS_CHECK: 19924 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19925 (scsi_sense_key((uint8_t *)&sense_buf) == 19926 KEY_ILLEGAL_REQUEST)) { 19927 status = ENOTSUP; 19928 } 19929 break; 19930 default: 19931 break; 19932 } 19933 break; 19934 default: 19935 break; 19936 } 19937 19938 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19939 19940 if (no_caller_buf == TRUE) { 19941 kmem_free(data_bufp, data_len); 19942 } 19943 19944 return (status); 19945 } 19946 19947 19948 /* 19949 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19950 * 19951 * Description: This routine is the driver entry point for handling CD-ROM 19952 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19953 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19954 * device. 19955 * 19956 * Arguments: un - Pointer to soft state struct for the target. 19957 * usr_cmd SCSI-3 reservation facility command (one of 19958 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19959 * SD_SCSI3_PREEMPTANDABORT) 19960 * usr_bufp - user provided pointer register, reserve descriptor or 19961 * preempt and abort structure (mhioc_register_t, 19962 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19963 * 19964 * Return Code: 0 - Success 19965 * EACCES 19966 * ENOTSUP 19967 * errno return code from sd_send_scsi_cmd() 19968 * 19969 * Context: Can sleep. Does not return until command is completed. 19970 */ 19971 19972 static int 19973 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19974 uchar_t *usr_bufp) 19975 { 19976 struct scsi_extended_sense sense_buf; 19977 union scsi_cdb cdb; 19978 struct uscsi_cmd ucmd_buf; 19979 int status; 19980 uchar_t data_len = sizeof (sd_prout_t); 19981 sd_prout_t *prp; 19982 19983 ASSERT(un != NULL); 19984 ASSERT(!mutex_owned(SD_MUTEX(un))); 19985 ASSERT(data_len == 24); /* required by scsi spec */ 19986 19987 SD_TRACE(SD_LOG_IO, un, 19988 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19989 19990 if (usr_bufp == NULL) { 19991 return (EINVAL); 19992 } 19993 19994 bzero(&cdb, sizeof (cdb)); 19995 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19996 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19997 prp = kmem_zalloc(data_len, KM_SLEEP); 19998 19999 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20000 cdb.cdb_opaque[1] = usr_cmd; 20001 FORMG1COUNT(&cdb, data_len); 20002 20003 ucmd_buf.uscsi_cdb = (char *)&cdb; 20004 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20005 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20006 ucmd_buf.uscsi_buflen = data_len; 20007 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20008 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20009 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20010 ucmd_buf.uscsi_timeout = 60; 20011 20012 switch (usr_cmd) { 20013 case SD_SCSI3_REGISTER: { 20014 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20015 20016 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20017 bcopy(ptr->newkey.key, prp->service_key, 20018 MHIOC_RESV_KEY_SIZE); 20019 prp->aptpl = ptr->aptpl; 20020 break; 20021 } 20022 case SD_SCSI3_RESERVE: 20023 case SD_SCSI3_RELEASE: { 20024 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20025 20026 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20027 prp->scope_address = BE_32(ptr->scope_specific_addr); 20028 cdb.cdb_opaque[2] = ptr->type; 20029 break; 20030 } 20031 case SD_SCSI3_PREEMPTANDABORT: { 20032 mhioc_preemptandabort_t *ptr = 20033 (mhioc_preemptandabort_t *)usr_bufp; 20034 20035 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20036 bcopy(ptr->victim_key.key, prp->service_key, 20037 MHIOC_RESV_KEY_SIZE); 20038 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20039 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20040 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20041 break; 20042 } 20043 case SD_SCSI3_REGISTERANDIGNOREKEY: 20044 { 20045 mhioc_registerandignorekey_t *ptr; 20046 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20047 bcopy(ptr->newkey.key, 20048 prp->service_key, MHIOC_RESV_KEY_SIZE); 20049 prp->aptpl = ptr->aptpl; 20050 break; 20051 } 20052 default: 20053 ASSERT(FALSE); 20054 break; 20055 } 20056 20057 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20058 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20059 20060 switch (status) { 20061 case 0: 20062 break; /* Success! */ 20063 case EIO: 20064 switch (ucmd_buf.uscsi_status) { 20065 case STATUS_RESERVATION_CONFLICT: 20066 status = EACCES; 20067 break; 20068 case STATUS_CHECK: 20069 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20070 (scsi_sense_key((uint8_t *)&sense_buf) == 20071 KEY_ILLEGAL_REQUEST)) { 20072 status = ENOTSUP; 20073 } 20074 break; 20075 default: 20076 break; 20077 } 20078 break; 20079 default: 20080 break; 20081 } 20082 20083 kmem_free(prp, data_len); 20084 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20085 return (status); 20086 } 20087 20088 20089 /* 20090 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20091 * 20092 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20093 * 20094 * Arguments: un - pointer to the target's soft state struct 20095 * 20096 * Return Code: 0 - success 20097 * errno-type error code 20098 * 20099 * Context: kernel thread context only. 20100 */ 20101 20102 static int 20103 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20104 { 20105 struct sd_uscsi_info *uip; 20106 struct uscsi_cmd *uscmd; 20107 union scsi_cdb *cdb; 20108 struct buf *bp; 20109 int rval = 0; 20110 20111 SD_TRACE(SD_LOG_IO, un, 20112 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20113 20114 ASSERT(un != NULL); 20115 ASSERT(!mutex_owned(SD_MUTEX(un))); 20116 20117 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20118 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20119 20120 /* 20121 * First get some memory for the uscsi_cmd struct and cdb 20122 * and initialize for SYNCHRONIZE_CACHE cmd. 20123 */ 20124 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20125 uscmd->uscsi_cdblen = CDB_GROUP1; 20126 uscmd->uscsi_cdb = (caddr_t)cdb; 20127 uscmd->uscsi_bufaddr = NULL; 20128 uscmd->uscsi_buflen = 0; 20129 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20130 uscmd->uscsi_rqlen = SENSE_LENGTH; 20131 uscmd->uscsi_rqresid = SENSE_LENGTH; 20132 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20133 uscmd->uscsi_timeout = sd_io_time; 20134 20135 /* 20136 * Allocate an sd_uscsi_info struct and fill it with the info 20137 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20138 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20139 * since we allocate the buf here in this function, we do not 20140 * need to preserve the prior contents of b_private. 20141 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20142 */ 20143 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20144 uip->ui_flags = SD_PATH_DIRECT; 20145 uip->ui_cmdp = uscmd; 20146 20147 bp = getrbuf(KM_SLEEP); 20148 bp->b_private = uip; 20149 20150 /* 20151 * Setup buffer to carry uscsi request. 20152 */ 20153 bp->b_flags = B_BUSY; 20154 bp->b_bcount = 0; 20155 bp->b_blkno = 0; 20156 20157 if (dkc != NULL) { 20158 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20159 uip->ui_dkc = *dkc; 20160 } 20161 20162 bp->b_edev = SD_GET_DEV(un); 20163 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20164 20165 (void) sd_uscsi_strategy(bp); 20166 20167 /* 20168 * If synchronous request, wait for completion 20169 * If async just return and let b_iodone callback 20170 * cleanup. 20171 * NOTE: On return, u_ncmds_in_driver will be decremented, 20172 * but it was also incremented in sd_uscsi_strategy(), so 20173 * we should be ok. 20174 */ 20175 if (dkc == NULL) { 20176 (void) biowait(bp); 20177 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20178 } 20179 20180 return (rval); 20181 } 20182 20183 20184 static int 20185 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20186 { 20187 struct sd_uscsi_info *uip; 20188 struct uscsi_cmd *uscmd; 20189 uint8_t *sense_buf; 20190 struct sd_lun *un; 20191 int status; 20192 20193 uip = (struct sd_uscsi_info *)(bp->b_private); 20194 ASSERT(uip != NULL); 20195 20196 uscmd = uip->ui_cmdp; 20197 ASSERT(uscmd != NULL); 20198 20199 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20200 ASSERT(sense_buf != NULL); 20201 20202 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20203 ASSERT(un != NULL); 20204 20205 status = geterror(bp); 20206 switch (status) { 20207 case 0: 20208 break; /* Success! */ 20209 case EIO: 20210 switch (uscmd->uscsi_status) { 20211 case STATUS_RESERVATION_CONFLICT: 20212 /* Ignore reservation conflict */ 20213 status = 0; 20214 goto done; 20215 20216 case STATUS_CHECK: 20217 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20218 (scsi_sense_key(sense_buf) == 20219 KEY_ILLEGAL_REQUEST)) { 20220 /* Ignore Illegal Request error */ 20221 mutex_enter(SD_MUTEX(un)); 20222 un->un_f_sync_cache_supported = FALSE; 20223 mutex_exit(SD_MUTEX(un)); 20224 status = ENOTSUP; 20225 goto done; 20226 } 20227 break; 20228 default: 20229 break; 20230 } 20231 /* FALLTHRU */ 20232 default: 20233 /* Ignore error if the media is not present */ 20234 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 20235 status = 0; 20236 goto done; 20237 } 20238 /* If we reach this, we had an error */ 20239 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20240 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20241 break; 20242 } 20243 20244 done: 20245 if (uip->ui_dkc.dkc_callback != NULL) { 20246 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20247 } 20248 20249 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20250 freerbuf(bp); 20251 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20252 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20253 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20254 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20255 20256 return (status); 20257 } 20258 20259 20260 /* 20261 * Function: sd_send_scsi_GET_CONFIGURATION 20262 * 20263 * Description: Issues the get configuration command to the device. 20264 * Called from sd_check_for_writable_cd & sd_get_media_info 20265 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20266 * Arguments: un 20267 * ucmdbuf 20268 * rqbuf 20269 * rqbuflen 20270 * bufaddr 20271 * buflen 20272 * 20273 * Return Code: 0 - Success 20274 * errno return code from sd_send_scsi_cmd() 20275 * 20276 * Context: Can sleep. Does not return until command is completed. 20277 * 20278 */ 20279 20280 static int 20281 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20282 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20283 { 20284 char cdb[CDB_GROUP1]; 20285 int status; 20286 20287 ASSERT(un != NULL); 20288 ASSERT(!mutex_owned(SD_MUTEX(un))); 20289 ASSERT(bufaddr != NULL); 20290 ASSERT(ucmdbuf != NULL); 20291 ASSERT(rqbuf != NULL); 20292 20293 SD_TRACE(SD_LOG_IO, un, 20294 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20295 20296 bzero(cdb, sizeof (cdb)); 20297 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20298 bzero(rqbuf, rqbuflen); 20299 bzero(bufaddr, buflen); 20300 20301 /* 20302 * Set up cdb field for the get configuration command. 20303 */ 20304 cdb[0] = SCMD_GET_CONFIGURATION; 20305 cdb[1] = 0x02; /* Requested Type */ 20306 cdb[8] = SD_PROFILE_HEADER_LEN; 20307 ucmdbuf->uscsi_cdb = cdb; 20308 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20309 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20310 ucmdbuf->uscsi_buflen = buflen; 20311 ucmdbuf->uscsi_timeout = sd_io_time; 20312 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20313 ucmdbuf->uscsi_rqlen = rqbuflen; 20314 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20315 20316 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20317 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20318 20319 switch (status) { 20320 case 0: 20321 break; /* Success! */ 20322 case EIO: 20323 switch (ucmdbuf->uscsi_status) { 20324 case STATUS_RESERVATION_CONFLICT: 20325 status = EACCES; 20326 break; 20327 default: 20328 break; 20329 } 20330 break; 20331 default: 20332 break; 20333 } 20334 20335 if (status == 0) { 20336 SD_DUMP_MEMORY(un, SD_LOG_IO, 20337 "sd_send_scsi_GET_CONFIGURATION: data", 20338 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20339 } 20340 20341 SD_TRACE(SD_LOG_IO, un, 20342 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20343 20344 return (status); 20345 } 20346 20347 /* 20348 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20349 * 20350 * Description: Issues the get configuration command to the device to 20351 * retrieve a specfic feature. Called from 20352 * sd_check_for_writable_cd & sd_set_mmc_caps. 20353 * Arguments: un 20354 * ucmdbuf 20355 * rqbuf 20356 * rqbuflen 20357 * bufaddr 20358 * buflen 20359 * feature 20360 * 20361 * Return Code: 0 - Success 20362 * errno return code from sd_send_scsi_cmd() 20363 * 20364 * Context: Can sleep. Does not return until command is completed. 20365 * 20366 */ 20367 static int 20368 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20369 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20370 uchar_t *bufaddr, uint_t buflen, char feature) 20371 { 20372 char cdb[CDB_GROUP1]; 20373 int status; 20374 20375 ASSERT(un != NULL); 20376 ASSERT(!mutex_owned(SD_MUTEX(un))); 20377 ASSERT(bufaddr != NULL); 20378 ASSERT(ucmdbuf != NULL); 20379 ASSERT(rqbuf != NULL); 20380 20381 SD_TRACE(SD_LOG_IO, un, 20382 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20383 20384 bzero(cdb, sizeof (cdb)); 20385 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20386 bzero(rqbuf, rqbuflen); 20387 bzero(bufaddr, buflen); 20388 20389 /* 20390 * Set up cdb field for the get configuration command. 20391 */ 20392 cdb[0] = SCMD_GET_CONFIGURATION; 20393 cdb[1] = 0x02; /* Requested Type */ 20394 cdb[3] = feature; 20395 cdb[8] = buflen; 20396 ucmdbuf->uscsi_cdb = cdb; 20397 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20398 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20399 ucmdbuf->uscsi_buflen = buflen; 20400 ucmdbuf->uscsi_timeout = sd_io_time; 20401 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20402 ucmdbuf->uscsi_rqlen = rqbuflen; 20403 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20404 20405 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20406 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20407 20408 switch (status) { 20409 case 0: 20410 break; /* Success! */ 20411 case EIO: 20412 switch (ucmdbuf->uscsi_status) { 20413 case STATUS_RESERVATION_CONFLICT: 20414 status = EACCES; 20415 break; 20416 default: 20417 break; 20418 } 20419 break; 20420 default: 20421 break; 20422 } 20423 20424 if (status == 0) { 20425 SD_DUMP_MEMORY(un, SD_LOG_IO, 20426 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20427 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20428 } 20429 20430 SD_TRACE(SD_LOG_IO, un, 20431 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20432 20433 return (status); 20434 } 20435 20436 20437 /* 20438 * Function: sd_send_scsi_MODE_SENSE 20439 * 20440 * Description: Utility function for issuing a scsi MODE SENSE command. 20441 * Note: This routine uses a consistent implementation for Group0, 20442 * Group1, and Group2 commands across all platforms. ATAPI devices 20443 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20444 * 20445 * Arguments: un - pointer to the softstate struct for the target. 20446 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20447 * CDB_GROUP[1|2] (10 byte). 20448 * bufaddr - buffer for page data retrieved from the target. 20449 * buflen - size of page to be retrieved. 20450 * page_code - page code of data to be retrieved from the target. 20451 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20452 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20453 * to use the USCSI "direct" chain and bypass the normal 20454 * command waitq. 20455 * 20456 * Return Code: 0 - Success 20457 * errno return code from sd_send_scsi_cmd() 20458 * 20459 * Context: Can sleep. Does not return until command is completed. 20460 */ 20461 20462 static int 20463 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20464 size_t buflen, uchar_t page_code, int path_flag) 20465 { 20466 struct scsi_extended_sense sense_buf; 20467 union scsi_cdb cdb; 20468 struct uscsi_cmd ucmd_buf; 20469 int status; 20470 int headlen; 20471 20472 ASSERT(un != NULL); 20473 ASSERT(!mutex_owned(SD_MUTEX(un))); 20474 ASSERT(bufaddr != NULL); 20475 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20476 (cdbsize == CDB_GROUP2)); 20477 20478 SD_TRACE(SD_LOG_IO, un, 20479 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20480 20481 bzero(&cdb, sizeof (cdb)); 20482 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20483 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20484 bzero(bufaddr, buflen); 20485 20486 if (cdbsize == CDB_GROUP0) { 20487 cdb.scc_cmd = SCMD_MODE_SENSE; 20488 cdb.cdb_opaque[2] = page_code; 20489 FORMG0COUNT(&cdb, buflen); 20490 headlen = MODE_HEADER_LENGTH; 20491 } else { 20492 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20493 cdb.cdb_opaque[2] = page_code; 20494 FORMG1COUNT(&cdb, buflen); 20495 headlen = MODE_HEADER_LENGTH_GRP2; 20496 } 20497 20498 ASSERT(headlen <= buflen); 20499 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20500 20501 ucmd_buf.uscsi_cdb = (char *)&cdb; 20502 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20503 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20504 ucmd_buf.uscsi_buflen = buflen; 20505 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20506 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20507 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20508 ucmd_buf.uscsi_timeout = 60; 20509 20510 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20511 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20512 20513 switch (status) { 20514 case 0: 20515 /* 20516 * sr_check_wp() uses 0x3f page code and check the header of 20517 * mode page to determine if target device is write-protected. 20518 * But some USB devices return 0 bytes for 0x3f page code. For 20519 * this case, make sure that mode page header is returned at 20520 * least. 20521 */ 20522 if (buflen - ucmd_buf.uscsi_resid < headlen) 20523 status = EIO; 20524 break; /* Success! */ 20525 case EIO: 20526 switch (ucmd_buf.uscsi_status) { 20527 case STATUS_RESERVATION_CONFLICT: 20528 status = EACCES; 20529 break; 20530 default: 20531 break; 20532 } 20533 break; 20534 default: 20535 break; 20536 } 20537 20538 if (status == 0) { 20539 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20540 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20541 } 20542 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20543 20544 return (status); 20545 } 20546 20547 20548 /* 20549 * Function: sd_send_scsi_MODE_SELECT 20550 * 20551 * Description: Utility function for issuing a scsi MODE SELECT command. 20552 * Note: This routine uses a consistent implementation for Group0, 20553 * Group1, and Group2 commands across all platforms. ATAPI devices 20554 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20555 * 20556 * Arguments: un - pointer to the softstate struct for the target. 20557 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20558 * CDB_GROUP[1|2] (10 byte). 20559 * bufaddr - buffer for page data retrieved from the target. 20560 * buflen - size of page to be retrieved. 20561 * save_page - boolean to determin if SP bit should be set. 20562 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20563 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20564 * to use the USCSI "direct" chain and bypass the normal 20565 * command waitq. 20566 * 20567 * Return Code: 0 - Success 20568 * errno return code from sd_send_scsi_cmd() 20569 * 20570 * Context: Can sleep. Does not return until command is completed. 20571 */ 20572 20573 static int 20574 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20575 size_t buflen, uchar_t save_page, int path_flag) 20576 { 20577 struct scsi_extended_sense sense_buf; 20578 union scsi_cdb cdb; 20579 struct uscsi_cmd ucmd_buf; 20580 int status; 20581 20582 ASSERT(un != NULL); 20583 ASSERT(!mutex_owned(SD_MUTEX(un))); 20584 ASSERT(bufaddr != NULL); 20585 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20586 (cdbsize == CDB_GROUP2)); 20587 20588 SD_TRACE(SD_LOG_IO, un, 20589 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20590 20591 bzero(&cdb, sizeof (cdb)); 20592 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20593 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20594 20595 /* Set the PF bit for many third party drives */ 20596 cdb.cdb_opaque[1] = 0x10; 20597 20598 /* Set the savepage(SP) bit if given */ 20599 if (save_page == SD_SAVE_PAGE) { 20600 cdb.cdb_opaque[1] |= 0x01; 20601 } 20602 20603 if (cdbsize == CDB_GROUP0) { 20604 cdb.scc_cmd = SCMD_MODE_SELECT; 20605 FORMG0COUNT(&cdb, buflen); 20606 } else { 20607 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20608 FORMG1COUNT(&cdb, buflen); 20609 } 20610 20611 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20612 20613 ucmd_buf.uscsi_cdb = (char *)&cdb; 20614 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20615 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20616 ucmd_buf.uscsi_buflen = buflen; 20617 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20618 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20619 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20620 ucmd_buf.uscsi_timeout = 60; 20621 20622 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20623 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20624 20625 switch (status) { 20626 case 0: 20627 break; /* Success! */ 20628 case EIO: 20629 switch (ucmd_buf.uscsi_status) { 20630 case STATUS_RESERVATION_CONFLICT: 20631 status = EACCES; 20632 break; 20633 default: 20634 break; 20635 } 20636 break; 20637 default: 20638 break; 20639 } 20640 20641 if (status == 0) { 20642 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20643 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20644 } 20645 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20646 20647 return (status); 20648 } 20649 20650 20651 /* 20652 * Function: sd_send_scsi_RDWR 20653 * 20654 * Description: Issue a scsi READ or WRITE command with the given parameters. 20655 * 20656 * Arguments: un: Pointer to the sd_lun struct for the target. 20657 * cmd: SCMD_READ or SCMD_WRITE 20658 * bufaddr: Address of caller's buffer to receive the RDWR data 20659 * buflen: Length of caller's buffer receive the RDWR data. 20660 * start_block: Block number for the start of the RDWR operation. 20661 * (Assumes target-native block size.) 20662 * residp: Pointer to variable to receive the redisual of the 20663 * RDWR operation (may be NULL of no residual requested). 20664 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20665 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20666 * to use the USCSI "direct" chain and bypass the normal 20667 * command waitq. 20668 * 20669 * Return Code: 0 - Success 20670 * errno return code from sd_send_scsi_cmd() 20671 * 20672 * Context: Can sleep. Does not return until command is completed. 20673 */ 20674 20675 static int 20676 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20677 size_t buflen, daddr_t start_block, int path_flag) 20678 { 20679 struct scsi_extended_sense sense_buf; 20680 union scsi_cdb cdb; 20681 struct uscsi_cmd ucmd_buf; 20682 uint32_t block_count; 20683 int status; 20684 int cdbsize; 20685 uchar_t flag; 20686 20687 ASSERT(un != NULL); 20688 ASSERT(!mutex_owned(SD_MUTEX(un))); 20689 ASSERT(bufaddr != NULL); 20690 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20691 20692 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20693 20694 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20695 return (EINVAL); 20696 } 20697 20698 mutex_enter(SD_MUTEX(un)); 20699 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20700 mutex_exit(SD_MUTEX(un)); 20701 20702 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20703 20704 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20705 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20706 bufaddr, buflen, start_block, block_count); 20707 20708 bzero(&cdb, sizeof (cdb)); 20709 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20710 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20711 20712 /* Compute CDB size to use */ 20713 if (start_block > 0xffffffff) 20714 cdbsize = CDB_GROUP4; 20715 else if ((start_block & 0xFFE00000) || 20716 (un->un_f_cfg_is_atapi == TRUE)) 20717 cdbsize = CDB_GROUP1; 20718 else 20719 cdbsize = CDB_GROUP0; 20720 20721 switch (cdbsize) { 20722 case CDB_GROUP0: /* 6-byte CDBs */ 20723 cdb.scc_cmd = cmd; 20724 FORMG0ADDR(&cdb, start_block); 20725 FORMG0COUNT(&cdb, block_count); 20726 break; 20727 case CDB_GROUP1: /* 10-byte CDBs */ 20728 cdb.scc_cmd = cmd | SCMD_GROUP1; 20729 FORMG1ADDR(&cdb, start_block); 20730 FORMG1COUNT(&cdb, block_count); 20731 break; 20732 case CDB_GROUP4: /* 16-byte CDBs */ 20733 cdb.scc_cmd = cmd | SCMD_GROUP4; 20734 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20735 FORMG4COUNT(&cdb, block_count); 20736 break; 20737 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20738 default: 20739 /* All others reserved */ 20740 return (EINVAL); 20741 } 20742 20743 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20744 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20745 20746 ucmd_buf.uscsi_cdb = (char *)&cdb; 20747 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20748 ucmd_buf.uscsi_bufaddr = bufaddr; 20749 ucmd_buf.uscsi_buflen = buflen; 20750 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20751 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20752 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20753 ucmd_buf.uscsi_timeout = 60; 20754 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20755 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20756 switch (status) { 20757 case 0: 20758 break; /* Success! */ 20759 case EIO: 20760 switch (ucmd_buf.uscsi_status) { 20761 case STATUS_RESERVATION_CONFLICT: 20762 status = EACCES; 20763 break; 20764 default: 20765 break; 20766 } 20767 break; 20768 default: 20769 break; 20770 } 20771 20772 if (status == 0) { 20773 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20774 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20775 } 20776 20777 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20778 20779 return (status); 20780 } 20781 20782 20783 /* 20784 * Function: sd_send_scsi_LOG_SENSE 20785 * 20786 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20787 * 20788 * Arguments: un: Pointer to the sd_lun struct for the target. 20789 * 20790 * Return Code: 0 - Success 20791 * errno return code from sd_send_scsi_cmd() 20792 * 20793 * Context: Can sleep. Does not return until command is completed. 20794 */ 20795 20796 static int 20797 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20798 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20799 int path_flag) 20800 20801 { 20802 struct scsi_extended_sense sense_buf; 20803 union scsi_cdb cdb; 20804 struct uscsi_cmd ucmd_buf; 20805 int status; 20806 20807 ASSERT(un != NULL); 20808 ASSERT(!mutex_owned(SD_MUTEX(un))); 20809 20810 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20811 20812 bzero(&cdb, sizeof (cdb)); 20813 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20814 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20815 20816 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20817 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20818 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20819 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20820 FORMG1COUNT(&cdb, buflen); 20821 20822 ucmd_buf.uscsi_cdb = (char *)&cdb; 20823 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20824 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20825 ucmd_buf.uscsi_buflen = buflen; 20826 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20827 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20828 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20829 ucmd_buf.uscsi_timeout = 60; 20830 20831 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20832 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20833 20834 switch (status) { 20835 case 0: 20836 break; 20837 case EIO: 20838 switch (ucmd_buf.uscsi_status) { 20839 case STATUS_RESERVATION_CONFLICT: 20840 status = EACCES; 20841 break; 20842 case STATUS_CHECK: 20843 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20844 (scsi_sense_key((uint8_t *)&sense_buf) == 20845 KEY_ILLEGAL_REQUEST) && 20846 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 20847 /* 20848 * ASC 0x24: INVALID FIELD IN CDB 20849 */ 20850 switch (page_code) { 20851 case START_STOP_CYCLE_PAGE: 20852 /* 20853 * The start stop cycle counter is 20854 * implemented as page 0x31 in earlier 20855 * generation disks. In new generation 20856 * disks the start stop cycle counter is 20857 * implemented as page 0xE. To properly 20858 * handle this case if an attempt for 20859 * log page 0xE is made and fails we 20860 * will try again using page 0x31. 20861 * 20862 * Network storage BU committed to 20863 * maintain the page 0x31 for this 20864 * purpose and will not have any other 20865 * page implemented with page code 0x31 20866 * until all disks transition to the 20867 * standard page. 20868 */ 20869 mutex_enter(SD_MUTEX(un)); 20870 un->un_start_stop_cycle_page = 20871 START_STOP_CYCLE_VU_PAGE; 20872 cdb.cdb_opaque[2] = 20873 (char)(page_control << 6) | 20874 un->un_start_stop_cycle_page; 20875 mutex_exit(SD_MUTEX(un)); 20876 status = sd_send_scsi_cmd( 20877 SD_GET_DEV(un), &ucmd_buf, 20878 UIO_SYSSPACE, UIO_SYSSPACE, 20879 UIO_SYSSPACE, path_flag); 20880 20881 break; 20882 case TEMPERATURE_PAGE: 20883 status = ENOTTY; 20884 break; 20885 default: 20886 break; 20887 } 20888 } 20889 break; 20890 default: 20891 break; 20892 } 20893 break; 20894 default: 20895 break; 20896 } 20897 20898 if (status == 0) { 20899 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20900 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20901 } 20902 20903 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20904 20905 return (status); 20906 } 20907 20908 20909 /* 20910 * Function: sdioctl 20911 * 20912 * Description: Driver's ioctl(9e) entry point function. 20913 * 20914 * Arguments: dev - device number 20915 * cmd - ioctl operation to be performed 20916 * arg - user argument, contains data to be set or reference 20917 * parameter for get 20918 * flag - bit flag, indicating open settings, 32/64 bit type 20919 * cred_p - user credential pointer 20920 * rval_p - calling process return value (OPT) 20921 * 20922 * Return Code: EINVAL 20923 * ENOTTY 20924 * ENXIO 20925 * EIO 20926 * EFAULT 20927 * ENOTSUP 20928 * EPERM 20929 * 20930 * Context: Called from the device switch at normal priority. 20931 */ 20932 20933 static int 20934 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20935 { 20936 struct sd_lun *un = NULL; 20937 int geom_validated = FALSE; 20938 int err = 0; 20939 int i = 0; 20940 cred_t *cr; 20941 20942 /* 20943 * All device accesses go thru sdstrategy where we check on suspend 20944 * status 20945 */ 20946 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20947 return (ENXIO); 20948 } 20949 20950 ASSERT(!mutex_owned(SD_MUTEX(un))); 20951 20952 /* 20953 * Moved this wait from sd_uscsi_strategy to here for 20954 * reasons of deadlock prevention. Internal driver commands, 20955 * specifically those to change a devices power level, result 20956 * in a call to sd_uscsi_strategy. 20957 */ 20958 mutex_enter(SD_MUTEX(un)); 20959 while ((un->un_state == SD_STATE_SUSPENDED) || 20960 (un->un_state == SD_STATE_PM_CHANGING)) { 20961 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20962 } 20963 /* 20964 * Twiddling the counter here protects commands from now 20965 * through to the top of sd_uscsi_strategy. Without the 20966 * counter inc. a power down, for example, could get in 20967 * after the above check for state is made and before 20968 * execution gets to the top of sd_uscsi_strategy. 20969 * That would cause problems. 20970 */ 20971 un->un_ncmds_in_driver++; 20972 20973 if ((un->un_f_geometry_is_valid == FALSE) && 20974 (flag & (FNDELAY | FNONBLOCK))) { 20975 switch (cmd) { 20976 case CDROMPAUSE: 20977 case CDROMRESUME: 20978 case CDROMPLAYMSF: 20979 case CDROMPLAYTRKIND: 20980 case CDROMREADTOCHDR: 20981 case CDROMREADTOCENTRY: 20982 case CDROMSTOP: 20983 case CDROMSTART: 20984 case CDROMVOLCTRL: 20985 case CDROMSUBCHNL: 20986 case CDROMREADMODE2: 20987 case CDROMREADMODE1: 20988 case CDROMREADOFFSET: 20989 case CDROMSBLKMODE: 20990 case CDROMGBLKMODE: 20991 case CDROMGDRVSPEED: 20992 case CDROMSDRVSPEED: 20993 case CDROMCDDA: 20994 case CDROMCDXA: 20995 case CDROMSUBCODE: 20996 if (!ISCD(un)) { 20997 un->un_ncmds_in_driver--; 20998 ASSERT(un->un_ncmds_in_driver >= 0); 20999 mutex_exit(SD_MUTEX(un)); 21000 return (ENOTTY); 21001 } 21002 break; 21003 case FDEJECT: 21004 case DKIOCEJECT: 21005 case CDROMEJECT: 21006 if (!un->un_f_eject_media_supported) { 21007 un->un_ncmds_in_driver--; 21008 ASSERT(un->un_ncmds_in_driver >= 0); 21009 mutex_exit(SD_MUTEX(un)); 21010 return (ENOTTY); 21011 } 21012 break; 21013 case DKIOCSVTOC: 21014 case DKIOCSETEFI: 21015 case DKIOCSMBOOT: 21016 case DKIOCFLUSHWRITECACHE: 21017 mutex_exit(SD_MUTEX(un)); 21018 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21019 if (err != 0) { 21020 mutex_enter(SD_MUTEX(un)); 21021 un->un_ncmds_in_driver--; 21022 ASSERT(un->un_ncmds_in_driver >= 0); 21023 mutex_exit(SD_MUTEX(un)); 21024 return (EIO); 21025 } 21026 mutex_enter(SD_MUTEX(un)); 21027 /* FALLTHROUGH */ 21028 case DKIOCREMOVABLE: 21029 case DKIOCHOTPLUGGABLE: 21030 case DKIOCINFO: 21031 case DKIOCGMEDIAINFO: 21032 case MHIOCENFAILFAST: 21033 case MHIOCSTATUS: 21034 case MHIOCTKOWN: 21035 case MHIOCRELEASE: 21036 case MHIOCGRP_INKEYS: 21037 case MHIOCGRP_INRESV: 21038 case MHIOCGRP_REGISTER: 21039 case MHIOCGRP_RESERVE: 21040 case MHIOCGRP_PREEMPTANDABORT: 21041 case MHIOCGRP_REGISTERANDIGNOREKEY: 21042 case CDROMCLOSETRAY: 21043 case USCSICMD: 21044 goto skip_ready_valid; 21045 default: 21046 break; 21047 } 21048 21049 mutex_exit(SD_MUTEX(un)); 21050 err = sd_ready_and_valid(un); 21051 mutex_enter(SD_MUTEX(un)); 21052 if (err == SD_READY_NOT_VALID) { 21053 switch (cmd) { 21054 case DKIOCGAPART: 21055 case DKIOCGGEOM: 21056 case DKIOCSGEOM: 21057 case DKIOCGVTOC: 21058 case DKIOCSVTOC: 21059 case DKIOCSAPART: 21060 case DKIOCG_PHYGEOM: 21061 case DKIOCG_VIRTGEOM: 21062 err = ENOTSUP; 21063 un->un_ncmds_in_driver--; 21064 ASSERT(un->un_ncmds_in_driver >= 0); 21065 mutex_exit(SD_MUTEX(un)); 21066 return (err); 21067 } 21068 } 21069 if (err != SD_READY_VALID) { 21070 switch (cmd) { 21071 case DKIOCSTATE: 21072 case CDROMGDRVSPEED: 21073 case CDROMSDRVSPEED: 21074 case FDEJECT: /* for eject command */ 21075 case DKIOCEJECT: 21076 case CDROMEJECT: 21077 case DKIOCGETEFI: 21078 case DKIOCSGEOM: 21079 case DKIOCREMOVABLE: 21080 case DKIOCHOTPLUGGABLE: 21081 case DKIOCSAPART: 21082 case DKIOCSETEFI: 21083 break; 21084 default: 21085 if (un->un_f_has_removable_media) { 21086 err = ENXIO; 21087 } else { 21088 /* Do not map EACCES to EIO */ 21089 if (err != EACCES) 21090 err = EIO; 21091 } 21092 un->un_ncmds_in_driver--; 21093 ASSERT(un->un_ncmds_in_driver >= 0); 21094 mutex_exit(SD_MUTEX(un)); 21095 return (err); 21096 } 21097 } 21098 geom_validated = TRUE; 21099 } 21100 if ((un->un_f_geometry_is_valid == TRUE) && 21101 (un->un_solaris_size > 0)) { 21102 /* 21103 * the "geometry_is_valid" flag could be true if we 21104 * have an fdisk table but no Solaris partition 21105 */ 21106 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21107 /* it is EFI, so return ENOTSUP for these */ 21108 switch (cmd) { 21109 case DKIOCGAPART: 21110 case DKIOCGGEOM: 21111 case DKIOCGVTOC: 21112 case DKIOCSVTOC: 21113 case DKIOCSAPART: 21114 err = ENOTSUP; 21115 un->un_ncmds_in_driver--; 21116 ASSERT(un->un_ncmds_in_driver >= 0); 21117 mutex_exit(SD_MUTEX(un)); 21118 return (err); 21119 } 21120 } 21121 } 21122 21123 skip_ready_valid: 21124 mutex_exit(SD_MUTEX(un)); 21125 21126 switch (cmd) { 21127 case DKIOCINFO: 21128 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21129 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21130 break; 21131 21132 case DKIOCGMEDIAINFO: 21133 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21134 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21135 break; 21136 21137 case DKIOCGGEOM: 21138 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21139 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21140 geom_validated); 21141 break; 21142 21143 case DKIOCSGEOM: 21144 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21145 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21146 break; 21147 21148 case DKIOCGAPART: 21149 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21150 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21151 geom_validated); 21152 break; 21153 21154 case DKIOCSAPART: 21155 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21156 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21157 break; 21158 21159 case DKIOCGVTOC: 21160 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21161 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21162 geom_validated); 21163 break; 21164 21165 case DKIOCGETEFI: 21166 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21167 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21168 break; 21169 21170 case DKIOCPARTITION: 21171 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21172 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21173 break; 21174 21175 case DKIOCSVTOC: 21176 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21177 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21178 break; 21179 21180 case DKIOCSETEFI: 21181 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21182 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21183 break; 21184 21185 case DKIOCGMBOOT: 21186 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21187 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21188 break; 21189 21190 case DKIOCSMBOOT: 21191 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21192 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21193 break; 21194 21195 case DKIOCLOCK: 21196 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21197 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21198 SD_PATH_STANDARD); 21199 break; 21200 21201 case DKIOCUNLOCK: 21202 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21203 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21204 SD_PATH_STANDARD); 21205 break; 21206 21207 case DKIOCSTATE: { 21208 enum dkio_state state; 21209 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21210 21211 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21212 err = EFAULT; 21213 } else { 21214 err = sd_check_media(dev, state); 21215 if (err == 0) { 21216 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21217 sizeof (int), flag) != 0) 21218 err = EFAULT; 21219 } 21220 } 21221 break; 21222 } 21223 21224 case DKIOCREMOVABLE: 21225 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21226 /* 21227 * At present, vold only does automount for removable-media 21228 * devices, in order not to break current applications, we 21229 * still let hopluggable devices pretend to be removable media 21230 * devices for vold. In the near future, once vold is EOL'ed, 21231 * we should remove this workaround. 21232 */ 21233 if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) { 21234 i = 1; 21235 } else { 21236 i = 0; 21237 } 21238 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21239 err = EFAULT; 21240 } else { 21241 err = 0; 21242 } 21243 break; 21244 21245 case DKIOCHOTPLUGGABLE: 21246 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21247 if (un->un_f_is_hotpluggable) { 21248 i = 1; 21249 } else { 21250 i = 0; 21251 } 21252 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21253 err = EFAULT; 21254 } else { 21255 err = 0; 21256 } 21257 break; 21258 21259 case DKIOCGTEMPERATURE: 21260 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21261 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21262 break; 21263 21264 case MHIOCENFAILFAST: 21265 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21266 if ((err = drv_priv(cred_p)) == 0) { 21267 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21268 } 21269 break; 21270 21271 case MHIOCTKOWN: 21272 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21273 if ((err = drv_priv(cred_p)) == 0) { 21274 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21275 } 21276 break; 21277 21278 case MHIOCRELEASE: 21279 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21280 if ((err = drv_priv(cred_p)) == 0) { 21281 err = sd_mhdioc_release(dev); 21282 } 21283 break; 21284 21285 case MHIOCSTATUS: 21286 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21287 if ((err = drv_priv(cred_p)) == 0) { 21288 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21289 case 0: 21290 err = 0; 21291 break; 21292 case EACCES: 21293 *rval_p = 1; 21294 err = 0; 21295 break; 21296 default: 21297 err = EIO; 21298 break; 21299 } 21300 } 21301 break; 21302 21303 case MHIOCQRESERVE: 21304 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21305 if ((err = drv_priv(cred_p)) == 0) { 21306 err = sd_reserve_release(dev, SD_RESERVE); 21307 } 21308 break; 21309 21310 case MHIOCREREGISTERDEVID: 21311 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21312 if (drv_priv(cred_p) == EPERM) { 21313 err = EPERM; 21314 } else if (!un->un_f_devid_supported) { 21315 err = ENOTTY; 21316 } else { 21317 err = sd_mhdioc_register_devid(dev); 21318 } 21319 break; 21320 21321 case MHIOCGRP_INKEYS: 21322 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21323 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21324 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21325 err = ENOTSUP; 21326 } else { 21327 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21328 flag); 21329 } 21330 } 21331 break; 21332 21333 case MHIOCGRP_INRESV: 21334 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21335 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21336 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21337 err = ENOTSUP; 21338 } else { 21339 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21340 } 21341 } 21342 break; 21343 21344 case MHIOCGRP_REGISTER: 21345 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21346 if ((err = drv_priv(cred_p)) != EPERM) { 21347 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21348 err = ENOTSUP; 21349 } else if (arg != NULL) { 21350 mhioc_register_t reg; 21351 if (ddi_copyin((void *)arg, ®, 21352 sizeof (mhioc_register_t), flag) != 0) { 21353 err = EFAULT; 21354 } else { 21355 err = 21356 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21357 un, SD_SCSI3_REGISTER, 21358 (uchar_t *)®); 21359 } 21360 } 21361 } 21362 break; 21363 21364 case MHIOCGRP_RESERVE: 21365 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21366 if ((err = drv_priv(cred_p)) != EPERM) { 21367 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21368 err = ENOTSUP; 21369 } else if (arg != NULL) { 21370 mhioc_resv_desc_t resv_desc; 21371 if (ddi_copyin((void *)arg, &resv_desc, 21372 sizeof (mhioc_resv_desc_t), flag) != 0) { 21373 err = EFAULT; 21374 } else { 21375 err = 21376 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21377 un, SD_SCSI3_RESERVE, 21378 (uchar_t *)&resv_desc); 21379 } 21380 } 21381 } 21382 break; 21383 21384 case MHIOCGRP_PREEMPTANDABORT: 21385 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21386 if ((err = drv_priv(cred_p)) != EPERM) { 21387 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21388 err = ENOTSUP; 21389 } else if (arg != NULL) { 21390 mhioc_preemptandabort_t preempt_abort; 21391 if (ddi_copyin((void *)arg, &preempt_abort, 21392 sizeof (mhioc_preemptandabort_t), 21393 flag) != 0) { 21394 err = EFAULT; 21395 } else { 21396 err = 21397 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21398 un, SD_SCSI3_PREEMPTANDABORT, 21399 (uchar_t *)&preempt_abort); 21400 } 21401 } 21402 } 21403 break; 21404 21405 case MHIOCGRP_REGISTERANDIGNOREKEY: 21406 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21407 if ((err = drv_priv(cred_p)) != EPERM) { 21408 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21409 err = ENOTSUP; 21410 } else if (arg != NULL) { 21411 mhioc_registerandignorekey_t r_and_i; 21412 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21413 sizeof (mhioc_registerandignorekey_t), 21414 flag) != 0) { 21415 err = EFAULT; 21416 } else { 21417 err = 21418 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21419 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21420 (uchar_t *)&r_and_i); 21421 } 21422 } 21423 } 21424 break; 21425 21426 case USCSICMD: 21427 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21428 cr = ddi_get_cred(); 21429 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21430 err = EPERM; 21431 } else { 21432 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21433 } 21434 break; 21435 21436 case CDROMPAUSE: 21437 case CDROMRESUME: 21438 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21439 if (!ISCD(un)) { 21440 err = ENOTTY; 21441 } else { 21442 err = sr_pause_resume(dev, cmd); 21443 } 21444 break; 21445 21446 case CDROMPLAYMSF: 21447 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21448 if (!ISCD(un)) { 21449 err = ENOTTY; 21450 } else { 21451 err = sr_play_msf(dev, (caddr_t)arg, flag); 21452 } 21453 break; 21454 21455 case CDROMPLAYTRKIND: 21456 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21457 #if defined(__i386) || defined(__amd64) 21458 /* 21459 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21460 */ 21461 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21462 #else 21463 if (!ISCD(un)) { 21464 #endif 21465 err = ENOTTY; 21466 } else { 21467 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21468 } 21469 break; 21470 21471 case CDROMREADTOCHDR: 21472 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21473 if (!ISCD(un)) { 21474 err = ENOTTY; 21475 } else { 21476 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21477 } 21478 break; 21479 21480 case CDROMREADTOCENTRY: 21481 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21482 if (!ISCD(un)) { 21483 err = ENOTTY; 21484 } else { 21485 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21486 } 21487 break; 21488 21489 case CDROMSTOP: 21490 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21491 if (!ISCD(un)) { 21492 err = ENOTTY; 21493 } else { 21494 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21495 SD_PATH_STANDARD); 21496 } 21497 break; 21498 21499 case CDROMSTART: 21500 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21501 if (!ISCD(un)) { 21502 err = ENOTTY; 21503 } else { 21504 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21505 SD_PATH_STANDARD); 21506 } 21507 break; 21508 21509 case CDROMCLOSETRAY: 21510 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21511 if (!ISCD(un)) { 21512 err = ENOTTY; 21513 } else { 21514 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21515 SD_PATH_STANDARD); 21516 } 21517 break; 21518 21519 case FDEJECT: /* for eject command */ 21520 case DKIOCEJECT: 21521 case CDROMEJECT: 21522 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21523 if (!un->un_f_eject_media_supported) { 21524 err = ENOTTY; 21525 } else { 21526 err = sr_eject(dev); 21527 } 21528 break; 21529 21530 case CDROMVOLCTRL: 21531 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21532 if (!ISCD(un)) { 21533 err = ENOTTY; 21534 } else { 21535 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21536 } 21537 break; 21538 21539 case CDROMSUBCHNL: 21540 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21541 if (!ISCD(un)) { 21542 err = ENOTTY; 21543 } else { 21544 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21545 } 21546 break; 21547 21548 case CDROMREADMODE2: 21549 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21550 if (!ISCD(un)) { 21551 err = ENOTTY; 21552 } else if (un->un_f_cfg_is_atapi == TRUE) { 21553 /* 21554 * If the drive supports READ CD, use that instead of 21555 * switching the LBA size via a MODE SELECT 21556 * Block Descriptor 21557 */ 21558 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21559 } else { 21560 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21561 } 21562 break; 21563 21564 case CDROMREADMODE1: 21565 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21566 if (!ISCD(un)) { 21567 err = ENOTTY; 21568 } else { 21569 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21570 } 21571 break; 21572 21573 case CDROMREADOFFSET: 21574 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21575 if (!ISCD(un)) { 21576 err = ENOTTY; 21577 } else { 21578 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21579 flag); 21580 } 21581 break; 21582 21583 case CDROMSBLKMODE: 21584 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21585 /* 21586 * There is no means of changing block size in case of atapi 21587 * drives, thus return ENOTTY if drive type is atapi 21588 */ 21589 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21590 err = ENOTTY; 21591 } else if (un->un_f_mmc_cap == TRUE) { 21592 21593 /* 21594 * MMC Devices do not support changing the 21595 * logical block size 21596 * 21597 * Note: EINVAL is being returned instead of ENOTTY to 21598 * maintain consistancy with the original mmc 21599 * driver update. 21600 */ 21601 err = EINVAL; 21602 } else { 21603 mutex_enter(SD_MUTEX(un)); 21604 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21605 (un->un_ncmds_in_transport > 0)) { 21606 mutex_exit(SD_MUTEX(un)); 21607 err = EINVAL; 21608 } else { 21609 mutex_exit(SD_MUTEX(un)); 21610 err = sr_change_blkmode(dev, cmd, arg, flag); 21611 } 21612 } 21613 break; 21614 21615 case CDROMGBLKMODE: 21616 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21617 if (!ISCD(un)) { 21618 err = ENOTTY; 21619 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21620 (un->un_f_blockcount_is_valid != FALSE)) { 21621 /* 21622 * Drive is an ATAPI drive so return target block 21623 * size for ATAPI drives since we cannot change the 21624 * blocksize on ATAPI drives. Used primarily to detect 21625 * if an ATAPI cdrom is present. 21626 */ 21627 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21628 sizeof (int), flag) != 0) { 21629 err = EFAULT; 21630 } else { 21631 err = 0; 21632 } 21633 21634 } else { 21635 /* 21636 * Drive supports changing block sizes via a Mode 21637 * Select. 21638 */ 21639 err = sr_change_blkmode(dev, cmd, arg, flag); 21640 } 21641 break; 21642 21643 case CDROMGDRVSPEED: 21644 case CDROMSDRVSPEED: 21645 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21646 if (!ISCD(un)) { 21647 err = ENOTTY; 21648 } else if (un->un_f_mmc_cap == TRUE) { 21649 /* 21650 * Note: In the future the driver implementation 21651 * for getting and 21652 * setting cd speed should entail: 21653 * 1) If non-mmc try the Toshiba mode page 21654 * (sr_change_speed) 21655 * 2) If mmc but no support for Real Time Streaming try 21656 * the SET CD SPEED (0xBB) command 21657 * (sr_atapi_change_speed) 21658 * 3) If mmc and support for Real Time Streaming 21659 * try the GET PERFORMANCE and SET STREAMING 21660 * commands (not yet implemented, 4380808) 21661 */ 21662 /* 21663 * As per recent MMC spec, CD-ROM speed is variable 21664 * and changes with LBA. Since there is no such 21665 * things as drive speed now, fail this ioctl. 21666 * 21667 * Note: EINVAL is returned for consistancy of original 21668 * implementation which included support for getting 21669 * the drive speed of mmc devices but not setting 21670 * the drive speed. Thus EINVAL would be returned 21671 * if a set request was made for an mmc device. 21672 * We no longer support get or set speed for 21673 * mmc but need to remain consistant with regard 21674 * to the error code returned. 21675 */ 21676 err = EINVAL; 21677 } else if (un->un_f_cfg_is_atapi == TRUE) { 21678 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21679 } else { 21680 err = sr_change_speed(dev, cmd, arg, flag); 21681 } 21682 break; 21683 21684 case CDROMCDDA: 21685 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21686 if (!ISCD(un)) { 21687 err = ENOTTY; 21688 } else { 21689 err = sr_read_cdda(dev, (void *)arg, flag); 21690 } 21691 break; 21692 21693 case CDROMCDXA: 21694 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21695 if (!ISCD(un)) { 21696 err = ENOTTY; 21697 } else { 21698 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21699 } 21700 break; 21701 21702 case CDROMSUBCODE: 21703 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21704 if (!ISCD(un)) { 21705 err = ENOTTY; 21706 } else { 21707 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21708 } 21709 break; 21710 21711 case DKIOCPARTINFO: { 21712 /* 21713 * Return parameters describing the selected disk slice. 21714 * Note: this ioctl is for the intel platform only 21715 */ 21716 #if defined(__i386) || defined(__amd64) 21717 int part; 21718 21719 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21720 part = SDPART(dev); 21721 21722 /* don't check un_solaris_size for pN */ 21723 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21724 err = EIO; 21725 } else { 21726 struct part_info p; 21727 21728 p.p_start = (daddr_t)un->un_offset[part]; 21729 p.p_length = (int)un->un_map[part].dkl_nblk; 21730 #ifdef _MULTI_DATAMODEL 21731 switch (ddi_model_convert_from(flag & FMODELS)) { 21732 case DDI_MODEL_ILP32: 21733 { 21734 struct part_info32 p32; 21735 21736 p32.p_start = (daddr32_t)p.p_start; 21737 p32.p_length = p.p_length; 21738 if (ddi_copyout(&p32, (void *)arg, 21739 sizeof (p32), flag)) 21740 err = EFAULT; 21741 break; 21742 } 21743 21744 case DDI_MODEL_NONE: 21745 { 21746 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21747 flag)) 21748 err = EFAULT; 21749 break; 21750 } 21751 } 21752 #else /* ! _MULTI_DATAMODEL */ 21753 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21754 err = EFAULT; 21755 #endif /* _MULTI_DATAMODEL */ 21756 } 21757 #else 21758 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21759 err = ENOTTY; 21760 #endif 21761 break; 21762 } 21763 21764 case DKIOCG_PHYGEOM: { 21765 /* Return the driver's notion of the media physical geometry */ 21766 #if defined(__i386) || defined(__amd64) 21767 uint64_t capacity; 21768 struct dk_geom disk_geom; 21769 struct dk_geom *dkgp = &disk_geom; 21770 21771 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21772 mutex_enter(SD_MUTEX(un)); 21773 21774 if (un->un_g.dkg_nhead != 0 && 21775 un->un_g.dkg_nsect != 0) { 21776 /* 21777 * We succeeded in getting a geometry, but 21778 * right now it is being reported as just the 21779 * Solaris fdisk partition, just like for 21780 * DKIOCGGEOM. We need to change that to be 21781 * correct for the entire disk now. 21782 */ 21783 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21784 dkgp->dkg_acyl = 0; 21785 dkgp->dkg_ncyl = un->un_blockcount / 21786 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21787 } else { 21788 bzero(dkgp, sizeof (struct dk_geom)); 21789 /* 21790 * This disk does not have a Solaris VTOC 21791 * so we must present a physical geometry 21792 * that will remain consistent regardless 21793 * of how the disk is used. This will ensure 21794 * that the geometry does not change regardless 21795 * of the fdisk partition type (ie. EFI, FAT32, 21796 * Solaris, etc). 21797 */ 21798 if (ISCD(un)) { 21799 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21800 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21801 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21802 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21803 } else { 21804 /* 21805 * Invalid un_blockcount can generate invalid 21806 * dk_geom and may result in division by zero 21807 * system failure. Should make sure blockcount 21808 * is valid before using it here. 21809 */ 21810 if (un->un_f_blockcount_is_valid == FALSE) { 21811 mutex_exit(SD_MUTEX(un)); 21812 err = EIO; 21813 21814 break; 21815 } 21816 21817 /* 21818 * Refer to comments related to off-by-1 at the 21819 * header of this file 21820 */ 21821 if (!un->un_f_capacity_adjusted && 21822 !un->un_f_has_removable_media && 21823 !un->un_f_is_hotpluggable && 21824 (un->un_tgt_blocksize == 21825 un->un_sys_blocksize)) 21826 capacity = un->un_blockcount - 1; 21827 else 21828 capacity = un->un_blockcount; 21829 21830 sd_convert_geometry(capacity, dkgp); 21831 dkgp->dkg_acyl = 0; 21832 dkgp->dkg_ncyl = capacity / 21833 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21834 } 21835 } 21836 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21837 21838 if (ddi_copyout(dkgp, (void *)arg, 21839 sizeof (struct dk_geom), flag)) { 21840 mutex_exit(SD_MUTEX(un)); 21841 err = EFAULT; 21842 } else { 21843 mutex_exit(SD_MUTEX(un)); 21844 err = 0; 21845 } 21846 #else 21847 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21848 err = ENOTTY; 21849 #endif 21850 break; 21851 } 21852 21853 case DKIOCG_VIRTGEOM: { 21854 /* Return the driver's notion of the media's logical geometry */ 21855 #if defined(__i386) || defined(__amd64) 21856 struct dk_geom disk_geom; 21857 struct dk_geom *dkgp = &disk_geom; 21858 21859 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21860 mutex_enter(SD_MUTEX(un)); 21861 /* 21862 * If there is no HBA geometry available, or 21863 * if the HBA returned us something that doesn't 21864 * really fit into an Int 13/function 8 geometry 21865 * result, just fail the ioctl. See PSARC 1998/313. 21866 */ 21867 if (un->un_lgeom.g_nhead == 0 || 21868 un->un_lgeom.g_nsect == 0 || 21869 un->un_lgeom.g_ncyl > 1024) { 21870 mutex_exit(SD_MUTEX(un)); 21871 err = EINVAL; 21872 } else { 21873 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21874 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21875 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21876 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21877 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21878 21879 if (ddi_copyout(dkgp, (void *)arg, 21880 sizeof (struct dk_geom), flag)) { 21881 mutex_exit(SD_MUTEX(un)); 21882 err = EFAULT; 21883 } else { 21884 mutex_exit(SD_MUTEX(un)); 21885 err = 0; 21886 } 21887 } 21888 #else 21889 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21890 err = ENOTTY; 21891 #endif 21892 break; 21893 } 21894 #ifdef SDDEBUG 21895 /* RESET/ABORTS testing ioctls */ 21896 case DKIOCRESET: { 21897 int reset_level; 21898 21899 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21900 err = EFAULT; 21901 } else { 21902 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21903 "reset_level = 0x%lx\n", reset_level); 21904 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21905 err = 0; 21906 } else { 21907 err = EIO; 21908 } 21909 } 21910 break; 21911 } 21912 21913 case DKIOCABORT: 21914 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21915 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21916 err = 0; 21917 } else { 21918 err = EIO; 21919 } 21920 break; 21921 #endif 21922 21923 #ifdef SD_FAULT_INJECTION 21924 /* SDIOC FaultInjection testing ioctls */ 21925 case SDIOCSTART: 21926 case SDIOCSTOP: 21927 case SDIOCINSERTPKT: 21928 case SDIOCINSERTXB: 21929 case SDIOCINSERTUN: 21930 case SDIOCINSERTARQ: 21931 case SDIOCPUSH: 21932 case SDIOCRETRIEVE: 21933 case SDIOCRUN: 21934 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21935 "SDIOC detected cmd:0x%X:\n", cmd); 21936 /* call error generator */ 21937 sd_faultinjection_ioctl(cmd, arg, un); 21938 err = 0; 21939 break; 21940 21941 #endif /* SD_FAULT_INJECTION */ 21942 21943 case DKIOCFLUSHWRITECACHE: 21944 { 21945 struct dk_callback *dkc = (struct dk_callback *)arg; 21946 21947 mutex_enter(SD_MUTEX(un)); 21948 if (!un->un_f_sync_cache_supported || 21949 !un->un_f_write_cache_enabled) { 21950 err = un->un_f_sync_cache_supported ? 21951 0 : ENOTSUP; 21952 mutex_exit(SD_MUTEX(un)); 21953 if ((flag & FKIOCTL) && dkc != NULL && 21954 dkc->dkc_callback != NULL) { 21955 (*dkc->dkc_callback)(dkc->dkc_cookie, 21956 err); 21957 /* 21958 * Did callback and reported error. 21959 * Since we did a callback, ioctl 21960 * should return 0. 21961 */ 21962 err = 0; 21963 } 21964 break; 21965 } 21966 mutex_exit(SD_MUTEX(un)); 21967 21968 if ((flag & FKIOCTL) && dkc != NULL && 21969 dkc->dkc_callback != NULL) { 21970 /* async SYNC CACHE request */ 21971 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 21972 } else { 21973 /* synchronous SYNC CACHE request */ 21974 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21975 } 21976 } 21977 break; 21978 21979 case DKIOCGETWCE: { 21980 21981 int wce; 21982 21983 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 21984 break; 21985 } 21986 21987 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 21988 err = EFAULT; 21989 } 21990 break; 21991 } 21992 21993 case DKIOCSETWCE: { 21994 21995 int wce, sync_supported; 21996 21997 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 21998 err = EFAULT; 21999 break; 22000 } 22001 22002 /* 22003 * Synchronize multiple threads trying to enable 22004 * or disable the cache via the un_f_wcc_cv 22005 * condition variable. 22006 */ 22007 mutex_enter(SD_MUTEX(un)); 22008 22009 /* 22010 * Don't allow the cache to be enabled if the 22011 * config file has it disabled. 22012 */ 22013 if (un->un_f_opt_disable_cache && wce) { 22014 mutex_exit(SD_MUTEX(un)); 22015 err = EINVAL; 22016 break; 22017 } 22018 22019 /* 22020 * Wait for write cache change in progress 22021 * bit to be clear before proceeding. 22022 */ 22023 while (un->un_f_wcc_inprog) 22024 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22025 22026 un->un_f_wcc_inprog = 1; 22027 22028 if (un->un_f_write_cache_enabled && wce == 0) { 22029 /* 22030 * Disable the write cache. Don't clear 22031 * un_f_write_cache_enabled until after 22032 * the mode select and flush are complete. 22033 */ 22034 sync_supported = un->un_f_sync_cache_supported; 22035 mutex_exit(SD_MUTEX(un)); 22036 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22037 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22038 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22039 } 22040 22041 mutex_enter(SD_MUTEX(un)); 22042 if (err == 0) { 22043 un->un_f_write_cache_enabled = 0; 22044 } 22045 22046 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22047 /* 22048 * Set un_f_write_cache_enabled first, so there is 22049 * no window where the cache is enabled, but the 22050 * bit says it isn't. 22051 */ 22052 un->un_f_write_cache_enabled = 1; 22053 mutex_exit(SD_MUTEX(un)); 22054 22055 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22056 SD_CACHE_ENABLE); 22057 22058 mutex_enter(SD_MUTEX(un)); 22059 22060 if (err) { 22061 un->un_f_write_cache_enabled = 0; 22062 } 22063 } 22064 22065 un->un_f_wcc_inprog = 0; 22066 cv_broadcast(&un->un_wcc_cv); 22067 mutex_exit(SD_MUTEX(un)); 22068 break; 22069 } 22070 22071 default: 22072 err = ENOTTY; 22073 break; 22074 } 22075 mutex_enter(SD_MUTEX(un)); 22076 un->un_ncmds_in_driver--; 22077 ASSERT(un->un_ncmds_in_driver >= 0); 22078 mutex_exit(SD_MUTEX(un)); 22079 22080 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22081 return (err); 22082 } 22083 22084 22085 /* 22086 * Function: sd_uscsi_ioctl 22087 * 22088 * Description: This routine is the driver entry point for handling USCSI ioctl 22089 * requests (USCSICMD). 22090 * 22091 * Arguments: dev - the device number 22092 * arg - user provided scsi command 22093 * flag - this argument is a pass through to ddi_copyxxx() 22094 * directly from the mode argument of ioctl(). 22095 * 22096 * Return Code: code returned by sd_send_scsi_cmd 22097 * ENXIO 22098 * EFAULT 22099 * EAGAIN 22100 */ 22101 22102 static int 22103 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22104 { 22105 #ifdef _MULTI_DATAMODEL 22106 /* 22107 * For use when a 32 bit app makes a call into a 22108 * 64 bit ioctl 22109 */ 22110 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22111 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22112 model_t model; 22113 #endif /* _MULTI_DATAMODEL */ 22114 struct uscsi_cmd *scmd = NULL; 22115 struct sd_lun *un = NULL; 22116 enum uio_seg uioseg; 22117 char cdb[CDB_GROUP0]; 22118 int rval = 0; 22119 22120 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22121 return (ENXIO); 22122 } 22123 22124 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22125 22126 scmd = (struct uscsi_cmd *) 22127 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22128 22129 #ifdef _MULTI_DATAMODEL 22130 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22131 case DDI_MODEL_ILP32: 22132 { 22133 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22134 rval = EFAULT; 22135 goto done; 22136 } 22137 /* 22138 * Convert the ILP32 uscsi data from the 22139 * application to LP64 for internal use. 22140 */ 22141 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22142 break; 22143 } 22144 case DDI_MODEL_NONE: 22145 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22146 rval = EFAULT; 22147 goto done; 22148 } 22149 break; 22150 } 22151 #else /* ! _MULTI_DATAMODEL */ 22152 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22153 rval = EFAULT; 22154 goto done; 22155 } 22156 #endif /* _MULTI_DATAMODEL */ 22157 22158 scmd->uscsi_flags &= ~USCSI_NOINTR; 22159 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22160 if (un->un_f_format_in_progress == TRUE) { 22161 rval = EAGAIN; 22162 goto done; 22163 } 22164 22165 /* 22166 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22167 * we will have a valid cdb[0] to test. 22168 */ 22169 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22170 (cdb[0] == SCMD_FORMAT)) { 22171 SD_TRACE(SD_LOG_IOCTL, un, 22172 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22173 mutex_enter(SD_MUTEX(un)); 22174 un->un_f_format_in_progress = TRUE; 22175 mutex_exit(SD_MUTEX(un)); 22176 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22177 SD_PATH_STANDARD); 22178 mutex_enter(SD_MUTEX(un)); 22179 un->un_f_format_in_progress = FALSE; 22180 mutex_exit(SD_MUTEX(un)); 22181 } else { 22182 SD_TRACE(SD_LOG_IOCTL, un, 22183 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22184 /* 22185 * It's OK to fall into here even if the ddi_copyin() 22186 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22187 * does this same copyin and will return the EFAULT 22188 * if it fails. 22189 */ 22190 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22191 SD_PATH_STANDARD); 22192 } 22193 #ifdef _MULTI_DATAMODEL 22194 switch (model) { 22195 case DDI_MODEL_ILP32: 22196 /* 22197 * Convert back to ILP32 before copyout to the 22198 * application 22199 */ 22200 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22201 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22202 if (rval != 0) { 22203 rval = EFAULT; 22204 } 22205 } 22206 break; 22207 case DDI_MODEL_NONE: 22208 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22209 if (rval != 0) { 22210 rval = EFAULT; 22211 } 22212 } 22213 break; 22214 } 22215 #else /* ! _MULTI_DATAMODE */ 22216 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22217 if (rval != 0) { 22218 rval = EFAULT; 22219 } 22220 } 22221 #endif /* _MULTI_DATAMODE */ 22222 done: 22223 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22224 22225 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22226 22227 return (rval); 22228 } 22229 22230 22231 /* 22232 * Function: sd_dkio_ctrl_info 22233 * 22234 * Description: This routine is the driver entry point for handling controller 22235 * information ioctl requests (DKIOCINFO). 22236 * 22237 * Arguments: dev - the device number 22238 * arg - pointer to user provided dk_cinfo structure 22239 * specifying the controller type and attributes. 22240 * flag - this argument is a pass through to ddi_copyxxx() 22241 * directly from the mode argument of ioctl(). 22242 * 22243 * Return Code: 0 22244 * EFAULT 22245 * ENXIO 22246 */ 22247 22248 static int 22249 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22250 { 22251 struct sd_lun *un = NULL; 22252 struct dk_cinfo *info; 22253 dev_info_t *pdip; 22254 int lun, tgt; 22255 22256 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22257 return (ENXIO); 22258 } 22259 22260 info = (struct dk_cinfo *) 22261 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22262 22263 switch (un->un_ctype) { 22264 case CTYPE_CDROM: 22265 info->dki_ctype = DKC_CDROM; 22266 break; 22267 default: 22268 info->dki_ctype = DKC_SCSI_CCS; 22269 break; 22270 } 22271 pdip = ddi_get_parent(SD_DEVINFO(un)); 22272 info->dki_cnum = ddi_get_instance(pdip); 22273 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22274 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22275 } else { 22276 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22277 DK_DEVLEN - 1); 22278 } 22279 22280 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22281 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22282 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22283 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22284 22285 /* Unit Information */ 22286 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22287 info->dki_slave = ((tgt << 3) | lun); 22288 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22289 DK_DEVLEN - 1); 22290 info->dki_flags = DKI_FMTVOL; 22291 info->dki_partition = SDPART(dev); 22292 22293 /* Max Transfer size of this device in blocks */ 22294 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22295 info->dki_addr = 0; 22296 info->dki_space = 0; 22297 info->dki_prio = 0; 22298 info->dki_vec = 0; 22299 22300 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22301 kmem_free(info, sizeof (struct dk_cinfo)); 22302 return (EFAULT); 22303 } else { 22304 kmem_free(info, sizeof (struct dk_cinfo)); 22305 return (0); 22306 } 22307 } 22308 22309 22310 /* 22311 * Function: sd_get_media_info 22312 * 22313 * Description: This routine is the driver entry point for handling ioctl 22314 * requests for the media type or command set profile used by the 22315 * drive to operate on the media (DKIOCGMEDIAINFO). 22316 * 22317 * Arguments: dev - the device number 22318 * arg - pointer to user provided dk_minfo structure 22319 * specifying the media type, logical block size and 22320 * drive capacity. 22321 * flag - this argument is a pass through to ddi_copyxxx() 22322 * directly from the mode argument of ioctl(). 22323 * 22324 * Return Code: 0 22325 * EACCESS 22326 * EFAULT 22327 * ENXIO 22328 * EIO 22329 */ 22330 22331 static int 22332 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22333 { 22334 struct sd_lun *un = NULL; 22335 struct uscsi_cmd com; 22336 struct scsi_inquiry *sinq; 22337 struct dk_minfo media_info; 22338 u_longlong_t media_capacity; 22339 uint64_t capacity; 22340 uint_t lbasize; 22341 uchar_t *out_data; 22342 uchar_t *rqbuf; 22343 int rval = 0; 22344 int rtn; 22345 22346 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22347 (un->un_state == SD_STATE_OFFLINE)) { 22348 return (ENXIO); 22349 } 22350 22351 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22352 22353 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22354 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22355 22356 /* Issue a TUR to determine if the drive is ready with media present */ 22357 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22358 if (rval == ENXIO) { 22359 goto done; 22360 } 22361 22362 /* Now get configuration data */ 22363 if (ISCD(un)) { 22364 media_info.dki_media_type = DK_CDROM; 22365 22366 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22367 if (un->un_f_mmc_cap == TRUE) { 22368 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22369 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22370 22371 if (rtn) { 22372 /* 22373 * Failed for other than an illegal request 22374 * or command not supported 22375 */ 22376 if ((com.uscsi_status == STATUS_CHECK) && 22377 (com.uscsi_rqstatus == STATUS_GOOD)) { 22378 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22379 (rqbuf[12] != 0x20)) { 22380 rval = EIO; 22381 goto done; 22382 } 22383 } 22384 } else { 22385 /* 22386 * The GET CONFIGURATION command succeeded 22387 * so set the media type according to the 22388 * returned data 22389 */ 22390 media_info.dki_media_type = out_data[6]; 22391 media_info.dki_media_type <<= 8; 22392 media_info.dki_media_type |= out_data[7]; 22393 } 22394 } 22395 } else { 22396 /* 22397 * The profile list is not available, so we attempt to identify 22398 * the media type based on the inquiry data 22399 */ 22400 sinq = un->un_sd->sd_inq; 22401 if (sinq->inq_qual == 0) { 22402 /* This is a direct access device */ 22403 media_info.dki_media_type = DK_FIXED_DISK; 22404 22405 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22406 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22407 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22408 media_info.dki_media_type = DK_ZIP; 22409 } else if ( 22410 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22411 media_info.dki_media_type = DK_JAZ; 22412 } 22413 } 22414 } else { 22415 /* Not a CD or direct access so return unknown media */ 22416 media_info.dki_media_type = DK_UNKNOWN; 22417 } 22418 } 22419 22420 /* Now read the capacity so we can provide the lbasize and capacity */ 22421 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22422 SD_PATH_DIRECT)) { 22423 case 0: 22424 break; 22425 case EACCES: 22426 rval = EACCES; 22427 goto done; 22428 default: 22429 rval = EIO; 22430 goto done; 22431 } 22432 22433 media_info.dki_lbsize = lbasize; 22434 media_capacity = capacity; 22435 22436 /* 22437 * sd_send_scsi_READ_CAPACITY() reports capacity in 22438 * un->un_sys_blocksize chunks. So we need to convert it into 22439 * cap.lbasize chunks. 22440 */ 22441 media_capacity *= un->un_sys_blocksize; 22442 media_capacity /= lbasize; 22443 media_info.dki_capacity = media_capacity; 22444 22445 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22446 rval = EFAULT; 22447 /* Put goto. Anybody might add some code below in future */ 22448 goto done; 22449 } 22450 done: 22451 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22452 kmem_free(rqbuf, SENSE_LENGTH); 22453 return (rval); 22454 } 22455 22456 22457 /* 22458 * Function: sd_dkio_get_geometry 22459 * 22460 * Description: This routine is the driver entry point for handling user 22461 * requests to get the device geometry (DKIOCGGEOM). 22462 * 22463 * Arguments: dev - the device number 22464 * arg - pointer to user provided dk_geom structure specifying 22465 * the controller's notion of the current geometry. 22466 * flag - this argument is a pass through to ddi_copyxxx() 22467 * directly from the mode argument of ioctl(). 22468 * geom_validated - flag indicating if the device geometry has been 22469 * previously validated in the sdioctl routine. 22470 * 22471 * Return Code: 0 22472 * EFAULT 22473 * ENXIO 22474 * EIO 22475 */ 22476 22477 static int 22478 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22479 { 22480 struct sd_lun *un = NULL; 22481 struct dk_geom *tmp_geom = NULL; 22482 int rval = 0; 22483 22484 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22485 return (ENXIO); 22486 } 22487 22488 if (geom_validated == FALSE) { 22489 /* 22490 * sd_validate_geometry does not spin a disk up 22491 * if it was spun down. We need to make sure it 22492 * is ready. 22493 */ 22494 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22495 return (rval); 22496 } 22497 mutex_enter(SD_MUTEX(un)); 22498 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22499 mutex_exit(SD_MUTEX(un)); 22500 } 22501 if (rval) 22502 return (rval); 22503 22504 /* 22505 * It is possible that un_solaris_size is 0(uninitialized) 22506 * after sd_unit_attach. Reservation conflict may cause the 22507 * above situation. Thus, the zero check of un_solaris_size 22508 * should occur after the sd_validate_geometry() call. 22509 */ 22510 #if defined(__i386) || defined(__amd64) 22511 if (un->un_solaris_size == 0) { 22512 return (EIO); 22513 } 22514 #endif 22515 22516 /* 22517 * Make a local copy of the soft state geometry to avoid some potential 22518 * race conditions associated with holding the mutex and updating the 22519 * write_reinstruct value 22520 */ 22521 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22522 mutex_enter(SD_MUTEX(un)); 22523 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22524 mutex_exit(SD_MUTEX(un)); 22525 22526 if (tmp_geom->dkg_write_reinstruct == 0) { 22527 tmp_geom->dkg_write_reinstruct = 22528 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22529 sd_rot_delay) / (int)60000); 22530 } 22531 22532 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22533 flag); 22534 if (rval != 0) { 22535 rval = EFAULT; 22536 } 22537 22538 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22539 return (rval); 22540 22541 } 22542 22543 22544 /* 22545 * Function: sd_dkio_set_geometry 22546 * 22547 * Description: This routine is the driver entry point for handling user 22548 * requests to set the device geometry (DKIOCSGEOM). The actual 22549 * device geometry is not updated, just the driver "notion" of it. 22550 * 22551 * Arguments: dev - the device number 22552 * arg - pointer to user provided dk_geom structure used to set 22553 * the controller's notion of the current geometry. 22554 * flag - this argument is a pass through to ddi_copyxxx() 22555 * directly from the mode argument of ioctl(). 22556 * 22557 * Return Code: 0 22558 * EFAULT 22559 * ENXIO 22560 * EIO 22561 */ 22562 22563 static int 22564 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22565 { 22566 struct sd_lun *un = NULL; 22567 struct dk_geom *tmp_geom; 22568 struct dk_map *lp; 22569 int rval = 0; 22570 int i; 22571 22572 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22573 return (ENXIO); 22574 } 22575 22576 /* 22577 * Make sure there is no reservation conflict on the lun. 22578 */ 22579 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22580 return (EACCES); 22581 } 22582 22583 #if defined(__i386) || defined(__amd64) 22584 if (un->un_solaris_size == 0) { 22585 return (EIO); 22586 } 22587 #endif 22588 22589 /* 22590 * We need to copy the user specified geometry into local 22591 * storage and then update the softstate. We don't want to hold 22592 * the mutex and copyin directly from the user to the soft state 22593 */ 22594 tmp_geom = (struct dk_geom *) 22595 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22596 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22597 if (rval != 0) { 22598 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22599 return (EFAULT); 22600 } 22601 22602 mutex_enter(SD_MUTEX(un)); 22603 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22604 for (i = 0; i < NDKMAP; i++) { 22605 lp = &un->un_map[i]; 22606 un->un_offset[i] = 22607 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22608 #if defined(__i386) || defined(__amd64) 22609 un->un_offset[i] += un->un_solaris_offset; 22610 #endif 22611 } 22612 un->un_f_geometry_is_valid = FALSE; 22613 mutex_exit(SD_MUTEX(un)); 22614 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22615 22616 return (rval); 22617 } 22618 22619 22620 /* 22621 * Function: sd_dkio_get_partition 22622 * 22623 * Description: This routine is the driver entry point for handling user 22624 * requests to get the partition table (DKIOCGAPART). 22625 * 22626 * Arguments: dev - the device number 22627 * arg - pointer to user provided dk_allmap structure specifying 22628 * the controller's notion of the current partition table. 22629 * flag - this argument is a pass through to ddi_copyxxx() 22630 * directly from the mode argument of ioctl(). 22631 * geom_validated - flag indicating if the device geometry has been 22632 * previously validated in the sdioctl routine. 22633 * 22634 * Return Code: 0 22635 * EFAULT 22636 * ENXIO 22637 * EIO 22638 */ 22639 22640 static int 22641 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22642 { 22643 struct sd_lun *un = NULL; 22644 int rval = 0; 22645 int size; 22646 22647 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22648 return (ENXIO); 22649 } 22650 22651 /* 22652 * Make sure the geometry is valid before getting the partition 22653 * information. 22654 */ 22655 mutex_enter(SD_MUTEX(un)); 22656 if (geom_validated == FALSE) { 22657 /* 22658 * sd_validate_geometry does not spin a disk up 22659 * if it was spun down. We need to make sure it 22660 * is ready before validating the geometry. 22661 */ 22662 mutex_exit(SD_MUTEX(un)); 22663 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22664 return (rval); 22665 } 22666 mutex_enter(SD_MUTEX(un)); 22667 22668 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22669 mutex_exit(SD_MUTEX(un)); 22670 return (rval); 22671 } 22672 } 22673 mutex_exit(SD_MUTEX(un)); 22674 22675 /* 22676 * It is possible that un_solaris_size is 0(uninitialized) 22677 * after sd_unit_attach. Reservation conflict may cause the 22678 * above situation. Thus, the zero check of un_solaris_size 22679 * should occur after the sd_validate_geometry() call. 22680 */ 22681 #if defined(__i386) || defined(__amd64) 22682 if (un->un_solaris_size == 0) { 22683 return (EIO); 22684 } 22685 #endif 22686 22687 #ifdef _MULTI_DATAMODEL 22688 switch (ddi_model_convert_from(flag & FMODELS)) { 22689 case DDI_MODEL_ILP32: { 22690 struct dk_map32 dk_map32[NDKMAP]; 22691 int i; 22692 22693 for (i = 0; i < NDKMAP; i++) { 22694 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22695 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22696 } 22697 size = NDKMAP * sizeof (struct dk_map32); 22698 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22699 if (rval != 0) { 22700 rval = EFAULT; 22701 } 22702 break; 22703 } 22704 case DDI_MODEL_NONE: 22705 size = NDKMAP * sizeof (struct dk_map); 22706 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22707 if (rval != 0) { 22708 rval = EFAULT; 22709 } 22710 break; 22711 } 22712 #else /* ! _MULTI_DATAMODEL */ 22713 size = NDKMAP * sizeof (struct dk_map); 22714 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22715 if (rval != 0) { 22716 rval = EFAULT; 22717 } 22718 #endif /* _MULTI_DATAMODEL */ 22719 return (rval); 22720 } 22721 22722 22723 /* 22724 * Function: sd_dkio_set_partition 22725 * 22726 * Description: This routine is the driver entry point for handling user 22727 * requests to set the partition table (DKIOCSAPART). The actual 22728 * device partition is not updated. 22729 * 22730 * Arguments: dev - the device number 22731 * arg - pointer to user provided dk_allmap structure used to set 22732 * the controller's notion of the partition table. 22733 * flag - this argument is a pass through to ddi_copyxxx() 22734 * directly from the mode argument of ioctl(). 22735 * 22736 * Return Code: 0 22737 * EINVAL 22738 * EFAULT 22739 * ENXIO 22740 * EIO 22741 */ 22742 22743 static int 22744 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22745 { 22746 struct sd_lun *un = NULL; 22747 struct dk_map dk_map[NDKMAP]; 22748 struct dk_map *lp; 22749 int rval = 0; 22750 int size; 22751 int i; 22752 #if defined(_SUNOS_VTOC_16) 22753 struct dkl_partition *vp; 22754 #endif 22755 22756 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22757 return (ENXIO); 22758 } 22759 22760 /* 22761 * Set the map for all logical partitions. We lock 22762 * the priority just to make sure an interrupt doesn't 22763 * come in while the map is half updated. 22764 */ 22765 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22766 mutex_enter(SD_MUTEX(un)); 22767 if (un->un_blockcount > DK_MAX_BLOCKS) { 22768 mutex_exit(SD_MUTEX(un)); 22769 return (ENOTSUP); 22770 } 22771 mutex_exit(SD_MUTEX(un)); 22772 22773 /* 22774 * Make sure there is no reservation conflict on the lun. 22775 */ 22776 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22777 return (EACCES); 22778 } 22779 22780 #if defined(__i386) || defined(__amd64) 22781 if (un->un_solaris_size == 0) { 22782 return (EIO); 22783 } 22784 #endif 22785 22786 #ifdef _MULTI_DATAMODEL 22787 switch (ddi_model_convert_from(flag & FMODELS)) { 22788 case DDI_MODEL_ILP32: { 22789 struct dk_map32 dk_map32[NDKMAP]; 22790 22791 size = NDKMAP * sizeof (struct dk_map32); 22792 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22793 if (rval != 0) { 22794 return (EFAULT); 22795 } 22796 for (i = 0; i < NDKMAP; i++) { 22797 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22798 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22799 } 22800 break; 22801 } 22802 case DDI_MODEL_NONE: 22803 size = NDKMAP * sizeof (struct dk_map); 22804 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22805 if (rval != 0) { 22806 return (EFAULT); 22807 } 22808 break; 22809 } 22810 #else /* ! _MULTI_DATAMODEL */ 22811 size = NDKMAP * sizeof (struct dk_map); 22812 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22813 if (rval != 0) { 22814 return (EFAULT); 22815 } 22816 #endif /* _MULTI_DATAMODEL */ 22817 22818 mutex_enter(SD_MUTEX(un)); 22819 /* Note: The size used in this bcopy is set based upon the data model */ 22820 bcopy(dk_map, un->un_map, size); 22821 #if defined(_SUNOS_VTOC_16) 22822 vp = (struct dkl_partition *)&(un->un_vtoc); 22823 #endif /* defined(_SUNOS_VTOC_16) */ 22824 for (i = 0; i < NDKMAP; i++) { 22825 lp = &un->un_map[i]; 22826 un->un_offset[i] = 22827 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22828 #if defined(_SUNOS_VTOC_16) 22829 vp->p_start = un->un_offset[i]; 22830 vp->p_size = lp->dkl_nblk; 22831 vp++; 22832 #endif /* defined(_SUNOS_VTOC_16) */ 22833 #if defined(__i386) || defined(__amd64) 22834 un->un_offset[i] += un->un_solaris_offset; 22835 #endif 22836 } 22837 mutex_exit(SD_MUTEX(un)); 22838 return (rval); 22839 } 22840 22841 22842 /* 22843 * Function: sd_dkio_get_vtoc 22844 * 22845 * Description: This routine is the driver entry point for handling user 22846 * requests to get the current volume table of contents 22847 * (DKIOCGVTOC). 22848 * 22849 * Arguments: dev - the device number 22850 * arg - pointer to user provided vtoc structure specifying 22851 * the current vtoc. 22852 * flag - this argument is a pass through to ddi_copyxxx() 22853 * directly from the mode argument of ioctl(). 22854 * geom_validated - flag indicating if the device geometry has been 22855 * previously validated in the sdioctl routine. 22856 * 22857 * Return Code: 0 22858 * EFAULT 22859 * ENXIO 22860 * EIO 22861 */ 22862 22863 static int 22864 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22865 { 22866 struct sd_lun *un = NULL; 22867 #if defined(_SUNOS_VTOC_8) 22868 struct vtoc user_vtoc; 22869 #endif /* defined(_SUNOS_VTOC_8) */ 22870 int rval = 0; 22871 22872 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22873 return (ENXIO); 22874 } 22875 22876 mutex_enter(SD_MUTEX(un)); 22877 if (geom_validated == FALSE) { 22878 /* 22879 * sd_validate_geometry does not spin a disk up 22880 * if it was spun down. We need to make sure it 22881 * is ready. 22882 */ 22883 mutex_exit(SD_MUTEX(un)); 22884 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22885 return (rval); 22886 } 22887 mutex_enter(SD_MUTEX(un)); 22888 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22889 mutex_exit(SD_MUTEX(un)); 22890 return (rval); 22891 } 22892 } 22893 22894 #if defined(_SUNOS_VTOC_8) 22895 sd_build_user_vtoc(un, &user_vtoc); 22896 mutex_exit(SD_MUTEX(un)); 22897 22898 #ifdef _MULTI_DATAMODEL 22899 switch (ddi_model_convert_from(flag & FMODELS)) { 22900 case DDI_MODEL_ILP32: { 22901 struct vtoc32 user_vtoc32; 22902 22903 vtoctovtoc32(user_vtoc, user_vtoc32); 22904 if (ddi_copyout(&user_vtoc32, (void *)arg, 22905 sizeof (struct vtoc32), flag)) { 22906 return (EFAULT); 22907 } 22908 break; 22909 } 22910 22911 case DDI_MODEL_NONE: 22912 if (ddi_copyout(&user_vtoc, (void *)arg, 22913 sizeof (struct vtoc), flag)) { 22914 return (EFAULT); 22915 } 22916 break; 22917 } 22918 #else /* ! _MULTI_DATAMODEL */ 22919 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22920 return (EFAULT); 22921 } 22922 #endif /* _MULTI_DATAMODEL */ 22923 22924 #elif defined(_SUNOS_VTOC_16) 22925 mutex_exit(SD_MUTEX(un)); 22926 22927 #ifdef _MULTI_DATAMODEL 22928 /* 22929 * The un_vtoc structure is a "struct dk_vtoc" which is always 22930 * 32-bit to maintain compatibility with existing on-disk 22931 * structures. Thus, we need to convert the structure when copying 22932 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22933 * program. If the target is a 32-bit program, then no conversion 22934 * is necessary. 22935 */ 22936 /* LINTED: logical expression always true: op "||" */ 22937 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22938 switch (ddi_model_convert_from(flag & FMODELS)) { 22939 case DDI_MODEL_ILP32: 22940 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22941 sizeof (un->un_vtoc), flag)) { 22942 return (EFAULT); 22943 } 22944 break; 22945 22946 case DDI_MODEL_NONE: { 22947 struct vtoc user_vtoc; 22948 22949 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22950 if (ddi_copyout(&user_vtoc, (void *)arg, 22951 sizeof (struct vtoc), flag)) { 22952 return (EFAULT); 22953 } 22954 break; 22955 } 22956 } 22957 #else /* ! _MULTI_DATAMODEL */ 22958 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22959 flag)) { 22960 return (EFAULT); 22961 } 22962 #endif /* _MULTI_DATAMODEL */ 22963 #else 22964 #error "No VTOC format defined." 22965 #endif 22966 22967 return (rval); 22968 } 22969 22970 static int 22971 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22972 { 22973 struct sd_lun *un = NULL; 22974 dk_efi_t user_efi; 22975 int rval = 0; 22976 void *buffer; 22977 22978 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22979 return (ENXIO); 22980 22981 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22982 return (EFAULT); 22983 22984 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22985 22986 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22987 (user_efi.dki_length > un->un_max_xfer_size)) 22988 return (EINVAL); 22989 22990 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22991 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22992 user_efi.dki_lba, SD_PATH_DIRECT); 22993 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22994 user_efi.dki_length, flag) != 0) 22995 rval = EFAULT; 22996 22997 kmem_free(buffer, user_efi.dki_length); 22998 return (rval); 22999 } 23000 23001 /* 23002 * Function: sd_build_user_vtoc 23003 * 23004 * Description: This routine populates a pass by reference variable with the 23005 * current volume table of contents. 23006 * 23007 * Arguments: un - driver soft state (unit) structure 23008 * user_vtoc - pointer to vtoc structure to be populated 23009 */ 23010 23011 static void 23012 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23013 { 23014 struct dk_map2 *lpart; 23015 struct dk_map *lmap; 23016 struct partition *vpart; 23017 int nblks; 23018 int i; 23019 23020 ASSERT(mutex_owned(SD_MUTEX(un))); 23021 23022 /* 23023 * Return vtoc structure fields in the provided VTOC area, addressed 23024 * by *vtoc. 23025 */ 23026 bzero(user_vtoc, sizeof (struct vtoc)); 23027 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23028 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23029 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23030 user_vtoc->v_sanity = VTOC_SANE; 23031 user_vtoc->v_version = un->un_vtoc.v_version; 23032 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23033 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23034 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23035 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23036 sizeof (un->un_vtoc.v_reserved)); 23037 /* 23038 * Convert partitioning information. 23039 * 23040 * Note the conversion from starting cylinder number 23041 * to starting sector number. 23042 */ 23043 lmap = un->un_map; 23044 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23045 vpart = user_vtoc->v_part; 23046 23047 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23048 23049 for (i = 0; i < V_NUMPAR; i++) { 23050 vpart->p_tag = lpart->p_tag; 23051 vpart->p_flag = lpart->p_flag; 23052 vpart->p_start = lmap->dkl_cylno * nblks; 23053 vpart->p_size = lmap->dkl_nblk; 23054 lmap++; 23055 lpart++; 23056 vpart++; 23057 23058 /* (4364927) */ 23059 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23060 } 23061 23062 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23063 } 23064 23065 static int 23066 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23067 { 23068 struct sd_lun *un = NULL; 23069 struct partition64 p64; 23070 int rval = 0; 23071 uint_t nparts; 23072 efi_gpe_t *partitions; 23073 efi_gpt_t *buffer; 23074 diskaddr_t gpe_lba; 23075 23076 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23077 return (ENXIO); 23078 } 23079 23080 if (ddi_copyin((const void *)arg, &p64, 23081 sizeof (struct partition64), flag)) { 23082 return (EFAULT); 23083 } 23084 23085 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23086 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23087 1, SD_PATH_DIRECT); 23088 if (rval != 0) 23089 goto done_error; 23090 23091 sd_swap_efi_gpt(buffer); 23092 23093 if ((rval = sd_validate_efi(buffer)) != 0) 23094 goto done_error; 23095 23096 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23097 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23098 if (p64.p_partno > nparts) { 23099 /* couldn't find it */ 23100 rval = ESRCH; 23101 goto done_error; 23102 } 23103 /* 23104 * if we're dealing with a partition that's out of the normal 23105 * 16K block, adjust accordingly 23106 */ 23107 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23108 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23109 gpe_lba, SD_PATH_DIRECT); 23110 if (rval) { 23111 goto done_error; 23112 } 23113 partitions = (efi_gpe_t *)buffer; 23114 23115 sd_swap_efi_gpe(nparts, partitions); 23116 23117 partitions += p64.p_partno; 23118 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23119 sizeof (struct uuid)); 23120 p64.p_start = partitions->efi_gpe_StartingLBA; 23121 p64.p_size = partitions->efi_gpe_EndingLBA - 23122 p64.p_start + 1; 23123 23124 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23125 rval = EFAULT; 23126 23127 done_error: 23128 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23129 return (rval); 23130 } 23131 23132 23133 /* 23134 * Function: sd_dkio_set_vtoc 23135 * 23136 * Description: This routine is the driver entry point for handling user 23137 * requests to set the current volume table of contents 23138 * (DKIOCSVTOC). 23139 * 23140 * Arguments: dev - the device number 23141 * arg - pointer to user provided vtoc structure used to set the 23142 * current vtoc. 23143 * flag - this argument is a pass through to ddi_copyxxx() 23144 * directly from the mode argument of ioctl(). 23145 * 23146 * Return Code: 0 23147 * EFAULT 23148 * ENXIO 23149 * EINVAL 23150 * ENOTSUP 23151 */ 23152 23153 static int 23154 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23155 { 23156 struct sd_lun *un = NULL; 23157 struct vtoc user_vtoc; 23158 int rval = 0; 23159 23160 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23161 return (ENXIO); 23162 } 23163 23164 #if defined(__i386) || defined(__amd64) 23165 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23166 return (EINVAL); 23167 } 23168 #endif 23169 23170 #ifdef _MULTI_DATAMODEL 23171 switch (ddi_model_convert_from(flag & FMODELS)) { 23172 case DDI_MODEL_ILP32: { 23173 struct vtoc32 user_vtoc32; 23174 23175 if (ddi_copyin((const void *)arg, &user_vtoc32, 23176 sizeof (struct vtoc32), flag)) { 23177 return (EFAULT); 23178 } 23179 vtoc32tovtoc(user_vtoc32, user_vtoc); 23180 break; 23181 } 23182 23183 case DDI_MODEL_NONE: 23184 if (ddi_copyin((const void *)arg, &user_vtoc, 23185 sizeof (struct vtoc), flag)) { 23186 return (EFAULT); 23187 } 23188 break; 23189 } 23190 #else /* ! _MULTI_DATAMODEL */ 23191 if (ddi_copyin((const void *)arg, &user_vtoc, 23192 sizeof (struct vtoc), flag)) { 23193 return (EFAULT); 23194 } 23195 #endif /* _MULTI_DATAMODEL */ 23196 23197 mutex_enter(SD_MUTEX(un)); 23198 if (un->un_blockcount > DK_MAX_BLOCKS) { 23199 mutex_exit(SD_MUTEX(un)); 23200 return (ENOTSUP); 23201 } 23202 if (un->un_g.dkg_ncyl == 0) { 23203 mutex_exit(SD_MUTEX(un)); 23204 return (EINVAL); 23205 } 23206 23207 mutex_exit(SD_MUTEX(un)); 23208 sd_clear_efi(un); 23209 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23210 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23211 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23212 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23213 un->un_node_type, NULL); 23214 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23215 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23216 un->un_node_type, NULL); 23217 mutex_enter(SD_MUTEX(un)); 23218 23219 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23220 if ((rval = sd_write_label(dev)) == 0) { 23221 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23222 != 0) { 23223 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23224 "sd_dkio_set_vtoc: " 23225 "Failed validate geometry\n"); 23226 } 23227 } 23228 } 23229 23230 /* 23231 * If sd_build_label_vtoc, or sd_write_label failed above write the 23232 * devid anyway, what can it hurt? Also preserve the device id by 23233 * writing to the disk acyl for the case where a devid has been 23234 * fabricated. 23235 */ 23236 if (un->un_f_devid_supported && 23237 (un->un_f_opt_fab_devid == TRUE)) { 23238 if (un->un_devid == NULL) { 23239 sd_register_devid(un, SD_DEVINFO(un), 23240 SD_TARGET_IS_UNRESERVED); 23241 } else { 23242 /* 23243 * The device id for this disk has been 23244 * fabricated. Fabricated device id's are 23245 * managed by storing them in the last 2 23246 * available sectors on the drive. The device 23247 * id must be preserved by writing it back out 23248 * to this location. 23249 */ 23250 if (sd_write_deviceid(un) != 0) { 23251 ddi_devid_free(un->un_devid); 23252 un->un_devid = NULL; 23253 } 23254 } 23255 } 23256 mutex_exit(SD_MUTEX(un)); 23257 return (rval); 23258 } 23259 23260 23261 /* 23262 * Function: sd_build_label_vtoc 23263 * 23264 * Description: This routine updates the driver soft state current volume table 23265 * of contents based on a user specified vtoc. 23266 * 23267 * Arguments: un - driver soft state (unit) structure 23268 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23269 * to update the driver soft state. 23270 * 23271 * Return Code: 0 23272 * EINVAL 23273 */ 23274 23275 static int 23276 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23277 { 23278 struct dk_map *lmap; 23279 struct partition *vpart; 23280 int nblks; 23281 #if defined(_SUNOS_VTOC_8) 23282 int ncyl; 23283 struct dk_map2 *lpart; 23284 #endif /* defined(_SUNOS_VTOC_8) */ 23285 int i; 23286 23287 ASSERT(mutex_owned(SD_MUTEX(un))); 23288 23289 /* Sanity-check the vtoc */ 23290 if (user_vtoc->v_sanity != VTOC_SANE || 23291 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23292 user_vtoc->v_nparts != V_NUMPAR) { 23293 return (EINVAL); 23294 } 23295 23296 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23297 if (nblks == 0) { 23298 return (EINVAL); 23299 } 23300 23301 #if defined(_SUNOS_VTOC_8) 23302 vpart = user_vtoc->v_part; 23303 for (i = 0; i < V_NUMPAR; i++) { 23304 if ((vpart->p_start % nblks) != 0) { 23305 return (EINVAL); 23306 } 23307 ncyl = vpart->p_start / nblks; 23308 ncyl += vpart->p_size / nblks; 23309 if ((vpart->p_size % nblks) != 0) { 23310 ncyl++; 23311 } 23312 if (ncyl > (int)un->un_g.dkg_ncyl) { 23313 return (EINVAL); 23314 } 23315 vpart++; 23316 } 23317 #endif /* defined(_SUNOS_VTOC_8) */ 23318 23319 /* Put appropriate vtoc structure fields into the disk label */ 23320 #if defined(_SUNOS_VTOC_16) 23321 /* 23322 * The vtoc is always a 32bit data structure to maintain the 23323 * on-disk format. Convert "in place" instead of bcopying it. 23324 */ 23325 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23326 23327 /* 23328 * in the 16-slice vtoc, starting sectors are expressed in 23329 * numbers *relative* to the start of the Solaris fdisk partition. 23330 */ 23331 lmap = un->un_map; 23332 vpart = user_vtoc->v_part; 23333 23334 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23335 lmap->dkl_cylno = vpart->p_start / nblks; 23336 lmap->dkl_nblk = vpart->p_size; 23337 } 23338 23339 #elif defined(_SUNOS_VTOC_8) 23340 23341 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23342 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23343 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23344 23345 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23346 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23347 23348 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23349 23350 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23351 23352 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23353 sizeof (un->un_vtoc.v_reserved)); 23354 23355 /* 23356 * Note the conversion from starting sector number 23357 * to starting cylinder number. 23358 * Return error if division results in a remainder. 23359 */ 23360 lmap = un->un_map; 23361 lpart = un->un_vtoc.v_part; 23362 vpart = user_vtoc->v_part; 23363 23364 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23365 lpart->p_tag = vpart->p_tag; 23366 lpart->p_flag = vpart->p_flag; 23367 lmap->dkl_cylno = vpart->p_start / nblks; 23368 lmap->dkl_nblk = vpart->p_size; 23369 23370 lmap++; 23371 lpart++; 23372 vpart++; 23373 23374 /* (4387723) */ 23375 #ifdef _LP64 23376 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23377 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23378 } else { 23379 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23380 } 23381 #else 23382 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23383 #endif 23384 } 23385 23386 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23387 #else 23388 #error "No VTOC format defined." 23389 #endif 23390 return (0); 23391 } 23392 23393 /* 23394 * Function: sd_clear_efi 23395 * 23396 * Description: This routine clears all EFI labels. 23397 * 23398 * Arguments: un - driver soft state (unit) structure 23399 * 23400 * Return Code: void 23401 */ 23402 23403 static void 23404 sd_clear_efi(struct sd_lun *un) 23405 { 23406 efi_gpt_t *gpt; 23407 uint_t lbasize; 23408 uint64_t cap; 23409 int rval; 23410 23411 ASSERT(!mutex_owned(SD_MUTEX(un))); 23412 23413 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23414 23415 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23416 goto done; 23417 } 23418 23419 sd_swap_efi_gpt(gpt); 23420 rval = sd_validate_efi(gpt); 23421 if (rval == 0) { 23422 /* clear primary */ 23423 bzero(gpt, sizeof (efi_gpt_t)); 23424 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23425 SD_PATH_DIRECT))) { 23426 SD_INFO(SD_LOG_IO_PARTITION, un, 23427 "sd_clear_efi: clear primary label failed\n"); 23428 } 23429 } 23430 /* the backup */ 23431 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23432 SD_PATH_DIRECT); 23433 if (rval) { 23434 goto done; 23435 } 23436 /* 23437 * The MMC standard allows READ CAPACITY to be 23438 * inaccurate by a bounded amount (in the interest of 23439 * response latency). As a result, failed READs are 23440 * commonplace (due to the reading of metadata and not 23441 * data). Depending on the per-Vendor/drive Sense data, 23442 * the failed READ can cause many (unnecessary) retries. 23443 */ 23444 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23445 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23446 SD_PATH_DIRECT)) != 0) { 23447 goto done; 23448 } 23449 sd_swap_efi_gpt(gpt); 23450 rval = sd_validate_efi(gpt); 23451 if (rval == 0) { 23452 /* clear backup */ 23453 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23454 cap-1); 23455 bzero(gpt, sizeof (efi_gpt_t)); 23456 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23457 cap-1, SD_PATH_DIRECT))) { 23458 SD_INFO(SD_LOG_IO_PARTITION, un, 23459 "sd_clear_efi: clear backup label failed\n"); 23460 } 23461 } else { 23462 /* 23463 * Refer to comments related to off-by-1 at the 23464 * header of this file 23465 */ 23466 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23467 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23468 SD_PATH_DIRECT)) != 0) { 23469 goto done; 23470 } 23471 sd_swap_efi_gpt(gpt); 23472 rval = sd_validate_efi(gpt); 23473 if (rval == 0) { 23474 /* clear legacy backup EFI label */ 23475 SD_TRACE(SD_LOG_IOCTL, un, 23476 "sd_clear_efi clear backup@%lu\n", cap-2); 23477 bzero(gpt, sizeof (efi_gpt_t)); 23478 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23479 cap-2, SD_PATH_DIRECT))) { 23480 SD_INFO(SD_LOG_IO_PARTITION, 23481 un, "sd_clear_efi: " 23482 " clear legacy backup label failed\n"); 23483 } 23484 } 23485 } 23486 23487 done: 23488 kmem_free(gpt, sizeof (efi_gpt_t)); 23489 } 23490 23491 /* 23492 * Function: sd_set_vtoc 23493 * 23494 * Description: This routine writes data to the appropriate positions 23495 * 23496 * Arguments: un - driver soft state (unit) structure 23497 * dkl - the data to be written 23498 * 23499 * Return: void 23500 */ 23501 23502 static int 23503 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23504 { 23505 void *shadow_buf; 23506 uint_t label_addr; 23507 int sec; 23508 int blk; 23509 int head; 23510 int cyl; 23511 int rval; 23512 23513 #if defined(__i386) || defined(__amd64) 23514 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23515 #else 23516 /* Write the primary label at block 0 of the solaris partition. */ 23517 label_addr = 0; 23518 #endif 23519 23520 if (NOT_DEVBSIZE(un)) { 23521 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23522 /* 23523 * Read the target's first block. 23524 */ 23525 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23526 un->un_tgt_blocksize, label_addr, 23527 SD_PATH_STANDARD)) != 0) { 23528 goto exit; 23529 } 23530 /* 23531 * Copy the contents of the label into the shadow buffer 23532 * which is of the size of target block size. 23533 */ 23534 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23535 } 23536 23537 /* Write the primary label */ 23538 if (NOT_DEVBSIZE(un)) { 23539 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23540 label_addr, SD_PATH_STANDARD); 23541 } else { 23542 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23543 label_addr, SD_PATH_STANDARD); 23544 } 23545 if (rval != 0) { 23546 return (rval); 23547 } 23548 23549 /* 23550 * Calculate where the backup labels go. They are always on 23551 * the last alternate cylinder, but some older drives put them 23552 * on head 2 instead of the last head. They are always on the 23553 * first 5 odd sectors of the appropriate track. 23554 * 23555 * We have no choice at this point, but to believe that the 23556 * disk label is valid. Use the geometry of the disk 23557 * as described in the label. 23558 */ 23559 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23560 head = dkl->dkl_nhead - 1; 23561 23562 /* 23563 * Write and verify the backup labels. Make sure we don't try to 23564 * write past the last cylinder. 23565 */ 23566 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23567 blk = (daddr_t)( 23568 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23569 (head * dkl->dkl_nsect) + sec); 23570 #if defined(__i386) || defined(__amd64) 23571 blk += un->un_solaris_offset; 23572 #endif 23573 if (NOT_DEVBSIZE(un)) { 23574 uint64_t tblk; 23575 /* 23576 * Need to read the block first for read modify write. 23577 */ 23578 tblk = (uint64_t)blk; 23579 blk = (int)((tblk * un->un_sys_blocksize) / 23580 un->un_tgt_blocksize); 23581 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23582 un->un_tgt_blocksize, blk, 23583 SD_PATH_STANDARD)) != 0) { 23584 goto exit; 23585 } 23586 /* 23587 * Modify the shadow buffer with the label. 23588 */ 23589 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23590 rval = sd_send_scsi_WRITE(un, shadow_buf, 23591 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23592 } else { 23593 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23594 blk, SD_PATH_STANDARD); 23595 SD_INFO(SD_LOG_IO_PARTITION, un, 23596 "sd_set_vtoc: wrote backup label %d\n", blk); 23597 } 23598 if (rval != 0) { 23599 goto exit; 23600 } 23601 } 23602 exit: 23603 if (NOT_DEVBSIZE(un)) { 23604 kmem_free(shadow_buf, un->un_tgt_blocksize); 23605 } 23606 return (rval); 23607 } 23608 23609 /* 23610 * Function: sd_clear_vtoc 23611 * 23612 * Description: This routine clears out the VTOC labels. 23613 * 23614 * Arguments: un - driver soft state (unit) structure 23615 * 23616 * Return: void 23617 */ 23618 23619 static void 23620 sd_clear_vtoc(struct sd_lun *un) 23621 { 23622 struct dk_label *dkl; 23623 23624 mutex_exit(SD_MUTEX(un)); 23625 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23626 mutex_enter(SD_MUTEX(un)); 23627 /* 23628 * sd_set_vtoc uses these fields in order to figure out 23629 * where to overwrite the backup labels 23630 */ 23631 dkl->dkl_apc = un->un_g.dkg_apc; 23632 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23633 dkl->dkl_acyl = un->un_g.dkg_acyl; 23634 dkl->dkl_nhead = un->un_g.dkg_nhead; 23635 dkl->dkl_nsect = un->un_g.dkg_nsect; 23636 mutex_exit(SD_MUTEX(un)); 23637 (void) sd_set_vtoc(un, dkl); 23638 kmem_free(dkl, sizeof (struct dk_label)); 23639 23640 mutex_enter(SD_MUTEX(un)); 23641 } 23642 23643 /* 23644 * Function: sd_write_label 23645 * 23646 * Description: This routine will validate and write the driver soft state vtoc 23647 * contents to the device. 23648 * 23649 * Arguments: dev - the device number 23650 * 23651 * Return Code: the code returned by sd_send_scsi_cmd() 23652 * 0 23653 * EINVAL 23654 * ENXIO 23655 * ENOMEM 23656 */ 23657 23658 static int 23659 sd_write_label(dev_t dev) 23660 { 23661 struct sd_lun *un; 23662 struct dk_label *dkl; 23663 short sum; 23664 short *sp; 23665 int i; 23666 int rval; 23667 23668 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23669 (un->un_state == SD_STATE_OFFLINE)) { 23670 return (ENXIO); 23671 } 23672 ASSERT(mutex_owned(SD_MUTEX(un))); 23673 mutex_exit(SD_MUTEX(un)); 23674 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23675 mutex_enter(SD_MUTEX(un)); 23676 23677 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23678 dkl->dkl_rpm = un->un_g.dkg_rpm; 23679 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23680 dkl->dkl_apc = un->un_g.dkg_apc; 23681 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23682 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23683 dkl->dkl_acyl = un->un_g.dkg_acyl; 23684 dkl->dkl_nhead = un->un_g.dkg_nhead; 23685 dkl->dkl_nsect = un->un_g.dkg_nsect; 23686 23687 #if defined(_SUNOS_VTOC_8) 23688 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23689 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23690 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23691 for (i = 0; i < NDKMAP; i++) { 23692 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23693 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23694 } 23695 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23696 #elif defined(_SUNOS_VTOC_16) 23697 dkl->dkl_skew = un->un_dkg_skew; 23698 #else 23699 #error "No VTOC format defined." 23700 #endif 23701 23702 dkl->dkl_magic = DKL_MAGIC; 23703 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23704 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23705 23706 /* Construct checksum for the new disk label */ 23707 sum = 0; 23708 sp = (short *)dkl; 23709 i = sizeof (struct dk_label) / sizeof (short); 23710 while (i--) { 23711 sum ^= *sp++; 23712 } 23713 dkl->dkl_cksum = sum; 23714 23715 mutex_exit(SD_MUTEX(un)); 23716 23717 rval = sd_set_vtoc(un, dkl); 23718 exit: 23719 kmem_free(dkl, sizeof (struct dk_label)); 23720 mutex_enter(SD_MUTEX(un)); 23721 return (rval); 23722 } 23723 23724 static int 23725 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23726 { 23727 struct sd_lun *un = NULL; 23728 dk_efi_t user_efi; 23729 int rval = 0; 23730 void *buffer; 23731 23732 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23733 return (ENXIO); 23734 23735 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23736 return (EFAULT); 23737 23738 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23739 23740 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23741 (user_efi.dki_length > un->un_max_xfer_size)) 23742 return (EINVAL); 23743 23744 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23745 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23746 rval = EFAULT; 23747 } else { 23748 /* 23749 * let's clear the vtoc labels and clear the softstate 23750 * vtoc. 23751 */ 23752 mutex_enter(SD_MUTEX(un)); 23753 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23754 SD_TRACE(SD_LOG_IO_PARTITION, un, 23755 "sd_dkio_set_efi: CLEAR VTOC\n"); 23756 sd_clear_vtoc(un); 23757 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23758 mutex_exit(SD_MUTEX(un)); 23759 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23760 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23761 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23762 S_IFBLK, 23763 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23764 un->un_node_type, NULL); 23765 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23766 S_IFCHR, 23767 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23768 un->un_node_type, NULL); 23769 } else 23770 mutex_exit(SD_MUTEX(un)); 23771 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23772 user_efi.dki_lba, SD_PATH_DIRECT); 23773 if (rval == 0) { 23774 mutex_enter(SD_MUTEX(un)); 23775 un->un_f_geometry_is_valid = FALSE; 23776 mutex_exit(SD_MUTEX(un)); 23777 } 23778 } 23779 kmem_free(buffer, user_efi.dki_length); 23780 return (rval); 23781 } 23782 23783 /* 23784 * Function: sd_dkio_get_mboot 23785 * 23786 * Description: This routine is the driver entry point for handling user 23787 * requests to get the current device mboot (DKIOCGMBOOT) 23788 * 23789 * Arguments: dev - the device number 23790 * arg - pointer to user provided mboot structure specifying 23791 * the current mboot. 23792 * flag - this argument is a pass through to ddi_copyxxx() 23793 * directly from the mode argument of ioctl(). 23794 * 23795 * Return Code: 0 23796 * EINVAL 23797 * EFAULT 23798 * ENXIO 23799 */ 23800 23801 static int 23802 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23803 { 23804 struct sd_lun *un; 23805 struct mboot *mboot; 23806 int rval; 23807 size_t buffer_size; 23808 23809 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23810 (un->un_state == SD_STATE_OFFLINE)) { 23811 return (ENXIO); 23812 } 23813 23814 if (!un->un_f_mboot_supported || arg == NULL) { 23815 return (EINVAL); 23816 } 23817 23818 /* 23819 * Read the mboot block, located at absolute block 0 on the target. 23820 */ 23821 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23822 23823 SD_TRACE(SD_LOG_IO_PARTITION, un, 23824 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23825 23826 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23827 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23828 SD_PATH_STANDARD)) == 0) { 23829 if (ddi_copyout(mboot, (void *)arg, 23830 sizeof (struct mboot), flag) != 0) { 23831 rval = EFAULT; 23832 } 23833 } 23834 kmem_free(mboot, buffer_size); 23835 return (rval); 23836 } 23837 23838 23839 /* 23840 * Function: sd_dkio_set_mboot 23841 * 23842 * Description: This routine is the driver entry point for handling user 23843 * requests to validate and set the device master boot 23844 * (DKIOCSMBOOT). 23845 * 23846 * Arguments: dev - the device number 23847 * arg - pointer to user provided mboot structure used to set the 23848 * master boot. 23849 * flag - this argument is a pass through to ddi_copyxxx() 23850 * directly from the mode argument of ioctl(). 23851 * 23852 * Return Code: 0 23853 * EINVAL 23854 * EFAULT 23855 * ENXIO 23856 */ 23857 23858 static int 23859 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23860 { 23861 struct sd_lun *un = NULL; 23862 struct mboot *mboot = NULL; 23863 int rval; 23864 ushort_t magic; 23865 23866 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23867 return (ENXIO); 23868 } 23869 23870 ASSERT(!mutex_owned(SD_MUTEX(un))); 23871 23872 if (!un->un_f_mboot_supported) { 23873 return (EINVAL); 23874 } 23875 23876 if (arg == NULL) { 23877 return (EINVAL); 23878 } 23879 23880 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23881 23882 if (ddi_copyin((const void *)arg, mboot, 23883 sizeof (struct mboot), flag) != 0) { 23884 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23885 return (EFAULT); 23886 } 23887 23888 /* Is this really a master boot record? */ 23889 magic = LE_16(mboot->signature); 23890 if (magic != MBB_MAGIC) { 23891 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23892 return (EINVAL); 23893 } 23894 23895 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23896 SD_PATH_STANDARD); 23897 23898 mutex_enter(SD_MUTEX(un)); 23899 #if defined(__i386) || defined(__amd64) 23900 if (rval == 0) { 23901 /* 23902 * mboot has been written successfully. 23903 * update the fdisk and vtoc tables in memory 23904 */ 23905 rval = sd_update_fdisk_and_vtoc(un); 23906 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23907 mutex_exit(SD_MUTEX(un)); 23908 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23909 return (rval); 23910 } 23911 } 23912 23913 /* 23914 * If the mboot write fails, write the devid anyway, what can it hurt? 23915 * Also preserve the device id by writing to the disk acyl for the case 23916 * where a devid has been fabricated. 23917 */ 23918 if (un->un_f_devid_supported && un->un_f_opt_fab_devid) { 23919 if (un->un_devid == NULL) { 23920 sd_register_devid(un, SD_DEVINFO(un), 23921 SD_TARGET_IS_UNRESERVED); 23922 } else { 23923 /* 23924 * The device id for this disk has been 23925 * fabricated. Fabricated device id's are 23926 * managed by storing them in the last 2 23927 * available sectors on the drive. The device 23928 * id must be preserved by writing it back out 23929 * to this location. 23930 */ 23931 if (sd_write_deviceid(un) != 0) { 23932 ddi_devid_free(un->un_devid); 23933 un->un_devid = NULL; 23934 } 23935 } 23936 } 23937 23938 #ifdef __lock_lint 23939 sd_setup_default_geometry(un); 23940 #endif 23941 23942 #else 23943 if (rval == 0) { 23944 /* 23945 * mboot has been written successfully. 23946 * set up the default geometry and VTOC 23947 */ 23948 if (un->un_blockcount <= DK_MAX_BLOCKS) 23949 sd_setup_default_geometry(un); 23950 } 23951 #endif 23952 mutex_exit(SD_MUTEX(un)); 23953 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23954 return (rval); 23955 } 23956 23957 23958 /* 23959 * Function: sd_setup_default_geometry 23960 * 23961 * Description: This local utility routine sets the default geometry as part of 23962 * setting the device mboot. 23963 * 23964 * Arguments: un - driver soft state (unit) structure 23965 * 23966 * Note: This may be redundant with sd_build_default_label. 23967 */ 23968 23969 static void 23970 sd_setup_default_geometry(struct sd_lun *un) 23971 { 23972 /* zero out the soft state geometry and partition table. */ 23973 bzero(&un->un_g, sizeof (struct dk_geom)); 23974 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23975 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23976 un->un_asciilabel[0] = '\0'; 23977 23978 /* 23979 * For the rpm, we use the minimum for the disk. 23980 * For the head, cyl and number of sector per track, 23981 * if the capacity <= 1GB, head = 64, sect = 32. 23982 * else head = 255, sect 63 23983 * Note: the capacity should be equal to C*H*S values. 23984 * This will cause some truncation of size due to 23985 * round off errors. For CD-ROMs, this truncation can 23986 * have adverse side effects, so returning ncyl and 23987 * nhead as 1. The nsect will overflow for most of 23988 * CD-ROMs as nsect is of type ushort. 23989 */ 23990 if (ISCD(un)) { 23991 un->un_g.dkg_ncyl = 1; 23992 un->un_g.dkg_nhead = 1; 23993 un->un_g.dkg_nsect = un->un_blockcount; 23994 } else { 23995 if (un->un_blockcount <= 0x1000) { 23996 /* Needed for unlabeled SCSI floppies. */ 23997 un->un_g.dkg_nhead = 2; 23998 un->un_g.dkg_ncyl = 80; 23999 un->un_g.dkg_pcyl = 80; 24000 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24001 } else if (un->un_blockcount <= 0x200000) { 24002 un->un_g.dkg_nhead = 64; 24003 un->un_g.dkg_nsect = 32; 24004 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24005 } else { 24006 un->un_g.dkg_nhead = 255; 24007 un->un_g.dkg_nsect = 63; 24008 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24009 } 24010 un->un_blockcount = un->un_g.dkg_ncyl * 24011 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24012 } 24013 un->un_g.dkg_acyl = 0; 24014 un->un_g.dkg_bcyl = 0; 24015 un->un_g.dkg_intrlv = 1; 24016 un->un_g.dkg_rpm = 200; 24017 un->un_g.dkg_read_reinstruct = 0; 24018 un->un_g.dkg_write_reinstruct = 0; 24019 if (un->un_g.dkg_pcyl == 0) { 24020 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24021 } 24022 24023 un->un_map['a'-'a'].dkl_cylno = 0; 24024 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24025 un->un_map['c'-'a'].dkl_cylno = 0; 24026 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24027 un->un_f_geometry_is_valid = FALSE; 24028 } 24029 24030 24031 #if defined(__i386) || defined(__amd64) 24032 /* 24033 * Function: sd_update_fdisk_and_vtoc 24034 * 24035 * Description: This local utility routine updates the device fdisk and vtoc 24036 * as part of setting the device mboot. 24037 * 24038 * Arguments: un - driver soft state (unit) structure 24039 * 24040 * Return Code: 0 for success or errno-type return code. 24041 * 24042 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24043 * these did exist seperately in x86 sd.c!!! 24044 */ 24045 24046 static int 24047 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24048 { 24049 static char labelstring[128]; 24050 static char buf[256]; 24051 char *label = 0; 24052 int count; 24053 int label_rc = 0; 24054 int gvalid = un->un_f_geometry_is_valid; 24055 int fdisk_rval; 24056 int lbasize; 24057 int capacity; 24058 24059 ASSERT(mutex_owned(SD_MUTEX(un))); 24060 24061 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24062 return (EINVAL); 24063 } 24064 24065 if (un->un_f_blockcount_is_valid == FALSE) { 24066 return (EINVAL); 24067 } 24068 24069 #if defined(_SUNOS_VTOC_16) 24070 /* 24071 * Set up the "whole disk" fdisk partition; this should always 24072 * exist, regardless of whether the disk contains an fdisk table 24073 * or vtoc. 24074 */ 24075 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24076 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24077 #endif /* defined(_SUNOS_VTOC_16) */ 24078 24079 /* 24080 * copy the lbasize and capacity so that if they're 24081 * reset while we're not holding the SD_MUTEX(un), we will 24082 * continue to use valid values after the SD_MUTEX(un) is 24083 * reacquired. 24084 */ 24085 lbasize = un->un_tgt_blocksize; 24086 capacity = un->un_blockcount; 24087 24088 /* 24089 * refresh the logical and physical geometry caches. 24090 * (data from mode sense format/rigid disk geometry pages, 24091 * and scsi_ifgetcap("geometry"). 24092 */ 24093 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24094 24095 /* 24096 * Only DIRECT ACCESS devices will have Sun labels. 24097 * CD's supposedly have a Sun label, too 24098 */ 24099 if (un->un_f_vtoc_label_supported) { 24100 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24101 SD_PATH_DIRECT); 24102 if (fdisk_rval == SD_CMD_FAILURE) { 24103 ASSERT(mutex_owned(SD_MUTEX(un))); 24104 return (EIO); 24105 } 24106 24107 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24108 ASSERT(mutex_owned(SD_MUTEX(un))); 24109 return (EACCES); 24110 } 24111 24112 if (un->un_solaris_size <= DK_LABEL_LOC) { 24113 /* 24114 * Found fdisk table but no Solaris partition entry, 24115 * so don't call sd_uselabel() and don't create 24116 * a default label. 24117 */ 24118 label_rc = 0; 24119 un->un_f_geometry_is_valid = TRUE; 24120 goto no_solaris_partition; 24121 } 24122 24123 #if defined(_SUNOS_VTOC_8) 24124 label = (char *)un->un_asciilabel; 24125 #elif defined(_SUNOS_VTOC_16) 24126 label = (char *)un->un_vtoc.v_asciilabel; 24127 #else 24128 #error "No VTOC format defined." 24129 #endif 24130 } else if (capacity < 0) { 24131 ASSERT(mutex_owned(SD_MUTEX(un))); 24132 return (EINVAL); 24133 } 24134 24135 /* 24136 * For Removable media We reach here if we have found a 24137 * SOLARIS PARTITION. 24138 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24139 * PARTITION has changed from the previous one, hence we will setup a 24140 * default VTOC in this case. 24141 */ 24142 if (un->un_f_geometry_is_valid == FALSE) { 24143 sd_build_default_label(un); 24144 label_rc = 0; 24145 } 24146 24147 no_solaris_partition: 24148 if ((!un->un_f_has_removable_media || 24149 (un->un_f_has_removable_media && 24150 un->un_mediastate == DKIO_EJECTED)) && 24151 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24152 /* 24153 * Print out a message indicating who and what we are. 24154 * We do this only when we happen to really validate the 24155 * geometry. We may call sd_validate_geometry() at other 24156 * times, ioctl()'s like Get VTOC in which case we 24157 * don't want to print the label. 24158 * If the geometry is valid, print the label string, 24159 * else print vendor and product info, if available 24160 */ 24161 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24162 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24163 } else { 24164 mutex_enter(&sd_label_mutex); 24165 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24166 labelstring); 24167 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24168 &labelstring[64]); 24169 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24170 labelstring, &labelstring[64]); 24171 if (un->un_f_blockcount_is_valid == TRUE) { 24172 (void) sprintf(&buf[strlen(buf)], 24173 ", %" PRIu64 " %u byte blocks\n", 24174 un->un_blockcount, 24175 un->un_tgt_blocksize); 24176 } else { 24177 (void) sprintf(&buf[strlen(buf)], 24178 ", (unknown capacity)\n"); 24179 } 24180 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24181 mutex_exit(&sd_label_mutex); 24182 } 24183 } 24184 24185 #if defined(_SUNOS_VTOC_16) 24186 /* 24187 * If we have valid geometry, set up the remaining fdisk partitions. 24188 * Note that dkl_cylno is not used for the fdisk map entries, so 24189 * we set it to an entirely bogus value. 24190 */ 24191 for (count = 0; count < FD_NUMPART; count++) { 24192 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24193 un->un_map[FDISK_P1 + count].dkl_nblk = 24194 un->un_fmap[count].fmap_nblk; 24195 un->un_offset[FDISK_P1 + count] = 24196 un->un_fmap[count].fmap_start; 24197 } 24198 #endif 24199 24200 for (count = 0; count < NDKMAP; count++) { 24201 #if defined(_SUNOS_VTOC_8) 24202 struct dk_map *lp = &un->un_map[count]; 24203 un->un_offset[count] = 24204 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24205 #elif defined(_SUNOS_VTOC_16) 24206 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24207 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24208 #else 24209 #error "No VTOC format defined." 24210 #endif 24211 } 24212 24213 ASSERT(mutex_owned(SD_MUTEX(un))); 24214 return (label_rc); 24215 } 24216 #endif 24217 24218 24219 /* 24220 * Function: sd_check_media 24221 * 24222 * Description: This utility routine implements the functionality for the 24223 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24224 * driver state changes from that specified by the user 24225 * (inserted or ejected). For example, if the user specifies 24226 * DKIO_EJECTED and the current media state is inserted this 24227 * routine will immediately return DKIO_INSERTED. However, if the 24228 * current media state is not inserted the user thread will be 24229 * blocked until the drive state changes. If DKIO_NONE is specified 24230 * the user thread will block until a drive state change occurs. 24231 * 24232 * Arguments: dev - the device number 24233 * state - user pointer to a dkio_state, updated with the current 24234 * drive state at return. 24235 * 24236 * Return Code: ENXIO 24237 * EIO 24238 * EAGAIN 24239 * EINTR 24240 */ 24241 24242 static int 24243 sd_check_media(dev_t dev, enum dkio_state state) 24244 { 24245 struct sd_lun *un = NULL; 24246 enum dkio_state prev_state; 24247 opaque_t token = NULL; 24248 int rval = 0; 24249 24250 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24251 return (ENXIO); 24252 } 24253 24254 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24255 24256 mutex_enter(SD_MUTEX(un)); 24257 24258 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24259 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24260 24261 prev_state = un->un_mediastate; 24262 24263 /* is there anything to do? */ 24264 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24265 /* 24266 * submit the request to the scsi_watch service; 24267 * scsi_media_watch_cb() does the real work 24268 */ 24269 mutex_exit(SD_MUTEX(un)); 24270 24271 /* 24272 * This change handles the case where a scsi watch request is 24273 * added to a device that is powered down. To accomplish this 24274 * we power up the device before adding the scsi watch request, 24275 * since the scsi watch sends a TUR directly to the device 24276 * which the device cannot handle if it is powered down. 24277 */ 24278 if (sd_pm_entry(un) != DDI_SUCCESS) { 24279 mutex_enter(SD_MUTEX(un)); 24280 goto done; 24281 } 24282 24283 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24284 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24285 (caddr_t)dev); 24286 24287 sd_pm_exit(un); 24288 24289 mutex_enter(SD_MUTEX(un)); 24290 if (token == NULL) { 24291 rval = EAGAIN; 24292 goto done; 24293 } 24294 24295 /* 24296 * This is a special case IOCTL that doesn't return 24297 * until the media state changes. Routine sdpower 24298 * knows about and handles this so don't count it 24299 * as an active cmd in the driver, which would 24300 * keep the device busy to the pm framework. 24301 * If the count isn't decremented the device can't 24302 * be powered down. 24303 */ 24304 un->un_ncmds_in_driver--; 24305 ASSERT(un->un_ncmds_in_driver >= 0); 24306 24307 /* 24308 * if a prior request had been made, this will be the same 24309 * token, as scsi_watch was designed that way. 24310 */ 24311 un->un_swr_token = token; 24312 un->un_specified_mediastate = state; 24313 24314 /* 24315 * now wait for media change 24316 * we will not be signalled unless mediastate == state but it is 24317 * still better to test for this condition, since there is a 24318 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24319 */ 24320 SD_TRACE(SD_LOG_COMMON, un, 24321 "sd_check_media: waiting for media state change\n"); 24322 while (un->un_mediastate == state) { 24323 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24324 SD_TRACE(SD_LOG_COMMON, un, 24325 "sd_check_media: waiting for media state " 24326 "was interrupted\n"); 24327 un->un_ncmds_in_driver++; 24328 rval = EINTR; 24329 goto done; 24330 } 24331 SD_TRACE(SD_LOG_COMMON, un, 24332 "sd_check_media: received signal, state=%x\n", 24333 un->un_mediastate); 24334 } 24335 /* 24336 * Inc the counter to indicate the device once again 24337 * has an active outstanding cmd. 24338 */ 24339 un->un_ncmds_in_driver++; 24340 } 24341 24342 /* invalidate geometry */ 24343 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24344 sr_ejected(un); 24345 } 24346 24347 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24348 uint64_t capacity; 24349 uint_t lbasize; 24350 24351 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24352 mutex_exit(SD_MUTEX(un)); 24353 /* 24354 * Since the following routines use SD_PATH_DIRECT, we must 24355 * call PM directly before the upcoming disk accesses. This 24356 * may cause the disk to be power/spin up. 24357 */ 24358 24359 if (sd_pm_entry(un) == DDI_SUCCESS) { 24360 rval = sd_send_scsi_READ_CAPACITY(un, 24361 &capacity, 24362 &lbasize, SD_PATH_DIRECT); 24363 if (rval != 0) { 24364 sd_pm_exit(un); 24365 mutex_enter(SD_MUTEX(un)); 24366 goto done; 24367 } 24368 } else { 24369 rval = EIO; 24370 mutex_enter(SD_MUTEX(un)); 24371 goto done; 24372 } 24373 mutex_enter(SD_MUTEX(un)); 24374 24375 sd_update_block_info(un, lbasize, capacity); 24376 24377 un->un_f_geometry_is_valid = FALSE; 24378 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24379 24380 mutex_exit(SD_MUTEX(un)); 24381 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24382 SD_PATH_DIRECT); 24383 sd_pm_exit(un); 24384 24385 mutex_enter(SD_MUTEX(un)); 24386 } 24387 done: 24388 un->un_f_watcht_stopped = FALSE; 24389 if (un->un_swr_token) { 24390 /* 24391 * Use of this local token and the mutex ensures that we avoid 24392 * some race conditions associated with terminating the 24393 * scsi watch. 24394 */ 24395 token = un->un_swr_token; 24396 un->un_swr_token = (opaque_t)NULL; 24397 mutex_exit(SD_MUTEX(un)); 24398 (void) scsi_watch_request_terminate(token, 24399 SCSI_WATCH_TERMINATE_WAIT); 24400 mutex_enter(SD_MUTEX(un)); 24401 } 24402 24403 /* 24404 * Update the capacity kstat value, if no media previously 24405 * (capacity kstat is 0) and a media has been inserted 24406 * (un_f_blockcount_is_valid == TRUE) 24407 */ 24408 if (un->un_errstats) { 24409 struct sd_errstats *stp = NULL; 24410 24411 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24412 if ((stp->sd_capacity.value.ui64 == 0) && 24413 (un->un_f_blockcount_is_valid == TRUE)) { 24414 stp->sd_capacity.value.ui64 = 24415 (uint64_t)((uint64_t)un->un_blockcount * 24416 un->un_sys_blocksize); 24417 } 24418 } 24419 mutex_exit(SD_MUTEX(un)); 24420 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24421 return (rval); 24422 } 24423 24424 24425 /* 24426 * Function: sd_delayed_cv_broadcast 24427 * 24428 * Description: Delayed cv_broadcast to allow for target to recover from media 24429 * insertion. 24430 * 24431 * Arguments: arg - driver soft state (unit) structure 24432 */ 24433 24434 static void 24435 sd_delayed_cv_broadcast(void *arg) 24436 { 24437 struct sd_lun *un = arg; 24438 24439 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24440 24441 mutex_enter(SD_MUTEX(un)); 24442 un->un_dcvb_timeid = NULL; 24443 cv_broadcast(&un->un_state_cv); 24444 mutex_exit(SD_MUTEX(un)); 24445 } 24446 24447 24448 /* 24449 * Function: sd_media_watch_cb 24450 * 24451 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24452 * routine processes the TUR sense data and updates the driver 24453 * state if a transition has occurred. The user thread 24454 * (sd_check_media) is then signalled. 24455 * 24456 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24457 * among multiple watches that share this callback function 24458 * resultp - scsi watch facility result packet containing scsi 24459 * packet, status byte and sense data 24460 * 24461 * Return Code: 0 for success, -1 for failure 24462 */ 24463 24464 static int 24465 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24466 { 24467 struct sd_lun *un; 24468 struct scsi_status *statusp = resultp->statusp; 24469 uint8_t *sensep = (uint8_t *)resultp->sensep; 24470 enum dkio_state state = DKIO_NONE; 24471 dev_t dev = (dev_t)arg; 24472 uchar_t actual_sense_length; 24473 uint8_t skey, asc, ascq; 24474 24475 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24476 return (-1); 24477 } 24478 actual_sense_length = resultp->actual_sense_length; 24479 24480 mutex_enter(SD_MUTEX(un)); 24481 SD_TRACE(SD_LOG_COMMON, un, 24482 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24483 *((char *)statusp), (void *)sensep, actual_sense_length); 24484 24485 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24486 un->un_mediastate = DKIO_DEV_GONE; 24487 cv_broadcast(&un->un_state_cv); 24488 mutex_exit(SD_MUTEX(un)); 24489 24490 return (0); 24491 } 24492 24493 /* 24494 * If there was a check condition then sensep points to valid sense data 24495 * If status was not a check condition but a reservation or busy status 24496 * then the new state is DKIO_NONE 24497 */ 24498 if (sensep != NULL) { 24499 skey = scsi_sense_key(sensep); 24500 asc = scsi_sense_asc(sensep); 24501 ascq = scsi_sense_ascq(sensep); 24502 24503 SD_INFO(SD_LOG_COMMON, un, 24504 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24505 skey, asc, ascq); 24506 /* This routine only uses up to 13 bytes of sense data. */ 24507 if (actual_sense_length >= 13) { 24508 if (skey == KEY_UNIT_ATTENTION) { 24509 if (asc == 0x28) { 24510 state = DKIO_INSERTED; 24511 } 24512 } else { 24513 /* 24514 * if 02/04/02 means that the host 24515 * should send start command. Explicitly 24516 * leave the media state as is 24517 * (inserted) as the media is inserted 24518 * and host has stopped device for PM 24519 * reasons. Upon next true read/write 24520 * to this media will bring the 24521 * device to the right state good for 24522 * media access. 24523 */ 24524 if ((skey == KEY_NOT_READY) && 24525 (asc == 0x3a)) { 24526 state = DKIO_EJECTED; 24527 } 24528 24529 /* 24530 * If the drivge is busy with an operation 24531 * or long write, keep the media in an 24532 * inserted state. 24533 */ 24534 24535 if ((skey == KEY_NOT_READY) && 24536 (asc == 0x04) && 24537 ((ascq == 0x02) || 24538 (ascq == 0x07) || 24539 (ascq == 0x08))) { 24540 state = DKIO_INSERTED; 24541 } 24542 } 24543 } 24544 } else if ((*((char *)statusp) == STATUS_GOOD) && 24545 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24546 state = DKIO_INSERTED; 24547 } 24548 24549 SD_TRACE(SD_LOG_COMMON, un, 24550 "sd_media_watch_cb: state=%x, specified=%x\n", 24551 state, un->un_specified_mediastate); 24552 24553 /* 24554 * now signal the waiting thread if this is *not* the specified state; 24555 * delay the signal if the state is DKIO_INSERTED to allow the target 24556 * to recover 24557 */ 24558 if (state != un->un_specified_mediastate) { 24559 un->un_mediastate = state; 24560 if (state == DKIO_INSERTED) { 24561 /* 24562 * delay the signal to give the drive a chance 24563 * to do what it apparently needs to do 24564 */ 24565 SD_TRACE(SD_LOG_COMMON, un, 24566 "sd_media_watch_cb: delayed cv_broadcast\n"); 24567 if (un->un_dcvb_timeid == NULL) { 24568 un->un_dcvb_timeid = 24569 timeout(sd_delayed_cv_broadcast, un, 24570 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24571 } 24572 } else { 24573 SD_TRACE(SD_LOG_COMMON, un, 24574 "sd_media_watch_cb: immediate cv_broadcast\n"); 24575 cv_broadcast(&un->un_state_cv); 24576 } 24577 } 24578 mutex_exit(SD_MUTEX(un)); 24579 return (0); 24580 } 24581 24582 24583 /* 24584 * Function: sd_dkio_get_temp 24585 * 24586 * Description: This routine is the driver entry point for handling ioctl 24587 * requests to get the disk temperature. 24588 * 24589 * Arguments: dev - the device number 24590 * arg - pointer to user provided dk_temperature structure. 24591 * flag - this argument is a pass through to ddi_copyxxx() 24592 * directly from the mode argument of ioctl(). 24593 * 24594 * Return Code: 0 24595 * EFAULT 24596 * ENXIO 24597 * EAGAIN 24598 */ 24599 24600 static int 24601 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24602 { 24603 struct sd_lun *un = NULL; 24604 struct dk_temperature *dktemp = NULL; 24605 uchar_t *temperature_page; 24606 int rval = 0; 24607 int path_flag = SD_PATH_STANDARD; 24608 24609 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24610 return (ENXIO); 24611 } 24612 24613 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24614 24615 /* copyin the disk temp argument to get the user flags */ 24616 if (ddi_copyin((void *)arg, dktemp, 24617 sizeof (struct dk_temperature), flag) != 0) { 24618 rval = EFAULT; 24619 goto done; 24620 } 24621 24622 /* Initialize the temperature to invalid. */ 24623 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24624 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24625 24626 /* 24627 * Note: Investigate removing the "bypass pm" semantic. 24628 * Can we just bypass PM always? 24629 */ 24630 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24631 path_flag = SD_PATH_DIRECT; 24632 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24633 mutex_enter(&un->un_pm_mutex); 24634 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24635 /* 24636 * If DKT_BYPASS_PM is set, and the drive happens to be 24637 * in low power mode, we can not wake it up, Need to 24638 * return EAGAIN. 24639 */ 24640 mutex_exit(&un->un_pm_mutex); 24641 rval = EAGAIN; 24642 goto done; 24643 } else { 24644 /* 24645 * Indicate to PM the device is busy. This is required 24646 * to avoid a race - i.e. the ioctl is issuing a 24647 * command and the pm framework brings down the device 24648 * to low power mode (possible power cut-off on some 24649 * platforms). 24650 */ 24651 mutex_exit(&un->un_pm_mutex); 24652 if (sd_pm_entry(un) != DDI_SUCCESS) { 24653 rval = EAGAIN; 24654 goto done; 24655 } 24656 } 24657 } 24658 24659 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24660 24661 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24662 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24663 goto done2; 24664 } 24665 24666 /* 24667 * For the current temperature verify that the parameter length is 0x02 24668 * and the parameter code is 0x00 24669 */ 24670 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24671 (temperature_page[5] == 0x00)) { 24672 if (temperature_page[9] == 0xFF) { 24673 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24674 } else { 24675 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24676 } 24677 } 24678 24679 /* 24680 * For the reference temperature verify that the parameter 24681 * length is 0x02 and the parameter code is 0x01 24682 */ 24683 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24684 (temperature_page[11] == 0x01)) { 24685 if (temperature_page[15] == 0xFF) { 24686 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24687 } else { 24688 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24689 } 24690 } 24691 24692 /* Do the copyout regardless of the temperature commands status. */ 24693 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24694 flag) != 0) { 24695 rval = EFAULT; 24696 } 24697 24698 done2: 24699 if (path_flag == SD_PATH_DIRECT) { 24700 sd_pm_exit(un); 24701 } 24702 24703 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24704 done: 24705 if (dktemp != NULL) { 24706 kmem_free(dktemp, sizeof (struct dk_temperature)); 24707 } 24708 24709 return (rval); 24710 } 24711 24712 24713 /* 24714 * Function: sd_log_page_supported 24715 * 24716 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24717 * supported log pages. 24718 * 24719 * Arguments: un - 24720 * log_page - 24721 * 24722 * Return Code: -1 - on error (log sense is optional and may not be supported). 24723 * 0 - log page not found. 24724 * 1 - log page found. 24725 */ 24726 24727 static int 24728 sd_log_page_supported(struct sd_lun *un, int log_page) 24729 { 24730 uchar_t *log_page_data; 24731 int i; 24732 int match = 0; 24733 int log_size; 24734 24735 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24736 24737 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24738 SD_PATH_DIRECT) != 0) { 24739 SD_ERROR(SD_LOG_COMMON, un, 24740 "sd_log_page_supported: failed log page retrieval\n"); 24741 kmem_free(log_page_data, 0xFF); 24742 return (-1); 24743 } 24744 log_size = log_page_data[3]; 24745 24746 /* 24747 * The list of supported log pages start from the fourth byte. Check 24748 * until we run out of log pages or a match is found. 24749 */ 24750 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24751 if (log_page_data[i] == log_page) { 24752 match++; 24753 } 24754 } 24755 kmem_free(log_page_data, 0xFF); 24756 return (match); 24757 } 24758 24759 24760 /* 24761 * Function: sd_mhdioc_failfast 24762 * 24763 * Description: This routine is the driver entry point for handling ioctl 24764 * requests to enable/disable the multihost failfast option. 24765 * (MHIOCENFAILFAST) 24766 * 24767 * Arguments: dev - the device number 24768 * arg - user specified probing interval. 24769 * flag - this argument is a pass through to ddi_copyxxx() 24770 * directly from the mode argument of ioctl(). 24771 * 24772 * Return Code: 0 24773 * EFAULT 24774 * ENXIO 24775 */ 24776 24777 static int 24778 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24779 { 24780 struct sd_lun *un = NULL; 24781 int mh_time; 24782 int rval = 0; 24783 24784 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24785 return (ENXIO); 24786 } 24787 24788 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24789 return (EFAULT); 24790 24791 if (mh_time) { 24792 mutex_enter(SD_MUTEX(un)); 24793 un->un_resvd_status |= SD_FAILFAST; 24794 mutex_exit(SD_MUTEX(un)); 24795 /* 24796 * If mh_time is INT_MAX, then this ioctl is being used for 24797 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24798 */ 24799 if (mh_time != INT_MAX) { 24800 rval = sd_check_mhd(dev, mh_time); 24801 } 24802 } else { 24803 (void) sd_check_mhd(dev, 0); 24804 mutex_enter(SD_MUTEX(un)); 24805 un->un_resvd_status &= ~SD_FAILFAST; 24806 mutex_exit(SD_MUTEX(un)); 24807 } 24808 return (rval); 24809 } 24810 24811 24812 /* 24813 * Function: sd_mhdioc_takeown 24814 * 24815 * Description: This routine is the driver entry point for handling ioctl 24816 * requests to forcefully acquire exclusive access rights to the 24817 * multihost disk (MHIOCTKOWN). 24818 * 24819 * Arguments: dev - the device number 24820 * arg - user provided structure specifying the delay 24821 * parameters in milliseconds 24822 * flag - this argument is a pass through to ddi_copyxxx() 24823 * directly from the mode argument of ioctl(). 24824 * 24825 * Return Code: 0 24826 * EFAULT 24827 * ENXIO 24828 */ 24829 24830 static int 24831 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24832 { 24833 struct sd_lun *un = NULL; 24834 struct mhioctkown *tkown = NULL; 24835 int rval = 0; 24836 24837 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24838 return (ENXIO); 24839 } 24840 24841 if (arg != NULL) { 24842 tkown = (struct mhioctkown *) 24843 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24844 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24845 if (rval != 0) { 24846 rval = EFAULT; 24847 goto error; 24848 } 24849 } 24850 24851 rval = sd_take_ownership(dev, tkown); 24852 mutex_enter(SD_MUTEX(un)); 24853 if (rval == 0) { 24854 un->un_resvd_status |= SD_RESERVE; 24855 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24856 sd_reinstate_resv_delay = 24857 tkown->reinstate_resv_delay * 1000; 24858 } else { 24859 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24860 } 24861 /* 24862 * Give the scsi_watch routine interval set by 24863 * the MHIOCENFAILFAST ioctl precedence here. 24864 */ 24865 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24866 mutex_exit(SD_MUTEX(un)); 24867 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24868 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24869 "sd_mhdioc_takeown : %d\n", 24870 sd_reinstate_resv_delay); 24871 } else { 24872 mutex_exit(SD_MUTEX(un)); 24873 } 24874 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24875 sd_mhd_reset_notify_cb, (caddr_t)un); 24876 } else { 24877 un->un_resvd_status &= ~SD_RESERVE; 24878 mutex_exit(SD_MUTEX(un)); 24879 } 24880 24881 error: 24882 if (tkown != NULL) { 24883 kmem_free(tkown, sizeof (struct mhioctkown)); 24884 } 24885 return (rval); 24886 } 24887 24888 24889 /* 24890 * Function: sd_mhdioc_release 24891 * 24892 * Description: This routine is the driver entry point for handling ioctl 24893 * requests to release exclusive access rights to the multihost 24894 * disk (MHIOCRELEASE). 24895 * 24896 * Arguments: dev - the device number 24897 * 24898 * Return Code: 0 24899 * ENXIO 24900 */ 24901 24902 static int 24903 sd_mhdioc_release(dev_t dev) 24904 { 24905 struct sd_lun *un = NULL; 24906 timeout_id_t resvd_timeid_save; 24907 int resvd_status_save; 24908 int rval = 0; 24909 24910 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24911 return (ENXIO); 24912 } 24913 24914 mutex_enter(SD_MUTEX(un)); 24915 resvd_status_save = un->un_resvd_status; 24916 un->un_resvd_status &= 24917 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24918 if (un->un_resvd_timeid) { 24919 resvd_timeid_save = un->un_resvd_timeid; 24920 un->un_resvd_timeid = NULL; 24921 mutex_exit(SD_MUTEX(un)); 24922 (void) untimeout(resvd_timeid_save); 24923 } else { 24924 mutex_exit(SD_MUTEX(un)); 24925 } 24926 24927 /* 24928 * destroy any pending timeout thread that may be attempting to 24929 * reinstate reservation on this device. 24930 */ 24931 sd_rmv_resv_reclaim_req(dev); 24932 24933 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24934 mutex_enter(SD_MUTEX(un)); 24935 if ((un->un_mhd_token) && 24936 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24937 mutex_exit(SD_MUTEX(un)); 24938 (void) sd_check_mhd(dev, 0); 24939 } else { 24940 mutex_exit(SD_MUTEX(un)); 24941 } 24942 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24943 sd_mhd_reset_notify_cb, (caddr_t)un); 24944 } else { 24945 /* 24946 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24947 */ 24948 mutex_enter(SD_MUTEX(un)); 24949 un->un_resvd_status = resvd_status_save; 24950 mutex_exit(SD_MUTEX(un)); 24951 } 24952 return (rval); 24953 } 24954 24955 24956 /* 24957 * Function: sd_mhdioc_register_devid 24958 * 24959 * Description: This routine is the driver entry point for handling ioctl 24960 * requests to register the device id (MHIOCREREGISTERDEVID). 24961 * 24962 * Note: The implementation for this ioctl has been updated to 24963 * be consistent with the original PSARC case (1999/357) 24964 * (4375899, 4241671, 4220005) 24965 * 24966 * Arguments: dev - the device number 24967 * 24968 * Return Code: 0 24969 * ENXIO 24970 */ 24971 24972 static int 24973 sd_mhdioc_register_devid(dev_t dev) 24974 { 24975 struct sd_lun *un = NULL; 24976 int rval = 0; 24977 24978 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24979 return (ENXIO); 24980 } 24981 24982 ASSERT(!mutex_owned(SD_MUTEX(un))); 24983 24984 mutex_enter(SD_MUTEX(un)); 24985 24986 /* If a devid already exists, de-register it */ 24987 if (un->un_devid != NULL) { 24988 ddi_devid_unregister(SD_DEVINFO(un)); 24989 /* 24990 * After unregister devid, needs to free devid memory 24991 */ 24992 ddi_devid_free(un->un_devid); 24993 un->un_devid = NULL; 24994 } 24995 24996 /* Check for reservation conflict */ 24997 mutex_exit(SD_MUTEX(un)); 24998 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24999 mutex_enter(SD_MUTEX(un)); 25000 25001 switch (rval) { 25002 case 0: 25003 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25004 break; 25005 case EACCES: 25006 break; 25007 default: 25008 rval = EIO; 25009 } 25010 25011 mutex_exit(SD_MUTEX(un)); 25012 return (rval); 25013 } 25014 25015 25016 /* 25017 * Function: sd_mhdioc_inkeys 25018 * 25019 * Description: This routine is the driver entry point for handling ioctl 25020 * requests to issue the SCSI-3 Persistent In Read Keys command 25021 * to the device (MHIOCGRP_INKEYS). 25022 * 25023 * Arguments: dev - the device number 25024 * arg - user provided in_keys structure 25025 * flag - this argument is a pass through to ddi_copyxxx() 25026 * directly from the mode argument of ioctl(). 25027 * 25028 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25029 * ENXIO 25030 * EFAULT 25031 */ 25032 25033 static int 25034 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25035 { 25036 struct sd_lun *un; 25037 mhioc_inkeys_t inkeys; 25038 int rval = 0; 25039 25040 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25041 return (ENXIO); 25042 } 25043 25044 #ifdef _MULTI_DATAMODEL 25045 switch (ddi_model_convert_from(flag & FMODELS)) { 25046 case DDI_MODEL_ILP32: { 25047 struct mhioc_inkeys32 inkeys32; 25048 25049 if (ddi_copyin(arg, &inkeys32, 25050 sizeof (struct mhioc_inkeys32), flag) != 0) { 25051 return (EFAULT); 25052 } 25053 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25054 if ((rval = sd_persistent_reservation_in_read_keys(un, 25055 &inkeys, flag)) != 0) { 25056 return (rval); 25057 } 25058 inkeys32.generation = inkeys.generation; 25059 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25060 flag) != 0) { 25061 return (EFAULT); 25062 } 25063 break; 25064 } 25065 case DDI_MODEL_NONE: 25066 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25067 flag) != 0) { 25068 return (EFAULT); 25069 } 25070 if ((rval = sd_persistent_reservation_in_read_keys(un, 25071 &inkeys, flag)) != 0) { 25072 return (rval); 25073 } 25074 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25075 flag) != 0) { 25076 return (EFAULT); 25077 } 25078 break; 25079 } 25080 25081 #else /* ! _MULTI_DATAMODEL */ 25082 25083 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25084 return (EFAULT); 25085 } 25086 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25087 if (rval != 0) { 25088 return (rval); 25089 } 25090 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25091 return (EFAULT); 25092 } 25093 25094 #endif /* _MULTI_DATAMODEL */ 25095 25096 return (rval); 25097 } 25098 25099 25100 /* 25101 * Function: sd_mhdioc_inresv 25102 * 25103 * Description: This routine is the driver entry point for handling ioctl 25104 * requests to issue the SCSI-3 Persistent In Read Reservations 25105 * command to the device (MHIOCGRP_INKEYS). 25106 * 25107 * Arguments: dev - the device number 25108 * arg - user provided in_resv structure 25109 * flag - this argument is a pass through to ddi_copyxxx() 25110 * directly from the mode argument of ioctl(). 25111 * 25112 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25113 * ENXIO 25114 * EFAULT 25115 */ 25116 25117 static int 25118 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25119 { 25120 struct sd_lun *un; 25121 mhioc_inresvs_t inresvs; 25122 int rval = 0; 25123 25124 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25125 return (ENXIO); 25126 } 25127 25128 #ifdef _MULTI_DATAMODEL 25129 25130 switch (ddi_model_convert_from(flag & FMODELS)) { 25131 case DDI_MODEL_ILP32: { 25132 struct mhioc_inresvs32 inresvs32; 25133 25134 if (ddi_copyin(arg, &inresvs32, 25135 sizeof (struct mhioc_inresvs32), flag) != 0) { 25136 return (EFAULT); 25137 } 25138 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25139 if ((rval = sd_persistent_reservation_in_read_resv(un, 25140 &inresvs, flag)) != 0) { 25141 return (rval); 25142 } 25143 inresvs32.generation = inresvs.generation; 25144 if (ddi_copyout(&inresvs32, arg, 25145 sizeof (struct mhioc_inresvs32), flag) != 0) { 25146 return (EFAULT); 25147 } 25148 break; 25149 } 25150 case DDI_MODEL_NONE: 25151 if (ddi_copyin(arg, &inresvs, 25152 sizeof (mhioc_inresvs_t), flag) != 0) { 25153 return (EFAULT); 25154 } 25155 if ((rval = sd_persistent_reservation_in_read_resv(un, 25156 &inresvs, flag)) != 0) { 25157 return (rval); 25158 } 25159 if (ddi_copyout(&inresvs, arg, 25160 sizeof (mhioc_inresvs_t), flag) != 0) { 25161 return (EFAULT); 25162 } 25163 break; 25164 } 25165 25166 #else /* ! _MULTI_DATAMODEL */ 25167 25168 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25169 return (EFAULT); 25170 } 25171 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25172 if (rval != 0) { 25173 return (rval); 25174 } 25175 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25176 return (EFAULT); 25177 } 25178 25179 #endif /* ! _MULTI_DATAMODEL */ 25180 25181 return (rval); 25182 } 25183 25184 25185 /* 25186 * The following routines support the clustering functionality described below 25187 * and implement lost reservation reclaim functionality. 25188 * 25189 * Clustering 25190 * ---------- 25191 * The clustering code uses two different, independent forms of SCSI 25192 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25193 * Persistent Group Reservations. For any particular disk, it will use either 25194 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25195 * 25196 * SCSI-2 25197 * The cluster software takes ownership of a multi-hosted disk by issuing the 25198 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25199 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25200 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25201 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25202 * meaning of failfast is that if the driver (on this host) ever encounters the 25203 * scsi error return code RESERVATION_CONFLICT from the device, it should 25204 * immediately panic the host. The motivation for this ioctl is that if this 25205 * host does encounter reservation conflict, the underlying cause is that some 25206 * other host of the cluster has decided that this host is no longer in the 25207 * cluster and has seized control of the disks for itself. Since this host is no 25208 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25209 * does two things: 25210 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25211 * error to panic the host 25212 * (b) it sets up a periodic timer to test whether this host still has 25213 * "access" (in that no other host has reserved the device): if the 25214 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25215 * purpose of that periodic timer is to handle scenarios where the host is 25216 * otherwise temporarily quiescent, temporarily doing no real i/o. 25217 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25218 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25219 * the device itself. 25220 * 25221 * SCSI-3 PGR 25222 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25223 * facility is supported through the shared multihost disk ioctls 25224 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25225 * MHIOCGRP_PREEMPTANDABORT) 25226 * 25227 * Reservation Reclaim: 25228 * -------------------- 25229 * To support the lost reservation reclaim operations this driver creates a 25230 * single thread to handle reinstating reservations on all devices that have 25231 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25232 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25233 * and the reservation reclaim thread loops through the requests to regain the 25234 * lost reservations. 25235 */ 25236 25237 /* 25238 * Function: sd_check_mhd() 25239 * 25240 * Description: This function sets up and submits a scsi watch request or 25241 * terminates an existing watch request. This routine is used in 25242 * support of reservation reclaim. 25243 * 25244 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25245 * among multiple watches that share the callback function 25246 * interval - the number of microseconds specifying the watch 25247 * interval for issuing TEST UNIT READY commands. If 25248 * set to 0 the watch should be terminated. If the 25249 * interval is set to 0 and if the device is required 25250 * to hold reservation while disabling failfast, the 25251 * watch is restarted with an interval of 25252 * reinstate_resv_delay. 25253 * 25254 * Return Code: 0 - Successful submit/terminate of scsi watch request 25255 * ENXIO - Indicates an invalid device was specified 25256 * EAGAIN - Unable to submit the scsi watch request 25257 */ 25258 25259 static int 25260 sd_check_mhd(dev_t dev, int interval) 25261 { 25262 struct sd_lun *un; 25263 opaque_t token; 25264 25265 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25266 return (ENXIO); 25267 } 25268 25269 /* is this a watch termination request? */ 25270 if (interval == 0) { 25271 mutex_enter(SD_MUTEX(un)); 25272 /* if there is an existing watch task then terminate it */ 25273 if (un->un_mhd_token) { 25274 token = un->un_mhd_token; 25275 un->un_mhd_token = NULL; 25276 mutex_exit(SD_MUTEX(un)); 25277 (void) scsi_watch_request_terminate(token, 25278 SCSI_WATCH_TERMINATE_WAIT); 25279 mutex_enter(SD_MUTEX(un)); 25280 } else { 25281 mutex_exit(SD_MUTEX(un)); 25282 /* 25283 * Note: If we return here we don't check for the 25284 * failfast case. This is the original legacy 25285 * implementation but perhaps we should be checking 25286 * the failfast case. 25287 */ 25288 return (0); 25289 } 25290 /* 25291 * If the device is required to hold reservation while 25292 * disabling failfast, we need to restart the scsi_watch 25293 * routine with an interval of reinstate_resv_delay. 25294 */ 25295 if (un->un_resvd_status & SD_RESERVE) { 25296 interval = sd_reinstate_resv_delay/1000; 25297 } else { 25298 /* no failfast so bail */ 25299 mutex_exit(SD_MUTEX(un)); 25300 return (0); 25301 } 25302 mutex_exit(SD_MUTEX(un)); 25303 } 25304 25305 /* 25306 * adjust minimum time interval to 1 second, 25307 * and convert from msecs to usecs 25308 */ 25309 if (interval > 0 && interval < 1000) { 25310 interval = 1000; 25311 } 25312 interval *= 1000; 25313 25314 /* 25315 * submit the request to the scsi_watch service 25316 */ 25317 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25318 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25319 if (token == NULL) { 25320 return (EAGAIN); 25321 } 25322 25323 /* 25324 * save token for termination later on 25325 */ 25326 mutex_enter(SD_MUTEX(un)); 25327 un->un_mhd_token = token; 25328 mutex_exit(SD_MUTEX(un)); 25329 return (0); 25330 } 25331 25332 25333 /* 25334 * Function: sd_mhd_watch_cb() 25335 * 25336 * Description: This function is the call back function used by the scsi watch 25337 * facility. The scsi watch facility sends the "Test Unit Ready" 25338 * and processes the status. If applicable (i.e. a "Unit Attention" 25339 * status and automatic "Request Sense" not used) the scsi watch 25340 * facility will send a "Request Sense" and retrieve the sense data 25341 * to be passed to this callback function. In either case the 25342 * automatic "Request Sense" or the facility submitting one, this 25343 * callback is passed the status and sense data. 25344 * 25345 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25346 * among multiple watches that share this callback function 25347 * resultp - scsi watch facility result packet containing scsi 25348 * packet, status byte and sense data 25349 * 25350 * Return Code: 0 - continue the watch task 25351 * non-zero - terminate the watch task 25352 */ 25353 25354 static int 25355 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25356 { 25357 struct sd_lun *un; 25358 struct scsi_status *statusp; 25359 uint8_t *sensep; 25360 struct scsi_pkt *pkt; 25361 uchar_t actual_sense_length; 25362 dev_t dev = (dev_t)arg; 25363 25364 ASSERT(resultp != NULL); 25365 statusp = resultp->statusp; 25366 sensep = (uint8_t *)resultp->sensep; 25367 pkt = resultp->pkt; 25368 actual_sense_length = resultp->actual_sense_length; 25369 25370 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25371 return (ENXIO); 25372 } 25373 25374 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25375 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25376 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25377 25378 /* Begin processing of the status and/or sense data */ 25379 if (pkt->pkt_reason != CMD_CMPLT) { 25380 /* Handle the incomplete packet */ 25381 sd_mhd_watch_incomplete(un, pkt); 25382 return (0); 25383 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25384 if (*((unsigned char *)statusp) 25385 == STATUS_RESERVATION_CONFLICT) { 25386 /* 25387 * Handle a reservation conflict by panicking if 25388 * configured for failfast or by logging the conflict 25389 * and updating the reservation status 25390 */ 25391 mutex_enter(SD_MUTEX(un)); 25392 if ((un->un_resvd_status & SD_FAILFAST) && 25393 (sd_failfast_enable)) { 25394 sd_panic_for_res_conflict(un); 25395 /*NOTREACHED*/ 25396 } 25397 SD_INFO(SD_LOG_IOCTL_MHD, un, 25398 "sd_mhd_watch_cb: Reservation Conflict\n"); 25399 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25400 mutex_exit(SD_MUTEX(un)); 25401 } 25402 } 25403 25404 if (sensep != NULL) { 25405 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25406 mutex_enter(SD_MUTEX(un)); 25407 if ((scsi_sense_asc(sensep) == 25408 SD_SCSI_RESET_SENSE_CODE) && 25409 (un->un_resvd_status & SD_RESERVE)) { 25410 /* 25411 * The additional sense code indicates a power 25412 * on or bus device reset has occurred; update 25413 * the reservation status. 25414 */ 25415 un->un_resvd_status |= 25416 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25417 SD_INFO(SD_LOG_IOCTL_MHD, un, 25418 "sd_mhd_watch_cb: Lost Reservation\n"); 25419 } 25420 } else { 25421 return (0); 25422 } 25423 } else { 25424 mutex_enter(SD_MUTEX(un)); 25425 } 25426 25427 if ((un->un_resvd_status & SD_RESERVE) && 25428 (un->un_resvd_status & SD_LOST_RESERVE)) { 25429 if (un->un_resvd_status & SD_WANT_RESERVE) { 25430 /* 25431 * A reset occurred in between the last probe and this 25432 * one so if a timeout is pending cancel it. 25433 */ 25434 if (un->un_resvd_timeid) { 25435 timeout_id_t temp_id = un->un_resvd_timeid; 25436 un->un_resvd_timeid = NULL; 25437 mutex_exit(SD_MUTEX(un)); 25438 (void) untimeout(temp_id); 25439 mutex_enter(SD_MUTEX(un)); 25440 } 25441 un->un_resvd_status &= ~SD_WANT_RESERVE; 25442 } 25443 if (un->un_resvd_timeid == 0) { 25444 /* Schedule a timeout to handle the lost reservation */ 25445 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25446 (void *)dev, 25447 drv_usectohz(sd_reinstate_resv_delay)); 25448 } 25449 } 25450 mutex_exit(SD_MUTEX(un)); 25451 return (0); 25452 } 25453 25454 25455 /* 25456 * Function: sd_mhd_watch_incomplete() 25457 * 25458 * Description: This function is used to find out why a scsi pkt sent by the 25459 * scsi watch facility was not completed. Under some scenarios this 25460 * routine will return. Otherwise it will send a bus reset to see 25461 * if the drive is still online. 25462 * 25463 * Arguments: un - driver soft state (unit) structure 25464 * pkt - incomplete scsi pkt 25465 */ 25466 25467 static void 25468 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25469 { 25470 int be_chatty; 25471 int perr; 25472 25473 ASSERT(pkt != NULL); 25474 ASSERT(un != NULL); 25475 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25476 perr = (pkt->pkt_statistics & STAT_PERR); 25477 25478 mutex_enter(SD_MUTEX(un)); 25479 if (un->un_state == SD_STATE_DUMPING) { 25480 mutex_exit(SD_MUTEX(un)); 25481 return; 25482 } 25483 25484 switch (pkt->pkt_reason) { 25485 case CMD_UNX_BUS_FREE: 25486 /* 25487 * If we had a parity error that caused the target to drop BSY*, 25488 * don't be chatty about it. 25489 */ 25490 if (perr && be_chatty) { 25491 be_chatty = 0; 25492 } 25493 break; 25494 case CMD_TAG_REJECT: 25495 /* 25496 * The SCSI-2 spec states that a tag reject will be sent by the 25497 * target if tagged queuing is not supported. A tag reject may 25498 * also be sent during certain initialization periods or to 25499 * control internal resources. For the latter case the target 25500 * may also return Queue Full. 25501 * 25502 * If this driver receives a tag reject from a target that is 25503 * going through an init period or controlling internal 25504 * resources tagged queuing will be disabled. This is a less 25505 * than optimal behavior but the driver is unable to determine 25506 * the target state and assumes tagged queueing is not supported 25507 */ 25508 pkt->pkt_flags = 0; 25509 un->un_tagflags = 0; 25510 25511 if (un->un_f_opt_queueing == TRUE) { 25512 un->un_throttle = min(un->un_throttle, 3); 25513 } else { 25514 un->un_throttle = 1; 25515 } 25516 mutex_exit(SD_MUTEX(un)); 25517 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25518 mutex_enter(SD_MUTEX(un)); 25519 break; 25520 case CMD_INCOMPLETE: 25521 /* 25522 * The transport stopped with an abnormal state, fallthrough and 25523 * reset the target and/or bus unless selection did not complete 25524 * (indicated by STATE_GOT_BUS) in which case we don't want to 25525 * go through a target/bus reset 25526 */ 25527 if (pkt->pkt_state == STATE_GOT_BUS) { 25528 break; 25529 } 25530 /*FALLTHROUGH*/ 25531 25532 case CMD_TIMEOUT: 25533 default: 25534 /* 25535 * The lun may still be running the command, so a lun reset 25536 * should be attempted. If the lun reset fails or cannot be 25537 * issued, than try a target reset. Lastly try a bus reset. 25538 */ 25539 if ((pkt->pkt_statistics & 25540 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25541 int reset_retval = 0; 25542 mutex_exit(SD_MUTEX(un)); 25543 if (un->un_f_allow_bus_device_reset == TRUE) { 25544 if (un->un_f_lun_reset_enabled == TRUE) { 25545 reset_retval = 25546 scsi_reset(SD_ADDRESS(un), 25547 RESET_LUN); 25548 } 25549 if (reset_retval == 0) { 25550 reset_retval = 25551 scsi_reset(SD_ADDRESS(un), 25552 RESET_TARGET); 25553 } 25554 } 25555 if (reset_retval == 0) { 25556 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25557 } 25558 mutex_enter(SD_MUTEX(un)); 25559 } 25560 break; 25561 } 25562 25563 /* A device/bus reset has occurred; update the reservation status. */ 25564 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25565 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25566 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25567 un->un_resvd_status |= 25568 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25569 SD_INFO(SD_LOG_IOCTL_MHD, un, 25570 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25571 } 25572 } 25573 25574 /* 25575 * The disk has been turned off; Update the device state. 25576 * 25577 * Note: Should we be offlining the disk here? 25578 */ 25579 if (pkt->pkt_state == STATE_GOT_BUS) { 25580 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25581 "Disk not responding to selection\n"); 25582 if (un->un_state != SD_STATE_OFFLINE) { 25583 New_state(un, SD_STATE_OFFLINE); 25584 } 25585 } else if (be_chatty) { 25586 /* 25587 * suppress messages if they are all the same pkt reason; 25588 * with TQ, many (up to 256) are returned with the same 25589 * pkt_reason 25590 */ 25591 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25592 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25593 "sd_mhd_watch_incomplete: " 25594 "SCSI transport failed: reason '%s'\n", 25595 scsi_rname(pkt->pkt_reason)); 25596 } 25597 } 25598 un->un_last_pkt_reason = pkt->pkt_reason; 25599 mutex_exit(SD_MUTEX(un)); 25600 } 25601 25602 25603 /* 25604 * Function: sd_sname() 25605 * 25606 * Description: This is a simple little routine to return a string containing 25607 * a printable description of command status byte for use in 25608 * logging. 25609 * 25610 * Arguments: status - pointer to a status byte 25611 * 25612 * Return Code: char * - string containing status description. 25613 */ 25614 25615 static char * 25616 sd_sname(uchar_t status) 25617 { 25618 switch (status & STATUS_MASK) { 25619 case STATUS_GOOD: 25620 return ("good status"); 25621 case STATUS_CHECK: 25622 return ("check condition"); 25623 case STATUS_MET: 25624 return ("condition met"); 25625 case STATUS_BUSY: 25626 return ("busy"); 25627 case STATUS_INTERMEDIATE: 25628 return ("intermediate"); 25629 case STATUS_INTERMEDIATE_MET: 25630 return ("intermediate - condition met"); 25631 case STATUS_RESERVATION_CONFLICT: 25632 return ("reservation_conflict"); 25633 case STATUS_TERMINATED: 25634 return ("command terminated"); 25635 case STATUS_QFULL: 25636 return ("queue full"); 25637 default: 25638 return ("<unknown status>"); 25639 } 25640 } 25641 25642 25643 /* 25644 * Function: sd_mhd_resvd_recover() 25645 * 25646 * Description: This function adds a reservation entry to the 25647 * sd_resv_reclaim_request list and signals the reservation 25648 * reclaim thread that there is work pending. If the reservation 25649 * reclaim thread has not been previously created this function 25650 * will kick it off. 25651 * 25652 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25653 * among multiple watches that share this callback function 25654 * 25655 * Context: This routine is called by timeout() and is run in interrupt 25656 * context. It must not sleep or call other functions which may 25657 * sleep. 25658 */ 25659 25660 static void 25661 sd_mhd_resvd_recover(void *arg) 25662 { 25663 dev_t dev = (dev_t)arg; 25664 struct sd_lun *un; 25665 struct sd_thr_request *sd_treq = NULL; 25666 struct sd_thr_request *sd_cur = NULL; 25667 struct sd_thr_request *sd_prev = NULL; 25668 int already_there = 0; 25669 25670 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25671 return; 25672 } 25673 25674 mutex_enter(SD_MUTEX(un)); 25675 un->un_resvd_timeid = NULL; 25676 if (un->un_resvd_status & SD_WANT_RESERVE) { 25677 /* 25678 * There was a reset so don't issue the reserve, allow the 25679 * sd_mhd_watch_cb callback function to notice this and 25680 * reschedule the timeout for reservation. 25681 */ 25682 mutex_exit(SD_MUTEX(un)); 25683 return; 25684 } 25685 mutex_exit(SD_MUTEX(un)); 25686 25687 /* 25688 * Add this device to the sd_resv_reclaim_request list and the 25689 * sd_resv_reclaim_thread should take care of the rest. 25690 * 25691 * Note: We can't sleep in this context so if the memory allocation 25692 * fails allow the sd_mhd_watch_cb callback function to notice this and 25693 * reschedule the timeout for reservation. (4378460) 25694 */ 25695 sd_treq = (struct sd_thr_request *) 25696 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25697 if (sd_treq == NULL) { 25698 return; 25699 } 25700 25701 sd_treq->sd_thr_req_next = NULL; 25702 sd_treq->dev = dev; 25703 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25704 if (sd_tr.srq_thr_req_head == NULL) { 25705 sd_tr.srq_thr_req_head = sd_treq; 25706 } else { 25707 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25708 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25709 if (sd_cur->dev == dev) { 25710 /* 25711 * already in Queue so don't log 25712 * another request for the device 25713 */ 25714 already_there = 1; 25715 break; 25716 } 25717 sd_prev = sd_cur; 25718 } 25719 if (!already_there) { 25720 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25721 "logging request for %lx\n", dev); 25722 sd_prev->sd_thr_req_next = sd_treq; 25723 } else { 25724 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25725 } 25726 } 25727 25728 /* 25729 * Create a kernel thread to do the reservation reclaim and free up this 25730 * thread. We cannot block this thread while we go away to do the 25731 * reservation reclaim 25732 */ 25733 if (sd_tr.srq_resv_reclaim_thread == NULL) 25734 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25735 sd_resv_reclaim_thread, NULL, 25736 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25737 25738 /* Tell the reservation reclaim thread that it has work to do */ 25739 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25740 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25741 } 25742 25743 /* 25744 * Function: sd_resv_reclaim_thread() 25745 * 25746 * Description: This function implements the reservation reclaim operations 25747 * 25748 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25749 * among multiple watches that share this callback function 25750 */ 25751 25752 static void 25753 sd_resv_reclaim_thread() 25754 { 25755 struct sd_lun *un; 25756 struct sd_thr_request *sd_mhreq; 25757 25758 /* Wait for work */ 25759 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25760 if (sd_tr.srq_thr_req_head == NULL) { 25761 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25762 &sd_tr.srq_resv_reclaim_mutex); 25763 } 25764 25765 /* Loop while we have work */ 25766 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25767 un = ddi_get_soft_state(sd_state, 25768 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25769 if (un == NULL) { 25770 /* 25771 * softstate structure is NULL so just 25772 * dequeue the request and continue 25773 */ 25774 sd_tr.srq_thr_req_head = 25775 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25776 kmem_free(sd_tr.srq_thr_cur_req, 25777 sizeof (struct sd_thr_request)); 25778 continue; 25779 } 25780 25781 /* dequeue the request */ 25782 sd_mhreq = sd_tr.srq_thr_cur_req; 25783 sd_tr.srq_thr_req_head = 25784 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25785 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25786 25787 /* 25788 * Reclaim reservation only if SD_RESERVE is still set. There 25789 * may have been a call to MHIOCRELEASE before we got here. 25790 */ 25791 mutex_enter(SD_MUTEX(un)); 25792 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25793 /* 25794 * Note: The SD_LOST_RESERVE flag is cleared before 25795 * reclaiming the reservation. If this is done after the 25796 * call to sd_reserve_release a reservation loss in the 25797 * window between pkt completion of reserve cmd and 25798 * mutex_enter below may not be recognized 25799 */ 25800 un->un_resvd_status &= ~SD_LOST_RESERVE; 25801 mutex_exit(SD_MUTEX(un)); 25802 25803 if (sd_reserve_release(sd_mhreq->dev, 25804 SD_RESERVE) == 0) { 25805 mutex_enter(SD_MUTEX(un)); 25806 un->un_resvd_status |= SD_RESERVE; 25807 mutex_exit(SD_MUTEX(un)); 25808 SD_INFO(SD_LOG_IOCTL_MHD, un, 25809 "sd_resv_reclaim_thread: " 25810 "Reservation Recovered\n"); 25811 } else { 25812 mutex_enter(SD_MUTEX(un)); 25813 un->un_resvd_status |= SD_LOST_RESERVE; 25814 mutex_exit(SD_MUTEX(un)); 25815 SD_INFO(SD_LOG_IOCTL_MHD, un, 25816 "sd_resv_reclaim_thread: Failed " 25817 "Reservation Recovery\n"); 25818 } 25819 } else { 25820 mutex_exit(SD_MUTEX(un)); 25821 } 25822 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25823 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25824 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25825 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25826 /* 25827 * wakeup the destroy thread if anyone is waiting on 25828 * us to complete. 25829 */ 25830 cv_signal(&sd_tr.srq_inprocess_cv); 25831 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25832 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25833 } 25834 25835 /* 25836 * cleanup the sd_tr structure now that this thread will not exist 25837 */ 25838 ASSERT(sd_tr.srq_thr_req_head == NULL); 25839 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25840 sd_tr.srq_resv_reclaim_thread = NULL; 25841 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25842 thread_exit(); 25843 } 25844 25845 25846 /* 25847 * Function: sd_rmv_resv_reclaim_req() 25848 * 25849 * Description: This function removes any pending reservation reclaim requests 25850 * for the specified device. 25851 * 25852 * Arguments: dev - the device 'dev_t' 25853 */ 25854 25855 static void 25856 sd_rmv_resv_reclaim_req(dev_t dev) 25857 { 25858 struct sd_thr_request *sd_mhreq; 25859 struct sd_thr_request *sd_prev; 25860 25861 /* Remove a reservation reclaim request from the list */ 25862 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25863 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25864 /* 25865 * We are attempting to reinstate reservation for 25866 * this device. We wait for sd_reserve_release() 25867 * to return before we return. 25868 */ 25869 cv_wait(&sd_tr.srq_inprocess_cv, 25870 &sd_tr.srq_resv_reclaim_mutex); 25871 } else { 25872 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25873 if (sd_mhreq && sd_mhreq->dev == dev) { 25874 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25875 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25876 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25877 return; 25878 } 25879 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25880 if (sd_mhreq && sd_mhreq->dev == dev) { 25881 break; 25882 } 25883 sd_prev = sd_mhreq; 25884 } 25885 if (sd_mhreq != NULL) { 25886 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25887 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25888 } 25889 } 25890 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25891 } 25892 25893 25894 /* 25895 * Function: sd_mhd_reset_notify_cb() 25896 * 25897 * Description: This is a call back function for scsi_reset_notify. This 25898 * function updates the softstate reserved status and logs the 25899 * reset. The driver scsi watch facility callback function 25900 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25901 * will reclaim the reservation. 25902 * 25903 * Arguments: arg - driver soft state (unit) structure 25904 */ 25905 25906 static void 25907 sd_mhd_reset_notify_cb(caddr_t arg) 25908 { 25909 struct sd_lun *un = (struct sd_lun *)arg; 25910 25911 mutex_enter(SD_MUTEX(un)); 25912 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25913 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25914 SD_INFO(SD_LOG_IOCTL_MHD, un, 25915 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25916 } 25917 mutex_exit(SD_MUTEX(un)); 25918 } 25919 25920 25921 /* 25922 * Function: sd_take_ownership() 25923 * 25924 * Description: This routine implements an algorithm to achieve a stable 25925 * reservation on disks which don't implement priority reserve, 25926 * and makes sure that other host lose re-reservation attempts. 25927 * This algorithm contains of a loop that keeps issuing the RESERVE 25928 * for some period of time (min_ownership_delay, default 6 seconds) 25929 * During that loop, it looks to see if there has been a bus device 25930 * reset or bus reset (both of which cause an existing reservation 25931 * to be lost). If the reservation is lost issue RESERVE until a 25932 * period of min_ownership_delay with no resets has gone by, or 25933 * until max_ownership_delay has expired. This loop ensures that 25934 * the host really did manage to reserve the device, in spite of 25935 * resets. The looping for min_ownership_delay (default six 25936 * seconds) is important to early generation clustering products, 25937 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25938 * MHIOCENFAILFAST periodic timer of two seconds. By having 25939 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25940 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25941 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25942 * have already noticed, via the MHIOCENFAILFAST polling, that it 25943 * no longer "owns" the disk and will have panicked itself. Thus, 25944 * the host issuing the MHIOCTKOWN is assured (with timing 25945 * dependencies) that by the time it actually starts to use the 25946 * disk for real work, the old owner is no longer accessing it. 25947 * 25948 * min_ownership_delay is the minimum amount of time for which the 25949 * disk must be reserved continuously devoid of resets before the 25950 * MHIOCTKOWN ioctl will return success. 25951 * 25952 * max_ownership_delay indicates the amount of time by which the 25953 * take ownership should succeed or timeout with an error. 25954 * 25955 * Arguments: dev - the device 'dev_t' 25956 * *p - struct containing timing info. 25957 * 25958 * Return Code: 0 for success or error code 25959 */ 25960 25961 static int 25962 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25963 { 25964 struct sd_lun *un; 25965 int rval; 25966 int err; 25967 int reservation_count = 0; 25968 int min_ownership_delay = 6000000; /* in usec */ 25969 int max_ownership_delay = 30000000; /* in usec */ 25970 clock_t start_time; /* starting time of this algorithm */ 25971 clock_t end_time; /* time limit for giving up */ 25972 clock_t ownership_time; /* time limit for stable ownership */ 25973 clock_t current_time; 25974 clock_t previous_current_time; 25975 25976 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25977 return (ENXIO); 25978 } 25979 25980 /* 25981 * Attempt a device reservation. A priority reservation is requested. 25982 */ 25983 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25984 != SD_SUCCESS) { 25985 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25986 "sd_take_ownership: return(1)=%d\n", rval); 25987 return (rval); 25988 } 25989 25990 /* Update the softstate reserved status to indicate the reservation */ 25991 mutex_enter(SD_MUTEX(un)); 25992 un->un_resvd_status |= SD_RESERVE; 25993 un->un_resvd_status &= 25994 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25995 mutex_exit(SD_MUTEX(un)); 25996 25997 if (p != NULL) { 25998 if (p->min_ownership_delay != 0) { 25999 min_ownership_delay = p->min_ownership_delay * 1000; 26000 } 26001 if (p->max_ownership_delay != 0) { 26002 max_ownership_delay = p->max_ownership_delay * 1000; 26003 } 26004 } 26005 SD_INFO(SD_LOG_IOCTL_MHD, un, 26006 "sd_take_ownership: min, max delays: %d, %d\n", 26007 min_ownership_delay, max_ownership_delay); 26008 26009 start_time = ddi_get_lbolt(); 26010 current_time = start_time; 26011 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26012 end_time = start_time + drv_usectohz(max_ownership_delay); 26013 26014 while (current_time - end_time < 0) { 26015 delay(drv_usectohz(500000)); 26016 26017 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26018 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26019 mutex_enter(SD_MUTEX(un)); 26020 rval = (un->un_resvd_status & 26021 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26022 mutex_exit(SD_MUTEX(un)); 26023 break; 26024 } 26025 } 26026 previous_current_time = current_time; 26027 current_time = ddi_get_lbolt(); 26028 mutex_enter(SD_MUTEX(un)); 26029 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26030 ownership_time = ddi_get_lbolt() + 26031 drv_usectohz(min_ownership_delay); 26032 reservation_count = 0; 26033 } else { 26034 reservation_count++; 26035 } 26036 un->un_resvd_status |= SD_RESERVE; 26037 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26038 mutex_exit(SD_MUTEX(un)); 26039 26040 SD_INFO(SD_LOG_IOCTL_MHD, un, 26041 "sd_take_ownership: ticks for loop iteration=%ld, " 26042 "reservation=%s\n", (current_time - previous_current_time), 26043 reservation_count ? "ok" : "reclaimed"); 26044 26045 if (current_time - ownership_time >= 0 && 26046 reservation_count >= 4) { 26047 rval = 0; /* Achieved a stable ownership */ 26048 break; 26049 } 26050 if (current_time - end_time >= 0) { 26051 rval = EACCES; /* No ownership in max possible time */ 26052 break; 26053 } 26054 } 26055 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26056 "sd_take_ownership: return(2)=%d\n", rval); 26057 return (rval); 26058 } 26059 26060 26061 /* 26062 * Function: sd_reserve_release() 26063 * 26064 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26065 * PRIORITY RESERVE commands based on a user specified command type 26066 * 26067 * Arguments: dev - the device 'dev_t' 26068 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26069 * SD_RESERVE, SD_RELEASE 26070 * 26071 * Return Code: 0 or Error Code 26072 */ 26073 26074 static int 26075 sd_reserve_release(dev_t dev, int cmd) 26076 { 26077 struct uscsi_cmd *com = NULL; 26078 struct sd_lun *un = NULL; 26079 char cdb[CDB_GROUP0]; 26080 int rval; 26081 26082 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26083 (cmd == SD_PRIORITY_RESERVE)); 26084 26085 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26086 return (ENXIO); 26087 } 26088 26089 /* instantiate and initialize the command and cdb */ 26090 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26091 bzero(cdb, CDB_GROUP0); 26092 com->uscsi_flags = USCSI_SILENT; 26093 com->uscsi_timeout = un->un_reserve_release_time; 26094 com->uscsi_cdblen = CDB_GROUP0; 26095 com->uscsi_cdb = cdb; 26096 if (cmd == SD_RELEASE) { 26097 cdb[0] = SCMD_RELEASE; 26098 } else { 26099 cdb[0] = SCMD_RESERVE; 26100 } 26101 26102 /* Send the command. */ 26103 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26104 UIO_SYSSPACE, SD_PATH_STANDARD); 26105 26106 /* 26107 * "break" a reservation that is held by another host, by issuing a 26108 * reset if priority reserve is desired, and we could not get the 26109 * device. 26110 */ 26111 if ((cmd == SD_PRIORITY_RESERVE) && 26112 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26113 /* 26114 * First try to reset the LUN. If we cannot, then try a target 26115 * reset, followed by a bus reset if the target reset fails. 26116 */ 26117 int reset_retval = 0; 26118 if (un->un_f_lun_reset_enabled == TRUE) { 26119 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26120 } 26121 if (reset_retval == 0) { 26122 /* The LUN reset either failed or was not issued */ 26123 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26124 } 26125 if ((reset_retval == 0) && 26126 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26127 rval = EIO; 26128 kmem_free(com, sizeof (*com)); 26129 return (rval); 26130 } 26131 26132 bzero(com, sizeof (struct uscsi_cmd)); 26133 com->uscsi_flags = USCSI_SILENT; 26134 com->uscsi_cdb = cdb; 26135 com->uscsi_cdblen = CDB_GROUP0; 26136 com->uscsi_timeout = 5; 26137 26138 /* 26139 * Reissue the last reserve command, this time without request 26140 * sense. Assume that it is just a regular reserve command. 26141 */ 26142 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26143 UIO_SYSSPACE, SD_PATH_STANDARD); 26144 } 26145 26146 /* Return an error if still getting a reservation conflict. */ 26147 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26148 rval = EACCES; 26149 } 26150 26151 kmem_free(com, sizeof (*com)); 26152 return (rval); 26153 } 26154 26155 26156 #define SD_NDUMP_RETRIES 12 26157 /* 26158 * System Crash Dump routine 26159 */ 26160 26161 static int 26162 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26163 { 26164 int instance; 26165 int partition; 26166 int i; 26167 int err; 26168 struct sd_lun *un; 26169 struct dk_map *lp; 26170 struct scsi_pkt *wr_pktp; 26171 struct buf *wr_bp; 26172 struct buf wr_buf; 26173 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26174 daddr_t tgt_blkno; /* rmw - blkno for target */ 26175 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26176 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26177 size_t io_start_offset; 26178 int doing_rmw = FALSE; 26179 int rval; 26180 #if defined(__i386) || defined(__amd64) 26181 ssize_t dma_resid; 26182 daddr_t oblkno; 26183 #endif 26184 26185 instance = SDUNIT(dev); 26186 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26187 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26188 return (ENXIO); 26189 } 26190 26191 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26192 26193 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26194 26195 partition = SDPART(dev); 26196 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26197 26198 /* Validate blocks to dump at against partition size. */ 26199 lp = &un->un_map[partition]; 26200 if ((blkno + nblk) > lp->dkl_nblk) { 26201 SD_TRACE(SD_LOG_DUMP, un, 26202 "sddump: dump range larger than partition: " 26203 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26204 blkno, nblk, lp->dkl_nblk); 26205 return (EINVAL); 26206 } 26207 26208 mutex_enter(&un->un_pm_mutex); 26209 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26210 struct scsi_pkt *start_pktp; 26211 26212 mutex_exit(&un->un_pm_mutex); 26213 26214 /* 26215 * use pm framework to power on HBA 1st 26216 */ 26217 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26218 26219 /* 26220 * Dump no long uses sdpower to power on a device, it's 26221 * in-line here so it can be done in polled mode. 26222 */ 26223 26224 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26225 26226 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26227 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26228 26229 if (start_pktp == NULL) { 26230 /* We were not given a SCSI packet, fail. */ 26231 return (EIO); 26232 } 26233 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26234 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26235 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26236 start_pktp->pkt_flags = FLAG_NOINTR; 26237 26238 mutex_enter(SD_MUTEX(un)); 26239 SD_FILL_SCSI1_LUN(un, start_pktp); 26240 mutex_exit(SD_MUTEX(un)); 26241 /* 26242 * Scsi_poll returns 0 (success) if the command completes and 26243 * the status block is STATUS_GOOD. 26244 */ 26245 if (sd_scsi_poll(un, start_pktp) != 0) { 26246 scsi_destroy_pkt(start_pktp); 26247 return (EIO); 26248 } 26249 scsi_destroy_pkt(start_pktp); 26250 (void) sd_ddi_pm_resume(un); 26251 } else { 26252 mutex_exit(&un->un_pm_mutex); 26253 } 26254 26255 mutex_enter(SD_MUTEX(un)); 26256 un->un_throttle = 0; 26257 26258 /* 26259 * The first time through, reset the specific target device. 26260 * However, when cpr calls sddump we know that sd is in a 26261 * a good state so no bus reset is required. 26262 * Clear sense data via Request Sense cmd. 26263 * In sddump we don't care about allow_bus_device_reset anymore 26264 */ 26265 26266 if ((un->un_state != SD_STATE_SUSPENDED) && 26267 (un->un_state != SD_STATE_DUMPING)) { 26268 26269 New_state(un, SD_STATE_DUMPING); 26270 26271 if (un->un_f_is_fibre == FALSE) { 26272 mutex_exit(SD_MUTEX(un)); 26273 /* 26274 * Attempt a bus reset for parallel scsi. 26275 * 26276 * Note: A bus reset is required because on some host 26277 * systems (i.e. E420R) a bus device reset is 26278 * insufficient to reset the state of the target. 26279 * 26280 * Note: Don't issue the reset for fibre-channel, 26281 * because this tends to hang the bus (loop) for 26282 * too long while everyone is logging out and in 26283 * and the deadman timer for dumping will fire 26284 * before the dump is complete. 26285 */ 26286 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26287 mutex_enter(SD_MUTEX(un)); 26288 Restore_state(un); 26289 mutex_exit(SD_MUTEX(un)); 26290 return (EIO); 26291 } 26292 26293 /* Delay to give the device some recovery time. */ 26294 drv_usecwait(10000); 26295 26296 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26297 SD_INFO(SD_LOG_DUMP, un, 26298 "sddump: sd_send_polled_RQS failed\n"); 26299 } 26300 mutex_enter(SD_MUTEX(un)); 26301 } 26302 } 26303 26304 /* 26305 * Convert the partition-relative block number to a 26306 * disk physical block number. 26307 */ 26308 blkno += un->un_offset[partition]; 26309 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26310 26311 26312 /* 26313 * Check if the device has a non-512 block size. 26314 */ 26315 wr_bp = NULL; 26316 if (NOT_DEVBSIZE(un)) { 26317 tgt_byte_offset = blkno * un->un_sys_blocksize; 26318 tgt_byte_count = nblk * un->un_sys_blocksize; 26319 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26320 (tgt_byte_count % un->un_tgt_blocksize)) { 26321 doing_rmw = TRUE; 26322 /* 26323 * Calculate the block number and number of block 26324 * in terms of the media block size. 26325 */ 26326 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26327 tgt_nblk = 26328 ((tgt_byte_offset + tgt_byte_count + 26329 (un->un_tgt_blocksize - 1)) / 26330 un->un_tgt_blocksize) - tgt_blkno; 26331 26332 /* 26333 * Invoke the routine which is going to do read part 26334 * of read-modify-write. 26335 * Note that this routine returns a pointer to 26336 * a valid bp in wr_bp. 26337 */ 26338 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26339 &wr_bp); 26340 if (err) { 26341 mutex_exit(SD_MUTEX(un)); 26342 return (err); 26343 } 26344 /* 26345 * Offset is being calculated as - 26346 * (original block # * system block size) - 26347 * (new block # * target block size) 26348 */ 26349 io_start_offset = 26350 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26351 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26352 26353 ASSERT((io_start_offset >= 0) && 26354 (io_start_offset < un->un_tgt_blocksize)); 26355 /* 26356 * Do the modify portion of read modify write. 26357 */ 26358 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26359 (size_t)nblk * un->un_sys_blocksize); 26360 } else { 26361 doing_rmw = FALSE; 26362 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26363 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26364 } 26365 26366 /* Convert blkno and nblk to target blocks */ 26367 blkno = tgt_blkno; 26368 nblk = tgt_nblk; 26369 } else { 26370 wr_bp = &wr_buf; 26371 bzero(wr_bp, sizeof (struct buf)); 26372 wr_bp->b_flags = B_BUSY; 26373 wr_bp->b_un.b_addr = addr; 26374 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26375 wr_bp->b_resid = 0; 26376 } 26377 26378 mutex_exit(SD_MUTEX(un)); 26379 26380 /* 26381 * Obtain a SCSI packet for the write command. 26382 * It should be safe to call the allocator here without 26383 * worrying about being locked for DVMA mapping because 26384 * the address we're passed is already a DVMA mapping 26385 * 26386 * We are also not going to worry about semaphore ownership 26387 * in the dump buffer. Dumping is single threaded at present. 26388 */ 26389 26390 wr_pktp = NULL; 26391 26392 #if defined(__i386) || defined(__amd64) 26393 dma_resid = wr_bp->b_bcount; 26394 oblkno = blkno; 26395 while (dma_resid != 0) { 26396 #endif 26397 26398 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26399 wr_bp->b_flags &= ~B_ERROR; 26400 26401 #if defined(__i386) || defined(__amd64) 26402 blkno = oblkno + 26403 ((wr_bp->b_bcount - dma_resid) / 26404 un->un_tgt_blocksize); 26405 nblk = dma_resid / un->un_tgt_blocksize; 26406 26407 if (wr_pktp) { 26408 /* Partial DMA transfers after initial transfer */ 26409 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26410 blkno, nblk); 26411 } else { 26412 /* Initial transfer */ 26413 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26414 un->un_pkt_flags, NULL_FUNC, NULL, 26415 blkno, nblk); 26416 } 26417 #else 26418 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26419 0, NULL_FUNC, NULL, blkno, nblk); 26420 #endif 26421 26422 if (rval == 0) { 26423 /* We were given a SCSI packet, continue. */ 26424 break; 26425 } 26426 26427 if (i == 0) { 26428 if (wr_bp->b_flags & B_ERROR) { 26429 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26430 "no resources for dumping; " 26431 "error code: 0x%x, retrying", 26432 geterror(wr_bp)); 26433 } else { 26434 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26435 "no resources for dumping; retrying"); 26436 } 26437 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26438 if (wr_bp->b_flags & B_ERROR) { 26439 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26440 "no resources for dumping; error code: " 26441 "0x%x, retrying\n", geterror(wr_bp)); 26442 } 26443 } else { 26444 if (wr_bp->b_flags & B_ERROR) { 26445 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26446 "no resources for dumping; " 26447 "error code: 0x%x, retries failed, " 26448 "giving up.\n", geterror(wr_bp)); 26449 } else { 26450 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26451 "no resources for dumping; " 26452 "retries failed, giving up.\n"); 26453 } 26454 mutex_enter(SD_MUTEX(un)); 26455 Restore_state(un); 26456 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26457 mutex_exit(SD_MUTEX(un)); 26458 scsi_free_consistent_buf(wr_bp); 26459 } else { 26460 mutex_exit(SD_MUTEX(un)); 26461 } 26462 return (EIO); 26463 } 26464 drv_usecwait(10000); 26465 } 26466 26467 #if defined(__i386) || defined(__amd64) 26468 /* 26469 * save the resid from PARTIAL_DMA 26470 */ 26471 dma_resid = wr_pktp->pkt_resid; 26472 if (dma_resid != 0) 26473 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26474 wr_pktp->pkt_resid = 0; 26475 #endif 26476 26477 /* SunBug 1222170 */ 26478 wr_pktp->pkt_flags = FLAG_NOINTR; 26479 26480 err = EIO; 26481 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26482 26483 /* 26484 * Scsi_poll returns 0 (success) if the command completes and 26485 * the status block is STATUS_GOOD. We should only check 26486 * errors if this condition is not true. Even then we should 26487 * send our own request sense packet only if we have a check 26488 * condition and auto request sense has not been performed by 26489 * the hba. 26490 */ 26491 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26492 26493 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26494 (wr_pktp->pkt_resid == 0)) { 26495 err = SD_SUCCESS; 26496 break; 26497 } 26498 26499 /* 26500 * Check CMD_DEV_GONE 1st, give up if device is gone. 26501 */ 26502 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26503 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26504 "Device is gone\n"); 26505 break; 26506 } 26507 26508 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26509 SD_INFO(SD_LOG_DUMP, un, 26510 "sddump: write failed with CHECK, try # %d\n", i); 26511 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26512 (void) sd_send_polled_RQS(un); 26513 } 26514 26515 continue; 26516 } 26517 26518 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26519 int reset_retval = 0; 26520 26521 SD_INFO(SD_LOG_DUMP, un, 26522 "sddump: write failed with BUSY, try # %d\n", i); 26523 26524 if (un->un_f_lun_reset_enabled == TRUE) { 26525 reset_retval = scsi_reset(SD_ADDRESS(un), 26526 RESET_LUN); 26527 } 26528 if (reset_retval == 0) { 26529 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26530 } 26531 (void) sd_send_polled_RQS(un); 26532 26533 } else { 26534 SD_INFO(SD_LOG_DUMP, un, 26535 "sddump: write failed with 0x%x, try # %d\n", 26536 SD_GET_PKT_STATUS(wr_pktp), i); 26537 mutex_enter(SD_MUTEX(un)); 26538 sd_reset_target(un, wr_pktp); 26539 mutex_exit(SD_MUTEX(un)); 26540 } 26541 26542 /* 26543 * If we are not getting anywhere with lun/target resets, 26544 * let's reset the bus. 26545 */ 26546 if (i == SD_NDUMP_RETRIES/2) { 26547 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26548 (void) sd_send_polled_RQS(un); 26549 } 26550 26551 } 26552 #if defined(__i386) || defined(__amd64) 26553 } /* dma_resid */ 26554 #endif 26555 26556 scsi_destroy_pkt(wr_pktp); 26557 mutex_enter(SD_MUTEX(un)); 26558 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26559 mutex_exit(SD_MUTEX(un)); 26560 scsi_free_consistent_buf(wr_bp); 26561 } else { 26562 mutex_exit(SD_MUTEX(un)); 26563 } 26564 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26565 return (err); 26566 } 26567 26568 /* 26569 * Function: sd_scsi_poll() 26570 * 26571 * Description: This is a wrapper for the scsi_poll call. 26572 * 26573 * Arguments: sd_lun - The unit structure 26574 * scsi_pkt - The scsi packet being sent to the device. 26575 * 26576 * Return Code: 0 - Command completed successfully with good status 26577 * -1 - Command failed. This could indicate a check condition 26578 * or other status value requiring recovery action. 26579 * 26580 */ 26581 26582 static int 26583 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26584 { 26585 int status; 26586 26587 ASSERT(un != NULL); 26588 ASSERT(!mutex_owned(SD_MUTEX(un))); 26589 ASSERT(pktp != NULL); 26590 26591 status = SD_SUCCESS; 26592 26593 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26594 pktp->pkt_flags |= un->un_tagflags; 26595 pktp->pkt_flags &= ~FLAG_NODISCON; 26596 } 26597 26598 status = sd_ddi_scsi_poll(pktp); 26599 /* 26600 * Scsi_poll returns 0 (success) if the command completes and the 26601 * status block is STATUS_GOOD. We should only check errors if this 26602 * condition is not true. Even then we should send our own request 26603 * sense packet only if we have a check condition and auto 26604 * request sense has not been performed by the hba. 26605 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26606 */ 26607 if ((status != SD_SUCCESS) && 26608 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26609 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26610 (pktp->pkt_reason != CMD_DEV_GONE)) 26611 (void) sd_send_polled_RQS(un); 26612 26613 return (status); 26614 } 26615 26616 /* 26617 * Function: sd_send_polled_RQS() 26618 * 26619 * Description: This sends the request sense command to a device. 26620 * 26621 * Arguments: sd_lun - The unit structure 26622 * 26623 * Return Code: 0 - Command completed successfully with good status 26624 * -1 - Command failed. 26625 * 26626 */ 26627 26628 static int 26629 sd_send_polled_RQS(struct sd_lun *un) 26630 { 26631 int ret_val; 26632 struct scsi_pkt *rqs_pktp; 26633 struct buf *rqs_bp; 26634 26635 ASSERT(un != NULL); 26636 ASSERT(!mutex_owned(SD_MUTEX(un))); 26637 26638 ret_val = SD_SUCCESS; 26639 26640 rqs_pktp = un->un_rqs_pktp; 26641 rqs_bp = un->un_rqs_bp; 26642 26643 mutex_enter(SD_MUTEX(un)); 26644 26645 if (un->un_sense_isbusy) { 26646 ret_val = SD_FAILURE; 26647 mutex_exit(SD_MUTEX(un)); 26648 return (ret_val); 26649 } 26650 26651 /* 26652 * If the request sense buffer (and packet) is not in use, 26653 * let's set the un_sense_isbusy and send our packet 26654 */ 26655 un->un_sense_isbusy = 1; 26656 rqs_pktp->pkt_resid = 0; 26657 rqs_pktp->pkt_reason = 0; 26658 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26659 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26660 26661 mutex_exit(SD_MUTEX(un)); 26662 26663 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26664 " 0x%p\n", rqs_bp->b_un.b_addr); 26665 26666 /* 26667 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26668 * axle - it has a call into us! 26669 */ 26670 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26671 SD_INFO(SD_LOG_COMMON, un, 26672 "sd_send_polled_RQS: RQS failed\n"); 26673 } 26674 26675 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26676 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26677 26678 mutex_enter(SD_MUTEX(un)); 26679 un->un_sense_isbusy = 0; 26680 mutex_exit(SD_MUTEX(un)); 26681 26682 return (ret_val); 26683 } 26684 26685 /* 26686 * Defines needed for localized version of the scsi_poll routine. 26687 */ 26688 #define SD_CSEC 10000 /* usecs */ 26689 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26690 26691 26692 /* 26693 * Function: sd_ddi_scsi_poll() 26694 * 26695 * Description: Localized version of the scsi_poll routine. The purpose is to 26696 * send a scsi_pkt to a device as a polled command. This version 26697 * is to ensure more robust handling of transport errors. 26698 * Specifically this routine cures not ready, coming ready 26699 * transition for power up and reset of sonoma's. This can take 26700 * up to 45 seconds for power-on and 20 seconds for reset of a 26701 * sonoma lun. 26702 * 26703 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26704 * 26705 * Return Code: 0 - Command completed successfully with good status 26706 * -1 - Command failed. 26707 * 26708 */ 26709 26710 static int 26711 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26712 { 26713 int busy_count; 26714 int timeout; 26715 int rval = SD_FAILURE; 26716 int savef; 26717 uint8_t *sensep; 26718 long savet; 26719 void (*savec)(); 26720 /* 26721 * The following is defined in machdep.c and is used in determining if 26722 * the scsi transport system will do polled I/O instead of interrupt 26723 * I/O when called from xx_dump(). 26724 */ 26725 extern int do_polled_io; 26726 26727 /* 26728 * save old flags in pkt, to restore at end 26729 */ 26730 savef = pkt->pkt_flags; 26731 savec = pkt->pkt_comp; 26732 savet = pkt->pkt_time; 26733 26734 pkt->pkt_flags |= FLAG_NOINTR; 26735 26736 /* 26737 * XXX there is nothing in the SCSA spec that states that we should not 26738 * do a callback for polled cmds; however, removing this will break sd 26739 * and probably other target drivers 26740 */ 26741 pkt->pkt_comp = NULL; 26742 26743 /* 26744 * we don't like a polled command without timeout. 26745 * 60 seconds seems long enough. 26746 */ 26747 if (pkt->pkt_time == 0) { 26748 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26749 } 26750 26751 /* 26752 * Send polled cmd. 26753 * 26754 * We do some error recovery for various errors. Tran_busy, 26755 * queue full, and non-dispatched commands are retried every 10 msec. 26756 * as they are typically transient failures. Busy status and Not 26757 * Ready are retried every second as this status takes a while to 26758 * change. Unit attention is retried for pkt_time (60) times 26759 * with no delay. 26760 */ 26761 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26762 26763 for (busy_count = 0; busy_count < timeout; busy_count++) { 26764 int rc; 26765 int poll_delay; 26766 26767 /* 26768 * Initialize pkt status variables. 26769 */ 26770 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26771 26772 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26773 if (rc != TRAN_BUSY) { 26774 /* Transport failed - give up. */ 26775 break; 26776 } else { 26777 /* Transport busy - try again. */ 26778 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26779 } 26780 } else { 26781 /* 26782 * Transport accepted - check pkt status. 26783 */ 26784 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26785 if (pkt->pkt_reason == CMD_CMPLT && 26786 rc == STATUS_CHECK && 26787 pkt->pkt_state & STATE_ARQ_DONE) { 26788 struct scsi_arq_status *arqstat = 26789 (struct scsi_arq_status *)(pkt->pkt_scbp); 26790 26791 sensep = (uint8_t *)&arqstat->sts_sensedata; 26792 } else { 26793 sensep = NULL; 26794 } 26795 26796 if ((pkt->pkt_reason == CMD_CMPLT) && 26797 (rc == STATUS_GOOD)) { 26798 /* No error - we're done */ 26799 rval = SD_SUCCESS; 26800 break; 26801 26802 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26803 /* Lost connection - give up */ 26804 break; 26805 26806 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26807 (pkt->pkt_state == 0)) { 26808 /* Pkt not dispatched - try again. */ 26809 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26810 26811 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26812 (rc == STATUS_QFULL)) { 26813 /* Queue full - try again. */ 26814 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26815 26816 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26817 (rc == STATUS_BUSY)) { 26818 /* Busy - try again. */ 26819 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26820 busy_count += (SD_SEC_TO_CSEC - 1); 26821 26822 } else if ((sensep != NULL) && 26823 (scsi_sense_key(sensep) == 26824 KEY_UNIT_ATTENTION)) { 26825 /* Unit Attention - try again */ 26826 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26827 continue; 26828 26829 } else if ((sensep != NULL) && 26830 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26831 (scsi_sense_asc(sensep) == 0x04) && 26832 (scsi_sense_ascq(sensep) == 0x01)) { 26833 /* Not ready -> ready - try again. */ 26834 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26835 busy_count += (SD_SEC_TO_CSEC - 1); 26836 26837 } else { 26838 /* BAD status - give up. */ 26839 break; 26840 } 26841 } 26842 26843 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26844 !do_polled_io) { 26845 delay(drv_usectohz(poll_delay)); 26846 } else { 26847 /* we busy wait during cpr_dump or interrupt threads */ 26848 drv_usecwait(poll_delay); 26849 } 26850 } 26851 26852 pkt->pkt_flags = savef; 26853 pkt->pkt_comp = savec; 26854 pkt->pkt_time = savet; 26855 return (rval); 26856 } 26857 26858 26859 /* 26860 * Function: sd_persistent_reservation_in_read_keys 26861 * 26862 * Description: This routine is the driver entry point for handling CD-ROM 26863 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26864 * by sending the SCSI-3 PRIN commands to the device. 26865 * Processes the read keys command response by copying the 26866 * reservation key information into the user provided buffer. 26867 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26868 * 26869 * Arguments: un - Pointer to soft state struct for the target. 26870 * usrp - user provided pointer to multihost Persistent In Read 26871 * Keys structure (mhioc_inkeys_t) 26872 * flag - this argument is a pass through to ddi_copyxxx() 26873 * directly from the mode argument of ioctl(). 26874 * 26875 * Return Code: 0 - Success 26876 * EACCES 26877 * ENOTSUP 26878 * errno return code from sd_send_scsi_cmd() 26879 * 26880 * Context: Can sleep. Does not return until command is completed. 26881 */ 26882 26883 static int 26884 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26885 mhioc_inkeys_t *usrp, int flag) 26886 { 26887 #ifdef _MULTI_DATAMODEL 26888 struct mhioc_key_list32 li32; 26889 #endif 26890 sd_prin_readkeys_t *in; 26891 mhioc_inkeys_t *ptr; 26892 mhioc_key_list_t li; 26893 uchar_t *data_bufp; 26894 int data_len; 26895 int rval; 26896 size_t copysz; 26897 26898 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26899 return (EINVAL); 26900 } 26901 bzero(&li, sizeof (mhioc_key_list_t)); 26902 26903 /* 26904 * Get the listsize from user 26905 */ 26906 #ifdef _MULTI_DATAMODEL 26907 26908 switch (ddi_model_convert_from(flag & FMODELS)) { 26909 case DDI_MODEL_ILP32: 26910 copysz = sizeof (struct mhioc_key_list32); 26911 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26912 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26913 "sd_persistent_reservation_in_read_keys: " 26914 "failed ddi_copyin: mhioc_key_list32_t\n"); 26915 rval = EFAULT; 26916 goto done; 26917 } 26918 li.listsize = li32.listsize; 26919 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26920 break; 26921 26922 case DDI_MODEL_NONE: 26923 copysz = sizeof (mhioc_key_list_t); 26924 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26925 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26926 "sd_persistent_reservation_in_read_keys: " 26927 "failed ddi_copyin: mhioc_key_list_t\n"); 26928 rval = EFAULT; 26929 goto done; 26930 } 26931 break; 26932 } 26933 26934 #else /* ! _MULTI_DATAMODEL */ 26935 copysz = sizeof (mhioc_key_list_t); 26936 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26937 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26938 "sd_persistent_reservation_in_read_keys: " 26939 "failed ddi_copyin: mhioc_key_list_t\n"); 26940 rval = EFAULT; 26941 goto done; 26942 } 26943 #endif 26944 26945 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26946 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26947 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26948 26949 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26950 data_len, data_bufp)) != 0) { 26951 goto done; 26952 } 26953 in = (sd_prin_readkeys_t *)data_bufp; 26954 ptr->generation = BE_32(in->generation); 26955 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26956 26957 /* 26958 * Return the min(listsize, listlen) keys 26959 */ 26960 #ifdef _MULTI_DATAMODEL 26961 26962 switch (ddi_model_convert_from(flag & FMODELS)) { 26963 case DDI_MODEL_ILP32: 26964 li32.listlen = li.listlen; 26965 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26966 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26967 "sd_persistent_reservation_in_read_keys: " 26968 "failed ddi_copyout: mhioc_key_list32_t\n"); 26969 rval = EFAULT; 26970 goto done; 26971 } 26972 break; 26973 26974 case DDI_MODEL_NONE: 26975 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26976 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26977 "sd_persistent_reservation_in_read_keys: " 26978 "failed ddi_copyout: mhioc_key_list_t\n"); 26979 rval = EFAULT; 26980 goto done; 26981 } 26982 break; 26983 } 26984 26985 #else /* ! _MULTI_DATAMODEL */ 26986 26987 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26988 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26989 "sd_persistent_reservation_in_read_keys: " 26990 "failed ddi_copyout: mhioc_key_list_t\n"); 26991 rval = EFAULT; 26992 goto done; 26993 } 26994 26995 #endif /* _MULTI_DATAMODEL */ 26996 26997 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26998 li.listsize * MHIOC_RESV_KEY_SIZE); 26999 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27000 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27001 "sd_persistent_reservation_in_read_keys: " 27002 "failed ddi_copyout: keylist\n"); 27003 rval = EFAULT; 27004 } 27005 done: 27006 kmem_free(data_bufp, data_len); 27007 return (rval); 27008 } 27009 27010 27011 /* 27012 * Function: sd_persistent_reservation_in_read_resv 27013 * 27014 * Description: This routine is the driver entry point for handling CD-ROM 27015 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27016 * by sending the SCSI-3 PRIN commands to the device. 27017 * Process the read persistent reservations command response by 27018 * copying the reservation information into the user provided 27019 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27020 * 27021 * Arguments: un - Pointer to soft state struct for the target. 27022 * usrp - user provided pointer to multihost Persistent In Read 27023 * Keys structure (mhioc_inkeys_t) 27024 * flag - this argument is a pass through to ddi_copyxxx() 27025 * directly from the mode argument of ioctl(). 27026 * 27027 * Return Code: 0 - Success 27028 * EACCES 27029 * ENOTSUP 27030 * errno return code from sd_send_scsi_cmd() 27031 * 27032 * Context: Can sleep. Does not return until command is completed. 27033 */ 27034 27035 static int 27036 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27037 mhioc_inresvs_t *usrp, int flag) 27038 { 27039 #ifdef _MULTI_DATAMODEL 27040 struct mhioc_resv_desc_list32 resvlist32; 27041 #endif 27042 sd_prin_readresv_t *in; 27043 mhioc_inresvs_t *ptr; 27044 sd_readresv_desc_t *readresv_ptr; 27045 mhioc_resv_desc_list_t resvlist; 27046 mhioc_resv_desc_t resvdesc; 27047 uchar_t *data_bufp; 27048 int data_len; 27049 int rval; 27050 int i; 27051 size_t copysz; 27052 mhioc_resv_desc_t *bufp; 27053 27054 if ((ptr = usrp) == NULL) { 27055 return (EINVAL); 27056 } 27057 27058 /* 27059 * Get the listsize from user 27060 */ 27061 #ifdef _MULTI_DATAMODEL 27062 switch (ddi_model_convert_from(flag & FMODELS)) { 27063 case DDI_MODEL_ILP32: 27064 copysz = sizeof (struct mhioc_resv_desc_list32); 27065 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27066 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27067 "sd_persistent_reservation_in_read_resv: " 27068 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27069 rval = EFAULT; 27070 goto done; 27071 } 27072 resvlist.listsize = resvlist32.listsize; 27073 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27074 break; 27075 27076 case DDI_MODEL_NONE: 27077 copysz = sizeof (mhioc_resv_desc_list_t); 27078 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27079 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27080 "sd_persistent_reservation_in_read_resv: " 27081 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27082 rval = EFAULT; 27083 goto done; 27084 } 27085 break; 27086 } 27087 #else /* ! _MULTI_DATAMODEL */ 27088 copysz = sizeof (mhioc_resv_desc_list_t); 27089 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27090 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27091 "sd_persistent_reservation_in_read_resv: " 27092 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27093 rval = EFAULT; 27094 goto done; 27095 } 27096 #endif /* ! _MULTI_DATAMODEL */ 27097 27098 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27099 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27100 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27101 27102 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27103 data_len, data_bufp)) != 0) { 27104 goto done; 27105 } 27106 in = (sd_prin_readresv_t *)data_bufp; 27107 ptr->generation = BE_32(in->generation); 27108 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27109 27110 /* 27111 * Return the min(listsize, listlen( keys 27112 */ 27113 #ifdef _MULTI_DATAMODEL 27114 27115 switch (ddi_model_convert_from(flag & FMODELS)) { 27116 case DDI_MODEL_ILP32: 27117 resvlist32.listlen = resvlist.listlen; 27118 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27119 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27120 "sd_persistent_reservation_in_read_resv: " 27121 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27122 rval = EFAULT; 27123 goto done; 27124 } 27125 break; 27126 27127 case DDI_MODEL_NONE: 27128 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27129 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27130 "sd_persistent_reservation_in_read_resv: " 27131 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27132 rval = EFAULT; 27133 goto done; 27134 } 27135 break; 27136 } 27137 27138 #else /* ! _MULTI_DATAMODEL */ 27139 27140 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27141 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27142 "sd_persistent_reservation_in_read_resv: " 27143 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27144 rval = EFAULT; 27145 goto done; 27146 } 27147 27148 #endif /* ! _MULTI_DATAMODEL */ 27149 27150 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27151 bufp = resvlist.list; 27152 copysz = sizeof (mhioc_resv_desc_t); 27153 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27154 i++, readresv_ptr++, bufp++) { 27155 27156 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27157 MHIOC_RESV_KEY_SIZE); 27158 resvdesc.type = readresv_ptr->type; 27159 resvdesc.scope = readresv_ptr->scope; 27160 resvdesc.scope_specific_addr = 27161 BE_32(readresv_ptr->scope_specific_addr); 27162 27163 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27164 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27165 "sd_persistent_reservation_in_read_resv: " 27166 "failed ddi_copyout: resvlist\n"); 27167 rval = EFAULT; 27168 goto done; 27169 } 27170 } 27171 done: 27172 kmem_free(data_bufp, data_len); 27173 return (rval); 27174 } 27175 27176 27177 /* 27178 * Function: sr_change_blkmode() 27179 * 27180 * Description: This routine is the driver entry point for handling CD-ROM 27181 * block mode ioctl requests. Support for returning and changing 27182 * the current block size in use by the device is implemented. The 27183 * LBA size is changed via a MODE SELECT Block Descriptor. 27184 * 27185 * This routine issues a mode sense with an allocation length of 27186 * 12 bytes for the mode page header and a single block descriptor. 27187 * 27188 * Arguments: dev - the device 'dev_t' 27189 * cmd - the request type; one of CDROMGBLKMODE (get) or 27190 * CDROMSBLKMODE (set) 27191 * data - current block size or requested block size 27192 * flag - this argument is a pass through to ddi_copyxxx() directly 27193 * from the mode argument of ioctl(). 27194 * 27195 * Return Code: the code returned by sd_send_scsi_cmd() 27196 * EINVAL if invalid arguments are provided 27197 * EFAULT if ddi_copyxxx() fails 27198 * ENXIO if fail ddi_get_soft_state 27199 * EIO if invalid mode sense block descriptor length 27200 * 27201 */ 27202 27203 static int 27204 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27205 { 27206 struct sd_lun *un = NULL; 27207 struct mode_header *sense_mhp, *select_mhp; 27208 struct block_descriptor *sense_desc, *select_desc; 27209 int current_bsize; 27210 int rval = EINVAL; 27211 uchar_t *sense = NULL; 27212 uchar_t *select = NULL; 27213 27214 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27215 27216 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27217 return (ENXIO); 27218 } 27219 27220 /* 27221 * The block length is changed via the Mode Select block descriptor, the 27222 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27223 * required as part of this routine. Therefore the mode sense allocation 27224 * length is specified to be the length of a mode page header and a 27225 * block descriptor. 27226 */ 27227 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27228 27229 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27230 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27231 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27232 "sr_change_blkmode: Mode Sense Failed\n"); 27233 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27234 return (rval); 27235 } 27236 27237 /* Check the block descriptor len to handle only 1 block descriptor */ 27238 sense_mhp = (struct mode_header *)sense; 27239 if ((sense_mhp->bdesc_length == 0) || 27240 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27241 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27242 "sr_change_blkmode: Mode Sense returned invalid block" 27243 " descriptor length\n"); 27244 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27245 return (EIO); 27246 } 27247 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27248 current_bsize = ((sense_desc->blksize_hi << 16) | 27249 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27250 27251 /* Process command */ 27252 switch (cmd) { 27253 case CDROMGBLKMODE: 27254 /* Return the block size obtained during the mode sense */ 27255 if (ddi_copyout(¤t_bsize, (void *)data, 27256 sizeof (int), flag) != 0) 27257 rval = EFAULT; 27258 break; 27259 case CDROMSBLKMODE: 27260 /* Validate the requested block size */ 27261 switch (data) { 27262 case CDROM_BLK_512: 27263 case CDROM_BLK_1024: 27264 case CDROM_BLK_2048: 27265 case CDROM_BLK_2056: 27266 case CDROM_BLK_2336: 27267 case CDROM_BLK_2340: 27268 case CDROM_BLK_2352: 27269 case CDROM_BLK_2368: 27270 case CDROM_BLK_2448: 27271 case CDROM_BLK_2646: 27272 case CDROM_BLK_2647: 27273 break; 27274 default: 27275 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27276 "sr_change_blkmode: " 27277 "Block Size '%ld' Not Supported\n", data); 27278 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27279 return (EINVAL); 27280 } 27281 27282 /* 27283 * The current block size matches the requested block size so 27284 * there is no need to send the mode select to change the size 27285 */ 27286 if (current_bsize == data) { 27287 break; 27288 } 27289 27290 /* Build the select data for the requested block size */ 27291 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27292 select_mhp = (struct mode_header *)select; 27293 select_desc = 27294 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27295 /* 27296 * The LBA size is changed via the block descriptor, so the 27297 * descriptor is built according to the user data 27298 */ 27299 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27300 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27301 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27302 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27303 27304 /* Send the mode select for the requested block size */ 27305 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27306 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27307 SD_PATH_STANDARD)) != 0) { 27308 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27309 "sr_change_blkmode: Mode Select Failed\n"); 27310 /* 27311 * The mode select failed for the requested block size, 27312 * so reset the data for the original block size and 27313 * send it to the target. The error is indicated by the 27314 * return value for the failed mode select. 27315 */ 27316 select_desc->blksize_hi = sense_desc->blksize_hi; 27317 select_desc->blksize_mid = sense_desc->blksize_mid; 27318 select_desc->blksize_lo = sense_desc->blksize_lo; 27319 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27320 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27321 SD_PATH_STANDARD); 27322 } else { 27323 ASSERT(!mutex_owned(SD_MUTEX(un))); 27324 mutex_enter(SD_MUTEX(un)); 27325 sd_update_block_info(un, (uint32_t)data, 0); 27326 27327 mutex_exit(SD_MUTEX(un)); 27328 } 27329 break; 27330 default: 27331 /* should not reach here, but check anyway */ 27332 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27333 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27334 rval = EINVAL; 27335 break; 27336 } 27337 27338 if (select) { 27339 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27340 } 27341 if (sense) { 27342 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27343 } 27344 return (rval); 27345 } 27346 27347 27348 /* 27349 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27350 * implement driver support for getting and setting the CD speed. The command 27351 * set used will be based on the device type. If the device has not been 27352 * identified as MMC the Toshiba vendor specific mode page will be used. If 27353 * the device is MMC but does not support the Real Time Streaming feature 27354 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27355 * be used to read the speed. 27356 */ 27357 27358 /* 27359 * Function: sr_change_speed() 27360 * 27361 * Description: This routine is the driver entry point for handling CD-ROM 27362 * drive speed ioctl requests for devices supporting the Toshiba 27363 * vendor specific drive speed mode page. Support for returning 27364 * and changing the current drive speed in use by the device is 27365 * implemented. 27366 * 27367 * Arguments: dev - the device 'dev_t' 27368 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27369 * CDROMSDRVSPEED (set) 27370 * data - current drive speed or requested drive speed 27371 * flag - this argument is a pass through to ddi_copyxxx() directly 27372 * from the mode argument of ioctl(). 27373 * 27374 * Return Code: the code returned by sd_send_scsi_cmd() 27375 * EINVAL if invalid arguments are provided 27376 * EFAULT if ddi_copyxxx() fails 27377 * ENXIO if fail ddi_get_soft_state 27378 * EIO if invalid mode sense block descriptor length 27379 */ 27380 27381 static int 27382 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27383 { 27384 struct sd_lun *un = NULL; 27385 struct mode_header *sense_mhp, *select_mhp; 27386 struct mode_speed *sense_page, *select_page; 27387 int current_speed; 27388 int rval = EINVAL; 27389 int bd_len; 27390 uchar_t *sense = NULL; 27391 uchar_t *select = NULL; 27392 27393 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27394 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27395 return (ENXIO); 27396 } 27397 27398 /* 27399 * Note: The drive speed is being modified here according to a Toshiba 27400 * vendor specific mode page (0x31). 27401 */ 27402 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27403 27404 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27405 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27406 SD_PATH_STANDARD)) != 0) { 27407 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27408 "sr_change_speed: Mode Sense Failed\n"); 27409 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27410 return (rval); 27411 } 27412 sense_mhp = (struct mode_header *)sense; 27413 27414 /* Check the block descriptor len to handle only 1 block descriptor */ 27415 bd_len = sense_mhp->bdesc_length; 27416 if (bd_len > MODE_BLK_DESC_LENGTH) { 27417 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27418 "sr_change_speed: Mode Sense returned invalid block " 27419 "descriptor length\n"); 27420 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27421 return (EIO); 27422 } 27423 27424 sense_page = (struct mode_speed *) 27425 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27426 current_speed = sense_page->speed; 27427 27428 /* Process command */ 27429 switch (cmd) { 27430 case CDROMGDRVSPEED: 27431 /* Return the drive speed obtained during the mode sense */ 27432 if (current_speed == 0x2) { 27433 current_speed = CDROM_TWELVE_SPEED; 27434 } 27435 if (ddi_copyout(¤t_speed, (void *)data, 27436 sizeof (int), flag) != 0) { 27437 rval = EFAULT; 27438 } 27439 break; 27440 case CDROMSDRVSPEED: 27441 /* Validate the requested drive speed */ 27442 switch ((uchar_t)data) { 27443 case CDROM_TWELVE_SPEED: 27444 data = 0x2; 27445 /*FALLTHROUGH*/ 27446 case CDROM_NORMAL_SPEED: 27447 case CDROM_DOUBLE_SPEED: 27448 case CDROM_QUAD_SPEED: 27449 case CDROM_MAXIMUM_SPEED: 27450 break; 27451 default: 27452 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27453 "sr_change_speed: " 27454 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27455 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27456 return (EINVAL); 27457 } 27458 27459 /* 27460 * The current drive speed matches the requested drive speed so 27461 * there is no need to send the mode select to change the speed 27462 */ 27463 if (current_speed == data) { 27464 break; 27465 } 27466 27467 /* Build the select data for the requested drive speed */ 27468 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27469 select_mhp = (struct mode_header *)select; 27470 select_mhp->bdesc_length = 0; 27471 select_page = 27472 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27473 select_page = 27474 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27475 select_page->mode_page.code = CDROM_MODE_SPEED; 27476 select_page->mode_page.length = 2; 27477 select_page->speed = (uchar_t)data; 27478 27479 /* Send the mode select for the requested block size */ 27480 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27481 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27482 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27483 /* 27484 * The mode select failed for the requested drive speed, 27485 * so reset the data for the original drive speed and 27486 * send it to the target. The error is indicated by the 27487 * return value for the failed mode select. 27488 */ 27489 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27490 "sr_drive_speed: Mode Select Failed\n"); 27491 select_page->speed = sense_page->speed; 27492 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27493 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27494 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27495 } 27496 break; 27497 default: 27498 /* should not reach here, but check anyway */ 27499 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27500 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27501 rval = EINVAL; 27502 break; 27503 } 27504 27505 if (select) { 27506 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27507 } 27508 if (sense) { 27509 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27510 } 27511 27512 return (rval); 27513 } 27514 27515 27516 /* 27517 * Function: sr_atapi_change_speed() 27518 * 27519 * Description: This routine is the driver entry point for handling CD-ROM 27520 * drive speed ioctl requests for MMC devices that do not support 27521 * the Real Time Streaming feature (0x107). 27522 * 27523 * Note: This routine will use the SET SPEED command which may not 27524 * be supported by all devices. 27525 * 27526 * Arguments: dev- the device 'dev_t' 27527 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27528 * CDROMSDRVSPEED (set) 27529 * data- current drive speed or requested drive speed 27530 * flag- this argument is a pass through to ddi_copyxxx() directly 27531 * from the mode argument of ioctl(). 27532 * 27533 * Return Code: the code returned by sd_send_scsi_cmd() 27534 * EINVAL if invalid arguments are provided 27535 * EFAULT if ddi_copyxxx() fails 27536 * ENXIO if fail ddi_get_soft_state 27537 * EIO if invalid mode sense block descriptor length 27538 */ 27539 27540 static int 27541 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27542 { 27543 struct sd_lun *un; 27544 struct uscsi_cmd *com = NULL; 27545 struct mode_header_grp2 *sense_mhp; 27546 uchar_t *sense_page; 27547 uchar_t *sense = NULL; 27548 char cdb[CDB_GROUP5]; 27549 int bd_len; 27550 int current_speed = 0; 27551 int max_speed = 0; 27552 int rval; 27553 27554 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27555 27556 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27557 return (ENXIO); 27558 } 27559 27560 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27561 27562 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27563 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27564 SD_PATH_STANDARD)) != 0) { 27565 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27566 "sr_atapi_change_speed: Mode Sense Failed\n"); 27567 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27568 return (rval); 27569 } 27570 27571 /* Check the block descriptor len to handle only 1 block descriptor */ 27572 sense_mhp = (struct mode_header_grp2 *)sense; 27573 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27574 if (bd_len > MODE_BLK_DESC_LENGTH) { 27575 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27576 "sr_atapi_change_speed: Mode Sense returned invalid " 27577 "block descriptor length\n"); 27578 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27579 return (EIO); 27580 } 27581 27582 /* Calculate the current and maximum drive speeds */ 27583 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27584 current_speed = (sense_page[14] << 8) | sense_page[15]; 27585 max_speed = (sense_page[8] << 8) | sense_page[9]; 27586 27587 /* Process the command */ 27588 switch (cmd) { 27589 case CDROMGDRVSPEED: 27590 current_speed /= SD_SPEED_1X; 27591 if (ddi_copyout(¤t_speed, (void *)data, 27592 sizeof (int), flag) != 0) 27593 rval = EFAULT; 27594 break; 27595 case CDROMSDRVSPEED: 27596 /* Convert the speed code to KB/sec */ 27597 switch ((uchar_t)data) { 27598 case CDROM_NORMAL_SPEED: 27599 current_speed = SD_SPEED_1X; 27600 break; 27601 case CDROM_DOUBLE_SPEED: 27602 current_speed = 2 * SD_SPEED_1X; 27603 break; 27604 case CDROM_QUAD_SPEED: 27605 current_speed = 4 * SD_SPEED_1X; 27606 break; 27607 case CDROM_TWELVE_SPEED: 27608 current_speed = 12 * SD_SPEED_1X; 27609 break; 27610 case CDROM_MAXIMUM_SPEED: 27611 current_speed = 0xffff; 27612 break; 27613 default: 27614 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27615 "sr_atapi_change_speed: invalid drive speed %d\n", 27616 (uchar_t)data); 27617 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27618 return (EINVAL); 27619 } 27620 27621 /* Check the request against the drive's max speed. */ 27622 if (current_speed != 0xffff) { 27623 if (current_speed > max_speed) { 27624 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27625 return (EINVAL); 27626 } 27627 } 27628 27629 /* 27630 * Build and send the SET SPEED command 27631 * 27632 * Note: The SET SPEED (0xBB) command used in this routine is 27633 * obsolete per the SCSI MMC spec but still supported in the 27634 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27635 * therefore the command is still implemented in this routine. 27636 */ 27637 bzero(cdb, sizeof (cdb)); 27638 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27639 cdb[2] = (uchar_t)(current_speed >> 8); 27640 cdb[3] = (uchar_t)current_speed; 27641 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27642 com->uscsi_cdb = (caddr_t)cdb; 27643 com->uscsi_cdblen = CDB_GROUP5; 27644 com->uscsi_bufaddr = NULL; 27645 com->uscsi_buflen = 0; 27646 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27647 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 27648 UIO_SYSSPACE, SD_PATH_STANDARD); 27649 break; 27650 default: 27651 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27652 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27653 rval = EINVAL; 27654 } 27655 27656 if (sense) { 27657 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27658 } 27659 if (com) { 27660 kmem_free(com, sizeof (*com)); 27661 } 27662 return (rval); 27663 } 27664 27665 27666 /* 27667 * Function: sr_pause_resume() 27668 * 27669 * Description: This routine is the driver entry point for handling CD-ROM 27670 * pause/resume ioctl requests. This only affects the audio play 27671 * operation. 27672 * 27673 * Arguments: dev - the device 'dev_t' 27674 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27675 * for setting the resume bit of the cdb. 27676 * 27677 * Return Code: the code returned by sd_send_scsi_cmd() 27678 * EINVAL if invalid mode specified 27679 * 27680 */ 27681 27682 static int 27683 sr_pause_resume(dev_t dev, int cmd) 27684 { 27685 struct sd_lun *un; 27686 struct uscsi_cmd *com; 27687 char cdb[CDB_GROUP1]; 27688 int rval; 27689 27690 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27691 return (ENXIO); 27692 } 27693 27694 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27695 bzero(cdb, CDB_GROUP1); 27696 cdb[0] = SCMD_PAUSE_RESUME; 27697 switch (cmd) { 27698 case CDROMRESUME: 27699 cdb[8] = 1; 27700 break; 27701 case CDROMPAUSE: 27702 cdb[8] = 0; 27703 break; 27704 default: 27705 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27706 " Command '%x' Not Supported\n", cmd); 27707 rval = EINVAL; 27708 goto done; 27709 } 27710 27711 com->uscsi_cdb = cdb; 27712 com->uscsi_cdblen = CDB_GROUP1; 27713 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27714 27715 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27716 UIO_SYSSPACE, SD_PATH_STANDARD); 27717 27718 done: 27719 kmem_free(com, sizeof (*com)); 27720 return (rval); 27721 } 27722 27723 27724 /* 27725 * Function: sr_play_msf() 27726 * 27727 * Description: This routine is the driver entry point for handling CD-ROM 27728 * ioctl requests to output the audio signals at the specified 27729 * starting address and continue the audio play until the specified 27730 * ending address (CDROMPLAYMSF) The address is in Minute Second 27731 * Frame (MSF) format. 27732 * 27733 * Arguments: dev - the device 'dev_t' 27734 * data - pointer to user provided audio msf structure, 27735 * specifying start/end addresses. 27736 * flag - this argument is a pass through to ddi_copyxxx() 27737 * directly from the mode argument of ioctl(). 27738 * 27739 * Return Code: the code returned by sd_send_scsi_cmd() 27740 * EFAULT if ddi_copyxxx() fails 27741 * ENXIO if fail ddi_get_soft_state 27742 * EINVAL if data pointer is NULL 27743 */ 27744 27745 static int 27746 sr_play_msf(dev_t dev, caddr_t data, int flag) 27747 { 27748 struct sd_lun *un; 27749 struct uscsi_cmd *com; 27750 struct cdrom_msf msf_struct; 27751 struct cdrom_msf *msf = &msf_struct; 27752 char cdb[CDB_GROUP1]; 27753 int rval; 27754 27755 if (data == NULL) { 27756 return (EINVAL); 27757 } 27758 27759 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27760 return (ENXIO); 27761 } 27762 27763 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27764 return (EFAULT); 27765 } 27766 27767 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27768 bzero(cdb, CDB_GROUP1); 27769 cdb[0] = SCMD_PLAYAUDIO_MSF; 27770 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27771 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27772 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27773 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27774 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27775 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27776 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27777 } else { 27778 cdb[3] = msf->cdmsf_min0; 27779 cdb[4] = msf->cdmsf_sec0; 27780 cdb[5] = msf->cdmsf_frame0; 27781 cdb[6] = msf->cdmsf_min1; 27782 cdb[7] = msf->cdmsf_sec1; 27783 cdb[8] = msf->cdmsf_frame1; 27784 } 27785 com->uscsi_cdb = cdb; 27786 com->uscsi_cdblen = CDB_GROUP1; 27787 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27788 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27789 UIO_SYSSPACE, SD_PATH_STANDARD); 27790 kmem_free(com, sizeof (*com)); 27791 return (rval); 27792 } 27793 27794 27795 /* 27796 * Function: sr_play_trkind() 27797 * 27798 * Description: This routine is the driver entry point for handling CD-ROM 27799 * ioctl requests to output the audio signals at the specified 27800 * starting address and continue the audio play until the specified 27801 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27802 * format. 27803 * 27804 * Arguments: dev - the device 'dev_t' 27805 * data - pointer to user provided audio track/index structure, 27806 * specifying start/end addresses. 27807 * flag - this argument is a pass through to ddi_copyxxx() 27808 * directly from the mode argument of ioctl(). 27809 * 27810 * Return Code: the code returned by sd_send_scsi_cmd() 27811 * EFAULT if ddi_copyxxx() fails 27812 * ENXIO if fail ddi_get_soft_state 27813 * EINVAL if data pointer is NULL 27814 */ 27815 27816 static int 27817 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27818 { 27819 struct cdrom_ti ti_struct; 27820 struct cdrom_ti *ti = &ti_struct; 27821 struct uscsi_cmd *com = NULL; 27822 char cdb[CDB_GROUP1]; 27823 int rval; 27824 27825 if (data == NULL) { 27826 return (EINVAL); 27827 } 27828 27829 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27830 return (EFAULT); 27831 } 27832 27833 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27834 bzero(cdb, CDB_GROUP1); 27835 cdb[0] = SCMD_PLAYAUDIO_TI; 27836 cdb[4] = ti->cdti_trk0; 27837 cdb[5] = ti->cdti_ind0; 27838 cdb[7] = ti->cdti_trk1; 27839 cdb[8] = ti->cdti_ind1; 27840 com->uscsi_cdb = cdb; 27841 com->uscsi_cdblen = CDB_GROUP1; 27842 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27843 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27844 UIO_SYSSPACE, SD_PATH_STANDARD); 27845 kmem_free(com, sizeof (*com)); 27846 return (rval); 27847 } 27848 27849 27850 /* 27851 * Function: sr_read_all_subcodes() 27852 * 27853 * Description: This routine is the driver entry point for handling CD-ROM 27854 * ioctl requests to return raw subcode data while the target is 27855 * playing audio (CDROMSUBCODE). 27856 * 27857 * Arguments: dev - the device 'dev_t' 27858 * data - pointer to user provided cdrom subcode structure, 27859 * specifying the transfer length and address. 27860 * flag - this argument is a pass through to ddi_copyxxx() 27861 * directly from the mode argument of ioctl(). 27862 * 27863 * Return Code: the code returned by sd_send_scsi_cmd() 27864 * EFAULT if ddi_copyxxx() fails 27865 * ENXIO if fail ddi_get_soft_state 27866 * EINVAL if data pointer is NULL 27867 */ 27868 27869 static int 27870 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27871 { 27872 struct sd_lun *un = NULL; 27873 struct uscsi_cmd *com = NULL; 27874 struct cdrom_subcode *subcode = NULL; 27875 int rval; 27876 size_t buflen; 27877 char cdb[CDB_GROUP5]; 27878 27879 #ifdef _MULTI_DATAMODEL 27880 /* To support ILP32 applications in an LP64 world */ 27881 struct cdrom_subcode32 cdrom_subcode32; 27882 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27883 #endif 27884 if (data == NULL) { 27885 return (EINVAL); 27886 } 27887 27888 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27889 return (ENXIO); 27890 } 27891 27892 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27893 27894 #ifdef _MULTI_DATAMODEL 27895 switch (ddi_model_convert_from(flag & FMODELS)) { 27896 case DDI_MODEL_ILP32: 27897 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27898 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27899 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27900 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27901 return (EFAULT); 27902 } 27903 /* Convert the ILP32 uscsi data from the application to LP64 */ 27904 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27905 break; 27906 case DDI_MODEL_NONE: 27907 if (ddi_copyin(data, subcode, 27908 sizeof (struct cdrom_subcode), flag)) { 27909 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27910 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27911 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27912 return (EFAULT); 27913 } 27914 break; 27915 } 27916 #else /* ! _MULTI_DATAMODEL */ 27917 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27918 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27919 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27920 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27921 return (EFAULT); 27922 } 27923 #endif /* _MULTI_DATAMODEL */ 27924 27925 /* 27926 * Since MMC-2 expects max 3 bytes for length, check if the 27927 * length input is greater than 3 bytes 27928 */ 27929 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27930 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27931 "sr_read_all_subcodes: " 27932 "cdrom transfer length too large: %d (limit %d)\n", 27933 subcode->cdsc_length, 0xFFFFFF); 27934 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27935 return (EINVAL); 27936 } 27937 27938 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27939 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27940 bzero(cdb, CDB_GROUP5); 27941 27942 if (un->un_f_mmc_cap == TRUE) { 27943 cdb[0] = (char)SCMD_READ_CD; 27944 cdb[2] = (char)0xff; 27945 cdb[3] = (char)0xff; 27946 cdb[4] = (char)0xff; 27947 cdb[5] = (char)0xff; 27948 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27949 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27950 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27951 cdb[10] = 1; 27952 } else { 27953 /* 27954 * Note: A vendor specific command (0xDF) is being used her to 27955 * request a read of all subcodes. 27956 */ 27957 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27958 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27959 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27960 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27961 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27962 } 27963 com->uscsi_cdb = cdb; 27964 com->uscsi_cdblen = CDB_GROUP5; 27965 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27966 com->uscsi_buflen = buflen; 27967 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27968 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27969 UIO_SYSSPACE, SD_PATH_STANDARD); 27970 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27971 kmem_free(com, sizeof (*com)); 27972 return (rval); 27973 } 27974 27975 27976 /* 27977 * Function: sr_read_subchannel() 27978 * 27979 * Description: This routine is the driver entry point for handling CD-ROM 27980 * ioctl requests to return the Q sub-channel data of the CD 27981 * current position block. (CDROMSUBCHNL) The data includes the 27982 * track number, index number, absolute CD-ROM address (LBA or MSF 27983 * format per the user) , track relative CD-ROM address (LBA or MSF 27984 * format per the user), control data and audio status. 27985 * 27986 * Arguments: dev - the device 'dev_t' 27987 * data - pointer to user provided cdrom sub-channel structure 27988 * flag - this argument is a pass through to ddi_copyxxx() 27989 * directly from the mode argument of ioctl(). 27990 * 27991 * Return Code: the code returned by sd_send_scsi_cmd() 27992 * EFAULT if ddi_copyxxx() fails 27993 * ENXIO if fail ddi_get_soft_state 27994 * EINVAL if data pointer is NULL 27995 */ 27996 27997 static int 27998 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27999 { 28000 struct sd_lun *un; 28001 struct uscsi_cmd *com; 28002 struct cdrom_subchnl subchanel; 28003 struct cdrom_subchnl *subchnl = &subchanel; 28004 char cdb[CDB_GROUP1]; 28005 caddr_t buffer; 28006 int rval; 28007 28008 if (data == NULL) { 28009 return (EINVAL); 28010 } 28011 28012 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28013 (un->un_state == SD_STATE_OFFLINE)) { 28014 return (ENXIO); 28015 } 28016 28017 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28018 return (EFAULT); 28019 } 28020 28021 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28022 bzero(cdb, CDB_GROUP1); 28023 cdb[0] = SCMD_READ_SUBCHANNEL; 28024 /* Set the MSF bit based on the user requested address format */ 28025 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28026 /* 28027 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28028 * returned 28029 */ 28030 cdb[2] = 0x40; 28031 /* 28032 * Set byte 3 to specify the return data format. A value of 0x01 28033 * indicates that the CD-ROM current position should be returned. 28034 */ 28035 cdb[3] = 0x01; 28036 cdb[8] = 0x10; 28037 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28038 com->uscsi_cdb = cdb; 28039 com->uscsi_cdblen = CDB_GROUP1; 28040 com->uscsi_bufaddr = buffer; 28041 com->uscsi_buflen = 16; 28042 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28043 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28044 UIO_SYSSPACE, SD_PATH_STANDARD); 28045 if (rval != 0) { 28046 kmem_free(buffer, 16); 28047 kmem_free(com, sizeof (*com)); 28048 return (rval); 28049 } 28050 28051 /* Process the returned Q sub-channel data */ 28052 subchnl->cdsc_audiostatus = buffer[1]; 28053 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28054 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28055 subchnl->cdsc_trk = buffer[6]; 28056 subchnl->cdsc_ind = buffer[7]; 28057 if (subchnl->cdsc_format & CDROM_LBA) { 28058 subchnl->cdsc_absaddr.lba = 28059 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28060 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28061 subchnl->cdsc_reladdr.lba = 28062 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28063 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28064 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28065 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28066 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28067 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28068 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28069 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28070 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28071 } else { 28072 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28073 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28074 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28075 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28076 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28077 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28078 } 28079 kmem_free(buffer, 16); 28080 kmem_free(com, sizeof (*com)); 28081 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28082 != 0) { 28083 return (EFAULT); 28084 } 28085 return (rval); 28086 } 28087 28088 28089 /* 28090 * Function: sr_read_tocentry() 28091 * 28092 * Description: This routine is the driver entry point for handling CD-ROM 28093 * ioctl requests to read from the Table of Contents (TOC) 28094 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28095 * fields, the starting address (LBA or MSF format per the user) 28096 * and the data mode if the user specified track is a data track. 28097 * 28098 * Note: The READ HEADER (0x44) command used in this routine is 28099 * obsolete per the SCSI MMC spec but still supported in the 28100 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28101 * therefore the command is still implemented in this routine. 28102 * 28103 * Arguments: dev - the device 'dev_t' 28104 * data - pointer to user provided toc entry structure, 28105 * specifying the track # and the address format 28106 * (LBA or MSF). 28107 * flag - this argument is a pass through to ddi_copyxxx() 28108 * directly from the mode argument of ioctl(). 28109 * 28110 * Return Code: the code returned by sd_send_scsi_cmd() 28111 * EFAULT if ddi_copyxxx() fails 28112 * ENXIO if fail ddi_get_soft_state 28113 * EINVAL if data pointer is NULL 28114 */ 28115 28116 static int 28117 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28118 { 28119 struct sd_lun *un = NULL; 28120 struct uscsi_cmd *com; 28121 struct cdrom_tocentry toc_entry; 28122 struct cdrom_tocentry *entry = &toc_entry; 28123 caddr_t buffer; 28124 int rval; 28125 char cdb[CDB_GROUP1]; 28126 28127 if (data == NULL) { 28128 return (EINVAL); 28129 } 28130 28131 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28132 (un->un_state == SD_STATE_OFFLINE)) { 28133 return (ENXIO); 28134 } 28135 28136 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28137 return (EFAULT); 28138 } 28139 28140 /* Validate the requested track and address format */ 28141 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28142 return (EINVAL); 28143 } 28144 28145 if (entry->cdte_track == 0) { 28146 return (EINVAL); 28147 } 28148 28149 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28150 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28151 bzero(cdb, CDB_GROUP1); 28152 28153 cdb[0] = SCMD_READ_TOC; 28154 /* Set the MSF bit based on the user requested address format */ 28155 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28156 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28157 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28158 } else { 28159 cdb[6] = entry->cdte_track; 28160 } 28161 28162 /* 28163 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28164 * (4 byte TOC response header + 8 byte track descriptor) 28165 */ 28166 cdb[8] = 12; 28167 com->uscsi_cdb = cdb; 28168 com->uscsi_cdblen = CDB_GROUP1; 28169 com->uscsi_bufaddr = buffer; 28170 com->uscsi_buflen = 0x0C; 28171 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28172 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28173 UIO_SYSSPACE, SD_PATH_STANDARD); 28174 if (rval != 0) { 28175 kmem_free(buffer, 12); 28176 kmem_free(com, sizeof (*com)); 28177 return (rval); 28178 } 28179 28180 /* Process the toc entry */ 28181 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28182 entry->cdte_ctrl = (buffer[5] & 0x0F); 28183 if (entry->cdte_format & CDROM_LBA) { 28184 entry->cdte_addr.lba = 28185 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28186 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28187 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28188 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28189 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28190 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28191 /* 28192 * Send a READ TOC command using the LBA address format to get 28193 * the LBA for the track requested so it can be used in the 28194 * READ HEADER request 28195 * 28196 * Note: The MSF bit of the READ HEADER command specifies the 28197 * output format. The block address specified in that command 28198 * must be in LBA format. 28199 */ 28200 cdb[1] = 0; 28201 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28202 UIO_SYSSPACE, SD_PATH_STANDARD); 28203 if (rval != 0) { 28204 kmem_free(buffer, 12); 28205 kmem_free(com, sizeof (*com)); 28206 return (rval); 28207 } 28208 } else { 28209 entry->cdte_addr.msf.minute = buffer[9]; 28210 entry->cdte_addr.msf.second = buffer[10]; 28211 entry->cdte_addr.msf.frame = buffer[11]; 28212 /* 28213 * Send a READ TOC command using the LBA address format to get 28214 * the LBA for the track requested so it can be used in the 28215 * READ HEADER request 28216 * 28217 * Note: The MSF bit of the READ HEADER command specifies the 28218 * output format. The block address specified in that command 28219 * must be in LBA format. 28220 */ 28221 cdb[1] = 0; 28222 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28223 UIO_SYSSPACE, SD_PATH_STANDARD); 28224 if (rval != 0) { 28225 kmem_free(buffer, 12); 28226 kmem_free(com, sizeof (*com)); 28227 return (rval); 28228 } 28229 } 28230 28231 /* 28232 * Build and send the READ HEADER command to determine the data mode of 28233 * the user specified track. 28234 */ 28235 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28236 (entry->cdte_track != CDROM_LEADOUT)) { 28237 bzero(cdb, CDB_GROUP1); 28238 cdb[0] = SCMD_READ_HEADER; 28239 cdb[2] = buffer[8]; 28240 cdb[3] = buffer[9]; 28241 cdb[4] = buffer[10]; 28242 cdb[5] = buffer[11]; 28243 cdb[8] = 0x08; 28244 com->uscsi_buflen = 0x08; 28245 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28246 UIO_SYSSPACE, SD_PATH_STANDARD); 28247 if (rval == 0) { 28248 entry->cdte_datamode = buffer[0]; 28249 } else { 28250 /* 28251 * READ HEADER command failed, since this is 28252 * obsoleted in one spec, its better to return 28253 * -1 for an invlid track so that we can still 28254 * recieve the rest of the TOC data. 28255 */ 28256 entry->cdte_datamode = (uchar_t)-1; 28257 } 28258 } else { 28259 entry->cdte_datamode = (uchar_t)-1; 28260 } 28261 28262 kmem_free(buffer, 12); 28263 kmem_free(com, sizeof (*com)); 28264 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28265 return (EFAULT); 28266 28267 return (rval); 28268 } 28269 28270 28271 /* 28272 * Function: sr_read_tochdr() 28273 * 28274 * Description: This routine is the driver entry point for handling CD-ROM 28275 * ioctl requests to read the Table of Contents (TOC) header 28276 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28277 * and ending track numbers 28278 * 28279 * Arguments: dev - the device 'dev_t' 28280 * data - pointer to user provided toc header structure, 28281 * specifying the starting and ending track numbers. 28282 * flag - this argument is a pass through to ddi_copyxxx() 28283 * directly from the mode argument of ioctl(). 28284 * 28285 * Return Code: the code returned by sd_send_scsi_cmd() 28286 * EFAULT if ddi_copyxxx() fails 28287 * ENXIO if fail ddi_get_soft_state 28288 * EINVAL if data pointer is NULL 28289 */ 28290 28291 static int 28292 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28293 { 28294 struct sd_lun *un; 28295 struct uscsi_cmd *com; 28296 struct cdrom_tochdr toc_header; 28297 struct cdrom_tochdr *hdr = &toc_header; 28298 char cdb[CDB_GROUP1]; 28299 int rval; 28300 caddr_t buffer; 28301 28302 if (data == NULL) { 28303 return (EINVAL); 28304 } 28305 28306 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28307 (un->un_state == SD_STATE_OFFLINE)) { 28308 return (ENXIO); 28309 } 28310 28311 buffer = kmem_zalloc(4, KM_SLEEP); 28312 bzero(cdb, CDB_GROUP1); 28313 cdb[0] = SCMD_READ_TOC; 28314 /* 28315 * Specifying a track number of 0x00 in the READ TOC command indicates 28316 * that the TOC header should be returned 28317 */ 28318 cdb[6] = 0x00; 28319 /* 28320 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28321 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28322 */ 28323 cdb[8] = 0x04; 28324 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28325 com->uscsi_cdb = cdb; 28326 com->uscsi_cdblen = CDB_GROUP1; 28327 com->uscsi_bufaddr = buffer; 28328 com->uscsi_buflen = 0x04; 28329 com->uscsi_timeout = 300; 28330 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28331 28332 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28333 UIO_SYSSPACE, SD_PATH_STANDARD); 28334 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28335 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28336 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28337 } else { 28338 hdr->cdth_trk0 = buffer[2]; 28339 hdr->cdth_trk1 = buffer[3]; 28340 } 28341 kmem_free(buffer, 4); 28342 kmem_free(com, sizeof (*com)); 28343 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28344 return (EFAULT); 28345 } 28346 return (rval); 28347 } 28348 28349 28350 /* 28351 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28352 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28353 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28354 * digital audio and extended architecture digital audio. These modes are 28355 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28356 * MMC specs. 28357 * 28358 * In addition to support for the various data formats these routines also 28359 * include support for devices that implement only the direct access READ 28360 * commands (0x08, 0x28), devices that implement the READ_CD commands 28361 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28362 * READ CDXA commands (0xD8, 0xDB) 28363 */ 28364 28365 /* 28366 * Function: sr_read_mode1() 28367 * 28368 * Description: This routine is the driver entry point for handling CD-ROM 28369 * ioctl read mode1 requests (CDROMREADMODE1). 28370 * 28371 * Arguments: dev - the device 'dev_t' 28372 * data - pointer to user provided cd read structure specifying 28373 * the lba buffer address and length. 28374 * flag - this argument is a pass through to ddi_copyxxx() 28375 * directly from the mode argument of ioctl(). 28376 * 28377 * Return Code: the code returned by sd_send_scsi_cmd() 28378 * EFAULT if ddi_copyxxx() fails 28379 * ENXIO if fail ddi_get_soft_state 28380 * EINVAL if data pointer is NULL 28381 */ 28382 28383 static int 28384 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28385 { 28386 struct sd_lun *un; 28387 struct cdrom_read mode1_struct; 28388 struct cdrom_read *mode1 = &mode1_struct; 28389 int rval; 28390 #ifdef _MULTI_DATAMODEL 28391 /* To support ILP32 applications in an LP64 world */ 28392 struct cdrom_read32 cdrom_read32; 28393 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28394 #endif /* _MULTI_DATAMODEL */ 28395 28396 if (data == NULL) { 28397 return (EINVAL); 28398 } 28399 28400 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28401 (un->un_state == SD_STATE_OFFLINE)) { 28402 return (ENXIO); 28403 } 28404 28405 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28406 "sd_read_mode1: entry: un:0x%p\n", un); 28407 28408 #ifdef _MULTI_DATAMODEL 28409 switch (ddi_model_convert_from(flag & FMODELS)) { 28410 case DDI_MODEL_ILP32: 28411 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28412 return (EFAULT); 28413 } 28414 /* Convert the ILP32 uscsi data from the application to LP64 */ 28415 cdrom_read32tocdrom_read(cdrd32, mode1); 28416 break; 28417 case DDI_MODEL_NONE: 28418 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28419 return (EFAULT); 28420 } 28421 } 28422 #else /* ! _MULTI_DATAMODEL */ 28423 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28424 return (EFAULT); 28425 } 28426 #endif /* _MULTI_DATAMODEL */ 28427 28428 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28429 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28430 28431 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28432 "sd_read_mode1: exit: un:0x%p\n", un); 28433 28434 return (rval); 28435 } 28436 28437 28438 /* 28439 * Function: sr_read_cd_mode2() 28440 * 28441 * Description: This routine is the driver entry point for handling CD-ROM 28442 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28443 * support the READ CD (0xBE) command or the 1st generation 28444 * READ CD (0xD4) command. 28445 * 28446 * Arguments: dev - the device 'dev_t' 28447 * data - pointer to user provided cd read structure specifying 28448 * the lba buffer address and length. 28449 * flag - this argument is a pass through to ddi_copyxxx() 28450 * directly from the mode argument of ioctl(). 28451 * 28452 * Return Code: the code returned by sd_send_scsi_cmd() 28453 * EFAULT if ddi_copyxxx() fails 28454 * ENXIO if fail ddi_get_soft_state 28455 * EINVAL if data pointer is NULL 28456 */ 28457 28458 static int 28459 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28460 { 28461 struct sd_lun *un; 28462 struct uscsi_cmd *com; 28463 struct cdrom_read mode2_struct; 28464 struct cdrom_read *mode2 = &mode2_struct; 28465 uchar_t cdb[CDB_GROUP5]; 28466 int nblocks; 28467 int rval; 28468 #ifdef _MULTI_DATAMODEL 28469 /* To support ILP32 applications in an LP64 world */ 28470 struct cdrom_read32 cdrom_read32; 28471 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28472 #endif /* _MULTI_DATAMODEL */ 28473 28474 if (data == NULL) { 28475 return (EINVAL); 28476 } 28477 28478 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28479 (un->un_state == SD_STATE_OFFLINE)) { 28480 return (ENXIO); 28481 } 28482 28483 #ifdef _MULTI_DATAMODEL 28484 switch (ddi_model_convert_from(flag & FMODELS)) { 28485 case DDI_MODEL_ILP32: 28486 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28487 return (EFAULT); 28488 } 28489 /* Convert the ILP32 uscsi data from the application to LP64 */ 28490 cdrom_read32tocdrom_read(cdrd32, mode2); 28491 break; 28492 case DDI_MODEL_NONE: 28493 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28494 return (EFAULT); 28495 } 28496 break; 28497 } 28498 28499 #else /* ! _MULTI_DATAMODEL */ 28500 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28501 return (EFAULT); 28502 } 28503 #endif /* _MULTI_DATAMODEL */ 28504 28505 bzero(cdb, sizeof (cdb)); 28506 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28507 /* Read command supported by 1st generation atapi drives */ 28508 cdb[0] = SCMD_READ_CDD4; 28509 } else { 28510 /* Universal CD Access Command */ 28511 cdb[0] = SCMD_READ_CD; 28512 } 28513 28514 /* 28515 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28516 */ 28517 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28518 28519 /* set the start address */ 28520 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28521 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28522 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28523 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28524 28525 /* set the transfer length */ 28526 nblocks = mode2->cdread_buflen / 2336; 28527 cdb[6] = (uchar_t)(nblocks >> 16); 28528 cdb[7] = (uchar_t)(nblocks >> 8); 28529 cdb[8] = (uchar_t)nblocks; 28530 28531 /* set the filter bits */ 28532 cdb[9] = CDROM_READ_CD_USERDATA; 28533 28534 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28535 com->uscsi_cdb = (caddr_t)cdb; 28536 com->uscsi_cdblen = sizeof (cdb); 28537 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28538 com->uscsi_buflen = mode2->cdread_buflen; 28539 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28540 28541 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28542 UIO_SYSSPACE, SD_PATH_STANDARD); 28543 kmem_free(com, sizeof (*com)); 28544 return (rval); 28545 } 28546 28547 28548 /* 28549 * Function: sr_read_mode2() 28550 * 28551 * Description: This routine is the driver entry point for handling CD-ROM 28552 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28553 * do not support the READ CD (0xBE) command. 28554 * 28555 * Arguments: dev - the device 'dev_t' 28556 * data - pointer to user provided cd read structure specifying 28557 * the lba buffer address and length. 28558 * flag - this argument is a pass through to ddi_copyxxx() 28559 * directly from the mode argument of ioctl(). 28560 * 28561 * Return Code: the code returned by sd_send_scsi_cmd() 28562 * EFAULT if ddi_copyxxx() fails 28563 * ENXIO if fail ddi_get_soft_state 28564 * EINVAL if data pointer is NULL 28565 * EIO if fail to reset block size 28566 * EAGAIN if commands are in progress in the driver 28567 */ 28568 28569 static int 28570 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28571 { 28572 struct sd_lun *un; 28573 struct cdrom_read mode2_struct; 28574 struct cdrom_read *mode2 = &mode2_struct; 28575 int rval; 28576 uint32_t restore_blksize; 28577 struct uscsi_cmd *com; 28578 uchar_t cdb[CDB_GROUP0]; 28579 int nblocks; 28580 28581 #ifdef _MULTI_DATAMODEL 28582 /* To support ILP32 applications in an LP64 world */ 28583 struct cdrom_read32 cdrom_read32; 28584 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28585 #endif /* _MULTI_DATAMODEL */ 28586 28587 if (data == NULL) { 28588 return (EINVAL); 28589 } 28590 28591 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28592 (un->un_state == SD_STATE_OFFLINE)) { 28593 return (ENXIO); 28594 } 28595 28596 /* 28597 * Because this routine will update the device and driver block size 28598 * being used we want to make sure there are no commands in progress. 28599 * If commands are in progress the user will have to try again. 28600 * 28601 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28602 * in sdioctl to protect commands from sdioctl through to the top of 28603 * sd_uscsi_strategy. See sdioctl for details. 28604 */ 28605 mutex_enter(SD_MUTEX(un)); 28606 if (un->un_ncmds_in_driver != 1) { 28607 mutex_exit(SD_MUTEX(un)); 28608 return (EAGAIN); 28609 } 28610 mutex_exit(SD_MUTEX(un)); 28611 28612 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28613 "sd_read_mode2: entry: un:0x%p\n", un); 28614 28615 #ifdef _MULTI_DATAMODEL 28616 switch (ddi_model_convert_from(flag & FMODELS)) { 28617 case DDI_MODEL_ILP32: 28618 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28619 return (EFAULT); 28620 } 28621 /* Convert the ILP32 uscsi data from the application to LP64 */ 28622 cdrom_read32tocdrom_read(cdrd32, mode2); 28623 break; 28624 case DDI_MODEL_NONE: 28625 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28626 return (EFAULT); 28627 } 28628 break; 28629 } 28630 #else /* ! _MULTI_DATAMODEL */ 28631 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28632 return (EFAULT); 28633 } 28634 #endif /* _MULTI_DATAMODEL */ 28635 28636 /* Store the current target block size for restoration later */ 28637 restore_blksize = un->un_tgt_blocksize; 28638 28639 /* Change the device and soft state target block size to 2336 */ 28640 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28641 rval = EIO; 28642 goto done; 28643 } 28644 28645 28646 bzero(cdb, sizeof (cdb)); 28647 28648 /* set READ operation */ 28649 cdb[0] = SCMD_READ; 28650 28651 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28652 mode2->cdread_lba >>= 2; 28653 28654 /* set the start address */ 28655 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28656 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28657 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28658 28659 /* set the transfer length */ 28660 nblocks = mode2->cdread_buflen / 2336; 28661 cdb[4] = (uchar_t)nblocks & 0xFF; 28662 28663 /* build command */ 28664 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28665 com->uscsi_cdb = (caddr_t)cdb; 28666 com->uscsi_cdblen = sizeof (cdb); 28667 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28668 com->uscsi_buflen = mode2->cdread_buflen; 28669 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28670 28671 /* 28672 * Issue SCSI command with user space address for read buffer. 28673 * 28674 * This sends the command through main channel in the driver. 28675 * 28676 * Since this is accessed via an IOCTL call, we go through the 28677 * standard path, so that if the device was powered down, then 28678 * it would be 'awakened' to handle the command. 28679 */ 28680 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28681 UIO_SYSSPACE, SD_PATH_STANDARD); 28682 28683 kmem_free(com, sizeof (*com)); 28684 28685 /* Restore the device and soft state target block size */ 28686 if (sr_sector_mode(dev, restore_blksize) != 0) { 28687 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28688 "can't do switch back to mode 1\n"); 28689 /* 28690 * If sd_send_scsi_READ succeeded we still need to report 28691 * an error because we failed to reset the block size 28692 */ 28693 if (rval == 0) { 28694 rval = EIO; 28695 } 28696 } 28697 28698 done: 28699 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28700 "sd_read_mode2: exit: un:0x%p\n", un); 28701 28702 return (rval); 28703 } 28704 28705 28706 /* 28707 * Function: sr_sector_mode() 28708 * 28709 * Description: This utility function is used by sr_read_mode2 to set the target 28710 * block size based on the user specified size. This is a legacy 28711 * implementation based upon a vendor specific mode page 28712 * 28713 * Arguments: dev - the device 'dev_t' 28714 * data - flag indicating if block size is being set to 2336 or 28715 * 512. 28716 * 28717 * Return Code: the code returned by sd_send_scsi_cmd() 28718 * EFAULT if ddi_copyxxx() fails 28719 * ENXIO if fail ddi_get_soft_state 28720 * EINVAL if data pointer is NULL 28721 */ 28722 28723 static int 28724 sr_sector_mode(dev_t dev, uint32_t blksize) 28725 { 28726 struct sd_lun *un; 28727 uchar_t *sense; 28728 uchar_t *select; 28729 int rval; 28730 28731 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28732 (un->un_state == SD_STATE_OFFLINE)) { 28733 return (ENXIO); 28734 } 28735 28736 sense = kmem_zalloc(20, KM_SLEEP); 28737 28738 /* Note: This is a vendor specific mode page (0x81) */ 28739 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28740 SD_PATH_STANDARD)) != 0) { 28741 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28742 "sr_sector_mode: Mode Sense failed\n"); 28743 kmem_free(sense, 20); 28744 return (rval); 28745 } 28746 select = kmem_zalloc(20, KM_SLEEP); 28747 select[3] = 0x08; 28748 select[10] = ((blksize >> 8) & 0xff); 28749 select[11] = (blksize & 0xff); 28750 select[12] = 0x01; 28751 select[13] = 0x06; 28752 select[14] = sense[14]; 28753 select[15] = sense[15]; 28754 if (blksize == SD_MODE2_BLKSIZE) { 28755 select[14] |= 0x01; 28756 } 28757 28758 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28759 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28760 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28761 "sr_sector_mode: Mode Select failed\n"); 28762 } else { 28763 /* 28764 * Only update the softstate block size if we successfully 28765 * changed the device block mode. 28766 */ 28767 mutex_enter(SD_MUTEX(un)); 28768 sd_update_block_info(un, blksize, 0); 28769 mutex_exit(SD_MUTEX(un)); 28770 } 28771 kmem_free(sense, 20); 28772 kmem_free(select, 20); 28773 return (rval); 28774 } 28775 28776 28777 /* 28778 * Function: sr_read_cdda() 28779 * 28780 * Description: This routine is the driver entry point for handling CD-ROM 28781 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28782 * the target supports CDDA these requests are handled via a vendor 28783 * specific command (0xD8) If the target does not support CDDA 28784 * these requests are handled via the READ CD command (0xBE). 28785 * 28786 * Arguments: dev - the device 'dev_t' 28787 * data - pointer to user provided CD-DA structure specifying 28788 * the track starting address, transfer length, and 28789 * subcode options. 28790 * flag - this argument is a pass through to ddi_copyxxx() 28791 * directly from the mode argument of ioctl(). 28792 * 28793 * Return Code: the code returned by sd_send_scsi_cmd() 28794 * EFAULT if ddi_copyxxx() fails 28795 * ENXIO if fail ddi_get_soft_state 28796 * EINVAL if invalid arguments are provided 28797 * ENOTTY 28798 */ 28799 28800 static int 28801 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28802 { 28803 struct sd_lun *un; 28804 struct uscsi_cmd *com; 28805 struct cdrom_cdda *cdda; 28806 int rval; 28807 size_t buflen; 28808 char cdb[CDB_GROUP5]; 28809 28810 #ifdef _MULTI_DATAMODEL 28811 /* To support ILP32 applications in an LP64 world */ 28812 struct cdrom_cdda32 cdrom_cdda32; 28813 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28814 #endif /* _MULTI_DATAMODEL */ 28815 28816 if (data == NULL) { 28817 return (EINVAL); 28818 } 28819 28820 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28821 return (ENXIO); 28822 } 28823 28824 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28825 28826 #ifdef _MULTI_DATAMODEL 28827 switch (ddi_model_convert_from(flag & FMODELS)) { 28828 case DDI_MODEL_ILP32: 28829 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28830 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28831 "sr_read_cdda: ddi_copyin Failed\n"); 28832 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28833 return (EFAULT); 28834 } 28835 /* Convert the ILP32 uscsi data from the application to LP64 */ 28836 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28837 break; 28838 case DDI_MODEL_NONE: 28839 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28840 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28841 "sr_read_cdda: ddi_copyin Failed\n"); 28842 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28843 return (EFAULT); 28844 } 28845 break; 28846 } 28847 #else /* ! _MULTI_DATAMODEL */ 28848 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28849 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28850 "sr_read_cdda: ddi_copyin Failed\n"); 28851 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28852 return (EFAULT); 28853 } 28854 #endif /* _MULTI_DATAMODEL */ 28855 28856 /* 28857 * Since MMC-2 expects max 3 bytes for length, check if the 28858 * length input is greater than 3 bytes 28859 */ 28860 if ((cdda->cdda_length & 0xFF000000) != 0) { 28861 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28862 "cdrom transfer length too large: %d (limit %d)\n", 28863 cdda->cdda_length, 0xFFFFFF); 28864 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28865 return (EINVAL); 28866 } 28867 28868 switch (cdda->cdda_subcode) { 28869 case CDROM_DA_NO_SUBCODE: 28870 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28871 break; 28872 case CDROM_DA_SUBQ: 28873 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28874 break; 28875 case CDROM_DA_ALL_SUBCODE: 28876 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28877 break; 28878 case CDROM_DA_SUBCODE_ONLY: 28879 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28880 break; 28881 default: 28882 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28883 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28884 cdda->cdda_subcode); 28885 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28886 return (EINVAL); 28887 } 28888 28889 /* Build and send the command */ 28890 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28891 bzero(cdb, CDB_GROUP5); 28892 28893 if (un->un_f_cfg_cdda == TRUE) { 28894 cdb[0] = (char)SCMD_READ_CD; 28895 cdb[1] = 0x04; 28896 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28897 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28898 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28899 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28900 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28901 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28902 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28903 cdb[9] = 0x10; 28904 switch (cdda->cdda_subcode) { 28905 case CDROM_DA_NO_SUBCODE : 28906 cdb[10] = 0x0; 28907 break; 28908 case CDROM_DA_SUBQ : 28909 cdb[10] = 0x2; 28910 break; 28911 case CDROM_DA_ALL_SUBCODE : 28912 cdb[10] = 0x1; 28913 break; 28914 case CDROM_DA_SUBCODE_ONLY : 28915 /* FALLTHROUGH */ 28916 default : 28917 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28918 kmem_free(com, sizeof (*com)); 28919 return (ENOTTY); 28920 } 28921 } else { 28922 cdb[0] = (char)SCMD_READ_CDDA; 28923 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28924 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28925 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28926 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28927 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28928 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28929 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28930 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28931 cdb[10] = cdda->cdda_subcode; 28932 } 28933 28934 com->uscsi_cdb = cdb; 28935 com->uscsi_cdblen = CDB_GROUP5; 28936 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28937 com->uscsi_buflen = buflen; 28938 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28939 28940 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28941 UIO_SYSSPACE, SD_PATH_STANDARD); 28942 28943 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28944 kmem_free(com, sizeof (*com)); 28945 return (rval); 28946 } 28947 28948 28949 /* 28950 * Function: sr_read_cdxa() 28951 * 28952 * Description: This routine is the driver entry point for handling CD-ROM 28953 * ioctl requests to return CD-XA (Extended Architecture) data. 28954 * (CDROMCDXA). 28955 * 28956 * Arguments: dev - the device 'dev_t' 28957 * data - pointer to user provided CD-XA structure specifying 28958 * the data starting address, transfer length, and format 28959 * flag - this argument is a pass through to ddi_copyxxx() 28960 * directly from the mode argument of ioctl(). 28961 * 28962 * Return Code: the code returned by sd_send_scsi_cmd() 28963 * EFAULT if ddi_copyxxx() fails 28964 * ENXIO if fail ddi_get_soft_state 28965 * EINVAL if data pointer is NULL 28966 */ 28967 28968 static int 28969 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28970 { 28971 struct sd_lun *un; 28972 struct uscsi_cmd *com; 28973 struct cdrom_cdxa *cdxa; 28974 int rval; 28975 size_t buflen; 28976 char cdb[CDB_GROUP5]; 28977 uchar_t read_flags; 28978 28979 #ifdef _MULTI_DATAMODEL 28980 /* To support ILP32 applications in an LP64 world */ 28981 struct cdrom_cdxa32 cdrom_cdxa32; 28982 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28983 #endif /* _MULTI_DATAMODEL */ 28984 28985 if (data == NULL) { 28986 return (EINVAL); 28987 } 28988 28989 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28990 return (ENXIO); 28991 } 28992 28993 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28994 28995 #ifdef _MULTI_DATAMODEL 28996 switch (ddi_model_convert_from(flag & FMODELS)) { 28997 case DDI_MODEL_ILP32: 28998 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28999 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29000 return (EFAULT); 29001 } 29002 /* 29003 * Convert the ILP32 uscsi data from the 29004 * application to LP64 for internal use. 29005 */ 29006 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29007 break; 29008 case DDI_MODEL_NONE: 29009 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29010 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29011 return (EFAULT); 29012 } 29013 break; 29014 } 29015 #else /* ! _MULTI_DATAMODEL */ 29016 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29017 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29018 return (EFAULT); 29019 } 29020 #endif /* _MULTI_DATAMODEL */ 29021 29022 /* 29023 * Since MMC-2 expects max 3 bytes for length, check if the 29024 * length input is greater than 3 bytes 29025 */ 29026 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29027 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29028 "cdrom transfer length too large: %d (limit %d)\n", 29029 cdxa->cdxa_length, 0xFFFFFF); 29030 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29031 return (EINVAL); 29032 } 29033 29034 switch (cdxa->cdxa_format) { 29035 case CDROM_XA_DATA: 29036 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29037 read_flags = 0x10; 29038 break; 29039 case CDROM_XA_SECTOR_DATA: 29040 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29041 read_flags = 0xf8; 29042 break; 29043 case CDROM_XA_DATA_W_ERROR: 29044 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29045 read_flags = 0xfc; 29046 break; 29047 default: 29048 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29049 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29050 cdxa->cdxa_format); 29051 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29052 return (EINVAL); 29053 } 29054 29055 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29056 bzero(cdb, CDB_GROUP5); 29057 if (un->un_f_mmc_cap == TRUE) { 29058 cdb[0] = (char)SCMD_READ_CD; 29059 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29060 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29061 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29062 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29063 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29064 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29065 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29066 cdb[9] = (char)read_flags; 29067 } else { 29068 /* 29069 * Note: A vendor specific command (0xDB) is being used her to 29070 * request a read of all subcodes. 29071 */ 29072 cdb[0] = (char)SCMD_READ_CDXA; 29073 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29074 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29075 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29076 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29077 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29078 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29079 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29080 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29081 cdb[10] = cdxa->cdxa_format; 29082 } 29083 com->uscsi_cdb = cdb; 29084 com->uscsi_cdblen = CDB_GROUP5; 29085 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29086 com->uscsi_buflen = buflen; 29087 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29088 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29089 UIO_SYSSPACE, SD_PATH_STANDARD); 29090 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29091 kmem_free(com, sizeof (*com)); 29092 return (rval); 29093 } 29094 29095 29096 /* 29097 * Function: sr_eject() 29098 * 29099 * Description: This routine is the driver entry point for handling CD-ROM 29100 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29101 * 29102 * Arguments: dev - the device 'dev_t' 29103 * 29104 * Return Code: the code returned by sd_send_scsi_cmd() 29105 */ 29106 29107 static int 29108 sr_eject(dev_t dev) 29109 { 29110 struct sd_lun *un; 29111 int rval; 29112 29113 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29114 (un->un_state == SD_STATE_OFFLINE)) { 29115 return (ENXIO); 29116 } 29117 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29118 SD_PATH_STANDARD)) != 0) { 29119 return (rval); 29120 } 29121 29122 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29123 SD_PATH_STANDARD); 29124 29125 if (rval == 0) { 29126 mutex_enter(SD_MUTEX(un)); 29127 sr_ejected(un); 29128 un->un_mediastate = DKIO_EJECTED; 29129 cv_broadcast(&un->un_state_cv); 29130 mutex_exit(SD_MUTEX(un)); 29131 } 29132 return (rval); 29133 } 29134 29135 29136 /* 29137 * Function: sr_ejected() 29138 * 29139 * Description: This routine updates the soft state structure to invalidate the 29140 * geometry information after the media has been ejected or a 29141 * media eject has been detected. 29142 * 29143 * Arguments: un - driver soft state (unit) structure 29144 */ 29145 29146 static void 29147 sr_ejected(struct sd_lun *un) 29148 { 29149 struct sd_errstats *stp; 29150 29151 ASSERT(un != NULL); 29152 ASSERT(mutex_owned(SD_MUTEX(un))); 29153 29154 un->un_f_blockcount_is_valid = FALSE; 29155 un->un_f_tgt_blocksize_is_valid = FALSE; 29156 un->un_f_geometry_is_valid = FALSE; 29157 29158 if (un->un_errstats != NULL) { 29159 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29160 stp->sd_capacity.value.ui64 = 0; 29161 } 29162 } 29163 29164 29165 /* 29166 * Function: sr_check_wp() 29167 * 29168 * Description: This routine checks the write protection of a removable 29169 * media disk and hotpluggable devices via the write protect bit of 29170 * the Mode Page Header device specific field. Some devices choke 29171 * on unsupported mode page. In order to workaround this issue, 29172 * this routine has been implemented to use 0x3f mode page(request 29173 * for all pages) for all device types. 29174 * 29175 * Arguments: dev - the device 'dev_t' 29176 * 29177 * Return Code: int indicating if the device is write protected (1) or not (0) 29178 * 29179 * Context: Kernel thread. 29180 * 29181 */ 29182 29183 static int 29184 sr_check_wp(dev_t dev) 29185 { 29186 struct sd_lun *un; 29187 uchar_t device_specific; 29188 uchar_t *sense; 29189 int hdrlen; 29190 int rval = FALSE; 29191 29192 /* 29193 * Note: The return codes for this routine should be reworked to 29194 * properly handle the case of a NULL softstate. 29195 */ 29196 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29197 return (FALSE); 29198 } 29199 29200 if (un->un_f_cfg_is_atapi == TRUE) { 29201 /* 29202 * The mode page contents are not required; set the allocation 29203 * length for the mode page header only 29204 */ 29205 hdrlen = MODE_HEADER_LENGTH_GRP2; 29206 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29207 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29208 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29209 goto err_exit; 29210 device_specific = 29211 ((struct mode_header_grp2 *)sense)->device_specific; 29212 } else { 29213 hdrlen = MODE_HEADER_LENGTH; 29214 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29215 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29216 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29217 goto err_exit; 29218 device_specific = 29219 ((struct mode_header *)sense)->device_specific; 29220 } 29221 29222 /* 29223 * Write protect mode sense failed; not all disks 29224 * understand this query. Return FALSE assuming that 29225 * these devices are not writable. 29226 */ 29227 if (device_specific & WRITE_PROTECT) { 29228 rval = TRUE; 29229 } 29230 29231 err_exit: 29232 kmem_free(sense, hdrlen); 29233 return (rval); 29234 } 29235 29236 /* 29237 * Function: sr_volume_ctrl() 29238 * 29239 * Description: This routine is the driver entry point for handling CD-ROM 29240 * audio output volume ioctl requests. (CDROMVOLCTRL) 29241 * 29242 * Arguments: dev - the device 'dev_t' 29243 * data - pointer to user audio volume control structure 29244 * flag - this argument is a pass through to ddi_copyxxx() 29245 * directly from the mode argument of ioctl(). 29246 * 29247 * Return Code: the code returned by sd_send_scsi_cmd() 29248 * EFAULT if ddi_copyxxx() fails 29249 * ENXIO if fail ddi_get_soft_state 29250 * EINVAL if data pointer is NULL 29251 * 29252 */ 29253 29254 static int 29255 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29256 { 29257 struct sd_lun *un; 29258 struct cdrom_volctrl volume; 29259 struct cdrom_volctrl *vol = &volume; 29260 uchar_t *sense_page; 29261 uchar_t *select_page; 29262 uchar_t *sense; 29263 uchar_t *select; 29264 int sense_buflen; 29265 int select_buflen; 29266 int rval; 29267 29268 if (data == NULL) { 29269 return (EINVAL); 29270 } 29271 29272 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29273 (un->un_state == SD_STATE_OFFLINE)) { 29274 return (ENXIO); 29275 } 29276 29277 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29278 return (EFAULT); 29279 } 29280 29281 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29282 struct mode_header_grp2 *sense_mhp; 29283 struct mode_header_grp2 *select_mhp; 29284 int bd_len; 29285 29286 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29287 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29288 MODEPAGE_AUDIO_CTRL_LEN; 29289 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29290 select = kmem_zalloc(select_buflen, KM_SLEEP); 29291 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29292 sense_buflen, MODEPAGE_AUDIO_CTRL, 29293 SD_PATH_STANDARD)) != 0) { 29294 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29295 "sr_volume_ctrl: Mode Sense Failed\n"); 29296 kmem_free(sense, sense_buflen); 29297 kmem_free(select, select_buflen); 29298 return (rval); 29299 } 29300 sense_mhp = (struct mode_header_grp2 *)sense; 29301 select_mhp = (struct mode_header_grp2 *)select; 29302 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29303 sense_mhp->bdesc_length_lo; 29304 if (bd_len > MODE_BLK_DESC_LENGTH) { 29305 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29306 "sr_volume_ctrl: Mode Sense returned invalid " 29307 "block descriptor length\n"); 29308 kmem_free(sense, sense_buflen); 29309 kmem_free(select, select_buflen); 29310 return (EIO); 29311 } 29312 sense_page = (uchar_t *) 29313 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29314 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29315 select_mhp->length_msb = 0; 29316 select_mhp->length_lsb = 0; 29317 select_mhp->bdesc_length_hi = 0; 29318 select_mhp->bdesc_length_lo = 0; 29319 } else { 29320 struct mode_header *sense_mhp, *select_mhp; 29321 29322 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29323 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29324 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29325 select = kmem_zalloc(select_buflen, KM_SLEEP); 29326 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29327 sense_buflen, MODEPAGE_AUDIO_CTRL, 29328 SD_PATH_STANDARD)) != 0) { 29329 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29330 "sr_volume_ctrl: Mode Sense Failed\n"); 29331 kmem_free(sense, sense_buflen); 29332 kmem_free(select, select_buflen); 29333 return (rval); 29334 } 29335 sense_mhp = (struct mode_header *)sense; 29336 select_mhp = (struct mode_header *)select; 29337 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29338 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29339 "sr_volume_ctrl: Mode Sense returned invalid " 29340 "block descriptor length\n"); 29341 kmem_free(sense, sense_buflen); 29342 kmem_free(select, select_buflen); 29343 return (EIO); 29344 } 29345 sense_page = (uchar_t *) 29346 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29347 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29348 select_mhp->length = 0; 29349 select_mhp->bdesc_length = 0; 29350 } 29351 /* 29352 * Note: An audio control data structure could be created and overlayed 29353 * on the following in place of the array indexing method implemented. 29354 */ 29355 29356 /* Build the select data for the user volume data */ 29357 select_page[0] = MODEPAGE_AUDIO_CTRL; 29358 select_page[1] = 0xE; 29359 /* Set the immediate bit */ 29360 select_page[2] = 0x04; 29361 /* Zero out reserved fields */ 29362 select_page[3] = 0x00; 29363 select_page[4] = 0x00; 29364 /* Return sense data for fields not to be modified */ 29365 select_page[5] = sense_page[5]; 29366 select_page[6] = sense_page[6]; 29367 select_page[7] = sense_page[7]; 29368 /* Set the user specified volume levels for channel 0 and 1 */ 29369 select_page[8] = 0x01; 29370 select_page[9] = vol->channel0; 29371 select_page[10] = 0x02; 29372 select_page[11] = vol->channel1; 29373 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29374 select_page[12] = sense_page[12]; 29375 select_page[13] = sense_page[13]; 29376 select_page[14] = sense_page[14]; 29377 select_page[15] = sense_page[15]; 29378 29379 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29380 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29381 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29382 } else { 29383 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29384 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29385 } 29386 29387 kmem_free(sense, sense_buflen); 29388 kmem_free(select, select_buflen); 29389 return (rval); 29390 } 29391 29392 29393 /* 29394 * Function: sr_read_sony_session_offset() 29395 * 29396 * Description: This routine is the driver entry point for handling CD-ROM 29397 * ioctl requests for session offset information. (CDROMREADOFFSET) 29398 * The address of the first track in the last session of a 29399 * multi-session CD-ROM is returned 29400 * 29401 * Note: This routine uses a vendor specific key value in the 29402 * command control field without implementing any vendor check here 29403 * or in the ioctl routine. 29404 * 29405 * Arguments: dev - the device 'dev_t' 29406 * data - pointer to an int to hold the requested address 29407 * flag - this argument is a pass through to ddi_copyxxx() 29408 * directly from the mode argument of ioctl(). 29409 * 29410 * Return Code: the code returned by sd_send_scsi_cmd() 29411 * EFAULT if ddi_copyxxx() fails 29412 * ENXIO if fail ddi_get_soft_state 29413 * EINVAL if data pointer is NULL 29414 */ 29415 29416 static int 29417 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29418 { 29419 struct sd_lun *un; 29420 struct uscsi_cmd *com; 29421 caddr_t buffer; 29422 char cdb[CDB_GROUP1]; 29423 int session_offset = 0; 29424 int rval; 29425 29426 if (data == NULL) { 29427 return (EINVAL); 29428 } 29429 29430 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29431 (un->un_state == SD_STATE_OFFLINE)) { 29432 return (ENXIO); 29433 } 29434 29435 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29436 bzero(cdb, CDB_GROUP1); 29437 cdb[0] = SCMD_READ_TOC; 29438 /* 29439 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29440 * (4 byte TOC response header + 8 byte response data) 29441 */ 29442 cdb[8] = SONY_SESSION_OFFSET_LEN; 29443 /* Byte 9 is the control byte. A vendor specific value is used */ 29444 cdb[9] = SONY_SESSION_OFFSET_KEY; 29445 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29446 com->uscsi_cdb = cdb; 29447 com->uscsi_cdblen = CDB_GROUP1; 29448 com->uscsi_bufaddr = buffer; 29449 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29450 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29451 29452 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29453 UIO_SYSSPACE, SD_PATH_STANDARD); 29454 if (rval != 0) { 29455 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29456 kmem_free(com, sizeof (*com)); 29457 return (rval); 29458 } 29459 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29460 session_offset = 29461 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29462 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29463 /* 29464 * Offset returned offset in current lbasize block's. Convert to 29465 * 2k block's to return to the user 29466 */ 29467 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29468 session_offset >>= 2; 29469 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29470 session_offset >>= 1; 29471 } 29472 } 29473 29474 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29475 rval = EFAULT; 29476 } 29477 29478 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29479 kmem_free(com, sizeof (*com)); 29480 return (rval); 29481 } 29482 29483 29484 /* 29485 * Function: sd_wm_cache_constructor() 29486 * 29487 * Description: Cache Constructor for the wmap cache for the read/modify/write 29488 * devices. 29489 * 29490 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29491 * un - sd_lun structure for the device. 29492 * flag - the km flags passed to constructor 29493 * 29494 * Return Code: 0 on success. 29495 * -1 on failure. 29496 */ 29497 29498 /*ARGSUSED*/ 29499 static int 29500 sd_wm_cache_constructor(void *wm, void *un, int flags) 29501 { 29502 bzero(wm, sizeof (struct sd_w_map)); 29503 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29504 return (0); 29505 } 29506 29507 29508 /* 29509 * Function: sd_wm_cache_destructor() 29510 * 29511 * Description: Cache destructor for the wmap cache for the read/modify/write 29512 * devices. 29513 * 29514 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29515 * un - sd_lun structure for the device. 29516 */ 29517 /*ARGSUSED*/ 29518 static void 29519 sd_wm_cache_destructor(void *wm, void *un) 29520 { 29521 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29522 } 29523 29524 29525 /* 29526 * Function: sd_range_lock() 29527 * 29528 * Description: Lock the range of blocks specified as parameter to ensure 29529 * that read, modify write is atomic and no other i/o writes 29530 * to the same location. The range is specified in terms 29531 * of start and end blocks. Block numbers are the actual 29532 * media block numbers and not system. 29533 * 29534 * Arguments: un - sd_lun structure for the device. 29535 * startb - The starting block number 29536 * endb - The end block number 29537 * typ - type of i/o - simple/read_modify_write 29538 * 29539 * Return Code: wm - pointer to the wmap structure. 29540 * 29541 * Context: This routine can sleep. 29542 */ 29543 29544 static struct sd_w_map * 29545 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29546 { 29547 struct sd_w_map *wmp = NULL; 29548 struct sd_w_map *sl_wmp = NULL; 29549 struct sd_w_map *tmp_wmp; 29550 wm_state state = SD_WM_CHK_LIST; 29551 29552 29553 ASSERT(un != NULL); 29554 ASSERT(!mutex_owned(SD_MUTEX(un))); 29555 29556 mutex_enter(SD_MUTEX(un)); 29557 29558 while (state != SD_WM_DONE) { 29559 29560 switch (state) { 29561 case SD_WM_CHK_LIST: 29562 /* 29563 * This is the starting state. Check the wmap list 29564 * to see if the range is currently available. 29565 */ 29566 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29567 /* 29568 * If this is a simple write and no rmw 29569 * i/o is pending then try to lock the 29570 * range as the range should be available. 29571 */ 29572 state = SD_WM_LOCK_RANGE; 29573 } else { 29574 tmp_wmp = sd_get_range(un, startb, endb); 29575 if (tmp_wmp != NULL) { 29576 if ((wmp != NULL) && ONLIST(un, wmp)) { 29577 /* 29578 * Should not keep onlist wmps 29579 * while waiting this macro 29580 * will also do wmp = NULL; 29581 */ 29582 FREE_ONLIST_WMAP(un, wmp); 29583 } 29584 /* 29585 * sl_wmp is the wmap on which wait 29586 * is done, since the tmp_wmp points 29587 * to the inuse wmap, set sl_wmp to 29588 * tmp_wmp and change the state to sleep 29589 */ 29590 sl_wmp = tmp_wmp; 29591 state = SD_WM_WAIT_MAP; 29592 } else { 29593 state = SD_WM_LOCK_RANGE; 29594 } 29595 29596 } 29597 break; 29598 29599 case SD_WM_LOCK_RANGE: 29600 ASSERT(un->un_wm_cache); 29601 /* 29602 * The range need to be locked, try to get a wmap. 29603 * First attempt it with NO_SLEEP, want to avoid a sleep 29604 * if possible as we will have to release the sd mutex 29605 * if we have to sleep. 29606 */ 29607 if (wmp == NULL) 29608 wmp = kmem_cache_alloc(un->un_wm_cache, 29609 KM_NOSLEEP); 29610 if (wmp == NULL) { 29611 mutex_exit(SD_MUTEX(un)); 29612 _NOTE(DATA_READABLE_WITHOUT_LOCK 29613 (sd_lun::un_wm_cache)) 29614 wmp = kmem_cache_alloc(un->un_wm_cache, 29615 KM_SLEEP); 29616 mutex_enter(SD_MUTEX(un)); 29617 /* 29618 * we released the mutex so recheck and go to 29619 * check list state. 29620 */ 29621 state = SD_WM_CHK_LIST; 29622 } else { 29623 /* 29624 * We exit out of state machine since we 29625 * have the wmap. Do the housekeeping first. 29626 * place the wmap on the wmap list if it is not 29627 * on it already and then set the state to done. 29628 */ 29629 wmp->wm_start = startb; 29630 wmp->wm_end = endb; 29631 wmp->wm_flags = typ | SD_WM_BUSY; 29632 if (typ & SD_WTYPE_RMW) { 29633 un->un_rmw_count++; 29634 } 29635 /* 29636 * If not already on the list then link 29637 */ 29638 if (!ONLIST(un, wmp)) { 29639 wmp->wm_next = un->un_wm; 29640 wmp->wm_prev = NULL; 29641 if (wmp->wm_next) 29642 wmp->wm_next->wm_prev = wmp; 29643 un->un_wm = wmp; 29644 } 29645 state = SD_WM_DONE; 29646 } 29647 break; 29648 29649 case SD_WM_WAIT_MAP: 29650 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29651 /* 29652 * Wait is done on sl_wmp, which is set in the 29653 * check_list state. 29654 */ 29655 sl_wmp->wm_wanted_count++; 29656 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29657 sl_wmp->wm_wanted_count--; 29658 /* 29659 * We can reuse the memory from the completed sl_wmp 29660 * lock range for our new lock, but only if noone is 29661 * waiting for it. 29662 */ 29663 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29664 if (sl_wmp->wm_wanted_count == 0) { 29665 if (wmp != NULL) 29666 CHK_N_FREEWMP(un, wmp); 29667 wmp = sl_wmp; 29668 } 29669 sl_wmp = NULL; 29670 /* 29671 * After waking up, need to recheck for availability of 29672 * range. 29673 */ 29674 state = SD_WM_CHK_LIST; 29675 break; 29676 29677 default: 29678 panic("sd_range_lock: " 29679 "Unknown state %d in sd_range_lock", state); 29680 /*NOTREACHED*/ 29681 } /* switch(state) */ 29682 29683 } /* while(state != SD_WM_DONE) */ 29684 29685 mutex_exit(SD_MUTEX(un)); 29686 29687 ASSERT(wmp != NULL); 29688 29689 return (wmp); 29690 } 29691 29692 29693 /* 29694 * Function: sd_get_range() 29695 * 29696 * Description: Find if there any overlapping I/O to this one 29697 * Returns the write-map of 1st such I/O, NULL otherwise. 29698 * 29699 * Arguments: un - sd_lun structure for the device. 29700 * startb - The starting block number 29701 * endb - The end block number 29702 * 29703 * Return Code: wm - pointer to the wmap structure. 29704 */ 29705 29706 static struct sd_w_map * 29707 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29708 { 29709 struct sd_w_map *wmp; 29710 29711 ASSERT(un != NULL); 29712 29713 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29714 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29715 continue; 29716 } 29717 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29718 break; 29719 } 29720 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29721 break; 29722 } 29723 } 29724 29725 return (wmp); 29726 } 29727 29728 29729 /* 29730 * Function: sd_free_inlist_wmap() 29731 * 29732 * Description: Unlink and free a write map struct. 29733 * 29734 * Arguments: un - sd_lun structure for the device. 29735 * wmp - sd_w_map which needs to be unlinked. 29736 */ 29737 29738 static void 29739 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29740 { 29741 ASSERT(un != NULL); 29742 29743 if (un->un_wm == wmp) { 29744 un->un_wm = wmp->wm_next; 29745 } else { 29746 wmp->wm_prev->wm_next = wmp->wm_next; 29747 } 29748 29749 if (wmp->wm_next) { 29750 wmp->wm_next->wm_prev = wmp->wm_prev; 29751 } 29752 29753 wmp->wm_next = wmp->wm_prev = NULL; 29754 29755 kmem_cache_free(un->un_wm_cache, wmp); 29756 } 29757 29758 29759 /* 29760 * Function: sd_range_unlock() 29761 * 29762 * Description: Unlock the range locked by wm. 29763 * Free write map if nobody else is waiting on it. 29764 * 29765 * Arguments: un - sd_lun structure for the device. 29766 * wmp - sd_w_map which needs to be unlinked. 29767 */ 29768 29769 static void 29770 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29771 { 29772 ASSERT(un != NULL); 29773 ASSERT(wm != NULL); 29774 ASSERT(!mutex_owned(SD_MUTEX(un))); 29775 29776 mutex_enter(SD_MUTEX(un)); 29777 29778 if (wm->wm_flags & SD_WTYPE_RMW) { 29779 un->un_rmw_count--; 29780 } 29781 29782 if (wm->wm_wanted_count) { 29783 wm->wm_flags = 0; 29784 /* 29785 * Broadcast that the wmap is available now. 29786 */ 29787 cv_broadcast(&wm->wm_avail); 29788 } else { 29789 /* 29790 * If no one is waiting on the map, it should be free'ed. 29791 */ 29792 sd_free_inlist_wmap(un, wm); 29793 } 29794 29795 mutex_exit(SD_MUTEX(un)); 29796 } 29797 29798 29799 /* 29800 * Function: sd_read_modify_write_task 29801 * 29802 * Description: Called from a taskq thread to initiate the write phase of 29803 * a read-modify-write request. This is used for targets where 29804 * un->un_sys_blocksize != un->un_tgt_blocksize. 29805 * 29806 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29807 * 29808 * Context: Called under taskq thread context. 29809 */ 29810 29811 static void 29812 sd_read_modify_write_task(void *arg) 29813 { 29814 struct sd_mapblocksize_info *bsp; 29815 struct buf *bp; 29816 struct sd_xbuf *xp; 29817 struct sd_lun *un; 29818 29819 bp = arg; /* The bp is given in arg */ 29820 ASSERT(bp != NULL); 29821 29822 /* Get the pointer to the layer-private data struct */ 29823 xp = SD_GET_XBUF(bp); 29824 ASSERT(xp != NULL); 29825 bsp = xp->xb_private; 29826 ASSERT(bsp != NULL); 29827 29828 un = SD_GET_UN(bp); 29829 ASSERT(un != NULL); 29830 ASSERT(!mutex_owned(SD_MUTEX(un))); 29831 29832 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29833 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29834 29835 /* 29836 * This is the write phase of a read-modify-write request, called 29837 * under the context of a taskq thread in response to the completion 29838 * of the read portion of the rmw request completing under interrupt 29839 * context. The write request must be sent from here down the iostart 29840 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29841 * we use the layer index saved in the layer-private data area. 29842 */ 29843 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29844 29845 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29846 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29847 } 29848 29849 29850 /* 29851 * Function: sddump_do_read_of_rmw() 29852 * 29853 * Description: This routine will be called from sddump, If sddump is called 29854 * with an I/O which not aligned on device blocksize boundary 29855 * then the write has to be converted to read-modify-write. 29856 * Do the read part here in order to keep sddump simple. 29857 * Note - That the sd_mutex is held across the call to this 29858 * routine. 29859 * 29860 * Arguments: un - sd_lun 29861 * blkno - block number in terms of media block size. 29862 * nblk - number of blocks. 29863 * bpp - pointer to pointer to the buf structure. On return 29864 * from this function, *bpp points to the valid buffer 29865 * to which the write has to be done. 29866 * 29867 * Return Code: 0 for success or errno-type return code 29868 */ 29869 29870 static int 29871 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29872 struct buf **bpp) 29873 { 29874 int err; 29875 int i; 29876 int rval; 29877 struct buf *bp; 29878 struct scsi_pkt *pkt = NULL; 29879 uint32_t target_blocksize; 29880 29881 ASSERT(un != NULL); 29882 ASSERT(mutex_owned(SD_MUTEX(un))); 29883 29884 target_blocksize = un->un_tgt_blocksize; 29885 29886 mutex_exit(SD_MUTEX(un)); 29887 29888 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29889 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29890 if (bp == NULL) { 29891 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29892 "no resources for dumping; giving up"); 29893 err = ENOMEM; 29894 goto done; 29895 } 29896 29897 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29898 blkno, nblk); 29899 if (rval != 0) { 29900 scsi_free_consistent_buf(bp); 29901 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29902 "no resources for dumping; giving up"); 29903 err = ENOMEM; 29904 goto done; 29905 } 29906 29907 pkt->pkt_flags |= FLAG_NOINTR; 29908 29909 err = EIO; 29910 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29911 29912 /* 29913 * Scsi_poll returns 0 (success) if the command completes and 29914 * the status block is STATUS_GOOD. We should only check 29915 * errors if this condition is not true. Even then we should 29916 * send our own request sense packet only if we have a check 29917 * condition and auto request sense has not been performed by 29918 * the hba. 29919 */ 29920 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29921 29922 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29923 err = 0; 29924 break; 29925 } 29926 29927 /* 29928 * Check CMD_DEV_GONE 1st, give up if device is gone, 29929 * no need to read RQS data. 29930 */ 29931 if (pkt->pkt_reason == CMD_DEV_GONE) { 29932 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29933 "Device is gone\n"); 29934 break; 29935 } 29936 29937 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29938 SD_INFO(SD_LOG_DUMP, un, 29939 "sddump: read failed with CHECK, try # %d\n", i); 29940 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29941 (void) sd_send_polled_RQS(un); 29942 } 29943 29944 continue; 29945 } 29946 29947 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29948 int reset_retval = 0; 29949 29950 SD_INFO(SD_LOG_DUMP, un, 29951 "sddump: read failed with BUSY, try # %d\n", i); 29952 29953 if (un->un_f_lun_reset_enabled == TRUE) { 29954 reset_retval = scsi_reset(SD_ADDRESS(un), 29955 RESET_LUN); 29956 } 29957 if (reset_retval == 0) { 29958 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29959 } 29960 (void) sd_send_polled_RQS(un); 29961 29962 } else { 29963 SD_INFO(SD_LOG_DUMP, un, 29964 "sddump: read failed with 0x%x, try # %d\n", 29965 SD_GET_PKT_STATUS(pkt), i); 29966 mutex_enter(SD_MUTEX(un)); 29967 sd_reset_target(un, pkt); 29968 mutex_exit(SD_MUTEX(un)); 29969 } 29970 29971 /* 29972 * If we are not getting anywhere with lun/target resets, 29973 * let's reset the bus. 29974 */ 29975 if (i > SD_NDUMP_RETRIES/2) { 29976 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29977 (void) sd_send_polled_RQS(un); 29978 } 29979 29980 } 29981 scsi_destroy_pkt(pkt); 29982 29983 if (err != 0) { 29984 scsi_free_consistent_buf(bp); 29985 *bpp = NULL; 29986 } else { 29987 *bpp = bp; 29988 } 29989 29990 done: 29991 mutex_enter(SD_MUTEX(un)); 29992 return (err); 29993 } 29994 29995 29996 /* 29997 * Function: sd_failfast_flushq 29998 * 29999 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30000 * in b_flags and move them onto the failfast queue, then kick 30001 * off a thread to return all bp's on the failfast queue to 30002 * their owners with an error set. 30003 * 30004 * Arguments: un - pointer to the soft state struct for the instance. 30005 * 30006 * Context: may execute in interrupt context. 30007 */ 30008 30009 static void 30010 sd_failfast_flushq(struct sd_lun *un) 30011 { 30012 struct buf *bp; 30013 struct buf *next_waitq_bp; 30014 struct buf *prev_waitq_bp = NULL; 30015 30016 ASSERT(un != NULL); 30017 ASSERT(mutex_owned(SD_MUTEX(un))); 30018 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30019 ASSERT(un->un_failfast_bp == NULL); 30020 30021 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30022 "sd_failfast_flushq: entry: un:0x%p\n", un); 30023 30024 /* 30025 * Check if we should flush all bufs when entering failfast state, or 30026 * just those with B_FAILFAST set. 30027 */ 30028 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30029 /* 30030 * Move *all* bp's on the wait queue to the failfast flush 30031 * queue, including those that do NOT have B_FAILFAST set. 30032 */ 30033 if (un->un_failfast_headp == NULL) { 30034 ASSERT(un->un_failfast_tailp == NULL); 30035 un->un_failfast_headp = un->un_waitq_headp; 30036 } else { 30037 ASSERT(un->un_failfast_tailp != NULL); 30038 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30039 } 30040 30041 un->un_failfast_tailp = un->un_waitq_tailp; 30042 30043 /* update kstat for each bp moved out of the waitq */ 30044 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30045 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30046 } 30047 30048 /* empty the waitq */ 30049 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30050 30051 } else { 30052 /* 30053 * Go thru the wait queue, pick off all entries with 30054 * B_FAILFAST set, and move these onto the failfast queue. 30055 */ 30056 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30057 /* 30058 * Save the pointer to the next bp on the wait queue, 30059 * so we get to it on the next iteration of this loop. 30060 */ 30061 next_waitq_bp = bp->av_forw; 30062 30063 /* 30064 * If this bp from the wait queue does NOT have 30065 * B_FAILFAST set, just move on to the next element 30066 * in the wait queue. Note, this is the only place 30067 * where it is correct to set prev_waitq_bp. 30068 */ 30069 if ((bp->b_flags & B_FAILFAST) == 0) { 30070 prev_waitq_bp = bp; 30071 continue; 30072 } 30073 30074 /* 30075 * Remove the bp from the wait queue. 30076 */ 30077 if (bp == un->un_waitq_headp) { 30078 /* The bp is the first element of the waitq. */ 30079 un->un_waitq_headp = next_waitq_bp; 30080 if (un->un_waitq_headp == NULL) { 30081 /* The wait queue is now empty */ 30082 un->un_waitq_tailp = NULL; 30083 } 30084 } else { 30085 /* 30086 * The bp is either somewhere in the middle 30087 * or at the end of the wait queue. 30088 */ 30089 ASSERT(un->un_waitq_headp != NULL); 30090 ASSERT(prev_waitq_bp != NULL); 30091 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30092 == 0); 30093 if (bp == un->un_waitq_tailp) { 30094 /* bp is the last entry on the waitq. */ 30095 ASSERT(next_waitq_bp == NULL); 30096 un->un_waitq_tailp = prev_waitq_bp; 30097 } 30098 prev_waitq_bp->av_forw = next_waitq_bp; 30099 } 30100 bp->av_forw = NULL; 30101 30102 /* 30103 * update kstat since the bp is moved out of 30104 * the waitq 30105 */ 30106 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30107 30108 /* 30109 * Now put the bp onto the failfast queue. 30110 */ 30111 if (un->un_failfast_headp == NULL) { 30112 /* failfast queue is currently empty */ 30113 ASSERT(un->un_failfast_tailp == NULL); 30114 un->un_failfast_headp = 30115 un->un_failfast_tailp = bp; 30116 } else { 30117 /* Add the bp to the end of the failfast q */ 30118 ASSERT(un->un_failfast_tailp != NULL); 30119 ASSERT(un->un_failfast_tailp->b_flags & 30120 B_FAILFAST); 30121 un->un_failfast_tailp->av_forw = bp; 30122 un->un_failfast_tailp = bp; 30123 } 30124 } 30125 } 30126 30127 /* 30128 * Now return all bp's on the failfast queue to their owners. 30129 */ 30130 while ((bp = un->un_failfast_headp) != NULL) { 30131 30132 un->un_failfast_headp = bp->av_forw; 30133 if (un->un_failfast_headp == NULL) { 30134 un->un_failfast_tailp = NULL; 30135 } 30136 30137 /* 30138 * We want to return the bp with a failure error code, but 30139 * we do not want a call to sd_start_cmds() to occur here, 30140 * so use sd_return_failed_command_no_restart() instead of 30141 * sd_return_failed_command(). 30142 */ 30143 sd_return_failed_command_no_restart(un, bp, EIO); 30144 } 30145 30146 /* Flush the xbuf queues if required. */ 30147 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30148 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30149 } 30150 30151 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30152 "sd_failfast_flushq: exit: un:0x%p\n", un); 30153 } 30154 30155 30156 /* 30157 * Function: sd_failfast_flushq_callback 30158 * 30159 * Description: Return TRUE if the given bp meets the criteria for failfast 30160 * flushing. Used with ddi_xbuf_flushq(9F). 30161 * 30162 * Arguments: bp - ptr to buf struct to be examined. 30163 * 30164 * Context: Any 30165 */ 30166 30167 static int 30168 sd_failfast_flushq_callback(struct buf *bp) 30169 { 30170 /* 30171 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30172 * state is entered; OR (2) the given bp has B_FAILFAST set. 30173 */ 30174 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30175 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30176 } 30177 30178 30179 30180 #if defined(__i386) || defined(__amd64) 30181 /* 30182 * Function: sd_setup_next_xfer 30183 * 30184 * Description: Prepare next I/O operation using DMA_PARTIAL 30185 * 30186 */ 30187 30188 static int 30189 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30190 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30191 { 30192 ssize_t num_blks_not_xfered; 30193 daddr_t strt_blk_num; 30194 ssize_t bytes_not_xfered; 30195 int rval; 30196 30197 ASSERT(pkt->pkt_resid == 0); 30198 30199 /* 30200 * Calculate next block number and amount to be transferred. 30201 * 30202 * How much data NOT transfered to the HBA yet. 30203 */ 30204 bytes_not_xfered = xp->xb_dma_resid; 30205 30206 /* 30207 * figure how many blocks NOT transfered to the HBA yet. 30208 */ 30209 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30210 30211 /* 30212 * set starting block number to the end of what WAS transfered. 30213 */ 30214 strt_blk_num = xp->xb_blkno + 30215 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30216 30217 /* 30218 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30219 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30220 * the disk mutex here. 30221 */ 30222 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30223 strt_blk_num, num_blks_not_xfered); 30224 30225 if (rval == 0) { 30226 30227 /* 30228 * Success. 30229 * 30230 * Adjust things if there are still more blocks to be 30231 * transfered. 30232 */ 30233 xp->xb_dma_resid = pkt->pkt_resid; 30234 pkt->pkt_resid = 0; 30235 30236 return (1); 30237 } 30238 30239 /* 30240 * There's really only one possible return value from 30241 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30242 * returns NULL. 30243 */ 30244 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30245 30246 bp->b_resid = bp->b_bcount; 30247 bp->b_flags |= B_ERROR; 30248 30249 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30250 "Error setting up next portion of DMA transfer\n"); 30251 30252 return (0); 30253 } 30254 #endif 30255 30256 /* 30257 * Function: sd_panic_for_res_conflict 30258 * 30259 * Description: Call panic with a string formated with "Reservation Conflict" 30260 * and a human readable identifier indicating the SD instance 30261 * that experienced the reservation conflict. 30262 * 30263 * Arguments: un - pointer to the soft state struct for the instance. 30264 * 30265 * Context: may execute in interrupt context. 30266 */ 30267 30268 #define SD_RESV_CONFLICT_FMT_LEN 40 30269 void 30270 sd_panic_for_res_conflict(struct sd_lun *un) 30271 { 30272 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30273 char path_str[MAXPATHLEN]; 30274 30275 (void) snprintf(panic_str, sizeof (panic_str), 30276 "Reservation Conflict\nDisk: %s", 30277 ddi_pathname(SD_DEVINFO(un), path_str)); 30278 30279 panic(panic_str); 30280 } 30281 30282 /* 30283 * Note: The following sd_faultinjection_ioctl( ) routines implement 30284 * driver support for handling fault injection for error analysis 30285 * causing faults in multiple layers of the driver. 30286 * 30287 */ 30288 30289 #ifdef SD_FAULT_INJECTION 30290 static uint_t sd_fault_injection_on = 0; 30291 30292 /* 30293 * Function: sd_faultinjection_ioctl() 30294 * 30295 * Description: This routine is the driver entry point for handling 30296 * faultinjection ioctls to inject errors into the 30297 * layer model 30298 * 30299 * Arguments: cmd - the ioctl cmd recieved 30300 * arg - the arguments from user and returns 30301 */ 30302 30303 static void 30304 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30305 30306 uint_t i; 30307 uint_t rval; 30308 30309 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30310 30311 mutex_enter(SD_MUTEX(un)); 30312 30313 switch (cmd) { 30314 case SDIOCRUN: 30315 /* Allow pushed faults to be injected */ 30316 SD_INFO(SD_LOG_SDTEST, un, 30317 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30318 30319 sd_fault_injection_on = 1; 30320 30321 SD_INFO(SD_LOG_IOERR, un, 30322 "sd_faultinjection_ioctl: run finished\n"); 30323 break; 30324 30325 case SDIOCSTART: 30326 /* Start Injection Session */ 30327 SD_INFO(SD_LOG_SDTEST, un, 30328 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30329 30330 sd_fault_injection_on = 0; 30331 un->sd_injection_mask = 0xFFFFFFFF; 30332 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30333 un->sd_fi_fifo_pkt[i] = NULL; 30334 un->sd_fi_fifo_xb[i] = NULL; 30335 un->sd_fi_fifo_un[i] = NULL; 30336 un->sd_fi_fifo_arq[i] = NULL; 30337 } 30338 un->sd_fi_fifo_start = 0; 30339 un->sd_fi_fifo_end = 0; 30340 30341 mutex_enter(&(un->un_fi_mutex)); 30342 un->sd_fi_log[0] = '\0'; 30343 un->sd_fi_buf_len = 0; 30344 mutex_exit(&(un->un_fi_mutex)); 30345 30346 SD_INFO(SD_LOG_IOERR, un, 30347 "sd_faultinjection_ioctl: start finished\n"); 30348 break; 30349 30350 case SDIOCSTOP: 30351 /* Stop Injection Session */ 30352 SD_INFO(SD_LOG_SDTEST, un, 30353 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30354 sd_fault_injection_on = 0; 30355 un->sd_injection_mask = 0x0; 30356 30357 /* Empty stray or unuseds structs from fifo */ 30358 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30359 if (un->sd_fi_fifo_pkt[i] != NULL) { 30360 kmem_free(un->sd_fi_fifo_pkt[i], 30361 sizeof (struct sd_fi_pkt)); 30362 } 30363 if (un->sd_fi_fifo_xb[i] != NULL) { 30364 kmem_free(un->sd_fi_fifo_xb[i], 30365 sizeof (struct sd_fi_xb)); 30366 } 30367 if (un->sd_fi_fifo_un[i] != NULL) { 30368 kmem_free(un->sd_fi_fifo_un[i], 30369 sizeof (struct sd_fi_un)); 30370 } 30371 if (un->sd_fi_fifo_arq[i] != NULL) { 30372 kmem_free(un->sd_fi_fifo_arq[i], 30373 sizeof (struct sd_fi_arq)); 30374 } 30375 un->sd_fi_fifo_pkt[i] = NULL; 30376 un->sd_fi_fifo_un[i] = NULL; 30377 un->sd_fi_fifo_xb[i] = NULL; 30378 un->sd_fi_fifo_arq[i] = NULL; 30379 } 30380 un->sd_fi_fifo_start = 0; 30381 un->sd_fi_fifo_end = 0; 30382 30383 SD_INFO(SD_LOG_IOERR, un, 30384 "sd_faultinjection_ioctl: stop finished\n"); 30385 break; 30386 30387 case SDIOCINSERTPKT: 30388 /* Store a packet struct to be pushed onto fifo */ 30389 SD_INFO(SD_LOG_SDTEST, un, 30390 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30391 30392 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30393 30394 sd_fault_injection_on = 0; 30395 30396 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30397 if (un->sd_fi_fifo_pkt[i] != NULL) { 30398 kmem_free(un->sd_fi_fifo_pkt[i], 30399 sizeof (struct sd_fi_pkt)); 30400 } 30401 if (arg != NULL) { 30402 un->sd_fi_fifo_pkt[i] = 30403 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30404 if (un->sd_fi_fifo_pkt[i] == NULL) { 30405 /* Alloc failed don't store anything */ 30406 break; 30407 } 30408 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30409 sizeof (struct sd_fi_pkt), 0); 30410 if (rval == -1) { 30411 kmem_free(un->sd_fi_fifo_pkt[i], 30412 sizeof (struct sd_fi_pkt)); 30413 un->sd_fi_fifo_pkt[i] = NULL; 30414 } 30415 } else { 30416 SD_INFO(SD_LOG_IOERR, un, 30417 "sd_faultinjection_ioctl: pkt null\n"); 30418 } 30419 break; 30420 30421 case SDIOCINSERTXB: 30422 /* Store a xb struct to be pushed onto fifo */ 30423 SD_INFO(SD_LOG_SDTEST, un, 30424 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30425 30426 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30427 30428 sd_fault_injection_on = 0; 30429 30430 if (un->sd_fi_fifo_xb[i] != NULL) { 30431 kmem_free(un->sd_fi_fifo_xb[i], 30432 sizeof (struct sd_fi_xb)); 30433 un->sd_fi_fifo_xb[i] = NULL; 30434 } 30435 if (arg != NULL) { 30436 un->sd_fi_fifo_xb[i] = 30437 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30438 if (un->sd_fi_fifo_xb[i] == NULL) { 30439 /* Alloc failed don't store anything */ 30440 break; 30441 } 30442 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30443 sizeof (struct sd_fi_xb), 0); 30444 30445 if (rval == -1) { 30446 kmem_free(un->sd_fi_fifo_xb[i], 30447 sizeof (struct sd_fi_xb)); 30448 un->sd_fi_fifo_xb[i] = NULL; 30449 } 30450 } else { 30451 SD_INFO(SD_LOG_IOERR, un, 30452 "sd_faultinjection_ioctl: xb null\n"); 30453 } 30454 break; 30455 30456 case SDIOCINSERTUN: 30457 /* Store a un struct to be pushed onto fifo */ 30458 SD_INFO(SD_LOG_SDTEST, un, 30459 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30460 30461 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30462 30463 sd_fault_injection_on = 0; 30464 30465 if (un->sd_fi_fifo_un[i] != NULL) { 30466 kmem_free(un->sd_fi_fifo_un[i], 30467 sizeof (struct sd_fi_un)); 30468 un->sd_fi_fifo_un[i] = NULL; 30469 } 30470 if (arg != NULL) { 30471 un->sd_fi_fifo_un[i] = 30472 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30473 if (un->sd_fi_fifo_un[i] == NULL) { 30474 /* Alloc failed don't store anything */ 30475 break; 30476 } 30477 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30478 sizeof (struct sd_fi_un), 0); 30479 if (rval == -1) { 30480 kmem_free(un->sd_fi_fifo_un[i], 30481 sizeof (struct sd_fi_un)); 30482 un->sd_fi_fifo_un[i] = NULL; 30483 } 30484 30485 } else { 30486 SD_INFO(SD_LOG_IOERR, un, 30487 "sd_faultinjection_ioctl: un null\n"); 30488 } 30489 30490 break; 30491 30492 case SDIOCINSERTARQ: 30493 /* Store a arq struct to be pushed onto fifo */ 30494 SD_INFO(SD_LOG_SDTEST, un, 30495 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30496 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30497 30498 sd_fault_injection_on = 0; 30499 30500 if (un->sd_fi_fifo_arq[i] != NULL) { 30501 kmem_free(un->sd_fi_fifo_arq[i], 30502 sizeof (struct sd_fi_arq)); 30503 un->sd_fi_fifo_arq[i] = NULL; 30504 } 30505 if (arg != NULL) { 30506 un->sd_fi_fifo_arq[i] = 30507 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30508 if (un->sd_fi_fifo_arq[i] == NULL) { 30509 /* Alloc failed don't store anything */ 30510 break; 30511 } 30512 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30513 sizeof (struct sd_fi_arq), 0); 30514 if (rval == -1) { 30515 kmem_free(un->sd_fi_fifo_arq[i], 30516 sizeof (struct sd_fi_arq)); 30517 un->sd_fi_fifo_arq[i] = NULL; 30518 } 30519 30520 } else { 30521 SD_INFO(SD_LOG_IOERR, un, 30522 "sd_faultinjection_ioctl: arq null\n"); 30523 } 30524 30525 break; 30526 30527 case SDIOCPUSH: 30528 /* Push stored xb, pkt, un, and arq onto fifo */ 30529 sd_fault_injection_on = 0; 30530 30531 if (arg != NULL) { 30532 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30533 if (rval != -1 && 30534 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30535 un->sd_fi_fifo_end += i; 30536 } 30537 } else { 30538 SD_INFO(SD_LOG_IOERR, un, 30539 "sd_faultinjection_ioctl: push arg null\n"); 30540 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30541 un->sd_fi_fifo_end++; 30542 } 30543 } 30544 SD_INFO(SD_LOG_IOERR, un, 30545 "sd_faultinjection_ioctl: push to end=%d\n", 30546 un->sd_fi_fifo_end); 30547 break; 30548 30549 case SDIOCRETRIEVE: 30550 /* Return buffer of log from Injection session */ 30551 SD_INFO(SD_LOG_SDTEST, un, 30552 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30553 30554 sd_fault_injection_on = 0; 30555 30556 mutex_enter(&(un->un_fi_mutex)); 30557 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30558 un->sd_fi_buf_len+1, 0); 30559 mutex_exit(&(un->un_fi_mutex)); 30560 30561 if (rval == -1) { 30562 /* 30563 * arg is possibly invalid setting 30564 * it to NULL for return 30565 */ 30566 arg = NULL; 30567 } 30568 break; 30569 } 30570 30571 mutex_exit(SD_MUTEX(un)); 30572 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30573 " exit\n"); 30574 } 30575 30576 30577 /* 30578 * Function: sd_injection_log() 30579 * 30580 * Description: This routine adds buff to the already existing injection log 30581 * for retrieval via faultinjection_ioctl for use in fault 30582 * detection and recovery 30583 * 30584 * Arguments: buf - the string to add to the log 30585 */ 30586 30587 static void 30588 sd_injection_log(char *buf, struct sd_lun *un) 30589 { 30590 uint_t len; 30591 30592 ASSERT(un != NULL); 30593 ASSERT(buf != NULL); 30594 30595 mutex_enter(&(un->un_fi_mutex)); 30596 30597 len = min(strlen(buf), 255); 30598 /* Add logged value to Injection log to be returned later */ 30599 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30600 uint_t offset = strlen((char *)un->sd_fi_log); 30601 char *destp = (char *)un->sd_fi_log + offset; 30602 int i; 30603 for (i = 0; i < len; i++) { 30604 *destp++ = *buf++; 30605 } 30606 un->sd_fi_buf_len += len; 30607 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30608 } 30609 30610 mutex_exit(&(un->un_fi_mutex)); 30611 } 30612 30613 30614 /* 30615 * Function: sd_faultinjection() 30616 * 30617 * Description: This routine takes the pkt and changes its 30618 * content based on error injection scenerio. 30619 * 30620 * Arguments: pktp - packet to be changed 30621 */ 30622 30623 static void 30624 sd_faultinjection(struct scsi_pkt *pktp) 30625 { 30626 uint_t i; 30627 struct sd_fi_pkt *fi_pkt; 30628 struct sd_fi_xb *fi_xb; 30629 struct sd_fi_un *fi_un; 30630 struct sd_fi_arq *fi_arq; 30631 struct buf *bp; 30632 struct sd_xbuf *xb; 30633 struct sd_lun *un; 30634 30635 ASSERT(pktp != NULL); 30636 30637 /* pull bp xb and un from pktp */ 30638 bp = (struct buf *)pktp->pkt_private; 30639 xb = SD_GET_XBUF(bp); 30640 un = SD_GET_UN(bp); 30641 30642 ASSERT(un != NULL); 30643 30644 mutex_enter(SD_MUTEX(un)); 30645 30646 SD_TRACE(SD_LOG_SDTEST, un, 30647 "sd_faultinjection: entry Injection from sdintr\n"); 30648 30649 /* if injection is off return */ 30650 if (sd_fault_injection_on == 0 || 30651 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30652 mutex_exit(SD_MUTEX(un)); 30653 return; 30654 } 30655 30656 30657 /* take next set off fifo */ 30658 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30659 30660 fi_pkt = un->sd_fi_fifo_pkt[i]; 30661 fi_xb = un->sd_fi_fifo_xb[i]; 30662 fi_un = un->sd_fi_fifo_un[i]; 30663 fi_arq = un->sd_fi_fifo_arq[i]; 30664 30665 30666 /* set variables accordingly */ 30667 /* set pkt if it was on fifo */ 30668 if (fi_pkt != NULL) { 30669 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30670 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30671 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30672 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30673 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30674 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30675 30676 } 30677 30678 /* set xb if it was on fifo */ 30679 if (fi_xb != NULL) { 30680 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30681 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30682 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30683 SD_CONDSET(xb, xb, xb_victim_retry_count, 30684 "xb_victim_retry_count"); 30685 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30686 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30687 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30688 30689 /* copy in block data from sense */ 30690 if (fi_xb->xb_sense_data[0] != -1) { 30691 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30692 SENSE_LENGTH); 30693 } 30694 30695 /* copy in extended sense codes */ 30696 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30697 "es_code"); 30698 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30699 "es_key"); 30700 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30701 "es_add_code"); 30702 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30703 es_qual_code, "es_qual_code"); 30704 } 30705 30706 /* set un if it was on fifo */ 30707 if (fi_un != NULL) { 30708 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30709 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30710 SD_CONDSET(un, un, un_reset_retry_count, 30711 "un_reset_retry_count"); 30712 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30713 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30714 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30715 SD_CONDSET(un, un, un_f_geometry_is_valid, 30716 "un_f_geometry_is_valid"); 30717 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30718 "un_f_allow_bus_device_reset"); 30719 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30720 30721 } 30722 30723 /* copy in auto request sense if it was on fifo */ 30724 if (fi_arq != NULL) { 30725 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30726 } 30727 30728 /* free structs */ 30729 if (un->sd_fi_fifo_pkt[i] != NULL) { 30730 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30731 } 30732 if (un->sd_fi_fifo_xb[i] != NULL) { 30733 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30734 } 30735 if (un->sd_fi_fifo_un[i] != NULL) { 30736 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30737 } 30738 if (un->sd_fi_fifo_arq[i] != NULL) { 30739 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30740 } 30741 30742 /* 30743 * kmem_free does not gurantee to set to NULL 30744 * since we uses these to determine if we set 30745 * values or not lets confirm they are always 30746 * NULL after free 30747 */ 30748 un->sd_fi_fifo_pkt[i] = NULL; 30749 un->sd_fi_fifo_un[i] = NULL; 30750 un->sd_fi_fifo_xb[i] = NULL; 30751 un->sd_fi_fifo_arq[i] = NULL; 30752 30753 un->sd_fi_fifo_start++; 30754 30755 mutex_exit(SD_MUTEX(un)); 30756 30757 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30758 } 30759 30760 #endif /* SD_FAULT_INJECTION */ 30761 30762 /* 30763 * This routine is invoked in sd_unit_attach(). Before calling it, the 30764 * properties in conf file should be processed already, and "hotpluggable" 30765 * property was processed also. 30766 * 30767 * The sd driver distinguishes 3 different type of devices: removable media, 30768 * non-removable media, and hotpluggable. Below the differences are defined: 30769 * 30770 * 1. Device ID 30771 * 30772 * The device ID of a device is used to identify this device. Refer to 30773 * ddi_devid_register(9F). 30774 * 30775 * For a non-removable media disk device which can provide 0x80 or 0x83 30776 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30777 * device ID is created to identify this device. For other non-removable 30778 * media devices, a default device ID is created only if this device has 30779 * at least 2 alter cylinders. Otherwise, this device has no devid. 30780 * 30781 * ------------------------------------------------------- 30782 * removable media hotpluggable | Can Have Device ID 30783 * ------------------------------------------------------- 30784 * false false | Yes 30785 * false true | Yes 30786 * true x | No 30787 * ------------------------------------------------------ 30788 * 30789 * 30790 * 2. SCSI group 4 commands 30791 * 30792 * In SCSI specs, only some commands in group 4 command set can use 30793 * 8-byte addresses that can be used to access >2TB storage spaces. 30794 * Other commands have no such capability. Without supporting group4, 30795 * it is impossible to make full use of storage spaces of a disk with 30796 * capacity larger than 2TB. 30797 * 30798 * ----------------------------------------------- 30799 * removable media hotpluggable LP64 | Group 30800 * ----------------------------------------------- 30801 * false false false | 1 30802 * false false true | 4 30803 * false true false | 1 30804 * false true true | 4 30805 * true x x | 5 30806 * ----------------------------------------------- 30807 * 30808 * 30809 * 3. Check for VTOC Label 30810 * 30811 * If a direct-access disk has no EFI label, sd will check if it has a 30812 * valid VTOC label. Now, sd also does that check for removable media 30813 * and hotpluggable devices. 30814 * 30815 * -------------------------------------------------------------- 30816 * Direct-Access removable media hotpluggable | Check Label 30817 * ------------------------------------------------------------- 30818 * false false false | No 30819 * false false true | No 30820 * false true false | Yes 30821 * false true true | Yes 30822 * true x x | Yes 30823 * -------------------------------------------------------------- 30824 * 30825 * 30826 * 4. Building default VTOC label 30827 * 30828 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30829 * If those devices have no valid VTOC label, sd(7d) will attempt to 30830 * create default VTOC for them. Currently sd creates default VTOC label 30831 * for all devices on x86 platform (VTOC_16), but only for removable 30832 * media devices on SPARC (VTOC_8). 30833 * 30834 * ----------------------------------------------------------- 30835 * removable media hotpluggable platform | Default Label 30836 * ----------------------------------------------------------- 30837 * false false sparc | No 30838 * false true x86 | Yes 30839 * false true sparc | Yes 30840 * true x x | Yes 30841 * ---------------------------------------------------------- 30842 * 30843 * 30844 * 5. Supported blocksizes of target devices 30845 * 30846 * Sd supports non-512-byte blocksize for removable media devices only. 30847 * For other devices, only 512-byte blocksize is supported. This may be 30848 * changed in near future because some RAID devices require non-512-byte 30849 * blocksize 30850 * 30851 * ----------------------------------------------------------- 30852 * removable media hotpluggable | non-512-byte blocksize 30853 * ----------------------------------------------------------- 30854 * false false | No 30855 * false true | No 30856 * true x | Yes 30857 * ----------------------------------------------------------- 30858 * 30859 * 30860 * 6. Automatic mount & unmount (i.e. vold) 30861 * 30862 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30863 * if a device is removable media device. It return 1 for removable media 30864 * devices, and 0 for others. 30865 * 30866 * Vold treats a device as removable one only if DKIOREMOVABLE returns 1. 30867 * And it does automounting only for removable media devices. In order to 30868 * preserve users' experience and let vold continue to do automounting for 30869 * USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk 30870 * devices. 30871 * 30872 * ------------------------------------------------------ 30873 * removable media hotpluggable | automatic mount 30874 * ------------------------------------------------------ 30875 * false false | No 30876 * false true | Yes 30877 * true x | Yes 30878 * ------------------------------------------------------ 30879 * 30880 * 30881 * 7. fdisk partition management 30882 * 30883 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30884 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30885 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30886 * fdisk partitions on both x86 and SPARC platform. 30887 * 30888 * ----------------------------------------------------------- 30889 * platform removable media USB/1394 | fdisk supported 30890 * ----------------------------------------------------------- 30891 * x86 X X | true 30892 * ------------------------------------------------------------ 30893 * sparc X X | false 30894 * ------------------------------------------------------------ 30895 * 30896 * 30897 * 8. MBOOT/MBR 30898 * 30899 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30900 * read/write mboot for removable media devices on sparc platform. 30901 * 30902 * ----------------------------------------------------------- 30903 * platform removable media USB/1394 | mboot supported 30904 * ----------------------------------------------------------- 30905 * x86 X X | true 30906 * ------------------------------------------------------------ 30907 * sparc false false | false 30908 * sparc false true | true 30909 * sparc true false | true 30910 * sparc true true | true 30911 * ------------------------------------------------------------ 30912 * 30913 * 30914 * 9. error handling during opening device 30915 * 30916 * If failed to open a disk device, an errno is returned. For some kinds 30917 * of errors, different errno is returned depending on if this device is 30918 * a removable media device. This brings USB/1394 hard disks in line with 30919 * expected hard disk behavior. It is not expected that this breaks any 30920 * application. 30921 * 30922 * ------------------------------------------------------ 30923 * removable media hotpluggable | errno 30924 * ------------------------------------------------------ 30925 * false false | EIO 30926 * false true | EIO 30927 * true x | ENXIO 30928 * ------------------------------------------------------ 30929 * 30930 * 30931 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30932 * 30933 * These IOCTLs are applicable only to removable media devices. 30934 * 30935 * ----------------------------------------------------------- 30936 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30937 * ----------------------------------------------------------- 30938 * false false | No 30939 * false true | No 30940 * true x | Yes 30941 * ----------------------------------------------------------- 30942 * 30943 * 30944 * 12. Kstats for partitions 30945 * 30946 * sd creates partition kstat for non-removable media devices. USB and 30947 * Firewire hard disks now have partition kstats 30948 * 30949 * ------------------------------------------------------ 30950 * removable media hotplugable | kstat 30951 * ------------------------------------------------------ 30952 * false false | Yes 30953 * false true | Yes 30954 * true x | No 30955 * ------------------------------------------------------ 30956 * 30957 * 30958 * 13. Removable media & hotpluggable properties 30959 * 30960 * Sd driver creates a "removable-media" property for removable media 30961 * devices. Parent nexus drivers create a "hotpluggable" property if 30962 * it supports hotplugging. 30963 * 30964 * --------------------------------------------------------------------- 30965 * removable media hotpluggable | "removable-media" " hotpluggable" 30966 * --------------------------------------------------------------------- 30967 * false false | No No 30968 * false true | No Yes 30969 * true false | Yes No 30970 * true true | Yes Yes 30971 * --------------------------------------------------------------------- 30972 * 30973 * 30974 * 14. Power Management 30975 * 30976 * sd only power manages removable media devices or devices that support 30977 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30978 * 30979 * A parent nexus that supports hotplugging can also set "pm-capable" 30980 * if the disk can be power managed. 30981 * 30982 * ------------------------------------------------------------ 30983 * removable media hotpluggable pm-capable | power manage 30984 * ------------------------------------------------------------ 30985 * false false false | No 30986 * false false true | Yes 30987 * false true false | No 30988 * false true true | Yes 30989 * true x x | Yes 30990 * ------------------------------------------------------------ 30991 * 30992 * USB and firewire hard disks can now be power managed independently 30993 * of the framebuffer 30994 * 30995 * 30996 * 15. Support for USB disks with capacity larger than 1TB 30997 * 30998 * Currently, sd doesn't permit a fixed disk device with capacity 30999 * larger than 1TB to be used in a 32-bit operating system environment. 31000 * However, sd doesn't do that for removable media devices. Instead, it 31001 * assumes that removable media devices cannot have a capacity larger 31002 * than 1TB. Therefore, using those devices on 32-bit system is partially 31003 * supported, which can cause some unexpected results. 31004 * 31005 * --------------------------------------------------------------------- 31006 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31007 * --------------------------------------------------------------------- 31008 * false false | true | no 31009 * false true | true | no 31010 * true false | true | Yes 31011 * true true | true | Yes 31012 * --------------------------------------------------------------------- 31013 * 31014 * 31015 * 16. Check write-protection at open time 31016 * 31017 * When a removable media device is being opened for writing without NDELAY 31018 * flag, sd will check if this device is writable. If attempting to open 31019 * without NDELAY flag a write-protected device, this operation will abort. 31020 * 31021 * ------------------------------------------------------------ 31022 * removable media USB/1394 | WP Check 31023 * ------------------------------------------------------------ 31024 * false false | No 31025 * false true | No 31026 * true false | Yes 31027 * true true | Yes 31028 * ------------------------------------------------------------ 31029 * 31030 * 31031 * 17. syslog when corrupted VTOC is encountered 31032 * 31033 * Currently, if an invalid VTOC is encountered, sd only print syslog 31034 * for fixed SCSI disks. 31035 * ------------------------------------------------------------ 31036 * removable media USB/1394 | print syslog 31037 * ------------------------------------------------------------ 31038 * false false | Yes 31039 * false true | No 31040 * true false | No 31041 * true true | No 31042 * ------------------------------------------------------------ 31043 */ 31044 static void 31045 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31046 { 31047 int pm_capable_prop; 31048 31049 ASSERT(un->un_sd); 31050 ASSERT(un->un_sd->sd_inq); 31051 31052 #if defined(_SUNOS_VTOC_16) 31053 /* 31054 * For VTOC_16 devices, the default label will be created for all 31055 * devices. (see sd_build_default_label) 31056 */ 31057 un->un_f_default_vtoc_supported = TRUE; 31058 #endif 31059 31060 if (un->un_sd->sd_inq->inq_rmb) { 31061 /* 31062 * The media of this device is removable. And for this kind 31063 * of devices, it is possible to change medium after openning 31064 * devices. Thus we should support this operation. 31065 */ 31066 un->un_f_has_removable_media = TRUE; 31067 31068 #if defined(_SUNOS_VTOC_8) 31069 /* 31070 * Note: currently, for VTOC_8 devices, default label is 31071 * created for removable and hotpluggable devices only. 31072 */ 31073 un->un_f_default_vtoc_supported = TRUE; 31074 #endif 31075 /* 31076 * support non-512-byte blocksize of removable media devices 31077 */ 31078 un->un_f_non_devbsize_supported = TRUE; 31079 31080 /* 31081 * Assume that all removable media devices support DOOR_LOCK 31082 */ 31083 un->un_f_doorlock_supported = TRUE; 31084 31085 /* 31086 * For a removable media device, it is possible to be opened 31087 * with NDELAY flag when there is no media in drive, in this 31088 * case we don't care if device is writable. But if without 31089 * NDELAY flag, we need to check if media is write-protected. 31090 */ 31091 un->un_f_chk_wp_open = TRUE; 31092 31093 /* 31094 * need to start a SCSI watch thread to monitor media state, 31095 * when media is being inserted or ejected, notify syseventd. 31096 */ 31097 un->un_f_monitor_media_state = TRUE; 31098 31099 /* 31100 * Some devices don't support START_STOP_UNIT command. 31101 * Therefore, we'd better check if a device supports it 31102 * before sending it. 31103 */ 31104 un->un_f_check_start_stop = TRUE; 31105 31106 /* 31107 * support eject media ioctl: 31108 * FDEJECT, DKIOCEJECT, CDROMEJECT 31109 */ 31110 un->un_f_eject_media_supported = TRUE; 31111 31112 /* 31113 * Because many removable-media devices don't support 31114 * LOG_SENSE, we couldn't use this command to check if 31115 * a removable media device support power-management. 31116 * We assume that they support power-management via 31117 * START_STOP_UNIT command and can be spun up and down 31118 * without limitations. 31119 */ 31120 un->un_f_pm_supported = TRUE; 31121 31122 /* 31123 * Need to create a zero length (Boolean) property 31124 * removable-media for the removable media devices. 31125 * Note that the return value of the property is not being 31126 * checked, since if unable to create the property 31127 * then do not want the attach to fail altogether. Consistent 31128 * with other property creation in attach. 31129 */ 31130 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31131 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31132 31133 } else { 31134 /* 31135 * create device ID for device 31136 */ 31137 un->un_f_devid_supported = TRUE; 31138 31139 /* 31140 * Spin up non-removable-media devices once it is attached 31141 */ 31142 un->un_f_attach_spinup = TRUE; 31143 31144 /* 31145 * According to SCSI specification, Sense data has two kinds of 31146 * format: fixed format, and descriptor format. At present, we 31147 * don't support descriptor format sense data for removable 31148 * media. 31149 */ 31150 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31151 un->un_f_descr_format_supported = TRUE; 31152 } 31153 31154 /* 31155 * kstats are created only for non-removable media devices. 31156 * 31157 * Set this in sd.conf to 0 in order to disable kstats. The 31158 * default is 1, so they are enabled by default. 31159 */ 31160 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31161 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31162 "enable-partition-kstats", 1)); 31163 31164 /* 31165 * Check if HBA has set the "pm-capable" property. 31166 * If "pm-capable" exists and is non-zero then we can 31167 * power manage the device without checking the start/stop 31168 * cycle count log sense page. 31169 * 31170 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31171 * then we should not power manage the device. 31172 * 31173 * If "pm-capable" doesn't exist then pm_capable_prop will 31174 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31175 * sd will check the start/stop cycle count log sense page 31176 * and power manage the device if the cycle count limit has 31177 * not been exceeded. 31178 */ 31179 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31180 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31181 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31182 un->un_f_log_sense_supported = TRUE; 31183 } else { 31184 /* 31185 * pm-capable property exists. 31186 * 31187 * Convert "TRUE" values for pm_capable_prop to 31188 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31189 * later. "TRUE" values are any values except 31190 * SD_PM_CAPABLE_FALSE (0) and 31191 * SD_PM_CAPABLE_UNDEFINED (-1) 31192 */ 31193 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31194 un->un_f_log_sense_supported = FALSE; 31195 } else { 31196 un->un_f_pm_supported = TRUE; 31197 } 31198 31199 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31200 "sd_unit_attach: un:0x%p pm-capable " 31201 "property set to %d.\n", un, un->un_f_pm_supported); 31202 } 31203 } 31204 31205 if (un->un_f_is_hotpluggable) { 31206 #if defined(_SUNOS_VTOC_8) 31207 /* 31208 * Note: currently, for VTOC_8 devices, default label is 31209 * created for removable and hotpluggable devices only. 31210 */ 31211 un->un_f_default_vtoc_supported = TRUE; 31212 #endif 31213 31214 /* 31215 * Temporarily, let hotpluggable devices pretend to be 31216 * removable-media devices for vold. 31217 */ 31218 un->un_f_monitor_media_state = TRUE; 31219 31220 un->un_f_check_start_stop = TRUE; 31221 31222 } 31223 31224 /* 31225 * By default, only DIRECT ACCESS devices and CDs will have Sun 31226 * labels. 31227 */ 31228 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31229 (un->un_sd->sd_inq->inq_rmb)) { 31230 /* 31231 * Direct access devices have disk label 31232 */ 31233 un->un_f_vtoc_label_supported = TRUE; 31234 } 31235 31236 /* 31237 * Fdisk partitions are supported for all direct access devices on 31238 * x86 platform, and just for removable media and hotpluggable 31239 * devices on SPARC platform. Later, we will set the following flag 31240 * to FALSE if current device is not removable media or hotpluggable 31241 * device and if sd works on SAPRC platform. 31242 */ 31243 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31244 un->un_f_mboot_supported = TRUE; 31245 } 31246 31247 if (!un->un_f_is_hotpluggable && 31248 !un->un_sd->sd_inq->inq_rmb) { 31249 31250 #if defined(_SUNOS_VTOC_8) 31251 /* 31252 * Don't support fdisk on fixed disk 31253 */ 31254 un->un_f_mboot_supported = FALSE; 31255 #endif 31256 31257 /* 31258 * Fixed disk support SYNC CACHE 31259 */ 31260 un->un_f_sync_cache_supported = TRUE; 31261 31262 /* 31263 * For fixed disk, if its VTOC is not valid, we will write 31264 * errlog into system log 31265 */ 31266 if (un->un_f_vtoc_label_supported) 31267 un->un_f_vtoc_errlog_supported = TRUE; 31268 } 31269 } 31270