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 32 33 34 35 #include <sys/scsi/scsi.h> 36 #include <sys/dkbad.h> 37 #include <sys/dklabel.h> 38 #include <sys/dkio.h> 39 #include <sys/fdio.h> 40 #include <sys/cdio.h> 41 #include <sys/mhd.h> 42 #include <sys/vtoc.h> 43 #include <sys/dktp/fdisk.h> 44 #include <sys/file.h> 45 #include <sys/stat.h> 46 #include <sys/kstat.h> 47 #include <sys/vtrace.h> 48 #include <sys/note.h> 49 #include <sys/thread.h> 50 #include <sys/proc.h> 51 #include <sys/efi_partition.h> 52 #include <sys/var.h> 53 #include <sys/aio_req.h> 54 55 #ifdef __lock_lint 56 #define _LP64 57 #define __amd64 58 #endif 59 60 #if (defined(__fibre)) 61 /* Note: is there a leadville version of the following? */ 62 #include <sys/fc4/fcal_linkapp.h> 63 #endif 64 #include <sys/taskq.h> 65 #include <sys/uuid.h> 66 #include <sys/byteorder.h> 67 #include <sys/sdt.h> 68 69 #include "sd_xbuf.h" 70 71 #include <sys/scsi/targets/sddef.h> 72 73 74 /* 75 * Loadable module info. 76 */ 77 #if (defined(__fibre)) 78 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 79 char _depends_on[] = "misc/scsi drv/fcp"; 80 #else 81 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 82 char _depends_on[] = "misc/scsi"; 83 #endif 84 85 /* 86 * Define the interconnect type, to allow the driver to distinguish 87 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 88 * 89 * This is really for backward compatability. In the future, the driver 90 * should actually check the "interconnect-type" property as reported by 91 * the HBA; however at present this property is not defined by all HBAs, 92 * so we will use this #define (1) to permit the driver to run in 93 * backward-compatability mode; and (2) to print a notification message 94 * if an FC HBA does not support the "interconnect-type" property. The 95 * behavior of the driver will be to assume parallel SCSI behaviors unless 96 * the "interconnect-type" property is defined by the HBA **AND** has a 97 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 98 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 99 * Channel behaviors (as per the old ssd). (Note that the 100 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 101 * will result in the driver assuming parallel SCSI behaviors.) 102 * 103 * (see common/sys/scsi/impl/services.h) 104 * 105 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 106 * since some FC HBAs may already support that, and there is some code in 107 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 108 * default would confuse that code, and besides things should work fine 109 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 110 * "interconnect_type" property. 111 */ 112 #if (defined(__fibre)) 113 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 114 #else 115 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 116 #endif 117 118 /* 119 * The name of the driver, established from the module name in _init. 120 */ 121 static char *sd_label = NULL; 122 123 /* 124 * Driver name is unfortunately prefixed on some driver.conf properties. 125 */ 126 #if (defined(__fibre)) 127 #define sd_max_xfer_size ssd_max_xfer_size 128 #define sd_config_list ssd_config_list 129 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 130 static char *sd_config_list = "ssd-config-list"; 131 #else 132 static char *sd_max_xfer_size = "sd_max_xfer_size"; 133 static char *sd_config_list = "sd-config-list"; 134 #endif 135 136 /* 137 * Driver global variables 138 */ 139 140 #if (defined(__fibre)) 141 /* 142 * These #defines are to avoid namespace collisions that occur because this 143 * code is currently used to compile two seperate driver modules: sd and ssd. 144 * All global variables need to be treated this way (even if declared static) 145 * in order to allow the debugger to resolve the names properly. 146 * It is anticipated that in the near future the ssd module will be obsoleted, 147 * at which time this namespace issue should go away. 148 */ 149 #define sd_state ssd_state 150 #define sd_io_time ssd_io_time 151 #define sd_failfast_enable ssd_failfast_enable 152 #define sd_ua_retry_count ssd_ua_retry_count 153 #define sd_report_pfa ssd_report_pfa 154 #define sd_max_throttle ssd_max_throttle 155 #define sd_min_throttle ssd_min_throttle 156 #define sd_rot_delay ssd_rot_delay 157 158 #define sd_retry_on_reservation_conflict \ 159 ssd_retry_on_reservation_conflict 160 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 161 #define sd_resv_conflict_name ssd_resv_conflict_name 162 163 #define sd_component_mask ssd_component_mask 164 #define sd_level_mask ssd_level_mask 165 #define sd_debug_un ssd_debug_un 166 #define sd_error_level ssd_error_level 167 168 #define sd_xbuf_active_limit ssd_xbuf_active_limit 169 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 170 171 #define sd_tr ssd_tr 172 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 173 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 174 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 175 #define sd_check_media_time ssd_check_media_time 176 #define sd_wait_cmds_complete ssd_wait_cmds_complete 177 #define sd_label_mutex ssd_label_mutex 178 #define sd_detach_mutex ssd_detach_mutex 179 #define sd_log_buf ssd_log_buf 180 #define sd_log_mutex ssd_log_mutex 181 182 #define sd_disk_table ssd_disk_table 183 #define sd_disk_table_size ssd_disk_table_size 184 #define sd_sense_mutex ssd_sense_mutex 185 #define sd_cdbtab ssd_cdbtab 186 187 #define sd_cb_ops ssd_cb_ops 188 #define sd_ops ssd_ops 189 #define sd_additional_codes ssd_additional_codes 190 191 #define sd_minor_data ssd_minor_data 192 #define sd_minor_data_efi ssd_minor_data_efi 193 194 #define sd_tq ssd_tq 195 #define sd_wmr_tq ssd_wmr_tq 196 #define sd_taskq_name ssd_taskq_name 197 #define sd_wmr_taskq_name ssd_wmr_taskq_name 198 #define sd_taskq_minalloc ssd_taskq_minalloc 199 #define sd_taskq_maxalloc ssd_taskq_maxalloc 200 201 #define sd_dump_format_string ssd_dump_format_string 202 203 #define sd_iostart_chain ssd_iostart_chain 204 #define sd_iodone_chain ssd_iodone_chain 205 206 #define sd_pm_idletime ssd_pm_idletime 207 208 #define sd_force_pm_supported ssd_force_pm_supported 209 210 #define sd_dtype_optical_bind ssd_dtype_optical_bind 211 212 #endif 213 214 215 #ifdef SDDEBUG 216 int sd_force_pm_supported = 0; 217 #endif /* SDDEBUG */ 218 219 void *sd_state = NULL; 220 int sd_io_time = SD_IO_TIME; 221 int sd_failfast_enable = 1; 222 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 223 int sd_report_pfa = 1; 224 int sd_max_throttle = SD_MAX_THROTTLE; 225 int sd_min_throttle = SD_MIN_THROTTLE; 226 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 227 int sd_qfull_throttle_enable = TRUE; 228 229 int sd_retry_on_reservation_conflict = 1; 230 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 231 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 232 233 static int sd_dtype_optical_bind = -1; 234 235 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 236 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 237 238 /* 239 * Global data for debug logging. To enable debug printing, sd_component_mask 240 * and sd_level_mask should be set to the desired bit patterns as outlined in 241 * sddef.h. 242 */ 243 uint_t sd_component_mask = 0x0; 244 uint_t sd_level_mask = 0x0; 245 struct sd_lun *sd_debug_un = NULL; 246 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 247 248 /* Note: these may go away in the future... */ 249 static uint32_t sd_xbuf_active_limit = 512; 250 static uint32_t sd_xbuf_reserve_limit = 16; 251 252 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 253 254 /* 255 * Timer value used to reset the throttle after it has been reduced 256 * (typically in response to TRAN_BUSY or STATUS_QFULL) 257 */ 258 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 259 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 260 261 /* 262 * Interval value associated with the media change scsi watch. 263 */ 264 static int sd_check_media_time = 3000000; 265 266 /* 267 * Wait value used for in progress operations during a DDI_SUSPEND 268 */ 269 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 270 271 /* 272 * sd_label_mutex protects a static buffer used in the disk label 273 * component of the driver 274 */ 275 static kmutex_t sd_label_mutex; 276 277 /* 278 * sd_detach_mutex protects un_layer_count, un_detach_count, and 279 * un_opens_in_progress in the sd_lun structure. 280 */ 281 static kmutex_t sd_detach_mutex; 282 283 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 284 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 285 286 /* 287 * Global buffer and mutex for debug logging 288 */ 289 static char sd_log_buf[1024]; 290 static kmutex_t sd_log_mutex; 291 292 293 /* 294 * "Smart" Probe Caching structs, globals, #defines, etc. 295 * For parallel scsi and non-self-identify device only. 296 */ 297 298 /* 299 * The following resources and routines are implemented to support 300 * "smart" probing, which caches the scsi_probe() results in an array, 301 * in order to help avoid long probe times. 302 */ 303 struct sd_scsi_probe_cache { 304 struct sd_scsi_probe_cache *next; 305 dev_info_t *pdip; 306 int cache[NTARGETS_WIDE]; 307 }; 308 309 static kmutex_t sd_scsi_probe_cache_mutex; 310 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 311 312 /* 313 * Really we only need protection on the head of the linked list, but 314 * better safe than sorry. 315 */ 316 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 317 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 318 319 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 320 sd_scsi_probe_cache_head)) 321 322 323 /* 324 * Vendor specific data name property declarations 325 */ 326 327 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 328 329 static sd_tunables seagate_properties = { 330 SEAGATE_THROTTLE_VALUE, 331 0, 332 0, 333 0, 334 0, 335 0, 336 0, 337 0, 338 0 339 }; 340 341 342 static sd_tunables fujitsu_properties = { 343 FUJITSU_THROTTLE_VALUE, 344 0, 345 0, 346 0, 347 0, 348 0, 349 0, 350 0, 351 0 352 }; 353 354 static sd_tunables ibm_properties = { 355 IBM_THROTTLE_VALUE, 356 0, 357 0, 358 0, 359 0, 360 0, 361 0, 362 0, 363 0 364 }; 365 366 static sd_tunables purple_properties = { 367 PURPLE_THROTTLE_VALUE, 368 0, 369 0, 370 PURPLE_BUSY_RETRIES, 371 PURPLE_RESET_RETRY_COUNT, 372 PURPLE_RESERVE_RELEASE_TIME, 373 0, 374 0, 375 0 376 }; 377 378 static sd_tunables sve_properties = { 379 SVE_THROTTLE_VALUE, 380 0, 381 0, 382 SVE_BUSY_RETRIES, 383 SVE_RESET_RETRY_COUNT, 384 SVE_RESERVE_RELEASE_TIME, 385 SVE_MIN_THROTTLE_VALUE, 386 SVE_DISKSORT_DISABLED_FLAG, 387 0 388 }; 389 390 static sd_tunables maserati_properties = { 391 0, 392 0, 393 0, 394 0, 395 0, 396 0, 397 0, 398 MASERATI_DISKSORT_DISABLED_FLAG, 399 MASERATI_LUN_RESET_ENABLED_FLAG 400 }; 401 402 static sd_tunables pirus_properties = { 403 PIRUS_THROTTLE_VALUE, 404 0, 405 PIRUS_NRR_COUNT, 406 PIRUS_BUSY_RETRIES, 407 PIRUS_RESET_RETRY_COUNT, 408 0, 409 PIRUS_MIN_THROTTLE_VALUE, 410 PIRUS_DISKSORT_DISABLED_FLAG, 411 PIRUS_LUN_RESET_ENABLED_FLAG 412 }; 413 414 #endif 415 416 #if (defined(__sparc) && !defined(__fibre)) || \ 417 (defined(__i386) || defined(__amd64)) 418 419 420 static sd_tunables elite_properties = { 421 ELITE_THROTTLE_VALUE, 422 0, 423 0, 424 0, 425 0, 426 0, 427 0, 428 0, 429 0 430 }; 431 432 static sd_tunables st31200n_properties = { 433 ST31200N_THROTTLE_VALUE, 434 0, 435 0, 436 0, 437 0, 438 0, 439 0, 440 0, 441 0 442 }; 443 444 #endif /* Fibre or not */ 445 446 static sd_tunables lsi_properties_scsi = { 447 LSI_THROTTLE_VALUE, 448 0, 449 LSI_NOTREADY_RETRIES, 450 0, 451 0, 452 0, 453 0, 454 0, 455 0 456 }; 457 458 static sd_tunables symbios_properties = { 459 SYMBIOS_THROTTLE_VALUE, 460 0, 461 SYMBIOS_NOTREADY_RETRIES, 462 0, 463 0, 464 0, 465 0, 466 0, 467 0 468 }; 469 470 static sd_tunables lsi_properties = { 471 0, 472 0, 473 LSI_NOTREADY_RETRIES, 474 0, 475 0, 476 0, 477 0, 478 0, 479 0 480 }; 481 482 static sd_tunables lsi_oem_properties = { 483 0, 484 0, 485 LSI_OEM_NOTREADY_RETRIES, 486 0, 487 0, 488 0, 489 0, 490 0, 491 0 492 }; 493 494 495 496 #if (defined(SD_PROP_TST)) 497 498 #define SD_TST_CTYPE_VAL CTYPE_CDROM 499 #define SD_TST_THROTTLE_VAL 16 500 #define SD_TST_NOTREADY_VAL 12 501 #define SD_TST_BUSY_VAL 60 502 #define SD_TST_RST_RETRY_VAL 36 503 #define SD_TST_RSV_REL_TIME 60 504 505 static sd_tunables tst_properties = { 506 SD_TST_THROTTLE_VAL, 507 SD_TST_CTYPE_VAL, 508 SD_TST_NOTREADY_VAL, 509 SD_TST_BUSY_VAL, 510 SD_TST_RST_RETRY_VAL, 511 SD_TST_RSV_REL_TIME, 512 0, 513 0, 514 0 515 }; 516 #endif 517 518 /* This is similiar to the ANSI toupper implementation */ 519 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 520 521 /* 522 * Static Driver Configuration Table 523 * 524 * This is the table of disks which need throttle adjustment (or, perhaps 525 * something else as defined by the flags at a future time.) device_id 526 * is a string consisting of concatenated vid (vendor), pid (product/model) 527 * and revision strings as defined in the scsi_inquiry structure. Offsets of 528 * the parts of the string are as defined by the sizes in the scsi_inquiry 529 * structure. Device type is searched as far as the device_id string is 530 * defined. Flags defines which values are to be set in the driver from the 531 * properties list. 532 * 533 * Entries below which begin and end with a "*" are a special case. 534 * These do not have a specific vendor, and the string which follows 535 * can appear anywhere in the 16 byte PID portion of the inquiry data. 536 * 537 * Entries below which begin and end with a " " (blank) are a special 538 * case. The comparison function will treat multiple consecutive blanks 539 * as equivalent to a single blank. For example, this causes a 540 * sd_disk_table entry of " NEC CDROM " to match a device's id string 541 * of "NEC CDROM". 542 * 543 * Note: The MD21 controller type has been obsoleted. 544 * ST318202F is a Legacy device 545 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 546 * made with an FC connection. The entries here are a legacy. 547 */ 548 static sd_disk_config_t sd_disk_table[] = { 549 #if defined(__fibre) || defined(__i386) || defined(__amd64) 550 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 551 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 552 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 553 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 554 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 555 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 556 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 557 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 558 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 559 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 560 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 561 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 562 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 563 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 564 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 565 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 566 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 567 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 568 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 569 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 570 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 571 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 572 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 573 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 574 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 575 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 576 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 577 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 578 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 579 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 580 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 581 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 582 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 583 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 584 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 585 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 586 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 587 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 588 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 589 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 590 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 591 { "SUN T3", SD_CONF_BSET_THROTTLE | 592 SD_CONF_BSET_BSY_RETRY_COUNT| 593 SD_CONF_BSET_RST_RETRIES| 594 SD_CONF_BSET_RSV_REL_TIME, 595 &purple_properties }, 596 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 597 SD_CONF_BSET_BSY_RETRY_COUNT| 598 SD_CONF_BSET_RST_RETRIES| 599 SD_CONF_BSET_RSV_REL_TIME| 600 SD_CONF_BSET_MIN_THROTTLE| 601 SD_CONF_BSET_DISKSORT_DISABLED, 602 &sve_properties }, 603 { "SUN T4", SD_CONF_BSET_THROTTLE | 604 SD_CONF_BSET_BSY_RETRY_COUNT| 605 SD_CONF_BSET_RST_RETRIES| 606 SD_CONF_BSET_RSV_REL_TIME, 607 &purple_properties }, 608 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 609 SD_CONF_BSET_LUN_RESET_ENABLED, 610 &maserati_properties }, 611 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 612 SD_CONF_BSET_NRR_COUNT| 613 SD_CONF_BSET_BSY_RETRY_COUNT| 614 SD_CONF_BSET_RST_RETRIES| 615 SD_CONF_BSET_MIN_THROTTLE| 616 SD_CONF_BSET_DISKSORT_DISABLED| 617 SD_CONF_BSET_LUN_RESET_ENABLED, 618 &pirus_properties }, 619 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 620 SD_CONF_BSET_NRR_COUNT| 621 SD_CONF_BSET_BSY_RETRY_COUNT| 622 SD_CONF_BSET_RST_RETRIES| 623 SD_CONF_BSET_MIN_THROTTLE| 624 SD_CONF_BSET_DISKSORT_DISABLED| 625 SD_CONF_BSET_LUN_RESET_ENABLED, 626 &pirus_properties }, 627 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 628 SD_CONF_BSET_NRR_COUNT| 629 SD_CONF_BSET_BSY_RETRY_COUNT| 630 SD_CONF_BSET_RST_RETRIES| 631 SD_CONF_BSET_MIN_THROTTLE| 632 SD_CONF_BSET_DISKSORT_DISABLED| 633 SD_CONF_BSET_LUN_RESET_ENABLED, 634 &pirus_properties }, 635 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 636 SD_CONF_BSET_NRR_COUNT| 637 SD_CONF_BSET_BSY_RETRY_COUNT| 638 SD_CONF_BSET_RST_RETRIES| 639 SD_CONF_BSET_MIN_THROTTLE| 640 SD_CONF_BSET_DISKSORT_DISABLED| 641 SD_CONF_BSET_LUN_RESET_ENABLED, 642 &pirus_properties }, 643 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 644 SD_CONF_BSET_NRR_COUNT| 645 SD_CONF_BSET_BSY_RETRY_COUNT| 646 SD_CONF_BSET_RST_RETRIES| 647 SD_CONF_BSET_MIN_THROTTLE| 648 SD_CONF_BSET_DISKSORT_DISABLED| 649 SD_CONF_BSET_LUN_RESET_ENABLED, 650 &pirus_properties }, 651 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 652 SD_CONF_BSET_NRR_COUNT| 653 SD_CONF_BSET_BSY_RETRY_COUNT| 654 SD_CONF_BSET_RST_RETRIES| 655 SD_CONF_BSET_MIN_THROTTLE| 656 SD_CONF_BSET_DISKSORT_DISABLED| 657 SD_CONF_BSET_LUN_RESET_ENABLED, 658 &pirus_properties }, 659 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 660 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 661 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 662 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 663 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 664 #endif /* fibre or NON-sparc platforms */ 665 #if ((defined(__sparc) && !defined(__fibre)) ||\ 666 (defined(__i386) || defined(__amd64))) 667 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 668 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 669 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 670 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 671 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 672 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 673 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 674 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 675 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 676 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 677 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 678 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 679 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 680 &symbios_properties }, 681 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 682 &lsi_properties_scsi }, 683 #if defined(__i386) || defined(__amd64) 684 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 685 | SD_CONF_BSET_READSUB_BCD 686 | SD_CONF_BSET_READ_TOC_ADDR_BCD 687 | SD_CONF_BSET_NO_READ_HEADER 688 | SD_CONF_BSET_READ_CD_XD4), NULL }, 689 690 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 691 | SD_CONF_BSET_READSUB_BCD 692 | SD_CONF_BSET_READ_TOC_ADDR_BCD 693 | SD_CONF_BSET_NO_READ_HEADER 694 | SD_CONF_BSET_READ_CD_XD4), NULL }, 695 #endif /* __i386 || __amd64 */ 696 #endif /* sparc NON-fibre or NON-sparc platforms */ 697 698 #if (defined(SD_PROP_TST)) 699 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 700 | SD_CONF_BSET_CTYPE 701 | SD_CONF_BSET_NRR_COUNT 702 | SD_CONF_BSET_FAB_DEVID 703 | SD_CONF_BSET_NOCACHE 704 | SD_CONF_BSET_BSY_RETRY_COUNT 705 | SD_CONF_BSET_PLAYMSF_BCD 706 | SD_CONF_BSET_READSUB_BCD 707 | SD_CONF_BSET_READ_TOC_TRK_BCD 708 | SD_CONF_BSET_READ_TOC_ADDR_BCD 709 | SD_CONF_BSET_NO_READ_HEADER 710 | SD_CONF_BSET_READ_CD_XD4 711 | SD_CONF_BSET_RST_RETRIES 712 | SD_CONF_BSET_RSV_REL_TIME 713 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 714 #endif 715 }; 716 717 static const int sd_disk_table_size = 718 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 719 720 721 /* 722 * Return codes of sd_uselabel(). 723 */ 724 #define SD_LABEL_IS_VALID 0 725 #define SD_LABEL_IS_INVALID 1 726 727 #define SD_INTERCONNECT_PARALLEL 0 728 #define SD_INTERCONNECT_FABRIC 1 729 #define SD_INTERCONNECT_FIBRE 2 730 #define SD_INTERCONNECT_SSA 3 731 #define SD_IS_PARALLEL_SCSI(un) \ 732 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 733 734 /* 735 * Definitions used by device id registration routines 736 */ 737 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 738 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 739 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 740 #define WD_NODE 7 /* the whole disk minor */ 741 742 static kmutex_t sd_sense_mutex = {0}; 743 744 /* 745 * Macros for updates of the driver state 746 */ 747 #define New_state(un, s) \ 748 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 749 #define Restore_state(un) \ 750 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 751 752 static struct sd_cdbinfo sd_cdbtab[] = { 753 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 754 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 755 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 756 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 757 }; 758 759 /* 760 * Specifies the number of seconds that must have elapsed since the last 761 * cmd. has completed for a device to be declared idle to the PM framework. 762 */ 763 static int sd_pm_idletime = 1; 764 765 /* 766 * Internal function prototypes 767 */ 768 769 #if (defined(__fibre)) 770 /* 771 * These #defines are to avoid namespace collisions that occur because this 772 * code is currently used to compile two seperate driver modules: sd and ssd. 773 * All function names need to be treated this way (even if declared static) 774 * in order to allow the debugger to resolve the names properly. 775 * It is anticipated that in the near future the ssd module will be obsoleted, 776 * at which time this ugliness should go away. 777 */ 778 #define sd_log_trace ssd_log_trace 779 #define sd_log_info ssd_log_info 780 #define sd_log_err ssd_log_err 781 #define sdprobe ssdprobe 782 #define sdinfo ssdinfo 783 #define sd_prop_op ssd_prop_op 784 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 785 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 786 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 787 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 788 #define sd_spin_up_unit ssd_spin_up_unit 789 #define sd_enable_descr_sense ssd_enable_descr_sense 790 #define sd_set_mmc_caps ssd_set_mmc_caps 791 #define sd_read_unit_properties ssd_read_unit_properties 792 #define sd_process_sdconf_file ssd_process_sdconf_file 793 #define sd_process_sdconf_table ssd_process_sdconf_table 794 #define sd_sdconf_id_match ssd_sdconf_id_match 795 #define sd_blank_cmp ssd_blank_cmp 796 #define sd_chk_vers1_data ssd_chk_vers1_data 797 #define sd_set_vers1_properties ssd_set_vers1_properties 798 #define sd_validate_geometry ssd_validate_geometry 799 800 #if defined(_SUNOS_VTOC_16) 801 #define sd_convert_geometry ssd_convert_geometry 802 #endif 803 804 #define sd_resync_geom_caches ssd_resync_geom_caches 805 #define sd_read_fdisk ssd_read_fdisk 806 #define sd_get_physical_geometry ssd_get_physical_geometry 807 #define sd_get_virtual_geometry ssd_get_virtual_geometry 808 #define sd_update_block_info ssd_update_block_info 809 #define sd_swap_efi_gpt ssd_swap_efi_gpt 810 #define sd_swap_efi_gpe ssd_swap_efi_gpe 811 #define sd_validate_efi ssd_validate_efi 812 #define sd_use_efi ssd_use_efi 813 #define sd_uselabel ssd_uselabel 814 #define sd_build_default_label ssd_build_default_label 815 #define sd_has_max_chs_vals ssd_has_max_chs_vals 816 #define sd_inq_fill ssd_inq_fill 817 #define sd_register_devid ssd_register_devid 818 #define sd_get_devid_block ssd_get_devid_block 819 #define sd_get_devid ssd_get_devid 820 #define sd_create_devid ssd_create_devid 821 #define sd_write_deviceid ssd_write_deviceid 822 #define sd_check_vpd_page_support ssd_check_vpd_page_support 823 #define sd_setup_pm ssd_setup_pm 824 #define sd_create_pm_components ssd_create_pm_components 825 #define sd_ddi_suspend ssd_ddi_suspend 826 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 827 #define sd_ddi_resume ssd_ddi_resume 828 #define sd_ddi_pm_resume ssd_ddi_pm_resume 829 #define sdpower ssdpower 830 #define sdattach ssdattach 831 #define sddetach ssddetach 832 #define sd_unit_attach ssd_unit_attach 833 #define sd_unit_detach ssd_unit_detach 834 #define sd_set_unit_attributes ssd_set_unit_attributes 835 #define sd_create_minor_nodes ssd_create_minor_nodes 836 #define sd_create_errstats ssd_create_errstats 837 #define sd_set_errstats ssd_set_errstats 838 #define sd_set_pstats ssd_set_pstats 839 #define sddump ssddump 840 #define sd_scsi_poll ssd_scsi_poll 841 #define sd_send_polled_RQS ssd_send_polled_RQS 842 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 843 #define sd_init_event_callbacks ssd_init_event_callbacks 844 #define sd_event_callback ssd_event_callback 845 #define sd_cache_control ssd_cache_control 846 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 847 #define sd_make_device ssd_make_device 848 #define sdopen ssdopen 849 #define sdclose ssdclose 850 #define sd_ready_and_valid ssd_ready_and_valid 851 #define sdmin ssdmin 852 #define sdread ssdread 853 #define sdwrite ssdwrite 854 #define sdaread ssdaread 855 #define sdawrite ssdawrite 856 #define sdstrategy ssdstrategy 857 #define sdioctl ssdioctl 858 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 859 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 860 #define sd_checksum_iostart ssd_checksum_iostart 861 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 862 #define sd_pm_iostart ssd_pm_iostart 863 #define sd_core_iostart ssd_core_iostart 864 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 865 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 866 #define sd_checksum_iodone ssd_checksum_iodone 867 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 868 #define sd_pm_iodone ssd_pm_iodone 869 #define sd_initpkt_for_buf ssd_initpkt_for_buf 870 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 871 #define sd_setup_rw_pkt ssd_setup_rw_pkt 872 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 873 #define sd_buf_iodone ssd_buf_iodone 874 #define sd_uscsi_strategy ssd_uscsi_strategy 875 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 876 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 877 #define sd_uscsi_iodone ssd_uscsi_iodone 878 #define sd_xbuf_strategy ssd_xbuf_strategy 879 #define sd_xbuf_init ssd_xbuf_init 880 #define sd_pm_entry ssd_pm_entry 881 #define sd_pm_exit ssd_pm_exit 882 883 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 884 #define sd_pm_timeout_handler ssd_pm_timeout_handler 885 886 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 887 #define sdintr ssdintr 888 #define sd_start_cmds ssd_start_cmds 889 #define sd_send_scsi_cmd ssd_send_scsi_cmd 890 #define sd_bioclone_alloc ssd_bioclone_alloc 891 #define sd_bioclone_free ssd_bioclone_free 892 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 893 #define sd_shadow_buf_free ssd_shadow_buf_free 894 #define sd_print_transport_rejected_message \ 895 ssd_print_transport_rejected_message 896 #define sd_retry_command ssd_retry_command 897 #define sd_set_retry_bp ssd_set_retry_bp 898 #define sd_send_request_sense_command ssd_send_request_sense_command 899 #define sd_start_retry_command ssd_start_retry_command 900 #define sd_start_direct_priority_command \ 901 ssd_start_direct_priority_command 902 #define sd_return_failed_command ssd_return_failed_command 903 #define sd_return_failed_command_no_restart \ 904 ssd_return_failed_command_no_restart 905 #define sd_return_command ssd_return_command 906 #define sd_sync_with_callback ssd_sync_with_callback 907 #define sdrunout ssdrunout 908 #define sd_mark_rqs_busy ssd_mark_rqs_busy 909 #define sd_mark_rqs_idle ssd_mark_rqs_idle 910 #define sd_reduce_throttle ssd_reduce_throttle 911 #define sd_restore_throttle ssd_restore_throttle 912 #define sd_print_incomplete_msg ssd_print_incomplete_msg 913 #define sd_init_cdb_limits ssd_init_cdb_limits 914 #define sd_pkt_status_good ssd_pkt_status_good 915 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 916 #define sd_pkt_status_busy ssd_pkt_status_busy 917 #define sd_pkt_status_reservation_conflict \ 918 ssd_pkt_status_reservation_conflict 919 #define sd_pkt_status_qfull ssd_pkt_status_qfull 920 #define sd_handle_request_sense ssd_handle_request_sense 921 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 922 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 923 #define sd_validate_sense_data ssd_validate_sense_data 924 #define sd_decode_sense ssd_decode_sense 925 #define sd_print_sense_msg ssd_print_sense_msg 926 #define sd_extract_sense_info_descr ssd_extract_sense_info_descr 927 #define sd_sense_key_no_sense ssd_sense_key_no_sense 928 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 929 #define sd_sense_key_not_ready ssd_sense_key_not_ready 930 #define sd_sense_key_medium_or_hardware_error \ 931 ssd_sense_key_medium_or_hardware_error 932 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 933 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 934 #define sd_sense_key_fail_command ssd_sense_key_fail_command 935 #define sd_sense_key_blank_check ssd_sense_key_blank_check 936 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 937 #define sd_sense_key_default ssd_sense_key_default 938 #define sd_print_retry_msg ssd_print_retry_msg 939 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 940 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 941 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 942 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 943 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 944 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 945 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 946 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 947 #define sd_pkt_reason_default ssd_pkt_reason_default 948 #define sd_reset_target ssd_reset_target 949 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 950 #define sd_start_stop_unit_task ssd_start_stop_unit_task 951 #define sd_taskq_create ssd_taskq_create 952 #define sd_taskq_delete ssd_taskq_delete 953 #define sd_media_change_task ssd_media_change_task 954 #define sd_handle_mchange ssd_handle_mchange 955 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 956 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 957 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 958 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 959 #define sd_send_scsi_feature_GET_CONFIGURATION \ 960 sd_send_scsi_feature_GET_CONFIGURATION 961 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 962 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 963 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 964 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 965 ssd_send_scsi_PERSISTENT_RESERVE_IN 966 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 967 ssd_send_scsi_PERSISTENT_RESERVE_OUT 968 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 969 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 970 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 971 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 972 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 973 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 974 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 975 #define sd_alloc_rqs ssd_alloc_rqs 976 #define sd_free_rqs ssd_free_rqs 977 #define sd_dump_memory ssd_dump_memory 978 #define sd_uscsi_ioctl ssd_uscsi_ioctl 979 #define sd_get_media_info ssd_get_media_info 980 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 981 #define sd_dkio_get_geometry ssd_dkio_get_geometry 982 #define sd_dkio_set_geometry ssd_dkio_set_geometry 983 #define sd_dkio_get_partition ssd_dkio_get_partition 984 #define sd_dkio_set_partition ssd_dkio_set_partition 985 #define sd_dkio_partition ssd_dkio_partition 986 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 987 #define sd_dkio_get_efi ssd_dkio_get_efi 988 #define sd_build_user_vtoc ssd_build_user_vtoc 989 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 990 #define sd_dkio_set_efi ssd_dkio_set_efi 991 #define sd_build_label_vtoc ssd_build_label_vtoc 992 #define sd_write_label ssd_write_label 993 #define sd_clear_vtoc ssd_clear_vtoc 994 #define sd_clear_efi ssd_clear_efi 995 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 996 #define sd_setup_next_xfer ssd_setup_next_xfer 997 #define sd_dkio_get_temp ssd_dkio_get_temp 998 #define sd_dkio_get_mboot ssd_dkio_get_mboot 999 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1000 #define sd_setup_default_geometry ssd_setup_default_geometry 1001 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1002 #define sd_check_mhd ssd_check_mhd 1003 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1004 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1005 #define sd_sname ssd_sname 1006 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1007 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1008 #define sd_take_ownership ssd_take_ownership 1009 #define sd_reserve_release ssd_reserve_release 1010 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1011 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1012 #define sd_persistent_reservation_in_read_keys \ 1013 ssd_persistent_reservation_in_read_keys 1014 #define sd_persistent_reservation_in_read_resv \ 1015 ssd_persistent_reservation_in_read_resv 1016 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1017 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1018 #define sd_mhdioc_release ssd_mhdioc_release 1019 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1020 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1021 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1022 #define sr_change_blkmode ssr_change_blkmode 1023 #define sr_change_speed ssr_change_speed 1024 #define sr_atapi_change_speed ssr_atapi_change_speed 1025 #define sr_pause_resume ssr_pause_resume 1026 #define sr_play_msf ssr_play_msf 1027 #define sr_play_trkind ssr_play_trkind 1028 #define sr_read_all_subcodes ssr_read_all_subcodes 1029 #define sr_read_subchannel ssr_read_subchannel 1030 #define sr_read_tocentry ssr_read_tocentry 1031 #define sr_read_tochdr ssr_read_tochdr 1032 #define sr_read_cdda ssr_read_cdda 1033 #define sr_read_cdxa ssr_read_cdxa 1034 #define sr_read_mode1 ssr_read_mode1 1035 #define sr_read_mode2 ssr_read_mode2 1036 #define sr_read_cd_mode2 ssr_read_cd_mode2 1037 #define sr_sector_mode ssr_sector_mode 1038 #define sr_eject ssr_eject 1039 #define sr_ejected ssr_ejected 1040 #define sr_check_wp ssr_check_wp 1041 #define sd_check_media ssd_check_media 1042 #define sd_media_watch_cb ssd_media_watch_cb 1043 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1044 #define sr_volume_ctrl ssr_volume_ctrl 1045 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1046 #define sd_log_page_supported ssd_log_page_supported 1047 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1048 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1049 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1050 #define sd_range_lock ssd_range_lock 1051 #define sd_get_range ssd_get_range 1052 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1053 #define sd_range_unlock ssd_range_unlock 1054 #define sd_read_modify_write_task ssd_read_modify_write_task 1055 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1056 1057 #define sd_iostart_chain ssd_iostart_chain 1058 #define sd_iodone_chain ssd_iodone_chain 1059 #define sd_initpkt_map ssd_initpkt_map 1060 #define sd_destroypkt_map ssd_destroypkt_map 1061 #define sd_chain_type_map ssd_chain_type_map 1062 #define sd_chain_index_map ssd_chain_index_map 1063 1064 #define sd_failfast_flushctl ssd_failfast_flushctl 1065 #define sd_failfast_flushq ssd_failfast_flushq 1066 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1067 1068 #define sd_is_lsi ssd_is_lsi 1069 1070 #endif /* #if (defined(__fibre)) */ 1071 1072 1073 int _init(void); 1074 int _fini(void); 1075 int _info(struct modinfo *modinfop); 1076 1077 /*PRINTFLIKE3*/ 1078 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1079 /*PRINTFLIKE3*/ 1080 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1081 /*PRINTFLIKE3*/ 1082 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1083 1084 static int sdprobe(dev_info_t *devi); 1085 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1086 void **result); 1087 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1088 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1089 1090 /* 1091 * Smart probe for parallel scsi 1092 */ 1093 static void sd_scsi_probe_cache_init(void); 1094 static void sd_scsi_probe_cache_fini(void); 1095 static void sd_scsi_clear_probe_cache(void); 1096 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1097 1098 static int sd_spin_up_unit(struct sd_lun *un); 1099 #ifdef _LP64 1100 static void sd_enable_descr_sense(struct sd_lun *un); 1101 #endif /* _LP64 */ 1102 static void sd_set_mmc_caps(struct sd_lun *un); 1103 1104 static void sd_read_unit_properties(struct sd_lun *un); 1105 static int sd_process_sdconf_file(struct sd_lun *un); 1106 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1107 int *data_list, sd_tunables *values); 1108 static void sd_process_sdconf_table(struct sd_lun *un); 1109 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1110 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1111 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1112 int list_len, char *dataname_ptr); 1113 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1114 sd_tunables *prop_list); 1115 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1116 1117 #if defined(_SUNOS_VTOC_16) 1118 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1119 #endif 1120 1121 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1122 int path_flag); 1123 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1124 int path_flag); 1125 static void sd_get_physical_geometry(struct sd_lun *un, 1126 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1127 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1128 int lbasize); 1129 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1130 static void sd_swap_efi_gpt(efi_gpt_t *); 1131 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1132 static int sd_validate_efi(efi_gpt_t *); 1133 static int sd_use_efi(struct sd_lun *, int); 1134 static void sd_build_default_label(struct sd_lun *un); 1135 1136 #if defined(_FIRMWARE_NEEDS_FDISK) 1137 static int sd_has_max_chs_vals(struct ipart *fdp); 1138 #endif 1139 static void sd_inq_fill(char *p, int l, char *s); 1140 1141 1142 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1143 int reservation_flag); 1144 static daddr_t sd_get_devid_block(struct sd_lun *un); 1145 static int sd_get_devid(struct sd_lun *un); 1146 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1147 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1148 static int sd_write_deviceid(struct sd_lun *un); 1149 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1150 static int sd_check_vpd_page_support(struct sd_lun *un); 1151 1152 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1153 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1154 1155 static int sd_ddi_suspend(dev_info_t *devi); 1156 static int sd_ddi_pm_suspend(struct sd_lun *un); 1157 static int sd_ddi_resume(dev_info_t *devi); 1158 static int sd_ddi_pm_resume(struct sd_lun *un); 1159 static int sdpower(dev_info_t *devi, int component, int level); 1160 1161 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1162 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1163 static int sd_unit_attach(dev_info_t *devi); 1164 static int sd_unit_detach(dev_info_t *devi); 1165 1166 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1167 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1168 static void sd_create_errstats(struct sd_lun *un, int instance); 1169 static void sd_set_errstats(struct sd_lun *un); 1170 static void sd_set_pstats(struct sd_lun *un); 1171 1172 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1173 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1174 static int sd_send_polled_RQS(struct sd_lun *un); 1175 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1176 1177 #if (defined(__fibre)) 1178 /* 1179 * Event callbacks (photon) 1180 */ 1181 static void sd_init_event_callbacks(struct sd_lun *un); 1182 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1183 #endif 1184 1185 /* 1186 * Defines for sd_cache_control 1187 */ 1188 1189 #define SD_CACHE_ENABLE 1 1190 #define SD_CACHE_DISABLE 0 1191 #define SD_CACHE_NOCHANGE -1 1192 1193 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1194 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1195 static dev_t sd_make_device(dev_info_t *devi); 1196 1197 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1198 uint64_t capacity); 1199 1200 /* 1201 * Driver entry point functions. 1202 */ 1203 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1204 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1205 static int sd_ready_and_valid(struct sd_lun *un); 1206 1207 static void sdmin(struct buf *bp); 1208 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1209 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1210 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1211 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1212 1213 static int sdstrategy(struct buf *bp); 1214 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1215 1216 /* 1217 * Function prototypes for layering functions in the iostart chain. 1218 */ 1219 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1220 struct buf *bp); 1221 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1222 struct buf *bp); 1223 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1224 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1225 struct buf *bp); 1226 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1227 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1228 1229 /* 1230 * Function prototypes for layering functions in the iodone chain. 1231 */ 1232 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1233 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1234 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1235 struct buf *bp); 1236 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1237 struct buf *bp); 1238 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1239 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1240 struct buf *bp); 1241 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1242 1243 /* 1244 * Prototypes for functions to support buf(9S) based IO. 1245 */ 1246 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1247 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1248 static void sd_destroypkt_for_buf(struct buf *); 1249 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1250 struct buf *bp, int flags, 1251 int (*callback)(caddr_t), caddr_t callback_arg, 1252 diskaddr_t lba, uint32_t blockcount); 1253 #if defined(__i386) || defined(__amd64) 1254 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1255 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1256 #endif /* defined(__i386) || defined(__amd64) */ 1257 1258 /* 1259 * Prototypes for functions to support USCSI IO. 1260 */ 1261 static int sd_uscsi_strategy(struct buf *bp); 1262 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1263 static void sd_destroypkt_for_uscsi(struct buf *); 1264 1265 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1266 uchar_t chain_type, void *pktinfop); 1267 1268 static int sd_pm_entry(struct sd_lun *un); 1269 static void sd_pm_exit(struct sd_lun *un); 1270 1271 static void sd_pm_idletimeout_handler(void *arg); 1272 1273 /* 1274 * sd_core internal functions (used at the sd_core_io layer). 1275 */ 1276 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1277 static void sdintr(struct scsi_pkt *pktp); 1278 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1279 1280 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1281 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1282 int path_flag); 1283 1284 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1285 daddr_t blkno, int (*func)(struct buf *)); 1286 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1287 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1288 static void sd_bioclone_free(struct buf *bp); 1289 static void sd_shadow_buf_free(struct buf *bp); 1290 1291 static void sd_print_transport_rejected_message(struct sd_lun *un, 1292 struct sd_xbuf *xp, int code); 1293 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1294 void *arg, int code); 1295 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1296 void *arg, int code); 1297 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1298 void *arg, int code); 1299 1300 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1301 int retry_check_flag, 1302 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1303 int c), 1304 void *user_arg, int failure_code, clock_t retry_delay, 1305 void (*statp)(kstat_io_t *)); 1306 1307 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1308 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1309 1310 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1311 struct scsi_pkt *pktp); 1312 static void sd_start_retry_command(void *arg); 1313 static void sd_start_direct_priority_command(void *arg); 1314 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1315 int errcode); 1316 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1317 struct buf *bp, int errcode); 1318 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1319 static void sd_sync_with_callback(struct sd_lun *un); 1320 static int sdrunout(caddr_t arg); 1321 1322 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1323 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1324 1325 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1326 static void sd_restore_throttle(void *arg); 1327 1328 static void sd_init_cdb_limits(struct sd_lun *un); 1329 1330 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1331 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1332 1333 /* 1334 * Error handling functions 1335 */ 1336 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1337 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1338 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1339 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1340 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1341 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1342 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1343 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1344 1345 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1346 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1347 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1348 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1349 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1350 struct sd_xbuf *xp); 1351 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1352 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1353 1354 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1355 void *arg, int code); 1356 static diskaddr_t sd_extract_sense_info_descr( 1357 struct scsi_descr_sense_hdr *sdsp); 1358 1359 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1360 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1361 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1362 uint8_t asc, 1363 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1364 static void sd_sense_key_not_ready(struct sd_lun *un, 1365 uint8_t asc, uint8_t ascq, 1366 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1367 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1368 int sense_key, uint8_t asc, 1369 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1370 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1371 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1372 static void sd_sense_key_unit_attention(struct sd_lun *un, 1373 uint8_t asc, 1374 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1375 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1376 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1377 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1378 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1379 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1380 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1381 static void sd_sense_key_default(struct sd_lun *un, 1382 int sense_key, 1383 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1384 1385 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1386 void *arg, int flag); 1387 1388 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1389 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1390 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1391 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1392 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1393 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1394 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1395 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1396 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1397 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1398 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1399 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1400 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1401 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1402 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1403 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1404 1405 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1406 1407 static void sd_start_stop_unit_callback(void *arg); 1408 static void sd_start_stop_unit_task(void *arg); 1409 1410 static void sd_taskq_create(void); 1411 static void sd_taskq_delete(void); 1412 static void sd_media_change_task(void *arg); 1413 1414 static int sd_handle_mchange(struct sd_lun *un); 1415 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1416 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1417 uint32_t *lbap, int path_flag); 1418 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1419 uint32_t *lbap, int path_flag); 1420 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1421 int path_flag); 1422 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1423 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1424 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1425 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1426 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1427 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1428 uchar_t usr_cmd, uchar_t *usr_bufp); 1429 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1430 struct dk_callback *dkc); 1431 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1432 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1433 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1434 uchar_t *bufaddr, uint_t buflen); 1435 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1436 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1437 uchar_t *bufaddr, uint_t buflen, char feature); 1438 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1439 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1440 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1441 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1442 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1443 size_t buflen, daddr_t start_block, int path_flag); 1444 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1445 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1446 path_flag) 1447 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1448 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1449 path_flag) 1450 1451 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1452 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1453 uint16_t param_ptr, int path_flag); 1454 1455 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1456 static void sd_free_rqs(struct sd_lun *un); 1457 1458 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1459 uchar_t *data, int len, int fmt); 1460 static void sd_panic_for_res_conflict(struct sd_lun *un); 1461 1462 /* 1463 * Disk Ioctl Function Prototypes 1464 */ 1465 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1466 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1467 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1468 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1469 int geom_validated); 1470 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1471 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1472 int geom_validated); 1473 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1474 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1475 int geom_validated); 1476 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1477 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1478 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1479 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1480 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1481 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1482 static int sd_write_label(dev_t dev); 1483 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1484 static void sd_clear_vtoc(struct sd_lun *un); 1485 static void sd_clear_efi(struct sd_lun *un); 1486 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1487 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1488 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1489 static void sd_setup_default_geometry(struct sd_lun *un); 1490 #if defined(__i386) || defined(__amd64) 1491 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1492 #endif 1493 1494 /* 1495 * Multi-host Ioctl Prototypes 1496 */ 1497 static int sd_check_mhd(dev_t dev, int interval); 1498 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1499 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1500 static char *sd_sname(uchar_t status); 1501 static void sd_mhd_resvd_recover(void *arg); 1502 static void sd_resv_reclaim_thread(); 1503 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1504 static int sd_reserve_release(dev_t dev, int cmd); 1505 static void sd_rmv_resv_reclaim_req(dev_t dev); 1506 static void sd_mhd_reset_notify_cb(caddr_t arg); 1507 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1508 mhioc_inkeys_t *usrp, int flag); 1509 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1510 mhioc_inresvs_t *usrp, int flag); 1511 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1512 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1513 static int sd_mhdioc_release(dev_t dev); 1514 static int sd_mhdioc_register_devid(dev_t dev); 1515 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1516 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1517 1518 /* 1519 * SCSI removable prototypes 1520 */ 1521 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1522 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1523 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1524 static int sr_pause_resume(dev_t dev, int mode); 1525 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1526 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1527 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1528 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1529 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1530 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1531 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1532 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1533 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1534 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1535 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1536 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1537 static int sr_eject(dev_t dev); 1538 static void sr_ejected(register struct sd_lun *un); 1539 static int sr_check_wp(dev_t dev); 1540 static int sd_check_media(dev_t dev, enum dkio_state state); 1541 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1542 static void sd_delayed_cv_broadcast(void *arg); 1543 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1544 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1545 1546 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1547 1548 /* 1549 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1550 */ 1551 static void sd_check_for_writable_cd(struct sd_lun *un); 1552 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1553 static void sd_wm_cache_destructor(void *wm, void *un); 1554 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1555 daddr_t endb, ushort_t typ); 1556 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1557 daddr_t endb); 1558 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1559 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1560 static void sd_read_modify_write_task(void * arg); 1561 static int 1562 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1563 struct buf **bpp); 1564 1565 1566 /* 1567 * Function prototypes for failfast support. 1568 */ 1569 static void sd_failfast_flushq(struct sd_lun *un); 1570 static int sd_failfast_flushq_callback(struct buf *bp); 1571 1572 /* 1573 * Function prototypes to check for lsi devices 1574 */ 1575 static void sd_is_lsi(struct sd_lun *un); 1576 1577 /* 1578 * Function prototypes for x86 support 1579 */ 1580 #if defined(__i386) || defined(__amd64) 1581 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1582 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1583 #endif 1584 1585 /* 1586 * Constants for failfast support: 1587 * 1588 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1589 * failfast processing being performed. 1590 * 1591 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1592 * failfast processing on all bufs with B_FAILFAST set. 1593 */ 1594 1595 #define SD_FAILFAST_INACTIVE 0 1596 #define SD_FAILFAST_ACTIVE 1 1597 1598 /* 1599 * Bitmask to control behavior of buf(9S) flushes when a transition to 1600 * the failfast state occurs. Optional bits include: 1601 * 1602 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1603 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1604 * be flushed. 1605 * 1606 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1607 * driver, in addition to the regular wait queue. This includes the xbuf 1608 * queues. When clear, only the driver's wait queue will be flushed. 1609 */ 1610 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1611 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1612 1613 /* 1614 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1615 * to flush all queues within the driver. 1616 */ 1617 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1618 1619 1620 /* 1621 * SD Testing Fault Injection 1622 */ 1623 #ifdef SD_FAULT_INJECTION 1624 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1625 static void sd_faultinjection(struct scsi_pkt *pktp); 1626 static void sd_injection_log(char *buf, struct sd_lun *un); 1627 #endif 1628 1629 /* 1630 * Device driver ops vector 1631 */ 1632 static struct cb_ops sd_cb_ops = { 1633 sdopen, /* open */ 1634 sdclose, /* close */ 1635 sdstrategy, /* strategy */ 1636 nodev, /* print */ 1637 sddump, /* dump */ 1638 sdread, /* read */ 1639 sdwrite, /* write */ 1640 sdioctl, /* ioctl */ 1641 nodev, /* devmap */ 1642 nodev, /* mmap */ 1643 nodev, /* segmap */ 1644 nochpoll, /* poll */ 1645 sd_prop_op, /* cb_prop_op */ 1646 0, /* streamtab */ 1647 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1648 CB_REV, /* cb_rev */ 1649 sdaread, /* async I/O read entry point */ 1650 sdawrite /* async I/O write entry point */ 1651 }; 1652 1653 static struct dev_ops sd_ops = { 1654 DEVO_REV, /* devo_rev, */ 1655 0, /* refcnt */ 1656 sdinfo, /* info */ 1657 nulldev, /* identify */ 1658 sdprobe, /* probe */ 1659 sdattach, /* attach */ 1660 sddetach, /* detach */ 1661 nodev, /* reset */ 1662 &sd_cb_ops, /* driver operations */ 1663 NULL, /* bus operations */ 1664 sdpower /* power */ 1665 }; 1666 1667 1668 /* 1669 * This is the loadable module wrapper. 1670 */ 1671 #include <sys/modctl.h> 1672 1673 static struct modldrv modldrv = { 1674 &mod_driverops, /* Type of module. This one is a driver */ 1675 SD_MODULE_NAME, /* Module name. */ 1676 &sd_ops /* driver ops */ 1677 }; 1678 1679 1680 static struct modlinkage modlinkage = { 1681 MODREV_1, 1682 &modldrv, 1683 NULL 1684 }; 1685 1686 1687 static struct scsi_asq_key_strings sd_additional_codes[] = { 1688 0x81, 0, "Logical Unit is Reserved", 1689 0x85, 0, "Audio Address Not Valid", 1690 0xb6, 0, "Media Load Mechanism Failed", 1691 0xB9, 0, "Audio Play Operation Aborted", 1692 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1693 0x53, 2, "Medium removal prevented", 1694 0x6f, 0, "Authentication failed during key exchange", 1695 0x6f, 1, "Key not present", 1696 0x6f, 2, "Key not established", 1697 0x6f, 3, "Read without proper authentication", 1698 0x6f, 4, "Mismatched region to this logical unit", 1699 0x6f, 5, "Region reset count error", 1700 0xffff, 0x0, NULL 1701 }; 1702 1703 1704 /* 1705 * Struct for passing printing information for sense data messages 1706 */ 1707 struct sd_sense_info { 1708 int ssi_severity; 1709 int ssi_pfa_flag; 1710 }; 1711 1712 /* 1713 * Table of function pointers for iostart-side routines. Seperate "chains" 1714 * of layered function calls are formed by placing the function pointers 1715 * sequentially in the desired order. Functions are called according to an 1716 * incrementing table index ordering. The last function in each chain must 1717 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1718 * in the sd_iodone_chain[] array. 1719 * 1720 * Note: It may seem more natural to organize both the iostart and iodone 1721 * functions together, into an array of structures (or some similar 1722 * organization) with a common index, rather than two seperate arrays which 1723 * must be maintained in synchronization. The purpose of this division is 1724 * to achiece improved performance: individual arrays allows for more 1725 * effective cache line utilization on certain platforms. 1726 */ 1727 1728 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1729 1730 1731 static sd_chain_t sd_iostart_chain[] = { 1732 1733 /* Chain for buf IO for disk drive targets (PM enabled) */ 1734 sd_mapblockaddr_iostart, /* Index: 0 */ 1735 sd_pm_iostart, /* Index: 1 */ 1736 sd_core_iostart, /* Index: 2 */ 1737 1738 /* Chain for buf IO for disk drive targets (PM disabled) */ 1739 sd_mapblockaddr_iostart, /* Index: 3 */ 1740 sd_core_iostart, /* Index: 4 */ 1741 1742 /* Chain for buf IO for removable-media targets (PM enabled) */ 1743 sd_mapblockaddr_iostart, /* Index: 5 */ 1744 sd_mapblocksize_iostart, /* Index: 6 */ 1745 sd_pm_iostart, /* Index: 7 */ 1746 sd_core_iostart, /* Index: 8 */ 1747 1748 /* Chain for buf IO for removable-media targets (PM disabled) */ 1749 sd_mapblockaddr_iostart, /* Index: 9 */ 1750 sd_mapblocksize_iostart, /* Index: 10 */ 1751 sd_core_iostart, /* Index: 11 */ 1752 1753 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1754 sd_mapblockaddr_iostart, /* Index: 12 */ 1755 sd_checksum_iostart, /* Index: 13 */ 1756 sd_pm_iostart, /* Index: 14 */ 1757 sd_core_iostart, /* Index: 15 */ 1758 1759 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1760 sd_mapblockaddr_iostart, /* Index: 16 */ 1761 sd_checksum_iostart, /* Index: 17 */ 1762 sd_core_iostart, /* Index: 18 */ 1763 1764 /* Chain for USCSI commands (all targets) */ 1765 sd_pm_iostart, /* Index: 19 */ 1766 sd_core_iostart, /* Index: 20 */ 1767 1768 /* Chain for checksumming USCSI commands (all targets) */ 1769 sd_checksum_uscsi_iostart, /* Index: 21 */ 1770 sd_pm_iostart, /* Index: 22 */ 1771 sd_core_iostart, /* Index: 23 */ 1772 1773 /* Chain for "direct" USCSI commands (all targets) */ 1774 sd_core_iostart, /* Index: 24 */ 1775 1776 /* Chain for "direct priority" USCSI commands (all targets) */ 1777 sd_core_iostart, /* Index: 25 */ 1778 }; 1779 1780 /* 1781 * Macros to locate the first function of each iostart chain in the 1782 * sd_iostart_chain[] array. These are located by the index in the array. 1783 */ 1784 #define SD_CHAIN_DISK_IOSTART 0 1785 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1786 #define SD_CHAIN_RMMEDIA_IOSTART 5 1787 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1788 #define SD_CHAIN_CHKSUM_IOSTART 12 1789 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1790 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1791 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1792 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1793 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1794 1795 1796 /* 1797 * Table of function pointers for the iodone-side routines for the driver- 1798 * internal layering mechanism. The calling sequence for iodone routines 1799 * uses a decrementing table index, so the last routine called in a chain 1800 * must be at the lowest array index location for that chain. The last 1801 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1802 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1803 * of the functions in an iodone side chain must correspond to the ordering 1804 * of the iostart routines for that chain. Note that there is no iodone 1805 * side routine that corresponds to sd_core_iostart(), so there is no 1806 * entry in the table for this. 1807 */ 1808 1809 static sd_chain_t sd_iodone_chain[] = { 1810 1811 /* Chain for buf IO for disk drive targets (PM enabled) */ 1812 sd_buf_iodone, /* Index: 0 */ 1813 sd_mapblockaddr_iodone, /* Index: 1 */ 1814 sd_pm_iodone, /* Index: 2 */ 1815 1816 /* Chain for buf IO for disk drive targets (PM disabled) */ 1817 sd_buf_iodone, /* Index: 3 */ 1818 sd_mapblockaddr_iodone, /* Index: 4 */ 1819 1820 /* Chain for buf IO for removable-media targets (PM enabled) */ 1821 sd_buf_iodone, /* Index: 5 */ 1822 sd_mapblockaddr_iodone, /* Index: 6 */ 1823 sd_mapblocksize_iodone, /* Index: 7 */ 1824 sd_pm_iodone, /* Index: 8 */ 1825 1826 /* Chain for buf IO for removable-media targets (PM disabled) */ 1827 sd_buf_iodone, /* Index: 9 */ 1828 sd_mapblockaddr_iodone, /* Index: 10 */ 1829 sd_mapblocksize_iodone, /* Index: 11 */ 1830 1831 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1832 sd_buf_iodone, /* Index: 12 */ 1833 sd_mapblockaddr_iodone, /* Index: 13 */ 1834 sd_checksum_iodone, /* Index: 14 */ 1835 sd_pm_iodone, /* Index: 15 */ 1836 1837 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1838 sd_buf_iodone, /* Index: 16 */ 1839 sd_mapblockaddr_iodone, /* Index: 17 */ 1840 sd_checksum_iodone, /* Index: 18 */ 1841 1842 /* Chain for USCSI commands (non-checksum targets) */ 1843 sd_uscsi_iodone, /* Index: 19 */ 1844 sd_pm_iodone, /* Index: 20 */ 1845 1846 /* Chain for USCSI commands (checksum targets) */ 1847 sd_uscsi_iodone, /* Index: 21 */ 1848 sd_checksum_uscsi_iodone, /* Index: 22 */ 1849 sd_pm_iodone, /* Index: 22 */ 1850 1851 /* Chain for "direct" USCSI commands (all targets) */ 1852 sd_uscsi_iodone, /* Index: 24 */ 1853 1854 /* Chain for "direct priority" USCSI commands (all targets) */ 1855 sd_uscsi_iodone, /* Index: 25 */ 1856 }; 1857 1858 1859 /* 1860 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1861 * each iodone-side chain. These are located by the array index, but as the 1862 * iodone side functions are called in a decrementing-index order, the 1863 * highest index number in each chain must be specified (as these correspond 1864 * to the first function in the iodone chain that will be called by the core 1865 * at IO completion time). 1866 */ 1867 1868 #define SD_CHAIN_DISK_IODONE 2 1869 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1870 #define SD_CHAIN_RMMEDIA_IODONE 8 1871 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1872 #define SD_CHAIN_CHKSUM_IODONE 15 1873 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1874 #define SD_CHAIN_USCSI_CMD_IODONE 20 1875 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1876 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1877 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1878 1879 1880 1881 1882 /* 1883 * Array to map a layering chain index to the appropriate initpkt routine. 1884 * The redundant entries are present so that the index used for accessing 1885 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1886 * with this table as well. 1887 */ 1888 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1889 1890 static sd_initpkt_t sd_initpkt_map[] = { 1891 1892 /* Chain for buf IO for disk drive targets (PM enabled) */ 1893 sd_initpkt_for_buf, /* Index: 0 */ 1894 sd_initpkt_for_buf, /* Index: 1 */ 1895 sd_initpkt_for_buf, /* Index: 2 */ 1896 1897 /* Chain for buf IO for disk drive targets (PM disabled) */ 1898 sd_initpkt_for_buf, /* Index: 3 */ 1899 sd_initpkt_for_buf, /* Index: 4 */ 1900 1901 /* Chain for buf IO for removable-media targets (PM enabled) */ 1902 sd_initpkt_for_buf, /* Index: 5 */ 1903 sd_initpkt_for_buf, /* Index: 6 */ 1904 sd_initpkt_for_buf, /* Index: 7 */ 1905 sd_initpkt_for_buf, /* Index: 8 */ 1906 1907 /* Chain for buf IO for removable-media targets (PM disabled) */ 1908 sd_initpkt_for_buf, /* Index: 9 */ 1909 sd_initpkt_for_buf, /* Index: 10 */ 1910 sd_initpkt_for_buf, /* Index: 11 */ 1911 1912 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1913 sd_initpkt_for_buf, /* Index: 12 */ 1914 sd_initpkt_for_buf, /* Index: 13 */ 1915 sd_initpkt_for_buf, /* Index: 14 */ 1916 sd_initpkt_for_buf, /* Index: 15 */ 1917 1918 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1919 sd_initpkt_for_buf, /* Index: 16 */ 1920 sd_initpkt_for_buf, /* Index: 17 */ 1921 sd_initpkt_for_buf, /* Index: 18 */ 1922 1923 /* Chain for USCSI commands (non-checksum targets) */ 1924 sd_initpkt_for_uscsi, /* Index: 19 */ 1925 sd_initpkt_for_uscsi, /* Index: 20 */ 1926 1927 /* Chain for USCSI commands (checksum targets) */ 1928 sd_initpkt_for_uscsi, /* Index: 21 */ 1929 sd_initpkt_for_uscsi, /* Index: 22 */ 1930 sd_initpkt_for_uscsi, /* Index: 22 */ 1931 1932 /* Chain for "direct" USCSI commands (all targets) */ 1933 sd_initpkt_for_uscsi, /* Index: 24 */ 1934 1935 /* Chain for "direct priority" USCSI commands (all targets) */ 1936 sd_initpkt_for_uscsi, /* Index: 25 */ 1937 1938 }; 1939 1940 1941 /* 1942 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1943 * The redundant entries are present so that the index used for accessing 1944 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1945 * with this table as well. 1946 */ 1947 typedef void (*sd_destroypkt_t)(struct buf *); 1948 1949 static sd_destroypkt_t sd_destroypkt_map[] = { 1950 1951 /* Chain for buf IO for disk drive targets (PM enabled) */ 1952 sd_destroypkt_for_buf, /* Index: 0 */ 1953 sd_destroypkt_for_buf, /* Index: 1 */ 1954 sd_destroypkt_for_buf, /* Index: 2 */ 1955 1956 /* Chain for buf IO for disk drive targets (PM disabled) */ 1957 sd_destroypkt_for_buf, /* Index: 3 */ 1958 sd_destroypkt_for_buf, /* Index: 4 */ 1959 1960 /* Chain for buf IO for removable-media targets (PM enabled) */ 1961 sd_destroypkt_for_buf, /* Index: 5 */ 1962 sd_destroypkt_for_buf, /* Index: 6 */ 1963 sd_destroypkt_for_buf, /* Index: 7 */ 1964 sd_destroypkt_for_buf, /* Index: 8 */ 1965 1966 /* Chain for buf IO for removable-media targets (PM disabled) */ 1967 sd_destroypkt_for_buf, /* Index: 9 */ 1968 sd_destroypkt_for_buf, /* Index: 10 */ 1969 sd_destroypkt_for_buf, /* Index: 11 */ 1970 1971 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1972 sd_destroypkt_for_buf, /* Index: 12 */ 1973 sd_destroypkt_for_buf, /* Index: 13 */ 1974 sd_destroypkt_for_buf, /* Index: 14 */ 1975 sd_destroypkt_for_buf, /* Index: 15 */ 1976 1977 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1978 sd_destroypkt_for_buf, /* Index: 16 */ 1979 sd_destroypkt_for_buf, /* Index: 17 */ 1980 sd_destroypkt_for_buf, /* Index: 18 */ 1981 1982 /* Chain for USCSI commands (non-checksum targets) */ 1983 sd_destroypkt_for_uscsi, /* Index: 19 */ 1984 sd_destroypkt_for_uscsi, /* Index: 20 */ 1985 1986 /* Chain for USCSI commands (checksum targets) */ 1987 sd_destroypkt_for_uscsi, /* Index: 21 */ 1988 sd_destroypkt_for_uscsi, /* Index: 22 */ 1989 sd_destroypkt_for_uscsi, /* Index: 22 */ 1990 1991 /* Chain for "direct" USCSI commands (all targets) */ 1992 sd_destroypkt_for_uscsi, /* Index: 24 */ 1993 1994 /* Chain for "direct priority" USCSI commands (all targets) */ 1995 sd_destroypkt_for_uscsi, /* Index: 25 */ 1996 1997 }; 1998 1999 2000 2001 /* 2002 * Array to map a layering chain index to the appropriate chain "type". 2003 * The chain type indicates a specific property/usage of the chain. 2004 * The redundant entries are present so that the index used for accessing 2005 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2006 * with this table as well. 2007 */ 2008 2009 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2010 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2011 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2012 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2013 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2014 /* (for error recovery) */ 2015 2016 static int sd_chain_type_map[] = { 2017 2018 /* Chain for buf IO for disk drive targets (PM enabled) */ 2019 SD_CHAIN_BUFIO, /* Index: 0 */ 2020 SD_CHAIN_BUFIO, /* Index: 1 */ 2021 SD_CHAIN_BUFIO, /* Index: 2 */ 2022 2023 /* Chain for buf IO for disk drive targets (PM disabled) */ 2024 SD_CHAIN_BUFIO, /* Index: 3 */ 2025 SD_CHAIN_BUFIO, /* Index: 4 */ 2026 2027 /* Chain for buf IO for removable-media targets (PM enabled) */ 2028 SD_CHAIN_BUFIO, /* Index: 5 */ 2029 SD_CHAIN_BUFIO, /* Index: 6 */ 2030 SD_CHAIN_BUFIO, /* Index: 7 */ 2031 SD_CHAIN_BUFIO, /* Index: 8 */ 2032 2033 /* Chain for buf IO for removable-media targets (PM disabled) */ 2034 SD_CHAIN_BUFIO, /* Index: 9 */ 2035 SD_CHAIN_BUFIO, /* Index: 10 */ 2036 SD_CHAIN_BUFIO, /* Index: 11 */ 2037 2038 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2039 SD_CHAIN_BUFIO, /* Index: 12 */ 2040 SD_CHAIN_BUFIO, /* Index: 13 */ 2041 SD_CHAIN_BUFIO, /* Index: 14 */ 2042 SD_CHAIN_BUFIO, /* Index: 15 */ 2043 2044 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2045 SD_CHAIN_BUFIO, /* Index: 16 */ 2046 SD_CHAIN_BUFIO, /* Index: 17 */ 2047 SD_CHAIN_BUFIO, /* Index: 18 */ 2048 2049 /* Chain for USCSI commands (non-checksum targets) */ 2050 SD_CHAIN_USCSI, /* Index: 19 */ 2051 SD_CHAIN_USCSI, /* Index: 20 */ 2052 2053 /* Chain for USCSI commands (checksum targets) */ 2054 SD_CHAIN_USCSI, /* Index: 21 */ 2055 SD_CHAIN_USCSI, /* Index: 22 */ 2056 SD_CHAIN_USCSI, /* Index: 22 */ 2057 2058 /* Chain for "direct" USCSI commands (all targets) */ 2059 SD_CHAIN_DIRECT, /* Index: 24 */ 2060 2061 /* Chain for "direct priority" USCSI commands (all targets) */ 2062 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2063 }; 2064 2065 2066 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2067 #define SD_IS_BUFIO(xp) \ 2068 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2069 2070 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2071 #define SD_IS_DIRECT_PRIORITY(xp) \ 2072 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2073 2074 2075 2076 /* 2077 * Struct, array, and macros to map a specific chain to the appropriate 2078 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2079 * 2080 * The sd_chain_index_map[] array is used at attach time to set the various 2081 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2082 * chain to be used with the instance. This allows different instances to use 2083 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2084 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2085 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2086 * dynamically & without the use of locking; and (2) a layer may update the 2087 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2088 * to allow for deferred processing of an IO within the same chain from a 2089 * different execution context. 2090 */ 2091 2092 struct sd_chain_index { 2093 int sci_iostart_index; 2094 int sci_iodone_index; 2095 }; 2096 2097 static struct sd_chain_index sd_chain_index_map[] = { 2098 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2099 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2100 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2101 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2102 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2103 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2104 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2105 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2106 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2107 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2108 }; 2109 2110 2111 /* 2112 * The following are indexes into the sd_chain_index_map[] array. 2113 */ 2114 2115 /* un->un_buf_chain_type must be set to one of these */ 2116 #define SD_CHAIN_INFO_DISK 0 2117 #define SD_CHAIN_INFO_DISK_NO_PM 1 2118 #define SD_CHAIN_INFO_RMMEDIA 2 2119 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2120 #define SD_CHAIN_INFO_CHKSUM 4 2121 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2122 2123 /* un->un_uscsi_chain_type must be set to one of these */ 2124 #define SD_CHAIN_INFO_USCSI_CMD 6 2125 /* USCSI with PM disabled is the same as DIRECT */ 2126 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2127 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2128 2129 /* un->un_direct_chain_type must be set to one of these */ 2130 #define SD_CHAIN_INFO_DIRECT_CMD 8 2131 2132 /* un->un_priority_chain_type must be set to one of these */ 2133 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2134 2135 /* size for devid inquiries */ 2136 #define MAX_INQUIRY_SIZE 0xF0 2137 2138 /* 2139 * Macros used by functions to pass a given buf(9S) struct along to the 2140 * next function in the layering chain for further processing. 2141 * 2142 * In the following macros, passing more than three arguments to the called 2143 * routines causes the optimizer for the SPARC compiler to stop doing tail 2144 * call elimination which results in significant performance degradation. 2145 */ 2146 #define SD_BEGIN_IOSTART(index, un, bp) \ 2147 ((*(sd_iostart_chain[index]))(index, un, bp)) 2148 2149 #define SD_BEGIN_IODONE(index, un, bp) \ 2150 ((*(sd_iodone_chain[index]))(index, un, bp)) 2151 2152 #define SD_NEXT_IOSTART(index, un, bp) \ 2153 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2154 2155 #define SD_NEXT_IODONE(index, un, bp) \ 2156 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2157 2158 /* 2159 * Function: _init 2160 * 2161 * Description: This is the driver _init(9E) entry point. 2162 * 2163 * Return Code: Returns the value from mod_install(9F) or 2164 * ddi_soft_state_init(9F) as appropriate. 2165 * 2166 * Context: Called when driver module loaded. 2167 */ 2168 2169 int 2170 _init(void) 2171 { 2172 int err; 2173 2174 /* establish driver name from module name */ 2175 sd_label = mod_modname(&modlinkage); 2176 2177 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2178 SD_MAXUNIT); 2179 2180 if (err != 0) { 2181 return (err); 2182 } 2183 2184 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2185 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2186 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2187 2188 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2189 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2190 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2191 2192 /* 2193 * it's ok to init here even for fibre device 2194 */ 2195 sd_scsi_probe_cache_init(); 2196 2197 /* 2198 * Creating taskq before mod_install ensures that all callers (threads) 2199 * that enter the module after a successfull mod_install encounter 2200 * a valid taskq. 2201 */ 2202 sd_taskq_create(); 2203 2204 err = mod_install(&modlinkage); 2205 if (err != 0) { 2206 /* delete taskq if install fails */ 2207 sd_taskq_delete(); 2208 2209 mutex_destroy(&sd_detach_mutex); 2210 mutex_destroy(&sd_log_mutex); 2211 mutex_destroy(&sd_label_mutex); 2212 2213 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2214 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2215 cv_destroy(&sd_tr.srq_inprocess_cv); 2216 2217 sd_scsi_probe_cache_fini(); 2218 2219 ddi_soft_state_fini(&sd_state); 2220 return (err); 2221 } 2222 2223 return (err); 2224 } 2225 2226 2227 /* 2228 * Function: _fini 2229 * 2230 * Description: This is the driver _fini(9E) entry point. 2231 * 2232 * Return Code: Returns the value from mod_remove(9F) 2233 * 2234 * Context: Called when driver module is unloaded. 2235 */ 2236 2237 int 2238 _fini(void) 2239 { 2240 int err; 2241 2242 if ((err = mod_remove(&modlinkage)) != 0) { 2243 return (err); 2244 } 2245 2246 sd_taskq_delete(); 2247 2248 mutex_destroy(&sd_detach_mutex); 2249 mutex_destroy(&sd_log_mutex); 2250 mutex_destroy(&sd_label_mutex); 2251 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2252 2253 sd_scsi_probe_cache_fini(); 2254 2255 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2256 cv_destroy(&sd_tr.srq_inprocess_cv); 2257 2258 ddi_soft_state_fini(&sd_state); 2259 2260 return (err); 2261 } 2262 2263 2264 /* 2265 * Function: _info 2266 * 2267 * Description: This is the driver _info(9E) entry point. 2268 * 2269 * Arguments: modinfop - pointer to the driver modinfo structure 2270 * 2271 * Return Code: Returns the value from mod_info(9F). 2272 * 2273 * Context: Kernel thread context 2274 */ 2275 2276 int 2277 _info(struct modinfo *modinfop) 2278 { 2279 return (mod_info(&modlinkage, modinfop)); 2280 } 2281 2282 2283 /* 2284 * The following routines implement the driver message logging facility. 2285 * They provide component- and level- based debug output filtering. 2286 * Output may also be restricted to messages for a single instance by 2287 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2288 * to NULL, then messages for all instances are printed. 2289 * 2290 * These routines have been cloned from each other due to the language 2291 * constraints of macros and variable argument list processing. 2292 */ 2293 2294 2295 /* 2296 * Function: sd_log_err 2297 * 2298 * Description: This routine is called by the SD_ERROR macro for debug 2299 * logging of error conditions. 2300 * 2301 * Arguments: comp - driver component being logged 2302 * dev - pointer to driver info structure 2303 * fmt - error string and format to be logged 2304 */ 2305 2306 static void 2307 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2308 { 2309 va_list ap; 2310 dev_info_t *dev; 2311 2312 ASSERT(un != NULL); 2313 dev = SD_DEVINFO(un); 2314 ASSERT(dev != NULL); 2315 2316 /* 2317 * Filter messages based on the global component and level masks. 2318 * Also print if un matches the value of sd_debug_un, or if 2319 * sd_debug_un is set to NULL. 2320 */ 2321 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2322 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2323 mutex_enter(&sd_log_mutex); 2324 va_start(ap, fmt); 2325 (void) vsprintf(sd_log_buf, fmt, ap); 2326 va_end(ap); 2327 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2328 mutex_exit(&sd_log_mutex); 2329 } 2330 #ifdef SD_FAULT_INJECTION 2331 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2332 if (un->sd_injection_mask & comp) { 2333 mutex_enter(&sd_log_mutex); 2334 va_start(ap, fmt); 2335 (void) vsprintf(sd_log_buf, fmt, ap); 2336 va_end(ap); 2337 sd_injection_log(sd_log_buf, un); 2338 mutex_exit(&sd_log_mutex); 2339 } 2340 #endif 2341 } 2342 2343 2344 /* 2345 * Function: sd_log_info 2346 * 2347 * Description: This routine is called by the SD_INFO macro for debug 2348 * logging of general purpose informational conditions. 2349 * 2350 * Arguments: comp - driver component being logged 2351 * dev - pointer to driver info structure 2352 * fmt - info string and format to be logged 2353 */ 2354 2355 static void 2356 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2357 { 2358 va_list ap; 2359 dev_info_t *dev; 2360 2361 ASSERT(un != NULL); 2362 dev = SD_DEVINFO(un); 2363 ASSERT(dev != NULL); 2364 2365 /* 2366 * Filter messages based on the global component and level masks. 2367 * Also print if un matches the value of sd_debug_un, or if 2368 * sd_debug_un is set to NULL. 2369 */ 2370 if ((sd_component_mask & component) && 2371 (sd_level_mask & SD_LOGMASK_INFO) && 2372 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2373 mutex_enter(&sd_log_mutex); 2374 va_start(ap, fmt); 2375 (void) vsprintf(sd_log_buf, fmt, ap); 2376 va_end(ap); 2377 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2378 mutex_exit(&sd_log_mutex); 2379 } 2380 #ifdef SD_FAULT_INJECTION 2381 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2382 if (un->sd_injection_mask & component) { 2383 mutex_enter(&sd_log_mutex); 2384 va_start(ap, fmt); 2385 (void) vsprintf(sd_log_buf, fmt, ap); 2386 va_end(ap); 2387 sd_injection_log(sd_log_buf, un); 2388 mutex_exit(&sd_log_mutex); 2389 } 2390 #endif 2391 } 2392 2393 2394 /* 2395 * Function: sd_log_trace 2396 * 2397 * Description: This routine is called by the SD_TRACE macro for debug 2398 * logging of trace conditions (i.e. function entry/exit). 2399 * 2400 * Arguments: comp - driver component being logged 2401 * dev - pointer to driver info structure 2402 * fmt - trace string and format to be logged 2403 */ 2404 2405 static void 2406 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2407 { 2408 va_list ap; 2409 dev_info_t *dev; 2410 2411 ASSERT(un != NULL); 2412 dev = SD_DEVINFO(un); 2413 ASSERT(dev != NULL); 2414 2415 /* 2416 * Filter messages based on the global component and level masks. 2417 * Also print if un matches the value of sd_debug_un, or if 2418 * sd_debug_un is set to NULL. 2419 */ 2420 if ((sd_component_mask & component) && 2421 (sd_level_mask & SD_LOGMASK_TRACE) && 2422 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2423 mutex_enter(&sd_log_mutex); 2424 va_start(ap, fmt); 2425 (void) vsprintf(sd_log_buf, fmt, ap); 2426 va_end(ap); 2427 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2428 mutex_exit(&sd_log_mutex); 2429 } 2430 #ifdef SD_FAULT_INJECTION 2431 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2432 if (un->sd_injection_mask & component) { 2433 mutex_enter(&sd_log_mutex); 2434 va_start(ap, fmt); 2435 (void) vsprintf(sd_log_buf, fmt, ap); 2436 va_end(ap); 2437 sd_injection_log(sd_log_buf, un); 2438 mutex_exit(&sd_log_mutex); 2439 } 2440 #endif 2441 } 2442 2443 2444 /* 2445 * Function: sdprobe 2446 * 2447 * Description: This is the driver probe(9e) entry point function. 2448 * 2449 * Arguments: devi - opaque device info handle 2450 * 2451 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2452 * DDI_PROBE_FAILURE: If the probe failed. 2453 * DDI_PROBE_PARTIAL: If the instance is not present now, 2454 * but may be present in the future. 2455 */ 2456 2457 static int 2458 sdprobe(dev_info_t *devi) 2459 { 2460 struct scsi_device *devp; 2461 int rval; 2462 int instance; 2463 2464 /* 2465 * if it wasn't for pln, sdprobe could actually be nulldev 2466 * in the "__fibre" case. 2467 */ 2468 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2469 return (DDI_PROBE_DONTCARE); 2470 } 2471 2472 devp = ddi_get_driver_private(devi); 2473 2474 if (devp == NULL) { 2475 /* Ooops... nexus driver is mis-configured... */ 2476 return (DDI_PROBE_FAILURE); 2477 } 2478 2479 instance = ddi_get_instance(devi); 2480 2481 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2482 return (DDI_PROBE_PARTIAL); 2483 } 2484 2485 /* 2486 * Call the SCSA utility probe routine to see if we actually 2487 * have a target at this SCSI nexus. 2488 */ 2489 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2490 case SCSIPROBE_EXISTS: 2491 switch (devp->sd_inq->inq_dtype) { 2492 case DTYPE_DIRECT: 2493 rval = DDI_PROBE_SUCCESS; 2494 break; 2495 case DTYPE_RODIRECT: 2496 /* CDs etc. Can be removable media */ 2497 rval = DDI_PROBE_SUCCESS; 2498 break; 2499 case DTYPE_OPTICAL: 2500 /* 2501 * Rewritable optical driver HP115AA 2502 * Can also be removable media 2503 */ 2504 2505 /* 2506 * Do not attempt to bind to DTYPE_OPTICAL if 2507 * pre solaris 9 sparc sd behavior is required 2508 * 2509 * If first time through and sd_dtype_optical_bind 2510 * has not been set in /etc/system check properties 2511 */ 2512 2513 if (sd_dtype_optical_bind < 0) { 2514 sd_dtype_optical_bind = ddi_prop_get_int 2515 (DDI_DEV_T_ANY, devi, 0, 2516 "optical-device-bind", 1); 2517 } 2518 2519 if (sd_dtype_optical_bind == 0) { 2520 rval = DDI_PROBE_FAILURE; 2521 } else { 2522 rval = DDI_PROBE_SUCCESS; 2523 } 2524 break; 2525 2526 case DTYPE_NOTPRESENT: 2527 default: 2528 rval = DDI_PROBE_FAILURE; 2529 break; 2530 } 2531 break; 2532 default: 2533 rval = DDI_PROBE_PARTIAL; 2534 break; 2535 } 2536 2537 /* 2538 * This routine checks for resource allocation prior to freeing, 2539 * so it will take care of the "smart probing" case where a 2540 * scsi_probe() may or may not have been issued and will *not* 2541 * free previously-freed resources. 2542 */ 2543 scsi_unprobe(devp); 2544 return (rval); 2545 } 2546 2547 2548 /* 2549 * Function: sdinfo 2550 * 2551 * Description: This is the driver getinfo(9e) entry point function. 2552 * Given the device number, return the devinfo pointer from 2553 * the scsi_device structure or the instance number 2554 * associated with the dev_t. 2555 * 2556 * Arguments: dip - pointer to device info structure 2557 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2558 * DDI_INFO_DEVT2INSTANCE) 2559 * arg - driver dev_t 2560 * resultp - user buffer for request response 2561 * 2562 * Return Code: DDI_SUCCESS 2563 * DDI_FAILURE 2564 */ 2565 /* ARGSUSED */ 2566 static int 2567 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2568 { 2569 struct sd_lun *un; 2570 dev_t dev; 2571 int instance; 2572 int error; 2573 2574 switch (infocmd) { 2575 case DDI_INFO_DEVT2DEVINFO: 2576 dev = (dev_t)arg; 2577 instance = SDUNIT(dev); 2578 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2579 return (DDI_FAILURE); 2580 } 2581 *result = (void *) SD_DEVINFO(un); 2582 error = DDI_SUCCESS; 2583 break; 2584 case DDI_INFO_DEVT2INSTANCE: 2585 dev = (dev_t)arg; 2586 instance = SDUNIT(dev); 2587 *result = (void *)(uintptr_t)instance; 2588 error = DDI_SUCCESS; 2589 break; 2590 default: 2591 error = DDI_FAILURE; 2592 } 2593 return (error); 2594 } 2595 2596 /* 2597 * Function: sd_prop_op 2598 * 2599 * Description: This is the driver prop_op(9e) entry point function. 2600 * Return the number of blocks for the partition in question 2601 * or forward the request to the property facilities. 2602 * 2603 * Arguments: dev - device number 2604 * dip - pointer to device info structure 2605 * prop_op - property operator 2606 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2607 * name - pointer to property name 2608 * valuep - pointer or address of the user buffer 2609 * lengthp - property length 2610 * 2611 * Return Code: DDI_PROP_SUCCESS 2612 * DDI_PROP_NOT_FOUND 2613 * DDI_PROP_UNDEFINED 2614 * DDI_PROP_NO_MEMORY 2615 * DDI_PROP_BUF_TOO_SMALL 2616 */ 2617 2618 static int 2619 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2620 char *name, caddr_t valuep, int *lengthp) 2621 { 2622 int instance = ddi_get_instance(dip); 2623 struct sd_lun *un; 2624 uint64_t nblocks64; 2625 2626 /* 2627 * Our dynamic properties are all device specific and size oriented. 2628 * Requests issued under conditions where size is valid are passed 2629 * to ddi_prop_op_nblocks with the size information, otherwise the 2630 * request is passed to ddi_prop_op. Size depends on valid geometry. 2631 */ 2632 un = ddi_get_soft_state(sd_state, instance); 2633 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2634 (un->un_f_geometry_is_valid == FALSE)) { 2635 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2636 name, valuep, lengthp)); 2637 } else { 2638 /* get nblocks value */ 2639 ASSERT(!mutex_owned(SD_MUTEX(un))); 2640 mutex_enter(SD_MUTEX(un)); 2641 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2642 mutex_exit(SD_MUTEX(un)); 2643 2644 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2645 name, valuep, lengthp, nblocks64)); 2646 } 2647 } 2648 2649 /* 2650 * The following functions are for smart probing: 2651 * sd_scsi_probe_cache_init() 2652 * sd_scsi_probe_cache_fini() 2653 * sd_scsi_clear_probe_cache() 2654 * sd_scsi_probe_with_cache() 2655 */ 2656 2657 /* 2658 * Function: sd_scsi_probe_cache_init 2659 * 2660 * Description: Initializes the probe response cache mutex and head pointer. 2661 * 2662 * Context: Kernel thread context 2663 */ 2664 2665 static void 2666 sd_scsi_probe_cache_init(void) 2667 { 2668 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2669 sd_scsi_probe_cache_head = NULL; 2670 } 2671 2672 2673 /* 2674 * Function: sd_scsi_probe_cache_fini 2675 * 2676 * Description: Frees all resources associated with the probe response cache. 2677 * 2678 * Context: Kernel thread context 2679 */ 2680 2681 static void 2682 sd_scsi_probe_cache_fini(void) 2683 { 2684 struct sd_scsi_probe_cache *cp; 2685 struct sd_scsi_probe_cache *ncp; 2686 2687 /* Clean up our smart probing linked list */ 2688 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2689 ncp = cp->next; 2690 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2691 } 2692 sd_scsi_probe_cache_head = NULL; 2693 mutex_destroy(&sd_scsi_probe_cache_mutex); 2694 } 2695 2696 2697 /* 2698 * Function: sd_scsi_clear_probe_cache 2699 * 2700 * Description: This routine clears the probe response cache. This is 2701 * done when open() returns ENXIO so that when deferred 2702 * attach is attempted (possibly after a device has been 2703 * turned on) we will retry the probe. Since we don't know 2704 * which target we failed to open, we just clear the 2705 * entire cache. 2706 * 2707 * Context: Kernel thread context 2708 */ 2709 2710 static void 2711 sd_scsi_clear_probe_cache(void) 2712 { 2713 struct sd_scsi_probe_cache *cp; 2714 int i; 2715 2716 mutex_enter(&sd_scsi_probe_cache_mutex); 2717 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2718 /* 2719 * Reset all entries to SCSIPROBE_EXISTS. This will 2720 * force probing to be performed the next time 2721 * sd_scsi_probe_with_cache is called. 2722 */ 2723 for (i = 0; i < NTARGETS_WIDE; i++) { 2724 cp->cache[i] = SCSIPROBE_EXISTS; 2725 } 2726 } 2727 mutex_exit(&sd_scsi_probe_cache_mutex); 2728 } 2729 2730 2731 /* 2732 * Function: sd_scsi_probe_with_cache 2733 * 2734 * Description: This routine implements support for a scsi device probe 2735 * with cache. The driver maintains a cache of the target 2736 * responses to scsi probes. If we get no response from a 2737 * target during a probe inquiry, we remember that, and we 2738 * avoid additional calls to scsi_probe on non-zero LUNs 2739 * on the same target until the cache is cleared. By doing 2740 * so we avoid the 1/4 sec selection timeout for nonzero 2741 * LUNs. lun0 of a target is always probed. 2742 * 2743 * Arguments: devp - Pointer to a scsi_device(9S) structure 2744 * waitfunc - indicates what the allocator routines should 2745 * do when resources are not available. This value 2746 * is passed on to scsi_probe() when that routine 2747 * is called. 2748 * 2749 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2750 * otherwise the value returned by scsi_probe(9F). 2751 * 2752 * Context: Kernel thread context 2753 */ 2754 2755 static int 2756 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2757 { 2758 struct sd_scsi_probe_cache *cp; 2759 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2760 int lun, tgt; 2761 2762 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2763 SCSI_ADDR_PROP_LUN, 0); 2764 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2765 SCSI_ADDR_PROP_TARGET, -1); 2766 2767 /* Make sure caching enabled and target in range */ 2768 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2769 /* do it the old way (no cache) */ 2770 return (scsi_probe(devp, waitfn)); 2771 } 2772 2773 mutex_enter(&sd_scsi_probe_cache_mutex); 2774 2775 /* Find the cache for this scsi bus instance */ 2776 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2777 if (cp->pdip == pdip) { 2778 break; 2779 } 2780 } 2781 2782 /* If we can't find a cache for this pdip, create one */ 2783 if (cp == NULL) { 2784 int i; 2785 2786 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2787 KM_SLEEP); 2788 cp->pdip = pdip; 2789 cp->next = sd_scsi_probe_cache_head; 2790 sd_scsi_probe_cache_head = cp; 2791 for (i = 0; i < NTARGETS_WIDE; i++) { 2792 cp->cache[i] = SCSIPROBE_EXISTS; 2793 } 2794 } 2795 2796 mutex_exit(&sd_scsi_probe_cache_mutex); 2797 2798 /* Recompute the cache for this target if LUN zero */ 2799 if (lun == 0) { 2800 cp->cache[tgt] = SCSIPROBE_EXISTS; 2801 } 2802 2803 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2804 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2805 return (SCSIPROBE_NORESP); 2806 } 2807 2808 /* Do the actual probe; save & return the result */ 2809 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2810 } 2811 2812 2813 /* 2814 * Function: sd_spin_up_unit 2815 * 2816 * Description: Issues the following commands to spin-up the device: 2817 * START STOP UNIT, and INQUIRY. 2818 * 2819 * Arguments: un - driver soft state (unit) structure 2820 * 2821 * Return Code: 0 - success 2822 * EIO - failure 2823 * EACCES - reservation conflict 2824 * 2825 * Context: Kernel thread context 2826 */ 2827 2828 static int 2829 sd_spin_up_unit(struct sd_lun *un) 2830 { 2831 size_t resid = 0; 2832 int has_conflict = FALSE; 2833 uchar_t *bufaddr; 2834 2835 ASSERT(un != NULL); 2836 2837 /* 2838 * Send a throwaway START UNIT command. 2839 * 2840 * If we fail on this, we don't care presently what precisely 2841 * is wrong. EMC's arrays will also fail this with a check 2842 * condition (0x2/0x4/0x3) if the device is "inactive," but 2843 * we don't want to fail the attach because it may become 2844 * "active" later. 2845 */ 2846 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2847 == EACCES) 2848 has_conflict = TRUE; 2849 2850 /* 2851 * Send another INQUIRY command to the target. This is necessary for 2852 * non-removable media direct access devices because their INQUIRY data 2853 * may not be fully qualified until they are spun up (perhaps via the 2854 * START command above). Note: This seems to be needed for some 2855 * legacy devices only.) The INQUIRY command should succeed even if a 2856 * Reservation Conflict is present. 2857 */ 2858 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2859 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2860 kmem_free(bufaddr, SUN_INQSIZE); 2861 return (EIO); 2862 } 2863 2864 /* 2865 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2866 * Note that this routine does not return a failure here even if the 2867 * INQUIRY command did not return any data. This is a legacy behavior. 2868 */ 2869 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2870 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2871 } 2872 2873 kmem_free(bufaddr, SUN_INQSIZE); 2874 2875 /* If we hit a reservation conflict above, tell the caller. */ 2876 if (has_conflict == TRUE) { 2877 return (EACCES); 2878 } 2879 2880 return (0); 2881 } 2882 2883 #ifdef _LP64 2884 /* 2885 * Function: sd_enable_descr_sense 2886 * 2887 * Description: This routine attempts to select descriptor sense format 2888 * using the Control mode page. Devices that support 64 bit 2889 * LBAs (for >2TB luns) should also implement descriptor 2890 * sense data so we will call this function whenever we see 2891 * a lun larger than 2TB. If for some reason the device 2892 * supports 64 bit LBAs but doesn't support descriptor sense 2893 * presumably the mode select will fail. Everything will 2894 * continue to work normally except that we will not get 2895 * complete sense data for commands that fail with an LBA 2896 * larger than 32 bits. 2897 * 2898 * Arguments: un - driver soft state (unit) structure 2899 * 2900 * Context: Kernel thread context only 2901 */ 2902 2903 static void 2904 sd_enable_descr_sense(struct sd_lun *un) 2905 { 2906 uchar_t *header; 2907 struct mode_control_scsi3 *ctrl_bufp; 2908 size_t buflen; 2909 size_t bd_len; 2910 2911 /* 2912 * Read MODE SENSE page 0xA, Control Mode Page 2913 */ 2914 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2915 sizeof (struct mode_control_scsi3); 2916 header = kmem_zalloc(buflen, KM_SLEEP); 2917 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2918 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2919 SD_ERROR(SD_LOG_COMMON, un, 2920 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2921 goto eds_exit; 2922 } 2923 2924 /* 2925 * Determine size of Block Descriptors in order to locate 2926 * the mode page data. ATAPI devices return 0, SCSI devices 2927 * should return MODE_BLK_DESC_LENGTH. 2928 */ 2929 bd_len = ((struct mode_header *)header)->bdesc_length; 2930 2931 ctrl_bufp = (struct mode_control_scsi3 *) 2932 (header + MODE_HEADER_LENGTH + bd_len); 2933 2934 /* 2935 * Clear PS bit for MODE SELECT 2936 */ 2937 ctrl_bufp->mode_page.ps = 0; 2938 2939 /* 2940 * Set D_SENSE to enable descriptor sense format. 2941 */ 2942 ctrl_bufp->d_sense = 1; 2943 2944 /* 2945 * Use MODE SELECT to commit the change to the D_SENSE bit 2946 */ 2947 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2948 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2949 SD_INFO(SD_LOG_COMMON, un, 2950 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2951 goto eds_exit; 2952 } 2953 2954 eds_exit: 2955 kmem_free(header, buflen); 2956 } 2957 #endif /* _LP64 */ 2958 2959 2960 /* 2961 * Function: sd_set_mmc_caps 2962 * 2963 * Description: This routine determines if the device is MMC compliant and if 2964 * the device supports CDDA via a mode sense of the CDVD 2965 * capabilities mode page. Also checks if the device is a 2966 * dvdram writable device. 2967 * 2968 * Arguments: un - driver soft state (unit) structure 2969 * 2970 * Context: Kernel thread context only 2971 */ 2972 2973 static void 2974 sd_set_mmc_caps(struct sd_lun *un) 2975 { 2976 struct mode_header_grp2 *sense_mhp; 2977 uchar_t *sense_page; 2978 caddr_t buf; 2979 int bd_len; 2980 int status; 2981 struct uscsi_cmd com; 2982 int rtn; 2983 uchar_t *out_data_rw, *out_data_hd; 2984 uchar_t *rqbuf_rw, *rqbuf_hd; 2985 2986 ASSERT(un != NULL); 2987 2988 /* 2989 * The flags which will be set in this function are - mmc compliant, 2990 * dvdram writable device, cdda support. Initialize them to FALSE 2991 * and if a capability is detected - it will be set to TRUE. 2992 */ 2993 un->un_f_mmc_cap = FALSE; 2994 un->un_f_dvdram_writable_device = FALSE; 2995 un->un_f_cfg_cdda = FALSE; 2996 2997 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 2998 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 2999 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3000 3001 if (status != 0) { 3002 /* command failed; just return */ 3003 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3004 return; 3005 } 3006 /* 3007 * If the mode sense request for the CDROM CAPABILITIES 3008 * page (0x2A) succeeds the device is assumed to be MMC. 3009 */ 3010 un->un_f_mmc_cap = TRUE; 3011 3012 /* Get to the page data */ 3013 sense_mhp = (struct mode_header_grp2 *)buf; 3014 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3015 sense_mhp->bdesc_length_lo; 3016 if (bd_len > MODE_BLK_DESC_LENGTH) { 3017 /* 3018 * We did not get back the expected block descriptor 3019 * length so we cannot determine if the device supports 3020 * CDDA. However, we still indicate the device is MMC 3021 * according to the successful response to the page 3022 * 0x2A mode sense request. 3023 */ 3024 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3025 "sd_set_mmc_caps: Mode Sense returned " 3026 "invalid block descriptor length\n"); 3027 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3028 return; 3029 } 3030 3031 /* See if read CDDA is supported */ 3032 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3033 bd_len); 3034 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3035 3036 /* See if writing DVD RAM is supported. */ 3037 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3038 if (un->un_f_dvdram_writable_device == TRUE) { 3039 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3040 return; 3041 } 3042 3043 /* 3044 * If the device presents DVD or CD capabilities in the mode 3045 * page, we can return here since a RRD will not have 3046 * these capabilities. 3047 */ 3048 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3049 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3050 return; 3051 } 3052 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3053 3054 /* 3055 * If un->un_f_dvdram_writable_device is still FALSE, 3056 * check for a Removable Rigid Disk (RRD). A RRD 3057 * device is identified by the features RANDOM_WRITABLE and 3058 * HARDWARE_DEFECT_MANAGEMENT. 3059 */ 3060 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3061 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3062 3063 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3064 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3065 RANDOM_WRITABLE); 3066 if (rtn != 0) { 3067 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3068 kmem_free(rqbuf_rw, SENSE_LENGTH); 3069 return; 3070 } 3071 3072 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3073 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3074 3075 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3076 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3077 HARDWARE_DEFECT_MANAGEMENT); 3078 if (rtn == 0) { 3079 /* 3080 * We have good information, check for random writable 3081 * and hardware defect features. 3082 */ 3083 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3084 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3085 un->un_f_dvdram_writable_device = TRUE; 3086 } 3087 } 3088 3089 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3090 kmem_free(rqbuf_rw, SENSE_LENGTH); 3091 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3092 kmem_free(rqbuf_hd, SENSE_LENGTH); 3093 } 3094 3095 /* 3096 * Function: sd_check_for_writable_cd 3097 * 3098 * Description: This routine determines if the media in the device is 3099 * writable or not. It uses the get configuration command (0x46) 3100 * to determine if the media is writable 3101 * 3102 * Arguments: un - driver soft state (unit) structure 3103 * 3104 * Context: Never called at interrupt context. 3105 */ 3106 3107 static void 3108 sd_check_for_writable_cd(struct sd_lun *un) 3109 { 3110 struct uscsi_cmd com; 3111 uchar_t *out_data; 3112 uchar_t *rqbuf; 3113 int rtn; 3114 uchar_t *out_data_rw, *out_data_hd; 3115 uchar_t *rqbuf_rw, *rqbuf_hd; 3116 struct mode_header_grp2 *sense_mhp; 3117 uchar_t *sense_page; 3118 caddr_t buf; 3119 int bd_len; 3120 int status; 3121 3122 ASSERT(un != NULL); 3123 ASSERT(mutex_owned(SD_MUTEX(un))); 3124 3125 /* 3126 * Initialize the writable media to false, if configuration info. 3127 * tells us otherwise then only we will set it. 3128 */ 3129 un->un_f_mmc_writable_media = FALSE; 3130 mutex_exit(SD_MUTEX(un)); 3131 3132 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3133 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3134 3135 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3136 out_data, SD_PROFILE_HEADER_LEN); 3137 3138 mutex_enter(SD_MUTEX(un)); 3139 if (rtn == 0) { 3140 /* 3141 * We have good information, check for writable DVD. 3142 */ 3143 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3144 un->un_f_mmc_writable_media = TRUE; 3145 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3146 kmem_free(rqbuf, SENSE_LENGTH); 3147 return; 3148 } 3149 } 3150 3151 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3152 kmem_free(rqbuf, SENSE_LENGTH); 3153 3154 /* 3155 * Determine if this is a RRD type device. 3156 */ 3157 mutex_exit(SD_MUTEX(un)); 3158 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3159 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3160 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3161 mutex_enter(SD_MUTEX(un)); 3162 if (status != 0) { 3163 /* command failed; just return */ 3164 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3165 return; 3166 } 3167 3168 /* Get to the page data */ 3169 sense_mhp = (struct mode_header_grp2 *)buf; 3170 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3171 if (bd_len > MODE_BLK_DESC_LENGTH) { 3172 /* 3173 * We did not get back the expected block descriptor length so 3174 * we cannot check the mode page. 3175 */ 3176 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3177 "sd_check_for_writable_cd: Mode Sense returned " 3178 "invalid block descriptor length\n"); 3179 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3180 return; 3181 } 3182 3183 /* 3184 * If the device presents DVD or CD capabilities in the mode 3185 * page, we can return here since a RRD device will not have 3186 * these capabilities. 3187 */ 3188 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3189 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3190 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3191 return; 3192 } 3193 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3194 3195 /* 3196 * If un->un_f_mmc_writable_media is still FALSE, 3197 * check for RRD type media. A RRD device is identified 3198 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3199 */ 3200 mutex_exit(SD_MUTEX(un)); 3201 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3202 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3203 3204 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3205 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3206 RANDOM_WRITABLE); 3207 if (rtn != 0) { 3208 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3209 kmem_free(rqbuf_rw, SENSE_LENGTH); 3210 mutex_enter(SD_MUTEX(un)); 3211 return; 3212 } 3213 3214 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3215 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3216 3217 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3218 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3219 HARDWARE_DEFECT_MANAGEMENT); 3220 mutex_enter(SD_MUTEX(un)); 3221 if (rtn == 0) { 3222 /* 3223 * We have good information, check for random writable 3224 * and hardware defect features as current. 3225 */ 3226 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3227 (out_data_rw[10] & 0x1) && 3228 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3229 (out_data_hd[10] & 0x1)) { 3230 un->un_f_mmc_writable_media = TRUE; 3231 } 3232 } 3233 3234 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3235 kmem_free(rqbuf_rw, SENSE_LENGTH); 3236 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3237 kmem_free(rqbuf_hd, SENSE_LENGTH); 3238 } 3239 3240 /* 3241 * Function: sd_read_unit_properties 3242 * 3243 * Description: The following implements a property lookup mechanism. 3244 * Properties for particular disks (keyed on vendor, model 3245 * and rev numbers) are sought in the sd.conf file via 3246 * sd_process_sdconf_file(), and if not found there, are 3247 * looked for in a list hardcoded in this driver via 3248 * sd_process_sdconf_table() Once located the properties 3249 * are used to update the driver unit structure. 3250 * 3251 * Arguments: un - driver soft state (unit) structure 3252 */ 3253 3254 static void 3255 sd_read_unit_properties(struct sd_lun *un) 3256 { 3257 /* 3258 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3259 * the "sd-config-list" property (from the sd.conf file) or if 3260 * there was not a match for the inquiry vid/pid. If this event 3261 * occurs the static driver configuration table is searched for 3262 * a match. 3263 */ 3264 ASSERT(un != NULL); 3265 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3266 sd_process_sdconf_table(un); 3267 } 3268 3269 /* check for LSI device */ 3270 sd_is_lsi(un); 3271 3272 3273 } 3274 3275 3276 /* 3277 * Function: sd_process_sdconf_file 3278 * 3279 * Description: Use ddi_getlongprop to obtain the properties from the 3280 * driver's config file (ie, sd.conf) and update the driver 3281 * soft state structure accordingly. 3282 * 3283 * Arguments: un - driver soft state (unit) structure 3284 * 3285 * Return Code: SD_SUCCESS - The properties were successfully set according 3286 * to the driver configuration file. 3287 * SD_FAILURE - The driver config list was not obtained or 3288 * there was no vid/pid match. This indicates that 3289 * the static config table should be used. 3290 * 3291 * The config file has a property, "sd-config-list", which consists of 3292 * one or more duplets as follows: 3293 * 3294 * sd-config-list= 3295 * <duplet>, 3296 * [<duplet>,] 3297 * [<duplet>]; 3298 * 3299 * The structure of each duplet is as follows: 3300 * 3301 * <duplet>:= <vid+pid>,<data-property-name_list> 3302 * 3303 * The first entry of the duplet is the device ID string (the concatenated 3304 * vid & pid; not to be confused with a device_id). This is defined in 3305 * the same way as in the sd_disk_table. 3306 * 3307 * The second part of the duplet is a string that identifies a 3308 * data-property-name-list. The data-property-name-list is defined as 3309 * follows: 3310 * 3311 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3312 * 3313 * The syntax of <data-property-name> depends on the <version> field. 3314 * 3315 * If version = SD_CONF_VERSION_1 we have the following syntax: 3316 * 3317 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3318 * 3319 * where the prop0 value will be used to set prop0 if bit0 set in the 3320 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3321 * 3322 */ 3323 3324 static int 3325 sd_process_sdconf_file(struct sd_lun *un) 3326 { 3327 char *config_list = NULL; 3328 int config_list_len; 3329 int len; 3330 int dupletlen = 0; 3331 char *vidptr; 3332 int vidlen; 3333 char *dnlist_ptr; 3334 char *dataname_ptr; 3335 int dnlist_len; 3336 int dataname_len; 3337 int *data_list; 3338 int data_list_len; 3339 int rval = SD_FAILURE; 3340 int i; 3341 3342 ASSERT(un != NULL); 3343 3344 /* Obtain the configuration list associated with the .conf file */ 3345 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3346 sd_config_list, (caddr_t)&config_list, &config_list_len) 3347 != DDI_PROP_SUCCESS) { 3348 return (SD_FAILURE); 3349 } 3350 3351 /* 3352 * Compare vids in each duplet to the inquiry vid - if a match is 3353 * made, get the data value and update the soft state structure 3354 * accordingly. 3355 * 3356 * Note: This algorithm is complex and difficult to maintain. It should 3357 * be replaced with a more robust implementation. 3358 */ 3359 for (len = config_list_len, vidptr = config_list; len > 0; 3360 vidptr += dupletlen, len -= dupletlen) { 3361 /* 3362 * Note: The assumption here is that each vid entry is on 3363 * a unique line from its associated duplet. 3364 */ 3365 vidlen = dupletlen = (int)strlen(vidptr); 3366 if ((vidlen == 0) || 3367 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3368 dupletlen++; 3369 continue; 3370 } 3371 3372 /* 3373 * dnlist contains 1 or more blank separated 3374 * data-property-name entries 3375 */ 3376 dnlist_ptr = vidptr + vidlen + 1; 3377 dnlist_len = (int)strlen(dnlist_ptr); 3378 dupletlen += dnlist_len + 2; 3379 3380 /* 3381 * Set a pointer for the first data-property-name 3382 * entry in the list 3383 */ 3384 dataname_ptr = dnlist_ptr; 3385 dataname_len = 0; 3386 3387 /* 3388 * Loop through all data-property-name entries in the 3389 * data-property-name-list setting the properties for each. 3390 */ 3391 while (dataname_len < dnlist_len) { 3392 int version; 3393 3394 /* 3395 * Determine the length of the current 3396 * data-property-name entry by indexing until a 3397 * blank or NULL is encountered. When the space is 3398 * encountered reset it to a NULL for compliance 3399 * with ddi_getlongprop(). 3400 */ 3401 for (i = 0; ((dataname_ptr[i] != ' ') && 3402 (dataname_ptr[i] != '\0')); i++) { 3403 ; 3404 } 3405 3406 dataname_len += i; 3407 /* If not null terminated, Make it so */ 3408 if (dataname_ptr[i] == ' ') { 3409 dataname_ptr[i] = '\0'; 3410 } 3411 dataname_len++; 3412 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3413 "sd_process_sdconf_file: disk:%s, data:%s\n", 3414 vidptr, dataname_ptr); 3415 3416 /* Get the data list */ 3417 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3418 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3419 != DDI_PROP_SUCCESS) { 3420 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3421 "sd_process_sdconf_file: data property (%s)" 3422 " has no value\n", dataname_ptr); 3423 dataname_ptr = dnlist_ptr + dataname_len; 3424 continue; 3425 } 3426 3427 version = data_list[0]; 3428 3429 if (version == SD_CONF_VERSION_1) { 3430 sd_tunables values; 3431 3432 /* Set the properties */ 3433 if (sd_chk_vers1_data(un, data_list[1], 3434 &data_list[2], data_list_len, dataname_ptr) 3435 == SD_SUCCESS) { 3436 sd_get_tunables_from_conf(un, 3437 data_list[1], &data_list[2], 3438 &values); 3439 sd_set_vers1_properties(un, 3440 data_list[1], &values); 3441 rval = SD_SUCCESS; 3442 } else { 3443 rval = SD_FAILURE; 3444 } 3445 } else { 3446 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3447 "data property %s version 0x%x is invalid.", 3448 dataname_ptr, version); 3449 rval = SD_FAILURE; 3450 } 3451 kmem_free(data_list, data_list_len); 3452 dataname_ptr = dnlist_ptr + dataname_len; 3453 } 3454 } 3455 3456 /* free up the memory allocated by ddi_getlongprop */ 3457 if (config_list) { 3458 kmem_free(config_list, config_list_len); 3459 } 3460 3461 return (rval); 3462 } 3463 3464 /* 3465 * Function: sd_get_tunables_from_conf() 3466 * 3467 * 3468 * This function reads the data list from the sd.conf file and pulls 3469 * the values that can have numeric values as arguments and places 3470 * the values in the apropriate sd_tunables member. 3471 * Since the order of the data list members varies across platforms 3472 * This function reads them from the data list in a platform specific 3473 * order and places them into the correct sd_tunable member that is 3474 * a consistant across all platforms. 3475 */ 3476 static void 3477 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3478 sd_tunables *values) 3479 { 3480 int i; 3481 int mask; 3482 3483 bzero(values, sizeof (sd_tunables)); 3484 3485 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3486 3487 mask = 1 << i; 3488 if (mask > flags) { 3489 break; 3490 } 3491 3492 switch (mask & flags) { 3493 case 0: /* This mask bit not set in flags */ 3494 continue; 3495 case SD_CONF_BSET_THROTTLE: 3496 values->sdt_throttle = data_list[i]; 3497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3498 "sd_get_tunables_from_conf: throttle = %d\n", 3499 values->sdt_throttle); 3500 break; 3501 case SD_CONF_BSET_CTYPE: 3502 values->sdt_ctype = data_list[i]; 3503 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3504 "sd_get_tunables_from_conf: ctype = %d\n", 3505 values->sdt_ctype); 3506 break; 3507 case SD_CONF_BSET_NRR_COUNT: 3508 values->sdt_not_rdy_retries = data_list[i]; 3509 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3510 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3511 values->sdt_not_rdy_retries); 3512 break; 3513 case SD_CONF_BSET_BSY_RETRY_COUNT: 3514 values->sdt_busy_retries = data_list[i]; 3515 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3516 "sd_get_tunables_from_conf: busy_retries = %d\n", 3517 values->sdt_busy_retries); 3518 break; 3519 case SD_CONF_BSET_RST_RETRIES: 3520 values->sdt_reset_retries = data_list[i]; 3521 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3522 "sd_get_tunables_from_conf: reset_retries = %d\n", 3523 values->sdt_reset_retries); 3524 break; 3525 case SD_CONF_BSET_RSV_REL_TIME: 3526 values->sdt_reserv_rel_time = data_list[i]; 3527 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3528 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3529 values->sdt_reserv_rel_time); 3530 break; 3531 case SD_CONF_BSET_MIN_THROTTLE: 3532 values->sdt_min_throttle = data_list[i]; 3533 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3534 "sd_get_tunables_from_conf: min_throttle = %d\n", 3535 values->sdt_min_throttle); 3536 break; 3537 case SD_CONF_BSET_DISKSORT_DISABLED: 3538 values->sdt_disk_sort_dis = data_list[i]; 3539 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3540 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3541 values->sdt_disk_sort_dis); 3542 break; 3543 case SD_CONF_BSET_LUN_RESET_ENABLED: 3544 values->sdt_lun_reset_enable = data_list[i]; 3545 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3546 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3547 "\n", values->sdt_lun_reset_enable); 3548 break; 3549 } 3550 } 3551 } 3552 3553 /* 3554 * Function: sd_process_sdconf_table 3555 * 3556 * Description: Search the static configuration table for a match on the 3557 * inquiry vid/pid and update the driver soft state structure 3558 * according to the table property values for the device. 3559 * 3560 * The form of a configuration table entry is: 3561 * <vid+pid>,<flags>,<property-data> 3562 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3563 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3564 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3565 * 3566 * Arguments: un - driver soft state (unit) structure 3567 */ 3568 3569 static void 3570 sd_process_sdconf_table(struct sd_lun *un) 3571 { 3572 char *id = NULL; 3573 int table_index; 3574 int idlen; 3575 3576 ASSERT(un != NULL); 3577 for (table_index = 0; table_index < sd_disk_table_size; 3578 table_index++) { 3579 id = sd_disk_table[table_index].device_id; 3580 idlen = strlen(id); 3581 if (idlen == 0) { 3582 continue; 3583 } 3584 3585 /* 3586 * The static configuration table currently does not 3587 * implement version 10 properties. Additionally, 3588 * multiple data-property-name entries are not 3589 * implemented in the static configuration table. 3590 */ 3591 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3592 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3593 "sd_process_sdconf_table: disk %s\n", id); 3594 sd_set_vers1_properties(un, 3595 sd_disk_table[table_index].flags, 3596 sd_disk_table[table_index].properties); 3597 break; 3598 } 3599 } 3600 } 3601 3602 3603 /* 3604 * Function: sd_sdconf_id_match 3605 * 3606 * Description: This local function implements a case sensitive vid/pid 3607 * comparison as well as the boundary cases of wild card and 3608 * multiple blanks. 3609 * 3610 * Note: An implicit assumption made here is that the scsi 3611 * inquiry structure will always keep the vid, pid and 3612 * revision strings in consecutive sequence, so they can be 3613 * read as a single string. If this assumption is not the 3614 * case, a separate string, to be used for the check, needs 3615 * to be built with these strings concatenated. 3616 * 3617 * Arguments: un - driver soft state (unit) structure 3618 * id - table or config file vid/pid 3619 * idlen - length of the vid/pid (bytes) 3620 * 3621 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3622 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3623 */ 3624 3625 static int 3626 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3627 { 3628 struct scsi_inquiry *sd_inq; 3629 int rval = SD_SUCCESS; 3630 3631 ASSERT(un != NULL); 3632 sd_inq = un->un_sd->sd_inq; 3633 ASSERT(id != NULL); 3634 3635 /* 3636 * We use the inq_vid as a pointer to a buffer containing the 3637 * vid and pid and use the entire vid/pid length of the table 3638 * entry for the comparison. This works because the inq_pid 3639 * data member follows inq_vid in the scsi_inquiry structure. 3640 */ 3641 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3642 /* 3643 * The user id string is compared to the inquiry vid/pid 3644 * using a case insensitive comparison and ignoring 3645 * multiple spaces. 3646 */ 3647 rval = sd_blank_cmp(un, id, idlen); 3648 if (rval != SD_SUCCESS) { 3649 /* 3650 * User id strings that start and end with a "*" 3651 * are a special case. These do not have a 3652 * specific vendor, and the product string can 3653 * appear anywhere in the 16 byte PID portion of 3654 * the inquiry data. This is a simple strstr() 3655 * type search for the user id in the inquiry data. 3656 */ 3657 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3658 char *pidptr = &id[1]; 3659 int i; 3660 int j; 3661 int pidstrlen = idlen - 2; 3662 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3663 pidstrlen; 3664 3665 if (j < 0) { 3666 return (SD_FAILURE); 3667 } 3668 for (i = 0; i < j; i++) { 3669 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3670 pidptr, pidstrlen) == 0) { 3671 rval = SD_SUCCESS; 3672 break; 3673 } 3674 } 3675 } 3676 } 3677 } 3678 return (rval); 3679 } 3680 3681 3682 /* 3683 * Function: sd_blank_cmp 3684 * 3685 * Description: If the id string starts and ends with a space, treat 3686 * multiple consecutive spaces as equivalent to a single 3687 * space. For example, this causes a sd_disk_table entry 3688 * of " NEC CDROM " to match a device's id string of 3689 * "NEC CDROM". 3690 * 3691 * Note: The success exit condition for this routine is if 3692 * the pointer to the table entry is '\0' and the cnt of 3693 * the inquiry length is zero. This will happen if the inquiry 3694 * string returned by the device is padded with spaces to be 3695 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3696 * SCSI spec states that the inquiry string is to be padded with 3697 * spaces. 3698 * 3699 * Arguments: un - driver soft state (unit) structure 3700 * id - table or config file vid/pid 3701 * idlen - length of the vid/pid (bytes) 3702 * 3703 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3704 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3705 */ 3706 3707 static int 3708 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3709 { 3710 char *p1; 3711 char *p2; 3712 int cnt; 3713 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3714 sizeof (SD_INQUIRY(un)->inq_pid); 3715 3716 ASSERT(un != NULL); 3717 p2 = un->un_sd->sd_inq->inq_vid; 3718 ASSERT(id != NULL); 3719 p1 = id; 3720 3721 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3722 /* 3723 * Note: string p1 is terminated by a NUL but string p2 3724 * isn't. The end of p2 is determined by cnt. 3725 */ 3726 for (;;) { 3727 /* skip over any extra blanks in both strings */ 3728 while ((*p1 != '\0') && (*p1 == ' ')) { 3729 p1++; 3730 } 3731 while ((cnt != 0) && (*p2 == ' ')) { 3732 p2++; 3733 cnt--; 3734 } 3735 3736 /* compare the two strings */ 3737 if ((cnt == 0) || 3738 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3739 break; 3740 } 3741 while ((cnt > 0) && 3742 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3743 p1++; 3744 p2++; 3745 cnt--; 3746 } 3747 } 3748 } 3749 3750 /* return SD_SUCCESS if both strings match */ 3751 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3752 } 3753 3754 3755 /* 3756 * Function: sd_chk_vers1_data 3757 * 3758 * Description: Verify the version 1 device properties provided by the 3759 * user via the configuration file 3760 * 3761 * Arguments: un - driver soft state (unit) structure 3762 * flags - integer mask indicating properties to be set 3763 * prop_list - integer list of property values 3764 * list_len - length of user provided data 3765 * 3766 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3767 * SD_FAILURE - Indicates the user provided data is invalid 3768 */ 3769 3770 static int 3771 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3772 int list_len, char *dataname_ptr) 3773 { 3774 int i; 3775 int mask = 1; 3776 int index = 0; 3777 3778 ASSERT(un != NULL); 3779 3780 /* Check for a NULL property name and list */ 3781 if (dataname_ptr == NULL) { 3782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3783 "sd_chk_vers1_data: NULL data property name."); 3784 return (SD_FAILURE); 3785 } 3786 if (prop_list == NULL) { 3787 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3788 "sd_chk_vers1_data: %s NULL data property list.", 3789 dataname_ptr); 3790 return (SD_FAILURE); 3791 } 3792 3793 /* Display a warning if undefined bits are set in the flags */ 3794 if (flags & ~SD_CONF_BIT_MASK) { 3795 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3796 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3797 "Properties not set.", 3798 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3799 return (SD_FAILURE); 3800 } 3801 3802 /* 3803 * Verify the length of the list by identifying the highest bit set 3804 * in the flags and validating that the property list has a length 3805 * up to the index of this bit. 3806 */ 3807 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3808 if (flags & mask) { 3809 index++; 3810 } 3811 mask = 1 << i; 3812 } 3813 if ((list_len / sizeof (int)) < (index + 2)) { 3814 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3815 "sd_chk_vers1_data: " 3816 "Data property list %s size is incorrect. " 3817 "Properties not set.", dataname_ptr); 3818 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3819 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3820 return (SD_FAILURE); 3821 } 3822 return (SD_SUCCESS); 3823 } 3824 3825 3826 /* 3827 * Function: sd_set_vers1_properties 3828 * 3829 * Description: Set version 1 device properties based on a property list 3830 * retrieved from the driver configuration file or static 3831 * configuration table. Version 1 properties have the format: 3832 * 3833 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3834 * 3835 * where the prop0 value will be used to set prop0 if bit0 3836 * is set in the flags 3837 * 3838 * Arguments: un - driver soft state (unit) structure 3839 * flags - integer mask indicating properties to be set 3840 * prop_list - integer list of property values 3841 */ 3842 3843 static void 3844 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3845 { 3846 ASSERT(un != NULL); 3847 3848 /* 3849 * Set the flag to indicate cache is to be disabled. An attempt 3850 * to disable the cache via sd_cache_control() will be made 3851 * later during attach once the basic initialization is complete. 3852 */ 3853 if (flags & SD_CONF_BSET_NOCACHE) { 3854 un->un_f_opt_disable_cache = TRUE; 3855 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3856 "sd_set_vers1_properties: caching disabled flag set\n"); 3857 } 3858 3859 /* CD-specific configuration parameters */ 3860 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3861 un->un_f_cfg_playmsf_bcd = TRUE; 3862 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3863 "sd_set_vers1_properties: playmsf_bcd set\n"); 3864 } 3865 if (flags & SD_CONF_BSET_READSUB_BCD) { 3866 un->un_f_cfg_readsub_bcd = TRUE; 3867 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3868 "sd_set_vers1_properties: readsub_bcd set\n"); 3869 } 3870 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3871 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3872 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3873 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3874 } 3875 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3876 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3877 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3878 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3879 } 3880 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3881 un->un_f_cfg_no_read_header = TRUE; 3882 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3883 "sd_set_vers1_properties: no_read_header set\n"); 3884 } 3885 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3886 un->un_f_cfg_read_cd_xd4 = TRUE; 3887 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3888 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3889 } 3890 3891 /* Support for devices which do not have valid/unique serial numbers */ 3892 if (flags & SD_CONF_BSET_FAB_DEVID) { 3893 un->un_f_opt_fab_devid = TRUE; 3894 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3895 "sd_set_vers1_properties: fab_devid bit set\n"); 3896 } 3897 3898 /* Support for user throttle configuration */ 3899 if (flags & SD_CONF_BSET_THROTTLE) { 3900 ASSERT(prop_list != NULL); 3901 un->un_saved_throttle = un->un_throttle = 3902 prop_list->sdt_throttle; 3903 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3904 "sd_set_vers1_properties: throttle set to %d\n", 3905 prop_list->sdt_throttle); 3906 } 3907 3908 /* Set the per disk retry count according to the conf file or table. */ 3909 if (flags & SD_CONF_BSET_NRR_COUNT) { 3910 ASSERT(prop_list != NULL); 3911 if (prop_list->sdt_not_rdy_retries) { 3912 un->un_notready_retry_count = 3913 prop_list->sdt_not_rdy_retries; 3914 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3915 "sd_set_vers1_properties: not ready retry count" 3916 " set to %d\n", un->un_notready_retry_count); 3917 } 3918 } 3919 3920 /* The controller type is reported for generic disk driver ioctls */ 3921 if (flags & SD_CONF_BSET_CTYPE) { 3922 ASSERT(prop_list != NULL); 3923 switch (prop_list->sdt_ctype) { 3924 case CTYPE_CDROM: 3925 un->un_ctype = prop_list->sdt_ctype; 3926 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3927 "sd_set_vers1_properties: ctype set to " 3928 "CTYPE_CDROM\n"); 3929 break; 3930 case CTYPE_CCS: 3931 un->un_ctype = prop_list->sdt_ctype; 3932 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3933 "sd_set_vers1_properties: ctype set to " 3934 "CTYPE_CCS\n"); 3935 break; 3936 case CTYPE_ROD: /* RW optical */ 3937 un->un_ctype = prop_list->sdt_ctype; 3938 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3939 "sd_set_vers1_properties: ctype set to " 3940 "CTYPE_ROD\n"); 3941 break; 3942 default: 3943 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3944 "sd_set_vers1_properties: Could not set " 3945 "invalid ctype value (%d)", 3946 prop_list->sdt_ctype); 3947 } 3948 } 3949 3950 /* Purple failover timeout */ 3951 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3952 ASSERT(prop_list != NULL); 3953 un->un_busy_retry_count = 3954 prop_list->sdt_busy_retries; 3955 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3956 "sd_set_vers1_properties: " 3957 "busy retry count set to %d\n", 3958 un->un_busy_retry_count); 3959 } 3960 3961 /* Purple reset retry count */ 3962 if (flags & SD_CONF_BSET_RST_RETRIES) { 3963 ASSERT(prop_list != NULL); 3964 un->un_reset_retry_count = 3965 prop_list->sdt_reset_retries; 3966 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3967 "sd_set_vers1_properties: " 3968 "reset retry count set to %d\n", 3969 un->un_reset_retry_count); 3970 } 3971 3972 /* Purple reservation release timeout */ 3973 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 3974 ASSERT(prop_list != NULL); 3975 un->un_reserve_release_time = 3976 prop_list->sdt_reserv_rel_time; 3977 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3978 "sd_set_vers1_properties: " 3979 "reservation release timeout set to %d\n", 3980 un->un_reserve_release_time); 3981 } 3982 3983 /* 3984 * Driver flag telling the driver to verify that no commands are pending 3985 * for a device before issuing a Test Unit Ready. This is a workaround 3986 * for a firmware bug in some Seagate eliteI drives. 3987 */ 3988 if (flags & SD_CONF_BSET_TUR_CHECK) { 3989 un->un_f_cfg_tur_check = TRUE; 3990 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3991 "sd_set_vers1_properties: tur queue check set\n"); 3992 } 3993 3994 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 3995 un->un_min_throttle = prop_list->sdt_min_throttle; 3996 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3997 "sd_set_vers1_properties: min throttle set to %d\n", 3998 un->un_min_throttle); 3999 } 4000 4001 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4002 un->un_f_disksort_disabled = 4003 (prop_list->sdt_disk_sort_dis != 0) ? 4004 TRUE : FALSE; 4005 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4006 "sd_set_vers1_properties: disksort disabled " 4007 "flag set to %d\n", 4008 prop_list->sdt_disk_sort_dis); 4009 } 4010 4011 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4012 un->un_f_lun_reset_enabled = 4013 (prop_list->sdt_lun_reset_enable != 0) ? 4014 TRUE : FALSE; 4015 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4016 "sd_set_vers1_properties: lun reset enabled " 4017 "flag set to %d\n", 4018 prop_list->sdt_lun_reset_enable); 4019 } 4020 4021 /* 4022 * Validate the throttle values. 4023 * If any of the numbers are invalid, set everything to defaults. 4024 */ 4025 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4026 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4027 (un->un_min_throttle > un->un_throttle)) { 4028 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4029 un->un_min_throttle = sd_min_throttle; 4030 } 4031 } 4032 4033 /* 4034 * Function: sd_is_lsi() 4035 * 4036 * Description: Check for lsi devices, step throught the static device 4037 * table to match vid/pid. 4038 * 4039 * Args: un - ptr to sd_lun 4040 * 4041 * Notes: When creating new LSI property, need to add the new LSI property 4042 * to this function. 4043 */ 4044 static void 4045 sd_is_lsi(struct sd_lun *un) 4046 { 4047 char *id = NULL; 4048 int table_index; 4049 int idlen; 4050 void *prop; 4051 4052 ASSERT(un != NULL); 4053 for (table_index = 0; table_index < sd_disk_table_size; 4054 table_index++) { 4055 id = sd_disk_table[table_index].device_id; 4056 idlen = strlen(id); 4057 if (idlen == 0) { 4058 continue; 4059 } 4060 4061 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4062 prop = sd_disk_table[table_index].properties; 4063 if (prop == &lsi_properties || 4064 prop == &lsi_oem_properties || 4065 prop == &lsi_properties_scsi || 4066 prop == &symbios_properties) { 4067 un->un_f_cfg_is_lsi = TRUE; 4068 } 4069 break; 4070 } 4071 } 4072 } 4073 4074 4075 /* 4076 * The following routines support reading and interpretation of disk labels, 4077 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4078 * fdisk tables. 4079 */ 4080 4081 /* 4082 * Function: sd_validate_geometry 4083 * 4084 * Description: Read the label from the disk (if present). Update the unit's 4085 * geometry and vtoc information from the data in the label. 4086 * Verify that the label is valid. 4087 * 4088 * Arguments: un - driver soft state (unit) structure 4089 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4090 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4091 * to use the USCSI "direct" chain and bypass the normal 4092 * command waitq. 4093 * 4094 * Return Code: 0 - Successful completion 4095 * EINVAL - Invalid value in un->un_tgt_blocksize or 4096 * un->un_blockcount; or label on disk is corrupted 4097 * or unreadable. 4098 * EACCES - Reservation conflict at the device. 4099 * ENOMEM - Resource allocation error 4100 * ENOTSUP - geometry not applicable 4101 * 4102 * Context: Kernel thread only (can sleep). 4103 */ 4104 4105 static int 4106 sd_validate_geometry(struct sd_lun *un, int path_flag) 4107 { 4108 static char labelstring[128]; 4109 static char buf[256]; 4110 char *label = NULL; 4111 int label_error = 0; 4112 int gvalid = un->un_f_geometry_is_valid; 4113 int lbasize; 4114 uint_t capacity; 4115 int count; 4116 4117 ASSERT(un != NULL); 4118 ASSERT(mutex_owned(SD_MUTEX(un))); 4119 4120 /* 4121 * If the required values are not valid, then try getting them 4122 * once via read capacity. If that fails, then fail this call. 4123 * This is necessary with the new mpxio failover behavior in 4124 * the T300 where we can get an attach for the inactive path 4125 * before the active path. The inactive path fails commands with 4126 * sense data of 02,04,88 which happens to the read capacity 4127 * before mpxio has had sufficient knowledge to know if it should 4128 * force a fail over or not. (Which it won't do at attach anyhow). 4129 * If the read capacity at attach time fails, un_tgt_blocksize and 4130 * un_blockcount won't be valid. 4131 */ 4132 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4133 (un->un_f_blockcount_is_valid != TRUE)) { 4134 uint64_t cap; 4135 uint32_t lbasz; 4136 int rval; 4137 4138 mutex_exit(SD_MUTEX(un)); 4139 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4140 &lbasz, SD_PATH_DIRECT); 4141 mutex_enter(SD_MUTEX(un)); 4142 if (rval == 0) { 4143 /* 4144 * The following relies on 4145 * sd_send_scsi_READ_CAPACITY never 4146 * returning 0 for capacity and/or lbasize. 4147 */ 4148 sd_update_block_info(un, lbasz, cap); 4149 } 4150 4151 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4152 (un->un_f_blockcount_is_valid != TRUE)) { 4153 return (EINVAL); 4154 } 4155 } 4156 4157 /* 4158 * Copy the lbasize and capacity so that if they're reset while we're 4159 * not holding the SD_MUTEX, we will continue to use valid values 4160 * after the SD_MUTEX is reacquired. (4119659) 4161 */ 4162 lbasize = un->un_tgt_blocksize; 4163 capacity = un->un_blockcount; 4164 4165 #if defined(_SUNOS_VTOC_16) 4166 /* 4167 * Set up the "whole disk" fdisk partition; this should always 4168 * exist, regardless of whether the disk contains an fdisk table 4169 * or vtoc. 4170 */ 4171 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4172 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4173 #endif 4174 4175 /* 4176 * Refresh the logical and physical geometry caches. 4177 * (data from MODE SENSE format/rigid disk geometry pages, 4178 * and scsi_ifgetcap("geometry"). 4179 */ 4180 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4181 4182 label_error = sd_use_efi(un, path_flag); 4183 if (label_error == 0) { 4184 /* found a valid EFI label */ 4185 SD_TRACE(SD_LOG_IO_PARTITION, un, 4186 "sd_validate_geometry: found EFI label\n"); 4187 un->un_solaris_offset = 0; 4188 un->un_solaris_size = capacity; 4189 return (ENOTSUP); 4190 } 4191 if (un->un_blockcount > DK_MAX_BLOCKS) { 4192 if (label_error == ESRCH) { 4193 /* 4194 * they've configured a LUN over 1TB, but used 4195 * format.dat to restrict format's view of the 4196 * capacity to be under 1TB 4197 */ 4198 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4199 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4200 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4201 "size to be < 1TB or relabel the disk with an EFI label"); 4202 } else { 4203 /* unlabeled disk over 1TB */ 4204 return (ENOTSUP); 4205 } 4206 } 4207 label_error = 0; 4208 4209 /* 4210 * at this point it is either labeled with a VTOC or it is 4211 * under 1TB 4212 */ 4213 if (un->un_f_vtoc_label_supported) { 4214 struct dk_label *dkl; 4215 offset_t dkl1; 4216 offset_t label_addr, real_addr; 4217 int rval; 4218 size_t buffer_size; 4219 4220 /* 4221 * Note: This will set up un->un_solaris_size and 4222 * un->un_solaris_offset. 4223 */ 4224 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4225 case SD_CMD_RESERVATION_CONFLICT: 4226 ASSERT(mutex_owned(SD_MUTEX(un))); 4227 return (EACCES); 4228 case SD_CMD_FAILURE: 4229 ASSERT(mutex_owned(SD_MUTEX(un))); 4230 return (ENOMEM); 4231 } 4232 4233 if (un->un_solaris_size <= DK_LABEL_LOC) { 4234 /* 4235 * Found fdisk table but no Solaris partition entry, 4236 * so don't call sd_uselabel() and don't create 4237 * a default label. 4238 */ 4239 label_error = 0; 4240 un->un_f_geometry_is_valid = TRUE; 4241 goto no_solaris_partition; 4242 } 4243 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4244 4245 /* 4246 * sys_blocksize != tgt_blocksize, need to re-adjust 4247 * blkno and save the index to beginning of dk_label 4248 */ 4249 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4250 buffer_size = SD_REQBYTES2TGTBYTES(un, 4251 sizeof (struct dk_label)); 4252 4253 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4254 "label_addr: 0x%x allocation size: 0x%x\n", 4255 label_addr, buffer_size); 4256 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4257 if (dkl == NULL) { 4258 return (ENOMEM); 4259 } 4260 4261 mutex_exit(SD_MUTEX(un)); 4262 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4263 path_flag); 4264 mutex_enter(SD_MUTEX(un)); 4265 4266 switch (rval) { 4267 case 0: 4268 /* 4269 * sd_uselabel will establish that the geometry 4270 * is valid. 4271 * For sys_blocksize != tgt_blocksize, need 4272 * to index into the beginning of dk_label 4273 */ 4274 dkl1 = (daddr_t)dkl 4275 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4276 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4277 path_flag) != SD_LABEL_IS_VALID) { 4278 label_error = EINVAL; 4279 } 4280 break; 4281 case EACCES: 4282 label_error = EACCES; 4283 break; 4284 default: 4285 label_error = EINVAL; 4286 break; 4287 } 4288 4289 kmem_free(dkl, buffer_size); 4290 4291 #if defined(_SUNOS_VTOC_8) 4292 label = (char *)un->un_asciilabel; 4293 #elif defined(_SUNOS_VTOC_16) 4294 label = (char *)un->un_vtoc.v_asciilabel; 4295 #else 4296 #error "No VTOC format defined." 4297 #endif 4298 } 4299 4300 /* 4301 * If a valid label was not found, AND if no reservation conflict 4302 * was detected, then go ahead and create a default label (4069506). 4303 */ 4304 4305 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4306 if (un->un_f_geometry_is_valid == FALSE) { 4307 sd_build_default_label(un); 4308 } 4309 label_error = 0; 4310 } 4311 4312 no_solaris_partition: 4313 if ((!un->un_f_has_removable_media || 4314 (un->un_f_has_removable_media && 4315 un->un_mediastate == DKIO_EJECTED)) && 4316 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4317 /* 4318 * Print out a message indicating who and what we are. 4319 * We do this only when we happen to really validate the 4320 * geometry. We may call sd_validate_geometry() at other 4321 * times, e.g., ioctl()'s like Get VTOC in which case we 4322 * don't want to print the label. 4323 * If the geometry is valid, print the label string, 4324 * else print vendor and product info, if available 4325 */ 4326 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4327 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4328 } else { 4329 mutex_enter(&sd_label_mutex); 4330 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4331 labelstring); 4332 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4333 &labelstring[64]); 4334 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4335 labelstring, &labelstring[64]); 4336 if (un->un_f_blockcount_is_valid == TRUE) { 4337 (void) sprintf(&buf[strlen(buf)], 4338 ", %llu %u byte blocks\n", 4339 (longlong_t)un->un_blockcount, 4340 un->un_tgt_blocksize); 4341 } else { 4342 (void) sprintf(&buf[strlen(buf)], 4343 ", (unknown capacity)\n"); 4344 } 4345 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4346 mutex_exit(&sd_label_mutex); 4347 } 4348 } 4349 4350 #if defined(_SUNOS_VTOC_16) 4351 /* 4352 * If we have valid geometry, set up the remaining fdisk partitions. 4353 * Note that dkl_cylno is not used for the fdisk map entries, so 4354 * we set it to an entirely bogus value. 4355 */ 4356 for (count = 0; count < FD_NUMPART; count++) { 4357 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4358 un->un_map[FDISK_P1 + count].dkl_nblk = 4359 un->un_fmap[count].fmap_nblk; 4360 4361 un->un_offset[FDISK_P1 + count] = 4362 un->un_fmap[count].fmap_start; 4363 } 4364 #endif 4365 4366 for (count = 0; count < NDKMAP; count++) { 4367 #if defined(_SUNOS_VTOC_8) 4368 struct dk_map *lp = &un->un_map[count]; 4369 un->un_offset[count] = 4370 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4371 #elif defined(_SUNOS_VTOC_16) 4372 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4373 4374 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4375 #else 4376 #error "No VTOC format defined." 4377 #endif 4378 } 4379 4380 return (label_error); 4381 } 4382 4383 4384 #if defined(_SUNOS_VTOC_16) 4385 /* 4386 * Macro: MAX_BLKS 4387 * 4388 * This macro is used for table entries where we need to have the largest 4389 * possible sector value for that head & SPT (sectors per track) 4390 * combination. Other entries for some smaller disk sizes are set by 4391 * convention to match those used by X86 BIOS usage. 4392 */ 4393 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4394 4395 /* 4396 * Function: sd_convert_geometry 4397 * 4398 * Description: Convert physical geometry into a dk_geom structure. In 4399 * other words, make sure we don't wrap 16-bit values. 4400 * e.g. converting from geom_cache to dk_geom 4401 * 4402 * Context: Kernel thread only 4403 */ 4404 static void 4405 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4406 { 4407 int i; 4408 static const struct chs_values { 4409 uint_t max_cap; /* Max Capacity for this HS. */ 4410 uint_t nhead; /* Heads to use. */ 4411 uint_t nsect; /* SPT to use. */ 4412 } CHS_values[] = { 4413 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4414 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4415 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4416 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4417 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4418 }; 4419 4420 /* Unlabeled SCSI floppy device */ 4421 if (capacity <= 0x1000) { 4422 un_g->dkg_nhead = 2; 4423 un_g->dkg_ncyl = 80; 4424 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4425 return; 4426 } 4427 4428 /* 4429 * For all devices we calculate cylinders using the 4430 * heads and sectors we assign based on capacity of the 4431 * device. The table is designed to be compatible with the 4432 * way other operating systems lay out fdisk tables for X86 4433 * and to insure that the cylinders never exceed 65535 to 4434 * prevent problems with X86 ioctls that report geometry. 4435 * We use SPT that are multiples of 63, since other OSes that 4436 * are not limited to 16-bits for cylinders stop at 63 SPT 4437 * we make do by using multiples of 63 SPT. 4438 * 4439 * Note than capacities greater than or equal to 1TB will simply 4440 * get the largest geometry from the table. This should be okay 4441 * since disks this large shouldn't be using CHS values anyway. 4442 */ 4443 for (i = 0; CHS_values[i].max_cap < capacity && 4444 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4445 ; 4446 4447 un_g->dkg_nhead = CHS_values[i].nhead; 4448 un_g->dkg_nsect = CHS_values[i].nsect; 4449 } 4450 #endif 4451 4452 4453 /* 4454 * Function: sd_resync_geom_caches 4455 * 4456 * Description: (Re)initialize both geometry caches: the virtual geometry 4457 * information is extracted from the HBA (the "geometry" 4458 * capability), and the physical geometry cache data is 4459 * generated by issuing MODE SENSE commands. 4460 * 4461 * Arguments: un - driver soft state (unit) structure 4462 * capacity - disk capacity in #blocks 4463 * lbasize - disk block size in bytes 4464 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4465 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4466 * to use the USCSI "direct" chain and bypass the normal 4467 * command waitq. 4468 * 4469 * Context: Kernel thread only (can sleep). 4470 */ 4471 4472 static void 4473 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4474 int path_flag) 4475 { 4476 struct geom_cache pgeom; 4477 struct geom_cache *pgeom_p = &pgeom; 4478 int spc; 4479 unsigned short nhead; 4480 unsigned short nsect; 4481 4482 ASSERT(un != NULL); 4483 ASSERT(mutex_owned(SD_MUTEX(un))); 4484 4485 /* 4486 * Ask the controller for its logical geometry. 4487 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4488 * then the lgeom cache will be invalid. 4489 */ 4490 sd_get_virtual_geometry(un, capacity, lbasize); 4491 4492 /* 4493 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4494 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4495 */ 4496 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4497 /* 4498 * Note: Perhaps this needs to be more adaptive? The rationale 4499 * is that, if there's no HBA geometry from the HBA driver, any 4500 * guess is good, since this is the physical geometry. If MODE 4501 * SENSE fails this gives a max cylinder size for non-LBA access 4502 */ 4503 nhead = 255; 4504 nsect = 63; 4505 } else { 4506 nhead = un->un_lgeom.g_nhead; 4507 nsect = un->un_lgeom.g_nsect; 4508 } 4509 4510 if (ISCD(un)) { 4511 pgeom_p->g_nhead = 1; 4512 pgeom_p->g_nsect = nsect * nhead; 4513 } else { 4514 pgeom_p->g_nhead = nhead; 4515 pgeom_p->g_nsect = nsect; 4516 } 4517 4518 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4519 pgeom_p->g_capacity = capacity; 4520 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4521 pgeom_p->g_acyl = 0; 4522 4523 /* 4524 * Retrieve fresh geometry data from the hardware, stash it 4525 * here temporarily before we rebuild the incore label. 4526 * 4527 * We want to use the MODE SENSE commands to derive the 4528 * physical geometry of the device, but if either command 4529 * fails, the logical geometry is used as the fallback for 4530 * disk label geometry. 4531 */ 4532 mutex_exit(SD_MUTEX(un)); 4533 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4534 mutex_enter(SD_MUTEX(un)); 4535 4536 /* 4537 * Now update the real copy while holding the mutex. This 4538 * way the global copy is never in an inconsistent state. 4539 */ 4540 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4541 4542 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4543 "(cached from lgeom)\n"); 4544 SD_INFO(SD_LOG_COMMON, un, 4545 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4546 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4547 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4548 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4549 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4550 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4551 un->un_pgeom.g_rpm); 4552 } 4553 4554 4555 /* 4556 * Function: sd_read_fdisk 4557 * 4558 * Description: utility routine to read the fdisk table. 4559 * 4560 * Arguments: un - driver soft state (unit) structure 4561 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4562 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4563 * to use the USCSI "direct" chain and bypass the normal 4564 * command waitq. 4565 * 4566 * Return Code: SD_CMD_SUCCESS 4567 * SD_CMD_FAILURE 4568 * 4569 * Context: Kernel thread only (can sleep). 4570 */ 4571 /* ARGSUSED */ 4572 static int 4573 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4574 { 4575 #if defined(_NO_FDISK_PRESENT) 4576 4577 un->un_solaris_offset = 0; 4578 un->un_solaris_size = capacity; 4579 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4580 return (SD_CMD_SUCCESS); 4581 4582 #elif defined(_FIRMWARE_NEEDS_FDISK) 4583 4584 struct ipart *fdp; 4585 struct mboot *mbp; 4586 struct ipart fdisk[FD_NUMPART]; 4587 int i; 4588 char sigbuf[2]; 4589 caddr_t bufp; 4590 int uidx; 4591 int rval; 4592 int lba = 0; 4593 uint_t solaris_offset; /* offset to solaris part. */ 4594 daddr_t solaris_size; /* size of solaris partition */ 4595 uint32_t blocksize; 4596 4597 ASSERT(un != NULL); 4598 ASSERT(mutex_owned(SD_MUTEX(un))); 4599 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4600 4601 blocksize = un->un_tgt_blocksize; 4602 4603 /* 4604 * Start off assuming no fdisk table 4605 */ 4606 solaris_offset = 0; 4607 solaris_size = capacity; 4608 4609 mutex_exit(SD_MUTEX(un)); 4610 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4611 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4612 mutex_enter(SD_MUTEX(un)); 4613 4614 if (rval != 0) { 4615 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4616 "sd_read_fdisk: fdisk read err\n"); 4617 kmem_free(bufp, blocksize); 4618 return (SD_CMD_FAILURE); 4619 } 4620 4621 mbp = (struct mboot *)bufp; 4622 4623 /* 4624 * The fdisk table does not begin on a 4-byte boundary within the 4625 * master boot record, so we copy it to an aligned structure to avoid 4626 * alignment exceptions on some processors. 4627 */ 4628 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4629 4630 /* 4631 * Check for lba support before verifying sig; sig might not be 4632 * there, say on a blank disk, but the max_chs mark may still 4633 * be present. 4634 * 4635 * Note: LBA support and BEFs are an x86-only concept but this 4636 * code should work OK on SPARC as well. 4637 */ 4638 4639 /* 4640 * First, check for lba-access-ok on root node (or prom root node) 4641 * if present there, don't need to search fdisk table. 4642 */ 4643 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4644 "lba-access-ok", 0) != 0) { 4645 /* All drives do LBA; don't search fdisk table */ 4646 lba = 1; 4647 } else { 4648 /* Okay, look for mark in fdisk table */ 4649 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4650 /* accumulate "lba" value from all partitions */ 4651 lba = (lba || sd_has_max_chs_vals(fdp)); 4652 } 4653 } 4654 4655 if (lba != 0) { 4656 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4657 4658 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4659 "lba-access-ok", 0) == 0) { 4660 /* not found; create it */ 4661 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4662 "lba-access-ok", (caddr_t)NULL, 0) != 4663 DDI_PROP_SUCCESS) { 4664 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4665 "sd_read_fdisk: Can't create lba property " 4666 "for instance %d\n", 4667 ddi_get_instance(SD_DEVINFO(un))); 4668 } 4669 } 4670 } 4671 4672 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4673 4674 /* 4675 * Endian-independent signature check 4676 */ 4677 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4678 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4679 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4680 "sd_read_fdisk: no fdisk\n"); 4681 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4682 rval = SD_CMD_SUCCESS; 4683 goto done; 4684 } 4685 4686 #ifdef SDDEBUG 4687 if (sd_level_mask & SD_LOGMASK_INFO) { 4688 fdp = fdisk; 4689 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4690 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4691 "numsect sysid bootid\n"); 4692 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4693 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4694 " %d: %8d %8d 0x%08x 0x%08x\n", 4695 i, fdp->relsect, fdp->numsect, 4696 fdp->systid, fdp->bootid); 4697 } 4698 } 4699 #endif 4700 4701 /* 4702 * Try to find the unix partition 4703 */ 4704 uidx = -1; 4705 solaris_offset = 0; 4706 solaris_size = 0; 4707 4708 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4709 int relsect; 4710 int numsect; 4711 4712 if (fdp->numsect == 0) { 4713 un->un_fmap[i].fmap_start = 0; 4714 un->un_fmap[i].fmap_nblk = 0; 4715 continue; 4716 } 4717 4718 /* 4719 * Data in the fdisk table is little-endian. 4720 */ 4721 relsect = LE_32(fdp->relsect); 4722 numsect = LE_32(fdp->numsect); 4723 4724 un->un_fmap[i].fmap_start = relsect; 4725 un->un_fmap[i].fmap_nblk = numsect; 4726 4727 if (fdp->systid != SUNIXOS && 4728 fdp->systid != SUNIXOS2 && 4729 fdp->systid != EFI_PMBR) { 4730 continue; 4731 } 4732 4733 /* 4734 * use the last active solaris partition id found 4735 * (there should only be 1 active partition id) 4736 * 4737 * if there are no active solaris partition id 4738 * then use the first inactive solaris partition id 4739 */ 4740 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4741 uidx = i; 4742 solaris_offset = relsect; 4743 solaris_size = numsect; 4744 } 4745 } 4746 4747 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4748 un->un_solaris_offset, un->un_solaris_size); 4749 4750 rval = SD_CMD_SUCCESS; 4751 4752 done: 4753 4754 /* 4755 * Clear the VTOC info, only if the Solaris partition entry 4756 * has moved, changed size, been deleted, or if the size of 4757 * the partition is too small to even fit the label sector. 4758 */ 4759 if ((un->un_solaris_offset != solaris_offset) || 4760 (un->un_solaris_size != solaris_size) || 4761 solaris_size <= DK_LABEL_LOC) { 4762 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4763 solaris_offset, solaris_size); 4764 bzero(&un->un_g, sizeof (struct dk_geom)); 4765 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4766 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4767 un->un_f_geometry_is_valid = FALSE; 4768 } 4769 un->un_solaris_offset = solaris_offset; 4770 un->un_solaris_size = solaris_size; 4771 kmem_free(bufp, blocksize); 4772 return (rval); 4773 4774 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4775 #error "fdisk table presence undetermined for this platform." 4776 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4777 } 4778 4779 4780 /* 4781 * Function: sd_get_physical_geometry 4782 * 4783 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4784 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4785 * target, and use this information to initialize the physical 4786 * geometry cache specified by pgeom_p. 4787 * 4788 * MODE SENSE is an optional command, so failure in this case 4789 * does not necessarily denote an error. We want to use the 4790 * MODE SENSE commands to derive the physical geometry of the 4791 * device, but if either command fails, the logical geometry is 4792 * used as the fallback for disk label geometry. 4793 * 4794 * This requires that un->un_blockcount and un->un_tgt_blocksize 4795 * have already been initialized for the current target and 4796 * that the current values be passed as args so that we don't 4797 * end up ever trying to use -1 as a valid value. This could 4798 * happen if either value is reset while we're not holding 4799 * the mutex. 4800 * 4801 * Arguments: un - driver soft state (unit) structure 4802 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4803 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4804 * to use the USCSI "direct" chain and bypass the normal 4805 * command waitq. 4806 * 4807 * Context: Kernel thread only (can sleep). 4808 */ 4809 4810 static void 4811 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4812 int capacity, int lbasize, int path_flag) 4813 { 4814 struct mode_format *page3p; 4815 struct mode_geometry *page4p; 4816 struct mode_header *headerp; 4817 int sector_size; 4818 int nsect; 4819 int nhead; 4820 int ncyl; 4821 int intrlv; 4822 int spc; 4823 int modesense_capacity; 4824 int rpm; 4825 int bd_len; 4826 int mode_header_length; 4827 uchar_t *p3bufp; 4828 uchar_t *p4bufp; 4829 int cdbsize; 4830 4831 ASSERT(un != NULL); 4832 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4833 4834 if (un->un_f_blockcount_is_valid != TRUE) { 4835 return; 4836 } 4837 4838 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4839 return; 4840 } 4841 4842 if (lbasize == 0) { 4843 if (ISCD(un)) { 4844 lbasize = 2048; 4845 } else { 4846 lbasize = un->un_sys_blocksize; 4847 } 4848 } 4849 pgeom_p->g_secsize = (unsigned short)lbasize; 4850 4851 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4852 4853 /* 4854 * Retrieve MODE SENSE page 3 - Format Device Page 4855 */ 4856 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4857 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4858 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4859 != 0) { 4860 SD_ERROR(SD_LOG_COMMON, un, 4861 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4862 goto page3_exit; 4863 } 4864 4865 /* 4866 * Determine size of Block Descriptors in order to locate the mode 4867 * page data. ATAPI devices return 0, SCSI devices should return 4868 * MODE_BLK_DESC_LENGTH. 4869 */ 4870 headerp = (struct mode_header *)p3bufp; 4871 if (un->un_f_cfg_is_atapi == TRUE) { 4872 struct mode_header_grp2 *mhp = 4873 (struct mode_header_grp2 *)headerp; 4874 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4875 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4876 } else { 4877 mode_header_length = MODE_HEADER_LENGTH; 4878 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4879 } 4880 4881 if (bd_len > MODE_BLK_DESC_LENGTH) { 4882 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4883 "received unexpected bd_len of %d, page3\n", bd_len); 4884 goto page3_exit; 4885 } 4886 4887 page3p = (struct mode_format *) 4888 ((caddr_t)headerp + mode_header_length + bd_len); 4889 4890 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4891 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4892 "mode sense pg3 code mismatch %d\n", 4893 page3p->mode_page.code); 4894 goto page3_exit; 4895 } 4896 4897 /* 4898 * Use this physical geometry data only if BOTH MODE SENSE commands 4899 * complete successfully; otherwise, revert to the logical geometry. 4900 * So, we need to save everything in temporary variables. 4901 */ 4902 sector_size = BE_16(page3p->data_bytes_sect); 4903 4904 /* 4905 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4906 */ 4907 if (sector_size == 0) { 4908 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 4909 } else { 4910 sector_size &= ~(un->un_sys_blocksize - 1); 4911 } 4912 4913 nsect = BE_16(page3p->sect_track); 4914 intrlv = BE_16(page3p->interleave); 4915 4916 SD_INFO(SD_LOG_COMMON, un, 4917 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4918 SD_INFO(SD_LOG_COMMON, un, 4919 " mode page: %d; nsect: %d; sector size: %d;\n", 4920 page3p->mode_page.code, nsect, sector_size); 4921 SD_INFO(SD_LOG_COMMON, un, 4922 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4923 BE_16(page3p->track_skew), 4924 BE_16(page3p->cylinder_skew)); 4925 4926 4927 /* 4928 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4929 */ 4930 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4931 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4932 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4933 != 0) { 4934 SD_ERROR(SD_LOG_COMMON, un, 4935 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4936 goto page4_exit; 4937 } 4938 4939 /* 4940 * Determine size of Block Descriptors in order to locate the mode 4941 * page data. ATAPI devices return 0, SCSI devices should return 4942 * MODE_BLK_DESC_LENGTH. 4943 */ 4944 headerp = (struct mode_header *)p4bufp; 4945 if (un->un_f_cfg_is_atapi == TRUE) { 4946 struct mode_header_grp2 *mhp = 4947 (struct mode_header_grp2 *)headerp; 4948 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4949 } else { 4950 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4951 } 4952 4953 if (bd_len > MODE_BLK_DESC_LENGTH) { 4954 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4955 "received unexpected bd_len of %d, page4\n", bd_len); 4956 goto page4_exit; 4957 } 4958 4959 page4p = (struct mode_geometry *) 4960 ((caddr_t)headerp + mode_header_length + bd_len); 4961 4962 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4963 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4964 "mode sense pg4 code mismatch %d\n", 4965 page4p->mode_page.code); 4966 goto page4_exit; 4967 } 4968 4969 /* 4970 * Stash the data now, after we know that both commands completed. 4971 */ 4972 4973 mutex_enter(SD_MUTEX(un)); 4974 4975 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4976 spc = nhead * nsect; 4977 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4978 rpm = BE_16(page4p->rpm); 4979 4980 modesense_capacity = spc * ncyl; 4981 4982 SD_INFO(SD_LOG_COMMON, un, 4983 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4984 SD_INFO(SD_LOG_COMMON, un, 4985 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4986 SD_INFO(SD_LOG_COMMON, un, 4987 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4988 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4989 (void *)pgeom_p, capacity); 4990 4991 /* 4992 * Compensate if the drive's geometry is not rectangular, i.e., 4993 * the product of C * H * S returned by MODE SENSE >= that returned 4994 * by read capacity. This is an idiosyncrasy of the original x86 4995 * disk subsystem. 4996 */ 4997 if (modesense_capacity >= capacity) { 4998 SD_INFO(SD_LOG_COMMON, un, 4999 "sd_get_physical_geometry: adjusting acyl; " 5000 "old: %d; new: %d\n", pgeom_p->g_acyl, 5001 (modesense_capacity - capacity + spc - 1) / spc); 5002 if (sector_size != 0) { 5003 /* 1243403: NEC D38x7 drives don't support sec size */ 5004 pgeom_p->g_secsize = (unsigned short)sector_size; 5005 } 5006 pgeom_p->g_nsect = (unsigned short)nsect; 5007 pgeom_p->g_nhead = (unsigned short)nhead; 5008 pgeom_p->g_capacity = capacity; 5009 pgeom_p->g_acyl = 5010 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5011 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5012 } 5013 5014 pgeom_p->g_rpm = (unsigned short)rpm; 5015 pgeom_p->g_intrlv = (unsigned short)intrlv; 5016 5017 SD_INFO(SD_LOG_COMMON, un, 5018 "sd_get_physical_geometry: mode sense geometry:\n"); 5019 SD_INFO(SD_LOG_COMMON, un, 5020 " nsect: %d; sector size: %d; interlv: %d\n", 5021 nsect, sector_size, intrlv); 5022 SD_INFO(SD_LOG_COMMON, un, 5023 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5024 nhead, ncyl, rpm, modesense_capacity); 5025 SD_INFO(SD_LOG_COMMON, un, 5026 "sd_get_physical_geometry: (cached)\n"); 5027 SD_INFO(SD_LOG_COMMON, un, 5028 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5029 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5030 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5031 SD_INFO(SD_LOG_COMMON, un, 5032 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5033 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5034 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5035 5036 mutex_exit(SD_MUTEX(un)); 5037 5038 page4_exit: 5039 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5040 page3_exit: 5041 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5042 } 5043 5044 5045 /* 5046 * Function: sd_get_virtual_geometry 5047 * 5048 * Description: Ask the controller to tell us about the target device. 5049 * 5050 * Arguments: un - pointer to softstate 5051 * capacity - disk capacity in #blocks 5052 * lbasize - disk block size in bytes 5053 * 5054 * Context: Kernel thread only 5055 */ 5056 5057 static void 5058 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5059 { 5060 struct geom_cache *lgeom_p = &un->un_lgeom; 5061 uint_t geombuf; 5062 int spc; 5063 5064 ASSERT(un != NULL); 5065 ASSERT(mutex_owned(SD_MUTEX(un))); 5066 5067 mutex_exit(SD_MUTEX(un)); 5068 5069 /* Set sector size, and total number of sectors */ 5070 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5071 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5072 5073 /* Let the HBA tell us its geometry */ 5074 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5075 5076 mutex_enter(SD_MUTEX(un)); 5077 5078 /* A value of -1 indicates an undefined "geometry" property */ 5079 if (geombuf == (-1)) { 5080 return; 5081 } 5082 5083 /* Initialize the logical geometry cache. */ 5084 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5085 lgeom_p->g_nsect = geombuf & 0xffff; 5086 lgeom_p->g_secsize = un->un_sys_blocksize; 5087 5088 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5089 5090 /* 5091 * Note: The driver originally converted the capacity value from 5092 * target blocks to system blocks. However, the capacity value passed 5093 * to this routine is already in terms of system blocks (this scaling 5094 * is done when the READ CAPACITY command is issued and processed). 5095 * This 'error' may have gone undetected because the usage of g_ncyl 5096 * (which is based upon g_capacity) is very limited within the driver 5097 */ 5098 lgeom_p->g_capacity = capacity; 5099 5100 /* 5101 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5102 * hba may return zero values if the device has been removed. 5103 */ 5104 if (spc == 0) { 5105 lgeom_p->g_ncyl = 0; 5106 } else { 5107 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5108 } 5109 lgeom_p->g_acyl = 0; 5110 5111 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5112 SD_INFO(SD_LOG_COMMON, un, 5113 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5114 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5115 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5116 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5117 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5118 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5119 } 5120 5121 5122 /* 5123 * Function: sd_update_block_info 5124 * 5125 * Description: Calculate a byte count to sector count bitshift value 5126 * from sector size. 5127 * 5128 * Arguments: un: unit struct. 5129 * lbasize: new target sector size 5130 * capacity: new target capacity, ie. block count 5131 * 5132 * Context: Kernel thread context 5133 */ 5134 5135 static void 5136 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5137 { 5138 if (lbasize != 0) { 5139 un->un_tgt_blocksize = lbasize; 5140 un->un_f_tgt_blocksize_is_valid = TRUE; 5141 } 5142 5143 if (capacity != 0) { 5144 un->un_blockcount = capacity; 5145 un->un_f_blockcount_is_valid = TRUE; 5146 } 5147 } 5148 5149 5150 static void 5151 sd_swap_efi_gpt(efi_gpt_t *e) 5152 { 5153 _NOTE(ASSUMING_PROTECTED(*e)) 5154 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5155 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5156 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5157 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5158 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5159 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5160 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5161 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5162 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5163 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5164 e->efi_gpt_NumberOfPartitionEntries = 5165 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5166 e->efi_gpt_SizeOfPartitionEntry = 5167 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5168 e->efi_gpt_PartitionEntryArrayCRC32 = 5169 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5170 } 5171 5172 static void 5173 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5174 { 5175 int i; 5176 5177 _NOTE(ASSUMING_PROTECTED(*p)) 5178 for (i = 0; i < nparts; i++) { 5179 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5180 p[i].efi_gpe_PartitionTypeGUID); 5181 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5182 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5183 /* PartitionAttrs */ 5184 } 5185 } 5186 5187 static int 5188 sd_validate_efi(efi_gpt_t *labp) 5189 { 5190 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5191 return (EINVAL); 5192 /* at least 96 bytes in this version of the spec. */ 5193 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5194 labp->efi_gpt_HeaderSize) 5195 return (EINVAL); 5196 /* this should be 128 bytes */ 5197 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5198 return (EINVAL); 5199 return (0); 5200 } 5201 5202 static int 5203 sd_use_efi(struct sd_lun *un, int path_flag) 5204 { 5205 int i; 5206 int rval = 0; 5207 efi_gpe_t *partitions; 5208 uchar_t *buf; 5209 uint_t lbasize; 5210 uint64_t cap; 5211 uint_t nparts; 5212 diskaddr_t gpe_lba; 5213 5214 ASSERT(mutex_owned(SD_MUTEX(un))); 5215 lbasize = un->un_tgt_blocksize; 5216 5217 mutex_exit(SD_MUTEX(un)); 5218 5219 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5220 5221 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5222 rval = EINVAL; 5223 goto done_err; 5224 } 5225 5226 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5227 if (rval) { 5228 goto done_err; 5229 } 5230 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5231 /* not ours */ 5232 rval = ESRCH; 5233 goto done_err; 5234 } 5235 5236 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5237 if (rval) { 5238 goto done_err; 5239 } 5240 sd_swap_efi_gpt((efi_gpt_t *)buf); 5241 5242 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5243 /* 5244 * Couldn't read the primary, try the backup. Our 5245 * capacity at this point could be based on CHS, so 5246 * check what the device reports. 5247 */ 5248 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5249 path_flag); 5250 if (rval) { 5251 goto done_err; 5252 } 5253 5254 /* 5255 * The MMC standard allows READ CAPACITY to be 5256 * inaccurate by a bounded amount (in the interest of 5257 * response latency). As a result, failed READs are 5258 * commonplace (due to the reading of metadata and not 5259 * data). Depending on the per-Vendor/drive Sense data, 5260 * the failed READ can cause many (unnecessary) retries. 5261 */ 5262 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5263 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5264 path_flag)) != 0) { 5265 goto done_err; 5266 } 5267 5268 sd_swap_efi_gpt((efi_gpt_t *)buf); 5269 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5270 goto done_err; 5271 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5272 "primary label corrupt; using backup\n"); 5273 } 5274 5275 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5276 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5277 5278 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5279 path_flag); 5280 if (rval) { 5281 goto done_err; 5282 } 5283 partitions = (efi_gpe_t *)buf; 5284 5285 if (nparts > MAXPART) { 5286 nparts = MAXPART; 5287 } 5288 sd_swap_efi_gpe(nparts, partitions); 5289 5290 mutex_enter(SD_MUTEX(un)); 5291 5292 /* Fill in partition table. */ 5293 for (i = 0; i < nparts; i++) { 5294 if (partitions->efi_gpe_StartingLBA != 0 || 5295 partitions->efi_gpe_EndingLBA != 0) { 5296 un->un_map[i].dkl_cylno = 5297 partitions->efi_gpe_StartingLBA; 5298 un->un_map[i].dkl_nblk = 5299 partitions->efi_gpe_EndingLBA - 5300 partitions->efi_gpe_StartingLBA + 1; 5301 un->un_offset[i] = 5302 partitions->efi_gpe_StartingLBA; 5303 } 5304 if (i == WD_NODE) { 5305 /* 5306 * minor number 7 corresponds to the whole disk 5307 */ 5308 un->un_map[i].dkl_cylno = 0; 5309 un->un_map[i].dkl_nblk = un->un_blockcount; 5310 un->un_offset[i] = 0; 5311 } 5312 partitions++; 5313 } 5314 un->un_solaris_offset = 0; 5315 un->un_solaris_size = cap; 5316 un->un_f_geometry_is_valid = TRUE; 5317 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5318 return (0); 5319 5320 done_err: 5321 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5322 mutex_enter(SD_MUTEX(un)); 5323 /* 5324 * if we didn't find something that could look like a VTOC 5325 * and the disk is over 1TB, we know there isn't a valid label. 5326 * Otherwise let sd_uselabel decide what to do. We only 5327 * want to invalidate this if we're certain the label isn't 5328 * valid because sd_prop_op will now fail, which in turn 5329 * causes things like opens and stats on the partition to fail. 5330 */ 5331 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5332 un->un_f_geometry_is_valid = FALSE; 5333 } 5334 return (rval); 5335 } 5336 5337 5338 /* 5339 * Function: sd_uselabel 5340 * 5341 * Description: Validate the disk label and update the relevant data (geometry, 5342 * partition, vtoc, and capacity data) in the sd_lun struct. 5343 * Marks the geometry of the unit as being valid. 5344 * 5345 * Arguments: un: unit struct. 5346 * dk_label: disk label 5347 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5348 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5349 * to use the USCSI "direct" chain and bypass the normal 5350 * command waitq. 5351 * 5352 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5353 * partition, vtoc, and capacity data are good. 5354 * 5355 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5356 * label; or computed capacity does not jibe with capacity 5357 * reported from the READ CAPACITY command. 5358 * 5359 * Context: Kernel thread only (can sleep). 5360 */ 5361 5362 static int 5363 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5364 { 5365 short *sp; 5366 short sum; 5367 short count; 5368 int label_error = SD_LABEL_IS_VALID; 5369 int i; 5370 int capacity; 5371 int part_end; 5372 int track_capacity; 5373 int err; 5374 #if defined(_SUNOS_VTOC_16) 5375 struct dkl_partition *vpartp; 5376 #endif 5377 ASSERT(un != NULL); 5378 ASSERT(mutex_owned(SD_MUTEX(un))); 5379 5380 /* Validate the magic number of the label. */ 5381 if (labp->dkl_magic != DKL_MAGIC) { 5382 #if defined(__sparc) 5383 if ((un->un_state == SD_STATE_NORMAL) && 5384 un->un_f_vtoc_errlog_supported) { 5385 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5386 "Corrupt label; wrong magic number\n"); 5387 } 5388 #endif 5389 return (SD_LABEL_IS_INVALID); 5390 } 5391 5392 /* Validate the checksum of the label. */ 5393 sp = (short *)labp; 5394 sum = 0; 5395 count = sizeof (struct dk_label) / sizeof (short); 5396 while (count--) { 5397 sum ^= *sp++; 5398 } 5399 5400 if (sum != 0) { 5401 #if defined(_SUNOS_VTOC_16) 5402 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5403 #elif defined(_SUNOS_VTOC_8) 5404 if ((un->un_state == SD_STATE_NORMAL) && 5405 un->un_f_vtoc_errlog_supported) { 5406 #endif 5407 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5408 "Corrupt label - label checksum failed\n"); 5409 } 5410 return (SD_LABEL_IS_INVALID); 5411 } 5412 5413 5414 /* 5415 * Fill in geometry structure with data from label. 5416 */ 5417 bzero(&un->un_g, sizeof (struct dk_geom)); 5418 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5419 un->un_g.dkg_acyl = labp->dkl_acyl; 5420 un->un_g.dkg_bcyl = 0; 5421 un->un_g.dkg_nhead = labp->dkl_nhead; 5422 un->un_g.dkg_nsect = labp->dkl_nsect; 5423 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5424 5425 #if defined(_SUNOS_VTOC_8) 5426 un->un_g.dkg_gap1 = labp->dkl_gap1; 5427 un->un_g.dkg_gap2 = labp->dkl_gap2; 5428 un->un_g.dkg_bhead = labp->dkl_bhead; 5429 #endif 5430 #if defined(_SUNOS_VTOC_16) 5431 un->un_dkg_skew = labp->dkl_skew; 5432 #endif 5433 5434 #if defined(__i386) || defined(__amd64) 5435 un->un_g.dkg_apc = labp->dkl_apc; 5436 #endif 5437 5438 /* 5439 * Currently we rely on the values in the label being accurate. If 5440 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5441 * 5442 * Note: In the future a MODE SENSE may be used to retrieve this data, 5443 * although this command is optional in SCSI-2. 5444 */ 5445 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5446 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5447 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5448 5449 /* 5450 * The Read and Write reinstruct values may not be valid 5451 * for older disks. 5452 */ 5453 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5454 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5455 5456 /* Fill in partition table. */ 5457 #if defined(_SUNOS_VTOC_8) 5458 for (i = 0; i < NDKMAP; i++) { 5459 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5460 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5461 } 5462 #endif 5463 #if defined(_SUNOS_VTOC_16) 5464 vpartp = labp->dkl_vtoc.v_part; 5465 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5466 5467 /* Prevent divide by zero */ 5468 if (track_capacity == 0) { 5469 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5470 "Corrupt label - zero nhead or nsect value\n"); 5471 5472 return (SD_LABEL_IS_INVALID); 5473 } 5474 5475 for (i = 0; i < NDKMAP; i++, vpartp++) { 5476 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5477 un->un_map[i].dkl_nblk = vpartp->p_size; 5478 } 5479 #endif 5480 5481 /* Fill in VTOC Structure. */ 5482 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5483 #if defined(_SUNOS_VTOC_8) 5484 /* 5485 * The 8-slice vtoc does not include the ascii label; save it into 5486 * the device's soft state structure here. 5487 */ 5488 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5489 #endif 5490 5491 /* Now look for a valid capacity. */ 5492 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5493 capacity = (un->un_g.dkg_ncyl * track_capacity); 5494 5495 if (un->un_g.dkg_acyl) { 5496 #if defined(__i386) || defined(__amd64) 5497 /* we may have > 1 alts cylinder */ 5498 capacity += (track_capacity * un->un_g.dkg_acyl); 5499 #else 5500 capacity += track_capacity; 5501 #endif 5502 } 5503 5504 /* 5505 * Force check here to ensure the computed capacity is valid. 5506 * If capacity is zero, it indicates an invalid label and 5507 * we should abort updating the relevant data then. 5508 */ 5509 if (capacity == 0) { 5510 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5511 "Corrupt label - no valid capacity could be retrieved\n"); 5512 5513 return (SD_LABEL_IS_INVALID); 5514 } 5515 5516 /* Mark the geometry as valid. */ 5517 un->un_f_geometry_is_valid = TRUE; 5518 5519 /* 5520 * At this point, un->un_blockcount should contain valid data from 5521 * the READ CAPACITY command. 5522 */ 5523 if (un->un_f_blockcount_is_valid != TRUE) { 5524 /* 5525 * We have a situation where the target didn't give us a good 5526 * READ CAPACITY value, yet there appears to be a valid label. 5527 * In this case, we'll fake the capacity. 5528 */ 5529 un->un_blockcount = capacity; 5530 un->un_f_blockcount_is_valid = TRUE; 5531 goto done; 5532 } 5533 5534 5535 if ((capacity <= un->un_blockcount) || 5536 (un->un_state != SD_STATE_NORMAL)) { 5537 #if defined(_SUNOS_VTOC_8) 5538 /* 5539 * We can't let this happen on drives that are subdivided 5540 * into logical disks (i.e., that have an fdisk table). 5541 * The un_blockcount field should always hold the full media 5542 * size in sectors, period. This code would overwrite 5543 * un_blockcount with the size of the Solaris fdisk partition. 5544 */ 5545 SD_ERROR(SD_LOG_COMMON, un, 5546 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5547 capacity, un->un_blockcount); 5548 un->un_blockcount = capacity; 5549 un->un_f_blockcount_is_valid = TRUE; 5550 #endif /* defined(_SUNOS_VTOC_8) */ 5551 goto done; 5552 } 5553 5554 if (ISCD(un)) { 5555 /* For CDROMs, we trust that the data in the label is OK. */ 5556 #if defined(_SUNOS_VTOC_8) 5557 for (i = 0; i < NDKMAP; i++) { 5558 part_end = labp->dkl_nhead * labp->dkl_nsect * 5559 labp->dkl_map[i].dkl_cylno + 5560 labp->dkl_map[i].dkl_nblk - 1; 5561 5562 if ((labp->dkl_map[i].dkl_nblk) && 5563 (part_end > un->un_blockcount)) { 5564 un->un_f_geometry_is_valid = FALSE; 5565 break; 5566 } 5567 } 5568 #endif 5569 #if defined(_SUNOS_VTOC_16) 5570 vpartp = &(labp->dkl_vtoc.v_part[0]); 5571 for (i = 0; i < NDKMAP; i++, vpartp++) { 5572 part_end = vpartp->p_start + vpartp->p_size; 5573 if ((vpartp->p_size > 0) && 5574 (part_end > un->un_blockcount)) { 5575 un->un_f_geometry_is_valid = FALSE; 5576 break; 5577 } 5578 } 5579 #endif 5580 } else { 5581 uint64_t t_capacity; 5582 uint32_t t_lbasize; 5583 5584 mutex_exit(SD_MUTEX(un)); 5585 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5586 path_flag); 5587 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5588 mutex_enter(SD_MUTEX(un)); 5589 5590 if (err == 0) { 5591 sd_update_block_info(un, t_lbasize, t_capacity); 5592 } 5593 5594 if (capacity > un->un_blockcount) { 5595 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5596 "Corrupt label - bad geometry\n"); 5597 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5598 "Label says %u blocks; Drive says %llu blocks\n", 5599 capacity, (unsigned long long)un->un_blockcount); 5600 un->un_f_geometry_is_valid = FALSE; 5601 label_error = SD_LABEL_IS_INVALID; 5602 } 5603 } 5604 5605 done: 5606 5607 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5608 SD_INFO(SD_LOG_COMMON, un, 5609 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5610 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5611 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5612 SD_INFO(SD_LOG_COMMON, un, 5613 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5614 un->un_tgt_blocksize, un->un_blockcount, 5615 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5616 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5617 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5618 5619 ASSERT(mutex_owned(SD_MUTEX(un))); 5620 5621 return (label_error); 5622 } 5623 5624 5625 /* 5626 * Function: sd_build_default_label 5627 * 5628 * Description: Generate a default label for those devices that do not have 5629 * one, e.g., new media, removable cartridges, etc.. 5630 * 5631 * Context: Kernel thread only 5632 */ 5633 5634 static void 5635 sd_build_default_label(struct sd_lun *un) 5636 { 5637 #if defined(_SUNOS_VTOC_16) 5638 uint_t phys_spc; 5639 uint_t disksize; 5640 struct dk_geom un_g; 5641 #endif 5642 5643 ASSERT(un != NULL); 5644 ASSERT(mutex_owned(SD_MUTEX(un))); 5645 5646 #if defined(_SUNOS_VTOC_8) 5647 /* 5648 * Note: This is a legacy check for non-removable devices on VTOC_8 5649 * only. This may be a valid check for VTOC_16 as well. 5650 * Once we understand why there is this difference between SPARC and 5651 * x86 platform, we could remove this legacy check. 5652 */ 5653 ASSERT(un->un_f_default_vtoc_supported); 5654 #endif 5655 5656 bzero(&un->un_g, sizeof (struct dk_geom)); 5657 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5658 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5659 5660 #if defined(_SUNOS_VTOC_8) 5661 5662 /* 5663 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5664 * But it is still necessary to set up various geometry information, 5665 * and we are doing this here. 5666 */ 5667 5668 /* 5669 * For the rpm, we use the minimum for the disk. For the head, cyl, 5670 * and number of sector per track, if the capacity <= 1GB, head = 64, 5671 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5672 * equal to C*H*S values. This will cause some truncation of size due 5673 * to round off errors. For CD-ROMs, this truncation can have adverse 5674 * side effects, so returning ncyl and nhead as 1. The nsect will 5675 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5676 */ 5677 if (ISCD(un)) { 5678 /* 5679 * Preserve the old behavior for non-writable 5680 * medias. Since dkg_nsect is a ushort, it 5681 * will lose bits as cdroms have more than 5682 * 65536 sectors. So if we recalculate 5683 * capacity, it will become much shorter. 5684 * But the dkg_* information is not 5685 * used for CDROMs so it is OK. But for 5686 * Writable CDs we need this information 5687 * to be valid (for newfs say). So we 5688 * make nsect and nhead > 1 that way 5689 * nsect can still stay within ushort limit 5690 * without losing any bits. 5691 */ 5692 if (un->un_f_mmc_writable_media == TRUE) { 5693 un->un_g.dkg_nhead = 64; 5694 un->un_g.dkg_nsect = 32; 5695 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5696 un->un_blockcount = un->un_g.dkg_ncyl * 5697 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5698 } else { 5699 un->un_g.dkg_ncyl = 1; 5700 un->un_g.dkg_nhead = 1; 5701 un->un_g.dkg_nsect = un->un_blockcount; 5702 } 5703 } else { 5704 if (un->un_blockcount <= 0x1000) { 5705 /* unlabeled SCSI floppy device */ 5706 un->un_g.dkg_nhead = 2; 5707 un->un_g.dkg_ncyl = 80; 5708 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 5709 } else if (un->un_blockcount <= 0x200000) { 5710 un->un_g.dkg_nhead = 64; 5711 un->un_g.dkg_nsect = 32; 5712 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5713 } else { 5714 un->un_g.dkg_nhead = 255; 5715 un->un_g.dkg_nsect = 63; 5716 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 5717 } 5718 un->un_blockcount = 5719 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5720 } 5721 5722 un->un_g.dkg_acyl = 0; 5723 un->un_g.dkg_bcyl = 0; 5724 un->un_g.dkg_rpm = 200; 5725 un->un_asciilabel[0] = '\0'; 5726 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 5727 5728 un->un_map[0].dkl_cylno = 0; 5729 un->un_map[0].dkl_nblk = un->un_blockcount; 5730 un->un_map[2].dkl_cylno = 0; 5731 un->un_map[2].dkl_nblk = un->un_blockcount; 5732 5733 #elif defined(_SUNOS_VTOC_16) 5734 5735 if (un->un_solaris_size == 0) { 5736 /* 5737 * Got fdisk table but no solaris entry therefore 5738 * don't create a default label 5739 */ 5740 un->un_f_geometry_is_valid = TRUE; 5741 return; 5742 } 5743 5744 /* 5745 * For CDs we continue to use the physical geometry to calculate 5746 * number of cylinders. All other devices must convert the 5747 * physical geometry (geom_cache) to values that will fit 5748 * in a dk_geom structure. 5749 */ 5750 if (ISCD(un)) { 5751 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 5752 } else { 5753 /* Convert physical geometry to disk geometry */ 5754 bzero(&un_g, sizeof (struct dk_geom)); 5755 sd_convert_geometry(un->un_blockcount, &un_g); 5756 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 5757 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5758 } 5759 5760 ASSERT(phys_spc != 0); 5761 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 5762 un->un_g.dkg_acyl = DK_ACYL; 5763 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 5764 disksize = un->un_g.dkg_ncyl * phys_spc; 5765 5766 if (ISCD(un)) { 5767 /* 5768 * CD's don't use the "heads * sectors * cyls"-type of 5769 * geometry, but instead use the entire capacity of the media. 5770 */ 5771 disksize = un->un_solaris_size; 5772 un->un_g.dkg_nhead = 1; 5773 un->un_g.dkg_nsect = 1; 5774 un->un_g.dkg_rpm = 5775 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 5776 5777 un->un_vtoc.v_part[0].p_start = 0; 5778 un->un_vtoc.v_part[0].p_size = disksize; 5779 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 5780 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 5781 5782 un->un_map[0].dkl_cylno = 0; 5783 un->un_map[0].dkl_nblk = disksize; 5784 un->un_offset[0] = 0; 5785 5786 } else { 5787 /* 5788 * Hard disks and removable media cartridges 5789 */ 5790 un->un_g.dkg_rpm = 5791 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 5792 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 5793 5794 /* Add boot slice */ 5795 un->un_vtoc.v_part[8].p_start = 0; 5796 un->un_vtoc.v_part[8].p_size = phys_spc; 5797 un->un_vtoc.v_part[8].p_tag = V_BOOT; 5798 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 5799 5800 un->un_map[8].dkl_cylno = 0; 5801 un->un_map[8].dkl_nblk = phys_spc; 5802 un->un_offset[8] = 0; 5803 } 5804 5805 un->un_g.dkg_apc = 0; 5806 un->un_vtoc.v_nparts = V_NUMPAR; 5807 5808 /* Add backup slice */ 5809 un->un_vtoc.v_part[2].p_start = 0; 5810 un->un_vtoc.v_part[2].p_size = disksize; 5811 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 5812 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 5813 5814 un->un_map[2].dkl_cylno = 0; 5815 un->un_map[2].dkl_nblk = disksize; 5816 un->un_offset[2] = 0; 5817 5818 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 5819 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5820 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5821 5822 #else 5823 #error "No VTOC format defined." 5824 #endif 5825 5826 un->un_g.dkg_read_reinstruct = 0; 5827 un->un_g.dkg_write_reinstruct = 0; 5828 5829 un->un_g.dkg_intrlv = 1; 5830 5831 un->un_vtoc.v_version = V_VERSION; 5832 un->un_vtoc.v_sanity = VTOC_SANE; 5833 5834 un->un_f_geometry_is_valid = TRUE; 5835 5836 SD_INFO(SD_LOG_COMMON, un, 5837 "sd_build_default_label: Default label created: " 5838 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 5839 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 5840 un->un_g.dkg_nsect, un->un_blockcount); 5841 } 5842 5843 5844 #if defined(_FIRMWARE_NEEDS_FDISK) 5845 /* 5846 * Max CHS values, as they are encoded into bytes, for 1022/254/63 5847 */ 5848 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 5849 #define LBA_MAX_CYL (1022 & 0xFF) 5850 #define LBA_MAX_HEAD (254) 5851 5852 5853 /* 5854 * Function: sd_has_max_chs_vals 5855 * 5856 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 5857 * 5858 * Arguments: fdp - ptr to CHS info 5859 * 5860 * Return Code: True or false 5861 * 5862 * Context: Any. 5863 */ 5864 5865 static int 5866 sd_has_max_chs_vals(struct ipart *fdp) 5867 { 5868 return ((fdp->begcyl == LBA_MAX_CYL) && 5869 (fdp->beghead == LBA_MAX_HEAD) && 5870 (fdp->begsect == LBA_MAX_SECT) && 5871 (fdp->endcyl == LBA_MAX_CYL) && 5872 (fdp->endhead == LBA_MAX_HEAD) && 5873 (fdp->endsect == LBA_MAX_SECT)); 5874 } 5875 #endif 5876 5877 5878 /* 5879 * Function: sd_inq_fill 5880 * 5881 * Description: Print a piece of inquiry data, cleaned up for non-printable 5882 * characters and stopping at the first space character after 5883 * the beginning of the passed string; 5884 * 5885 * Arguments: p - source string 5886 * l - maximum length to copy 5887 * s - destination string 5888 * 5889 * Context: Any. 5890 */ 5891 5892 static void 5893 sd_inq_fill(char *p, int l, char *s) 5894 { 5895 unsigned i = 0; 5896 char c; 5897 5898 while (i++ < l) { 5899 if ((c = *p++) < ' ' || c >= 0x7F) { 5900 c = '*'; 5901 } else if (i != 1 && c == ' ') { 5902 break; 5903 } 5904 *s++ = c; 5905 } 5906 *s++ = 0; 5907 } 5908 5909 5910 /* 5911 * Function: sd_register_devid 5912 * 5913 * Description: This routine will obtain the device id information from the 5914 * target, obtain the serial number, and register the device 5915 * id with the ddi framework. 5916 * 5917 * Arguments: devi - the system's dev_info_t for the device. 5918 * un - driver soft state (unit) structure 5919 * reservation_flag - indicates if a reservation conflict 5920 * occurred during attach 5921 * 5922 * Context: Kernel Thread 5923 */ 5924 static void 5925 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 5926 { 5927 int rval = 0; 5928 uchar_t *inq80 = NULL; 5929 size_t inq80_len = MAX_INQUIRY_SIZE; 5930 size_t inq80_resid = 0; 5931 uchar_t *inq83 = NULL; 5932 size_t inq83_len = MAX_INQUIRY_SIZE; 5933 size_t inq83_resid = 0; 5934 5935 ASSERT(un != NULL); 5936 ASSERT(mutex_owned(SD_MUTEX(un))); 5937 ASSERT((SD_DEVINFO(un)) == devi); 5938 5939 /* 5940 * This is the case of antiquated Sun disk drives that have the 5941 * FAB_DEVID property set in the disk_table. These drives 5942 * manage the devid's by storing them in last 2 available sectors 5943 * on the drive and have them fabricated by the ddi layer by calling 5944 * ddi_devid_init and passing the DEVID_FAB flag. 5945 */ 5946 if (un->un_f_opt_fab_devid == TRUE) { 5947 /* 5948 * Depending on EINVAL isn't reliable, since a reserved disk 5949 * may result in invalid geometry, so check to make sure a 5950 * reservation conflict did not occur during attach. 5951 */ 5952 if ((sd_get_devid(un) == EINVAL) && 5953 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5954 /* 5955 * The devid is invalid AND there is no reservation 5956 * conflict. Fabricate a new devid. 5957 */ 5958 (void) sd_create_devid(un); 5959 } 5960 5961 /* Register the devid if it exists */ 5962 if (un->un_devid != NULL) { 5963 (void) ddi_devid_register(SD_DEVINFO(un), 5964 un->un_devid); 5965 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5966 "sd_register_devid: Devid Fabricated\n"); 5967 } 5968 return; 5969 } 5970 5971 /* 5972 * We check the availibility of the World Wide Name (0x83) and Unit 5973 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5974 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5975 * 0x83 is availible, that is the best choice. Our next choice is 5976 * 0x80. If neither are availible, we munge the devid from the device 5977 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5978 * to fabricate a devid for non-Sun qualified disks. 5979 */ 5980 if (sd_check_vpd_page_support(un) == 0) { 5981 /* collect page 80 data if available */ 5982 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5983 5984 mutex_exit(SD_MUTEX(un)); 5985 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5986 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 5987 0x01, 0x80, &inq80_resid); 5988 5989 if (rval != 0) { 5990 kmem_free(inq80, inq80_len); 5991 inq80 = NULL; 5992 inq80_len = 0; 5993 } 5994 mutex_enter(SD_MUTEX(un)); 5995 } 5996 5997 /* collect page 83 data if available */ 5998 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5999 mutex_exit(SD_MUTEX(un)); 6000 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6001 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6002 0x01, 0x83, &inq83_resid); 6003 6004 if (rval != 0) { 6005 kmem_free(inq83, inq83_len); 6006 inq83 = NULL; 6007 inq83_len = 0; 6008 } 6009 mutex_enter(SD_MUTEX(un)); 6010 } 6011 } 6012 6013 /* encode best devid possible based on data available */ 6014 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6015 (char *)ddi_driver_name(SD_DEVINFO(un)), 6016 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6017 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6018 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6019 6020 /* devid successfully encoded, register devid */ 6021 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6022 6023 } else { 6024 /* 6025 * Unable to encode a devid based on data available. 6026 * This is not a Sun qualified disk. Older Sun disk 6027 * drives that have the SD_FAB_DEVID property 6028 * set in the disk_table and non Sun qualified 6029 * disks are treated in the same manner. These 6030 * drives manage the devid's by storing them in 6031 * last 2 available sectors on the drive and 6032 * have them fabricated by the ddi layer by 6033 * calling ddi_devid_init and passing the 6034 * DEVID_FAB flag. 6035 * Create a fabricate devid only if there's no 6036 * fabricate devid existed. 6037 */ 6038 if (sd_get_devid(un) == EINVAL) { 6039 (void) sd_create_devid(un); 6040 un->un_f_opt_fab_devid = TRUE; 6041 } 6042 6043 /* Register the devid if it exists */ 6044 if (un->un_devid != NULL) { 6045 (void) ddi_devid_register(SD_DEVINFO(un), 6046 un->un_devid); 6047 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6048 "sd_register_devid: devid fabricated using " 6049 "ddi framework\n"); 6050 } 6051 } 6052 6053 /* clean up resources */ 6054 if (inq80 != NULL) { 6055 kmem_free(inq80, inq80_len); 6056 } 6057 if (inq83 != NULL) { 6058 kmem_free(inq83, inq83_len); 6059 } 6060 } 6061 6062 static daddr_t 6063 sd_get_devid_block(struct sd_lun *un) 6064 { 6065 daddr_t spc, blk, head, cyl; 6066 6067 if (un->un_blockcount <= DK_MAX_BLOCKS) { 6068 /* this geometry doesn't allow us to write a devid */ 6069 if (un->un_g.dkg_acyl < 2) { 6070 return (-1); 6071 } 6072 6073 /* 6074 * Subtract 2 guarantees that the next to last cylinder 6075 * is used 6076 */ 6077 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6078 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6079 head = un->un_g.dkg_nhead - 1; 6080 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6081 (head * un->un_g.dkg_nsect) + 1; 6082 } else { 6083 if (un->un_reserved != -1) { 6084 blk = un->un_map[un->un_reserved].dkl_cylno + 1; 6085 } else { 6086 return (-1); 6087 } 6088 } 6089 return (blk); 6090 } 6091 6092 /* 6093 * Function: sd_get_devid 6094 * 6095 * Description: This routine will return 0 if a valid device id has been 6096 * obtained from the target and stored in the soft state. If a 6097 * valid device id has not been previously read and stored, a 6098 * read attempt will be made. 6099 * 6100 * Arguments: un - driver soft state (unit) structure 6101 * 6102 * Return Code: 0 if we successfully get the device id 6103 * 6104 * Context: Kernel Thread 6105 */ 6106 6107 static int 6108 sd_get_devid(struct sd_lun *un) 6109 { 6110 struct dk_devid *dkdevid; 6111 ddi_devid_t tmpid; 6112 uint_t *ip; 6113 size_t sz; 6114 daddr_t blk; 6115 int status; 6116 int chksum; 6117 int i; 6118 size_t buffer_size; 6119 6120 ASSERT(un != NULL); 6121 ASSERT(mutex_owned(SD_MUTEX(un))); 6122 6123 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6124 un); 6125 6126 if (un->un_devid != NULL) { 6127 return (0); 6128 } 6129 6130 blk = sd_get_devid_block(un); 6131 if (blk < 0) 6132 return (EINVAL); 6133 6134 /* 6135 * Read and verify device id, stored in the reserved cylinders at the 6136 * end of the disk. Backup label is on the odd sectors of the last 6137 * track of the last cylinder. Device id will be on track of the next 6138 * to last cylinder. 6139 */ 6140 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6141 mutex_exit(SD_MUTEX(un)); 6142 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6143 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6144 SD_PATH_DIRECT); 6145 if (status != 0) { 6146 goto error; 6147 } 6148 6149 /* Validate the revision */ 6150 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6151 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6152 status = EINVAL; 6153 goto error; 6154 } 6155 6156 /* Calculate the checksum */ 6157 chksum = 0; 6158 ip = (uint_t *)dkdevid; 6159 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6160 i++) { 6161 chksum ^= ip[i]; 6162 } 6163 6164 /* Compare the checksums */ 6165 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6166 status = EINVAL; 6167 goto error; 6168 } 6169 6170 /* Validate the device id */ 6171 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6172 status = EINVAL; 6173 goto error; 6174 } 6175 6176 /* 6177 * Store the device id in the driver soft state 6178 */ 6179 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6180 tmpid = kmem_alloc(sz, KM_SLEEP); 6181 6182 mutex_enter(SD_MUTEX(un)); 6183 6184 un->un_devid = tmpid; 6185 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6186 6187 kmem_free(dkdevid, buffer_size); 6188 6189 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6190 6191 return (status); 6192 error: 6193 mutex_enter(SD_MUTEX(un)); 6194 kmem_free(dkdevid, buffer_size); 6195 return (status); 6196 } 6197 6198 6199 /* 6200 * Function: sd_create_devid 6201 * 6202 * Description: This routine will fabricate the device id and write it 6203 * to the disk. 6204 * 6205 * Arguments: un - driver soft state (unit) structure 6206 * 6207 * Return Code: value of the fabricated device id 6208 * 6209 * Context: Kernel Thread 6210 */ 6211 6212 static ddi_devid_t 6213 sd_create_devid(struct sd_lun *un) 6214 { 6215 ASSERT(un != NULL); 6216 6217 /* Fabricate the devid */ 6218 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6219 == DDI_FAILURE) { 6220 return (NULL); 6221 } 6222 6223 /* Write the devid to disk */ 6224 if (sd_write_deviceid(un) != 0) { 6225 ddi_devid_free(un->un_devid); 6226 un->un_devid = NULL; 6227 } 6228 6229 return (un->un_devid); 6230 } 6231 6232 6233 /* 6234 * Function: sd_write_deviceid 6235 * 6236 * Description: This routine will write the device id to the disk 6237 * reserved sector. 6238 * 6239 * Arguments: un - driver soft state (unit) structure 6240 * 6241 * Return Code: EINVAL 6242 * value returned by sd_send_scsi_cmd 6243 * 6244 * Context: Kernel Thread 6245 */ 6246 6247 static int 6248 sd_write_deviceid(struct sd_lun *un) 6249 { 6250 struct dk_devid *dkdevid; 6251 daddr_t blk; 6252 uint_t *ip, chksum; 6253 int status; 6254 int i; 6255 6256 ASSERT(mutex_owned(SD_MUTEX(un))); 6257 6258 blk = sd_get_devid_block(un); 6259 if (blk < 0) 6260 return (-1); 6261 mutex_exit(SD_MUTEX(un)); 6262 6263 /* Allocate the buffer */ 6264 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6265 6266 /* Fill in the revision */ 6267 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6268 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6269 6270 /* Copy in the device id */ 6271 mutex_enter(SD_MUTEX(un)); 6272 bcopy(un->un_devid, &dkdevid->dkd_devid, 6273 ddi_devid_sizeof(un->un_devid)); 6274 mutex_exit(SD_MUTEX(un)); 6275 6276 /* Calculate the checksum */ 6277 chksum = 0; 6278 ip = (uint_t *)dkdevid; 6279 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6280 i++) { 6281 chksum ^= ip[i]; 6282 } 6283 6284 /* Fill-in checksum */ 6285 DKD_FORMCHKSUM(chksum, dkdevid); 6286 6287 /* Write the reserved sector */ 6288 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6289 SD_PATH_DIRECT); 6290 6291 kmem_free(dkdevid, un->un_sys_blocksize); 6292 6293 mutex_enter(SD_MUTEX(un)); 6294 return (status); 6295 } 6296 6297 6298 /* 6299 * Function: sd_check_vpd_page_support 6300 * 6301 * Description: This routine sends an inquiry command with the EVPD bit set and 6302 * a page code of 0x00 to the device. It is used to determine which 6303 * vital product pages are availible to find the devid. We are 6304 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6305 * device does not support that command. 6306 * 6307 * Arguments: un - driver soft state (unit) structure 6308 * 6309 * Return Code: 0 - success 6310 * 1 - check condition 6311 * 6312 * Context: This routine can sleep. 6313 */ 6314 6315 static int 6316 sd_check_vpd_page_support(struct sd_lun *un) 6317 { 6318 uchar_t *page_list = NULL; 6319 uchar_t page_length = 0xff; /* Use max possible length */ 6320 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6321 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6322 int rval = 0; 6323 int counter; 6324 6325 ASSERT(un != NULL); 6326 ASSERT(mutex_owned(SD_MUTEX(un))); 6327 6328 mutex_exit(SD_MUTEX(un)); 6329 6330 /* 6331 * We'll set the page length to the maximum to save figuring it out 6332 * with an additional call. 6333 */ 6334 page_list = kmem_zalloc(page_length, KM_SLEEP); 6335 6336 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6337 page_code, NULL); 6338 6339 mutex_enter(SD_MUTEX(un)); 6340 6341 /* 6342 * Now we must validate that the device accepted the command, as some 6343 * drives do not support it. If the drive does support it, we will 6344 * return 0, and the supported pages will be in un_vpd_page_mask. If 6345 * not, we return -1. 6346 */ 6347 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6348 /* Loop to find one of the 2 pages we need */ 6349 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6350 6351 /* 6352 * Pages are returned in ascending order, and 0x83 is what we 6353 * are hoping for. 6354 */ 6355 while ((page_list[counter] <= 0x83) && 6356 (counter <= (page_list[VPD_PAGE_LENGTH] + 6357 VPD_HEAD_OFFSET))) { 6358 /* 6359 * Add 3 because page_list[3] is the number of 6360 * pages minus 3 6361 */ 6362 6363 switch (page_list[counter]) { 6364 case 0x00: 6365 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6366 break; 6367 case 0x80: 6368 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6369 break; 6370 case 0x81: 6371 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6372 break; 6373 case 0x82: 6374 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6375 break; 6376 case 0x83: 6377 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6378 break; 6379 } 6380 counter++; 6381 } 6382 6383 } else { 6384 rval = -1; 6385 6386 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6387 "sd_check_vpd_page_support: This drive does not implement " 6388 "VPD pages.\n"); 6389 } 6390 6391 kmem_free(page_list, page_length); 6392 6393 return (rval); 6394 } 6395 6396 6397 /* 6398 * Function: sd_setup_pm 6399 * 6400 * Description: Initialize Power Management on the device 6401 * 6402 * Context: Kernel Thread 6403 */ 6404 6405 static void 6406 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6407 { 6408 uint_t log_page_size; 6409 uchar_t *log_page_data; 6410 int rval; 6411 6412 /* 6413 * Since we are called from attach, holding a mutex for 6414 * un is unnecessary. Because some of the routines called 6415 * from here require SD_MUTEX to not be held, assert this 6416 * right up front. 6417 */ 6418 ASSERT(!mutex_owned(SD_MUTEX(un))); 6419 /* 6420 * Since the sd device does not have the 'reg' property, 6421 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6422 * The following code is to tell cpr that this device 6423 * DOES need to be suspended and resumed. 6424 */ 6425 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6426 "pm-hardware-state", "needs-suspend-resume"); 6427 6428 /* 6429 * This complies with the new power management framework 6430 * for certain desktop machines. Create the pm_components 6431 * property as a string array property. 6432 */ 6433 if (un->un_f_pm_supported) { 6434 /* 6435 * not all devices have a motor, try it first. 6436 * some devices may return ILLEGAL REQUEST, some 6437 * will hang 6438 * The following START_STOP_UNIT is used to check if target 6439 * device has a motor. 6440 */ 6441 un->un_f_start_stop_supported = TRUE; 6442 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6443 SD_PATH_DIRECT) != 0) { 6444 un->un_f_start_stop_supported = FALSE; 6445 } 6446 6447 /* 6448 * create pm properties anyways otherwise the parent can't 6449 * go to sleep 6450 */ 6451 (void) sd_create_pm_components(devi, un); 6452 un->un_f_pm_is_enabled = TRUE; 6453 return; 6454 } 6455 6456 if (!un->un_f_log_sense_supported) { 6457 un->un_power_level = SD_SPINDLE_ON; 6458 un->un_f_pm_is_enabled = FALSE; 6459 return; 6460 } 6461 6462 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6463 6464 #ifdef SDDEBUG 6465 if (sd_force_pm_supported) { 6466 /* Force a successful result */ 6467 rval = 1; 6468 } 6469 #endif 6470 6471 /* 6472 * If the start-stop cycle counter log page is not supported 6473 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6474 * then we should not create the pm_components property. 6475 */ 6476 if (rval == -1) { 6477 /* 6478 * Error. 6479 * Reading log sense failed, most likely this is 6480 * an older drive that does not support log sense. 6481 * If this fails auto-pm is not supported. 6482 */ 6483 un->un_power_level = SD_SPINDLE_ON; 6484 un->un_f_pm_is_enabled = FALSE; 6485 6486 } else if (rval == 0) { 6487 /* 6488 * Page not found. 6489 * The start stop cycle counter is implemented as page 6490 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6491 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6492 */ 6493 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6494 /* 6495 * Page found, use this one. 6496 */ 6497 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6498 un->un_f_pm_is_enabled = TRUE; 6499 } else { 6500 /* 6501 * Error or page not found. 6502 * auto-pm is not supported for this device. 6503 */ 6504 un->un_power_level = SD_SPINDLE_ON; 6505 un->un_f_pm_is_enabled = FALSE; 6506 } 6507 } else { 6508 /* 6509 * Page found, use it. 6510 */ 6511 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6512 un->un_f_pm_is_enabled = TRUE; 6513 } 6514 6515 6516 if (un->un_f_pm_is_enabled == TRUE) { 6517 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6518 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6519 6520 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6521 log_page_size, un->un_start_stop_cycle_page, 6522 0x01, 0, SD_PATH_DIRECT); 6523 #ifdef SDDEBUG 6524 if (sd_force_pm_supported) { 6525 /* Force a successful result */ 6526 rval = 0; 6527 } 6528 #endif 6529 6530 /* 6531 * If the Log sense for Page( Start/stop cycle counter page) 6532 * succeeds, then power managment is supported and we can 6533 * enable auto-pm. 6534 */ 6535 if (rval == 0) { 6536 (void) sd_create_pm_components(devi, un); 6537 } else { 6538 un->un_power_level = SD_SPINDLE_ON; 6539 un->un_f_pm_is_enabled = FALSE; 6540 } 6541 6542 kmem_free(log_page_data, log_page_size); 6543 } 6544 } 6545 6546 6547 /* 6548 * Function: sd_create_pm_components 6549 * 6550 * Description: Initialize PM property. 6551 * 6552 * Context: Kernel thread context 6553 */ 6554 6555 static void 6556 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6557 { 6558 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6559 6560 ASSERT(!mutex_owned(SD_MUTEX(un))); 6561 6562 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6563 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6564 /* 6565 * When components are initially created they are idle, 6566 * power up any non-removables. 6567 * Note: the return value of pm_raise_power can't be used 6568 * for determining if PM should be enabled for this device. 6569 * Even if you check the return values and remove this 6570 * property created above, the PM framework will not honor the 6571 * change after the first call to pm_raise_power. Hence, 6572 * removal of that property does not help if pm_raise_power 6573 * fails. In the case of removable media, the start/stop 6574 * will fail if the media is not present. 6575 */ 6576 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6577 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6578 mutex_enter(SD_MUTEX(un)); 6579 un->un_power_level = SD_SPINDLE_ON; 6580 mutex_enter(&un->un_pm_mutex); 6581 /* Set to on and not busy. */ 6582 un->un_pm_count = 0; 6583 } else { 6584 mutex_enter(SD_MUTEX(un)); 6585 un->un_power_level = SD_SPINDLE_OFF; 6586 mutex_enter(&un->un_pm_mutex); 6587 /* Set to off. */ 6588 un->un_pm_count = -1; 6589 } 6590 mutex_exit(&un->un_pm_mutex); 6591 mutex_exit(SD_MUTEX(un)); 6592 } else { 6593 un->un_power_level = SD_SPINDLE_ON; 6594 un->un_f_pm_is_enabled = FALSE; 6595 } 6596 } 6597 6598 6599 /* 6600 * Function: sd_ddi_suspend 6601 * 6602 * Description: Performs system power-down operations. This includes 6603 * setting the drive state to indicate its suspended so 6604 * that no new commands will be accepted. Also, wait for 6605 * all commands that are in transport or queued to a timer 6606 * for retry to complete. All timeout threads are cancelled. 6607 * 6608 * Return Code: DDI_FAILURE or DDI_SUCCESS 6609 * 6610 * Context: Kernel thread context 6611 */ 6612 6613 static int 6614 sd_ddi_suspend(dev_info_t *devi) 6615 { 6616 struct sd_lun *un; 6617 clock_t wait_cmds_complete; 6618 6619 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6620 if (un == NULL) { 6621 return (DDI_FAILURE); 6622 } 6623 6624 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6625 6626 mutex_enter(SD_MUTEX(un)); 6627 6628 /* Return success if the device is already suspended. */ 6629 if (un->un_state == SD_STATE_SUSPENDED) { 6630 mutex_exit(SD_MUTEX(un)); 6631 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6632 "device already suspended, exiting\n"); 6633 return (DDI_SUCCESS); 6634 } 6635 6636 /* Return failure if the device is being used by HA */ 6637 if (un->un_resvd_status & 6638 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6639 mutex_exit(SD_MUTEX(un)); 6640 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6641 "device in use by HA, exiting\n"); 6642 return (DDI_FAILURE); 6643 } 6644 6645 /* 6646 * Return failure if the device is in a resource wait 6647 * or power changing state. 6648 */ 6649 if ((un->un_state == SD_STATE_RWAIT) || 6650 (un->un_state == SD_STATE_PM_CHANGING)) { 6651 mutex_exit(SD_MUTEX(un)); 6652 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6653 "device in resource wait state, exiting\n"); 6654 return (DDI_FAILURE); 6655 } 6656 6657 6658 un->un_save_state = un->un_last_state; 6659 New_state(un, SD_STATE_SUSPENDED); 6660 6661 /* 6662 * Wait for all commands that are in transport or queued to a timer 6663 * for retry to complete. 6664 * 6665 * While waiting, no new commands will be accepted or sent because of 6666 * the new state we set above. 6667 * 6668 * Wait till current operation has completed. If we are in the resource 6669 * wait state (with an intr outstanding) then we need to wait till the 6670 * intr completes and starts the next cmd. We want to wait for 6671 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6672 */ 6673 wait_cmds_complete = ddi_get_lbolt() + 6674 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6675 6676 while (un->un_ncmds_in_transport != 0) { 6677 /* 6678 * Fail if commands do not finish in the specified time. 6679 */ 6680 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6681 wait_cmds_complete) == -1) { 6682 /* 6683 * Undo the state changes made above. Everything 6684 * must go back to it's original value. 6685 */ 6686 Restore_state(un); 6687 un->un_last_state = un->un_save_state; 6688 /* Wake up any threads that might be waiting. */ 6689 cv_broadcast(&un->un_suspend_cv); 6690 mutex_exit(SD_MUTEX(un)); 6691 SD_ERROR(SD_LOG_IO_PM, un, 6692 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6693 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6694 return (DDI_FAILURE); 6695 } 6696 } 6697 6698 /* 6699 * Cancel SCSI watch thread and timeouts, if any are active 6700 */ 6701 6702 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6703 opaque_t temp_token = un->un_swr_token; 6704 mutex_exit(SD_MUTEX(un)); 6705 scsi_watch_suspend(temp_token); 6706 mutex_enter(SD_MUTEX(un)); 6707 } 6708 6709 if (un->un_reset_throttle_timeid != NULL) { 6710 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6711 un->un_reset_throttle_timeid = NULL; 6712 mutex_exit(SD_MUTEX(un)); 6713 (void) untimeout(temp_id); 6714 mutex_enter(SD_MUTEX(un)); 6715 } 6716 6717 if (un->un_dcvb_timeid != NULL) { 6718 timeout_id_t temp_id = un->un_dcvb_timeid; 6719 un->un_dcvb_timeid = NULL; 6720 mutex_exit(SD_MUTEX(un)); 6721 (void) untimeout(temp_id); 6722 mutex_enter(SD_MUTEX(un)); 6723 } 6724 6725 mutex_enter(&un->un_pm_mutex); 6726 if (un->un_pm_timeid != NULL) { 6727 timeout_id_t temp_id = un->un_pm_timeid; 6728 un->un_pm_timeid = NULL; 6729 mutex_exit(&un->un_pm_mutex); 6730 mutex_exit(SD_MUTEX(un)); 6731 (void) untimeout(temp_id); 6732 mutex_enter(SD_MUTEX(un)); 6733 } else { 6734 mutex_exit(&un->un_pm_mutex); 6735 } 6736 6737 if (un->un_retry_timeid != NULL) { 6738 timeout_id_t temp_id = un->un_retry_timeid; 6739 un->un_retry_timeid = NULL; 6740 mutex_exit(SD_MUTEX(un)); 6741 (void) untimeout(temp_id); 6742 mutex_enter(SD_MUTEX(un)); 6743 } 6744 6745 if (un->un_direct_priority_timeid != NULL) { 6746 timeout_id_t temp_id = un->un_direct_priority_timeid; 6747 un->un_direct_priority_timeid = NULL; 6748 mutex_exit(SD_MUTEX(un)); 6749 (void) untimeout(temp_id); 6750 mutex_enter(SD_MUTEX(un)); 6751 } 6752 6753 if (un->un_f_is_fibre == TRUE) { 6754 /* 6755 * Remove callbacks for insert and remove events 6756 */ 6757 if (un->un_insert_event != NULL) { 6758 mutex_exit(SD_MUTEX(un)); 6759 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6760 mutex_enter(SD_MUTEX(un)); 6761 un->un_insert_event = NULL; 6762 } 6763 6764 if (un->un_remove_event != NULL) { 6765 mutex_exit(SD_MUTEX(un)); 6766 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6767 mutex_enter(SD_MUTEX(un)); 6768 un->un_remove_event = NULL; 6769 } 6770 } 6771 6772 mutex_exit(SD_MUTEX(un)); 6773 6774 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6775 6776 return (DDI_SUCCESS); 6777 } 6778 6779 6780 /* 6781 * Function: sd_ddi_pm_suspend 6782 * 6783 * Description: Set the drive state to low power. 6784 * Someone else is required to actually change the drive 6785 * power level. 6786 * 6787 * Arguments: un - driver soft state (unit) structure 6788 * 6789 * Return Code: DDI_FAILURE or DDI_SUCCESS 6790 * 6791 * Context: Kernel thread context 6792 */ 6793 6794 static int 6795 sd_ddi_pm_suspend(struct sd_lun *un) 6796 { 6797 ASSERT(un != NULL); 6798 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 6799 6800 ASSERT(!mutex_owned(SD_MUTEX(un))); 6801 mutex_enter(SD_MUTEX(un)); 6802 6803 /* 6804 * Exit if power management is not enabled for this device, or if 6805 * the device is being used by HA. 6806 */ 6807 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6808 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6809 mutex_exit(SD_MUTEX(un)); 6810 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 6811 return (DDI_SUCCESS); 6812 } 6813 6814 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 6815 un->un_ncmds_in_driver); 6816 6817 /* 6818 * See if the device is not busy, ie.: 6819 * - we have no commands in the driver for this device 6820 * - not waiting for resources 6821 */ 6822 if ((un->un_ncmds_in_driver == 0) && 6823 (un->un_state != SD_STATE_RWAIT)) { 6824 /* 6825 * The device is not busy, so it is OK to go to low power state. 6826 * Indicate low power, but rely on someone else to actually 6827 * change it. 6828 */ 6829 mutex_enter(&un->un_pm_mutex); 6830 un->un_pm_count = -1; 6831 mutex_exit(&un->un_pm_mutex); 6832 un->un_power_level = SD_SPINDLE_OFF; 6833 } 6834 6835 mutex_exit(SD_MUTEX(un)); 6836 6837 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 6838 6839 return (DDI_SUCCESS); 6840 } 6841 6842 6843 /* 6844 * Function: sd_ddi_resume 6845 * 6846 * Description: Performs system power-up operations.. 6847 * 6848 * Return Code: DDI_SUCCESS 6849 * DDI_FAILURE 6850 * 6851 * Context: Kernel thread context 6852 */ 6853 6854 static int 6855 sd_ddi_resume(dev_info_t *devi) 6856 { 6857 struct sd_lun *un; 6858 6859 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6860 if (un == NULL) { 6861 return (DDI_FAILURE); 6862 } 6863 6864 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6865 6866 mutex_enter(SD_MUTEX(un)); 6867 Restore_state(un); 6868 6869 /* 6870 * Restore the state which was saved to give the 6871 * the right state in un_last_state 6872 */ 6873 un->un_last_state = un->un_save_state; 6874 /* 6875 * Note: throttle comes back at full. 6876 * Also note: this MUST be done before calling pm_raise_power 6877 * otherwise the system can get hung in biowait. The scenario where 6878 * this'll happen is under cpr suspend. Writing of the system 6879 * state goes through sddump, which writes 0 to un_throttle. If 6880 * writing the system state then fails, example if the partition is 6881 * too small, then cpr attempts a resume. If throttle isn't restored 6882 * from the saved value until after calling pm_raise_power then 6883 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6884 * in biowait. 6885 */ 6886 un->un_throttle = un->un_saved_throttle; 6887 6888 /* 6889 * The chance of failure is very rare as the only command done in power 6890 * entry point is START command when you transition from 0->1 or 6891 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6892 * which suspend was done. Ignore the return value as the resume should 6893 * not be failed. In the case of removable media the media need not be 6894 * inserted and hence there is a chance that raise power will fail with 6895 * media not present. 6896 */ 6897 if (un->un_f_attach_spinup) { 6898 mutex_exit(SD_MUTEX(un)); 6899 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 6900 mutex_enter(SD_MUTEX(un)); 6901 } 6902 6903 /* 6904 * Don't broadcast to the suspend cv and therefore possibly 6905 * start I/O until after power has been restored. 6906 */ 6907 cv_broadcast(&un->un_suspend_cv); 6908 cv_broadcast(&un->un_state_cv); 6909 6910 /* restart thread */ 6911 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6912 scsi_watch_resume(un->un_swr_token); 6913 } 6914 6915 #if (defined(__fibre)) 6916 if (un->un_f_is_fibre == TRUE) { 6917 /* 6918 * Add callbacks for insert and remove events 6919 */ 6920 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6921 sd_init_event_callbacks(un); 6922 } 6923 } 6924 #endif 6925 6926 /* 6927 * Transport any pending commands to the target. 6928 * 6929 * If this is a low-activity device commands in queue will have to wait 6930 * until new commands come in, which may take awhile. Also, we 6931 * specifically don't check un_ncmds_in_transport because we know that 6932 * there really are no commands in progress after the unit was 6933 * suspended and we could have reached the throttle level, been 6934 * suspended, and have no new commands coming in for awhile. Highly 6935 * unlikely, but so is the low-activity disk scenario. 6936 */ 6937 ddi_xbuf_dispatch(un->un_xbuf_attr); 6938 6939 sd_start_cmds(un, NULL); 6940 mutex_exit(SD_MUTEX(un)); 6941 6942 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6943 6944 return (DDI_SUCCESS); 6945 } 6946 6947 6948 /* 6949 * Function: sd_ddi_pm_resume 6950 * 6951 * Description: Set the drive state to powered on. 6952 * Someone else is required to actually change the drive 6953 * power level. 6954 * 6955 * Arguments: un - driver soft state (unit) structure 6956 * 6957 * Return Code: DDI_SUCCESS 6958 * 6959 * Context: Kernel thread context 6960 */ 6961 6962 static int 6963 sd_ddi_pm_resume(struct sd_lun *un) 6964 { 6965 ASSERT(un != NULL); 6966 6967 ASSERT(!mutex_owned(SD_MUTEX(un))); 6968 mutex_enter(SD_MUTEX(un)); 6969 un->un_power_level = SD_SPINDLE_ON; 6970 6971 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6972 mutex_enter(&un->un_pm_mutex); 6973 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6974 un->un_pm_count++; 6975 ASSERT(un->un_pm_count == 0); 6976 /* 6977 * Note: no longer do the cv_broadcast on un_suspend_cv. The 6978 * un_suspend_cv is for a system resume, not a power management 6979 * device resume. (4297749) 6980 * cv_broadcast(&un->un_suspend_cv); 6981 */ 6982 } 6983 mutex_exit(&un->un_pm_mutex); 6984 mutex_exit(SD_MUTEX(un)); 6985 6986 return (DDI_SUCCESS); 6987 } 6988 6989 6990 /* 6991 * Function: sd_pm_idletimeout_handler 6992 * 6993 * Description: A timer routine that's active only while a device is busy. 6994 * The purpose is to extend slightly the pm framework's busy 6995 * view of the device to prevent busy/idle thrashing for 6996 * back-to-back commands. Do this by comparing the current time 6997 * to the time at which the last command completed and when the 6998 * difference is greater than sd_pm_idletime, call 6999 * pm_idle_component. In addition to indicating idle to the pm 7000 * framework, update the chain type to again use the internal pm 7001 * layers of the driver. 7002 * 7003 * Arguments: arg - driver soft state (unit) structure 7004 * 7005 * Context: Executes in a timeout(9F) thread context 7006 */ 7007 7008 static void 7009 sd_pm_idletimeout_handler(void *arg) 7010 { 7011 struct sd_lun *un = arg; 7012 7013 time_t now; 7014 7015 mutex_enter(&sd_detach_mutex); 7016 if (un->un_detach_count != 0) { 7017 /* Abort if the instance is detaching */ 7018 mutex_exit(&sd_detach_mutex); 7019 return; 7020 } 7021 mutex_exit(&sd_detach_mutex); 7022 7023 now = ddi_get_time(); 7024 /* 7025 * Grab both mutexes, in the proper order, since we're accessing 7026 * both PM and softstate variables. 7027 */ 7028 mutex_enter(SD_MUTEX(un)); 7029 mutex_enter(&un->un_pm_mutex); 7030 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7031 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7032 /* 7033 * Update the chain types. 7034 * This takes affect on the next new command received. 7035 */ 7036 if (un->un_f_non_devbsize_supported) { 7037 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7038 } else { 7039 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7040 } 7041 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7042 7043 SD_TRACE(SD_LOG_IO_PM, un, 7044 "sd_pm_idletimeout_handler: idling device\n"); 7045 (void) pm_idle_component(SD_DEVINFO(un), 0); 7046 un->un_pm_idle_timeid = NULL; 7047 } else { 7048 un->un_pm_idle_timeid = 7049 timeout(sd_pm_idletimeout_handler, un, 7050 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7051 } 7052 mutex_exit(&un->un_pm_mutex); 7053 mutex_exit(SD_MUTEX(un)); 7054 } 7055 7056 7057 /* 7058 * Function: sd_pm_timeout_handler 7059 * 7060 * Description: Callback to tell framework we are idle. 7061 * 7062 * Context: timeout(9f) thread context. 7063 */ 7064 7065 static void 7066 sd_pm_timeout_handler(void *arg) 7067 { 7068 struct sd_lun *un = arg; 7069 7070 (void) pm_idle_component(SD_DEVINFO(un), 0); 7071 mutex_enter(&un->un_pm_mutex); 7072 un->un_pm_timeid = NULL; 7073 mutex_exit(&un->un_pm_mutex); 7074 } 7075 7076 7077 /* 7078 * Function: sdpower 7079 * 7080 * Description: PM entry point. 7081 * 7082 * Return Code: DDI_SUCCESS 7083 * DDI_FAILURE 7084 * 7085 * Context: Kernel thread context 7086 */ 7087 7088 static int 7089 sdpower(dev_info_t *devi, int component, int level) 7090 { 7091 struct sd_lun *un; 7092 int instance; 7093 int rval = DDI_SUCCESS; 7094 uint_t i, log_page_size, maxcycles, ncycles; 7095 uchar_t *log_page_data; 7096 int log_sense_page; 7097 int medium_present; 7098 time_t intvlp; 7099 dev_t dev; 7100 struct pm_trans_data sd_pm_tran_data; 7101 uchar_t save_state; 7102 int sval; 7103 uchar_t state_before_pm; 7104 int got_semaphore_here; 7105 7106 instance = ddi_get_instance(devi); 7107 7108 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7109 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7110 component != 0) { 7111 return (DDI_FAILURE); 7112 } 7113 7114 dev = sd_make_device(SD_DEVINFO(un)); 7115 7116 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7117 7118 /* 7119 * Must synchronize power down with close. 7120 * Attempt to decrement/acquire the open/close semaphore, 7121 * but do NOT wait on it. If it's not greater than zero, 7122 * ie. it can't be decremented without waiting, then 7123 * someone else, either open or close, already has it 7124 * and the try returns 0. Use that knowledge here to determine 7125 * if it's OK to change the device power level. 7126 * Also, only increment it on exit if it was decremented, ie. gotten, 7127 * here. 7128 */ 7129 got_semaphore_here = sema_tryp(&un->un_semoclose); 7130 7131 mutex_enter(SD_MUTEX(un)); 7132 7133 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7134 un->un_ncmds_in_driver); 7135 7136 /* 7137 * If un_ncmds_in_driver is non-zero it indicates commands are 7138 * already being processed in the driver, or if the semaphore was 7139 * not gotten here it indicates an open or close is being processed. 7140 * At the same time somebody is requesting to go low power which 7141 * can't happen, therefore we need to return failure. 7142 */ 7143 if ((level == SD_SPINDLE_OFF) && 7144 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7145 mutex_exit(SD_MUTEX(un)); 7146 7147 if (got_semaphore_here != 0) { 7148 sema_v(&un->un_semoclose); 7149 } 7150 SD_TRACE(SD_LOG_IO_PM, un, 7151 "sdpower: exit, device has queued cmds.\n"); 7152 return (DDI_FAILURE); 7153 } 7154 7155 /* 7156 * if it is OFFLINE that means the disk is completely dead 7157 * in our case we have to put the disk in on or off by sending commands 7158 * Of course that will fail anyway so return back here. 7159 * 7160 * Power changes to a device that's OFFLINE or SUSPENDED 7161 * are not allowed. 7162 */ 7163 if ((un->un_state == SD_STATE_OFFLINE) || 7164 (un->un_state == SD_STATE_SUSPENDED)) { 7165 mutex_exit(SD_MUTEX(un)); 7166 7167 if (got_semaphore_here != 0) { 7168 sema_v(&un->un_semoclose); 7169 } 7170 SD_TRACE(SD_LOG_IO_PM, un, 7171 "sdpower: exit, device is off-line.\n"); 7172 return (DDI_FAILURE); 7173 } 7174 7175 /* 7176 * Change the device's state to indicate it's power level 7177 * is being changed. Do this to prevent a power off in the 7178 * middle of commands, which is especially bad on devices 7179 * that are really powered off instead of just spun down. 7180 */ 7181 state_before_pm = un->un_state; 7182 un->un_state = SD_STATE_PM_CHANGING; 7183 7184 mutex_exit(SD_MUTEX(un)); 7185 7186 /* 7187 * If "pm-capable" property is set to TRUE by HBA drivers, 7188 * bypass the following checking, otherwise, check the log 7189 * sense information for this device 7190 */ 7191 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7192 /* 7193 * Get the log sense information to understand whether the 7194 * the powercycle counts have gone beyond the threshhold. 7195 */ 7196 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7197 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7198 7199 mutex_enter(SD_MUTEX(un)); 7200 log_sense_page = un->un_start_stop_cycle_page; 7201 mutex_exit(SD_MUTEX(un)); 7202 7203 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7204 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7205 #ifdef SDDEBUG 7206 if (sd_force_pm_supported) { 7207 /* Force a successful result */ 7208 rval = 0; 7209 } 7210 #endif 7211 if (rval != 0) { 7212 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7213 "Log Sense Failed\n"); 7214 kmem_free(log_page_data, log_page_size); 7215 /* Cannot support power management on those drives */ 7216 7217 if (got_semaphore_here != 0) { 7218 sema_v(&un->un_semoclose); 7219 } 7220 /* 7221 * On exit put the state back to it's original value 7222 * and broadcast to anyone waiting for the power 7223 * change completion. 7224 */ 7225 mutex_enter(SD_MUTEX(un)); 7226 un->un_state = state_before_pm; 7227 cv_broadcast(&un->un_suspend_cv); 7228 mutex_exit(SD_MUTEX(un)); 7229 SD_TRACE(SD_LOG_IO_PM, un, 7230 "sdpower: exit, Log Sense Failed.\n"); 7231 return (DDI_FAILURE); 7232 } 7233 7234 /* 7235 * From the page data - Convert the essential information to 7236 * pm_trans_data 7237 */ 7238 maxcycles = 7239 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7240 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7241 7242 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7243 7244 ncycles = 7245 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7246 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7247 7248 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7249 7250 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7251 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7252 log_page_data[8+i]; 7253 } 7254 7255 kmem_free(log_page_data, log_page_size); 7256 7257 /* 7258 * Call pm_trans_check routine to get the Ok from 7259 * the global policy 7260 */ 7261 7262 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7263 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7264 7265 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7266 #ifdef SDDEBUG 7267 if (sd_force_pm_supported) { 7268 /* Force a successful result */ 7269 rval = 1; 7270 } 7271 #endif 7272 switch (rval) { 7273 case 0: 7274 /* 7275 * Not Ok to Power cycle or error in parameters passed 7276 * Would have given the advised time to consider power 7277 * cycle. Based on the new intvlp parameter we are 7278 * supposed to pretend we are busy so that pm framework 7279 * will never call our power entry point. Because of 7280 * that install a timeout handler and wait for the 7281 * recommended time to elapse so that power management 7282 * can be effective again. 7283 * 7284 * To effect this behavior, call pm_busy_component to 7285 * indicate to the framework this device is busy. 7286 * By not adjusting un_pm_count the rest of PM in 7287 * the driver will function normally, and independant 7288 * of this but because the framework is told the device 7289 * is busy it won't attempt powering down until it gets 7290 * a matching idle. The timeout handler sends this. 7291 * Note: sd_pm_entry can't be called here to do this 7292 * because sdpower may have been called as a result 7293 * of a call to pm_raise_power from within sd_pm_entry. 7294 * 7295 * If a timeout handler is already active then 7296 * don't install another. 7297 */ 7298 mutex_enter(&un->un_pm_mutex); 7299 if (un->un_pm_timeid == NULL) { 7300 un->un_pm_timeid = 7301 timeout(sd_pm_timeout_handler, 7302 un, intvlp * drv_usectohz(1000000)); 7303 mutex_exit(&un->un_pm_mutex); 7304 (void) pm_busy_component(SD_DEVINFO(un), 0); 7305 } else { 7306 mutex_exit(&un->un_pm_mutex); 7307 } 7308 if (got_semaphore_here != 0) { 7309 sema_v(&un->un_semoclose); 7310 } 7311 /* 7312 * On exit put the state back to it's original value 7313 * and broadcast to anyone waiting for the power 7314 * change completion. 7315 */ 7316 mutex_enter(SD_MUTEX(un)); 7317 un->un_state = state_before_pm; 7318 cv_broadcast(&un->un_suspend_cv); 7319 mutex_exit(SD_MUTEX(un)); 7320 7321 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7322 "trans check Failed, not ok to power cycle.\n"); 7323 return (DDI_FAILURE); 7324 7325 case -1: 7326 if (got_semaphore_here != 0) { 7327 sema_v(&un->un_semoclose); 7328 } 7329 /* 7330 * On exit put the state back to it's original value 7331 * and broadcast to anyone waiting for the power 7332 * change completion. 7333 */ 7334 mutex_enter(SD_MUTEX(un)); 7335 un->un_state = state_before_pm; 7336 cv_broadcast(&un->un_suspend_cv); 7337 mutex_exit(SD_MUTEX(un)); 7338 SD_TRACE(SD_LOG_IO_PM, un, 7339 "sdpower: exit, trans check command Failed.\n"); 7340 return (DDI_FAILURE); 7341 } 7342 } 7343 7344 if (level == SD_SPINDLE_OFF) { 7345 /* 7346 * Save the last state... if the STOP FAILS we need it 7347 * for restoring 7348 */ 7349 mutex_enter(SD_MUTEX(un)); 7350 save_state = un->un_last_state; 7351 /* 7352 * There must not be any cmds. getting processed 7353 * in the driver when we get here. Power to the 7354 * device is potentially going off. 7355 */ 7356 ASSERT(un->un_ncmds_in_driver == 0); 7357 mutex_exit(SD_MUTEX(un)); 7358 7359 /* 7360 * For now suspend the device completely before spindle is 7361 * turned off 7362 */ 7363 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7364 if (got_semaphore_here != 0) { 7365 sema_v(&un->un_semoclose); 7366 } 7367 /* 7368 * On exit put the state back to it's original value 7369 * and broadcast to anyone waiting for the power 7370 * change completion. 7371 */ 7372 mutex_enter(SD_MUTEX(un)); 7373 un->un_state = state_before_pm; 7374 cv_broadcast(&un->un_suspend_cv); 7375 mutex_exit(SD_MUTEX(un)); 7376 SD_TRACE(SD_LOG_IO_PM, un, 7377 "sdpower: exit, PM suspend Failed.\n"); 7378 return (DDI_FAILURE); 7379 } 7380 } 7381 7382 /* 7383 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7384 * close, or strategy. Dump no long uses this routine, it uses it's 7385 * own code so it can be done in polled mode. 7386 */ 7387 7388 medium_present = TRUE; 7389 7390 /* 7391 * When powering up, issue a TUR in case the device is at unit 7392 * attention. Don't do retries. Bypass the PM layer, otherwise 7393 * a deadlock on un_pm_busy_cv will occur. 7394 */ 7395 if (level == SD_SPINDLE_ON) { 7396 (void) sd_send_scsi_TEST_UNIT_READY(un, 7397 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7398 } 7399 7400 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7401 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7402 7403 sval = sd_send_scsi_START_STOP_UNIT(un, 7404 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7405 SD_PATH_DIRECT); 7406 /* Command failed, check for media present. */ 7407 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7408 medium_present = FALSE; 7409 } 7410 7411 /* 7412 * The conditions of interest here are: 7413 * if a spindle off with media present fails, 7414 * then restore the state and return an error. 7415 * else if a spindle on fails, 7416 * then return an error (there's no state to restore). 7417 * In all other cases we setup for the new state 7418 * and return success. 7419 */ 7420 switch (level) { 7421 case SD_SPINDLE_OFF: 7422 if ((medium_present == TRUE) && (sval != 0)) { 7423 /* The stop command from above failed */ 7424 rval = DDI_FAILURE; 7425 /* 7426 * The stop command failed, and we have media 7427 * present. Put the level back by calling the 7428 * sd_pm_resume() and set the state back to 7429 * it's previous value. 7430 */ 7431 (void) sd_ddi_pm_resume(un); 7432 mutex_enter(SD_MUTEX(un)); 7433 un->un_last_state = save_state; 7434 mutex_exit(SD_MUTEX(un)); 7435 break; 7436 } 7437 /* 7438 * The stop command from above succeeded. 7439 */ 7440 if (un->un_f_monitor_media_state) { 7441 /* 7442 * Terminate watch thread in case of removable media 7443 * devices going into low power state. This is as per 7444 * the requirements of pm framework, otherwise commands 7445 * will be generated for the device (through watch 7446 * thread), even when the device is in low power state. 7447 */ 7448 mutex_enter(SD_MUTEX(un)); 7449 un->un_f_watcht_stopped = FALSE; 7450 if (un->un_swr_token != NULL) { 7451 opaque_t temp_token = un->un_swr_token; 7452 un->un_f_watcht_stopped = TRUE; 7453 un->un_swr_token = NULL; 7454 mutex_exit(SD_MUTEX(un)); 7455 (void) scsi_watch_request_terminate(temp_token, 7456 SCSI_WATCH_TERMINATE_WAIT); 7457 } else { 7458 mutex_exit(SD_MUTEX(un)); 7459 } 7460 } 7461 break; 7462 7463 default: /* The level requested is spindle on... */ 7464 /* 7465 * Legacy behavior: return success on a failed spinup 7466 * if there is no media in the drive. 7467 * Do this by looking at medium_present here. 7468 */ 7469 if ((sval != 0) && medium_present) { 7470 /* The start command from above failed */ 7471 rval = DDI_FAILURE; 7472 break; 7473 } 7474 /* 7475 * The start command from above succeeded 7476 * Resume the devices now that we have 7477 * started the disks 7478 */ 7479 (void) sd_ddi_pm_resume(un); 7480 7481 /* 7482 * Resume the watch thread since it was suspended 7483 * when the device went into low power mode. 7484 */ 7485 if (un->un_f_monitor_media_state) { 7486 mutex_enter(SD_MUTEX(un)); 7487 if (un->un_f_watcht_stopped == TRUE) { 7488 opaque_t temp_token; 7489 7490 un->un_f_watcht_stopped = FALSE; 7491 mutex_exit(SD_MUTEX(un)); 7492 temp_token = scsi_watch_request_submit( 7493 SD_SCSI_DEVP(un), 7494 sd_check_media_time, 7495 SENSE_LENGTH, sd_media_watch_cb, 7496 (caddr_t)dev); 7497 mutex_enter(SD_MUTEX(un)); 7498 un->un_swr_token = temp_token; 7499 } 7500 mutex_exit(SD_MUTEX(un)); 7501 } 7502 } 7503 if (got_semaphore_here != 0) { 7504 sema_v(&un->un_semoclose); 7505 } 7506 /* 7507 * On exit put the state back to it's original value 7508 * and broadcast to anyone waiting for the power 7509 * change completion. 7510 */ 7511 mutex_enter(SD_MUTEX(un)); 7512 un->un_state = state_before_pm; 7513 cv_broadcast(&un->un_suspend_cv); 7514 mutex_exit(SD_MUTEX(un)); 7515 7516 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7517 7518 return (rval); 7519 } 7520 7521 7522 7523 /* 7524 * Function: sdattach 7525 * 7526 * Description: Driver's attach(9e) entry point function. 7527 * 7528 * Arguments: devi - opaque device info handle 7529 * cmd - attach type 7530 * 7531 * Return Code: DDI_SUCCESS 7532 * DDI_FAILURE 7533 * 7534 * Context: Kernel thread context 7535 */ 7536 7537 static int 7538 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7539 { 7540 switch (cmd) { 7541 case DDI_ATTACH: 7542 return (sd_unit_attach(devi)); 7543 case DDI_RESUME: 7544 return (sd_ddi_resume(devi)); 7545 default: 7546 break; 7547 } 7548 return (DDI_FAILURE); 7549 } 7550 7551 7552 /* 7553 * Function: sddetach 7554 * 7555 * Description: Driver's detach(9E) entry point function. 7556 * 7557 * Arguments: devi - opaque device info handle 7558 * cmd - detach type 7559 * 7560 * Return Code: DDI_SUCCESS 7561 * DDI_FAILURE 7562 * 7563 * Context: Kernel thread context 7564 */ 7565 7566 static int 7567 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7568 { 7569 switch (cmd) { 7570 case DDI_DETACH: 7571 return (sd_unit_detach(devi)); 7572 case DDI_SUSPEND: 7573 return (sd_ddi_suspend(devi)); 7574 default: 7575 break; 7576 } 7577 return (DDI_FAILURE); 7578 } 7579 7580 7581 /* 7582 * Function: sd_sync_with_callback 7583 * 7584 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7585 * state while the callback routine is active. 7586 * 7587 * Arguments: un: softstate structure for the instance 7588 * 7589 * Context: Kernel thread context 7590 */ 7591 7592 static void 7593 sd_sync_with_callback(struct sd_lun *un) 7594 { 7595 ASSERT(un != NULL); 7596 7597 mutex_enter(SD_MUTEX(un)); 7598 7599 ASSERT(un->un_in_callback >= 0); 7600 7601 while (un->un_in_callback > 0) { 7602 mutex_exit(SD_MUTEX(un)); 7603 delay(2); 7604 mutex_enter(SD_MUTEX(un)); 7605 } 7606 7607 mutex_exit(SD_MUTEX(un)); 7608 } 7609 7610 /* 7611 * Function: sd_unit_attach 7612 * 7613 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7614 * the soft state structure for the device and performs 7615 * all necessary structure and device initializations. 7616 * 7617 * Arguments: devi: the system's dev_info_t for the device. 7618 * 7619 * Return Code: DDI_SUCCESS if attach is successful. 7620 * DDI_FAILURE if any part of the attach fails. 7621 * 7622 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7623 * Kernel thread context only. Can sleep. 7624 */ 7625 7626 static int 7627 sd_unit_attach(dev_info_t *devi) 7628 { 7629 struct scsi_device *devp; 7630 struct sd_lun *un; 7631 char *variantp; 7632 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7633 int instance; 7634 int rval; 7635 int wc_enabled; 7636 uint64_t capacity; 7637 uint_t lbasize; 7638 7639 /* 7640 * Retrieve the target driver's private data area. This was set 7641 * up by the HBA. 7642 */ 7643 devp = ddi_get_driver_private(devi); 7644 7645 /* 7646 * Since we have no idea what state things were left in by the last 7647 * user of the device, set up some 'default' settings, ie. turn 'em 7648 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7649 * Do this before the scsi_probe, which sends an inquiry. 7650 * This is a fix for bug (4430280). 7651 * Of special importance is wide-xfer. The drive could have been left 7652 * in wide transfer mode by the last driver to communicate with it, 7653 * this includes us. If that's the case, and if the following is not 7654 * setup properly or we don't re-negotiate with the drive prior to 7655 * transferring data to/from the drive, it causes bus parity errors, 7656 * data overruns, and unexpected interrupts. This first occurred when 7657 * the fix for bug (4378686) was made. 7658 */ 7659 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7660 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7661 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7662 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7663 7664 /* 7665 * Use scsi_probe() to issue an INQUIRY command to the device. 7666 * This call will allocate and fill in the scsi_inquiry structure 7667 * and point the sd_inq member of the scsi_device structure to it. 7668 * If the attach succeeds, then this memory will not be de-allocated 7669 * (via scsi_unprobe()) until the instance is detached. 7670 */ 7671 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7672 goto probe_failed; 7673 } 7674 7675 /* 7676 * Check the device type as specified in the inquiry data and 7677 * claim it if it is of a type that we support. 7678 */ 7679 switch (devp->sd_inq->inq_dtype) { 7680 case DTYPE_DIRECT: 7681 break; 7682 case DTYPE_RODIRECT: 7683 break; 7684 case DTYPE_OPTICAL: 7685 break; 7686 case DTYPE_NOTPRESENT: 7687 default: 7688 /* Unsupported device type; fail the attach. */ 7689 goto probe_failed; 7690 } 7691 7692 /* 7693 * Allocate the soft state structure for this unit. 7694 * 7695 * We rely upon this memory being set to all zeroes by 7696 * ddi_soft_state_zalloc(). We assume that any member of the 7697 * soft state structure that is not explicitly initialized by 7698 * this routine will have a value of zero. 7699 */ 7700 instance = ddi_get_instance(devp->sd_dev); 7701 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7702 goto probe_failed; 7703 } 7704 7705 /* 7706 * Retrieve a pointer to the newly-allocated soft state. 7707 * 7708 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7709 * was successful, unless something has gone horribly wrong and the 7710 * ddi's soft state internals are corrupt (in which case it is 7711 * probably better to halt here than just fail the attach....) 7712 */ 7713 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7714 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7715 instance); 7716 /*NOTREACHED*/ 7717 } 7718 7719 /* 7720 * Link the back ptr of the driver soft state to the scsi_device 7721 * struct for this lun. 7722 * Save a pointer to the softstate in the driver-private area of 7723 * the scsi_device struct. 7724 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7725 * we first set un->un_sd below. 7726 */ 7727 un->un_sd = devp; 7728 devp->sd_private = (opaque_t)un; 7729 7730 /* 7731 * The following must be after devp is stored in the soft state struct. 7732 */ 7733 #ifdef SDDEBUG 7734 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7735 "%s_unit_attach: un:0x%p instance:%d\n", 7736 ddi_driver_name(devi), un, instance); 7737 #endif 7738 7739 /* 7740 * Set up the device type and node type (for the minor nodes). 7741 * By default we assume that the device can at least support the 7742 * Common Command Set. Call it a CD-ROM if it reports itself 7743 * as a RODIRECT device. 7744 */ 7745 switch (devp->sd_inq->inq_dtype) { 7746 case DTYPE_RODIRECT: 7747 un->un_node_type = DDI_NT_CD_CHAN; 7748 un->un_ctype = CTYPE_CDROM; 7749 break; 7750 case DTYPE_OPTICAL: 7751 un->un_node_type = DDI_NT_BLOCK_CHAN; 7752 un->un_ctype = CTYPE_ROD; 7753 break; 7754 default: 7755 un->un_node_type = DDI_NT_BLOCK_CHAN; 7756 un->un_ctype = CTYPE_CCS; 7757 break; 7758 } 7759 7760 /* 7761 * Try to read the interconnect type from the HBA. 7762 * 7763 * Note: This driver is currently compiled as two binaries, a parallel 7764 * scsi version (sd) and a fibre channel version (ssd). All functional 7765 * differences are determined at compile time. In the future a single 7766 * binary will be provided and the inteconnect type will be used to 7767 * differentiate between fibre and parallel scsi behaviors. At that time 7768 * it will be necessary for all fibre channel HBAs to support this 7769 * property. 7770 * 7771 * set un_f_is_fiber to TRUE ( default fiber ) 7772 */ 7773 un->un_f_is_fibre = TRUE; 7774 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7775 case INTERCONNECT_SSA: 7776 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7777 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7778 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7779 break; 7780 case INTERCONNECT_PARALLEL: 7781 un->un_f_is_fibre = FALSE; 7782 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7783 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7784 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7785 break; 7786 case INTERCONNECT_FIBRE: 7787 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7788 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7789 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7790 break; 7791 case INTERCONNECT_FABRIC: 7792 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7793 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7794 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7795 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7796 break; 7797 default: 7798 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7799 /* 7800 * The HBA does not support the "interconnect-type" property 7801 * (or did not provide a recognized type). 7802 * 7803 * Note: This will be obsoleted when a single fibre channel 7804 * and parallel scsi driver is delivered. In the meantime the 7805 * interconnect type will be set to the platform default.If that 7806 * type is not parallel SCSI, it means that we should be 7807 * assuming "ssd" semantics. However, here this also means that 7808 * the FC HBA is not supporting the "interconnect-type" property 7809 * like we expect it to, so log this occurrence. 7810 */ 7811 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7812 if (!SD_IS_PARALLEL_SCSI(un)) { 7813 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7814 "sd_unit_attach: un:0x%p Assuming " 7815 "INTERCONNECT_FIBRE\n", un); 7816 } else { 7817 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7818 "sd_unit_attach: un:0x%p Assuming " 7819 "INTERCONNECT_PARALLEL\n", un); 7820 un->un_f_is_fibre = FALSE; 7821 } 7822 #else 7823 /* 7824 * Note: This source will be implemented when a single fibre 7825 * channel and parallel scsi driver is delivered. The default 7826 * will be to assume that if a device does not support the 7827 * "interconnect-type" property it is a parallel SCSI HBA and 7828 * we will set the interconnect type for parallel scsi. 7829 */ 7830 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7831 un->un_f_is_fibre = FALSE; 7832 #endif 7833 break; 7834 } 7835 7836 if (un->un_f_is_fibre == TRUE) { 7837 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7838 SCSI_VERSION_3) { 7839 switch (un->un_interconnect_type) { 7840 case SD_INTERCONNECT_FIBRE: 7841 case SD_INTERCONNECT_SSA: 7842 un->un_node_type = DDI_NT_BLOCK_WWN; 7843 break; 7844 default: 7845 break; 7846 } 7847 } 7848 } 7849 7850 /* 7851 * Initialize the Request Sense command for the target 7852 */ 7853 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7854 goto alloc_rqs_failed; 7855 } 7856 7857 /* 7858 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7859 * with seperate binary for sd and ssd. 7860 * 7861 * x86 has 1 binary, un_retry_count is set base on connection type. 7862 * The hardcoded values will go away when Sparc uses 1 binary 7863 * for sd and ssd. This hardcoded values need to match 7864 * SD_RETRY_COUNT in sddef.h 7865 * The value used is base on interconnect type. 7866 * fibre = 3, parallel = 5 7867 */ 7868 #if defined(__i386) || defined(__amd64) 7869 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7870 #else 7871 un->un_retry_count = SD_RETRY_COUNT; 7872 #endif 7873 7874 /* 7875 * Set the per disk retry count to the default number of retries 7876 * for disks and CDROMs. This value can be overridden by the 7877 * disk property list or an entry in sd.conf. 7878 */ 7879 un->un_notready_retry_count = 7880 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7881 : DISK_NOT_READY_RETRY_COUNT(un); 7882 7883 /* 7884 * Set the busy retry count to the default value of un_retry_count. 7885 * This can be overridden by entries in sd.conf or the device 7886 * config table. 7887 */ 7888 un->un_busy_retry_count = un->un_retry_count; 7889 7890 /* 7891 * Init the reset threshold for retries. This number determines 7892 * how many retries must be performed before a reset can be issued 7893 * (for certain error conditions). This can be overridden by entries 7894 * in sd.conf or the device config table. 7895 */ 7896 un->un_reset_retry_count = (un->un_retry_count / 2); 7897 7898 /* 7899 * Set the victim_retry_count to the default un_retry_count 7900 */ 7901 un->un_victim_retry_count = (2 * un->un_retry_count); 7902 7903 /* 7904 * Set the reservation release timeout to the default value of 7905 * 5 seconds. This can be overridden by entries in ssd.conf or the 7906 * device config table. 7907 */ 7908 un->un_reserve_release_time = 5; 7909 7910 /* 7911 * Set up the default maximum transfer size. Note that this may 7912 * get updated later in the attach, when setting up default wide 7913 * operations for disks. 7914 */ 7915 #if defined(__i386) || defined(__amd64) 7916 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7917 #else 7918 un->un_max_xfer_size = (uint_t)maxphys; 7919 #endif 7920 7921 /* 7922 * Get "allow bus device reset" property (defaults to "enabled" if 7923 * the property was not defined). This is to disable bus resets for 7924 * certain kinds of error recovery. Note: In the future when a run-time 7925 * fibre check is available the soft state flag should default to 7926 * enabled. 7927 */ 7928 if (un->un_f_is_fibre == TRUE) { 7929 un->un_f_allow_bus_device_reset = TRUE; 7930 } else { 7931 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7932 "allow-bus-device-reset", 1) != 0) { 7933 un->un_f_allow_bus_device_reset = TRUE; 7934 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7935 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 7936 un); 7937 } else { 7938 un->un_f_allow_bus_device_reset = FALSE; 7939 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7940 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 7941 un); 7942 } 7943 } 7944 7945 /* 7946 * Check if this is an ATAPI device. ATAPI devices use Group 1 7947 * Read/Write commands and Group 2 Mode Sense/Select commands. 7948 * 7949 * Note: The "obsolete" way of doing this is to check for the "atapi" 7950 * property. The new "variant" property with a value of "atapi" has been 7951 * introduced so that future 'variants' of standard SCSI behavior (like 7952 * atapi) could be specified by the underlying HBA drivers by supplying 7953 * a new value for the "variant" property, instead of having to define a 7954 * new property. 7955 */ 7956 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7957 un->un_f_cfg_is_atapi = TRUE; 7958 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7959 "sd_unit_attach: un:0x%p Atapi device\n", un); 7960 } 7961 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7962 &variantp) == DDI_PROP_SUCCESS) { 7963 if (strcmp(variantp, "atapi") == 0) { 7964 un->un_f_cfg_is_atapi = TRUE; 7965 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7966 "sd_unit_attach: un:0x%p Atapi device\n", un); 7967 } 7968 ddi_prop_free(variantp); 7969 } 7970 7971 un->un_cmd_timeout = SD_IO_TIME; 7972 7973 /* Info on current states, statuses, etc. (Updated frequently) */ 7974 un->un_state = SD_STATE_NORMAL; 7975 un->un_last_state = SD_STATE_NORMAL; 7976 7977 /* Control & status info for command throttling */ 7978 un->un_throttle = sd_max_throttle; 7979 un->un_saved_throttle = sd_max_throttle; 7980 un->un_min_throttle = sd_min_throttle; 7981 7982 if (un->un_f_is_fibre == TRUE) { 7983 un->un_f_use_adaptive_throttle = TRUE; 7984 } else { 7985 un->un_f_use_adaptive_throttle = FALSE; 7986 } 7987 7988 /* Removable media support. */ 7989 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7990 un->un_mediastate = DKIO_NONE; 7991 un->un_specified_mediastate = DKIO_NONE; 7992 7993 /* CVs for suspend/resume (PM or DR) */ 7994 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7995 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7996 7997 /* Power management support. */ 7998 un->un_power_level = SD_SPINDLE_UNINIT; 7999 8000 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8001 un->un_f_wcc_inprog = 0; 8002 8003 /* 8004 * The open/close semaphore is used to serialize threads executing 8005 * in the driver's open & close entry point routines for a given 8006 * instance. 8007 */ 8008 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8009 8010 /* 8011 * The conf file entry and softstate variable is a forceful override, 8012 * meaning a non-zero value must be entered to change the default. 8013 */ 8014 un->un_f_disksort_disabled = FALSE; 8015 8016 /* 8017 * Retrieve the properties from the static driver table or the driver 8018 * configuration file (.conf) for this unit and update the soft state 8019 * for the device as needed for the indicated properties. 8020 * Note: the property configuration needs to occur here as some of the 8021 * following routines may have dependancies on soft state flags set 8022 * as part of the driver property configuration. 8023 */ 8024 sd_read_unit_properties(un); 8025 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8026 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8027 8028 /* 8029 * Only if a device has "hotpluggable" property, it is 8030 * treated as hotpluggable device. Otherwise, it is 8031 * regarded as non-hotpluggable one. 8032 */ 8033 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8034 -1) != -1) { 8035 un->un_f_is_hotpluggable = TRUE; 8036 } 8037 8038 /* 8039 * set unit's attributes(flags) according to "hotpluggable" and 8040 * RMB bit in INQUIRY data. 8041 */ 8042 sd_set_unit_attributes(un, devi); 8043 8044 /* 8045 * By default, we mark the capacity, lbasize, and geometry 8046 * as invalid. Only if we successfully read a valid capacity 8047 * will we update the un_blockcount and un_tgt_blocksize with the 8048 * valid values (the geometry will be validated later). 8049 */ 8050 un->un_f_blockcount_is_valid = FALSE; 8051 un->un_f_tgt_blocksize_is_valid = FALSE; 8052 un->un_f_geometry_is_valid = FALSE; 8053 8054 /* 8055 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8056 * otherwise. 8057 */ 8058 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8059 un->un_blockcount = 0; 8060 8061 /* 8062 * Set up the per-instance info needed to determine the correct 8063 * CDBs and other info for issuing commands to the target. 8064 */ 8065 sd_init_cdb_limits(un); 8066 8067 /* 8068 * Set up the IO chains to use, based upon the target type. 8069 */ 8070 if (un->un_f_non_devbsize_supported) { 8071 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8072 } else { 8073 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8074 } 8075 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8076 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8077 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8078 8079 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8080 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8081 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8082 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8083 8084 8085 if (ISCD(un)) { 8086 un->un_additional_codes = sd_additional_codes; 8087 } else { 8088 un->un_additional_codes = NULL; 8089 } 8090 8091 /* 8092 * Create the kstats here so they can be available for attach-time 8093 * routines that send commands to the unit (either polled or via 8094 * sd_send_scsi_cmd). 8095 * 8096 * Note: This is a critical sequence that needs to be maintained: 8097 * 1) Instantiate the kstats here, before any routines using the 8098 * iopath (i.e. sd_send_scsi_cmd). 8099 * 2) Initialize the error stats (sd_set_errstats) and partition 8100 * stats (sd_set_pstats), following sd_validate_geometry(), 8101 * sd_register_devid(), and sd_cache_control(). 8102 */ 8103 8104 un->un_stats = kstat_create(sd_label, instance, 8105 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8106 if (un->un_stats != NULL) { 8107 un->un_stats->ks_lock = SD_MUTEX(un); 8108 kstat_install(un->un_stats); 8109 } 8110 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8111 "sd_unit_attach: un:0x%p un_stats created\n", un); 8112 8113 sd_create_errstats(un, instance); 8114 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8115 "sd_unit_attach: un:0x%p errstats created\n", un); 8116 8117 /* 8118 * The following if/else code was relocated here from below as part 8119 * of the fix for bug (4430280). However with the default setup added 8120 * on entry to this routine, it's no longer absolutely necessary for 8121 * this to be before the call to sd_spin_up_unit. 8122 */ 8123 if (SD_IS_PARALLEL_SCSI(un)) { 8124 /* 8125 * If SCSI-2 tagged queueing is supported by the target 8126 * and by the host adapter then we will enable it. 8127 */ 8128 un->un_tagflags = 0; 8129 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8130 (devp->sd_inq->inq_cmdque) && 8131 (un->un_f_arq_enabled == TRUE)) { 8132 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8133 1, 1) == 1) { 8134 un->un_tagflags = FLAG_STAG; 8135 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8136 "sd_unit_attach: un:0x%p tag queueing " 8137 "enabled\n", un); 8138 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8139 "untagged-qing", 0) == 1) { 8140 un->un_f_opt_queueing = TRUE; 8141 un->un_saved_throttle = un->un_throttle = 8142 min(un->un_throttle, 3); 8143 } else { 8144 un->un_f_opt_queueing = FALSE; 8145 un->un_saved_throttle = un->un_throttle = 1; 8146 } 8147 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8148 == 1) && (un->un_f_arq_enabled == TRUE)) { 8149 /* The Host Adapter supports internal queueing. */ 8150 un->un_f_opt_queueing = TRUE; 8151 un->un_saved_throttle = un->un_throttle = 8152 min(un->un_throttle, 3); 8153 } else { 8154 un->un_f_opt_queueing = FALSE; 8155 un->un_saved_throttle = un->un_throttle = 1; 8156 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8157 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8158 } 8159 8160 8161 /* Setup or tear down default wide operations for disks */ 8162 8163 /* 8164 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8165 * and "ssd_max_xfer_size" to exist simultaneously on the same 8166 * system and be set to different values. In the future this 8167 * code may need to be updated when the ssd module is 8168 * obsoleted and removed from the system. (4299588) 8169 */ 8170 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8171 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8172 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8173 1, 1) == 1) { 8174 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8175 "sd_unit_attach: un:0x%p Wide Transfer " 8176 "enabled\n", un); 8177 } 8178 8179 /* 8180 * If tagged queuing has also been enabled, then 8181 * enable large xfers 8182 */ 8183 if (un->un_saved_throttle == sd_max_throttle) { 8184 un->un_max_xfer_size = 8185 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8186 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8187 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8188 "sd_unit_attach: un:0x%p max transfer " 8189 "size=0x%x\n", un, un->un_max_xfer_size); 8190 } 8191 } else { 8192 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8193 0, 1) == 1) { 8194 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8195 "sd_unit_attach: un:0x%p " 8196 "Wide Transfer disabled\n", un); 8197 } 8198 } 8199 } else { 8200 un->un_tagflags = FLAG_STAG; 8201 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8202 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8203 } 8204 8205 /* 8206 * If this target supports LUN reset, try to enable it. 8207 */ 8208 if (un->un_f_lun_reset_enabled) { 8209 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8210 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8211 "un:0x%p lun_reset capability set\n", un); 8212 } else { 8213 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8214 "un:0x%p lun-reset capability not set\n", un); 8215 } 8216 } 8217 8218 /* 8219 * At this point in the attach, we have enough info in the 8220 * soft state to be able to issue commands to the target. 8221 * 8222 * All command paths used below MUST issue their commands as 8223 * SD_PATH_DIRECT. This is important as intermediate layers 8224 * are not all initialized yet (such as PM). 8225 */ 8226 8227 /* 8228 * Send a TEST UNIT READY command to the device. This should clear 8229 * any outstanding UNIT ATTENTION that may be present. 8230 * 8231 * Note: Don't check for success, just track if there is a reservation, 8232 * this is a throw away command to clear any unit attentions. 8233 * 8234 * Note: This MUST be the first command issued to the target during 8235 * attach to ensure power on UNIT ATTENTIONS are cleared. 8236 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8237 * with attempts at spinning up a device with no media. 8238 */ 8239 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8240 reservation_flag = SD_TARGET_IS_RESERVED; 8241 } 8242 8243 /* 8244 * If the device is NOT a removable media device, attempt to spin 8245 * it up (using the START_STOP_UNIT command) and read its capacity 8246 * (using the READ CAPACITY command). Note, however, that either 8247 * of these could fail and in some cases we would continue with 8248 * the attach despite the failure (see below). 8249 */ 8250 if (un->un_f_descr_format_supported) { 8251 switch (sd_spin_up_unit(un)) { 8252 case 0: 8253 /* 8254 * Spin-up was successful; now try to read the 8255 * capacity. If successful then save the results 8256 * and mark the capacity & lbasize as valid. 8257 */ 8258 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8259 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8260 8261 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8262 &lbasize, SD_PATH_DIRECT)) { 8263 case 0: { 8264 if (capacity > DK_MAX_BLOCKS) { 8265 #ifdef _LP64 8266 /* 8267 * Enable descriptor format sense data 8268 * so that we can get 64 bit sense 8269 * data fields. 8270 */ 8271 sd_enable_descr_sense(un); 8272 #else 8273 /* 32-bit kernels can't handle this */ 8274 scsi_log(SD_DEVINFO(un), 8275 sd_label, CE_WARN, 8276 "disk has %llu blocks, which " 8277 "is too large for a 32-bit " 8278 "kernel", capacity); 8279 goto spinup_failed; 8280 #endif 8281 } 8282 /* 8283 * The following relies on 8284 * sd_send_scsi_READ_CAPACITY never 8285 * returning 0 for capacity and/or lbasize. 8286 */ 8287 sd_update_block_info(un, lbasize, capacity); 8288 8289 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8290 "sd_unit_attach: un:0x%p capacity = %ld " 8291 "blocks; lbasize= %ld.\n", un, 8292 un->un_blockcount, un->un_tgt_blocksize); 8293 8294 break; 8295 } 8296 case EACCES: 8297 /* 8298 * Should never get here if the spin-up 8299 * succeeded, but code it in anyway. 8300 * From here, just continue with the attach... 8301 */ 8302 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8303 "sd_unit_attach: un:0x%p " 8304 "sd_send_scsi_READ_CAPACITY " 8305 "returned reservation conflict\n", un); 8306 reservation_flag = SD_TARGET_IS_RESERVED; 8307 break; 8308 default: 8309 /* 8310 * Likewise, should never get here if the 8311 * spin-up succeeded. Just continue with 8312 * the attach... 8313 */ 8314 break; 8315 } 8316 break; 8317 case EACCES: 8318 /* 8319 * Device is reserved by another host. In this case 8320 * we could not spin it up or read the capacity, but 8321 * we continue with the attach anyway. 8322 */ 8323 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8324 "sd_unit_attach: un:0x%p spin-up reservation " 8325 "conflict.\n", un); 8326 reservation_flag = SD_TARGET_IS_RESERVED; 8327 break; 8328 default: 8329 /* Fail the attach if the spin-up failed. */ 8330 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8331 "sd_unit_attach: un:0x%p spin-up failed.", un); 8332 goto spinup_failed; 8333 } 8334 } 8335 8336 /* 8337 * Check to see if this is a MMC drive 8338 */ 8339 if (ISCD(un)) { 8340 sd_set_mmc_caps(un); 8341 } 8342 8343 /* 8344 * Create the minor nodes for the device. 8345 * Note: If we want to support fdisk on both sparc and intel, this will 8346 * have to separate out the notion that VTOC8 is always sparc, and 8347 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8348 * type will have to be determined at run-time, and the fdisk 8349 * partitioning will have to have been read & set up before we 8350 * create the minor nodes. (any other inits (such as kstats) that 8351 * also ought to be done before creating the minor nodes?) (Doesn't 8352 * setting up the minor nodes kind of imply that we're ready to 8353 * handle an open from userland?) 8354 */ 8355 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8356 goto create_minor_nodes_failed; 8357 } 8358 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8359 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8360 8361 /* 8362 * Add a zero-length attribute to tell the world we support 8363 * kernel ioctls (for layered drivers) 8364 */ 8365 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8366 DDI_KERNEL_IOCTL, NULL, 0); 8367 8368 /* 8369 * Add a boolean property to tell the world we support 8370 * the B_FAILFAST flag (for layered drivers) 8371 */ 8372 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8373 "ddi-failfast-supported", NULL, 0); 8374 8375 /* 8376 * Initialize power management 8377 */ 8378 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8379 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8380 sd_setup_pm(un, devi); 8381 if (un->un_f_pm_is_enabled == FALSE) { 8382 /* 8383 * For performance, point to a jump table that does 8384 * not include pm. 8385 * The direct and priority chains don't change with PM. 8386 * 8387 * Note: this is currently done based on individual device 8388 * capabilities. When an interface for determining system 8389 * power enabled state becomes available, or when additional 8390 * layers are added to the command chain, these values will 8391 * have to be re-evaluated for correctness. 8392 */ 8393 if (un->un_f_non_devbsize_supported) { 8394 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8395 } else { 8396 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8397 } 8398 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8399 } 8400 8401 /* 8402 * This property is set to 0 by HA software to avoid retries 8403 * on a reserved disk. (The preferred property name is 8404 * "retry-on-reservation-conflict") (1189689) 8405 * 8406 * Note: The use of a global here can have unintended consequences. A 8407 * per instance variable is preferrable to match the capabilities of 8408 * different underlying hba's (4402600) 8409 */ 8410 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8411 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8412 sd_retry_on_reservation_conflict); 8413 if (sd_retry_on_reservation_conflict != 0) { 8414 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8415 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8416 sd_retry_on_reservation_conflict); 8417 } 8418 8419 /* Set up options for QFULL handling. */ 8420 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8421 "qfull-retries", -1)) != -1) { 8422 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8423 rval, 1); 8424 } 8425 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8426 "qfull-retry-interval", -1)) != -1) { 8427 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8428 rval, 1); 8429 } 8430 8431 /* 8432 * This just prints a message that announces the existence of the 8433 * device. The message is always printed in the system logfile, but 8434 * only appears on the console if the system is booted with the 8435 * -v (verbose) argument. 8436 */ 8437 ddi_report_dev(devi); 8438 8439 /* 8440 * The framework calls driver attach routines single-threaded 8441 * for a given instance. However we still acquire SD_MUTEX here 8442 * because this required for calling the sd_validate_geometry() 8443 * and sd_register_devid() functions. 8444 */ 8445 mutex_enter(SD_MUTEX(un)); 8446 un->un_f_geometry_is_valid = FALSE; 8447 un->un_mediastate = DKIO_NONE; 8448 un->un_reserved = -1; 8449 8450 /* 8451 * Read and validate the device's geometry (ie, disk label) 8452 * A new unformatted drive will not have a valid geometry, but 8453 * the driver needs to successfully attach to this device so 8454 * the drive can be formatted via ioctls. 8455 */ 8456 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8457 ENOTSUP)) && 8458 (un->un_blockcount < DK_MAX_BLOCKS)) { 8459 /* 8460 * We found a small disk with an EFI label on it; 8461 * we need to fix up the minor nodes accordingly. 8462 */ 8463 ddi_remove_minor_node(devi, "h"); 8464 ddi_remove_minor_node(devi, "h,raw"); 8465 (void) ddi_create_minor_node(devi, "wd", 8466 S_IFBLK, 8467 (instance << SDUNIT_SHIFT) | WD_NODE, 8468 un->un_node_type, NULL); 8469 (void) ddi_create_minor_node(devi, "wd,raw", 8470 S_IFCHR, 8471 (instance << SDUNIT_SHIFT) | WD_NODE, 8472 un->un_node_type, NULL); 8473 } 8474 8475 /* 8476 * Read and initialize the devid for the unit. 8477 */ 8478 ASSERT(un->un_errstats != NULL); 8479 if (un->un_f_devid_supported) { 8480 sd_register_devid(un, devi, reservation_flag); 8481 } 8482 mutex_exit(SD_MUTEX(un)); 8483 8484 #if (defined(__fibre)) 8485 /* 8486 * Register callbacks for fibre only. You can't do this soley 8487 * on the basis of the devid_type because this is hba specific. 8488 * We need to query our hba capabilities to find out whether to 8489 * register or not. 8490 */ 8491 if (un->un_f_is_fibre) { 8492 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8493 sd_init_event_callbacks(un); 8494 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8495 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8496 } 8497 } 8498 #endif 8499 8500 if (un->un_f_opt_disable_cache == TRUE) { 8501 /* 8502 * Disable both read cache and write cache. This is 8503 * the historic behavior of the keywords in the config file. 8504 */ 8505 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8506 0) { 8507 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8508 "sd_unit_attach: un:0x%p Could not disable " 8509 "caching", un); 8510 goto devid_failed; 8511 } 8512 } 8513 8514 /* 8515 * Check the value of the WCE bit now and 8516 * set un_f_write_cache_enabled accordingly. 8517 */ 8518 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8519 mutex_enter(SD_MUTEX(un)); 8520 un->un_f_write_cache_enabled = (wc_enabled != 0); 8521 mutex_exit(SD_MUTEX(un)); 8522 8523 /* 8524 * Set the pstat and error stat values here, so data obtained during the 8525 * previous attach-time routines is available. 8526 * 8527 * Note: This is a critical sequence that needs to be maintained: 8528 * 1) Instantiate the kstats before any routines using the iopath 8529 * (i.e. sd_send_scsi_cmd). 8530 * 2) Initialize the error stats (sd_set_errstats) and partition 8531 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8532 * sd_register_devid(), and sd_cache_control(). 8533 */ 8534 if (un->un_f_pkstats_enabled) { 8535 sd_set_pstats(un); 8536 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8537 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8538 } 8539 8540 sd_set_errstats(un); 8541 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8542 "sd_unit_attach: un:0x%p errstats set\n", un); 8543 8544 /* 8545 * Find out what type of reservation this disk supports. 8546 */ 8547 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8548 case 0: 8549 /* 8550 * SCSI-3 reservations are supported. 8551 */ 8552 un->un_reservation_type = SD_SCSI3_RESERVATION; 8553 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8554 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8555 break; 8556 case ENOTSUP: 8557 /* 8558 * The PERSISTENT RESERVE IN command would not be recognized by 8559 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8560 */ 8561 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8562 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8563 un->un_reservation_type = SD_SCSI2_RESERVATION; 8564 break; 8565 default: 8566 /* 8567 * default to SCSI-3 reservations 8568 */ 8569 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8570 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8571 un->un_reservation_type = SD_SCSI3_RESERVATION; 8572 break; 8573 } 8574 8575 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8576 "sd_unit_attach: un:0x%p exit success\n", un); 8577 8578 return (DDI_SUCCESS); 8579 8580 /* 8581 * An error occurred during the attach; clean up & return failure. 8582 */ 8583 8584 devid_failed: 8585 8586 setup_pm_failed: 8587 ddi_remove_minor_node(devi, NULL); 8588 8589 create_minor_nodes_failed: 8590 /* 8591 * Cleanup from the scsi_ifsetcap() calls (437868) 8592 */ 8593 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8594 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8595 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8596 8597 if (un->un_f_is_fibre == FALSE) { 8598 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8599 } 8600 8601 spinup_failed: 8602 8603 mutex_enter(SD_MUTEX(un)); 8604 8605 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8606 if (un->un_direct_priority_timeid != NULL) { 8607 timeout_id_t temp_id = un->un_direct_priority_timeid; 8608 un->un_direct_priority_timeid = NULL; 8609 mutex_exit(SD_MUTEX(un)); 8610 (void) untimeout(temp_id); 8611 mutex_enter(SD_MUTEX(un)); 8612 } 8613 8614 /* Cancel any pending start/stop timeouts */ 8615 if (un->un_startstop_timeid != NULL) { 8616 timeout_id_t temp_id = un->un_startstop_timeid; 8617 un->un_startstop_timeid = NULL; 8618 mutex_exit(SD_MUTEX(un)); 8619 (void) untimeout(temp_id); 8620 mutex_enter(SD_MUTEX(un)); 8621 } 8622 8623 /* Cancel any pending reset-throttle timeouts */ 8624 if (un->un_reset_throttle_timeid != NULL) { 8625 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8626 un->un_reset_throttle_timeid = NULL; 8627 mutex_exit(SD_MUTEX(un)); 8628 (void) untimeout(temp_id); 8629 mutex_enter(SD_MUTEX(un)); 8630 } 8631 8632 /* Cancel any pending retry timeouts */ 8633 if (un->un_retry_timeid != NULL) { 8634 timeout_id_t temp_id = un->un_retry_timeid; 8635 un->un_retry_timeid = NULL; 8636 mutex_exit(SD_MUTEX(un)); 8637 (void) untimeout(temp_id); 8638 mutex_enter(SD_MUTEX(un)); 8639 } 8640 8641 /* Cancel any pending delayed cv broadcast timeouts */ 8642 if (un->un_dcvb_timeid != NULL) { 8643 timeout_id_t temp_id = un->un_dcvb_timeid; 8644 un->un_dcvb_timeid = NULL; 8645 mutex_exit(SD_MUTEX(un)); 8646 (void) untimeout(temp_id); 8647 mutex_enter(SD_MUTEX(un)); 8648 } 8649 8650 mutex_exit(SD_MUTEX(un)); 8651 8652 /* There should not be any in-progress I/O so ASSERT this check */ 8653 ASSERT(un->un_ncmds_in_transport == 0); 8654 ASSERT(un->un_ncmds_in_driver == 0); 8655 8656 /* Do not free the softstate if the callback routine is active */ 8657 sd_sync_with_callback(un); 8658 8659 /* 8660 * Partition stats apparently are not used with removables. These would 8661 * not have been created during attach, so no need to clean them up... 8662 */ 8663 if (un->un_stats != NULL) { 8664 kstat_delete(un->un_stats); 8665 un->un_stats = NULL; 8666 } 8667 if (un->un_errstats != NULL) { 8668 kstat_delete(un->un_errstats); 8669 un->un_errstats = NULL; 8670 } 8671 8672 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8673 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8674 8675 ddi_prop_remove_all(devi); 8676 sema_destroy(&un->un_semoclose); 8677 cv_destroy(&un->un_state_cv); 8678 8679 getrbuf_failed: 8680 8681 sd_free_rqs(un); 8682 8683 alloc_rqs_failed: 8684 8685 devp->sd_private = NULL; 8686 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8687 8688 get_softstate_failed: 8689 /* 8690 * Note: the man pages are unclear as to whether or not doing a 8691 * ddi_soft_state_free(sd_state, instance) is the right way to 8692 * clean up after the ddi_soft_state_zalloc() if the subsequent 8693 * ddi_get_soft_state() fails. The implication seems to be 8694 * that the get_soft_state cannot fail if the zalloc succeeds. 8695 */ 8696 ddi_soft_state_free(sd_state, instance); 8697 8698 probe_failed: 8699 scsi_unprobe(devp); 8700 #ifdef SDDEBUG 8701 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8702 (sd_level_mask & SD_LOGMASK_TRACE)) { 8703 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8704 (void *)un); 8705 } 8706 #endif 8707 return (DDI_FAILURE); 8708 } 8709 8710 8711 /* 8712 * Function: sd_unit_detach 8713 * 8714 * Description: Performs DDI_DETACH processing for sddetach(). 8715 * 8716 * Return Code: DDI_SUCCESS 8717 * DDI_FAILURE 8718 * 8719 * Context: Kernel thread context 8720 */ 8721 8722 static int 8723 sd_unit_detach(dev_info_t *devi) 8724 { 8725 struct scsi_device *devp; 8726 struct sd_lun *un; 8727 int i; 8728 dev_t dev; 8729 int instance = ddi_get_instance(devi); 8730 8731 mutex_enter(&sd_detach_mutex); 8732 8733 /* 8734 * Fail the detach for any of the following: 8735 * - Unable to get the sd_lun struct for the instance 8736 * - A layered driver has an outstanding open on the instance 8737 * - Another thread is already detaching this instance 8738 * - Another thread is currently performing an open 8739 */ 8740 devp = ddi_get_driver_private(devi); 8741 if ((devp == NULL) || 8742 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8743 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8744 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8745 mutex_exit(&sd_detach_mutex); 8746 return (DDI_FAILURE); 8747 } 8748 8749 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8750 8751 /* 8752 * Mark this instance as currently in a detach, to inhibit any 8753 * opens from a layered driver. 8754 */ 8755 un->un_detach_count++; 8756 mutex_exit(&sd_detach_mutex); 8757 8758 dev = sd_make_device(SD_DEVINFO(un)); 8759 8760 _NOTE(COMPETING_THREADS_NOW); 8761 8762 mutex_enter(SD_MUTEX(un)); 8763 8764 /* 8765 * Fail the detach if there are any outstanding layered 8766 * opens on this device. 8767 */ 8768 for (i = 0; i < NDKMAP; i++) { 8769 if (un->un_ocmap.lyropen[i] != 0) { 8770 goto err_notclosed; 8771 } 8772 } 8773 8774 /* 8775 * Verify there are NO outstanding commands issued to this device. 8776 * ie, un_ncmds_in_transport == 0. 8777 * It's possible to have outstanding commands through the physio 8778 * code path, even though everything's closed. 8779 */ 8780 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8781 (un->un_direct_priority_timeid != NULL) || 8782 (un->un_state == SD_STATE_RWAIT)) { 8783 mutex_exit(SD_MUTEX(un)); 8784 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8785 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8786 goto err_stillbusy; 8787 } 8788 8789 /* 8790 * If we have the device reserved, release the reservation. 8791 */ 8792 if ((un->un_resvd_status & SD_RESERVE) && 8793 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8794 mutex_exit(SD_MUTEX(un)); 8795 /* 8796 * Note: sd_reserve_release sends a command to the device 8797 * via the sd_ioctlcmd() path, and can sleep. 8798 */ 8799 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8800 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8801 "sd_dr_detach: Cannot release reservation \n"); 8802 } 8803 } else { 8804 mutex_exit(SD_MUTEX(un)); 8805 } 8806 8807 /* 8808 * Untimeout any reserve recover, throttle reset, restart unit 8809 * and delayed broadcast timeout threads. Protect the timeout pointer 8810 * from getting nulled by their callback functions. 8811 */ 8812 mutex_enter(SD_MUTEX(un)); 8813 if (un->un_resvd_timeid != NULL) { 8814 timeout_id_t temp_id = un->un_resvd_timeid; 8815 un->un_resvd_timeid = NULL; 8816 mutex_exit(SD_MUTEX(un)); 8817 (void) untimeout(temp_id); 8818 mutex_enter(SD_MUTEX(un)); 8819 } 8820 8821 if (un->un_reset_throttle_timeid != NULL) { 8822 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8823 un->un_reset_throttle_timeid = NULL; 8824 mutex_exit(SD_MUTEX(un)); 8825 (void) untimeout(temp_id); 8826 mutex_enter(SD_MUTEX(un)); 8827 } 8828 8829 if (un->un_startstop_timeid != NULL) { 8830 timeout_id_t temp_id = un->un_startstop_timeid; 8831 un->un_startstop_timeid = NULL; 8832 mutex_exit(SD_MUTEX(un)); 8833 (void) untimeout(temp_id); 8834 mutex_enter(SD_MUTEX(un)); 8835 } 8836 8837 if (un->un_dcvb_timeid != NULL) { 8838 timeout_id_t temp_id = un->un_dcvb_timeid; 8839 un->un_dcvb_timeid = NULL; 8840 mutex_exit(SD_MUTEX(un)); 8841 (void) untimeout(temp_id); 8842 } else { 8843 mutex_exit(SD_MUTEX(un)); 8844 } 8845 8846 /* Remove any pending reservation reclaim requests for this device */ 8847 sd_rmv_resv_reclaim_req(dev); 8848 8849 mutex_enter(SD_MUTEX(un)); 8850 8851 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8852 if (un->un_direct_priority_timeid != NULL) { 8853 timeout_id_t temp_id = un->un_direct_priority_timeid; 8854 un->un_direct_priority_timeid = NULL; 8855 mutex_exit(SD_MUTEX(un)); 8856 (void) untimeout(temp_id); 8857 mutex_enter(SD_MUTEX(un)); 8858 } 8859 8860 /* Cancel any active multi-host disk watch thread requests */ 8861 if (un->un_mhd_token != NULL) { 8862 mutex_exit(SD_MUTEX(un)); 8863 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8864 if (scsi_watch_request_terminate(un->un_mhd_token, 8865 SCSI_WATCH_TERMINATE_NOWAIT)) { 8866 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8867 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8868 /* 8869 * Note: We are returning here after having removed 8870 * some driver timeouts above. This is consistent with 8871 * the legacy implementation but perhaps the watch 8872 * terminate call should be made with the wait flag set. 8873 */ 8874 goto err_stillbusy; 8875 } 8876 mutex_enter(SD_MUTEX(un)); 8877 un->un_mhd_token = NULL; 8878 } 8879 8880 if (un->un_swr_token != NULL) { 8881 mutex_exit(SD_MUTEX(un)); 8882 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8883 if (scsi_watch_request_terminate(un->un_swr_token, 8884 SCSI_WATCH_TERMINATE_NOWAIT)) { 8885 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8886 "sd_dr_detach: Cannot cancel swr watch request\n"); 8887 /* 8888 * Note: We are returning here after having removed 8889 * some driver timeouts above. This is consistent with 8890 * the legacy implementation but perhaps the watch 8891 * terminate call should be made with the wait flag set. 8892 */ 8893 goto err_stillbusy; 8894 } 8895 mutex_enter(SD_MUTEX(un)); 8896 un->un_swr_token = NULL; 8897 } 8898 8899 mutex_exit(SD_MUTEX(un)); 8900 8901 /* 8902 * Clear any scsi_reset_notifies. We clear the reset notifies 8903 * if we have not registered one. 8904 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8905 */ 8906 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8907 sd_mhd_reset_notify_cb, (caddr_t)un); 8908 8909 /* 8910 * protect the timeout pointers from getting nulled by 8911 * their callback functions during the cancellation process. 8912 * In such a scenario untimeout can be invoked with a null value. 8913 */ 8914 _NOTE(NO_COMPETING_THREADS_NOW); 8915 8916 mutex_enter(&un->un_pm_mutex); 8917 if (un->un_pm_idle_timeid != NULL) { 8918 timeout_id_t temp_id = un->un_pm_idle_timeid; 8919 un->un_pm_idle_timeid = NULL; 8920 mutex_exit(&un->un_pm_mutex); 8921 8922 /* 8923 * Timeout is active; cancel it. 8924 * Note that it'll never be active on a device 8925 * that does not support PM therefore we don't 8926 * have to check before calling pm_idle_component. 8927 */ 8928 (void) untimeout(temp_id); 8929 (void) pm_idle_component(SD_DEVINFO(un), 0); 8930 mutex_enter(&un->un_pm_mutex); 8931 } 8932 8933 /* 8934 * Check whether there is already a timeout scheduled for power 8935 * management. If yes then don't lower the power here, that's. 8936 * the timeout handler's job. 8937 */ 8938 if (un->un_pm_timeid != NULL) { 8939 timeout_id_t temp_id = un->un_pm_timeid; 8940 un->un_pm_timeid = NULL; 8941 mutex_exit(&un->un_pm_mutex); 8942 /* 8943 * Timeout is active; cancel it. 8944 * Note that it'll never be active on a device 8945 * that does not support PM therefore we don't 8946 * have to check before calling pm_idle_component. 8947 */ 8948 (void) untimeout(temp_id); 8949 (void) pm_idle_component(SD_DEVINFO(un), 0); 8950 8951 } else { 8952 mutex_exit(&un->un_pm_mutex); 8953 if ((un->un_f_pm_is_enabled == TRUE) && 8954 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8955 DDI_SUCCESS)) { 8956 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8957 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8958 /* 8959 * Fix for bug: 4297749, item # 13 8960 * The above test now includes a check to see if PM is 8961 * supported by this device before call 8962 * pm_lower_power(). 8963 * Note, the following is not dead code. The call to 8964 * pm_lower_power above will generate a call back into 8965 * our sdpower routine which might result in a timeout 8966 * handler getting activated. Therefore the following 8967 * code is valid and necessary. 8968 */ 8969 mutex_enter(&un->un_pm_mutex); 8970 if (un->un_pm_timeid != NULL) { 8971 timeout_id_t temp_id = un->un_pm_timeid; 8972 un->un_pm_timeid = NULL; 8973 mutex_exit(&un->un_pm_mutex); 8974 (void) untimeout(temp_id); 8975 (void) pm_idle_component(SD_DEVINFO(un), 0); 8976 } else { 8977 mutex_exit(&un->un_pm_mutex); 8978 } 8979 } 8980 } 8981 8982 /* 8983 * Cleanup from the scsi_ifsetcap() calls (437868) 8984 * Relocated here from above to be after the call to 8985 * pm_lower_power, which was getting errors. 8986 */ 8987 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8988 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8989 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8990 8991 if (un->un_f_is_fibre == FALSE) { 8992 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8993 } 8994 8995 /* 8996 * Remove any event callbacks, fibre only 8997 */ 8998 if (un->un_f_is_fibre == TRUE) { 8999 if ((un->un_insert_event != NULL) && 9000 (ddi_remove_event_handler(un->un_insert_cb_id) != 9001 DDI_SUCCESS)) { 9002 /* 9003 * Note: We are returning here after having done 9004 * substantial cleanup above. This is consistent 9005 * with the legacy implementation but this may not 9006 * be the right thing to do. 9007 */ 9008 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9009 "sd_dr_detach: Cannot cancel insert event\n"); 9010 goto err_remove_event; 9011 } 9012 un->un_insert_event = NULL; 9013 9014 if ((un->un_remove_event != NULL) && 9015 (ddi_remove_event_handler(un->un_remove_cb_id) != 9016 DDI_SUCCESS)) { 9017 /* 9018 * Note: We are returning here after having done 9019 * substantial cleanup above. This is consistent 9020 * with the legacy implementation but this may not 9021 * be the right thing to do. 9022 */ 9023 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9024 "sd_dr_detach: Cannot cancel remove event\n"); 9025 goto err_remove_event; 9026 } 9027 un->un_remove_event = NULL; 9028 } 9029 9030 /* Do not free the softstate if the callback routine is active */ 9031 sd_sync_with_callback(un); 9032 9033 /* 9034 * Hold the detach mutex here, to make sure that no other threads ever 9035 * can access a (partially) freed soft state structure. 9036 */ 9037 mutex_enter(&sd_detach_mutex); 9038 9039 /* 9040 * Clean up the soft state struct. 9041 * Cleanup is done in reverse order of allocs/inits. 9042 * At this point there should be no competing threads anymore. 9043 */ 9044 9045 /* Unregister and free device id. */ 9046 ddi_devid_unregister(devi); 9047 if (un->un_devid) { 9048 ddi_devid_free(un->un_devid); 9049 un->un_devid = NULL; 9050 } 9051 9052 /* 9053 * Destroy wmap cache if it exists. 9054 */ 9055 if (un->un_wm_cache != NULL) { 9056 kmem_cache_destroy(un->un_wm_cache); 9057 un->un_wm_cache = NULL; 9058 } 9059 9060 /* Remove minor nodes */ 9061 ddi_remove_minor_node(devi, NULL); 9062 9063 /* 9064 * kstat cleanup is done in detach for all device types (4363169). 9065 * We do not want to fail detach if the device kstats are not deleted 9066 * since there is a confusion about the devo_refcnt for the device. 9067 * We just delete the kstats and let detach complete successfully. 9068 */ 9069 if (un->un_stats != NULL) { 9070 kstat_delete(un->un_stats); 9071 un->un_stats = NULL; 9072 } 9073 if (un->un_errstats != NULL) { 9074 kstat_delete(un->un_errstats); 9075 un->un_errstats = NULL; 9076 } 9077 9078 /* Remove partition stats */ 9079 if (un->un_f_pkstats_enabled) { 9080 for (i = 0; i < NSDMAP; i++) { 9081 if (un->un_pstats[i] != NULL) { 9082 kstat_delete(un->un_pstats[i]); 9083 un->un_pstats[i] = NULL; 9084 } 9085 } 9086 } 9087 9088 /* Remove xbuf registration */ 9089 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9090 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9091 9092 /* Remove driver properties */ 9093 ddi_prop_remove_all(devi); 9094 9095 mutex_destroy(&un->un_pm_mutex); 9096 cv_destroy(&un->un_pm_busy_cv); 9097 9098 cv_destroy(&un->un_wcc_cv); 9099 9100 /* Open/close semaphore */ 9101 sema_destroy(&un->un_semoclose); 9102 9103 /* Removable media condvar. */ 9104 cv_destroy(&un->un_state_cv); 9105 9106 /* Suspend/resume condvar. */ 9107 cv_destroy(&un->un_suspend_cv); 9108 cv_destroy(&un->un_disk_busy_cv); 9109 9110 sd_free_rqs(un); 9111 9112 /* Free up soft state */ 9113 devp->sd_private = NULL; 9114 bzero(un, sizeof (struct sd_lun)); 9115 ddi_soft_state_free(sd_state, instance); 9116 9117 mutex_exit(&sd_detach_mutex); 9118 9119 /* This frees up the INQUIRY data associated with the device. */ 9120 scsi_unprobe(devp); 9121 9122 return (DDI_SUCCESS); 9123 9124 err_notclosed: 9125 mutex_exit(SD_MUTEX(un)); 9126 9127 err_stillbusy: 9128 _NOTE(NO_COMPETING_THREADS_NOW); 9129 9130 err_remove_event: 9131 mutex_enter(&sd_detach_mutex); 9132 un->un_detach_count--; 9133 mutex_exit(&sd_detach_mutex); 9134 9135 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9136 return (DDI_FAILURE); 9137 } 9138 9139 9140 /* 9141 * Driver minor node structure and data table 9142 */ 9143 struct driver_minor_data { 9144 char *name; 9145 minor_t minor; 9146 int type; 9147 }; 9148 9149 static struct driver_minor_data sd_minor_data[] = { 9150 {"a", 0, S_IFBLK}, 9151 {"b", 1, S_IFBLK}, 9152 {"c", 2, S_IFBLK}, 9153 {"d", 3, S_IFBLK}, 9154 {"e", 4, S_IFBLK}, 9155 {"f", 5, S_IFBLK}, 9156 {"g", 6, S_IFBLK}, 9157 {"h", 7, S_IFBLK}, 9158 #if defined(_SUNOS_VTOC_16) 9159 {"i", 8, S_IFBLK}, 9160 {"j", 9, S_IFBLK}, 9161 {"k", 10, S_IFBLK}, 9162 {"l", 11, S_IFBLK}, 9163 {"m", 12, S_IFBLK}, 9164 {"n", 13, S_IFBLK}, 9165 {"o", 14, S_IFBLK}, 9166 {"p", 15, S_IFBLK}, 9167 #endif /* defined(_SUNOS_VTOC_16) */ 9168 #if defined(_FIRMWARE_NEEDS_FDISK) 9169 {"q", 16, S_IFBLK}, 9170 {"r", 17, S_IFBLK}, 9171 {"s", 18, S_IFBLK}, 9172 {"t", 19, S_IFBLK}, 9173 {"u", 20, S_IFBLK}, 9174 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9175 {"a,raw", 0, S_IFCHR}, 9176 {"b,raw", 1, S_IFCHR}, 9177 {"c,raw", 2, S_IFCHR}, 9178 {"d,raw", 3, S_IFCHR}, 9179 {"e,raw", 4, S_IFCHR}, 9180 {"f,raw", 5, S_IFCHR}, 9181 {"g,raw", 6, S_IFCHR}, 9182 {"h,raw", 7, S_IFCHR}, 9183 #if defined(_SUNOS_VTOC_16) 9184 {"i,raw", 8, S_IFCHR}, 9185 {"j,raw", 9, S_IFCHR}, 9186 {"k,raw", 10, S_IFCHR}, 9187 {"l,raw", 11, S_IFCHR}, 9188 {"m,raw", 12, S_IFCHR}, 9189 {"n,raw", 13, S_IFCHR}, 9190 {"o,raw", 14, S_IFCHR}, 9191 {"p,raw", 15, S_IFCHR}, 9192 #endif /* defined(_SUNOS_VTOC_16) */ 9193 #if defined(_FIRMWARE_NEEDS_FDISK) 9194 {"q,raw", 16, S_IFCHR}, 9195 {"r,raw", 17, S_IFCHR}, 9196 {"s,raw", 18, S_IFCHR}, 9197 {"t,raw", 19, S_IFCHR}, 9198 {"u,raw", 20, S_IFCHR}, 9199 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9200 {0} 9201 }; 9202 9203 static struct driver_minor_data sd_minor_data_efi[] = { 9204 {"a", 0, S_IFBLK}, 9205 {"b", 1, S_IFBLK}, 9206 {"c", 2, S_IFBLK}, 9207 {"d", 3, S_IFBLK}, 9208 {"e", 4, S_IFBLK}, 9209 {"f", 5, S_IFBLK}, 9210 {"g", 6, S_IFBLK}, 9211 {"wd", 7, S_IFBLK}, 9212 #if defined(_FIRMWARE_NEEDS_FDISK) 9213 {"q", 16, S_IFBLK}, 9214 {"r", 17, S_IFBLK}, 9215 {"s", 18, S_IFBLK}, 9216 {"t", 19, S_IFBLK}, 9217 {"u", 20, S_IFBLK}, 9218 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9219 {"a,raw", 0, S_IFCHR}, 9220 {"b,raw", 1, S_IFCHR}, 9221 {"c,raw", 2, S_IFCHR}, 9222 {"d,raw", 3, S_IFCHR}, 9223 {"e,raw", 4, S_IFCHR}, 9224 {"f,raw", 5, S_IFCHR}, 9225 {"g,raw", 6, S_IFCHR}, 9226 {"wd,raw", 7, S_IFCHR}, 9227 #if defined(_FIRMWARE_NEEDS_FDISK) 9228 {"q,raw", 16, S_IFCHR}, 9229 {"r,raw", 17, S_IFCHR}, 9230 {"s,raw", 18, S_IFCHR}, 9231 {"t,raw", 19, S_IFCHR}, 9232 {"u,raw", 20, S_IFCHR}, 9233 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9234 {0} 9235 }; 9236 9237 9238 /* 9239 * Function: sd_create_minor_nodes 9240 * 9241 * Description: Create the minor device nodes for the instance. 9242 * 9243 * Arguments: un - driver soft state (unit) structure 9244 * devi - pointer to device info structure 9245 * 9246 * Return Code: DDI_SUCCESS 9247 * DDI_FAILURE 9248 * 9249 * Context: Kernel thread context 9250 */ 9251 9252 static int 9253 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9254 { 9255 struct driver_minor_data *dmdp; 9256 struct scsi_device *devp; 9257 int instance; 9258 char name[48]; 9259 9260 ASSERT(un != NULL); 9261 devp = ddi_get_driver_private(devi); 9262 instance = ddi_get_instance(devp->sd_dev); 9263 9264 /* 9265 * Create all the minor nodes for this target. 9266 */ 9267 if (un->un_blockcount > DK_MAX_BLOCKS) 9268 dmdp = sd_minor_data_efi; 9269 else 9270 dmdp = sd_minor_data; 9271 while (dmdp->name != NULL) { 9272 9273 (void) sprintf(name, "%s", dmdp->name); 9274 9275 if (ddi_create_minor_node(devi, name, dmdp->type, 9276 (instance << SDUNIT_SHIFT) | dmdp->minor, 9277 un->un_node_type, NULL) == DDI_FAILURE) { 9278 /* 9279 * Clean up any nodes that may have been created, in 9280 * case this fails in the middle of the loop. 9281 */ 9282 ddi_remove_minor_node(devi, NULL); 9283 return (DDI_FAILURE); 9284 } 9285 dmdp++; 9286 } 9287 9288 return (DDI_SUCCESS); 9289 } 9290 9291 9292 /* 9293 * Function: sd_create_errstats 9294 * 9295 * Description: This routine instantiates the device error stats. 9296 * 9297 * Note: During attach the stats are instantiated first so they are 9298 * available for attach-time routines that utilize the driver 9299 * iopath to send commands to the device. The stats are initialized 9300 * separately so data obtained during some attach-time routines is 9301 * available. (4362483) 9302 * 9303 * Arguments: un - driver soft state (unit) structure 9304 * instance - driver instance 9305 * 9306 * Context: Kernel thread context 9307 */ 9308 9309 static void 9310 sd_create_errstats(struct sd_lun *un, int instance) 9311 { 9312 struct sd_errstats *stp; 9313 char kstatmodule_err[KSTAT_STRLEN]; 9314 char kstatname[KSTAT_STRLEN]; 9315 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9316 9317 ASSERT(un != NULL); 9318 9319 if (un->un_errstats != NULL) { 9320 return; 9321 } 9322 9323 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9324 "%serr", sd_label); 9325 (void) snprintf(kstatname, sizeof (kstatname), 9326 "%s%d,err", sd_label, instance); 9327 9328 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9329 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9330 9331 if (un->un_errstats == NULL) { 9332 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9333 "sd_create_errstats: Failed kstat_create\n"); 9334 return; 9335 } 9336 9337 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9338 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9339 KSTAT_DATA_UINT32); 9340 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9341 KSTAT_DATA_UINT32); 9342 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9343 KSTAT_DATA_UINT32); 9344 kstat_named_init(&stp->sd_vid, "Vendor", 9345 KSTAT_DATA_CHAR); 9346 kstat_named_init(&stp->sd_pid, "Product", 9347 KSTAT_DATA_CHAR); 9348 kstat_named_init(&stp->sd_revision, "Revision", 9349 KSTAT_DATA_CHAR); 9350 kstat_named_init(&stp->sd_serial, "Serial No", 9351 KSTAT_DATA_CHAR); 9352 kstat_named_init(&stp->sd_capacity, "Size", 9353 KSTAT_DATA_ULONGLONG); 9354 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9355 KSTAT_DATA_UINT32); 9356 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9357 KSTAT_DATA_UINT32); 9358 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9359 KSTAT_DATA_UINT32); 9360 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9361 KSTAT_DATA_UINT32); 9362 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9363 KSTAT_DATA_UINT32); 9364 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9365 KSTAT_DATA_UINT32); 9366 9367 un->un_errstats->ks_private = un; 9368 un->un_errstats->ks_update = nulldev; 9369 9370 kstat_install(un->un_errstats); 9371 } 9372 9373 9374 /* 9375 * Function: sd_set_errstats 9376 * 9377 * Description: This routine sets the value of the vendor id, product id, 9378 * revision, serial number, and capacity device error stats. 9379 * 9380 * Note: During attach the stats are instantiated first so they are 9381 * available for attach-time routines that utilize the driver 9382 * iopath to send commands to the device. The stats are initialized 9383 * separately so data obtained during some attach-time routines is 9384 * available. (4362483) 9385 * 9386 * Arguments: un - driver soft state (unit) structure 9387 * 9388 * Context: Kernel thread context 9389 */ 9390 9391 static void 9392 sd_set_errstats(struct sd_lun *un) 9393 { 9394 struct sd_errstats *stp; 9395 9396 ASSERT(un != NULL); 9397 ASSERT(un->un_errstats != NULL); 9398 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9399 ASSERT(stp != NULL); 9400 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9401 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9402 (void) strncpy(stp->sd_revision.value.c, 9403 un->un_sd->sd_inq->inq_revision, 4); 9404 9405 /* 9406 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9407 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9408 * (4376302)) 9409 */ 9410 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9411 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9412 sizeof (SD_INQUIRY(un)->inq_serial)); 9413 } 9414 9415 if (un->un_f_blockcount_is_valid != TRUE) { 9416 /* 9417 * Set capacity error stat to 0 for no media. This ensures 9418 * a valid capacity is displayed in response to 'iostat -E' 9419 * when no media is present in the device. 9420 */ 9421 stp->sd_capacity.value.ui64 = 0; 9422 } else { 9423 /* 9424 * Multiply un_blockcount by un->un_sys_blocksize to get 9425 * capacity. 9426 * 9427 * Note: for non-512 blocksize devices "un_blockcount" has been 9428 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9429 * (un_tgt_blocksize / un->un_sys_blocksize). 9430 */ 9431 stp->sd_capacity.value.ui64 = (uint64_t) 9432 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9433 } 9434 } 9435 9436 9437 /* 9438 * Function: sd_set_pstats 9439 * 9440 * Description: This routine instantiates and initializes the partition 9441 * stats for each partition with more than zero blocks. 9442 * (4363169) 9443 * 9444 * Arguments: un - driver soft state (unit) structure 9445 * 9446 * Context: Kernel thread context 9447 */ 9448 9449 static void 9450 sd_set_pstats(struct sd_lun *un) 9451 { 9452 char kstatname[KSTAT_STRLEN]; 9453 int instance; 9454 int i; 9455 9456 ASSERT(un != NULL); 9457 9458 instance = ddi_get_instance(SD_DEVINFO(un)); 9459 9460 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9461 for (i = 0; i < NSDMAP; i++) { 9462 if ((un->un_pstats[i] == NULL) && 9463 (un->un_map[i].dkl_nblk != 0)) { 9464 (void) snprintf(kstatname, sizeof (kstatname), 9465 "%s%d,%s", sd_label, instance, 9466 sd_minor_data[i].name); 9467 un->un_pstats[i] = kstat_create(sd_label, 9468 instance, kstatname, "partition", KSTAT_TYPE_IO, 9469 1, KSTAT_FLAG_PERSISTENT); 9470 if (un->un_pstats[i] != NULL) { 9471 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9472 kstat_install(un->un_pstats[i]); 9473 } 9474 } 9475 } 9476 } 9477 9478 9479 #if (defined(__fibre)) 9480 /* 9481 * Function: sd_init_event_callbacks 9482 * 9483 * Description: This routine initializes the insertion and removal event 9484 * callbacks. (fibre only) 9485 * 9486 * Arguments: un - driver soft state (unit) structure 9487 * 9488 * Context: Kernel thread context 9489 */ 9490 9491 static void 9492 sd_init_event_callbacks(struct sd_lun *un) 9493 { 9494 ASSERT(un != NULL); 9495 9496 if ((un->un_insert_event == NULL) && 9497 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9498 &un->un_insert_event) == DDI_SUCCESS)) { 9499 /* 9500 * Add the callback for an insertion event 9501 */ 9502 (void) ddi_add_event_handler(SD_DEVINFO(un), 9503 un->un_insert_event, sd_event_callback, (void *)un, 9504 &(un->un_insert_cb_id)); 9505 } 9506 9507 if ((un->un_remove_event == NULL) && 9508 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9509 &un->un_remove_event) == DDI_SUCCESS)) { 9510 /* 9511 * Add the callback for a removal event 9512 */ 9513 (void) ddi_add_event_handler(SD_DEVINFO(un), 9514 un->un_remove_event, sd_event_callback, (void *)un, 9515 &(un->un_remove_cb_id)); 9516 } 9517 } 9518 9519 9520 /* 9521 * Function: sd_event_callback 9522 * 9523 * Description: This routine handles insert/remove events (photon). The 9524 * state is changed to OFFLINE which can be used to supress 9525 * error msgs. (fibre only) 9526 * 9527 * Arguments: un - driver soft state (unit) structure 9528 * 9529 * Context: Callout thread context 9530 */ 9531 /* ARGSUSED */ 9532 static void 9533 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9534 void *bus_impldata) 9535 { 9536 struct sd_lun *un = (struct sd_lun *)arg; 9537 9538 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9539 if (event == un->un_insert_event) { 9540 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9541 mutex_enter(SD_MUTEX(un)); 9542 if (un->un_state == SD_STATE_OFFLINE) { 9543 if (un->un_last_state != SD_STATE_SUSPENDED) { 9544 un->un_state = un->un_last_state; 9545 } else { 9546 /* 9547 * We have gone through SUSPEND/RESUME while 9548 * we were offline. Restore the last state 9549 */ 9550 un->un_state = un->un_save_state; 9551 } 9552 } 9553 mutex_exit(SD_MUTEX(un)); 9554 9555 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9556 } else if (event == un->un_remove_event) { 9557 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9558 mutex_enter(SD_MUTEX(un)); 9559 /* 9560 * We need to handle an event callback that occurs during 9561 * the suspend operation, since we don't prevent it. 9562 */ 9563 if (un->un_state != SD_STATE_OFFLINE) { 9564 if (un->un_state != SD_STATE_SUSPENDED) { 9565 New_state(un, SD_STATE_OFFLINE); 9566 } else { 9567 un->un_last_state = SD_STATE_OFFLINE; 9568 } 9569 } 9570 mutex_exit(SD_MUTEX(un)); 9571 } else { 9572 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9573 "!Unknown event\n"); 9574 } 9575 9576 } 9577 #endif 9578 9579 /* 9580 * Function: sd_cache_control() 9581 * 9582 * Description: This routine is the driver entry point for setting 9583 * read and write caching by modifying the WCE (write cache 9584 * enable) and RCD (read cache disable) bits of mode 9585 * page 8 (MODEPAGE_CACHING). 9586 * 9587 * Arguments: un - driver soft state (unit) structure 9588 * rcd_flag - flag for controlling the read cache 9589 * wce_flag - flag for controlling the write cache 9590 * 9591 * Return Code: EIO 9592 * code returned by sd_send_scsi_MODE_SENSE and 9593 * sd_send_scsi_MODE_SELECT 9594 * 9595 * Context: Kernel Thread 9596 */ 9597 9598 static int 9599 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 9600 { 9601 struct mode_caching *mode_caching_page; 9602 uchar_t *header; 9603 size_t buflen; 9604 int hdrlen; 9605 int bd_len; 9606 int rval = 0; 9607 struct mode_header_grp2 *mhp; 9608 9609 ASSERT(un != NULL); 9610 9611 /* 9612 * Do a test unit ready, otherwise a mode sense may not work if this 9613 * is the first command sent to the device after boot. 9614 */ 9615 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9616 9617 if (un->un_f_cfg_is_atapi == TRUE) { 9618 hdrlen = MODE_HEADER_LENGTH_GRP2; 9619 } else { 9620 hdrlen = MODE_HEADER_LENGTH; 9621 } 9622 9623 /* 9624 * Allocate memory for the retrieved mode page and its headers. Set 9625 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9626 * we get all of the mode sense data otherwise, the mode select 9627 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9628 */ 9629 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9630 sizeof (struct mode_cache_scsi3); 9631 9632 header = kmem_zalloc(buflen, KM_SLEEP); 9633 9634 /* Get the information from the device. */ 9635 if (un->un_f_cfg_is_atapi == TRUE) { 9636 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9637 MODEPAGE_CACHING, SD_PATH_DIRECT); 9638 } else { 9639 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9640 MODEPAGE_CACHING, SD_PATH_DIRECT); 9641 } 9642 if (rval != 0) { 9643 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9644 "sd_cache_control: Mode Sense Failed\n"); 9645 kmem_free(header, buflen); 9646 return (rval); 9647 } 9648 9649 /* 9650 * Determine size of Block Descriptors in order to locate 9651 * the mode page data. ATAPI devices return 0, SCSI devices 9652 * should return MODE_BLK_DESC_LENGTH. 9653 */ 9654 if (un->un_f_cfg_is_atapi == TRUE) { 9655 mhp = (struct mode_header_grp2 *)header; 9656 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9657 } else { 9658 bd_len = ((struct mode_header *)header)->bdesc_length; 9659 } 9660 9661 if (bd_len > MODE_BLK_DESC_LENGTH) { 9662 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9663 "sd_cache_control: Mode Sense returned invalid " 9664 "block descriptor length\n"); 9665 kmem_free(header, buflen); 9666 return (EIO); 9667 } 9668 9669 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9670 9671 /* Check the relevant bits on successful mode sense. */ 9672 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9673 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9674 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9675 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9676 9677 size_t sbuflen; 9678 9679 /* 9680 * Construct select buffer length based on the 9681 * length of the sense data returned. 9682 */ 9683 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 9684 sizeof (struct mode_page) + 9685 (int)mode_caching_page->mode_page.length; 9686 9687 /* 9688 * Set the caching bits as requested. 9689 */ 9690 if (rcd_flag == SD_CACHE_ENABLE) 9691 mode_caching_page->rcd = 0; 9692 else if (rcd_flag == SD_CACHE_DISABLE) 9693 mode_caching_page->rcd = 1; 9694 9695 if (wce_flag == SD_CACHE_ENABLE) 9696 mode_caching_page->wce = 1; 9697 else if (wce_flag == SD_CACHE_DISABLE) 9698 mode_caching_page->wce = 0; 9699 9700 /* Clear reserved bits before mode select. */ 9701 mode_caching_page->mode_page.ps = 0; 9702 9703 /* 9704 * Clear out mode header for mode select. 9705 * The rest of the retrieved page will be reused. 9706 */ 9707 bzero(header, hdrlen); 9708 9709 if (un->un_f_cfg_is_atapi == TRUE) { 9710 mhp = (struct mode_header_grp2 *)header; 9711 mhp->bdesc_length_hi = bd_len >> 8; 9712 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9713 } else { 9714 ((struct mode_header *)header)->bdesc_length = bd_len; 9715 } 9716 9717 /* Issue mode select to change the cache settings */ 9718 if (un->un_f_cfg_is_atapi == TRUE) { 9719 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9720 sbuflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9721 } else { 9722 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9723 sbuflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9724 } 9725 } 9726 9727 kmem_free(header, buflen); 9728 return (rval); 9729 } 9730 9731 9732 /* 9733 * Function: sd_get_write_cache_enabled() 9734 * 9735 * Description: This routine is the driver entry point for determining if 9736 * write caching is enabled. It examines the WCE (write cache 9737 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9738 * 9739 * Arguments: un - driver soft state (unit) structure 9740 * is_enabled - pointer to int where write cache enabled state 9741 * is returned (non-zero -> write cache enabled) 9742 * 9743 * 9744 * Return Code: EIO 9745 * code returned by sd_send_scsi_MODE_SENSE 9746 * 9747 * Context: Kernel Thread 9748 * 9749 * NOTE: If ioctl is added to disable write cache, this sequence should 9750 * be followed so that no locking is required for accesses to 9751 * un->un_f_write_cache_enabled: 9752 * do mode select to clear wce 9753 * do synchronize cache to flush cache 9754 * set un->un_f_write_cache_enabled = FALSE 9755 * 9756 * Conversely, an ioctl to enable the write cache should be done 9757 * in this order: 9758 * set un->un_f_write_cache_enabled = TRUE 9759 * do mode select to set wce 9760 */ 9761 9762 static int 9763 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 9764 { 9765 struct mode_caching *mode_caching_page; 9766 uchar_t *header; 9767 size_t buflen; 9768 int hdrlen; 9769 int bd_len; 9770 int rval = 0; 9771 9772 ASSERT(un != NULL); 9773 ASSERT(is_enabled != NULL); 9774 9775 /* in case of error, flag as enabled */ 9776 *is_enabled = TRUE; 9777 9778 /* 9779 * Do a test unit ready, otherwise a mode sense may not work if this 9780 * is the first command sent to the device after boot. 9781 */ 9782 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9783 9784 if (un->un_f_cfg_is_atapi == TRUE) { 9785 hdrlen = MODE_HEADER_LENGTH_GRP2; 9786 } else { 9787 hdrlen = MODE_HEADER_LENGTH; 9788 } 9789 9790 /* 9791 * Allocate memory for the retrieved mode page and its headers. Set 9792 * a pointer to the page itself. 9793 */ 9794 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9795 header = kmem_zalloc(buflen, KM_SLEEP); 9796 9797 /* Get the information from the device. */ 9798 if (un->un_f_cfg_is_atapi == TRUE) { 9799 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9800 MODEPAGE_CACHING, SD_PATH_DIRECT); 9801 } else { 9802 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9803 MODEPAGE_CACHING, SD_PATH_DIRECT); 9804 } 9805 if (rval != 0) { 9806 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9807 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9808 kmem_free(header, buflen); 9809 return (rval); 9810 } 9811 9812 /* 9813 * Determine size of Block Descriptors in order to locate 9814 * the mode page data. ATAPI devices return 0, SCSI devices 9815 * should return MODE_BLK_DESC_LENGTH. 9816 */ 9817 if (un->un_f_cfg_is_atapi == TRUE) { 9818 struct mode_header_grp2 *mhp; 9819 mhp = (struct mode_header_grp2 *)header; 9820 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9821 } else { 9822 bd_len = ((struct mode_header *)header)->bdesc_length; 9823 } 9824 9825 if (bd_len > MODE_BLK_DESC_LENGTH) { 9826 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9827 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9828 "block descriptor length\n"); 9829 kmem_free(header, buflen); 9830 return (EIO); 9831 } 9832 9833 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9834 *is_enabled = mode_caching_page->wce; 9835 9836 kmem_free(header, buflen); 9837 return (0); 9838 } 9839 9840 9841 /* 9842 * Function: sd_make_device 9843 * 9844 * Description: Utility routine to return the Solaris device number from 9845 * the data in the device's dev_info structure. 9846 * 9847 * Return Code: The Solaris device number 9848 * 9849 * Context: Any 9850 */ 9851 9852 static dev_t 9853 sd_make_device(dev_info_t *devi) 9854 { 9855 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 9856 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9857 } 9858 9859 9860 /* 9861 * Function: sd_pm_entry 9862 * 9863 * Description: Called at the start of a new command to manage power 9864 * and busy status of a device. This includes determining whether 9865 * the current power state of the device is sufficient for 9866 * performing the command or whether it must be changed. 9867 * The PM framework is notified appropriately. 9868 * Only with a return status of DDI_SUCCESS will the 9869 * component be busy to the framework. 9870 * 9871 * All callers of sd_pm_entry must check the return status 9872 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9873 * of DDI_FAILURE indicates the device failed to power up. 9874 * In this case un_pm_count has been adjusted so the result 9875 * on exit is still powered down, ie. count is less than 0. 9876 * Calling sd_pm_exit with this count value hits an ASSERT. 9877 * 9878 * Return Code: DDI_SUCCESS or DDI_FAILURE 9879 * 9880 * Context: Kernel thread context. 9881 */ 9882 9883 static int 9884 sd_pm_entry(struct sd_lun *un) 9885 { 9886 int return_status = DDI_SUCCESS; 9887 9888 ASSERT(!mutex_owned(SD_MUTEX(un))); 9889 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9890 9891 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9892 9893 if (un->un_f_pm_is_enabled == FALSE) { 9894 SD_TRACE(SD_LOG_IO_PM, un, 9895 "sd_pm_entry: exiting, PM not enabled\n"); 9896 return (return_status); 9897 } 9898 9899 /* 9900 * Just increment a counter if PM is enabled. On the transition from 9901 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9902 * the count with each IO and mark the device as idle when the count 9903 * hits 0. 9904 * 9905 * If the count is less than 0 the device is powered down. If a powered 9906 * down device is successfully powered up then the count must be 9907 * incremented to reflect the power up. Note that it'll get incremented 9908 * a second time to become busy. 9909 * 9910 * Because the following has the potential to change the device state 9911 * and must release the un_pm_mutex to do so, only one thread can be 9912 * allowed through at a time. 9913 */ 9914 9915 mutex_enter(&un->un_pm_mutex); 9916 while (un->un_pm_busy == TRUE) { 9917 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9918 } 9919 un->un_pm_busy = TRUE; 9920 9921 if (un->un_pm_count < 1) { 9922 9923 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9924 9925 /* 9926 * Indicate we are now busy so the framework won't attempt to 9927 * power down the device. This call will only fail if either 9928 * we passed a bad component number or the device has no 9929 * components. Neither of these should ever happen. 9930 */ 9931 mutex_exit(&un->un_pm_mutex); 9932 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9933 ASSERT(return_status == DDI_SUCCESS); 9934 9935 mutex_enter(&un->un_pm_mutex); 9936 9937 if (un->un_pm_count < 0) { 9938 mutex_exit(&un->un_pm_mutex); 9939 9940 SD_TRACE(SD_LOG_IO_PM, un, 9941 "sd_pm_entry: power up component\n"); 9942 9943 /* 9944 * pm_raise_power will cause sdpower to be called 9945 * which brings the device power level to the 9946 * desired state, ON in this case. If successful, 9947 * un_pm_count and un_power_level will be updated 9948 * appropriately. 9949 */ 9950 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9951 SD_SPINDLE_ON); 9952 9953 mutex_enter(&un->un_pm_mutex); 9954 9955 if (return_status != DDI_SUCCESS) { 9956 /* 9957 * Power up failed. 9958 * Idle the device and adjust the count 9959 * so the result on exit is that we're 9960 * still powered down, ie. count is less than 0. 9961 */ 9962 SD_TRACE(SD_LOG_IO_PM, un, 9963 "sd_pm_entry: power up failed," 9964 " idle the component\n"); 9965 9966 (void) pm_idle_component(SD_DEVINFO(un), 0); 9967 un->un_pm_count--; 9968 } else { 9969 /* 9970 * Device is powered up, verify the 9971 * count is non-negative. 9972 * This is debug only. 9973 */ 9974 ASSERT(un->un_pm_count == 0); 9975 } 9976 } 9977 9978 if (return_status == DDI_SUCCESS) { 9979 /* 9980 * For performance, now that the device has been tagged 9981 * as busy, and it's known to be powered up, update the 9982 * chain types to use jump tables that do not include 9983 * pm. This significantly lowers the overhead and 9984 * therefore improves performance. 9985 */ 9986 9987 mutex_exit(&un->un_pm_mutex); 9988 mutex_enter(SD_MUTEX(un)); 9989 SD_TRACE(SD_LOG_IO_PM, un, 9990 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9991 un->un_uscsi_chain_type); 9992 9993 if (un->un_f_non_devbsize_supported) { 9994 un->un_buf_chain_type = 9995 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9996 } else { 9997 un->un_buf_chain_type = 9998 SD_CHAIN_INFO_DISK_NO_PM; 9999 } 10000 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10001 10002 SD_TRACE(SD_LOG_IO_PM, un, 10003 " changed uscsi_chain_type to %d\n", 10004 un->un_uscsi_chain_type); 10005 mutex_exit(SD_MUTEX(un)); 10006 mutex_enter(&un->un_pm_mutex); 10007 10008 if (un->un_pm_idle_timeid == NULL) { 10009 /* 300 ms. */ 10010 un->un_pm_idle_timeid = 10011 timeout(sd_pm_idletimeout_handler, un, 10012 (drv_usectohz((clock_t)300000))); 10013 /* 10014 * Include an extra call to busy which keeps the 10015 * device busy with-respect-to the PM layer 10016 * until the timer fires, at which time it'll 10017 * get the extra idle call. 10018 */ 10019 (void) pm_busy_component(SD_DEVINFO(un), 0); 10020 } 10021 } 10022 } 10023 un->un_pm_busy = FALSE; 10024 /* Next... */ 10025 cv_signal(&un->un_pm_busy_cv); 10026 10027 un->un_pm_count++; 10028 10029 SD_TRACE(SD_LOG_IO_PM, un, 10030 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10031 10032 mutex_exit(&un->un_pm_mutex); 10033 10034 return (return_status); 10035 } 10036 10037 10038 /* 10039 * Function: sd_pm_exit 10040 * 10041 * Description: Called at the completion of a command to manage busy 10042 * status for the device. If the device becomes idle the 10043 * PM framework is notified. 10044 * 10045 * Context: Kernel thread context 10046 */ 10047 10048 static void 10049 sd_pm_exit(struct sd_lun *un) 10050 { 10051 ASSERT(!mutex_owned(SD_MUTEX(un))); 10052 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10053 10054 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10055 10056 /* 10057 * After attach the following flag is only read, so don't 10058 * take the penalty of acquiring a mutex for it. 10059 */ 10060 if (un->un_f_pm_is_enabled == TRUE) { 10061 10062 mutex_enter(&un->un_pm_mutex); 10063 un->un_pm_count--; 10064 10065 SD_TRACE(SD_LOG_IO_PM, un, 10066 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10067 10068 ASSERT(un->un_pm_count >= 0); 10069 if (un->un_pm_count == 0) { 10070 mutex_exit(&un->un_pm_mutex); 10071 10072 SD_TRACE(SD_LOG_IO_PM, un, 10073 "sd_pm_exit: idle component\n"); 10074 10075 (void) pm_idle_component(SD_DEVINFO(un), 0); 10076 10077 } else { 10078 mutex_exit(&un->un_pm_mutex); 10079 } 10080 } 10081 10082 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10083 } 10084 10085 10086 /* 10087 * Function: sdopen 10088 * 10089 * Description: Driver's open(9e) entry point function. 10090 * 10091 * Arguments: dev_i - pointer to device number 10092 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10093 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10094 * cred_p - user credential pointer 10095 * 10096 * Return Code: EINVAL 10097 * ENXIO 10098 * EIO 10099 * EROFS 10100 * EBUSY 10101 * 10102 * Context: Kernel thread context 10103 */ 10104 /* ARGSUSED */ 10105 static int 10106 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10107 { 10108 struct sd_lun *un; 10109 int nodelay; 10110 int part; 10111 uint64_t partmask; 10112 int instance; 10113 dev_t dev; 10114 int rval = EIO; 10115 10116 /* Validate the open type */ 10117 if (otyp >= OTYPCNT) { 10118 return (EINVAL); 10119 } 10120 10121 dev = *dev_p; 10122 instance = SDUNIT(dev); 10123 mutex_enter(&sd_detach_mutex); 10124 10125 /* 10126 * Fail the open if there is no softstate for the instance, or 10127 * if another thread somewhere is trying to detach the instance. 10128 */ 10129 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10130 (un->un_detach_count != 0)) { 10131 mutex_exit(&sd_detach_mutex); 10132 /* 10133 * The probe cache only needs to be cleared when open (9e) fails 10134 * with ENXIO (4238046). 10135 */ 10136 /* 10137 * un-conditionally clearing probe cache is ok with 10138 * separate sd/ssd binaries 10139 * x86 platform can be an issue with both parallel 10140 * and fibre in 1 binary 10141 */ 10142 sd_scsi_clear_probe_cache(); 10143 return (ENXIO); 10144 } 10145 10146 /* 10147 * The un_layer_count is to prevent another thread in specfs from 10148 * trying to detach the instance, which can happen when we are 10149 * called from a higher-layer driver instead of thru specfs. 10150 * This will not be needed when DDI provides a layered driver 10151 * interface that allows specfs to know that an instance is in 10152 * use by a layered driver & should not be detached. 10153 * 10154 * Note: the semantics for layered driver opens are exactly one 10155 * close for every open. 10156 */ 10157 if (otyp == OTYP_LYR) { 10158 un->un_layer_count++; 10159 } 10160 10161 /* 10162 * Keep a count of the current # of opens in progress. This is because 10163 * some layered drivers try to call us as a regular open. This can 10164 * cause problems that we cannot prevent, however by keeping this count 10165 * we can at least keep our open and detach routines from racing against 10166 * each other under such conditions. 10167 */ 10168 un->un_opens_in_progress++; 10169 mutex_exit(&sd_detach_mutex); 10170 10171 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10172 part = SDPART(dev); 10173 partmask = 1 << part; 10174 10175 /* 10176 * We use a semaphore here in order to serialize 10177 * open and close requests on the device. 10178 */ 10179 sema_p(&un->un_semoclose); 10180 10181 mutex_enter(SD_MUTEX(un)); 10182 10183 /* 10184 * All device accesses go thru sdstrategy() where we check 10185 * on suspend status but there could be a scsi_poll command, 10186 * which bypasses sdstrategy(), so we need to check pm 10187 * status. 10188 */ 10189 10190 if (!nodelay) { 10191 while ((un->un_state == SD_STATE_SUSPENDED) || 10192 (un->un_state == SD_STATE_PM_CHANGING)) { 10193 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10194 } 10195 10196 mutex_exit(SD_MUTEX(un)); 10197 if (sd_pm_entry(un) != DDI_SUCCESS) { 10198 rval = EIO; 10199 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10200 "sdopen: sd_pm_entry failed\n"); 10201 goto open_failed_with_pm; 10202 } 10203 mutex_enter(SD_MUTEX(un)); 10204 } 10205 10206 /* check for previous exclusive open */ 10207 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10208 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10209 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10210 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10211 10212 if (un->un_exclopen & (partmask)) { 10213 goto excl_open_fail; 10214 } 10215 10216 if (flag & FEXCL) { 10217 int i; 10218 if (un->un_ocmap.lyropen[part]) { 10219 goto excl_open_fail; 10220 } 10221 for (i = 0; i < (OTYPCNT - 1); i++) { 10222 if (un->un_ocmap.regopen[i] & (partmask)) { 10223 goto excl_open_fail; 10224 } 10225 } 10226 } 10227 10228 /* 10229 * Check the write permission if this is a removable media device, 10230 * NDELAY has not been set, and writable permission is requested. 10231 * 10232 * Note: If NDELAY was set and this is write-protected media the WRITE 10233 * attempt will fail with EIO as part of the I/O processing. This is a 10234 * more permissive implementation that allows the open to succeed and 10235 * WRITE attempts to fail when appropriate. 10236 */ 10237 if (un->un_f_chk_wp_open) { 10238 if ((flag & FWRITE) && (!nodelay)) { 10239 mutex_exit(SD_MUTEX(un)); 10240 /* 10241 * Defer the check for write permission on writable 10242 * DVD drive till sdstrategy and will not fail open even 10243 * if FWRITE is set as the device can be writable 10244 * depending upon the media and the media can change 10245 * after the call to open(). 10246 */ 10247 if (un->un_f_dvdram_writable_device == FALSE) { 10248 if (ISCD(un) || sr_check_wp(dev)) { 10249 rval = EROFS; 10250 mutex_enter(SD_MUTEX(un)); 10251 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10252 "write to cd or write protected media\n"); 10253 goto open_fail; 10254 } 10255 } 10256 mutex_enter(SD_MUTEX(un)); 10257 } 10258 } 10259 10260 /* 10261 * If opening in NDELAY/NONBLOCK mode, just return. 10262 * Check if disk is ready and has a valid geometry later. 10263 */ 10264 if (!nodelay) { 10265 mutex_exit(SD_MUTEX(un)); 10266 rval = sd_ready_and_valid(un); 10267 mutex_enter(SD_MUTEX(un)); 10268 /* 10269 * Fail if device is not ready or if the number of disk 10270 * blocks is zero or negative for non CD devices. 10271 */ 10272 if ((rval != SD_READY_VALID) || 10273 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10274 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10275 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10276 "device not ready or invalid disk block value\n"); 10277 goto open_fail; 10278 } 10279 #if defined(__i386) || defined(__amd64) 10280 } else { 10281 uchar_t *cp; 10282 /* 10283 * x86 requires special nodelay handling, so that p0 is 10284 * always defined and accessible. 10285 * Invalidate geometry only if device is not already open. 10286 */ 10287 cp = &un->un_ocmap.chkd[0]; 10288 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10289 if (*cp != (uchar_t)0) { 10290 break; 10291 } 10292 cp++; 10293 } 10294 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10295 un->un_f_geometry_is_valid = FALSE; 10296 } 10297 10298 #endif 10299 } 10300 10301 if (otyp == OTYP_LYR) { 10302 un->un_ocmap.lyropen[part]++; 10303 } else { 10304 un->un_ocmap.regopen[otyp] |= partmask; 10305 } 10306 10307 /* Set up open and exclusive open flags */ 10308 if (flag & FEXCL) { 10309 un->un_exclopen |= (partmask); 10310 } 10311 10312 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10313 "open of part %d type %d\n", part, otyp); 10314 10315 mutex_exit(SD_MUTEX(un)); 10316 if (!nodelay) { 10317 sd_pm_exit(un); 10318 } 10319 10320 sema_v(&un->un_semoclose); 10321 10322 mutex_enter(&sd_detach_mutex); 10323 un->un_opens_in_progress--; 10324 mutex_exit(&sd_detach_mutex); 10325 10326 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10327 return (DDI_SUCCESS); 10328 10329 excl_open_fail: 10330 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10331 rval = EBUSY; 10332 10333 open_fail: 10334 mutex_exit(SD_MUTEX(un)); 10335 10336 /* 10337 * On a failed open we must exit the pm management. 10338 */ 10339 if (!nodelay) { 10340 sd_pm_exit(un); 10341 } 10342 open_failed_with_pm: 10343 sema_v(&un->un_semoclose); 10344 10345 mutex_enter(&sd_detach_mutex); 10346 un->un_opens_in_progress--; 10347 if (otyp == OTYP_LYR) { 10348 un->un_layer_count--; 10349 } 10350 mutex_exit(&sd_detach_mutex); 10351 10352 return (rval); 10353 } 10354 10355 10356 /* 10357 * Function: sdclose 10358 * 10359 * Description: Driver's close(9e) entry point function. 10360 * 10361 * Arguments: dev - device number 10362 * flag - file status flag, informational only 10363 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10364 * cred_p - user credential pointer 10365 * 10366 * Return Code: ENXIO 10367 * 10368 * Context: Kernel thread context 10369 */ 10370 /* ARGSUSED */ 10371 static int 10372 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10373 { 10374 struct sd_lun *un; 10375 uchar_t *cp; 10376 int part; 10377 int nodelay; 10378 int rval = 0; 10379 10380 /* Validate the open type */ 10381 if (otyp >= OTYPCNT) { 10382 return (ENXIO); 10383 } 10384 10385 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10386 return (ENXIO); 10387 } 10388 10389 part = SDPART(dev); 10390 nodelay = flag & (FNDELAY | FNONBLOCK); 10391 10392 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10393 "sdclose: close of part %d type %d\n", part, otyp); 10394 10395 /* 10396 * We use a semaphore here in order to serialize 10397 * open and close requests on the device. 10398 */ 10399 sema_p(&un->un_semoclose); 10400 10401 mutex_enter(SD_MUTEX(un)); 10402 10403 /* Don't proceed if power is being changed. */ 10404 while (un->un_state == SD_STATE_PM_CHANGING) { 10405 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10406 } 10407 10408 if (un->un_exclopen & (1 << part)) { 10409 un->un_exclopen &= ~(1 << part); 10410 } 10411 10412 /* Update the open partition map */ 10413 if (otyp == OTYP_LYR) { 10414 un->un_ocmap.lyropen[part] -= 1; 10415 } else { 10416 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10417 } 10418 10419 cp = &un->un_ocmap.chkd[0]; 10420 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10421 if (*cp != NULL) { 10422 break; 10423 } 10424 cp++; 10425 } 10426 10427 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10428 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10429 10430 /* 10431 * We avoid persistance upon the last close, and set 10432 * the throttle back to the maximum. 10433 */ 10434 un->un_throttle = un->un_saved_throttle; 10435 10436 if (un->un_state == SD_STATE_OFFLINE) { 10437 if (un->un_f_is_fibre == FALSE) { 10438 scsi_log(SD_DEVINFO(un), sd_label, 10439 CE_WARN, "offline\n"); 10440 } 10441 un->un_f_geometry_is_valid = FALSE; 10442 10443 } else { 10444 /* 10445 * Flush any outstanding writes in NVRAM cache. 10446 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10447 * cmd, it may not work for non-Pluto devices. 10448 * SYNCHRONIZE CACHE is not required for removables, 10449 * except DVD-RAM drives. 10450 * 10451 * Also note: because SYNCHRONIZE CACHE is currently 10452 * the only command issued here that requires the 10453 * drive be powered up, only do the power up before 10454 * sending the Sync Cache command. If additional 10455 * commands are added which require a powered up 10456 * drive, the following sequence may have to change. 10457 * 10458 * And finally, note that parallel SCSI on SPARC 10459 * only issues a Sync Cache to DVD-RAM, a newly 10460 * supported device. 10461 */ 10462 #if defined(__i386) || defined(__amd64) 10463 if (un->un_f_sync_cache_supported || 10464 un->un_f_dvdram_writable_device == TRUE) { 10465 #else 10466 if (un->un_f_dvdram_writable_device == TRUE) { 10467 #endif 10468 mutex_exit(SD_MUTEX(un)); 10469 if (sd_pm_entry(un) == DDI_SUCCESS) { 10470 rval = 10471 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10472 NULL); 10473 /* ignore error if not supported */ 10474 if (rval == ENOTSUP) { 10475 rval = 0; 10476 } else if (rval != 0) { 10477 rval = EIO; 10478 } 10479 sd_pm_exit(un); 10480 } else { 10481 rval = EIO; 10482 } 10483 mutex_enter(SD_MUTEX(un)); 10484 } 10485 10486 /* 10487 * For devices which supports DOOR_LOCK, send an ALLOW 10488 * MEDIA REMOVAL command, but don't get upset if it 10489 * fails. We need to raise the power of the drive before 10490 * we can call sd_send_scsi_DOORLOCK() 10491 */ 10492 if (un->un_f_doorlock_supported) { 10493 mutex_exit(SD_MUTEX(un)); 10494 if (sd_pm_entry(un) == DDI_SUCCESS) { 10495 rval = sd_send_scsi_DOORLOCK(un, 10496 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10497 10498 sd_pm_exit(un); 10499 if (ISCD(un) && (rval != 0) && 10500 (nodelay != 0)) { 10501 rval = ENXIO; 10502 } 10503 } else { 10504 rval = EIO; 10505 } 10506 mutex_enter(SD_MUTEX(un)); 10507 } 10508 10509 /* 10510 * If a device has removable media, invalidate all 10511 * parameters related to media, such as geometry, 10512 * blocksize, and blockcount. 10513 */ 10514 if (un->un_f_has_removable_media) { 10515 sr_ejected(un); 10516 } 10517 10518 } 10519 } 10520 10521 /* 10522 * Destroy the cache (if it exists) which was 10523 * allocated for the write maps since this is 10524 * the last close for this media. 10525 */ 10526 if (un->un_wm_cache) { 10527 /* 10528 * Check if there are pending commands. 10529 * and if there are give a warning and 10530 * do not destroy the cache. 10531 */ 10532 if (un->un_ncmds_in_driver > 0) { 10533 scsi_log(SD_DEVINFO(un), 10534 sd_label, CE_WARN, 10535 "Unable to clean up memory " 10536 "because of pending I/O\n"); 10537 } else { 10538 kmem_cache_destroy( 10539 un->un_wm_cache); 10540 un->un_wm_cache = NULL; 10541 } 10542 } 10543 10544 mutex_exit(SD_MUTEX(un)); 10545 sema_v(&un->un_semoclose); 10546 10547 if (otyp == OTYP_LYR) { 10548 mutex_enter(&sd_detach_mutex); 10549 /* 10550 * The detach routine may run when the layer count 10551 * drops to zero. 10552 */ 10553 un->un_layer_count--; 10554 mutex_exit(&sd_detach_mutex); 10555 } 10556 10557 return (rval); 10558 } 10559 10560 10561 /* 10562 * Function: sd_ready_and_valid 10563 * 10564 * Description: Test if device is ready and has a valid geometry. 10565 * 10566 * Arguments: dev - device number 10567 * un - driver soft state (unit) structure 10568 * 10569 * Return Code: SD_READY_VALID ready and valid label 10570 * SD_READY_NOT_VALID ready, geom ops never applicable 10571 * SD_NOT_READY_VALID not ready, no label 10572 * 10573 * Context: Never called at interrupt context. 10574 */ 10575 10576 static int 10577 sd_ready_and_valid(struct sd_lun *un) 10578 { 10579 struct sd_errstats *stp; 10580 uint64_t capacity; 10581 uint_t lbasize; 10582 int rval = SD_READY_VALID; 10583 char name_str[48]; 10584 10585 ASSERT(un != NULL); 10586 ASSERT(!mutex_owned(SD_MUTEX(un))); 10587 10588 mutex_enter(SD_MUTEX(un)); 10589 /* 10590 * If a device has removable media, we must check if media is 10591 * ready when checking if this device is ready and valid. 10592 */ 10593 if (un->un_f_has_removable_media) { 10594 mutex_exit(SD_MUTEX(un)); 10595 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10596 rval = SD_NOT_READY_VALID; 10597 mutex_enter(SD_MUTEX(un)); 10598 goto done; 10599 } 10600 10601 mutex_enter(SD_MUTEX(un)); 10602 if ((un->un_f_geometry_is_valid == FALSE) || 10603 (un->un_f_blockcount_is_valid == FALSE) || 10604 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10605 10606 /* capacity has to be read every open. */ 10607 mutex_exit(SD_MUTEX(un)); 10608 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10609 &lbasize, SD_PATH_DIRECT) != 0) { 10610 mutex_enter(SD_MUTEX(un)); 10611 un->un_f_geometry_is_valid = FALSE; 10612 rval = SD_NOT_READY_VALID; 10613 goto done; 10614 } else { 10615 mutex_enter(SD_MUTEX(un)); 10616 sd_update_block_info(un, lbasize, capacity); 10617 } 10618 } 10619 10620 /* 10621 * Check if the media in the device is writable or not. 10622 */ 10623 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10624 sd_check_for_writable_cd(un); 10625 } 10626 10627 } else { 10628 /* 10629 * Do a test unit ready to clear any unit attention from non-cd 10630 * devices. 10631 */ 10632 mutex_exit(SD_MUTEX(un)); 10633 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10634 mutex_enter(SD_MUTEX(un)); 10635 } 10636 10637 10638 /* 10639 * If this is a non 512 block device, allocate space for 10640 * the wmap cache. This is being done here since every time 10641 * a media is changed this routine will be called and the 10642 * block size is a function of media rather than device. 10643 */ 10644 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 10645 if (!(un->un_wm_cache)) { 10646 (void) snprintf(name_str, sizeof (name_str), 10647 "%s%d_cache", 10648 ddi_driver_name(SD_DEVINFO(un)), 10649 ddi_get_instance(SD_DEVINFO(un))); 10650 un->un_wm_cache = kmem_cache_create( 10651 name_str, sizeof (struct sd_w_map), 10652 8, sd_wm_cache_constructor, 10653 sd_wm_cache_destructor, NULL, 10654 (void *)un, NULL, 0); 10655 if (!(un->un_wm_cache)) { 10656 rval = ENOMEM; 10657 goto done; 10658 } 10659 } 10660 } 10661 10662 if (un->un_state == SD_STATE_NORMAL) { 10663 /* 10664 * If the target is not yet ready here (defined by a TUR 10665 * failure), invalidate the geometry and print an 'offline' 10666 * message. This is a legacy message, as the state of the 10667 * target is not actually changed to SD_STATE_OFFLINE. 10668 * 10669 * If the TUR fails for EACCES (Reservation Conflict), it 10670 * means there actually is nothing wrong with the target that 10671 * would require invalidating the geometry, so continue in 10672 * that case as if the TUR was successful. 10673 */ 10674 int err; 10675 10676 mutex_exit(SD_MUTEX(un)); 10677 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10678 mutex_enter(SD_MUTEX(un)); 10679 10680 if ((err != 0) && (err != EACCES)) { 10681 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10682 "offline\n"); 10683 un->un_f_geometry_is_valid = FALSE; 10684 rval = SD_NOT_READY_VALID; 10685 goto done; 10686 } 10687 } 10688 10689 if (un->un_f_format_in_progress == FALSE) { 10690 /* 10691 * Note: sd_validate_geometry may return TRUE, but that does 10692 * not necessarily mean un_f_geometry_is_valid == TRUE! 10693 */ 10694 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10695 if (rval == ENOTSUP) { 10696 if (un->un_f_geometry_is_valid == TRUE) 10697 rval = 0; 10698 else { 10699 rval = SD_READY_NOT_VALID; 10700 goto done; 10701 } 10702 } 10703 if (rval != 0) { 10704 /* 10705 * We don't check the validity of geometry for 10706 * CDROMs. Also we assume we have a good label 10707 * even if sd_validate_geometry returned ENOMEM. 10708 */ 10709 if (!ISCD(un) && rval != ENOMEM) { 10710 rval = SD_NOT_READY_VALID; 10711 goto done; 10712 } 10713 } 10714 } 10715 10716 #ifdef DOESNTWORK /* on eliteII, see 1118607 */ 10717 /* 10718 * check to see if this disk is write protected, if it is and we have 10719 * not set read-only, then fail 10720 */ 10721 if ((flag & FWRITE) && (sr_check_wp(dev))) { 10722 New_state(un, SD_STATE_CLOSED); 10723 goto done; 10724 } 10725 #endif 10726 10727 /* 10728 * If this device supports DOOR_LOCK command, try and send 10729 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10730 * if it fails. For a CD, however, it is an error 10731 */ 10732 if (un->un_f_doorlock_supported) { 10733 mutex_exit(SD_MUTEX(un)); 10734 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10735 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10736 rval = SD_NOT_READY_VALID; 10737 mutex_enter(SD_MUTEX(un)); 10738 goto done; 10739 } 10740 mutex_enter(SD_MUTEX(un)); 10741 } 10742 10743 /* The state has changed, inform the media watch routines */ 10744 un->un_mediastate = DKIO_INSERTED; 10745 cv_broadcast(&un->un_state_cv); 10746 rval = SD_READY_VALID; 10747 10748 done: 10749 10750 /* 10751 * Initialize the capacity kstat value, if no media previously 10752 * (capacity kstat is 0) and a media has been inserted 10753 * (un_blockcount > 0). 10754 */ 10755 if (un->un_errstats != NULL) { 10756 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10757 if ((stp->sd_capacity.value.ui64 == 0) && 10758 (un->un_f_blockcount_is_valid == TRUE)) { 10759 stp->sd_capacity.value.ui64 = 10760 (uint64_t)((uint64_t)un->un_blockcount * 10761 un->un_sys_blocksize); 10762 } 10763 } 10764 10765 mutex_exit(SD_MUTEX(un)); 10766 return (rval); 10767 } 10768 10769 10770 /* 10771 * Function: sdmin 10772 * 10773 * Description: Routine to limit the size of a data transfer. Used in 10774 * conjunction with physio(9F). 10775 * 10776 * Arguments: bp - pointer to the indicated buf(9S) struct. 10777 * 10778 * Context: Kernel thread context. 10779 */ 10780 10781 static void 10782 sdmin(struct buf *bp) 10783 { 10784 struct sd_lun *un; 10785 int instance; 10786 10787 instance = SDUNIT(bp->b_edev); 10788 10789 un = ddi_get_soft_state(sd_state, instance); 10790 ASSERT(un != NULL); 10791 10792 if (bp->b_bcount > un->un_max_xfer_size) { 10793 bp->b_bcount = un->un_max_xfer_size; 10794 } 10795 } 10796 10797 10798 /* 10799 * Function: sdread 10800 * 10801 * Description: Driver's read(9e) entry point function. 10802 * 10803 * Arguments: dev - device number 10804 * uio - structure pointer describing where data is to be stored 10805 * in user's space 10806 * cred_p - user credential pointer 10807 * 10808 * Return Code: ENXIO 10809 * EIO 10810 * EINVAL 10811 * value returned by physio 10812 * 10813 * Context: Kernel thread context. 10814 */ 10815 /* ARGSUSED */ 10816 static int 10817 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10818 { 10819 struct sd_lun *un = NULL; 10820 int secmask; 10821 int err; 10822 10823 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10824 return (ENXIO); 10825 } 10826 10827 ASSERT(!mutex_owned(SD_MUTEX(un))); 10828 10829 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10830 mutex_enter(SD_MUTEX(un)); 10831 /* 10832 * Because the call to sd_ready_and_valid will issue I/O we 10833 * must wait here if either the device is suspended or 10834 * if it's power level is changing. 10835 */ 10836 while ((un->un_state == SD_STATE_SUSPENDED) || 10837 (un->un_state == SD_STATE_PM_CHANGING)) { 10838 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10839 } 10840 un->un_ncmds_in_driver++; 10841 mutex_exit(SD_MUTEX(un)); 10842 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10843 mutex_enter(SD_MUTEX(un)); 10844 un->un_ncmds_in_driver--; 10845 ASSERT(un->un_ncmds_in_driver >= 0); 10846 mutex_exit(SD_MUTEX(un)); 10847 return (EIO); 10848 } 10849 mutex_enter(SD_MUTEX(un)); 10850 un->un_ncmds_in_driver--; 10851 ASSERT(un->un_ncmds_in_driver >= 0); 10852 mutex_exit(SD_MUTEX(un)); 10853 } 10854 10855 /* 10856 * Read requests are restricted to multiples of the system block size. 10857 */ 10858 secmask = un->un_sys_blocksize - 1; 10859 10860 if (uio->uio_loffset & ((offset_t)(secmask))) { 10861 SD_ERROR(SD_LOG_READ_WRITE, un, 10862 "sdread: file offset not modulo %d\n", 10863 un->un_sys_blocksize); 10864 err = EINVAL; 10865 } else if (uio->uio_iov->iov_len & (secmask)) { 10866 SD_ERROR(SD_LOG_READ_WRITE, un, 10867 "sdread: transfer length not modulo %d\n", 10868 un->un_sys_blocksize); 10869 err = EINVAL; 10870 } else { 10871 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10872 } 10873 return (err); 10874 } 10875 10876 10877 /* 10878 * Function: sdwrite 10879 * 10880 * Description: Driver's write(9e) entry point function. 10881 * 10882 * Arguments: dev - device number 10883 * uio - structure pointer describing where data is stored in 10884 * user's space 10885 * cred_p - user credential pointer 10886 * 10887 * Return Code: ENXIO 10888 * EIO 10889 * EINVAL 10890 * value returned by physio 10891 * 10892 * Context: Kernel thread context. 10893 */ 10894 /* ARGSUSED */ 10895 static int 10896 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10897 { 10898 struct sd_lun *un = NULL; 10899 int secmask; 10900 int err; 10901 10902 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10903 return (ENXIO); 10904 } 10905 10906 ASSERT(!mutex_owned(SD_MUTEX(un))); 10907 10908 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10909 mutex_enter(SD_MUTEX(un)); 10910 /* 10911 * Because the call to sd_ready_and_valid will issue I/O we 10912 * must wait here if either the device is suspended or 10913 * if it's power level is changing. 10914 */ 10915 while ((un->un_state == SD_STATE_SUSPENDED) || 10916 (un->un_state == SD_STATE_PM_CHANGING)) { 10917 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10918 } 10919 un->un_ncmds_in_driver++; 10920 mutex_exit(SD_MUTEX(un)); 10921 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10922 mutex_enter(SD_MUTEX(un)); 10923 un->un_ncmds_in_driver--; 10924 ASSERT(un->un_ncmds_in_driver >= 0); 10925 mutex_exit(SD_MUTEX(un)); 10926 return (EIO); 10927 } 10928 mutex_enter(SD_MUTEX(un)); 10929 un->un_ncmds_in_driver--; 10930 ASSERT(un->un_ncmds_in_driver >= 0); 10931 mutex_exit(SD_MUTEX(un)); 10932 } 10933 10934 /* 10935 * Write requests are restricted to multiples of the system block size. 10936 */ 10937 secmask = un->un_sys_blocksize - 1; 10938 10939 if (uio->uio_loffset & ((offset_t)(secmask))) { 10940 SD_ERROR(SD_LOG_READ_WRITE, un, 10941 "sdwrite: file offset not modulo %d\n", 10942 un->un_sys_blocksize); 10943 err = EINVAL; 10944 } else if (uio->uio_iov->iov_len & (secmask)) { 10945 SD_ERROR(SD_LOG_READ_WRITE, un, 10946 "sdwrite: transfer length not modulo %d\n", 10947 un->un_sys_blocksize); 10948 err = EINVAL; 10949 } else { 10950 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10951 } 10952 return (err); 10953 } 10954 10955 10956 /* 10957 * Function: sdaread 10958 * 10959 * Description: Driver's aread(9e) entry point function. 10960 * 10961 * Arguments: dev - device number 10962 * aio - structure pointer describing where data is to be stored 10963 * cred_p - user credential pointer 10964 * 10965 * Return Code: ENXIO 10966 * EIO 10967 * EINVAL 10968 * value returned by aphysio 10969 * 10970 * Context: Kernel thread context. 10971 */ 10972 /* ARGSUSED */ 10973 static int 10974 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10975 { 10976 struct sd_lun *un = NULL; 10977 struct uio *uio = aio->aio_uio; 10978 int secmask; 10979 int err; 10980 10981 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10982 return (ENXIO); 10983 } 10984 10985 ASSERT(!mutex_owned(SD_MUTEX(un))); 10986 10987 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10988 mutex_enter(SD_MUTEX(un)); 10989 /* 10990 * Because the call to sd_ready_and_valid will issue I/O we 10991 * must wait here if either the device is suspended or 10992 * if it's power level is changing. 10993 */ 10994 while ((un->un_state == SD_STATE_SUSPENDED) || 10995 (un->un_state == SD_STATE_PM_CHANGING)) { 10996 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10997 } 10998 un->un_ncmds_in_driver++; 10999 mutex_exit(SD_MUTEX(un)); 11000 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11001 mutex_enter(SD_MUTEX(un)); 11002 un->un_ncmds_in_driver--; 11003 ASSERT(un->un_ncmds_in_driver >= 0); 11004 mutex_exit(SD_MUTEX(un)); 11005 return (EIO); 11006 } 11007 mutex_enter(SD_MUTEX(un)); 11008 un->un_ncmds_in_driver--; 11009 ASSERT(un->un_ncmds_in_driver >= 0); 11010 mutex_exit(SD_MUTEX(un)); 11011 } 11012 11013 /* 11014 * Read requests are restricted to multiples of the system block size. 11015 */ 11016 secmask = un->un_sys_blocksize - 1; 11017 11018 if (uio->uio_loffset & ((offset_t)(secmask))) { 11019 SD_ERROR(SD_LOG_READ_WRITE, un, 11020 "sdaread: file offset not modulo %d\n", 11021 un->un_sys_blocksize); 11022 err = EINVAL; 11023 } else if (uio->uio_iov->iov_len & (secmask)) { 11024 SD_ERROR(SD_LOG_READ_WRITE, un, 11025 "sdaread: transfer length not modulo %d\n", 11026 un->un_sys_blocksize); 11027 err = EINVAL; 11028 } else { 11029 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11030 } 11031 return (err); 11032 } 11033 11034 11035 /* 11036 * Function: sdawrite 11037 * 11038 * Description: Driver's awrite(9e) entry point function. 11039 * 11040 * Arguments: dev - device number 11041 * aio - structure pointer describing where data is stored 11042 * cred_p - user credential pointer 11043 * 11044 * Return Code: ENXIO 11045 * EIO 11046 * EINVAL 11047 * value returned by aphysio 11048 * 11049 * Context: Kernel thread context. 11050 */ 11051 /* ARGSUSED */ 11052 static int 11053 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11054 { 11055 struct sd_lun *un = NULL; 11056 struct uio *uio = aio->aio_uio; 11057 int secmask; 11058 int err; 11059 11060 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11061 return (ENXIO); 11062 } 11063 11064 ASSERT(!mutex_owned(SD_MUTEX(un))); 11065 11066 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11067 mutex_enter(SD_MUTEX(un)); 11068 /* 11069 * Because the call to sd_ready_and_valid will issue I/O we 11070 * must wait here if either the device is suspended or 11071 * if it's power level is changing. 11072 */ 11073 while ((un->un_state == SD_STATE_SUSPENDED) || 11074 (un->un_state == SD_STATE_PM_CHANGING)) { 11075 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11076 } 11077 un->un_ncmds_in_driver++; 11078 mutex_exit(SD_MUTEX(un)); 11079 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11080 mutex_enter(SD_MUTEX(un)); 11081 un->un_ncmds_in_driver--; 11082 ASSERT(un->un_ncmds_in_driver >= 0); 11083 mutex_exit(SD_MUTEX(un)); 11084 return (EIO); 11085 } 11086 mutex_enter(SD_MUTEX(un)); 11087 un->un_ncmds_in_driver--; 11088 ASSERT(un->un_ncmds_in_driver >= 0); 11089 mutex_exit(SD_MUTEX(un)); 11090 } 11091 11092 /* 11093 * Write requests are restricted to multiples of the system block size. 11094 */ 11095 secmask = un->un_sys_blocksize - 1; 11096 11097 if (uio->uio_loffset & ((offset_t)(secmask))) { 11098 SD_ERROR(SD_LOG_READ_WRITE, un, 11099 "sdawrite: file offset not modulo %d\n", 11100 un->un_sys_blocksize); 11101 err = EINVAL; 11102 } else if (uio->uio_iov->iov_len & (secmask)) { 11103 SD_ERROR(SD_LOG_READ_WRITE, un, 11104 "sdawrite: transfer length not modulo %d\n", 11105 un->un_sys_blocksize); 11106 err = EINVAL; 11107 } else { 11108 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11109 } 11110 return (err); 11111 } 11112 11113 11114 11115 11116 11117 /* 11118 * Driver IO processing follows the following sequence: 11119 * 11120 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11121 * | | ^ 11122 * v v | 11123 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11124 * | | | | 11125 * v | | | 11126 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11127 * | | ^ ^ 11128 * v v | | 11129 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11130 * | | | | 11131 * +---+ | +------------+ +-------+ 11132 * | | | | 11133 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11134 * | v | | 11135 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11136 * | | ^ | 11137 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11138 * | v | | 11139 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11140 * | | ^ | 11141 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11142 * | v | | 11143 * | sd_checksum_iostart() sd_checksum_iodone() | 11144 * | | ^ | 11145 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11146 * | v | | 11147 * | sd_pm_iostart() sd_pm_iodone() | 11148 * | | ^ | 11149 * | | | | 11150 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11151 * | ^ 11152 * v | 11153 * sd_core_iostart() | 11154 * | | 11155 * | +------>(*destroypkt)() 11156 * +-> sd_start_cmds() <-+ | | 11157 * | | | v 11158 * | | | scsi_destroy_pkt(9F) 11159 * | | | 11160 * +->(*initpkt)() +- sdintr() 11161 * | | | | 11162 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11163 * | +-> scsi_setup_cdb(9F) | 11164 * | | 11165 * +--> scsi_transport(9F) | 11166 * | | 11167 * +----> SCSA ---->+ 11168 * 11169 * 11170 * This code is based upon the following presumtions: 11171 * 11172 * - iostart and iodone functions operate on buf(9S) structures. These 11173 * functions perform the necessary operations on the buf(9S) and pass 11174 * them along to the next function in the chain by using the macros 11175 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11176 * (for iodone side functions). 11177 * 11178 * - The iostart side functions may sleep. The iodone side functions 11179 * are called under interrupt context and may NOT sleep. Therefore 11180 * iodone side functions also may not call iostart side functions. 11181 * (NOTE: iostart side functions should NOT sleep for memory, as 11182 * this could result in deadlock.) 11183 * 11184 * - An iostart side function may call its corresponding iodone side 11185 * function directly (if necessary). 11186 * 11187 * - In the event of an error, an iostart side function can return a buf(9S) 11188 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11189 * b_error in the usual way of course). 11190 * 11191 * - The taskq mechanism may be used by the iodone side functions to dispatch 11192 * requests to the iostart side functions. The iostart side functions in 11193 * this case would be called under the context of a taskq thread, so it's 11194 * OK for them to block/sleep/spin in this case. 11195 * 11196 * - iostart side functions may allocate "shadow" buf(9S) structs and 11197 * pass them along to the next function in the chain. The corresponding 11198 * iodone side functions must coalesce the "shadow" bufs and return 11199 * the "original" buf to the next higher layer. 11200 * 11201 * - The b_private field of the buf(9S) struct holds a pointer to 11202 * an sd_xbuf struct, which contains information needed to 11203 * construct the scsi_pkt for the command. 11204 * 11205 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11206 * layer must acquire & release the SD_MUTEX(un) as needed. 11207 */ 11208 11209 11210 /* 11211 * Create taskq for all targets in the system. This is created at 11212 * _init(9E) and destroyed at _fini(9E). 11213 * 11214 * Note: here we set the minalloc to a reasonably high number to ensure that 11215 * we will have an adequate supply of task entries available at interrupt time. 11216 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11217 * sd_create_taskq(). Since we do not want to sleep for allocations at 11218 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11219 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11220 * requests any one instant in time. 11221 */ 11222 #define SD_TASKQ_NUMTHREADS 8 11223 #define SD_TASKQ_MINALLOC 256 11224 #define SD_TASKQ_MAXALLOC 256 11225 11226 static taskq_t *sd_tq = NULL; 11227 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11228 11229 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11230 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11231 11232 /* 11233 * The following task queue is being created for the write part of 11234 * read-modify-write of non-512 block size devices. 11235 * Limit the number of threads to 1 for now. This number has been choosen 11236 * considering the fact that it applies only to dvd ram drives/MO drives 11237 * currently. Performance for which is not main criteria at this stage. 11238 * Note: It needs to be explored if we can use a single taskq in future 11239 */ 11240 #define SD_WMR_TASKQ_NUMTHREADS 1 11241 static taskq_t *sd_wmr_tq = NULL; 11242 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11243 11244 /* 11245 * Function: sd_taskq_create 11246 * 11247 * Description: Create taskq thread(s) and preallocate task entries 11248 * 11249 * Return Code: Returns a pointer to the allocated taskq_t. 11250 * 11251 * Context: Can sleep. Requires blockable context. 11252 * 11253 * Notes: - The taskq() facility currently is NOT part of the DDI. 11254 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11255 * - taskq_create() will block for memory, also it will panic 11256 * if it cannot create the requested number of threads. 11257 * - Currently taskq_create() creates threads that cannot be 11258 * swapped. 11259 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11260 * supply of taskq entries at interrupt time (ie, so that we 11261 * do not have to sleep for memory) 11262 */ 11263 11264 static void 11265 sd_taskq_create(void) 11266 { 11267 char taskq_name[TASKQ_NAMELEN]; 11268 11269 ASSERT(sd_tq == NULL); 11270 ASSERT(sd_wmr_tq == NULL); 11271 11272 (void) snprintf(taskq_name, sizeof (taskq_name), 11273 "%s_drv_taskq", sd_label); 11274 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11275 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11276 TASKQ_PREPOPULATE)); 11277 11278 (void) snprintf(taskq_name, sizeof (taskq_name), 11279 "%s_rmw_taskq", sd_label); 11280 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11281 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11282 TASKQ_PREPOPULATE)); 11283 } 11284 11285 11286 /* 11287 * Function: sd_taskq_delete 11288 * 11289 * Description: Complementary cleanup routine for sd_taskq_create(). 11290 * 11291 * Context: Kernel thread context. 11292 */ 11293 11294 static void 11295 sd_taskq_delete(void) 11296 { 11297 ASSERT(sd_tq != NULL); 11298 ASSERT(sd_wmr_tq != NULL); 11299 taskq_destroy(sd_tq); 11300 taskq_destroy(sd_wmr_tq); 11301 sd_tq = NULL; 11302 sd_wmr_tq = NULL; 11303 } 11304 11305 11306 /* 11307 * Function: sdstrategy 11308 * 11309 * Description: Driver's strategy (9E) entry point function. 11310 * 11311 * Arguments: bp - pointer to buf(9S) 11312 * 11313 * Return Code: Always returns zero 11314 * 11315 * Context: Kernel thread context. 11316 */ 11317 11318 static int 11319 sdstrategy(struct buf *bp) 11320 { 11321 struct sd_lun *un; 11322 11323 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11324 if (un == NULL) { 11325 bioerror(bp, EIO); 11326 bp->b_resid = bp->b_bcount; 11327 biodone(bp); 11328 return (0); 11329 } 11330 /* As was done in the past, fail new cmds. if state is dumping. */ 11331 if (un->un_state == SD_STATE_DUMPING) { 11332 bioerror(bp, ENXIO); 11333 bp->b_resid = bp->b_bcount; 11334 biodone(bp); 11335 return (0); 11336 } 11337 11338 ASSERT(!mutex_owned(SD_MUTEX(un))); 11339 11340 /* 11341 * Commands may sneak in while we released the mutex in 11342 * DDI_SUSPEND, we should block new commands. However, old 11343 * commands that are still in the driver at this point should 11344 * still be allowed to drain. 11345 */ 11346 mutex_enter(SD_MUTEX(un)); 11347 /* 11348 * Must wait here if either the device is suspended or 11349 * if it's power level is changing. 11350 */ 11351 while ((un->un_state == SD_STATE_SUSPENDED) || 11352 (un->un_state == SD_STATE_PM_CHANGING)) { 11353 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11354 } 11355 11356 un->un_ncmds_in_driver++; 11357 11358 /* 11359 * atapi: Since we are running the CD for now in PIO mode we need to 11360 * call bp_mapin here to avoid bp_mapin called interrupt context under 11361 * the HBA's init_pkt routine. 11362 */ 11363 if (un->un_f_cfg_is_atapi == TRUE) { 11364 mutex_exit(SD_MUTEX(un)); 11365 bp_mapin(bp); 11366 mutex_enter(SD_MUTEX(un)); 11367 } 11368 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11369 un->un_ncmds_in_driver); 11370 11371 mutex_exit(SD_MUTEX(un)); 11372 11373 /* 11374 * This will (eventually) allocate the sd_xbuf area and 11375 * call sd_xbuf_strategy(). We just want to return the 11376 * result of ddi_xbuf_qstrategy so that we have an opt- 11377 * imized tail call which saves us a stack frame. 11378 */ 11379 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11380 } 11381 11382 11383 /* 11384 * Function: sd_xbuf_strategy 11385 * 11386 * Description: Function for initiating IO operations via the 11387 * ddi_xbuf_qstrategy() mechanism. 11388 * 11389 * Context: Kernel thread context. 11390 */ 11391 11392 static void 11393 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11394 { 11395 struct sd_lun *un = arg; 11396 11397 ASSERT(bp != NULL); 11398 ASSERT(xp != NULL); 11399 ASSERT(un != NULL); 11400 ASSERT(!mutex_owned(SD_MUTEX(un))); 11401 11402 /* 11403 * Initialize the fields in the xbuf and save a pointer to the 11404 * xbuf in bp->b_private. 11405 */ 11406 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11407 11408 /* Send the buf down the iostart chain */ 11409 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11410 } 11411 11412 11413 /* 11414 * Function: sd_xbuf_init 11415 * 11416 * Description: Prepare the given sd_xbuf struct for use. 11417 * 11418 * Arguments: un - ptr to softstate 11419 * bp - ptr to associated buf(9S) 11420 * xp - ptr to associated sd_xbuf 11421 * chain_type - IO chain type to use: 11422 * SD_CHAIN_NULL 11423 * SD_CHAIN_BUFIO 11424 * SD_CHAIN_USCSI 11425 * SD_CHAIN_DIRECT 11426 * SD_CHAIN_DIRECT_PRIORITY 11427 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11428 * initialization; may be NULL if none. 11429 * 11430 * Context: Kernel thread context 11431 */ 11432 11433 static void 11434 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11435 uchar_t chain_type, void *pktinfop) 11436 { 11437 int index; 11438 11439 ASSERT(un != NULL); 11440 ASSERT(bp != NULL); 11441 ASSERT(xp != NULL); 11442 11443 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11444 bp, chain_type); 11445 11446 xp->xb_un = un; 11447 xp->xb_pktp = NULL; 11448 xp->xb_pktinfo = pktinfop; 11449 xp->xb_private = bp->b_private; 11450 xp->xb_blkno = (daddr_t)bp->b_blkno; 11451 11452 /* 11453 * Set up the iostart and iodone chain indexes in the xbuf, based 11454 * upon the specified chain type to use. 11455 */ 11456 switch (chain_type) { 11457 case SD_CHAIN_NULL: 11458 /* 11459 * Fall thru to just use the values for the buf type, even 11460 * tho for the NULL chain these values will never be used. 11461 */ 11462 /* FALLTHRU */ 11463 case SD_CHAIN_BUFIO: 11464 index = un->un_buf_chain_type; 11465 break; 11466 case SD_CHAIN_USCSI: 11467 index = un->un_uscsi_chain_type; 11468 break; 11469 case SD_CHAIN_DIRECT: 11470 index = un->un_direct_chain_type; 11471 break; 11472 case SD_CHAIN_DIRECT_PRIORITY: 11473 index = un->un_priority_chain_type; 11474 break; 11475 default: 11476 /* We're really broken if we ever get here... */ 11477 panic("sd_xbuf_init: illegal chain type!"); 11478 /*NOTREACHED*/ 11479 } 11480 11481 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11482 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11483 11484 /* 11485 * It might be a bit easier to simply bzero the entire xbuf above, 11486 * but it turns out that since we init a fair number of members anyway, 11487 * we save a fair number cycles by doing explicit assignment of zero. 11488 */ 11489 xp->xb_pkt_flags = 0; 11490 xp->xb_dma_resid = 0; 11491 xp->xb_retry_count = 0; 11492 xp->xb_victim_retry_count = 0; 11493 xp->xb_ua_retry_count = 0; 11494 xp->xb_sense_bp = NULL; 11495 xp->xb_sense_status = 0; 11496 xp->xb_sense_state = 0; 11497 xp->xb_sense_resid = 0; 11498 11499 bp->b_private = xp; 11500 bp->b_flags &= ~(B_DONE | B_ERROR); 11501 bp->b_resid = 0; 11502 bp->av_forw = NULL; 11503 bp->av_back = NULL; 11504 bioerror(bp, 0); 11505 11506 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11507 } 11508 11509 11510 /* 11511 * Function: sd_uscsi_strategy 11512 * 11513 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11514 * 11515 * Arguments: bp - buf struct ptr 11516 * 11517 * Return Code: Always returns 0 11518 * 11519 * Context: Kernel thread context 11520 */ 11521 11522 static int 11523 sd_uscsi_strategy(struct buf *bp) 11524 { 11525 struct sd_lun *un; 11526 struct sd_uscsi_info *uip; 11527 struct sd_xbuf *xp; 11528 uchar_t chain_type; 11529 11530 ASSERT(bp != NULL); 11531 11532 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11533 if (un == NULL) { 11534 bioerror(bp, EIO); 11535 bp->b_resid = bp->b_bcount; 11536 biodone(bp); 11537 return (0); 11538 } 11539 11540 ASSERT(!mutex_owned(SD_MUTEX(un))); 11541 11542 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11543 11544 mutex_enter(SD_MUTEX(un)); 11545 /* 11546 * atapi: Since we are running the CD for now in PIO mode we need to 11547 * call bp_mapin here to avoid bp_mapin called interrupt context under 11548 * the HBA's init_pkt routine. 11549 */ 11550 if (un->un_f_cfg_is_atapi == TRUE) { 11551 mutex_exit(SD_MUTEX(un)); 11552 bp_mapin(bp); 11553 mutex_enter(SD_MUTEX(un)); 11554 } 11555 un->un_ncmds_in_driver++; 11556 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11557 un->un_ncmds_in_driver); 11558 mutex_exit(SD_MUTEX(un)); 11559 11560 /* 11561 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11562 */ 11563 ASSERT(bp->b_private != NULL); 11564 uip = (struct sd_uscsi_info *)bp->b_private; 11565 11566 switch (uip->ui_flags) { 11567 case SD_PATH_DIRECT: 11568 chain_type = SD_CHAIN_DIRECT; 11569 break; 11570 case SD_PATH_DIRECT_PRIORITY: 11571 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11572 break; 11573 default: 11574 chain_type = SD_CHAIN_USCSI; 11575 break; 11576 } 11577 11578 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11579 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11580 11581 /* Use the index obtained within xbuf_init */ 11582 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11583 11584 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11585 11586 return (0); 11587 } 11588 11589 11590 /* 11591 * These routines perform raw i/o operations. 11592 */ 11593 /*ARGSUSED*/ 11594 static void 11595 sduscsimin(struct buf *bp) 11596 { 11597 /* 11598 * do not break up because the CDB count would then 11599 * be incorrect and data underruns would result (incomplete 11600 * read/writes which would be retried and then failed, see 11601 * sdintr(). 11602 */ 11603 } 11604 11605 11606 11607 /* 11608 * Function: sd_send_scsi_cmd 11609 * 11610 * Description: Runs a USCSI command for user (when called thru sdioctl), 11611 * or for the driver 11612 * 11613 * Arguments: dev - the dev_t for the device 11614 * incmd - ptr to a valid uscsi_cmd struct 11615 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11616 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11617 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11618 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11619 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11620 * to use the USCSI "direct" chain and bypass the normal 11621 * command waitq. 11622 * 11623 * Return Code: 0 - successful completion of the given command 11624 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11625 * ENXIO - soft state not found for specified dev 11626 * EINVAL 11627 * EFAULT - copyin/copyout error 11628 * return code of biowait(9F) or physio(9F): 11629 * EIO - IO error, caller may check incmd->uscsi_status 11630 * ENXIO 11631 * EACCES - reservation conflict 11632 * 11633 * Context: Waits for command to complete. Can sleep. 11634 */ 11635 11636 static int 11637 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11638 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11639 int path_flag) 11640 { 11641 struct sd_uscsi_info *uip; 11642 struct uscsi_cmd *uscmd; 11643 struct sd_lun *un; 11644 struct buf *bp; 11645 int rval; 11646 int flags; 11647 11648 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11649 if (un == NULL) { 11650 return (ENXIO); 11651 } 11652 11653 ASSERT(!mutex_owned(SD_MUTEX(un))); 11654 11655 #ifdef SDDEBUG 11656 switch (dataspace) { 11657 case UIO_USERSPACE: 11658 SD_TRACE(SD_LOG_IO, un, 11659 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11660 break; 11661 case UIO_SYSSPACE: 11662 SD_TRACE(SD_LOG_IO, un, 11663 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11664 break; 11665 default: 11666 SD_TRACE(SD_LOG_IO, un, 11667 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11668 break; 11669 } 11670 #endif 11671 11672 /* 11673 * Perform resets directly; no need to generate a command to do it. 11674 */ 11675 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11676 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11677 RESET_ALL : RESET_TARGET; 11678 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11679 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11680 /* Reset attempt was unsuccessful */ 11681 SD_TRACE(SD_LOG_IO, un, 11682 "sd_send_scsi_cmd: reset: failure\n"); 11683 return (EIO); 11684 } 11685 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11686 return (0); 11687 } 11688 11689 /* Perfunctory sanity check... */ 11690 if (incmd->uscsi_cdblen <= 0) { 11691 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11692 "invalid uscsi_cdblen, returning EINVAL\n"); 11693 return (EINVAL); 11694 } 11695 11696 /* 11697 * In order to not worry about where the uscsi structure came from 11698 * (or where the cdb it points to came from) we're going to make 11699 * kmem_alloc'd copies of them here. This will also allow reference 11700 * to the data they contain long after this process has gone to 11701 * sleep and its kernel stack has been unmapped, etc. 11702 * 11703 * First get some memory for the uscsi_cmd struct and copy the 11704 * contents of the given uscsi_cmd struct into it. 11705 */ 11706 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11707 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11708 11709 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11710 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11711 11712 /* 11713 * Now get some space for the CDB, and copy the given CDB into 11714 * it. Use ddi_copyin() in case the data is in user space. 11715 */ 11716 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11717 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11718 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11719 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11720 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11721 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11722 return (EFAULT); 11723 } 11724 11725 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11726 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11727 11728 bp = getrbuf(KM_SLEEP); 11729 11730 /* 11731 * Allocate an sd_uscsi_info struct and fill it with the info 11732 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11733 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11734 * since we allocate the buf here in this function, we do not 11735 * need to preserve the prior contents of b_private. 11736 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11737 */ 11738 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11739 uip->ui_flags = path_flag; 11740 uip->ui_cmdp = uscmd; 11741 bp->b_private = uip; 11742 11743 /* 11744 * Initialize Request Sense buffering, if requested. 11745 */ 11746 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11747 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11748 /* 11749 * Here uscmd->uscsi_rqbuf currently points to the caller's 11750 * buffer, but we replace this with a kernel buffer that 11751 * we allocate to use with the sense data. The sense data 11752 * (if present) gets copied into this new buffer before the 11753 * command is completed. Then we copy the sense data from 11754 * our allocated buf into the caller's buffer below. Note 11755 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11756 * below to perform the copy back to the caller's buf. 11757 */ 11758 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11759 if (rqbufspace == UIO_USERSPACE) { 11760 uscmd->uscsi_rqlen = SENSE_LENGTH; 11761 uscmd->uscsi_rqresid = SENSE_LENGTH; 11762 } else { 11763 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11764 uscmd->uscsi_rqlen = rlen; 11765 uscmd->uscsi_rqresid = rlen; 11766 } 11767 } else { 11768 uscmd->uscsi_rqbuf = NULL; 11769 uscmd->uscsi_rqlen = 0; 11770 uscmd->uscsi_rqresid = 0; 11771 } 11772 11773 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11774 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11775 11776 if (un->un_f_is_fibre == FALSE) { 11777 /* 11778 * Force asynchronous mode, if necessary. Doing this here 11779 * has the unfortunate effect of running other queued 11780 * commands async also, but since the main purpose of this 11781 * capability is downloading new drive firmware, we can 11782 * probably live with it. 11783 */ 11784 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11785 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11786 == 1) { 11787 if (scsi_ifsetcap(SD_ADDRESS(un), 11788 "synchronous", 0, 1) == 1) { 11789 SD_TRACE(SD_LOG_IO, un, 11790 "sd_send_scsi_cmd: forced async ok\n"); 11791 } else { 11792 SD_TRACE(SD_LOG_IO, un, 11793 "sd_send_scsi_cmd:\ 11794 forced async failed\n"); 11795 rval = EINVAL; 11796 goto done; 11797 } 11798 } 11799 } 11800 11801 /* 11802 * Re-enable synchronous mode, if requested 11803 */ 11804 if (uscmd->uscsi_flags & USCSI_SYNC) { 11805 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11806 == 0) { 11807 int i = scsi_ifsetcap(SD_ADDRESS(un), 11808 "synchronous", 1, 1); 11809 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11810 "re-enabled sync %s\n", 11811 (i == 1) ? "ok" : "failed"); 11812 } 11813 } 11814 } 11815 11816 /* 11817 * Commands sent with priority are intended for error recovery 11818 * situations, and do not have retries performed. 11819 */ 11820 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11821 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11822 } 11823 11824 /* 11825 * If we're going to do actual I/O, let physio do all the right things 11826 */ 11827 if (uscmd->uscsi_buflen != 0) { 11828 struct iovec aiov; 11829 struct uio auio; 11830 struct uio *uio = &auio; 11831 11832 bzero(&auio, sizeof (struct uio)); 11833 bzero(&aiov, sizeof (struct iovec)); 11834 aiov.iov_base = uscmd->uscsi_bufaddr; 11835 aiov.iov_len = uscmd->uscsi_buflen; 11836 uio->uio_iov = &aiov; 11837 11838 uio->uio_iovcnt = 1; 11839 uio->uio_resid = uscmd->uscsi_buflen; 11840 uio->uio_segflg = dataspace; 11841 11842 /* 11843 * physio() will block here until the command completes.... 11844 */ 11845 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 11846 11847 rval = physio(sd_uscsi_strategy, bp, dev, 11848 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 11849 sduscsimin, uio); 11850 11851 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11852 "returned from physio with 0x%x\n", rval); 11853 11854 } else { 11855 /* 11856 * We have to mimic what physio would do here! Argh! 11857 */ 11858 bp->b_flags = B_BUSY | 11859 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 11860 bp->b_edev = dev; 11861 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 11862 bp->b_bcount = 0; 11863 bp->b_blkno = 0; 11864 11865 SD_TRACE(SD_LOG_IO, un, 11866 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 11867 11868 (void) sd_uscsi_strategy(bp); 11869 11870 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 11871 11872 rval = biowait(bp); 11873 11874 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11875 "returned from biowait with 0x%x\n", rval); 11876 } 11877 11878 done: 11879 11880 #ifdef SDDEBUG 11881 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11882 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11883 uscmd->uscsi_status, uscmd->uscsi_resid); 11884 if (uscmd->uscsi_bufaddr != NULL) { 11885 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11886 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11887 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11888 if (dataspace == UIO_SYSSPACE) { 11889 SD_DUMP_MEMORY(un, SD_LOG_IO, 11890 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11891 uscmd->uscsi_buflen, SD_LOG_HEX); 11892 } 11893 } 11894 #endif 11895 11896 /* 11897 * Get the status and residual to return to the caller. 11898 */ 11899 incmd->uscsi_status = uscmd->uscsi_status; 11900 incmd->uscsi_resid = uscmd->uscsi_resid; 11901 11902 /* 11903 * If the caller wants sense data, copy back whatever sense data 11904 * we may have gotten, and update the relevant rqsense info. 11905 */ 11906 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11907 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11908 11909 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 11910 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 11911 11912 /* Update the Request Sense status and resid */ 11913 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 11914 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 11915 11916 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11917 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 11918 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 11919 11920 /* Copy out the sense data for user processes */ 11921 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 11922 int flags = 11923 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 11924 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 11925 rqlen, flags) != 0) { 11926 rval = EFAULT; 11927 } 11928 /* 11929 * Note: Can't touch incmd->uscsi_rqbuf so use 11930 * uscmd->uscsi_rqbuf instead. They're the same. 11931 */ 11932 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11933 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 11934 incmd->uscsi_rqbuf, rqlen); 11935 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 11936 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 11937 } 11938 } 11939 11940 /* 11941 * Free allocated resources and return; mapout the buf in case it was 11942 * mapped in by a lower layer. 11943 */ 11944 bp_mapout(bp); 11945 freerbuf(bp); 11946 kmem_free(uip, sizeof (struct sd_uscsi_info)); 11947 if (uscmd->uscsi_rqbuf != NULL) { 11948 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 11949 } 11950 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 11951 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11952 11953 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 11954 11955 return (rval); 11956 } 11957 11958 11959 /* 11960 * Function: sd_buf_iodone 11961 * 11962 * Description: Frees the sd_xbuf & returns the buf to its originator. 11963 * 11964 * Context: May be called from interrupt context. 11965 */ 11966 /* ARGSUSED */ 11967 static void 11968 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 11969 { 11970 struct sd_xbuf *xp; 11971 11972 ASSERT(un != NULL); 11973 ASSERT(bp != NULL); 11974 ASSERT(!mutex_owned(SD_MUTEX(un))); 11975 11976 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 11977 11978 xp = SD_GET_XBUF(bp); 11979 ASSERT(xp != NULL); 11980 11981 mutex_enter(SD_MUTEX(un)); 11982 11983 /* 11984 * Grab time when the cmd completed. 11985 * This is used for determining if the system has been 11986 * idle long enough to make it idle to the PM framework. 11987 * This is for lowering the overhead, and therefore improving 11988 * performance per I/O operation. 11989 */ 11990 un->un_pm_idle_time = ddi_get_time(); 11991 11992 un->un_ncmds_in_driver--; 11993 ASSERT(un->un_ncmds_in_driver >= 0); 11994 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 11995 un->un_ncmds_in_driver); 11996 11997 mutex_exit(SD_MUTEX(un)); 11998 11999 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12000 biodone(bp); /* bp is gone after this */ 12001 12002 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12003 } 12004 12005 12006 /* 12007 * Function: sd_uscsi_iodone 12008 * 12009 * Description: Frees the sd_xbuf & returns the buf to its originator. 12010 * 12011 * Context: May be called from interrupt context. 12012 */ 12013 /* ARGSUSED */ 12014 static void 12015 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12016 { 12017 struct sd_xbuf *xp; 12018 12019 ASSERT(un != NULL); 12020 ASSERT(bp != NULL); 12021 12022 xp = SD_GET_XBUF(bp); 12023 ASSERT(xp != NULL); 12024 ASSERT(!mutex_owned(SD_MUTEX(un))); 12025 12026 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12027 12028 bp->b_private = xp->xb_private; 12029 12030 mutex_enter(SD_MUTEX(un)); 12031 12032 /* 12033 * Grab time when the cmd completed. 12034 * This is used for determining if the system has been 12035 * idle long enough to make it idle to the PM framework. 12036 * This is for lowering the overhead, and therefore improving 12037 * performance per I/O operation. 12038 */ 12039 un->un_pm_idle_time = ddi_get_time(); 12040 12041 un->un_ncmds_in_driver--; 12042 ASSERT(un->un_ncmds_in_driver >= 0); 12043 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12044 un->un_ncmds_in_driver); 12045 12046 mutex_exit(SD_MUTEX(un)); 12047 12048 kmem_free(xp, sizeof (struct sd_xbuf)); 12049 biodone(bp); 12050 12051 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12052 } 12053 12054 12055 /* 12056 * Function: sd_mapblockaddr_iostart 12057 * 12058 * Description: Verify request lies withing the partition limits for 12059 * the indicated minor device. Issue "overrun" buf if 12060 * request would exceed partition range. Converts 12061 * partition-relative block address to absolute. 12062 * 12063 * Context: Can sleep 12064 * 12065 * Issues: This follows what the old code did, in terms of accessing 12066 * some of the partition info in the unit struct without holding 12067 * the mutext. This is a general issue, if the partition info 12068 * can be altered while IO is in progress... as soon as we send 12069 * a buf, its partitioning can be invalid before it gets to the 12070 * device. Probably the right fix is to move partitioning out 12071 * of the driver entirely. 12072 */ 12073 12074 static void 12075 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12076 { 12077 daddr_t nblocks; /* #blocks in the given partition */ 12078 daddr_t blocknum; /* Block number specified by the buf */ 12079 size_t requested_nblocks; 12080 size_t available_nblocks; 12081 int partition; 12082 diskaddr_t partition_offset; 12083 struct sd_xbuf *xp; 12084 12085 12086 ASSERT(un != NULL); 12087 ASSERT(bp != NULL); 12088 ASSERT(!mutex_owned(SD_MUTEX(un))); 12089 12090 SD_TRACE(SD_LOG_IO_PARTITION, un, 12091 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12092 12093 xp = SD_GET_XBUF(bp); 12094 ASSERT(xp != NULL); 12095 12096 /* 12097 * If the geometry is not indicated as valid, attempt to access 12098 * the unit & verify the geometry/label. This can be the case for 12099 * removable-media devices, of if the device was opened in 12100 * NDELAY/NONBLOCK mode. 12101 */ 12102 if ((un->un_f_geometry_is_valid != TRUE) && 12103 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12104 /* 12105 * For removable devices it is possible to start an I/O 12106 * without a media by opening the device in nodelay mode. 12107 * Also for writable CDs there can be many scenarios where 12108 * there is no geometry yet but volume manager is trying to 12109 * issue a read() just because it can see TOC on the CD. So 12110 * do not print a message for removables. 12111 */ 12112 if (!un->un_f_has_removable_media) { 12113 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12114 "i/o to invalid geometry\n"); 12115 } 12116 bioerror(bp, EIO); 12117 bp->b_resid = bp->b_bcount; 12118 SD_BEGIN_IODONE(index, un, bp); 12119 return; 12120 } 12121 12122 partition = SDPART(bp->b_edev); 12123 12124 /* #blocks in partition */ 12125 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12126 12127 /* Use of a local variable potentially improves performance slightly */ 12128 partition_offset = un->un_offset[partition]; 12129 12130 /* 12131 * blocknum is the starting block number of the request. At this 12132 * point it is still relative to the start of the minor device. 12133 */ 12134 blocknum = xp->xb_blkno; 12135 12136 /* 12137 * Legacy: If the starting block number is one past the last block 12138 * in the partition, do not set B_ERROR in the buf. 12139 */ 12140 if (blocknum == nblocks) { 12141 goto error_exit; 12142 } 12143 12144 /* 12145 * Confirm that the first block of the request lies within the 12146 * partition limits. Also the requested number of bytes must be 12147 * a multiple of the system block size. 12148 */ 12149 if ((blocknum < 0) || (blocknum >= nblocks) || 12150 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12151 bp->b_flags |= B_ERROR; 12152 goto error_exit; 12153 } 12154 12155 /* 12156 * If the requsted # blocks exceeds the available # blocks, that 12157 * is an overrun of the partition. 12158 */ 12159 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12160 available_nblocks = (size_t)(nblocks - blocknum); 12161 ASSERT(nblocks >= blocknum); 12162 12163 if (requested_nblocks > available_nblocks) { 12164 /* 12165 * Allocate an "overrun" buf to allow the request to proceed 12166 * for the amount of space available in the partition. The 12167 * amount not transferred will be added into the b_resid 12168 * when the operation is complete. The overrun buf 12169 * replaces the original buf here, and the original buf 12170 * is saved inside the overrun buf, for later use. 12171 */ 12172 size_t resid = SD_SYSBLOCKS2BYTES(un, 12173 (offset_t)(requested_nblocks - available_nblocks)); 12174 size_t count = bp->b_bcount - resid; 12175 /* 12176 * Note: count is an unsigned entity thus it'll NEVER 12177 * be less than 0 so ASSERT the original values are 12178 * correct. 12179 */ 12180 ASSERT(bp->b_bcount >= resid); 12181 12182 bp = sd_bioclone_alloc(bp, count, blocknum, 12183 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12184 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12185 ASSERT(xp != NULL); 12186 } 12187 12188 /* At this point there should be no residual for this buf. */ 12189 ASSERT(bp->b_resid == 0); 12190 12191 /* Convert the block number to an absolute address. */ 12192 xp->xb_blkno += partition_offset; 12193 12194 SD_NEXT_IOSTART(index, un, bp); 12195 12196 SD_TRACE(SD_LOG_IO_PARTITION, un, 12197 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12198 12199 return; 12200 12201 error_exit: 12202 bp->b_resid = bp->b_bcount; 12203 SD_BEGIN_IODONE(index, un, bp); 12204 SD_TRACE(SD_LOG_IO_PARTITION, un, 12205 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12206 } 12207 12208 12209 /* 12210 * Function: sd_mapblockaddr_iodone 12211 * 12212 * Description: Completion-side processing for partition management. 12213 * 12214 * Context: May be called under interrupt context 12215 */ 12216 12217 static void 12218 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12219 { 12220 /* int partition; */ /* Not used, see below. */ 12221 ASSERT(un != NULL); 12222 ASSERT(bp != NULL); 12223 ASSERT(!mutex_owned(SD_MUTEX(un))); 12224 12225 SD_TRACE(SD_LOG_IO_PARTITION, un, 12226 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12227 12228 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12229 /* 12230 * We have an "overrun" buf to deal with... 12231 */ 12232 struct sd_xbuf *xp; 12233 struct buf *obp; /* ptr to the original buf */ 12234 12235 xp = SD_GET_XBUF(bp); 12236 ASSERT(xp != NULL); 12237 12238 /* Retrieve the pointer to the original buf */ 12239 obp = (struct buf *)xp->xb_private; 12240 ASSERT(obp != NULL); 12241 12242 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12243 bioerror(obp, bp->b_error); 12244 12245 sd_bioclone_free(bp); 12246 12247 /* 12248 * Get back the original buf. 12249 * Note that since the restoration of xb_blkno below 12250 * was removed, the sd_xbuf is not needed. 12251 */ 12252 bp = obp; 12253 /* 12254 * xp = SD_GET_XBUF(bp); 12255 * ASSERT(xp != NULL); 12256 */ 12257 } 12258 12259 /* 12260 * Convert sd->xb_blkno back to a minor-device relative value. 12261 * Note: this has been commented out, as it is not needed in the 12262 * current implementation of the driver (ie, since this function 12263 * is at the top of the layering chains, so the info will be 12264 * discarded) and it is in the "hot" IO path. 12265 * 12266 * partition = getminor(bp->b_edev) & SDPART_MASK; 12267 * xp->xb_blkno -= un->un_offset[partition]; 12268 */ 12269 12270 SD_NEXT_IODONE(index, un, bp); 12271 12272 SD_TRACE(SD_LOG_IO_PARTITION, un, 12273 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12274 } 12275 12276 12277 /* 12278 * Function: sd_mapblocksize_iostart 12279 * 12280 * Description: Convert between system block size (un->un_sys_blocksize) 12281 * and target block size (un->un_tgt_blocksize). 12282 * 12283 * Context: Can sleep to allocate resources. 12284 * 12285 * Assumptions: A higher layer has already performed any partition validation, 12286 * and converted the xp->xb_blkno to an absolute value relative 12287 * to the start of the device. 12288 * 12289 * It is also assumed that the higher layer has implemented 12290 * an "overrun" mechanism for the case where the request would 12291 * read/write beyond the end of a partition. In this case we 12292 * assume (and ASSERT) that bp->b_resid == 0. 12293 * 12294 * Note: The implementation for this routine assumes the target 12295 * block size remains constant between allocation and transport. 12296 */ 12297 12298 static void 12299 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12300 { 12301 struct sd_mapblocksize_info *bsp; 12302 struct sd_xbuf *xp; 12303 offset_t first_byte; 12304 daddr_t start_block, end_block; 12305 daddr_t request_bytes; 12306 ushort_t is_aligned = FALSE; 12307 12308 ASSERT(un != NULL); 12309 ASSERT(bp != NULL); 12310 ASSERT(!mutex_owned(SD_MUTEX(un))); 12311 ASSERT(bp->b_resid == 0); 12312 12313 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12314 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12315 12316 /* 12317 * For a non-writable CD, a write request is an error 12318 */ 12319 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12320 (un->un_f_mmc_writable_media == FALSE)) { 12321 bioerror(bp, EIO); 12322 bp->b_resid = bp->b_bcount; 12323 SD_BEGIN_IODONE(index, un, bp); 12324 return; 12325 } 12326 12327 /* 12328 * We do not need a shadow buf if the device is using 12329 * un->un_sys_blocksize as its block size or if bcount == 0. 12330 * In this case there is no layer-private data block allocated. 12331 */ 12332 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12333 (bp->b_bcount == 0)) { 12334 goto done; 12335 } 12336 12337 #if defined(__i386) || defined(__amd64) 12338 /* We do not support non-block-aligned transfers for ROD devices */ 12339 ASSERT(!ISROD(un)); 12340 #endif 12341 12342 xp = SD_GET_XBUF(bp); 12343 ASSERT(xp != NULL); 12344 12345 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12346 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12347 un->un_tgt_blocksize, un->un_sys_blocksize); 12348 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12349 "request start block:0x%x\n", xp->xb_blkno); 12350 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12351 "request len:0x%x\n", bp->b_bcount); 12352 12353 /* 12354 * Allocate the layer-private data area for the mapblocksize layer. 12355 * Layers are allowed to use the xp_private member of the sd_xbuf 12356 * struct to store the pointer to their layer-private data block, but 12357 * each layer also has the responsibility of restoring the prior 12358 * contents of xb_private before returning the buf/xbuf to the 12359 * higher layer that sent it. 12360 * 12361 * Here we save the prior contents of xp->xb_private into the 12362 * bsp->mbs_oprivate field of our layer-private data area. This value 12363 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12364 * the layer-private area and returning the buf/xbuf to the layer 12365 * that sent it. 12366 * 12367 * Note that here we use kmem_zalloc for the allocation as there are 12368 * parts of the mapblocksize code that expect certain fields to be 12369 * zero unless explicitly set to a required value. 12370 */ 12371 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12372 bsp->mbs_oprivate = xp->xb_private; 12373 xp->xb_private = bsp; 12374 12375 /* 12376 * This treats the data on the disk (target) as an array of bytes. 12377 * first_byte is the byte offset, from the beginning of the device, 12378 * to the location of the request. This is converted from a 12379 * un->un_sys_blocksize block address to a byte offset, and then back 12380 * to a block address based upon a un->un_tgt_blocksize block size. 12381 * 12382 * xp->xb_blkno should be absolute upon entry into this function, 12383 * but, but it is based upon partitions that use the "system" 12384 * block size. It must be adjusted to reflect the block size of 12385 * the target. 12386 * 12387 * Note that end_block is actually the block that follows the last 12388 * block of the request, but that's what is needed for the computation. 12389 */ 12390 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12391 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12392 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12393 un->un_tgt_blocksize; 12394 12395 /* request_bytes is rounded up to a multiple of the target block size */ 12396 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12397 12398 /* 12399 * See if the starting address of the request and the request 12400 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12401 * then we do not need to allocate a shadow buf to handle the request. 12402 */ 12403 if (((first_byte % un->un_tgt_blocksize) == 0) && 12404 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12405 is_aligned = TRUE; 12406 } 12407 12408 if ((bp->b_flags & B_READ) == 0) { 12409 /* 12410 * Lock the range for a write operation. An aligned request is 12411 * considered a simple write; otherwise the request must be a 12412 * read-modify-write. 12413 */ 12414 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12415 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12416 } 12417 12418 /* 12419 * Alloc a shadow buf if the request is not aligned. Also, this is 12420 * where the READ command is generated for a read-modify-write. (The 12421 * write phase is deferred until after the read completes.) 12422 */ 12423 if (is_aligned == FALSE) { 12424 12425 struct sd_mapblocksize_info *shadow_bsp; 12426 struct sd_xbuf *shadow_xp; 12427 struct buf *shadow_bp; 12428 12429 /* 12430 * Allocate the shadow buf and it associated xbuf. Note that 12431 * after this call the xb_blkno value in both the original 12432 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12433 * same: absolute relative to the start of the device, and 12434 * adjusted for the target block size. The b_blkno in the 12435 * shadow buf will also be set to this value. We should never 12436 * change b_blkno in the original bp however. 12437 * 12438 * Note also that the shadow buf will always need to be a 12439 * READ command, regardless of whether the incoming command 12440 * is a READ or a WRITE. 12441 */ 12442 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12443 xp->xb_blkno, 12444 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12445 12446 shadow_xp = SD_GET_XBUF(shadow_bp); 12447 12448 /* 12449 * Allocate the layer-private data for the shadow buf. 12450 * (No need to preserve xb_private in the shadow xbuf.) 12451 */ 12452 shadow_xp->xb_private = shadow_bsp = 12453 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12454 12455 /* 12456 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12457 * to figure out where the start of the user data is (based upon 12458 * the system block size) in the data returned by the READ 12459 * command (which will be based upon the target blocksize). Note 12460 * that this is only really used if the request is unaligned. 12461 */ 12462 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12463 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12464 ASSERT((bsp->mbs_copy_offset >= 0) && 12465 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12466 12467 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12468 12469 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12470 12471 /* Transfer the wmap (if any) to the shadow buf */ 12472 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12473 bsp->mbs_wmp = NULL; 12474 12475 /* 12476 * The shadow buf goes on from here in place of the 12477 * original buf. 12478 */ 12479 shadow_bsp->mbs_orig_bp = bp; 12480 bp = shadow_bp; 12481 } 12482 12483 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12484 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12485 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12486 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12487 request_bytes); 12488 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12489 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12490 12491 done: 12492 SD_NEXT_IOSTART(index, un, bp); 12493 12494 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12495 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12496 } 12497 12498 12499 /* 12500 * Function: sd_mapblocksize_iodone 12501 * 12502 * Description: Completion side processing for block-size mapping. 12503 * 12504 * Context: May be called under interrupt context 12505 */ 12506 12507 static void 12508 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12509 { 12510 struct sd_mapblocksize_info *bsp; 12511 struct sd_xbuf *xp; 12512 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12513 struct buf *orig_bp; /* ptr to the original buf */ 12514 offset_t shadow_end; 12515 offset_t request_end; 12516 offset_t shadow_start; 12517 ssize_t copy_offset; 12518 size_t copy_length; 12519 size_t shortfall; 12520 uint_t is_write; /* TRUE if this bp is a WRITE */ 12521 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12522 12523 ASSERT(un != NULL); 12524 ASSERT(bp != NULL); 12525 12526 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12527 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12528 12529 /* 12530 * There is no shadow buf or layer-private data if the target is 12531 * using un->un_sys_blocksize as its block size or if bcount == 0. 12532 */ 12533 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12534 (bp->b_bcount == 0)) { 12535 goto exit; 12536 } 12537 12538 xp = SD_GET_XBUF(bp); 12539 ASSERT(xp != NULL); 12540 12541 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12542 bsp = xp->xb_private; 12543 12544 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12545 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12546 12547 if (is_write) { 12548 /* 12549 * For a WRITE request we must free up the block range that 12550 * we have locked up. This holds regardless of whether this is 12551 * an aligned write request or a read-modify-write request. 12552 */ 12553 sd_range_unlock(un, bsp->mbs_wmp); 12554 bsp->mbs_wmp = NULL; 12555 } 12556 12557 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12558 /* 12559 * An aligned read or write command will have no shadow buf; 12560 * there is not much else to do with it. 12561 */ 12562 goto done; 12563 } 12564 12565 orig_bp = bsp->mbs_orig_bp; 12566 ASSERT(orig_bp != NULL); 12567 orig_xp = SD_GET_XBUF(orig_bp); 12568 ASSERT(orig_xp != NULL); 12569 ASSERT(!mutex_owned(SD_MUTEX(un))); 12570 12571 if (!is_write && has_wmap) { 12572 /* 12573 * A READ with a wmap means this is the READ phase of a 12574 * read-modify-write. If an error occurred on the READ then 12575 * we do not proceed with the WRITE phase or copy any data. 12576 * Just release the write maps and return with an error. 12577 */ 12578 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12579 orig_bp->b_resid = orig_bp->b_bcount; 12580 bioerror(orig_bp, bp->b_error); 12581 sd_range_unlock(un, bsp->mbs_wmp); 12582 goto freebuf_done; 12583 } 12584 } 12585 12586 /* 12587 * Here is where we set up to copy the data from the shadow buf 12588 * into the space associated with the original buf. 12589 * 12590 * To deal with the conversion between block sizes, these 12591 * computations treat the data as an array of bytes, with the 12592 * first byte (byte 0) corresponding to the first byte in the 12593 * first block on the disk. 12594 */ 12595 12596 /* 12597 * shadow_start and shadow_len indicate the location and size of 12598 * the data returned with the shadow IO request. 12599 */ 12600 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12601 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12602 12603 /* 12604 * copy_offset gives the offset (in bytes) from the start of the first 12605 * block of the READ request to the beginning of the data. We retrieve 12606 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12607 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12608 * data to be copied (in bytes). 12609 */ 12610 copy_offset = bsp->mbs_copy_offset; 12611 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12612 copy_length = orig_bp->b_bcount; 12613 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12614 12615 /* 12616 * Set up the resid and error fields of orig_bp as appropriate. 12617 */ 12618 if (shadow_end >= request_end) { 12619 /* We got all the requested data; set resid to zero */ 12620 orig_bp->b_resid = 0; 12621 } else { 12622 /* 12623 * We failed to get enough data to fully satisfy the original 12624 * request. Just copy back whatever data we got and set 12625 * up the residual and error code as required. 12626 * 12627 * 'shortfall' is the amount by which the data received with the 12628 * shadow buf has "fallen short" of the requested amount. 12629 */ 12630 shortfall = (size_t)(request_end - shadow_end); 12631 12632 if (shortfall > orig_bp->b_bcount) { 12633 /* 12634 * We did not get enough data to even partially 12635 * fulfill the original request. The residual is 12636 * equal to the amount requested. 12637 */ 12638 orig_bp->b_resid = orig_bp->b_bcount; 12639 } else { 12640 /* 12641 * We did not get all the data that we requested 12642 * from the device, but we will try to return what 12643 * portion we did get. 12644 */ 12645 orig_bp->b_resid = shortfall; 12646 } 12647 ASSERT(copy_length >= orig_bp->b_resid); 12648 copy_length -= orig_bp->b_resid; 12649 } 12650 12651 /* Propagate the error code from the shadow buf to the original buf */ 12652 bioerror(orig_bp, bp->b_error); 12653 12654 if (is_write) { 12655 goto freebuf_done; /* No data copying for a WRITE */ 12656 } 12657 12658 if (has_wmap) { 12659 /* 12660 * This is a READ command from the READ phase of a 12661 * read-modify-write request. We have to copy the data given 12662 * by the user OVER the data returned by the READ command, 12663 * then convert the command from a READ to a WRITE and send 12664 * it back to the target. 12665 */ 12666 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12667 copy_length); 12668 12669 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12670 12671 /* 12672 * Dispatch the WRITE command to the taskq thread, which 12673 * will in turn send the command to the target. When the 12674 * WRITE command completes, we (sd_mapblocksize_iodone()) 12675 * will get called again as part of the iodone chain 12676 * processing for it. Note that we will still be dealing 12677 * with the shadow buf at that point. 12678 */ 12679 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12680 KM_NOSLEEP) != 0) { 12681 /* 12682 * Dispatch was successful so we are done. Return 12683 * without going any higher up the iodone chain. Do 12684 * not free up any layer-private data until after the 12685 * WRITE completes. 12686 */ 12687 return; 12688 } 12689 12690 /* 12691 * Dispatch of the WRITE command failed; set up the error 12692 * condition and send this IO back up the iodone chain. 12693 */ 12694 bioerror(orig_bp, EIO); 12695 orig_bp->b_resid = orig_bp->b_bcount; 12696 12697 } else { 12698 /* 12699 * This is a regular READ request (ie, not a RMW). Copy the 12700 * data from the shadow buf into the original buf. The 12701 * copy_offset compensates for any "misalignment" between the 12702 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12703 * original buf (with its un->un_sys_blocksize blocks). 12704 */ 12705 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12706 copy_length); 12707 } 12708 12709 freebuf_done: 12710 12711 /* 12712 * At this point we still have both the shadow buf AND the original 12713 * buf to deal with, as well as the layer-private data area in each. 12714 * Local variables are as follows: 12715 * 12716 * bp -- points to shadow buf 12717 * xp -- points to xbuf of shadow buf 12718 * bsp -- points to layer-private data area of shadow buf 12719 * orig_bp -- points to original buf 12720 * 12721 * First free the shadow buf and its associated xbuf, then free the 12722 * layer-private data area from the shadow buf. There is no need to 12723 * restore xb_private in the shadow xbuf. 12724 */ 12725 sd_shadow_buf_free(bp); 12726 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12727 12728 /* 12729 * Now update the local variables to point to the original buf, xbuf, 12730 * and layer-private area. 12731 */ 12732 bp = orig_bp; 12733 xp = SD_GET_XBUF(bp); 12734 ASSERT(xp != NULL); 12735 ASSERT(xp == orig_xp); 12736 bsp = xp->xb_private; 12737 ASSERT(bsp != NULL); 12738 12739 done: 12740 /* 12741 * Restore xb_private to whatever it was set to by the next higher 12742 * layer in the chain, then free the layer-private data area. 12743 */ 12744 xp->xb_private = bsp->mbs_oprivate; 12745 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12746 12747 exit: 12748 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12749 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12750 12751 SD_NEXT_IODONE(index, un, bp); 12752 } 12753 12754 12755 /* 12756 * Function: sd_checksum_iostart 12757 * 12758 * Description: A stub function for a layer that's currently not used. 12759 * For now just a placeholder. 12760 * 12761 * Context: Kernel thread context 12762 */ 12763 12764 static void 12765 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12766 { 12767 ASSERT(un != NULL); 12768 ASSERT(bp != NULL); 12769 ASSERT(!mutex_owned(SD_MUTEX(un))); 12770 SD_NEXT_IOSTART(index, un, bp); 12771 } 12772 12773 12774 /* 12775 * Function: sd_checksum_iodone 12776 * 12777 * Description: A stub function for a layer that's currently not used. 12778 * For now just a placeholder. 12779 * 12780 * Context: May be called under interrupt context 12781 */ 12782 12783 static void 12784 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12785 { 12786 ASSERT(un != NULL); 12787 ASSERT(bp != NULL); 12788 ASSERT(!mutex_owned(SD_MUTEX(un))); 12789 SD_NEXT_IODONE(index, un, bp); 12790 } 12791 12792 12793 /* 12794 * Function: sd_checksum_uscsi_iostart 12795 * 12796 * Description: A stub function for a layer that's currently not used. 12797 * For now just a placeholder. 12798 * 12799 * Context: Kernel thread context 12800 */ 12801 12802 static void 12803 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12804 { 12805 ASSERT(un != NULL); 12806 ASSERT(bp != NULL); 12807 ASSERT(!mutex_owned(SD_MUTEX(un))); 12808 SD_NEXT_IOSTART(index, un, bp); 12809 } 12810 12811 12812 /* 12813 * Function: sd_checksum_uscsi_iodone 12814 * 12815 * Description: A stub function for a layer that's currently not used. 12816 * For now just a placeholder. 12817 * 12818 * Context: May be called under interrupt context 12819 */ 12820 12821 static void 12822 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12823 { 12824 ASSERT(un != NULL); 12825 ASSERT(bp != NULL); 12826 ASSERT(!mutex_owned(SD_MUTEX(un))); 12827 SD_NEXT_IODONE(index, un, bp); 12828 } 12829 12830 12831 /* 12832 * Function: sd_pm_iostart 12833 * 12834 * Description: iostart-side routine for Power mangement. 12835 * 12836 * Context: Kernel thread context 12837 */ 12838 12839 static void 12840 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12841 { 12842 ASSERT(un != NULL); 12843 ASSERT(bp != NULL); 12844 ASSERT(!mutex_owned(SD_MUTEX(un))); 12845 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12846 12847 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12848 12849 if (sd_pm_entry(un) != DDI_SUCCESS) { 12850 /* 12851 * Set up to return the failed buf back up the 'iodone' 12852 * side of the calling chain. 12853 */ 12854 bioerror(bp, EIO); 12855 bp->b_resid = bp->b_bcount; 12856 12857 SD_BEGIN_IODONE(index, un, bp); 12858 12859 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12860 return; 12861 } 12862 12863 SD_NEXT_IOSTART(index, un, bp); 12864 12865 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12866 } 12867 12868 12869 /* 12870 * Function: sd_pm_iodone 12871 * 12872 * Description: iodone-side routine for power mangement. 12873 * 12874 * Context: may be called from interrupt context 12875 */ 12876 12877 static void 12878 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12879 { 12880 ASSERT(un != NULL); 12881 ASSERT(bp != NULL); 12882 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12883 12884 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12885 12886 /* 12887 * After attach the following flag is only read, so don't 12888 * take the penalty of acquiring a mutex for it. 12889 */ 12890 if (un->un_f_pm_is_enabled == TRUE) { 12891 sd_pm_exit(un); 12892 } 12893 12894 SD_NEXT_IODONE(index, un, bp); 12895 12896 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12897 } 12898 12899 12900 /* 12901 * Function: sd_core_iostart 12902 * 12903 * Description: Primary driver function for enqueuing buf(9S) structs from 12904 * the system and initiating IO to the target device 12905 * 12906 * Context: Kernel thread context. Can sleep. 12907 * 12908 * Assumptions: - The given xp->xb_blkno is absolute 12909 * (ie, relative to the start of the device). 12910 * - The IO is to be done using the native blocksize of 12911 * the device, as specified in un->un_tgt_blocksize. 12912 */ 12913 /* ARGSUSED */ 12914 static void 12915 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12916 { 12917 struct sd_xbuf *xp; 12918 12919 ASSERT(un != NULL); 12920 ASSERT(bp != NULL); 12921 ASSERT(!mutex_owned(SD_MUTEX(un))); 12922 ASSERT(bp->b_resid == 0); 12923 12924 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12925 12926 xp = SD_GET_XBUF(bp); 12927 ASSERT(xp != NULL); 12928 12929 mutex_enter(SD_MUTEX(un)); 12930 12931 /* 12932 * If we are currently in the failfast state, fail any new IO 12933 * that has B_FAILFAST set, then return. 12934 */ 12935 if ((bp->b_flags & B_FAILFAST) && 12936 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12937 mutex_exit(SD_MUTEX(un)); 12938 bioerror(bp, EIO); 12939 bp->b_resid = bp->b_bcount; 12940 SD_BEGIN_IODONE(index, un, bp); 12941 return; 12942 } 12943 12944 if (SD_IS_DIRECT_PRIORITY(xp)) { 12945 /* 12946 * Priority command -- transport it immediately. 12947 * 12948 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12949 * because all direct priority commands should be associated 12950 * with error recovery actions which we don't want to retry. 12951 */ 12952 sd_start_cmds(un, bp); 12953 } else { 12954 /* 12955 * Normal command -- add it to the wait queue, then start 12956 * transporting commands from the wait queue. 12957 */ 12958 sd_add_buf_to_waitq(un, bp); 12959 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12960 sd_start_cmds(un, NULL); 12961 } 12962 12963 mutex_exit(SD_MUTEX(un)); 12964 12965 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 12966 } 12967 12968 12969 /* 12970 * Function: sd_init_cdb_limits 12971 * 12972 * Description: This is to handle scsi_pkt initialization differences 12973 * between the driver platforms. 12974 * 12975 * Legacy behaviors: 12976 * 12977 * If the block number or the sector count exceeds the 12978 * capabilities of a Group 0 command, shift over to a 12979 * Group 1 command. We don't blindly use Group 1 12980 * commands because a) some drives (CDC Wren IVs) get a 12981 * bit confused, and b) there is probably a fair amount 12982 * of speed difference for a target to receive and decode 12983 * a 10 byte command instead of a 6 byte command. 12984 * 12985 * The xfer time difference of 6 vs 10 byte CDBs is 12986 * still significant so this code is still worthwhile. 12987 * 10 byte CDBs are very inefficient with the fas HBA driver 12988 * and older disks. Each CDB byte took 1 usec with some 12989 * popular disks. 12990 * 12991 * Context: Must be called at attach time 12992 */ 12993 12994 static void 12995 sd_init_cdb_limits(struct sd_lun *un) 12996 { 12997 /* 12998 * Use CDB_GROUP1 commands for most devices except for 12999 * parallel SCSI fixed drives in which case we get better 13000 * performance using CDB_GROUP0 commands (where applicable). 13001 */ 13002 un->un_mincdb = SD_CDB_GROUP1; 13003 #if !defined(__fibre) 13004 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13005 !un->un_f_has_removable_media) { 13006 un->un_mincdb = SD_CDB_GROUP0; 13007 } 13008 #endif 13009 13010 /* 13011 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13012 * commands for fixed disks unless we are building for a 32 bit 13013 * kernel. 13014 */ 13015 #ifdef _LP64 13016 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13017 SD_CDB_GROUP4; 13018 #else 13019 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13020 SD_CDB_GROUP1; 13021 #endif 13022 13023 /* 13024 * x86 systems require the PKT_DMA_PARTIAL flag 13025 */ 13026 #if defined(__x86) 13027 un->un_pkt_flags = PKT_DMA_PARTIAL; 13028 #else 13029 un->un_pkt_flags = 0; 13030 #endif 13031 13032 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13033 ? sizeof (struct scsi_arq_status) : 1); 13034 un->un_cmd_timeout = (ushort_t)sd_io_time; 13035 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13036 } 13037 13038 13039 /* 13040 * Function: sd_initpkt_for_buf 13041 * 13042 * Description: Allocate and initialize for transport a scsi_pkt struct, 13043 * based upon the info specified in the given buf struct. 13044 * 13045 * Assumes the xb_blkno in the request is absolute (ie, 13046 * relative to the start of the device (NOT partition!). 13047 * Also assumes that the request is using the native block 13048 * size of the device (as returned by the READ CAPACITY 13049 * command). 13050 * 13051 * Return Code: SD_PKT_ALLOC_SUCCESS 13052 * SD_PKT_ALLOC_FAILURE 13053 * SD_PKT_ALLOC_FAILURE_NO_DMA 13054 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13055 * 13056 * Context: Kernel thread and may be called from software interrupt context 13057 * as part of a sdrunout callback. This function may not block or 13058 * call routines that block 13059 */ 13060 13061 static int 13062 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13063 { 13064 struct sd_xbuf *xp; 13065 struct scsi_pkt *pktp = NULL; 13066 struct sd_lun *un; 13067 size_t blockcount; 13068 daddr_t startblock; 13069 int rval; 13070 int cmd_flags; 13071 13072 ASSERT(bp != NULL); 13073 ASSERT(pktpp != NULL); 13074 xp = SD_GET_XBUF(bp); 13075 ASSERT(xp != NULL); 13076 un = SD_GET_UN(bp); 13077 ASSERT(un != NULL); 13078 ASSERT(mutex_owned(SD_MUTEX(un))); 13079 ASSERT(bp->b_resid == 0); 13080 13081 SD_TRACE(SD_LOG_IO_CORE, un, 13082 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13083 13084 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13085 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13086 /* 13087 * Already have a scsi_pkt -- just need DMA resources. 13088 * We must recompute the CDB in case the mapping returns 13089 * a nonzero pkt_resid. 13090 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13091 * that is being retried, the unmap/remap of the DMA resouces 13092 * will result in the entire transfer starting over again 13093 * from the very first block. 13094 */ 13095 ASSERT(xp->xb_pktp != NULL); 13096 pktp = xp->xb_pktp; 13097 } else { 13098 pktp = NULL; 13099 } 13100 #endif /* __i386 || __amd64 */ 13101 13102 startblock = xp->xb_blkno; /* Absolute block num. */ 13103 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13104 13105 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13106 13107 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13108 13109 #else 13110 13111 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13112 13113 #endif 13114 13115 /* 13116 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13117 * call scsi_init_pkt, and build the CDB. 13118 */ 13119 rval = sd_setup_rw_pkt(un, &pktp, bp, 13120 cmd_flags, sdrunout, (caddr_t)un, 13121 startblock, blockcount); 13122 13123 if (rval == 0) { 13124 /* 13125 * Success. 13126 * 13127 * If partial DMA is being used and required for this transfer. 13128 * set it up here. 13129 */ 13130 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13131 (pktp->pkt_resid != 0)) { 13132 13133 /* 13134 * Save the CDB length and pkt_resid for the 13135 * next xfer 13136 */ 13137 xp->xb_dma_resid = pktp->pkt_resid; 13138 13139 /* rezero resid */ 13140 pktp->pkt_resid = 0; 13141 13142 } else { 13143 xp->xb_dma_resid = 0; 13144 } 13145 13146 pktp->pkt_flags = un->un_tagflags; 13147 pktp->pkt_time = un->un_cmd_timeout; 13148 pktp->pkt_comp = sdintr; 13149 13150 pktp->pkt_private = bp; 13151 *pktpp = pktp; 13152 13153 SD_TRACE(SD_LOG_IO_CORE, un, 13154 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13155 13156 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13157 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13158 #endif 13159 13160 return (SD_PKT_ALLOC_SUCCESS); 13161 13162 } 13163 13164 /* 13165 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13166 * from sd_setup_rw_pkt. 13167 */ 13168 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13169 13170 if (rval == SD_PKT_ALLOC_FAILURE) { 13171 *pktpp = NULL; 13172 /* 13173 * Set the driver state to RWAIT to indicate the driver 13174 * is waiting on resource allocations. The driver will not 13175 * suspend, pm_suspend, or detatch while the state is RWAIT. 13176 */ 13177 New_state(un, SD_STATE_RWAIT); 13178 13179 SD_ERROR(SD_LOG_IO_CORE, un, 13180 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13181 13182 if ((bp->b_flags & B_ERROR) != 0) { 13183 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13184 } 13185 return (SD_PKT_ALLOC_FAILURE); 13186 } else { 13187 /* 13188 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13189 * 13190 * This should never happen. Maybe someone messed with the 13191 * kernel's minphys? 13192 */ 13193 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13194 "Request rejected: too large for CDB: " 13195 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13196 SD_ERROR(SD_LOG_IO_CORE, un, 13197 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13198 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13199 13200 } 13201 } 13202 13203 13204 /* 13205 * Function: sd_destroypkt_for_buf 13206 * 13207 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13208 * 13209 * Context: Kernel thread or interrupt context 13210 */ 13211 13212 static void 13213 sd_destroypkt_for_buf(struct buf *bp) 13214 { 13215 ASSERT(bp != NULL); 13216 ASSERT(SD_GET_UN(bp) != NULL); 13217 13218 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13219 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13220 13221 ASSERT(SD_GET_PKTP(bp) != NULL); 13222 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13223 13224 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13225 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13226 } 13227 13228 /* 13229 * Function: sd_setup_rw_pkt 13230 * 13231 * Description: Determines appropriate CDB group for the requested LBA 13232 * and transfer length, calls scsi_init_pkt, and builds 13233 * the CDB. Do not use for partial DMA transfers except 13234 * for the initial transfer since the CDB size must 13235 * remain constant. 13236 * 13237 * Context: Kernel thread and may be called from software interrupt 13238 * context as part of a sdrunout callback. This function may not 13239 * block or call routines that block 13240 */ 13241 13242 13243 int 13244 sd_setup_rw_pkt(struct sd_lun *un, 13245 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13246 int (*callback)(caddr_t), caddr_t callback_arg, 13247 diskaddr_t lba, uint32_t blockcount) 13248 { 13249 struct scsi_pkt *return_pktp; 13250 union scsi_cdb *cdbp; 13251 struct sd_cdbinfo *cp = NULL; 13252 int i; 13253 13254 /* 13255 * See which size CDB to use, based upon the request. 13256 */ 13257 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13258 13259 /* 13260 * Check lba and block count against sd_cdbtab limits. 13261 * In the partial DMA case, we have to use the same size 13262 * CDB for all the transfers. Check lba + blockcount 13263 * against the max LBA so we know that segment of the 13264 * transfer can use the CDB we select. 13265 */ 13266 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13267 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13268 13269 /* 13270 * The command will fit into the CDB type 13271 * specified by sd_cdbtab[i]. 13272 */ 13273 cp = sd_cdbtab + i; 13274 13275 /* 13276 * Call scsi_init_pkt so we can fill in the 13277 * CDB. 13278 */ 13279 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13280 bp, cp->sc_grpcode, un->un_status_len, 0, 13281 flags, callback, callback_arg); 13282 13283 if (return_pktp != NULL) { 13284 13285 /* 13286 * Return new value of pkt 13287 */ 13288 *pktpp = return_pktp; 13289 13290 /* 13291 * To be safe, zero the CDB insuring there is 13292 * no leftover data from a previous command. 13293 */ 13294 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13295 13296 /* 13297 * Handle partial DMA mapping 13298 */ 13299 if (return_pktp->pkt_resid != 0) { 13300 13301 /* 13302 * Not going to xfer as many blocks as 13303 * originally expected 13304 */ 13305 blockcount -= 13306 SD_BYTES2TGTBLOCKS(un, 13307 return_pktp->pkt_resid); 13308 } 13309 13310 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13311 13312 /* 13313 * Set command byte based on the CDB 13314 * type we matched. 13315 */ 13316 cdbp->scc_cmd = cp->sc_grpmask | 13317 ((bp->b_flags & B_READ) ? 13318 SCMD_READ : SCMD_WRITE); 13319 13320 SD_FILL_SCSI1_LUN(un, return_pktp); 13321 13322 /* 13323 * Fill in LBA and length 13324 */ 13325 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13326 (cp->sc_grpcode == CDB_GROUP4) || 13327 (cp->sc_grpcode == CDB_GROUP0) || 13328 (cp->sc_grpcode == CDB_GROUP5)); 13329 13330 if (cp->sc_grpcode == CDB_GROUP1) { 13331 FORMG1ADDR(cdbp, lba); 13332 FORMG1COUNT(cdbp, blockcount); 13333 return (0); 13334 } else if (cp->sc_grpcode == CDB_GROUP4) { 13335 FORMG4LONGADDR(cdbp, lba); 13336 FORMG4COUNT(cdbp, blockcount); 13337 return (0); 13338 } else if (cp->sc_grpcode == CDB_GROUP0) { 13339 FORMG0ADDR(cdbp, lba); 13340 FORMG0COUNT(cdbp, blockcount); 13341 return (0); 13342 } else if (cp->sc_grpcode == CDB_GROUP5) { 13343 FORMG5ADDR(cdbp, lba); 13344 FORMG5COUNT(cdbp, blockcount); 13345 return (0); 13346 } 13347 13348 /* 13349 * It should be impossible to not match one 13350 * of the CDB types above, so we should never 13351 * reach this point. Set the CDB command byte 13352 * to test-unit-ready to avoid writing 13353 * to somewhere we don't intend. 13354 */ 13355 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13356 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13357 } else { 13358 /* 13359 * Couldn't get scsi_pkt 13360 */ 13361 return (SD_PKT_ALLOC_FAILURE); 13362 } 13363 } 13364 } 13365 13366 /* 13367 * None of the available CDB types were suitable. This really 13368 * should never happen: on a 64 bit system we support 13369 * READ16/WRITE16 which will hold an entire 64 bit disk address 13370 * and on a 32 bit system we will refuse to bind to a device 13371 * larger than 2TB so addresses will never be larger than 32 bits. 13372 */ 13373 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13374 } 13375 13376 #if defined(__i386) || defined(__amd64) 13377 /* 13378 * Function: sd_setup_next_rw_pkt 13379 * 13380 * Description: Setup packet for partial DMA transfers, except for the 13381 * initial transfer. sd_setup_rw_pkt should be used for 13382 * the initial transfer. 13383 * 13384 * Context: Kernel thread and may be called from interrupt context. 13385 */ 13386 13387 int 13388 sd_setup_next_rw_pkt(struct sd_lun *un, 13389 struct scsi_pkt *pktp, struct buf *bp, 13390 diskaddr_t lba, uint32_t blockcount) 13391 { 13392 uchar_t com; 13393 union scsi_cdb *cdbp; 13394 uchar_t cdb_group_id; 13395 13396 ASSERT(pktp != NULL); 13397 ASSERT(pktp->pkt_cdbp != NULL); 13398 13399 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13400 com = cdbp->scc_cmd; 13401 cdb_group_id = CDB_GROUPID(com); 13402 13403 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13404 (cdb_group_id == CDB_GROUPID_1) || 13405 (cdb_group_id == CDB_GROUPID_4) || 13406 (cdb_group_id == CDB_GROUPID_5)); 13407 13408 /* 13409 * Move pkt to the next portion of the xfer. 13410 * func is NULL_FUNC so we do not have to release 13411 * the disk mutex here. 13412 */ 13413 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13414 NULL_FUNC, NULL) == pktp) { 13415 /* Success. Handle partial DMA */ 13416 if (pktp->pkt_resid != 0) { 13417 blockcount -= 13418 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13419 } 13420 13421 cdbp->scc_cmd = com; 13422 SD_FILL_SCSI1_LUN(un, pktp); 13423 if (cdb_group_id == CDB_GROUPID_1) { 13424 FORMG1ADDR(cdbp, lba); 13425 FORMG1COUNT(cdbp, blockcount); 13426 return (0); 13427 } else if (cdb_group_id == CDB_GROUPID_4) { 13428 FORMG4LONGADDR(cdbp, lba); 13429 FORMG4COUNT(cdbp, blockcount); 13430 return (0); 13431 } else if (cdb_group_id == CDB_GROUPID_0) { 13432 FORMG0ADDR(cdbp, lba); 13433 FORMG0COUNT(cdbp, blockcount); 13434 return (0); 13435 } else if (cdb_group_id == CDB_GROUPID_5) { 13436 FORMG5ADDR(cdbp, lba); 13437 FORMG5COUNT(cdbp, blockcount); 13438 return (0); 13439 } 13440 13441 /* Unreachable */ 13442 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13443 } 13444 13445 /* 13446 * Error setting up next portion of cmd transfer. 13447 * Something is definitely very wrong and this 13448 * should not happen. 13449 */ 13450 return (SD_PKT_ALLOC_FAILURE); 13451 } 13452 #endif /* defined(__i386) || defined(__amd64) */ 13453 13454 /* 13455 * Function: sd_initpkt_for_uscsi 13456 * 13457 * Description: Allocate and initialize for transport a scsi_pkt struct, 13458 * based upon the info specified in the given uscsi_cmd struct. 13459 * 13460 * Return Code: SD_PKT_ALLOC_SUCCESS 13461 * SD_PKT_ALLOC_FAILURE 13462 * SD_PKT_ALLOC_FAILURE_NO_DMA 13463 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13464 * 13465 * Context: Kernel thread and may be called from software interrupt context 13466 * as part of a sdrunout callback. This function may not block or 13467 * call routines that block 13468 */ 13469 13470 static int 13471 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13472 { 13473 struct uscsi_cmd *uscmd; 13474 struct sd_xbuf *xp; 13475 struct scsi_pkt *pktp; 13476 struct sd_lun *un; 13477 uint32_t flags = 0; 13478 13479 ASSERT(bp != NULL); 13480 ASSERT(pktpp != NULL); 13481 xp = SD_GET_XBUF(bp); 13482 ASSERT(xp != NULL); 13483 un = SD_GET_UN(bp); 13484 ASSERT(un != NULL); 13485 ASSERT(mutex_owned(SD_MUTEX(un))); 13486 13487 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13488 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13489 ASSERT(uscmd != NULL); 13490 13491 SD_TRACE(SD_LOG_IO_CORE, un, 13492 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13493 13494 /* 13495 * Allocate the scsi_pkt for the command. 13496 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13497 * during scsi_init_pkt time and will continue to use the 13498 * same path as long as the same scsi_pkt is used without 13499 * intervening scsi_dma_free(). Since uscsi command does 13500 * not call scsi_dmafree() before retry failed command, it 13501 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13502 * set such that scsi_vhci can use other available path for 13503 * retry. Besides, ucsci command does not allow DMA breakup, 13504 * so there is no need to set PKT_DMA_PARTIAL flag. 13505 */ 13506 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13507 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13508 sizeof (struct scsi_arq_status), 0, 13509 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13510 sdrunout, (caddr_t)un); 13511 13512 if (pktp == NULL) { 13513 *pktpp = NULL; 13514 /* 13515 * Set the driver state to RWAIT to indicate the driver 13516 * is waiting on resource allocations. The driver will not 13517 * suspend, pm_suspend, or detatch while the state is RWAIT. 13518 */ 13519 New_state(un, SD_STATE_RWAIT); 13520 13521 SD_ERROR(SD_LOG_IO_CORE, un, 13522 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13523 13524 if ((bp->b_flags & B_ERROR) != 0) { 13525 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13526 } 13527 return (SD_PKT_ALLOC_FAILURE); 13528 } 13529 13530 /* 13531 * We do not do DMA breakup for USCSI commands, so return failure 13532 * here if all the needed DMA resources were not allocated. 13533 */ 13534 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13535 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13536 scsi_destroy_pkt(pktp); 13537 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13538 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13539 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13540 } 13541 13542 /* Init the cdb from the given uscsi struct */ 13543 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13544 uscmd->uscsi_cdb[0], 0, 0, 0); 13545 13546 SD_FILL_SCSI1_LUN(un, pktp); 13547 13548 /* 13549 * Set up the optional USCSI flags. See the uscsi (7I) man page 13550 * for listing of the supported flags. 13551 */ 13552 13553 if (uscmd->uscsi_flags & USCSI_SILENT) { 13554 flags |= FLAG_SILENT; 13555 } 13556 13557 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13558 flags |= FLAG_DIAGNOSE; 13559 } 13560 13561 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13562 flags |= FLAG_ISOLATE; 13563 } 13564 13565 if (un->un_f_is_fibre == FALSE) { 13566 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13567 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13568 } 13569 } 13570 13571 /* 13572 * Set the pkt flags here so we save time later. 13573 * Note: These flags are NOT in the uscsi man page!!! 13574 */ 13575 if (uscmd->uscsi_flags & USCSI_HEAD) { 13576 flags |= FLAG_HEAD; 13577 } 13578 13579 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13580 flags |= FLAG_NOINTR; 13581 } 13582 13583 /* 13584 * For tagged queueing, things get a bit complicated. 13585 * Check first for head of queue and last for ordered queue. 13586 * If neither head nor order, use the default driver tag flags. 13587 */ 13588 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13589 if (uscmd->uscsi_flags & USCSI_HTAG) { 13590 flags |= FLAG_HTAG; 13591 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13592 flags |= FLAG_OTAG; 13593 } else { 13594 flags |= un->un_tagflags & FLAG_TAGMASK; 13595 } 13596 } 13597 13598 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13599 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13600 } 13601 13602 pktp->pkt_flags = flags; 13603 13604 /* Copy the caller's CDB into the pkt... */ 13605 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13606 13607 if (uscmd->uscsi_timeout == 0) { 13608 pktp->pkt_time = un->un_uscsi_timeout; 13609 } else { 13610 pktp->pkt_time = uscmd->uscsi_timeout; 13611 } 13612 13613 /* need it later to identify USCSI request in sdintr */ 13614 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13615 13616 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13617 13618 pktp->pkt_private = bp; 13619 pktp->pkt_comp = sdintr; 13620 *pktpp = pktp; 13621 13622 SD_TRACE(SD_LOG_IO_CORE, un, 13623 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13624 13625 return (SD_PKT_ALLOC_SUCCESS); 13626 } 13627 13628 13629 /* 13630 * Function: sd_destroypkt_for_uscsi 13631 * 13632 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13633 * IOs.. Also saves relevant info into the associated uscsi_cmd 13634 * struct. 13635 * 13636 * Context: May be called under interrupt context 13637 */ 13638 13639 static void 13640 sd_destroypkt_for_uscsi(struct buf *bp) 13641 { 13642 struct uscsi_cmd *uscmd; 13643 struct sd_xbuf *xp; 13644 struct scsi_pkt *pktp; 13645 struct sd_lun *un; 13646 13647 ASSERT(bp != NULL); 13648 xp = SD_GET_XBUF(bp); 13649 ASSERT(xp != NULL); 13650 un = SD_GET_UN(bp); 13651 ASSERT(un != NULL); 13652 ASSERT(!mutex_owned(SD_MUTEX(un))); 13653 pktp = SD_GET_PKTP(bp); 13654 ASSERT(pktp != NULL); 13655 13656 SD_TRACE(SD_LOG_IO_CORE, un, 13657 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13658 13659 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13660 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13661 ASSERT(uscmd != NULL); 13662 13663 /* Save the status and the residual into the uscsi_cmd struct */ 13664 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13665 uscmd->uscsi_resid = bp->b_resid; 13666 13667 /* 13668 * If enabled, copy any saved sense data into the area specified 13669 * by the uscsi command. 13670 */ 13671 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13672 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13673 /* 13674 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13675 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13676 */ 13677 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13678 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13679 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13680 } 13681 13682 /* We are done with the scsi_pkt; free it now */ 13683 ASSERT(SD_GET_PKTP(bp) != NULL); 13684 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13685 13686 SD_TRACE(SD_LOG_IO_CORE, un, 13687 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13688 } 13689 13690 13691 /* 13692 * Function: sd_bioclone_alloc 13693 * 13694 * Description: Allocate a buf(9S) and init it as per the given buf 13695 * and the various arguments. The associated sd_xbuf 13696 * struct is (nearly) duplicated. The struct buf *bp 13697 * argument is saved in new_xp->xb_private. 13698 * 13699 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13700 * datalen - size of data area for the shadow bp 13701 * blkno - starting LBA 13702 * func - function pointer for b_iodone in the shadow buf. (May 13703 * be NULL if none.) 13704 * 13705 * Return Code: Pointer to allocates buf(9S) struct 13706 * 13707 * Context: Can sleep. 13708 */ 13709 13710 static struct buf * 13711 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13712 daddr_t blkno, int (*func)(struct buf *)) 13713 { 13714 struct sd_lun *un; 13715 struct sd_xbuf *xp; 13716 struct sd_xbuf *new_xp; 13717 struct buf *new_bp; 13718 13719 ASSERT(bp != NULL); 13720 xp = SD_GET_XBUF(bp); 13721 ASSERT(xp != NULL); 13722 un = SD_GET_UN(bp); 13723 ASSERT(un != NULL); 13724 ASSERT(!mutex_owned(SD_MUTEX(un))); 13725 13726 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13727 NULL, KM_SLEEP); 13728 13729 new_bp->b_lblkno = blkno; 13730 13731 /* 13732 * Allocate an xbuf for the shadow bp and copy the contents of the 13733 * original xbuf into it. 13734 */ 13735 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13736 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13737 13738 /* 13739 * The given bp is automatically saved in the xb_private member 13740 * of the new xbuf. Callers are allowed to depend on this. 13741 */ 13742 new_xp->xb_private = bp; 13743 13744 new_bp->b_private = new_xp; 13745 13746 return (new_bp); 13747 } 13748 13749 /* 13750 * Function: sd_shadow_buf_alloc 13751 * 13752 * Description: Allocate a buf(9S) and init it as per the given buf 13753 * and the various arguments. The associated sd_xbuf 13754 * struct is (nearly) duplicated. The struct buf *bp 13755 * argument is saved in new_xp->xb_private. 13756 * 13757 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13758 * datalen - size of data area for the shadow bp 13759 * bflags - B_READ or B_WRITE (pseudo flag) 13760 * blkno - starting LBA 13761 * func - function pointer for b_iodone in the shadow buf. (May 13762 * be NULL if none.) 13763 * 13764 * Return Code: Pointer to allocates buf(9S) struct 13765 * 13766 * Context: Can sleep. 13767 */ 13768 13769 static struct buf * 13770 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13771 daddr_t blkno, int (*func)(struct buf *)) 13772 { 13773 struct sd_lun *un; 13774 struct sd_xbuf *xp; 13775 struct sd_xbuf *new_xp; 13776 struct buf *new_bp; 13777 13778 ASSERT(bp != NULL); 13779 xp = SD_GET_XBUF(bp); 13780 ASSERT(xp != NULL); 13781 un = SD_GET_UN(bp); 13782 ASSERT(un != NULL); 13783 ASSERT(!mutex_owned(SD_MUTEX(un))); 13784 13785 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13786 bp_mapin(bp); 13787 } 13788 13789 bflags &= (B_READ | B_WRITE); 13790 #if defined(__i386) || defined(__amd64) 13791 new_bp = getrbuf(KM_SLEEP); 13792 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13793 new_bp->b_bcount = datalen; 13794 new_bp->b_flags = bp->b_flags | bflags; 13795 #else 13796 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13797 datalen, bflags, SLEEP_FUNC, NULL); 13798 #endif 13799 new_bp->av_forw = NULL; 13800 new_bp->av_back = NULL; 13801 new_bp->b_dev = bp->b_dev; 13802 new_bp->b_blkno = blkno; 13803 new_bp->b_iodone = func; 13804 new_bp->b_edev = bp->b_edev; 13805 new_bp->b_resid = 0; 13806 13807 /* We need to preserve the B_FAILFAST flag */ 13808 if (bp->b_flags & B_FAILFAST) { 13809 new_bp->b_flags |= B_FAILFAST; 13810 } 13811 13812 /* 13813 * Allocate an xbuf for the shadow bp and copy the contents of the 13814 * original xbuf into it. 13815 */ 13816 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13817 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13818 13819 /* Need later to copy data between the shadow buf & original buf! */ 13820 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13821 13822 /* 13823 * The given bp is automatically saved in the xb_private member 13824 * of the new xbuf. Callers are allowed to depend on this. 13825 */ 13826 new_xp->xb_private = bp; 13827 13828 new_bp->b_private = new_xp; 13829 13830 return (new_bp); 13831 } 13832 13833 /* 13834 * Function: sd_bioclone_free 13835 * 13836 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13837 * in the larger than partition operation. 13838 * 13839 * Context: May be called under interrupt context 13840 */ 13841 13842 static void 13843 sd_bioclone_free(struct buf *bp) 13844 { 13845 struct sd_xbuf *xp; 13846 13847 ASSERT(bp != NULL); 13848 xp = SD_GET_XBUF(bp); 13849 ASSERT(xp != NULL); 13850 13851 /* 13852 * Call bp_mapout() before freeing the buf, in case a lower 13853 * layer or HBA had done a bp_mapin(). we must do this here 13854 * as we are the "originator" of the shadow buf. 13855 */ 13856 bp_mapout(bp); 13857 13858 /* 13859 * Null out b_iodone before freeing the bp, to ensure that the driver 13860 * never gets confused by a stale value in this field. (Just a little 13861 * extra defensiveness here.) 13862 */ 13863 bp->b_iodone = NULL; 13864 13865 freerbuf(bp); 13866 13867 kmem_free(xp, sizeof (struct sd_xbuf)); 13868 } 13869 13870 /* 13871 * Function: sd_shadow_buf_free 13872 * 13873 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13874 * 13875 * Context: May be called under interrupt context 13876 */ 13877 13878 static void 13879 sd_shadow_buf_free(struct buf *bp) 13880 { 13881 struct sd_xbuf *xp; 13882 13883 ASSERT(bp != NULL); 13884 xp = SD_GET_XBUF(bp); 13885 ASSERT(xp != NULL); 13886 13887 #if defined(__sparc) 13888 /* 13889 * Call bp_mapout() before freeing the buf, in case a lower 13890 * layer or HBA had done a bp_mapin(). we must do this here 13891 * as we are the "originator" of the shadow buf. 13892 */ 13893 bp_mapout(bp); 13894 #endif 13895 13896 /* 13897 * Null out b_iodone before freeing the bp, to ensure that the driver 13898 * never gets confused by a stale value in this field. (Just a little 13899 * extra defensiveness here.) 13900 */ 13901 bp->b_iodone = NULL; 13902 13903 #if defined(__i386) || defined(__amd64) 13904 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13905 freerbuf(bp); 13906 #else 13907 scsi_free_consistent_buf(bp); 13908 #endif 13909 13910 kmem_free(xp, sizeof (struct sd_xbuf)); 13911 } 13912 13913 13914 /* 13915 * Function: sd_print_transport_rejected_message 13916 * 13917 * Description: This implements the ludicrously complex rules for printing 13918 * a "transport rejected" message. This is to address the 13919 * specific problem of having a flood of this error message 13920 * produced when a failover occurs. 13921 * 13922 * Context: Any. 13923 */ 13924 13925 static void 13926 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13927 int code) 13928 { 13929 ASSERT(un != NULL); 13930 ASSERT(mutex_owned(SD_MUTEX(un))); 13931 ASSERT(xp != NULL); 13932 13933 /* 13934 * Print the "transport rejected" message under the following 13935 * conditions: 13936 * 13937 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13938 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13939 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 13940 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 13941 * scsi_transport(9F) (which indicates that the target might have 13942 * gone off-line). This uses the un->un_tran_fatal_count 13943 * count, which is incremented whenever a TRAN_FATAL_ERROR is 13944 * received, and reset to zero whenver a TRAN_ACCEPT is returned 13945 * from scsi_transport(). 13946 * 13947 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 13948 * the preceeding cases in order for the message to be printed. 13949 */ 13950 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 13951 if ((sd_level_mask & SD_LOGMASK_DIAG) || 13952 (code != TRAN_FATAL_ERROR) || 13953 (un->un_tran_fatal_count == 1)) { 13954 switch (code) { 13955 case TRAN_BADPKT: 13956 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13957 "transport rejected bad packet\n"); 13958 break; 13959 case TRAN_FATAL_ERROR: 13960 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13961 "transport rejected fatal error\n"); 13962 break; 13963 default: 13964 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13965 "transport rejected (%d)\n", code); 13966 break; 13967 } 13968 } 13969 } 13970 } 13971 13972 13973 /* 13974 * Function: sd_add_buf_to_waitq 13975 * 13976 * Description: Add the given buf(9S) struct to the wait queue for the 13977 * instance. If sorting is enabled, then the buf is added 13978 * to the queue via an elevator sort algorithm (a la 13979 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 13980 * If sorting is not enabled, then the buf is just added 13981 * to the end of the wait queue. 13982 * 13983 * Return Code: void 13984 * 13985 * Context: Does not sleep/block, therefore technically can be called 13986 * from any context. However if sorting is enabled then the 13987 * execution time is indeterminate, and may take long if 13988 * the wait queue grows large. 13989 */ 13990 13991 static void 13992 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 13993 { 13994 struct buf *ap; 13995 13996 ASSERT(bp != NULL); 13997 ASSERT(un != NULL); 13998 ASSERT(mutex_owned(SD_MUTEX(un))); 13999 14000 /* If the queue is empty, add the buf as the only entry & return. */ 14001 if (un->un_waitq_headp == NULL) { 14002 ASSERT(un->un_waitq_tailp == NULL); 14003 un->un_waitq_headp = un->un_waitq_tailp = bp; 14004 bp->av_forw = NULL; 14005 return; 14006 } 14007 14008 ASSERT(un->un_waitq_tailp != NULL); 14009 14010 /* 14011 * If sorting is disabled, just add the buf to the tail end of 14012 * the wait queue and return. 14013 */ 14014 if (un->un_f_disksort_disabled) { 14015 un->un_waitq_tailp->av_forw = bp; 14016 un->un_waitq_tailp = bp; 14017 bp->av_forw = NULL; 14018 return; 14019 } 14020 14021 /* 14022 * Sort thru the list of requests currently on the wait queue 14023 * and add the new buf request at the appropriate position. 14024 * 14025 * The un->un_waitq_headp is an activity chain pointer on which 14026 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14027 * first queue holds those requests which are positioned after 14028 * the current SD_GET_BLKNO() (in the first request); the second holds 14029 * requests which came in after their SD_GET_BLKNO() number was passed. 14030 * Thus we implement a one way scan, retracting after reaching 14031 * the end of the drive to the first request on the second 14032 * queue, at which time it becomes the first queue. 14033 * A one-way scan is natural because of the way UNIX read-ahead 14034 * blocks are allocated. 14035 * 14036 * If we lie after the first request, then we must locate the 14037 * second request list and add ourselves to it. 14038 */ 14039 ap = un->un_waitq_headp; 14040 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14041 while (ap->av_forw != NULL) { 14042 /* 14043 * Look for an "inversion" in the (normally 14044 * ascending) block numbers. This indicates 14045 * the start of the second request list. 14046 */ 14047 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14048 /* 14049 * Search the second request list for the 14050 * first request at a larger block number. 14051 * We go before that; however if there is 14052 * no such request, we go at the end. 14053 */ 14054 do { 14055 if (SD_GET_BLKNO(bp) < 14056 SD_GET_BLKNO(ap->av_forw)) { 14057 goto insert; 14058 } 14059 ap = ap->av_forw; 14060 } while (ap->av_forw != NULL); 14061 goto insert; /* after last */ 14062 } 14063 ap = ap->av_forw; 14064 } 14065 14066 /* 14067 * No inversions... we will go after the last, and 14068 * be the first request in the second request list. 14069 */ 14070 goto insert; 14071 } 14072 14073 /* 14074 * Request is at/after the current request... 14075 * sort in the first request list. 14076 */ 14077 while (ap->av_forw != NULL) { 14078 /* 14079 * We want to go after the current request (1) if 14080 * there is an inversion after it (i.e. it is the end 14081 * of the first request list), or (2) if the next 14082 * request is a larger block no. than our request. 14083 */ 14084 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14085 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14086 goto insert; 14087 } 14088 ap = ap->av_forw; 14089 } 14090 14091 /* 14092 * Neither a second list nor a larger request, therefore 14093 * we go at the end of the first list (which is the same 14094 * as the end of the whole schebang). 14095 */ 14096 insert: 14097 bp->av_forw = ap->av_forw; 14098 ap->av_forw = bp; 14099 14100 /* 14101 * If we inserted onto the tail end of the waitq, make sure the 14102 * tail pointer is updated. 14103 */ 14104 if (ap == un->un_waitq_tailp) { 14105 un->un_waitq_tailp = bp; 14106 } 14107 } 14108 14109 14110 /* 14111 * Function: sd_start_cmds 14112 * 14113 * Description: Remove and transport cmds from the driver queues. 14114 * 14115 * Arguments: un - pointer to the unit (soft state) struct for the target. 14116 * 14117 * immed_bp - ptr to a buf to be transported immediately. Only 14118 * the immed_bp is transported; bufs on the waitq are not 14119 * processed and the un_retry_bp is not checked. If immed_bp is 14120 * NULL, then normal queue processing is performed. 14121 * 14122 * Context: May be called from kernel thread context, interrupt context, 14123 * or runout callback context. This function may not block or 14124 * call routines that block. 14125 */ 14126 14127 static void 14128 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14129 { 14130 struct sd_xbuf *xp; 14131 struct buf *bp; 14132 void (*statp)(kstat_io_t *); 14133 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14134 void (*saved_statp)(kstat_io_t *); 14135 #endif 14136 int rval; 14137 14138 ASSERT(un != NULL); 14139 ASSERT(mutex_owned(SD_MUTEX(un))); 14140 ASSERT(un->un_ncmds_in_transport >= 0); 14141 ASSERT(un->un_throttle >= 0); 14142 14143 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14144 14145 do { 14146 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14147 saved_statp = NULL; 14148 #endif 14149 14150 /* 14151 * If we are syncing or dumping, fail the command to 14152 * avoid recursively calling back into scsi_transport(). 14153 * The dump I/O itself uses a separate code path so this 14154 * only prevents non-dump I/O from being sent while dumping. 14155 * File system sync takes place before dumping begins. 14156 * During panic, filesystem I/O is allowed provided 14157 * un_in_callback is <= 1. This is to prevent recursion 14158 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14159 * sd_start_cmds and so on. See panic.c for more information 14160 * about the states the system can be in during panic. 14161 */ 14162 if ((un->un_state == SD_STATE_DUMPING) || 14163 (ddi_in_panic() && (un->un_in_callback > 1))) { 14164 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14165 "sd_start_cmds: panicking\n"); 14166 goto exit; 14167 } 14168 14169 if ((bp = immed_bp) != NULL) { 14170 /* 14171 * We have a bp that must be transported immediately. 14172 * It's OK to transport the immed_bp here without doing 14173 * the throttle limit check because the immed_bp is 14174 * always used in a retry/recovery case. This means 14175 * that we know we are not at the throttle limit by 14176 * virtue of the fact that to get here we must have 14177 * already gotten a command back via sdintr(). This also 14178 * relies on (1) the command on un_retry_bp preventing 14179 * further commands from the waitq from being issued; 14180 * and (2) the code in sd_retry_command checking the 14181 * throttle limit before issuing a delayed or immediate 14182 * retry. This holds even if the throttle limit is 14183 * currently ratcheted down from its maximum value. 14184 */ 14185 statp = kstat_runq_enter; 14186 if (bp == un->un_retry_bp) { 14187 ASSERT((un->un_retry_statp == NULL) || 14188 (un->un_retry_statp == kstat_waitq_enter) || 14189 (un->un_retry_statp == 14190 kstat_runq_back_to_waitq)); 14191 /* 14192 * If the waitq kstat was incremented when 14193 * sd_set_retry_bp() queued this bp for a retry, 14194 * then we must set up statp so that the waitq 14195 * count will get decremented correctly below. 14196 * Also we must clear un->un_retry_statp to 14197 * ensure that we do not act on a stale value 14198 * in this field. 14199 */ 14200 if ((un->un_retry_statp == kstat_waitq_enter) || 14201 (un->un_retry_statp == 14202 kstat_runq_back_to_waitq)) { 14203 statp = kstat_waitq_to_runq; 14204 } 14205 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14206 saved_statp = un->un_retry_statp; 14207 #endif 14208 un->un_retry_statp = NULL; 14209 14210 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14211 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14212 "un_throttle:%d un_ncmds_in_transport:%d\n", 14213 un, un->un_retry_bp, un->un_throttle, 14214 un->un_ncmds_in_transport); 14215 } else { 14216 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14217 "processing priority bp:0x%p\n", bp); 14218 } 14219 14220 } else if ((bp = un->un_waitq_headp) != NULL) { 14221 /* 14222 * A command on the waitq is ready to go, but do not 14223 * send it if: 14224 * 14225 * (1) the throttle limit has been reached, or 14226 * (2) a retry is pending, or 14227 * (3) a START_STOP_UNIT callback pending, or 14228 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14229 * command is pending. 14230 * 14231 * For all of these conditions, IO processing will 14232 * restart after the condition is cleared. 14233 */ 14234 if (un->un_ncmds_in_transport >= un->un_throttle) { 14235 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14236 "sd_start_cmds: exiting, " 14237 "throttle limit reached!\n"); 14238 goto exit; 14239 } 14240 if (un->un_retry_bp != NULL) { 14241 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14242 "sd_start_cmds: exiting, retry pending!\n"); 14243 goto exit; 14244 } 14245 if (un->un_startstop_timeid != NULL) { 14246 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14247 "sd_start_cmds: exiting, " 14248 "START_STOP pending!\n"); 14249 goto exit; 14250 } 14251 if (un->un_direct_priority_timeid != NULL) { 14252 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14253 "sd_start_cmds: exiting, " 14254 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14255 goto exit; 14256 } 14257 14258 /* Dequeue the command */ 14259 un->un_waitq_headp = bp->av_forw; 14260 if (un->un_waitq_headp == NULL) { 14261 un->un_waitq_tailp = NULL; 14262 } 14263 bp->av_forw = NULL; 14264 statp = kstat_waitq_to_runq; 14265 SD_TRACE(SD_LOG_IO_CORE, un, 14266 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14267 14268 } else { 14269 /* No work to do so bail out now */ 14270 SD_TRACE(SD_LOG_IO_CORE, un, 14271 "sd_start_cmds: no more work, exiting!\n"); 14272 goto exit; 14273 } 14274 14275 /* 14276 * Reset the state to normal. This is the mechanism by which 14277 * the state transitions from either SD_STATE_RWAIT or 14278 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14279 * If state is SD_STATE_PM_CHANGING then this command is 14280 * part of the device power control and the state must 14281 * not be put back to normal. Doing so would would 14282 * allow new commands to proceed when they shouldn't, 14283 * the device may be going off. 14284 */ 14285 if ((un->un_state != SD_STATE_SUSPENDED) && 14286 (un->un_state != SD_STATE_PM_CHANGING)) { 14287 New_state(un, SD_STATE_NORMAL); 14288 } 14289 14290 xp = SD_GET_XBUF(bp); 14291 ASSERT(xp != NULL); 14292 14293 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14294 /* 14295 * Allocate the scsi_pkt if we need one, or attach DMA 14296 * resources if we have a scsi_pkt that needs them. The 14297 * latter should only occur for commands that are being 14298 * retried. 14299 */ 14300 if ((xp->xb_pktp == NULL) || 14301 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14302 #else 14303 if (xp->xb_pktp == NULL) { 14304 #endif 14305 /* 14306 * There is no scsi_pkt allocated for this buf. Call 14307 * the initpkt function to allocate & init one. 14308 * 14309 * The scsi_init_pkt runout callback functionality is 14310 * implemented as follows: 14311 * 14312 * 1) The initpkt function always calls 14313 * scsi_init_pkt(9F) with sdrunout specified as the 14314 * callback routine. 14315 * 2) A successful packet allocation is initialized and 14316 * the I/O is transported. 14317 * 3) The I/O associated with an allocation resource 14318 * failure is left on its queue to be retried via 14319 * runout or the next I/O. 14320 * 4) The I/O associated with a DMA error is removed 14321 * from the queue and failed with EIO. Processing of 14322 * the transport queues is also halted to be 14323 * restarted via runout or the next I/O. 14324 * 5) The I/O associated with a CDB size or packet 14325 * size error is removed from the queue and failed 14326 * with EIO. Processing of the transport queues is 14327 * continued. 14328 * 14329 * Note: there is no interface for canceling a runout 14330 * callback. To prevent the driver from detaching or 14331 * suspending while a runout is pending the driver 14332 * state is set to SD_STATE_RWAIT 14333 * 14334 * Note: using the scsi_init_pkt callback facility can 14335 * result in an I/O request persisting at the head of 14336 * the list which cannot be satisfied even after 14337 * multiple retries. In the future the driver may 14338 * implement some kind of maximum runout count before 14339 * failing an I/O. 14340 * 14341 * Note: the use of funcp below may seem superfluous, 14342 * but it helps warlock figure out the correct 14343 * initpkt function calls (see [s]sd.wlcmd). 14344 */ 14345 struct scsi_pkt *pktp; 14346 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14347 14348 ASSERT(bp != un->un_rqs_bp); 14349 14350 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14351 switch ((*funcp)(bp, &pktp)) { 14352 case SD_PKT_ALLOC_SUCCESS: 14353 xp->xb_pktp = pktp; 14354 SD_TRACE(SD_LOG_IO_CORE, un, 14355 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14356 pktp); 14357 goto got_pkt; 14358 14359 case SD_PKT_ALLOC_FAILURE: 14360 /* 14361 * Temporary (hopefully) resource depletion. 14362 * Since retries and RQS commands always have a 14363 * scsi_pkt allocated, these cases should never 14364 * get here. So the only cases this needs to 14365 * handle is a bp from the waitq (which we put 14366 * back onto the waitq for sdrunout), or a bp 14367 * sent as an immed_bp (which we just fail). 14368 */ 14369 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14370 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14371 14372 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14373 14374 if (bp == immed_bp) { 14375 /* 14376 * If SD_XB_DMA_FREED is clear, then 14377 * this is a failure to allocate a 14378 * scsi_pkt, and we must fail the 14379 * command. 14380 */ 14381 if ((xp->xb_pkt_flags & 14382 SD_XB_DMA_FREED) == 0) { 14383 break; 14384 } 14385 14386 /* 14387 * If this immediate command is NOT our 14388 * un_retry_bp, then we must fail it. 14389 */ 14390 if (bp != un->un_retry_bp) { 14391 break; 14392 } 14393 14394 /* 14395 * We get here if this cmd is our 14396 * un_retry_bp that was DMAFREED, but 14397 * scsi_init_pkt() failed to reallocate 14398 * DMA resources when we attempted to 14399 * retry it. This can happen when an 14400 * mpxio failover is in progress, but 14401 * we don't want to just fail the 14402 * command in this case. 14403 * 14404 * Use timeout(9F) to restart it after 14405 * a 100ms delay. We don't want to 14406 * let sdrunout() restart it, because 14407 * sdrunout() is just supposed to start 14408 * commands that are sitting on the 14409 * wait queue. The un_retry_bp stays 14410 * set until the command completes, but 14411 * sdrunout can be called many times 14412 * before that happens. Since sdrunout 14413 * cannot tell if the un_retry_bp is 14414 * already in the transport, it could 14415 * end up calling scsi_transport() for 14416 * the un_retry_bp multiple times. 14417 * 14418 * Also: don't schedule the callback 14419 * if some other callback is already 14420 * pending. 14421 */ 14422 if (un->un_retry_statp == NULL) { 14423 /* 14424 * restore the kstat pointer to 14425 * keep kstat counts coherent 14426 * when we do retry the command. 14427 */ 14428 un->un_retry_statp = 14429 saved_statp; 14430 } 14431 14432 if ((un->un_startstop_timeid == NULL) && 14433 (un->un_retry_timeid == NULL) && 14434 (un->un_direct_priority_timeid == 14435 NULL)) { 14436 14437 un->un_retry_timeid = 14438 timeout( 14439 sd_start_retry_command, 14440 un, SD_RESTART_TIMEOUT); 14441 } 14442 goto exit; 14443 } 14444 14445 #else 14446 if (bp == immed_bp) { 14447 break; /* Just fail the command */ 14448 } 14449 #endif 14450 14451 /* Add the buf back to the head of the waitq */ 14452 bp->av_forw = un->un_waitq_headp; 14453 un->un_waitq_headp = bp; 14454 if (un->un_waitq_tailp == NULL) { 14455 un->un_waitq_tailp = bp; 14456 } 14457 goto exit; 14458 14459 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14460 /* 14461 * HBA DMA resource failure. Fail the command 14462 * and continue processing of the queues. 14463 */ 14464 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14465 "sd_start_cmds: " 14466 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14467 break; 14468 14469 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14470 /* 14471 * Note:x86: Partial DMA mapping not supported 14472 * for USCSI commands, and all the needed DMA 14473 * resources were not allocated. 14474 */ 14475 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14476 "sd_start_cmds: " 14477 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14478 break; 14479 14480 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14481 /* 14482 * Note:x86: Request cannot fit into CDB based 14483 * on lba and len. 14484 */ 14485 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14486 "sd_start_cmds: " 14487 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14488 break; 14489 14490 default: 14491 /* Should NEVER get here! */ 14492 panic("scsi_initpkt error"); 14493 /*NOTREACHED*/ 14494 } 14495 14496 /* 14497 * Fatal error in allocating a scsi_pkt for this buf. 14498 * Update kstats & return the buf with an error code. 14499 * We must use sd_return_failed_command_no_restart() to 14500 * avoid a recursive call back into sd_start_cmds(). 14501 * However this also means that we must keep processing 14502 * the waitq here in order to avoid stalling. 14503 */ 14504 if (statp == kstat_waitq_to_runq) { 14505 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14506 } 14507 sd_return_failed_command_no_restart(un, bp, EIO); 14508 if (bp == immed_bp) { 14509 /* immed_bp is gone by now, so clear this */ 14510 immed_bp = NULL; 14511 } 14512 continue; 14513 } 14514 got_pkt: 14515 if (bp == immed_bp) { 14516 /* goto the head of the class.... */ 14517 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14518 } 14519 14520 un->un_ncmds_in_transport++; 14521 SD_UPDATE_KSTATS(un, statp, bp); 14522 14523 /* 14524 * Call scsi_transport() to send the command to the target. 14525 * According to SCSA architecture, we must drop the mutex here 14526 * before calling scsi_transport() in order to avoid deadlock. 14527 * Note that the scsi_pkt's completion routine can be executed 14528 * (from interrupt context) even before the call to 14529 * scsi_transport() returns. 14530 */ 14531 SD_TRACE(SD_LOG_IO_CORE, un, 14532 "sd_start_cmds: calling scsi_transport()\n"); 14533 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14534 14535 mutex_exit(SD_MUTEX(un)); 14536 rval = scsi_transport(xp->xb_pktp); 14537 mutex_enter(SD_MUTEX(un)); 14538 14539 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14540 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14541 14542 switch (rval) { 14543 case TRAN_ACCEPT: 14544 /* Clear this with every pkt accepted by the HBA */ 14545 un->un_tran_fatal_count = 0; 14546 break; /* Success; try the next cmd (if any) */ 14547 14548 case TRAN_BUSY: 14549 un->un_ncmds_in_transport--; 14550 ASSERT(un->un_ncmds_in_transport >= 0); 14551 14552 /* 14553 * Don't retry request sense, the sense data 14554 * is lost when another request is sent. 14555 * Free up the rqs buf and retry 14556 * the original failed cmd. Update kstat. 14557 */ 14558 if (bp == un->un_rqs_bp) { 14559 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14560 bp = sd_mark_rqs_idle(un, xp); 14561 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14562 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14563 kstat_waitq_enter); 14564 goto exit; 14565 } 14566 14567 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14568 /* 14569 * Free the DMA resources for the scsi_pkt. This will 14570 * allow mpxio to select another path the next time 14571 * we call scsi_transport() with this scsi_pkt. 14572 * See sdintr() for the rationalization behind this. 14573 */ 14574 if ((un->un_f_is_fibre == TRUE) && 14575 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14576 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14577 scsi_dmafree(xp->xb_pktp); 14578 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14579 } 14580 #endif 14581 14582 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14583 /* 14584 * Commands that are SD_PATH_DIRECT_PRIORITY 14585 * are for error recovery situations. These do 14586 * not use the normal command waitq, so if they 14587 * get a TRAN_BUSY we cannot put them back onto 14588 * the waitq for later retry. One possible 14589 * problem is that there could already be some 14590 * other command on un_retry_bp that is waiting 14591 * for this one to complete, so we would be 14592 * deadlocked if we put this command back onto 14593 * the waitq for later retry (since un_retry_bp 14594 * must complete before the driver gets back to 14595 * commands on the waitq). 14596 * 14597 * To avoid deadlock we must schedule a callback 14598 * that will restart this command after a set 14599 * interval. This should keep retrying for as 14600 * long as the underlying transport keeps 14601 * returning TRAN_BUSY (just like for other 14602 * commands). Use the same timeout interval as 14603 * for the ordinary TRAN_BUSY retry. 14604 */ 14605 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14606 "sd_start_cmds: scsi_transport() returned " 14607 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14608 14609 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14610 un->un_direct_priority_timeid = 14611 timeout(sd_start_direct_priority_command, 14612 bp, SD_BSY_TIMEOUT / 500); 14613 14614 goto exit; 14615 } 14616 14617 /* 14618 * For TRAN_BUSY, we want to reduce the throttle value, 14619 * unless we are retrying a command. 14620 */ 14621 if (bp != un->un_retry_bp) { 14622 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14623 } 14624 14625 /* 14626 * Set up the bp to be tried again 10 ms later. 14627 * Note:x86: Is there a timeout value in the sd_lun 14628 * for this condition? 14629 */ 14630 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14631 kstat_runq_back_to_waitq); 14632 goto exit; 14633 14634 case TRAN_FATAL_ERROR: 14635 un->un_tran_fatal_count++; 14636 /* FALLTHRU */ 14637 14638 case TRAN_BADPKT: 14639 default: 14640 un->un_ncmds_in_transport--; 14641 ASSERT(un->un_ncmds_in_transport >= 0); 14642 14643 /* 14644 * If this is our REQUEST SENSE command with a 14645 * transport error, we must get back the pointers 14646 * to the original buf, and mark the REQUEST 14647 * SENSE command as "available". 14648 */ 14649 if (bp == un->un_rqs_bp) { 14650 bp = sd_mark_rqs_idle(un, xp); 14651 xp = SD_GET_XBUF(bp); 14652 } else { 14653 /* 14654 * Legacy behavior: do not update transport 14655 * error count for request sense commands. 14656 */ 14657 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14658 } 14659 14660 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14661 sd_print_transport_rejected_message(un, xp, rval); 14662 14663 /* 14664 * We must use sd_return_failed_command_no_restart() to 14665 * avoid a recursive call back into sd_start_cmds(). 14666 * However this also means that we must keep processing 14667 * the waitq here in order to avoid stalling. 14668 */ 14669 sd_return_failed_command_no_restart(un, bp, EIO); 14670 14671 /* 14672 * Notify any threads waiting in sd_ddi_suspend() that 14673 * a command completion has occurred. 14674 */ 14675 if (un->un_state == SD_STATE_SUSPENDED) { 14676 cv_broadcast(&un->un_disk_busy_cv); 14677 } 14678 14679 if (bp == immed_bp) { 14680 /* immed_bp is gone by now, so clear this */ 14681 immed_bp = NULL; 14682 } 14683 break; 14684 } 14685 14686 } while (immed_bp == NULL); 14687 14688 exit: 14689 ASSERT(mutex_owned(SD_MUTEX(un))); 14690 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14691 } 14692 14693 14694 /* 14695 * Function: sd_return_command 14696 * 14697 * Description: Returns a command to its originator (with or without an 14698 * error). Also starts commands waiting to be transported 14699 * to the target. 14700 * 14701 * Context: May be called from interrupt, kernel, or timeout context 14702 */ 14703 14704 static void 14705 sd_return_command(struct sd_lun *un, struct buf *bp) 14706 { 14707 struct sd_xbuf *xp; 14708 #if defined(__i386) || defined(__amd64) 14709 struct scsi_pkt *pktp; 14710 #endif 14711 14712 ASSERT(bp != NULL); 14713 ASSERT(un != NULL); 14714 ASSERT(mutex_owned(SD_MUTEX(un))); 14715 ASSERT(bp != un->un_rqs_bp); 14716 xp = SD_GET_XBUF(bp); 14717 ASSERT(xp != NULL); 14718 14719 #if defined(__i386) || defined(__amd64) 14720 pktp = SD_GET_PKTP(bp); 14721 #endif 14722 14723 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14724 14725 #if defined(__i386) || defined(__amd64) 14726 /* 14727 * Note:x86: check for the "sdrestart failed" case. 14728 */ 14729 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14730 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14731 (xp->xb_pktp->pkt_resid == 0)) { 14732 14733 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14734 /* 14735 * Successfully set up next portion of cmd 14736 * transfer, try sending it 14737 */ 14738 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14739 NULL, NULL, 0, (clock_t)0, NULL); 14740 sd_start_cmds(un, NULL); 14741 return; /* Note:x86: need a return here? */ 14742 } 14743 } 14744 #endif 14745 14746 /* 14747 * If this is the failfast bp, clear it from un_failfast_bp. This 14748 * can happen if upon being re-tried the failfast bp either 14749 * succeeded or encountered another error (possibly even a different 14750 * error than the one that precipitated the failfast state, but in 14751 * that case it would have had to exhaust retries as well). Regardless, 14752 * this should not occur whenever the instance is in the active 14753 * failfast state. 14754 */ 14755 if (bp == un->un_failfast_bp) { 14756 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14757 un->un_failfast_bp = NULL; 14758 } 14759 14760 /* 14761 * Clear the failfast state upon successful completion of ANY cmd. 14762 */ 14763 if (bp->b_error == 0) { 14764 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14765 } 14766 14767 /* 14768 * This is used if the command was retried one or more times. Show that 14769 * we are done with it, and allow processing of the waitq to resume. 14770 */ 14771 if (bp == un->un_retry_bp) { 14772 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14773 "sd_return_command: un:0x%p: " 14774 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14775 un->un_retry_bp = NULL; 14776 un->un_retry_statp = NULL; 14777 } 14778 14779 SD_UPDATE_RDWR_STATS(un, bp); 14780 SD_UPDATE_PARTITION_STATS(un, bp); 14781 14782 switch (un->un_state) { 14783 case SD_STATE_SUSPENDED: 14784 /* 14785 * Notify any threads waiting in sd_ddi_suspend() that 14786 * a command completion has occurred. 14787 */ 14788 cv_broadcast(&un->un_disk_busy_cv); 14789 break; 14790 default: 14791 sd_start_cmds(un, NULL); 14792 break; 14793 } 14794 14795 /* Return this command up the iodone chain to its originator. */ 14796 mutex_exit(SD_MUTEX(un)); 14797 14798 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14799 xp->xb_pktp = NULL; 14800 14801 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14802 14803 ASSERT(!mutex_owned(SD_MUTEX(un))); 14804 mutex_enter(SD_MUTEX(un)); 14805 14806 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14807 } 14808 14809 14810 /* 14811 * Function: sd_return_failed_command 14812 * 14813 * Description: Command completion when an error occurred. 14814 * 14815 * Context: May be called from interrupt context 14816 */ 14817 14818 static void 14819 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14820 { 14821 ASSERT(bp != NULL); 14822 ASSERT(un != NULL); 14823 ASSERT(mutex_owned(SD_MUTEX(un))); 14824 14825 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14826 "sd_return_failed_command: entry\n"); 14827 14828 /* 14829 * b_resid could already be nonzero due to a partial data 14830 * transfer, so do not change it here. 14831 */ 14832 SD_BIOERROR(bp, errcode); 14833 14834 sd_return_command(un, bp); 14835 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14836 "sd_return_failed_command: exit\n"); 14837 } 14838 14839 14840 /* 14841 * Function: sd_return_failed_command_no_restart 14842 * 14843 * Description: Same as sd_return_failed_command, but ensures that no 14844 * call back into sd_start_cmds will be issued. 14845 * 14846 * Context: May be called from interrupt context 14847 */ 14848 14849 static void 14850 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14851 int errcode) 14852 { 14853 struct sd_xbuf *xp; 14854 14855 ASSERT(bp != NULL); 14856 ASSERT(un != NULL); 14857 ASSERT(mutex_owned(SD_MUTEX(un))); 14858 xp = SD_GET_XBUF(bp); 14859 ASSERT(xp != NULL); 14860 ASSERT(errcode != 0); 14861 14862 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14863 "sd_return_failed_command_no_restart: entry\n"); 14864 14865 /* 14866 * b_resid could already be nonzero due to a partial data 14867 * transfer, so do not change it here. 14868 */ 14869 SD_BIOERROR(bp, errcode); 14870 14871 /* 14872 * If this is the failfast bp, clear it. This can happen if the 14873 * failfast bp encounterd a fatal error when we attempted to 14874 * re-try it (such as a scsi_transport(9F) failure). However 14875 * we should NOT be in an active failfast state if the failfast 14876 * bp is not NULL. 14877 */ 14878 if (bp == un->un_failfast_bp) { 14879 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14880 un->un_failfast_bp = NULL; 14881 } 14882 14883 if (bp == un->un_retry_bp) { 14884 /* 14885 * This command was retried one or more times. Show that we are 14886 * done with it, and allow processing of the waitq to resume. 14887 */ 14888 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14889 "sd_return_failed_command_no_restart: " 14890 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14891 un->un_retry_bp = NULL; 14892 un->un_retry_statp = NULL; 14893 } 14894 14895 SD_UPDATE_RDWR_STATS(un, bp); 14896 SD_UPDATE_PARTITION_STATS(un, bp); 14897 14898 mutex_exit(SD_MUTEX(un)); 14899 14900 if (xp->xb_pktp != NULL) { 14901 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14902 xp->xb_pktp = NULL; 14903 } 14904 14905 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14906 14907 mutex_enter(SD_MUTEX(un)); 14908 14909 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14910 "sd_return_failed_command_no_restart: exit\n"); 14911 } 14912 14913 14914 /* 14915 * Function: sd_retry_command 14916 * 14917 * Description: queue up a command for retry, or (optionally) fail it 14918 * if retry counts are exhausted. 14919 * 14920 * Arguments: un - Pointer to the sd_lun struct for the target. 14921 * 14922 * bp - Pointer to the buf for the command to be retried. 14923 * 14924 * retry_check_flag - Flag to see which (if any) of the retry 14925 * counts should be decremented/checked. If the indicated 14926 * retry count is exhausted, then the command will not be 14927 * retried; it will be failed instead. This should use a 14928 * value equal to one of the following: 14929 * 14930 * SD_RETRIES_NOCHECK 14931 * SD_RESD_RETRIES_STANDARD 14932 * SD_RETRIES_VICTIM 14933 * 14934 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14935 * if the check should be made to see of FLAG_ISOLATE is set 14936 * in the pkt. If FLAG_ISOLATE is set, then the command is 14937 * not retried, it is simply failed. 14938 * 14939 * user_funcp - Ptr to function to call before dispatching the 14940 * command. May be NULL if no action needs to be performed. 14941 * (Primarily intended for printing messages.) 14942 * 14943 * user_arg - Optional argument to be passed along to 14944 * the user_funcp call. 14945 * 14946 * failure_code - errno return code to set in the bp if the 14947 * command is going to be failed. 14948 * 14949 * retry_delay - Retry delay interval in (clock_t) units. May 14950 * be zero which indicates that the retry should be retried 14951 * immediately (ie, without an intervening delay). 14952 * 14953 * statp - Ptr to kstat function to be updated if the command 14954 * is queued for a delayed retry. May be NULL if no kstat 14955 * update is desired. 14956 * 14957 * Context: May be called from interupt context. 14958 */ 14959 14960 static void 14961 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 14962 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 14963 code), void *user_arg, int failure_code, clock_t retry_delay, 14964 void (*statp)(kstat_io_t *)) 14965 { 14966 struct sd_xbuf *xp; 14967 struct scsi_pkt *pktp; 14968 14969 ASSERT(un != NULL); 14970 ASSERT(mutex_owned(SD_MUTEX(un))); 14971 ASSERT(bp != NULL); 14972 xp = SD_GET_XBUF(bp); 14973 ASSERT(xp != NULL); 14974 pktp = SD_GET_PKTP(bp); 14975 ASSERT(pktp != NULL); 14976 14977 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14978 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 14979 14980 /* 14981 * If we are syncing or dumping, fail the command to avoid 14982 * recursively calling back into scsi_transport(). 14983 */ 14984 if (ddi_in_panic()) { 14985 goto fail_command_no_log; 14986 } 14987 14988 /* 14989 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 14990 * log an error and fail the command. 14991 */ 14992 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14993 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 14994 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 14995 sd_dump_memory(un, SD_LOG_IO, "CDB", 14996 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 14997 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 14998 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 14999 goto fail_command; 15000 } 15001 15002 /* 15003 * If we are suspended, then put the command onto head of the 15004 * wait queue since we don't want to start more commands. 15005 */ 15006 switch (un->un_state) { 15007 case SD_STATE_SUSPENDED: 15008 case SD_STATE_DUMPING: 15009 bp->av_forw = un->un_waitq_headp; 15010 un->un_waitq_headp = bp; 15011 if (un->un_waitq_tailp == NULL) { 15012 un->un_waitq_tailp = bp; 15013 } 15014 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15015 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15016 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15017 return; 15018 default: 15019 break; 15020 } 15021 15022 /* 15023 * If the caller wants us to check FLAG_ISOLATE, then see if that 15024 * is set; if it is then we do not want to retry the command. 15025 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15026 */ 15027 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15028 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15029 goto fail_command; 15030 } 15031 } 15032 15033 15034 /* 15035 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15036 * command timeout or a selection timeout has occurred. This means 15037 * that we were unable to establish an kind of communication with 15038 * the target, and subsequent retries and/or commands are likely 15039 * to encounter similar results and take a long time to complete. 15040 * 15041 * If this is a failfast error condition, we need to update the 15042 * failfast state, even if this bp does not have B_FAILFAST set. 15043 */ 15044 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15045 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15046 ASSERT(un->un_failfast_bp == NULL); 15047 /* 15048 * If we are already in the active failfast state, and 15049 * another failfast error condition has been detected, 15050 * then fail this command if it has B_FAILFAST set. 15051 * If B_FAILFAST is clear, then maintain the legacy 15052 * behavior of retrying heroically, even tho this will 15053 * take a lot more time to fail the command. 15054 */ 15055 if (bp->b_flags & B_FAILFAST) { 15056 goto fail_command; 15057 } 15058 } else { 15059 /* 15060 * We're not in the active failfast state, but we 15061 * have a failfast error condition, so we must begin 15062 * transition to the next state. We do this regardless 15063 * of whether or not this bp has B_FAILFAST set. 15064 */ 15065 if (un->un_failfast_bp == NULL) { 15066 /* 15067 * This is the first bp to meet a failfast 15068 * condition so save it on un_failfast_bp & 15069 * do normal retry processing. Do not enter 15070 * active failfast state yet. This marks 15071 * entry into the "failfast pending" state. 15072 */ 15073 un->un_failfast_bp = bp; 15074 15075 } else if (un->un_failfast_bp == bp) { 15076 /* 15077 * This is the second time *this* bp has 15078 * encountered a failfast error condition, 15079 * so enter active failfast state & flush 15080 * queues as appropriate. 15081 */ 15082 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15083 un->un_failfast_bp = NULL; 15084 sd_failfast_flushq(un); 15085 15086 /* 15087 * Fail this bp now if B_FAILFAST set; 15088 * otherwise continue with retries. (It would 15089 * be pretty ironic if this bp succeeded on a 15090 * subsequent retry after we just flushed all 15091 * the queues). 15092 */ 15093 if (bp->b_flags & B_FAILFAST) { 15094 goto fail_command; 15095 } 15096 15097 #if !defined(lint) && !defined(__lint) 15098 } else { 15099 /* 15100 * If neither of the preceeding conditionals 15101 * was true, it means that there is some 15102 * *other* bp that has met an inital failfast 15103 * condition and is currently either being 15104 * retried or is waiting to be retried. In 15105 * that case we should perform normal retry 15106 * processing on *this* bp, since there is a 15107 * chance that the current failfast condition 15108 * is transient and recoverable. If that does 15109 * not turn out to be the case, then retries 15110 * will be cleared when the wait queue is 15111 * flushed anyway. 15112 */ 15113 #endif 15114 } 15115 } 15116 } else { 15117 /* 15118 * SD_RETRIES_FAILFAST is clear, which indicates that we 15119 * likely were able to at least establish some level of 15120 * communication with the target and subsequent commands 15121 * and/or retries are likely to get through to the target, 15122 * In this case we want to be aggressive about clearing 15123 * the failfast state. Note that this does not affect 15124 * the "failfast pending" condition. 15125 */ 15126 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15127 } 15128 15129 15130 /* 15131 * Check the specified retry count to see if we can still do 15132 * any retries with this pkt before we should fail it. 15133 */ 15134 switch (retry_check_flag & SD_RETRIES_MASK) { 15135 case SD_RETRIES_VICTIM: 15136 /* 15137 * Check the victim retry count. If exhausted, then fall 15138 * thru & check against the standard retry count. 15139 */ 15140 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15141 /* Increment count & proceed with the retry */ 15142 xp->xb_victim_retry_count++; 15143 break; 15144 } 15145 /* Victim retries exhausted, fall back to std. retries... */ 15146 /* FALLTHRU */ 15147 15148 case SD_RETRIES_STANDARD: 15149 if (xp->xb_retry_count >= un->un_retry_count) { 15150 /* Retries exhausted, fail the command */ 15151 SD_TRACE(SD_LOG_IO_CORE, un, 15152 "sd_retry_command: retries exhausted!\n"); 15153 /* 15154 * update b_resid for failed SCMD_READ & SCMD_WRITE 15155 * commands with nonzero pkt_resid. 15156 */ 15157 if ((pktp->pkt_reason == CMD_CMPLT) && 15158 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15159 (pktp->pkt_resid != 0)) { 15160 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15161 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15162 SD_UPDATE_B_RESID(bp, pktp); 15163 } 15164 } 15165 goto fail_command; 15166 } 15167 xp->xb_retry_count++; 15168 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15169 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15170 break; 15171 15172 case SD_RETRIES_UA: 15173 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15174 /* Retries exhausted, fail the command */ 15175 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15176 "Unit Attention retries exhausted. " 15177 "Check the target.\n"); 15178 goto fail_command; 15179 } 15180 xp->xb_ua_retry_count++; 15181 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15182 "sd_retry_command: retry count:%d\n", 15183 xp->xb_ua_retry_count); 15184 break; 15185 15186 case SD_RETRIES_BUSY: 15187 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15188 /* Retries exhausted, fail the command */ 15189 SD_TRACE(SD_LOG_IO_CORE, un, 15190 "sd_retry_command: retries exhausted!\n"); 15191 goto fail_command; 15192 } 15193 xp->xb_retry_count++; 15194 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15195 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15196 break; 15197 15198 case SD_RETRIES_NOCHECK: 15199 default: 15200 /* No retry count to check. Just proceed with the retry */ 15201 break; 15202 } 15203 15204 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15205 15206 /* 15207 * If we were given a zero timeout, we must attempt to retry the 15208 * command immediately (ie, without a delay). 15209 */ 15210 if (retry_delay == 0) { 15211 /* 15212 * Check some limiting conditions to see if we can actually 15213 * do the immediate retry. If we cannot, then we must 15214 * fall back to queueing up a delayed retry. 15215 */ 15216 if (un->un_ncmds_in_transport >= un->un_throttle) { 15217 /* 15218 * We are at the throttle limit for the target, 15219 * fall back to delayed retry. 15220 */ 15221 retry_delay = SD_BSY_TIMEOUT; 15222 statp = kstat_waitq_enter; 15223 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15224 "sd_retry_command: immed. retry hit " 15225 "throttle!\n"); 15226 } else { 15227 /* 15228 * We're clear to proceed with the immediate retry. 15229 * First call the user-provided function (if any) 15230 */ 15231 if (user_funcp != NULL) { 15232 (*user_funcp)(un, bp, user_arg, 15233 SD_IMMEDIATE_RETRY_ISSUED); 15234 #ifdef __lock_lint 15235 sd_print_incomplete_msg(un, bp, user_arg, 15236 SD_IMMEDIATE_RETRY_ISSUED); 15237 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15238 SD_IMMEDIATE_RETRY_ISSUED); 15239 sd_print_sense_failed_msg(un, bp, user_arg, 15240 SD_IMMEDIATE_RETRY_ISSUED); 15241 #endif 15242 } 15243 15244 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15245 "sd_retry_command: issuing immediate retry\n"); 15246 15247 /* 15248 * Call sd_start_cmds() to transport the command to 15249 * the target. 15250 */ 15251 sd_start_cmds(un, bp); 15252 15253 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15254 "sd_retry_command exit\n"); 15255 return; 15256 } 15257 } 15258 15259 /* 15260 * Set up to retry the command after a delay. 15261 * First call the user-provided function (if any) 15262 */ 15263 if (user_funcp != NULL) { 15264 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15265 } 15266 15267 sd_set_retry_bp(un, bp, retry_delay, statp); 15268 15269 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15270 return; 15271 15272 fail_command: 15273 15274 if (user_funcp != NULL) { 15275 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15276 } 15277 15278 fail_command_no_log: 15279 15280 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15281 "sd_retry_command: returning failed command\n"); 15282 15283 sd_return_failed_command(un, bp, failure_code); 15284 15285 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15286 } 15287 15288 15289 /* 15290 * Function: sd_set_retry_bp 15291 * 15292 * Description: Set up the given bp for retry. 15293 * 15294 * Arguments: un - ptr to associated softstate 15295 * bp - ptr to buf(9S) for the command 15296 * retry_delay - time interval before issuing retry (may be 0) 15297 * statp - optional pointer to kstat function 15298 * 15299 * Context: May be called under interrupt context 15300 */ 15301 15302 static void 15303 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15304 void (*statp)(kstat_io_t *)) 15305 { 15306 ASSERT(un != NULL); 15307 ASSERT(mutex_owned(SD_MUTEX(un))); 15308 ASSERT(bp != NULL); 15309 15310 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15311 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15312 15313 /* 15314 * Indicate that the command is being retried. This will not allow any 15315 * other commands on the wait queue to be transported to the target 15316 * until this command has been completed (success or failure). The 15317 * "retry command" is not transported to the target until the given 15318 * time delay expires, unless the user specified a 0 retry_delay. 15319 * 15320 * Note: the timeout(9F) callback routine is what actually calls 15321 * sd_start_cmds() to transport the command, with the exception of a 15322 * zero retry_delay. The only current implementor of a zero retry delay 15323 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15324 */ 15325 if (un->un_retry_bp == NULL) { 15326 ASSERT(un->un_retry_statp == NULL); 15327 un->un_retry_bp = bp; 15328 15329 /* 15330 * If the user has not specified a delay the command should 15331 * be queued and no timeout should be scheduled. 15332 */ 15333 if (retry_delay == 0) { 15334 /* 15335 * Save the kstat pointer that will be used in the 15336 * call to SD_UPDATE_KSTATS() below, so that 15337 * sd_start_cmds() can correctly decrement the waitq 15338 * count when it is time to transport this command. 15339 */ 15340 un->un_retry_statp = statp; 15341 goto done; 15342 } 15343 } 15344 15345 if (un->un_retry_bp == bp) { 15346 /* 15347 * Save the kstat pointer that will be used in the call to 15348 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15349 * correctly decrement the waitq count when it is time to 15350 * transport this command. 15351 */ 15352 un->un_retry_statp = statp; 15353 15354 /* 15355 * Schedule a timeout if: 15356 * 1) The user has specified a delay. 15357 * 2) There is not a START_STOP_UNIT callback pending. 15358 * 15359 * If no delay has been specified, then it is up to the caller 15360 * to ensure that IO processing continues without stalling. 15361 * Effectively, this means that the caller will issue the 15362 * required call to sd_start_cmds(). The START_STOP_UNIT 15363 * callback does this after the START STOP UNIT command has 15364 * completed. In either of these cases we should not schedule 15365 * a timeout callback here. Also don't schedule the timeout if 15366 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15367 */ 15368 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15369 (un->un_direct_priority_timeid == NULL)) { 15370 un->un_retry_timeid = 15371 timeout(sd_start_retry_command, un, retry_delay); 15372 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15373 "sd_set_retry_bp: setting timeout: un: 0x%p" 15374 " bp:0x%p un_retry_timeid:0x%p\n", 15375 un, bp, un->un_retry_timeid); 15376 } 15377 } else { 15378 /* 15379 * We only get in here if there is already another command 15380 * waiting to be retried. In this case, we just put the 15381 * given command onto the wait queue, so it can be transported 15382 * after the current retry command has completed. 15383 * 15384 * Also we have to make sure that if the command at the head 15385 * of the wait queue is the un_failfast_bp, that we do not 15386 * put ahead of it any other commands that are to be retried. 15387 */ 15388 if ((un->un_failfast_bp != NULL) && 15389 (un->un_failfast_bp == un->un_waitq_headp)) { 15390 /* 15391 * Enqueue this command AFTER the first command on 15392 * the wait queue (which is also un_failfast_bp). 15393 */ 15394 bp->av_forw = un->un_waitq_headp->av_forw; 15395 un->un_waitq_headp->av_forw = bp; 15396 if (un->un_waitq_headp == un->un_waitq_tailp) { 15397 un->un_waitq_tailp = bp; 15398 } 15399 } else { 15400 /* Enqueue this command at the head of the waitq. */ 15401 bp->av_forw = un->un_waitq_headp; 15402 un->un_waitq_headp = bp; 15403 if (un->un_waitq_tailp == NULL) { 15404 un->un_waitq_tailp = bp; 15405 } 15406 } 15407 15408 if (statp == NULL) { 15409 statp = kstat_waitq_enter; 15410 } 15411 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15412 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15413 } 15414 15415 done: 15416 if (statp != NULL) { 15417 SD_UPDATE_KSTATS(un, statp, bp); 15418 } 15419 15420 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15421 "sd_set_retry_bp: exit un:0x%p\n", un); 15422 } 15423 15424 15425 /* 15426 * Function: sd_start_retry_command 15427 * 15428 * Description: Start the command that has been waiting on the target's 15429 * retry queue. Called from timeout(9F) context after the 15430 * retry delay interval has expired. 15431 * 15432 * Arguments: arg - pointer to associated softstate for the device. 15433 * 15434 * Context: timeout(9F) thread context. May not sleep. 15435 */ 15436 15437 static void 15438 sd_start_retry_command(void *arg) 15439 { 15440 struct sd_lun *un = arg; 15441 15442 ASSERT(un != NULL); 15443 ASSERT(!mutex_owned(SD_MUTEX(un))); 15444 15445 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15446 "sd_start_retry_command: entry\n"); 15447 15448 mutex_enter(SD_MUTEX(un)); 15449 15450 un->un_retry_timeid = NULL; 15451 15452 if (un->un_retry_bp != NULL) { 15453 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15454 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15455 un, un->un_retry_bp); 15456 sd_start_cmds(un, un->un_retry_bp); 15457 } 15458 15459 mutex_exit(SD_MUTEX(un)); 15460 15461 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15462 "sd_start_retry_command: exit\n"); 15463 } 15464 15465 15466 /* 15467 * Function: sd_start_direct_priority_command 15468 * 15469 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15470 * received TRAN_BUSY when we called scsi_transport() to send it 15471 * to the underlying HBA. This function is called from timeout(9F) 15472 * context after the delay interval has expired. 15473 * 15474 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15475 * 15476 * Context: timeout(9F) thread context. May not sleep. 15477 */ 15478 15479 static void 15480 sd_start_direct_priority_command(void *arg) 15481 { 15482 struct buf *priority_bp = arg; 15483 struct sd_lun *un; 15484 15485 ASSERT(priority_bp != NULL); 15486 un = SD_GET_UN(priority_bp); 15487 ASSERT(un != NULL); 15488 ASSERT(!mutex_owned(SD_MUTEX(un))); 15489 15490 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15491 "sd_start_direct_priority_command: entry\n"); 15492 15493 mutex_enter(SD_MUTEX(un)); 15494 un->un_direct_priority_timeid = NULL; 15495 sd_start_cmds(un, priority_bp); 15496 mutex_exit(SD_MUTEX(un)); 15497 15498 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15499 "sd_start_direct_priority_command: exit\n"); 15500 } 15501 15502 15503 /* 15504 * Function: sd_send_request_sense_command 15505 * 15506 * Description: Sends a REQUEST SENSE command to the target 15507 * 15508 * Context: May be called from interrupt context. 15509 */ 15510 15511 static void 15512 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15513 struct scsi_pkt *pktp) 15514 { 15515 ASSERT(bp != NULL); 15516 ASSERT(un != NULL); 15517 ASSERT(mutex_owned(SD_MUTEX(un))); 15518 15519 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15520 "entry: buf:0x%p\n", bp); 15521 15522 /* 15523 * If we are syncing or dumping, then fail the command to avoid a 15524 * recursive callback into scsi_transport(). Also fail the command 15525 * if we are suspended (legacy behavior). 15526 */ 15527 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15528 (un->un_state == SD_STATE_DUMPING)) { 15529 sd_return_failed_command(un, bp, EIO); 15530 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15531 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15532 return; 15533 } 15534 15535 /* 15536 * Retry the failed command and don't issue the request sense if: 15537 * 1) the sense buf is busy 15538 * 2) we have 1 or more outstanding commands on the target 15539 * (the sense data will be cleared or invalidated any way) 15540 * 15541 * Note: There could be an issue with not checking a retry limit here, 15542 * the problem is determining which retry limit to check. 15543 */ 15544 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15545 /* Don't retry if the command is flagged as non-retryable */ 15546 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15547 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15548 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15549 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15550 "sd_send_request_sense_command: " 15551 "at full throttle, retrying exit\n"); 15552 } else { 15553 sd_return_failed_command(un, bp, EIO); 15554 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15555 "sd_send_request_sense_command: " 15556 "at full throttle, non-retryable exit\n"); 15557 } 15558 return; 15559 } 15560 15561 sd_mark_rqs_busy(un, bp); 15562 sd_start_cmds(un, un->un_rqs_bp); 15563 15564 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15565 "sd_send_request_sense_command: exit\n"); 15566 } 15567 15568 15569 /* 15570 * Function: sd_mark_rqs_busy 15571 * 15572 * Description: Indicate that the request sense bp for this instance is 15573 * in use. 15574 * 15575 * Context: May be called under interrupt context 15576 */ 15577 15578 static void 15579 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15580 { 15581 struct sd_xbuf *sense_xp; 15582 15583 ASSERT(un != NULL); 15584 ASSERT(bp != NULL); 15585 ASSERT(mutex_owned(SD_MUTEX(un))); 15586 ASSERT(un->un_sense_isbusy == 0); 15587 15588 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15589 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15590 15591 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15592 ASSERT(sense_xp != NULL); 15593 15594 SD_INFO(SD_LOG_IO, un, 15595 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15596 15597 ASSERT(sense_xp->xb_pktp != NULL); 15598 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15599 == (FLAG_SENSING | FLAG_HEAD)); 15600 15601 un->un_sense_isbusy = 1; 15602 un->un_rqs_bp->b_resid = 0; 15603 sense_xp->xb_pktp->pkt_resid = 0; 15604 sense_xp->xb_pktp->pkt_reason = 0; 15605 15606 /* So we can get back the bp at interrupt time! */ 15607 sense_xp->xb_sense_bp = bp; 15608 15609 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15610 15611 /* 15612 * Mark this buf as awaiting sense data. (This is already set in 15613 * the pkt_flags for the RQS packet.) 15614 */ 15615 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15616 15617 sense_xp->xb_retry_count = 0; 15618 sense_xp->xb_victim_retry_count = 0; 15619 sense_xp->xb_ua_retry_count = 0; 15620 sense_xp->xb_dma_resid = 0; 15621 15622 /* Clean up the fields for auto-request sense */ 15623 sense_xp->xb_sense_status = 0; 15624 sense_xp->xb_sense_state = 0; 15625 sense_xp->xb_sense_resid = 0; 15626 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15627 15628 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15629 } 15630 15631 15632 /* 15633 * Function: sd_mark_rqs_idle 15634 * 15635 * Description: SD_MUTEX must be held continuously through this routine 15636 * to prevent reuse of the rqs struct before the caller can 15637 * complete it's processing. 15638 * 15639 * Return Code: Pointer to the RQS buf 15640 * 15641 * Context: May be called under interrupt context 15642 */ 15643 15644 static struct buf * 15645 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15646 { 15647 struct buf *bp; 15648 ASSERT(un != NULL); 15649 ASSERT(sense_xp != NULL); 15650 ASSERT(mutex_owned(SD_MUTEX(un))); 15651 ASSERT(un->un_sense_isbusy != 0); 15652 15653 un->un_sense_isbusy = 0; 15654 bp = sense_xp->xb_sense_bp; 15655 sense_xp->xb_sense_bp = NULL; 15656 15657 /* This pkt is no longer interested in getting sense data */ 15658 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15659 15660 return (bp); 15661 } 15662 15663 15664 15665 /* 15666 * Function: sd_alloc_rqs 15667 * 15668 * Description: Set up the unit to receive auto request sense data 15669 * 15670 * Return Code: DDI_SUCCESS or DDI_FAILURE 15671 * 15672 * Context: Called under attach(9E) context 15673 */ 15674 15675 static int 15676 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15677 { 15678 struct sd_xbuf *xp; 15679 15680 ASSERT(un != NULL); 15681 ASSERT(!mutex_owned(SD_MUTEX(un))); 15682 ASSERT(un->un_rqs_bp == NULL); 15683 ASSERT(un->un_rqs_pktp == NULL); 15684 15685 /* 15686 * First allocate the required buf and scsi_pkt structs, then set up 15687 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15688 */ 15689 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15690 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15691 if (un->un_rqs_bp == NULL) { 15692 return (DDI_FAILURE); 15693 } 15694 15695 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15696 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15697 15698 if (un->un_rqs_pktp == NULL) { 15699 sd_free_rqs(un); 15700 return (DDI_FAILURE); 15701 } 15702 15703 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15704 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15705 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15706 15707 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15708 15709 /* Set up the other needed members in the ARQ scsi_pkt. */ 15710 un->un_rqs_pktp->pkt_comp = sdintr; 15711 un->un_rqs_pktp->pkt_time = sd_io_time; 15712 un->un_rqs_pktp->pkt_flags |= 15713 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15714 15715 /* 15716 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15717 * provide any intpkt, destroypkt routines as we take care of 15718 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15719 */ 15720 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15721 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15722 xp->xb_pktp = un->un_rqs_pktp; 15723 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15724 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15725 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15726 15727 /* 15728 * Save the pointer to the request sense private bp so it can 15729 * be retrieved in sdintr. 15730 */ 15731 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15732 ASSERT(un->un_rqs_bp->b_private == xp); 15733 15734 /* 15735 * See if the HBA supports auto-request sense for the specified 15736 * target/lun. If it does, then try to enable it (if not already 15737 * enabled). 15738 * 15739 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15740 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15741 * return success. However, in both of these cases ARQ is always 15742 * enabled and scsi_ifgetcap will always return true. The best approach 15743 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15744 * 15745 * The 3rd case is the HBA (adp) always return enabled on 15746 * scsi_ifgetgetcap even when it's not enable, the best approach 15747 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15748 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15749 */ 15750 15751 if (un->un_f_is_fibre == TRUE) { 15752 un->un_f_arq_enabled = TRUE; 15753 } else { 15754 #if defined(__i386) || defined(__amd64) 15755 /* 15756 * Circumvent the Adaptec bug, remove this code when 15757 * the bug is fixed 15758 */ 15759 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15760 #endif 15761 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15762 case 0: 15763 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15764 "sd_alloc_rqs: HBA supports ARQ\n"); 15765 /* 15766 * ARQ is supported by this HBA but currently is not 15767 * enabled. Attempt to enable it and if successful then 15768 * mark this instance as ARQ enabled. 15769 */ 15770 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15771 == 1) { 15772 /* Successfully enabled ARQ in the HBA */ 15773 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15774 "sd_alloc_rqs: ARQ enabled\n"); 15775 un->un_f_arq_enabled = TRUE; 15776 } else { 15777 /* Could not enable ARQ in the HBA */ 15778 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15779 "sd_alloc_rqs: failed ARQ enable\n"); 15780 un->un_f_arq_enabled = FALSE; 15781 } 15782 break; 15783 case 1: 15784 /* 15785 * ARQ is supported by this HBA and is already enabled. 15786 * Just mark ARQ as enabled for this instance. 15787 */ 15788 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15789 "sd_alloc_rqs: ARQ already enabled\n"); 15790 un->un_f_arq_enabled = TRUE; 15791 break; 15792 default: 15793 /* 15794 * ARQ is not supported by this HBA; disable it for this 15795 * instance. 15796 */ 15797 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15798 "sd_alloc_rqs: HBA does not support ARQ\n"); 15799 un->un_f_arq_enabled = FALSE; 15800 break; 15801 } 15802 } 15803 15804 return (DDI_SUCCESS); 15805 } 15806 15807 15808 /* 15809 * Function: sd_free_rqs 15810 * 15811 * Description: Cleanup for the pre-instance RQS command. 15812 * 15813 * Context: Kernel thread context 15814 */ 15815 15816 static void 15817 sd_free_rqs(struct sd_lun *un) 15818 { 15819 ASSERT(un != NULL); 15820 15821 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15822 15823 /* 15824 * If consistent memory is bound to a scsi_pkt, the pkt 15825 * has to be destroyed *before* freeing the consistent memory. 15826 * Don't change the sequence of this operations. 15827 * scsi_destroy_pkt() might access memory, which isn't allowed, 15828 * after it was freed in scsi_free_consistent_buf(). 15829 */ 15830 if (un->un_rqs_pktp != NULL) { 15831 scsi_destroy_pkt(un->un_rqs_pktp); 15832 un->un_rqs_pktp = NULL; 15833 } 15834 15835 if (un->un_rqs_bp != NULL) { 15836 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 15837 scsi_free_consistent_buf(un->un_rqs_bp); 15838 un->un_rqs_bp = NULL; 15839 } 15840 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15841 } 15842 15843 15844 15845 /* 15846 * Function: sd_reduce_throttle 15847 * 15848 * Description: Reduces the maximun # of outstanding commands on a 15849 * target to the current number of outstanding commands. 15850 * Queues a tiemout(9F) callback to restore the limit 15851 * after a specified interval has elapsed. 15852 * Typically used when we get a TRAN_BUSY return code 15853 * back from scsi_transport(). 15854 * 15855 * Arguments: un - ptr to the sd_lun softstate struct 15856 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15857 * 15858 * Context: May be called from interrupt context 15859 */ 15860 15861 static void 15862 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15863 { 15864 ASSERT(un != NULL); 15865 ASSERT(mutex_owned(SD_MUTEX(un))); 15866 ASSERT(un->un_ncmds_in_transport >= 0); 15867 15868 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15869 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15870 un, un->un_throttle, un->un_ncmds_in_transport); 15871 15872 if (un->un_throttle > 1) { 15873 if (un->un_f_use_adaptive_throttle == TRUE) { 15874 switch (throttle_type) { 15875 case SD_THROTTLE_TRAN_BUSY: 15876 if (un->un_busy_throttle == 0) { 15877 un->un_busy_throttle = un->un_throttle; 15878 } 15879 break; 15880 case SD_THROTTLE_QFULL: 15881 un->un_busy_throttle = 0; 15882 break; 15883 default: 15884 ASSERT(FALSE); 15885 } 15886 15887 if (un->un_ncmds_in_transport > 0) { 15888 un->un_throttle = un->un_ncmds_in_transport; 15889 } 15890 15891 } else { 15892 if (un->un_ncmds_in_transport == 0) { 15893 un->un_throttle = 1; 15894 } else { 15895 un->un_throttle = un->un_ncmds_in_transport; 15896 } 15897 } 15898 } 15899 15900 /* Reschedule the timeout if none is currently active */ 15901 if (un->un_reset_throttle_timeid == NULL) { 15902 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15903 un, SD_THROTTLE_RESET_INTERVAL); 15904 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15905 "sd_reduce_throttle: timeout scheduled!\n"); 15906 } 15907 15908 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15909 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15910 } 15911 15912 15913 15914 /* 15915 * Function: sd_restore_throttle 15916 * 15917 * Description: Callback function for timeout(9F). Resets the current 15918 * value of un->un_throttle to its default. 15919 * 15920 * Arguments: arg - pointer to associated softstate for the device. 15921 * 15922 * Context: May be called from interrupt context 15923 */ 15924 15925 static void 15926 sd_restore_throttle(void *arg) 15927 { 15928 struct sd_lun *un = arg; 15929 15930 ASSERT(un != NULL); 15931 ASSERT(!mutex_owned(SD_MUTEX(un))); 15932 15933 mutex_enter(SD_MUTEX(un)); 15934 15935 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15936 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15937 15938 un->un_reset_throttle_timeid = NULL; 15939 15940 if (un->un_f_use_adaptive_throttle == TRUE) { 15941 /* 15942 * If un_busy_throttle is nonzero, then it contains the 15943 * value that un_throttle was when we got a TRAN_BUSY back 15944 * from scsi_transport(). We want to revert back to this 15945 * value. 15946 * 15947 * In the QFULL case, the throttle limit will incrementally 15948 * increase until it reaches max throttle. 15949 */ 15950 if (un->un_busy_throttle > 0) { 15951 un->un_throttle = un->un_busy_throttle; 15952 un->un_busy_throttle = 0; 15953 } else { 15954 /* 15955 * increase throttle by 10% open gate slowly, schedule 15956 * another restore if saved throttle has not been 15957 * reached 15958 */ 15959 short throttle; 15960 if (sd_qfull_throttle_enable) { 15961 throttle = un->un_throttle + 15962 max((un->un_throttle / 10), 1); 15963 un->un_throttle = 15964 (throttle < un->un_saved_throttle) ? 15965 throttle : un->un_saved_throttle; 15966 if (un->un_throttle < un->un_saved_throttle) { 15967 un->un_reset_throttle_timeid = 15968 timeout(sd_restore_throttle, 15969 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 15970 } 15971 } 15972 } 15973 15974 /* 15975 * If un_throttle has fallen below the low-water mark, we 15976 * restore the maximum value here (and allow it to ratchet 15977 * down again if necessary). 15978 */ 15979 if (un->un_throttle < un->un_min_throttle) { 15980 un->un_throttle = un->un_saved_throttle; 15981 } 15982 } else { 15983 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15984 "restoring limit from 0x%x to 0x%x\n", 15985 un->un_throttle, un->un_saved_throttle); 15986 un->un_throttle = un->un_saved_throttle; 15987 } 15988 15989 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15990 "sd_restore_throttle: calling sd_start_cmds!\n"); 15991 15992 sd_start_cmds(un, NULL); 15993 15994 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15995 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 15996 un, un->un_throttle); 15997 15998 mutex_exit(SD_MUTEX(un)); 15999 16000 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16001 } 16002 16003 /* 16004 * Function: sdrunout 16005 * 16006 * Description: Callback routine for scsi_init_pkt when a resource allocation 16007 * fails. 16008 * 16009 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16010 * soft state instance. 16011 * 16012 * Return Code: The scsi_init_pkt routine allows for the callback function to 16013 * return a 0 indicating the callback should be rescheduled or a 1 16014 * indicating not to reschedule. This routine always returns 1 16015 * because the driver always provides a callback function to 16016 * scsi_init_pkt. This results in a callback always being scheduled 16017 * (via the scsi_init_pkt callback implementation) if a resource 16018 * failure occurs. 16019 * 16020 * Context: This callback function may not block or call routines that block 16021 * 16022 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16023 * request persisting at the head of the list which cannot be 16024 * satisfied even after multiple retries. In the future the driver 16025 * may implement some time of maximum runout count before failing 16026 * an I/O. 16027 */ 16028 16029 static int 16030 sdrunout(caddr_t arg) 16031 { 16032 struct sd_lun *un = (struct sd_lun *)arg; 16033 16034 ASSERT(un != NULL); 16035 ASSERT(!mutex_owned(SD_MUTEX(un))); 16036 16037 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16038 16039 mutex_enter(SD_MUTEX(un)); 16040 sd_start_cmds(un, NULL); 16041 mutex_exit(SD_MUTEX(un)); 16042 /* 16043 * This callback routine always returns 1 (i.e. do not reschedule) 16044 * because we always specify sdrunout as the callback handler for 16045 * scsi_init_pkt inside the call to sd_start_cmds. 16046 */ 16047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16048 return (1); 16049 } 16050 16051 16052 /* 16053 * Function: sdintr 16054 * 16055 * Description: Completion callback routine for scsi_pkt(9S) structs 16056 * sent to the HBA driver via scsi_transport(9F). 16057 * 16058 * Context: Interrupt context 16059 */ 16060 16061 static void 16062 sdintr(struct scsi_pkt *pktp) 16063 { 16064 struct buf *bp; 16065 struct sd_xbuf *xp; 16066 struct sd_lun *un; 16067 16068 ASSERT(pktp != NULL); 16069 bp = (struct buf *)pktp->pkt_private; 16070 ASSERT(bp != NULL); 16071 xp = SD_GET_XBUF(bp); 16072 ASSERT(xp != NULL); 16073 ASSERT(xp->xb_pktp != NULL); 16074 un = SD_GET_UN(bp); 16075 ASSERT(un != NULL); 16076 ASSERT(!mutex_owned(SD_MUTEX(un))); 16077 16078 #ifdef SD_FAULT_INJECTION 16079 16080 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16081 /* SD FaultInjection */ 16082 sd_faultinjection(pktp); 16083 16084 #endif /* SD_FAULT_INJECTION */ 16085 16086 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16087 " xp:0x%p, un:0x%p\n", bp, xp, un); 16088 16089 mutex_enter(SD_MUTEX(un)); 16090 16091 /* Reduce the count of the #commands currently in transport */ 16092 un->un_ncmds_in_transport--; 16093 ASSERT(un->un_ncmds_in_transport >= 0); 16094 16095 /* Increment counter to indicate that the callback routine is active */ 16096 un->un_in_callback++; 16097 16098 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16099 16100 #ifdef SDDEBUG 16101 if (bp == un->un_retry_bp) { 16102 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16103 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16104 un, un->un_retry_bp, un->un_ncmds_in_transport); 16105 } 16106 #endif 16107 16108 /* 16109 * If pkt_reason is CMD_DEV_GONE, just fail the command 16110 */ 16111 if (pktp->pkt_reason == CMD_DEV_GONE) { 16112 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16113 "Device is gone\n"); 16114 sd_return_failed_command(un, bp, EIO); 16115 goto exit; 16116 } 16117 16118 /* 16119 * First see if the pkt has auto-request sense data with it.... 16120 * Look at the packet state first so we don't take a performance 16121 * hit looking at the arq enabled flag unless absolutely necessary. 16122 */ 16123 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16124 (un->un_f_arq_enabled == TRUE)) { 16125 /* 16126 * The HBA did an auto request sense for this command so check 16127 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16128 * driver command that should not be retried. 16129 */ 16130 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16131 /* 16132 * Save the relevant sense info into the xp for the 16133 * original cmd. 16134 */ 16135 struct scsi_arq_status *asp; 16136 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16137 xp->xb_sense_status = 16138 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16139 xp->xb_sense_state = asp->sts_rqpkt_state; 16140 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16141 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16142 min(sizeof (struct scsi_extended_sense), 16143 SENSE_LENGTH)); 16144 16145 /* fail the command */ 16146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16147 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16148 sd_return_failed_command(un, bp, EIO); 16149 goto exit; 16150 } 16151 16152 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16153 /* 16154 * We want to either retry or fail this command, so free 16155 * the DMA resources here. If we retry the command then 16156 * the DMA resources will be reallocated in sd_start_cmds(). 16157 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16158 * causes the *entire* transfer to start over again from the 16159 * beginning of the request, even for PARTIAL chunks that 16160 * have already transferred successfully. 16161 */ 16162 if ((un->un_f_is_fibre == TRUE) && 16163 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16164 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16165 scsi_dmafree(pktp); 16166 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16167 } 16168 #endif 16169 16170 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16171 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16172 16173 sd_handle_auto_request_sense(un, bp, xp, pktp); 16174 goto exit; 16175 } 16176 16177 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16178 if (pktp->pkt_flags & FLAG_SENSING) { 16179 /* This pktp is from the unit's REQUEST_SENSE command */ 16180 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16181 "sdintr: sd_handle_request_sense\n"); 16182 sd_handle_request_sense(un, bp, xp, pktp); 16183 goto exit; 16184 } 16185 16186 /* 16187 * Check to see if the command successfully completed as requested; 16188 * this is the most common case (and also the hot performance path). 16189 * 16190 * Requirements for successful completion are: 16191 * pkt_reason is CMD_CMPLT and packet status is status good. 16192 * In addition: 16193 * - A residual of zero indicates successful completion no matter what 16194 * the command is. 16195 * - If the residual is not zero and the command is not a read or 16196 * write, then it's still defined as successful completion. In other 16197 * words, if the command is a read or write the residual must be 16198 * zero for successful completion. 16199 * - If the residual is not zero and the command is a read or 16200 * write, and it's a USCSICMD, then it's still defined as 16201 * successful completion. 16202 */ 16203 if ((pktp->pkt_reason == CMD_CMPLT) && 16204 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16205 16206 /* 16207 * Since this command is returned with a good status, we 16208 * can reset the count for Sonoma failover. 16209 */ 16210 un->un_sonoma_failure_count = 0; 16211 16212 /* 16213 * Return all USCSI commands on good status 16214 */ 16215 if (pktp->pkt_resid == 0) { 16216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16217 "sdintr: returning command for resid == 0\n"); 16218 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16219 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16220 SD_UPDATE_B_RESID(bp, pktp); 16221 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16222 "sdintr: returning command for resid != 0\n"); 16223 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16224 SD_UPDATE_B_RESID(bp, pktp); 16225 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16226 "sdintr: returning uscsi command\n"); 16227 } else { 16228 goto not_successful; 16229 } 16230 sd_return_command(un, bp); 16231 16232 /* 16233 * Decrement counter to indicate that the callback routine 16234 * is done. 16235 */ 16236 un->un_in_callback--; 16237 ASSERT(un->un_in_callback >= 0); 16238 mutex_exit(SD_MUTEX(un)); 16239 16240 return; 16241 } 16242 16243 not_successful: 16244 16245 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16246 /* 16247 * The following is based upon knowledge of the underlying transport 16248 * and its use of DMA resources. This code should be removed when 16249 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16250 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16251 * and sd_start_cmds(). 16252 * 16253 * Free any DMA resources associated with this command if there 16254 * is a chance it could be retried or enqueued for later retry. 16255 * If we keep the DMA binding then mpxio cannot reissue the 16256 * command on another path whenever a path failure occurs. 16257 * 16258 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16259 * causes the *entire* transfer to start over again from the 16260 * beginning of the request, even for PARTIAL chunks that 16261 * have already transferred successfully. 16262 * 16263 * This is only done for non-uscsi commands (and also skipped for the 16264 * driver's internal RQS command). Also just do this for Fibre Channel 16265 * devices as these are the only ones that support mpxio. 16266 */ 16267 if ((un->un_f_is_fibre == TRUE) && 16268 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16269 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16270 scsi_dmafree(pktp); 16271 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16272 } 16273 #endif 16274 16275 /* 16276 * The command did not successfully complete as requested so check 16277 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16278 * driver command that should not be retried so just return. If 16279 * FLAG_DIAGNOSE is not set the error will be processed below. 16280 */ 16281 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16282 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16283 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16284 /* 16285 * Issue a request sense if a check condition caused the error 16286 * (we handle the auto request sense case above), otherwise 16287 * just fail the command. 16288 */ 16289 if ((pktp->pkt_reason == CMD_CMPLT) && 16290 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16291 sd_send_request_sense_command(un, bp, pktp); 16292 } else { 16293 sd_return_failed_command(un, bp, EIO); 16294 } 16295 goto exit; 16296 } 16297 16298 /* 16299 * The command did not successfully complete as requested so process 16300 * the error, retry, and/or attempt recovery. 16301 */ 16302 switch (pktp->pkt_reason) { 16303 case CMD_CMPLT: 16304 switch (SD_GET_PKT_STATUS(pktp)) { 16305 case STATUS_GOOD: 16306 /* 16307 * The command completed successfully with a non-zero 16308 * residual 16309 */ 16310 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16311 "sdintr: STATUS_GOOD \n"); 16312 sd_pkt_status_good(un, bp, xp, pktp); 16313 break; 16314 16315 case STATUS_CHECK: 16316 case STATUS_TERMINATED: 16317 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16318 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16319 sd_pkt_status_check_condition(un, bp, xp, pktp); 16320 break; 16321 16322 case STATUS_BUSY: 16323 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16324 "sdintr: STATUS_BUSY\n"); 16325 sd_pkt_status_busy(un, bp, xp, pktp); 16326 break; 16327 16328 case STATUS_RESERVATION_CONFLICT: 16329 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16330 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16331 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16332 break; 16333 16334 case STATUS_QFULL: 16335 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16336 "sdintr: STATUS_QFULL\n"); 16337 sd_pkt_status_qfull(un, bp, xp, pktp); 16338 break; 16339 16340 case STATUS_MET: 16341 case STATUS_INTERMEDIATE: 16342 case STATUS_SCSI2: 16343 case STATUS_INTERMEDIATE_MET: 16344 case STATUS_ACA_ACTIVE: 16345 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16346 "Unexpected SCSI status received: 0x%x\n", 16347 SD_GET_PKT_STATUS(pktp)); 16348 sd_return_failed_command(un, bp, EIO); 16349 break; 16350 16351 default: 16352 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16353 "Invalid SCSI status received: 0x%x\n", 16354 SD_GET_PKT_STATUS(pktp)); 16355 sd_return_failed_command(un, bp, EIO); 16356 break; 16357 16358 } 16359 break; 16360 16361 case CMD_INCOMPLETE: 16362 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16363 "sdintr: CMD_INCOMPLETE\n"); 16364 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16365 break; 16366 case CMD_TRAN_ERR: 16367 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16368 "sdintr: CMD_TRAN_ERR\n"); 16369 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16370 break; 16371 case CMD_RESET: 16372 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16373 "sdintr: CMD_RESET \n"); 16374 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16375 break; 16376 case CMD_ABORTED: 16377 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16378 "sdintr: CMD_ABORTED \n"); 16379 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16380 break; 16381 case CMD_TIMEOUT: 16382 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16383 "sdintr: CMD_TIMEOUT\n"); 16384 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16385 break; 16386 case CMD_UNX_BUS_FREE: 16387 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16388 "sdintr: CMD_UNX_BUS_FREE \n"); 16389 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16390 break; 16391 case CMD_TAG_REJECT: 16392 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16393 "sdintr: CMD_TAG_REJECT\n"); 16394 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16395 break; 16396 default: 16397 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16398 "sdintr: default\n"); 16399 sd_pkt_reason_default(un, bp, xp, pktp); 16400 break; 16401 } 16402 16403 exit: 16404 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16405 16406 /* Decrement counter to indicate that the callback routine is done. */ 16407 un->un_in_callback--; 16408 ASSERT(un->un_in_callback >= 0); 16409 16410 /* 16411 * At this point, the pkt has been dispatched, ie, it is either 16412 * being re-tried or has been returned to its caller and should 16413 * not be referenced. 16414 */ 16415 16416 mutex_exit(SD_MUTEX(un)); 16417 } 16418 16419 16420 /* 16421 * Function: sd_print_incomplete_msg 16422 * 16423 * Description: Prints the error message for a CMD_INCOMPLETE error. 16424 * 16425 * Arguments: un - ptr to associated softstate for the device. 16426 * bp - ptr to the buf(9S) for the command. 16427 * arg - message string ptr 16428 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16429 * or SD_NO_RETRY_ISSUED. 16430 * 16431 * Context: May be called under interrupt context 16432 */ 16433 16434 static void 16435 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16436 { 16437 struct scsi_pkt *pktp; 16438 char *msgp; 16439 char *cmdp = arg; 16440 16441 ASSERT(un != NULL); 16442 ASSERT(mutex_owned(SD_MUTEX(un))); 16443 ASSERT(bp != NULL); 16444 ASSERT(arg != NULL); 16445 pktp = SD_GET_PKTP(bp); 16446 ASSERT(pktp != NULL); 16447 16448 switch (code) { 16449 case SD_DELAYED_RETRY_ISSUED: 16450 case SD_IMMEDIATE_RETRY_ISSUED: 16451 msgp = "retrying"; 16452 break; 16453 case SD_NO_RETRY_ISSUED: 16454 default: 16455 msgp = "giving up"; 16456 break; 16457 } 16458 16459 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16460 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16461 "incomplete %s- %s\n", cmdp, msgp); 16462 } 16463 } 16464 16465 16466 16467 /* 16468 * Function: sd_pkt_status_good 16469 * 16470 * Description: Processing for a STATUS_GOOD code in pkt_status. 16471 * 16472 * Context: May be called under interrupt context 16473 */ 16474 16475 static void 16476 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16477 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16478 { 16479 char *cmdp; 16480 16481 ASSERT(un != NULL); 16482 ASSERT(mutex_owned(SD_MUTEX(un))); 16483 ASSERT(bp != NULL); 16484 ASSERT(xp != NULL); 16485 ASSERT(pktp != NULL); 16486 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16487 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16488 ASSERT(pktp->pkt_resid != 0); 16489 16490 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16491 16492 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16493 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16494 case SCMD_READ: 16495 cmdp = "read"; 16496 break; 16497 case SCMD_WRITE: 16498 cmdp = "write"; 16499 break; 16500 default: 16501 SD_UPDATE_B_RESID(bp, pktp); 16502 sd_return_command(un, bp); 16503 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16504 return; 16505 } 16506 16507 /* 16508 * See if we can retry the read/write, preferrably immediately. 16509 * If retries are exhaused, then sd_retry_command() will update 16510 * the b_resid count. 16511 */ 16512 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16513 cmdp, EIO, (clock_t)0, NULL); 16514 16515 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16516 } 16517 16518 16519 16520 16521 16522 /* 16523 * Function: sd_handle_request_sense 16524 * 16525 * Description: Processing for non-auto Request Sense command. 16526 * 16527 * Arguments: un - ptr to associated softstate 16528 * sense_bp - ptr to buf(9S) for the RQS command 16529 * sense_xp - ptr to the sd_xbuf for the RQS command 16530 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16531 * 16532 * Context: May be called under interrupt context 16533 */ 16534 16535 static void 16536 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16537 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16538 { 16539 struct buf *cmd_bp; /* buf for the original command */ 16540 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16541 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16542 16543 ASSERT(un != NULL); 16544 ASSERT(mutex_owned(SD_MUTEX(un))); 16545 ASSERT(sense_bp != NULL); 16546 ASSERT(sense_xp != NULL); 16547 ASSERT(sense_pktp != NULL); 16548 16549 /* 16550 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16551 * RQS command and not the original command. 16552 */ 16553 ASSERT(sense_pktp == un->un_rqs_pktp); 16554 ASSERT(sense_bp == un->un_rqs_bp); 16555 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16556 (FLAG_SENSING | FLAG_HEAD)); 16557 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16558 FLAG_SENSING) == FLAG_SENSING); 16559 16560 /* These are the bp, xp, and pktp for the original command */ 16561 cmd_bp = sense_xp->xb_sense_bp; 16562 cmd_xp = SD_GET_XBUF(cmd_bp); 16563 cmd_pktp = SD_GET_PKTP(cmd_bp); 16564 16565 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16566 /* 16567 * The REQUEST SENSE command failed. Release the REQUEST 16568 * SENSE command for re-use, get back the bp for the original 16569 * command, and attempt to re-try the original command if 16570 * FLAG_DIAGNOSE is not set in the original packet. 16571 */ 16572 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16573 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16574 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16575 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16576 NULL, NULL, EIO, (clock_t)0, NULL); 16577 return; 16578 } 16579 } 16580 16581 /* 16582 * Save the relevant sense info into the xp for the original cmd. 16583 * 16584 * Note: if the request sense failed the state info will be zero 16585 * as set in sd_mark_rqs_busy() 16586 */ 16587 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16588 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16589 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16590 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16591 16592 /* 16593 * Free up the RQS command.... 16594 * NOTE: 16595 * Must do this BEFORE calling sd_validate_sense_data! 16596 * sd_validate_sense_data may return the original command in 16597 * which case the pkt will be freed and the flags can no 16598 * longer be touched. 16599 * SD_MUTEX is held through this process until the command 16600 * is dispatched based upon the sense data, so there are 16601 * no race conditions. 16602 */ 16603 (void) sd_mark_rqs_idle(un, sense_xp); 16604 16605 /* 16606 * For a retryable command see if we have valid sense data, if so then 16607 * turn it over to sd_decode_sense() to figure out the right course of 16608 * action. Just fail a non-retryable command. 16609 */ 16610 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16611 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16612 SD_SENSE_DATA_IS_VALID) { 16613 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16614 } 16615 } else { 16616 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16617 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16618 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16619 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16620 sd_return_failed_command(un, cmd_bp, EIO); 16621 } 16622 } 16623 16624 16625 16626 16627 /* 16628 * Function: sd_handle_auto_request_sense 16629 * 16630 * Description: Processing for auto-request sense information. 16631 * 16632 * Arguments: un - ptr to associated softstate 16633 * bp - ptr to buf(9S) for the command 16634 * xp - ptr to the sd_xbuf for the command 16635 * pktp - ptr to the scsi_pkt(9S) for the command 16636 * 16637 * Context: May be called under interrupt context 16638 */ 16639 16640 static void 16641 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16642 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16643 { 16644 struct scsi_arq_status *asp; 16645 16646 ASSERT(un != NULL); 16647 ASSERT(mutex_owned(SD_MUTEX(un))); 16648 ASSERT(bp != NULL); 16649 ASSERT(xp != NULL); 16650 ASSERT(pktp != NULL); 16651 ASSERT(pktp != un->un_rqs_pktp); 16652 ASSERT(bp != un->un_rqs_bp); 16653 16654 /* 16655 * For auto-request sense, we get a scsi_arq_status back from 16656 * the HBA, with the sense data in the sts_sensedata member. 16657 * The pkt_scbp of the packet points to this scsi_arq_status. 16658 */ 16659 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16660 16661 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16662 /* 16663 * The auto REQUEST SENSE failed; see if we can re-try 16664 * the original command. 16665 */ 16666 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16667 "auto request sense failed (reason=%s)\n", 16668 scsi_rname(asp->sts_rqpkt_reason)); 16669 16670 sd_reset_target(un, pktp); 16671 16672 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16673 NULL, NULL, EIO, (clock_t)0, NULL); 16674 return; 16675 } 16676 16677 /* Save the relevant sense info into the xp for the original cmd. */ 16678 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16679 xp->xb_sense_state = asp->sts_rqpkt_state; 16680 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16681 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16682 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16683 16684 /* 16685 * See if we have valid sense data, if so then turn it over to 16686 * sd_decode_sense() to figure out the right course of action. 16687 */ 16688 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16689 sd_decode_sense(un, bp, xp, pktp); 16690 } 16691 } 16692 16693 16694 /* 16695 * Function: sd_print_sense_failed_msg 16696 * 16697 * Description: Print log message when RQS has failed. 16698 * 16699 * Arguments: un - ptr to associated softstate 16700 * bp - ptr to buf(9S) for the command 16701 * arg - generic message string ptr 16702 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16703 * or SD_NO_RETRY_ISSUED 16704 * 16705 * Context: May be called from interrupt context 16706 */ 16707 16708 static void 16709 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16710 int code) 16711 { 16712 char *msgp = arg; 16713 16714 ASSERT(un != NULL); 16715 ASSERT(mutex_owned(SD_MUTEX(un))); 16716 ASSERT(bp != NULL); 16717 16718 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16719 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16720 } 16721 } 16722 16723 16724 /* 16725 * Function: sd_validate_sense_data 16726 * 16727 * Description: Check the given sense data for validity. 16728 * If the sense data is not valid, the command will 16729 * be either failed or retried! 16730 * 16731 * Return Code: SD_SENSE_DATA_IS_INVALID 16732 * SD_SENSE_DATA_IS_VALID 16733 * 16734 * Context: May be called from interrupt context 16735 */ 16736 16737 static int 16738 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16739 { 16740 struct scsi_extended_sense *esp; 16741 struct scsi_pkt *pktp; 16742 size_t actual_len; 16743 char *msgp = NULL; 16744 16745 ASSERT(un != NULL); 16746 ASSERT(mutex_owned(SD_MUTEX(un))); 16747 ASSERT(bp != NULL); 16748 ASSERT(bp != un->un_rqs_bp); 16749 ASSERT(xp != NULL); 16750 16751 pktp = SD_GET_PKTP(bp); 16752 ASSERT(pktp != NULL); 16753 16754 /* 16755 * Check the status of the RQS command (auto or manual). 16756 */ 16757 switch (xp->xb_sense_status & STATUS_MASK) { 16758 case STATUS_GOOD: 16759 break; 16760 16761 case STATUS_RESERVATION_CONFLICT: 16762 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16763 return (SD_SENSE_DATA_IS_INVALID); 16764 16765 case STATUS_BUSY: 16766 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16767 "Busy Status on REQUEST SENSE\n"); 16768 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16769 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16770 return (SD_SENSE_DATA_IS_INVALID); 16771 16772 case STATUS_QFULL: 16773 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16774 "QFULL Status on REQUEST SENSE\n"); 16775 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16776 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16777 return (SD_SENSE_DATA_IS_INVALID); 16778 16779 case STATUS_CHECK: 16780 case STATUS_TERMINATED: 16781 msgp = "Check Condition on REQUEST SENSE\n"; 16782 goto sense_failed; 16783 16784 default: 16785 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16786 goto sense_failed; 16787 } 16788 16789 /* 16790 * See if we got the minimum required amount of sense data. 16791 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16792 * or less. 16793 */ 16794 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 16795 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16796 (actual_len == 0)) { 16797 msgp = "Request Sense couldn't get sense data\n"; 16798 goto sense_failed; 16799 } 16800 16801 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16802 msgp = "Not enough sense information\n"; 16803 goto sense_failed; 16804 } 16805 16806 /* 16807 * We require the extended sense data 16808 */ 16809 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16810 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16811 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16812 static char tmp[8]; 16813 static char buf[148]; 16814 char *p = (char *)(xp->xb_sense_data); 16815 int i; 16816 16817 mutex_enter(&sd_sense_mutex); 16818 (void) strcpy(buf, "undecodable sense information:"); 16819 for (i = 0; i < actual_len; i++) { 16820 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16821 (void) strcpy(&buf[strlen(buf)], tmp); 16822 } 16823 i = strlen(buf); 16824 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16825 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 16826 mutex_exit(&sd_sense_mutex); 16827 } 16828 /* Note: Legacy behavior, fail the command with no retry */ 16829 sd_return_failed_command(un, bp, EIO); 16830 return (SD_SENSE_DATA_IS_INVALID); 16831 } 16832 16833 /* 16834 * Check that es_code is valid (es_class concatenated with es_code 16835 * make up the "response code" field. es_class will always be 7, so 16836 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16837 * format. 16838 */ 16839 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16840 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16841 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16842 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16843 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16844 goto sense_failed; 16845 } 16846 16847 return (SD_SENSE_DATA_IS_VALID); 16848 16849 sense_failed: 16850 /* 16851 * If the request sense failed (for whatever reason), attempt 16852 * to retry the original command. 16853 */ 16854 #if defined(__i386) || defined(__amd64) 16855 /* 16856 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 16857 * sddef.h for Sparc platform, and x86 uses 1 binary 16858 * for both SCSI/FC. 16859 * The SD_RETRY_DELAY value need to be adjusted here 16860 * when SD_RETRY_DELAY change in sddef.h 16861 */ 16862 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16863 sd_print_sense_failed_msg, msgp, EIO, 16864 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 16865 #else 16866 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16867 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 16868 #endif 16869 16870 return (SD_SENSE_DATA_IS_INVALID); 16871 } 16872 16873 16874 16875 /* 16876 * Function: sd_decode_sense 16877 * 16878 * Description: Take recovery action(s) when SCSI Sense Data is received. 16879 * 16880 * Context: Interrupt context. 16881 */ 16882 16883 static void 16884 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16885 struct scsi_pkt *pktp) 16886 { 16887 struct scsi_extended_sense *esp; 16888 struct scsi_descr_sense_hdr *sdsp; 16889 uint8_t asc, ascq, sense_key; 16890 16891 ASSERT(un != NULL); 16892 ASSERT(mutex_owned(SD_MUTEX(un))); 16893 ASSERT(bp != NULL); 16894 ASSERT(bp != un->un_rqs_bp); 16895 ASSERT(xp != NULL); 16896 ASSERT(pktp != NULL); 16897 16898 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16899 16900 switch (esp->es_code) { 16901 case CODE_FMT_DESCR_CURRENT: 16902 case CODE_FMT_DESCR_DEFERRED: 16903 sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data; 16904 sense_key = sdsp->ds_key; 16905 asc = sdsp->ds_add_code; 16906 ascq = sdsp->ds_qual_code; 16907 break; 16908 case CODE_FMT_VENDOR_SPECIFIC: 16909 case CODE_FMT_FIXED_CURRENT: 16910 case CODE_FMT_FIXED_DEFERRED: 16911 default: 16912 sense_key = esp->es_key; 16913 asc = esp->es_add_code; 16914 ascq = esp->es_qual_code; 16915 break; 16916 } 16917 16918 switch (sense_key) { 16919 case KEY_NO_SENSE: 16920 sd_sense_key_no_sense(un, bp, xp, pktp); 16921 break; 16922 case KEY_RECOVERABLE_ERROR: 16923 sd_sense_key_recoverable_error(un, asc, bp, xp, pktp); 16924 break; 16925 case KEY_NOT_READY: 16926 sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp); 16927 break; 16928 case KEY_MEDIUM_ERROR: 16929 case KEY_HARDWARE_ERROR: 16930 sd_sense_key_medium_or_hardware_error(un, 16931 sense_key, asc, bp, xp, pktp); 16932 break; 16933 case KEY_ILLEGAL_REQUEST: 16934 sd_sense_key_illegal_request(un, bp, xp, pktp); 16935 break; 16936 case KEY_UNIT_ATTENTION: 16937 sd_sense_key_unit_attention(un, asc, bp, xp, pktp); 16938 break; 16939 case KEY_WRITE_PROTECT: 16940 case KEY_VOLUME_OVERFLOW: 16941 case KEY_MISCOMPARE: 16942 sd_sense_key_fail_command(un, bp, xp, pktp); 16943 break; 16944 case KEY_BLANK_CHECK: 16945 sd_sense_key_blank_check(un, bp, xp, pktp); 16946 break; 16947 case KEY_ABORTED_COMMAND: 16948 sd_sense_key_aborted_command(un, bp, xp, pktp); 16949 break; 16950 case KEY_VENDOR_UNIQUE: 16951 case KEY_COPY_ABORTED: 16952 case KEY_EQUAL: 16953 case KEY_RESERVED: 16954 default: 16955 sd_sense_key_default(un, sense_key, bp, xp, pktp); 16956 break; 16957 } 16958 } 16959 16960 16961 /* 16962 * Function: sd_dump_memory 16963 * 16964 * Description: Debug logging routine to print the contents of a user provided 16965 * buffer. The output of the buffer is broken up into 256 byte 16966 * segments due to a size constraint of the scsi_log. 16967 * implementation. 16968 * 16969 * Arguments: un - ptr to softstate 16970 * comp - component mask 16971 * title - "title" string to preceed data when printed 16972 * data - ptr to data block to be printed 16973 * len - size of data block to be printed 16974 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 16975 * 16976 * Context: May be called from interrupt context 16977 */ 16978 16979 #define SD_DUMP_MEMORY_BUF_SIZE 256 16980 16981 static char *sd_dump_format_string[] = { 16982 " 0x%02x", 16983 " %c" 16984 }; 16985 16986 static void 16987 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 16988 int len, int fmt) 16989 { 16990 int i, j; 16991 int avail_count; 16992 int start_offset; 16993 int end_offset; 16994 size_t entry_len; 16995 char *bufp; 16996 char *local_buf; 16997 char *format_string; 16998 16999 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17000 17001 /* 17002 * In the debug version of the driver, this function is called from a 17003 * number of places which are NOPs in the release driver. 17004 * The debug driver therefore has additional methods of filtering 17005 * debug output. 17006 */ 17007 #ifdef SDDEBUG 17008 /* 17009 * In the debug version of the driver we can reduce the amount of debug 17010 * messages by setting sd_error_level to something other than 17011 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17012 * sd_component_mask. 17013 */ 17014 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17015 (sd_error_level != SCSI_ERR_ALL)) { 17016 return; 17017 } 17018 if (((sd_component_mask & comp) == 0) || 17019 (sd_error_level != SCSI_ERR_ALL)) { 17020 return; 17021 } 17022 #else 17023 if (sd_error_level != SCSI_ERR_ALL) { 17024 return; 17025 } 17026 #endif 17027 17028 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17029 bufp = local_buf; 17030 /* 17031 * Available length is the length of local_buf[], minus the 17032 * length of the title string, minus one for the ":", minus 17033 * one for the newline, minus one for the NULL terminator. 17034 * This gives the #bytes available for holding the printed 17035 * values from the given data buffer. 17036 */ 17037 if (fmt == SD_LOG_HEX) { 17038 format_string = sd_dump_format_string[0]; 17039 } else /* SD_LOG_CHAR */ { 17040 format_string = sd_dump_format_string[1]; 17041 } 17042 /* 17043 * Available count is the number of elements from the given 17044 * data buffer that we can fit into the available length. 17045 * This is based upon the size of the format string used. 17046 * Make one entry and find it's size. 17047 */ 17048 (void) sprintf(bufp, format_string, data[0]); 17049 entry_len = strlen(bufp); 17050 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17051 17052 j = 0; 17053 while (j < len) { 17054 bufp = local_buf; 17055 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17056 start_offset = j; 17057 17058 end_offset = start_offset + avail_count; 17059 17060 (void) sprintf(bufp, "%s:", title); 17061 bufp += strlen(bufp); 17062 for (i = start_offset; ((i < end_offset) && (j < len)); 17063 i++, j++) { 17064 (void) sprintf(bufp, format_string, data[i]); 17065 bufp += entry_len; 17066 } 17067 (void) sprintf(bufp, "\n"); 17068 17069 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17070 } 17071 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17072 } 17073 17074 /* 17075 * Function: sd_print_sense_msg 17076 * 17077 * Description: Log a message based upon the given sense data. 17078 * 17079 * Arguments: un - ptr to associated softstate 17080 * bp - ptr to buf(9S) for the command 17081 * arg - ptr to associate sd_sense_info struct 17082 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17083 * or SD_NO_RETRY_ISSUED 17084 * 17085 * Context: May be called from interrupt context 17086 */ 17087 17088 static void 17089 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17090 { 17091 struct sd_xbuf *xp; 17092 struct scsi_pkt *pktp; 17093 struct scsi_extended_sense *sensep; 17094 daddr_t request_blkno; 17095 diskaddr_t err_blkno; 17096 int severity; 17097 int pfa_flag; 17098 int fixed_format = TRUE; 17099 extern struct scsi_key_strings scsi_cmds[]; 17100 17101 ASSERT(un != NULL); 17102 ASSERT(mutex_owned(SD_MUTEX(un))); 17103 ASSERT(bp != NULL); 17104 xp = SD_GET_XBUF(bp); 17105 ASSERT(xp != NULL); 17106 pktp = SD_GET_PKTP(bp); 17107 ASSERT(pktp != NULL); 17108 ASSERT(arg != NULL); 17109 17110 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17111 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17112 17113 if ((code == SD_DELAYED_RETRY_ISSUED) || 17114 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17115 severity = SCSI_ERR_RETRYABLE; 17116 } 17117 17118 /* Use absolute block number for the request block number */ 17119 request_blkno = xp->xb_blkno; 17120 17121 /* 17122 * Now try to get the error block number from the sense data 17123 */ 17124 sensep = (struct scsi_extended_sense *)xp->xb_sense_data; 17125 switch (sensep->es_code) { 17126 case CODE_FMT_DESCR_CURRENT: 17127 case CODE_FMT_DESCR_DEFERRED: 17128 err_blkno = 17129 sd_extract_sense_info_descr( 17130 (struct scsi_descr_sense_hdr *)sensep); 17131 fixed_format = FALSE; 17132 break; 17133 case CODE_FMT_FIXED_CURRENT: 17134 case CODE_FMT_FIXED_DEFERRED: 17135 case CODE_FMT_VENDOR_SPECIFIC: 17136 default: 17137 /* 17138 * With the es_valid bit set, we assume that the error 17139 * blkno is in the sense data. Also, if xp->xb_blkno is 17140 * greater than 0xffffffff then the target *should* have used 17141 * a descriptor sense format (or it shouldn't have set 17142 * the es_valid bit), and we may as well ignore the 17143 * 32-bit value. 17144 */ 17145 if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) { 17146 err_blkno = (diskaddr_t) 17147 ((sensep->es_info_1 << 24) | 17148 (sensep->es_info_2 << 16) | 17149 (sensep->es_info_3 << 8) | 17150 (sensep->es_info_4)); 17151 } else { 17152 err_blkno = (diskaddr_t)-1; 17153 } 17154 break; 17155 } 17156 17157 if (err_blkno == (diskaddr_t)-1) { 17158 /* 17159 * Without the es_valid bit set (for fixed format) or an 17160 * information descriptor (for descriptor format) we cannot 17161 * be certain of the error blkno, so just use the 17162 * request_blkno. 17163 */ 17164 err_blkno = (diskaddr_t)request_blkno; 17165 } else { 17166 /* 17167 * We retrieved the error block number from the information 17168 * portion of the sense data. 17169 * 17170 * For USCSI commands we are better off using the error 17171 * block no. as the requested block no. (This is the best 17172 * we can estimate.) 17173 */ 17174 if ((SD_IS_BUFIO(xp) == FALSE) && 17175 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17176 request_blkno = err_blkno; 17177 } 17178 } 17179 17180 /* 17181 * The following will log the buffer contents for the release driver 17182 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17183 * level is set to verbose. 17184 */ 17185 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17186 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17187 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17188 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17189 17190 if (pfa_flag == FALSE) { 17191 /* This is normally only set for USCSI */ 17192 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17193 return; 17194 } 17195 17196 if ((SD_IS_BUFIO(xp) == TRUE) && 17197 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17198 (severity < sd_error_level))) { 17199 return; 17200 } 17201 } 17202 17203 /* 17204 * If the data is fixed format then check for Sonoma Failover, 17205 * and keep a count of how many failed I/O's. We should not have 17206 * to worry about Sonoma returning descriptor format sense data, 17207 * and asc/ascq are in a different location in descriptor format. 17208 */ 17209 if (fixed_format && 17210 (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) && 17211 (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) { 17212 un->un_sonoma_failure_count++; 17213 if (un->un_sonoma_failure_count > 1) { 17214 return; 17215 } 17216 } 17217 17218 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17219 request_blkno, err_blkno, scsi_cmds, sensep, 17220 un->un_additional_codes, NULL); 17221 } 17222 17223 /* 17224 * Function: sd_extract_sense_info_descr 17225 * 17226 * Description: Retrieve "information" field from descriptor format 17227 * sense data. Iterates through each sense descriptor 17228 * looking for the information descriptor and returns 17229 * the information field from that descriptor. 17230 * 17231 * Context: May be called from interrupt context 17232 */ 17233 17234 static diskaddr_t 17235 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp) 17236 { 17237 diskaddr_t result; 17238 uint8_t *descr_offset; 17239 int valid_sense_length; 17240 struct scsi_information_sense_descr *isd; 17241 17242 /* 17243 * Initialize result to -1 indicating there is no information 17244 * descriptor 17245 */ 17246 result = (diskaddr_t)-1; 17247 17248 /* 17249 * The first descriptor will immediately follow the header 17250 */ 17251 descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */ 17252 17253 /* 17254 * Calculate the amount of valid sense data 17255 */ 17256 valid_sense_length = 17257 min((sizeof (struct scsi_descr_sense_hdr) + 17258 sdsp->ds_addl_sense_length), 17259 SENSE_LENGTH); 17260 17261 /* 17262 * Iterate through the list of descriptors, stopping when we 17263 * run out of sense data 17264 */ 17265 while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <= 17266 (uint8_t *)sdsp + valid_sense_length) { 17267 /* 17268 * Check if this is an information descriptor. We can 17269 * use the scsi_information_sense_descr structure as a 17270 * template sense the first two fields are always the 17271 * same 17272 */ 17273 isd = (struct scsi_information_sense_descr *)descr_offset; 17274 if (isd->isd_descr_type == DESCR_INFORMATION) { 17275 /* 17276 * Found an information descriptor. Copy the 17277 * information field. There will only be one 17278 * information descriptor so we can stop looking. 17279 */ 17280 result = 17281 (((diskaddr_t)isd->isd_information[0] << 56) | 17282 ((diskaddr_t)isd->isd_information[1] << 48) | 17283 ((diskaddr_t)isd->isd_information[2] << 40) | 17284 ((diskaddr_t)isd->isd_information[3] << 32) | 17285 ((diskaddr_t)isd->isd_information[4] << 24) | 17286 ((diskaddr_t)isd->isd_information[5] << 16) | 17287 ((diskaddr_t)isd->isd_information[6] << 8) | 17288 ((diskaddr_t)isd->isd_information[7])); 17289 break; 17290 } 17291 17292 /* 17293 * Get pointer to the next descriptor. The "additional 17294 * length" field holds the length of the descriptor except 17295 * for the "type" and "additional length" fields, so 17296 * we need to add 2 to get the total length. 17297 */ 17298 descr_offset += (isd->isd_addl_length + 2); 17299 } 17300 17301 return (result); 17302 } 17303 17304 /* 17305 * Function: sd_sense_key_no_sense 17306 * 17307 * Description: Recovery action when sense data was not received. 17308 * 17309 * Context: May be called from interrupt context 17310 */ 17311 17312 static void 17313 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17314 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17315 { 17316 struct sd_sense_info si; 17317 17318 ASSERT(un != NULL); 17319 ASSERT(mutex_owned(SD_MUTEX(un))); 17320 ASSERT(bp != NULL); 17321 ASSERT(xp != NULL); 17322 ASSERT(pktp != NULL); 17323 17324 si.ssi_severity = SCSI_ERR_FATAL; 17325 si.ssi_pfa_flag = FALSE; 17326 17327 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17328 17329 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17330 &si, EIO, (clock_t)0, NULL); 17331 } 17332 17333 17334 /* 17335 * Function: sd_sense_key_recoverable_error 17336 * 17337 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17338 * 17339 * Context: May be called from interrupt context 17340 */ 17341 17342 static void 17343 sd_sense_key_recoverable_error(struct sd_lun *un, 17344 uint8_t asc, 17345 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17346 { 17347 struct sd_sense_info si; 17348 17349 ASSERT(un != NULL); 17350 ASSERT(mutex_owned(SD_MUTEX(un))); 17351 ASSERT(bp != NULL); 17352 ASSERT(xp != NULL); 17353 ASSERT(pktp != NULL); 17354 17355 /* 17356 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17357 */ 17358 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17359 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17360 si.ssi_severity = SCSI_ERR_INFO; 17361 si.ssi_pfa_flag = TRUE; 17362 } else { 17363 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17364 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17365 si.ssi_severity = SCSI_ERR_RECOVERED; 17366 si.ssi_pfa_flag = FALSE; 17367 } 17368 17369 if (pktp->pkt_resid == 0) { 17370 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17371 sd_return_command(un, bp); 17372 return; 17373 } 17374 17375 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17376 &si, EIO, (clock_t)0, NULL); 17377 } 17378 17379 17380 17381 17382 /* 17383 * Function: sd_sense_key_not_ready 17384 * 17385 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17386 * 17387 * Context: May be called from interrupt context 17388 */ 17389 17390 static void 17391 sd_sense_key_not_ready(struct sd_lun *un, 17392 uint8_t asc, uint8_t ascq, 17393 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17394 { 17395 struct sd_sense_info si; 17396 17397 ASSERT(un != NULL); 17398 ASSERT(mutex_owned(SD_MUTEX(un))); 17399 ASSERT(bp != NULL); 17400 ASSERT(xp != NULL); 17401 ASSERT(pktp != NULL); 17402 17403 si.ssi_severity = SCSI_ERR_FATAL; 17404 si.ssi_pfa_flag = FALSE; 17405 17406 /* 17407 * Update error stats after first NOT READY error. Disks may have 17408 * been powered down and may need to be restarted. For CDROMs, 17409 * report NOT READY errors only if media is present. 17410 */ 17411 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17412 (xp->xb_retry_count > 0)) { 17413 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17414 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17415 } 17416 17417 /* 17418 * Just fail if the "not ready" retry limit has been reached. 17419 */ 17420 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17421 /* Special check for error message printing for removables. */ 17422 if (un->un_f_has_removable_media && (asc == 0x04) && 17423 (ascq >= 0x04)) { 17424 si.ssi_severity = SCSI_ERR_ALL; 17425 } 17426 goto fail_command; 17427 } 17428 17429 /* 17430 * Check the ASC and ASCQ in the sense data as needed, to determine 17431 * what to do. 17432 */ 17433 switch (asc) { 17434 case 0x04: /* LOGICAL UNIT NOT READY */ 17435 /* 17436 * disk drives that don't spin up result in a very long delay 17437 * in format without warning messages. We will log a message 17438 * if the error level is set to verbose. 17439 */ 17440 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17441 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17442 "logical unit not ready, resetting disk\n"); 17443 } 17444 17445 /* 17446 * There are different requirements for CDROMs and disks for 17447 * the number of retries. If a CD-ROM is giving this, it is 17448 * probably reading TOC and is in the process of getting 17449 * ready, so we should keep on trying for a long time to make 17450 * sure that all types of media are taken in account (for 17451 * some media the drive takes a long time to read TOC). For 17452 * disks we do not want to retry this too many times as this 17453 * can cause a long hang in format when the drive refuses to 17454 * spin up (a very common failure). 17455 */ 17456 switch (ascq) { 17457 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17458 /* 17459 * Disk drives frequently refuse to spin up which 17460 * results in a very long hang in format without 17461 * warning messages. 17462 * 17463 * Note: This code preserves the legacy behavior of 17464 * comparing xb_retry_count against zero for fibre 17465 * channel targets instead of comparing against the 17466 * un_reset_retry_count value. The reason for this 17467 * discrepancy has been so utterly lost beneath the 17468 * Sands of Time that even Indiana Jones could not 17469 * find it. 17470 */ 17471 if (un->un_f_is_fibre == TRUE) { 17472 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17473 (xp->xb_retry_count > 0)) && 17474 (un->un_startstop_timeid == NULL)) { 17475 scsi_log(SD_DEVINFO(un), sd_label, 17476 CE_WARN, "logical unit not ready, " 17477 "resetting disk\n"); 17478 sd_reset_target(un, pktp); 17479 } 17480 } else { 17481 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17482 (xp->xb_retry_count > 17483 un->un_reset_retry_count)) && 17484 (un->un_startstop_timeid == NULL)) { 17485 scsi_log(SD_DEVINFO(un), sd_label, 17486 CE_WARN, "logical unit not ready, " 17487 "resetting disk\n"); 17488 sd_reset_target(un, pktp); 17489 } 17490 } 17491 break; 17492 17493 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17494 /* 17495 * If the target is in the process of becoming 17496 * ready, just proceed with the retry. This can 17497 * happen with CD-ROMs that take a long time to 17498 * read TOC after a power cycle or reset. 17499 */ 17500 goto do_retry; 17501 17502 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17503 break; 17504 17505 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17506 /* 17507 * Retries cannot help here so just fail right away. 17508 */ 17509 goto fail_command; 17510 17511 case 0x88: 17512 /* 17513 * Vendor-unique code for T3/T4: it indicates a 17514 * path problem in a mutipathed config, but as far as 17515 * the target driver is concerned it equates to a fatal 17516 * error, so we should just fail the command right away 17517 * (without printing anything to the console). If this 17518 * is not a T3/T4, fall thru to the default recovery 17519 * action. 17520 * T3/T4 is FC only, don't need to check is_fibre 17521 */ 17522 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17523 sd_return_failed_command(un, bp, EIO); 17524 return; 17525 } 17526 /* FALLTHRU */ 17527 17528 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17529 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17530 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17531 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17532 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17533 default: /* Possible future codes in SCSI spec? */ 17534 /* 17535 * For removable-media devices, do not retry if 17536 * ASCQ > 2 as these result mostly from USCSI commands 17537 * on MMC devices issued to check status of an 17538 * operation initiated in immediate mode. Also for 17539 * ASCQ >= 4 do not print console messages as these 17540 * mainly represent a user-initiated operation 17541 * instead of a system failure. 17542 */ 17543 if (un->un_f_has_removable_media) { 17544 si.ssi_severity = SCSI_ERR_ALL; 17545 goto fail_command; 17546 } 17547 break; 17548 } 17549 17550 /* 17551 * As part of our recovery attempt for the NOT READY 17552 * condition, we issue a START STOP UNIT command. However 17553 * we want to wait for a short delay before attempting this 17554 * as there may still be more commands coming back from the 17555 * target with the check condition. To do this we use 17556 * timeout(9F) to call sd_start_stop_unit_callback() after 17557 * the delay interval expires. (sd_start_stop_unit_callback() 17558 * dispatches sd_start_stop_unit_task(), which will issue 17559 * the actual START STOP UNIT command. The delay interval 17560 * is one-half of the delay that we will use to retry the 17561 * command that generated the NOT READY condition. 17562 * 17563 * Note that we could just dispatch sd_start_stop_unit_task() 17564 * from here and allow it to sleep for the delay interval, 17565 * but then we would be tying up the taskq thread 17566 * uncesessarily for the duration of the delay. 17567 * 17568 * Do not issue the START STOP UNIT if the current command 17569 * is already a START STOP UNIT. 17570 */ 17571 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17572 break; 17573 } 17574 17575 /* 17576 * Do not schedule the timeout if one is already pending. 17577 */ 17578 if (un->un_startstop_timeid != NULL) { 17579 SD_INFO(SD_LOG_ERROR, un, 17580 "sd_sense_key_not_ready: restart already issued to" 17581 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17582 ddi_get_instance(SD_DEVINFO(un))); 17583 break; 17584 } 17585 17586 /* 17587 * Schedule the START STOP UNIT command, then queue the command 17588 * for a retry. 17589 * 17590 * Note: A timeout is not scheduled for this retry because we 17591 * want the retry to be serial with the START_STOP_UNIT. The 17592 * retry will be started when the START_STOP_UNIT is completed 17593 * in sd_start_stop_unit_task. 17594 */ 17595 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17596 un, SD_BSY_TIMEOUT / 2); 17597 xp->xb_retry_count++; 17598 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17599 return; 17600 17601 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17602 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17603 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17604 "unit does not respond to selection\n"); 17605 } 17606 break; 17607 17608 case 0x3A: /* MEDIUM NOT PRESENT */ 17609 if (sd_error_level >= SCSI_ERR_FATAL) { 17610 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17611 "Caddy not inserted in drive\n"); 17612 } 17613 17614 sr_ejected(un); 17615 un->un_mediastate = DKIO_EJECTED; 17616 /* The state has changed, inform the media watch routines */ 17617 cv_broadcast(&un->un_state_cv); 17618 /* Just fail if no media is present in the drive. */ 17619 goto fail_command; 17620 17621 default: 17622 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17623 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17624 "Unit not Ready. Additional sense code 0x%x\n", 17625 asc); 17626 } 17627 break; 17628 } 17629 17630 do_retry: 17631 17632 /* 17633 * Retry the command, as some targets may report NOT READY for 17634 * several seconds after being reset. 17635 */ 17636 xp->xb_retry_count++; 17637 si.ssi_severity = SCSI_ERR_RETRYABLE; 17638 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17639 &si, EIO, SD_BSY_TIMEOUT, NULL); 17640 17641 return; 17642 17643 fail_command: 17644 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17645 sd_return_failed_command(un, bp, EIO); 17646 } 17647 17648 17649 17650 /* 17651 * Function: sd_sense_key_medium_or_hardware_error 17652 * 17653 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17654 * sense key. 17655 * 17656 * Context: May be called from interrupt context 17657 */ 17658 17659 static void 17660 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17661 int sense_key, uint8_t asc, 17662 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17663 { 17664 struct sd_sense_info si; 17665 17666 ASSERT(un != NULL); 17667 ASSERT(mutex_owned(SD_MUTEX(un))); 17668 ASSERT(bp != NULL); 17669 ASSERT(xp != NULL); 17670 ASSERT(pktp != NULL); 17671 17672 si.ssi_severity = SCSI_ERR_FATAL; 17673 si.ssi_pfa_flag = FALSE; 17674 17675 if (sense_key == KEY_MEDIUM_ERROR) { 17676 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17677 } 17678 17679 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17680 17681 if ((un->un_reset_retry_count != 0) && 17682 (xp->xb_retry_count == un->un_reset_retry_count)) { 17683 mutex_exit(SD_MUTEX(un)); 17684 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17685 if (un->un_f_allow_bus_device_reset == TRUE) { 17686 17687 boolean_t try_resetting_target = B_TRUE; 17688 17689 /* 17690 * We need to be able to handle specific ASC when we are 17691 * handling a KEY_HARDWARE_ERROR. In particular 17692 * taking the default action of resetting the target may 17693 * not be the appropriate way to attempt recovery. 17694 * Resetting a target because of a single LUN failure 17695 * victimizes all LUNs on that target. 17696 * 17697 * This is true for the LSI arrays, if an LSI 17698 * array controller returns an ASC of 0x84 (LUN Dead) we 17699 * should trust it. 17700 */ 17701 17702 if (sense_key == KEY_HARDWARE_ERROR) { 17703 switch (asc) { 17704 case 0x84: 17705 if (SD_IS_LSI(un)) { 17706 try_resetting_target = B_FALSE; 17707 } 17708 break; 17709 default: 17710 break; 17711 } 17712 } 17713 17714 if (try_resetting_target == B_TRUE) { 17715 int reset_retval = 0; 17716 if (un->un_f_lun_reset_enabled == TRUE) { 17717 SD_TRACE(SD_LOG_IO_CORE, un, 17718 "sd_sense_key_medium_or_hardware_" 17719 "error: issuing RESET_LUN\n"); 17720 reset_retval = 17721 scsi_reset(SD_ADDRESS(un), 17722 RESET_LUN); 17723 } 17724 if (reset_retval == 0) { 17725 SD_TRACE(SD_LOG_IO_CORE, un, 17726 "sd_sense_key_medium_or_hardware_" 17727 "error: issuing RESET_TARGET\n"); 17728 (void) scsi_reset(SD_ADDRESS(un), 17729 RESET_TARGET); 17730 } 17731 } 17732 } 17733 mutex_enter(SD_MUTEX(un)); 17734 } 17735 17736 /* 17737 * This really ought to be a fatal error, but we will retry anyway 17738 * as some drives report this as a spurious error. 17739 */ 17740 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17741 &si, EIO, (clock_t)0, NULL); 17742 } 17743 17744 17745 17746 /* 17747 * Function: sd_sense_key_illegal_request 17748 * 17749 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17750 * 17751 * Context: May be called from interrupt context 17752 */ 17753 17754 static void 17755 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17756 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17757 { 17758 struct sd_sense_info si; 17759 17760 ASSERT(un != NULL); 17761 ASSERT(mutex_owned(SD_MUTEX(un))); 17762 ASSERT(bp != NULL); 17763 ASSERT(xp != NULL); 17764 ASSERT(pktp != NULL); 17765 17766 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17767 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17768 17769 si.ssi_severity = SCSI_ERR_INFO; 17770 si.ssi_pfa_flag = FALSE; 17771 17772 /* Pointless to retry if the target thinks it's an illegal request */ 17773 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17774 sd_return_failed_command(un, bp, EIO); 17775 } 17776 17777 17778 17779 17780 /* 17781 * Function: sd_sense_key_unit_attention 17782 * 17783 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17784 * 17785 * Context: May be called from interrupt context 17786 */ 17787 17788 static void 17789 sd_sense_key_unit_attention(struct sd_lun *un, 17790 uint8_t asc, 17791 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17792 { 17793 /* 17794 * For UNIT ATTENTION we allow retries for one minute. Devices 17795 * like Sonoma can return UNIT ATTENTION close to a minute 17796 * under certain conditions. 17797 */ 17798 int retry_check_flag = SD_RETRIES_UA; 17799 boolean_t kstat_updated = B_FALSE; 17800 struct sd_sense_info si; 17801 17802 ASSERT(un != NULL); 17803 ASSERT(mutex_owned(SD_MUTEX(un))); 17804 ASSERT(bp != NULL); 17805 ASSERT(xp != NULL); 17806 ASSERT(pktp != NULL); 17807 17808 si.ssi_severity = SCSI_ERR_INFO; 17809 si.ssi_pfa_flag = FALSE; 17810 17811 17812 switch (asc) { 17813 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17814 if (sd_report_pfa != 0) { 17815 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17816 si.ssi_pfa_flag = TRUE; 17817 retry_check_flag = SD_RETRIES_STANDARD; 17818 goto do_retry; 17819 } 17820 break; 17821 17822 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17823 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17824 un->un_resvd_status |= 17825 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17826 } 17827 /* FALLTHRU */ 17828 17829 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17830 if (!un->un_f_has_removable_media) { 17831 break; 17832 } 17833 17834 /* 17835 * When we get a unit attention from a removable-media device, 17836 * it may be in a state that will take a long time to recover 17837 * (e.g., from a reset). Since we are executing in interrupt 17838 * context here, we cannot wait around for the device to come 17839 * back. So hand this command off to sd_media_change_task() 17840 * for deferred processing under taskq thread context. (Note 17841 * that the command still may be failed if a problem is 17842 * encountered at a later time.) 17843 */ 17844 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17845 KM_NOSLEEP) == 0) { 17846 /* 17847 * Cannot dispatch the request so fail the command. 17848 */ 17849 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17850 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17851 si.ssi_severity = SCSI_ERR_FATAL; 17852 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17853 sd_return_failed_command(un, bp, EIO); 17854 } 17855 17856 /* 17857 * If failed to dispatch sd_media_change_task(), we already 17858 * updated kstat. If succeed to dispatch sd_media_change_task(), 17859 * we should update kstat later if it encounters an error. So, 17860 * we update kstat_updated flag here. 17861 */ 17862 kstat_updated = B_TRUE; 17863 17864 /* 17865 * Either the command has been successfully dispatched to a 17866 * task Q for retrying, or the dispatch failed. In either case 17867 * do NOT retry again by calling sd_retry_command. This sets up 17868 * two retries of the same command and when one completes and 17869 * frees the resources the other will access freed memory, 17870 * a bad thing. 17871 */ 17872 return; 17873 17874 default: 17875 break; 17876 } 17877 17878 /* 17879 * Update kstat if we haven't done that. 17880 */ 17881 if (!kstat_updated) { 17882 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17883 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17884 } 17885 17886 do_retry: 17887 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17888 EIO, SD_UA_RETRY_DELAY, NULL); 17889 } 17890 17891 17892 17893 /* 17894 * Function: sd_sense_key_fail_command 17895 * 17896 * Description: Use to fail a command when we don't like the sense key that 17897 * was returned. 17898 * 17899 * Context: May be called from interrupt context 17900 */ 17901 17902 static void 17903 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17904 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17905 { 17906 struct sd_sense_info si; 17907 17908 ASSERT(un != NULL); 17909 ASSERT(mutex_owned(SD_MUTEX(un))); 17910 ASSERT(bp != NULL); 17911 ASSERT(xp != NULL); 17912 ASSERT(pktp != NULL); 17913 17914 si.ssi_severity = SCSI_ERR_FATAL; 17915 si.ssi_pfa_flag = FALSE; 17916 17917 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17918 sd_return_failed_command(un, bp, EIO); 17919 } 17920 17921 17922 17923 /* 17924 * Function: sd_sense_key_blank_check 17925 * 17926 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17927 * Has no monetary connotation. 17928 * 17929 * Context: May be called from interrupt context 17930 */ 17931 17932 static void 17933 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17934 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17935 { 17936 struct sd_sense_info si; 17937 17938 ASSERT(un != NULL); 17939 ASSERT(mutex_owned(SD_MUTEX(un))); 17940 ASSERT(bp != NULL); 17941 ASSERT(xp != NULL); 17942 ASSERT(pktp != NULL); 17943 17944 /* 17945 * Blank check is not fatal for removable devices, therefore 17946 * it does not require a console message. 17947 */ 17948 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 17949 SCSI_ERR_FATAL; 17950 si.ssi_pfa_flag = FALSE; 17951 17952 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17953 sd_return_failed_command(un, bp, EIO); 17954 } 17955 17956 17957 17958 17959 /* 17960 * Function: sd_sense_key_aborted_command 17961 * 17962 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 17963 * 17964 * Context: May be called from interrupt context 17965 */ 17966 17967 static void 17968 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 17969 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17970 { 17971 struct sd_sense_info si; 17972 17973 ASSERT(un != NULL); 17974 ASSERT(mutex_owned(SD_MUTEX(un))); 17975 ASSERT(bp != NULL); 17976 ASSERT(xp != NULL); 17977 ASSERT(pktp != NULL); 17978 17979 si.ssi_severity = SCSI_ERR_FATAL; 17980 si.ssi_pfa_flag = FALSE; 17981 17982 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17983 17984 /* 17985 * This really ought to be a fatal error, but we will retry anyway 17986 * as some drives report this as a spurious error. 17987 */ 17988 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17989 &si, EIO, (clock_t)0, NULL); 17990 } 17991 17992 17993 17994 /* 17995 * Function: sd_sense_key_default 17996 * 17997 * Description: Default recovery action for several SCSI sense keys (basically 17998 * attempts a retry). 17999 * 18000 * Context: May be called from interrupt context 18001 */ 18002 18003 static void 18004 sd_sense_key_default(struct sd_lun *un, 18005 int sense_key, 18006 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18007 { 18008 struct sd_sense_info si; 18009 18010 ASSERT(un != NULL); 18011 ASSERT(mutex_owned(SD_MUTEX(un))); 18012 ASSERT(bp != NULL); 18013 ASSERT(xp != NULL); 18014 ASSERT(pktp != NULL); 18015 18016 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18017 18018 /* 18019 * Undecoded sense key. Attempt retries and hope that will fix 18020 * the problem. Otherwise, we're dead. 18021 */ 18022 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18023 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18024 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18025 } 18026 18027 si.ssi_severity = SCSI_ERR_FATAL; 18028 si.ssi_pfa_flag = FALSE; 18029 18030 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18031 &si, EIO, (clock_t)0, NULL); 18032 } 18033 18034 18035 18036 /* 18037 * Function: sd_print_retry_msg 18038 * 18039 * Description: Print a message indicating the retry action being taken. 18040 * 18041 * Arguments: un - ptr to associated softstate 18042 * bp - ptr to buf(9S) for the command 18043 * arg - not used. 18044 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18045 * or SD_NO_RETRY_ISSUED 18046 * 18047 * Context: May be called from interrupt context 18048 */ 18049 /* ARGSUSED */ 18050 static void 18051 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18052 { 18053 struct sd_xbuf *xp; 18054 struct scsi_pkt *pktp; 18055 char *reasonp; 18056 char *msgp; 18057 18058 ASSERT(un != NULL); 18059 ASSERT(mutex_owned(SD_MUTEX(un))); 18060 ASSERT(bp != NULL); 18061 pktp = SD_GET_PKTP(bp); 18062 ASSERT(pktp != NULL); 18063 xp = SD_GET_XBUF(bp); 18064 ASSERT(xp != NULL); 18065 18066 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18067 mutex_enter(&un->un_pm_mutex); 18068 if ((un->un_state == SD_STATE_SUSPENDED) || 18069 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18070 (pktp->pkt_flags & FLAG_SILENT)) { 18071 mutex_exit(&un->un_pm_mutex); 18072 goto update_pkt_reason; 18073 } 18074 mutex_exit(&un->un_pm_mutex); 18075 18076 /* 18077 * Suppress messages if they are all the same pkt_reason; with 18078 * TQ, many (up to 256) are returned with the same pkt_reason. 18079 * If we are in panic, then suppress the retry messages. 18080 */ 18081 switch (flag) { 18082 case SD_NO_RETRY_ISSUED: 18083 msgp = "giving up"; 18084 break; 18085 case SD_IMMEDIATE_RETRY_ISSUED: 18086 case SD_DELAYED_RETRY_ISSUED: 18087 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18088 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18089 (sd_error_level != SCSI_ERR_ALL))) { 18090 return; 18091 } 18092 msgp = "retrying command"; 18093 break; 18094 default: 18095 goto update_pkt_reason; 18096 } 18097 18098 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18099 scsi_rname(pktp->pkt_reason)); 18100 18101 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18102 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18103 18104 update_pkt_reason: 18105 /* 18106 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18107 * This is to prevent multiple console messages for the same failure 18108 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18109 * when the command is retried successfully because there still may be 18110 * more commands coming back with the same value of pktp->pkt_reason. 18111 */ 18112 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18113 un->un_last_pkt_reason = pktp->pkt_reason; 18114 } 18115 } 18116 18117 18118 /* 18119 * Function: sd_print_cmd_incomplete_msg 18120 * 18121 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18122 * 18123 * Arguments: un - ptr to associated softstate 18124 * bp - ptr to buf(9S) for the command 18125 * arg - passed to sd_print_retry_msg() 18126 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18127 * or SD_NO_RETRY_ISSUED 18128 * 18129 * Context: May be called from interrupt context 18130 */ 18131 18132 static void 18133 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18134 int code) 18135 { 18136 dev_info_t *dip; 18137 18138 ASSERT(un != NULL); 18139 ASSERT(mutex_owned(SD_MUTEX(un))); 18140 ASSERT(bp != NULL); 18141 18142 switch (code) { 18143 case SD_NO_RETRY_ISSUED: 18144 /* Command was failed. Someone turned off this target? */ 18145 if (un->un_state != SD_STATE_OFFLINE) { 18146 /* 18147 * Suppress message if we are detaching and 18148 * device has been disconnected 18149 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18150 * private interface and not part of the DDI 18151 */ 18152 dip = un->un_sd->sd_dev; 18153 if (!(DEVI_IS_DETACHING(dip) && 18154 DEVI_IS_DEVICE_REMOVED(dip))) { 18155 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18156 "disk not responding to selection\n"); 18157 } 18158 New_state(un, SD_STATE_OFFLINE); 18159 } 18160 break; 18161 18162 case SD_DELAYED_RETRY_ISSUED: 18163 case SD_IMMEDIATE_RETRY_ISSUED: 18164 default: 18165 /* Command was successfully queued for retry */ 18166 sd_print_retry_msg(un, bp, arg, code); 18167 break; 18168 } 18169 } 18170 18171 18172 /* 18173 * Function: sd_pkt_reason_cmd_incomplete 18174 * 18175 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18176 * 18177 * Context: May be called from interrupt context 18178 */ 18179 18180 static void 18181 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18182 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18183 { 18184 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18185 18186 ASSERT(un != NULL); 18187 ASSERT(mutex_owned(SD_MUTEX(un))); 18188 ASSERT(bp != NULL); 18189 ASSERT(xp != NULL); 18190 ASSERT(pktp != NULL); 18191 18192 /* Do not do a reset if selection did not complete */ 18193 /* Note: Should this not just check the bit? */ 18194 if (pktp->pkt_state != STATE_GOT_BUS) { 18195 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18196 sd_reset_target(un, pktp); 18197 } 18198 18199 /* 18200 * If the target was not successfully selected, then set 18201 * SD_RETRIES_FAILFAST to indicate that we lost communication 18202 * with the target, and further retries and/or commands are 18203 * likely to take a long time. 18204 */ 18205 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18206 flag |= SD_RETRIES_FAILFAST; 18207 } 18208 18209 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18210 18211 sd_retry_command(un, bp, flag, 18212 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18213 } 18214 18215 18216 18217 /* 18218 * Function: sd_pkt_reason_cmd_tran_err 18219 * 18220 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18221 * 18222 * Context: May be called from interrupt context 18223 */ 18224 18225 static void 18226 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18227 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18228 { 18229 ASSERT(un != NULL); 18230 ASSERT(mutex_owned(SD_MUTEX(un))); 18231 ASSERT(bp != NULL); 18232 ASSERT(xp != NULL); 18233 ASSERT(pktp != NULL); 18234 18235 /* 18236 * Do not reset if we got a parity error, or if 18237 * selection did not complete. 18238 */ 18239 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18240 /* Note: Should this not just check the bit for pkt_state? */ 18241 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18242 (pktp->pkt_state != STATE_GOT_BUS)) { 18243 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18244 sd_reset_target(un, pktp); 18245 } 18246 18247 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18248 18249 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18250 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18251 } 18252 18253 18254 18255 /* 18256 * Function: sd_pkt_reason_cmd_reset 18257 * 18258 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18259 * 18260 * Context: May be called from interrupt context 18261 */ 18262 18263 static void 18264 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18265 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18266 { 18267 ASSERT(un != NULL); 18268 ASSERT(mutex_owned(SD_MUTEX(un))); 18269 ASSERT(bp != NULL); 18270 ASSERT(xp != NULL); 18271 ASSERT(pktp != NULL); 18272 18273 /* The target may still be running the command, so try to reset. */ 18274 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18275 sd_reset_target(un, pktp); 18276 18277 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18278 18279 /* 18280 * If pkt_reason is CMD_RESET chances are that this pkt got 18281 * reset because another target on this bus caused it. The target 18282 * that caused it should get CMD_TIMEOUT with pkt_statistics 18283 * of STAT_TIMEOUT/STAT_DEV_RESET. 18284 */ 18285 18286 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18287 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18288 } 18289 18290 18291 18292 18293 /* 18294 * Function: sd_pkt_reason_cmd_aborted 18295 * 18296 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18297 * 18298 * Context: May be called from interrupt context 18299 */ 18300 18301 static void 18302 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18303 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18304 { 18305 ASSERT(un != NULL); 18306 ASSERT(mutex_owned(SD_MUTEX(un))); 18307 ASSERT(bp != NULL); 18308 ASSERT(xp != NULL); 18309 ASSERT(pktp != NULL); 18310 18311 /* The target may still be running the command, so try to reset. */ 18312 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18313 sd_reset_target(un, pktp); 18314 18315 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18316 18317 /* 18318 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18319 * aborted because another target on this bus caused it. The target 18320 * that caused it should get CMD_TIMEOUT with pkt_statistics 18321 * of STAT_TIMEOUT/STAT_DEV_RESET. 18322 */ 18323 18324 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18325 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18326 } 18327 18328 18329 18330 /* 18331 * Function: sd_pkt_reason_cmd_timeout 18332 * 18333 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18334 * 18335 * Context: May be called from interrupt context 18336 */ 18337 18338 static void 18339 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18340 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18341 { 18342 ASSERT(un != NULL); 18343 ASSERT(mutex_owned(SD_MUTEX(un))); 18344 ASSERT(bp != NULL); 18345 ASSERT(xp != NULL); 18346 ASSERT(pktp != NULL); 18347 18348 18349 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18350 sd_reset_target(un, pktp); 18351 18352 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18353 18354 /* 18355 * A command timeout indicates that we could not establish 18356 * communication with the target, so set SD_RETRIES_FAILFAST 18357 * as further retries/commands are likely to take a long time. 18358 */ 18359 sd_retry_command(un, bp, 18360 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18361 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18362 } 18363 18364 18365 18366 /* 18367 * Function: sd_pkt_reason_cmd_unx_bus_free 18368 * 18369 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18370 * 18371 * Context: May be called from interrupt context 18372 */ 18373 18374 static void 18375 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18376 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18377 { 18378 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18379 18380 ASSERT(un != NULL); 18381 ASSERT(mutex_owned(SD_MUTEX(un))); 18382 ASSERT(bp != NULL); 18383 ASSERT(xp != NULL); 18384 ASSERT(pktp != NULL); 18385 18386 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18387 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18388 18389 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18390 sd_print_retry_msg : NULL; 18391 18392 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18393 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18394 } 18395 18396 18397 /* 18398 * Function: sd_pkt_reason_cmd_tag_reject 18399 * 18400 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18401 * 18402 * Context: May be called from interrupt context 18403 */ 18404 18405 static void 18406 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18407 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18408 { 18409 ASSERT(un != NULL); 18410 ASSERT(mutex_owned(SD_MUTEX(un))); 18411 ASSERT(bp != NULL); 18412 ASSERT(xp != NULL); 18413 ASSERT(pktp != NULL); 18414 18415 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18416 pktp->pkt_flags = 0; 18417 un->un_tagflags = 0; 18418 if (un->un_f_opt_queueing == TRUE) { 18419 un->un_throttle = min(un->un_throttle, 3); 18420 } else { 18421 un->un_throttle = 1; 18422 } 18423 mutex_exit(SD_MUTEX(un)); 18424 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18425 mutex_enter(SD_MUTEX(un)); 18426 18427 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18428 18429 /* Legacy behavior not to check retry counts here. */ 18430 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18431 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18432 } 18433 18434 18435 /* 18436 * Function: sd_pkt_reason_default 18437 * 18438 * Description: Default recovery actions for SCSA pkt_reason values that 18439 * do not have more explicit recovery actions. 18440 * 18441 * Context: May be called from interrupt context 18442 */ 18443 18444 static void 18445 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18446 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18447 { 18448 ASSERT(un != NULL); 18449 ASSERT(mutex_owned(SD_MUTEX(un))); 18450 ASSERT(bp != NULL); 18451 ASSERT(xp != NULL); 18452 ASSERT(pktp != NULL); 18453 18454 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18455 sd_reset_target(un, pktp); 18456 18457 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18458 18459 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18460 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18461 } 18462 18463 18464 18465 /* 18466 * Function: sd_pkt_status_check_condition 18467 * 18468 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18469 * 18470 * Context: May be called from interrupt context 18471 */ 18472 18473 static void 18474 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18475 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18476 { 18477 ASSERT(un != NULL); 18478 ASSERT(mutex_owned(SD_MUTEX(un))); 18479 ASSERT(bp != NULL); 18480 ASSERT(xp != NULL); 18481 ASSERT(pktp != NULL); 18482 18483 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18484 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18485 18486 /* 18487 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18488 * command will be retried after the request sense). Otherwise, retry 18489 * the command. Note: we are issuing the request sense even though the 18490 * retry limit may have been reached for the failed command. 18491 */ 18492 if (un->un_f_arq_enabled == FALSE) { 18493 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18494 "no ARQ, sending request sense command\n"); 18495 sd_send_request_sense_command(un, bp, pktp); 18496 } else { 18497 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18498 "ARQ,retrying request sense command\n"); 18499 #if defined(__i386) || defined(__amd64) 18500 /* 18501 * The SD_RETRY_DELAY value need to be adjusted here 18502 * when SD_RETRY_DELAY change in sddef.h 18503 */ 18504 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18505 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18506 NULL); 18507 #else 18508 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18509 EIO, SD_RETRY_DELAY, NULL); 18510 #endif 18511 } 18512 18513 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18514 } 18515 18516 18517 /* 18518 * Function: sd_pkt_status_busy 18519 * 18520 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18521 * 18522 * Context: May be called from interrupt context 18523 */ 18524 18525 static void 18526 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18527 struct scsi_pkt *pktp) 18528 { 18529 ASSERT(un != NULL); 18530 ASSERT(mutex_owned(SD_MUTEX(un))); 18531 ASSERT(bp != NULL); 18532 ASSERT(xp != NULL); 18533 ASSERT(pktp != NULL); 18534 18535 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18536 "sd_pkt_status_busy: entry\n"); 18537 18538 /* If retries are exhausted, just fail the command. */ 18539 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18540 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18541 "device busy too long\n"); 18542 sd_return_failed_command(un, bp, EIO); 18543 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18544 "sd_pkt_status_busy: exit\n"); 18545 return; 18546 } 18547 xp->xb_retry_count++; 18548 18549 /* 18550 * Try to reset the target. However, we do not want to perform 18551 * more than one reset if the device continues to fail. The reset 18552 * will be performed when the retry count reaches the reset 18553 * threshold. This threshold should be set such that at least 18554 * one retry is issued before the reset is performed. 18555 */ 18556 if (xp->xb_retry_count == 18557 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18558 int rval = 0; 18559 mutex_exit(SD_MUTEX(un)); 18560 if (un->un_f_allow_bus_device_reset == TRUE) { 18561 /* 18562 * First try to reset the LUN; if we cannot then 18563 * try to reset the target. 18564 */ 18565 if (un->un_f_lun_reset_enabled == TRUE) { 18566 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18567 "sd_pkt_status_busy: RESET_LUN\n"); 18568 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18569 } 18570 if (rval == 0) { 18571 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18572 "sd_pkt_status_busy: RESET_TARGET\n"); 18573 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18574 } 18575 } 18576 if (rval == 0) { 18577 /* 18578 * If the RESET_LUN and/or RESET_TARGET failed, 18579 * try RESET_ALL 18580 */ 18581 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18582 "sd_pkt_status_busy: RESET_ALL\n"); 18583 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18584 } 18585 mutex_enter(SD_MUTEX(un)); 18586 if (rval == 0) { 18587 /* 18588 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18589 * At this point we give up & fail the command. 18590 */ 18591 sd_return_failed_command(un, bp, EIO); 18592 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18593 "sd_pkt_status_busy: exit (failed cmd)\n"); 18594 return; 18595 } 18596 } 18597 18598 /* 18599 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18600 * we have already checked the retry counts above. 18601 */ 18602 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18603 EIO, SD_BSY_TIMEOUT, NULL); 18604 18605 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18606 "sd_pkt_status_busy: exit\n"); 18607 } 18608 18609 18610 /* 18611 * Function: sd_pkt_status_reservation_conflict 18612 * 18613 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18614 * command status. 18615 * 18616 * Context: May be called from interrupt context 18617 */ 18618 18619 static void 18620 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18621 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18622 { 18623 ASSERT(un != NULL); 18624 ASSERT(mutex_owned(SD_MUTEX(un))); 18625 ASSERT(bp != NULL); 18626 ASSERT(xp != NULL); 18627 ASSERT(pktp != NULL); 18628 18629 /* 18630 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18631 * conflict could be due to various reasons like incorrect keys, not 18632 * registered or not reserved etc. So, we return EACCES to the caller. 18633 */ 18634 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18635 int cmd = SD_GET_PKT_OPCODE(pktp); 18636 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18637 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18638 sd_return_failed_command(un, bp, EACCES); 18639 return; 18640 } 18641 } 18642 18643 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18644 18645 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18646 if (sd_failfast_enable != 0) { 18647 /* By definition, we must panic here.... */ 18648 sd_panic_for_res_conflict(un); 18649 /*NOTREACHED*/ 18650 } 18651 SD_ERROR(SD_LOG_IO, un, 18652 "sd_handle_resv_conflict: Disk Reserved\n"); 18653 sd_return_failed_command(un, bp, EACCES); 18654 return; 18655 } 18656 18657 /* 18658 * 1147670: retry only if sd_retry_on_reservation_conflict 18659 * property is set (default is 1). Retries will not succeed 18660 * on a disk reserved by another initiator. HA systems 18661 * may reset this via sd.conf to avoid these retries. 18662 * 18663 * Note: The legacy return code for this failure is EIO, however EACCES 18664 * seems more appropriate for a reservation conflict. 18665 */ 18666 if (sd_retry_on_reservation_conflict == 0) { 18667 SD_ERROR(SD_LOG_IO, un, 18668 "sd_handle_resv_conflict: Device Reserved\n"); 18669 sd_return_failed_command(un, bp, EIO); 18670 return; 18671 } 18672 18673 /* 18674 * Retry the command if we can. 18675 * 18676 * Note: The legacy return code for this failure is EIO, however EACCES 18677 * seems more appropriate for a reservation conflict. 18678 */ 18679 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18680 (clock_t)2, NULL); 18681 } 18682 18683 18684 18685 /* 18686 * Function: sd_pkt_status_qfull 18687 * 18688 * Description: Handle a QUEUE FULL condition from the target. This can 18689 * occur if the HBA does not handle the queue full condition. 18690 * (Basically this means third-party HBAs as Sun HBAs will 18691 * handle the queue full condition.) Note that if there are 18692 * some commands already in the transport, then the queue full 18693 * has occurred because the queue for this nexus is actually 18694 * full. If there are no commands in the transport, then the 18695 * queue full is resulting from some other initiator or lun 18696 * consuming all the resources at the target. 18697 * 18698 * Context: May be called from interrupt context 18699 */ 18700 18701 static void 18702 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18703 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18704 { 18705 ASSERT(un != NULL); 18706 ASSERT(mutex_owned(SD_MUTEX(un))); 18707 ASSERT(bp != NULL); 18708 ASSERT(xp != NULL); 18709 ASSERT(pktp != NULL); 18710 18711 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18712 "sd_pkt_status_qfull: entry\n"); 18713 18714 /* 18715 * Just lower the QFULL throttle and retry the command. Note that 18716 * we do not limit the number of retries here. 18717 */ 18718 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18719 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18720 SD_RESTART_TIMEOUT, NULL); 18721 18722 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18723 "sd_pkt_status_qfull: exit\n"); 18724 } 18725 18726 18727 /* 18728 * Function: sd_reset_target 18729 * 18730 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18731 * RESET_TARGET, or RESET_ALL. 18732 * 18733 * Context: May be called under interrupt context. 18734 */ 18735 18736 static void 18737 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18738 { 18739 int rval = 0; 18740 18741 ASSERT(un != NULL); 18742 ASSERT(mutex_owned(SD_MUTEX(un))); 18743 ASSERT(pktp != NULL); 18744 18745 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18746 18747 /* 18748 * No need to reset if the transport layer has already done so. 18749 */ 18750 if ((pktp->pkt_statistics & 18751 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18752 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18753 "sd_reset_target: no reset\n"); 18754 return; 18755 } 18756 18757 mutex_exit(SD_MUTEX(un)); 18758 18759 if (un->un_f_allow_bus_device_reset == TRUE) { 18760 if (un->un_f_lun_reset_enabled == TRUE) { 18761 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18762 "sd_reset_target: RESET_LUN\n"); 18763 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18764 } 18765 if (rval == 0) { 18766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18767 "sd_reset_target: RESET_TARGET\n"); 18768 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18769 } 18770 } 18771 18772 if (rval == 0) { 18773 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18774 "sd_reset_target: RESET_ALL\n"); 18775 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18776 } 18777 18778 mutex_enter(SD_MUTEX(un)); 18779 18780 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18781 } 18782 18783 18784 /* 18785 * Function: sd_media_change_task 18786 * 18787 * Description: Recovery action for CDROM to become available. 18788 * 18789 * Context: Executes in a taskq() thread context 18790 */ 18791 18792 static void 18793 sd_media_change_task(void *arg) 18794 { 18795 struct scsi_pkt *pktp = arg; 18796 struct sd_lun *un; 18797 struct buf *bp; 18798 struct sd_xbuf *xp; 18799 int err = 0; 18800 int retry_count = 0; 18801 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18802 struct sd_sense_info si; 18803 18804 ASSERT(pktp != NULL); 18805 bp = (struct buf *)pktp->pkt_private; 18806 ASSERT(bp != NULL); 18807 xp = SD_GET_XBUF(bp); 18808 ASSERT(xp != NULL); 18809 un = SD_GET_UN(bp); 18810 ASSERT(un != NULL); 18811 ASSERT(!mutex_owned(SD_MUTEX(un))); 18812 ASSERT(un->un_f_monitor_media_state); 18813 18814 si.ssi_severity = SCSI_ERR_INFO; 18815 si.ssi_pfa_flag = FALSE; 18816 18817 /* 18818 * When a reset is issued on a CDROM, it takes a long time to 18819 * recover. First few attempts to read capacity and other things 18820 * related to handling unit attention fail (with a ASC 0x4 and 18821 * ASCQ 0x1). In that case we want to do enough retries and we want 18822 * to limit the retries in other cases of genuine failures like 18823 * no media in drive. 18824 */ 18825 while (retry_count++ < retry_limit) { 18826 if ((err = sd_handle_mchange(un)) == 0) { 18827 break; 18828 } 18829 if (err == EAGAIN) { 18830 retry_limit = SD_UNIT_ATTENTION_RETRY; 18831 } 18832 /* Sleep for 0.5 sec. & try again */ 18833 delay(drv_usectohz(500000)); 18834 } 18835 18836 /* 18837 * Dispatch (retry or fail) the original command here, 18838 * along with appropriate console messages.... 18839 * 18840 * Must grab the mutex before calling sd_retry_command, 18841 * sd_print_sense_msg and sd_return_failed_command. 18842 */ 18843 mutex_enter(SD_MUTEX(un)); 18844 if (err != SD_CMD_SUCCESS) { 18845 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18846 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18847 si.ssi_severity = SCSI_ERR_FATAL; 18848 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18849 sd_return_failed_command(un, bp, EIO); 18850 } else { 18851 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18852 &si, EIO, (clock_t)0, NULL); 18853 } 18854 mutex_exit(SD_MUTEX(un)); 18855 } 18856 18857 18858 18859 /* 18860 * Function: sd_handle_mchange 18861 * 18862 * Description: Perform geometry validation & other recovery when CDROM 18863 * has been removed from drive. 18864 * 18865 * Return Code: 0 for success 18866 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18867 * sd_send_scsi_READ_CAPACITY() 18868 * 18869 * Context: Executes in a taskq() thread context 18870 */ 18871 18872 static int 18873 sd_handle_mchange(struct sd_lun *un) 18874 { 18875 uint64_t capacity; 18876 uint32_t lbasize; 18877 int rval; 18878 18879 ASSERT(!mutex_owned(SD_MUTEX(un))); 18880 ASSERT(un->un_f_monitor_media_state); 18881 18882 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18883 SD_PATH_DIRECT_PRIORITY)) != 0) { 18884 return (rval); 18885 } 18886 18887 mutex_enter(SD_MUTEX(un)); 18888 sd_update_block_info(un, lbasize, capacity); 18889 18890 if (un->un_errstats != NULL) { 18891 struct sd_errstats *stp = 18892 (struct sd_errstats *)un->un_errstats->ks_data; 18893 stp->sd_capacity.value.ui64 = (uint64_t) 18894 ((uint64_t)un->un_blockcount * 18895 (uint64_t)un->un_tgt_blocksize); 18896 } 18897 18898 /* 18899 * Note: Maybe let the strategy/partitioning chain worry about getting 18900 * valid geometry. 18901 */ 18902 un->un_f_geometry_is_valid = FALSE; 18903 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 18904 if (un->un_f_geometry_is_valid == FALSE) { 18905 mutex_exit(SD_MUTEX(un)); 18906 return (EIO); 18907 } 18908 18909 mutex_exit(SD_MUTEX(un)); 18910 18911 /* 18912 * Try to lock the door 18913 */ 18914 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 18915 SD_PATH_DIRECT_PRIORITY)); 18916 } 18917 18918 18919 /* 18920 * Function: sd_send_scsi_DOORLOCK 18921 * 18922 * Description: Issue the scsi DOOR LOCK command 18923 * 18924 * Arguments: un - pointer to driver soft state (unit) structure for 18925 * this target. 18926 * flag - SD_REMOVAL_ALLOW 18927 * SD_REMOVAL_PREVENT 18928 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18929 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18930 * to use the USCSI "direct" chain and bypass the normal 18931 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18932 * command is issued as part of an error recovery action. 18933 * 18934 * Return Code: 0 - Success 18935 * errno return code from sd_send_scsi_cmd() 18936 * 18937 * Context: Can sleep. 18938 */ 18939 18940 static int 18941 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 18942 { 18943 union scsi_cdb cdb; 18944 struct uscsi_cmd ucmd_buf; 18945 struct scsi_extended_sense sense_buf; 18946 int status; 18947 18948 ASSERT(un != NULL); 18949 ASSERT(!mutex_owned(SD_MUTEX(un))); 18950 18951 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 18952 18953 /* already determined doorlock is not supported, fake success */ 18954 if (un->un_f_doorlock_supported == FALSE) { 18955 return (0); 18956 } 18957 18958 bzero(&cdb, sizeof (cdb)); 18959 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18960 18961 cdb.scc_cmd = SCMD_DOORLOCK; 18962 cdb.cdb_opaque[4] = (uchar_t)flag; 18963 18964 ucmd_buf.uscsi_cdb = (char *)&cdb; 18965 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18966 ucmd_buf.uscsi_bufaddr = NULL; 18967 ucmd_buf.uscsi_buflen = 0; 18968 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18969 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18970 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18971 ucmd_buf.uscsi_timeout = 15; 18972 18973 SD_TRACE(SD_LOG_IO, un, 18974 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 18975 18976 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18977 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18978 18979 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 18980 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18981 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 18982 /* fake success and skip subsequent doorlock commands */ 18983 un->un_f_doorlock_supported = FALSE; 18984 return (0); 18985 } 18986 18987 return (status); 18988 } 18989 18990 /* 18991 * Function: sd_send_scsi_READ_CAPACITY 18992 * 18993 * Description: This routine uses the scsi READ CAPACITY command to determine 18994 * the device capacity in number of blocks and the device native 18995 * block size. If this function returns a failure, then the 18996 * values in *capp and *lbap are undefined. If the capacity 18997 * returned is 0xffffffff then the lun is too large for a 18998 * normal READ CAPACITY command and the results of a 18999 * READ CAPACITY 16 will be used instead. 19000 * 19001 * Arguments: un - ptr to soft state struct for the target 19002 * capp - ptr to unsigned 64-bit variable to receive the 19003 * capacity value from the command. 19004 * lbap - ptr to unsigned 32-bit varaible to receive the 19005 * block size value from the command 19006 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19007 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19008 * to use the USCSI "direct" chain and bypass the normal 19009 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19010 * command is issued as part of an error recovery action. 19011 * 19012 * Return Code: 0 - Success 19013 * EIO - IO error 19014 * EACCES - Reservation conflict detected 19015 * EAGAIN - Device is becoming ready 19016 * errno return code from sd_send_scsi_cmd() 19017 * 19018 * Context: Can sleep. Blocks until command completes. 19019 */ 19020 19021 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19022 19023 static int 19024 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19025 int path_flag) 19026 { 19027 struct scsi_extended_sense sense_buf; 19028 struct uscsi_cmd ucmd_buf; 19029 union scsi_cdb cdb; 19030 uint32_t *capacity_buf; 19031 uint64_t capacity; 19032 uint32_t lbasize; 19033 int status; 19034 19035 ASSERT(un != NULL); 19036 ASSERT(!mutex_owned(SD_MUTEX(un))); 19037 ASSERT(capp != NULL); 19038 ASSERT(lbap != NULL); 19039 19040 SD_TRACE(SD_LOG_IO, un, 19041 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19042 19043 /* 19044 * First send a READ_CAPACITY command to the target. 19045 * (This command is mandatory under SCSI-2.) 19046 * 19047 * Set up the CDB for the READ_CAPACITY command. The Partial 19048 * Medium Indicator bit is cleared. The address field must be 19049 * zero if the PMI bit is zero. 19050 */ 19051 bzero(&cdb, sizeof (cdb)); 19052 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19053 19054 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19055 19056 cdb.scc_cmd = SCMD_READ_CAPACITY; 19057 19058 ucmd_buf.uscsi_cdb = (char *)&cdb; 19059 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19060 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19061 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19062 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19063 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19064 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19065 ucmd_buf.uscsi_timeout = 60; 19066 19067 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19068 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19069 19070 switch (status) { 19071 case 0: 19072 /* Return failure if we did not get valid capacity data. */ 19073 if (ucmd_buf.uscsi_resid != 0) { 19074 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19075 return (EIO); 19076 } 19077 19078 /* 19079 * Read capacity and block size from the READ CAPACITY 10 data. 19080 * This data may be adjusted later due to device specific 19081 * issues. 19082 * 19083 * According to the SCSI spec, the READ CAPACITY 10 19084 * command returns the following: 19085 * 19086 * bytes 0-3: Maximum logical block address available. 19087 * (MSB in byte:0 & LSB in byte:3) 19088 * 19089 * bytes 4-7: Block length in bytes 19090 * (MSB in byte:4 & LSB in byte:7) 19091 * 19092 */ 19093 capacity = BE_32(capacity_buf[0]); 19094 lbasize = BE_32(capacity_buf[1]); 19095 19096 /* 19097 * Done with capacity_buf 19098 */ 19099 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19100 19101 /* 19102 * if the reported capacity is set to all 0xf's, then 19103 * this disk is too large and requires SBC-2 commands. 19104 * Reissue the request using READ CAPACITY 16. 19105 */ 19106 if (capacity == 0xffffffff) { 19107 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19108 &lbasize, path_flag); 19109 if (status != 0) { 19110 return (status); 19111 } 19112 } 19113 break; /* Success! */ 19114 case EIO: 19115 switch (ucmd_buf.uscsi_status) { 19116 case STATUS_RESERVATION_CONFLICT: 19117 status = EACCES; 19118 break; 19119 case STATUS_CHECK: 19120 /* 19121 * Check condition; look for ASC/ASCQ of 0x04/0x01 19122 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19123 */ 19124 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19125 (sense_buf.es_add_code == 0x04) && 19126 (sense_buf.es_qual_code == 0x01)) { 19127 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19128 return (EAGAIN); 19129 } 19130 break; 19131 default: 19132 break; 19133 } 19134 /* FALLTHRU */ 19135 default: 19136 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19137 return (status); 19138 } 19139 19140 /* 19141 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19142 * (2352 and 0 are common) so for these devices always force the value 19143 * to 2048 as required by the ATAPI specs. 19144 */ 19145 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19146 lbasize = 2048; 19147 } 19148 19149 /* 19150 * Get the maximum LBA value from the READ CAPACITY data. 19151 * Here we assume that the Partial Medium Indicator (PMI) bit 19152 * was cleared when issuing the command. This means that the LBA 19153 * returned from the device is the LBA of the last logical block 19154 * on the logical unit. The actual logical block count will be 19155 * this value plus one. 19156 * 19157 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19158 * so scale the capacity value to reflect this. 19159 */ 19160 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19161 19162 #if defined(__i386) || defined(__amd64) 19163 /* 19164 * On x86, compensate for off-by-1 error (number of sectors on 19165 * media) (1175930) 19166 */ 19167 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 19168 (lbasize == un->un_sys_blocksize)) { 19169 capacity -= 1; 19170 } 19171 #endif 19172 19173 /* 19174 * Copy the values from the READ CAPACITY command into the space 19175 * provided by the caller. 19176 */ 19177 *capp = capacity; 19178 *lbap = lbasize; 19179 19180 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19181 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19182 19183 /* 19184 * Both the lbasize and capacity from the device must be nonzero, 19185 * otherwise we assume that the values are not valid and return 19186 * failure to the caller. (4203735) 19187 */ 19188 if ((capacity == 0) || (lbasize == 0)) { 19189 return (EIO); 19190 } 19191 19192 return (0); 19193 } 19194 19195 /* 19196 * Function: sd_send_scsi_READ_CAPACITY_16 19197 * 19198 * Description: This routine uses the scsi READ CAPACITY 16 command to 19199 * determine the device capacity in number of blocks and the 19200 * device native block size. If this function returns a failure, 19201 * then the values in *capp and *lbap are undefined. 19202 * This routine should always be called by 19203 * sd_send_scsi_READ_CAPACITY which will appy any device 19204 * specific adjustments to capacity and lbasize. 19205 * 19206 * Arguments: un - ptr to soft state struct for the target 19207 * capp - ptr to unsigned 64-bit variable to receive the 19208 * capacity value from the command. 19209 * lbap - ptr to unsigned 32-bit varaible to receive the 19210 * block size value from the command 19211 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19212 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19213 * to use the USCSI "direct" chain and bypass the normal 19214 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19215 * this command is issued as part of an error recovery 19216 * action. 19217 * 19218 * Return Code: 0 - Success 19219 * EIO - IO error 19220 * EACCES - Reservation conflict detected 19221 * EAGAIN - Device is becoming ready 19222 * errno return code from sd_send_scsi_cmd() 19223 * 19224 * Context: Can sleep. Blocks until command completes. 19225 */ 19226 19227 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19228 19229 static int 19230 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19231 uint32_t *lbap, int path_flag) 19232 { 19233 struct scsi_extended_sense sense_buf; 19234 struct uscsi_cmd ucmd_buf; 19235 union scsi_cdb cdb; 19236 uint64_t *capacity16_buf; 19237 uint64_t capacity; 19238 uint32_t lbasize; 19239 int status; 19240 19241 ASSERT(un != NULL); 19242 ASSERT(!mutex_owned(SD_MUTEX(un))); 19243 ASSERT(capp != NULL); 19244 ASSERT(lbap != NULL); 19245 19246 SD_TRACE(SD_LOG_IO, un, 19247 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19248 19249 /* 19250 * First send a READ_CAPACITY_16 command to the target. 19251 * 19252 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19253 * Medium Indicator bit is cleared. The address field must be 19254 * zero if the PMI bit is zero. 19255 */ 19256 bzero(&cdb, sizeof (cdb)); 19257 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19258 19259 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19260 19261 ucmd_buf.uscsi_cdb = (char *)&cdb; 19262 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19263 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19264 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19265 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19266 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19267 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19268 ucmd_buf.uscsi_timeout = 60; 19269 19270 /* 19271 * Read Capacity (16) is a Service Action In command. One 19272 * command byte (0x9E) is overloaded for multiple operations, 19273 * with the second CDB byte specifying the desired operation 19274 */ 19275 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19276 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19277 19278 /* 19279 * Fill in allocation length field 19280 */ 19281 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19282 19283 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19284 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19285 19286 switch (status) { 19287 case 0: 19288 /* Return failure if we did not get valid capacity data. */ 19289 if (ucmd_buf.uscsi_resid > 20) { 19290 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19291 return (EIO); 19292 } 19293 19294 /* 19295 * Read capacity and block size from the READ CAPACITY 10 data. 19296 * This data may be adjusted later due to device specific 19297 * issues. 19298 * 19299 * According to the SCSI spec, the READ CAPACITY 10 19300 * command returns the following: 19301 * 19302 * bytes 0-7: Maximum logical block address available. 19303 * (MSB in byte:0 & LSB in byte:7) 19304 * 19305 * bytes 8-11: Block length in bytes 19306 * (MSB in byte:8 & LSB in byte:11) 19307 * 19308 */ 19309 capacity = BE_64(capacity16_buf[0]); 19310 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19311 19312 /* 19313 * Done with capacity16_buf 19314 */ 19315 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19316 19317 /* 19318 * if the reported capacity is set to all 0xf's, then 19319 * this disk is too large. This could only happen with 19320 * a device that supports LBAs larger than 64 bits which 19321 * are not defined by any current T10 standards. 19322 */ 19323 if (capacity == 0xffffffffffffffff) { 19324 return (EIO); 19325 } 19326 break; /* Success! */ 19327 case EIO: 19328 switch (ucmd_buf.uscsi_status) { 19329 case STATUS_RESERVATION_CONFLICT: 19330 status = EACCES; 19331 break; 19332 case STATUS_CHECK: 19333 /* 19334 * Check condition; look for ASC/ASCQ of 0x04/0x01 19335 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19336 */ 19337 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19338 (sense_buf.es_add_code == 0x04) && 19339 (sense_buf.es_qual_code == 0x01)) { 19340 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19341 return (EAGAIN); 19342 } 19343 break; 19344 default: 19345 break; 19346 } 19347 /* FALLTHRU */ 19348 default: 19349 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19350 return (status); 19351 } 19352 19353 *capp = capacity; 19354 *lbap = lbasize; 19355 19356 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19357 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19358 19359 return (0); 19360 } 19361 19362 19363 /* 19364 * Function: sd_send_scsi_START_STOP_UNIT 19365 * 19366 * Description: Issue a scsi START STOP UNIT command to the target. 19367 * 19368 * Arguments: un - pointer to driver soft state (unit) structure for 19369 * this target. 19370 * flag - SD_TARGET_START 19371 * SD_TARGET_STOP 19372 * SD_TARGET_EJECT 19373 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19374 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19375 * to use the USCSI "direct" chain and bypass the normal 19376 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19377 * command is issued as part of an error recovery action. 19378 * 19379 * Return Code: 0 - Success 19380 * EIO - IO error 19381 * EACCES - Reservation conflict detected 19382 * ENXIO - Not Ready, medium not present 19383 * errno return code from sd_send_scsi_cmd() 19384 * 19385 * Context: Can sleep. 19386 */ 19387 19388 static int 19389 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19390 { 19391 struct scsi_extended_sense sense_buf; 19392 union scsi_cdb cdb; 19393 struct uscsi_cmd ucmd_buf; 19394 int status; 19395 19396 ASSERT(un != NULL); 19397 ASSERT(!mutex_owned(SD_MUTEX(un))); 19398 19399 SD_TRACE(SD_LOG_IO, un, 19400 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19401 19402 if (un->un_f_check_start_stop && 19403 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19404 (un->un_f_start_stop_supported != TRUE)) { 19405 return (0); 19406 } 19407 19408 bzero(&cdb, sizeof (cdb)); 19409 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19410 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19411 19412 cdb.scc_cmd = SCMD_START_STOP; 19413 cdb.cdb_opaque[4] = (uchar_t)flag; 19414 19415 ucmd_buf.uscsi_cdb = (char *)&cdb; 19416 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19417 ucmd_buf.uscsi_bufaddr = NULL; 19418 ucmd_buf.uscsi_buflen = 0; 19419 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19420 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19421 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19422 ucmd_buf.uscsi_timeout = 200; 19423 19424 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19425 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19426 19427 switch (status) { 19428 case 0: 19429 break; /* Success! */ 19430 case EIO: 19431 switch (ucmd_buf.uscsi_status) { 19432 case STATUS_RESERVATION_CONFLICT: 19433 status = EACCES; 19434 break; 19435 case STATUS_CHECK: 19436 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19437 switch (sense_buf.es_key) { 19438 case KEY_ILLEGAL_REQUEST: 19439 status = ENOTSUP; 19440 break; 19441 case KEY_NOT_READY: 19442 if (sense_buf.es_add_code == 0x3A) { 19443 status = ENXIO; 19444 } 19445 break; 19446 default: 19447 break; 19448 } 19449 } 19450 break; 19451 default: 19452 break; 19453 } 19454 break; 19455 default: 19456 break; 19457 } 19458 19459 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19460 19461 return (status); 19462 } 19463 19464 19465 /* 19466 * Function: sd_start_stop_unit_callback 19467 * 19468 * Description: timeout(9F) callback to begin recovery process for a 19469 * device that has spun down. 19470 * 19471 * Arguments: arg - pointer to associated softstate struct. 19472 * 19473 * Context: Executes in a timeout(9F) thread context 19474 */ 19475 19476 static void 19477 sd_start_stop_unit_callback(void *arg) 19478 { 19479 struct sd_lun *un = arg; 19480 ASSERT(un != NULL); 19481 ASSERT(!mutex_owned(SD_MUTEX(un))); 19482 19483 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19484 19485 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19486 } 19487 19488 19489 /* 19490 * Function: sd_start_stop_unit_task 19491 * 19492 * Description: Recovery procedure when a drive is spun down. 19493 * 19494 * Arguments: arg - pointer to associated softstate struct. 19495 * 19496 * Context: Executes in a taskq() thread context 19497 */ 19498 19499 static void 19500 sd_start_stop_unit_task(void *arg) 19501 { 19502 struct sd_lun *un = arg; 19503 19504 ASSERT(un != NULL); 19505 ASSERT(!mutex_owned(SD_MUTEX(un))); 19506 19507 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19508 19509 /* 19510 * Some unformatted drives report not ready error, no need to 19511 * restart if format has been initiated. 19512 */ 19513 mutex_enter(SD_MUTEX(un)); 19514 if (un->un_f_format_in_progress == TRUE) { 19515 mutex_exit(SD_MUTEX(un)); 19516 return; 19517 } 19518 mutex_exit(SD_MUTEX(un)); 19519 19520 /* 19521 * When a START STOP command is issued from here, it is part of a 19522 * failure recovery operation and must be issued before any other 19523 * commands, including any pending retries. Thus it must be sent 19524 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19525 * succeeds or not, we will start I/O after the attempt. 19526 */ 19527 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19528 SD_PATH_DIRECT_PRIORITY); 19529 19530 /* 19531 * The above call blocks until the START_STOP_UNIT command completes. 19532 * Now that it has completed, we must re-try the original IO that 19533 * received the NOT READY condition in the first place. There are 19534 * three possible conditions here: 19535 * 19536 * (1) The original IO is on un_retry_bp. 19537 * (2) The original IO is on the regular wait queue, and un_retry_bp 19538 * is NULL. 19539 * (3) The original IO is on the regular wait queue, and un_retry_bp 19540 * points to some other, unrelated bp. 19541 * 19542 * For each case, we must call sd_start_cmds() with un_retry_bp 19543 * as the argument. If un_retry_bp is NULL, this will initiate 19544 * processing of the regular wait queue. If un_retry_bp is not NULL, 19545 * then this will process the bp on un_retry_bp. That may or may not 19546 * be the original IO, but that does not matter: the important thing 19547 * is to keep the IO processing going at this point. 19548 * 19549 * Note: This is a very specific error recovery sequence associated 19550 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19551 * serialize the I/O with completion of the spin-up. 19552 */ 19553 mutex_enter(SD_MUTEX(un)); 19554 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19555 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19556 un, un->un_retry_bp); 19557 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19558 sd_start_cmds(un, un->un_retry_bp); 19559 mutex_exit(SD_MUTEX(un)); 19560 19561 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19562 } 19563 19564 19565 /* 19566 * Function: sd_send_scsi_INQUIRY 19567 * 19568 * Description: Issue the scsi INQUIRY command. 19569 * 19570 * Arguments: un 19571 * bufaddr 19572 * buflen 19573 * evpd 19574 * page_code 19575 * page_length 19576 * 19577 * Return Code: 0 - Success 19578 * errno return code from sd_send_scsi_cmd() 19579 * 19580 * Context: Can sleep. Does not return until command is completed. 19581 */ 19582 19583 static int 19584 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19585 uchar_t evpd, uchar_t page_code, size_t *residp) 19586 { 19587 union scsi_cdb cdb; 19588 struct uscsi_cmd ucmd_buf; 19589 int status; 19590 19591 ASSERT(un != NULL); 19592 ASSERT(!mutex_owned(SD_MUTEX(un))); 19593 ASSERT(bufaddr != NULL); 19594 19595 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19596 19597 bzero(&cdb, sizeof (cdb)); 19598 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19599 bzero(bufaddr, buflen); 19600 19601 cdb.scc_cmd = SCMD_INQUIRY; 19602 cdb.cdb_opaque[1] = evpd; 19603 cdb.cdb_opaque[2] = page_code; 19604 FORMG0COUNT(&cdb, buflen); 19605 19606 ucmd_buf.uscsi_cdb = (char *)&cdb; 19607 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19608 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19609 ucmd_buf.uscsi_buflen = buflen; 19610 ucmd_buf.uscsi_rqbuf = NULL; 19611 ucmd_buf.uscsi_rqlen = 0; 19612 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19613 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19614 19615 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19616 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19617 19618 if ((status == 0) && (residp != NULL)) { 19619 *residp = ucmd_buf.uscsi_resid; 19620 } 19621 19622 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19623 19624 return (status); 19625 } 19626 19627 19628 /* 19629 * Function: sd_send_scsi_TEST_UNIT_READY 19630 * 19631 * Description: Issue the scsi TEST UNIT READY command. 19632 * This routine can be told to set the flag USCSI_DIAGNOSE to 19633 * prevent retrying failed commands. Use this when the intent 19634 * is either to check for device readiness, to clear a Unit 19635 * Attention, or to clear any outstanding sense data. 19636 * However under specific conditions the expected behavior 19637 * is for retries to bring a device ready, so use the flag 19638 * with caution. 19639 * 19640 * Arguments: un 19641 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19642 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19643 * 0: dont check for media present, do retries on cmd. 19644 * 19645 * Return Code: 0 - Success 19646 * EIO - IO error 19647 * EACCES - Reservation conflict detected 19648 * ENXIO - Not Ready, medium not present 19649 * errno return code from sd_send_scsi_cmd() 19650 * 19651 * Context: Can sleep. Does not return until command is completed. 19652 */ 19653 19654 static int 19655 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19656 { 19657 struct scsi_extended_sense sense_buf; 19658 union scsi_cdb cdb; 19659 struct uscsi_cmd ucmd_buf; 19660 int status; 19661 19662 ASSERT(un != NULL); 19663 ASSERT(!mutex_owned(SD_MUTEX(un))); 19664 19665 SD_TRACE(SD_LOG_IO, un, 19666 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19667 19668 /* 19669 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19670 * timeouts when they receive a TUR and the queue is not empty. Check 19671 * the configuration flag set during attach (indicating the drive has 19672 * this firmware bug) and un_ncmds_in_transport before issuing the 19673 * TUR. If there are 19674 * pending commands return success, this is a bit arbitrary but is ok 19675 * for non-removables (i.e. the eliteI disks) and non-clustering 19676 * configurations. 19677 */ 19678 if (un->un_f_cfg_tur_check == TRUE) { 19679 mutex_enter(SD_MUTEX(un)); 19680 if (un->un_ncmds_in_transport != 0) { 19681 mutex_exit(SD_MUTEX(un)); 19682 return (0); 19683 } 19684 mutex_exit(SD_MUTEX(un)); 19685 } 19686 19687 bzero(&cdb, sizeof (cdb)); 19688 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19689 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19690 19691 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19692 19693 ucmd_buf.uscsi_cdb = (char *)&cdb; 19694 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19695 ucmd_buf.uscsi_bufaddr = NULL; 19696 ucmd_buf.uscsi_buflen = 0; 19697 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19698 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19699 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19700 19701 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19702 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19703 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19704 } 19705 ucmd_buf.uscsi_timeout = 60; 19706 19707 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19708 UIO_SYSSPACE, UIO_SYSSPACE, 19709 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19710 19711 switch (status) { 19712 case 0: 19713 break; /* Success! */ 19714 case EIO: 19715 switch (ucmd_buf.uscsi_status) { 19716 case STATUS_RESERVATION_CONFLICT: 19717 status = EACCES; 19718 break; 19719 case STATUS_CHECK: 19720 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19721 break; 19722 } 19723 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19724 (sense_buf.es_key == KEY_NOT_READY) && 19725 (sense_buf.es_add_code == 0x3A)) { 19726 status = ENXIO; 19727 } 19728 break; 19729 default: 19730 break; 19731 } 19732 break; 19733 default: 19734 break; 19735 } 19736 19737 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19738 19739 return (status); 19740 } 19741 19742 19743 /* 19744 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19745 * 19746 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19747 * 19748 * Arguments: un 19749 * 19750 * Return Code: 0 - Success 19751 * EACCES 19752 * ENOTSUP 19753 * errno return code from sd_send_scsi_cmd() 19754 * 19755 * Context: Can sleep. Does not return until command is completed. 19756 */ 19757 19758 static int 19759 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19760 uint16_t data_len, uchar_t *data_bufp) 19761 { 19762 struct scsi_extended_sense sense_buf; 19763 union scsi_cdb cdb; 19764 struct uscsi_cmd ucmd_buf; 19765 int status; 19766 int no_caller_buf = FALSE; 19767 19768 ASSERT(un != NULL); 19769 ASSERT(!mutex_owned(SD_MUTEX(un))); 19770 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19771 19772 SD_TRACE(SD_LOG_IO, un, 19773 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19774 19775 bzero(&cdb, sizeof (cdb)); 19776 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19777 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19778 if (data_bufp == NULL) { 19779 /* Allocate a default buf if the caller did not give one */ 19780 ASSERT(data_len == 0); 19781 data_len = MHIOC_RESV_KEY_SIZE; 19782 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19783 no_caller_buf = TRUE; 19784 } 19785 19786 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19787 cdb.cdb_opaque[1] = usr_cmd; 19788 FORMG1COUNT(&cdb, data_len); 19789 19790 ucmd_buf.uscsi_cdb = (char *)&cdb; 19791 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19792 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19793 ucmd_buf.uscsi_buflen = data_len; 19794 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19795 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19796 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19797 ucmd_buf.uscsi_timeout = 60; 19798 19799 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19800 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19801 19802 switch (status) { 19803 case 0: 19804 break; /* Success! */ 19805 case EIO: 19806 switch (ucmd_buf.uscsi_status) { 19807 case STATUS_RESERVATION_CONFLICT: 19808 status = EACCES; 19809 break; 19810 case STATUS_CHECK: 19811 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19812 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19813 status = ENOTSUP; 19814 } 19815 break; 19816 default: 19817 break; 19818 } 19819 break; 19820 default: 19821 break; 19822 } 19823 19824 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19825 19826 if (no_caller_buf == TRUE) { 19827 kmem_free(data_bufp, data_len); 19828 } 19829 19830 return (status); 19831 } 19832 19833 19834 /* 19835 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19836 * 19837 * Description: This routine is the driver entry point for handling CD-ROM 19838 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19839 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19840 * device. 19841 * 19842 * Arguments: un - Pointer to soft state struct for the target. 19843 * usr_cmd SCSI-3 reservation facility command (one of 19844 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19845 * SD_SCSI3_PREEMPTANDABORT) 19846 * usr_bufp - user provided pointer register, reserve descriptor or 19847 * preempt and abort structure (mhioc_register_t, 19848 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19849 * 19850 * Return Code: 0 - Success 19851 * EACCES 19852 * ENOTSUP 19853 * errno return code from sd_send_scsi_cmd() 19854 * 19855 * Context: Can sleep. Does not return until command is completed. 19856 */ 19857 19858 static int 19859 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19860 uchar_t *usr_bufp) 19861 { 19862 struct scsi_extended_sense sense_buf; 19863 union scsi_cdb cdb; 19864 struct uscsi_cmd ucmd_buf; 19865 int status; 19866 uchar_t data_len = sizeof (sd_prout_t); 19867 sd_prout_t *prp; 19868 19869 ASSERT(un != NULL); 19870 ASSERT(!mutex_owned(SD_MUTEX(un))); 19871 ASSERT(data_len == 24); /* required by scsi spec */ 19872 19873 SD_TRACE(SD_LOG_IO, un, 19874 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19875 19876 if (usr_bufp == NULL) { 19877 return (EINVAL); 19878 } 19879 19880 bzero(&cdb, sizeof (cdb)); 19881 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19882 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19883 prp = kmem_zalloc(data_len, KM_SLEEP); 19884 19885 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 19886 cdb.cdb_opaque[1] = usr_cmd; 19887 FORMG1COUNT(&cdb, data_len); 19888 19889 ucmd_buf.uscsi_cdb = (char *)&cdb; 19890 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19891 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 19892 ucmd_buf.uscsi_buflen = data_len; 19893 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19894 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19895 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19896 ucmd_buf.uscsi_timeout = 60; 19897 19898 switch (usr_cmd) { 19899 case SD_SCSI3_REGISTER: { 19900 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 19901 19902 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19903 bcopy(ptr->newkey.key, prp->service_key, 19904 MHIOC_RESV_KEY_SIZE); 19905 prp->aptpl = ptr->aptpl; 19906 break; 19907 } 19908 case SD_SCSI3_RESERVE: 19909 case SD_SCSI3_RELEASE: { 19910 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 19911 19912 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19913 prp->scope_address = BE_32(ptr->scope_specific_addr); 19914 cdb.cdb_opaque[2] = ptr->type; 19915 break; 19916 } 19917 case SD_SCSI3_PREEMPTANDABORT: { 19918 mhioc_preemptandabort_t *ptr = 19919 (mhioc_preemptandabort_t *)usr_bufp; 19920 19921 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19922 bcopy(ptr->victim_key.key, prp->service_key, 19923 MHIOC_RESV_KEY_SIZE); 19924 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 19925 cdb.cdb_opaque[2] = ptr->resvdesc.type; 19926 ucmd_buf.uscsi_flags |= USCSI_HEAD; 19927 break; 19928 } 19929 case SD_SCSI3_REGISTERANDIGNOREKEY: 19930 { 19931 mhioc_registerandignorekey_t *ptr; 19932 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 19933 bcopy(ptr->newkey.key, 19934 prp->service_key, MHIOC_RESV_KEY_SIZE); 19935 prp->aptpl = ptr->aptpl; 19936 break; 19937 } 19938 default: 19939 ASSERT(FALSE); 19940 break; 19941 } 19942 19943 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19944 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19945 19946 switch (status) { 19947 case 0: 19948 break; /* Success! */ 19949 case EIO: 19950 switch (ucmd_buf.uscsi_status) { 19951 case STATUS_RESERVATION_CONFLICT: 19952 status = EACCES; 19953 break; 19954 case STATUS_CHECK: 19955 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19956 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19957 status = ENOTSUP; 19958 } 19959 break; 19960 default: 19961 break; 19962 } 19963 break; 19964 default: 19965 break; 19966 } 19967 19968 kmem_free(prp, data_len); 19969 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 19970 return (status); 19971 } 19972 19973 19974 /* 19975 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 19976 * 19977 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 19978 * 19979 * Arguments: un - pointer to the target's soft state struct 19980 * 19981 * Return Code: 0 - success 19982 * errno-type error code 19983 * 19984 * Context: kernel thread context only. 19985 */ 19986 19987 static int 19988 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 19989 { 19990 struct sd_uscsi_info *uip; 19991 struct uscsi_cmd *uscmd; 19992 union scsi_cdb *cdb; 19993 struct buf *bp; 19994 int rval = 0; 19995 19996 SD_TRACE(SD_LOG_IO, un, 19997 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 19998 19999 ASSERT(un != NULL); 20000 ASSERT(!mutex_owned(SD_MUTEX(un))); 20001 20002 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20003 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20004 20005 /* 20006 * First get some memory for the uscsi_cmd struct and cdb 20007 * and initialize for SYNCHRONIZE_CACHE cmd. 20008 */ 20009 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20010 uscmd->uscsi_cdblen = CDB_GROUP1; 20011 uscmd->uscsi_cdb = (caddr_t)cdb; 20012 uscmd->uscsi_bufaddr = NULL; 20013 uscmd->uscsi_buflen = 0; 20014 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20015 uscmd->uscsi_rqlen = SENSE_LENGTH; 20016 uscmd->uscsi_rqresid = SENSE_LENGTH; 20017 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20018 uscmd->uscsi_timeout = sd_io_time; 20019 20020 /* 20021 * Allocate an sd_uscsi_info struct and fill it with the info 20022 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20023 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20024 * since we allocate the buf here in this function, we do not 20025 * need to preserve the prior contents of b_private. 20026 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20027 */ 20028 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20029 uip->ui_flags = SD_PATH_DIRECT; 20030 uip->ui_cmdp = uscmd; 20031 20032 bp = getrbuf(KM_SLEEP); 20033 bp->b_private = uip; 20034 20035 /* 20036 * Setup buffer to carry uscsi request. 20037 */ 20038 bp->b_flags = B_BUSY; 20039 bp->b_bcount = 0; 20040 bp->b_blkno = 0; 20041 20042 if (dkc != NULL) { 20043 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20044 uip->ui_dkc = *dkc; 20045 } 20046 20047 bp->b_edev = SD_GET_DEV(un); 20048 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20049 20050 (void) sd_uscsi_strategy(bp); 20051 20052 /* 20053 * If synchronous request, wait for completion 20054 * If async just return and let b_iodone callback 20055 * cleanup. 20056 * NOTE: On return, u_ncmds_in_driver will be decremented, 20057 * but it was also incremented in sd_uscsi_strategy(), so 20058 * we should be ok. 20059 */ 20060 if (dkc == NULL) { 20061 (void) biowait(bp); 20062 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20063 } 20064 20065 return (rval); 20066 } 20067 20068 20069 static int 20070 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20071 { 20072 struct sd_uscsi_info *uip; 20073 struct uscsi_cmd *uscmd; 20074 struct scsi_extended_sense *sense_buf; 20075 struct sd_lun *un; 20076 int status; 20077 20078 uip = (struct sd_uscsi_info *)(bp->b_private); 20079 ASSERT(uip != NULL); 20080 20081 uscmd = uip->ui_cmdp; 20082 ASSERT(uscmd != NULL); 20083 20084 sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf; 20085 ASSERT(sense_buf != NULL); 20086 20087 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20088 ASSERT(un != NULL); 20089 20090 status = geterror(bp); 20091 switch (status) { 20092 case 0: 20093 break; /* Success! */ 20094 case EIO: 20095 switch (uscmd->uscsi_status) { 20096 case STATUS_RESERVATION_CONFLICT: 20097 /* Ignore reservation conflict */ 20098 status = 0; 20099 goto done; 20100 20101 case STATUS_CHECK: 20102 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20103 (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) { 20104 /* Ignore Illegal Request error */ 20105 mutex_enter(SD_MUTEX(un)); 20106 un->un_f_sync_cache_supported = FALSE; 20107 mutex_exit(SD_MUTEX(un)); 20108 status = ENOTSUP; 20109 goto done; 20110 } 20111 break; 20112 default: 20113 break; 20114 } 20115 /* FALLTHRU */ 20116 default: 20117 /* Ignore error if the media is not present */ 20118 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 20119 status = 0; 20120 goto done; 20121 } 20122 /* If we reach this, we had an error */ 20123 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20124 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20125 break; 20126 } 20127 20128 done: 20129 if (uip->ui_dkc.dkc_callback != NULL) { 20130 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20131 } 20132 20133 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20134 freerbuf(bp); 20135 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20136 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20137 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20138 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20139 20140 return (status); 20141 } 20142 20143 20144 /* 20145 * Function: sd_send_scsi_GET_CONFIGURATION 20146 * 20147 * Description: Issues the get configuration command to the device. 20148 * Called from sd_check_for_writable_cd & sd_get_media_info 20149 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20150 * Arguments: un 20151 * ucmdbuf 20152 * rqbuf 20153 * rqbuflen 20154 * bufaddr 20155 * buflen 20156 * 20157 * Return Code: 0 - Success 20158 * errno return code from sd_send_scsi_cmd() 20159 * 20160 * Context: Can sleep. Does not return until command is completed. 20161 * 20162 */ 20163 20164 static int 20165 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20166 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20167 { 20168 char cdb[CDB_GROUP1]; 20169 int status; 20170 20171 ASSERT(un != NULL); 20172 ASSERT(!mutex_owned(SD_MUTEX(un))); 20173 ASSERT(bufaddr != NULL); 20174 ASSERT(ucmdbuf != NULL); 20175 ASSERT(rqbuf != NULL); 20176 20177 SD_TRACE(SD_LOG_IO, un, 20178 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20179 20180 bzero(cdb, sizeof (cdb)); 20181 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20182 bzero(rqbuf, rqbuflen); 20183 bzero(bufaddr, buflen); 20184 20185 /* 20186 * Set up cdb field for the get configuration command. 20187 */ 20188 cdb[0] = SCMD_GET_CONFIGURATION; 20189 cdb[1] = 0x02; /* Requested Type */ 20190 cdb[8] = SD_PROFILE_HEADER_LEN; 20191 ucmdbuf->uscsi_cdb = cdb; 20192 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20193 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20194 ucmdbuf->uscsi_buflen = buflen; 20195 ucmdbuf->uscsi_timeout = sd_io_time; 20196 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20197 ucmdbuf->uscsi_rqlen = rqbuflen; 20198 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20199 20200 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20201 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20202 20203 switch (status) { 20204 case 0: 20205 break; /* Success! */ 20206 case EIO: 20207 switch (ucmdbuf->uscsi_status) { 20208 case STATUS_RESERVATION_CONFLICT: 20209 status = EACCES; 20210 break; 20211 default: 20212 break; 20213 } 20214 break; 20215 default: 20216 break; 20217 } 20218 20219 if (status == 0) { 20220 SD_DUMP_MEMORY(un, SD_LOG_IO, 20221 "sd_send_scsi_GET_CONFIGURATION: data", 20222 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20223 } 20224 20225 SD_TRACE(SD_LOG_IO, un, 20226 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20227 20228 return (status); 20229 } 20230 20231 /* 20232 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20233 * 20234 * Description: Issues the get configuration command to the device to 20235 * retrieve a specfic feature. Called from 20236 * sd_check_for_writable_cd & sd_set_mmc_caps. 20237 * Arguments: un 20238 * ucmdbuf 20239 * rqbuf 20240 * rqbuflen 20241 * bufaddr 20242 * buflen 20243 * feature 20244 * 20245 * Return Code: 0 - Success 20246 * errno return code from sd_send_scsi_cmd() 20247 * 20248 * Context: Can sleep. Does not return until command is completed. 20249 * 20250 */ 20251 static int 20252 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20253 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20254 uchar_t *bufaddr, uint_t buflen, char feature) 20255 { 20256 char cdb[CDB_GROUP1]; 20257 int status; 20258 20259 ASSERT(un != NULL); 20260 ASSERT(!mutex_owned(SD_MUTEX(un))); 20261 ASSERT(bufaddr != NULL); 20262 ASSERT(ucmdbuf != NULL); 20263 ASSERT(rqbuf != NULL); 20264 20265 SD_TRACE(SD_LOG_IO, un, 20266 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20267 20268 bzero(cdb, sizeof (cdb)); 20269 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20270 bzero(rqbuf, rqbuflen); 20271 bzero(bufaddr, buflen); 20272 20273 /* 20274 * Set up cdb field for the get configuration command. 20275 */ 20276 cdb[0] = SCMD_GET_CONFIGURATION; 20277 cdb[1] = 0x02; /* Requested Type */ 20278 cdb[3] = feature; 20279 cdb[8] = buflen; 20280 ucmdbuf->uscsi_cdb = cdb; 20281 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20282 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20283 ucmdbuf->uscsi_buflen = buflen; 20284 ucmdbuf->uscsi_timeout = sd_io_time; 20285 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20286 ucmdbuf->uscsi_rqlen = rqbuflen; 20287 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20288 20289 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20290 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20291 20292 switch (status) { 20293 case 0: 20294 break; /* Success! */ 20295 case EIO: 20296 switch (ucmdbuf->uscsi_status) { 20297 case STATUS_RESERVATION_CONFLICT: 20298 status = EACCES; 20299 break; 20300 default: 20301 break; 20302 } 20303 break; 20304 default: 20305 break; 20306 } 20307 20308 if (status == 0) { 20309 SD_DUMP_MEMORY(un, SD_LOG_IO, 20310 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20311 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20312 } 20313 20314 SD_TRACE(SD_LOG_IO, un, 20315 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20316 20317 return (status); 20318 } 20319 20320 20321 /* 20322 * Function: sd_send_scsi_MODE_SENSE 20323 * 20324 * Description: Utility function for issuing a scsi MODE SENSE command. 20325 * Note: This routine uses a consistent implementation for Group0, 20326 * Group1, and Group2 commands across all platforms. ATAPI devices 20327 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20328 * 20329 * Arguments: un - pointer to the softstate struct for the target. 20330 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20331 * CDB_GROUP[1|2] (10 byte). 20332 * bufaddr - buffer for page data retrieved from the target. 20333 * buflen - size of page to be retrieved. 20334 * page_code - page code of data to be retrieved from the target. 20335 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20336 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20337 * to use the USCSI "direct" chain and bypass the normal 20338 * command waitq. 20339 * 20340 * Return Code: 0 - Success 20341 * errno return code from sd_send_scsi_cmd() 20342 * 20343 * Context: Can sleep. Does not return until command is completed. 20344 */ 20345 20346 static int 20347 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20348 size_t buflen, uchar_t page_code, int path_flag) 20349 { 20350 struct scsi_extended_sense sense_buf; 20351 union scsi_cdb cdb; 20352 struct uscsi_cmd ucmd_buf; 20353 int status; 20354 20355 ASSERT(un != NULL); 20356 ASSERT(!mutex_owned(SD_MUTEX(un))); 20357 ASSERT(bufaddr != NULL); 20358 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20359 (cdbsize == CDB_GROUP2)); 20360 20361 SD_TRACE(SD_LOG_IO, un, 20362 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20363 20364 bzero(&cdb, sizeof (cdb)); 20365 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20366 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20367 bzero(bufaddr, buflen); 20368 20369 if (cdbsize == CDB_GROUP0) { 20370 cdb.scc_cmd = SCMD_MODE_SENSE; 20371 cdb.cdb_opaque[2] = page_code; 20372 FORMG0COUNT(&cdb, buflen); 20373 } else { 20374 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20375 cdb.cdb_opaque[2] = page_code; 20376 FORMG1COUNT(&cdb, buflen); 20377 } 20378 20379 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20380 20381 ucmd_buf.uscsi_cdb = (char *)&cdb; 20382 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20383 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20384 ucmd_buf.uscsi_buflen = buflen; 20385 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20386 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20387 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20388 ucmd_buf.uscsi_timeout = 60; 20389 20390 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20391 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20392 20393 switch (status) { 20394 case 0: 20395 break; /* Success! */ 20396 case EIO: 20397 switch (ucmd_buf.uscsi_status) { 20398 case STATUS_RESERVATION_CONFLICT: 20399 status = EACCES; 20400 break; 20401 default: 20402 break; 20403 } 20404 break; 20405 default: 20406 break; 20407 } 20408 20409 if (status == 0) { 20410 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20411 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20412 } 20413 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20414 20415 return (status); 20416 } 20417 20418 20419 /* 20420 * Function: sd_send_scsi_MODE_SELECT 20421 * 20422 * Description: Utility function for issuing a scsi MODE SELECT command. 20423 * Note: This routine uses a consistent implementation for Group0, 20424 * Group1, and Group2 commands across all platforms. ATAPI devices 20425 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20426 * 20427 * Arguments: un - pointer to the softstate struct for the target. 20428 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20429 * CDB_GROUP[1|2] (10 byte). 20430 * bufaddr - buffer for page data retrieved from the target. 20431 * buflen - size of page to be retrieved. 20432 * save_page - boolean to determin if SP bit should be set. 20433 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20434 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20435 * to use the USCSI "direct" chain and bypass the normal 20436 * command waitq. 20437 * 20438 * Return Code: 0 - Success 20439 * errno return code from sd_send_scsi_cmd() 20440 * 20441 * Context: Can sleep. Does not return until command is completed. 20442 */ 20443 20444 static int 20445 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20446 size_t buflen, uchar_t save_page, int path_flag) 20447 { 20448 struct scsi_extended_sense sense_buf; 20449 union scsi_cdb cdb; 20450 struct uscsi_cmd ucmd_buf; 20451 int status; 20452 20453 ASSERT(un != NULL); 20454 ASSERT(!mutex_owned(SD_MUTEX(un))); 20455 ASSERT(bufaddr != NULL); 20456 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20457 (cdbsize == CDB_GROUP2)); 20458 20459 SD_TRACE(SD_LOG_IO, un, 20460 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20461 20462 bzero(&cdb, sizeof (cdb)); 20463 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20464 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20465 20466 /* Set the PF bit for many third party drives */ 20467 cdb.cdb_opaque[1] = 0x10; 20468 20469 /* Set the savepage(SP) bit if given */ 20470 if (save_page == SD_SAVE_PAGE) { 20471 cdb.cdb_opaque[1] |= 0x01; 20472 } 20473 20474 if (cdbsize == CDB_GROUP0) { 20475 cdb.scc_cmd = SCMD_MODE_SELECT; 20476 FORMG0COUNT(&cdb, buflen); 20477 } else { 20478 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20479 FORMG1COUNT(&cdb, buflen); 20480 } 20481 20482 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20483 20484 ucmd_buf.uscsi_cdb = (char *)&cdb; 20485 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20486 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20487 ucmd_buf.uscsi_buflen = buflen; 20488 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20489 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20490 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20491 ucmd_buf.uscsi_timeout = 60; 20492 20493 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20494 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20495 20496 switch (status) { 20497 case 0: 20498 break; /* Success! */ 20499 case EIO: 20500 switch (ucmd_buf.uscsi_status) { 20501 case STATUS_RESERVATION_CONFLICT: 20502 status = EACCES; 20503 break; 20504 default: 20505 break; 20506 } 20507 break; 20508 default: 20509 break; 20510 } 20511 20512 if (status == 0) { 20513 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20514 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20515 } 20516 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20517 20518 return (status); 20519 } 20520 20521 20522 /* 20523 * Function: sd_send_scsi_RDWR 20524 * 20525 * Description: Issue a scsi READ or WRITE command with the given parameters. 20526 * 20527 * Arguments: un: Pointer to the sd_lun struct for the target. 20528 * cmd: SCMD_READ or SCMD_WRITE 20529 * bufaddr: Address of caller's buffer to receive the RDWR data 20530 * buflen: Length of caller's buffer receive the RDWR data. 20531 * start_block: Block number for the start of the RDWR operation. 20532 * (Assumes target-native block size.) 20533 * residp: Pointer to variable to receive the redisual of the 20534 * RDWR operation (may be NULL of no residual requested). 20535 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20536 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20537 * to use the USCSI "direct" chain and bypass the normal 20538 * command waitq. 20539 * 20540 * Return Code: 0 - Success 20541 * errno return code from sd_send_scsi_cmd() 20542 * 20543 * Context: Can sleep. Does not return until command is completed. 20544 */ 20545 20546 static int 20547 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20548 size_t buflen, daddr_t start_block, int path_flag) 20549 { 20550 struct scsi_extended_sense sense_buf; 20551 union scsi_cdb cdb; 20552 struct uscsi_cmd ucmd_buf; 20553 uint32_t block_count; 20554 int status; 20555 int cdbsize; 20556 uchar_t flag; 20557 20558 ASSERT(un != NULL); 20559 ASSERT(!mutex_owned(SD_MUTEX(un))); 20560 ASSERT(bufaddr != NULL); 20561 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20562 20563 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20564 20565 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20566 return (EINVAL); 20567 } 20568 20569 mutex_enter(SD_MUTEX(un)); 20570 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20571 mutex_exit(SD_MUTEX(un)); 20572 20573 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20574 20575 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20576 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20577 bufaddr, buflen, start_block, block_count); 20578 20579 bzero(&cdb, sizeof (cdb)); 20580 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20581 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20582 20583 /* Compute CDB size to use */ 20584 if (start_block > 0xffffffff) 20585 cdbsize = CDB_GROUP4; 20586 else if ((start_block & 0xFFE00000) || 20587 (un->un_f_cfg_is_atapi == TRUE)) 20588 cdbsize = CDB_GROUP1; 20589 else 20590 cdbsize = CDB_GROUP0; 20591 20592 switch (cdbsize) { 20593 case CDB_GROUP0: /* 6-byte CDBs */ 20594 cdb.scc_cmd = cmd; 20595 FORMG0ADDR(&cdb, start_block); 20596 FORMG0COUNT(&cdb, block_count); 20597 break; 20598 case CDB_GROUP1: /* 10-byte CDBs */ 20599 cdb.scc_cmd = cmd | SCMD_GROUP1; 20600 FORMG1ADDR(&cdb, start_block); 20601 FORMG1COUNT(&cdb, block_count); 20602 break; 20603 case CDB_GROUP4: /* 16-byte CDBs */ 20604 cdb.scc_cmd = cmd | SCMD_GROUP4; 20605 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20606 FORMG4COUNT(&cdb, block_count); 20607 break; 20608 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20609 default: 20610 /* All others reserved */ 20611 return (EINVAL); 20612 } 20613 20614 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20615 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20616 20617 ucmd_buf.uscsi_cdb = (char *)&cdb; 20618 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20619 ucmd_buf.uscsi_bufaddr = bufaddr; 20620 ucmd_buf.uscsi_buflen = buflen; 20621 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20622 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20623 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20624 ucmd_buf.uscsi_timeout = 60; 20625 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20626 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20627 switch (status) { 20628 case 0: 20629 break; /* Success! */ 20630 case EIO: 20631 switch (ucmd_buf.uscsi_status) { 20632 case STATUS_RESERVATION_CONFLICT: 20633 status = EACCES; 20634 break; 20635 default: 20636 break; 20637 } 20638 break; 20639 default: 20640 break; 20641 } 20642 20643 if (status == 0) { 20644 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20645 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20646 } 20647 20648 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20649 20650 return (status); 20651 } 20652 20653 20654 /* 20655 * Function: sd_send_scsi_LOG_SENSE 20656 * 20657 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20658 * 20659 * Arguments: un: Pointer to the sd_lun struct for the target. 20660 * 20661 * Return Code: 0 - Success 20662 * errno return code from sd_send_scsi_cmd() 20663 * 20664 * Context: Can sleep. Does not return until command is completed. 20665 */ 20666 20667 static int 20668 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20669 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20670 int path_flag) 20671 20672 { 20673 struct scsi_extended_sense sense_buf; 20674 union scsi_cdb cdb; 20675 struct uscsi_cmd ucmd_buf; 20676 int status; 20677 20678 ASSERT(un != NULL); 20679 ASSERT(!mutex_owned(SD_MUTEX(un))); 20680 20681 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20682 20683 bzero(&cdb, sizeof (cdb)); 20684 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20685 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20686 20687 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20688 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20689 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20690 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20691 FORMG1COUNT(&cdb, buflen); 20692 20693 ucmd_buf.uscsi_cdb = (char *)&cdb; 20694 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20695 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20696 ucmd_buf.uscsi_buflen = buflen; 20697 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20698 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20699 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20700 ucmd_buf.uscsi_timeout = 60; 20701 20702 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20703 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20704 20705 switch (status) { 20706 case 0: 20707 break; 20708 case EIO: 20709 switch (ucmd_buf.uscsi_status) { 20710 case STATUS_RESERVATION_CONFLICT: 20711 status = EACCES; 20712 break; 20713 case STATUS_CHECK: 20714 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20715 (sense_buf.es_key == KEY_ILLEGAL_REQUEST) && 20716 (sense_buf.es_add_code == 0x24)) { 20717 /* 20718 * ASC 0x24: INVALID FIELD IN CDB 20719 */ 20720 switch (page_code) { 20721 case START_STOP_CYCLE_PAGE: 20722 /* 20723 * The start stop cycle counter is 20724 * implemented as page 0x31 in earlier 20725 * generation disks. In new generation 20726 * disks the start stop cycle counter is 20727 * implemented as page 0xE. To properly 20728 * handle this case if an attempt for 20729 * log page 0xE is made and fails we 20730 * will try again using page 0x31. 20731 * 20732 * Network storage BU committed to 20733 * maintain the page 0x31 for this 20734 * purpose and will not have any other 20735 * page implemented with page code 0x31 20736 * until all disks transition to the 20737 * standard page. 20738 */ 20739 mutex_enter(SD_MUTEX(un)); 20740 un->un_start_stop_cycle_page = 20741 START_STOP_CYCLE_VU_PAGE; 20742 cdb.cdb_opaque[2] = 20743 (char)(page_control << 6) | 20744 un->un_start_stop_cycle_page; 20745 mutex_exit(SD_MUTEX(un)); 20746 status = sd_send_scsi_cmd( 20747 SD_GET_DEV(un), &ucmd_buf, 20748 UIO_SYSSPACE, UIO_SYSSPACE, 20749 UIO_SYSSPACE, path_flag); 20750 20751 break; 20752 case TEMPERATURE_PAGE: 20753 status = ENOTTY; 20754 break; 20755 default: 20756 break; 20757 } 20758 } 20759 break; 20760 default: 20761 break; 20762 } 20763 break; 20764 default: 20765 break; 20766 } 20767 20768 if (status == 0) { 20769 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20770 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20771 } 20772 20773 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20774 20775 return (status); 20776 } 20777 20778 20779 /* 20780 * Function: sdioctl 20781 * 20782 * Description: Driver's ioctl(9e) entry point function. 20783 * 20784 * Arguments: dev - device number 20785 * cmd - ioctl operation to be performed 20786 * arg - user argument, contains data to be set or reference 20787 * parameter for get 20788 * flag - bit flag, indicating open settings, 32/64 bit type 20789 * cred_p - user credential pointer 20790 * rval_p - calling process return value (OPT) 20791 * 20792 * Return Code: EINVAL 20793 * ENOTTY 20794 * ENXIO 20795 * EIO 20796 * EFAULT 20797 * ENOTSUP 20798 * EPERM 20799 * 20800 * Context: Called from the device switch at normal priority. 20801 */ 20802 20803 static int 20804 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20805 { 20806 struct sd_lun *un = NULL; 20807 int geom_validated = FALSE; 20808 int err = 0; 20809 int i = 0; 20810 cred_t *cr; 20811 20812 /* 20813 * All device accesses go thru sdstrategy where we check on suspend 20814 * status 20815 */ 20816 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20817 return (ENXIO); 20818 } 20819 20820 ASSERT(!mutex_owned(SD_MUTEX(un))); 20821 20822 /* 20823 * Moved this wait from sd_uscsi_strategy to here for 20824 * reasons of deadlock prevention. Internal driver commands, 20825 * specifically those to change a devices power level, result 20826 * in a call to sd_uscsi_strategy. 20827 */ 20828 mutex_enter(SD_MUTEX(un)); 20829 while ((un->un_state == SD_STATE_SUSPENDED) || 20830 (un->un_state == SD_STATE_PM_CHANGING)) { 20831 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20832 } 20833 /* 20834 * Twiddling the counter here protects commands from now 20835 * through to the top of sd_uscsi_strategy. Without the 20836 * counter inc. a power down, for example, could get in 20837 * after the above check for state is made and before 20838 * execution gets to the top of sd_uscsi_strategy. 20839 * That would cause problems. 20840 */ 20841 un->un_ncmds_in_driver++; 20842 20843 if ((un->un_f_geometry_is_valid == FALSE) && 20844 (flag & (FNDELAY | FNONBLOCK))) { 20845 switch (cmd) { 20846 case CDROMPAUSE: 20847 case CDROMRESUME: 20848 case CDROMPLAYMSF: 20849 case CDROMPLAYTRKIND: 20850 case CDROMREADTOCHDR: 20851 case CDROMREADTOCENTRY: 20852 case CDROMSTOP: 20853 case CDROMSTART: 20854 case CDROMVOLCTRL: 20855 case CDROMSUBCHNL: 20856 case CDROMREADMODE2: 20857 case CDROMREADMODE1: 20858 case CDROMREADOFFSET: 20859 case CDROMSBLKMODE: 20860 case CDROMGBLKMODE: 20861 case CDROMGDRVSPEED: 20862 case CDROMSDRVSPEED: 20863 case CDROMCDDA: 20864 case CDROMCDXA: 20865 case CDROMSUBCODE: 20866 if (!ISCD(un)) { 20867 un->un_ncmds_in_driver--; 20868 ASSERT(un->un_ncmds_in_driver >= 0); 20869 mutex_exit(SD_MUTEX(un)); 20870 return (ENOTTY); 20871 } 20872 break; 20873 case FDEJECT: 20874 case DKIOCEJECT: 20875 case CDROMEJECT: 20876 if (!un->un_f_eject_media_supported) { 20877 un->un_ncmds_in_driver--; 20878 ASSERT(un->un_ncmds_in_driver >= 0); 20879 mutex_exit(SD_MUTEX(un)); 20880 return (ENOTTY); 20881 } 20882 break; 20883 case DKIOCSVTOC: 20884 case DKIOCSETEFI: 20885 case DKIOCSMBOOT: 20886 case DKIOCFLUSHWRITECACHE: 20887 mutex_exit(SD_MUTEX(un)); 20888 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 20889 if (err != 0) { 20890 mutex_enter(SD_MUTEX(un)); 20891 un->un_ncmds_in_driver--; 20892 ASSERT(un->un_ncmds_in_driver >= 0); 20893 mutex_exit(SD_MUTEX(un)); 20894 return (EIO); 20895 } 20896 mutex_enter(SD_MUTEX(un)); 20897 /* FALLTHROUGH */ 20898 case DKIOCREMOVABLE: 20899 case DKIOCHOTPLUGGABLE: 20900 case DKIOCINFO: 20901 case DKIOCGMEDIAINFO: 20902 case MHIOCENFAILFAST: 20903 case MHIOCSTATUS: 20904 case MHIOCTKOWN: 20905 case MHIOCRELEASE: 20906 case MHIOCGRP_INKEYS: 20907 case MHIOCGRP_INRESV: 20908 case MHIOCGRP_REGISTER: 20909 case MHIOCGRP_RESERVE: 20910 case MHIOCGRP_PREEMPTANDABORT: 20911 case MHIOCGRP_REGISTERANDIGNOREKEY: 20912 case CDROMCLOSETRAY: 20913 case USCSICMD: 20914 goto skip_ready_valid; 20915 default: 20916 break; 20917 } 20918 20919 mutex_exit(SD_MUTEX(un)); 20920 err = sd_ready_and_valid(un); 20921 mutex_enter(SD_MUTEX(un)); 20922 if (err == SD_READY_NOT_VALID) { 20923 switch (cmd) { 20924 case DKIOCGAPART: 20925 case DKIOCGGEOM: 20926 case DKIOCSGEOM: 20927 case DKIOCGVTOC: 20928 case DKIOCSVTOC: 20929 case DKIOCSAPART: 20930 case DKIOCG_PHYGEOM: 20931 case DKIOCG_VIRTGEOM: 20932 err = ENOTSUP; 20933 un->un_ncmds_in_driver--; 20934 ASSERT(un->un_ncmds_in_driver >= 0); 20935 mutex_exit(SD_MUTEX(un)); 20936 return (err); 20937 } 20938 } 20939 if (err != SD_READY_VALID) { 20940 switch (cmd) { 20941 case DKIOCSTATE: 20942 case CDROMGDRVSPEED: 20943 case CDROMSDRVSPEED: 20944 case FDEJECT: /* for eject command */ 20945 case DKIOCEJECT: 20946 case CDROMEJECT: 20947 case DKIOCGETEFI: 20948 case DKIOCSGEOM: 20949 case DKIOCREMOVABLE: 20950 case DKIOCHOTPLUGGABLE: 20951 case DKIOCSAPART: 20952 case DKIOCSETEFI: 20953 break; 20954 default: 20955 if (un->un_f_has_removable_media) { 20956 err = ENXIO; 20957 } else { 20958 /* Do not map EACCES to EIO */ 20959 if (err != EACCES) 20960 err = EIO; 20961 } 20962 un->un_ncmds_in_driver--; 20963 ASSERT(un->un_ncmds_in_driver >= 0); 20964 mutex_exit(SD_MUTEX(un)); 20965 return (err); 20966 } 20967 } 20968 geom_validated = TRUE; 20969 } 20970 if ((un->un_f_geometry_is_valid == TRUE) && 20971 (un->un_solaris_size > 0)) { 20972 /* 20973 * the "geometry_is_valid" flag could be true if we 20974 * have an fdisk table but no Solaris partition 20975 */ 20976 if (un->un_vtoc.v_sanity != VTOC_SANE) { 20977 /* it is EFI, so return ENOTSUP for these */ 20978 switch (cmd) { 20979 case DKIOCGAPART: 20980 case DKIOCGGEOM: 20981 case DKIOCGVTOC: 20982 case DKIOCSVTOC: 20983 case DKIOCSAPART: 20984 err = ENOTSUP; 20985 un->un_ncmds_in_driver--; 20986 ASSERT(un->un_ncmds_in_driver >= 0); 20987 mutex_exit(SD_MUTEX(un)); 20988 return (err); 20989 } 20990 } 20991 } 20992 20993 skip_ready_valid: 20994 mutex_exit(SD_MUTEX(un)); 20995 20996 switch (cmd) { 20997 case DKIOCINFO: 20998 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 20999 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21000 break; 21001 21002 case DKIOCGMEDIAINFO: 21003 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21004 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21005 break; 21006 21007 case DKIOCGGEOM: 21008 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21009 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21010 geom_validated); 21011 break; 21012 21013 case DKIOCSGEOM: 21014 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21015 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21016 break; 21017 21018 case DKIOCGAPART: 21019 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21020 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21021 geom_validated); 21022 break; 21023 21024 case DKIOCSAPART: 21025 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21026 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21027 break; 21028 21029 case DKIOCGVTOC: 21030 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21031 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21032 geom_validated); 21033 break; 21034 21035 case DKIOCGETEFI: 21036 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21037 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21038 break; 21039 21040 case DKIOCPARTITION: 21041 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21042 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21043 break; 21044 21045 case DKIOCSVTOC: 21046 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21047 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21048 break; 21049 21050 case DKIOCSETEFI: 21051 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21052 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21053 break; 21054 21055 case DKIOCGMBOOT: 21056 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21057 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21058 break; 21059 21060 case DKIOCSMBOOT: 21061 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21062 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21063 break; 21064 21065 case DKIOCLOCK: 21066 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21067 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21068 SD_PATH_STANDARD); 21069 break; 21070 21071 case DKIOCUNLOCK: 21072 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21073 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21074 SD_PATH_STANDARD); 21075 break; 21076 21077 case DKIOCSTATE: { 21078 enum dkio_state state; 21079 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21080 21081 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21082 err = EFAULT; 21083 } else { 21084 err = sd_check_media(dev, state); 21085 if (err == 0) { 21086 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21087 sizeof (int), flag) != 0) 21088 err = EFAULT; 21089 } 21090 } 21091 break; 21092 } 21093 21094 case DKIOCREMOVABLE: 21095 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21096 /* 21097 * At present, vold only does automount for removable-media 21098 * devices, in order not to break current applications, we 21099 * still let hopluggable devices pretend to be removable media 21100 * devices for vold. In the near future, once vold is EOL'ed, 21101 * we should remove this workaround. 21102 */ 21103 if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) { 21104 i = 1; 21105 } else { 21106 i = 0; 21107 } 21108 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21109 err = EFAULT; 21110 } else { 21111 err = 0; 21112 } 21113 break; 21114 21115 case DKIOCHOTPLUGGABLE: 21116 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21117 if (un->un_f_is_hotpluggable) { 21118 i = 1; 21119 } else { 21120 i = 0; 21121 } 21122 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21123 err = EFAULT; 21124 } else { 21125 err = 0; 21126 } 21127 break; 21128 21129 case DKIOCGTEMPERATURE: 21130 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21131 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21132 break; 21133 21134 case MHIOCENFAILFAST: 21135 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21136 if ((err = drv_priv(cred_p)) == 0) { 21137 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21138 } 21139 break; 21140 21141 case MHIOCTKOWN: 21142 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21143 if ((err = drv_priv(cred_p)) == 0) { 21144 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21145 } 21146 break; 21147 21148 case MHIOCRELEASE: 21149 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21150 if ((err = drv_priv(cred_p)) == 0) { 21151 err = sd_mhdioc_release(dev); 21152 } 21153 break; 21154 21155 case MHIOCSTATUS: 21156 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21157 if ((err = drv_priv(cred_p)) == 0) { 21158 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21159 case 0: 21160 err = 0; 21161 break; 21162 case EACCES: 21163 *rval_p = 1; 21164 err = 0; 21165 break; 21166 default: 21167 err = EIO; 21168 break; 21169 } 21170 } 21171 break; 21172 21173 case MHIOCQRESERVE: 21174 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21175 if ((err = drv_priv(cred_p)) == 0) { 21176 err = sd_reserve_release(dev, SD_RESERVE); 21177 } 21178 break; 21179 21180 case MHIOCREREGISTERDEVID: 21181 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21182 if (drv_priv(cred_p) == EPERM) { 21183 err = EPERM; 21184 } else if (!un->un_f_devid_supported) { 21185 err = ENOTTY; 21186 } else { 21187 err = sd_mhdioc_register_devid(dev); 21188 } 21189 break; 21190 21191 case MHIOCGRP_INKEYS: 21192 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21193 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21194 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21195 err = ENOTSUP; 21196 } else { 21197 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21198 flag); 21199 } 21200 } 21201 break; 21202 21203 case MHIOCGRP_INRESV: 21204 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21205 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21206 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21207 err = ENOTSUP; 21208 } else { 21209 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21210 } 21211 } 21212 break; 21213 21214 case MHIOCGRP_REGISTER: 21215 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21216 if ((err = drv_priv(cred_p)) != EPERM) { 21217 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21218 err = ENOTSUP; 21219 } else if (arg != NULL) { 21220 mhioc_register_t reg; 21221 if (ddi_copyin((void *)arg, ®, 21222 sizeof (mhioc_register_t), flag) != 0) { 21223 err = EFAULT; 21224 } else { 21225 err = 21226 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21227 un, SD_SCSI3_REGISTER, 21228 (uchar_t *)®); 21229 } 21230 } 21231 } 21232 break; 21233 21234 case MHIOCGRP_RESERVE: 21235 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21236 if ((err = drv_priv(cred_p)) != EPERM) { 21237 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21238 err = ENOTSUP; 21239 } else if (arg != NULL) { 21240 mhioc_resv_desc_t resv_desc; 21241 if (ddi_copyin((void *)arg, &resv_desc, 21242 sizeof (mhioc_resv_desc_t), flag) != 0) { 21243 err = EFAULT; 21244 } else { 21245 err = 21246 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21247 un, SD_SCSI3_RESERVE, 21248 (uchar_t *)&resv_desc); 21249 } 21250 } 21251 } 21252 break; 21253 21254 case MHIOCGRP_PREEMPTANDABORT: 21255 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21256 if ((err = drv_priv(cred_p)) != EPERM) { 21257 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21258 err = ENOTSUP; 21259 } else if (arg != NULL) { 21260 mhioc_preemptandabort_t preempt_abort; 21261 if (ddi_copyin((void *)arg, &preempt_abort, 21262 sizeof (mhioc_preemptandabort_t), 21263 flag) != 0) { 21264 err = EFAULT; 21265 } else { 21266 err = 21267 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21268 un, SD_SCSI3_PREEMPTANDABORT, 21269 (uchar_t *)&preempt_abort); 21270 } 21271 } 21272 } 21273 break; 21274 21275 case MHIOCGRP_REGISTERANDIGNOREKEY: 21276 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21277 if ((err = drv_priv(cred_p)) != EPERM) { 21278 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21279 err = ENOTSUP; 21280 } else if (arg != NULL) { 21281 mhioc_registerandignorekey_t r_and_i; 21282 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21283 sizeof (mhioc_registerandignorekey_t), 21284 flag) != 0) { 21285 err = EFAULT; 21286 } else { 21287 err = 21288 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21289 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21290 (uchar_t *)&r_and_i); 21291 } 21292 } 21293 } 21294 break; 21295 21296 case USCSICMD: 21297 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21298 cr = ddi_get_cred(); 21299 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21300 err = EPERM; 21301 } else { 21302 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21303 } 21304 break; 21305 21306 case CDROMPAUSE: 21307 case CDROMRESUME: 21308 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21309 if (!ISCD(un)) { 21310 err = ENOTTY; 21311 } else { 21312 err = sr_pause_resume(dev, cmd); 21313 } 21314 break; 21315 21316 case CDROMPLAYMSF: 21317 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21318 if (!ISCD(un)) { 21319 err = ENOTTY; 21320 } else { 21321 err = sr_play_msf(dev, (caddr_t)arg, flag); 21322 } 21323 break; 21324 21325 case CDROMPLAYTRKIND: 21326 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21327 #if defined(__i386) || defined(__amd64) 21328 /* 21329 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21330 */ 21331 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21332 #else 21333 if (!ISCD(un)) { 21334 #endif 21335 err = ENOTTY; 21336 } else { 21337 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21338 } 21339 break; 21340 21341 case CDROMREADTOCHDR: 21342 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21343 if (!ISCD(un)) { 21344 err = ENOTTY; 21345 } else { 21346 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21347 } 21348 break; 21349 21350 case CDROMREADTOCENTRY: 21351 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21352 if (!ISCD(un)) { 21353 err = ENOTTY; 21354 } else { 21355 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21356 } 21357 break; 21358 21359 case CDROMSTOP: 21360 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21361 if (!ISCD(un)) { 21362 err = ENOTTY; 21363 } else { 21364 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21365 SD_PATH_STANDARD); 21366 } 21367 break; 21368 21369 case CDROMSTART: 21370 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21371 if (!ISCD(un)) { 21372 err = ENOTTY; 21373 } else { 21374 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21375 SD_PATH_STANDARD); 21376 } 21377 break; 21378 21379 case CDROMCLOSETRAY: 21380 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21381 if (!ISCD(un)) { 21382 err = ENOTTY; 21383 } else { 21384 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21385 SD_PATH_STANDARD); 21386 } 21387 break; 21388 21389 case FDEJECT: /* for eject command */ 21390 case DKIOCEJECT: 21391 case CDROMEJECT: 21392 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21393 if (!un->un_f_eject_media_supported) { 21394 err = ENOTTY; 21395 } else { 21396 err = sr_eject(dev); 21397 } 21398 break; 21399 21400 case CDROMVOLCTRL: 21401 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21402 if (!ISCD(un)) { 21403 err = ENOTTY; 21404 } else { 21405 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21406 } 21407 break; 21408 21409 case CDROMSUBCHNL: 21410 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21411 if (!ISCD(un)) { 21412 err = ENOTTY; 21413 } else { 21414 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21415 } 21416 break; 21417 21418 case CDROMREADMODE2: 21419 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21420 if (!ISCD(un)) { 21421 err = ENOTTY; 21422 } else if (un->un_f_cfg_is_atapi == TRUE) { 21423 /* 21424 * If the drive supports READ CD, use that instead of 21425 * switching the LBA size via a MODE SELECT 21426 * Block Descriptor 21427 */ 21428 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21429 } else { 21430 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21431 } 21432 break; 21433 21434 case CDROMREADMODE1: 21435 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21436 if (!ISCD(un)) { 21437 err = ENOTTY; 21438 } else { 21439 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21440 } 21441 break; 21442 21443 case CDROMREADOFFSET: 21444 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21445 if (!ISCD(un)) { 21446 err = ENOTTY; 21447 } else { 21448 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21449 flag); 21450 } 21451 break; 21452 21453 case CDROMSBLKMODE: 21454 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21455 /* 21456 * There is no means of changing block size in case of atapi 21457 * drives, thus return ENOTTY if drive type is atapi 21458 */ 21459 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21460 err = ENOTTY; 21461 } else if (un->un_f_mmc_cap == TRUE) { 21462 21463 /* 21464 * MMC Devices do not support changing the 21465 * logical block size 21466 * 21467 * Note: EINVAL is being returned instead of ENOTTY to 21468 * maintain consistancy with the original mmc 21469 * driver update. 21470 */ 21471 err = EINVAL; 21472 } else { 21473 mutex_enter(SD_MUTEX(un)); 21474 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21475 (un->un_ncmds_in_transport > 0)) { 21476 mutex_exit(SD_MUTEX(un)); 21477 err = EINVAL; 21478 } else { 21479 mutex_exit(SD_MUTEX(un)); 21480 err = sr_change_blkmode(dev, cmd, arg, flag); 21481 } 21482 } 21483 break; 21484 21485 case CDROMGBLKMODE: 21486 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21487 if (!ISCD(un)) { 21488 err = ENOTTY; 21489 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21490 (un->un_f_blockcount_is_valid != FALSE)) { 21491 /* 21492 * Drive is an ATAPI drive so return target block 21493 * size for ATAPI drives since we cannot change the 21494 * blocksize on ATAPI drives. Used primarily to detect 21495 * if an ATAPI cdrom is present. 21496 */ 21497 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21498 sizeof (int), flag) != 0) { 21499 err = EFAULT; 21500 } else { 21501 err = 0; 21502 } 21503 21504 } else { 21505 /* 21506 * Drive supports changing block sizes via a Mode 21507 * Select. 21508 */ 21509 err = sr_change_blkmode(dev, cmd, arg, flag); 21510 } 21511 break; 21512 21513 case CDROMGDRVSPEED: 21514 case CDROMSDRVSPEED: 21515 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21516 if (!ISCD(un)) { 21517 err = ENOTTY; 21518 } else if (un->un_f_mmc_cap == TRUE) { 21519 /* 21520 * Note: In the future the driver implementation 21521 * for getting and 21522 * setting cd speed should entail: 21523 * 1) If non-mmc try the Toshiba mode page 21524 * (sr_change_speed) 21525 * 2) If mmc but no support for Real Time Streaming try 21526 * the SET CD SPEED (0xBB) command 21527 * (sr_atapi_change_speed) 21528 * 3) If mmc and support for Real Time Streaming 21529 * try the GET PERFORMANCE and SET STREAMING 21530 * commands (not yet implemented, 4380808) 21531 */ 21532 /* 21533 * As per recent MMC spec, CD-ROM speed is variable 21534 * and changes with LBA. Since there is no such 21535 * things as drive speed now, fail this ioctl. 21536 * 21537 * Note: EINVAL is returned for consistancy of original 21538 * implementation which included support for getting 21539 * the drive speed of mmc devices but not setting 21540 * the drive speed. Thus EINVAL would be returned 21541 * if a set request was made for an mmc device. 21542 * We no longer support get or set speed for 21543 * mmc but need to remain consistant with regard 21544 * to the error code returned. 21545 */ 21546 err = EINVAL; 21547 } else if (un->un_f_cfg_is_atapi == TRUE) { 21548 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21549 } else { 21550 err = sr_change_speed(dev, cmd, arg, flag); 21551 } 21552 break; 21553 21554 case CDROMCDDA: 21555 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21556 if (!ISCD(un)) { 21557 err = ENOTTY; 21558 } else { 21559 err = sr_read_cdda(dev, (void *)arg, flag); 21560 } 21561 break; 21562 21563 case CDROMCDXA: 21564 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21565 if (!ISCD(un)) { 21566 err = ENOTTY; 21567 } else { 21568 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21569 } 21570 break; 21571 21572 case CDROMSUBCODE: 21573 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21574 if (!ISCD(un)) { 21575 err = ENOTTY; 21576 } else { 21577 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21578 } 21579 break; 21580 21581 case DKIOCPARTINFO: { 21582 /* 21583 * Return parameters describing the selected disk slice. 21584 * Note: this ioctl is for the intel platform only 21585 */ 21586 #if defined(__i386) || defined(__amd64) 21587 int part; 21588 21589 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21590 part = SDPART(dev); 21591 21592 /* don't check un_solaris_size for pN */ 21593 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21594 err = EIO; 21595 } else { 21596 struct part_info p; 21597 21598 p.p_start = (daddr_t)un->un_offset[part]; 21599 p.p_length = (int)un->un_map[part].dkl_nblk; 21600 #ifdef _MULTI_DATAMODEL 21601 switch (ddi_model_convert_from(flag & FMODELS)) { 21602 case DDI_MODEL_ILP32: 21603 { 21604 struct part_info32 p32; 21605 21606 p32.p_start = (daddr32_t)p.p_start; 21607 p32.p_length = p.p_length; 21608 if (ddi_copyout(&p32, (void *)arg, 21609 sizeof (p32), flag)) 21610 err = EFAULT; 21611 break; 21612 } 21613 21614 case DDI_MODEL_NONE: 21615 { 21616 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21617 flag)) 21618 err = EFAULT; 21619 break; 21620 } 21621 } 21622 #else /* ! _MULTI_DATAMODEL */ 21623 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21624 err = EFAULT; 21625 #endif /* _MULTI_DATAMODEL */ 21626 } 21627 #else 21628 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21629 err = ENOTTY; 21630 #endif 21631 break; 21632 } 21633 21634 case DKIOCG_PHYGEOM: { 21635 /* Return the driver's notion of the media physical geometry */ 21636 #if defined(__i386) || defined(__amd64) 21637 struct dk_geom disk_geom; 21638 struct dk_geom *dkgp = &disk_geom; 21639 21640 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21641 mutex_enter(SD_MUTEX(un)); 21642 21643 if (un->un_g.dkg_nhead != 0 && 21644 un->un_g.dkg_nsect != 0) { 21645 /* 21646 * We succeeded in getting a geometry, but 21647 * right now it is being reported as just the 21648 * Solaris fdisk partition, just like for 21649 * DKIOCGGEOM. We need to change that to be 21650 * correct for the entire disk now. 21651 */ 21652 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21653 dkgp->dkg_acyl = 0; 21654 dkgp->dkg_ncyl = un->un_blockcount / 21655 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21656 } else { 21657 bzero(dkgp, sizeof (struct dk_geom)); 21658 /* 21659 * This disk does not have a Solaris VTOC 21660 * so we must present a physical geometry 21661 * that will remain consistent regardless 21662 * of how the disk is used. This will ensure 21663 * that the geometry does not change regardless 21664 * of the fdisk partition type (ie. EFI, FAT32, 21665 * Solaris, etc). 21666 */ 21667 if (ISCD(un)) { 21668 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21669 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21670 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21671 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21672 } else { 21673 /* 21674 * Invalid un_blockcount can generate invalid 21675 * dk_geom and may result in division by zero 21676 * system failure. Should make sure blockcount 21677 * is valid before using it here. 21678 */ 21679 if (un->un_f_blockcount_is_valid == FALSE) { 21680 mutex_exit(SD_MUTEX(un)); 21681 err = EIO; 21682 21683 break; 21684 } 21685 sd_convert_geometry(un->un_blockcount, dkgp); 21686 dkgp->dkg_acyl = 0; 21687 dkgp->dkg_ncyl = un->un_blockcount / 21688 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21689 } 21690 } 21691 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21692 21693 if (ddi_copyout(dkgp, (void *)arg, 21694 sizeof (struct dk_geom), flag)) { 21695 mutex_exit(SD_MUTEX(un)); 21696 err = EFAULT; 21697 } else { 21698 mutex_exit(SD_MUTEX(un)); 21699 err = 0; 21700 } 21701 #else 21702 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21703 err = ENOTTY; 21704 #endif 21705 break; 21706 } 21707 21708 case DKIOCG_VIRTGEOM: { 21709 /* Return the driver's notion of the media's logical geometry */ 21710 #if defined(__i386) || defined(__amd64) 21711 struct dk_geom disk_geom; 21712 struct dk_geom *dkgp = &disk_geom; 21713 21714 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21715 mutex_enter(SD_MUTEX(un)); 21716 /* 21717 * If there is no HBA geometry available, or 21718 * if the HBA returned us something that doesn't 21719 * really fit into an Int 13/function 8 geometry 21720 * result, just fail the ioctl. See PSARC 1998/313. 21721 */ 21722 if (un->un_lgeom.g_nhead == 0 || 21723 un->un_lgeom.g_nsect == 0 || 21724 un->un_lgeom.g_ncyl > 1024) { 21725 mutex_exit(SD_MUTEX(un)); 21726 err = EINVAL; 21727 } else { 21728 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21729 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21730 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21731 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21732 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21733 21734 if (ddi_copyout(dkgp, (void *)arg, 21735 sizeof (struct dk_geom), flag)) { 21736 mutex_exit(SD_MUTEX(un)); 21737 err = EFAULT; 21738 } else { 21739 mutex_exit(SD_MUTEX(un)); 21740 err = 0; 21741 } 21742 } 21743 #else 21744 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21745 err = ENOTTY; 21746 #endif 21747 break; 21748 } 21749 #ifdef SDDEBUG 21750 /* RESET/ABORTS testing ioctls */ 21751 case DKIOCRESET: { 21752 int reset_level; 21753 21754 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21755 err = EFAULT; 21756 } else { 21757 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21758 "reset_level = 0x%lx\n", reset_level); 21759 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21760 err = 0; 21761 } else { 21762 err = EIO; 21763 } 21764 } 21765 break; 21766 } 21767 21768 case DKIOCABORT: 21769 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21770 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21771 err = 0; 21772 } else { 21773 err = EIO; 21774 } 21775 break; 21776 #endif 21777 21778 #ifdef SD_FAULT_INJECTION 21779 /* SDIOC FaultInjection testing ioctls */ 21780 case SDIOCSTART: 21781 case SDIOCSTOP: 21782 case SDIOCINSERTPKT: 21783 case SDIOCINSERTXB: 21784 case SDIOCINSERTUN: 21785 case SDIOCINSERTARQ: 21786 case SDIOCPUSH: 21787 case SDIOCRETRIEVE: 21788 case SDIOCRUN: 21789 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21790 "SDIOC detected cmd:0x%X:\n", cmd); 21791 /* call error generator */ 21792 sd_faultinjection_ioctl(cmd, arg, un); 21793 err = 0; 21794 break; 21795 21796 #endif /* SD_FAULT_INJECTION */ 21797 21798 case DKIOCFLUSHWRITECACHE: 21799 { 21800 struct dk_callback *dkc = (struct dk_callback *)arg; 21801 21802 mutex_enter(SD_MUTEX(un)); 21803 if (!un->un_f_sync_cache_supported || 21804 !un->un_f_write_cache_enabled) { 21805 err = un->un_f_sync_cache_supported ? 21806 0 : ENOTSUP; 21807 mutex_exit(SD_MUTEX(un)); 21808 if ((flag & FKIOCTL) && dkc != NULL && 21809 dkc->dkc_callback != NULL) { 21810 (*dkc->dkc_callback)(dkc->dkc_cookie, 21811 err); 21812 /* 21813 * Did callback and reported error. 21814 * Since we did a callback, ioctl 21815 * should return 0. 21816 */ 21817 err = 0; 21818 } 21819 break; 21820 } 21821 mutex_exit(SD_MUTEX(un)); 21822 21823 if ((flag & FKIOCTL) && dkc != NULL && 21824 dkc->dkc_callback != NULL) { 21825 /* async SYNC CACHE request */ 21826 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 21827 } else { 21828 /* synchronous SYNC CACHE request */ 21829 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21830 } 21831 } 21832 break; 21833 21834 case DKIOCGETWCE: { 21835 21836 int wce; 21837 21838 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 21839 break; 21840 } 21841 21842 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 21843 err = EFAULT; 21844 } 21845 break; 21846 } 21847 21848 case DKIOCSETWCE: { 21849 21850 int wce, sync_supported; 21851 21852 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 21853 err = EFAULT; 21854 break; 21855 } 21856 21857 /* 21858 * Synchronize multiple threads trying to enable 21859 * or disable the cache via the un_f_wcc_cv 21860 * condition variable. 21861 */ 21862 mutex_enter(SD_MUTEX(un)); 21863 21864 /* 21865 * Don't allow the cache to be enabled if the 21866 * config file has it disabled. 21867 */ 21868 if (un->un_f_opt_disable_cache && wce) { 21869 mutex_exit(SD_MUTEX(un)); 21870 err = EINVAL; 21871 break; 21872 } 21873 21874 /* 21875 * Wait for write cache change in progress 21876 * bit to be clear before proceeding. 21877 */ 21878 while (un->un_f_wcc_inprog) 21879 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 21880 21881 un->un_f_wcc_inprog = 1; 21882 21883 if (un->un_f_write_cache_enabled && wce == 0) { 21884 /* 21885 * Disable the write cache. Don't clear 21886 * un_f_write_cache_enabled until after 21887 * the mode select and flush are complete. 21888 */ 21889 sync_supported = un->un_f_sync_cache_supported; 21890 mutex_exit(SD_MUTEX(un)); 21891 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 21892 SD_CACHE_DISABLE)) == 0 && sync_supported) { 21893 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21894 } 21895 21896 mutex_enter(SD_MUTEX(un)); 21897 if (err == 0) { 21898 un->un_f_write_cache_enabled = 0; 21899 } 21900 21901 } else if (!un->un_f_write_cache_enabled && wce != 0) { 21902 /* 21903 * Set un_f_write_cache_enabled first, so there is 21904 * no window where the cache is enabled, but the 21905 * bit says it isn't. 21906 */ 21907 un->un_f_write_cache_enabled = 1; 21908 mutex_exit(SD_MUTEX(un)); 21909 21910 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 21911 SD_CACHE_ENABLE); 21912 21913 mutex_enter(SD_MUTEX(un)); 21914 21915 if (err) { 21916 un->un_f_write_cache_enabled = 0; 21917 } 21918 } 21919 21920 un->un_f_wcc_inprog = 0; 21921 cv_broadcast(&un->un_wcc_cv); 21922 mutex_exit(SD_MUTEX(un)); 21923 break; 21924 } 21925 21926 default: 21927 err = ENOTTY; 21928 break; 21929 } 21930 mutex_enter(SD_MUTEX(un)); 21931 un->un_ncmds_in_driver--; 21932 ASSERT(un->un_ncmds_in_driver >= 0); 21933 mutex_exit(SD_MUTEX(un)); 21934 21935 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 21936 return (err); 21937 } 21938 21939 21940 /* 21941 * Function: sd_uscsi_ioctl 21942 * 21943 * Description: This routine is the driver entry point for handling USCSI ioctl 21944 * requests (USCSICMD). 21945 * 21946 * Arguments: dev - the device number 21947 * arg - user provided scsi command 21948 * flag - this argument is a pass through to ddi_copyxxx() 21949 * directly from the mode argument of ioctl(). 21950 * 21951 * Return Code: code returned by sd_send_scsi_cmd 21952 * ENXIO 21953 * EFAULT 21954 * EAGAIN 21955 */ 21956 21957 static int 21958 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 21959 { 21960 #ifdef _MULTI_DATAMODEL 21961 /* 21962 * For use when a 32 bit app makes a call into a 21963 * 64 bit ioctl 21964 */ 21965 struct uscsi_cmd32 uscsi_cmd_32_for_64; 21966 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 21967 model_t model; 21968 #endif /* _MULTI_DATAMODEL */ 21969 struct uscsi_cmd *scmd = NULL; 21970 struct sd_lun *un = NULL; 21971 enum uio_seg uioseg; 21972 char cdb[CDB_GROUP0]; 21973 int rval = 0; 21974 21975 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21976 return (ENXIO); 21977 } 21978 21979 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 21980 21981 scmd = (struct uscsi_cmd *) 21982 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21983 21984 #ifdef _MULTI_DATAMODEL 21985 switch (model = ddi_model_convert_from(flag & FMODELS)) { 21986 case DDI_MODEL_ILP32: 21987 { 21988 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 21989 rval = EFAULT; 21990 goto done; 21991 } 21992 /* 21993 * Convert the ILP32 uscsi data from the 21994 * application to LP64 for internal use. 21995 */ 21996 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 21997 break; 21998 } 21999 case DDI_MODEL_NONE: 22000 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22001 rval = EFAULT; 22002 goto done; 22003 } 22004 break; 22005 } 22006 #else /* ! _MULTI_DATAMODEL */ 22007 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22008 rval = EFAULT; 22009 goto done; 22010 } 22011 #endif /* _MULTI_DATAMODEL */ 22012 22013 scmd->uscsi_flags &= ~USCSI_NOINTR; 22014 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22015 if (un->un_f_format_in_progress == TRUE) { 22016 rval = EAGAIN; 22017 goto done; 22018 } 22019 22020 /* 22021 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22022 * we will have a valid cdb[0] to test. 22023 */ 22024 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22025 (cdb[0] == SCMD_FORMAT)) { 22026 SD_TRACE(SD_LOG_IOCTL, un, 22027 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22028 mutex_enter(SD_MUTEX(un)); 22029 un->un_f_format_in_progress = TRUE; 22030 mutex_exit(SD_MUTEX(un)); 22031 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22032 SD_PATH_STANDARD); 22033 mutex_enter(SD_MUTEX(un)); 22034 un->un_f_format_in_progress = FALSE; 22035 mutex_exit(SD_MUTEX(un)); 22036 } else { 22037 SD_TRACE(SD_LOG_IOCTL, un, 22038 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22039 /* 22040 * It's OK to fall into here even if the ddi_copyin() 22041 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22042 * does this same copyin and will return the EFAULT 22043 * if it fails. 22044 */ 22045 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22046 SD_PATH_STANDARD); 22047 } 22048 #ifdef _MULTI_DATAMODEL 22049 switch (model) { 22050 case DDI_MODEL_ILP32: 22051 /* 22052 * Convert back to ILP32 before copyout to the 22053 * application 22054 */ 22055 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22056 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22057 if (rval != 0) { 22058 rval = EFAULT; 22059 } 22060 } 22061 break; 22062 case DDI_MODEL_NONE: 22063 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22064 if (rval != 0) { 22065 rval = EFAULT; 22066 } 22067 } 22068 break; 22069 } 22070 #else /* ! _MULTI_DATAMODE */ 22071 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22072 if (rval != 0) { 22073 rval = EFAULT; 22074 } 22075 } 22076 #endif /* _MULTI_DATAMODE */ 22077 done: 22078 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22079 22080 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22081 22082 return (rval); 22083 } 22084 22085 22086 /* 22087 * Function: sd_dkio_ctrl_info 22088 * 22089 * Description: This routine is the driver entry point for handling controller 22090 * information ioctl requests (DKIOCINFO). 22091 * 22092 * Arguments: dev - the device number 22093 * arg - pointer to user provided dk_cinfo structure 22094 * specifying the controller type and attributes. 22095 * flag - this argument is a pass through to ddi_copyxxx() 22096 * directly from the mode argument of ioctl(). 22097 * 22098 * Return Code: 0 22099 * EFAULT 22100 * ENXIO 22101 */ 22102 22103 static int 22104 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22105 { 22106 struct sd_lun *un = NULL; 22107 struct dk_cinfo *info; 22108 dev_info_t *pdip; 22109 int lun, tgt; 22110 22111 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22112 return (ENXIO); 22113 } 22114 22115 info = (struct dk_cinfo *) 22116 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22117 22118 switch (un->un_ctype) { 22119 case CTYPE_CDROM: 22120 info->dki_ctype = DKC_CDROM; 22121 break; 22122 default: 22123 info->dki_ctype = DKC_SCSI_CCS; 22124 break; 22125 } 22126 pdip = ddi_get_parent(SD_DEVINFO(un)); 22127 info->dki_cnum = ddi_get_instance(pdip); 22128 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22129 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22130 } else { 22131 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22132 DK_DEVLEN - 1); 22133 } 22134 22135 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22136 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22137 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22138 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22139 22140 /* Unit Information */ 22141 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22142 info->dki_slave = ((tgt << 3) | lun); 22143 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22144 DK_DEVLEN - 1); 22145 info->dki_flags = DKI_FMTVOL; 22146 info->dki_partition = SDPART(dev); 22147 22148 /* Max Transfer size of this device in blocks */ 22149 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22150 info->dki_addr = 0; 22151 info->dki_space = 0; 22152 info->dki_prio = 0; 22153 info->dki_vec = 0; 22154 22155 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22156 kmem_free(info, sizeof (struct dk_cinfo)); 22157 return (EFAULT); 22158 } else { 22159 kmem_free(info, sizeof (struct dk_cinfo)); 22160 return (0); 22161 } 22162 } 22163 22164 22165 /* 22166 * Function: sd_get_media_info 22167 * 22168 * Description: This routine is the driver entry point for handling ioctl 22169 * requests for the media type or command set profile used by the 22170 * drive to operate on the media (DKIOCGMEDIAINFO). 22171 * 22172 * Arguments: dev - the device number 22173 * arg - pointer to user provided dk_minfo structure 22174 * specifying the media type, logical block size and 22175 * drive capacity. 22176 * flag - this argument is a pass through to ddi_copyxxx() 22177 * directly from the mode argument of ioctl(). 22178 * 22179 * Return Code: 0 22180 * EACCESS 22181 * EFAULT 22182 * ENXIO 22183 * EIO 22184 */ 22185 22186 static int 22187 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22188 { 22189 struct sd_lun *un = NULL; 22190 struct uscsi_cmd com; 22191 struct scsi_inquiry *sinq; 22192 struct dk_minfo media_info; 22193 u_longlong_t media_capacity; 22194 uint64_t capacity; 22195 uint_t lbasize; 22196 uchar_t *out_data; 22197 uchar_t *rqbuf; 22198 int rval = 0; 22199 int rtn; 22200 22201 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22202 (un->un_state == SD_STATE_OFFLINE)) { 22203 return (ENXIO); 22204 } 22205 22206 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22207 22208 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22209 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22210 22211 /* Issue a TUR to determine if the drive is ready with media present */ 22212 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22213 if (rval == ENXIO) { 22214 goto done; 22215 } 22216 22217 /* Now get configuration data */ 22218 if (ISCD(un)) { 22219 media_info.dki_media_type = DK_CDROM; 22220 22221 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22222 if (un->un_f_mmc_cap == TRUE) { 22223 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22224 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22225 22226 if (rtn) { 22227 /* 22228 * Failed for other than an illegal request 22229 * or command not supported 22230 */ 22231 if ((com.uscsi_status == STATUS_CHECK) && 22232 (com.uscsi_rqstatus == STATUS_GOOD)) { 22233 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22234 (rqbuf[12] != 0x20)) { 22235 rval = EIO; 22236 goto done; 22237 } 22238 } 22239 } else { 22240 /* 22241 * The GET CONFIGURATION command succeeded 22242 * so set the media type according to the 22243 * returned data 22244 */ 22245 media_info.dki_media_type = out_data[6]; 22246 media_info.dki_media_type <<= 8; 22247 media_info.dki_media_type |= out_data[7]; 22248 } 22249 } 22250 } else { 22251 /* 22252 * The profile list is not available, so we attempt to identify 22253 * the media type based on the inquiry data 22254 */ 22255 sinq = un->un_sd->sd_inq; 22256 if (sinq->inq_qual == 0) { 22257 /* This is a direct access device */ 22258 media_info.dki_media_type = DK_FIXED_DISK; 22259 22260 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22261 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22262 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22263 media_info.dki_media_type = DK_ZIP; 22264 } else if ( 22265 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22266 media_info.dki_media_type = DK_JAZ; 22267 } 22268 } 22269 } else { 22270 /* Not a CD or direct access so return unknown media */ 22271 media_info.dki_media_type = DK_UNKNOWN; 22272 } 22273 } 22274 22275 /* Now read the capacity so we can provide the lbasize and capacity */ 22276 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22277 SD_PATH_DIRECT)) { 22278 case 0: 22279 break; 22280 case EACCES: 22281 rval = EACCES; 22282 goto done; 22283 default: 22284 rval = EIO; 22285 goto done; 22286 } 22287 22288 media_info.dki_lbsize = lbasize; 22289 media_capacity = capacity; 22290 22291 /* 22292 * sd_send_scsi_READ_CAPACITY() reports capacity in 22293 * un->un_sys_blocksize chunks. So we need to convert it into 22294 * cap.lbasize chunks. 22295 */ 22296 media_capacity *= un->un_sys_blocksize; 22297 media_capacity /= lbasize; 22298 media_info.dki_capacity = media_capacity; 22299 22300 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22301 rval = EFAULT; 22302 /* Put goto. Anybody might add some code below in future */ 22303 goto done; 22304 } 22305 done: 22306 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22307 kmem_free(rqbuf, SENSE_LENGTH); 22308 return (rval); 22309 } 22310 22311 22312 /* 22313 * Function: sd_dkio_get_geometry 22314 * 22315 * Description: This routine is the driver entry point for handling user 22316 * requests to get the device geometry (DKIOCGGEOM). 22317 * 22318 * Arguments: dev - the device number 22319 * arg - pointer to user provided dk_geom structure specifying 22320 * the controller's notion of the current geometry. 22321 * flag - this argument is a pass through to ddi_copyxxx() 22322 * directly from the mode argument of ioctl(). 22323 * geom_validated - flag indicating if the device geometry has been 22324 * previously validated in the sdioctl routine. 22325 * 22326 * Return Code: 0 22327 * EFAULT 22328 * ENXIO 22329 * EIO 22330 */ 22331 22332 static int 22333 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22334 { 22335 struct sd_lun *un = NULL; 22336 struct dk_geom *tmp_geom = NULL; 22337 int rval = 0; 22338 22339 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22340 return (ENXIO); 22341 } 22342 22343 #if defined(__i386) || defined(__amd64) 22344 if (un->un_solaris_size == 0) { 22345 return (EIO); 22346 } 22347 #endif 22348 if (geom_validated == FALSE) { 22349 /* 22350 * sd_validate_geometry does not spin a disk up 22351 * if it was spun down. We need to make sure it 22352 * is ready. 22353 */ 22354 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22355 return (rval); 22356 } 22357 mutex_enter(SD_MUTEX(un)); 22358 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22359 mutex_exit(SD_MUTEX(un)); 22360 } 22361 if (rval) 22362 return (rval); 22363 22364 /* 22365 * Make a local copy of the soft state geometry to avoid some potential 22366 * race conditions associated with holding the mutex and updating the 22367 * write_reinstruct value 22368 */ 22369 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22370 mutex_enter(SD_MUTEX(un)); 22371 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22372 mutex_exit(SD_MUTEX(un)); 22373 22374 if (tmp_geom->dkg_write_reinstruct == 0) { 22375 tmp_geom->dkg_write_reinstruct = 22376 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22377 sd_rot_delay) / (int)60000); 22378 } 22379 22380 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22381 flag); 22382 if (rval != 0) { 22383 rval = EFAULT; 22384 } 22385 22386 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22387 return (rval); 22388 22389 } 22390 22391 22392 /* 22393 * Function: sd_dkio_set_geometry 22394 * 22395 * Description: This routine is the driver entry point for handling user 22396 * requests to set the device geometry (DKIOCSGEOM). The actual 22397 * device geometry is not updated, just the driver "notion" of it. 22398 * 22399 * Arguments: dev - the device number 22400 * arg - pointer to user provided dk_geom structure used to set 22401 * the controller's notion of the current geometry. 22402 * flag - this argument is a pass through to ddi_copyxxx() 22403 * directly from the mode argument of ioctl(). 22404 * 22405 * Return Code: 0 22406 * EFAULT 22407 * ENXIO 22408 * EIO 22409 */ 22410 22411 static int 22412 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22413 { 22414 struct sd_lun *un = NULL; 22415 struct dk_geom *tmp_geom; 22416 struct dk_map *lp; 22417 int rval = 0; 22418 int i; 22419 22420 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22421 return (ENXIO); 22422 } 22423 22424 #if defined(__i386) || defined(__amd64) 22425 if (un->un_solaris_size == 0) { 22426 return (EIO); 22427 } 22428 #endif 22429 /* 22430 * We need to copy the user specified geometry into local 22431 * storage and then update the softstate. We don't want to hold 22432 * the mutex and copyin directly from the user to the soft state 22433 */ 22434 tmp_geom = (struct dk_geom *) 22435 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22436 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22437 if (rval != 0) { 22438 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22439 return (EFAULT); 22440 } 22441 22442 mutex_enter(SD_MUTEX(un)); 22443 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22444 for (i = 0; i < NDKMAP; i++) { 22445 lp = &un->un_map[i]; 22446 un->un_offset[i] = 22447 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22448 #if defined(__i386) || defined(__amd64) 22449 un->un_offset[i] += un->un_solaris_offset; 22450 #endif 22451 } 22452 un->un_f_geometry_is_valid = FALSE; 22453 mutex_exit(SD_MUTEX(un)); 22454 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22455 22456 return (rval); 22457 } 22458 22459 22460 /* 22461 * Function: sd_dkio_get_partition 22462 * 22463 * Description: This routine is the driver entry point for handling user 22464 * requests to get the partition table (DKIOCGAPART). 22465 * 22466 * Arguments: dev - the device number 22467 * arg - pointer to user provided dk_allmap structure specifying 22468 * the controller's notion of the current partition table. 22469 * flag - this argument is a pass through to ddi_copyxxx() 22470 * directly from the mode argument of ioctl(). 22471 * geom_validated - flag indicating if the device geometry has been 22472 * previously validated in the sdioctl routine. 22473 * 22474 * Return Code: 0 22475 * EFAULT 22476 * ENXIO 22477 * EIO 22478 */ 22479 22480 static int 22481 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22482 { 22483 struct sd_lun *un = NULL; 22484 int rval = 0; 22485 int size; 22486 22487 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22488 return (ENXIO); 22489 } 22490 22491 #if defined(__i386) || defined(__amd64) 22492 if (un->un_solaris_size == 0) { 22493 return (EIO); 22494 } 22495 #endif 22496 /* 22497 * Make sure the geometry is valid before getting the partition 22498 * information. 22499 */ 22500 mutex_enter(SD_MUTEX(un)); 22501 if (geom_validated == FALSE) { 22502 /* 22503 * sd_validate_geometry does not spin a disk up 22504 * if it was spun down. We need to make sure it 22505 * is ready before validating the geometry. 22506 */ 22507 mutex_exit(SD_MUTEX(un)); 22508 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22509 return (rval); 22510 } 22511 mutex_enter(SD_MUTEX(un)); 22512 22513 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22514 mutex_exit(SD_MUTEX(un)); 22515 return (rval); 22516 } 22517 } 22518 mutex_exit(SD_MUTEX(un)); 22519 22520 #ifdef _MULTI_DATAMODEL 22521 switch (ddi_model_convert_from(flag & FMODELS)) { 22522 case DDI_MODEL_ILP32: { 22523 struct dk_map32 dk_map32[NDKMAP]; 22524 int i; 22525 22526 for (i = 0; i < NDKMAP; i++) { 22527 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22528 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22529 } 22530 size = NDKMAP * sizeof (struct dk_map32); 22531 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22532 if (rval != 0) { 22533 rval = EFAULT; 22534 } 22535 break; 22536 } 22537 case DDI_MODEL_NONE: 22538 size = NDKMAP * sizeof (struct dk_map); 22539 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22540 if (rval != 0) { 22541 rval = EFAULT; 22542 } 22543 break; 22544 } 22545 #else /* ! _MULTI_DATAMODEL */ 22546 size = NDKMAP * sizeof (struct dk_map); 22547 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22548 if (rval != 0) { 22549 rval = EFAULT; 22550 } 22551 #endif /* _MULTI_DATAMODEL */ 22552 return (rval); 22553 } 22554 22555 22556 /* 22557 * Function: sd_dkio_set_partition 22558 * 22559 * Description: This routine is the driver entry point for handling user 22560 * requests to set the partition table (DKIOCSAPART). The actual 22561 * device partition is not updated. 22562 * 22563 * Arguments: dev - the device number 22564 * arg - pointer to user provided dk_allmap structure used to set 22565 * the controller's notion of the partition table. 22566 * flag - this argument is a pass through to ddi_copyxxx() 22567 * directly from the mode argument of ioctl(). 22568 * 22569 * Return Code: 0 22570 * EINVAL 22571 * EFAULT 22572 * ENXIO 22573 * EIO 22574 */ 22575 22576 static int 22577 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22578 { 22579 struct sd_lun *un = NULL; 22580 struct dk_map dk_map[NDKMAP]; 22581 struct dk_map *lp; 22582 int rval = 0; 22583 int size; 22584 int i; 22585 #if defined(_SUNOS_VTOC_16) 22586 struct dkl_partition *vp; 22587 #endif 22588 22589 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22590 return (ENXIO); 22591 } 22592 22593 /* 22594 * Set the map for all logical partitions. We lock 22595 * the priority just to make sure an interrupt doesn't 22596 * come in while the map is half updated. 22597 */ 22598 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22599 mutex_enter(SD_MUTEX(un)); 22600 if (un->un_blockcount > DK_MAX_BLOCKS) { 22601 mutex_exit(SD_MUTEX(un)); 22602 return (ENOTSUP); 22603 } 22604 mutex_exit(SD_MUTEX(un)); 22605 if (un->un_solaris_size == 0) { 22606 return (EIO); 22607 } 22608 22609 #ifdef _MULTI_DATAMODEL 22610 switch (ddi_model_convert_from(flag & FMODELS)) { 22611 case DDI_MODEL_ILP32: { 22612 struct dk_map32 dk_map32[NDKMAP]; 22613 22614 size = NDKMAP * sizeof (struct dk_map32); 22615 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22616 if (rval != 0) { 22617 return (EFAULT); 22618 } 22619 for (i = 0; i < NDKMAP; i++) { 22620 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22621 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22622 } 22623 break; 22624 } 22625 case DDI_MODEL_NONE: 22626 size = NDKMAP * sizeof (struct dk_map); 22627 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22628 if (rval != 0) { 22629 return (EFAULT); 22630 } 22631 break; 22632 } 22633 #else /* ! _MULTI_DATAMODEL */ 22634 size = NDKMAP * sizeof (struct dk_map); 22635 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22636 if (rval != 0) { 22637 return (EFAULT); 22638 } 22639 #endif /* _MULTI_DATAMODEL */ 22640 22641 mutex_enter(SD_MUTEX(un)); 22642 /* Note: The size used in this bcopy is set based upon the data model */ 22643 bcopy(dk_map, un->un_map, size); 22644 #if defined(_SUNOS_VTOC_16) 22645 vp = (struct dkl_partition *)&(un->un_vtoc); 22646 #endif /* defined(_SUNOS_VTOC_16) */ 22647 for (i = 0; i < NDKMAP; i++) { 22648 lp = &un->un_map[i]; 22649 un->un_offset[i] = 22650 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22651 #if defined(_SUNOS_VTOC_16) 22652 vp->p_start = un->un_offset[i]; 22653 vp->p_size = lp->dkl_nblk; 22654 vp++; 22655 #endif /* defined(_SUNOS_VTOC_16) */ 22656 #if defined(__i386) || defined(__amd64) 22657 un->un_offset[i] += un->un_solaris_offset; 22658 #endif 22659 } 22660 mutex_exit(SD_MUTEX(un)); 22661 return (rval); 22662 } 22663 22664 22665 /* 22666 * Function: sd_dkio_get_vtoc 22667 * 22668 * Description: This routine is the driver entry point for handling user 22669 * requests to get the current volume table of contents 22670 * (DKIOCGVTOC). 22671 * 22672 * Arguments: dev - the device number 22673 * arg - pointer to user provided vtoc structure specifying 22674 * the current vtoc. 22675 * flag - this argument is a pass through to ddi_copyxxx() 22676 * directly from the mode argument of ioctl(). 22677 * geom_validated - flag indicating if the device geometry has been 22678 * previously validated in the sdioctl routine. 22679 * 22680 * Return Code: 0 22681 * EFAULT 22682 * ENXIO 22683 * EIO 22684 */ 22685 22686 static int 22687 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22688 { 22689 struct sd_lun *un = NULL; 22690 #if defined(_SUNOS_VTOC_8) 22691 struct vtoc user_vtoc; 22692 #endif /* defined(_SUNOS_VTOC_8) */ 22693 int rval = 0; 22694 22695 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22696 return (ENXIO); 22697 } 22698 22699 mutex_enter(SD_MUTEX(un)); 22700 if (geom_validated == FALSE) { 22701 /* 22702 * sd_validate_geometry does not spin a disk up 22703 * if it was spun down. We need to make sure it 22704 * is ready. 22705 */ 22706 mutex_exit(SD_MUTEX(un)); 22707 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22708 return (rval); 22709 } 22710 mutex_enter(SD_MUTEX(un)); 22711 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22712 mutex_exit(SD_MUTEX(un)); 22713 return (rval); 22714 } 22715 } 22716 22717 #if defined(_SUNOS_VTOC_8) 22718 sd_build_user_vtoc(un, &user_vtoc); 22719 mutex_exit(SD_MUTEX(un)); 22720 22721 #ifdef _MULTI_DATAMODEL 22722 switch (ddi_model_convert_from(flag & FMODELS)) { 22723 case DDI_MODEL_ILP32: { 22724 struct vtoc32 user_vtoc32; 22725 22726 vtoctovtoc32(user_vtoc, user_vtoc32); 22727 if (ddi_copyout(&user_vtoc32, (void *)arg, 22728 sizeof (struct vtoc32), flag)) { 22729 return (EFAULT); 22730 } 22731 break; 22732 } 22733 22734 case DDI_MODEL_NONE: 22735 if (ddi_copyout(&user_vtoc, (void *)arg, 22736 sizeof (struct vtoc), flag)) { 22737 return (EFAULT); 22738 } 22739 break; 22740 } 22741 #else /* ! _MULTI_DATAMODEL */ 22742 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22743 return (EFAULT); 22744 } 22745 #endif /* _MULTI_DATAMODEL */ 22746 22747 #elif defined(_SUNOS_VTOC_16) 22748 mutex_exit(SD_MUTEX(un)); 22749 22750 #ifdef _MULTI_DATAMODEL 22751 /* 22752 * The un_vtoc structure is a "struct dk_vtoc" which is always 22753 * 32-bit to maintain compatibility with existing on-disk 22754 * structures. Thus, we need to convert the structure when copying 22755 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22756 * program. If the target is a 32-bit program, then no conversion 22757 * is necessary. 22758 */ 22759 /* LINTED: logical expression always true: op "||" */ 22760 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22761 switch (ddi_model_convert_from(flag & FMODELS)) { 22762 case DDI_MODEL_ILP32: 22763 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22764 sizeof (un->un_vtoc), flag)) { 22765 return (EFAULT); 22766 } 22767 break; 22768 22769 case DDI_MODEL_NONE: { 22770 struct vtoc user_vtoc; 22771 22772 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22773 if (ddi_copyout(&user_vtoc, (void *)arg, 22774 sizeof (struct vtoc), flag)) { 22775 return (EFAULT); 22776 } 22777 break; 22778 } 22779 } 22780 #else /* ! _MULTI_DATAMODEL */ 22781 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22782 flag)) { 22783 return (EFAULT); 22784 } 22785 #endif /* _MULTI_DATAMODEL */ 22786 #else 22787 #error "No VTOC format defined." 22788 #endif 22789 22790 return (rval); 22791 } 22792 22793 static int 22794 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22795 { 22796 struct sd_lun *un = NULL; 22797 dk_efi_t user_efi; 22798 int rval = 0; 22799 void *buffer; 22800 22801 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22802 return (ENXIO); 22803 22804 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22805 return (EFAULT); 22806 22807 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22808 22809 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22810 (user_efi.dki_length > un->un_max_xfer_size)) 22811 return (EINVAL); 22812 22813 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22814 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22815 user_efi.dki_lba, SD_PATH_DIRECT); 22816 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22817 user_efi.dki_length, flag) != 0) 22818 rval = EFAULT; 22819 22820 kmem_free(buffer, user_efi.dki_length); 22821 return (rval); 22822 } 22823 22824 /* 22825 * Function: sd_build_user_vtoc 22826 * 22827 * Description: This routine populates a pass by reference variable with the 22828 * current volume table of contents. 22829 * 22830 * Arguments: un - driver soft state (unit) structure 22831 * user_vtoc - pointer to vtoc structure to be populated 22832 */ 22833 22834 static void 22835 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22836 { 22837 struct dk_map2 *lpart; 22838 struct dk_map *lmap; 22839 struct partition *vpart; 22840 int nblks; 22841 int i; 22842 22843 ASSERT(mutex_owned(SD_MUTEX(un))); 22844 22845 /* 22846 * Return vtoc structure fields in the provided VTOC area, addressed 22847 * by *vtoc. 22848 */ 22849 bzero(user_vtoc, sizeof (struct vtoc)); 22850 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 22851 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 22852 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 22853 user_vtoc->v_sanity = VTOC_SANE; 22854 user_vtoc->v_version = un->un_vtoc.v_version; 22855 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 22856 user_vtoc->v_sectorsz = un->un_sys_blocksize; 22857 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 22858 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 22859 sizeof (un->un_vtoc.v_reserved)); 22860 /* 22861 * Convert partitioning information. 22862 * 22863 * Note the conversion from starting cylinder number 22864 * to starting sector number. 22865 */ 22866 lmap = un->un_map; 22867 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 22868 vpart = user_vtoc->v_part; 22869 22870 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22871 22872 for (i = 0; i < V_NUMPAR; i++) { 22873 vpart->p_tag = lpart->p_tag; 22874 vpart->p_flag = lpart->p_flag; 22875 vpart->p_start = lmap->dkl_cylno * nblks; 22876 vpart->p_size = lmap->dkl_nblk; 22877 lmap++; 22878 lpart++; 22879 vpart++; 22880 22881 /* (4364927) */ 22882 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 22883 } 22884 22885 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 22886 } 22887 22888 static int 22889 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 22890 { 22891 struct sd_lun *un = NULL; 22892 struct partition64 p64; 22893 int rval = 0; 22894 uint_t nparts; 22895 efi_gpe_t *partitions; 22896 efi_gpt_t *buffer; 22897 diskaddr_t gpe_lba; 22898 22899 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22900 return (ENXIO); 22901 } 22902 22903 if (ddi_copyin((const void *)arg, &p64, 22904 sizeof (struct partition64), flag)) { 22905 return (EFAULT); 22906 } 22907 22908 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 22909 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 22910 1, SD_PATH_DIRECT); 22911 if (rval != 0) 22912 goto done_error; 22913 22914 sd_swap_efi_gpt(buffer); 22915 22916 if ((rval = sd_validate_efi(buffer)) != 0) 22917 goto done_error; 22918 22919 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 22920 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 22921 if (p64.p_partno > nparts) { 22922 /* couldn't find it */ 22923 rval = ESRCH; 22924 goto done_error; 22925 } 22926 /* 22927 * if we're dealing with a partition that's out of the normal 22928 * 16K block, adjust accordingly 22929 */ 22930 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 22931 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 22932 gpe_lba, SD_PATH_DIRECT); 22933 if (rval) { 22934 goto done_error; 22935 } 22936 partitions = (efi_gpe_t *)buffer; 22937 22938 sd_swap_efi_gpe(nparts, partitions); 22939 22940 partitions += p64.p_partno; 22941 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 22942 sizeof (struct uuid)); 22943 p64.p_start = partitions->efi_gpe_StartingLBA; 22944 p64.p_size = partitions->efi_gpe_EndingLBA - 22945 p64.p_start + 1; 22946 22947 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 22948 rval = EFAULT; 22949 22950 done_error: 22951 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 22952 return (rval); 22953 } 22954 22955 22956 /* 22957 * Function: sd_dkio_set_vtoc 22958 * 22959 * Description: This routine is the driver entry point for handling user 22960 * requests to set the current volume table of contents 22961 * (DKIOCSVTOC). 22962 * 22963 * Arguments: dev - the device number 22964 * arg - pointer to user provided vtoc structure used to set the 22965 * current vtoc. 22966 * flag - this argument is a pass through to ddi_copyxxx() 22967 * directly from the mode argument of ioctl(). 22968 * 22969 * Return Code: 0 22970 * EFAULT 22971 * ENXIO 22972 * EINVAL 22973 * ENOTSUP 22974 */ 22975 22976 static int 22977 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 22978 { 22979 struct sd_lun *un = NULL; 22980 struct vtoc user_vtoc; 22981 int rval = 0; 22982 22983 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22984 return (ENXIO); 22985 } 22986 22987 #if defined(__i386) || defined(__amd64) 22988 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 22989 return (EINVAL); 22990 } 22991 #endif 22992 22993 #ifdef _MULTI_DATAMODEL 22994 switch (ddi_model_convert_from(flag & FMODELS)) { 22995 case DDI_MODEL_ILP32: { 22996 struct vtoc32 user_vtoc32; 22997 22998 if (ddi_copyin((const void *)arg, &user_vtoc32, 22999 sizeof (struct vtoc32), flag)) { 23000 return (EFAULT); 23001 } 23002 vtoc32tovtoc(user_vtoc32, user_vtoc); 23003 break; 23004 } 23005 23006 case DDI_MODEL_NONE: 23007 if (ddi_copyin((const void *)arg, &user_vtoc, 23008 sizeof (struct vtoc), flag)) { 23009 return (EFAULT); 23010 } 23011 break; 23012 } 23013 #else /* ! _MULTI_DATAMODEL */ 23014 if (ddi_copyin((const void *)arg, &user_vtoc, 23015 sizeof (struct vtoc), flag)) { 23016 return (EFAULT); 23017 } 23018 #endif /* _MULTI_DATAMODEL */ 23019 23020 mutex_enter(SD_MUTEX(un)); 23021 if (un->un_blockcount > DK_MAX_BLOCKS) { 23022 mutex_exit(SD_MUTEX(un)); 23023 return (ENOTSUP); 23024 } 23025 if (un->un_g.dkg_ncyl == 0) { 23026 mutex_exit(SD_MUTEX(un)); 23027 return (EINVAL); 23028 } 23029 23030 mutex_exit(SD_MUTEX(un)); 23031 sd_clear_efi(un); 23032 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23033 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23034 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23035 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23036 un->un_node_type, NULL); 23037 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23038 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23039 un->un_node_type, NULL); 23040 mutex_enter(SD_MUTEX(un)); 23041 23042 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23043 if ((rval = sd_write_label(dev)) == 0) { 23044 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23045 != 0) { 23046 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23047 "sd_dkio_set_vtoc: " 23048 "Failed validate geometry\n"); 23049 } 23050 } 23051 } 23052 23053 /* 23054 * If sd_build_label_vtoc, or sd_write_label failed above write the 23055 * devid anyway, what can it hurt? Also preserve the device id by 23056 * writing to the disk acyl for the case where a devid has been 23057 * fabricated. 23058 */ 23059 if (un->un_f_devid_supported && 23060 (un->un_f_opt_fab_devid == TRUE)) { 23061 if (un->un_devid == NULL) { 23062 sd_register_devid(un, SD_DEVINFO(un), 23063 SD_TARGET_IS_UNRESERVED); 23064 } else { 23065 /* 23066 * The device id for this disk has been 23067 * fabricated. Fabricated device id's are 23068 * managed by storing them in the last 2 23069 * available sectors on the drive. The device 23070 * id must be preserved by writing it back out 23071 * to this location. 23072 */ 23073 if (sd_write_deviceid(un) != 0) { 23074 ddi_devid_free(un->un_devid); 23075 un->un_devid = NULL; 23076 } 23077 } 23078 } 23079 mutex_exit(SD_MUTEX(un)); 23080 return (rval); 23081 } 23082 23083 23084 /* 23085 * Function: sd_build_label_vtoc 23086 * 23087 * Description: This routine updates the driver soft state current volume table 23088 * of contents based on a user specified vtoc. 23089 * 23090 * Arguments: un - driver soft state (unit) structure 23091 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23092 * to update the driver soft state. 23093 * 23094 * Return Code: 0 23095 * EINVAL 23096 */ 23097 23098 static int 23099 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23100 { 23101 struct dk_map *lmap; 23102 struct partition *vpart; 23103 int nblks; 23104 #if defined(_SUNOS_VTOC_8) 23105 int ncyl; 23106 struct dk_map2 *lpart; 23107 #endif /* defined(_SUNOS_VTOC_8) */ 23108 int i; 23109 23110 ASSERT(mutex_owned(SD_MUTEX(un))); 23111 23112 /* Sanity-check the vtoc */ 23113 if (user_vtoc->v_sanity != VTOC_SANE || 23114 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23115 user_vtoc->v_nparts != V_NUMPAR) { 23116 return (EINVAL); 23117 } 23118 23119 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23120 if (nblks == 0) { 23121 return (EINVAL); 23122 } 23123 23124 #if defined(_SUNOS_VTOC_8) 23125 vpart = user_vtoc->v_part; 23126 for (i = 0; i < V_NUMPAR; i++) { 23127 if ((vpart->p_start % nblks) != 0) { 23128 return (EINVAL); 23129 } 23130 ncyl = vpart->p_start / nblks; 23131 ncyl += vpart->p_size / nblks; 23132 if ((vpart->p_size % nblks) != 0) { 23133 ncyl++; 23134 } 23135 if (ncyl > (int)un->un_g.dkg_ncyl) { 23136 return (EINVAL); 23137 } 23138 vpart++; 23139 } 23140 #endif /* defined(_SUNOS_VTOC_8) */ 23141 23142 /* Put appropriate vtoc structure fields into the disk label */ 23143 #if defined(_SUNOS_VTOC_16) 23144 /* 23145 * The vtoc is always a 32bit data structure to maintain the 23146 * on-disk format. Convert "in place" instead of bcopying it. 23147 */ 23148 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23149 23150 /* 23151 * in the 16-slice vtoc, starting sectors are expressed in 23152 * numbers *relative* to the start of the Solaris fdisk partition. 23153 */ 23154 lmap = un->un_map; 23155 vpart = user_vtoc->v_part; 23156 23157 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23158 lmap->dkl_cylno = vpart->p_start / nblks; 23159 lmap->dkl_nblk = vpart->p_size; 23160 } 23161 23162 #elif defined(_SUNOS_VTOC_8) 23163 23164 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23165 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23166 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23167 23168 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23169 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23170 23171 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23172 23173 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23174 23175 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23176 sizeof (un->un_vtoc.v_reserved)); 23177 23178 /* 23179 * Note the conversion from starting sector number 23180 * to starting cylinder number. 23181 * Return error if division results in a remainder. 23182 */ 23183 lmap = un->un_map; 23184 lpart = un->un_vtoc.v_part; 23185 vpart = user_vtoc->v_part; 23186 23187 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23188 lpart->p_tag = vpart->p_tag; 23189 lpart->p_flag = vpart->p_flag; 23190 lmap->dkl_cylno = vpart->p_start / nblks; 23191 lmap->dkl_nblk = vpart->p_size; 23192 23193 lmap++; 23194 lpart++; 23195 vpart++; 23196 23197 /* (4387723) */ 23198 #ifdef _LP64 23199 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23200 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23201 } else { 23202 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23203 } 23204 #else 23205 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23206 #endif 23207 } 23208 23209 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23210 #else 23211 #error "No VTOC format defined." 23212 #endif 23213 return (0); 23214 } 23215 23216 /* 23217 * Function: sd_clear_efi 23218 * 23219 * Description: This routine clears all EFI labels. 23220 * 23221 * Arguments: un - driver soft state (unit) structure 23222 * 23223 * Return Code: void 23224 */ 23225 23226 static void 23227 sd_clear_efi(struct sd_lun *un) 23228 { 23229 efi_gpt_t *gpt; 23230 uint_t lbasize; 23231 uint64_t cap; 23232 int rval; 23233 23234 ASSERT(!mutex_owned(SD_MUTEX(un))); 23235 23236 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23237 23238 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23239 goto done; 23240 } 23241 23242 sd_swap_efi_gpt(gpt); 23243 rval = sd_validate_efi(gpt); 23244 if (rval == 0) { 23245 /* clear primary */ 23246 bzero(gpt, sizeof (efi_gpt_t)); 23247 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23248 SD_PATH_DIRECT))) { 23249 SD_INFO(SD_LOG_IO_PARTITION, un, 23250 "sd_clear_efi: clear primary label failed\n"); 23251 } 23252 } 23253 /* the backup */ 23254 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23255 SD_PATH_DIRECT); 23256 if (rval) { 23257 goto done; 23258 } 23259 /* 23260 * The MMC standard allows READ CAPACITY to be 23261 * inaccurate by a bounded amount (in the interest of 23262 * response latency). As a result, failed READs are 23263 * commonplace (due to the reading of metadata and not 23264 * data). Depending on the per-Vendor/drive Sense data, 23265 * the failed READ can cause many (unnecessary) retries. 23266 */ 23267 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23268 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23269 SD_PATH_DIRECT)) != 0) { 23270 goto done; 23271 } 23272 sd_swap_efi_gpt(gpt); 23273 rval = sd_validate_efi(gpt); 23274 if (rval == 0) { 23275 /* clear backup */ 23276 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23277 cap-1); 23278 bzero(gpt, sizeof (efi_gpt_t)); 23279 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23280 cap-1, SD_PATH_DIRECT))) { 23281 SD_INFO(SD_LOG_IO_PARTITION, un, 23282 "sd_clear_efi: clear backup label failed\n"); 23283 } 23284 } 23285 23286 done: 23287 kmem_free(gpt, sizeof (efi_gpt_t)); 23288 } 23289 23290 /* 23291 * Function: sd_set_vtoc 23292 * 23293 * Description: This routine writes data to the appropriate positions 23294 * 23295 * Arguments: un - driver soft state (unit) structure 23296 * dkl - the data to be written 23297 * 23298 * Return: void 23299 */ 23300 23301 static int 23302 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23303 { 23304 void *shadow_buf; 23305 uint_t label_addr; 23306 int sec; 23307 int blk; 23308 int head; 23309 int cyl; 23310 int rval; 23311 23312 #if defined(__i386) || defined(__amd64) 23313 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23314 #else 23315 /* Write the primary label at block 0 of the solaris partition. */ 23316 label_addr = 0; 23317 #endif 23318 23319 if (NOT_DEVBSIZE(un)) { 23320 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23321 /* 23322 * Read the target's first block. 23323 */ 23324 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23325 un->un_tgt_blocksize, label_addr, 23326 SD_PATH_STANDARD)) != 0) { 23327 goto exit; 23328 } 23329 /* 23330 * Copy the contents of the label into the shadow buffer 23331 * which is of the size of target block size. 23332 */ 23333 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23334 } 23335 23336 /* Write the primary label */ 23337 if (NOT_DEVBSIZE(un)) { 23338 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23339 label_addr, SD_PATH_STANDARD); 23340 } else { 23341 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23342 label_addr, SD_PATH_STANDARD); 23343 } 23344 if (rval != 0) { 23345 return (rval); 23346 } 23347 23348 /* 23349 * Calculate where the backup labels go. They are always on 23350 * the last alternate cylinder, but some older drives put them 23351 * on head 2 instead of the last head. They are always on the 23352 * first 5 odd sectors of the appropriate track. 23353 * 23354 * We have no choice at this point, but to believe that the 23355 * disk label is valid. Use the geometry of the disk 23356 * as described in the label. 23357 */ 23358 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23359 head = dkl->dkl_nhead - 1; 23360 23361 /* 23362 * Write and verify the backup labels. Make sure we don't try to 23363 * write past the last cylinder. 23364 */ 23365 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23366 blk = (daddr_t)( 23367 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23368 (head * dkl->dkl_nsect) + sec); 23369 #if defined(__i386) || defined(__amd64) 23370 blk += un->un_solaris_offset; 23371 #endif 23372 if (NOT_DEVBSIZE(un)) { 23373 uint64_t tblk; 23374 /* 23375 * Need to read the block first for read modify write. 23376 */ 23377 tblk = (uint64_t)blk; 23378 blk = (int)((tblk * un->un_sys_blocksize) / 23379 un->un_tgt_blocksize); 23380 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23381 un->un_tgt_blocksize, blk, 23382 SD_PATH_STANDARD)) != 0) { 23383 goto exit; 23384 } 23385 /* 23386 * Modify the shadow buffer with the label. 23387 */ 23388 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23389 rval = sd_send_scsi_WRITE(un, shadow_buf, 23390 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23391 } else { 23392 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23393 blk, SD_PATH_STANDARD); 23394 SD_INFO(SD_LOG_IO_PARTITION, un, 23395 "sd_set_vtoc: wrote backup label %d\n", blk); 23396 } 23397 if (rval != 0) { 23398 goto exit; 23399 } 23400 } 23401 exit: 23402 if (NOT_DEVBSIZE(un)) { 23403 kmem_free(shadow_buf, un->un_tgt_blocksize); 23404 } 23405 return (rval); 23406 } 23407 23408 /* 23409 * Function: sd_clear_vtoc 23410 * 23411 * Description: This routine clears out the VTOC labels. 23412 * 23413 * Arguments: un - driver soft state (unit) structure 23414 * 23415 * Return: void 23416 */ 23417 23418 static void 23419 sd_clear_vtoc(struct sd_lun *un) 23420 { 23421 struct dk_label *dkl; 23422 23423 mutex_exit(SD_MUTEX(un)); 23424 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23425 mutex_enter(SD_MUTEX(un)); 23426 /* 23427 * sd_set_vtoc uses these fields in order to figure out 23428 * where to overwrite the backup labels 23429 */ 23430 dkl->dkl_apc = un->un_g.dkg_apc; 23431 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23432 dkl->dkl_acyl = un->un_g.dkg_acyl; 23433 dkl->dkl_nhead = un->un_g.dkg_nhead; 23434 dkl->dkl_nsect = un->un_g.dkg_nsect; 23435 mutex_exit(SD_MUTEX(un)); 23436 (void) sd_set_vtoc(un, dkl); 23437 kmem_free(dkl, sizeof (struct dk_label)); 23438 23439 mutex_enter(SD_MUTEX(un)); 23440 } 23441 23442 /* 23443 * Function: sd_write_label 23444 * 23445 * Description: This routine will validate and write the driver soft state vtoc 23446 * contents to the device. 23447 * 23448 * Arguments: dev - the device number 23449 * 23450 * Return Code: the code returned by sd_send_scsi_cmd() 23451 * 0 23452 * EINVAL 23453 * ENXIO 23454 * ENOMEM 23455 */ 23456 23457 static int 23458 sd_write_label(dev_t dev) 23459 { 23460 struct sd_lun *un; 23461 struct dk_label *dkl; 23462 short sum; 23463 short *sp; 23464 int i; 23465 int rval; 23466 23467 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23468 (un->un_state == SD_STATE_OFFLINE)) { 23469 return (ENXIO); 23470 } 23471 ASSERT(mutex_owned(SD_MUTEX(un))); 23472 mutex_exit(SD_MUTEX(un)); 23473 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23474 mutex_enter(SD_MUTEX(un)); 23475 23476 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23477 dkl->dkl_rpm = un->un_g.dkg_rpm; 23478 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23479 dkl->dkl_apc = un->un_g.dkg_apc; 23480 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23481 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23482 dkl->dkl_acyl = un->un_g.dkg_acyl; 23483 dkl->dkl_nhead = un->un_g.dkg_nhead; 23484 dkl->dkl_nsect = un->un_g.dkg_nsect; 23485 23486 #if defined(_SUNOS_VTOC_8) 23487 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23488 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23489 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23490 for (i = 0; i < NDKMAP; i++) { 23491 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23492 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23493 } 23494 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23495 #elif defined(_SUNOS_VTOC_16) 23496 dkl->dkl_skew = un->un_dkg_skew; 23497 #else 23498 #error "No VTOC format defined." 23499 #endif 23500 23501 dkl->dkl_magic = DKL_MAGIC; 23502 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23503 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23504 23505 /* Construct checksum for the new disk label */ 23506 sum = 0; 23507 sp = (short *)dkl; 23508 i = sizeof (struct dk_label) / sizeof (short); 23509 while (i--) { 23510 sum ^= *sp++; 23511 } 23512 dkl->dkl_cksum = sum; 23513 23514 mutex_exit(SD_MUTEX(un)); 23515 23516 rval = sd_set_vtoc(un, dkl); 23517 exit: 23518 kmem_free(dkl, sizeof (struct dk_label)); 23519 mutex_enter(SD_MUTEX(un)); 23520 return (rval); 23521 } 23522 23523 static int 23524 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23525 { 23526 struct sd_lun *un = NULL; 23527 dk_efi_t user_efi; 23528 int rval = 0; 23529 void *buffer; 23530 23531 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23532 return (ENXIO); 23533 23534 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23535 return (EFAULT); 23536 23537 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23538 23539 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23540 (user_efi.dki_length > un->un_max_xfer_size)) 23541 return (EINVAL); 23542 23543 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23544 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23545 rval = EFAULT; 23546 } else { 23547 /* 23548 * let's clear the vtoc labels and clear the softstate 23549 * vtoc. 23550 */ 23551 mutex_enter(SD_MUTEX(un)); 23552 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23553 SD_TRACE(SD_LOG_IO_PARTITION, un, 23554 "sd_dkio_set_efi: CLEAR VTOC\n"); 23555 sd_clear_vtoc(un); 23556 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23557 mutex_exit(SD_MUTEX(un)); 23558 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23559 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23560 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23561 S_IFBLK, 23562 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23563 un->un_node_type, NULL); 23564 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23565 S_IFCHR, 23566 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23567 un->un_node_type, NULL); 23568 } else 23569 mutex_exit(SD_MUTEX(un)); 23570 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23571 user_efi.dki_lba, SD_PATH_DIRECT); 23572 if (rval == 0) { 23573 mutex_enter(SD_MUTEX(un)); 23574 un->un_f_geometry_is_valid = FALSE; 23575 mutex_exit(SD_MUTEX(un)); 23576 } 23577 } 23578 kmem_free(buffer, user_efi.dki_length); 23579 return (rval); 23580 } 23581 23582 /* 23583 * Function: sd_dkio_get_mboot 23584 * 23585 * Description: This routine is the driver entry point for handling user 23586 * requests to get the current device mboot (DKIOCGMBOOT) 23587 * 23588 * Arguments: dev - the device number 23589 * arg - pointer to user provided mboot structure specifying 23590 * the current mboot. 23591 * flag - this argument is a pass through to ddi_copyxxx() 23592 * directly from the mode argument of ioctl(). 23593 * 23594 * Return Code: 0 23595 * EINVAL 23596 * EFAULT 23597 * ENXIO 23598 */ 23599 23600 static int 23601 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23602 { 23603 struct sd_lun *un; 23604 struct mboot *mboot; 23605 int rval; 23606 size_t buffer_size; 23607 23608 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23609 (un->un_state == SD_STATE_OFFLINE)) { 23610 return (ENXIO); 23611 } 23612 23613 if (!un->un_f_mboot_supported || arg == NULL) { 23614 return (EINVAL); 23615 } 23616 23617 /* 23618 * Read the mboot block, located at absolute block 0 on the target. 23619 */ 23620 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23621 23622 SD_TRACE(SD_LOG_IO_PARTITION, un, 23623 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23624 23625 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23626 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23627 SD_PATH_STANDARD)) == 0) { 23628 if (ddi_copyout(mboot, (void *)arg, 23629 sizeof (struct mboot), flag) != 0) { 23630 rval = EFAULT; 23631 } 23632 } 23633 kmem_free(mboot, buffer_size); 23634 return (rval); 23635 } 23636 23637 23638 /* 23639 * Function: sd_dkio_set_mboot 23640 * 23641 * Description: This routine is the driver entry point for handling user 23642 * requests to validate and set the device master boot 23643 * (DKIOCSMBOOT). 23644 * 23645 * Arguments: dev - the device number 23646 * arg - pointer to user provided mboot structure used to set the 23647 * master boot. 23648 * flag - this argument is a pass through to ddi_copyxxx() 23649 * directly from the mode argument of ioctl(). 23650 * 23651 * Return Code: 0 23652 * EINVAL 23653 * EFAULT 23654 * ENXIO 23655 */ 23656 23657 static int 23658 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23659 { 23660 struct sd_lun *un = NULL; 23661 struct mboot *mboot = NULL; 23662 int rval; 23663 ushort_t magic; 23664 23665 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23666 return (ENXIO); 23667 } 23668 23669 ASSERT(!mutex_owned(SD_MUTEX(un))); 23670 23671 if (!un->un_f_mboot_supported) { 23672 return (EINVAL); 23673 } 23674 23675 if (arg == NULL) { 23676 return (EINVAL); 23677 } 23678 23679 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23680 23681 if (ddi_copyin((const void *)arg, mboot, 23682 sizeof (struct mboot), flag) != 0) { 23683 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23684 return (EFAULT); 23685 } 23686 23687 /* Is this really a master boot record? */ 23688 magic = LE_16(mboot->signature); 23689 if (magic != MBB_MAGIC) { 23690 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23691 return (EINVAL); 23692 } 23693 23694 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23695 SD_PATH_STANDARD); 23696 23697 mutex_enter(SD_MUTEX(un)); 23698 #if defined(__i386) || defined(__amd64) 23699 if (rval == 0) { 23700 /* 23701 * mboot has been written successfully. 23702 * update the fdisk and vtoc tables in memory 23703 */ 23704 rval = sd_update_fdisk_and_vtoc(un); 23705 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23706 mutex_exit(SD_MUTEX(un)); 23707 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23708 return (rval); 23709 } 23710 } 23711 23712 /* 23713 * If the mboot write fails, write the devid anyway, what can it hurt? 23714 * Also preserve the device id by writing to the disk acyl for the case 23715 * where a devid has been fabricated. 23716 */ 23717 if (un->un_f_devid_supported && un->un_f_opt_fab_devid) { 23718 if (un->un_devid == NULL) { 23719 sd_register_devid(un, SD_DEVINFO(un), 23720 SD_TARGET_IS_UNRESERVED); 23721 } else { 23722 /* 23723 * The device id for this disk has been 23724 * fabricated. Fabricated device id's are 23725 * managed by storing them in the last 2 23726 * available sectors on the drive. The device 23727 * id must be preserved by writing it back out 23728 * to this location. 23729 */ 23730 if (sd_write_deviceid(un) != 0) { 23731 ddi_devid_free(un->un_devid); 23732 un->un_devid = NULL; 23733 } 23734 } 23735 } 23736 23737 #ifdef __lock_lint 23738 sd_setup_default_geometry(un); 23739 #endif 23740 23741 #else 23742 if (rval == 0) { 23743 /* 23744 * mboot has been written successfully. 23745 * set up the default geometry and VTOC 23746 */ 23747 if (un->un_blockcount <= DK_MAX_BLOCKS) 23748 sd_setup_default_geometry(un); 23749 } 23750 #endif 23751 mutex_exit(SD_MUTEX(un)); 23752 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23753 return (rval); 23754 } 23755 23756 23757 /* 23758 * Function: sd_setup_default_geometry 23759 * 23760 * Description: This local utility routine sets the default geometry as part of 23761 * setting the device mboot. 23762 * 23763 * Arguments: un - driver soft state (unit) structure 23764 * 23765 * Note: This may be redundant with sd_build_default_label. 23766 */ 23767 23768 static void 23769 sd_setup_default_geometry(struct sd_lun *un) 23770 { 23771 /* zero out the soft state geometry and partition table. */ 23772 bzero(&un->un_g, sizeof (struct dk_geom)); 23773 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23774 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23775 un->un_asciilabel[0] = '\0'; 23776 23777 /* 23778 * For the rpm, we use the minimum for the disk. 23779 * For the head, cyl and number of sector per track, 23780 * if the capacity <= 1GB, head = 64, sect = 32. 23781 * else head = 255, sect 63 23782 * Note: the capacity should be equal to C*H*S values. 23783 * This will cause some truncation of size due to 23784 * round off errors. For CD-ROMs, this truncation can 23785 * have adverse side effects, so returning ncyl and 23786 * nhead as 1. The nsect will overflow for most of 23787 * CD-ROMs as nsect is of type ushort. 23788 */ 23789 if (ISCD(un)) { 23790 un->un_g.dkg_ncyl = 1; 23791 un->un_g.dkg_nhead = 1; 23792 un->un_g.dkg_nsect = un->un_blockcount; 23793 } else { 23794 if (un->un_blockcount <= 0x1000) { 23795 /* Needed for unlabeled SCSI floppies. */ 23796 un->un_g.dkg_nhead = 2; 23797 un->un_g.dkg_ncyl = 80; 23798 un->un_g.dkg_pcyl = 80; 23799 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 23800 } else if (un->un_blockcount <= 0x200000) { 23801 un->un_g.dkg_nhead = 64; 23802 un->un_g.dkg_nsect = 32; 23803 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 23804 } else { 23805 un->un_g.dkg_nhead = 255; 23806 un->un_g.dkg_nsect = 63; 23807 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 23808 } 23809 un->un_blockcount = un->un_g.dkg_ncyl * 23810 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 23811 } 23812 un->un_g.dkg_acyl = 0; 23813 un->un_g.dkg_bcyl = 0; 23814 un->un_g.dkg_intrlv = 1; 23815 un->un_g.dkg_rpm = 200; 23816 un->un_g.dkg_read_reinstruct = 0; 23817 un->un_g.dkg_write_reinstruct = 0; 23818 if (un->un_g.dkg_pcyl == 0) { 23819 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 23820 } 23821 23822 un->un_map['a'-'a'].dkl_cylno = 0; 23823 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 23824 un->un_map['c'-'a'].dkl_cylno = 0; 23825 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 23826 un->un_f_geometry_is_valid = FALSE; 23827 } 23828 23829 23830 #if defined(__i386) || defined(__amd64) 23831 /* 23832 * Function: sd_update_fdisk_and_vtoc 23833 * 23834 * Description: This local utility routine updates the device fdisk and vtoc 23835 * as part of setting the device mboot. 23836 * 23837 * Arguments: un - driver soft state (unit) structure 23838 * 23839 * Return Code: 0 for success or errno-type return code. 23840 * 23841 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 23842 * these did exist seperately in x86 sd.c!!! 23843 */ 23844 23845 static int 23846 sd_update_fdisk_and_vtoc(struct sd_lun *un) 23847 { 23848 static char labelstring[128]; 23849 static char buf[256]; 23850 char *label = 0; 23851 int count; 23852 int label_rc = 0; 23853 int gvalid = un->un_f_geometry_is_valid; 23854 int fdisk_rval; 23855 int lbasize; 23856 int capacity; 23857 23858 ASSERT(mutex_owned(SD_MUTEX(un))); 23859 23860 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 23861 return (EINVAL); 23862 } 23863 23864 if (un->un_f_blockcount_is_valid == FALSE) { 23865 return (EINVAL); 23866 } 23867 23868 #if defined(_SUNOS_VTOC_16) 23869 /* 23870 * Set up the "whole disk" fdisk partition; this should always 23871 * exist, regardless of whether the disk contains an fdisk table 23872 * or vtoc. 23873 */ 23874 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 23875 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 23876 #endif /* defined(_SUNOS_VTOC_16) */ 23877 23878 /* 23879 * copy the lbasize and capacity so that if they're 23880 * reset while we're not holding the SD_MUTEX(un), we will 23881 * continue to use valid values after the SD_MUTEX(un) is 23882 * reacquired. 23883 */ 23884 lbasize = un->un_tgt_blocksize; 23885 capacity = un->un_blockcount; 23886 23887 /* 23888 * refresh the logical and physical geometry caches. 23889 * (data from mode sense format/rigid disk geometry pages, 23890 * and scsi_ifgetcap("geometry"). 23891 */ 23892 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 23893 23894 /* 23895 * Only DIRECT ACCESS devices will have Sun labels. 23896 * CD's supposedly have a Sun label, too 23897 */ 23898 if (un->un_f_vtoc_label_supported) { 23899 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 23900 SD_PATH_DIRECT); 23901 if (fdisk_rval == SD_CMD_FAILURE) { 23902 ASSERT(mutex_owned(SD_MUTEX(un))); 23903 return (EIO); 23904 } 23905 23906 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 23907 ASSERT(mutex_owned(SD_MUTEX(un))); 23908 return (EACCES); 23909 } 23910 23911 if (un->un_solaris_size <= DK_LABEL_LOC) { 23912 /* 23913 * Found fdisk table but no Solaris partition entry, 23914 * so don't call sd_uselabel() and don't create 23915 * a default label. 23916 */ 23917 label_rc = 0; 23918 un->un_f_geometry_is_valid = TRUE; 23919 goto no_solaris_partition; 23920 } 23921 23922 #if defined(_SUNOS_VTOC_8) 23923 label = (char *)un->un_asciilabel; 23924 #elif defined(_SUNOS_VTOC_16) 23925 label = (char *)un->un_vtoc.v_asciilabel; 23926 #else 23927 #error "No VTOC format defined." 23928 #endif 23929 } else if (capacity < 0) { 23930 ASSERT(mutex_owned(SD_MUTEX(un))); 23931 return (EINVAL); 23932 } 23933 23934 /* 23935 * For Removable media We reach here if we have found a 23936 * SOLARIS PARTITION. 23937 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 23938 * PARTITION has changed from the previous one, hence we will setup a 23939 * default VTOC in this case. 23940 */ 23941 if (un->un_f_geometry_is_valid == FALSE) { 23942 sd_build_default_label(un); 23943 label_rc = 0; 23944 } 23945 23946 no_solaris_partition: 23947 if ((!un->un_f_has_removable_media || 23948 (un->un_f_has_removable_media && 23949 un->un_mediastate == DKIO_EJECTED)) && 23950 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 23951 /* 23952 * Print out a message indicating who and what we are. 23953 * We do this only when we happen to really validate the 23954 * geometry. We may call sd_validate_geometry() at other 23955 * times, ioctl()'s like Get VTOC in which case we 23956 * don't want to print the label. 23957 * If the geometry is valid, print the label string, 23958 * else print vendor and product info, if available 23959 */ 23960 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 23961 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 23962 } else { 23963 mutex_enter(&sd_label_mutex); 23964 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 23965 labelstring); 23966 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 23967 &labelstring[64]); 23968 (void) sprintf(buf, "?Vendor '%s', product '%s'", 23969 labelstring, &labelstring[64]); 23970 if (un->un_f_blockcount_is_valid == TRUE) { 23971 (void) sprintf(&buf[strlen(buf)], 23972 ", %" PRIu64 " %u byte blocks\n", 23973 un->un_blockcount, 23974 un->un_tgt_blocksize); 23975 } else { 23976 (void) sprintf(&buf[strlen(buf)], 23977 ", (unknown capacity)\n"); 23978 } 23979 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 23980 mutex_exit(&sd_label_mutex); 23981 } 23982 } 23983 23984 #if defined(_SUNOS_VTOC_16) 23985 /* 23986 * If we have valid geometry, set up the remaining fdisk partitions. 23987 * Note that dkl_cylno is not used for the fdisk map entries, so 23988 * we set it to an entirely bogus value. 23989 */ 23990 for (count = 0; count < FD_NUMPART; count++) { 23991 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 23992 un->un_map[FDISK_P1 + count].dkl_nblk = 23993 un->un_fmap[count].fmap_nblk; 23994 un->un_offset[FDISK_P1 + count] = 23995 un->un_fmap[count].fmap_start; 23996 } 23997 #endif 23998 23999 for (count = 0; count < NDKMAP; count++) { 24000 #if defined(_SUNOS_VTOC_8) 24001 struct dk_map *lp = &un->un_map[count]; 24002 un->un_offset[count] = 24003 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24004 #elif defined(_SUNOS_VTOC_16) 24005 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24006 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24007 #else 24008 #error "No VTOC format defined." 24009 #endif 24010 } 24011 24012 ASSERT(mutex_owned(SD_MUTEX(un))); 24013 return (label_rc); 24014 } 24015 #endif 24016 24017 24018 /* 24019 * Function: sd_check_media 24020 * 24021 * Description: This utility routine implements the functionality for the 24022 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24023 * driver state changes from that specified by the user 24024 * (inserted or ejected). For example, if the user specifies 24025 * DKIO_EJECTED and the current media state is inserted this 24026 * routine will immediately return DKIO_INSERTED. However, if the 24027 * current media state is not inserted the user thread will be 24028 * blocked until the drive state changes. If DKIO_NONE is specified 24029 * the user thread will block until a drive state change occurs. 24030 * 24031 * Arguments: dev - the device number 24032 * state - user pointer to a dkio_state, updated with the current 24033 * drive state at return. 24034 * 24035 * Return Code: ENXIO 24036 * EIO 24037 * EAGAIN 24038 * EINTR 24039 */ 24040 24041 static int 24042 sd_check_media(dev_t dev, enum dkio_state state) 24043 { 24044 struct sd_lun *un = NULL; 24045 enum dkio_state prev_state; 24046 opaque_t token = NULL; 24047 int rval = 0; 24048 24049 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24050 return (ENXIO); 24051 } 24052 24053 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24054 24055 mutex_enter(SD_MUTEX(un)); 24056 24057 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24058 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24059 24060 prev_state = un->un_mediastate; 24061 24062 /* is there anything to do? */ 24063 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24064 /* 24065 * submit the request to the scsi_watch service; 24066 * scsi_media_watch_cb() does the real work 24067 */ 24068 mutex_exit(SD_MUTEX(un)); 24069 24070 /* 24071 * This change handles the case where a scsi watch request is 24072 * added to a device that is powered down. To accomplish this 24073 * we power up the device before adding the scsi watch request, 24074 * since the scsi watch sends a TUR directly to the device 24075 * which the device cannot handle if it is powered down. 24076 */ 24077 if (sd_pm_entry(un) != DDI_SUCCESS) { 24078 mutex_enter(SD_MUTEX(un)); 24079 goto done; 24080 } 24081 24082 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24083 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24084 (caddr_t)dev); 24085 24086 sd_pm_exit(un); 24087 24088 mutex_enter(SD_MUTEX(un)); 24089 if (token == NULL) { 24090 rval = EAGAIN; 24091 goto done; 24092 } 24093 24094 /* 24095 * This is a special case IOCTL that doesn't return 24096 * until the media state changes. Routine sdpower 24097 * knows about and handles this so don't count it 24098 * as an active cmd in the driver, which would 24099 * keep the device busy to the pm framework. 24100 * If the count isn't decremented the device can't 24101 * be powered down. 24102 */ 24103 un->un_ncmds_in_driver--; 24104 ASSERT(un->un_ncmds_in_driver >= 0); 24105 24106 /* 24107 * if a prior request had been made, this will be the same 24108 * token, as scsi_watch was designed that way. 24109 */ 24110 un->un_swr_token = token; 24111 un->un_specified_mediastate = state; 24112 24113 /* 24114 * now wait for media change 24115 * we will not be signalled unless mediastate == state but it is 24116 * still better to test for this condition, since there is a 24117 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24118 */ 24119 SD_TRACE(SD_LOG_COMMON, un, 24120 "sd_check_media: waiting for media state change\n"); 24121 while (un->un_mediastate == state) { 24122 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24123 SD_TRACE(SD_LOG_COMMON, un, 24124 "sd_check_media: waiting for media state " 24125 "was interrupted\n"); 24126 un->un_ncmds_in_driver++; 24127 rval = EINTR; 24128 goto done; 24129 } 24130 SD_TRACE(SD_LOG_COMMON, un, 24131 "sd_check_media: received signal, state=%x\n", 24132 un->un_mediastate); 24133 } 24134 /* 24135 * Inc the counter to indicate the device once again 24136 * has an active outstanding cmd. 24137 */ 24138 un->un_ncmds_in_driver++; 24139 } 24140 24141 /* invalidate geometry */ 24142 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24143 sr_ejected(un); 24144 } 24145 24146 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24147 uint64_t capacity; 24148 uint_t lbasize; 24149 24150 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24151 mutex_exit(SD_MUTEX(un)); 24152 /* 24153 * Since the following routines use SD_PATH_DIRECT, we must 24154 * call PM directly before the upcoming disk accesses. This 24155 * may cause the disk to be power/spin up. 24156 */ 24157 24158 if (sd_pm_entry(un) == DDI_SUCCESS) { 24159 rval = sd_send_scsi_READ_CAPACITY(un, 24160 &capacity, 24161 &lbasize, SD_PATH_DIRECT); 24162 if (rval != 0) { 24163 sd_pm_exit(un); 24164 mutex_enter(SD_MUTEX(un)); 24165 goto done; 24166 } 24167 } else { 24168 rval = EIO; 24169 mutex_enter(SD_MUTEX(un)); 24170 goto done; 24171 } 24172 mutex_enter(SD_MUTEX(un)); 24173 24174 sd_update_block_info(un, lbasize, capacity); 24175 24176 un->un_f_geometry_is_valid = FALSE; 24177 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24178 24179 mutex_exit(SD_MUTEX(un)); 24180 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24181 SD_PATH_DIRECT); 24182 sd_pm_exit(un); 24183 24184 mutex_enter(SD_MUTEX(un)); 24185 } 24186 done: 24187 un->un_f_watcht_stopped = FALSE; 24188 if (un->un_swr_token) { 24189 /* 24190 * Use of this local token and the mutex ensures that we avoid 24191 * some race conditions associated with terminating the 24192 * scsi watch. 24193 */ 24194 token = un->un_swr_token; 24195 un->un_swr_token = (opaque_t)NULL; 24196 mutex_exit(SD_MUTEX(un)); 24197 (void) scsi_watch_request_terminate(token, 24198 SCSI_WATCH_TERMINATE_WAIT); 24199 mutex_enter(SD_MUTEX(un)); 24200 } 24201 24202 /* 24203 * Update the capacity kstat value, if no media previously 24204 * (capacity kstat is 0) and a media has been inserted 24205 * (un_f_blockcount_is_valid == TRUE) 24206 */ 24207 if (un->un_errstats) { 24208 struct sd_errstats *stp = NULL; 24209 24210 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24211 if ((stp->sd_capacity.value.ui64 == 0) && 24212 (un->un_f_blockcount_is_valid == TRUE)) { 24213 stp->sd_capacity.value.ui64 = 24214 (uint64_t)((uint64_t)un->un_blockcount * 24215 un->un_sys_blocksize); 24216 } 24217 } 24218 mutex_exit(SD_MUTEX(un)); 24219 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24220 return (rval); 24221 } 24222 24223 24224 /* 24225 * Function: sd_delayed_cv_broadcast 24226 * 24227 * Description: Delayed cv_broadcast to allow for target to recover from media 24228 * insertion. 24229 * 24230 * Arguments: arg - driver soft state (unit) structure 24231 */ 24232 24233 static void 24234 sd_delayed_cv_broadcast(void *arg) 24235 { 24236 struct sd_lun *un = arg; 24237 24238 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24239 24240 mutex_enter(SD_MUTEX(un)); 24241 un->un_dcvb_timeid = NULL; 24242 cv_broadcast(&un->un_state_cv); 24243 mutex_exit(SD_MUTEX(un)); 24244 } 24245 24246 24247 /* 24248 * Function: sd_media_watch_cb 24249 * 24250 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24251 * routine processes the TUR sense data and updates the driver 24252 * state if a transition has occurred. The user thread 24253 * (sd_check_media) is then signalled. 24254 * 24255 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24256 * among multiple watches that share this callback function 24257 * resultp - scsi watch facility result packet containing scsi 24258 * packet, status byte and sense data 24259 * 24260 * Return Code: 0 for success, -1 for failure 24261 */ 24262 24263 static int 24264 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24265 { 24266 struct sd_lun *un; 24267 struct scsi_status *statusp = resultp->statusp; 24268 struct scsi_extended_sense *sensep = resultp->sensep; 24269 enum dkio_state state = DKIO_NONE; 24270 dev_t dev = (dev_t)arg; 24271 uchar_t actual_sense_length; 24272 24273 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24274 return (-1); 24275 } 24276 actual_sense_length = resultp->actual_sense_length; 24277 24278 mutex_enter(SD_MUTEX(un)); 24279 SD_TRACE(SD_LOG_COMMON, un, 24280 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24281 *((char *)statusp), (void *)sensep, actual_sense_length); 24282 24283 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24284 un->un_mediastate = DKIO_DEV_GONE; 24285 cv_broadcast(&un->un_state_cv); 24286 mutex_exit(SD_MUTEX(un)); 24287 24288 return (0); 24289 } 24290 24291 /* 24292 * If there was a check condition then sensep points to valid sense data 24293 * If status was not a check condition but a reservation or busy status 24294 * then the new state is DKIO_NONE 24295 */ 24296 if (sensep != NULL) { 24297 SD_INFO(SD_LOG_COMMON, un, 24298 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24299 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 24300 /* This routine only uses up to 13 bytes of sense data. */ 24301 if (actual_sense_length >= 13) { 24302 if (sensep->es_key == KEY_UNIT_ATTENTION) { 24303 if (sensep->es_add_code == 0x28) { 24304 state = DKIO_INSERTED; 24305 } 24306 } else { 24307 /* 24308 * if 02/04/02 means that the host 24309 * should send start command. Explicitly 24310 * leave the media state as is 24311 * (inserted) as the media is inserted 24312 * and host has stopped device for PM 24313 * reasons. Upon next true read/write 24314 * to this media will bring the 24315 * device to the right state good for 24316 * media access. 24317 */ 24318 if ((sensep->es_key == KEY_NOT_READY) && 24319 (sensep->es_add_code == 0x3a)) { 24320 state = DKIO_EJECTED; 24321 } 24322 24323 /* 24324 * If the drivge is busy with an operation 24325 * or long write, keep the media in an 24326 * inserted state. 24327 */ 24328 24329 if ((sensep->es_key == KEY_NOT_READY) && 24330 (sensep->es_add_code == 0x04) && 24331 ((sensep->es_qual_code == 0x02) || 24332 (sensep->es_qual_code == 0x07) || 24333 (sensep->es_qual_code == 0x08))) { 24334 state = DKIO_INSERTED; 24335 } 24336 } 24337 } 24338 } else if ((*((char *)statusp) == STATUS_GOOD) && 24339 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24340 state = DKIO_INSERTED; 24341 } 24342 24343 SD_TRACE(SD_LOG_COMMON, un, 24344 "sd_media_watch_cb: state=%x, specified=%x\n", 24345 state, un->un_specified_mediastate); 24346 24347 /* 24348 * now signal the waiting thread if this is *not* the specified state; 24349 * delay the signal if the state is DKIO_INSERTED to allow the target 24350 * to recover 24351 */ 24352 if (state != un->un_specified_mediastate) { 24353 un->un_mediastate = state; 24354 if (state == DKIO_INSERTED) { 24355 /* 24356 * delay the signal to give the drive a chance 24357 * to do what it apparently needs to do 24358 */ 24359 SD_TRACE(SD_LOG_COMMON, un, 24360 "sd_media_watch_cb: delayed cv_broadcast\n"); 24361 if (un->un_dcvb_timeid == NULL) { 24362 un->un_dcvb_timeid = 24363 timeout(sd_delayed_cv_broadcast, un, 24364 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24365 } 24366 } else { 24367 SD_TRACE(SD_LOG_COMMON, un, 24368 "sd_media_watch_cb: immediate cv_broadcast\n"); 24369 cv_broadcast(&un->un_state_cv); 24370 } 24371 } 24372 mutex_exit(SD_MUTEX(un)); 24373 return (0); 24374 } 24375 24376 24377 /* 24378 * Function: sd_dkio_get_temp 24379 * 24380 * Description: This routine is the driver entry point for handling ioctl 24381 * requests to get the disk temperature. 24382 * 24383 * Arguments: dev - the device number 24384 * arg - pointer to user provided dk_temperature structure. 24385 * flag - this argument is a pass through to ddi_copyxxx() 24386 * directly from the mode argument of ioctl(). 24387 * 24388 * Return Code: 0 24389 * EFAULT 24390 * ENXIO 24391 * EAGAIN 24392 */ 24393 24394 static int 24395 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24396 { 24397 struct sd_lun *un = NULL; 24398 struct dk_temperature *dktemp = NULL; 24399 uchar_t *temperature_page; 24400 int rval = 0; 24401 int path_flag = SD_PATH_STANDARD; 24402 24403 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24404 return (ENXIO); 24405 } 24406 24407 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24408 24409 /* copyin the disk temp argument to get the user flags */ 24410 if (ddi_copyin((void *)arg, dktemp, 24411 sizeof (struct dk_temperature), flag) != 0) { 24412 rval = EFAULT; 24413 goto done; 24414 } 24415 24416 /* Initialize the temperature to invalid. */ 24417 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24418 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24419 24420 /* 24421 * Note: Investigate removing the "bypass pm" semantic. 24422 * Can we just bypass PM always? 24423 */ 24424 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24425 path_flag = SD_PATH_DIRECT; 24426 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24427 mutex_enter(&un->un_pm_mutex); 24428 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24429 /* 24430 * If DKT_BYPASS_PM is set, and the drive happens to be 24431 * in low power mode, we can not wake it up, Need to 24432 * return EAGAIN. 24433 */ 24434 mutex_exit(&un->un_pm_mutex); 24435 rval = EAGAIN; 24436 goto done; 24437 } else { 24438 /* 24439 * Indicate to PM the device is busy. This is required 24440 * to avoid a race - i.e. the ioctl is issuing a 24441 * command and the pm framework brings down the device 24442 * to low power mode (possible power cut-off on some 24443 * platforms). 24444 */ 24445 mutex_exit(&un->un_pm_mutex); 24446 if (sd_pm_entry(un) != DDI_SUCCESS) { 24447 rval = EAGAIN; 24448 goto done; 24449 } 24450 } 24451 } 24452 24453 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24454 24455 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24456 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24457 goto done2; 24458 } 24459 24460 /* 24461 * For the current temperature verify that the parameter length is 0x02 24462 * and the parameter code is 0x00 24463 */ 24464 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24465 (temperature_page[5] == 0x00)) { 24466 if (temperature_page[9] == 0xFF) { 24467 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24468 } else { 24469 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24470 } 24471 } 24472 24473 /* 24474 * For the reference temperature verify that the parameter 24475 * length is 0x02 and the parameter code is 0x01 24476 */ 24477 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24478 (temperature_page[11] == 0x01)) { 24479 if (temperature_page[15] == 0xFF) { 24480 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24481 } else { 24482 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24483 } 24484 } 24485 24486 /* Do the copyout regardless of the temperature commands status. */ 24487 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24488 flag) != 0) { 24489 rval = EFAULT; 24490 } 24491 24492 done2: 24493 if (path_flag == SD_PATH_DIRECT) { 24494 sd_pm_exit(un); 24495 } 24496 24497 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24498 done: 24499 if (dktemp != NULL) { 24500 kmem_free(dktemp, sizeof (struct dk_temperature)); 24501 } 24502 24503 return (rval); 24504 } 24505 24506 24507 /* 24508 * Function: sd_log_page_supported 24509 * 24510 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24511 * supported log pages. 24512 * 24513 * Arguments: un - 24514 * log_page - 24515 * 24516 * Return Code: -1 - on error (log sense is optional and may not be supported). 24517 * 0 - log page not found. 24518 * 1 - log page found. 24519 */ 24520 24521 static int 24522 sd_log_page_supported(struct sd_lun *un, int log_page) 24523 { 24524 uchar_t *log_page_data; 24525 int i; 24526 int match = 0; 24527 int log_size; 24528 24529 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24530 24531 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24532 SD_PATH_DIRECT) != 0) { 24533 SD_ERROR(SD_LOG_COMMON, un, 24534 "sd_log_page_supported: failed log page retrieval\n"); 24535 kmem_free(log_page_data, 0xFF); 24536 return (-1); 24537 } 24538 log_size = log_page_data[3]; 24539 24540 /* 24541 * The list of supported log pages start from the fourth byte. Check 24542 * until we run out of log pages or a match is found. 24543 */ 24544 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24545 if (log_page_data[i] == log_page) { 24546 match++; 24547 } 24548 } 24549 kmem_free(log_page_data, 0xFF); 24550 return (match); 24551 } 24552 24553 24554 /* 24555 * Function: sd_mhdioc_failfast 24556 * 24557 * Description: This routine is the driver entry point for handling ioctl 24558 * requests to enable/disable the multihost failfast option. 24559 * (MHIOCENFAILFAST) 24560 * 24561 * Arguments: dev - the device number 24562 * arg - user specified probing interval. 24563 * flag - this argument is a pass through to ddi_copyxxx() 24564 * directly from the mode argument of ioctl(). 24565 * 24566 * Return Code: 0 24567 * EFAULT 24568 * ENXIO 24569 */ 24570 24571 static int 24572 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24573 { 24574 struct sd_lun *un = NULL; 24575 int mh_time; 24576 int rval = 0; 24577 24578 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24579 return (ENXIO); 24580 } 24581 24582 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24583 return (EFAULT); 24584 24585 if (mh_time) { 24586 mutex_enter(SD_MUTEX(un)); 24587 un->un_resvd_status |= SD_FAILFAST; 24588 mutex_exit(SD_MUTEX(un)); 24589 /* 24590 * If mh_time is INT_MAX, then this ioctl is being used for 24591 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24592 */ 24593 if (mh_time != INT_MAX) { 24594 rval = sd_check_mhd(dev, mh_time); 24595 } 24596 } else { 24597 (void) sd_check_mhd(dev, 0); 24598 mutex_enter(SD_MUTEX(un)); 24599 un->un_resvd_status &= ~SD_FAILFAST; 24600 mutex_exit(SD_MUTEX(un)); 24601 } 24602 return (rval); 24603 } 24604 24605 24606 /* 24607 * Function: sd_mhdioc_takeown 24608 * 24609 * Description: This routine is the driver entry point for handling ioctl 24610 * requests to forcefully acquire exclusive access rights to the 24611 * multihost disk (MHIOCTKOWN). 24612 * 24613 * Arguments: dev - the device number 24614 * arg - user provided structure specifying the delay 24615 * parameters in milliseconds 24616 * flag - this argument is a pass through to ddi_copyxxx() 24617 * directly from the mode argument of ioctl(). 24618 * 24619 * Return Code: 0 24620 * EFAULT 24621 * ENXIO 24622 */ 24623 24624 static int 24625 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24626 { 24627 struct sd_lun *un = NULL; 24628 struct mhioctkown *tkown = NULL; 24629 int rval = 0; 24630 24631 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24632 return (ENXIO); 24633 } 24634 24635 if (arg != NULL) { 24636 tkown = (struct mhioctkown *) 24637 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24638 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24639 if (rval != 0) { 24640 rval = EFAULT; 24641 goto error; 24642 } 24643 } 24644 24645 rval = sd_take_ownership(dev, tkown); 24646 mutex_enter(SD_MUTEX(un)); 24647 if (rval == 0) { 24648 un->un_resvd_status |= SD_RESERVE; 24649 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24650 sd_reinstate_resv_delay = 24651 tkown->reinstate_resv_delay * 1000; 24652 } else { 24653 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24654 } 24655 /* 24656 * Give the scsi_watch routine interval set by 24657 * the MHIOCENFAILFAST ioctl precedence here. 24658 */ 24659 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24660 mutex_exit(SD_MUTEX(un)); 24661 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24662 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24663 "sd_mhdioc_takeown : %d\n", 24664 sd_reinstate_resv_delay); 24665 } else { 24666 mutex_exit(SD_MUTEX(un)); 24667 } 24668 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24669 sd_mhd_reset_notify_cb, (caddr_t)un); 24670 } else { 24671 un->un_resvd_status &= ~SD_RESERVE; 24672 mutex_exit(SD_MUTEX(un)); 24673 } 24674 24675 error: 24676 if (tkown != NULL) { 24677 kmem_free(tkown, sizeof (struct mhioctkown)); 24678 } 24679 return (rval); 24680 } 24681 24682 24683 /* 24684 * Function: sd_mhdioc_release 24685 * 24686 * Description: This routine is the driver entry point for handling ioctl 24687 * requests to release exclusive access rights to the multihost 24688 * disk (MHIOCRELEASE). 24689 * 24690 * Arguments: dev - the device number 24691 * 24692 * Return Code: 0 24693 * ENXIO 24694 */ 24695 24696 static int 24697 sd_mhdioc_release(dev_t dev) 24698 { 24699 struct sd_lun *un = NULL; 24700 timeout_id_t resvd_timeid_save; 24701 int resvd_status_save; 24702 int rval = 0; 24703 24704 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24705 return (ENXIO); 24706 } 24707 24708 mutex_enter(SD_MUTEX(un)); 24709 resvd_status_save = un->un_resvd_status; 24710 un->un_resvd_status &= 24711 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24712 if (un->un_resvd_timeid) { 24713 resvd_timeid_save = un->un_resvd_timeid; 24714 un->un_resvd_timeid = NULL; 24715 mutex_exit(SD_MUTEX(un)); 24716 (void) untimeout(resvd_timeid_save); 24717 } else { 24718 mutex_exit(SD_MUTEX(un)); 24719 } 24720 24721 /* 24722 * destroy any pending timeout thread that may be attempting to 24723 * reinstate reservation on this device. 24724 */ 24725 sd_rmv_resv_reclaim_req(dev); 24726 24727 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24728 mutex_enter(SD_MUTEX(un)); 24729 if ((un->un_mhd_token) && 24730 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24731 mutex_exit(SD_MUTEX(un)); 24732 (void) sd_check_mhd(dev, 0); 24733 } else { 24734 mutex_exit(SD_MUTEX(un)); 24735 } 24736 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24737 sd_mhd_reset_notify_cb, (caddr_t)un); 24738 } else { 24739 /* 24740 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24741 */ 24742 mutex_enter(SD_MUTEX(un)); 24743 un->un_resvd_status = resvd_status_save; 24744 mutex_exit(SD_MUTEX(un)); 24745 } 24746 return (rval); 24747 } 24748 24749 24750 /* 24751 * Function: sd_mhdioc_register_devid 24752 * 24753 * Description: This routine is the driver entry point for handling ioctl 24754 * requests to register the device id (MHIOCREREGISTERDEVID). 24755 * 24756 * Note: The implementation for this ioctl has been updated to 24757 * be consistent with the original PSARC case (1999/357) 24758 * (4375899, 4241671, 4220005) 24759 * 24760 * Arguments: dev - the device number 24761 * 24762 * Return Code: 0 24763 * ENXIO 24764 */ 24765 24766 static int 24767 sd_mhdioc_register_devid(dev_t dev) 24768 { 24769 struct sd_lun *un = NULL; 24770 int rval = 0; 24771 24772 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24773 return (ENXIO); 24774 } 24775 24776 ASSERT(!mutex_owned(SD_MUTEX(un))); 24777 24778 mutex_enter(SD_MUTEX(un)); 24779 24780 /* If a devid already exists, de-register it */ 24781 if (un->un_devid != NULL) { 24782 ddi_devid_unregister(SD_DEVINFO(un)); 24783 /* 24784 * After unregister devid, needs to free devid memory 24785 */ 24786 ddi_devid_free(un->un_devid); 24787 un->un_devid = NULL; 24788 } 24789 24790 /* Check for reservation conflict */ 24791 mutex_exit(SD_MUTEX(un)); 24792 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24793 mutex_enter(SD_MUTEX(un)); 24794 24795 switch (rval) { 24796 case 0: 24797 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24798 break; 24799 case EACCES: 24800 break; 24801 default: 24802 rval = EIO; 24803 } 24804 24805 mutex_exit(SD_MUTEX(un)); 24806 return (rval); 24807 } 24808 24809 24810 /* 24811 * Function: sd_mhdioc_inkeys 24812 * 24813 * Description: This routine is the driver entry point for handling ioctl 24814 * requests to issue the SCSI-3 Persistent In Read Keys command 24815 * to the device (MHIOCGRP_INKEYS). 24816 * 24817 * Arguments: dev - the device number 24818 * arg - user provided in_keys structure 24819 * flag - this argument is a pass through to ddi_copyxxx() 24820 * directly from the mode argument of ioctl(). 24821 * 24822 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24823 * ENXIO 24824 * EFAULT 24825 */ 24826 24827 static int 24828 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24829 { 24830 struct sd_lun *un; 24831 mhioc_inkeys_t inkeys; 24832 int rval = 0; 24833 24834 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24835 return (ENXIO); 24836 } 24837 24838 #ifdef _MULTI_DATAMODEL 24839 switch (ddi_model_convert_from(flag & FMODELS)) { 24840 case DDI_MODEL_ILP32: { 24841 struct mhioc_inkeys32 inkeys32; 24842 24843 if (ddi_copyin(arg, &inkeys32, 24844 sizeof (struct mhioc_inkeys32), flag) != 0) { 24845 return (EFAULT); 24846 } 24847 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24848 if ((rval = sd_persistent_reservation_in_read_keys(un, 24849 &inkeys, flag)) != 0) { 24850 return (rval); 24851 } 24852 inkeys32.generation = inkeys.generation; 24853 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24854 flag) != 0) { 24855 return (EFAULT); 24856 } 24857 break; 24858 } 24859 case DDI_MODEL_NONE: 24860 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24861 flag) != 0) { 24862 return (EFAULT); 24863 } 24864 if ((rval = sd_persistent_reservation_in_read_keys(un, 24865 &inkeys, flag)) != 0) { 24866 return (rval); 24867 } 24868 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24869 flag) != 0) { 24870 return (EFAULT); 24871 } 24872 break; 24873 } 24874 24875 #else /* ! _MULTI_DATAMODEL */ 24876 24877 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24878 return (EFAULT); 24879 } 24880 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24881 if (rval != 0) { 24882 return (rval); 24883 } 24884 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24885 return (EFAULT); 24886 } 24887 24888 #endif /* _MULTI_DATAMODEL */ 24889 24890 return (rval); 24891 } 24892 24893 24894 /* 24895 * Function: sd_mhdioc_inresv 24896 * 24897 * Description: This routine is the driver entry point for handling ioctl 24898 * requests to issue the SCSI-3 Persistent In Read Reservations 24899 * command to the device (MHIOCGRP_INKEYS). 24900 * 24901 * Arguments: dev - the device number 24902 * arg - user provided in_resv structure 24903 * flag - this argument is a pass through to ddi_copyxxx() 24904 * directly from the mode argument of ioctl(). 24905 * 24906 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24907 * ENXIO 24908 * EFAULT 24909 */ 24910 24911 static int 24912 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24913 { 24914 struct sd_lun *un; 24915 mhioc_inresvs_t inresvs; 24916 int rval = 0; 24917 24918 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24919 return (ENXIO); 24920 } 24921 24922 #ifdef _MULTI_DATAMODEL 24923 24924 switch (ddi_model_convert_from(flag & FMODELS)) { 24925 case DDI_MODEL_ILP32: { 24926 struct mhioc_inresvs32 inresvs32; 24927 24928 if (ddi_copyin(arg, &inresvs32, 24929 sizeof (struct mhioc_inresvs32), flag) != 0) { 24930 return (EFAULT); 24931 } 24932 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24933 if ((rval = sd_persistent_reservation_in_read_resv(un, 24934 &inresvs, flag)) != 0) { 24935 return (rval); 24936 } 24937 inresvs32.generation = inresvs.generation; 24938 if (ddi_copyout(&inresvs32, arg, 24939 sizeof (struct mhioc_inresvs32), flag) != 0) { 24940 return (EFAULT); 24941 } 24942 break; 24943 } 24944 case DDI_MODEL_NONE: 24945 if (ddi_copyin(arg, &inresvs, 24946 sizeof (mhioc_inresvs_t), flag) != 0) { 24947 return (EFAULT); 24948 } 24949 if ((rval = sd_persistent_reservation_in_read_resv(un, 24950 &inresvs, flag)) != 0) { 24951 return (rval); 24952 } 24953 if (ddi_copyout(&inresvs, arg, 24954 sizeof (mhioc_inresvs_t), flag) != 0) { 24955 return (EFAULT); 24956 } 24957 break; 24958 } 24959 24960 #else /* ! _MULTI_DATAMODEL */ 24961 24962 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24963 return (EFAULT); 24964 } 24965 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24966 if (rval != 0) { 24967 return (rval); 24968 } 24969 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24970 return (EFAULT); 24971 } 24972 24973 #endif /* ! _MULTI_DATAMODEL */ 24974 24975 return (rval); 24976 } 24977 24978 24979 /* 24980 * The following routines support the clustering functionality described below 24981 * and implement lost reservation reclaim functionality. 24982 * 24983 * Clustering 24984 * ---------- 24985 * The clustering code uses two different, independent forms of SCSI 24986 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24987 * Persistent Group Reservations. For any particular disk, it will use either 24988 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24989 * 24990 * SCSI-2 24991 * The cluster software takes ownership of a multi-hosted disk by issuing the 24992 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24993 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 24994 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 24995 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 24996 * meaning of failfast is that if the driver (on this host) ever encounters the 24997 * scsi error return code RESERVATION_CONFLICT from the device, it should 24998 * immediately panic the host. The motivation for this ioctl is that if this 24999 * host does encounter reservation conflict, the underlying cause is that some 25000 * other host of the cluster has decided that this host is no longer in the 25001 * cluster and has seized control of the disks for itself. Since this host is no 25002 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25003 * does two things: 25004 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25005 * error to panic the host 25006 * (b) it sets up a periodic timer to test whether this host still has 25007 * "access" (in that no other host has reserved the device): if the 25008 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25009 * purpose of that periodic timer is to handle scenarios where the host is 25010 * otherwise temporarily quiescent, temporarily doing no real i/o. 25011 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25012 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25013 * the device itself. 25014 * 25015 * SCSI-3 PGR 25016 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25017 * facility is supported through the shared multihost disk ioctls 25018 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25019 * MHIOCGRP_PREEMPTANDABORT) 25020 * 25021 * Reservation Reclaim: 25022 * -------------------- 25023 * To support the lost reservation reclaim operations this driver creates a 25024 * single thread to handle reinstating reservations on all devices that have 25025 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25026 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25027 * and the reservation reclaim thread loops through the requests to regain the 25028 * lost reservations. 25029 */ 25030 25031 /* 25032 * Function: sd_check_mhd() 25033 * 25034 * Description: This function sets up and submits a scsi watch request or 25035 * terminates an existing watch request. This routine is used in 25036 * support of reservation reclaim. 25037 * 25038 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25039 * among multiple watches that share the callback function 25040 * interval - the number of microseconds specifying the watch 25041 * interval for issuing TEST UNIT READY commands. If 25042 * set to 0 the watch should be terminated. If the 25043 * interval is set to 0 and if the device is required 25044 * to hold reservation while disabling failfast, the 25045 * watch is restarted with an interval of 25046 * reinstate_resv_delay. 25047 * 25048 * Return Code: 0 - Successful submit/terminate of scsi watch request 25049 * ENXIO - Indicates an invalid device was specified 25050 * EAGAIN - Unable to submit the scsi watch request 25051 */ 25052 25053 static int 25054 sd_check_mhd(dev_t dev, int interval) 25055 { 25056 struct sd_lun *un; 25057 opaque_t token; 25058 25059 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25060 return (ENXIO); 25061 } 25062 25063 /* is this a watch termination request? */ 25064 if (interval == 0) { 25065 mutex_enter(SD_MUTEX(un)); 25066 /* if there is an existing watch task then terminate it */ 25067 if (un->un_mhd_token) { 25068 token = un->un_mhd_token; 25069 un->un_mhd_token = NULL; 25070 mutex_exit(SD_MUTEX(un)); 25071 (void) scsi_watch_request_terminate(token, 25072 SCSI_WATCH_TERMINATE_WAIT); 25073 mutex_enter(SD_MUTEX(un)); 25074 } else { 25075 mutex_exit(SD_MUTEX(un)); 25076 /* 25077 * Note: If we return here we don't check for the 25078 * failfast case. This is the original legacy 25079 * implementation but perhaps we should be checking 25080 * the failfast case. 25081 */ 25082 return (0); 25083 } 25084 /* 25085 * If the device is required to hold reservation while 25086 * disabling failfast, we need to restart the scsi_watch 25087 * routine with an interval of reinstate_resv_delay. 25088 */ 25089 if (un->un_resvd_status & SD_RESERVE) { 25090 interval = sd_reinstate_resv_delay/1000; 25091 } else { 25092 /* no failfast so bail */ 25093 mutex_exit(SD_MUTEX(un)); 25094 return (0); 25095 } 25096 mutex_exit(SD_MUTEX(un)); 25097 } 25098 25099 /* 25100 * adjust minimum time interval to 1 second, 25101 * and convert from msecs to usecs 25102 */ 25103 if (interval > 0 && interval < 1000) { 25104 interval = 1000; 25105 } 25106 interval *= 1000; 25107 25108 /* 25109 * submit the request to the scsi_watch service 25110 */ 25111 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25112 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25113 if (token == NULL) { 25114 return (EAGAIN); 25115 } 25116 25117 /* 25118 * save token for termination later on 25119 */ 25120 mutex_enter(SD_MUTEX(un)); 25121 un->un_mhd_token = token; 25122 mutex_exit(SD_MUTEX(un)); 25123 return (0); 25124 } 25125 25126 25127 /* 25128 * Function: sd_mhd_watch_cb() 25129 * 25130 * Description: This function is the call back function used by the scsi watch 25131 * facility. The scsi watch facility sends the "Test Unit Ready" 25132 * and processes the status. If applicable (i.e. a "Unit Attention" 25133 * status and automatic "Request Sense" not used) the scsi watch 25134 * facility will send a "Request Sense" and retrieve the sense data 25135 * to be passed to this callback function. In either case the 25136 * automatic "Request Sense" or the facility submitting one, this 25137 * callback is passed the status and sense data. 25138 * 25139 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25140 * among multiple watches that share this callback function 25141 * resultp - scsi watch facility result packet containing scsi 25142 * packet, status byte and sense data 25143 * 25144 * Return Code: 0 - continue the watch task 25145 * non-zero - terminate the watch task 25146 */ 25147 25148 static int 25149 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25150 { 25151 struct sd_lun *un; 25152 struct scsi_status *statusp; 25153 struct scsi_extended_sense *sensep; 25154 struct scsi_pkt *pkt; 25155 uchar_t actual_sense_length; 25156 dev_t dev = (dev_t)arg; 25157 25158 ASSERT(resultp != NULL); 25159 statusp = resultp->statusp; 25160 sensep = resultp->sensep; 25161 pkt = resultp->pkt; 25162 actual_sense_length = resultp->actual_sense_length; 25163 25164 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25165 return (ENXIO); 25166 } 25167 25168 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25169 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25170 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25171 25172 /* Begin processing of the status and/or sense data */ 25173 if (pkt->pkt_reason != CMD_CMPLT) { 25174 /* Handle the incomplete packet */ 25175 sd_mhd_watch_incomplete(un, pkt); 25176 return (0); 25177 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25178 if (*((unsigned char *)statusp) 25179 == STATUS_RESERVATION_CONFLICT) { 25180 /* 25181 * Handle a reservation conflict by panicking if 25182 * configured for failfast or by logging the conflict 25183 * and updating the reservation status 25184 */ 25185 mutex_enter(SD_MUTEX(un)); 25186 if ((un->un_resvd_status & SD_FAILFAST) && 25187 (sd_failfast_enable)) { 25188 sd_panic_for_res_conflict(un); 25189 /*NOTREACHED*/ 25190 } 25191 SD_INFO(SD_LOG_IOCTL_MHD, un, 25192 "sd_mhd_watch_cb: Reservation Conflict\n"); 25193 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25194 mutex_exit(SD_MUTEX(un)); 25195 } 25196 } 25197 25198 if (sensep != NULL) { 25199 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25200 mutex_enter(SD_MUTEX(un)); 25201 if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) && 25202 (un->un_resvd_status & SD_RESERVE)) { 25203 /* 25204 * The additional sense code indicates a power 25205 * on or bus device reset has occurred; update 25206 * the reservation status. 25207 */ 25208 un->un_resvd_status |= 25209 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25210 SD_INFO(SD_LOG_IOCTL_MHD, un, 25211 "sd_mhd_watch_cb: Lost Reservation\n"); 25212 } 25213 } else { 25214 return (0); 25215 } 25216 } else { 25217 mutex_enter(SD_MUTEX(un)); 25218 } 25219 25220 if ((un->un_resvd_status & SD_RESERVE) && 25221 (un->un_resvd_status & SD_LOST_RESERVE)) { 25222 if (un->un_resvd_status & SD_WANT_RESERVE) { 25223 /* 25224 * A reset occurred in between the last probe and this 25225 * one so if a timeout is pending cancel it. 25226 */ 25227 if (un->un_resvd_timeid) { 25228 timeout_id_t temp_id = un->un_resvd_timeid; 25229 un->un_resvd_timeid = NULL; 25230 mutex_exit(SD_MUTEX(un)); 25231 (void) untimeout(temp_id); 25232 mutex_enter(SD_MUTEX(un)); 25233 } 25234 un->un_resvd_status &= ~SD_WANT_RESERVE; 25235 } 25236 if (un->un_resvd_timeid == 0) { 25237 /* Schedule a timeout to handle the lost reservation */ 25238 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25239 (void *)dev, 25240 drv_usectohz(sd_reinstate_resv_delay)); 25241 } 25242 } 25243 mutex_exit(SD_MUTEX(un)); 25244 return (0); 25245 } 25246 25247 25248 /* 25249 * Function: sd_mhd_watch_incomplete() 25250 * 25251 * Description: This function is used to find out why a scsi pkt sent by the 25252 * scsi watch facility was not completed. Under some scenarios this 25253 * routine will return. Otherwise it will send a bus reset to see 25254 * if the drive is still online. 25255 * 25256 * Arguments: un - driver soft state (unit) structure 25257 * pkt - incomplete scsi pkt 25258 */ 25259 25260 static void 25261 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25262 { 25263 int be_chatty; 25264 int perr; 25265 25266 ASSERT(pkt != NULL); 25267 ASSERT(un != NULL); 25268 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25269 perr = (pkt->pkt_statistics & STAT_PERR); 25270 25271 mutex_enter(SD_MUTEX(un)); 25272 if (un->un_state == SD_STATE_DUMPING) { 25273 mutex_exit(SD_MUTEX(un)); 25274 return; 25275 } 25276 25277 switch (pkt->pkt_reason) { 25278 case CMD_UNX_BUS_FREE: 25279 /* 25280 * If we had a parity error that caused the target to drop BSY*, 25281 * don't be chatty about it. 25282 */ 25283 if (perr && be_chatty) { 25284 be_chatty = 0; 25285 } 25286 break; 25287 case CMD_TAG_REJECT: 25288 /* 25289 * The SCSI-2 spec states that a tag reject will be sent by the 25290 * target if tagged queuing is not supported. A tag reject may 25291 * also be sent during certain initialization periods or to 25292 * control internal resources. For the latter case the target 25293 * may also return Queue Full. 25294 * 25295 * If this driver receives a tag reject from a target that is 25296 * going through an init period or controlling internal 25297 * resources tagged queuing will be disabled. This is a less 25298 * than optimal behavior but the driver is unable to determine 25299 * the target state and assumes tagged queueing is not supported 25300 */ 25301 pkt->pkt_flags = 0; 25302 un->un_tagflags = 0; 25303 25304 if (un->un_f_opt_queueing == TRUE) { 25305 un->un_throttle = min(un->un_throttle, 3); 25306 } else { 25307 un->un_throttle = 1; 25308 } 25309 mutex_exit(SD_MUTEX(un)); 25310 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25311 mutex_enter(SD_MUTEX(un)); 25312 break; 25313 case CMD_INCOMPLETE: 25314 /* 25315 * The transport stopped with an abnormal state, fallthrough and 25316 * reset the target and/or bus unless selection did not complete 25317 * (indicated by STATE_GOT_BUS) in which case we don't want to 25318 * go through a target/bus reset 25319 */ 25320 if (pkt->pkt_state == STATE_GOT_BUS) { 25321 break; 25322 } 25323 /*FALLTHROUGH*/ 25324 25325 case CMD_TIMEOUT: 25326 default: 25327 /* 25328 * The lun may still be running the command, so a lun reset 25329 * should be attempted. If the lun reset fails or cannot be 25330 * issued, than try a target reset. Lastly try a bus reset. 25331 */ 25332 if ((pkt->pkt_statistics & 25333 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25334 int reset_retval = 0; 25335 mutex_exit(SD_MUTEX(un)); 25336 if (un->un_f_allow_bus_device_reset == TRUE) { 25337 if (un->un_f_lun_reset_enabled == TRUE) { 25338 reset_retval = 25339 scsi_reset(SD_ADDRESS(un), 25340 RESET_LUN); 25341 } 25342 if (reset_retval == 0) { 25343 reset_retval = 25344 scsi_reset(SD_ADDRESS(un), 25345 RESET_TARGET); 25346 } 25347 } 25348 if (reset_retval == 0) { 25349 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25350 } 25351 mutex_enter(SD_MUTEX(un)); 25352 } 25353 break; 25354 } 25355 25356 /* A device/bus reset has occurred; update the reservation status. */ 25357 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25358 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25359 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25360 un->un_resvd_status |= 25361 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25362 SD_INFO(SD_LOG_IOCTL_MHD, un, 25363 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25364 } 25365 } 25366 25367 /* 25368 * The disk has been turned off; Update the device state. 25369 * 25370 * Note: Should we be offlining the disk here? 25371 */ 25372 if (pkt->pkt_state == STATE_GOT_BUS) { 25373 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25374 "Disk not responding to selection\n"); 25375 if (un->un_state != SD_STATE_OFFLINE) { 25376 New_state(un, SD_STATE_OFFLINE); 25377 } 25378 } else if (be_chatty) { 25379 /* 25380 * suppress messages if they are all the same pkt reason; 25381 * with TQ, many (up to 256) are returned with the same 25382 * pkt_reason 25383 */ 25384 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25385 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25386 "sd_mhd_watch_incomplete: " 25387 "SCSI transport failed: reason '%s'\n", 25388 scsi_rname(pkt->pkt_reason)); 25389 } 25390 } 25391 un->un_last_pkt_reason = pkt->pkt_reason; 25392 mutex_exit(SD_MUTEX(un)); 25393 } 25394 25395 25396 /* 25397 * Function: sd_sname() 25398 * 25399 * Description: This is a simple little routine to return a string containing 25400 * a printable description of command status byte for use in 25401 * logging. 25402 * 25403 * Arguments: status - pointer to a status byte 25404 * 25405 * Return Code: char * - string containing status description. 25406 */ 25407 25408 static char * 25409 sd_sname(uchar_t status) 25410 { 25411 switch (status & STATUS_MASK) { 25412 case STATUS_GOOD: 25413 return ("good status"); 25414 case STATUS_CHECK: 25415 return ("check condition"); 25416 case STATUS_MET: 25417 return ("condition met"); 25418 case STATUS_BUSY: 25419 return ("busy"); 25420 case STATUS_INTERMEDIATE: 25421 return ("intermediate"); 25422 case STATUS_INTERMEDIATE_MET: 25423 return ("intermediate - condition met"); 25424 case STATUS_RESERVATION_CONFLICT: 25425 return ("reservation_conflict"); 25426 case STATUS_TERMINATED: 25427 return ("command terminated"); 25428 case STATUS_QFULL: 25429 return ("queue full"); 25430 default: 25431 return ("<unknown status>"); 25432 } 25433 } 25434 25435 25436 /* 25437 * Function: sd_mhd_resvd_recover() 25438 * 25439 * Description: This function adds a reservation entry to the 25440 * sd_resv_reclaim_request list and signals the reservation 25441 * reclaim thread that there is work pending. If the reservation 25442 * reclaim thread has not been previously created this function 25443 * will kick it off. 25444 * 25445 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25446 * among multiple watches that share this callback function 25447 * 25448 * Context: This routine is called by timeout() and is run in interrupt 25449 * context. It must not sleep or call other functions which may 25450 * sleep. 25451 */ 25452 25453 static void 25454 sd_mhd_resvd_recover(void *arg) 25455 { 25456 dev_t dev = (dev_t)arg; 25457 struct sd_lun *un; 25458 struct sd_thr_request *sd_treq = NULL; 25459 struct sd_thr_request *sd_cur = NULL; 25460 struct sd_thr_request *sd_prev = NULL; 25461 int already_there = 0; 25462 25463 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25464 return; 25465 } 25466 25467 mutex_enter(SD_MUTEX(un)); 25468 un->un_resvd_timeid = NULL; 25469 if (un->un_resvd_status & SD_WANT_RESERVE) { 25470 /* 25471 * There was a reset so don't issue the reserve, allow the 25472 * sd_mhd_watch_cb callback function to notice this and 25473 * reschedule the timeout for reservation. 25474 */ 25475 mutex_exit(SD_MUTEX(un)); 25476 return; 25477 } 25478 mutex_exit(SD_MUTEX(un)); 25479 25480 /* 25481 * Add this device to the sd_resv_reclaim_request list and the 25482 * sd_resv_reclaim_thread should take care of the rest. 25483 * 25484 * Note: We can't sleep in this context so if the memory allocation 25485 * fails allow the sd_mhd_watch_cb callback function to notice this and 25486 * reschedule the timeout for reservation. (4378460) 25487 */ 25488 sd_treq = (struct sd_thr_request *) 25489 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25490 if (sd_treq == NULL) { 25491 return; 25492 } 25493 25494 sd_treq->sd_thr_req_next = NULL; 25495 sd_treq->dev = dev; 25496 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25497 if (sd_tr.srq_thr_req_head == NULL) { 25498 sd_tr.srq_thr_req_head = sd_treq; 25499 } else { 25500 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25501 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25502 if (sd_cur->dev == dev) { 25503 /* 25504 * already in Queue so don't log 25505 * another request for the device 25506 */ 25507 already_there = 1; 25508 break; 25509 } 25510 sd_prev = sd_cur; 25511 } 25512 if (!already_there) { 25513 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25514 "logging request for %lx\n", dev); 25515 sd_prev->sd_thr_req_next = sd_treq; 25516 } else { 25517 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25518 } 25519 } 25520 25521 /* 25522 * Create a kernel thread to do the reservation reclaim and free up this 25523 * thread. We cannot block this thread while we go away to do the 25524 * reservation reclaim 25525 */ 25526 if (sd_tr.srq_resv_reclaim_thread == NULL) 25527 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25528 sd_resv_reclaim_thread, NULL, 25529 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25530 25531 /* Tell the reservation reclaim thread that it has work to do */ 25532 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25533 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25534 } 25535 25536 /* 25537 * Function: sd_resv_reclaim_thread() 25538 * 25539 * Description: This function implements the reservation reclaim operations 25540 * 25541 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25542 * among multiple watches that share this callback function 25543 */ 25544 25545 static void 25546 sd_resv_reclaim_thread() 25547 { 25548 struct sd_lun *un; 25549 struct sd_thr_request *sd_mhreq; 25550 25551 /* Wait for work */ 25552 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25553 if (sd_tr.srq_thr_req_head == NULL) { 25554 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25555 &sd_tr.srq_resv_reclaim_mutex); 25556 } 25557 25558 /* Loop while we have work */ 25559 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25560 un = ddi_get_soft_state(sd_state, 25561 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25562 if (un == NULL) { 25563 /* 25564 * softstate structure is NULL so just 25565 * dequeue the request and continue 25566 */ 25567 sd_tr.srq_thr_req_head = 25568 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25569 kmem_free(sd_tr.srq_thr_cur_req, 25570 sizeof (struct sd_thr_request)); 25571 continue; 25572 } 25573 25574 /* dequeue the request */ 25575 sd_mhreq = sd_tr.srq_thr_cur_req; 25576 sd_tr.srq_thr_req_head = 25577 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25578 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25579 25580 /* 25581 * Reclaim reservation only if SD_RESERVE is still set. There 25582 * may have been a call to MHIOCRELEASE before we got here. 25583 */ 25584 mutex_enter(SD_MUTEX(un)); 25585 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25586 /* 25587 * Note: The SD_LOST_RESERVE flag is cleared before 25588 * reclaiming the reservation. If this is done after the 25589 * call to sd_reserve_release a reservation loss in the 25590 * window between pkt completion of reserve cmd and 25591 * mutex_enter below may not be recognized 25592 */ 25593 un->un_resvd_status &= ~SD_LOST_RESERVE; 25594 mutex_exit(SD_MUTEX(un)); 25595 25596 if (sd_reserve_release(sd_mhreq->dev, 25597 SD_RESERVE) == 0) { 25598 mutex_enter(SD_MUTEX(un)); 25599 un->un_resvd_status |= SD_RESERVE; 25600 mutex_exit(SD_MUTEX(un)); 25601 SD_INFO(SD_LOG_IOCTL_MHD, un, 25602 "sd_resv_reclaim_thread: " 25603 "Reservation Recovered\n"); 25604 } else { 25605 mutex_enter(SD_MUTEX(un)); 25606 un->un_resvd_status |= SD_LOST_RESERVE; 25607 mutex_exit(SD_MUTEX(un)); 25608 SD_INFO(SD_LOG_IOCTL_MHD, un, 25609 "sd_resv_reclaim_thread: Failed " 25610 "Reservation Recovery\n"); 25611 } 25612 } else { 25613 mutex_exit(SD_MUTEX(un)); 25614 } 25615 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25616 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25617 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25618 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25619 /* 25620 * wakeup the destroy thread if anyone is waiting on 25621 * us to complete. 25622 */ 25623 cv_signal(&sd_tr.srq_inprocess_cv); 25624 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25625 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25626 } 25627 25628 /* 25629 * cleanup the sd_tr structure now that this thread will not exist 25630 */ 25631 ASSERT(sd_tr.srq_thr_req_head == NULL); 25632 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25633 sd_tr.srq_resv_reclaim_thread = NULL; 25634 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25635 thread_exit(); 25636 } 25637 25638 25639 /* 25640 * Function: sd_rmv_resv_reclaim_req() 25641 * 25642 * Description: This function removes any pending reservation reclaim requests 25643 * for the specified device. 25644 * 25645 * Arguments: dev - the device 'dev_t' 25646 */ 25647 25648 static void 25649 sd_rmv_resv_reclaim_req(dev_t dev) 25650 { 25651 struct sd_thr_request *sd_mhreq; 25652 struct sd_thr_request *sd_prev; 25653 25654 /* Remove a reservation reclaim request from the list */ 25655 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25656 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25657 /* 25658 * We are attempting to reinstate reservation for 25659 * this device. We wait for sd_reserve_release() 25660 * to return before we return. 25661 */ 25662 cv_wait(&sd_tr.srq_inprocess_cv, 25663 &sd_tr.srq_resv_reclaim_mutex); 25664 } else { 25665 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25666 if (sd_mhreq && sd_mhreq->dev == dev) { 25667 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25668 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25669 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25670 return; 25671 } 25672 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25673 if (sd_mhreq && sd_mhreq->dev == dev) { 25674 break; 25675 } 25676 sd_prev = sd_mhreq; 25677 } 25678 if (sd_mhreq != NULL) { 25679 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25680 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25681 } 25682 } 25683 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25684 } 25685 25686 25687 /* 25688 * Function: sd_mhd_reset_notify_cb() 25689 * 25690 * Description: This is a call back function for scsi_reset_notify. This 25691 * function updates the softstate reserved status and logs the 25692 * reset. The driver scsi watch facility callback function 25693 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25694 * will reclaim the reservation. 25695 * 25696 * Arguments: arg - driver soft state (unit) structure 25697 */ 25698 25699 static void 25700 sd_mhd_reset_notify_cb(caddr_t arg) 25701 { 25702 struct sd_lun *un = (struct sd_lun *)arg; 25703 25704 mutex_enter(SD_MUTEX(un)); 25705 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25706 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25707 SD_INFO(SD_LOG_IOCTL_MHD, un, 25708 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25709 } 25710 mutex_exit(SD_MUTEX(un)); 25711 } 25712 25713 25714 /* 25715 * Function: sd_take_ownership() 25716 * 25717 * Description: This routine implements an algorithm to achieve a stable 25718 * reservation on disks which don't implement priority reserve, 25719 * and makes sure that other host lose re-reservation attempts. 25720 * This algorithm contains of a loop that keeps issuing the RESERVE 25721 * for some period of time (min_ownership_delay, default 6 seconds) 25722 * During that loop, it looks to see if there has been a bus device 25723 * reset or bus reset (both of which cause an existing reservation 25724 * to be lost). If the reservation is lost issue RESERVE until a 25725 * period of min_ownership_delay with no resets has gone by, or 25726 * until max_ownership_delay has expired. This loop ensures that 25727 * the host really did manage to reserve the device, in spite of 25728 * resets. The looping for min_ownership_delay (default six 25729 * seconds) is important to early generation clustering products, 25730 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25731 * MHIOCENFAILFAST periodic timer of two seconds. By having 25732 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25733 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25734 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25735 * have already noticed, via the MHIOCENFAILFAST polling, that it 25736 * no longer "owns" the disk and will have panicked itself. Thus, 25737 * the host issuing the MHIOCTKOWN is assured (with timing 25738 * dependencies) that by the time it actually starts to use the 25739 * disk for real work, the old owner is no longer accessing it. 25740 * 25741 * min_ownership_delay is the minimum amount of time for which the 25742 * disk must be reserved continuously devoid of resets before the 25743 * MHIOCTKOWN ioctl will return success. 25744 * 25745 * max_ownership_delay indicates the amount of time by which the 25746 * take ownership should succeed or timeout with an error. 25747 * 25748 * Arguments: dev - the device 'dev_t' 25749 * *p - struct containing timing info. 25750 * 25751 * Return Code: 0 for success or error code 25752 */ 25753 25754 static int 25755 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25756 { 25757 struct sd_lun *un; 25758 int rval; 25759 int err; 25760 int reservation_count = 0; 25761 int min_ownership_delay = 6000000; /* in usec */ 25762 int max_ownership_delay = 30000000; /* in usec */ 25763 clock_t start_time; /* starting time of this algorithm */ 25764 clock_t end_time; /* time limit for giving up */ 25765 clock_t ownership_time; /* time limit for stable ownership */ 25766 clock_t current_time; 25767 clock_t previous_current_time; 25768 25769 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25770 return (ENXIO); 25771 } 25772 25773 /* 25774 * Attempt a device reservation. A priority reservation is requested. 25775 */ 25776 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25777 != SD_SUCCESS) { 25778 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25779 "sd_take_ownership: return(1)=%d\n", rval); 25780 return (rval); 25781 } 25782 25783 /* Update the softstate reserved status to indicate the reservation */ 25784 mutex_enter(SD_MUTEX(un)); 25785 un->un_resvd_status |= SD_RESERVE; 25786 un->un_resvd_status &= 25787 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25788 mutex_exit(SD_MUTEX(un)); 25789 25790 if (p != NULL) { 25791 if (p->min_ownership_delay != 0) { 25792 min_ownership_delay = p->min_ownership_delay * 1000; 25793 } 25794 if (p->max_ownership_delay != 0) { 25795 max_ownership_delay = p->max_ownership_delay * 1000; 25796 } 25797 } 25798 SD_INFO(SD_LOG_IOCTL_MHD, un, 25799 "sd_take_ownership: min, max delays: %d, %d\n", 25800 min_ownership_delay, max_ownership_delay); 25801 25802 start_time = ddi_get_lbolt(); 25803 current_time = start_time; 25804 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25805 end_time = start_time + drv_usectohz(max_ownership_delay); 25806 25807 while (current_time - end_time < 0) { 25808 delay(drv_usectohz(500000)); 25809 25810 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25811 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25812 mutex_enter(SD_MUTEX(un)); 25813 rval = (un->un_resvd_status & 25814 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25815 mutex_exit(SD_MUTEX(un)); 25816 break; 25817 } 25818 } 25819 previous_current_time = current_time; 25820 current_time = ddi_get_lbolt(); 25821 mutex_enter(SD_MUTEX(un)); 25822 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25823 ownership_time = ddi_get_lbolt() + 25824 drv_usectohz(min_ownership_delay); 25825 reservation_count = 0; 25826 } else { 25827 reservation_count++; 25828 } 25829 un->un_resvd_status |= SD_RESERVE; 25830 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25831 mutex_exit(SD_MUTEX(un)); 25832 25833 SD_INFO(SD_LOG_IOCTL_MHD, un, 25834 "sd_take_ownership: ticks for loop iteration=%ld, " 25835 "reservation=%s\n", (current_time - previous_current_time), 25836 reservation_count ? "ok" : "reclaimed"); 25837 25838 if (current_time - ownership_time >= 0 && 25839 reservation_count >= 4) { 25840 rval = 0; /* Achieved a stable ownership */ 25841 break; 25842 } 25843 if (current_time - end_time >= 0) { 25844 rval = EACCES; /* No ownership in max possible time */ 25845 break; 25846 } 25847 } 25848 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25849 "sd_take_ownership: return(2)=%d\n", rval); 25850 return (rval); 25851 } 25852 25853 25854 /* 25855 * Function: sd_reserve_release() 25856 * 25857 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25858 * PRIORITY RESERVE commands based on a user specified command type 25859 * 25860 * Arguments: dev - the device 'dev_t' 25861 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25862 * SD_RESERVE, SD_RELEASE 25863 * 25864 * Return Code: 0 or Error Code 25865 */ 25866 25867 static int 25868 sd_reserve_release(dev_t dev, int cmd) 25869 { 25870 struct uscsi_cmd *com = NULL; 25871 struct sd_lun *un = NULL; 25872 char cdb[CDB_GROUP0]; 25873 int rval; 25874 25875 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25876 (cmd == SD_PRIORITY_RESERVE)); 25877 25878 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25879 return (ENXIO); 25880 } 25881 25882 /* instantiate and initialize the command and cdb */ 25883 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25884 bzero(cdb, CDB_GROUP0); 25885 com->uscsi_flags = USCSI_SILENT; 25886 com->uscsi_timeout = un->un_reserve_release_time; 25887 com->uscsi_cdblen = CDB_GROUP0; 25888 com->uscsi_cdb = cdb; 25889 if (cmd == SD_RELEASE) { 25890 cdb[0] = SCMD_RELEASE; 25891 } else { 25892 cdb[0] = SCMD_RESERVE; 25893 } 25894 25895 /* Send the command. */ 25896 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25897 UIO_SYSSPACE, SD_PATH_STANDARD); 25898 25899 /* 25900 * "break" a reservation that is held by another host, by issuing a 25901 * reset if priority reserve is desired, and we could not get the 25902 * device. 25903 */ 25904 if ((cmd == SD_PRIORITY_RESERVE) && 25905 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25906 /* 25907 * First try to reset the LUN. If we cannot, then try a target 25908 * reset, followed by a bus reset if the target reset fails. 25909 */ 25910 int reset_retval = 0; 25911 if (un->un_f_lun_reset_enabled == TRUE) { 25912 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25913 } 25914 if (reset_retval == 0) { 25915 /* The LUN reset either failed or was not issued */ 25916 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25917 } 25918 if ((reset_retval == 0) && 25919 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25920 rval = EIO; 25921 kmem_free(com, sizeof (*com)); 25922 return (rval); 25923 } 25924 25925 bzero(com, sizeof (struct uscsi_cmd)); 25926 com->uscsi_flags = USCSI_SILENT; 25927 com->uscsi_cdb = cdb; 25928 com->uscsi_cdblen = CDB_GROUP0; 25929 com->uscsi_timeout = 5; 25930 25931 /* 25932 * Reissue the last reserve command, this time without request 25933 * sense. Assume that it is just a regular reserve command. 25934 */ 25935 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25936 UIO_SYSSPACE, SD_PATH_STANDARD); 25937 } 25938 25939 /* Return an error if still getting a reservation conflict. */ 25940 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25941 rval = EACCES; 25942 } 25943 25944 kmem_free(com, sizeof (*com)); 25945 return (rval); 25946 } 25947 25948 25949 #define SD_NDUMP_RETRIES 12 25950 /* 25951 * System Crash Dump routine 25952 */ 25953 25954 static int 25955 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25956 { 25957 int instance; 25958 int partition; 25959 int i; 25960 int err; 25961 struct sd_lun *un; 25962 struct dk_map *lp; 25963 struct scsi_pkt *wr_pktp; 25964 struct buf *wr_bp; 25965 struct buf wr_buf; 25966 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25967 daddr_t tgt_blkno; /* rmw - blkno for target */ 25968 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25969 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25970 size_t io_start_offset; 25971 int doing_rmw = FALSE; 25972 int rval; 25973 #if defined(__i386) || defined(__amd64) 25974 ssize_t dma_resid; 25975 daddr_t oblkno; 25976 #endif 25977 25978 instance = SDUNIT(dev); 25979 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25980 (!un->un_f_geometry_is_valid) || ISCD(un)) { 25981 return (ENXIO); 25982 } 25983 25984 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25985 25986 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25987 25988 partition = SDPART(dev); 25989 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25990 25991 /* Validate blocks to dump at against partition size. */ 25992 lp = &un->un_map[partition]; 25993 if ((blkno + nblk) > lp->dkl_nblk) { 25994 SD_TRACE(SD_LOG_DUMP, un, 25995 "sddump: dump range larger than partition: " 25996 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25997 blkno, nblk, lp->dkl_nblk); 25998 return (EINVAL); 25999 } 26000 26001 mutex_enter(&un->un_pm_mutex); 26002 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26003 struct scsi_pkt *start_pktp; 26004 26005 mutex_exit(&un->un_pm_mutex); 26006 26007 /* 26008 * use pm framework to power on HBA 1st 26009 */ 26010 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26011 26012 /* 26013 * Dump no long uses sdpower to power on a device, it's 26014 * in-line here so it can be done in polled mode. 26015 */ 26016 26017 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26018 26019 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26020 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26021 26022 if (start_pktp == NULL) { 26023 /* We were not given a SCSI packet, fail. */ 26024 return (EIO); 26025 } 26026 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26027 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26028 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26029 start_pktp->pkt_flags = FLAG_NOINTR; 26030 26031 mutex_enter(SD_MUTEX(un)); 26032 SD_FILL_SCSI1_LUN(un, start_pktp); 26033 mutex_exit(SD_MUTEX(un)); 26034 /* 26035 * Scsi_poll returns 0 (success) if the command completes and 26036 * the status block is STATUS_GOOD. 26037 */ 26038 if (sd_scsi_poll(un, start_pktp) != 0) { 26039 scsi_destroy_pkt(start_pktp); 26040 return (EIO); 26041 } 26042 scsi_destroy_pkt(start_pktp); 26043 (void) sd_ddi_pm_resume(un); 26044 } else { 26045 mutex_exit(&un->un_pm_mutex); 26046 } 26047 26048 mutex_enter(SD_MUTEX(un)); 26049 un->un_throttle = 0; 26050 26051 /* 26052 * The first time through, reset the specific target device. 26053 * However, when cpr calls sddump we know that sd is in a 26054 * a good state so no bus reset is required. 26055 * Clear sense data via Request Sense cmd. 26056 * In sddump we don't care about allow_bus_device_reset anymore 26057 */ 26058 26059 if ((un->un_state != SD_STATE_SUSPENDED) && 26060 (un->un_state != SD_STATE_DUMPING)) { 26061 26062 New_state(un, SD_STATE_DUMPING); 26063 26064 if (un->un_f_is_fibre == FALSE) { 26065 mutex_exit(SD_MUTEX(un)); 26066 /* 26067 * Attempt a bus reset for parallel scsi. 26068 * 26069 * Note: A bus reset is required because on some host 26070 * systems (i.e. E420R) a bus device reset is 26071 * insufficient to reset the state of the target. 26072 * 26073 * Note: Don't issue the reset for fibre-channel, 26074 * because this tends to hang the bus (loop) for 26075 * too long while everyone is logging out and in 26076 * and the deadman timer for dumping will fire 26077 * before the dump is complete. 26078 */ 26079 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26080 mutex_enter(SD_MUTEX(un)); 26081 Restore_state(un); 26082 mutex_exit(SD_MUTEX(un)); 26083 return (EIO); 26084 } 26085 26086 /* Delay to give the device some recovery time. */ 26087 drv_usecwait(10000); 26088 26089 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26090 SD_INFO(SD_LOG_DUMP, un, 26091 "sddump: sd_send_polled_RQS failed\n"); 26092 } 26093 mutex_enter(SD_MUTEX(un)); 26094 } 26095 } 26096 26097 /* 26098 * Convert the partition-relative block number to a 26099 * disk physical block number. 26100 */ 26101 blkno += un->un_offset[partition]; 26102 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26103 26104 26105 /* 26106 * Check if the device has a non-512 block size. 26107 */ 26108 wr_bp = NULL; 26109 if (NOT_DEVBSIZE(un)) { 26110 tgt_byte_offset = blkno * un->un_sys_blocksize; 26111 tgt_byte_count = nblk * un->un_sys_blocksize; 26112 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26113 (tgt_byte_count % un->un_tgt_blocksize)) { 26114 doing_rmw = TRUE; 26115 /* 26116 * Calculate the block number and number of block 26117 * in terms of the media block size. 26118 */ 26119 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26120 tgt_nblk = 26121 ((tgt_byte_offset + tgt_byte_count + 26122 (un->un_tgt_blocksize - 1)) / 26123 un->un_tgt_blocksize) - tgt_blkno; 26124 26125 /* 26126 * Invoke the routine which is going to do read part 26127 * of read-modify-write. 26128 * Note that this routine returns a pointer to 26129 * a valid bp in wr_bp. 26130 */ 26131 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26132 &wr_bp); 26133 if (err) { 26134 mutex_exit(SD_MUTEX(un)); 26135 return (err); 26136 } 26137 /* 26138 * Offset is being calculated as - 26139 * (original block # * system block size) - 26140 * (new block # * target block size) 26141 */ 26142 io_start_offset = 26143 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26144 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26145 26146 ASSERT((io_start_offset >= 0) && 26147 (io_start_offset < un->un_tgt_blocksize)); 26148 /* 26149 * Do the modify portion of read modify write. 26150 */ 26151 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26152 (size_t)nblk * un->un_sys_blocksize); 26153 } else { 26154 doing_rmw = FALSE; 26155 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26156 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26157 } 26158 26159 /* Convert blkno and nblk to target blocks */ 26160 blkno = tgt_blkno; 26161 nblk = tgt_nblk; 26162 } else { 26163 wr_bp = &wr_buf; 26164 bzero(wr_bp, sizeof (struct buf)); 26165 wr_bp->b_flags = B_BUSY; 26166 wr_bp->b_un.b_addr = addr; 26167 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26168 wr_bp->b_resid = 0; 26169 } 26170 26171 mutex_exit(SD_MUTEX(un)); 26172 26173 /* 26174 * Obtain a SCSI packet for the write command. 26175 * It should be safe to call the allocator here without 26176 * worrying about being locked for DVMA mapping because 26177 * the address we're passed is already a DVMA mapping 26178 * 26179 * We are also not going to worry about semaphore ownership 26180 * in the dump buffer. Dumping is single threaded at present. 26181 */ 26182 26183 wr_pktp = NULL; 26184 26185 #if defined(__i386) || defined(__amd64) 26186 dma_resid = wr_bp->b_bcount; 26187 oblkno = blkno; 26188 while (dma_resid != 0) { 26189 #endif 26190 26191 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26192 wr_bp->b_flags &= ~B_ERROR; 26193 26194 #if defined(__i386) || defined(__amd64) 26195 blkno = oblkno + 26196 ((wr_bp->b_bcount - dma_resid) / 26197 un->un_tgt_blocksize); 26198 nblk = dma_resid / un->un_tgt_blocksize; 26199 26200 if (wr_pktp) { 26201 /* Partial DMA transfers after initial transfer */ 26202 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26203 blkno, nblk); 26204 } else { 26205 /* Initial transfer */ 26206 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26207 un->un_pkt_flags, NULL_FUNC, NULL, 26208 blkno, nblk); 26209 } 26210 #else 26211 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26212 0, NULL_FUNC, NULL, blkno, nblk); 26213 #endif 26214 26215 if (rval == 0) { 26216 /* We were given a SCSI packet, continue. */ 26217 break; 26218 } 26219 26220 if (i == 0) { 26221 if (wr_bp->b_flags & B_ERROR) { 26222 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26223 "no resources for dumping; " 26224 "error code: 0x%x, retrying", 26225 geterror(wr_bp)); 26226 } else { 26227 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26228 "no resources for dumping; retrying"); 26229 } 26230 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26231 if (wr_bp->b_flags & B_ERROR) { 26232 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26233 "no resources for dumping; error code: " 26234 "0x%x, retrying\n", geterror(wr_bp)); 26235 } 26236 } else { 26237 if (wr_bp->b_flags & B_ERROR) { 26238 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26239 "no resources for dumping; " 26240 "error code: 0x%x, retries failed, " 26241 "giving up.\n", geterror(wr_bp)); 26242 } else { 26243 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26244 "no resources for dumping; " 26245 "retries failed, giving up.\n"); 26246 } 26247 mutex_enter(SD_MUTEX(un)); 26248 Restore_state(un); 26249 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26250 mutex_exit(SD_MUTEX(un)); 26251 scsi_free_consistent_buf(wr_bp); 26252 } else { 26253 mutex_exit(SD_MUTEX(un)); 26254 } 26255 return (EIO); 26256 } 26257 drv_usecwait(10000); 26258 } 26259 26260 #if defined(__i386) || defined(__amd64) 26261 /* 26262 * save the resid from PARTIAL_DMA 26263 */ 26264 dma_resid = wr_pktp->pkt_resid; 26265 if (dma_resid != 0) 26266 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26267 wr_pktp->pkt_resid = 0; 26268 #endif 26269 26270 /* SunBug 1222170 */ 26271 wr_pktp->pkt_flags = FLAG_NOINTR; 26272 26273 err = EIO; 26274 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26275 26276 /* 26277 * Scsi_poll returns 0 (success) if the command completes and 26278 * the status block is STATUS_GOOD. We should only check 26279 * errors if this condition is not true. Even then we should 26280 * send our own request sense packet only if we have a check 26281 * condition and auto request sense has not been performed by 26282 * the hba. 26283 */ 26284 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26285 26286 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26287 (wr_pktp->pkt_resid == 0)) { 26288 err = SD_SUCCESS; 26289 break; 26290 } 26291 26292 /* 26293 * Check CMD_DEV_GONE 1st, give up if device is gone. 26294 */ 26295 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26296 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26297 "Device is gone\n"); 26298 break; 26299 } 26300 26301 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26302 SD_INFO(SD_LOG_DUMP, un, 26303 "sddump: write failed with CHECK, try # %d\n", i); 26304 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26305 (void) sd_send_polled_RQS(un); 26306 } 26307 26308 continue; 26309 } 26310 26311 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26312 int reset_retval = 0; 26313 26314 SD_INFO(SD_LOG_DUMP, un, 26315 "sddump: write failed with BUSY, try # %d\n", i); 26316 26317 if (un->un_f_lun_reset_enabled == TRUE) { 26318 reset_retval = scsi_reset(SD_ADDRESS(un), 26319 RESET_LUN); 26320 } 26321 if (reset_retval == 0) { 26322 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26323 } 26324 (void) sd_send_polled_RQS(un); 26325 26326 } else { 26327 SD_INFO(SD_LOG_DUMP, un, 26328 "sddump: write failed with 0x%x, try # %d\n", 26329 SD_GET_PKT_STATUS(wr_pktp), i); 26330 mutex_enter(SD_MUTEX(un)); 26331 sd_reset_target(un, wr_pktp); 26332 mutex_exit(SD_MUTEX(un)); 26333 } 26334 26335 /* 26336 * If we are not getting anywhere with lun/target resets, 26337 * let's reset the bus. 26338 */ 26339 if (i == SD_NDUMP_RETRIES/2) { 26340 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26341 (void) sd_send_polled_RQS(un); 26342 } 26343 26344 } 26345 #if defined(__i386) || defined(__amd64) 26346 } /* dma_resid */ 26347 #endif 26348 26349 scsi_destroy_pkt(wr_pktp); 26350 mutex_enter(SD_MUTEX(un)); 26351 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26352 mutex_exit(SD_MUTEX(un)); 26353 scsi_free_consistent_buf(wr_bp); 26354 } else { 26355 mutex_exit(SD_MUTEX(un)); 26356 } 26357 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26358 return (err); 26359 } 26360 26361 /* 26362 * Function: sd_scsi_poll() 26363 * 26364 * Description: This is a wrapper for the scsi_poll call. 26365 * 26366 * Arguments: sd_lun - The unit structure 26367 * scsi_pkt - The scsi packet being sent to the device. 26368 * 26369 * Return Code: 0 - Command completed successfully with good status 26370 * -1 - Command failed. This could indicate a check condition 26371 * or other status value requiring recovery action. 26372 * 26373 */ 26374 26375 static int 26376 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26377 { 26378 int status; 26379 26380 ASSERT(un != NULL); 26381 ASSERT(!mutex_owned(SD_MUTEX(un))); 26382 ASSERT(pktp != NULL); 26383 26384 status = SD_SUCCESS; 26385 26386 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26387 pktp->pkt_flags |= un->un_tagflags; 26388 pktp->pkt_flags &= ~FLAG_NODISCON; 26389 } 26390 26391 status = sd_ddi_scsi_poll(pktp); 26392 /* 26393 * Scsi_poll returns 0 (success) if the command completes and the 26394 * status block is STATUS_GOOD. We should only check errors if this 26395 * condition is not true. Even then we should send our own request 26396 * sense packet only if we have a check condition and auto 26397 * request sense has not been performed by the hba. 26398 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26399 */ 26400 if ((status != SD_SUCCESS) && 26401 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26402 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26403 (pktp->pkt_reason != CMD_DEV_GONE)) 26404 (void) sd_send_polled_RQS(un); 26405 26406 return (status); 26407 } 26408 26409 /* 26410 * Function: sd_send_polled_RQS() 26411 * 26412 * Description: This sends the request sense command to a device. 26413 * 26414 * Arguments: sd_lun - The unit structure 26415 * 26416 * Return Code: 0 - Command completed successfully with good status 26417 * -1 - Command failed. 26418 * 26419 */ 26420 26421 static int 26422 sd_send_polled_RQS(struct sd_lun *un) 26423 { 26424 int ret_val; 26425 struct scsi_pkt *rqs_pktp; 26426 struct buf *rqs_bp; 26427 26428 ASSERT(un != NULL); 26429 ASSERT(!mutex_owned(SD_MUTEX(un))); 26430 26431 ret_val = SD_SUCCESS; 26432 26433 rqs_pktp = un->un_rqs_pktp; 26434 rqs_bp = un->un_rqs_bp; 26435 26436 mutex_enter(SD_MUTEX(un)); 26437 26438 if (un->un_sense_isbusy) { 26439 ret_val = SD_FAILURE; 26440 mutex_exit(SD_MUTEX(un)); 26441 return (ret_val); 26442 } 26443 26444 /* 26445 * If the request sense buffer (and packet) is not in use, 26446 * let's set the un_sense_isbusy and send our packet 26447 */ 26448 un->un_sense_isbusy = 1; 26449 rqs_pktp->pkt_resid = 0; 26450 rqs_pktp->pkt_reason = 0; 26451 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26452 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26453 26454 mutex_exit(SD_MUTEX(un)); 26455 26456 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26457 " 0x%p\n", rqs_bp->b_un.b_addr); 26458 26459 /* 26460 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26461 * axle - it has a call into us! 26462 */ 26463 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26464 SD_INFO(SD_LOG_COMMON, un, 26465 "sd_send_polled_RQS: RQS failed\n"); 26466 } 26467 26468 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26469 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26470 26471 mutex_enter(SD_MUTEX(un)); 26472 un->un_sense_isbusy = 0; 26473 mutex_exit(SD_MUTEX(un)); 26474 26475 return (ret_val); 26476 } 26477 26478 /* 26479 * Defines needed for localized version of the scsi_poll routine. 26480 */ 26481 #define SD_CSEC 10000 /* usecs */ 26482 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26483 26484 26485 /* 26486 * Function: sd_ddi_scsi_poll() 26487 * 26488 * Description: Localized version of the scsi_poll routine. The purpose is to 26489 * send a scsi_pkt to a device as a polled command. This version 26490 * is to ensure more robust handling of transport errors. 26491 * Specifically this routine cures not ready, coming ready 26492 * transition for power up and reset of sonoma's. This can take 26493 * up to 45 seconds for power-on and 20 seconds for reset of a 26494 * sonoma lun. 26495 * 26496 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26497 * 26498 * Return Code: 0 - Command completed successfully with good status 26499 * -1 - Command failed. 26500 * 26501 */ 26502 26503 static int 26504 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26505 { 26506 int busy_count; 26507 int timeout; 26508 int rval = SD_FAILURE; 26509 int savef; 26510 struct scsi_extended_sense *sensep; 26511 long savet; 26512 void (*savec)(); 26513 /* 26514 * The following is defined in machdep.c and is used in determining if 26515 * the scsi transport system will do polled I/O instead of interrupt 26516 * I/O when called from xx_dump(). 26517 */ 26518 extern int do_polled_io; 26519 26520 /* 26521 * save old flags in pkt, to restore at end 26522 */ 26523 savef = pkt->pkt_flags; 26524 savec = pkt->pkt_comp; 26525 savet = pkt->pkt_time; 26526 26527 pkt->pkt_flags |= FLAG_NOINTR; 26528 26529 /* 26530 * XXX there is nothing in the SCSA spec that states that we should not 26531 * do a callback for polled cmds; however, removing this will break sd 26532 * and probably other target drivers 26533 */ 26534 pkt->pkt_comp = NULL; 26535 26536 /* 26537 * we don't like a polled command without timeout. 26538 * 60 seconds seems long enough. 26539 */ 26540 if (pkt->pkt_time == 0) { 26541 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26542 } 26543 26544 /* 26545 * Send polled cmd. 26546 * 26547 * We do some error recovery for various errors. Tran_busy, 26548 * queue full, and non-dispatched commands are retried every 10 msec. 26549 * as they are typically transient failures. Busy status and Not 26550 * Ready are retried every second as this status takes a while to 26551 * change. Unit attention is retried for pkt_time (60) times 26552 * with no delay. 26553 */ 26554 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26555 26556 for (busy_count = 0; busy_count < timeout; busy_count++) { 26557 int rc; 26558 int poll_delay; 26559 26560 /* 26561 * Initialize pkt status variables. 26562 */ 26563 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26564 26565 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26566 if (rc != TRAN_BUSY) { 26567 /* Transport failed - give up. */ 26568 break; 26569 } else { 26570 /* Transport busy - try again. */ 26571 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26572 } 26573 } else { 26574 /* 26575 * Transport accepted - check pkt status. 26576 */ 26577 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26578 if (pkt->pkt_reason == CMD_CMPLT && 26579 rc == STATUS_CHECK && 26580 pkt->pkt_state & STATE_ARQ_DONE) { 26581 struct scsi_arq_status *arqstat = 26582 (struct scsi_arq_status *)(pkt->pkt_scbp); 26583 26584 sensep = &arqstat->sts_sensedata; 26585 } else { 26586 sensep = NULL; 26587 } 26588 26589 if ((pkt->pkt_reason == CMD_CMPLT) && 26590 (rc == STATUS_GOOD)) { 26591 /* No error - we're done */ 26592 rval = SD_SUCCESS; 26593 break; 26594 26595 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26596 /* Lost connection - give up */ 26597 break; 26598 26599 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26600 (pkt->pkt_state == 0)) { 26601 /* Pkt not dispatched - try again. */ 26602 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26603 26604 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26605 (rc == STATUS_QFULL)) { 26606 /* Queue full - try again. */ 26607 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26608 26609 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26610 (rc == STATUS_BUSY)) { 26611 /* Busy - try again. */ 26612 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26613 busy_count += (SD_SEC_TO_CSEC - 1); 26614 26615 } else if ((sensep != NULL) && 26616 (sensep->es_key == KEY_UNIT_ATTENTION)) { 26617 /* Unit Attention - try again */ 26618 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26619 continue; 26620 26621 } else if ((sensep != NULL) && 26622 (sensep->es_key == KEY_NOT_READY) && 26623 (sensep->es_add_code == 0x04) && 26624 (sensep->es_qual_code == 0x01)) { 26625 /* Not ready -> ready - try again. */ 26626 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26627 busy_count += (SD_SEC_TO_CSEC - 1); 26628 26629 } else { 26630 /* BAD status - give up. */ 26631 break; 26632 } 26633 } 26634 26635 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26636 !do_polled_io) { 26637 delay(drv_usectohz(poll_delay)); 26638 } else { 26639 /* we busy wait during cpr_dump or interrupt threads */ 26640 drv_usecwait(poll_delay); 26641 } 26642 } 26643 26644 pkt->pkt_flags = savef; 26645 pkt->pkt_comp = savec; 26646 pkt->pkt_time = savet; 26647 return (rval); 26648 } 26649 26650 26651 /* 26652 * Function: sd_persistent_reservation_in_read_keys 26653 * 26654 * Description: This routine is the driver entry point for handling CD-ROM 26655 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26656 * by sending the SCSI-3 PRIN commands to the device. 26657 * Processes the read keys command response by copying the 26658 * reservation key information into the user provided buffer. 26659 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26660 * 26661 * Arguments: un - Pointer to soft state struct for the target. 26662 * usrp - user provided pointer to multihost Persistent In Read 26663 * Keys structure (mhioc_inkeys_t) 26664 * flag - this argument is a pass through to ddi_copyxxx() 26665 * directly from the mode argument of ioctl(). 26666 * 26667 * Return Code: 0 - Success 26668 * EACCES 26669 * ENOTSUP 26670 * errno return code from sd_send_scsi_cmd() 26671 * 26672 * Context: Can sleep. Does not return until command is completed. 26673 */ 26674 26675 static int 26676 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26677 mhioc_inkeys_t *usrp, int flag) 26678 { 26679 #ifdef _MULTI_DATAMODEL 26680 struct mhioc_key_list32 li32; 26681 #endif 26682 sd_prin_readkeys_t *in; 26683 mhioc_inkeys_t *ptr; 26684 mhioc_key_list_t li; 26685 uchar_t *data_bufp; 26686 int data_len; 26687 int rval; 26688 size_t copysz; 26689 26690 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26691 return (EINVAL); 26692 } 26693 bzero(&li, sizeof (mhioc_key_list_t)); 26694 26695 /* 26696 * Get the listsize from user 26697 */ 26698 #ifdef _MULTI_DATAMODEL 26699 26700 switch (ddi_model_convert_from(flag & FMODELS)) { 26701 case DDI_MODEL_ILP32: 26702 copysz = sizeof (struct mhioc_key_list32); 26703 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26704 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26705 "sd_persistent_reservation_in_read_keys: " 26706 "failed ddi_copyin: mhioc_key_list32_t\n"); 26707 rval = EFAULT; 26708 goto done; 26709 } 26710 li.listsize = li32.listsize; 26711 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26712 break; 26713 26714 case DDI_MODEL_NONE: 26715 copysz = sizeof (mhioc_key_list_t); 26716 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26717 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26718 "sd_persistent_reservation_in_read_keys: " 26719 "failed ddi_copyin: mhioc_key_list_t\n"); 26720 rval = EFAULT; 26721 goto done; 26722 } 26723 break; 26724 } 26725 26726 #else /* ! _MULTI_DATAMODEL */ 26727 copysz = sizeof (mhioc_key_list_t); 26728 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26729 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26730 "sd_persistent_reservation_in_read_keys: " 26731 "failed ddi_copyin: mhioc_key_list_t\n"); 26732 rval = EFAULT; 26733 goto done; 26734 } 26735 #endif 26736 26737 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26738 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26739 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26740 26741 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26742 data_len, data_bufp)) != 0) { 26743 goto done; 26744 } 26745 in = (sd_prin_readkeys_t *)data_bufp; 26746 ptr->generation = BE_32(in->generation); 26747 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26748 26749 /* 26750 * Return the min(listsize, listlen) keys 26751 */ 26752 #ifdef _MULTI_DATAMODEL 26753 26754 switch (ddi_model_convert_from(flag & FMODELS)) { 26755 case DDI_MODEL_ILP32: 26756 li32.listlen = li.listlen; 26757 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26758 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26759 "sd_persistent_reservation_in_read_keys: " 26760 "failed ddi_copyout: mhioc_key_list32_t\n"); 26761 rval = EFAULT; 26762 goto done; 26763 } 26764 break; 26765 26766 case DDI_MODEL_NONE: 26767 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26768 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26769 "sd_persistent_reservation_in_read_keys: " 26770 "failed ddi_copyout: mhioc_key_list_t\n"); 26771 rval = EFAULT; 26772 goto done; 26773 } 26774 break; 26775 } 26776 26777 #else /* ! _MULTI_DATAMODEL */ 26778 26779 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26780 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26781 "sd_persistent_reservation_in_read_keys: " 26782 "failed ddi_copyout: mhioc_key_list_t\n"); 26783 rval = EFAULT; 26784 goto done; 26785 } 26786 26787 #endif /* _MULTI_DATAMODEL */ 26788 26789 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26790 li.listsize * MHIOC_RESV_KEY_SIZE); 26791 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26792 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26793 "sd_persistent_reservation_in_read_keys: " 26794 "failed ddi_copyout: keylist\n"); 26795 rval = EFAULT; 26796 } 26797 done: 26798 kmem_free(data_bufp, data_len); 26799 return (rval); 26800 } 26801 26802 26803 /* 26804 * Function: sd_persistent_reservation_in_read_resv 26805 * 26806 * Description: This routine is the driver entry point for handling CD-ROM 26807 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26808 * by sending the SCSI-3 PRIN commands to the device. 26809 * Process the read persistent reservations command response by 26810 * copying the reservation information into the user provided 26811 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26812 * 26813 * Arguments: un - Pointer to soft state struct for the target. 26814 * usrp - user provided pointer to multihost Persistent In Read 26815 * Keys structure (mhioc_inkeys_t) 26816 * flag - this argument is a pass through to ddi_copyxxx() 26817 * directly from the mode argument of ioctl(). 26818 * 26819 * Return Code: 0 - Success 26820 * EACCES 26821 * ENOTSUP 26822 * errno return code from sd_send_scsi_cmd() 26823 * 26824 * Context: Can sleep. Does not return until command is completed. 26825 */ 26826 26827 static int 26828 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26829 mhioc_inresvs_t *usrp, int flag) 26830 { 26831 #ifdef _MULTI_DATAMODEL 26832 struct mhioc_resv_desc_list32 resvlist32; 26833 #endif 26834 sd_prin_readresv_t *in; 26835 mhioc_inresvs_t *ptr; 26836 sd_readresv_desc_t *readresv_ptr; 26837 mhioc_resv_desc_list_t resvlist; 26838 mhioc_resv_desc_t resvdesc; 26839 uchar_t *data_bufp; 26840 int data_len; 26841 int rval; 26842 int i; 26843 size_t copysz; 26844 mhioc_resv_desc_t *bufp; 26845 26846 if ((ptr = usrp) == NULL) { 26847 return (EINVAL); 26848 } 26849 26850 /* 26851 * Get the listsize from user 26852 */ 26853 #ifdef _MULTI_DATAMODEL 26854 switch (ddi_model_convert_from(flag & FMODELS)) { 26855 case DDI_MODEL_ILP32: 26856 copysz = sizeof (struct mhioc_resv_desc_list32); 26857 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26858 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26859 "sd_persistent_reservation_in_read_resv: " 26860 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26861 rval = EFAULT; 26862 goto done; 26863 } 26864 resvlist.listsize = resvlist32.listsize; 26865 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26866 break; 26867 26868 case DDI_MODEL_NONE: 26869 copysz = sizeof (mhioc_resv_desc_list_t); 26870 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26871 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26872 "sd_persistent_reservation_in_read_resv: " 26873 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26874 rval = EFAULT; 26875 goto done; 26876 } 26877 break; 26878 } 26879 #else /* ! _MULTI_DATAMODEL */ 26880 copysz = sizeof (mhioc_resv_desc_list_t); 26881 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26882 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26883 "sd_persistent_reservation_in_read_resv: " 26884 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26885 rval = EFAULT; 26886 goto done; 26887 } 26888 #endif /* ! _MULTI_DATAMODEL */ 26889 26890 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26891 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26892 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26893 26894 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 26895 data_len, data_bufp)) != 0) { 26896 goto done; 26897 } 26898 in = (sd_prin_readresv_t *)data_bufp; 26899 ptr->generation = BE_32(in->generation); 26900 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26901 26902 /* 26903 * Return the min(listsize, listlen( keys 26904 */ 26905 #ifdef _MULTI_DATAMODEL 26906 26907 switch (ddi_model_convert_from(flag & FMODELS)) { 26908 case DDI_MODEL_ILP32: 26909 resvlist32.listlen = resvlist.listlen; 26910 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26911 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26912 "sd_persistent_reservation_in_read_resv: " 26913 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26914 rval = EFAULT; 26915 goto done; 26916 } 26917 break; 26918 26919 case DDI_MODEL_NONE: 26920 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26921 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26922 "sd_persistent_reservation_in_read_resv: " 26923 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26924 rval = EFAULT; 26925 goto done; 26926 } 26927 break; 26928 } 26929 26930 #else /* ! _MULTI_DATAMODEL */ 26931 26932 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26933 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26934 "sd_persistent_reservation_in_read_resv: " 26935 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26936 rval = EFAULT; 26937 goto done; 26938 } 26939 26940 #endif /* ! _MULTI_DATAMODEL */ 26941 26942 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26943 bufp = resvlist.list; 26944 copysz = sizeof (mhioc_resv_desc_t); 26945 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26946 i++, readresv_ptr++, bufp++) { 26947 26948 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26949 MHIOC_RESV_KEY_SIZE); 26950 resvdesc.type = readresv_ptr->type; 26951 resvdesc.scope = readresv_ptr->scope; 26952 resvdesc.scope_specific_addr = 26953 BE_32(readresv_ptr->scope_specific_addr); 26954 26955 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26956 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26957 "sd_persistent_reservation_in_read_resv: " 26958 "failed ddi_copyout: resvlist\n"); 26959 rval = EFAULT; 26960 goto done; 26961 } 26962 } 26963 done: 26964 kmem_free(data_bufp, data_len); 26965 return (rval); 26966 } 26967 26968 26969 /* 26970 * Function: sr_change_blkmode() 26971 * 26972 * Description: This routine is the driver entry point for handling CD-ROM 26973 * block mode ioctl requests. Support for returning and changing 26974 * the current block size in use by the device is implemented. The 26975 * LBA size is changed via a MODE SELECT Block Descriptor. 26976 * 26977 * This routine issues a mode sense with an allocation length of 26978 * 12 bytes for the mode page header and a single block descriptor. 26979 * 26980 * Arguments: dev - the device 'dev_t' 26981 * cmd - the request type; one of CDROMGBLKMODE (get) or 26982 * CDROMSBLKMODE (set) 26983 * data - current block size or requested block size 26984 * flag - this argument is a pass through to ddi_copyxxx() directly 26985 * from the mode argument of ioctl(). 26986 * 26987 * Return Code: the code returned by sd_send_scsi_cmd() 26988 * EINVAL if invalid arguments are provided 26989 * EFAULT if ddi_copyxxx() fails 26990 * ENXIO if fail ddi_get_soft_state 26991 * EIO if invalid mode sense block descriptor length 26992 * 26993 */ 26994 26995 static int 26996 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26997 { 26998 struct sd_lun *un = NULL; 26999 struct mode_header *sense_mhp, *select_mhp; 27000 struct block_descriptor *sense_desc, *select_desc; 27001 int current_bsize; 27002 int rval = EINVAL; 27003 uchar_t *sense = NULL; 27004 uchar_t *select = NULL; 27005 27006 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27007 27008 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27009 return (ENXIO); 27010 } 27011 27012 /* 27013 * The block length is changed via the Mode Select block descriptor, the 27014 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27015 * required as part of this routine. Therefore the mode sense allocation 27016 * length is specified to be the length of a mode page header and a 27017 * block descriptor. 27018 */ 27019 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27020 27021 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27022 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27023 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27024 "sr_change_blkmode: Mode Sense Failed\n"); 27025 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27026 return (rval); 27027 } 27028 27029 /* Check the block descriptor len to handle only 1 block descriptor */ 27030 sense_mhp = (struct mode_header *)sense; 27031 if ((sense_mhp->bdesc_length == 0) || 27032 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27033 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27034 "sr_change_blkmode: Mode Sense returned invalid block" 27035 " descriptor length\n"); 27036 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27037 return (EIO); 27038 } 27039 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27040 current_bsize = ((sense_desc->blksize_hi << 16) | 27041 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27042 27043 /* Process command */ 27044 switch (cmd) { 27045 case CDROMGBLKMODE: 27046 /* Return the block size obtained during the mode sense */ 27047 if (ddi_copyout(¤t_bsize, (void *)data, 27048 sizeof (int), flag) != 0) 27049 rval = EFAULT; 27050 break; 27051 case CDROMSBLKMODE: 27052 /* Validate the requested block size */ 27053 switch (data) { 27054 case CDROM_BLK_512: 27055 case CDROM_BLK_1024: 27056 case CDROM_BLK_2048: 27057 case CDROM_BLK_2056: 27058 case CDROM_BLK_2336: 27059 case CDROM_BLK_2340: 27060 case CDROM_BLK_2352: 27061 case CDROM_BLK_2368: 27062 case CDROM_BLK_2448: 27063 case CDROM_BLK_2646: 27064 case CDROM_BLK_2647: 27065 break; 27066 default: 27067 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27068 "sr_change_blkmode: " 27069 "Block Size '%ld' Not Supported\n", data); 27070 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27071 return (EINVAL); 27072 } 27073 27074 /* 27075 * The current block size matches the requested block size so 27076 * there is no need to send the mode select to change the size 27077 */ 27078 if (current_bsize == data) { 27079 break; 27080 } 27081 27082 /* Build the select data for the requested block size */ 27083 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27084 select_mhp = (struct mode_header *)select; 27085 select_desc = 27086 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27087 /* 27088 * The LBA size is changed via the block descriptor, so the 27089 * descriptor is built according to the user data 27090 */ 27091 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27092 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27093 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27094 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27095 27096 /* Send the mode select for the requested block size */ 27097 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27098 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27099 SD_PATH_STANDARD)) != 0) { 27100 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27101 "sr_change_blkmode: Mode Select Failed\n"); 27102 /* 27103 * The mode select failed for the requested block size, 27104 * so reset the data for the original block size and 27105 * send it to the target. The error is indicated by the 27106 * return value for the failed mode select. 27107 */ 27108 select_desc->blksize_hi = sense_desc->blksize_hi; 27109 select_desc->blksize_mid = sense_desc->blksize_mid; 27110 select_desc->blksize_lo = sense_desc->blksize_lo; 27111 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27112 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27113 SD_PATH_STANDARD); 27114 } else { 27115 ASSERT(!mutex_owned(SD_MUTEX(un))); 27116 mutex_enter(SD_MUTEX(un)); 27117 sd_update_block_info(un, (uint32_t)data, 0); 27118 27119 mutex_exit(SD_MUTEX(un)); 27120 } 27121 break; 27122 default: 27123 /* should not reach here, but check anyway */ 27124 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27125 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27126 rval = EINVAL; 27127 break; 27128 } 27129 27130 if (select) { 27131 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27132 } 27133 if (sense) { 27134 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27135 } 27136 return (rval); 27137 } 27138 27139 27140 /* 27141 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27142 * implement driver support for getting and setting the CD speed. The command 27143 * set used will be based on the device type. If the device has not been 27144 * identified as MMC the Toshiba vendor specific mode page will be used. If 27145 * the device is MMC but does not support the Real Time Streaming feature 27146 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27147 * be used to read the speed. 27148 */ 27149 27150 /* 27151 * Function: sr_change_speed() 27152 * 27153 * Description: This routine is the driver entry point for handling CD-ROM 27154 * drive speed ioctl requests for devices supporting the Toshiba 27155 * vendor specific drive speed mode page. Support for returning 27156 * and changing the current drive speed in use by the device is 27157 * implemented. 27158 * 27159 * Arguments: dev - the device 'dev_t' 27160 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27161 * CDROMSDRVSPEED (set) 27162 * data - current drive speed or requested drive speed 27163 * flag - this argument is a pass through to ddi_copyxxx() directly 27164 * from the mode argument of ioctl(). 27165 * 27166 * Return Code: the code returned by sd_send_scsi_cmd() 27167 * EINVAL if invalid arguments are provided 27168 * EFAULT if ddi_copyxxx() fails 27169 * ENXIO if fail ddi_get_soft_state 27170 * EIO if invalid mode sense block descriptor length 27171 */ 27172 27173 static int 27174 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27175 { 27176 struct sd_lun *un = NULL; 27177 struct mode_header *sense_mhp, *select_mhp; 27178 struct mode_speed *sense_page, *select_page; 27179 int current_speed; 27180 int rval = EINVAL; 27181 int bd_len; 27182 uchar_t *sense = NULL; 27183 uchar_t *select = NULL; 27184 27185 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27186 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27187 return (ENXIO); 27188 } 27189 27190 /* 27191 * Note: The drive speed is being modified here according to a Toshiba 27192 * vendor specific mode page (0x31). 27193 */ 27194 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27195 27196 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27197 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27198 SD_PATH_STANDARD)) != 0) { 27199 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27200 "sr_change_speed: Mode Sense Failed\n"); 27201 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27202 return (rval); 27203 } 27204 sense_mhp = (struct mode_header *)sense; 27205 27206 /* Check the block descriptor len to handle only 1 block descriptor */ 27207 bd_len = sense_mhp->bdesc_length; 27208 if (bd_len > MODE_BLK_DESC_LENGTH) { 27209 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27210 "sr_change_speed: Mode Sense returned invalid block " 27211 "descriptor length\n"); 27212 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27213 return (EIO); 27214 } 27215 27216 sense_page = (struct mode_speed *) 27217 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27218 current_speed = sense_page->speed; 27219 27220 /* Process command */ 27221 switch (cmd) { 27222 case CDROMGDRVSPEED: 27223 /* Return the drive speed obtained during the mode sense */ 27224 if (current_speed == 0x2) { 27225 current_speed = CDROM_TWELVE_SPEED; 27226 } 27227 if (ddi_copyout(¤t_speed, (void *)data, 27228 sizeof (int), flag) != 0) { 27229 rval = EFAULT; 27230 } 27231 break; 27232 case CDROMSDRVSPEED: 27233 /* Validate the requested drive speed */ 27234 switch ((uchar_t)data) { 27235 case CDROM_TWELVE_SPEED: 27236 data = 0x2; 27237 /*FALLTHROUGH*/ 27238 case CDROM_NORMAL_SPEED: 27239 case CDROM_DOUBLE_SPEED: 27240 case CDROM_QUAD_SPEED: 27241 case CDROM_MAXIMUM_SPEED: 27242 break; 27243 default: 27244 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27245 "sr_change_speed: " 27246 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27247 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27248 return (EINVAL); 27249 } 27250 27251 /* 27252 * The current drive speed matches the requested drive speed so 27253 * there is no need to send the mode select to change the speed 27254 */ 27255 if (current_speed == data) { 27256 break; 27257 } 27258 27259 /* Build the select data for the requested drive speed */ 27260 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27261 select_mhp = (struct mode_header *)select; 27262 select_mhp->bdesc_length = 0; 27263 select_page = 27264 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27265 select_page = 27266 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27267 select_page->mode_page.code = CDROM_MODE_SPEED; 27268 select_page->mode_page.length = 2; 27269 select_page->speed = (uchar_t)data; 27270 27271 /* Send the mode select for the requested block size */ 27272 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27273 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27274 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27275 /* 27276 * The mode select failed for the requested drive speed, 27277 * so reset the data for the original drive speed and 27278 * send it to the target. The error is indicated by the 27279 * return value for the failed mode select. 27280 */ 27281 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27282 "sr_drive_speed: Mode Select Failed\n"); 27283 select_page->speed = sense_page->speed; 27284 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27285 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27286 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27287 } 27288 break; 27289 default: 27290 /* should not reach here, but check anyway */ 27291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27292 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27293 rval = EINVAL; 27294 break; 27295 } 27296 27297 if (select) { 27298 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27299 } 27300 if (sense) { 27301 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27302 } 27303 27304 return (rval); 27305 } 27306 27307 27308 /* 27309 * Function: sr_atapi_change_speed() 27310 * 27311 * Description: This routine is the driver entry point for handling CD-ROM 27312 * drive speed ioctl requests for MMC devices that do not support 27313 * the Real Time Streaming feature (0x107). 27314 * 27315 * Note: This routine will use the SET SPEED command which may not 27316 * be supported by all devices. 27317 * 27318 * Arguments: dev- the device 'dev_t' 27319 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27320 * CDROMSDRVSPEED (set) 27321 * data- current drive speed or requested drive speed 27322 * flag- this argument is a pass through to ddi_copyxxx() directly 27323 * from the mode argument of ioctl(). 27324 * 27325 * Return Code: the code returned by sd_send_scsi_cmd() 27326 * EINVAL if invalid arguments are provided 27327 * EFAULT if ddi_copyxxx() fails 27328 * ENXIO if fail ddi_get_soft_state 27329 * EIO if invalid mode sense block descriptor length 27330 */ 27331 27332 static int 27333 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27334 { 27335 struct sd_lun *un; 27336 struct uscsi_cmd *com = NULL; 27337 struct mode_header_grp2 *sense_mhp; 27338 uchar_t *sense_page; 27339 uchar_t *sense = NULL; 27340 char cdb[CDB_GROUP5]; 27341 int bd_len; 27342 int current_speed = 0; 27343 int max_speed = 0; 27344 int rval; 27345 27346 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27347 27348 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27349 return (ENXIO); 27350 } 27351 27352 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27353 27354 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27355 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27356 SD_PATH_STANDARD)) != 0) { 27357 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27358 "sr_atapi_change_speed: Mode Sense Failed\n"); 27359 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27360 return (rval); 27361 } 27362 27363 /* Check the block descriptor len to handle only 1 block descriptor */ 27364 sense_mhp = (struct mode_header_grp2 *)sense; 27365 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27366 if (bd_len > MODE_BLK_DESC_LENGTH) { 27367 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27368 "sr_atapi_change_speed: Mode Sense returned invalid " 27369 "block descriptor length\n"); 27370 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27371 return (EIO); 27372 } 27373 27374 /* Calculate the current and maximum drive speeds */ 27375 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27376 current_speed = (sense_page[14] << 8) | sense_page[15]; 27377 max_speed = (sense_page[8] << 8) | sense_page[9]; 27378 27379 /* Process the command */ 27380 switch (cmd) { 27381 case CDROMGDRVSPEED: 27382 current_speed /= SD_SPEED_1X; 27383 if (ddi_copyout(¤t_speed, (void *)data, 27384 sizeof (int), flag) != 0) 27385 rval = EFAULT; 27386 break; 27387 case CDROMSDRVSPEED: 27388 /* Convert the speed code to KB/sec */ 27389 switch ((uchar_t)data) { 27390 case CDROM_NORMAL_SPEED: 27391 current_speed = SD_SPEED_1X; 27392 break; 27393 case CDROM_DOUBLE_SPEED: 27394 current_speed = 2 * SD_SPEED_1X; 27395 break; 27396 case CDROM_QUAD_SPEED: 27397 current_speed = 4 * SD_SPEED_1X; 27398 break; 27399 case CDROM_TWELVE_SPEED: 27400 current_speed = 12 * SD_SPEED_1X; 27401 break; 27402 case CDROM_MAXIMUM_SPEED: 27403 current_speed = 0xffff; 27404 break; 27405 default: 27406 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27407 "sr_atapi_change_speed: invalid drive speed %d\n", 27408 (uchar_t)data); 27409 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27410 return (EINVAL); 27411 } 27412 27413 /* Check the request against the drive's max speed. */ 27414 if (current_speed != 0xffff) { 27415 if (current_speed > max_speed) { 27416 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27417 return (EINVAL); 27418 } 27419 } 27420 27421 /* 27422 * Build and send the SET SPEED command 27423 * 27424 * Note: The SET SPEED (0xBB) command used in this routine is 27425 * obsolete per the SCSI MMC spec but still supported in the 27426 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27427 * therefore the command is still implemented in this routine. 27428 */ 27429 bzero(cdb, sizeof (cdb)); 27430 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27431 cdb[2] = (uchar_t)(current_speed >> 8); 27432 cdb[3] = (uchar_t)current_speed; 27433 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27434 com->uscsi_cdb = (caddr_t)cdb; 27435 com->uscsi_cdblen = CDB_GROUP5; 27436 com->uscsi_bufaddr = NULL; 27437 com->uscsi_buflen = 0; 27438 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27439 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 27440 UIO_SYSSPACE, SD_PATH_STANDARD); 27441 break; 27442 default: 27443 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27444 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27445 rval = EINVAL; 27446 } 27447 27448 if (sense) { 27449 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27450 } 27451 if (com) { 27452 kmem_free(com, sizeof (*com)); 27453 } 27454 return (rval); 27455 } 27456 27457 27458 /* 27459 * Function: sr_pause_resume() 27460 * 27461 * Description: This routine is the driver entry point for handling CD-ROM 27462 * pause/resume ioctl requests. This only affects the audio play 27463 * operation. 27464 * 27465 * Arguments: dev - the device 'dev_t' 27466 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27467 * for setting the resume bit of the cdb. 27468 * 27469 * Return Code: the code returned by sd_send_scsi_cmd() 27470 * EINVAL if invalid mode specified 27471 * 27472 */ 27473 27474 static int 27475 sr_pause_resume(dev_t dev, int cmd) 27476 { 27477 struct sd_lun *un; 27478 struct uscsi_cmd *com; 27479 char cdb[CDB_GROUP1]; 27480 int rval; 27481 27482 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27483 return (ENXIO); 27484 } 27485 27486 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27487 bzero(cdb, CDB_GROUP1); 27488 cdb[0] = SCMD_PAUSE_RESUME; 27489 switch (cmd) { 27490 case CDROMRESUME: 27491 cdb[8] = 1; 27492 break; 27493 case CDROMPAUSE: 27494 cdb[8] = 0; 27495 break; 27496 default: 27497 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27498 " Command '%x' Not Supported\n", cmd); 27499 rval = EINVAL; 27500 goto done; 27501 } 27502 27503 com->uscsi_cdb = cdb; 27504 com->uscsi_cdblen = CDB_GROUP1; 27505 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27506 27507 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27508 UIO_SYSSPACE, SD_PATH_STANDARD); 27509 27510 done: 27511 kmem_free(com, sizeof (*com)); 27512 return (rval); 27513 } 27514 27515 27516 /* 27517 * Function: sr_play_msf() 27518 * 27519 * Description: This routine is the driver entry point for handling CD-ROM 27520 * ioctl requests to output the audio signals at the specified 27521 * starting address and continue the audio play until the specified 27522 * ending address (CDROMPLAYMSF) The address is in Minute Second 27523 * Frame (MSF) format. 27524 * 27525 * Arguments: dev - the device 'dev_t' 27526 * data - pointer to user provided audio msf structure, 27527 * specifying start/end addresses. 27528 * flag - this argument is a pass through to ddi_copyxxx() 27529 * directly from the mode argument of ioctl(). 27530 * 27531 * Return Code: the code returned by sd_send_scsi_cmd() 27532 * EFAULT if ddi_copyxxx() fails 27533 * ENXIO if fail ddi_get_soft_state 27534 * EINVAL if data pointer is NULL 27535 */ 27536 27537 static int 27538 sr_play_msf(dev_t dev, caddr_t data, int flag) 27539 { 27540 struct sd_lun *un; 27541 struct uscsi_cmd *com; 27542 struct cdrom_msf msf_struct; 27543 struct cdrom_msf *msf = &msf_struct; 27544 char cdb[CDB_GROUP1]; 27545 int rval; 27546 27547 if (data == NULL) { 27548 return (EINVAL); 27549 } 27550 27551 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27552 return (ENXIO); 27553 } 27554 27555 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27556 return (EFAULT); 27557 } 27558 27559 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27560 bzero(cdb, CDB_GROUP1); 27561 cdb[0] = SCMD_PLAYAUDIO_MSF; 27562 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27563 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27564 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27565 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27566 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27567 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27568 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27569 } else { 27570 cdb[3] = msf->cdmsf_min0; 27571 cdb[4] = msf->cdmsf_sec0; 27572 cdb[5] = msf->cdmsf_frame0; 27573 cdb[6] = msf->cdmsf_min1; 27574 cdb[7] = msf->cdmsf_sec1; 27575 cdb[8] = msf->cdmsf_frame1; 27576 } 27577 com->uscsi_cdb = cdb; 27578 com->uscsi_cdblen = CDB_GROUP1; 27579 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27580 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27581 UIO_SYSSPACE, SD_PATH_STANDARD); 27582 kmem_free(com, sizeof (*com)); 27583 return (rval); 27584 } 27585 27586 27587 /* 27588 * Function: sr_play_trkind() 27589 * 27590 * Description: This routine is the driver entry point for handling CD-ROM 27591 * ioctl requests to output the audio signals at the specified 27592 * starting address and continue the audio play until the specified 27593 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27594 * format. 27595 * 27596 * Arguments: dev - the device 'dev_t' 27597 * data - pointer to user provided audio track/index structure, 27598 * specifying start/end addresses. 27599 * flag - this argument is a pass through to ddi_copyxxx() 27600 * directly from the mode argument of ioctl(). 27601 * 27602 * Return Code: the code returned by sd_send_scsi_cmd() 27603 * EFAULT if ddi_copyxxx() fails 27604 * ENXIO if fail ddi_get_soft_state 27605 * EINVAL if data pointer is NULL 27606 */ 27607 27608 static int 27609 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27610 { 27611 struct cdrom_ti ti_struct; 27612 struct cdrom_ti *ti = &ti_struct; 27613 struct uscsi_cmd *com = NULL; 27614 char cdb[CDB_GROUP1]; 27615 int rval; 27616 27617 if (data == NULL) { 27618 return (EINVAL); 27619 } 27620 27621 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27622 return (EFAULT); 27623 } 27624 27625 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27626 bzero(cdb, CDB_GROUP1); 27627 cdb[0] = SCMD_PLAYAUDIO_TI; 27628 cdb[4] = ti->cdti_trk0; 27629 cdb[5] = ti->cdti_ind0; 27630 cdb[7] = ti->cdti_trk1; 27631 cdb[8] = ti->cdti_ind1; 27632 com->uscsi_cdb = cdb; 27633 com->uscsi_cdblen = CDB_GROUP1; 27634 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27635 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27636 UIO_SYSSPACE, SD_PATH_STANDARD); 27637 kmem_free(com, sizeof (*com)); 27638 return (rval); 27639 } 27640 27641 27642 /* 27643 * Function: sr_read_all_subcodes() 27644 * 27645 * Description: This routine is the driver entry point for handling CD-ROM 27646 * ioctl requests to return raw subcode data while the target is 27647 * playing audio (CDROMSUBCODE). 27648 * 27649 * Arguments: dev - the device 'dev_t' 27650 * data - pointer to user provided cdrom subcode structure, 27651 * specifying the transfer length and address. 27652 * flag - this argument is a pass through to ddi_copyxxx() 27653 * directly from the mode argument of ioctl(). 27654 * 27655 * Return Code: the code returned by sd_send_scsi_cmd() 27656 * EFAULT if ddi_copyxxx() fails 27657 * ENXIO if fail ddi_get_soft_state 27658 * EINVAL if data pointer is NULL 27659 */ 27660 27661 static int 27662 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27663 { 27664 struct sd_lun *un = NULL; 27665 struct uscsi_cmd *com = NULL; 27666 struct cdrom_subcode *subcode = NULL; 27667 int rval; 27668 size_t buflen; 27669 char cdb[CDB_GROUP5]; 27670 27671 #ifdef _MULTI_DATAMODEL 27672 /* To support ILP32 applications in an LP64 world */ 27673 struct cdrom_subcode32 cdrom_subcode32; 27674 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27675 #endif 27676 if (data == NULL) { 27677 return (EINVAL); 27678 } 27679 27680 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27681 return (ENXIO); 27682 } 27683 27684 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27685 27686 #ifdef _MULTI_DATAMODEL 27687 switch (ddi_model_convert_from(flag & FMODELS)) { 27688 case DDI_MODEL_ILP32: 27689 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27690 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27691 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27692 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27693 return (EFAULT); 27694 } 27695 /* Convert the ILP32 uscsi data from the application to LP64 */ 27696 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27697 break; 27698 case DDI_MODEL_NONE: 27699 if (ddi_copyin(data, subcode, 27700 sizeof (struct cdrom_subcode), flag)) { 27701 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27702 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27703 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27704 return (EFAULT); 27705 } 27706 break; 27707 } 27708 #else /* ! _MULTI_DATAMODEL */ 27709 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27710 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27711 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27712 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27713 return (EFAULT); 27714 } 27715 #endif /* _MULTI_DATAMODEL */ 27716 27717 /* 27718 * Since MMC-2 expects max 3 bytes for length, check if the 27719 * length input is greater than 3 bytes 27720 */ 27721 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27722 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27723 "sr_read_all_subcodes: " 27724 "cdrom transfer length too large: %d (limit %d)\n", 27725 subcode->cdsc_length, 0xFFFFFF); 27726 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27727 return (EINVAL); 27728 } 27729 27730 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27731 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27732 bzero(cdb, CDB_GROUP5); 27733 27734 if (un->un_f_mmc_cap == TRUE) { 27735 cdb[0] = (char)SCMD_READ_CD; 27736 cdb[2] = (char)0xff; 27737 cdb[3] = (char)0xff; 27738 cdb[4] = (char)0xff; 27739 cdb[5] = (char)0xff; 27740 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27741 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27742 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27743 cdb[10] = 1; 27744 } else { 27745 /* 27746 * Note: A vendor specific command (0xDF) is being used her to 27747 * request a read of all subcodes. 27748 */ 27749 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27750 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27751 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27752 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27753 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27754 } 27755 com->uscsi_cdb = cdb; 27756 com->uscsi_cdblen = CDB_GROUP5; 27757 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27758 com->uscsi_buflen = buflen; 27759 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27760 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27761 UIO_SYSSPACE, SD_PATH_STANDARD); 27762 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27763 kmem_free(com, sizeof (*com)); 27764 return (rval); 27765 } 27766 27767 27768 /* 27769 * Function: sr_read_subchannel() 27770 * 27771 * Description: This routine is the driver entry point for handling CD-ROM 27772 * ioctl requests to return the Q sub-channel data of the CD 27773 * current position block. (CDROMSUBCHNL) The data includes the 27774 * track number, index number, absolute CD-ROM address (LBA or MSF 27775 * format per the user) , track relative CD-ROM address (LBA or MSF 27776 * format per the user), control data and audio status. 27777 * 27778 * Arguments: dev - the device 'dev_t' 27779 * data - pointer to user provided cdrom sub-channel structure 27780 * flag - this argument is a pass through to ddi_copyxxx() 27781 * directly from the mode argument of ioctl(). 27782 * 27783 * Return Code: the code returned by sd_send_scsi_cmd() 27784 * EFAULT if ddi_copyxxx() fails 27785 * ENXIO if fail ddi_get_soft_state 27786 * EINVAL if data pointer is NULL 27787 */ 27788 27789 static int 27790 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27791 { 27792 struct sd_lun *un; 27793 struct uscsi_cmd *com; 27794 struct cdrom_subchnl subchanel; 27795 struct cdrom_subchnl *subchnl = &subchanel; 27796 char cdb[CDB_GROUP1]; 27797 caddr_t buffer; 27798 int rval; 27799 27800 if (data == NULL) { 27801 return (EINVAL); 27802 } 27803 27804 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27805 (un->un_state == SD_STATE_OFFLINE)) { 27806 return (ENXIO); 27807 } 27808 27809 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27810 return (EFAULT); 27811 } 27812 27813 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27814 bzero(cdb, CDB_GROUP1); 27815 cdb[0] = SCMD_READ_SUBCHANNEL; 27816 /* Set the MSF bit based on the user requested address format */ 27817 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27818 /* 27819 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27820 * returned 27821 */ 27822 cdb[2] = 0x40; 27823 /* 27824 * Set byte 3 to specify the return data format. A value of 0x01 27825 * indicates that the CD-ROM current position should be returned. 27826 */ 27827 cdb[3] = 0x01; 27828 cdb[8] = 0x10; 27829 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27830 com->uscsi_cdb = cdb; 27831 com->uscsi_cdblen = CDB_GROUP1; 27832 com->uscsi_bufaddr = buffer; 27833 com->uscsi_buflen = 16; 27834 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27835 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27836 UIO_SYSSPACE, SD_PATH_STANDARD); 27837 if (rval != 0) { 27838 kmem_free(buffer, 16); 27839 kmem_free(com, sizeof (*com)); 27840 return (rval); 27841 } 27842 27843 /* Process the returned Q sub-channel data */ 27844 subchnl->cdsc_audiostatus = buffer[1]; 27845 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27846 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27847 subchnl->cdsc_trk = buffer[6]; 27848 subchnl->cdsc_ind = buffer[7]; 27849 if (subchnl->cdsc_format & CDROM_LBA) { 27850 subchnl->cdsc_absaddr.lba = 27851 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27852 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27853 subchnl->cdsc_reladdr.lba = 27854 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27855 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27856 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27857 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27858 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27859 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27860 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27861 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27862 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27863 } else { 27864 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27865 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27866 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27867 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27868 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27869 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27870 } 27871 kmem_free(buffer, 16); 27872 kmem_free(com, sizeof (*com)); 27873 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27874 != 0) { 27875 return (EFAULT); 27876 } 27877 return (rval); 27878 } 27879 27880 27881 /* 27882 * Function: sr_read_tocentry() 27883 * 27884 * Description: This routine is the driver entry point for handling CD-ROM 27885 * ioctl requests to read from the Table of Contents (TOC) 27886 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27887 * fields, the starting address (LBA or MSF format per the user) 27888 * and the data mode if the user specified track is a data track. 27889 * 27890 * Note: The READ HEADER (0x44) command used in this routine is 27891 * obsolete per the SCSI MMC spec but still supported in the 27892 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27893 * therefore the command is still implemented in this routine. 27894 * 27895 * Arguments: dev - the device 'dev_t' 27896 * data - pointer to user provided toc entry structure, 27897 * specifying the track # and the address format 27898 * (LBA or MSF). 27899 * flag - this argument is a pass through to ddi_copyxxx() 27900 * directly from the mode argument of ioctl(). 27901 * 27902 * Return Code: the code returned by sd_send_scsi_cmd() 27903 * EFAULT if ddi_copyxxx() fails 27904 * ENXIO if fail ddi_get_soft_state 27905 * EINVAL if data pointer is NULL 27906 */ 27907 27908 static int 27909 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27910 { 27911 struct sd_lun *un = NULL; 27912 struct uscsi_cmd *com; 27913 struct cdrom_tocentry toc_entry; 27914 struct cdrom_tocentry *entry = &toc_entry; 27915 caddr_t buffer; 27916 int rval; 27917 char cdb[CDB_GROUP1]; 27918 27919 if (data == NULL) { 27920 return (EINVAL); 27921 } 27922 27923 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27924 (un->un_state == SD_STATE_OFFLINE)) { 27925 return (ENXIO); 27926 } 27927 27928 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27929 return (EFAULT); 27930 } 27931 27932 /* Validate the requested track and address format */ 27933 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27934 return (EINVAL); 27935 } 27936 27937 if (entry->cdte_track == 0) { 27938 return (EINVAL); 27939 } 27940 27941 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27942 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27943 bzero(cdb, CDB_GROUP1); 27944 27945 cdb[0] = SCMD_READ_TOC; 27946 /* Set the MSF bit based on the user requested address format */ 27947 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27948 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27949 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27950 } else { 27951 cdb[6] = entry->cdte_track; 27952 } 27953 27954 /* 27955 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27956 * (4 byte TOC response header + 8 byte track descriptor) 27957 */ 27958 cdb[8] = 12; 27959 com->uscsi_cdb = cdb; 27960 com->uscsi_cdblen = CDB_GROUP1; 27961 com->uscsi_bufaddr = buffer; 27962 com->uscsi_buflen = 0x0C; 27963 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27964 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27965 UIO_SYSSPACE, SD_PATH_STANDARD); 27966 if (rval != 0) { 27967 kmem_free(buffer, 12); 27968 kmem_free(com, sizeof (*com)); 27969 return (rval); 27970 } 27971 27972 /* Process the toc entry */ 27973 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27974 entry->cdte_ctrl = (buffer[5] & 0x0F); 27975 if (entry->cdte_format & CDROM_LBA) { 27976 entry->cdte_addr.lba = 27977 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27978 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27979 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27980 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27981 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27982 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27983 /* 27984 * Send a READ TOC command using the LBA address format to get 27985 * the LBA for the track requested so it can be used in the 27986 * READ HEADER request 27987 * 27988 * Note: The MSF bit of the READ HEADER command specifies the 27989 * output format. The block address specified in that command 27990 * must be in LBA format. 27991 */ 27992 cdb[1] = 0; 27993 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27994 UIO_SYSSPACE, SD_PATH_STANDARD); 27995 if (rval != 0) { 27996 kmem_free(buffer, 12); 27997 kmem_free(com, sizeof (*com)); 27998 return (rval); 27999 } 28000 } else { 28001 entry->cdte_addr.msf.minute = buffer[9]; 28002 entry->cdte_addr.msf.second = buffer[10]; 28003 entry->cdte_addr.msf.frame = buffer[11]; 28004 /* 28005 * Send a READ TOC command using the LBA address format to get 28006 * the LBA for the track requested so it can be used in the 28007 * READ HEADER request 28008 * 28009 * Note: The MSF bit of the READ HEADER command specifies the 28010 * output format. The block address specified in that command 28011 * must be in LBA format. 28012 */ 28013 cdb[1] = 0; 28014 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28015 UIO_SYSSPACE, SD_PATH_STANDARD); 28016 if (rval != 0) { 28017 kmem_free(buffer, 12); 28018 kmem_free(com, sizeof (*com)); 28019 return (rval); 28020 } 28021 } 28022 28023 /* 28024 * Build and send the READ HEADER command to determine the data mode of 28025 * the user specified track. 28026 */ 28027 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28028 (entry->cdte_track != CDROM_LEADOUT)) { 28029 bzero(cdb, CDB_GROUP1); 28030 cdb[0] = SCMD_READ_HEADER; 28031 cdb[2] = buffer[8]; 28032 cdb[3] = buffer[9]; 28033 cdb[4] = buffer[10]; 28034 cdb[5] = buffer[11]; 28035 cdb[8] = 0x08; 28036 com->uscsi_buflen = 0x08; 28037 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28038 UIO_SYSSPACE, SD_PATH_STANDARD); 28039 if (rval == 0) { 28040 entry->cdte_datamode = buffer[0]; 28041 } else { 28042 /* 28043 * READ HEADER command failed, since this is 28044 * obsoleted in one spec, its better to return 28045 * -1 for an invlid track so that we can still 28046 * recieve the rest of the TOC data. 28047 */ 28048 entry->cdte_datamode = (uchar_t)-1; 28049 } 28050 } else { 28051 entry->cdte_datamode = (uchar_t)-1; 28052 } 28053 28054 kmem_free(buffer, 12); 28055 kmem_free(com, sizeof (*com)); 28056 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28057 return (EFAULT); 28058 28059 return (rval); 28060 } 28061 28062 28063 /* 28064 * Function: sr_read_tochdr() 28065 * 28066 * Description: This routine is the driver entry point for handling CD-ROM 28067 * ioctl requests to read the Table of Contents (TOC) header 28068 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28069 * and ending track numbers 28070 * 28071 * Arguments: dev - the device 'dev_t' 28072 * data - pointer to user provided toc header structure, 28073 * specifying the starting and ending track numbers. 28074 * flag - this argument is a pass through to ddi_copyxxx() 28075 * directly from the mode argument of ioctl(). 28076 * 28077 * Return Code: the code returned by sd_send_scsi_cmd() 28078 * EFAULT if ddi_copyxxx() fails 28079 * ENXIO if fail ddi_get_soft_state 28080 * EINVAL if data pointer is NULL 28081 */ 28082 28083 static int 28084 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28085 { 28086 struct sd_lun *un; 28087 struct uscsi_cmd *com; 28088 struct cdrom_tochdr toc_header; 28089 struct cdrom_tochdr *hdr = &toc_header; 28090 char cdb[CDB_GROUP1]; 28091 int rval; 28092 caddr_t buffer; 28093 28094 if (data == NULL) { 28095 return (EINVAL); 28096 } 28097 28098 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28099 (un->un_state == SD_STATE_OFFLINE)) { 28100 return (ENXIO); 28101 } 28102 28103 buffer = kmem_zalloc(4, KM_SLEEP); 28104 bzero(cdb, CDB_GROUP1); 28105 cdb[0] = SCMD_READ_TOC; 28106 /* 28107 * Specifying a track number of 0x00 in the READ TOC command indicates 28108 * that the TOC header should be returned 28109 */ 28110 cdb[6] = 0x00; 28111 /* 28112 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28113 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28114 */ 28115 cdb[8] = 0x04; 28116 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28117 com->uscsi_cdb = cdb; 28118 com->uscsi_cdblen = CDB_GROUP1; 28119 com->uscsi_bufaddr = buffer; 28120 com->uscsi_buflen = 0x04; 28121 com->uscsi_timeout = 300; 28122 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28123 28124 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28125 UIO_SYSSPACE, SD_PATH_STANDARD); 28126 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28127 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28128 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28129 } else { 28130 hdr->cdth_trk0 = buffer[2]; 28131 hdr->cdth_trk1 = buffer[3]; 28132 } 28133 kmem_free(buffer, 4); 28134 kmem_free(com, sizeof (*com)); 28135 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28136 return (EFAULT); 28137 } 28138 return (rval); 28139 } 28140 28141 28142 /* 28143 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28144 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28145 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28146 * digital audio and extended architecture digital audio. These modes are 28147 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28148 * MMC specs. 28149 * 28150 * In addition to support for the various data formats these routines also 28151 * include support for devices that implement only the direct access READ 28152 * commands (0x08, 0x28), devices that implement the READ_CD commands 28153 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28154 * READ CDXA commands (0xD8, 0xDB) 28155 */ 28156 28157 /* 28158 * Function: sr_read_mode1() 28159 * 28160 * Description: This routine is the driver entry point for handling CD-ROM 28161 * ioctl read mode1 requests (CDROMREADMODE1). 28162 * 28163 * Arguments: dev - the device 'dev_t' 28164 * data - pointer to user provided cd read structure specifying 28165 * the lba buffer address and length. 28166 * flag - this argument is a pass through to ddi_copyxxx() 28167 * directly from the mode argument of ioctl(). 28168 * 28169 * Return Code: the code returned by sd_send_scsi_cmd() 28170 * EFAULT if ddi_copyxxx() fails 28171 * ENXIO if fail ddi_get_soft_state 28172 * EINVAL if data pointer is NULL 28173 */ 28174 28175 static int 28176 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28177 { 28178 struct sd_lun *un; 28179 struct cdrom_read mode1_struct; 28180 struct cdrom_read *mode1 = &mode1_struct; 28181 int rval; 28182 #ifdef _MULTI_DATAMODEL 28183 /* To support ILP32 applications in an LP64 world */ 28184 struct cdrom_read32 cdrom_read32; 28185 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28186 #endif /* _MULTI_DATAMODEL */ 28187 28188 if (data == NULL) { 28189 return (EINVAL); 28190 } 28191 28192 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28193 (un->un_state == SD_STATE_OFFLINE)) { 28194 return (ENXIO); 28195 } 28196 28197 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28198 "sd_read_mode1: entry: un:0x%p\n", un); 28199 28200 #ifdef _MULTI_DATAMODEL 28201 switch (ddi_model_convert_from(flag & FMODELS)) { 28202 case DDI_MODEL_ILP32: 28203 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28204 return (EFAULT); 28205 } 28206 /* Convert the ILP32 uscsi data from the application to LP64 */ 28207 cdrom_read32tocdrom_read(cdrd32, mode1); 28208 break; 28209 case DDI_MODEL_NONE: 28210 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28211 return (EFAULT); 28212 } 28213 } 28214 #else /* ! _MULTI_DATAMODEL */ 28215 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28216 return (EFAULT); 28217 } 28218 #endif /* _MULTI_DATAMODEL */ 28219 28220 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28221 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28222 28223 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28224 "sd_read_mode1: exit: un:0x%p\n", un); 28225 28226 return (rval); 28227 } 28228 28229 28230 /* 28231 * Function: sr_read_cd_mode2() 28232 * 28233 * Description: This routine is the driver entry point for handling CD-ROM 28234 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28235 * support the READ CD (0xBE) command or the 1st generation 28236 * READ CD (0xD4) command. 28237 * 28238 * Arguments: dev - the device 'dev_t' 28239 * data - pointer to user provided cd read structure specifying 28240 * the lba buffer address and length. 28241 * flag - this argument is a pass through to ddi_copyxxx() 28242 * directly from the mode argument of ioctl(). 28243 * 28244 * Return Code: the code returned by sd_send_scsi_cmd() 28245 * EFAULT if ddi_copyxxx() fails 28246 * ENXIO if fail ddi_get_soft_state 28247 * EINVAL if data pointer is NULL 28248 */ 28249 28250 static int 28251 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28252 { 28253 struct sd_lun *un; 28254 struct uscsi_cmd *com; 28255 struct cdrom_read mode2_struct; 28256 struct cdrom_read *mode2 = &mode2_struct; 28257 uchar_t cdb[CDB_GROUP5]; 28258 int nblocks; 28259 int rval; 28260 #ifdef _MULTI_DATAMODEL 28261 /* To support ILP32 applications in an LP64 world */ 28262 struct cdrom_read32 cdrom_read32; 28263 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28264 #endif /* _MULTI_DATAMODEL */ 28265 28266 if (data == NULL) { 28267 return (EINVAL); 28268 } 28269 28270 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28271 (un->un_state == SD_STATE_OFFLINE)) { 28272 return (ENXIO); 28273 } 28274 28275 #ifdef _MULTI_DATAMODEL 28276 switch (ddi_model_convert_from(flag & FMODELS)) { 28277 case DDI_MODEL_ILP32: 28278 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28279 return (EFAULT); 28280 } 28281 /* Convert the ILP32 uscsi data from the application to LP64 */ 28282 cdrom_read32tocdrom_read(cdrd32, mode2); 28283 break; 28284 case DDI_MODEL_NONE: 28285 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28286 return (EFAULT); 28287 } 28288 break; 28289 } 28290 28291 #else /* ! _MULTI_DATAMODEL */ 28292 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28293 return (EFAULT); 28294 } 28295 #endif /* _MULTI_DATAMODEL */ 28296 28297 bzero(cdb, sizeof (cdb)); 28298 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28299 /* Read command supported by 1st generation atapi drives */ 28300 cdb[0] = SCMD_READ_CDD4; 28301 } else { 28302 /* Universal CD Access Command */ 28303 cdb[0] = SCMD_READ_CD; 28304 } 28305 28306 /* 28307 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28308 */ 28309 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28310 28311 /* set the start address */ 28312 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28313 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28314 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28315 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28316 28317 /* set the transfer length */ 28318 nblocks = mode2->cdread_buflen / 2336; 28319 cdb[6] = (uchar_t)(nblocks >> 16); 28320 cdb[7] = (uchar_t)(nblocks >> 8); 28321 cdb[8] = (uchar_t)nblocks; 28322 28323 /* set the filter bits */ 28324 cdb[9] = CDROM_READ_CD_USERDATA; 28325 28326 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28327 com->uscsi_cdb = (caddr_t)cdb; 28328 com->uscsi_cdblen = sizeof (cdb); 28329 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28330 com->uscsi_buflen = mode2->cdread_buflen; 28331 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28332 28333 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28334 UIO_SYSSPACE, SD_PATH_STANDARD); 28335 kmem_free(com, sizeof (*com)); 28336 return (rval); 28337 } 28338 28339 28340 /* 28341 * Function: sr_read_mode2() 28342 * 28343 * Description: This routine is the driver entry point for handling CD-ROM 28344 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28345 * do not support the READ CD (0xBE) command. 28346 * 28347 * Arguments: dev - the device 'dev_t' 28348 * data - pointer to user provided cd read structure specifying 28349 * the lba buffer address and length. 28350 * flag - this argument is a pass through to ddi_copyxxx() 28351 * directly from the mode argument of ioctl(). 28352 * 28353 * Return Code: the code returned by sd_send_scsi_cmd() 28354 * EFAULT if ddi_copyxxx() fails 28355 * ENXIO if fail ddi_get_soft_state 28356 * EINVAL if data pointer is NULL 28357 * EIO if fail to reset block size 28358 * EAGAIN if commands are in progress in the driver 28359 */ 28360 28361 static int 28362 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28363 { 28364 struct sd_lun *un; 28365 struct cdrom_read mode2_struct; 28366 struct cdrom_read *mode2 = &mode2_struct; 28367 int rval; 28368 uint32_t restore_blksize; 28369 struct uscsi_cmd *com; 28370 uchar_t cdb[CDB_GROUP0]; 28371 int nblocks; 28372 28373 #ifdef _MULTI_DATAMODEL 28374 /* To support ILP32 applications in an LP64 world */ 28375 struct cdrom_read32 cdrom_read32; 28376 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28377 #endif /* _MULTI_DATAMODEL */ 28378 28379 if (data == NULL) { 28380 return (EINVAL); 28381 } 28382 28383 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28384 (un->un_state == SD_STATE_OFFLINE)) { 28385 return (ENXIO); 28386 } 28387 28388 /* 28389 * Because this routine will update the device and driver block size 28390 * being used we want to make sure there are no commands in progress. 28391 * If commands are in progress the user will have to try again. 28392 * 28393 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28394 * in sdioctl to protect commands from sdioctl through to the top of 28395 * sd_uscsi_strategy. See sdioctl for details. 28396 */ 28397 mutex_enter(SD_MUTEX(un)); 28398 if (un->un_ncmds_in_driver != 1) { 28399 mutex_exit(SD_MUTEX(un)); 28400 return (EAGAIN); 28401 } 28402 mutex_exit(SD_MUTEX(un)); 28403 28404 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28405 "sd_read_mode2: entry: un:0x%p\n", un); 28406 28407 #ifdef _MULTI_DATAMODEL 28408 switch (ddi_model_convert_from(flag & FMODELS)) { 28409 case DDI_MODEL_ILP32: 28410 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28411 return (EFAULT); 28412 } 28413 /* Convert the ILP32 uscsi data from the application to LP64 */ 28414 cdrom_read32tocdrom_read(cdrd32, mode2); 28415 break; 28416 case DDI_MODEL_NONE: 28417 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28418 return (EFAULT); 28419 } 28420 break; 28421 } 28422 #else /* ! _MULTI_DATAMODEL */ 28423 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28424 return (EFAULT); 28425 } 28426 #endif /* _MULTI_DATAMODEL */ 28427 28428 /* Store the current target block size for restoration later */ 28429 restore_blksize = un->un_tgt_blocksize; 28430 28431 /* Change the device and soft state target block size to 2336 */ 28432 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28433 rval = EIO; 28434 goto done; 28435 } 28436 28437 28438 bzero(cdb, sizeof (cdb)); 28439 28440 /* set READ operation */ 28441 cdb[0] = SCMD_READ; 28442 28443 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28444 mode2->cdread_lba >>= 2; 28445 28446 /* set the start address */ 28447 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28448 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28449 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28450 28451 /* set the transfer length */ 28452 nblocks = mode2->cdread_buflen / 2336; 28453 cdb[4] = (uchar_t)nblocks & 0xFF; 28454 28455 /* build command */ 28456 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28457 com->uscsi_cdb = (caddr_t)cdb; 28458 com->uscsi_cdblen = sizeof (cdb); 28459 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28460 com->uscsi_buflen = mode2->cdread_buflen; 28461 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28462 28463 /* 28464 * Issue SCSI command with user space address for read buffer. 28465 * 28466 * This sends the command through main channel in the driver. 28467 * 28468 * Since this is accessed via an IOCTL call, we go through the 28469 * standard path, so that if the device was powered down, then 28470 * it would be 'awakened' to handle the command. 28471 */ 28472 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28473 UIO_SYSSPACE, SD_PATH_STANDARD); 28474 28475 kmem_free(com, sizeof (*com)); 28476 28477 /* Restore the device and soft state target block size */ 28478 if (sr_sector_mode(dev, restore_blksize) != 0) { 28479 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28480 "can't do switch back to mode 1\n"); 28481 /* 28482 * If sd_send_scsi_READ succeeded we still need to report 28483 * an error because we failed to reset the block size 28484 */ 28485 if (rval == 0) { 28486 rval = EIO; 28487 } 28488 } 28489 28490 done: 28491 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28492 "sd_read_mode2: exit: un:0x%p\n", un); 28493 28494 return (rval); 28495 } 28496 28497 28498 /* 28499 * Function: sr_sector_mode() 28500 * 28501 * Description: This utility function is used by sr_read_mode2 to set the target 28502 * block size based on the user specified size. This is a legacy 28503 * implementation based upon a vendor specific mode page 28504 * 28505 * Arguments: dev - the device 'dev_t' 28506 * data - flag indicating if block size is being set to 2336 or 28507 * 512. 28508 * 28509 * Return Code: the code returned by sd_send_scsi_cmd() 28510 * EFAULT if ddi_copyxxx() fails 28511 * ENXIO if fail ddi_get_soft_state 28512 * EINVAL if data pointer is NULL 28513 */ 28514 28515 static int 28516 sr_sector_mode(dev_t dev, uint32_t blksize) 28517 { 28518 struct sd_lun *un; 28519 uchar_t *sense; 28520 uchar_t *select; 28521 int rval; 28522 28523 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28524 (un->un_state == SD_STATE_OFFLINE)) { 28525 return (ENXIO); 28526 } 28527 28528 sense = kmem_zalloc(20, KM_SLEEP); 28529 28530 /* Note: This is a vendor specific mode page (0x81) */ 28531 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28532 SD_PATH_STANDARD)) != 0) { 28533 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28534 "sr_sector_mode: Mode Sense failed\n"); 28535 kmem_free(sense, 20); 28536 return (rval); 28537 } 28538 select = kmem_zalloc(20, KM_SLEEP); 28539 select[3] = 0x08; 28540 select[10] = ((blksize >> 8) & 0xff); 28541 select[11] = (blksize & 0xff); 28542 select[12] = 0x01; 28543 select[13] = 0x06; 28544 select[14] = sense[14]; 28545 select[15] = sense[15]; 28546 if (blksize == SD_MODE2_BLKSIZE) { 28547 select[14] |= 0x01; 28548 } 28549 28550 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28551 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28552 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28553 "sr_sector_mode: Mode Select failed\n"); 28554 } else { 28555 /* 28556 * Only update the softstate block size if we successfully 28557 * changed the device block mode. 28558 */ 28559 mutex_enter(SD_MUTEX(un)); 28560 sd_update_block_info(un, blksize, 0); 28561 mutex_exit(SD_MUTEX(un)); 28562 } 28563 kmem_free(sense, 20); 28564 kmem_free(select, 20); 28565 return (rval); 28566 } 28567 28568 28569 /* 28570 * Function: sr_read_cdda() 28571 * 28572 * Description: This routine is the driver entry point for handling CD-ROM 28573 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28574 * the target supports CDDA these requests are handled via a vendor 28575 * specific command (0xD8) If the target does not support CDDA 28576 * these requests are handled via the READ CD command (0xBE). 28577 * 28578 * Arguments: dev - the device 'dev_t' 28579 * data - pointer to user provided CD-DA structure specifying 28580 * the track starting address, transfer length, and 28581 * subcode options. 28582 * flag - this argument is a pass through to ddi_copyxxx() 28583 * directly from the mode argument of ioctl(). 28584 * 28585 * Return Code: the code returned by sd_send_scsi_cmd() 28586 * EFAULT if ddi_copyxxx() fails 28587 * ENXIO if fail ddi_get_soft_state 28588 * EINVAL if invalid arguments are provided 28589 * ENOTTY 28590 */ 28591 28592 static int 28593 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28594 { 28595 struct sd_lun *un; 28596 struct uscsi_cmd *com; 28597 struct cdrom_cdda *cdda; 28598 int rval; 28599 size_t buflen; 28600 char cdb[CDB_GROUP5]; 28601 28602 #ifdef _MULTI_DATAMODEL 28603 /* To support ILP32 applications in an LP64 world */ 28604 struct cdrom_cdda32 cdrom_cdda32; 28605 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28606 #endif /* _MULTI_DATAMODEL */ 28607 28608 if (data == NULL) { 28609 return (EINVAL); 28610 } 28611 28612 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28613 return (ENXIO); 28614 } 28615 28616 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28617 28618 #ifdef _MULTI_DATAMODEL 28619 switch (ddi_model_convert_from(flag & FMODELS)) { 28620 case DDI_MODEL_ILP32: 28621 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28622 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28623 "sr_read_cdda: ddi_copyin Failed\n"); 28624 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28625 return (EFAULT); 28626 } 28627 /* Convert the ILP32 uscsi data from the application to LP64 */ 28628 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28629 break; 28630 case DDI_MODEL_NONE: 28631 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28632 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28633 "sr_read_cdda: ddi_copyin Failed\n"); 28634 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28635 return (EFAULT); 28636 } 28637 break; 28638 } 28639 #else /* ! _MULTI_DATAMODEL */ 28640 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28641 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28642 "sr_read_cdda: ddi_copyin Failed\n"); 28643 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28644 return (EFAULT); 28645 } 28646 #endif /* _MULTI_DATAMODEL */ 28647 28648 /* 28649 * Since MMC-2 expects max 3 bytes for length, check if the 28650 * length input is greater than 3 bytes 28651 */ 28652 if ((cdda->cdda_length & 0xFF000000) != 0) { 28653 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28654 "cdrom transfer length too large: %d (limit %d)\n", 28655 cdda->cdda_length, 0xFFFFFF); 28656 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28657 return (EINVAL); 28658 } 28659 28660 switch (cdda->cdda_subcode) { 28661 case CDROM_DA_NO_SUBCODE: 28662 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28663 break; 28664 case CDROM_DA_SUBQ: 28665 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28666 break; 28667 case CDROM_DA_ALL_SUBCODE: 28668 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28669 break; 28670 case CDROM_DA_SUBCODE_ONLY: 28671 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28672 break; 28673 default: 28674 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28675 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28676 cdda->cdda_subcode); 28677 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28678 return (EINVAL); 28679 } 28680 28681 /* Build and send the command */ 28682 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28683 bzero(cdb, CDB_GROUP5); 28684 28685 if (un->un_f_cfg_cdda == TRUE) { 28686 cdb[0] = (char)SCMD_READ_CD; 28687 cdb[1] = 0x04; 28688 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28689 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28690 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28691 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28692 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28693 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28694 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28695 cdb[9] = 0x10; 28696 switch (cdda->cdda_subcode) { 28697 case CDROM_DA_NO_SUBCODE : 28698 cdb[10] = 0x0; 28699 break; 28700 case CDROM_DA_SUBQ : 28701 cdb[10] = 0x2; 28702 break; 28703 case CDROM_DA_ALL_SUBCODE : 28704 cdb[10] = 0x1; 28705 break; 28706 case CDROM_DA_SUBCODE_ONLY : 28707 /* FALLTHROUGH */ 28708 default : 28709 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28710 kmem_free(com, sizeof (*com)); 28711 return (ENOTTY); 28712 } 28713 } else { 28714 cdb[0] = (char)SCMD_READ_CDDA; 28715 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28716 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28717 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28718 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28719 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28720 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28721 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28722 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28723 cdb[10] = cdda->cdda_subcode; 28724 } 28725 28726 com->uscsi_cdb = cdb; 28727 com->uscsi_cdblen = CDB_GROUP5; 28728 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28729 com->uscsi_buflen = buflen; 28730 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28731 28732 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28733 UIO_SYSSPACE, SD_PATH_STANDARD); 28734 28735 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28736 kmem_free(com, sizeof (*com)); 28737 return (rval); 28738 } 28739 28740 28741 /* 28742 * Function: sr_read_cdxa() 28743 * 28744 * Description: This routine is the driver entry point for handling CD-ROM 28745 * ioctl requests to return CD-XA (Extended Architecture) data. 28746 * (CDROMCDXA). 28747 * 28748 * Arguments: dev - the device 'dev_t' 28749 * data - pointer to user provided CD-XA structure specifying 28750 * the data starting address, transfer length, and format 28751 * flag - this argument is a pass through to ddi_copyxxx() 28752 * directly from the mode argument of ioctl(). 28753 * 28754 * Return Code: the code returned by sd_send_scsi_cmd() 28755 * EFAULT if ddi_copyxxx() fails 28756 * ENXIO if fail ddi_get_soft_state 28757 * EINVAL if data pointer is NULL 28758 */ 28759 28760 static int 28761 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28762 { 28763 struct sd_lun *un; 28764 struct uscsi_cmd *com; 28765 struct cdrom_cdxa *cdxa; 28766 int rval; 28767 size_t buflen; 28768 char cdb[CDB_GROUP5]; 28769 uchar_t read_flags; 28770 28771 #ifdef _MULTI_DATAMODEL 28772 /* To support ILP32 applications in an LP64 world */ 28773 struct cdrom_cdxa32 cdrom_cdxa32; 28774 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28775 #endif /* _MULTI_DATAMODEL */ 28776 28777 if (data == NULL) { 28778 return (EINVAL); 28779 } 28780 28781 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28782 return (ENXIO); 28783 } 28784 28785 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28786 28787 #ifdef _MULTI_DATAMODEL 28788 switch (ddi_model_convert_from(flag & FMODELS)) { 28789 case DDI_MODEL_ILP32: 28790 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28791 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28792 return (EFAULT); 28793 } 28794 /* 28795 * Convert the ILP32 uscsi data from the 28796 * application to LP64 for internal use. 28797 */ 28798 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28799 break; 28800 case DDI_MODEL_NONE: 28801 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28802 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28803 return (EFAULT); 28804 } 28805 break; 28806 } 28807 #else /* ! _MULTI_DATAMODEL */ 28808 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28809 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28810 return (EFAULT); 28811 } 28812 #endif /* _MULTI_DATAMODEL */ 28813 28814 /* 28815 * Since MMC-2 expects max 3 bytes for length, check if the 28816 * length input is greater than 3 bytes 28817 */ 28818 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28819 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28820 "cdrom transfer length too large: %d (limit %d)\n", 28821 cdxa->cdxa_length, 0xFFFFFF); 28822 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28823 return (EINVAL); 28824 } 28825 28826 switch (cdxa->cdxa_format) { 28827 case CDROM_XA_DATA: 28828 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28829 read_flags = 0x10; 28830 break; 28831 case CDROM_XA_SECTOR_DATA: 28832 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28833 read_flags = 0xf8; 28834 break; 28835 case CDROM_XA_DATA_W_ERROR: 28836 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28837 read_flags = 0xfc; 28838 break; 28839 default: 28840 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28841 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28842 cdxa->cdxa_format); 28843 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28844 return (EINVAL); 28845 } 28846 28847 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28848 bzero(cdb, CDB_GROUP5); 28849 if (un->un_f_mmc_cap == TRUE) { 28850 cdb[0] = (char)SCMD_READ_CD; 28851 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28852 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28853 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28854 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28855 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28856 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28857 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28858 cdb[9] = (char)read_flags; 28859 } else { 28860 /* 28861 * Note: A vendor specific command (0xDB) is being used her to 28862 * request a read of all subcodes. 28863 */ 28864 cdb[0] = (char)SCMD_READ_CDXA; 28865 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28866 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28867 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28868 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28869 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28870 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28871 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28872 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28873 cdb[10] = cdxa->cdxa_format; 28874 } 28875 com->uscsi_cdb = cdb; 28876 com->uscsi_cdblen = CDB_GROUP5; 28877 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28878 com->uscsi_buflen = buflen; 28879 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28880 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28881 UIO_SYSSPACE, SD_PATH_STANDARD); 28882 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28883 kmem_free(com, sizeof (*com)); 28884 return (rval); 28885 } 28886 28887 28888 /* 28889 * Function: sr_eject() 28890 * 28891 * Description: This routine is the driver entry point for handling CD-ROM 28892 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28893 * 28894 * Arguments: dev - the device 'dev_t' 28895 * 28896 * Return Code: the code returned by sd_send_scsi_cmd() 28897 */ 28898 28899 static int 28900 sr_eject(dev_t dev) 28901 { 28902 struct sd_lun *un; 28903 int rval; 28904 28905 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28906 (un->un_state == SD_STATE_OFFLINE)) { 28907 return (ENXIO); 28908 } 28909 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 28910 SD_PATH_STANDARD)) != 0) { 28911 return (rval); 28912 } 28913 28914 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 28915 SD_PATH_STANDARD); 28916 28917 if (rval == 0) { 28918 mutex_enter(SD_MUTEX(un)); 28919 sr_ejected(un); 28920 un->un_mediastate = DKIO_EJECTED; 28921 cv_broadcast(&un->un_state_cv); 28922 mutex_exit(SD_MUTEX(un)); 28923 } 28924 return (rval); 28925 } 28926 28927 28928 /* 28929 * Function: sr_ejected() 28930 * 28931 * Description: This routine updates the soft state structure to invalidate the 28932 * geometry information after the media has been ejected or a 28933 * media eject has been detected. 28934 * 28935 * Arguments: un - driver soft state (unit) structure 28936 */ 28937 28938 static void 28939 sr_ejected(struct sd_lun *un) 28940 { 28941 struct sd_errstats *stp; 28942 28943 ASSERT(un != NULL); 28944 ASSERT(mutex_owned(SD_MUTEX(un))); 28945 28946 un->un_f_blockcount_is_valid = FALSE; 28947 un->un_f_tgt_blocksize_is_valid = FALSE; 28948 un->un_f_geometry_is_valid = FALSE; 28949 28950 if (un->un_errstats != NULL) { 28951 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28952 stp->sd_capacity.value.ui64 = 0; 28953 } 28954 } 28955 28956 28957 /* 28958 * Function: sr_check_wp() 28959 * 28960 * Description: This routine checks the write protection of a removable media 28961 * disk via the write protect bit of the Mode Page Header device 28962 * specific field. This routine has been implemented to use the 28963 * error recovery mode page for all device types. 28964 * Note: In the future use a sd_send_scsi_MODE_SENSE() routine 28965 * 28966 * Arguments: dev - the device 'dev_t' 28967 * 28968 * Return Code: int indicating if the device is write protected (1) or not (0) 28969 * 28970 * Context: Kernel thread. 28971 * 28972 */ 28973 28974 static int 28975 sr_check_wp(dev_t dev) 28976 { 28977 struct sd_lun *un; 28978 uchar_t device_specific; 28979 uchar_t *sense; 28980 int hdrlen; 28981 int rval; 28982 int retry_flag = FALSE; 28983 28984 /* 28985 * Note: The return codes for this routine should be reworked to 28986 * properly handle the case of a NULL softstate. 28987 */ 28988 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28989 return (FALSE); 28990 } 28991 28992 if (un->un_f_cfg_is_atapi == TRUE) { 28993 retry_flag = TRUE; 28994 } 28995 28996 retry: 28997 if (un->un_f_cfg_is_atapi == TRUE) { 28998 /* 28999 * The mode page contents are not required; set the allocation 29000 * length for the mode page header only 29001 */ 29002 hdrlen = MODE_HEADER_LENGTH_GRP2; 29003 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29004 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29005 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 29006 device_specific = 29007 ((struct mode_header_grp2 *)sense)->device_specific; 29008 } else { 29009 hdrlen = MODE_HEADER_LENGTH; 29010 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29011 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29012 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 29013 device_specific = 29014 ((struct mode_header *)sense)->device_specific; 29015 } 29016 29017 if (rval != 0) { 29018 if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) { 29019 /* 29020 * For an Atapi Zip drive, observed the drive 29021 * reporting check condition for the first attempt. 29022 * Sense data indicating power on or bus device/reset. 29023 * Hence in case of failure need to try at least once 29024 * for Atapi devices. 29025 */ 29026 retry_flag = FALSE; 29027 kmem_free(sense, hdrlen); 29028 goto retry; 29029 } else { 29030 /* 29031 * Write protect mode sense failed; not all disks 29032 * understand this query. Return FALSE assuming that 29033 * these devices are not writable. 29034 */ 29035 rval = FALSE; 29036 } 29037 } else { 29038 if (device_specific & WRITE_PROTECT) { 29039 rval = TRUE; 29040 } else { 29041 rval = FALSE; 29042 } 29043 } 29044 kmem_free(sense, hdrlen); 29045 return (rval); 29046 } 29047 29048 29049 /* 29050 * Function: sr_volume_ctrl() 29051 * 29052 * Description: This routine is the driver entry point for handling CD-ROM 29053 * audio output volume ioctl requests. (CDROMVOLCTRL) 29054 * 29055 * Arguments: dev - the device 'dev_t' 29056 * data - pointer to user audio volume control structure 29057 * flag - this argument is a pass through to ddi_copyxxx() 29058 * directly from the mode argument of ioctl(). 29059 * 29060 * Return Code: the code returned by sd_send_scsi_cmd() 29061 * EFAULT if ddi_copyxxx() fails 29062 * ENXIO if fail ddi_get_soft_state 29063 * EINVAL if data pointer is NULL 29064 * 29065 */ 29066 29067 static int 29068 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29069 { 29070 struct sd_lun *un; 29071 struct cdrom_volctrl volume; 29072 struct cdrom_volctrl *vol = &volume; 29073 uchar_t *sense_page; 29074 uchar_t *select_page; 29075 uchar_t *sense; 29076 uchar_t *select; 29077 int sense_buflen; 29078 int select_buflen; 29079 int rval; 29080 29081 if (data == NULL) { 29082 return (EINVAL); 29083 } 29084 29085 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29086 (un->un_state == SD_STATE_OFFLINE)) { 29087 return (ENXIO); 29088 } 29089 29090 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29091 return (EFAULT); 29092 } 29093 29094 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29095 struct mode_header_grp2 *sense_mhp; 29096 struct mode_header_grp2 *select_mhp; 29097 int bd_len; 29098 29099 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29100 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29101 MODEPAGE_AUDIO_CTRL_LEN; 29102 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29103 select = kmem_zalloc(select_buflen, KM_SLEEP); 29104 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29105 sense_buflen, MODEPAGE_AUDIO_CTRL, 29106 SD_PATH_STANDARD)) != 0) { 29107 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29108 "sr_volume_ctrl: Mode Sense Failed\n"); 29109 kmem_free(sense, sense_buflen); 29110 kmem_free(select, select_buflen); 29111 return (rval); 29112 } 29113 sense_mhp = (struct mode_header_grp2 *)sense; 29114 select_mhp = (struct mode_header_grp2 *)select; 29115 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29116 sense_mhp->bdesc_length_lo; 29117 if (bd_len > MODE_BLK_DESC_LENGTH) { 29118 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29119 "sr_volume_ctrl: Mode Sense returned invalid " 29120 "block descriptor length\n"); 29121 kmem_free(sense, sense_buflen); 29122 kmem_free(select, select_buflen); 29123 return (EIO); 29124 } 29125 sense_page = (uchar_t *) 29126 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29127 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29128 select_mhp->length_msb = 0; 29129 select_mhp->length_lsb = 0; 29130 select_mhp->bdesc_length_hi = 0; 29131 select_mhp->bdesc_length_lo = 0; 29132 } else { 29133 struct mode_header *sense_mhp, *select_mhp; 29134 29135 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29136 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29137 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29138 select = kmem_zalloc(select_buflen, KM_SLEEP); 29139 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29140 sense_buflen, MODEPAGE_AUDIO_CTRL, 29141 SD_PATH_STANDARD)) != 0) { 29142 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29143 "sr_volume_ctrl: Mode Sense Failed\n"); 29144 kmem_free(sense, sense_buflen); 29145 kmem_free(select, select_buflen); 29146 return (rval); 29147 } 29148 sense_mhp = (struct mode_header *)sense; 29149 select_mhp = (struct mode_header *)select; 29150 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29151 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29152 "sr_volume_ctrl: Mode Sense returned invalid " 29153 "block descriptor length\n"); 29154 kmem_free(sense, sense_buflen); 29155 kmem_free(select, select_buflen); 29156 return (EIO); 29157 } 29158 sense_page = (uchar_t *) 29159 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29160 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29161 select_mhp->length = 0; 29162 select_mhp->bdesc_length = 0; 29163 } 29164 /* 29165 * Note: An audio control data structure could be created and overlayed 29166 * on the following in place of the array indexing method implemented. 29167 */ 29168 29169 /* Build the select data for the user volume data */ 29170 select_page[0] = MODEPAGE_AUDIO_CTRL; 29171 select_page[1] = 0xE; 29172 /* Set the immediate bit */ 29173 select_page[2] = 0x04; 29174 /* Zero out reserved fields */ 29175 select_page[3] = 0x00; 29176 select_page[4] = 0x00; 29177 /* Return sense data for fields not to be modified */ 29178 select_page[5] = sense_page[5]; 29179 select_page[6] = sense_page[6]; 29180 select_page[7] = sense_page[7]; 29181 /* Set the user specified volume levels for channel 0 and 1 */ 29182 select_page[8] = 0x01; 29183 select_page[9] = vol->channel0; 29184 select_page[10] = 0x02; 29185 select_page[11] = vol->channel1; 29186 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29187 select_page[12] = sense_page[12]; 29188 select_page[13] = sense_page[13]; 29189 select_page[14] = sense_page[14]; 29190 select_page[15] = sense_page[15]; 29191 29192 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29193 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29194 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29195 } else { 29196 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29197 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29198 } 29199 29200 kmem_free(sense, sense_buflen); 29201 kmem_free(select, select_buflen); 29202 return (rval); 29203 } 29204 29205 29206 /* 29207 * Function: sr_read_sony_session_offset() 29208 * 29209 * Description: This routine is the driver entry point for handling CD-ROM 29210 * ioctl requests for session offset information. (CDROMREADOFFSET) 29211 * The address of the first track in the last session of a 29212 * multi-session CD-ROM is returned 29213 * 29214 * Note: This routine uses a vendor specific key value in the 29215 * command control field without implementing any vendor check here 29216 * or in the ioctl routine. 29217 * 29218 * Arguments: dev - the device 'dev_t' 29219 * data - pointer to an int to hold the requested address 29220 * flag - this argument is a pass through to ddi_copyxxx() 29221 * directly from the mode argument of ioctl(). 29222 * 29223 * Return Code: the code returned by sd_send_scsi_cmd() 29224 * EFAULT if ddi_copyxxx() fails 29225 * ENXIO if fail ddi_get_soft_state 29226 * EINVAL if data pointer is NULL 29227 */ 29228 29229 static int 29230 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29231 { 29232 struct sd_lun *un; 29233 struct uscsi_cmd *com; 29234 caddr_t buffer; 29235 char cdb[CDB_GROUP1]; 29236 int session_offset = 0; 29237 int rval; 29238 29239 if (data == NULL) { 29240 return (EINVAL); 29241 } 29242 29243 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29244 (un->un_state == SD_STATE_OFFLINE)) { 29245 return (ENXIO); 29246 } 29247 29248 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29249 bzero(cdb, CDB_GROUP1); 29250 cdb[0] = SCMD_READ_TOC; 29251 /* 29252 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29253 * (4 byte TOC response header + 8 byte response data) 29254 */ 29255 cdb[8] = SONY_SESSION_OFFSET_LEN; 29256 /* Byte 9 is the control byte. A vendor specific value is used */ 29257 cdb[9] = SONY_SESSION_OFFSET_KEY; 29258 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29259 com->uscsi_cdb = cdb; 29260 com->uscsi_cdblen = CDB_GROUP1; 29261 com->uscsi_bufaddr = buffer; 29262 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29263 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29264 29265 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29266 UIO_SYSSPACE, SD_PATH_STANDARD); 29267 if (rval != 0) { 29268 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29269 kmem_free(com, sizeof (*com)); 29270 return (rval); 29271 } 29272 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29273 session_offset = 29274 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29275 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29276 /* 29277 * Offset returned offset in current lbasize block's. Convert to 29278 * 2k block's to return to the user 29279 */ 29280 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29281 session_offset >>= 2; 29282 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29283 session_offset >>= 1; 29284 } 29285 } 29286 29287 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29288 rval = EFAULT; 29289 } 29290 29291 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29292 kmem_free(com, sizeof (*com)); 29293 return (rval); 29294 } 29295 29296 29297 /* 29298 * Function: sd_wm_cache_constructor() 29299 * 29300 * Description: Cache Constructor for the wmap cache for the read/modify/write 29301 * devices. 29302 * 29303 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29304 * un - sd_lun structure for the device. 29305 * flag - the km flags passed to constructor 29306 * 29307 * Return Code: 0 on success. 29308 * -1 on failure. 29309 */ 29310 29311 /*ARGSUSED*/ 29312 static int 29313 sd_wm_cache_constructor(void *wm, void *un, int flags) 29314 { 29315 bzero(wm, sizeof (struct sd_w_map)); 29316 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29317 return (0); 29318 } 29319 29320 29321 /* 29322 * Function: sd_wm_cache_destructor() 29323 * 29324 * Description: Cache destructor for the wmap cache for the read/modify/write 29325 * devices. 29326 * 29327 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29328 * un - sd_lun structure for the device. 29329 */ 29330 /*ARGSUSED*/ 29331 static void 29332 sd_wm_cache_destructor(void *wm, void *un) 29333 { 29334 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29335 } 29336 29337 29338 /* 29339 * Function: sd_range_lock() 29340 * 29341 * Description: Lock the range of blocks specified as parameter to ensure 29342 * that read, modify write is atomic and no other i/o writes 29343 * to the same location. The range is specified in terms 29344 * of start and end blocks. Block numbers are the actual 29345 * media block numbers and not system. 29346 * 29347 * Arguments: un - sd_lun structure for the device. 29348 * startb - The starting block number 29349 * endb - The end block number 29350 * typ - type of i/o - simple/read_modify_write 29351 * 29352 * Return Code: wm - pointer to the wmap structure. 29353 * 29354 * Context: This routine can sleep. 29355 */ 29356 29357 static struct sd_w_map * 29358 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29359 { 29360 struct sd_w_map *wmp = NULL; 29361 struct sd_w_map *sl_wmp = NULL; 29362 struct sd_w_map *tmp_wmp; 29363 wm_state state = SD_WM_CHK_LIST; 29364 29365 29366 ASSERT(un != NULL); 29367 ASSERT(!mutex_owned(SD_MUTEX(un))); 29368 29369 mutex_enter(SD_MUTEX(un)); 29370 29371 while (state != SD_WM_DONE) { 29372 29373 switch (state) { 29374 case SD_WM_CHK_LIST: 29375 /* 29376 * This is the starting state. Check the wmap list 29377 * to see if the range is currently available. 29378 */ 29379 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29380 /* 29381 * If this is a simple write and no rmw 29382 * i/o is pending then try to lock the 29383 * range as the range should be available. 29384 */ 29385 state = SD_WM_LOCK_RANGE; 29386 } else { 29387 tmp_wmp = sd_get_range(un, startb, endb); 29388 if (tmp_wmp != NULL) { 29389 if ((wmp != NULL) && ONLIST(un, wmp)) { 29390 /* 29391 * Should not keep onlist wmps 29392 * while waiting this macro 29393 * will also do wmp = NULL; 29394 */ 29395 FREE_ONLIST_WMAP(un, wmp); 29396 } 29397 /* 29398 * sl_wmp is the wmap on which wait 29399 * is done, since the tmp_wmp points 29400 * to the inuse wmap, set sl_wmp to 29401 * tmp_wmp and change the state to sleep 29402 */ 29403 sl_wmp = tmp_wmp; 29404 state = SD_WM_WAIT_MAP; 29405 } else { 29406 state = SD_WM_LOCK_RANGE; 29407 } 29408 29409 } 29410 break; 29411 29412 case SD_WM_LOCK_RANGE: 29413 ASSERT(un->un_wm_cache); 29414 /* 29415 * The range need to be locked, try to get a wmap. 29416 * First attempt it with NO_SLEEP, want to avoid a sleep 29417 * if possible as we will have to release the sd mutex 29418 * if we have to sleep. 29419 */ 29420 if (wmp == NULL) 29421 wmp = kmem_cache_alloc(un->un_wm_cache, 29422 KM_NOSLEEP); 29423 if (wmp == NULL) { 29424 mutex_exit(SD_MUTEX(un)); 29425 _NOTE(DATA_READABLE_WITHOUT_LOCK 29426 (sd_lun::un_wm_cache)) 29427 wmp = kmem_cache_alloc(un->un_wm_cache, 29428 KM_SLEEP); 29429 mutex_enter(SD_MUTEX(un)); 29430 /* 29431 * we released the mutex so recheck and go to 29432 * check list state. 29433 */ 29434 state = SD_WM_CHK_LIST; 29435 } else { 29436 /* 29437 * We exit out of state machine since we 29438 * have the wmap. Do the housekeeping first. 29439 * place the wmap on the wmap list if it is not 29440 * on it already and then set the state to done. 29441 */ 29442 wmp->wm_start = startb; 29443 wmp->wm_end = endb; 29444 wmp->wm_flags = typ | SD_WM_BUSY; 29445 if (typ & SD_WTYPE_RMW) { 29446 un->un_rmw_count++; 29447 } 29448 /* 29449 * If not already on the list then link 29450 */ 29451 if (!ONLIST(un, wmp)) { 29452 wmp->wm_next = un->un_wm; 29453 wmp->wm_prev = NULL; 29454 if (wmp->wm_next) 29455 wmp->wm_next->wm_prev = wmp; 29456 un->un_wm = wmp; 29457 } 29458 state = SD_WM_DONE; 29459 } 29460 break; 29461 29462 case SD_WM_WAIT_MAP: 29463 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29464 /* 29465 * Wait is done on sl_wmp, which is set in the 29466 * check_list state. 29467 */ 29468 sl_wmp->wm_wanted_count++; 29469 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29470 sl_wmp->wm_wanted_count--; 29471 /* 29472 * We can reuse the memory from the completed sl_wmp 29473 * lock range for our new lock, but only if noone is 29474 * waiting for it. 29475 */ 29476 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29477 if (sl_wmp->wm_wanted_count == 0) { 29478 if (wmp != NULL) 29479 CHK_N_FREEWMP(un, wmp); 29480 wmp = sl_wmp; 29481 } 29482 sl_wmp = NULL; 29483 /* 29484 * After waking up, need to recheck for availability of 29485 * range. 29486 */ 29487 state = SD_WM_CHK_LIST; 29488 break; 29489 29490 default: 29491 panic("sd_range_lock: " 29492 "Unknown state %d in sd_range_lock", state); 29493 /*NOTREACHED*/ 29494 } /* switch(state) */ 29495 29496 } /* while(state != SD_WM_DONE) */ 29497 29498 mutex_exit(SD_MUTEX(un)); 29499 29500 ASSERT(wmp != NULL); 29501 29502 return (wmp); 29503 } 29504 29505 29506 /* 29507 * Function: sd_get_range() 29508 * 29509 * Description: Find if there any overlapping I/O to this one 29510 * Returns the write-map of 1st such I/O, NULL otherwise. 29511 * 29512 * Arguments: un - sd_lun structure for the device. 29513 * startb - The starting block number 29514 * endb - The end block number 29515 * 29516 * Return Code: wm - pointer to the wmap structure. 29517 */ 29518 29519 static struct sd_w_map * 29520 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29521 { 29522 struct sd_w_map *wmp; 29523 29524 ASSERT(un != NULL); 29525 29526 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29527 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29528 continue; 29529 } 29530 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29531 break; 29532 } 29533 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29534 break; 29535 } 29536 } 29537 29538 return (wmp); 29539 } 29540 29541 29542 /* 29543 * Function: sd_free_inlist_wmap() 29544 * 29545 * Description: Unlink and free a write map struct. 29546 * 29547 * Arguments: un - sd_lun structure for the device. 29548 * wmp - sd_w_map which needs to be unlinked. 29549 */ 29550 29551 static void 29552 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29553 { 29554 ASSERT(un != NULL); 29555 29556 if (un->un_wm == wmp) { 29557 un->un_wm = wmp->wm_next; 29558 } else { 29559 wmp->wm_prev->wm_next = wmp->wm_next; 29560 } 29561 29562 if (wmp->wm_next) { 29563 wmp->wm_next->wm_prev = wmp->wm_prev; 29564 } 29565 29566 wmp->wm_next = wmp->wm_prev = NULL; 29567 29568 kmem_cache_free(un->un_wm_cache, wmp); 29569 } 29570 29571 29572 /* 29573 * Function: sd_range_unlock() 29574 * 29575 * Description: Unlock the range locked by wm. 29576 * Free write map if nobody else is waiting on it. 29577 * 29578 * Arguments: un - sd_lun structure for the device. 29579 * wmp - sd_w_map which needs to be unlinked. 29580 */ 29581 29582 static void 29583 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29584 { 29585 ASSERT(un != NULL); 29586 ASSERT(wm != NULL); 29587 ASSERT(!mutex_owned(SD_MUTEX(un))); 29588 29589 mutex_enter(SD_MUTEX(un)); 29590 29591 if (wm->wm_flags & SD_WTYPE_RMW) { 29592 un->un_rmw_count--; 29593 } 29594 29595 if (wm->wm_wanted_count) { 29596 wm->wm_flags = 0; 29597 /* 29598 * Broadcast that the wmap is available now. 29599 */ 29600 cv_broadcast(&wm->wm_avail); 29601 } else { 29602 /* 29603 * If no one is waiting on the map, it should be free'ed. 29604 */ 29605 sd_free_inlist_wmap(un, wm); 29606 } 29607 29608 mutex_exit(SD_MUTEX(un)); 29609 } 29610 29611 29612 /* 29613 * Function: sd_read_modify_write_task 29614 * 29615 * Description: Called from a taskq thread to initiate the write phase of 29616 * a read-modify-write request. This is used for targets where 29617 * un->un_sys_blocksize != un->un_tgt_blocksize. 29618 * 29619 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29620 * 29621 * Context: Called under taskq thread context. 29622 */ 29623 29624 static void 29625 sd_read_modify_write_task(void *arg) 29626 { 29627 struct sd_mapblocksize_info *bsp; 29628 struct buf *bp; 29629 struct sd_xbuf *xp; 29630 struct sd_lun *un; 29631 29632 bp = arg; /* The bp is given in arg */ 29633 ASSERT(bp != NULL); 29634 29635 /* Get the pointer to the layer-private data struct */ 29636 xp = SD_GET_XBUF(bp); 29637 ASSERT(xp != NULL); 29638 bsp = xp->xb_private; 29639 ASSERT(bsp != NULL); 29640 29641 un = SD_GET_UN(bp); 29642 ASSERT(un != NULL); 29643 ASSERT(!mutex_owned(SD_MUTEX(un))); 29644 29645 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29646 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29647 29648 /* 29649 * This is the write phase of a read-modify-write request, called 29650 * under the context of a taskq thread in response to the completion 29651 * of the read portion of the rmw request completing under interrupt 29652 * context. The write request must be sent from here down the iostart 29653 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29654 * we use the layer index saved in the layer-private data area. 29655 */ 29656 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29657 29658 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29659 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29660 } 29661 29662 29663 /* 29664 * Function: sddump_do_read_of_rmw() 29665 * 29666 * Description: This routine will be called from sddump, If sddump is called 29667 * with an I/O which not aligned on device blocksize boundary 29668 * then the write has to be converted to read-modify-write. 29669 * Do the read part here in order to keep sddump simple. 29670 * Note - That the sd_mutex is held across the call to this 29671 * routine. 29672 * 29673 * Arguments: un - sd_lun 29674 * blkno - block number in terms of media block size. 29675 * nblk - number of blocks. 29676 * bpp - pointer to pointer to the buf structure. On return 29677 * from this function, *bpp points to the valid buffer 29678 * to which the write has to be done. 29679 * 29680 * Return Code: 0 for success or errno-type return code 29681 */ 29682 29683 static int 29684 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29685 struct buf **bpp) 29686 { 29687 int err; 29688 int i; 29689 int rval; 29690 struct buf *bp; 29691 struct scsi_pkt *pkt = NULL; 29692 uint32_t target_blocksize; 29693 29694 ASSERT(un != NULL); 29695 ASSERT(mutex_owned(SD_MUTEX(un))); 29696 29697 target_blocksize = un->un_tgt_blocksize; 29698 29699 mutex_exit(SD_MUTEX(un)); 29700 29701 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29702 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29703 if (bp == NULL) { 29704 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29705 "no resources for dumping; giving up"); 29706 err = ENOMEM; 29707 goto done; 29708 } 29709 29710 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29711 blkno, nblk); 29712 if (rval != 0) { 29713 scsi_free_consistent_buf(bp); 29714 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29715 "no resources for dumping; giving up"); 29716 err = ENOMEM; 29717 goto done; 29718 } 29719 29720 pkt->pkt_flags |= FLAG_NOINTR; 29721 29722 err = EIO; 29723 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29724 29725 /* 29726 * Scsi_poll returns 0 (success) if the command completes and 29727 * the status block is STATUS_GOOD. We should only check 29728 * errors if this condition is not true. Even then we should 29729 * send our own request sense packet only if we have a check 29730 * condition and auto request sense has not been performed by 29731 * the hba. 29732 */ 29733 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29734 29735 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29736 err = 0; 29737 break; 29738 } 29739 29740 /* 29741 * Check CMD_DEV_GONE 1st, give up if device is gone, 29742 * no need to read RQS data. 29743 */ 29744 if (pkt->pkt_reason == CMD_DEV_GONE) { 29745 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29746 "Device is gone\n"); 29747 break; 29748 } 29749 29750 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29751 SD_INFO(SD_LOG_DUMP, un, 29752 "sddump: read failed with CHECK, try # %d\n", i); 29753 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29754 (void) sd_send_polled_RQS(un); 29755 } 29756 29757 continue; 29758 } 29759 29760 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29761 int reset_retval = 0; 29762 29763 SD_INFO(SD_LOG_DUMP, un, 29764 "sddump: read failed with BUSY, try # %d\n", i); 29765 29766 if (un->un_f_lun_reset_enabled == TRUE) { 29767 reset_retval = scsi_reset(SD_ADDRESS(un), 29768 RESET_LUN); 29769 } 29770 if (reset_retval == 0) { 29771 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29772 } 29773 (void) sd_send_polled_RQS(un); 29774 29775 } else { 29776 SD_INFO(SD_LOG_DUMP, un, 29777 "sddump: read failed with 0x%x, try # %d\n", 29778 SD_GET_PKT_STATUS(pkt), i); 29779 mutex_enter(SD_MUTEX(un)); 29780 sd_reset_target(un, pkt); 29781 mutex_exit(SD_MUTEX(un)); 29782 } 29783 29784 /* 29785 * If we are not getting anywhere with lun/target resets, 29786 * let's reset the bus. 29787 */ 29788 if (i > SD_NDUMP_RETRIES/2) { 29789 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29790 (void) sd_send_polled_RQS(un); 29791 } 29792 29793 } 29794 scsi_destroy_pkt(pkt); 29795 29796 if (err != 0) { 29797 scsi_free_consistent_buf(bp); 29798 *bpp = NULL; 29799 } else { 29800 *bpp = bp; 29801 } 29802 29803 done: 29804 mutex_enter(SD_MUTEX(un)); 29805 return (err); 29806 } 29807 29808 29809 /* 29810 * Function: sd_failfast_flushq 29811 * 29812 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29813 * in b_flags and move them onto the failfast queue, then kick 29814 * off a thread to return all bp's on the failfast queue to 29815 * their owners with an error set. 29816 * 29817 * Arguments: un - pointer to the soft state struct for the instance. 29818 * 29819 * Context: may execute in interrupt context. 29820 */ 29821 29822 static void 29823 sd_failfast_flushq(struct sd_lun *un) 29824 { 29825 struct buf *bp; 29826 struct buf *next_waitq_bp; 29827 struct buf *prev_waitq_bp = NULL; 29828 29829 ASSERT(un != NULL); 29830 ASSERT(mutex_owned(SD_MUTEX(un))); 29831 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29832 ASSERT(un->un_failfast_bp == NULL); 29833 29834 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29835 "sd_failfast_flushq: entry: un:0x%p\n", un); 29836 29837 /* 29838 * Check if we should flush all bufs when entering failfast state, or 29839 * just those with B_FAILFAST set. 29840 */ 29841 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29842 /* 29843 * Move *all* bp's on the wait queue to the failfast flush 29844 * queue, including those that do NOT have B_FAILFAST set. 29845 */ 29846 if (un->un_failfast_headp == NULL) { 29847 ASSERT(un->un_failfast_tailp == NULL); 29848 un->un_failfast_headp = un->un_waitq_headp; 29849 } else { 29850 ASSERT(un->un_failfast_tailp != NULL); 29851 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29852 } 29853 29854 un->un_failfast_tailp = un->un_waitq_tailp; 29855 29856 /* update kstat for each bp moved out of the waitq */ 29857 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29858 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29859 } 29860 29861 /* empty the waitq */ 29862 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29863 29864 } else { 29865 /* 29866 * Go thru the wait queue, pick off all entries with 29867 * B_FAILFAST set, and move these onto the failfast queue. 29868 */ 29869 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29870 /* 29871 * Save the pointer to the next bp on the wait queue, 29872 * so we get to it on the next iteration of this loop. 29873 */ 29874 next_waitq_bp = bp->av_forw; 29875 29876 /* 29877 * If this bp from the wait queue does NOT have 29878 * B_FAILFAST set, just move on to the next element 29879 * in the wait queue. Note, this is the only place 29880 * where it is correct to set prev_waitq_bp. 29881 */ 29882 if ((bp->b_flags & B_FAILFAST) == 0) { 29883 prev_waitq_bp = bp; 29884 continue; 29885 } 29886 29887 /* 29888 * Remove the bp from the wait queue. 29889 */ 29890 if (bp == un->un_waitq_headp) { 29891 /* The bp is the first element of the waitq. */ 29892 un->un_waitq_headp = next_waitq_bp; 29893 if (un->un_waitq_headp == NULL) { 29894 /* The wait queue is now empty */ 29895 un->un_waitq_tailp = NULL; 29896 } 29897 } else { 29898 /* 29899 * The bp is either somewhere in the middle 29900 * or at the end of the wait queue. 29901 */ 29902 ASSERT(un->un_waitq_headp != NULL); 29903 ASSERT(prev_waitq_bp != NULL); 29904 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29905 == 0); 29906 if (bp == un->un_waitq_tailp) { 29907 /* bp is the last entry on the waitq. */ 29908 ASSERT(next_waitq_bp == NULL); 29909 un->un_waitq_tailp = prev_waitq_bp; 29910 } 29911 prev_waitq_bp->av_forw = next_waitq_bp; 29912 } 29913 bp->av_forw = NULL; 29914 29915 /* 29916 * update kstat since the bp is moved out of 29917 * the waitq 29918 */ 29919 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29920 29921 /* 29922 * Now put the bp onto the failfast queue. 29923 */ 29924 if (un->un_failfast_headp == NULL) { 29925 /* failfast queue is currently empty */ 29926 ASSERT(un->un_failfast_tailp == NULL); 29927 un->un_failfast_headp = 29928 un->un_failfast_tailp = bp; 29929 } else { 29930 /* Add the bp to the end of the failfast q */ 29931 ASSERT(un->un_failfast_tailp != NULL); 29932 ASSERT(un->un_failfast_tailp->b_flags & 29933 B_FAILFAST); 29934 un->un_failfast_tailp->av_forw = bp; 29935 un->un_failfast_tailp = bp; 29936 } 29937 } 29938 } 29939 29940 /* 29941 * Now return all bp's on the failfast queue to their owners. 29942 */ 29943 while ((bp = un->un_failfast_headp) != NULL) { 29944 29945 un->un_failfast_headp = bp->av_forw; 29946 if (un->un_failfast_headp == NULL) { 29947 un->un_failfast_tailp = NULL; 29948 } 29949 29950 /* 29951 * We want to return the bp with a failure error code, but 29952 * we do not want a call to sd_start_cmds() to occur here, 29953 * so use sd_return_failed_command_no_restart() instead of 29954 * sd_return_failed_command(). 29955 */ 29956 sd_return_failed_command_no_restart(un, bp, EIO); 29957 } 29958 29959 /* Flush the xbuf queues if required. */ 29960 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29961 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29962 } 29963 29964 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29965 "sd_failfast_flushq: exit: un:0x%p\n", un); 29966 } 29967 29968 29969 /* 29970 * Function: sd_failfast_flushq_callback 29971 * 29972 * Description: Return TRUE if the given bp meets the criteria for failfast 29973 * flushing. Used with ddi_xbuf_flushq(9F). 29974 * 29975 * Arguments: bp - ptr to buf struct to be examined. 29976 * 29977 * Context: Any 29978 */ 29979 29980 static int 29981 sd_failfast_flushq_callback(struct buf *bp) 29982 { 29983 /* 29984 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29985 * state is entered; OR (2) the given bp has B_FAILFAST set. 29986 */ 29987 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29988 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29989 } 29990 29991 29992 29993 #if defined(__i386) || defined(__amd64) 29994 /* 29995 * Function: sd_setup_next_xfer 29996 * 29997 * Description: Prepare next I/O operation using DMA_PARTIAL 29998 * 29999 */ 30000 30001 static int 30002 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30003 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30004 { 30005 ssize_t num_blks_not_xfered; 30006 daddr_t strt_blk_num; 30007 ssize_t bytes_not_xfered; 30008 int rval; 30009 30010 ASSERT(pkt->pkt_resid == 0); 30011 30012 /* 30013 * Calculate next block number and amount to be transferred. 30014 * 30015 * How much data NOT transfered to the HBA yet. 30016 */ 30017 bytes_not_xfered = xp->xb_dma_resid; 30018 30019 /* 30020 * figure how many blocks NOT transfered to the HBA yet. 30021 */ 30022 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30023 30024 /* 30025 * set starting block number to the end of what WAS transfered. 30026 */ 30027 strt_blk_num = xp->xb_blkno + 30028 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30029 30030 /* 30031 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30032 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30033 * the disk mutex here. 30034 */ 30035 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30036 strt_blk_num, num_blks_not_xfered); 30037 30038 if (rval == 0) { 30039 30040 /* 30041 * Success. 30042 * 30043 * Adjust things if there are still more blocks to be 30044 * transfered. 30045 */ 30046 xp->xb_dma_resid = pkt->pkt_resid; 30047 pkt->pkt_resid = 0; 30048 30049 return (1); 30050 } 30051 30052 /* 30053 * There's really only one possible return value from 30054 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30055 * returns NULL. 30056 */ 30057 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30058 30059 bp->b_resid = bp->b_bcount; 30060 bp->b_flags |= B_ERROR; 30061 30062 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30063 "Error setting up next portion of DMA transfer\n"); 30064 30065 return (0); 30066 } 30067 #endif 30068 30069 /* 30070 * Function: sd_panic_for_res_conflict 30071 * 30072 * Description: Call panic with a string formated with "Reservation Conflict" 30073 * and a human readable identifier indicating the SD instance 30074 * that experienced the reservation conflict. 30075 * 30076 * Arguments: un - pointer to the soft state struct for the instance. 30077 * 30078 * Context: may execute in interrupt context. 30079 */ 30080 30081 #define SD_RESV_CONFLICT_FMT_LEN 40 30082 void 30083 sd_panic_for_res_conflict(struct sd_lun *un) 30084 { 30085 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30086 char path_str[MAXPATHLEN]; 30087 30088 (void) snprintf(panic_str, sizeof (panic_str), 30089 "Reservation Conflict\nDisk: %s", 30090 ddi_pathname(SD_DEVINFO(un), path_str)); 30091 30092 panic(panic_str); 30093 } 30094 30095 /* 30096 * Note: The following sd_faultinjection_ioctl( ) routines implement 30097 * driver support for handling fault injection for error analysis 30098 * causing faults in multiple layers of the driver. 30099 * 30100 */ 30101 30102 #ifdef SD_FAULT_INJECTION 30103 static uint_t sd_fault_injection_on = 0; 30104 30105 /* 30106 * Function: sd_faultinjection_ioctl() 30107 * 30108 * Description: This routine is the driver entry point for handling 30109 * faultinjection ioctls to inject errors into the 30110 * layer model 30111 * 30112 * Arguments: cmd - the ioctl cmd recieved 30113 * arg - the arguments from user and returns 30114 */ 30115 30116 static void 30117 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30118 30119 uint_t i; 30120 uint_t rval; 30121 30122 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30123 30124 mutex_enter(SD_MUTEX(un)); 30125 30126 switch (cmd) { 30127 case SDIOCRUN: 30128 /* Allow pushed faults to be injected */ 30129 SD_INFO(SD_LOG_SDTEST, un, 30130 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30131 30132 sd_fault_injection_on = 1; 30133 30134 SD_INFO(SD_LOG_IOERR, un, 30135 "sd_faultinjection_ioctl: run finished\n"); 30136 break; 30137 30138 case SDIOCSTART: 30139 /* Start Injection Session */ 30140 SD_INFO(SD_LOG_SDTEST, un, 30141 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30142 30143 sd_fault_injection_on = 0; 30144 un->sd_injection_mask = 0xFFFFFFFF; 30145 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30146 un->sd_fi_fifo_pkt[i] = NULL; 30147 un->sd_fi_fifo_xb[i] = NULL; 30148 un->sd_fi_fifo_un[i] = NULL; 30149 un->sd_fi_fifo_arq[i] = NULL; 30150 } 30151 un->sd_fi_fifo_start = 0; 30152 un->sd_fi_fifo_end = 0; 30153 30154 mutex_enter(&(un->un_fi_mutex)); 30155 un->sd_fi_log[0] = '\0'; 30156 un->sd_fi_buf_len = 0; 30157 mutex_exit(&(un->un_fi_mutex)); 30158 30159 SD_INFO(SD_LOG_IOERR, un, 30160 "sd_faultinjection_ioctl: start finished\n"); 30161 break; 30162 30163 case SDIOCSTOP: 30164 /* Stop Injection Session */ 30165 SD_INFO(SD_LOG_SDTEST, un, 30166 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30167 sd_fault_injection_on = 0; 30168 un->sd_injection_mask = 0x0; 30169 30170 /* Empty stray or unuseds structs from fifo */ 30171 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30172 if (un->sd_fi_fifo_pkt[i] != NULL) { 30173 kmem_free(un->sd_fi_fifo_pkt[i], 30174 sizeof (struct sd_fi_pkt)); 30175 } 30176 if (un->sd_fi_fifo_xb[i] != NULL) { 30177 kmem_free(un->sd_fi_fifo_xb[i], 30178 sizeof (struct sd_fi_xb)); 30179 } 30180 if (un->sd_fi_fifo_un[i] != NULL) { 30181 kmem_free(un->sd_fi_fifo_un[i], 30182 sizeof (struct sd_fi_un)); 30183 } 30184 if (un->sd_fi_fifo_arq[i] != NULL) { 30185 kmem_free(un->sd_fi_fifo_arq[i], 30186 sizeof (struct sd_fi_arq)); 30187 } 30188 un->sd_fi_fifo_pkt[i] = NULL; 30189 un->sd_fi_fifo_un[i] = NULL; 30190 un->sd_fi_fifo_xb[i] = NULL; 30191 un->sd_fi_fifo_arq[i] = NULL; 30192 } 30193 un->sd_fi_fifo_start = 0; 30194 un->sd_fi_fifo_end = 0; 30195 30196 SD_INFO(SD_LOG_IOERR, un, 30197 "sd_faultinjection_ioctl: stop finished\n"); 30198 break; 30199 30200 case SDIOCINSERTPKT: 30201 /* Store a packet struct to be pushed onto fifo */ 30202 SD_INFO(SD_LOG_SDTEST, un, 30203 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30204 30205 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30206 30207 sd_fault_injection_on = 0; 30208 30209 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30210 if (un->sd_fi_fifo_pkt[i] != NULL) { 30211 kmem_free(un->sd_fi_fifo_pkt[i], 30212 sizeof (struct sd_fi_pkt)); 30213 } 30214 if (arg != NULL) { 30215 un->sd_fi_fifo_pkt[i] = 30216 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30217 if (un->sd_fi_fifo_pkt[i] == NULL) { 30218 /* Alloc failed don't store anything */ 30219 break; 30220 } 30221 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30222 sizeof (struct sd_fi_pkt), 0); 30223 if (rval == -1) { 30224 kmem_free(un->sd_fi_fifo_pkt[i], 30225 sizeof (struct sd_fi_pkt)); 30226 un->sd_fi_fifo_pkt[i] = NULL; 30227 } 30228 } else { 30229 SD_INFO(SD_LOG_IOERR, un, 30230 "sd_faultinjection_ioctl: pkt null\n"); 30231 } 30232 break; 30233 30234 case SDIOCINSERTXB: 30235 /* Store a xb struct to be pushed onto fifo */ 30236 SD_INFO(SD_LOG_SDTEST, un, 30237 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30238 30239 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30240 30241 sd_fault_injection_on = 0; 30242 30243 if (un->sd_fi_fifo_xb[i] != NULL) { 30244 kmem_free(un->sd_fi_fifo_xb[i], 30245 sizeof (struct sd_fi_xb)); 30246 un->sd_fi_fifo_xb[i] = NULL; 30247 } 30248 if (arg != NULL) { 30249 un->sd_fi_fifo_xb[i] = 30250 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30251 if (un->sd_fi_fifo_xb[i] == NULL) { 30252 /* Alloc failed don't store anything */ 30253 break; 30254 } 30255 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30256 sizeof (struct sd_fi_xb), 0); 30257 30258 if (rval == -1) { 30259 kmem_free(un->sd_fi_fifo_xb[i], 30260 sizeof (struct sd_fi_xb)); 30261 un->sd_fi_fifo_xb[i] = NULL; 30262 } 30263 } else { 30264 SD_INFO(SD_LOG_IOERR, un, 30265 "sd_faultinjection_ioctl: xb null\n"); 30266 } 30267 break; 30268 30269 case SDIOCINSERTUN: 30270 /* Store a un struct to be pushed onto fifo */ 30271 SD_INFO(SD_LOG_SDTEST, un, 30272 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30273 30274 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30275 30276 sd_fault_injection_on = 0; 30277 30278 if (un->sd_fi_fifo_un[i] != NULL) { 30279 kmem_free(un->sd_fi_fifo_un[i], 30280 sizeof (struct sd_fi_un)); 30281 un->sd_fi_fifo_un[i] = NULL; 30282 } 30283 if (arg != NULL) { 30284 un->sd_fi_fifo_un[i] = 30285 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30286 if (un->sd_fi_fifo_un[i] == NULL) { 30287 /* Alloc failed don't store anything */ 30288 break; 30289 } 30290 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30291 sizeof (struct sd_fi_un), 0); 30292 if (rval == -1) { 30293 kmem_free(un->sd_fi_fifo_un[i], 30294 sizeof (struct sd_fi_un)); 30295 un->sd_fi_fifo_un[i] = NULL; 30296 } 30297 30298 } else { 30299 SD_INFO(SD_LOG_IOERR, un, 30300 "sd_faultinjection_ioctl: un null\n"); 30301 } 30302 30303 break; 30304 30305 case SDIOCINSERTARQ: 30306 /* Store a arq struct to be pushed onto fifo */ 30307 SD_INFO(SD_LOG_SDTEST, un, 30308 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30309 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30310 30311 sd_fault_injection_on = 0; 30312 30313 if (un->sd_fi_fifo_arq[i] != NULL) { 30314 kmem_free(un->sd_fi_fifo_arq[i], 30315 sizeof (struct sd_fi_arq)); 30316 un->sd_fi_fifo_arq[i] = NULL; 30317 } 30318 if (arg != NULL) { 30319 un->sd_fi_fifo_arq[i] = 30320 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30321 if (un->sd_fi_fifo_arq[i] == NULL) { 30322 /* Alloc failed don't store anything */ 30323 break; 30324 } 30325 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30326 sizeof (struct sd_fi_arq), 0); 30327 if (rval == -1) { 30328 kmem_free(un->sd_fi_fifo_arq[i], 30329 sizeof (struct sd_fi_arq)); 30330 un->sd_fi_fifo_arq[i] = NULL; 30331 } 30332 30333 } else { 30334 SD_INFO(SD_LOG_IOERR, un, 30335 "sd_faultinjection_ioctl: arq null\n"); 30336 } 30337 30338 break; 30339 30340 case SDIOCPUSH: 30341 /* Push stored xb, pkt, un, and arq onto fifo */ 30342 sd_fault_injection_on = 0; 30343 30344 if (arg != NULL) { 30345 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30346 if (rval != -1 && 30347 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30348 un->sd_fi_fifo_end += i; 30349 } 30350 } else { 30351 SD_INFO(SD_LOG_IOERR, un, 30352 "sd_faultinjection_ioctl: push arg null\n"); 30353 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30354 un->sd_fi_fifo_end++; 30355 } 30356 } 30357 SD_INFO(SD_LOG_IOERR, un, 30358 "sd_faultinjection_ioctl: push to end=%d\n", 30359 un->sd_fi_fifo_end); 30360 break; 30361 30362 case SDIOCRETRIEVE: 30363 /* Return buffer of log from Injection session */ 30364 SD_INFO(SD_LOG_SDTEST, un, 30365 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30366 30367 sd_fault_injection_on = 0; 30368 30369 mutex_enter(&(un->un_fi_mutex)); 30370 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30371 un->sd_fi_buf_len+1, 0); 30372 mutex_exit(&(un->un_fi_mutex)); 30373 30374 if (rval == -1) { 30375 /* 30376 * arg is possibly invalid setting 30377 * it to NULL for return 30378 */ 30379 arg = NULL; 30380 } 30381 break; 30382 } 30383 30384 mutex_exit(SD_MUTEX(un)); 30385 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30386 " exit\n"); 30387 } 30388 30389 30390 /* 30391 * Function: sd_injection_log() 30392 * 30393 * Description: This routine adds buff to the already existing injection log 30394 * for retrieval via faultinjection_ioctl for use in fault 30395 * detection and recovery 30396 * 30397 * Arguments: buf - the string to add to the log 30398 */ 30399 30400 static void 30401 sd_injection_log(char *buf, struct sd_lun *un) 30402 { 30403 uint_t len; 30404 30405 ASSERT(un != NULL); 30406 ASSERT(buf != NULL); 30407 30408 mutex_enter(&(un->un_fi_mutex)); 30409 30410 len = min(strlen(buf), 255); 30411 /* Add logged value to Injection log to be returned later */ 30412 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30413 uint_t offset = strlen((char *)un->sd_fi_log); 30414 char *destp = (char *)un->sd_fi_log + offset; 30415 int i; 30416 for (i = 0; i < len; i++) { 30417 *destp++ = *buf++; 30418 } 30419 un->sd_fi_buf_len += len; 30420 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30421 } 30422 30423 mutex_exit(&(un->un_fi_mutex)); 30424 } 30425 30426 30427 /* 30428 * Function: sd_faultinjection() 30429 * 30430 * Description: This routine takes the pkt and changes its 30431 * content based on error injection scenerio. 30432 * 30433 * Arguments: pktp - packet to be changed 30434 */ 30435 30436 static void 30437 sd_faultinjection(struct scsi_pkt *pktp) 30438 { 30439 uint_t i; 30440 struct sd_fi_pkt *fi_pkt; 30441 struct sd_fi_xb *fi_xb; 30442 struct sd_fi_un *fi_un; 30443 struct sd_fi_arq *fi_arq; 30444 struct buf *bp; 30445 struct sd_xbuf *xb; 30446 struct sd_lun *un; 30447 30448 ASSERT(pktp != NULL); 30449 30450 /* pull bp xb and un from pktp */ 30451 bp = (struct buf *)pktp->pkt_private; 30452 xb = SD_GET_XBUF(bp); 30453 un = SD_GET_UN(bp); 30454 30455 ASSERT(un != NULL); 30456 30457 mutex_enter(SD_MUTEX(un)); 30458 30459 SD_TRACE(SD_LOG_SDTEST, un, 30460 "sd_faultinjection: entry Injection from sdintr\n"); 30461 30462 /* if injection is off return */ 30463 if (sd_fault_injection_on == 0 || 30464 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30465 mutex_exit(SD_MUTEX(un)); 30466 return; 30467 } 30468 30469 30470 /* take next set off fifo */ 30471 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30472 30473 fi_pkt = un->sd_fi_fifo_pkt[i]; 30474 fi_xb = un->sd_fi_fifo_xb[i]; 30475 fi_un = un->sd_fi_fifo_un[i]; 30476 fi_arq = un->sd_fi_fifo_arq[i]; 30477 30478 30479 /* set variables accordingly */ 30480 /* set pkt if it was on fifo */ 30481 if (fi_pkt != NULL) { 30482 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30483 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30484 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30485 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30486 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30487 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30488 30489 } 30490 30491 /* set xb if it was on fifo */ 30492 if (fi_xb != NULL) { 30493 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30494 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30495 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30496 SD_CONDSET(xb, xb, xb_victim_retry_count, 30497 "xb_victim_retry_count"); 30498 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30499 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30500 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30501 30502 /* copy in block data from sense */ 30503 if (fi_xb->xb_sense_data[0] != -1) { 30504 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30505 SENSE_LENGTH); 30506 } 30507 30508 /* copy in extended sense codes */ 30509 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30510 "es_code"); 30511 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30512 "es_key"); 30513 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30514 "es_add_code"); 30515 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30516 es_qual_code, "es_qual_code"); 30517 } 30518 30519 /* set un if it was on fifo */ 30520 if (fi_un != NULL) { 30521 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30522 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30523 SD_CONDSET(un, un, un_reset_retry_count, 30524 "un_reset_retry_count"); 30525 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30526 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30527 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30528 SD_CONDSET(un, un, un_f_geometry_is_valid, 30529 "un_f_geometry_is_valid"); 30530 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30531 "un_f_allow_bus_device_reset"); 30532 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30533 30534 } 30535 30536 /* copy in auto request sense if it was on fifo */ 30537 if (fi_arq != NULL) { 30538 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30539 } 30540 30541 /* free structs */ 30542 if (un->sd_fi_fifo_pkt[i] != NULL) { 30543 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30544 } 30545 if (un->sd_fi_fifo_xb[i] != NULL) { 30546 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30547 } 30548 if (un->sd_fi_fifo_un[i] != NULL) { 30549 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30550 } 30551 if (un->sd_fi_fifo_arq[i] != NULL) { 30552 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30553 } 30554 30555 /* 30556 * kmem_free does not gurantee to set to NULL 30557 * since we uses these to determine if we set 30558 * values or not lets confirm they are always 30559 * NULL after free 30560 */ 30561 un->sd_fi_fifo_pkt[i] = NULL; 30562 un->sd_fi_fifo_un[i] = NULL; 30563 un->sd_fi_fifo_xb[i] = NULL; 30564 un->sd_fi_fifo_arq[i] = NULL; 30565 30566 un->sd_fi_fifo_start++; 30567 30568 mutex_exit(SD_MUTEX(un)); 30569 30570 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30571 } 30572 30573 #endif /* SD_FAULT_INJECTION */ 30574 30575 /* 30576 * This routine is invoked in sd_unit_attach(). Before calling it, the 30577 * properties in conf file should be processed already, and "hotpluggable" 30578 * property was processed also. 30579 * 30580 * The sd driver distinguishes 3 different type of devices: removable media, 30581 * non-removable media, and hotpluggable. Below the differences are defined: 30582 * 30583 * 1. Device ID 30584 * 30585 * The device ID of a device is used to identify this device. Refer to 30586 * ddi_devid_register(9F). 30587 * 30588 * For a non-removable media disk device which can provide 0x80 or 0x83 30589 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30590 * device ID is created to identify this device. For other non-removable 30591 * media devices, a default device ID is created only if this device has 30592 * at least 2 alter cylinders. Otherwise, this device has no devid. 30593 * 30594 * ------------------------------------------------------- 30595 * removable media hotpluggable | Can Have Device ID 30596 * ------------------------------------------------------- 30597 * false false | Yes 30598 * false true | Yes 30599 * true x | No 30600 * ------------------------------------------------------ 30601 * 30602 * 30603 * 2. SCSI group 4 commands 30604 * 30605 * In SCSI specs, only some commands in group 4 command set can use 30606 * 8-byte addresses that can be used to access >2TB storage spaces. 30607 * Other commands have no such capability. Without supporting group4, 30608 * it is impossible to make full use of storage spaces of a disk with 30609 * capacity larger than 2TB. 30610 * 30611 * ----------------------------------------------- 30612 * removable media hotpluggable LP64 | Group 30613 * ----------------------------------------------- 30614 * false false false | 1 30615 * false false true | 4 30616 * false true false | 1 30617 * false true true | 4 30618 * true x x | 5 30619 * ----------------------------------------------- 30620 * 30621 * 30622 * 3. Check for VTOC Label 30623 * 30624 * If a direct-access disk has no EFI label, sd will check if it has a 30625 * valid VTOC label. Now, sd also does that check for removable media 30626 * and hotpluggable devices. 30627 * 30628 * -------------------------------------------------------------- 30629 * Direct-Access removable media hotpluggable | Check Label 30630 * ------------------------------------------------------------- 30631 * false false false | No 30632 * false false true | No 30633 * false true false | Yes 30634 * false true true | Yes 30635 * true x x | Yes 30636 * -------------------------------------------------------------- 30637 * 30638 * 30639 * 4. Building default VTOC label 30640 * 30641 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30642 * If those devices have no valid VTOC label, sd(7d) will attempt to 30643 * create default VTOC for them. Currently sd creates default VTOC label 30644 * for all devices on x86 platform (VTOC_16), but only for removable 30645 * media devices on SPARC (VTOC_8). 30646 * 30647 * ----------------------------------------------------------- 30648 * removable media hotpluggable platform | Default Label 30649 * ----------------------------------------------------------- 30650 * false false sparc | No 30651 * false true x86 | Yes 30652 * false true sparc | Yes 30653 * true x x | Yes 30654 * ---------------------------------------------------------- 30655 * 30656 * 30657 * 5. Supported blocksizes of target devices 30658 * 30659 * Sd supports non-512-byte blocksize for removable media devices only. 30660 * For other devices, only 512-byte blocksize is supported. This may be 30661 * changed in near future because some RAID devices require non-512-byte 30662 * blocksize 30663 * 30664 * ----------------------------------------------------------- 30665 * removable media hotpluggable | non-512-byte blocksize 30666 * ----------------------------------------------------------- 30667 * false false | No 30668 * false true | No 30669 * true x | Yes 30670 * ----------------------------------------------------------- 30671 * 30672 * 30673 * 6. Automatic mount & unmount (i.e. vold) 30674 * 30675 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30676 * if a device is removable media device. It return 1 for removable media 30677 * devices, and 0 for others. 30678 * 30679 * Vold treats a device as removable one only if DKIOREMOVABLE returns 1. 30680 * And it does automounting only for removable media devices. In order to 30681 * preserve users' experience and let vold continue to do automounting for 30682 * USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk 30683 * devices. 30684 * 30685 * ------------------------------------------------------ 30686 * removable media hotpluggable | automatic mount 30687 * ------------------------------------------------------ 30688 * false false | No 30689 * false true | Yes 30690 * true x | Yes 30691 * ------------------------------------------------------ 30692 * 30693 * 30694 * 7. fdisk partition management 30695 * 30696 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30697 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30698 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30699 * fdisk partitions on both x86 and SPARC platform. 30700 * 30701 * ----------------------------------------------------------- 30702 * platform removable media USB/1394 | fdisk supported 30703 * ----------------------------------------------------------- 30704 * x86 X X | true 30705 * ------------------------------------------------------------ 30706 * sparc X X | false 30707 * ------------------------------------------------------------ 30708 * 30709 * 30710 * 8. MBOOT/MBR 30711 * 30712 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30713 * read/write mboot for removable media devices on sparc platform. 30714 * 30715 * ----------------------------------------------------------- 30716 * platform removable media USB/1394 | mboot supported 30717 * ----------------------------------------------------------- 30718 * x86 X X | true 30719 * ------------------------------------------------------------ 30720 * sparc false false | false 30721 * sparc false true | true 30722 * sparc true false | true 30723 * sparc true true | true 30724 * ------------------------------------------------------------ 30725 * 30726 * 30727 * 9. error handling during opening device 30728 * 30729 * If failed to open a disk device, an errno is returned. For some kinds 30730 * of errors, different errno is returned depending on if this device is 30731 * a removable media device. This brings USB/1394 hard disks in line with 30732 * expected hard disk behavior. It is not expected that this breaks any 30733 * application. 30734 * 30735 * ------------------------------------------------------ 30736 * removable media hotpluggable | errno 30737 * ------------------------------------------------------ 30738 * false false | EIO 30739 * false true | EIO 30740 * true x | ENXIO 30741 * ------------------------------------------------------ 30742 * 30743 * 30744 * 10. off-by-1 workaround (bug 1175930, and 4996920) (x86 only) 30745 * 30746 * [ this is a bit of very ugly history, soon to be removed ] 30747 * 30748 * SCSI READ_CAPACITY command returns the last valid logical block number 30749 * which starts from 0. So real capacity is larger than the returned 30750 * value by 1. However, because scdk.c (which was EOL'ed) directly used 30751 * the logical block number as capacity of disk devices, off-by-1 work- 30752 * around was applied. This workaround causes fixed SCSI disk to loss a 30753 * sector on x86 platform, and precludes exchanging fixed hard disks 30754 * between sparc and x86. 30755 * 30756 * ------------------------------------------------------ 30757 * removable media hotplug | Off-by-1 works 30758 * ------------------------------------------------------- 30759 * false false | Yes 30760 * false true | No 30761 * true false | No 30762 * true true | No 30763 * ------------------------------------------------------ 30764 * 30765 * 30766 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30767 * 30768 * These IOCTLs are applicable only to removable media devices. 30769 * 30770 * ----------------------------------------------------------- 30771 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30772 * ----------------------------------------------------------- 30773 * false false | No 30774 * false true | No 30775 * true x | Yes 30776 * ----------------------------------------------------------- 30777 * 30778 * 30779 * 12. Kstats for partitions 30780 * 30781 * sd creates partition kstat for non-removable media devices. USB and 30782 * Firewire hard disks now have partition kstats 30783 * 30784 * ------------------------------------------------------ 30785 * removable media hotplugable | kstat 30786 * ------------------------------------------------------ 30787 * false false | Yes 30788 * false true | Yes 30789 * true x | No 30790 * ------------------------------------------------------ 30791 * 30792 * 30793 * 13. Removable media & hotpluggable properties 30794 * 30795 * Sd driver creates a "removable-media" property for removable media 30796 * devices. Parent nexus drivers create a "hotpluggable" property if 30797 * it supports hotplugging. 30798 * 30799 * --------------------------------------------------------------------- 30800 * removable media hotpluggable | "removable-media" " hotpluggable" 30801 * --------------------------------------------------------------------- 30802 * false false | No No 30803 * false true | No Yes 30804 * true false | Yes No 30805 * true true | Yes Yes 30806 * --------------------------------------------------------------------- 30807 * 30808 * 30809 * 14. Power Management 30810 * 30811 * sd only power manages removable media devices or devices that support 30812 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30813 * 30814 * A parent nexus that supports hotplugging can also set "pm-capable" 30815 * if the disk can be power managed. 30816 * 30817 * ------------------------------------------------------------ 30818 * removable media hotpluggable pm-capable | power manage 30819 * ------------------------------------------------------------ 30820 * false false false | No 30821 * false false true | Yes 30822 * false true false | No 30823 * false true true | Yes 30824 * true x x | Yes 30825 * ------------------------------------------------------------ 30826 * 30827 * USB and firewire hard disks can now be power managed independently 30828 * of the framebuffer 30829 * 30830 * 30831 * 15. Support for USB disks with capacity larger than 1TB 30832 * 30833 * Currently, sd doesn't permit a fixed disk device with capacity 30834 * larger than 1TB to be used in a 32-bit operating system environment. 30835 * However, sd doesn't do that for removable media devices. Instead, it 30836 * assumes that removable media devices cannot have a capacity larger 30837 * than 1TB. Therefore, using those devices on 32-bit system is partially 30838 * supported, which can cause some unexpected results. 30839 * 30840 * --------------------------------------------------------------------- 30841 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30842 * --------------------------------------------------------------------- 30843 * false false | true | no 30844 * false true | true | no 30845 * true false | true | Yes 30846 * true true | true | Yes 30847 * --------------------------------------------------------------------- 30848 * 30849 * 30850 * 16. Check write-protection at open time 30851 * 30852 * When a removable media device is being opened for writing without NDELAY 30853 * flag, sd will check if this device is writable. If attempting to open 30854 * without NDELAY flag a write-protected device, this operation will abort. 30855 * 30856 * ------------------------------------------------------------ 30857 * removable media USB/1394 | WP Check 30858 * ------------------------------------------------------------ 30859 * false false | No 30860 * false true | No 30861 * true false | Yes 30862 * true true | Yes 30863 * ------------------------------------------------------------ 30864 * 30865 * 30866 * 17. syslog when corrupted VTOC is encountered 30867 * 30868 * Currently, if an invalid VTOC is encountered, sd only print syslog 30869 * for fixed SCSI disks. 30870 * ------------------------------------------------------------ 30871 * removable media USB/1394 | print syslog 30872 * ------------------------------------------------------------ 30873 * false false | Yes 30874 * false true | No 30875 * true false | No 30876 * true true | No 30877 * ------------------------------------------------------------ 30878 */ 30879 static void 30880 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30881 { 30882 int pm_capable_prop; 30883 30884 ASSERT(un->un_sd); 30885 ASSERT(un->un_sd->sd_inq); 30886 30887 #if defined(_SUNOS_VTOC_16) 30888 /* 30889 * For VTOC_16 devices, the default label will be created for all 30890 * devices. (see sd_build_default_label) 30891 */ 30892 un->un_f_default_vtoc_supported = TRUE; 30893 #endif 30894 30895 if (un->un_sd->sd_inq->inq_rmb) { 30896 /* 30897 * The media of this device is removable. And for this kind 30898 * of devices, it is possible to change medium after openning 30899 * devices. Thus we should support this operation. 30900 */ 30901 un->un_f_has_removable_media = TRUE; 30902 30903 #if defined(_SUNOS_VTOC_8) 30904 /* 30905 * Note: currently, for VTOC_8 devices, default label is 30906 * created for removable and hotpluggable devices only. 30907 */ 30908 un->un_f_default_vtoc_supported = TRUE; 30909 #endif 30910 /* 30911 * support non-512-byte blocksize of removable media devices 30912 */ 30913 un->un_f_non_devbsize_supported = TRUE; 30914 30915 /* 30916 * Assume that all removable media devices support DOOR_LOCK 30917 */ 30918 un->un_f_doorlock_supported = TRUE; 30919 30920 /* 30921 * For a removable media device, it is possible to be opened 30922 * with NDELAY flag when there is no media in drive, in this 30923 * case we don't care if device is writable. But if without 30924 * NDELAY flag, we need to check if media is write-protected. 30925 */ 30926 un->un_f_chk_wp_open = TRUE; 30927 30928 /* 30929 * need to start a SCSI watch thread to monitor media state, 30930 * when media is being inserted or ejected, notify syseventd. 30931 */ 30932 un->un_f_monitor_media_state = TRUE; 30933 30934 /* 30935 * Some devices don't support START_STOP_UNIT command. 30936 * Therefore, we'd better check if a device supports it 30937 * before sending it. 30938 */ 30939 un->un_f_check_start_stop = TRUE; 30940 30941 /* 30942 * support eject media ioctl: 30943 * FDEJECT, DKIOCEJECT, CDROMEJECT 30944 */ 30945 un->un_f_eject_media_supported = TRUE; 30946 30947 /* 30948 * Because many removable-media devices don't support 30949 * LOG_SENSE, we couldn't use this command to check if 30950 * a removable media device support power-management. 30951 * We assume that they support power-management via 30952 * START_STOP_UNIT command and can be spun up and down 30953 * without limitations. 30954 */ 30955 un->un_f_pm_supported = TRUE; 30956 30957 /* 30958 * Need to create a zero length (Boolean) property 30959 * removable-media for the removable media devices. 30960 * Note that the return value of the property is not being 30961 * checked, since if unable to create the property 30962 * then do not want the attach to fail altogether. Consistent 30963 * with other property creation in attach. 30964 */ 30965 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30966 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30967 30968 } else { 30969 /* 30970 * create device ID for device 30971 */ 30972 un->un_f_devid_supported = TRUE; 30973 30974 /* 30975 * Spin up non-removable-media devices once it is attached 30976 */ 30977 un->un_f_attach_spinup = TRUE; 30978 30979 /* 30980 * According to SCSI specification, Sense data has two kinds of 30981 * format: fixed format, and descriptor format. At present, we 30982 * don't support descriptor format sense data for removable 30983 * media. 30984 */ 30985 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30986 un->un_f_descr_format_supported = TRUE; 30987 } 30988 30989 /* 30990 * kstats are created only for non-removable media devices. 30991 * 30992 * Set this in sd.conf to 0 in order to disable kstats. The 30993 * default is 1, so they are enabled by default. 30994 */ 30995 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30996 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30997 "enable-partition-kstats", 1)); 30998 30999 /* 31000 * Check if HBA has set the "pm-capable" property. 31001 * If "pm-capable" exists and is non-zero then we can 31002 * power manage the device without checking the start/stop 31003 * cycle count log sense page. 31004 * 31005 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31006 * then we should not power manage the device. 31007 * 31008 * If "pm-capable" doesn't exist then pm_capable_prop will 31009 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31010 * sd will check the start/stop cycle count log sense page 31011 * and power manage the device if the cycle count limit has 31012 * not been exceeded. 31013 */ 31014 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31015 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31016 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31017 un->un_f_log_sense_supported = TRUE; 31018 } else { 31019 /* 31020 * pm-capable property exists. 31021 * 31022 * Convert "TRUE" values for pm_capable_prop to 31023 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31024 * later. "TRUE" values are any values except 31025 * SD_PM_CAPABLE_FALSE (0) and 31026 * SD_PM_CAPABLE_UNDEFINED (-1) 31027 */ 31028 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31029 un->un_f_log_sense_supported = FALSE; 31030 } else { 31031 un->un_f_pm_supported = TRUE; 31032 } 31033 31034 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31035 "sd_unit_attach: un:0x%p pm-capable " 31036 "property set to %d.\n", un, un->un_f_pm_supported); 31037 } 31038 } 31039 31040 if (un->un_f_is_hotpluggable) { 31041 #if defined(_SUNOS_VTOC_8) 31042 /* 31043 * Note: currently, for VTOC_8 devices, default label is 31044 * created for removable and hotpluggable devices only. 31045 */ 31046 un->un_f_default_vtoc_supported = TRUE; 31047 #endif 31048 31049 /* 31050 * Temporarily, let hotpluggable devices pretend to be 31051 * removable-media devices for vold. 31052 */ 31053 un->un_f_monitor_media_state = TRUE; 31054 31055 un->un_f_check_start_stop = TRUE; 31056 31057 } 31058 31059 /* 31060 * By default, only DIRECT ACCESS devices and CDs will have Sun 31061 * labels. 31062 */ 31063 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31064 (un->un_sd->sd_inq->inq_rmb)) { 31065 /* 31066 * Direct access devices have disk label 31067 */ 31068 un->un_f_vtoc_label_supported = TRUE; 31069 } 31070 31071 /* 31072 * Fdisk partitions are supported for all direct access devices on 31073 * x86 platform, and just for removable media and hotpluggable 31074 * devices on SPARC platform. Later, we will set the following flag 31075 * to FALSE if current device is not removable media or hotpluggable 31076 * device and if sd works on SAPRC platform. 31077 */ 31078 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31079 un->un_f_mboot_supported = TRUE; 31080 } 31081 31082 if (!un->un_f_is_hotpluggable && 31083 !un->un_sd->sd_inq->inq_rmb) { 31084 31085 #if defined(_SUNOS_VTOC_8) 31086 /* 31087 * Don't support fdisk on fixed disk 31088 */ 31089 un->un_f_mboot_supported = FALSE; 31090 #endif 31091 31092 /* 31093 * Fixed disk support SYNC CACHE 31094 */ 31095 un->un_f_sync_cache_supported = TRUE; 31096 31097 /* 31098 * For fixed disk, if its VTOC is not valid, we will write 31099 * errlog into system log 31100 */ 31101 if (un->un_f_vtoc_label_supported) 31102 un->un_f_vtoc_errlog_supported = TRUE; 31103 } 31104 } 31105