1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * SCSI disk target driver. 30 */ 31 #include <sys/scsi/scsi.h> 32 #include <sys/dkbad.h> 33 #include <sys/dklabel.h> 34 #include <sys/dkio.h> 35 #include <sys/fdio.h> 36 #include <sys/cdio.h> 37 #include <sys/mhd.h> 38 #include <sys/vtoc.h> 39 #include <sys/dktp/fdisk.h> 40 #include <sys/file.h> 41 #include <sys/stat.h> 42 #include <sys/kstat.h> 43 #include <sys/vtrace.h> 44 #include <sys/note.h> 45 #include <sys/thread.h> 46 #include <sys/proc.h> 47 #include <sys/efi_partition.h> 48 #include <sys/var.h> 49 #include <sys/aio_req.h> 50 51 #ifdef __lock_lint 52 #define _LP64 53 #define __amd64 54 #endif 55 56 #if (defined(__fibre)) 57 /* Note: is there a leadville version of the following? */ 58 #include <sys/fc4/fcal_linkapp.h> 59 #endif 60 #include <sys/taskq.h> 61 #include <sys/uuid.h> 62 #include <sys/byteorder.h> 63 #include <sys/sdt.h> 64 65 #include "sd_xbuf.h" 66 67 #include <sys/scsi/targets/sddef.h> 68 69 70 /* 71 * Loadable module info. 72 */ 73 #if (defined(__fibre)) 74 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 75 char _depends_on[] = "misc/scsi drv/fcp"; 76 #else 77 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 78 char _depends_on[] = "misc/scsi"; 79 #endif 80 81 /* 82 * Define the interconnect type, to allow the driver to distinguish 83 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 84 * 85 * This is really for backward compatability. In the future, the driver 86 * should actually check the "interconnect-type" property as reported by 87 * the HBA; however at present this property is not defined by all HBAs, 88 * so we will use this #define (1) to permit the driver to run in 89 * backward-compatability mode; and (2) to print a notification message 90 * if an FC HBA does not support the "interconnect-type" property. The 91 * behavior of the driver will be to assume parallel SCSI behaviors unless 92 * the "interconnect-type" property is defined by the HBA **AND** has a 93 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 94 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 95 * Channel behaviors (as per the old ssd). (Note that the 96 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 97 * will result in the driver assuming parallel SCSI behaviors.) 98 * 99 * (see common/sys/scsi/impl/services.h) 100 * 101 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 102 * since some FC HBAs may already support that, and there is some code in 103 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 104 * default would confuse that code, and besides things should work fine 105 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 106 * "interconnect_type" property. 107 */ 108 #if (defined(__fibre)) 109 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 110 #else 111 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 112 #endif 113 114 /* 115 * The name of the driver, established from the module name in _init. 116 */ 117 static char *sd_label = NULL; 118 119 /* 120 * Driver name is unfortunately prefixed on some driver.conf properties. 121 */ 122 #if (defined(__fibre)) 123 #define sd_max_xfer_size ssd_max_xfer_size 124 #define sd_config_list ssd_config_list 125 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 126 static char *sd_config_list = "ssd-config-list"; 127 #else 128 static char *sd_max_xfer_size = "sd_max_xfer_size"; 129 static char *sd_config_list = "sd-config-list"; 130 #endif 131 132 /* 133 * Driver global variables 134 */ 135 136 #if (defined(__fibre)) 137 /* 138 * These #defines are to avoid namespace collisions that occur because this 139 * code is currently used to compile two seperate driver modules: sd and ssd. 140 * All global variables need to be treated this way (even if declared static) 141 * in order to allow the debugger to resolve the names properly. 142 * It is anticipated that in the near future the ssd module will be obsoleted, 143 * at which time this namespace issue should go away. 144 */ 145 #define sd_state ssd_state 146 #define sd_io_time ssd_io_time 147 #define sd_failfast_enable ssd_failfast_enable 148 #define sd_ua_retry_count ssd_ua_retry_count 149 #define sd_report_pfa ssd_report_pfa 150 #define sd_max_throttle ssd_max_throttle 151 #define sd_min_throttle ssd_min_throttle 152 #define sd_rot_delay ssd_rot_delay 153 154 #define sd_retry_on_reservation_conflict \ 155 ssd_retry_on_reservation_conflict 156 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 157 #define sd_resv_conflict_name ssd_resv_conflict_name 158 159 #define sd_component_mask ssd_component_mask 160 #define sd_level_mask ssd_level_mask 161 #define sd_debug_un ssd_debug_un 162 #define sd_error_level ssd_error_level 163 164 #define sd_xbuf_active_limit ssd_xbuf_active_limit 165 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 166 167 #define sd_tr ssd_tr 168 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 169 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 170 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 171 #define sd_check_media_time ssd_check_media_time 172 #define sd_wait_cmds_complete ssd_wait_cmds_complete 173 #define sd_label_mutex ssd_label_mutex 174 #define sd_detach_mutex ssd_detach_mutex 175 #define sd_log_buf ssd_log_buf 176 #define sd_log_mutex ssd_log_mutex 177 178 #define sd_disk_table ssd_disk_table 179 #define sd_disk_table_size ssd_disk_table_size 180 #define sd_sense_mutex ssd_sense_mutex 181 #define sd_cdbtab ssd_cdbtab 182 183 #define sd_cb_ops ssd_cb_ops 184 #define sd_ops ssd_ops 185 #define sd_additional_codes ssd_additional_codes 186 187 #define sd_minor_data ssd_minor_data 188 #define sd_minor_data_efi ssd_minor_data_efi 189 190 #define sd_tq ssd_tq 191 #define sd_wmr_tq ssd_wmr_tq 192 #define sd_taskq_name ssd_taskq_name 193 #define sd_wmr_taskq_name ssd_wmr_taskq_name 194 #define sd_taskq_minalloc ssd_taskq_minalloc 195 #define sd_taskq_maxalloc ssd_taskq_maxalloc 196 197 #define sd_dump_format_string ssd_dump_format_string 198 199 #define sd_iostart_chain ssd_iostart_chain 200 #define sd_iodone_chain ssd_iodone_chain 201 202 #define sd_pm_idletime ssd_pm_idletime 203 204 #define sd_force_pm_supported ssd_force_pm_supported 205 206 #define sd_dtype_optical_bind ssd_dtype_optical_bind 207 208 #endif 209 210 211 #ifdef SDDEBUG 212 int sd_force_pm_supported = 0; 213 #endif /* SDDEBUG */ 214 215 void *sd_state = NULL; 216 int sd_io_time = SD_IO_TIME; 217 int sd_failfast_enable = 1; 218 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 219 int sd_report_pfa = 1; 220 int sd_max_throttle = SD_MAX_THROTTLE; 221 int sd_min_throttle = SD_MIN_THROTTLE; 222 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 223 int sd_qfull_throttle_enable = TRUE; 224 225 int sd_retry_on_reservation_conflict = 1; 226 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 227 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 228 229 static int sd_dtype_optical_bind = -1; 230 231 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 232 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 233 234 /* 235 * Global data for debug logging. To enable debug printing, sd_component_mask 236 * and sd_level_mask should be set to the desired bit patterns as outlined in 237 * sddef.h. 238 */ 239 uint_t sd_component_mask = 0x0; 240 uint_t sd_level_mask = 0x0; 241 struct sd_lun *sd_debug_un = NULL; 242 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 243 244 /* Note: these may go away in the future... */ 245 static uint32_t sd_xbuf_active_limit = 512; 246 static uint32_t sd_xbuf_reserve_limit = 16; 247 248 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 249 250 /* 251 * Timer value used to reset the throttle after it has been reduced 252 * (typically in response to TRAN_BUSY or STATUS_QFULL) 253 */ 254 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 255 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 256 257 /* 258 * Interval value associated with the media change scsi watch. 259 */ 260 static int sd_check_media_time = 3000000; 261 262 /* 263 * Wait value used for in progress operations during a DDI_SUSPEND 264 */ 265 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 266 267 /* 268 * sd_label_mutex protects a static buffer used in the disk label 269 * component of the driver 270 */ 271 static kmutex_t sd_label_mutex; 272 273 /* 274 * sd_detach_mutex protects un_layer_count, un_detach_count, and 275 * un_opens_in_progress in the sd_lun structure. 276 */ 277 static kmutex_t sd_detach_mutex; 278 279 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 280 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 281 282 /* 283 * Global buffer and mutex for debug logging 284 */ 285 static char sd_log_buf[1024]; 286 static kmutex_t sd_log_mutex; 287 288 289 /* 290 * "Smart" Probe Caching structs, globals, #defines, etc. 291 * For parallel scsi and non-self-identify device only. 292 */ 293 294 /* 295 * The following resources and routines are implemented to support 296 * "smart" probing, which caches the scsi_probe() results in an array, 297 * in order to help avoid long probe times. 298 */ 299 struct sd_scsi_probe_cache { 300 struct sd_scsi_probe_cache *next; 301 dev_info_t *pdip; 302 int cache[NTARGETS_WIDE]; 303 }; 304 305 static kmutex_t sd_scsi_probe_cache_mutex; 306 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 307 308 /* 309 * Really we only need protection on the head of the linked list, but 310 * better safe than sorry. 311 */ 312 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 313 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 314 315 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 316 sd_scsi_probe_cache_head)) 317 318 319 /* 320 * Vendor specific data name property declarations 321 */ 322 323 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 324 325 static sd_tunables seagate_properties = { 326 SEAGATE_THROTTLE_VALUE, 327 0, 328 0, 329 0, 330 0, 331 0, 332 0, 333 0, 334 0 335 }; 336 337 338 static sd_tunables fujitsu_properties = { 339 FUJITSU_THROTTLE_VALUE, 340 0, 341 0, 342 0, 343 0, 344 0, 345 0, 346 0, 347 0 348 }; 349 350 static sd_tunables ibm_properties = { 351 IBM_THROTTLE_VALUE, 352 0, 353 0, 354 0, 355 0, 356 0, 357 0, 358 0, 359 0 360 }; 361 362 static sd_tunables purple_properties = { 363 PURPLE_THROTTLE_VALUE, 364 0, 365 0, 366 PURPLE_BUSY_RETRIES, 367 PURPLE_RESET_RETRY_COUNT, 368 PURPLE_RESERVE_RELEASE_TIME, 369 0, 370 0, 371 0 372 }; 373 374 static sd_tunables sve_properties = { 375 SVE_THROTTLE_VALUE, 376 0, 377 0, 378 SVE_BUSY_RETRIES, 379 SVE_RESET_RETRY_COUNT, 380 SVE_RESERVE_RELEASE_TIME, 381 SVE_MIN_THROTTLE_VALUE, 382 SVE_DISKSORT_DISABLED_FLAG, 383 0 384 }; 385 386 static sd_tunables maserati_properties = { 387 0, 388 0, 389 0, 390 0, 391 0, 392 0, 393 0, 394 MASERATI_DISKSORT_DISABLED_FLAG, 395 MASERATI_LUN_RESET_ENABLED_FLAG 396 }; 397 398 static sd_tunables pirus_properties = { 399 PIRUS_THROTTLE_VALUE, 400 0, 401 PIRUS_NRR_COUNT, 402 PIRUS_BUSY_RETRIES, 403 PIRUS_RESET_RETRY_COUNT, 404 0, 405 PIRUS_MIN_THROTTLE_VALUE, 406 PIRUS_DISKSORT_DISABLED_FLAG, 407 PIRUS_LUN_RESET_ENABLED_FLAG 408 }; 409 410 #endif 411 412 #if (defined(__sparc) && !defined(__fibre)) || \ 413 (defined(__i386) || defined(__amd64)) 414 415 416 static sd_tunables elite_properties = { 417 ELITE_THROTTLE_VALUE, 418 0, 419 0, 420 0, 421 0, 422 0, 423 0, 424 0, 425 0 426 }; 427 428 static sd_tunables st31200n_properties = { 429 ST31200N_THROTTLE_VALUE, 430 0, 431 0, 432 0, 433 0, 434 0, 435 0, 436 0, 437 0 438 }; 439 440 #endif /* Fibre or not */ 441 442 static sd_tunables lsi_properties_scsi = { 443 LSI_THROTTLE_VALUE, 444 0, 445 LSI_NOTREADY_RETRIES, 446 0, 447 0, 448 0, 449 0, 450 0, 451 0 452 }; 453 454 static sd_tunables symbios_properties = { 455 SYMBIOS_THROTTLE_VALUE, 456 0, 457 SYMBIOS_NOTREADY_RETRIES, 458 0, 459 0, 460 0, 461 0, 462 0, 463 0 464 }; 465 466 static sd_tunables lsi_properties = { 467 0, 468 0, 469 LSI_NOTREADY_RETRIES, 470 0, 471 0, 472 0, 473 0, 474 0, 475 0 476 }; 477 478 static sd_tunables lsi_oem_properties = { 479 0, 480 0, 481 LSI_OEM_NOTREADY_RETRIES, 482 0, 483 0, 484 0, 485 0, 486 0, 487 0 488 }; 489 490 491 492 #if (defined(SD_PROP_TST)) 493 494 #define SD_TST_CTYPE_VAL CTYPE_CDROM 495 #define SD_TST_THROTTLE_VAL 16 496 #define SD_TST_NOTREADY_VAL 12 497 #define SD_TST_BUSY_VAL 60 498 #define SD_TST_RST_RETRY_VAL 36 499 #define SD_TST_RSV_REL_TIME 60 500 501 static sd_tunables tst_properties = { 502 SD_TST_THROTTLE_VAL, 503 SD_TST_CTYPE_VAL, 504 SD_TST_NOTREADY_VAL, 505 SD_TST_BUSY_VAL, 506 SD_TST_RST_RETRY_VAL, 507 SD_TST_RSV_REL_TIME, 508 0, 509 0, 510 0 511 }; 512 #endif 513 514 /* This is similiar to the ANSI toupper implementation */ 515 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 516 517 /* 518 * Static Driver Configuration Table 519 * 520 * This is the table of disks which need throttle adjustment (or, perhaps 521 * something else as defined by the flags at a future time.) device_id 522 * is a string consisting of concatenated vid (vendor), pid (product/model) 523 * and revision strings as defined in the scsi_inquiry structure. Offsets of 524 * the parts of the string are as defined by the sizes in the scsi_inquiry 525 * structure. Device type is searched as far as the device_id string is 526 * defined. Flags defines which values are to be set in the driver from the 527 * properties list. 528 * 529 * Entries below which begin and end with a "*" are a special case. 530 * These do not have a specific vendor, and the string which follows 531 * can appear anywhere in the 16 byte PID portion of the inquiry data. 532 * 533 * Entries below which begin and end with a " " (blank) are a special 534 * case. The comparison function will treat multiple consecutive blanks 535 * as equivalent to a single blank. For example, this causes a 536 * sd_disk_table entry of " NEC CDROM " to match a device's id string 537 * of "NEC CDROM". 538 * 539 * Note: The MD21 controller type has been obsoleted. 540 * ST318202F is a Legacy device 541 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 542 * made with an FC connection. The entries here are a legacy. 543 */ 544 static sd_disk_config_t sd_disk_table[] = { 545 #if defined(__fibre) || defined(__i386) || defined(__amd64) 546 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 547 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 548 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 549 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 550 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 551 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 552 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 553 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 554 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 555 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 556 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 557 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 558 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 559 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 560 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 561 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 562 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 563 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 564 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 565 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 566 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 567 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 568 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 569 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 570 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 571 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 572 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 573 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 574 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 575 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 576 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 577 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 578 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 579 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 580 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 581 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 582 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 583 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 584 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 585 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 586 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 587 { "SUN T3", SD_CONF_BSET_THROTTLE | 588 SD_CONF_BSET_BSY_RETRY_COUNT| 589 SD_CONF_BSET_RST_RETRIES| 590 SD_CONF_BSET_RSV_REL_TIME, 591 &purple_properties }, 592 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 593 SD_CONF_BSET_BSY_RETRY_COUNT| 594 SD_CONF_BSET_RST_RETRIES| 595 SD_CONF_BSET_RSV_REL_TIME| 596 SD_CONF_BSET_MIN_THROTTLE| 597 SD_CONF_BSET_DISKSORT_DISABLED, 598 &sve_properties }, 599 { "SUN T4", SD_CONF_BSET_THROTTLE | 600 SD_CONF_BSET_BSY_RETRY_COUNT| 601 SD_CONF_BSET_RST_RETRIES| 602 SD_CONF_BSET_RSV_REL_TIME, 603 &purple_properties }, 604 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 605 SD_CONF_BSET_LUN_RESET_ENABLED, 606 &maserati_properties }, 607 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 608 SD_CONF_BSET_NRR_COUNT| 609 SD_CONF_BSET_BSY_RETRY_COUNT| 610 SD_CONF_BSET_RST_RETRIES| 611 SD_CONF_BSET_MIN_THROTTLE| 612 SD_CONF_BSET_DISKSORT_DISABLED| 613 SD_CONF_BSET_LUN_RESET_ENABLED, 614 &pirus_properties }, 615 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 616 SD_CONF_BSET_NRR_COUNT| 617 SD_CONF_BSET_BSY_RETRY_COUNT| 618 SD_CONF_BSET_RST_RETRIES| 619 SD_CONF_BSET_MIN_THROTTLE| 620 SD_CONF_BSET_DISKSORT_DISABLED| 621 SD_CONF_BSET_LUN_RESET_ENABLED, 622 &pirus_properties }, 623 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 624 SD_CONF_BSET_NRR_COUNT| 625 SD_CONF_BSET_BSY_RETRY_COUNT| 626 SD_CONF_BSET_RST_RETRIES| 627 SD_CONF_BSET_MIN_THROTTLE| 628 SD_CONF_BSET_DISKSORT_DISABLED| 629 SD_CONF_BSET_LUN_RESET_ENABLED, 630 &pirus_properties }, 631 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 632 SD_CONF_BSET_NRR_COUNT| 633 SD_CONF_BSET_BSY_RETRY_COUNT| 634 SD_CONF_BSET_RST_RETRIES| 635 SD_CONF_BSET_MIN_THROTTLE| 636 SD_CONF_BSET_DISKSORT_DISABLED| 637 SD_CONF_BSET_LUN_RESET_ENABLED, 638 &pirus_properties }, 639 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 640 SD_CONF_BSET_NRR_COUNT| 641 SD_CONF_BSET_BSY_RETRY_COUNT| 642 SD_CONF_BSET_RST_RETRIES| 643 SD_CONF_BSET_MIN_THROTTLE| 644 SD_CONF_BSET_DISKSORT_DISABLED| 645 SD_CONF_BSET_LUN_RESET_ENABLED, 646 &pirus_properties }, 647 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 648 SD_CONF_BSET_NRR_COUNT| 649 SD_CONF_BSET_BSY_RETRY_COUNT| 650 SD_CONF_BSET_RST_RETRIES| 651 SD_CONF_BSET_MIN_THROTTLE| 652 SD_CONF_BSET_DISKSORT_DISABLED| 653 SD_CONF_BSET_LUN_RESET_ENABLED, 654 &pirus_properties }, 655 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 656 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 657 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 658 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 659 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 660 #endif /* fibre or NON-sparc platforms */ 661 #if ((defined(__sparc) && !defined(__fibre)) ||\ 662 (defined(__i386) || defined(__amd64))) 663 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 664 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 665 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 666 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 667 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 668 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 669 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 670 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 671 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 672 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 673 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 674 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 675 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 676 &symbios_properties }, 677 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 678 &lsi_properties_scsi }, 679 #if defined(__i386) || defined(__amd64) 680 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 681 | SD_CONF_BSET_READSUB_BCD 682 | SD_CONF_BSET_READ_TOC_ADDR_BCD 683 | SD_CONF_BSET_NO_READ_HEADER 684 | SD_CONF_BSET_READ_CD_XD4), NULL }, 685 686 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 687 | SD_CONF_BSET_READSUB_BCD 688 | SD_CONF_BSET_READ_TOC_ADDR_BCD 689 | SD_CONF_BSET_NO_READ_HEADER 690 | SD_CONF_BSET_READ_CD_XD4), NULL }, 691 #endif /* __i386 || __amd64 */ 692 #endif /* sparc NON-fibre or NON-sparc platforms */ 693 694 #if (defined(SD_PROP_TST)) 695 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 696 | SD_CONF_BSET_CTYPE 697 | SD_CONF_BSET_NRR_COUNT 698 | SD_CONF_BSET_FAB_DEVID 699 | SD_CONF_BSET_NOCACHE 700 | SD_CONF_BSET_BSY_RETRY_COUNT 701 | SD_CONF_BSET_PLAYMSF_BCD 702 | SD_CONF_BSET_READSUB_BCD 703 | SD_CONF_BSET_READ_TOC_TRK_BCD 704 | SD_CONF_BSET_READ_TOC_ADDR_BCD 705 | SD_CONF_BSET_NO_READ_HEADER 706 | SD_CONF_BSET_READ_CD_XD4 707 | SD_CONF_BSET_RST_RETRIES 708 | SD_CONF_BSET_RSV_REL_TIME 709 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 710 #endif 711 }; 712 713 static const int sd_disk_table_size = 714 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 715 716 717 /* 718 * Return codes of sd_uselabel(). 719 */ 720 #define SD_LABEL_IS_VALID 0 721 #define SD_LABEL_IS_INVALID 1 722 723 #define SD_INTERCONNECT_PARALLEL 0 724 #define SD_INTERCONNECT_FABRIC 1 725 #define SD_INTERCONNECT_FIBRE 2 726 #define SD_INTERCONNECT_SSA 3 727 #define SD_IS_PARALLEL_SCSI(un) \ 728 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 729 730 /* 731 * Definitions used by device id registration routines 732 */ 733 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 734 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 735 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 736 #define WD_NODE 7 /* the whole disk minor */ 737 738 static kmutex_t sd_sense_mutex = {0}; 739 740 /* 741 * Macros for updates of the driver state 742 */ 743 #define New_state(un, s) \ 744 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 745 #define Restore_state(un) \ 746 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 747 748 static struct sd_cdbinfo sd_cdbtab[] = { 749 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 750 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 751 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 752 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 753 }; 754 755 /* 756 * Specifies the number of seconds that must have elapsed since the last 757 * cmd. has completed for a device to be declared idle to the PM framework. 758 */ 759 static int sd_pm_idletime = 1; 760 761 /* 762 * Internal function prototypes 763 */ 764 765 #if (defined(__fibre)) 766 /* 767 * These #defines are to avoid namespace collisions that occur because this 768 * code is currently used to compile two seperate driver modules: sd and ssd. 769 * All function names need to be treated this way (even if declared static) 770 * in order to allow the debugger to resolve the names properly. 771 * It is anticipated that in the near future the ssd module will be obsoleted, 772 * at which time this ugliness should go away. 773 */ 774 #define sd_log_trace ssd_log_trace 775 #define sd_log_info ssd_log_info 776 #define sd_log_err ssd_log_err 777 #define sdprobe ssdprobe 778 #define sdinfo ssdinfo 779 #define sd_prop_op ssd_prop_op 780 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 781 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 782 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 783 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 784 #define sd_spin_up_unit ssd_spin_up_unit 785 #define sd_enable_descr_sense ssd_enable_descr_sense 786 #define sd_set_mmc_caps ssd_set_mmc_caps 787 #define sd_read_unit_properties ssd_read_unit_properties 788 #define sd_process_sdconf_file ssd_process_sdconf_file 789 #define sd_process_sdconf_table ssd_process_sdconf_table 790 #define sd_sdconf_id_match ssd_sdconf_id_match 791 #define sd_blank_cmp ssd_blank_cmp 792 #define sd_chk_vers1_data ssd_chk_vers1_data 793 #define sd_set_vers1_properties ssd_set_vers1_properties 794 #define sd_validate_geometry ssd_validate_geometry 795 796 #if defined(_SUNOS_VTOC_16) 797 #define sd_convert_geometry ssd_convert_geometry 798 #endif 799 800 #define sd_resync_geom_caches ssd_resync_geom_caches 801 #define sd_read_fdisk ssd_read_fdisk 802 #define sd_get_physical_geometry ssd_get_physical_geometry 803 #define sd_get_virtual_geometry ssd_get_virtual_geometry 804 #define sd_update_block_info ssd_update_block_info 805 #define sd_swap_efi_gpt ssd_swap_efi_gpt 806 #define sd_swap_efi_gpe ssd_swap_efi_gpe 807 #define sd_validate_efi ssd_validate_efi 808 #define sd_use_efi ssd_use_efi 809 #define sd_uselabel ssd_uselabel 810 #define sd_build_default_label ssd_build_default_label 811 #define sd_has_max_chs_vals ssd_has_max_chs_vals 812 #define sd_inq_fill ssd_inq_fill 813 #define sd_register_devid ssd_register_devid 814 #define sd_get_devid_block ssd_get_devid_block 815 #define sd_get_devid ssd_get_devid 816 #define sd_create_devid ssd_create_devid 817 #define sd_write_deviceid ssd_write_deviceid 818 #define sd_check_vpd_page_support ssd_check_vpd_page_support 819 #define sd_setup_pm ssd_setup_pm 820 #define sd_create_pm_components ssd_create_pm_components 821 #define sd_ddi_suspend ssd_ddi_suspend 822 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 823 #define sd_ddi_resume ssd_ddi_resume 824 #define sd_ddi_pm_resume ssd_ddi_pm_resume 825 #define sdpower ssdpower 826 #define sdattach ssdattach 827 #define sddetach ssddetach 828 #define sd_unit_attach ssd_unit_attach 829 #define sd_unit_detach ssd_unit_detach 830 #define sd_set_unit_attributes ssd_set_unit_attributes 831 #define sd_create_minor_nodes ssd_create_minor_nodes 832 #define sd_create_errstats ssd_create_errstats 833 #define sd_set_errstats ssd_set_errstats 834 #define sd_set_pstats ssd_set_pstats 835 #define sddump ssddump 836 #define sd_scsi_poll ssd_scsi_poll 837 #define sd_send_polled_RQS ssd_send_polled_RQS 838 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 839 #define sd_init_event_callbacks ssd_init_event_callbacks 840 #define sd_event_callback ssd_event_callback 841 #define sd_cache_control ssd_cache_control 842 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 843 #define sd_make_device ssd_make_device 844 #define sdopen ssdopen 845 #define sdclose ssdclose 846 #define sd_ready_and_valid ssd_ready_and_valid 847 #define sdmin ssdmin 848 #define sdread ssdread 849 #define sdwrite ssdwrite 850 #define sdaread ssdaread 851 #define sdawrite ssdawrite 852 #define sdstrategy ssdstrategy 853 #define sdioctl ssdioctl 854 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 855 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 856 #define sd_checksum_iostart ssd_checksum_iostart 857 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 858 #define sd_pm_iostart ssd_pm_iostart 859 #define sd_core_iostart ssd_core_iostart 860 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 861 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 862 #define sd_checksum_iodone ssd_checksum_iodone 863 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 864 #define sd_pm_iodone ssd_pm_iodone 865 #define sd_initpkt_for_buf ssd_initpkt_for_buf 866 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 867 #define sd_setup_rw_pkt ssd_setup_rw_pkt 868 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 869 #define sd_buf_iodone ssd_buf_iodone 870 #define sd_uscsi_strategy ssd_uscsi_strategy 871 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 872 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 873 #define sd_uscsi_iodone ssd_uscsi_iodone 874 #define sd_xbuf_strategy ssd_xbuf_strategy 875 #define sd_xbuf_init ssd_xbuf_init 876 #define sd_pm_entry ssd_pm_entry 877 #define sd_pm_exit ssd_pm_exit 878 879 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 880 #define sd_pm_timeout_handler ssd_pm_timeout_handler 881 882 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 883 #define sdintr ssdintr 884 #define sd_start_cmds ssd_start_cmds 885 #define sd_send_scsi_cmd ssd_send_scsi_cmd 886 #define sd_bioclone_alloc ssd_bioclone_alloc 887 #define sd_bioclone_free ssd_bioclone_free 888 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 889 #define sd_shadow_buf_free ssd_shadow_buf_free 890 #define sd_print_transport_rejected_message \ 891 ssd_print_transport_rejected_message 892 #define sd_retry_command ssd_retry_command 893 #define sd_set_retry_bp ssd_set_retry_bp 894 #define sd_send_request_sense_command ssd_send_request_sense_command 895 #define sd_start_retry_command ssd_start_retry_command 896 #define sd_start_direct_priority_command \ 897 ssd_start_direct_priority_command 898 #define sd_return_failed_command ssd_return_failed_command 899 #define sd_return_failed_command_no_restart \ 900 ssd_return_failed_command_no_restart 901 #define sd_return_command ssd_return_command 902 #define sd_sync_with_callback ssd_sync_with_callback 903 #define sdrunout ssdrunout 904 #define sd_mark_rqs_busy ssd_mark_rqs_busy 905 #define sd_mark_rqs_idle ssd_mark_rqs_idle 906 #define sd_reduce_throttle ssd_reduce_throttle 907 #define sd_restore_throttle ssd_restore_throttle 908 #define sd_print_incomplete_msg ssd_print_incomplete_msg 909 #define sd_init_cdb_limits ssd_init_cdb_limits 910 #define sd_pkt_status_good ssd_pkt_status_good 911 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 912 #define sd_pkt_status_busy ssd_pkt_status_busy 913 #define sd_pkt_status_reservation_conflict \ 914 ssd_pkt_status_reservation_conflict 915 #define sd_pkt_status_qfull ssd_pkt_status_qfull 916 #define sd_handle_request_sense ssd_handle_request_sense 917 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 918 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 919 #define sd_validate_sense_data ssd_validate_sense_data 920 #define sd_decode_sense ssd_decode_sense 921 #define sd_print_sense_msg ssd_print_sense_msg 922 #define sd_extract_sense_info_descr ssd_extract_sense_info_descr 923 #define sd_sense_key_no_sense ssd_sense_key_no_sense 924 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 925 #define sd_sense_key_not_ready ssd_sense_key_not_ready 926 #define sd_sense_key_medium_or_hardware_error \ 927 ssd_sense_key_medium_or_hardware_error 928 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 929 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 930 #define sd_sense_key_fail_command ssd_sense_key_fail_command 931 #define sd_sense_key_blank_check ssd_sense_key_blank_check 932 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 933 #define sd_sense_key_default ssd_sense_key_default 934 #define sd_print_retry_msg ssd_print_retry_msg 935 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 936 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 937 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 938 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 939 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 940 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 941 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 942 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 943 #define sd_pkt_reason_default ssd_pkt_reason_default 944 #define sd_reset_target ssd_reset_target 945 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 946 #define sd_start_stop_unit_task ssd_start_stop_unit_task 947 #define sd_taskq_create ssd_taskq_create 948 #define sd_taskq_delete ssd_taskq_delete 949 #define sd_media_change_task ssd_media_change_task 950 #define sd_handle_mchange ssd_handle_mchange 951 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 952 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 953 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 954 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 955 #define sd_send_scsi_feature_GET_CONFIGURATION \ 956 sd_send_scsi_feature_GET_CONFIGURATION 957 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 958 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 959 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 960 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 961 ssd_send_scsi_PERSISTENT_RESERVE_IN 962 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 963 ssd_send_scsi_PERSISTENT_RESERVE_OUT 964 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 965 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 966 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 967 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 968 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 969 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 970 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 971 #define sd_alloc_rqs ssd_alloc_rqs 972 #define sd_free_rqs ssd_free_rqs 973 #define sd_dump_memory ssd_dump_memory 974 #define sd_uscsi_ioctl ssd_uscsi_ioctl 975 #define sd_get_media_info ssd_get_media_info 976 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 977 #define sd_dkio_get_geometry ssd_dkio_get_geometry 978 #define sd_dkio_set_geometry ssd_dkio_set_geometry 979 #define sd_dkio_get_partition ssd_dkio_get_partition 980 #define sd_dkio_set_partition ssd_dkio_set_partition 981 #define sd_dkio_partition ssd_dkio_partition 982 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 983 #define sd_dkio_get_efi ssd_dkio_get_efi 984 #define sd_build_user_vtoc ssd_build_user_vtoc 985 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 986 #define sd_dkio_set_efi ssd_dkio_set_efi 987 #define sd_build_label_vtoc ssd_build_label_vtoc 988 #define sd_write_label ssd_write_label 989 #define sd_clear_vtoc ssd_clear_vtoc 990 #define sd_clear_efi ssd_clear_efi 991 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 992 #define sd_setup_next_xfer ssd_setup_next_xfer 993 #define sd_dkio_get_temp ssd_dkio_get_temp 994 #define sd_dkio_get_mboot ssd_dkio_get_mboot 995 #define sd_dkio_set_mboot ssd_dkio_set_mboot 996 #define sd_setup_default_geometry ssd_setup_default_geometry 997 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 998 #define sd_check_mhd ssd_check_mhd 999 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1000 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1001 #define sd_sname ssd_sname 1002 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1003 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1004 #define sd_take_ownership ssd_take_ownership 1005 #define sd_reserve_release ssd_reserve_release 1006 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1007 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1008 #define sd_persistent_reservation_in_read_keys \ 1009 ssd_persistent_reservation_in_read_keys 1010 #define sd_persistent_reservation_in_read_resv \ 1011 ssd_persistent_reservation_in_read_resv 1012 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1013 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1014 #define sd_mhdioc_release ssd_mhdioc_release 1015 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1016 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1017 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1018 #define sr_change_blkmode ssr_change_blkmode 1019 #define sr_change_speed ssr_change_speed 1020 #define sr_atapi_change_speed ssr_atapi_change_speed 1021 #define sr_pause_resume ssr_pause_resume 1022 #define sr_play_msf ssr_play_msf 1023 #define sr_play_trkind ssr_play_trkind 1024 #define sr_read_all_subcodes ssr_read_all_subcodes 1025 #define sr_read_subchannel ssr_read_subchannel 1026 #define sr_read_tocentry ssr_read_tocentry 1027 #define sr_read_tochdr ssr_read_tochdr 1028 #define sr_read_cdda ssr_read_cdda 1029 #define sr_read_cdxa ssr_read_cdxa 1030 #define sr_read_mode1 ssr_read_mode1 1031 #define sr_read_mode2 ssr_read_mode2 1032 #define sr_read_cd_mode2 ssr_read_cd_mode2 1033 #define sr_sector_mode ssr_sector_mode 1034 #define sr_eject ssr_eject 1035 #define sr_ejected ssr_ejected 1036 #define sr_check_wp ssr_check_wp 1037 #define sd_check_media ssd_check_media 1038 #define sd_media_watch_cb ssd_media_watch_cb 1039 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1040 #define sr_volume_ctrl ssr_volume_ctrl 1041 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1042 #define sd_log_page_supported ssd_log_page_supported 1043 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1044 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1045 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1046 #define sd_range_lock ssd_range_lock 1047 #define sd_get_range ssd_get_range 1048 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1049 #define sd_range_unlock ssd_range_unlock 1050 #define sd_read_modify_write_task ssd_read_modify_write_task 1051 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1052 1053 #define sd_iostart_chain ssd_iostart_chain 1054 #define sd_iodone_chain ssd_iodone_chain 1055 #define sd_initpkt_map ssd_initpkt_map 1056 #define sd_destroypkt_map ssd_destroypkt_map 1057 #define sd_chain_type_map ssd_chain_type_map 1058 #define sd_chain_index_map ssd_chain_index_map 1059 1060 #define sd_failfast_flushctl ssd_failfast_flushctl 1061 #define sd_failfast_flushq ssd_failfast_flushq 1062 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1063 1064 #define sd_is_lsi ssd_is_lsi 1065 1066 #endif /* #if (defined(__fibre)) */ 1067 1068 1069 int _init(void); 1070 int _fini(void); 1071 int _info(struct modinfo *modinfop); 1072 1073 /*PRINTFLIKE3*/ 1074 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1075 /*PRINTFLIKE3*/ 1076 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1077 /*PRINTFLIKE3*/ 1078 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1079 1080 static int sdprobe(dev_info_t *devi); 1081 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1082 void **result); 1083 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1084 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1085 1086 /* 1087 * Smart probe for parallel scsi 1088 */ 1089 static void sd_scsi_probe_cache_init(void); 1090 static void sd_scsi_probe_cache_fini(void); 1091 static void sd_scsi_clear_probe_cache(void); 1092 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1093 1094 static int sd_spin_up_unit(struct sd_lun *un); 1095 #ifdef _LP64 1096 static void sd_enable_descr_sense(struct sd_lun *un); 1097 #endif /* _LP64 */ 1098 static void sd_set_mmc_caps(struct sd_lun *un); 1099 1100 static void sd_read_unit_properties(struct sd_lun *un); 1101 static int sd_process_sdconf_file(struct sd_lun *un); 1102 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1103 int *data_list, sd_tunables *values); 1104 static void sd_process_sdconf_table(struct sd_lun *un); 1105 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1106 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1107 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1108 int list_len, char *dataname_ptr); 1109 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1110 sd_tunables *prop_list); 1111 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1112 1113 #if defined(_SUNOS_VTOC_16) 1114 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1115 #endif 1116 1117 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1118 int path_flag); 1119 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1120 int path_flag); 1121 static void sd_get_physical_geometry(struct sd_lun *un, 1122 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1123 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1124 int lbasize); 1125 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1126 static void sd_swap_efi_gpt(efi_gpt_t *); 1127 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1128 static int sd_validate_efi(efi_gpt_t *); 1129 static int sd_use_efi(struct sd_lun *, int); 1130 static void sd_build_default_label(struct sd_lun *un); 1131 1132 #if defined(_FIRMWARE_NEEDS_FDISK) 1133 static int sd_has_max_chs_vals(struct ipart *fdp); 1134 #endif 1135 static void sd_inq_fill(char *p, int l, char *s); 1136 1137 1138 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1139 int reservation_flag); 1140 static daddr_t sd_get_devid_block(struct sd_lun *un); 1141 static int sd_get_devid(struct sd_lun *un); 1142 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1143 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1144 static int sd_write_deviceid(struct sd_lun *un); 1145 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1146 static int sd_check_vpd_page_support(struct sd_lun *un); 1147 1148 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1149 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1150 1151 static int sd_ddi_suspend(dev_info_t *devi); 1152 static int sd_ddi_pm_suspend(struct sd_lun *un); 1153 static int sd_ddi_resume(dev_info_t *devi); 1154 static int sd_ddi_pm_resume(struct sd_lun *un); 1155 static int sdpower(dev_info_t *devi, int component, int level); 1156 1157 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1158 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1159 static int sd_unit_attach(dev_info_t *devi); 1160 static int sd_unit_detach(dev_info_t *devi); 1161 1162 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1163 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1164 static void sd_create_errstats(struct sd_lun *un, int instance); 1165 static void sd_set_errstats(struct sd_lun *un); 1166 static void sd_set_pstats(struct sd_lun *un); 1167 1168 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1169 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1170 static int sd_send_polled_RQS(struct sd_lun *un); 1171 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1172 1173 #if (defined(__fibre)) 1174 /* 1175 * Event callbacks (photon) 1176 */ 1177 static void sd_init_event_callbacks(struct sd_lun *un); 1178 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1179 #endif 1180 1181 /* 1182 * Defines for sd_cache_control 1183 */ 1184 1185 #define SD_CACHE_ENABLE 1 1186 #define SD_CACHE_DISABLE 0 1187 #define SD_CACHE_NOCHANGE -1 1188 1189 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1190 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1191 static dev_t sd_make_device(dev_info_t *devi); 1192 1193 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1194 uint64_t capacity); 1195 1196 /* 1197 * Driver entry point functions. 1198 */ 1199 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1200 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1201 static int sd_ready_and_valid(struct sd_lun *un); 1202 1203 static void sdmin(struct buf *bp); 1204 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1205 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1206 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1207 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1208 1209 static int sdstrategy(struct buf *bp); 1210 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1211 1212 /* 1213 * Function prototypes for layering functions in the iostart chain. 1214 */ 1215 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1216 struct buf *bp); 1217 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1218 struct buf *bp); 1219 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1220 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1221 struct buf *bp); 1222 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1223 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1224 1225 /* 1226 * Function prototypes for layering functions in the iodone chain. 1227 */ 1228 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1229 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1230 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1231 struct buf *bp); 1232 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1233 struct buf *bp); 1234 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1235 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1236 struct buf *bp); 1237 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1238 1239 /* 1240 * Prototypes for functions to support buf(9S) based IO. 1241 */ 1242 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1243 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1244 static void sd_destroypkt_for_buf(struct buf *); 1245 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1246 struct buf *bp, int flags, 1247 int (*callback)(caddr_t), caddr_t callback_arg, 1248 diskaddr_t lba, uint32_t blockcount); 1249 #if defined(__i386) || defined(__amd64) 1250 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1251 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1252 #endif /* defined(__i386) || defined(__amd64) */ 1253 1254 /* 1255 * Prototypes for functions to support USCSI IO. 1256 */ 1257 static int sd_uscsi_strategy(struct buf *bp); 1258 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1259 static void sd_destroypkt_for_uscsi(struct buf *); 1260 1261 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1262 uchar_t chain_type, void *pktinfop); 1263 1264 static int sd_pm_entry(struct sd_lun *un); 1265 static void sd_pm_exit(struct sd_lun *un); 1266 1267 static void sd_pm_idletimeout_handler(void *arg); 1268 1269 /* 1270 * sd_core internal functions (used at the sd_core_io layer). 1271 */ 1272 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1273 static void sdintr(struct scsi_pkt *pktp); 1274 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1275 1276 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1277 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1278 int path_flag); 1279 1280 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1281 daddr_t blkno, int (*func)(struct buf *)); 1282 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1283 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1284 static void sd_bioclone_free(struct buf *bp); 1285 static void sd_shadow_buf_free(struct buf *bp); 1286 1287 static void sd_print_transport_rejected_message(struct sd_lun *un, 1288 struct sd_xbuf *xp, int code); 1289 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1290 void *arg, int code); 1291 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1292 void *arg, int code); 1293 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1294 void *arg, int code); 1295 1296 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1297 int retry_check_flag, 1298 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1299 int c), 1300 void *user_arg, int failure_code, clock_t retry_delay, 1301 void (*statp)(kstat_io_t *)); 1302 1303 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1304 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1305 1306 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1307 struct scsi_pkt *pktp); 1308 static void sd_start_retry_command(void *arg); 1309 static void sd_start_direct_priority_command(void *arg); 1310 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1311 int errcode); 1312 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1313 struct buf *bp, int errcode); 1314 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1315 static void sd_sync_with_callback(struct sd_lun *un); 1316 static int sdrunout(caddr_t arg); 1317 1318 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1319 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1320 1321 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1322 static void sd_restore_throttle(void *arg); 1323 1324 static void sd_init_cdb_limits(struct sd_lun *un); 1325 1326 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1327 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1328 1329 /* 1330 * Error handling functions 1331 */ 1332 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1333 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1334 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1335 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1336 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1337 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1338 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1339 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1340 1341 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1342 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1343 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1344 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1345 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1346 struct sd_xbuf *xp); 1347 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1348 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1349 1350 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1351 void *arg, int code); 1352 static diskaddr_t sd_extract_sense_info_descr( 1353 struct scsi_descr_sense_hdr *sdsp); 1354 1355 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1356 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1357 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1358 uint8_t asc, 1359 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1360 static void sd_sense_key_not_ready(struct sd_lun *un, 1361 uint8_t asc, uint8_t ascq, 1362 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1363 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1364 int sense_key, uint8_t asc, 1365 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1366 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1367 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1368 static void sd_sense_key_unit_attention(struct sd_lun *un, 1369 uint8_t asc, 1370 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1371 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1372 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1373 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1374 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1375 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1376 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1377 static void sd_sense_key_default(struct sd_lun *un, 1378 int sense_key, 1379 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1380 1381 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1382 void *arg, int flag); 1383 1384 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1385 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1386 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1387 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1388 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1389 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1390 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1391 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1392 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1393 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1394 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1395 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1396 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1397 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1398 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1399 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1400 1401 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1402 1403 static void sd_start_stop_unit_callback(void *arg); 1404 static void sd_start_stop_unit_task(void *arg); 1405 1406 static void sd_taskq_create(void); 1407 static void sd_taskq_delete(void); 1408 static void sd_media_change_task(void *arg); 1409 1410 static int sd_handle_mchange(struct sd_lun *un); 1411 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1412 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1413 uint32_t *lbap, int path_flag); 1414 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1415 uint32_t *lbap, int path_flag); 1416 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1417 int path_flag); 1418 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1419 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1420 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1421 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1422 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1423 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1424 uchar_t usr_cmd, uchar_t *usr_bufp); 1425 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1426 struct dk_callback *dkc); 1427 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1428 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1429 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1430 uchar_t *bufaddr, uint_t buflen); 1431 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1432 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1433 uchar_t *bufaddr, uint_t buflen, char feature); 1434 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1435 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1436 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1437 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1438 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1439 size_t buflen, daddr_t start_block, int path_flag); 1440 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1441 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1442 path_flag) 1443 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1444 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1445 path_flag) 1446 1447 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1448 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1449 uint16_t param_ptr, int path_flag); 1450 1451 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1452 static void sd_free_rqs(struct sd_lun *un); 1453 1454 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1455 uchar_t *data, int len, int fmt); 1456 static void sd_panic_for_res_conflict(struct sd_lun *un); 1457 1458 /* 1459 * Disk Ioctl Function Prototypes 1460 */ 1461 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1462 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1463 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1464 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1465 int geom_validated); 1466 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1467 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1468 int geom_validated); 1469 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1470 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1471 int geom_validated); 1472 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1473 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1474 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1475 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1476 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1477 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1478 static int sd_write_label(dev_t dev); 1479 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1480 static void sd_clear_vtoc(struct sd_lun *un); 1481 static void sd_clear_efi(struct sd_lun *un); 1482 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1483 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1484 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1485 static void sd_setup_default_geometry(struct sd_lun *un); 1486 #if defined(__i386) || defined(__amd64) 1487 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1488 #endif 1489 1490 /* 1491 * Multi-host Ioctl Prototypes 1492 */ 1493 static int sd_check_mhd(dev_t dev, int interval); 1494 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1495 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1496 static char *sd_sname(uchar_t status); 1497 static void sd_mhd_resvd_recover(void *arg); 1498 static void sd_resv_reclaim_thread(); 1499 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1500 static int sd_reserve_release(dev_t dev, int cmd); 1501 static void sd_rmv_resv_reclaim_req(dev_t dev); 1502 static void sd_mhd_reset_notify_cb(caddr_t arg); 1503 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1504 mhioc_inkeys_t *usrp, int flag); 1505 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1506 mhioc_inresvs_t *usrp, int flag); 1507 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1508 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1509 static int sd_mhdioc_release(dev_t dev); 1510 static int sd_mhdioc_register_devid(dev_t dev); 1511 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1512 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1513 1514 /* 1515 * SCSI removable prototypes 1516 */ 1517 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1518 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1519 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1520 static int sr_pause_resume(dev_t dev, int mode); 1521 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1522 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1523 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1524 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1525 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1526 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1527 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1528 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1529 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1530 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1531 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1532 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1533 static int sr_eject(dev_t dev); 1534 static void sr_ejected(register struct sd_lun *un); 1535 static int sr_check_wp(dev_t dev); 1536 static int sd_check_media(dev_t dev, enum dkio_state state); 1537 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1538 static void sd_delayed_cv_broadcast(void *arg); 1539 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1540 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1541 1542 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1543 1544 /* 1545 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1546 */ 1547 static void sd_check_for_writable_cd(struct sd_lun *un); 1548 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1549 static void sd_wm_cache_destructor(void *wm, void *un); 1550 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1551 daddr_t endb, ushort_t typ); 1552 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1553 daddr_t endb); 1554 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1555 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1556 static void sd_read_modify_write_task(void * arg); 1557 static int 1558 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1559 struct buf **bpp); 1560 1561 1562 /* 1563 * Function prototypes for failfast support. 1564 */ 1565 static void sd_failfast_flushq(struct sd_lun *un); 1566 static int sd_failfast_flushq_callback(struct buf *bp); 1567 1568 /* 1569 * Function prototypes to check for lsi devices 1570 */ 1571 static void sd_is_lsi(struct sd_lun *un); 1572 1573 /* 1574 * Function prototypes for x86 support 1575 */ 1576 #if defined(__i386) || defined(__amd64) 1577 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1578 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1579 #endif 1580 1581 /* 1582 * Constants for failfast support: 1583 * 1584 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1585 * failfast processing being performed. 1586 * 1587 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1588 * failfast processing on all bufs with B_FAILFAST set. 1589 */ 1590 1591 #define SD_FAILFAST_INACTIVE 0 1592 #define SD_FAILFAST_ACTIVE 1 1593 1594 /* 1595 * Bitmask to control behavior of buf(9S) flushes when a transition to 1596 * the failfast state occurs. Optional bits include: 1597 * 1598 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1599 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1600 * be flushed. 1601 * 1602 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1603 * driver, in addition to the regular wait queue. This includes the xbuf 1604 * queues. When clear, only the driver's wait queue will be flushed. 1605 */ 1606 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1607 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1608 1609 /* 1610 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1611 * to flush all queues within the driver. 1612 */ 1613 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1614 1615 1616 /* 1617 * SD Testing Fault Injection 1618 */ 1619 #ifdef SD_FAULT_INJECTION 1620 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1621 static void sd_faultinjection(struct scsi_pkt *pktp); 1622 static void sd_injection_log(char *buf, struct sd_lun *un); 1623 #endif 1624 1625 /* 1626 * Device driver ops vector 1627 */ 1628 static struct cb_ops sd_cb_ops = { 1629 sdopen, /* open */ 1630 sdclose, /* close */ 1631 sdstrategy, /* strategy */ 1632 nodev, /* print */ 1633 sddump, /* dump */ 1634 sdread, /* read */ 1635 sdwrite, /* write */ 1636 sdioctl, /* ioctl */ 1637 nodev, /* devmap */ 1638 nodev, /* mmap */ 1639 nodev, /* segmap */ 1640 nochpoll, /* poll */ 1641 sd_prop_op, /* cb_prop_op */ 1642 0, /* streamtab */ 1643 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1644 CB_REV, /* cb_rev */ 1645 sdaread, /* async I/O read entry point */ 1646 sdawrite /* async I/O write entry point */ 1647 }; 1648 1649 static struct dev_ops sd_ops = { 1650 DEVO_REV, /* devo_rev, */ 1651 0, /* refcnt */ 1652 sdinfo, /* info */ 1653 nulldev, /* identify */ 1654 sdprobe, /* probe */ 1655 sdattach, /* attach */ 1656 sddetach, /* detach */ 1657 nodev, /* reset */ 1658 &sd_cb_ops, /* driver operations */ 1659 NULL, /* bus operations */ 1660 sdpower /* power */ 1661 }; 1662 1663 1664 /* 1665 * This is the loadable module wrapper. 1666 */ 1667 #include <sys/modctl.h> 1668 1669 static struct modldrv modldrv = { 1670 &mod_driverops, /* Type of module. This one is a driver */ 1671 SD_MODULE_NAME, /* Module name. */ 1672 &sd_ops /* driver ops */ 1673 }; 1674 1675 1676 static struct modlinkage modlinkage = { 1677 MODREV_1, 1678 &modldrv, 1679 NULL 1680 }; 1681 1682 1683 static struct scsi_asq_key_strings sd_additional_codes[] = { 1684 0x81, 0, "Logical Unit is Reserved", 1685 0x85, 0, "Audio Address Not Valid", 1686 0xb6, 0, "Media Load Mechanism Failed", 1687 0xB9, 0, "Audio Play Operation Aborted", 1688 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1689 0x53, 2, "Medium removal prevented", 1690 0x6f, 0, "Authentication failed during key exchange", 1691 0x6f, 1, "Key not present", 1692 0x6f, 2, "Key not established", 1693 0x6f, 3, "Read without proper authentication", 1694 0x6f, 4, "Mismatched region to this logical unit", 1695 0x6f, 5, "Region reset count error", 1696 0xffff, 0x0, NULL 1697 }; 1698 1699 1700 /* 1701 * Struct for passing printing information for sense data messages 1702 */ 1703 struct sd_sense_info { 1704 int ssi_severity; 1705 int ssi_pfa_flag; 1706 }; 1707 1708 /* 1709 * Table of function pointers for iostart-side routines. Seperate "chains" 1710 * of layered function calls are formed by placing the function pointers 1711 * sequentially in the desired order. Functions are called according to an 1712 * incrementing table index ordering. The last function in each chain must 1713 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1714 * in the sd_iodone_chain[] array. 1715 * 1716 * Note: It may seem more natural to organize both the iostart and iodone 1717 * functions together, into an array of structures (or some similar 1718 * organization) with a common index, rather than two seperate arrays which 1719 * must be maintained in synchronization. The purpose of this division is 1720 * to achiece improved performance: individual arrays allows for more 1721 * effective cache line utilization on certain platforms. 1722 */ 1723 1724 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1725 1726 1727 static sd_chain_t sd_iostart_chain[] = { 1728 1729 /* Chain for buf IO for disk drive targets (PM enabled) */ 1730 sd_mapblockaddr_iostart, /* Index: 0 */ 1731 sd_pm_iostart, /* Index: 1 */ 1732 sd_core_iostart, /* Index: 2 */ 1733 1734 /* Chain for buf IO for disk drive targets (PM disabled) */ 1735 sd_mapblockaddr_iostart, /* Index: 3 */ 1736 sd_core_iostart, /* Index: 4 */ 1737 1738 /* Chain for buf IO for removable-media targets (PM enabled) */ 1739 sd_mapblockaddr_iostart, /* Index: 5 */ 1740 sd_mapblocksize_iostart, /* Index: 6 */ 1741 sd_pm_iostart, /* Index: 7 */ 1742 sd_core_iostart, /* Index: 8 */ 1743 1744 /* Chain for buf IO for removable-media targets (PM disabled) */ 1745 sd_mapblockaddr_iostart, /* Index: 9 */ 1746 sd_mapblocksize_iostart, /* Index: 10 */ 1747 sd_core_iostart, /* Index: 11 */ 1748 1749 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1750 sd_mapblockaddr_iostart, /* Index: 12 */ 1751 sd_checksum_iostart, /* Index: 13 */ 1752 sd_pm_iostart, /* Index: 14 */ 1753 sd_core_iostart, /* Index: 15 */ 1754 1755 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1756 sd_mapblockaddr_iostart, /* Index: 16 */ 1757 sd_checksum_iostart, /* Index: 17 */ 1758 sd_core_iostart, /* Index: 18 */ 1759 1760 /* Chain for USCSI commands (all targets) */ 1761 sd_pm_iostart, /* Index: 19 */ 1762 sd_core_iostart, /* Index: 20 */ 1763 1764 /* Chain for checksumming USCSI commands (all targets) */ 1765 sd_checksum_uscsi_iostart, /* Index: 21 */ 1766 sd_pm_iostart, /* Index: 22 */ 1767 sd_core_iostart, /* Index: 23 */ 1768 1769 /* Chain for "direct" USCSI commands (all targets) */ 1770 sd_core_iostart, /* Index: 24 */ 1771 1772 /* Chain for "direct priority" USCSI commands (all targets) */ 1773 sd_core_iostart, /* Index: 25 */ 1774 }; 1775 1776 /* 1777 * Macros to locate the first function of each iostart chain in the 1778 * sd_iostart_chain[] array. These are located by the index in the array. 1779 */ 1780 #define SD_CHAIN_DISK_IOSTART 0 1781 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1782 #define SD_CHAIN_RMMEDIA_IOSTART 5 1783 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1784 #define SD_CHAIN_CHKSUM_IOSTART 12 1785 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1786 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1787 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1788 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1789 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1790 1791 1792 /* 1793 * Table of function pointers for the iodone-side routines for the driver- 1794 * internal layering mechanism. The calling sequence for iodone routines 1795 * uses a decrementing table index, so the last routine called in a chain 1796 * must be at the lowest array index location for that chain. The last 1797 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1798 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1799 * of the functions in an iodone side chain must correspond to the ordering 1800 * of the iostart routines for that chain. Note that there is no iodone 1801 * side routine that corresponds to sd_core_iostart(), so there is no 1802 * entry in the table for this. 1803 */ 1804 1805 static sd_chain_t sd_iodone_chain[] = { 1806 1807 /* Chain for buf IO for disk drive targets (PM enabled) */ 1808 sd_buf_iodone, /* Index: 0 */ 1809 sd_mapblockaddr_iodone, /* Index: 1 */ 1810 sd_pm_iodone, /* Index: 2 */ 1811 1812 /* Chain for buf IO for disk drive targets (PM disabled) */ 1813 sd_buf_iodone, /* Index: 3 */ 1814 sd_mapblockaddr_iodone, /* Index: 4 */ 1815 1816 /* Chain for buf IO for removable-media targets (PM enabled) */ 1817 sd_buf_iodone, /* Index: 5 */ 1818 sd_mapblockaddr_iodone, /* Index: 6 */ 1819 sd_mapblocksize_iodone, /* Index: 7 */ 1820 sd_pm_iodone, /* Index: 8 */ 1821 1822 /* Chain for buf IO for removable-media targets (PM disabled) */ 1823 sd_buf_iodone, /* Index: 9 */ 1824 sd_mapblockaddr_iodone, /* Index: 10 */ 1825 sd_mapblocksize_iodone, /* Index: 11 */ 1826 1827 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1828 sd_buf_iodone, /* Index: 12 */ 1829 sd_mapblockaddr_iodone, /* Index: 13 */ 1830 sd_checksum_iodone, /* Index: 14 */ 1831 sd_pm_iodone, /* Index: 15 */ 1832 1833 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1834 sd_buf_iodone, /* Index: 16 */ 1835 sd_mapblockaddr_iodone, /* Index: 17 */ 1836 sd_checksum_iodone, /* Index: 18 */ 1837 1838 /* Chain for USCSI commands (non-checksum targets) */ 1839 sd_uscsi_iodone, /* Index: 19 */ 1840 sd_pm_iodone, /* Index: 20 */ 1841 1842 /* Chain for USCSI commands (checksum targets) */ 1843 sd_uscsi_iodone, /* Index: 21 */ 1844 sd_checksum_uscsi_iodone, /* Index: 22 */ 1845 sd_pm_iodone, /* Index: 22 */ 1846 1847 /* Chain for "direct" USCSI commands (all targets) */ 1848 sd_uscsi_iodone, /* Index: 24 */ 1849 1850 /* Chain for "direct priority" USCSI commands (all targets) */ 1851 sd_uscsi_iodone, /* Index: 25 */ 1852 }; 1853 1854 1855 /* 1856 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1857 * each iodone-side chain. These are located by the array index, but as the 1858 * iodone side functions are called in a decrementing-index order, the 1859 * highest index number in each chain must be specified (as these correspond 1860 * to the first function in the iodone chain that will be called by the core 1861 * at IO completion time). 1862 */ 1863 1864 #define SD_CHAIN_DISK_IODONE 2 1865 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1866 #define SD_CHAIN_RMMEDIA_IODONE 8 1867 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1868 #define SD_CHAIN_CHKSUM_IODONE 15 1869 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1870 #define SD_CHAIN_USCSI_CMD_IODONE 20 1871 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1872 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1873 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1874 1875 1876 1877 1878 /* 1879 * Array to map a layering chain index to the appropriate initpkt routine. 1880 * The redundant entries are present so that the index used for accessing 1881 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1882 * with this table as well. 1883 */ 1884 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1885 1886 static sd_initpkt_t sd_initpkt_map[] = { 1887 1888 /* Chain for buf IO for disk drive targets (PM enabled) */ 1889 sd_initpkt_for_buf, /* Index: 0 */ 1890 sd_initpkt_for_buf, /* Index: 1 */ 1891 sd_initpkt_for_buf, /* Index: 2 */ 1892 1893 /* Chain for buf IO for disk drive targets (PM disabled) */ 1894 sd_initpkt_for_buf, /* Index: 3 */ 1895 sd_initpkt_for_buf, /* Index: 4 */ 1896 1897 /* Chain for buf IO for removable-media targets (PM enabled) */ 1898 sd_initpkt_for_buf, /* Index: 5 */ 1899 sd_initpkt_for_buf, /* Index: 6 */ 1900 sd_initpkt_for_buf, /* Index: 7 */ 1901 sd_initpkt_for_buf, /* Index: 8 */ 1902 1903 /* Chain for buf IO for removable-media targets (PM disabled) */ 1904 sd_initpkt_for_buf, /* Index: 9 */ 1905 sd_initpkt_for_buf, /* Index: 10 */ 1906 sd_initpkt_for_buf, /* Index: 11 */ 1907 1908 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1909 sd_initpkt_for_buf, /* Index: 12 */ 1910 sd_initpkt_for_buf, /* Index: 13 */ 1911 sd_initpkt_for_buf, /* Index: 14 */ 1912 sd_initpkt_for_buf, /* Index: 15 */ 1913 1914 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1915 sd_initpkt_for_buf, /* Index: 16 */ 1916 sd_initpkt_for_buf, /* Index: 17 */ 1917 sd_initpkt_for_buf, /* Index: 18 */ 1918 1919 /* Chain for USCSI commands (non-checksum targets) */ 1920 sd_initpkt_for_uscsi, /* Index: 19 */ 1921 sd_initpkt_for_uscsi, /* Index: 20 */ 1922 1923 /* Chain for USCSI commands (checksum targets) */ 1924 sd_initpkt_for_uscsi, /* Index: 21 */ 1925 sd_initpkt_for_uscsi, /* Index: 22 */ 1926 sd_initpkt_for_uscsi, /* Index: 22 */ 1927 1928 /* Chain for "direct" USCSI commands (all targets) */ 1929 sd_initpkt_for_uscsi, /* Index: 24 */ 1930 1931 /* Chain for "direct priority" USCSI commands (all targets) */ 1932 sd_initpkt_for_uscsi, /* Index: 25 */ 1933 1934 }; 1935 1936 1937 /* 1938 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1939 * The redundant entries are present so that the index used for accessing 1940 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1941 * with this table as well. 1942 */ 1943 typedef void (*sd_destroypkt_t)(struct buf *); 1944 1945 static sd_destroypkt_t sd_destroypkt_map[] = { 1946 1947 /* Chain for buf IO for disk drive targets (PM enabled) */ 1948 sd_destroypkt_for_buf, /* Index: 0 */ 1949 sd_destroypkt_for_buf, /* Index: 1 */ 1950 sd_destroypkt_for_buf, /* Index: 2 */ 1951 1952 /* Chain for buf IO for disk drive targets (PM disabled) */ 1953 sd_destroypkt_for_buf, /* Index: 3 */ 1954 sd_destroypkt_for_buf, /* Index: 4 */ 1955 1956 /* Chain for buf IO for removable-media targets (PM enabled) */ 1957 sd_destroypkt_for_buf, /* Index: 5 */ 1958 sd_destroypkt_for_buf, /* Index: 6 */ 1959 sd_destroypkt_for_buf, /* Index: 7 */ 1960 sd_destroypkt_for_buf, /* Index: 8 */ 1961 1962 /* Chain for buf IO for removable-media targets (PM disabled) */ 1963 sd_destroypkt_for_buf, /* Index: 9 */ 1964 sd_destroypkt_for_buf, /* Index: 10 */ 1965 sd_destroypkt_for_buf, /* Index: 11 */ 1966 1967 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1968 sd_destroypkt_for_buf, /* Index: 12 */ 1969 sd_destroypkt_for_buf, /* Index: 13 */ 1970 sd_destroypkt_for_buf, /* Index: 14 */ 1971 sd_destroypkt_for_buf, /* Index: 15 */ 1972 1973 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1974 sd_destroypkt_for_buf, /* Index: 16 */ 1975 sd_destroypkt_for_buf, /* Index: 17 */ 1976 sd_destroypkt_for_buf, /* Index: 18 */ 1977 1978 /* Chain for USCSI commands (non-checksum targets) */ 1979 sd_destroypkt_for_uscsi, /* Index: 19 */ 1980 sd_destroypkt_for_uscsi, /* Index: 20 */ 1981 1982 /* Chain for USCSI commands (checksum targets) */ 1983 sd_destroypkt_for_uscsi, /* Index: 21 */ 1984 sd_destroypkt_for_uscsi, /* Index: 22 */ 1985 sd_destroypkt_for_uscsi, /* Index: 22 */ 1986 1987 /* Chain for "direct" USCSI commands (all targets) */ 1988 sd_destroypkt_for_uscsi, /* Index: 24 */ 1989 1990 /* Chain for "direct priority" USCSI commands (all targets) */ 1991 sd_destroypkt_for_uscsi, /* Index: 25 */ 1992 1993 }; 1994 1995 1996 1997 /* 1998 * Array to map a layering chain index to the appropriate chain "type". 1999 * The chain type indicates a specific property/usage of the chain. 2000 * The redundant entries are present so that the index used for accessing 2001 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2002 * with this table as well. 2003 */ 2004 2005 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2006 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2007 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2008 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2009 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2010 /* (for error recovery) */ 2011 2012 static int sd_chain_type_map[] = { 2013 2014 /* Chain for buf IO for disk drive targets (PM enabled) */ 2015 SD_CHAIN_BUFIO, /* Index: 0 */ 2016 SD_CHAIN_BUFIO, /* Index: 1 */ 2017 SD_CHAIN_BUFIO, /* Index: 2 */ 2018 2019 /* Chain for buf IO for disk drive targets (PM disabled) */ 2020 SD_CHAIN_BUFIO, /* Index: 3 */ 2021 SD_CHAIN_BUFIO, /* Index: 4 */ 2022 2023 /* Chain for buf IO for removable-media targets (PM enabled) */ 2024 SD_CHAIN_BUFIO, /* Index: 5 */ 2025 SD_CHAIN_BUFIO, /* Index: 6 */ 2026 SD_CHAIN_BUFIO, /* Index: 7 */ 2027 SD_CHAIN_BUFIO, /* Index: 8 */ 2028 2029 /* Chain for buf IO for removable-media targets (PM disabled) */ 2030 SD_CHAIN_BUFIO, /* Index: 9 */ 2031 SD_CHAIN_BUFIO, /* Index: 10 */ 2032 SD_CHAIN_BUFIO, /* Index: 11 */ 2033 2034 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2035 SD_CHAIN_BUFIO, /* Index: 12 */ 2036 SD_CHAIN_BUFIO, /* Index: 13 */ 2037 SD_CHAIN_BUFIO, /* Index: 14 */ 2038 SD_CHAIN_BUFIO, /* Index: 15 */ 2039 2040 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2041 SD_CHAIN_BUFIO, /* Index: 16 */ 2042 SD_CHAIN_BUFIO, /* Index: 17 */ 2043 SD_CHAIN_BUFIO, /* Index: 18 */ 2044 2045 /* Chain for USCSI commands (non-checksum targets) */ 2046 SD_CHAIN_USCSI, /* Index: 19 */ 2047 SD_CHAIN_USCSI, /* Index: 20 */ 2048 2049 /* Chain for USCSI commands (checksum targets) */ 2050 SD_CHAIN_USCSI, /* Index: 21 */ 2051 SD_CHAIN_USCSI, /* Index: 22 */ 2052 SD_CHAIN_USCSI, /* Index: 22 */ 2053 2054 /* Chain for "direct" USCSI commands (all targets) */ 2055 SD_CHAIN_DIRECT, /* Index: 24 */ 2056 2057 /* Chain for "direct priority" USCSI commands (all targets) */ 2058 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2059 }; 2060 2061 2062 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2063 #define SD_IS_BUFIO(xp) \ 2064 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2065 2066 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2067 #define SD_IS_DIRECT_PRIORITY(xp) \ 2068 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2069 2070 2071 2072 /* 2073 * Struct, array, and macros to map a specific chain to the appropriate 2074 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2075 * 2076 * The sd_chain_index_map[] array is used at attach time to set the various 2077 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2078 * chain to be used with the instance. This allows different instances to use 2079 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2080 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2081 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2082 * dynamically & without the use of locking; and (2) a layer may update the 2083 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2084 * to allow for deferred processing of an IO within the same chain from a 2085 * different execution context. 2086 */ 2087 2088 struct sd_chain_index { 2089 int sci_iostart_index; 2090 int sci_iodone_index; 2091 }; 2092 2093 static struct sd_chain_index sd_chain_index_map[] = { 2094 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2095 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2096 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2097 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2098 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2099 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2100 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2101 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2102 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2103 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2104 }; 2105 2106 2107 /* 2108 * The following are indexes into the sd_chain_index_map[] array. 2109 */ 2110 2111 /* un->un_buf_chain_type must be set to one of these */ 2112 #define SD_CHAIN_INFO_DISK 0 2113 #define SD_CHAIN_INFO_DISK_NO_PM 1 2114 #define SD_CHAIN_INFO_RMMEDIA 2 2115 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2116 #define SD_CHAIN_INFO_CHKSUM 4 2117 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2118 2119 /* un->un_uscsi_chain_type must be set to one of these */ 2120 #define SD_CHAIN_INFO_USCSI_CMD 6 2121 /* USCSI with PM disabled is the same as DIRECT */ 2122 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2123 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2124 2125 /* un->un_direct_chain_type must be set to one of these */ 2126 #define SD_CHAIN_INFO_DIRECT_CMD 8 2127 2128 /* un->un_priority_chain_type must be set to one of these */ 2129 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2130 2131 /* size for devid inquiries */ 2132 #define MAX_INQUIRY_SIZE 0xF0 2133 2134 /* 2135 * Macros used by functions to pass a given buf(9S) struct along to the 2136 * next function in the layering chain for further processing. 2137 * 2138 * In the following macros, passing more than three arguments to the called 2139 * routines causes the optimizer for the SPARC compiler to stop doing tail 2140 * call elimination which results in significant performance degradation. 2141 */ 2142 #define SD_BEGIN_IOSTART(index, un, bp) \ 2143 ((*(sd_iostart_chain[index]))(index, un, bp)) 2144 2145 #define SD_BEGIN_IODONE(index, un, bp) \ 2146 ((*(sd_iodone_chain[index]))(index, un, bp)) 2147 2148 #define SD_NEXT_IOSTART(index, un, bp) \ 2149 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2150 2151 #define SD_NEXT_IODONE(index, un, bp) \ 2152 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2153 2154 /* 2155 * Function: _init 2156 * 2157 * Description: This is the driver _init(9E) entry point. 2158 * 2159 * Return Code: Returns the value from mod_install(9F) or 2160 * ddi_soft_state_init(9F) as appropriate. 2161 * 2162 * Context: Called when driver module loaded. 2163 */ 2164 2165 int 2166 _init(void) 2167 { 2168 int err; 2169 2170 /* establish driver name from module name */ 2171 sd_label = mod_modname(&modlinkage); 2172 2173 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2174 SD_MAXUNIT); 2175 2176 if (err != 0) { 2177 return (err); 2178 } 2179 2180 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2181 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2182 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2183 2184 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2185 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2186 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2187 2188 /* 2189 * it's ok to init here even for fibre device 2190 */ 2191 sd_scsi_probe_cache_init(); 2192 2193 /* 2194 * Creating taskq before mod_install ensures that all callers (threads) 2195 * that enter the module after a successfull mod_install encounter 2196 * a valid taskq. 2197 */ 2198 sd_taskq_create(); 2199 2200 err = mod_install(&modlinkage); 2201 if (err != 0) { 2202 /* delete taskq if install fails */ 2203 sd_taskq_delete(); 2204 2205 mutex_destroy(&sd_detach_mutex); 2206 mutex_destroy(&sd_log_mutex); 2207 mutex_destroy(&sd_label_mutex); 2208 2209 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2210 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2211 cv_destroy(&sd_tr.srq_inprocess_cv); 2212 2213 sd_scsi_probe_cache_fini(); 2214 2215 ddi_soft_state_fini(&sd_state); 2216 return (err); 2217 } 2218 2219 return (err); 2220 } 2221 2222 2223 /* 2224 * Function: _fini 2225 * 2226 * Description: This is the driver _fini(9E) entry point. 2227 * 2228 * Return Code: Returns the value from mod_remove(9F) 2229 * 2230 * Context: Called when driver module is unloaded. 2231 */ 2232 2233 int 2234 _fini(void) 2235 { 2236 int err; 2237 2238 if ((err = mod_remove(&modlinkage)) != 0) { 2239 return (err); 2240 } 2241 2242 sd_taskq_delete(); 2243 2244 mutex_destroy(&sd_detach_mutex); 2245 mutex_destroy(&sd_log_mutex); 2246 mutex_destroy(&sd_label_mutex); 2247 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2248 2249 sd_scsi_probe_cache_fini(); 2250 2251 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2252 cv_destroy(&sd_tr.srq_inprocess_cv); 2253 2254 ddi_soft_state_fini(&sd_state); 2255 2256 return (err); 2257 } 2258 2259 2260 /* 2261 * Function: _info 2262 * 2263 * Description: This is the driver _info(9E) entry point. 2264 * 2265 * Arguments: modinfop - pointer to the driver modinfo structure 2266 * 2267 * Return Code: Returns the value from mod_info(9F). 2268 * 2269 * Context: Kernel thread context 2270 */ 2271 2272 int 2273 _info(struct modinfo *modinfop) 2274 { 2275 return (mod_info(&modlinkage, modinfop)); 2276 } 2277 2278 2279 /* 2280 * The following routines implement the driver message logging facility. 2281 * They provide component- and level- based debug output filtering. 2282 * Output may also be restricted to messages for a single instance by 2283 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2284 * to NULL, then messages for all instances are printed. 2285 * 2286 * These routines have been cloned from each other due to the language 2287 * constraints of macros and variable argument list processing. 2288 */ 2289 2290 2291 /* 2292 * Function: sd_log_err 2293 * 2294 * Description: This routine is called by the SD_ERROR macro for debug 2295 * logging of error conditions. 2296 * 2297 * Arguments: comp - driver component being logged 2298 * dev - pointer to driver info structure 2299 * fmt - error string and format to be logged 2300 */ 2301 2302 static void 2303 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2304 { 2305 va_list ap; 2306 dev_info_t *dev; 2307 2308 ASSERT(un != NULL); 2309 dev = SD_DEVINFO(un); 2310 ASSERT(dev != NULL); 2311 2312 /* 2313 * Filter messages based on the global component and level masks. 2314 * Also print if un matches the value of sd_debug_un, or if 2315 * sd_debug_un is set to NULL. 2316 */ 2317 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2318 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2319 mutex_enter(&sd_log_mutex); 2320 va_start(ap, fmt); 2321 (void) vsprintf(sd_log_buf, fmt, ap); 2322 va_end(ap); 2323 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2324 mutex_exit(&sd_log_mutex); 2325 } 2326 #ifdef SD_FAULT_INJECTION 2327 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2328 if (un->sd_injection_mask & comp) { 2329 mutex_enter(&sd_log_mutex); 2330 va_start(ap, fmt); 2331 (void) vsprintf(sd_log_buf, fmt, ap); 2332 va_end(ap); 2333 sd_injection_log(sd_log_buf, un); 2334 mutex_exit(&sd_log_mutex); 2335 } 2336 #endif 2337 } 2338 2339 2340 /* 2341 * Function: sd_log_info 2342 * 2343 * Description: This routine is called by the SD_INFO macro for debug 2344 * logging of general purpose informational conditions. 2345 * 2346 * Arguments: comp - driver component being logged 2347 * dev - pointer to driver info structure 2348 * fmt - info string and format to be logged 2349 */ 2350 2351 static void 2352 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2353 { 2354 va_list ap; 2355 dev_info_t *dev; 2356 2357 ASSERT(un != NULL); 2358 dev = SD_DEVINFO(un); 2359 ASSERT(dev != NULL); 2360 2361 /* 2362 * Filter messages based on the global component and level masks. 2363 * Also print if un matches the value of sd_debug_un, or if 2364 * sd_debug_un is set to NULL. 2365 */ 2366 if ((sd_component_mask & component) && 2367 (sd_level_mask & SD_LOGMASK_INFO) && 2368 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2369 mutex_enter(&sd_log_mutex); 2370 va_start(ap, fmt); 2371 (void) vsprintf(sd_log_buf, fmt, ap); 2372 va_end(ap); 2373 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2374 mutex_exit(&sd_log_mutex); 2375 } 2376 #ifdef SD_FAULT_INJECTION 2377 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2378 if (un->sd_injection_mask & component) { 2379 mutex_enter(&sd_log_mutex); 2380 va_start(ap, fmt); 2381 (void) vsprintf(sd_log_buf, fmt, ap); 2382 va_end(ap); 2383 sd_injection_log(sd_log_buf, un); 2384 mutex_exit(&sd_log_mutex); 2385 } 2386 #endif 2387 } 2388 2389 2390 /* 2391 * Function: sd_log_trace 2392 * 2393 * Description: This routine is called by the SD_TRACE macro for debug 2394 * logging of trace conditions (i.e. function entry/exit). 2395 * 2396 * Arguments: comp - driver component being logged 2397 * dev - pointer to driver info structure 2398 * fmt - trace string and format to be logged 2399 */ 2400 2401 static void 2402 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2403 { 2404 va_list ap; 2405 dev_info_t *dev; 2406 2407 ASSERT(un != NULL); 2408 dev = SD_DEVINFO(un); 2409 ASSERT(dev != NULL); 2410 2411 /* 2412 * Filter messages based on the global component and level masks. 2413 * Also print if un matches the value of sd_debug_un, or if 2414 * sd_debug_un is set to NULL. 2415 */ 2416 if ((sd_component_mask & component) && 2417 (sd_level_mask & SD_LOGMASK_TRACE) && 2418 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2419 mutex_enter(&sd_log_mutex); 2420 va_start(ap, fmt); 2421 (void) vsprintf(sd_log_buf, fmt, ap); 2422 va_end(ap); 2423 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2424 mutex_exit(&sd_log_mutex); 2425 } 2426 #ifdef SD_FAULT_INJECTION 2427 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2428 if (un->sd_injection_mask & component) { 2429 mutex_enter(&sd_log_mutex); 2430 va_start(ap, fmt); 2431 (void) vsprintf(sd_log_buf, fmt, ap); 2432 va_end(ap); 2433 sd_injection_log(sd_log_buf, un); 2434 mutex_exit(&sd_log_mutex); 2435 } 2436 #endif 2437 } 2438 2439 2440 /* 2441 * Function: sdprobe 2442 * 2443 * Description: This is the driver probe(9e) entry point function. 2444 * 2445 * Arguments: devi - opaque device info handle 2446 * 2447 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2448 * DDI_PROBE_FAILURE: If the probe failed. 2449 * DDI_PROBE_PARTIAL: If the instance is not present now, 2450 * but may be present in the future. 2451 */ 2452 2453 static int 2454 sdprobe(dev_info_t *devi) 2455 { 2456 struct scsi_device *devp; 2457 int rval; 2458 int instance; 2459 2460 /* 2461 * if it wasn't for pln, sdprobe could actually be nulldev 2462 * in the "__fibre" case. 2463 */ 2464 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2465 return (DDI_PROBE_DONTCARE); 2466 } 2467 2468 devp = ddi_get_driver_private(devi); 2469 2470 if (devp == NULL) { 2471 /* Ooops... nexus driver is mis-configured... */ 2472 return (DDI_PROBE_FAILURE); 2473 } 2474 2475 instance = ddi_get_instance(devi); 2476 2477 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2478 return (DDI_PROBE_PARTIAL); 2479 } 2480 2481 /* 2482 * Call the SCSA utility probe routine to see if we actually 2483 * have a target at this SCSI nexus. 2484 */ 2485 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2486 case SCSIPROBE_EXISTS: 2487 switch (devp->sd_inq->inq_dtype) { 2488 case DTYPE_DIRECT: 2489 rval = DDI_PROBE_SUCCESS; 2490 break; 2491 case DTYPE_RODIRECT: 2492 /* CDs etc. Can be removable media */ 2493 rval = DDI_PROBE_SUCCESS; 2494 break; 2495 case DTYPE_OPTICAL: 2496 /* 2497 * Rewritable optical driver HP115AA 2498 * Can also be removable media 2499 */ 2500 2501 /* 2502 * Do not attempt to bind to DTYPE_OPTICAL if 2503 * pre solaris 9 sparc sd behavior is required 2504 * 2505 * If first time through and sd_dtype_optical_bind 2506 * has not been set in /etc/system check properties 2507 */ 2508 2509 if (sd_dtype_optical_bind < 0) { 2510 sd_dtype_optical_bind = ddi_prop_get_int 2511 (DDI_DEV_T_ANY, devi, 0, 2512 "optical-device-bind", 1); 2513 } 2514 2515 if (sd_dtype_optical_bind == 0) { 2516 rval = DDI_PROBE_FAILURE; 2517 } else { 2518 rval = DDI_PROBE_SUCCESS; 2519 } 2520 break; 2521 2522 case DTYPE_NOTPRESENT: 2523 default: 2524 rval = DDI_PROBE_FAILURE; 2525 break; 2526 } 2527 break; 2528 default: 2529 rval = DDI_PROBE_PARTIAL; 2530 break; 2531 } 2532 2533 /* 2534 * This routine checks for resource allocation prior to freeing, 2535 * so it will take care of the "smart probing" case where a 2536 * scsi_probe() may or may not have been issued and will *not* 2537 * free previously-freed resources. 2538 */ 2539 scsi_unprobe(devp); 2540 return (rval); 2541 } 2542 2543 2544 /* 2545 * Function: sdinfo 2546 * 2547 * Description: This is the driver getinfo(9e) entry point function. 2548 * Given the device number, return the devinfo pointer from 2549 * the scsi_device structure or the instance number 2550 * associated with the dev_t. 2551 * 2552 * Arguments: dip - pointer to device info structure 2553 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2554 * DDI_INFO_DEVT2INSTANCE) 2555 * arg - driver dev_t 2556 * resultp - user buffer for request response 2557 * 2558 * Return Code: DDI_SUCCESS 2559 * DDI_FAILURE 2560 */ 2561 /* ARGSUSED */ 2562 static int 2563 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2564 { 2565 struct sd_lun *un; 2566 dev_t dev; 2567 int instance; 2568 int error; 2569 2570 switch (infocmd) { 2571 case DDI_INFO_DEVT2DEVINFO: 2572 dev = (dev_t)arg; 2573 instance = SDUNIT(dev); 2574 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2575 return (DDI_FAILURE); 2576 } 2577 *result = (void *) SD_DEVINFO(un); 2578 error = DDI_SUCCESS; 2579 break; 2580 case DDI_INFO_DEVT2INSTANCE: 2581 dev = (dev_t)arg; 2582 instance = SDUNIT(dev); 2583 *result = (void *)(uintptr_t)instance; 2584 error = DDI_SUCCESS; 2585 break; 2586 default: 2587 error = DDI_FAILURE; 2588 } 2589 return (error); 2590 } 2591 2592 /* 2593 * Function: sd_prop_op 2594 * 2595 * Description: This is the driver prop_op(9e) entry point function. 2596 * Return the number of blocks for the partition in question 2597 * or forward the request to the property facilities. 2598 * 2599 * Arguments: dev - device number 2600 * dip - pointer to device info structure 2601 * prop_op - property operator 2602 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2603 * name - pointer to property name 2604 * valuep - pointer or address of the user buffer 2605 * lengthp - property length 2606 * 2607 * Return Code: DDI_PROP_SUCCESS 2608 * DDI_PROP_NOT_FOUND 2609 * DDI_PROP_UNDEFINED 2610 * DDI_PROP_NO_MEMORY 2611 * DDI_PROP_BUF_TOO_SMALL 2612 */ 2613 2614 static int 2615 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2616 char *name, caddr_t valuep, int *lengthp) 2617 { 2618 int instance = ddi_get_instance(dip); 2619 struct sd_lun *un; 2620 uint64_t nblocks64; 2621 2622 /* 2623 * Our dynamic properties are all device specific and size oriented. 2624 * Requests issued under conditions where size is valid are passed 2625 * to ddi_prop_op_nblocks with the size information, otherwise the 2626 * request is passed to ddi_prop_op. Size depends on valid geometry. 2627 */ 2628 un = ddi_get_soft_state(sd_state, instance); 2629 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2630 (un->un_f_geometry_is_valid == FALSE)) { 2631 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2632 name, valuep, lengthp)); 2633 } else { 2634 /* get nblocks value */ 2635 ASSERT(!mutex_owned(SD_MUTEX(un))); 2636 mutex_enter(SD_MUTEX(un)); 2637 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2638 mutex_exit(SD_MUTEX(un)); 2639 2640 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2641 name, valuep, lengthp, nblocks64)); 2642 } 2643 } 2644 2645 /* 2646 * The following functions are for smart probing: 2647 * sd_scsi_probe_cache_init() 2648 * sd_scsi_probe_cache_fini() 2649 * sd_scsi_clear_probe_cache() 2650 * sd_scsi_probe_with_cache() 2651 */ 2652 2653 /* 2654 * Function: sd_scsi_probe_cache_init 2655 * 2656 * Description: Initializes the probe response cache mutex and head pointer. 2657 * 2658 * Context: Kernel thread context 2659 */ 2660 2661 static void 2662 sd_scsi_probe_cache_init(void) 2663 { 2664 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2665 sd_scsi_probe_cache_head = NULL; 2666 } 2667 2668 2669 /* 2670 * Function: sd_scsi_probe_cache_fini 2671 * 2672 * Description: Frees all resources associated with the probe response cache. 2673 * 2674 * Context: Kernel thread context 2675 */ 2676 2677 static void 2678 sd_scsi_probe_cache_fini(void) 2679 { 2680 struct sd_scsi_probe_cache *cp; 2681 struct sd_scsi_probe_cache *ncp; 2682 2683 /* Clean up our smart probing linked list */ 2684 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2685 ncp = cp->next; 2686 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2687 } 2688 sd_scsi_probe_cache_head = NULL; 2689 mutex_destroy(&sd_scsi_probe_cache_mutex); 2690 } 2691 2692 2693 /* 2694 * Function: sd_scsi_clear_probe_cache 2695 * 2696 * Description: This routine clears the probe response cache. This is 2697 * done when open() returns ENXIO so that when deferred 2698 * attach is attempted (possibly after a device has been 2699 * turned on) we will retry the probe. Since we don't know 2700 * which target we failed to open, we just clear the 2701 * entire cache. 2702 * 2703 * Context: Kernel thread context 2704 */ 2705 2706 static void 2707 sd_scsi_clear_probe_cache(void) 2708 { 2709 struct sd_scsi_probe_cache *cp; 2710 int i; 2711 2712 mutex_enter(&sd_scsi_probe_cache_mutex); 2713 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2714 /* 2715 * Reset all entries to SCSIPROBE_EXISTS. This will 2716 * force probing to be performed the next time 2717 * sd_scsi_probe_with_cache is called. 2718 */ 2719 for (i = 0; i < NTARGETS_WIDE; i++) { 2720 cp->cache[i] = SCSIPROBE_EXISTS; 2721 } 2722 } 2723 mutex_exit(&sd_scsi_probe_cache_mutex); 2724 } 2725 2726 2727 /* 2728 * Function: sd_scsi_probe_with_cache 2729 * 2730 * Description: This routine implements support for a scsi device probe 2731 * with cache. The driver maintains a cache of the target 2732 * responses to scsi probes. If we get no response from a 2733 * target during a probe inquiry, we remember that, and we 2734 * avoid additional calls to scsi_probe on non-zero LUNs 2735 * on the same target until the cache is cleared. By doing 2736 * so we avoid the 1/4 sec selection timeout for nonzero 2737 * LUNs. lun0 of a target is always probed. 2738 * 2739 * Arguments: devp - Pointer to a scsi_device(9S) structure 2740 * waitfunc - indicates what the allocator routines should 2741 * do when resources are not available. This value 2742 * is passed on to scsi_probe() when that routine 2743 * is called. 2744 * 2745 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2746 * otherwise the value returned by scsi_probe(9F). 2747 * 2748 * Context: Kernel thread context 2749 */ 2750 2751 static int 2752 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2753 { 2754 struct sd_scsi_probe_cache *cp; 2755 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2756 int lun, tgt; 2757 2758 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2759 SCSI_ADDR_PROP_LUN, 0); 2760 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2761 SCSI_ADDR_PROP_TARGET, -1); 2762 2763 /* Make sure caching enabled and target in range */ 2764 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2765 /* do it the old way (no cache) */ 2766 return (scsi_probe(devp, waitfn)); 2767 } 2768 2769 mutex_enter(&sd_scsi_probe_cache_mutex); 2770 2771 /* Find the cache for this scsi bus instance */ 2772 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2773 if (cp->pdip == pdip) { 2774 break; 2775 } 2776 } 2777 2778 /* If we can't find a cache for this pdip, create one */ 2779 if (cp == NULL) { 2780 int i; 2781 2782 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2783 KM_SLEEP); 2784 cp->pdip = pdip; 2785 cp->next = sd_scsi_probe_cache_head; 2786 sd_scsi_probe_cache_head = cp; 2787 for (i = 0; i < NTARGETS_WIDE; i++) { 2788 cp->cache[i] = SCSIPROBE_EXISTS; 2789 } 2790 } 2791 2792 mutex_exit(&sd_scsi_probe_cache_mutex); 2793 2794 /* Recompute the cache for this target if LUN zero */ 2795 if (lun == 0) { 2796 cp->cache[tgt] = SCSIPROBE_EXISTS; 2797 } 2798 2799 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2800 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2801 return (SCSIPROBE_NORESP); 2802 } 2803 2804 /* Do the actual probe; save & return the result */ 2805 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2806 } 2807 2808 2809 /* 2810 * Function: sd_spin_up_unit 2811 * 2812 * Description: Issues the following commands to spin-up the device: 2813 * START STOP UNIT, and INQUIRY. 2814 * 2815 * Arguments: un - driver soft state (unit) structure 2816 * 2817 * Return Code: 0 - success 2818 * EIO - failure 2819 * EACCES - reservation conflict 2820 * 2821 * Context: Kernel thread context 2822 */ 2823 2824 static int 2825 sd_spin_up_unit(struct sd_lun *un) 2826 { 2827 size_t resid = 0; 2828 int has_conflict = FALSE; 2829 uchar_t *bufaddr; 2830 2831 ASSERT(un != NULL); 2832 2833 /* 2834 * Send a throwaway START UNIT command. 2835 * 2836 * If we fail on this, we don't care presently what precisely 2837 * is wrong. EMC's arrays will also fail this with a check 2838 * condition (0x2/0x4/0x3) if the device is "inactive," but 2839 * we don't want to fail the attach because it may become 2840 * "active" later. 2841 */ 2842 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2843 == EACCES) 2844 has_conflict = TRUE; 2845 2846 /* 2847 * Send another INQUIRY command to the target. This is necessary for 2848 * non-removable media direct access devices because their INQUIRY data 2849 * may not be fully qualified until they are spun up (perhaps via the 2850 * START command above). Note: This seems to be needed for some 2851 * legacy devices only.) The INQUIRY command should succeed even if a 2852 * Reservation Conflict is present. 2853 */ 2854 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2855 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2856 kmem_free(bufaddr, SUN_INQSIZE); 2857 return (EIO); 2858 } 2859 2860 /* 2861 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2862 * Note that this routine does not return a failure here even if the 2863 * INQUIRY command did not return any data. This is a legacy behavior. 2864 */ 2865 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2866 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2867 } 2868 2869 kmem_free(bufaddr, SUN_INQSIZE); 2870 2871 /* If we hit a reservation conflict above, tell the caller. */ 2872 if (has_conflict == TRUE) { 2873 return (EACCES); 2874 } 2875 2876 return (0); 2877 } 2878 2879 #ifdef _LP64 2880 /* 2881 * Function: sd_enable_descr_sense 2882 * 2883 * Description: This routine attempts to select descriptor sense format 2884 * using the Control mode page. Devices that support 64 bit 2885 * LBAs (for >2TB luns) should also implement descriptor 2886 * sense data so we will call this function whenever we see 2887 * a lun larger than 2TB. If for some reason the device 2888 * supports 64 bit LBAs but doesn't support descriptor sense 2889 * presumably the mode select will fail. Everything will 2890 * continue to work normally except that we will not get 2891 * complete sense data for commands that fail with an LBA 2892 * larger than 32 bits. 2893 * 2894 * Arguments: un - driver soft state (unit) structure 2895 * 2896 * Context: Kernel thread context only 2897 */ 2898 2899 static void 2900 sd_enable_descr_sense(struct sd_lun *un) 2901 { 2902 uchar_t *header; 2903 struct mode_control_scsi3 *ctrl_bufp; 2904 size_t buflen; 2905 size_t bd_len; 2906 2907 /* 2908 * Read MODE SENSE page 0xA, Control Mode Page 2909 */ 2910 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2911 sizeof (struct mode_control_scsi3); 2912 header = kmem_zalloc(buflen, KM_SLEEP); 2913 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2914 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2915 SD_ERROR(SD_LOG_COMMON, un, 2916 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2917 goto eds_exit; 2918 } 2919 2920 /* 2921 * Determine size of Block Descriptors in order to locate 2922 * the mode page data. ATAPI devices return 0, SCSI devices 2923 * should return MODE_BLK_DESC_LENGTH. 2924 */ 2925 bd_len = ((struct mode_header *)header)->bdesc_length; 2926 2927 ctrl_bufp = (struct mode_control_scsi3 *) 2928 (header + MODE_HEADER_LENGTH + bd_len); 2929 2930 /* 2931 * Clear PS bit for MODE SELECT 2932 */ 2933 ctrl_bufp->mode_page.ps = 0; 2934 2935 /* 2936 * Set D_SENSE to enable descriptor sense format. 2937 */ 2938 ctrl_bufp->d_sense = 1; 2939 2940 /* 2941 * Use MODE SELECT to commit the change to the D_SENSE bit 2942 */ 2943 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2944 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2945 SD_INFO(SD_LOG_COMMON, un, 2946 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2947 goto eds_exit; 2948 } 2949 2950 eds_exit: 2951 kmem_free(header, buflen); 2952 } 2953 #endif /* _LP64 */ 2954 2955 2956 /* 2957 * Function: sd_set_mmc_caps 2958 * 2959 * Description: This routine determines if the device is MMC compliant and if 2960 * the device supports CDDA via a mode sense of the CDVD 2961 * capabilities mode page. Also checks if the device is a 2962 * dvdram writable device. 2963 * 2964 * Arguments: un - driver soft state (unit) structure 2965 * 2966 * Context: Kernel thread context only 2967 */ 2968 2969 static void 2970 sd_set_mmc_caps(struct sd_lun *un) 2971 { 2972 struct mode_header_grp2 *sense_mhp; 2973 uchar_t *sense_page; 2974 caddr_t buf; 2975 int bd_len; 2976 int status; 2977 struct uscsi_cmd com; 2978 int rtn; 2979 uchar_t *out_data_rw, *out_data_hd; 2980 uchar_t *rqbuf_rw, *rqbuf_hd; 2981 2982 ASSERT(un != NULL); 2983 2984 /* 2985 * The flags which will be set in this function are - mmc compliant, 2986 * dvdram writable device, cdda support. Initialize them to FALSE 2987 * and if a capability is detected - it will be set to TRUE. 2988 */ 2989 un->un_f_mmc_cap = FALSE; 2990 un->un_f_dvdram_writable_device = FALSE; 2991 un->un_f_cfg_cdda = FALSE; 2992 2993 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 2994 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 2995 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 2996 2997 if (status != 0) { 2998 /* command failed; just return */ 2999 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3000 return; 3001 } 3002 /* 3003 * If the mode sense request for the CDROM CAPABILITIES 3004 * page (0x2A) succeeds the device is assumed to be MMC. 3005 */ 3006 un->un_f_mmc_cap = TRUE; 3007 3008 /* Get to the page data */ 3009 sense_mhp = (struct mode_header_grp2 *)buf; 3010 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3011 sense_mhp->bdesc_length_lo; 3012 if (bd_len > MODE_BLK_DESC_LENGTH) { 3013 /* 3014 * We did not get back the expected block descriptor 3015 * length so we cannot determine if the device supports 3016 * CDDA. However, we still indicate the device is MMC 3017 * according to the successful response to the page 3018 * 0x2A mode sense request. 3019 */ 3020 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3021 "sd_set_mmc_caps: Mode Sense returned " 3022 "invalid block descriptor length\n"); 3023 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3024 return; 3025 } 3026 3027 /* See if read CDDA is supported */ 3028 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3029 bd_len); 3030 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3031 3032 /* See if writing DVD RAM is supported. */ 3033 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3034 if (un->un_f_dvdram_writable_device == TRUE) { 3035 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3036 return; 3037 } 3038 3039 /* 3040 * If the device presents DVD or CD capabilities in the mode 3041 * page, we can return here since a RRD will not have 3042 * these capabilities. 3043 */ 3044 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3045 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3046 return; 3047 } 3048 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3049 3050 /* 3051 * If un->un_f_dvdram_writable_device is still FALSE, 3052 * check for a Removable Rigid Disk (RRD). A RRD 3053 * device is identified by the features RANDOM_WRITABLE and 3054 * HARDWARE_DEFECT_MANAGEMENT. 3055 */ 3056 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3057 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3058 3059 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3060 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3061 RANDOM_WRITABLE); 3062 if (rtn != 0) { 3063 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3064 kmem_free(rqbuf_rw, SENSE_LENGTH); 3065 return; 3066 } 3067 3068 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3069 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3070 3071 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3072 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3073 HARDWARE_DEFECT_MANAGEMENT); 3074 if (rtn == 0) { 3075 /* 3076 * We have good information, check for random writable 3077 * and hardware defect features. 3078 */ 3079 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3080 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3081 un->un_f_dvdram_writable_device = TRUE; 3082 } 3083 } 3084 3085 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3086 kmem_free(rqbuf_rw, SENSE_LENGTH); 3087 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3088 kmem_free(rqbuf_hd, SENSE_LENGTH); 3089 } 3090 3091 /* 3092 * Function: sd_check_for_writable_cd 3093 * 3094 * Description: This routine determines if the media in the device is 3095 * writable or not. It uses the get configuration command (0x46) 3096 * to determine if the media is writable 3097 * 3098 * Arguments: un - driver soft state (unit) structure 3099 * 3100 * Context: Never called at interrupt context. 3101 */ 3102 3103 static void 3104 sd_check_for_writable_cd(struct sd_lun *un) 3105 { 3106 struct uscsi_cmd com; 3107 uchar_t *out_data; 3108 uchar_t *rqbuf; 3109 int rtn; 3110 uchar_t *out_data_rw, *out_data_hd; 3111 uchar_t *rqbuf_rw, *rqbuf_hd; 3112 struct mode_header_grp2 *sense_mhp; 3113 uchar_t *sense_page; 3114 caddr_t buf; 3115 int bd_len; 3116 int status; 3117 3118 ASSERT(un != NULL); 3119 ASSERT(mutex_owned(SD_MUTEX(un))); 3120 3121 /* 3122 * Initialize the writable media to false, if configuration info. 3123 * tells us otherwise then only we will set it. 3124 */ 3125 un->un_f_mmc_writable_media = FALSE; 3126 mutex_exit(SD_MUTEX(un)); 3127 3128 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3129 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3130 3131 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3132 out_data, SD_PROFILE_HEADER_LEN); 3133 3134 mutex_enter(SD_MUTEX(un)); 3135 if (rtn == 0) { 3136 /* 3137 * We have good information, check for writable DVD. 3138 */ 3139 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3140 un->un_f_mmc_writable_media = TRUE; 3141 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3142 kmem_free(rqbuf, SENSE_LENGTH); 3143 return; 3144 } 3145 } 3146 3147 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3148 kmem_free(rqbuf, SENSE_LENGTH); 3149 3150 /* 3151 * Determine if this is a RRD type device. 3152 */ 3153 mutex_exit(SD_MUTEX(un)); 3154 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3155 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3156 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3157 mutex_enter(SD_MUTEX(un)); 3158 if (status != 0) { 3159 /* command failed; just return */ 3160 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3161 return; 3162 } 3163 3164 /* Get to the page data */ 3165 sense_mhp = (struct mode_header_grp2 *)buf; 3166 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3167 if (bd_len > MODE_BLK_DESC_LENGTH) { 3168 /* 3169 * We did not get back the expected block descriptor length so 3170 * we cannot check the mode page. 3171 */ 3172 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3173 "sd_check_for_writable_cd: Mode Sense returned " 3174 "invalid block descriptor length\n"); 3175 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3176 return; 3177 } 3178 3179 /* 3180 * If the device presents DVD or CD capabilities in the mode 3181 * page, we can return here since a RRD device will not have 3182 * these capabilities. 3183 */ 3184 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3185 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3186 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3187 return; 3188 } 3189 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3190 3191 /* 3192 * If un->un_f_mmc_writable_media is still FALSE, 3193 * check for RRD type media. A RRD device is identified 3194 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3195 */ 3196 mutex_exit(SD_MUTEX(un)); 3197 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3198 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3199 3200 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3201 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3202 RANDOM_WRITABLE); 3203 if (rtn != 0) { 3204 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3205 kmem_free(rqbuf_rw, SENSE_LENGTH); 3206 mutex_enter(SD_MUTEX(un)); 3207 return; 3208 } 3209 3210 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3211 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3212 3213 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3214 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3215 HARDWARE_DEFECT_MANAGEMENT); 3216 mutex_enter(SD_MUTEX(un)); 3217 if (rtn == 0) { 3218 /* 3219 * We have good information, check for random writable 3220 * and hardware defect features as current. 3221 */ 3222 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3223 (out_data_rw[10] & 0x1) && 3224 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3225 (out_data_hd[10] & 0x1)) { 3226 un->un_f_mmc_writable_media = TRUE; 3227 } 3228 } 3229 3230 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3231 kmem_free(rqbuf_rw, SENSE_LENGTH); 3232 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3233 kmem_free(rqbuf_hd, SENSE_LENGTH); 3234 } 3235 3236 /* 3237 * Function: sd_read_unit_properties 3238 * 3239 * Description: The following implements a property lookup mechanism. 3240 * Properties for particular disks (keyed on vendor, model 3241 * and rev numbers) are sought in the sd.conf file via 3242 * sd_process_sdconf_file(), and if not found there, are 3243 * looked for in a list hardcoded in this driver via 3244 * sd_process_sdconf_table() Once located the properties 3245 * are used to update the driver unit structure. 3246 * 3247 * Arguments: un - driver soft state (unit) structure 3248 */ 3249 3250 static void 3251 sd_read_unit_properties(struct sd_lun *un) 3252 { 3253 /* 3254 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3255 * the "sd-config-list" property (from the sd.conf file) or if 3256 * there was not a match for the inquiry vid/pid. If this event 3257 * occurs the static driver configuration table is searched for 3258 * a match. 3259 */ 3260 ASSERT(un != NULL); 3261 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3262 sd_process_sdconf_table(un); 3263 } 3264 3265 /* check for LSI device */ 3266 sd_is_lsi(un); 3267 3268 3269 } 3270 3271 3272 /* 3273 * Function: sd_process_sdconf_file 3274 * 3275 * Description: Use ddi_getlongprop to obtain the properties from the 3276 * driver's config file (ie, sd.conf) and update the driver 3277 * soft state structure accordingly. 3278 * 3279 * Arguments: un - driver soft state (unit) structure 3280 * 3281 * Return Code: SD_SUCCESS - The properties were successfully set according 3282 * to the driver configuration file. 3283 * SD_FAILURE - The driver config list was not obtained or 3284 * there was no vid/pid match. This indicates that 3285 * the static config table should be used. 3286 * 3287 * The config file has a property, "sd-config-list", which consists of 3288 * one or more duplets as follows: 3289 * 3290 * sd-config-list= 3291 * <duplet>, 3292 * [<duplet>,] 3293 * [<duplet>]; 3294 * 3295 * The structure of each duplet is as follows: 3296 * 3297 * <duplet>:= <vid+pid>,<data-property-name_list> 3298 * 3299 * The first entry of the duplet is the device ID string (the concatenated 3300 * vid & pid; not to be confused with a device_id). This is defined in 3301 * the same way as in the sd_disk_table. 3302 * 3303 * The second part of the duplet is a string that identifies a 3304 * data-property-name-list. The data-property-name-list is defined as 3305 * follows: 3306 * 3307 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3308 * 3309 * The syntax of <data-property-name> depends on the <version> field. 3310 * 3311 * If version = SD_CONF_VERSION_1 we have the following syntax: 3312 * 3313 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3314 * 3315 * where the prop0 value will be used to set prop0 if bit0 set in the 3316 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3317 * 3318 */ 3319 3320 static int 3321 sd_process_sdconf_file(struct sd_lun *un) 3322 { 3323 char *config_list = NULL; 3324 int config_list_len; 3325 int len; 3326 int dupletlen = 0; 3327 char *vidptr; 3328 int vidlen; 3329 char *dnlist_ptr; 3330 char *dataname_ptr; 3331 int dnlist_len; 3332 int dataname_len; 3333 int *data_list; 3334 int data_list_len; 3335 int rval = SD_FAILURE; 3336 int i; 3337 3338 ASSERT(un != NULL); 3339 3340 /* Obtain the configuration list associated with the .conf file */ 3341 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3342 sd_config_list, (caddr_t)&config_list, &config_list_len) 3343 != DDI_PROP_SUCCESS) { 3344 return (SD_FAILURE); 3345 } 3346 3347 /* 3348 * Compare vids in each duplet to the inquiry vid - if a match is 3349 * made, get the data value and update the soft state structure 3350 * accordingly. 3351 * 3352 * Note: This algorithm is complex and difficult to maintain. It should 3353 * be replaced with a more robust implementation. 3354 */ 3355 for (len = config_list_len, vidptr = config_list; len > 0; 3356 vidptr += dupletlen, len -= dupletlen) { 3357 /* 3358 * Note: The assumption here is that each vid entry is on 3359 * a unique line from its associated duplet. 3360 */ 3361 vidlen = dupletlen = (int)strlen(vidptr); 3362 if ((vidlen == 0) || 3363 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3364 dupletlen++; 3365 continue; 3366 } 3367 3368 /* 3369 * dnlist contains 1 or more blank separated 3370 * data-property-name entries 3371 */ 3372 dnlist_ptr = vidptr + vidlen + 1; 3373 dnlist_len = (int)strlen(dnlist_ptr); 3374 dupletlen += dnlist_len + 2; 3375 3376 /* 3377 * Set a pointer for the first data-property-name 3378 * entry in the list 3379 */ 3380 dataname_ptr = dnlist_ptr; 3381 dataname_len = 0; 3382 3383 /* 3384 * Loop through all data-property-name entries in the 3385 * data-property-name-list setting the properties for each. 3386 */ 3387 while (dataname_len < dnlist_len) { 3388 int version; 3389 3390 /* 3391 * Determine the length of the current 3392 * data-property-name entry by indexing until a 3393 * blank or NULL is encountered. When the space is 3394 * encountered reset it to a NULL for compliance 3395 * with ddi_getlongprop(). 3396 */ 3397 for (i = 0; ((dataname_ptr[i] != ' ') && 3398 (dataname_ptr[i] != '\0')); i++) { 3399 ; 3400 } 3401 3402 dataname_len += i; 3403 /* If not null terminated, Make it so */ 3404 if (dataname_ptr[i] == ' ') { 3405 dataname_ptr[i] = '\0'; 3406 } 3407 dataname_len++; 3408 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3409 "sd_process_sdconf_file: disk:%s, data:%s\n", 3410 vidptr, dataname_ptr); 3411 3412 /* Get the data list */ 3413 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3414 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3415 != DDI_PROP_SUCCESS) { 3416 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3417 "sd_process_sdconf_file: data property (%s)" 3418 " has no value\n", dataname_ptr); 3419 dataname_ptr = dnlist_ptr + dataname_len; 3420 continue; 3421 } 3422 3423 version = data_list[0]; 3424 3425 if (version == SD_CONF_VERSION_1) { 3426 sd_tunables values; 3427 3428 /* Set the properties */ 3429 if (sd_chk_vers1_data(un, data_list[1], 3430 &data_list[2], data_list_len, dataname_ptr) 3431 == SD_SUCCESS) { 3432 sd_get_tunables_from_conf(un, 3433 data_list[1], &data_list[2], 3434 &values); 3435 sd_set_vers1_properties(un, 3436 data_list[1], &values); 3437 rval = SD_SUCCESS; 3438 } else { 3439 rval = SD_FAILURE; 3440 } 3441 } else { 3442 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3443 "data property %s version 0x%x is invalid.", 3444 dataname_ptr, version); 3445 rval = SD_FAILURE; 3446 } 3447 kmem_free(data_list, data_list_len); 3448 dataname_ptr = dnlist_ptr + dataname_len; 3449 } 3450 } 3451 3452 /* free up the memory allocated by ddi_getlongprop */ 3453 if (config_list) { 3454 kmem_free(config_list, config_list_len); 3455 } 3456 3457 return (rval); 3458 } 3459 3460 /* 3461 * Function: sd_get_tunables_from_conf() 3462 * 3463 * 3464 * This function reads the data list from the sd.conf file and pulls 3465 * the values that can have numeric values as arguments and places 3466 * the values in the apropriate sd_tunables member. 3467 * Since the order of the data list members varies across platforms 3468 * This function reads them from the data list in a platform specific 3469 * order and places them into the correct sd_tunable member that is 3470 * a consistant across all platforms. 3471 */ 3472 static void 3473 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3474 sd_tunables *values) 3475 { 3476 int i; 3477 int mask; 3478 3479 bzero(values, sizeof (sd_tunables)); 3480 3481 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3482 3483 mask = 1 << i; 3484 if (mask > flags) { 3485 break; 3486 } 3487 3488 switch (mask & flags) { 3489 case 0: /* This mask bit not set in flags */ 3490 continue; 3491 case SD_CONF_BSET_THROTTLE: 3492 values->sdt_throttle = data_list[i]; 3493 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3494 "sd_get_tunables_from_conf: throttle = %d\n", 3495 values->sdt_throttle); 3496 break; 3497 case SD_CONF_BSET_CTYPE: 3498 values->sdt_ctype = data_list[i]; 3499 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3500 "sd_get_tunables_from_conf: ctype = %d\n", 3501 values->sdt_ctype); 3502 break; 3503 case SD_CONF_BSET_NRR_COUNT: 3504 values->sdt_not_rdy_retries = data_list[i]; 3505 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3506 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3507 values->sdt_not_rdy_retries); 3508 break; 3509 case SD_CONF_BSET_BSY_RETRY_COUNT: 3510 values->sdt_busy_retries = data_list[i]; 3511 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3512 "sd_get_tunables_from_conf: busy_retries = %d\n", 3513 values->sdt_busy_retries); 3514 break; 3515 case SD_CONF_BSET_RST_RETRIES: 3516 values->sdt_reset_retries = data_list[i]; 3517 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3518 "sd_get_tunables_from_conf: reset_retries = %d\n", 3519 values->sdt_reset_retries); 3520 break; 3521 case SD_CONF_BSET_RSV_REL_TIME: 3522 values->sdt_reserv_rel_time = data_list[i]; 3523 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3524 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3525 values->sdt_reserv_rel_time); 3526 break; 3527 case SD_CONF_BSET_MIN_THROTTLE: 3528 values->sdt_min_throttle = data_list[i]; 3529 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3530 "sd_get_tunables_from_conf: min_throttle = %d\n", 3531 values->sdt_min_throttle); 3532 break; 3533 case SD_CONF_BSET_DISKSORT_DISABLED: 3534 values->sdt_disk_sort_dis = data_list[i]; 3535 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3536 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3537 values->sdt_disk_sort_dis); 3538 break; 3539 case SD_CONF_BSET_LUN_RESET_ENABLED: 3540 values->sdt_lun_reset_enable = data_list[i]; 3541 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3542 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3543 "\n", values->sdt_lun_reset_enable); 3544 break; 3545 } 3546 } 3547 } 3548 3549 /* 3550 * Function: sd_process_sdconf_table 3551 * 3552 * Description: Search the static configuration table for a match on the 3553 * inquiry vid/pid and update the driver soft state structure 3554 * according to the table property values for the device. 3555 * 3556 * The form of a configuration table entry is: 3557 * <vid+pid>,<flags>,<property-data> 3558 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3559 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3560 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3561 * 3562 * Arguments: un - driver soft state (unit) structure 3563 */ 3564 3565 static void 3566 sd_process_sdconf_table(struct sd_lun *un) 3567 { 3568 char *id = NULL; 3569 int table_index; 3570 int idlen; 3571 3572 ASSERT(un != NULL); 3573 for (table_index = 0; table_index < sd_disk_table_size; 3574 table_index++) { 3575 id = sd_disk_table[table_index].device_id; 3576 idlen = strlen(id); 3577 if (idlen == 0) { 3578 continue; 3579 } 3580 3581 /* 3582 * The static configuration table currently does not 3583 * implement version 10 properties. Additionally, 3584 * multiple data-property-name entries are not 3585 * implemented in the static configuration table. 3586 */ 3587 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3588 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3589 "sd_process_sdconf_table: disk %s\n", id); 3590 sd_set_vers1_properties(un, 3591 sd_disk_table[table_index].flags, 3592 sd_disk_table[table_index].properties); 3593 break; 3594 } 3595 } 3596 } 3597 3598 3599 /* 3600 * Function: sd_sdconf_id_match 3601 * 3602 * Description: This local function implements a case sensitive vid/pid 3603 * comparison as well as the boundary cases of wild card and 3604 * multiple blanks. 3605 * 3606 * Note: An implicit assumption made here is that the scsi 3607 * inquiry structure will always keep the vid, pid and 3608 * revision strings in consecutive sequence, so they can be 3609 * read as a single string. If this assumption is not the 3610 * case, a separate string, to be used for the check, needs 3611 * to be built with these strings concatenated. 3612 * 3613 * Arguments: un - driver soft state (unit) structure 3614 * id - table or config file vid/pid 3615 * idlen - length of the vid/pid (bytes) 3616 * 3617 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3618 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3619 */ 3620 3621 static int 3622 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3623 { 3624 struct scsi_inquiry *sd_inq; 3625 int rval = SD_SUCCESS; 3626 3627 ASSERT(un != NULL); 3628 sd_inq = un->un_sd->sd_inq; 3629 ASSERT(id != NULL); 3630 3631 /* 3632 * We use the inq_vid as a pointer to a buffer containing the 3633 * vid and pid and use the entire vid/pid length of the table 3634 * entry for the comparison. This works because the inq_pid 3635 * data member follows inq_vid in the scsi_inquiry structure. 3636 */ 3637 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3638 /* 3639 * The user id string is compared to the inquiry vid/pid 3640 * using a case insensitive comparison and ignoring 3641 * multiple spaces. 3642 */ 3643 rval = sd_blank_cmp(un, id, idlen); 3644 if (rval != SD_SUCCESS) { 3645 /* 3646 * User id strings that start and end with a "*" 3647 * are a special case. These do not have a 3648 * specific vendor, and the product string can 3649 * appear anywhere in the 16 byte PID portion of 3650 * the inquiry data. This is a simple strstr() 3651 * type search for the user id in the inquiry data. 3652 */ 3653 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3654 char *pidptr = &id[1]; 3655 int i; 3656 int j; 3657 int pidstrlen = idlen - 2; 3658 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3659 pidstrlen; 3660 3661 if (j < 0) { 3662 return (SD_FAILURE); 3663 } 3664 for (i = 0; i < j; i++) { 3665 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3666 pidptr, pidstrlen) == 0) { 3667 rval = SD_SUCCESS; 3668 break; 3669 } 3670 } 3671 } 3672 } 3673 } 3674 return (rval); 3675 } 3676 3677 3678 /* 3679 * Function: sd_blank_cmp 3680 * 3681 * Description: If the id string starts and ends with a space, treat 3682 * multiple consecutive spaces as equivalent to a single 3683 * space. For example, this causes a sd_disk_table entry 3684 * of " NEC CDROM " to match a device's id string of 3685 * "NEC CDROM". 3686 * 3687 * Note: The success exit condition for this routine is if 3688 * the pointer to the table entry is '\0' and the cnt of 3689 * the inquiry length is zero. This will happen if the inquiry 3690 * string returned by the device is padded with spaces to be 3691 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3692 * SCSI spec states that the inquiry string is to be padded with 3693 * spaces. 3694 * 3695 * Arguments: un - driver soft state (unit) structure 3696 * id - table or config file vid/pid 3697 * idlen - length of the vid/pid (bytes) 3698 * 3699 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3700 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3701 */ 3702 3703 static int 3704 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3705 { 3706 char *p1; 3707 char *p2; 3708 int cnt; 3709 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3710 sizeof (SD_INQUIRY(un)->inq_pid); 3711 3712 ASSERT(un != NULL); 3713 p2 = un->un_sd->sd_inq->inq_vid; 3714 ASSERT(id != NULL); 3715 p1 = id; 3716 3717 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3718 /* 3719 * Note: string p1 is terminated by a NUL but string p2 3720 * isn't. The end of p2 is determined by cnt. 3721 */ 3722 for (;;) { 3723 /* skip over any extra blanks in both strings */ 3724 while ((*p1 != '\0') && (*p1 == ' ')) { 3725 p1++; 3726 } 3727 while ((cnt != 0) && (*p2 == ' ')) { 3728 p2++; 3729 cnt--; 3730 } 3731 3732 /* compare the two strings */ 3733 if ((cnt == 0) || 3734 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3735 break; 3736 } 3737 while ((cnt > 0) && 3738 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3739 p1++; 3740 p2++; 3741 cnt--; 3742 } 3743 } 3744 } 3745 3746 /* return SD_SUCCESS if both strings match */ 3747 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3748 } 3749 3750 3751 /* 3752 * Function: sd_chk_vers1_data 3753 * 3754 * Description: Verify the version 1 device properties provided by the 3755 * user via the configuration file 3756 * 3757 * Arguments: un - driver soft state (unit) structure 3758 * flags - integer mask indicating properties to be set 3759 * prop_list - integer list of property values 3760 * list_len - length of user provided data 3761 * 3762 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3763 * SD_FAILURE - Indicates the user provided data is invalid 3764 */ 3765 3766 static int 3767 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3768 int list_len, char *dataname_ptr) 3769 { 3770 int i; 3771 int mask = 1; 3772 int index = 0; 3773 3774 ASSERT(un != NULL); 3775 3776 /* Check for a NULL property name and list */ 3777 if (dataname_ptr == NULL) { 3778 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3779 "sd_chk_vers1_data: NULL data property name."); 3780 return (SD_FAILURE); 3781 } 3782 if (prop_list == NULL) { 3783 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3784 "sd_chk_vers1_data: %s NULL data property list.", 3785 dataname_ptr); 3786 return (SD_FAILURE); 3787 } 3788 3789 /* Display a warning if undefined bits are set in the flags */ 3790 if (flags & ~SD_CONF_BIT_MASK) { 3791 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3792 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3793 "Properties not set.", 3794 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3795 return (SD_FAILURE); 3796 } 3797 3798 /* 3799 * Verify the length of the list by identifying the highest bit set 3800 * in the flags and validating that the property list has a length 3801 * up to the index of this bit. 3802 */ 3803 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3804 if (flags & mask) { 3805 index++; 3806 } 3807 mask = 1 << i; 3808 } 3809 if ((list_len / sizeof (int)) < (index + 2)) { 3810 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3811 "sd_chk_vers1_data: " 3812 "Data property list %s size is incorrect. " 3813 "Properties not set.", dataname_ptr); 3814 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3815 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3816 return (SD_FAILURE); 3817 } 3818 return (SD_SUCCESS); 3819 } 3820 3821 3822 /* 3823 * Function: sd_set_vers1_properties 3824 * 3825 * Description: Set version 1 device properties based on a property list 3826 * retrieved from the driver configuration file or static 3827 * configuration table. Version 1 properties have the format: 3828 * 3829 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3830 * 3831 * where the prop0 value will be used to set prop0 if bit0 3832 * is set in the flags 3833 * 3834 * Arguments: un - driver soft state (unit) structure 3835 * flags - integer mask indicating properties to be set 3836 * prop_list - integer list of property values 3837 */ 3838 3839 static void 3840 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3841 { 3842 ASSERT(un != NULL); 3843 3844 /* 3845 * Set the flag to indicate cache is to be disabled. An attempt 3846 * to disable the cache via sd_cache_control() will be made 3847 * later during attach once the basic initialization is complete. 3848 */ 3849 if (flags & SD_CONF_BSET_NOCACHE) { 3850 un->un_f_opt_disable_cache = TRUE; 3851 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3852 "sd_set_vers1_properties: caching disabled flag set\n"); 3853 } 3854 3855 /* CD-specific configuration parameters */ 3856 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3857 un->un_f_cfg_playmsf_bcd = TRUE; 3858 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3859 "sd_set_vers1_properties: playmsf_bcd set\n"); 3860 } 3861 if (flags & SD_CONF_BSET_READSUB_BCD) { 3862 un->un_f_cfg_readsub_bcd = TRUE; 3863 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3864 "sd_set_vers1_properties: readsub_bcd set\n"); 3865 } 3866 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3867 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3868 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3869 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3870 } 3871 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3872 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3873 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3874 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3875 } 3876 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3877 un->un_f_cfg_no_read_header = TRUE; 3878 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3879 "sd_set_vers1_properties: no_read_header set\n"); 3880 } 3881 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3882 un->un_f_cfg_read_cd_xd4 = TRUE; 3883 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3884 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3885 } 3886 3887 /* Support for devices which do not have valid/unique serial numbers */ 3888 if (flags & SD_CONF_BSET_FAB_DEVID) { 3889 un->un_f_opt_fab_devid = TRUE; 3890 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3891 "sd_set_vers1_properties: fab_devid bit set\n"); 3892 } 3893 3894 /* Support for user throttle configuration */ 3895 if (flags & SD_CONF_BSET_THROTTLE) { 3896 ASSERT(prop_list != NULL); 3897 un->un_saved_throttle = un->un_throttle = 3898 prop_list->sdt_throttle; 3899 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3900 "sd_set_vers1_properties: throttle set to %d\n", 3901 prop_list->sdt_throttle); 3902 } 3903 3904 /* Set the per disk retry count according to the conf file or table. */ 3905 if (flags & SD_CONF_BSET_NRR_COUNT) { 3906 ASSERT(prop_list != NULL); 3907 if (prop_list->sdt_not_rdy_retries) { 3908 un->un_notready_retry_count = 3909 prop_list->sdt_not_rdy_retries; 3910 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3911 "sd_set_vers1_properties: not ready retry count" 3912 " set to %d\n", un->un_notready_retry_count); 3913 } 3914 } 3915 3916 /* The controller type is reported for generic disk driver ioctls */ 3917 if (flags & SD_CONF_BSET_CTYPE) { 3918 ASSERT(prop_list != NULL); 3919 switch (prop_list->sdt_ctype) { 3920 case CTYPE_CDROM: 3921 un->un_ctype = prop_list->sdt_ctype; 3922 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3923 "sd_set_vers1_properties: ctype set to " 3924 "CTYPE_CDROM\n"); 3925 break; 3926 case CTYPE_CCS: 3927 un->un_ctype = prop_list->sdt_ctype; 3928 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3929 "sd_set_vers1_properties: ctype set to " 3930 "CTYPE_CCS\n"); 3931 break; 3932 case CTYPE_ROD: /* RW optical */ 3933 un->un_ctype = prop_list->sdt_ctype; 3934 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3935 "sd_set_vers1_properties: ctype set to " 3936 "CTYPE_ROD\n"); 3937 break; 3938 default: 3939 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3940 "sd_set_vers1_properties: Could not set " 3941 "invalid ctype value (%d)", 3942 prop_list->sdt_ctype); 3943 } 3944 } 3945 3946 /* Purple failover timeout */ 3947 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3948 ASSERT(prop_list != NULL); 3949 un->un_busy_retry_count = 3950 prop_list->sdt_busy_retries; 3951 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3952 "sd_set_vers1_properties: " 3953 "busy retry count set to %d\n", 3954 un->un_busy_retry_count); 3955 } 3956 3957 /* Purple reset retry count */ 3958 if (flags & SD_CONF_BSET_RST_RETRIES) { 3959 ASSERT(prop_list != NULL); 3960 un->un_reset_retry_count = 3961 prop_list->sdt_reset_retries; 3962 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3963 "sd_set_vers1_properties: " 3964 "reset retry count set to %d\n", 3965 un->un_reset_retry_count); 3966 } 3967 3968 /* Purple reservation release timeout */ 3969 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 3970 ASSERT(prop_list != NULL); 3971 un->un_reserve_release_time = 3972 prop_list->sdt_reserv_rel_time; 3973 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3974 "sd_set_vers1_properties: " 3975 "reservation release timeout set to %d\n", 3976 un->un_reserve_release_time); 3977 } 3978 3979 /* 3980 * Driver flag telling the driver to verify that no commands are pending 3981 * for a device before issuing a Test Unit Ready. This is a workaround 3982 * for a firmware bug in some Seagate eliteI drives. 3983 */ 3984 if (flags & SD_CONF_BSET_TUR_CHECK) { 3985 un->un_f_cfg_tur_check = TRUE; 3986 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3987 "sd_set_vers1_properties: tur queue check set\n"); 3988 } 3989 3990 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 3991 un->un_min_throttle = prop_list->sdt_min_throttle; 3992 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3993 "sd_set_vers1_properties: min throttle set to %d\n", 3994 un->un_min_throttle); 3995 } 3996 3997 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 3998 un->un_f_disksort_disabled = 3999 (prop_list->sdt_disk_sort_dis != 0) ? 4000 TRUE : FALSE; 4001 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4002 "sd_set_vers1_properties: disksort disabled " 4003 "flag set to %d\n", 4004 prop_list->sdt_disk_sort_dis); 4005 } 4006 4007 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4008 un->un_f_lun_reset_enabled = 4009 (prop_list->sdt_lun_reset_enable != 0) ? 4010 TRUE : FALSE; 4011 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4012 "sd_set_vers1_properties: lun reset enabled " 4013 "flag set to %d\n", 4014 prop_list->sdt_lun_reset_enable); 4015 } 4016 4017 /* 4018 * Validate the throttle values. 4019 * If any of the numbers are invalid, set everything to defaults. 4020 */ 4021 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4022 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4023 (un->un_min_throttle > un->un_throttle)) { 4024 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4025 un->un_min_throttle = sd_min_throttle; 4026 } 4027 } 4028 4029 /* 4030 * Function: sd_is_lsi() 4031 * 4032 * Description: Check for lsi devices, step throught the static device 4033 * table to match vid/pid. 4034 * 4035 * Args: un - ptr to sd_lun 4036 * 4037 * Notes: When creating new LSI property, need to add the new LSI property 4038 * to this function. 4039 */ 4040 static void 4041 sd_is_lsi(struct sd_lun *un) 4042 { 4043 char *id = NULL; 4044 int table_index; 4045 int idlen; 4046 void *prop; 4047 4048 ASSERT(un != NULL); 4049 for (table_index = 0; table_index < sd_disk_table_size; 4050 table_index++) { 4051 id = sd_disk_table[table_index].device_id; 4052 idlen = strlen(id); 4053 if (idlen == 0) { 4054 continue; 4055 } 4056 4057 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4058 prop = sd_disk_table[table_index].properties; 4059 if (prop == &lsi_properties || 4060 prop == &lsi_oem_properties || 4061 prop == &lsi_properties_scsi || 4062 prop == &symbios_properties) { 4063 un->un_f_cfg_is_lsi = TRUE; 4064 } 4065 break; 4066 } 4067 } 4068 } 4069 4070 4071 /* 4072 * The following routines support reading and interpretation of disk labels, 4073 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4074 * fdisk tables. 4075 */ 4076 4077 /* 4078 * Function: sd_validate_geometry 4079 * 4080 * Description: Read the label from the disk (if present). Update the unit's 4081 * geometry and vtoc information from the data in the label. 4082 * Verify that the label is valid. 4083 * 4084 * Arguments: un - driver soft state (unit) structure 4085 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4086 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4087 * to use the USCSI "direct" chain and bypass the normal 4088 * command waitq. 4089 * 4090 * Return Code: 0 - Successful completion 4091 * EINVAL - Invalid value in un->un_tgt_blocksize or 4092 * un->un_blockcount; or label on disk is corrupted 4093 * or unreadable. 4094 * EACCES - Reservation conflict at the device. 4095 * ENOMEM - Resource allocation error 4096 * ENOTSUP - geometry not applicable 4097 * 4098 * Context: Kernel thread only (can sleep). 4099 */ 4100 4101 static int 4102 sd_validate_geometry(struct sd_lun *un, int path_flag) 4103 { 4104 static char labelstring[128]; 4105 static char buf[256]; 4106 char *label = NULL; 4107 int label_error = 0; 4108 int gvalid = un->un_f_geometry_is_valid; 4109 int lbasize; 4110 uint_t capacity; 4111 int count; 4112 4113 ASSERT(un != NULL); 4114 ASSERT(mutex_owned(SD_MUTEX(un))); 4115 4116 /* 4117 * If the required values are not valid, then try getting them 4118 * once via read capacity. If that fails, then fail this call. 4119 * This is necessary with the new mpxio failover behavior in 4120 * the T300 where we can get an attach for the inactive path 4121 * before the active path. The inactive path fails commands with 4122 * sense data of 02,04,88 which happens to the read capacity 4123 * before mpxio has had sufficient knowledge to know if it should 4124 * force a fail over or not. (Which it won't do at attach anyhow). 4125 * If the read capacity at attach time fails, un_tgt_blocksize and 4126 * un_blockcount won't be valid. 4127 */ 4128 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4129 (un->un_f_blockcount_is_valid != TRUE)) { 4130 uint64_t cap; 4131 uint32_t lbasz; 4132 int rval; 4133 4134 mutex_exit(SD_MUTEX(un)); 4135 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4136 &lbasz, SD_PATH_DIRECT); 4137 mutex_enter(SD_MUTEX(un)); 4138 if (rval == 0) { 4139 /* 4140 * The following relies on 4141 * sd_send_scsi_READ_CAPACITY never 4142 * returning 0 for capacity and/or lbasize. 4143 */ 4144 sd_update_block_info(un, lbasz, cap); 4145 } 4146 4147 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4148 (un->un_f_blockcount_is_valid != TRUE)) { 4149 return (EINVAL); 4150 } 4151 } 4152 4153 /* 4154 * Copy the lbasize and capacity so that if they're reset while we're 4155 * not holding the SD_MUTEX, we will continue to use valid values 4156 * after the SD_MUTEX is reacquired. (4119659) 4157 */ 4158 lbasize = un->un_tgt_blocksize; 4159 capacity = un->un_blockcount; 4160 4161 #if defined(_SUNOS_VTOC_16) 4162 /* 4163 * Set up the "whole disk" fdisk partition; this should always 4164 * exist, regardless of whether the disk contains an fdisk table 4165 * or vtoc. 4166 */ 4167 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4168 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4169 #endif 4170 4171 /* 4172 * Refresh the logical and physical geometry caches. 4173 * (data from MODE SENSE format/rigid disk geometry pages, 4174 * and scsi_ifgetcap("geometry"). 4175 */ 4176 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4177 4178 label_error = sd_use_efi(un, path_flag); 4179 if (label_error == 0) { 4180 /* found a valid EFI label */ 4181 SD_TRACE(SD_LOG_IO_PARTITION, un, 4182 "sd_validate_geometry: found EFI label\n"); 4183 un->un_solaris_offset = 0; 4184 un->un_solaris_size = capacity; 4185 return (ENOTSUP); 4186 } 4187 if (un->un_blockcount > DK_MAX_BLOCKS) { 4188 if (label_error == ESRCH) { 4189 /* 4190 * they've configured a LUN over 1TB, but used 4191 * format.dat to restrict format's view of the 4192 * capacity to be under 1TB 4193 */ 4194 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4195 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4196 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4197 "size to be < 1TB or relabel the disk with an EFI label"); 4198 } else { 4199 /* unlabeled disk over 1TB */ 4200 return (ENOTSUP); 4201 } 4202 } 4203 label_error = 0; 4204 4205 /* 4206 * at this point it is either labeled with a VTOC or it is 4207 * under 1TB 4208 */ 4209 if (un->un_f_vtoc_label_supported) { 4210 struct dk_label *dkl; 4211 offset_t dkl1; 4212 offset_t label_addr, real_addr; 4213 int rval; 4214 size_t buffer_size; 4215 4216 /* 4217 * Note: This will set up un->un_solaris_size and 4218 * un->un_solaris_offset. 4219 */ 4220 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4221 case SD_CMD_RESERVATION_CONFLICT: 4222 ASSERT(mutex_owned(SD_MUTEX(un))); 4223 return (EACCES); 4224 case SD_CMD_FAILURE: 4225 ASSERT(mutex_owned(SD_MUTEX(un))); 4226 return (ENOMEM); 4227 } 4228 4229 if (un->un_solaris_size <= DK_LABEL_LOC) { 4230 /* 4231 * Found fdisk table but no Solaris partition entry, 4232 * so don't call sd_uselabel() and don't create 4233 * a default label. 4234 */ 4235 label_error = 0; 4236 un->un_f_geometry_is_valid = TRUE; 4237 goto no_solaris_partition; 4238 } 4239 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4240 4241 /* 4242 * sys_blocksize != tgt_blocksize, need to re-adjust 4243 * blkno and save the index to beginning of dk_label 4244 */ 4245 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4246 buffer_size = SD_REQBYTES2TGTBYTES(un, 4247 sizeof (struct dk_label)); 4248 4249 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4250 "label_addr: 0x%x allocation size: 0x%x\n", 4251 label_addr, buffer_size); 4252 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4253 if (dkl == NULL) { 4254 return (ENOMEM); 4255 } 4256 4257 mutex_exit(SD_MUTEX(un)); 4258 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4259 path_flag); 4260 mutex_enter(SD_MUTEX(un)); 4261 4262 switch (rval) { 4263 case 0: 4264 /* 4265 * sd_uselabel will establish that the geometry 4266 * is valid. 4267 * For sys_blocksize != tgt_blocksize, need 4268 * to index into the beginning of dk_label 4269 */ 4270 dkl1 = (daddr_t)dkl 4271 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4272 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4273 path_flag) != SD_LABEL_IS_VALID) { 4274 label_error = EINVAL; 4275 } 4276 break; 4277 case EACCES: 4278 label_error = EACCES; 4279 break; 4280 default: 4281 label_error = EINVAL; 4282 break; 4283 } 4284 4285 kmem_free(dkl, buffer_size); 4286 4287 #if defined(_SUNOS_VTOC_8) 4288 label = (char *)un->un_asciilabel; 4289 #elif defined(_SUNOS_VTOC_16) 4290 label = (char *)un->un_vtoc.v_asciilabel; 4291 #else 4292 #error "No VTOC format defined." 4293 #endif 4294 } 4295 4296 /* 4297 * If a valid label was not found, AND if no reservation conflict 4298 * was detected, then go ahead and create a default label (4069506). 4299 */ 4300 4301 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4302 if (un->un_f_geometry_is_valid == FALSE) { 4303 sd_build_default_label(un); 4304 } 4305 label_error = 0; 4306 } 4307 4308 no_solaris_partition: 4309 if ((!un->un_f_has_removable_media || 4310 (un->un_f_has_removable_media && 4311 un->un_mediastate == DKIO_EJECTED)) && 4312 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4313 /* 4314 * Print out a message indicating who and what we are. 4315 * We do this only when we happen to really validate the 4316 * geometry. We may call sd_validate_geometry() at other 4317 * times, e.g., ioctl()'s like Get VTOC in which case we 4318 * don't want to print the label. 4319 * If the geometry is valid, print the label string, 4320 * else print vendor and product info, if available 4321 */ 4322 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4323 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4324 } else { 4325 mutex_enter(&sd_label_mutex); 4326 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4327 labelstring); 4328 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4329 &labelstring[64]); 4330 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4331 labelstring, &labelstring[64]); 4332 if (un->un_f_blockcount_is_valid == TRUE) { 4333 (void) sprintf(&buf[strlen(buf)], 4334 ", %llu %u byte blocks\n", 4335 (longlong_t)un->un_blockcount, 4336 un->un_tgt_blocksize); 4337 } else { 4338 (void) sprintf(&buf[strlen(buf)], 4339 ", (unknown capacity)\n"); 4340 } 4341 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4342 mutex_exit(&sd_label_mutex); 4343 } 4344 } 4345 4346 #if defined(_SUNOS_VTOC_16) 4347 /* 4348 * If we have valid geometry, set up the remaining fdisk partitions. 4349 * Note that dkl_cylno is not used for the fdisk map entries, so 4350 * we set it to an entirely bogus value. 4351 */ 4352 for (count = 0; count < FD_NUMPART; count++) { 4353 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4354 un->un_map[FDISK_P1 + count].dkl_nblk = 4355 un->un_fmap[count].fmap_nblk; 4356 4357 un->un_offset[FDISK_P1 + count] = 4358 un->un_fmap[count].fmap_start; 4359 } 4360 #endif 4361 4362 for (count = 0; count < NDKMAP; count++) { 4363 #if defined(_SUNOS_VTOC_8) 4364 struct dk_map *lp = &un->un_map[count]; 4365 un->un_offset[count] = 4366 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4367 #elif defined(_SUNOS_VTOC_16) 4368 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4369 4370 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4371 #else 4372 #error "No VTOC format defined." 4373 #endif 4374 } 4375 4376 return (label_error); 4377 } 4378 4379 4380 #if defined(_SUNOS_VTOC_16) 4381 /* 4382 * Macro: MAX_BLKS 4383 * 4384 * This macro is used for table entries where we need to have the largest 4385 * possible sector value for that head & SPT (sectors per track) 4386 * combination. Other entries for some smaller disk sizes are set by 4387 * convention to match those used by X86 BIOS usage. 4388 */ 4389 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4390 4391 /* 4392 * Function: sd_convert_geometry 4393 * 4394 * Description: Convert physical geometry into a dk_geom structure. In 4395 * other words, make sure we don't wrap 16-bit values. 4396 * e.g. converting from geom_cache to dk_geom 4397 * 4398 * Context: Kernel thread only 4399 */ 4400 static void 4401 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4402 { 4403 int i; 4404 static const struct chs_values { 4405 uint_t max_cap; /* Max Capacity for this HS. */ 4406 uint_t nhead; /* Heads to use. */ 4407 uint_t nsect; /* SPT to use. */ 4408 } CHS_values[] = { 4409 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4410 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4411 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4412 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4413 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4414 }; 4415 4416 /* Unlabeled SCSI floppy device */ 4417 if (capacity <= 0x1000) { 4418 un_g->dkg_nhead = 2; 4419 un_g->dkg_ncyl = 80; 4420 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4421 return; 4422 } 4423 4424 /* 4425 * For all devices we calculate cylinders using the 4426 * heads and sectors we assign based on capacity of the 4427 * device. The table is designed to be compatible with the 4428 * way other operating systems lay out fdisk tables for X86 4429 * and to insure that the cylinders never exceed 65535 to 4430 * prevent problems with X86 ioctls that report geometry. 4431 * We use SPT that are multiples of 63, since other OSes that 4432 * are not limited to 16-bits for cylinders stop at 63 SPT 4433 * we make do by using multiples of 63 SPT. 4434 * 4435 * Note than capacities greater than or equal to 1TB will simply 4436 * get the largest geometry from the table. This should be okay 4437 * since disks this large shouldn't be using CHS values anyway. 4438 */ 4439 for (i = 0; CHS_values[i].max_cap < capacity && 4440 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4441 ; 4442 4443 un_g->dkg_nhead = CHS_values[i].nhead; 4444 un_g->dkg_nsect = CHS_values[i].nsect; 4445 } 4446 #endif 4447 4448 4449 /* 4450 * Function: sd_resync_geom_caches 4451 * 4452 * Description: (Re)initialize both geometry caches: the virtual geometry 4453 * information is extracted from the HBA (the "geometry" 4454 * capability), and the physical geometry cache data is 4455 * generated by issuing MODE SENSE commands. 4456 * 4457 * Arguments: un - driver soft state (unit) structure 4458 * capacity - disk capacity in #blocks 4459 * lbasize - disk block size in bytes 4460 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4461 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4462 * to use the USCSI "direct" chain and bypass the normal 4463 * command waitq. 4464 * 4465 * Context: Kernel thread only (can sleep). 4466 */ 4467 4468 static void 4469 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4470 int path_flag) 4471 { 4472 struct geom_cache pgeom; 4473 struct geom_cache *pgeom_p = &pgeom; 4474 int spc; 4475 unsigned short nhead; 4476 unsigned short nsect; 4477 4478 ASSERT(un != NULL); 4479 ASSERT(mutex_owned(SD_MUTEX(un))); 4480 4481 /* 4482 * Ask the controller for its logical geometry. 4483 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4484 * then the lgeom cache will be invalid. 4485 */ 4486 sd_get_virtual_geometry(un, capacity, lbasize); 4487 4488 /* 4489 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4490 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4491 */ 4492 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4493 /* 4494 * Note: Perhaps this needs to be more adaptive? The rationale 4495 * is that, if there's no HBA geometry from the HBA driver, any 4496 * guess is good, since this is the physical geometry. If MODE 4497 * SENSE fails this gives a max cylinder size for non-LBA access 4498 */ 4499 nhead = 255; 4500 nsect = 63; 4501 } else { 4502 nhead = un->un_lgeom.g_nhead; 4503 nsect = un->un_lgeom.g_nsect; 4504 } 4505 4506 if (ISCD(un)) { 4507 pgeom_p->g_nhead = 1; 4508 pgeom_p->g_nsect = nsect * nhead; 4509 } else { 4510 pgeom_p->g_nhead = nhead; 4511 pgeom_p->g_nsect = nsect; 4512 } 4513 4514 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4515 pgeom_p->g_capacity = capacity; 4516 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4517 pgeom_p->g_acyl = 0; 4518 4519 /* 4520 * Retrieve fresh geometry data from the hardware, stash it 4521 * here temporarily before we rebuild the incore label. 4522 * 4523 * We want to use the MODE SENSE commands to derive the 4524 * physical geometry of the device, but if either command 4525 * fails, the logical geometry is used as the fallback for 4526 * disk label geometry. 4527 */ 4528 mutex_exit(SD_MUTEX(un)); 4529 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4530 mutex_enter(SD_MUTEX(un)); 4531 4532 /* 4533 * Now update the real copy while holding the mutex. This 4534 * way the global copy is never in an inconsistent state. 4535 */ 4536 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4537 4538 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4539 "(cached from lgeom)\n"); 4540 SD_INFO(SD_LOG_COMMON, un, 4541 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4542 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4543 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4544 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4545 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4546 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4547 un->un_pgeom.g_rpm); 4548 } 4549 4550 4551 /* 4552 * Function: sd_read_fdisk 4553 * 4554 * Description: utility routine to read the fdisk table. 4555 * 4556 * Arguments: un - driver soft state (unit) structure 4557 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4558 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4559 * to use the USCSI "direct" chain and bypass the normal 4560 * command waitq. 4561 * 4562 * Return Code: SD_CMD_SUCCESS 4563 * SD_CMD_FAILURE 4564 * 4565 * Context: Kernel thread only (can sleep). 4566 */ 4567 /* ARGSUSED */ 4568 static int 4569 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4570 { 4571 #if defined(_NO_FDISK_PRESENT) 4572 4573 un->un_solaris_offset = 0; 4574 un->un_solaris_size = capacity; 4575 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4576 return (SD_CMD_SUCCESS); 4577 4578 #elif defined(_FIRMWARE_NEEDS_FDISK) 4579 4580 struct ipart *fdp; 4581 struct mboot *mbp; 4582 struct ipart fdisk[FD_NUMPART]; 4583 int i; 4584 char sigbuf[2]; 4585 caddr_t bufp; 4586 int uidx; 4587 int rval; 4588 int lba = 0; 4589 uint_t solaris_offset; /* offset to solaris part. */ 4590 daddr_t solaris_size; /* size of solaris partition */ 4591 uint32_t blocksize; 4592 4593 ASSERT(un != NULL); 4594 ASSERT(mutex_owned(SD_MUTEX(un))); 4595 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4596 4597 blocksize = un->un_tgt_blocksize; 4598 4599 /* 4600 * Start off assuming no fdisk table 4601 */ 4602 solaris_offset = 0; 4603 solaris_size = capacity; 4604 4605 mutex_exit(SD_MUTEX(un)); 4606 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4607 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4608 mutex_enter(SD_MUTEX(un)); 4609 4610 if (rval != 0) { 4611 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4612 "sd_read_fdisk: fdisk read err\n"); 4613 kmem_free(bufp, blocksize); 4614 return (SD_CMD_FAILURE); 4615 } 4616 4617 mbp = (struct mboot *)bufp; 4618 4619 /* 4620 * The fdisk table does not begin on a 4-byte boundary within the 4621 * master boot record, so we copy it to an aligned structure to avoid 4622 * alignment exceptions on some processors. 4623 */ 4624 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4625 4626 /* 4627 * Check for lba support before verifying sig; sig might not be 4628 * there, say on a blank disk, but the max_chs mark may still 4629 * be present. 4630 * 4631 * Note: LBA support and BEFs are an x86-only concept but this 4632 * code should work OK on SPARC as well. 4633 */ 4634 4635 /* 4636 * First, check for lba-access-ok on root node (or prom root node) 4637 * if present there, don't need to search fdisk table. 4638 */ 4639 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4640 "lba-access-ok", 0) != 0) { 4641 /* All drives do LBA; don't search fdisk table */ 4642 lba = 1; 4643 } else { 4644 /* Okay, look for mark in fdisk table */ 4645 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4646 /* accumulate "lba" value from all partitions */ 4647 lba = (lba || sd_has_max_chs_vals(fdp)); 4648 } 4649 } 4650 4651 if (lba != 0) { 4652 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4653 4654 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4655 "lba-access-ok", 0) == 0) { 4656 /* not found; create it */ 4657 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4658 "lba-access-ok", (caddr_t)NULL, 0) != 4659 DDI_PROP_SUCCESS) { 4660 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4661 "sd_read_fdisk: Can't create lba property " 4662 "for instance %d\n", 4663 ddi_get_instance(SD_DEVINFO(un))); 4664 } 4665 } 4666 } 4667 4668 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4669 4670 /* 4671 * Endian-independent signature check 4672 */ 4673 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4674 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4675 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4676 "sd_read_fdisk: no fdisk\n"); 4677 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4678 rval = SD_CMD_SUCCESS; 4679 goto done; 4680 } 4681 4682 #ifdef SDDEBUG 4683 if (sd_level_mask & SD_LOGMASK_INFO) { 4684 fdp = fdisk; 4685 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4686 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4687 "numsect sysid bootid\n"); 4688 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4689 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4690 " %d: %8d %8d 0x%08x 0x%08x\n", 4691 i, fdp->relsect, fdp->numsect, 4692 fdp->systid, fdp->bootid); 4693 } 4694 } 4695 #endif 4696 4697 /* 4698 * Try to find the unix partition 4699 */ 4700 uidx = -1; 4701 solaris_offset = 0; 4702 solaris_size = 0; 4703 4704 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4705 int relsect; 4706 int numsect; 4707 4708 if (fdp->numsect == 0) { 4709 un->un_fmap[i].fmap_start = 0; 4710 un->un_fmap[i].fmap_nblk = 0; 4711 continue; 4712 } 4713 4714 /* 4715 * Data in the fdisk table is little-endian. 4716 */ 4717 relsect = LE_32(fdp->relsect); 4718 numsect = LE_32(fdp->numsect); 4719 4720 un->un_fmap[i].fmap_start = relsect; 4721 un->un_fmap[i].fmap_nblk = numsect; 4722 4723 if (fdp->systid != SUNIXOS && 4724 fdp->systid != SUNIXOS2 && 4725 fdp->systid != EFI_PMBR) { 4726 continue; 4727 } 4728 4729 /* 4730 * use the last active solaris partition id found 4731 * (there should only be 1 active partition id) 4732 * 4733 * if there are no active solaris partition id 4734 * then use the first inactive solaris partition id 4735 */ 4736 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4737 uidx = i; 4738 solaris_offset = relsect; 4739 solaris_size = numsect; 4740 } 4741 } 4742 4743 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4744 un->un_solaris_offset, un->un_solaris_size); 4745 4746 rval = SD_CMD_SUCCESS; 4747 4748 done: 4749 4750 /* 4751 * Clear the VTOC info, only if the Solaris partition entry 4752 * has moved, changed size, been deleted, or if the size of 4753 * the partition is too small to even fit the label sector. 4754 */ 4755 if ((un->un_solaris_offset != solaris_offset) || 4756 (un->un_solaris_size != solaris_size) || 4757 solaris_size <= DK_LABEL_LOC) { 4758 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4759 solaris_offset, solaris_size); 4760 bzero(&un->un_g, sizeof (struct dk_geom)); 4761 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4762 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4763 un->un_f_geometry_is_valid = FALSE; 4764 } 4765 un->un_solaris_offset = solaris_offset; 4766 un->un_solaris_size = solaris_size; 4767 kmem_free(bufp, blocksize); 4768 return (rval); 4769 4770 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4771 #error "fdisk table presence undetermined for this platform." 4772 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4773 } 4774 4775 4776 /* 4777 * Function: sd_get_physical_geometry 4778 * 4779 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4780 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4781 * target, and use this information to initialize the physical 4782 * geometry cache specified by pgeom_p. 4783 * 4784 * MODE SENSE is an optional command, so failure in this case 4785 * does not necessarily denote an error. We want to use the 4786 * MODE SENSE commands to derive the physical geometry of the 4787 * device, but if either command fails, the logical geometry is 4788 * used as the fallback for disk label geometry. 4789 * 4790 * This requires that un->un_blockcount and un->un_tgt_blocksize 4791 * have already been initialized for the current target and 4792 * that the current values be passed as args so that we don't 4793 * end up ever trying to use -1 as a valid value. This could 4794 * happen if either value is reset while we're not holding 4795 * the mutex. 4796 * 4797 * Arguments: un - driver soft state (unit) structure 4798 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4799 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4800 * to use the USCSI "direct" chain and bypass the normal 4801 * command waitq. 4802 * 4803 * Context: Kernel thread only (can sleep). 4804 */ 4805 4806 static void 4807 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4808 int capacity, int lbasize, int path_flag) 4809 { 4810 struct mode_format *page3p; 4811 struct mode_geometry *page4p; 4812 struct mode_header *headerp; 4813 int sector_size; 4814 int nsect; 4815 int nhead; 4816 int ncyl; 4817 int intrlv; 4818 int spc; 4819 int modesense_capacity; 4820 int rpm; 4821 int bd_len; 4822 int mode_header_length; 4823 uchar_t *p3bufp; 4824 uchar_t *p4bufp; 4825 int cdbsize; 4826 4827 ASSERT(un != NULL); 4828 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4829 4830 if (un->un_f_blockcount_is_valid != TRUE) { 4831 return; 4832 } 4833 4834 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4835 return; 4836 } 4837 4838 if (lbasize == 0) { 4839 if (ISCD(un)) { 4840 lbasize = 2048; 4841 } else { 4842 lbasize = un->un_sys_blocksize; 4843 } 4844 } 4845 pgeom_p->g_secsize = (unsigned short)lbasize; 4846 4847 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4848 4849 /* 4850 * Retrieve MODE SENSE page 3 - Format Device Page 4851 */ 4852 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4853 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4854 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4855 != 0) { 4856 SD_ERROR(SD_LOG_COMMON, un, 4857 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4858 goto page3_exit; 4859 } 4860 4861 /* 4862 * Determine size of Block Descriptors in order to locate the mode 4863 * page data. ATAPI devices return 0, SCSI devices should return 4864 * MODE_BLK_DESC_LENGTH. 4865 */ 4866 headerp = (struct mode_header *)p3bufp; 4867 if (un->un_f_cfg_is_atapi == TRUE) { 4868 struct mode_header_grp2 *mhp = 4869 (struct mode_header_grp2 *)headerp; 4870 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4871 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4872 } else { 4873 mode_header_length = MODE_HEADER_LENGTH; 4874 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4875 } 4876 4877 if (bd_len > MODE_BLK_DESC_LENGTH) { 4878 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4879 "received unexpected bd_len of %d, page3\n", bd_len); 4880 goto page3_exit; 4881 } 4882 4883 page3p = (struct mode_format *) 4884 ((caddr_t)headerp + mode_header_length + bd_len); 4885 4886 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4887 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4888 "mode sense pg3 code mismatch %d\n", 4889 page3p->mode_page.code); 4890 goto page3_exit; 4891 } 4892 4893 /* 4894 * Use this physical geometry data only if BOTH MODE SENSE commands 4895 * complete successfully; otherwise, revert to the logical geometry. 4896 * So, we need to save everything in temporary variables. 4897 */ 4898 sector_size = BE_16(page3p->data_bytes_sect); 4899 4900 /* 4901 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4902 */ 4903 if (sector_size == 0) { 4904 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 4905 } else { 4906 sector_size &= ~(un->un_sys_blocksize - 1); 4907 } 4908 4909 nsect = BE_16(page3p->sect_track); 4910 intrlv = BE_16(page3p->interleave); 4911 4912 SD_INFO(SD_LOG_COMMON, un, 4913 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4914 SD_INFO(SD_LOG_COMMON, un, 4915 " mode page: %d; nsect: %d; sector size: %d;\n", 4916 page3p->mode_page.code, nsect, sector_size); 4917 SD_INFO(SD_LOG_COMMON, un, 4918 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4919 BE_16(page3p->track_skew), 4920 BE_16(page3p->cylinder_skew)); 4921 4922 4923 /* 4924 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4925 */ 4926 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4927 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4928 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4929 != 0) { 4930 SD_ERROR(SD_LOG_COMMON, un, 4931 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4932 goto page4_exit; 4933 } 4934 4935 /* 4936 * Determine size of Block Descriptors in order to locate the mode 4937 * page data. ATAPI devices return 0, SCSI devices should return 4938 * MODE_BLK_DESC_LENGTH. 4939 */ 4940 headerp = (struct mode_header *)p4bufp; 4941 if (un->un_f_cfg_is_atapi == TRUE) { 4942 struct mode_header_grp2 *mhp = 4943 (struct mode_header_grp2 *)headerp; 4944 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4945 } else { 4946 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4947 } 4948 4949 if (bd_len > MODE_BLK_DESC_LENGTH) { 4950 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4951 "received unexpected bd_len of %d, page4\n", bd_len); 4952 goto page4_exit; 4953 } 4954 4955 page4p = (struct mode_geometry *) 4956 ((caddr_t)headerp + mode_header_length + bd_len); 4957 4958 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4959 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4960 "mode sense pg4 code mismatch %d\n", 4961 page4p->mode_page.code); 4962 goto page4_exit; 4963 } 4964 4965 /* 4966 * Stash the data now, after we know that both commands completed. 4967 */ 4968 4969 mutex_enter(SD_MUTEX(un)); 4970 4971 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4972 spc = nhead * nsect; 4973 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4974 rpm = BE_16(page4p->rpm); 4975 4976 modesense_capacity = spc * ncyl; 4977 4978 SD_INFO(SD_LOG_COMMON, un, 4979 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4980 SD_INFO(SD_LOG_COMMON, un, 4981 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4982 SD_INFO(SD_LOG_COMMON, un, 4983 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4984 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4985 (void *)pgeom_p, capacity); 4986 4987 /* 4988 * Compensate if the drive's geometry is not rectangular, i.e., 4989 * the product of C * H * S returned by MODE SENSE >= that returned 4990 * by read capacity. This is an idiosyncrasy of the original x86 4991 * disk subsystem. 4992 */ 4993 if (modesense_capacity >= capacity) { 4994 SD_INFO(SD_LOG_COMMON, un, 4995 "sd_get_physical_geometry: adjusting acyl; " 4996 "old: %d; new: %d\n", pgeom_p->g_acyl, 4997 (modesense_capacity - capacity + spc - 1) / spc); 4998 if (sector_size != 0) { 4999 /* 1243403: NEC D38x7 drives don't support sec size */ 5000 pgeom_p->g_secsize = (unsigned short)sector_size; 5001 } 5002 pgeom_p->g_nsect = (unsigned short)nsect; 5003 pgeom_p->g_nhead = (unsigned short)nhead; 5004 pgeom_p->g_capacity = capacity; 5005 pgeom_p->g_acyl = 5006 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5007 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5008 } 5009 5010 pgeom_p->g_rpm = (unsigned short)rpm; 5011 pgeom_p->g_intrlv = (unsigned short)intrlv; 5012 5013 SD_INFO(SD_LOG_COMMON, un, 5014 "sd_get_physical_geometry: mode sense geometry:\n"); 5015 SD_INFO(SD_LOG_COMMON, un, 5016 " nsect: %d; sector size: %d; interlv: %d\n", 5017 nsect, sector_size, intrlv); 5018 SD_INFO(SD_LOG_COMMON, un, 5019 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5020 nhead, ncyl, rpm, modesense_capacity); 5021 SD_INFO(SD_LOG_COMMON, un, 5022 "sd_get_physical_geometry: (cached)\n"); 5023 SD_INFO(SD_LOG_COMMON, un, 5024 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5025 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5026 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5027 SD_INFO(SD_LOG_COMMON, un, 5028 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5029 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5030 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5031 5032 mutex_exit(SD_MUTEX(un)); 5033 5034 page4_exit: 5035 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5036 page3_exit: 5037 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5038 } 5039 5040 5041 /* 5042 * Function: sd_get_virtual_geometry 5043 * 5044 * Description: Ask the controller to tell us about the target device. 5045 * 5046 * Arguments: un - pointer to softstate 5047 * capacity - disk capacity in #blocks 5048 * lbasize - disk block size in bytes 5049 * 5050 * Context: Kernel thread only 5051 */ 5052 5053 static void 5054 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5055 { 5056 struct geom_cache *lgeom_p = &un->un_lgeom; 5057 uint_t geombuf; 5058 int spc; 5059 5060 ASSERT(un != NULL); 5061 ASSERT(mutex_owned(SD_MUTEX(un))); 5062 5063 mutex_exit(SD_MUTEX(un)); 5064 5065 /* Set sector size, and total number of sectors */ 5066 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5067 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5068 5069 /* Let the HBA tell us its geometry */ 5070 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5071 5072 mutex_enter(SD_MUTEX(un)); 5073 5074 /* A value of -1 indicates an undefined "geometry" property */ 5075 if (geombuf == (-1)) { 5076 return; 5077 } 5078 5079 /* Initialize the logical geometry cache. */ 5080 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5081 lgeom_p->g_nsect = geombuf & 0xffff; 5082 lgeom_p->g_secsize = un->un_sys_blocksize; 5083 5084 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5085 5086 /* 5087 * Note: The driver originally converted the capacity value from 5088 * target blocks to system blocks. However, the capacity value passed 5089 * to this routine is already in terms of system blocks (this scaling 5090 * is done when the READ CAPACITY command is issued and processed). 5091 * This 'error' may have gone undetected because the usage of g_ncyl 5092 * (which is based upon g_capacity) is very limited within the driver 5093 */ 5094 lgeom_p->g_capacity = capacity; 5095 5096 /* 5097 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5098 * hba may return zero values if the device has been removed. 5099 */ 5100 if (spc == 0) { 5101 lgeom_p->g_ncyl = 0; 5102 } else { 5103 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5104 } 5105 lgeom_p->g_acyl = 0; 5106 5107 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5108 SD_INFO(SD_LOG_COMMON, un, 5109 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5110 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5111 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5112 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5113 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5114 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5115 } 5116 5117 5118 /* 5119 * Function: sd_update_block_info 5120 * 5121 * Description: Calculate a byte count to sector count bitshift value 5122 * from sector size. 5123 * 5124 * Arguments: un: unit struct. 5125 * lbasize: new target sector size 5126 * capacity: new target capacity, ie. block count 5127 * 5128 * Context: Kernel thread context 5129 */ 5130 5131 static void 5132 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5133 { 5134 if (lbasize != 0) { 5135 un->un_tgt_blocksize = lbasize; 5136 un->un_f_tgt_blocksize_is_valid = TRUE; 5137 } 5138 5139 if (capacity != 0) { 5140 un->un_blockcount = capacity; 5141 un->un_f_blockcount_is_valid = TRUE; 5142 } 5143 } 5144 5145 5146 static void 5147 sd_swap_efi_gpt(efi_gpt_t *e) 5148 { 5149 _NOTE(ASSUMING_PROTECTED(*e)) 5150 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5151 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5152 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5153 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5154 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5155 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5156 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5157 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5158 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5159 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5160 e->efi_gpt_NumberOfPartitionEntries = 5161 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5162 e->efi_gpt_SizeOfPartitionEntry = 5163 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5164 e->efi_gpt_PartitionEntryArrayCRC32 = 5165 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5166 } 5167 5168 static void 5169 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5170 { 5171 int i; 5172 5173 _NOTE(ASSUMING_PROTECTED(*p)) 5174 for (i = 0; i < nparts; i++) { 5175 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5176 p[i].efi_gpe_PartitionTypeGUID); 5177 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5178 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5179 /* PartitionAttrs */ 5180 } 5181 } 5182 5183 static int 5184 sd_validate_efi(efi_gpt_t *labp) 5185 { 5186 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5187 return (EINVAL); 5188 /* at least 96 bytes in this version of the spec. */ 5189 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5190 labp->efi_gpt_HeaderSize) 5191 return (EINVAL); 5192 /* this should be 128 bytes */ 5193 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5194 return (EINVAL); 5195 return (0); 5196 } 5197 5198 static int 5199 sd_use_efi(struct sd_lun *un, int path_flag) 5200 { 5201 int i; 5202 int rval = 0; 5203 efi_gpe_t *partitions; 5204 uchar_t *buf; 5205 uint_t lbasize; 5206 uint64_t cap; 5207 uint_t nparts; 5208 diskaddr_t gpe_lba; 5209 5210 ASSERT(mutex_owned(SD_MUTEX(un))); 5211 lbasize = un->un_tgt_blocksize; 5212 5213 mutex_exit(SD_MUTEX(un)); 5214 5215 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5216 5217 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5218 rval = EINVAL; 5219 goto done_err; 5220 } 5221 5222 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5223 if (rval) { 5224 goto done_err; 5225 } 5226 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5227 /* not ours */ 5228 rval = ESRCH; 5229 goto done_err; 5230 } 5231 5232 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5233 if (rval) { 5234 goto done_err; 5235 } 5236 sd_swap_efi_gpt((efi_gpt_t *)buf); 5237 5238 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5239 /* 5240 * Couldn't read the primary, try the backup. Our 5241 * capacity at this point could be based on CHS, so 5242 * check what the device reports. 5243 */ 5244 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5245 path_flag); 5246 if (rval) { 5247 goto done_err; 5248 } 5249 5250 /* 5251 * The MMC standard allows READ CAPACITY to be 5252 * inaccurate by a bounded amount (in the interest of 5253 * response latency). As a result, failed READs are 5254 * commonplace (due to the reading of metadata and not 5255 * data). Depending on the per-Vendor/drive Sense data, 5256 * the failed READ can cause many (unnecessary) retries. 5257 */ 5258 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5259 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5260 path_flag)) != 0) { 5261 goto done_err; 5262 } 5263 5264 sd_swap_efi_gpt((efi_gpt_t *)buf); 5265 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5266 goto done_err; 5267 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5268 "primary label corrupt; using backup\n"); 5269 } 5270 5271 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5272 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5273 5274 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5275 path_flag); 5276 if (rval) { 5277 goto done_err; 5278 } 5279 partitions = (efi_gpe_t *)buf; 5280 5281 if (nparts > MAXPART) { 5282 nparts = MAXPART; 5283 } 5284 sd_swap_efi_gpe(nparts, partitions); 5285 5286 mutex_enter(SD_MUTEX(un)); 5287 5288 /* Fill in partition table. */ 5289 for (i = 0; i < nparts; i++) { 5290 if (partitions->efi_gpe_StartingLBA != 0 || 5291 partitions->efi_gpe_EndingLBA != 0) { 5292 un->un_map[i].dkl_cylno = 5293 partitions->efi_gpe_StartingLBA; 5294 un->un_map[i].dkl_nblk = 5295 partitions->efi_gpe_EndingLBA - 5296 partitions->efi_gpe_StartingLBA + 1; 5297 un->un_offset[i] = 5298 partitions->efi_gpe_StartingLBA; 5299 } 5300 if (i == WD_NODE) { 5301 /* 5302 * minor number 7 corresponds to the whole disk 5303 */ 5304 un->un_map[i].dkl_cylno = 0; 5305 un->un_map[i].dkl_nblk = un->un_blockcount; 5306 un->un_offset[i] = 0; 5307 } 5308 partitions++; 5309 } 5310 un->un_solaris_offset = 0; 5311 un->un_solaris_size = cap; 5312 un->un_f_geometry_is_valid = TRUE; 5313 5314 /* clear the vtoc label */ 5315 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5316 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 /* 8284 * Here it's not necessary to check the case: 8285 * the capacity of the device is bigger than 8286 * what the max hba cdb can support. Because 8287 * sd_send_scsi_READ_CAPACITY will retrieve 8288 * the capacity by sending USCSI command, which 8289 * is constrained by the max hba cdb. Actually, 8290 * sd_send_scsi_READ_CAPACITY will return 8291 * EINVAL when using bigger cdb than required 8292 * cdb length. Will handle this case in 8293 * "case EINVAL". 8294 */ 8295 8296 /* 8297 * The following relies on 8298 * sd_send_scsi_READ_CAPACITY never 8299 * returning 0 for capacity and/or lbasize. 8300 */ 8301 sd_update_block_info(un, lbasize, capacity); 8302 8303 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8304 "sd_unit_attach: un:0x%p capacity = %ld " 8305 "blocks; lbasize= %ld.\n", un, 8306 un->un_blockcount, un->un_tgt_blocksize); 8307 8308 break; 8309 } 8310 case EINVAL: 8311 /* 8312 * In the case where the max-cdb-length property 8313 * is smaller than the required CDB length for 8314 * a SCSI device, a target driver can fail to 8315 * attach to that device. 8316 */ 8317 scsi_log(SD_DEVINFO(un), 8318 sd_label, CE_WARN, 8319 "disk capacity is too large " 8320 "for current cdb length"); 8321 goto spinup_failed; 8322 case EACCES: 8323 /* 8324 * Should never get here if the spin-up 8325 * succeeded, but code it in anyway. 8326 * From here, just continue with the attach... 8327 */ 8328 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8329 "sd_unit_attach: un:0x%p " 8330 "sd_send_scsi_READ_CAPACITY " 8331 "returned reservation conflict\n", un); 8332 reservation_flag = SD_TARGET_IS_RESERVED; 8333 break; 8334 default: 8335 /* 8336 * Likewise, should never get here if the 8337 * spin-up succeeded. Just continue with 8338 * the attach... 8339 */ 8340 break; 8341 } 8342 break; 8343 case EACCES: 8344 /* 8345 * Device is reserved by another host. In this case 8346 * we could not spin it up or read the capacity, but 8347 * we continue with the attach anyway. 8348 */ 8349 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8350 "sd_unit_attach: un:0x%p spin-up reservation " 8351 "conflict.\n", un); 8352 reservation_flag = SD_TARGET_IS_RESERVED; 8353 break; 8354 default: 8355 /* Fail the attach if the spin-up failed. */ 8356 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8357 "sd_unit_attach: un:0x%p spin-up failed.", un); 8358 goto spinup_failed; 8359 } 8360 } 8361 8362 /* 8363 * Check to see if this is a MMC drive 8364 */ 8365 if (ISCD(un)) { 8366 sd_set_mmc_caps(un); 8367 } 8368 8369 /* 8370 * Create the minor nodes for the device. 8371 * Note: If we want to support fdisk on both sparc and intel, this will 8372 * have to separate out the notion that VTOC8 is always sparc, and 8373 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8374 * type will have to be determined at run-time, and the fdisk 8375 * partitioning will have to have been read & set up before we 8376 * create the minor nodes. (any other inits (such as kstats) that 8377 * also ought to be done before creating the minor nodes?) (Doesn't 8378 * setting up the minor nodes kind of imply that we're ready to 8379 * handle an open from userland?) 8380 */ 8381 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8382 goto create_minor_nodes_failed; 8383 } 8384 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8385 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8386 8387 /* 8388 * Add a zero-length attribute to tell the world we support 8389 * kernel ioctls (for layered drivers) 8390 */ 8391 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8392 DDI_KERNEL_IOCTL, NULL, 0); 8393 8394 /* 8395 * Add a boolean property to tell the world we support 8396 * the B_FAILFAST flag (for layered drivers) 8397 */ 8398 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8399 "ddi-failfast-supported", NULL, 0); 8400 8401 /* 8402 * Initialize power management 8403 */ 8404 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8405 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8406 sd_setup_pm(un, devi); 8407 if (un->un_f_pm_is_enabled == FALSE) { 8408 /* 8409 * For performance, point to a jump table that does 8410 * not include pm. 8411 * The direct and priority chains don't change with PM. 8412 * 8413 * Note: this is currently done based on individual device 8414 * capabilities. When an interface for determining system 8415 * power enabled state becomes available, or when additional 8416 * layers are added to the command chain, these values will 8417 * have to be re-evaluated for correctness. 8418 */ 8419 if (un->un_f_non_devbsize_supported) { 8420 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8421 } else { 8422 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8423 } 8424 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8425 } 8426 8427 /* 8428 * This property is set to 0 by HA software to avoid retries 8429 * on a reserved disk. (The preferred property name is 8430 * "retry-on-reservation-conflict") (1189689) 8431 * 8432 * Note: The use of a global here can have unintended consequences. A 8433 * per instance variable is preferrable to match the capabilities of 8434 * different underlying hba's (4402600) 8435 */ 8436 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8437 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8438 sd_retry_on_reservation_conflict); 8439 if (sd_retry_on_reservation_conflict != 0) { 8440 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8441 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8442 sd_retry_on_reservation_conflict); 8443 } 8444 8445 /* Set up options for QFULL handling. */ 8446 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8447 "qfull-retries", -1)) != -1) { 8448 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8449 rval, 1); 8450 } 8451 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8452 "qfull-retry-interval", -1)) != -1) { 8453 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8454 rval, 1); 8455 } 8456 8457 /* 8458 * This just prints a message that announces the existence of the 8459 * device. The message is always printed in the system logfile, but 8460 * only appears on the console if the system is booted with the 8461 * -v (verbose) argument. 8462 */ 8463 ddi_report_dev(devi); 8464 8465 /* 8466 * The framework calls driver attach routines single-threaded 8467 * for a given instance. However we still acquire SD_MUTEX here 8468 * because this required for calling the sd_validate_geometry() 8469 * and sd_register_devid() functions. 8470 */ 8471 mutex_enter(SD_MUTEX(un)); 8472 un->un_f_geometry_is_valid = FALSE; 8473 un->un_mediastate = DKIO_NONE; 8474 un->un_reserved = -1; 8475 8476 /* 8477 * Read and validate the device's geometry (ie, disk label) 8478 * A new unformatted drive will not have a valid geometry, but 8479 * the driver needs to successfully attach to this device so 8480 * the drive can be formatted via ioctls. 8481 */ 8482 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8483 ENOTSUP)) && 8484 (un->un_blockcount < DK_MAX_BLOCKS)) { 8485 /* 8486 * We found a small disk with an EFI label on it; 8487 * we need to fix up the minor nodes accordingly. 8488 */ 8489 ddi_remove_minor_node(devi, "h"); 8490 ddi_remove_minor_node(devi, "h,raw"); 8491 (void) ddi_create_minor_node(devi, "wd", 8492 S_IFBLK, 8493 (instance << SDUNIT_SHIFT) | WD_NODE, 8494 un->un_node_type, NULL); 8495 (void) ddi_create_minor_node(devi, "wd,raw", 8496 S_IFCHR, 8497 (instance << SDUNIT_SHIFT) | WD_NODE, 8498 un->un_node_type, NULL); 8499 } 8500 8501 /* 8502 * Read and initialize the devid for the unit. 8503 */ 8504 ASSERT(un->un_errstats != NULL); 8505 if (un->un_f_devid_supported) { 8506 sd_register_devid(un, devi, reservation_flag); 8507 } 8508 mutex_exit(SD_MUTEX(un)); 8509 8510 #if (defined(__fibre)) 8511 /* 8512 * Register callbacks for fibre only. You can't do this soley 8513 * on the basis of the devid_type because this is hba specific. 8514 * We need to query our hba capabilities to find out whether to 8515 * register or not. 8516 */ 8517 if (un->un_f_is_fibre) { 8518 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8519 sd_init_event_callbacks(un); 8520 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8521 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8522 } 8523 } 8524 #endif 8525 8526 if (un->un_f_opt_disable_cache == TRUE) { 8527 /* 8528 * Disable both read cache and write cache. This is 8529 * the historic behavior of the keywords in the config file. 8530 */ 8531 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8532 0) { 8533 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8534 "sd_unit_attach: un:0x%p Could not disable " 8535 "caching", un); 8536 goto devid_failed; 8537 } 8538 } 8539 8540 /* 8541 * Check the value of the WCE bit now and 8542 * set un_f_write_cache_enabled accordingly. 8543 */ 8544 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8545 mutex_enter(SD_MUTEX(un)); 8546 un->un_f_write_cache_enabled = (wc_enabled != 0); 8547 mutex_exit(SD_MUTEX(un)); 8548 8549 /* 8550 * Set the pstat and error stat values here, so data obtained during the 8551 * previous attach-time routines is available. 8552 * 8553 * Note: This is a critical sequence that needs to be maintained: 8554 * 1) Instantiate the kstats before any routines using the iopath 8555 * (i.e. sd_send_scsi_cmd). 8556 * 2) Initialize the error stats (sd_set_errstats) and partition 8557 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8558 * sd_register_devid(), and sd_cache_control(). 8559 */ 8560 if (un->un_f_pkstats_enabled) { 8561 sd_set_pstats(un); 8562 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8563 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8564 } 8565 8566 sd_set_errstats(un); 8567 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8568 "sd_unit_attach: un:0x%p errstats set\n", un); 8569 8570 /* 8571 * Find out what type of reservation this disk supports. 8572 */ 8573 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8574 case 0: 8575 /* 8576 * SCSI-3 reservations are supported. 8577 */ 8578 un->un_reservation_type = SD_SCSI3_RESERVATION; 8579 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8580 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8581 break; 8582 case ENOTSUP: 8583 /* 8584 * The PERSISTENT RESERVE IN command would not be recognized by 8585 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8586 */ 8587 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8588 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8589 un->un_reservation_type = SD_SCSI2_RESERVATION; 8590 break; 8591 default: 8592 /* 8593 * default to SCSI-3 reservations 8594 */ 8595 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8596 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8597 un->un_reservation_type = SD_SCSI3_RESERVATION; 8598 break; 8599 } 8600 8601 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8602 "sd_unit_attach: un:0x%p exit success\n", un); 8603 8604 return (DDI_SUCCESS); 8605 8606 /* 8607 * An error occurred during the attach; clean up & return failure. 8608 */ 8609 8610 devid_failed: 8611 8612 setup_pm_failed: 8613 ddi_remove_minor_node(devi, NULL); 8614 8615 create_minor_nodes_failed: 8616 /* 8617 * Cleanup from the scsi_ifsetcap() calls (437868) 8618 */ 8619 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8620 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8621 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8622 8623 if (un->un_f_is_fibre == FALSE) { 8624 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8625 } 8626 8627 spinup_failed: 8628 8629 mutex_enter(SD_MUTEX(un)); 8630 8631 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8632 if (un->un_direct_priority_timeid != NULL) { 8633 timeout_id_t temp_id = un->un_direct_priority_timeid; 8634 un->un_direct_priority_timeid = NULL; 8635 mutex_exit(SD_MUTEX(un)); 8636 (void) untimeout(temp_id); 8637 mutex_enter(SD_MUTEX(un)); 8638 } 8639 8640 /* Cancel any pending start/stop timeouts */ 8641 if (un->un_startstop_timeid != NULL) { 8642 timeout_id_t temp_id = un->un_startstop_timeid; 8643 un->un_startstop_timeid = NULL; 8644 mutex_exit(SD_MUTEX(un)); 8645 (void) untimeout(temp_id); 8646 mutex_enter(SD_MUTEX(un)); 8647 } 8648 8649 /* Cancel any pending reset-throttle timeouts */ 8650 if (un->un_reset_throttle_timeid != NULL) { 8651 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8652 un->un_reset_throttle_timeid = NULL; 8653 mutex_exit(SD_MUTEX(un)); 8654 (void) untimeout(temp_id); 8655 mutex_enter(SD_MUTEX(un)); 8656 } 8657 8658 /* Cancel any pending retry timeouts */ 8659 if (un->un_retry_timeid != NULL) { 8660 timeout_id_t temp_id = un->un_retry_timeid; 8661 un->un_retry_timeid = NULL; 8662 mutex_exit(SD_MUTEX(un)); 8663 (void) untimeout(temp_id); 8664 mutex_enter(SD_MUTEX(un)); 8665 } 8666 8667 /* Cancel any pending delayed cv broadcast timeouts */ 8668 if (un->un_dcvb_timeid != NULL) { 8669 timeout_id_t temp_id = un->un_dcvb_timeid; 8670 un->un_dcvb_timeid = NULL; 8671 mutex_exit(SD_MUTEX(un)); 8672 (void) untimeout(temp_id); 8673 mutex_enter(SD_MUTEX(un)); 8674 } 8675 8676 mutex_exit(SD_MUTEX(un)); 8677 8678 /* There should not be any in-progress I/O so ASSERT this check */ 8679 ASSERT(un->un_ncmds_in_transport == 0); 8680 ASSERT(un->un_ncmds_in_driver == 0); 8681 8682 /* Do not free the softstate if the callback routine is active */ 8683 sd_sync_with_callback(un); 8684 8685 /* 8686 * Partition stats apparently are not used with removables. These would 8687 * not have been created during attach, so no need to clean them up... 8688 */ 8689 if (un->un_stats != NULL) { 8690 kstat_delete(un->un_stats); 8691 un->un_stats = NULL; 8692 } 8693 if (un->un_errstats != NULL) { 8694 kstat_delete(un->un_errstats); 8695 un->un_errstats = NULL; 8696 } 8697 8698 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8699 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8700 8701 ddi_prop_remove_all(devi); 8702 sema_destroy(&un->un_semoclose); 8703 cv_destroy(&un->un_state_cv); 8704 8705 getrbuf_failed: 8706 8707 sd_free_rqs(un); 8708 8709 alloc_rqs_failed: 8710 8711 devp->sd_private = NULL; 8712 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8713 8714 get_softstate_failed: 8715 /* 8716 * Note: the man pages are unclear as to whether or not doing a 8717 * ddi_soft_state_free(sd_state, instance) is the right way to 8718 * clean up after the ddi_soft_state_zalloc() if the subsequent 8719 * ddi_get_soft_state() fails. The implication seems to be 8720 * that the get_soft_state cannot fail if the zalloc succeeds. 8721 */ 8722 ddi_soft_state_free(sd_state, instance); 8723 8724 probe_failed: 8725 scsi_unprobe(devp); 8726 #ifdef SDDEBUG 8727 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8728 (sd_level_mask & SD_LOGMASK_TRACE)) { 8729 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8730 (void *)un); 8731 } 8732 #endif 8733 return (DDI_FAILURE); 8734 } 8735 8736 8737 /* 8738 * Function: sd_unit_detach 8739 * 8740 * Description: Performs DDI_DETACH processing for sddetach(). 8741 * 8742 * Return Code: DDI_SUCCESS 8743 * DDI_FAILURE 8744 * 8745 * Context: Kernel thread context 8746 */ 8747 8748 static int 8749 sd_unit_detach(dev_info_t *devi) 8750 { 8751 struct scsi_device *devp; 8752 struct sd_lun *un; 8753 int i; 8754 dev_t dev; 8755 int instance = ddi_get_instance(devi); 8756 8757 mutex_enter(&sd_detach_mutex); 8758 8759 /* 8760 * Fail the detach for any of the following: 8761 * - Unable to get the sd_lun struct for the instance 8762 * - A layered driver has an outstanding open on the instance 8763 * - Another thread is already detaching this instance 8764 * - Another thread is currently performing an open 8765 */ 8766 devp = ddi_get_driver_private(devi); 8767 if ((devp == NULL) || 8768 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8769 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8770 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8771 mutex_exit(&sd_detach_mutex); 8772 return (DDI_FAILURE); 8773 } 8774 8775 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8776 8777 /* 8778 * Mark this instance as currently in a detach, to inhibit any 8779 * opens from a layered driver. 8780 */ 8781 un->un_detach_count++; 8782 mutex_exit(&sd_detach_mutex); 8783 8784 dev = sd_make_device(SD_DEVINFO(un)); 8785 8786 _NOTE(COMPETING_THREADS_NOW); 8787 8788 mutex_enter(SD_MUTEX(un)); 8789 8790 /* 8791 * Fail the detach if there are any outstanding layered 8792 * opens on this device. 8793 */ 8794 for (i = 0; i < NDKMAP; i++) { 8795 if (un->un_ocmap.lyropen[i] != 0) { 8796 goto err_notclosed; 8797 } 8798 } 8799 8800 /* 8801 * Verify there are NO outstanding commands issued to this device. 8802 * ie, un_ncmds_in_transport == 0. 8803 * It's possible to have outstanding commands through the physio 8804 * code path, even though everything's closed. 8805 */ 8806 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8807 (un->un_direct_priority_timeid != NULL) || 8808 (un->un_state == SD_STATE_RWAIT)) { 8809 mutex_exit(SD_MUTEX(un)); 8810 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8811 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8812 goto err_stillbusy; 8813 } 8814 8815 /* 8816 * If we have the device reserved, release the reservation. 8817 */ 8818 if ((un->un_resvd_status & SD_RESERVE) && 8819 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8820 mutex_exit(SD_MUTEX(un)); 8821 /* 8822 * Note: sd_reserve_release sends a command to the device 8823 * via the sd_ioctlcmd() path, and can sleep. 8824 */ 8825 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8826 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8827 "sd_dr_detach: Cannot release reservation \n"); 8828 } 8829 } else { 8830 mutex_exit(SD_MUTEX(un)); 8831 } 8832 8833 /* 8834 * Untimeout any reserve recover, throttle reset, restart unit 8835 * and delayed broadcast timeout threads. Protect the timeout pointer 8836 * from getting nulled by their callback functions. 8837 */ 8838 mutex_enter(SD_MUTEX(un)); 8839 if (un->un_resvd_timeid != NULL) { 8840 timeout_id_t temp_id = un->un_resvd_timeid; 8841 un->un_resvd_timeid = NULL; 8842 mutex_exit(SD_MUTEX(un)); 8843 (void) untimeout(temp_id); 8844 mutex_enter(SD_MUTEX(un)); 8845 } 8846 8847 if (un->un_reset_throttle_timeid != NULL) { 8848 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8849 un->un_reset_throttle_timeid = NULL; 8850 mutex_exit(SD_MUTEX(un)); 8851 (void) untimeout(temp_id); 8852 mutex_enter(SD_MUTEX(un)); 8853 } 8854 8855 if (un->un_startstop_timeid != NULL) { 8856 timeout_id_t temp_id = un->un_startstop_timeid; 8857 un->un_startstop_timeid = NULL; 8858 mutex_exit(SD_MUTEX(un)); 8859 (void) untimeout(temp_id); 8860 mutex_enter(SD_MUTEX(un)); 8861 } 8862 8863 if (un->un_dcvb_timeid != NULL) { 8864 timeout_id_t temp_id = un->un_dcvb_timeid; 8865 un->un_dcvb_timeid = NULL; 8866 mutex_exit(SD_MUTEX(un)); 8867 (void) untimeout(temp_id); 8868 } else { 8869 mutex_exit(SD_MUTEX(un)); 8870 } 8871 8872 /* Remove any pending reservation reclaim requests for this device */ 8873 sd_rmv_resv_reclaim_req(dev); 8874 8875 mutex_enter(SD_MUTEX(un)); 8876 8877 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8878 if (un->un_direct_priority_timeid != NULL) { 8879 timeout_id_t temp_id = un->un_direct_priority_timeid; 8880 un->un_direct_priority_timeid = NULL; 8881 mutex_exit(SD_MUTEX(un)); 8882 (void) untimeout(temp_id); 8883 mutex_enter(SD_MUTEX(un)); 8884 } 8885 8886 /* Cancel any active multi-host disk watch thread requests */ 8887 if (un->un_mhd_token != NULL) { 8888 mutex_exit(SD_MUTEX(un)); 8889 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8890 if (scsi_watch_request_terminate(un->un_mhd_token, 8891 SCSI_WATCH_TERMINATE_NOWAIT)) { 8892 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8893 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8894 /* 8895 * Note: We are returning here after having removed 8896 * some driver timeouts above. This is consistent with 8897 * the legacy implementation but perhaps the watch 8898 * terminate call should be made with the wait flag set. 8899 */ 8900 goto err_stillbusy; 8901 } 8902 mutex_enter(SD_MUTEX(un)); 8903 un->un_mhd_token = NULL; 8904 } 8905 8906 if (un->un_swr_token != NULL) { 8907 mutex_exit(SD_MUTEX(un)); 8908 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8909 if (scsi_watch_request_terminate(un->un_swr_token, 8910 SCSI_WATCH_TERMINATE_NOWAIT)) { 8911 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8912 "sd_dr_detach: Cannot cancel swr watch request\n"); 8913 /* 8914 * Note: We are returning here after having removed 8915 * some driver timeouts above. This is consistent with 8916 * the legacy implementation but perhaps the watch 8917 * terminate call should be made with the wait flag set. 8918 */ 8919 goto err_stillbusy; 8920 } 8921 mutex_enter(SD_MUTEX(un)); 8922 un->un_swr_token = NULL; 8923 } 8924 8925 mutex_exit(SD_MUTEX(un)); 8926 8927 /* 8928 * Clear any scsi_reset_notifies. We clear the reset notifies 8929 * if we have not registered one. 8930 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8931 */ 8932 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8933 sd_mhd_reset_notify_cb, (caddr_t)un); 8934 8935 /* 8936 * protect the timeout pointers from getting nulled by 8937 * their callback functions during the cancellation process. 8938 * In such a scenario untimeout can be invoked with a null value. 8939 */ 8940 _NOTE(NO_COMPETING_THREADS_NOW); 8941 8942 mutex_enter(&un->un_pm_mutex); 8943 if (un->un_pm_idle_timeid != NULL) { 8944 timeout_id_t temp_id = un->un_pm_idle_timeid; 8945 un->un_pm_idle_timeid = NULL; 8946 mutex_exit(&un->un_pm_mutex); 8947 8948 /* 8949 * Timeout is active; cancel it. 8950 * Note that it'll never be active on a device 8951 * that does not support PM therefore we don't 8952 * have to check before calling pm_idle_component. 8953 */ 8954 (void) untimeout(temp_id); 8955 (void) pm_idle_component(SD_DEVINFO(un), 0); 8956 mutex_enter(&un->un_pm_mutex); 8957 } 8958 8959 /* 8960 * Check whether there is already a timeout scheduled for power 8961 * management. If yes then don't lower the power here, that's. 8962 * the timeout handler's job. 8963 */ 8964 if (un->un_pm_timeid != NULL) { 8965 timeout_id_t temp_id = un->un_pm_timeid; 8966 un->un_pm_timeid = NULL; 8967 mutex_exit(&un->un_pm_mutex); 8968 /* 8969 * Timeout is active; cancel it. 8970 * Note that it'll never be active on a device 8971 * that does not support PM therefore we don't 8972 * have to check before calling pm_idle_component. 8973 */ 8974 (void) untimeout(temp_id); 8975 (void) pm_idle_component(SD_DEVINFO(un), 0); 8976 8977 } else { 8978 mutex_exit(&un->un_pm_mutex); 8979 if ((un->un_f_pm_is_enabled == TRUE) && 8980 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8981 DDI_SUCCESS)) { 8982 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8983 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8984 /* 8985 * Fix for bug: 4297749, item # 13 8986 * The above test now includes a check to see if PM is 8987 * supported by this device before call 8988 * pm_lower_power(). 8989 * Note, the following is not dead code. The call to 8990 * pm_lower_power above will generate a call back into 8991 * our sdpower routine which might result in a timeout 8992 * handler getting activated. Therefore the following 8993 * code is valid and necessary. 8994 */ 8995 mutex_enter(&un->un_pm_mutex); 8996 if (un->un_pm_timeid != NULL) { 8997 timeout_id_t temp_id = un->un_pm_timeid; 8998 un->un_pm_timeid = NULL; 8999 mutex_exit(&un->un_pm_mutex); 9000 (void) untimeout(temp_id); 9001 (void) pm_idle_component(SD_DEVINFO(un), 0); 9002 } else { 9003 mutex_exit(&un->un_pm_mutex); 9004 } 9005 } 9006 } 9007 9008 /* 9009 * Cleanup from the scsi_ifsetcap() calls (437868) 9010 * Relocated here from above to be after the call to 9011 * pm_lower_power, which was getting errors. 9012 */ 9013 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9014 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9015 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9016 9017 if (un->un_f_is_fibre == FALSE) { 9018 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9019 } 9020 9021 /* 9022 * Remove any event callbacks, fibre only 9023 */ 9024 if (un->un_f_is_fibre == TRUE) { 9025 if ((un->un_insert_event != NULL) && 9026 (ddi_remove_event_handler(un->un_insert_cb_id) != 9027 DDI_SUCCESS)) { 9028 /* 9029 * Note: We are returning here after having done 9030 * substantial cleanup above. This is consistent 9031 * with the legacy implementation but this may not 9032 * be the right thing to do. 9033 */ 9034 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9035 "sd_dr_detach: Cannot cancel insert event\n"); 9036 goto err_remove_event; 9037 } 9038 un->un_insert_event = NULL; 9039 9040 if ((un->un_remove_event != NULL) && 9041 (ddi_remove_event_handler(un->un_remove_cb_id) != 9042 DDI_SUCCESS)) { 9043 /* 9044 * Note: We are returning here after having done 9045 * substantial cleanup above. This is consistent 9046 * with the legacy implementation but this may not 9047 * be the right thing to do. 9048 */ 9049 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9050 "sd_dr_detach: Cannot cancel remove event\n"); 9051 goto err_remove_event; 9052 } 9053 un->un_remove_event = NULL; 9054 } 9055 9056 /* Do not free the softstate if the callback routine is active */ 9057 sd_sync_with_callback(un); 9058 9059 /* 9060 * Hold the detach mutex here, to make sure that no other threads ever 9061 * can access a (partially) freed soft state structure. 9062 */ 9063 mutex_enter(&sd_detach_mutex); 9064 9065 /* 9066 * Clean up the soft state struct. 9067 * Cleanup is done in reverse order of allocs/inits. 9068 * At this point there should be no competing threads anymore. 9069 */ 9070 9071 /* Unregister and free device id. */ 9072 ddi_devid_unregister(devi); 9073 if (un->un_devid) { 9074 ddi_devid_free(un->un_devid); 9075 un->un_devid = NULL; 9076 } 9077 9078 /* 9079 * Destroy wmap cache if it exists. 9080 */ 9081 if (un->un_wm_cache != NULL) { 9082 kmem_cache_destroy(un->un_wm_cache); 9083 un->un_wm_cache = NULL; 9084 } 9085 9086 /* Remove minor nodes */ 9087 ddi_remove_minor_node(devi, NULL); 9088 9089 /* 9090 * kstat cleanup is done in detach for all device types (4363169). 9091 * We do not want to fail detach if the device kstats are not deleted 9092 * since there is a confusion about the devo_refcnt for the device. 9093 * We just delete the kstats and let detach complete successfully. 9094 */ 9095 if (un->un_stats != NULL) { 9096 kstat_delete(un->un_stats); 9097 un->un_stats = NULL; 9098 } 9099 if (un->un_errstats != NULL) { 9100 kstat_delete(un->un_errstats); 9101 un->un_errstats = NULL; 9102 } 9103 9104 /* Remove partition stats */ 9105 if (un->un_f_pkstats_enabled) { 9106 for (i = 0; i < NSDMAP; i++) { 9107 if (un->un_pstats[i] != NULL) { 9108 kstat_delete(un->un_pstats[i]); 9109 un->un_pstats[i] = NULL; 9110 } 9111 } 9112 } 9113 9114 /* Remove xbuf registration */ 9115 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9116 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9117 9118 /* Remove driver properties */ 9119 ddi_prop_remove_all(devi); 9120 9121 mutex_destroy(&un->un_pm_mutex); 9122 cv_destroy(&un->un_pm_busy_cv); 9123 9124 cv_destroy(&un->un_wcc_cv); 9125 9126 /* Open/close semaphore */ 9127 sema_destroy(&un->un_semoclose); 9128 9129 /* Removable media condvar. */ 9130 cv_destroy(&un->un_state_cv); 9131 9132 /* Suspend/resume condvar. */ 9133 cv_destroy(&un->un_suspend_cv); 9134 cv_destroy(&un->un_disk_busy_cv); 9135 9136 sd_free_rqs(un); 9137 9138 /* Free up soft state */ 9139 devp->sd_private = NULL; 9140 bzero(un, sizeof (struct sd_lun)); 9141 ddi_soft_state_free(sd_state, instance); 9142 9143 mutex_exit(&sd_detach_mutex); 9144 9145 /* This frees up the INQUIRY data associated with the device. */ 9146 scsi_unprobe(devp); 9147 9148 return (DDI_SUCCESS); 9149 9150 err_notclosed: 9151 mutex_exit(SD_MUTEX(un)); 9152 9153 err_stillbusy: 9154 _NOTE(NO_COMPETING_THREADS_NOW); 9155 9156 err_remove_event: 9157 mutex_enter(&sd_detach_mutex); 9158 un->un_detach_count--; 9159 mutex_exit(&sd_detach_mutex); 9160 9161 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9162 return (DDI_FAILURE); 9163 } 9164 9165 9166 /* 9167 * Driver minor node structure and data table 9168 */ 9169 struct driver_minor_data { 9170 char *name; 9171 minor_t minor; 9172 int type; 9173 }; 9174 9175 static struct driver_minor_data sd_minor_data[] = { 9176 {"a", 0, S_IFBLK}, 9177 {"b", 1, S_IFBLK}, 9178 {"c", 2, S_IFBLK}, 9179 {"d", 3, S_IFBLK}, 9180 {"e", 4, S_IFBLK}, 9181 {"f", 5, S_IFBLK}, 9182 {"g", 6, S_IFBLK}, 9183 {"h", 7, S_IFBLK}, 9184 #if defined(_SUNOS_VTOC_16) 9185 {"i", 8, S_IFBLK}, 9186 {"j", 9, S_IFBLK}, 9187 {"k", 10, S_IFBLK}, 9188 {"l", 11, S_IFBLK}, 9189 {"m", 12, S_IFBLK}, 9190 {"n", 13, S_IFBLK}, 9191 {"o", 14, S_IFBLK}, 9192 {"p", 15, S_IFBLK}, 9193 #endif /* defined(_SUNOS_VTOC_16) */ 9194 #if defined(_FIRMWARE_NEEDS_FDISK) 9195 {"q", 16, S_IFBLK}, 9196 {"r", 17, S_IFBLK}, 9197 {"s", 18, S_IFBLK}, 9198 {"t", 19, S_IFBLK}, 9199 {"u", 20, S_IFBLK}, 9200 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9201 {"a,raw", 0, S_IFCHR}, 9202 {"b,raw", 1, S_IFCHR}, 9203 {"c,raw", 2, S_IFCHR}, 9204 {"d,raw", 3, S_IFCHR}, 9205 {"e,raw", 4, S_IFCHR}, 9206 {"f,raw", 5, S_IFCHR}, 9207 {"g,raw", 6, S_IFCHR}, 9208 {"h,raw", 7, S_IFCHR}, 9209 #if defined(_SUNOS_VTOC_16) 9210 {"i,raw", 8, S_IFCHR}, 9211 {"j,raw", 9, S_IFCHR}, 9212 {"k,raw", 10, S_IFCHR}, 9213 {"l,raw", 11, S_IFCHR}, 9214 {"m,raw", 12, S_IFCHR}, 9215 {"n,raw", 13, S_IFCHR}, 9216 {"o,raw", 14, S_IFCHR}, 9217 {"p,raw", 15, S_IFCHR}, 9218 #endif /* defined(_SUNOS_VTOC_16) */ 9219 #if defined(_FIRMWARE_NEEDS_FDISK) 9220 {"q,raw", 16, S_IFCHR}, 9221 {"r,raw", 17, S_IFCHR}, 9222 {"s,raw", 18, S_IFCHR}, 9223 {"t,raw", 19, S_IFCHR}, 9224 {"u,raw", 20, S_IFCHR}, 9225 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9226 {0} 9227 }; 9228 9229 static struct driver_minor_data sd_minor_data_efi[] = { 9230 {"a", 0, S_IFBLK}, 9231 {"b", 1, S_IFBLK}, 9232 {"c", 2, S_IFBLK}, 9233 {"d", 3, S_IFBLK}, 9234 {"e", 4, S_IFBLK}, 9235 {"f", 5, S_IFBLK}, 9236 {"g", 6, S_IFBLK}, 9237 {"wd", 7, S_IFBLK}, 9238 #if defined(_FIRMWARE_NEEDS_FDISK) 9239 {"q", 16, S_IFBLK}, 9240 {"r", 17, S_IFBLK}, 9241 {"s", 18, S_IFBLK}, 9242 {"t", 19, S_IFBLK}, 9243 {"u", 20, S_IFBLK}, 9244 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9245 {"a,raw", 0, S_IFCHR}, 9246 {"b,raw", 1, S_IFCHR}, 9247 {"c,raw", 2, S_IFCHR}, 9248 {"d,raw", 3, S_IFCHR}, 9249 {"e,raw", 4, S_IFCHR}, 9250 {"f,raw", 5, S_IFCHR}, 9251 {"g,raw", 6, S_IFCHR}, 9252 {"wd,raw", 7, S_IFCHR}, 9253 #if defined(_FIRMWARE_NEEDS_FDISK) 9254 {"q,raw", 16, S_IFCHR}, 9255 {"r,raw", 17, S_IFCHR}, 9256 {"s,raw", 18, S_IFCHR}, 9257 {"t,raw", 19, S_IFCHR}, 9258 {"u,raw", 20, S_IFCHR}, 9259 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9260 {0} 9261 }; 9262 9263 9264 /* 9265 * Function: sd_create_minor_nodes 9266 * 9267 * Description: Create the minor device nodes for the instance. 9268 * 9269 * Arguments: un - driver soft state (unit) structure 9270 * devi - pointer to device info structure 9271 * 9272 * Return Code: DDI_SUCCESS 9273 * DDI_FAILURE 9274 * 9275 * Context: Kernel thread context 9276 */ 9277 9278 static int 9279 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9280 { 9281 struct driver_minor_data *dmdp; 9282 struct scsi_device *devp; 9283 int instance; 9284 char name[48]; 9285 9286 ASSERT(un != NULL); 9287 devp = ddi_get_driver_private(devi); 9288 instance = ddi_get_instance(devp->sd_dev); 9289 9290 /* 9291 * Create all the minor nodes for this target. 9292 */ 9293 if (un->un_blockcount > DK_MAX_BLOCKS) 9294 dmdp = sd_minor_data_efi; 9295 else 9296 dmdp = sd_minor_data; 9297 while (dmdp->name != NULL) { 9298 9299 (void) sprintf(name, "%s", dmdp->name); 9300 9301 if (ddi_create_minor_node(devi, name, dmdp->type, 9302 (instance << SDUNIT_SHIFT) | dmdp->minor, 9303 un->un_node_type, NULL) == DDI_FAILURE) { 9304 /* 9305 * Clean up any nodes that may have been created, in 9306 * case this fails in the middle of the loop. 9307 */ 9308 ddi_remove_minor_node(devi, NULL); 9309 return (DDI_FAILURE); 9310 } 9311 dmdp++; 9312 } 9313 9314 return (DDI_SUCCESS); 9315 } 9316 9317 9318 /* 9319 * Function: sd_create_errstats 9320 * 9321 * Description: This routine instantiates the device error stats. 9322 * 9323 * Note: During attach the stats are instantiated first so they are 9324 * available for attach-time routines that utilize the driver 9325 * iopath to send commands to the device. The stats are initialized 9326 * separately so data obtained during some attach-time routines is 9327 * available. (4362483) 9328 * 9329 * Arguments: un - driver soft state (unit) structure 9330 * instance - driver instance 9331 * 9332 * Context: Kernel thread context 9333 */ 9334 9335 static void 9336 sd_create_errstats(struct sd_lun *un, int instance) 9337 { 9338 struct sd_errstats *stp; 9339 char kstatmodule_err[KSTAT_STRLEN]; 9340 char kstatname[KSTAT_STRLEN]; 9341 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9342 9343 ASSERT(un != NULL); 9344 9345 if (un->un_errstats != NULL) { 9346 return; 9347 } 9348 9349 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9350 "%serr", sd_label); 9351 (void) snprintf(kstatname, sizeof (kstatname), 9352 "%s%d,err", sd_label, instance); 9353 9354 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9355 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9356 9357 if (un->un_errstats == NULL) { 9358 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9359 "sd_create_errstats: Failed kstat_create\n"); 9360 return; 9361 } 9362 9363 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9364 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9365 KSTAT_DATA_UINT32); 9366 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9367 KSTAT_DATA_UINT32); 9368 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9369 KSTAT_DATA_UINT32); 9370 kstat_named_init(&stp->sd_vid, "Vendor", 9371 KSTAT_DATA_CHAR); 9372 kstat_named_init(&stp->sd_pid, "Product", 9373 KSTAT_DATA_CHAR); 9374 kstat_named_init(&stp->sd_revision, "Revision", 9375 KSTAT_DATA_CHAR); 9376 kstat_named_init(&stp->sd_serial, "Serial No", 9377 KSTAT_DATA_CHAR); 9378 kstat_named_init(&stp->sd_capacity, "Size", 9379 KSTAT_DATA_ULONGLONG); 9380 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9381 KSTAT_DATA_UINT32); 9382 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9383 KSTAT_DATA_UINT32); 9384 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9385 KSTAT_DATA_UINT32); 9386 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9387 KSTAT_DATA_UINT32); 9388 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9389 KSTAT_DATA_UINT32); 9390 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9391 KSTAT_DATA_UINT32); 9392 9393 un->un_errstats->ks_private = un; 9394 un->un_errstats->ks_update = nulldev; 9395 9396 kstat_install(un->un_errstats); 9397 } 9398 9399 9400 /* 9401 * Function: sd_set_errstats 9402 * 9403 * Description: This routine sets the value of the vendor id, product id, 9404 * revision, serial number, and capacity device error stats. 9405 * 9406 * Note: During attach the stats are instantiated first so they are 9407 * available for attach-time routines that utilize the driver 9408 * iopath to send commands to the device. The stats are initialized 9409 * separately so data obtained during some attach-time routines is 9410 * available. (4362483) 9411 * 9412 * Arguments: un - driver soft state (unit) structure 9413 * 9414 * Context: Kernel thread context 9415 */ 9416 9417 static void 9418 sd_set_errstats(struct sd_lun *un) 9419 { 9420 struct sd_errstats *stp; 9421 9422 ASSERT(un != NULL); 9423 ASSERT(un->un_errstats != NULL); 9424 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9425 ASSERT(stp != NULL); 9426 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9427 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9428 (void) strncpy(stp->sd_revision.value.c, 9429 un->un_sd->sd_inq->inq_revision, 4); 9430 9431 /* 9432 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9433 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9434 * (4376302)) 9435 */ 9436 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9437 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9438 sizeof (SD_INQUIRY(un)->inq_serial)); 9439 } 9440 9441 if (un->un_f_blockcount_is_valid != TRUE) { 9442 /* 9443 * Set capacity error stat to 0 for no media. This ensures 9444 * a valid capacity is displayed in response to 'iostat -E' 9445 * when no media is present in the device. 9446 */ 9447 stp->sd_capacity.value.ui64 = 0; 9448 } else { 9449 /* 9450 * Multiply un_blockcount by un->un_sys_blocksize to get 9451 * capacity. 9452 * 9453 * Note: for non-512 blocksize devices "un_blockcount" has been 9454 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9455 * (un_tgt_blocksize / un->un_sys_blocksize). 9456 */ 9457 stp->sd_capacity.value.ui64 = (uint64_t) 9458 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9459 } 9460 } 9461 9462 9463 /* 9464 * Function: sd_set_pstats 9465 * 9466 * Description: This routine instantiates and initializes the partition 9467 * stats for each partition with more than zero blocks. 9468 * (4363169) 9469 * 9470 * Arguments: un - driver soft state (unit) structure 9471 * 9472 * Context: Kernel thread context 9473 */ 9474 9475 static void 9476 sd_set_pstats(struct sd_lun *un) 9477 { 9478 char kstatname[KSTAT_STRLEN]; 9479 int instance; 9480 int i; 9481 9482 ASSERT(un != NULL); 9483 9484 instance = ddi_get_instance(SD_DEVINFO(un)); 9485 9486 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9487 for (i = 0; i < NSDMAP; i++) { 9488 if ((un->un_pstats[i] == NULL) && 9489 (un->un_map[i].dkl_nblk != 0)) { 9490 (void) snprintf(kstatname, sizeof (kstatname), 9491 "%s%d,%s", sd_label, instance, 9492 sd_minor_data[i].name); 9493 un->un_pstats[i] = kstat_create(sd_label, 9494 instance, kstatname, "partition", KSTAT_TYPE_IO, 9495 1, KSTAT_FLAG_PERSISTENT); 9496 if (un->un_pstats[i] != NULL) { 9497 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9498 kstat_install(un->un_pstats[i]); 9499 } 9500 } 9501 } 9502 } 9503 9504 9505 #if (defined(__fibre)) 9506 /* 9507 * Function: sd_init_event_callbacks 9508 * 9509 * Description: This routine initializes the insertion and removal event 9510 * callbacks. (fibre only) 9511 * 9512 * Arguments: un - driver soft state (unit) structure 9513 * 9514 * Context: Kernel thread context 9515 */ 9516 9517 static void 9518 sd_init_event_callbacks(struct sd_lun *un) 9519 { 9520 ASSERT(un != NULL); 9521 9522 if ((un->un_insert_event == NULL) && 9523 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9524 &un->un_insert_event) == DDI_SUCCESS)) { 9525 /* 9526 * Add the callback for an insertion event 9527 */ 9528 (void) ddi_add_event_handler(SD_DEVINFO(un), 9529 un->un_insert_event, sd_event_callback, (void *)un, 9530 &(un->un_insert_cb_id)); 9531 } 9532 9533 if ((un->un_remove_event == NULL) && 9534 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9535 &un->un_remove_event) == DDI_SUCCESS)) { 9536 /* 9537 * Add the callback for a removal event 9538 */ 9539 (void) ddi_add_event_handler(SD_DEVINFO(un), 9540 un->un_remove_event, sd_event_callback, (void *)un, 9541 &(un->un_remove_cb_id)); 9542 } 9543 } 9544 9545 9546 /* 9547 * Function: sd_event_callback 9548 * 9549 * Description: This routine handles insert/remove events (photon). The 9550 * state is changed to OFFLINE which can be used to supress 9551 * error msgs. (fibre only) 9552 * 9553 * Arguments: un - driver soft state (unit) structure 9554 * 9555 * Context: Callout thread context 9556 */ 9557 /* ARGSUSED */ 9558 static void 9559 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9560 void *bus_impldata) 9561 { 9562 struct sd_lun *un = (struct sd_lun *)arg; 9563 9564 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9565 if (event == un->un_insert_event) { 9566 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9567 mutex_enter(SD_MUTEX(un)); 9568 if (un->un_state == SD_STATE_OFFLINE) { 9569 if (un->un_last_state != SD_STATE_SUSPENDED) { 9570 un->un_state = un->un_last_state; 9571 } else { 9572 /* 9573 * We have gone through SUSPEND/RESUME while 9574 * we were offline. Restore the last state 9575 */ 9576 un->un_state = un->un_save_state; 9577 } 9578 } 9579 mutex_exit(SD_MUTEX(un)); 9580 9581 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9582 } else if (event == un->un_remove_event) { 9583 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9584 mutex_enter(SD_MUTEX(un)); 9585 /* 9586 * We need to handle an event callback that occurs during 9587 * the suspend operation, since we don't prevent it. 9588 */ 9589 if (un->un_state != SD_STATE_OFFLINE) { 9590 if (un->un_state != SD_STATE_SUSPENDED) { 9591 New_state(un, SD_STATE_OFFLINE); 9592 } else { 9593 un->un_last_state = SD_STATE_OFFLINE; 9594 } 9595 } 9596 mutex_exit(SD_MUTEX(un)); 9597 } else { 9598 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9599 "!Unknown event\n"); 9600 } 9601 9602 } 9603 #endif 9604 9605 /* 9606 * Function: sd_cache_control() 9607 * 9608 * Description: This routine is the driver entry point for setting 9609 * read and write caching by modifying the WCE (write cache 9610 * enable) and RCD (read cache disable) bits of mode 9611 * page 8 (MODEPAGE_CACHING). 9612 * 9613 * Arguments: un - driver soft state (unit) structure 9614 * rcd_flag - flag for controlling the read cache 9615 * wce_flag - flag for controlling the write cache 9616 * 9617 * Return Code: EIO 9618 * code returned by sd_send_scsi_MODE_SENSE and 9619 * sd_send_scsi_MODE_SELECT 9620 * 9621 * Context: Kernel Thread 9622 */ 9623 9624 static int 9625 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 9626 { 9627 struct mode_caching *mode_caching_page; 9628 uchar_t *header; 9629 size_t buflen; 9630 int hdrlen; 9631 int bd_len; 9632 int rval = 0; 9633 struct mode_header_grp2 *mhp; 9634 9635 ASSERT(un != NULL); 9636 9637 /* 9638 * Do a test unit ready, otherwise a mode sense may not work if this 9639 * is the first command sent to the device after boot. 9640 */ 9641 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9642 9643 if (un->un_f_cfg_is_atapi == TRUE) { 9644 hdrlen = MODE_HEADER_LENGTH_GRP2; 9645 } else { 9646 hdrlen = MODE_HEADER_LENGTH; 9647 } 9648 9649 /* 9650 * Allocate memory for the retrieved mode page and its headers. Set 9651 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9652 * we get all of the mode sense data otherwise, the mode select 9653 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9654 */ 9655 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9656 sizeof (struct mode_cache_scsi3); 9657 9658 header = kmem_zalloc(buflen, KM_SLEEP); 9659 9660 /* Get the information from the device. */ 9661 if (un->un_f_cfg_is_atapi == TRUE) { 9662 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9663 MODEPAGE_CACHING, SD_PATH_DIRECT); 9664 } else { 9665 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9666 MODEPAGE_CACHING, SD_PATH_DIRECT); 9667 } 9668 if (rval != 0) { 9669 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9670 "sd_cache_control: Mode Sense Failed\n"); 9671 kmem_free(header, buflen); 9672 return (rval); 9673 } 9674 9675 /* 9676 * Determine size of Block Descriptors in order to locate 9677 * the mode page data. ATAPI devices return 0, SCSI devices 9678 * should return MODE_BLK_DESC_LENGTH. 9679 */ 9680 if (un->un_f_cfg_is_atapi == TRUE) { 9681 mhp = (struct mode_header_grp2 *)header; 9682 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9683 } else { 9684 bd_len = ((struct mode_header *)header)->bdesc_length; 9685 } 9686 9687 if (bd_len > MODE_BLK_DESC_LENGTH) { 9688 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9689 "sd_cache_control: Mode Sense returned invalid " 9690 "block descriptor length\n"); 9691 kmem_free(header, buflen); 9692 return (EIO); 9693 } 9694 9695 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9696 9697 /* Check the relevant bits on successful mode sense. */ 9698 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9699 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9700 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9701 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9702 9703 size_t sbuflen; 9704 uchar_t save_pg; 9705 9706 /* 9707 * Construct select buffer length based on the 9708 * length of the sense data returned. 9709 */ 9710 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 9711 sizeof (struct mode_page) + 9712 (int)mode_caching_page->mode_page.length; 9713 9714 /* 9715 * Set the caching bits as requested. 9716 */ 9717 if (rcd_flag == SD_CACHE_ENABLE) 9718 mode_caching_page->rcd = 0; 9719 else if (rcd_flag == SD_CACHE_DISABLE) 9720 mode_caching_page->rcd = 1; 9721 9722 if (wce_flag == SD_CACHE_ENABLE) 9723 mode_caching_page->wce = 1; 9724 else if (wce_flag == SD_CACHE_DISABLE) 9725 mode_caching_page->wce = 0; 9726 9727 /* 9728 * Save the page if the mode sense says the 9729 * drive supports it. 9730 */ 9731 save_pg = mode_caching_page->mode_page.ps ? 9732 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9733 9734 /* Clear reserved bits before mode select. */ 9735 mode_caching_page->mode_page.ps = 0; 9736 9737 /* 9738 * Clear out mode header for mode select. 9739 * The rest of the retrieved page will be reused. 9740 */ 9741 bzero(header, hdrlen); 9742 9743 if (un->un_f_cfg_is_atapi == TRUE) { 9744 mhp = (struct mode_header_grp2 *)header; 9745 mhp->bdesc_length_hi = bd_len >> 8; 9746 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9747 } else { 9748 ((struct mode_header *)header)->bdesc_length = bd_len; 9749 } 9750 9751 /* Issue mode select to change the cache settings */ 9752 if (un->un_f_cfg_is_atapi == TRUE) { 9753 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9754 sbuflen, save_pg, SD_PATH_DIRECT); 9755 } else { 9756 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9757 sbuflen, save_pg, SD_PATH_DIRECT); 9758 } 9759 } 9760 9761 kmem_free(header, buflen); 9762 return (rval); 9763 } 9764 9765 9766 /* 9767 * Function: sd_get_write_cache_enabled() 9768 * 9769 * Description: This routine is the driver entry point for determining if 9770 * write caching is enabled. It examines the WCE (write cache 9771 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9772 * 9773 * Arguments: un - driver soft state (unit) structure 9774 * is_enabled - pointer to int where write cache enabled state 9775 * is returned (non-zero -> write cache enabled) 9776 * 9777 * 9778 * Return Code: EIO 9779 * code returned by sd_send_scsi_MODE_SENSE 9780 * 9781 * Context: Kernel Thread 9782 * 9783 * NOTE: If ioctl is added to disable write cache, this sequence should 9784 * be followed so that no locking is required for accesses to 9785 * un->un_f_write_cache_enabled: 9786 * do mode select to clear wce 9787 * do synchronize cache to flush cache 9788 * set un->un_f_write_cache_enabled = FALSE 9789 * 9790 * Conversely, an ioctl to enable the write cache should be done 9791 * in this order: 9792 * set un->un_f_write_cache_enabled = TRUE 9793 * do mode select to set wce 9794 */ 9795 9796 static int 9797 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 9798 { 9799 struct mode_caching *mode_caching_page; 9800 uchar_t *header; 9801 size_t buflen; 9802 int hdrlen; 9803 int bd_len; 9804 int rval = 0; 9805 9806 ASSERT(un != NULL); 9807 ASSERT(is_enabled != NULL); 9808 9809 /* in case of error, flag as enabled */ 9810 *is_enabled = TRUE; 9811 9812 /* 9813 * Do a test unit ready, otherwise a mode sense may not work if this 9814 * is the first command sent to the device after boot. 9815 */ 9816 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9817 9818 if (un->un_f_cfg_is_atapi == TRUE) { 9819 hdrlen = MODE_HEADER_LENGTH_GRP2; 9820 } else { 9821 hdrlen = MODE_HEADER_LENGTH; 9822 } 9823 9824 /* 9825 * Allocate memory for the retrieved mode page and its headers. Set 9826 * a pointer to the page itself. 9827 */ 9828 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9829 header = kmem_zalloc(buflen, KM_SLEEP); 9830 9831 /* Get the information from the device. */ 9832 if (un->un_f_cfg_is_atapi == TRUE) { 9833 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9834 MODEPAGE_CACHING, SD_PATH_DIRECT); 9835 } else { 9836 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9837 MODEPAGE_CACHING, SD_PATH_DIRECT); 9838 } 9839 if (rval != 0) { 9840 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9841 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9842 kmem_free(header, buflen); 9843 return (rval); 9844 } 9845 9846 /* 9847 * Determine size of Block Descriptors in order to locate 9848 * the mode page data. ATAPI devices return 0, SCSI devices 9849 * should return MODE_BLK_DESC_LENGTH. 9850 */ 9851 if (un->un_f_cfg_is_atapi == TRUE) { 9852 struct mode_header_grp2 *mhp; 9853 mhp = (struct mode_header_grp2 *)header; 9854 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9855 } else { 9856 bd_len = ((struct mode_header *)header)->bdesc_length; 9857 } 9858 9859 if (bd_len > MODE_BLK_DESC_LENGTH) { 9860 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9861 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9862 "block descriptor length\n"); 9863 kmem_free(header, buflen); 9864 return (EIO); 9865 } 9866 9867 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9868 *is_enabled = mode_caching_page->wce; 9869 9870 kmem_free(header, buflen); 9871 return (0); 9872 } 9873 9874 9875 /* 9876 * Function: sd_make_device 9877 * 9878 * Description: Utility routine to return the Solaris device number from 9879 * the data in the device's dev_info structure. 9880 * 9881 * Return Code: The Solaris device number 9882 * 9883 * Context: Any 9884 */ 9885 9886 static dev_t 9887 sd_make_device(dev_info_t *devi) 9888 { 9889 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 9890 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9891 } 9892 9893 9894 /* 9895 * Function: sd_pm_entry 9896 * 9897 * Description: Called at the start of a new command to manage power 9898 * and busy status of a device. This includes determining whether 9899 * the current power state of the device is sufficient for 9900 * performing the command or whether it must be changed. 9901 * The PM framework is notified appropriately. 9902 * Only with a return status of DDI_SUCCESS will the 9903 * component be busy to the framework. 9904 * 9905 * All callers of sd_pm_entry must check the return status 9906 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9907 * of DDI_FAILURE indicates the device failed to power up. 9908 * In this case un_pm_count has been adjusted so the result 9909 * on exit is still powered down, ie. count is less than 0. 9910 * Calling sd_pm_exit with this count value hits an ASSERT. 9911 * 9912 * Return Code: DDI_SUCCESS or DDI_FAILURE 9913 * 9914 * Context: Kernel thread context. 9915 */ 9916 9917 static int 9918 sd_pm_entry(struct sd_lun *un) 9919 { 9920 int return_status = DDI_SUCCESS; 9921 9922 ASSERT(!mutex_owned(SD_MUTEX(un))); 9923 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9924 9925 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9926 9927 if (un->un_f_pm_is_enabled == FALSE) { 9928 SD_TRACE(SD_LOG_IO_PM, un, 9929 "sd_pm_entry: exiting, PM not enabled\n"); 9930 return (return_status); 9931 } 9932 9933 /* 9934 * Just increment a counter if PM is enabled. On the transition from 9935 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9936 * the count with each IO and mark the device as idle when the count 9937 * hits 0. 9938 * 9939 * If the count is less than 0 the device is powered down. If a powered 9940 * down device is successfully powered up then the count must be 9941 * incremented to reflect the power up. Note that it'll get incremented 9942 * a second time to become busy. 9943 * 9944 * Because the following has the potential to change the device state 9945 * and must release the un_pm_mutex to do so, only one thread can be 9946 * allowed through at a time. 9947 */ 9948 9949 mutex_enter(&un->un_pm_mutex); 9950 while (un->un_pm_busy == TRUE) { 9951 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9952 } 9953 un->un_pm_busy = TRUE; 9954 9955 if (un->un_pm_count < 1) { 9956 9957 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9958 9959 /* 9960 * Indicate we are now busy so the framework won't attempt to 9961 * power down the device. This call will only fail if either 9962 * we passed a bad component number or the device has no 9963 * components. Neither of these should ever happen. 9964 */ 9965 mutex_exit(&un->un_pm_mutex); 9966 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9967 ASSERT(return_status == DDI_SUCCESS); 9968 9969 mutex_enter(&un->un_pm_mutex); 9970 9971 if (un->un_pm_count < 0) { 9972 mutex_exit(&un->un_pm_mutex); 9973 9974 SD_TRACE(SD_LOG_IO_PM, un, 9975 "sd_pm_entry: power up component\n"); 9976 9977 /* 9978 * pm_raise_power will cause sdpower to be called 9979 * which brings the device power level to the 9980 * desired state, ON in this case. If successful, 9981 * un_pm_count and un_power_level will be updated 9982 * appropriately. 9983 */ 9984 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9985 SD_SPINDLE_ON); 9986 9987 mutex_enter(&un->un_pm_mutex); 9988 9989 if (return_status != DDI_SUCCESS) { 9990 /* 9991 * Power up failed. 9992 * Idle the device and adjust the count 9993 * so the result on exit is that we're 9994 * still powered down, ie. count is less than 0. 9995 */ 9996 SD_TRACE(SD_LOG_IO_PM, un, 9997 "sd_pm_entry: power up failed," 9998 " idle the component\n"); 9999 10000 (void) pm_idle_component(SD_DEVINFO(un), 0); 10001 un->un_pm_count--; 10002 } else { 10003 /* 10004 * Device is powered up, verify the 10005 * count is non-negative. 10006 * This is debug only. 10007 */ 10008 ASSERT(un->un_pm_count == 0); 10009 } 10010 } 10011 10012 if (return_status == DDI_SUCCESS) { 10013 /* 10014 * For performance, now that the device has been tagged 10015 * as busy, and it's known to be powered up, update the 10016 * chain types to use jump tables that do not include 10017 * pm. This significantly lowers the overhead and 10018 * therefore improves performance. 10019 */ 10020 10021 mutex_exit(&un->un_pm_mutex); 10022 mutex_enter(SD_MUTEX(un)); 10023 SD_TRACE(SD_LOG_IO_PM, un, 10024 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10025 un->un_uscsi_chain_type); 10026 10027 if (un->un_f_non_devbsize_supported) { 10028 un->un_buf_chain_type = 10029 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10030 } else { 10031 un->un_buf_chain_type = 10032 SD_CHAIN_INFO_DISK_NO_PM; 10033 } 10034 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10035 10036 SD_TRACE(SD_LOG_IO_PM, un, 10037 " changed uscsi_chain_type to %d\n", 10038 un->un_uscsi_chain_type); 10039 mutex_exit(SD_MUTEX(un)); 10040 mutex_enter(&un->un_pm_mutex); 10041 10042 if (un->un_pm_idle_timeid == NULL) { 10043 /* 300 ms. */ 10044 un->un_pm_idle_timeid = 10045 timeout(sd_pm_idletimeout_handler, un, 10046 (drv_usectohz((clock_t)300000))); 10047 /* 10048 * Include an extra call to busy which keeps the 10049 * device busy with-respect-to the PM layer 10050 * until the timer fires, at which time it'll 10051 * get the extra idle call. 10052 */ 10053 (void) pm_busy_component(SD_DEVINFO(un), 0); 10054 } 10055 } 10056 } 10057 un->un_pm_busy = FALSE; 10058 /* Next... */ 10059 cv_signal(&un->un_pm_busy_cv); 10060 10061 un->un_pm_count++; 10062 10063 SD_TRACE(SD_LOG_IO_PM, un, 10064 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10065 10066 mutex_exit(&un->un_pm_mutex); 10067 10068 return (return_status); 10069 } 10070 10071 10072 /* 10073 * Function: sd_pm_exit 10074 * 10075 * Description: Called at the completion of a command to manage busy 10076 * status for the device. If the device becomes idle the 10077 * PM framework is notified. 10078 * 10079 * Context: Kernel thread context 10080 */ 10081 10082 static void 10083 sd_pm_exit(struct sd_lun *un) 10084 { 10085 ASSERT(!mutex_owned(SD_MUTEX(un))); 10086 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10087 10088 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10089 10090 /* 10091 * After attach the following flag is only read, so don't 10092 * take the penalty of acquiring a mutex for it. 10093 */ 10094 if (un->un_f_pm_is_enabled == TRUE) { 10095 10096 mutex_enter(&un->un_pm_mutex); 10097 un->un_pm_count--; 10098 10099 SD_TRACE(SD_LOG_IO_PM, un, 10100 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10101 10102 ASSERT(un->un_pm_count >= 0); 10103 if (un->un_pm_count == 0) { 10104 mutex_exit(&un->un_pm_mutex); 10105 10106 SD_TRACE(SD_LOG_IO_PM, un, 10107 "sd_pm_exit: idle component\n"); 10108 10109 (void) pm_idle_component(SD_DEVINFO(un), 0); 10110 10111 } else { 10112 mutex_exit(&un->un_pm_mutex); 10113 } 10114 } 10115 10116 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10117 } 10118 10119 10120 /* 10121 * Function: sdopen 10122 * 10123 * Description: Driver's open(9e) entry point function. 10124 * 10125 * Arguments: dev_i - pointer to device number 10126 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10127 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10128 * cred_p - user credential pointer 10129 * 10130 * Return Code: EINVAL 10131 * ENXIO 10132 * EIO 10133 * EROFS 10134 * EBUSY 10135 * 10136 * Context: Kernel thread context 10137 */ 10138 /* ARGSUSED */ 10139 static int 10140 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10141 { 10142 struct sd_lun *un; 10143 int nodelay; 10144 int part; 10145 uint64_t partmask; 10146 int instance; 10147 dev_t dev; 10148 int rval = EIO; 10149 10150 /* Validate the open type */ 10151 if (otyp >= OTYPCNT) { 10152 return (EINVAL); 10153 } 10154 10155 dev = *dev_p; 10156 instance = SDUNIT(dev); 10157 mutex_enter(&sd_detach_mutex); 10158 10159 /* 10160 * Fail the open if there is no softstate for the instance, or 10161 * if another thread somewhere is trying to detach the instance. 10162 */ 10163 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10164 (un->un_detach_count != 0)) { 10165 mutex_exit(&sd_detach_mutex); 10166 /* 10167 * The probe cache only needs to be cleared when open (9e) fails 10168 * with ENXIO (4238046). 10169 */ 10170 /* 10171 * un-conditionally clearing probe cache is ok with 10172 * separate sd/ssd binaries 10173 * x86 platform can be an issue with both parallel 10174 * and fibre in 1 binary 10175 */ 10176 sd_scsi_clear_probe_cache(); 10177 return (ENXIO); 10178 } 10179 10180 /* 10181 * The un_layer_count is to prevent another thread in specfs from 10182 * trying to detach the instance, which can happen when we are 10183 * called from a higher-layer driver instead of thru specfs. 10184 * This will not be needed when DDI provides a layered driver 10185 * interface that allows specfs to know that an instance is in 10186 * use by a layered driver & should not be detached. 10187 * 10188 * Note: the semantics for layered driver opens are exactly one 10189 * close for every open. 10190 */ 10191 if (otyp == OTYP_LYR) { 10192 un->un_layer_count++; 10193 } 10194 10195 /* 10196 * Keep a count of the current # of opens in progress. This is because 10197 * some layered drivers try to call us as a regular open. This can 10198 * cause problems that we cannot prevent, however by keeping this count 10199 * we can at least keep our open and detach routines from racing against 10200 * each other under such conditions. 10201 */ 10202 un->un_opens_in_progress++; 10203 mutex_exit(&sd_detach_mutex); 10204 10205 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10206 part = SDPART(dev); 10207 partmask = 1 << part; 10208 10209 /* 10210 * We use a semaphore here in order to serialize 10211 * open and close requests on the device. 10212 */ 10213 sema_p(&un->un_semoclose); 10214 10215 mutex_enter(SD_MUTEX(un)); 10216 10217 /* 10218 * All device accesses go thru sdstrategy() where we check 10219 * on suspend status but there could be a scsi_poll command, 10220 * which bypasses sdstrategy(), so we need to check pm 10221 * status. 10222 */ 10223 10224 if (!nodelay) { 10225 while ((un->un_state == SD_STATE_SUSPENDED) || 10226 (un->un_state == SD_STATE_PM_CHANGING)) { 10227 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10228 } 10229 10230 mutex_exit(SD_MUTEX(un)); 10231 if (sd_pm_entry(un) != DDI_SUCCESS) { 10232 rval = EIO; 10233 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10234 "sdopen: sd_pm_entry failed\n"); 10235 goto open_failed_with_pm; 10236 } 10237 mutex_enter(SD_MUTEX(un)); 10238 } 10239 10240 /* check for previous exclusive open */ 10241 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10242 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10243 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10244 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10245 10246 if (un->un_exclopen & (partmask)) { 10247 goto excl_open_fail; 10248 } 10249 10250 if (flag & FEXCL) { 10251 int i; 10252 if (un->un_ocmap.lyropen[part]) { 10253 goto excl_open_fail; 10254 } 10255 for (i = 0; i < (OTYPCNT - 1); i++) { 10256 if (un->un_ocmap.regopen[i] & (partmask)) { 10257 goto excl_open_fail; 10258 } 10259 } 10260 } 10261 10262 /* 10263 * Check the write permission if this is a removable media device, 10264 * NDELAY has not been set, and writable permission is requested. 10265 * 10266 * Note: If NDELAY was set and this is write-protected media the WRITE 10267 * attempt will fail with EIO as part of the I/O processing. This is a 10268 * more permissive implementation that allows the open to succeed and 10269 * WRITE attempts to fail when appropriate. 10270 */ 10271 if (un->un_f_chk_wp_open) { 10272 if ((flag & FWRITE) && (!nodelay)) { 10273 mutex_exit(SD_MUTEX(un)); 10274 /* 10275 * Defer the check for write permission on writable 10276 * DVD drive till sdstrategy and will not fail open even 10277 * if FWRITE is set as the device can be writable 10278 * depending upon the media and the media can change 10279 * after the call to open(). 10280 */ 10281 if (un->un_f_dvdram_writable_device == FALSE) { 10282 if (ISCD(un) || sr_check_wp(dev)) { 10283 rval = EROFS; 10284 mutex_enter(SD_MUTEX(un)); 10285 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10286 "write to cd or write protected media\n"); 10287 goto open_fail; 10288 } 10289 } 10290 mutex_enter(SD_MUTEX(un)); 10291 } 10292 } 10293 10294 /* 10295 * If opening in NDELAY/NONBLOCK mode, just return. 10296 * Check if disk is ready and has a valid geometry later. 10297 */ 10298 if (!nodelay) { 10299 mutex_exit(SD_MUTEX(un)); 10300 rval = sd_ready_and_valid(un); 10301 mutex_enter(SD_MUTEX(un)); 10302 /* 10303 * Fail if device is not ready or if the number of disk 10304 * blocks is zero or negative for non CD devices. 10305 */ 10306 if ((rval != SD_READY_VALID) || 10307 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10308 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10309 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10310 "device not ready or invalid disk block value\n"); 10311 goto open_fail; 10312 } 10313 #if defined(__i386) || defined(__amd64) 10314 } else { 10315 uchar_t *cp; 10316 /* 10317 * x86 requires special nodelay handling, so that p0 is 10318 * always defined and accessible. 10319 * Invalidate geometry only if device is not already open. 10320 */ 10321 cp = &un->un_ocmap.chkd[0]; 10322 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10323 if (*cp != (uchar_t)0) { 10324 break; 10325 } 10326 cp++; 10327 } 10328 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10329 un->un_f_geometry_is_valid = FALSE; 10330 } 10331 10332 #endif 10333 } 10334 10335 if (otyp == OTYP_LYR) { 10336 un->un_ocmap.lyropen[part]++; 10337 } else { 10338 un->un_ocmap.regopen[otyp] |= partmask; 10339 } 10340 10341 /* Set up open and exclusive open flags */ 10342 if (flag & FEXCL) { 10343 un->un_exclopen |= (partmask); 10344 } 10345 10346 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10347 "open of part %d type %d\n", part, otyp); 10348 10349 mutex_exit(SD_MUTEX(un)); 10350 if (!nodelay) { 10351 sd_pm_exit(un); 10352 } 10353 10354 sema_v(&un->un_semoclose); 10355 10356 mutex_enter(&sd_detach_mutex); 10357 un->un_opens_in_progress--; 10358 mutex_exit(&sd_detach_mutex); 10359 10360 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10361 return (DDI_SUCCESS); 10362 10363 excl_open_fail: 10364 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10365 rval = EBUSY; 10366 10367 open_fail: 10368 mutex_exit(SD_MUTEX(un)); 10369 10370 /* 10371 * On a failed open we must exit the pm management. 10372 */ 10373 if (!nodelay) { 10374 sd_pm_exit(un); 10375 } 10376 open_failed_with_pm: 10377 sema_v(&un->un_semoclose); 10378 10379 mutex_enter(&sd_detach_mutex); 10380 un->un_opens_in_progress--; 10381 if (otyp == OTYP_LYR) { 10382 un->un_layer_count--; 10383 } 10384 mutex_exit(&sd_detach_mutex); 10385 10386 return (rval); 10387 } 10388 10389 10390 /* 10391 * Function: sdclose 10392 * 10393 * Description: Driver's close(9e) entry point function. 10394 * 10395 * Arguments: dev - device number 10396 * flag - file status flag, informational only 10397 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10398 * cred_p - user credential pointer 10399 * 10400 * Return Code: ENXIO 10401 * 10402 * Context: Kernel thread context 10403 */ 10404 /* ARGSUSED */ 10405 static int 10406 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10407 { 10408 struct sd_lun *un; 10409 uchar_t *cp; 10410 int part; 10411 int nodelay; 10412 int rval = 0; 10413 10414 /* Validate the open type */ 10415 if (otyp >= OTYPCNT) { 10416 return (ENXIO); 10417 } 10418 10419 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10420 return (ENXIO); 10421 } 10422 10423 part = SDPART(dev); 10424 nodelay = flag & (FNDELAY | FNONBLOCK); 10425 10426 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10427 "sdclose: close of part %d type %d\n", part, otyp); 10428 10429 /* 10430 * We use a semaphore here in order to serialize 10431 * open and close requests on the device. 10432 */ 10433 sema_p(&un->un_semoclose); 10434 10435 mutex_enter(SD_MUTEX(un)); 10436 10437 /* Don't proceed if power is being changed. */ 10438 while (un->un_state == SD_STATE_PM_CHANGING) { 10439 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10440 } 10441 10442 if (un->un_exclopen & (1 << part)) { 10443 un->un_exclopen &= ~(1 << part); 10444 } 10445 10446 /* Update the open partition map */ 10447 if (otyp == OTYP_LYR) { 10448 un->un_ocmap.lyropen[part] -= 1; 10449 } else { 10450 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10451 } 10452 10453 cp = &un->un_ocmap.chkd[0]; 10454 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10455 if (*cp != NULL) { 10456 break; 10457 } 10458 cp++; 10459 } 10460 10461 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10462 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10463 10464 /* 10465 * We avoid persistance upon the last close, and set 10466 * the throttle back to the maximum. 10467 */ 10468 un->un_throttle = un->un_saved_throttle; 10469 10470 if (un->un_state == SD_STATE_OFFLINE) { 10471 if (un->un_f_is_fibre == FALSE) { 10472 scsi_log(SD_DEVINFO(un), sd_label, 10473 CE_WARN, "offline\n"); 10474 } 10475 un->un_f_geometry_is_valid = FALSE; 10476 10477 } else { 10478 /* 10479 * Flush any outstanding writes in NVRAM cache. 10480 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10481 * cmd, it may not work for non-Pluto devices. 10482 * SYNCHRONIZE CACHE is not required for removables, 10483 * except DVD-RAM drives. 10484 * 10485 * Also note: because SYNCHRONIZE CACHE is currently 10486 * the only command issued here that requires the 10487 * drive be powered up, only do the power up before 10488 * sending the Sync Cache command. If additional 10489 * commands are added which require a powered up 10490 * drive, the following sequence may have to change. 10491 * 10492 * And finally, note that parallel SCSI on SPARC 10493 * only issues a Sync Cache to DVD-RAM, a newly 10494 * supported device. 10495 */ 10496 #if defined(__i386) || defined(__amd64) 10497 if (un->un_f_sync_cache_supported || 10498 un->un_f_dvdram_writable_device == TRUE) { 10499 #else 10500 if (un->un_f_dvdram_writable_device == TRUE) { 10501 #endif 10502 mutex_exit(SD_MUTEX(un)); 10503 if (sd_pm_entry(un) == DDI_SUCCESS) { 10504 rval = 10505 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10506 NULL); 10507 /* ignore error if not supported */ 10508 if (rval == ENOTSUP) { 10509 rval = 0; 10510 } else if (rval != 0) { 10511 rval = EIO; 10512 } 10513 sd_pm_exit(un); 10514 } else { 10515 rval = EIO; 10516 } 10517 mutex_enter(SD_MUTEX(un)); 10518 } 10519 10520 /* 10521 * For devices which supports DOOR_LOCK, send an ALLOW 10522 * MEDIA REMOVAL command, but don't get upset if it 10523 * fails. We need to raise the power of the drive before 10524 * we can call sd_send_scsi_DOORLOCK() 10525 */ 10526 if (un->un_f_doorlock_supported) { 10527 mutex_exit(SD_MUTEX(un)); 10528 if (sd_pm_entry(un) == DDI_SUCCESS) { 10529 rval = sd_send_scsi_DOORLOCK(un, 10530 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10531 10532 sd_pm_exit(un); 10533 if (ISCD(un) && (rval != 0) && 10534 (nodelay != 0)) { 10535 rval = ENXIO; 10536 } 10537 } else { 10538 rval = EIO; 10539 } 10540 mutex_enter(SD_MUTEX(un)); 10541 } 10542 10543 /* 10544 * If a device has removable media, invalidate all 10545 * parameters related to media, such as geometry, 10546 * blocksize, and blockcount. 10547 */ 10548 if (un->un_f_has_removable_media) { 10549 sr_ejected(un); 10550 } 10551 10552 /* 10553 * Destroy the cache (if it exists) which was 10554 * allocated for the write maps since this is 10555 * the last close for this media. 10556 */ 10557 if (un->un_wm_cache) { 10558 /* 10559 * Check if there are pending commands. 10560 * and if there are give a warning and 10561 * do not destroy the cache. 10562 */ 10563 if (un->un_ncmds_in_driver > 0) { 10564 scsi_log(SD_DEVINFO(un), 10565 sd_label, CE_WARN, 10566 "Unable to clean up memory " 10567 "because of pending I/O\n"); 10568 } else { 10569 kmem_cache_destroy( 10570 un->un_wm_cache); 10571 un->un_wm_cache = NULL; 10572 } 10573 } 10574 } 10575 } 10576 10577 mutex_exit(SD_MUTEX(un)); 10578 sema_v(&un->un_semoclose); 10579 10580 if (otyp == OTYP_LYR) { 10581 mutex_enter(&sd_detach_mutex); 10582 /* 10583 * The detach routine may run when the layer count 10584 * drops to zero. 10585 */ 10586 un->un_layer_count--; 10587 mutex_exit(&sd_detach_mutex); 10588 } 10589 10590 return (rval); 10591 } 10592 10593 10594 /* 10595 * Function: sd_ready_and_valid 10596 * 10597 * Description: Test if device is ready and has a valid geometry. 10598 * 10599 * Arguments: dev - device number 10600 * un - driver soft state (unit) structure 10601 * 10602 * Return Code: SD_READY_VALID ready and valid label 10603 * SD_READY_NOT_VALID ready, geom ops never applicable 10604 * SD_NOT_READY_VALID not ready, no label 10605 * 10606 * Context: Never called at interrupt context. 10607 */ 10608 10609 static int 10610 sd_ready_and_valid(struct sd_lun *un) 10611 { 10612 struct sd_errstats *stp; 10613 uint64_t capacity; 10614 uint_t lbasize; 10615 int rval = SD_READY_VALID; 10616 char name_str[48]; 10617 10618 ASSERT(un != NULL); 10619 ASSERT(!mutex_owned(SD_MUTEX(un))); 10620 10621 mutex_enter(SD_MUTEX(un)); 10622 /* 10623 * If a device has removable media, we must check if media is 10624 * ready when checking if this device is ready and valid. 10625 */ 10626 if (un->un_f_has_removable_media) { 10627 mutex_exit(SD_MUTEX(un)); 10628 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10629 rval = SD_NOT_READY_VALID; 10630 mutex_enter(SD_MUTEX(un)); 10631 goto done; 10632 } 10633 10634 mutex_enter(SD_MUTEX(un)); 10635 if ((un->un_f_geometry_is_valid == FALSE) || 10636 (un->un_f_blockcount_is_valid == FALSE) || 10637 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10638 10639 /* capacity has to be read every open. */ 10640 mutex_exit(SD_MUTEX(un)); 10641 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10642 &lbasize, SD_PATH_DIRECT) != 0) { 10643 mutex_enter(SD_MUTEX(un)); 10644 un->un_f_geometry_is_valid = FALSE; 10645 rval = SD_NOT_READY_VALID; 10646 goto done; 10647 } else { 10648 mutex_enter(SD_MUTEX(un)); 10649 sd_update_block_info(un, lbasize, capacity); 10650 } 10651 } 10652 10653 /* 10654 * Check if the media in the device is writable or not. 10655 */ 10656 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10657 sd_check_for_writable_cd(un); 10658 } 10659 10660 } else { 10661 /* 10662 * Do a test unit ready to clear any unit attention from non-cd 10663 * devices. 10664 */ 10665 mutex_exit(SD_MUTEX(un)); 10666 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10667 mutex_enter(SD_MUTEX(un)); 10668 } 10669 10670 10671 /* 10672 * If this is a non 512 block device, allocate space for 10673 * the wmap cache. This is being done here since every time 10674 * a media is changed this routine will be called and the 10675 * block size is a function of media rather than device. 10676 */ 10677 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 10678 if (!(un->un_wm_cache)) { 10679 (void) snprintf(name_str, sizeof (name_str), 10680 "%s%d_cache", 10681 ddi_driver_name(SD_DEVINFO(un)), 10682 ddi_get_instance(SD_DEVINFO(un))); 10683 un->un_wm_cache = kmem_cache_create( 10684 name_str, sizeof (struct sd_w_map), 10685 8, sd_wm_cache_constructor, 10686 sd_wm_cache_destructor, NULL, 10687 (void *)un, NULL, 0); 10688 if (!(un->un_wm_cache)) { 10689 rval = ENOMEM; 10690 goto done; 10691 } 10692 } 10693 } 10694 10695 if (un->un_state == SD_STATE_NORMAL) { 10696 /* 10697 * If the target is not yet ready here (defined by a TUR 10698 * failure), invalidate the geometry and print an 'offline' 10699 * message. This is a legacy message, as the state of the 10700 * target is not actually changed to SD_STATE_OFFLINE. 10701 * 10702 * If the TUR fails for EACCES (Reservation Conflict), it 10703 * means there actually is nothing wrong with the target that 10704 * would require invalidating the geometry, so continue in 10705 * that case as if the TUR was successful. 10706 */ 10707 int err; 10708 10709 mutex_exit(SD_MUTEX(un)); 10710 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10711 mutex_enter(SD_MUTEX(un)); 10712 10713 if ((err != 0) && (err != EACCES)) { 10714 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10715 "offline\n"); 10716 un->un_f_geometry_is_valid = FALSE; 10717 rval = SD_NOT_READY_VALID; 10718 goto done; 10719 } 10720 } 10721 10722 if (un->un_f_format_in_progress == FALSE) { 10723 /* 10724 * Note: sd_validate_geometry may return TRUE, but that does 10725 * not necessarily mean un_f_geometry_is_valid == TRUE! 10726 */ 10727 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10728 if (rval == ENOTSUP) { 10729 if (un->un_f_geometry_is_valid == TRUE) 10730 rval = 0; 10731 else { 10732 rval = SD_READY_NOT_VALID; 10733 goto done; 10734 } 10735 } 10736 if (rval != 0) { 10737 /* 10738 * We don't check the validity of geometry for 10739 * CDROMs. Also we assume we have a good label 10740 * even if sd_validate_geometry returned ENOMEM. 10741 */ 10742 if (!ISCD(un) && rval != ENOMEM) { 10743 rval = SD_NOT_READY_VALID; 10744 goto done; 10745 } 10746 } 10747 } 10748 10749 #ifdef DOESNTWORK /* on eliteII, see 1118607 */ 10750 /* 10751 * check to see if this disk is write protected, if it is and we have 10752 * not set read-only, then fail 10753 */ 10754 if ((flag & FWRITE) && (sr_check_wp(dev))) { 10755 New_state(un, SD_STATE_CLOSED); 10756 goto done; 10757 } 10758 #endif 10759 10760 /* 10761 * If this device supports DOOR_LOCK command, try and send 10762 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10763 * if it fails. For a CD, however, it is an error 10764 */ 10765 if (un->un_f_doorlock_supported) { 10766 mutex_exit(SD_MUTEX(un)); 10767 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10768 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10769 rval = SD_NOT_READY_VALID; 10770 mutex_enter(SD_MUTEX(un)); 10771 goto done; 10772 } 10773 mutex_enter(SD_MUTEX(un)); 10774 } 10775 10776 /* The state has changed, inform the media watch routines */ 10777 un->un_mediastate = DKIO_INSERTED; 10778 cv_broadcast(&un->un_state_cv); 10779 rval = SD_READY_VALID; 10780 10781 done: 10782 10783 /* 10784 * Initialize the capacity kstat value, if no media previously 10785 * (capacity kstat is 0) and a media has been inserted 10786 * (un_blockcount > 0). 10787 */ 10788 if (un->un_errstats != NULL) { 10789 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10790 if ((stp->sd_capacity.value.ui64 == 0) && 10791 (un->un_f_blockcount_is_valid == TRUE)) { 10792 stp->sd_capacity.value.ui64 = 10793 (uint64_t)((uint64_t)un->un_blockcount * 10794 un->un_sys_blocksize); 10795 } 10796 } 10797 10798 mutex_exit(SD_MUTEX(un)); 10799 return (rval); 10800 } 10801 10802 10803 /* 10804 * Function: sdmin 10805 * 10806 * Description: Routine to limit the size of a data transfer. Used in 10807 * conjunction with physio(9F). 10808 * 10809 * Arguments: bp - pointer to the indicated buf(9S) struct. 10810 * 10811 * Context: Kernel thread context. 10812 */ 10813 10814 static void 10815 sdmin(struct buf *bp) 10816 { 10817 struct sd_lun *un; 10818 int instance; 10819 10820 instance = SDUNIT(bp->b_edev); 10821 10822 un = ddi_get_soft_state(sd_state, instance); 10823 ASSERT(un != NULL); 10824 10825 if (bp->b_bcount > un->un_max_xfer_size) { 10826 bp->b_bcount = un->un_max_xfer_size; 10827 } 10828 } 10829 10830 10831 /* 10832 * Function: sdread 10833 * 10834 * Description: Driver's read(9e) entry point function. 10835 * 10836 * Arguments: dev - device number 10837 * uio - structure pointer describing where data is to be stored 10838 * in user's space 10839 * cred_p - user credential pointer 10840 * 10841 * Return Code: ENXIO 10842 * EIO 10843 * EINVAL 10844 * value returned by physio 10845 * 10846 * Context: Kernel thread context. 10847 */ 10848 /* ARGSUSED */ 10849 static int 10850 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10851 { 10852 struct sd_lun *un = NULL; 10853 int secmask; 10854 int err; 10855 10856 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10857 return (ENXIO); 10858 } 10859 10860 ASSERT(!mutex_owned(SD_MUTEX(un))); 10861 10862 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10863 mutex_enter(SD_MUTEX(un)); 10864 /* 10865 * Because the call to sd_ready_and_valid will issue I/O we 10866 * must wait here if either the device is suspended or 10867 * if it's power level is changing. 10868 */ 10869 while ((un->un_state == SD_STATE_SUSPENDED) || 10870 (un->un_state == SD_STATE_PM_CHANGING)) { 10871 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10872 } 10873 un->un_ncmds_in_driver++; 10874 mutex_exit(SD_MUTEX(un)); 10875 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10876 mutex_enter(SD_MUTEX(un)); 10877 un->un_ncmds_in_driver--; 10878 ASSERT(un->un_ncmds_in_driver >= 0); 10879 mutex_exit(SD_MUTEX(un)); 10880 return (EIO); 10881 } 10882 mutex_enter(SD_MUTEX(un)); 10883 un->un_ncmds_in_driver--; 10884 ASSERT(un->un_ncmds_in_driver >= 0); 10885 mutex_exit(SD_MUTEX(un)); 10886 } 10887 10888 /* 10889 * Read requests are restricted to multiples of the system block size. 10890 */ 10891 secmask = un->un_sys_blocksize - 1; 10892 10893 if (uio->uio_loffset & ((offset_t)(secmask))) { 10894 SD_ERROR(SD_LOG_READ_WRITE, un, 10895 "sdread: file offset not modulo %d\n", 10896 un->un_sys_blocksize); 10897 err = EINVAL; 10898 } else if (uio->uio_iov->iov_len & (secmask)) { 10899 SD_ERROR(SD_LOG_READ_WRITE, un, 10900 "sdread: transfer length not modulo %d\n", 10901 un->un_sys_blocksize); 10902 err = EINVAL; 10903 } else { 10904 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10905 } 10906 return (err); 10907 } 10908 10909 10910 /* 10911 * Function: sdwrite 10912 * 10913 * Description: Driver's write(9e) entry point function. 10914 * 10915 * Arguments: dev - device number 10916 * uio - structure pointer describing where data is stored in 10917 * user's space 10918 * cred_p - user credential pointer 10919 * 10920 * Return Code: ENXIO 10921 * EIO 10922 * EINVAL 10923 * value returned by physio 10924 * 10925 * Context: Kernel thread context. 10926 */ 10927 /* ARGSUSED */ 10928 static int 10929 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10930 { 10931 struct sd_lun *un = NULL; 10932 int secmask; 10933 int err; 10934 10935 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10936 return (ENXIO); 10937 } 10938 10939 ASSERT(!mutex_owned(SD_MUTEX(un))); 10940 10941 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10942 mutex_enter(SD_MUTEX(un)); 10943 /* 10944 * Because the call to sd_ready_and_valid will issue I/O we 10945 * must wait here if either the device is suspended or 10946 * if it's power level is changing. 10947 */ 10948 while ((un->un_state == SD_STATE_SUSPENDED) || 10949 (un->un_state == SD_STATE_PM_CHANGING)) { 10950 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10951 } 10952 un->un_ncmds_in_driver++; 10953 mutex_exit(SD_MUTEX(un)); 10954 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10955 mutex_enter(SD_MUTEX(un)); 10956 un->un_ncmds_in_driver--; 10957 ASSERT(un->un_ncmds_in_driver >= 0); 10958 mutex_exit(SD_MUTEX(un)); 10959 return (EIO); 10960 } 10961 mutex_enter(SD_MUTEX(un)); 10962 un->un_ncmds_in_driver--; 10963 ASSERT(un->un_ncmds_in_driver >= 0); 10964 mutex_exit(SD_MUTEX(un)); 10965 } 10966 10967 /* 10968 * Write requests are restricted to multiples of the system block size. 10969 */ 10970 secmask = un->un_sys_blocksize - 1; 10971 10972 if (uio->uio_loffset & ((offset_t)(secmask))) { 10973 SD_ERROR(SD_LOG_READ_WRITE, un, 10974 "sdwrite: file offset not modulo %d\n", 10975 un->un_sys_blocksize); 10976 err = EINVAL; 10977 } else if (uio->uio_iov->iov_len & (secmask)) { 10978 SD_ERROR(SD_LOG_READ_WRITE, un, 10979 "sdwrite: transfer length not modulo %d\n", 10980 un->un_sys_blocksize); 10981 err = EINVAL; 10982 } else { 10983 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10984 } 10985 return (err); 10986 } 10987 10988 10989 /* 10990 * Function: sdaread 10991 * 10992 * Description: Driver's aread(9e) entry point function. 10993 * 10994 * Arguments: dev - device number 10995 * aio - structure pointer describing where data is to be stored 10996 * cred_p - user credential pointer 10997 * 10998 * Return Code: ENXIO 10999 * EIO 11000 * EINVAL 11001 * value returned by aphysio 11002 * 11003 * Context: Kernel thread context. 11004 */ 11005 /* ARGSUSED */ 11006 static int 11007 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11008 { 11009 struct sd_lun *un = NULL; 11010 struct uio *uio = aio->aio_uio; 11011 int secmask; 11012 int err; 11013 11014 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11015 return (ENXIO); 11016 } 11017 11018 ASSERT(!mutex_owned(SD_MUTEX(un))); 11019 11020 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11021 mutex_enter(SD_MUTEX(un)); 11022 /* 11023 * Because the call to sd_ready_and_valid will issue I/O we 11024 * must wait here if either the device is suspended or 11025 * if it's power level is changing. 11026 */ 11027 while ((un->un_state == SD_STATE_SUSPENDED) || 11028 (un->un_state == SD_STATE_PM_CHANGING)) { 11029 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11030 } 11031 un->un_ncmds_in_driver++; 11032 mutex_exit(SD_MUTEX(un)); 11033 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11034 mutex_enter(SD_MUTEX(un)); 11035 un->un_ncmds_in_driver--; 11036 ASSERT(un->un_ncmds_in_driver >= 0); 11037 mutex_exit(SD_MUTEX(un)); 11038 return (EIO); 11039 } 11040 mutex_enter(SD_MUTEX(un)); 11041 un->un_ncmds_in_driver--; 11042 ASSERT(un->un_ncmds_in_driver >= 0); 11043 mutex_exit(SD_MUTEX(un)); 11044 } 11045 11046 /* 11047 * Read requests are restricted to multiples of the system block size. 11048 */ 11049 secmask = un->un_sys_blocksize - 1; 11050 11051 if (uio->uio_loffset & ((offset_t)(secmask))) { 11052 SD_ERROR(SD_LOG_READ_WRITE, un, 11053 "sdaread: file offset not modulo %d\n", 11054 un->un_sys_blocksize); 11055 err = EINVAL; 11056 } else if (uio->uio_iov->iov_len & (secmask)) { 11057 SD_ERROR(SD_LOG_READ_WRITE, un, 11058 "sdaread: transfer length not modulo %d\n", 11059 un->un_sys_blocksize); 11060 err = EINVAL; 11061 } else { 11062 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11063 } 11064 return (err); 11065 } 11066 11067 11068 /* 11069 * Function: sdawrite 11070 * 11071 * Description: Driver's awrite(9e) entry point function. 11072 * 11073 * Arguments: dev - device number 11074 * aio - structure pointer describing where data is stored 11075 * cred_p - user credential pointer 11076 * 11077 * Return Code: ENXIO 11078 * EIO 11079 * EINVAL 11080 * value returned by aphysio 11081 * 11082 * Context: Kernel thread context. 11083 */ 11084 /* ARGSUSED */ 11085 static int 11086 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11087 { 11088 struct sd_lun *un = NULL; 11089 struct uio *uio = aio->aio_uio; 11090 int secmask; 11091 int err; 11092 11093 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11094 return (ENXIO); 11095 } 11096 11097 ASSERT(!mutex_owned(SD_MUTEX(un))); 11098 11099 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11100 mutex_enter(SD_MUTEX(un)); 11101 /* 11102 * Because the call to sd_ready_and_valid will issue I/O we 11103 * must wait here if either the device is suspended or 11104 * if it's power level is changing. 11105 */ 11106 while ((un->un_state == SD_STATE_SUSPENDED) || 11107 (un->un_state == SD_STATE_PM_CHANGING)) { 11108 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11109 } 11110 un->un_ncmds_in_driver++; 11111 mutex_exit(SD_MUTEX(un)); 11112 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11113 mutex_enter(SD_MUTEX(un)); 11114 un->un_ncmds_in_driver--; 11115 ASSERT(un->un_ncmds_in_driver >= 0); 11116 mutex_exit(SD_MUTEX(un)); 11117 return (EIO); 11118 } 11119 mutex_enter(SD_MUTEX(un)); 11120 un->un_ncmds_in_driver--; 11121 ASSERT(un->un_ncmds_in_driver >= 0); 11122 mutex_exit(SD_MUTEX(un)); 11123 } 11124 11125 /* 11126 * Write requests are restricted to multiples of the system block size. 11127 */ 11128 secmask = un->un_sys_blocksize - 1; 11129 11130 if (uio->uio_loffset & ((offset_t)(secmask))) { 11131 SD_ERROR(SD_LOG_READ_WRITE, un, 11132 "sdawrite: file offset not modulo %d\n", 11133 un->un_sys_blocksize); 11134 err = EINVAL; 11135 } else if (uio->uio_iov->iov_len & (secmask)) { 11136 SD_ERROR(SD_LOG_READ_WRITE, un, 11137 "sdawrite: transfer length not modulo %d\n", 11138 un->un_sys_blocksize); 11139 err = EINVAL; 11140 } else { 11141 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11142 } 11143 return (err); 11144 } 11145 11146 11147 11148 11149 11150 /* 11151 * Driver IO processing follows the following sequence: 11152 * 11153 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11154 * | | ^ 11155 * v v | 11156 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11157 * | | | | 11158 * v | | | 11159 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11160 * | | ^ ^ 11161 * v v | | 11162 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11163 * | | | | 11164 * +---+ | +------------+ +-------+ 11165 * | | | | 11166 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11167 * | v | | 11168 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11169 * | | ^ | 11170 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11171 * | v | | 11172 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11173 * | | ^ | 11174 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11175 * | v | | 11176 * | sd_checksum_iostart() sd_checksum_iodone() | 11177 * | | ^ | 11178 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11179 * | v | | 11180 * | sd_pm_iostart() sd_pm_iodone() | 11181 * | | ^ | 11182 * | | | | 11183 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11184 * | ^ 11185 * v | 11186 * sd_core_iostart() | 11187 * | | 11188 * | +------>(*destroypkt)() 11189 * +-> sd_start_cmds() <-+ | | 11190 * | | | v 11191 * | | | scsi_destroy_pkt(9F) 11192 * | | | 11193 * +->(*initpkt)() +- sdintr() 11194 * | | | | 11195 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11196 * | +-> scsi_setup_cdb(9F) | 11197 * | | 11198 * +--> scsi_transport(9F) | 11199 * | | 11200 * +----> SCSA ---->+ 11201 * 11202 * 11203 * This code is based upon the following presumtions: 11204 * 11205 * - iostart and iodone functions operate on buf(9S) structures. These 11206 * functions perform the necessary operations on the buf(9S) and pass 11207 * them along to the next function in the chain by using the macros 11208 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11209 * (for iodone side functions). 11210 * 11211 * - The iostart side functions may sleep. The iodone side functions 11212 * are called under interrupt context and may NOT sleep. Therefore 11213 * iodone side functions also may not call iostart side functions. 11214 * (NOTE: iostart side functions should NOT sleep for memory, as 11215 * this could result in deadlock.) 11216 * 11217 * - An iostart side function may call its corresponding iodone side 11218 * function directly (if necessary). 11219 * 11220 * - In the event of an error, an iostart side function can return a buf(9S) 11221 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11222 * b_error in the usual way of course). 11223 * 11224 * - The taskq mechanism may be used by the iodone side functions to dispatch 11225 * requests to the iostart side functions. The iostart side functions in 11226 * this case would be called under the context of a taskq thread, so it's 11227 * OK for them to block/sleep/spin in this case. 11228 * 11229 * - iostart side functions may allocate "shadow" buf(9S) structs and 11230 * pass them along to the next function in the chain. The corresponding 11231 * iodone side functions must coalesce the "shadow" bufs and return 11232 * the "original" buf to the next higher layer. 11233 * 11234 * - The b_private field of the buf(9S) struct holds a pointer to 11235 * an sd_xbuf struct, which contains information needed to 11236 * construct the scsi_pkt for the command. 11237 * 11238 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11239 * layer must acquire & release the SD_MUTEX(un) as needed. 11240 */ 11241 11242 11243 /* 11244 * Create taskq for all targets in the system. This is created at 11245 * _init(9E) and destroyed at _fini(9E). 11246 * 11247 * Note: here we set the minalloc to a reasonably high number to ensure that 11248 * we will have an adequate supply of task entries available at interrupt time. 11249 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11250 * sd_create_taskq(). Since we do not want to sleep for allocations at 11251 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11252 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11253 * requests any one instant in time. 11254 */ 11255 #define SD_TASKQ_NUMTHREADS 8 11256 #define SD_TASKQ_MINALLOC 256 11257 #define SD_TASKQ_MAXALLOC 256 11258 11259 static taskq_t *sd_tq = NULL; 11260 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11261 11262 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11263 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11264 11265 /* 11266 * The following task queue is being created for the write part of 11267 * read-modify-write of non-512 block size devices. 11268 * Limit the number of threads to 1 for now. This number has been choosen 11269 * considering the fact that it applies only to dvd ram drives/MO drives 11270 * currently. Performance for which is not main criteria at this stage. 11271 * Note: It needs to be explored if we can use a single taskq in future 11272 */ 11273 #define SD_WMR_TASKQ_NUMTHREADS 1 11274 static taskq_t *sd_wmr_tq = NULL; 11275 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11276 11277 /* 11278 * Function: sd_taskq_create 11279 * 11280 * Description: Create taskq thread(s) and preallocate task entries 11281 * 11282 * Return Code: Returns a pointer to the allocated taskq_t. 11283 * 11284 * Context: Can sleep. Requires blockable context. 11285 * 11286 * Notes: - The taskq() facility currently is NOT part of the DDI. 11287 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11288 * - taskq_create() will block for memory, also it will panic 11289 * if it cannot create the requested number of threads. 11290 * - Currently taskq_create() creates threads that cannot be 11291 * swapped. 11292 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11293 * supply of taskq entries at interrupt time (ie, so that we 11294 * do not have to sleep for memory) 11295 */ 11296 11297 static void 11298 sd_taskq_create(void) 11299 { 11300 char taskq_name[TASKQ_NAMELEN]; 11301 11302 ASSERT(sd_tq == NULL); 11303 ASSERT(sd_wmr_tq == NULL); 11304 11305 (void) snprintf(taskq_name, sizeof (taskq_name), 11306 "%s_drv_taskq", sd_label); 11307 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11308 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11309 TASKQ_PREPOPULATE)); 11310 11311 (void) snprintf(taskq_name, sizeof (taskq_name), 11312 "%s_rmw_taskq", sd_label); 11313 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11314 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11315 TASKQ_PREPOPULATE)); 11316 } 11317 11318 11319 /* 11320 * Function: sd_taskq_delete 11321 * 11322 * Description: Complementary cleanup routine for sd_taskq_create(). 11323 * 11324 * Context: Kernel thread context. 11325 */ 11326 11327 static void 11328 sd_taskq_delete(void) 11329 { 11330 ASSERT(sd_tq != NULL); 11331 ASSERT(sd_wmr_tq != NULL); 11332 taskq_destroy(sd_tq); 11333 taskq_destroy(sd_wmr_tq); 11334 sd_tq = NULL; 11335 sd_wmr_tq = NULL; 11336 } 11337 11338 11339 /* 11340 * Function: sdstrategy 11341 * 11342 * Description: Driver's strategy (9E) entry point function. 11343 * 11344 * Arguments: bp - pointer to buf(9S) 11345 * 11346 * Return Code: Always returns zero 11347 * 11348 * Context: Kernel thread context. 11349 */ 11350 11351 static int 11352 sdstrategy(struct buf *bp) 11353 { 11354 struct sd_lun *un; 11355 11356 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11357 if (un == NULL) { 11358 bioerror(bp, EIO); 11359 bp->b_resid = bp->b_bcount; 11360 biodone(bp); 11361 return (0); 11362 } 11363 /* As was done in the past, fail new cmds. if state is dumping. */ 11364 if (un->un_state == SD_STATE_DUMPING) { 11365 bioerror(bp, ENXIO); 11366 bp->b_resid = bp->b_bcount; 11367 biodone(bp); 11368 return (0); 11369 } 11370 11371 ASSERT(!mutex_owned(SD_MUTEX(un))); 11372 11373 /* 11374 * Commands may sneak in while we released the mutex in 11375 * DDI_SUSPEND, we should block new commands. However, old 11376 * commands that are still in the driver at this point should 11377 * still be allowed to drain. 11378 */ 11379 mutex_enter(SD_MUTEX(un)); 11380 /* 11381 * Must wait here if either the device is suspended or 11382 * if it's power level is changing. 11383 */ 11384 while ((un->un_state == SD_STATE_SUSPENDED) || 11385 (un->un_state == SD_STATE_PM_CHANGING)) { 11386 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11387 } 11388 11389 un->un_ncmds_in_driver++; 11390 11391 /* 11392 * atapi: Since we are running the CD for now in PIO mode we need to 11393 * call bp_mapin here to avoid bp_mapin called interrupt context under 11394 * the HBA's init_pkt routine. 11395 */ 11396 if (un->un_f_cfg_is_atapi == TRUE) { 11397 mutex_exit(SD_MUTEX(un)); 11398 bp_mapin(bp); 11399 mutex_enter(SD_MUTEX(un)); 11400 } 11401 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11402 un->un_ncmds_in_driver); 11403 11404 mutex_exit(SD_MUTEX(un)); 11405 11406 /* 11407 * This will (eventually) allocate the sd_xbuf area and 11408 * call sd_xbuf_strategy(). We just want to return the 11409 * result of ddi_xbuf_qstrategy so that we have an opt- 11410 * imized tail call which saves us a stack frame. 11411 */ 11412 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11413 } 11414 11415 11416 /* 11417 * Function: sd_xbuf_strategy 11418 * 11419 * Description: Function for initiating IO operations via the 11420 * ddi_xbuf_qstrategy() mechanism. 11421 * 11422 * Context: Kernel thread context. 11423 */ 11424 11425 static void 11426 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11427 { 11428 struct sd_lun *un = arg; 11429 11430 ASSERT(bp != NULL); 11431 ASSERT(xp != NULL); 11432 ASSERT(un != NULL); 11433 ASSERT(!mutex_owned(SD_MUTEX(un))); 11434 11435 /* 11436 * Initialize the fields in the xbuf and save a pointer to the 11437 * xbuf in bp->b_private. 11438 */ 11439 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11440 11441 /* Send the buf down the iostart chain */ 11442 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11443 } 11444 11445 11446 /* 11447 * Function: sd_xbuf_init 11448 * 11449 * Description: Prepare the given sd_xbuf struct for use. 11450 * 11451 * Arguments: un - ptr to softstate 11452 * bp - ptr to associated buf(9S) 11453 * xp - ptr to associated sd_xbuf 11454 * chain_type - IO chain type to use: 11455 * SD_CHAIN_NULL 11456 * SD_CHAIN_BUFIO 11457 * SD_CHAIN_USCSI 11458 * SD_CHAIN_DIRECT 11459 * SD_CHAIN_DIRECT_PRIORITY 11460 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11461 * initialization; may be NULL if none. 11462 * 11463 * Context: Kernel thread context 11464 */ 11465 11466 static void 11467 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11468 uchar_t chain_type, void *pktinfop) 11469 { 11470 int index; 11471 11472 ASSERT(un != NULL); 11473 ASSERT(bp != NULL); 11474 ASSERT(xp != NULL); 11475 11476 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11477 bp, chain_type); 11478 11479 xp->xb_un = un; 11480 xp->xb_pktp = NULL; 11481 xp->xb_pktinfo = pktinfop; 11482 xp->xb_private = bp->b_private; 11483 xp->xb_blkno = (daddr_t)bp->b_blkno; 11484 11485 /* 11486 * Set up the iostart and iodone chain indexes in the xbuf, based 11487 * upon the specified chain type to use. 11488 */ 11489 switch (chain_type) { 11490 case SD_CHAIN_NULL: 11491 /* 11492 * Fall thru to just use the values for the buf type, even 11493 * tho for the NULL chain these values will never be used. 11494 */ 11495 /* FALLTHRU */ 11496 case SD_CHAIN_BUFIO: 11497 index = un->un_buf_chain_type; 11498 break; 11499 case SD_CHAIN_USCSI: 11500 index = un->un_uscsi_chain_type; 11501 break; 11502 case SD_CHAIN_DIRECT: 11503 index = un->un_direct_chain_type; 11504 break; 11505 case SD_CHAIN_DIRECT_PRIORITY: 11506 index = un->un_priority_chain_type; 11507 break; 11508 default: 11509 /* We're really broken if we ever get here... */ 11510 panic("sd_xbuf_init: illegal chain type!"); 11511 /*NOTREACHED*/ 11512 } 11513 11514 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11515 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11516 11517 /* 11518 * It might be a bit easier to simply bzero the entire xbuf above, 11519 * but it turns out that since we init a fair number of members anyway, 11520 * we save a fair number cycles by doing explicit assignment of zero. 11521 */ 11522 xp->xb_pkt_flags = 0; 11523 xp->xb_dma_resid = 0; 11524 xp->xb_retry_count = 0; 11525 xp->xb_victim_retry_count = 0; 11526 xp->xb_ua_retry_count = 0; 11527 xp->xb_sense_bp = NULL; 11528 xp->xb_sense_status = 0; 11529 xp->xb_sense_state = 0; 11530 xp->xb_sense_resid = 0; 11531 11532 bp->b_private = xp; 11533 bp->b_flags &= ~(B_DONE | B_ERROR); 11534 bp->b_resid = 0; 11535 bp->av_forw = NULL; 11536 bp->av_back = NULL; 11537 bioerror(bp, 0); 11538 11539 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11540 } 11541 11542 11543 /* 11544 * Function: sd_uscsi_strategy 11545 * 11546 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11547 * 11548 * Arguments: bp - buf struct ptr 11549 * 11550 * Return Code: Always returns 0 11551 * 11552 * Context: Kernel thread context 11553 */ 11554 11555 static int 11556 sd_uscsi_strategy(struct buf *bp) 11557 { 11558 struct sd_lun *un; 11559 struct sd_uscsi_info *uip; 11560 struct sd_xbuf *xp; 11561 uchar_t chain_type; 11562 11563 ASSERT(bp != NULL); 11564 11565 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11566 if (un == NULL) { 11567 bioerror(bp, EIO); 11568 bp->b_resid = bp->b_bcount; 11569 biodone(bp); 11570 return (0); 11571 } 11572 11573 ASSERT(!mutex_owned(SD_MUTEX(un))); 11574 11575 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11576 11577 mutex_enter(SD_MUTEX(un)); 11578 /* 11579 * atapi: Since we are running the CD for now in PIO mode we need to 11580 * call bp_mapin here to avoid bp_mapin called interrupt context under 11581 * the HBA's init_pkt routine. 11582 */ 11583 if (un->un_f_cfg_is_atapi == TRUE) { 11584 mutex_exit(SD_MUTEX(un)); 11585 bp_mapin(bp); 11586 mutex_enter(SD_MUTEX(un)); 11587 } 11588 un->un_ncmds_in_driver++; 11589 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11590 un->un_ncmds_in_driver); 11591 mutex_exit(SD_MUTEX(un)); 11592 11593 /* 11594 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11595 */ 11596 ASSERT(bp->b_private != NULL); 11597 uip = (struct sd_uscsi_info *)bp->b_private; 11598 11599 switch (uip->ui_flags) { 11600 case SD_PATH_DIRECT: 11601 chain_type = SD_CHAIN_DIRECT; 11602 break; 11603 case SD_PATH_DIRECT_PRIORITY: 11604 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11605 break; 11606 default: 11607 chain_type = SD_CHAIN_USCSI; 11608 break; 11609 } 11610 11611 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11612 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11613 11614 /* Use the index obtained within xbuf_init */ 11615 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11616 11617 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11618 11619 return (0); 11620 } 11621 11622 11623 /* 11624 * These routines perform raw i/o operations. 11625 */ 11626 /*ARGSUSED*/ 11627 static void 11628 sduscsimin(struct buf *bp) 11629 { 11630 /* 11631 * do not break up because the CDB count would then 11632 * be incorrect and data underruns would result (incomplete 11633 * read/writes which would be retried and then failed, see 11634 * sdintr(). 11635 */ 11636 } 11637 11638 11639 11640 /* 11641 * Function: sd_send_scsi_cmd 11642 * 11643 * Description: Runs a USCSI command for user (when called thru sdioctl), 11644 * or for the driver 11645 * 11646 * Arguments: dev - the dev_t for the device 11647 * incmd - ptr to a valid uscsi_cmd struct 11648 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11649 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11650 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11651 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11652 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11653 * to use the USCSI "direct" chain and bypass the normal 11654 * command waitq. 11655 * 11656 * Return Code: 0 - successful completion of the given command 11657 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11658 * ENXIO - soft state not found for specified dev 11659 * EINVAL 11660 * EFAULT - copyin/copyout error 11661 * return code of biowait(9F) or physio(9F): 11662 * EIO - IO error, caller may check incmd->uscsi_status 11663 * ENXIO 11664 * EACCES - reservation conflict 11665 * 11666 * Context: Waits for command to complete. Can sleep. 11667 */ 11668 11669 static int 11670 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11671 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11672 int path_flag) 11673 { 11674 struct sd_uscsi_info *uip; 11675 struct uscsi_cmd *uscmd; 11676 struct sd_lun *un; 11677 struct buf *bp; 11678 int rval; 11679 int flags; 11680 11681 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11682 if (un == NULL) { 11683 return (ENXIO); 11684 } 11685 11686 ASSERT(!mutex_owned(SD_MUTEX(un))); 11687 11688 #ifdef SDDEBUG 11689 switch (dataspace) { 11690 case UIO_USERSPACE: 11691 SD_TRACE(SD_LOG_IO, un, 11692 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11693 break; 11694 case UIO_SYSSPACE: 11695 SD_TRACE(SD_LOG_IO, un, 11696 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11697 break; 11698 default: 11699 SD_TRACE(SD_LOG_IO, un, 11700 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11701 break; 11702 } 11703 #endif 11704 11705 /* 11706 * Perform resets directly; no need to generate a command to do it. 11707 */ 11708 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11709 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11710 RESET_ALL : RESET_TARGET; 11711 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11712 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11713 /* Reset attempt was unsuccessful */ 11714 SD_TRACE(SD_LOG_IO, un, 11715 "sd_send_scsi_cmd: reset: failure\n"); 11716 return (EIO); 11717 } 11718 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11719 return (0); 11720 } 11721 11722 /* Perfunctory sanity check... */ 11723 if (incmd->uscsi_cdblen <= 0) { 11724 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11725 "invalid uscsi_cdblen, returning EINVAL\n"); 11726 return (EINVAL); 11727 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 11728 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11729 "unsupported uscsi_cdblen, returning EINVAL\n"); 11730 return (EINVAL); 11731 } 11732 11733 /* 11734 * In order to not worry about where the uscsi structure came from 11735 * (or where the cdb it points to came from) we're going to make 11736 * kmem_alloc'd copies of them here. This will also allow reference 11737 * to the data they contain long after this process has gone to 11738 * sleep and its kernel stack has been unmapped, etc. 11739 * 11740 * First get some memory for the uscsi_cmd struct and copy the 11741 * contents of the given uscsi_cmd struct into it. 11742 */ 11743 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11744 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11745 11746 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11747 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11748 11749 /* 11750 * Now get some space for the CDB, and copy the given CDB into 11751 * it. Use ddi_copyin() in case the data is in user space. 11752 */ 11753 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11754 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11755 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11756 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11757 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11758 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11759 return (EFAULT); 11760 } 11761 11762 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11763 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11764 11765 bp = getrbuf(KM_SLEEP); 11766 11767 /* 11768 * Allocate an sd_uscsi_info struct and fill it with the info 11769 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11770 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11771 * since we allocate the buf here in this function, we do not 11772 * need to preserve the prior contents of b_private. 11773 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11774 */ 11775 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11776 uip->ui_flags = path_flag; 11777 uip->ui_cmdp = uscmd; 11778 bp->b_private = uip; 11779 11780 /* 11781 * Initialize Request Sense buffering, if requested. 11782 */ 11783 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11784 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11785 /* 11786 * Here uscmd->uscsi_rqbuf currently points to the caller's 11787 * buffer, but we replace this with a kernel buffer that 11788 * we allocate to use with the sense data. The sense data 11789 * (if present) gets copied into this new buffer before the 11790 * command is completed. Then we copy the sense data from 11791 * our allocated buf into the caller's buffer below. Note 11792 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11793 * below to perform the copy back to the caller's buf. 11794 */ 11795 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11796 if (rqbufspace == UIO_USERSPACE) { 11797 uscmd->uscsi_rqlen = SENSE_LENGTH; 11798 uscmd->uscsi_rqresid = SENSE_LENGTH; 11799 } else { 11800 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11801 uscmd->uscsi_rqlen = rlen; 11802 uscmd->uscsi_rqresid = rlen; 11803 } 11804 } else { 11805 uscmd->uscsi_rqbuf = NULL; 11806 uscmd->uscsi_rqlen = 0; 11807 uscmd->uscsi_rqresid = 0; 11808 } 11809 11810 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11811 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11812 11813 if (un->un_f_is_fibre == FALSE) { 11814 /* 11815 * Force asynchronous mode, if necessary. Doing this here 11816 * has the unfortunate effect of running other queued 11817 * commands async also, but since the main purpose of this 11818 * capability is downloading new drive firmware, we can 11819 * probably live with it. 11820 */ 11821 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11822 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11823 == 1) { 11824 if (scsi_ifsetcap(SD_ADDRESS(un), 11825 "synchronous", 0, 1) == 1) { 11826 SD_TRACE(SD_LOG_IO, un, 11827 "sd_send_scsi_cmd: forced async ok\n"); 11828 } else { 11829 SD_TRACE(SD_LOG_IO, un, 11830 "sd_send_scsi_cmd:\ 11831 forced async failed\n"); 11832 rval = EINVAL; 11833 goto done; 11834 } 11835 } 11836 } 11837 11838 /* 11839 * Re-enable synchronous mode, if requested 11840 */ 11841 if (uscmd->uscsi_flags & USCSI_SYNC) { 11842 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11843 == 0) { 11844 int i = scsi_ifsetcap(SD_ADDRESS(un), 11845 "synchronous", 1, 1); 11846 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11847 "re-enabled sync %s\n", 11848 (i == 1) ? "ok" : "failed"); 11849 } 11850 } 11851 } 11852 11853 /* 11854 * Commands sent with priority are intended for error recovery 11855 * situations, and do not have retries performed. 11856 */ 11857 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11858 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11859 } 11860 11861 /* 11862 * If we're going to do actual I/O, let physio do all the right things 11863 */ 11864 if (uscmd->uscsi_buflen != 0) { 11865 struct iovec aiov; 11866 struct uio auio; 11867 struct uio *uio = &auio; 11868 11869 bzero(&auio, sizeof (struct uio)); 11870 bzero(&aiov, sizeof (struct iovec)); 11871 aiov.iov_base = uscmd->uscsi_bufaddr; 11872 aiov.iov_len = uscmd->uscsi_buflen; 11873 uio->uio_iov = &aiov; 11874 11875 uio->uio_iovcnt = 1; 11876 uio->uio_resid = uscmd->uscsi_buflen; 11877 uio->uio_segflg = dataspace; 11878 11879 /* 11880 * physio() will block here until the command completes.... 11881 */ 11882 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 11883 11884 rval = physio(sd_uscsi_strategy, bp, dev, 11885 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 11886 sduscsimin, uio); 11887 11888 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11889 "returned from physio with 0x%x\n", rval); 11890 11891 } else { 11892 /* 11893 * We have to mimic what physio would do here! Argh! 11894 */ 11895 bp->b_flags = B_BUSY | 11896 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 11897 bp->b_edev = dev; 11898 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 11899 bp->b_bcount = 0; 11900 bp->b_blkno = 0; 11901 11902 SD_TRACE(SD_LOG_IO, un, 11903 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 11904 11905 (void) sd_uscsi_strategy(bp); 11906 11907 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 11908 11909 rval = biowait(bp); 11910 11911 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11912 "returned from biowait with 0x%x\n", rval); 11913 } 11914 11915 done: 11916 11917 #ifdef SDDEBUG 11918 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11919 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11920 uscmd->uscsi_status, uscmd->uscsi_resid); 11921 if (uscmd->uscsi_bufaddr != NULL) { 11922 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11923 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11924 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11925 if (dataspace == UIO_SYSSPACE) { 11926 SD_DUMP_MEMORY(un, SD_LOG_IO, 11927 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11928 uscmd->uscsi_buflen, SD_LOG_HEX); 11929 } 11930 } 11931 #endif 11932 11933 /* 11934 * Get the status and residual to return to the caller. 11935 */ 11936 incmd->uscsi_status = uscmd->uscsi_status; 11937 incmd->uscsi_resid = uscmd->uscsi_resid; 11938 11939 /* 11940 * If the caller wants sense data, copy back whatever sense data 11941 * we may have gotten, and update the relevant rqsense info. 11942 */ 11943 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11944 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11945 11946 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 11947 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 11948 11949 /* Update the Request Sense status and resid */ 11950 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 11951 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 11952 11953 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11954 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 11955 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 11956 11957 /* Copy out the sense data for user processes */ 11958 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 11959 int flags = 11960 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 11961 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 11962 rqlen, flags) != 0) { 11963 rval = EFAULT; 11964 } 11965 /* 11966 * Note: Can't touch incmd->uscsi_rqbuf so use 11967 * uscmd->uscsi_rqbuf instead. They're the same. 11968 */ 11969 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11970 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 11971 incmd->uscsi_rqbuf, rqlen); 11972 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 11973 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 11974 } 11975 } 11976 11977 /* 11978 * Free allocated resources and return; mapout the buf in case it was 11979 * mapped in by a lower layer. 11980 */ 11981 bp_mapout(bp); 11982 freerbuf(bp); 11983 kmem_free(uip, sizeof (struct sd_uscsi_info)); 11984 if (uscmd->uscsi_rqbuf != NULL) { 11985 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 11986 } 11987 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 11988 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11989 11990 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 11991 11992 return (rval); 11993 } 11994 11995 11996 /* 11997 * Function: sd_buf_iodone 11998 * 11999 * Description: Frees the sd_xbuf & returns the buf to its originator. 12000 * 12001 * Context: May be called from interrupt context. 12002 */ 12003 /* ARGSUSED */ 12004 static void 12005 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12006 { 12007 struct sd_xbuf *xp; 12008 12009 ASSERT(un != NULL); 12010 ASSERT(bp != NULL); 12011 ASSERT(!mutex_owned(SD_MUTEX(un))); 12012 12013 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12014 12015 xp = SD_GET_XBUF(bp); 12016 ASSERT(xp != NULL); 12017 12018 mutex_enter(SD_MUTEX(un)); 12019 12020 /* 12021 * Grab time when the cmd completed. 12022 * This is used for determining if the system has been 12023 * idle long enough to make it idle to the PM framework. 12024 * This is for lowering the overhead, and therefore improving 12025 * performance per I/O operation. 12026 */ 12027 un->un_pm_idle_time = ddi_get_time(); 12028 12029 un->un_ncmds_in_driver--; 12030 ASSERT(un->un_ncmds_in_driver >= 0); 12031 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12032 un->un_ncmds_in_driver); 12033 12034 mutex_exit(SD_MUTEX(un)); 12035 12036 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12037 biodone(bp); /* bp is gone after this */ 12038 12039 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12040 } 12041 12042 12043 /* 12044 * Function: sd_uscsi_iodone 12045 * 12046 * Description: Frees the sd_xbuf & returns the buf to its originator. 12047 * 12048 * Context: May be called from interrupt context. 12049 */ 12050 /* ARGSUSED */ 12051 static void 12052 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12053 { 12054 struct sd_xbuf *xp; 12055 12056 ASSERT(un != NULL); 12057 ASSERT(bp != NULL); 12058 12059 xp = SD_GET_XBUF(bp); 12060 ASSERT(xp != NULL); 12061 ASSERT(!mutex_owned(SD_MUTEX(un))); 12062 12063 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12064 12065 bp->b_private = xp->xb_private; 12066 12067 mutex_enter(SD_MUTEX(un)); 12068 12069 /* 12070 * Grab time when the cmd completed. 12071 * This is used for determining if the system has been 12072 * idle long enough to make it idle to the PM framework. 12073 * This is for lowering the overhead, and therefore improving 12074 * performance per I/O operation. 12075 */ 12076 un->un_pm_idle_time = ddi_get_time(); 12077 12078 un->un_ncmds_in_driver--; 12079 ASSERT(un->un_ncmds_in_driver >= 0); 12080 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12081 un->un_ncmds_in_driver); 12082 12083 mutex_exit(SD_MUTEX(un)); 12084 12085 kmem_free(xp, sizeof (struct sd_xbuf)); 12086 biodone(bp); 12087 12088 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12089 } 12090 12091 12092 /* 12093 * Function: sd_mapblockaddr_iostart 12094 * 12095 * Description: Verify request lies withing the partition limits for 12096 * the indicated minor device. Issue "overrun" buf if 12097 * request would exceed partition range. Converts 12098 * partition-relative block address to absolute. 12099 * 12100 * Context: Can sleep 12101 * 12102 * Issues: This follows what the old code did, in terms of accessing 12103 * some of the partition info in the unit struct without holding 12104 * the mutext. This is a general issue, if the partition info 12105 * can be altered while IO is in progress... as soon as we send 12106 * a buf, its partitioning can be invalid before it gets to the 12107 * device. Probably the right fix is to move partitioning out 12108 * of the driver entirely. 12109 */ 12110 12111 static void 12112 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12113 { 12114 daddr_t nblocks; /* #blocks in the given partition */ 12115 daddr_t blocknum; /* Block number specified by the buf */ 12116 size_t requested_nblocks; 12117 size_t available_nblocks; 12118 int partition; 12119 diskaddr_t partition_offset; 12120 struct sd_xbuf *xp; 12121 12122 12123 ASSERT(un != NULL); 12124 ASSERT(bp != NULL); 12125 ASSERT(!mutex_owned(SD_MUTEX(un))); 12126 12127 SD_TRACE(SD_LOG_IO_PARTITION, un, 12128 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12129 12130 xp = SD_GET_XBUF(bp); 12131 ASSERT(xp != NULL); 12132 12133 /* 12134 * If the geometry is not indicated as valid, attempt to access 12135 * the unit & verify the geometry/label. This can be the case for 12136 * removable-media devices, of if the device was opened in 12137 * NDELAY/NONBLOCK mode. 12138 */ 12139 if ((un->un_f_geometry_is_valid != TRUE) && 12140 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12141 /* 12142 * For removable devices it is possible to start an I/O 12143 * without a media by opening the device in nodelay mode. 12144 * Also for writable CDs there can be many scenarios where 12145 * there is no geometry yet but volume manager is trying to 12146 * issue a read() just because it can see TOC on the CD. So 12147 * do not print a message for removables. 12148 */ 12149 if (!un->un_f_has_removable_media) { 12150 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12151 "i/o to invalid geometry\n"); 12152 } 12153 bioerror(bp, EIO); 12154 bp->b_resid = bp->b_bcount; 12155 SD_BEGIN_IODONE(index, un, bp); 12156 return; 12157 } 12158 12159 partition = SDPART(bp->b_edev); 12160 12161 /* #blocks in partition */ 12162 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12163 12164 /* Use of a local variable potentially improves performance slightly */ 12165 partition_offset = un->un_offset[partition]; 12166 12167 /* 12168 * blocknum is the starting block number of the request. At this 12169 * point it is still relative to the start of the minor device. 12170 */ 12171 blocknum = xp->xb_blkno; 12172 12173 /* 12174 * Legacy: If the starting block number is one past the last block 12175 * in the partition, do not set B_ERROR in the buf. 12176 */ 12177 if (blocknum == nblocks) { 12178 goto error_exit; 12179 } 12180 12181 /* 12182 * Confirm that the first block of the request lies within the 12183 * partition limits. Also the requested number of bytes must be 12184 * a multiple of the system block size. 12185 */ 12186 if ((blocknum < 0) || (blocknum >= nblocks) || 12187 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12188 bp->b_flags |= B_ERROR; 12189 goto error_exit; 12190 } 12191 12192 /* 12193 * If the requsted # blocks exceeds the available # blocks, that 12194 * is an overrun of the partition. 12195 */ 12196 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12197 available_nblocks = (size_t)(nblocks - blocknum); 12198 ASSERT(nblocks >= blocknum); 12199 12200 if (requested_nblocks > available_nblocks) { 12201 /* 12202 * Allocate an "overrun" buf to allow the request to proceed 12203 * for the amount of space available in the partition. The 12204 * amount not transferred will be added into the b_resid 12205 * when the operation is complete. The overrun buf 12206 * replaces the original buf here, and the original buf 12207 * is saved inside the overrun buf, for later use. 12208 */ 12209 size_t resid = SD_SYSBLOCKS2BYTES(un, 12210 (offset_t)(requested_nblocks - available_nblocks)); 12211 size_t count = bp->b_bcount - resid; 12212 /* 12213 * Note: count is an unsigned entity thus it'll NEVER 12214 * be less than 0 so ASSERT the original values are 12215 * correct. 12216 */ 12217 ASSERT(bp->b_bcount >= resid); 12218 12219 bp = sd_bioclone_alloc(bp, count, blocknum, 12220 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12221 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12222 ASSERT(xp != NULL); 12223 } 12224 12225 /* At this point there should be no residual for this buf. */ 12226 ASSERT(bp->b_resid == 0); 12227 12228 /* Convert the block number to an absolute address. */ 12229 xp->xb_blkno += partition_offset; 12230 12231 SD_NEXT_IOSTART(index, un, bp); 12232 12233 SD_TRACE(SD_LOG_IO_PARTITION, un, 12234 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12235 12236 return; 12237 12238 error_exit: 12239 bp->b_resid = bp->b_bcount; 12240 SD_BEGIN_IODONE(index, un, bp); 12241 SD_TRACE(SD_LOG_IO_PARTITION, un, 12242 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12243 } 12244 12245 12246 /* 12247 * Function: sd_mapblockaddr_iodone 12248 * 12249 * Description: Completion-side processing for partition management. 12250 * 12251 * Context: May be called under interrupt context 12252 */ 12253 12254 static void 12255 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12256 { 12257 /* int partition; */ /* Not used, see below. */ 12258 ASSERT(un != NULL); 12259 ASSERT(bp != NULL); 12260 ASSERT(!mutex_owned(SD_MUTEX(un))); 12261 12262 SD_TRACE(SD_LOG_IO_PARTITION, un, 12263 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12264 12265 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12266 /* 12267 * We have an "overrun" buf to deal with... 12268 */ 12269 struct sd_xbuf *xp; 12270 struct buf *obp; /* ptr to the original buf */ 12271 12272 xp = SD_GET_XBUF(bp); 12273 ASSERT(xp != NULL); 12274 12275 /* Retrieve the pointer to the original buf */ 12276 obp = (struct buf *)xp->xb_private; 12277 ASSERT(obp != NULL); 12278 12279 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12280 bioerror(obp, bp->b_error); 12281 12282 sd_bioclone_free(bp); 12283 12284 /* 12285 * Get back the original buf. 12286 * Note that since the restoration of xb_blkno below 12287 * was removed, the sd_xbuf is not needed. 12288 */ 12289 bp = obp; 12290 /* 12291 * xp = SD_GET_XBUF(bp); 12292 * ASSERT(xp != NULL); 12293 */ 12294 } 12295 12296 /* 12297 * Convert sd->xb_blkno back to a minor-device relative value. 12298 * Note: this has been commented out, as it is not needed in the 12299 * current implementation of the driver (ie, since this function 12300 * is at the top of the layering chains, so the info will be 12301 * discarded) and it is in the "hot" IO path. 12302 * 12303 * partition = getminor(bp->b_edev) & SDPART_MASK; 12304 * xp->xb_blkno -= un->un_offset[partition]; 12305 */ 12306 12307 SD_NEXT_IODONE(index, un, bp); 12308 12309 SD_TRACE(SD_LOG_IO_PARTITION, un, 12310 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12311 } 12312 12313 12314 /* 12315 * Function: sd_mapblocksize_iostart 12316 * 12317 * Description: Convert between system block size (un->un_sys_blocksize) 12318 * and target block size (un->un_tgt_blocksize). 12319 * 12320 * Context: Can sleep to allocate resources. 12321 * 12322 * Assumptions: A higher layer has already performed any partition validation, 12323 * and converted the xp->xb_blkno to an absolute value relative 12324 * to the start of the device. 12325 * 12326 * It is also assumed that the higher layer has implemented 12327 * an "overrun" mechanism for the case where the request would 12328 * read/write beyond the end of a partition. In this case we 12329 * assume (and ASSERT) that bp->b_resid == 0. 12330 * 12331 * Note: The implementation for this routine assumes the target 12332 * block size remains constant between allocation and transport. 12333 */ 12334 12335 static void 12336 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12337 { 12338 struct sd_mapblocksize_info *bsp; 12339 struct sd_xbuf *xp; 12340 offset_t first_byte; 12341 daddr_t start_block, end_block; 12342 daddr_t request_bytes; 12343 ushort_t is_aligned = FALSE; 12344 12345 ASSERT(un != NULL); 12346 ASSERT(bp != NULL); 12347 ASSERT(!mutex_owned(SD_MUTEX(un))); 12348 ASSERT(bp->b_resid == 0); 12349 12350 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12351 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12352 12353 /* 12354 * For a non-writable CD, a write request is an error 12355 */ 12356 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12357 (un->un_f_mmc_writable_media == FALSE)) { 12358 bioerror(bp, EIO); 12359 bp->b_resid = bp->b_bcount; 12360 SD_BEGIN_IODONE(index, un, bp); 12361 return; 12362 } 12363 12364 /* 12365 * We do not need a shadow buf if the device is using 12366 * un->un_sys_blocksize as its block size or if bcount == 0. 12367 * In this case there is no layer-private data block allocated. 12368 */ 12369 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12370 (bp->b_bcount == 0)) { 12371 goto done; 12372 } 12373 12374 #if defined(__i386) || defined(__amd64) 12375 /* We do not support non-block-aligned transfers for ROD devices */ 12376 ASSERT(!ISROD(un)); 12377 #endif 12378 12379 xp = SD_GET_XBUF(bp); 12380 ASSERT(xp != NULL); 12381 12382 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12383 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12384 un->un_tgt_blocksize, un->un_sys_blocksize); 12385 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12386 "request start block:0x%x\n", xp->xb_blkno); 12387 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12388 "request len:0x%x\n", bp->b_bcount); 12389 12390 /* 12391 * Allocate the layer-private data area for the mapblocksize layer. 12392 * Layers are allowed to use the xp_private member of the sd_xbuf 12393 * struct to store the pointer to their layer-private data block, but 12394 * each layer also has the responsibility of restoring the prior 12395 * contents of xb_private before returning the buf/xbuf to the 12396 * higher layer that sent it. 12397 * 12398 * Here we save the prior contents of xp->xb_private into the 12399 * bsp->mbs_oprivate field of our layer-private data area. This value 12400 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12401 * the layer-private area and returning the buf/xbuf to the layer 12402 * that sent it. 12403 * 12404 * Note that here we use kmem_zalloc for the allocation as there are 12405 * parts of the mapblocksize code that expect certain fields to be 12406 * zero unless explicitly set to a required value. 12407 */ 12408 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12409 bsp->mbs_oprivate = xp->xb_private; 12410 xp->xb_private = bsp; 12411 12412 /* 12413 * This treats the data on the disk (target) as an array of bytes. 12414 * first_byte is the byte offset, from the beginning of the device, 12415 * to the location of the request. This is converted from a 12416 * un->un_sys_blocksize block address to a byte offset, and then back 12417 * to a block address based upon a un->un_tgt_blocksize block size. 12418 * 12419 * xp->xb_blkno should be absolute upon entry into this function, 12420 * but, but it is based upon partitions that use the "system" 12421 * block size. It must be adjusted to reflect the block size of 12422 * the target. 12423 * 12424 * Note that end_block is actually the block that follows the last 12425 * block of the request, but that's what is needed for the computation. 12426 */ 12427 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12428 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12429 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12430 un->un_tgt_blocksize; 12431 12432 /* request_bytes is rounded up to a multiple of the target block size */ 12433 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12434 12435 /* 12436 * See if the starting address of the request and the request 12437 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12438 * then we do not need to allocate a shadow buf to handle the request. 12439 */ 12440 if (((first_byte % un->un_tgt_blocksize) == 0) && 12441 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12442 is_aligned = TRUE; 12443 } 12444 12445 if ((bp->b_flags & B_READ) == 0) { 12446 /* 12447 * Lock the range for a write operation. An aligned request is 12448 * considered a simple write; otherwise the request must be a 12449 * read-modify-write. 12450 */ 12451 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12452 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12453 } 12454 12455 /* 12456 * Alloc a shadow buf if the request is not aligned. Also, this is 12457 * where the READ command is generated for a read-modify-write. (The 12458 * write phase is deferred until after the read completes.) 12459 */ 12460 if (is_aligned == FALSE) { 12461 12462 struct sd_mapblocksize_info *shadow_bsp; 12463 struct sd_xbuf *shadow_xp; 12464 struct buf *shadow_bp; 12465 12466 /* 12467 * Allocate the shadow buf and it associated xbuf. Note that 12468 * after this call the xb_blkno value in both the original 12469 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12470 * same: absolute relative to the start of the device, and 12471 * adjusted for the target block size. The b_blkno in the 12472 * shadow buf will also be set to this value. We should never 12473 * change b_blkno in the original bp however. 12474 * 12475 * Note also that the shadow buf will always need to be a 12476 * READ command, regardless of whether the incoming command 12477 * is a READ or a WRITE. 12478 */ 12479 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12480 xp->xb_blkno, 12481 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12482 12483 shadow_xp = SD_GET_XBUF(shadow_bp); 12484 12485 /* 12486 * Allocate the layer-private data for the shadow buf. 12487 * (No need to preserve xb_private in the shadow xbuf.) 12488 */ 12489 shadow_xp->xb_private = shadow_bsp = 12490 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12491 12492 /* 12493 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12494 * to figure out where the start of the user data is (based upon 12495 * the system block size) in the data returned by the READ 12496 * command (which will be based upon the target blocksize). Note 12497 * that this is only really used if the request is unaligned. 12498 */ 12499 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12500 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12501 ASSERT((bsp->mbs_copy_offset >= 0) && 12502 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12503 12504 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12505 12506 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12507 12508 /* Transfer the wmap (if any) to the shadow buf */ 12509 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12510 bsp->mbs_wmp = NULL; 12511 12512 /* 12513 * The shadow buf goes on from here in place of the 12514 * original buf. 12515 */ 12516 shadow_bsp->mbs_orig_bp = bp; 12517 bp = shadow_bp; 12518 } 12519 12520 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12521 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12522 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12523 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12524 request_bytes); 12525 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12526 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12527 12528 done: 12529 SD_NEXT_IOSTART(index, un, bp); 12530 12531 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12532 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12533 } 12534 12535 12536 /* 12537 * Function: sd_mapblocksize_iodone 12538 * 12539 * Description: Completion side processing for block-size mapping. 12540 * 12541 * Context: May be called under interrupt context 12542 */ 12543 12544 static void 12545 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12546 { 12547 struct sd_mapblocksize_info *bsp; 12548 struct sd_xbuf *xp; 12549 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12550 struct buf *orig_bp; /* ptr to the original buf */ 12551 offset_t shadow_end; 12552 offset_t request_end; 12553 offset_t shadow_start; 12554 ssize_t copy_offset; 12555 size_t copy_length; 12556 size_t shortfall; 12557 uint_t is_write; /* TRUE if this bp is a WRITE */ 12558 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12559 12560 ASSERT(un != NULL); 12561 ASSERT(bp != NULL); 12562 12563 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12564 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12565 12566 /* 12567 * There is no shadow buf or layer-private data if the target is 12568 * using un->un_sys_blocksize as its block size or if bcount == 0. 12569 */ 12570 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12571 (bp->b_bcount == 0)) { 12572 goto exit; 12573 } 12574 12575 xp = SD_GET_XBUF(bp); 12576 ASSERT(xp != NULL); 12577 12578 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12579 bsp = xp->xb_private; 12580 12581 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12582 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12583 12584 if (is_write) { 12585 /* 12586 * For a WRITE request we must free up the block range that 12587 * we have locked up. This holds regardless of whether this is 12588 * an aligned write request or a read-modify-write request. 12589 */ 12590 sd_range_unlock(un, bsp->mbs_wmp); 12591 bsp->mbs_wmp = NULL; 12592 } 12593 12594 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12595 /* 12596 * An aligned read or write command will have no shadow buf; 12597 * there is not much else to do with it. 12598 */ 12599 goto done; 12600 } 12601 12602 orig_bp = bsp->mbs_orig_bp; 12603 ASSERT(orig_bp != NULL); 12604 orig_xp = SD_GET_XBUF(orig_bp); 12605 ASSERT(orig_xp != NULL); 12606 ASSERT(!mutex_owned(SD_MUTEX(un))); 12607 12608 if (!is_write && has_wmap) { 12609 /* 12610 * A READ with a wmap means this is the READ phase of a 12611 * read-modify-write. If an error occurred on the READ then 12612 * we do not proceed with the WRITE phase or copy any data. 12613 * Just release the write maps and return with an error. 12614 */ 12615 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12616 orig_bp->b_resid = orig_bp->b_bcount; 12617 bioerror(orig_bp, bp->b_error); 12618 sd_range_unlock(un, bsp->mbs_wmp); 12619 goto freebuf_done; 12620 } 12621 } 12622 12623 /* 12624 * Here is where we set up to copy the data from the shadow buf 12625 * into the space associated with the original buf. 12626 * 12627 * To deal with the conversion between block sizes, these 12628 * computations treat the data as an array of bytes, with the 12629 * first byte (byte 0) corresponding to the first byte in the 12630 * first block on the disk. 12631 */ 12632 12633 /* 12634 * shadow_start and shadow_len indicate the location and size of 12635 * the data returned with the shadow IO request. 12636 */ 12637 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12638 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12639 12640 /* 12641 * copy_offset gives the offset (in bytes) from the start of the first 12642 * block of the READ request to the beginning of the data. We retrieve 12643 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12644 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12645 * data to be copied (in bytes). 12646 */ 12647 copy_offset = bsp->mbs_copy_offset; 12648 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12649 copy_length = orig_bp->b_bcount; 12650 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12651 12652 /* 12653 * Set up the resid and error fields of orig_bp as appropriate. 12654 */ 12655 if (shadow_end >= request_end) { 12656 /* We got all the requested data; set resid to zero */ 12657 orig_bp->b_resid = 0; 12658 } else { 12659 /* 12660 * We failed to get enough data to fully satisfy the original 12661 * request. Just copy back whatever data we got and set 12662 * up the residual and error code as required. 12663 * 12664 * 'shortfall' is the amount by which the data received with the 12665 * shadow buf has "fallen short" of the requested amount. 12666 */ 12667 shortfall = (size_t)(request_end - shadow_end); 12668 12669 if (shortfall > orig_bp->b_bcount) { 12670 /* 12671 * We did not get enough data to even partially 12672 * fulfill the original request. The residual is 12673 * equal to the amount requested. 12674 */ 12675 orig_bp->b_resid = orig_bp->b_bcount; 12676 } else { 12677 /* 12678 * We did not get all the data that we requested 12679 * from the device, but we will try to return what 12680 * portion we did get. 12681 */ 12682 orig_bp->b_resid = shortfall; 12683 } 12684 ASSERT(copy_length >= orig_bp->b_resid); 12685 copy_length -= orig_bp->b_resid; 12686 } 12687 12688 /* Propagate the error code from the shadow buf to the original buf */ 12689 bioerror(orig_bp, bp->b_error); 12690 12691 if (is_write) { 12692 goto freebuf_done; /* No data copying for a WRITE */ 12693 } 12694 12695 if (has_wmap) { 12696 /* 12697 * This is a READ command from the READ phase of a 12698 * read-modify-write request. We have to copy the data given 12699 * by the user OVER the data returned by the READ command, 12700 * then convert the command from a READ to a WRITE and send 12701 * it back to the target. 12702 */ 12703 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12704 copy_length); 12705 12706 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12707 12708 /* 12709 * Dispatch the WRITE command to the taskq thread, which 12710 * will in turn send the command to the target. When the 12711 * WRITE command completes, we (sd_mapblocksize_iodone()) 12712 * will get called again as part of the iodone chain 12713 * processing for it. Note that we will still be dealing 12714 * with the shadow buf at that point. 12715 */ 12716 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12717 KM_NOSLEEP) != 0) { 12718 /* 12719 * Dispatch was successful so we are done. Return 12720 * without going any higher up the iodone chain. Do 12721 * not free up any layer-private data until after the 12722 * WRITE completes. 12723 */ 12724 return; 12725 } 12726 12727 /* 12728 * Dispatch of the WRITE command failed; set up the error 12729 * condition and send this IO back up the iodone chain. 12730 */ 12731 bioerror(orig_bp, EIO); 12732 orig_bp->b_resid = orig_bp->b_bcount; 12733 12734 } else { 12735 /* 12736 * This is a regular READ request (ie, not a RMW). Copy the 12737 * data from the shadow buf into the original buf. The 12738 * copy_offset compensates for any "misalignment" between the 12739 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12740 * original buf (with its un->un_sys_blocksize blocks). 12741 */ 12742 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12743 copy_length); 12744 } 12745 12746 freebuf_done: 12747 12748 /* 12749 * At this point we still have both the shadow buf AND the original 12750 * buf to deal with, as well as the layer-private data area in each. 12751 * Local variables are as follows: 12752 * 12753 * bp -- points to shadow buf 12754 * xp -- points to xbuf of shadow buf 12755 * bsp -- points to layer-private data area of shadow buf 12756 * orig_bp -- points to original buf 12757 * 12758 * First free the shadow buf and its associated xbuf, then free the 12759 * layer-private data area from the shadow buf. There is no need to 12760 * restore xb_private in the shadow xbuf. 12761 */ 12762 sd_shadow_buf_free(bp); 12763 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12764 12765 /* 12766 * Now update the local variables to point to the original buf, xbuf, 12767 * and layer-private area. 12768 */ 12769 bp = orig_bp; 12770 xp = SD_GET_XBUF(bp); 12771 ASSERT(xp != NULL); 12772 ASSERT(xp == orig_xp); 12773 bsp = xp->xb_private; 12774 ASSERT(bsp != NULL); 12775 12776 done: 12777 /* 12778 * Restore xb_private to whatever it was set to by the next higher 12779 * layer in the chain, then free the layer-private data area. 12780 */ 12781 xp->xb_private = bsp->mbs_oprivate; 12782 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12783 12784 exit: 12785 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12786 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12787 12788 SD_NEXT_IODONE(index, un, bp); 12789 } 12790 12791 12792 /* 12793 * Function: sd_checksum_iostart 12794 * 12795 * Description: A stub function for a layer that's currently not used. 12796 * For now just a placeholder. 12797 * 12798 * Context: Kernel thread context 12799 */ 12800 12801 static void 12802 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12803 { 12804 ASSERT(un != NULL); 12805 ASSERT(bp != NULL); 12806 ASSERT(!mutex_owned(SD_MUTEX(un))); 12807 SD_NEXT_IOSTART(index, un, bp); 12808 } 12809 12810 12811 /* 12812 * Function: sd_checksum_iodone 12813 * 12814 * Description: A stub function for a layer that's currently not used. 12815 * For now just a placeholder. 12816 * 12817 * Context: May be called under interrupt context 12818 */ 12819 12820 static void 12821 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12822 { 12823 ASSERT(un != NULL); 12824 ASSERT(bp != NULL); 12825 ASSERT(!mutex_owned(SD_MUTEX(un))); 12826 SD_NEXT_IODONE(index, un, bp); 12827 } 12828 12829 12830 /* 12831 * Function: sd_checksum_uscsi_iostart 12832 * 12833 * Description: A stub function for a layer that's currently not used. 12834 * For now just a placeholder. 12835 * 12836 * Context: Kernel thread context 12837 */ 12838 12839 static void 12840 sd_checksum_uscsi_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 SD_NEXT_IOSTART(index, un, bp); 12846 } 12847 12848 12849 /* 12850 * Function: sd_checksum_uscsi_iodone 12851 * 12852 * Description: A stub function for a layer that's currently not used. 12853 * For now just a placeholder. 12854 * 12855 * Context: May be called under interrupt context 12856 */ 12857 12858 static void 12859 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12860 { 12861 ASSERT(un != NULL); 12862 ASSERT(bp != NULL); 12863 ASSERT(!mutex_owned(SD_MUTEX(un))); 12864 SD_NEXT_IODONE(index, un, bp); 12865 } 12866 12867 12868 /* 12869 * Function: sd_pm_iostart 12870 * 12871 * Description: iostart-side routine for Power mangement. 12872 * 12873 * Context: Kernel thread context 12874 */ 12875 12876 static void 12877 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12878 { 12879 ASSERT(un != NULL); 12880 ASSERT(bp != NULL); 12881 ASSERT(!mutex_owned(SD_MUTEX(un))); 12882 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12883 12884 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12885 12886 if (sd_pm_entry(un) != DDI_SUCCESS) { 12887 /* 12888 * Set up to return the failed buf back up the 'iodone' 12889 * side of the calling chain. 12890 */ 12891 bioerror(bp, EIO); 12892 bp->b_resid = bp->b_bcount; 12893 12894 SD_BEGIN_IODONE(index, un, bp); 12895 12896 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12897 return; 12898 } 12899 12900 SD_NEXT_IOSTART(index, un, bp); 12901 12902 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12903 } 12904 12905 12906 /* 12907 * Function: sd_pm_iodone 12908 * 12909 * Description: iodone-side routine for power mangement. 12910 * 12911 * Context: may be called from interrupt context 12912 */ 12913 12914 static void 12915 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12916 { 12917 ASSERT(un != NULL); 12918 ASSERT(bp != NULL); 12919 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12920 12921 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12922 12923 /* 12924 * After attach the following flag is only read, so don't 12925 * take the penalty of acquiring a mutex for it. 12926 */ 12927 if (un->un_f_pm_is_enabled == TRUE) { 12928 sd_pm_exit(un); 12929 } 12930 12931 SD_NEXT_IODONE(index, un, bp); 12932 12933 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12934 } 12935 12936 12937 /* 12938 * Function: sd_core_iostart 12939 * 12940 * Description: Primary driver function for enqueuing buf(9S) structs from 12941 * the system and initiating IO to the target device 12942 * 12943 * Context: Kernel thread context. Can sleep. 12944 * 12945 * Assumptions: - The given xp->xb_blkno is absolute 12946 * (ie, relative to the start of the device). 12947 * - The IO is to be done using the native blocksize of 12948 * the device, as specified in un->un_tgt_blocksize. 12949 */ 12950 /* ARGSUSED */ 12951 static void 12952 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12953 { 12954 struct sd_xbuf *xp; 12955 12956 ASSERT(un != NULL); 12957 ASSERT(bp != NULL); 12958 ASSERT(!mutex_owned(SD_MUTEX(un))); 12959 ASSERT(bp->b_resid == 0); 12960 12961 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12962 12963 xp = SD_GET_XBUF(bp); 12964 ASSERT(xp != NULL); 12965 12966 mutex_enter(SD_MUTEX(un)); 12967 12968 /* 12969 * If we are currently in the failfast state, fail any new IO 12970 * that has B_FAILFAST set, then return. 12971 */ 12972 if ((bp->b_flags & B_FAILFAST) && 12973 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12974 mutex_exit(SD_MUTEX(un)); 12975 bioerror(bp, EIO); 12976 bp->b_resid = bp->b_bcount; 12977 SD_BEGIN_IODONE(index, un, bp); 12978 return; 12979 } 12980 12981 if (SD_IS_DIRECT_PRIORITY(xp)) { 12982 /* 12983 * Priority command -- transport it immediately. 12984 * 12985 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12986 * because all direct priority commands should be associated 12987 * with error recovery actions which we don't want to retry. 12988 */ 12989 sd_start_cmds(un, bp); 12990 } else { 12991 /* 12992 * Normal command -- add it to the wait queue, then start 12993 * transporting commands from the wait queue. 12994 */ 12995 sd_add_buf_to_waitq(un, bp); 12996 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12997 sd_start_cmds(un, NULL); 12998 } 12999 13000 mutex_exit(SD_MUTEX(un)); 13001 13002 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13003 } 13004 13005 13006 /* 13007 * Function: sd_init_cdb_limits 13008 * 13009 * Description: This is to handle scsi_pkt initialization differences 13010 * between the driver platforms. 13011 * 13012 * Legacy behaviors: 13013 * 13014 * If the block number or the sector count exceeds the 13015 * capabilities of a Group 0 command, shift over to a 13016 * Group 1 command. We don't blindly use Group 1 13017 * commands because a) some drives (CDC Wren IVs) get a 13018 * bit confused, and b) there is probably a fair amount 13019 * of speed difference for a target to receive and decode 13020 * a 10 byte command instead of a 6 byte command. 13021 * 13022 * The xfer time difference of 6 vs 10 byte CDBs is 13023 * still significant so this code is still worthwhile. 13024 * 10 byte CDBs are very inefficient with the fas HBA driver 13025 * and older disks. Each CDB byte took 1 usec with some 13026 * popular disks. 13027 * 13028 * Context: Must be called at attach time 13029 */ 13030 13031 static void 13032 sd_init_cdb_limits(struct sd_lun *un) 13033 { 13034 int hba_cdb_limit; 13035 13036 /* 13037 * Use CDB_GROUP1 commands for most devices except for 13038 * parallel SCSI fixed drives in which case we get better 13039 * performance using CDB_GROUP0 commands (where applicable). 13040 */ 13041 un->un_mincdb = SD_CDB_GROUP1; 13042 #if !defined(__fibre) 13043 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13044 !un->un_f_has_removable_media) { 13045 un->un_mincdb = SD_CDB_GROUP0; 13046 } 13047 #endif 13048 13049 /* 13050 * Try to read the max-cdb-length supported by HBA. 13051 */ 13052 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13053 if (0 >= un->un_max_hba_cdb) { 13054 un->un_max_hba_cdb = CDB_GROUP4; 13055 hba_cdb_limit = SD_CDB_GROUP4; 13056 } else if (0 < un->un_max_hba_cdb && 13057 un->un_max_hba_cdb < CDB_GROUP1) { 13058 hba_cdb_limit = SD_CDB_GROUP0; 13059 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13060 un->un_max_hba_cdb < CDB_GROUP5) { 13061 hba_cdb_limit = SD_CDB_GROUP1; 13062 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13063 un->un_max_hba_cdb < CDB_GROUP4) { 13064 hba_cdb_limit = SD_CDB_GROUP5; 13065 } else { 13066 hba_cdb_limit = SD_CDB_GROUP4; 13067 } 13068 13069 /* 13070 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13071 * commands for fixed disks unless we are building for a 32 bit 13072 * kernel. 13073 */ 13074 #ifdef _LP64 13075 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13076 min(hba_cdb_limit, SD_CDB_GROUP4); 13077 #else 13078 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13079 min(hba_cdb_limit, SD_CDB_GROUP1); 13080 #endif 13081 13082 /* 13083 * x86 systems require the PKT_DMA_PARTIAL flag 13084 */ 13085 #if defined(__x86) 13086 un->un_pkt_flags = PKT_DMA_PARTIAL; 13087 #else 13088 un->un_pkt_flags = 0; 13089 #endif 13090 13091 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13092 ? sizeof (struct scsi_arq_status) : 1); 13093 un->un_cmd_timeout = (ushort_t)sd_io_time; 13094 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13095 } 13096 13097 13098 /* 13099 * Function: sd_initpkt_for_buf 13100 * 13101 * Description: Allocate and initialize for transport a scsi_pkt struct, 13102 * based upon the info specified in the given buf struct. 13103 * 13104 * Assumes the xb_blkno in the request is absolute (ie, 13105 * relative to the start of the device (NOT partition!). 13106 * Also assumes that the request is using the native block 13107 * size of the device (as returned by the READ CAPACITY 13108 * command). 13109 * 13110 * Return Code: SD_PKT_ALLOC_SUCCESS 13111 * SD_PKT_ALLOC_FAILURE 13112 * SD_PKT_ALLOC_FAILURE_NO_DMA 13113 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13114 * 13115 * Context: Kernel thread and may be called from software interrupt context 13116 * as part of a sdrunout callback. This function may not block or 13117 * call routines that block 13118 */ 13119 13120 static int 13121 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13122 { 13123 struct sd_xbuf *xp; 13124 struct scsi_pkt *pktp = NULL; 13125 struct sd_lun *un; 13126 size_t blockcount; 13127 daddr_t startblock; 13128 int rval; 13129 int cmd_flags; 13130 13131 ASSERT(bp != NULL); 13132 ASSERT(pktpp != NULL); 13133 xp = SD_GET_XBUF(bp); 13134 ASSERT(xp != NULL); 13135 un = SD_GET_UN(bp); 13136 ASSERT(un != NULL); 13137 ASSERT(mutex_owned(SD_MUTEX(un))); 13138 ASSERT(bp->b_resid == 0); 13139 13140 SD_TRACE(SD_LOG_IO_CORE, un, 13141 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13142 13143 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13144 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13145 /* 13146 * Already have a scsi_pkt -- just need DMA resources. 13147 * We must recompute the CDB in case the mapping returns 13148 * a nonzero pkt_resid. 13149 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13150 * that is being retried, the unmap/remap of the DMA resouces 13151 * will result in the entire transfer starting over again 13152 * from the very first block. 13153 */ 13154 ASSERT(xp->xb_pktp != NULL); 13155 pktp = xp->xb_pktp; 13156 } else { 13157 pktp = NULL; 13158 } 13159 #endif /* __i386 || __amd64 */ 13160 13161 startblock = xp->xb_blkno; /* Absolute block num. */ 13162 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13163 13164 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13165 13166 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13167 13168 #else 13169 13170 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13171 13172 #endif 13173 13174 /* 13175 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13176 * call scsi_init_pkt, and build the CDB. 13177 */ 13178 rval = sd_setup_rw_pkt(un, &pktp, bp, 13179 cmd_flags, sdrunout, (caddr_t)un, 13180 startblock, blockcount); 13181 13182 if (rval == 0) { 13183 /* 13184 * Success. 13185 * 13186 * If partial DMA is being used and required for this transfer. 13187 * set it up here. 13188 */ 13189 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13190 (pktp->pkt_resid != 0)) { 13191 13192 /* 13193 * Save the CDB length and pkt_resid for the 13194 * next xfer 13195 */ 13196 xp->xb_dma_resid = pktp->pkt_resid; 13197 13198 /* rezero resid */ 13199 pktp->pkt_resid = 0; 13200 13201 } else { 13202 xp->xb_dma_resid = 0; 13203 } 13204 13205 pktp->pkt_flags = un->un_tagflags; 13206 pktp->pkt_time = un->un_cmd_timeout; 13207 pktp->pkt_comp = sdintr; 13208 13209 pktp->pkt_private = bp; 13210 *pktpp = pktp; 13211 13212 SD_TRACE(SD_LOG_IO_CORE, un, 13213 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13214 13215 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13216 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13217 #endif 13218 13219 return (SD_PKT_ALLOC_SUCCESS); 13220 13221 } 13222 13223 /* 13224 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13225 * from sd_setup_rw_pkt. 13226 */ 13227 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13228 13229 if (rval == SD_PKT_ALLOC_FAILURE) { 13230 *pktpp = NULL; 13231 /* 13232 * Set the driver state to RWAIT to indicate the driver 13233 * is waiting on resource allocations. The driver will not 13234 * suspend, pm_suspend, or detatch while the state is RWAIT. 13235 */ 13236 New_state(un, SD_STATE_RWAIT); 13237 13238 SD_ERROR(SD_LOG_IO_CORE, un, 13239 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13240 13241 if ((bp->b_flags & B_ERROR) != 0) { 13242 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13243 } 13244 return (SD_PKT_ALLOC_FAILURE); 13245 } else { 13246 /* 13247 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13248 * 13249 * This should never happen. Maybe someone messed with the 13250 * kernel's minphys? 13251 */ 13252 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13253 "Request rejected: too large for CDB: " 13254 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13255 SD_ERROR(SD_LOG_IO_CORE, un, 13256 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13257 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13258 13259 } 13260 } 13261 13262 13263 /* 13264 * Function: sd_destroypkt_for_buf 13265 * 13266 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13267 * 13268 * Context: Kernel thread or interrupt context 13269 */ 13270 13271 static void 13272 sd_destroypkt_for_buf(struct buf *bp) 13273 { 13274 ASSERT(bp != NULL); 13275 ASSERT(SD_GET_UN(bp) != NULL); 13276 13277 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13278 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13279 13280 ASSERT(SD_GET_PKTP(bp) != NULL); 13281 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13282 13283 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13284 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13285 } 13286 13287 /* 13288 * Function: sd_setup_rw_pkt 13289 * 13290 * Description: Determines appropriate CDB group for the requested LBA 13291 * and transfer length, calls scsi_init_pkt, and builds 13292 * the CDB. Do not use for partial DMA transfers except 13293 * for the initial transfer since the CDB size must 13294 * remain constant. 13295 * 13296 * Context: Kernel thread and may be called from software interrupt 13297 * context as part of a sdrunout callback. This function may not 13298 * block or call routines that block 13299 */ 13300 13301 13302 int 13303 sd_setup_rw_pkt(struct sd_lun *un, 13304 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13305 int (*callback)(caddr_t), caddr_t callback_arg, 13306 diskaddr_t lba, uint32_t blockcount) 13307 { 13308 struct scsi_pkt *return_pktp; 13309 union scsi_cdb *cdbp; 13310 struct sd_cdbinfo *cp = NULL; 13311 int i; 13312 13313 /* 13314 * See which size CDB to use, based upon the request. 13315 */ 13316 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13317 13318 /* 13319 * Check lba and block count against sd_cdbtab limits. 13320 * In the partial DMA case, we have to use the same size 13321 * CDB for all the transfers. Check lba + blockcount 13322 * against the max LBA so we know that segment of the 13323 * transfer can use the CDB we select. 13324 */ 13325 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13326 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13327 13328 /* 13329 * The command will fit into the CDB type 13330 * specified by sd_cdbtab[i]. 13331 */ 13332 cp = sd_cdbtab + i; 13333 13334 /* 13335 * Call scsi_init_pkt so we can fill in the 13336 * CDB. 13337 */ 13338 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13339 bp, cp->sc_grpcode, un->un_status_len, 0, 13340 flags, callback, callback_arg); 13341 13342 if (return_pktp != NULL) { 13343 13344 /* 13345 * Return new value of pkt 13346 */ 13347 *pktpp = return_pktp; 13348 13349 /* 13350 * To be safe, zero the CDB insuring there is 13351 * no leftover data from a previous command. 13352 */ 13353 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13354 13355 /* 13356 * Handle partial DMA mapping 13357 */ 13358 if (return_pktp->pkt_resid != 0) { 13359 13360 /* 13361 * Not going to xfer as many blocks as 13362 * originally expected 13363 */ 13364 blockcount -= 13365 SD_BYTES2TGTBLOCKS(un, 13366 return_pktp->pkt_resid); 13367 } 13368 13369 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13370 13371 /* 13372 * Set command byte based on the CDB 13373 * type we matched. 13374 */ 13375 cdbp->scc_cmd = cp->sc_grpmask | 13376 ((bp->b_flags & B_READ) ? 13377 SCMD_READ : SCMD_WRITE); 13378 13379 SD_FILL_SCSI1_LUN(un, return_pktp); 13380 13381 /* 13382 * Fill in LBA and length 13383 */ 13384 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13385 (cp->sc_grpcode == CDB_GROUP4) || 13386 (cp->sc_grpcode == CDB_GROUP0) || 13387 (cp->sc_grpcode == CDB_GROUP5)); 13388 13389 if (cp->sc_grpcode == CDB_GROUP1) { 13390 FORMG1ADDR(cdbp, lba); 13391 FORMG1COUNT(cdbp, blockcount); 13392 return (0); 13393 } else if (cp->sc_grpcode == CDB_GROUP4) { 13394 FORMG4LONGADDR(cdbp, lba); 13395 FORMG4COUNT(cdbp, blockcount); 13396 return (0); 13397 } else if (cp->sc_grpcode == CDB_GROUP0) { 13398 FORMG0ADDR(cdbp, lba); 13399 FORMG0COUNT(cdbp, blockcount); 13400 return (0); 13401 } else if (cp->sc_grpcode == CDB_GROUP5) { 13402 FORMG5ADDR(cdbp, lba); 13403 FORMG5COUNT(cdbp, blockcount); 13404 return (0); 13405 } 13406 13407 /* 13408 * It should be impossible to not match one 13409 * of the CDB types above, so we should never 13410 * reach this point. Set the CDB command byte 13411 * to test-unit-ready to avoid writing 13412 * to somewhere we don't intend. 13413 */ 13414 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13415 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13416 } else { 13417 /* 13418 * Couldn't get scsi_pkt 13419 */ 13420 return (SD_PKT_ALLOC_FAILURE); 13421 } 13422 } 13423 } 13424 13425 /* 13426 * None of the available CDB types were suitable. This really 13427 * should never happen: on a 64 bit system we support 13428 * READ16/WRITE16 which will hold an entire 64 bit disk address 13429 * and on a 32 bit system we will refuse to bind to a device 13430 * larger than 2TB so addresses will never be larger than 32 bits. 13431 */ 13432 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13433 } 13434 13435 #if defined(__i386) || defined(__amd64) 13436 /* 13437 * Function: sd_setup_next_rw_pkt 13438 * 13439 * Description: Setup packet for partial DMA transfers, except for the 13440 * initial transfer. sd_setup_rw_pkt should be used for 13441 * the initial transfer. 13442 * 13443 * Context: Kernel thread and may be called from interrupt context. 13444 */ 13445 13446 int 13447 sd_setup_next_rw_pkt(struct sd_lun *un, 13448 struct scsi_pkt *pktp, struct buf *bp, 13449 diskaddr_t lba, uint32_t blockcount) 13450 { 13451 uchar_t com; 13452 union scsi_cdb *cdbp; 13453 uchar_t cdb_group_id; 13454 13455 ASSERT(pktp != NULL); 13456 ASSERT(pktp->pkt_cdbp != NULL); 13457 13458 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13459 com = cdbp->scc_cmd; 13460 cdb_group_id = CDB_GROUPID(com); 13461 13462 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13463 (cdb_group_id == CDB_GROUPID_1) || 13464 (cdb_group_id == CDB_GROUPID_4) || 13465 (cdb_group_id == CDB_GROUPID_5)); 13466 13467 /* 13468 * Move pkt to the next portion of the xfer. 13469 * func is NULL_FUNC so we do not have to release 13470 * the disk mutex here. 13471 */ 13472 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13473 NULL_FUNC, NULL) == pktp) { 13474 /* Success. Handle partial DMA */ 13475 if (pktp->pkt_resid != 0) { 13476 blockcount -= 13477 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13478 } 13479 13480 cdbp->scc_cmd = com; 13481 SD_FILL_SCSI1_LUN(un, pktp); 13482 if (cdb_group_id == CDB_GROUPID_1) { 13483 FORMG1ADDR(cdbp, lba); 13484 FORMG1COUNT(cdbp, blockcount); 13485 return (0); 13486 } else if (cdb_group_id == CDB_GROUPID_4) { 13487 FORMG4LONGADDR(cdbp, lba); 13488 FORMG4COUNT(cdbp, blockcount); 13489 return (0); 13490 } else if (cdb_group_id == CDB_GROUPID_0) { 13491 FORMG0ADDR(cdbp, lba); 13492 FORMG0COUNT(cdbp, blockcount); 13493 return (0); 13494 } else if (cdb_group_id == CDB_GROUPID_5) { 13495 FORMG5ADDR(cdbp, lba); 13496 FORMG5COUNT(cdbp, blockcount); 13497 return (0); 13498 } 13499 13500 /* Unreachable */ 13501 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13502 } 13503 13504 /* 13505 * Error setting up next portion of cmd transfer. 13506 * Something is definitely very wrong and this 13507 * should not happen. 13508 */ 13509 return (SD_PKT_ALLOC_FAILURE); 13510 } 13511 #endif /* defined(__i386) || defined(__amd64) */ 13512 13513 /* 13514 * Function: sd_initpkt_for_uscsi 13515 * 13516 * Description: Allocate and initialize for transport a scsi_pkt struct, 13517 * based upon the info specified in the given uscsi_cmd struct. 13518 * 13519 * Return Code: SD_PKT_ALLOC_SUCCESS 13520 * SD_PKT_ALLOC_FAILURE 13521 * SD_PKT_ALLOC_FAILURE_NO_DMA 13522 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13523 * 13524 * Context: Kernel thread and may be called from software interrupt context 13525 * as part of a sdrunout callback. This function may not block or 13526 * call routines that block 13527 */ 13528 13529 static int 13530 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13531 { 13532 struct uscsi_cmd *uscmd; 13533 struct sd_xbuf *xp; 13534 struct scsi_pkt *pktp; 13535 struct sd_lun *un; 13536 uint32_t flags = 0; 13537 13538 ASSERT(bp != NULL); 13539 ASSERT(pktpp != NULL); 13540 xp = SD_GET_XBUF(bp); 13541 ASSERT(xp != NULL); 13542 un = SD_GET_UN(bp); 13543 ASSERT(un != NULL); 13544 ASSERT(mutex_owned(SD_MUTEX(un))); 13545 13546 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13547 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13548 ASSERT(uscmd != NULL); 13549 13550 SD_TRACE(SD_LOG_IO_CORE, un, 13551 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13552 13553 /* 13554 * Allocate the scsi_pkt for the command. 13555 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13556 * during scsi_init_pkt time and will continue to use the 13557 * same path as long as the same scsi_pkt is used without 13558 * intervening scsi_dma_free(). Since uscsi command does 13559 * not call scsi_dmafree() before retry failed command, it 13560 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13561 * set such that scsi_vhci can use other available path for 13562 * retry. Besides, ucsci command does not allow DMA breakup, 13563 * so there is no need to set PKT_DMA_PARTIAL flag. 13564 */ 13565 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13566 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13567 sizeof (struct scsi_arq_status), 0, 13568 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13569 sdrunout, (caddr_t)un); 13570 13571 if (pktp == NULL) { 13572 *pktpp = NULL; 13573 /* 13574 * Set the driver state to RWAIT to indicate the driver 13575 * is waiting on resource allocations. The driver will not 13576 * suspend, pm_suspend, or detatch while the state is RWAIT. 13577 */ 13578 New_state(un, SD_STATE_RWAIT); 13579 13580 SD_ERROR(SD_LOG_IO_CORE, un, 13581 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13582 13583 if ((bp->b_flags & B_ERROR) != 0) { 13584 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13585 } 13586 return (SD_PKT_ALLOC_FAILURE); 13587 } 13588 13589 /* 13590 * We do not do DMA breakup for USCSI commands, so return failure 13591 * here if all the needed DMA resources were not allocated. 13592 */ 13593 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13594 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13595 scsi_destroy_pkt(pktp); 13596 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13597 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13598 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13599 } 13600 13601 /* Init the cdb from the given uscsi struct */ 13602 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13603 uscmd->uscsi_cdb[0], 0, 0, 0); 13604 13605 SD_FILL_SCSI1_LUN(un, pktp); 13606 13607 /* 13608 * Set up the optional USCSI flags. See the uscsi (7I) man page 13609 * for listing of the supported flags. 13610 */ 13611 13612 if (uscmd->uscsi_flags & USCSI_SILENT) { 13613 flags |= FLAG_SILENT; 13614 } 13615 13616 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13617 flags |= FLAG_DIAGNOSE; 13618 } 13619 13620 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13621 flags |= FLAG_ISOLATE; 13622 } 13623 13624 if (un->un_f_is_fibre == FALSE) { 13625 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13626 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13627 } 13628 } 13629 13630 /* 13631 * Set the pkt flags here so we save time later. 13632 * Note: These flags are NOT in the uscsi man page!!! 13633 */ 13634 if (uscmd->uscsi_flags & USCSI_HEAD) { 13635 flags |= FLAG_HEAD; 13636 } 13637 13638 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13639 flags |= FLAG_NOINTR; 13640 } 13641 13642 /* 13643 * For tagged queueing, things get a bit complicated. 13644 * Check first for head of queue and last for ordered queue. 13645 * If neither head nor order, use the default driver tag flags. 13646 */ 13647 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13648 if (uscmd->uscsi_flags & USCSI_HTAG) { 13649 flags |= FLAG_HTAG; 13650 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13651 flags |= FLAG_OTAG; 13652 } else { 13653 flags |= un->un_tagflags & FLAG_TAGMASK; 13654 } 13655 } 13656 13657 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13658 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13659 } 13660 13661 pktp->pkt_flags = flags; 13662 13663 /* Copy the caller's CDB into the pkt... */ 13664 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13665 13666 if (uscmd->uscsi_timeout == 0) { 13667 pktp->pkt_time = un->un_uscsi_timeout; 13668 } else { 13669 pktp->pkt_time = uscmd->uscsi_timeout; 13670 } 13671 13672 /* need it later to identify USCSI request in sdintr */ 13673 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13674 13675 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13676 13677 pktp->pkt_private = bp; 13678 pktp->pkt_comp = sdintr; 13679 *pktpp = pktp; 13680 13681 SD_TRACE(SD_LOG_IO_CORE, un, 13682 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13683 13684 return (SD_PKT_ALLOC_SUCCESS); 13685 } 13686 13687 13688 /* 13689 * Function: sd_destroypkt_for_uscsi 13690 * 13691 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13692 * IOs.. Also saves relevant info into the associated uscsi_cmd 13693 * struct. 13694 * 13695 * Context: May be called under interrupt context 13696 */ 13697 13698 static void 13699 sd_destroypkt_for_uscsi(struct buf *bp) 13700 { 13701 struct uscsi_cmd *uscmd; 13702 struct sd_xbuf *xp; 13703 struct scsi_pkt *pktp; 13704 struct sd_lun *un; 13705 13706 ASSERT(bp != NULL); 13707 xp = SD_GET_XBUF(bp); 13708 ASSERT(xp != NULL); 13709 un = SD_GET_UN(bp); 13710 ASSERT(un != NULL); 13711 ASSERT(!mutex_owned(SD_MUTEX(un))); 13712 pktp = SD_GET_PKTP(bp); 13713 ASSERT(pktp != NULL); 13714 13715 SD_TRACE(SD_LOG_IO_CORE, un, 13716 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13717 13718 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13719 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13720 ASSERT(uscmd != NULL); 13721 13722 /* Save the status and the residual into the uscsi_cmd struct */ 13723 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13724 uscmd->uscsi_resid = bp->b_resid; 13725 13726 /* 13727 * If enabled, copy any saved sense data into the area specified 13728 * by the uscsi command. 13729 */ 13730 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13731 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13732 /* 13733 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13734 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13735 */ 13736 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13737 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13738 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13739 } 13740 13741 /* We are done with the scsi_pkt; free it now */ 13742 ASSERT(SD_GET_PKTP(bp) != NULL); 13743 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13744 13745 SD_TRACE(SD_LOG_IO_CORE, un, 13746 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13747 } 13748 13749 13750 /* 13751 * Function: sd_bioclone_alloc 13752 * 13753 * Description: Allocate a buf(9S) and init it as per the given buf 13754 * and the various arguments. The associated sd_xbuf 13755 * struct is (nearly) duplicated. The struct buf *bp 13756 * argument is saved in new_xp->xb_private. 13757 * 13758 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13759 * datalen - size of data area for the shadow bp 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_bioclone_alloc(struct buf *bp, size_t datalen, 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 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13786 NULL, KM_SLEEP); 13787 13788 new_bp->b_lblkno = blkno; 13789 13790 /* 13791 * Allocate an xbuf for the shadow bp and copy the contents of the 13792 * original xbuf into it. 13793 */ 13794 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13795 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13796 13797 /* 13798 * The given bp is automatically saved in the xb_private member 13799 * of the new xbuf. Callers are allowed to depend on this. 13800 */ 13801 new_xp->xb_private = bp; 13802 13803 new_bp->b_private = new_xp; 13804 13805 return (new_bp); 13806 } 13807 13808 /* 13809 * Function: sd_shadow_buf_alloc 13810 * 13811 * Description: Allocate a buf(9S) and init it as per the given buf 13812 * and the various arguments. The associated sd_xbuf 13813 * struct is (nearly) duplicated. The struct buf *bp 13814 * argument is saved in new_xp->xb_private. 13815 * 13816 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13817 * datalen - size of data area for the shadow bp 13818 * bflags - B_READ or B_WRITE (pseudo flag) 13819 * blkno - starting LBA 13820 * func - function pointer for b_iodone in the shadow buf. (May 13821 * be NULL if none.) 13822 * 13823 * Return Code: Pointer to allocates buf(9S) struct 13824 * 13825 * Context: Can sleep. 13826 */ 13827 13828 static struct buf * 13829 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13830 daddr_t blkno, int (*func)(struct buf *)) 13831 { 13832 struct sd_lun *un; 13833 struct sd_xbuf *xp; 13834 struct sd_xbuf *new_xp; 13835 struct buf *new_bp; 13836 13837 ASSERT(bp != NULL); 13838 xp = SD_GET_XBUF(bp); 13839 ASSERT(xp != NULL); 13840 un = SD_GET_UN(bp); 13841 ASSERT(un != NULL); 13842 ASSERT(!mutex_owned(SD_MUTEX(un))); 13843 13844 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13845 bp_mapin(bp); 13846 } 13847 13848 bflags &= (B_READ | B_WRITE); 13849 #if defined(__i386) || defined(__amd64) 13850 new_bp = getrbuf(KM_SLEEP); 13851 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13852 new_bp->b_bcount = datalen; 13853 new_bp->b_flags = bflags | 13854 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 13855 #else 13856 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13857 datalen, bflags, SLEEP_FUNC, NULL); 13858 #endif 13859 new_bp->av_forw = NULL; 13860 new_bp->av_back = NULL; 13861 new_bp->b_dev = bp->b_dev; 13862 new_bp->b_blkno = blkno; 13863 new_bp->b_iodone = func; 13864 new_bp->b_edev = bp->b_edev; 13865 new_bp->b_resid = 0; 13866 13867 /* We need to preserve the B_FAILFAST flag */ 13868 if (bp->b_flags & B_FAILFAST) { 13869 new_bp->b_flags |= B_FAILFAST; 13870 } 13871 13872 /* 13873 * Allocate an xbuf for the shadow bp and copy the contents of the 13874 * original xbuf into it. 13875 */ 13876 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13877 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13878 13879 /* Need later to copy data between the shadow buf & original buf! */ 13880 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13881 13882 /* 13883 * The given bp is automatically saved in the xb_private member 13884 * of the new xbuf. Callers are allowed to depend on this. 13885 */ 13886 new_xp->xb_private = bp; 13887 13888 new_bp->b_private = new_xp; 13889 13890 return (new_bp); 13891 } 13892 13893 /* 13894 * Function: sd_bioclone_free 13895 * 13896 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13897 * in the larger than partition operation. 13898 * 13899 * Context: May be called under interrupt context 13900 */ 13901 13902 static void 13903 sd_bioclone_free(struct buf *bp) 13904 { 13905 struct sd_xbuf *xp; 13906 13907 ASSERT(bp != NULL); 13908 xp = SD_GET_XBUF(bp); 13909 ASSERT(xp != NULL); 13910 13911 /* 13912 * Call bp_mapout() before freeing the buf, in case a lower 13913 * layer or HBA had done a bp_mapin(). we must do this here 13914 * as we are the "originator" of the shadow buf. 13915 */ 13916 bp_mapout(bp); 13917 13918 /* 13919 * Null out b_iodone before freeing the bp, to ensure that the driver 13920 * never gets confused by a stale value in this field. (Just a little 13921 * extra defensiveness here.) 13922 */ 13923 bp->b_iodone = NULL; 13924 13925 freerbuf(bp); 13926 13927 kmem_free(xp, sizeof (struct sd_xbuf)); 13928 } 13929 13930 /* 13931 * Function: sd_shadow_buf_free 13932 * 13933 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13934 * 13935 * Context: May be called under interrupt context 13936 */ 13937 13938 static void 13939 sd_shadow_buf_free(struct buf *bp) 13940 { 13941 struct sd_xbuf *xp; 13942 13943 ASSERT(bp != NULL); 13944 xp = SD_GET_XBUF(bp); 13945 ASSERT(xp != NULL); 13946 13947 #if defined(__sparc) 13948 /* 13949 * Call bp_mapout() before freeing the buf, in case a lower 13950 * layer or HBA had done a bp_mapin(). we must do this here 13951 * as we are the "originator" of the shadow buf. 13952 */ 13953 bp_mapout(bp); 13954 #endif 13955 13956 /* 13957 * Null out b_iodone before freeing the bp, to ensure that the driver 13958 * never gets confused by a stale value in this field. (Just a little 13959 * extra defensiveness here.) 13960 */ 13961 bp->b_iodone = NULL; 13962 13963 #if defined(__i386) || defined(__amd64) 13964 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13965 freerbuf(bp); 13966 #else 13967 scsi_free_consistent_buf(bp); 13968 #endif 13969 13970 kmem_free(xp, sizeof (struct sd_xbuf)); 13971 } 13972 13973 13974 /* 13975 * Function: sd_print_transport_rejected_message 13976 * 13977 * Description: This implements the ludicrously complex rules for printing 13978 * a "transport rejected" message. This is to address the 13979 * specific problem of having a flood of this error message 13980 * produced when a failover occurs. 13981 * 13982 * Context: Any. 13983 */ 13984 13985 static void 13986 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13987 int code) 13988 { 13989 ASSERT(un != NULL); 13990 ASSERT(mutex_owned(SD_MUTEX(un))); 13991 ASSERT(xp != NULL); 13992 13993 /* 13994 * Print the "transport rejected" message under the following 13995 * conditions: 13996 * 13997 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13998 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13999 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14000 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14001 * scsi_transport(9F) (which indicates that the target might have 14002 * gone off-line). This uses the un->un_tran_fatal_count 14003 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14004 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14005 * from scsi_transport(). 14006 * 14007 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14008 * the preceeding cases in order for the message to be printed. 14009 */ 14010 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14011 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14012 (code != TRAN_FATAL_ERROR) || 14013 (un->un_tran_fatal_count == 1)) { 14014 switch (code) { 14015 case TRAN_BADPKT: 14016 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14017 "transport rejected bad packet\n"); 14018 break; 14019 case TRAN_FATAL_ERROR: 14020 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14021 "transport rejected fatal error\n"); 14022 break; 14023 default: 14024 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14025 "transport rejected (%d)\n", code); 14026 break; 14027 } 14028 } 14029 } 14030 } 14031 14032 14033 /* 14034 * Function: sd_add_buf_to_waitq 14035 * 14036 * Description: Add the given buf(9S) struct to the wait queue for the 14037 * instance. If sorting is enabled, then the buf is added 14038 * to the queue via an elevator sort algorithm (a la 14039 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14040 * If sorting is not enabled, then the buf is just added 14041 * to the end of the wait queue. 14042 * 14043 * Return Code: void 14044 * 14045 * Context: Does not sleep/block, therefore technically can be called 14046 * from any context. However if sorting is enabled then the 14047 * execution time is indeterminate, and may take long if 14048 * the wait queue grows large. 14049 */ 14050 14051 static void 14052 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14053 { 14054 struct buf *ap; 14055 14056 ASSERT(bp != NULL); 14057 ASSERT(un != NULL); 14058 ASSERT(mutex_owned(SD_MUTEX(un))); 14059 14060 /* If the queue is empty, add the buf as the only entry & return. */ 14061 if (un->un_waitq_headp == NULL) { 14062 ASSERT(un->un_waitq_tailp == NULL); 14063 un->un_waitq_headp = un->un_waitq_tailp = bp; 14064 bp->av_forw = NULL; 14065 return; 14066 } 14067 14068 ASSERT(un->un_waitq_tailp != NULL); 14069 14070 /* 14071 * If sorting is disabled, just add the buf to the tail end of 14072 * the wait queue and return. 14073 */ 14074 if (un->un_f_disksort_disabled) { 14075 un->un_waitq_tailp->av_forw = bp; 14076 un->un_waitq_tailp = bp; 14077 bp->av_forw = NULL; 14078 return; 14079 } 14080 14081 /* 14082 * Sort thru the list of requests currently on the wait queue 14083 * and add the new buf request at the appropriate position. 14084 * 14085 * The un->un_waitq_headp is an activity chain pointer on which 14086 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14087 * first queue holds those requests which are positioned after 14088 * the current SD_GET_BLKNO() (in the first request); the second holds 14089 * requests which came in after their SD_GET_BLKNO() number was passed. 14090 * Thus we implement a one way scan, retracting after reaching 14091 * the end of the drive to the first request on the second 14092 * queue, at which time it becomes the first queue. 14093 * A one-way scan is natural because of the way UNIX read-ahead 14094 * blocks are allocated. 14095 * 14096 * If we lie after the first request, then we must locate the 14097 * second request list and add ourselves to it. 14098 */ 14099 ap = un->un_waitq_headp; 14100 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14101 while (ap->av_forw != NULL) { 14102 /* 14103 * Look for an "inversion" in the (normally 14104 * ascending) block numbers. This indicates 14105 * the start of the second request list. 14106 */ 14107 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14108 /* 14109 * Search the second request list for the 14110 * first request at a larger block number. 14111 * We go before that; however if there is 14112 * no such request, we go at the end. 14113 */ 14114 do { 14115 if (SD_GET_BLKNO(bp) < 14116 SD_GET_BLKNO(ap->av_forw)) { 14117 goto insert; 14118 } 14119 ap = ap->av_forw; 14120 } while (ap->av_forw != NULL); 14121 goto insert; /* after last */ 14122 } 14123 ap = ap->av_forw; 14124 } 14125 14126 /* 14127 * No inversions... we will go after the last, and 14128 * be the first request in the second request list. 14129 */ 14130 goto insert; 14131 } 14132 14133 /* 14134 * Request is at/after the current request... 14135 * sort in the first request list. 14136 */ 14137 while (ap->av_forw != NULL) { 14138 /* 14139 * We want to go after the current request (1) if 14140 * there is an inversion after it (i.e. it is the end 14141 * of the first request list), or (2) if the next 14142 * request is a larger block no. than our request. 14143 */ 14144 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14145 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14146 goto insert; 14147 } 14148 ap = ap->av_forw; 14149 } 14150 14151 /* 14152 * Neither a second list nor a larger request, therefore 14153 * we go at the end of the first list (which is the same 14154 * as the end of the whole schebang). 14155 */ 14156 insert: 14157 bp->av_forw = ap->av_forw; 14158 ap->av_forw = bp; 14159 14160 /* 14161 * If we inserted onto the tail end of the waitq, make sure the 14162 * tail pointer is updated. 14163 */ 14164 if (ap == un->un_waitq_tailp) { 14165 un->un_waitq_tailp = bp; 14166 } 14167 } 14168 14169 14170 /* 14171 * Function: sd_start_cmds 14172 * 14173 * Description: Remove and transport cmds from the driver queues. 14174 * 14175 * Arguments: un - pointer to the unit (soft state) struct for the target. 14176 * 14177 * immed_bp - ptr to a buf to be transported immediately. Only 14178 * the immed_bp is transported; bufs on the waitq are not 14179 * processed and the un_retry_bp is not checked. If immed_bp is 14180 * NULL, then normal queue processing is performed. 14181 * 14182 * Context: May be called from kernel thread context, interrupt context, 14183 * or runout callback context. This function may not block or 14184 * call routines that block. 14185 */ 14186 14187 static void 14188 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14189 { 14190 struct sd_xbuf *xp; 14191 struct buf *bp; 14192 void (*statp)(kstat_io_t *); 14193 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14194 void (*saved_statp)(kstat_io_t *); 14195 #endif 14196 int rval; 14197 14198 ASSERT(un != NULL); 14199 ASSERT(mutex_owned(SD_MUTEX(un))); 14200 ASSERT(un->un_ncmds_in_transport >= 0); 14201 ASSERT(un->un_throttle >= 0); 14202 14203 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14204 14205 do { 14206 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14207 saved_statp = NULL; 14208 #endif 14209 14210 /* 14211 * If we are syncing or dumping, fail the command to 14212 * avoid recursively calling back into scsi_transport(). 14213 * The dump I/O itself uses a separate code path so this 14214 * only prevents non-dump I/O from being sent while dumping. 14215 * File system sync takes place before dumping begins. 14216 * During panic, filesystem I/O is allowed provided 14217 * un_in_callback is <= 1. This is to prevent recursion 14218 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14219 * sd_start_cmds and so on. See panic.c for more information 14220 * about the states the system can be in during panic. 14221 */ 14222 if ((un->un_state == SD_STATE_DUMPING) || 14223 (ddi_in_panic() && (un->un_in_callback > 1))) { 14224 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14225 "sd_start_cmds: panicking\n"); 14226 goto exit; 14227 } 14228 14229 if ((bp = immed_bp) != NULL) { 14230 /* 14231 * We have a bp that must be transported immediately. 14232 * It's OK to transport the immed_bp here without doing 14233 * the throttle limit check because the immed_bp is 14234 * always used in a retry/recovery case. This means 14235 * that we know we are not at the throttle limit by 14236 * virtue of the fact that to get here we must have 14237 * already gotten a command back via sdintr(). This also 14238 * relies on (1) the command on un_retry_bp preventing 14239 * further commands from the waitq from being issued; 14240 * and (2) the code in sd_retry_command checking the 14241 * throttle limit before issuing a delayed or immediate 14242 * retry. This holds even if the throttle limit is 14243 * currently ratcheted down from its maximum value. 14244 */ 14245 statp = kstat_runq_enter; 14246 if (bp == un->un_retry_bp) { 14247 ASSERT((un->un_retry_statp == NULL) || 14248 (un->un_retry_statp == kstat_waitq_enter) || 14249 (un->un_retry_statp == 14250 kstat_runq_back_to_waitq)); 14251 /* 14252 * If the waitq kstat was incremented when 14253 * sd_set_retry_bp() queued this bp for a retry, 14254 * then we must set up statp so that the waitq 14255 * count will get decremented correctly below. 14256 * Also we must clear un->un_retry_statp to 14257 * ensure that we do not act on a stale value 14258 * in this field. 14259 */ 14260 if ((un->un_retry_statp == kstat_waitq_enter) || 14261 (un->un_retry_statp == 14262 kstat_runq_back_to_waitq)) { 14263 statp = kstat_waitq_to_runq; 14264 } 14265 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14266 saved_statp = un->un_retry_statp; 14267 #endif 14268 un->un_retry_statp = NULL; 14269 14270 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14271 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14272 "un_throttle:%d un_ncmds_in_transport:%d\n", 14273 un, un->un_retry_bp, un->un_throttle, 14274 un->un_ncmds_in_transport); 14275 } else { 14276 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14277 "processing priority bp:0x%p\n", bp); 14278 } 14279 14280 } else if ((bp = un->un_waitq_headp) != NULL) { 14281 /* 14282 * A command on the waitq is ready to go, but do not 14283 * send it if: 14284 * 14285 * (1) the throttle limit has been reached, or 14286 * (2) a retry is pending, or 14287 * (3) a START_STOP_UNIT callback pending, or 14288 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14289 * command is pending. 14290 * 14291 * For all of these conditions, IO processing will 14292 * restart after the condition is cleared. 14293 */ 14294 if (un->un_ncmds_in_transport >= un->un_throttle) { 14295 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14296 "sd_start_cmds: exiting, " 14297 "throttle limit reached!\n"); 14298 goto exit; 14299 } 14300 if (un->un_retry_bp != NULL) { 14301 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14302 "sd_start_cmds: exiting, retry pending!\n"); 14303 goto exit; 14304 } 14305 if (un->un_startstop_timeid != NULL) { 14306 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14307 "sd_start_cmds: exiting, " 14308 "START_STOP pending!\n"); 14309 goto exit; 14310 } 14311 if (un->un_direct_priority_timeid != NULL) { 14312 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14313 "sd_start_cmds: exiting, " 14314 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14315 goto exit; 14316 } 14317 14318 /* Dequeue the command */ 14319 un->un_waitq_headp = bp->av_forw; 14320 if (un->un_waitq_headp == NULL) { 14321 un->un_waitq_tailp = NULL; 14322 } 14323 bp->av_forw = NULL; 14324 statp = kstat_waitq_to_runq; 14325 SD_TRACE(SD_LOG_IO_CORE, un, 14326 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14327 14328 } else { 14329 /* No work to do so bail out now */ 14330 SD_TRACE(SD_LOG_IO_CORE, un, 14331 "sd_start_cmds: no more work, exiting!\n"); 14332 goto exit; 14333 } 14334 14335 /* 14336 * Reset the state to normal. This is the mechanism by which 14337 * the state transitions from either SD_STATE_RWAIT or 14338 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14339 * If state is SD_STATE_PM_CHANGING then this command is 14340 * part of the device power control and the state must 14341 * not be put back to normal. Doing so would would 14342 * allow new commands to proceed when they shouldn't, 14343 * the device may be going off. 14344 */ 14345 if ((un->un_state != SD_STATE_SUSPENDED) && 14346 (un->un_state != SD_STATE_PM_CHANGING)) { 14347 New_state(un, SD_STATE_NORMAL); 14348 } 14349 14350 xp = SD_GET_XBUF(bp); 14351 ASSERT(xp != NULL); 14352 14353 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14354 /* 14355 * Allocate the scsi_pkt if we need one, or attach DMA 14356 * resources if we have a scsi_pkt that needs them. The 14357 * latter should only occur for commands that are being 14358 * retried. 14359 */ 14360 if ((xp->xb_pktp == NULL) || 14361 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14362 #else 14363 if (xp->xb_pktp == NULL) { 14364 #endif 14365 /* 14366 * There is no scsi_pkt allocated for this buf. Call 14367 * the initpkt function to allocate & init one. 14368 * 14369 * The scsi_init_pkt runout callback functionality is 14370 * implemented as follows: 14371 * 14372 * 1) The initpkt function always calls 14373 * scsi_init_pkt(9F) with sdrunout specified as the 14374 * callback routine. 14375 * 2) A successful packet allocation is initialized and 14376 * the I/O is transported. 14377 * 3) The I/O associated with an allocation resource 14378 * failure is left on its queue to be retried via 14379 * runout or the next I/O. 14380 * 4) The I/O associated with a DMA error is removed 14381 * from the queue and failed with EIO. Processing of 14382 * the transport queues is also halted to be 14383 * restarted via runout or the next I/O. 14384 * 5) The I/O associated with a CDB size or packet 14385 * size error is removed from the queue and failed 14386 * with EIO. Processing of the transport queues is 14387 * continued. 14388 * 14389 * Note: there is no interface for canceling a runout 14390 * callback. To prevent the driver from detaching or 14391 * suspending while a runout is pending the driver 14392 * state is set to SD_STATE_RWAIT 14393 * 14394 * Note: using the scsi_init_pkt callback facility can 14395 * result in an I/O request persisting at the head of 14396 * the list which cannot be satisfied even after 14397 * multiple retries. In the future the driver may 14398 * implement some kind of maximum runout count before 14399 * failing an I/O. 14400 * 14401 * Note: the use of funcp below may seem superfluous, 14402 * but it helps warlock figure out the correct 14403 * initpkt function calls (see [s]sd.wlcmd). 14404 */ 14405 struct scsi_pkt *pktp; 14406 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14407 14408 ASSERT(bp != un->un_rqs_bp); 14409 14410 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14411 switch ((*funcp)(bp, &pktp)) { 14412 case SD_PKT_ALLOC_SUCCESS: 14413 xp->xb_pktp = pktp; 14414 SD_TRACE(SD_LOG_IO_CORE, un, 14415 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14416 pktp); 14417 goto got_pkt; 14418 14419 case SD_PKT_ALLOC_FAILURE: 14420 /* 14421 * Temporary (hopefully) resource depletion. 14422 * Since retries and RQS commands always have a 14423 * scsi_pkt allocated, these cases should never 14424 * get here. So the only cases this needs to 14425 * handle is a bp from the waitq (which we put 14426 * back onto the waitq for sdrunout), or a bp 14427 * sent as an immed_bp (which we just fail). 14428 */ 14429 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14430 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14431 14432 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14433 14434 if (bp == immed_bp) { 14435 /* 14436 * If SD_XB_DMA_FREED is clear, then 14437 * this is a failure to allocate a 14438 * scsi_pkt, and we must fail the 14439 * command. 14440 */ 14441 if ((xp->xb_pkt_flags & 14442 SD_XB_DMA_FREED) == 0) { 14443 break; 14444 } 14445 14446 /* 14447 * If this immediate command is NOT our 14448 * un_retry_bp, then we must fail it. 14449 */ 14450 if (bp != un->un_retry_bp) { 14451 break; 14452 } 14453 14454 /* 14455 * We get here if this cmd is our 14456 * un_retry_bp that was DMAFREED, but 14457 * scsi_init_pkt() failed to reallocate 14458 * DMA resources when we attempted to 14459 * retry it. This can happen when an 14460 * mpxio failover is in progress, but 14461 * we don't want to just fail the 14462 * command in this case. 14463 * 14464 * Use timeout(9F) to restart it after 14465 * a 100ms delay. We don't want to 14466 * let sdrunout() restart it, because 14467 * sdrunout() is just supposed to start 14468 * commands that are sitting on the 14469 * wait queue. The un_retry_bp stays 14470 * set until the command completes, but 14471 * sdrunout can be called many times 14472 * before that happens. Since sdrunout 14473 * cannot tell if the un_retry_bp is 14474 * already in the transport, it could 14475 * end up calling scsi_transport() for 14476 * the un_retry_bp multiple times. 14477 * 14478 * Also: don't schedule the callback 14479 * if some other callback is already 14480 * pending. 14481 */ 14482 if (un->un_retry_statp == NULL) { 14483 /* 14484 * restore the kstat pointer to 14485 * keep kstat counts coherent 14486 * when we do retry the command. 14487 */ 14488 un->un_retry_statp = 14489 saved_statp; 14490 } 14491 14492 if ((un->un_startstop_timeid == NULL) && 14493 (un->un_retry_timeid == NULL) && 14494 (un->un_direct_priority_timeid == 14495 NULL)) { 14496 14497 un->un_retry_timeid = 14498 timeout( 14499 sd_start_retry_command, 14500 un, SD_RESTART_TIMEOUT); 14501 } 14502 goto exit; 14503 } 14504 14505 #else 14506 if (bp == immed_bp) { 14507 break; /* Just fail the command */ 14508 } 14509 #endif 14510 14511 /* Add the buf back to the head of the waitq */ 14512 bp->av_forw = un->un_waitq_headp; 14513 un->un_waitq_headp = bp; 14514 if (un->un_waitq_tailp == NULL) { 14515 un->un_waitq_tailp = bp; 14516 } 14517 goto exit; 14518 14519 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14520 /* 14521 * HBA DMA resource failure. Fail the command 14522 * and continue processing of the queues. 14523 */ 14524 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14525 "sd_start_cmds: " 14526 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14527 break; 14528 14529 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14530 /* 14531 * Note:x86: Partial DMA mapping not supported 14532 * for USCSI commands, and all the needed DMA 14533 * resources were not allocated. 14534 */ 14535 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14536 "sd_start_cmds: " 14537 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14538 break; 14539 14540 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14541 /* 14542 * Note:x86: Request cannot fit into CDB based 14543 * on lba and len. 14544 */ 14545 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14546 "sd_start_cmds: " 14547 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14548 break; 14549 14550 default: 14551 /* Should NEVER get here! */ 14552 panic("scsi_initpkt error"); 14553 /*NOTREACHED*/ 14554 } 14555 14556 /* 14557 * Fatal error in allocating a scsi_pkt for this buf. 14558 * Update kstats & return the buf with an error code. 14559 * We must use sd_return_failed_command_no_restart() to 14560 * avoid a recursive call back into sd_start_cmds(). 14561 * However this also means that we must keep processing 14562 * the waitq here in order to avoid stalling. 14563 */ 14564 if (statp == kstat_waitq_to_runq) { 14565 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14566 } 14567 sd_return_failed_command_no_restart(un, bp, EIO); 14568 if (bp == immed_bp) { 14569 /* immed_bp is gone by now, so clear this */ 14570 immed_bp = NULL; 14571 } 14572 continue; 14573 } 14574 got_pkt: 14575 if (bp == immed_bp) { 14576 /* goto the head of the class.... */ 14577 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14578 } 14579 14580 un->un_ncmds_in_transport++; 14581 SD_UPDATE_KSTATS(un, statp, bp); 14582 14583 /* 14584 * Call scsi_transport() to send the command to the target. 14585 * According to SCSA architecture, we must drop the mutex here 14586 * before calling scsi_transport() in order to avoid deadlock. 14587 * Note that the scsi_pkt's completion routine can be executed 14588 * (from interrupt context) even before the call to 14589 * scsi_transport() returns. 14590 */ 14591 SD_TRACE(SD_LOG_IO_CORE, un, 14592 "sd_start_cmds: calling scsi_transport()\n"); 14593 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14594 14595 mutex_exit(SD_MUTEX(un)); 14596 rval = scsi_transport(xp->xb_pktp); 14597 mutex_enter(SD_MUTEX(un)); 14598 14599 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14600 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14601 14602 switch (rval) { 14603 case TRAN_ACCEPT: 14604 /* Clear this with every pkt accepted by the HBA */ 14605 un->un_tran_fatal_count = 0; 14606 break; /* Success; try the next cmd (if any) */ 14607 14608 case TRAN_BUSY: 14609 un->un_ncmds_in_transport--; 14610 ASSERT(un->un_ncmds_in_transport >= 0); 14611 14612 /* 14613 * Don't retry request sense, the sense data 14614 * is lost when another request is sent. 14615 * Free up the rqs buf and retry 14616 * the original failed cmd. Update kstat. 14617 */ 14618 if (bp == un->un_rqs_bp) { 14619 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14620 bp = sd_mark_rqs_idle(un, xp); 14621 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14622 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14623 kstat_waitq_enter); 14624 goto exit; 14625 } 14626 14627 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14628 /* 14629 * Free the DMA resources for the scsi_pkt. This will 14630 * allow mpxio to select another path the next time 14631 * we call scsi_transport() with this scsi_pkt. 14632 * See sdintr() for the rationalization behind this. 14633 */ 14634 if ((un->un_f_is_fibre == TRUE) && 14635 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14636 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14637 scsi_dmafree(xp->xb_pktp); 14638 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14639 } 14640 #endif 14641 14642 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14643 /* 14644 * Commands that are SD_PATH_DIRECT_PRIORITY 14645 * are for error recovery situations. These do 14646 * not use the normal command waitq, so if they 14647 * get a TRAN_BUSY we cannot put them back onto 14648 * the waitq for later retry. One possible 14649 * problem is that there could already be some 14650 * other command on un_retry_bp that is waiting 14651 * for this one to complete, so we would be 14652 * deadlocked if we put this command back onto 14653 * the waitq for later retry (since un_retry_bp 14654 * must complete before the driver gets back to 14655 * commands on the waitq). 14656 * 14657 * To avoid deadlock we must schedule a callback 14658 * that will restart this command after a set 14659 * interval. This should keep retrying for as 14660 * long as the underlying transport keeps 14661 * returning TRAN_BUSY (just like for other 14662 * commands). Use the same timeout interval as 14663 * for the ordinary TRAN_BUSY retry. 14664 */ 14665 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14666 "sd_start_cmds: scsi_transport() returned " 14667 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14668 14669 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14670 un->un_direct_priority_timeid = 14671 timeout(sd_start_direct_priority_command, 14672 bp, SD_BSY_TIMEOUT / 500); 14673 14674 goto exit; 14675 } 14676 14677 /* 14678 * For TRAN_BUSY, we want to reduce the throttle value, 14679 * unless we are retrying a command. 14680 */ 14681 if (bp != un->un_retry_bp) { 14682 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14683 } 14684 14685 /* 14686 * Set up the bp to be tried again 10 ms later. 14687 * Note:x86: Is there a timeout value in the sd_lun 14688 * for this condition? 14689 */ 14690 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14691 kstat_runq_back_to_waitq); 14692 goto exit; 14693 14694 case TRAN_FATAL_ERROR: 14695 un->un_tran_fatal_count++; 14696 /* FALLTHRU */ 14697 14698 case TRAN_BADPKT: 14699 default: 14700 un->un_ncmds_in_transport--; 14701 ASSERT(un->un_ncmds_in_transport >= 0); 14702 14703 /* 14704 * If this is our REQUEST SENSE command with a 14705 * transport error, we must get back the pointers 14706 * to the original buf, and mark the REQUEST 14707 * SENSE command as "available". 14708 */ 14709 if (bp == un->un_rqs_bp) { 14710 bp = sd_mark_rqs_idle(un, xp); 14711 xp = SD_GET_XBUF(bp); 14712 } else { 14713 /* 14714 * Legacy behavior: do not update transport 14715 * error count for request sense commands. 14716 */ 14717 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14718 } 14719 14720 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14721 sd_print_transport_rejected_message(un, xp, rval); 14722 14723 /* 14724 * We must use sd_return_failed_command_no_restart() to 14725 * avoid a recursive call back into sd_start_cmds(). 14726 * However this also means that we must keep processing 14727 * the waitq here in order to avoid stalling. 14728 */ 14729 sd_return_failed_command_no_restart(un, bp, EIO); 14730 14731 /* 14732 * Notify any threads waiting in sd_ddi_suspend() that 14733 * a command completion has occurred. 14734 */ 14735 if (un->un_state == SD_STATE_SUSPENDED) { 14736 cv_broadcast(&un->un_disk_busy_cv); 14737 } 14738 14739 if (bp == immed_bp) { 14740 /* immed_bp is gone by now, so clear this */ 14741 immed_bp = NULL; 14742 } 14743 break; 14744 } 14745 14746 } while (immed_bp == NULL); 14747 14748 exit: 14749 ASSERT(mutex_owned(SD_MUTEX(un))); 14750 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14751 } 14752 14753 14754 /* 14755 * Function: sd_return_command 14756 * 14757 * Description: Returns a command to its originator (with or without an 14758 * error). Also starts commands waiting to be transported 14759 * to the target. 14760 * 14761 * Context: May be called from interrupt, kernel, or timeout context 14762 */ 14763 14764 static void 14765 sd_return_command(struct sd_lun *un, struct buf *bp) 14766 { 14767 struct sd_xbuf *xp; 14768 #if defined(__i386) || defined(__amd64) 14769 struct scsi_pkt *pktp; 14770 #endif 14771 14772 ASSERT(bp != NULL); 14773 ASSERT(un != NULL); 14774 ASSERT(mutex_owned(SD_MUTEX(un))); 14775 ASSERT(bp != un->un_rqs_bp); 14776 xp = SD_GET_XBUF(bp); 14777 ASSERT(xp != NULL); 14778 14779 #if defined(__i386) || defined(__amd64) 14780 pktp = SD_GET_PKTP(bp); 14781 #endif 14782 14783 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14784 14785 #if defined(__i386) || defined(__amd64) 14786 /* 14787 * Note:x86: check for the "sdrestart failed" case. 14788 */ 14789 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14790 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14791 (xp->xb_pktp->pkt_resid == 0)) { 14792 14793 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14794 /* 14795 * Successfully set up next portion of cmd 14796 * transfer, try sending it 14797 */ 14798 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14799 NULL, NULL, 0, (clock_t)0, NULL); 14800 sd_start_cmds(un, NULL); 14801 return; /* Note:x86: need a return here? */ 14802 } 14803 } 14804 #endif 14805 14806 /* 14807 * If this is the failfast bp, clear it from un_failfast_bp. This 14808 * can happen if upon being re-tried the failfast bp either 14809 * succeeded or encountered another error (possibly even a different 14810 * error than the one that precipitated the failfast state, but in 14811 * that case it would have had to exhaust retries as well). Regardless, 14812 * this should not occur whenever the instance is in the active 14813 * failfast state. 14814 */ 14815 if (bp == un->un_failfast_bp) { 14816 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14817 un->un_failfast_bp = NULL; 14818 } 14819 14820 /* 14821 * Clear the failfast state upon successful completion of ANY cmd. 14822 */ 14823 if (bp->b_error == 0) { 14824 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14825 } 14826 14827 /* 14828 * This is used if the command was retried one or more times. Show that 14829 * we are done with it, and allow processing of the waitq to resume. 14830 */ 14831 if (bp == un->un_retry_bp) { 14832 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14833 "sd_return_command: un:0x%p: " 14834 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14835 un->un_retry_bp = NULL; 14836 un->un_retry_statp = NULL; 14837 } 14838 14839 SD_UPDATE_RDWR_STATS(un, bp); 14840 SD_UPDATE_PARTITION_STATS(un, bp); 14841 14842 switch (un->un_state) { 14843 case SD_STATE_SUSPENDED: 14844 /* 14845 * Notify any threads waiting in sd_ddi_suspend() that 14846 * a command completion has occurred. 14847 */ 14848 cv_broadcast(&un->un_disk_busy_cv); 14849 break; 14850 default: 14851 sd_start_cmds(un, NULL); 14852 break; 14853 } 14854 14855 /* Return this command up the iodone chain to its originator. */ 14856 mutex_exit(SD_MUTEX(un)); 14857 14858 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14859 xp->xb_pktp = NULL; 14860 14861 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14862 14863 ASSERT(!mutex_owned(SD_MUTEX(un))); 14864 mutex_enter(SD_MUTEX(un)); 14865 14866 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14867 } 14868 14869 14870 /* 14871 * Function: sd_return_failed_command 14872 * 14873 * Description: Command completion when an error occurred. 14874 * 14875 * Context: May be called from interrupt context 14876 */ 14877 14878 static void 14879 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14880 { 14881 ASSERT(bp != NULL); 14882 ASSERT(un != NULL); 14883 ASSERT(mutex_owned(SD_MUTEX(un))); 14884 14885 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14886 "sd_return_failed_command: entry\n"); 14887 14888 /* 14889 * b_resid could already be nonzero due to a partial data 14890 * transfer, so do not change it here. 14891 */ 14892 SD_BIOERROR(bp, errcode); 14893 14894 sd_return_command(un, bp); 14895 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14896 "sd_return_failed_command: exit\n"); 14897 } 14898 14899 14900 /* 14901 * Function: sd_return_failed_command_no_restart 14902 * 14903 * Description: Same as sd_return_failed_command, but ensures that no 14904 * call back into sd_start_cmds will be issued. 14905 * 14906 * Context: May be called from interrupt context 14907 */ 14908 14909 static void 14910 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14911 int errcode) 14912 { 14913 struct sd_xbuf *xp; 14914 14915 ASSERT(bp != NULL); 14916 ASSERT(un != NULL); 14917 ASSERT(mutex_owned(SD_MUTEX(un))); 14918 xp = SD_GET_XBUF(bp); 14919 ASSERT(xp != NULL); 14920 ASSERT(errcode != 0); 14921 14922 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14923 "sd_return_failed_command_no_restart: entry\n"); 14924 14925 /* 14926 * b_resid could already be nonzero due to a partial data 14927 * transfer, so do not change it here. 14928 */ 14929 SD_BIOERROR(bp, errcode); 14930 14931 /* 14932 * If this is the failfast bp, clear it. This can happen if the 14933 * failfast bp encounterd a fatal error when we attempted to 14934 * re-try it (such as a scsi_transport(9F) failure). However 14935 * we should NOT be in an active failfast state if the failfast 14936 * bp is not NULL. 14937 */ 14938 if (bp == un->un_failfast_bp) { 14939 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14940 un->un_failfast_bp = NULL; 14941 } 14942 14943 if (bp == un->un_retry_bp) { 14944 /* 14945 * This command was retried one or more times. Show that we are 14946 * done with it, and allow processing of the waitq to resume. 14947 */ 14948 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14949 "sd_return_failed_command_no_restart: " 14950 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14951 un->un_retry_bp = NULL; 14952 un->un_retry_statp = NULL; 14953 } 14954 14955 SD_UPDATE_RDWR_STATS(un, bp); 14956 SD_UPDATE_PARTITION_STATS(un, bp); 14957 14958 mutex_exit(SD_MUTEX(un)); 14959 14960 if (xp->xb_pktp != NULL) { 14961 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14962 xp->xb_pktp = NULL; 14963 } 14964 14965 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14966 14967 mutex_enter(SD_MUTEX(un)); 14968 14969 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14970 "sd_return_failed_command_no_restart: exit\n"); 14971 } 14972 14973 14974 /* 14975 * Function: sd_retry_command 14976 * 14977 * Description: queue up a command for retry, or (optionally) fail it 14978 * if retry counts are exhausted. 14979 * 14980 * Arguments: un - Pointer to the sd_lun struct for the target. 14981 * 14982 * bp - Pointer to the buf for the command to be retried. 14983 * 14984 * retry_check_flag - Flag to see which (if any) of the retry 14985 * counts should be decremented/checked. If the indicated 14986 * retry count is exhausted, then the command will not be 14987 * retried; it will be failed instead. This should use a 14988 * value equal to one of the following: 14989 * 14990 * SD_RETRIES_NOCHECK 14991 * SD_RESD_RETRIES_STANDARD 14992 * SD_RETRIES_VICTIM 14993 * 14994 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14995 * if the check should be made to see of FLAG_ISOLATE is set 14996 * in the pkt. If FLAG_ISOLATE is set, then the command is 14997 * not retried, it is simply failed. 14998 * 14999 * user_funcp - Ptr to function to call before dispatching the 15000 * command. May be NULL if no action needs to be performed. 15001 * (Primarily intended for printing messages.) 15002 * 15003 * user_arg - Optional argument to be passed along to 15004 * the user_funcp call. 15005 * 15006 * failure_code - errno return code to set in the bp if the 15007 * command is going to be failed. 15008 * 15009 * retry_delay - Retry delay interval in (clock_t) units. May 15010 * be zero which indicates that the retry should be retried 15011 * immediately (ie, without an intervening delay). 15012 * 15013 * statp - Ptr to kstat function to be updated if the command 15014 * is queued for a delayed retry. May be NULL if no kstat 15015 * update is desired. 15016 * 15017 * Context: May be called from interupt context. 15018 */ 15019 15020 static void 15021 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15022 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15023 code), void *user_arg, int failure_code, clock_t retry_delay, 15024 void (*statp)(kstat_io_t *)) 15025 { 15026 struct sd_xbuf *xp; 15027 struct scsi_pkt *pktp; 15028 15029 ASSERT(un != NULL); 15030 ASSERT(mutex_owned(SD_MUTEX(un))); 15031 ASSERT(bp != NULL); 15032 xp = SD_GET_XBUF(bp); 15033 ASSERT(xp != NULL); 15034 pktp = SD_GET_PKTP(bp); 15035 ASSERT(pktp != NULL); 15036 15037 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15038 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15039 15040 /* 15041 * If we are syncing or dumping, fail the command to avoid 15042 * recursively calling back into scsi_transport(). 15043 */ 15044 if (ddi_in_panic()) { 15045 goto fail_command_no_log; 15046 } 15047 15048 /* 15049 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15050 * log an error and fail the command. 15051 */ 15052 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15053 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15054 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15055 sd_dump_memory(un, SD_LOG_IO, "CDB", 15056 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15057 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15058 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15059 goto fail_command; 15060 } 15061 15062 /* 15063 * If we are suspended, then put the command onto head of the 15064 * wait queue since we don't want to start more commands. 15065 */ 15066 switch (un->un_state) { 15067 case SD_STATE_SUSPENDED: 15068 case SD_STATE_DUMPING: 15069 bp->av_forw = un->un_waitq_headp; 15070 un->un_waitq_headp = bp; 15071 if (un->un_waitq_tailp == NULL) { 15072 un->un_waitq_tailp = bp; 15073 } 15074 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15075 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15076 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15077 return; 15078 default: 15079 break; 15080 } 15081 15082 /* 15083 * If the caller wants us to check FLAG_ISOLATE, then see if that 15084 * is set; if it is then we do not want to retry the command. 15085 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15086 */ 15087 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15088 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15089 goto fail_command; 15090 } 15091 } 15092 15093 15094 /* 15095 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15096 * command timeout or a selection timeout has occurred. This means 15097 * that we were unable to establish an kind of communication with 15098 * the target, and subsequent retries and/or commands are likely 15099 * to encounter similar results and take a long time to complete. 15100 * 15101 * If this is a failfast error condition, we need to update the 15102 * failfast state, even if this bp does not have B_FAILFAST set. 15103 */ 15104 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15105 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15106 ASSERT(un->un_failfast_bp == NULL); 15107 /* 15108 * If we are already in the active failfast state, and 15109 * another failfast error condition has been detected, 15110 * then fail this command if it has B_FAILFAST set. 15111 * If B_FAILFAST is clear, then maintain the legacy 15112 * behavior of retrying heroically, even tho this will 15113 * take a lot more time to fail the command. 15114 */ 15115 if (bp->b_flags & B_FAILFAST) { 15116 goto fail_command; 15117 } 15118 } else { 15119 /* 15120 * We're not in the active failfast state, but we 15121 * have a failfast error condition, so we must begin 15122 * transition to the next state. We do this regardless 15123 * of whether or not this bp has B_FAILFAST set. 15124 */ 15125 if (un->un_failfast_bp == NULL) { 15126 /* 15127 * This is the first bp to meet a failfast 15128 * condition so save it on un_failfast_bp & 15129 * do normal retry processing. Do not enter 15130 * active failfast state yet. This marks 15131 * entry into the "failfast pending" state. 15132 */ 15133 un->un_failfast_bp = bp; 15134 15135 } else if (un->un_failfast_bp == bp) { 15136 /* 15137 * This is the second time *this* bp has 15138 * encountered a failfast error condition, 15139 * so enter active failfast state & flush 15140 * queues as appropriate. 15141 */ 15142 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15143 un->un_failfast_bp = NULL; 15144 sd_failfast_flushq(un); 15145 15146 /* 15147 * Fail this bp now if B_FAILFAST set; 15148 * otherwise continue with retries. (It would 15149 * be pretty ironic if this bp succeeded on a 15150 * subsequent retry after we just flushed all 15151 * the queues). 15152 */ 15153 if (bp->b_flags & B_FAILFAST) { 15154 goto fail_command; 15155 } 15156 15157 #if !defined(lint) && !defined(__lint) 15158 } else { 15159 /* 15160 * If neither of the preceeding conditionals 15161 * was true, it means that there is some 15162 * *other* bp that has met an inital failfast 15163 * condition and is currently either being 15164 * retried or is waiting to be retried. In 15165 * that case we should perform normal retry 15166 * processing on *this* bp, since there is a 15167 * chance that the current failfast condition 15168 * is transient and recoverable. If that does 15169 * not turn out to be the case, then retries 15170 * will be cleared when the wait queue is 15171 * flushed anyway. 15172 */ 15173 #endif 15174 } 15175 } 15176 } else { 15177 /* 15178 * SD_RETRIES_FAILFAST is clear, which indicates that we 15179 * likely were able to at least establish some level of 15180 * communication with the target and subsequent commands 15181 * and/or retries are likely to get through to the target, 15182 * In this case we want to be aggressive about clearing 15183 * the failfast state. Note that this does not affect 15184 * the "failfast pending" condition. 15185 */ 15186 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15187 } 15188 15189 15190 /* 15191 * Check the specified retry count to see if we can still do 15192 * any retries with this pkt before we should fail it. 15193 */ 15194 switch (retry_check_flag & SD_RETRIES_MASK) { 15195 case SD_RETRIES_VICTIM: 15196 /* 15197 * Check the victim retry count. If exhausted, then fall 15198 * thru & check against the standard retry count. 15199 */ 15200 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15201 /* Increment count & proceed with the retry */ 15202 xp->xb_victim_retry_count++; 15203 break; 15204 } 15205 /* Victim retries exhausted, fall back to std. retries... */ 15206 /* FALLTHRU */ 15207 15208 case SD_RETRIES_STANDARD: 15209 if (xp->xb_retry_count >= un->un_retry_count) { 15210 /* Retries exhausted, fail the command */ 15211 SD_TRACE(SD_LOG_IO_CORE, un, 15212 "sd_retry_command: retries exhausted!\n"); 15213 /* 15214 * update b_resid for failed SCMD_READ & SCMD_WRITE 15215 * commands with nonzero pkt_resid. 15216 */ 15217 if ((pktp->pkt_reason == CMD_CMPLT) && 15218 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15219 (pktp->pkt_resid != 0)) { 15220 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15221 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15222 SD_UPDATE_B_RESID(bp, pktp); 15223 } 15224 } 15225 goto fail_command; 15226 } 15227 xp->xb_retry_count++; 15228 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15229 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15230 break; 15231 15232 case SD_RETRIES_UA: 15233 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15234 /* Retries exhausted, fail the command */ 15235 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15236 "Unit Attention retries exhausted. " 15237 "Check the target.\n"); 15238 goto fail_command; 15239 } 15240 xp->xb_ua_retry_count++; 15241 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15242 "sd_retry_command: retry count:%d\n", 15243 xp->xb_ua_retry_count); 15244 break; 15245 15246 case SD_RETRIES_BUSY: 15247 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15248 /* Retries exhausted, fail the command */ 15249 SD_TRACE(SD_LOG_IO_CORE, un, 15250 "sd_retry_command: retries exhausted!\n"); 15251 goto fail_command; 15252 } 15253 xp->xb_retry_count++; 15254 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15255 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15256 break; 15257 15258 case SD_RETRIES_NOCHECK: 15259 default: 15260 /* No retry count to check. Just proceed with the retry */ 15261 break; 15262 } 15263 15264 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15265 15266 /* 15267 * If we were given a zero timeout, we must attempt to retry the 15268 * command immediately (ie, without a delay). 15269 */ 15270 if (retry_delay == 0) { 15271 /* 15272 * Check some limiting conditions to see if we can actually 15273 * do the immediate retry. If we cannot, then we must 15274 * fall back to queueing up a delayed retry. 15275 */ 15276 if (un->un_ncmds_in_transport >= un->un_throttle) { 15277 /* 15278 * We are at the throttle limit for the target, 15279 * fall back to delayed retry. 15280 */ 15281 retry_delay = SD_BSY_TIMEOUT; 15282 statp = kstat_waitq_enter; 15283 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15284 "sd_retry_command: immed. retry hit " 15285 "throttle!\n"); 15286 } else { 15287 /* 15288 * We're clear to proceed with the immediate retry. 15289 * First call the user-provided function (if any) 15290 */ 15291 if (user_funcp != NULL) { 15292 (*user_funcp)(un, bp, user_arg, 15293 SD_IMMEDIATE_RETRY_ISSUED); 15294 #ifdef __lock_lint 15295 sd_print_incomplete_msg(un, bp, user_arg, 15296 SD_IMMEDIATE_RETRY_ISSUED); 15297 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15298 SD_IMMEDIATE_RETRY_ISSUED); 15299 sd_print_sense_failed_msg(un, bp, user_arg, 15300 SD_IMMEDIATE_RETRY_ISSUED); 15301 #endif 15302 } 15303 15304 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15305 "sd_retry_command: issuing immediate retry\n"); 15306 15307 /* 15308 * Call sd_start_cmds() to transport the command to 15309 * the target. 15310 */ 15311 sd_start_cmds(un, bp); 15312 15313 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15314 "sd_retry_command exit\n"); 15315 return; 15316 } 15317 } 15318 15319 /* 15320 * Set up to retry the command after a delay. 15321 * First call the user-provided function (if any) 15322 */ 15323 if (user_funcp != NULL) { 15324 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15325 } 15326 15327 sd_set_retry_bp(un, bp, retry_delay, statp); 15328 15329 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15330 return; 15331 15332 fail_command: 15333 15334 if (user_funcp != NULL) { 15335 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15336 } 15337 15338 fail_command_no_log: 15339 15340 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15341 "sd_retry_command: returning failed command\n"); 15342 15343 sd_return_failed_command(un, bp, failure_code); 15344 15345 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15346 } 15347 15348 15349 /* 15350 * Function: sd_set_retry_bp 15351 * 15352 * Description: Set up the given bp for retry. 15353 * 15354 * Arguments: un - ptr to associated softstate 15355 * bp - ptr to buf(9S) for the command 15356 * retry_delay - time interval before issuing retry (may be 0) 15357 * statp - optional pointer to kstat function 15358 * 15359 * Context: May be called under interrupt context 15360 */ 15361 15362 static void 15363 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15364 void (*statp)(kstat_io_t *)) 15365 { 15366 ASSERT(un != NULL); 15367 ASSERT(mutex_owned(SD_MUTEX(un))); 15368 ASSERT(bp != NULL); 15369 15370 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15371 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15372 15373 /* 15374 * Indicate that the command is being retried. This will not allow any 15375 * other commands on the wait queue to be transported to the target 15376 * until this command has been completed (success or failure). The 15377 * "retry command" is not transported to the target until the given 15378 * time delay expires, unless the user specified a 0 retry_delay. 15379 * 15380 * Note: the timeout(9F) callback routine is what actually calls 15381 * sd_start_cmds() to transport the command, with the exception of a 15382 * zero retry_delay. The only current implementor of a zero retry delay 15383 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15384 */ 15385 if (un->un_retry_bp == NULL) { 15386 ASSERT(un->un_retry_statp == NULL); 15387 un->un_retry_bp = bp; 15388 15389 /* 15390 * If the user has not specified a delay the command should 15391 * be queued and no timeout should be scheduled. 15392 */ 15393 if (retry_delay == 0) { 15394 /* 15395 * Save the kstat pointer that will be used in the 15396 * call to SD_UPDATE_KSTATS() below, so that 15397 * sd_start_cmds() can correctly decrement the waitq 15398 * count when it is time to transport this command. 15399 */ 15400 un->un_retry_statp = statp; 15401 goto done; 15402 } 15403 } 15404 15405 if (un->un_retry_bp == bp) { 15406 /* 15407 * Save the kstat pointer that will be used in the call to 15408 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15409 * correctly decrement the waitq count when it is time to 15410 * transport this command. 15411 */ 15412 un->un_retry_statp = statp; 15413 15414 /* 15415 * Schedule a timeout if: 15416 * 1) The user has specified a delay. 15417 * 2) There is not a START_STOP_UNIT callback pending. 15418 * 15419 * If no delay has been specified, then it is up to the caller 15420 * to ensure that IO processing continues without stalling. 15421 * Effectively, this means that the caller will issue the 15422 * required call to sd_start_cmds(). The START_STOP_UNIT 15423 * callback does this after the START STOP UNIT command has 15424 * completed. In either of these cases we should not schedule 15425 * a timeout callback here. Also don't schedule the timeout if 15426 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15427 */ 15428 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15429 (un->un_direct_priority_timeid == NULL)) { 15430 un->un_retry_timeid = 15431 timeout(sd_start_retry_command, un, retry_delay); 15432 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15433 "sd_set_retry_bp: setting timeout: un: 0x%p" 15434 " bp:0x%p un_retry_timeid:0x%p\n", 15435 un, bp, un->un_retry_timeid); 15436 } 15437 } else { 15438 /* 15439 * We only get in here if there is already another command 15440 * waiting to be retried. In this case, we just put the 15441 * given command onto the wait queue, so it can be transported 15442 * after the current retry command has completed. 15443 * 15444 * Also we have to make sure that if the command at the head 15445 * of the wait queue is the un_failfast_bp, that we do not 15446 * put ahead of it any other commands that are to be retried. 15447 */ 15448 if ((un->un_failfast_bp != NULL) && 15449 (un->un_failfast_bp == un->un_waitq_headp)) { 15450 /* 15451 * Enqueue this command AFTER the first command on 15452 * the wait queue (which is also un_failfast_bp). 15453 */ 15454 bp->av_forw = un->un_waitq_headp->av_forw; 15455 un->un_waitq_headp->av_forw = bp; 15456 if (un->un_waitq_headp == un->un_waitq_tailp) { 15457 un->un_waitq_tailp = bp; 15458 } 15459 } else { 15460 /* Enqueue this command at the head of the waitq. */ 15461 bp->av_forw = un->un_waitq_headp; 15462 un->un_waitq_headp = bp; 15463 if (un->un_waitq_tailp == NULL) { 15464 un->un_waitq_tailp = bp; 15465 } 15466 } 15467 15468 if (statp == NULL) { 15469 statp = kstat_waitq_enter; 15470 } 15471 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15472 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15473 } 15474 15475 done: 15476 if (statp != NULL) { 15477 SD_UPDATE_KSTATS(un, statp, bp); 15478 } 15479 15480 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15481 "sd_set_retry_bp: exit un:0x%p\n", un); 15482 } 15483 15484 15485 /* 15486 * Function: sd_start_retry_command 15487 * 15488 * Description: Start the command that has been waiting on the target's 15489 * retry queue. Called from timeout(9F) context after the 15490 * retry delay interval has expired. 15491 * 15492 * Arguments: arg - pointer to associated softstate for the device. 15493 * 15494 * Context: timeout(9F) thread context. May not sleep. 15495 */ 15496 15497 static void 15498 sd_start_retry_command(void *arg) 15499 { 15500 struct sd_lun *un = arg; 15501 15502 ASSERT(un != NULL); 15503 ASSERT(!mutex_owned(SD_MUTEX(un))); 15504 15505 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15506 "sd_start_retry_command: entry\n"); 15507 15508 mutex_enter(SD_MUTEX(un)); 15509 15510 un->un_retry_timeid = NULL; 15511 15512 if (un->un_retry_bp != NULL) { 15513 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15514 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15515 un, un->un_retry_bp); 15516 sd_start_cmds(un, un->un_retry_bp); 15517 } 15518 15519 mutex_exit(SD_MUTEX(un)); 15520 15521 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15522 "sd_start_retry_command: exit\n"); 15523 } 15524 15525 15526 /* 15527 * Function: sd_start_direct_priority_command 15528 * 15529 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15530 * received TRAN_BUSY when we called scsi_transport() to send it 15531 * to the underlying HBA. This function is called from timeout(9F) 15532 * context after the delay interval has expired. 15533 * 15534 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15535 * 15536 * Context: timeout(9F) thread context. May not sleep. 15537 */ 15538 15539 static void 15540 sd_start_direct_priority_command(void *arg) 15541 { 15542 struct buf *priority_bp = arg; 15543 struct sd_lun *un; 15544 15545 ASSERT(priority_bp != NULL); 15546 un = SD_GET_UN(priority_bp); 15547 ASSERT(un != NULL); 15548 ASSERT(!mutex_owned(SD_MUTEX(un))); 15549 15550 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15551 "sd_start_direct_priority_command: entry\n"); 15552 15553 mutex_enter(SD_MUTEX(un)); 15554 un->un_direct_priority_timeid = NULL; 15555 sd_start_cmds(un, priority_bp); 15556 mutex_exit(SD_MUTEX(un)); 15557 15558 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15559 "sd_start_direct_priority_command: exit\n"); 15560 } 15561 15562 15563 /* 15564 * Function: sd_send_request_sense_command 15565 * 15566 * Description: Sends a REQUEST SENSE command to the target 15567 * 15568 * Context: May be called from interrupt context. 15569 */ 15570 15571 static void 15572 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15573 struct scsi_pkt *pktp) 15574 { 15575 ASSERT(bp != NULL); 15576 ASSERT(un != NULL); 15577 ASSERT(mutex_owned(SD_MUTEX(un))); 15578 15579 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15580 "entry: buf:0x%p\n", bp); 15581 15582 /* 15583 * If we are syncing or dumping, then fail the command to avoid a 15584 * recursive callback into scsi_transport(). Also fail the command 15585 * if we are suspended (legacy behavior). 15586 */ 15587 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15588 (un->un_state == SD_STATE_DUMPING)) { 15589 sd_return_failed_command(un, bp, EIO); 15590 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15591 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15592 return; 15593 } 15594 15595 /* 15596 * Retry the failed command and don't issue the request sense if: 15597 * 1) the sense buf is busy 15598 * 2) we have 1 or more outstanding commands on the target 15599 * (the sense data will be cleared or invalidated any way) 15600 * 15601 * Note: There could be an issue with not checking a retry limit here, 15602 * the problem is determining which retry limit to check. 15603 */ 15604 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15605 /* Don't retry if the command is flagged as non-retryable */ 15606 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15607 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15608 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15609 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15610 "sd_send_request_sense_command: " 15611 "at full throttle, retrying exit\n"); 15612 } else { 15613 sd_return_failed_command(un, bp, EIO); 15614 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15615 "sd_send_request_sense_command: " 15616 "at full throttle, non-retryable exit\n"); 15617 } 15618 return; 15619 } 15620 15621 sd_mark_rqs_busy(un, bp); 15622 sd_start_cmds(un, un->un_rqs_bp); 15623 15624 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15625 "sd_send_request_sense_command: exit\n"); 15626 } 15627 15628 15629 /* 15630 * Function: sd_mark_rqs_busy 15631 * 15632 * Description: Indicate that the request sense bp for this instance is 15633 * in use. 15634 * 15635 * Context: May be called under interrupt context 15636 */ 15637 15638 static void 15639 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15640 { 15641 struct sd_xbuf *sense_xp; 15642 15643 ASSERT(un != NULL); 15644 ASSERT(bp != NULL); 15645 ASSERT(mutex_owned(SD_MUTEX(un))); 15646 ASSERT(un->un_sense_isbusy == 0); 15647 15648 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15649 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15650 15651 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15652 ASSERT(sense_xp != NULL); 15653 15654 SD_INFO(SD_LOG_IO, un, 15655 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15656 15657 ASSERT(sense_xp->xb_pktp != NULL); 15658 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15659 == (FLAG_SENSING | FLAG_HEAD)); 15660 15661 un->un_sense_isbusy = 1; 15662 un->un_rqs_bp->b_resid = 0; 15663 sense_xp->xb_pktp->pkt_resid = 0; 15664 sense_xp->xb_pktp->pkt_reason = 0; 15665 15666 /* So we can get back the bp at interrupt time! */ 15667 sense_xp->xb_sense_bp = bp; 15668 15669 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15670 15671 /* 15672 * Mark this buf as awaiting sense data. (This is already set in 15673 * the pkt_flags for the RQS packet.) 15674 */ 15675 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15676 15677 sense_xp->xb_retry_count = 0; 15678 sense_xp->xb_victim_retry_count = 0; 15679 sense_xp->xb_ua_retry_count = 0; 15680 sense_xp->xb_dma_resid = 0; 15681 15682 /* Clean up the fields for auto-request sense */ 15683 sense_xp->xb_sense_status = 0; 15684 sense_xp->xb_sense_state = 0; 15685 sense_xp->xb_sense_resid = 0; 15686 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15687 15688 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15689 } 15690 15691 15692 /* 15693 * Function: sd_mark_rqs_idle 15694 * 15695 * Description: SD_MUTEX must be held continuously through this routine 15696 * to prevent reuse of the rqs struct before the caller can 15697 * complete it's processing. 15698 * 15699 * Return Code: Pointer to the RQS buf 15700 * 15701 * Context: May be called under interrupt context 15702 */ 15703 15704 static struct buf * 15705 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15706 { 15707 struct buf *bp; 15708 ASSERT(un != NULL); 15709 ASSERT(sense_xp != NULL); 15710 ASSERT(mutex_owned(SD_MUTEX(un))); 15711 ASSERT(un->un_sense_isbusy != 0); 15712 15713 un->un_sense_isbusy = 0; 15714 bp = sense_xp->xb_sense_bp; 15715 sense_xp->xb_sense_bp = NULL; 15716 15717 /* This pkt is no longer interested in getting sense data */ 15718 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15719 15720 return (bp); 15721 } 15722 15723 15724 15725 /* 15726 * Function: sd_alloc_rqs 15727 * 15728 * Description: Set up the unit to receive auto request sense data 15729 * 15730 * Return Code: DDI_SUCCESS or DDI_FAILURE 15731 * 15732 * Context: Called under attach(9E) context 15733 */ 15734 15735 static int 15736 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15737 { 15738 struct sd_xbuf *xp; 15739 15740 ASSERT(un != NULL); 15741 ASSERT(!mutex_owned(SD_MUTEX(un))); 15742 ASSERT(un->un_rqs_bp == NULL); 15743 ASSERT(un->un_rqs_pktp == NULL); 15744 15745 /* 15746 * First allocate the required buf and scsi_pkt structs, then set up 15747 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15748 */ 15749 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15750 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15751 if (un->un_rqs_bp == NULL) { 15752 return (DDI_FAILURE); 15753 } 15754 15755 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15756 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15757 15758 if (un->un_rqs_pktp == NULL) { 15759 sd_free_rqs(un); 15760 return (DDI_FAILURE); 15761 } 15762 15763 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15764 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15765 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15766 15767 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15768 15769 /* Set up the other needed members in the ARQ scsi_pkt. */ 15770 un->un_rqs_pktp->pkt_comp = sdintr; 15771 un->un_rqs_pktp->pkt_time = sd_io_time; 15772 un->un_rqs_pktp->pkt_flags |= 15773 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15774 15775 /* 15776 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15777 * provide any intpkt, destroypkt routines as we take care of 15778 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15779 */ 15780 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15781 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15782 xp->xb_pktp = un->un_rqs_pktp; 15783 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15784 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15785 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15786 15787 /* 15788 * Save the pointer to the request sense private bp so it can 15789 * be retrieved in sdintr. 15790 */ 15791 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15792 ASSERT(un->un_rqs_bp->b_private == xp); 15793 15794 /* 15795 * See if the HBA supports auto-request sense for the specified 15796 * target/lun. If it does, then try to enable it (if not already 15797 * enabled). 15798 * 15799 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15800 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15801 * return success. However, in both of these cases ARQ is always 15802 * enabled and scsi_ifgetcap will always return true. The best approach 15803 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15804 * 15805 * The 3rd case is the HBA (adp) always return enabled on 15806 * scsi_ifgetgetcap even when it's not enable, the best approach 15807 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15808 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15809 */ 15810 15811 if (un->un_f_is_fibre == TRUE) { 15812 un->un_f_arq_enabled = TRUE; 15813 } else { 15814 #if defined(__i386) || defined(__amd64) 15815 /* 15816 * Circumvent the Adaptec bug, remove this code when 15817 * the bug is fixed 15818 */ 15819 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15820 #endif 15821 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15822 case 0: 15823 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15824 "sd_alloc_rqs: HBA supports ARQ\n"); 15825 /* 15826 * ARQ is supported by this HBA but currently is not 15827 * enabled. Attempt to enable it and if successful then 15828 * mark this instance as ARQ enabled. 15829 */ 15830 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15831 == 1) { 15832 /* Successfully enabled ARQ in the HBA */ 15833 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15834 "sd_alloc_rqs: ARQ enabled\n"); 15835 un->un_f_arq_enabled = TRUE; 15836 } else { 15837 /* Could not enable ARQ in the HBA */ 15838 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15839 "sd_alloc_rqs: failed ARQ enable\n"); 15840 un->un_f_arq_enabled = FALSE; 15841 } 15842 break; 15843 case 1: 15844 /* 15845 * ARQ is supported by this HBA and is already enabled. 15846 * Just mark ARQ as enabled for this instance. 15847 */ 15848 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15849 "sd_alloc_rqs: ARQ already enabled\n"); 15850 un->un_f_arq_enabled = TRUE; 15851 break; 15852 default: 15853 /* 15854 * ARQ is not supported by this HBA; disable it for this 15855 * instance. 15856 */ 15857 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15858 "sd_alloc_rqs: HBA does not support ARQ\n"); 15859 un->un_f_arq_enabled = FALSE; 15860 break; 15861 } 15862 } 15863 15864 return (DDI_SUCCESS); 15865 } 15866 15867 15868 /* 15869 * Function: sd_free_rqs 15870 * 15871 * Description: Cleanup for the pre-instance RQS command. 15872 * 15873 * Context: Kernel thread context 15874 */ 15875 15876 static void 15877 sd_free_rqs(struct sd_lun *un) 15878 { 15879 ASSERT(un != NULL); 15880 15881 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15882 15883 /* 15884 * If consistent memory is bound to a scsi_pkt, the pkt 15885 * has to be destroyed *before* freeing the consistent memory. 15886 * Don't change the sequence of this operations. 15887 * scsi_destroy_pkt() might access memory, which isn't allowed, 15888 * after it was freed in scsi_free_consistent_buf(). 15889 */ 15890 if (un->un_rqs_pktp != NULL) { 15891 scsi_destroy_pkt(un->un_rqs_pktp); 15892 un->un_rqs_pktp = NULL; 15893 } 15894 15895 if (un->un_rqs_bp != NULL) { 15896 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 15897 scsi_free_consistent_buf(un->un_rqs_bp); 15898 un->un_rqs_bp = NULL; 15899 } 15900 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15901 } 15902 15903 15904 15905 /* 15906 * Function: sd_reduce_throttle 15907 * 15908 * Description: Reduces the maximun # of outstanding commands on a 15909 * target to the current number of outstanding commands. 15910 * Queues a tiemout(9F) callback to restore the limit 15911 * after a specified interval has elapsed. 15912 * Typically used when we get a TRAN_BUSY return code 15913 * back from scsi_transport(). 15914 * 15915 * Arguments: un - ptr to the sd_lun softstate struct 15916 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15917 * 15918 * Context: May be called from interrupt context 15919 */ 15920 15921 static void 15922 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15923 { 15924 ASSERT(un != NULL); 15925 ASSERT(mutex_owned(SD_MUTEX(un))); 15926 ASSERT(un->un_ncmds_in_transport >= 0); 15927 15928 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15929 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15930 un, un->un_throttle, un->un_ncmds_in_transport); 15931 15932 if (un->un_throttle > 1) { 15933 if (un->un_f_use_adaptive_throttle == TRUE) { 15934 switch (throttle_type) { 15935 case SD_THROTTLE_TRAN_BUSY: 15936 if (un->un_busy_throttle == 0) { 15937 un->un_busy_throttle = un->un_throttle; 15938 } 15939 break; 15940 case SD_THROTTLE_QFULL: 15941 un->un_busy_throttle = 0; 15942 break; 15943 default: 15944 ASSERT(FALSE); 15945 } 15946 15947 if (un->un_ncmds_in_transport > 0) { 15948 un->un_throttle = un->un_ncmds_in_transport; 15949 } 15950 15951 } else { 15952 if (un->un_ncmds_in_transport == 0) { 15953 un->un_throttle = 1; 15954 } else { 15955 un->un_throttle = un->un_ncmds_in_transport; 15956 } 15957 } 15958 } 15959 15960 /* Reschedule the timeout if none is currently active */ 15961 if (un->un_reset_throttle_timeid == NULL) { 15962 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15963 un, SD_THROTTLE_RESET_INTERVAL); 15964 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15965 "sd_reduce_throttle: timeout scheduled!\n"); 15966 } 15967 15968 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15969 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15970 } 15971 15972 15973 15974 /* 15975 * Function: sd_restore_throttle 15976 * 15977 * Description: Callback function for timeout(9F). Resets the current 15978 * value of un->un_throttle to its default. 15979 * 15980 * Arguments: arg - pointer to associated softstate for the device. 15981 * 15982 * Context: May be called from interrupt context 15983 */ 15984 15985 static void 15986 sd_restore_throttle(void *arg) 15987 { 15988 struct sd_lun *un = arg; 15989 15990 ASSERT(un != NULL); 15991 ASSERT(!mutex_owned(SD_MUTEX(un))); 15992 15993 mutex_enter(SD_MUTEX(un)); 15994 15995 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15996 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15997 15998 un->un_reset_throttle_timeid = NULL; 15999 16000 if (un->un_f_use_adaptive_throttle == TRUE) { 16001 /* 16002 * If un_busy_throttle is nonzero, then it contains the 16003 * value that un_throttle was when we got a TRAN_BUSY back 16004 * from scsi_transport(). We want to revert back to this 16005 * value. 16006 * 16007 * In the QFULL case, the throttle limit will incrementally 16008 * increase until it reaches max throttle. 16009 */ 16010 if (un->un_busy_throttle > 0) { 16011 un->un_throttle = un->un_busy_throttle; 16012 un->un_busy_throttle = 0; 16013 } else { 16014 /* 16015 * increase throttle by 10% open gate slowly, schedule 16016 * another restore if saved throttle has not been 16017 * reached 16018 */ 16019 short throttle; 16020 if (sd_qfull_throttle_enable) { 16021 throttle = un->un_throttle + 16022 max((un->un_throttle / 10), 1); 16023 un->un_throttle = 16024 (throttle < un->un_saved_throttle) ? 16025 throttle : un->un_saved_throttle; 16026 if (un->un_throttle < un->un_saved_throttle) { 16027 un->un_reset_throttle_timeid = 16028 timeout(sd_restore_throttle, 16029 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16030 } 16031 } 16032 } 16033 16034 /* 16035 * If un_throttle has fallen below the low-water mark, we 16036 * restore the maximum value here (and allow it to ratchet 16037 * down again if necessary). 16038 */ 16039 if (un->un_throttle < un->un_min_throttle) { 16040 un->un_throttle = un->un_saved_throttle; 16041 } 16042 } else { 16043 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16044 "restoring limit from 0x%x to 0x%x\n", 16045 un->un_throttle, un->un_saved_throttle); 16046 un->un_throttle = un->un_saved_throttle; 16047 } 16048 16049 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16050 "sd_restore_throttle: calling sd_start_cmds!\n"); 16051 16052 sd_start_cmds(un, NULL); 16053 16054 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16055 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16056 un, un->un_throttle); 16057 16058 mutex_exit(SD_MUTEX(un)); 16059 16060 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16061 } 16062 16063 /* 16064 * Function: sdrunout 16065 * 16066 * Description: Callback routine for scsi_init_pkt when a resource allocation 16067 * fails. 16068 * 16069 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16070 * soft state instance. 16071 * 16072 * Return Code: The scsi_init_pkt routine allows for the callback function to 16073 * return a 0 indicating the callback should be rescheduled or a 1 16074 * indicating not to reschedule. This routine always returns 1 16075 * because the driver always provides a callback function to 16076 * scsi_init_pkt. This results in a callback always being scheduled 16077 * (via the scsi_init_pkt callback implementation) if a resource 16078 * failure occurs. 16079 * 16080 * Context: This callback function may not block or call routines that block 16081 * 16082 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16083 * request persisting at the head of the list which cannot be 16084 * satisfied even after multiple retries. In the future the driver 16085 * may implement some time of maximum runout count before failing 16086 * an I/O. 16087 */ 16088 16089 static int 16090 sdrunout(caddr_t arg) 16091 { 16092 struct sd_lun *un = (struct sd_lun *)arg; 16093 16094 ASSERT(un != NULL); 16095 ASSERT(!mutex_owned(SD_MUTEX(un))); 16096 16097 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16098 16099 mutex_enter(SD_MUTEX(un)); 16100 sd_start_cmds(un, NULL); 16101 mutex_exit(SD_MUTEX(un)); 16102 /* 16103 * This callback routine always returns 1 (i.e. do not reschedule) 16104 * because we always specify sdrunout as the callback handler for 16105 * scsi_init_pkt inside the call to sd_start_cmds. 16106 */ 16107 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16108 return (1); 16109 } 16110 16111 16112 /* 16113 * Function: sdintr 16114 * 16115 * Description: Completion callback routine for scsi_pkt(9S) structs 16116 * sent to the HBA driver via scsi_transport(9F). 16117 * 16118 * Context: Interrupt context 16119 */ 16120 16121 static void 16122 sdintr(struct scsi_pkt *pktp) 16123 { 16124 struct buf *bp; 16125 struct sd_xbuf *xp; 16126 struct sd_lun *un; 16127 16128 ASSERT(pktp != NULL); 16129 bp = (struct buf *)pktp->pkt_private; 16130 ASSERT(bp != NULL); 16131 xp = SD_GET_XBUF(bp); 16132 ASSERT(xp != NULL); 16133 ASSERT(xp->xb_pktp != NULL); 16134 un = SD_GET_UN(bp); 16135 ASSERT(un != NULL); 16136 ASSERT(!mutex_owned(SD_MUTEX(un))); 16137 16138 #ifdef SD_FAULT_INJECTION 16139 16140 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16141 /* SD FaultInjection */ 16142 sd_faultinjection(pktp); 16143 16144 #endif /* SD_FAULT_INJECTION */ 16145 16146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16147 " xp:0x%p, un:0x%p\n", bp, xp, un); 16148 16149 mutex_enter(SD_MUTEX(un)); 16150 16151 /* Reduce the count of the #commands currently in transport */ 16152 un->un_ncmds_in_transport--; 16153 ASSERT(un->un_ncmds_in_transport >= 0); 16154 16155 /* Increment counter to indicate that the callback routine is active */ 16156 un->un_in_callback++; 16157 16158 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16159 16160 #ifdef SDDEBUG 16161 if (bp == un->un_retry_bp) { 16162 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16163 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16164 un, un->un_retry_bp, un->un_ncmds_in_transport); 16165 } 16166 #endif 16167 16168 /* 16169 * If pkt_reason is CMD_DEV_GONE, just fail the command 16170 */ 16171 if (pktp->pkt_reason == CMD_DEV_GONE) { 16172 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16173 "Device is gone\n"); 16174 sd_return_failed_command(un, bp, EIO); 16175 goto exit; 16176 } 16177 16178 /* 16179 * First see if the pkt has auto-request sense data with it.... 16180 * Look at the packet state first so we don't take a performance 16181 * hit looking at the arq enabled flag unless absolutely necessary. 16182 */ 16183 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16184 (un->un_f_arq_enabled == TRUE)) { 16185 /* 16186 * The HBA did an auto request sense for this command so check 16187 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16188 * driver command that should not be retried. 16189 */ 16190 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16191 /* 16192 * Save the relevant sense info into the xp for the 16193 * original cmd. 16194 */ 16195 struct scsi_arq_status *asp; 16196 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16197 xp->xb_sense_status = 16198 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16199 xp->xb_sense_state = asp->sts_rqpkt_state; 16200 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16201 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16202 min(sizeof (struct scsi_extended_sense), 16203 SENSE_LENGTH)); 16204 16205 /* fail the command */ 16206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16207 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16208 sd_return_failed_command(un, bp, EIO); 16209 goto exit; 16210 } 16211 16212 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16213 /* 16214 * We want to either retry or fail this command, so free 16215 * the DMA resources here. If we retry the command then 16216 * the DMA resources will be reallocated in sd_start_cmds(). 16217 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16218 * causes the *entire* transfer to start over again from the 16219 * beginning of the request, even for PARTIAL chunks that 16220 * have already transferred successfully. 16221 */ 16222 if ((un->un_f_is_fibre == TRUE) && 16223 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16224 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16225 scsi_dmafree(pktp); 16226 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16227 } 16228 #endif 16229 16230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16231 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16232 16233 sd_handle_auto_request_sense(un, bp, xp, pktp); 16234 goto exit; 16235 } 16236 16237 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16238 if (pktp->pkt_flags & FLAG_SENSING) { 16239 /* This pktp is from the unit's REQUEST_SENSE command */ 16240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16241 "sdintr: sd_handle_request_sense\n"); 16242 sd_handle_request_sense(un, bp, xp, pktp); 16243 goto exit; 16244 } 16245 16246 /* 16247 * Check to see if the command successfully completed as requested; 16248 * this is the most common case (and also the hot performance path). 16249 * 16250 * Requirements for successful completion are: 16251 * pkt_reason is CMD_CMPLT and packet status is status good. 16252 * In addition: 16253 * - A residual of zero indicates successful completion no matter what 16254 * the command is. 16255 * - If the residual is not zero and the command is not a read or 16256 * write, then it's still defined as successful completion. In other 16257 * words, if the command is a read or write the residual must be 16258 * zero for successful completion. 16259 * - If the residual is not zero and the command is a read or 16260 * write, and it's a USCSICMD, then it's still defined as 16261 * successful completion. 16262 */ 16263 if ((pktp->pkt_reason == CMD_CMPLT) && 16264 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16265 16266 /* 16267 * Since this command is returned with a good status, we 16268 * can reset the count for Sonoma failover. 16269 */ 16270 un->un_sonoma_failure_count = 0; 16271 16272 /* 16273 * Return all USCSI commands on good status 16274 */ 16275 if (pktp->pkt_resid == 0) { 16276 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16277 "sdintr: returning command for resid == 0\n"); 16278 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16279 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16280 SD_UPDATE_B_RESID(bp, pktp); 16281 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16282 "sdintr: returning command for resid != 0\n"); 16283 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16284 SD_UPDATE_B_RESID(bp, pktp); 16285 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16286 "sdintr: returning uscsi command\n"); 16287 } else { 16288 goto not_successful; 16289 } 16290 sd_return_command(un, bp); 16291 16292 /* 16293 * Decrement counter to indicate that the callback routine 16294 * is done. 16295 */ 16296 un->un_in_callback--; 16297 ASSERT(un->un_in_callback >= 0); 16298 mutex_exit(SD_MUTEX(un)); 16299 16300 return; 16301 } 16302 16303 not_successful: 16304 16305 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16306 /* 16307 * The following is based upon knowledge of the underlying transport 16308 * and its use of DMA resources. This code should be removed when 16309 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16310 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16311 * and sd_start_cmds(). 16312 * 16313 * Free any DMA resources associated with this command if there 16314 * is a chance it could be retried or enqueued for later retry. 16315 * If we keep the DMA binding then mpxio cannot reissue the 16316 * command on another path whenever a path failure occurs. 16317 * 16318 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16319 * causes the *entire* transfer to start over again from the 16320 * beginning of the request, even for PARTIAL chunks that 16321 * have already transferred successfully. 16322 * 16323 * This is only done for non-uscsi commands (and also skipped for the 16324 * driver's internal RQS command). Also just do this for Fibre Channel 16325 * devices as these are the only ones that support mpxio. 16326 */ 16327 if ((un->un_f_is_fibre == TRUE) && 16328 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16329 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16330 scsi_dmafree(pktp); 16331 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16332 } 16333 #endif 16334 16335 /* 16336 * The command did not successfully complete as requested so check 16337 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16338 * driver command that should not be retried so just return. If 16339 * FLAG_DIAGNOSE is not set the error will be processed below. 16340 */ 16341 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16342 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16343 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16344 /* 16345 * Issue a request sense if a check condition caused the error 16346 * (we handle the auto request sense case above), otherwise 16347 * just fail the command. 16348 */ 16349 if ((pktp->pkt_reason == CMD_CMPLT) && 16350 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16351 sd_send_request_sense_command(un, bp, pktp); 16352 } else { 16353 sd_return_failed_command(un, bp, EIO); 16354 } 16355 goto exit; 16356 } 16357 16358 /* 16359 * The command did not successfully complete as requested so process 16360 * the error, retry, and/or attempt recovery. 16361 */ 16362 switch (pktp->pkt_reason) { 16363 case CMD_CMPLT: 16364 switch (SD_GET_PKT_STATUS(pktp)) { 16365 case STATUS_GOOD: 16366 /* 16367 * The command completed successfully with a non-zero 16368 * residual 16369 */ 16370 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16371 "sdintr: STATUS_GOOD \n"); 16372 sd_pkt_status_good(un, bp, xp, pktp); 16373 break; 16374 16375 case STATUS_CHECK: 16376 case STATUS_TERMINATED: 16377 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16378 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16379 sd_pkt_status_check_condition(un, bp, xp, pktp); 16380 break; 16381 16382 case STATUS_BUSY: 16383 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16384 "sdintr: STATUS_BUSY\n"); 16385 sd_pkt_status_busy(un, bp, xp, pktp); 16386 break; 16387 16388 case STATUS_RESERVATION_CONFLICT: 16389 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16390 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16391 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16392 break; 16393 16394 case STATUS_QFULL: 16395 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16396 "sdintr: STATUS_QFULL\n"); 16397 sd_pkt_status_qfull(un, bp, xp, pktp); 16398 break; 16399 16400 case STATUS_MET: 16401 case STATUS_INTERMEDIATE: 16402 case STATUS_SCSI2: 16403 case STATUS_INTERMEDIATE_MET: 16404 case STATUS_ACA_ACTIVE: 16405 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16406 "Unexpected SCSI status received: 0x%x\n", 16407 SD_GET_PKT_STATUS(pktp)); 16408 sd_return_failed_command(un, bp, EIO); 16409 break; 16410 16411 default: 16412 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16413 "Invalid SCSI status received: 0x%x\n", 16414 SD_GET_PKT_STATUS(pktp)); 16415 sd_return_failed_command(un, bp, EIO); 16416 break; 16417 16418 } 16419 break; 16420 16421 case CMD_INCOMPLETE: 16422 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16423 "sdintr: CMD_INCOMPLETE\n"); 16424 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16425 break; 16426 case CMD_TRAN_ERR: 16427 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16428 "sdintr: CMD_TRAN_ERR\n"); 16429 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16430 break; 16431 case CMD_RESET: 16432 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16433 "sdintr: CMD_RESET \n"); 16434 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16435 break; 16436 case CMD_ABORTED: 16437 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16438 "sdintr: CMD_ABORTED \n"); 16439 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16440 break; 16441 case CMD_TIMEOUT: 16442 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16443 "sdintr: CMD_TIMEOUT\n"); 16444 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16445 break; 16446 case CMD_UNX_BUS_FREE: 16447 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16448 "sdintr: CMD_UNX_BUS_FREE \n"); 16449 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16450 break; 16451 case CMD_TAG_REJECT: 16452 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16453 "sdintr: CMD_TAG_REJECT\n"); 16454 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16455 break; 16456 default: 16457 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16458 "sdintr: default\n"); 16459 sd_pkt_reason_default(un, bp, xp, pktp); 16460 break; 16461 } 16462 16463 exit: 16464 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16465 16466 /* Decrement counter to indicate that the callback routine is done. */ 16467 un->un_in_callback--; 16468 ASSERT(un->un_in_callback >= 0); 16469 16470 /* 16471 * At this point, the pkt has been dispatched, ie, it is either 16472 * being re-tried or has been returned to its caller and should 16473 * not be referenced. 16474 */ 16475 16476 mutex_exit(SD_MUTEX(un)); 16477 } 16478 16479 16480 /* 16481 * Function: sd_print_incomplete_msg 16482 * 16483 * Description: Prints the error message for a CMD_INCOMPLETE error. 16484 * 16485 * Arguments: un - ptr to associated softstate for the device. 16486 * bp - ptr to the buf(9S) for the command. 16487 * arg - message string ptr 16488 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16489 * or SD_NO_RETRY_ISSUED. 16490 * 16491 * Context: May be called under interrupt context 16492 */ 16493 16494 static void 16495 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16496 { 16497 struct scsi_pkt *pktp; 16498 char *msgp; 16499 char *cmdp = arg; 16500 16501 ASSERT(un != NULL); 16502 ASSERT(mutex_owned(SD_MUTEX(un))); 16503 ASSERT(bp != NULL); 16504 ASSERT(arg != NULL); 16505 pktp = SD_GET_PKTP(bp); 16506 ASSERT(pktp != NULL); 16507 16508 switch (code) { 16509 case SD_DELAYED_RETRY_ISSUED: 16510 case SD_IMMEDIATE_RETRY_ISSUED: 16511 msgp = "retrying"; 16512 break; 16513 case SD_NO_RETRY_ISSUED: 16514 default: 16515 msgp = "giving up"; 16516 break; 16517 } 16518 16519 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16520 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16521 "incomplete %s- %s\n", cmdp, msgp); 16522 } 16523 } 16524 16525 16526 16527 /* 16528 * Function: sd_pkt_status_good 16529 * 16530 * Description: Processing for a STATUS_GOOD code in pkt_status. 16531 * 16532 * Context: May be called under interrupt context 16533 */ 16534 16535 static void 16536 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16537 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16538 { 16539 char *cmdp; 16540 16541 ASSERT(un != NULL); 16542 ASSERT(mutex_owned(SD_MUTEX(un))); 16543 ASSERT(bp != NULL); 16544 ASSERT(xp != NULL); 16545 ASSERT(pktp != NULL); 16546 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16547 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16548 ASSERT(pktp->pkt_resid != 0); 16549 16550 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16551 16552 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16553 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16554 case SCMD_READ: 16555 cmdp = "read"; 16556 break; 16557 case SCMD_WRITE: 16558 cmdp = "write"; 16559 break; 16560 default: 16561 SD_UPDATE_B_RESID(bp, pktp); 16562 sd_return_command(un, bp); 16563 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16564 return; 16565 } 16566 16567 /* 16568 * See if we can retry the read/write, preferrably immediately. 16569 * If retries are exhaused, then sd_retry_command() will update 16570 * the b_resid count. 16571 */ 16572 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16573 cmdp, EIO, (clock_t)0, NULL); 16574 16575 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16576 } 16577 16578 16579 16580 16581 16582 /* 16583 * Function: sd_handle_request_sense 16584 * 16585 * Description: Processing for non-auto Request Sense command. 16586 * 16587 * Arguments: un - ptr to associated softstate 16588 * sense_bp - ptr to buf(9S) for the RQS command 16589 * sense_xp - ptr to the sd_xbuf for the RQS command 16590 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16591 * 16592 * Context: May be called under interrupt context 16593 */ 16594 16595 static void 16596 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16597 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16598 { 16599 struct buf *cmd_bp; /* buf for the original command */ 16600 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16601 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16602 16603 ASSERT(un != NULL); 16604 ASSERT(mutex_owned(SD_MUTEX(un))); 16605 ASSERT(sense_bp != NULL); 16606 ASSERT(sense_xp != NULL); 16607 ASSERT(sense_pktp != NULL); 16608 16609 /* 16610 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16611 * RQS command and not the original command. 16612 */ 16613 ASSERT(sense_pktp == un->un_rqs_pktp); 16614 ASSERT(sense_bp == un->un_rqs_bp); 16615 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16616 (FLAG_SENSING | FLAG_HEAD)); 16617 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16618 FLAG_SENSING) == FLAG_SENSING); 16619 16620 /* These are the bp, xp, and pktp for the original command */ 16621 cmd_bp = sense_xp->xb_sense_bp; 16622 cmd_xp = SD_GET_XBUF(cmd_bp); 16623 cmd_pktp = SD_GET_PKTP(cmd_bp); 16624 16625 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16626 /* 16627 * The REQUEST SENSE command failed. Release the REQUEST 16628 * SENSE command for re-use, get back the bp for the original 16629 * command, and attempt to re-try the original command if 16630 * FLAG_DIAGNOSE is not set in the original packet. 16631 */ 16632 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16633 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16634 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16635 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16636 NULL, NULL, EIO, (clock_t)0, NULL); 16637 return; 16638 } 16639 } 16640 16641 /* 16642 * Save the relevant sense info into the xp for the original cmd. 16643 * 16644 * Note: if the request sense failed the state info will be zero 16645 * as set in sd_mark_rqs_busy() 16646 */ 16647 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16648 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16649 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16650 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16651 16652 /* 16653 * Free up the RQS command.... 16654 * NOTE: 16655 * Must do this BEFORE calling sd_validate_sense_data! 16656 * sd_validate_sense_data may return the original command in 16657 * which case the pkt will be freed and the flags can no 16658 * longer be touched. 16659 * SD_MUTEX is held through this process until the command 16660 * is dispatched based upon the sense data, so there are 16661 * no race conditions. 16662 */ 16663 (void) sd_mark_rqs_idle(un, sense_xp); 16664 16665 /* 16666 * For a retryable command see if we have valid sense data, if so then 16667 * turn it over to sd_decode_sense() to figure out the right course of 16668 * action. Just fail a non-retryable command. 16669 */ 16670 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16671 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16672 SD_SENSE_DATA_IS_VALID) { 16673 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16674 } 16675 } else { 16676 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16677 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16678 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16679 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16680 sd_return_failed_command(un, cmd_bp, EIO); 16681 } 16682 } 16683 16684 16685 16686 16687 /* 16688 * Function: sd_handle_auto_request_sense 16689 * 16690 * Description: Processing for auto-request sense information. 16691 * 16692 * Arguments: un - ptr to associated softstate 16693 * bp - ptr to buf(9S) for the command 16694 * xp - ptr to the sd_xbuf for the command 16695 * pktp - ptr to the scsi_pkt(9S) for the command 16696 * 16697 * Context: May be called under interrupt context 16698 */ 16699 16700 static void 16701 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16702 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16703 { 16704 struct scsi_arq_status *asp; 16705 16706 ASSERT(un != NULL); 16707 ASSERT(mutex_owned(SD_MUTEX(un))); 16708 ASSERT(bp != NULL); 16709 ASSERT(xp != NULL); 16710 ASSERT(pktp != NULL); 16711 ASSERT(pktp != un->un_rqs_pktp); 16712 ASSERT(bp != un->un_rqs_bp); 16713 16714 /* 16715 * For auto-request sense, we get a scsi_arq_status back from 16716 * the HBA, with the sense data in the sts_sensedata member. 16717 * The pkt_scbp of the packet points to this scsi_arq_status. 16718 */ 16719 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16720 16721 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16722 /* 16723 * The auto REQUEST SENSE failed; see if we can re-try 16724 * the original command. 16725 */ 16726 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16727 "auto request sense failed (reason=%s)\n", 16728 scsi_rname(asp->sts_rqpkt_reason)); 16729 16730 sd_reset_target(un, pktp); 16731 16732 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16733 NULL, NULL, EIO, (clock_t)0, NULL); 16734 return; 16735 } 16736 16737 /* Save the relevant sense info into the xp for the original cmd. */ 16738 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16739 xp->xb_sense_state = asp->sts_rqpkt_state; 16740 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16741 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16742 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16743 16744 /* 16745 * See if we have valid sense data, if so then turn it over to 16746 * sd_decode_sense() to figure out the right course of action. 16747 */ 16748 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16749 sd_decode_sense(un, bp, xp, pktp); 16750 } 16751 } 16752 16753 16754 /* 16755 * Function: sd_print_sense_failed_msg 16756 * 16757 * Description: Print log message when RQS has failed. 16758 * 16759 * Arguments: un - ptr to associated softstate 16760 * bp - ptr to buf(9S) for the command 16761 * arg - generic message string ptr 16762 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16763 * or SD_NO_RETRY_ISSUED 16764 * 16765 * Context: May be called from interrupt context 16766 */ 16767 16768 static void 16769 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16770 int code) 16771 { 16772 char *msgp = arg; 16773 16774 ASSERT(un != NULL); 16775 ASSERT(mutex_owned(SD_MUTEX(un))); 16776 ASSERT(bp != NULL); 16777 16778 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16779 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16780 } 16781 } 16782 16783 16784 /* 16785 * Function: sd_validate_sense_data 16786 * 16787 * Description: Check the given sense data for validity. 16788 * If the sense data is not valid, the command will 16789 * be either failed or retried! 16790 * 16791 * Return Code: SD_SENSE_DATA_IS_INVALID 16792 * SD_SENSE_DATA_IS_VALID 16793 * 16794 * Context: May be called from interrupt context 16795 */ 16796 16797 static int 16798 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16799 { 16800 struct scsi_extended_sense *esp; 16801 struct scsi_pkt *pktp; 16802 size_t actual_len; 16803 char *msgp = NULL; 16804 16805 ASSERT(un != NULL); 16806 ASSERT(mutex_owned(SD_MUTEX(un))); 16807 ASSERT(bp != NULL); 16808 ASSERT(bp != un->un_rqs_bp); 16809 ASSERT(xp != NULL); 16810 16811 pktp = SD_GET_PKTP(bp); 16812 ASSERT(pktp != NULL); 16813 16814 /* 16815 * Check the status of the RQS command (auto or manual). 16816 */ 16817 switch (xp->xb_sense_status & STATUS_MASK) { 16818 case STATUS_GOOD: 16819 break; 16820 16821 case STATUS_RESERVATION_CONFLICT: 16822 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16823 return (SD_SENSE_DATA_IS_INVALID); 16824 16825 case STATUS_BUSY: 16826 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16827 "Busy Status on REQUEST SENSE\n"); 16828 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16829 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16830 return (SD_SENSE_DATA_IS_INVALID); 16831 16832 case STATUS_QFULL: 16833 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16834 "QFULL Status on REQUEST SENSE\n"); 16835 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16836 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16837 return (SD_SENSE_DATA_IS_INVALID); 16838 16839 case STATUS_CHECK: 16840 case STATUS_TERMINATED: 16841 msgp = "Check Condition on REQUEST SENSE\n"; 16842 goto sense_failed; 16843 16844 default: 16845 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16846 goto sense_failed; 16847 } 16848 16849 /* 16850 * See if we got the minimum required amount of sense data. 16851 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16852 * or less. 16853 */ 16854 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 16855 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16856 (actual_len == 0)) { 16857 msgp = "Request Sense couldn't get sense data\n"; 16858 goto sense_failed; 16859 } 16860 16861 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16862 msgp = "Not enough sense information\n"; 16863 goto sense_failed; 16864 } 16865 16866 /* 16867 * We require the extended sense data 16868 */ 16869 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16870 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16871 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16872 static char tmp[8]; 16873 static char buf[148]; 16874 char *p = (char *)(xp->xb_sense_data); 16875 int i; 16876 16877 mutex_enter(&sd_sense_mutex); 16878 (void) strcpy(buf, "undecodable sense information:"); 16879 for (i = 0; i < actual_len; i++) { 16880 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16881 (void) strcpy(&buf[strlen(buf)], tmp); 16882 } 16883 i = strlen(buf); 16884 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16885 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 16886 mutex_exit(&sd_sense_mutex); 16887 } 16888 /* Note: Legacy behavior, fail the command with no retry */ 16889 sd_return_failed_command(un, bp, EIO); 16890 return (SD_SENSE_DATA_IS_INVALID); 16891 } 16892 16893 /* 16894 * Check that es_code is valid (es_class concatenated with es_code 16895 * make up the "response code" field. es_class will always be 7, so 16896 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16897 * format. 16898 */ 16899 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16900 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16901 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16902 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16903 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16904 goto sense_failed; 16905 } 16906 16907 return (SD_SENSE_DATA_IS_VALID); 16908 16909 sense_failed: 16910 /* 16911 * If the request sense failed (for whatever reason), attempt 16912 * to retry the original command. 16913 */ 16914 #if defined(__i386) || defined(__amd64) 16915 /* 16916 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 16917 * sddef.h for Sparc platform, and x86 uses 1 binary 16918 * for both SCSI/FC. 16919 * The SD_RETRY_DELAY value need to be adjusted here 16920 * when SD_RETRY_DELAY change in sddef.h 16921 */ 16922 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16923 sd_print_sense_failed_msg, msgp, EIO, 16924 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 16925 #else 16926 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16927 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 16928 #endif 16929 16930 return (SD_SENSE_DATA_IS_INVALID); 16931 } 16932 16933 16934 16935 /* 16936 * Function: sd_decode_sense 16937 * 16938 * Description: Take recovery action(s) when SCSI Sense Data is received. 16939 * 16940 * Context: Interrupt context. 16941 */ 16942 16943 static void 16944 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16945 struct scsi_pkt *pktp) 16946 { 16947 struct scsi_extended_sense *esp; 16948 struct scsi_descr_sense_hdr *sdsp; 16949 uint8_t asc, ascq, sense_key; 16950 16951 ASSERT(un != NULL); 16952 ASSERT(mutex_owned(SD_MUTEX(un))); 16953 ASSERT(bp != NULL); 16954 ASSERT(bp != un->un_rqs_bp); 16955 ASSERT(xp != NULL); 16956 ASSERT(pktp != NULL); 16957 16958 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16959 16960 switch (esp->es_code) { 16961 case CODE_FMT_DESCR_CURRENT: 16962 case CODE_FMT_DESCR_DEFERRED: 16963 sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data; 16964 sense_key = sdsp->ds_key; 16965 asc = sdsp->ds_add_code; 16966 ascq = sdsp->ds_qual_code; 16967 break; 16968 case CODE_FMT_VENDOR_SPECIFIC: 16969 case CODE_FMT_FIXED_CURRENT: 16970 case CODE_FMT_FIXED_DEFERRED: 16971 default: 16972 sense_key = esp->es_key; 16973 asc = esp->es_add_code; 16974 ascq = esp->es_qual_code; 16975 break; 16976 } 16977 16978 switch (sense_key) { 16979 case KEY_NO_SENSE: 16980 sd_sense_key_no_sense(un, bp, xp, pktp); 16981 break; 16982 case KEY_RECOVERABLE_ERROR: 16983 sd_sense_key_recoverable_error(un, asc, bp, xp, pktp); 16984 break; 16985 case KEY_NOT_READY: 16986 sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp); 16987 break; 16988 case KEY_MEDIUM_ERROR: 16989 case KEY_HARDWARE_ERROR: 16990 sd_sense_key_medium_or_hardware_error(un, 16991 sense_key, asc, bp, xp, pktp); 16992 break; 16993 case KEY_ILLEGAL_REQUEST: 16994 sd_sense_key_illegal_request(un, bp, xp, pktp); 16995 break; 16996 case KEY_UNIT_ATTENTION: 16997 sd_sense_key_unit_attention(un, asc, bp, xp, pktp); 16998 break; 16999 case KEY_WRITE_PROTECT: 17000 case KEY_VOLUME_OVERFLOW: 17001 case KEY_MISCOMPARE: 17002 sd_sense_key_fail_command(un, bp, xp, pktp); 17003 break; 17004 case KEY_BLANK_CHECK: 17005 sd_sense_key_blank_check(un, bp, xp, pktp); 17006 break; 17007 case KEY_ABORTED_COMMAND: 17008 sd_sense_key_aborted_command(un, bp, xp, pktp); 17009 break; 17010 case KEY_VENDOR_UNIQUE: 17011 case KEY_COPY_ABORTED: 17012 case KEY_EQUAL: 17013 case KEY_RESERVED: 17014 default: 17015 sd_sense_key_default(un, sense_key, bp, xp, pktp); 17016 break; 17017 } 17018 } 17019 17020 17021 /* 17022 * Function: sd_dump_memory 17023 * 17024 * Description: Debug logging routine to print the contents of a user provided 17025 * buffer. The output of the buffer is broken up into 256 byte 17026 * segments due to a size constraint of the scsi_log. 17027 * implementation. 17028 * 17029 * Arguments: un - ptr to softstate 17030 * comp - component mask 17031 * title - "title" string to preceed data when printed 17032 * data - ptr to data block to be printed 17033 * len - size of data block to be printed 17034 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17035 * 17036 * Context: May be called from interrupt context 17037 */ 17038 17039 #define SD_DUMP_MEMORY_BUF_SIZE 256 17040 17041 static char *sd_dump_format_string[] = { 17042 " 0x%02x", 17043 " %c" 17044 }; 17045 17046 static void 17047 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17048 int len, int fmt) 17049 { 17050 int i, j; 17051 int avail_count; 17052 int start_offset; 17053 int end_offset; 17054 size_t entry_len; 17055 char *bufp; 17056 char *local_buf; 17057 char *format_string; 17058 17059 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17060 17061 /* 17062 * In the debug version of the driver, this function is called from a 17063 * number of places which are NOPs in the release driver. 17064 * The debug driver therefore has additional methods of filtering 17065 * debug output. 17066 */ 17067 #ifdef SDDEBUG 17068 /* 17069 * In the debug version of the driver we can reduce the amount of debug 17070 * messages by setting sd_error_level to something other than 17071 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17072 * sd_component_mask. 17073 */ 17074 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17075 (sd_error_level != SCSI_ERR_ALL)) { 17076 return; 17077 } 17078 if (((sd_component_mask & comp) == 0) || 17079 (sd_error_level != SCSI_ERR_ALL)) { 17080 return; 17081 } 17082 #else 17083 if (sd_error_level != SCSI_ERR_ALL) { 17084 return; 17085 } 17086 #endif 17087 17088 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17089 bufp = local_buf; 17090 /* 17091 * Available length is the length of local_buf[], minus the 17092 * length of the title string, minus one for the ":", minus 17093 * one for the newline, minus one for the NULL terminator. 17094 * This gives the #bytes available for holding the printed 17095 * values from the given data buffer. 17096 */ 17097 if (fmt == SD_LOG_HEX) { 17098 format_string = sd_dump_format_string[0]; 17099 } else /* SD_LOG_CHAR */ { 17100 format_string = sd_dump_format_string[1]; 17101 } 17102 /* 17103 * Available count is the number of elements from the given 17104 * data buffer that we can fit into the available length. 17105 * This is based upon the size of the format string used. 17106 * Make one entry and find it's size. 17107 */ 17108 (void) sprintf(bufp, format_string, data[0]); 17109 entry_len = strlen(bufp); 17110 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17111 17112 j = 0; 17113 while (j < len) { 17114 bufp = local_buf; 17115 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17116 start_offset = j; 17117 17118 end_offset = start_offset + avail_count; 17119 17120 (void) sprintf(bufp, "%s:", title); 17121 bufp += strlen(bufp); 17122 for (i = start_offset; ((i < end_offset) && (j < len)); 17123 i++, j++) { 17124 (void) sprintf(bufp, format_string, data[i]); 17125 bufp += entry_len; 17126 } 17127 (void) sprintf(bufp, "\n"); 17128 17129 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17130 } 17131 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17132 } 17133 17134 /* 17135 * Function: sd_print_sense_msg 17136 * 17137 * Description: Log a message based upon the given sense data. 17138 * 17139 * Arguments: un - ptr to associated softstate 17140 * bp - ptr to buf(9S) for the command 17141 * arg - ptr to associate sd_sense_info struct 17142 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17143 * or SD_NO_RETRY_ISSUED 17144 * 17145 * Context: May be called from interrupt context 17146 */ 17147 17148 static void 17149 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17150 { 17151 struct sd_xbuf *xp; 17152 struct scsi_pkt *pktp; 17153 struct scsi_extended_sense *sensep; 17154 daddr_t request_blkno; 17155 diskaddr_t err_blkno; 17156 int severity; 17157 int pfa_flag; 17158 int fixed_format = TRUE; 17159 extern struct scsi_key_strings scsi_cmds[]; 17160 17161 ASSERT(un != NULL); 17162 ASSERT(mutex_owned(SD_MUTEX(un))); 17163 ASSERT(bp != NULL); 17164 xp = SD_GET_XBUF(bp); 17165 ASSERT(xp != NULL); 17166 pktp = SD_GET_PKTP(bp); 17167 ASSERT(pktp != NULL); 17168 ASSERT(arg != NULL); 17169 17170 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17171 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17172 17173 if ((code == SD_DELAYED_RETRY_ISSUED) || 17174 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17175 severity = SCSI_ERR_RETRYABLE; 17176 } 17177 17178 /* Use absolute block number for the request block number */ 17179 request_blkno = xp->xb_blkno; 17180 17181 /* 17182 * Now try to get the error block number from the sense data 17183 */ 17184 sensep = (struct scsi_extended_sense *)xp->xb_sense_data; 17185 switch (sensep->es_code) { 17186 case CODE_FMT_DESCR_CURRENT: 17187 case CODE_FMT_DESCR_DEFERRED: 17188 err_blkno = 17189 sd_extract_sense_info_descr( 17190 (struct scsi_descr_sense_hdr *)sensep); 17191 fixed_format = FALSE; 17192 break; 17193 case CODE_FMT_FIXED_CURRENT: 17194 case CODE_FMT_FIXED_DEFERRED: 17195 case CODE_FMT_VENDOR_SPECIFIC: 17196 default: 17197 /* 17198 * With the es_valid bit set, we assume that the error 17199 * blkno is in the sense data. Also, if xp->xb_blkno is 17200 * greater than 0xffffffff then the target *should* have used 17201 * a descriptor sense format (or it shouldn't have set 17202 * the es_valid bit), and we may as well ignore the 17203 * 32-bit value. 17204 */ 17205 if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) { 17206 err_blkno = (diskaddr_t) 17207 ((sensep->es_info_1 << 24) | 17208 (sensep->es_info_2 << 16) | 17209 (sensep->es_info_3 << 8) | 17210 (sensep->es_info_4)); 17211 } else { 17212 err_blkno = (diskaddr_t)-1; 17213 } 17214 break; 17215 } 17216 17217 if (err_blkno == (diskaddr_t)-1) { 17218 /* 17219 * Without the es_valid bit set (for fixed format) or an 17220 * information descriptor (for descriptor format) we cannot 17221 * be certain of the error blkno, so just use the 17222 * request_blkno. 17223 */ 17224 err_blkno = (diskaddr_t)request_blkno; 17225 } else { 17226 /* 17227 * We retrieved the error block number from the information 17228 * portion of the sense data. 17229 * 17230 * For USCSI commands we are better off using the error 17231 * block no. as the requested block no. (This is the best 17232 * we can estimate.) 17233 */ 17234 if ((SD_IS_BUFIO(xp) == FALSE) && 17235 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17236 request_blkno = err_blkno; 17237 } 17238 } 17239 17240 /* 17241 * The following will log the buffer contents for the release driver 17242 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17243 * level is set to verbose. 17244 */ 17245 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17246 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17247 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17248 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17249 17250 if (pfa_flag == FALSE) { 17251 /* This is normally only set for USCSI */ 17252 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17253 return; 17254 } 17255 17256 if ((SD_IS_BUFIO(xp) == TRUE) && 17257 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17258 (severity < sd_error_level))) { 17259 return; 17260 } 17261 } 17262 17263 /* 17264 * If the data is fixed format then check for Sonoma Failover, 17265 * and keep a count of how many failed I/O's. We should not have 17266 * to worry about Sonoma returning descriptor format sense data, 17267 * and asc/ascq are in a different location in descriptor format. 17268 */ 17269 if (fixed_format && 17270 (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) && 17271 (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) { 17272 un->un_sonoma_failure_count++; 17273 if (un->un_sonoma_failure_count > 1) { 17274 return; 17275 } 17276 } 17277 17278 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17279 request_blkno, err_blkno, scsi_cmds, sensep, 17280 un->un_additional_codes, NULL); 17281 } 17282 17283 /* 17284 * Function: sd_extract_sense_info_descr 17285 * 17286 * Description: Retrieve "information" field from descriptor format 17287 * sense data. Iterates through each sense descriptor 17288 * looking for the information descriptor and returns 17289 * the information field from that descriptor. 17290 * 17291 * Context: May be called from interrupt context 17292 */ 17293 17294 static diskaddr_t 17295 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp) 17296 { 17297 diskaddr_t result; 17298 uint8_t *descr_offset; 17299 int valid_sense_length; 17300 struct scsi_information_sense_descr *isd; 17301 17302 /* 17303 * Initialize result to -1 indicating there is no information 17304 * descriptor 17305 */ 17306 result = (diskaddr_t)-1; 17307 17308 /* 17309 * The first descriptor will immediately follow the header 17310 */ 17311 descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */ 17312 17313 /* 17314 * Calculate the amount of valid sense data 17315 */ 17316 valid_sense_length = 17317 min((sizeof (struct scsi_descr_sense_hdr) + 17318 sdsp->ds_addl_sense_length), 17319 SENSE_LENGTH); 17320 17321 /* 17322 * Iterate through the list of descriptors, stopping when we 17323 * run out of sense data 17324 */ 17325 while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <= 17326 (uint8_t *)sdsp + valid_sense_length) { 17327 /* 17328 * Check if this is an information descriptor. We can 17329 * use the scsi_information_sense_descr structure as a 17330 * template sense the first two fields are always the 17331 * same 17332 */ 17333 isd = (struct scsi_information_sense_descr *)descr_offset; 17334 if (isd->isd_descr_type == DESCR_INFORMATION) { 17335 /* 17336 * Found an information descriptor. Copy the 17337 * information field. There will only be one 17338 * information descriptor so we can stop looking. 17339 */ 17340 result = 17341 (((diskaddr_t)isd->isd_information[0] << 56) | 17342 ((diskaddr_t)isd->isd_information[1] << 48) | 17343 ((diskaddr_t)isd->isd_information[2] << 40) | 17344 ((diskaddr_t)isd->isd_information[3] << 32) | 17345 ((diskaddr_t)isd->isd_information[4] << 24) | 17346 ((diskaddr_t)isd->isd_information[5] << 16) | 17347 ((diskaddr_t)isd->isd_information[6] << 8) | 17348 ((diskaddr_t)isd->isd_information[7])); 17349 break; 17350 } 17351 17352 /* 17353 * Get pointer to the next descriptor. The "additional 17354 * length" field holds the length of the descriptor except 17355 * for the "type" and "additional length" fields, so 17356 * we need to add 2 to get the total length. 17357 */ 17358 descr_offset += (isd->isd_addl_length + 2); 17359 } 17360 17361 return (result); 17362 } 17363 17364 /* 17365 * Function: sd_sense_key_no_sense 17366 * 17367 * Description: Recovery action when sense data was not received. 17368 * 17369 * Context: May be called from interrupt context 17370 */ 17371 17372 static void 17373 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17374 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17375 { 17376 struct sd_sense_info si; 17377 17378 ASSERT(un != NULL); 17379 ASSERT(mutex_owned(SD_MUTEX(un))); 17380 ASSERT(bp != NULL); 17381 ASSERT(xp != NULL); 17382 ASSERT(pktp != NULL); 17383 17384 si.ssi_severity = SCSI_ERR_FATAL; 17385 si.ssi_pfa_flag = FALSE; 17386 17387 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17388 17389 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17390 &si, EIO, (clock_t)0, NULL); 17391 } 17392 17393 17394 /* 17395 * Function: sd_sense_key_recoverable_error 17396 * 17397 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17398 * 17399 * Context: May be called from interrupt context 17400 */ 17401 17402 static void 17403 sd_sense_key_recoverable_error(struct sd_lun *un, 17404 uint8_t asc, 17405 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17406 { 17407 struct sd_sense_info si; 17408 17409 ASSERT(un != NULL); 17410 ASSERT(mutex_owned(SD_MUTEX(un))); 17411 ASSERT(bp != NULL); 17412 ASSERT(xp != NULL); 17413 ASSERT(pktp != NULL); 17414 17415 /* 17416 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17417 */ 17418 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17419 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17420 si.ssi_severity = SCSI_ERR_INFO; 17421 si.ssi_pfa_flag = TRUE; 17422 } else { 17423 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17424 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17425 si.ssi_severity = SCSI_ERR_RECOVERED; 17426 si.ssi_pfa_flag = FALSE; 17427 } 17428 17429 if (pktp->pkt_resid == 0) { 17430 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17431 sd_return_command(un, bp); 17432 return; 17433 } 17434 17435 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17436 &si, EIO, (clock_t)0, NULL); 17437 } 17438 17439 17440 17441 17442 /* 17443 * Function: sd_sense_key_not_ready 17444 * 17445 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17446 * 17447 * Context: May be called from interrupt context 17448 */ 17449 17450 static void 17451 sd_sense_key_not_ready(struct sd_lun *un, 17452 uint8_t asc, uint8_t ascq, 17453 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17454 { 17455 struct sd_sense_info si; 17456 17457 ASSERT(un != NULL); 17458 ASSERT(mutex_owned(SD_MUTEX(un))); 17459 ASSERT(bp != NULL); 17460 ASSERT(xp != NULL); 17461 ASSERT(pktp != NULL); 17462 17463 si.ssi_severity = SCSI_ERR_FATAL; 17464 si.ssi_pfa_flag = FALSE; 17465 17466 /* 17467 * Update error stats after first NOT READY error. Disks may have 17468 * been powered down and may need to be restarted. For CDROMs, 17469 * report NOT READY errors only if media is present. 17470 */ 17471 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17472 (xp->xb_retry_count > 0)) { 17473 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17474 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17475 } 17476 17477 /* 17478 * Just fail if the "not ready" retry limit has been reached. 17479 */ 17480 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17481 /* Special check for error message printing for removables. */ 17482 if (un->un_f_has_removable_media && (asc == 0x04) && 17483 (ascq >= 0x04)) { 17484 si.ssi_severity = SCSI_ERR_ALL; 17485 } 17486 goto fail_command; 17487 } 17488 17489 /* 17490 * Check the ASC and ASCQ in the sense data as needed, to determine 17491 * what to do. 17492 */ 17493 switch (asc) { 17494 case 0x04: /* LOGICAL UNIT NOT READY */ 17495 /* 17496 * disk drives that don't spin up result in a very long delay 17497 * in format without warning messages. We will log a message 17498 * if the error level is set to verbose. 17499 */ 17500 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17501 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17502 "logical unit not ready, resetting disk\n"); 17503 } 17504 17505 /* 17506 * There are different requirements for CDROMs and disks for 17507 * the number of retries. If a CD-ROM is giving this, it is 17508 * probably reading TOC and is in the process of getting 17509 * ready, so we should keep on trying for a long time to make 17510 * sure that all types of media are taken in account (for 17511 * some media the drive takes a long time to read TOC). For 17512 * disks we do not want to retry this too many times as this 17513 * can cause a long hang in format when the drive refuses to 17514 * spin up (a very common failure). 17515 */ 17516 switch (ascq) { 17517 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17518 /* 17519 * Disk drives frequently refuse to spin up which 17520 * results in a very long hang in format without 17521 * warning messages. 17522 * 17523 * Note: This code preserves the legacy behavior of 17524 * comparing xb_retry_count against zero for fibre 17525 * channel targets instead of comparing against the 17526 * un_reset_retry_count value. The reason for this 17527 * discrepancy has been so utterly lost beneath the 17528 * Sands of Time that even Indiana Jones could not 17529 * find it. 17530 */ 17531 if (un->un_f_is_fibre == TRUE) { 17532 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17533 (xp->xb_retry_count > 0)) && 17534 (un->un_startstop_timeid == NULL)) { 17535 scsi_log(SD_DEVINFO(un), sd_label, 17536 CE_WARN, "logical unit not ready, " 17537 "resetting disk\n"); 17538 sd_reset_target(un, pktp); 17539 } 17540 } else { 17541 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17542 (xp->xb_retry_count > 17543 un->un_reset_retry_count)) && 17544 (un->un_startstop_timeid == NULL)) { 17545 scsi_log(SD_DEVINFO(un), sd_label, 17546 CE_WARN, "logical unit not ready, " 17547 "resetting disk\n"); 17548 sd_reset_target(un, pktp); 17549 } 17550 } 17551 break; 17552 17553 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17554 /* 17555 * If the target is in the process of becoming 17556 * ready, just proceed with the retry. This can 17557 * happen with CD-ROMs that take a long time to 17558 * read TOC after a power cycle or reset. 17559 */ 17560 goto do_retry; 17561 17562 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17563 break; 17564 17565 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17566 /* 17567 * Retries cannot help here so just fail right away. 17568 */ 17569 goto fail_command; 17570 17571 case 0x88: 17572 /* 17573 * Vendor-unique code for T3/T4: it indicates a 17574 * path problem in a mutipathed config, but as far as 17575 * the target driver is concerned it equates to a fatal 17576 * error, so we should just fail the command right away 17577 * (without printing anything to the console). If this 17578 * is not a T3/T4, fall thru to the default recovery 17579 * action. 17580 * T3/T4 is FC only, don't need to check is_fibre 17581 */ 17582 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17583 sd_return_failed_command(un, bp, EIO); 17584 return; 17585 } 17586 /* FALLTHRU */ 17587 17588 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17589 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17590 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17591 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17592 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17593 default: /* Possible future codes in SCSI spec? */ 17594 /* 17595 * For removable-media devices, do not retry if 17596 * ASCQ > 2 as these result mostly from USCSI commands 17597 * on MMC devices issued to check status of an 17598 * operation initiated in immediate mode. Also for 17599 * ASCQ >= 4 do not print console messages as these 17600 * mainly represent a user-initiated operation 17601 * instead of a system failure. 17602 */ 17603 if (un->un_f_has_removable_media) { 17604 si.ssi_severity = SCSI_ERR_ALL; 17605 goto fail_command; 17606 } 17607 break; 17608 } 17609 17610 /* 17611 * As part of our recovery attempt for the NOT READY 17612 * condition, we issue a START STOP UNIT command. However 17613 * we want to wait for a short delay before attempting this 17614 * as there may still be more commands coming back from the 17615 * target with the check condition. To do this we use 17616 * timeout(9F) to call sd_start_stop_unit_callback() after 17617 * the delay interval expires. (sd_start_stop_unit_callback() 17618 * dispatches sd_start_stop_unit_task(), which will issue 17619 * the actual START STOP UNIT command. The delay interval 17620 * is one-half of the delay that we will use to retry the 17621 * command that generated the NOT READY condition. 17622 * 17623 * Note that we could just dispatch sd_start_stop_unit_task() 17624 * from here and allow it to sleep for the delay interval, 17625 * but then we would be tying up the taskq thread 17626 * uncesessarily for the duration of the delay. 17627 * 17628 * Do not issue the START STOP UNIT if the current command 17629 * is already a START STOP UNIT. 17630 */ 17631 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17632 break; 17633 } 17634 17635 /* 17636 * Do not schedule the timeout if one is already pending. 17637 */ 17638 if (un->un_startstop_timeid != NULL) { 17639 SD_INFO(SD_LOG_ERROR, un, 17640 "sd_sense_key_not_ready: restart already issued to" 17641 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17642 ddi_get_instance(SD_DEVINFO(un))); 17643 break; 17644 } 17645 17646 /* 17647 * Schedule the START STOP UNIT command, then queue the command 17648 * for a retry. 17649 * 17650 * Note: A timeout is not scheduled for this retry because we 17651 * want the retry to be serial with the START_STOP_UNIT. The 17652 * retry will be started when the START_STOP_UNIT is completed 17653 * in sd_start_stop_unit_task. 17654 */ 17655 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17656 un, SD_BSY_TIMEOUT / 2); 17657 xp->xb_retry_count++; 17658 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17659 return; 17660 17661 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17662 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17664 "unit does not respond to selection\n"); 17665 } 17666 break; 17667 17668 case 0x3A: /* MEDIUM NOT PRESENT */ 17669 if (sd_error_level >= SCSI_ERR_FATAL) { 17670 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17671 "Caddy not inserted in drive\n"); 17672 } 17673 17674 sr_ejected(un); 17675 un->un_mediastate = DKIO_EJECTED; 17676 /* The state has changed, inform the media watch routines */ 17677 cv_broadcast(&un->un_state_cv); 17678 /* Just fail if no media is present in the drive. */ 17679 goto fail_command; 17680 17681 default: 17682 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17683 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17684 "Unit not Ready. Additional sense code 0x%x\n", 17685 asc); 17686 } 17687 break; 17688 } 17689 17690 do_retry: 17691 17692 /* 17693 * Retry the command, as some targets may report NOT READY for 17694 * several seconds after being reset. 17695 */ 17696 xp->xb_retry_count++; 17697 si.ssi_severity = SCSI_ERR_RETRYABLE; 17698 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17699 &si, EIO, SD_BSY_TIMEOUT, NULL); 17700 17701 return; 17702 17703 fail_command: 17704 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17705 sd_return_failed_command(un, bp, EIO); 17706 } 17707 17708 17709 17710 /* 17711 * Function: sd_sense_key_medium_or_hardware_error 17712 * 17713 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17714 * sense key. 17715 * 17716 * Context: May be called from interrupt context 17717 */ 17718 17719 static void 17720 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17721 int sense_key, uint8_t asc, 17722 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17723 { 17724 struct sd_sense_info si; 17725 17726 ASSERT(un != NULL); 17727 ASSERT(mutex_owned(SD_MUTEX(un))); 17728 ASSERT(bp != NULL); 17729 ASSERT(xp != NULL); 17730 ASSERT(pktp != NULL); 17731 17732 si.ssi_severity = SCSI_ERR_FATAL; 17733 si.ssi_pfa_flag = FALSE; 17734 17735 if (sense_key == KEY_MEDIUM_ERROR) { 17736 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17737 } 17738 17739 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17740 17741 if ((un->un_reset_retry_count != 0) && 17742 (xp->xb_retry_count == un->un_reset_retry_count)) { 17743 mutex_exit(SD_MUTEX(un)); 17744 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17745 if (un->un_f_allow_bus_device_reset == TRUE) { 17746 17747 boolean_t try_resetting_target = B_TRUE; 17748 17749 /* 17750 * We need to be able to handle specific ASC when we are 17751 * handling a KEY_HARDWARE_ERROR. In particular 17752 * taking the default action of resetting the target may 17753 * not be the appropriate way to attempt recovery. 17754 * Resetting a target because of a single LUN failure 17755 * victimizes all LUNs on that target. 17756 * 17757 * This is true for the LSI arrays, if an LSI 17758 * array controller returns an ASC of 0x84 (LUN Dead) we 17759 * should trust it. 17760 */ 17761 17762 if (sense_key == KEY_HARDWARE_ERROR) { 17763 switch (asc) { 17764 case 0x84: 17765 if (SD_IS_LSI(un)) { 17766 try_resetting_target = B_FALSE; 17767 } 17768 break; 17769 default: 17770 break; 17771 } 17772 } 17773 17774 if (try_resetting_target == B_TRUE) { 17775 int reset_retval = 0; 17776 if (un->un_f_lun_reset_enabled == TRUE) { 17777 SD_TRACE(SD_LOG_IO_CORE, un, 17778 "sd_sense_key_medium_or_hardware_" 17779 "error: issuing RESET_LUN\n"); 17780 reset_retval = 17781 scsi_reset(SD_ADDRESS(un), 17782 RESET_LUN); 17783 } 17784 if (reset_retval == 0) { 17785 SD_TRACE(SD_LOG_IO_CORE, un, 17786 "sd_sense_key_medium_or_hardware_" 17787 "error: issuing RESET_TARGET\n"); 17788 (void) scsi_reset(SD_ADDRESS(un), 17789 RESET_TARGET); 17790 } 17791 } 17792 } 17793 mutex_enter(SD_MUTEX(un)); 17794 } 17795 17796 /* 17797 * This really ought to be a fatal error, but we will retry anyway 17798 * as some drives report this as a spurious error. 17799 */ 17800 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17801 &si, EIO, (clock_t)0, NULL); 17802 } 17803 17804 17805 17806 /* 17807 * Function: sd_sense_key_illegal_request 17808 * 17809 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17810 * 17811 * Context: May be called from interrupt context 17812 */ 17813 17814 static void 17815 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17816 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17817 { 17818 struct sd_sense_info si; 17819 17820 ASSERT(un != NULL); 17821 ASSERT(mutex_owned(SD_MUTEX(un))); 17822 ASSERT(bp != NULL); 17823 ASSERT(xp != NULL); 17824 ASSERT(pktp != NULL); 17825 17826 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17827 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17828 17829 si.ssi_severity = SCSI_ERR_INFO; 17830 si.ssi_pfa_flag = FALSE; 17831 17832 /* Pointless to retry if the target thinks it's an illegal request */ 17833 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17834 sd_return_failed_command(un, bp, EIO); 17835 } 17836 17837 17838 17839 17840 /* 17841 * Function: sd_sense_key_unit_attention 17842 * 17843 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17844 * 17845 * Context: May be called from interrupt context 17846 */ 17847 17848 static void 17849 sd_sense_key_unit_attention(struct sd_lun *un, 17850 uint8_t asc, 17851 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17852 { 17853 /* 17854 * For UNIT ATTENTION we allow retries for one minute. Devices 17855 * like Sonoma can return UNIT ATTENTION close to a minute 17856 * under certain conditions. 17857 */ 17858 int retry_check_flag = SD_RETRIES_UA; 17859 boolean_t kstat_updated = B_FALSE; 17860 struct sd_sense_info si; 17861 17862 ASSERT(un != NULL); 17863 ASSERT(mutex_owned(SD_MUTEX(un))); 17864 ASSERT(bp != NULL); 17865 ASSERT(xp != NULL); 17866 ASSERT(pktp != NULL); 17867 17868 si.ssi_severity = SCSI_ERR_INFO; 17869 si.ssi_pfa_flag = FALSE; 17870 17871 17872 switch (asc) { 17873 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17874 if (sd_report_pfa != 0) { 17875 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17876 si.ssi_pfa_flag = TRUE; 17877 retry_check_flag = SD_RETRIES_STANDARD; 17878 goto do_retry; 17879 } 17880 break; 17881 17882 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17883 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17884 un->un_resvd_status |= 17885 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17886 } 17887 /* FALLTHRU */ 17888 17889 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17890 if (!un->un_f_has_removable_media) { 17891 break; 17892 } 17893 17894 /* 17895 * When we get a unit attention from a removable-media device, 17896 * it may be in a state that will take a long time to recover 17897 * (e.g., from a reset). Since we are executing in interrupt 17898 * context here, we cannot wait around for the device to come 17899 * back. So hand this command off to sd_media_change_task() 17900 * for deferred processing under taskq thread context. (Note 17901 * that the command still may be failed if a problem is 17902 * encountered at a later time.) 17903 */ 17904 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17905 KM_NOSLEEP) == 0) { 17906 /* 17907 * Cannot dispatch the request so fail the command. 17908 */ 17909 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17910 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17911 si.ssi_severity = SCSI_ERR_FATAL; 17912 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17913 sd_return_failed_command(un, bp, EIO); 17914 } 17915 17916 /* 17917 * If failed to dispatch sd_media_change_task(), we already 17918 * updated kstat. If succeed to dispatch sd_media_change_task(), 17919 * we should update kstat later if it encounters an error. So, 17920 * we update kstat_updated flag here. 17921 */ 17922 kstat_updated = B_TRUE; 17923 17924 /* 17925 * Either the command has been successfully dispatched to a 17926 * task Q for retrying, or the dispatch failed. In either case 17927 * do NOT retry again by calling sd_retry_command. This sets up 17928 * two retries of the same command and when one completes and 17929 * frees the resources the other will access freed memory, 17930 * a bad thing. 17931 */ 17932 return; 17933 17934 default: 17935 break; 17936 } 17937 17938 /* 17939 * Update kstat if we haven't done that. 17940 */ 17941 if (!kstat_updated) { 17942 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17943 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17944 } 17945 17946 do_retry: 17947 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17948 EIO, SD_UA_RETRY_DELAY, NULL); 17949 } 17950 17951 17952 17953 /* 17954 * Function: sd_sense_key_fail_command 17955 * 17956 * Description: Use to fail a command when we don't like the sense key that 17957 * was returned. 17958 * 17959 * Context: May be called from interrupt context 17960 */ 17961 17962 static void 17963 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17964 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17965 { 17966 struct sd_sense_info si; 17967 17968 ASSERT(un != NULL); 17969 ASSERT(mutex_owned(SD_MUTEX(un))); 17970 ASSERT(bp != NULL); 17971 ASSERT(xp != NULL); 17972 ASSERT(pktp != NULL); 17973 17974 si.ssi_severity = SCSI_ERR_FATAL; 17975 si.ssi_pfa_flag = FALSE; 17976 17977 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17978 sd_return_failed_command(un, bp, EIO); 17979 } 17980 17981 17982 17983 /* 17984 * Function: sd_sense_key_blank_check 17985 * 17986 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17987 * Has no monetary connotation. 17988 * 17989 * Context: May be called from interrupt context 17990 */ 17991 17992 static void 17993 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17994 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17995 { 17996 struct sd_sense_info si; 17997 17998 ASSERT(un != NULL); 17999 ASSERT(mutex_owned(SD_MUTEX(un))); 18000 ASSERT(bp != NULL); 18001 ASSERT(xp != NULL); 18002 ASSERT(pktp != NULL); 18003 18004 /* 18005 * Blank check is not fatal for removable devices, therefore 18006 * it does not require a console message. 18007 */ 18008 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18009 SCSI_ERR_FATAL; 18010 si.ssi_pfa_flag = FALSE; 18011 18012 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18013 sd_return_failed_command(un, bp, EIO); 18014 } 18015 18016 18017 18018 18019 /* 18020 * Function: sd_sense_key_aborted_command 18021 * 18022 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18023 * 18024 * Context: May be called from interrupt context 18025 */ 18026 18027 static void 18028 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18029 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18030 { 18031 struct sd_sense_info si; 18032 18033 ASSERT(un != NULL); 18034 ASSERT(mutex_owned(SD_MUTEX(un))); 18035 ASSERT(bp != NULL); 18036 ASSERT(xp != NULL); 18037 ASSERT(pktp != NULL); 18038 18039 si.ssi_severity = SCSI_ERR_FATAL; 18040 si.ssi_pfa_flag = FALSE; 18041 18042 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18043 18044 /* 18045 * This really ought to be a fatal error, but we will retry anyway 18046 * as some drives report this as a spurious error. 18047 */ 18048 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18049 &si, EIO, (clock_t)0, NULL); 18050 } 18051 18052 18053 18054 /* 18055 * Function: sd_sense_key_default 18056 * 18057 * Description: Default recovery action for several SCSI sense keys (basically 18058 * attempts a retry). 18059 * 18060 * Context: May be called from interrupt context 18061 */ 18062 18063 static void 18064 sd_sense_key_default(struct sd_lun *un, 18065 int sense_key, 18066 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18067 { 18068 struct sd_sense_info si; 18069 18070 ASSERT(un != NULL); 18071 ASSERT(mutex_owned(SD_MUTEX(un))); 18072 ASSERT(bp != NULL); 18073 ASSERT(xp != NULL); 18074 ASSERT(pktp != NULL); 18075 18076 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18077 18078 /* 18079 * Undecoded sense key. Attempt retries and hope that will fix 18080 * the problem. Otherwise, we're dead. 18081 */ 18082 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18083 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18084 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18085 } 18086 18087 si.ssi_severity = SCSI_ERR_FATAL; 18088 si.ssi_pfa_flag = FALSE; 18089 18090 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18091 &si, EIO, (clock_t)0, NULL); 18092 } 18093 18094 18095 18096 /* 18097 * Function: sd_print_retry_msg 18098 * 18099 * Description: Print a message indicating the retry action being taken. 18100 * 18101 * Arguments: un - ptr to associated softstate 18102 * bp - ptr to buf(9S) for the command 18103 * arg - not used. 18104 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18105 * or SD_NO_RETRY_ISSUED 18106 * 18107 * Context: May be called from interrupt context 18108 */ 18109 /* ARGSUSED */ 18110 static void 18111 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18112 { 18113 struct sd_xbuf *xp; 18114 struct scsi_pkt *pktp; 18115 char *reasonp; 18116 char *msgp; 18117 18118 ASSERT(un != NULL); 18119 ASSERT(mutex_owned(SD_MUTEX(un))); 18120 ASSERT(bp != NULL); 18121 pktp = SD_GET_PKTP(bp); 18122 ASSERT(pktp != NULL); 18123 xp = SD_GET_XBUF(bp); 18124 ASSERT(xp != NULL); 18125 18126 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18127 mutex_enter(&un->un_pm_mutex); 18128 if ((un->un_state == SD_STATE_SUSPENDED) || 18129 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18130 (pktp->pkt_flags & FLAG_SILENT)) { 18131 mutex_exit(&un->un_pm_mutex); 18132 goto update_pkt_reason; 18133 } 18134 mutex_exit(&un->un_pm_mutex); 18135 18136 /* 18137 * Suppress messages if they are all the same pkt_reason; with 18138 * TQ, many (up to 256) are returned with the same pkt_reason. 18139 * If we are in panic, then suppress the retry messages. 18140 */ 18141 switch (flag) { 18142 case SD_NO_RETRY_ISSUED: 18143 msgp = "giving up"; 18144 break; 18145 case SD_IMMEDIATE_RETRY_ISSUED: 18146 case SD_DELAYED_RETRY_ISSUED: 18147 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18148 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18149 (sd_error_level != SCSI_ERR_ALL))) { 18150 return; 18151 } 18152 msgp = "retrying command"; 18153 break; 18154 default: 18155 goto update_pkt_reason; 18156 } 18157 18158 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18159 scsi_rname(pktp->pkt_reason)); 18160 18161 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18162 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18163 18164 update_pkt_reason: 18165 /* 18166 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18167 * This is to prevent multiple console messages for the same failure 18168 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18169 * when the command is retried successfully because there still may be 18170 * more commands coming back with the same value of pktp->pkt_reason. 18171 */ 18172 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18173 un->un_last_pkt_reason = pktp->pkt_reason; 18174 } 18175 } 18176 18177 18178 /* 18179 * Function: sd_print_cmd_incomplete_msg 18180 * 18181 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18182 * 18183 * Arguments: un - ptr to associated softstate 18184 * bp - ptr to buf(9S) for the command 18185 * arg - passed to sd_print_retry_msg() 18186 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18187 * or SD_NO_RETRY_ISSUED 18188 * 18189 * Context: May be called from interrupt context 18190 */ 18191 18192 static void 18193 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18194 int code) 18195 { 18196 dev_info_t *dip; 18197 18198 ASSERT(un != NULL); 18199 ASSERT(mutex_owned(SD_MUTEX(un))); 18200 ASSERT(bp != NULL); 18201 18202 switch (code) { 18203 case SD_NO_RETRY_ISSUED: 18204 /* Command was failed. Someone turned off this target? */ 18205 if (un->un_state != SD_STATE_OFFLINE) { 18206 /* 18207 * Suppress message if we are detaching and 18208 * device has been disconnected 18209 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18210 * private interface and not part of the DDI 18211 */ 18212 dip = un->un_sd->sd_dev; 18213 if (!(DEVI_IS_DETACHING(dip) && 18214 DEVI_IS_DEVICE_REMOVED(dip))) { 18215 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18216 "disk not responding to selection\n"); 18217 } 18218 New_state(un, SD_STATE_OFFLINE); 18219 } 18220 break; 18221 18222 case SD_DELAYED_RETRY_ISSUED: 18223 case SD_IMMEDIATE_RETRY_ISSUED: 18224 default: 18225 /* Command was successfully queued for retry */ 18226 sd_print_retry_msg(un, bp, arg, code); 18227 break; 18228 } 18229 } 18230 18231 18232 /* 18233 * Function: sd_pkt_reason_cmd_incomplete 18234 * 18235 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18236 * 18237 * Context: May be called from interrupt context 18238 */ 18239 18240 static void 18241 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18242 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18243 { 18244 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18245 18246 ASSERT(un != NULL); 18247 ASSERT(mutex_owned(SD_MUTEX(un))); 18248 ASSERT(bp != NULL); 18249 ASSERT(xp != NULL); 18250 ASSERT(pktp != NULL); 18251 18252 /* Do not do a reset if selection did not complete */ 18253 /* Note: Should this not just check the bit? */ 18254 if (pktp->pkt_state != STATE_GOT_BUS) { 18255 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18256 sd_reset_target(un, pktp); 18257 } 18258 18259 /* 18260 * If the target was not successfully selected, then set 18261 * SD_RETRIES_FAILFAST to indicate that we lost communication 18262 * with the target, and further retries and/or commands are 18263 * likely to take a long time. 18264 */ 18265 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18266 flag |= SD_RETRIES_FAILFAST; 18267 } 18268 18269 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18270 18271 sd_retry_command(un, bp, flag, 18272 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18273 } 18274 18275 18276 18277 /* 18278 * Function: sd_pkt_reason_cmd_tran_err 18279 * 18280 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18281 * 18282 * Context: May be called from interrupt context 18283 */ 18284 18285 static void 18286 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18287 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18288 { 18289 ASSERT(un != NULL); 18290 ASSERT(mutex_owned(SD_MUTEX(un))); 18291 ASSERT(bp != NULL); 18292 ASSERT(xp != NULL); 18293 ASSERT(pktp != NULL); 18294 18295 /* 18296 * Do not reset if we got a parity error, or if 18297 * selection did not complete. 18298 */ 18299 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18300 /* Note: Should this not just check the bit for pkt_state? */ 18301 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18302 (pktp->pkt_state != STATE_GOT_BUS)) { 18303 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18304 sd_reset_target(un, pktp); 18305 } 18306 18307 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18308 18309 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18310 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18311 } 18312 18313 18314 18315 /* 18316 * Function: sd_pkt_reason_cmd_reset 18317 * 18318 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18319 * 18320 * Context: May be called from interrupt context 18321 */ 18322 18323 static void 18324 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18325 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18326 { 18327 ASSERT(un != NULL); 18328 ASSERT(mutex_owned(SD_MUTEX(un))); 18329 ASSERT(bp != NULL); 18330 ASSERT(xp != NULL); 18331 ASSERT(pktp != NULL); 18332 18333 /* The target may still be running the command, so try to reset. */ 18334 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18335 sd_reset_target(un, pktp); 18336 18337 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18338 18339 /* 18340 * If pkt_reason is CMD_RESET chances are that this pkt got 18341 * reset because another target on this bus caused it. The target 18342 * that caused it should get CMD_TIMEOUT with pkt_statistics 18343 * of STAT_TIMEOUT/STAT_DEV_RESET. 18344 */ 18345 18346 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18347 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18348 } 18349 18350 18351 18352 18353 /* 18354 * Function: sd_pkt_reason_cmd_aborted 18355 * 18356 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18357 * 18358 * Context: May be called from interrupt context 18359 */ 18360 18361 static void 18362 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18363 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18364 { 18365 ASSERT(un != NULL); 18366 ASSERT(mutex_owned(SD_MUTEX(un))); 18367 ASSERT(bp != NULL); 18368 ASSERT(xp != NULL); 18369 ASSERT(pktp != NULL); 18370 18371 /* The target may still be running the command, so try to reset. */ 18372 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18373 sd_reset_target(un, pktp); 18374 18375 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18376 18377 /* 18378 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18379 * aborted because another target on this bus caused it. The target 18380 * that caused it should get CMD_TIMEOUT with pkt_statistics 18381 * of STAT_TIMEOUT/STAT_DEV_RESET. 18382 */ 18383 18384 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18385 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18386 } 18387 18388 18389 18390 /* 18391 * Function: sd_pkt_reason_cmd_timeout 18392 * 18393 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18394 * 18395 * Context: May be called from interrupt context 18396 */ 18397 18398 static void 18399 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18400 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18401 { 18402 ASSERT(un != NULL); 18403 ASSERT(mutex_owned(SD_MUTEX(un))); 18404 ASSERT(bp != NULL); 18405 ASSERT(xp != NULL); 18406 ASSERT(pktp != NULL); 18407 18408 18409 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18410 sd_reset_target(un, pktp); 18411 18412 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18413 18414 /* 18415 * A command timeout indicates that we could not establish 18416 * communication with the target, so set SD_RETRIES_FAILFAST 18417 * as further retries/commands are likely to take a long time. 18418 */ 18419 sd_retry_command(un, bp, 18420 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18421 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18422 } 18423 18424 18425 18426 /* 18427 * Function: sd_pkt_reason_cmd_unx_bus_free 18428 * 18429 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18430 * 18431 * Context: May be called from interrupt context 18432 */ 18433 18434 static void 18435 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18436 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18437 { 18438 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18439 18440 ASSERT(un != NULL); 18441 ASSERT(mutex_owned(SD_MUTEX(un))); 18442 ASSERT(bp != NULL); 18443 ASSERT(xp != NULL); 18444 ASSERT(pktp != NULL); 18445 18446 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18447 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18448 18449 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18450 sd_print_retry_msg : NULL; 18451 18452 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18453 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18454 } 18455 18456 18457 /* 18458 * Function: sd_pkt_reason_cmd_tag_reject 18459 * 18460 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18461 * 18462 * Context: May be called from interrupt context 18463 */ 18464 18465 static void 18466 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18467 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18468 { 18469 ASSERT(un != NULL); 18470 ASSERT(mutex_owned(SD_MUTEX(un))); 18471 ASSERT(bp != NULL); 18472 ASSERT(xp != NULL); 18473 ASSERT(pktp != NULL); 18474 18475 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18476 pktp->pkt_flags = 0; 18477 un->un_tagflags = 0; 18478 if (un->un_f_opt_queueing == TRUE) { 18479 un->un_throttle = min(un->un_throttle, 3); 18480 } else { 18481 un->un_throttle = 1; 18482 } 18483 mutex_exit(SD_MUTEX(un)); 18484 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18485 mutex_enter(SD_MUTEX(un)); 18486 18487 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18488 18489 /* Legacy behavior not to check retry counts here. */ 18490 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18491 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18492 } 18493 18494 18495 /* 18496 * Function: sd_pkt_reason_default 18497 * 18498 * Description: Default recovery actions for SCSA pkt_reason values that 18499 * do not have more explicit recovery actions. 18500 * 18501 * Context: May be called from interrupt context 18502 */ 18503 18504 static void 18505 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18506 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18507 { 18508 ASSERT(un != NULL); 18509 ASSERT(mutex_owned(SD_MUTEX(un))); 18510 ASSERT(bp != NULL); 18511 ASSERT(xp != NULL); 18512 ASSERT(pktp != NULL); 18513 18514 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18515 sd_reset_target(un, pktp); 18516 18517 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18518 18519 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18520 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18521 } 18522 18523 18524 18525 /* 18526 * Function: sd_pkt_status_check_condition 18527 * 18528 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18529 * 18530 * Context: May be called from interrupt context 18531 */ 18532 18533 static void 18534 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18535 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18536 { 18537 ASSERT(un != NULL); 18538 ASSERT(mutex_owned(SD_MUTEX(un))); 18539 ASSERT(bp != NULL); 18540 ASSERT(xp != NULL); 18541 ASSERT(pktp != NULL); 18542 18543 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18544 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18545 18546 /* 18547 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18548 * command will be retried after the request sense). Otherwise, retry 18549 * the command. Note: we are issuing the request sense even though the 18550 * retry limit may have been reached for the failed command. 18551 */ 18552 if (un->un_f_arq_enabled == FALSE) { 18553 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18554 "no ARQ, sending request sense command\n"); 18555 sd_send_request_sense_command(un, bp, pktp); 18556 } else { 18557 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18558 "ARQ,retrying request sense command\n"); 18559 #if defined(__i386) || defined(__amd64) 18560 /* 18561 * The SD_RETRY_DELAY value need to be adjusted here 18562 * when SD_RETRY_DELAY change in sddef.h 18563 */ 18564 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18565 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18566 NULL); 18567 #else 18568 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18569 EIO, SD_RETRY_DELAY, NULL); 18570 #endif 18571 } 18572 18573 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18574 } 18575 18576 18577 /* 18578 * Function: sd_pkt_status_busy 18579 * 18580 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18581 * 18582 * Context: May be called from interrupt context 18583 */ 18584 18585 static void 18586 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18587 struct scsi_pkt *pktp) 18588 { 18589 ASSERT(un != NULL); 18590 ASSERT(mutex_owned(SD_MUTEX(un))); 18591 ASSERT(bp != NULL); 18592 ASSERT(xp != NULL); 18593 ASSERT(pktp != NULL); 18594 18595 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18596 "sd_pkt_status_busy: entry\n"); 18597 18598 /* If retries are exhausted, just fail the command. */ 18599 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18600 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18601 "device busy too long\n"); 18602 sd_return_failed_command(un, bp, EIO); 18603 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18604 "sd_pkt_status_busy: exit\n"); 18605 return; 18606 } 18607 xp->xb_retry_count++; 18608 18609 /* 18610 * Try to reset the target. However, we do not want to perform 18611 * more than one reset if the device continues to fail. The reset 18612 * will be performed when the retry count reaches the reset 18613 * threshold. This threshold should be set such that at least 18614 * one retry is issued before the reset is performed. 18615 */ 18616 if (xp->xb_retry_count == 18617 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18618 int rval = 0; 18619 mutex_exit(SD_MUTEX(un)); 18620 if (un->un_f_allow_bus_device_reset == TRUE) { 18621 /* 18622 * First try to reset the LUN; if we cannot then 18623 * try to reset the target. 18624 */ 18625 if (un->un_f_lun_reset_enabled == TRUE) { 18626 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18627 "sd_pkt_status_busy: RESET_LUN\n"); 18628 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18629 } 18630 if (rval == 0) { 18631 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18632 "sd_pkt_status_busy: RESET_TARGET\n"); 18633 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18634 } 18635 } 18636 if (rval == 0) { 18637 /* 18638 * If the RESET_LUN and/or RESET_TARGET failed, 18639 * try RESET_ALL 18640 */ 18641 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18642 "sd_pkt_status_busy: RESET_ALL\n"); 18643 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18644 } 18645 mutex_enter(SD_MUTEX(un)); 18646 if (rval == 0) { 18647 /* 18648 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18649 * At this point we give up & fail the command. 18650 */ 18651 sd_return_failed_command(un, bp, EIO); 18652 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18653 "sd_pkt_status_busy: exit (failed cmd)\n"); 18654 return; 18655 } 18656 } 18657 18658 /* 18659 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18660 * we have already checked the retry counts above. 18661 */ 18662 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18663 EIO, SD_BSY_TIMEOUT, NULL); 18664 18665 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18666 "sd_pkt_status_busy: exit\n"); 18667 } 18668 18669 18670 /* 18671 * Function: sd_pkt_status_reservation_conflict 18672 * 18673 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18674 * command status. 18675 * 18676 * Context: May be called from interrupt context 18677 */ 18678 18679 static void 18680 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18681 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18682 { 18683 ASSERT(un != NULL); 18684 ASSERT(mutex_owned(SD_MUTEX(un))); 18685 ASSERT(bp != NULL); 18686 ASSERT(xp != NULL); 18687 ASSERT(pktp != NULL); 18688 18689 /* 18690 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18691 * conflict could be due to various reasons like incorrect keys, not 18692 * registered or not reserved etc. So, we return EACCES to the caller. 18693 */ 18694 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18695 int cmd = SD_GET_PKT_OPCODE(pktp); 18696 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18697 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18698 sd_return_failed_command(un, bp, EACCES); 18699 return; 18700 } 18701 } 18702 18703 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18704 18705 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18706 if (sd_failfast_enable != 0) { 18707 /* By definition, we must panic here.... */ 18708 sd_panic_for_res_conflict(un); 18709 /*NOTREACHED*/ 18710 } 18711 SD_ERROR(SD_LOG_IO, un, 18712 "sd_handle_resv_conflict: Disk Reserved\n"); 18713 sd_return_failed_command(un, bp, EACCES); 18714 return; 18715 } 18716 18717 /* 18718 * 1147670: retry only if sd_retry_on_reservation_conflict 18719 * property is set (default is 1). Retries will not succeed 18720 * on a disk reserved by another initiator. HA systems 18721 * may reset this via sd.conf to avoid these retries. 18722 * 18723 * Note: The legacy return code for this failure is EIO, however EACCES 18724 * seems more appropriate for a reservation conflict. 18725 */ 18726 if (sd_retry_on_reservation_conflict == 0) { 18727 SD_ERROR(SD_LOG_IO, un, 18728 "sd_handle_resv_conflict: Device Reserved\n"); 18729 sd_return_failed_command(un, bp, EIO); 18730 return; 18731 } 18732 18733 /* 18734 * Retry the command if we can. 18735 * 18736 * Note: The legacy return code for this failure is EIO, however EACCES 18737 * seems more appropriate for a reservation conflict. 18738 */ 18739 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18740 (clock_t)2, NULL); 18741 } 18742 18743 18744 18745 /* 18746 * Function: sd_pkt_status_qfull 18747 * 18748 * Description: Handle a QUEUE FULL condition from the target. This can 18749 * occur if the HBA does not handle the queue full condition. 18750 * (Basically this means third-party HBAs as Sun HBAs will 18751 * handle the queue full condition.) Note that if there are 18752 * some commands already in the transport, then the queue full 18753 * has occurred because the queue for this nexus is actually 18754 * full. If there are no commands in the transport, then the 18755 * queue full is resulting from some other initiator or lun 18756 * consuming all the resources at the target. 18757 * 18758 * Context: May be called from interrupt context 18759 */ 18760 18761 static void 18762 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18763 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18764 { 18765 ASSERT(un != NULL); 18766 ASSERT(mutex_owned(SD_MUTEX(un))); 18767 ASSERT(bp != NULL); 18768 ASSERT(xp != NULL); 18769 ASSERT(pktp != NULL); 18770 18771 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18772 "sd_pkt_status_qfull: entry\n"); 18773 18774 /* 18775 * Just lower the QFULL throttle and retry the command. Note that 18776 * we do not limit the number of retries here. 18777 */ 18778 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18779 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18780 SD_RESTART_TIMEOUT, NULL); 18781 18782 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18783 "sd_pkt_status_qfull: exit\n"); 18784 } 18785 18786 18787 /* 18788 * Function: sd_reset_target 18789 * 18790 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18791 * RESET_TARGET, or RESET_ALL. 18792 * 18793 * Context: May be called under interrupt context. 18794 */ 18795 18796 static void 18797 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18798 { 18799 int rval = 0; 18800 18801 ASSERT(un != NULL); 18802 ASSERT(mutex_owned(SD_MUTEX(un))); 18803 ASSERT(pktp != NULL); 18804 18805 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18806 18807 /* 18808 * No need to reset if the transport layer has already done so. 18809 */ 18810 if ((pktp->pkt_statistics & 18811 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18812 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18813 "sd_reset_target: no reset\n"); 18814 return; 18815 } 18816 18817 mutex_exit(SD_MUTEX(un)); 18818 18819 if (un->un_f_allow_bus_device_reset == TRUE) { 18820 if (un->un_f_lun_reset_enabled == TRUE) { 18821 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18822 "sd_reset_target: RESET_LUN\n"); 18823 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18824 } 18825 if (rval == 0) { 18826 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18827 "sd_reset_target: RESET_TARGET\n"); 18828 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18829 } 18830 } 18831 18832 if (rval == 0) { 18833 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18834 "sd_reset_target: RESET_ALL\n"); 18835 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18836 } 18837 18838 mutex_enter(SD_MUTEX(un)); 18839 18840 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18841 } 18842 18843 18844 /* 18845 * Function: sd_media_change_task 18846 * 18847 * Description: Recovery action for CDROM to become available. 18848 * 18849 * Context: Executes in a taskq() thread context 18850 */ 18851 18852 static void 18853 sd_media_change_task(void *arg) 18854 { 18855 struct scsi_pkt *pktp = arg; 18856 struct sd_lun *un; 18857 struct buf *bp; 18858 struct sd_xbuf *xp; 18859 int err = 0; 18860 int retry_count = 0; 18861 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18862 struct sd_sense_info si; 18863 18864 ASSERT(pktp != NULL); 18865 bp = (struct buf *)pktp->pkt_private; 18866 ASSERT(bp != NULL); 18867 xp = SD_GET_XBUF(bp); 18868 ASSERT(xp != NULL); 18869 un = SD_GET_UN(bp); 18870 ASSERT(un != NULL); 18871 ASSERT(!mutex_owned(SD_MUTEX(un))); 18872 ASSERT(un->un_f_monitor_media_state); 18873 18874 si.ssi_severity = SCSI_ERR_INFO; 18875 si.ssi_pfa_flag = FALSE; 18876 18877 /* 18878 * When a reset is issued on a CDROM, it takes a long time to 18879 * recover. First few attempts to read capacity and other things 18880 * related to handling unit attention fail (with a ASC 0x4 and 18881 * ASCQ 0x1). In that case we want to do enough retries and we want 18882 * to limit the retries in other cases of genuine failures like 18883 * no media in drive. 18884 */ 18885 while (retry_count++ < retry_limit) { 18886 if ((err = sd_handle_mchange(un)) == 0) { 18887 break; 18888 } 18889 if (err == EAGAIN) { 18890 retry_limit = SD_UNIT_ATTENTION_RETRY; 18891 } 18892 /* Sleep for 0.5 sec. & try again */ 18893 delay(drv_usectohz(500000)); 18894 } 18895 18896 /* 18897 * Dispatch (retry or fail) the original command here, 18898 * along with appropriate console messages.... 18899 * 18900 * Must grab the mutex before calling sd_retry_command, 18901 * sd_print_sense_msg and sd_return_failed_command. 18902 */ 18903 mutex_enter(SD_MUTEX(un)); 18904 if (err != SD_CMD_SUCCESS) { 18905 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18906 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18907 si.ssi_severity = SCSI_ERR_FATAL; 18908 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18909 sd_return_failed_command(un, bp, EIO); 18910 } else { 18911 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18912 &si, EIO, (clock_t)0, NULL); 18913 } 18914 mutex_exit(SD_MUTEX(un)); 18915 } 18916 18917 18918 18919 /* 18920 * Function: sd_handle_mchange 18921 * 18922 * Description: Perform geometry validation & other recovery when CDROM 18923 * has been removed from drive. 18924 * 18925 * Return Code: 0 for success 18926 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18927 * sd_send_scsi_READ_CAPACITY() 18928 * 18929 * Context: Executes in a taskq() thread context 18930 */ 18931 18932 static int 18933 sd_handle_mchange(struct sd_lun *un) 18934 { 18935 uint64_t capacity; 18936 uint32_t lbasize; 18937 int rval; 18938 18939 ASSERT(!mutex_owned(SD_MUTEX(un))); 18940 ASSERT(un->un_f_monitor_media_state); 18941 18942 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18943 SD_PATH_DIRECT_PRIORITY)) != 0) { 18944 return (rval); 18945 } 18946 18947 mutex_enter(SD_MUTEX(un)); 18948 sd_update_block_info(un, lbasize, capacity); 18949 18950 if (un->un_errstats != NULL) { 18951 struct sd_errstats *stp = 18952 (struct sd_errstats *)un->un_errstats->ks_data; 18953 stp->sd_capacity.value.ui64 = (uint64_t) 18954 ((uint64_t)un->un_blockcount * 18955 (uint64_t)un->un_tgt_blocksize); 18956 } 18957 18958 /* 18959 * Note: Maybe let the strategy/partitioning chain worry about getting 18960 * valid geometry. 18961 */ 18962 un->un_f_geometry_is_valid = FALSE; 18963 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 18964 if (un->un_f_geometry_is_valid == FALSE) { 18965 mutex_exit(SD_MUTEX(un)); 18966 return (EIO); 18967 } 18968 18969 mutex_exit(SD_MUTEX(un)); 18970 18971 /* 18972 * Try to lock the door 18973 */ 18974 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 18975 SD_PATH_DIRECT_PRIORITY)); 18976 } 18977 18978 18979 /* 18980 * Function: sd_send_scsi_DOORLOCK 18981 * 18982 * Description: Issue the scsi DOOR LOCK command 18983 * 18984 * Arguments: un - pointer to driver soft state (unit) structure for 18985 * this target. 18986 * flag - SD_REMOVAL_ALLOW 18987 * SD_REMOVAL_PREVENT 18988 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18989 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18990 * to use the USCSI "direct" chain and bypass the normal 18991 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18992 * command is issued as part of an error recovery action. 18993 * 18994 * Return Code: 0 - Success 18995 * errno return code from sd_send_scsi_cmd() 18996 * 18997 * Context: Can sleep. 18998 */ 18999 19000 static int 19001 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19002 { 19003 union scsi_cdb cdb; 19004 struct uscsi_cmd ucmd_buf; 19005 struct scsi_extended_sense sense_buf; 19006 int status; 19007 19008 ASSERT(un != NULL); 19009 ASSERT(!mutex_owned(SD_MUTEX(un))); 19010 19011 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19012 19013 /* already determined doorlock is not supported, fake success */ 19014 if (un->un_f_doorlock_supported == FALSE) { 19015 return (0); 19016 } 19017 19018 bzero(&cdb, sizeof (cdb)); 19019 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19020 19021 cdb.scc_cmd = SCMD_DOORLOCK; 19022 cdb.cdb_opaque[4] = (uchar_t)flag; 19023 19024 ucmd_buf.uscsi_cdb = (char *)&cdb; 19025 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19026 ucmd_buf.uscsi_bufaddr = NULL; 19027 ucmd_buf.uscsi_buflen = 0; 19028 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19029 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19030 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19031 ucmd_buf.uscsi_timeout = 15; 19032 19033 SD_TRACE(SD_LOG_IO, un, 19034 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19035 19036 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19037 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19038 19039 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19040 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19041 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19042 /* fake success and skip subsequent doorlock commands */ 19043 un->un_f_doorlock_supported = FALSE; 19044 return (0); 19045 } 19046 19047 return (status); 19048 } 19049 19050 /* 19051 * Function: sd_send_scsi_READ_CAPACITY 19052 * 19053 * Description: This routine uses the scsi READ CAPACITY command to determine 19054 * the device capacity in number of blocks and the device native 19055 * block size. If this function returns a failure, then the 19056 * values in *capp and *lbap are undefined. If the capacity 19057 * returned is 0xffffffff then the lun is too large for a 19058 * normal READ CAPACITY command and the results of a 19059 * READ CAPACITY 16 will be used instead. 19060 * 19061 * Arguments: un - ptr to soft state struct for the target 19062 * capp - ptr to unsigned 64-bit variable to receive the 19063 * capacity value from the command. 19064 * lbap - ptr to unsigned 32-bit varaible to receive the 19065 * block size value from the command 19066 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19067 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19068 * to use the USCSI "direct" chain and bypass the normal 19069 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19070 * command is issued as part of an error recovery action. 19071 * 19072 * Return Code: 0 - Success 19073 * EIO - IO error 19074 * EACCES - Reservation conflict detected 19075 * EAGAIN - Device is becoming ready 19076 * errno return code from sd_send_scsi_cmd() 19077 * 19078 * Context: Can sleep. Blocks until command completes. 19079 */ 19080 19081 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19082 19083 static int 19084 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19085 int path_flag) 19086 { 19087 struct scsi_extended_sense sense_buf; 19088 struct uscsi_cmd ucmd_buf; 19089 union scsi_cdb cdb; 19090 uint32_t *capacity_buf; 19091 uint64_t capacity; 19092 uint32_t lbasize; 19093 int status; 19094 19095 ASSERT(un != NULL); 19096 ASSERT(!mutex_owned(SD_MUTEX(un))); 19097 ASSERT(capp != NULL); 19098 ASSERT(lbap != NULL); 19099 19100 SD_TRACE(SD_LOG_IO, un, 19101 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19102 19103 /* 19104 * First send a READ_CAPACITY command to the target. 19105 * (This command is mandatory under SCSI-2.) 19106 * 19107 * Set up the CDB for the READ_CAPACITY command. The Partial 19108 * Medium Indicator bit is cleared. The address field must be 19109 * zero if the PMI bit is zero. 19110 */ 19111 bzero(&cdb, sizeof (cdb)); 19112 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19113 19114 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19115 19116 cdb.scc_cmd = SCMD_READ_CAPACITY; 19117 19118 ucmd_buf.uscsi_cdb = (char *)&cdb; 19119 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19120 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19121 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19122 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19123 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19124 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19125 ucmd_buf.uscsi_timeout = 60; 19126 19127 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19128 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19129 19130 switch (status) { 19131 case 0: 19132 /* Return failure if we did not get valid capacity data. */ 19133 if (ucmd_buf.uscsi_resid != 0) { 19134 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19135 return (EIO); 19136 } 19137 19138 /* 19139 * Read capacity and block size from the READ CAPACITY 10 data. 19140 * This data may be adjusted later due to device specific 19141 * issues. 19142 * 19143 * According to the SCSI spec, the READ CAPACITY 10 19144 * command returns the following: 19145 * 19146 * bytes 0-3: Maximum logical block address available. 19147 * (MSB in byte:0 & LSB in byte:3) 19148 * 19149 * bytes 4-7: Block length in bytes 19150 * (MSB in byte:4 & LSB in byte:7) 19151 * 19152 */ 19153 capacity = BE_32(capacity_buf[0]); 19154 lbasize = BE_32(capacity_buf[1]); 19155 19156 /* 19157 * Done with capacity_buf 19158 */ 19159 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19160 19161 /* 19162 * if the reported capacity is set to all 0xf's, then 19163 * this disk is too large and requires SBC-2 commands. 19164 * Reissue the request using READ CAPACITY 16. 19165 */ 19166 if (capacity == 0xffffffff) { 19167 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19168 &lbasize, path_flag); 19169 if (status != 0) { 19170 return (status); 19171 } 19172 } 19173 break; /* Success! */ 19174 case EIO: 19175 switch (ucmd_buf.uscsi_status) { 19176 case STATUS_RESERVATION_CONFLICT: 19177 status = EACCES; 19178 break; 19179 case STATUS_CHECK: 19180 /* 19181 * Check condition; look for ASC/ASCQ of 0x04/0x01 19182 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19183 */ 19184 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19185 (sense_buf.es_add_code == 0x04) && 19186 (sense_buf.es_qual_code == 0x01)) { 19187 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19188 return (EAGAIN); 19189 } 19190 break; 19191 default: 19192 break; 19193 } 19194 /* FALLTHRU */ 19195 default: 19196 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19197 return (status); 19198 } 19199 19200 /* 19201 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19202 * (2352 and 0 are common) so for these devices always force the value 19203 * to 2048 as required by the ATAPI specs. 19204 */ 19205 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19206 lbasize = 2048; 19207 } 19208 19209 /* 19210 * Get the maximum LBA value from the READ CAPACITY data. 19211 * Here we assume that the Partial Medium Indicator (PMI) bit 19212 * was cleared when issuing the command. This means that the LBA 19213 * returned from the device is the LBA of the last logical block 19214 * on the logical unit. The actual logical block count will be 19215 * this value plus one. 19216 * 19217 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19218 * so scale the capacity value to reflect this. 19219 */ 19220 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19221 19222 #if defined(__i386) || defined(__amd64) 19223 /* 19224 * On x86, compensate for off-by-1 error (number of sectors on 19225 * media) (1175930) 19226 */ 19227 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 19228 (lbasize == un->un_sys_blocksize)) { 19229 capacity -= 1; 19230 } 19231 #endif 19232 19233 /* 19234 * Copy the values from the READ CAPACITY command into the space 19235 * provided by the caller. 19236 */ 19237 *capp = capacity; 19238 *lbap = lbasize; 19239 19240 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19241 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19242 19243 /* 19244 * Both the lbasize and capacity from the device must be nonzero, 19245 * otherwise we assume that the values are not valid and return 19246 * failure to the caller. (4203735) 19247 */ 19248 if ((capacity == 0) || (lbasize == 0)) { 19249 return (EIO); 19250 } 19251 19252 return (0); 19253 } 19254 19255 /* 19256 * Function: sd_send_scsi_READ_CAPACITY_16 19257 * 19258 * Description: This routine uses the scsi READ CAPACITY 16 command to 19259 * determine the device capacity in number of blocks and the 19260 * device native block size. If this function returns a failure, 19261 * then the values in *capp and *lbap are undefined. 19262 * This routine should always be called by 19263 * sd_send_scsi_READ_CAPACITY which will appy any device 19264 * specific adjustments to capacity and lbasize. 19265 * 19266 * Arguments: un - ptr to soft state struct for the target 19267 * capp - ptr to unsigned 64-bit variable to receive the 19268 * capacity value from the command. 19269 * lbap - ptr to unsigned 32-bit varaible to receive the 19270 * block size value from the command 19271 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19272 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19273 * to use the USCSI "direct" chain and bypass the normal 19274 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19275 * this command is issued as part of an error recovery 19276 * action. 19277 * 19278 * Return Code: 0 - Success 19279 * EIO - IO error 19280 * EACCES - Reservation conflict detected 19281 * EAGAIN - Device is becoming ready 19282 * errno return code from sd_send_scsi_cmd() 19283 * 19284 * Context: Can sleep. Blocks until command completes. 19285 */ 19286 19287 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19288 19289 static int 19290 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19291 uint32_t *lbap, int path_flag) 19292 { 19293 struct scsi_extended_sense sense_buf; 19294 struct uscsi_cmd ucmd_buf; 19295 union scsi_cdb cdb; 19296 uint64_t *capacity16_buf; 19297 uint64_t capacity; 19298 uint32_t lbasize; 19299 int status; 19300 19301 ASSERT(un != NULL); 19302 ASSERT(!mutex_owned(SD_MUTEX(un))); 19303 ASSERT(capp != NULL); 19304 ASSERT(lbap != NULL); 19305 19306 SD_TRACE(SD_LOG_IO, un, 19307 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19308 19309 /* 19310 * First send a READ_CAPACITY_16 command to the target. 19311 * 19312 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19313 * Medium Indicator bit is cleared. The address field must be 19314 * zero if the PMI bit is zero. 19315 */ 19316 bzero(&cdb, sizeof (cdb)); 19317 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19318 19319 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19320 19321 ucmd_buf.uscsi_cdb = (char *)&cdb; 19322 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19323 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19324 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19325 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19326 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19327 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19328 ucmd_buf.uscsi_timeout = 60; 19329 19330 /* 19331 * Read Capacity (16) is a Service Action In command. One 19332 * command byte (0x9E) is overloaded for multiple operations, 19333 * with the second CDB byte specifying the desired operation 19334 */ 19335 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19336 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19337 19338 /* 19339 * Fill in allocation length field 19340 */ 19341 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19342 19343 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19344 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19345 19346 switch (status) { 19347 case 0: 19348 /* Return failure if we did not get valid capacity data. */ 19349 if (ucmd_buf.uscsi_resid > 20) { 19350 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19351 return (EIO); 19352 } 19353 19354 /* 19355 * Read capacity and block size from the READ CAPACITY 10 data. 19356 * This data may be adjusted later due to device specific 19357 * issues. 19358 * 19359 * According to the SCSI spec, the READ CAPACITY 10 19360 * command returns the following: 19361 * 19362 * bytes 0-7: Maximum logical block address available. 19363 * (MSB in byte:0 & LSB in byte:7) 19364 * 19365 * bytes 8-11: Block length in bytes 19366 * (MSB in byte:8 & LSB in byte:11) 19367 * 19368 */ 19369 capacity = BE_64(capacity16_buf[0]); 19370 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19371 19372 /* 19373 * Done with capacity16_buf 19374 */ 19375 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19376 19377 /* 19378 * if the reported capacity is set to all 0xf's, then 19379 * this disk is too large. This could only happen with 19380 * a device that supports LBAs larger than 64 bits which 19381 * are not defined by any current T10 standards. 19382 */ 19383 if (capacity == 0xffffffffffffffff) { 19384 return (EIO); 19385 } 19386 break; /* Success! */ 19387 case EIO: 19388 switch (ucmd_buf.uscsi_status) { 19389 case STATUS_RESERVATION_CONFLICT: 19390 status = EACCES; 19391 break; 19392 case STATUS_CHECK: 19393 /* 19394 * Check condition; look for ASC/ASCQ of 0x04/0x01 19395 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19396 */ 19397 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19398 (sense_buf.es_add_code == 0x04) && 19399 (sense_buf.es_qual_code == 0x01)) { 19400 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19401 return (EAGAIN); 19402 } 19403 break; 19404 default: 19405 break; 19406 } 19407 /* FALLTHRU */ 19408 default: 19409 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19410 return (status); 19411 } 19412 19413 *capp = capacity; 19414 *lbap = lbasize; 19415 19416 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19417 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19418 19419 return (0); 19420 } 19421 19422 19423 /* 19424 * Function: sd_send_scsi_START_STOP_UNIT 19425 * 19426 * Description: Issue a scsi START STOP UNIT command to the target. 19427 * 19428 * Arguments: un - pointer to driver soft state (unit) structure for 19429 * this target. 19430 * flag - SD_TARGET_START 19431 * SD_TARGET_STOP 19432 * SD_TARGET_EJECT 19433 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19434 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19435 * to use the USCSI "direct" chain and bypass the normal 19436 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19437 * command is issued as part of an error recovery action. 19438 * 19439 * Return Code: 0 - Success 19440 * EIO - IO error 19441 * EACCES - Reservation conflict detected 19442 * ENXIO - Not Ready, medium not present 19443 * errno return code from sd_send_scsi_cmd() 19444 * 19445 * Context: Can sleep. 19446 */ 19447 19448 static int 19449 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19450 { 19451 struct scsi_extended_sense sense_buf; 19452 union scsi_cdb cdb; 19453 struct uscsi_cmd ucmd_buf; 19454 int status; 19455 19456 ASSERT(un != NULL); 19457 ASSERT(!mutex_owned(SD_MUTEX(un))); 19458 19459 SD_TRACE(SD_LOG_IO, un, 19460 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19461 19462 if (un->un_f_check_start_stop && 19463 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19464 (un->un_f_start_stop_supported != TRUE)) { 19465 return (0); 19466 } 19467 19468 bzero(&cdb, sizeof (cdb)); 19469 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19470 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19471 19472 cdb.scc_cmd = SCMD_START_STOP; 19473 cdb.cdb_opaque[4] = (uchar_t)flag; 19474 19475 ucmd_buf.uscsi_cdb = (char *)&cdb; 19476 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19477 ucmd_buf.uscsi_bufaddr = NULL; 19478 ucmd_buf.uscsi_buflen = 0; 19479 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19480 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19481 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19482 ucmd_buf.uscsi_timeout = 200; 19483 19484 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19485 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19486 19487 switch (status) { 19488 case 0: 19489 break; /* Success! */ 19490 case EIO: 19491 switch (ucmd_buf.uscsi_status) { 19492 case STATUS_RESERVATION_CONFLICT: 19493 status = EACCES; 19494 break; 19495 case STATUS_CHECK: 19496 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19497 switch (sense_buf.es_key) { 19498 case KEY_ILLEGAL_REQUEST: 19499 status = ENOTSUP; 19500 break; 19501 case KEY_NOT_READY: 19502 if (sense_buf.es_add_code == 0x3A) { 19503 status = ENXIO; 19504 } 19505 break; 19506 default: 19507 break; 19508 } 19509 } 19510 break; 19511 default: 19512 break; 19513 } 19514 break; 19515 default: 19516 break; 19517 } 19518 19519 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19520 19521 return (status); 19522 } 19523 19524 19525 /* 19526 * Function: sd_start_stop_unit_callback 19527 * 19528 * Description: timeout(9F) callback to begin recovery process for a 19529 * device that has spun down. 19530 * 19531 * Arguments: arg - pointer to associated softstate struct. 19532 * 19533 * Context: Executes in a timeout(9F) thread context 19534 */ 19535 19536 static void 19537 sd_start_stop_unit_callback(void *arg) 19538 { 19539 struct sd_lun *un = arg; 19540 ASSERT(un != NULL); 19541 ASSERT(!mutex_owned(SD_MUTEX(un))); 19542 19543 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19544 19545 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19546 } 19547 19548 19549 /* 19550 * Function: sd_start_stop_unit_task 19551 * 19552 * Description: Recovery procedure when a drive is spun down. 19553 * 19554 * Arguments: arg - pointer to associated softstate struct. 19555 * 19556 * Context: Executes in a taskq() thread context 19557 */ 19558 19559 static void 19560 sd_start_stop_unit_task(void *arg) 19561 { 19562 struct sd_lun *un = arg; 19563 19564 ASSERT(un != NULL); 19565 ASSERT(!mutex_owned(SD_MUTEX(un))); 19566 19567 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19568 19569 /* 19570 * Some unformatted drives report not ready error, no need to 19571 * restart if format has been initiated. 19572 */ 19573 mutex_enter(SD_MUTEX(un)); 19574 if (un->un_f_format_in_progress == TRUE) { 19575 mutex_exit(SD_MUTEX(un)); 19576 return; 19577 } 19578 mutex_exit(SD_MUTEX(un)); 19579 19580 /* 19581 * When a START STOP command is issued from here, it is part of a 19582 * failure recovery operation and must be issued before any other 19583 * commands, including any pending retries. Thus it must be sent 19584 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19585 * succeeds or not, we will start I/O after the attempt. 19586 */ 19587 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19588 SD_PATH_DIRECT_PRIORITY); 19589 19590 /* 19591 * The above call blocks until the START_STOP_UNIT command completes. 19592 * Now that it has completed, we must re-try the original IO that 19593 * received the NOT READY condition in the first place. There are 19594 * three possible conditions here: 19595 * 19596 * (1) The original IO is on un_retry_bp. 19597 * (2) The original IO is on the regular wait queue, and un_retry_bp 19598 * is NULL. 19599 * (3) The original IO is on the regular wait queue, and un_retry_bp 19600 * points to some other, unrelated bp. 19601 * 19602 * For each case, we must call sd_start_cmds() with un_retry_bp 19603 * as the argument. If un_retry_bp is NULL, this will initiate 19604 * processing of the regular wait queue. If un_retry_bp is not NULL, 19605 * then this will process the bp on un_retry_bp. That may or may not 19606 * be the original IO, but that does not matter: the important thing 19607 * is to keep the IO processing going at this point. 19608 * 19609 * Note: This is a very specific error recovery sequence associated 19610 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19611 * serialize the I/O with completion of the spin-up. 19612 */ 19613 mutex_enter(SD_MUTEX(un)); 19614 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19615 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19616 un, un->un_retry_bp); 19617 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19618 sd_start_cmds(un, un->un_retry_bp); 19619 mutex_exit(SD_MUTEX(un)); 19620 19621 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19622 } 19623 19624 19625 /* 19626 * Function: sd_send_scsi_INQUIRY 19627 * 19628 * Description: Issue the scsi INQUIRY command. 19629 * 19630 * Arguments: un 19631 * bufaddr 19632 * buflen 19633 * evpd 19634 * page_code 19635 * page_length 19636 * 19637 * Return Code: 0 - Success 19638 * errno return code from sd_send_scsi_cmd() 19639 * 19640 * Context: Can sleep. Does not return until command is completed. 19641 */ 19642 19643 static int 19644 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19645 uchar_t evpd, uchar_t page_code, size_t *residp) 19646 { 19647 union scsi_cdb cdb; 19648 struct uscsi_cmd ucmd_buf; 19649 int status; 19650 19651 ASSERT(un != NULL); 19652 ASSERT(!mutex_owned(SD_MUTEX(un))); 19653 ASSERT(bufaddr != NULL); 19654 19655 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19656 19657 bzero(&cdb, sizeof (cdb)); 19658 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19659 bzero(bufaddr, buflen); 19660 19661 cdb.scc_cmd = SCMD_INQUIRY; 19662 cdb.cdb_opaque[1] = evpd; 19663 cdb.cdb_opaque[2] = page_code; 19664 FORMG0COUNT(&cdb, buflen); 19665 19666 ucmd_buf.uscsi_cdb = (char *)&cdb; 19667 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19668 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19669 ucmd_buf.uscsi_buflen = buflen; 19670 ucmd_buf.uscsi_rqbuf = NULL; 19671 ucmd_buf.uscsi_rqlen = 0; 19672 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19673 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19674 19675 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19676 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19677 19678 if ((status == 0) && (residp != NULL)) { 19679 *residp = ucmd_buf.uscsi_resid; 19680 } 19681 19682 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19683 19684 return (status); 19685 } 19686 19687 19688 /* 19689 * Function: sd_send_scsi_TEST_UNIT_READY 19690 * 19691 * Description: Issue the scsi TEST UNIT READY command. 19692 * This routine can be told to set the flag USCSI_DIAGNOSE to 19693 * prevent retrying failed commands. Use this when the intent 19694 * is either to check for device readiness, to clear a Unit 19695 * Attention, or to clear any outstanding sense data. 19696 * However under specific conditions the expected behavior 19697 * is for retries to bring a device ready, so use the flag 19698 * with caution. 19699 * 19700 * Arguments: un 19701 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19702 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19703 * 0: dont check for media present, do retries on cmd. 19704 * 19705 * Return Code: 0 - Success 19706 * EIO - IO error 19707 * EACCES - Reservation conflict detected 19708 * ENXIO - Not Ready, medium not present 19709 * errno return code from sd_send_scsi_cmd() 19710 * 19711 * Context: Can sleep. Does not return until command is completed. 19712 */ 19713 19714 static int 19715 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19716 { 19717 struct scsi_extended_sense sense_buf; 19718 union scsi_cdb cdb; 19719 struct uscsi_cmd ucmd_buf; 19720 int status; 19721 19722 ASSERT(un != NULL); 19723 ASSERT(!mutex_owned(SD_MUTEX(un))); 19724 19725 SD_TRACE(SD_LOG_IO, un, 19726 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19727 19728 /* 19729 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19730 * timeouts when they receive a TUR and the queue is not empty. Check 19731 * the configuration flag set during attach (indicating the drive has 19732 * this firmware bug) and un_ncmds_in_transport before issuing the 19733 * TUR. If there are 19734 * pending commands return success, this is a bit arbitrary but is ok 19735 * for non-removables (i.e. the eliteI disks) and non-clustering 19736 * configurations. 19737 */ 19738 if (un->un_f_cfg_tur_check == TRUE) { 19739 mutex_enter(SD_MUTEX(un)); 19740 if (un->un_ncmds_in_transport != 0) { 19741 mutex_exit(SD_MUTEX(un)); 19742 return (0); 19743 } 19744 mutex_exit(SD_MUTEX(un)); 19745 } 19746 19747 bzero(&cdb, sizeof (cdb)); 19748 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19749 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19750 19751 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19752 19753 ucmd_buf.uscsi_cdb = (char *)&cdb; 19754 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19755 ucmd_buf.uscsi_bufaddr = NULL; 19756 ucmd_buf.uscsi_buflen = 0; 19757 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19758 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19759 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19760 19761 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19762 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19763 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19764 } 19765 ucmd_buf.uscsi_timeout = 60; 19766 19767 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19768 UIO_SYSSPACE, UIO_SYSSPACE, 19769 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19770 19771 switch (status) { 19772 case 0: 19773 break; /* Success! */ 19774 case EIO: 19775 switch (ucmd_buf.uscsi_status) { 19776 case STATUS_RESERVATION_CONFLICT: 19777 status = EACCES; 19778 break; 19779 case STATUS_CHECK: 19780 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19781 break; 19782 } 19783 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19784 (sense_buf.es_key == KEY_NOT_READY) && 19785 (sense_buf.es_add_code == 0x3A)) { 19786 status = ENXIO; 19787 } 19788 break; 19789 default: 19790 break; 19791 } 19792 break; 19793 default: 19794 break; 19795 } 19796 19797 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19798 19799 return (status); 19800 } 19801 19802 19803 /* 19804 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19805 * 19806 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19807 * 19808 * Arguments: un 19809 * 19810 * Return Code: 0 - Success 19811 * EACCES 19812 * ENOTSUP 19813 * errno return code from sd_send_scsi_cmd() 19814 * 19815 * Context: Can sleep. Does not return until command is completed. 19816 */ 19817 19818 static int 19819 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19820 uint16_t data_len, uchar_t *data_bufp) 19821 { 19822 struct scsi_extended_sense sense_buf; 19823 union scsi_cdb cdb; 19824 struct uscsi_cmd ucmd_buf; 19825 int status; 19826 int no_caller_buf = FALSE; 19827 19828 ASSERT(un != NULL); 19829 ASSERT(!mutex_owned(SD_MUTEX(un))); 19830 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19831 19832 SD_TRACE(SD_LOG_IO, un, 19833 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19834 19835 bzero(&cdb, sizeof (cdb)); 19836 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19837 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19838 if (data_bufp == NULL) { 19839 /* Allocate a default buf if the caller did not give one */ 19840 ASSERT(data_len == 0); 19841 data_len = MHIOC_RESV_KEY_SIZE; 19842 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19843 no_caller_buf = TRUE; 19844 } 19845 19846 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19847 cdb.cdb_opaque[1] = usr_cmd; 19848 FORMG1COUNT(&cdb, data_len); 19849 19850 ucmd_buf.uscsi_cdb = (char *)&cdb; 19851 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19852 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19853 ucmd_buf.uscsi_buflen = data_len; 19854 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19855 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19856 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19857 ucmd_buf.uscsi_timeout = 60; 19858 19859 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19860 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19861 19862 switch (status) { 19863 case 0: 19864 break; /* Success! */ 19865 case EIO: 19866 switch (ucmd_buf.uscsi_status) { 19867 case STATUS_RESERVATION_CONFLICT: 19868 status = EACCES; 19869 break; 19870 case STATUS_CHECK: 19871 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19872 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19873 status = ENOTSUP; 19874 } 19875 break; 19876 default: 19877 break; 19878 } 19879 break; 19880 default: 19881 break; 19882 } 19883 19884 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19885 19886 if (no_caller_buf == TRUE) { 19887 kmem_free(data_bufp, data_len); 19888 } 19889 19890 return (status); 19891 } 19892 19893 19894 /* 19895 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19896 * 19897 * Description: This routine is the driver entry point for handling CD-ROM 19898 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19899 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19900 * device. 19901 * 19902 * Arguments: un - Pointer to soft state struct for the target. 19903 * usr_cmd SCSI-3 reservation facility command (one of 19904 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19905 * SD_SCSI3_PREEMPTANDABORT) 19906 * usr_bufp - user provided pointer register, reserve descriptor or 19907 * preempt and abort structure (mhioc_register_t, 19908 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19909 * 19910 * Return Code: 0 - Success 19911 * EACCES 19912 * ENOTSUP 19913 * errno return code from sd_send_scsi_cmd() 19914 * 19915 * Context: Can sleep. Does not return until command is completed. 19916 */ 19917 19918 static int 19919 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19920 uchar_t *usr_bufp) 19921 { 19922 struct scsi_extended_sense sense_buf; 19923 union scsi_cdb cdb; 19924 struct uscsi_cmd ucmd_buf; 19925 int status; 19926 uchar_t data_len = sizeof (sd_prout_t); 19927 sd_prout_t *prp; 19928 19929 ASSERT(un != NULL); 19930 ASSERT(!mutex_owned(SD_MUTEX(un))); 19931 ASSERT(data_len == 24); /* required by scsi spec */ 19932 19933 SD_TRACE(SD_LOG_IO, un, 19934 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19935 19936 if (usr_bufp == NULL) { 19937 return (EINVAL); 19938 } 19939 19940 bzero(&cdb, sizeof (cdb)); 19941 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19942 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19943 prp = kmem_zalloc(data_len, KM_SLEEP); 19944 19945 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 19946 cdb.cdb_opaque[1] = usr_cmd; 19947 FORMG1COUNT(&cdb, data_len); 19948 19949 ucmd_buf.uscsi_cdb = (char *)&cdb; 19950 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19951 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 19952 ucmd_buf.uscsi_buflen = data_len; 19953 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19954 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19955 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19956 ucmd_buf.uscsi_timeout = 60; 19957 19958 switch (usr_cmd) { 19959 case SD_SCSI3_REGISTER: { 19960 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 19961 19962 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19963 bcopy(ptr->newkey.key, prp->service_key, 19964 MHIOC_RESV_KEY_SIZE); 19965 prp->aptpl = ptr->aptpl; 19966 break; 19967 } 19968 case SD_SCSI3_RESERVE: 19969 case SD_SCSI3_RELEASE: { 19970 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 19971 19972 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19973 prp->scope_address = BE_32(ptr->scope_specific_addr); 19974 cdb.cdb_opaque[2] = ptr->type; 19975 break; 19976 } 19977 case SD_SCSI3_PREEMPTANDABORT: { 19978 mhioc_preemptandabort_t *ptr = 19979 (mhioc_preemptandabort_t *)usr_bufp; 19980 19981 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19982 bcopy(ptr->victim_key.key, prp->service_key, 19983 MHIOC_RESV_KEY_SIZE); 19984 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 19985 cdb.cdb_opaque[2] = ptr->resvdesc.type; 19986 ucmd_buf.uscsi_flags |= USCSI_HEAD; 19987 break; 19988 } 19989 case SD_SCSI3_REGISTERANDIGNOREKEY: 19990 { 19991 mhioc_registerandignorekey_t *ptr; 19992 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 19993 bcopy(ptr->newkey.key, 19994 prp->service_key, MHIOC_RESV_KEY_SIZE); 19995 prp->aptpl = ptr->aptpl; 19996 break; 19997 } 19998 default: 19999 ASSERT(FALSE); 20000 break; 20001 } 20002 20003 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20004 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20005 20006 switch (status) { 20007 case 0: 20008 break; /* Success! */ 20009 case EIO: 20010 switch (ucmd_buf.uscsi_status) { 20011 case STATUS_RESERVATION_CONFLICT: 20012 status = EACCES; 20013 break; 20014 case STATUS_CHECK: 20015 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20016 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 20017 status = ENOTSUP; 20018 } 20019 break; 20020 default: 20021 break; 20022 } 20023 break; 20024 default: 20025 break; 20026 } 20027 20028 kmem_free(prp, data_len); 20029 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20030 return (status); 20031 } 20032 20033 20034 /* 20035 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20036 * 20037 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20038 * 20039 * Arguments: un - pointer to the target's soft state struct 20040 * 20041 * Return Code: 0 - success 20042 * errno-type error code 20043 * 20044 * Context: kernel thread context only. 20045 */ 20046 20047 static int 20048 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20049 { 20050 struct sd_uscsi_info *uip; 20051 struct uscsi_cmd *uscmd; 20052 union scsi_cdb *cdb; 20053 struct buf *bp; 20054 int rval = 0; 20055 20056 SD_TRACE(SD_LOG_IO, un, 20057 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20058 20059 ASSERT(un != NULL); 20060 ASSERT(!mutex_owned(SD_MUTEX(un))); 20061 20062 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20063 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20064 20065 /* 20066 * First get some memory for the uscsi_cmd struct and cdb 20067 * and initialize for SYNCHRONIZE_CACHE cmd. 20068 */ 20069 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20070 uscmd->uscsi_cdblen = CDB_GROUP1; 20071 uscmd->uscsi_cdb = (caddr_t)cdb; 20072 uscmd->uscsi_bufaddr = NULL; 20073 uscmd->uscsi_buflen = 0; 20074 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20075 uscmd->uscsi_rqlen = SENSE_LENGTH; 20076 uscmd->uscsi_rqresid = SENSE_LENGTH; 20077 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20078 uscmd->uscsi_timeout = sd_io_time; 20079 20080 /* 20081 * Allocate an sd_uscsi_info struct and fill it with the info 20082 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20083 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20084 * since we allocate the buf here in this function, we do not 20085 * need to preserve the prior contents of b_private. 20086 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20087 */ 20088 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20089 uip->ui_flags = SD_PATH_DIRECT; 20090 uip->ui_cmdp = uscmd; 20091 20092 bp = getrbuf(KM_SLEEP); 20093 bp->b_private = uip; 20094 20095 /* 20096 * Setup buffer to carry uscsi request. 20097 */ 20098 bp->b_flags = B_BUSY; 20099 bp->b_bcount = 0; 20100 bp->b_blkno = 0; 20101 20102 if (dkc != NULL) { 20103 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20104 uip->ui_dkc = *dkc; 20105 } 20106 20107 bp->b_edev = SD_GET_DEV(un); 20108 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20109 20110 (void) sd_uscsi_strategy(bp); 20111 20112 /* 20113 * If synchronous request, wait for completion 20114 * If async just return and let b_iodone callback 20115 * cleanup. 20116 * NOTE: On return, u_ncmds_in_driver will be decremented, 20117 * but it was also incremented in sd_uscsi_strategy(), so 20118 * we should be ok. 20119 */ 20120 if (dkc == NULL) { 20121 (void) biowait(bp); 20122 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20123 } 20124 20125 return (rval); 20126 } 20127 20128 20129 static int 20130 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20131 { 20132 struct sd_uscsi_info *uip; 20133 struct uscsi_cmd *uscmd; 20134 struct scsi_extended_sense *sense_buf; 20135 struct sd_lun *un; 20136 int status; 20137 20138 uip = (struct sd_uscsi_info *)(bp->b_private); 20139 ASSERT(uip != NULL); 20140 20141 uscmd = uip->ui_cmdp; 20142 ASSERT(uscmd != NULL); 20143 20144 sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf; 20145 ASSERT(sense_buf != NULL); 20146 20147 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20148 ASSERT(un != NULL); 20149 20150 status = geterror(bp); 20151 switch (status) { 20152 case 0: 20153 break; /* Success! */ 20154 case EIO: 20155 switch (uscmd->uscsi_status) { 20156 case STATUS_RESERVATION_CONFLICT: 20157 /* Ignore reservation conflict */ 20158 status = 0; 20159 goto done; 20160 20161 case STATUS_CHECK: 20162 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20163 (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) { 20164 /* Ignore Illegal Request error */ 20165 mutex_enter(SD_MUTEX(un)); 20166 un->un_f_sync_cache_supported = FALSE; 20167 mutex_exit(SD_MUTEX(un)); 20168 status = ENOTSUP; 20169 goto done; 20170 } 20171 break; 20172 default: 20173 break; 20174 } 20175 /* FALLTHRU */ 20176 default: 20177 /* Ignore error if the media is not present */ 20178 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 20179 status = 0; 20180 goto done; 20181 } 20182 /* If we reach this, we had an error */ 20183 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20184 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20185 break; 20186 } 20187 20188 done: 20189 if (uip->ui_dkc.dkc_callback != NULL) { 20190 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20191 } 20192 20193 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20194 freerbuf(bp); 20195 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20196 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20197 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20198 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20199 20200 return (status); 20201 } 20202 20203 20204 /* 20205 * Function: sd_send_scsi_GET_CONFIGURATION 20206 * 20207 * Description: Issues the get configuration command to the device. 20208 * Called from sd_check_for_writable_cd & sd_get_media_info 20209 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20210 * Arguments: un 20211 * ucmdbuf 20212 * rqbuf 20213 * rqbuflen 20214 * bufaddr 20215 * buflen 20216 * 20217 * Return Code: 0 - Success 20218 * errno return code from sd_send_scsi_cmd() 20219 * 20220 * Context: Can sleep. Does not return until command is completed. 20221 * 20222 */ 20223 20224 static int 20225 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20226 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20227 { 20228 char cdb[CDB_GROUP1]; 20229 int status; 20230 20231 ASSERT(un != NULL); 20232 ASSERT(!mutex_owned(SD_MUTEX(un))); 20233 ASSERT(bufaddr != NULL); 20234 ASSERT(ucmdbuf != NULL); 20235 ASSERT(rqbuf != NULL); 20236 20237 SD_TRACE(SD_LOG_IO, un, 20238 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20239 20240 bzero(cdb, sizeof (cdb)); 20241 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20242 bzero(rqbuf, rqbuflen); 20243 bzero(bufaddr, buflen); 20244 20245 /* 20246 * Set up cdb field for the get configuration command. 20247 */ 20248 cdb[0] = SCMD_GET_CONFIGURATION; 20249 cdb[1] = 0x02; /* Requested Type */ 20250 cdb[8] = SD_PROFILE_HEADER_LEN; 20251 ucmdbuf->uscsi_cdb = cdb; 20252 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20253 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20254 ucmdbuf->uscsi_buflen = buflen; 20255 ucmdbuf->uscsi_timeout = sd_io_time; 20256 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20257 ucmdbuf->uscsi_rqlen = rqbuflen; 20258 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20259 20260 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20261 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20262 20263 switch (status) { 20264 case 0: 20265 break; /* Success! */ 20266 case EIO: 20267 switch (ucmdbuf->uscsi_status) { 20268 case STATUS_RESERVATION_CONFLICT: 20269 status = EACCES; 20270 break; 20271 default: 20272 break; 20273 } 20274 break; 20275 default: 20276 break; 20277 } 20278 20279 if (status == 0) { 20280 SD_DUMP_MEMORY(un, SD_LOG_IO, 20281 "sd_send_scsi_GET_CONFIGURATION: data", 20282 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20283 } 20284 20285 SD_TRACE(SD_LOG_IO, un, 20286 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20287 20288 return (status); 20289 } 20290 20291 /* 20292 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20293 * 20294 * Description: Issues the get configuration command to the device to 20295 * retrieve a specfic feature. Called from 20296 * sd_check_for_writable_cd & sd_set_mmc_caps. 20297 * Arguments: un 20298 * ucmdbuf 20299 * rqbuf 20300 * rqbuflen 20301 * bufaddr 20302 * buflen 20303 * feature 20304 * 20305 * Return Code: 0 - Success 20306 * errno return code from sd_send_scsi_cmd() 20307 * 20308 * Context: Can sleep. Does not return until command is completed. 20309 * 20310 */ 20311 static int 20312 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20313 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20314 uchar_t *bufaddr, uint_t buflen, char feature) 20315 { 20316 char cdb[CDB_GROUP1]; 20317 int status; 20318 20319 ASSERT(un != NULL); 20320 ASSERT(!mutex_owned(SD_MUTEX(un))); 20321 ASSERT(bufaddr != NULL); 20322 ASSERT(ucmdbuf != NULL); 20323 ASSERT(rqbuf != NULL); 20324 20325 SD_TRACE(SD_LOG_IO, un, 20326 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20327 20328 bzero(cdb, sizeof (cdb)); 20329 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20330 bzero(rqbuf, rqbuflen); 20331 bzero(bufaddr, buflen); 20332 20333 /* 20334 * Set up cdb field for the get configuration command. 20335 */ 20336 cdb[0] = SCMD_GET_CONFIGURATION; 20337 cdb[1] = 0x02; /* Requested Type */ 20338 cdb[3] = feature; 20339 cdb[8] = buflen; 20340 ucmdbuf->uscsi_cdb = cdb; 20341 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20342 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20343 ucmdbuf->uscsi_buflen = buflen; 20344 ucmdbuf->uscsi_timeout = sd_io_time; 20345 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20346 ucmdbuf->uscsi_rqlen = rqbuflen; 20347 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20348 20349 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20350 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20351 20352 switch (status) { 20353 case 0: 20354 break; /* Success! */ 20355 case EIO: 20356 switch (ucmdbuf->uscsi_status) { 20357 case STATUS_RESERVATION_CONFLICT: 20358 status = EACCES; 20359 break; 20360 default: 20361 break; 20362 } 20363 break; 20364 default: 20365 break; 20366 } 20367 20368 if (status == 0) { 20369 SD_DUMP_MEMORY(un, SD_LOG_IO, 20370 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20371 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20372 } 20373 20374 SD_TRACE(SD_LOG_IO, un, 20375 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20376 20377 return (status); 20378 } 20379 20380 20381 /* 20382 * Function: sd_send_scsi_MODE_SENSE 20383 * 20384 * Description: Utility function for issuing a scsi MODE SENSE command. 20385 * Note: This routine uses a consistent implementation for Group0, 20386 * Group1, and Group2 commands across all platforms. ATAPI devices 20387 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20388 * 20389 * Arguments: un - pointer to the softstate struct for the target. 20390 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20391 * CDB_GROUP[1|2] (10 byte). 20392 * bufaddr - buffer for page data retrieved from the target. 20393 * buflen - size of page to be retrieved. 20394 * page_code - page code of data to be retrieved from the target. 20395 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20396 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20397 * to use the USCSI "direct" chain and bypass the normal 20398 * command waitq. 20399 * 20400 * Return Code: 0 - Success 20401 * errno return code from sd_send_scsi_cmd() 20402 * 20403 * Context: Can sleep. Does not return until command is completed. 20404 */ 20405 20406 static int 20407 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20408 size_t buflen, uchar_t page_code, int path_flag) 20409 { 20410 struct scsi_extended_sense sense_buf; 20411 union scsi_cdb cdb; 20412 struct uscsi_cmd ucmd_buf; 20413 int status; 20414 20415 ASSERT(un != NULL); 20416 ASSERT(!mutex_owned(SD_MUTEX(un))); 20417 ASSERT(bufaddr != NULL); 20418 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20419 (cdbsize == CDB_GROUP2)); 20420 20421 SD_TRACE(SD_LOG_IO, un, 20422 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20423 20424 bzero(&cdb, sizeof (cdb)); 20425 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20426 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20427 bzero(bufaddr, buflen); 20428 20429 if (cdbsize == CDB_GROUP0) { 20430 cdb.scc_cmd = SCMD_MODE_SENSE; 20431 cdb.cdb_opaque[2] = page_code; 20432 FORMG0COUNT(&cdb, buflen); 20433 } else { 20434 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20435 cdb.cdb_opaque[2] = page_code; 20436 FORMG1COUNT(&cdb, buflen); 20437 } 20438 20439 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20440 20441 ucmd_buf.uscsi_cdb = (char *)&cdb; 20442 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20443 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20444 ucmd_buf.uscsi_buflen = buflen; 20445 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20446 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20447 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20448 ucmd_buf.uscsi_timeout = 60; 20449 20450 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20451 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20452 20453 switch (status) { 20454 case 0: 20455 break; /* Success! */ 20456 case EIO: 20457 switch (ucmd_buf.uscsi_status) { 20458 case STATUS_RESERVATION_CONFLICT: 20459 status = EACCES; 20460 break; 20461 default: 20462 break; 20463 } 20464 break; 20465 default: 20466 break; 20467 } 20468 20469 if (status == 0) { 20470 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20471 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20472 } 20473 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20474 20475 return (status); 20476 } 20477 20478 20479 /* 20480 * Function: sd_send_scsi_MODE_SELECT 20481 * 20482 * Description: Utility function for issuing a scsi MODE SELECT command. 20483 * Note: This routine uses a consistent implementation for Group0, 20484 * Group1, and Group2 commands across all platforms. ATAPI devices 20485 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20486 * 20487 * Arguments: un - pointer to the softstate struct for the target. 20488 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20489 * CDB_GROUP[1|2] (10 byte). 20490 * bufaddr - buffer for page data retrieved from the target. 20491 * buflen - size of page to be retrieved. 20492 * save_page - boolean to determin if SP bit should be set. 20493 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20494 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20495 * to use the USCSI "direct" chain and bypass the normal 20496 * command waitq. 20497 * 20498 * Return Code: 0 - Success 20499 * errno return code from sd_send_scsi_cmd() 20500 * 20501 * Context: Can sleep. Does not return until command is completed. 20502 */ 20503 20504 static int 20505 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20506 size_t buflen, uchar_t save_page, int path_flag) 20507 { 20508 struct scsi_extended_sense sense_buf; 20509 union scsi_cdb cdb; 20510 struct uscsi_cmd ucmd_buf; 20511 int status; 20512 20513 ASSERT(un != NULL); 20514 ASSERT(!mutex_owned(SD_MUTEX(un))); 20515 ASSERT(bufaddr != NULL); 20516 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20517 (cdbsize == CDB_GROUP2)); 20518 20519 SD_TRACE(SD_LOG_IO, un, 20520 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20521 20522 bzero(&cdb, sizeof (cdb)); 20523 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20524 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20525 20526 /* Set the PF bit for many third party drives */ 20527 cdb.cdb_opaque[1] = 0x10; 20528 20529 /* Set the savepage(SP) bit if given */ 20530 if (save_page == SD_SAVE_PAGE) { 20531 cdb.cdb_opaque[1] |= 0x01; 20532 } 20533 20534 if (cdbsize == CDB_GROUP0) { 20535 cdb.scc_cmd = SCMD_MODE_SELECT; 20536 FORMG0COUNT(&cdb, buflen); 20537 } else { 20538 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20539 FORMG1COUNT(&cdb, buflen); 20540 } 20541 20542 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20543 20544 ucmd_buf.uscsi_cdb = (char *)&cdb; 20545 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20546 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20547 ucmd_buf.uscsi_buflen = buflen; 20548 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20549 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20550 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20551 ucmd_buf.uscsi_timeout = 60; 20552 20553 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20554 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20555 20556 switch (status) { 20557 case 0: 20558 break; /* Success! */ 20559 case EIO: 20560 switch (ucmd_buf.uscsi_status) { 20561 case STATUS_RESERVATION_CONFLICT: 20562 status = EACCES; 20563 break; 20564 default: 20565 break; 20566 } 20567 break; 20568 default: 20569 break; 20570 } 20571 20572 if (status == 0) { 20573 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20574 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20575 } 20576 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20577 20578 return (status); 20579 } 20580 20581 20582 /* 20583 * Function: sd_send_scsi_RDWR 20584 * 20585 * Description: Issue a scsi READ or WRITE command with the given parameters. 20586 * 20587 * Arguments: un: Pointer to the sd_lun struct for the target. 20588 * cmd: SCMD_READ or SCMD_WRITE 20589 * bufaddr: Address of caller's buffer to receive the RDWR data 20590 * buflen: Length of caller's buffer receive the RDWR data. 20591 * start_block: Block number for the start of the RDWR operation. 20592 * (Assumes target-native block size.) 20593 * residp: Pointer to variable to receive the redisual of the 20594 * RDWR operation (may be NULL of no residual requested). 20595 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20596 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20597 * to use the USCSI "direct" chain and bypass the normal 20598 * command waitq. 20599 * 20600 * Return Code: 0 - Success 20601 * errno return code from sd_send_scsi_cmd() 20602 * 20603 * Context: Can sleep. Does not return until command is completed. 20604 */ 20605 20606 static int 20607 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20608 size_t buflen, daddr_t start_block, int path_flag) 20609 { 20610 struct scsi_extended_sense sense_buf; 20611 union scsi_cdb cdb; 20612 struct uscsi_cmd ucmd_buf; 20613 uint32_t block_count; 20614 int status; 20615 int cdbsize; 20616 uchar_t flag; 20617 20618 ASSERT(un != NULL); 20619 ASSERT(!mutex_owned(SD_MUTEX(un))); 20620 ASSERT(bufaddr != NULL); 20621 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20622 20623 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20624 20625 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20626 return (EINVAL); 20627 } 20628 20629 mutex_enter(SD_MUTEX(un)); 20630 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20631 mutex_exit(SD_MUTEX(un)); 20632 20633 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20634 20635 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20636 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20637 bufaddr, buflen, start_block, block_count); 20638 20639 bzero(&cdb, sizeof (cdb)); 20640 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20641 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20642 20643 /* Compute CDB size to use */ 20644 if (start_block > 0xffffffff) 20645 cdbsize = CDB_GROUP4; 20646 else if ((start_block & 0xFFE00000) || 20647 (un->un_f_cfg_is_atapi == TRUE)) 20648 cdbsize = CDB_GROUP1; 20649 else 20650 cdbsize = CDB_GROUP0; 20651 20652 switch (cdbsize) { 20653 case CDB_GROUP0: /* 6-byte CDBs */ 20654 cdb.scc_cmd = cmd; 20655 FORMG0ADDR(&cdb, start_block); 20656 FORMG0COUNT(&cdb, block_count); 20657 break; 20658 case CDB_GROUP1: /* 10-byte CDBs */ 20659 cdb.scc_cmd = cmd | SCMD_GROUP1; 20660 FORMG1ADDR(&cdb, start_block); 20661 FORMG1COUNT(&cdb, block_count); 20662 break; 20663 case CDB_GROUP4: /* 16-byte CDBs */ 20664 cdb.scc_cmd = cmd | SCMD_GROUP4; 20665 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20666 FORMG4COUNT(&cdb, block_count); 20667 break; 20668 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20669 default: 20670 /* All others reserved */ 20671 return (EINVAL); 20672 } 20673 20674 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20675 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20676 20677 ucmd_buf.uscsi_cdb = (char *)&cdb; 20678 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20679 ucmd_buf.uscsi_bufaddr = bufaddr; 20680 ucmd_buf.uscsi_buflen = buflen; 20681 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20682 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20683 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20684 ucmd_buf.uscsi_timeout = 60; 20685 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20686 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20687 switch (status) { 20688 case 0: 20689 break; /* Success! */ 20690 case EIO: 20691 switch (ucmd_buf.uscsi_status) { 20692 case STATUS_RESERVATION_CONFLICT: 20693 status = EACCES; 20694 break; 20695 default: 20696 break; 20697 } 20698 break; 20699 default: 20700 break; 20701 } 20702 20703 if (status == 0) { 20704 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20705 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20706 } 20707 20708 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20709 20710 return (status); 20711 } 20712 20713 20714 /* 20715 * Function: sd_send_scsi_LOG_SENSE 20716 * 20717 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20718 * 20719 * Arguments: un: Pointer to the sd_lun struct for the target. 20720 * 20721 * Return Code: 0 - Success 20722 * errno return code from sd_send_scsi_cmd() 20723 * 20724 * Context: Can sleep. Does not return until command is completed. 20725 */ 20726 20727 static int 20728 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20729 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20730 int path_flag) 20731 20732 { 20733 struct scsi_extended_sense sense_buf; 20734 union scsi_cdb cdb; 20735 struct uscsi_cmd ucmd_buf; 20736 int status; 20737 20738 ASSERT(un != NULL); 20739 ASSERT(!mutex_owned(SD_MUTEX(un))); 20740 20741 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20742 20743 bzero(&cdb, sizeof (cdb)); 20744 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20745 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20746 20747 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20748 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20749 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20750 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20751 FORMG1COUNT(&cdb, buflen); 20752 20753 ucmd_buf.uscsi_cdb = (char *)&cdb; 20754 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20755 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20756 ucmd_buf.uscsi_buflen = buflen; 20757 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20758 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20759 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20760 ucmd_buf.uscsi_timeout = 60; 20761 20762 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20763 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20764 20765 switch (status) { 20766 case 0: 20767 break; 20768 case EIO: 20769 switch (ucmd_buf.uscsi_status) { 20770 case STATUS_RESERVATION_CONFLICT: 20771 status = EACCES; 20772 break; 20773 case STATUS_CHECK: 20774 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20775 (sense_buf.es_key == KEY_ILLEGAL_REQUEST) && 20776 (sense_buf.es_add_code == 0x24)) { 20777 /* 20778 * ASC 0x24: INVALID FIELD IN CDB 20779 */ 20780 switch (page_code) { 20781 case START_STOP_CYCLE_PAGE: 20782 /* 20783 * The start stop cycle counter is 20784 * implemented as page 0x31 in earlier 20785 * generation disks. In new generation 20786 * disks the start stop cycle counter is 20787 * implemented as page 0xE. To properly 20788 * handle this case if an attempt for 20789 * log page 0xE is made and fails we 20790 * will try again using page 0x31. 20791 * 20792 * Network storage BU committed to 20793 * maintain the page 0x31 for this 20794 * purpose and will not have any other 20795 * page implemented with page code 0x31 20796 * until all disks transition to the 20797 * standard page. 20798 */ 20799 mutex_enter(SD_MUTEX(un)); 20800 un->un_start_stop_cycle_page = 20801 START_STOP_CYCLE_VU_PAGE; 20802 cdb.cdb_opaque[2] = 20803 (char)(page_control << 6) | 20804 un->un_start_stop_cycle_page; 20805 mutex_exit(SD_MUTEX(un)); 20806 status = sd_send_scsi_cmd( 20807 SD_GET_DEV(un), &ucmd_buf, 20808 UIO_SYSSPACE, UIO_SYSSPACE, 20809 UIO_SYSSPACE, path_flag); 20810 20811 break; 20812 case TEMPERATURE_PAGE: 20813 status = ENOTTY; 20814 break; 20815 default: 20816 break; 20817 } 20818 } 20819 break; 20820 default: 20821 break; 20822 } 20823 break; 20824 default: 20825 break; 20826 } 20827 20828 if (status == 0) { 20829 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20830 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20831 } 20832 20833 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20834 20835 return (status); 20836 } 20837 20838 20839 /* 20840 * Function: sdioctl 20841 * 20842 * Description: Driver's ioctl(9e) entry point function. 20843 * 20844 * Arguments: dev - device number 20845 * cmd - ioctl operation to be performed 20846 * arg - user argument, contains data to be set or reference 20847 * parameter for get 20848 * flag - bit flag, indicating open settings, 32/64 bit type 20849 * cred_p - user credential pointer 20850 * rval_p - calling process return value (OPT) 20851 * 20852 * Return Code: EINVAL 20853 * ENOTTY 20854 * ENXIO 20855 * EIO 20856 * EFAULT 20857 * ENOTSUP 20858 * EPERM 20859 * 20860 * Context: Called from the device switch at normal priority. 20861 */ 20862 20863 static int 20864 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20865 { 20866 struct sd_lun *un = NULL; 20867 int geom_validated = FALSE; 20868 int err = 0; 20869 int i = 0; 20870 cred_t *cr; 20871 20872 /* 20873 * All device accesses go thru sdstrategy where we check on suspend 20874 * status 20875 */ 20876 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20877 return (ENXIO); 20878 } 20879 20880 ASSERT(!mutex_owned(SD_MUTEX(un))); 20881 20882 /* 20883 * Moved this wait from sd_uscsi_strategy to here for 20884 * reasons of deadlock prevention. Internal driver commands, 20885 * specifically those to change a devices power level, result 20886 * in a call to sd_uscsi_strategy. 20887 */ 20888 mutex_enter(SD_MUTEX(un)); 20889 while ((un->un_state == SD_STATE_SUSPENDED) || 20890 (un->un_state == SD_STATE_PM_CHANGING)) { 20891 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20892 } 20893 /* 20894 * Twiddling the counter here protects commands from now 20895 * through to the top of sd_uscsi_strategy. Without the 20896 * counter inc. a power down, for example, could get in 20897 * after the above check for state is made and before 20898 * execution gets to the top of sd_uscsi_strategy. 20899 * That would cause problems. 20900 */ 20901 un->un_ncmds_in_driver++; 20902 20903 if ((un->un_f_geometry_is_valid == FALSE) && 20904 (flag & (FNDELAY | FNONBLOCK))) { 20905 switch (cmd) { 20906 case CDROMPAUSE: 20907 case CDROMRESUME: 20908 case CDROMPLAYMSF: 20909 case CDROMPLAYTRKIND: 20910 case CDROMREADTOCHDR: 20911 case CDROMREADTOCENTRY: 20912 case CDROMSTOP: 20913 case CDROMSTART: 20914 case CDROMVOLCTRL: 20915 case CDROMSUBCHNL: 20916 case CDROMREADMODE2: 20917 case CDROMREADMODE1: 20918 case CDROMREADOFFSET: 20919 case CDROMSBLKMODE: 20920 case CDROMGBLKMODE: 20921 case CDROMGDRVSPEED: 20922 case CDROMSDRVSPEED: 20923 case CDROMCDDA: 20924 case CDROMCDXA: 20925 case CDROMSUBCODE: 20926 if (!ISCD(un)) { 20927 un->un_ncmds_in_driver--; 20928 ASSERT(un->un_ncmds_in_driver >= 0); 20929 mutex_exit(SD_MUTEX(un)); 20930 return (ENOTTY); 20931 } 20932 break; 20933 case FDEJECT: 20934 case DKIOCEJECT: 20935 case CDROMEJECT: 20936 if (!un->un_f_eject_media_supported) { 20937 un->un_ncmds_in_driver--; 20938 ASSERT(un->un_ncmds_in_driver >= 0); 20939 mutex_exit(SD_MUTEX(un)); 20940 return (ENOTTY); 20941 } 20942 break; 20943 case DKIOCSVTOC: 20944 case DKIOCSETEFI: 20945 case DKIOCSMBOOT: 20946 case DKIOCFLUSHWRITECACHE: 20947 mutex_exit(SD_MUTEX(un)); 20948 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 20949 if (err != 0) { 20950 mutex_enter(SD_MUTEX(un)); 20951 un->un_ncmds_in_driver--; 20952 ASSERT(un->un_ncmds_in_driver >= 0); 20953 mutex_exit(SD_MUTEX(un)); 20954 return (EIO); 20955 } 20956 mutex_enter(SD_MUTEX(un)); 20957 /* FALLTHROUGH */ 20958 case DKIOCREMOVABLE: 20959 case DKIOCHOTPLUGGABLE: 20960 case DKIOCINFO: 20961 case DKIOCGMEDIAINFO: 20962 case MHIOCENFAILFAST: 20963 case MHIOCSTATUS: 20964 case MHIOCTKOWN: 20965 case MHIOCRELEASE: 20966 case MHIOCGRP_INKEYS: 20967 case MHIOCGRP_INRESV: 20968 case MHIOCGRP_REGISTER: 20969 case MHIOCGRP_RESERVE: 20970 case MHIOCGRP_PREEMPTANDABORT: 20971 case MHIOCGRP_REGISTERANDIGNOREKEY: 20972 case CDROMCLOSETRAY: 20973 case USCSICMD: 20974 goto skip_ready_valid; 20975 default: 20976 break; 20977 } 20978 20979 mutex_exit(SD_MUTEX(un)); 20980 err = sd_ready_and_valid(un); 20981 mutex_enter(SD_MUTEX(un)); 20982 if (err == SD_READY_NOT_VALID) { 20983 switch (cmd) { 20984 case DKIOCGAPART: 20985 case DKIOCGGEOM: 20986 case DKIOCSGEOM: 20987 case DKIOCGVTOC: 20988 case DKIOCSVTOC: 20989 case DKIOCSAPART: 20990 case DKIOCG_PHYGEOM: 20991 case DKIOCG_VIRTGEOM: 20992 err = ENOTSUP; 20993 un->un_ncmds_in_driver--; 20994 ASSERT(un->un_ncmds_in_driver >= 0); 20995 mutex_exit(SD_MUTEX(un)); 20996 return (err); 20997 } 20998 } 20999 if (err != SD_READY_VALID) { 21000 switch (cmd) { 21001 case DKIOCSTATE: 21002 case CDROMGDRVSPEED: 21003 case CDROMSDRVSPEED: 21004 case FDEJECT: /* for eject command */ 21005 case DKIOCEJECT: 21006 case CDROMEJECT: 21007 case DKIOCGETEFI: 21008 case DKIOCSGEOM: 21009 case DKIOCREMOVABLE: 21010 case DKIOCHOTPLUGGABLE: 21011 case DKIOCSAPART: 21012 case DKIOCSETEFI: 21013 break; 21014 default: 21015 if (un->un_f_has_removable_media) { 21016 err = ENXIO; 21017 } else { 21018 /* Do not map EACCES to EIO */ 21019 if (err != EACCES) 21020 err = EIO; 21021 } 21022 un->un_ncmds_in_driver--; 21023 ASSERT(un->un_ncmds_in_driver >= 0); 21024 mutex_exit(SD_MUTEX(un)); 21025 return (err); 21026 } 21027 } 21028 geom_validated = TRUE; 21029 } 21030 if ((un->un_f_geometry_is_valid == TRUE) && 21031 (un->un_solaris_size > 0)) { 21032 /* 21033 * the "geometry_is_valid" flag could be true if we 21034 * have an fdisk table but no Solaris partition 21035 */ 21036 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21037 /* it is EFI, so return ENOTSUP for these */ 21038 switch (cmd) { 21039 case DKIOCGAPART: 21040 case DKIOCGGEOM: 21041 case DKIOCGVTOC: 21042 case DKIOCSVTOC: 21043 case DKIOCSAPART: 21044 err = ENOTSUP; 21045 un->un_ncmds_in_driver--; 21046 ASSERT(un->un_ncmds_in_driver >= 0); 21047 mutex_exit(SD_MUTEX(un)); 21048 return (err); 21049 } 21050 } 21051 } 21052 21053 skip_ready_valid: 21054 mutex_exit(SD_MUTEX(un)); 21055 21056 switch (cmd) { 21057 case DKIOCINFO: 21058 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21059 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21060 break; 21061 21062 case DKIOCGMEDIAINFO: 21063 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21064 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21065 break; 21066 21067 case DKIOCGGEOM: 21068 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21069 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21070 geom_validated); 21071 break; 21072 21073 case DKIOCSGEOM: 21074 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21075 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21076 break; 21077 21078 case DKIOCGAPART: 21079 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21080 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21081 geom_validated); 21082 break; 21083 21084 case DKIOCSAPART: 21085 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21086 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21087 break; 21088 21089 case DKIOCGVTOC: 21090 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21091 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21092 geom_validated); 21093 break; 21094 21095 case DKIOCGETEFI: 21096 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21097 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21098 break; 21099 21100 case DKIOCPARTITION: 21101 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21102 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21103 break; 21104 21105 case DKIOCSVTOC: 21106 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21107 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21108 break; 21109 21110 case DKIOCSETEFI: 21111 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21112 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21113 break; 21114 21115 case DKIOCGMBOOT: 21116 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21117 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21118 break; 21119 21120 case DKIOCSMBOOT: 21121 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21122 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21123 break; 21124 21125 case DKIOCLOCK: 21126 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21127 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21128 SD_PATH_STANDARD); 21129 break; 21130 21131 case DKIOCUNLOCK: 21132 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21133 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21134 SD_PATH_STANDARD); 21135 break; 21136 21137 case DKIOCSTATE: { 21138 enum dkio_state state; 21139 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21140 21141 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21142 err = EFAULT; 21143 } else { 21144 err = sd_check_media(dev, state); 21145 if (err == 0) { 21146 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21147 sizeof (int), flag) != 0) 21148 err = EFAULT; 21149 } 21150 } 21151 break; 21152 } 21153 21154 case DKIOCREMOVABLE: 21155 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21156 /* 21157 * At present, vold only does automount for removable-media 21158 * devices, in order not to break current applications, we 21159 * still let hopluggable devices pretend to be removable media 21160 * devices for vold. In the near future, once vold is EOL'ed, 21161 * we should remove this workaround. 21162 */ 21163 if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) { 21164 i = 1; 21165 } else { 21166 i = 0; 21167 } 21168 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21169 err = EFAULT; 21170 } else { 21171 err = 0; 21172 } 21173 break; 21174 21175 case DKIOCHOTPLUGGABLE: 21176 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21177 if (un->un_f_is_hotpluggable) { 21178 i = 1; 21179 } else { 21180 i = 0; 21181 } 21182 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21183 err = EFAULT; 21184 } else { 21185 err = 0; 21186 } 21187 break; 21188 21189 case DKIOCGTEMPERATURE: 21190 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21191 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21192 break; 21193 21194 case MHIOCENFAILFAST: 21195 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21196 if ((err = drv_priv(cred_p)) == 0) { 21197 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21198 } 21199 break; 21200 21201 case MHIOCTKOWN: 21202 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21203 if ((err = drv_priv(cred_p)) == 0) { 21204 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21205 } 21206 break; 21207 21208 case MHIOCRELEASE: 21209 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21210 if ((err = drv_priv(cred_p)) == 0) { 21211 err = sd_mhdioc_release(dev); 21212 } 21213 break; 21214 21215 case MHIOCSTATUS: 21216 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21217 if ((err = drv_priv(cred_p)) == 0) { 21218 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21219 case 0: 21220 err = 0; 21221 break; 21222 case EACCES: 21223 *rval_p = 1; 21224 err = 0; 21225 break; 21226 default: 21227 err = EIO; 21228 break; 21229 } 21230 } 21231 break; 21232 21233 case MHIOCQRESERVE: 21234 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21235 if ((err = drv_priv(cred_p)) == 0) { 21236 err = sd_reserve_release(dev, SD_RESERVE); 21237 } 21238 break; 21239 21240 case MHIOCREREGISTERDEVID: 21241 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21242 if (drv_priv(cred_p) == EPERM) { 21243 err = EPERM; 21244 } else if (!un->un_f_devid_supported) { 21245 err = ENOTTY; 21246 } else { 21247 err = sd_mhdioc_register_devid(dev); 21248 } 21249 break; 21250 21251 case MHIOCGRP_INKEYS: 21252 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21253 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21254 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21255 err = ENOTSUP; 21256 } else { 21257 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21258 flag); 21259 } 21260 } 21261 break; 21262 21263 case MHIOCGRP_INRESV: 21264 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21265 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21266 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21267 err = ENOTSUP; 21268 } else { 21269 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21270 } 21271 } 21272 break; 21273 21274 case MHIOCGRP_REGISTER: 21275 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21276 if ((err = drv_priv(cred_p)) != EPERM) { 21277 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21278 err = ENOTSUP; 21279 } else if (arg != NULL) { 21280 mhioc_register_t reg; 21281 if (ddi_copyin((void *)arg, ®, 21282 sizeof (mhioc_register_t), flag) != 0) { 21283 err = EFAULT; 21284 } else { 21285 err = 21286 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21287 un, SD_SCSI3_REGISTER, 21288 (uchar_t *)®); 21289 } 21290 } 21291 } 21292 break; 21293 21294 case MHIOCGRP_RESERVE: 21295 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21296 if ((err = drv_priv(cred_p)) != EPERM) { 21297 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21298 err = ENOTSUP; 21299 } else if (arg != NULL) { 21300 mhioc_resv_desc_t resv_desc; 21301 if (ddi_copyin((void *)arg, &resv_desc, 21302 sizeof (mhioc_resv_desc_t), flag) != 0) { 21303 err = EFAULT; 21304 } else { 21305 err = 21306 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21307 un, SD_SCSI3_RESERVE, 21308 (uchar_t *)&resv_desc); 21309 } 21310 } 21311 } 21312 break; 21313 21314 case MHIOCGRP_PREEMPTANDABORT: 21315 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21316 if ((err = drv_priv(cred_p)) != EPERM) { 21317 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21318 err = ENOTSUP; 21319 } else if (arg != NULL) { 21320 mhioc_preemptandabort_t preempt_abort; 21321 if (ddi_copyin((void *)arg, &preempt_abort, 21322 sizeof (mhioc_preemptandabort_t), 21323 flag) != 0) { 21324 err = EFAULT; 21325 } else { 21326 err = 21327 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21328 un, SD_SCSI3_PREEMPTANDABORT, 21329 (uchar_t *)&preempt_abort); 21330 } 21331 } 21332 } 21333 break; 21334 21335 case MHIOCGRP_REGISTERANDIGNOREKEY: 21336 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21337 if ((err = drv_priv(cred_p)) != EPERM) { 21338 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21339 err = ENOTSUP; 21340 } else if (arg != NULL) { 21341 mhioc_registerandignorekey_t r_and_i; 21342 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21343 sizeof (mhioc_registerandignorekey_t), 21344 flag) != 0) { 21345 err = EFAULT; 21346 } else { 21347 err = 21348 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21349 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21350 (uchar_t *)&r_and_i); 21351 } 21352 } 21353 } 21354 break; 21355 21356 case USCSICMD: 21357 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21358 cr = ddi_get_cred(); 21359 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21360 err = EPERM; 21361 } else { 21362 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21363 } 21364 break; 21365 21366 case CDROMPAUSE: 21367 case CDROMRESUME: 21368 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21369 if (!ISCD(un)) { 21370 err = ENOTTY; 21371 } else { 21372 err = sr_pause_resume(dev, cmd); 21373 } 21374 break; 21375 21376 case CDROMPLAYMSF: 21377 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21378 if (!ISCD(un)) { 21379 err = ENOTTY; 21380 } else { 21381 err = sr_play_msf(dev, (caddr_t)arg, flag); 21382 } 21383 break; 21384 21385 case CDROMPLAYTRKIND: 21386 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21387 #if defined(__i386) || defined(__amd64) 21388 /* 21389 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21390 */ 21391 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21392 #else 21393 if (!ISCD(un)) { 21394 #endif 21395 err = ENOTTY; 21396 } else { 21397 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21398 } 21399 break; 21400 21401 case CDROMREADTOCHDR: 21402 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21403 if (!ISCD(un)) { 21404 err = ENOTTY; 21405 } else { 21406 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21407 } 21408 break; 21409 21410 case CDROMREADTOCENTRY: 21411 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21412 if (!ISCD(un)) { 21413 err = ENOTTY; 21414 } else { 21415 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21416 } 21417 break; 21418 21419 case CDROMSTOP: 21420 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21421 if (!ISCD(un)) { 21422 err = ENOTTY; 21423 } else { 21424 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21425 SD_PATH_STANDARD); 21426 } 21427 break; 21428 21429 case CDROMSTART: 21430 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21431 if (!ISCD(un)) { 21432 err = ENOTTY; 21433 } else { 21434 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21435 SD_PATH_STANDARD); 21436 } 21437 break; 21438 21439 case CDROMCLOSETRAY: 21440 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21441 if (!ISCD(un)) { 21442 err = ENOTTY; 21443 } else { 21444 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21445 SD_PATH_STANDARD); 21446 } 21447 break; 21448 21449 case FDEJECT: /* for eject command */ 21450 case DKIOCEJECT: 21451 case CDROMEJECT: 21452 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21453 if (!un->un_f_eject_media_supported) { 21454 err = ENOTTY; 21455 } else { 21456 err = sr_eject(dev); 21457 } 21458 break; 21459 21460 case CDROMVOLCTRL: 21461 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21462 if (!ISCD(un)) { 21463 err = ENOTTY; 21464 } else { 21465 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21466 } 21467 break; 21468 21469 case CDROMSUBCHNL: 21470 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21471 if (!ISCD(un)) { 21472 err = ENOTTY; 21473 } else { 21474 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21475 } 21476 break; 21477 21478 case CDROMREADMODE2: 21479 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21480 if (!ISCD(un)) { 21481 err = ENOTTY; 21482 } else if (un->un_f_cfg_is_atapi == TRUE) { 21483 /* 21484 * If the drive supports READ CD, use that instead of 21485 * switching the LBA size via a MODE SELECT 21486 * Block Descriptor 21487 */ 21488 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21489 } else { 21490 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21491 } 21492 break; 21493 21494 case CDROMREADMODE1: 21495 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21496 if (!ISCD(un)) { 21497 err = ENOTTY; 21498 } else { 21499 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21500 } 21501 break; 21502 21503 case CDROMREADOFFSET: 21504 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21505 if (!ISCD(un)) { 21506 err = ENOTTY; 21507 } else { 21508 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21509 flag); 21510 } 21511 break; 21512 21513 case CDROMSBLKMODE: 21514 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21515 /* 21516 * There is no means of changing block size in case of atapi 21517 * drives, thus return ENOTTY if drive type is atapi 21518 */ 21519 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21520 err = ENOTTY; 21521 } else if (un->un_f_mmc_cap == TRUE) { 21522 21523 /* 21524 * MMC Devices do not support changing the 21525 * logical block size 21526 * 21527 * Note: EINVAL is being returned instead of ENOTTY to 21528 * maintain consistancy with the original mmc 21529 * driver update. 21530 */ 21531 err = EINVAL; 21532 } else { 21533 mutex_enter(SD_MUTEX(un)); 21534 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21535 (un->un_ncmds_in_transport > 0)) { 21536 mutex_exit(SD_MUTEX(un)); 21537 err = EINVAL; 21538 } else { 21539 mutex_exit(SD_MUTEX(un)); 21540 err = sr_change_blkmode(dev, cmd, arg, flag); 21541 } 21542 } 21543 break; 21544 21545 case CDROMGBLKMODE: 21546 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21547 if (!ISCD(un)) { 21548 err = ENOTTY; 21549 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21550 (un->un_f_blockcount_is_valid != FALSE)) { 21551 /* 21552 * Drive is an ATAPI drive so return target block 21553 * size for ATAPI drives since we cannot change the 21554 * blocksize on ATAPI drives. Used primarily to detect 21555 * if an ATAPI cdrom is present. 21556 */ 21557 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21558 sizeof (int), flag) != 0) { 21559 err = EFAULT; 21560 } else { 21561 err = 0; 21562 } 21563 21564 } else { 21565 /* 21566 * Drive supports changing block sizes via a Mode 21567 * Select. 21568 */ 21569 err = sr_change_blkmode(dev, cmd, arg, flag); 21570 } 21571 break; 21572 21573 case CDROMGDRVSPEED: 21574 case CDROMSDRVSPEED: 21575 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21576 if (!ISCD(un)) { 21577 err = ENOTTY; 21578 } else if (un->un_f_mmc_cap == TRUE) { 21579 /* 21580 * Note: In the future the driver implementation 21581 * for getting and 21582 * setting cd speed should entail: 21583 * 1) If non-mmc try the Toshiba mode page 21584 * (sr_change_speed) 21585 * 2) If mmc but no support for Real Time Streaming try 21586 * the SET CD SPEED (0xBB) command 21587 * (sr_atapi_change_speed) 21588 * 3) If mmc and support for Real Time Streaming 21589 * try the GET PERFORMANCE and SET STREAMING 21590 * commands (not yet implemented, 4380808) 21591 */ 21592 /* 21593 * As per recent MMC spec, CD-ROM speed is variable 21594 * and changes with LBA. Since there is no such 21595 * things as drive speed now, fail this ioctl. 21596 * 21597 * Note: EINVAL is returned for consistancy of original 21598 * implementation which included support for getting 21599 * the drive speed of mmc devices but not setting 21600 * the drive speed. Thus EINVAL would be returned 21601 * if a set request was made for an mmc device. 21602 * We no longer support get or set speed for 21603 * mmc but need to remain consistant with regard 21604 * to the error code returned. 21605 */ 21606 err = EINVAL; 21607 } else if (un->un_f_cfg_is_atapi == TRUE) { 21608 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21609 } else { 21610 err = sr_change_speed(dev, cmd, arg, flag); 21611 } 21612 break; 21613 21614 case CDROMCDDA: 21615 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21616 if (!ISCD(un)) { 21617 err = ENOTTY; 21618 } else { 21619 err = sr_read_cdda(dev, (void *)arg, flag); 21620 } 21621 break; 21622 21623 case CDROMCDXA: 21624 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21625 if (!ISCD(un)) { 21626 err = ENOTTY; 21627 } else { 21628 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21629 } 21630 break; 21631 21632 case CDROMSUBCODE: 21633 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21634 if (!ISCD(un)) { 21635 err = ENOTTY; 21636 } else { 21637 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21638 } 21639 break; 21640 21641 case DKIOCPARTINFO: { 21642 /* 21643 * Return parameters describing the selected disk slice. 21644 * Note: this ioctl is for the intel platform only 21645 */ 21646 #if defined(__i386) || defined(__amd64) 21647 int part; 21648 21649 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21650 part = SDPART(dev); 21651 21652 /* don't check un_solaris_size for pN */ 21653 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21654 err = EIO; 21655 } else { 21656 struct part_info p; 21657 21658 p.p_start = (daddr_t)un->un_offset[part]; 21659 p.p_length = (int)un->un_map[part].dkl_nblk; 21660 #ifdef _MULTI_DATAMODEL 21661 switch (ddi_model_convert_from(flag & FMODELS)) { 21662 case DDI_MODEL_ILP32: 21663 { 21664 struct part_info32 p32; 21665 21666 p32.p_start = (daddr32_t)p.p_start; 21667 p32.p_length = p.p_length; 21668 if (ddi_copyout(&p32, (void *)arg, 21669 sizeof (p32), flag)) 21670 err = EFAULT; 21671 break; 21672 } 21673 21674 case DDI_MODEL_NONE: 21675 { 21676 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21677 flag)) 21678 err = EFAULT; 21679 break; 21680 } 21681 } 21682 #else /* ! _MULTI_DATAMODEL */ 21683 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21684 err = EFAULT; 21685 #endif /* _MULTI_DATAMODEL */ 21686 } 21687 #else 21688 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21689 err = ENOTTY; 21690 #endif 21691 break; 21692 } 21693 21694 case DKIOCG_PHYGEOM: { 21695 /* Return the driver's notion of the media physical geometry */ 21696 #if defined(__i386) || defined(__amd64) 21697 struct dk_geom disk_geom; 21698 struct dk_geom *dkgp = &disk_geom; 21699 21700 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21701 mutex_enter(SD_MUTEX(un)); 21702 21703 if (un->un_g.dkg_nhead != 0 && 21704 un->un_g.dkg_nsect != 0) { 21705 /* 21706 * We succeeded in getting a geometry, but 21707 * right now it is being reported as just the 21708 * Solaris fdisk partition, just like for 21709 * DKIOCGGEOM. We need to change that to be 21710 * correct for the entire disk now. 21711 */ 21712 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21713 dkgp->dkg_acyl = 0; 21714 dkgp->dkg_ncyl = un->un_blockcount / 21715 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21716 } else { 21717 bzero(dkgp, sizeof (struct dk_geom)); 21718 /* 21719 * This disk does not have a Solaris VTOC 21720 * so we must present a physical geometry 21721 * that will remain consistent regardless 21722 * of how the disk is used. This will ensure 21723 * that the geometry does not change regardless 21724 * of the fdisk partition type (ie. EFI, FAT32, 21725 * Solaris, etc). 21726 */ 21727 if (ISCD(un)) { 21728 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21729 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21730 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21731 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21732 } else { 21733 /* 21734 * Invalid un_blockcount can generate invalid 21735 * dk_geom and may result in division by zero 21736 * system failure. Should make sure blockcount 21737 * is valid before using it here. 21738 */ 21739 if (un->un_f_blockcount_is_valid == FALSE) { 21740 mutex_exit(SD_MUTEX(un)); 21741 err = EIO; 21742 21743 break; 21744 } 21745 sd_convert_geometry(un->un_blockcount, dkgp); 21746 dkgp->dkg_acyl = 0; 21747 dkgp->dkg_ncyl = un->un_blockcount / 21748 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21749 } 21750 } 21751 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21752 21753 if (ddi_copyout(dkgp, (void *)arg, 21754 sizeof (struct dk_geom), flag)) { 21755 mutex_exit(SD_MUTEX(un)); 21756 err = EFAULT; 21757 } else { 21758 mutex_exit(SD_MUTEX(un)); 21759 err = 0; 21760 } 21761 #else 21762 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21763 err = ENOTTY; 21764 #endif 21765 break; 21766 } 21767 21768 case DKIOCG_VIRTGEOM: { 21769 /* Return the driver's notion of the media's logical geometry */ 21770 #if defined(__i386) || defined(__amd64) 21771 struct dk_geom disk_geom; 21772 struct dk_geom *dkgp = &disk_geom; 21773 21774 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21775 mutex_enter(SD_MUTEX(un)); 21776 /* 21777 * If there is no HBA geometry available, or 21778 * if the HBA returned us something that doesn't 21779 * really fit into an Int 13/function 8 geometry 21780 * result, just fail the ioctl. See PSARC 1998/313. 21781 */ 21782 if (un->un_lgeom.g_nhead == 0 || 21783 un->un_lgeom.g_nsect == 0 || 21784 un->un_lgeom.g_ncyl > 1024) { 21785 mutex_exit(SD_MUTEX(un)); 21786 err = EINVAL; 21787 } else { 21788 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21789 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21790 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21791 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21792 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21793 21794 if (ddi_copyout(dkgp, (void *)arg, 21795 sizeof (struct dk_geom), flag)) { 21796 mutex_exit(SD_MUTEX(un)); 21797 err = EFAULT; 21798 } else { 21799 mutex_exit(SD_MUTEX(un)); 21800 err = 0; 21801 } 21802 } 21803 #else 21804 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21805 err = ENOTTY; 21806 #endif 21807 break; 21808 } 21809 #ifdef SDDEBUG 21810 /* RESET/ABORTS testing ioctls */ 21811 case DKIOCRESET: { 21812 int reset_level; 21813 21814 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21815 err = EFAULT; 21816 } else { 21817 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21818 "reset_level = 0x%lx\n", reset_level); 21819 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21820 err = 0; 21821 } else { 21822 err = EIO; 21823 } 21824 } 21825 break; 21826 } 21827 21828 case DKIOCABORT: 21829 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21830 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21831 err = 0; 21832 } else { 21833 err = EIO; 21834 } 21835 break; 21836 #endif 21837 21838 #ifdef SD_FAULT_INJECTION 21839 /* SDIOC FaultInjection testing ioctls */ 21840 case SDIOCSTART: 21841 case SDIOCSTOP: 21842 case SDIOCINSERTPKT: 21843 case SDIOCINSERTXB: 21844 case SDIOCINSERTUN: 21845 case SDIOCINSERTARQ: 21846 case SDIOCPUSH: 21847 case SDIOCRETRIEVE: 21848 case SDIOCRUN: 21849 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21850 "SDIOC detected cmd:0x%X:\n", cmd); 21851 /* call error generator */ 21852 sd_faultinjection_ioctl(cmd, arg, un); 21853 err = 0; 21854 break; 21855 21856 #endif /* SD_FAULT_INJECTION */ 21857 21858 case DKIOCFLUSHWRITECACHE: 21859 { 21860 struct dk_callback *dkc = (struct dk_callback *)arg; 21861 21862 mutex_enter(SD_MUTEX(un)); 21863 if (!un->un_f_sync_cache_supported || 21864 !un->un_f_write_cache_enabled) { 21865 err = un->un_f_sync_cache_supported ? 21866 0 : ENOTSUP; 21867 mutex_exit(SD_MUTEX(un)); 21868 if ((flag & FKIOCTL) && dkc != NULL && 21869 dkc->dkc_callback != NULL) { 21870 (*dkc->dkc_callback)(dkc->dkc_cookie, 21871 err); 21872 /* 21873 * Did callback and reported error. 21874 * Since we did a callback, ioctl 21875 * should return 0. 21876 */ 21877 err = 0; 21878 } 21879 break; 21880 } 21881 mutex_exit(SD_MUTEX(un)); 21882 21883 if ((flag & FKIOCTL) && dkc != NULL && 21884 dkc->dkc_callback != NULL) { 21885 /* async SYNC CACHE request */ 21886 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 21887 } else { 21888 /* synchronous SYNC CACHE request */ 21889 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21890 } 21891 } 21892 break; 21893 21894 case DKIOCGETWCE: { 21895 21896 int wce; 21897 21898 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 21899 break; 21900 } 21901 21902 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 21903 err = EFAULT; 21904 } 21905 break; 21906 } 21907 21908 case DKIOCSETWCE: { 21909 21910 int wce, sync_supported; 21911 21912 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 21913 err = EFAULT; 21914 break; 21915 } 21916 21917 /* 21918 * Synchronize multiple threads trying to enable 21919 * or disable the cache via the un_f_wcc_cv 21920 * condition variable. 21921 */ 21922 mutex_enter(SD_MUTEX(un)); 21923 21924 /* 21925 * Don't allow the cache to be enabled if the 21926 * config file has it disabled. 21927 */ 21928 if (un->un_f_opt_disable_cache && wce) { 21929 mutex_exit(SD_MUTEX(un)); 21930 err = EINVAL; 21931 break; 21932 } 21933 21934 /* 21935 * Wait for write cache change in progress 21936 * bit to be clear before proceeding. 21937 */ 21938 while (un->un_f_wcc_inprog) 21939 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 21940 21941 un->un_f_wcc_inprog = 1; 21942 21943 if (un->un_f_write_cache_enabled && wce == 0) { 21944 /* 21945 * Disable the write cache. Don't clear 21946 * un_f_write_cache_enabled until after 21947 * the mode select and flush are complete. 21948 */ 21949 sync_supported = un->un_f_sync_cache_supported; 21950 mutex_exit(SD_MUTEX(un)); 21951 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 21952 SD_CACHE_DISABLE)) == 0 && sync_supported) { 21953 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21954 } 21955 21956 mutex_enter(SD_MUTEX(un)); 21957 if (err == 0) { 21958 un->un_f_write_cache_enabled = 0; 21959 } 21960 21961 } else if (!un->un_f_write_cache_enabled && wce != 0) { 21962 /* 21963 * Set un_f_write_cache_enabled first, so there is 21964 * no window where the cache is enabled, but the 21965 * bit says it isn't. 21966 */ 21967 un->un_f_write_cache_enabled = 1; 21968 mutex_exit(SD_MUTEX(un)); 21969 21970 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 21971 SD_CACHE_ENABLE); 21972 21973 mutex_enter(SD_MUTEX(un)); 21974 21975 if (err) { 21976 un->un_f_write_cache_enabled = 0; 21977 } 21978 } 21979 21980 un->un_f_wcc_inprog = 0; 21981 cv_broadcast(&un->un_wcc_cv); 21982 mutex_exit(SD_MUTEX(un)); 21983 break; 21984 } 21985 21986 default: 21987 err = ENOTTY; 21988 break; 21989 } 21990 mutex_enter(SD_MUTEX(un)); 21991 un->un_ncmds_in_driver--; 21992 ASSERT(un->un_ncmds_in_driver >= 0); 21993 mutex_exit(SD_MUTEX(un)); 21994 21995 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 21996 return (err); 21997 } 21998 21999 22000 /* 22001 * Function: sd_uscsi_ioctl 22002 * 22003 * Description: This routine is the driver entry point for handling USCSI ioctl 22004 * requests (USCSICMD). 22005 * 22006 * Arguments: dev - the device number 22007 * arg - user provided scsi command 22008 * flag - this argument is a pass through to ddi_copyxxx() 22009 * directly from the mode argument of ioctl(). 22010 * 22011 * Return Code: code returned by sd_send_scsi_cmd 22012 * ENXIO 22013 * EFAULT 22014 * EAGAIN 22015 */ 22016 22017 static int 22018 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22019 { 22020 #ifdef _MULTI_DATAMODEL 22021 /* 22022 * For use when a 32 bit app makes a call into a 22023 * 64 bit ioctl 22024 */ 22025 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22026 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22027 model_t model; 22028 #endif /* _MULTI_DATAMODEL */ 22029 struct uscsi_cmd *scmd = NULL; 22030 struct sd_lun *un = NULL; 22031 enum uio_seg uioseg; 22032 char cdb[CDB_GROUP0]; 22033 int rval = 0; 22034 22035 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22036 return (ENXIO); 22037 } 22038 22039 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22040 22041 scmd = (struct uscsi_cmd *) 22042 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22043 22044 #ifdef _MULTI_DATAMODEL 22045 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22046 case DDI_MODEL_ILP32: 22047 { 22048 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22049 rval = EFAULT; 22050 goto done; 22051 } 22052 /* 22053 * Convert the ILP32 uscsi data from the 22054 * application to LP64 for internal use. 22055 */ 22056 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22057 break; 22058 } 22059 case DDI_MODEL_NONE: 22060 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22061 rval = EFAULT; 22062 goto done; 22063 } 22064 break; 22065 } 22066 #else /* ! _MULTI_DATAMODEL */ 22067 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22068 rval = EFAULT; 22069 goto done; 22070 } 22071 #endif /* _MULTI_DATAMODEL */ 22072 22073 scmd->uscsi_flags &= ~USCSI_NOINTR; 22074 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22075 if (un->un_f_format_in_progress == TRUE) { 22076 rval = EAGAIN; 22077 goto done; 22078 } 22079 22080 /* 22081 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22082 * we will have a valid cdb[0] to test. 22083 */ 22084 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22085 (cdb[0] == SCMD_FORMAT)) { 22086 SD_TRACE(SD_LOG_IOCTL, un, 22087 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22088 mutex_enter(SD_MUTEX(un)); 22089 un->un_f_format_in_progress = TRUE; 22090 mutex_exit(SD_MUTEX(un)); 22091 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22092 SD_PATH_STANDARD); 22093 mutex_enter(SD_MUTEX(un)); 22094 un->un_f_format_in_progress = FALSE; 22095 mutex_exit(SD_MUTEX(un)); 22096 } else { 22097 SD_TRACE(SD_LOG_IOCTL, un, 22098 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22099 /* 22100 * It's OK to fall into here even if the ddi_copyin() 22101 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22102 * does this same copyin and will return the EFAULT 22103 * if it fails. 22104 */ 22105 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22106 SD_PATH_STANDARD); 22107 } 22108 #ifdef _MULTI_DATAMODEL 22109 switch (model) { 22110 case DDI_MODEL_ILP32: 22111 /* 22112 * Convert back to ILP32 before copyout to the 22113 * application 22114 */ 22115 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22116 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22117 if (rval != 0) { 22118 rval = EFAULT; 22119 } 22120 } 22121 break; 22122 case DDI_MODEL_NONE: 22123 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22124 if (rval != 0) { 22125 rval = EFAULT; 22126 } 22127 } 22128 break; 22129 } 22130 #else /* ! _MULTI_DATAMODE */ 22131 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22132 if (rval != 0) { 22133 rval = EFAULT; 22134 } 22135 } 22136 #endif /* _MULTI_DATAMODE */ 22137 done: 22138 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22139 22140 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22141 22142 return (rval); 22143 } 22144 22145 22146 /* 22147 * Function: sd_dkio_ctrl_info 22148 * 22149 * Description: This routine is the driver entry point for handling controller 22150 * information ioctl requests (DKIOCINFO). 22151 * 22152 * Arguments: dev - the device number 22153 * arg - pointer to user provided dk_cinfo structure 22154 * specifying the controller type and attributes. 22155 * flag - this argument is a pass through to ddi_copyxxx() 22156 * directly from the mode argument of ioctl(). 22157 * 22158 * Return Code: 0 22159 * EFAULT 22160 * ENXIO 22161 */ 22162 22163 static int 22164 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22165 { 22166 struct sd_lun *un = NULL; 22167 struct dk_cinfo *info; 22168 dev_info_t *pdip; 22169 int lun, tgt; 22170 22171 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22172 return (ENXIO); 22173 } 22174 22175 info = (struct dk_cinfo *) 22176 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22177 22178 switch (un->un_ctype) { 22179 case CTYPE_CDROM: 22180 info->dki_ctype = DKC_CDROM; 22181 break; 22182 default: 22183 info->dki_ctype = DKC_SCSI_CCS; 22184 break; 22185 } 22186 pdip = ddi_get_parent(SD_DEVINFO(un)); 22187 info->dki_cnum = ddi_get_instance(pdip); 22188 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22189 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22190 } else { 22191 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22192 DK_DEVLEN - 1); 22193 } 22194 22195 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22196 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22197 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22198 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22199 22200 /* Unit Information */ 22201 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22202 info->dki_slave = ((tgt << 3) | lun); 22203 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22204 DK_DEVLEN - 1); 22205 info->dki_flags = DKI_FMTVOL; 22206 info->dki_partition = SDPART(dev); 22207 22208 /* Max Transfer size of this device in blocks */ 22209 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22210 info->dki_addr = 0; 22211 info->dki_space = 0; 22212 info->dki_prio = 0; 22213 info->dki_vec = 0; 22214 22215 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22216 kmem_free(info, sizeof (struct dk_cinfo)); 22217 return (EFAULT); 22218 } else { 22219 kmem_free(info, sizeof (struct dk_cinfo)); 22220 return (0); 22221 } 22222 } 22223 22224 22225 /* 22226 * Function: sd_get_media_info 22227 * 22228 * Description: This routine is the driver entry point for handling ioctl 22229 * requests for the media type or command set profile used by the 22230 * drive to operate on the media (DKIOCGMEDIAINFO). 22231 * 22232 * Arguments: dev - the device number 22233 * arg - pointer to user provided dk_minfo structure 22234 * specifying the media type, logical block size and 22235 * drive capacity. 22236 * flag - this argument is a pass through to ddi_copyxxx() 22237 * directly from the mode argument of ioctl(). 22238 * 22239 * Return Code: 0 22240 * EACCESS 22241 * EFAULT 22242 * ENXIO 22243 * EIO 22244 */ 22245 22246 static int 22247 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22248 { 22249 struct sd_lun *un = NULL; 22250 struct uscsi_cmd com; 22251 struct scsi_inquiry *sinq; 22252 struct dk_minfo media_info; 22253 u_longlong_t media_capacity; 22254 uint64_t capacity; 22255 uint_t lbasize; 22256 uchar_t *out_data; 22257 uchar_t *rqbuf; 22258 int rval = 0; 22259 int rtn; 22260 22261 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22262 (un->un_state == SD_STATE_OFFLINE)) { 22263 return (ENXIO); 22264 } 22265 22266 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22267 22268 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22269 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22270 22271 /* Issue a TUR to determine if the drive is ready with media present */ 22272 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22273 if (rval == ENXIO) { 22274 goto done; 22275 } 22276 22277 /* Now get configuration data */ 22278 if (ISCD(un)) { 22279 media_info.dki_media_type = DK_CDROM; 22280 22281 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22282 if (un->un_f_mmc_cap == TRUE) { 22283 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22284 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22285 22286 if (rtn) { 22287 /* 22288 * Failed for other than an illegal request 22289 * or command not supported 22290 */ 22291 if ((com.uscsi_status == STATUS_CHECK) && 22292 (com.uscsi_rqstatus == STATUS_GOOD)) { 22293 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22294 (rqbuf[12] != 0x20)) { 22295 rval = EIO; 22296 goto done; 22297 } 22298 } 22299 } else { 22300 /* 22301 * The GET CONFIGURATION command succeeded 22302 * so set the media type according to the 22303 * returned data 22304 */ 22305 media_info.dki_media_type = out_data[6]; 22306 media_info.dki_media_type <<= 8; 22307 media_info.dki_media_type |= out_data[7]; 22308 } 22309 } 22310 } else { 22311 /* 22312 * The profile list is not available, so we attempt to identify 22313 * the media type based on the inquiry data 22314 */ 22315 sinq = un->un_sd->sd_inq; 22316 if (sinq->inq_qual == 0) { 22317 /* This is a direct access device */ 22318 media_info.dki_media_type = DK_FIXED_DISK; 22319 22320 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22321 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22322 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22323 media_info.dki_media_type = DK_ZIP; 22324 } else if ( 22325 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22326 media_info.dki_media_type = DK_JAZ; 22327 } 22328 } 22329 } else { 22330 /* Not a CD or direct access so return unknown media */ 22331 media_info.dki_media_type = DK_UNKNOWN; 22332 } 22333 } 22334 22335 /* Now read the capacity so we can provide the lbasize and capacity */ 22336 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22337 SD_PATH_DIRECT)) { 22338 case 0: 22339 break; 22340 case EACCES: 22341 rval = EACCES; 22342 goto done; 22343 default: 22344 rval = EIO; 22345 goto done; 22346 } 22347 22348 media_info.dki_lbsize = lbasize; 22349 media_capacity = capacity; 22350 22351 /* 22352 * sd_send_scsi_READ_CAPACITY() reports capacity in 22353 * un->un_sys_blocksize chunks. So we need to convert it into 22354 * cap.lbasize chunks. 22355 */ 22356 media_capacity *= un->un_sys_blocksize; 22357 media_capacity /= lbasize; 22358 media_info.dki_capacity = media_capacity; 22359 22360 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22361 rval = EFAULT; 22362 /* Put goto. Anybody might add some code below in future */ 22363 goto done; 22364 } 22365 done: 22366 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22367 kmem_free(rqbuf, SENSE_LENGTH); 22368 return (rval); 22369 } 22370 22371 22372 /* 22373 * Function: sd_dkio_get_geometry 22374 * 22375 * Description: This routine is the driver entry point for handling user 22376 * requests to get the device geometry (DKIOCGGEOM). 22377 * 22378 * Arguments: dev - the device number 22379 * arg - pointer to user provided dk_geom structure specifying 22380 * the controller's notion of the current geometry. 22381 * flag - this argument is a pass through to ddi_copyxxx() 22382 * directly from the mode argument of ioctl(). 22383 * geom_validated - flag indicating if the device geometry has been 22384 * previously validated in the sdioctl routine. 22385 * 22386 * Return Code: 0 22387 * EFAULT 22388 * ENXIO 22389 * EIO 22390 */ 22391 22392 static int 22393 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22394 { 22395 struct sd_lun *un = NULL; 22396 struct dk_geom *tmp_geom = NULL; 22397 int rval = 0; 22398 22399 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22400 return (ENXIO); 22401 } 22402 22403 if (geom_validated == FALSE) { 22404 /* 22405 * sd_validate_geometry does not spin a disk up 22406 * if it was spun down. We need to make sure it 22407 * is ready. 22408 */ 22409 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22410 return (rval); 22411 } 22412 mutex_enter(SD_MUTEX(un)); 22413 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22414 mutex_exit(SD_MUTEX(un)); 22415 } 22416 if (rval) 22417 return (rval); 22418 22419 /* 22420 * It is possible that un_solaris_size is 0(uninitialized) 22421 * after sd_unit_attach. Reservation conflict may cause the 22422 * above situation. Thus, the zero check of un_solaris_size 22423 * should occur after the sd_validate_geometry() call. 22424 */ 22425 #if defined(__i386) || defined(__amd64) 22426 if (un->un_solaris_size == 0) { 22427 return (EIO); 22428 } 22429 #endif 22430 22431 /* 22432 * Make a local copy of the soft state geometry to avoid some potential 22433 * race conditions associated with holding the mutex and updating the 22434 * write_reinstruct value 22435 */ 22436 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22437 mutex_enter(SD_MUTEX(un)); 22438 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22439 mutex_exit(SD_MUTEX(un)); 22440 22441 if (tmp_geom->dkg_write_reinstruct == 0) { 22442 tmp_geom->dkg_write_reinstruct = 22443 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22444 sd_rot_delay) / (int)60000); 22445 } 22446 22447 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22448 flag); 22449 if (rval != 0) { 22450 rval = EFAULT; 22451 } 22452 22453 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22454 return (rval); 22455 22456 } 22457 22458 22459 /* 22460 * Function: sd_dkio_set_geometry 22461 * 22462 * Description: This routine is the driver entry point for handling user 22463 * requests to set the device geometry (DKIOCSGEOM). The actual 22464 * device geometry is not updated, just the driver "notion" of it. 22465 * 22466 * Arguments: dev - the device number 22467 * arg - pointer to user provided dk_geom structure used to set 22468 * the controller's notion of the current geometry. 22469 * flag - this argument is a pass through to ddi_copyxxx() 22470 * directly from the mode argument of ioctl(). 22471 * 22472 * Return Code: 0 22473 * EFAULT 22474 * ENXIO 22475 * EIO 22476 */ 22477 22478 static int 22479 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22480 { 22481 struct sd_lun *un = NULL; 22482 struct dk_geom *tmp_geom; 22483 struct dk_map *lp; 22484 int rval = 0; 22485 int i; 22486 22487 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22488 return (ENXIO); 22489 } 22490 22491 /* 22492 * Make sure there is no reservation conflict on the lun. 22493 */ 22494 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22495 return (EACCES); 22496 } 22497 22498 #if defined(__i386) || defined(__amd64) 22499 if (un->un_solaris_size == 0) { 22500 return (EIO); 22501 } 22502 #endif 22503 22504 /* 22505 * We need to copy the user specified geometry into local 22506 * storage and then update the softstate. We don't want to hold 22507 * the mutex and copyin directly from the user to the soft state 22508 */ 22509 tmp_geom = (struct dk_geom *) 22510 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22511 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22512 if (rval != 0) { 22513 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22514 return (EFAULT); 22515 } 22516 22517 mutex_enter(SD_MUTEX(un)); 22518 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22519 for (i = 0; i < NDKMAP; i++) { 22520 lp = &un->un_map[i]; 22521 un->un_offset[i] = 22522 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22523 #if defined(__i386) || defined(__amd64) 22524 un->un_offset[i] += un->un_solaris_offset; 22525 #endif 22526 } 22527 un->un_f_geometry_is_valid = FALSE; 22528 mutex_exit(SD_MUTEX(un)); 22529 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22530 22531 return (rval); 22532 } 22533 22534 22535 /* 22536 * Function: sd_dkio_get_partition 22537 * 22538 * Description: This routine is the driver entry point for handling user 22539 * requests to get the partition table (DKIOCGAPART). 22540 * 22541 * Arguments: dev - the device number 22542 * arg - pointer to user provided dk_allmap structure specifying 22543 * the controller's notion of the current partition table. 22544 * flag - this argument is a pass through to ddi_copyxxx() 22545 * directly from the mode argument of ioctl(). 22546 * geom_validated - flag indicating if the device geometry has been 22547 * previously validated in the sdioctl routine. 22548 * 22549 * Return Code: 0 22550 * EFAULT 22551 * ENXIO 22552 * EIO 22553 */ 22554 22555 static int 22556 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22557 { 22558 struct sd_lun *un = NULL; 22559 int rval = 0; 22560 int size; 22561 22562 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22563 return (ENXIO); 22564 } 22565 22566 /* 22567 * Make sure the geometry is valid before getting the partition 22568 * information. 22569 */ 22570 mutex_enter(SD_MUTEX(un)); 22571 if (geom_validated == FALSE) { 22572 /* 22573 * sd_validate_geometry does not spin a disk up 22574 * if it was spun down. We need to make sure it 22575 * is ready before validating the geometry. 22576 */ 22577 mutex_exit(SD_MUTEX(un)); 22578 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22579 return (rval); 22580 } 22581 mutex_enter(SD_MUTEX(un)); 22582 22583 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22584 mutex_exit(SD_MUTEX(un)); 22585 return (rval); 22586 } 22587 } 22588 mutex_exit(SD_MUTEX(un)); 22589 22590 /* 22591 * It is possible that un_solaris_size is 0(uninitialized) 22592 * after sd_unit_attach. Reservation conflict may cause the 22593 * above situation. Thus, the zero check of un_solaris_size 22594 * should occur after the sd_validate_geometry() call. 22595 */ 22596 #if defined(__i386) || defined(__amd64) 22597 if (un->un_solaris_size == 0) { 22598 return (EIO); 22599 } 22600 #endif 22601 22602 #ifdef _MULTI_DATAMODEL 22603 switch (ddi_model_convert_from(flag & FMODELS)) { 22604 case DDI_MODEL_ILP32: { 22605 struct dk_map32 dk_map32[NDKMAP]; 22606 int i; 22607 22608 for (i = 0; i < NDKMAP; i++) { 22609 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22610 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22611 } 22612 size = NDKMAP * sizeof (struct dk_map32); 22613 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22614 if (rval != 0) { 22615 rval = EFAULT; 22616 } 22617 break; 22618 } 22619 case DDI_MODEL_NONE: 22620 size = NDKMAP * sizeof (struct dk_map); 22621 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22622 if (rval != 0) { 22623 rval = EFAULT; 22624 } 22625 break; 22626 } 22627 #else /* ! _MULTI_DATAMODEL */ 22628 size = NDKMAP * sizeof (struct dk_map); 22629 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22630 if (rval != 0) { 22631 rval = EFAULT; 22632 } 22633 #endif /* _MULTI_DATAMODEL */ 22634 return (rval); 22635 } 22636 22637 22638 /* 22639 * Function: sd_dkio_set_partition 22640 * 22641 * Description: This routine is the driver entry point for handling user 22642 * requests to set the partition table (DKIOCSAPART). The actual 22643 * device partition is not updated. 22644 * 22645 * Arguments: dev - the device number 22646 * arg - pointer to user provided dk_allmap structure used to set 22647 * the controller's notion of the partition table. 22648 * flag - this argument is a pass through to ddi_copyxxx() 22649 * directly from the mode argument of ioctl(). 22650 * 22651 * Return Code: 0 22652 * EINVAL 22653 * EFAULT 22654 * ENXIO 22655 * EIO 22656 */ 22657 22658 static int 22659 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22660 { 22661 struct sd_lun *un = NULL; 22662 struct dk_map dk_map[NDKMAP]; 22663 struct dk_map *lp; 22664 int rval = 0; 22665 int size; 22666 int i; 22667 #if defined(_SUNOS_VTOC_16) 22668 struct dkl_partition *vp; 22669 #endif 22670 22671 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22672 return (ENXIO); 22673 } 22674 22675 /* 22676 * Set the map for all logical partitions. We lock 22677 * the priority just to make sure an interrupt doesn't 22678 * come in while the map is half updated. 22679 */ 22680 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22681 mutex_enter(SD_MUTEX(un)); 22682 if (un->un_blockcount > DK_MAX_BLOCKS) { 22683 mutex_exit(SD_MUTEX(un)); 22684 return (ENOTSUP); 22685 } 22686 mutex_exit(SD_MUTEX(un)); 22687 22688 /* 22689 * Make sure there is no reservation conflict on the lun. 22690 */ 22691 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22692 return (EACCES); 22693 } 22694 22695 #if defined(__i386) || defined(__amd64) 22696 if (un->un_solaris_size == 0) { 22697 return (EIO); 22698 } 22699 #endif 22700 22701 #ifdef _MULTI_DATAMODEL 22702 switch (ddi_model_convert_from(flag & FMODELS)) { 22703 case DDI_MODEL_ILP32: { 22704 struct dk_map32 dk_map32[NDKMAP]; 22705 22706 size = NDKMAP * sizeof (struct dk_map32); 22707 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22708 if (rval != 0) { 22709 return (EFAULT); 22710 } 22711 for (i = 0; i < NDKMAP; i++) { 22712 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22713 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22714 } 22715 break; 22716 } 22717 case DDI_MODEL_NONE: 22718 size = NDKMAP * sizeof (struct dk_map); 22719 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22720 if (rval != 0) { 22721 return (EFAULT); 22722 } 22723 break; 22724 } 22725 #else /* ! _MULTI_DATAMODEL */ 22726 size = NDKMAP * sizeof (struct dk_map); 22727 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22728 if (rval != 0) { 22729 return (EFAULT); 22730 } 22731 #endif /* _MULTI_DATAMODEL */ 22732 22733 mutex_enter(SD_MUTEX(un)); 22734 /* Note: The size used in this bcopy is set based upon the data model */ 22735 bcopy(dk_map, un->un_map, size); 22736 #if defined(_SUNOS_VTOC_16) 22737 vp = (struct dkl_partition *)&(un->un_vtoc); 22738 #endif /* defined(_SUNOS_VTOC_16) */ 22739 for (i = 0; i < NDKMAP; i++) { 22740 lp = &un->un_map[i]; 22741 un->un_offset[i] = 22742 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22743 #if defined(_SUNOS_VTOC_16) 22744 vp->p_start = un->un_offset[i]; 22745 vp->p_size = lp->dkl_nblk; 22746 vp++; 22747 #endif /* defined(_SUNOS_VTOC_16) */ 22748 #if defined(__i386) || defined(__amd64) 22749 un->un_offset[i] += un->un_solaris_offset; 22750 #endif 22751 } 22752 mutex_exit(SD_MUTEX(un)); 22753 return (rval); 22754 } 22755 22756 22757 /* 22758 * Function: sd_dkio_get_vtoc 22759 * 22760 * Description: This routine is the driver entry point for handling user 22761 * requests to get the current volume table of contents 22762 * (DKIOCGVTOC). 22763 * 22764 * Arguments: dev - the device number 22765 * arg - pointer to user provided vtoc structure specifying 22766 * the current vtoc. 22767 * flag - this argument is a pass through to ddi_copyxxx() 22768 * directly from the mode argument of ioctl(). 22769 * geom_validated - flag indicating if the device geometry has been 22770 * previously validated in the sdioctl routine. 22771 * 22772 * Return Code: 0 22773 * EFAULT 22774 * ENXIO 22775 * EIO 22776 */ 22777 22778 static int 22779 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22780 { 22781 struct sd_lun *un = NULL; 22782 #if defined(_SUNOS_VTOC_8) 22783 struct vtoc user_vtoc; 22784 #endif /* defined(_SUNOS_VTOC_8) */ 22785 int rval = 0; 22786 22787 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22788 return (ENXIO); 22789 } 22790 22791 mutex_enter(SD_MUTEX(un)); 22792 if (geom_validated == FALSE) { 22793 /* 22794 * sd_validate_geometry does not spin a disk up 22795 * if it was spun down. We need to make sure it 22796 * is ready. 22797 */ 22798 mutex_exit(SD_MUTEX(un)); 22799 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22800 return (rval); 22801 } 22802 mutex_enter(SD_MUTEX(un)); 22803 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22804 mutex_exit(SD_MUTEX(un)); 22805 return (rval); 22806 } 22807 } 22808 22809 #if defined(_SUNOS_VTOC_8) 22810 sd_build_user_vtoc(un, &user_vtoc); 22811 mutex_exit(SD_MUTEX(un)); 22812 22813 #ifdef _MULTI_DATAMODEL 22814 switch (ddi_model_convert_from(flag & FMODELS)) { 22815 case DDI_MODEL_ILP32: { 22816 struct vtoc32 user_vtoc32; 22817 22818 vtoctovtoc32(user_vtoc, user_vtoc32); 22819 if (ddi_copyout(&user_vtoc32, (void *)arg, 22820 sizeof (struct vtoc32), flag)) { 22821 return (EFAULT); 22822 } 22823 break; 22824 } 22825 22826 case DDI_MODEL_NONE: 22827 if (ddi_copyout(&user_vtoc, (void *)arg, 22828 sizeof (struct vtoc), flag)) { 22829 return (EFAULT); 22830 } 22831 break; 22832 } 22833 #else /* ! _MULTI_DATAMODEL */ 22834 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22835 return (EFAULT); 22836 } 22837 #endif /* _MULTI_DATAMODEL */ 22838 22839 #elif defined(_SUNOS_VTOC_16) 22840 mutex_exit(SD_MUTEX(un)); 22841 22842 #ifdef _MULTI_DATAMODEL 22843 /* 22844 * The un_vtoc structure is a "struct dk_vtoc" which is always 22845 * 32-bit to maintain compatibility with existing on-disk 22846 * structures. Thus, we need to convert the structure when copying 22847 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22848 * program. If the target is a 32-bit program, then no conversion 22849 * is necessary. 22850 */ 22851 /* LINTED: logical expression always true: op "||" */ 22852 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22853 switch (ddi_model_convert_from(flag & FMODELS)) { 22854 case DDI_MODEL_ILP32: 22855 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22856 sizeof (un->un_vtoc), flag)) { 22857 return (EFAULT); 22858 } 22859 break; 22860 22861 case DDI_MODEL_NONE: { 22862 struct vtoc user_vtoc; 22863 22864 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22865 if (ddi_copyout(&user_vtoc, (void *)arg, 22866 sizeof (struct vtoc), flag)) { 22867 return (EFAULT); 22868 } 22869 break; 22870 } 22871 } 22872 #else /* ! _MULTI_DATAMODEL */ 22873 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22874 flag)) { 22875 return (EFAULT); 22876 } 22877 #endif /* _MULTI_DATAMODEL */ 22878 #else 22879 #error "No VTOC format defined." 22880 #endif 22881 22882 return (rval); 22883 } 22884 22885 static int 22886 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22887 { 22888 struct sd_lun *un = NULL; 22889 dk_efi_t user_efi; 22890 int rval = 0; 22891 void *buffer; 22892 22893 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22894 return (ENXIO); 22895 22896 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22897 return (EFAULT); 22898 22899 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22900 22901 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22902 (user_efi.dki_length > un->un_max_xfer_size)) 22903 return (EINVAL); 22904 22905 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22906 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22907 user_efi.dki_lba, SD_PATH_DIRECT); 22908 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22909 user_efi.dki_length, flag) != 0) 22910 rval = EFAULT; 22911 22912 kmem_free(buffer, user_efi.dki_length); 22913 return (rval); 22914 } 22915 22916 /* 22917 * Function: sd_build_user_vtoc 22918 * 22919 * Description: This routine populates a pass by reference variable with the 22920 * current volume table of contents. 22921 * 22922 * Arguments: un - driver soft state (unit) structure 22923 * user_vtoc - pointer to vtoc structure to be populated 22924 */ 22925 22926 static void 22927 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22928 { 22929 struct dk_map2 *lpart; 22930 struct dk_map *lmap; 22931 struct partition *vpart; 22932 int nblks; 22933 int i; 22934 22935 ASSERT(mutex_owned(SD_MUTEX(un))); 22936 22937 /* 22938 * Return vtoc structure fields in the provided VTOC area, addressed 22939 * by *vtoc. 22940 */ 22941 bzero(user_vtoc, sizeof (struct vtoc)); 22942 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 22943 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 22944 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 22945 user_vtoc->v_sanity = VTOC_SANE; 22946 user_vtoc->v_version = un->un_vtoc.v_version; 22947 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 22948 user_vtoc->v_sectorsz = un->un_sys_blocksize; 22949 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 22950 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 22951 sizeof (un->un_vtoc.v_reserved)); 22952 /* 22953 * Convert partitioning information. 22954 * 22955 * Note the conversion from starting cylinder number 22956 * to starting sector number. 22957 */ 22958 lmap = un->un_map; 22959 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 22960 vpart = user_vtoc->v_part; 22961 22962 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22963 22964 for (i = 0; i < V_NUMPAR; i++) { 22965 vpart->p_tag = lpart->p_tag; 22966 vpart->p_flag = lpart->p_flag; 22967 vpart->p_start = lmap->dkl_cylno * nblks; 22968 vpart->p_size = lmap->dkl_nblk; 22969 lmap++; 22970 lpart++; 22971 vpart++; 22972 22973 /* (4364927) */ 22974 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 22975 } 22976 22977 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 22978 } 22979 22980 static int 22981 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 22982 { 22983 struct sd_lun *un = NULL; 22984 struct partition64 p64; 22985 int rval = 0; 22986 uint_t nparts; 22987 efi_gpe_t *partitions; 22988 efi_gpt_t *buffer; 22989 diskaddr_t gpe_lba; 22990 22991 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22992 return (ENXIO); 22993 } 22994 22995 if (ddi_copyin((const void *)arg, &p64, 22996 sizeof (struct partition64), flag)) { 22997 return (EFAULT); 22998 } 22999 23000 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23001 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23002 1, SD_PATH_DIRECT); 23003 if (rval != 0) 23004 goto done_error; 23005 23006 sd_swap_efi_gpt(buffer); 23007 23008 if ((rval = sd_validate_efi(buffer)) != 0) 23009 goto done_error; 23010 23011 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23012 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23013 if (p64.p_partno > nparts) { 23014 /* couldn't find it */ 23015 rval = ESRCH; 23016 goto done_error; 23017 } 23018 /* 23019 * if we're dealing with a partition that's out of the normal 23020 * 16K block, adjust accordingly 23021 */ 23022 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23023 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23024 gpe_lba, SD_PATH_DIRECT); 23025 if (rval) { 23026 goto done_error; 23027 } 23028 partitions = (efi_gpe_t *)buffer; 23029 23030 sd_swap_efi_gpe(nparts, partitions); 23031 23032 partitions += p64.p_partno; 23033 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23034 sizeof (struct uuid)); 23035 p64.p_start = partitions->efi_gpe_StartingLBA; 23036 p64.p_size = partitions->efi_gpe_EndingLBA - 23037 p64.p_start + 1; 23038 23039 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23040 rval = EFAULT; 23041 23042 done_error: 23043 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23044 return (rval); 23045 } 23046 23047 23048 /* 23049 * Function: sd_dkio_set_vtoc 23050 * 23051 * Description: This routine is the driver entry point for handling user 23052 * requests to set the current volume table of contents 23053 * (DKIOCSVTOC). 23054 * 23055 * Arguments: dev - the device number 23056 * arg - pointer to user provided vtoc structure used to set the 23057 * current vtoc. 23058 * flag - this argument is a pass through to ddi_copyxxx() 23059 * directly from the mode argument of ioctl(). 23060 * 23061 * Return Code: 0 23062 * EFAULT 23063 * ENXIO 23064 * EINVAL 23065 * ENOTSUP 23066 */ 23067 23068 static int 23069 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23070 { 23071 struct sd_lun *un = NULL; 23072 struct vtoc user_vtoc; 23073 int rval = 0; 23074 23075 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23076 return (ENXIO); 23077 } 23078 23079 #if defined(__i386) || defined(__amd64) 23080 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23081 return (EINVAL); 23082 } 23083 #endif 23084 23085 #ifdef _MULTI_DATAMODEL 23086 switch (ddi_model_convert_from(flag & FMODELS)) { 23087 case DDI_MODEL_ILP32: { 23088 struct vtoc32 user_vtoc32; 23089 23090 if (ddi_copyin((const void *)arg, &user_vtoc32, 23091 sizeof (struct vtoc32), flag)) { 23092 return (EFAULT); 23093 } 23094 vtoc32tovtoc(user_vtoc32, user_vtoc); 23095 break; 23096 } 23097 23098 case DDI_MODEL_NONE: 23099 if (ddi_copyin((const void *)arg, &user_vtoc, 23100 sizeof (struct vtoc), flag)) { 23101 return (EFAULT); 23102 } 23103 break; 23104 } 23105 #else /* ! _MULTI_DATAMODEL */ 23106 if (ddi_copyin((const void *)arg, &user_vtoc, 23107 sizeof (struct vtoc), flag)) { 23108 return (EFAULT); 23109 } 23110 #endif /* _MULTI_DATAMODEL */ 23111 23112 mutex_enter(SD_MUTEX(un)); 23113 if (un->un_blockcount > DK_MAX_BLOCKS) { 23114 mutex_exit(SD_MUTEX(un)); 23115 return (ENOTSUP); 23116 } 23117 if (un->un_g.dkg_ncyl == 0) { 23118 mutex_exit(SD_MUTEX(un)); 23119 return (EINVAL); 23120 } 23121 23122 mutex_exit(SD_MUTEX(un)); 23123 sd_clear_efi(un); 23124 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23125 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23126 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23127 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23128 un->un_node_type, NULL); 23129 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23130 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23131 un->un_node_type, NULL); 23132 mutex_enter(SD_MUTEX(un)); 23133 23134 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23135 if ((rval = sd_write_label(dev)) == 0) { 23136 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23137 != 0) { 23138 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23139 "sd_dkio_set_vtoc: " 23140 "Failed validate geometry\n"); 23141 } 23142 } 23143 } 23144 23145 /* 23146 * If sd_build_label_vtoc, or sd_write_label failed above write the 23147 * devid anyway, what can it hurt? Also preserve the device id by 23148 * writing to the disk acyl for the case where a devid has been 23149 * fabricated. 23150 */ 23151 if (un->un_f_devid_supported && 23152 (un->un_f_opt_fab_devid == TRUE)) { 23153 if (un->un_devid == NULL) { 23154 sd_register_devid(un, SD_DEVINFO(un), 23155 SD_TARGET_IS_UNRESERVED); 23156 } else { 23157 /* 23158 * The device id for this disk has been 23159 * fabricated. Fabricated device id's are 23160 * managed by storing them in the last 2 23161 * available sectors on the drive. The device 23162 * id must be preserved by writing it back out 23163 * to this location. 23164 */ 23165 if (sd_write_deviceid(un) != 0) { 23166 ddi_devid_free(un->un_devid); 23167 un->un_devid = NULL; 23168 } 23169 } 23170 } 23171 mutex_exit(SD_MUTEX(un)); 23172 return (rval); 23173 } 23174 23175 23176 /* 23177 * Function: sd_build_label_vtoc 23178 * 23179 * Description: This routine updates the driver soft state current volume table 23180 * of contents based on a user specified vtoc. 23181 * 23182 * Arguments: un - driver soft state (unit) structure 23183 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23184 * to update the driver soft state. 23185 * 23186 * Return Code: 0 23187 * EINVAL 23188 */ 23189 23190 static int 23191 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23192 { 23193 struct dk_map *lmap; 23194 struct partition *vpart; 23195 int nblks; 23196 #if defined(_SUNOS_VTOC_8) 23197 int ncyl; 23198 struct dk_map2 *lpart; 23199 #endif /* defined(_SUNOS_VTOC_8) */ 23200 int i; 23201 23202 ASSERT(mutex_owned(SD_MUTEX(un))); 23203 23204 /* Sanity-check the vtoc */ 23205 if (user_vtoc->v_sanity != VTOC_SANE || 23206 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23207 user_vtoc->v_nparts != V_NUMPAR) { 23208 return (EINVAL); 23209 } 23210 23211 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23212 if (nblks == 0) { 23213 return (EINVAL); 23214 } 23215 23216 #if defined(_SUNOS_VTOC_8) 23217 vpart = user_vtoc->v_part; 23218 for (i = 0; i < V_NUMPAR; i++) { 23219 if ((vpart->p_start % nblks) != 0) { 23220 return (EINVAL); 23221 } 23222 ncyl = vpart->p_start / nblks; 23223 ncyl += vpart->p_size / nblks; 23224 if ((vpart->p_size % nblks) != 0) { 23225 ncyl++; 23226 } 23227 if (ncyl > (int)un->un_g.dkg_ncyl) { 23228 return (EINVAL); 23229 } 23230 vpart++; 23231 } 23232 #endif /* defined(_SUNOS_VTOC_8) */ 23233 23234 /* Put appropriate vtoc structure fields into the disk label */ 23235 #if defined(_SUNOS_VTOC_16) 23236 /* 23237 * The vtoc is always a 32bit data structure to maintain the 23238 * on-disk format. Convert "in place" instead of bcopying it. 23239 */ 23240 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23241 23242 /* 23243 * in the 16-slice vtoc, starting sectors are expressed in 23244 * numbers *relative* to the start of the Solaris fdisk partition. 23245 */ 23246 lmap = un->un_map; 23247 vpart = user_vtoc->v_part; 23248 23249 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23250 lmap->dkl_cylno = vpart->p_start / nblks; 23251 lmap->dkl_nblk = vpart->p_size; 23252 } 23253 23254 #elif defined(_SUNOS_VTOC_8) 23255 23256 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23257 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23258 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23259 23260 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23261 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23262 23263 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23264 23265 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23266 23267 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23268 sizeof (un->un_vtoc.v_reserved)); 23269 23270 /* 23271 * Note the conversion from starting sector number 23272 * to starting cylinder number. 23273 * Return error if division results in a remainder. 23274 */ 23275 lmap = un->un_map; 23276 lpart = un->un_vtoc.v_part; 23277 vpart = user_vtoc->v_part; 23278 23279 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23280 lpart->p_tag = vpart->p_tag; 23281 lpart->p_flag = vpart->p_flag; 23282 lmap->dkl_cylno = vpart->p_start / nblks; 23283 lmap->dkl_nblk = vpart->p_size; 23284 23285 lmap++; 23286 lpart++; 23287 vpart++; 23288 23289 /* (4387723) */ 23290 #ifdef _LP64 23291 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23292 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23293 } else { 23294 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23295 } 23296 #else 23297 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23298 #endif 23299 } 23300 23301 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23302 #else 23303 #error "No VTOC format defined." 23304 #endif 23305 return (0); 23306 } 23307 23308 /* 23309 * Function: sd_clear_efi 23310 * 23311 * Description: This routine clears all EFI labels. 23312 * 23313 * Arguments: un - driver soft state (unit) structure 23314 * 23315 * Return Code: void 23316 */ 23317 23318 static void 23319 sd_clear_efi(struct sd_lun *un) 23320 { 23321 efi_gpt_t *gpt; 23322 uint_t lbasize; 23323 uint64_t cap; 23324 int rval; 23325 23326 ASSERT(!mutex_owned(SD_MUTEX(un))); 23327 23328 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23329 23330 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23331 goto done; 23332 } 23333 23334 sd_swap_efi_gpt(gpt); 23335 rval = sd_validate_efi(gpt); 23336 if (rval == 0) { 23337 /* clear primary */ 23338 bzero(gpt, sizeof (efi_gpt_t)); 23339 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23340 SD_PATH_DIRECT))) { 23341 SD_INFO(SD_LOG_IO_PARTITION, un, 23342 "sd_clear_efi: clear primary label failed\n"); 23343 } 23344 } 23345 /* the backup */ 23346 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23347 SD_PATH_DIRECT); 23348 if (rval) { 23349 goto done; 23350 } 23351 /* 23352 * The MMC standard allows READ CAPACITY to be 23353 * inaccurate by a bounded amount (in the interest of 23354 * response latency). As a result, failed READs are 23355 * commonplace (due to the reading of metadata and not 23356 * data). Depending on the per-Vendor/drive Sense data, 23357 * the failed READ can cause many (unnecessary) retries. 23358 */ 23359 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23360 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23361 SD_PATH_DIRECT)) != 0) { 23362 goto done; 23363 } 23364 sd_swap_efi_gpt(gpt); 23365 rval = sd_validate_efi(gpt); 23366 if (rval == 0) { 23367 /* clear backup */ 23368 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23369 cap-1); 23370 bzero(gpt, sizeof (efi_gpt_t)); 23371 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23372 cap-1, SD_PATH_DIRECT))) { 23373 SD_INFO(SD_LOG_IO_PARTITION, un, 23374 "sd_clear_efi: clear backup label failed\n"); 23375 } 23376 } 23377 23378 done: 23379 kmem_free(gpt, sizeof (efi_gpt_t)); 23380 } 23381 23382 /* 23383 * Function: sd_set_vtoc 23384 * 23385 * Description: This routine writes data to the appropriate positions 23386 * 23387 * Arguments: un - driver soft state (unit) structure 23388 * dkl - the data to be written 23389 * 23390 * Return: void 23391 */ 23392 23393 static int 23394 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23395 { 23396 void *shadow_buf; 23397 uint_t label_addr; 23398 int sec; 23399 int blk; 23400 int head; 23401 int cyl; 23402 int rval; 23403 23404 #if defined(__i386) || defined(__amd64) 23405 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23406 #else 23407 /* Write the primary label at block 0 of the solaris partition. */ 23408 label_addr = 0; 23409 #endif 23410 23411 if (NOT_DEVBSIZE(un)) { 23412 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23413 /* 23414 * Read the target's first block. 23415 */ 23416 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23417 un->un_tgt_blocksize, label_addr, 23418 SD_PATH_STANDARD)) != 0) { 23419 goto exit; 23420 } 23421 /* 23422 * Copy the contents of the label into the shadow buffer 23423 * which is of the size of target block size. 23424 */ 23425 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23426 } 23427 23428 /* Write the primary label */ 23429 if (NOT_DEVBSIZE(un)) { 23430 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23431 label_addr, SD_PATH_STANDARD); 23432 } else { 23433 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23434 label_addr, SD_PATH_STANDARD); 23435 } 23436 if (rval != 0) { 23437 return (rval); 23438 } 23439 23440 /* 23441 * Calculate where the backup labels go. They are always on 23442 * the last alternate cylinder, but some older drives put them 23443 * on head 2 instead of the last head. They are always on the 23444 * first 5 odd sectors of the appropriate track. 23445 * 23446 * We have no choice at this point, but to believe that the 23447 * disk label is valid. Use the geometry of the disk 23448 * as described in the label. 23449 */ 23450 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23451 head = dkl->dkl_nhead - 1; 23452 23453 /* 23454 * Write and verify the backup labels. Make sure we don't try to 23455 * write past the last cylinder. 23456 */ 23457 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23458 blk = (daddr_t)( 23459 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23460 (head * dkl->dkl_nsect) + sec); 23461 #if defined(__i386) || defined(__amd64) 23462 blk += un->un_solaris_offset; 23463 #endif 23464 if (NOT_DEVBSIZE(un)) { 23465 uint64_t tblk; 23466 /* 23467 * Need to read the block first for read modify write. 23468 */ 23469 tblk = (uint64_t)blk; 23470 blk = (int)((tblk * un->un_sys_blocksize) / 23471 un->un_tgt_blocksize); 23472 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23473 un->un_tgt_blocksize, blk, 23474 SD_PATH_STANDARD)) != 0) { 23475 goto exit; 23476 } 23477 /* 23478 * Modify the shadow buffer with the label. 23479 */ 23480 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23481 rval = sd_send_scsi_WRITE(un, shadow_buf, 23482 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23483 } else { 23484 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23485 blk, SD_PATH_STANDARD); 23486 SD_INFO(SD_LOG_IO_PARTITION, un, 23487 "sd_set_vtoc: wrote backup label %d\n", blk); 23488 } 23489 if (rval != 0) { 23490 goto exit; 23491 } 23492 } 23493 exit: 23494 if (NOT_DEVBSIZE(un)) { 23495 kmem_free(shadow_buf, un->un_tgt_blocksize); 23496 } 23497 return (rval); 23498 } 23499 23500 /* 23501 * Function: sd_clear_vtoc 23502 * 23503 * Description: This routine clears out the VTOC labels. 23504 * 23505 * Arguments: un - driver soft state (unit) structure 23506 * 23507 * Return: void 23508 */ 23509 23510 static void 23511 sd_clear_vtoc(struct sd_lun *un) 23512 { 23513 struct dk_label *dkl; 23514 23515 mutex_exit(SD_MUTEX(un)); 23516 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23517 mutex_enter(SD_MUTEX(un)); 23518 /* 23519 * sd_set_vtoc uses these fields in order to figure out 23520 * where to overwrite the backup labels 23521 */ 23522 dkl->dkl_apc = un->un_g.dkg_apc; 23523 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23524 dkl->dkl_acyl = un->un_g.dkg_acyl; 23525 dkl->dkl_nhead = un->un_g.dkg_nhead; 23526 dkl->dkl_nsect = un->un_g.dkg_nsect; 23527 mutex_exit(SD_MUTEX(un)); 23528 (void) sd_set_vtoc(un, dkl); 23529 kmem_free(dkl, sizeof (struct dk_label)); 23530 23531 mutex_enter(SD_MUTEX(un)); 23532 } 23533 23534 /* 23535 * Function: sd_write_label 23536 * 23537 * Description: This routine will validate and write the driver soft state vtoc 23538 * contents to the device. 23539 * 23540 * Arguments: dev - the device number 23541 * 23542 * Return Code: the code returned by sd_send_scsi_cmd() 23543 * 0 23544 * EINVAL 23545 * ENXIO 23546 * ENOMEM 23547 */ 23548 23549 static int 23550 sd_write_label(dev_t dev) 23551 { 23552 struct sd_lun *un; 23553 struct dk_label *dkl; 23554 short sum; 23555 short *sp; 23556 int i; 23557 int rval; 23558 23559 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23560 (un->un_state == SD_STATE_OFFLINE)) { 23561 return (ENXIO); 23562 } 23563 ASSERT(mutex_owned(SD_MUTEX(un))); 23564 mutex_exit(SD_MUTEX(un)); 23565 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23566 mutex_enter(SD_MUTEX(un)); 23567 23568 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23569 dkl->dkl_rpm = un->un_g.dkg_rpm; 23570 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23571 dkl->dkl_apc = un->un_g.dkg_apc; 23572 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23573 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23574 dkl->dkl_acyl = un->un_g.dkg_acyl; 23575 dkl->dkl_nhead = un->un_g.dkg_nhead; 23576 dkl->dkl_nsect = un->un_g.dkg_nsect; 23577 23578 #if defined(_SUNOS_VTOC_8) 23579 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23580 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23581 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23582 for (i = 0; i < NDKMAP; i++) { 23583 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23584 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23585 } 23586 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23587 #elif defined(_SUNOS_VTOC_16) 23588 dkl->dkl_skew = un->un_dkg_skew; 23589 #else 23590 #error "No VTOC format defined." 23591 #endif 23592 23593 dkl->dkl_magic = DKL_MAGIC; 23594 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23595 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23596 23597 /* Construct checksum for the new disk label */ 23598 sum = 0; 23599 sp = (short *)dkl; 23600 i = sizeof (struct dk_label) / sizeof (short); 23601 while (i--) { 23602 sum ^= *sp++; 23603 } 23604 dkl->dkl_cksum = sum; 23605 23606 mutex_exit(SD_MUTEX(un)); 23607 23608 rval = sd_set_vtoc(un, dkl); 23609 exit: 23610 kmem_free(dkl, sizeof (struct dk_label)); 23611 mutex_enter(SD_MUTEX(un)); 23612 return (rval); 23613 } 23614 23615 static int 23616 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23617 { 23618 struct sd_lun *un = NULL; 23619 dk_efi_t user_efi; 23620 int rval = 0; 23621 void *buffer; 23622 23623 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23624 return (ENXIO); 23625 23626 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23627 return (EFAULT); 23628 23629 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23630 23631 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23632 (user_efi.dki_length > un->un_max_xfer_size)) 23633 return (EINVAL); 23634 23635 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23636 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23637 rval = EFAULT; 23638 } else { 23639 /* 23640 * let's clear the vtoc labels and clear the softstate 23641 * vtoc. 23642 */ 23643 mutex_enter(SD_MUTEX(un)); 23644 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23645 SD_TRACE(SD_LOG_IO_PARTITION, un, 23646 "sd_dkio_set_efi: CLEAR VTOC\n"); 23647 sd_clear_vtoc(un); 23648 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23649 mutex_exit(SD_MUTEX(un)); 23650 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23651 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23652 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23653 S_IFBLK, 23654 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23655 un->un_node_type, NULL); 23656 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23657 S_IFCHR, 23658 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23659 un->un_node_type, NULL); 23660 } else 23661 mutex_exit(SD_MUTEX(un)); 23662 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23663 user_efi.dki_lba, SD_PATH_DIRECT); 23664 if (rval == 0) { 23665 mutex_enter(SD_MUTEX(un)); 23666 un->un_f_geometry_is_valid = FALSE; 23667 mutex_exit(SD_MUTEX(un)); 23668 } 23669 } 23670 kmem_free(buffer, user_efi.dki_length); 23671 return (rval); 23672 } 23673 23674 /* 23675 * Function: sd_dkio_get_mboot 23676 * 23677 * Description: This routine is the driver entry point for handling user 23678 * requests to get the current device mboot (DKIOCGMBOOT) 23679 * 23680 * Arguments: dev - the device number 23681 * arg - pointer to user provided mboot structure specifying 23682 * the current mboot. 23683 * flag - this argument is a pass through to ddi_copyxxx() 23684 * directly from the mode argument of ioctl(). 23685 * 23686 * Return Code: 0 23687 * EINVAL 23688 * EFAULT 23689 * ENXIO 23690 */ 23691 23692 static int 23693 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23694 { 23695 struct sd_lun *un; 23696 struct mboot *mboot; 23697 int rval; 23698 size_t buffer_size; 23699 23700 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23701 (un->un_state == SD_STATE_OFFLINE)) { 23702 return (ENXIO); 23703 } 23704 23705 if (!un->un_f_mboot_supported || arg == NULL) { 23706 return (EINVAL); 23707 } 23708 23709 /* 23710 * Read the mboot block, located at absolute block 0 on the target. 23711 */ 23712 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23713 23714 SD_TRACE(SD_LOG_IO_PARTITION, un, 23715 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23716 23717 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23718 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23719 SD_PATH_STANDARD)) == 0) { 23720 if (ddi_copyout(mboot, (void *)arg, 23721 sizeof (struct mboot), flag) != 0) { 23722 rval = EFAULT; 23723 } 23724 } 23725 kmem_free(mboot, buffer_size); 23726 return (rval); 23727 } 23728 23729 23730 /* 23731 * Function: sd_dkio_set_mboot 23732 * 23733 * Description: This routine is the driver entry point for handling user 23734 * requests to validate and set the device master boot 23735 * (DKIOCSMBOOT). 23736 * 23737 * Arguments: dev - the device number 23738 * arg - pointer to user provided mboot structure used to set the 23739 * master boot. 23740 * flag - this argument is a pass through to ddi_copyxxx() 23741 * directly from the mode argument of ioctl(). 23742 * 23743 * Return Code: 0 23744 * EINVAL 23745 * EFAULT 23746 * ENXIO 23747 */ 23748 23749 static int 23750 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23751 { 23752 struct sd_lun *un = NULL; 23753 struct mboot *mboot = NULL; 23754 int rval; 23755 ushort_t magic; 23756 23757 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23758 return (ENXIO); 23759 } 23760 23761 ASSERT(!mutex_owned(SD_MUTEX(un))); 23762 23763 if (!un->un_f_mboot_supported) { 23764 return (EINVAL); 23765 } 23766 23767 if (arg == NULL) { 23768 return (EINVAL); 23769 } 23770 23771 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23772 23773 if (ddi_copyin((const void *)arg, mboot, 23774 sizeof (struct mboot), flag) != 0) { 23775 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23776 return (EFAULT); 23777 } 23778 23779 /* Is this really a master boot record? */ 23780 magic = LE_16(mboot->signature); 23781 if (magic != MBB_MAGIC) { 23782 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23783 return (EINVAL); 23784 } 23785 23786 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23787 SD_PATH_STANDARD); 23788 23789 mutex_enter(SD_MUTEX(un)); 23790 #if defined(__i386) || defined(__amd64) 23791 if (rval == 0) { 23792 /* 23793 * mboot has been written successfully. 23794 * update the fdisk and vtoc tables in memory 23795 */ 23796 rval = sd_update_fdisk_and_vtoc(un); 23797 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23798 mutex_exit(SD_MUTEX(un)); 23799 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23800 return (rval); 23801 } 23802 } 23803 23804 /* 23805 * If the mboot write fails, write the devid anyway, what can it hurt? 23806 * Also preserve the device id by writing to the disk acyl for the case 23807 * where a devid has been fabricated. 23808 */ 23809 if (un->un_f_devid_supported && un->un_f_opt_fab_devid) { 23810 if (un->un_devid == NULL) { 23811 sd_register_devid(un, SD_DEVINFO(un), 23812 SD_TARGET_IS_UNRESERVED); 23813 } else { 23814 /* 23815 * The device id for this disk has been 23816 * fabricated. Fabricated device id's are 23817 * managed by storing them in the last 2 23818 * available sectors on the drive. The device 23819 * id must be preserved by writing it back out 23820 * to this location. 23821 */ 23822 if (sd_write_deviceid(un) != 0) { 23823 ddi_devid_free(un->un_devid); 23824 un->un_devid = NULL; 23825 } 23826 } 23827 } 23828 23829 #ifdef __lock_lint 23830 sd_setup_default_geometry(un); 23831 #endif 23832 23833 #else 23834 if (rval == 0) { 23835 /* 23836 * mboot has been written successfully. 23837 * set up the default geometry and VTOC 23838 */ 23839 if (un->un_blockcount <= DK_MAX_BLOCKS) 23840 sd_setup_default_geometry(un); 23841 } 23842 #endif 23843 mutex_exit(SD_MUTEX(un)); 23844 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23845 return (rval); 23846 } 23847 23848 23849 /* 23850 * Function: sd_setup_default_geometry 23851 * 23852 * Description: This local utility routine sets the default geometry as part of 23853 * setting the device mboot. 23854 * 23855 * Arguments: un - driver soft state (unit) structure 23856 * 23857 * Note: This may be redundant with sd_build_default_label. 23858 */ 23859 23860 static void 23861 sd_setup_default_geometry(struct sd_lun *un) 23862 { 23863 /* zero out the soft state geometry and partition table. */ 23864 bzero(&un->un_g, sizeof (struct dk_geom)); 23865 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23866 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23867 un->un_asciilabel[0] = '\0'; 23868 23869 /* 23870 * For the rpm, we use the minimum for the disk. 23871 * For the head, cyl and number of sector per track, 23872 * if the capacity <= 1GB, head = 64, sect = 32. 23873 * else head = 255, sect 63 23874 * Note: the capacity should be equal to C*H*S values. 23875 * This will cause some truncation of size due to 23876 * round off errors. For CD-ROMs, this truncation can 23877 * have adverse side effects, so returning ncyl and 23878 * nhead as 1. The nsect will overflow for most of 23879 * CD-ROMs as nsect is of type ushort. 23880 */ 23881 if (ISCD(un)) { 23882 un->un_g.dkg_ncyl = 1; 23883 un->un_g.dkg_nhead = 1; 23884 un->un_g.dkg_nsect = un->un_blockcount; 23885 } else { 23886 if (un->un_blockcount <= 0x1000) { 23887 /* Needed for unlabeled SCSI floppies. */ 23888 un->un_g.dkg_nhead = 2; 23889 un->un_g.dkg_ncyl = 80; 23890 un->un_g.dkg_pcyl = 80; 23891 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 23892 } else if (un->un_blockcount <= 0x200000) { 23893 un->un_g.dkg_nhead = 64; 23894 un->un_g.dkg_nsect = 32; 23895 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 23896 } else { 23897 un->un_g.dkg_nhead = 255; 23898 un->un_g.dkg_nsect = 63; 23899 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 23900 } 23901 un->un_blockcount = un->un_g.dkg_ncyl * 23902 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 23903 } 23904 un->un_g.dkg_acyl = 0; 23905 un->un_g.dkg_bcyl = 0; 23906 un->un_g.dkg_intrlv = 1; 23907 un->un_g.dkg_rpm = 200; 23908 un->un_g.dkg_read_reinstruct = 0; 23909 un->un_g.dkg_write_reinstruct = 0; 23910 if (un->un_g.dkg_pcyl == 0) { 23911 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 23912 } 23913 23914 un->un_map['a'-'a'].dkl_cylno = 0; 23915 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 23916 un->un_map['c'-'a'].dkl_cylno = 0; 23917 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 23918 un->un_f_geometry_is_valid = FALSE; 23919 } 23920 23921 23922 #if defined(__i386) || defined(__amd64) 23923 /* 23924 * Function: sd_update_fdisk_and_vtoc 23925 * 23926 * Description: This local utility routine updates the device fdisk and vtoc 23927 * as part of setting the device mboot. 23928 * 23929 * Arguments: un - driver soft state (unit) structure 23930 * 23931 * Return Code: 0 for success or errno-type return code. 23932 * 23933 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 23934 * these did exist seperately in x86 sd.c!!! 23935 */ 23936 23937 static int 23938 sd_update_fdisk_and_vtoc(struct sd_lun *un) 23939 { 23940 static char labelstring[128]; 23941 static char buf[256]; 23942 char *label = 0; 23943 int count; 23944 int label_rc = 0; 23945 int gvalid = un->un_f_geometry_is_valid; 23946 int fdisk_rval; 23947 int lbasize; 23948 int capacity; 23949 23950 ASSERT(mutex_owned(SD_MUTEX(un))); 23951 23952 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 23953 return (EINVAL); 23954 } 23955 23956 if (un->un_f_blockcount_is_valid == FALSE) { 23957 return (EINVAL); 23958 } 23959 23960 #if defined(_SUNOS_VTOC_16) 23961 /* 23962 * Set up the "whole disk" fdisk partition; this should always 23963 * exist, regardless of whether the disk contains an fdisk table 23964 * or vtoc. 23965 */ 23966 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 23967 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 23968 #endif /* defined(_SUNOS_VTOC_16) */ 23969 23970 /* 23971 * copy the lbasize and capacity so that if they're 23972 * reset while we're not holding the SD_MUTEX(un), we will 23973 * continue to use valid values after the SD_MUTEX(un) is 23974 * reacquired. 23975 */ 23976 lbasize = un->un_tgt_blocksize; 23977 capacity = un->un_blockcount; 23978 23979 /* 23980 * refresh the logical and physical geometry caches. 23981 * (data from mode sense format/rigid disk geometry pages, 23982 * and scsi_ifgetcap("geometry"). 23983 */ 23984 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 23985 23986 /* 23987 * Only DIRECT ACCESS devices will have Sun labels. 23988 * CD's supposedly have a Sun label, too 23989 */ 23990 if (un->un_f_vtoc_label_supported) { 23991 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 23992 SD_PATH_DIRECT); 23993 if (fdisk_rval == SD_CMD_FAILURE) { 23994 ASSERT(mutex_owned(SD_MUTEX(un))); 23995 return (EIO); 23996 } 23997 23998 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 23999 ASSERT(mutex_owned(SD_MUTEX(un))); 24000 return (EACCES); 24001 } 24002 24003 if (un->un_solaris_size <= DK_LABEL_LOC) { 24004 /* 24005 * Found fdisk table but no Solaris partition entry, 24006 * so don't call sd_uselabel() and don't create 24007 * a default label. 24008 */ 24009 label_rc = 0; 24010 un->un_f_geometry_is_valid = TRUE; 24011 goto no_solaris_partition; 24012 } 24013 24014 #if defined(_SUNOS_VTOC_8) 24015 label = (char *)un->un_asciilabel; 24016 #elif defined(_SUNOS_VTOC_16) 24017 label = (char *)un->un_vtoc.v_asciilabel; 24018 #else 24019 #error "No VTOC format defined." 24020 #endif 24021 } else if (capacity < 0) { 24022 ASSERT(mutex_owned(SD_MUTEX(un))); 24023 return (EINVAL); 24024 } 24025 24026 /* 24027 * For Removable media We reach here if we have found a 24028 * SOLARIS PARTITION. 24029 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24030 * PARTITION has changed from the previous one, hence we will setup a 24031 * default VTOC in this case. 24032 */ 24033 if (un->un_f_geometry_is_valid == FALSE) { 24034 sd_build_default_label(un); 24035 label_rc = 0; 24036 } 24037 24038 no_solaris_partition: 24039 if ((!un->un_f_has_removable_media || 24040 (un->un_f_has_removable_media && 24041 un->un_mediastate == DKIO_EJECTED)) && 24042 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24043 /* 24044 * Print out a message indicating who and what we are. 24045 * We do this only when we happen to really validate the 24046 * geometry. We may call sd_validate_geometry() at other 24047 * times, ioctl()'s like Get VTOC in which case we 24048 * don't want to print the label. 24049 * If the geometry is valid, print the label string, 24050 * else print vendor and product info, if available 24051 */ 24052 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24053 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24054 } else { 24055 mutex_enter(&sd_label_mutex); 24056 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24057 labelstring); 24058 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24059 &labelstring[64]); 24060 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24061 labelstring, &labelstring[64]); 24062 if (un->un_f_blockcount_is_valid == TRUE) { 24063 (void) sprintf(&buf[strlen(buf)], 24064 ", %" PRIu64 " %u byte blocks\n", 24065 un->un_blockcount, 24066 un->un_tgt_blocksize); 24067 } else { 24068 (void) sprintf(&buf[strlen(buf)], 24069 ", (unknown capacity)\n"); 24070 } 24071 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24072 mutex_exit(&sd_label_mutex); 24073 } 24074 } 24075 24076 #if defined(_SUNOS_VTOC_16) 24077 /* 24078 * If we have valid geometry, set up the remaining fdisk partitions. 24079 * Note that dkl_cylno is not used for the fdisk map entries, so 24080 * we set it to an entirely bogus value. 24081 */ 24082 for (count = 0; count < FD_NUMPART; count++) { 24083 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24084 un->un_map[FDISK_P1 + count].dkl_nblk = 24085 un->un_fmap[count].fmap_nblk; 24086 un->un_offset[FDISK_P1 + count] = 24087 un->un_fmap[count].fmap_start; 24088 } 24089 #endif 24090 24091 for (count = 0; count < NDKMAP; count++) { 24092 #if defined(_SUNOS_VTOC_8) 24093 struct dk_map *lp = &un->un_map[count]; 24094 un->un_offset[count] = 24095 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24096 #elif defined(_SUNOS_VTOC_16) 24097 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24098 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24099 #else 24100 #error "No VTOC format defined." 24101 #endif 24102 } 24103 24104 ASSERT(mutex_owned(SD_MUTEX(un))); 24105 return (label_rc); 24106 } 24107 #endif 24108 24109 24110 /* 24111 * Function: sd_check_media 24112 * 24113 * Description: This utility routine implements the functionality for the 24114 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24115 * driver state changes from that specified by the user 24116 * (inserted or ejected). For example, if the user specifies 24117 * DKIO_EJECTED and the current media state is inserted this 24118 * routine will immediately return DKIO_INSERTED. However, if the 24119 * current media state is not inserted the user thread will be 24120 * blocked until the drive state changes. If DKIO_NONE is specified 24121 * the user thread will block until a drive state change occurs. 24122 * 24123 * Arguments: dev - the device number 24124 * state - user pointer to a dkio_state, updated with the current 24125 * drive state at return. 24126 * 24127 * Return Code: ENXIO 24128 * EIO 24129 * EAGAIN 24130 * EINTR 24131 */ 24132 24133 static int 24134 sd_check_media(dev_t dev, enum dkio_state state) 24135 { 24136 struct sd_lun *un = NULL; 24137 enum dkio_state prev_state; 24138 opaque_t token = NULL; 24139 int rval = 0; 24140 24141 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24142 return (ENXIO); 24143 } 24144 24145 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24146 24147 mutex_enter(SD_MUTEX(un)); 24148 24149 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24150 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24151 24152 prev_state = un->un_mediastate; 24153 24154 /* is there anything to do? */ 24155 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24156 /* 24157 * submit the request to the scsi_watch service; 24158 * scsi_media_watch_cb() does the real work 24159 */ 24160 mutex_exit(SD_MUTEX(un)); 24161 24162 /* 24163 * This change handles the case where a scsi watch request is 24164 * added to a device that is powered down. To accomplish this 24165 * we power up the device before adding the scsi watch request, 24166 * since the scsi watch sends a TUR directly to the device 24167 * which the device cannot handle if it is powered down. 24168 */ 24169 if (sd_pm_entry(un) != DDI_SUCCESS) { 24170 mutex_enter(SD_MUTEX(un)); 24171 goto done; 24172 } 24173 24174 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24175 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24176 (caddr_t)dev); 24177 24178 sd_pm_exit(un); 24179 24180 mutex_enter(SD_MUTEX(un)); 24181 if (token == NULL) { 24182 rval = EAGAIN; 24183 goto done; 24184 } 24185 24186 /* 24187 * This is a special case IOCTL that doesn't return 24188 * until the media state changes. Routine sdpower 24189 * knows about and handles this so don't count it 24190 * as an active cmd in the driver, which would 24191 * keep the device busy to the pm framework. 24192 * If the count isn't decremented the device can't 24193 * be powered down. 24194 */ 24195 un->un_ncmds_in_driver--; 24196 ASSERT(un->un_ncmds_in_driver >= 0); 24197 24198 /* 24199 * if a prior request had been made, this will be the same 24200 * token, as scsi_watch was designed that way. 24201 */ 24202 un->un_swr_token = token; 24203 un->un_specified_mediastate = state; 24204 24205 /* 24206 * now wait for media change 24207 * we will not be signalled unless mediastate == state but it is 24208 * still better to test for this condition, since there is a 24209 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24210 */ 24211 SD_TRACE(SD_LOG_COMMON, un, 24212 "sd_check_media: waiting for media state change\n"); 24213 while (un->un_mediastate == state) { 24214 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24215 SD_TRACE(SD_LOG_COMMON, un, 24216 "sd_check_media: waiting for media state " 24217 "was interrupted\n"); 24218 un->un_ncmds_in_driver++; 24219 rval = EINTR; 24220 goto done; 24221 } 24222 SD_TRACE(SD_LOG_COMMON, un, 24223 "sd_check_media: received signal, state=%x\n", 24224 un->un_mediastate); 24225 } 24226 /* 24227 * Inc the counter to indicate the device once again 24228 * has an active outstanding cmd. 24229 */ 24230 un->un_ncmds_in_driver++; 24231 } 24232 24233 /* invalidate geometry */ 24234 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24235 sr_ejected(un); 24236 } 24237 24238 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24239 uint64_t capacity; 24240 uint_t lbasize; 24241 24242 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24243 mutex_exit(SD_MUTEX(un)); 24244 /* 24245 * Since the following routines use SD_PATH_DIRECT, we must 24246 * call PM directly before the upcoming disk accesses. This 24247 * may cause the disk to be power/spin up. 24248 */ 24249 24250 if (sd_pm_entry(un) == DDI_SUCCESS) { 24251 rval = sd_send_scsi_READ_CAPACITY(un, 24252 &capacity, 24253 &lbasize, SD_PATH_DIRECT); 24254 if (rval != 0) { 24255 sd_pm_exit(un); 24256 mutex_enter(SD_MUTEX(un)); 24257 goto done; 24258 } 24259 } else { 24260 rval = EIO; 24261 mutex_enter(SD_MUTEX(un)); 24262 goto done; 24263 } 24264 mutex_enter(SD_MUTEX(un)); 24265 24266 sd_update_block_info(un, lbasize, capacity); 24267 24268 un->un_f_geometry_is_valid = FALSE; 24269 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24270 24271 mutex_exit(SD_MUTEX(un)); 24272 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24273 SD_PATH_DIRECT); 24274 sd_pm_exit(un); 24275 24276 mutex_enter(SD_MUTEX(un)); 24277 } 24278 done: 24279 un->un_f_watcht_stopped = FALSE; 24280 if (un->un_swr_token) { 24281 /* 24282 * Use of this local token and the mutex ensures that we avoid 24283 * some race conditions associated with terminating the 24284 * scsi watch. 24285 */ 24286 token = un->un_swr_token; 24287 un->un_swr_token = (opaque_t)NULL; 24288 mutex_exit(SD_MUTEX(un)); 24289 (void) scsi_watch_request_terminate(token, 24290 SCSI_WATCH_TERMINATE_WAIT); 24291 mutex_enter(SD_MUTEX(un)); 24292 } 24293 24294 /* 24295 * Update the capacity kstat value, if no media previously 24296 * (capacity kstat is 0) and a media has been inserted 24297 * (un_f_blockcount_is_valid == TRUE) 24298 */ 24299 if (un->un_errstats) { 24300 struct sd_errstats *stp = NULL; 24301 24302 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24303 if ((stp->sd_capacity.value.ui64 == 0) && 24304 (un->un_f_blockcount_is_valid == TRUE)) { 24305 stp->sd_capacity.value.ui64 = 24306 (uint64_t)((uint64_t)un->un_blockcount * 24307 un->un_sys_blocksize); 24308 } 24309 } 24310 mutex_exit(SD_MUTEX(un)); 24311 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24312 return (rval); 24313 } 24314 24315 24316 /* 24317 * Function: sd_delayed_cv_broadcast 24318 * 24319 * Description: Delayed cv_broadcast to allow for target to recover from media 24320 * insertion. 24321 * 24322 * Arguments: arg - driver soft state (unit) structure 24323 */ 24324 24325 static void 24326 sd_delayed_cv_broadcast(void *arg) 24327 { 24328 struct sd_lun *un = arg; 24329 24330 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24331 24332 mutex_enter(SD_MUTEX(un)); 24333 un->un_dcvb_timeid = NULL; 24334 cv_broadcast(&un->un_state_cv); 24335 mutex_exit(SD_MUTEX(un)); 24336 } 24337 24338 24339 /* 24340 * Function: sd_media_watch_cb 24341 * 24342 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24343 * routine processes the TUR sense data and updates the driver 24344 * state if a transition has occurred. The user thread 24345 * (sd_check_media) is then signalled. 24346 * 24347 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24348 * among multiple watches that share this callback function 24349 * resultp - scsi watch facility result packet containing scsi 24350 * packet, status byte and sense data 24351 * 24352 * Return Code: 0 for success, -1 for failure 24353 */ 24354 24355 static int 24356 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24357 { 24358 struct sd_lun *un; 24359 struct scsi_status *statusp = resultp->statusp; 24360 struct scsi_extended_sense *sensep = resultp->sensep; 24361 enum dkio_state state = DKIO_NONE; 24362 dev_t dev = (dev_t)arg; 24363 uchar_t actual_sense_length; 24364 24365 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24366 return (-1); 24367 } 24368 actual_sense_length = resultp->actual_sense_length; 24369 24370 mutex_enter(SD_MUTEX(un)); 24371 SD_TRACE(SD_LOG_COMMON, un, 24372 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24373 *((char *)statusp), (void *)sensep, actual_sense_length); 24374 24375 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24376 un->un_mediastate = DKIO_DEV_GONE; 24377 cv_broadcast(&un->un_state_cv); 24378 mutex_exit(SD_MUTEX(un)); 24379 24380 return (0); 24381 } 24382 24383 /* 24384 * If there was a check condition then sensep points to valid sense data 24385 * If status was not a check condition but a reservation or busy status 24386 * then the new state is DKIO_NONE 24387 */ 24388 if (sensep != NULL) { 24389 SD_INFO(SD_LOG_COMMON, un, 24390 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24391 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 24392 /* This routine only uses up to 13 bytes of sense data. */ 24393 if (actual_sense_length >= 13) { 24394 if (sensep->es_key == KEY_UNIT_ATTENTION) { 24395 if (sensep->es_add_code == 0x28) { 24396 state = DKIO_INSERTED; 24397 } 24398 } else { 24399 /* 24400 * if 02/04/02 means that the host 24401 * should send start command. Explicitly 24402 * leave the media state as is 24403 * (inserted) as the media is inserted 24404 * and host has stopped device for PM 24405 * reasons. Upon next true read/write 24406 * to this media will bring the 24407 * device to the right state good for 24408 * media access. 24409 */ 24410 if ((sensep->es_key == KEY_NOT_READY) && 24411 (sensep->es_add_code == 0x3a)) { 24412 state = DKIO_EJECTED; 24413 } 24414 24415 /* 24416 * If the drivge is busy with an operation 24417 * or long write, keep the media in an 24418 * inserted state. 24419 */ 24420 24421 if ((sensep->es_key == KEY_NOT_READY) && 24422 (sensep->es_add_code == 0x04) && 24423 ((sensep->es_qual_code == 0x02) || 24424 (sensep->es_qual_code == 0x07) || 24425 (sensep->es_qual_code == 0x08))) { 24426 state = DKIO_INSERTED; 24427 } 24428 } 24429 } 24430 } else if ((*((char *)statusp) == STATUS_GOOD) && 24431 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24432 state = DKIO_INSERTED; 24433 } 24434 24435 SD_TRACE(SD_LOG_COMMON, un, 24436 "sd_media_watch_cb: state=%x, specified=%x\n", 24437 state, un->un_specified_mediastate); 24438 24439 /* 24440 * now signal the waiting thread if this is *not* the specified state; 24441 * delay the signal if the state is DKIO_INSERTED to allow the target 24442 * to recover 24443 */ 24444 if (state != un->un_specified_mediastate) { 24445 un->un_mediastate = state; 24446 if (state == DKIO_INSERTED) { 24447 /* 24448 * delay the signal to give the drive a chance 24449 * to do what it apparently needs to do 24450 */ 24451 SD_TRACE(SD_LOG_COMMON, un, 24452 "sd_media_watch_cb: delayed cv_broadcast\n"); 24453 if (un->un_dcvb_timeid == NULL) { 24454 un->un_dcvb_timeid = 24455 timeout(sd_delayed_cv_broadcast, un, 24456 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24457 } 24458 } else { 24459 SD_TRACE(SD_LOG_COMMON, un, 24460 "sd_media_watch_cb: immediate cv_broadcast\n"); 24461 cv_broadcast(&un->un_state_cv); 24462 } 24463 } 24464 mutex_exit(SD_MUTEX(un)); 24465 return (0); 24466 } 24467 24468 24469 /* 24470 * Function: sd_dkio_get_temp 24471 * 24472 * Description: This routine is the driver entry point for handling ioctl 24473 * requests to get the disk temperature. 24474 * 24475 * Arguments: dev - the device number 24476 * arg - pointer to user provided dk_temperature structure. 24477 * flag - this argument is a pass through to ddi_copyxxx() 24478 * directly from the mode argument of ioctl(). 24479 * 24480 * Return Code: 0 24481 * EFAULT 24482 * ENXIO 24483 * EAGAIN 24484 */ 24485 24486 static int 24487 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24488 { 24489 struct sd_lun *un = NULL; 24490 struct dk_temperature *dktemp = NULL; 24491 uchar_t *temperature_page; 24492 int rval = 0; 24493 int path_flag = SD_PATH_STANDARD; 24494 24495 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24496 return (ENXIO); 24497 } 24498 24499 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24500 24501 /* copyin the disk temp argument to get the user flags */ 24502 if (ddi_copyin((void *)arg, dktemp, 24503 sizeof (struct dk_temperature), flag) != 0) { 24504 rval = EFAULT; 24505 goto done; 24506 } 24507 24508 /* Initialize the temperature to invalid. */ 24509 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24510 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24511 24512 /* 24513 * Note: Investigate removing the "bypass pm" semantic. 24514 * Can we just bypass PM always? 24515 */ 24516 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24517 path_flag = SD_PATH_DIRECT; 24518 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24519 mutex_enter(&un->un_pm_mutex); 24520 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24521 /* 24522 * If DKT_BYPASS_PM is set, and the drive happens to be 24523 * in low power mode, we can not wake it up, Need to 24524 * return EAGAIN. 24525 */ 24526 mutex_exit(&un->un_pm_mutex); 24527 rval = EAGAIN; 24528 goto done; 24529 } else { 24530 /* 24531 * Indicate to PM the device is busy. This is required 24532 * to avoid a race - i.e. the ioctl is issuing a 24533 * command and the pm framework brings down the device 24534 * to low power mode (possible power cut-off on some 24535 * platforms). 24536 */ 24537 mutex_exit(&un->un_pm_mutex); 24538 if (sd_pm_entry(un) != DDI_SUCCESS) { 24539 rval = EAGAIN; 24540 goto done; 24541 } 24542 } 24543 } 24544 24545 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24546 24547 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24548 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24549 goto done2; 24550 } 24551 24552 /* 24553 * For the current temperature verify that the parameter length is 0x02 24554 * and the parameter code is 0x00 24555 */ 24556 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24557 (temperature_page[5] == 0x00)) { 24558 if (temperature_page[9] == 0xFF) { 24559 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24560 } else { 24561 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24562 } 24563 } 24564 24565 /* 24566 * For the reference temperature verify that the parameter 24567 * length is 0x02 and the parameter code is 0x01 24568 */ 24569 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24570 (temperature_page[11] == 0x01)) { 24571 if (temperature_page[15] == 0xFF) { 24572 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24573 } else { 24574 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24575 } 24576 } 24577 24578 /* Do the copyout regardless of the temperature commands status. */ 24579 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24580 flag) != 0) { 24581 rval = EFAULT; 24582 } 24583 24584 done2: 24585 if (path_flag == SD_PATH_DIRECT) { 24586 sd_pm_exit(un); 24587 } 24588 24589 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24590 done: 24591 if (dktemp != NULL) { 24592 kmem_free(dktemp, sizeof (struct dk_temperature)); 24593 } 24594 24595 return (rval); 24596 } 24597 24598 24599 /* 24600 * Function: sd_log_page_supported 24601 * 24602 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24603 * supported log pages. 24604 * 24605 * Arguments: un - 24606 * log_page - 24607 * 24608 * Return Code: -1 - on error (log sense is optional and may not be supported). 24609 * 0 - log page not found. 24610 * 1 - log page found. 24611 */ 24612 24613 static int 24614 sd_log_page_supported(struct sd_lun *un, int log_page) 24615 { 24616 uchar_t *log_page_data; 24617 int i; 24618 int match = 0; 24619 int log_size; 24620 24621 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24622 24623 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24624 SD_PATH_DIRECT) != 0) { 24625 SD_ERROR(SD_LOG_COMMON, un, 24626 "sd_log_page_supported: failed log page retrieval\n"); 24627 kmem_free(log_page_data, 0xFF); 24628 return (-1); 24629 } 24630 log_size = log_page_data[3]; 24631 24632 /* 24633 * The list of supported log pages start from the fourth byte. Check 24634 * until we run out of log pages or a match is found. 24635 */ 24636 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24637 if (log_page_data[i] == log_page) { 24638 match++; 24639 } 24640 } 24641 kmem_free(log_page_data, 0xFF); 24642 return (match); 24643 } 24644 24645 24646 /* 24647 * Function: sd_mhdioc_failfast 24648 * 24649 * Description: This routine is the driver entry point for handling ioctl 24650 * requests to enable/disable the multihost failfast option. 24651 * (MHIOCENFAILFAST) 24652 * 24653 * Arguments: dev - the device number 24654 * arg - user specified probing interval. 24655 * flag - this argument is a pass through to ddi_copyxxx() 24656 * directly from the mode argument of ioctl(). 24657 * 24658 * Return Code: 0 24659 * EFAULT 24660 * ENXIO 24661 */ 24662 24663 static int 24664 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24665 { 24666 struct sd_lun *un = NULL; 24667 int mh_time; 24668 int rval = 0; 24669 24670 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24671 return (ENXIO); 24672 } 24673 24674 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24675 return (EFAULT); 24676 24677 if (mh_time) { 24678 mutex_enter(SD_MUTEX(un)); 24679 un->un_resvd_status |= SD_FAILFAST; 24680 mutex_exit(SD_MUTEX(un)); 24681 /* 24682 * If mh_time is INT_MAX, then this ioctl is being used for 24683 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24684 */ 24685 if (mh_time != INT_MAX) { 24686 rval = sd_check_mhd(dev, mh_time); 24687 } 24688 } else { 24689 (void) sd_check_mhd(dev, 0); 24690 mutex_enter(SD_MUTEX(un)); 24691 un->un_resvd_status &= ~SD_FAILFAST; 24692 mutex_exit(SD_MUTEX(un)); 24693 } 24694 return (rval); 24695 } 24696 24697 24698 /* 24699 * Function: sd_mhdioc_takeown 24700 * 24701 * Description: This routine is the driver entry point for handling ioctl 24702 * requests to forcefully acquire exclusive access rights to the 24703 * multihost disk (MHIOCTKOWN). 24704 * 24705 * Arguments: dev - the device number 24706 * arg - user provided structure specifying the delay 24707 * parameters in milliseconds 24708 * flag - this argument is a pass through to ddi_copyxxx() 24709 * directly from the mode argument of ioctl(). 24710 * 24711 * Return Code: 0 24712 * EFAULT 24713 * ENXIO 24714 */ 24715 24716 static int 24717 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24718 { 24719 struct sd_lun *un = NULL; 24720 struct mhioctkown *tkown = NULL; 24721 int rval = 0; 24722 24723 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24724 return (ENXIO); 24725 } 24726 24727 if (arg != NULL) { 24728 tkown = (struct mhioctkown *) 24729 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24730 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24731 if (rval != 0) { 24732 rval = EFAULT; 24733 goto error; 24734 } 24735 } 24736 24737 rval = sd_take_ownership(dev, tkown); 24738 mutex_enter(SD_MUTEX(un)); 24739 if (rval == 0) { 24740 un->un_resvd_status |= SD_RESERVE; 24741 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24742 sd_reinstate_resv_delay = 24743 tkown->reinstate_resv_delay * 1000; 24744 } else { 24745 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24746 } 24747 /* 24748 * Give the scsi_watch routine interval set by 24749 * the MHIOCENFAILFAST ioctl precedence here. 24750 */ 24751 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24752 mutex_exit(SD_MUTEX(un)); 24753 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24754 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24755 "sd_mhdioc_takeown : %d\n", 24756 sd_reinstate_resv_delay); 24757 } else { 24758 mutex_exit(SD_MUTEX(un)); 24759 } 24760 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24761 sd_mhd_reset_notify_cb, (caddr_t)un); 24762 } else { 24763 un->un_resvd_status &= ~SD_RESERVE; 24764 mutex_exit(SD_MUTEX(un)); 24765 } 24766 24767 error: 24768 if (tkown != NULL) { 24769 kmem_free(tkown, sizeof (struct mhioctkown)); 24770 } 24771 return (rval); 24772 } 24773 24774 24775 /* 24776 * Function: sd_mhdioc_release 24777 * 24778 * Description: This routine is the driver entry point for handling ioctl 24779 * requests to release exclusive access rights to the multihost 24780 * disk (MHIOCRELEASE). 24781 * 24782 * Arguments: dev - the device number 24783 * 24784 * Return Code: 0 24785 * ENXIO 24786 */ 24787 24788 static int 24789 sd_mhdioc_release(dev_t dev) 24790 { 24791 struct sd_lun *un = NULL; 24792 timeout_id_t resvd_timeid_save; 24793 int resvd_status_save; 24794 int rval = 0; 24795 24796 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24797 return (ENXIO); 24798 } 24799 24800 mutex_enter(SD_MUTEX(un)); 24801 resvd_status_save = un->un_resvd_status; 24802 un->un_resvd_status &= 24803 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24804 if (un->un_resvd_timeid) { 24805 resvd_timeid_save = un->un_resvd_timeid; 24806 un->un_resvd_timeid = NULL; 24807 mutex_exit(SD_MUTEX(un)); 24808 (void) untimeout(resvd_timeid_save); 24809 } else { 24810 mutex_exit(SD_MUTEX(un)); 24811 } 24812 24813 /* 24814 * destroy any pending timeout thread that may be attempting to 24815 * reinstate reservation on this device. 24816 */ 24817 sd_rmv_resv_reclaim_req(dev); 24818 24819 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24820 mutex_enter(SD_MUTEX(un)); 24821 if ((un->un_mhd_token) && 24822 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24823 mutex_exit(SD_MUTEX(un)); 24824 (void) sd_check_mhd(dev, 0); 24825 } else { 24826 mutex_exit(SD_MUTEX(un)); 24827 } 24828 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24829 sd_mhd_reset_notify_cb, (caddr_t)un); 24830 } else { 24831 /* 24832 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24833 */ 24834 mutex_enter(SD_MUTEX(un)); 24835 un->un_resvd_status = resvd_status_save; 24836 mutex_exit(SD_MUTEX(un)); 24837 } 24838 return (rval); 24839 } 24840 24841 24842 /* 24843 * Function: sd_mhdioc_register_devid 24844 * 24845 * Description: This routine is the driver entry point for handling ioctl 24846 * requests to register the device id (MHIOCREREGISTERDEVID). 24847 * 24848 * Note: The implementation for this ioctl has been updated to 24849 * be consistent with the original PSARC case (1999/357) 24850 * (4375899, 4241671, 4220005) 24851 * 24852 * Arguments: dev - the device number 24853 * 24854 * Return Code: 0 24855 * ENXIO 24856 */ 24857 24858 static int 24859 sd_mhdioc_register_devid(dev_t dev) 24860 { 24861 struct sd_lun *un = NULL; 24862 int rval = 0; 24863 24864 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24865 return (ENXIO); 24866 } 24867 24868 ASSERT(!mutex_owned(SD_MUTEX(un))); 24869 24870 mutex_enter(SD_MUTEX(un)); 24871 24872 /* If a devid already exists, de-register it */ 24873 if (un->un_devid != NULL) { 24874 ddi_devid_unregister(SD_DEVINFO(un)); 24875 /* 24876 * After unregister devid, needs to free devid memory 24877 */ 24878 ddi_devid_free(un->un_devid); 24879 un->un_devid = NULL; 24880 } 24881 24882 /* Check for reservation conflict */ 24883 mutex_exit(SD_MUTEX(un)); 24884 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24885 mutex_enter(SD_MUTEX(un)); 24886 24887 switch (rval) { 24888 case 0: 24889 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24890 break; 24891 case EACCES: 24892 break; 24893 default: 24894 rval = EIO; 24895 } 24896 24897 mutex_exit(SD_MUTEX(un)); 24898 return (rval); 24899 } 24900 24901 24902 /* 24903 * Function: sd_mhdioc_inkeys 24904 * 24905 * Description: This routine is the driver entry point for handling ioctl 24906 * requests to issue the SCSI-3 Persistent In Read Keys command 24907 * to the device (MHIOCGRP_INKEYS). 24908 * 24909 * Arguments: dev - the device number 24910 * arg - user provided in_keys structure 24911 * flag - this argument is a pass through to ddi_copyxxx() 24912 * directly from the mode argument of ioctl(). 24913 * 24914 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24915 * ENXIO 24916 * EFAULT 24917 */ 24918 24919 static int 24920 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24921 { 24922 struct sd_lun *un; 24923 mhioc_inkeys_t inkeys; 24924 int rval = 0; 24925 24926 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24927 return (ENXIO); 24928 } 24929 24930 #ifdef _MULTI_DATAMODEL 24931 switch (ddi_model_convert_from(flag & FMODELS)) { 24932 case DDI_MODEL_ILP32: { 24933 struct mhioc_inkeys32 inkeys32; 24934 24935 if (ddi_copyin(arg, &inkeys32, 24936 sizeof (struct mhioc_inkeys32), flag) != 0) { 24937 return (EFAULT); 24938 } 24939 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24940 if ((rval = sd_persistent_reservation_in_read_keys(un, 24941 &inkeys, flag)) != 0) { 24942 return (rval); 24943 } 24944 inkeys32.generation = inkeys.generation; 24945 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24946 flag) != 0) { 24947 return (EFAULT); 24948 } 24949 break; 24950 } 24951 case DDI_MODEL_NONE: 24952 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24953 flag) != 0) { 24954 return (EFAULT); 24955 } 24956 if ((rval = sd_persistent_reservation_in_read_keys(un, 24957 &inkeys, flag)) != 0) { 24958 return (rval); 24959 } 24960 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24961 flag) != 0) { 24962 return (EFAULT); 24963 } 24964 break; 24965 } 24966 24967 #else /* ! _MULTI_DATAMODEL */ 24968 24969 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24970 return (EFAULT); 24971 } 24972 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24973 if (rval != 0) { 24974 return (rval); 24975 } 24976 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24977 return (EFAULT); 24978 } 24979 24980 #endif /* _MULTI_DATAMODEL */ 24981 24982 return (rval); 24983 } 24984 24985 24986 /* 24987 * Function: sd_mhdioc_inresv 24988 * 24989 * Description: This routine is the driver entry point for handling ioctl 24990 * requests to issue the SCSI-3 Persistent In Read Reservations 24991 * command to the device (MHIOCGRP_INKEYS). 24992 * 24993 * Arguments: dev - the device number 24994 * arg - user provided in_resv structure 24995 * flag - this argument is a pass through to ddi_copyxxx() 24996 * directly from the mode argument of ioctl(). 24997 * 24998 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24999 * ENXIO 25000 * EFAULT 25001 */ 25002 25003 static int 25004 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25005 { 25006 struct sd_lun *un; 25007 mhioc_inresvs_t inresvs; 25008 int rval = 0; 25009 25010 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25011 return (ENXIO); 25012 } 25013 25014 #ifdef _MULTI_DATAMODEL 25015 25016 switch (ddi_model_convert_from(flag & FMODELS)) { 25017 case DDI_MODEL_ILP32: { 25018 struct mhioc_inresvs32 inresvs32; 25019 25020 if (ddi_copyin(arg, &inresvs32, 25021 sizeof (struct mhioc_inresvs32), flag) != 0) { 25022 return (EFAULT); 25023 } 25024 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25025 if ((rval = sd_persistent_reservation_in_read_resv(un, 25026 &inresvs, flag)) != 0) { 25027 return (rval); 25028 } 25029 inresvs32.generation = inresvs.generation; 25030 if (ddi_copyout(&inresvs32, arg, 25031 sizeof (struct mhioc_inresvs32), flag) != 0) { 25032 return (EFAULT); 25033 } 25034 break; 25035 } 25036 case DDI_MODEL_NONE: 25037 if (ddi_copyin(arg, &inresvs, 25038 sizeof (mhioc_inresvs_t), flag) != 0) { 25039 return (EFAULT); 25040 } 25041 if ((rval = sd_persistent_reservation_in_read_resv(un, 25042 &inresvs, flag)) != 0) { 25043 return (rval); 25044 } 25045 if (ddi_copyout(&inresvs, arg, 25046 sizeof (mhioc_inresvs_t), flag) != 0) { 25047 return (EFAULT); 25048 } 25049 break; 25050 } 25051 25052 #else /* ! _MULTI_DATAMODEL */ 25053 25054 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25055 return (EFAULT); 25056 } 25057 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25058 if (rval != 0) { 25059 return (rval); 25060 } 25061 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25062 return (EFAULT); 25063 } 25064 25065 #endif /* ! _MULTI_DATAMODEL */ 25066 25067 return (rval); 25068 } 25069 25070 25071 /* 25072 * The following routines support the clustering functionality described below 25073 * and implement lost reservation reclaim functionality. 25074 * 25075 * Clustering 25076 * ---------- 25077 * The clustering code uses two different, independent forms of SCSI 25078 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25079 * Persistent Group Reservations. For any particular disk, it will use either 25080 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25081 * 25082 * SCSI-2 25083 * The cluster software takes ownership of a multi-hosted disk by issuing the 25084 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25085 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25086 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25087 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25088 * meaning of failfast is that if the driver (on this host) ever encounters the 25089 * scsi error return code RESERVATION_CONFLICT from the device, it should 25090 * immediately panic the host. The motivation for this ioctl is that if this 25091 * host does encounter reservation conflict, the underlying cause is that some 25092 * other host of the cluster has decided that this host is no longer in the 25093 * cluster and has seized control of the disks for itself. Since this host is no 25094 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25095 * does two things: 25096 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25097 * error to panic the host 25098 * (b) it sets up a periodic timer to test whether this host still has 25099 * "access" (in that no other host has reserved the device): if the 25100 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25101 * purpose of that periodic timer is to handle scenarios where the host is 25102 * otherwise temporarily quiescent, temporarily doing no real i/o. 25103 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25104 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25105 * the device itself. 25106 * 25107 * SCSI-3 PGR 25108 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25109 * facility is supported through the shared multihost disk ioctls 25110 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25111 * MHIOCGRP_PREEMPTANDABORT) 25112 * 25113 * Reservation Reclaim: 25114 * -------------------- 25115 * To support the lost reservation reclaim operations this driver creates a 25116 * single thread to handle reinstating reservations on all devices that have 25117 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25118 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25119 * and the reservation reclaim thread loops through the requests to regain the 25120 * lost reservations. 25121 */ 25122 25123 /* 25124 * Function: sd_check_mhd() 25125 * 25126 * Description: This function sets up and submits a scsi watch request or 25127 * terminates an existing watch request. This routine is used in 25128 * support of reservation reclaim. 25129 * 25130 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25131 * among multiple watches that share the callback function 25132 * interval - the number of microseconds specifying the watch 25133 * interval for issuing TEST UNIT READY commands. If 25134 * set to 0 the watch should be terminated. If the 25135 * interval is set to 0 and if the device is required 25136 * to hold reservation while disabling failfast, the 25137 * watch is restarted with an interval of 25138 * reinstate_resv_delay. 25139 * 25140 * Return Code: 0 - Successful submit/terminate of scsi watch request 25141 * ENXIO - Indicates an invalid device was specified 25142 * EAGAIN - Unable to submit the scsi watch request 25143 */ 25144 25145 static int 25146 sd_check_mhd(dev_t dev, int interval) 25147 { 25148 struct sd_lun *un; 25149 opaque_t token; 25150 25151 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25152 return (ENXIO); 25153 } 25154 25155 /* is this a watch termination request? */ 25156 if (interval == 0) { 25157 mutex_enter(SD_MUTEX(un)); 25158 /* if there is an existing watch task then terminate it */ 25159 if (un->un_mhd_token) { 25160 token = un->un_mhd_token; 25161 un->un_mhd_token = NULL; 25162 mutex_exit(SD_MUTEX(un)); 25163 (void) scsi_watch_request_terminate(token, 25164 SCSI_WATCH_TERMINATE_WAIT); 25165 mutex_enter(SD_MUTEX(un)); 25166 } else { 25167 mutex_exit(SD_MUTEX(un)); 25168 /* 25169 * Note: If we return here we don't check for the 25170 * failfast case. This is the original legacy 25171 * implementation but perhaps we should be checking 25172 * the failfast case. 25173 */ 25174 return (0); 25175 } 25176 /* 25177 * If the device is required to hold reservation while 25178 * disabling failfast, we need to restart the scsi_watch 25179 * routine with an interval of reinstate_resv_delay. 25180 */ 25181 if (un->un_resvd_status & SD_RESERVE) { 25182 interval = sd_reinstate_resv_delay/1000; 25183 } else { 25184 /* no failfast so bail */ 25185 mutex_exit(SD_MUTEX(un)); 25186 return (0); 25187 } 25188 mutex_exit(SD_MUTEX(un)); 25189 } 25190 25191 /* 25192 * adjust minimum time interval to 1 second, 25193 * and convert from msecs to usecs 25194 */ 25195 if (interval > 0 && interval < 1000) { 25196 interval = 1000; 25197 } 25198 interval *= 1000; 25199 25200 /* 25201 * submit the request to the scsi_watch service 25202 */ 25203 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25204 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25205 if (token == NULL) { 25206 return (EAGAIN); 25207 } 25208 25209 /* 25210 * save token for termination later on 25211 */ 25212 mutex_enter(SD_MUTEX(un)); 25213 un->un_mhd_token = token; 25214 mutex_exit(SD_MUTEX(un)); 25215 return (0); 25216 } 25217 25218 25219 /* 25220 * Function: sd_mhd_watch_cb() 25221 * 25222 * Description: This function is the call back function used by the scsi watch 25223 * facility. The scsi watch facility sends the "Test Unit Ready" 25224 * and processes the status. If applicable (i.e. a "Unit Attention" 25225 * status and automatic "Request Sense" not used) the scsi watch 25226 * facility will send a "Request Sense" and retrieve the sense data 25227 * to be passed to this callback function. In either case the 25228 * automatic "Request Sense" or the facility submitting one, this 25229 * callback is passed the status and sense data. 25230 * 25231 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25232 * among multiple watches that share this callback function 25233 * resultp - scsi watch facility result packet containing scsi 25234 * packet, status byte and sense data 25235 * 25236 * Return Code: 0 - continue the watch task 25237 * non-zero - terminate the watch task 25238 */ 25239 25240 static int 25241 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25242 { 25243 struct sd_lun *un; 25244 struct scsi_status *statusp; 25245 struct scsi_extended_sense *sensep; 25246 struct scsi_pkt *pkt; 25247 uchar_t actual_sense_length; 25248 dev_t dev = (dev_t)arg; 25249 25250 ASSERT(resultp != NULL); 25251 statusp = resultp->statusp; 25252 sensep = resultp->sensep; 25253 pkt = resultp->pkt; 25254 actual_sense_length = resultp->actual_sense_length; 25255 25256 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25257 return (ENXIO); 25258 } 25259 25260 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25261 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25262 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25263 25264 /* Begin processing of the status and/or sense data */ 25265 if (pkt->pkt_reason != CMD_CMPLT) { 25266 /* Handle the incomplete packet */ 25267 sd_mhd_watch_incomplete(un, pkt); 25268 return (0); 25269 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25270 if (*((unsigned char *)statusp) 25271 == STATUS_RESERVATION_CONFLICT) { 25272 /* 25273 * Handle a reservation conflict by panicking if 25274 * configured for failfast or by logging the conflict 25275 * and updating the reservation status 25276 */ 25277 mutex_enter(SD_MUTEX(un)); 25278 if ((un->un_resvd_status & SD_FAILFAST) && 25279 (sd_failfast_enable)) { 25280 sd_panic_for_res_conflict(un); 25281 /*NOTREACHED*/ 25282 } 25283 SD_INFO(SD_LOG_IOCTL_MHD, un, 25284 "sd_mhd_watch_cb: Reservation Conflict\n"); 25285 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25286 mutex_exit(SD_MUTEX(un)); 25287 } 25288 } 25289 25290 if (sensep != NULL) { 25291 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25292 mutex_enter(SD_MUTEX(un)); 25293 if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) && 25294 (un->un_resvd_status & SD_RESERVE)) { 25295 /* 25296 * The additional sense code indicates a power 25297 * on or bus device reset has occurred; update 25298 * the reservation status. 25299 */ 25300 un->un_resvd_status |= 25301 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25302 SD_INFO(SD_LOG_IOCTL_MHD, un, 25303 "sd_mhd_watch_cb: Lost Reservation\n"); 25304 } 25305 } else { 25306 return (0); 25307 } 25308 } else { 25309 mutex_enter(SD_MUTEX(un)); 25310 } 25311 25312 if ((un->un_resvd_status & SD_RESERVE) && 25313 (un->un_resvd_status & SD_LOST_RESERVE)) { 25314 if (un->un_resvd_status & SD_WANT_RESERVE) { 25315 /* 25316 * A reset occurred in between the last probe and this 25317 * one so if a timeout is pending cancel it. 25318 */ 25319 if (un->un_resvd_timeid) { 25320 timeout_id_t temp_id = un->un_resvd_timeid; 25321 un->un_resvd_timeid = NULL; 25322 mutex_exit(SD_MUTEX(un)); 25323 (void) untimeout(temp_id); 25324 mutex_enter(SD_MUTEX(un)); 25325 } 25326 un->un_resvd_status &= ~SD_WANT_RESERVE; 25327 } 25328 if (un->un_resvd_timeid == 0) { 25329 /* Schedule a timeout to handle the lost reservation */ 25330 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25331 (void *)dev, 25332 drv_usectohz(sd_reinstate_resv_delay)); 25333 } 25334 } 25335 mutex_exit(SD_MUTEX(un)); 25336 return (0); 25337 } 25338 25339 25340 /* 25341 * Function: sd_mhd_watch_incomplete() 25342 * 25343 * Description: This function is used to find out why a scsi pkt sent by the 25344 * scsi watch facility was not completed. Under some scenarios this 25345 * routine will return. Otherwise it will send a bus reset to see 25346 * if the drive is still online. 25347 * 25348 * Arguments: un - driver soft state (unit) structure 25349 * pkt - incomplete scsi pkt 25350 */ 25351 25352 static void 25353 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25354 { 25355 int be_chatty; 25356 int perr; 25357 25358 ASSERT(pkt != NULL); 25359 ASSERT(un != NULL); 25360 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25361 perr = (pkt->pkt_statistics & STAT_PERR); 25362 25363 mutex_enter(SD_MUTEX(un)); 25364 if (un->un_state == SD_STATE_DUMPING) { 25365 mutex_exit(SD_MUTEX(un)); 25366 return; 25367 } 25368 25369 switch (pkt->pkt_reason) { 25370 case CMD_UNX_BUS_FREE: 25371 /* 25372 * If we had a parity error that caused the target to drop BSY*, 25373 * don't be chatty about it. 25374 */ 25375 if (perr && be_chatty) { 25376 be_chatty = 0; 25377 } 25378 break; 25379 case CMD_TAG_REJECT: 25380 /* 25381 * The SCSI-2 spec states that a tag reject will be sent by the 25382 * target if tagged queuing is not supported. A tag reject may 25383 * also be sent during certain initialization periods or to 25384 * control internal resources. For the latter case the target 25385 * may also return Queue Full. 25386 * 25387 * If this driver receives a tag reject from a target that is 25388 * going through an init period or controlling internal 25389 * resources tagged queuing will be disabled. This is a less 25390 * than optimal behavior but the driver is unable to determine 25391 * the target state and assumes tagged queueing is not supported 25392 */ 25393 pkt->pkt_flags = 0; 25394 un->un_tagflags = 0; 25395 25396 if (un->un_f_opt_queueing == TRUE) { 25397 un->un_throttle = min(un->un_throttle, 3); 25398 } else { 25399 un->un_throttle = 1; 25400 } 25401 mutex_exit(SD_MUTEX(un)); 25402 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25403 mutex_enter(SD_MUTEX(un)); 25404 break; 25405 case CMD_INCOMPLETE: 25406 /* 25407 * The transport stopped with an abnormal state, fallthrough and 25408 * reset the target and/or bus unless selection did not complete 25409 * (indicated by STATE_GOT_BUS) in which case we don't want to 25410 * go through a target/bus reset 25411 */ 25412 if (pkt->pkt_state == STATE_GOT_BUS) { 25413 break; 25414 } 25415 /*FALLTHROUGH*/ 25416 25417 case CMD_TIMEOUT: 25418 default: 25419 /* 25420 * The lun may still be running the command, so a lun reset 25421 * should be attempted. If the lun reset fails or cannot be 25422 * issued, than try a target reset. Lastly try a bus reset. 25423 */ 25424 if ((pkt->pkt_statistics & 25425 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25426 int reset_retval = 0; 25427 mutex_exit(SD_MUTEX(un)); 25428 if (un->un_f_allow_bus_device_reset == TRUE) { 25429 if (un->un_f_lun_reset_enabled == TRUE) { 25430 reset_retval = 25431 scsi_reset(SD_ADDRESS(un), 25432 RESET_LUN); 25433 } 25434 if (reset_retval == 0) { 25435 reset_retval = 25436 scsi_reset(SD_ADDRESS(un), 25437 RESET_TARGET); 25438 } 25439 } 25440 if (reset_retval == 0) { 25441 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25442 } 25443 mutex_enter(SD_MUTEX(un)); 25444 } 25445 break; 25446 } 25447 25448 /* A device/bus reset has occurred; update the reservation status. */ 25449 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25450 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25451 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25452 un->un_resvd_status |= 25453 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25454 SD_INFO(SD_LOG_IOCTL_MHD, un, 25455 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25456 } 25457 } 25458 25459 /* 25460 * The disk has been turned off; Update the device state. 25461 * 25462 * Note: Should we be offlining the disk here? 25463 */ 25464 if (pkt->pkt_state == STATE_GOT_BUS) { 25465 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25466 "Disk not responding to selection\n"); 25467 if (un->un_state != SD_STATE_OFFLINE) { 25468 New_state(un, SD_STATE_OFFLINE); 25469 } 25470 } else if (be_chatty) { 25471 /* 25472 * suppress messages if they are all the same pkt reason; 25473 * with TQ, many (up to 256) are returned with the same 25474 * pkt_reason 25475 */ 25476 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25477 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25478 "sd_mhd_watch_incomplete: " 25479 "SCSI transport failed: reason '%s'\n", 25480 scsi_rname(pkt->pkt_reason)); 25481 } 25482 } 25483 un->un_last_pkt_reason = pkt->pkt_reason; 25484 mutex_exit(SD_MUTEX(un)); 25485 } 25486 25487 25488 /* 25489 * Function: sd_sname() 25490 * 25491 * Description: This is a simple little routine to return a string containing 25492 * a printable description of command status byte for use in 25493 * logging. 25494 * 25495 * Arguments: status - pointer to a status byte 25496 * 25497 * Return Code: char * - string containing status description. 25498 */ 25499 25500 static char * 25501 sd_sname(uchar_t status) 25502 { 25503 switch (status & STATUS_MASK) { 25504 case STATUS_GOOD: 25505 return ("good status"); 25506 case STATUS_CHECK: 25507 return ("check condition"); 25508 case STATUS_MET: 25509 return ("condition met"); 25510 case STATUS_BUSY: 25511 return ("busy"); 25512 case STATUS_INTERMEDIATE: 25513 return ("intermediate"); 25514 case STATUS_INTERMEDIATE_MET: 25515 return ("intermediate - condition met"); 25516 case STATUS_RESERVATION_CONFLICT: 25517 return ("reservation_conflict"); 25518 case STATUS_TERMINATED: 25519 return ("command terminated"); 25520 case STATUS_QFULL: 25521 return ("queue full"); 25522 default: 25523 return ("<unknown status>"); 25524 } 25525 } 25526 25527 25528 /* 25529 * Function: sd_mhd_resvd_recover() 25530 * 25531 * Description: This function adds a reservation entry to the 25532 * sd_resv_reclaim_request list and signals the reservation 25533 * reclaim thread that there is work pending. If the reservation 25534 * reclaim thread has not been previously created this function 25535 * will kick it off. 25536 * 25537 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25538 * among multiple watches that share this callback function 25539 * 25540 * Context: This routine is called by timeout() and is run in interrupt 25541 * context. It must not sleep or call other functions which may 25542 * sleep. 25543 */ 25544 25545 static void 25546 sd_mhd_resvd_recover(void *arg) 25547 { 25548 dev_t dev = (dev_t)arg; 25549 struct sd_lun *un; 25550 struct sd_thr_request *sd_treq = NULL; 25551 struct sd_thr_request *sd_cur = NULL; 25552 struct sd_thr_request *sd_prev = NULL; 25553 int already_there = 0; 25554 25555 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25556 return; 25557 } 25558 25559 mutex_enter(SD_MUTEX(un)); 25560 un->un_resvd_timeid = NULL; 25561 if (un->un_resvd_status & SD_WANT_RESERVE) { 25562 /* 25563 * There was a reset so don't issue the reserve, allow the 25564 * sd_mhd_watch_cb callback function to notice this and 25565 * reschedule the timeout for reservation. 25566 */ 25567 mutex_exit(SD_MUTEX(un)); 25568 return; 25569 } 25570 mutex_exit(SD_MUTEX(un)); 25571 25572 /* 25573 * Add this device to the sd_resv_reclaim_request list and the 25574 * sd_resv_reclaim_thread should take care of the rest. 25575 * 25576 * Note: We can't sleep in this context so if the memory allocation 25577 * fails allow the sd_mhd_watch_cb callback function to notice this and 25578 * reschedule the timeout for reservation. (4378460) 25579 */ 25580 sd_treq = (struct sd_thr_request *) 25581 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25582 if (sd_treq == NULL) { 25583 return; 25584 } 25585 25586 sd_treq->sd_thr_req_next = NULL; 25587 sd_treq->dev = dev; 25588 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25589 if (sd_tr.srq_thr_req_head == NULL) { 25590 sd_tr.srq_thr_req_head = sd_treq; 25591 } else { 25592 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25593 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25594 if (sd_cur->dev == dev) { 25595 /* 25596 * already in Queue so don't log 25597 * another request for the device 25598 */ 25599 already_there = 1; 25600 break; 25601 } 25602 sd_prev = sd_cur; 25603 } 25604 if (!already_there) { 25605 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25606 "logging request for %lx\n", dev); 25607 sd_prev->sd_thr_req_next = sd_treq; 25608 } else { 25609 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25610 } 25611 } 25612 25613 /* 25614 * Create a kernel thread to do the reservation reclaim and free up this 25615 * thread. We cannot block this thread while we go away to do the 25616 * reservation reclaim 25617 */ 25618 if (sd_tr.srq_resv_reclaim_thread == NULL) 25619 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25620 sd_resv_reclaim_thread, NULL, 25621 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25622 25623 /* Tell the reservation reclaim thread that it has work to do */ 25624 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25625 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25626 } 25627 25628 /* 25629 * Function: sd_resv_reclaim_thread() 25630 * 25631 * Description: This function implements the reservation reclaim operations 25632 * 25633 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25634 * among multiple watches that share this callback function 25635 */ 25636 25637 static void 25638 sd_resv_reclaim_thread() 25639 { 25640 struct sd_lun *un; 25641 struct sd_thr_request *sd_mhreq; 25642 25643 /* Wait for work */ 25644 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25645 if (sd_tr.srq_thr_req_head == NULL) { 25646 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25647 &sd_tr.srq_resv_reclaim_mutex); 25648 } 25649 25650 /* Loop while we have work */ 25651 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25652 un = ddi_get_soft_state(sd_state, 25653 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25654 if (un == NULL) { 25655 /* 25656 * softstate structure is NULL so just 25657 * dequeue the request and continue 25658 */ 25659 sd_tr.srq_thr_req_head = 25660 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25661 kmem_free(sd_tr.srq_thr_cur_req, 25662 sizeof (struct sd_thr_request)); 25663 continue; 25664 } 25665 25666 /* dequeue the request */ 25667 sd_mhreq = sd_tr.srq_thr_cur_req; 25668 sd_tr.srq_thr_req_head = 25669 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25670 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25671 25672 /* 25673 * Reclaim reservation only if SD_RESERVE is still set. There 25674 * may have been a call to MHIOCRELEASE before we got here. 25675 */ 25676 mutex_enter(SD_MUTEX(un)); 25677 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25678 /* 25679 * Note: The SD_LOST_RESERVE flag is cleared before 25680 * reclaiming the reservation. If this is done after the 25681 * call to sd_reserve_release a reservation loss in the 25682 * window between pkt completion of reserve cmd and 25683 * mutex_enter below may not be recognized 25684 */ 25685 un->un_resvd_status &= ~SD_LOST_RESERVE; 25686 mutex_exit(SD_MUTEX(un)); 25687 25688 if (sd_reserve_release(sd_mhreq->dev, 25689 SD_RESERVE) == 0) { 25690 mutex_enter(SD_MUTEX(un)); 25691 un->un_resvd_status |= SD_RESERVE; 25692 mutex_exit(SD_MUTEX(un)); 25693 SD_INFO(SD_LOG_IOCTL_MHD, un, 25694 "sd_resv_reclaim_thread: " 25695 "Reservation Recovered\n"); 25696 } else { 25697 mutex_enter(SD_MUTEX(un)); 25698 un->un_resvd_status |= SD_LOST_RESERVE; 25699 mutex_exit(SD_MUTEX(un)); 25700 SD_INFO(SD_LOG_IOCTL_MHD, un, 25701 "sd_resv_reclaim_thread: Failed " 25702 "Reservation Recovery\n"); 25703 } 25704 } else { 25705 mutex_exit(SD_MUTEX(un)); 25706 } 25707 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25708 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25709 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25710 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25711 /* 25712 * wakeup the destroy thread if anyone is waiting on 25713 * us to complete. 25714 */ 25715 cv_signal(&sd_tr.srq_inprocess_cv); 25716 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25717 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25718 } 25719 25720 /* 25721 * cleanup the sd_tr structure now that this thread will not exist 25722 */ 25723 ASSERT(sd_tr.srq_thr_req_head == NULL); 25724 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25725 sd_tr.srq_resv_reclaim_thread = NULL; 25726 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25727 thread_exit(); 25728 } 25729 25730 25731 /* 25732 * Function: sd_rmv_resv_reclaim_req() 25733 * 25734 * Description: This function removes any pending reservation reclaim requests 25735 * for the specified device. 25736 * 25737 * Arguments: dev - the device 'dev_t' 25738 */ 25739 25740 static void 25741 sd_rmv_resv_reclaim_req(dev_t dev) 25742 { 25743 struct sd_thr_request *sd_mhreq; 25744 struct sd_thr_request *sd_prev; 25745 25746 /* Remove a reservation reclaim request from the list */ 25747 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25748 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25749 /* 25750 * We are attempting to reinstate reservation for 25751 * this device. We wait for sd_reserve_release() 25752 * to return before we return. 25753 */ 25754 cv_wait(&sd_tr.srq_inprocess_cv, 25755 &sd_tr.srq_resv_reclaim_mutex); 25756 } else { 25757 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25758 if (sd_mhreq && sd_mhreq->dev == dev) { 25759 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25760 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25761 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25762 return; 25763 } 25764 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25765 if (sd_mhreq && sd_mhreq->dev == dev) { 25766 break; 25767 } 25768 sd_prev = sd_mhreq; 25769 } 25770 if (sd_mhreq != NULL) { 25771 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25772 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25773 } 25774 } 25775 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25776 } 25777 25778 25779 /* 25780 * Function: sd_mhd_reset_notify_cb() 25781 * 25782 * Description: This is a call back function for scsi_reset_notify. This 25783 * function updates the softstate reserved status and logs the 25784 * reset. The driver scsi watch facility callback function 25785 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25786 * will reclaim the reservation. 25787 * 25788 * Arguments: arg - driver soft state (unit) structure 25789 */ 25790 25791 static void 25792 sd_mhd_reset_notify_cb(caddr_t arg) 25793 { 25794 struct sd_lun *un = (struct sd_lun *)arg; 25795 25796 mutex_enter(SD_MUTEX(un)); 25797 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25798 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25799 SD_INFO(SD_LOG_IOCTL_MHD, un, 25800 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25801 } 25802 mutex_exit(SD_MUTEX(un)); 25803 } 25804 25805 25806 /* 25807 * Function: sd_take_ownership() 25808 * 25809 * Description: This routine implements an algorithm to achieve a stable 25810 * reservation on disks which don't implement priority reserve, 25811 * and makes sure that other host lose re-reservation attempts. 25812 * This algorithm contains of a loop that keeps issuing the RESERVE 25813 * for some period of time (min_ownership_delay, default 6 seconds) 25814 * During that loop, it looks to see if there has been a bus device 25815 * reset or bus reset (both of which cause an existing reservation 25816 * to be lost). If the reservation is lost issue RESERVE until a 25817 * period of min_ownership_delay with no resets has gone by, or 25818 * until max_ownership_delay has expired. This loop ensures that 25819 * the host really did manage to reserve the device, in spite of 25820 * resets. The looping for min_ownership_delay (default six 25821 * seconds) is important to early generation clustering products, 25822 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25823 * MHIOCENFAILFAST periodic timer of two seconds. By having 25824 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25825 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25826 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25827 * have already noticed, via the MHIOCENFAILFAST polling, that it 25828 * no longer "owns" the disk and will have panicked itself. Thus, 25829 * the host issuing the MHIOCTKOWN is assured (with timing 25830 * dependencies) that by the time it actually starts to use the 25831 * disk for real work, the old owner is no longer accessing it. 25832 * 25833 * min_ownership_delay is the minimum amount of time for which the 25834 * disk must be reserved continuously devoid of resets before the 25835 * MHIOCTKOWN ioctl will return success. 25836 * 25837 * max_ownership_delay indicates the amount of time by which the 25838 * take ownership should succeed or timeout with an error. 25839 * 25840 * Arguments: dev - the device 'dev_t' 25841 * *p - struct containing timing info. 25842 * 25843 * Return Code: 0 for success or error code 25844 */ 25845 25846 static int 25847 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25848 { 25849 struct sd_lun *un; 25850 int rval; 25851 int err; 25852 int reservation_count = 0; 25853 int min_ownership_delay = 6000000; /* in usec */ 25854 int max_ownership_delay = 30000000; /* in usec */ 25855 clock_t start_time; /* starting time of this algorithm */ 25856 clock_t end_time; /* time limit for giving up */ 25857 clock_t ownership_time; /* time limit for stable ownership */ 25858 clock_t current_time; 25859 clock_t previous_current_time; 25860 25861 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25862 return (ENXIO); 25863 } 25864 25865 /* 25866 * Attempt a device reservation. A priority reservation is requested. 25867 */ 25868 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25869 != SD_SUCCESS) { 25870 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25871 "sd_take_ownership: return(1)=%d\n", rval); 25872 return (rval); 25873 } 25874 25875 /* Update the softstate reserved status to indicate the reservation */ 25876 mutex_enter(SD_MUTEX(un)); 25877 un->un_resvd_status |= SD_RESERVE; 25878 un->un_resvd_status &= 25879 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25880 mutex_exit(SD_MUTEX(un)); 25881 25882 if (p != NULL) { 25883 if (p->min_ownership_delay != 0) { 25884 min_ownership_delay = p->min_ownership_delay * 1000; 25885 } 25886 if (p->max_ownership_delay != 0) { 25887 max_ownership_delay = p->max_ownership_delay * 1000; 25888 } 25889 } 25890 SD_INFO(SD_LOG_IOCTL_MHD, un, 25891 "sd_take_ownership: min, max delays: %d, %d\n", 25892 min_ownership_delay, max_ownership_delay); 25893 25894 start_time = ddi_get_lbolt(); 25895 current_time = start_time; 25896 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25897 end_time = start_time + drv_usectohz(max_ownership_delay); 25898 25899 while (current_time - end_time < 0) { 25900 delay(drv_usectohz(500000)); 25901 25902 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25903 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25904 mutex_enter(SD_MUTEX(un)); 25905 rval = (un->un_resvd_status & 25906 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25907 mutex_exit(SD_MUTEX(un)); 25908 break; 25909 } 25910 } 25911 previous_current_time = current_time; 25912 current_time = ddi_get_lbolt(); 25913 mutex_enter(SD_MUTEX(un)); 25914 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25915 ownership_time = ddi_get_lbolt() + 25916 drv_usectohz(min_ownership_delay); 25917 reservation_count = 0; 25918 } else { 25919 reservation_count++; 25920 } 25921 un->un_resvd_status |= SD_RESERVE; 25922 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25923 mutex_exit(SD_MUTEX(un)); 25924 25925 SD_INFO(SD_LOG_IOCTL_MHD, un, 25926 "sd_take_ownership: ticks for loop iteration=%ld, " 25927 "reservation=%s\n", (current_time - previous_current_time), 25928 reservation_count ? "ok" : "reclaimed"); 25929 25930 if (current_time - ownership_time >= 0 && 25931 reservation_count >= 4) { 25932 rval = 0; /* Achieved a stable ownership */ 25933 break; 25934 } 25935 if (current_time - end_time >= 0) { 25936 rval = EACCES; /* No ownership in max possible time */ 25937 break; 25938 } 25939 } 25940 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25941 "sd_take_ownership: return(2)=%d\n", rval); 25942 return (rval); 25943 } 25944 25945 25946 /* 25947 * Function: sd_reserve_release() 25948 * 25949 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25950 * PRIORITY RESERVE commands based on a user specified command type 25951 * 25952 * Arguments: dev - the device 'dev_t' 25953 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25954 * SD_RESERVE, SD_RELEASE 25955 * 25956 * Return Code: 0 or Error Code 25957 */ 25958 25959 static int 25960 sd_reserve_release(dev_t dev, int cmd) 25961 { 25962 struct uscsi_cmd *com = NULL; 25963 struct sd_lun *un = NULL; 25964 char cdb[CDB_GROUP0]; 25965 int rval; 25966 25967 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25968 (cmd == SD_PRIORITY_RESERVE)); 25969 25970 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25971 return (ENXIO); 25972 } 25973 25974 /* instantiate and initialize the command and cdb */ 25975 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25976 bzero(cdb, CDB_GROUP0); 25977 com->uscsi_flags = USCSI_SILENT; 25978 com->uscsi_timeout = un->un_reserve_release_time; 25979 com->uscsi_cdblen = CDB_GROUP0; 25980 com->uscsi_cdb = cdb; 25981 if (cmd == SD_RELEASE) { 25982 cdb[0] = SCMD_RELEASE; 25983 } else { 25984 cdb[0] = SCMD_RESERVE; 25985 } 25986 25987 /* Send the command. */ 25988 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25989 UIO_SYSSPACE, SD_PATH_STANDARD); 25990 25991 /* 25992 * "break" a reservation that is held by another host, by issuing a 25993 * reset if priority reserve is desired, and we could not get the 25994 * device. 25995 */ 25996 if ((cmd == SD_PRIORITY_RESERVE) && 25997 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25998 /* 25999 * First try to reset the LUN. If we cannot, then try a target 26000 * reset, followed by a bus reset if the target reset fails. 26001 */ 26002 int reset_retval = 0; 26003 if (un->un_f_lun_reset_enabled == TRUE) { 26004 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26005 } 26006 if (reset_retval == 0) { 26007 /* The LUN reset either failed or was not issued */ 26008 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26009 } 26010 if ((reset_retval == 0) && 26011 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26012 rval = EIO; 26013 kmem_free(com, sizeof (*com)); 26014 return (rval); 26015 } 26016 26017 bzero(com, sizeof (struct uscsi_cmd)); 26018 com->uscsi_flags = USCSI_SILENT; 26019 com->uscsi_cdb = cdb; 26020 com->uscsi_cdblen = CDB_GROUP0; 26021 com->uscsi_timeout = 5; 26022 26023 /* 26024 * Reissue the last reserve command, this time without request 26025 * sense. Assume that it is just a regular reserve command. 26026 */ 26027 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26028 UIO_SYSSPACE, SD_PATH_STANDARD); 26029 } 26030 26031 /* Return an error if still getting a reservation conflict. */ 26032 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26033 rval = EACCES; 26034 } 26035 26036 kmem_free(com, sizeof (*com)); 26037 return (rval); 26038 } 26039 26040 26041 #define SD_NDUMP_RETRIES 12 26042 /* 26043 * System Crash Dump routine 26044 */ 26045 26046 static int 26047 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26048 { 26049 int instance; 26050 int partition; 26051 int i; 26052 int err; 26053 struct sd_lun *un; 26054 struct dk_map *lp; 26055 struct scsi_pkt *wr_pktp; 26056 struct buf *wr_bp; 26057 struct buf wr_buf; 26058 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26059 daddr_t tgt_blkno; /* rmw - blkno for target */ 26060 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26061 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26062 size_t io_start_offset; 26063 int doing_rmw = FALSE; 26064 int rval; 26065 #if defined(__i386) || defined(__amd64) 26066 ssize_t dma_resid; 26067 daddr_t oblkno; 26068 #endif 26069 26070 instance = SDUNIT(dev); 26071 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26072 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26073 return (ENXIO); 26074 } 26075 26076 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26077 26078 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26079 26080 partition = SDPART(dev); 26081 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26082 26083 /* Validate blocks to dump at against partition size. */ 26084 lp = &un->un_map[partition]; 26085 if ((blkno + nblk) > lp->dkl_nblk) { 26086 SD_TRACE(SD_LOG_DUMP, un, 26087 "sddump: dump range larger than partition: " 26088 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26089 blkno, nblk, lp->dkl_nblk); 26090 return (EINVAL); 26091 } 26092 26093 mutex_enter(&un->un_pm_mutex); 26094 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26095 struct scsi_pkt *start_pktp; 26096 26097 mutex_exit(&un->un_pm_mutex); 26098 26099 /* 26100 * use pm framework to power on HBA 1st 26101 */ 26102 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26103 26104 /* 26105 * Dump no long uses sdpower to power on a device, it's 26106 * in-line here so it can be done in polled mode. 26107 */ 26108 26109 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26110 26111 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26112 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26113 26114 if (start_pktp == NULL) { 26115 /* We were not given a SCSI packet, fail. */ 26116 return (EIO); 26117 } 26118 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26119 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26120 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26121 start_pktp->pkt_flags = FLAG_NOINTR; 26122 26123 mutex_enter(SD_MUTEX(un)); 26124 SD_FILL_SCSI1_LUN(un, start_pktp); 26125 mutex_exit(SD_MUTEX(un)); 26126 /* 26127 * Scsi_poll returns 0 (success) if the command completes and 26128 * the status block is STATUS_GOOD. 26129 */ 26130 if (sd_scsi_poll(un, start_pktp) != 0) { 26131 scsi_destroy_pkt(start_pktp); 26132 return (EIO); 26133 } 26134 scsi_destroy_pkt(start_pktp); 26135 (void) sd_ddi_pm_resume(un); 26136 } else { 26137 mutex_exit(&un->un_pm_mutex); 26138 } 26139 26140 mutex_enter(SD_MUTEX(un)); 26141 un->un_throttle = 0; 26142 26143 /* 26144 * The first time through, reset the specific target device. 26145 * However, when cpr calls sddump we know that sd is in a 26146 * a good state so no bus reset is required. 26147 * Clear sense data via Request Sense cmd. 26148 * In sddump we don't care about allow_bus_device_reset anymore 26149 */ 26150 26151 if ((un->un_state != SD_STATE_SUSPENDED) && 26152 (un->un_state != SD_STATE_DUMPING)) { 26153 26154 New_state(un, SD_STATE_DUMPING); 26155 26156 if (un->un_f_is_fibre == FALSE) { 26157 mutex_exit(SD_MUTEX(un)); 26158 /* 26159 * Attempt a bus reset for parallel scsi. 26160 * 26161 * Note: A bus reset is required because on some host 26162 * systems (i.e. E420R) a bus device reset is 26163 * insufficient to reset the state of the target. 26164 * 26165 * Note: Don't issue the reset for fibre-channel, 26166 * because this tends to hang the bus (loop) for 26167 * too long while everyone is logging out and in 26168 * and the deadman timer for dumping will fire 26169 * before the dump is complete. 26170 */ 26171 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26172 mutex_enter(SD_MUTEX(un)); 26173 Restore_state(un); 26174 mutex_exit(SD_MUTEX(un)); 26175 return (EIO); 26176 } 26177 26178 /* Delay to give the device some recovery time. */ 26179 drv_usecwait(10000); 26180 26181 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26182 SD_INFO(SD_LOG_DUMP, un, 26183 "sddump: sd_send_polled_RQS failed\n"); 26184 } 26185 mutex_enter(SD_MUTEX(un)); 26186 } 26187 } 26188 26189 /* 26190 * Convert the partition-relative block number to a 26191 * disk physical block number. 26192 */ 26193 blkno += un->un_offset[partition]; 26194 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26195 26196 26197 /* 26198 * Check if the device has a non-512 block size. 26199 */ 26200 wr_bp = NULL; 26201 if (NOT_DEVBSIZE(un)) { 26202 tgt_byte_offset = blkno * un->un_sys_blocksize; 26203 tgt_byte_count = nblk * un->un_sys_blocksize; 26204 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26205 (tgt_byte_count % un->un_tgt_blocksize)) { 26206 doing_rmw = TRUE; 26207 /* 26208 * Calculate the block number and number of block 26209 * in terms of the media block size. 26210 */ 26211 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26212 tgt_nblk = 26213 ((tgt_byte_offset + tgt_byte_count + 26214 (un->un_tgt_blocksize - 1)) / 26215 un->un_tgt_blocksize) - tgt_blkno; 26216 26217 /* 26218 * Invoke the routine which is going to do read part 26219 * of read-modify-write. 26220 * Note that this routine returns a pointer to 26221 * a valid bp in wr_bp. 26222 */ 26223 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26224 &wr_bp); 26225 if (err) { 26226 mutex_exit(SD_MUTEX(un)); 26227 return (err); 26228 } 26229 /* 26230 * Offset is being calculated as - 26231 * (original block # * system block size) - 26232 * (new block # * target block size) 26233 */ 26234 io_start_offset = 26235 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26236 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26237 26238 ASSERT((io_start_offset >= 0) && 26239 (io_start_offset < un->un_tgt_blocksize)); 26240 /* 26241 * Do the modify portion of read modify write. 26242 */ 26243 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26244 (size_t)nblk * un->un_sys_blocksize); 26245 } else { 26246 doing_rmw = FALSE; 26247 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26248 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26249 } 26250 26251 /* Convert blkno and nblk to target blocks */ 26252 blkno = tgt_blkno; 26253 nblk = tgt_nblk; 26254 } else { 26255 wr_bp = &wr_buf; 26256 bzero(wr_bp, sizeof (struct buf)); 26257 wr_bp->b_flags = B_BUSY; 26258 wr_bp->b_un.b_addr = addr; 26259 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26260 wr_bp->b_resid = 0; 26261 } 26262 26263 mutex_exit(SD_MUTEX(un)); 26264 26265 /* 26266 * Obtain a SCSI packet for the write command. 26267 * It should be safe to call the allocator here without 26268 * worrying about being locked for DVMA mapping because 26269 * the address we're passed is already a DVMA mapping 26270 * 26271 * We are also not going to worry about semaphore ownership 26272 * in the dump buffer. Dumping is single threaded at present. 26273 */ 26274 26275 wr_pktp = NULL; 26276 26277 #if defined(__i386) || defined(__amd64) 26278 dma_resid = wr_bp->b_bcount; 26279 oblkno = blkno; 26280 while (dma_resid != 0) { 26281 #endif 26282 26283 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26284 wr_bp->b_flags &= ~B_ERROR; 26285 26286 #if defined(__i386) || defined(__amd64) 26287 blkno = oblkno + 26288 ((wr_bp->b_bcount - dma_resid) / 26289 un->un_tgt_blocksize); 26290 nblk = dma_resid / un->un_tgt_blocksize; 26291 26292 if (wr_pktp) { 26293 /* Partial DMA transfers after initial transfer */ 26294 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26295 blkno, nblk); 26296 } else { 26297 /* Initial transfer */ 26298 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26299 un->un_pkt_flags, NULL_FUNC, NULL, 26300 blkno, nblk); 26301 } 26302 #else 26303 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26304 0, NULL_FUNC, NULL, blkno, nblk); 26305 #endif 26306 26307 if (rval == 0) { 26308 /* We were given a SCSI packet, continue. */ 26309 break; 26310 } 26311 26312 if (i == 0) { 26313 if (wr_bp->b_flags & B_ERROR) { 26314 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26315 "no resources for dumping; " 26316 "error code: 0x%x, retrying", 26317 geterror(wr_bp)); 26318 } else { 26319 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26320 "no resources for dumping; retrying"); 26321 } 26322 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26323 if (wr_bp->b_flags & B_ERROR) { 26324 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26325 "no resources for dumping; error code: " 26326 "0x%x, retrying\n", geterror(wr_bp)); 26327 } 26328 } else { 26329 if (wr_bp->b_flags & B_ERROR) { 26330 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26331 "no resources for dumping; " 26332 "error code: 0x%x, retries failed, " 26333 "giving up.\n", geterror(wr_bp)); 26334 } else { 26335 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26336 "no resources for dumping; " 26337 "retries failed, giving up.\n"); 26338 } 26339 mutex_enter(SD_MUTEX(un)); 26340 Restore_state(un); 26341 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26342 mutex_exit(SD_MUTEX(un)); 26343 scsi_free_consistent_buf(wr_bp); 26344 } else { 26345 mutex_exit(SD_MUTEX(un)); 26346 } 26347 return (EIO); 26348 } 26349 drv_usecwait(10000); 26350 } 26351 26352 #if defined(__i386) || defined(__amd64) 26353 /* 26354 * save the resid from PARTIAL_DMA 26355 */ 26356 dma_resid = wr_pktp->pkt_resid; 26357 if (dma_resid != 0) 26358 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26359 wr_pktp->pkt_resid = 0; 26360 #endif 26361 26362 /* SunBug 1222170 */ 26363 wr_pktp->pkt_flags = FLAG_NOINTR; 26364 26365 err = EIO; 26366 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26367 26368 /* 26369 * Scsi_poll returns 0 (success) if the command completes and 26370 * the status block is STATUS_GOOD. We should only check 26371 * errors if this condition is not true. Even then we should 26372 * send our own request sense packet only if we have a check 26373 * condition and auto request sense has not been performed by 26374 * the hba. 26375 */ 26376 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26377 26378 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26379 (wr_pktp->pkt_resid == 0)) { 26380 err = SD_SUCCESS; 26381 break; 26382 } 26383 26384 /* 26385 * Check CMD_DEV_GONE 1st, give up if device is gone. 26386 */ 26387 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26388 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26389 "Device is gone\n"); 26390 break; 26391 } 26392 26393 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26394 SD_INFO(SD_LOG_DUMP, un, 26395 "sddump: write failed with CHECK, try # %d\n", i); 26396 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26397 (void) sd_send_polled_RQS(un); 26398 } 26399 26400 continue; 26401 } 26402 26403 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26404 int reset_retval = 0; 26405 26406 SD_INFO(SD_LOG_DUMP, un, 26407 "sddump: write failed with BUSY, try # %d\n", i); 26408 26409 if (un->un_f_lun_reset_enabled == TRUE) { 26410 reset_retval = scsi_reset(SD_ADDRESS(un), 26411 RESET_LUN); 26412 } 26413 if (reset_retval == 0) { 26414 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26415 } 26416 (void) sd_send_polled_RQS(un); 26417 26418 } else { 26419 SD_INFO(SD_LOG_DUMP, un, 26420 "sddump: write failed with 0x%x, try # %d\n", 26421 SD_GET_PKT_STATUS(wr_pktp), i); 26422 mutex_enter(SD_MUTEX(un)); 26423 sd_reset_target(un, wr_pktp); 26424 mutex_exit(SD_MUTEX(un)); 26425 } 26426 26427 /* 26428 * If we are not getting anywhere with lun/target resets, 26429 * let's reset the bus. 26430 */ 26431 if (i == SD_NDUMP_RETRIES/2) { 26432 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26433 (void) sd_send_polled_RQS(un); 26434 } 26435 26436 } 26437 #if defined(__i386) || defined(__amd64) 26438 } /* dma_resid */ 26439 #endif 26440 26441 scsi_destroy_pkt(wr_pktp); 26442 mutex_enter(SD_MUTEX(un)); 26443 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26444 mutex_exit(SD_MUTEX(un)); 26445 scsi_free_consistent_buf(wr_bp); 26446 } else { 26447 mutex_exit(SD_MUTEX(un)); 26448 } 26449 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26450 return (err); 26451 } 26452 26453 /* 26454 * Function: sd_scsi_poll() 26455 * 26456 * Description: This is a wrapper for the scsi_poll call. 26457 * 26458 * Arguments: sd_lun - The unit structure 26459 * scsi_pkt - The scsi packet being sent to the device. 26460 * 26461 * Return Code: 0 - Command completed successfully with good status 26462 * -1 - Command failed. This could indicate a check condition 26463 * or other status value requiring recovery action. 26464 * 26465 */ 26466 26467 static int 26468 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26469 { 26470 int status; 26471 26472 ASSERT(un != NULL); 26473 ASSERT(!mutex_owned(SD_MUTEX(un))); 26474 ASSERT(pktp != NULL); 26475 26476 status = SD_SUCCESS; 26477 26478 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26479 pktp->pkt_flags |= un->un_tagflags; 26480 pktp->pkt_flags &= ~FLAG_NODISCON; 26481 } 26482 26483 status = sd_ddi_scsi_poll(pktp); 26484 /* 26485 * Scsi_poll returns 0 (success) if the command completes and the 26486 * status block is STATUS_GOOD. We should only check errors if this 26487 * condition is not true. Even then we should send our own request 26488 * sense packet only if we have a check condition and auto 26489 * request sense has not been performed by the hba. 26490 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26491 */ 26492 if ((status != SD_SUCCESS) && 26493 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26494 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26495 (pktp->pkt_reason != CMD_DEV_GONE)) 26496 (void) sd_send_polled_RQS(un); 26497 26498 return (status); 26499 } 26500 26501 /* 26502 * Function: sd_send_polled_RQS() 26503 * 26504 * Description: This sends the request sense command to a device. 26505 * 26506 * Arguments: sd_lun - The unit structure 26507 * 26508 * Return Code: 0 - Command completed successfully with good status 26509 * -1 - Command failed. 26510 * 26511 */ 26512 26513 static int 26514 sd_send_polled_RQS(struct sd_lun *un) 26515 { 26516 int ret_val; 26517 struct scsi_pkt *rqs_pktp; 26518 struct buf *rqs_bp; 26519 26520 ASSERT(un != NULL); 26521 ASSERT(!mutex_owned(SD_MUTEX(un))); 26522 26523 ret_val = SD_SUCCESS; 26524 26525 rqs_pktp = un->un_rqs_pktp; 26526 rqs_bp = un->un_rqs_bp; 26527 26528 mutex_enter(SD_MUTEX(un)); 26529 26530 if (un->un_sense_isbusy) { 26531 ret_val = SD_FAILURE; 26532 mutex_exit(SD_MUTEX(un)); 26533 return (ret_val); 26534 } 26535 26536 /* 26537 * If the request sense buffer (and packet) is not in use, 26538 * let's set the un_sense_isbusy and send our packet 26539 */ 26540 un->un_sense_isbusy = 1; 26541 rqs_pktp->pkt_resid = 0; 26542 rqs_pktp->pkt_reason = 0; 26543 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26544 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26545 26546 mutex_exit(SD_MUTEX(un)); 26547 26548 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26549 " 0x%p\n", rqs_bp->b_un.b_addr); 26550 26551 /* 26552 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26553 * axle - it has a call into us! 26554 */ 26555 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26556 SD_INFO(SD_LOG_COMMON, un, 26557 "sd_send_polled_RQS: RQS failed\n"); 26558 } 26559 26560 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26561 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26562 26563 mutex_enter(SD_MUTEX(un)); 26564 un->un_sense_isbusy = 0; 26565 mutex_exit(SD_MUTEX(un)); 26566 26567 return (ret_val); 26568 } 26569 26570 /* 26571 * Defines needed for localized version of the scsi_poll routine. 26572 */ 26573 #define SD_CSEC 10000 /* usecs */ 26574 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26575 26576 26577 /* 26578 * Function: sd_ddi_scsi_poll() 26579 * 26580 * Description: Localized version of the scsi_poll routine. The purpose is to 26581 * send a scsi_pkt to a device as a polled command. This version 26582 * is to ensure more robust handling of transport errors. 26583 * Specifically this routine cures not ready, coming ready 26584 * transition for power up and reset of sonoma's. This can take 26585 * up to 45 seconds for power-on and 20 seconds for reset of a 26586 * sonoma lun. 26587 * 26588 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26589 * 26590 * Return Code: 0 - Command completed successfully with good status 26591 * -1 - Command failed. 26592 * 26593 */ 26594 26595 static int 26596 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26597 { 26598 int busy_count; 26599 int timeout; 26600 int rval = SD_FAILURE; 26601 int savef; 26602 struct scsi_extended_sense *sensep; 26603 long savet; 26604 void (*savec)(); 26605 /* 26606 * The following is defined in machdep.c and is used in determining if 26607 * the scsi transport system will do polled I/O instead of interrupt 26608 * I/O when called from xx_dump(). 26609 */ 26610 extern int do_polled_io; 26611 26612 /* 26613 * save old flags in pkt, to restore at end 26614 */ 26615 savef = pkt->pkt_flags; 26616 savec = pkt->pkt_comp; 26617 savet = pkt->pkt_time; 26618 26619 pkt->pkt_flags |= FLAG_NOINTR; 26620 26621 /* 26622 * XXX there is nothing in the SCSA spec that states that we should not 26623 * do a callback for polled cmds; however, removing this will break sd 26624 * and probably other target drivers 26625 */ 26626 pkt->pkt_comp = NULL; 26627 26628 /* 26629 * we don't like a polled command without timeout. 26630 * 60 seconds seems long enough. 26631 */ 26632 if (pkt->pkt_time == 0) { 26633 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26634 } 26635 26636 /* 26637 * Send polled cmd. 26638 * 26639 * We do some error recovery for various errors. Tran_busy, 26640 * queue full, and non-dispatched commands are retried every 10 msec. 26641 * as they are typically transient failures. Busy status and Not 26642 * Ready are retried every second as this status takes a while to 26643 * change. Unit attention is retried for pkt_time (60) times 26644 * with no delay. 26645 */ 26646 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26647 26648 for (busy_count = 0; busy_count < timeout; busy_count++) { 26649 int rc; 26650 int poll_delay; 26651 26652 /* 26653 * Initialize pkt status variables. 26654 */ 26655 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26656 26657 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26658 if (rc != TRAN_BUSY) { 26659 /* Transport failed - give up. */ 26660 break; 26661 } else { 26662 /* Transport busy - try again. */ 26663 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26664 } 26665 } else { 26666 /* 26667 * Transport accepted - check pkt status. 26668 */ 26669 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26670 if (pkt->pkt_reason == CMD_CMPLT && 26671 rc == STATUS_CHECK && 26672 pkt->pkt_state & STATE_ARQ_DONE) { 26673 struct scsi_arq_status *arqstat = 26674 (struct scsi_arq_status *)(pkt->pkt_scbp); 26675 26676 sensep = &arqstat->sts_sensedata; 26677 } else { 26678 sensep = NULL; 26679 } 26680 26681 if ((pkt->pkt_reason == CMD_CMPLT) && 26682 (rc == STATUS_GOOD)) { 26683 /* No error - we're done */ 26684 rval = SD_SUCCESS; 26685 break; 26686 26687 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26688 /* Lost connection - give up */ 26689 break; 26690 26691 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26692 (pkt->pkt_state == 0)) { 26693 /* Pkt not dispatched - try again. */ 26694 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26695 26696 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26697 (rc == STATUS_QFULL)) { 26698 /* Queue full - try again. */ 26699 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26700 26701 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26702 (rc == STATUS_BUSY)) { 26703 /* Busy - try again. */ 26704 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26705 busy_count += (SD_SEC_TO_CSEC - 1); 26706 26707 } else if ((sensep != NULL) && 26708 (sensep->es_key == KEY_UNIT_ATTENTION)) { 26709 /* Unit Attention - try again */ 26710 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26711 continue; 26712 26713 } else if ((sensep != NULL) && 26714 (sensep->es_key == KEY_NOT_READY) && 26715 (sensep->es_add_code == 0x04) && 26716 (sensep->es_qual_code == 0x01)) { 26717 /* Not ready -> ready - try again. */ 26718 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26719 busy_count += (SD_SEC_TO_CSEC - 1); 26720 26721 } else { 26722 /* BAD status - give up. */ 26723 break; 26724 } 26725 } 26726 26727 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26728 !do_polled_io) { 26729 delay(drv_usectohz(poll_delay)); 26730 } else { 26731 /* we busy wait during cpr_dump or interrupt threads */ 26732 drv_usecwait(poll_delay); 26733 } 26734 } 26735 26736 pkt->pkt_flags = savef; 26737 pkt->pkt_comp = savec; 26738 pkt->pkt_time = savet; 26739 return (rval); 26740 } 26741 26742 26743 /* 26744 * Function: sd_persistent_reservation_in_read_keys 26745 * 26746 * Description: This routine is the driver entry point for handling CD-ROM 26747 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26748 * by sending the SCSI-3 PRIN commands to the device. 26749 * Processes the read keys command response by copying the 26750 * reservation key information into the user provided buffer. 26751 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26752 * 26753 * Arguments: un - Pointer to soft state struct for the target. 26754 * usrp - user provided pointer to multihost Persistent In Read 26755 * Keys structure (mhioc_inkeys_t) 26756 * flag - this argument is a pass through to ddi_copyxxx() 26757 * directly from the mode argument of ioctl(). 26758 * 26759 * Return Code: 0 - Success 26760 * EACCES 26761 * ENOTSUP 26762 * errno return code from sd_send_scsi_cmd() 26763 * 26764 * Context: Can sleep. Does not return until command is completed. 26765 */ 26766 26767 static int 26768 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26769 mhioc_inkeys_t *usrp, int flag) 26770 { 26771 #ifdef _MULTI_DATAMODEL 26772 struct mhioc_key_list32 li32; 26773 #endif 26774 sd_prin_readkeys_t *in; 26775 mhioc_inkeys_t *ptr; 26776 mhioc_key_list_t li; 26777 uchar_t *data_bufp; 26778 int data_len; 26779 int rval; 26780 size_t copysz; 26781 26782 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26783 return (EINVAL); 26784 } 26785 bzero(&li, sizeof (mhioc_key_list_t)); 26786 26787 /* 26788 * Get the listsize from user 26789 */ 26790 #ifdef _MULTI_DATAMODEL 26791 26792 switch (ddi_model_convert_from(flag & FMODELS)) { 26793 case DDI_MODEL_ILP32: 26794 copysz = sizeof (struct mhioc_key_list32); 26795 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26796 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26797 "sd_persistent_reservation_in_read_keys: " 26798 "failed ddi_copyin: mhioc_key_list32_t\n"); 26799 rval = EFAULT; 26800 goto done; 26801 } 26802 li.listsize = li32.listsize; 26803 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26804 break; 26805 26806 case DDI_MODEL_NONE: 26807 copysz = sizeof (mhioc_key_list_t); 26808 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26809 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26810 "sd_persistent_reservation_in_read_keys: " 26811 "failed ddi_copyin: mhioc_key_list_t\n"); 26812 rval = EFAULT; 26813 goto done; 26814 } 26815 break; 26816 } 26817 26818 #else /* ! _MULTI_DATAMODEL */ 26819 copysz = sizeof (mhioc_key_list_t); 26820 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26821 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26822 "sd_persistent_reservation_in_read_keys: " 26823 "failed ddi_copyin: mhioc_key_list_t\n"); 26824 rval = EFAULT; 26825 goto done; 26826 } 26827 #endif 26828 26829 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26830 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26831 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26832 26833 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26834 data_len, data_bufp)) != 0) { 26835 goto done; 26836 } 26837 in = (sd_prin_readkeys_t *)data_bufp; 26838 ptr->generation = BE_32(in->generation); 26839 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26840 26841 /* 26842 * Return the min(listsize, listlen) keys 26843 */ 26844 #ifdef _MULTI_DATAMODEL 26845 26846 switch (ddi_model_convert_from(flag & FMODELS)) { 26847 case DDI_MODEL_ILP32: 26848 li32.listlen = li.listlen; 26849 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26850 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26851 "sd_persistent_reservation_in_read_keys: " 26852 "failed ddi_copyout: mhioc_key_list32_t\n"); 26853 rval = EFAULT; 26854 goto done; 26855 } 26856 break; 26857 26858 case DDI_MODEL_NONE: 26859 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26860 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26861 "sd_persistent_reservation_in_read_keys: " 26862 "failed ddi_copyout: mhioc_key_list_t\n"); 26863 rval = EFAULT; 26864 goto done; 26865 } 26866 break; 26867 } 26868 26869 #else /* ! _MULTI_DATAMODEL */ 26870 26871 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26872 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26873 "sd_persistent_reservation_in_read_keys: " 26874 "failed ddi_copyout: mhioc_key_list_t\n"); 26875 rval = EFAULT; 26876 goto done; 26877 } 26878 26879 #endif /* _MULTI_DATAMODEL */ 26880 26881 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26882 li.listsize * MHIOC_RESV_KEY_SIZE); 26883 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26884 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26885 "sd_persistent_reservation_in_read_keys: " 26886 "failed ddi_copyout: keylist\n"); 26887 rval = EFAULT; 26888 } 26889 done: 26890 kmem_free(data_bufp, data_len); 26891 return (rval); 26892 } 26893 26894 26895 /* 26896 * Function: sd_persistent_reservation_in_read_resv 26897 * 26898 * Description: This routine is the driver entry point for handling CD-ROM 26899 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26900 * by sending the SCSI-3 PRIN commands to the device. 26901 * Process the read persistent reservations command response by 26902 * copying the reservation information into the user provided 26903 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26904 * 26905 * Arguments: un - Pointer to soft state struct for the target. 26906 * usrp - user provided pointer to multihost Persistent In Read 26907 * Keys structure (mhioc_inkeys_t) 26908 * flag - this argument is a pass through to ddi_copyxxx() 26909 * directly from the mode argument of ioctl(). 26910 * 26911 * Return Code: 0 - Success 26912 * EACCES 26913 * ENOTSUP 26914 * errno return code from sd_send_scsi_cmd() 26915 * 26916 * Context: Can sleep. Does not return until command is completed. 26917 */ 26918 26919 static int 26920 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26921 mhioc_inresvs_t *usrp, int flag) 26922 { 26923 #ifdef _MULTI_DATAMODEL 26924 struct mhioc_resv_desc_list32 resvlist32; 26925 #endif 26926 sd_prin_readresv_t *in; 26927 mhioc_inresvs_t *ptr; 26928 sd_readresv_desc_t *readresv_ptr; 26929 mhioc_resv_desc_list_t resvlist; 26930 mhioc_resv_desc_t resvdesc; 26931 uchar_t *data_bufp; 26932 int data_len; 26933 int rval; 26934 int i; 26935 size_t copysz; 26936 mhioc_resv_desc_t *bufp; 26937 26938 if ((ptr = usrp) == NULL) { 26939 return (EINVAL); 26940 } 26941 26942 /* 26943 * Get the listsize from user 26944 */ 26945 #ifdef _MULTI_DATAMODEL 26946 switch (ddi_model_convert_from(flag & FMODELS)) { 26947 case DDI_MODEL_ILP32: 26948 copysz = sizeof (struct mhioc_resv_desc_list32); 26949 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26950 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26951 "sd_persistent_reservation_in_read_resv: " 26952 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26953 rval = EFAULT; 26954 goto done; 26955 } 26956 resvlist.listsize = resvlist32.listsize; 26957 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26958 break; 26959 26960 case DDI_MODEL_NONE: 26961 copysz = sizeof (mhioc_resv_desc_list_t); 26962 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26963 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26964 "sd_persistent_reservation_in_read_resv: " 26965 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26966 rval = EFAULT; 26967 goto done; 26968 } 26969 break; 26970 } 26971 #else /* ! _MULTI_DATAMODEL */ 26972 copysz = sizeof (mhioc_resv_desc_list_t); 26973 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26974 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26975 "sd_persistent_reservation_in_read_resv: " 26976 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26977 rval = EFAULT; 26978 goto done; 26979 } 26980 #endif /* ! _MULTI_DATAMODEL */ 26981 26982 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26983 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26984 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26985 26986 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 26987 data_len, data_bufp)) != 0) { 26988 goto done; 26989 } 26990 in = (sd_prin_readresv_t *)data_bufp; 26991 ptr->generation = BE_32(in->generation); 26992 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26993 26994 /* 26995 * Return the min(listsize, listlen( keys 26996 */ 26997 #ifdef _MULTI_DATAMODEL 26998 26999 switch (ddi_model_convert_from(flag & FMODELS)) { 27000 case DDI_MODEL_ILP32: 27001 resvlist32.listlen = resvlist.listlen; 27002 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27003 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27004 "sd_persistent_reservation_in_read_resv: " 27005 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27006 rval = EFAULT; 27007 goto done; 27008 } 27009 break; 27010 27011 case DDI_MODEL_NONE: 27012 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27013 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27014 "sd_persistent_reservation_in_read_resv: " 27015 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27016 rval = EFAULT; 27017 goto done; 27018 } 27019 break; 27020 } 27021 27022 #else /* ! _MULTI_DATAMODEL */ 27023 27024 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27025 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27026 "sd_persistent_reservation_in_read_resv: " 27027 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27028 rval = EFAULT; 27029 goto done; 27030 } 27031 27032 #endif /* ! _MULTI_DATAMODEL */ 27033 27034 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27035 bufp = resvlist.list; 27036 copysz = sizeof (mhioc_resv_desc_t); 27037 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27038 i++, readresv_ptr++, bufp++) { 27039 27040 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27041 MHIOC_RESV_KEY_SIZE); 27042 resvdesc.type = readresv_ptr->type; 27043 resvdesc.scope = readresv_ptr->scope; 27044 resvdesc.scope_specific_addr = 27045 BE_32(readresv_ptr->scope_specific_addr); 27046 27047 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27048 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27049 "sd_persistent_reservation_in_read_resv: " 27050 "failed ddi_copyout: resvlist\n"); 27051 rval = EFAULT; 27052 goto done; 27053 } 27054 } 27055 done: 27056 kmem_free(data_bufp, data_len); 27057 return (rval); 27058 } 27059 27060 27061 /* 27062 * Function: sr_change_blkmode() 27063 * 27064 * Description: This routine is the driver entry point for handling CD-ROM 27065 * block mode ioctl requests. Support for returning and changing 27066 * the current block size in use by the device is implemented. The 27067 * LBA size is changed via a MODE SELECT Block Descriptor. 27068 * 27069 * This routine issues a mode sense with an allocation length of 27070 * 12 bytes for the mode page header and a single block descriptor. 27071 * 27072 * Arguments: dev - the device 'dev_t' 27073 * cmd - the request type; one of CDROMGBLKMODE (get) or 27074 * CDROMSBLKMODE (set) 27075 * data - current block size or requested block size 27076 * flag - this argument is a pass through to ddi_copyxxx() directly 27077 * from the mode argument of ioctl(). 27078 * 27079 * Return Code: the code returned by sd_send_scsi_cmd() 27080 * EINVAL if invalid arguments are provided 27081 * EFAULT if ddi_copyxxx() fails 27082 * ENXIO if fail ddi_get_soft_state 27083 * EIO if invalid mode sense block descriptor length 27084 * 27085 */ 27086 27087 static int 27088 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27089 { 27090 struct sd_lun *un = NULL; 27091 struct mode_header *sense_mhp, *select_mhp; 27092 struct block_descriptor *sense_desc, *select_desc; 27093 int current_bsize; 27094 int rval = EINVAL; 27095 uchar_t *sense = NULL; 27096 uchar_t *select = NULL; 27097 27098 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27099 27100 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27101 return (ENXIO); 27102 } 27103 27104 /* 27105 * The block length is changed via the Mode Select block descriptor, the 27106 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27107 * required as part of this routine. Therefore the mode sense allocation 27108 * length is specified to be the length of a mode page header and a 27109 * block descriptor. 27110 */ 27111 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27112 27113 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27114 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27115 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27116 "sr_change_blkmode: Mode Sense Failed\n"); 27117 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27118 return (rval); 27119 } 27120 27121 /* Check the block descriptor len to handle only 1 block descriptor */ 27122 sense_mhp = (struct mode_header *)sense; 27123 if ((sense_mhp->bdesc_length == 0) || 27124 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27125 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27126 "sr_change_blkmode: Mode Sense returned invalid block" 27127 " descriptor length\n"); 27128 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27129 return (EIO); 27130 } 27131 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27132 current_bsize = ((sense_desc->blksize_hi << 16) | 27133 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27134 27135 /* Process command */ 27136 switch (cmd) { 27137 case CDROMGBLKMODE: 27138 /* Return the block size obtained during the mode sense */ 27139 if (ddi_copyout(¤t_bsize, (void *)data, 27140 sizeof (int), flag) != 0) 27141 rval = EFAULT; 27142 break; 27143 case CDROMSBLKMODE: 27144 /* Validate the requested block size */ 27145 switch (data) { 27146 case CDROM_BLK_512: 27147 case CDROM_BLK_1024: 27148 case CDROM_BLK_2048: 27149 case CDROM_BLK_2056: 27150 case CDROM_BLK_2336: 27151 case CDROM_BLK_2340: 27152 case CDROM_BLK_2352: 27153 case CDROM_BLK_2368: 27154 case CDROM_BLK_2448: 27155 case CDROM_BLK_2646: 27156 case CDROM_BLK_2647: 27157 break; 27158 default: 27159 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27160 "sr_change_blkmode: " 27161 "Block Size '%ld' Not Supported\n", data); 27162 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27163 return (EINVAL); 27164 } 27165 27166 /* 27167 * The current block size matches the requested block size so 27168 * there is no need to send the mode select to change the size 27169 */ 27170 if (current_bsize == data) { 27171 break; 27172 } 27173 27174 /* Build the select data for the requested block size */ 27175 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27176 select_mhp = (struct mode_header *)select; 27177 select_desc = 27178 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27179 /* 27180 * The LBA size is changed via the block descriptor, so the 27181 * descriptor is built according to the user data 27182 */ 27183 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27184 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27185 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27186 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27187 27188 /* Send the mode select for the requested block size */ 27189 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27190 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27191 SD_PATH_STANDARD)) != 0) { 27192 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27193 "sr_change_blkmode: Mode Select Failed\n"); 27194 /* 27195 * The mode select failed for the requested block size, 27196 * so reset the data for the original block size and 27197 * send it to the target. The error is indicated by the 27198 * return value for the failed mode select. 27199 */ 27200 select_desc->blksize_hi = sense_desc->blksize_hi; 27201 select_desc->blksize_mid = sense_desc->blksize_mid; 27202 select_desc->blksize_lo = sense_desc->blksize_lo; 27203 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27204 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27205 SD_PATH_STANDARD); 27206 } else { 27207 ASSERT(!mutex_owned(SD_MUTEX(un))); 27208 mutex_enter(SD_MUTEX(un)); 27209 sd_update_block_info(un, (uint32_t)data, 0); 27210 27211 mutex_exit(SD_MUTEX(un)); 27212 } 27213 break; 27214 default: 27215 /* should not reach here, but check anyway */ 27216 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27217 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27218 rval = EINVAL; 27219 break; 27220 } 27221 27222 if (select) { 27223 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27224 } 27225 if (sense) { 27226 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27227 } 27228 return (rval); 27229 } 27230 27231 27232 /* 27233 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27234 * implement driver support for getting and setting the CD speed. The command 27235 * set used will be based on the device type. If the device has not been 27236 * identified as MMC the Toshiba vendor specific mode page will be used. If 27237 * the device is MMC but does not support the Real Time Streaming feature 27238 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27239 * be used to read the speed. 27240 */ 27241 27242 /* 27243 * Function: sr_change_speed() 27244 * 27245 * Description: This routine is the driver entry point for handling CD-ROM 27246 * drive speed ioctl requests for devices supporting the Toshiba 27247 * vendor specific drive speed mode page. Support for returning 27248 * and changing the current drive speed in use by the device is 27249 * implemented. 27250 * 27251 * Arguments: dev - the device 'dev_t' 27252 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27253 * CDROMSDRVSPEED (set) 27254 * data - current drive speed or requested drive speed 27255 * flag - this argument is a pass through to ddi_copyxxx() directly 27256 * from the mode argument of ioctl(). 27257 * 27258 * Return Code: the code returned by sd_send_scsi_cmd() 27259 * EINVAL if invalid arguments are provided 27260 * EFAULT if ddi_copyxxx() fails 27261 * ENXIO if fail ddi_get_soft_state 27262 * EIO if invalid mode sense block descriptor length 27263 */ 27264 27265 static int 27266 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27267 { 27268 struct sd_lun *un = NULL; 27269 struct mode_header *sense_mhp, *select_mhp; 27270 struct mode_speed *sense_page, *select_page; 27271 int current_speed; 27272 int rval = EINVAL; 27273 int bd_len; 27274 uchar_t *sense = NULL; 27275 uchar_t *select = NULL; 27276 27277 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27278 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27279 return (ENXIO); 27280 } 27281 27282 /* 27283 * Note: The drive speed is being modified here according to a Toshiba 27284 * vendor specific mode page (0x31). 27285 */ 27286 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27287 27288 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27289 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27290 SD_PATH_STANDARD)) != 0) { 27291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27292 "sr_change_speed: Mode Sense Failed\n"); 27293 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27294 return (rval); 27295 } 27296 sense_mhp = (struct mode_header *)sense; 27297 27298 /* Check the block descriptor len to handle only 1 block descriptor */ 27299 bd_len = sense_mhp->bdesc_length; 27300 if (bd_len > MODE_BLK_DESC_LENGTH) { 27301 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27302 "sr_change_speed: Mode Sense returned invalid block " 27303 "descriptor length\n"); 27304 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27305 return (EIO); 27306 } 27307 27308 sense_page = (struct mode_speed *) 27309 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27310 current_speed = sense_page->speed; 27311 27312 /* Process command */ 27313 switch (cmd) { 27314 case CDROMGDRVSPEED: 27315 /* Return the drive speed obtained during the mode sense */ 27316 if (current_speed == 0x2) { 27317 current_speed = CDROM_TWELVE_SPEED; 27318 } 27319 if (ddi_copyout(¤t_speed, (void *)data, 27320 sizeof (int), flag) != 0) { 27321 rval = EFAULT; 27322 } 27323 break; 27324 case CDROMSDRVSPEED: 27325 /* Validate the requested drive speed */ 27326 switch ((uchar_t)data) { 27327 case CDROM_TWELVE_SPEED: 27328 data = 0x2; 27329 /*FALLTHROUGH*/ 27330 case CDROM_NORMAL_SPEED: 27331 case CDROM_DOUBLE_SPEED: 27332 case CDROM_QUAD_SPEED: 27333 case CDROM_MAXIMUM_SPEED: 27334 break; 27335 default: 27336 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27337 "sr_change_speed: " 27338 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27339 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27340 return (EINVAL); 27341 } 27342 27343 /* 27344 * The current drive speed matches the requested drive speed so 27345 * there is no need to send the mode select to change the speed 27346 */ 27347 if (current_speed == data) { 27348 break; 27349 } 27350 27351 /* Build the select data for the requested drive speed */ 27352 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27353 select_mhp = (struct mode_header *)select; 27354 select_mhp->bdesc_length = 0; 27355 select_page = 27356 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27357 select_page = 27358 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27359 select_page->mode_page.code = CDROM_MODE_SPEED; 27360 select_page->mode_page.length = 2; 27361 select_page->speed = (uchar_t)data; 27362 27363 /* Send the mode select for the requested block size */ 27364 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27365 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27366 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27367 /* 27368 * The mode select failed for the requested drive speed, 27369 * so reset the data for the original drive speed and 27370 * send it to the target. The error is indicated by the 27371 * return value for the failed mode select. 27372 */ 27373 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27374 "sr_drive_speed: Mode Select Failed\n"); 27375 select_page->speed = sense_page->speed; 27376 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27377 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27378 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27379 } 27380 break; 27381 default: 27382 /* should not reach here, but check anyway */ 27383 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27384 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27385 rval = EINVAL; 27386 break; 27387 } 27388 27389 if (select) { 27390 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27391 } 27392 if (sense) { 27393 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27394 } 27395 27396 return (rval); 27397 } 27398 27399 27400 /* 27401 * Function: sr_atapi_change_speed() 27402 * 27403 * Description: This routine is the driver entry point for handling CD-ROM 27404 * drive speed ioctl requests for MMC devices that do not support 27405 * the Real Time Streaming feature (0x107). 27406 * 27407 * Note: This routine will use the SET SPEED command which may not 27408 * be supported by all devices. 27409 * 27410 * Arguments: dev- the device 'dev_t' 27411 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27412 * CDROMSDRVSPEED (set) 27413 * data- current drive speed or requested drive speed 27414 * flag- this argument is a pass through to ddi_copyxxx() directly 27415 * from the mode argument of ioctl(). 27416 * 27417 * Return Code: the code returned by sd_send_scsi_cmd() 27418 * EINVAL if invalid arguments are provided 27419 * EFAULT if ddi_copyxxx() fails 27420 * ENXIO if fail ddi_get_soft_state 27421 * EIO if invalid mode sense block descriptor length 27422 */ 27423 27424 static int 27425 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27426 { 27427 struct sd_lun *un; 27428 struct uscsi_cmd *com = NULL; 27429 struct mode_header_grp2 *sense_mhp; 27430 uchar_t *sense_page; 27431 uchar_t *sense = NULL; 27432 char cdb[CDB_GROUP5]; 27433 int bd_len; 27434 int current_speed = 0; 27435 int max_speed = 0; 27436 int rval; 27437 27438 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27439 27440 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27441 return (ENXIO); 27442 } 27443 27444 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27445 27446 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27447 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27448 SD_PATH_STANDARD)) != 0) { 27449 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27450 "sr_atapi_change_speed: Mode Sense Failed\n"); 27451 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27452 return (rval); 27453 } 27454 27455 /* Check the block descriptor len to handle only 1 block descriptor */ 27456 sense_mhp = (struct mode_header_grp2 *)sense; 27457 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27458 if (bd_len > MODE_BLK_DESC_LENGTH) { 27459 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27460 "sr_atapi_change_speed: Mode Sense returned invalid " 27461 "block descriptor length\n"); 27462 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27463 return (EIO); 27464 } 27465 27466 /* Calculate the current and maximum drive speeds */ 27467 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27468 current_speed = (sense_page[14] << 8) | sense_page[15]; 27469 max_speed = (sense_page[8] << 8) | sense_page[9]; 27470 27471 /* Process the command */ 27472 switch (cmd) { 27473 case CDROMGDRVSPEED: 27474 current_speed /= SD_SPEED_1X; 27475 if (ddi_copyout(¤t_speed, (void *)data, 27476 sizeof (int), flag) != 0) 27477 rval = EFAULT; 27478 break; 27479 case CDROMSDRVSPEED: 27480 /* Convert the speed code to KB/sec */ 27481 switch ((uchar_t)data) { 27482 case CDROM_NORMAL_SPEED: 27483 current_speed = SD_SPEED_1X; 27484 break; 27485 case CDROM_DOUBLE_SPEED: 27486 current_speed = 2 * SD_SPEED_1X; 27487 break; 27488 case CDROM_QUAD_SPEED: 27489 current_speed = 4 * SD_SPEED_1X; 27490 break; 27491 case CDROM_TWELVE_SPEED: 27492 current_speed = 12 * SD_SPEED_1X; 27493 break; 27494 case CDROM_MAXIMUM_SPEED: 27495 current_speed = 0xffff; 27496 break; 27497 default: 27498 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27499 "sr_atapi_change_speed: invalid drive speed %d\n", 27500 (uchar_t)data); 27501 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27502 return (EINVAL); 27503 } 27504 27505 /* Check the request against the drive's max speed. */ 27506 if (current_speed != 0xffff) { 27507 if (current_speed > max_speed) { 27508 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27509 return (EINVAL); 27510 } 27511 } 27512 27513 /* 27514 * Build and send the SET SPEED command 27515 * 27516 * Note: The SET SPEED (0xBB) command used in this routine is 27517 * obsolete per the SCSI MMC spec but still supported in the 27518 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27519 * therefore the command is still implemented in this routine. 27520 */ 27521 bzero(cdb, sizeof (cdb)); 27522 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27523 cdb[2] = (uchar_t)(current_speed >> 8); 27524 cdb[3] = (uchar_t)current_speed; 27525 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27526 com->uscsi_cdb = (caddr_t)cdb; 27527 com->uscsi_cdblen = CDB_GROUP5; 27528 com->uscsi_bufaddr = NULL; 27529 com->uscsi_buflen = 0; 27530 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27531 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 27532 UIO_SYSSPACE, SD_PATH_STANDARD); 27533 break; 27534 default: 27535 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27536 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27537 rval = EINVAL; 27538 } 27539 27540 if (sense) { 27541 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27542 } 27543 if (com) { 27544 kmem_free(com, sizeof (*com)); 27545 } 27546 return (rval); 27547 } 27548 27549 27550 /* 27551 * Function: sr_pause_resume() 27552 * 27553 * Description: This routine is the driver entry point for handling CD-ROM 27554 * pause/resume ioctl requests. This only affects the audio play 27555 * operation. 27556 * 27557 * Arguments: dev - the device 'dev_t' 27558 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27559 * for setting the resume bit of the cdb. 27560 * 27561 * Return Code: the code returned by sd_send_scsi_cmd() 27562 * EINVAL if invalid mode specified 27563 * 27564 */ 27565 27566 static int 27567 sr_pause_resume(dev_t dev, int cmd) 27568 { 27569 struct sd_lun *un; 27570 struct uscsi_cmd *com; 27571 char cdb[CDB_GROUP1]; 27572 int rval; 27573 27574 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27575 return (ENXIO); 27576 } 27577 27578 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27579 bzero(cdb, CDB_GROUP1); 27580 cdb[0] = SCMD_PAUSE_RESUME; 27581 switch (cmd) { 27582 case CDROMRESUME: 27583 cdb[8] = 1; 27584 break; 27585 case CDROMPAUSE: 27586 cdb[8] = 0; 27587 break; 27588 default: 27589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27590 " Command '%x' Not Supported\n", cmd); 27591 rval = EINVAL; 27592 goto done; 27593 } 27594 27595 com->uscsi_cdb = cdb; 27596 com->uscsi_cdblen = CDB_GROUP1; 27597 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27598 27599 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27600 UIO_SYSSPACE, SD_PATH_STANDARD); 27601 27602 done: 27603 kmem_free(com, sizeof (*com)); 27604 return (rval); 27605 } 27606 27607 27608 /* 27609 * Function: sr_play_msf() 27610 * 27611 * Description: This routine is the driver entry point for handling CD-ROM 27612 * ioctl requests to output the audio signals at the specified 27613 * starting address and continue the audio play until the specified 27614 * ending address (CDROMPLAYMSF) The address is in Minute Second 27615 * Frame (MSF) format. 27616 * 27617 * Arguments: dev - the device 'dev_t' 27618 * data - pointer to user provided audio msf structure, 27619 * specifying start/end addresses. 27620 * flag - this argument is a pass through to ddi_copyxxx() 27621 * directly from the mode argument of ioctl(). 27622 * 27623 * Return Code: the code returned by sd_send_scsi_cmd() 27624 * EFAULT if ddi_copyxxx() fails 27625 * ENXIO if fail ddi_get_soft_state 27626 * EINVAL if data pointer is NULL 27627 */ 27628 27629 static int 27630 sr_play_msf(dev_t dev, caddr_t data, int flag) 27631 { 27632 struct sd_lun *un; 27633 struct uscsi_cmd *com; 27634 struct cdrom_msf msf_struct; 27635 struct cdrom_msf *msf = &msf_struct; 27636 char cdb[CDB_GROUP1]; 27637 int rval; 27638 27639 if (data == NULL) { 27640 return (EINVAL); 27641 } 27642 27643 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27644 return (ENXIO); 27645 } 27646 27647 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27648 return (EFAULT); 27649 } 27650 27651 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27652 bzero(cdb, CDB_GROUP1); 27653 cdb[0] = SCMD_PLAYAUDIO_MSF; 27654 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27655 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27656 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27657 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27658 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27659 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27660 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27661 } else { 27662 cdb[3] = msf->cdmsf_min0; 27663 cdb[4] = msf->cdmsf_sec0; 27664 cdb[5] = msf->cdmsf_frame0; 27665 cdb[6] = msf->cdmsf_min1; 27666 cdb[7] = msf->cdmsf_sec1; 27667 cdb[8] = msf->cdmsf_frame1; 27668 } 27669 com->uscsi_cdb = cdb; 27670 com->uscsi_cdblen = CDB_GROUP1; 27671 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27672 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27673 UIO_SYSSPACE, SD_PATH_STANDARD); 27674 kmem_free(com, sizeof (*com)); 27675 return (rval); 27676 } 27677 27678 27679 /* 27680 * Function: sr_play_trkind() 27681 * 27682 * Description: This routine is the driver entry point for handling CD-ROM 27683 * ioctl requests to output the audio signals at the specified 27684 * starting address and continue the audio play until the specified 27685 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27686 * format. 27687 * 27688 * Arguments: dev - the device 'dev_t' 27689 * data - pointer to user provided audio track/index structure, 27690 * specifying start/end addresses. 27691 * flag - this argument is a pass through to ddi_copyxxx() 27692 * directly from the mode argument of ioctl(). 27693 * 27694 * Return Code: the code returned by sd_send_scsi_cmd() 27695 * EFAULT if ddi_copyxxx() fails 27696 * ENXIO if fail ddi_get_soft_state 27697 * EINVAL if data pointer is NULL 27698 */ 27699 27700 static int 27701 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27702 { 27703 struct cdrom_ti ti_struct; 27704 struct cdrom_ti *ti = &ti_struct; 27705 struct uscsi_cmd *com = NULL; 27706 char cdb[CDB_GROUP1]; 27707 int rval; 27708 27709 if (data == NULL) { 27710 return (EINVAL); 27711 } 27712 27713 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27714 return (EFAULT); 27715 } 27716 27717 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27718 bzero(cdb, CDB_GROUP1); 27719 cdb[0] = SCMD_PLAYAUDIO_TI; 27720 cdb[4] = ti->cdti_trk0; 27721 cdb[5] = ti->cdti_ind0; 27722 cdb[7] = ti->cdti_trk1; 27723 cdb[8] = ti->cdti_ind1; 27724 com->uscsi_cdb = cdb; 27725 com->uscsi_cdblen = CDB_GROUP1; 27726 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27727 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27728 UIO_SYSSPACE, SD_PATH_STANDARD); 27729 kmem_free(com, sizeof (*com)); 27730 return (rval); 27731 } 27732 27733 27734 /* 27735 * Function: sr_read_all_subcodes() 27736 * 27737 * Description: This routine is the driver entry point for handling CD-ROM 27738 * ioctl requests to return raw subcode data while the target is 27739 * playing audio (CDROMSUBCODE). 27740 * 27741 * Arguments: dev - the device 'dev_t' 27742 * data - pointer to user provided cdrom subcode structure, 27743 * specifying the transfer length and address. 27744 * flag - this argument is a pass through to ddi_copyxxx() 27745 * directly from the mode argument of ioctl(). 27746 * 27747 * Return Code: the code returned by sd_send_scsi_cmd() 27748 * EFAULT if ddi_copyxxx() fails 27749 * ENXIO if fail ddi_get_soft_state 27750 * EINVAL if data pointer is NULL 27751 */ 27752 27753 static int 27754 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27755 { 27756 struct sd_lun *un = NULL; 27757 struct uscsi_cmd *com = NULL; 27758 struct cdrom_subcode *subcode = NULL; 27759 int rval; 27760 size_t buflen; 27761 char cdb[CDB_GROUP5]; 27762 27763 #ifdef _MULTI_DATAMODEL 27764 /* To support ILP32 applications in an LP64 world */ 27765 struct cdrom_subcode32 cdrom_subcode32; 27766 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27767 #endif 27768 if (data == NULL) { 27769 return (EINVAL); 27770 } 27771 27772 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27773 return (ENXIO); 27774 } 27775 27776 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27777 27778 #ifdef _MULTI_DATAMODEL 27779 switch (ddi_model_convert_from(flag & FMODELS)) { 27780 case DDI_MODEL_ILP32: 27781 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27783 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27784 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27785 return (EFAULT); 27786 } 27787 /* Convert the ILP32 uscsi data from the application to LP64 */ 27788 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27789 break; 27790 case DDI_MODEL_NONE: 27791 if (ddi_copyin(data, subcode, 27792 sizeof (struct cdrom_subcode), flag)) { 27793 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27794 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27795 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27796 return (EFAULT); 27797 } 27798 break; 27799 } 27800 #else /* ! _MULTI_DATAMODEL */ 27801 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27802 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27803 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27804 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27805 return (EFAULT); 27806 } 27807 #endif /* _MULTI_DATAMODEL */ 27808 27809 /* 27810 * Since MMC-2 expects max 3 bytes for length, check if the 27811 * length input is greater than 3 bytes 27812 */ 27813 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27814 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27815 "sr_read_all_subcodes: " 27816 "cdrom transfer length too large: %d (limit %d)\n", 27817 subcode->cdsc_length, 0xFFFFFF); 27818 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27819 return (EINVAL); 27820 } 27821 27822 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27823 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27824 bzero(cdb, CDB_GROUP5); 27825 27826 if (un->un_f_mmc_cap == TRUE) { 27827 cdb[0] = (char)SCMD_READ_CD; 27828 cdb[2] = (char)0xff; 27829 cdb[3] = (char)0xff; 27830 cdb[4] = (char)0xff; 27831 cdb[5] = (char)0xff; 27832 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27833 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27834 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27835 cdb[10] = 1; 27836 } else { 27837 /* 27838 * Note: A vendor specific command (0xDF) is being used her to 27839 * request a read of all subcodes. 27840 */ 27841 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27842 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27843 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27844 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27845 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27846 } 27847 com->uscsi_cdb = cdb; 27848 com->uscsi_cdblen = CDB_GROUP5; 27849 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27850 com->uscsi_buflen = buflen; 27851 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27852 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27853 UIO_SYSSPACE, SD_PATH_STANDARD); 27854 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27855 kmem_free(com, sizeof (*com)); 27856 return (rval); 27857 } 27858 27859 27860 /* 27861 * Function: sr_read_subchannel() 27862 * 27863 * Description: This routine is the driver entry point for handling CD-ROM 27864 * ioctl requests to return the Q sub-channel data of the CD 27865 * current position block. (CDROMSUBCHNL) The data includes the 27866 * track number, index number, absolute CD-ROM address (LBA or MSF 27867 * format per the user) , track relative CD-ROM address (LBA or MSF 27868 * format per the user), control data and audio status. 27869 * 27870 * Arguments: dev - the device 'dev_t' 27871 * data - pointer to user provided cdrom sub-channel structure 27872 * flag - this argument is a pass through to ddi_copyxxx() 27873 * directly from the mode argument of ioctl(). 27874 * 27875 * Return Code: the code returned by sd_send_scsi_cmd() 27876 * EFAULT if ddi_copyxxx() fails 27877 * ENXIO if fail ddi_get_soft_state 27878 * EINVAL if data pointer is NULL 27879 */ 27880 27881 static int 27882 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27883 { 27884 struct sd_lun *un; 27885 struct uscsi_cmd *com; 27886 struct cdrom_subchnl subchanel; 27887 struct cdrom_subchnl *subchnl = &subchanel; 27888 char cdb[CDB_GROUP1]; 27889 caddr_t buffer; 27890 int rval; 27891 27892 if (data == NULL) { 27893 return (EINVAL); 27894 } 27895 27896 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27897 (un->un_state == SD_STATE_OFFLINE)) { 27898 return (ENXIO); 27899 } 27900 27901 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27902 return (EFAULT); 27903 } 27904 27905 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27906 bzero(cdb, CDB_GROUP1); 27907 cdb[0] = SCMD_READ_SUBCHANNEL; 27908 /* Set the MSF bit based on the user requested address format */ 27909 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27910 /* 27911 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27912 * returned 27913 */ 27914 cdb[2] = 0x40; 27915 /* 27916 * Set byte 3 to specify the return data format. A value of 0x01 27917 * indicates that the CD-ROM current position should be returned. 27918 */ 27919 cdb[3] = 0x01; 27920 cdb[8] = 0x10; 27921 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27922 com->uscsi_cdb = cdb; 27923 com->uscsi_cdblen = CDB_GROUP1; 27924 com->uscsi_bufaddr = buffer; 27925 com->uscsi_buflen = 16; 27926 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27927 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27928 UIO_SYSSPACE, SD_PATH_STANDARD); 27929 if (rval != 0) { 27930 kmem_free(buffer, 16); 27931 kmem_free(com, sizeof (*com)); 27932 return (rval); 27933 } 27934 27935 /* Process the returned Q sub-channel data */ 27936 subchnl->cdsc_audiostatus = buffer[1]; 27937 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27938 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27939 subchnl->cdsc_trk = buffer[6]; 27940 subchnl->cdsc_ind = buffer[7]; 27941 if (subchnl->cdsc_format & CDROM_LBA) { 27942 subchnl->cdsc_absaddr.lba = 27943 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27944 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27945 subchnl->cdsc_reladdr.lba = 27946 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27947 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27948 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27949 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27950 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27951 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27952 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27953 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27954 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27955 } else { 27956 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27957 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27958 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27959 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27960 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27961 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27962 } 27963 kmem_free(buffer, 16); 27964 kmem_free(com, sizeof (*com)); 27965 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27966 != 0) { 27967 return (EFAULT); 27968 } 27969 return (rval); 27970 } 27971 27972 27973 /* 27974 * Function: sr_read_tocentry() 27975 * 27976 * Description: This routine is the driver entry point for handling CD-ROM 27977 * ioctl requests to read from the Table of Contents (TOC) 27978 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27979 * fields, the starting address (LBA or MSF format per the user) 27980 * and the data mode if the user specified track is a data track. 27981 * 27982 * Note: The READ HEADER (0x44) command used in this routine is 27983 * obsolete per the SCSI MMC spec but still supported in the 27984 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27985 * therefore the command is still implemented in this routine. 27986 * 27987 * Arguments: dev - the device 'dev_t' 27988 * data - pointer to user provided toc entry structure, 27989 * specifying the track # and the address format 27990 * (LBA or MSF). 27991 * flag - this argument is a pass through to ddi_copyxxx() 27992 * directly from the mode argument of ioctl(). 27993 * 27994 * Return Code: the code returned by sd_send_scsi_cmd() 27995 * EFAULT if ddi_copyxxx() fails 27996 * ENXIO if fail ddi_get_soft_state 27997 * EINVAL if data pointer is NULL 27998 */ 27999 28000 static int 28001 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28002 { 28003 struct sd_lun *un = NULL; 28004 struct uscsi_cmd *com; 28005 struct cdrom_tocentry toc_entry; 28006 struct cdrom_tocentry *entry = &toc_entry; 28007 caddr_t buffer; 28008 int rval; 28009 char cdb[CDB_GROUP1]; 28010 28011 if (data == NULL) { 28012 return (EINVAL); 28013 } 28014 28015 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28016 (un->un_state == SD_STATE_OFFLINE)) { 28017 return (ENXIO); 28018 } 28019 28020 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28021 return (EFAULT); 28022 } 28023 28024 /* Validate the requested track and address format */ 28025 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28026 return (EINVAL); 28027 } 28028 28029 if (entry->cdte_track == 0) { 28030 return (EINVAL); 28031 } 28032 28033 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28034 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28035 bzero(cdb, CDB_GROUP1); 28036 28037 cdb[0] = SCMD_READ_TOC; 28038 /* Set the MSF bit based on the user requested address format */ 28039 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28040 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28041 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28042 } else { 28043 cdb[6] = entry->cdte_track; 28044 } 28045 28046 /* 28047 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28048 * (4 byte TOC response header + 8 byte track descriptor) 28049 */ 28050 cdb[8] = 12; 28051 com->uscsi_cdb = cdb; 28052 com->uscsi_cdblen = CDB_GROUP1; 28053 com->uscsi_bufaddr = buffer; 28054 com->uscsi_buflen = 0x0C; 28055 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28056 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28057 UIO_SYSSPACE, SD_PATH_STANDARD); 28058 if (rval != 0) { 28059 kmem_free(buffer, 12); 28060 kmem_free(com, sizeof (*com)); 28061 return (rval); 28062 } 28063 28064 /* Process the toc entry */ 28065 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28066 entry->cdte_ctrl = (buffer[5] & 0x0F); 28067 if (entry->cdte_format & CDROM_LBA) { 28068 entry->cdte_addr.lba = 28069 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28070 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28071 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28072 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28073 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28074 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28075 /* 28076 * Send a READ TOC command using the LBA address format to get 28077 * the LBA for the track requested so it can be used in the 28078 * READ HEADER request 28079 * 28080 * Note: The MSF bit of the READ HEADER command specifies the 28081 * output format. The block address specified in that command 28082 * must be in LBA format. 28083 */ 28084 cdb[1] = 0; 28085 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28086 UIO_SYSSPACE, SD_PATH_STANDARD); 28087 if (rval != 0) { 28088 kmem_free(buffer, 12); 28089 kmem_free(com, sizeof (*com)); 28090 return (rval); 28091 } 28092 } else { 28093 entry->cdte_addr.msf.minute = buffer[9]; 28094 entry->cdte_addr.msf.second = buffer[10]; 28095 entry->cdte_addr.msf.frame = buffer[11]; 28096 /* 28097 * Send a READ TOC command using the LBA address format to get 28098 * the LBA for the track requested so it can be used in the 28099 * READ HEADER request 28100 * 28101 * Note: The MSF bit of the READ HEADER command specifies the 28102 * output format. The block address specified in that command 28103 * must be in LBA format. 28104 */ 28105 cdb[1] = 0; 28106 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28107 UIO_SYSSPACE, SD_PATH_STANDARD); 28108 if (rval != 0) { 28109 kmem_free(buffer, 12); 28110 kmem_free(com, sizeof (*com)); 28111 return (rval); 28112 } 28113 } 28114 28115 /* 28116 * Build and send the READ HEADER command to determine the data mode of 28117 * the user specified track. 28118 */ 28119 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28120 (entry->cdte_track != CDROM_LEADOUT)) { 28121 bzero(cdb, CDB_GROUP1); 28122 cdb[0] = SCMD_READ_HEADER; 28123 cdb[2] = buffer[8]; 28124 cdb[3] = buffer[9]; 28125 cdb[4] = buffer[10]; 28126 cdb[5] = buffer[11]; 28127 cdb[8] = 0x08; 28128 com->uscsi_buflen = 0x08; 28129 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28130 UIO_SYSSPACE, SD_PATH_STANDARD); 28131 if (rval == 0) { 28132 entry->cdte_datamode = buffer[0]; 28133 } else { 28134 /* 28135 * READ HEADER command failed, since this is 28136 * obsoleted in one spec, its better to return 28137 * -1 for an invlid track so that we can still 28138 * recieve the rest of the TOC data. 28139 */ 28140 entry->cdte_datamode = (uchar_t)-1; 28141 } 28142 } else { 28143 entry->cdte_datamode = (uchar_t)-1; 28144 } 28145 28146 kmem_free(buffer, 12); 28147 kmem_free(com, sizeof (*com)); 28148 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28149 return (EFAULT); 28150 28151 return (rval); 28152 } 28153 28154 28155 /* 28156 * Function: sr_read_tochdr() 28157 * 28158 * Description: This routine is the driver entry point for handling CD-ROM 28159 * ioctl requests to read the Table of Contents (TOC) header 28160 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28161 * and ending track numbers 28162 * 28163 * Arguments: dev - the device 'dev_t' 28164 * data - pointer to user provided toc header structure, 28165 * specifying the starting and ending track numbers. 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_tochdr(dev_t dev, caddr_t data, int flag) 28177 { 28178 struct sd_lun *un; 28179 struct uscsi_cmd *com; 28180 struct cdrom_tochdr toc_header; 28181 struct cdrom_tochdr *hdr = &toc_header; 28182 char cdb[CDB_GROUP1]; 28183 int rval; 28184 caddr_t buffer; 28185 28186 if (data == NULL) { 28187 return (EINVAL); 28188 } 28189 28190 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28191 (un->un_state == SD_STATE_OFFLINE)) { 28192 return (ENXIO); 28193 } 28194 28195 buffer = kmem_zalloc(4, KM_SLEEP); 28196 bzero(cdb, CDB_GROUP1); 28197 cdb[0] = SCMD_READ_TOC; 28198 /* 28199 * Specifying a track number of 0x00 in the READ TOC command indicates 28200 * that the TOC header should be returned 28201 */ 28202 cdb[6] = 0x00; 28203 /* 28204 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28205 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28206 */ 28207 cdb[8] = 0x04; 28208 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28209 com->uscsi_cdb = cdb; 28210 com->uscsi_cdblen = CDB_GROUP1; 28211 com->uscsi_bufaddr = buffer; 28212 com->uscsi_buflen = 0x04; 28213 com->uscsi_timeout = 300; 28214 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28215 28216 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28217 UIO_SYSSPACE, SD_PATH_STANDARD); 28218 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28219 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28220 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28221 } else { 28222 hdr->cdth_trk0 = buffer[2]; 28223 hdr->cdth_trk1 = buffer[3]; 28224 } 28225 kmem_free(buffer, 4); 28226 kmem_free(com, sizeof (*com)); 28227 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28228 return (EFAULT); 28229 } 28230 return (rval); 28231 } 28232 28233 28234 /* 28235 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28236 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28237 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28238 * digital audio and extended architecture digital audio. These modes are 28239 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28240 * MMC specs. 28241 * 28242 * In addition to support for the various data formats these routines also 28243 * include support for devices that implement only the direct access READ 28244 * commands (0x08, 0x28), devices that implement the READ_CD commands 28245 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28246 * READ CDXA commands (0xD8, 0xDB) 28247 */ 28248 28249 /* 28250 * Function: sr_read_mode1() 28251 * 28252 * Description: This routine is the driver entry point for handling CD-ROM 28253 * ioctl read mode1 requests (CDROMREADMODE1). 28254 * 28255 * Arguments: dev - the device 'dev_t' 28256 * data - pointer to user provided cd read structure specifying 28257 * the lba buffer address and length. 28258 * flag - this argument is a pass through to ddi_copyxxx() 28259 * directly from the mode argument of ioctl(). 28260 * 28261 * Return Code: the code returned by sd_send_scsi_cmd() 28262 * EFAULT if ddi_copyxxx() fails 28263 * ENXIO if fail ddi_get_soft_state 28264 * EINVAL if data pointer is NULL 28265 */ 28266 28267 static int 28268 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28269 { 28270 struct sd_lun *un; 28271 struct cdrom_read mode1_struct; 28272 struct cdrom_read *mode1 = &mode1_struct; 28273 int rval; 28274 #ifdef _MULTI_DATAMODEL 28275 /* To support ILP32 applications in an LP64 world */ 28276 struct cdrom_read32 cdrom_read32; 28277 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28278 #endif /* _MULTI_DATAMODEL */ 28279 28280 if (data == NULL) { 28281 return (EINVAL); 28282 } 28283 28284 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28285 (un->un_state == SD_STATE_OFFLINE)) { 28286 return (ENXIO); 28287 } 28288 28289 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28290 "sd_read_mode1: entry: un:0x%p\n", un); 28291 28292 #ifdef _MULTI_DATAMODEL 28293 switch (ddi_model_convert_from(flag & FMODELS)) { 28294 case DDI_MODEL_ILP32: 28295 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28296 return (EFAULT); 28297 } 28298 /* Convert the ILP32 uscsi data from the application to LP64 */ 28299 cdrom_read32tocdrom_read(cdrd32, mode1); 28300 break; 28301 case DDI_MODEL_NONE: 28302 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28303 return (EFAULT); 28304 } 28305 } 28306 #else /* ! _MULTI_DATAMODEL */ 28307 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28308 return (EFAULT); 28309 } 28310 #endif /* _MULTI_DATAMODEL */ 28311 28312 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28313 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28314 28315 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28316 "sd_read_mode1: exit: un:0x%p\n", un); 28317 28318 return (rval); 28319 } 28320 28321 28322 /* 28323 * Function: sr_read_cd_mode2() 28324 * 28325 * Description: This routine is the driver entry point for handling CD-ROM 28326 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28327 * support the READ CD (0xBE) command or the 1st generation 28328 * READ CD (0xD4) command. 28329 * 28330 * Arguments: dev - the device 'dev_t' 28331 * data - pointer to user provided cd read structure specifying 28332 * the lba buffer address and length. 28333 * flag - this argument is a pass through to ddi_copyxxx() 28334 * directly from the mode argument of ioctl(). 28335 * 28336 * Return Code: the code returned by sd_send_scsi_cmd() 28337 * EFAULT if ddi_copyxxx() fails 28338 * ENXIO if fail ddi_get_soft_state 28339 * EINVAL if data pointer is NULL 28340 */ 28341 28342 static int 28343 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28344 { 28345 struct sd_lun *un; 28346 struct uscsi_cmd *com; 28347 struct cdrom_read mode2_struct; 28348 struct cdrom_read *mode2 = &mode2_struct; 28349 uchar_t cdb[CDB_GROUP5]; 28350 int nblocks; 28351 int rval; 28352 #ifdef _MULTI_DATAMODEL 28353 /* To support ILP32 applications in an LP64 world */ 28354 struct cdrom_read32 cdrom_read32; 28355 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28356 #endif /* _MULTI_DATAMODEL */ 28357 28358 if (data == NULL) { 28359 return (EINVAL); 28360 } 28361 28362 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28363 (un->un_state == SD_STATE_OFFLINE)) { 28364 return (ENXIO); 28365 } 28366 28367 #ifdef _MULTI_DATAMODEL 28368 switch (ddi_model_convert_from(flag & FMODELS)) { 28369 case DDI_MODEL_ILP32: 28370 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28371 return (EFAULT); 28372 } 28373 /* Convert the ILP32 uscsi data from the application to LP64 */ 28374 cdrom_read32tocdrom_read(cdrd32, mode2); 28375 break; 28376 case DDI_MODEL_NONE: 28377 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28378 return (EFAULT); 28379 } 28380 break; 28381 } 28382 28383 #else /* ! _MULTI_DATAMODEL */ 28384 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28385 return (EFAULT); 28386 } 28387 #endif /* _MULTI_DATAMODEL */ 28388 28389 bzero(cdb, sizeof (cdb)); 28390 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28391 /* Read command supported by 1st generation atapi drives */ 28392 cdb[0] = SCMD_READ_CDD4; 28393 } else { 28394 /* Universal CD Access Command */ 28395 cdb[0] = SCMD_READ_CD; 28396 } 28397 28398 /* 28399 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28400 */ 28401 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28402 28403 /* set the start address */ 28404 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28405 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28406 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28407 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28408 28409 /* set the transfer length */ 28410 nblocks = mode2->cdread_buflen / 2336; 28411 cdb[6] = (uchar_t)(nblocks >> 16); 28412 cdb[7] = (uchar_t)(nblocks >> 8); 28413 cdb[8] = (uchar_t)nblocks; 28414 28415 /* set the filter bits */ 28416 cdb[9] = CDROM_READ_CD_USERDATA; 28417 28418 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28419 com->uscsi_cdb = (caddr_t)cdb; 28420 com->uscsi_cdblen = sizeof (cdb); 28421 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28422 com->uscsi_buflen = mode2->cdread_buflen; 28423 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28424 28425 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28426 UIO_SYSSPACE, SD_PATH_STANDARD); 28427 kmem_free(com, sizeof (*com)); 28428 return (rval); 28429 } 28430 28431 28432 /* 28433 * Function: sr_read_mode2() 28434 * 28435 * Description: This routine is the driver entry point for handling CD-ROM 28436 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28437 * do not support the READ CD (0xBE) command. 28438 * 28439 * Arguments: dev - the device 'dev_t' 28440 * data - pointer to user provided cd read structure specifying 28441 * the lba buffer address and length. 28442 * flag - this argument is a pass through to ddi_copyxxx() 28443 * directly from the mode argument of ioctl(). 28444 * 28445 * Return Code: the code returned by sd_send_scsi_cmd() 28446 * EFAULT if ddi_copyxxx() fails 28447 * ENXIO if fail ddi_get_soft_state 28448 * EINVAL if data pointer is NULL 28449 * EIO if fail to reset block size 28450 * EAGAIN if commands are in progress in the driver 28451 */ 28452 28453 static int 28454 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28455 { 28456 struct sd_lun *un; 28457 struct cdrom_read mode2_struct; 28458 struct cdrom_read *mode2 = &mode2_struct; 28459 int rval; 28460 uint32_t restore_blksize; 28461 struct uscsi_cmd *com; 28462 uchar_t cdb[CDB_GROUP0]; 28463 int nblocks; 28464 28465 #ifdef _MULTI_DATAMODEL 28466 /* To support ILP32 applications in an LP64 world */ 28467 struct cdrom_read32 cdrom_read32; 28468 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28469 #endif /* _MULTI_DATAMODEL */ 28470 28471 if (data == NULL) { 28472 return (EINVAL); 28473 } 28474 28475 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28476 (un->un_state == SD_STATE_OFFLINE)) { 28477 return (ENXIO); 28478 } 28479 28480 /* 28481 * Because this routine will update the device and driver block size 28482 * being used we want to make sure there are no commands in progress. 28483 * If commands are in progress the user will have to try again. 28484 * 28485 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28486 * in sdioctl to protect commands from sdioctl through to the top of 28487 * sd_uscsi_strategy. See sdioctl for details. 28488 */ 28489 mutex_enter(SD_MUTEX(un)); 28490 if (un->un_ncmds_in_driver != 1) { 28491 mutex_exit(SD_MUTEX(un)); 28492 return (EAGAIN); 28493 } 28494 mutex_exit(SD_MUTEX(un)); 28495 28496 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28497 "sd_read_mode2: entry: un:0x%p\n", un); 28498 28499 #ifdef _MULTI_DATAMODEL 28500 switch (ddi_model_convert_from(flag & FMODELS)) { 28501 case DDI_MODEL_ILP32: 28502 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28503 return (EFAULT); 28504 } 28505 /* Convert the ILP32 uscsi data from the application to LP64 */ 28506 cdrom_read32tocdrom_read(cdrd32, mode2); 28507 break; 28508 case DDI_MODEL_NONE: 28509 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28510 return (EFAULT); 28511 } 28512 break; 28513 } 28514 #else /* ! _MULTI_DATAMODEL */ 28515 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28516 return (EFAULT); 28517 } 28518 #endif /* _MULTI_DATAMODEL */ 28519 28520 /* Store the current target block size for restoration later */ 28521 restore_blksize = un->un_tgt_blocksize; 28522 28523 /* Change the device and soft state target block size to 2336 */ 28524 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28525 rval = EIO; 28526 goto done; 28527 } 28528 28529 28530 bzero(cdb, sizeof (cdb)); 28531 28532 /* set READ operation */ 28533 cdb[0] = SCMD_READ; 28534 28535 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28536 mode2->cdread_lba >>= 2; 28537 28538 /* set the start address */ 28539 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28540 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28541 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28542 28543 /* set the transfer length */ 28544 nblocks = mode2->cdread_buflen / 2336; 28545 cdb[4] = (uchar_t)nblocks & 0xFF; 28546 28547 /* build command */ 28548 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28549 com->uscsi_cdb = (caddr_t)cdb; 28550 com->uscsi_cdblen = sizeof (cdb); 28551 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28552 com->uscsi_buflen = mode2->cdread_buflen; 28553 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28554 28555 /* 28556 * Issue SCSI command with user space address for read buffer. 28557 * 28558 * This sends the command through main channel in the driver. 28559 * 28560 * Since this is accessed via an IOCTL call, we go through the 28561 * standard path, so that if the device was powered down, then 28562 * it would be 'awakened' to handle the command. 28563 */ 28564 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28565 UIO_SYSSPACE, SD_PATH_STANDARD); 28566 28567 kmem_free(com, sizeof (*com)); 28568 28569 /* Restore the device and soft state target block size */ 28570 if (sr_sector_mode(dev, restore_blksize) != 0) { 28571 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28572 "can't do switch back to mode 1\n"); 28573 /* 28574 * If sd_send_scsi_READ succeeded we still need to report 28575 * an error because we failed to reset the block size 28576 */ 28577 if (rval == 0) { 28578 rval = EIO; 28579 } 28580 } 28581 28582 done: 28583 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28584 "sd_read_mode2: exit: un:0x%p\n", un); 28585 28586 return (rval); 28587 } 28588 28589 28590 /* 28591 * Function: sr_sector_mode() 28592 * 28593 * Description: This utility function is used by sr_read_mode2 to set the target 28594 * block size based on the user specified size. This is a legacy 28595 * implementation based upon a vendor specific mode page 28596 * 28597 * Arguments: dev - the device 'dev_t' 28598 * data - flag indicating if block size is being set to 2336 or 28599 * 512. 28600 * 28601 * Return Code: the code returned by sd_send_scsi_cmd() 28602 * EFAULT if ddi_copyxxx() fails 28603 * ENXIO if fail ddi_get_soft_state 28604 * EINVAL if data pointer is NULL 28605 */ 28606 28607 static int 28608 sr_sector_mode(dev_t dev, uint32_t blksize) 28609 { 28610 struct sd_lun *un; 28611 uchar_t *sense; 28612 uchar_t *select; 28613 int rval; 28614 28615 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28616 (un->un_state == SD_STATE_OFFLINE)) { 28617 return (ENXIO); 28618 } 28619 28620 sense = kmem_zalloc(20, KM_SLEEP); 28621 28622 /* Note: This is a vendor specific mode page (0x81) */ 28623 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28624 SD_PATH_STANDARD)) != 0) { 28625 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28626 "sr_sector_mode: Mode Sense failed\n"); 28627 kmem_free(sense, 20); 28628 return (rval); 28629 } 28630 select = kmem_zalloc(20, KM_SLEEP); 28631 select[3] = 0x08; 28632 select[10] = ((blksize >> 8) & 0xff); 28633 select[11] = (blksize & 0xff); 28634 select[12] = 0x01; 28635 select[13] = 0x06; 28636 select[14] = sense[14]; 28637 select[15] = sense[15]; 28638 if (blksize == SD_MODE2_BLKSIZE) { 28639 select[14] |= 0x01; 28640 } 28641 28642 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28643 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28644 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28645 "sr_sector_mode: Mode Select failed\n"); 28646 } else { 28647 /* 28648 * Only update the softstate block size if we successfully 28649 * changed the device block mode. 28650 */ 28651 mutex_enter(SD_MUTEX(un)); 28652 sd_update_block_info(un, blksize, 0); 28653 mutex_exit(SD_MUTEX(un)); 28654 } 28655 kmem_free(sense, 20); 28656 kmem_free(select, 20); 28657 return (rval); 28658 } 28659 28660 28661 /* 28662 * Function: sr_read_cdda() 28663 * 28664 * Description: This routine is the driver entry point for handling CD-ROM 28665 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28666 * the target supports CDDA these requests are handled via a vendor 28667 * specific command (0xD8) If the target does not support CDDA 28668 * these requests are handled via the READ CD command (0xBE). 28669 * 28670 * Arguments: dev - the device 'dev_t' 28671 * data - pointer to user provided CD-DA structure specifying 28672 * the track starting address, transfer length, and 28673 * subcode options. 28674 * flag - this argument is a pass through to ddi_copyxxx() 28675 * directly from the mode argument of ioctl(). 28676 * 28677 * Return Code: the code returned by sd_send_scsi_cmd() 28678 * EFAULT if ddi_copyxxx() fails 28679 * ENXIO if fail ddi_get_soft_state 28680 * EINVAL if invalid arguments are provided 28681 * ENOTTY 28682 */ 28683 28684 static int 28685 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28686 { 28687 struct sd_lun *un; 28688 struct uscsi_cmd *com; 28689 struct cdrom_cdda *cdda; 28690 int rval; 28691 size_t buflen; 28692 char cdb[CDB_GROUP5]; 28693 28694 #ifdef _MULTI_DATAMODEL 28695 /* To support ILP32 applications in an LP64 world */ 28696 struct cdrom_cdda32 cdrom_cdda32; 28697 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28698 #endif /* _MULTI_DATAMODEL */ 28699 28700 if (data == NULL) { 28701 return (EINVAL); 28702 } 28703 28704 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28705 return (ENXIO); 28706 } 28707 28708 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28709 28710 #ifdef _MULTI_DATAMODEL 28711 switch (ddi_model_convert_from(flag & FMODELS)) { 28712 case DDI_MODEL_ILP32: 28713 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28714 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28715 "sr_read_cdda: ddi_copyin Failed\n"); 28716 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28717 return (EFAULT); 28718 } 28719 /* Convert the ILP32 uscsi data from the application to LP64 */ 28720 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28721 break; 28722 case DDI_MODEL_NONE: 28723 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28724 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28725 "sr_read_cdda: ddi_copyin Failed\n"); 28726 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28727 return (EFAULT); 28728 } 28729 break; 28730 } 28731 #else /* ! _MULTI_DATAMODEL */ 28732 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28733 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28734 "sr_read_cdda: ddi_copyin Failed\n"); 28735 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28736 return (EFAULT); 28737 } 28738 #endif /* _MULTI_DATAMODEL */ 28739 28740 /* 28741 * Since MMC-2 expects max 3 bytes for length, check if the 28742 * length input is greater than 3 bytes 28743 */ 28744 if ((cdda->cdda_length & 0xFF000000) != 0) { 28745 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28746 "cdrom transfer length too large: %d (limit %d)\n", 28747 cdda->cdda_length, 0xFFFFFF); 28748 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28749 return (EINVAL); 28750 } 28751 28752 switch (cdda->cdda_subcode) { 28753 case CDROM_DA_NO_SUBCODE: 28754 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28755 break; 28756 case CDROM_DA_SUBQ: 28757 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28758 break; 28759 case CDROM_DA_ALL_SUBCODE: 28760 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28761 break; 28762 case CDROM_DA_SUBCODE_ONLY: 28763 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28764 break; 28765 default: 28766 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28767 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28768 cdda->cdda_subcode); 28769 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28770 return (EINVAL); 28771 } 28772 28773 /* Build and send the command */ 28774 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28775 bzero(cdb, CDB_GROUP5); 28776 28777 if (un->un_f_cfg_cdda == TRUE) { 28778 cdb[0] = (char)SCMD_READ_CD; 28779 cdb[1] = 0x04; 28780 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28781 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28782 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28783 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28784 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28785 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28786 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28787 cdb[9] = 0x10; 28788 switch (cdda->cdda_subcode) { 28789 case CDROM_DA_NO_SUBCODE : 28790 cdb[10] = 0x0; 28791 break; 28792 case CDROM_DA_SUBQ : 28793 cdb[10] = 0x2; 28794 break; 28795 case CDROM_DA_ALL_SUBCODE : 28796 cdb[10] = 0x1; 28797 break; 28798 case CDROM_DA_SUBCODE_ONLY : 28799 /* FALLTHROUGH */ 28800 default : 28801 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28802 kmem_free(com, sizeof (*com)); 28803 return (ENOTTY); 28804 } 28805 } else { 28806 cdb[0] = (char)SCMD_READ_CDDA; 28807 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28808 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28809 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28810 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28811 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28812 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28813 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28814 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28815 cdb[10] = cdda->cdda_subcode; 28816 } 28817 28818 com->uscsi_cdb = cdb; 28819 com->uscsi_cdblen = CDB_GROUP5; 28820 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28821 com->uscsi_buflen = buflen; 28822 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28823 28824 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28825 UIO_SYSSPACE, SD_PATH_STANDARD); 28826 28827 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28828 kmem_free(com, sizeof (*com)); 28829 return (rval); 28830 } 28831 28832 28833 /* 28834 * Function: sr_read_cdxa() 28835 * 28836 * Description: This routine is the driver entry point for handling CD-ROM 28837 * ioctl requests to return CD-XA (Extended Architecture) data. 28838 * (CDROMCDXA). 28839 * 28840 * Arguments: dev - the device 'dev_t' 28841 * data - pointer to user provided CD-XA structure specifying 28842 * the data starting address, transfer length, and format 28843 * flag - this argument is a pass through to ddi_copyxxx() 28844 * directly from the mode argument of ioctl(). 28845 * 28846 * Return Code: the code returned by sd_send_scsi_cmd() 28847 * EFAULT if ddi_copyxxx() fails 28848 * ENXIO if fail ddi_get_soft_state 28849 * EINVAL if data pointer is NULL 28850 */ 28851 28852 static int 28853 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28854 { 28855 struct sd_lun *un; 28856 struct uscsi_cmd *com; 28857 struct cdrom_cdxa *cdxa; 28858 int rval; 28859 size_t buflen; 28860 char cdb[CDB_GROUP5]; 28861 uchar_t read_flags; 28862 28863 #ifdef _MULTI_DATAMODEL 28864 /* To support ILP32 applications in an LP64 world */ 28865 struct cdrom_cdxa32 cdrom_cdxa32; 28866 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28867 #endif /* _MULTI_DATAMODEL */ 28868 28869 if (data == NULL) { 28870 return (EINVAL); 28871 } 28872 28873 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28874 return (ENXIO); 28875 } 28876 28877 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28878 28879 #ifdef _MULTI_DATAMODEL 28880 switch (ddi_model_convert_from(flag & FMODELS)) { 28881 case DDI_MODEL_ILP32: 28882 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28883 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28884 return (EFAULT); 28885 } 28886 /* 28887 * Convert the ILP32 uscsi data from the 28888 * application to LP64 for internal use. 28889 */ 28890 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28891 break; 28892 case DDI_MODEL_NONE: 28893 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28894 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28895 return (EFAULT); 28896 } 28897 break; 28898 } 28899 #else /* ! _MULTI_DATAMODEL */ 28900 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28901 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28902 return (EFAULT); 28903 } 28904 #endif /* _MULTI_DATAMODEL */ 28905 28906 /* 28907 * Since MMC-2 expects max 3 bytes for length, check if the 28908 * length input is greater than 3 bytes 28909 */ 28910 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28911 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28912 "cdrom transfer length too large: %d (limit %d)\n", 28913 cdxa->cdxa_length, 0xFFFFFF); 28914 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28915 return (EINVAL); 28916 } 28917 28918 switch (cdxa->cdxa_format) { 28919 case CDROM_XA_DATA: 28920 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28921 read_flags = 0x10; 28922 break; 28923 case CDROM_XA_SECTOR_DATA: 28924 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28925 read_flags = 0xf8; 28926 break; 28927 case CDROM_XA_DATA_W_ERROR: 28928 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28929 read_flags = 0xfc; 28930 break; 28931 default: 28932 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28933 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28934 cdxa->cdxa_format); 28935 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28936 return (EINVAL); 28937 } 28938 28939 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28940 bzero(cdb, CDB_GROUP5); 28941 if (un->un_f_mmc_cap == TRUE) { 28942 cdb[0] = (char)SCMD_READ_CD; 28943 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28944 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28945 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28946 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28947 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28948 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28949 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28950 cdb[9] = (char)read_flags; 28951 } else { 28952 /* 28953 * Note: A vendor specific command (0xDB) is being used her to 28954 * request a read of all subcodes. 28955 */ 28956 cdb[0] = (char)SCMD_READ_CDXA; 28957 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28958 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28959 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28960 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28961 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28962 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28963 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28964 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28965 cdb[10] = cdxa->cdxa_format; 28966 } 28967 com->uscsi_cdb = cdb; 28968 com->uscsi_cdblen = CDB_GROUP5; 28969 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28970 com->uscsi_buflen = buflen; 28971 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28972 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28973 UIO_SYSSPACE, SD_PATH_STANDARD); 28974 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28975 kmem_free(com, sizeof (*com)); 28976 return (rval); 28977 } 28978 28979 28980 /* 28981 * Function: sr_eject() 28982 * 28983 * Description: This routine is the driver entry point for handling CD-ROM 28984 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28985 * 28986 * Arguments: dev - the device 'dev_t' 28987 * 28988 * Return Code: the code returned by sd_send_scsi_cmd() 28989 */ 28990 28991 static int 28992 sr_eject(dev_t dev) 28993 { 28994 struct sd_lun *un; 28995 int rval; 28996 28997 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28998 (un->un_state == SD_STATE_OFFLINE)) { 28999 return (ENXIO); 29000 } 29001 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29002 SD_PATH_STANDARD)) != 0) { 29003 return (rval); 29004 } 29005 29006 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29007 SD_PATH_STANDARD); 29008 29009 if (rval == 0) { 29010 mutex_enter(SD_MUTEX(un)); 29011 sr_ejected(un); 29012 un->un_mediastate = DKIO_EJECTED; 29013 cv_broadcast(&un->un_state_cv); 29014 mutex_exit(SD_MUTEX(un)); 29015 } 29016 return (rval); 29017 } 29018 29019 29020 /* 29021 * Function: sr_ejected() 29022 * 29023 * Description: This routine updates the soft state structure to invalidate the 29024 * geometry information after the media has been ejected or a 29025 * media eject has been detected. 29026 * 29027 * Arguments: un - driver soft state (unit) structure 29028 */ 29029 29030 static void 29031 sr_ejected(struct sd_lun *un) 29032 { 29033 struct sd_errstats *stp; 29034 29035 ASSERT(un != NULL); 29036 ASSERT(mutex_owned(SD_MUTEX(un))); 29037 29038 un->un_f_blockcount_is_valid = FALSE; 29039 un->un_f_tgt_blocksize_is_valid = FALSE; 29040 un->un_f_geometry_is_valid = FALSE; 29041 29042 if (un->un_errstats != NULL) { 29043 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29044 stp->sd_capacity.value.ui64 = 0; 29045 } 29046 } 29047 29048 29049 /* 29050 * Function: sr_check_wp() 29051 * 29052 * Description: This routine checks the write protection of a removable media 29053 * disk via the write protect bit of the Mode Page Header device 29054 * specific field. This routine has been implemented to use the 29055 * error recovery mode page for all device types. 29056 * Note: In the future use a sd_send_scsi_MODE_SENSE() routine 29057 * 29058 * Arguments: dev - the device 'dev_t' 29059 * 29060 * Return Code: int indicating if the device is write protected (1) or not (0) 29061 * 29062 * Context: Kernel thread. 29063 * 29064 */ 29065 29066 static int 29067 sr_check_wp(dev_t dev) 29068 { 29069 struct sd_lun *un; 29070 uchar_t device_specific; 29071 uchar_t *sense; 29072 int hdrlen; 29073 int rval; 29074 int retry_flag = FALSE; 29075 29076 /* 29077 * Note: The return codes for this routine should be reworked to 29078 * properly handle the case of a NULL softstate. 29079 */ 29080 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29081 return (FALSE); 29082 } 29083 29084 if (un->un_f_cfg_is_atapi == TRUE) { 29085 retry_flag = TRUE; 29086 } 29087 29088 retry: 29089 if (un->un_f_cfg_is_atapi == TRUE) { 29090 /* 29091 * The mode page contents are not required; set the allocation 29092 * length for the mode page header only 29093 */ 29094 hdrlen = MODE_HEADER_LENGTH_GRP2; 29095 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29096 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29097 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 29098 device_specific = 29099 ((struct mode_header_grp2 *)sense)->device_specific; 29100 } else { 29101 hdrlen = MODE_HEADER_LENGTH; 29102 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29103 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29104 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 29105 device_specific = 29106 ((struct mode_header *)sense)->device_specific; 29107 } 29108 29109 if (rval != 0) { 29110 if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) { 29111 /* 29112 * For an Atapi Zip drive, observed the drive 29113 * reporting check condition for the first attempt. 29114 * Sense data indicating power on or bus device/reset. 29115 * Hence in case of failure need to try at least once 29116 * for Atapi devices. 29117 */ 29118 retry_flag = FALSE; 29119 kmem_free(sense, hdrlen); 29120 goto retry; 29121 } else { 29122 /* 29123 * Write protect mode sense failed; not all disks 29124 * understand this query. Return FALSE assuming that 29125 * these devices are not writable. 29126 */ 29127 rval = FALSE; 29128 } 29129 } else { 29130 if (device_specific & WRITE_PROTECT) { 29131 rval = TRUE; 29132 } else { 29133 rval = FALSE; 29134 } 29135 } 29136 kmem_free(sense, hdrlen); 29137 return (rval); 29138 } 29139 29140 29141 /* 29142 * Function: sr_volume_ctrl() 29143 * 29144 * Description: This routine is the driver entry point for handling CD-ROM 29145 * audio output volume ioctl requests. (CDROMVOLCTRL) 29146 * 29147 * Arguments: dev - the device 'dev_t' 29148 * data - pointer to user audio volume control structure 29149 * flag - this argument is a pass through to ddi_copyxxx() 29150 * directly from the mode argument of ioctl(). 29151 * 29152 * Return Code: the code returned by sd_send_scsi_cmd() 29153 * EFAULT if ddi_copyxxx() fails 29154 * ENXIO if fail ddi_get_soft_state 29155 * EINVAL if data pointer is NULL 29156 * 29157 */ 29158 29159 static int 29160 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29161 { 29162 struct sd_lun *un; 29163 struct cdrom_volctrl volume; 29164 struct cdrom_volctrl *vol = &volume; 29165 uchar_t *sense_page; 29166 uchar_t *select_page; 29167 uchar_t *sense; 29168 uchar_t *select; 29169 int sense_buflen; 29170 int select_buflen; 29171 int rval; 29172 29173 if (data == NULL) { 29174 return (EINVAL); 29175 } 29176 29177 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29178 (un->un_state == SD_STATE_OFFLINE)) { 29179 return (ENXIO); 29180 } 29181 29182 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29183 return (EFAULT); 29184 } 29185 29186 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29187 struct mode_header_grp2 *sense_mhp; 29188 struct mode_header_grp2 *select_mhp; 29189 int bd_len; 29190 29191 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29192 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29193 MODEPAGE_AUDIO_CTRL_LEN; 29194 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29195 select = kmem_zalloc(select_buflen, KM_SLEEP); 29196 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29197 sense_buflen, MODEPAGE_AUDIO_CTRL, 29198 SD_PATH_STANDARD)) != 0) { 29199 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29200 "sr_volume_ctrl: Mode Sense Failed\n"); 29201 kmem_free(sense, sense_buflen); 29202 kmem_free(select, select_buflen); 29203 return (rval); 29204 } 29205 sense_mhp = (struct mode_header_grp2 *)sense; 29206 select_mhp = (struct mode_header_grp2 *)select; 29207 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29208 sense_mhp->bdesc_length_lo; 29209 if (bd_len > MODE_BLK_DESC_LENGTH) { 29210 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29211 "sr_volume_ctrl: Mode Sense returned invalid " 29212 "block descriptor length\n"); 29213 kmem_free(sense, sense_buflen); 29214 kmem_free(select, select_buflen); 29215 return (EIO); 29216 } 29217 sense_page = (uchar_t *) 29218 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29219 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29220 select_mhp->length_msb = 0; 29221 select_mhp->length_lsb = 0; 29222 select_mhp->bdesc_length_hi = 0; 29223 select_mhp->bdesc_length_lo = 0; 29224 } else { 29225 struct mode_header *sense_mhp, *select_mhp; 29226 29227 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29228 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29229 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29230 select = kmem_zalloc(select_buflen, KM_SLEEP); 29231 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29232 sense_buflen, MODEPAGE_AUDIO_CTRL, 29233 SD_PATH_STANDARD)) != 0) { 29234 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29235 "sr_volume_ctrl: Mode Sense Failed\n"); 29236 kmem_free(sense, sense_buflen); 29237 kmem_free(select, select_buflen); 29238 return (rval); 29239 } 29240 sense_mhp = (struct mode_header *)sense; 29241 select_mhp = (struct mode_header *)select; 29242 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29243 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29244 "sr_volume_ctrl: Mode Sense returned invalid " 29245 "block descriptor length\n"); 29246 kmem_free(sense, sense_buflen); 29247 kmem_free(select, select_buflen); 29248 return (EIO); 29249 } 29250 sense_page = (uchar_t *) 29251 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29252 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29253 select_mhp->length = 0; 29254 select_mhp->bdesc_length = 0; 29255 } 29256 /* 29257 * Note: An audio control data structure could be created and overlayed 29258 * on the following in place of the array indexing method implemented. 29259 */ 29260 29261 /* Build the select data for the user volume data */ 29262 select_page[0] = MODEPAGE_AUDIO_CTRL; 29263 select_page[1] = 0xE; 29264 /* Set the immediate bit */ 29265 select_page[2] = 0x04; 29266 /* Zero out reserved fields */ 29267 select_page[3] = 0x00; 29268 select_page[4] = 0x00; 29269 /* Return sense data for fields not to be modified */ 29270 select_page[5] = sense_page[5]; 29271 select_page[6] = sense_page[6]; 29272 select_page[7] = sense_page[7]; 29273 /* Set the user specified volume levels for channel 0 and 1 */ 29274 select_page[8] = 0x01; 29275 select_page[9] = vol->channel0; 29276 select_page[10] = 0x02; 29277 select_page[11] = vol->channel1; 29278 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29279 select_page[12] = sense_page[12]; 29280 select_page[13] = sense_page[13]; 29281 select_page[14] = sense_page[14]; 29282 select_page[15] = sense_page[15]; 29283 29284 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29285 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29286 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29287 } else { 29288 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29289 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29290 } 29291 29292 kmem_free(sense, sense_buflen); 29293 kmem_free(select, select_buflen); 29294 return (rval); 29295 } 29296 29297 29298 /* 29299 * Function: sr_read_sony_session_offset() 29300 * 29301 * Description: This routine is the driver entry point for handling CD-ROM 29302 * ioctl requests for session offset information. (CDROMREADOFFSET) 29303 * The address of the first track in the last session of a 29304 * multi-session CD-ROM is returned 29305 * 29306 * Note: This routine uses a vendor specific key value in the 29307 * command control field without implementing any vendor check here 29308 * or in the ioctl routine. 29309 * 29310 * Arguments: dev - the device 'dev_t' 29311 * data - pointer to an int to hold the requested address 29312 * flag - this argument is a pass through to ddi_copyxxx() 29313 * directly from the mode argument of ioctl(). 29314 * 29315 * Return Code: the code returned by sd_send_scsi_cmd() 29316 * EFAULT if ddi_copyxxx() fails 29317 * ENXIO if fail ddi_get_soft_state 29318 * EINVAL if data pointer is NULL 29319 */ 29320 29321 static int 29322 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29323 { 29324 struct sd_lun *un; 29325 struct uscsi_cmd *com; 29326 caddr_t buffer; 29327 char cdb[CDB_GROUP1]; 29328 int session_offset = 0; 29329 int rval; 29330 29331 if (data == NULL) { 29332 return (EINVAL); 29333 } 29334 29335 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29336 (un->un_state == SD_STATE_OFFLINE)) { 29337 return (ENXIO); 29338 } 29339 29340 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29341 bzero(cdb, CDB_GROUP1); 29342 cdb[0] = SCMD_READ_TOC; 29343 /* 29344 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29345 * (4 byte TOC response header + 8 byte response data) 29346 */ 29347 cdb[8] = SONY_SESSION_OFFSET_LEN; 29348 /* Byte 9 is the control byte. A vendor specific value is used */ 29349 cdb[9] = SONY_SESSION_OFFSET_KEY; 29350 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29351 com->uscsi_cdb = cdb; 29352 com->uscsi_cdblen = CDB_GROUP1; 29353 com->uscsi_bufaddr = buffer; 29354 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29355 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29356 29357 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29358 UIO_SYSSPACE, SD_PATH_STANDARD); 29359 if (rval != 0) { 29360 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29361 kmem_free(com, sizeof (*com)); 29362 return (rval); 29363 } 29364 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29365 session_offset = 29366 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29367 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29368 /* 29369 * Offset returned offset in current lbasize block's. Convert to 29370 * 2k block's to return to the user 29371 */ 29372 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29373 session_offset >>= 2; 29374 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29375 session_offset >>= 1; 29376 } 29377 } 29378 29379 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29380 rval = EFAULT; 29381 } 29382 29383 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29384 kmem_free(com, sizeof (*com)); 29385 return (rval); 29386 } 29387 29388 29389 /* 29390 * Function: sd_wm_cache_constructor() 29391 * 29392 * Description: Cache Constructor for the wmap cache for the read/modify/write 29393 * devices. 29394 * 29395 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29396 * un - sd_lun structure for the device. 29397 * flag - the km flags passed to constructor 29398 * 29399 * Return Code: 0 on success. 29400 * -1 on failure. 29401 */ 29402 29403 /*ARGSUSED*/ 29404 static int 29405 sd_wm_cache_constructor(void *wm, void *un, int flags) 29406 { 29407 bzero(wm, sizeof (struct sd_w_map)); 29408 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29409 return (0); 29410 } 29411 29412 29413 /* 29414 * Function: sd_wm_cache_destructor() 29415 * 29416 * Description: Cache destructor for the wmap cache for the read/modify/write 29417 * devices. 29418 * 29419 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29420 * un - sd_lun structure for the device. 29421 */ 29422 /*ARGSUSED*/ 29423 static void 29424 sd_wm_cache_destructor(void *wm, void *un) 29425 { 29426 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29427 } 29428 29429 29430 /* 29431 * Function: sd_range_lock() 29432 * 29433 * Description: Lock the range of blocks specified as parameter to ensure 29434 * that read, modify write is atomic and no other i/o writes 29435 * to the same location. The range is specified in terms 29436 * of start and end blocks. Block numbers are the actual 29437 * media block numbers and not system. 29438 * 29439 * Arguments: un - sd_lun structure for the device. 29440 * startb - The starting block number 29441 * endb - The end block number 29442 * typ - type of i/o - simple/read_modify_write 29443 * 29444 * Return Code: wm - pointer to the wmap structure. 29445 * 29446 * Context: This routine can sleep. 29447 */ 29448 29449 static struct sd_w_map * 29450 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29451 { 29452 struct sd_w_map *wmp = NULL; 29453 struct sd_w_map *sl_wmp = NULL; 29454 struct sd_w_map *tmp_wmp; 29455 wm_state state = SD_WM_CHK_LIST; 29456 29457 29458 ASSERT(un != NULL); 29459 ASSERT(!mutex_owned(SD_MUTEX(un))); 29460 29461 mutex_enter(SD_MUTEX(un)); 29462 29463 while (state != SD_WM_DONE) { 29464 29465 switch (state) { 29466 case SD_WM_CHK_LIST: 29467 /* 29468 * This is the starting state. Check the wmap list 29469 * to see if the range is currently available. 29470 */ 29471 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29472 /* 29473 * If this is a simple write and no rmw 29474 * i/o is pending then try to lock the 29475 * range as the range should be available. 29476 */ 29477 state = SD_WM_LOCK_RANGE; 29478 } else { 29479 tmp_wmp = sd_get_range(un, startb, endb); 29480 if (tmp_wmp != NULL) { 29481 if ((wmp != NULL) && ONLIST(un, wmp)) { 29482 /* 29483 * Should not keep onlist wmps 29484 * while waiting this macro 29485 * will also do wmp = NULL; 29486 */ 29487 FREE_ONLIST_WMAP(un, wmp); 29488 } 29489 /* 29490 * sl_wmp is the wmap on which wait 29491 * is done, since the tmp_wmp points 29492 * to the inuse wmap, set sl_wmp to 29493 * tmp_wmp and change the state to sleep 29494 */ 29495 sl_wmp = tmp_wmp; 29496 state = SD_WM_WAIT_MAP; 29497 } else { 29498 state = SD_WM_LOCK_RANGE; 29499 } 29500 29501 } 29502 break; 29503 29504 case SD_WM_LOCK_RANGE: 29505 ASSERT(un->un_wm_cache); 29506 /* 29507 * The range need to be locked, try to get a wmap. 29508 * First attempt it with NO_SLEEP, want to avoid a sleep 29509 * if possible as we will have to release the sd mutex 29510 * if we have to sleep. 29511 */ 29512 if (wmp == NULL) 29513 wmp = kmem_cache_alloc(un->un_wm_cache, 29514 KM_NOSLEEP); 29515 if (wmp == NULL) { 29516 mutex_exit(SD_MUTEX(un)); 29517 _NOTE(DATA_READABLE_WITHOUT_LOCK 29518 (sd_lun::un_wm_cache)) 29519 wmp = kmem_cache_alloc(un->un_wm_cache, 29520 KM_SLEEP); 29521 mutex_enter(SD_MUTEX(un)); 29522 /* 29523 * we released the mutex so recheck and go to 29524 * check list state. 29525 */ 29526 state = SD_WM_CHK_LIST; 29527 } else { 29528 /* 29529 * We exit out of state machine since we 29530 * have the wmap. Do the housekeeping first. 29531 * place the wmap on the wmap list if it is not 29532 * on it already and then set the state to done. 29533 */ 29534 wmp->wm_start = startb; 29535 wmp->wm_end = endb; 29536 wmp->wm_flags = typ | SD_WM_BUSY; 29537 if (typ & SD_WTYPE_RMW) { 29538 un->un_rmw_count++; 29539 } 29540 /* 29541 * If not already on the list then link 29542 */ 29543 if (!ONLIST(un, wmp)) { 29544 wmp->wm_next = un->un_wm; 29545 wmp->wm_prev = NULL; 29546 if (wmp->wm_next) 29547 wmp->wm_next->wm_prev = wmp; 29548 un->un_wm = wmp; 29549 } 29550 state = SD_WM_DONE; 29551 } 29552 break; 29553 29554 case SD_WM_WAIT_MAP: 29555 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29556 /* 29557 * Wait is done on sl_wmp, which is set in the 29558 * check_list state. 29559 */ 29560 sl_wmp->wm_wanted_count++; 29561 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29562 sl_wmp->wm_wanted_count--; 29563 /* 29564 * We can reuse the memory from the completed sl_wmp 29565 * lock range for our new lock, but only if noone is 29566 * waiting for it. 29567 */ 29568 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29569 if (sl_wmp->wm_wanted_count == 0) { 29570 if (wmp != NULL) 29571 CHK_N_FREEWMP(un, wmp); 29572 wmp = sl_wmp; 29573 } 29574 sl_wmp = NULL; 29575 /* 29576 * After waking up, need to recheck for availability of 29577 * range. 29578 */ 29579 state = SD_WM_CHK_LIST; 29580 break; 29581 29582 default: 29583 panic("sd_range_lock: " 29584 "Unknown state %d in sd_range_lock", state); 29585 /*NOTREACHED*/ 29586 } /* switch(state) */ 29587 29588 } /* while(state != SD_WM_DONE) */ 29589 29590 mutex_exit(SD_MUTEX(un)); 29591 29592 ASSERT(wmp != NULL); 29593 29594 return (wmp); 29595 } 29596 29597 29598 /* 29599 * Function: sd_get_range() 29600 * 29601 * Description: Find if there any overlapping I/O to this one 29602 * Returns the write-map of 1st such I/O, NULL otherwise. 29603 * 29604 * Arguments: un - sd_lun structure for the device. 29605 * startb - The starting block number 29606 * endb - The end block number 29607 * 29608 * Return Code: wm - pointer to the wmap structure. 29609 */ 29610 29611 static struct sd_w_map * 29612 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29613 { 29614 struct sd_w_map *wmp; 29615 29616 ASSERT(un != NULL); 29617 29618 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29619 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29620 continue; 29621 } 29622 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29623 break; 29624 } 29625 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29626 break; 29627 } 29628 } 29629 29630 return (wmp); 29631 } 29632 29633 29634 /* 29635 * Function: sd_free_inlist_wmap() 29636 * 29637 * Description: Unlink and free a write map struct. 29638 * 29639 * Arguments: un - sd_lun structure for the device. 29640 * wmp - sd_w_map which needs to be unlinked. 29641 */ 29642 29643 static void 29644 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29645 { 29646 ASSERT(un != NULL); 29647 29648 if (un->un_wm == wmp) { 29649 un->un_wm = wmp->wm_next; 29650 } else { 29651 wmp->wm_prev->wm_next = wmp->wm_next; 29652 } 29653 29654 if (wmp->wm_next) { 29655 wmp->wm_next->wm_prev = wmp->wm_prev; 29656 } 29657 29658 wmp->wm_next = wmp->wm_prev = NULL; 29659 29660 kmem_cache_free(un->un_wm_cache, wmp); 29661 } 29662 29663 29664 /* 29665 * Function: sd_range_unlock() 29666 * 29667 * Description: Unlock the range locked by wm. 29668 * Free write map if nobody else is waiting on it. 29669 * 29670 * Arguments: un - sd_lun structure for the device. 29671 * wmp - sd_w_map which needs to be unlinked. 29672 */ 29673 29674 static void 29675 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29676 { 29677 ASSERT(un != NULL); 29678 ASSERT(wm != NULL); 29679 ASSERT(!mutex_owned(SD_MUTEX(un))); 29680 29681 mutex_enter(SD_MUTEX(un)); 29682 29683 if (wm->wm_flags & SD_WTYPE_RMW) { 29684 un->un_rmw_count--; 29685 } 29686 29687 if (wm->wm_wanted_count) { 29688 wm->wm_flags = 0; 29689 /* 29690 * Broadcast that the wmap is available now. 29691 */ 29692 cv_broadcast(&wm->wm_avail); 29693 } else { 29694 /* 29695 * If no one is waiting on the map, it should be free'ed. 29696 */ 29697 sd_free_inlist_wmap(un, wm); 29698 } 29699 29700 mutex_exit(SD_MUTEX(un)); 29701 } 29702 29703 29704 /* 29705 * Function: sd_read_modify_write_task 29706 * 29707 * Description: Called from a taskq thread to initiate the write phase of 29708 * a read-modify-write request. This is used for targets where 29709 * un->un_sys_blocksize != un->un_tgt_blocksize. 29710 * 29711 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29712 * 29713 * Context: Called under taskq thread context. 29714 */ 29715 29716 static void 29717 sd_read_modify_write_task(void *arg) 29718 { 29719 struct sd_mapblocksize_info *bsp; 29720 struct buf *bp; 29721 struct sd_xbuf *xp; 29722 struct sd_lun *un; 29723 29724 bp = arg; /* The bp is given in arg */ 29725 ASSERT(bp != NULL); 29726 29727 /* Get the pointer to the layer-private data struct */ 29728 xp = SD_GET_XBUF(bp); 29729 ASSERT(xp != NULL); 29730 bsp = xp->xb_private; 29731 ASSERT(bsp != NULL); 29732 29733 un = SD_GET_UN(bp); 29734 ASSERT(un != NULL); 29735 ASSERT(!mutex_owned(SD_MUTEX(un))); 29736 29737 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29738 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29739 29740 /* 29741 * This is the write phase of a read-modify-write request, called 29742 * under the context of a taskq thread in response to the completion 29743 * of the read portion of the rmw request completing under interrupt 29744 * context. The write request must be sent from here down the iostart 29745 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29746 * we use the layer index saved in the layer-private data area. 29747 */ 29748 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29749 29750 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29751 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29752 } 29753 29754 29755 /* 29756 * Function: sddump_do_read_of_rmw() 29757 * 29758 * Description: This routine will be called from sddump, If sddump is called 29759 * with an I/O which not aligned on device blocksize boundary 29760 * then the write has to be converted to read-modify-write. 29761 * Do the read part here in order to keep sddump simple. 29762 * Note - That the sd_mutex is held across the call to this 29763 * routine. 29764 * 29765 * Arguments: un - sd_lun 29766 * blkno - block number in terms of media block size. 29767 * nblk - number of blocks. 29768 * bpp - pointer to pointer to the buf structure. On return 29769 * from this function, *bpp points to the valid buffer 29770 * to which the write has to be done. 29771 * 29772 * Return Code: 0 for success or errno-type return code 29773 */ 29774 29775 static int 29776 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29777 struct buf **bpp) 29778 { 29779 int err; 29780 int i; 29781 int rval; 29782 struct buf *bp; 29783 struct scsi_pkt *pkt = NULL; 29784 uint32_t target_blocksize; 29785 29786 ASSERT(un != NULL); 29787 ASSERT(mutex_owned(SD_MUTEX(un))); 29788 29789 target_blocksize = un->un_tgt_blocksize; 29790 29791 mutex_exit(SD_MUTEX(un)); 29792 29793 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29794 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29795 if (bp == NULL) { 29796 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29797 "no resources for dumping; giving up"); 29798 err = ENOMEM; 29799 goto done; 29800 } 29801 29802 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29803 blkno, nblk); 29804 if (rval != 0) { 29805 scsi_free_consistent_buf(bp); 29806 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29807 "no resources for dumping; giving up"); 29808 err = ENOMEM; 29809 goto done; 29810 } 29811 29812 pkt->pkt_flags |= FLAG_NOINTR; 29813 29814 err = EIO; 29815 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29816 29817 /* 29818 * Scsi_poll returns 0 (success) if the command completes and 29819 * the status block is STATUS_GOOD. We should only check 29820 * errors if this condition is not true. Even then we should 29821 * send our own request sense packet only if we have a check 29822 * condition and auto request sense has not been performed by 29823 * the hba. 29824 */ 29825 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29826 29827 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29828 err = 0; 29829 break; 29830 } 29831 29832 /* 29833 * Check CMD_DEV_GONE 1st, give up if device is gone, 29834 * no need to read RQS data. 29835 */ 29836 if (pkt->pkt_reason == CMD_DEV_GONE) { 29837 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29838 "Device is gone\n"); 29839 break; 29840 } 29841 29842 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29843 SD_INFO(SD_LOG_DUMP, un, 29844 "sddump: read failed with CHECK, try # %d\n", i); 29845 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29846 (void) sd_send_polled_RQS(un); 29847 } 29848 29849 continue; 29850 } 29851 29852 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29853 int reset_retval = 0; 29854 29855 SD_INFO(SD_LOG_DUMP, un, 29856 "sddump: read failed with BUSY, try # %d\n", i); 29857 29858 if (un->un_f_lun_reset_enabled == TRUE) { 29859 reset_retval = scsi_reset(SD_ADDRESS(un), 29860 RESET_LUN); 29861 } 29862 if (reset_retval == 0) { 29863 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29864 } 29865 (void) sd_send_polled_RQS(un); 29866 29867 } else { 29868 SD_INFO(SD_LOG_DUMP, un, 29869 "sddump: read failed with 0x%x, try # %d\n", 29870 SD_GET_PKT_STATUS(pkt), i); 29871 mutex_enter(SD_MUTEX(un)); 29872 sd_reset_target(un, pkt); 29873 mutex_exit(SD_MUTEX(un)); 29874 } 29875 29876 /* 29877 * If we are not getting anywhere with lun/target resets, 29878 * let's reset the bus. 29879 */ 29880 if (i > SD_NDUMP_RETRIES/2) { 29881 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29882 (void) sd_send_polled_RQS(un); 29883 } 29884 29885 } 29886 scsi_destroy_pkt(pkt); 29887 29888 if (err != 0) { 29889 scsi_free_consistent_buf(bp); 29890 *bpp = NULL; 29891 } else { 29892 *bpp = bp; 29893 } 29894 29895 done: 29896 mutex_enter(SD_MUTEX(un)); 29897 return (err); 29898 } 29899 29900 29901 /* 29902 * Function: sd_failfast_flushq 29903 * 29904 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29905 * in b_flags and move them onto the failfast queue, then kick 29906 * off a thread to return all bp's on the failfast queue to 29907 * their owners with an error set. 29908 * 29909 * Arguments: un - pointer to the soft state struct for the instance. 29910 * 29911 * Context: may execute in interrupt context. 29912 */ 29913 29914 static void 29915 sd_failfast_flushq(struct sd_lun *un) 29916 { 29917 struct buf *bp; 29918 struct buf *next_waitq_bp; 29919 struct buf *prev_waitq_bp = NULL; 29920 29921 ASSERT(un != NULL); 29922 ASSERT(mutex_owned(SD_MUTEX(un))); 29923 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29924 ASSERT(un->un_failfast_bp == NULL); 29925 29926 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29927 "sd_failfast_flushq: entry: un:0x%p\n", un); 29928 29929 /* 29930 * Check if we should flush all bufs when entering failfast state, or 29931 * just those with B_FAILFAST set. 29932 */ 29933 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29934 /* 29935 * Move *all* bp's on the wait queue to the failfast flush 29936 * queue, including those that do NOT have B_FAILFAST set. 29937 */ 29938 if (un->un_failfast_headp == NULL) { 29939 ASSERT(un->un_failfast_tailp == NULL); 29940 un->un_failfast_headp = un->un_waitq_headp; 29941 } else { 29942 ASSERT(un->un_failfast_tailp != NULL); 29943 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29944 } 29945 29946 un->un_failfast_tailp = un->un_waitq_tailp; 29947 29948 /* update kstat for each bp moved out of the waitq */ 29949 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29950 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29951 } 29952 29953 /* empty the waitq */ 29954 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29955 29956 } else { 29957 /* 29958 * Go thru the wait queue, pick off all entries with 29959 * B_FAILFAST set, and move these onto the failfast queue. 29960 */ 29961 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29962 /* 29963 * Save the pointer to the next bp on the wait queue, 29964 * so we get to it on the next iteration of this loop. 29965 */ 29966 next_waitq_bp = bp->av_forw; 29967 29968 /* 29969 * If this bp from the wait queue does NOT have 29970 * B_FAILFAST set, just move on to the next element 29971 * in the wait queue. Note, this is the only place 29972 * where it is correct to set prev_waitq_bp. 29973 */ 29974 if ((bp->b_flags & B_FAILFAST) == 0) { 29975 prev_waitq_bp = bp; 29976 continue; 29977 } 29978 29979 /* 29980 * Remove the bp from the wait queue. 29981 */ 29982 if (bp == un->un_waitq_headp) { 29983 /* The bp is the first element of the waitq. */ 29984 un->un_waitq_headp = next_waitq_bp; 29985 if (un->un_waitq_headp == NULL) { 29986 /* The wait queue is now empty */ 29987 un->un_waitq_tailp = NULL; 29988 } 29989 } else { 29990 /* 29991 * The bp is either somewhere in the middle 29992 * or at the end of the wait queue. 29993 */ 29994 ASSERT(un->un_waitq_headp != NULL); 29995 ASSERT(prev_waitq_bp != NULL); 29996 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29997 == 0); 29998 if (bp == un->un_waitq_tailp) { 29999 /* bp is the last entry on the waitq. */ 30000 ASSERT(next_waitq_bp == NULL); 30001 un->un_waitq_tailp = prev_waitq_bp; 30002 } 30003 prev_waitq_bp->av_forw = next_waitq_bp; 30004 } 30005 bp->av_forw = NULL; 30006 30007 /* 30008 * update kstat since the bp is moved out of 30009 * the waitq 30010 */ 30011 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30012 30013 /* 30014 * Now put the bp onto the failfast queue. 30015 */ 30016 if (un->un_failfast_headp == NULL) { 30017 /* failfast queue is currently empty */ 30018 ASSERT(un->un_failfast_tailp == NULL); 30019 un->un_failfast_headp = 30020 un->un_failfast_tailp = bp; 30021 } else { 30022 /* Add the bp to the end of the failfast q */ 30023 ASSERT(un->un_failfast_tailp != NULL); 30024 ASSERT(un->un_failfast_tailp->b_flags & 30025 B_FAILFAST); 30026 un->un_failfast_tailp->av_forw = bp; 30027 un->un_failfast_tailp = bp; 30028 } 30029 } 30030 } 30031 30032 /* 30033 * Now return all bp's on the failfast queue to their owners. 30034 */ 30035 while ((bp = un->un_failfast_headp) != NULL) { 30036 30037 un->un_failfast_headp = bp->av_forw; 30038 if (un->un_failfast_headp == NULL) { 30039 un->un_failfast_tailp = NULL; 30040 } 30041 30042 /* 30043 * We want to return the bp with a failure error code, but 30044 * we do not want a call to sd_start_cmds() to occur here, 30045 * so use sd_return_failed_command_no_restart() instead of 30046 * sd_return_failed_command(). 30047 */ 30048 sd_return_failed_command_no_restart(un, bp, EIO); 30049 } 30050 30051 /* Flush the xbuf queues if required. */ 30052 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30053 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30054 } 30055 30056 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30057 "sd_failfast_flushq: exit: un:0x%p\n", un); 30058 } 30059 30060 30061 /* 30062 * Function: sd_failfast_flushq_callback 30063 * 30064 * Description: Return TRUE if the given bp meets the criteria for failfast 30065 * flushing. Used with ddi_xbuf_flushq(9F). 30066 * 30067 * Arguments: bp - ptr to buf struct to be examined. 30068 * 30069 * Context: Any 30070 */ 30071 30072 static int 30073 sd_failfast_flushq_callback(struct buf *bp) 30074 { 30075 /* 30076 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30077 * state is entered; OR (2) the given bp has B_FAILFAST set. 30078 */ 30079 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30080 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30081 } 30082 30083 30084 30085 #if defined(__i386) || defined(__amd64) 30086 /* 30087 * Function: sd_setup_next_xfer 30088 * 30089 * Description: Prepare next I/O operation using DMA_PARTIAL 30090 * 30091 */ 30092 30093 static int 30094 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30095 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30096 { 30097 ssize_t num_blks_not_xfered; 30098 daddr_t strt_blk_num; 30099 ssize_t bytes_not_xfered; 30100 int rval; 30101 30102 ASSERT(pkt->pkt_resid == 0); 30103 30104 /* 30105 * Calculate next block number and amount to be transferred. 30106 * 30107 * How much data NOT transfered to the HBA yet. 30108 */ 30109 bytes_not_xfered = xp->xb_dma_resid; 30110 30111 /* 30112 * figure how many blocks NOT transfered to the HBA yet. 30113 */ 30114 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30115 30116 /* 30117 * set starting block number to the end of what WAS transfered. 30118 */ 30119 strt_blk_num = xp->xb_blkno + 30120 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30121 30122 /* 30123 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30124 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30125 * the disk mutex here. 30126 */ 30127 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30128 strt_blk_num, num_blks_not_xfered); 30129 30130 if (rval == 0) { 30131 30132 /* 30133 * Success. 30134 * 30135 * Adjust things if there are still more blocks to be 30136 * transfered. 30137 */ 30138 xp->xb_dma_resid = pkt->pkt_resid; 30139 pkt->pkt_resid = 0; 30140 30141 return (1); 30142 } 30143 30144 /* 30145 * There's really only one possible return value from 30146 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30147 * returns NULL. 30148 */ 30149 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30150 30151 bp->b_resid = bp->b_bcount; 30152 bp->b_flags |= B_ERROR; 30153 30154 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30155 "Error setting up next portion of DMA transfer\n"); 30156 30157 return (0); 30158 } 30159 #endif 30160 30161 /* 30162 * Function: sd_panic_for_res_conflict 30163 * 30164 * Description: Call panic with a string formated with "Reservation Conflict" 30165 * and a human readable identifier indicating the SD instance 30166 * that experienced the reservation conflict. 30167 * 30168 * Arguments: un - pointer to the soft state struct for the instance. 30169 * 30170 * Context: may execute in interrupt context. 30171 */ 30172 30173 #define SD_RESV_CONFLICT_FMT_LEN 40 30174 void 30175 sd_panic_for_res_conflict(struct sd_lun *un) 30176 { 30177 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30178 char path_str[MAXPATHLEN]; 30179 30180 (void) snprintf(panic_str, sizeof (panic_str), 30181 "Reservation Conflict\nDisk: %s", 30182 ddi_pathname(SD_DEVINFO(un), path_str)); 30183 30184 panic(panic_str); 30185 } 30186 30187 /* 30188 * Note: The following sd_faultinjection_ioctl( ) routines implement 30189 * driver support for handling fault injection for error analysis 30190 * causing faults in multiple layers of the driver. 30191 * 30192 */ 30193 30194 #ifdef SD_FAULT_INJECTION 30195 static uint_t sd_fault_injection_on = 0; 30196 30197 /* 30198 * Function: sd_faultinjection_ioctl() 30199 * 30200 * Description: This routine is the driver entry point for handling 30201 * faultinjection ioctls to inject errors into the 30202 * layer model 30203 * 30204 * Arguments: cmd - the ioctl cmd recieved 30205 * arg - the arguments from user and returns 30206 */ 30207 30208 static void 30209 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30210 30211 uint_t i; 30212 uint_t rval; 30213 30214 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30215 30216 mutex_enter(SD_MUTEX(un)); 30217 30218 switch (cmd) { 30219 case SDIOCRUN: 30220 /* Allow pushed faults to be injected */ 30221 SD_INFO(SD_LOG_SDTEST, un, 30222 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30223 30224 sd_fault_injection_on = 1; 30225 30226 SD_INFO(SD_LOG_IOERR, un, 30227 "sd_faultinjection_ioctl: run finished\n"); 30228 break; 30229 30230 case SDIOCSTART: 30231 /* Start Injection Session */ 30232 SD_INFO(SD_LOG_SDTEST, un, 30233 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30234 30235 sd_fault_injection_on = 0; 30236 un->sd_injection_mask = 0xFFFFFFFF; 30237 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30238 un->sd_fi_fifo_pkt[i] = NULL; 30239 un->sd_fi_fifo_xb[i] = NULL; 30240 un->sd_fi_fifo_un[i] = NULL; 30241 un->sd_fi_fifo_arq[i] = NULL; 30242 } 30243 un->sd_fi_fifo_start = 0; 30244 un->sd_fi_fifo_end = 0; 30245 30246 mutex_enter(&(un->un_fi_mutex)); 30247 un->sd_fi_log[0] = '\0'; 30248 un->sd_fi_buf_len = 0; 30249 mutex_exit(&(un->un_fi_mutex)); 30250 30251 SD_INFO(SD_LOG_IOERR, un, 30252 "sd_faultinjection_ioctl: start finished\n"); 30253 break; 30254 30255 case SDIOCSTOP: 30256 /* Stop Injection Session */ 30257 SD_INFO(SD_LOG_SDTEST, un, 30258 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30259 sd_fault_injection_on = 0; 30260 un->sd_injection_mask = 0x0; 30261 30262 /* Empty stray or unuseds structs from fifo */ 30263 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30264 if (un->sd_fi_fifo_pkt[i] != NULL) { 30265 kmem_free(un->sd_fi_fifo_pkt[i], 30266 sizeof (struct sd_fi_pkt)); 30267 } 30268 if (un->sd_fi_fifo_xb[i] != NULL) { 30269 kmem_free(un->sd_fi_fifo_xb[i], 30270 sizeof (struct sd_fi_xb)); 30271 } 30272 if (un->sd_fi_fifo_un[i] != NULL) { 30273 kmem_free(un->sd_fi_fifo_un[i], 30274 sizeof (struct sd_fi_un)); 30275 } 30276 if (un->sd_fi_fifo_arq[i] != NULL) { 30277 kmem_free(un->sd_fi_fifo_arq[i], 30278 sizeof (struct sd_fi_arq)); 30279 } 30280 un->sd_fi_fifo_pkt[i] = NULL; 30281 un->sd_fi_fifo_un[i] = NULL; 30282 un->sd_fi_fifo_xb[i] = NULL; 30283 un->sd_fi_fifo_arq[i] = NULL; 30284 } 30285 un->sd_fi_fifo_start = 0; 30286 un->sd_fi_fifo_end = 0; 30287 30288 SD_INFO(SD_LOG_IOERR, un, 30289 "sd_faultinjection_ioctl: stop finished\n"); 30290 break; 30291 30292 case SDIOCINSERTPKT: 30293 /* Store a packet struct to be pushed onto fifo */ 30294 SD_INFO(SD_LOG_SDTEST, un, 30295 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30296 30297 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30298 30299 sd_fault_injection_on = 0; 30300 30301 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30302 if (un->sd_fi_fifo_pkt[i] != NULL) { 30303 kmem_free(un->sd_fi_fifo_pkt[i], 30304 sizeof (struct sd_fi_pkt)); 30305 } 30306 if (arg != NULL) { 30307 un->sd_fi_fifo_pkt[i] = 30308 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30309 if (un->sd_fi_fifo_pkt[i] == NULL) { 30310 /* Alloc failed don't store anything */ 30311 break; 30312 } 30313 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30314 sizeof (struct sd_fi_pkt), 0); 30315 if (rval == -1) { 30316 kmem_free(un->sd_fi_fifo_pkt[i], 30317 sizeof (struct sd_fi_pkt)); 30318 un->sd_fi_fifo_pkt[i] = NULL; 30319 } 30320 } else { 30321 SD_INFO(SD_LOG_IOERR, un, 30322 "sd_faultinjection_ioctl: pkt null\n"); 30323 } 30324 break; 30325 30326 case SDIOCINSERTXB: 30327 /* Store a xb struct to be pushed onto fifo */ 30328 SD_INFO(SD_LOG_SDTEST, un, 30329 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30330 30331 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30332 30333 sd_fault_injection_on = 0; 30334 30335 if (un->sd_fi_fifo_xb[i] != NULL) { 30336 kmem_free(un->sd_fi_fifo_xb[i], 30337 sizeof (struct sd_fi_xb)); 30338 un->sd_fi_fifo_xb[i] = NULL; 30339 } 30340 if (arg != NULL) { 30341 un->sd_fi_fifo_xb[i] = 30342 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30343 if (un->sd_fi_fifo_xb[i] == NULL) { 30344 /* Alloc failed don't store anything */ 30345 break; 30346 } 30347 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30348 sizeof (struct sd_fi_xb), 0); 30349 30350 if (rval == -1) { 30351 kmem_free(un->sd_fi_fifo_xb[i], 30352 sizeof (struct sd_fi_xb)); 30353 un->sd_fi_fifo_xb[i] = NULL; 30354 } 30355 } else { 30356 SD_INFO(SD_LOG_IOERR, un, 30357 "sd_faultinjection_ioctl: xb null\n"); 30358 } 30359 break; 30360 30361 case SDIOCINSERTUN: 30362 /* Store a un struct to be pushed onto fifo */ 30363 SD_INFO(SD_LOG_SDTEST, un, 30364 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30365 30366 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30367 30368 sd_fault_injection_on = 0; 30369 30370 if (un->sd_fi_fifo_un[i] != NULL) { 30371 kmem_free(un->sd_fi_fifo_un[i], 30372 sizeof (struct sd_fi_un)); 30373 un->sd_fi_fifo_un[i] = NULL; 30374 } 30375 if (arg != NULL) { 30376 un->sd_fi_fifo_un[i] = 30377 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30378 if (un->sd_fi_fifo_un[i] == NULL) { 30379 /* Alloc failed don't store anything */ 30380 break; 30381 } 30382 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30383 sizeof (struct sd_fi_un), 0); 30384 if (rval == -1) { 30385 kmem_free(un->sd_fi_fifo_un[i], 30386 sizeof (struct sd_fi_un)); 30387 un->sd_fi_fifo_un[i] = NULL; 30388 } 30389 30390 } else { 30391 SD_INFO(SD_LOG_IOERR, un, 30392 "sd_faultinjection_ioctl: un null\n"); 30393 } 30394 30395 break; 30396 30397 case SDIOCINSERTARQ: 30398 /* Store a arq struct to be pushed onto fifo */ 30399 SD_INFO(SD_LOG_SDTEST, un, 30400 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30401 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30402 30403 sd_fault_injection_on = 0; 30404 30405 if (un->sd_fi_fifo_arq[i] != NULL) { 30406 kmem_free(un->sd_fi_fifo_arq[i], 30407 sizeof (struct sd_fi_arq)); 30408 un->sd_fi_fifo_arq[i] = NULL; 30409 } 30410 if (arg != NULL) { 30411 un->sd_fi_fifo_arq[i] = 30412 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30413 if (un->sd_fi_fifo_arq[i] == NULL) { 30414 /* Alloc failed don't store anything */ 30415 break; 30416 } 30417 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30418 sizeof (struct sd_fi_arq), 0); 30419 if (rval == -1) { 30420 kmem_free(un->sd_fi_fifo_arq[i], 30421 sizeof (struct sd_fi_arq)); 30422 un->sd_fi_fifo_arq[i] = NULL; 30423 } 30424 30425 } else { 30426 SD_INFO(SD_LOG_IOERR, un, 30427 "sd_faultinjection_ioctl: arq null\n"); 30428 } 30429 30430 break; 30431 30432 case SDIOCPUSH: 30433 /* Push stored xb, pkt, un, and arq onto fifo */ 30434 sd_fault_injection_on = 0; 30435 30436 if (arg != NULL) { 30437 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30438 if (rval != -1 && 30439 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30440 un->sd_fi_fifo_end += i; 30441 } 30442 } else { 30443 SD_INFO(SD_LOG_IOERR, un, 30444 "sd_faultinjection_ioctl: push arg null\n"); 30445 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30446 un->sd_fi_fifo_end++; 30447 } 30448 } 30449 SD_INFO(SD_LOG_IOERR, un, 30450 "sd_faultinjection_ioctl: push to end=%d\n", 30451 un->sd_fi_fifo_end); 30452 break; 30453 30454 case SDIOCRETRIEVE: 30455 /* Return buffer of log from Injection session */ 30456 SD_INFO(SD_LOG_SDTEST, un, 30457 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30458 30459 sd_fault_injection_on = 0; 30460 30461 mutex_enter(&(un->un_fi_mutex)); 30462 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30463 un->sd_fi_buf_len+1, 0); 30464 mutex_exit(&(un->un_fi_mutex)); 30465 30466 if (rval == -1) { 30467 /* 30468 * arg is possibly invalid setting 30469 * it to NULL for return 30470 */ 30471 arg = NULL; 30472 } 30473 break; 30474 } 30475 30476 mutex_exit(SD_MUTEX(un)); 30477 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30478 " exit\n"); 30479 } 30480 30481 30482 /* 30483 * Function: sd_injection_log() 30484 * 30485 * Description: This routine adds buff to the already existing injection log 30486 * for retrieval via faultinjection_ioctl for use in fault 30487 * detection and recovery 30488 * 30489 * Arguments: buf - the string to add to the log 30490 */ 30491 30492 static void 30493 sd_injection_log(char *buf, struct sd_lun *un) 30494 { 30495 uint_t len; 30496 30497 ASSERT(un != NULL); 30498 ASSERT(buf != NULL); 30499 30500 mutex_enter(&(un->un_fi_mutex)); 30501 30502 len = min(strlen(buf), 255); 30503 /* Add logged value to Injection log to be returned later */ 30504 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30505 uint_t offset = strlen((char *)un->sd_fi_log); 30506 char *destp = (char *)un->sd_fi_log + offset; 30507 int i; 30508 for (i = 0; i < len; i++) { 30509 *destp++ = *buf++; 30510 } 30511 un->sd_fi_buf_len += len; 30512 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30513 } 30514 30515 mutex_exit(&(un->un_fi_mutex)); 30516 } 30517 30518 30519 /* 30520 * Function: sd_faultinjection() 30521 * 30522 * Description: This routine takes the pkt and changes its 30523 * content based on error injection scenerio. 30524 * 30525 * Arguments: pktp - packet to be changed 30526 */ 30527 30528 static void 30529 sd_faultinjection(struct scsi_pkt *pktp) 30530 { 30531 uint_t i; 30532 struct sd_fi_pkt *fi_pkt; 30533 struct sd_fi_xb *fi_xb; 30534 struct sd_fi_un *fi_un; 30535 struct sd_fi_arq *fi_arq; 30536 struct buf *bp; 30537 struct sd_xbuf *xb; 30538 struct sd_lun *un; 30539 30540 ASSERT(pktp != NULL); 30541 30542 /* pull bp xb and un from pktp */ 30543 bp = (struct buf *)pktp->pkt_private; 30544 xb = SD_GET_XBUF(bp); 30545 un = SD_GET_UN(bp); 30546 30547 ASSERT(un != NULL); 30548 30549 mutex_enter(SD_MUTEX(un)); 30550 30551 SD_TRACE(SD_LOG_SDTEST, un, 30552 "sd_faultinjection: entry Injection from sdintr\n"); 30553 30554 /* if injection is off return */ 30555 if (sd_fault_injection_on == 0 || 30556 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30557 mutex_exit(SD_MUTEX(un)); 30558 return; 30559 } 30560 30561 30562 /* take next set off fifo */ 30563 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30564 30565 fi_pkt = un->sd_fi_fifo_pkt[i]; 30566 fi_xb = un->sd_fi_fifo_xb[i]; 30567 fi_un = un->sd_fi_fifo_un[i]; 30568 fi_arq = un->sd_fi_fifo_arq[i]; 30569 30570 30571 /* set variables accordingly */ 30572 /* set pkt if it was on fifo */ 30573 if (fi_pkt != NULL) { 30574 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30575 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30576 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30577 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30578 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30579 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30580 30581 } 30582 30583 /* set xb if it was on fifo */ 30584 if (fi_xb != NULL) { 30585 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30586 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30587 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30588 SD_CONDSET(xb, xb, xb_victim_retry_count, 30589 "xb_victim_retry_count"); 30590 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30591 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30592 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30593 30594 /* copy in block data from sense */ 30595 if (fi_xb->xb_sense_data[0] != -1) { 30596 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30597 SENSE_LENGTH); 30598 } 30599 30600 /* copy in extended sense codes */ 30601 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30602 "es_code"); 30603 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30604 "es_key"); 30605 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30606 "es_add_code"); 30607 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30608 es_qual_code, "es_qual_code"); 30609 } 30610 30611 /* set un if it was on fifo */ 30612 if (fi_un != NULL) { 30613 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30614 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30615 SD_CONDSET(un, un, un_reset_retry_count, 30616 "un_reset_retry_count"); 30617 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30618 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30619 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30620 SD_CONDSET(un, un, un_f_geometry_is_valid, 30621 "un_f_geometry_is_valid"); 30622 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30623 "un_f_allow_bus_device_reset"); 30624 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30625 30626 } 30627 30628 /* copy in auto request sense if it was on fifo */ 30629 if (fi_arq != NULL) { 30630 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30631 } 30632 30633 /* free structs */ 30634 if (un->sd_fi_fifo_pkt[i] != NULL) { 30635 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30636 } 30637 if (un->sd_fi_fifo_xb[i] != NULL) { 30638 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30639 } 30640 if (un->sd_fi_fifo_un[i] != NULL) { 30641 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30642 } 30643 if (un->sd_fi_fifo_arq[i] != NULL) { 30644 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30645 } 30646 30647 /* 30648 * kmem_free does not gurantee to set to NULL 30649 * since we uses these to determine if we set 30650 * values or not lets confirm they are always 30651 * NULL after free 30652 */ 30653 un->sd_fi_fifo_pkt[i] = NULL; 30654 un->sd_fi_fifo_un[i] = NULL; 30655 un->sd_fi_fifo_xb[i] = NULL; 30656 un->sd_fi_fifo_arq[i] = NULL; 30657 30658 un->sd_fi_fifo_start++; 30659 30660 mutex_exit(SD_MUTEX(un)); 30661 30662 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30663 } 30664 30665 #endif /* SD_FAULT_INJECTION */ 30666 30667 /* 30668 * This routine is invoked in sd_unit_attach(). Before calling it, the 30669 * properties in conf file should be processed already, and "hotpluggable" 30670 * property was processed also. 30671 * 30672 * The sd driver distinguishes 3 different type of devices: removable media, 30673 * non-removable media, and hotpluggable. Below the differences are defined: 30674 * 30675 * 1. Device ID 30676 * 30677 * The device ID of a device is used to identify this device. Refer to 30678 * ddi_devid_register(9F). 30679 * 30680 * For a non-removable media disk device which can provide 0x80 or 0x83 30681 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30682 * device ID is created to identify this device. For other non-removable 30683 * media devices, a default device ID is created only if this device has 30684 * at least 2 alter cylinders. Otherwise, this device has no devid. 30685 * 30686 * ------------------------------------------------------- 30687 * removable media hotpluggable | Can Have Device ID 30688 * ------------------------------------------------------- 30689 * false false | Yes 30690 * false true | Yes 30691 * true x | No 30692 * ------------------------------------------------------ 30693 * 30694 * 30695 * 2. SCSI group 4 commands 30696 * 30697 * In SCSI specs, only some commands in group 4 command set can use 30698 * 8-byte addresses that can be used to access >2TB storage spaces. 30699 * Other commands have no such capability. Without supporting group4, 30700 * it is impossible to make full use of storage spaces of a disk with 30701 * capacity larger than 2TB. 30702 * 30703 * ----------------------------------------------- 30704 * removable media hotpluggable LP64 | Group 30705 * ----------------------------------------------- 30706 * false false false | 1 30707 * false false true | 4 30708 * false true false | 1 30709 * false true true | 4 30710 * true x x | 5 30711 * ----------------------------------------------- 30712 * 30713 * 30714 * 3. Check for VTOC Label 30715 * 30716 * If a direct-access disk has no EFI label, sd will check if it has a 30717 * valid VTOC label. Now, sd also does that check for removable media 30718 * and hotpluggable devices. 30719 * 30720 * -------------------------------------------------------------- 30721 * Direct-Access removable media hotpluggable | Check Label 30722 * ------------------------------------------------------------- 30723 * false false false | No 30724 * false false true | No 30725 * false true false | Yes 30726 * false true true | Yes 30727 * true x x | Yes 30728 * -------------------------------------------------------------- 30729 * 30730 * 30731 * 4. Building default VTOC label 30732 * 30733 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30734 * If those devices have no valid VTOC label, sd(7d) will attempt to 30735 * create default VTOC for them. Currently sd creates default VTOC label 30736 * for all devices on x86 platform (VTOC_16), but only for removable 30737 * media devices on SPARC (VTOC_8). 30738 * 30739 * ----------------------------------------------------------- 30740 * removable media hotpluggable platform | Default Label 30741 * ----------------------------------------------------------- 30742 * false false sparc | No 30743 * false true x86 | Yes 30744 * false true sparc | Yes 30745 * true x x | Yes 30746 * ---------------------------------------------------------- 30747 * 30748 * 30749 * 5. Supported blocksizes of target devices 30750 * 30751 * Sd supports non-512-byte blocksize for removable media devices only. 30752 * For other devices, only 512-byte blocksize is supported. This may be 30753 * changed in near future because some RAID devices require non-512-byte 30754 * blocksize 30755 * 30756 * ----------------------------------------------------------- 30757 * removable media hotpluggable | non-512-byte blocksize 30758 * ----------------------------------------------------------- 30759 * false false | No 30760 * false true | No 30761 * true x | Yes 30762 * ----------------------------------------------------------- 30763 * 30764 * 30765 * 6. Automatic mount & unmount (i.e. vold) 30766 * 30767 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30768 * if a device is removable media device. It return 1 for removable media 30769 * devices, and 0 for others. 30770 * 30771 * Vold treats a device as removable one only if DKIOREMOVABLE returns 1. 30772 * And it does automounting only for removable media devices. In order to 30773 * preserve users' experience and let vold continue to do automounting for 30774 * USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk 30775 * devices. 30776 * 30777 * ------------------------------------------------------ 30778 * removable media hotpluggable | automatic mount 30779 * ------------------------------------------------------ 30780 * false false | No 30781 * false true | Yes 30782 * true x | Yes 30783 * ------------------------------------------------------ 30784 * 30785 * 30786 * 7. fdisk partition management 30787 * 30788 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30789 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30790 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30791 * fdisk partitions on both x86 and SPARC platform. 30792 * 30793 * ----------------------------------------------------------- 30794 * platform removable media USB/1394 | fdisk supported 30795 * ----------------------------------------------------------- 30796 * x86 X X | true 30797 * ------------------------------------------------------------ 30798 * sparc X X | false 30799 * ------------------------------------------------------------ 30800 * 30801 * 30802 * 8. MBOOT/MBR 30803 * 30804 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30805 * read/write mboot for removable media devices on sparc platform. 30806 * 30807 * ----------------------------------------------------------- 30808 * platform removable media USB/1394 | mboot supported 30809 * ----------------------------------------------------------- 30810 * x86 X X | true 30811 * ------------------------------------------------------------ 30812 * sparc false false | false 30813 * sparc false true | true 30814 * sparc true false | true 30815 * sparc true true | true 30816 * ------------------------------------------------------------ 30817 * 30818 * 30819 * 9. error handling during opening device 30820 * 30821 * If failed to open a disk device, an errno is returned. For some kinds 30822 * of errors, different errno is returned depending on if this device is 30823 * a removable media device. This brings USB/1394 hard disks in line with 30824 * expected hard disk behavior. It is not expected that this breaks any 30825 * application. 30826 * 30827 * ------------------------------------------------------ 30828 * removable media hotpluggable | errno 30829 * ------------------------------------------------------ 30830 * false false | EIO 30831 * false true | EIO 30832 * true x | ENXIO 30833 * ------------------------------------------------------ 30834 * 30835 * 30836 * 10. off-by-1 workaround (bug 1175930, and 4996920) (x86 only) 30837 * 30838 * [ this is a bit of very ugly history, soon to be removed ] 30839 * 30840 * SCSI READ_CAPACITY command returns the last valid logical block number 30841 * which starts from 0. So real capacity is larger than the returned 30842 * value by 1. However, because scdk.c (which was EOL'ed) directly used 30843 * the logical block number as capacity of disk devices, off-by-1 work- 30844 * around was applied. This workaround causes fixed SCSI disk to loss a 30845 * sector on x86 platform, and precludes exchanging fixed hard disks 30846 * between sparc and x86. 30847 * 30848 * ------------------------------------------------------ 30849 * removable media hotplug | Off-by-1 works 30850 * ------------------------------------------------------- 30851 * false false | Yes 30852 * false true | No 30853 * true false | No 30854 * true true | No 30855 * ------------------------------------------------------ 30856 * 30857 * 30858 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30859 * 30860 * These IOCTLs are applicable only to removable media devices. 30861 * 30862 * ----------------------------------------------------------- 30863 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30864 * ----------------------------------------------------------- 30865 * false false | No 30866 * false true | No 30867 * true x | Yes 30868 * ----------------------------------------------------------- 30869 * 30870 * 30871 * 12. Kstats for partitions 30872 * 30873 * sd creates partition kstat for non-removable media devices. USB and 30874 * Firewire hard disks now have partition kstats 30875 * 30876 * ------------------------------------------------------ 30877 * removable media hotplugable | kstat 30878 * ------------------------------------------------------ 30879 * false false | Yes 30880 * false true | Yes 30881 * true x | No 30882 * ------------------------------------------------------ 30883 * 30884 * 30885 * 13. Removable media & hotpluggable properties 30886 * 30887 * Sd driver creates a "removable-media" property for removable media 30888 * devices. Parent nexus drivers create a "hotpluggable" property if 30889 * it supports hotplugging. 30890 * 30891 * --------------------------------------------------------------------- 30892 * removable media hotpluggable | "removable-media" " hotpluggable" 30893 * --------------------------------------------------------------------- 30894 * false false | No No 30895 * false true | No Yes 30896 * true false | Yes No 30897 * true true | Yes Yes 30898 * --------------------------------------------------------------------- 30899 * 30900 * 30901 * 14. Power Management 30902 * 30903 * sd only power manages removable media devices or devices that support 30904 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30905 * 30906 * A parent nexus that supports hotplugging can also set "pm-capable" 30907 * if the disk can be power managed. 30908 * 30909 * ------------------------------------------------------------ 30910 * removable media hotpluggable pm-capable | power manage 30911 * ------------------------------------------------------------ 30912 * false false false | No 30913 * false false true | Yes 30914 * false true false | No 30915 * false true true | Yes 30916 * true x x | Yes 30917 * ------------------------------------------------------------ 30918 * 30919 * USB and firewire hard disks can now be power managed independently 30920 * of the framebuffer 30921 * 30922 * 30923 * 15. Support for USB disks with capacity larger than 1TB 30924 * 30925 * Currently, sd doesn't permit a fixed disk device with capacity 30926 * larger than 1TB to be used in a 32-bit operating system environment. 30927 * However, sd doesn't do that for removable media devices. Instead, it 30928 * assumes that removable media devices cannot have a capacity larger 30929 * than 1TB. Therefore, using those devices on 32-bit system is partially 30930 * supported, which can cause some unexpected results. 30931 * 30932 * --------------------------------------------------------------------- 30933 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30934 * --------------------------------------------------------------------- 30935 * false false | true | no 30936 * false true | true | no 30937 * true false | true | Yes 30938 * true true | true | Yes 30939 * --------------------------------------------------------------------- 30940 * 30941 * 30942 * 16. Check write-protection at open time 30943 * 30944 * When a removable media device is being opened for writing without NDELAY 30945 * flag, sd will check if this device is writable. If attempting to open 30946 * without NDELAY flag a write-protected device, this operation will abort. 30947 * 30948 * ------------------------------------------------------------ 30949 * removable media USB/1394 | WP Check 30950 * ------------------------------------------------------------ 30951 * false false | No 30952 * false true | No 30953 * true false | Yes 30954 * true true | Yes 30955 * ------------------------------------------------------------ 30956 * 30957 * 30958 * 17. syslog when corrupted VTOC is encountered 30959 * 30960 * Currently, if an invalid VTOC is encountered, sd only print syslog 30961 * for fixed SCSI disks. 30962 * ------------------------------------------------------------ 30963 * removable media USB/1394 | print syslog 30964 * ------------------------------------------------------------ 30965 * false false | Yes 30966 * false true | No 30967 * true false | No 30968 * true true | No 30969 * ------------------------------------------------------------ 30970 */ 30971 static void 30972 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30973 { 30974 int pm_capable_prop; 30975 30976 ASSERT(un->un_sd); 30977 ASSERT(un->un_sd->sd_inq); 30978 30979 #if defined(_SUNOS_VTOC_16) 30980 /* 30981 * For VTOC_16 devices, the default label will be created for all 30982 * devices. (see sd_build_default_label) 30983 */ 30984 un->un_f_default_vtoc_supported = TRUE; 30985 #endif 30986 30987 if (un->un_sd->sd_inq->inq_rmb) { 30988 /* 30989 * The media of this device is removable. And for this kind 30990 * of devices, it is possible to change medium after openning 30991 * devices. Thus we should support this operation. 30992 */ 30993 un->un_f_has_removable_media = TRUE; 30994 30995 #if defined(_SUNOS_VTOC_8) 30996 /* 30997 * Note: currently, for VTOC_8 devices, default label is 30998 * created for removable and hotpluggable devices only. 30999 */ 31000 un->un_f_default_vtoc_supported = TRUE; 31001 #endif 31002 /* 31003 * support non-512-byte blocksize of removable media devices 31004 */ 31005 un->un_f_non_devbsize_supported = TRUE; 31006 31007 /* 31008 * Assume that all removable media devices support DOOR_LOCK 31009 */ 31010 un->un_f_doorlock_supported = TRUE; 31011 31012 /* 31013 * For a removable media device, it is possible to be opened 31014 * with NDELAY flag when there is no media in drive, in this 31015 * case we don't care if device is writable. But if without 31016 * NDELAY flag, we need to check if media is write-protected. 31017 */ 31018 un->un_f_chk_wp_open = TRUE; 31019 31020 /* 31021 * need to start a SCSI watch thread to monitor media state, 31022 * when media is being inserted or ejected, notify syseventd. 31023 */ 31024 un->un_f_monitor_media_state = TRUE; 31025 31026 /* 31027 * Some devices don't support START_STOP_UNIT command. 31028 * Therefore, we'd better check if a device supports it 31029 * before sending it. 31030 */ 31031 un->un_f_check_start_stop = TRUE; 31032 31033 /* 31034 * support eject media ioctl: 31035 * FDEJECT, DKIOCEJECT, CDROMEJECT 31036 */ 31037 un->un_f_eject_media_supported = TRUE; 31038 31039 /* 31040 * Because many removable-media devices don't support 31041 * LOG_SENSE, we couldn't use this command to check if 31042 * a removable media device support power-management. 31043 * We assume that they support power-management via 31044 * START_STOP_UNIT command and can be spun up and down 31045 * without limitations. 31046 */ 31047 un->un_f_pm_supported = TRUE; 31048 31049 /* 31050 * Need to create a zero length (Boolean) property 31051 * removable-media for the removable media devices. 31052 * Note that the return value of the property is not being 31053 * checked, since if unable to create the property 31054 * then do not want the attach to fail altogether. Consistent 31055 * with other property creation in attach. 31056 */ 31057 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31058 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31059 31060 } else { 31061 /* 31062 * create device ID for device 31063 */ 31064 un->un_f_devid_supported = TRUE; 31065 31066 /* 31067 * Spin up non-removable-media devices once it is attached 31068 */ 31069 un->un_f_attach_spinup = TRUE; 31070 31071 /* 31072 * According to SCSI specification, Sense data has two kinds of 31073 * format: fixed format, and descriptor format. At present, we 31074 * don't support descriptor format sense data for removable 31075 * media. 31076 */ 31077 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31078 un->un_f_descr_format_supported = TRUE; 31079 } 31080 31081 /* 31082 * kstats are created only for non-removable media devices. 31083 * 31084 * Set this in sd.conf to 0 in order to disable kstats. The 31085 * default is 1, so they are enabled by default. 31086 */ 31087 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31088 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31089 "enable-partition-kstats", 1)); 31090 31091 /* 31092 * Check if HBA has set the "pm-capable" property. 31093 * If "pm-capable" exists and is non-zero then we can 31094 * power manage the device without checking the start/stop 31095 * cycle count log sense page. 31096 * 31097 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31098 * then we should not power manage the device. 31099 * 31100 * If "pm-capable" doesn't exist then pm_capable_prop will 31101 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31102 * sd will check the start/stop cycle count log sense page 31103 * and power manage the device if the cycle count limit has 31104 * not been exceeded. 31105 */ 31106 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31107 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31108 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31109 un->un_f_log_sense_supported = TRUE; 31110 } else { 31111 /* 31112 * pm-capable property exists. 31113 * 31114 * Convert "TRUE" values for pm_capable_prop to 31115 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31116 * later. "TRUE" values are any values except 31117 * SD_PM_CAPABLE_FALSE (0) and 31118 * SD_PM_CAPABLE_UNDEFINED (-1) 31119 */ 31120 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31121 un->un_f_log_sense_supported = FALSE; 31122 } else { 31123 un->un_f_pm_supported = TRUE; 31124 } 31125 31126 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31127 "sd_unit_attach: un:0x%p pm-capable " 31128 "property set to %d.\n", un, un->un_f_pm_supported); 31129 } 31130 } 31131 31132 if (un->un_f_is_hotpluggable) { 31133 #if defined(_SUNOS_VTOC_8) 31134 /* 31135 * Note: currently, for VTOC_8 devices, default label is 31136 * created for removable and hotpluggable devices only. 31137 */ 31138 un->un_f_default_vtoc_supported = TRUE; 31139 #endif 31140 31141 /* 31142 * Temporarily, let hotpluggable devices pretend to be 31143 * removable-media devices for vold. 31144 */ 31145 un->un_f_monitor_media_state = TRUE; 31146 31147 un->un_f_check_start_stop = TRUE; 31148 31149 } 31150 31151 /* 31152 * By default, only DIRECT ACCESS devices and CDs will have Sun 31153 * labels. 31154 */ 31155 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31156 (un->un_sd->sd_inq->inq_rmb)) { 31157 /* 31158 * Direct access devices have disk label 31159 */ 31160 un->un_f_vtoc_label_supported = TRUE; 31161 } 31162 31163 /* 31164 * Fdisk partitions are supported for all direct access devices on 31165 * x86 platform, and just for removable media and hotpluggable 31166 * devices on SPARC platform. Later, we will set the following flag 31167 * to FALSE if current device is not removable media or hotpluggable 31168 * device and if sd works on SAPRC platform. 31169 */ 31170 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31171 un->un_f_mboot_supported = TRUE; 31172 } 31173 31174 if (!un->un_f_is_hotpluggable && 31175 !un->un_sd->sd_inq->inq_rmb) { 31176 31177 #if defined(_SUNOS_VTOC_8) 31178 /* 31179 * Don't support fdisk on fixed disk 31180 */ 31181 un->un_f_mboot_supported = FALSE; 31182 #endif 31183 31184 /* 31185 * Fixed disk support SYNC CACHE 31186 */ 31187 un->un_f_sync_cache_supported = TRUE; 31188 31189 /* 31190 * For fixed disk, if its VTOC is not valid, we will write 31191 * errlog into system log 31192 */ 31193 if (un->un_f_vtoc_label_supported) 31194 un->un_f_vtoc_errlog_supported = TRUE; 31195 } 31196 } 31197