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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * SCSI disk target driver. 31 */ 32 33 34 35 36 #include <sys/scsi/scsi.h> 37 #include <sys/dkbad.h> 38 #include <sys/dklabel.h> 39 #include <sys/dkio.h> 40 #include <sys/fdio.h> 41 #include <sys/cdio.h> 42 #include <sys/mhd.h> 43 #include <sys/vtoc.h> 44 #include <sys/dktp/fdisk.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <sys/kstat.h> 48 #include <sys/vtrace.h> 49 #include <sys/note.h> 50 #include <sys/thread.h> 51 #include <sys/proc.h> 52 #include <sys/efi_partition.h> 53 #include <sys/var.h> 54 #include <sys/aio_req.h> 55 56 #ifdef __lock_lint 57 #define _LP64 58 #define __amd64 59 #endif 60 61 #if (defined(__fibre)) 62 /* Note: is there a leadville version of the following? */ 63 #include <sys/fc4/fcal_linkapp.h> 64 #endif 65 #include <sys/taskq.h> 66 #include <sys/uuid.h> 67 #include <sys/byteorder.h> 68 #include <sys/sdt.h> 69 70 #include "sd_xbuf.h" 71 72 #include <sys/scsi/targets/sddef.h> 73 74 75 /* 76 * Loadable module info. 77 */ 78 #if (defined(__fibre)) 79 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 80 char _depends_on[] = "misc/scsi drv/fcp"; 81 #else 82 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 83 char _depends_on[] = "misc/scsi"; 84 #endif 85 86 /* 87 * Define the interconnect type, to allow the driver to distinguish 88 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 89 * 90 * This is really for backward compatability. In the future, the driver 91 * should actually check the "interconnect-type" property as reported by 92 * the HBA; however at present this property is not defined by all HBAs, 93 * so we will use this #define (1) to permit the driver to run in 94 * backward-compatability mode; and (2) to print a notification message 95 * if an FC HBA does not support the "interconnect-type" property. The 96 * behavior of the driver will be to assume parallel SCSI behaviors unless 97 * the "interconnect-type" property is defined by the HBA **AND** has a 98 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 99 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 100 * Channel behaviors (as per the old ssd). (Note that the 101 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 102 * will result in the driver assuming parallel SCSI behaviors.) 103 * 104 * (see common/sys/scsi/impl/services.h) 105 * 106 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 107 * since some FC HBAs may already support that, and there is some code in 108 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 109 * default would confuse that code, and besides things should work fine 110 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 111 * "interconnect_type" property. 112 */ 113 #if (defined(__fibre)) 114 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 115 #else 116 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 117 #endif 118 119 /* 120 * The name of the driver, established from the module name in _init. 121 */ 122 static char *sd_label = NULL; 123 124 /* 125 * Driver name is unfortunately prefixed on some driver.conf properties. 126 */ 127 #if (defined(__fibre)) 128 #define sd_max_xfer_size ssd_max_xfer_size 129 #define sd_config_list ssd_config_list 130 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 131 static char *sd_config_list = "ssd-config-list"; 132 #else 133 static char *sd_max_xfer_size = "sd_max_xfer_size"; 134 static char *sd_config_list = "sd-config-list"; 135 #endif 136 137 /* 138 * Driver global variables 139 */ 140 141 #if (defined(__fibre)) 142 /* 143 * These #defines are to avoid namespace collisions that occur because this 144 * code is currently used to compile two seperate driver modules: sd and ssd. 145 * All global variables need to be treated this way (even if declared static) 146 * in order to allow the debugger to resolve the names properly. 147 * It is anticipated that in the near future the ssd module will be obsoleted, 148 * at which time this namespace issue should go away. 149 */ 150 #define sd_state ssd_state 151 #define sd_io_time ssd_io_time 152 #define sd_failfast_enable ssd_failfast_enable 153 #define sd_ua_retry_count ssd_ua_retry_count 154 #define sd_report_pfa ssd_report_pfa 155 #define sd_max_throttle ssd_max_throttle 156 #define sd_min_throttle ssd_min_throttle 157 #define sd_rot_delay ssd_rot_delay 158 159 #define sd_retry_on_reservation_conflict \ 160 ssd_retry_on_reservation_conflict 161 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 162 #define sd_resv_conflict_name ssd_resv_conflict_name 163 164 #define sd_component_mask ssd_component_mask 165 #define sd_level_mask ssd_level_mask 166 #define sd_debug_un ssd_debug_un 167 #define sd_error_level ssd_error_level 168 169 #define sd_xbuf_active_limit ssd_xbuf_active_limit 170 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 171 172 #define sd_tr ssd_tr 173 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 174 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 175 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 176 #define sd_check_media_time ssd_check_media_time 177 #define sd_wait_cmds_complete ssd_wait_cmds_complete 178 #define sd_label_mutex ssd_label_mutex 179 #define sd_detach_mutex ssd_detach_mutex 180 #define sd_log_buf ssd_log_buf 181 #define sd_log_mutex ssd_log_mutex 182 183 #define sd_disk_table ssd_disk_table 184 #define sd_disk_table_size ssd_disk_table_size 185 #define sd_sense_mutex ssd_sense_mutex 186 #define sd_cdbtab ssd_cdbtab 187 188 #define sd_cb_ops ssd_cb_ops 189 #define sd_ops ssd_ops 190 #define sd_additional_codes ssd_additional_codes 191 192 #define sd_minor_data ssd_minor_data 193 #define sd_minor_data_efi ssd_minor_data_efi 194 195 #define sd_tq ssd_tq 196 #define sd_wmr_tq ssd_wmr_tq 197 #define sd_taskq_name ssd_taskq_name 198 #define sd_wmr_taskq_name ssd_wmr_taskq_name 199 #define sd_taskq_minalloc ssd_taskq_minalloc 200 #define sd_taskq_maxalloc ssd_taskq_maxalloc 201 202 #define sd_dump_format_string ssd_dump_format_string 203 204 #define sd_iostart_chain ssd_iostart_chain 205 #define sd_iodone_chain ssd_iodone_chain 206 207 #define sd_pm_idletime ssd_pm_idletime 208 209 #define sd_force_pm_supported ssd_force_pm_supported 210 211 #define sd_dtype_optical_bind ssd_dtype_optical_bind 212 213 #endif 214 215 216 #ifdef SDDEBUG 217 int sd_force_pm_supported = 0; 218 #endif /* SDDEBUG */ 219 220 void *sd_state = NULL; 221 int sd_io_time = SD_IO_TIME; 222 int sd_failfast_enable = 1; 223 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 224 int sd_report_pfa = 1; 225 int sd_max_throttle = SD_MAX_THROTTLE; 226 int sd_min_throttle = SD_MIN_THROTTLE; 227 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 228 int sd_qfull_throttle_enable = TRUE; 229 230 int sd_retry_on_reservation_conflict = 1; 231 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 232 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 233 234 static int sd_dtype_optical_bind = -1; 235 236 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 237 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 238 239 /* 240 * Global data for debug logging. To enable debug printing, sd_component_mask 241 * and sd_level_mask should be set to the desired bit patterns as outlined in 242 * sddef.h. 243 */ 244 uint_t sd_component_mask = 0x0; 245 uint_t sd_level_mask = 0x0; 246 struct sd_lun *sd_debug_un = NULL; 247 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 248 249 /* Note: these may go away in the future... */ 250 static uint32_t sd_xbuf_active_limit = 512; 251 static uint32_t sd_xbuf_reserve_limit = 16; 252 253 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 254 255 /* 256 * Timer value used to reset the throttle after it has been reduced 257 * (typically in response to TRAN_BUSY or STATUS_QFULL) 258 */ 259 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 260 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 261 262 /* 263 * Interval value associated with the media change scsi watch. 264 */ 265 static int sd_check_media_time = 3000000; 266 267 /* 268 * Wait value used for in progress operations during a DDI_SUSPEND 269 */ 270 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 271 272 /* 273 * sd_label_mutex protects a static buffer used in the disk label 274 * component of the driver 275 */ 276 static kmutex_t sd_label_mutex; 277 278 /* 279 * sd_detach_mutex protects un_layer_count, un_detach_count, and 280 * un_opens_in_progress in the sd_lun structure. 281 */ 282 static kmutex_t sd_detach_mutex; 283 284 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 285 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 286 287 /* 288 * Global buffer and mutex for debug logging 289 */ 290 static char sd_log_buf[1024]; 291 static kmutex_t sd_log_mutex; 292 293 294 /* 295 * "Smart" Probe Caching structs, globals, #defines, etc. 296 * For parallel scsi and non-self-identify device only. 297 */ 298 299 /* 300 * The following resources and routines are implemented to support 301 * "smart" probing, which caches the scsi_probe() results in an array, 302 * in order to help avoid long probe times. 303 */ 304 struct sd_scsi_probe_cache { 305 struct sd_scsi_probe_cache *next; 306 dev_info_t *pdip; 307 int cache[NTARGETS_WIDE]; 308 }; 309 310 static kmutex_t sd_scsi_probe_cache_mutex; 311 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 312 313 /* 314 * Really we only need protection on the head of the linked list, but 315 * better safe than sorry. 316 */ 317 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 318 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 319 320 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 321 sd_scsi_probe_cache_head)) 322 323 324 /* 325 * Vendor specific data name property declarations 326 */ 327 328 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 329 330 static sd_tunables seagate_properties = { 331 SEAGATE_THROTTLE_VALUE, 332 0, 333 0, 334 0, 335 0, 336 0, 337 0, 338 0, 339 0 340 }; 341 342 343 static sd_tunables fujitsu_properties = { 344 FUJITSU_THROTTLE_VALUE, 345 0, 346 0, 347 0, 348 0, 349 0, 350 0, 351 0, 352 0 353 }; 354 355 static sd_tunables ibm_properties = { 356 IBM_THROTTLE_VALUE, 357 0, 358 0, 359 0, 360 0, 361 0, 362 0, 363 0, 364 0 365 }; 366 367 static sd_tunables purple_properties = { 368 PURPLE_THROTTLE_VALUE, 369 0, 370 0, 371 PURPLE_BUSY_RETRIES, 372 PURPLE_RESET_RETRY_COUNT, 373 PURPLE_RESERVE_RELEASE_TIME, 374 0, 375 0, 376 0 377 }; 378 379 static sd_tunables sve_properties = { 380 SVE_THROTTLE_VALUE, 381 0, 382 0, 383 SVE_BUSY_RETRIES, 384 SVE_RESET_RETRY_COUNT, 385 SVE_RESERVE_RELEASE_TIME, 386 SVE_MIN_THROTTLE_VALUE, 387 SVE_DISKSORT_DISABLED_FLAG, 388 0 389 }; 390 391 static sd_tunables maserati_properties = { 392 0, 393 0, 394 0, 395 0, 396 0, 397 0, 398 0, 399 MASERATI_DISKSORT_DISABLED_FLAG, 400 MASERATI_LUN_RESET_ENABLED_FLAG 401 }; 402 403 static sd_tunables pirus_properties = { 404 PIRUS_THROTTLE_VALUE, 405 0, 406 PIRUS_NRR_COUNT, 407 PIRUS_BUSY_RETRIES, 408 PIRUS_RESET_RETRY_COUNT, 409 0, 410 PIRUS_MIN_THROTTLE_VALUE, 411 PIRUS_DISKSORT_DISABLED_FLAG, 412 PIRUS_LUN_RESET_ENABLED_FLAG 413 }; 414 415 #endif 416 417 #if (defined(__sparc) && !defined(__fibre)) || \ 418 (defined(__i386) || defined(__amd64)) 419 420 421 static sd_tunables elite_properties = { 422 ELITE_THROTTLE_VALUE, 423 0, 424 0, 425 0, 426 0, 427 0, 428 0, 429 0, 430 0 431 }; 432 433 static sd_tunables st31200n_properties = { 434 ST31200N_THROTTLE_VALUE, 435 0, 436 0, 437 0, 438 0, 439 0, 440 0, 441 0, 442 0 443 }; 444 445 #endif /* Fibre or not */ 446 447 static sd_tunables lsi_properties_scsi = { 448 LSI_THROTTLE_VALUE, 449 0, 450 LSI_NOTREADY_RETRIES, 451 0, 452 0, 453 0, 454 0, 455 0, 456 0 457 }; 458 459 static sd_tunables symbios_properties = { 460 SYMBIOS_THROTTLE_VALUE, 461 0, 462 SYMBIOS_NOTREADY_RETRIES, 463 0, 464 0, 465 0, 466 0, 467 0, 468 0 469 }; 470 471 static sd_tunables lsi_properties = { 472 0, 473 0, 474 LSI_NOTREADY_RETRIES, 475 0, 476 0, 477 0, 478 0, 479 0, 480 0 481 }; 482 483 static sd_tunables lsi_oem_properties = { 484 0, 485 0, 486 LSI_OEM_NOTREADY_RETRIES, 487 0, 488 0, 489 0, 490 0, 491 0, 492 0 493 }; 494 495 496 497 #if (defined(SD_PROP_TST)) 498 499 #define SD_TST_CTYPE_VAL CTYPE_CDROM 500 #define SD_TST_THROTTLE_VAL 16 501 #define SD_TST_NOTREADY_VAL 12 502 #define SD_TST_BUSY_VAL 60 503 #define SD_TST_RST_RETRY_VAL 36 504 #define SD_TST_RSV_REL_TIME 60 505 506 static sd_tunables tst_properties = { 507 SD_TST_THROTTLE_VAL, 508 SD_TST_CTYPE_VAL, 509 SD_TST_NOTREADY_VAL, 510 SD_TST_BUSY_VAL, 511 SD_TST_RST_RETRY_VAL, 512 SD_TST_RSV_REL_TIME, 513 0, 514 0, 515 0 516 }; 517 #endif 518 519 /* This is similiar to the ANSI toupper implementation */ 520 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 521 522 /* 523 * Static Driver Configuration Table 524 * 525 * This is the table of disks which need throttle adjustment (or, perhaps 526 * something else as defined by the flags at a future time.) device_id 527 * is a string consisting of concatenated vid (vendor), pid (product/model) 528 * and revision strings as defined in the scsi_inquiry structure. Offsets of 529 * the parts of the string are as defined by the sizes in the scsi_inquiry 530 * structure. Device type is searched as far as the device_id string is 531 * defined. Flags defines which values are to be set in the driver from the 532 * properties list. 533 * 534 * Entries below which begin and end with a "*" are a special case. 535 * These do not have a specific vendor, and the string which follows 536 * can appear anywhere in the 16 byte PID portion of the inquiry data. 537 * 538 * Entries below which begin and end with a " " (blank) are a special 539 * case. The comparison function will treat multiple consecutive blanks 540 * as equivalent to a single blank. For example, this causes a 541 * sd_disk_table entry of " NEC CDROM " to match a device's id string 542 * of "NEC CDROM". 543 * 544 * Note: The MD21 controller type has been obsoleted. 545 * ST318202F is a Legacy device 546 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 547 * made with an FC connection. The entries here are a legacy. 548 */ 549 static sd_disk_config_t sd_disk_table[] = { 550 #if defined(__fibre) || defined(__i386) || defined(__amd64) 551 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 552 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 553 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 554 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 555 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 556 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 557 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 558 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 559 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 560 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 561 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 562 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 563 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 564 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 565 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 566 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 567 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 568 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 569 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 570 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 571 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 572 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 573 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 574 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 575 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 576 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 577 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 578 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 579 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 580 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 581 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 582 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 583 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 584 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 585 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 586 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 587 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 588 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 589 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 590 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 591 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 592 { "SUN T3", 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 &purple_properties }, 597 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 598 SD_CONF_BSET_BSY_RETRY_COUNT| 599 SD_CONF_BSET_RST_RETRIES| 600 SD_CONF_BSET_RSV_REL_TIME| 601 SD_CONF_BSET_MIN_THROTTLE| 602 SD_CONF_BSET_DISKSORT_DISABLED, 603 &sve_properties }, 604 { "SUN T4", SD_CONF_BSET_THROTTLE | 605 SD_CONF_BSET_BSY_RETRY_COUNT| 606 SD_CONF_BSET_RST_RETRIES| 607 SD_CONF_BSET_RSV_REL_TIME, 608 &purple_properties }, 609 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 610 SD_CONF_BSET_LUN_RESET_ENABLED, 611 &maserati_properties }, 612 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 613 SD_CONF_BSET_NRR_COUNT| 614 SD_CONF_BSET_BSY_RETRY_COUNT| 615 SD_CONF_BSET_RST_RETRIES| 616 SD_CONF_BSET_MIN_THROTTLE| 617 SD_CONF_BSET_DISKSORT_DISABLED| 618 SD_CONF_BSET_LUN_RESET_ENABLED, 619 &pirus_properties }, 620 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 621 SD_CONF_BSET_NRR_COUNT| 622 SD_CONF_BSET_BSY_RETRY_COUNT| 623 SD_CONF_BSET_RST_RETRIES| 624 SD_CONF_BSET_MIN_THROTTLE| 625 SD_CONF_BSET_DISKSORT_DISABLED| 626 SD_CONF_BSET_LUN_RESET_ENABLED, 627 &pirus_properties }, 628 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 629 SD_CONF_BSET_NRR_COUNT| 630 SD_CONF_BSET_BSY_RETRY_COUNT| 631 SD_CONF_BSET_RST_RETRIES| 632 SD_CONF_BSET_MIN_THROTTLE| 633 SD_CONF_BSET_DISKSORT_DISABLED| 634 SD_CONF_BSET_LUN_RESET_ENABLED, 635 &pirus_properties }, 636 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 637 SD_CONF_BSET_NRR_COUNT| 638 SD_CONF_BSET_BSY_RETRY_COUNT| 639 SD_CONF_BSET_RST_RETRIES| 640 SD_CONF_BSET_MIN_THROTTLE| 641 SD_CONF_BSET_DISKSORT_DISABLED| 642 SD_CONF_BSET_LUN_RESET_ENABLED, 643 &pirus_properties }, 644 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 645 SD_CONF_BSET_NRR_COUNT| 646 SD_CONF_BSET_BSY_RETRY_COUNT| 647 SD_CONF_BSET_RST_RETRIES| 648 SD_CONF_BSET_MIN_THROTTLE| 649 SD_CONF_BSET_DISKSORT_DISABLED| 650 SD_CONF_BSET_LUN_RESET_ENABLED, 651 &pirus_properties }, 652 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 653 SD_CONF_BSET_NRR_COUNT| 654 SD_CONF_BSET_BSY_RETRY_COUNT| 655 SD_CONF_BSET_RST_RETRIES| 656 SD_CONF_BSET_MIN_THROTTLE| 657 SD_CONF_BSET_DISKSORT_DISABLED| 658 SD_CONF_BSET_LUN_RESET_ENABLED, 659 &pirus_properties }, 660 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 661 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 662 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 663 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 664 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 665 #endif /* fibre or NON-sparc platforms */ 666 #if ((defined(__sparc) && !defined(__fibre)) ||\ 667 (defined(__i386) || defined(__amd64))) 668 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 669 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 670 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 671 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 672 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 673 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 674 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 675 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 676 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 677 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 678 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 679 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 680 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 681 &symbios_properties }, 682 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 683 &lsi_properties_scsi }, 684 #if defined(__i386) || defined(__amd64) 685 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 686 | SD_CONF_BSET_READSUB_BCD 687 | SD_CONF_BSET_READ_TOC_ADDR_BCD 688 | SD_CONF_BSET_NO_READ_HEADER 689 | SD_CONF_BSET_READ_CD_XD4), NULL }, 690 691 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 692 | SD_CONF_BSET_READSUB_BCD 693 | SD_CONF_BSET_READ_TOC_ADDR_BCD 694 | SD_CONF_BSET_NO_READ_HEADER 695 | SD_CONF_BSET_READ_CD_XD4), NULL }, 696 #endif /* __i386 || __amd64 */ 697 #endif /* sparc NON-fibre or NON-sparc platforms */ 698 699 #if (defined(SD_PROP_TST)) 700 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 701 | SD_CONF_BSET_CTYPE 702 | SD_CONF_BSET_NRR_COUNT 703 | SD_CONF_BSET_FAB_DEVID 704 | SD_CONF_BSET_NOCACHE 705 | SD_CONF_BSET_BSY_RETRY_COUNT 706 | SD_CONF_BSET_PLAYMSF_BCD 707 | SD_CONF_BSET_READSUB_BCD 708 | SD_CONF_BSET_READ_TOC_TRK_BCD 709 | SD_CONF_BSET_READ_TOC_ADDR_BCD 710 | SD_CONF_BSET_NO_READ_HEADER 711 | SD_CONF_BSET_READ_CD_XD4 712 | SD_CONF_BSET_RST_RETRIES 713 | SD_CONF_BSET_RSV_REL_TIME 714 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 715 #endif 716 }; 717 718 static const int sd_disk_table_size = 719 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 720 721 722 /* 723 * Return codes of sd_uselabel(). 724 */ 725 #define SD_LABEL_IS_VALID 0 726 #define SD_LABEL_IS_INVALID 1 727 728 #define SD_INTERCONNECT_PARALLEL 0 729 #define SD_INTERCONNECT_FABRIC 1 730 #define SD_INTERCONNECT_FIBRE 2 731 #define SD_INTERCONNECT_SSA 3 732 #define SD_IS_PARALLEL_SCSI(un) \ 733 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 734 735 /* 736 * Definitions used by device id registration routines 737 */ 738 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 739 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 740 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 741 #define WD_NODE 7 /* the whole disk minor */ 742 743 static kmutex_t sd_sense_mutex = {0}; 744 745 /* 746 * Macros for updates of the driver state 747 */ 748 #define New_state(un, s) \ 749 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 750 #define Restore_state(un) \ 751 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 752 753 static struct sd_cdbinfo sd_cdbtab[] = { 754 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 755 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 756 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 757 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 758 }; 759 760 /* 761 * Specifies the number of seconds that must have elapsed since the last 762 * cmd. has completed for a device to be declared idle to the PM framework. 763 */ 764 static int sd_pm_idletime = 1; 765 766 /* 767 * Internal function prototypes 768 */ 769 770 #if (defined(__fibre)) 771 /* 772 * These #defines are to avoid namespace collisions that occur because this 773 * code is currently used to compile two seperate driver modules: sd and ssd. 774 * All function names need to be treated this way (even if declared static) 775 * in order to allow the debugger to resolve the names properly. 776 * It is anticipated that in the near future the ssd module will be obsoleted, 777 * at which time this ugliness should go away. 778 */ 779 #define sd_log_trace ssd_log_trace 780 #define sd_log_info ssd_log_info 781 #define sd_log_err ssd_log_err 782 #define sdprobe ssdprobe 783 #define sdinfo ssdinfo 784 #define sd_prop_op ssd_prop_op 785 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 786 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 787 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 788 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 789 #define sd_spin_up_unit ssd_spin_up_unit 790 #define sd_enable_descr_sense ssd_enable_descr_sense 791 #define sd_set_mmc_caps ssd_set_mmc_caps 792 #define sd_read_unit_properties ssd_read_unit_properties 793 #define sd_process_sdconf_file ssd_process_sdconf_file 794 #define sd_process_sdconf_table ssd_process_sdconf_table 795 #define sd_sdconf_id_match ssd_sdconf_id_match 796 #define sd_blank_cmp ssd_blank_cmp 797 #define sd_chk_vers1_data ssd_chk_vers1_data 798 #define sd_set_vers1_properties ssd_set_vers1_properties 799 #define sd_validate_geometry ssd_validate_geometry 800 801 #if defined(_SUNOS_VTOC_16) 802 #define sd_convert_geometry ssd_convert_geometry 803 #endif 804 805 #define sd_resync_geom_caches ssd_resync_geom_caches 806 #define sd_read_fdisk ssd_read_fdisk 807 #define sd_get_physical_geometry ssd_get_physical_geometry 808 #define sd_get_virtual_geometry ssd_get_virtual_geometry 809 #define sd_update_block_info ssd_update_block_info 810 #define sd_swap_efi_gpt ssd_swap_efi_gpt 811 #define sd_swap_efi_gpe ssd_swap_efi_gpe 812 #define sd_validate_efi ssd_validate_efi 813 #define sd_use_efi ssd_use_efi 814 #define sd_uselabel ssd_uselabel 815 #define sd_build_default_label ssd_build_default_label 816 #define sd_has_max_chs_vals ssd_has_max_chs_vals 817 #define sd_inq_fill ssd_inq_fill 818 #define sd_register_devid ssd_register_devid 819 #define sd_get_devid_block ssd_get_devid_block 820 #define sd_get_devid ssd_get_devid 821 #define sd_create_devid ssd_create_devid 822 #define sd_write_deviceid ssd_write_deviceid 823 #define sd_check_vpd_page_support ssd_check_vpd_page_support 824 #define sd_setup_pm ssd_setup_pm 825 #define sd_create_pm_components ssd_create_pm_components 826 #define sd_ddi_suspend ssd_ddi_suspend 827 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 828 #define sd_ddi_resume ssd_ddi_resume 829 #define sd_ddi_pm_resume ssd_ddi_pm_resume 830 #define sdpower ssdpower 831 #define sdattach ssdattach 832 #define sddetach ssddetach 833 #define sd_unit_attach ssd_unit_attach 834 #define sd_unit_detach ssd_unit_detach 835 #define sd_set_unit_attributes ssd_set_unit_attributes 836 #define sd_create_minor_nodes ssd_create_minor_nodes 837 #define sd_create_errstats ssd_create_errstats 838 #define sd_set_errstats ssd_set_errstats 839 #define sd_set_pstats ssd_set_pstats 840 #define sddump ssddump 841 #define sd_scsi_poll ssd_scsi_poll 842 #define sd_send_polled_RQS ssd_send_polled_RQS 843 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 844 #define sd_init_event_callbacks ssd_init_event_callbacks 845 #define sd_event_callback ssd_event_callback 846 #define sd_disable_caching ssd_disable_caching 847 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 848 #define sd_make_device ssd_make_device 849 #define sdopen ssdopen 850 #define sdclose ssdclose 851 #define sd_ready_and_valid ssd_ready_and_valid 852 #define sdmin ssdmin 853 #define sdread ssdread 854 #define sdwrite ssdwrite 855 #define sdaread ssdaread 856 #define sdawrite ssdawrite 857 #define sdstrategy ssdstrategy 858 #define sdioctl ssdioctl 859 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 860 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 861 #define sd_checksum_iostart ssd_checksum_iostart 862 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 863 #define sd_pm_iostart ssd_pm_iostart 864 #define sd_core_iostart ssd_core_iostart 865 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 866 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 867 #define sd_checksum_iodone ssd_checksum_iodone 868 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 869 #define sd_pm_iodone ssd_pm_iodone 870 #define sd_initpkt_for_buf ssd_initpkt_for_buf 871 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 872 #define sd_setup_rw_pkt ssd_setup_rw_pkt 873 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 874 #define sd_buf_iodone ssd_buf_iodone 875 #define sd_uscsi_strategy ssd_uscsi_strategy 876 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 877 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 878 #define sd_uscsi_iodone ssd_uscsi_iodone 879 #define sd_xbuf_strategy ssd_xbuf_strategy 880 #define sd_xbuf_init ssd_xbuf_init 881 #define sd_pm_entry ssd_pm_entry 882 #define sd_pm_exit ssd_pm_exit 883 884 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 885 #define sd_pm_timeout_handler ssd_pm_timeout_handler 886 887 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 888 #define sdintr ssdintr 889 #define sd_start_cmds ssd_start_cmds 890 #define sd_send_scsi_cmd ssd_send_scsi_cmd 891 #define sd_bioclone_alloc ssd_bioclone_alloc 892 #define sd_bioclone_free ssd_bioclone_free 893 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 894 #define sd_shadow_buf_free ssd_shadow_buf_free 895 #define sd_print_transport_rejected_message \ 896 ssd_print_transport_rejected_message 897 #define sd_retry_command ssd_retry_command 898 #define sd_set_retry_bp ssd_set_retry_bp 899 #define sd_send_request_sense_command ssd_send_request_sense_command 900 #define sd_start_retry_command ssd_start_retry_command 901 #define sd_start_direct_priority_command \ 902 ssd_start_direct_priority_command 903 #define sd_return_failed_command ssd_return_failed_command 904 #define sd_return_failed_command_no_restart \ 905 ssd_return_failed_command_no_restart 906 #define sd_return_command ssd_return_command 907 #define sd_sync_with_callback ssd_sync_with_callback 908 #define sdrunout ssdrunout 909 #define sd_mark_rqs_busy ssd_mark_rqs_busy 910 #define sd_mark_rqs_idle ssd_mark_rqs_idle 911 #define sd_reduce_throttle ssd_reduce_throttle 912 #define sd_restore_throttle ssd_restore_throttle 913 #define sd_print_incomplete_msg ssd_print_incomplete_msg 914 #define sd_init_cdb_limits ssd_init_cdb_limits 915 #define sd_pkt_status_good ssd_pkt_status_good 916 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 917 #define sd_pkt_status_busy ssd_pkt_status_busy 918 #define sd_pkt_status_reservation_conflict \ 919 ssd_pkt_status_reservation_conflict 920 #define sd_pkt_status_qfull ssd_pkt_status_qfull 921 #define sd_handle_request_sense ssd_handle_request_sense 922 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 923 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 924 #define sd_validate_sense_data ssd_validate_sense_data 925 #define sd_decode_sense ssd_decode_sense 926 #define sd_print_sense_msg ssd_print_sense_msg 927 #define sd_extract_sense_info_descr ssd_extract_sense_info_descr 928 #define sd_sense_key_no_sense ssd_sense_key_no_sense 929 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 930 #define sd_sense_key_not_ready ssd_sense_key_not_ready 931 #define sd_sense_key_medium_or_hardware_error \ 932 ssd_sense_key_medium_or_hardware_error 933 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 934 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 935 #define sd_sense_key_fail_command ssd_sense_key_fail_command 936 #define sd_sense_key_blank_check ssd_sense_key_blank_check 937 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 938 #define sd_sense_key_default ssd_sense_key_default 939 #define sd_print_retry_msg ssd_print_retry_msg 940 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 941 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 942 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 943 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 944 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 945 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 946 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 947 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 948 #define sd_pkt_reason_default ssd_pkt_reason_default 949 #define sd_reset_target ssd_reset_target 950 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 951 #define sd_start_stop_unit_task ssd_start_stop_unit_task 952 #define sd_taskq_create ssd_taskq_create 953 #define sd_taskq_delete ssd_taskq_delete 954 #define sd_media_change_task ssd_media_change_task 955 #define sd_handle_mchange ssd_handle_mchange 956 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 957 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 958 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 959 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 960 #define sd_send_scsi_feature_GET_CONFIGURATION \ 961 sd_send_scsi_feature_GET_CONFIGURATION 962 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 963 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 964 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 965 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 966 ssd_send_scsi_PERSISTENT_RESERVE_IN 967 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 968 ssd_send_scsi_PERSISTENT_RESERVE_OUT 969 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 970 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 971 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 972 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 973 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 974 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 975 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 976 #define sd_alloc_rqs ssd_alloc_rqs 977 #define sd_free_rqs ssd_free_rqs 978 #define sd_dump_memory ssd_dump_memory 979 #define sd_uscsi_ioctl ssd_uscsi_ioctl 980 #define sd_get_media_info ssd_get_media_info 981 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 982 #define sd_dkio_get_geometry ssd_dkio_get_geometry 983 #define sd_dkio_set_geometry ssd_dkio_set_geometry 984 #define sd_dkio_get_partition ssd_dkio_get_partition 985 #define sd_dkio_set_partition ssd_dkio_set_partition 986 #define sd_dkio_partition ssd_dkio_partition 987 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 988 #define sd_dkio_get_efi ssd_dkio_get_efi 989 #define sd_build_user_vtoc ssd_build_user_vtoc 990 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 991 #define sd_dkio_set_efi ssd_dkio_set_efi 992 #define sd_build_label_vtoc ssd_build_label_vtoc 993 #define sd_write_label ssd_write_label 994 #define sd_clear_vtoc ssd_clear_vtoc 995 #define sd_clear_efi ssd_clear_efi 996 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 997 #define sd_setup_next_xfer ssd_setup_next_xfer 998 #define sd_dkio_get_temp ssd_dkio_get_temp 999 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1000 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1001 #define sd_setup_default_geometry ssd_setup_default_geometry 1002 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1003 #define sd_check_mhd ssd_check_mhd 1004 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1005 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1006 #define sd_sname ssd_sname 1007 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1008 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1009 #define sd_take_ownership ssd_take_ownership 1010 #define sd_reserve_release ssd_reserve_release 1011 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1012 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1013 #define sd_persistent_reservation_in_read_keys \ 1014 ssd_persistent_reservation_in_read_keys 1015 #define sd_persistent_reservation_in_read_resv \ 1016 ssd_persistent_reservation_in_read_resv 1017 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1018 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1019 #define sd_mhdioc_release ssd_mhdioc_release 1020 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1021 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1022 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1023 #define sr_change_blkmode ssr_change_blkmode 1024 #define sr_change_speed ssr_change_speed 1025 #define sr_atapi_change_speed ssr_atapi_change_speed 1026 #define sr_pause_resume ssr_pause_resume 1027 #define sr_play_msf ssr_play_msf 1028 #define sr_play_trkind ssr_play_trkind 1029 #define sr_read_all_subcodes ssr_read_all_subcodes 1030 #define sr_read_subchannel ssr_read_subchannel 1031 #define sr_read_tocentry ssr_read_tocentry 1032 #define sr_read_tochdr ssr_read_tochdr 1033 #define sr_read_cdda ssr_read_cdda 1034 #define sr_read_cdxa ssr_read_cdxa 1035 #define sr_read_mode1 ssr_read_mode1 1036 #define sr_read_mode2 ssr_read_mode2 1037 #define sr_read_cd_mode2 ssr_read_cd_mode2 1038 #define sr_sector_mode ssr_sector_mode 1039 #define sr_eject ssr_eject 1040 #define sr_ejected ssr_ejected 1041 #define sr_check_wp ssr_check_wp 1042 #define sd_check_media ssd_check_media 1043 #define sd_media_watch_cb ssd_media_watch_cb 1044 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1045 #define sr_volume_ctrl ssr_volume_ctrl 1046 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1047 #define sd_log_page_supported ssd_log_page_supported 1048 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1049 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1050 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1051 #define sd_range_lock ssd_range_lock 1052 #define sd_get_range ssd_get_range 1053 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1054 #define sd_range_unlock ssd_range_unlock 1055 #define sd_read_modify_write_task ssd_read_modify_write_task 1056 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1057 1058 #define sd_iostart_chain ssd_iostart_chain 1059 #define sd_iodone_chain ssd_iodone_chain 1060 #define sd_initpkt_map ssd_initpkt_map 1061 #define sd_destroypkt_map ssd_destroypkt_map 1062 #define sd_chain_type_map ssd_chain_type_map 1063 #define sd_chain_index_map ssd_chain_index_map 1064 1065 #define sd_failfast_flushctl ssd_failfast_flushctl 1066 #define sd_failfast_flushq ssd_failfast_flushq 1067 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1068 1069 #define sd_is_lsi ssd_is_lsi 1070 1071 #endif /* #if (defined(__fibre)) */ 1072 1073 1074 int _init(void); 1075 int _fini(void); 1076 int _info(struct modinfo *modinfop); 1077 1078 /*PRINTFLIKE3*/ 1079 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1080 /*PRINTFLIKE3*/ 1081 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1082 /*PRINTFLIKE3*/ 1083 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1084 1085 static int sdprobe(dev_info_t *devi); 1086 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1087 void **result); 1088 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1089 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1090 1091 /* 1092 * Smart probe for parallel scsi 1093 */ 1094 static void sd_scsi_probe_cache_init(void); 1095 static void sd_scsi_probe_cache_fini(void); 1096 static void sd_scsi_clear_probe_cache(void); 1097 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1098 1099 static int sd_spin_up_unit(struct sd_lun *un); 1100 #ifdef _LP64 1101 static void sd_enable_descr_sense(struct sd_lun *un); 1102 #endif /* _LP64 */ 1103 static void sd_set_mmc_caps(struct sd_lun *un); 1104 1105 static void sd_read_unit_properties(struct sd_lun *un); 1106 static int sd_process_sdconf_file(struct sd_lun *un); 1107 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1108 int *data_list, sd_tunables *values); 1109 static void sd_process_sdconf_table(struct sd_lun *un); 1110 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1111 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1112 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1113 int list_len, char *dataname_ptr); 1114 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1115 sd_tunables *prop_list); 1116 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1117 1118 #if defined(_SUNOS_VTOC_16) 1119 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1120 #endif 1121 1122 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1123 int path_flag); 1124 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1125 int path_flag); 1126 static void sd_get_physical_geometry(struct sd_lun *un, 1127 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1128 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1129 int lbasize); 1130 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1131 static void sd_swap_efi_gpt(efi_gpt_t *); 1132 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1133 static int sd_validate_efi(efi_gpt_t *); 1134 static int sd_use_efi(struct sd_lun *, int); 1135 static void sd_build_default_label(struct sd_lun *un); 1136 1137 #if defined(_FIRMWARE_NEEDS_FDISK) 1138 static int sd_has_max_chs_vals(struct ipart *fdp); 1139 #endif 1140 static void sd_inq_fill(char *p, int l, char *s); 1141 1142 1143 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1144 int reservation_flag); 1145 static daddr_t sd_get_devid_block(struct sd_lun *un); 1146 static int sd_get_devid(struct sd_lun *un); 1147 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1148 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1149 static int sd_write_deviceid(struct sd_lun *un); 1150 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1151 static int sd_check_vpd_page_support(struct sd_lun *un); 1152 1153 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1154 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1155 1156 static int sd_ddi_suspend(dev_info_t *devi); 1157 static int sd_ddi_pm_suspend(struct sd_lun *un); 1158 static int sd_ddi_resume(dev_info_t *devi); 1159 static int sd_ddi_pm_resume(struct sd_lun *un); 1160 static int sdpower(dev_info_t *devi, int component, int level); 1161 1162 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1163 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1164 static int sd_unit_attach(dev_info_t *devi); 1165 static int sd_unit_detach(dev_info_t *devi); 1166 1167 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1168 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1169 static void sd_create_errstats(struct sd_lun *un, int instance); 1170 static void sd_set_errstats(struct sd_lun *un); 1171 static void sd_set_pstats(struct sd_lun *un); 1172 1173 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1174 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1175 static int sd_send_polled_RQS(struct sd_lun *un); 1176 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1177 1178 #if (defined(__fibre)) 1179 /* 1180 * Event callbacks (photon) 1181 */ 1182 static void sd_init_event_callbacks(struct sd_lun *un); 1183 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1184 #endif 1185 1186 1187 static int sd_disable_caching(struct sd_lun *un); 1188 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1189 static dev_t sd_make_device(dev_info_t *devi); 1190 1191 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1192 uint64_t capacity); 1193 1194 /* 1195 * Driver entry point functions. 1196 */ 1197 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1198 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1199 static int sd_ready_and_valid(struct sd_lun *un); 1200 1201 static void sdmin(struct buf *bp); 1202 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1203 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1204 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1205 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1206 1207 static int sdstrategy(struct buf *bp); 1208 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1209 1210 /* 1211 * Function prototypes for layering functions in the iostart chain. 1212 */ 1213 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1214 struct buf *bp); 1215 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1216 struct buf *bp); 1217 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1218 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1219 struct buf *bp); 1220 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1221 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1222 1223 /* 1224 * Function prototypes for layering functions in the iodone chain. 1225 */ 1226 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1227 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1228 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1229 struct buf *bp); 1230 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1231 struct buf *bp); 1232 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1233 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1234 struct buf *bp); 1235 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1236 1237 /* 1238 * Prototypes for functions to support buf(9S) based IO. 1239 */ 1240 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1241 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1242 static void sd_destroypkt_for_buf(struct buf *); 1243 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1244 struct buf *bp, int flags, 1245 int (*callback)(caddr_t), caddr_t callback_arg, 1246 diskaddr_t lba, uint32_t blockcount); 1247 #if defined(__i386) || defined(__amd64) 1248 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1249 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1250 #endif /* defined(__i386) || defined(__amd64) */ 1251 1252 /* 1253 * Prototypes for functions to support USCSI IO. 1254 */ 1255 static int sd_uscsi_strategy(struct buf *bp); 1256 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1257 static void sd_destroypkt_for_uscsi(struct buf *); 1258 1259 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1260 uchar_t chain_type, void *pktinfop); 1261 1262 static int sd_pm_entry(struct sd_lun *un); 1263 static void sd_pm_exit(struct sd_lun *un); 1264 1265 static void sd_pm_idletimeout_handler(void *arg); 1266 1267 /* 1268 * sd_core internal functions (used at the sd_core_io layer). 1269 */ 1270 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1271 static void sdintr(struct scsi_pkt *pktp); 1272 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1273 1274 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1275 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1276 int path_flag); 1277 1278 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1279 daddr_t blkno, int (*func)(struct buf *)); 1280 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1281 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1282 static void sd_bioclone_free(struct buf *bp); 1283 static void sd_shadow_buf_free(struct buf *bp); 1284 1285 static void sd_print_transport_rejected_message(struct sd_lun *un, 1286 struct sd_xbuf *xp, int code); 1287 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1288 void *arg, int code); 1289 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1290 void *arg, int code); 1291 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1292 void *arg, int code); 1293 1294 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1295 int retry_check_flag, 1296 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1297 int c), 1298 void *user_arg, int failure_code, clock_t retry_delay, 1299 void (*statp)(kstat_io_t *)); 1300 1301 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1302 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1303 1304 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1305 struct scsi_pkt *pktp); 1306 static void sd_start_retry_command(void *arg); 1307 static void sd_start_direct_priority_command(void *arg); 1308 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1309 int errcode); 1310 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1311 struct buf *bp, int errcode); 1312 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1313 static void sd_sync_with_callback(struct sd_lun *un); 1314 static int sdrunout(caddr_t arg); 1315 1316 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1317 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1318 1319 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1320 static void sd_restore_throttle(void *arg); 1321 1322 static void sd_init_cdb_limits(struct sd_lun *un); 1323 1324 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1325 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1326 1327 /* 1328 * Error handling functions 1329 */ 1330 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1331 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1332 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1333 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1334 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1335 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1336 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1337 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1338 1339 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1340 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1341 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1342 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1343 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1344 struct sd_xbuf *xp); 1345 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1346 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1347 1348 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1349 void *arg, int code); 1350 static diskaddr_t sd_extract_sense_info_descr( 1351 struct scsi_descr_sense_hdr *sdsp); 1352 1353 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1354 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1355 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1356 uint8_t asc, 1357 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1358 static void sd_sense_key_not_ready(struct sd_lun *un, 1359 uint8_t asc, uint8_t ascq, 1360 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1361 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1362 int sense_key, uint8_t asc, 1363 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1364 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1365 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1366 static void sd_sense_key_unit_attention(struct sd_lun *un, 1367 uint8_t asc, 1368 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1369 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1370 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1371 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1372 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1373 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1374 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1375 static void sd_sense_key_default(struct sd_lun *un, 1376 int sense_key, 1377 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1378 1379 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1380 void *arg, int flag); 1381 1382 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1383 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1384 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1385 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1386 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1387 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1388 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1389 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1390 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1391 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1392 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1393 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1394 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1395 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1396 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1397 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1398 1399 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1400 1401 static void sd_start_stop_unit_callback(void *arg); 1402 static void sd_start_stop_unit_task(void *arg); 1403 1404 static void sd_taskq_create(void); 1405 static void sd_taskq_delete(void); 1406 static void sd_media_change_task(void *arg); 1407 1408 static int sd_handle_mchange(struct sd_lun *un); 1409 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1410 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1411 uint32_t *lbap, int path_flag); 1412 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1413 uint32_t *lbap, int path_flag); 1414 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1415 int path_flag); 1416 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1417 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1418 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1419 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1420 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1421 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1422 uchar_t usr_cmd, uchar_t *usr_bufp); 1423 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1424 struct dk_callback *dkc); 1425 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1426 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1427 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1428 uchar_t *bufaddr, uint_t buflen); 1429 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1430 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1431 uchar_t *bufaddr, uint_t buflen, char feature); 1432 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1433 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1434 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1435 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1436 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1437 size_t buflen, daddr_t start_block, int path_flag); 1438 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1439 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1440 path_flag) 1441 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1442 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1443 path_flag) 1444 1445 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1446 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1447 uint16_t param_ptr, int path_flag); 1448 1449 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1450 static void sd_free_rqs(struct sd_lun *un); 1451 1452 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1453 uchar_t *data, int len, int fmt); 1454 static void sd_panic_for_res_conflict(struct sd_lun *un); 1455 1456 /* 1457 * Disk Ioctl Function Prototypes 1458 */ 1459 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1460 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1461 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1462 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1463 int geom_validated); 1464 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1465 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1466 int geom_validated); 1467 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1468 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1469 int geom_validated); 1470 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1471 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1472 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1473 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1474 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1475 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1476 static int sd_write_label(dev_t dev); 1477 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1478 static void sd_clear_vtoc(struct sd_lun *un); 1479 static void sd_clear_efi(struct sd_lun *un); 1480 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1481 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1482 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1483 static void sd_setup_default_geometry(struct sd_lun *un); 1484 #if defined(__i386) || defined(__amd64) 1485 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1486 #endif 1487 1488 /* 1489 * Multi-host Ioctl Prototypes 1490 */ 1491 static int sd_check_mhd(dev_t dev, int interval); 1492 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1493 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1494 static char *sd_sname(uchar_t status); 1495 static void sd_mhd_resvd_recover(void *arg); 1496 static void sd_resv_reclaim_thread(); 1497 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1498 static int sd_reserve_release(dev_t dev, int cmd); 1499 static void sd_rmv_resv_reclaim_req(dev_t dev); 1500 static void sd_mhd_reset_notify_cb(caddr_t arg); 1501 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1502 mhioc_inkeys_t *usrp, int flag); 1503 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1504 mhioc_inresvs_t *usrp, int flag); 1505 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1506 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1507 static int sd_mhdioc_release(dev_t dev); 1508 static int sd_mhdioc_register_devid(dev_t dev); 1509 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1510 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1511 1512 /* 1513 * SCSI removable prototypes 1514 */ 1515 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1516 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1517 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1518 static int sr_pause_resume(dev_t dev, int mode); 1519 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1520 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1521 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1522 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1523 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1524 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1525 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1526 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1527 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1528 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1529 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1530 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1531 static int sr_eject(dev_t dev); 1532 static void sr_ejected(register struct sd_lun *un); 1533 static int sr_check_wp(dev_t dev); 1534 static int sd_check_media(dev_t dev, enum dkio_state state); 1535 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1536 static void sd_delayed_cv_broadcast(void *arg); 1537 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1538 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1539 1540 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1541 1542 /* 1543 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1544 */ 1545 static void sd_check_for_writable_cd(struct sd_lun *un); 1546 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1547 static void sd_wm_cache_destructor(void *wm, void *un); 1548 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1549 daddr_t endb, ushort_t typ); 1550 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1551 daddr_t endb); 1552 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1553 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1554 static void sd_read_modify_write_task(void * arg); 1555 static int 1556 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1557 struct buf **bpp); 1558 1559 1560 /* 1561 * Function prototypes for failfast support. 1562 */ 1563 static void sd_failfast_flushq(struct sd_lun *un); 1564 static int sd_failfast_flushq_callback(struct buf *bp); 1565 1566 /* 1567 * Function prototypes to check for lsi devices 1568 */ 1569 static void sd_is_lsi(struct sd_lun *un); 1570 1571 /* 1572 * Function prototypes for x86 support 1573 */ 1574 #if defined(__i386) || defined(__amd64) 1575 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1576 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1577 #endif 1578 1579 /* 1580 * Constants for failfast support: 1581 * 1582 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1583 * failfast processing being performed. 1584 * 1585 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1586 * failfast processing on all bufs with B_FAILFAST set. 1587 */ 1588 1589 #define SD_FAILFAST_INACTIVE 0 1590 #define SD_FAILFAST_ACTIVE 1 1591 1592 /* 1593 * Bitmask to control behavior of buf(9S) flushes when a transition to 1594 * the failfast state occurs. Optional bits include: 1595 * 1596 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1597 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1598 * be flushed. 1599 * 1600 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1601 * driver, in addition to the regular wait queue. This includes the xbuf 1602 * queues. When clear, only the driver's wait queue will be flushed. 1603 */ 1604 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1605 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1606 1607 /* 1608 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1609 * to flush all queues within the driver. 1610 */ 1611 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1612 1613 1614 /* 1615 * SD Testing Fault Injection 1616 */ 1617 #ifdef SD_FAULT_INJECTION 1618 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1619 static void sd_faultinjection(struct scsi_pkt *pktp); 1620 static void sd_injection_log(char *buf, struct sd_lun *un); 1621 #endif 1622 1623 /* 1624 * Device driver ops vector 1625 */ 1626 static struct cb_ops sd_cb_ops = { 1627 sdopen, /* open */ 1628 sdclose, /* close */ 1629 sdstrategy, /* strategy */ 1630 nodev, /* print */ 1631 sddump, /* dump */ 1632 sdread, /* read */ 1633 sdwrite, /* write */ 1634 sdioctl, /* ioctl */ 1635 nodev, /* devmap */ 1636 nodev, /* mmap */ 1637 nodev, /* segmap */ 1638 nochpoll, /* poll */ 1639 sd_prop_op, /* cb_prop_op */ 1640 0, /* streamtab */ 1641 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1642 CB_REV, /* cb_rev */ 1643 sdaread, /* async I/O read entry point */ 1644 sdawrite /* async I/O write entry point */ 1645 }; 1646 1647 static struct dev_ops sd_ops = { 1648 DEVO_REV, /* devo_rev, */ 1649 0, /* refcnt */ 1650 sdinfo, /* info */ 1651 nulldev, /* identify */ 1652 sdprobe, /* probe */ 1653 sdattach, /* attach */ 1654 sddetach, /* detach */ 1655 nodev, /* reset */ 1656 &sd_cb_ops, /* driver operations */ 1657 NULL, /* bus operations */ 1658 sdpower /* power */ 1659 }; 1660 1661 1662 /* 1663 * This is the loadable module wrapper. 1664 */ 1665 #include <sys/modctl.h> 1666 1667 static struct modldrv modldrv = { 1668 &mod_driverops, /* Type of module. This one is a driver */ 1669 SD_MODULE_NAME, /* Module name. */ 1670 &sd_ops /* driver ops */ 1671 }; 1672 1673 1674 static struct modlinkage modlinkage = { 1675 MODREV_1, 1676 &modldrv, 1677 NULL 1678 }; 1679 1680 1681 static struct scsi_asq_key_strings sd_additional_codes[] = { 1682 0x81, 0, "Logical Unit is Reserved", 1683 0x85, 0, "Audio Address Not Valid", 1684 0xb6, 0, "Media Load Mechanism Failed", 1685 0xB9, 0, "Audio Play Operation Aborted", 1686 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1687 0x53, 2, "Medium removal prevented", 1688 0x6f, 0, "Authentication failed during key exchange", 1689 0x6f, 1, "Key not present", 1690 0x6f, 2, "Key not established", 1691 0x6f, 3, "Read without proper authentication", 1692 0x6f, 4, "Mismatched region to this logical unit", 1693 0x6f, 5, "Region reset count error", 1694 0xffff, 0x0, NULL 1695 }; 1696 1697 1698 /* 1699 * Struct for passing printing information for sense data messages 1700 */ 1701 struct sd_sense_info { 1702 int ssi_severity; 1703 int ssi_pfa_flag; 1704 }; 1705 1706 /* 1707 * Table of function pointers for iostart-side routines. Seperate "chains" 1708 * of layered function calls are formed by placing the function pointers 1709 * sequentially in the desired order. Functions are called according to an 1710 * incrementing table index ordering. The last function in each chain must 1711 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1712 * in the sd_iodone_chain[] array. 1713 * 1714 * Note: It may seem more natural to organize both the iostart and iodone 1715 * functions together, into an array of structures (or some similar 1716 * organization) with a common index, rather than two seperate arrays which 1717 * must be maintained in synchronization. The purpose of this division is 1718 * to achiece improved performance: individual arrays allows for more 1719 * effective cache line utilization on certain platforms. 1720 */ 1721 1722 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1723 1724 1725 static sd_chain_t sd_iostart_chain[] = { 1726 1727 /* Chain for buf IO for disk drive targets (PM enabled) */ 1728 sd_mapblockaddr_iostart, /* Index: 0 */ 1729 sd_pm_iostart, /* Index: 1 */ 1730 sd_core_iostart, /* Index: 2 */ 1731 1732 /* Chain for buf IO for disk drive targets (PM disabled) */ 1733 sd_mapblockaddr_iostart, /* Index: 3 */ 1734 sd_core_iostart, /* Index: 4 */ 1735 1736 /* Chain for buf IO for removable-media targets (PM enabled) */ 1737 sd_mapblockaddr_iostart, /* Index: 5 */ 1738 sd_mapblocksize_iostart, /* Index: 6 */ 1739 sd_pm_iostart, /* Index: 7 */ 1740 sd_core_iostart, /* Index: 8 */ 1741 1742 /* Chain for buf IO for removable-media targets (PM disabled) */ 1743 sd_mapblockaddr_iostart, /* Index: 9 */ 1744 sd_mapblocksize_iostart, /* Index: 10 */ 1745 sd_core_iostart, /* Index: 11 */ 1746 1747 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1748 sd_mapblockaddr_iostart, /* Index: 12 */ 1749 sd_checksum_iostart, /* Index: 13 */ 1750 sd_pm_iostart, /* Index: 14 */ 1751 sd_core_iostart, /* Index: 15 */ 1752 1753 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1754 sd_mapblockaddr_iostart, /* Index: 16 */ 1755 sd_checksum_iostart, /* Index: 17 */ 1756 sd_core_iostart, /* Index: 18 */ 1757 1758 /* Chain for USCSI commands (all targets) */ 1759 sd_pm_iostart, /* Index: 19 */ 1760 sd_core_iostart, /* Index: 20 */ 1761 1762 /* Chain for checksumming USCSI commands (all targets) */ 1763 sd_checksum_uscsi_iostart, /* Index: 21 */ 1764 sd_pm_iostart, /* Index: 22 */ 1765 sd_core_iostart, /* Index: 23 */ 1766 1767 /* Chain for "direct" USCSI commands (all targets) */ 1768 sd_core_iostart, /* Index: 24 */ 1769 1770 /* Chain for "direct priority" USCSI commands (all targets) */ 1771 sd_core_iostart, /* Index: 25 */ 1772 }; 1773 1774 /* 1775 * Macros to locate the first function of each iostart chain in the 1776 * sd_iostart_chain[] array. These are located by the index in the array. 1777 */ 1778 #define SD_CHAIN_DISK_IOSTART 0 1779 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1780 #define SD_CHAIN_RMMEDIA_IOSTART 5 1781 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1782 #define SD_CHAIN_CHKSUM_IOSTART 12 1783 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1784 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1785 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1786 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1787 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1788 1789 1790 /* 1791 * Table of function pointers for the iodone-side routines for the driver- 1792 * internal layering mechanism. The calling sequence for iodone routines 1793 * uses a decrementing table index, so the last routine called in a chain 1794 * must be at the lowest array index location for that chain. The last 1795 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1796 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1797 * of the functions in an iodone side chain must correspond to the ordering 1798 * of the iostart routines for that chain. Note that there is no iodone 1799 * side routine that corresponds to sd_core_iostart(), so there is no 1800 * entry in the table for this. 1801 */ 1802 1803 static sd_chain_t sd_iodone_chain[] = { 1804 1805 /* Chain for buf IO for disk drive targets (PM enabled) */ 1806 sd_buf_iodone, /* Index: 0 */ 1807 sd_mapblockaddr_iodone, /* Index: 1 */ 1808 sd_pm_iodone, /* Index: 2 */ 1809 1810 /* Chain for buf IO for disk drive targets (PM disabled) */ 1811 sd_buf_iodone, /* Index: 3 */ 1812 sd_mapblockaddr_iodone, /* Index: 4 */ 1813 1814 /* Chain for buf IO for removable-media targets (PM enabled) */ 1815 sd_buf_iodone, /* Index: 5 */ 1816 sd_mapblockaddr_iodone, /* Index: 6 */ 1817 sd_mapblocksize_iodone, /* Index: 7 */ 1818 sd_pm_iodone, /* Index: 8 */ 1819 1820 /* Chain for buf IO for removable-media targets (PM disabled) */ 1821 sd_buf_iodone, /* Index: 9 */ 1822 sd_mapblockaddr_iodone, /* Index: 10 */ 1823 sd_mapblocksize_iodone, /* Index: 11 */ 1824 1825 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1826 sd_buf_iodone, /* Index: 12 */ 1827 sd_mapblockaddr_iodone, /* Index: 13 */ 1828 sd_checksum_iodone, /* Index: 14 */ 1829 sd_pm_iodone, /* Index: 15 */ 1830 1831 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1832 sd_buf_iodone, /* Index: 16 */ 1833 sd_mapblockaddr_iodone, /* Index: 17 */ 1834 sd_checksum_iodone, /* Index: 18 */ 1835 1836 /* Chain for USCSI commands (non-checksum targets) */ 1837 sd_uscsi_iodone, /* Index: 19 */ 1838 sd_pm_iodone, /* Index: 20 */ 1839 1840 /* Chain for USCSI commands (checksum targets) */ 1841 sd_uscsi_iodone, /* Index: 21 */ 1842 sd_checksum_uscsi_iodone, /* Index: 22 */ 1843 sd_pm_iodone, /* Index: 22 */ 1844 1845 /* Chain for "direct" USCSI commands (all targets) */ 1846 sd_uscsi_iodone, /* Index: 24 */ 1847 1848 /* Chain for "direct priority" USCSI commands (all targets) */ 1849 sd_uscsi_iodone, /* Index: 25 */ 1850 }; 1851 1852 1853 /* 1854 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1855 * each iodone-side chain. These are located by the array index, but as the 1856 * iodone side functions are called in a decrementing-index order, the 1857 * highest index number in each chain must be specified (as these correspond 1858 * to the first function in the iodone chain that will be called by the core 1859 * at IO completion time). 1860 */ 1861 1862 #define SD_CHAIN_DISK_IODONE 2 1863 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1864 #define SD_CHAIN_RMMEDIA_IODONE 8 1865 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1866 #define SD_CHAIN_CHKSUM_IODONE 15 1867 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1868 #define SD_CHAIN_USCSI_CMD_IODONE 20 1869 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1870 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1871 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1872 1873 1874 1875 1876 /* 1877 * Array to map a layering chain index to the appropriate initpkt routine. 1878 * The redundant entries are present so that the index used for accessing 1879 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1880 * with this table as well. 1881 */ 1882 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1883 1884 static sd_initpkt_t sd_initpkt_map[] = { 1885 1886 /* Chain for buf IO for disk drive targets (PM enabled) */ 1887 sd_initpkt_for_buf, /* Index: 0 */ 1888 sd_initpkt_for_buf, /* Index: 1 */ 1889 sd_initpkt_for_buf, /* Index: 2 */ 1890 1891 /* Chain for buf IO for disk drive targets (PM disabled) */ 1892 sd_initpkt_for_buf, /* Index: 3 */ 1893 sd_initpkt_for_buf, /* Index: 4 */ 1894 1895 /* Chain for buf IO for removable-media targets (PM enabled) */ 1896 sd_initpkt_for_buf, /* Index: 5 */ 1897 sd_initpkt_for_buf, /* Index: 6 */ 1898 sd_initpkt_for_buf, /* Index: 7 */ 1899 sd_initpkt_for_buf, /* Index: 8 */ 1900 1901 /* Chain for buf IO for removable-media targets (PM disabled) */ 1902 sd_initpkt_for_buf, /* Index: 9 */ 1903 sd_initpkt_for_buf, /* Index: 10 */ 1904 sd_initpkt_for_buf, /* Index: 11 */ 1905 1906 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1907 sd_initpkt_for_buf, /* Index: 12 */ 1908 sd_initpkt_for_buf, /* Index: 13 */ 1909 sd_initpkt_for_buf, /* Index: 14 */ 1910 sd_initpkt_for_buf, /* Index: 15 */ 1911 1912 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1913 sd_initpkt_for_buf, /* Index: 16 */ 1914 sd_initpkt_for_buf, /* Index: 17 */ 1915 sd_initpkt_for_buf, /* Index: 18 */ 1916 1917 /* Chain for USCSI commands (non-checksum targets) */ 1918 sd_initpkt_for_uscsi, /* Index: 19 */ 1919 sd_initpkt_for_uscsi, /* Index: 20 */ 1920 1921 /* Chain for USCSI commands (checksum targets) */ 1922 sd_initpkt_for_uscsi, /* Index: 21 */ 1923 sd_initpkt_for_uscsi, /* Index: 22 */ 1924 sd_initpkt_for_uscsi, /* Index: 22 */ 1925 1926 /* Chain for "direct" USCSI commands (all targets) */ 1927 sd_initpkt_for_uscsi, /* Index: 24 */ 1928 1929 /* Chain for "direct priority" USCSI commands (all targets) */ 1930 sd_initpkt_for_uscsi, /* Index: 25 */ 1931 1932 }; 1933 1934 1935 /* 1936 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1937 * The redundant entries are present so that the index used for accessing 1938 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1939 * with this table as well. 1940 */ 1941 typedef void (*sd_destroypkt_t)(struct buf *); 1942 1943 static sd_destroypkt_t sd_destroypkt_map[] = { 1944 1945 /* Chain for buf IO for disk drive targets (PM enabled) */ 1946 sd_destroypkt_for_buf, /* Index: 0 */ 1947 sd_destroypkt_for_buf, /* Index: 1 */ 1948 sd_destroypkt_for_buf, /* Index: 2 */ 1949 1950 /* Chain for buf IO for disk drive targets (PM disabled) */ 1951 sd_destroypkt_for_buf, /* Index: 3 */ 1952 sd_destroypkt_for_buf, /* Index: 4 */ 1953 1954 /* Chain for buf IO for removable-media targets (PM enabled) */ 1955 sd_destroypkt_for_buf, /* Index: 5 */ 1956 sd_destroypkt_for_buf, /* Index: 6 */ 1957 sd_destroypkt_for_buf, /* Index: 7 */ 1958 sd_destroypkt_for_buf, /* Index: 8 */ 1959 1960 /* Chain for buf IO for removable-media targets (PM disabled) */ 1961 sd_destroypkt_for_buf, /* Index: 9 */ 1962 sd_destroypkt_for_buf, /* Index: 10 */ 1963 sd_destroypkt_for_buf, /* Index: 11 */ 1964 1965 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1966 sd_destroypkt_for_buf, /* Index: 12 */ 1967 sd_destroypkt_for_buf, /* Index: 13 */ 1968 sd_destroypkt_for_buf, /* Index: 14 */ 1969 sd_destroypkt_for_buf, /* Index: 15 */ 1970 1971 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1972 sd_destroypkt_for_buf, /* Index: 16 */ 1973 sd_destroypkt_for_buf, /* Index: 17 */ 1974 sd_destroypkt_for_buf, /* Index: 18 */ 1975 1976 /* Chain for USCSI commands (non-checksum targets) */ 1977 sd_destroypkt_for_uscsi, /* Index: 19 */ 1978 sd_destroypkt_for_uscsi, /* Index: 20 */ 1979 1980 /* Chain for USCSI commands (checksum targets) */ 1981 sd_destroypkt_for_uscsi, /* Index: 21 */ 1982 sd_destroypkt_for_uscsi, /* Index: 22 */ 1983 sd_destroypkt_for_uscsi, /* Index: 22 */ 1984 1985 /* Chain for "direct" USCSI commands (all targets) */ 1986 sd_destroypkt_for_uscsi, /* Index: 24 */ 1987 1988 /* Chain for "direct priority" USCSI commands (all targets) */ 1989 sd_destroypkt_for_uscsi, /* Index: 25 */ 1990 1991 }; 1992 1993 1994 1995 /* 1996 * Array to map a layering chain index to the appropriate chain "type". 1997 * The chain type indicates a specific property/usage of the chain. 1998 * The redundant entries are present so that the index used for accessing 1999 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2000 * with this table as well. 2001 */ 2002 2003 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2004 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2005 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2006 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2007 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2008 /* (for error recovery) */ 2009 2010 static int sd_chain_type_map[] = { 2011 2012 /* Chain for buf IO for disk drive targets (PM enabled) */ 2013 SD_CHAIN_BUFIO, /* Index: 0 */ 2014 SD_CHAIN_BUFIO, /* Index: 1 */ 2015 SD_CHAIN_BUFIO, /* Index: 2 */ 2016 2017 /* Chain for buf IO for disk drive targets (PM disabled) */ 2018 SD_CHAIN_BUFIO, /* Index: 3 */ 2019 SD_CHAIN_BUFIO, /* Index: 4 */ 2020 2021 /* Chain for buf IO for removable-media targets (PM enabled) */ 2022 SD_CHAIN_BUFIO, /* Index: 5 */ 2023 SD_CHAIN_BUFIO, /* Index: 6 */ 2024 SD_CHAIN_BUFIO, /* Index: 7 */ 2025 SD_CHAIN_BUFIO, /* Index: 8 */ 2026 2027 /* Chain for buf IO for removable-media targets (PM disabled) */ 2028 SD_CHAIN_BUFIO, /* Index: 9 */ 2029 SD_CHAIN_BUFIO, /* Index: 10 */ 2030 SD_CHAIN_BUFIO, /* Index: 11 */ 2031 2032 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2033 SD_CHAIN_BUFIO, /* Index: 12 */ 2034 SD_CHAIN_BUFIO, /* Index: 13 */ 2035 SD_CHAIN_BUFIO, /* Index: 14 */ 2036 SD_CHAIN_BUFIO, /* Index: 15 */ 2037 2038 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2039 SD_CHAIN_BUFIO, /* Index: 16 */ 2040 SD_CHAIN_BUFIO, /* Index: 17 */ 2041 SD_CHAIN_BUFIO, /* Index: 18 */ 2042 2043 /* Chain for USCSI commands (non-checksum targets) */ 2044 SD_CHAIN_USCSI, /* Index: 19 */ 2045 SD_CHAIN_USCSI, /* Index: 20 */ 2046 2047 /* Chain for USCSI commands (checksum targets) */ 2048 SD_CHAIN_USCSI, /* Index: 21 */ 2049 SD_CHAIN_USCSI, /* Index: 22 */ 2050 SD_CHAIN_USCSI, /* Index: 22 */ 2051 2052 /* Chain for "direct" USCSI commands (all targets) */ 2053 SD_CHAIN_DIRECT, /* Index: 24 */ 2054 2055 /* Chain for "direct priority" USCSI commands (all targets) */ 2056 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2057 }; 2058 2059 2060 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2061 #define SD_IS_BUFIO(xp) \ 2062 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2063 2064 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2065 #define SD_IS_DIRECT_PRIORITY(xp) \ 2066 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2067 2068 2069 2070 /* 2071 * Struct, array, and macros to map a specific chain to the appropriate 2072 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2073 * 2074 * The sd_chain_index_map[] array is used at attach time to set the various 2075 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2076 * chain to be used with the instance. This allows different instances to use 2077 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2078 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2079 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2080 * dynamically & without the use of locking; and (2) a layer may update the 2081 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2082 * to allow for deferred processing of an IO within the same chain from a 2083 * different execution context. 2084 */ 2085 2086 struct sd_chain_index { 2087 int sci_iostart_index; 2088 int sci_iodone_index; 2089 }; 2090 2091 static struct sd_chain_index sd_chain_index_map[] = { 2092 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2093 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2094 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2095 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2096 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2097 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2098 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2099 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2100 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2101 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2102 }; 2103 2104 2105 /* 2106 * The following are indexes into the sd_chain_index_map[] array. 2107 */ 2108 2109 /* un->un_buf_chain_type must be set to one of these */ 2110 #define SD_CHAIN_INFO_DISK 0 2111 #define SD_CHAIN_INFO_DISK_NO_PM 1 2112 #define SD_CHAIN_INFO_RMMEDIA 2 2113 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2114 #define SD_CHAIN_INFO_CHKSUM 4 2115 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2116 2117 /* un->un_uscsi_chain_type must be set to one of these */ 2118 #define SD_CHAIN_INFO_USCSI_CMD 6 2119 /* USCSI with PM disabled is the same as DIRECT */ 2120 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2121 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2122 2123 /* un->un_direct_chain_type must be set to one of these */ 2124 #define SD_CHAIN_INFO_DIRECT_CMD 8 2125 2126 /* un->un_priority_chain_type must be set to one of these */ 2127 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2128 2129 /* size for devid inquiries */ 2130 #define MAX_INQUIRY_SIZE 0xF0 2131 2132 /* 2133 * Macros used by functions to pass a given buf(9S) struct along to the 2134 * next function in the layering chain for further processing. 2135 * 2136 * In the following macros, passing more than three arguments to the called 2137 * routines causes the optimizer for the SPARC compiler to stop doing tail 2138 * call elimination which results in significant performance degradation. 2139 */ 2140 #define SD_BEGIN_IOSTART(index, un, bp) \ 2141 ((*(sd_iostart_chain[index]))(index, un, bp)) 2142 2143 #define SD_BEGIN_IODONE(index, un, bp) \ 2144 ((*(sd_iodone_chain[index]))(index, un, bp)) 2145 2146 #define SD_NEXT_IOSTART(index, un, bp) \ 2147 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2148 2149 #define SD_NEXT_IODONE(index, un, bp) \ 2150 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2151 2152 2153 /* 2154 * Function: _init 2155 * 2156 * Description: This is the driver _init(9E) entry point. 2157 * 2158 * Return Code: Returns the value from mod_install(9F) or 2159 * ddi_soft_state_init(9F) as appropriate. 2160 * 2161 * Context: Called when driver module loaded. 2162 */ 2163 2164 int 2165 _init(void) 2166 { 2167 int err; 2168 2169 /* establish driver name from module name */ 2170 sd_label = mod_modname(&modlinkage); 2171 2172 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2173 SD_MAXUNIT); 2174 2175 if (err != 0) { 2176 return (err); 2177 } 2178 2179 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2180 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2181 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2182 2183 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2184 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2185 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2186 2187 /* 2188 * it's ok to init here even for fibre device 2189 */ 2190 sd_scsi_probe_cache_init(); 2191 2192 /* 2193 * Creating taskq before mod_install ensures that all callers (threads) 2194 * that enter the module after a successfull mod_install encounter 2195 * a valid taskq. 2196 */ 2197 sd_taskq_create(); 2198 2199 err = mod_install(&modlinkage); 2200 if (err != 0) { 2201 /* delete taskq if install fails */ 2202 sd_taskq_delete(); 2203 2204 mutex_destroy(&sd_detach_mutex); 2205 mutex_destroy(&sd_log_mutex); 2206 mutex_destroy(&sd_label_mutex); 2207 2208 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2209 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2210 cv_destroy(&sd_tr.srq_inprocess_cv); 2211 2212 sd_scsi_probe_cache_fini(); 2213 2214 ddi_soft_state_fini(&sd_state); 2215 return (err); 2216 } 2217 2218 return (err); 2219 } 2220 2221 2222 /* 2223 * Function: _fini 2224 * 2225 * Description: This is the driver _fini(9E) entry point. 2226 * 2227 * Return Code: Returns the value from mod_remove(9F) 2228 * 2229 * Context: Called when driver module is unloaded. 2230 */ 2231 2232 int 2233 _fini(void) 2234 { 2235 int err; 2236 2237 if ((err = mod_remove(&modlinkage)) != 0) { 2238 return (err); 2239 } 2240 2241 sd_taskq_delete(); 2242 2243 mutex_destroy(&sd_detach_mutex); 2244 mutex_destroy(&sd_log_mutex); 2245 mutex_destroy(&sd_label_mutex); 2246 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2247 2248 sd_scsi_probe_cache_fini(); 2249 2250 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2251 cv_destroy(&sd_tr.srq_inprocess_cv); 2252 2253 ddi_soft_state_fini(&sd_state); 2254 2255 return (err); 2256 } 2257 2258 2259 /* 2260 * Function: _info 2261 * 2262 * Description: This is the driver _info(9E) entry point. 2263 * 2264 * Arguments: modinfop - pointer to the driver modinfo structure 2265 * 2266 * Return Code: Returns the value from mod_info(9F). 2267 * 2268 * Context: Kernel thread context 2269 */ 2270 2271 int 2272 _info(struct modinfo *modinfop) 2273 { 2274 return (mod_info(&modlinkage, modinfop)); 2275 } 2276 2277 2278 /* 2279 * The following routines implement the driver message logging facility. 2280 * They provide component- and level- based debug output filtering. 2281 * Output may also be restricted to messages for a single instance by 2282 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2283 * to NULL, then messages for all instances are printed. 2284 * 2285 * These routines have been cloned from each other due to the language 2286 * constraints of macros and variable argument list processing. 2287 */ 2288 2289 2290 /* 2291 * Function: sd_log_err 2292 * 2293 * Description: This routine is called by the SD_ERROR macro for debug 2294 * logging of error conditions. 2295 * 2296 * Arguments: comp - driver component being logged 2297 * dev - pointer to driver info structure 2298 * fmt - error string and format to be logged 2299 */ 2300 2301 static void 2302 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2303 { 2304 va_list ap; 2305 dev_info_t *dev; 2306 2307 ASSERT(un != NULL); 2308 dev = SD_DEVINFO(un); 2309 ASSERT(dev != NULL); 2310 2311 /* 2312 * Filter messages based on the global component and level masks. 2313 * Also print if un matches the value of sd_debug_un, or if 2314 * sd_debug_un is set to NULL. 2315 */ 2316 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2317 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2318 mutex_enter(&sd_log_mutex); 2319 va_start(ap, fmt); 2320 (void) vsprintf(sd_log_buf, fmt, ap); 2321 va_end(ap); 2322 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2323 mutex_exit(&sd_log_mutex); 2324 } 2325 #ifdef SD_FAULT_INJECTION 2326 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2327 if (un->sd_injection_mask & comp) { 2328 mutex_enter(&sd_log_mutex); 2329 va_start(ap, fmt); 2330 (void) vsprintf(sd_log_buf, fmt, ap); 2331 va_end(ap); 2332 sd_injection_log(sd_log_buf, un); 2333 mutex_exit(&sd_log_mutex); 2334 } 2335 #endif 2336 } 2337 2338 2339 /* 2340 * Function: sd_log_info 2341 * 2342 * Description: This routine is called by the SD_INFO macro for debug 2343 * logging of general purpose informational conditions. 2344 * 2345 * Arguments: comp - driver component being logged 2346 * dev - pointer to driver info structure 2347 * fmt - info string and format to be logged 2348 */ 2349 2350 static void 2351 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2352 { 2353 va_list ap; 2354 dev_info_t *dev; 2355 2356 ASSERT(un != NULL); 2357 dev = SD_DEVINFO(un); 2358 ASSERT(dev != NULL); 2359 2360 /* 2361 * Filter messages based on the global component and level masks. 2362 * Also print if un matches the value of sd_debug_un, or if 2363 * sd_debug_un is set to NULL. 2364 */ 2365 if ((sd_component_mask & component) && 2366 (sd_level_mask & SD_LOGMASK_INFO) && 2367 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2368 mutex_enter(&sd_log_mutex); 2369 va_start(ap, fmt); 2370 (void) vsprintf(sd_log_buf, fmt, ap); 2371 va_end(ap); 2372 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2373 mutex_exit(&sd_log_mutex); 2374 } 2375 #ifdef SD_FAULT_INJECTION 2376 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2377 if (un->sd_injection_mask & component) { 2378 mutex_enter(&sd_log_mutex); 2379 va_start(ap, fmt); 2380 (void) vsprintf(sd_log_buf, fmt, ap); 2381 va_end(ap); 2382 sd_injection_log(sd_log_buf, un); 2383 mutex_exit(&sd_log_mutex); 2384 } 2385 #endif 2386 } 2387 2388 2389 /* 2390 * Function: sd_log_trace 2391 * 2392 * Description: This routine is called by the SD_TRACE macro for debug 2393 * logging of trace conditions (i.e. function entry/exit). 2394 * 2395 * Arguments: comp - driver component being logged 2396 * dev - pointer to driver info structure 2397 * fmt - trace string and format to be logged 2398 */ 2399 2400 static void 2401 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2402 { 2403 va_list ap; 2404 dev_info_t *dev; 2405 2406 ASSERT(un != NULL); 2407 dev = SD_DEVINFO(un); 2408 ASSERT(dev != NULL); 2409 2410 /* 2411 * Filter messages based on the global component and level masks. 2412 * Also print if un matches the value of sd_debug_un, or if 2413 * sd_debug_un is set to NULL. 2414 */ 2415 if ((sd_component_mask & component) && 2416 (sd_level_mask & SD_LOGMASK_TRACE) && 2417 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2418 mutex_enter(&sd_log_mutex); 2419 va_start(ap, fmt); 2420 (void) vsprintf(sd_log_buf, fmt, ap); 2421 va_end(ap); 2422 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2423 mutex_exit(&sd_log_mutex); 2424 } 2425 #ifdef SD_FAULT_INJECTION 2426 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2427 if (un->sd_injection_mask & component) { 2428 mutex_enter(&sd_log_mutex); 2429 va_start(ap, fmt); 2430 (void) vsprintf(sd_log_buf, fmt, ap); 2431 va_end(ap); 2432 sd_injection_log(sd_log_buf, un); 2433 mutex_exit(&sd_log_mutex); 2434 } 2435 #endif 2436 } 2437 2438 2439 /* 2440 * Function: sdprobe 2441 * 2442 * Description: This is the driver probe(9e) entry point function. 2443 * 2444 * Arguments: devi - opaque device info handle 2445 * 2446 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2447 * DDI_PROBE_FAILURE: If the probe failed. 2448 * DDI_PROBE_PARTIAL: If the instance is not present now, 2449 * but may be present in the future. 2450 */ 2451 2452 static int 2453 sdprobe(dev_info_t *devi) 2454 { 2455 struct scsi_device *devp; 2456 int rval; 2457 int instance; 2458 2459 /* 2460 * if it wasn't for pln, sdprobe could actually be nulldev 2461 * in the "__fibre" case. 2462 */ 2463 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2464 return (DDI_PROBE_DONTCARE); 2465 } 2466 2467 devp = ddi_get_driver_private(devi); 2468 2469 if (devp == NULL) { 2470 /* Ooops... nexus driver is mis-configured... */ 2471 return (DDI_PROBE_FAILURE); 2472 } 2473 2474 instance = ddi_get_instance(devi); 2475 2476 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2477 return (DDI_PROBE_PARTIAL); 2478 } 2479 2480 /* 2481 * Call the SCSA utility probe routine to see if we actually 2482 * have a target at this SCSI nexus. 2483 */ 2484 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2485 case SCSIPROBE_EXISTS: 2486 switch (devp->sd_inq->inq_dtype) { 2487 case DTYPE_DIRECT: 2488 rval = DDI_PROBE_SUCCESS; 2489 break; 2490 case DTYPE_RODIRECT: 2491 /* CDs etc. Can be removable media */ 2492 rval = DDI_PROBE_SUCCESS; 2493 break; 2494 case DTYPE_OPTICAL: 2495 /* 2496 * Rewritable optical driver HP115AA 2497 * Can also be removable media 2498 */ 2499 2500 /* 2501 * Do not attempt to bind to DTYPE_OPTICAL if 2502 * pre solaris 9 sparc sd behavior is required 2503 * 2504 * If first time through and sd_dtype_optical_bind 2505 * has not been set in /etc/system check properties 2506 */ 2507 2508 if (sd_dtype_optical_bind < 0) { 2509 sd_dtype_optical_bind = ddi_prop_get_int 2510 (DDI_DEV_T_ANY, devi, 0, 2511 "optical-device-bind", 1); 2512 } 2513 2514 if (sd_dtype_optical_bind == 0) { 2515 rval = DDI_PROBE_FAILURE; 2516 } else { 2517 rval = DDI_PROBE_SUCCESS; 2518 } 2519 break; 2520 2521 case DTYPE_NOTPRESENT: 2522 default: 2523 rval = DDI_PROBE_FAILURE; 2524 break; 2525 } 2526 break; 2527 default: 2528 rval = DDI_PROBE_PARTIAL; 2529 break; 2530 } 2531 2532 /* 2533 * This routine checks for resource allocation prior to freeing, 2534 * so it will take care of the "smart probing" case where a 2535 * scsi_probe() may or may not have been issued and will *not* 2536 * free previously-freed resources. 2537 */ 2538 scsi_unprobe(devp); 2539 return (rval); 2540 } 2541 2542 2543 /* 2544 * Function: sdinfo 2545 * 2546 * Description: This is the driver getinfo(9e) entry point function. 2547 * Given the device number, return the devinfo pointer from 2548 * the scsi_device structure or the instance number 2549 * associated with the dev_t. 2550 * 2551 * Arguments: dip - pointer to device info structure 2552 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2553 * DDI_INFO_DEVT2INSTANCE) 2554 * arg - driver dev_t 2555 * resultp - user buffer for request response 2556 * 2557 * Return Code: DDI_SUCCESS 2558 * DDI_FAILURE 2559 */ 2560 /* ARGSUSED */ 2561 static int 2562 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2563 { 2564 struct sd_lun *un; 2565 dev_t dev; 2566 int instance; 2567 int error; 2568 2569 switch (infocmd) { 2570 case DDI_INFO_DEVT2DEVINFO: 2571 dev = (dev_t)arg; 2572 instance = SDUNIT(dev); 2573 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2574 return (DDI_FAILURE); 2575 } 2576 *result = (void *) SD_DEVINFO(un); 2577 error = DDI_SUCCESS; 2578 break; 2579 case DDI_INFO_DEVT2INSTANCE: 2580 dev = (dev_t)arg; 2581 instance = SDUNIT(dev); 2582 *result = (void *)(uintptr_t)instance; 2583 error = DDI_SUCCESS; 2584 break; 2585 default: 2586 error = DDI_FAILURE; 2587 } 2588 return (error); 2589 } 2590 2591 /* 2592 * Function: sd_prop_op 2593 * 2594 * Description: This is the driver prop_op(9e) entry point function. 2595 * Return the number of blocks for the partition in question 2596 * or forward the request to the property facilities. 2597 * 2598 * Arguments: dev - device number 2599 * dip - pointer to device info structure 2600 * prop_op - property operator 2601 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2602 * name - pointer to property name 2603 * valuep - pointer or address of the user buffer 2604 * lengthp - property length 2605 * 2606 * Return Code: DDI_PROP_SUCCESS 2607 * DDI_PROP_NOT_FOUND 2608 * DDI_PROP_UNDEFINED 2609 * DDI_PROP_NO_MEMORY 2610 * DDI_PROP_BUF_TOO_SMALL 2611 */ 2612 2613 static int 2614 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2615 char *name, caddr_t valuep, int *lengthp) 2616 { 2617 int instance = ddi_get_instance(dip); 2618 struct sd_lun *un; 2619 uint64_t nblocks64; 2620 2621 /* 2622 * Our dynamic properties are all device specific and size oriented. 2623 * Requests issued under conditions where size is valid are passed 2624 * to ddi_prop_op_nblocks with the size information, otherwise the 2625 * request is passed to ddi_prop_op. Size depends on valid geometry. 2626 */ 2627 un = ddi_get_soft_state(sd_state, instance); 2628 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2629 (un->un_f_geometry_is_valid == FALSE)) { 2630 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2631 name, valuep, lengthp)); 2632 } else { 2633 /* get nblocks value */ 2634 ASSERT(!mutex_owned(SD_MUTEX(un))); 2635 mutex_enter(SD_MUTEX(un)); 2636 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2637 mutex_exit(SD_MUTEX(un)); 2638 2639 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2640 name, valuep, lengthp, nblocks64)); 2641 } 2642 } 2643 2644 /* 2645 * The following functions are for smart probing: 2646 * sd_scsi_probe_cache_init() 2647 * sd_scsi_probe_cache_fini() 2648 * sd_scsi_clear_probe_cache() 2649 * sd_scsi_probe_with_cache() 2650 */ 2651 2652 /* 2653 * Function: sd_scsi_probe_cache_init 2654 * 2655 * Description: Initializes the probe response cache mutex and head pointer. 2656 * 2657 * Context: Kernel thread context 2658 */ 2659 2660 static void 2661 sd_scsi_probe_cache_init(void) 2662 { 2663 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2664 sd_scsi_probe_cache_head = NULL; 2665 } 2666 2667 2668 /* 2669 * Function: sd_scsi_probe_cache_fini 2670 * 2671 * Description: Frees all resources associated with the probe response cache. 2672 * 2673 * Context: Kernel thread context 2674 */ 2675 2676 static void 2677 sd_scsi_probe_cache_fini(void) 2678 { 2679 struct sd_scsi_probe_cache *cp; 2680 struct sd_scsi_probe_cache *ncp; 2681 2682 /* Clean up our smart probing linked list */ 2683 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2684 ncp = cp->next; 2685 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2686 } 2687 sd_scsi_probe_cache_head = NULL; 2688 mutex_destroy(&sd_scsi_probe_cache_mutex); 2689 } 2690 2691 2692 /* 2693 * Function: sd_scsi_clear_probe_cache 2694 * 2695 * Description: This routine clears the probe response cache. This is 2696 * done when open() returns ENXIO so that when deferred 2697 * attach is attempted (possibly after a device has been 2698 * turned on) we will retry the probe. Since we don't know 2699 * which target we failed to open, we just clear the 2700 * entire cache. 2701 * 2702 * Context: Kernel thread context 2703 */ 2704 2705 static void 2706 sd_scsi_clear_probe_cache(void) 2707 { 2708 struct sd_scsi_probe_cache *cp; 2709 int i; 2710 2711 mutex_enter(&sd_scsi_probe_cache_mutex); 2712 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2713 /* 2714 * Reset all entries to SCSIPROBE_EXISTS. This will 2715 * force probing to be performed the next time 2716 * sd_scsi_probe_with_cache is called. 2717 */ 2718 for (i = 0; i < NTARGETS_WIDE; i++) { 2719 cp->cache[i] = SCSIPROBE_EXISTS; 2720 } 2721 } 2722 mutex_exit(&sd_scsi_probe_cache_mutex); 2723 } 2724 2725 2726 /* 2727 * Function: sd_scsi_probe_with_cache 2728 * 2729 * Description: This routine implements support for a scsi device probe 2730 * with cache. The driver maintains a cache of the target 2731 * responses to scsi probes. If we get no response from a 2732 * target during a probe inquiry, we remember that, and we 2733 * avoid additional calls to scsi_probe on non-zero LUNs 2734 * on the same target until the cache is cleared. By doing 2735 * so we avoid the 1/4 sec selection timeout for nonzero 2736 * LUNs. lun0 of a target is always probed. 2737 * 2738 * Arguments: devp - Pointer to a scsi_device(9S) structure 2739 * waitfunc - indicates what the allocator routines should 2740 * do when resources are not available. This value 2741 * is passed on to scsi_probe() when that routine 2742 * is called. 2743 * 2744 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2745 * otherwise the value returned by scsi_probe(9F). 2746 * 2747 * Context: Kernel thread context 2748 */ 2749 2750 static int 2751 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2752 { 2753 struct sd_scsi_probe_cache *cp; 2754 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2755 int lun, tgt; 2756 2757 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2758 SCSI_ADDR_PROP_LUN, 0); 2759 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2760 SCSI_ADDR_PROP_TARGET, -1); 2761 2762 /* Make sure caching enabled and target in range */ 2763 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2764 /* do it the old way (no cache) */ 2765 return (scsi_probe(devp, waitfn)); 2766 } 2767 2768 mutex_enter(&sd_scsi_probe_cache_mutex); 2769 2770 /* Find the cache for this scsi bus instance */ 2771 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2772 if (cp->pdip == pdip) { 2773 break; 2774 } 2775 } 2776 2777 /* If we can't find a cache for this pdip, create one */ 2778 if (cp == NULL) { 2779 int i; 2780 2781 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2782 KM_SLEEP); 2783 cp->pdip = pdip; 2784 cp->next = sd_scsi_probe_cache_head; 2785 sd_scsi_probe_cache_head = cp; 2786 for (i = 0; i < NTARGETS_WIDE; i++) { 2787 cp->cache[i] = SCSIPROBE_EXISTS; 2788 } 2789 } 2790 2791 mutex_exit(&sd_scsi_probe_cache_mutex); 2792 2793 /* Recompute the cache for this target if LUN zero */ 2794 if (lun == 0) { 2795 cp->cache[tgt] = SCSIPROBE_EXISTS; 2796 } 2797 2798 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2799 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2800 return (SCSIPROBE_NORESP); 2801 } 2802 2803 /* Do the actual probe; save & return the result */ 2804 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2805 } 2806 2807 2808 /* 2809 * Function: sd_spin_up_unit 2810 * 2811 * Description: Issues the following commands to spin-up the device: 2812 * START STOP UNIT, and INQUIRY. 2813 * 2814 * Arguments: un - driver soft state (unit) structure 2815 * 2816 * Return Code: 0 - success 2817 * EIO - failure 2818 * EACCES - reservation conflict 2819 * 2820 * Context: Kernel thread context 2821 */ 2822 2823 static int 2824 sd_spin_up_unit(struct sd_lun *un) 2825 { 2826 size_t resid = 0; 2827 int has_conflict = FALSE; 2828 uchar_t *bufaddr; 2829 2830 ASSERT(un != NULL); 2831 2832 /* 2833 * Send a throwaway START UNIT command. 2834 * 2835 * If we fail on this, we don't care presently what precisely 2836 * is wrong. EMC's arrays will also fail this with a check 2837 * condition (0x2/0x4/0x3) if the device is "inactive," but 2838 * we don't want to fail the attach because it may become 2839 * "active" later. 2840 */ 2841 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2842 == EACCES) 2843 has_conflict = TRUE; 2844 2845 /* 2846 * Send another INQUIRY command to the target. This is necessary for 2847 * non-removable media direct access devices because their INQUIRY data 2848 * may not be fully qualified until they are spun up (perhaps via the 2849 * START command above). Note: This seems to be needed for some 2850 * legacy devices only.) The INQUIRY command should succeed even if a 2851 * Reservation Conflict is present. 2852 */ 2853 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2854 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2855 kmem_free(bufaddr, SUN_INQSIZE); 2856 return (EIO); 2857 } 2858 2859 /* 2860 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2861 * Note that this routine does not return a failure here even if the 2862 * INQUIRY command did not return any data. This is a legacy behavior. 2863 */ 2864 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2865 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2866 } 2867 2868 kmem_free(bufaddr, SUN_INQSIZE); 2869 2870 /* If we hit a reservation conflict above, tell the caller. */ 2871 if (has_conflict == TRUE) { 2872 return (EACCES); 2873 } 2874 2875 return (0); 2876 } 2877 2878 #ifdef _LP64 2879 /* 2880 * Function: sd_enable_descr_sense 2881 * 2882 * Description: This routine attempts to select descriptor sense format 2883 * using the Control mode page. Devices that support 64 bit 2884 * LBAs (for >2TB luns) should also implement descriptor 2885 * sense data so we will call this function whenever we see 2886 * a lun larger than 2TB. If for some reason the device 2887 * supports 64 bit LBAs but doesn't support descriptor sense 2888 * presumably the mode select will fail. Everything will 2889 * continue to work normally except that we will not get 2890 * complete sense data for commands that fail with an LBA 2891 * larger than 32 bits. 2892 * 2893 * Arguments: un - driver soft state (unit) structure 2894 * 2895 * Context: Kernel thread context only 2896 */ 2897 2898 static void 2899 sd_enable_descr_sense(struct sd_lun *un) 2900 { 2901 uchar_t *header; 2902 struct mode_control_scsi3 *ctrl_bufp; 2903 size_t buflen; 2904 size_t bd_len; 2905 2906 /* 2907 * Read MODE SENSE page 0xA, Control Mode Page 2908 */ 2909 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2910 sizeof (struct mode_control_scsi3); 2911 header = kmem_zalloc(buflen, KM_SLEEP); 2912 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2913 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2914 SD_ERROR(SD_LOG_COMMON, un, 2915 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2916 goto eds_exit; 2917 } 2918 2919 /* 2920 * Determine size of Block Descriptors in order to locate 2921 * the mode page data. ATAPI devices return 0, SCSI devices 2922 * should return MODE_BLK_DESC_LENGTH. 2923 */ 2924 bd_len = ((struct mode_header *)header)->bdesc_length; 2925 2926 ctrl_bufp = (struct mode_control_scsi3 *) 2927 (header + MODE_HEADER_LENGTH + bd_len); 2928 2929 /* 2930 * Clear PS bit for MODE SELECT 2931 */ 2932 ctrl_bufp->mode_page.ps = 0; 2933 2934 /* 2935 * Set D_SENSE to enable descriptor sense format. 2936 */ 2937 ctrl_bufp->d_sense = 1; 2938 2939 /* 2940 * Use MODE SELECT to commit the change to the D_SENSE bit 2941 */ 2942 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2943 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2944 SD_INFO(SD_LOG_COMMON, un, 2945 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2946 goto eds_exit; 2947 } 2948 2949 eds_exit: 2950 kmem_free(header, buflen); 2951 } 2952 #endif /* _LP64 */ 2953 2954 2955 /* 2956 * Function: sd_set_mmc_caps 2957 * 2958 * Description: This routine determines if the device is MMC compliant and if 2959 * the device supports CDDA via a mode sense of the CDVD 2960 * capabilities mode page. Also checks if the device is a 2961 * dvdram writable device. 2962 * 2963 * Arguments: un - driver soft state (unit) structure 2964 * 2965 * Context: Kernel thread context only 2966 */ 2967 2968 static void 2969 sd_set_mmc_caps(struct sd_lun *un) 2970 { 2971 struct mode_header_grp2 *sense_mhp; 2972 uchar_t *sense_page; 2973 caddr_t buf; 2974 int bd_len; 2975 int status; 2976 struct uscsi_cmd com; 2977 int rtn; 2978 uchar_t *out_data_rw, *out_data_hd; 2979 uchar_t *rqbuf_rw, *rqbuf_hd; 2980 2981 ASSERT(un != NULL); 2982 2983 /* 2984 * The flags which will be set in this function are - mmc compliant, 2985 * dvdram writable device, cdda support. Initialize them to FALSE 2986 * and if a capability is detected - it will be set to TRUE. 2987 */ 2988 un->un_f_mmc_cap = FALSE; 2989 un->un_f_dvdram_writable_device = FALSE; 2990 un->un_f_cfg_cdda = FALSE; 2991 2992 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 2993 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 2994 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 2995 2996 if (status != 0) { 2997 /* command failed; just return */ 2998 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 2999 return; 3000 } 3001 /* 3002 * If the mode sense request for the CDROM CAPABILITIES 3003 * page (0x2A) succeeds the device is assumed to be MMC. 3004 */ 3005 un->un_f_mmc_cap = TRUE; 3006 3007 /* Get to the page data */ 3008 sense_mhp = (struct mode_header_grp2 *)buf; 3009 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3010 sense_mhp->bdesc_length_lo; 3011 if (bd_len > MODE_BLK_DESC_LENGTH) { 3012 /* 3013 * We did not get back the expected block descriptor 3014 * length so we cannot determine if the device supports 3015 * CDDA. However, we still indicate the device is MMC 3016 * according to the successful response to the page 3017 * 0x2A mode sense request. 3018 */ 3019 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3020 "sd_set_mmc_caps: Mode Sense returned " 3021 "invalid block descriptor length\n"); 3022 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3023 return; 3024 } 3025 3026 /* See if read CDDA is supported */ 3027 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3028 bd_len); 3029 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3030 3031 /* See if writing DVD RAM is supported. */ 3032 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3033 if (un->un_f_dvdram_writable_device == TRUE) { 3034 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3035 return; 3036 } 3037 3038 /* 3039 * If the device presents DVD or CD capabilities in the mode 3040 * page, we can return here since a RRD will not have 3041 * these capabilities. 3042 */ 3043 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3044 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3045 return; 3046 } 3047 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3048 3049 /* 3050 * If un->un_f_dvdram_writable_device is still FALSE, 3051 * check for a Removable Rigid Disk (RRD). A RRD 3052 * device is identified by the features RANDOM_WRITABLE and 3053 * HARDWARE_DEFECT_MANAGEMENT. 3054 */ 3055 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3056 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3057 3058 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3059 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3060 RANDOM_WRITABLE); 3061 if (rtn != 0) { 3062 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3063 kmem_free(rqbuf_rw, SENSE_LENGTH); 3064 return; 3065 } 3066 3067 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3068 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3069 3070 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3071 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3072 HARDWARE_DEFECT_MANAGEMENT); 3073 if (rtn == 0) { 3074 /* 3075 * We have good information, check for random writable 3076 * and hardware defect features. 3077 */ 3078 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3079 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3080 un->un_f_dvdram_writable_device = TRUE; 3081 } 3082 } 3083 3084 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3085 kmem_free(rqbuf_rw, SENSE_LENGTH); 3086 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3087 kmem_free(rqbuf_hd, SENSE_LENGTH); 3088 } 3089 3090 /* 3091 * Function: sd_check_for_writable_cd 3092 * 3093 * Description: This routine determines if the media in the device is 3094 * writable or not. It uses the get configuration command (0x46) 3095 * to determine if the media is writable 3096 * 3097 * Arguments: un - driver soft state (unit) structure 3098 * 3099 * Context: Never called at interrupt context. 3100 */ 3101 3102 static void 3103 sd_check_for_writable_cd(struct sd_lun *un) 3104 { 3105 struct uscsi_cmd com; 3106 uchar_t *out_data; 3107 uchar_t *rqbuf; 3108 int rtn; 3109 uchar_t *out_data_rw, *out_data_hd; 3110 uchar_t *rqbuf_rw, *rqbuf_hd; 3111 struct mode_header_grp2 *sense_mhp; 3112 uchar_t *sense_page; 3113 caddr_t buf; 3114 int bd_len; 3115 int status; 3116 3117 ASSERT(un != NULL); 3118 ASSERT(mutex_owned(SD_MUTEX(un))); 3119 3120 /* 3121 * Initialize the writable media to false, if configuration info. 3122 * tells us otherwise then only we will set it. 3123 */ 3124 un->un_f_mmc_writable_media = FALSE; 3125 mutex_exit(SD_MUTEX(un)); 3126 3127 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3128 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3129 3130 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3131 out_data, SD_PROFILE_HEADER_LEN); 3132 3133 mutex_enter(SD_MUTEX(un)); 3134 if (rtn == 0) { 3135 /* 3136 * We have good information, check for writable DVD. 3137 */ 3138 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3139 un->un_f_mmc_writable_media = TRUE; 3140 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3141 kmem_free(rqbuf, SENSE_LENGTH); 3142 return; 3143 } 3144 } 3145 3146 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3147 kmem_free(rqbuf, SENSE_LENGTH); 3148 3149 /* 3150 * Determine if this is a RRD type device. 3151 */ 3152 mutex_exit(SD_MUTEX(un)); 3153 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3154 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3155 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3156 mutex_enter(SD_MUTEX(un)); 3157 if (status != 0) { 3158 /* command failed; just return */ 3159 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3160 return; 3161 } 3162 3163 /* Get to the page data */ 3164 sense_mhp = (struct mode_header_grp2 *)buf; 3165 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3166 if (bd_len > MODE_BLK_DESC_LENGTH) { 3167 /* 3168 * We did not get back the expected block descriptor length so 3169 * we cannot check the mode page. 3170 */ 3171 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3172 "sd_check_for_writable_cd: Mode Sense returned " 3173 "invalid block descriptor length\n"); 3174 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3175 return; 3176 } 3177 3178 /* 3179 * If the device presents DVD or CD capabilities in the mode 3180 * page, we can return here since a RRD device will not have 3181 * these capabilities. 3182 */ 3183 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3184 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3185 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3186 return; 3187 } 3188 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3189 3190 /* 3191 * If un->un_f_mmc_writable_media is still FALSE, 3192 * check for RRD type media. A RRD device is identified 3193 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3194 */ 3195 mutex_exit(SD_MUTEX(un)); 3196 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3197 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3198 3199 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3200 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3201 RANDOM_WRITABLE); 3202 if (rtn != 0) { 3203 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3204 kmem_free(rqbuf_rw, SENSE_LENGTH); 3205 mutex_enter(SD_MUTEX(un)); 3206 return; 3207 } 3208 3209 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3210 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3211 3212 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3213 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3214 HARDWARE_DEFECT_MANAGEMENT); 3215 mutex_enter(SD_MUTEX(un)); 3216 if (rtn == 0) { 3217 /* 3218 * We have good information, check for random writable 3219 * and hardware defect features as current. 3220 */ 3221 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3222 (out_data_rw[10] & 0x1) && 3223 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3224 (out_data_hd[10] & 0x1)) { 3225 un->un_f_mmc_writable_media = TRUE; 3226 } 3227 } 3228 3229 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3230 kmem_free(rqbuf_rw, SENSE_LENGTH); 3231 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3232 kmem_free(rqbuf_hd, SENSE_LENGTH); 3233 } 3234 3235 /* 3236 * Function: sd_read_unit_properties 3237 * 3238 * Description: The following implements a property lookup mechanism. 3239 * Properties for particular disks (keyed on vendor, model 3240 * and rev numbers) are sought in the sd.conf file via 3241 * sd_process_sdconf_file(), and if not found there, are 3242 * looked for in a list hardcoded in this driver via 3243 * sd_process_sdconf_table() Once located the properties 3244 * are used to update the driver unit structure. 3245 * 3246 * Arguments: un - driver soft state (unit) structure 3247 */ 3248 3249 static void 3250 sd_read_unit_properties(struct sd_lun *un) 3251 { 3252 /* 3253 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3254 * the "sd-config-list" property (from the sd.conf file) or if 3255 * there was not a match for the inquiry vid/pid. If this event 3256 * occurs the static driver configuration table is searched for 3257 * a match. 3258 */ 3259 ASSERT(un != NULL); 3260 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3261 sd_process_sdconf_table(un); 3262 } 3263 3264 /* check for LSI device */ 3265 sd_is_lsi(un); 3266 3267 3268 } 3269 3270 3271 /* 3272 * Function: sd_process_sdconf_file 3273 * 3274 * Description: Use ddi_getlongprop to obtain the properties from the 3275 * driver's config file (ie, sd.conf) and update the driver 3276 * soft state structure accordingly. 3277 * 3278 * Arguments: un - driver soft state (unit) structure 3279 * 3280 * Return Code: SD_SUCCESS - The properties were successfully set according 3281 * to the driver configuration file. 3282 * SD_FAILURE - The driver config list was not obtained or 3283 * there was no vid/pid match. This indicates that 3284 * the static config table should be used. 3285 * 3286 * The config file has a property, "sd-config-list", which consists of 3287 * one or more duplets as follows: 3288 * 3289 * sd-config-list= 3290 * <duplet>, 3291 * [<duplet>,] 3292 * [<duplet>]; 3293 * 3294 * The structure of each duplet is as follows: 3295 * 3296 * <duplet>:= <vid+pid>,<data-property-name_list> 3297 * 3298 * The first entry of the duplet is the device ID string (the concatenated 3299 * vid & pid; not to be confused with a device_id). This is defined in 3300 * the same way as in the sd_disk_table. 3301 * 3302 * The second part of the duplet is a string that identifies a 3303 * data-property-name-list. The data-property-name-list is defined as 3304 * follows: 3305 * 3306 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3307 * 3308 * The syntax of <data-property-name> depends on the <version> field. 3309 * 3310 * If version = SD_CONF_VERSION_1 we have the following syntax: 3311 * 3312 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3313 * 3314 * where the prop0 value will be used to set prop0 if bit0 set in the 3315 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3316 * 3317 */ 3318 3319 static int 3320 sd_process_sdconf_file(struct sd_lun *un) 3321 { 3322 char *config_list = NULL; 3323 int config_list_len; 3324 int len; 3325 int dupletlen = 0; 3326 char *vidptr; 3327 int vidlen; 3328 char *dnlist_ptr; 3329 char *dataname_ptr; 3330 int dnlist_len; 3331 int dataname_len; 3332 int *data_list; 3333 int data_list_len; 3334 int rval = SD_FAILURE; 3335 int i; 3336 3337 ASSERT(un != NULL); 3338 3339 /* Obtain the configuration list associated with the .conf file */ 3340 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3341 sd_config_list, (caddr_t)&config_list, &config_list_len) 3342 != DDI_PROP_SUCCESS) { 3343 return (SD_FAILURE); 3344 } 3345 3346 /* 3347 * Compare vids in each duplet to the inquiry vid - if a match is 3348 * made, get the data value and update the soft state structure 3349 * accordingly. 3350 * 3351 * Note: This algorithm is complex and difficult to maintain. It should 3352 * be replaced with a more robust implementation. 3353 */ 3354 for (len = config_list_len, vidptr = config_list; len > 0; 3355 vidptr += dupletlen, len -= dupletlen) { 3356 /* 3357 * Note: The assumption here is that each vid entry is on 3358 * a unique line from its associated duplet. 3359 */ 3360 vidlen = dupletlen = (int)strlen(vidptr); 3361 if ((vidlen == 0) || 3362 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3363 dupletlen++; 3364 continue; 3365 } 3366 3367 /* 3368 * dnlist contains 1 or more blank separated 3369 * data-property-name entries 3370 */ 3371 dnlist_ptr = vidptr + vidlen + 1; 3372 dnlist_len = (int)strlen(dnlist_ptr); 3373 dupletlen += dnlist_len + 2; 3374 3375 /* 3376 * Set a pointer for the first data-property-name 3377 * entry in the list 3378 */ 3379 dataname_ptr = dnlist_ptr; 3380 dataname_len = 0; 3381 3382 /* 3383 * Loop through all data-property-name entries in the 3384 * data-property-name-list setting the properties for each. 3385 */ 3386 while (dataname_len < dnlist_len) { 3387 int version; 3388 3389 /* 3390 * Determine the length of the current 3391 * data-property-name entry by indexing until a 3392 * blank or NULL is encountered. When the space is 3393 * encountered reset it to a NULL for compliance 3394 * with ddi_getlongprop(). 3395 */ 3396 for (i = 0; ((dataname_ptr[i] != ' ') && 3397 (dataname_ptr[i] != '\0')); i++) { 3398 ; 3399 } 3400 3401 dataname_len += i; 3402 /* If not null terminated, Make it so */ 3403 if (dataname_ptr[i] == ' ') { 3404 dataname_ptr[i] = '\0'; 3405 } 3406 dataname_len++; 3407 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3408 "sd_process_sdconf_file: disk:%s, data:%s\n", 3409 vidptr, dataname_ptr); 3410 3411 /* Get the data list */ 3412 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3413 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3414 != DDI_PROP_SUCCESS) { 3415 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3416 "sd_process_sdconf_file: data property (%s)" 3417 " has no value\n", dataname_ptr); 3418 dataname_ptr = dnlist_ptr + dataname_len; 3419 continue; 3420 } 3421 3422 version = data_list[0]; 3423 3424 if (version == SD_CONF_VERSION_1) { 3425 sd_tunables values; 3426 3427 /* Set the properties */ 3428 if (sd_chk_vers1_data(un, data_list[1], 3429 &data_list[2], data_list_len, dataname_ptr) 3430 == SD_SUCCESS) { 3431 sd_get_tunables_from_conf(un, 3432 data_list[1], &data_list[2], 3433 &values); 3434 sd_set_vers1_properties(un, 3435 data_list[1], &values); 3436 rval = SD_SUCCESS; 3437 } else { 3438 rval = SD_FAILURE; 3439 } 3440 } else { 3441 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3442 "data property %s version 0x%x is invalid.", 3443 dataname_ptr, version); 3444 rval = SD_FAILURE; 3445 } 3446 kmem_free(data_list, data_list_len); 3447 dataname_ptr = dnlist_ptr + dataname_len; 3448 } 3449 } 3450 3451 /* free up the memory allocated by ddi_getlongprop */ 3452 if (config_list) { 3453 kmem_free(config_list, config_list_len); 3454 } 3455 3456 return (rval); 3457 } 3458 3459 /* 3460 * Function: sd_get_tunables_from_conf() 3461 * 3462 * 3463 * This function reads the data list from the sd.conf file and pulls 3464 * the values that can have numeric values as arguments and places 3465 * the values in the apropriate sd_tunables member. 3466 * Since the order of the data list members varies across platforms 3467 * This function reads them from the data list in a platform specific 3468 * order and places them into the correct sd_tunable member that is 3469 * a consistant across all platforms. 3470 */ 3471 static void 3472 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3473 sd_tunables *values) 3474 { 3475 int i; 3476 int mask; 3477 3478 bzero(values, sizeof (sd_tunables)); 3479 3480 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3481 3482 mask = 1 << i; 3483 if (mask > flags) { 3484 break; 3485 } 3486 3487 switch (mask & flags) { 3488 case 0: /* This mask bit not set in flags */ 3489 continue; 3490 case SD_CONF_BSET_THROTTLE: 3491 values->sdt_throttle = data_list[i]; 3492 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3493 "sd_get_tunables_from_conf: throttle = %d\n", 3494 values->sdt_throttle); 3495 break; 3496 case SD_CONF_BSET_CTYPE: 3497 values->sdt_ctype = data_list[i]; 3498 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3499 "sd_get_tunables_from_conf: ctype = %d\n", 3500 values->sdt_ctype); 3501 break; 3502 case SD_CONF_BSET_NRR_COUNT: 3503 values->sdt_not_rdy_retries = data_list[i]; 3504 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3505 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3506 values->sdt_not_rdy_retries); 3507 break; 3508 case SD_CONF_BSET_BSY_RETRY_COUNT: 3509 values->sdt_busy_retries = data_list[i]; 3510 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3511 "sd_get_tunables_from_conf: busy_retries = %d\n", 3512 values->sdt_busy_retries); 3513 break; 3514 case SD_CONF_BSET_RST_RETRIES: 3515 values->sdt_reset_retries = data_list[i]; 3516 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3517 "sd_get_tunables_from_conf: reset_retries = %d\n", 3518 values->sdt_reset_retries); 3519 break; 3520 case SD_CONF_BSET_RSV_REL_TIME: 3521 values->sdt_reserv_rel_time = data_list[i]; 3522 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3523 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3524 values->sdt_reserv_rel_time); 3525 break; 3526 case SD_CONF_BSET_MIN_THROTTLE: 3527 values->sdt_min_throttle = data_list[i]; 3528 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3529 "sd_get_tunables_from_conf: min_throttle = %d\n", 3530 values->sdt_min_throttle); 3531 break; 3532 case SD_CONF_BSET_DISKSORT_DISABLED: 3533 values->sdt_disk_sort_dis = data_list[i]; 3534 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3535 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3536 values->sdt_disk_sort_dis); 3537 break; 3538 case SD_CONF_BSET_LUN_RESET_ENABLED: 3539 values->sdt_lun_reset_enable = data_list[i]; 3540 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3541 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3542 "\n", values->sdt_lun_reset_enable); 3543 break; 3544 } 3545 } 3546 } 3547 3548 /* 3549 * Function: sd_process_sdconf_table 3550 * 3551 * Description: Search the static configuration table for a match on the 3552 * inquiry vid/pid and update the driver soft state structure 3553 * according to the table property values for the device. 3554 * 3555 * The form of a configuration table entry is: 3556 * <vid+pid>,<flags>,<property-data> 3557 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3558 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3559 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3560 * 3561 * Arguments: un - driver soft state (unit) structure 3562 */ 3563 3564 static void 3565 sd_process_sdconf_table(struct sd_lun *un) 3566 { 3567 char *id = NULL; 3568 int table_index; 3569 int idlen; 3570 3571 ASSERT(un != NULL); 3572 for (table_index = 0; table_index < sd_disk_table_size; 3573 table_index++) { 3574 id = sd_disk_table[table_index].device_id; 3575 idlen = strlen(id); 3576 if (idlen == 0) { 3577 continue; 3578 } 3579 3580 /* 3581 * The static configuration table currently does not 3582 * implement version 10 properties. Additionally, 3583 * multiple data-property-name entries are not 3584 * implemented in the static configuration table. 3585 */ 3586 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3587 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3588 "sd_process_sdconf_table: disk %s\n", id); 3589 sd_set_vers1_properties(un, 3590 sd_disk_table[table_index].flags, 3591 sd_disk_table[table_index].properties); 3592 break; 3593 } 3594 } 3595 } 3596 3597 3598 /* 3599 * Function: sd_sdconf_id_match 3600 * 3601 * Description: This local function implements a case sensitive vid/pid 3602 * comparison as well as the boundary cases of wild card and 3603 * multiple blanks. 3604 * 3605 * Note: An implicit assumption made here is that the scsi 3606 * inquiry structure will always keep the vid, pid and 3607 * revision strings in consecutive sequence, so they can be 3608 * read as a single string. If this assumption is not the 3609 * case, a separate string, to be used for the check, needs 3610 * to be built with these strings concatenated. 3611 * 3612 * Arguments: un - driver soft state (unit) structure 3613 * id - table or config file vid/pid 3614 * idlen - length of the vid/pid (bytes) 3615 * 3616 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3617 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3618 */ 3619 3620 static int 3621 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3622 { 3623 struct scsi_inquiry *sd_inq; 3624 int rval = SD_SUCCESS; 3625 3626 ASSERT(un != NULL); 3627 sd_inq = un->un_sd->sd_inq; 3628 ASSERT(id != NULL); 3629 3630 /* 3631 * We use the inq_vid as a pointer to a buffer containing the 3632 * vid and pid and use the entire vid/pid length of the table 3633 * entry for the comparison. This works because the inq_pid 3634 * data member follows inq_vid in the scsi_inquiry structure. 3635 */ 3636 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3637 /* 3638 * The user id string is compared to the inquiry vid/pid 3639 * using a case insensitive comparison and ignoring 3640 * multiple spaces. 3641 */ 3642 rval = sd_blank_cmp(un, id, idlen); 3643 if (rval != SD_SUCCESS) { 3644 /* 3645 * User id strings that start and end with a "*" 3646 * are a special case. These do not have a 3647 * specific vendor, and the product string can 3648 * appear anywhere in the 16 byte PID portion of 3649 * the inquiry data. This is a simple strstr() 3650 * type search for the user id in the inquiry data. 3651 */ 3652 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3653 char *pidptr = &id[1]; 3654 int i; 3655 int j; 3656 int pidstrlen = idlen - 2; 3657 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3658 pidstrlen; 3659 3660 if (j < 0) { 3661 return (SD_FAILURE); 3662 } 3663 for (i = 0; i < j; i++) { 3664 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3665 pidptr, pidstrlen) == 0) { 3666 rval = SD_SUCCESS; 3667 break; 3668 } 3669 } 3670 } 3671 } 3672 } 3673 return (rval); 3674 } 3675 3676 3677 /* 3678 * Function: sd_blank_cmp 3679 * 3680 * Description: If the id string starts and ends with a space, treat 3681 * multiple consecutive spaces as equivalent to a single 3682 * space. For example, this causes a sd_disk_table entry 3683 * of " NEC CDROM " to match a device's id string of 3684 * "NEC CDROM". 3685 * 3686 * Note: The success exit condition for this routine is if 3687 * the pointer to the table entry is '\0' and the cnt of 3688 * the inquiry length is zero. This will happen if the inquiry 3689 * string returned by the device is padded with spaces to be 3690 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3691 * SCSI spec states that the inquiry string is to be padded with 3692 * spaces. 3693 * 3694 * Arguments: un - driver soft state (unit) structure 3695 * id - table or config file vid/pid 3696 * idlen - length of the vid/pid (bytes) 3697 * 3698 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3699 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3700 */ 3701 3702 static int 3703 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3704 { 3705 char *p1; 3706 char *p2; 3707 int cnt; 3708 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3709 sizeof (SD_INQUIRY(un)->inq_pid); 3710 3711 ASSERT(un != NULL); 3712 p2 = un->un_sd->sd_inq->inq_vid; 3713 ASSERT(id != NULL); 3714 p1 = id; 3715 3716 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3717 /* 3718 * Note: string p1 is terminated by a NUL but string p2 3719 * isn't. The end of p2 is determined by cnt. 3720 */ 3721 for (;;) { 3722 /* skip over any extra blanks in both strings */ 3723 while ((*p1 != '\0') && (*p1 == ' ')) { 3724 p1++; 3725 } 3726 while ((cnt != 0) && (*p2 == ' ')) { 3727 p2++; 3728 cnt--; 3729 } 3730 3731 /* compare the two strings */ 3732 if ((cnt == 0) || 3733 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3734 break; 3735 } 3736 while ((cnt > 0) && 3737 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3738 p1++; 3739 p2++; 3740 cnt--; 3741 } 3742 } 3743 } 3744 3745 /* return SD_SUCCESS if both strings match */ 3746 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3747 } 3748 3749 3750 /* 3751 * Function: sd_chk_vers1_data 3752 * 3753 * Description: Verify the version 1 device properties provided by the 3754 * user via the configuration file 3755 * 3756 * Arguments: un - driver soft state (unit) structure 3757 * flags - integer mask indicating properties to be set 3758 * prop_list - integer list of property values 3759 * list_len - length of user provided data 3760 * 3761 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3762 * SD_FAILURE - Indicates the user provided data is invalid 3763 */ 3764 3765 static int 3766 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3767 int list_len, char *dataname_ptr) 3768 { 3769 int i; 3770 int mask = 1; 3771 int index = 0; 3772 3773 ASSERT(un != NULL); 3774 3775 /* Check for a NULL property name and list */ 3776 if (dataname_ptr == NULL) { 3777 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3778 "sd_chk_vers1_data: NULL data property name."); 3779 return (SD_FAILURE); 3780 } 3781 if (prop_list == NULL) { 3782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3783 "sd_chk_vers1_data: %s NULL data property list.", 3784 dataname_ptr); 3785 return (SD_FAILURE); 3786 } 3787 3788 /* Display a warning if undefined bits are set in the flags */ 3789 if (flags & ~SD_CONF_BIT_MASK) { 3790 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3791 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3792 "Properties not set.", 3793 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3794 return (SD_FAILURE); 3795 } 3796 3797 /* 3798 * Verify the length of the list by identifying the highest bit set 3799 * in the flags and validating that the property list has a length 3800 * up to the index of this bit. 3801 */ 3802 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3803 if (flags & mask) { 3804 index++; 3805 } 3806 mask = 1 << i; 3807 } 3808 if ((list_len / sizeof (int)) < (index + 2)) { 3809 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3810 "sd_chk_vers1_data: " 3811 "Data property list %s size is incorrect. " 3812 "Properties not set.", dataname_ptr); 3813 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3814 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3815 return (SD_FAILURE); 3816 } 3817 return (SD_SUCCESS); 3818 } 3819 3820 3821 /* 3822 * Function: sd_set_vers1_properties 3823 * 3824 * Description: Set version 1 device properties based on a property list 3825 * retrieved from the driver configuration file or static 3826 * configuration table. Version 1 properties have the format: 3827 * 3828 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3829 * 3830 * where the prop0 value will be used to set prop0 if bit0 3831 * is set in the flags 3832 * 3833 * Arguments: un - driver soft state (unit) structure 3834 * flags - integer mask indicating properties to be set 3835 * prop_list - integer list of property values 3836 */ 3837 3838 static void 3839 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3840 { 3841 ASSERT(un != NULL); 3842 3843 /* 3844 * Set the flag to indicate cache is to be disabled. An attempt 3845 * to disable the cache via sd_disable_caching() will be made 3846 * later during attach once the basic initialization is complete. 3847 */ 3848 if (flags & SD_CONF_BSET_NOCACHE) { 3849 un->un_f_opt_disable_cache = TRUE; 3850 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3851 "sd_set_vers1_properties: caching disabled flag set\n"); 3852 } 3853 3854 /* CD-specific configuration parameters */ 3855 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3856 un->un_f_cfg_playmsf_bcd = TRUE; 3857 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3858 "sd_set_vers1_properties: playmsf_bcd set\n"); 3859 } 3860 if (flags & SD_CONF_BSET_READSUB_BCD) { 3861 un->un_f_cfg_readsub_bcd = TRUE; 3862 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3863 "sd_set_vers1_properties: readsub_bcd set\n"); 3864 } 3865 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3866 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3867 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3868 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3869 } 3870 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3871 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3872 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3873 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3874 } 3875 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3876 un->un_f_cfg_no_read_header = TRUE; 3877 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3878 "sd_set_vers1_properties: no_read_header set\n"); 3879 } 3880 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3881 un->un_f_cfg_read_cd_xd4 = TRUE; 3882 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3883 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3884 } 3885 3886 /* Support for devices which do not have valid/unique serial numbers */ 3887 if (flags & SD_CONF_BSET_FAB_DEVID) { 3888 un->un_f_opt_fab_devid = TRUE; 3889 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3890 "sd_set_vers1_properties: fab_devid bit set\n"); 3891 } 3892 3893 /* Support for user throttle configuration */ 3894 if (flags & SD_CONF_BSET_THROTTLE) { 3895 ASSERT(prop_list != NULL); 3896 un->un_saved_throttle = un->un_throttle = 3897 prop_list->sdt_throttle; 3898 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3899 "sd_set_vers1_properties: throttle set to %d\n", 3900 prop_list->sdt_throttle); 3901 } 3902 3903 /* Set the per disk retry count according to the conf file or table. */ 3904 if (flags & SD_CONF_BSET_NRR_COUNT) { 3905 ASSERT(prop_list != NULL); 3906 if (prop_list->sdt_not_rdy_retries) { 3907 un->un_notready_retry_count = 3908 prop_list->sdt_not_rdy_retries; 3909 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3910 "sd_set_vers1_properties: not ready retry count" 3911 " set to %d\n", un->un_notready_retry_count); 3912 } 3913 } 3914 3915 /* The controller type is reported for generic disk driver ioctls */ 3916 if (flags & SD_CONF_BSET_CTYPE) { 3917 ASSERT(prop_list != NULL); 3918 switch (prop_list->sdt_ctype) { 3919 case CTYPE_CDROM: 3920 un->un_ctype = prop_list->sdt_ctype; 3921 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3922 "sd_set_vers1_properties: ctype set to " 3923 "CTYPE_CDROM\n"); 3924 break; 3925 case CTYPE_CCS: 3926 un->un_ctype = prop_list->sdt_ctype; 3927 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3928 "sd_set_vers1_properties: ctype set to " 3929 "CTYPE_CCS\n"); 3930 break; 3931 case CTYPE_ROD: /* RW optical */ 3932 un->un_ctype = prop_list->sdt_ctype; 3933 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3934 "sd_set_vers1_properties: ctype set to " 3935 "CTYPE_ROD\n"); 3936 break; 3937 default: 3938 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3939 "sd_set_vers1_properties: Could not set " 3940 "invalid ctype value (%d)", 3941 prop_list->sdt_ctype); 3942 } 3943 } 3944 3945 /* Purple failover timeout */ 3946 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3947 ASSERT(prop_list != NULL); 3948 un->un_busy_retry_count = 3949 prop_list->sdt_busy_retries; 3950 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3951 "sd_set_vers1_properties: " 3952 "busy retry count set to %d\n", 3953 un->un_busy_retry_count); 3954 } 3955 3956 /* Purple reset retry count */ 3957 if (flags & SD_CONF_BSET_RST_RETRIES) { 3958 ASSERT(prop_list != NULL); 3959 un->un_reset_retry_count = 3960 prop_list->sdt_reset_retries; 3961 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3962 "sd_set_vers1_properties: " 3963 "reset retry count set to %d\n", 3964 un->un_reset_retry_count); 3965 } 3966 3967 /* Purple reservation release timeout */ 3968 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 3969 ASSERT(prop_list != NULL); 3970 un->un_reserve_release_time = 3971 prop_list->sdt_reserv_rel_time; 3972 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3973 "sd_set_vers1_properties: " 3974 "reservation release timeout set to %d\n", 3975 un->un_reserve_release_time); 3976 } 3977 3978 /* 3979 * Driver flag telling the driver to verify that no commands are pending 3980 * for a device before issuing a Test Unit Ready. This is a workaround 3981 * for a firmware bug in some Seagate eliteI drives. 3982 */ 3983 if (flags & SD_CONF_BSET_TUR_CHECK) { 3984 un->un_f_cfg_tur_check = TRUE; 3985 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3986 "sd_set_vers1_properties: tur queue check set\n"); 3987 } 3988 3989 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 3990 un->un_min_throttle = prop_list->sdt_min_throttle; 3991 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3992 "sd_set_vers1_properties: min throttle set to %d\n", 3993 un->un_min_throttle); 3994 } 3995 3996 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 3997 un->un_f_disksort_disabled = 3998 (prop_list->sdt_disk_sort_dis != 0) ? 3999 TRUE : FALSE; 4000 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4001 "sd_set_vers1_properties: disksort disabled " 4002 "flag set to %d\n", 4003 prop_list->sdt_disk_sort_dis); 4004 } 4005 4006 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4007 un->un_f_lun_reset_enabled = 4008 (prop_list->sdt_lun_reset_enable != 0) ? 4009 TRUE : FALSE; 4010 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4011 "sd_set_vers1_properties: lun reset enabled " 4012 "flag set to %d\n", 4013 prop_list->sdt_lun_reset_enable); 4014 } 4015 4016 /* 4017 * Validate the throttle values. 4018 * If any of the numbers are invalid, set everything to defaults. 4019 */ 4020 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4021 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4022 (un->un_min_throttle > un->un_throttle)) { 4023 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4024 un->un_min_throttle = sd_min_throttle; 4025 } 4026 } 4027 4028 /* 4029 * Function: sd_is_lsi() 4030 * 4031 * Description: Check for lsi devices, step throught the static device 4032 * table to match vid/pid. 4033 * 4034 * Args: un - ptr to sd_lun 4035 * 4036 * Notes: When creating new LSI property, need to add the new LSI property 4037 * to this function. 4038 */ 4039 static void 4040 sd_is_lsi(struct sd_lun *un) 4041 { 4042 char *id = NULL; 4043 int table_index; 4044 int idlen; 4045 void *prop; 4046 4047 ASSERT(un != NULL); 4048 for (table_index = 0; table_index < sd_disk_table_size; 4049 table_index++) { 4050 id = sd_disk_table[table_index].device_id; 4051 idlen = strlen(id); 4052 if (idlen == 0) { 4053 continue; 4054 } 4055 4056 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4057 prop = sd_disk_table[table_index].properties; 4058 if (prop == &lsi_properties || 4059 prop == &lsi_oem_properties || 4060 prop == &lsi_properties_scsi || 4061 prop == &symbios_properties) { 4062 un->un_f_cfg_is_lsi = TRUE; 4063 } 4064 break; 4065 } 4066 } 4067 } 4068 4069 4070 /* 4071 * The following routines support reading and interpretation of disk labels, 4072 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4073 * fdisk tables. 4074 */ 4075 4076 /* 4077 * Function: sd_validate_geometry 4078 * 4079 * Description: Read the label from the disk (if present). Update the unit's 4080 * geometry and vtoc information from the data in the label. 4081 * Verify that the label is valid. 4082 * 4083 * Arguments: un - driver soft state (unit) structure 4084 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4085 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4086 * to use the USCSI "direct" chain and bypass the normal 4087 * command waitq. 4088 * 4089 * Return Code: 0 - Successful completion 4090 * EINVAL - Invalid value in un->un_tgt_blocksize or 4091 * un->un_blockcount; or label on disk is corrupted 4092 * or unreadable. 4093 * EACCES - Reservation conflict at the device. 4094 * ENOMEM - Resource allocation error 4095 * ENOTSUP - geometry not applicable 4096 * 4097 * Context: Kernel thread only (can sleep). 4098 */ 4099 4100 static int 4101 sd_validate_geometry(struct sd_lun *un, int path_flag) 4102 { 4103 static char labelstring[128]; 4104 static char buf[256]; 4105 char *label = NULL; 4106 int label_error = 0; 4107 int gvalid = un->un_f_geometry_is_valid; 4108 int lbasize; 4109 uint_t capacity; 4110 int count; 4111 4112 ASSERT(un != NULL); 4113 ASSERT(mutex_owned(SD_MUTEX(un))); 4114 4115 /* 4116 * If the required values are not valid, then try getting them 4117 * once via read capacity. If that fails, then fail this call. 4118 * This is necessary with the new mpxio failover behavior in 4119 * the T300 where we can get an attach for the inactive path 4120 * before the active path. The inactive path fails commands with 4121 * sense data of 02,04,88 which happens to the read capacity 4122 * before mpxio has had sufficient knowledge to know if it should 4123 * force a fail over or not. (Which it won't do at attach anyhow). 4124 * If the read capacity at attach time fails, un_tgt_blocksize and 4125 * un_blockcount won't be valid. 4126 */ 4127 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4128 (un->un_f_blockcount_is_valid != TRUE)) { 4129 uint64_t cap; 4130 uint32_t lbasz; 4131 int rval; 4132 4133 mutex_exit(SD_MUTEX(un)); 4134 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4135 &lbasz, SD_PATH_DIRECT); 4136 mutex_enter(SD_MUTEX(un)); 4137 if (rval == 0) { 4138 /* 4139 * The following relies on 4140 * sd_send_scsi_READ_CAPACITY never 4141 * returning 0 for capacity and/or lbasize. 4142 */ 4143 sd_update_block_info(un, lbasz, cap); 4144 } 4145 4146 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4147 (un->un_f_blockcount_is_valid != TRUE)) { 4148 return (EINVAL); 4149 } 4150 } 4151 4152 /* 4153 * Copy the lbasize and capacity so that if they're reset while we're 4154 * not holding the SD_MUTEX, we will continue to use valid values 4155 * after the SD_MUTEX is reacquired. (4119659) 4156 */ 4157 lbasize = un->un_tgt_blocksize; 4158 capacity = un->un_blockcount; 4159 4160 #if defined(_SUNOS_VTOC_16) 4161 /* 4162 * Set up the "whole disk" fdisk partition; this should always 4163 * exist, regardless of whether the disk contains an fdisk table 4164 * or vtoc. 4165 */ 4166 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4167 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4168 #endif 4169 4170 /* 4171 * Refresh the logical and physical geometry caches. 4172 * (data from MODE SENSE format/rigid disk geometry pages, 4173 * and scsi_ifgetcap("geometry"). 4174 */ 4175 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4176 4177 label_error = sd_use_efi(un, path_flag); 4178 if (label_error == 0) { 4179 /* found a valid EFI label */ 4180 SD_TRACE(SD_LOG_IO_PARTITION, un, 4181 "sd_validate_geometry: found EFI label\n"); 4182 un->un_solaris_offset = 0; 4183 un->un_solaris_size = capacity; 4184 return (ENOTSUP); 4185 } 4186 if (un->un_blockcount > DK_MAX_BLOCKS) { 4187 if (label_error == ESRCH) { 4188 /* 4189 * they've configured a LUN over 1TB, but used 4190 * format.dat to restrict format's view of the 4191 * capacity to be under 1TB 4192 */ 4193 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4194 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4195 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4196 "size to be < 1TB or relabel the disk with an EFI label"); 4197 } else { 4198 /* unlabeled disk over 1TB */ 4199 return (ENOTSUP); 4200 } 4201 } 4202 label_error = 0; 4203 4204 /* 4205 * at this point it is either labeled with a VTOC or it is 4206 * under 1TB 4207 */ 4208 if (un->un_f_vtoc_label_supported) { 4209 struct dk_label *dkl; 4210 offset_t dkl1; 4211 offset_t label_addr, real_addr; 4212 int rval; 4213 size_t buffer_size; 4214 4215 /* 4216 * Note: This will set up un->un_solaris_size and 4217 * un->un_solaris_offset. 4218 */ 4219 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4220 case SD_CMD_RESERVATION_CONFLICT: 4221 ASSERT(mutex_owned(SD_MUTEX(un))); 4222 return (EACCES); 4223 case SD_CMD_FAILURE: 4224 ASSERT(mutex_owned(SD_MUTEX(un))); 4225 return (ENOMEM); 4226 } 4227 4228 if (un->un_solaris_size <= DK_LABEL_LOC) { 4229 /* 4230 * Found fdisk table but no Solaris partition entry, 4231 * so don't call sd_uselabel() and don't create 4232 * a default label. 4233 */ 4234 label_error = 0; 4235 un->un_f_geometry_is_valid = TRUE; 4236 goto no_solaris_partition; 4237 } 4238 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4239 4240 /* 4241 * sys_blocksize != tgt_blocksize, need to re-adjust 4242 * blkno and save the index to beginning of dk_label 4243 */ 4244 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4245 buffer_size = SD_REQBYTES2TGTBYTES(un, 4246 sizeof (struct dk_label)); 4247 4248 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4249 "label_addr: 0x%x allocation size: 0x%x\n", 4250 label_addr, buffer_size); 4251 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4252 if (dkl == NULL) { 4253 return (ENOMEM); 4254 } 4255 4256 mutex_exit(SD_MUTEX(un)); 4257 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4258 path_flag); 4259 mutex_enter(SD_MUTEX(un)); 4260 4261 switch (rval) { 4262 case 0: 4263 /* 4264 * sd_uselabel will establish that the geometry 4265 * is valid. 4266 * For sys_blocksize != tgt_blocksize, need 4267 * to index into the beginning of dk_label 4268 */ 4269 dkl1 = (daddr_t)dkl 4270 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4271 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4272 path_flag) != SD_LABEL_IS_VALID) { 4273 label_error = EINVAL; 4274 } 4275 break; 4276 case EACCES: 4277 label_error = EACCES; 4278 break; 4279 default: 4280 label_error = EINVAL; 4281 break; 4282 } 4283 4284 kmem_free(dkl, buffer_size); 4285 4286 #if defined(_SUNOS_VTOC_8) 4287 label = (char *)un->un_asciilabel; 4288 #elif defined(_SUNOS_VTOC_16) 4289 label = (char *)un->un_vtoc.v_asciilabel; 4290 #else 4291 #error "No VTOC format defined." 4292 #endif 4293 } 4294 4295 /* 4296 * If a valid label was not found, AND if no reservation conflict 4297 * was detected, then go ahead and create a default label (4069506). 4298 */ 4299 4300 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4301 if (un->un_f_geometry_is_valid == FALSE) { 4302 sd_build_default_label(un); 4303 } 4304 label_error = 0; 4305 } 4306 4307 no_solaris_partition: 4308 if ((!un->un_f_has_removable_media || 4309 (un->un_f_has_removable_media && 4310 un->un_mediastate == DKIO_EJECTED)) && 4311 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4312 /* 4313 * Print out a message indicating who and what we are. 4314 * We do this only when we happen to really validate the 4315 * geometry. We may call sd_validate_geometry() at other 4316 * times, e.g., ioctl()'s like Get VTOC in which case we 4317 * don't want to print the label. 4318 * If the geometry is valid, print the label string, 4319 * else print vendor and product info, if available 4320 */ 4321 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4322 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4323 } else { 4324 mutex_enter(&sd_label_mutex); 4325 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4326 labelstring); 4327 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4328 &labelstring[64]); 4329 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4330 labelstring, &labelstring[64]); 4331 if (un->un_f_blockcount_is_valid == TRUE) { 4332 (void) sprintf(&buf[strlen(buf)], 4333 ", %llu %u byte blocks\n", 4334 (longlong_t)un->un_blockcount, 4335 un->un_tgt_blocksize); 4336 } else { 4337 (void) sprintf(&buf[strlen(buf)], 4338 ", (unknown capacity)\n"); 4339 } 4340 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4341 mutex_exit(&sd_label_mutex); 4342 } 4343 } 4344 4345 #if defined(_SUNOS_VTOC_16) 4346 /* 4347 * If we have valid geometry, set up the remaining fdisk partitions. 4348 * Note that dkl_cylno is not used for the fdisk map entries, so 4349 * we set it to an entirely bogus value. 4350 */ 4351 for (count = 0; count < FD_NUMPART; count++) { 4352 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4353 un->un_map[FDISK_P1 + count].dkl_nblk = 4354 un->un_fmap[count].fmap_nblk; 4355 4356 un->un_offset[FDISK_P1 + count] = 4357 un->un_fmap[count].fmap_start; 4358 } 4359 #endif 4360 4361 for (count = 0; count < NDKMAP; count++) { 4362 #if defined(_SUNOS_VTOC_8) 4363 struct dk_map *lp = &un->un_map[count]; 4364 un->un_offset[count] = 4365 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4366 #elif defined(_SUNOS_VTOC_16) 4367 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4368 4369 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4370 #else 4371 #error "No VTOC format defined." 4372 #endif 4373 } 4374 4375 return (label_error); 4376 } 4377 4378 4379 #if defined(_SUNOS_VTOC_16) 4380 /* 4381 * Macro: MAX_BLKS 4382 * 4383 * This macro is used for table entries where we need to have the largest 4384 * possible sector value for that head & SPT (sectors per track) 4385 * combination. Other entries for some smaller disk sizes are set by 4386 * convention to match those used by X86 BIOS usage. 4387 */ 4388 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4389 4390 /* 4391 * Function: sd_convert_geometry 4392 * 4393 * Description: Convert physical geometry into a dk_geom structure. In 4394 * other words, make sure we don't wrap 16-bit values. 4395 * e.g. converting from geom_cache to dk_geom 4396 * 4397 * Context: Kernel thread only 4398 */ 4399 static void 4400 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4401 { 4402 int i; 4403 static const struct chs_values { 4404 uint_t max_cap; /* Max Capacity for this HS. */ 4405 uint_t nhead; /* Heads to use. */ 4406 uint_t nsect; /* SPT to use. */ 4407 } CHS_values[] = { 4408 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4409 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4410 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4411 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4412 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4413 }; 4414 4415 /* Unlabeled SCSI floppy device */ 4416 if (capacity <= 0x1000) { 4417 un_g->dkg_nhead = 2; 4418 un_g->dkg_ncyl = 80; 4419 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4420 return; 4421 } 4422 4423 /* 4424 * For all devices we calculate cylinders using the 4425 * heads and sectors we assign based on capacity of the 4426 * device. The table is designed to be compatible with the 4427 * way other operating systems lay out fdisk tables for X86 4428 * and to insure that the cylinders never exceed 65535 to 4429 * prevent problems with X86 ioctls that report geometry. 4430 * We use SPT that are multiples of 63, since other OSes that 4431 * are not limited to 16-bits for cylinders stop at 63 SPT 4432 * we make do by using multiples of 63 SPT. 4433 * 4434 * Note than capacities greater than or equal to 1TB will simply 4435 * get the largest geometry from the table. This should be okay 4436 * since disks this large shouldn't be using CHS values anyway. 4437 */ 4438 for (i = 0; CHS_values[i].max_cap < capacity && 4439 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4440 ; 4441 4442 un_g->dkg_nhead = CHS_values[i].nhead; 4443 un_g->dkg_nsect = CHS_values[i].nsect; 4444 } 4445 #endif 4446 4447 4448 /* 4449 * Function: sd_resync_geom_caches 4450 * 4451 * Description: (Re)initialize both geometry caches: the virtual geometry 4452 * information is extracted from the HBA (the "geometry" 4453 * capability), and the physical geometry cache data is 4454 * generated by issuing MODE SENSE commands. 4455 * 4456 * Arguments: un - driver soft state (unit) structure 4457 * capacity - disk capacity in #blocks 4458 * lbasize - disk block size in bytes 4459 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4460 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4461 * to use the USCSI "direct" chain and bypass the normal 4462 * command waitq. 4463 * 4464 * Context: Kernel thread only (can sleep). 4465 */ 4466 4467 static void 4468 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4469 int path_flag) 4470 { 4471 struct geom_cache pgeom; 4472 struct geom_cache *pgeom_p = &pgeom; 4473 int spc; 4474 unsigned short nhead; 4475 unsigned short nsect; 4476 4477 ASSERT(un != NULL); 4478 ASSERT(mutex_owned(SD_MUTEX(un))); 4479 4480 /* 4481 * Ask the controller for its logical geometry. 4482 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4483 * then the lgeom cache will be invalid. 4484 */ 4485 sd_get_virtual_geometry(un, capacity, lbasize); 4486 4487 /* 4488 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4489 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4490 */ 4491 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4492 /* 4493 * Note: Perhaps this needs to be more adaptive? The rationale 4494 * is that, if there's no HBA geometry from the HBA driver, any 4495 * guess is good, since this is the physical geometry. If MODE 4496 * SENSE fails this gives a max cylinder size for non-LBA access 4497 */ 4498 nhead = 255; 4499 nsect = 63; 4500 } else { 4501 nhead = un->un_lgeom.g_nhead; 4502 nsect = un->un_lgeom.g_nsect; 4503 } 4504 4505 if (ISCD(un)) { 4506 pgeom_p->g_nhead = 1; 4507 pgeom_p->g_nsect = nsect * nhead; 4508 } else { 4509 pgeom_p->g_nhead = nhead; 4510 pgeom_p->g_nsect = nsect; 4511 } 4512 4513 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4514 pgeom_p->g_capacity = capacity; 4515 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4516 pgeom_p->g_acyl = 0; 4517 4518 /* 4519 * Retrieve fresh geometry data from the hardware, stash it 4520 * here temporarily before we rebuild the incore label. 4521 * 4522 * We want to use the MODE SENSE commands to derive the 4523 * physical geometry of the device, but if either command 4524 * fails, the logical geometry is used as the fallback for 4525 * disk label geometry. 4526 */ 4527 mutex_exit(SD_MUTEX(un)); 4528 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4529 mutex_enter(SD_MUTEX(un)); 4530 4531 /* 4532 * Now update the real copy while holding the mutex. This 4533 * way the global copy is never in an inconsistent state. 4534 */ 4535 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4536 4537 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4538 "(cached from lgeom)\n"); 4539 SD_INFO(SD_LOG_COMMON, un, 4540 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4541 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4542 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4543 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4544 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4545 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4546 un->un_pgeom.g_rpm); 4547 } 4548 4549 4550 /* 4551 * Function: sd_read_fdisk 4552 * 4553 * Description: utility routine to read the fdisk table. 4554 * 4555 * Arguments: un - driver soft state (unit) structure 4556 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4557 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4558 * to use the USCSI "direct" chain and bypass the normal 4559 * command waitq. 4560 * 4561 * Return Code: SD_CMD_SUCCESS 4562 * SD_CMD_FAILURE 4563 * 4564 * Context: Kernel thread only (can sleep). 4565 */ 4566 /* ARGSUSED */ 4567 static int 4568 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4569 { 4570 #if defined(_NO_FDISK_PRESENT) 4571 4572 un->un_solaris_offset = 0; 4573 un->un_solaris_size = capacity; 4574 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4575 return (SD_CMD_SUCCESS); 4576 4577 #elif defined(_FIRMWARE_NEEDS_FDISK) 4578 4579 struct ipart *fdp; 4580 struct mboot *mbp; 4581 struct ipart fdisk[FD_NUMPART]; 4582 int i; 4583 char sigbuf[2]; 4584 caddr_t bufp; 4585 int uidx; 4586 int rval; 4587 int lba = 0; 4588 uint_t solaris_offset; /* offset to solaris part. */ 4589 daddr_t solaris_size; /* size of solaris partition */ 4590 uint32_t blocksize; 4591 4592 ASSERT(un != NULL); 4593 ASSERT(mutex_owned(SD_MUTEX(un))); 4594 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4595 4596 blocksize = un->un_tgt_blocksize; 4597 4598 /* 4599 * Start off assuming no fdisk table 4600 */ 4601 solaris_offset = 0; 4602 solaris_size = capacity; 4603 4604 mutex_exit(SD_MUTEX(un)); 4605 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4606 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4607 mutex_enter(SD_MUTEX(un)); 4608 4609 if (rval != 0) { 4610 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4611 "sd_read_fdisk: fdisk read err\n"); 4612 kmem_free(bufp, blocksize); 4613 return (SD_CMD_FAILURE); 4614 } 4615 4616 mbp = (struct mboot *)bufp; 4617 4618 /* 4619 * The fdisk table does not begin on a 4-byte boundary within the 4620 * master boot record, so we copy it to an aligned structure to avoid 4621 * alignment exceptions on some processors. 4622 */ 4623 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4624 4625 /* 4626 * Check for lba support before verifying sig; sig might not be 4627 * there, say on a blank disk, but the max_chs mark may still 4628 * be present. 4629 * 4630 * Note: LBA support and BEFs are an x86-only concept but this 4631 * code should work OK on SPARC as well. 4632 */ 4633 4634 /* 4635 * First, check for lba-access-ok on root node (or prom root node) 4636 * if present there, don't need to search fdisk table. 4637 */ 4638 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4639 "lba-access-ok", 0) != 0) { 4640 /* All drives do LBA; don't search fdisk table */ 4641 lba = 1; 4642 } else { 4643 /* Okay, look for mark in fdisk table */ 4644 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4645 /* accumulate "lba" value from all partitions */ 4646 lba = (lba || sd_has_max_chs_vals(fdp)); 4647 } 4648 } 4649 4650 if (lba != 0) { 4651 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4652 4653 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4654 "lba-access-ok", 0) == 0) { 4655 /* not found; create it */ 4656 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4657 "lba-access-ok", (caddr_t)NULL, 0) != 4658 DDI_PROP_SUCCESS) { 4659 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4660 "sd_read_fdisk: Can't create lba property " 4661 "for instance %d\n", 4662 ddi_get_instance(SD_DEVINFO(un))); 4663 } 4664 } 4665 } 4666 4667 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4668 4669 /* 4670 * Endian-independent signature check 4671 */ 4672 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4673 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4674 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4675 "sd_read_fdisk: no fdisk\n"); 4676 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4677 rval = SD_CMD_SUCCESS; 4678 goto done; 4679 } 4680 4681 #ifdef SDDEBUG 4682 if (sd_level_mask & SD_LOGMASK_INFO) { 4683 fdp = fdisk; 4684 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4685 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4686 "numsect sysid bootid\n"); 4687 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4688 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4689 " %d: %8d %8d 0x%08x 0x%08x\n", 4690 i, fdp->relsect, fdp->numsect, 4691 fdp->systid, fdp->bootid); 4692 } 4693 } 4694 #endif 4695 4696 /* 4697 * Try to find the unix partition 4698 */ 4699 uidx = -1; 4700 solaris_offset = 0; 4701 solaris_size = 0; 4702 4703 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4704 int relsect; 4705 int numsect; 4706 4707 if (fdp->numsect == 0) { 4708 un->un_fmap[i].fmap_start = 0; 4709 un->un_fmap[i].fmap_nblk = 0; 4710 continue; 4711 } 4712 4713 /* 4714 * Data in the fdisk table is little-endian. 4715 */ 4716 relsect = LE_32(fdp->relsect); 4717 numsect = LE_32(fdp->numsect); 4718 4719 un->un_fmap[i].fmap_start = relsect; 4720 un->un_fmap[i].fmap_nblk = numsect; 4721 4722 if (fdp->systid != SUNIXOS && 4723 fdp->systid != SUNIXOS2 && 4724 fdp->systid != EFI_PMBR) { 4725 continue; 4726 } 4727 4728 /* 4729 * use the last active solaris partition id found 4730 * (there should only be 1 active partition id) 4731 * 4732 * if there are no active solaris partition id 4733 * then use the first inactive solaris partition id 4734 */ 4735 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4736 uidx = i; 4737 solaris_offset = relsect; 4738 solaris_size = numsect; 4739 } 4740 } 4741 4742 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4743 un->un_solaris_offset, un->un_solaris_size); 4744 4745 rval = SD_CMD_SUCCESS; 4746 4747 done: 4748 4749 /* 4750 * Clear the VTOC info, only if the Solaris partition entry 4751 * has moved, changed size, been deleted, or if the size of 4752 * the partition is too small to even fit the label sector. 4753 */ 4754 if ((un->un_solaris_offset != solaris_offset) || 4755 (un->un_solaris_size != solaris_size) || 4756 solaris_size <= DK_LABEL_LOC) { 4757 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4758 solaris_offset, solaris_size); 4759 bzero(&un->un_g, sizeof (struct dk_geom)); 4760 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4761 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4762 un->un_f_geometry_is_valid = FALSE; 4763 } 4764 un->un_solaris_offset = solaris_offset; 4765 un->un_solaris_size = solaris_size; 4766 kmem_free(bufp, blocksize); 4767 return (rval); 4768 4769 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4770 #error "fdisk table presence undetermined for this platform." 4771 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4772 } 4773 4774 4775 /* 4776 * Function: sd_get_physical_geometry 4777 * 4778 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4779 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4780 * target, and use this information to initialize the physical 4781 * geometry cache specified by pgeom_p. 4782 * 4783 * MODE SENSE is an optional command, so failure in this case 4784 * does not necessarily denote an error. We want to use the 4785 * MODE SENSE commands to derive the physical geometry of the 4786 * device, but if either command fails, the logical geometry is 4787 * used as the fallback for disk label geometry. 4788 * 4789 * This requires that un->un_blockcount and un->un_tgt_blocksize 4790 * have already been initialized for the current target and 4791 * that the current values be passed as args so that we don't 4792 * end up ever trying to use -1 as a valid value. This could 4793 * happen if either value is reset while we're not holding 4794 * the mutex. 4795 * 4796 * Arguments: un - driver soft state (unit) structure 4797 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4798 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4799 * to use the USCSI "direct" chain and bypass the normal 4800 * command waitq. 4801 * 4802 * Context: Kernel thread only (can sleep). 4803 */ 4804 4805 static void 4806 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4807 int capacity, int lbasize, int path_flag) 4808 { 4809 struct mode_format *page3p; 4810 struct mode_geometry *page4p; 4811 struct mode_header *headerp; 4812 int sector_size; 4813 int nsect; 4814 int nhead; 4815 int ncyl; 4816 int intrlv; 4817 int spc; 4818 int modesense_capacity; 4819 int rpm; 4820 int bd_len; 4821 int mode_header_length; 4822 uchar_t *p3bufp; 4823 uchar_t *p4bufp; 4824 int cdbsize; 4825 4826 ASSERT(un != NULL); 4827 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4828 4829 if (un->un_f_blockcount_is_valid != TRUE) { 4830 return; 4831 } 4832 4833 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4834 return; 4835 } 4836 4837 if (lbasize == 0) { 4838 if (ISCD(un)) { 4839 lbasize = 2048; 4840 } else { 4841 lbasize = un->un_sys_blocksize; 4842 } 4843 } 4844 pgeom_p->g_secsize = (unsigned short)lbasize; 4845 4846 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4847 4848 /* 4849 * Retrieve MODE SENSE page 3 - Format Device Page 4850 */ 4851 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4852 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4853 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4854 != 0) { 4855 SD_ERROR(SD_LOG_COMMON, un, 4856 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4857 goto page3_exit; 4858 } 4859 4860 /* 4861 * Determine size of Block Descriptors in order to locate the mode 4862 * page data. ATAPI devices return 0, SCSI devices should return 4863 * MODE_BLK_DESC_LENGTH. 4864 */ 4865 headerp = (struct mode_header *)p3bufp; 4866 if (un->un_f_cfg_is_atapi == TRUE) { 4867 struct mode_header_grp2 *mhp = 4868 (struct mode_header_grp2 *)headerp; 4869 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4870 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4871 } else { 4872 mode_header_length = MODE_HEADER_LENGTH; 4873 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4874 } 4875 4876 if (bd_len > MODE_BLK_DESC_LENGTH) { 4877 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4878 "received unexpected bd_len of %d, page3\n", bd_len); 4879 goto page3_exit; 4880 } 4881 4882 page3p = (struct mode_format *) 4883 ((caddr_t)headerp + mode_header_length + bd_len); 4884 4885 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4886 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4887 "mode sense pg3 code mismatch %d\n", 4888 page3p->mode_page.code); 4889 goto page3_exit; 4890 } 4891 4892 /* 4893 * Use this physical geometry data only if BOTH MODE SENSE commands 4894 * complete successfully; otherwise, revert to the logical geometry. 4895 * So, we need to save everything in temporary variables. 4896 */ 4897 sector_size = BE_16(page3p->data_bytes_sect); 4898 4899 /* 4900 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4901 */ 4902 if (sector_size == 0) { 4903 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 4904 } else { 4905 sector_size &= ~(un->un_sys_blocksize - 1); 4906 } 4907 4908 nsect = BE_16(page3p->sect_track); 4909 intrlv = BE_16(page3p->interleave); 4910 4911 SD_INFO(SD_LOG_COMMON, un, 4912 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4913 SD_INFO(SD_LOG_COMMON, un, 4914 " mode page: %d; nsect: %d; sector size: %d;\n", 4915 page3p->mode_page.code, nsect, sector_size); 4916 SD_INFO(SD_LOG_COMMON, un, 4917 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4918 BE_16(page3p->track_skew), 4919 BE_16(page3p->cylinder_skew)); 4920 4921 4922 /* 4923 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4924 */ 4925 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4926 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4927 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4928 != 0) { 4929 SD_ERROR(SD_LOG_COMMON, un, 4930 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4931 goto page4_exit; 4932 } 4933 4934 /* 4935 * Determine size of Block Descriptors in order to locate the mode 4936 * page data. ATAPI devices return 0, SCSI devices should return 4937 * MODE_BLK_DESC_LENGTH. 4938 */ 4939 headerp = (struct mode_header *)p4bufp; 4940 if (un->un_f_cfg_is_atapi == TRUE) { 4941 struct mode_header_grp2 *mhp = 4942 (struct mode_header_grp2 *)headerp; 4943 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4944 } else { 4945 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4946 } 4947 4948 if (bd_len > MODE_BLK_DESC_LENGTH) { 4949 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4950 "received unexpected bd_len of %d, page4\n", bd_len); 4951 goto page4_exit; 4952 } 4953 4954 page4p = (struct mode_geometry *) 4955 ((caddr_t)headerp + mode_header_length + bd_len); 4956 4957 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4958 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4959 "mode sense pg4 code mismatch %d\n", 4960 page4p->mode_page.code); 4961 goto page4_exit; 4962 } 4963 4964 /* 4965 * Stash the data now, after we know that both commands completed. 4966 */ 4967 4968 mutex_enter(SD_MUTEX(un)); 4969 4970 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4971 spc = nhead * nsect; 4972 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4973 rpm = BE_16(page4p->rpm); 4974 4975 modesense_capacity = spc * ncyl; 4976 4977 SD_INFO(SD_LOG_COMMON, un, 4978 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4979 SD_INFO(SD_LOG_COMMON, un, 4980 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4981 SD_INFO(SD_LOG_COMMON, un, 4982 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4983 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4984 (void *)pgeom_p, capacity); 4985 4986 /* 4987 * Compensate if the drive's geometry is not rectangular, i.e., 4988 * the product of C * H * S returned by MODE SENSE >= that returned 4989 * by read capacity. This is an idiosyncrasy of the original x86 4990 * disk subsystem. 4991 */ 4992 if (modesense_capacity >= capacity) { 4993 SD_INFO(SD_LOG_COMMON, un, 4994 "sd_get_physical_geometry: adjusting acyl; " 4995 "old: %d; new: %d\n", pgeom_p->g_acyl, 4996 (modesense_capacity - capacity + spc - 1) / spc); 4997 if (sector_size != 0) { 4998 /* 1243403: NEC D38x7 drives don't support sec size */ 4999 pgeom_p->g_secsize = (unsigned short)sector_size; 5000 } 5001 pgeom_p->g_nsect = (unsigned short)nsect; 5002 pgeom_p->g_nhead = (unsigned short)nhead; 5003 pgeom_p->g_capacity = capacity; 5004 pgeom_p->g_acyl = 5005 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5006 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5007 } 5008 5009 pgeom_p->g_rpm = (unsigned short)rpm; 5010 pgeom_p->g_intrlv = (unsigned short)intrlv; 5011 5012 SD_INFO(SD_LOG_COMMON, un, 5013 "sd_get_physical_geometry: mode sense geometry:\n"); 5014 SD_INFO(SD_LOG_COMMON, un, 5015 " nsect: %d; sector size: %d; interlv: %d\n", 5016 nsect, sector_size, intrlv); 5017 SD_INFO(SD_LOG_COMMON, un, 5018 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5019 nhead, ncyl, rpm, modesense_capacity); 5020 SD_INFO(SD_LOG_COMMON, un, 5021 "sd_get_physical_geometry: (cached)\n"); 5022 SD_INFO(SD_LOG_COMMON, un, 5023 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5024 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5025 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5026 SD_INFO(SD_LOG_COMMON, un, 5027 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5028 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5029 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5030 5031 mutex_exit(SD_MUTEX(un)); 5032 5033 page4_exit: 5034 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5035 page3_exit: 5036 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5037 } 5038 5039 5040 /* 5041 * Function: sd_get_virtual_geometry 5042 * 5043 * Description: Ask the controller to tell us about the target device. 5044 * 5045 * Arguments: un - pointer to softstate 5046 * capacity - disk capacity in #blocks 5047 * lbasize - disk block size in bytes 5048 * 5049 * Context: Kernel thread only 5050 */ 5051 5052 static void 5053 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5054 { 5055 struct geom_cache *lgeom_p = &un->un_lgeom; 5056 uint_t geombuf; 5057 int spc; 5058 5059 ASSERT(un != NULL); 5060 ASSERT(mutex_owned(SD_MUTEX(un))); 5061 5062 mutex_exit(SD_MUTEX(un)); 5063 5064 /* Set sector size, and total number of sectors */ 5065 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5066 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5067 5068 /* Let the HBA tell us its geometry */ 5069 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5070 5071 mutex_enter(SD_MUTEX(un)); 5072 5073 /* A value of -1 indicates an undefined "geometry" property */ 5074 if (geombuf == (-1)) { 5075 return; 5076 } 5077 5078 /* Initialize the logical geometry cache. */ 5079 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5080 lgeom_p->g_nsect = geombuf & 0xffff; 5081 lgeom_p->g_secsize = un->un_sys_blocksize; 5082 5083 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5084 5085 /* 5086 * Note: The driver originally converted the capacity value from 5087 * target blocks to system blocks. However, the capacity value passed 5088 * to this routine is already in terms of system blocks (this scaling 5089 * is done when the READ CAPACITY command is issued and processed). 5090 * This 'error' may have gone undetected because the usage of g_ncyl 5091 * (which is based upon g_capacity) is very limited within the driver 5092 */ 5093 lgeom_p->g_capacity = capacity; 5094 5095 /* 5096 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5097 * hba may return zero values if the device has been removed. 5098 */ 5099 if (spc == 0) { 5100 lgeom_p->g_ncyl = 0; 5101 } else { 5102 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5103 } 5104 lgeom_p->g_acyl = 0; 5105 5106 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5107 SD_INFO(SD_LOG_COMMON, un, 5108 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5109 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5110 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5111 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5112 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5113 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5114 } 5115 5116 5117 /* 5118 * Function: sd_update_block_info 5119 * 5120 * Description: Calculate a byte count to sector count bitshift value 5121 * from sector size. 5122 * 5123 * Arguments: un: unit struct. 5124 * lbasize: new target sector size 5125 * capacity: new target capacity, ie. block count 5126 * 5127 * Context: Kernel thread context 5128 */ 5129 5130 static void 5131 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5132 { 5133 if (lbasize != 0) { 5134 un->un_tgt_blocksize = lbasize; 5135 un->un_f_tgt_blocksize_is_valid = TRUE; 5136 } 5137 5138 if (capacity != 0) { 5139 un->un_blockcount = capacity; 5140 un->un_f_blockcount_is_valid = TRUE; 5141 } 5142 } 5143 5144 5145 static void 5146 sd_swap_efi_gpt(efi_gpt_t *e) 5147 { 5148 _NOTE(ASSUMING_PROTECTED(*e)) 5149 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5150 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5151 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5152 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5153 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5154 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5155 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5156 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5157 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5158 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5159 e->efi_gpt_NumberOfPartitionEntries = 5160 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5161 e->efi_gpt_SizeOfPartitionEntry = 5162 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5163 e->efi_gpt_PartitionEntryArrayCRC32 = 5164 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5165 } 5166 5167 static void 5168 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5169 { 5170 int i; 5171 5172 _NOTE(ASSUMING_PROTECTED(*p)) 5173 for (i = 0; i < nparts; i++) { 5174 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5175 p[i].efi_gpe_PartitionTypeGUID); 5176 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5177 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5178 /* PartitionAttrs */ 5179 } 5180 } 5181 5182 static int 5183 sd_validate_efi(efi_gpt_t *labp) 5184 { 5185 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5186 return (EINVAL); 5187 /* at least 96 bytes in this version of the spec. */ 5188 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5189 labp->efi_gpt_HeaderSize) 5190 return (EINVAL); 5191 /* this should be 128 bytes */ 5192 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5193 return (EINVAL); 5194 return (0); 5195 } 5196 5197 static int 5198 sd_use_efi(struct sd_lun *un, int path_flag) 5199 { 5200 int i; 5201 int rval = 0; 5202 efi_gpe_t *partitions; 5203 uchar_t *buf; 5204 uint_t lbasize; 5205 uint64_t cap; 5206 uint_t nparts; 5207 diskaddr_t gpe_lba; 5208 5209 ASSERT(mutex_owned(SD_MUTEX(un))); 5210 lbasize = un->un_tgt_blocksize; 5211 5212 mutex_exit(SD_MUTEX(un)); 5213 5214 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5215 5216 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5217 rval = EINVAL; 5218 goto done_err; 5219 } 5220 5221 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5222 if (rval) { 5223 goto done_err; 5224 } 5225 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5226 /* not ours */ 5227 rval = ESRCH; 5228 goto done_err; 5229 } 5230 5231 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5232 if (rval) { 5233 goto done_err; 5234 } 5235 sd_swap_efi_gpt((efi_gpt_t *)buf); 5236 5237 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5238 /* 5239 * Couldn't read the primary, try the backup. Our 5240 * capacity at this point could be based on CHS, so 5241 * check what the device reports. 5242 */ 5243 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5244 path_flag); 5245 if (rval) { 5246 goto done_err; 5247 } 5248 5249 /* 5250 * The MMC standard allows READ CAPACITY to be 5251 * inaccurate by a bounded amount (in the interest of 5252 * response latency). As a result, failed READs are 5253 * commonplace (due to the reading of metadata and not 5254 * data). Depending on the per-Vendor/drive Sense data, 5255 * the failed READ can cause many (unnecessary) retries. 5256 */ 5257 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5258 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5259 path_flag)) != 0) { 5260 goto done_err; 5261 } 5262 5263 sd_swap_efi_gpt((efi_gpt_t *)buf); 5264 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5265 goto done_err; 5266 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5267 "primary label corrupt; using backup\n"); 5268 } 5269 5270 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5271 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5272 5273 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5274 path_flag); 5275 if (rval) { 5276 goto done_err; 5277 } 5278 partitions = (efi_gpe_t *)buf; 5279 5280 if (nparts > MAXPART) { 5281 nparts = MAXPART; 5282 } 5283 sd_swap_efi_gpe(nparts, partitions); 5284 5285 mutex_enter(SD_MUTEX(un)); 5286 5287 /* Fill in partition table. */ 5288 for (i = 0; i < nparts; i++) { 5289 if (partitions->efi_gpe_StartingLBA != 0 || 5290 partitions->efi_gpe_EndingLBA != 0) { 5291 un->un_map[i].dkl_cylno = 5292 partitions->efi_gpe_StartingLBA; 5293 un->un_map[i].dkl_nblk = 5294 partitions->efi_gpe_EndingLBA - 5295 partitions->efi_gpe_StartingLBA + 1; 5296 un->un_offset[i] = 5297 partitions->efi_gpe_StartingLBA; 5298 } 5299 if (i == WD_NODE) { 5300 /* 5301 * minor number 7 corresponds to the whole disk 5302 */ 5303 un->un_map[i].dkl_cylno = 0; 5304 un->un_map[i].dkl_nblk = un->un_blockcount; 5305 un->un_offset[i] = 0; 5306 } 5307 partitions++; 5308 } 5309 un->un_solaris_offset = 0; 5310 un->un_solaris_size = cap; 5311 un->un_f_geometry_is_valid = TRUE; 5312 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5313 return (0); 5314 5315 done_err: 5316 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5317 mutex_enter(SD_MUTEX(un)); 5318 /* 5319 * if we didn't find something that could look like a VTOC 5320 * and the disk is over 1TB, we know there isn't a valid label. 5321 * Otherwise let sd_uselabel decide what to do. We only 5322 * want to invalidate this if we're certain the label isn't 5323 * valid because sd_prop_op will now fail, which in turn 5324 * causes things like opens and stats on the partition to fail. 5325 */ 5326 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5327 un->un_f_geometry_is_valid = FALSE; 5328 } 5329 return (rval); 5330 } 5331 5332 5333 /* 5334 * Function: sd_uselabel 5335 * 5336 * Description: Validate the disk label and update the relevant data (geometry, 5337 * partition, vtoc, and capacity data) in the sd_lun struct. 5338 * Marks the geometry of the unit as being valid. 5339 * 5340 * Arguments: un: unit struct. 5341 * dk_label: disk label 5342 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5343 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5344 * to use the USCSI "direct" chain and bypass the normal 5345 * command waitq. 5346 * 5347 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5348 * partition, vtoc, and capacity data are good. 5349 * 5350 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5351 * label; or computed capacity does not jibe with capacity 5352 * reported from the READ CAPACITY command. 5353 * 5354 * Context: Kernel thread only (can sleep). 5355 */ 5356 5357 static int 5358 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5359 { 5360 short *sp; 5361 short sum; 5362 short count; 5363 int label_error = SD_LABEL_IS_VALID; 5364 int i; 5365 int capacity; 5366 int part_end; 5367 int track_capacity; 5368 int err; 5369 #if defined(_SUNOS_VTOC_16) 5370 struct dkl_partition *vpartp; 5371 #endif 5372 ASSERT(un != NULL); 5373 ASSERT(mutex_owned(SD_MUTEX(un))); 5374 5375 /* Validate the magic number of the label. */ 5376 if (labp->dkl_magic != DKL_MAGIC) { 5377 #if defined(__sparc) 5378 if ((un->un_state == SD_STATE_NORMAL) && 5379 un->un_f_vtoc_errlog_supported) { 5380 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5381 "Corrupt label; wrong magic number\n"); 5382 } 5383 #endif 5384 return (SD_LABEL_IS_INVALID); 5385 } 5386 5387 /* Validate the checksum of the label. */ 5388 sp = (short *)labp; 5389 sum = 0; 5390 count = sizeof (struct dk_label) / sizeof (short); 5391 while (count--) { 5392 sum ^= *sp++; 5393 } 5394 5395 if (sum != 0) { 5396 #if defined(_SUNOS_VTOC_16) 5397 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5398 #elif defined(_SUNOS_VTOC_8) 5399 if ((un->un_state == SD_STATE_NORMAL) && 5400 un->un_f_vtoc_errlog_supported) { 5401 #endif 5402 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5403 "Corrupt label - label checksum failed\n"); 5404 } 5405 return (SD_LABEL_IS_INVALID); 5406 } 5407 5408 5409 /* 5410 * Fill in geometry structure with data from label. 5411 */ 5412 bzero(&un->un_g, sizeof (struct dk_geom)); 5413 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5414 un->un_g.dkg_acyl = labp->dkl_acyl; 5415 un->un_g.dkg_bcyl = 0; 5416 un->un_g.dkg_nhead = labp->dkl_nhead; 5417 un->un_g.dkg_nsect = labp->dkl_nsect; 5418 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5419 5420 #if defined(_SUNOS_VTOC_8) 5421 un->un_g.dkg_gap1 = labp->dkl_gap1; 5422 un->un_g.dkg_gap2 = labp->dkl_gap2; 5423 un->un_g.dkg_bhead = labp->dkl_bhead; 5424 #endif 5425 #if defined(_SUNOS_VTOC_16) 5426 un->un_dkg_skew = labp->dkl_skew; 5427 #endif 5428 5429 #if defined(__i386) || defined(__amd64) 5430 un->un_g.dkg_apc = labp->dkl_apc; 5431 #endif 5432 5433 /* 5434 * Currently we rely on the values in the label being accurate. If 5435 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5436 * 5437 * Note: In the future a MODE SENSE may be used to retrieve this data, 5438 * although this command is optional in SCSI-2. 5439 */ 5440 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5441 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5442 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5443 5444 /* 5445 * The Read and Write reinstruct values may not be valid 5446 * for older disks. 5447 */ 5448 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5449 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5450 5451 /* Fill in partition table. */ 5452 #if defined(_SUNOS_VTOC_8) 5453 for (i = 0; i < NDKMAP; i++) { 5454 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5455 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5456 } 5457 #endif 5458 #if defined(_SUNOS_VTOC_16) 5459 vpartp = labp->dkl_vtoc.v_part; 5460 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5461 5462 /* Prevent divide by zero */ 5463 if (track_capacity == 0) { 5464 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5465 "Corrupt label - zero nhead or nsect value\n"); 5466 5467 return (SD_LABEL_IS_INVALID); 5468 } 5469 5470 for (i = 0; i < NDKMAP; i++, vpartp++) { 5471 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5472 un->un_map[i].dkl_nblk = vpartp->p_size; 5473 } 5474 #endif 5475 5476 /* Fill in VTOC Structure. */ 5477 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5478 #if defined(_SUNOS_VTOC_8) 5479 /* 5480 * The 8-slice vtoc does not include the ascii label; save it into 5481 * the device's soft state structure here. 5482 */ 5483 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5484 #endif 5485 5486 /* Now look for a valid capacity. */ 5487 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5488 capacity = (un->un_g.dkg_ncyl * track_capacity); 5489 5490 if (un->un_g.dkg_acyl) { 5491 #if defined(__i386) || defined(__amd64) 5492 /* we may have > 1 alts cylinder */ 5493 capacity += (track_capacity * un->un_g.dkg_acyl); 5494 #else 5495 capacity += track_capacity; 5496 #endif 5497 } 5498 5499 /* 5500 * Force check here to ensure the computed capacity is valid. 5501 * If capacity is zero, it indicates an invalid label and 5502 * we should abort updating the relevant data then. 5503 */ 5504 if (capacity == 0) { 5505 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5506 "Corrupt label - no valid capacity could be retrieved\n"); 5507 5508 return (SD_LABEL_IS_INVALID); 5509 } 5510 5511 /* Mark the geometry as valid. */ 5512 un->un_f_geometry_is_valid = TRUE; 5513 5514 /* 5515 * At this point, un->un_blockcount should contain valid data from 5516 * the READ CAPACITY command. 5517 */ 5518 if (un->un_f_blockcount_is_valid != TRUE) { 5519 /* 5520 * We have a situation where the target didn't give us a good 5521 * READ CAPACITY value, yet there appears to be a valid label. 5522 * In this case, we'll fake the capacity. 5523 */ 5524 un->un_blockcount = capacity; 5525 un->un_f_blockcount_is_valid = TRUE; 5526 goto done; 5527 } 5528 5529 5530 if ((capacity <= un->un_blockcount) || 5531 (un->un_state != SD_STATE_NORMAL)) { 5532 #if defined(_SUNOS_VTOC_8) 5533 /* 5534 * We can't let this happen on drives that are subdivided 5535 * into logical disks (i.e., that have an fdisk table). 5536 * The un_blockcount field should always hold the full media 5537 * size in sectors, period. This code would overwrite 5538 * un_blockcount with the size of the Solaris fdisk partition. 5539 */ 5540 SD_ERROR(SD_LOG_COMMON, un, 5541 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5542 capacity, un->un_blockcount); 5543 un->un_blockcount = capacity; 5544 un->un_f_blockcount_is_valid = TRUE; 5545 #endif /* defined(_SUNOS_VTOC_8) */ 5546 goto done; 5547 } 5548 5549 if (ISCD(un)) { 5550 /* For CDROMs, we trust that the data in the label is OK. */ 5551 #if defined(_SUNOS_VTOC_8) 5552 for (i = 0; i < NDKMAP; i++) { 5553 part_end = labp->dkl_nhead * labp->dkl_nsect * 5554 labp->dkl_map[i].dkl_cylno + 5555 labp->dkl_map[i].dkl_nblk - 1; 5556 5557 if ((labp->dkl_map[i].dkl_nblk) && 5558 (part_end > un->un_blockcount)) { 5559 un->un_f_geometry_is_valid = FALSE; 5560 break; 5561 } 5562 } 5563 #endif 5564 #if defined(_SUNOS_VTOC_16) 5565 vpartp = &(labp->dkl_vtoc.v_part[0]); 5566 for (i = 0; i < NDKMAP; i++, vpartp++) { 5567 part_end = vpartp->p_start + vpartp->p_size; 5568 if ((vpartp->p_size > 0) && 5569 (part_end > un->un_blockcount)) { 5570 un->un_f_geometry_is_valid = FALSE; 5571 break; 5572 } 5573 } 5574 #endif 5575 } else { 5576 uint64_t t_capacity; 5577 uint32_t t_lbasize; 5578 5579 mutex_exit(SD_MUTEX(un)); 5580 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5581 path_flag); 5582 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5583 mutex_enter(SD_MUTEX(un)); 5584 5585 if (err == 0) { 5586 sd_update_block_info(un, t_lbasize, t_capacity); 5587 } 5588 5589 if (capacity > un->un_blockcount) { 5590 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5591 "Corrupt label - bad geometry\n"); 5592 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5593 "Label says %u blocks; Drive says %llu blocks\n", 5594 capacity, (unsigned long long)un->un_blockcount); 5595 un->un_f_geometry_is_valid = FALSE; 5596 label_error = SD_LABEL_IS_INVALID; 5597 } 5598 } 5599 5600 done: 5601 5602 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5603 SD_INFO(SD_LOG_COMMON, un, 5604 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5605 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5606 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5607 SD_INFO(SD_LOG_COMMON, un, 5608 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5609 un->un_tgt_blocksize, un->un_blockcount, 5610 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5611 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5612 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5613 5614 ASSERT(mutex_owned(SD_MUTEX(un))); 5615 5616 return (label_error); 5617 } 5618 5619 5620 /* 5621 * Function: sd_build_default_label 5622 * 5623 * Description: Generate a default label for those devices that do not have 5624 * one, e.g., new media, removable cartridges, etc.. 5625 * 5626 * Context: Kernel thread only 5627 */ 5628 5629 static void 5630 sd_build_default_label(struct sd_lun *un) 5631 { 5632 #if defined(_SUNOS_VTOC_16) 5633 uint_t phys_spc; 5634 uint_t disksize; 5635 struct dk_geom un_g; 5636 #endif 5637 5638 ASSERT(un != NULL); 5639 ASSERT(mutex_owned(SD_MUTEX(un))); 5640 5641 #if defined(_SUNOS_VTOC_8) 5642 /* 5643 * Note: This is a legacy check for non-removable devices on VTOC_8 5644 * only. This may be a valid check for VTOC_16 as well. 5645 * Once we understand why there is this difference between SPARC and 5646 * x86 platform, we could remove this legacy check. 5647 */ 5648 ASSERT(un->un_f_default_vtoc_supported); 5649 #endif 5650 5651 bzero(&un->un_g, sizeof (struct dk_geom)); 5652 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5653 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5654 5655 #if defined(_SUNOS_VTOC_8) 5656 5657 /* 5658 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5659 * But it is still necessary to set up various geometry information, 5660 * and we are doing this here. 5661 */ 5662 5663 /* 5664 * For the rpm, we use the minimum for the disk. For the head, cyl, 5665 * and number of sector per track, if the capacity <= 1GB, head = 64, 5666 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5667 * equal to C*H*S values. This will cause some truncation of size due 5668 * to round off errors. For CD-ROMs, this truncation can have adverse 5669 * side effects, so returning ncyl and nhead as 1. The nsect will 5670 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5671 */ 5672 if (ISCD(un)) { 5673 /* 5674 * Preserve the old behavior for non-writable 5675 * medias. Since dkg_nsect is a ushort, it 5676 * will lose bits as cdroms have more than 5677 * 65536 sectors. So if we recalculate 5678 * capacity, it will become much shorter. 5679 * But the dkg_* information is not 5680 * used for CDROMs so it is OK. But for 5681 * Writable CDs we need this information 5682 * to be valid (for newfs say). So we 5683 * make nsect and nhead > 1 that way 5684 * nsect can still stay within ushort limit 5685 * without losing any bits. 5686 */ 5687 if (un->un_f_mmc_writable_media == TRUE) { 5688 un->un_g.dkg_nhead = 64; 5689 un->un_g.dkg_nsect = 32; 5690 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5691 un->un_blockcount = un->un_g.dkg_ncyl * 5692 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5693 } else { 5694 un->un_g.dkg_ncyl = 1; 5695 un->un_g.dkg_nhead = 1; 5696 un->un_g.dkg_nsect = un->un_blockcount; 5697 } 5698 } else { 5699 if (un->un_blockcount <= 0x1000) { 5700 /* unlabeled SCSI floppy device */ 5701 un->un_g.dkg_nhead = 2; 5702 un->un_g.dkg_ncyl = 80; 5703 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 5704 } else if (un->un_blockcount <= 0x200000) { 5705 un->un_g.dkg_nhead = 64; 5706 un->un_g.dkg_nsect = 32; 5707 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5708 } else { 5709 un->un_g.dkg_nhead = 255; 5710 un->un_g.dkg_nsect = 63; 5711 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 5712 } 5713 un->un_blockcount = 5714 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5715 } 5716 5717 un->un_g.dkg_acyl = 0; 5718 un->un_g.dkg_bcyl = 0; 5719 un->un_g.dkg_rpm = 200; 5720 un->un_asciilabel[0] = '\0'; 5721 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 5722 5723 un->un_map[0].dkl_cylno = 0; 5724 un->un_map[0].dkl_nblk = un->un_blockcount; 5725 un->un_map[2].dkl_cylno = 0; 5726 un->un_map[2].dkl_nblk = un->un_blockcount; 5727 5728 #elif defined(_SUNOS_VTOC_16) 5729 5730 if (un->un_solaris_size == 0) { 5731 /* 5732 * Got fdisk table but no solaris entry therefore 5733 * don't create a default label 5734 */ 5735 un->un_f_geometry_is_valid = TRUE; 5736 return; 5737 } 5738 5739 /* 5740 * For CDs we continue to use the physical geometry to calculate 5741 * number of cylinders. All other devices must convert the 5742 * physical geometry (geom_cache) to values that will fit 5743 * in a dk_geom structure. 5744 */ 5745 if (ISCD(un)) { 5746 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 5747 } else { 5748 /* Convert physical geometry to disk geometry */ 5749 bzero(&un_g, sizeof (struct dk_geom)); 5750 sd_convert_geometry(un->un_blockcount, &un_g); 5751 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 5752 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5753 } 5754 5755 ASSERT(phys_spc != 0); 5756 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 5757 un->un_g.dkg_acyl = DK_ACYL; 5758 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 5759 disksize = un->un_g.dkg_ncyl * phys_spc; 5760 5761 if (ISCD(un)) { 5762 /* 5763 * CD's don't use the "heads * sectors * cyls"-type of 5764 * geometry, but instead use the entire capacity of the media. 5765 */ 5766 disksize = un->un_solaris_size; 5767 un->un_g.dkg_nhead = 1; 5768 un->un_g.dkg_nsect = 1; 5769 un->un_g.dkg_rpm = 5770 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 5771 5772 un->un_vtoc.v_part[0].p_start = 0; 5773 un->un_vtoc.v_part[0].p_size = disksize; 5774 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 5775 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 5776 5777 un->un_map[0].dkl_cylno = 0; 5778 un->un_map[0].dkl_nblk = disksize; 5779 un->un_offset[0] = 0; 5780 5781 } else { 5782 /* 5783 * Hard disks and removable media cartridges 5784 */ 5785 un->un_g.dkg_rpm = 5786 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 5787 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 5788 5789 /* Add boot slice */ 5790 un->un_vtoc.v_part[8].p_start = 0; 5791 un->un_vtoc.v_part[8].p_size = phys_spc; 5792 un->un_vtoc.v_part[8].p_tag = V_BOOT; 5793 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 5794 5795 un->un_map[8].dkl_cylno = 0; 5796 un->un_map[8].dkl_nblk = phys_spc; 5797 un->un_offset[8] = 0; 5798 } 5799 5800 un->un_g.dkg_apc = 0; 5801 un->un_vtoc.v_nparts = V_NUMPAR; 5802 5803 /* Add backup slice */ 5804 un->un_vtoc.v_part[2].p_start = 0; 5805 un->un_vtoc.v_part[2].p_size = disksize; 5806 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 5807 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 5808 5809 un->un_map[2].dkl_cylno = 0; 5810 un->un_map[2].dkl_nblk = disksize; 5811 un->un_offset[2] = 0; 5812 5813 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 5814 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5815 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5816 5817 #else 5818 #error "No VTOC format defined." 5819 #endif 5820 5821 un->un_g.dkg_read_reinstruct = 0; 5822 un->un_g.dkg_write_reinstruct = 0; 5823 5824 un->un_g.dkg_intrlv = 1; 5825 5826 un->un_vtoc.v_version = V_VERSION; 5827 un->un_vtoc.v_sanity = VTOC_SANE; 5828 5829 un->un_f_geometry_is_valid = TRUE; 5830 5831 SD_INFO(SD_LOG_COMMON, un, 5832 "sd_build_default_label: Default label created: " 5833 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 5834 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 5835 un->un_g.dkg_nsect, un->un_blockcount); 5836 } 5837 5838 5839 #if defined(_FIRMWARE_NEEDS_FDISK) 5840 /* 5841 * Max CHS values, as they are encoded into bytes, for 1022/254/63 5842 */ 5843 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 5844 #define LBA_MAX_CYL (1022 & 0xFF) 5845 #define LBA_MAX_HEAD (254) 5846 5847 5848 /* 5849 * Function: sd_has_max_chs_vals 5850 * 5851 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 5852 * 5853 * Arguments: fdp - ptr to CHS info 5854 * 5855 * Return Code: True or false 5856 * 5857 * Context: Any. 5858 */ 5859 5860 static int 5861 sd_has_max_chs_vals(struct ipart *fdp) 5862 { 5863 return ((fdp->begcyl == LBA_MAX_CYL) && 5864 (fdp->beghead == LBA_MAX_HEAD) && 5865 (fdp->begsect == LBA_MAX_SECT) && 5866 (fdp->endcyl == LBA_MAX_CYL) && 5867 (fdp->endhead == LBA_MAX_HEAD) && 5868 (fdp->endsect == LBA_MAX_SECT)); 5869 } 5870 #endif 5871 5872 5873 /* 5874 * Function: sd_inq_fill 5875 * 5876 * Description: Print a piece of inquiry data, cleaned up for non-printable 5877 * characters and stopping at the first space character after 5878 * the beginning of the passed string; 5879 * 5880 * Arguments: p - source string 5881 * l - maximum length to copy 5882 * s - destination string 5883 * 5884 * Context: Any. 5885 */ 5886 5887 static void 5888 sd_inq_fill(char *p, int l, char *s) 5889 { 5890 unsigned i = 0; 5891 char c; 5892 5893 while (i++ < l) { 5894 if ((c = *p++) < ' ' || c >= 0x7F) { 5895 c = '*'; 5896 } else if (i != 1 && c == ' ') { 5897 break; 5898 } 5899 *s++ = c; 5900 } 5901 *s++ = 0; 5902 } 5903 5904 5905 /* 5906 * Function: sd_register_devid 5907 * 5908 * Description: This routine will obtain the device id information from the 5909 * target, obtain the serial number, and register the device 5910 * id with the ddi framework. 5911 * 5912 * Arguments: devi - the system's dev_info_t for the device. 5913 * un - driver soft state (unit) structure 5914 * reservation_flag - indicates if a reservation conflict 5915 * occurred during attach 5916 * 5917 * Context: Kernel Thread 5918 */ 5919 static void 5920 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 5921 { 5922 int rval = 0; 5923 uchar_t *inq80 = NULL; 5924 size_t inq80_len = MAX_INQUIRY_SIZE; 5925 size_t inq80_resid = 0; 5926 uchar_t *inq83 = NULL; 5927 size_t inq83_len = MAX_INQUIRY_SIZE; 5928 size_t inq83_resid = 0; 5929 5930 ASSERT(un != NULL); 5931 ASSERT(mutex_owned(SD_MUTEX(un))); 5932 ASSERT((SD_DEVINFO(un)) == devi); 5933 5934 /* 5935 * This is the case of antiquated Sun disk drives that have the 5936 * FAB_DEVID property set in the disk_table. These drives 5937 * manage the devid's by storing them in last 2 available sectors 5938 * on the drive and have them fabricated by the ddi layer by calling 5939 * ddi_devid_init and passing the DEVID_FAB flag. 5940 */ 5941 if (un->un_f_opt_fab_devid == TRUE) { 5942 /* 5943 * Depending on EINVAL isn't reliable, since a reserved disk 5944 * may result in invalid geometry, so check to make sure a 5945 * reservation conflict did not occur during attach. 5946 */ 5947 if ((sd_get_devid(un) == EINVAL) && 5948 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5949 /* 5950 * The devid is invalid AND there is no reservation 5951 * conflict. Fabricate a new devid. 5952 */ 5953 (void) sd_create_devid(un); 5954 } 5955 5956 /* Register the devid if it exists */ 5957 if (un->un_devid != NULL) { 5958 (void) ddi_devid_register(SD_DEVINFO(un), 5959 un->un_devid); 5960 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5961 "sd_register_devid: Devid Fabricated\n"); 5962 } 5963 return; 5964 } 5965 5966 /* 5967 * We check the availibility of the World Wide Name (0x83) and Unit 5968 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5969 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5970 * 0x83 is availible, that is the best choice. Our next choice is 5971 * 0x80. If neither are availible, we munge the devid from the device 5972 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5973 * to fabricate a devid for non-Sun qualified disks. 5974 */ 5975 if (sd_check_vpd_page_support(un) == 0) { 5976 /* collect page 80 data if available */ 5977 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5978 5979 mutex_exit(SD_MUTEX(un)); 5980 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5981 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 5982 0x01, 0x80, &inq80_resid); 5983 5984 if (rval != 0) { 5985 kmem_free(inq80, inq80_len); 5986 inq80 = NULL; 5987 inq80_len = 0; 5988 } 5989 mutex_enter(SD_MUTEX(un)); 5990 } 5991 5992 /* collect page 83 data if available */ 5993 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5994 mutex_exit(SD_MUTEX(un)); 5995 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5996 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 5997 0x01, 0x83, &inq83_resid); 5998 5999 if (rval != 0) { 6000 kmem_free(inq83, inq83_len); 6001 inq83 = NULL; 6002 inq83_len = 0; 6003 } 6004 mutex_enter(SD_MUTEX(un)); 6005 } 6006 } 6007 6008 /* encode best devid possible based on data available */ 6009 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6010 (char *)ddi_driver_name(SD_DEVINFO(un)), 6011 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6012 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6013 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6014 6015 /* devid successfully encoded, register devid */ 6016 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6017 6018 } else { 6019 /* 6020 * Unable to encode a devid based on data available. 6021 * This is not a Sun qualified disk. Older Sun disk 6022 * drives that have the SD_FAB_DEVID property 6023 * set in the disk_table and non Sun qualified 6024 * disks are treated in the same manner. These 6025 * drives manage the devid's by storing them in 6026 * last 2 available sectors on the drive and 6027 * have them fabricated by the ddi layer by 6028 * calling ddi_devid_init and passing the 6029 * DEVID_FAB flag. 6030 * Create a fabricate devid only if there's no 6031 * fabricate devid existed. 6032 */ 6033 if (sd_get_devid(un) == EINVAL) { 6034 (void) sd_create_devid(un); 6035 un->un_f_opt_fab_devid = TRUE; 6036 } 6037 6038 /* Register the devid if it exists */ 6039 if (un->un_devid != NULL) { 6040 (void) ddi_devid_register(SD_DEVINFO(un), 6041 un->un_devid); 6042 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6043 "sd_register_devid: devid fabricated using " 6044 "ddi framework\n"); 6045 } 6046 } 6047 6048 /* clean up resources */ 6049 if (inq80 != NULL) { 6050 kmem_free(inq80, inq80_len); 6051 } 6052 if (inq83 != NULL) { 6053 kmem_free(inq83, inq83_len); 6054 } 6055 } 6056 6057 static daddr_t 6058 sd_get_devid_block(struct sd_lun *un) 6059 { 6060 daddr_t spc, blk, head, cyl; 6061 6062 if (un->un_blockcount <= DK_MAX_BLOCKS) { 6063 /* this geometry doesn't allow us to write a devid */ 6064 if (un->un_g.dkg_acyl < 2) { 6065 return (-1); 6066 } 6067 6068 /* 6069 * Subtract 2 guarantees that the next to last cylinder 6070 * is used 6071 */ 6072 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6073 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6074 head = un->un_g.dkg_nhead - 1; 6075 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6076 (head * un->un_g.dkg_nsect) + 1; 6077 } else { 6078 if (un->un_reserved != -1) { 6079 blk = un->un_map[un->un_reserved].dkl_cylno + 1; 6080 } else { 6081 return (-1); 6082 } 6083 } 6084 return (blk); 6085 } 6086 6087 /* 6088 * Function: sd_get_devid 6089 * 6090 * Description: This routine will return 0 if a valid device id has been 6091 * obtained from the target and stored in the soft state. If a 6092 * valid device id has not been previously read and stored, a 6093 * read attempt will be made. 6094 * 6095 * Arguments: un - driver soft state (unit) structure 6096 * 6097 * Return Code: 0 if we successfully get the device id 6098 * 6099 * Context: Kernel Thread 6100 */ 6101 6102 static int 6103 sd_get_devid(struct sd_lun *un) 6104 { 6105 struct dk_devid *dkdevid; 6106 ddi_devid_t tmpid; 6107 uint_t *ip; 6108 size_t sz; 6109 daddr_t blk; 6110 int status; 6111 int chksum; 6112 int i; 6113 size_t buffer_size; 6114 6115 ASSERT(un != NULL); 6116 ASSERT(mutex_owned(SD_MUTEX(un))); 6117 6118 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6119 un); 6120 6121 if (un->un_devid != NULL) { 6122 return (0); 6123 } 6124 6125 blk = sd_get_devid_block(un); 6126 if (blk < 0) 6127 return (EINVAL); 6128 6129 /* 6130 * Read and verify device id, stored in the reserved cylinders at the 6131 * end of the disk. Backup label is on the odd sectors of the last 6132 * track of the last cylinder. Device id will be on track of the next 6133 * to last cylinder. 6134 */ 6135 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6136 mutex_exit(SD_MUTEX(un)); 6137 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6138 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6139 SD_PATH_DIRECT); 6140 if (status != 0) { 6141 goto error; 6142 } 6143 6144 /* Validate the revision */ 6145 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6146 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6147 status = EINVAL; 6148 goto error; 6149 } 6150 6151 /* Calculate the checksum */ 6152 chksum = 0; 6153 ip = (uint_t *)dkdevid; 6154 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6155 i++) { 6156 chksum ^= ip[i]; 6157 } 6158 6159 /* Compare the checksums */ 6160 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6161 status = EINVAL; 6162 goto error; 6163 } 6164 6165 /* Validate the device id */ 6166 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6167 status = EINVAL; 6168 goto error; 6169 } 6170 6171 /* 6172 * Store the device id in the driver soft state 6173 */ 6174 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6175 tmpid = kmem_alloc(sz, KM_SLEEP); 6176 6177 mutex_enter(SD_MUTEX(un)); 6178 6179 un->un_devid = tmpid; 6180 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6181 6182 kmem_free(dkdevid, buffer_size); 6183 6184 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6185 6186 return (status); 6187 error: 6188 mutex_enter(SD_MUTEX(un)); 6189 kmem_free(dkdevid, buffer_size); 6190 return (status); 6191 } 6192 6193 6194 /* 6195 * Function: sd_create_devid 6196 * 6197 * Description: This routine will fabricate the device id and write it 6198 * to the disk. 6199 * 6200 * Arguments: un - driver soft state (unit) structure 6201 * 6202 * Return Code: value of the fabricated device id 6203 * 6204 * Context: Kernel Thread 6205 */ 6206 6207 static ddi_devid_t 6208 sd_create_devid(struct sd_lun *un) 6209 { 6210 ASSERT(un != NULL); 6211 6212 /* Fabricate the devid */ 6213 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6214 == DDI_FAILURE) { 6215 return (NULL); 6216 } 6217 6218 /* Write the devid to disk */ 6219 if (sd_write_deviceid(un) != 0) { 6220 ddi_devid_free(un->un_devid); 6221 un->un_devid = NULL; 6222 } 6223 6224 return (un->un_devid); 6225 } 6226 6227 6228 /* 6229 * Function: sd_write_deviceid 6230 * 6231 * Description: This routine will write the device id to the disk 6232 * reserved sector. 6233 * 6234 * Arguments: un - driver soft state (unit) structure 6235 * 6236 * Return Code: EINVAL 6237 * value returned by sd_send_scsi_cmd 6238 * 6239 * Context: Kernel Thread 6240 */ 6241 6242 static int 6243 sd_write_deviceid(struct sd_lun *un) 6244 { 6245 struct dk_devid *dkdevid; 6246 daddr_t blk; 6247 uint_t *ip, chksum; 6248 int status; 6249 int i; 6250 6251 ASSERT(mutex_owned(SD_MUTEX(un))); 6252 6253 blk = sd_get_devid_block(un); 6254 if (blk < 0) 6255 return (-1); 6256 mutex_exit(SD_MUTEX(un)); 6257 6258 /* Allocate the buffer */ 6259 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6260 6261 /* Fill in the revision */ 6262 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6263 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6264 6265 /* Copy in the device id */ 6266 mutex_enter(SD_MUTEX(un)); 6267 bcopy(un->un_devid, &dkdevid->dkd_devid, 6268 ddi_devid_sizeof(un->un_devid)); 6269 mutex_exit(SD_MUTEX(un)); 6270 6271 /* Calculate the checksum */ 6272 chksum = 0; 6273 ip = (uint_t *)dkdevid; 6274 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6275 i++) { 6276 chksum ^= ip[i]; 6277 } 6278 6279 /* Fill-in checksum */ 6280 DKD_FORMCHKSUM(chksum, dkdevid); 6281 6282 /* Write the reserved sector */ 6283 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6284 SD_PATH_DIRECT); 6285 6286 kmem_free(dkdevid, un->un_sys_blocksize); 6287 6288 mutex_enter(SD_MUTEX(un)); 6289 return (status); 6290 } 6291 6292 6293 /* 6294 * Function: sd_check_vpd_page_support 6295 * 6296 * Description: This routine sends an inquiry command with the EVPD bit set and 6297 * a page code of 0x00 to the device. It is used to determine which 6298 * vital product pages are availible to find the devid. We are 6299 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6300 * device does not support that command. 6301 * 6302 * Arguments: un - driver soft state (unit) structure 6303 * 6304 * Return Code: 0 - success 6305 * 1 - check condition 6306 * 6307 * Context: This routine can sleep. 6308 */ 6309 6310 static int 6311 sd_check_vpd_page_support(struct sd_lun *un) 6312 { 6313 uchar_t *page_list = NULL; 6314 uchar_t page_length = 0xff; /* Use max possible length */ 6315 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6316 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6317 int rval = 0; 6318 int counter; 6319 6320 ASSERT(un != NULL); 6321 ASSERT(mutex_owned(SD_MUTEX(un))); 6322 6323 mutex_exit(SD_MUTEX(un)); 6324 6325 /* 6326 * We'll set the page length to the maximum to save figuring it out 6327 * with an additional call. 6328 */ 6329 page_list = kmem_zalloc(page_length, KM_SLEEP); 6330 6331 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6332 page_code, NULL); 6333 6334 mutex_enter(SD_MUTEX(un)); 6335 6336 /* 6337 * Now we must validate that the device accepted the command, as some 6338 * drives do not support it. If the drive does support it, we will 6339 * return 0, and the supported pages will be in un_vpd_page_mask. If 6340 * not, we return -1. 6341 */ 6342 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6343 /* Loop to find one of the 2 pages we need */ 6344 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6345 6346 /* 6347 * Pages are returned in ascending order, and 0x83 is what we 6348 * are hoping for. 6349 */ 6350 while ((page_list[counter] <= 0x83) && 6351 (counter <= (page_list[VPD_PAGE_LENGTH] + 6352 VPD_HEAD_OFFSET))) { 6353 /* 6354 * Add 3 because page_list[3] is the number of 6355 * pages minus 3 6356 */ 6357 6358 switch (page_list[counter]) { 6359 case 0x00: 6360 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6361 break; 6362 case 0x80: 6363 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6364 break; 6365 case 0x81: 6366 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6367 break; 6368 case 0x82: 6369 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6370 break; 6371 case 0x83: 6372 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6373 break; 6374 } 6375 counter++; 6376 } 6377 6378 } else { 6379 rval = -1; 6380 6381 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6382 "sd_check_vpd_page_support: This drive does not implement " 6383 "VPD pages.\n"); 6384 } 6385 6386 kmem_free(page_list, page_length); 6387 6388 return (rval); 6389 } 6390 6391 6392 /* 6393 * Function: sd_setup_pm 6394 * 6395 * Description: Initialize Power Management on the device 6396 * 6397 * Context: Kernel Thread 6398 */ 6399 6400 static void 6401 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6402 { 6403 uint_t log_page_size; 6404 uchar_t *log_page_data; 6405 int rval; 6406 6407 /* 6408 * Since we are called from attach, holding a mutex for 6409 * un is unnecessary. Because some of the routines called 6410 * from here require SD_MUTEX to not be held, assert this 6411 * right up front. 6412 */ 6413 ASSERT(!mutex_owned(SD_MUTEX(un))); 6414 /* 6415 * Since the sd device does not have the 'reg' property, 6416 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6417 * The following code is to tell cpr that this device 6418 * DOES need to be suspended and resumed. 6419 */ 6420 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6421 "pm-hardware-state", "needs-suspend-resume"); 6422 6423 /* 6424 * This complies with the new power management framework 6425 * for certain desktop machines. Create the pm_components 6426 * property as a string array property. 6427 */ 6428 if (un->un_f_pm_supported) { 6429 /* 6430 * not all devices have a motor, try it first. 6431 * some devices may return ILLEGAL REQUEST, some 6432 * will hang 6433 * The following START_STOP_UNIT is used to check if target 6434 * device has a motor. 6435 */ 6436 un->un_f_start_stop_supported = TRUE; 6437 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6438 SD_PATH_DIRECT) != 0) { 6439 un->un_f_start_stop_supported = FALSE; 6440 } 6441 6442 /* 6443 * create pm properties anyways otherwise the parent can't 6444 * go to sleep 6445 */ 6446 (void) sd_create_pm_components(devi, un); 6447 un->un_f_pm_is_enabled = TRUE; 6448 return; 6449 } 6450 6451 if (!un->un_f_log_sense_supported) { 6452 un->un_power_level = SD_SPINDLE_ON; 6453 un->un_f_pm_is_enabled = FALSE; 6454 return; 6455 } 6456 6457 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6458 6459 #ifdef SDDEBUG 6460 if (sd_force_pm_supported) { 6461 /* Force a successful result */ 6462 rval = 1; 6463 } 6464 #endif 6465 6466 /* 6467 * If the start-stop cycle counter log page is not supported 6468 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6469 * then we should not create the pm_components property. 6470 */ 6471 if (rval == -1) { 6472 /* 6473 * Error. 6474 * Reading log sense failed, most likely this is 6475 * an older drive that does not support log sense. 6476 * If this fails auto-pm is not supported. 6477 */ 6478 un->un_power_level = SD_SPINDLE_ON; 6479 un->un_f_pm_is_enabled = FALSE; 6480 6481 } else if (rval == 0) { 6482 /* 6483 * Page not found. 6484 * The start stop cycle counter is implemented as page 6485 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6486 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6487 */ 6488 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6489 /* 6490 * Page found, use this one. 6491 */ 6492 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6493 un->un_f_pm_is_enabled = TRUE; 6494 } else { 6495 /* 6496 * Error or page not found. 6497 * auto-pm is not supported for this device. 6498 */ 6499 un->un_power_level = SD_SPINDLE_ON; 6500 un->un_f_pm_is_enabled = FALSE; 6501 } 6502 } else { 6503 /* 6504 * Page found, use it. 6505 */ 6506 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6507 un->un_f_pm_is_enabled = TRUE; 6508 } 6509 6510 6511 if (un->un_f_pm_is_enabled == TRUE) { 6512 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6513 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6514 6515 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6516 log_page_size, un->un_start_stop_cycle_page, 6517 0x01, 0, SD_PATH_DIRECT); 6518 #ifdef SDDEBUG 6519 if (sd_force_pm_supported) { 6520 /* Force a successful result */ 6521 rval = 0; 6522 } 6523 #endif 6524 6525 /* 6526 * If the Log sense for Page( Start/stop cycle counter page) 6527 * succeeds, then power managment is supported and we can 6528 * enable auto-pm. 6529 */ 6530 if (rval == 0) { 6531 (void) sd_create_pm_components(devi, un); 6532 } else { 6533 un->un_power_level = SD_SPINDLE_ON; 6534 un->un_f_pm_is_enabled = FALSE; 6535 } 6536 6537 kmem_free(log_page_data, log_page_size); 6538 } 6539 } 6540 6541 6542 /* 6543 * Function: sd_create_pm_components 6544 * 6545 * Description: Initialize PM property. 6546 * 6547 * Context: Kernel thread context 6548 */ 6549 6550 static void 6551 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6552 { 6553 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6554 6555 ASSERT(!mutex_owned(SD_MUTEX(un))); 6556 6557 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6558 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6559 /* 6560 * When components are initially created they are idle, 6561 * power up any non-removables. 6562 * Note: the return value of pm_raise_power can't be used 6563 * for determining if PM should be enabled for this device. 6564 * Even if you check the return values and remove this 6565 * property created above, the PM framework will not honor the 6566 * change after the first call to pm_raise_power. Hence, 6567 * removal of that property does not help if pm_raise_power 6568 * fails. In the case of removable media, the start/stop 6569 * will fail if the media is not present. 6570 */ 6571 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6572 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6573 mutex_enter(SD_MUTEX(un)); 6574 un->un_power_level = SD_SPINDLE_ON; 6575 mutex_enter(&un->un_pm_mutex); 6576 /* Set to on and not busy. */ 6577 un->un_pm_count = 0; 6578 } else { 6579 mutex_enter(SD_MUTEX(un)); 6580 un->un_power_level = SD_SPINDLE_OFF; 6581 mutex_enter(&un->un_pm_mutex); 6582 /* Set to off. */ 6583 un->un_pm_count = -1; 6584 } 6585 mutex_exit(&un->un_pm_mutex); 6586 mutex_exit(SD_MUTEX(un)); 6587 } else { 6588 un->un_power_level = SD_SPINDLE_ON; 6589 un->un_f_pm_is_enabled = FALSE; 6590 } 6591 } 6592 6593 6594 /* 6595 * Function: sd_ddi_suspend 6596 * 6597 * Description: Performs system power-down operations. This includes 6598 * setting the drive state to indicate its suspended so 6599 * that no new commands will be accepted. Also, wait for 6600 * all commands that are in transport or queued to a timer 6601 * for retry to complete. All timeout threads are cancelled. 6602 * 6603 * Return Code: DDI_FAILURE or DDI_SUCCESS 6604 * 6605 * Context: Kernel thread context 6606 */ 6607 6608 static int 6609 sd_ddi_suspend(dev_info_t *devi) 6610 { 6611 struct sd_lun *un; 6612 clock_t wait_cmds_complete; 6613 6614 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6615 if (un == NULL) { 6616 return (DDI_FAILURE); 6617 } 6618 6619 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6620 6621 mutex_enter(SD_MUTEX(un)); 6622 6623 /* Return success if the device is already suspended. */ 6624 if (un->un_state == SD_STATE_SUSPENDED) { 6625 mutex_exit(SD_MUTEX(un)); 6626 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6627 "device already suspended, exiting\n"); 6628 return (DDI_SUCCESS); 6629 } 6630 6631 /* Return failure if the device is being used by HA */ 6632 if (un->un_resvd_status & 6633 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6634 mutex_exit(SD_MUTEX(un)); 6635 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6636 "device in use by HA, exiting\n"); 6637 return (DDI_FAILURE); 6638 } 6639 6640 /* 6641 * Return failure if the device is in a resource wait 6642 * or power changing state. 6643 */ 6644 if ((un->un_state == SD_STATE_RWAIT) || 6645 (un->un_state == SD_STATE_PM_CHANGING)) { 6646 mutex_exit(SD_MUTEX(un)); 6647 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6648 "device in resource wait state, exiting\n"); 6649 return (DDI_FAILURE); 6650 } 6651 6652 6653 un->un_save_state = un->un_last_state; 6654 New_state(un, SD_STATE_SUSPENDED); 6655 6656 /* 6657 * Wait for all commands that are in transport or queued to a timer 6658 * for retry to complete. 6659 * 6660 * While waiting, no new commands will be accepted or sent because of 6661 * the new state we set above. 6662 * 6663 * Wait till current operation has completed. If we are in the resource 6664 * wait state (with an intr outstanding) then we need to wait till the 6665 * intr completes and starts the next cmd. We want to wait for 6666 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6667 */ 6668 wait_cmds_complete = ddi_get_lbolt() + 6669 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6670 6671 while (un->un_ncmds_in_transport != 0) { 6672 /* 6673 * Fail if commands do not finish in the specified time. 6674 */ 6675 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6676 wait_cmds_complete) == -1) { 6677 /* 6678 * Undo the state changes made above. Everything 6679 * must go back to it's original value. 6680 */ 6681 Restore_state(un); 6682 un->un_last_state = un->un_save_state; 6683 /* Wake up any threads that might be waiting. */ 6684 cv_broadcast(&un->un_suspend_cv); 6685 mutex_exit(SD_MUTEX(un)); 6686 SD_ERROR(SD_LOG_IO_PM, un, 6687 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6688 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6689 return (DDI_FAILURE); 6690 } 6691 } 6692 6693 /* 6694 * Cancel SCSI watch thread and timeouts, if any are active 6695 */ 6696 6697 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6698 opaque_t temp_token = un->un_swr_token; 6699 mutex_exit(SD_MUTEX(un)); 6700 scsi_watch_suspend(temp_token); 6701 mutex_enter(SD_MUTEX(un)); 6702 } 6703 6704 if (un->un_reset_throttle_timeid != NULL) { 6705 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6706 un->un_reset_throttle_timeid = NULL; 6707 mutex_exit(SD_MUTEX(un)); 6708 (void) untimeout(temp_id); 6709 mutex_enter(SD_MUTEX(un)); 6710 } 6711 6712 if (un->un_dcvb_timeid != NULL) { 6713 timeout_id_t temp_id = un->un_dcvb_timeid; 6714 un->un_dcvb_timeid = NULL; 6715 mutex_exit(SD_MUTEX(un)); 6716 (void) untimeout(temp_id); 6717 mutex_enter(SD_MUTEX(un)); 6718 } 6719 6720 mutex_enter(&un->un_pm_mutex); 6721 if (un->un_pm_timeid != NULL) { 6722 timeout_id_t temp_id = un->un_pm_timeid; 6723 un->un_pm_timeid = NULL; 6724 mutex_exit(&un->un_pm_mutex); 6725 mutex_exit(SD_MUTEX(un)); 6726 (void) untimeout(temp_id); 6727 mutex_enter(SD_MUTEX(un)); 6728 } else { 6729 mutex_exit(&un->un_pm_mutex); 6730 } 6731 6732 if (un->un_retry_timeid != NULL) { 6733 timeout_id_t temp_id = un->un_retry_timeid; 6734 un->un_retry_timeid = NULL; 6735 mutex_exit(SD_MUTEX(un)); 6736 (void) untimeout(temp_id); 6737 mutex_enter(SD_MUTEX(un)); 6738 } 6739 6740 if (un->un_direct_priority_timeid != NULL) { 6741 timeout_id_t temp_id = un->un_direct_priority_timeid; 6742 un->un_direct_priority_timeid = NULL; 6743 mutex_exit(SD_MUTEX(un)); 6744 (void) untimeout(temp_id); 6745 mutex_enter(SD_MUTEX(un)); 6746 } 6747 6748 if (un->un_f_is_fibre == TRUE) { 6749 /* 6750 * Remove callbacks for insert and remove events 6751 */ 6752 if (un->un_insert_event != NULL) { 6753 mutex_exit(SD_MUTEX(un)); 6754 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6755 mutex_enter(SD_MUTEX(un)); 6756 un->un_insert_event = NULL; 6757 } 6758 6759 if (un->un_remove_event != NULL) { 6760 mutex_exit(SD_MUTEX(un)); 6761 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6762 mutex_enter(SD_MUTEX(un)); 6763 un->un_remove_event = NULL; 6764 } 6765 } 6766 6767 mutex_exit(SD_MUTEX(un)); 6768 6769 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6770 6771 return (DDI_SUCCESS); 6772 } 6773 6774 6775 /* 6776 * Function: sd_ddi_pm_suspend 6777 * 6778 * Description: Set the drive state to low power. 6779 * Someone else is required to actually change the drive 6780 * power level. 6781 * 6782 * Arguments: un - driver soft state (unit) structure 6783 * 6784 * Return Code: DDI_FAILURE or DDI_SUCCESS 6785 * 6786 * Context: Kernel thread context 6787 */ 6788 6789 static int 6790 sd_ddi_pm_suspend(struct sd_lun *un) 6791 { 6792 ASSERT(un != NULL); 6793 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 6794 6795 ASSERT(!mutex_owned(SD_MUTEX(un))); 6796 mutex_enter(SD_MUTEX(un)); 6797 6798 /* 6799 * Exit if power management is not enabled for this device, or if 6800 * the device is being used by HA. 6801 */ 6802 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6803 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6804 mutex_exit(SD_MUTEX(un)); 6805 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 6806 return (DDI_SUCCESS); 6807 } 6808 6809 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 6810 un->un_ncmds_in_driver); 6811 6812 /* 6813 * See if the device is not busy, ie.: 6814 * - we have no commands in the driver for this device 6815 * - not waiting for resources 6816 */ 6817 if ((un->un_ncmds_in_driver == 0) && 6818 (un->un_state != SD_STATE_RWAIT)) { 6819 /* 6820 * The device is not busy, so it is OK to go to low power state. 6821 * Indicate low power, but rely on someone else to actually 6822 * change it. 6823 */ 6824 mutex_enter(&un->un_pm_mutex); 6825 un->un_pm_count = -1; 6826 mutex_exit(&un->un_pm_mutex); 6827 un->un_power_level = SD_SPINDLE_OFF; 6828 } 6829 6830 mutex_exit(SD_MUTEX(un)); 6831 6832 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 6833 6834 return (DDI_SUCCESS); 6835 } 6836 6837 6838 /* 6839 * Function: sd_ddi_resume 6840 * 6841 * Description: Performs system power-up operations.. 6842 * 6843 * Return Code: DDI_SUCCESS 6844 * DDI_FAILURE 6845 * 6846 * Context: Kernel thread context 6847 */ 6848 6849 static int 6850 sd_ddi_resume(dev_info_t *devi) 6851 { 6852 struct sd_lun *un; 6853 6854 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6855 if (un == NULL) { 6856 return (DDI_FAILURE); 6857 } 6858 6859 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6860 6861 mutex_enter(SD_MUTEX(un)); 6862 Restore_state(un); 6863 6864 /* 6865 * Restore the state which was saved to give the 6866 * the right state in un_last_state 6867 */ 6868 un->un_last_state = un->un_save_state; 6869 /* 6870 * Note: throttle comes back at full. 6871 * Also note: this MUST be done before calling pm_raise_power 6872 * otherwise the system can get hung in biowait. The scenario where 6873 * this'll happen is under cpr suspend. Writing of the system 6874 * state goes through sddump, which writes 0 to un_throttle. If 6875 * writing the system state then fails, example if the partition is 6876 * too small, then cpr attempts a resume. If throttle isn't restored 6877 * from the saved value until after calling pm_raise_power then 6878 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6879 * in biowait. 6880 */ 6881 un->un_throttle = un->un_saved_throttle; 6882 6883 /* 6884 * The chance of failure is very rare as the only command done in power 6885 * entry point is START command when you transition from 0->1 or 6886 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6887 * which suspend was done. Ignore the return value as the resume should 6888 * not be failed. In the case of removable media the media need not be 6889 * inserted and hence there is a chance that raise power will fail with 6890 * media not present. 6891 */ 6892 if (un->un_f_attach_spinup) { 6893 mutex_exit(SD_MUTEX(un)); 6894 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 6895 mutex_enter(SD_MUTEX(un)); 6896 } 6897 6898 /* 6899 * Don't broadcast to the suspend cv and therefore possibly 6900 * start I/O until after power has been restored. 6901 */ 6902 cv_broadcast(&un->un_suspend_cv); 6903 cv_broadcast(&un->un_state_cv); 6904 6905 /* restart thread */ 6906 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6907 scsi_watch_resume(un->un_swr_token); 6908 } 6909 6910 #if (defined(__fibre)) 6911 if (un->un_f_is_fibre == TRUE) { 6912 /* 6913 * Add callbacks for insert and remove events 6914 */ 6915 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6916 sd_init_event_callbacks(un); 6917 } 6918 } 6919 #endif 6920 6921 /* 6922 * Transport any pending commands to the target. 6923 * 6924 * If this is a low-activity device commands in queue will have to wait 6925 * until new commands come in, which may take awhile. Also, we 6926 * specifically don't check un_ncmds_in_transport because we know that 6927 * there really are no commands in progress after the unit was 6928 * suspended and we could have reached the throttle level, been 6929 * suspended, and have no new commands coming in for awhile. Highly 6930 * unlikely, but so is the low-activity disk scenario. 6931 */ 6932 ddi_xbuf_dispatch(un->un_xbuf_attr); 6933 6934 sd_start_cmds(un, NULL); 6935 mutex_exit(SD_MUTEX(un)); 6936 6937 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6938 6939 return (DDI_SUCCESS); 6940 } 6941 6942 6943 /* 6944 * Function: sd_ddi_pm_resume 6945 * 6946 * Description: Set the drive state to powered on. 6947 * Someone else is required to actually change the drive 6948 * power level. 6949 * 6950 * Arguments: un - driver soft state (unit) structure 6951 * 6952 * Return Code: DDI_SUCCESS 6953 * 6954 * Context: Kernel thread context 6955 */ 6956 6957 static int 6958 sd_ddi_pm_resume(struct sd_lun *un) 6959 { 6960 ASSERT(un != NULL); 6961 6962 ASSERT(!mutex_owned(SD_MUTEX(un))); 6963 mutex_enter(SD_MUTEX(un)); 6964 un->un_power_level = SD_SPINDLE_ON; 6965 6966 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6967 mutex_enter(&un->un_pm_mutex); 6968 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6969 un->un_pm_count++; 6970 ASSERT(un->un_pm_count == 0); 6971 /* 6972 * Note: no longer do the cv_broadcast on un_suspend_cv. The 6973 * un_suspend_cv is for a system resume, not a power management 6974 * device resume. (4297749) 6975 * cv_broadcast(&un->un_suspend_cv); 6976 */ 6977 } 6978 mutex_exit(&un->un_pm_mutex); 6979 mutex_exit(SD_MUTEX(un)); 6980 6981 return (DDI_SUCCESS); 6982 } 6983 6984 6985 /* 6986 * Function: sd_pm_idletimeout_handler 6987 * 6988 * Description: A timer routine that's active only while a device is busy. 6989 * The purpose is to extend slightly the pm framework's busy 6990 * view of the device to prevent busy/idle thrashing for 6991 * back-to-back commands. Do this by comparing the current time 6992 * to the time at which the last command completed and when the 6993 * difference is greater than sd_pm_idletime, call 6994 * pm_idle_component. In addition to indicating idle to the pm 6995 * framework, update the chain type to again use the internal pm 6996 * layers of the driver. 6997 * 6998 * Arguments: arg - driver soft state (unit) structure 6999 * 7000 * Context: Executes in a timeout(9F) thread context 7001 */ 7002 7003 static void 7004 sd_pm_idletimeout_handler(void *arg) 7005 { 7006 struct sd_lun *un = arg; 7007 7008 time_t now; 7009 7010 mutex_enter(&sd_detach_mutex); 7011 if (un->un_detach_count != 0) { 7012 /* Abort if the instance is detaching */ 7013 mutex_exit(&sd_detach_mutex); 7014 return; 7015 } 7016 mutex_exit(&sd_detach_mutex); 7017 7018 now = ddi_get_time(); 7019 /* 7020 * Grab both mutexes, in the proper order, since we're accessing 7021 * both PM and softstate variables. 7022 */ 7023 mutex_enter(SD_MUTEX(un)); 7024 mutex_enter(&un->un_pm_mutex); 7025 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7026 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7027 /* 7028 * Update the chain types. 7029 * This takes affect on the next new command received. 7030 */ 7031 if (un->un_f_non_devbsize_supported) { 7032 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7033 } else { 7034 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7035 } 7036 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7037 7038 SD_TRACE(SD_LOG_IO_PM, un, 7039 "sd_pm_idletimeout_handler: idling device\n"); 7040 (void) pm_idle_component(SD_DEVINFO(un), 0); 7041 un->un_pm_idle_timeid = NULL; 7042 } else { 7043 un->un_pm_idle_timeid = 7044 timeout(sd_pm_idletimeout_handler, un, 7045 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7046 } 7047 mutex_exit(&un->un_pm_mutex); 7048 mutex_exit(SD_MUTEX(un)); 7049 } 7050 7051 7052 /* 7053 * Function: sd_pm_timeout_handler 7054 * 7055 * Description: Callback to tell framework we are idle. 7056 * 7057 * Context: timeout(9f) thread context. 7058 */ 7059 7060 static void 7061 sd_pm_timeout_handler(void *arg) 7062 { 7063 struct sd_lun *un = arg; 7064 7065 (void) pm_idle_component(SD_DEVINFO(un), 0); 7066 mutex_enter(&un->un_pm_mutex); 7067 un->un_pm_timeid = NULL; 7068 mutex_exit(&un->un_pm_mutex); 7069 } 7070 7071 7072 /* 7073 * Function: sdpower 7074 * 7075 * Description: PM entry point. 7076 * 7077 * Return Code: DDI_SUCCESS 7078 * DDI_FAILURE 7079 * 7080 * Context: Kernel thread context 7081 */ 7082 7083 static int 7084 sdpower(dev_info_t *devi, int component, int level) 7085 { 7086 struct sd_lun *un; 7087 int instance; 7088 int rval = DDI_SUCCESS; 7089 uint_t i, log_page_size, maxcycles, ncycles; 7090 uchar_t *log_page_data; 7091 int log_sense_page; 7092 int medium_present; 7093 time_t intvlp; 7094 dev_t dev; 7095 struct pm_trans_data sd_pm_tran_data; 7096 uchar_t save_state; 7097 int sval; 7098 uchar_t state_before_pm; 7099 int got_semaphore_here; 7100 7101 instance = ddi_get_instance(devi); 7102 7103 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7104 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7105 component != 0) { 7106 return (DDI_FAILURE); 7107 } 7108 7109 dev = sd_make_device(SD_DEVINFO(un)); 7110 7111 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7112 7113 /* 7114 * Must synchronize power down with close. 7115 * Attempt to decrement/acquire the open/close semaphore, 7116 * but do NOT wait on it. If it's not greater than zero, 7117 * ie. it can't be decremented without waiting, then 7118 * someone else, either open or close, already has it 7119 * and the try returns 0. Use that knowledge here to determine 7120 * if it's OK to change the device power level. 7121 * Also, only increment it on exit if it was decremented, ie. gotten, 7122 * here. 7123 */ 7124 got_semaphore_here = sema_tryp(&un->un_semoclose); 7125 7126 mutex_enter(SD_MUTEX(un)); 7127 7128 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7129 un->un_ncmds_in_driver); 7130 7131 /* 7132 * If un_ncmds_in_driver is non-zero it indicates commands are 7133 * already being processed in the driver, or if the semaphore was 7134 * not gotten here it indicates an open or close is being processed. 7135 * At the same time somebody is requesting to go low power which 7136 * can't happen, therefore we need to return failure. 7137 */ 7138 if ((level == SD_SPINDLE_OFF) && 7139 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7140 mutex_exit(SD_MUTEX(un)); 7141 7142 if (got_semaphore_here != 0) { 7143 sema_v(&un->un_semoclose); 7144 } 7145 SD_TRACE(SD_LOG_IO_PM, un, 7146 "sdpower: exit, device has queued cmds.\n"); 7147 return (DDI_FAILURE); 7148 } 7149 7150 /* 7151 * if it is OFFLINE that means the disk is completely dead 7152 * in our case we have to put the disk in on or off by sending commands 7153 * Of course that will fail anyway so return back here. 7154 * 7155 * Power changes to a device that's OFFLINE or SUSPENDED 7156 * are not allowed. 7157 */ 7158 if ((un->un_state == SD_STATE_OFFLINE) || 7159 (un->un_state == SD_STATE_SUSPENDED)) { 7160 mutex_exit(SD_MUTEX(un)); 7161 7162 if (got_semaphore_here != 0) { 7163 sema_v(&un->un_semoclose); 7164 } 7165 SD_TRACE(SD_LOG_IO_PM, un, 7166 "sdpower: exit, device is off-line.\n"); 7167 return (DDI_FAILURE); 7168 } 7169 7170 /* 7171 * Change the device's state to indicate it's power level 7172 * is being changed. Do this to prevent a power off in the 7173 * middle of commands, which is especially bad on devices 7174 * that are really powered off instead of just spun down. 7175 */ 7176 state_before_pm = un->un_state; 7177 un->un_state = SD_STATE_PM_CHANGING; 7178 7179 mutex_exit(SD_MUTEX(un)); 7180 7181 /* 7182 * If "pm-capable" property is set to TRUE by HBA drivers, 7183 * bypass the following checking, otherwise, check the log 7184 * sense information for this device 7185 */ 7186 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7187 /* 7188 * Get the log sense information to understand whether the 7189 * the powercycle counts have gone beyond the threshhold. 7190 */ 7191 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7192 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7193 7194 mutex_enter(SD_MUTEX(un)); 7195 log_sense_page = un->un_start_stop_cycle_page; 7196 mutex_exit(SD_MUTEX(un)); 7197 7198 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7199 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7200 #ifdef SDDEBUG 7201 if (sd_force_pm_supported) { 7202 /* Force a successful result */ 7203 rval = 0; 7204 } 7205 #endif 7206 if (rval != 0) { 7207 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7208 "Log Sense Failed\n"); 7209 kmem_free(log_page_data, log_page_size); 7210 /* Cannot support power management on those drives */ 7211 7212 if (got_semaphore_here != 0) { 7213 sema_v(&un->un_semoclose); 7214 } 7215 /* 7216 * On exit put the state back to it's original value 7217 * and broadcast to anyone waiting for the power 7218 * change completion. 7219 */ 7220 mutex_enter(SD_MUTEX(un)); 7221 un->un_state = state_before_pm; 7222 cv_broadcast(&un->un_suspend_cv); 7223 mutex_exit(SD_MUTEX(un)); 7224 SD_TRACE(SD_LOG_IO_PM, un, 7225 "sdpower: exit, Log Sense Failed.\n"); 7226 return (DDI_FAILURE); 7227 } 7228 7229 /* 7230 * From the page data - Convert the essential information to 7231 * pm_trans_data 7232 */ 7233 maxcycles = 7234 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7235 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7236 7237 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7238 7239 ncycles = 7240 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7241 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7242 7243 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7244 7245 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7246 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7247 log_page_data[8+i]; 7248 } 7249 7250 kmem_free(log_page_data, log_page_size); 7251 7252 /* 7253 * Call pm_trans_check routine to get the Ok from 7254 * the global policy 7255 */ 7256 7257 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7258 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7259 7260 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7261 #ifdef SDDEBUG 7262 if (sd_force_pm_supported) { 7263 /* Force a successful result */ 7264 rval = 1; 7265 } 7266 #endif 7267 switch (rval) { 7268 case 0: 7269 /* 7270 * Not Ok to Power cycle or error in parameters passed 7271 * Would have given the advised time to consider power 7272 * cycle. Based on the new intvlp parameter we are 7273 * supposed to pretend we are busy so that pm framework 7274 * will never call our power entry point. Because of 7275 * that install a timeout handler and wait for the 7276 * recommended time to elapse so that power management 7277 * can be effective again. 7278 * 7279 * To effect this behavior, call pm_busy_component to 7280 * indicate to the framework this device is busy. 7281 * By not adjusting un_pm_count the rest of PM in 7282 * the driver will function normally, and independant 7283 * of this but because the framework is told the device 7284 * is busy it won't attempt powering down until it gets 7285 * a matching idle. The timeout handler sends this. 7286 * Note: sd_pm_entry can't be called here to do this 7287 * because sdpower may have been called as a result 7288 * of a call to pm_raise_power from within sd_pm_entry. 7289 * 7290 * If a timeout handler is already active then 7291 * don't install another. 7292 */ 7293 mutex_enter(&un->un_pm_mutex); 7294 if (un->un_pm_timeid == NULL) { 7295 un->un_pm_timeid = 7296 timeout(sd_pm_timeout_handler, 7297 un, intvlp * drv_usectohz(1000000)); 7298 mutex_exit(&un->un_pm_mutex); 7299 (void) pm_busy_component(SD_DEVINFO(un), 0); 7300 } else { 7301 mutex_exit(&un->un_pm_mutex); 7302 } 7303 if (got_semaphore_here != 0) { 7304 sema_v(&un->un_semoclose); 7305 } 7306 /* 7307 * On exit put the state back to it's original value 7308 * and broadcast to anyone waiting for the power 7309 * change completion. 7310 */ 7311 mutex_enter(SD_MUTEX(un)); 7312 un->un_state = state_before_pm; 7313 cv_broadcast(&un->un_suspend_cv); 7314 mutex_exit(SD_MUTEX(un)); 7315 7316 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7317 "trans check Failed, not ok to power cycle.\n"); 7318 return (DDI_FAILURE); 7319 7320 case -1: 7321 if (got_semaphore_here != 0) { 7322 sema_v(&un->un_semoclose); 7323 } 7324 /* 7325 * On exit put the state back to it's original value 7326 * and broadcast to anyone waiting for the power 7327 * change completion. 7328 */ 7329 mutex_enter(SD_MUTEX(un)); 7330 un->un_state = state_before_pm; 7331 cv_broadcast(&un->un_suspend_cv); 7332 mutex_exit(SD_MUTEX(un)); 7333 SD_TRACE(SD_LOG_IO_PM, un, 7334 "sdpower: exit, trans check command Failed.\n"); 7335 return (DDI_FAILURE); 7336 } 7337 } 7338 7339 if (level == SD_SPINDLE_OFF) { 7340 /* 7341 * Save the last state... if the STOP FAILS we need it 7342 * for restoring 7343 */ 7344 mutex_enter(SD_MUTEX(un)); 7345 save_state = un->un_last_state; 7346 /* 7347 * There must not be any cmds. getting processed 7348 * in the driver when we get here. Power to the 7349 * device is potentially going off. 7350 */ 7351 ASSERT(un->un_ncmds_in_driver == 0); 7352 mutex_exit(SD_MUTEX(un)); 7353 7354 /* 7355 * For now suspend the device completely before spindle is 7356 * turned off 7357 */ 7358 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7359 if (got_semaphore_here != 0) { 7360 sema_v(&un->un_semoclose); 7361 } 7362 /* 7363 * On exit put the state back to it's original value 7364 * and broadcast to anyone waiting for the power 7365 * change completion. 7366 */ 7367 mutex_enter(SD_MUTEX(un)); 7368 un->un_state = state_before_pm; 7369 cv_broadcast(&un->un_suspend_cv); 7370 mutex_exit(SD_MUTEX(un)); 7371 SD_TRACE(SD_LOG_IO_PM, un, 7372 "sdpower: exit, PM suspend Failed.\n"); 7373 return (DDI_FAILURE); 7374 } 7375 } 7376 7377 /* 7378 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7379 * close, or strategy. Dump no long uses this routine, it uses it's 7380 * own code so it can be done in polled mode. 7381 */ 7382 7383 medium_present = TRUE; 7384 7385 /* 7386 * When powering up, issue a TUR in case the device is at unit 7387 * attention. Don't do retries. Bypass the PM layer, otherwise 7388 * a deadlock on un_pm_busy_cv will occur. 7389 */ 7390 if (level == SD_SPINDLE_ON) { 7391 (void) sd_send_scsi_TEST_UNIT_READY(un, 7392 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7393 } 7394 7395 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7396 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7397 7398 sval = sd_send_scsi_START_STOP_UNIT(un, 7399 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7400 SD_PATH_DIRECT); 7401 /* Command failed, check for media present. */ 7402 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7403 medium_present = FALSE; 7404 } 7405 7406 /* 7407 * The conditions of interest here are: 7408 * if a spindle off with media present fails, 7409 * then restore the state and return an error. 7410 * else if a spindle on fails, 7411 * then return an error (there's no state to restore). 7412 * In all other cases we setup for the new state 7413 * and return success. 7414 */ 7415 switch (level) { 7416 case SD_SPINDLE_OFF: 7417 if ((medium_present == TRUE) && (sval != 0)) { 7418 /* The stop command from above failed */ 7419 rval = DDI_FAILURE; 7420 /* 7421 * The stop command failed, and we have media 7422 * present. Put the level back by calling the 7423 * sd_pm_resume() and set the state back to 7424 * it's previous value. 7425 */ 7426 (void) sd_ddi_pm_resume(un); 7427 mutex_enter(SD_MUTEX(un)); 7428 un->un_last_state = save_state; 7429 mutex_exit(SD_MUTEX(un)); 7430 break; 7431 } 7432 /* 7433 * The stop command from above succeeded. 7434 */ 7435 if (un->un_f_monitor_media_state) { 7436 /* 7437 * Terminate watch thread in case of removable media 7438 * devices going into low power state. This is as per 7439 * the requirements of pm framework, otherwise commands 7440 * will be generated for the device (through watch 7441 * thread), even when the device is in low power state. 7442 */ 7443 mutex_enter(SD_MUTEX(un)); 7444 un->un_f_watcht_stopped = FALSE; 7445 if (un->un_swr_token != NULL) { 7446 opaque_t temp_token = un->un_swr_token; 7447 un->un_f_watcht_stopped = TRUE; 7448 un->un_swr_token = NULL; 7449 mutex_exit(SD_MUTEX(un)); 7450 (void) scsi_watch_request_terminate(temp_token, 7451 SCSI_WATCH_TERMINATE_WAIT); 7452 } else { 7453 mutex_exit(SD_MUTEX(un)); 7454 } 7455 } 7456 break; 7457 7458 default: /* The level requested is spindle on... */ 7459 /* 7460 * Legacy behavior: return success on a failed spinup 7461 * if there is no media in the drive. 7462 * Do this by looking at medium_present here. 7463 */ 7464 if ((sval != 0) && medium_present) { 7465 /* The start command from above failed */ 7466 rval = DDI_FAILURE; 7467 break; 7468 } 7469 /* 7470 * The start command from above succeeded 7471 * Resume the devices now that we have 7472 * started the disks 7473 */ 7474 (void) sd_ddi_pm_resume(un); 7475 7476 /* 7477 * Resume the watch thread since it was suspended 7478 * when the device went into low power mode. 7479 */ 7480 if (un->un_f_monitor_media_state) { 7481 mutex_enter(SD_MUTEX(un)); 7482 if (un->un_f_watcht_stopped == TRUE) { 7483 opaque_t temp_token; 7484 7485 un->un_f_watcht_stopped = FALSE; 7486 mutex_exit(SD_MUTEX(un)); 7487 temp_token = scsi_watch_request_submit( 7488 SD_SCSI_DEVP(un), 7489 sd_check_media_time, 7490 SENSE_LENGTH, sd_media_watch_cb, 7491 (caddr_t)dev); 7492 mutex_enter(SD_MUTEX(un)); 7493 un->un_swr_token = temp_token; 7494 } 7495 mutex_exit(SD_MUTEX(un)); 7496 } 7497 } 7498 if (got_semaphore_here != 0) { 7499 sema_v(&un->un_semoclose); 7500 } 7501 /* 7502 * On exit put the state back to it's original value 7503 * and broadcast to anyone waiting for the power 7504 * change completion. 7505 */ 7506 mutex_enter(SD_MUTEX(un)); 7507 un->un_state = state_before_pm; 7508 cv_broadcast(&un->un_suspend_cv); 7509 mutex_exit(SD_MUTEX(un)); 7510 7511 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7512 7513 return (rval); 7514 } 7515 7516 7517 7518 /* 7519 * Function: sdattach 7520 * 7521 * Description: Driver's attach(9e) entry point function. 7522 * 7523 * Arguments: devi - opaque device info handle 7524 * cmd - attach type 7525 * 7526 * Return Code: DDI_SUCCESS 7527 * DDI_FAILURE 7528 * 7529 * Context: Kernel thread context 7530 */ 7531 7532 static int 7533 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7534 { 7535 switch (cmd) { 7536 case DDI_ATTACH: 7537 return (sd_unit_attach(devi)); 7538 case DDI_RESUME: 7539 return (sd_ddi_resume(devi)); 7540 default: 7541 break; 7542 } 7543 return (DDI_FAILURE); 7544 } 7545 7546 7547 /* 7548 * Function: sddetach 7549 * 7550 * Description: Driver's detach(9E) entry point function. 7551 * 7552 * Arguments: devi - opaque device info handle 7553 * cmd - detach type 7554 * 7555 * Return Code: DDI_SUCCESS 7556 * DDI_FAILURE 7557 * 7558 * Context: Kernel thread context 7559 */ 7560 7561 static int 7562 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7563 { 7564 switch (cmd) { 7565 case DDI_DETACH: 7566 return (sd_unit_detach(devi)); 7567 case DDI_SUSPEND: 7568 return (sd_ddi_suspend(devi)); 7569 default: 7570 break; 7571 } 7572 return (DDI_FAILURE); 7573 } 7574 7575 7576 /* 7577 * Function: sd_sync_with_callback 7578 * 7579 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7580 * state while the callback routine is active. 7581 * 7582 * Arguments: un: softstate structure for the instance 7583 * 7584 * Context: Kernel thread context 7585 */ 7586 7587 static void 7588 sd_sync_with_callback(struct sd_lun *un) 7589 { 7590 ASSERT(un != NULL); 7591 7592 mutex_enter(SD_MUTEX(un)); 7593 7594 ASSERT(un->un_in_callback >= 0); 7595 7596 while (un->un_in_callback > 0) { 7597 mutex_exit(SD_MUTEX(un)); 7598 delay(2); 7599 mutex_enter(SD_MUTEX(un)); 7600 } 7601 7602 mutex_exit(SD_MUTEX(un)); 7603 } 7604 7605 /* 7606 * Function: sd_unit_attach 7607 * 7608 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7609 * the soft state structure for the device and performs 7610 * all necessary structure and device initializations. 7611 * 7612 * Arguments: devi: the system's dev_info_t for the device. 7613 * 7614 * Return Code: DDI_SUCCESS if attach is successful. 7615 * DDI_FAILURE if any part of the attach fails. 7616 * 7617 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7618 * Kernel thread context only. Can sleep. 7619 */ 7620 7621 static int 7622 sd_unit_attach(dev_info_t *devi) 7623 { 7624 struct scsi_device *devp; 7625 struct sd_lun *un; 7626 char *variantp; 7627 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7628 int instance; 7629 int rval; 7630 int wc_enabled; 7631 uint64_t capacity; 7632 uint_t lbasize; 7633 7634 /* 7635 * Retrieve the target driver's private data area. This was set 7636 * up by the HBA. 7637 */ 7638 devp = ddi_get_driver_private(devi); 7639 7640 /* 7641 * Since we have no idea what state things were left in by the last 7642 * user of the device, set up some 'default' settings, ie. turn 'em 7643 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7644 * Do this before the scsi_probe, which sends an inquiry. 7645 * This is a fix for bug (4430280). 7646 * Of special importance is wide-xfer. The drive could have been left 7647 * in wide transfer mode by the last driver to communicate with it, 7648 * this includes us. If that's the case, and if the following is not 7649 * setup properly or we don't re-negotiate with the drive prior to 7650 * transferring data to/from the drive, it causes bus parity errors, 7651 * data overruns, and unexpected interrupts. This first occurred when 7652 * the fix for bug (4378686) was made. 7653 */ 7654 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7655 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7656 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7657 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7658 7659 /* 7660 * Use scsi_probe() to issue an INQUIRY command to the device. 7661 * This call will allocate and fill in the scsi_inquiry structure 7662 * and point the sd_inq member of the scsi_device structure to it. 7663 * If the attach succeeds, then this memory will not be de-allocated 7664 * (via scsi_unprobe()) until the instance is detached. 7665 */ 7666 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7667 goto probe_failed; 7668 } 7669 7670 /* 7671 * Check the device type as specified in the inquiry data and 7672 * claim it if it is of a type that we support. 7673 */ 7674 switch (devp->sd_inq->inq_dtype) { 7675 case DTYPE_DIRECT: 7676 break; 7677 case DTYPE_RODIRECT: 7678 break; 7679 case DTYPE_OPTICAL: 7680 break; 7681 case DTYPE_NOTPRESENT: 7682 default: 7683 /* Unsupported device type; fail the attach. */ 7684 goto probe_failed; 7685 } 7686 7687 /* 7688 * Allocate the soft state structure for this unit. 7689 * 7690 * We rely upon this memory being set to all zeroes by 7691 * ddi_soft_state_zalloc(). We assume that any member of the 7692 * soft state structure that is not explicitly initialized by 7693 * this routine will have a value of zero. 7694 */ 7695 instance = ddi_get_instance(devp->sd_dev); 7696 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7697 goto probe_failed; 7698 } 7699 7700 /* 7701 * Retrieve a pointer to the newly-allocated soft state. 7702 * 7703 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7704 * was successful, unless something has gone horribly wrong and the 7705 * ddi's soft state internals are corrupt (in which case it is 7706 * probably better to halt here than just fail the attach....) 7707 */ 7708 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7709 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7710 instance); 7711 /*NOTREACHED*/ 7712 } 7713 7714 /* 7715 * Link the back ptr of the driver soft state to the scsi_device 7716 * struct for this lun. 7717 * Save a pointer to the softstate in the driver-private area of 7718 * the scsi_device struct. 7719 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7720 * we first set un->un_sd below. 7721 */ 7722 un->un_sd = devp; 7723 devp->sd_private = (opaque_t)un; 7724 7725 /* 7726 * The following must be after devp is stored in the soft state struct. 7727 */ 7728 #ifdef SDDEBUG 7729 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7730 "%s_unit_attach: un:0x%p instance:%d\n", 7731 ddi_driver_name(devi), un, instance); 7732 #endif 7733 7734 /* 7735 * Set up the device type and node type (for the minor nodes). 7736 * By default we assume that the device can at least support the 7737 * Common Command Set. Call it a CD-ROM if it reports itself 7738 * as a RODIRECT device. 7739 */ 7740 switch (devp->sd_inq->inq_dtype) { 7741 case DTYPE_RODIRECT: 7742 un->un_node_type = DDI_NT_CD_CHAN; 7743 un->un_ctype = CTYPE_CDROM; 7744 break; 7745 case DTYPE_OPTICAL: 7746 un->un_node_type = DDI_NT_BLOCK_CHAN; 7747 un->un_ctype = CTYPE_ROD; 7748 break; 7749 default: 7750 un->un_node_type = DDI_NT_BLOCK_CHAN; 7751 un->un_ctype = CTYPE_CCS; 7752 break; 7753 } 7754 7755 /* 7756 * Try to read the interconnect type from the HBA. 7757 * 7758 * Note: This driver is currently compiled as two binaries, a parallel 7759 * scsi version (sd) and a fibre channel version (ssd). All functional 7760 * differences are determined at compile time. In the future a single 7761 * binary will be provided and the inteconnect type will be used to 7762 * differentiate between fibre and parallel scsi behaviors. At that time 7763 * it will be necessary for all fibre channel HBAs to support this 7764 * property. 7765 * 7766 * set un_f_is_fiber to TRUE ( default fiber ) 7767 */ 7768 un->un_f_is_fibre = TRUE; 7769 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7770 case INTERCONNECT_SSA: 7771 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7772 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7773 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7774 break; 7775 case INTERCONNECT_PARALLEL: 7776 un->un_f_is_fibre = FALSE; 7777 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7778 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7779 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7780 break; 7781 case INTERCONNECT_FIBRE: 7782 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7783 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7784 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7785 break; 7786 case INTERCONNECT_FABRIC: 7787 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7788 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7789 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7790 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7791 break; 7792 default: 7793 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7794 /* 7795 * The HBA does not support the "interconnect-type" property 7796 * (or did not provide a recognized type). 7797 * 7798 * Note: This will be obsoleted when a single fibre channel 7799 * and parallel scsi driver is delivered. In the meantime the 7800 * interconnect type will be set to the platform default.If that 7801 * type is not parallel SCSI, it means that we should be 7802 * assuming "ssd" semantics. However, here this also means that 7803 * the FC HBA is not supporting the "interconnect-type" property 7804 * like we expect it to, so log this occurrence. 7805 */ 7806 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7807 if (!SD_IS_PARALLEL_SCSI(un)) { 7808 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7809 "sd_unit_attach: un:0x%p Assuming " 7810 "INTERCONNECT_FIBRE\n", un); 7811 } else { 7812 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7813 "sd_unit_attach: un:0x%p Assuming " 7814 "INTERCONNECT_PARALLEL\n", un); 7815 un->un_f_is_fibre = FALSE; 7816 } 7817 #else 7818 /* 7819 * Note: This source will be implemented when a single fibre 7820 * channel and parallel scsi driver is delivered. The default 7821 * will be to assume that if a device does not support the 7822 * "interconnect-type" property it is a parallel SCSI HBA and 7823 * we will set the interconnect type for parallel scsi. 7824 */ 7825 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7826 un->un_f_is_fibre = FALSE; 7827 #endif 7828 break; 7829 } 7830 7831 if (un->un_f_is_fibre == TRUE) { 7832 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7833 SCSI_VERSION_3) { 7834 switch (un->un_interconnect_type) { 7835 case SD_INTERCONNECT_FIBRE: 7836 case SD_INTERCONNECT_SSA: 7837 un->un_node_type = DDI_NT_BLOCK_WWN; 7838 break; 7839 default: 7840 break; 7841 } 7842 } 7843 } 7844 7845 /* 7846 * Initialize the Request Sense command for the target 7847 */ 7848 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7849 goto alloc_rqs_failed; 7850 } 7851 7852 /* 7853 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7854 * with seperate binary for sd and ssd. 7855 * 7856 * x86 has 1 binary, un_retry_count is set base on connection type. 7857 * The hardcoded values will go away when Sparc uses 1 binary 7858 * for sd and ssd. This hardcoded values need to match 7859 * SD_RETRY_COUNT in sddef.h 7860 * The value used is base on interconnect type. 7861 * fibre = 3, parallel = 5 7862 */ 7863 #if defined(__i386) || defined(__amd64) 7864 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7865 #else 7866 un->un_retry_count = SD_RETRY_COUNT; 7867 #endif 7868 7869 /* 7870 * Set the per disk retry count to the default number of retries 7871 * for disks and CDROMs. This value can be overridden by the 7872 * disk property list or an entry in sd.conf. 7873 */ 7874 un->un_notready_retry_count = 7875 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7876 : DISK_NOT_READY_RETRY_COUNT(un); 7877 7878 /* 7879 * Set the busy retry count to the default value of un_retry_count. 7880 * This can be overridden by entries in sd.conf or the device 7881 * config table. 7882 */ 7883 un->un_busy_retry_count = un->un_retry_count; 7884 7885 /* 7886 * Init the reset threshold for retries. This number determines 7887 * how many retries must be performed before a reset can be issued 7888 * (for certain error conditions). This can be overridden by entries 7889 * in sd.conf or the device config table. 7890 */ 7891 un->un_reset_retry_count = (un->un_retry_count / 2); 7892 7893 /* 7894 * Set the victim_retry_count to the default un_retry_count 7895 */ 7896 un->un_victim_retry_count = (2 * un->un_retry_count); 7897 7898 /* 7899 * Set the reservation release timeout to the default value of 7900 * 5 seconds. This can be overridden by entries in ssd.conf or the 7901 * device config table. 7902 */ 7903 un->un_reserve_release_time = 5; 7904 7905 /* 7906 * Set up the default maximum transfer size. Note that this may 7907 * get updated later in the attach, when setting up default wide 7908 * operations for disks. 7909 */ 7910 #if defined(__i386) || defined(__amd64) 7911 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7912 #else 7913 un->un_max_xfer_size = (uint_t)maxphys; 7914 #endif 7915 7916 /* 7917 * Get "allow bus device reset" property (defaults to "enabled" if 7918 * the property was not defined). This is to disable bus resets for 7919 * certain kinds of error recovery. Note: In the future when a run-time 7920 * fibre check is available the soft state flag should default to 7921 * enabled. 7922 */ 7923 if (un->un_f_is_fibre == TRUE) { 7924 un->un_f_allow_bus_device_reset = TRUE; 7925 } else { 7926 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7927 "allow-bus-device-reset", 1) != 0) { 7928 un->un_f_allow_bus_device_reset = TRUE; 7929 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7930 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 7931 un); 7932 } else { 7933 un->un_f_allow_bus_device_reset = FALSE; 7934 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7935 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 7936 un); 7937 } 7938 } 7939 7940 /* 7941 * Check if this is an ATAPI device. ATAPI devices use Group 1 7942 * Read/Write commands and Group 2 Mode Sense/Select commands. 7943 * 7944 * Note: The "obsolete" way of doing this is to check for the "atapi" 7945 * property. The new "variant" property with a value of "atapi" has been 7946 * introduced so that future 'variants' of standard SCSI behavior (like 7947 * atapi) could be specified by the underlying HBA drivers by supplying 7948 * a new value for the "variant" property, instead of having to define a 7949 * new property. 7950 */ 7951 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7952 un->un_f_cfg_is_atapi = TRUE; 7953 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7954 "sd_unit_attach: un:0x%p Atapi device\n", un); 7955 } 7956 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7957 &variantp) == DDI_PROP_SUCCESS) { 7958 if (strcmp(variantp, "atapi") == 0) { 7959 un->un_f_cfg_is_atapi = TRUE; 7960 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7961 "sd_unit_attach: un:0x%p Atapi device\n", un); 7962 } 7963 ddi_prop_free(variantp); 7964 } 7965 7966 un->un_cmd_timeout = SD_IO_TIME; 7967 7968 /* Info on current states, statuses, etc. (Updated frequently) */ 7969 un->un_state = SD_STATE_NORMAL; 7970 un->un_last_state = SD_STATE_NORMAL; 7971 7972 /* Control & status info for command throttling */ 7973 un->un_throttle = sd_max_throttle; 7974 un->un_saved_throttle = sd_max_throttle; 7975 un->un_min_throttle = sd_min_throttle; 7976 7977 if (un->un_f_is_fibre == TRUE) { 7978 un->un_f_use_adaptive_throttle = TRUE; 7979 } else { 7980 un->un_f_use_adaptive_throttle = FALSE; 7981 } 7982 7983 /* Removable media support. */ 7984 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7985 un->un_mediastate = DKIO_NONE; 7986 un->un_specified_mediastate = DKIO_NONE; 7987 7988 /* CVs for suspend/resume (PM or DR) */ 7989 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7990 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7991 7992 /* Power management support. */ 7993 un->un_power_level = SD_SPINDLE_UNINIT; 7994 7995 /* 7996 * The open/close semaphore is used to serialize threads executing 7997 * in the driver's open & close entry point routines for a given 7998 * instance. 7999 */ 8000 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8001 8002 /* 8003 * The conf file entry and softstate variable is a forceful override, 8004 * meaning a non-zero value must be entered to change the default. 8005 */ 8006 un->un_f_disksort_disabled = FALSE; 8007 8008 /* 8009 * Retrieve the properties from the static driver table or the driver 8010 * configuration file (.conf) for this unit and update the soft state 8011 * for the device as needed for the indicated properties. 8012 * Note: the property configuration needs to occur here as some of the 8013 * following routines may have dependancies on soft state flags set 8014 * as part of the driver property configuration. 8015 */ 8016 sd_read_unit_properties(un); 8017 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8018 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8019 8020 /* 8021 * Only if a device has "hotpluggable" property, it is 8022 * treated as hotpluggable device. Otherwise, it is 8023 * regarded as non-hotpluggable one. 8024 */ 8025 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8026 -1) != -1) { 8027 un->un_f_is_hotpluggable = TRUE; 8028 } 8029 8030 /* 8031 * set unit's attributes(flags) according to "hotpluggable" and 8032 * RMB bit in INQUIRY data. 8033 */ 8034 sd_set_unit_attributes(un, devi); 8035 8036 /* 8037 * By default, we mark the capacity, lbasize, and geometry 8038 * as invalid. Only if we successfully read a valid capacity 8039 * will we update the un_blockcount and un_tgt_blocksize with the 8040 * valid values (the geometry will be validated later). 8041 */ 8042 un->un_f_blockcount_is_valid = FALSE; 8043 un->un_f_tgt_blocksize_is_valid = FALSE; 8044 un->un_f_geometry_is_valid = FALSE; 8045 8046 /* 8047 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8048 * otherwise. 8049 */ 8050 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8051 un->un_blockcount = 0; 8052 8053 /* 8054 * Set up the per-instance info needed to determine the correct 8055 * CDBs and other info for issuing commands to the target. 8056 */ 8057 sd_init_cdb_limits(un); 8058 8059 /* 8060 * Set up the IO chains to use, based upon the target type. 8061 */ 8062 if (un->un_f_non_devbsize_supported) { 8063 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8064 } else { 8065 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8066 } 8067 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8068 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8069 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8070 8071 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8072 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8073 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8074 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8075 8076 8077 if (ISCD(un)) { 8078 un->un_additional_codes = sd_additional_codes; 8079 } else { 8080 un->un_additional_codes = NULL; 8081 } 8082 8083 /* 8084 * Create the kstats here so they can be available for attach-time 8085 * routines that send commands to the unit (either polled or via 8086 * sd_send_scsi_cmd). 8087 * 8088 * Note: This is a critical sequence that needs to be maintained: 8089 * 1) Instantiate the kstats here, before any routines using the 8090 * iopath (i.e. sd_send_scsi_cmd). 8091 * 2) Initialize the error stats (sd_set_errstats) and partition 8092 * stats (sd_set_pstats), following sd_validate_geometry(), 8093 * sd_register_devid(), and sd_disable_caching(). 8094 */ 8095 8096 un->un_stats = kstat_create(sd_label, instance, 8097 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8098 if (un->un_stats != NULL) { 8099 un->un_stats->ks_lock = SD_MUTEX(un); 8100 kstat_install(un->un_stats); 8101 } 8102 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8103 "sd_unit_attach: un:0x%p un_stats created\n", un); 8104 8105 sd_create_errstats(un, instance); 8106 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8107 "sd_unit_attach: un:0x%p errstats created\n", un); 8108 8109 /* 8110 * The following if/else code was relocated here from below as part 8111 * of the fix for bug (4430280). However with the default setup added 8112 * on entry to this routine, it's no longer absolutely necessary for 8113 * this to be before the call to sd_spin_up_unit. 8114 */ 8115 if (SD_IS_PARALLEL_SCSI(un)) { 8116 /* 8117 * If SCSI-2 tagged queueing is supported by the target 8118 * and by the host adapter then we will enable it. 8119 */ 8120 un->un_tagflags = 0; 8121 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8122 (devp->sd_inq->inq_cmdque) && 8123 (un->un_f_arq_enabled == TRUE)) { 8124 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8125 1, 1) == 1) { 8126 un->un_tagflags = FLAG_STAG; 8127 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8128 "sd_unit_attach: un:0x%p tag queueing " 8129 "enabled\n", un); 8130 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8131 "untagged-qing", 0) == 1) { 8132 un->un_f_opt_queueing = TRUE; 8133 un->un_saved_throttle = un->un_throttle = 8134 min(un->un_throttle, 3); 8135 } else { 8136 un->un_f_opt_queueing = FALSE; 8137 un->un_saved_throttle = un->un_throttle = 1; 8138 } 8139 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8140 == 1) && (un->un_f_arq_enabled == TRUE)) { 8141 /* The Host Adapter supports internal queueing. */ 8142 un->un_f_opt_queueing = TRUE; 8143 un->un_saved_throttle = un->un_throttle = 8144 min(un->un_throttle, 3); 8145 } else { 8146 un->un_f_opt_queueing = FALSE; 8147 un->un_saved_throttle = un->un_throttle = 1; 8148 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8149 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8150 } 8151 8152 8153 /* Setup or tear down default wide operations for disks */ 8154 8155 /* 8156 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8157 * and "ssd_max_xfer_size" to exist simultaneously on the same 8158 * system and be set to different values. In the future this 8159 * code may need to be updated when the ssd module is 8160 * obsoleted and removed from the system. (4299588) 8161 */ 8162 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8163 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8164 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8165 1, 1) == 1) { 8166 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8167 "sd_unit_attach: un:0x%p Wide Transfer " 8168 "enabled\n", un); 8169 } 8170 8171 /* 8172 * If tagged queuing has also been enabled, then 8173 * enable large xfers 8174 */ 8175 if (un->un_saved_throttle == sd_max_throttle) { 8176 un->un_max_xfer_size = 8177 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8178 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8179 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8180 "sd_unit_attach: un:0x%p max transfer " 8181 "size=0x%x\n", un, un->un_max_xfer_size); 8182 } 8183 } else { 8184 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8185 0, 1) == 1) { 8186 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8187 "sd_unit_attach: un:0x%p " 8188 "Wide Transfer disabled\n", un); 8189 } 8190 } 8191 } else { 8192 un->un_tagflags = FLAG_STAG; 8193 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8194 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8195 } 8196 8197 /* 8198 * If this target supports LUN reset, try to enable it. 8199 */ 8200 if (un->un_f_lun_reset_enabled) { 8201 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8202 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8203 "un:0x%p lun_reset capability set\n", un); 8204 } else { 8205 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8206 "un:0x%p lun-reset capability not set\n", un); 8207 } 8208 } 8209 8210 /* 8211 * At this point in the attach, we have enough info in the 8212 * soft state to be able to issue commands to the target. 8213 * 8214 * All command paths used below MUST issue their commands as 8215 * SD_PATH_DIRECT. This is important as intermediate layers 8216 * are not all initialized yet (such as PM). 8217 */ 8218 8219 /* 8220 * Send a TEST UNIT READY command to the device. This should clear 8221 * any outstanding UNIT ATTENTION that may be present. 8222 * 8223 * Note: Don't check for success, just track if there is a reservation, 8224 * this is a throw away command to clear any unit attentions. 8225 * 8226 * Note: This MUST be the first command issued to the target during 8227 * attach to ensure power on UNIT ATTENTIONS are cleared. 8228 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8229 * with attempts at spinning up a device with no media. 8230 */ 8231 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8232 reservation_flag = SD_TARGET_IS_RESERVED; 8233 } 8234 8235 /* 8236 * If the device is NOT a removable media device, attempt to spin 8237 * it up (using the START_STOP_UNIT command) and read its capacity 8238 * (using the READ CAPACITY command). Note, however, that either 8239 * of these could fail and in some cases we would continue with 8240 * the attach despite the failure (see below). 8241 */ 8242 if (un->un_f_descr_format_supported) { 8243 switch (sd_spin_up_unit(un)) { 8244 case 0: 8245 /* 8246 * Spin-up was successful; now try to read the 8247 * capacity. If successful then save the results 8248 * and mark the capacity & lbasize as valid. 8249 */ 8250 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8251 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8252 8253 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8254 &lbasize, SD_PATH_DIRECT)) { 8255 case 0: { 8256 if (capacity > DK_MAX_BLOCKS) { 8257 #ifdef _LP64 8258 /* 8259 * Enable descriptor format sense data 8260 * so that we can get 64 bit sense 8261 * data fields. 8262 */ 8263 sd_enable_descr_sense(un); 8264 #else 8265 /* 32-bit kernels can't handle this */ 8266 scsi_log(SD_DEVINFO(un), 8267 sd_label, CE_WARN, 8268 "disk has %llu blocks, which " 8269 "is too large for a 32-bit " 8270 "kernel", capacity); 8271 goto spinup_failed; 8272 #endif 8273 } 8274 /* 8275 * The following relies on 8276 * sd_send_scsi_READ_CAPACITY never 8277 * returning 0 for capacity and/or lbasize. 8278 */ 8279 sd_update_block_info(un, lbasize, capacity); 8280 8281 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8282 "sd_unit_attach: un:0x%p capacity = %ld " 8283 "blocks; lbasize= %ld.\n", un, 8284 un->un_blockcount, un->un_tgt_blocksize); 8285 8286 break; 8287 } 8288 case EACCES: 8289 /* 8290 * Should never get here if the spin-up 8291 * succeeded, but code it in anyway. 8292 * From here, just continue with the attach... 8293 */ 8294 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8295 "sd_unit_attach: un:0x%p " 8296 "sd_send_scsi_READ_CAPACITY " 8297 "returned reservation conflict\n", un); 8298 reservation_flag = SD_TARGET_IS_RESERVED; 8299 break; 8300 default: 8301 /* 8302 * Likewise, should never get here if the 8303 * spin-up succeeded. Just continue with 8304 * the attach... 8305 */ 8306 break; 8307 } 8308 break; 8309 case EACCES: 8310 /* 8311 * Device is reserved by another host. In this case 8312 * we could not spin it up or read the capacity, but 8313 * we continue with the attach anyway. 8314 */ 8315 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8316 "sd_unit_attach: un:0x%p spin-up reservation " 8317 "conflict.\n", un); 8318 reservation_flag = SD_TARGET_IS_RESERVED; 8319 break; 8320 default: 8321 /* Fail the attach if the spin-up failed. */ 8322 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8323 "sd_unit_attach: un:0x%p spin-up failed.", un); 8324 goto spinup_failed; 8325 } 8326 } 8327 8328 /* 8329 * Check to see if this is a MMC drive 8330 */ 8331 if (ISCD(un)) { 8332 sd_set_mmc_caps(un); 8333 } 8334 8335 /* 8336 * Create the minor nodes for the device. 8337 * Note: If we want to support fdisk on both sparc and intel, this will 8338 * have to separate out the notion that VTOC8 is always sparc, and 8339 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8340 * type will have to be determined at run-time, and the fdisk 8341 * partitioning will have to have been read & set up before we 8342 * create the minor nodes. (any other inits (such as kstats) that 8343 * also ought to be done before creating the minor nodes?) (Doesn't 8344 * setting up the minor nodes kind of imply that we're ready to 8345 * handle an open from userland?) 8346 */ 8347 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8348 goto create_minor_nodes_failed; 8349 } 8350 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8351 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8352 8353 /* 8354 * Add a zero-length attribute to tell the world we support 8355 * kernel ioctls (for layered drivers) 8356 */ 8357 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8358 DDI_KERNEL_IOCTL, NULL, 0); 8359 8360 /* 8361 * Add a boolean property to tell the world we support 8362 * the B_FAILFAST flag (for layered drivers) 8363 */ 8364 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8365 "ddi-failfast-supported", NULL, 0); 8366 8367 /* 8368 * Initialize power management 8369 */ 8370 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8371 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8372 sd_setup_pm(un, devi); 8373 if (un->un_f_pm_is_enabled == FALSE) { 8374 /* 8375 * For performance, point to a jump table that does 8376 * not include pm. 8377 * The direct and priority chains don't change with PM. 8378 * 8379 * Note: this is currently done based on individual device 8380 * capabilities. When an interface for determining system 8381 * power enabled state becomes available, or when additional 8382 * layers are added to the command chain, these values will 8383 * have to be re-evaluated for correctness. 8384 */ 8385 if (un->un_f_non_devbsize_supported) { 8386 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8387 } else { 8388 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8389 } 8390 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8391 } 8392 8393 /* 8394 * This property is set to 0 by HA software to avoid retries 8395 * on a reserved disk. (The preferred property name is 8396 * "retry-on-reservation-conflict") (1189689) 8397 * 8398 * Note: The use of a global here can have unintended consequences. A 8399 * per instance variable is preferrable to match the capabilities of 8400 * different underlying hba's (4402600) 8401 */ 8402 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8403 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8404 sd_retry_on_reservation_conflict); 8405 if (sd_retry_on_reservation_conflict != 0) { 8406 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8407 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8408 sd_retry_on_reservation_conflict); 8409 } 8410 8411 /* Set up options for QFULL handling. */ 8412 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8413 "qfull-retries", -1)) != -1) { 8414 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8415 rval, 1); 8416 } 8417 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8418 "qfull-retry-interval", -1)) != -1) { 8419 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8420 rval, 1); 8421 } 8422 8423 /* 8424 * This just prints a message that announces the existence of the 8425 * device. The message is always printed in the system logfile, but 8426 * only appears on the console if the system is booted with the 8427 * -v (verbose) argument. 8428 */ 8429 ddi_report_dev(devi); 8430 8431 /* 8432 * The framework calls driver attach routines single-threaded 8433 * for a given instance. However we still acquire SD_MUTEX here 8434 * because this required for calling the sd_validate_geometry() 8435 * and sd_register_devid() functions. 8436 */ 8437 mutex_enter(SD_MUTEX(un)); 8438 un->un_f_geometry_is_valid = FALSE; 8439 un->un_mediastate = DKIO_NONE; 8440 un->un_reserved = -1; 8441 8442 /* 8443 * Read and validate the device's geometry (ie, disk label) 8444 * A new unformatted drive will not have a valid geometry, but 8445 * the driver needs to successfully attach to this device so 8446 * the drive can be formatted via ioctls. 8447 */ 8448 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8449 ENOTSUP)) && 8450 (un->un_blockcount < DK_MAX_BLOCKS)) { 8451 /* 8452 * We found a small disk with an EFI label on it; 8453 * we need to fix up the minor nodes accordingly. 8454 */ 8455 ddi_remove_minor_node(devi, "h"); 8456 ddi_remove_minor_node(devi, "h,raw"); 8457 (void) ddi_create_minor_node(devi, "wd", 8458 S_IFBLK, 8459 (instance << SDUNIT_SHIFT) | WD_NODE, 8460 un->un_node_type, NULL); 8461 (void) ddi_create_minor_node(devi, "wd,raw", 8462 S_IFCHR, 8463 (instance << SDUNIT_SHIFT) | WD_NODE, 8464 un->un_node_type, NULL); 8465 } 8466 8467 /* 8468 * Read and initialize the devid for the unit. 8469 */ 8470 ASSERT(un->un_errstats != NULL); 8471 if (un->un_f_devid_supported) { 8472 sd_register_devid(un, devi, reservation_flag); 8473 } 8474 mutex_exit(SD_MUTEX(un)); 8475 8476 #if (defined(__fibre)) 8477 /* 8478 * Register callbacks for fibre only. You can't do this soley 8479 * on the basis of the devid_type because this is hba specific. 8480 * We need to query our hba capabilities to find out whether to 8481 * register or not. 8482 */ 8483 if (un->un_f_is_fibre) { 8484 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8485 sd_init_event_callbacks(un); 8486 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8487 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8488 } 8489 } 8490 #endif 8491 8492 if (un->un_f_opt_disable_cache == TRUE) { 8493 if (sd_disable_caching(un) != 0) { 8494 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8495 "sd_unit_attach: un:0x%p Could not disable " 8496 "caching", un); 8497 goto devid_failed; 8498 } 8499 } 8500 8501 /* 8502 * NOTE: Since there is currently no mechanism to 8503 * change the state of the Write Cache Enable mode select, 8504 * this code just checks the value of the WCE bit 8505 * at device attach time. If a mechanism 8506 * is added to the driver to change WCE, un_f_write_cache_enabled 8507 * must be updated appropriately. 8508 */ 8509 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8510 mutex_enter(SD_MUTEX(un)); 8511 un->un_f_write_cache_enabled = (wc_enabled != 0); 8512 mutex_exit(SD_MUTEX(un)); 8513 8514 /* 8515 * Set the pstat and error stat values here, so data obtained during the 8516 * previous attach-time routines is available. 8517 * 8518 * Note: This is a critical sequence that needs to be maintained: 8519 * 1) Instantiate the kstats before any routines using the iopath 8520 * (i.e. sd_send_scsi_cmd). 8521 * 2) Initialize the error stats (sd_set_errstats) and partition 8522 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8523 * sd_register_devid(), and sd_disable_caching(). 8524 */ 8525 if (un->un_f_pkstats_enabled) { 8526 sd_set_pstats(un); 8527 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8528 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8529 } 8530 8531 sd_set_errstats(un); 8532 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8533 "sd_unit_attach: un:0x%p errstats set\n", un); 8534 8535 /* 8536 * Find out what type of reservation this disk supports. 8537 */ 8538 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8539 case 0: 8540 /* 8541 * SCSI-3 reservations are supported. 8542 */ 8543 un->un_reservation_type = SD_SCSI3_RESERVATION; 8544 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8545 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8546 break; 8547 case ENOTSUP: 8548 /* 8549 * The PERSISTENT RESERVE IN command would not be recognized by 8550 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8551 */ 8552 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8553 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8554 un->un_reservation_type = SD_SCSI2_RESERVATION; 8555 break; 8556 default: 8557 /* 8558 * default to SCSI-3 reservations 8559 */ 8560 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8561 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8562 un->un_reservation_type = SD_SCSI3_RESERVATION; 8563 break; 8564 } 8565 8566 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8567 "sd_unit_attach: un:0x%p exit success\n", un); 8568 8569 return (DDI_SUCCESS); 8570 8571 /* 8572 * An error occurred during the attach; clean up & return failure. 8573 */ 8574 8575 devid_failed: 8576 8577 setup_pm_failed: 8578 ddi_remove_minor_node(devi, NULL); 8579 8580 create_minor_nodes_failed: 8581 /* 8582 * Cleanup from the scsi_ifsetcap() calls (437868) 8583 */ 8584 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8585 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8586 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8587 8588 if (un->un_f_is_fibre == FALSE) { 8589 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8590 } 8591 8592 spinup_failed: 8593 8594 mutex_enter(SD_MUTEX(un)); 8595 8596 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8597 if (un->un_direct_priority_timeid != NULL) { 8598 timeout_id_t temp_id = un->un_direct_priority_timeid; 8599 un->un_direct_priority_timeid = NULL; 8600 mutex_exit(SD_MUTEX(un)); 8601 (void) untimeout(temp_id); 8602 mutex_enter(SD_MUTEX(un)); 8603 } 8604 8605 /* Cancel any pending start/stop timeouts */ 8606 if (un->un_startstop_timeid != NULL) { 8607 timeout_id_t temp_id = un->un_startstop_timeid; 8608 un->un_startstop_timeid = NULL; 8609 mutex_exit(SD_MUTEX(un)); 8610 (void) untimeout(temp_id); 8611 mutex_enter(SD_MUTEX(un)); 8612 } 8613 8614 /* Cancel any pending reset-throttle timeouts */ 8615 if (un->un_reset_throttle_timeid != NULL) { 8616 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8617 un->un_reset_throttle_timeid = NULL; 8618 mutex_exit(SD_MUTEX(un)); 8619 (void) untimeout(temp_id); 8620 mutex_enter(SD_MUTEX(un)); 8621 } 8622 8623 /* Cancel any pending retry timeouts */ 8624 if (un->un_retry_timeid != NULL) { 8625 timeout_id_t temp_id = un->un_retry_timeid; 8626 un->un_retry_timeid = NULL; 8627 mutex_exit(SD_MUTEX(un)); 8628 (void) untimeout(temp_id); 8629 mutex_enter(SD_MUTEX(un)); 8630 } 8631 8632 /* Cancel any pending delayed cv broadcast timeouts */ 8633 if (un->un_dcvb_timeid != NULL) { 8634 timeout_id_t temp_id = un->un_dcvb_timeid; 8635 un->un_dcvb_timeid = NULL; 8636 mutex_exit(SD_MUTEX(un)); 8637 (void) untimeout(temp_id); 8638 mutex_enter(SD_MUTEX(un)); 8639 } 8640 8641 mutex_exit(SD_MUTEX(un)); 8642 8643 /* There should not be any in-progress I/O so ASSERT this check */ 8644 ASSERT(un->un_ncmds_in_transport == 0); 8645 ASSERT(un->un_ncmds_in_driver == 0); 8646 8647 /* Do not free the softstate if the callback routine is active */ 8648 sd_sync_with_callback(un); 8649 8650 /* 8651 * Partition stats apparently are not used with removables. These would 8652 * not have been created during attach, so no need to clean them up... 8653 */ 8654 if (un->un_stats != NULL) { 8655 kstat_delete(un->un_stats); 8656 un->un_stats = NULL; 8657 } 8658 if (un->un_errstats != NULL) { 8659 kstat_delete(un->un_errstats); 8660 un->un_errstats = NULL; 8661 } 8662 8663 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8664 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8665 8666 ddi_prop_remove_all(devi); 8667 sema_destroy(&un->un_semoclose); 8668 cv_destroy(&un->un_state_cv); 8669 8670 getrbuf_failed: 8671 8672 sd_free_rqs(un); 8673 8674 alloc_rqs_failed: 8675 8676 devp->sd_private = NULL; 8677 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8678 8679 get_softstate_failed: 8680 /* 8681 * Note: the man pages are unclear as to whether or not doing a 8682 * ddi_soft_state_free(sd_state, instance) is the right way to 8683 * clean up after the ddi_soft_state_zalloc() if the subsequent 8684 * ddi_get_soft_state() fails. The implication seems to be 8685 * that the get_soft_state cannot fail if the zalloc succeeds. 8686 */ 8687 ddi_soft_state_free(sd_state, instance); 8688 8689 probe_failed: 8690 scsi_unprobe(devp); 8691 #ifdef SDDEBUG 8692 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8693 (sd_level_mask & SD_LOGMASK_TRACE)) { 8694 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8695 (void *)un); 8696 } 8697 #endif 8698 return (DDI_FAILURE); 8699 } 8700 8701 8702 /* 8703 * Function: sd_unit_detach 8704 * 8705 * Description: Performs DDI_DETACH processing for sddetach(). 8706 * 8707 * Return Code: DDI_SUCCESS 8708 * DDI_FAILURE 8709 * 8710 * Context: Kernel thread context 8711 */ 8712 8713 static int 8714 sd_unit_detach(dev_info_t *devi) 8715 { 8716 struct scsi_device *devp; 8717 struct sd_lun *un; 8718 int i; 8719 dev_t dev; 8720 int instance = ddi_get_instance(devi); 8721 8722 mutex_enter(&sd_detach_mutex); 8723 8724 /* 8725 * Fail the detach for any of the following: 8726 * - Unable to get the sd_lun struct for the instance 8727 * - A layered driver has an outstanding open on the instance 8728 * - Another thread is already detaching this instance 8729 * - Another thread is currently performing an open 8730 */ 8731 devp = ddi_get_driver_private(devi); 8732 if ((devp == NULL) || 8733 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8734 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8735 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8736 mutex_exit(&sd_detach_mutex); 8737 return (DDI_FAILURE); 8738 } 8739 8740 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8741 8742 /* 8743 * Mark this instance as currently in a detach, to inhibit any 8744 * opens from a layered driver. 8745 */ 8746 un->un_detach_count++; 8747 mutex_exit(&sd_detach_mutex); 8748 8749 dev = sd_make_device(SD_DEVINFO(un)); 8750 8751 _NOTE(COMPETING_THREADS_NOW); 8752 8753 mutex_enter(SD_MUTEX(un)); 8754 8755 /* 8756 * Fail the detach if there are any outstanding layered 8757 * opens on this device. 8758 */ 8759 for (i = 0; i < NDKMAP; i++) { 8760 if (un->un_ocmap.lyropen[i] != 0) { 8761 goto err_notclosed; 8762 } 8763 } 8764 8765 /* 8766 * Verify there are NO outstanding commands issued to this device. 8767 * ie, un_ncmds_in_transport == 0. 8768 * It's possible to have outstanding commands through the physio 8769 * code path, even though everything's closed. 8770 */ 8771 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8772 (un->un_direct_priority_timeid != NULL) || 8773 (un->un_state == SD_STATE_RWAIT)) { 8774 mutex_exit(SD_MUTEX(un)); 8775 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8776 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8777 goto err_stillbusy; 8778 } 8779 8780 /* 8781 * If we have the device reserved, release the reservation. 8782 */ 8783 if ((un->un_resvd_status & SD_RESERVE) && 8784 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8785 mutex_exit(SD_MUTEX(un)); 8786 /* 8787 * Note: sd_reserve_release sends a command to the device 8788 * via the sd_ioctlcmd() path, and can sleep. 8789 */ 8790 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8791 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8792 "sd_dr_detach: Cannot release reservation \n"); 8793 } 8794 } else { 8795 mutex_exit(SD_MUTEX(un)); 8796 } 8797 8798 /* 8799 * Untimeout any reserve recover, throttle reset, restart unit 8800 * and delayed broadcast timeout threads. Protect the timeout pointer 8801 * from getting nulled by their callback functions. 8802 */ 8803 mutex_enter(SD_MUTEX(un)); 8804 if (un->un_resvd_timeid != NULL) { 8805 timeout_id_t temp_id = un->un_resvd_timeid; 8806 un->un_resvd_timeid = NULL; 8807 mutex_exit(SD_MUTEX(un)); 8808 (void) untimeout(temp_id); 8809 mutex_enter(SD_MUTEX(un)); 8810 } 8811 8812 if (un->un_reset_throttle_timeid != NULL) { 8813 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8814 un->un_reset_throttle_timeid = NULL; 8815 mutex_exit(SD_MUTEX(un)); 8816 (void) untimeout(temp_id); 8817 mutex_enter(SD_MUTEX(un)); 8818 } 8819 8820 if (un->un_startstop_timeid != NULL) { 8821 timeout_id_t temp_id = un->un_startstop_timeid; 8822 un->un_startstop_timeid = NULL; 8823 mutex_exit(SD_MUTEX(un)); 8824 (void) untimeout(temp_id); 8825 mutex_enter(SD_MUTEX(un)); 8826 } 8827 8828 if (un->un_dcvb_timeid != NULL) { 8829 timeout_id_t temp_id = un->un_dcvb_timeid; 8830 un->un_dcvb_timeid = NULL; 8831 mutex_exit(SD_MUTEX(un)); 8832 (void) untimeout(temp_id); 8833 } else { 8834 mutex_exit(SD_MUTEX(un)); 8835 } 8836 8837 /* Remove any pending reservation reclaim requests for this device */ 8838 sd_rmv_resv_reclaim_req(dev); 8839 8840 mutex_enter(SD_MUTEX(un)); 8841 8842 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8843 if (un->un_direct_priority_timeid != NULL) { 8844 timeout_id_t temp_id = un->un_direct_priority_timeid; 8845 un->un_direct_priority_timeid = NULL; 8846 mutex_exit(SD_MUTEX(un)); 8847 (void) untimeout(temp_id); 8848 mutex_enter(SD_MUTEX(un)); 8849 } 8850 8851 /* Cancel any active multi-host disk watch thread requests */ 8852 if (un->un_mhd_token != NULL) { 8853 mutex_exit(SD_MUTEX(un)); 8854 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8855 if (scsi_watch_request_terminate(un->un_mhd_token, 8856 SCSI_WATCH_TERMINATE_NOWAIT)) { 8857 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8858 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8859 /* 8860 * Note: We are returning here after having removed 8861 * some driver timeouts above. This is consistent with 8862 * the legacy implementation but perhaps the watch 8863 * terminate call should be made with the wait flag set. 8864 */ 8865 goto err_stillbusy; 8866 } 8867 mutex_enter(SD_MUTEX(un)); 8868 un->un_mhd_token = NULL; 8869 } 8870 8871 if (un->un_swr_token != NULL) { 8872 mutex_exit(SD_MUTEX(un)); 8873 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8874 if (scsi_watch_request_terminate(un->un_swr_token, 8875 SCSI_WATCH_TERMINATE_NOWAIT)) { 8876 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8877 "sd_dr_detach: Cannot cancel swr watch request\n"); 8878 /* 8879 * Note: We are returning here after having removed 8880 * some driver timeouts above. This is consistent with 8881 * the legacy implementation but perhaps the watch 8882 * terminate call should be made with the wait flag set. 8883 */ 8884 goto err_stillbusy; 8885 } 8886 mutex_enter(SD_MUTEX(un)); 8887 un->un_swr_token = NULL; 8888 } 8889 8890 mutex_exit(SD_MUTEX(un)); 8891 8892 /* 8893 * Clear any scsi_reset_notifies. We clear the reset notifies 8894 * if we have not registered one. 8895 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8896 */ 8897 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8898 sd_mhd_reset_notify_cb, (caddr_t)un); 8899 8900 /* 8901 * protect the timeout pointers from getting nulled by 8902 * their callback functions during the cancellation process. 8903 * In such a scenario untimeout can be invoked with a null value. 8904 */ 8905 _NOTE(NO_COMPETING_THREADS_NOW); 8906 8907 mutex_enter(&un->un_pm_mutex); 8908 if (un->un_pm_idle_timeid != NULL) { 8909 timeout_id_t temp_id = un->un_pm_idle_timeid; 8910 un->un_pm_idle_timeid = NULL; 8911 mutex_exit(&un->un_pm_mutex); 8912 8913 /* 8914 * Timeout is active; cancel it. 8915 * Note that it'll never be active on a device 8916 * that does not support PM therefore we don't 8917 * have to check before calling pm_idle_component. 8918 */ 8919 (void) untimeout(temp_id); 8920 (void) pm_idle_component(SD_DEVINFO(un), 0); 8921 mutex_enter(&un->un_pm_mutex); 8922 } 8923 8924 /* 8925 * Check whether there is already a timeout scheduled for power 8926 * management. If yes then don't lower the power here, that's. 8927 * the timeout handler's job. 8928 */ 8929 if (un->un_pm_timeid != NULL) { 8930 timeout_id_t temp_id = un->un_pm_timeid; 8931 un->un_pm_timeid = NULL; 8932 mutex_exit(&un->un_pm_mutex); 8933 /* 8934 * Timeout is active; cancel it. 8935 * Note that it'll never be active on a device 8936 * that does not support PM therefore we don't 8937 * have to check before calling pm_idle_component. 8938 */ 8939 (void) untimeout(temp_id); 8940 (void) pm_idle_component(SD_DEVINFO(un), 0); 8941 8942 } else { 8943 mutex_exit(&un->un_pm_mutex); 8944 if ((un->un_f_pm_is_enabled == TRUE) && 8945 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8946 DDI_SUCCESS)) { 8947 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8948 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8949 /* 8950 * Fix for bug: 4297749, item # 13 8951 * The above test now includes a check to see if PM is 8952 * supported by this device before call 8953 * pm_lower_power(). 8954 * Note, the following is not dead code. The call to 8955 * pm_lower_power above will generate a call back into 8956 * our sdpower routine which might result in a timeout 8957 * handler getting activated. Therefore the following 8958 * code is valid and necessary. 8959 */ 8960 mutex_enter(&un->un_pm_mutex); 8961 if (un->un_pm_timeid != NULL) { 8962 timeout_id_t temp_id = un->un_pm_timeid; 8963 un->un_pm_timeid = NULL; 8964 mutex_exit(&un->un_pm_mutex); 8965 (void) untimeout(temp_id); 8966 (void) pm_idle_component(SD_DEVINFO(un), 0); 8967 } else { 8968 mutex_exit(&un->un_pm_mutex); 8969 } 8970 } 8971 } 8972 8973 /* 8974 * Cleanup from the scsi_ifsetcap() calls (437868) 8975 * Relocated here from above to be after the call to 8976 * pm_lower_power, which was getting errors. 8977 */ 8978 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8979 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8980 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8981 8982 if (un->un_f_is_fibre == FALSE) { 8983 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8984 } 8985 8986 /* 8987 * Remove any event callbacks, fibre only 8988 */ 8989 if (un->un_f_is_fibre == TRUE) { 8990 if ((un->un_insert_event != NULL) && 8991 (ddi_remove_event_handler(un->un_insert_cb_id) != 8992 DDI_SUCCESS)) { 8993 /* 8994 * Note: We are returning here after having done 8995 * substantial cleanup above. This is consistent 8996 * with the legacy implementation but this may not 8997 * be the right thing to do. 8998 */ 8999 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9000 "sd_dr_detach: Cannot cancel insert event\n"); 9001 goto err_remove_event; 9002 } 9003 un->un_insert_event = NULL; 9004 9005 if ((un->un_remove_event != NULL) && 9006 (ddi_remove_event_handler(un->un_remove_cb_id) != 9007 DDI_SUCCESS)) { 9008 /* 9009 * Note: We are returning here after having done 9010 * substantial cleanup above. This is consistent 9011 * with the legacy implementation but this may not 9012 * be the right thing to do. 9013 */ 9014 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9015 "sd_dr_detach: Cannot cancel remove event\n"); 9016 goto err_remove_event; 9017 } 9018 un->un_remove_event = NULL; 9019 } 9020 9021 /* Do not free the softstate if the callback routine is active */ 9022 sd_sync_with_callback(un); 9023 9024 /* 9025 * Hold the detach mutex here, to make sure that no other threads ever 9026 * can access a (partially) freed soft state structure. 9027 */ 9028 mutex_enter(&sd_detach_mutex); 9029 9030 /* 9031 * Clean up the soft state struct. 9032 * Cleanup is done in reverse order of allocs/inits. 9033 * At this point there should be no competing threads anymore. 9034 */ 9035 9036 /* Unregister and free device id. */ 9037 ddi_devid_unregister(devi); 9038 if (un->un_devid) { 9039 ddi_devid_free(un->un_devid); 9040 un->un_devid = NULL; 9041 } 9042 9043 /* 9044 * Destroy wmap cache if it exists. 9045 */ 9046 if (un->un_wm_cache != NULL) { 9047 kmem_cache_destroy(un->un_wm_cache); 9048 un->un_wm_cache = NULL; 9049 } 9050 9051 /* Remove minor nodes */ 9052 ddi_remove_minor_node(devi, NULL); 9053 9054 /* 9055 * kstat cleanup is done in detach for all device types (4363169). 9056 * We do not want to fail detach if the device kstats are not deleted 9057 * since there is a confusion about the devo_refcnt for the device. 9058 * We just delete the kstats and let detach complete successfully. 9059 */ 9060 if (un->un_stats != NULL) { 9061 kstat_delete(un->un_stats); 9062 un->un_stats = NULL; 9063 } 9064 if (un->un_errstats != NULL) { 9065 kstat_delete(un->un_errstats); 9066 un->un_errstats = NULL; 9067 } 9068 9069 /* Remove partition stats */ 9070 if (un->un_f_pkstats_enabled) { 9071 for (i = 0; i < NSDMAP; i++) { 9072 if (un->un_pstats[i] != NULL) { 9073 kstat_delete(un->un_pstats[i]); 9074 un->un_pstats[i] = NULL; 9075 } 9076 } 9077 } 9078 9079 /* Remove xbuf registration */ 9080 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9081 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9082 9083 /* Remove driver properties */ 9084 ddi_prop_remove_all(devi); 9085 9086 mutex_destroy(&un->un_pm_mutex); 9087 cv_destroy(&un->un_pm_busy_cv); 9088 9089 /* Open/close semaphore */ 9090 sema_destroy(&un->un_semoclose); 9091 9092 /* Removable media condvar. */ 9093 cv_destroy(&un->un_state_cv); 9094 9095 /* Suspend/resume condvar. */ 9096 cv_destroy(&un->un_suspend_cv); 9097 cv_destroy(&un->un_disk_busy_cv); 9098 9099 sd_free_rqs(un); 9100 9101 /* Free up soft state */ 9102 devp->sd_private = NULL; 9103 bzero(un, sizeof (struct sd_lun)); 9104 ddi_soft_state_free(sd_state, instance); 9105 9106 mutex_exit(&sd_detach_mutex); 9107 9108 /* This frees up the INQUIRY data associated with the device. */ 9109 scsi_unprobe(devp); 9110 9111 return (DDI_SUCCESS); 9112 9113 err_notclosed: 9114 mutex_exit(SD_MUTEX(un)); 9115 9116 err_stillbusy: 9117 _NOTE(NO_COMPETING_THREADS_NOW); 9118 9119 err_remove_event: 9120 mutex_enter(&sd_detach_mutex); 9121 un->un_detach_count--; 9122 mutex_exit(&sd_detach_mutex); 9123 9124 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9125 return (DDI_FAILURE); 9126 } 9127 9128 9129 /* 9130 * Driver minor node structure and data table 9131 */ 9132 struct driver_minor_data { 9133 char *name; 9134 minor_t minor; 9135 int type; 9136 }; 9137 9138 static struct driver_minor_data sd_minor_data[] = { 9139 {"a", 0, S_IFBLK}, 9140 {"b", 1, S_IFBLK}, 9141 {"c", 2, S_IFBLK}, 9142 {"d", 3, S_IFBLK}, 9143 {"e", 4, S_IFBLK}, 9144 {"f", 5, S_IFBLK}, 9145 {"g", 6, S_IFBLK}, 9146 {"h", 7, S_IFBLK}, 9147 #if defined(_SUNOS_VTOC_16) 9148 {"i", 8, S_IFBLK}, 9149 {"j", 9, S_IFBLK}, 9150 {"k", 10, S_IFBLK}, 9151 {"l", 11, S_IFBLK}, 9152 {"m", 12, S_IFBLK}, 9153 {"n", 13, S_IFBLK}, 9154 {"o", 14, S_IFBLK}, 9155 {"p", 15, S_IFBLK}, 9156 #endif /* defined(_SUNOS_VTOC_16) */ 9157 #if defined(_FIRMWARE_NEEDS_FDISK) 9158 {"q", 16, S_IFBLK}, 9159 {"r", 17, S_IFBLK}, 9160 {"s", 18, S_IFBLK}, 9161 {"t", 19, S_IFBLK}, 9162 {"u", 20, S_IFBLK}, 9163 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9164 {"a,raw", 0, S_IFCHR}, 9165 {"b,raw", 1, S_IFCHR}, 9166 {"c,raw", 2, S_IFCHR}, 9167 {"d,raw", 3, S_IFCHR}, 9168 {"e,raw", 4, S_IFCHR}, 9169 {"f,raw", 5, S_IFCHR}, 9170 {"g,raw", 6, S_IFCHR}, 9171 {"h,raw", 7, S_IFCHR}, 9172 #if defined(_SUNOS_VTOC_16) 9173 {"i,raw", 8, S_IFCHR}, 9174 {"j,raw", 9, S_IFCHR}, 9175 {"k,raw", 10, S_IFCHR}, 9176 {"l,raw", 11, S_IFCHR}, 9177 {"m,raw", 12, S_IFCHR}, 9178 {"n,raw", 13, S_IFCHR}, 9179 {"o,raw", 14, S_IFCHR}, 9180 {"p,raw", 15, S_IFCHR}, 9181 #endif /* defined(_SUNOS_VTOC_16) */ 9182 #if defined(_FIRMWARE_NEEDS_FDISK) 9183 {"q,raw", 16, S_IFCHR}, 9184 {"r,raw", 17, S_IFCHR}, 9185 {"s,raw", 18, S_IFCHR}, 9186 {"t,raw", 19, S_IFCHR}, 9187 {"u,raw", 20, S_IFCHR}, 9188 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9189 {0} 9190 }; 9191 9192 static struct driver_minor_data sd_minor_data_efi[] = { 9193 {"a", 0, S_IFBLK}, 9194 {"b", 1, S_IFBLK}, 9195 {"c", 2, S_IFBLK}, 9196 {"d", 3, S_IFBLK}, 9197 {"e", 4, S_IFBLK}, 9198 {"f", 5, S_IFBLK}, 9199 {"g", 6, S_IFBLK}, 9200 {"wd", 7, S_IFBLK}, 9201 #if defined(_FIRMWARE_NEEDS_FDISK) 9202 {"q", 16, S_IFBLK}, 9203 {"r", 17, S_IFBLK}, 9204 {"s", 18, S_IFBLK}, 9205 {"t", 19, S_IFBLK}, 9206 {"u", 20, S_IFBLK}, 9207 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9208 {"a,raw", 0, S_IFCHR}, 9209 {"b,raw", 1, S_IFCHR}, 9210 {"c,raw", 2, S_IFCHR}, 9211 {"d,raw", 3, S_IFCHR}, 9212 {"e,raw", 4, S_IFCHR}, 9213 {"f,raw", 5, S_IFCHR}, 9214 {"g,raw", 6, S_IFCHR}, 9215 {"wd,raw", 7, S_IFCHR}, 9216 #if defined(_FIRMWARE_NEEDS_FDISK) 9217 {"q,raw", 16, S_IFCHR}, 9218 {"r,raw", 17, S_IFCHR}, 9219 {"s,raw", 18, S_IFCHR}, 9220 {"t,raw", 19, S_IFCHR}, 9221 {"u,raw", 20, S_IFCHR}, 9222 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9223 {0} 9224 }; 9225 9226 9227 /* 9228 * Function: sd_create_minor_nodes 9229 * 9230 * Description: Create the minor device nodes for the instance. 9231 * 9232 * Arguments: un - driver soft state (unit) structure 9233 * devi - pointer to device info structure 9234 * 9235 * Return Code: DDI_SUCCESS 9236 * DDI_FAILURE 9237 * 9238 * Context: Kernel thread context 9239 */ 9240 9241 static int 9242 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9243 { 9244 struct driver_minor_data *dmdp; 9245 struct scsi_device *devp; 9246 int instance; 9247 char name[48]; 9248 9249 ASSERT(un != NULL); 9250 devp = ddi_get_driver_private(devi); 9251 instance = ddi_get_instance(devp->sd_dev); 9252 9253 /* 9254 * Create all the minor nodes for this target. 9255 */ 9256 if (un->un_blockcount > DK_MAX_BLOCKS) 9257 dmdp = sd_minor_data_efi; 9258 else 9259 dmdp = sd_minor_data; 9260 while (dmdp->name != NULL) { 9261 9262 (void) sprintf(name, "%s", dmdp->name); 9263 9264 if (ddi_create_minor_node(devi, name, dmdp->type, 9265 (instance << SDUNIT_SHIFT) | dmdp->minor, 9266 un->un_node_type, NULL) == DDI_FAILURE) { 9267 /* 9268 * Clean up any nodes that may have been created, in 9269 * case this fails in the middle of the loop. 9270 */ 9271 ddi_remove_minor_node(devi, NULL); 9272 return (DDI_FAILURE); 9273 } 9274 dmdp++; 9275 } 9276 9277 return (DDI_SUCCESS); 9278 } 9279 9280 9281 /* 9282 * Function: sd_create_errstats 9283 * 9284 * Description: This routine instantiates the device error stats. 9285 * 9286 * Note: During attach the stats are instantiated first so they are 9287 * available for attach-time routines that utilize the driver 9288 * iopath to send commands to the device. The stats are initialized 9289 * separately so data obtained during some attach-time routines is 9290 * available. (4362483) 9291 * 9292 * Arguments: un - driver soft state (unit) structure 9293 * instance - driver instance 9294 * 9295 * Context: Kernel thread context 9296 */ 9297 9298 static void 9299 sd_create_errstats(struct sd_lun *un, int instance) 9300 { 9301 struct sd_errstats *stp; 9302 char kstatmodule_err[KSTAT_STRLEN]; 9303 char kstatname[KSTAT_STRLEN]; 9304 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9305 9306 ASSERT(un != NULL); 9307 9308 if (un->un_errstats != NULL) { 9309 return; 9310 } 9311 9312 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9313 "%serr", sd_label); 9314 (void) snprintf(kstatname, sizeof (kstatname), 9315 "%s%d,err", sd_label, instance); 9316 9317 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9318 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9319 9320 if (un->un_errstats == NULL) { 9321 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9322 "sd_create_errstats: Failed kstat_create\n"); 9323 return; 9324 } 9325 9326 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9327 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9328 KSTAT_DATA_UINT32); 9329 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9330 KSTAT_DATA_UINT32); 9331 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9332 KSTAT_DATA_UINT32); 9333 kstat_named_init(&stp->sd_vid, "Vendor", 9334 KSTAT_DATA_CHAR); 9335 kstat_named_init(&stp->sd_pid, "Product", 9336 KSTAT_DATA_CHAR); 9337 kstat_named_init(&stp->sd_revision, "Revision", 9338 KSTAT_DATA_CHAR); 9339 kstat_named_init(&stp->sd_serial, "Serial No", 9340 KSTAT_DATA_CHAR); 9341 kstat_named_init(&stp->sd_capacity, "Size", 9342 KSTAT_DATA_ULONGLONG); 9343 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9344 KSTAT_DATA_UINT32); 9345 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9346 KSTAT_DATA_UINT32); 9347 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9348 KSTAT_DATA_UINT32); 9349 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9350 KSTAT_DATA_UINT32); 9351 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9352 KSTAT_DATA_UINT32); 9353 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9354 KSTAT_DATA_UINT32); 9355 9356 un->un_errstats->ks_private = un; 9357 un->un_errstats->ks_update = nulldev; 9358 9359 kstat_install(un->un_errstats); 9360 } 9361 9362 9363 /* 9364 * Function: sd_set_errstats 9365 * 9366 * Description: This routine sets the value of the vendor id, product id, 9367 * revision, serial number, and capacity device error stats. 9368 * 9369 * Note: During attach the stats are instantiated first so they are 9370 * available for attach-time routines that utilize the driver 9371 * iopath to send commands to the device. The stats are initialized 9372 * separately so data obtained during some attach-time routines is 9373 * available. (4362483) 9374 * 9375 * Arguments: un - driver soft state (unit) structure 9376 * 9377 * Context: Kernel thread context 9378 */ 9379 9380 static void 9381 sd_set_errstats(struct sd_lun *un) 9382 { 9383 struct sd_errstats *stp; 9384 9385 ASSERT(un != NULL); 9386 ASSERT(un->un_errstats != NULL); 9387 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9388 ASSERT(stp != NULL); 9389 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9390 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9391 (void) strncpy(stp->sd_revision.value.c, 9392 un->un_sd->sd_inq->inq_revision, 4); 9393 9394 /* 9395 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9396 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9397 * (4376302)) 9398 */ 9399 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9400 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9401 sizeof (SD_INQUIRY(un)->inq_serial)); 9402 } 9403 9404 if (un->un_f_blockcount_is_valid != TRUE) { 9405 /* 9406 * Set capacity error stat to 0 for no media. This ensures 9407 * a valid capacity is displayed in response to 'iostat -E' 9408 * when no media is present in the device. 9409 */ 9410 stp->sd_capacity.value.ui64 = 0; 9411 } else { 9412 /* 9413 * Multiply un_blockcount by un->un_sys_blocksize to get 9414 * capacity. 9415 * 9416 * Note: for non-512 blocksize devices "un_blockcount" has been 9417 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9418 * (un_tgt_blocksize / un->un_sys_blocksize). 9419 */ 9420 stp->sd_capacity.value.ui64 = (uint64_t) 9421 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9422 } 9423 } 9424 9425 9426 /* 9427 * Function: sd_set_pstats 9428 * 9429 * Description: This routine instantiates and initializes the partition 9430 * stats for each partition with more than zero blocks. 9431 * (4363169) 9432 * 9433 * Arguments: un - driver soft state (unit) structure 9434 * 9435 * Context: Kernel thread context 9436 */ 9437 9438 static void 9439 sd_set_pstats(struct sd_lun *un) 9440 { 9441 char kstatname[KSTAT_STRLEN]; 9442 int instance; 9443 int i; 9444 9445 ASSERT(un != NULL); 9446 9447 instance = ddi_get_instance(SD_DEVINFO(un)); 9448 9449 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9450 for (i = 0; i < NSDMAP; i++) { 9451 if ((un->un_pstats[i] == NULL) && 9452 (un->un_map[i].dkl_nblk != 0)) { 9453 (void) snprintf(kstatname, sizeof (kstatname), 9454 "%s%d,%s", sd_label, instance, 9455 sd_minor_data[i].name); 9456 un->un_pstats[i] = kstat_create(sd_label, 9457 instance, kstatname, "partition", KSTAT_TYPE_IO, 9458 1, KSTAT_FLAG_PERSISTENT); 9459 if (un->un_pstats[i] != NULL) { 9460 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9461 kstat_install(un->un_pstats[i]); 9462 } 9463 } 9464 } 9465 } 9466 9467 9468 #if (defined(__fibre)) 9469 /* 9470 * Function: sd_init_event_callbacks 9471 * 9472 * Description: This routine initializes the insertion and removal event 9473 * callbacks. (fibre only) 9474 * 9475 * Arguments: un - driver soft state (unit) structure 9476 * 9477 * Context: Kernel thread context 9478 */ 9479 9480 static void 9481 sd_init_event_callbacks(struct sd_lun *un) 9482 { 9483 ASSERT(un != NULL); 9484 9485 if ((un->un_insert_event == NULL) && 9486 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9487 &un->un_insert_event) == DDI_SUCCESS)) { 9488 /* 9489 * Add the callback for an insertion event 9490 */ 9491 (void) ddi_add_event_handler(SD_DEVINFO(un), 9492 un->un_insert_event, sd_event_callback, (void *)un, 9493 &(un->un_insert_cb_id)); 9494 } 9495 9496 if ((un->un_remove_event == NULL) && 9497 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9498 &un->un_remove_event) == DDI_SUCCESS)) { 9499 /* 9500 * Add the callback for a removal event 9501 */ 9502 (void) ddi_add_event_handler(SD_DEVINFO(un), 9503 un->un_remove_event, sd_event_callback, (void *)un, 9504 &(un->un_remove_cb_id)); 9505 } 9506 } 9507 9508 9509 /* 9510 * Function: sd_event_callback 9511 * 9512 * Description: This routine handles insert/remove events (photon). The 9513 * state is changed to OFFLINE which can be used to supress 9514 * error msgs. (fibre only) 9515 * 9516 * Arguments: un - driver soft state (unit) structure 9517 * 9518 * Context: Callout thread context 9519 */ 9520 /* ARGSUSED */ 9521 static void 9522 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9523 void *bus_impldata) 9524 { 9525 struct sd_lun *un = (struct sd_lun *)arg; 9526 9527 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9528 if (event == un->un_insert_event) { 9529 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9530 mutex_enter(SD_MUTEX(un)); 9531 if (un->un_state == SD_STATE_OFFLINE) { 9532 if (un->un_last_state != SD_STATE_SUSPENDED) { 9533 un->un_state = un->un_last_state; 9534 } else { 9535 /* 9536 * We have gone through SUSPEND/RESUME while 9537 * we were offline. Restore the last state 9538 */ 9539 un->un_state = un->un_save_state; 9540 } 9541 } 9542 mutex_exit(SD_MUTEX(un)); 9543 9544 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9545 } else if (event == un->un_remove_event) { 9546 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9547 mutex_enter(SD_MUTEX(un)); 9548 /* 9549 * We need to handle an event callback that occurs during 9550 * the suspend operation, since we don't prevent it. 9551 */ 9552 if (un->un_state != SD_STATE_OFFLINE) { 9553 if (un->un_state != SD_STATE_SUSPENDED) { 9554 New_state(un, SD_STATE_OFFLINE); 9555 } else { 9556 un->un_last_state = SD_STATE_OFFLINE; 9557 } 9558 } 9559 mutex_exit(SD_MUTEX(un)); 9560 } else { 9561 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9562 "!Unknown event\n"); 9563 } 9564 9565 } 9566 #endif 9567 9568 9569 /* 9570 * Function: sd_disable_caching() 9571 * 9572 * Description: This routine is the driver entry point for disabling 9573 * read and write caching by modifying the WCE (write cache 9574 * enable) and RCD (read cache disable) bits of mode 9575 * page 8 (MODEPAGE_CACHING). 9576 * 9577 * Arguments: un - driver soft state (unit) structure 9578 * 9579 * Return Code: EIO 9580 * code returned by sd_send_scsi_MODE_SENSE and 9581 * sd_send_scsi_MODE_SELECT 9582 * 9583 * Context: Kernel Thread 9584 */ 9585 9586 static int 9587 sd_disable_caching(struct sd_lun *un) 9588 { 9589 struct mode_caching *mode_caching_page; 9590 uchar_t *header; 9591 size_t buflen; 9592 int hdrlen; 9593 int bd_len; 9594 int rval = 0; 9595 9596 ASSERT(un != NULL); 9597 9598 /* 9599 * Do a test unit ready, otherwise a mode sense may not work if this 9600 * is the first command sent to the device after boot. 9601 */ 9602 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9603 9604 if (un->un_f_cfg_is_atapi == TRUE) { 9605 hdrlen = MODE_HEADER_LENGTH_GRP2; 9606 } else { 9607 hdrlen = MODE_HEADER_LENGTH; 9608 } 9609 9610 /* 9611 * Allocate memory for the retrieved mode page and its headers. Set 9612 * a pointer to the page itself. 9613 */ 9614 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9615 header = kmem_zalloc(buflen, KM_SLEEP); 9616 9617 /* Get the information from the device. */ 9618 if (un->un_f_cfg_is_atapi == TRUE) { 9619 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9620 MODEPAGE_CACHING, SD_PATH_DIRECT); 9621 } else { 9622 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9623 MODEPAGE_CACHING, SD_PATH_DIRECT); 9624 } 9625 if (rval != 0) { 9626 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9627 "sd_disable_caching: Mode Sense Failed\n"); 9628 kmem_free(header, buflen); 9629 return (rval); 9630 } 9631 9632 /* 9633 * Determine size of Block Descriptors in order to locate 9634 * the mode page data. ATAPI devices return 0, SCSI devices 9635 * should return MODE_BLK_DESC_LENGTH. 9636 */ 9637 if (un->un_f_cfg_is_atapi == TRUE) { 9638 struct mode_header_grp2 *mhp; 9639 mhp = (struct mode_header_grp2 *)header; 9640 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9641 } else { 9642 bd_len = ((struct mode_header *)header)->bdesc_length; 9643 } 9644 9645 if (bd_len > MODE_BLK_DESC_LENGTH) { 9646 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9647 "sd_disable_caching: Mode Sense returned invalid " 9648 "block descriptor length\n"); 9649 kmem_free(header, buflen); 9650 return (EIO); 9651 } 9652 9653 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9654 9655 /* Check the relevant bits on successful mode sense. */ 9656 if ((mode_caching_page->wce) || !(mode_caching_page->rcd)) { 9657 /* 9658 * Read or write caching is enabled. Disable both of them. 9659 */ 9660 mode_caching_page->wce = 0; 9661 mode_caching_page->rcd = 1; 9662 9663 /* Clear reserved bits before mode select. */ 9664 mode_caching_page->mode_page.ps = 0; 9665 9666 /* 9667 * Clear out mode header for mode select. 9668 * The rest of the retrieved page will be reused. 9669 */ 9670 bzero(header, hdrlen); 9671 9672 /* Change the cache page to disable all caching. */ 9673 if (un->un_f_cfg_is_atapi == TRUE) { 9674 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9675 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9676 } else { 9677 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9678 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9679 } 9680 } 9681 9682 kmem_free(header, buflen); 9683 return (rval); 9684 } 9685 9686 9687 /* 9688 * Function: sd_get_write_cache_enabled() 9689 * 9690 * Description: This routine is the driver entry point for determining if 9691 * write caching is enabled. It examines the WCE (write cache 9692 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9693 * 9694 * Arguments: un - driver soft state (unit) structure 9695 * is_enabled - pointer to int where write cache enabled state 9696 * is returned (non-zero -> write cache enabled) 9697 * 9698 * 9699 * Return Code: EIO 9700 * code returned by sd_send_scsi_MODE_SENSE 9701 * 9702 * Context: Kernel Thread 9703 * 9704 * NOTE: If ioctl is added to disable write cache, this sequence should 9705 * be followed so that no locking is required for accesses to 9706 * un->un_f_write_cache_enabled: 9707 * do mode select to clear wce 9708 * do synchronize cache to flush cache 9709 * set un->un_f_write_cache_enabled = FALSE 9710 * 9711 * Conversely, an ioctl to enable the write cache should be done 9712 * in this order: 9713 * set un->un_f_write_cache_enabled = TRUE 9714 * do mode select to set wce 9715 */ 9716 9717 static int 9718 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 9719 { 9720 struct mode_caching *mode_caching_page; 9721 uchar_t *header; 9722 size_t buflen; 9723 int hdrlen; 9724 int bd_len; 9725 int rval = 0; 9726 9727 ASSERT(un != NULL); 9728 ASSERT(is_enabled != NULL); 9729 9730 /* in case of error, flag as enabled */ 9731 *is_enabled = TRUE; 9732 9733 /* 9734 * Do a test unit ready, otherwise a mode sense may not work if this 9735 * is the first command sent to the device after boot. 9736 */ 9737 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9738 9739 if (un->un_f_cfg_is_atapi == TRUE) { 9740 hdrlen = MODE_HEADER_LENGTH_GRP2; 9741 } else { 9742 hdrlen = MODE_HEADER_LENGTH; 9743 } 9744 9745 /* 9746 * Allocate memory for the retrieved mode page and its headers. Set 9747 * a pointer to the page itself. 9748 */ 9749 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9750 header = kmem_zalloc(buflen, KM_SLEEP); 9751 9752 /* Get the information from the device. */ 9753 if (un->un_f_cfg_is_atapi == TRUE) { 9754 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9755 MODEPAGE_CACHING, SD_PATH_DIRECT); 9756 } else { 9757 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9758 MODEPAGE_CACHING, SD_PATH_DIRECT); 9759 } 9760 if (rval != 0) { 9761 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9762 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9763 kmem_free(header, buflen); 9764 return (rval); 9765 } 9766 9767 /* 9768 * Determine size of Block Descriptors in order to locate 9769 * the mode page data. ATAPI devices return 0, SCSI devices 9770 * should return MODE_BLK_DESC_LENGTH. 9771 */ 9772 if (un->un_f_cfg_is_atapi == TRUE) { 9773 struct mode_header_grp2 *mhp; 9774 mhp = (struct mode_header_grp2 *)header; 9775 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9776 } else { 9777 bd_len = ((struct mode_header *)header)->bdesc_length; 9778 } 9779 9780 if (bd_len > MODE_BLK_DESC_LENGTH) { 9781 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9782 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9783 "block descriptor length\n"); 9784 kmem_free(header, buflen); 9785 return (EIO); 9786 } 9787 9788 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9789 *is_enabled = mode_caching_page->wce; 9790 9791 kmem_free(header, buflen); 9792 return (0); 9793 } 9794 9795 9796 /* 9797 * Function: sd_make_device 9798 * 9799 * Description: Utility routine to return the Solaris device number from 9800 * the data in the device's dev_info structure. 9801 * 9802 * Return Code: The Solaris device number 9803 * 9804 * Context: Any 9805 */ 9806 9807 static dev_t 9808 sd_make_device(dev_info_t *devi) 9809 { 9810 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 9811 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9812 } 9813 9814 9815 /* 9816 * Function: sd_pm_entry 9817 * 9818 * Description: Called at the start of a new command to manage power 9819 * and busy status of a device. This includes determining whether 9820 * the current power state of the device is sufficient for 9821 * performing the command or whether it must be changed. 9822 * The PM framework is notified appropriately. 9823 * Only with a return status of DDI_SUCCESS will the 9824 * component be busy to the framework. 9825 * 9826 * All callers of sd_pm_entry must check the return status 9827 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9828 * of DDI_FAILURE indicates the device failed to power up. 9829 * In this case un_pm_count has been adjusted so the result 9830 * on exit is still powered down, ie. count is less than 0. 9831 * Calling sd_pm_exit with this count value hits an ASSERT. 9832 * 9833 * Return Code: DDI_SUCCESS or DDI_FAILURE 9834 * 9835 * Context: Kernel thread context. 9836 */ 9837 9838 static int 9839 sd_pm_entry(struct sd_lun *un) 9840 { 9841 int return_status = DDI_SUCCESS; 9842 9843 ASSERT(!mutex_owned(SD_MUTEX(un))); 9844 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9845 9846 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9847 9848 if (un->un_f_pm_is_enabled == FALSE) { 9849 SD_TRACE(SD_LOG_IO_PM, un, 9850 "sd_pm_entry: exiting, PM not enabled\n"); 9851 return (return_status); 9852 } 9853 9854 /* 9855 * Just increment a counter if PM is enabled. On the transition from 9856 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9857 * the count with each IO and mark the device as idle when the count 9858 * hits 0. 9859 * 9860 * If the count is less than 0 the device is powered down. If a powered 9861 * down device is successfully powered up then the count must be 9862 * incremented to reflect the power up. Note that it'll get incremented 9863 * a second time to become busy. 9864 * 9865 * Because the following has the potential to change the device state 9866 * and must release the un_pm_mutex to do so, only one thread can be 9867 * allowed through at a time. 9868 */ 9869 9870 mutex_enter(&un->un_pm_mutex); 9871 while (un->un_pm_busy == TRUE) { 9872 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9873 } 9874 un->un_pm_busy = TRUE; 9875 9876 if (un->un_pm_count < 1) { 9877 9878 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9879 9880 /* 9881 * Indicate we are now busy so the framework won't attempt to 9882 * power down the device. This call will only fail if either 9883 * we passed a bad component number or the device has no 9884 * components. Neither of these should ever happen. 9885 */ 9886 mutex_exit(&un->un_pm_mutex); 9887 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9888 ASSERT(return_status == DDI_SUCCESS); 9889 9890 mutex_enter(&un->un_pm_mutex); 9891 9892 if (un->un_pm_count < 0) { 9893 mutex_exit(&un->un_pm_mutex); 9894 9895 SD_TRACE(SD_LOG_IO_PM, un, 9896 "sd_pm_entry: power up component\n"); 9897 9898 /* 9899 * pm_raise_power will cause sdpower to be called 9900 * which brings the device power level to the 9901 * desired state, ON in this case. If successful, 9902 * un_pm_count and un_power_level will be updated 9903 * appropriately. 9904 */ 9905 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9906 SD_SPINDLE_ON); 9907 9908 mutex_enter(&un->un_pm_mutex); 9909 9910 if (return_status != DDI_SUCCESS) { 9911 /* 9912 * Power up failed. 9913 * Idle the device and adjust the count 9914 * so the result on exit is that we're 9915 * still powered down, ie. count is less than 0. 9916 */ 9917 SD_TRACE(SD_LOG_IO_PM, un, 9918 "sd_pm_entry: power up failed," 9919 " idle the component\n"); 9920 9921 (void) pm_idle_component(SD_DEVINFO(un), 0); 9922 un->un_pm_count--; 9923 } else { 9924 /* 9925 * Device is powered up, verify the 9926 * count is non-negative. 9927 * This is debug only. 9928 */ 9929 ASSERT(un->un_pm_count == 0); 9930 } 9931 } 9932 9933 if (return_status == DDI_SUCCESS) { 9934 /* 9935 * For performance, now that the device has been tagged 9936 * as busy, and it's known to be powered up, update the 9937 * chain types to use jump tables that do not include 9938 * pm. This significantly lowers the overhead and 9939 * therefore improves performance. 9940 */ 9941 9942 mutex_exit(&un->un_pm_mutex); 9943 mutex_enter(SD_MUTEX(un)); 9944 SD_TRACE(SD_LOG_IO_PM, un, 9945 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9946 un->un_uscsi_chain_type); 9947 9948 if (un->un_f_non_devbsize_supported) { 9949 un->un_buf_chain_type = 9950 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9951 } else { 9952 un->un_buf_chain_type = 9953 SD_CHAIN_INFO_DISK_NO_PM; 9954 } 9955 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9956 9957 SD_TRACE(SD_LOG_IO_PM, un, 9958 " changed uscsi_chain_type to %d\n", 9959 un->un_uscsi_chain_type); 9960 mutex_exit(SD_MUTEX(un)); 9961 mutex_enter(&un->un_pm_mutex); 9962 9963 if (un->un_pm_idle_timeid == NULL) { 9964 /* 300 ms. */ 9965 un->un_pm_idle_timeid = 9966 timeout(sd_pm_idletimeout_handler, un, 9967 (drv_usectohz((clock_t)300000))); 9968 /* 9969 * Include an extra call to busy which keeps the 9970 * device busy with-respect-to the PM layer 9971 * until the timer fires, at which time it'll 9972 * get the extra idle call. 9973 */ 9974 (void) pm_busy_component(SD_DEVINFO(un), 0); 9975 } 9976 } 9977 } 9978 un->un_pm_busy = FALSE; 9979 /* Next... */ 9980 cv_signal(&un->un_pm_busy_cv); 9981 9982 un->un_pm_count++; 9983 9984 SD_TRACE(SD_LOG_IO_PM, un, 9985 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 9986 9987 mutex_exit(&un->un_pm_mutex); 9988 9989 return (return_status); 9990 } 9991 9992 9993 /* 9994 * Function: sd_pm_exit 9995 * 9996 * Description: Called at the completion of a command to manage busy 9997 * status for the device. If the device becomes idle the 9998 * PM framework is notified. 9999 * 10000 * Context: Kernel thread context 10001 */ 10002 10003 static void 10004 sd_pm_exit(struct sd_lun *un) 10005 { 10006 ASSERT(!mutex_owned(SD_MUTEX(un))); 10007 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10008 10009 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10010 10011 /* 10012 * After attach the following flag is only read, so don't 10013 * take the penalty of acquiring a mutex for it. 10014 */ 10015 if (un->un_f_pm_is_enabled == TRUE) { 10016 10017 mutex_enter(&un->un_pm_mutex); 10018 un->un_pm_count--; 10019 10020 SD_TRACE(SD_LOG_IO_PM, un, 10021 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10022 10023 ASSERT(un->un_pm_count >= 0); 10024 if (un->un_pm_count == 0) { 10025 mutex_exit(&un->un_pm_mutex); 10026 10027 SD_TRACE(SD_LOG_IO_PM, un, 10028 "sd_pm_exit: idle component\n"); 10029 10030 (void) pm_idle_component(SD_DEVINFO(un), 0); 10031 10032 } else { 10033 mutex_exit(&un->un_pm_mutex); 10034 } 10035 } 10036 10037 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10038 } 10039 10040 10041 /* 10042 * Function: sdopen 10043 * 10044 * Description: Driver's open(9e) entry point function. 10045 * 10046 * Arguments: dev_i - pointer to device number 10047 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10048 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10049 * cred_p - user credential pointer 10050 * 10051 * Return Code: EINVAL 10052 * ENXIO 10053 * EIO 10054 * EROFS 10055 * EBUSY 10056 * 10057 * Context: Kernel thread context 10058 */ 10059 /* ARGSUSED */ 10060 static int 10061 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10062 { 10063 struct sd_lun *un; 10064 int nodelay; 10065 int part; 10066 uint64_t partmask; 10067 int instance; 10068 dev_t dev; 10069 int rval = EIO; 10070 10071 /* Validate the open type */ 10072 if (otyp >= OTYPCNT) { 10073 return (EINVAL); 10074 } 10075 10076 dev = *dev_p; 10077 instance = SDUNIT(dev); 10078 mutex_enter(&sd_detach_mutex); 10079 10080 /* 10081 * Fail the open if there is no softstate for the instance, or 10082 * if another thread somewhere is trying to detach the instance. 10083 */ 10084 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10085 (un->un_detach_count != 0)) { 10086 mutex_exit(&sd_detach_mutex); 10087 /* 10088 * The probe cache only needs to be cleared when open (9e) fails 10089 * with ENXIO (4238046). 10090 */ 10091 /* 10092 * un-conditionally clearing probe cache is ok with 10093 * separate sd/ssd binaries 10094 * x86 platform can be an issue with both parallel 10095 * and fibre in 1 binary 10096 */ 10097 sd_scsi_clear_probe_cache(); 10098 return (ENXIO); 10099 } 10100 10101 /* 10102 * The un_layer_count is to prevent another thread in specfs from 10103 * trying to detach the instance, which can happen when we are 10104 * called from a higher-layer driver instead of thru specfs. 10105 * This will not be needed when DDI provides a layered driver 10106 * interface that allows specfs to know that an instance is in 10107 * use by a layered driver & should not be detached. 10108 * 10109 * Note: the semantics for layered driver opens are exactly one 10110 * close for every open. 10111 */ 10112 if (otyp == OTYP_LYR) { 10113 un->un_layer_count++; 10114 } 10115 10116 /* 10117 * Keep a count of the current # of opens in progress. This is because 10118 * some layered drivers try to call us as a regular open. This can 10119 * cause problems that we cannot prevent, however by keeping this count 10120 * we can at least keep our open and detach routines from racing against 10121 * each other under such conditions. 10122 */ 10123 un->un_opens_in_progress++; 10124 mutex_exit(&sd_detach_mutex); 10125 10126 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10127 part = SDPART(dev); 10128 partmask = 1 << part; 10129 10130 /* 10131 * We use a semaphore here in order to serialize 10132 * open and close requests on the device. 10133 */ 10134 sema_p(&un->un_semoclose); 10135 10136 mutex_enter(SD_MUTEX(un)); 10137 10138 /* 10139 * All device accesses go thru sdstrategy() where we check 10140 * on suspend status but there could be a scsi_poll command, 10141 * which bypasses sdstrategy(), so we need to check pm 10142 * status. 10143 */ 10144 10145 if (!nodelay) { 10146 while ((un->un_state == SD_STATE_SUSPENDED) || 10147 (un->un_state == SD_STATE_PM_CHANGING)) { 10148 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10149 } 10150 10151 mutex_exit(SD_MUTEX(un)); 10152 if (sd_pm_entry(un) != DDI_SUCCESS) { 10153 rval = EIO; 10154 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10155 "sdopen: sd_pm_entry failed\n"); 10156 goto open_failed_with_pm; 10157 } 10158 mutex_enter(SD_MUTEX(un)); 10159 } 10160 10161 /* check for previous exclusive open */ 10162 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10163 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10164 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10165 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10166 10167 if (un->un_exclopen & (partmask)) { 10168 goto excl_open_fail; 10169 } 10170 10171 if (flag & FEXCL) { 10172 int i; 10173 if (un->un_ocmap.lyropen[part]) { 10174 goto excl_open_fail; 10175 } 10176 for (i = 0; i < (OTYPCNT - 1); i++) { 10177 if (un->un_ocmap.regopen[i] & (partmask)) { 10178 goto excl_open_fail; 10179 } 10180 } 10181 } 10182 10183 /* 10184 * Check the write permission if this is a removable media device, 10185 * NDELAY has not been set, and writable permission is requested. 10186 * 10187 * Note: If NDELAY was set and this is write-protected media the WRITE 10188 * attempt will fail with EIO as part of the I/O processing. This is a 10189 * more permissive implementation that allows the open to succeed and 10190 * WRITE attempts to fail when appropriate. 10191 */ 10192 if (un->un_f_chk_wp_open) { 10193 if ((flag & FWRITE) && (!nodelay)) { 10194 mutex_exit(SD_MUTEX(un)); 10195 /* 10196 * Defer the check for write permission on writable 10197 * DVD drive till sdstrategy and will not fail open even 10198 * if FWRITE is set as the device can be writable 10199 * depending upon the media and the media can change 10200 * after the call to open(). 10201 */ 10202 if (un->un_f_dvdram_writable_device == FALSE) { 10203 if (ISCD(un) || sr_check_wp(dev)) { 10204 rval = EROFS; 10205 mutex_enter(SD_MUTEX(un)); 10206 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10207 "write to cd or write protected media\n"); 10208 goto open_fail; 10209 } 10210 } 10211 mutex_enter(SD_MUTEX(un)); 10212 } 10213 } 10214 10215 /* 10216 * If opening in NDELAY/NONBLOCK mode, just return. 10217 * Check if disk is ready and has a valid geometry later. 10218 */ 10219 if (!nodelay) { 10220 mutex_exit(SD_MUTEX(un)); 10221 rval = sd_ready_and_valid(un); 10222 mutex_enter(SD_MUTEX(un)); 10223 /* 10224 * Fail if device is not ready or if the number of disk 10225 * blocks is zero or negative for non CD devices. 10226 */ 10227 if ((rval != SD_READY_VALID) || 10228 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10229 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10230 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10231 "device not ready or invalid disk block value\n"); 10232 goto open_fail; 10233 } 10234 #if defined(__i386) || defined(__amd64) 10235 } else { 10236 uchar_t *cp; 10237 /* 10238 * x86 requires special nodelay handling, so that p0 is 10239 * always defined and accessible. 10240 * Invalidate geometry only if device is not already open. 10241 */ 10242 cp = &un->un_ocmap.chkd[0]; 10243 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10244 if (*cp != (uchar_t)0) { 10245 break; 10246 } 10247 cp++; 10248 } 10249 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10250 un->un_f_geometry_is_valid = FALSE; 10251 } 10252 10253 #endif 10254 } 10255 10256 if (otyp == OTYP_LYR) { 10257 un->un_ocmap.lyropen[part]++; 10258 } else { 10259 un->un_ocmap.regopen[otyp] |= partmask; 10260 } 10261 10262 /* Set up open and exclusive open flags */ 10263 if (flag & FEXCL) { 10264 un->un_exclopen |= (partmask); 10265 } 10266 10267 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10268 "open of part %d type %d\n", part, otyp); 10269 10270 mutex_exit(SD_MUTEX(un)); 10271 if (!nodelay) { 10272 sd_pm_exit(un); 10273 } 10274 10275 sema_v(&un->un_semoclose); 10276 10277 mutex_enter(&sd_detach_mutex); 10278 un->un_opens_in_progress--; 10279 mutex_exit(&sd_detach_mutex); 10280 10281 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10282 return (DDI_SUCCESS); 10283 10284 excl_open_fail: 10285 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10286 rval = EBUSY; 10287 10288 open_fail: 10289 mutex_exit(SD_MUTEX(un)); 10290 10291 /* 10292 * On a failed open we must exit the pm management. 10293 */ 10294 if (!nodelay) { 10295 sd_pm_exit(un); 10296 } 10297 open_failed_with_pm: 10298 sema_v(&un->un_semoclose); 10299 10300 mutex_enter(&sd_detach_mutex); 10301 un->un_opens_in_progress--; 10302 if (otyp == OTYP_LYR) { 10303 un->un_layer_count--; 10304 } 10305 mutex_exit(&sd_detach_mutex); 10306 10307 return (rval); 10308 } 10309 10310 10311 /* 10312 * Function: sdclose 10313 * 10314 * Description: Driver's close(9e) entry point function. 10315 * 10316 * Arguments: dev - device number 10317 * flag - file status flag, informational only 10318 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10319 * cred_p - user credential pointer 10320 * 10321 * Return Code: ENXIO 10322 * 10323 * Context: Kernel thread context 10324 */ 10325 /* ARGSUSED */ 10326 static int 10327 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10328 { 10329 struct sd_lun *un; 10330 uchar_t *cp; 10331 int part; 10332 int nodelay; 10333 int rval = 0; 10334 10335 /* Validate the open type */ 10336 if (otyp >= OTYPCNT) { 10337 return (ENXIO); 10338 } 10339 10340 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10341 return (ENXIO); 10342 } 10343 10344 part = SDPART(dev); 10345 nodelay = flag & (FNDELAY | FNONBLOCK); 10346 10347 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10348 "sdclose: close of part %d type %d\n", part, otyp); 10349 10350 /* 10351 * We use a semaphore here in order to serialize 10352 * open and close requests on the device. 10353 */ 10354 sema_p(&un->un_semoclose); 10355 10356 mutex_enter(SD_MUTEX(un)); 10357 10358 /* Don't proceed if power is being changed. */ 10359 while (un->un_state == SD_STATE_PM_CHANGING) { 10360 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10361 } 10362 10363 if (un->un_exclopen & (1 << part)) { 10364 un->un_exclopen &= ~(1 << part); 10365 } 10366 10367 /* Update the open partition map */ 10368 if (otyp == OTYP_LYR) { 10369 un->un_ocmap.lyropen[part] -= 1; 10370 } else { 10371 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10372 } 10373 10374 cp = &un->un_ocmap.chkd[0]; 10375 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10376 if (*cp != NULL) { 10377 break; 10378 } 10379 cp++; 10380 } 10381 10382 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10383 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10384 10385 /* 10386 * We avoid persistance upon the last close, and set 10387 * the throttle back to the maximum. 10388 */ 10389 un->un_throttle = un->un_saved_throttle; 10390 10391 if (un->un_state == SD_STATE_OFFLINE) { 10392 if (un->un_f_is_fibre == FALSE) { 10393 scsi_log(SD_DEVINFO(un), sd_label, 10394 CE_WARN, "offline\n"); 10395 } 10396 un->un_f_geometry_is_valid = FALSE; 10397 10398 } else { 10399 /* 10400 * Flush any outstanding writes in NVRAM cache. 10401 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10402 * cmd, it may not work for non-Pluto devices. 10403 * SYNCHRONIZE CACHE is not required for removables, 10404 * except DVD-RAM drives. 10405 * 10406 * Also note: because SYNCHRONIZE CACHE is currently 10407 * the only command issued here that requires the 10408 * drive be powered up, only do the power up before 10409 * sending the Sync Cache command. If additional 10410 * commands are added which require a powered up 10411 * drive, the following sequence may have to change. 10412 * 10413 * And finally, note that parallel SCSI on SPARC 10414 * only issues a Sync Cache to DVD-RAM, a newly 10415 * supported device. 10416 */ 10417 #if defined(__i386) || defined(__amd64) 10418 if (un->un_f_sync_cache_supported || 10419 un->un_f_dvdram_writable_device == TRUE) { 10420 #else 10421 if (un->un_f_dvdram_writable_device == TRUE) { 10422 #endif 10423 mutex_exit(SD_MUTEX(un)); 10424 if (sd_pm_entry(un) == DDI_SUCCESS) { 10425 rval = 10426 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10427 NULL); 10428 /* ignore error if not supported */ 10429 if (rval == ENOTSUP) { 10430 rval = 0; 10431 } else if (rval != 0) { 10432 rval = EIO; 10433 } 10434 sd_pm_exit(un); 10435 } else { 10436 rval = EIO; 10437 } 10438 mutex_enter(SD_MUTEX(un)); 10439 } 10440 10441 /* 10442 * For devices which supports DOOR_LOCK, send an ALLOW 10443 * MEDIA REMOVAL command, but don't get upset if it 10444 * fails. We need to raise the power of the drive before 10445 * we can call sd_send_scsi_DOORLOCK() 10446 */ 10447 if (un->un_f_doorlock_supported) { 10448 mutex_exit(SD_MUTEX(un)); 10449 if (sd_pm_entry(un) == DDI_SUCCESS) { 10450 rval = sd_send_scsi_DOORLOCK(un, 10451 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10452 10453 sd_pm_exit(un); 10454 if (ISCD(un) && (rval != 0) && 10455 (nodelay != 0)) { 10456 rval = ENXIO; 10457 } 10458 } else { 10459 rval = EIO; 10460 } 10461 mutex_enter(SD_MUTEX(un)); 10462 } 10463 10464 /* 10465 * If a device has removable media, invalidate all 10466 * parameters related to media, such as geometry, 10467 * blocksize, and blockcount. 10468 */ 10469 if (un->un_f_has_removable_media) { 10470 sr_ejected(un); 10471 } 10472 10473 } 10474 } 10475 10476 /* 10477 * Destroy the cache (if it exists) which was 10478 * allocated for the write maps since this is 10479 * the last close for this media. 10480 */ 10481 if (un->un_wm_cache) { 10482 /* 10483 * Check if there are pending commands. 10484 * and if there are give a warning and 10485 * do not destroy the cache. 10486 */ 10487 if (un->un_ncmds_in_driver > 0) { 10488 scsi_log(SD_DEVINFO(un), 10489 sd_label, CE_WARN, 10490 "Unable to clean up memory " 10491 "because of pending I/O\n"); 10492 } else { 10493 kmem_cache_destroy( 10494 un->un_wm_cache); 10495 un->un_wm_cache = NULL; 10496 } 10497 } 10498 10499 mutex_exit(SD_MUTEX(un)); 10500 sema_v(&un->un_semoclose); 10501 10502 if (otyp == OTYP_LYR) { 10503 mutex_enter(&sd_detach_mutex); 10504 /* 10505 * The detach routine may run when the layer count 10506 * drops to zero. 10507 */ 10508 un->un_layer_count--; 10509 mutex_exit(&sd_detach_mutex); 10510 } 10511 10512 return (rval); 10513 } 10514 10515 10516 /* 10517 * Function: sd_ready_and_valid 10518 * 10519 * Description: Test if device is ready and has a valid geometry. 10520 * 10521 * Arguments: dev - device number 10522 * un - driver soft state (unit) structure 10523 * 10524 * Return Code: SD_READY_VALID ready and valid label 10525 * SD_READY_NOT_VALID ready, geom ops never applicable 10526 * SD_NOT_READY_VALID not ready, no label 10527 * 10528 * Context: Never called at interrupt context. 10529 */ 10530 10531 static int 10532 sd_ready_and_valid(struct sd_lun *un) 10533 { 10534 struct sd_errstats *stp; 10535 uint64_t capacity; 10536 uint_t lbasize; 10537 int rval = SD_READY_VALID; 10538 char name_str[48]; 10539 10540 ASSERT(un != NULL); 10541 ASSERT(!mutex_owned(SD_MUTEX(un))); 10542 10543 mutex_enter(SD_MUTEX(un)); 10544 /* 10545 * If a device has removable media, we must check if media is 10546 * ready when checking if this device is ready and valid. 10547 */ 10548 if (un->un_f_has_removable_media) { 10549 mutex_exit(SD_MUTEX(un)); 10550 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10551 rval = SD_NOT_READY_VALID; 10552 mutex_enter(SD_MUTEX(un)); 10553 goto done; 10554 } 10555 10556 mutex_enter(SD_MUTEX(un)); 10557 if ((un->un_f_geometry_is_valid == FALSE) || 10558 (un->un_f_blockcount_is_valid == FALSE) || 10559 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10560 10561 /* capacity has to be read every open. */ 10562 mutex_exit(SD_MUTEX(un)); 10563 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10564 &lbasize, SD_PATH_DIRECT) != 0) { 10565 mutex_enter(SD_MUTEX(un)); 10566 un->un_f_geometry_is_valid = FALSE; 10567 rval = SD_NOT_READY_VALID; 10568 goto done; 10569 } else { 10570 mutex_enter(SD_MUTEX(un)); 10571 sd_update_block_info(un, lbasize, capacity); 10572 } 10573 } 10574 10575 /* 10576 * Check if the media in the device is writable or not. 10577 */ 10578 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10579 sd_check_for_writable_cd(un); 10580 } 10581 10582 } else { 10583 /* 10584 * Do a test unit ready to clear any unit attention from non-cd 10585 * devices. 10586 */ 10587 mutex_exit(SD_MUTEX(un)); 10588 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10589 mutex_enter(SD_MUTEX(un)); 10590 } 10591 10592 10593 /* 10594 * If this is a non 512 block device, allocate space for 10595 * the wmap cache. This is being done here since every time 10596 * a media is changed this routine will be called and the 10597 * block size is a function of media rather than device. 10598 */ 10599 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 10600 if (!(un->un_wm_cache)) { 10601 (void) snprintf(name_str, sizeof (name_str), 10602 "%s%d_cache", 10603 ddi_driver_name(SD_DEVINFO(un)), 10604 ddi_get_instance(SD_DEVINFO(un))); 10605 un->un_wm_cache = kmem_cache_create( 10606 name_str, sizeof (struct sd_w_map), 10607 8, sd_wm_cache_constructor, 10608 sd_wm_cache_destructor, NULL, 10609 (void *)un, NULL, 0); 10610 if (!(un->un_wm_cache)) { 10611 rval = ENOMEM; 10612 goto done; 10613 } 10614 } 10615 } 10616 10617 if (un->un_state == SD_STATE_NORMAL) { 10618 /* 10619 * If the target is not yet ready here (defined by a TUR 10620 * failure), invalidate the geometry and print an 'offline' 10621 * message. This is a legacy message, as the state of the 10622 * target is not actually changed to SD_STATE_OFFLINE. 10623 * 10624 * If the TUR fails for EACCES (Reservation Conflict), it 10625 * means there actually is nothing wrong with the target that 10626 * would require invalidating the geometry, so continue in 10627 * that case as if the TUR was successful. 10628 */ 10629 int err; 10630 10631 mutex_exit(SD_MUTEX(un)); 10632 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10633 mutex_enter(SD_MUTEX(un)); 10634 10635 if ((err != 0) && (err != EACCES)) { 10636 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10637 "offline\n"); 10638 un->un_f_geometry_is_valid = FALSE; 10639 rval = SD_NOT_READY_VALID; 10640 goto done; 10641 } 10642 } 10643 10644 if (un->un_f_format_in_progress == FALSE) { 10645 /* 10646 * Note: sd_validate_geometry may return TRUE, but that does 10647 * not necessarily mean un_f_geometry_is_valid == TRUE! 10648 */ 10649 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10650 if (rval == ENOTSUP) { 10651 if (un->un_f_geometry_is_valid == TRUE) 10652 rval = 0; 10653 else { 10654 rval = SD_READY_NOT_VALID; 10655 goto done; 10656 } 10657 } 10658 if (rval != 0) { 10659 /* 10660 * We don't check the validity of geometry for 10661 * CDROMs. Also we assume we have a good label 10662 * even if sd_validate_geometry returned ENOMEM. 10663 */ 10664 if (!ISCD(un) && rval != ENOMEM) { 10665 rval = SD_NOT_READY_VALID; 10666 goto done; 10667 } 10668 } 10669 } 10670 10671 #ifdef DOESNTWORK /* on eliteII, see 1118607 */ 10672 /* 10673 * check to see if this disk is write protected, if it is and we have 10674 * not set read-only, then fail 10675 */ 10676 if ((flag & FWRITE) && (sr_check_wp(dev))) { 10677 New_state(un, SD_STATE_CLOSED); 10678 goto done; 10679 } 10680 #endif 10681 10682 /* 10683 * If this device supports DOOR_LOCK command, try and send 10684 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10685 * if it fails. For a CD, however, it is an error 10686 */ 10687 if (un->un_f_doorlock_supported) { 10688 mutex_exit(SD_MUTEX(un)); 10689 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10690 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10691 rval = SD_NOT_READY_VALID; 10692 mutex_enter(SD_MUTEX(un)); 10693 goto done; 10694 } 10695 mutex_enter(SD_MUTEX(un)); 10696 } 10697 10698 /* The state has changed, inform the media watch routines */ 10699 un->un_mediastate = DKIO_INSERTED; 10700 cv_broadcast(&un->un_state_cv); 10701 rval = SD_READY_VALID; 10702 10703 done: 10704 10705 /* 10706 * Initialize the capacity kstat value, if no media previously 10707 * (capacity kstat is 0) and a media has been inserted 10708 * (un_blockcount > 0). 10709 */ 10710 if (un->un_errstats != NULL) { 10711 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10712 if ((stp->sd_capacity.value.ui64 == 0) && 10713 (un->un_f_blockcount_is_valid == TRUE)) { 10714 stp->sd_capacity.value.ui64 = 10715 (uint64_t)((uint64_t)un->un_blockcount * 10716 un->un_sys_blocksize); 10717 } 10718 } 10719 10720 mutex_exit(SD_MUTEX(un)); 10721 return (rval); 10722 } 10723 10724 10725 /* 10726 * Function: sdmin 10727 * 10728 * Description: Routine to limit the size of a data transfer. Used in 10729 * conjunction with physio(9F). 10730 * 10731 * Arguments: bp - pointer to the indicated buf(9S) struct. 10732 * 10733 * Context: Kernel thread context. 10734 */ 10735 10736 static void 10737 sdmin(struct buf *bp) 10738 { 10739 struct sd_lun *un; 10740 int instance; 10741 10742 instance = SDUNIT(bp->b_edev); 10743 10744 un = ddi_get_soft_state(sd_state, instance); 10745 ASSERT(un != NULL); 10746 10747 if (bp->b_bcount > un->un_max_xfer_size) { 10748 bp->b_bcount = un->un_max_xfer_size; 10749 } 10750 } 10751 10752 10753 /* 10754 * Function: sdread 10755 * 10756 * Description: Driver's read(9e) entry point function. 10757 * 10758 * Arguments: dev - device number 10759 * uio - structure pointer describing where data is to be stored 10760 * in user's space 10761 * cred_p - user credential pointer 10762 * 10763 * Return Code: ENXIO 10764 * EIO 10765 * EINVAL 10766 * value returned by physio 10767 * 10768 * Context: Kernel thread context. 10769 */ 10770 /* ARGSUSED */ 10771 static int 10772 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10773 { 10774 struct sd_lun *un = NULL; 10775 int secmask; 10776 int err; 10777 10778 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10779 return (ENXIO); 10780 } 10781 10782 ASSERT(!mutex_owned(SD_MUTEX(un))); 10783 10784 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10785 mutex_enter(SD_MUTEX(un)); 10786 /* 10787 * Because the call to sd_ready_and_valid will issue I/O we 10788 * must wait here if either the device is suspended or 10789 * if it's power level is changing. 10790 */ 10791 while ((un->un_state == SD_STATE_SUSPENDED) || 10792 (un->un_state == SD_STATE_PM_CHANGING)) { 10793 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10794 } 10795 un->un_ncmds_in_driver++; 10796 mutex_exit(SD_MUTEX(un)); 10797 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10798 mutex_enter(SD_MUTEX(un)); 10799 un->un_ncmds_in_driver--; 10800 ASSERT(un->un_ncmds_in_driver >= 0); 10801 mutex_exit(SD_MUTEX(un)); 10802 return (EIO); 10803 } 10804 mutex_enter(SD_MUTEX(un)); 10805 un->un_ncmds_in_driver--; 10806 ASSERT(un->un_ncmds_in_driver >= 0); 10807 mutex_exit(SD_MUTEX(un)); 10808 } 10809 10810 /* 10811 * Read requests are restricted to multiples of the system block size. 10812 */ 10813 secmask = un->un_sys_blocksize - 1; 10814 10815 if (uio->uio_loffset & ((offset_t)(secmask))) { 10816 SD_ERROR(SD_LOG_READ_WRITE, un, 10817 "sdread: file offset not modulo %d\n", 10818 un->un_sys_blocksize); 10819 err = EINVAL; 10820 } else if (uio->uio_iov->iov_len & (secmask)) { 10821 SD_ERROR(SD_LOG_READ_WRITE, un, 10822 "sdread: transfer length not modulo %d\n", 10823 un->un_sys_blocksize); 10824 err = EINVAL; 10825 } else { 10826 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10827 } 10828 return (err); 10829 } 10830 10831 10832 /* 10833 * Function: sdwrite 10834 * 10835 * Description: Driver's write(9e) entry point function. 10836 * 10837 * Arguments: dev - device number 10838 * uio - structure pointer describing where data is stored in 10839 * user's space 10840 * cred_p - user credential pointer 10841 * 10842 * Return Code: ENXIO 10843 * EIO 10844 * EINVAL 10845 * value returned by physio 10846 * 10847 * Context: Kernel thread context. 10848 */ 10849 /* ARGSUSED */ 10850 static int 10851 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10852 { 10853 struct sd_lun *un = NULL; 10854 int secmask; 10855 int err; 10856 10857 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10858 return (ENXIO); 10859 } 10860 10861 ASSERT(!mutex_owned(SD_MUTEX(un))); 10862 10863 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10864 mutex_enter(SD_MUTEX(un)); 10865 /* 10866 * Because the call to sd_ready_and_valid will issue I/O we 10867 * must wait here if either the device is suspended or 10868 * if it's power level is changing. 10869 */ 10870 while ((un->un_state == SD_STATE_SUSPENDED) || 10871 (un->un_state == SD_STATE_PM_CHANGING)) { 10872 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10873 } 10874 un->un_ncmds_in_driver++; 10875 mutex_exit(SD_MUTEX(un)); 10876 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10877 mutex_enter(SD_MUTEX(un)); 10878 un->un_ncmds_in_driver--; 10879 ASSERT(un->un_ncmds_in_driver >= 0); 10880 mutex_exit(SD_MUTEX(un)); 10881 return (EIO); 10882 } 10883 mutex_enter(SD_MUTEX(un)); 10884 un->un_ncmds_in_driver--; 10885 ASSERT(un->un_ncmds_in_driver >= 0); 10886 mutex_exit(SD_MUTEX(un)); 10887 } 10888 10889 /* 10890 * Write requests are restricted to multiples of the system block size. 10891 */ 10892 secmask = un->un_sys_blocksize - 1; 10893 10894 if (uio->uio_loffset & ((offset_t)(secmask))) { 10895 SD_ERROR(SD_LOG_READ_WRITE, un, 10896 "sdwrite: file offset not modulo %d\n", 10897 un->un_sys_blocksize); 10898 err = EINVAL; 10899 } else if (uio->uio_iov->iov_len & (secmask)) { 10900 SD_ERROR(SD_LOG_READ_WRITE, un, 10901 "sdwrite: transfer length not modulo %d\n", 10902 un->un_sys_blocksize); 10903 err = EINVAL; 10904 } else { 10905 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10906 } 10907 return (err); 10908 } 10909 10910 10911 /* 10912 * Function: sdaread 10913 * 10914 * Description: Driver's aread(9e) entry point function. 10915 * 10916 * Arguments: dev - device number 10917 * aio - structure pointer describing where data is to be stored 10918 * cred_p - user credential pointer 10919 * 10920 * Return Code: ENXIO 10921 * EIO 10922 * EINVAL 10923 * value returned by aphysio 10924 * 10925 * Context: Kernel thread context. 10926 */ 10927 /* ARGSUSED */ 10928 static int 10929 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10930 { 10931 struct sd_lun *un = NULL; 10932 struct uio *uio = aio->aio_uio; 10933 int secmask; 10934 int err; 10935 10936 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10937 return (ENXIO); 10938 } 10939 10940 ASSERT(!mutex_owned(SD_MUTEX(un))); 10941 10942 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10943 mutex_enter(SD_MUTEX(un)); 10944 /* 10945 * Because the call to sd_ready_and_valid will issue I/O we 10946 * must wait here if either the device is suspended or 10947 * if it's power level is changing. 10948 */ 10949 while ((un->un_state == SD_STATE_SUSPENDED) || 10950 (un->un_state == SD_STATE_PM_CHANGING)) { 10951 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10952 } 10953 un->un_ncmds_in_driver++; 10954 mutex_exit(SD_MUTEX(un)); 10955 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10956 mutex_enter(SD_MUTEX(un)); 10957 un->un_ncmds_in_driver--; 10958 ASSERT(un->un_ncmds_in_driver >= 0); 10959 mutex_exit(SD_MUTEX(un)); 10960 return (EIO); 10961 } 10962 mutex_enter(SD_MUTEX(un)); 10963 un->un_ncmds_in_driver--; 10964 ASSERT(un->un_ncmds_in_driver >= 0); 10965 mutex_exit(SD_MUTEX(un)); 10966 } 10967 10968 /* 10969 * Read requests are restricted to multiples of the system block size. 10970 */ 10971 secmask = un->un_sys_blocksize - 1; 10972 10973 if (uio->uio_loffset & ((offset_t)(secmask))) { 10974 SD_ERROR(SD_LOG_READ_WRITE, un, 10975 "sdaread: file offset not modulo %d\n", 10976 un->un_sys_blocksize); 10977 err = EINVAL; 10978 } else if (uio->uio_iov->iov_len & (secmask)) { 10979 SD_ERROR(SD_LOG_READ_WRITE, un, 10980 "sdaread: transfer length not modulo %d\n", 10981 un->un_sys_blocksize); 10982 err = EINVAL; 10983 } else { 10984 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 10985 } 10986 return (err); 10987 } 10988 10989 10990 /* 10991 * Function: sdawrite 10992 * 10993 * Description: Driver's awrite(9e) entry point function. 10994 * 10995 * Arguments: dev - device number 10996 * aio - structure pointer describing where data is stored 10997 * cred_p - user credential pointer 10998 * 10999 * Return Code: ENXIO 11000 * EIO 11001 * EINVAL 11002 * value returned by aphysio 11003 * 11004 * Context: Kernel thread context. 11005 */ 11006 /* ARGSUSED */ 11007 static int 11008 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11009 { 11010 struct sd_lun *un = NULL; 11011 struct uio *uio = aio->aio_uio; 11012 int secmask; 11013 int err; 11014 11015 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11016 return (ENXIO); 11017 } 11018 11019 ASSERT(!mutex_owned(SD_MUTEX(un))); 11020 11021 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11022 mutex_enter(SD_MUTEX(un)); 11023 /* 11024 * Because the call to sd_ready_and_valid will issue I/O we 11025 * must wait here if either the device is suspended or 11026 * if it's power level is changing. 11027 */ 11028 while ((un->un_state == SD_STATE_SUSPENDED) || 11029 (un->un_state == SD_STATE_PM_CHANGING)) { 11030 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11031 } 11032 un->un_ncmds_in_driver++; 11033 mutex_exit(SD_MUTEX(un)); 11034 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11035 mutex_enter(SD_MUTEX(un)); 11036 un->un_ncmds_in_driver--; 11037 ASSERT(un->un_ncmds_in_driver >= 0); 11038 mutex_exit(SD_MUTEX(un)); 11039 return (EIO); 11040 } 11041 mutex_enter(SD_MUTEX(un)); 11042 un->un_ncmds_in_driver--; 11043 ASSERT(un->un_ncmds_in_driver >= 0); 11044 mutex_exit(SD_MUTEX(un)); 11045 } 11046 11047 /* 11048 * Write requests are restricted to multiples of the system block size. 11049 */ 11050 secmask = un->un_sys_blocksize - 1; 11051 11052 if (uio->uio_loffset & ((offset_t)(secmask))) { 11053 SD_ERROR(SD_LOG_READ_WRITE, un, 11054 "sdawrite: file offset not modulo %d\n", 11055 un->un_sys_blocksize); 11056 err = EINVAL; 11057 } else if (uio->uio_iov->iov_len & (secmask)) { 11058 SD_ERROR(SD_LOG_READ_WRITE, un, 11059 "sdawrite: transfer length not modulo %d\n", 11060 un->un_sys_blocksize); 11061 err = EINVAL; 11062 } else { 11063 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11064 } 11065 return (err); 11066 } 11067 11068 11069 11070 11071 11072 /* 11073 * Driver IO processing follows the following sequence: 11074 * 11075 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11076 * | | ^ 11077 * v v | 11078 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11079 * | | | | 11080 * v | | | 11081 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11082 * | | ^ ^ 11083 * v v | | 11084 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11085 * | | | | 11086 * +---+ | +------------+ +-------+ 11087 * | | | | 11088 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11089 * | v | | 11090 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11091 * | | ^ | 11092 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11093 * | v | | 11094 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11095 * | | ^ | 11096 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11097 * | v | | 11098 * | sd_checksum_iostart() sd_checksum_iodone() | 11099 * | | ^ | 11100 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11101 * | v | | 11102 * | sd_pm_iostart() sd_pm_iodone() | 11103 * | | ^ | 11104 * | | | | 11105 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11106 * | ^ 11107 * v | 11108 * sd_core_iostart() | 11109 * | | 11110 * | +------>(*destroypkt)() 11111 * +-> sd_start_cmds() <-+ | | 11112 * | | | v 11113 * | | | scsi_destroy_pkt(9F) 11114 * | | | 11115 * +->(*initpkt)() +- sdintr() 11116 * | | | | 11117 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11118 * | +-> scsi_setup_cdb(9F) | 11119 * | | 11120 * +--> scsi_transport(9F) | 11121 * | | 11122 * +----> SCSA ---->+ 11123 * 11124 * 11125 * This code is based upon the following presumtions: 11126 * 11127 * - iostart and iodone functions operate on buf(9S) structures. These 11128 * functions perform the necessary operations on the buf(9S) and pass 11129 * them along to the next function in the chain by using the macros 11130 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11131 * (for iodone side functions). 11132 * 11133 * - The iostart side functions may sleep. The iodone side functions 11134 * are called under interrupt context and may NOT sleep. Therefore 11135 * iodone side functions also may not call iostart side functions. 11136 * (NOTE: iostart side functions should NOT sleep for memory, as 11137 * this could result in deadlock.) 11138 * 11139 * - An iostart side function may call its corresponding iodone side 11140 * function directly (if necessary). 11141 * 11142 * - In the event of an error, an iostart side function can return a buf(9S) 11143 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11144 * b_error in the usual way of course). 11145 * 11146 * - The taskq mechanism may be used by the iodone side functions to dispatch 11147 * requests to the iostart side functions. The iostart side functions in 11148 * this case would be called under the context of a taskq thread, so it's 11149 * OK for them to block/sleep/spin in this case. 11150 * 11151 * - iostart side functions may allocate "shadow" buf(9S) structs and 11152 * pass them along to the next function in the chain. The corresponding 11153 * iodone side functions must coalesce the "shadow" bufs and return 11154 * the "original" buf to the next higher layer. 11155 * 11156 * - The b_private field of the buf(9S) struct holds a pointer to 11157 * an sd_xbuf struct, which contains information needed to 11158 * construct the scsi_pkt for the command. 11159 * 11160 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11161 * layer must acquire & release the SD_MUTEX(un) as needed. 11162 */ 11163 11164 11165 /* 11166 * Create taskq for all targets in the system. This is created at 11167 * _init(9E) and destroyed at _fini(9E). 11168 * 11169 * Note: here we set the minalloc to a reasonably high number to ensure that 11170 * we will have an adequate supply of task entries available at interrupt time. 11171 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11172 * sd_create_taskq(). Since we do not want to sleep for allocations at 11173 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11174 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11175 * requests any one instant in time. 11176 */ 11177 #define SD_TASKQ_NUMTHREADS 8 11178 #define SD_TASKQ_MINALLOC 256 11179 #define SD_TASKQ_MAXALLOC 256 11180 11181 static taskq_t *sd_tq = NULL; 11182 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11183 11184 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11185 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11186 11187 /* 11188 * The following task queue is being created for the write part of 11189 * read-modify-write of non-512 block size devices. 11190 * Limit the number of threads to 1 for now. This number has been choosen 11191 * considering the fact that it applies only to dvd ram drives/MO drives 11192 * currently. Performance for which is not main criteria at this stage. 11193 * Note: It needs to be explored if we can use a single taskq in future 11194 */ 11195 #define SD_WMR_TASKQ_NUMTHREADS 1 11196 static taskq_t *sd_wmr_tq = NULL; 11197 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11198 11199 /* 11200 * Function: sd_taskq_create 11201 * 11202 * Description: Create taskq thread(s) and preallocate task entries 11203 * 11204 * Return Code: Returns a pointer to the allocated taskq_t. 11205 * 11206 * Context: Can sleep. Requires blockable context. 11207 * 11208 * Notes: - The taskq() facility currently is NOT part of the DDI. 11209 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11210 * - taskq_create() will block for memory, also it will panic 11211 * if it cannot create the requested number of threads. 11212 * - Currently taskq_create() creates threads that cannot be 11213 * swapped. 11214 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11215 * supply of taskq entries at interrupt time (ie, so that we 11216 * do not have to sleep for memory) 11217 */ 11218 11219 static void 11220 sd_taskq_create(void) 11221 { 11222 char taskq_name[TASKQ_NAMELEN]; 11223 11224 ASSERT(sd_tq == NULL); 11225 ASSERT(sd_wmr_tq == NULL); 11226 11227 (void) snprintf(taskq_name, sizeof (taskq_name), 11228 "%s_drv_taskq", sd_label); 11229 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11230 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11231 TASKQ_PREPOPULATE)); 11232 11233 (void) snprintf(taskq_name, sizeof (taskq_name), 11234 "%s_rmw_taskq", sd_label); 11235 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11236 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11237 TASKQ_PREPOPULATE)); 11238 } 11239 11240 11241 /* 11242 * Function: sd_taskq_delete 11243 * 11244 * Description: Complementary cleanup routine for sd_taskq_create(). 11245 * 11246 * Context: Kernel thread context. 11247 */ 11248 11249 static void 11250 sd_taskq_delete(void) 11251 { 11252 ASSERT(sd_tq != NULL); 11253 ASSERT(sd_wmr_tq != NULL); 11254 taskq_destroy(sd_tq); 11255 taskq_destroy(sd_wmr_tq); 11256 sd_tq = NULL; 11257 sd_wmr_tq = NULL; 11258 } 11259 11260 11261 /* 11262 * Function: sdstrategy 11263 * 11264 * Description: Driver's strategy (9E) entry point function. 11265 * 11266 * Arguments: bp - pointer to buf(9S) 11267 * 11268 * Return Code: Always returns zero 11269 * 11270 * Context: Kernel thread context. 11271 */ 11272 11273 static int 11274 sdstrategy(struct buf *bp) 11275 { 11276 struct sd_lun *un; 11277 11278 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11279 if (un == NULL) { 11280 bioerror(bp, EIO); 11281 bp->b_resid = bp->b_bcount; 11282 biodone(bp); 11283 return (0); 11284 } 11285 /* As was done in the past, fail new cmds. if state is dumping. */ 11286 if (un->un_state == SD_STATE_DUMPING) { 11287 bioerror(bp, ENXIO); 11288 bp->b_resid = bp->b_bcount; 11289 biodone(bp); 11290 return (0); 11291 } 11292 11293 ASSERT(!mutex_owned(SD_MUTEX(un))); 11294 11295 /* 11296 * Commands may sneak in while we released the mutex in 11297 * DDI_SUSPEND, we should block new commands. However, old 11298 * commands that are still in the driver at this point should 11299 * still be allowed to drain. 11300 */ 11301 mutex_enter(SD_MUTEX(un)); 11302 /* 11303 * Must wait here if either the device is suspended or 11304 * if it's power level is changing. 11305 */ 11306 while ((un->un_state == SD_STATE_SUSPENDED) || 11307 (un->un_state == SD_STATE_PM_CHANGING)) { 11308 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11309 } 11310 11311 un->un_ncmds_in_driver++; 11312 11313 /* 11314 * atapi: Since we are running the CD for now in PIO mode we need to 11315 * call bp_mapin here to avoid bp_mapin called interrupt context under 11316 * the HBA's init_pkt routine. 11317 */ 11318 if (un->un_f_cfg_is_atapi == TRUE) { 11319 mutex_exit(SD_MUTEX(un)); 11320 bp_mapin(bp); 11321 mutex_enter(SD_MUTEX(un)); 11322 } 11323 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11324 un->un_ncmds_in_driver); 11325 11326 mutex_exit(SD_MUTEX(un)); 11327 11328 /* 11329 * This will (eventually) allocate the sd_xbuf area and 11330 * call sd_xbuf_strategy(). We just want to return the 11331 * result of ddi_xbuf_qstrategy so that we have an opt- 11332 * imized tail call which saves us a stack frame. 11333 */ 11334 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11335 } 11336 11337 11338 /* 11339 * Function: sd_xbuf_strategy 11340 * 11341 * Description: Function for initiating IO operations via the 11342 * ddi_xbuf_qstrategy() mechanism. 11343 * 11344 * Context: Kernel thread context. 11345 */ 11346 11347 static void 11348 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11349 { 11350 struct sd_lun *un = arg; 11351 11352 ASSERT(bp != NULL); 11353 ASSERT(xp != NULL); 11354 ASSERT(un != NULL); 11355 ASSERT(!mutex_owned(SD_MUTEX(un))); 11356 11357 /* 11358 * Initialize the fields in the xbuf and save a pointer to the 11359 * xbuf in bp->b_private. 11360 */ 11361 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11362 11363 /* Send the buf down the iostart chain */ 11364 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11365 } 11366 11367 11368 /* 11369 * Function: sd_xbuf_init 11370 * 11371 * Description: Prepare the given sd_xbuf struct for use. 11372 * 11373 * Arguments: un - ptr to softstate 11374 * bp - ptr to associated buf(9S) 11375 * xp - ptr to associated sd_xbuf 11376 * chain_type - IO chain type to use: 11377 * SD_CHAIN_NULL 11378 * SD_CHAIN_BUFIO 11379 * SD_CHAIN_USCSI 11380 * SD_CHAIN_DIRECT 11381 * SD_CHAIN_DIRECT_PRIORITY 11382 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11383 * initialization; may be NULL if none. 11384 * 11385 * Context: Kernel thread context 11386 */ 11387 11388 static void 11389 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11390 uchar_t chain_type, void *pktinfop) 11391 { 11392 int index; 11393 11394 ASSERT(un != NULL); 11395 ASSERT(bp != NULL); 11396 ASSERT(xp != NULL); 11397 11398 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11399 bp, chain_type); 11400 11401 xp->xb_un = un; 11402 xp->xb_pktp = NULL; 11403 xp->xb_pktinfo = pktinfop; 11404 xp->xb_private = bp->b_private; 11405 xp->xb_blkno = (daddr_t)bp->b_blkno; 11406 11407 /* 11408 * Set up the iostart and iodone chain indexes in the xbuf, based 11409 * upon the specified chain type to use. 11410 */ 11411 switch (chain_type) { 11412 case SD_CHAIN_NULL: 11413 /* 11414 * Fall thru to just use the values for the buf type, even 11415 * tho for the NULL chain these values will never be used. 11416 */ 11417 /* FALLTHRU */ 11418 case SD_CHAIN_BUFIO: 11419 index = un->un_buf_chain_type; 11420 break; 11421 case SD_CHAIN_USCSI: 11422 index = un->un_uscsi_chain_type; 11423 break; 11424 case SD_CHAIN_DIRECT: 11425 index = un->un_direct_chain_type; 11426 break; 11427 case SD_CHAIN_DIRECT_PRIORITY: 11428 index = un->un_priority_chain_type; 11429 break; 11430 default: 11431 /* We're really broken if we ever get here... */ 11432 panic("sd_xbuf_init: illegal chain type!"); 11433 /*NOTREACHED*/ 11434 } 11435 11436 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11437 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11438 11439 /* 11440 * It might be a bit easier to simply bzero the entire xbuf above, 11441 * but it turns out that since we init a fair number of members anyway, 11442 * we save a fair number cycles by doing explicit assignment of zero. 11443 */ 11444 xp->xb_pkt_flags = 0; 11445 xp->xb_dma_resid = 0; 11446 xp->xb_retry_count = 0; 11447 xp->xb_victim_retry_count = 0; 11448 xp->xb_ua_retry_count = 0; 11449 xp->xb_sense_bp = NULL; 11450 xp->xb_sense_status = 0; 11451 xp->xb_sense_state = 0; 11452 xp->xb_sense_resid = 0; 11453 11454 bp->b_private = xp; 11455 bp->b_flags &= ~(B_DONE | B_ERROR); 11456 bp->b_resid = 0; 11457 bp->av_forw = NULL; 11458 bp->av_back = NULL; 11459 bioerror(bp, 0); 11460 11461 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11462 } 11463 11464 11465 /* 11466 * Function: sd_uscsi_strategy 11467 * 11468 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11469 * 11470 * Arguments: bp - buf struct ptr 11471 * 11472 * Return Code: Always returns 0 11473 * 11474 * Context: Kernel thread context 11475 */ 11476 11477 static int 11478 sd_uscsi_strategy(struct buf *bp) 11479 { 11480 struct sd_lun *un; 11481 struct sd_uscsi_info *uip; 11482 struct sd_xbuf *xp; 11483 uchar_t chain_type; 11484 11485 ASSERT(bp != NULL); 11486 11487 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11488 if (un == NULL) { 11489 bioerror(bp, EIO); 11490 bp->b_resid = bp->b_bcount; 11491 biodone(bp); 11492 return (0); 11493 } 11494 11495 ASSERT(!mutex_owned(SD_MUTEX(un))); 11496 11497 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11498 11499 mutex_enter(SD_MUTEX(un)); 11500 /* 11501 * atapi: Since we are running the CD for now in PIO mode we need to 11502 * call bp_mapin here to avoid bp_mapin called interrupt context under 11503 * the HBA's init_pkt routine. 11504 */ 11505 if (un->un_f_cfg_is_atapi == TRUE) { 11506 mutex_exit(SD_MUTEX(un)); 11507 bp_mapin(bp); 11508 mutex_enter(SD_MUTEX(un)); 11509 } 11510 un->un_ncmds_in_driver++; 11511 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11512 un->un_ncmds_in_driver); 11513 mutex_exit(SD_MUTEX(un)); 11514 11515 /* 11516 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11517 */ 11518 ASSERT(bp->b_private != NULL); 11519 uip = (struct sd_uscsi_info *)bp->b_private; 11520 11521 switch (uip->ui_flags) { 11522 case SD_PATH_DIRECT: 11523 chain_type = SD_CHAIN_DIRECT; 11524 break; 11525 case SD_PATH_DIRECT_PRIORITY: 11526 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11527 break; 11528 default: 11529 chain_type = SD_CHAIN_USCSI; 11530 break; 11531 } 11532 11533 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11534 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11535 11536 /* Use the index obtained within xbuf_init */ 11537 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11538 11539 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11540 11541 return (0); 11542 } 11543 11544 11545 /* 11546 * These routines perform raw i/o operations. 11547 */ 11548 /*ARGSUSED*/ 11549 static void 11550 sduscsimin(struct buf *bp) 11551 { 11552 /* 11553 * do not break up because the CDB count would then 11554 * be incorrect and data underruns would result (incomplete 11555 * read/writes which would be retried and then failed, see 11556 * sdintr(). 11557 */ 11558 } 11559 11560 11561 11562 /* 11563 * Function: sd_send_scsi_cmd 11564 * 11565 * Description: Runs a USCSI command for user (when called thru sdioctl), 11566 * or for the driver 11567 * 11568 * Arguments: dev - the dev_t for the device 11569 * incmd - ptr to a valid uscsi_cmd struct 11570 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11571 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11572 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11573 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11574 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11575 * to use the USCSI "direct" chain and bypass the normal 11576 * command waitq. 11577 * 11578 * Return Code: 0 - successful completion of the given command 11579 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11580 * ENXIO - soft state not found for specified dev 11581 * EINVAL 11582 * EFAULT - copyin/copyout error 11583 * return code of biowait(9F) or physio(9F): 11584 * EIO - IO error, caller may check incmd->uscsi_status 11585 * ENXIO 11586 * EACCES - reservation conflict 11587 * 11588 * Context: Waits for command to complete. Can sleep. 11589 */ 11590 11591 static int 11592 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11593 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11594 int path_flag) 11595 { 11596 struct sd_uscsi_info *uip; 11597 struct uscsi_cmd *uscmd; 11598 struct sd_lun *un; 11599 struct buf *bp; 11600 int rval; 11601 int flags; 11602 11603 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11604 if (un == NULL) { 11605 return (ENXIO); 11606 } 11607 11608 ASSERT(!mutex_owned(SD_MUTEX(un))); 11609 11610 #ifdef SDDEBUG 11611 switch (dataspace) { 11612 case UIO_USERSPACE: 11613 SD_TRACE(SD_LOG_IO, un, 11614 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11615 break; 11616 case UIO_SYSSPACE: 11617 SD_TRACE(SD_LOG_IO, un, 11618 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11619 break; 11620 default: 11621 SD_TRACE(SD_LOG_IO, un, 11622 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11623 break; 11624 } 11625 #endif 11626 11627 /* 11628 * Perform resets directly; no need to generate a command to do it. 11629 */ 11630 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11631 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11632 RESET_ALL : RESET_TARGET; 11633 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11634 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11635 /* Reset attempt was unsuccessful */ 11636 SD_TRACE(SD_LOG_IO, un, 11637 "sd_send_scsi_cmd: reset: failure\n"); 11638 return (EIO); 11639 } 11640 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11641 return (0); 11642 } 11643 11644 /* Perfunctory sanity check... */ 11645 if (incmd->uscsi_cdblen <= 0) { 11646 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11647 "invalid uscsi_cdblen, returning EINVAL\n"); 11648 return (EINVAL); 11649 } 11650 11651 /* 11652 * In order to not worry about where the uscsi structure came from 11653 * (or where the cdb it points to came from) we're going to make 11654 * kmem_alloc'd copies of them here. This will also allow reference 11655 * to the data they contain long after this process has gone to 11656 * sleep and its kernel stack has been unmapped, etc. 11657 * 11658 * First get some memory for the uscsi_cmd struct and copy the 11659 * contents of the given uscsi_cmd struct into it. 11660 */ 11661 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11662 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11663 11664 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11665 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11666 11667 /* 11668 * Now get some space for the CDB, and copy the given CDB into 11669 * it. Use ddi_copyin() in case the data is in user space. 11670 */ 11671 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11672 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11673 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11674 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11675 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11676 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11677 return (EFAULT); 11678 } 11679 11680 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11681 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11682 11683 bp = getrbuf(KM_SLEEP); 11684 11685 /* 11686 * Allocate an sd_uscsi_info struct and fill it with the info 11687 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11688 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11689 * since we allocate the buf here in this function, we do not 11690 * need to preserve the prior contents of b_private. 11691 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11692 */ 11693 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11694 uip->ui_flags = path_flag; 11695 uip->ui_cmdp = uscmd; 11696 bp->b_private = uip; 11697 11698 /* 11699 * Initialize Request Sense buffering, if requested. 11700 */ 11701 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11702 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11703 /* 11704 * Here uscmd->uscsi_rqbuf currently points to the caller's 11705 * buffer, but we replace this with a kernel buffer that 11706 * we allocate to use with the sense data. The sense data 11707 * (if present) gets copied into this new buffer before the 11708 * command is completed. Then we copy the sense data from 11709 * our allocated buf into the caller's buffer below. Note 11710 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11711 * below to perform the copy back to the caller's buf. 11712 */ 11713 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11714 if (rqbufspace == UIO_USERSPACE) { 11715 uscmd->uscsi_rqlen = SENSE_LENGTH; 11716 uscmd->uscsi_rqresid = SENSE_LENGTH; 11717 } else { 11718 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11719 uscmd->uscsi_rqlen = rlen; 11720 uscmd->uscsi_rqresid = rlen; 11721 } 11722 } else { 11723 uscmd->uscsi_rqbuf = NULL; 11724 uscmd->uscsi_rqlen = 0; 11725 uscmd->uscsi_rqresid = 0; 11726 } 11727 11728 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11729 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11730 11731 if (un->un_f_is_fibre == FALSE) { 11732 /* 11733 * Force asynchronous mode, if necessary. Doing this here 11734 * has the unfortunate effect of running other queued 11735 * commands async also, but since the main purpose of this 11736 * capability is downloading new drive firmware, we can 11737 * probably live with it. 11738 */ 11739 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11740 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11741 == 1) { 11742 if (scsi_ifsetcap(SD_ADDRESS(un), 11743 "synchronous", 0, 1) == 1) { 11744 SD_TRACE(SD_LOG_IO, un, 11745 "sd_send_scsi_cmd: forced async ok\n"); 11746 } else { 11747 SD_TRACE(SD_LOG_IO, un, 11748 "sd_send_scsi_cmd:\ 11749 forced async failed\n"); 11750 rval = EINVAL; 11751 goto done; 11752 } 11753 } 11754 } 11755 11756 /* 11757 * Re-enable synchronous mode, if requested 11758 */ 11759 if (uscmd->uscsi_flags & USCSI_SYNC) { 11760 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11761 == 0) { 11762 int i = scsi_ifsetcap(SD_ADDRESS(un), 11763 "synchronous", 1, 1); 11764 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11765 "re-enabled sync %s\n", 11766 (i == 1) ? "ok" : "failed"); 11767 } 11768 } 11769 } 11770 11771 /* 11772 * Commands sent with priority are intended for error recovery 11773 * situations, and do not have retries performed. 11774 */ 11775 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11776 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11777 } 11778 11779 /* 11780 * If we're going to do actual I/O, let physio do all the right things 11781 */ 11782 if (uscmd->uscsi_buflen != 0) { 11783 struct iovec aiov; 11784 struct uio auio; 11785 struct uio *uio = &auio; 11786 11787 bzero(&auio, sizeof (struct uio)); 11788 bzero(&aiov, sizeof (struct iovec)); 11789 aiov.iov_base = uscmd->uscsi_bufaddr; 11790 aiov.iov_len = uscmd->uscsi_buflen; 11791 uio->uio_iov = &aiov; 11792 11793 uio->uio_iovcnt = 1; 11794 uio->uio_resid = uscmd->uscsi_buflen; 11795 uio->uio_segflg = dataspace; 11796 11797 /* 11798 * physio() will block here until the command completes.... 11799 */ 11800 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 11801 11802 rval = physio(sd_uscsi_strategy, bp, dev, 11803 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 11804 sduscsimin, uio); 11805 11806 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11807 "returned from physio with 0x%x\n", rval); 11808 11809 } else { 11810 /* 11811 * We have to mimic what physio would do here! Argh! 11812 */ 11813 bp->b_flags = B_BUSY | 11814 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 11815 bp->b_edev = dev; 11816 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 11817 bp->b_bcount = 0; 11818 bp->b_blkno = 0; 11819 11820 SD_TRACE(SD_LOG_IO, un, 11821 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 11822 11823 (void) sd_uscsi_strategy(bp); 11824 11825 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 11826 11827 rval = biowait(bp); 11828 11829 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11830 "returned from biowait with 0x%x\n", rval); 11831 } 11832 11833 done: 11834 11835 #ifdef SDDEBUG 11836 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11837 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11838 uscmd->uscsi_status, uscmd->uscsi_resid); 11839 if (uscmd->uscsi_bufaddr != NULL) { 11840 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11841 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11842 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11843 if (dataspace == UIO_SYSSPACE) { 11844 SD_DUMP_MEMORY(un, SD_LOG_IO, 11845 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11846 uscmd->uscsi_buflen, SD_LOG_HEX); 11847 } 11848 } 11849 #endif 11850 11851 /* 11852 * Get the status and residual to return to the caller. 11853 */ 11854 incmd->uscsi_status = uscmd->uscsi_status; 11855 incmd->uscsi_resid = uscmd->uscsi_resid; 11856 11857 /* 11858 * If the caller wants sense data, copy back whatever sense data 11859 * we may have gotten, and update the relevant rqsense info. 11860 */ 11861 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11862 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11863 11864 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 11865 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 11866 11867 /* Update the Request Sense status and resid */ 11868 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 11869 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 11870 11871 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11872 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 11873 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 11874 11875 /* Copy out the sense data for user processes */ 11876 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 11877 int flags = 11878 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 11879 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 11880 rqlen, flags) != 0) { 11881 rval = EFAULT; 11882 } 11883 /* 11884 * Note: Can't touch incmd->uscsi_rqbuf so use 11885 * uscmd->uscsi_rqbuf instead. They're the same. 11886 */ 11887 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11888 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 11889 incmd->uscsi_rqbuf, rqlen); 11890 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 11891 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 11892 } 11893 } 11894 11895 /* 11896 * Free allocated resources and return; mapout the buf in case it was 11897 * mapped in by a lower layer. 11898 */ 11899 bp_mapout(bp); 11900 freerbuf(bp); 11901 kmem_free(uip, sizeof (struct sd_uscsi_info)); 11902 if (uscmd->uscsi_rqbuf != NULL) { 11903 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 11904 } 11905 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 11906 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11907 11908 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 11909 11910 return (rval); 11911 } 11912 11913 11914 /* 11915 * Function: sd_buf_iodone 11916 * 11917 * Description: Frees the sd_xbuf & returns the buf to its originator. 11918 * 11919 * Context: May be called from interrupt context. 11920 */ 11921 /* ARGSUSED */ 11922 static void 11923 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 11924 { 11925 struct sd_xbuf *xp; 11926 11927 ASSERT(un != NULL); 11928 ASSERT(bp != NULL); 11929 ASSERT(!mutex_owned(SD_MUTEX(un))); 11930 11931 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 11932 11933 xp = SD_GET_XBUF(bp); 11934 ASSERT(xp != NULL); 11935 11936 mutex_enter(SD_MUTEX(un)); 11937 11938 /* 11939 * Grab time when the cmd completed. 11940 * This is used for determining if the system has been 11941 * idle long enough to make it idle to the PM framework. 11942 * This is for lowering the overhead, and therefore improving 11943 * performance per I/O operation. 11944 */ 11945 un->un_pm_idle_time = ddi_get_time(); 11946 11947 un->un_ncmds_in_driver--; 11948 ASSERT(un->un_ncmds_in_driver >= 0); 11949 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 11950 un->un_ncmds_in_driver); 11951 11952 mutex_exit(SD_MUTEX(un)); 11953 11954 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 11955 biodone(bp); /* bp is gone after this */ 11956 11957 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 11958 } 11959 11960 11961 /* 11962 * Function: sd_uscsi_iodone 11963 * 11964 * Description: Frees the sd_xbuf & returns the buf to its originator. 11965 * 11966 * Context: May be called from interrupt context. 11967 */ 11968 /* ARGSUSED */ 11969 static void 11970 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11971 { 11972 struct sd_xbuf *xp; 11973 11974 ASSERT(un != NULL); 11975 ASSERT(bp != NULL); 11976 11977 xp = SD_GET_XBUF(bp); 11978 ASSERT(xp != NULL); 11979 ASSERT(!mutex_owned(SD_MUTEX(un))); 11980 11981 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 11982 11983 bp->b_private = xp->xb_private; 11984 11985 mutex_enter(SD_MUTEX(un)); 11986 11987 /* 11988 * Grab time when the cmd completed. 11989 * This is used for determining if the system has been 11990 * idle long enough to make it idle to the PM framework. 11991 * This is for lowering the overhead, and therefore improving 11992 * performance per I/O operation. 11993 */ 11994 un->un_pm_idle_time = ddi_get_time(); 11995 11996 un->un_ncmds_in_driver--; 11997 ASSERT(un->un_ncmds_in_driver >= 0); 11998 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 11999 un->un_ncmds_in_driver); 12000 12001 mutex_exit(SD_MUTEX(un)); 12002 12003 kmem_free(xp, sizeof (struct sd_xbuf)); 12004 biodone(bp); 12005 12006 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12007 } 12008 12009 12010 /* 12011 * Function: sd_mapblockaddr_iostart 12012 * 12013 * Description: Verify request lies withing the partition limits for 12014 * the indicated minor device. Issue "overrun" buf if 12015 * request would exceed partition range. Converts 12016 * partition-relative block address to absolute. 12017 * 12018 * Context: Can sleep 12019 * 12020 * Issues: This follows what the old code did, in terms of accessing 12021 * some of the partition info in the unit struct without holding 12022 * the mutext. This is a general issue, if the partition info 12023 * can be altered while IO is in progress... as soon as we send 12024 * a buf, its partitioning can be invalid before it gets to the 12025 * device. Probably the right fix is to move partitioning out 12026 * of the driver entirely. 12027 */ 12028 12029 static void 12030 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12031 { 12032 daddr_t nblocks; /* #blocks in the given partition */ 12033 daddr_t blocknum; /* Block number specified by the buf */ 12034 size_t requested_nblocks; 12035 size_t available_nblocks; 12036 int partition; 12037 diskaddr_t partition_offset; 12038 struct sd_xbuf *xp; 12039 12040 12041 ASSERT(un != NULL); 12042 ASSERT(bp != NULL); 12043 ASSERT(!mutex_owned(SD_MUTEX(un))); 12044 12045 SD_TRACE(SD_LOG_IO_PARTITION, un, 12046 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12047 12048 xp = SD_GET_XBUF(bp); 12049 ASSERT(xp != NULL); 12050 12051 /* 12052 * If the geometry is not indicated as valid, attempt to access 12053 * the unit & verify the geometry/label. This can be the case for 12054 * removable-media devices, of if the device was opened in 12055 * NDELAY/NONBLOCK mode. 12056 */ 12057 if ((un->un_f_geometry_is_valid != TRUE) && 12058 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12059 /* 12060 * For removable devices it is possible to start an I/O 12061 * without a media by opening the device in nodelay mode. 12062 * Also for writable CDs there can be many scenarios where 12063 * there is no geometry yet but volume manager is trying to 12064 * issue a read() just because it can see TOC on the CD. So 12065 * do not print a message for removables. 12066 */ 12067 if (!un->un_f_has_removable_media) { 12068 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12069 "i/o to invalid geometry\n"); 12070 } 12071 bioerror(bp, EIO); 12072 bp->b_resid = bp->b_bcount; 12073 SD_BEGIN_IODONE(index, un, bp); 12074 return; 12075 } 12076 12077 partition = SDPART(bp->b_edev); 12078 12079 /* #blocks in partition */ 12080 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12081 12082 /* Use of a local variable potentially improves performance slightly */ 12083 partition_offset = un->un_offset[partition]; 12084 12085 /* 12086 * blocknum is the starting block number of the request. At this 12087 * point it is still relative to the start of the minor device. 12088 */ 12089 blocknum = xp->xb_blkno; 12090 12091 /* 12092 * Legacy: If the starting block number is one past the last block 12093 * in the partition, do not set B_ERROR in the buf. 12094 */ 12095 if (blocknum == nblocks) { 12096 goto error_exit; 12097 } 12098 12099 /* 12100 * Confirm that the first block of the request lies within the 12101 * partition limits. Also the requested number of bytes must be 12102 * a multiple of the system block size. 12103 */ 12104 if ((blocknum < 0) || (blocknum >= nblocks) || 12105 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12106 bp->b_flags |= B_ERROR; 12107 goto error_exit; 12108 } 12109 12110 /* 12111 * If the requsted # blocks exceeds the available # blocks, that 12112 * is an overrun of the partition. 12113 */ 12114 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12115 available_nblocks = (size_t)(nblocks - blocknum); 12116 ASSERT(nblocks >= blocknum); 12117 12118 if (requested_nblocks > available_nblocks) { 12119 /* 12120 * Allocate an "overrun" buf to allow the request to proceed 12121 * for the amount of space available in the partition. The 12122 * amount not transferred will be added into the b_resid 12123 * when the operation is complete. The overrun buf 12124 * replaces the original buf here, and the original buf 12125 * is saved inside the overrun buf, for later use. 12126 */ 12127 size_t resid = SD_SYSBLOCKS2BYTES(un, 12128 (offset_t)(requested_nblocks - available_nblocks)); 12129 size_t count = bp->b_bcount - resid; 12130 /* 12131 * Note: count is an unsigned entity thus it'll NEVER 12132 * be less than 0 so ASSERT the original values are 12133 * correct. 12134 */ 12135 ASSERT(bp->b_bcount >= resid); 12136 12137 bp = sd_bioclone_alloc(bp, count, blocknum, 12138 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12139 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12140 ASSERT(xp != NULL); 12141 } 12142 12143 /* At this point there should be no residual for this buf. */ 12144 ASSERT(bp->b_resid == 0); 12145 12146 /* Convert the block number to an absolute address. */ 12147 xp->xb_blkno += partition_offset; 12148 12149 SD_NEXT_IOSTART(index, un, bp); 12150 12151 SD_TRACE(SD_LOG_IO_PARTITION, un, 12152 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12153 12154 return; 12155 12156 error_exit: 12157 bp->b_resid = bp->b_bcount; 12158 SD_BEGIN_IODONE(index, un, bp); 12159 SD_TRACE(SD_LOG_IO_PARTITION, un, 12160 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12161 } 12162 12163 12164 /* 12165 * Function: sd_mapblockaddr_iodone 12166 * 12167 * Description: Completion-side processing for partition management. 12168 * 12169 * Context: May be called under interrupt context 12170 */ 12171 12172 static void 12173 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12174 { 12175 /* int partition; */ /* Not used, see below. */ 12176 ASSERT(un != NULL); 12177 ASSERT(bp != NULL); 12178 ASSERT(!mutex_owned(SD_MUTEX(un))); 12179 12180 SD_TRACE(SD_LOG_IO_PARTITION, un, 12181 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12182 12183 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12184 /* 12185 * We have an "overrun" buf to deal with... 12186 */ 12187 struct sd_xbuf *xp; 12188 struct buf *obp; /* ptr to the original buf */ 12189 12190 xp = SD_GET_XBUF(bp); 12191 ASSERT(xp != NULL); 12192 12193 /* Retrieve the pointer to the original buf */ 12194 obp = (struct buf *)xp->xb_private; 12195 ASSERT(obp != NULL); 12196 12197 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12198 bioerror(obp, bp->b_error); 12199 12200 sd_bioclone_free(bp); 12201 12202 /* 12203 * Get back the original buf. 12204 * Note that since the restoration of xb_blkno below 12205 * was removed, the sd_xbuf is not needed. 12206 */ 12207 bp = obp; 12208 /* 12209 * xp = SD_GET_XBUF(bp); 12210 * ASSERT(xp != NULL); 12211 */ 12212 } 12213 12214 /* 12215 * Convert sd->xb_blkno back to a minor-device relative value. 12216 * Note: this has been commented out, as it is not needed in the 12217 * current implementation of the driver (ie, since this function 12218 * is at the top of the layering chains, so the info will be 12219 * discarded) and it is in the "hot" IO path. 12220 * 12221 * partition = getminor(bp->b_edev) & SDPART_MASK; 12222 * xp->xb_blkno -= un->un_offset[partition]; 12223 */ 12224 12225 SD_NEXT_IODONE(index, un, bp); 12226 12227 SD_TRACE(SD_LOG_IO_PARTITION, un, 12228 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12229 } 12230 12231 12232 /* 12233 * Function: sd_mapblocksize_iostart 12234 * 12235 * Description: Convert between system block size (un->un_sys_blocksize) 12236 * and target block size (un->un_tgt_blocksize). 12237 * 12238 * Context: Can sleep to allocate resources. 12239 * 12240 * Assumptions: A higher layer has already performed any partition validation, 12241 * and converted the xp->xb_blkno to an absolute value relative 12242 * to the start of the device. 12243 * 12244 * It is also assumed that the higher layer has implemented 12245 * an "overrun" mechanism for the case where the request would 12246 * read/write beyond the end of a partition. In this case we 12247 * assume (and ASSERT) that bp->b_resid == 0. 12248 * 12249 * Note: The implementation for this routine assumes the target 12250 * block size remains constant between allocation and transport. 12251 */ 12252 12253 static void 12254 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12255 { 12256 struct sd_mapblocksize_info *bsp; 12257 struct sd_xbuf *xp; 12258 offset_t first_byte; 12259 daddr_t start_block, end_block; 12260 daddr_t request_bytes; 12261 ushort_t is_aligned = FALSE; 12262 12263 ASSERT(un != NULL); 12264 ASSERT(bp != NULL); 12265 ASSERT(!mutex_owned(SD_MUTEX(un))); 12266 ASSERT(bp->b_resid == 0); 12267 12268 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12269 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12270 12271 /* 12272 * For a non-writable CD, a write request is an error 12273 */ 12274 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12275 (un->un_f_mmc_writable_media == FALSE)) { 12276 bioerror(bp, EIO); 12277 bp->b_resid = bp->b_bcount; 12278 SD_BEGIN_IODONE(index, un, bp); 12279 return; 12280 } 12281 12282 /* 12283 * We do not need a shadow buf if the device is using 12284 * un->un_sys_blocksize as its block size or if bcount == 0. 12285 * In this case there is no layer-private data block allocated. 12286 */ 12287 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12288 (bp->b_bcount == 0)) { 12289 goto done; 12290 } 12291 12292 #if defined(__i386) || defined(__amd64) 12293 /* We do not support non-block-aligned transfers for ROD devices */ 12294 ASSERT(!ISROD(un)); 12295 #endif 12296 12297 xp = SD_GET_XBUF(bp); 12298 ASSERT(xp != NULL); 12299 12300 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12301 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12302 un->un_tgt_blocksize, un->un_sys_blocksize); 12303 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12304 "request start block:0x%x\n", xp->xb_blkno); 12305 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12306 "request len:0x%x\n", bp->b_bcount); 12307 12308 /* 12309 * Allocate the layer-private data area for the mapblocksize layer. 12310 * Layers are allowed to use the xp_private member of the sd_xbuf 12311 * struct to store the pointer to their layer-private data block, but 12312 * each layer also has the responsibility of restoring the prior 12313 * contents of xb_private before returning the buf/xbuf to the 12314 * higher layer that sent it. 12315 * 12316 * Here we save the prior contents of xp->xb_private into the 12317 * bsp->mbs_oprivate field of our layer-private data area. This value 12318 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12319 * the layer-private area and returning the buf/xbuf to the layer 12320 * that sent it. 12321 * 12322 * Note that here we use kmem_zalloc for the allocation as there are 12323 * parts of the mapblocksize code that expect certain fields to be 12324 * zero unless explicitly set to a required value. 12325 */ 12326 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12327 bsp->mbs_oprivate = xp->xb_private; 12328 xp->xb_private = bsp; 12329 12330 /* 12331 * This treats the data on the disk (target) as an array of bytes. 12332 * first_byte is the byte offset, from the beginning of the device, 12333 * to the location of the request. This is converted from a 12334 * un->un_sys_blocksize block address to a byte offset, and then back 12335 * to a block address based upon a un->un_tgt_blocksize block size. 12336 * 12337 * xp->xb_blkno should be absolute upon entry into this function, 12338 * but, but it is based upon partitions that use the "system" 12339 * block size. It must be adjusted to reflect the block size of 12340 * the target. 12341 * 12342 * Note that end_block is actually the block that follows the last 12343 * block of the request, but that's what is needed for the computation. 12344 */ 12345 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12346 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12347 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12348 un->un_tgt_blocksize; 12349 12350 /* request_bytes is rounded up to a multiple of the target block size */ 12351 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12352 12353 /* 12354 * See if the starting address of the request and the request 12355 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12356 * then we do not need to allocate a shadow buf to handle the request. 12357 */ 12358 if (((first_byte % un->un_tgt_blocksize) == 0) && 12359 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12360 is_aligned = TRUE; 12361 } 12362 12363 if ((bp->b_flags & B_READ) == 0) { 12364 /* 12365 * Lock the range for a write operation. An aligned request is 12366 * considered a simple write; otherwise the request must be a 12367 * read-modify-write. 12368 */ 12369 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12370 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12371 } 12372 12373 /* 12374 * Alloc a shadow buf if the request is not aligned. Also, this is 12375 * where the READ command is generated for a read-modify-write. (The 12376 * write phase is deferred until after the read completes.) 12377 */ 12378 if (is_aligned == FALSE) { 12379 12380 struct sd_mapblocksize_info *shadow_bsp; 12381 struct sd_xbuf *shadow_xp; 12382 struct buf *shadow_bp; 12383 12384 /* 12385 * Allocate the shadow buf and it associated xbuf. Note that 12386 * after this call the xb_blkno value in both the original 12387 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12388 * same: absolute relative to the start of the device, and 12389 * adjusted for the target block size. The b_blkno in the 12390 * shadow buf will also be set to this value. We should never 12391 * change b_blkno in the original bp however. 12392 * 12393 * Note also that the shadow buf will always need to be a 12394 * READ command, regardless of whether the incoming command 12395 * is a READ or a WRITE. 12396 */ 12397 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12398 xp->xb_blkno, 12399 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12400 12401 shadow_xp = SD_GET_XBUF(shadow_bp); 12402 12403 /* 12404 * Allocate the layer-private data for the shadow buf. 12405 * (No need to preserve xb_private in the shadow xbuf.) 12406 */ 12407 shadow_xp->xb_private = shadow_bsp = 12408 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12409 12410 /* 12411 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12412 * to figure out where the start of the user data is (based upon 12413 * the system block size) in the data returned by the READ 12414 * command (which will be based upon the target blocksize). Note 12415 * that this is only really used if the request is unaligned. 12416 */ 12417 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12418 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12419 ASSERT((bsp->mbs_copy_offset >= 0) && 12420 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12421 12422 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12423 12424 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12425 12426 /* Transfer the wmap (if any) to the shadow buf */ 12427 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12428 bsp->mbs_wmp = NULL; 12429 12430 /* 12431 * The shadow buf goes on from here in place of the 12432 * original buf. 12433 */ 12434 shadow_bsp->mbs_orig_bp = bp; 12435 bp = shadow_bp; 12436 } 12437 12438 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12439 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12440 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12441 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12442 request_bytes); 12443 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12444 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12445 12446 done: 12447 SD_NEXT_IOSTART(index, un, bp); 12448 12449 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12450 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12451 } 12452 12453 12454 /* 12455 * Function: sd_mapblocksize_iodone 12456 * 12457 * Description: Completion side processing for block-size mapping. 12458 * 12459 * Context: May be called under interrupt context 12460 */ 12461 12462 static void 12463 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12464 { 12465 struct sd_mapblocksize_info *bsp; 12466 struct sd_xbuf *xp; 12467 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12468 struct buf *orig_bp; /* ptr to the original buf */ 12469 offset_t shadow_end; 12470 offset_t request_end; 12471 offset_t shadow_start; 12472 ssize_t copy_offset; 12473 size_t copy_length; 12474 size_t shortfall; 12475 uint_t is_write; /* TRUE if this bp is a WRITE */ 12476 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12477 12478 ASSERT(un != NULL); 12479 ASSERT(bp != NULL); 12480 12481 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12482 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12483 12484 /* 12485 * There is no shadow buf or layer-private data if the target is 12486 * using un->un_sys_blocksize as its block size or if bcount == 0. 12487 */ 12488 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12489 (bp->b_bcount == 0)) { 12490 goto exit; 12491 } 12492 12493 xp = SD_GET_XBUF(bp); 12494 ASSERT(xp != NULL); 12495 12496 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12497 bsp = xp->xb_private; 12498 12499 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12500 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12501 12502 if (is_write) { 12503 /* 12504 * For a WRITE request we must free up the block range that 12505 * we have locked up. This holds regardless of whether this is 12506 * an aligned write request or a read-modify-write request. 12507 */ 12508 sd_range_unlock(un, bsp->mbs_wmp); 12509 bsp->mbs_wmp = NULL; 12510 } 12511 12512 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12513 /* 12514 * An aligned read or write command will have no shadow buf; 12515 * there is not much else to do with it. 12516 */ 12517 goto done; 12518 } 12519 12520 orig_bp = bsp->mbs_orig_bp; 12521 ASSERT(orig_bp != NULL); 12522 orig_xp = SD_GET_XBUF(orig_bp); 12523 ASSERT(orig_xp != NULL); 12524 ASSERT(!mutex_owned(SD_MUTEX(un))); 12525 12526 if (!is_write && has_wmap) { 12527 /* 12528 * A READ with a wmap means this is the READ phase of a 12529 * read-modify-write. If an error occurred on the READ then 12530 * we do not proceed with the WRITE phase or copy any data. 12531 * Just release the write maps and return with an error. 12532 */ 12533 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12534 orig_bp->b_resid = orig_bp->b_bcount; 12535 bioerror(orig_bp, bp->b_error); 12536 sd_range_unlock(un, bsp->mbs_wmp); 12537 goto freebuf_done; 12538 } 12539 } 12540 12541 /* 12542 * Here is where we set up to copy the data from the shadow buf 12543 * into the space associated with the original buf. 12544 * 12545 * To deal with the conversion between block sizes, these 12546 * computations treat the data as an array of bytes, with the 12547 * first byte (byte 0) corresponding to the first byte in the 12548 * first block on the disk. 12549 */ 12550 12551 /* 12552 * shadow_start and shadow_len indicate the location and size of 12553 * the data returned with the shadow IO request. 12554 */ 12555 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12556 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12557 12558 /* 12559 * copy_offset gives the offset (in bytes) from the start of the first 12560 * block of the READ request to the beginning of the data. We retrieve 12561 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12562 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12563 * data to be copied (in bytes). 12564 */ 12565 copy_offset = bsp->mbs_copy_offset; 12566 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12567 copy_length = orig_bp->b_bcount; 12568 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12569 12570 /* 12571 * Set up the resid and error fields of orig_bp as appropriate. 12572 */ 12573 if (shadow_end >= request_end) { 12574 /* We got all the requested data; set resid to zero */ 12575 orig_bp->b_resid = 0; 12576 } else { 12577 /* 12578 * We failed to get enough data to fully satisfy the original 12579 * request. Just copy back whatever data we got and set 12580 * up the residual and error code as required. 12581 * 12582 * 'shortfall' is the amount by which the data received with the 12583 * shadow buf has "fallen short" of the requested amount. 12584 */ 12585 shortfall = (size_t)(request_end - shadow_end); 12586 12587 if (shortfall > orig_bp->b_bcount) { 12588 /* 12589 * We did not get enough data to even partially 12590 * fulfill the original request. The residual is 12591 * equal to the amount requested. 12592 */ 12593 orig_bp->b_resid = orig_bp->b_bcount; 12594 } else { 12595 /* 12596 * We did not get all the data that we requested 12597 * from the device, but we will try to return what 12598 * portion we did get. 12599 */ 12600 orig_bp->b_resid = shortfall; 12601 } 12602 ASSERT(copy_length >= orig_bp->b_resid); 12603 copy_length -= orig_bp->b_resid; 12604 } 12605 12606 /* Propagate the error code from the shadow buf to the original buf */ 12607 bioerror(orig_bp, bp->b_error); 12608 12609 if (is_write) { 12610 goto freebuf_done; /* No data copying for a WRITE */ 12611 } 12612 12613 if (has_wmap) { 12614 /* 12615 * This is a READ command from the READ phase of a 12616 * read-modify-write request. We have to copy the data given 12617 * by the user OVER the data returned by the READ command, 12618 * then convert the command from a READ to a WRITE and send 12619 * it back to the target. 12620 */ 12621 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12622 copy_length); 12623 12624 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12625 12626 /* 12627 * Dispatch the WRITE command to the taskq thread, which 12628 * will in turn send the command to the target. When the 12629 * WRITE command completes, we (sd_mapblocksize_iodone()) 12630 * will get called again as part of the iodone chain 12631 * processing for it. Note that we will still be dealing 12632 * with the shadow buf at that point. 12633 */ 12634 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12635 KM_NOSLEEP) != 0) { 12636 /* 12637 * Dispatch was successful so we are done. Return 12638 * without going any higher up the iodone chain. Do 12639 * not free up any layer-private data until after the 12640 * WRITE completes. 12641 */ 12642 return; 12643 } 12644 12645 /* 12646 * Dispatch of the WRITE command failed; set up the error 12647 * condition and send this IO back up the iodone chain. 12648 */ 12649 bioerror(orig_bp, EIO); 12650 orig_bp->b_resid = orig_bp->b_bcount; 12651 12652 } else { 12653 /* 12654 * This is a regular READ request (ie, not a RMW). Copy the 12655 * data from the shadow buf into the original buf. The 12656 * copy_offset compensates for any "misalignment" between the 12657 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12658 * original buf (with its un->un_sys_blocksize blocks). 12659 */ 12660 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12661 copy_length); 12662 } 12663 12664 freebuf_done: 12665 12666 /* 12667 * At this point we still have both the shadow buf AND the original 12668 * buf to deal with, as well as the layer-private data area in each. 12669 * Local variables are as follows: 12670 * 12671 * bp -- points to shadow buf 12672 * xp -- points to xbuf of shadow buf 12673 * bsp -- points to layer-private data area of shadow buf 12674 * orig_bp -- points to original buf 12675 * 12676 * First free the shadow buf and its associated xbuf, then free the 12677 * layer-private data area from the shadow buf. There is no need to 12678 * restore xb_private in the shadow xbuf. 12679 */ 12680 sd_shadow_buf_free(bp); 12681 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12682 12683 /* 12684 * Now update the local variables to point to the original buf, xbuf, 12685 * and layer-private area. 12686 */ 12687 bp = orig_bp; 12688 xp = SD_GET_XBUF(bp); 12689 ASSERT(xp != NULL); 12690 ASSERT(xp == orig_xp); 12691 bsp = xp->xb_private; 12692 ASSERT(bsp != NULL); 12693 12694 done: 12695 /* 12696 * Restore xb_private to whatever it was set to by the next higher 12697 * layer in the chain, then free the layer-private data area. 12698 */ 12699 xp->xb_private = bsp->mbs_oprivate; 12700 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12701 12702 exit: 12703 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12704 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12705 12706 SD_NEXT_IODONE(index, un, bp); 12707 } 12708 12709 12710 /* 12711 * Function: sd_checksum_iostart 12712 * 12713 * Description: A stub function for a layer that's currently not used. 12714 * For now just a placeholder. 12715 * 12716 * Context: Kernel thread context 12717 */ 12718 12719 static void 12720 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12721 { 12722 ASSERT(un != NULL); 12723 ASSERT(bp != NULL); 12724 ASSERT(!mutex_owned(SD_MUTEX(un))); 12725 SD_NEXT_IOSTART(index, un, bp); 12726 } 12727 12728 12729 /* 12730 * Function: sd_checksum_iodone 12731 * 12732 * Description: A stub function for a layer that's currently not used. 12733 * For now just a placeholder. 12734 * 12735 * Context: May be called under interrupt context 12736 */ 12737 12738 static void 12739 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12740 { 12741 ASSERT(un != NULL); 12742 ASSERT(bp != NULL); 12743 ASSERT(!mutex_owned(SD_MUTEX(un))); 12744 SD_NEXT_IODONE(index, un, bp); 12745 } 12746 12747 12748 /* 12749 * Function: sd_checksum_uscsi_iostart 12750 * 12751 * Description: A stub function for a layer that's currently not used. 12752 * For now just a placeholder. 12753 * 12754 * Context: Kernel thread context 12755 */ 12756 12757 static void 12758 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12759 { 12760 ASSERT(un != NULL); 12761 ASSERT(bp != NULL); 12762 ASSERT(!mutex_owned(SD_MUTEX(un))); 12763 SD_NEXT_IOSTART(index, un, bp); 12764 } 12765 12766 12767 /* 12768 * Function: sd_checksum_uscsi_iodone 12769 * 12770 * Description: A stub function for a layer that's currently not used. 12771 * For now just a placeholder. 12772 * 12773 * Context: May be called under interrupt context 12774 */ 12775 12776 static void 12777 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12778 { 12779 ASSERT(un != NULL); 12780 ASSERT(bp != NULL); 12781 ASSERT(!mutex_owned(SD_MUTEX(un))); 12782 SD_NEXT_IODONE(index, un, bp); 12783 } 12784 12785 12786 /* 12787 * Function: sd_pm_iostart 12788 * 12789 * Description: iostart-side routine for Power mangement. 12790 * 12791 * Context: Kernel thread context 12792 */ 12793 12794 static void 12795 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12796 { 12797 ASSERT(un != NULL); 12798 ASSERT(bp != NULL); 12799 ASSERT(!mutex_owned(SD_MUTEX(un))); 12800 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12801 12802 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12803 12804 if (sd_pm_entry(un) != DDI_SUCCESS) { 12805 /* 12806 * Set up to return the failed buf back up the 'iodone' 12807 * side of the calling chain. 12808 */ 12809 bioerror(bp, EIO); 12810 bp->b_resid = bp->b_bcount; 12811 12812 SD_BEGIN_IODONE(index, un, bp); 12813 12814 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12815 return; 12816 } 12817 12818 SD_NEXT_IOSTART(index, un, bp); 12819 12820 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12821 } 12822 12823 12824 /* 12825 * Function: sd_pm_iodone 12826 * 12827 * Description: iodone-side routine for power mangement. 12828 * 12829 * Context: may be called from interrupt context 12830 */ 12831 12832 static void 12833 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12834 { 12835 ASSERT(un != NULL); 12836 ASSERT(bp != NULL); 12837 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12838 12839 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12840 12841 /* 12842 * After attach the following flag is only read, so don't 12843 * take the penalty of acquiring a mutex for it. 12844 */ 12845 if (un->un_f_pm_is_enabled == TRUE) { 12846 sd_pm_exit(un); 12847 } 12848 12849 SD_NEXT_IODONE(index, un, bp); 12850 12851 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12852 } 12853 12854 12855 /* 12856 * Function: sd_core_iostart 12857 * 12858 * Description: Primary driver function for enqueuing buf(9S) structs from 12859 * the system and initiating IO to the target device 12860 * 12861 * Context: Kernel thread context. Can sleep. 12862 * 12863 * Assumptions: - The given xp->xb_blkno is absolute 12864 * (ie, relative to the start of the device). 12865 * - The IO is to be done using the native blocksize of 12866 * the device, as specified in un->un_tgt_blocksize. 12867 */ 12868 /* ARGSUSED */ 12869 static void 12870 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12871 { 12872 struct sd_xbuf *xp; 12873 12874 ASSERT(un != NULL); 12875 ASSERT(bp != NULL); 12876 ASSERT(!mutex_owned(SD_MUTEX(un))); 12877 ASSERT(bp->b_resid == 0); 12878 12879 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12880 12881 xp = SD_GET_XBUF(bp); 12882 ASSERT(xp != NULL); 12883 12884 mutex_enter(SD_MUTEX(un)); 12885 12886 /* 12887 * If we are currently in the failfast state, fail any new IO 12888 * that has B_FAILFAST set, then return. 12889 */ 12890 if ((bp->b_flags & B_FAILFAST) && 12891 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12892 mutex_exit(SD_MUTEX(un)); 12893 bioerror(bp, EIO); 12894 bp->b_resid = bp->b_bcount; 12895 SD_BEGIN_IODONE(index, un, bp); 12896 return; 12897 } 12898 12899 if (SD_IS_DIRECT_PRIORITY(xp)) { 12900 /* 12901 * Priority command -- transport it immediately. 12902 * 12903 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12904 * because all direct priority commands should be associated 12905 * with error recovery actions which we don't want to retry. 12906 */ 12907 sd_start_cmds(un, bp); 12908 } else { 12909 /* 12910 * Normal command -- add it to the wait queue, then start 12911 * transporting commands from the wait queue. 12912 */ 12913 sd_add_buf_to_waitq(un, bp); 12914 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12915 sd_start_cmds(un, NULL); 12916 } 12917 12918 mutex_exit(SD_MUTEX(un)); 12919 12920 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 12921 } 12922 12923 12924 /* 12925 * Function: sd_init_cdb_limits 12926 * 12927 * Description: This is to handle scsi_pkt initialization differences 12928 * between the driver platforms. 12929 * 12930 * Legacy behaviors: 12931 * 12932 * If the block number or the sector count exceeds the 12933 * capabilities of a Group 0 command, shift over to a 12934 * Group 1 command. We don't blindly use Group 1 12935 * commands because a) some drives (CDC Wren IVs) get a 12936 * bit confused, and b) there is probably a fair amount 12937 * of speed difference for a target to receive and decode 12938 * a 10 byte command instead of a 6 byte command. 12939 * 12940 * The xfer time difference of 6 vs 10 byte CDBs is 12941 * still significant so this code is still worthwhile. 12942 * 10 byte CDBs are very inefficient with the fas HBA driver 12943 * and older disks. Each CDB byte took 1 usec with some 12944 * popular disks. 12945 * 12946 * Context: Must be called at attach time 12947 */ 12948 12949 static void 12950 sd_init_cdb_limits(struct sd_lun *un) 12951 { 12952 /* 12953 * Use CDB_GROUP1 commands for most devices except for 12954 * parallel SCSI fixed drives in which case we get better 12955 * performance using CDB_GROUP0 commands (where applicable). 12956 */ 12957 un->un_mincdb = SD_CDB_GROUP1; 12958 #if !defined(__fibre) 12959 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 12960 !un->un_f_has_removable_media) { 12961 un->un_mincdb = SD_CDB_GROUP0; 12962 } 12963 #endif 12964 12965 /* 12966 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 12967 * commands for fixed disks unless we are building for a 32 bit 12968 * kernel. 12969 */ 12970 #ifdef _LP64 12971 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 12972 SD_CDB_GROUP4; 12973 #else 12974 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 12975 SD_CDB_GROUP1; 12976 #endif 12977 12978 /* 12979 * x86 systems require the PKT_DMA_PARTIAL flag 12980 */ 12981 #if defined(__x86) 12982 un->un_pkt_flags = PKT_DMA_PARTIAL; 12983 #else 12984 un->un_pkt_flags = 0; 12985 #endif 12986 12987 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 12988 ? sizeof (struct scsi_arq_status) : 1); 12989 un->un_cmd_timeout = (ushort_t)sd_io_time; 12990 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 12991 } 12992 12993 12994 /* 12995 * Function: sd_initpkt_for_buf 12996 * 12997 * Description: Allocate and initialize for transport a scsi_pkt struct, 12998 * based upon the info specified in the given buf struct. 12999 * 13000 * Assumes the xb_blkno in the request is absolute (ie, 13001 * relative to the start of the device (NOT partition!). 13002 * Also assumes that the request is using the native block 13003 * size of the device (as returned by the READ CAPACITY 13004 * command). 13005 * 13006 * Return Code: SD_PKT_ALLOC_SUCCESS 13007 * SD_PKT_ALLOC_FAILURE 13008 * SD_PKT_ALLOC_FAILURE_NO_DMA 13009 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13010 * 13011 * Context: Kernel thread and may be called from software interrupt context 13012 * as part of a sdrunout callback. This function may not block or 13013 * call routines that block 13014 */ 13015 13016 static int 13017 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13018 { 13019 struct sd_xbuf *xp; 13020 struct scsi_pkt *pktp = NULL; 13021 struct sd_lun *un; 13022 size_t blockcount; 13023 daddr_t startblock; 13024 int rval; 13025 int cmd_flags; 13026 13027 ASSERT(bp != NULL); 13028 ASSERT(pktpp != NULL); 13029 xp = SD_GET_XBUF(bp); 13030 ASSERT(xp != NULL); 13031 un = SD_GET_UN(bp); 13032 ASSERT(un != NULL); 13033 ASSERT(mutex_owned(SD_MUTEX(un))); 13034 ASSERT(bp->b_resid == 0); 13035 13036 SD_TRACE(SD_LOG_IO_CORE, un, 13037 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13038 13039 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13040 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13041 /* 13042 * Already have a scsi_pkt -- just need DMA resources. 13043 * We must recompute the CDB in case the mapping returns 13044 * a nonzero pkt_resid. 13045 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13046 * that is being retried, the unmap/remap of the DMA resouces 13047 * will result in the entire transfer starting over again 13048 * from the very first block. 13049 */ 13050 ASSERT(xp->xb_pktp != NULL); 13051 pktp = xp->xb_pktp; 13052 } else { 13053 pktp = NULL; 13054 } 13055 #endif /* __i386 || __amd64 */ 13056 13057 startblock = xp->xb_blkno; /* Absolute block num. */ 13058 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13059 13060 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13061 13062 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13063 13064 #else 13065 13066 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13067 13068 #endif 13069 13070 /* 13071 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13072 * call scsi_init_pkt, and build the CDB. 13073 */ 13074 rval = sd_setup_rw_pkt(un, &pktp, bp, 13075 cmd_flags, sdrunout, (caddr_t)un, 13076 startblock, blockcount); 13077 13078 if (rval == 0) { 13079 /* 13080 * Success. 13081 * 13082 * If partial DMA is being used and required for this transfer. 13083 * set it up here. 13084 */ 13085 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13086 (pktp->pkt_resid != 0)) { 13087 13088 /* 13089 * Save the CDB length and pkt_resid for the 13090 * next xfer 13091 */ 13092 xp->xb_dma_resid = pktp->pkt_resid; 13093 13094 /* rezero resid */ 13095 pktp->pkt_resid = 0; 13096 13097 } else { 13098 xp->xb_dma_resid = 0; 13099 } 13100 13101 pktp->pkt_flags = un->un_tagflags; 13102 pktp->pkt_time = un->un_cmd_timeout; 13103 pktp->pkt_comp = sdintr; 13104 13105 pktp->pkt_private = bp; 13106 *pktpp = pktp; 13107 13108 SD_TRACE(SD_LOG_IO_CORE, un, 13109 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13110 13111 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13112 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13113 #endif 13114 13115 return (SD_PKT_ALLOC_SUCCESS); 13116 13117 } 13118 13119 /* 13120 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13121 * from sd_setup_rw_pkt. 13122 */ 13123 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13124 13125 if (rval == SD_PKT_ALLOC_FAILURE) { 13126 *pktpp = NULL; 13127 /* 13128 * Set the driver state to RWAIT to indicate the driver 13129 * is waiting on resource allocations. The driver will not 13130 * suspend, pm_suspend, or detatch while the state is RWAIT. 13131 */ 13132 New_state(un, SD_STATE_RWAIT); 13133 13134 SD_ERROR(SD_LOG_IO_CORE, un, 13135 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13136 13137 if ((bp->b_flags & B_ERROR) != 0) { 13138 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13139 } 13140 return (SD_PKT_ALLOC_FAILURE); 13141 } else { 13142 /* 13143 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13144 * 13145 * This should never happen. Maybe someone messed with the 13146 * kernel's minphys? 13147 */ 13148 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13149 "Request rejected: too large for CDB: " 13150 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13151 SD_ERROR(SD_LOG_IO_CORE, un, 13152 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13153 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13154 13155 } 13156 } 13157 13158 13159 /* 13160 * Function: sd_destroypkt_for_buf 13161 * 13162 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13163 * 13164 * Context: Kernel thread or interrupt context 13165 */ 13166 13167 static void 13168 sd_destroypkt_for_buf(struct buf *bp) 13169 { 13170 ASSERT(bp != NULL); 13171 ASSERT(SD_GET_UN(bp) != NULL); 13172 13173 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13174 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13175 13176 ASSERT(SD_GET_PKTP(bp) != NULL); 13177 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13178 13179 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13180 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13181 } 13182 13183 /* 13184 * Function: sd_setup_rw_pkt 13185 * 13186 * Description: Determines appropriate CDB group for the requested LBA 13187 * and transfer length, calls scsi_init_pkt, and builds 13188 * the CDB. Do not use for partial DMA transfers except 13189 * for the initial transfer since the CDB size must 13190 * remain constant. 13191 * 13192 * Context: Kernel thread and may be called from software interrupt 13193 * context as part of a sdrunout callback. This function may not 13194 * block or call routines that block 13195 */ 13196 13197 13198 int 13199 sd_setup_rw_pkt(struct sd_lun *un, 13200 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13201 int (*callback)(caddr_t), caddr_t callback_arg, 13202 diskaddr_t lba, uint32_t blockcount) 13203 { 13204 struct scsi_pkt *return_pktp; 13205 union scsi_cdb *cdbp; 13206 struct sd_cdbinfo *cp = NULL; 13207 int i; 13208 13209 /* 13210 * See which size CDB to use, based upon the request. 13211 */ 13212 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13213 13214 /* 13215 * Check lba and block count against sd_cdbtab limits. 13216 * In the partial DMA case, we have to use the same size 13217 * CDB for all the transfers. Check lba + blockcount 13218 * against the max LBA so we know that segment of the 13219 * transfer can use the CDB we select. 13220 */ 13221 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13222 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13223 13224 /* 13225 * The command will fit into the CDB type 13226 * specified by sd_cdbtab[i]. 13227 */ 13228 cp = sd_cdbtab + i; 13229 13230 /* 13231 * Call scsi_init_pkt so we can fill in the 13232 * CDB. 13233 */ 13234 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13235 bp, cp->sc_grpcode, un->un_status_len, 0, 13236 flags, callback, callback_arg); 13237 13238 if (return_pktp != NULL) { 13239 13240 /* 13241 * Return new value of pkt 13242 */ 13243 *pktpp = return_pktp; 13244 13245 /* 13246 * To be safe, zero the CDB insuring there is 13247 * no leftover data from a previous command. 13248 */ 13249 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13250 13251 /* 13252 * Handle partial DMA mapping 13253 */ 13254 if (return_pktp->pkt_resid != 0) { 13255 13256 /* 13257 * Not going to xfer as many blocks as 13258 * originally expected 13259 */ 13260 blockcount -= 13261 SD_BYTES2TGTBLOCKS(un, 13262 return_pktp->pkt_resid); 13263 } 13264 13265 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13266 13267 /* 13268 * Set command byte based on the CDB 13269 * type we matched. 13270 */ 13271 cdbp->scc_cmd = cp->sc_grpmask | 13272 ((bp->b_flags & B_READ) ? 13273 SCMD_READ : SCMD_WRITE); 13274 13275 SD_FILL_SCSI1_LUN(un, return_pktp); 13276 13277 /* 13278 * Fill in LBA and length 13279 */ 13280 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13281 (cp->sc_grpcode == CDB_GROUP4) || 13282 (cp->sc_grpcode == CDB_GROUP0) || 13283 (cp->sc_grpcode == CDB_GROUP5)); 13284 13285 if (cp->sc_grpcode == CDB_GROUP1) { 13286 FORMG1ADDR(cdbp, lba); 13287 FORMG1COUNT(cdbp, blockcount); 13288 return (0); 13289 } else if (cp->sc_grpcode == CDB_GROUP4) { 13290 FORMG4LONGADDR(cdbp, lba); 13291 FORMG4COUNT(cdbp, blockcount); 13292 return (0); 13293 } else if (cp->sc_grpcode == CDB_GROUP0) { 13294 FORMG0ADDR(cdbp, lba); 13295 FORMG0COUNT(cdbp, blockcount); 13296 return (0); 13297 } else if (cp->sc_grpcode == CDB_GROUP5) { 13298 FORMG5ADDR(cdbp, lba); 13299 FORMG5COUNT(cdbp, blockcount); 13300 return (0); 13301 } 13302 13303 /* 13304 * It should be impossible to not match one 13305 * of the CDB types above, so we should never 13306 * reach this point. Set the CDB command byte 13307 * to test-unit-ready to avoid writing 13308 * to somewhere we don't intend. 13309 */ 13310 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13311 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13312 } else { 13313 /* 13314 * Couldn't get scsi_pkt 13315 */ 13316 return (SD_PKT_ALLOC_FAILURE); 13317 } 13318 } 13319 } 13320 13321 /* 13322 * None of the available CDB types were suitable. This really 13323 * should never happen: on a 64 bit system we support 13324 * READ16/WRITE16 which will hold an entire 64 bit disk address 13325 * and on a 32 bit system we will refuse to bind to a device 13326 * larger than 2TB so addresses will never be larger than 32 bits. 13327 */ 13328 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13329 } 13330 13331 #if defined(__i386) || defined(__amd64) 13332 /* 13333 * Function: sd_setup_next_rw_pkt 13334 * 13335 * Description: Setup packet for partial DMA transfers, except for the 13336 * initial transfer. sd_setup_rw_pkt should be used for 13337 * the initial transfer. 13338 * 13339 * Context: Kernel thread and may be called from interrupt context. 13340 */ 13341 13342 int 13343 sd_setup_next_rw_pkt(struct sd_lun *un, 13344 struct scsi_pkt *pktp, struct buf *bp, 13345 diskaddr_t lba, uint32_t blockcount) 13346 { 13347 uchar_t com; 13348 union scsi_cdb *cdbp; 13349 uchar_t cdb_group_id; 13350 13351 ASSERT(pktp != NULL); 13352 ASSERT(pktp->pkt_cdbp != NULL); 13353 13354 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13355 com = cdbp->scc_cmd; 13356 cdb_group_id = CDB_GROUPID(com); 13357 13358 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13359 (cdb_group_id == CDB_GROUPID_1) || 13360 (cdb_group_id == CDB_GROUPID_4) || 13361 (cdb_group_id == CDB_GROUPID_5)); 13362 13363 /* 13364 * Move pkt to the next portion of the xfer. 13365 * func is NULL_FUNC so we do not have to release 13366 * the disk mutex here. 13367 */ 13368 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13369 NULL_FUNC, NULL) == pktp) { 13370 /* Success. Handle partial DMA */ 13371 if (pktp->pkt_resid != 0) { 13372 blockcount -= 13373 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13374 } 13375 13376 cdbp->scc_cmd = com; 13377 SD_FILL_SCSI1_LUN(un, pktp); 13378 if (cdb_group_id == CDB_GROUPID_1) { 13379 FORMG1ADDR(cdbp, lba); 13380 FORMG1COUNT(cdbp, blockcount); 13381 return (0); 13382 } else if (cdb_group_id == CDB_GROUPID_4) { 13383 FORMG4LONGADDR(cdbp, lba); 13384 FORMG4COUNT(cdbp, blockcount); 13385 return (0); 13386 } else if (cdb_group_id == CDB_GROUPID_0) { 13387 FORMG0ADDR(cdbp, lba); 13388 FORMG0COUNT(cdbp, blockcount); 13389 return (0); 13390 } else if (cdb_group_id == CDB_GROUPID_5) { 13391 FORMG5ADDR(cdbp, lba); 13392 FORMG5COUNT(cdbp, blockcount); 13393 return (0); 13394 } 13395 13396 /* Unreachable */ 13397 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13398 } 13399 13400 /* 13401 * Error setting up next portion of cmd transfer. 13402 * Something is definitely very wrong and this 13403 * should not happen. 13404 */ 13405 return (SD_PKT_ALLOC_FAILURE); 13406 } 13407 #endif /* defined(__i386) || defined(__amd64) */ 13408 13409 /* 13410 * Function: sd_initpkt_for_uscsi 13411 * 13412 * Description: Allocate and initialize for transport a scsi_pkt struct, 13413 * based upon the info specified in the given uscsi_cmd struct. 13414 * 13415 * Return Code: SD_PKT_ALLOC_SUCCESS 13416 * SD_PKT_ALLOC_FAILURE 13417 * SD_PKT_ALLOC_FAILURE_NO_DMA 13418 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13419 * 13420 * Context: Kernel thread and may be called from software interrupt context 13421 * as part of a sdrunout callback. This function may not block or 13422 * call routines that block 13423 */ 13424 13425 static int 13426 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13427 { 13428 struct uscsi_cmd *uscmd; 13429 struct sd_xbuf *xp; 13430 struct scsi_pkt *pktp; 13431 struct sd_lun *un; 13432 uint32_t flags = 0; 13433 13434 ASSERT(bp != NULL); 13435 ASSERT(pktpp != NULL); 13436 xp = SD_GET_XBUF(bp); 13437 ASSERT(xp != NULL); 13438 un = SD_GET_UN(bp); 13439 ASSERT(un != NULL); 13440 ASSERT(mutex_owned(SD_MUTEX(un))); 13441 13442 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13443 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13444 ASSERT(uscmd != NULL); 13445 13446 SD_TRACE(SD_LOG_IO_CORE, un, 13447 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13448 13449 /* 13450 * Allocate the scsi_pkt for the command. 13451 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13452 * during scsi_init_pkt time and will continue to use the 13453 * same path as long as the same scsi_pkt is used without 13454 * intervening scsi_dma_free(). Since uscsi command does 13455 * not call scsi_dmafree() before retry failed command, it 13456 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13457 * set such that scsi_vhci can use other available path for 13458 * retry. Besides, ucsci command does not allow DMA breakup, 13459 * so there is no need to set PKT_DMA_PARTIAL flag. 13460 */ 13461 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13462 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13463 sizeof (struct scsi_arq_status), 0, 13464 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13465 sdrunout, (caddr_t)un); 13466 13467 if (pktp == NULL) { 13468 *pktpp = NULL; 13469 /* 13470 * Set the driver state to RWAIT to indicate the driver 13471 * is waiting on resource allocations. The driver will not 13472 * suspend, pm_suspend, or detatch while the state is RWAIT. 13473 */ 13474 New_state(un, SD_STATE_RWAIT); 13475 13476 SD_ERROR(SD_LOG_IO_CORE, un, 13477 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13478 13479 if ((bp->b_flags & B_ERROR) != 0) { 13480 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13481 } 13482 return (SD_PKT_ALLOC_FAILURE); 13483 } 13484 13485 /* 13486 * We do not do DMA breakup for USCSI commands, so return failure 13487 * here if all the needed DMA resources were not allocated. 13488 */ 13489 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13490 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13491 scsi_destroy_pkt(pktp); 13492 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13493 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13494 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13495 } 13496 13497 /* Init the cdb from the given uscsi struct */ 13498 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13499 uscmd->uscsi_cdb[0], 0, 0, 0); 13500 13501 SD_FILL_SCSI1_LUN(un, pktp); 13502 13503 /* 13504 * Set up the optional USCSI flags. See the uscsi (7I) man page 13505 * for listing of the supported flags. 13506 */ 13507 13508 if (uscmd->uscsi_flags & USCSI_SILENT) { 13509 flags |= FLAG_SILENT; 13510 } 13511 13512 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13513 flags |= FLAG_DIAGNOSE; 13514 } 13515 13516 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13517 flags |= FLAG_ISOLATE; 13518 } 13519 13520 if (un->un_f_is_fibre == FALSE) { 13521 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13522 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13523 } 13524 } 13525 13526 /* 13527 * Set the pkt flags here so we save time later. 13528 * Note: These flags are NOT in the uscsi man page!!! 13529 */ 13530 if (uscmd->uscsi_flags & USCSI_HEAD) { 13531 flags |= FLAG_HEAD; 13532 } 13533 13534 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13535 flags |= FLAG_NOINTR; 13536 } 13537 13538 /* 13539 * For tagged queueing, things get a bit complicated. 13540 * Check first for head of queue and last for ordered queue. 13541 * If neither head nor order, use the default driver tag flags. 13542 */ 13543 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13544 if (uscmd->uscsi_flags & USCSI_HTAG) { 13545 flags |= FLAG_HTAG; 13546 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13547 flags |= FLAG_OTAG; 13548 } else { 13549 flags |= un->un_tagflags & FLAG_TAGMASK; 13550 } 13551 } 13552 13553 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13554 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13555 } 13556 13557 pktp->pkt_flags = flags; 13558 13559 /* Copy the caller's CDB into the pkt... */ 13560 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13561 13562 if (uscmd->uscsi_timeout == 0) { 13563 pktp->pkt_time = un->un_uscsi_timeout; 13564 } else { 13565 pktp->pkt_time = uscmd->uscsi_timeout; 13566 } 13567 13568 /* need it later to identify USCSI request in sdintr */ 13569 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13570 13571 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13572 13573 pktp->pkt_private = bp; 13574 pktp->pkt_comp = sdintr; 13575 *pktpp = pktp; 13576 13577 SD_TRACE(SD_LOG_IO_CORE, un, 13578 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13579 13580 return (SD_PKT_ALLOC_SUCCESS); 13581 } 13582 13583 13584 /* 13585 * Function: sd_destroypkt_for_uscsi 13586 * 13587 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13588 * IOs.. Also saves relevant info into the associated uscsi_cmd 13589 * struct. 13590 * 13591 * Context: May be called under interrupt context 13592 */ 13593 13594 static void 13595 sd_destroypkt_for_uscsi(struct buf *bp) 13596 { 13597 struct uscsi_cmd *uscmd; 13598 struct sd_xbuf *xp; 13599 struct scsi_pkt *pktp; 13600 struct sd_lun *un; 13601 13602 ASSERT(bp != NULL); 13603 xp = SD_GET_XBUF(bp); 13604 ASSERT(xp != NULL); 13605 un = SD_GET_UN(bp); 13606 ASSERT(un != NULL); 13607 ASSERT(!mutex_owned(SD_MUTEX(un))); 13608 pktp = SD_GET_PKTP(bp); 13609 ASSERT(pktp != NULL); 13610 13611 SD_TRACE(SD_LOG_IO_CORE, un, 13612 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13613 13614 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13615 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13616 ASSERT(uscmd != NULL); 13617 13618 /* Save the status and the residual into the uscsi_cmd struct */ 13619 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13620 uscmd->uscsi_resid = bp->b_resid; 13621 13622 /* 13623 * If enabled, copy any saved sense data into the area specified 13624 * by the uscsi command. 13625 */ 13626 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13627 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13628 /* 13629 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13630 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13631 */ 13632 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13633 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13634 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13635 } 13636 13637 /* We are done with the scsi_pkt; free it now */ 13638 ASSERT(SD_GET_PKTP(bp) != NULL); 13639 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13640 13641 SD_TRACE(SD_LOG_IO_CORE, un, 13642 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13643 } 13644 13645 13646 /* 13647 * Function: sd_bioclone_alloc 13648 * 13649 * Description: Allocate a buf(9S) and init it as per the given buf 13650 * and the various arguments. The associated sd_xbuf 13651 * struct is (nearly) duplicated. The struct buf *bp 13652 * argument is saved in new_xp->xb_private. 13653 * 13654 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13655 * datalen - size of data area for the shadow bp 13656 * blkno - starting LBA 13657 * func - function pointer for b_iodone in the shadow buf. (May 13658 * be NULL if none.) 13659 * 13660 * Return Code: Pointer to allocates buf(9S) struct 13661 * 13662 * Context: Can sleep. 13663 */ 13664 13665 static struct buf * 13666 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13667 daddr_t blkno, int (*func)(struct buf *)) 13668 { 13669 struct sd_lun *un; 13670 struct sd_xbuf *xp; 13671 struct sd_xbuf *new_xp; 13672 struct buf *new_bp; 13673 13674 ASSERT(bp != NULL); 13675 xp = SD_GET_XBUF(bp); 13676 ASSERT(xp != NULL); 13677 un = SD_GET_UN(bp); 13678 ASSERT(un != NULL); 13679 ASSERT(!mutex_owned(SD_MUTEX(un))); 13680 13681 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13682 NULL, KM_SLEEP); 13683 13684 new_bp->b_lblkno = blkno; 13685 13686 /* 13687 * Allocate an xbuf for the shadow bp and copy the contents of the 13688 * original xbuf into it. 13689 */ 13690 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13691 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13692 13693 /* 13694 * The given bp is automatically saved in the xb_private member 13695 * of the new xbuf. Callers are allowed to depend on this. 13696 */ 13697 new_xp->xb_private = bp; 13698 13699 new_bp->b_private = new_xp; 13700 13701 return (new_bp); 13702 } 13703 13704 /* 13705 * Function: sd_shadow_buf_alloc 13706 * 13707 * Description: Allocate a buf(9S) and init it as per the given buf 13708 * and the various arguments. The associated sd_xbuf 13709 * struct is (nearly) duplicated. The struct buf *bp 13710 * argument is saved in new_xp->xb_private. 13711 * 13712 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13713 * datalen - size of data area for the shadow bp 13714 * bflags - B_READ or B_WRITE (pseudo flag) 13715 * blkno - starting LBA 13716 * func - function pointer for b_iodone in the shadow buf. (May 13717 * be NULL if none.) 13718 * 13719 * Return Code: Pointer to allocates buf(9S) struct 13720 * 13721 * Context: Can sleep. 13722 */ 13723 13724 static struct buf * 13725 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13726 daddr_t blkno, int (*func)(struct buf *)) 13727 { 13728 struct sd_lun *un; 13729 struct sd_xbuf *xp; 13730 struct sd_xbuf *new_xp; 13731 struct buf *new_bp; 13732 13733 ASSERT(bp != NULL); 13734 xp = SD_GET_XBUF(bp); 13735 ASSERT(xp != NULL); 13736 un = SD_GET_UN(bp); 13737 ASSERT(un != NULL); 13738 ASSERT(!mutex_owned(SD_MUTEX(un))); 13739 13740 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13741 bp_mapin(bp); 13742 } 13743 13744 bflags &= (B_READ | B_WRITE); 13745 #if defined(__i386) || defined(__amd64) 13746 new_bp = getrbuf(KM_SLEEP); 13747 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13748 new_bp->b_bcount = datalen; 13749 new_bp->b_flags = bp->b_flags | bflags; 13750 #else 13751 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13752 datalen, bflags, SLEEP_FUNC, NULL); 13753 #endif 13754 new_bp->av_forw = NULL; 13755 new_bp->av_back = NULL; 13756 new_bp->b_dev = bp->b_dev; 13757 new_bp->b_blkno = blkno; 13758 new_bp->b_iodone = func; 13759 new_bp->b_edev = bp->b_edev; 13760 new_bp->b_resid = 0; 13761 13762 /* We need to preserve the B_FAILFAST flag */ 13763 if (bp->b_flags & B_FAILFAST) { 13764 new_bp->b_flags |= B_FAILFAST; 13765 } 13766 13767 /* 13768 * Allocate an xbuf for the shadow bp and copy the contents of the 13769 * original xbuf into it. 13770 */ 13771 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13772 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13773 13774 /* Need later to copy data between the shadow buf & original buf! */ 13775 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13776 13777 /* 13778 * The given bp is automatically saved in the xb_private member 13779 * of the new xbuf. Callers are allowed to depend on this. 13780 */ 13781 new_xp->xb_private = bp; 13782 13783 new_bp->b_private = new_xp; 13784 13785 return (new_bp); 13786 } 13787 13788 /* 13789 * Function: sd_bioclone_free 13790 * 13791 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13792 * in the larger than partition operation. 13793 * 13794 * Context: May be called under interrupt context 13795 */ 13796 13797 static void 13798 sd_bioclone_free(struct buf *bp) 13799 { 13800 struct sd_xbuf *xp; 13801 13802 ASSERT(bp != NULL); 13803 xp = SD_GET_XBUF(bp); 13804 ASSERT(xp != NULL); 13805 13806 /* 13807 * Call bp_mapout() before freeing the buf, in case a lower 13808 * layer or HBA had done a bp_mapin(). we must do this here 13809 * as we are the "originator" of the shadow buf. 13810 */ 13811 bp_mapout(bp); 13812 13813 /* 13814 * Null out b_iodone before freeing the bp, to ensure that the driver 13815 * never gets confused by a stale value in this field. (Just a little 13816 * extra defensiveness here.) 13817 */ 13818 bp->b_iodone = NULL; 13819 13820 freerbuf(bp); 13821 13822 kmem_free(xp, sizeof (struct sd_xbuf)); 13823 } 13824 13825 /* 13826 * Function: sd_shadow_buf_free 13827 * 13828 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13829 * 13830 * Context: May be called under interrupt context 13831 */ 13832 13833 static void 13834 sd_shadow_buf_free(struct buf *bp) 13835 { 13836 struct sd_xbuf *xp; 13837 13838 ASSERT(bp != NULL); 13839 xp = SD_GET_XBUF(bp); 13840 ASSERT(xp != NULL); 13841 13842 #if defined(__sparc) 13843 /* 13844 * Call bp_mapout() before freeing the buf, in case a lower 13845 * layer or HBA had done a bp_mapin(). we must do this here 13846 * as we are the "originator" of the shadow buf. 13847 */ 13848 bp_mapout(bp); 13849 #endif 13850 13851 /* 13852 * Null out b_iodone before freeing the bp, to ensure that the driver 13853 * never gets confused by a stale value in this field. (Just a little 13854 * extra defensiveness here.) 13855 */ 13856 bp->b_iodone = NULL; 13857 13858 #if defined(__i386) || defined(__amd64) 13859 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13860 freerbuf(bp); 13861 #else 13862 scsi_free_consistent_buf(bp); 13863 #endif 13864 13865 kmem_free(xp, sizeof (struct sd_xbuf)); 13866 } 13867 13868 13869 /* 13870 * Function: sd_print_transport_rejected_message 13871 * 13872 * Description: This implements the ludicrously complex rules for printing 13873 * a "transport rejected" message. This is to address the 13874 * specific problem of having a flood of this error message 13875 * produced when a failover occurs. 13876 * 13877 * Context: Any. 13878 */ 13879 13880 static void 13881 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13882 int code) 13883 { 13884 ASSERT(un != NULL); 13885 ASSERT(mutex_owned(SD_MUTEX(un))); 13886 ASSERT(xp != NULL); 13887 13888 /* 13889 * Print the "transport rejected" message under the following 13890 * conditions: 13891 * 13892 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13893 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13894 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 13895 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 13896 * scsi_transport(9F) (which indicates that the target might have 13897 * gone off-line). This uses the un->un_tran_fatal_count 13898 * count, which is incremented whenever a TRAN_FATAL_ERROR is 13899 * received, and reset to zero whenver a TRAN_ACCEPT is returned 13900 * from scsi_transport(). 13901 * 13902 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 13903 * the preceeding cases in order for the message to be printed. 13904 */ 13905 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 13906 if ((sd_level_mask & SD_LOGMASK_DIAG) || 13907 (code != TRAN_FATAL_ERROR) || 13908 (un->un_tran_fatal_count == 1)) { 13909 switch (code) { 13910 case TRAN_BADPKT: 13911 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13912 "transport rejected bad packet\n"); 13913 break; 13914 case TRAN_FATAL_ERROR: 13915 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13916 "transport rejected fatal error\n"); 13917 break; 13918 default: 13919 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13920 "transport rejected (%d)\n", code); 13921 break; 13922 } 13923 } 13924 } 13925 } 13926 13927 13928 /* 13929 * Function: sd_add_buf_to_waitq 13930 * 13931 * Description: Add the given buf(9S) struct to the wait queue for the 13932 * instance. If sorting is enabled, then the buf is added 13933 * to the queue via an elevator sort algorithm (a la 13934 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 13935 * If sorting is not enabled, then the buf is just added 13936 * to the end of the wait queue. 13937 * 13938 * Return Code: void 13939 * 13940 * Context: Does not sleep/block, therefore technically can be called 13941 * from any context. However if sorting is enabled then the 13942 * execution time is indeterminate, and may take long if 13943 * the wait queue grows large. 13944 */ 13945 13946 static void 13947 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 13948 { 13949 struct buf *ap; 13950 13951 ASSERT(bp != NULL); 13952 ASSERT(un != NULL); 13953 ASSERT(mutex_owned(SD_MUTEX(un))); 13954 13955 /* If the queue is empty, add the buf as the only entry & return. */ 13956 if (un->un_waitq_headp == NULL) { 13957 ASSERT(un->un_waitq_tailp == NULL); 13958 un->un_waitq_headp = un->un_waitq_tailp = bp; 13959 bp->av_forw = NULL; 13960 return; 13961 } 13962 13963 ASSERT(un->un_waitq_tailp != NULL); 13964 13965 /* 13966 * If sorting is disabled, just add the buf to the tail end of 13967 * the wait queue and return. 13968 */ 13969 if (un->un_f_disksort_disabled) { 13970 un->un_waitq_tailp->av_forw = bp; 13971 un->un_waitq_tailp = bp; 13972 bp->av_forw = NULL; 13973 return; 13974 } 13975 13976 /* 13977 * Sort thru the list of requests currently on the wait queue 13978 * and add the new buf request at the appropriate position. 13979 * 13980 * The un->un_waitq_headp is an activity chain pointer on which 13981 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 13982 * first queue holds those requests which are positioned after 13983 * the current SD_GET_BLKNO() (in the first request); the second holds 13984 * requests which came in after their SD_GET_BLKNO() number was passed. 13985 * Thus we implement a one way scan, retracting after reaching 13986 * the end of the drive to the first request on the second 13987 * queue, at which time it becomes the first queue. 13988 * A one-way scan is natural because of the way UNIX read-ahead 13989 * blocks are allocated. 13990 * 13991 * If we lie after the first request, then we must locate the 13992 * second request list and add ourselves to it. 13993 */ 13994 ap = un->un_waitq_headp; 13995 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 13996 while (ap->av_forw != NULL) { 13997 /* 13998 * Look for an "inversion" in the (normally 13999 * ascending) block numbers. This indicates 14000 * the start of the second request list. 14001 */ 14002 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14003 /* 14004 * Search the second request list for the 14005 * first request at a larger block number. 14006 * We go before that; however if there is 14007 * no such request, we go at the end. 14008 */ 14009 do { 14010 if (SD_GET_BLKNO(bp) < 14011 SD_GET_BLKNO(ap->av_forw)) { 14012 goto insert; 14013 } 14014 ap = ap->av_forw; 14015 } while (ap->av_forw != NULL); 14016 goto insert; /* after last */ 14017 } 14018 ap = ap->av_forw; 14019 } 14020 14021 /* 14022 * No inversions... we will go after the last, and 14023 * be the first request in the second request list. 14024 */ 14025 goto insert; 14026 } 14027 14028 /* 14029 * Request is at/after the current request... 14030 * sort in the first request list. 14031 */ 14032 while (ap->av_forw != NULL) { 14033 /* 14034 * We want to go after the current request (1) if 14035 * there is an inversion after it (i.e. it is the end 14036 * of the first request list), or (2) if the next 14037 * request is a larger block no. than our request. 14038 */ 14039 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14040 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14041 goto insert; 14042 } 14043 ap = ap->av_forw; 14044 } 14045 14046 /* 14047 * Neither a second list nor a larger request, therefore 14048 * we go at the end of the first list (which is the same 14049 * as the end of the whole schebang). 14050 */ 14051 insert: 14052 bp->av_forw = ap->av_forw; 14053 ap->av_forw = bp; 14054 14055 /* 14056 * If we inserted onto the tail end of the waitq, make sure the 14057 * tail pointer is updated. 14058 */ 14059 if (ap == un->un_waitq_tailp) { 14060 un->un_waitq_tailp = bp; 14061 } 14062 } 14063 14064 14065 /* 14066 * Function: sd_start_cmds 14067 * 14068 * Description: Remove and transport cmds from the driver queues. 14069 * 14070 * Arguments: un - pointer to the unit (soft state) struct for the target. 14071 * 14072 * immed_bp - ptr to a buf to be transported immediately. Only 14073 * the immed_bp is transported; bufs on the waitq are not 14074 * processed and the un_retry_bp is not checked. If immed_bp is 14075 * NULL, then normal queue processing is performed. 14076 * 14077 * Context: May be called from kernel thread context, interrupt context, 14078 * or runout callback context. This function may not block or 14079 * call routines that block. 14080 */ 14081 14082 static void 14083 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14084 { 14085 struct sd_xbuf *xp; 14086 struct buf *bp; 14087 void (*statp)(kstat_io_t *); 14088 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14089 void (*saved_statp)(kstat_io_t *); 14090 #endif 14091 int rval; 14092 14093 ASSERT(un != NULL); 14094 ASSERT(mutex_owned(SD_MUTEX(un))); 14095 ASSERT(un->un_ncmds_in_transport >= 0); 14096 ASSERT(un->un_throttle >= 0); 14097 14098 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14099 14100 do { 14101 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14102 saved_statp = NULL; 14103 #endif 14104 14105 /* 14106 * If we are syncing or dumping, fail the command to 14107 * avoid recursively calling back into scsi_transport(). 14108 * The dump I/O itself uses a separate code path so this 14109 * only prevents non-dump I/O from being sent while dumping. 14110 * File system sync takes place before dumping begins. 14111 * During panic, filesystem I/O is allowed provided 14112 * un_in_callback is <= 1. This is to prevent recursion 14113 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14114 * sd_start_cmds and so on. See panic.c for more information 14115 * about the states the system can be in during panic. 14116 */ 14117 if ((un->un_state == SD_STATE_DUMPING) || 14118 (ddi_in_panic() && (un->un_in_callback > 1))) { 14119 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14120 "sd_start_cmds: panicking\n"); 14121 goto exit; 14122 } 14123 14124 if ((bp = immed_bp) != NULL) { 14125 /* 14126 * We have a bp that must be transported immediately. 14127 * It's OK to transport the immed_bp here without doing 14128 * the throttle limit check because the immed_bp is 14129 * always used in a retry/recovery case. This means 14130 * that we know we are not at the throttle limit by 14131 * virtue of the fact that to get here we must have 14132 * already gotten a command back via sdintr(). This also 14133 * relies on (1) the command on un_retry_bp preventing 14134 * further commands from the waitq from being issued; 14135 * and (2) the code in sd_retry_command checking the 14136 * throttle limit before issuing a delayed or immediate 14137 * retry. This holds even if the throttle limit is 14138 * currently ratcheted down from its maximum value. 14139 */ 14140 statp = kstat_runq_enter; 14141 if (bp == un->un_retry_bp) { 14142 ASSERT((un->un_retry_statp == NULL) || 14143 (un->un_retry_statp == kstat_waitq_enter) || 14144 (un->un_retry_statp == 14145 kstat_runq_back_to_waitq)); 14146 /* 14147 * If the waitq kstat was incremented when 14148 * sd_set_retry_bp() queued this bp for a retry, 14149 * then we must set up statp so that the waitq 14150 * count will get decremented correctly below. 14151 * Also we must clear un->un_retry_statp to 14152 * ensure that we do not act on a stale value 14153 * in this field. 14154 */ 14155 if ((un->un_retry_statp == kstat_waitq_enter) || 14156 (un->un_retry_statp == 14157 kstat_runq_back_to_waitq)) { 14158 statp = kstat_waitq_to_runq; 14159 } 14160 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14161 saved_statp = un->un_retry_statp; 14162 #endif 14163 un->un_retry_statp = NULL; 14164 14165 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14166 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14167 "un_throttle:%d un_ncmds_in_transport:%d\n", 14168 un, un->un_retry_bp, un->un_throttle, 14169 un->un_ncmds_in_transport); 14170 } else { 14171 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14172 "processing priority bp:0x%p\n", bp); 14173 } 14174 14175 } else if ((bp = un->un_waitq_headp) != NULL) { 14176 /* 14177 * A command on the waitq is ready to go, but do not 14178 * send it if: 14179 * 14180 * (1) the throttle limit has been reached, or 14181 * (2) a retry is pending, or 14182 * (3) a START_STOP_UNIT callback pending, or 14183 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14184 * command is pending. 14185 * 14186 * For all of these conditions, IO processing will 14187 * restart after the condition is cleared. 14188 */ 14189 if (un->un_ncmds_in_transport >= un->un_throttle) { 14190 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14191 "sd_start_cmds: exiting, " 14192 "throttle limit reached!\n"); 14193 goto exit; 14194 } 14195 if (un->un_retry_bp != NULL) { 14196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14197 "sd_start_cmds: exiting, retry pending!\n"); 14198 goto exit; 14199 } 14200 if (un->un_startstop_timeid != NULL) { 14201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14202 "sd_start_cmds: exiting, " 14203 "START_STOP pending!\n"); 14204 goto exit; 14205 } 14206 if (un->un_direct_priority_timeid != NULL) { 14207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14208 "sd_start_cmds: exiting, " 14209 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14210 goto exit; 14211 } 14212 14213 /* Dequeue the command */ 14214 un->un_waitq_headp = bp->av_forw; 14215 if (un->un_waitq_headp == NULL) { 14216 un->un_waitq_tailp = NULL; 14217 } 14218 bp->av_forw = NULL; 14219 statp = kstat_waitq_to_runq; 14220 SD_TRACE(SD_LOG_IO_CORE, un, 14221 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14222 14223 } else { 14224 /* No work to do so bail out now */ 14225 SD_TRACE(SD_LOG_IO_CORE, un, 14226 "sd_start_cmds: no more work, exiting!\n"); 14227 goto exit; 14228 } 14229 14230 /* 14231 * Reset the state to normal. This is the mechanism by which 14232 * the state transitions from either SD_STATE_RWAIT or 14233 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14234 * If state is SD_STATE_PM_CHANGING then this command is 14235 * part of the device power control and the state must 14236 * not be put back to normal. Doing so would would 14237 * allow new commands to proceed when they shouldn't, 14238 * the device may be going off. 14239 */ 14240 if ((un->un_state != SD_STATE_SUSPENDED) && 14241 (un->un_state != SD_STATE_PM_CHANGING)) { 14242 New_state(un, SD_STATE_NORMAL); 14243 } 14244 14245 xp = SD_GET_XBUF(bp); 14246 ASSERT(xp != NULL); 14247 14248 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14249 /* 14250 * Allocate the scsi_pkt if we need one, or attach DMA 14251 * resources if we have a scsi_pkt that needs them. The 14252 * latter should only occur for commands that are being 14253 * retried. 14254 */ 14255 if ((xp->xb_pktp == NULL) || 14256 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14257 #else 14258 if (xp->xb_pktp == NULL) { 14259 #endif 14260 /* 14261 * There is no scsi_pkt allocated for this buf. Call 14262 * the initpkt function to allocate & init one. 14263 * 14264 * The scsi_init_pkt runout callback functionality is 14265 * implemented as follows: 14266 * 14267 * 1) The initpkt function always calls 14268 * scsi_init_pkt(9F) with sdrunout specified as the 14269 * callback routine. 14270 * 2) A successful packet allocation is initialized and 14271 * the I/O is transported. 14272 * 3) The I/O associated with an allocation resource 14273 * failure is left on its queue to be retried via 14274 * runout or the next I/O. 14275 * 4) The I/O associated with a DMA error is removed 14276 * from the queue and failed with EIO. Processing of 14277 * the transport queues is also halted to be 14278 * restarted via runout or the next I/O. 14279 * 5) The I/O associated with a CDB size or packet 14280 * size error is removed from the queue and failed 14281 * with EIO. Processing of the transport queues is 14282 * continued. 14283 * 14284 * Note: there is no interface for canceling a runout 14285 * callback. To prevent the driver from detaching or 14286 * suspending while a runout is pending the driver 14287 * state is set to SD_STATE_RWAIT 14288 * 14289 * Note: using the scsi_init_pkt callback facility can 14290 * result in an I/O request persisting at the head of 14291 * the list which cannot be satisfied even after 14292 * multiple retries. In the future the driver may 14293 * implement some kind of maximum runout count before 14294 * failing an I/O. 14295 * 14296 * Note: the use of funcp below may seem superfluous, 14297 * but it helps warlock figure out the correct 14298 * initpkt function calls (see [s]sd.wlcmd). 14299 */ 14300 struct scsi_pkt *pktp; 14301 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14302 14303 ASSERT(bp != un->un_rqs_bp); 14304 14305 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14306 switch ((*funcp)(bp, &pktp)) { 14307 case SD_PKT_ALLOC_SUCCESS: 14308 xp->xb_pktp = pktp; 14309 SD_TRACE(SD_LOG_IO_CORE, un, 14310 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14311 pktp); 14312 goto got_pkt; 14313 14314 case SD_PKT_ALLOC_FAILURE: 14315 /* 14316 * Temporary (hopefully) resource depletion. 14317 * Since retries and RQS commands always have a 14318 * scsi_pkt allocated, these cases should never 14319 * get here. So the only cases this needs to 14320 * handle is a bp from the waitq (which we put 14321 * back onto the waitq for sdrunout), or a bp 14322 * sent as an immed_bp (which we just fail). 14323 */ 14324 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14325 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14326 14327 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14328 14329 if (bp == immed_bp) { 14330 /* 14331 * If SD_XB_DMA_FREED is clear, then 14332 * this is a failure to allocate a 14333 * scsi_pkt, and we must fail the 14334 * command. 14335 */ 14336 if ((xp->xb_pkt_flags & 14337 SD_XB_DMA_FREED) == 0) { 14338 break; 14339 } 14340 14341 /* 14342 * If this immediate command is NOT our 14343 * un_retry_bp, then we must fail it. 14344 */ 14345 if (bp != un->un_retry_bp) { 14346 break; 14347 } 14348 14349 /* 14350 * We get here if this cmd is our 14351 * un_retry_bp that was DMAFREED, but 14352 * scsi_init_pkt() failed to reallocate 14353 * DMA resources when we attempted to 14354 * retry it. This can happen when an 14355 * mpxio failover is in progress, but 14356 * we don't want to just fail the 14357 * command in this case. 14358 * 14359 * Use timeout(9F) to restart it after 14360 * a 100ms delay. We don't want to 14361 * let sdrunout() restart it, because 14362 * sdrunout() is just supposed to start 14363 * commands that are sitting on the 14364 * wait queue. The un_retry_bp stays 14365 * set until the command completes, but 14366 * sdrunout can be called many times 14367 * before that happens. Since sdrunout 14368 * cannot tell if the un_retry_bp is 14369 * already in the transport, it could 14370 * end up calling scsi_transport() for 14371 * the un_retry_bp multiple times. 14372 * 14373 * Also: don't schedule the callback 14374 * if some other callback is already 14375 * pending. 14376 */ 14377 if (un->un_retry_statp == NULL) { 14378 /* 14379 * restore the kstat pointer to 14380 * keep kstat counts coherent 14381 * when we do retry the command. 14382 */ 14383 un->un_retry_statp = 14384 saved_statp; 14385 } 14386 14387 if ((un->un_startstop_timeid == NULL) && 14388 (un->un_retry_timeid == NULL) && 14389 (un->un_direct_priority_timeid == 14390 NULL)) { 14391 14392 un->un_retry_timeid = 14393 timeout( 14394 sd_start_retry_command, 14395 un, SD_RESTART_TIMEOUT); 14396 } 14397 goto exit; 14398 } 14399 14400 #else 14401 if (bp == immed_bp) { 14402 break; /* Just fail the command */ 14403 } 14404 #endif 14405 14406 /* Add the buf back to the head of the waitq */ 14407 bp->av_forw = un->un_waitq_headp; 14408 un->un_waitq_headp = bp; 14409 if (un->un_waitq_tailp == NULL) { 14410 un->un_waitq_tailp = bp; 14411 } 14412 goto exit; 14413 14414 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14415 /* 14416 * HBA DMA resource failure. Fail the command 14417 * and continue processing of the queues. 14418 */ 14419 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14420 "sd_start_cmds: " 14421 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14422 break; 14423 14424 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14425 /* 14426 * Note:x86: Partial DMA mapping not supported 14427 * for USCSI commands, and all the needed DMA 14428 * resources were not allocated. 14429 */ 14430 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14431 "sd_start_cmds: " 14432 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14433 break; 14434 14435 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14436 /* 14437 * Note:x86: Request cannot fit into CDB based 14438 * on lba and len. 14439 */ 14440 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14441 "sd_start_cmds: " 14442 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14443 break; 14444 14445 default: 14446 /* Should NEVER get here! */ 14447 panic("scsi_initpkt error"); 14448 /*NOTREACHED*/ 14449 } 14450 14451 /* 14452 * Fatal error in allocating a scsi_pkt for this buf. 14453 * Update kstats & return the buf with an error code. 14454 * We must use sd_return_failed_command_no_restart() to 14455 * avoid a recursive call back into sd_start_cmds(). 14456 * However this also means that we must keep processing 14457 * the waitq here in order to avoid stalling. 14458 */ 14459 if (statp == kstat_waitq_to_runq) { 14460 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14461 } 14462 sd_return_failed_command_no_restart(un, bp, EIO); 14463 if (bp == immed_bp) { 14464 /* immed_bp is gone by now, so clear this */ 14465 immed_bp = NULL; 14466 } 14467 continue; 14468 } 14469 got_pkt: 14470 if (bp == immed_bp) { 14471 /* goto the head of the class.... */ 14472 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14473 } 14474 14475 un->un_ncmds_in_transport++; 14476 SD_UPDATE_KSTATS(un, statp, bp); 14477 14478 /* 14479 * Call scsi_transport() to send the command to the target. 14480 * According to SCSA architecture, we must drop the mutex here 14481 * before calling scsi_transport() in order to avoid deadlock. 14482 * Note that the scsi_pkt's completion routine can be executed 14483 * (from interrupt context) even before the call to 14484 * scsi_transport() returns. 14485 */ 14486 SD_TRACE(SD_LOG_IO_CORE, un, 14487 "sd_start_cmds: calling scsi_transport()\n"); 14488 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14489 14490 mutex_exit(SD_MUTEX(un)); 14491 rval = scsi_transport(xp->xb_pktp); 14492 mutex_enter(SD_MUTEX(un)); 14493 14494 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14495 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14496 14497 switch (rval) { 14498 case TRAN_ACCEPT: 14499 /* Clear this with every pkt accepted by the HBA */ 14500 un->un_tran_fatal_count = 0; 14501 break; /* Success; try the next cmd (if any) */ 14502 14503 case TRAN_BUSY: 14504 un->un_ncmds_in_transport--; 14505 ASSERT(un->un_ncmds_in_transport >= 0); 14506 14507 /* 14508 * Don't retry request sense, the sense data 14509 * is lost when another request is sent. 14510 * Free up the rqs buf and retry 14511 * the original failed cmd. Update kstat. 14512 */ 14513 if (bp == un->un_rqs_bp) { 14514 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14515 bp = sd_mark_rqs_idle(un, xp); 14516 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14517 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14518 kstat_waitq_enter); 14519 goto exit; 14520 } 14521 14522 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14523 /* 14524 * Free the DMA resources for the scsi_pkt. This will 14525 * allow mpxio to select another path the next time 14526 * we call scsi_transport() with this scsi_pkt. 14527 * See sdintr() for the rationalization behind this. 14528 */ 14529 if ((un->un_f_is_fibre == TRUE) && 14530 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14531 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14532 scsi_dmafree(xp->xb_pktp); 14533 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14534 } 14535 #endif 14536 14537 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14538 /* 14539 * Commands that are SD_PATH_DIRECT_PRIORITY 14540 * are for error recovery situations. These do 14541 * not use the normal command waitq, so if they 14542 * get a TRAN_BUSY we cannot put them back onto 14543 * the waitq for later retry. One possible 14544 * problem is that there could already be some 14545 * other command on un_retry_bp that is waiting 14546 * for this one to complete, so we would be 14547 * deadlocked if we put this command back onto 14548 * the waitq for later retry (since un_retry_bp 14549 * must complete before the driver gets back to 14550 * commands on the waitq). 14551 * 14552 * To avoid deadlock we must schedule a callback 14553 * that will restart this command after a set 14554 * interval. This should keep retrying for as 14555 * long as the underlying transport keeps 14556 * returning TRAN_BUSY (just like for other 14557 * commands). Use the same timeout interval as 14558 * for the ordinary TRAN_BUSY retry. 14559 */ 14560 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14561 "sd_start_cmds: scsi_transport() returned " 14562 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14563 14564 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14565 un->un_direct_priority_timeid = 14566 timeout(sd_start_direct_priority_command, 14567 bp, SD_BSY_TIMEOUT / 500); 14568 14569 goto exit; 14570 } 14571 14572 /* 14573 * For TRAN_BUSY, we want to reduce the throttle value, 14574 * unless we are retrying a command. 14575 */ 14576 if (bp != un->un_retry_bp) { 14577 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14578 } 14579 14580 /* 14581 * Set up the bp to be tried again 10 ms later. 14582 * Note:x86: Is there a timeout value in the sd_lun 14583 * for this condition? 14584 */ 14585 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14586 kstat_runq_back_to_waitq); 14587 goto exit; 14588 14589 case TRAN_FATAL_ERROR: 14590 un->un_tran_fatal_count++; 14591 /* FALLTHRU */ 14592 14593 case TRAN_BADPKT: 14594 default: 14595 un->un_ncmds_in_transport--; 14596 ASSERT(un->un_ncmds_in_transport >= 0); 14597 14598 /* 14599 * If this is our REQUEST SENSE command with a 14600 * transport error, we must get back the pointers 14601 * to the original buf, and mark the REQUEST 14602 * SENSE command as "available". 14603 */ 14604 if (bp == un->un_rqs_bp) { 14605 bp = sd_mark_rqs_idle(un, xp); 14606 xp = SD_GET_XBUF(bp); 14607 } else { 14608 /* 14609 * Legacy behavior: do not update transport 14610 * error count for request sense commands. 14611 */ 14612 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14613 } 14614 14615 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14616 sd_print_transport_rejected_message(un, xp, rval); 14617 14618 /* 14619 * We must use sd_return_failed_command_no_restart() to 14620 * avoid a recursive call back into sd_start_cmds(). 14621 * However this also means that we must keep processing 14622 * the waitq here in order to avoid stalling. 14623 */ 14624 sd_return_failed_command_no_restart(un, bp, EIO); 14625 14626 /* 14627 * Notify any threads waiting in sd_ddi_suspend() that 14628 * a command completion has occurred. 14629 */ 14630 if (un->un_state == SD_STATE_SUSPENDED) { 14631 cv_broadcast(&un->un_disk_busy_cv); 14632 } 14633 14634 if (bp == immed_bp) { 14635 /* immed_bp is gone by now, so clear this */ 14636 immed_bp = NULL; 14637 } 14638 break; 14639 } 14640 14641 } while (immed_bp == NULL); 14642 14643 exit: 14644 ASSERT(mutex_owned(SD_MUTEX(un))); 14645 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14646 } 14647 14648 14649 /* 14650 * Function: sd_return_command 14651 * 14652 * Description: Returns a command to its originator (with or without an 14653 * error). Also starts commands waiting to be transported 14654 * to the target. 14655 * 14656 * Context: May be called from interrupt, kernel, or timeout context 14657 */ 14658 14659 static void 14660 sd_return_command(struct sd_lun *un, struct buf *bp) 14661 { 14662 struct sd_xbuf *xp; 14663 #if defined(__i386) || defined(__amd64) 14664 struct scsi_pkt *pktp; 14665 #endif 14666 14667 ASSERT(bp != NULL); 14668 ASSERT(un != NULL); 14669 ASSERT(mutex_owned(SD_MUTEX(un))); 14670 ASSERT(bp != un->un_rqs_bp); 14671 xp = SD_GET_XBUF(bp); 14672 ASSERT(xp != NULL); 14673 14674 #if defined(__i386) || defined(__amd64) 14675 pktp = SD_GET_PKTP(bp); 14676 #endif 14677 14678 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14679 14680 #if defined(__i386) || defined(__amd64) 14681 /* 14682 * Note:x86: check for the "sdrestart failed" case. 14683 */ 14684 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14685 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14686 (xp->xb_pktp->pkt_resid == 0)) { 14687 14688 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14689 /* 14690 * Successfully set up next portion of cmd 14691 * transfer, try sending it 14692 */ 14693 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14694 NULL, NULL, 0, (clock_t)0, NULL); 14695 sd_start_cmds(un, NULL); 14696 return; /* Note:x86: need a return here? */ 14697 } 14698 } 14699 #endif 14700 14701 /* 14702 * If this is the failfast bp, clear it from un_failfast_bp. This 14703 * can happen if upon being re-tried the failfast bp either 14704 * succeeded or encountered another error (possibly even a different 14705 * error than the one that precipitated the failfast state, but in 14706 * that case it would have had to exhaust retries as well). Regardless, 14707 * this should not occur whenever the instance is in the active 14708 * failfast state. 14709 */ 14710 if (bp == un->un_failfast_bp) { 14711 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14712 un->un_failfast_bp = NULL; 14713 } 14714 14715 /* 14716 * Clear the failfast state upon successful completion of ANY cmd. 14717 */ 14718 if (bp->b_error == 0) { 14719 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14720 } 14721 14722 /* 14723 * This is used if the command was retried one or more times. Show that 14724 * we are done with it, and allow processing of the waitq to resume. 14725 */ 14726 if (bp == un->un_retry_bp) { 14727 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14728 "sd_return_command: un:0x%p: " 14729 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14730 un->un_retry_bp = NULL; 14731 un->un_retry_statp = NULL; 14732 } 14733 14734 SD_UPDATE_RDWR_STATS(un, bp); 14735 SD_UPDATE_PARTITION_STATS(un, bp); 14736 14737 switch (un->un_state) { 14738 case SD_STATE_SUSPENDED: 14739 /* 14740 * Notify any threads waiting in sd_ddi_suspend() that 14741 * a command completion has occurred. 14742 */ 14743 cv_broadcast(&un->un_disk_busy_cv); 14744 break; 14745 default: 14746 sd_start_cmds(un, NULL); 14747 break; 14748 } 14749 14750 /* Return this command up the iodone chain to its originator. */ 14751 mutex_exit(SD_MUTEX(un)); 14752 14753 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14754 xp->xb_pktp = NULL; 14755 14756 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14757 14758 ASSERT(!mutex_owned(SD_MUTEX(un))); 14759 mutex_enter(SD_MUTEX(un)); 14760 14761 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14762 } 14763 14764 14765 /* 14766 * Function: sd_return_failed_command 14767 * 14768 * Description: Command completion when an error occurred. 14769 * 14770 * Context: May be called from interrupt context 14771 */ 14772 14773 static void 14774 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14775 { 14776 ASSERT(bp != NULL); 14777 ASSERT(un != NULL); 14778 ASSERT(mutex_owned(SD_MUTEX(un))); 14779 14780 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14781 "sd_return_failed_command: entry\n"); 14782 14783 /* 14784 * b_resid could already be nonzero due to a partial data 14785 * transfer, so do not change it here. 14786 */ 14787 SD_BIOERROR(bp, errcode); 14788 14789 sd_return_command(un, bp); 14790 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14791 "sd_return_failed_command: exit\n"); 14792 } 14793 14794 14795 /* 14796 * Function: sd_return_failed_command_no_restart 14797 * 14798 * Description: Same as sd_return_failed_command, but ensures that no 14799 * call back into sd_start_cmds will be issued. 14800 * 14801 * Context: May be called from interrupt context 14802 */ 14803 14804 static void 14805 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14806 int errcode) 14807 { 14808 struct sd_xbuf *xp; 14809 14810 ASSERT(bp != NULL); 14811 ASSERT(un != NULL); 14812 ASSERT(mutex_owned(SD_MUTEX(un))); 14813 xp = SD_GET_XBUF(bp); 14814 ASSERT(xp != NULL); 14815 ASSERT(errcode != 0); 14816 14817 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14818 "sd_return_failed_command_no_restart: entry\n"); 14819 14820 /* 14821 * b_resid could already be nonzero due to a partial data 14822 * transfer, so do not change it here. 14823 */ 14824 SD_BIOERROR(bp, errcode); 14825 14826 /* 14827 * If this is the failfast bp, clear it. This can happen if the 14828 * failfast bp encounterd a fatal error when we attempted to 14829 * re-try it (such as a scsi_transport(9F) failure). However 14830 * we should NOT be in an active failfast state if the failfast 14831 * bp is not NULL. 14832 */ 14833 if (bp == un->un_failfast_bp) { 14834 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14835 un->un_failfast_bp = NULL; 14836 } 14837 14838 if (bp == un->un_retry_bp) { 14839 /* 14840 * This command was retried one or more times. Show that we are 14841 * done with it, and allow processing of the waitq to resume. 14842 */ 14843 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14844 "sd_return_failed_command_no_restart: " 14845 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14846 un->un_retry_bp = NULL; 14847 un->un_retry_statp = NULL; 14848 } 14849 14850 SD_UPDATE_RDWR_STATS(un, bp); 14851 SD_UPDATE_PARTITION_STATS(un, bp); 14852 14853 mutex_exit(SD_MUTEX(un)); 14854 14855 if (xp->xb_pktp != NULL) { 14856 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14857 xp->xb_pktp = NULL; 14858 } 14859 14860 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14861 14862 mutex_enter(SD_MUTEX(un)); 14863 14864 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14865 "sd_return_failed_command_no_restart: exit\n"); 14866 } 14867 14868 14869 /* 14870 * Function: sd_retry_command 14871 * 14872 * Description: queue up a command for retry, or (optionally) fail it 14873 * if retry counts are exhausted. 14874 * 14875 * Arguments: un - Pointer to the sd_lun struct for the target. 14876 * 14877 * bp - Pointer to the buf for the command to be retried. 14878 * 14879 * retry_check_flag - Flag to see which (if any) of the retry 14880 * counts should be decremented/checked. If the indicated 14881 * retry count is exhausted, then the command will not be 14882 * retried; it will be failed instead. This should use a 14883 * value equal to one of the following: 14884 * 14885 * SD_RETRIES_NOCHECK 14886 * SD_RESD_RETRIES_STANDARD 14887 * SD_RETRIES_VICTIM 14888 * 14889 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14890 * if the check should be made to see of FLAG_ISOLATE is set 14891 * in the pkt. If FLAG_ISOLATE is set, then the command is 14892 * not retried, it is simply failed. 14893 * 14894 * user_funcp - Ptr to function to call before dispatching the 14895 * command. May be NULL if no action needs to be performed. 14896 * (Primarily intended for printing messages.) 14897 * 14898 * user_arg - Optional argument to be passed along to 14899 * the user_funcp call. 14900 * 14901 * failure_code - errno return code to set in the bp if the 14902 * command is going to be failed. 14903 * 14904 * retry_delay - Retry delay interval in (clock_t) units. May 14905 * be zero which indicates that the retry should be retried 14906 * immediately (ie, without an intervening delay). 14907 * 14908 * statp - Ptr to kstat function to be updated if the command 14909 * is queued for a delayed retry. May be NULL if no kstat 14910 * update is desired. 14911 * 14912 * Context: May be called from interupt context. 14913 */ 14914 14915 static void 14916 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 14917 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 14918 code), void *user_arg, int failure_code, clock_t retry_delay, 14919 void (*statp)(kstat_io_t *)) 14920 { 14921 struct sd_xbuf *xp; 14922 struct scsi_pkt *pktp; 14923 14924 ASSERT(un != NULL); 14925 ASSERT(mutex_owned(SD_MUTEX(un))); 14926 ASSERT(bp != NULL); 14927 xp = SD_GET_XBUF(bp); 14928 ASSERT(xp != NULL); 14929 pktp = SD_GET_PKTP(bp); 14930 ASSERT(pktp != NULL); 14931 14932 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14933 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 14934 14935 /* 14936 * If we are syncing or dumping, fail the command to avoid 14937 * recursively calling back into scsi_transport(). 14938 */ 14939 if (ddi_in_panic()) { 14940 goto fail_command_no_log; 14941 } 14942 14943 /* 14944 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 14945 * log an error and fail the command. 14946 */ 14947 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14948 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 14949 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 14950 sd_dump_memory(un, SD_LOG_IO, "CDB", 14951 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 14952 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 14953 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 14954 goto fail_command; 14955 } 14956 14957 /* 14958 * If we are suspended, then put the command onto head of the 14959 * wait queue since we don't want to start more commands. 14960 */ 14961 switch (un->un_state) { 14962 case SD_STATE_SUSPENDED: 14963 case SD_STATE_DUMPING: 14964 bp->av_forw = un->un_waitq_headp; 14965 un->un_waitq_headp = bp; 14966 if (un->un_waitq_tailp == NULL) { 14967 un->un_waitq_tailp = bp; 14968 } 14969 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 14970 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 14971 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 14972 return; 14973 default: 14974 break; 14975 } 14976 14977 /* 14978 * If the caller wants us to check FLAG_ISOLATE, then see if that 14979 * is set; if it is then we do not want to retry the command. 14980 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 14981 */ 14982 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 14983 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 14984 goto fail_command; 14985 } 14986 } 14987 14988 14989 /* 14990 * If SD_RETRIES_FAILFAST is set, it indicates that either a 14991 * command timeout or a selection timeout has occurred. This means 14992 * that we were unable to establish an kind of communication with 14993 * the target, and subsequent retries and/or commands are likely 14994 * to encounter similar results and take a long time to complete. 14995 * 14996 * If this is a failfast error condition, we need to update the 14997 * failfast state, even if this bp does not have B_FAILFAST set. 14998 */ 14999 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15000 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15001 ASSERT(un->un_failfast_bp == NULL); 15002 /* 15003 * If we are already in the active failfast state, and 15004 * another failfast error condition has been detected, 15005 * then fail this command if it has B_FAILFAST set. 15006 * If B_FAILFAST is clear, then maintain the legacy 15007 * behavior of retrying heroically, even tho this will 15008 * take a lot more time to fail the command. 15009 */ 15010 if (bp->b_flags & B_FAILFAST) { 15011 goto fail_command; 15012 } 15013 } else { 15014 /* 15015 * We're not in the active failfast state, but we 15016 * have a failfast error condition, so we must begin 15017 * transition to the next state. We do this regardless 15018 * of whether or not this bp has B_FAILFAST set. 15019 */ 15020 if (un->un_failfast_bp == NULL) { 15021 /* 15022 * This is the first bp to meet a failfast 15023 * condition so save it on un_failfast_bp & 15024 * do normal retry processing. Do not enter 15025 * active failfast state yet. This marks 15026 * entry into the "failfast pending" state. 15027 */ 15028 un->un_failfast_bp = bp; 15029 15030 } else if (un->un_failfast_bp == bp) { 15031 /* 15032 * This is the second time *this* bp has 15033 * encountered a failfast error condition, 15034 * so enter active failfast state & flush 15035 * queues as appropriate. 15036 */ 15037 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15038 un->un_failfast_bp = NULL; 15039 sd_failfast_flushq(un); 15040 15041 /* 15042 * Fail this bp now if B_FAILFAST set; 15043 * otherwise continue with retries. (It would 15044 * be pretty ironic if this bp succeeded on a 15045 * subsequent retry after we just flushed all 15046 * the queues). 15047 */ 15048 if (bp->b_flags & B_FAILFAST) { 15049 goto fail_command; 15050 } 15051 15052 #if !defined(lint) && !defined(__lint) 15053 } else { 15054 /* 15055 * If neither of the preceeding conditionals 15056 * was true, it means that there is some 15057 * *other* bp that has met an inital failfast 15058 * condition and is currently either being 15059 * retried or is waiting to be retried. In 15060 * that case we should perform normal retry 15061 * processing on *this* bp, since there is a 15062 * chance that the current failfast condition 15063 * is transient and recoverable. If that does 15064 * not turn out to be the case, then retries 15065 * will be cleared when the wait queue is 15066 * flushed anyway. 15067 */ 15068 #endif 15069 } 15070 } 15071 } else { 15072 /* 15073 * SD_RETRIES_FAILFAST is clear, which indicates that we 15074 * likely were able to at least establish some level of 15075 * communication with the target and subsequent commands 15076 * and/or retries are likely to get through to the target, 15077 * In this case we want to be aggressive about clearing 15078 * the failfast state. Note that this does not affect 15079 * the "failfast pending" condition. 15080 */ 15081 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15082 } 15083 15084 15085 /* 15086 * Check the specified retry count to see if we can still do 15087 * any retries with this pkt before we should fail it. 15088 */ 15089 switch (retry_check_flag & SD_RETRIES_MASK) { 15090 case SD_RETRIES_VICTIM: 15091 /* 15092 * Check the victim retry count. If exhausted, then fall 15093 * thru & check against the standard retry count. 15094 */ 15095 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15096 /* Increment count & proceed with the retry */ 15097 xp->xb_victim_retry_count++; 15098 break; 15099 } 15100 /* Victim retries exhausted, fall back to std. retries... */ 15101 /* FALLTHRU */ 15102 15103 case SD_RETRIES_STANDARD: 15104 if (xp->xb_retry_count >= un->un_retry_count) { 15105 /* Retries exhausted, fail the command */ 15106 SD_TRACE(SD_LOG_IO_CORE, un, 15107 "sd_retry_command: retries exhausted!\n"); 15108 /* 15109 * update b_resid for failed SCMD_READ & SCMD_WRITE 15110 * commands with nonzero pkt_resid. 15111 */ 15112 if ((pktp->pkt_reason == CMD_CMPLT) && 15113 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15114 (pktp->pkt_resid != 0)) { 15115 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15116 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15117 SD_UPDATE_B_RESID(bp, pktp); 15118 } 15119 } 15120 goto fail_command; 15121 } 15122 xp->xb_retry_count++; 15123 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15124 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15125 break; 15126 15127 case SD_RETRIES_UA: 15128 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15129 /* Retries exhausted, fail the command */ 15130 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15131 "Unit Attention retries exhausted. " 15132 "Check the target.\n"); 15133 goto fail_command; 15134 } 15135 xp->xb_ua_retry_count++; 15136 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15137 "sd_retry_command: retry count:%d\n", 15138 xp->xb_ua_retry_count); 15139 break; 15140 15141 case SD_RETRIES_BUSY: 15142 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15143 /* Retries exhausted, fail the command */ 15144 SD_TRACE(SD_LOG_IO_CORE, un, 15145 "sd_retry_command: retries exhausted!\n"); 15146 goto fail_command; 15147 } 15148 xp->xb_retry_count++; 15149 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15150 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15151 break; 15152 15153 case SD_RETRIES_NOCHECK: 15154 default: 15155 /* No retry count to check. Just proceed with the retry */ 15156 break; 15157 } 15158 15159 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15160 15161 /* 15162 * If we were given a zero timeout, we must attempt to retry the 15163 * command immediately (ie, without a delay). 15164 */ 15165 if (retry_delay == 0) { 15166 /* 15167 * Check some limiting conditions to see if we can actually 15168 * do the immediate retry. If we cannot, then we must 15169 * fall back to queueing up a delayed retry. 15170 */ 15171 if (un->un_ncmds_in_transport >= un->un_throttle) { 15172 /* 15173 * We are at the throttle limit for the target, 15174 * fall back to delayed retry. 15175 */ 15176 retry_delay = SD_BSY_TIMEOUT; 15177 statp = kstat_waitq_enter; 15178 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15179 "sd_retry_command: immed. retry hit " 15180 "throttle!\n"); 15181 } else { 15182 /* 15183 * We're clear to proceed with the immediate retry. 15184 * First call the user-provided function (if any) 15185 */ 15186 if (user_funcp != NULL) { 15187 (*user_funcp)(un, bp, user_arg, 15188 SD_IMMEDIATE_RETRY_ISSUED); 15189 #ifdef __lock_lint 15190 sd_print_incomplete_msg(un, bp, user_arg, 15191 SD_IMMEDIATE_RETRY_ISSUED); 15192 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15193 SD_IMMEDIATE_RETRY_ISSUED); 15194 sd_print_sense_failed_msg(un, bp, user_arg, 15195 SD_IMMEDIATE_RETRY_ISSUED); 15196 #endif 15197 } 15198 15199 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15200 "sd_retry_command: issuing immediate retry\n"); 15201 15202 /* 15203 * Call sd_start_cmds() to transport the command to 15204 * the target. 15205 */ 15206 sd_start_cmds(un, bp); 15207 15208 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15209 "sd_retry_command exit\n"); 15210 return; 15211 } 15212 } 15213 15214 /* 15215 * Set up to retry the command after a delay. 15216 * First call the user-provided function (if any) 15217 */ 15218 if (user_funcp != NULL) { 15219 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15220 } 15221 15222 sd_set_retry_bp(un, bp, retry_delay, statp); 15223 15224 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15225 return; 15226 15227 fail_command: 15228 15229 if (user_funcp != NULL) { 15230 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15231 } 15232 15233 fail_command_no_log: 15234 15235 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15236 "sd_retry_command: returning failed command\n"); 15237 15238 sd_return_failed_command(un, bp, failure_code); 15239 15240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15241 } 15242 15243 15244 /* 15245 * Function: sd_set_retry_bp 15246 * 15247 * Description: Set up the given bp for retry. 15248 * 15249 * Arguments: un - ptr to associated softstate 15250 * bp - ptr to buf(9S) for the command 15251 * retry_delay - time interval before issuing retry (may be 0) 15252 * statp - optional pointer to kstat function 15253 * 15254 * Context: May be called under interrupt context 15255 */ 15256 15257 static void 15258 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15259 void (*statp)(kstat_io_t *)) 15260 { 15261 ASSERT(un != NULL); 15262 ASSERT(mutex_owned(SD_MUTEX(un))); 15263 ASSERT(bp != NULL); 15264 15265 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15266 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15267 15268 /* 15269 * Indicate that the command is being retried. This will not allow any 15270 * other commands on the wait queue to be transported to the target 15271 * until this command has been completed (success or failure). The 15272 * "retry command" is not transported to the target until the given 15273 * time delay expires, unless the user specified a 0 retry_delay. 15274 * 15275 * Note: the timeout(9F) callback routine is what actually calls 15276 * sd_start_cmds() to transport the command, with the exception of a 15277 * zero retry_delay. The only current implementor of a zero retry delay 15278 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15279 */ 15280 if (un->un_retry_bp == NULL) { 15281 ASSERT(un->un_retry_statp == NULL); 15282 un->un_retry_bp = bp; 15283 15284 /* 15285 * If the user has not specified a delay the command should 15286 * be queued and no timeout should be scheduled. 15287 */ 15288 if (retry_delay == 0) { 15289 /* 15290 * Save the kstat pointer that will be used in the 15291 * call to SD_UPDATE_KSTATS() below, so that 15292 * sd_start_cmds() can correctly decrement the waitq 15293 * count when it is time to transport this command. 15294 */ 15295 un->un_retry_statp = statp; 15296 goto done; 15297 } 15298 } 15299 15300 if (un->un_retry_bp == bp) { 15301 /* 15302 * Save the kstat pointer that will be used in the call to 15303 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15304 * correctly decrement the waitq count when it is time to 15305 * transport this command. 15306 */ 15307 un->un_retry_statp = statp; 15308 15309 /* 15310 * Schedule a timeout if: 15311 * 1) The user has specified a delay. 15312 * 2) There is not a START_STOP_UNIT callback pending. 15313 * 15314 * If no delay has been specified, then it is up to the caller 15315 * to ensure that IO processing continues without stalling. 15316 * Effectively, this means that the caller will issue the 15317 * required call to sd_start_cmds(). The START_STOP_UNIT 15318 * callback does this after the START STOP UNIT command has 15319 * completed. In either of these cases we should not schedule 15320 * a timeout callback here. Also don't schedule the timeout if 15321 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15322 */ 15323 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15324 (un->un_direct_priority_timeid == NULL)) { 15325 un->un_retry_timeid = 15326 timeout(sd_start_retry_command, un, retry_delay); 15327 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15328 "sd_set_retry_bp: setting timeout: un: 0x%p" 15329 " bp:0x%p un_retry_timeid:0x%p\n", 15330 un, bp, un->un_retry_timeid); 15331 } 15332 } else { 15333 /* 15334 * We only get in here if there is already another command 15335 * waiting to be retried. In this case, we just put the 15336 * given command onto the wait queue, so it can be transported 15337 * after the current retry command has completed. 15338 * 15339 * Also we have to make sure that if the command at the head 15340 * of the wait queue is the un_failfast_bp, that we do not 15341 * put ahead of it any other commands that are to be retried. 15342 */ 15343 if ((un->un_failfast_bp != NULL) && 15344 (un->un_failfast_bp == un->un_waitq_headp)) { 15345 /* 15346 * Enqueue this command AFTER the first command on 15347 * the wait queue (which is also un_failfast_bp). 15348 */ 15349 bp->av_forw = un->un_waitq_headp->av_forw; 15350 un->un_waitq_headp->av_forw = bp; 15351 if (un->un_waitq_headp == un->un_waitq_tailp) { 15352 un->un_waitq_tailp = bp; 15353 } 15354 } else { 15355 /* Enqueue this command at the head of the waitq. */ 15356 bp->av_forw = un->un_waitq_headp; 15357 un->un_waitq_headp = bp; 15358 if (un->un_waitq_tailp == NULL) { 15359 un->un_waitq_tailp = bp; 15360 } 15361 } 15362 15363 if (statp == NULL) { 15364 statp = kstat_waitq_enter; 15365 } 15366 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15367 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15368 } 15369 15370 done: 15371 if (statp != NULL) { 15372 SD_UPDATE_KSTATS(un, statp, bp); 15373 } 15374 15375 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15376 "sd_set_retry_bp: exit un:0x%p\n", un); 15377 } 15378 15379 15380 /* 15381 * Function: sd_start_retry_command 15382 * 15383 * Description: Start the command that has been waiting on the target's 15384 * retry queue. Called from timeout(9F) context after the 15385 * retry delay interval has expired. 15386 * 15387 * Arguments: arg - pointer to associated softstate for the device. 15388 * 15389 * Context: timeout(9F) thread context. May not sleep. 15390 */ 15391 15392 static void 15393 sd_start_retry_command(void *arg) 15394 { 15395 struct sd_lun *un = arg; 15396 15397 ASSERT(un != NULL); 15398 ASSERT(!mutex_owned(SD_MUTEX(un))); 15399 15400 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15401 "sd_start_retry_command: entry\n"); 15402 15403 mutex_enter(SD_MUTEX(un)); 15404 15405 un->un_retry_timeid = NULL; 15406 15407 if (un->un_retry_bp != NULL) { 15408 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15409 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15410 un, un->un_retry_bp); 15411 sd_start_cmds(un, un->un_retry_bp); 15412 } 15413 15414 mutex_exit(SD_MUTEX(un)); 15415 15416 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15417 "sd_start_retry_command: exit\n"); 15418 } 15419 15420 15421 /* 15422 * Function: sd_start_direct_priority_command 15423 * 15424 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15425 * received TRAN_BUSY when we called scsi_transport() to send it 15426 * to the underlying HBA. This function is called from timeout(9F) 15427 * context after the delay interval has expired. 15428 * 15429 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15430 * 15431 * Context: timeout(9F) thread context. May not sleep. 15432 */ 15433 15434 static void 15435 sd_start_direct_priority_command(void *arg) 15436 { 15437 struct buf *priority_bp = arg; 15438 struct sd_lun *un; 15439 15440 ASSERT(priority_bp != NULL); 15441 un = SD_GET_UN(priority_bp); 15442 ASSERT(un != NULL); 15443 ASSERT(!mutex_owned(SD_MUTEX(un))); 15444 15445 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15446 "sd_start_direct_priority_command: entry\n"); 15447 15448 mutex_enter(SD_MUTEX(un)); 15449 un->un_direct_priority_timeid = NULL; 15450 sd_start_cmds(un, priority_bp); 15451 mutex_exit(SD_MUTEX(un)); 15452 15453 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15454 "sd_start_direct_priority_command: exit\n"); 15455 } 15456 15457 15458 /* 15459 * Function: sd_send_request_sense_command 15460 * 15461 * Description: Sends a REQUEST SENSE command to the target 15462 * 15463 * Context: May be called from interrupt context. 15464 */ 15465 15466 static void 15467 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15468 struct scsi_pkt *pktp) 15469 { 15470 ASSERT(bp != NULL); 15471 ASSERT(un != NULL); 15472 ASSERT(mutex_owned(SD_MUTEX(un))); 15473 15474 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15475 "entry: buf:0x%p\n", bp); 15476 15477 /* 15478 * If we are syncing or dumping, then fail the command to avoid a 15479 * recursive callback into scsi_transport(). Also fail the command 15480 * if we are suspended (legacy behavior). 15481 */ 15482 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15483 (un->un_state == SD_STATE_DUMPING)) { 15484 sd_return_failed_command(un, bp, EIO); 15485 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15486 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15487 return; 15488 } 15489 15490 /* 15491 * Retry the failed command and don't issue the request sense if: 15492 * 1) the sense buf is busy 15493 * 2) we have 1 or more outstanding commands on the target 15494 * (the sense data will be cleared or invalidated any way) 15495 * 15496 * Note: There could be an issue with not checking a retry limit here, 15497 * the problem is determining which retry limit to check. 15498 */ 15499 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15500 /* Don't retry if the command is flagged as non-retryable */ 15501 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15502 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15503 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15504 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15505 "sd_send_request_sense_command: " 15506 "at full throttle, retrying exit\n"); 15507 } else { 15508 sd_return_failed_command(un, bp, EIO); 15509 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15510 "sd_send_request_sense_command: " 15511 "at full throttle, non-retryable exit\n"); 15512 } 15513 return; 15514 } 15515 15516 sd_mark_rqs_busy(un, bp); 15517 sd_start_cmds(un, un->un_rqs_bp); 15518 15519 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15520 "sd_send_request_sense_command: exit\n"); 15521 } 15522 15523 15524 /* 15525 * Function: sd_mark_rqs_busy 15526 * 15527 * Description: Indicate that the request sense bp for this instance is 15528 * in use. 15529 * 15530 * Context: May be called under interrupt context 15531 */ 15532 15533 static void 15534 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15535 { 15536 struct sd_xbuf *sense_xp; 15537 15538 ASSERT(un != NULL); 15539 ASSERT(bp != NULL); 15540 ASSERT(mutex_owned(SD_MUTEX(un))); 15541 ASSERT(un->un_sense_isbusy == 0); 15542 15543 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15544 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15545 15546 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15547 ASSERT(sense_xp != NULL); 15548 15549 SD_INFO(SD_LOG_IO, un, 15550 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15551 15552 ASSERT(sense_xp->xb_pktp != NULL); 15553 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15554 == (FLAG_SENSING | FLAG_HEAD)); 15555 15556 un->un_sense_isbusy = 1; 15557 un->un_rqs_bp->b_resid = 0; 15558 sense_xp->xb_pktp->pkt_resid = 0; 15559 sense_xp->xb_pktp->pkt_reason = 0; 15560 15561 /* So we can get back the bp at interrupt time! */ 15562 sense_xp->xb_sense_bp = bp; 15563 15564 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15565 15566 /* 15567 * Mark this buf as awaiting sense data. (This is already set in 15568 * the pkt_flags for the RQS packet.) 15569 */ 15570 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15571 15572 sense_xp->xb_retry_count = 0; 15573 sense_xp->xb_victim_retry_count = 0; 15574 sense_xp->xb_ua_retry_count = 0; 15575 sense_xp->xb_dma_resid = 0; 15576 15577 /* Clean up the fields for auto-request sense */ 15578 sense_xp->xb_sense_status = 0; 15579 sense_xp->xb_sense_state = 0; 15580 sense_xp->xb_sense_resid = 0; 15581 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15582 15583 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15584 } 15585 15586 15587 /* 15588 * Function: sd_mark_rqs_idle 15589 * 15590 * Description: SD_MUTEX must be held continuously through this routine 15591 * to prevent reuse of the rqs struct before the caller can 15592 * complete it's processing. 15593 * 15594 * Return Code: Pointer to the RQS buf 15595 * 15596 * Context: May be called under interrupt context 15597 */ 15598 15599 static struct buf * 15600 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15601 { 15602 struct buf *bp; 15603 ASSERT(un != NULL); 15604 ASSERT(sense_xp != NULL); 15605 ASSERT(mutex_owned(SD_MUTEX(un))); 15606 ASSERT(un->un_sense_isbusy != 0); 15607 15608 un->un_sense_isbusy = 0; 15609 bp = sense_xp->xb_sense_bp; 15610 sense_xp->xb_sense_bp = NULL; 15611 15612 /* This pkt is no longer interested in getting sense data */ 15613 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15614 15615 return (bp); 15616 } 15617 15618 15619 15620 /* 15621 * Function: sd_alloc_rqs 15622 * 15623 * Description: Set up the unit to receive auto request sense data 15624 * 15625 * Return Code: DDI_SUCCESS or DDI_FAILURE 15626 * 15627 * Context: Called under attach(9E) context 15628 */ 15629 15630 static int 15631 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15632 { 15633 struct sd_xbuf *xp; 15634 15635 ASSERT(un != NULL); 15636 ASSERT(!mutex_owned(SD_MUTEX(un))); 15637 ASSERT(un->un_rqs_bp == NULL); 15638 ASSERT(un->un_rqs_pktp == NULL); 15639 15640 /* 15641 * First allocate the required buf and scsi_pkt structs, then set up 15642 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15643 */ 15644 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15645 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15646 if (un->un_rqs_bp == NULL) { 15647 return (DDI_FAILURE); 15648 } 15649 15650 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15651 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15652 15653 if (un->un_rqs_pktp == NULL) { 15654 sd_free_rqs(un); 15655 return (DDI_FAILURE); 15656 } 15657 15658 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15659 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15660 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15661 15662 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15663 15664 /* Set up the other needed members in the ARQ scsi_pkt. */ 15665 un->un_rqs_pktp->pkt_comp = sdintr; 15666 un->un_rqs_pktp->pkt_time = sd_io_time; 15667 un->un_rqs_pktp->pkt_flags |= 15668 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15669 15670 /* 15671 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15672 * provide any intpkt, destroypkt routines as we take care of 15673 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15674 */ 15675 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15676 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15677 xp->xb_pktp = un->un_rqs_pktp; 15678 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15679 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15680 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15681 15682 /* 15683 * Save the pointer to the request sense private bp so it can 15684 * be retrieved in sdintr. 15685 */ 15686 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15687 ASSERT(un->un_rqs_bp->b_private == xp); 15688 15689 /* 15690 * See if the HBA supports auto-request sense for the specified 15691 * target/lun. If it does, then try to enable it (if not already 15692 * enabled). 15693 * 15694 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15695 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15696 * return success. However, in both of these cases ARQ is always 15697 * enabled and scsi_ifgetcap will always return true. The best approach 15698 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15699 * 15700 * The 3rd case is the HBA (adp) always return enabled on 15701 * scsi_ifgetgetcap even when it's not enable, the best approach 15702 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15703 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15704 */ 15705 15706 if (un->un_f_is_fibre == TRUE) { 15707 un->un_f_arq_enabled = TRUE; 15708 } else { 15709 #if defined(__i386) || defined(__amd64) 15710 /* 15711 * Circumvent the Adaptec bug, remove this code when 15712 * the bug is fixed 15713 */ 15714 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15715 #endif 15716 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15717 case 0: 15718 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15719 "sd_alloc_rqs: HBA supports ARQ\n"); 15720 /* 15721 * ARQ is supported by this HBA but currently is not 15722 * enabled. Attempt to enable it and if successful then 15723 * mark this instance as ARQ enabled. 15724 */ 15725 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15726 == 1) { 15727 /* Successfully enabled ARQ in the HBA */ 15728 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15729 "sd_alloc_rqs: ARQ enabled\n"); 15730 un->un_f_arq_enabled = TRUE; 15731 } else { 15732 /* Could not enable ARQ in the HBA */ 15733 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15734 "sd_alloc_rqs: failed ARQ enable\n"); 15735 un->un_f_arq_enabled = FALSE; 15736 } 15737 break; 15738 case 1: 15739 /* 15740 * ARQ is supported by this HBA and is already enabled. 15741 * Just mark ARQ as enabled for this instance. 15742 */ 15743 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15744 "sd_alloc_rqs: ARQ already enabled\n"); 15745 un->un_f_arq_enabled = TRUE; 15746 break; 15747 default: 15748 /* 15749 * ARQ is not supported by this HBA; disable it for this 15750 * instance. 15751 */ 15752 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15753 "sd_alloc_rqs: HBA does not support ARQ\n"); 15754 un->un_f_arq_enabled = FALSE; 15755 break; 15756 } 15757 } 15758 15759 return (DDI_SUCCESS); 15760 } 15761 15762 15763 /* 15764 * Function: sd_free_rqs 15765 * 15766 * Description: Cleanup for the pre-instance RQS command. 15767 * 15768 * Context: Kernel thread context 15769 */ 15770 15771 static void 15772 sd_free_rqs(struct sd_lun *un) 15773 { 15774 ASSERT(un != NULL); 15775 15776 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15777 15778 /* 15779 * If consistent memory is bound to a scsi_pkt, the pkt 15780 * has to be destroyed *before* freeing the consistent memory. 15781 * Don't change the sequence of this operations. 15782 * scsi_destroy_pkt() might access memory, which isn't allowed, 15783 * after it was freed in scsi_free_consistent_buf(). 15784 */ 15785 if (un->un_rqs_pktp != NULL) { 15786 scsi_destroy_pkt(un->un_rqs_pktp); 15787 un->un_rqs_pktp = NULL; 15788 } 15789 15790 if (un->un_rqs_bp != NULL) { 15791 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 15792 scsi_free_consistent_buf(un->un_rqs_bp); 15793 un->un_rqs_bp = NULL; 15794 } 15795 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15796 } 15797 15798 15799 15800 /* 15801 * Function: sd_reduce_throttle 15802 * 15803 * Description: Reduces the maximun # of outstanding commands on a 15804 * target to the current number of outstanding commands. 15805 * Queues a tiemout(9F) callback to restore the limit 15806 * after a specified interval has elapsed. 15807 * Typically used when we get a TRAN_BUSY return code 15808 * back from scsi_transport(). 15809 * 15810 * Arguments: un - ptr to the sd_lun softstate struct 15811 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15812 * 15813 * Context: May be called from interrupt context 15814 */ 15815 15816 static void 15817 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15818 { 15819 ASSERT(un != NULL); 15820 ASSERT(mutex_owned(SD_MUTEX(un))); 15821 ASSERT(un->un_ncmds_in_transport >= 0); 15822 15823 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15824 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15825 un, un->un_throttle, un->un_ncmds_in_transport); 15826 15827 if (un->un_throttle > 1) { 15828 if (un->un_f_use_adaptive_throttle == TRUE) { 15829 switch (throttle_type) { 15830 case SD_THROTTLE_TRAN_BUSY: 15831 if (un->un_busy_throttle == 0) { 15832 un->un_busy_throttle = un->un_throttle; 15833 } 15834 break; 15835 case SD_THROTTLE_QFULL: 15836 un->un_busy_throttle = 0; 15837 break; 15838 default: 15839 ASSERT(FALSE); 15840 } 15841 15842 if (un->un_ncmds_in_transport > 0) { 15843 un->un_throttle = un->un_ncmds_in_transport; 15844 } 15845 15846 } else { 15847 if (un->un_ncmds_in_transport == 0) { 15848 un->un_throttle = 1; 15849 } else { 15850 un->un_throttle = un->un_ncmds_in_transport; 15851 } 15852 } 15853 } 15854 15855 /* Reschedule the timeout if none is currently active */ 15856 if (un->un_reset_throttle_timeid == NULL) { 15857 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15858 un, SD_THROTTLE_RESET_INTERVAL); 15859 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15860 "sd_reduce_throttle: timeout scheduled!\n"); 15861 } 15862 15863 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15864 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15865 } 15866 15867 15868 15869 /* 15870 * Function: sd_restore_throttle 15871 * 15872 * Description: Callback function for timeout(9F). Resets the current 15873 * value of un->un_throttle to its default. 15874 * 15875 * Arguments: arg - pointer to associated softstate for the device. 15876 * 15877 * Context: May be called from interrupt context 15878 */ 15879 15880 static void 15881 sd_restore_throttle(void *arg) 15882 { 15883 struct sd_lun *un = arg; 15884 15885 ASSERT(un != NULL); 15886 ASSERT(!mutex_owned(SD_MUTEX(un))); 15887 15888 mutex_enter(SD_MUTEX(un)); 15889 15890 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15891 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15892 15893 un->un_reset_throttle_timeid = NULL; 15894 15895 if (un->un_f_use_adaptive_throttle == TRUE) { 15896 /* 15897 * If un_busy_throttle is nonzero, then it contains the 15898 * value that un_throttle was when we got a TRAN_BUSY back 15899 * from scsi_transport(). We want to revert back to this 15900 * value. 15901 * 15902 * In the QFULL case, the throttle limit will incrementally 15903 * increase until it reaches max throttle. 15904 */ 15905 if (un->un_busy_throttle > 0) { 15906 un->un_throttle = un->un_busy_throttle; 15907 un->un_busy_throttle = 0; 15908 } else { 15909 /* 15910 * increase throttle by 10% open gate slowly, schedule 15911 * another restore if saved throttle has not been 15912 * reached 15913 */ 15914 short throttle; 15915 if (sd_qfull_throttle_enable) { 15916 throttle = un->un_throttle + 15917 max((un->un_throttle / 10), 1); 15918 un->un_throttle = 15919 (throttle < un->un_saved_throttle) ? 15920 throttle : un->un_saved_throttle; 15921 if (un->un_throttle < un->un_saved_throttle) { 15922 un->un_reset_throttle_timeid = 15923 timeout(sd_restore_throttle, 15924 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 15925 } 15926 } 15927 } 15928 15929 /* 15930 * If un_throttle has fallen below the low-water mark, we 15931 * restore the maximum value here (and allow it to ratchet 15932 * down again if necessary). 15933 */ 15934 if (un->un_throttle < un->un_min_throttle) { 15935 un->un_throttle = un->un_saved_throttle; 15936 } 15937 } else { 15938 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15939 "restoring limit from 0x%x to 0x%x\n", 15940 un->un_throttle, un->un_saved_throttle); 15941 un->un_throttle = un->un_saved_throttle; 15942 } 15943 15944 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15945 "sd_restore_throttle: calling sd_start_cmds!\n"); 15946 15947 sd_start_cmds(un, NULL); 15948 15949 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15950 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 15951 un, un->un_throttle); 15952 15953 mutex_exit(SD_MUTEX(un)); 15954 15955 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 15956 } 15957 15958 /* 15959 * Function: sdrunout 15960 * 15961 * Description: Callback routine for scsi_init_pkt when a resource allocation 15962 * fails. 15963 * 15964 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 15965 * soft state instance. 15966 * 15967 * Return Code: The scsi_init_pkt routine allows for the callback function to 15968 * return a 0 indicating the callback should be rescheduled or a 1 15969 * indicating not to reschedule. This routine always returns 1 15970 * because the driver always provides a callback function to 15971 * scsi_init_pkt. This results in a callback always being scheduled 15972 * (via the scsi_init_pkt callback implementation) if a resource 15973 * failure occurs. 15974 * 15975 * Context: This callback function may not block or call routines that block 15976 * 15977 * Note: Using the scsi_init_pkt callback facility can result in an I/O 15978 * request persisting at the head of the list which cannot be 15979 * satisfied even after multiple retries. In the future the driver 15980 * may implement some time of maximum runout count before failing 15981 * an I/O. 15982 */ 15983 15984 static int 15985 sdrunout(caddr_t arg) 15986 { 15987 struct sd_lun *un = (struct sd_lun *)arg; 15988 15989 ASSERT(un != NULL); 15990 ASSERT(!mutex_owned(SD_MUTEX(un))); 15991 15992 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 15993 15994 mutex_enter(SD_MUTEX(un)); 15995 sd_start_cmds(un, NULL); 15996 mutex_exit(SD_MUTEX(un)); 15997 /* 15998 * This callback routine always returns 1 (i.e. do not reschedule) 15999 * because we always specify sdrunout as the callback handler for 16000 * scsi_init_pkt inside the call to sd_start_cmds. 16001 */ 16002 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16003 return (1); 16004 } 16005 16006 16007 /* 16008 * Function: sdintr 16009 * 16010 * Description: Completion callback routine for scsi_pkt(9S) structs 16011 * sent to the HBA driver via scsi_transport(9F). 16012 * 16013 * Context: Interrupt context 16014 */ 16015 16016 static void 16017 sdintr(struct scsi_pkt *pktp) 16018 { 16019 struct buf *bp; 16020 struct sd_xbuf *xp; 16021 struct sd_lun *un; 16022 16023 ASSERT(pktp != NULL); 16024 bp = (struct buf *)pktp->pkt_private; 16025 ASSERT(bp != NULL); 16026 xp = SD_GET_XBUF(bp); 16027 ASSERT(xp != NULL); 16028 ASSERT(xp->xb_pktp != NULL); 16029 un = SD_GET_UN(bp); 16030 ASSERT(un != NULL); 16031 ASSERT(!mutex_owned(SD_MUTEX(un))); 16032 16033 #ifdef SD_FAULT_INJECTION 16034 16035 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16036 /* SD FaultInjection */ 16037 sd_faultinjection(pktp); 16038 16039 #endif /* SD_FAULT_INJECTION */ 16040 16041 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16042 " xp:0x%p, un:0x%p\n", bp, xp, un); 16043 16044 mutex_enter(SD_MUTEX(un)); 16045 16046 /* Reduce the count of the #commands currently in transport */ 16047 un->un_ncmds_in_transport--; 16048 ASSERT(un->un_ncmds_in_transport >= 0); 16049 16050 /* Increment counter to indicate that the callback routine is active */ 16051 un->un_in_callback++; 16052 16053 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16054 16055 #ifdef SDDEBUG 16056 if (bp == un->un_retry_bp) { 16057 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16058 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16059 un, un->un_retry_bp, un->un_ncmds_in_transport); 16060 } 16061 #endif 16062 16063 /* 16064 * If pkt_reason is CMD_DEV_GONE, just fail the command 16065 */ 16066 if (pktp->pkt_reason == CMD_DEV_GONE) { 16067 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16068 "Device is gone\n"); 16069 sd_return_failed_command(un, bp, EIO); 16070 goto exit; 16071 } 16072 16073 /* 16074 * First see if the pkt has auto-request sense data with it.... 16075 * Look at the packet state first so we don't take a performance 16076 * hit looking at the arq enabled flag unless absolutely necessary. 16077 */ 16078 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16079 (un->un_f_arq_enabled == TRUE)) { 16080 /* 16081 * The HBA did an auto request sense for this command so check 16082 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16083 * driver command that should not be retried. 16084 */ 16085 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16086 /* 16087 * Save the relevant sense info into the xp for the 16088 * original cmd. 16089 */ 16090 struct scsi_arq_status *asp; 16091 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16092 xp->xb_sense_status = 16093 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16094 xp->xb_sense_state = asp->sts_rqpkt_state; 16095 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16096 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16097 min(sizeof (struct scsi_extended_sense), 16098 SENSE_LENGTH)); 16099 16100 /* fail the command */ 16101 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16102 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16103 sd_return_failed_command(un, bp, EIO); 16104 goto exit; 16105 } 16106 16107 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16108 /* 16109 * We want to either retry or fail this command, so free 16110 * the DMA resources here. If we retry the command then 16111 * the DMA resources will be reallocated in sd_start_cmds(). 16112 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16113 * causes the *entire* transfer to start over again from the 16114 * beginning of the request, even for PARTIAL chunks that 16115 * have already transferred successfully. 16116 */ 16117 if ((un->un_f_is_fibre == TRUE) && 16118 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16119 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16120 scsi_dmafree(pktp); 16121 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16122 } 16123 #endif 16124 16125 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16126 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16127 16128 sd_handle_auto_request_sense(un, bp, xp, pktp); 16129 goto exit; 16130 } 16131 16132 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16133 if (pktp->pkt_flags & FLAG_SENSING) { 16134 /* This pktp is from the unit's REQUEST_SENSE command */ 16135 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16136 "sdintr: sd_handle_request_sense\n"); 16137 sd_handle_request_sense(un, bp, xp, pktp); 16138 goto exit; 16139 } 16140 16141 /* 16142 * Check to see if the command successfully completed as requested; 16143 * this is the most common case (and also the hot performance path). 16144 * 16145 * Requirements for successful completion are: 16146 * pkt_reason is CMD_CMPLT and packet status is status good. 16147 * In addition: 16148 * - A residual of zero indicates successful completion no matter what 16149 * the command is. 16150 * - If the residual is not zero and the command is not a read or 16151 * write, then it's still defined as successful completion. In other 16152 * words, if the command is a read or write the residual must be 16153 * zero for successful completion. 16154 * - If the residual is not zero and the command is a read or 16155 * write, and it's a USCSICMD, then it's still defined as 16156 * successful completion. 16157 */ 16158 if ((pktp->pkt_reason == CMD_CMPLT) && 16159 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16160 16161 /* 16162 * Since this command is returned with a good status, we 16163 * can reset the count for Sonoma failover. 16164 */ 16165 un->un_sonoma_failure_count = 0; 16166 16167 /* 16168 * Return all USCSI commands on good status 16169 */ 16170 if (pktp->pkt_resid == 0) { 16171 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16172 "sdintr: returning command for resid == 0\n"); 16173 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16174 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16175 SD_UPDATE_B_RESID(bp, pktp); 16176 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16177 "sdintr: returning command for resid != 0\n"); 16178 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16179 SD_UPDATE_B_RESID(bp, pktp); 16180 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16181 "sdintr: returning uscsi command\n"); 16182 } else { 16183 goto not_successful; 16184 } 16185 sd_return_command(un, bp); 16186 16187 /* 16188 * Decrement counter to indicate that the callback routine 16189 * is done. 16190 */ 16191 un->un_in_callback--; 16192 ASSERT(un->un_in_callback >= 0); 16193 mutex_exit(SD_MUTEX(un)); 16194 16195 return; 16196 } 16197 16198 not_successful: 16199 16200 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16201 /* 16202 * The following is based upon knowledge of the underlying transport 16203 * and its use of DMA resources. This code should be removed when 16204 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16205 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16206 * and sd_start_cmds(). 16207 * 16208 * Free any DMA resources associated with this command if there 16209 * is a chance it could be retried or enqueued for later retry. 16210 * If we keep the DMA binding then mpxio cannot reissue the 16211 * command on another path whenever a path failure occurs. 16212 * 16213 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16214 * causes the *entire* transfer to start over again from the 16215 * beginning of the request, even for PARTIAL chunks that 16216 * have already transferred successfully. 16217 * 16218 * This is only done for non-uscsi commands (and also skipped for the 16219 * driver's internal RQS command). Also just do this for Fibre Channel 16220 * devices as these are the only ones that support mpxio. 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 /* 16231 * The command did not successfully complete as requested so check 16232 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16233 * driver command that should not be retried so just return. If 16234 * FLAG_DIAGNOSE is not set the error will be processed below. 16235 */ 16236 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16237 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16238 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16239 /* 16240 * Issue a request sense if a check condition caused the error 16241 * (we handle the auto request sense case above), otherwise 16242 * just fail the command. 16243 */ 16244 if ((pktp->pkt_reason == CMD_CMPLT) && 16245 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16246 sd_send_request_sense_command(un, bp, pktp); 16247 } else { 16248 sd_return_failed_command(un, bp, EIO); 16249 } 16250 goto exit; 16251 } 16252 16253 /* 16254 * The command did not successfully complete as requested so process 16255 * the error, retry, and/or attempt recovery. 16256 */ 16257 switch (pktp->pkt_reason) { 16258 case CMD_CMPLT: 16259 switch (SD_GET_PKT_STATUS(pktp)) { 16260 case STATUS_GOOD: 16261 /* 16262 * The command completed successfully with a non-zero 16263 * residual 16264 */ 16265 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16266 "sdintr: STATUS_GOOD \n"); 16267 sd_pkt_status_good(un, bp, xp, pktp); 16268 break; 16269 16270 case STATUS_CHECK: 16271 case STATUS_TERMINATED: 16272 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16273 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16274 sd_pkt_status_check_condition(un, bp, xp, pktp); 16275 break; 16276 16277 case STATUS_BUSY: 16278 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16279 "sdintr: STATUS_BUSY\n"); 16280 sd_pkt_status_busy(un, bp, xp, pktp); 16281 break; 16282 16283 case STATUS_RESERVATION_CONFLICT: 16284 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16285 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16286 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16287 break; 16288 16289 case STATUS_QFULL: 16290 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16291 "sdintr: STATUS_QFULL\n"); 16292 sd_pkt_status_qfull(un, bp, xp, pktp); 16293 break; 16294 16295 case STATUS_MET: 16296 case STATUS_INTERMEDIATE: 16297 case STATUS_SCSI2: 16298 case STATUS_INTERMEDIATE_MET: 16299 case STATUS_ACA_ACTIVE: 16300 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16301 "Unexpected SCSI status received: 0x%x\n", 16302 SD_GET_PKT_STATUS(pktp)); 16303 sd_return_failed_command(un, bp, EIO); 16304 break; 16305 16306 default: 16307 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16308 "Invalid SCSI status received: 0x%x\n", 16309 SD_GET_PKT_STATUS(pktp)); 16310 sd_return_failed_command(un, bp, EIO); 16311 break; 16312 16313 } 16314 break; 16315 16316 case CMD_INCOMPLETE: 16317 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16318 "sdintr: CMD_INCOMPLETE\n"); 16319 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16320 break; 16321 case CMD_TRAN_ERR: 16322 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16323 "sdintr: CMD_TRAN_ERR\n"); 16324 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16325 break; 16326 case CMD_RESET: 16327 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16328 "sdintr: CMD_RESET \n"); 16329 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16330 break; 16331 case CMD_ABORTED: 16332 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16333 "sdintr: CMD_ABORTED \n"); 16334 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16335 break; 16336 case CMD_TIMEOUT: 16337 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16338 "sdintr: CMD_TIMEOUT\n"); 16339 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16340 break; 16341 case CMD_UNX_BUS_FREE: 16342 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16343 "sdintr: CMD_UNX_BUS_FREE \n"); 16344 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16345 break; 16346 case CMD_TAG_REJECT: 16347 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16348 "sdintr: CMD_TAG_REJECT\n"); 16349 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16350 break; 16351 default: 16352 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16353 "sdintr: default\n"); 16354 sd_pkt_reason_default(un, bp, xp, pktp); 16355 break; 16356 } 16357 16358 exit: 16359 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16360 16361 /* Decrement counter to indicate that the callback routine is done. */ 16362 un->un_in_callback--; 16363 ASSERT(un->un_in_callback >= 0); 16364 16365 /* 16366 * At this point, the pkt has been dispatched, ie, it is either 16367 * being re-tried or has been returned to its caller and should 16368 * not be referenced. 16369 */ 16370 16371 mutex_exit(SD_MUTEX(un)); 16372 } 16373 16374 16375 /* 16376 * Function: sd_print_incomplete_msg 16377 * 16378 * Description: Prints the error message for a CMD_INCOMPLETE error. 16379 * 16380 * Arguments: un - ptr to associated softstate for the device. 16381 * bp - ptr to the buf(9S) for the command. 16382 * arg - message string ptr 16383 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16384 * or SD_NO_RETRY_ISSUED. 16385 * 16386 * Context: May be called under interrupt context 16387 */ 16388 16389 static void 16390 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16391 { 16392 struct scsi_pkt *pktp; 16393 char *msgp; 16394 char *cmdp = arg; 16395 16396 ASSERT(un != NULL); 16397 ASSERT(mutex_owned(SD_MUTEX(un))); 16398 ASSERT(bp != NULL); 16399 ASSERT(arg != NULL); 16400 pktp = SD_GET_PKTP(bp); 16401 ASSERT(pktp != NULL); 16402 16403 switch (code) { 16404 case SD_DELAYED_RETRY_ISSUED: 16405 case SD_IMMEDIATE_RETRY_ISSUED: 16406 msgp = "retrying"; 16407 break; 16408 case SD_NO_RETRY_ISSUED: 16409 default: 16410 msgp = "giving up"; 16411 break; 16412 } 16413 16414 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16415 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16416 "incomplete %s- %s\n", cmdp, msgp); 16417 } 16418 } 16419 16420 16421 16422 /* 16423 * Function: sd_pkt_status_good 16424 * 16425 * Description: Processing for a STATUS_GOOD code in pkt_status. 16426 * 16427 * Context: May be called under interrupt context 16428 */ 16429 16430 static void 16431 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16432 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16433 { 16434 char *cmdp; 16435 16436 ASSERT(un != NULL); 16437 ASSERT(mutex_owned(SD_MUTEX(un))); 16438 ASSERT(bp != NULL); 16439 ASSERT(xp != NULL); 16440 ASSERT(pktp != NULL); 16441 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16442 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16443 ASSERT(pktp->pkt_resid != 0); 16444 16445 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16446 16447 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16448 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16449 case SCMD_READ: 16450 cmdp = "read"; 16451 break; 16452 case SCMD_WRITE: 16453 cmdp = "write"; 16454 break; 16455 default: 16456 SD_UPDATE_B_RESID(bp, pktp); 16457 sd_return_command(un, bp); 16458 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16459 return; 16460 } 16461 16462 /* 16463 * See if we can retry the read/write, preferrably immediately. 16464 * If retries are exhaused, then sd_retry_command() will update 16465 * the b_resid count. 16466 */ 16467 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16468 cmdp, EIO, (clock_t)0, NULL); 16469 16470 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16471 } 16472 16473 16474 16475 16476 16477 /* 16478 * Function: sd_handle_request_sense 16479 * 16480 * Description: Processing for non-auto Request Sense command. 16481 * 16482 * Arguments: un - ptr to associated softstate 16483 * sense_bp - ptr to buf(9S) for the RQS command 16484 * sense_xp - ptr to the sd_xbuf for the RQS command 16485 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16486 * 16487 * Context: May be called under interrupt context 16488 */ 16489 16490 static void 16491 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16492 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16493 { 16494 struct buf *cmd_bp; /* buf for the original command */ 16495 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16496 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16497 16498 ASSERT(un != NULL); 16499 ASSERT(mutex_owned(SD_MUTEX(un))); 16500 ASSERT(sense_bp != NULL); 16501 ASSERT(sense_xp != NULL); 16502 ASSERT(sense_pktp != NULL); 16503 16504 /* 16505 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16506 * RQS command and not the original command. 16507 */ 16508 ASSERT(sense_pktp == un->un_rqs_pktp); 16509 ASSERT(sense_bp == un->un_rqs_bp); 16510 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16511 (FLAG_SENSING | FLAG_HEAD)); 16512 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16513 FLAG_SENSING) == FLAG_SENSING); 16514 16515 /* These are the bp, xp, and pktp for the original command */ 16516 cmd_bp = sense_xp->xb_sense_bp; 16517 cmd_xp = SD_GET_XBUF(cmd_bp); 16518 cmd_pktp = SD_GET_PKTP(cmd_bp); 16519 16520 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16521 /* 16522 * The REQUEST SENSE command failed. Release the REQUEST 16523 * SENSE command for re-use, get back the bp for the original 16524 * command, and attempt to re-try the original command if 16525 * FLAG_DIAGNOSE is not set in the original packet. 16526 */ 16527 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16528 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16529 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16530 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16531 NULL, NULL, EIO, (clock_t)0, NULL); 16532 return; 16533 } 16534 } 16535 16536 /* 16537 * Save the relevant sense info into the xp for the original cmd. 16538 * 16539 * Note: if the request sense failed the state info will be zero 16540 * as set in sd_mark_rqs_busy() 16541 */ 16542 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16543 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16544 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16545 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16546 16547 /* 16548 * Free up the RQS command.... 16549 * NOTE: 16550 * Must do this BEFORE calling sd_validate_sense_data! 16551 * sd_validate_sense_data may return the original command in 16552 * which case the pkt will be freed and the flags can no 16553 * longer be touched. 16554 * SD_MUTEX is held through this process until the command 16555 * is dispatched based upon the sense data, so there are 16556 * no race conditions. 16557 */ 16558 (void) sd_mark_rqs_idle(un, sense_xp); 16559 16560 /* 16561 * For a retryable command see if we have valid sense data, if so then 16562 * turn it over to sd_decode_sense() to figure out the right course of 16563 * action. Just fail a non-retryable command. 16564 */ 16565 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16566 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16567 SD_SENSE_DATA_IS_VALID) { 16568 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16569 } 16570 } else { 16571 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16572 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16573 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16574 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16575 sd_return_failed_command(un, cmd_bp, EIO); 16576 } 16577 } 16578 16579 16580 16581 16582 /* 16583 * Function: sd_handle_auto_request_sense 16584 * 16585 * Description: Processing for auto-request sense information. 16586 * 16587 * Arguments: un - ptr to associated softstate 16588 * bp - ptr to buf(9S) for the command 16589 * xp - ptr to the sd_xbuf for the command 16590 * pktp - ptr to the scsi_pkt(9S) for the command 16591 * 16592 * Context: May be called under interrupt context 16593 */ 16594 16595 static void 16596 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16597 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16598 { 16599 struct scsi_arq_status *asp; 16600 16601 ASSERT(un != NULL); 16602 ASSERT(mutex_owned(SD_MUTEX(un))); 16603 ASSERT(bp != NULL); 16604 ASSERT(xp != NULL); 16605 ASSERT(pktp != NULL); 16606 ASSERT(pktp != un->un_rqs_pktp); 16607 ASSERT(bp != un->un_rqs_bp); 16608 16609 /* 16610 * For auto-request sense, we get a scsi_arq_status back from 16611 * the HBA, with the sense data in the sts_sensedata member. 16612 * The pkt_scbp of the packet points to this scsi_arq_status. 16613 */ 16614 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16615 16616 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16617 /* 16618 * The auto REQUEST SENSE failed; see if we can re-try 16619 * the original command. 16620 */ 16621 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16622 "auto request sense failed (reason=%s)\n", 16623 scsi_rname(asp->sts_rqpkt_reason)); 16624 16625 sd_reset_target(un, pktp); 16626 16627 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16628 NULL, NULL, EIO, (clock_t)0, NULL); 16629 return; 16630 } 16631 16632 /* Save the relevant sense info into the xp for the original cmd. */ 16633 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16634 xp->xb_sense_state = asp->sts_rqpkt_state; 16635 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16636 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16637 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16638 16639 /* 16640 * See if we have valid sense data, if so then turn it over to 16641 * sd_decode_sense() to figure out the right course of action. 16642 */ 16643 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16644 sd_decode_sense(un, bp, xp, pktp); 16645 } 16646 } 16647 16648 16649 /* 16650 * Function: sd_print_sense_failed_msg 16651 * 16652 * Description: Print log message when RQS has failed. 16653 * 16654 * Arguments: un - ptr to associated softstate 16655 * bp - ptr to buf(9S) for the command 16656 * arg - generic message string ptr 16657 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16658 * or SD_NO_RETRY_ISSUED 16659 * 16660 * Context: May be called from interrupt context 16661 */ 16662 16663 static void 16664 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16665 int code) 16666 { 16667 char *msgp = arg; 16668 16669 ASSERT(un != NULL); 16670 ASSERT(mutex_owned(SD_MUTEX(un))); 16671 ASSERT(bp != NULL); 16672 16673 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16674 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16675 } 16676 } 16677 16678 16679 /* 16680 * Function: sd_validate_sense_data 16681 * 16682 * Description: Check the given sense data for validity. 16683 * If the sense data is not valid, the command will 16684 * be either failed or retried! 16685 * 16686 * Return Code: SD_SENSE_DATA_IS_INVALID 16687 * SD_SENSE_DATA_IS_VALID 16688 * 16689 * Context: May be called from interrupt context 16690 */ 16691 16692 static int 16693 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16694 { 16695 struct scsi_extended_sense *esp; 16696 struct scsi_pkt *pktp; 16697 size_t actual_len; 16698 char *msgp = NULL; 16699 16700 ASSERT(un != NULL); 16701 ASSERT(mutex_owned(SD_MUTEX(un))); 16702 ASSERT(bp != NULL); 16703 ASSERT(bp != un->un_rqs_bp); 16704 ASSERT(xp != NULL); 16705 16706 pktp = SD_GET_PKTP(bp); 16707 ASSERT(pktp != NULL); 16708 16709 /* 16710 * Check the status of the RQS command (auto or manual). 16711 */ 16712 switch (xp->xb_sense_status & STATUS_MASK) { 16713 case STATUS_GOOD: 16714 break; 16715 16716 case STATUS_RESERVATION_CONFLICT: 16717 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16718 return (SD_SENSE_DATA_IS_INVALID); 16719 16720 case STATUS_BUSY: 16721 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16722 "Busy Status on REQUEST SENSE\n"); 16723 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16724 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16725 return (SD_SENSE_DATA_IS_INVALID); 16726 16727 case STATUS_QFULL: 16728 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16729 "QFULL Status on REQUEST SENSE\n"); 16730 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16731 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16732 return (SD_SENSE_DATA_IS_INVALID); 16733 16734 case STATUS_CHECK: 16735 case STATUS_TERMINATED: 16736 msgp = "Check Condition on REQUEST SENSE\n"; 16737 goto sense_failed; 16738 16739 default: 16740 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16741 goto sense_failed; 16742 } 16743 16744 /* 16745 * See if we got the minimum required amount of sense data. 16746 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16747 * or less. 16748 */ 16749 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 16750 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16751 (actual_len == 0)) { 16752 msgp = "Request Sense couldn't get sense data\n"; 16753 goto sense_failed; 16754 } 16755 16756 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16757 msgp = "Not enough sense information\n"; 16758 goto sense_failed; 16759 } 16760 16761 /* 16762 * We require the extended sense data 16763 */ 16764 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16765 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16766 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16767 static char tmp[8]; 16768 static char buf[148]; 16769 char *p = (char *)(xp->xb_sense_data); 16770 int i; 16771 16772 mutex_enter(&sd_sense_mutex); 16773 (void) strcpy(buf, "undecodable sense information:"); 16774 for (i = 0; i < actual_len; i++) { 16775 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16776 (void) strcpy(&buf[strlen(buf)], tmp); 16777 } 16778 i = strlen(buf); 16779 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16780 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 16781 mutex_exit(&sd_sense_mutex); 16782 } 16783 /* Note: Legacy behavior, fail the command with no retry */ 16784 sd_return_failed_command(un, bp, EIO); 16785 return (SD_SENSE_DATA_IS_INVALID); 16786 } 16787 16788 /* 16789 * Check that es_code is valid (es_class concatenated with es_code 16790 * make up the "response code" field. es_class will always be 7, so 16791 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16792 * format. 16793 */ 16794 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16795 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16796 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16797 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16798 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16799 goto sense_failed; 16800 } 16801 16802 return (SD_SENSE_DATA_IS_VALID); 16803 16804 sense_failed: 16805 /* 16806 * If the request sense failed (for whatever reason), attempt 16807 * to retry the original command. 16808 */ 16809 #if defined(__i386) || defined(__amd64) 16810 /* 16811 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 16812 * sddef.h for Sparc platform, and x86 uses 1 binary 16813 * for both SCSI/FC. 16814 * The SD_RETRY_DELAY value need to be adjusted here 16815 * when SD_RETRY_DELAY change in sddef.h 16816 */ 16817 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16818 sd_print_sense_failed_msg, msgp, EIO, 16819 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 16820 #else 16821 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16822 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 16823 #endif 16824 16825 return (SD_SENSE_DATA_IS_INVALID); 16826 } 16827 16828 16829 16830 /* 16831 * Function: sd_decode_sense 16832 * 16833 * Description: Take recovery action(s) when SCSI Sense Data is received. 16834 * 16835 * Context: Interrupt context. 16836 */ 16837 16838 static void 16839 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16840 struct scsi_pkt *pktp) 16841 { 16842 struct scsi_extended_sense *esp; 16843 struct scsi_descr_sense_hdr *sdsp; 16844 uint8_t asc, ascq, sense_key; 16845 16846 ASSERT(un != NULL); 16847 ASSERT(mutex_owned(SD_MUTEX(un))); 16848 ASSERT(bp != NULL); 16849 ASSERT(bp != un->un_rqs_bp); 16850 ASSERT(xp != NULL); 16851 ASSERT(pktp != NULL); 16852 16853 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16854 16855 switch (esp->es_code) { 16856 case CODE_FMT_DESCR_CURRENT: 16857 case CODE_FMT_DESCR_DEFERRED: 16858 sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data; 16859 sense_key = sdsp->ds_key; 16860 asc = sdsp->ds_add_code; 16861 ascq = sdsp->ds_qual_code; 16862 break; 16863 case CODE_FMT_VENDOR_SPECIFIC: 16864 case CODE_FMT_FIXED_CURRENT: 16865 case CODE_FMT_FIXED_DEFERRED: 16866 default: 16867 sense_key = esp->es_key; 16868 asc = esp->es_add_code; 16869 ascq = esp->es_qual_code; 16870 break; 16871 } 16872 16873 switch (sense_key) { 16874 case KEY_NO_SENSE: 16875 sd_sense_key_no_sense(un, bp, xp, pktp); 16876 break; 16877 case KEY_RECOVERABLE_ERROR: 16878 sd_sense_key_recoverable_error(un, asc, bp, xp, pktp); 16879 break; 16880 case KEY_NOT_READY: 16881 sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp); 16882 break; 16883 case KEY_MEDIUM_ERROR: 16884 case KEY_HARDWARE_ERROR: 16885 sd_sense_key_medium_or_hardware_error(un, 16886 sense_key, asc, bp, xp, pktp); 16887 break; 16888 case KEY_ILLEGAL_REQUEST: 16889 sd_sense_key_illegal_request(un, bp, xp, pktp); 16890 break; 16891 case KEY_UNIT_ATTENTION: 16892 sd_sense_key_unit_attention(un, asc, bp, xp, pktp); 16893 break; 16894 case KEY_WRITE_PROTECT: 16895 case KEY_VOLUME_OVERFLOW: 16896 case KEY_MISCOMPARE: 16897 sd_sense_key_fail_command(un, bp, xp, pktp); 16898 break; 16899 case KEY_BLANK_CHECK: 16900 sd_sense_key_blank_check(un, bp, xp, pktp); 16901 break; 16902 case KEY_ABORTED_COMMAND: 16903 sd_sense_key_aborted_command(un, bp, xp, pktp); 16904 break; 16905 case KEY_VENDOR_UNIQUE: 16906 case KEY_COPY_ABORTED: 16907 case KEY_EQUAL: 16908 case KEY_RESERVED: 16909 default: 16910 sd_sense_key_default(un, sense_key, bp, xp, pktp); 16911 break; 16912 } 16913 } 16914 16915 16916 /* 16917 * Function: sd_dump_memory 16918 * 16919 * Description: Debug logging routine to print the contents of a user provided 16920 * buffer. The output of the buffer is broken up into 256 byte 16921 * segments due to a size constraint of the scsi_log. 16922 * implementation. 16923 * 16924 * Arguments: un - ptr to softstate 16925 * comp - component mask 16926 * title - "title" string to preceed data when printed 16927 * data - ptr to data block to be printed 16928 * len - size of data block to be printed 16929 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 16930 * 16931 * Context: May be called from interrupt context 16932 */ 16933 16934 #define SD_DUMP_MEMORY_BUF_SIZE 256 16935 16936 static char *sd_dump_format_string[] = { 16937 " 0x%02x", 16938 " %c" 16939 }; 16940 16941 static void 16942 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 16943 int len, int fmt) 16944 { 16945 int i, j; 16946 int avail_count; 16947 int start_offset; 16948 int end_offset; 16949 size_t entry_len; 16950 char *bufp; 16951 char *local_buf; 16952 char *format_string; 16953 16954 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 16955 16956 /* 16957 * In the debug version of the driver, this function is called from a 16958 * number of places which are NOPs in the release driver. 16959 * The debug driver therefore has additional methods of filtering 16960 * debug output. 16961 */ 16962 #ifdef SDDEBUG 16963 /* 16964 * In the debug version of the driver we can reduce the amount of debug 16965 * messages by setting sd_error_level to something other than 16966 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 16967 * sd_component_mask. 16968 */ 16969 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 16970 (sd_error_level != SCSI_ERR_ALL)) { 16971 return; 16972 } 16973 if (((sd_component_mask & comp) == 0) || 16974 (sd_error_level != SCSI_ERR_ALL)) { 16975 return; 16976 } 16977 #else 16978 if (sd_error_level != SCSI_ERR_ALL) { 16979 return; 16980 } 16981 #endif 16982 16983 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 16984 bufp = local_buf; 16985 /* 16986 * Available length is the length of local_buf[], minus the 16987 * length of the title string, minus one for the ":", minus 16988 * one for the newline, minus one for the NULL terminator. 16989 * This gives the #bytes available for holding the printed 16990 * values from the given data buffer. 16991 */ 16992 if (fmt == SD_LOG_HEX) { 16993 format_string = sd_dump_format_string[0]; 16994 } else /* SD_LOG_CHAR */ { 16995 format_string = sd_dump_format_string[1]; 16996 } 16997 /* 16998 * Available count is the number of elements from the given 16999 * data buffer that we can fit into the available length. 17000 * This is based upon the size of the format string used. 17001 * Make one entry and find it's size. 17002 */ 17003 (void) sprintf(bufp, format_string, data[0]); 17004 entry_len = strlen(bufp); 17005 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17006 17007 j = 0; 17008 while (j < len) { 17009 bufp = local_buf; 17010 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17011 start_offset = j; 17012 17013 end_offset = start_offset + avail_count; 17014 17015 (void) sprintf(bufp, "%s:", title); 17016 bufp += strlen(bufp); 17017 for (i = start_offset; ((i < end_offset) && (j < len)); 17018 i++, j++) { 17019 (void) sprintf(bufp, format_string, data[i]); 17020 bufp += entry_len; 17021 } 17022 (void) sprintf(bufp, "\n"); 17023 17024 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17025 } 17026 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17027 } 17028 17029 /* 17030 * Function: sd_print_sense_msg 17031 * 17032 * Description: Log a message based upon the given sense data. 17033 * 17034 * Arguments: un - ptr to associated softstate 17035 * bp - ptr to buf(9S) for the command 17036 * arg - ptr to associate sd_sense_info struct 17037 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17038 * or SD_NO_RETRY_ISSUED 17039 * 17040 * Context: May be called from interrupt context 17041 */ 17042 17043 static void 17044 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17045 { 17046 struct sd_xbuf *xp; 17047 struct scsi_pkt *pktp; 17048 struct scsi_extended_sense *sensep; 17049 daddr_t request_blkno; 17050 diskaddr_t err_blkno; 17051 int severity; 17052 int pfa_flag; 17053 int fixed_format = TRUE; 17054 extern struct scsi_key_strings scsi_cmds[]; 17055 17056 ASSERT(un != NULL); 17057 ASSERT(mutex_owned(SD_MUTEX(un))); 17058 ASSERT(bp != NULL); 17059 xp = SD_GET_XBUF(bp); 17060 ASSERT(xp != NULL); 17061 pktp = SD_GET_PKTP(bp); 17062 ASSERT(pktp != NULL); 17063 ASSERT(arg != NULL); 17064 17065 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17066 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17067 17068 if ((code == SD_DELAYED_RETRY_ISSUED) || 17069 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17070 severity = SCSI_ERR_RETRYABLE; 17071 } 17072 17073 /* Use absolute block number for the request block number */ 17074 request_blkno = xp->xb_blkno; 17075 17076 /* 17077 * Now try to get the error block number from the sense data 17078 */ 17079 sensep = (struct scsi_extended_sense *)xp->xb_sense_data; 17080 switch (sensep->es_code) { 17081 case CODE_FMT_DESCR_CURRENT: 17082 case CODE_FMT_DESCR_DEFERRED: 17083 err_blkno = 17084 sd_extract_sense_info_descr( 17085 (struct scsi_descr_sense_hdr *)sensep); 17086 fixed_format = FALSE; 17087 break; 17088 case CODE_FMT_FIXED_CURRENT: 17089 case CODE_FMT_FIXED_DEFERRED: 17090 case CODE_FMT_VENDOR_SPECIFIC: 17091 default: 17092 /* 17093 * With the es_valid bit set, we assume that the error 17094 * blkno is in the sense data. Also, if xp->xb_blkno is 17095 * greater than 0xffffffff then the target *should* have used 17096 * a descriptor sense format (or it shouldn't have set 17097 * the es_valid bit), and we may as well ignore the 17098 * 32-bit value. 17099 */ 17100 if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) { 17101 err_blkno = (diskaddr_t) 17102 ((sensep->es_info_1 << 24) | 17103 (sensep->es_info_2 << 16) | 17104 (sensep->es_info_3 << 8) | 17105 (sensep->es_info_4)); 17106 } else { 17107 err_blkno = (diskaddr_t)-1; 17108 } 17109 break; 17110 } 17111 17112 if (err_blkno == (diskaddr_t)-1) { 17113 /* 17114 * Without the es_valid bit set (for fixed format) or an 17115 * information descriptor (for descriptor format) we cannot 17116 * be certain of the error blkno, so just use the 17117 * request_blkno. 17118 */ 17119 err_blkno = (diskaddr_t)request_blkno; 17120 } else { 17121 /* 17122 * We retrieved the error block number from the information 17123 * portion of the sense data. 17124 * 17125 * For USCSI commands we are better off using the error 17126 * block no. as the requested block no. (This is the best 17127 * we can estimate.) 17128 */ 17129 if ((SD_IS_BUFIO(xp) == FALSE) && 17130 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17131 request_blkno = err_blkno; 17132 } 17133 } 17134 17135 /* 17136 * The following will log the buffer contents for the release driver 17137 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17138 * level is set to verbose. 17139 */ 17140 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17141 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17142 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17143 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17144 17145 if (pfa_flag == FALSE) { 17146 /* This is normally only set for USCSI */ 17147 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17148 return; 17149 } 17150 17151 if ((SD_IS_BUFIO(xp) == TRUE) && 17152 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17153 (severity < sd_error_level))) { 17154 return; 17155 } 17156 } 17157 17158 /* 17159 * If the data is fixed format then check for Sonoma Failover, 17160 * and keep a count of how many failed I/O's. We should not have 17161 * to worry about Sonoma returning descriptor format sense data, 17162 * and asc/ascq are in a different location in descriptor format. 17163 */ 17164 if (fixed_format && 17165 (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) && 17166 (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) { 17167 un->un_sonoma_failure_count++; 17168 if (un->un_sonoma_failure_count > 1) { 17169 return; 17170 } 17171 } 17172 17173 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17174 request_blkno, err_blkno, scsi_cmds, sensep, 17175 un->un_additional_codes, NULL); 17176 } 17177 17178 /* 17179 * Function: sd_extract_sense_info_descr 17180 * 17181 * Description: Retrieve "information" field from descriptor format 17182 * sense data. Iterates through each sense descriptor 17183 * looking for the information descriptor and returns 17184 * the information field from that descriptor. 17185 * 17186 * Context: May be called from interrupt context 17187 */ 17188 17189 static diskaddr_t 17190 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp) 17191 { 17192 diskaddr_t result; 17193 uint8_t *descr_offset; 17194 int valid_sense_length; 17195 struct scsi_information_sense_descr *isd; 17196 17197 /* 17198 * Initialize result to -1 indicating there is no information 17199 * descriptor 17200 */ 17201 result = (diskaddr_t)-1; 17202 17203 /* 17204 * The first descriptor will immediately follow the header 17205 */ 17206 descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */ 17207 17208 /* 17209 * Calculate the amount of valid sense data 17210 */ 17211 valid_sense_length = 17212 min((sizeof (struct scsi_descr_sense_hdr) + 17213 sdsp->ds_addl_sense_length), 17214 SENSE_LENGTH); 17215 17216 /* 17217 * Iterate through the list of descriptors, stopping when we 17218 * run out of sense data 17219 */ 17220 while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <= 17221 (uint8_t *)sdsp + valid_sense_length) { 17222 /* 17223 * Check if this is an information descriptor. We can 17224 * use the scsi_information_sense_descr structure as a 17225 * template sense the first two fields are always the 17226 * same 17227 */ 17228 isd = (struct scsi_information_sense_descr *)descr_offset; 17229 if (isd->isd_descr_type == DESCR_INFORMATION) { 17230 /* 17231 * Found an information descriptor. Copy the 17232 * information field. There will only be one 17233 * information descriptor so we can stop looking. 17234 */ 17235 result = 17236 (((diskaddr_t)isd->isd_information[0] << 56) | 17237 ((diskaddr_t)isd->isd_information[1] << 48) | 17238 ((diskaddr_t)isd->isd_information[2] << 40) | 17239 ((diskaddr_t)isd->isd_information[3] << 32) | 17240 ((diskaddr_t)isd->isd_information[4] << 24) | 17241 ((diskaddr_t)isd->isd_information[5] << 16) | 17242 ((diskaddr_t)isd->isd_information[6] << 8) | 17243 ((diskaddr_t)isd->isd_information[7])); 17244 break; 17245 } 17246 17247 /* 17248 * Get pointer to the next descriptor. The "additional 17249 * length" field holds the length of the descriptor except 17250 * for the "type" and "additional length" fields, so 17251 * we need to add 2 to get the total length. 17252 */ 17253 descr_offset += (isd->isd_addl_length + 2); 17254 } 17255 17256 return (result); 17257 } 17258 17259 /* 17260 * Function: sd_sense_key_no_sense 17261 * 17262 * Description: Recovery action when sense data was not received. 17263 * 17264 * Context: May be called from interrupt context 17265 */ 17266 17267 static void 17268 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17269 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17270 { 17271 struct sd_sense_info si; 17272 17273 ASSERT(un != NULL); 17274 ASSERT(mutex_owned(SD_MUTEX(un))); 17275 ASSERT(bp != NULL); 17276 ASSERT(xp != NULL); 17277 ASSERT(pktp != NULL); 17278 17279 si.ssi_severity = SCSI_ERR_FATAL; 17280 si.ssi_pfa_flag = FALSE; 17281 17282 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17283 17284 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17285 &si, EIO, (clock_t)0, NULL); 17286 } 17287 17288 17289 /* 17290 * Function: sd_sense_key_recoverable_error 17291 * 17292 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17293 * 17294 * Context: May be called from interrupt context 17295 */ 17296 17297 static void 17298 sd_sense_key_recoverable_error(struct sd_lun *un, 17299 uint8_t asc, 17300 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17301 { 17302 struct sd_sense_info si; 17303 17304 ASSERT(un != NULL); 17305 ASSERT(mutex_owned(SD_MUTEX(un))); 17306 ASSERT(bp != NULL); 17307 ASSERT(xp != NULL); 17308 ASSERT(pktp != NULL); 17309 17310 /* 17311 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17312 */ 17313 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17314 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17315 si.ssi_severity = SCSI_ERR_INFO; 17316 si.ssi_pfa_flag = TRUE; 17317 } else { 17318 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17319 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17320 si.ssi_severity = SCSI_ERR_RECOVERED; 17321 si.ssi_pfa_flag = FALSE; 17322 } 17323 17324 if (pktp->pkt_resid == 0) { 17325 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17326 sd_return_command(un, bp); 17327 return; 17328 } 17329 17330 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17331 &si, EIO, (clock_t)0, NULL); 17332 } 17333 17334 17335 17336 17337 /* 17338 * Function: sd_sense_key_not_ready 17339 * 17340 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17341 * 17342 * Context: May be called from interrupt context 17343 */ 17344 17345 static void 17346 sd_sense_key_not_ready(struct sd_lun *un, 17347 uint8_t asc, uint8_t ascq, 17348 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17349 { 17350 struct sd_sense_info si; 17351 17352 ASSERT(un != NULL); 17353 ASSERT(mutex_owned(SD_MUTEX(un))); 17354 ASSERT(bp != NULL); 17355 ASSERT(xp != NULL); 17356 ASSERT(pktp != NULL); 17357 17358 si.ssi_severity = SCSI_ERR_FATAL; 17359 si.ssi_pfa_flag = FALSE; 17360 17361 /* 17362 * Update error stats after first NOT READY error. Disks may have 17363 * been powered down and may need to be restarted. For CDROMs, 17364 * report NOT READY errors only if media is present. 17365 */ 17366 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17367 (xp->xb_retry_count > 0)) { 17368 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17369 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17370 } 17371 17372 /* 17373 * Just fail if the "not ready" retry limit has been reached. 17374 */ 17375 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17376 /* Special check for error message printing for removables. */ 17377 if (un->un_f_has_removable_media && (asc == 0x04) && 17378 (ascq >= 0x04)) { 17379 si.ssi_severity = SCSI_ERR_ALL; 17380 } 17381 goto fail_command; 17382 } 17383 17384 /* 17385 * Check the ASC and ASCQ in the sense data as needed, to determine 17386 * what to do. 17387 */ 17388 switch (asc) { 17389 case 0x04: /* LOGICAL UNIT NOT READY */ 17390 /* 17391 * disk drives that don't spin up result in a very long delay 17392 * in format without warning messages. We will log a message 17393 * if the error level is set to verbose. 17394 */ 17395 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17396 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17397 "logical unit not ready, resetting disk\n"); 17398 } 17399 17400 /* 17401 * There are different requirements for CDROMs and disks for 17402 * the number of retries. If a CD-ROM is giving this, it is 17403 * probably reading TOC and is in the process of getting 17404 * ready, so we should keep on trying for a long time to make 17405 * sure that all types of media are taken in account (for 17406 * some media the drive takes a long time to read TOC). For 17407 * disks we do not want to retry this too many times as this 17408 * can cause a long hang in format when the drive refuses to 17409 * spin up (a very common failure). 17410 */ 17411 switch (ascq) { 17412 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17413 /* 17414 * Disk drives frequently refuse to spin up which 17415 * results in a very long hang in format without 17416 * warning messages. 17417 * 17418 * Note: This code preserves the legacy behavior of 17419 * comparing xb_retry_count against zero for fibre 17420 * channel targets instead of comparing against the 17421 * un_reset_retry_count value. The reason for this 17422 * discrepancy has been so utterly lost beneath the 17423 * Sands of Time that even Indiana Jones could not 17424 * find it. 17425 */ 17426 if (un->un_f_is_fibre == TRUE) { 17427 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17428 (xp->xb_retry_count > 0)) && 17429 (un->un_startstop_timeid == NULL)) { 17430 scsi_log(SD_DEVINFO(un), sd_label, 17431 CE_WARN, "logical unit not ready, " 17432 "resetting disk\n"); 17433 sd_reset_target(un, pktp); 17434 } 17435 } else { 17436 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17437 (xp->xb_retry_count > 17438 un->un_reset_retry_count)) && 17439 (un->un_startstop_timeid == NULL)) { 17440 scsi_log(SD_DEVINFO(un), sd_label, 17441 CE_WARN, "logical unit not ready, " 17442 "resetting disk\n"); 17443 sd_reset_target(un, pktp); 17444 } 17445 } 17446 break; 17447 17448 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17449 /* 17450 * If the target is in the process of becoming 17451 * ready, just proceed with the retry. This can 17452 * happen with CD-ROMs that take a long time to 17453 * read TOC after a power cycle or reset. 17454 */ 17455 goto do_retry; 17456 17457 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17458 break; 17459 17460 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17461 /* 17462 * Retries cannot help here so just fail right away. 17463 */ 17464 goto fail_command; 17465 17466 case 0x88: 17467 /* 17468 * Vendor-unique code for T3/T4: it indicates a 17469 * path problem in a mutipathed config, but as far as 17470 * the target driver is concerned it equates to a fatal 17471 * error, so we should just fail the command right away 17472 * (without printing anything to the console). If this 17473 * is not a T3/T4, fall thru to the default recovery 17474 * action. 17475 * T3/T4 is FC only, don't need to check is_fibre 17476 */ 17477 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17478 sd_return_failed_command(un, bp, EIO); 17479 return; 17480 } 17481 /* FALLTHRU */ 17482 17483 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17484 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17485 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17486 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17487 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17488 default: /* Possible future codes in SCSI spec? */ 17489 /* 17490 * For removable-media devices, do not retry if 17491 * ASCQ > 2 as these result mostly from USCSI commands 17492 * on MMC devices issued to check status of an 17493 * operation initiated in immediate mode. Also for 17494 * ASCQ >= 4 do not print console messages as these 17495 * mainly represent a user-initiated operation 17496 * instead of a system failure. 17497 */ 17498 if (un->un_f_has_removable_media) { 17499 si.ssi_severity = SCSI_ERR_ALL; 17500 goto fail_command; 17501 } 17502 break; 17503 } 17504 17505 /* 17506 * As part of our recovery attempt for the NOT READY 17507 * condition, we issue a START STOP UNIT command. However 17508 * we want to wait for a short delay before attempting this 17509 * as there may still be more commands coming back from the 17510 * target with the check condition. To do this we use 17511 * timeout(9F) to call sd_start_stop_unit_callback() after 17512 * the delay interval expires. (sd_start_stop_unit_callback() 17513 * dispatches sd_start_stop_unit_task(), which will issue 17514 * the actual START STOP UNIT command. The delay interval 17515 * is one-half of the delay that we will use to retry the 17516 * command that generated the NOT READY condition. 17517 * 17518 * Note that we could just dispatch sd_start_stop_unit_task() 17519 * from here and allow it to sleep for the delay interval, 17520 * but then we would be tying up the taskq thread 17521 * uncesessarily for the duration of the delay. 17522 * 17523 * Do not issue the START STOP UNIT if the current command 17524 * is already a START STOP UNIT. 17525 */ 17526 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17527 break; 17528 } 17529 17530 /* 17531 * Do not schedule the timeout if one is already pending. 17532 */ 17533 if (un->un_startstop_timeid != NULL) { 17534 SD_INFO(SD_LOG_ERROR, un, 17535 "sd_sense_key_not_ready: restart already issued to" 17536 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17537 ddi_get_instance(SD_DEVINFO(un))); 17538 break; 17539 } 17540 17541 /* 17542 * Schedule the START STOP UNIT command, then queue the command 17543 * for a retry. 17544 * 17545 * Note: A timeout is not scheduled for this retry because we 17546 * want the retry to be serial with the START_STOP_UNIT. The 17547 * retry will be started when the START_STOP_UNIT is completed 17548 * in sd_start_stop_unit_task. 17549 */ 17550 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17551 un, SD_BSY_TIMEOUT / 2); 17552 xp->xb_retry_count++; 17553 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17554 return; 17555 17556 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17557 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17558 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17559 "unit does not respond to selection\n"); 17560 } 17561 break; 17562 17563 case 0x3A: /* MEDIUM NOT PRESENT */ 17564 if (sd_error_level >= SCSI_ERR_FATAL) { 17565 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17566 "Caddy not inserted in drive\n"); 17567 } 17568 17569 sr_ejected(un); 17570 un->un_mediastate = DKIO_EJECTED; 17571 /* The state has changed, inform the media watch routines */ 17572 cv_broadcast(&un->un_state_cv); 17573 /* Just fail if no media is present in the drive. */ 17574 goto fail_command; 17575 17576 default: 17577 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17578 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17579 "Unit not Ready. Additional sense code 0x%x\n", 17580 asc); 17581 } 17582 break; 17583 } 17584 17585 do_retry: 17586 17587 /* 17588 * Retry the command, as some targets may report NOT READY for 17589 * several seconds after being reset. 17590 */ 17591 xp->xb_retry_count++; 17592 si.ssi_severity = SCSI_ERR_RETRYABLE; 17593 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17594 &si, EIO, SD_BSY_TIMEOUT, NULL); 17595 17596 return; 17597 17598 fail_command: 17599 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17600 sd_return_failed_command(un, bp, EIO); 17601 } 17602 17603 17604 17605 /* 17606 * Function: sd_sense_key_medium_or_hardware_error 17607 * 17608 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17609 * sense key. 17610 * 17611 * Context: May be called from interrupt context 17612 */ 17613 17614 static void 17615 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17616 int sense_key, uint8_t asc, 17617 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17618 { 17619 struct sd_sense_info si; 17620 17621 ASSERT(un != NULL); 17622 ASSERT(mutex_owned(SD_MUTEX(un))); 17623 ASSERT(bp != NULL); 17624 ASSERT(xp != NULL); 17625 ASSERT(pktp != NULL); 17626 17627 si.ssi_severity = SCSI_ERR_FATAL; 17628 si.ssi_pfa_flag = FALSE; 17629 17630 if (sense_key == KEY_MEDIUM_ERROR) { 17631 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17632 } 17633 17634 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17635 17636 if ((un->un_reset_retry_count != 0) && 17637 (xp->xb_retry_count == un->un_reset_retry_count)) { 17638 mutex_exit(SD_MUTEX(un)); 17639 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17640 if (un->un_f_allow_bus_device_reset == TRUE) { 17641 17642 boolean_t try_resetting_target = B_TRUE; 17643 17644 /* 17645 * We need to be able to handle specific ASC when we are 17646 * handling a KEY_HARDWARE_ERROR. In particular 17647 * taking the default action of resetting the target may 17648 * not be the appropriate way to attempt recovery. 17649 * Resetting a target because of a single LUN failure 17650 * victimizes all LUNs on that target. 17651 * 17652 * This is true for the LSI arrays, if an LSI 17653 * array controller returns an ASC of 0x84 (LUN Dead) we 17654 * should trust it. 17655 */ 17656 17657 if (sense_key == KEY_HARDWARE_ERROR) { 17658 switch (asc) { 17659 case 0x84: 17660 if (SD_IS_LSI(un)) { 17661 try_resetting_target = B_FALSE; 17662 } 17663 break; 17664 default: 17665 break; 17666 } 17667 } 17668 17669 if (try_resetting_target == B_TRUE) { 17670 int reset_retval = 0; 17671 if (un->un_f_lun_reset_enabled == TRUE) { 17672 SD_TRACE(SD_LOG_IO_CORE, un, 17673 "sd_sense_key_medium_or_hardware_" 17674 "error: issuing RESET_LUN\n"); 17675 reset_retval = 17676 scsi_reset(SD_ADDRESS(un), 17677 RESET_LUN); 17678 } 17679 if (reset_retval == 0) { 17680 SD_TRACE(SD_LOG_IO_CORE, un, 17681 "sd_sense_key_medium_or_hardware_" 17682 "error: issuing RESET_TARGET\n"); 17683 (void) scsi_reset(SD_ADDRESS(un), 17684 RESET_TARGET); 17685 } 17686 } 17687 } 17688 mutex_enter(SD_MUTEX(un)); 17689 } 17690 17691 /* 17692 * This really ought to be a fatal error, but we will retry anyway 17693 * as some drives report this as a spurious error. 17694 */ 17695 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17696 &si, EIO, (clock_t)0, NULL); 17697 } 17698 17699 17700 17701 /* 17702 * Function: sd_sense_key_illegal_request 17703 * 17704 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17705 * 17706 * Context: May be called from interrupt context 17707 */ 17708 17709 static void 17710 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17711 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17712 { 17713 struct sd_sense_info si; 17714 17715 ASSERT(un != NULL); 17716 ASSERT(mutex_owned(SD_MUTEX(un))); 17717 ASSERT(bp != NULL); 17718 ASSERT(xp != NULL); 17719 ASSERT(pktp != NULL); 17720 17721 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17722 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17723 17724 si.ssi_severity = SCSI_ERR_INFO; 17725 si.ssi_pfa_flag = FALSE; 17726 17727 /* Pointless to retry if the target thinks it's an illegal request */ 17728 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17729 sd_return_failed_command(un, bp, EIO); 17730 } 17731 17732 17733 17734 17735 /* 17736 * Function: sd_sense_key_unit_attention 17737 * 17738 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17739 * 17740 * Context: May be called from interrupt context 17741 */ 17742 17743 static void 17744 sd_sense_key_unit_attention(struct sd_lun *un, 17745 uint8_t asc, 17746 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17747 { 17748 /* 17749 * For UNIT ATTENTION we allow retries for one minute. Devices 17750 * like Sonoma can return UNIT ATTENTION close to a minute 17751 * under certain conditions. 17752 */ 17753 int retry_check_flag = SD_RETRIES_UA; 17754 boolean_t kstat_updated = B_FALSE; 17755 struct sd_sense_info si; 17756 17757 ASSERT(un != NULL); 17758 ASSERT(mutex_owned(SD_MUTEX(un))); 17759 ASSERT(bp != NULL); 17760 ASSERT(xp != NULL); 17761 ASSERT(pktp != NULL); 17762 17763 si.ssi_severity = SCSI_ERR_INFO; 17764 si.ssi_pfa_flag = FALSE; 17765 17766 17767 switch (asc) { 17768 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17769 if (sd_report_pfa != 0) { 17770 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17771 si.ssi_pfa_flag = TRUE; 17772 retry_check_flag = SD_RETRIES_STANDARD; 17773 goto do_retry; 17774 } 17775 break; 17776 17777 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17778 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17779 un->un_resvd_status |= 17780 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17781 } 17782 /* FALLTHRU */ 17783 17784 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17785 if (!un->un_f_has_removable_media) { 17786 break; 17787 } 17788 17789 /* 17790 * When we get a unit attention from a removable-media device, 17791 * it may be in a state that will take a long time to recover 17792 * (e.g., from a reset). Since we are executing in interrupt 17793 * context here, we cannot wait around for the device to come 17794 * back. So hand this command off to sd_media_change_task() 17795 * for deferred processing under taskq thread context. (Note 17796 * that the command still may be failed if a problem is 17797 * encountered at a later time.) 17798 */ 17799 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17800 KM_NOSLEEP) == 0) { 17801 /* 17802 * Cannot dispatch the request so fail the command. 17803 */ 17804 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17805 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17806 si.ssi_severity = SCSI_ERR_FATAL; 17807 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17808 sd_return_failed_command(un, bp, EIO); 17809 } 17810 17811 /* 17812 * If failed to dispatch sd_media_change_task(), we already 17813 * updated kstat. If succeed to dispatch sd_media_change_task(), 17814 * we should update kstat later if it encounters an error. So, 17815 * we update kstat_updated flag here. 17816 */ 17817 kstat_updated = B_TRUE; 17818 17819 /* 17820 * Either the command has been successfully dispatched to a 17821 * task Q for retrying, or the dispatch failed. In either case 17822 * do NOT retry again by calling sd_retry_command. This sets up 17823 * two retries of the same command and when one completes and 17824 * frees the resources the other will access freed memory, 17825 * a bad thing. 17826 */ 17827 return; 17828 17829 default: 17830 break; 17831 } 17832 17833 /* 17834 * Update kstat if we haven't done that. 17835 */ 17836 if (!kstat_updated) { 17837 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17838 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17839 } 17840 17841 do_retry: 17842 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17843 EIO, SD_UA_RETRY_DELAY, NULL); 17844 } 17845 17846 17847 17848 /* 17849 * Function: sd_sense_key_fail_command 17850 * 17851 * Description: Use to fail a command when we don't like the sense key that 17852 * was returned. 17853 * 17854 * Context: May be called from interrupt context 17855 */ 17856 17857 static void 17858 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17859 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17860 { 17861 struct sd_sense_info si; 17862 17863 ASSERT(un != NULL); 17864 ASSERT(mutex_owned(SD_MUTEX(un))); 17865 ASSERT(bp != NULL); 17866 ASSERT(xp != NULL); 17867 ASSERT(pktp != NULL); 17868 17869 si.ssi_severity = SCSI_ERR_FATAL; 17870 si.ssi_pfa_flag = FALSE; 17871 17872 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17873 sd_return_failed_command(un, bp, EIO); 17874 } 17875 17876 17877 17878 /* 17879 * Function: sd_sense_key_blank_check 17880 * 17881 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17882 * Has no monetary connotation. 17883 * 17884 * Context: May be called from interrupt context 17885 */ 17886 17887 static void 17888 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17889 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17890 { 17891 struct sd_sense_info si; 17892 17893 ASSERT(un != NULL); 17894 ASSERT(mutex_owned(SD_MUTEX(un))); 17895 ASSERT(bp != NULL); 17896 ASSERT(xp != NULL); 17897 ASSERT(pktp != NULL); 17898 17899 /* 17900 * Blank check is not fatal for removable devices, therefore 17901 * it does not require a console message. 17902 */ 17903 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 17904 SCSI_ERR_FATAL; 17905 si.ssi_pfa_flag = FALSE; 17906 17907 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17908 sd_return_failed_command(un, bp, EIO); 17909 } 17910 17911 17912 17913 17914 /* 17915 * Function: sd_sense_key_aborted_command 17916 * 17917 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 17918 * 17919 * Context: May be called from interrupt context 17920 */ 17921 17922 static void 17923 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 17924 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17925 { 17926 struct sd_sense_info si; 17927 17928 ASSERT(un != NULL); 17929 ASSERT(mutex_owned(SD_MUTEX(un))); 17930 ASSERT(bp != NULL); 17931 ASSERT(xp != NULL); 17932 ASSERT(pktp != NULL); 17933 17934 si.ssi_severity = SCSI_ERR_FATAL; 17935 si.ssi_pfa_flag = FALSE; 17936 17937 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17938 17939 /* 17940 * This really ought to be a fatal error, but we will retry anyway 17941 * as some drives report this as a spurious error. 17942 */ 17943 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17944 &si, EIO, (clock_t)0, NULL); 17945 } 17946 17947 17948 17949 /* 17950 * Function: sd_sense_key_default 17951 * 17952 * Description: Default recovery action for several SCSI sense keys (basically 17953 * attempts a retry). 17954 * 17955 * Context: May be called from interrupt context 17956 */ 17957 17958 static void 17959 sd_sense_key_default(struct sd_lun *un, 17960 int sense_key, 17961 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17962 { 17963 struct sd_sense_info si; 17964 17965 ASSERT(un != NULL); 17966 ASSERT(mutex_owned(SD_MUTEX(un))); 17967 ASSERT(bp != NULL); 17968 ASSERT(xp != NULL); 17969 ASSERT(pktp != NULL); 17970 17971 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17972 17973 /* 17974 * Undecoded sense key. Attempt retries and hope that will fix 17975 * the problem. Otherwise, we're dead. 17976 */ 17977 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17978 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17979 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 17980 } 17981 17982 si.ssi_severity = SCSI_ERR_FATAL; 17983 si.ssi_pfa_flag = FALSE; 17984 17985 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17986 &si, EIO, (clock_t)0, NULL); 17987 } 17988 17989 17990 17991 /* 17992 * Function: sd_print_retry_msg 17993 * 17994 * Description: Print a message indicating the retry action being taken. 17995 * 17996 * Arguments: un - ptr to associated softstate 17997 * bp - ptr to buf(9S) for the command 17998 * arg - not used. 17999 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18000 * or SD_NO_RETRY_ISSUED 18001 * 18002 * Context: May be called from interrupt context 18003 */ 18004 /* ARGSUSED */ 18005 static void 18006 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18007 { 18008 struct sd_xbuf *xp; 18009 struct scsi_pkt *pktp; 18010 char *reasonp; 18011 char *msgp; 18012 18013 ASSERT(un != NULL); 18014 ASSERT(mutex_owned(SD_MUTEX(un))); 18015 ASSERT(bp != NULL); 18016 pktp = SD_GET_PKTP(bp); 18017 ASSERT(pktp != NULL); 18018 xp = SD_GET_XBUF(bp); 18019 ASSERT(xp != NULL); 18020 18021 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18022 mutex_enter(&un->un_pm_mutex); 18023 if ((un->un_state == SD_STATE_SUSPENDED) || 18024 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18025 (pktp->pkt_flags & FLAG_SILENT)) { 18026 mutex_exit(&un->un_pm_mutex); 18027 goto update_pkt_reason; 18028 } 18029 mutex_exit(&un->un_pm_mutex); 18030 18031 /* 18032 * Suppress messages if they are all the same pkt_reason; with 18033 * TQ, many (up to 256) are returned with the same pkt_reason. 18034 * If we are in panic, then suppress the retry messages. 18035 */ 18036 switch (flag) { 18037 case SD_NO_RETRY_ISSUED: 18038 msgp = "giving up"; 18039 break; 18040 case SD_IMMEDIATE_RETRY_ISSUED: 18041 case SD_DELAYED_RETRY_ISSUED: 18042 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18043 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18044 (sd_error_level != SCSI_ERR_ALL))) { 18045 return; 18046 } 18047 msgp = "retrying command"; 18048 break; 18049 default: 18050 goto update_pkt_reason; 18051 } 18052 18053 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18054 scsi_rname(pktp->pkt_reason)); 18055 18056 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18057 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18058 18059 update_pkt_reason: 18060 /* 18061 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18062 * This is to prevent multiple console messages for the same failure 18063 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18064 * when the command is retried successfully because there still may be 18065 * more commands coming back with the same value of pktp->pkt_reason. 18066 */ 18067 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18068 un->un_last_pkt_reason = pktp->pkt_reason; 18069 } 18070 } 18071 18072 18073 /* 18074 * Function: sd_print_cmd_incomplete_msg 18075 * 18076 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18077 * 18078 * Arguments: un - ptr to associated softstate 18079 * bp - ptr to buf(9S) for the command 18080 * arg - passed to sd_print_retry_msg() 18081 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18082 * or SD_NO_RETRY_ISSUED 18083 * 18084 * Context: May be called from interrupt context 18085 */ 18086 18087 static void 18088 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18089 int code) 18090 { 18091 dev_info_t *dip; 18092 18093 ASSERT(un != NULL); 18094 ASSERT(mutex_owned(SD_MUTEX(un))); 18095 ASSERT(bp != NULL); 18096 18097 switch (code) { 18098 case SD_NO_RETRY_ISSUED: 18099 /* Command was failed. Someone turned off this target? */ 18100 if (un->un_state != SD_STATE_OFFLINE) { 18101 /* 18102 * Suppress message if we are detaching and 18103 * device has been disconnected 18104 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18105 * private interface and not part of the DDI 18106 */ 18107 dip = un->un_sd->sd_dev; 18108 if (!(DEVI_IS_DETACHING(dip) && 18109 DEVI_IS_DEVICE_REMOVED(dip))) { 18110 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18111 "disk not responding to selection\n"); 18112 } 18113 New_state(un, SD_STATE_OFFLINE); 18114 } 18115 break; 18116 18117 case SD_DELAYED_RETRY_ISSUED: 18118 case SD_IMMEDIATE_RETRY_ISSUED: 18119 default: 18120 /* Command was successfully queued for retry */ 18121 sd_print_retry_msg(un, bp, arg, code); 18122 break; 18123 } 18124 } 18125 18126 18127 /* 18128 * Function: sd_pkt_reason_cmd_incomplete 18129 * 18130 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18131 * 18132 * Context: May be called from interrupt context 18133 */ 18134 18135 static void 18136 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18137 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18138 { 18139 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18140 18141 ASSERT(un != NULL); 18142 ASSERT(mutex_owned(SD_MUTEX(un))); 18143 ASSERT(bp != NULL); 18144 ASSERT(xp != NULL); 18145 ASSERT(pktp != NULL); 18146 18147 /* Do not do a reset if selection did not complete */ 18148 /* Note: Should this not just check the bit? */ 18149 if (pktp->pkt_state != STATE_GOT_BUS) { 18150 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18151 sd_reset_target(un, pktp); 18152 } 18153 18154 /* 18155 * If the target was not successfully selected, then set 18156 * SD_RETRIES_FAILFAST to indicate that we lost communication 18157 * with the target, and further retries and/or commands are 18158 * likely to take a long time. 18159 */ 18160 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18161 flag |= SD_RETRIES_FAILFAST; 18162 } 18163 18164 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18165 18166 sd_retry_command(un, bp, flag, 18167 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18168 } 18169 18170 18171 18172 /* 18173 * Function: sd_pkt_reason_cmd_tran_err 18174 * 18175 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18176 * 18177 * Context: May be called from interrupt context 18178 */ 18179 18180 static void 18181 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18182 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18183 { 18184 ASSERT(un != NULL); 18185 ASSERT(mutex_owned(SD_MUTEX(un))); 18186 ASSERT(bp != NULL); 18187 ASSERT(xp != NULL); 18188 ASSERT(pktp != NULL); 18189 18190 /* 18191 * Do not reset if we got a parity error, or if 18192 * selection did not complete. 18193 */ 18194 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18195 /* Note: Should this not just check the bit for pkt_state? */ 18196 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18197 (pktp->pkt_state != STATE_GOT_BUS)) { 18198 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18199 sd_reset_target(un, pktp); 18200 } 18201 18202 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18203 18204 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18205 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18206 } 18207 18208 18209 18210 /* 18211 * Function: sd_pkt_reason_cmd_reset 18212 * 18213 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18214 * 18215 * Context: May be called from interrupt context 18216 */ 18217 18218 static void 18219 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18220 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18221 { 18222 ASSERT(un != NULL); 18223 ASSERT(mutex_owned(SD_MUTEX(un))); 18224 ASSERT(bp != NULL); 18225 ASSERT(xp != NULL); 18226 ASSERT(pktp != NULL); 18227 18228 /* The target may still be running the command, so try to reset. */ 18229 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18230 sd_reset_target(un, pktp); 18231 18232 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18233 18234 /* 18235 * If pkt_reason is CMD_RESET chances are that this pkt got 18236 * reset because another target on this bus caused it. The target 18237 * that caused it should get CMD_TIMEOUT with pkt_statistics 18238 * of STAT_TIMEOUT/STAT_DEV_RESET. 18239 */ 18240 18241 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18242 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18243 } 18244 18245 18246 18247 18248 /* 18249 * Function: sd_pkt_reason_cmd_aborted 18250 * 18251 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18252 * 18253 * Context: May be called from interrupt context 18254 */ 18255 18256 static void 18257 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18258 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18259 { 18260 ASSERT(un != NULL); 18261 ASSERT(mutex_owned(SD_MUTEX(un))); 18262 ASSERT(bp != NULL); 18263 ASSERT(xp != NULL); 18264 ASSERT(pktp != NULL); 18265 18266 /* The target may still be running the command, so try to reset. */ 18267 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18268 sd_reset_target(un, pktp); 18269 18270 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18271 18272 /* 18273 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18274 * aborted because another target on this bus caused it. The target 18275 * that caused it should get CMD_TIMEOUT with pkt_statistics 18276 * of STAT_TIMEOUT/STAT_DEV_RESET. 18277 */ 18278 18279 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18280 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18281 } 18282 18283 18284 18285 /* 18286 * Function: sd_pkt_reason_cmd_timeout 18287 * 18288 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18289 * 18290 * Context: May be called from interrupt context 18291 */ 18292 18293 static void 18294 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18295 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18296 { 18297 ASSERT(un != NULL); 18298 ASSERT(mutex_owned(SD_MUTEX(un))); 18299 ASSERT(bp != NULL); 18300 ASSERT(xp != NULL); 18301 ASSERT(pktp != NULL); 18302 18303 18304 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18305 sd_reset_target(un, pktp); 18306 18307 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18308 18309 /* 18310 * A command timeout indicates that we could not establish 18311 * communication with the target, so set SD_RETRIES_FAILFAST 18312 * as further retries/commands are likely to take a long time. 18313 */ 18314 sd_retry_command(un, bp, 18315 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18316 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18317 } 18318 18319 18320 18321 /* 18322 * Function: sd_pkt_reason_cmd_unx_bus_free 18323 * 18324 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18325 * 18326 * Context: May be called from interrupt context 18327 */ 18328 18329 static void 18330 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18331 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18332 { 18333 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18334 18335 ASSERT(un != NULL); 18336 ASSERT(mutex_owned(SD_MUTEX(un))); 18337 ASSERT(bp != NULL); 18338 ASSERT(xp != NULL); 18339 ASSERT(pktp != NULL); 18340 18341 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18342 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18343 18344 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18345 sd_print_retry_msg : NULL; 18346 18347 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18348 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18349 } 18350 18351 18352 /* 18353 * Function: sd_pkt_reason_cmd_tag_reject 18354 * 18355 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18356 * 18357 * Context: May be called from interrupt context 18358 */ 18359 18360 static void 18361 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18362 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18363 { 18364 ASSERT(un != NULL); 18365 ASSERT(mutex_owned(SD_MUTEX(un))); 18366 ASSERT(bp != NULL); 18367 ASSERT(xp != NULL); 18368 ASSERT(pktp != NULL); 18369 18370 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18371 pktp->pkt_flags = 0; 18372 un->un_tagflags = 0; 18373 if (un->un_f_opt_queueing == TRUE) { 18374 un->un_throttle = min(un->un_throttle, 3); 18375 } else { 18376 un->un_throttle = 1; 18377 } 18378 mutex_exit(SD_MUTEX(un)); 18379 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18380 mutex_enter(SD_MUTEX(un)); 18381 18382 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18383 18384 /* Legacy behavior not to check retry counts here. */ 18385 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18386 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18387 } 18388 18389 18390 /* 18391 * Function: sd_pkt_reason_default 18392 * 18393 * Description: Default recovery actions for SCSA pkt_reason values that 18394 * do not have more explicit recovery actions. 18395 * 18396 * Context: May be called from interrupt context 18397 */ 18398 18399 static void 18400 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18401 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18402 { 18403 ASSERT(un != NULL); 18404 ASSERT(mutex_owned(SD_MUTEX(un))); 18405 ASSERT(bp != NULL); 18406 ASSERT(xp != NULL); 18407 ASSERT(pktp != NULL); 18408 18409 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18410 sd_reset_target(un, pktp); 18411 18412 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18413 18414 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18415 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18416 } 18417 18418 18419 18420 /* 18421 * Function: sd_pkt_status_check_condition 18422 * 18423 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18424 * 18425 * Context: May be called from interrupt context 18426 */ 18427 18428 static void 18429 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18430 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18431 { 18432 ASSERT(un != NULL); 18433 ASSERT(mutex_owned(SD_MUTEX(un))); 18434 ASSERT(bp != NULL); 18435 ASSERT(xp != NULL); 18436 ASSERT(pktp != NULL); 18437 18438 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18439 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18440 18441 /* 18442 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18443 * command will be retried after the request sense). Otherwise, retry 18444 * the command. Note: we are issuing the request sense even though the 18445 * retry limit may have been reached for the failed command. 18446 */ 18447 if (un->un_f_arq_enabled == FALSE) { 18448 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18449 "no ARQ, sending request sense command\n"); 18450 sd_send_request_sense_command(un, bp, pktp); 18451 } else { 18452 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18453 "ARQ,retrying request sense command\n"); 18454 #if defined(__i386) || defined(__amd64) 18455 /* 18456 * The SD_RETRY_DELAY value need to be adjusted here 18457 * when SD_RETRY_DELAY change in sddef.h 18458 */ 18459 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18460 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18461 NULL); 18462 #else 18463 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18464 EIO, SD_RETRY_DELAY, NULL); 18465 #endif 18466 } 18467 18468 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18469 } 18470 18471 18472 /* 18473 * Function: sd_pkt_status_busy 18474 * 18475 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18476 * 18477 * Context: May be called from interrupt context 18478 */ 18479 18480 static void 18481 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18482 struct scsi_pkt *pktp) 18483 { 18484 ASSERT(un != NULL); 18485 ASSERT(mutex_owned(SD_MUTEX(un))); 18486 ASSERT(bp != NULL); 18487 ASSERT(xp != NULL); 18488 ASSERT(pktp != NULL); 18489 18490 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18491 "sd_pkt_status_busy: entry\n"); 18492 18493 /* If retries are exhausted, just fail the command. */ 18494 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18495 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18496 "device busy too long\n"); 18497 sd_return_failed_command(un, bp, EIO); 18498 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18499 "sd_pkt_status_busy: exit\n"); 18500 return; 18501 } 18502 xp->xb_retry_count++; 18503 18504 /* 18505 * Try to reset the target. However, we do not want to perform 18506 * more than one reset if the device continues to fail. The reset 18507 * will be performed when the retry count reaches the reset 18508 * threshold. This threshold should be set such that at least 18509 * one retry is issued before the reset is performed. 18510 */ 18511 if (xp->xb_retry_count == 18512 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18513 int rval = 0; 18514 mutex_exit(SD_MUTEX(un)); 18515 if (un->un_f_allow_bus_device_reset == TRUE) { 18516 /* 18517 * First try to reset the LUN; if we cannot then 18518 * try to reset the target. 18519 */ 18520 if (un->un_f_lun_reset_enabled == TRUE) { 18521 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18522 "sd_pkt_status_busy: RESET_LUN\n"); 18523 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18524 } 18525 if (rval == 0) { 18526 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18527 "sd_pkt_status_busy: RESET_TARGET\n"); 18528 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18529 } 18530 } 18531 if (rval == 0) { 18532 /* 18533 * If the RESET_LUN and/or RESET_TARGET failed, 18534 * try RESET_ALL 18535 */ 18536 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18537 "sd_pkt_status_busy: RESET_ALL\n"); 18538 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18539 } 18540 mutex_enter(SD_MUTEX(un)); 18541 if (rval == 0) { 18542 /* 18543 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18544 * At this point we give up & fail the command. 18545 */ 18546 sd_return_failed_command(un, bp, EIO); 18547 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18548 "sd_pkt_status_busy: exit (failed cmd)\n"); 18549 return; 18550 } 18551 } 18552 18553 /* 18554 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18555 * we have already checked the retry counts above. 18556 */ 18557 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18558 EIO, SD_BSY_TIMEOUT, NULL); 18559 18560 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18561 "sd_pkt_status_busy: exit\n"); 18562 } 18563 18564 18565 /* 18566 * Function: sd_pkt_status_reservation_conflict 18567 * 18568 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18569 * command status. 18570 * 18571 * Context: May be called from interrupt context 18572 */ 18573 18574 static void 18575 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18576 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18577 { 18578 ASSERT(un != NULL); 18579 ASSERT(mutex_owned(SD_MUTEX(un))); 18580 ASSERT(bp != NULL); 18581 ASSERT(xp != NULL); 18582 ASSERT(pktp != NULL); 18583 18584 /* 18585 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18586 * conflict could be due to various reasons like incorrect keys, not 18587 * registered or not reserved etc. So, we return EACCES to the caller. 18588 */ 18589 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18590 int cmd = SD_GET_PKT_OPCODE(pktp); 18591 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18592 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18593 sd_return_failed_command(un, bp, EACCES); 18594 return; 18595 } 18596 } 18597 18598 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18599 18600 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18601 if (sd_failfast_enable != 0) { 18602 /* By definition, we must panic here.... */ 18603 sd_panic_for_res_conflict(un); 18604 /*NOTREACHED*/ 18605 } 18606 SD_ERROR(SD_LOG_IO, un, 18607 "sd_handle_resv_conflict: Disk Reserved\n"); 18608 sd_return_failed_command(un, bp, EACCES); 18609 return; 18610 } 18611 18612 /* 18613 * 1147670: retry only if sd_retry_on_reservation_conflict 18614 * property is set (default is 1). Retries will not succeed 18615 * on a disk reserved by another initiator. HA systems 18616 * may reset this via sd.conf to avoid these retries. 18617 * 18618 * Note: The legacy return code for this failure is EIO, however EACCES 18619 * seems more appropriate for a reservation conflict. 18620 */ 18621 if (sd_retry_on_reservation_conflict == 0) { 18622 SD_ERROR(SD_LOG_IO, un, 18623 "sd_handle_resv_conflict: Device Reserved\n"); 18624 sd_return_failed_command(un, bp, EIO); 18625 return; 18626 } 18627 18628 /* 18629 * Retry the command if we can. 18630 * 18631 * Note: The legacy return code for this failure is EIO, however EACCES 18632 * seems more appropriate for a reservation conflict. 18633 */ 18634 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18635 (clock_t)2, NULL); 18636 } 18637 18638 18639 18640 /* 18641 * Function: sd_pkt_status_qfull 18642 * 18643 * Description: Handle a QUEUE FULL condition from the target. This can 18644 * occur if the HBA does not handle the queue full condition. 18645 * (Basically this means third-party HBAs as Sun HBAs will 18646 * handle the queue full condition.) Note that if there are 18647 * some commands already in the transport, then the queue full 18648 * has occurred because the queue for this nexus is actually 18649 * full. If there are no commands in the transport, then the 18650 * queue full is resulting from some other initiator or lun 18651 * consuming all the resources at the target. 18652 * 18653 * Context: May be called from interrupt context 18654 */ 18655 18656 static void 18657 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18658 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18659 { 18660 ASSERT(un != NULL); 18661 ASSERT(mutex_owned(SD_MUTEX(un))); 18662 ASSERT(bp != NULL); 18663 ASSERT(xp != NULL); 18664 ASSERT(pktp != NULL); 18665 18666 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18667 "sd_pkt_status_qfull: entry\n"); 18668 18669 /* 18670 * Just lower the QFULL throttle and retry the command. Note that 18671 * we do not limit the number of retries here. 18672 */ 18673 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18674 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18675 SD_RESTART_TIMEOUT, NULL); 18676 18677 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18678 "sd_pkt_status_qfull: exit\n"); 18679 } 18680 18681 18682 /* 18683 * Function: sd_reset_target 18684 * 18685 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18686 * RESET_TARGET, or RESET_ALL. 18687 * 18688 * Context: May be called under interrupt context. 18689 */ 18690 18691 static void 18692 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18693 { 18694 int rval = 0; 18695 18696 ASSERT(un != NULL); 18697 ASSERT(mutex_owned(SD_MUTEX(un))); 18698 ASSERT(pktp != NULL); 18699 18700 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18701 18702 /* 18703 * No need to reset if the transport layer has already done so. 18704 */ 18705 if ((pktp->pkt_statistics & 18706 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18707 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18708 "sd_reset_target: no reset\n"); 18709 return; 18710 } 18711 18712 mutex_exit(SD_MUTEX(un)); 18713 18714 if (un->un_f_allow_bus_device_reset == TRUE) { 18715 if (un->un_f_lun_reset_enabled == TRUE) { 18716 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18717 "sd_reset_target: RESET_LUN\n"); 18718 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18719 } 18720 if (rval == 0) { 18721 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18722 "sd_reset_target: RESET_TARGET\n"); 18723 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18724 } 18725 } 18726 18727 if (rval == 0) { 18728 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18729 "sd_reset_target: RESET_ALL\n"); 18730 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18731 } 18732 18733 mutex_enter(SD_MUTEX(un)); 18734 18735 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18736 } 18737 18738 18739 /* 18740 * Function: sd_media_change_task 18741 * 18742 * Description: Recovery action for CDROM to become available. 18743 * 18744 * Context: Executes in a taskq() thread context 18745 */ 18746 18747 static void 18748 sd_media_change_task(void *arg) 18749 { 18750 struct scsi_pkt *pktp = arg; 18751 struct sd_lun *un; 18752 struct buf *bp; 18753 struct sd_xbuf *xp; 18754 int err = 0; 18755 int retry_count = 0; 18756 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18757 struct sd_sense_info si; 18758 18759 ASSERT(pktp != NULL); 18760 bp = (struct buf *)pktp->pkt_private; 18761 ASSERT(bp != NULL); 18762 xp = SD_GET_XBUF(bp); 18763 ASSERT(xp != NULL); 18764 un = SD_GET_UN(bp); 18765 ASSERT(un != NULL); 18766 ASSERT(!mutex_owned(SD_MUTEX(un))); 18767 ASSERT(un->un_f_monitor_media_state); 18768 18769 si.ssi_severity = SCSI_ERR_INFO; 18770 si.ssi_pfa_flag = FALSE; 18771 18772 /* 18773 * When a reset is issued on a CDROM, it takes a long time to 18774 * recover. First few attempts to read capacity and other things 18775 * related to handling unit attention fail (with a ASC 0x4 and 18776 * ASCQ 0x1). In that case we want to do enough retries and we want 18777 * to limit the retries in other cases of genuine failures like 18778 * no media in drive. 18779 */ 18780 while (retry_count++ < retry_limit) { 18781 if ((err = sd_handle_mchange(un)) == 0) { 18782 break; 18783 } 18784 if (err == EAGAIN) { 18785 retry_limit = SD_UNIT_ATTENTION_RETRY; 18786 } 18787 /* Sleep for 0.5 sec. & try again */ 18788 delay(drv_usectohz(500000)); 18789 } 18790 18791 /* 18792 * Dispatch (retry or fail) the original command here, 18793 * along with appropriate console messages.... 18794 * 18795 * Must grab the mutex before calling sd_retry_command, 18796 * sd_print_sense_msg and sd_return_failed_command. 18797 */ 18798 mutex_enter(SD_MUTEX(un)); 18799 if (err != SD_CMD_SUCCESS) { 18800 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18801 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18802 si.ssi_severity = SCSI_ERR_FATAL; 18803 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18804 sd_return_failed_command(un, bp, EIO); 18805 } else { 18806 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18807 &si, EIO, (clock_t)0, NULL); 18808 } 18809 mutex_exit(SD_MUTEX(un)); 18810 } 18811 18812 18813 18814 /* 18815 * Function: sd_handle_mchange 18816 * 18817 * Description: Perform geometry validation & other recovery when CDROM 18818 * has been removed from drive. 18819 * 18820 * Return Code: 0 for success 18821 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18822 * sd_send_scsi_READ_CAPACITY() 18823 * 18824 * Context: Executes in a taskq() thread context 18825 */ 18826 18827 static int 18828 sd_handle_mchange(struct sd_lun *un) 18829 { 18830 uint64_t capacity; 18831 uint32_t lbasize; 18832 int rval; 18833 18834 ASSERT(!mutex_owned(SD_MUTEX(un))); 18835 ASSERT(un->un_f_monitor_media_state); 18836 18837 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18838 SD_PATH_DIRECT_PRIORITY)) != 0) { 18839 return (rval); 18840 } 18841 18842 mutex_enter(SD_MUTEX(un)); 18843 sd_update_block_info(un, lbasize, capacity); 18844 18845 if (un->un_errstats != NULL) { 18846 struct sd_errstats *stp = 18847 (struct sd_errstats *)un->un_errstats->ks_data; 18848 stp->sd_capacity.value.ui64 = (uint64_t) 18849 ((uint64_t)un->un_blockcount * 18850 (uint64_t)un->un_tgt_blocksize); 18851 } 18852 18853 /* 18854 * Note: Maybe let the strategy/partitioning chain worry about getting 18855 * valid geometry. 18856 */ 18857 un->un_f_geometry_is_valid = FALSE; 18858 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 18859 if (un->un_f_geometry_is_valid == FALSE) { 18860 mutex_exit(SD_MUTEX(un)); 18861 return (EIO); 18862 } 18863 18864 mutex_exit(SD_MUTEX(un)); 18865 18866 /* 18867 * Try to lock the door 18868 */ 18869 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 18870 SD_PATH_DIRECT_PRIORITY)); 18871 } 18872 18873 18874 /* 18875 * Function: sd_send_scsi_DOORLOCK 18876 * 18877 * Description: Issue the scsi DOOR LOCK command 18878 * 18879 * Arguments: un - pointer to driver soft state (unit) structure for 18880 * this target. 18881 * flag - SD_REMOVAL_ALLOW 18882 * SD_REMOVAL_PREVENT 18883 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18884 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18885 * to use the USCSI "direct" chain and bypass the normal 18886 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18887 * command is issued as part of an error recovery action. 18888 * 18889 * Return Code: 0 - Success 18890 * errno return code from sd_send_scsi_cmd() 18891 * 18892 * Context: Can sleep. 18893 */ 18894 18895 static int 18896 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 18897 { 18898 union scsi_cdb cdb; 18899 struct uscsi_cmd ucmd_buf; 18900 struct scsi_extended_sense sense_buf; 18901 int status; 18902 18903 ASSERT(un != NULL); 18904 ASSERT(!mutex_owned(SD_MUTEX(un))); 18905 18906 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 18907 18908 /* already determined doorlock is not supported, fake success */ 18909 if (un->un_f_doorlock_supported == FALSE) { 18910 return (0); 18911 } 18912 18913 bzero(&cdb, sizeof (cdb)); 18914 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18915 18916 cdb.scc_cmd = SCMD_DOORLOCK; 18917 cdb.cdb_opaque[4] = (uchar_t)flag; 18918 18919 ucmd_buf.uscsi_cdb = (char *)&cdb; 18920 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18921 ucmd_buf.uscsi_bufaddr = NULL; 18922 ucmd_buf.uscsi_buflen = 0; 18923 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18924 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18925 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18926 ucmd_buf.uscsi_timeout = 15; 18927 18928 SD_TRACE(SD_LOG_IO, un, 18929 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 18930 18931 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18932 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18933 18934 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 18935 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18936 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 18937 /* fake success and skip subsequent doorlock commands */ 18938 un->un_f_doorlock_supported = FALSE; 18939 return (0); 18940 } 18941 18942 return (status); 18943 } 18944 18945 /* 18946 * Function: sd_send_scsi_READ_CAPACITY 18947 * 18948 * Description: This routine uses the scsi READ CAPACITY command to determine 18949 * the device capacity in number of blocks and the device native 18950 * block size. If this function returns a failure, then the 18951 * values in *capp and *lbap are undefined. If the capacity 18952 * returned is 0xffffffff then the lun is too large for a 18953 * normal READ CAPACITY command and the results of a 18954 * READ CAPACITY 16 will be used instead. 18955 * 18956 * Arguments: un - ptr to soft state struct for the target 18957 * capp - ptr to unsigned 64-bit variable to receive the 18958 * capacity value from the command. 18959 * lbap - ptr to unsigned 32-bit varaible to receive the 18960 * block size value from the command 18961 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18962 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18963 * to use the USCSI "direct" chain and bypass the normal 18964 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18965 * command is issued as part of an error recovery action. 18966 * 18967 * Return Code: 0 - Success 18968 * EIO - IO error 18969 * EACCES - Reservation conflict detected 18970 * EAGAIN - Device is becoming ready 18971 * errno return code from sd_send_scsi_cmd() 18972 * 18973 * Context: Can sleep. Blocks until command completes. 18974 */ 18975 18976 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 18977 18978 static int 18979 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 18980 int path_flag) 18981 { 18982 struct scsi_extended_sense sense_buf; 18983 struct uscsi_cmd ucmd_buf; 18984 union scsi_cdb cdb; 18985 uint32_t *capacity_buf; 18986 uint64_t capacity; 18987 uint32_t lbasize; 18988 int status; 18989 18990 ASSERT(un != NULL); 18991 ASSERT(!mutex_owned(SD_MUTEX(un))); 18992 ASSERT(capp != NULL); 18993 ASSERT(lbap != NULL); 18994 18995 SD_TRACE(SD_LOG_IO, un, 18996 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 18997 18998 /* 18999 * First send a READ_CAPACITY command to the target. 19000 * (This command is mandatory under SCSI-2.) 19001 * 19002 * Set up the CDB for the READ_CAPACITY command. The Partial 19003 * Medium Indicator bit is cleared. The address field must be 19004 * zero if the PMI bit is zero. 19005 */ 19006 bzero(&cdb, sizeof (cdb)); 19007 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19008 19009 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19010 19011 cdb.scc_cmd = SCMD_READ_CAPACITY; 19012 19013 ucmd_buf.uscsi_cdb = (char *)&cdb; 19014 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19015 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19016 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19017 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19018 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19019 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19020 ucmd_buf.uscsi_timeout = 60; 19021 19022 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19023 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19024 19025 switch (status) { 19026 case 0: 19027 /* Return failure if we did not get valid capacity data. */ 19028 if (ucmd_buf.uscsi_resid != 0) { 19029 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19030 return (EIO); 19031 } 19032 19033 /* 19034 * Read capacity and block size from the READ CAPACITY 10 data. 19035 * This data may be adjusted later due to device specific 19036 * issues. 19037 * 19038 * According to the SCSI spec, the READ CAPACITY 10 19039 * command returns the following: 19040 * 19041 * bytes 0-3: Maximum logical block address available. 19042 * (MSB in byte:0 & LSB in byte:3) 19043 * 19044 * bytes 4-7: Block length in bytes 19045 * (MSB in byte:4 & LSB in byte:7) 19046 * 19047 */ 19048 capacity = BE_32(capacity_buf[0]); 19049 lbasize = BE_32(capacity_buf[1]); 19050 19051 /* 19052 * Done with capacity_buf 19053 */ 19054 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19055 19056 /* 19057 * if the reported capacity is set to all 0xf's, then 19058 * this disk is too large and requires SBC-2 commands. 19059 * Reissue the request using READ CAPACITY 16. 19060 */ 19061 if (capacity == 0xffffffff) { 19062 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19063 &lbasize, path_flag); 19064 if (status != 0) { 19065 return (status); 19066 } 19067 } 19068 break; /* Success! */ 19069 case EIO: 19070 switch (ucmd_buf.uscsi_status) { 19071 case STATUS_RESERVATION_CONFLICT: 19072 status = EACCES; 19073 break; 19074 case STATUS_CHECK: 19075 /* 19076 * Check condition; look for ASC/ASCQ of 0x04/0x01 19077 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19078 */ 19079 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19080 (sense_buf.es_add_code == 0x04) && 19081 (sense_buf.es_qual_code == 0x01)) { 19082 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19083 return (EAGAIN); 19084 } 19085 break; 19086 default: 19087 break; 19088 } 19089 /* FALLTHRU */ 19090 default: 19091 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19092 return (status); 19093 } 19094 19095 /* 19096 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19097 * (2352 and 0 are common) so for these devices always force the value 19098 * to 2048 as required by the ATAPI specs. 19099 */ 19100 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19101 lbasize = 2048; 19102 } 19103 19104 /* 19105 * Get the maximum LBA value from the READ CAPACITY data. 19106 * Here we assume that the Partial Medium Indicator (PMI) bit 19107 * was cleared when issuing the command. This means that the LBA 19108 * returned from the device is the LBA of the last logical block 19109 * on the logical unit. The actual logical block count will be 19110 * this value plus one. 19111 * 19112 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19113 * so scale the capacity value to reflect this. 19114 */ 19115 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19116 19117 #if defined(__i386) || defined(__amd64) 19118 /* 19119 * On x86, compensate for off-by-1 error (number of sectors on 19120 * media) (1175930) 19121 */ 19122 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 19123 (lbasize == un->un_sys_blocksize)) { 19124 capacity -= 1; 19125 } 19126 #endif 19127 19128 /* 19129 * Copy the values from the READ CAPACITY command into the space 19130 * provided by the caller. 19131 */ 19132 *capp = capacity; 19133 *lbap = lbasize; 19134 19135 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19136 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19137 19138 /* 19139 * Both the lbasize and capacity from the device must be nonzero, 19140 * otherwise we assume that the values are not valid and return 19141 * failure to the caller. (4203735) 19142 */ 19143 if ((capacity == 0) || (lbasize == 0)) { 19144 return (EIO); 19145 } 19146 19147 return (0); 19148 } 19149 19150 /* 19151 * Function: sd_send_scsi_READ_CAPACITY_16 19152 * 19153 * Description: This routine uses the scsi READ CAPACITY 16 command to 19154 * determine the device capacity in number of blocks and the 19155 * device native block size. If this function returns a failure, 19156 * then the values in *capp and *lbap are undefined. 19157 * This routine should always be called by 19158 * sd_send_scsi_READ_CAPACITY which will appy any device 19159 * specific adjustments to capacity and lbasize. 19160 * 19161 * Arguments: un - ptr to soft state struct for the target 19162 * capp - ptr to unsigned 64-bit variable to receive the 19163 * capacity value from the command. 19164 * lbap - ptr to unsigned 32-bit varaible to receive the 19165 * block size value from the command 19166 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19167 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19168 * to use the USCSI "direct" chain and bypass the normal 19169 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19170 * this command is issued as part of an error recovery 19171 * action. 19172 * 19173 * Return Code: 0 - Success 19174 * EIO - IO error 19175 * EACCES - Reservation conflict detected 19176 * EAGAIN - Device is becoming ready 19177 * errno return code from sd_send_scsi_cmd() 19178 * 19179 * Context: Can sleep. Blocks until command completes. 19180 */ 19181 19182 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19183 19184 static int 19185 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19186 uint32_t *lbap, int path_flag) 19187 { 19188 struct scsi_extended_sense sense_buf; 19189 struct uscsi_cmd ucmd_buf; 19190 union scsi_cdb cdb; 19191 uint64_t *capacity16_buf; 19192 uint64_t capacity; 19193 uint32_t lbasize; 19194 int status; 19195 19196 ASSERT(un != NULL); 19197 ASSERT(!mutex_owned(SD_MUTEX(un))); 19198 ASSERT(capp != NULL); 19199 ASSERT(lbap != NULL); 19200 19201 SD_TRACE(SD_LOG_IO, un, 19202 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19203 19204 /* 19205 * First send a READ_CAPACITY_16 command to the target. 19206 * 19207 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19208 * Medium Indicator bit is cleared. The address field must be 19209 * zero if the PMI bit is zero. 19210 */ 19211 bzero(&cdb, sizeof (cdb)); 19212 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19213 19214 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19215 19216 ucmd_buf.uscsi_cdb = (char *)&cdb; 19217 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19218 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19219 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19220 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19221 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19222 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19223 ucmd_buf.uscsi_timeout = 60; 19224 19225 /* 19226 * Read Capacity (16) is a Service Action In command. One 19227 * command byte (0x9E) is overloaded for multiple operations, 19228 * with the second CDB byte specifying the desired operation 19229 */ 19230 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19231 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19232 19233 /* 19234 * Fill in allocation length field 19235 */ 19236 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19237 19238 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19239 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19240 19241 switch (status) { 19242 case 0: 19243 /* Return failure if we did not get valid capacity data. */ 19244 if (ucmd_buf.uscsi_resid > 20) { 19245 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19246 return (EIO); 19247 } 19248 19249 /* 19250 * Read capacity and block size from the READ CAPACITY 10 data. 19251 * This data may be adjusted later due to device specific 19252 * issues. 19253 * 19254 * According to the SCSI spec, the READ CAPACITY 10 19255 * command returns the following: 19256 * 19257 * bytes 0-7: Maximum logical block address available. 19258 * (MSB in byte:0 & LSB in byte:7) 19259 * 19260 * bytes 8-11: Block length in bytes 19261 * (MSB in byte:8 & LSB in byte:11) 19262 * 19263 */ 19264 capacity = BE_64(capacity16_buf[0]); 19265 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19266 19267 /* 19268 * Done with capacity16_buf 19269 */ 19270 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19271 19272 /* 19273 * if the reported capacity is set to all 0xf's, then 19274 * this disk is too large. This could only happen with 19275 * a device that supports LBAs larger than 64 bits which 19276 * are not defined by any current T10 standards. 19277 */ 19278 if (capacity == 0xffffffffffffffff) { 19279 return (EIO); 19280 } 19281 break; /* Success! */ 19282 case EIO: 19283 switch (ucmd_buf.uscsi_status) { 19284 case STATUS_RESERVATION_CONFLICT: 19285 status = EACCES; 19286 break; 19287 case STATUS_CHECK: 19288 /* 19289 * Check condition; look for ASC/ASCQ of 0x04/0x01 19290 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19291 */ 19292 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19293 (sense_buf.es_add_code == 0x04) && 19294 (sense_buf.es_qual_code == 0x01)) { 19295 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19296 return (EAGAIN); 19297 } 19298 break; 19299 default: 19300 break; 19301 } 19302 /* FALLTHRU */ 19303 default: 19304 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19305 return (status); 19306 } 19307 19308 *capp = capacity; 19309 *lbap = lbasize; 19310 19311 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19312 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19313 19314 return (0); 19315 } 19316 19317 19318 /* 19319 * Function: sd_send_scsi_START_STOP_UNIT 19320 * 19321 * Description: Issue a scsi START STOP UNIT command to the target. 19322 * 19323 * Arguments: un - pointer to driver soft state (unit) structure for 19324 * this target. 19325 * flag - SD_TARGET_START 19326 * SD_TARGET_STOP 19327 * SD_TARGET_EJECT 19328 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19329 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19330 * to use the USCSI "direct" chain and bypass the normal 19331 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19332 * command is issued as part of an error recovery action. 19333 * 19334 * Return Code: 0 - Success 19335 * EIO - IO error 19336 * EACCES - Reservation conflict detected 19337 * ENXIO - Not Ready, medium not present 19338 * errno return code from sd_send_scsi_cmd() 19339 * 19340 * Context: Can sleep. 19341 */ 19342 19343 static int 19344 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19345 { 19346 struct scsi_extended_sense sense_buf; 19347 union scsi_cdb cdb; 19348 struct uscsi_cmd ucmd_buf; 19349 int status; 19350 19351 ASSERT(un != NULL); 19352 ASSERT(!mutex_owned(SD_MUTEX(un))); 19353 19354 SD_TRACE(SD_LOG_IO, un, 19355 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19356 19357 if (un->un_f_check_start_stop && 19358 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19359 (un->un_f_start_stop_supported != TRUE)) { 19360 return (0); 19361 } 19362 19363 bzero(&cdb, sizeof (cdb)); 19364 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19365 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19366 19367 cdb.scc_cmd = SCMD_START_STOP; 19368 cdb.cdb_opaque[4] = (uchar_t)flag; 19369 19370 ucmd_buf.uscsi_cdb = (char *)&cdb; 19371 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19372 ucmd_buf.uscsi_bufaddr = NULL; 19373 ucmd_buf.uscsi_buflen = 0; 19374 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19375 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19376 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19377 ucmd_buf.uscsi_timeout = 200; 19378 19379 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19380 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19381 19382 switch (status) { 19383 case 0: 19384 break; /* Success! */ 19385 case EIO: 19386 switch (ucmd_buf.uscsi_status) { 19387 case STATUS_RESERVATION_CONFLICT: 19388 status = EACCES; 19389 break; 19390 case STATUS_CHECK: 19391 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19392 switch (sense_buf.es_key) { 19393 case KEY_ILLEGAL_REQUEST: 19394 status = ENOTSUP; 19395 break; 19396 case KEY_NOT_READY: 19397 if (sense_buf.es_add_code == 0x3A) { 19398 status = ENXIO; 19399 } 19400 break; 19401 default: 19402 break; 19403 } 19404 } 19405 break; 19406 default: 19407 break; 19408 } 19409 break; 19410 default: 19411 break; 19412 } 19413 19414 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19415 19416 return (status); 19417 } 19418 19419 19420 /* 19421 * Function: sd_start_stop_unit_callback 19422 * 19423 * Description: timeout(9F) callback to begin recovery process for a 19424 * device that has spun down. 19425 * 19426 * Arguments: arg - pointer to associated softstate struct. 19427 * 19428 * Context: Executes in a timeout(9F) thread context 19429 */ 19430 19431 static void 19432 sd_start_stop_unit_callback(void *arg) 19433 { 19434 struct sd_lun *un = arg; 19435 ASSERT(un != NULL); 19436 ASSERT(!mutex_owned(SD_MUTEX(un))); 19437 19438 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19439 19440 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19441 } 19442 19443 19444 /* 19445 * Function: sd_start_stop_unit_task 19446 * 19447 * Description: Recovery procedure when a drive is spun down. 19448 * 19449 * Arguments: arg - pointer to associated softstate struct. 19450 * 19451 * Context: Executes in a taskq() thread context 19452 */ 19453 19454 static void 19455 sd_start_stop_unit_task(void *arg) 19456 { 19457 struct sd_lun *un = arg; 19458 19459 ASSERT(un != NULL); 19460 ASSERT(!mutex_owned(SD_MUTEX(un))); 19461 19462 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19463 19464 /* 19465 * Some unformatted drives report not ready error, no need to 19466 * restart if format has been initiated. 19467 */ 19468 mutex_enter(SD_MUTEX(un)); 19469 if (un->un_f_format_in_progress == TRUE) { 19470 mutex_exit(SD_MUTEX(un)); 19471 return; 19472 } 19473 mutex_exit(SD_MUTEX(un)); 19474 19475 /* 19476 * When a START STOP command is issued from here, it is part of a 19477 * failure recovery operation and must be issued before any other 19478 * commands, including any pending retries. Thus it must be sent 19479 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19480 * succeeds or not, we will start I/O after the attempt. 19481 */ 19482 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19483 SD_PATH_DIRECT_PRIORITY); 19484 19485 /* 19486 * The above call blocks until the START_STOP_UNIT command completes. 19487 * Now that it has completed, we must re-try the original IO that 19488 * received the NOT READY condition in the first place. There are 19489 * three possible conditions here: 19490 * 19491 * (1) The original IO is on un_retry_bp. 19492 * (2) The original IO is on the regular wait queue, and un_retry_bp 19493 * is NULL. 19494 * (3) The original IO is on the regular wait queue, and un_retry_bp 19495 * points to some other, unrelated bp. 19496 * 19497 * For each case, we must call sd_start_cmds() with un_retry_bp 19498 * as the argument. If un_retry_bp is NULL, this will initiate 19499 * processing of the regular wait queue. If un_retry_bp is not NULL, 19500 * then this will process the bp on un_retry_bp. That may or may not 19501 * be the original IO, but that does not matter: the important thing 19502 * is to keep the IO processing going at this point. 19503 * 19504 * Note: This is a very specific error recovery sequence associated 19505 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19506 * serialize the I/O with completion of the spin-up. 19507 */ 19508 mutex_enter(SD_MUTEX(un)); 19509 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19510 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19511 un, un->un_retry_bp); 19512 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19513 sd_start_cmds(un, un->un_retry_bp); 19514 mutex_exit(SD_MUTEX(un)); 19515 19516 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19517 } 19518 19519 19520 /* 19521 * Function: sd_send_scsi_INQUIRY 19522 * 19523 * Description: Issue the scsi INQUIRY command. 19524 * 19525 * Arguments: un 19526 * bufaddr 19527 * buflen 19528 * evpd 19529 * page_code 19530 * page_length 19531 * 19532 * Return Code: 0 - Success 19533 * errno return code from sd_send_scsi_cmd() 19534 * 19535 * Context: Can sleep. Does not return until command is completed. 19536 */ 19537 19538 static int 19539 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19540 uchar_t evpd, uchar_t page_code, size_t *residp) 19541 { 19542 union scsi_cdb cdb; 19543 struct uscsi_cmd ucmd_buf; 19544 int status; 19545 19546 ASSERT(un != NULL); 19547 ASSERT(!mutex_owned(SD_MUTEX(un))); 19548 ASSERT(bufaddr != NULL); 19549 19550 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19551 19552 bzero(&cdb, sizeof (cdb)); 19553 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19554 bzero(bufaddr, buflen); 19555 19556 cdb.scc_cmd = SCMD_INQUIRY; 19557 cdb.cdb_opaque[1] = evpd; 19558 cdb.cdb_opaque[2] = page_code; 19559 FORMG0COUNT(&cdb, buflen); 19560 19561 ucmd_buf.uscsi_cdb = (char *)&cdb; 19562 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19563 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19564 ucmd_buf.uscsi_buflen = buflen; 19565 ucmd_buf.uscsi_rqbuf = NULL; 19566 ucmd_buf.uscsi_rqlen = 0; 19567 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19568 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19569 19570 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19571 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19572 19573 if ((status == 0) && (residp != NULL)) { 19574 *residp = ucmd_buf.uscsi_resid; 19575 } 19576 19577 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19578 19579 return (status); 19580 } 19581 19582 19583 /* 19584 * Function: sd_send_scsi_TEST_UNIT_READY 19585 * 19586 * Description: Issue the scsi TEST UNIT READY command. 19587 * This routine can be told to set the flag USCSI_DIAGNOSE to 19588 * prevent retrying failed commands. Use this when the intent 19589 * is either to check for device readiness, to clear a Unit 19590 * Attention, or to clear any outstanding sense data. 19591 * However under specific conditions the expected behavior 19592 * is for retries to bring a device ready, so use the flag 19593 * with caution. 19594 * 19595 * Arguments: un 19596 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19597 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19598 * 0: dont check for media present, do retries on cmd. 19599 * 19600 * Return Code: 0 - Success 19601 * EIO - IO error 19602 * EACCES - Reservation conflict detected 19603 * ENXIO - Not Ready, medium not present 19604 * errno return code from sd_send_scsi_cmd() 19605 * 19606 * Context: Can sleep. Does not return until command is completed. 19607 */ 19608 19609 static int 19610 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19611 { 19612 struct scsi_extended_sense sense_buf; 19613 union scsi_cdb cdb; 19614 struct uscsi_cmd ucmd_buf; 19615 int status; 19616 19617 ASSERT(un != NULL); 19618 ASSERT(!mutex_owned(SD_MUTEX(un))); 19619 19620 SD_TRACE(SD_LOG_IO, un, 19621 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19622 19623 /* 19624 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19625 * timeouts when they receive a TUR and the queue is not empty. Check 19626 * the configuration flag set during attach (indicating the drive has 19627 * this firmware bug) and un_ncmds_in_transport before issuing the 19628 * TUR. If there are 19629 * pending commands return success, this is a bit arbitrary but is ok 19630 * for non-removables (i.e. the eliteI disks) and non-clustering 19631 * configurations. 19632 */ 19633 if (un->un_f_cfg_tur_check == TRUE) { 19634 mutex_enter(SD_MUTEX(un)); 19635 if (un->un_ncmds_in_transport != 0) { 19636 mutex_exit(SD_MUTEX(un)); 19637 return (0); 19638 } 19639 mutex_exit(SD_MUTEX(un)); 19640 } 19641 19642 bzero(&cdb, sizeof (cdb)); 19643 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19644 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19645 19646 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19647 19648 ucmd_buf.uscsi_cdb = (char *)&cdb; 19649 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19650 ucmd_buf.uscsi_bufaddr = NULL; 19651 ucmd_buf.uscsi_buflen = 0; 19652 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19653 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19654 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19655 19656 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19657 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19658 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19659 } 19660 ucmd_buf.uscsi_timeout = 60; 19661 19662 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19663 UIO_SYSSPACE, UIO_SYSSPACE, 19664 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19665 19666 switch (status) { 19667 case 0: 19668 break; /* Success! */ 19669 case EIO: 19670 switch (ucmd_buf.uscsi_status) { 19671 case STATUS_RESERVATION_CONFLICT: 19672 status = EACCES; 19673 break; 19674 case STATUS_CHECK: 19675 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19676 break; 19677 } 19678 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19679 (sense_buf.es_key == KEY_NOT_READY) && 19680 (sense_buf.es_add_code == 0x3A)) { 19681 status = ENXIO; 19682 } 19683 break; 19684 default: 19685 break; 19686 } 19687 break; 19688 default: 19689 break; 19690 } 19691 19692 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19693 19694 return (status); 19695 } 19696 19697 19698 /* 19699 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19700 * 19701 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19702 * 19703 * Arguments: un 19704 * 19705 * Return Code: 0 - Success 19706 * EACCES 19707 * ENOTSUP 19708 * errno return code from sd_send_scsi_cmd() 19709 * 19710 * Context: Can sleep. Does not return until command is completed. 19711 */ 19712 19713 static int 19714 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19715 uint16_t data_len, uchar_t *data_bufp) 19716 { 19717 struct scsi_extended_sense sense_buf; 19718 union scsi_cdb cdb; 19719 struct uscsi_cmd ucmd_buf; 19720 int status; 19721 int no_caller_buf = FALSE; 19722 19723 ASSERT(un != NULL); 19724 ASSERT(!mutex_owned(SD_MUTEX(un))); 19725 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19726 19727 SD_TRACE(SD_LOG_IO, un, 19728 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19729 19730 bzero(&cdb, sizeof (cdb)); 19731 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19732 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19733 if (data_bufp == NULL) { 19734 /* Allocate a default buf if the caller did not give one */ 19735 ASSERT(data_len == 0); 19736 data_len = MHIOC_RESV_KEY_SIZE; 19737 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19738 no_caller_buf = TRUE; 19739 } 19740 19741 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19742 cdb.cdb_opaque[1] = usr_cmd; 19743 FORMG1COUNT(&cdb, data_len); 19744 19745 ucmd_buf.uscsi_cdb = (char *)&cdb; 19746 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19747 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19748 ucmd_buf.uscsi_buflen = data_len; 19749 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19750 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19751 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19752 ucmd_buf.uscsi_timeout = 60; 19753 19754 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19755 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19756 19757 switch (status) { 19758 case 0: 19759 break; /* Success! */ 19760 case EIO: 19761 switch (ucmd_buf.uscsi_status) { 19762 case STATUS_RESERVATION_CONFLICT: 19763 status = EACCES; 19764 break; 19765 case STATUS_CHECK: 19766 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19767 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19768 status = ENOTSUP; 19769 } 19770 break; 19771 default: 19772 break; 19773 } 19774 break; 19775 default: 19776 break; 19777 } 19778 19779 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19780 19781 if (no_caller_buf == TRUE) { 19782 kmem_free(data_bufp, data_len); 19783 } 19784 19785 return (status); 19786 } 19787 19788 19789 /* 19790 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19791 * 19792 * Description: This routine is the driver entry point for handling CD-ROM 19793 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19794 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19795 * device. 19796 * 19797 * Arguments: un - Pointer to soft state struct for the target. 19798 * usr_cmd SCSI-3 reservation facility command (one of 19799 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19800 * SD_SCSI3_PREEMPTANDABORT) 19801 * usr_bufp - user provided pointer register, reserve descriptor or 19802 * preempt and abort structure (mhioc_register_t, 19803 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19804 * 19805 * Return Code: 0 - Success 19806 * EACCES 19807 * ENOTSUP 19808 * errno return code from sd_send_scsi_cmd() 19809 * 19810 * Context: Can sleep. Does not return until command is completed. 19811 */ 19812 19813 static int 19814 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19815 uchar_t *usr_bufp) 19816 { 19817 struct scsi_extended_sense sense_buf; 19818 union scsi_cdb cdb; 19819 struct uscsi_cmd ucmd_buf; 19820 int status; 19821 uchar_t data_len = sizeof (sd_prout_t); 19822 sd_prout_t *prp; 19823 19824 ASSERT(un != NULL); 19825 ASSERT(!mutex_owned(SD_MUTEX(un))); 19826 ASSERT(data_len == 24); /* required by scsi spec */ 19827 19828 SD_TRACE(SD_LOG_IO, un, 19829 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19830 19831 if (usr_bufp == NULL) { 19832 return (EINVAL); 19833 } 19834 19835 bzero(&cdb, sizeof (cdb)); 19836 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19837 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19838 prp = kmem_zalloc(data_len, KM_SLEEP); 19839 19840 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 19841 cdb.cdb_opaque[1] = usr_cmd; 19842 FORMG1COUNT(&cdb, data_len); 19843 19844 ucmd_buf.uscsi_cdb = (char *)&cdb; 19845 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19846 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 19847 ucmd_buf.uscsi_buflen = data_len; 19848 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19849 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19850 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19851 ucmd_buf.uscsi_timeout = 60; 19852 19853 switch (usr_cmd) { 19854 case SD_SCSI3_REGISTER: { 19855 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 19856 19857 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19858 bcopy(ptr->newkey.key, prp->service_key, 19859 MHIOC_RESV_KEY_SIZE); 19860 prp->aptpl = ptr->aptpl; 19861 break; 19862 } 19863 case SD_SCSI3_RESERVE: 19864 case SD_SCSI3_RELEASE: { 19865 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 19866 19867 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19868 prp->scope_address = BE_32(ptr->scope_specific_addr); 19869 cdb.cdb_opaque[2] = ptr->type; 19870 break; 19871 } 19872 case SD_SCSI3_PREEMPTANDABORT: { 19873 mhioc_preemptandabort_t *ptr = 19874 (mhioc_preemptandabort_t *)usr_bufp; 19875 19876 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19877 bcopy(ptr->victim_key.key, prp->service_key, 19878 MHIOC_RESV_KEY_SIZE); 19879 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 19880 cdb.cdb_opaque[2] = ptr->resvdesc.type; 19881 ucmd_buf.uscsi_flags |= USCSI_HEAD; 19882 break; 19883 } 19884 case SD_SCSI3_REGISTERANDIGNOREKEY: 19885 { 19886 mhioc_registerandignorekey_t *ptr; 19887 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 19888 bcopy(ptr->newkey.key, 19889 prp->service_key, MHIOC_RESV_KEY_SIZE); 19890 prp->aptpl = ptr->aptpl; 19891 break; 19892 } 19893 default: 19894 ASSERT(FALSE); 19895 break; 19896 } 19897 19898 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19899 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19900 19901 switch (status) { 19902 case 0: 19903 break; /* Success! */ 19904 case EIO: 19905 switch (ucmd_buf.uscsi_status) { 19906 case STATUS_RESERVATION_CONFLICT: 19907 status = EACCES; 19908 break; 19909 case STATUS_CHECK: 19910 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19911 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19912 status = ENOTSUP; 19913 } 19914 break; 19915 default: 19916 break; 19917 } 19918 break; 19919 default: 19920 break; 19921 } 19922 19923 kmem_free(prp, data_len); 19924 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 19925 return (status); 19926 } 19927 19928 19929 /* 19930 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 19931 * 19932 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 19933 * 19934 * Arguments: un - pointer to the target's soft state struct 19935 * 19936 * Return Code: 0 - success 19937 * errno-type error code 19938 * 19939 * Context: kernel thread context only. 19940 */ 19941 19942 static int 19943 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 19944 { 19945 struct sd_uscsi_info *uip; 19946 struct uscsi_cmd *uscmd; 19947 union scsi_cdb *cdb; 19948 struct buf *bp; 19949 int rval = 0; 19950 19951 SD_TRACE(SD_LOG_IO, un, 19952 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 19953 19954 ASSERT(un != NULL); 19955 ASSERT(!mutex_owned(SD_MUTEX(un))); 19956 19957 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 19958 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 19959 19960 /* 19961 * First get some memory for the uscsi_cmd struct and cdb 19962 * and initialize for SYNCHRONIZE_CACHE cmd. 19963 */ 19964 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 19965 uscmd->uscsi_cdblen = CDB_GROUP1; 19966 uscmd->uscsi_cdb = (caddr_t)cdb; 19967 uscmd->uscsi_bufaddr = NULL; 19968 uscmd->uscsi_buflen = 0; 19969 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 19970 uscmd->uscsi_rqlen = SENSE_LENGTH; 19971 uscmd->uscsi_rqresid = SENSE_LENGTH; 19972 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19973 uscmd->uscsi_timeout = sd_io_time; 19974 19975 /* 19976 * Allocate an sd_uscsi_info struct and fill it with the info 19977 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 19978 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 19979 * since we allocate the buf here in this function, we do not 19980 * need to preserve the prior contents of b_private. 19981 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 19982 */ 19983 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 19984 uip->ui_flags = SD_PATH_DIRECT; 19985 uip->ui_cmdp = uscmd; 19986 19987 bp = getrbuf(KM_SLEEP); 19988 bp->b_private = uip; 19989 19990 /* 19991 * Setup buffer to carry uscsi request. 19992 */ 19993 bp->b_flags = B_BUSY; 19994 bp->b_bcount = 0; 19995 bp->b_blkno = 0; 19996 19997 if (dkc != NULL) { 19998 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 19999 uip->ui_dkc = *dkc; 20000 } 20001 20002 bp->b_edev = SD_GET_DEV(un); 20003 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20004 20005 (void) sd_uscsi_strategy(bp); 20006 20007 /* 20008 * If synchronous request, wait for completion 20009 * If async just return and let b_iodone callback 20010 * cleanup. 20011 * NOTE: On return, u_ncmds_in_driver will be decremented, 20012 * but it was also incremented in sd_uscsi_strategy(), so 20013 * we should be ok. 20014 */ 20015 if (dkc == NULL) { 20016 (void) biowait(bp); 20017 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20018 } 20019 20020 return (rval); 20021 } 20022 20023 20024 static int 20025 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20026 { 20027 struct sd_uscsi_info *uip; 20028 struct uscsi_cmd *uscmd; 20029 struct scsi_extended_sense *sense_buf; 20030 struct sd_lun *un; 20031 int status; 20032 20033 uip = (struct sd_uscsi_info *)(bp->b_private); 20034 ASSERT(uip != NULL); 20035 20036 uscmd = uip->ui_cmdp; 20037 ASSERT(uscmd != NULL); 20038 20039 sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf; 20040 ASSERT(sense_buf != NULL); 20041 20042 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20043 ASSERT(un != NULL); 20044 20045 status = geterror(bp); 20046 switch (status) { 20047 case 0: 20048 break; /* Success! */ 20049 case EIO: 20050 switch (uscmd->uscsi_status) { 20051 case STATUS_RESERVATION_CONFLICT: 20052 /* Ignore reservation conflict */ 20053 status = 0; 20054 goto done; 20055 20056 case STATUS_CHECK: 20057 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20058 (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) { 20059 /* Ignore Illegal Request error */ 20060 mutex_enter(SD_MUTEX(un)); 20061 un->un_f_sync_cache_supported = FALSE; 20062 mutex_exit(SD_MUTEX(un)); 20063 status = ENOTSUP; 20064 goto done; 20065 } 20066 break; 20067 default: 20068 break; 20069 } 20070 /* FALLTHRU */ 20071 default: 20072 /* Ignore error if the media is not present */ 20073 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 20074 status = 0; 20075 goto done; 20076 } 20077 /* If we reach this, we had an error */ 20078 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20079 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20080 break; 20081 } 20082 20083 done: 20084 if (uip->ui_dkc.dkc_callback != NULL) { 20085 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20086 } 20087 20088 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20089 freerbuf(bp); 20090 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20091 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20092 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20093 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20094 20095 return (status); 20096 } 20097 20098 20099 /* 20100 * Function: sd_send_scsi_GET_CONFIGURATION 20101 * 20102 * Description: Issues the get configuration command to the device. 20103 * Called from sd_check_for_writable_cd & sd_get_media_info 20104 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20105 * Arguments: un 20106 * ucmdbuf 20107 * rqbuf 20108 * rqbuflen 20109 * bufaddr 20110 * buflen 20111 * 20112 * Return Code: 0 - Success 20113 * errno return code from sd_send_scsi_cmd() 20114 * 20115 * Context: Can sleep. Does not return until command is completed. 20116 * 20117 */ 20118 20119 static int 20120 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20121 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20122 { 20123 char cdb[CDB_GROUP1]; 20124 int status; 20125 20126 ASSERT(un != NULL); 20127 ASSERT(!mutex_owned(SD_MUTEX(un))); 20128 ASSERT(bufaddr != NULL); 20129 ASSERT(ucmdbuf != NULL); 20130 ASSERT(rqbuf != NULL); 20131 20132 SD_TRACE(SD_LOG_IO, un, 20133 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20134 20135 bzero(cdb, sizeof (cdb)); 20136 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20137 bzero(rqbuf, rqbuflen); 20138 bzero(bufaddr, buflen); 20139 20140 /* 20141 * Set up cdb field for the get configuration command. 20142 */ 20143 cdb[0] = SCMD_GET_CONFIGURATION; 20144 cdb[1] = 0x02; /* Requested Type */ 20145 cdb[8] = SD_PROFILE_HEADER_LEN; 20146 ucmdbuf->uscsi_cdb = cdb; 20147 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20148 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20149 ucmdbuf->uscsi_buflen = buflen; 20150 ucmdbuf->uscsi_timeout = sd_io_time; 20151 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20152 ucmdbuf->uscsi_rqlen = rqbuflen; 20153 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20154 20155 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20156 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20157 20158 switch (status) { 20159 case 0: 20160 break; /* Success! */ 20161 case EIO: 20162 switch (ucmdbuf->uscsi_status) { 20163 case STATUS_RESERVATION_CONFLICT: 20164 status = EACCES; 20165 break; 20166 default: 20167 break; 20168 } 20169 break; 20170 default: 20171 break; 20172 } 20173 20174 if (status == 0) { 20175 SD_DUMP_MEMORY(un, SD_LOG_IO, 20176 "sd_send_scsi_GET_CONFIGURATION: data", 20177 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20178 } 20179 20180 SD_TRACE(SD_LOG_IO, un, 20181 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20182 20183 return (status); 20184 } 20185 20186 /* 20187 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20188 * 20189 * Description: Issues the get configuration command to the device to 20190 * retrieve a specfic feature. Called from 20191 * sd_check_for_writable_cd & sd_set_mmc_caps. 20192 * Arguments: un 20193 * ucmdbuf 20194 * rqbuf 20195 * rqbuflen 20196 * bufaddr 20197 * buflen 20198 * feature 20199 * 20200 * Return Code: 0 - Success 20201 * errno return code from sd_send_scsi_cmd() 20202 * 20203 * Context: Can sleep. Does not return until command is completed. 20204 * 20205 */ 20206 static int 20207 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20208 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20209 uchar_t *bufaddr, uint_t buflen, char feature) 20210 { 20211 char cdb[CDB_GROUP1]; 20212 int status; 20213 20214 ASSERT(un != NULL); 20215 ASSERT(!mutex_owned(SD_MUTEX(un))); 20216 ASSERT(bufaddr != NULL); 20217 ASSERT(ucmdbuf != NULL); 20218 ASSERT(rqbuf != NULL); 20219 20220 SD_TRACE(SD_LOG_IO, un, 20221 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20222 20223 bzero(cdb, sizeof (cdb)); 20224 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20225 bzero(rqbuf, rqbuflen); 20226 bzero(bufaddr, buflen); 20227 20228 /* 20229 * Set up cdb field for the get configuration command. 20230 */ 20231 cdb[0] = SCMD_GET_CONFIGURATION; 20232 cdb[1] = 0x02; /* Requested Type */ 20233 cdb[3] = feature; 20234 cdb[8] = buflen; 20235 ucmdbuf->uscsi_cdb = cdb; 20236 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20237 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20238 ucmdbuf->uscsi_buflen = buflen; 20239 ucmdbuf->uscsi_timeout = sd_io_time; 20240 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20241 ucmdbuf->uscsi_rqlen = rqbuflen; 20242 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20243 20244 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20245 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20246 20247 switch (status) { 20248 case 0: 20249 break; /* Success! */ 20250 case EIO: 20251 switch (ucmdbuf->uscsi_status) { 20252 case STATUS_RESERVATION_CONFLICT: 20253 status = EACCES; 20254 break; 20255 default: 20256 break; 20257 } 20258 break; 20259 default: 20260 break; 20261 } 20262 20263 if (status == 0) { 20264 SD_DUMP_MEMORY(un, SD_LOG_IO, 20265 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20266 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20267 } 20268 20269 SD_TRACE(SD_LOG_IO, un, 20270 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20271 20272 return (status); 20273 } 20274 20275 20276 /* 20277 * Function: sd_send_scsi_MODE_SENSE 20278 * 20279 * Description: Utility function for issuing a scsi MODE SENSE command. 20280 * Note: This routine uses a consistent implementation for Group0, 20281 * Group1, and Group2 commands across all platforms. ATAPI devices 20282 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20283 * 20284 * Arguments: un - pointer to the softstate struct for the target. 20285 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20286 * CDB_GROUP[1|2] (10 byte). 20287 * bufaddr - buffer for page data retrieved from the target. 20288 * buflen - size of page to be retrieved. 20289 * page_code - page code of data to be retrieved from the target. 20290 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20291 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20292 * to use the USCSI "direct" chain and bypass the normal 20293 * command waitq. 20294 * 20295 * Return Code: 0 - Success 20296 * errno return code from sd_send_scsi_cmd() 20297 * 20298 * Context: Can sleep. Does not return until command is completed. 20299 */ 20300 20301 static int 20302 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20303 size_t buflen, uchar_t page_code, int path_flag) 20304 { 20305 struct scsi_extended_sense sense_buf; 20306 union scsi_cdb cdb; 20307 struct uscsi_cmd ucmd_buf; 20308 int status; 20309 20310 ASSERT(un != NULL); 20311 ASSERT(!mutex_owned(SD_MUTEX(un))); 20312 ASSERT(bufaddr != NULL); 20313 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20314 (cdbsize == CDB_GROUP2)); 20315 20316 SD_TRACE(SD_LOG_IO, un, 20317 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20318 20319 bzero(&cdb, sizeof (cdb)); 20320 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20321 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20322 bzero(bufaddr, buflen); 20323 20324 if (cdbsize == CDB_GROUP0) { 20325 cdb.scc_cmd = SCMD_MODE_SENSE; 20326 cdb.cdb_opaque[2] = page_code; 20327 FORMG0COUNT(&cdb, buflen); 20328 } else { 20329 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20330 cdb.cdb_opaque[2] = page_code; 20331 FORMG1COUNT(&cdb, buflen); 20332 } 20333 20334 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20335 20336 ucmd_buf.uscsi_cdb = (char *)&cdb; 20337 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20338 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20339 ucmd_buf.uscsi_buflen = buflen; 20340 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20341 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20342 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20343 ucmd_buf.uscsi_timeout = 60; 20344 20345 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20346 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20347 20348 switch (status) { 20349 case 0: 20350 break; /* Success! */ 20351 case EIO: 20352 switch (ucmd_buf.uscsi_status) { 20353 case STATUS_RESERVATION_CONFLICT: 20354 status = EACCES; 20355 break; 20356 default: 20357 break; 20358 } 20359 break; 20360 default: 20361 break; 20362 } 20363 20364 if (status == 0) { 20365 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20366 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20367 } 20368 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20369 20370 return (status); 20371 } 20372 20373 20374 /* 20375 * Function: sd_send_scsi_MODE_SELECT 20376 * 20377 * Description: Utility function for issuing a scsi MODE SELECT command. 20378 * Note: This routine uses a consistent implementation for Group0, 20379 * Group1, and Group2 commands across all platforms. ATAPI devices 20380 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20381 * 20382 * Arguments: un - pointer to the softstate struct for the target. 20383 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20384 * CDB_GROUP[1|2] (10 byte). 20385 * bufaddr - buffer for page data retrieved from the target. 20386 * buflen - size of page to be retrieved. 20387 * save_page - boolean to determin if SP bit should be set. 20388 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20389 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20390 * to use the USCSI "direct" chain and bypass the normal 20391 * command waitq. 20392 * 20393 * Return Code: 0 - Success 20394 * errno return code from sd_send_scsi_cmd() 20395 * 20396 * Context: Can sleep. Does not return until command is completed. 20397 */ 20398 20399 static int 20400 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20401 size_t buflen, uchar_t save_page, int path_flag) 20402 { 20403 struct scsi_extended_sense sense_buf; 20404 union scsi_cdb cdb; 20405 struct uscsi_cmd ucmd_buf; 20406 int status; 20407 20408 ASSERT(un != NULL); 20409 ASSERT(!mutex_owned(SD_MUTEX(un))); 20410 ASSERT(bufaddr != NULL); 20411 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20412 (cdbsize == CDB_GROUP2)); 20413 20414 SD_TRACE(SD_LOG_IO, un, 20415 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20416 20417 bzero(&cdb, sizeof (cdb)); 20418 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20419 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20420 20421 /* Set the PF bit for many third party drives */ 20422 cdb.cdb_opaque[1] = 0x10; 20423 20424 /* Set the savepage(SP) bit if given */ 20425 if (save_page == SD_SAVE_PAGE) { 20426 cdb.cdb_opaque[1] |= 0x01; 20427 } 20428 20429 if (cdbsize == CDB_GROUP0) { 20430 cdb.scc_cmd = SCMD_MODE_SELECT; 20431 FORMG0COUNT(&cdb, buflen); 20432 } else { 20433 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20434 FORMG1COUNT(&cdb, buflen); 20435 } 20436 20437 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20438 20439 ucmd_buf.uscsi_cdb = (char *)&cdb; 20440 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20441 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20442 ucmd_buf.uscsi_buflen = buflen; 20443 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20444 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20445 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20446 ucmd_buf.uscsi_timeout = 60; 20447 20448 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20449 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20450 20451 switch (status) { 20452 case 0: 20453 break; /* Success! */ 20454 case EIO: 20455 switch (ucmd_buf.uscsi_status) { 20456 case STATUS_RESERVATION_CONFLICT: 20457 status = EACCES; 20458 break; 20459 default: 20460 break; 20461 } 20462 break; 20463 default: 20464 break; 20465 } 20466 20467 if (status == 0) { 20468 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20469 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20470 } 20471 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20472 20473 return (status); 20474 } 20475 20476 20477 /* 20478 * Function: sd_send_scsi_RDWR 20479 * 20480 * Description: Issue a scsi READ or WRITE command with the given parameters. 20481 * 20482 * Arguments: un: Pointer to the sd_lun struct for the target. 20483 * cmd: SCMD_READ or SCMD_WRITE 20484 * bufaddr: Address of caller's buffer to receive the RDWR data 20485 * buflen: Length of caller's buffer receive the RDWR data. 20486 * start_block: Block number for the start of the RDWR operation. 20487 * (Assumes target-native block size.) 20488 * residp: Pointer to variable to receive the redisual of the 20489 * RDWR operation (may be NULL of no residual requested). 20490 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20491 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20492 * to use the USCSI "direct" chain and bypass the normal 20493 * command waitq. 20494 * 20495 * Return Code: 0 - Success 20496 * errno return code from sd_send_scsi_cmd() 20497 * 20498 * Context: Can sleep. Does not return until command is completed. 20499 */ 20500 20501 static int 20502 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20503 size_t buflen, daddr_t start_block, int path_flag) 20504 { 20505 struct scsi_extended_sense sense_buf; 20506 union scsi_cdb cdb; 20507 struct uscsi_cmd ucmd_buf; 20508 uint32_t block_count; 20509 int status; 20510 int cdbsize; 20511 uchar_t flag; 20512 20513 ASSERT(un != NULL); 20514 ASSERT(!mutex_owned(SD_MUTEX(un))); 20515 ASSERT(bufaddr != NULL); 20516 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20517 20518 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20519 20520 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20521 return (EINVAL); 20522 } 20523 20524 mutex_enter(SD_MUTEX(un)); 20525 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20526 mutex_exit(SD_MUTEX(un)); 20527 20528 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20529 20530 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20531 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20532 bufaddr, buflen, start_block, block_count); 20533 20534 bzero(&cdb, sizeof (cdb)); 20535 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20536 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20537 20538 /* Compute CDB size to use */ 20539 if (start_block > 0xffffffff) 20540 cdbsize = CDB_GROUP4; 20541 else if ((start_block & 0xFFE00000) || 20542 (un->un_f_cfg_is_atapi == TRUE)) 20543 cdbsize = CDB_GROUP1; 20544 else 20545 cdbsize = CDB_GROUP0; 20546 20547 switch (cdbsize) { 20548 case CDB_GROUP0: /* 6-byte CDBs */ 20549 cdb.scc_cmd = cmd; 20550 FORMG0ADDR(&cdb, start_block); 20551 FORMG0COUNT(&cdb, block_count); 20552 break; 20553 case CDB_GROUP1: /* 10-byte CDBs */ 20554 cdb.scc_cmd = cmd | SCMD_GROUP1; 20555 FORMG1ADDR(&cdb, start_block); 20556 FORMG1COUNT(&cdb, block_count); 20557 break; 20558 case CDB_GROUP4: /* 16-byte CDBs */ 20559 cdb.scc_cmd = cmd | SCMD_GROUP4; 20560 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20561 FORMG4COUNT(&cdb, block_count); 20562 break; 20563 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20564 default: 20565 /* All others reserved */ 20566 return (EINVAL); 20567 } 20568 20569 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20570 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20571 20572 ucmd_buf.uscsi_cdb = (char *)&cdb; 20573 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20574 ucmd_buf.uscsi_bufaddr = bufaddr; 20575 ucmd_buf.uscsi_buflen = buflen; 20576 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20577 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20578 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20579 ucmd_buf.uscsi_timeout = 60; 20580 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20581 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20582 switch (status) { 20583 case 0: 20584 break; /* Success! */ 20585 case EIO: 20586 switch (ucmd_buf.uscsi_status) { 20587 case STATUS_RESERVATION_CONFLICT: 20588 status = EACCES; 20589 break; 20590 default: 20591 break; 20592 } 20593 break; 20594 default: 20595 break; 20596 } 20597 20598 if (status == 0) { 20599 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20600 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20601 } 20602 20603 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20604 20605 return (status); 20606 } 20607 20608 20609 /* 20610 * Function: sd_send_scsi_LOG_SENSE 20611 * 20612 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20613 * 20614 * Arguments: un: Pointer to the sd_lun struct for the target. 20615 * 20616 * Return Code: 0 - Success 20617 * errno return code from sd_send_scsi_cmd() 20618 * 20619 * Context: Can sleep. Does not return until command is completed. 20620 */ 20621 20622 static int 20623 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20624 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20625 int path_flag) 20626 20627 { 20628 struct scsi_extended_sense sense_buf; 20629 union scsi_cdb cdb; 20630 struct uscsi_cmd ucmd_buf; 20631 int status; 20632 20633 ASSERT(un != NULL); 20634 ASSERT(!mutex_owned(SD_MUTEX(un))); 20635 20636 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20637 20638 bzero(&cdb, sizeof (cdb)); 20639 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20640 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20641 20642 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20643 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20644 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20645 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20646 FORMG1COUNT(&cdb, buflen); 20647 20648 ucmd_buf.uscsi_cdb = (char *)&cdb; 20649 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20650 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20651 ucmd_buf.uscsi_buflen = buflen; 20652 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20653 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20654 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20655 ucmd_buf.uscsi_timeout = 60; 20656 20657 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20658 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20659 20660 switch (status) { 20661 case 0: 20662 break; 20663 case EIO: 20664 switch (ucmd_buf.uscsi_status) { 20665 case STATUS_RESERVATION_CONFLICT: 20666 status = EACCES; 20667 break; 20668 case STATUS_CHECK: 20669 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20670 (sense_buf.es_key == KEY_ILLEGAL_REQUEST) && 20671 (sense_buf.es_add_code == 0x24)) { 20672 /* 20673 * ASC 0x24: INVALID FIELD IN CDB 20674 */ 20675 switch (page_code) { 20676 case START_STOP_CYCLE_PAGE: 20677 /* 20678 * The start stop cycle counter is 20679 * implemented as page 0x31 in earlier 20680 * generation disks. In new generation 20681 * disks the start stop cycle counter is 20682 * implemented as page 0xE. To properly 20683 * handle this case if an attempt for 20684 * log page 0xE is made and fails we 20685 * will try again using page 0x31. 20686 * 20687 * Network storage BU committed to 20688 * maintain the page 0x31 for this 20689 * purpose and will not have any other 20690 * page implemented with page code 0x31 20691 * until all disks transition to the 20692 * standard page. 20693 */ 20694 mutex_enter(SD_MUTEX(un)); 20695 un->un_start_stop_cycle_page = 20696 START_STOP_CYCLE_VU_PAGE; 20697 cdb.cdb_opaque[2] = 20698 (char)(page_control << 6) | 20699 un->un_start_stop_cycle_page; 20700 mutex_exit(SD_MUTEX(un)); 20701 status = sd_send_scsi_cmd( 20702 SD_GET_DEV(un), &ucmd_buf, 20703 UIO_SYSSPACE, UIO_SYSSPACE, 20704 UIO_SYSSPACE, path_flag); 20705 20706 break; 20707 case TEMPERATURE_PAGE: 20708 status = ENOTTY; 20709 break; 20710 default: 20711 break; 20712 } 20713 } 20714 break; 20715 default: 20716 break; 20717 } 20718 break; 20719 default: 20720 break; 20721 } 20722 20723 if (status == 0) { 20724 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20725 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20726 } 20727 20728 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20729 20730 return (status); 20731 } 20732 20733 20734 /* 20735 * Function: sdioctl 20736 * 20737 * Description: Driver's ioctl(9e) entry point function. 20738 * 20739 * Arguments: dev - device number 20740 * cmd - ioctl operation to be performed 20741 * arg - user argument, contains data to be set or reference 20742 * parameter for get 20743 * flag - bit flag, indicating open settings, 32/64 bit type 20744 * cred_p - user credential pointer 20745 * rval_p - calling process return value (OPT) 20746 * 20747 * Return Code: EINVAL 20748 * ENOTTY 20749 * ENXIO 20750 * EIO 20751 * EFAULT 20752 * ENOTSUP 20753 * EPERM 20754 * 20755 * Context: Called from the device switch at normal priority. 20756 */ 20757 20758 static int 20759 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20760 { 20761 struct sd_lun *un = NULL; 20762 int geom_validated = FALSE; 20763 int err = 0; 20764 int i = 0; 20765 cred_t *cr; 20766 20767 /* 20768 * All device accesses go thru sdstrategy where we check on suspend 20769 * status 20770 */ 20771 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20772 return (ENXIO); 20773 } 20774 20775 ASSERT(!mutex_owned(SD_MUTEX(un))); 20776 20777 /* 20778 * Moved this wait from sd_uscsi_strategy to here for 20779 * reasons of deadlock prevention. Internal driver commands, 20780 * specifically those to change a devices power level, result 20781 * in a call to sd_uscsi_strategy. 20782 */ 20783 mutex_enter(SD_MUTEX(un)); 20784 while ((un->un_state == SD_STATE_SUSPENDED) || 20785 (un->un_state == SD_STATE_PM_CHANGING)) { 20786 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20787 } 20788 /* 20789 * Twiddling the counter here protects commands from now 20790 * through to the top of sd_uscsi_strategy. Without the 20791 * counter inc. a power down, for example, could get in 20792 * after the above check for state is made and before 20793 * execution gets to the top of sd_uscsi_strategy. 20794 * That would cause problems. 20795 */ 20796 un->un_ncmds_in_driver++; 20797 20798 if ((un->un_f_geometry_is_valid == FALSE) && 20799 (flag & (FNDELAY | FNONBLOCK))) { 20800 switch (cmd) { 20801 case CDROMPAUSE: 20802 case CDROMRESUME: 20803 case CDROMPLAYMSF: 20804 case CDROMPLAYTRKIND: 20805 case CDROMREADTOCHDR: 20806 case CDROMREADTOCENTRY: 20807 case CDROMSTOP: 20808 case CDROMSTART: 20809 case CDROMVOLCTRL: 20810 case CDROMSUBCHNL: 20811 case CDROMREADMODE2: 20812 case CDROMREADMODE1: 20813 case CDROMREADOFFSET: 20814 case CDROMSBLKMODE: 20815 case CDROMGBLKMODE: 20816 case CDROMGDRVSPEED: 20817 case CDROMSDRVSPEED: 20818 case CDROMCDDA: 20819 case CDROMCDXA: 20820 case CDROMSUBCODE: 20821 if (!ISCD(un)) { 20822 un->un_ncmds_in_driver--; 20823 ASSERT(un->un_ncmds_in_driver >= 0); 20824 mutex_exit(SD_MUTEX(un)); 20825 return (ENOTTY); 20826 } 20827 break; 20828 case FDEJECT: 20829 case DKIOCEJECT: 20830 case CDROMEJECT: 20831 if (!un->un_f_eject_media_supported) { 20832 un->un_ncmds_in_driver--; 20833 ASSERT(un->un_ncmds_in_driver >= 0); 20834 mutex_exit(SD_MUTEX(un)); 20835 return (ENOTTY); 20836 } 20837 break; 20838 case DKIOCSVTOC: 20839 case DKIOCSETEFI: 20840 case DKIOCSMBOOT: 20841 case DKIOCFLUSHWRITECACHE: 20842 mutex_exit(SD_MUTEX(un)); 20843 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 20844 if (err != 0) { 20845 mutex_enter(SD_MUTEX(un)); 20846 un->un_ncmds_in_driver--; 20847 ASSERT(un->un_ncmds_in_driver >= 0); 20848 mutex_exit(SD_MUTEX(un)); 20849 return (EIO); 20850 } 20851 mutex_enter(SD_MUTEX(un)); 20852 /* FALLTHROUGH */ 20853 case DKIOCREMOVABLE: 20854 case DKIOCHOTPLUGGABLE: 20855 case DKIOCINFO: 20856 case DKIOCGMEDIAINFO: 20857 case MHIOCENFAILFAST: 20858 case MHIOCSTATUS: 20859 case MHIOCTKOWN: 20860 case MHIOCRELEASE: 20861 case MHIOCGRP_INKEYS: 20862 case MHIOCGRP_INRESV: 20863 case MHIOCGRP_REGISTER: 20864 case MHIOCGRP_RESERVE: 20865 case MHIOCGRP_PREEMPTANDABORT: 20866 case MHIOCGRP_REGISTERANDIGNOREKEY: 20867 case CDROMCLOSETRAY: 20868 case USCSICMD: 20869 goto skip_ready_valid; 20870 default: 20871 break; 20872 } 20873 20874 mutex_exit(SD_MUTEX(un)); 20875 err = sd_ready_and_valid(un); 20876 mutex_enter(SD_MUTEX(un)); 20877 if (err == SD_READY_NOT_VALID) { 20878 switch (cmd) { 20879 case DKIOCGAPART: 20880 case DKIOCGGEOM: 20881 case DKIOCSGEOM: 20882 case DKIOCGVTOC: 20883 case DKIOCSVTOC: 20884 case DKIOCSAPART: 20885 case DKIOCG_PHYGEOM: 20886 case DKIOCG_VIRTGEOM: 20887 err = ENOTSUP; 20888 un->un_ncmds_in_driver--; 20889 ASSERT(un->un_ncmds_in_driver >= 0); 20890 mutex_exit(SD_MUTEX(un)); 20891 return (err); 20892 } 20893 } 20894 if (err != SD_READY_VALID) { 20895 switch (cmd) { 20896 case DKIOCSTATE: 20897 case CDROMGDRVSPEED: 20898 case CDROMSDRVSPEED: 20899 case FDEJECT: /* for eject command */ 20900 case DKIOCEJECT: 20901 case CDROMEJECT: 20902 case DKIOCGETEFI: 20903 case DKIOCSGEOM: 20904 case DKIOCREMOVABLE: 20905 case DKIOCHOTPLUGGABLE: 20906 case DKIOCSAPART: 20907 case DKIOCSETEFI: 20908 break; 20909 default: 20910 if (un->un_f_has_removable_media) { 20911 err = ENXIO; 20912 } else { 20913 /* Do not map EACCES to EIO */ 20914 if (err != EACCES) 20915 err = EIO; 20916 } 20917 un->un_ncmds_in_driver--; 20918 ASSERT(un->un_ncmds_in_driver >= 0); 20919 mutex_exit(SD_MUTEX(un)); 20920 return (err); 20921 } 20922 } 20923 geom_validated = TRUE; 20924 } 20925 if ((un->un_f_geometry_is_valid == TRUE) && 20926 (un->un_solaris_size > 0)) { 20927 /* 20928 * the "geometry_is_valid" flag could be true if we 20929 * have an fdisk table but no Solaris partition 20930 */ 20931 if (un->un_vtoc.v_sanity != VTOC_SANE) { 20932 /* it is EFI, so return ENOTSUP for these */ 20933 switch (cmd) { 20934 case DKIOCGAPART: 20935 case DKIOCGGEOM: 20936 case DKIOCGVTOC: 20937 case DKIOCSVTOC: 20938 case DKIOCSAPART: 20939 err = ENOTSUP; 20940 un->un_ncmds_in_driver--; 20941 ASSERT(un->un_ncmds_in_driver >= 0); 20942 mutex_exit(SD_MUTEX(un)); 20943 return (err); 20944 } 20945 } 20946 } 20947 20948 skip_ready_valid: 20949 mutex_exit(SD_MUTEX(un)); 20950 20951 switch (cmd) { 20952 case DKIOCINFO: 20953 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 20954 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 20955 break; 20956 20957 case DKIOCGMEDIAINFO: 20958 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 20959 err = sd_get_media_info(dev, (caddr_t)arg, flag); 20960 break; 20961 20962 case DKIOCGGEOM: 20963 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 20964 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 20965 geom_validated); 20966 break; 20967 20968 case DKIOCSGEOM: 20969 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 20970 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 20971 break; 20972 20973 case DKIOCGAPART: 20974 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 20975 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 20976 geom_validated); 20977 break; 20978 20979 case DKIOCSAPART: 20980 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 20981 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 20982 break; 20983 20984 case DKIOCGVTOC: 20985 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 20986 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 20987 geom_validated); 20988 break; 20989 20990 case DKIOCGETEFI: 20991 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 20992 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 20993 break; 20994 20995 case DKIOCPARTITION: 20996 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 20997 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 20998 break; 20999 21000 case DKIOCSVTOC: 21001 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21002 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21003 break; 21004 21005 case DKIOCSETEFI: 21006 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21007 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21008 break; 21009 21010 case DKIOCGMBOOT: 21011 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21012 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21013 break; 21014 21015 case DKIOCSMBOOT: 21016 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21017 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21018 break; 21019 21020 case DKIOCLOCK: 21021 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21022 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21023 SD_PATH_STANDARD); 21024 break; 21025 21026 case DKIOCUNLOCK: 21027 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21028 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21029 SD_PATH_STANDARD); 21030 break; 21031 21032 case DKIOCSTATE: { 21033 enum dkio_state state; 21034 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21035 21036 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21037 err = EFAULT; 21038 } else { 21039 err = sd_check_media(dev, state); 21040 if (err == 0) { 21041 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21042 sizeof (int), flag) != 0) 21043 err = EFAULT; 21044 } 21045 } 21046 break; 21047 } 21048 21049 case DKIOCREMOVABLE: 21050 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21051 /* 21052 * At present, vold only does automount for removable-media 21053 * devices, in order not to break current applications, we 21054 * still let hopluggable devices pretend to be removable media 21055 * devices for vold. In the near future, once vold is EOL'ed, 21056 * we should remove this workaround. 21057 */ 21058 if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) { 21059 i = 1; 21060 } else { 21061 i = 0; 21062 } 21063 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21064 err = EFAULT; 21065 } else { 21066 err = 0; 21067 } 21068 break; 21069 21070 case DKIOCHOTPLUGGABLE: 21071 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21072 if (un->un_f_is_hotpluggable) { 21073 i = 1; 21074 } else { 21075 i = 0; 21076 } 21077 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21078 err = EFAULT; 21079 } else { 21080 err = 0; 21081 } 21082 break; 21083 21084 case DKIOCGTEMPERATURE: 21085 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21086 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21087 break; 21088 21089 case MHIOCENFAILFAST: 21090 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21091 if ((err = drv_priv(cred_p)) == 0) { 21092 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21093 } 21094 break; 21095 21096 case MHIOCTKOWN: 21097 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21098 if ((err = drv_priv(cred_p)) == 0) { 21099 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21100 } 21101 break; 21102 21103 case MHIOCRELEASE: 21104 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21105 if ((err = drv_priv(cred_p)) == 0) { 21106 err = sd_mhdioc_release(dev); 21107 } 21108 break; 21109 21110 case MHIOCSTATUS: 21111 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21112 if ((err = drv_priv(cred_p)) == 0) { 21113 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21114 case 0: 21115 err = 0; 21116 break; 21117 case EACCES: 21118 *rval_p = 1; 21119 err = 0; 21120 break; 21121 default: 21122 err = EIO; 21123 break; 21124 } 21125 } 21126 break; 21127 21128 case MHIOCQRESERVE: 21129 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21130 if ((err = drv_priv(cred_p)) == 0) { 21131 err = sd_reserve_release(dev, SD_RESERVE); 21132 } 21133 break; 21134 21135 case MHIOCREREGISTERDEVID: 21136 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21137 if (drv_priv(cred_p) == EPERM) { 21138 err = EPERM; 21139 } else if (!un->un_f_devid_supported) { 21140 err = ENOTTY; 21141 } else { 21142 err = sd_mhdioc_register_devid(dev); 21143 } 21144 break; 21145 21146 case MHIOCGRP_INKEYS: 21147 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21148 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21149 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21150 err = ENOTSUP; 21151 } else { 21152 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21153 flag); 21154 } 21155 } 21156 break; 21157 21158 case MHIOCGRP_INRESV: 21159 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21160 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21161 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21162 err = ENOTSUP; 21163 } else { 21164 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21165 } 21166 } 21167 break; 21168 21169 case MHIOCGRP_REGISTER: 21170 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21171 if ((err = drv_priv(cred_p)) != EPERM) { 21172 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21173 err = ENOTSUP; 21174 } else if (arg != NULL) { 21175 mhioc_register_t reg; 21176 if (ddi_copyin((void *)arg, ®, 21177 sizeof (mhioc_register_t), flag) != 0) { 21178 err = EFAULT; 21179 } else { 21180 err = 21181 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21182 un, SD_SCSI3_REGISTER, 21183 (uchar_t *)®); 21184 } 21185 } 21186 } 21187 break; 21188 21189 case MHIOCGRP_RESERVE: 21190 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21191 if ((err = drv_priv(cred_p)) != EPERM) { 21192 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21193 err = ENOTSUP; 21194 } else if (arg != NULL) { 21195 mhioc_resv_desc_t resv_desc; 21196 if (ddi_copyin((void *)arg, &resv_desc, 21197 sizeof (mhioc_resv_desc_t), flag) != 0) { 21198 err = EFAULT; 21199 } else { 21200 err = 21201 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21202 un, SD_SCSI3_RESERVE, 21203 (uchar_t *)&resv_desc); 21204 } 21205 } 21206 } 21207 break; 21208 21209 case MHIOCGRP_PREEMPTANDABORT: 21210 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21211 if ((err = drv_priv(cred_p)) != EPERM) { 21212 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21213 err = ENOTSUP; 21214 } else if (arg != NULL) { 21215 mhioc_preemptandabort_t preempt_abort; 21216 if (ddi_copyin((void *)arg, &preempt_abort, 21217 sizeof (mhioc_preemptandabort_t), 21218 flag) != 0) { 21219 err = EFAULT; 21220 } else { 21221 err = 21222 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21223 un, SD_SCSI3_PREEMPTANDABORT, 21224 (uchar_t *)&preempt_abort); 21225 } 21226 } 21227 } 21228 break; 21229 21230 case MHIOCGRP_REGISTERANDIGNOREKEY: 21231 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21232 if ((err = drv_priv(cred_p)) != EPERM) { 21233 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21234 err = ENOTSUP; 21235 } else if (arg != NULL) { 21236 mhioc_registerandignorekey_t r_and_i; 21237 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21238 sizeof (mhioc_registerandignorekey_t), 21239 flag) != 0) { 21240 err = EFAULT; 21241 } else { 21242 err = 21243 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21244 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21245 (uchar_t *)&r_and_i); 21246 } 21247 } 21248 } 21249 break; 21250 21251 case USCSICMD: 21252 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21253 cr = ddi_get_cred(); 21254 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21255 err = EPERM; 21256 } else { 21257 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21258 } 21259 break; 21260 21261 case CDROMPAUSE: 21262 case CDROMRESUME: 21263 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21264 if (!ISCD(un)) { 21265 err = ENOTTY; 21266 } else { 21267 err = sr_pause_resume(dev, cmd); 21268 } 21269 break; 21270 21271 case CDROMPLAYMSF: 21272 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21273 if (!ISCD(un)) { 21274 err = ENOTTY; 21275 } else { 21276 err = sr_play_msf(dev, (caddr_t)arg, flag); 21277 } 21278 break; 21279 21280 case CDROMPLAYTRKIND: 21281 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21282 #if defined(__i386) || defined(__amd64) 21283 /* 21284 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21285 */ 21286 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21287 #else 21288 if (!ISCD(un)) { 21289 #endif 21290 err = ENOTTY; 21291 } else { 21292 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21293 } 21294 break; 21295 21296 case CDROMREADTOCHDR: 21297 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21298 if (!ISCD(un)) { 21299 err = ENOTTY; 21300 } else { 21301 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21302 } 21303 break; 21304 21305 case CDROMREADTOCENTRY: 21306 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21307 if (!ISCD(un)) { 21308 err = ENOTTY; 21309 } else { 21310 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21311 } 21312 break; 21313 21314 case CDROMSTOP: 21315 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21316 if (!ISCD(un)) { 21317 err = ENOTTY; 21318 } else { 21319 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21320 SD_PATH_STANDARD); 21321 } 21322 break; 21323 21324 case CDROMSTART: 21325 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21326 if (!ISCD(un)) { 21327 err = ENOTTY; 21328 } else { 21329 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21330 SD_PATH_STANDARD); 21331 } 21332 break; 21333 21334 case CDROMCLOSETRAY: 21335 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21336 if (!ISCD(un)) { 21337 err = ENOTTY; 21338 } else { 21339 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21340 SD_PATH_STANDARD); 21341 } 21342 break; 21343 21344 case FDEJECT: /* for eject command */ 21345 case DKIOCEJECT: 21346 case CDROMEJECT: 21347 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21348 if (!un->un_f_eject_media_supported) { 21349 err = ENOTTY; 21350 } else { 21351 err = sr_eject(dev); 21352 } 21353 break; 21354 21355 case CDROMVOLCTRL: 21356 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21357 if (!ISCD(un)) { 21358 err = ENOTTY; 21359 } else { 21360 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21361 } 21362 break; 21363 21364 case CDROMSUBCHNL: 21365 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21366 if (!ISCD(un)) { 21367 err = ENOTTY; 21368 } else { 21369 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21370 } 21371 break; 21372 21373 case CDROMREADMODE2: 21374 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21375 if (!ISCD(un)) { 21376 err = ENOTTY; 21377 } else if (un->un_f_cfg_is_atapi == TRUE) { 21378 /* 21379 * If the drive supports READ CD, use that instead of 21380 * switching the LBA size via a MODE SELECT 21381 * Block Descriptor 21382 */ 21383 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21384 } else { 21385 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21386 } 21387 break; 21388 21389 case CDROMREADMODE1: 21390 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21391 if (!ISCD(un)) { 21392 err = ENOTTY; 21393 } else { 21394 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21395 } 21396 break; 21397 21398 case CDROMREADOFFSET: 21399 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21400 if (!ISCD(un)) { 21401 err = ENOTTY; 21402 } else { 21403 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21404 flag); 21405 } 21406 break; 21407 21408 case CDROMSBLKMODE: 21409 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21410 /* 21411 * There is no means of changing block size in case of atapi 21412 * drives, thus return ENOTTY if drive type is atapi 21413 */ 21414 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21415 err = ENOTTY; 21416 } else if (un->un_f_mmc_cap == TRUE) { 21417 21418 /* 21419 * MMC Devices do not support changing the 21420 * logical block size 21421 * 21422 * Note: EINVAL is being returned instead of ENOTTY to 21423 * maintain consistancy with the original mmc 21424 * driver update. 21425 */ 21426 err = EINVAL; 21427 } else { 21428 mutex_enter(SD_MUTEX(un)); 21429 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21430 (un->un_ncmds_in_transport > 0)) { 21431 mutex_exit(SD_MUTEX(un)); 21432 err = EINVAL; 21433 } else { 21434 mutex_exit(SD_MUTEX(un)); 21435 err = sr_change_blkmode(dev, cmd, arg, flag); 21436 } 21437 } 21438 break; 21439 21440 case CDROMGBLKMODE: 21441 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21442 if (!ISCD(un)) { 21443 err = ENOTTY; 21444 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21445 (un->un_f_blockcount_is_valid != FALSE)) { 21446 /* 21447 * Drive is an ATAPI drive so return target block 21448 * size for ATAPI drives since we cannot change the 21449 * blocksize on ATAPI drives. Used primarily to detect 21450 * if an ATAPI cdrom is present. 21451 */ 21452 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21453 sizeof (int), flag) != 0) { 21454 err = EFAULT; 21455 } else { 21456 err = 0; 21457 } 21458 21459 } else { 21460 /* 21461 * Drive supports changing block sizes via a Mode 21462 * Select. 21463 */ 21464 err = sr_change_blkmode(dev, cmd, arg, flag); 21465 } 21466 break; 21467 21468 case CDROMGDRVSPEED: 21469 case CDROMSDRVSPEED: 21470 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21471 if (!ISCD(un)) { 21472 err = ENOTTY; 21473 } else if (un->un_f_mmc_cap == TRUE) { 21474 /* 21475 * Note: In the future the driver implementation 21476 * for getting and 21477 * setting cd speed should entail: 21478 * 1) If non-mmc try the Toshiba mode page 21479 * (sr_change_speed) 21480 * 2) If mmc but no support for Real Time Streaming try 21481 * the SET CD SPEED (0xBB) command 21482 * (sr_atapi_change_speed) 21483 * 3) If mmc and support for Real Time Streaming 21484 * try the GET PERFORMANCE and SET STREAMING 21485 * commands (not yet implemented, 4380808) 21486 */ 21487 /* 21488 * As per recent MMC spec, CD-ROM speed is variable 21489 * and changes with LBA. Since there is no such 21490 * things as drive speed now, fail this ioctl. 21491 * 21492 * Note: EINVAL is returned for consistancy of original 21493 * implementation which included support for getting 21494 * the drive speed of mmc devices but not setting 21495 * the drive speed. Thus EINVAL would be returned 21496 * if a set request was made for an mmc device. 21497 * We no longer support get or set speed for 21498 * mmc but need to remain consistant with regard 21499 * to the error code returned. 21500 */ 21501 err = EINVAL; 21502 } else if (un->un_f_cfg_is_atapi == TRUE) { 21503 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21504 } else { 21505 err = sr_change_speed(dev, cmd, arg, flag); 21506 } 21507 break; 21508 21509 case CDROMCDDA: 21510 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21511 if (!ISCD(un)) { 21512 err = ENOTTY; 21513 } else { 21514 err = sr_read_cdda(dev, (void *)arg, flag); 21515 } 21516 break; 21517 21518 case CDROMCDXA: 21519 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21520 if (!ISCD(un)) { 21521 err = ENOTTY; 21522 } else { 21523 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21524 } 21525 break; 21526 21527 case CDROMSUBCODE: 21528 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21529 if (!ISCD(un)) { 21530 err = ENOTTY; 21531 } else { 21532 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21533 } 21534 break; 21535 21536 case DKIOCPARTINFO: { 21537 /* 21538 * Return parameters describing the selected disk slice. 21539 * Note: this ioctl is for the intel platform only 21540 */ 21541 #if defined(__i386) || defined(__amd64) 21542 int part; 21543 21544 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21545 part = SDPART(dev); 21546 21547 /* don't check un_solaris_size for pN */ 21548 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21549 err = EIO; 21550 } else { 21551 struct part_info p; 21552 21553 p.p_start = (daddr_t)un->un_offset[part]; 21554 p.p_length = (int)un->un_map[part].dkl_nblk; 21555 #ifdef _MULTI_DATAMODEL 21556 switch (ddi_model_convert_from(flag & FMODELS)) { 21557 case DDI_MODEL_ILP32: 21558 { 21559 struct part_info32 p32; 21560 21561 p32.p_start = (daddr32_t)p.p_start; 21562 p32.p_length = p.p_length; 21563 if (ddi_copyout(&p32, (void *)arg, 21564 sizeof (p32), flag)) 21565 err = EFAULT; 21566 break; 21567 } 21568 21569 case DDI_MODEL_NONE: 21570 { 21571 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21572 flag)) 21573 err = EFAULT; 21574 break; 21575 } 21576 } 21577 #else /* ! _MULTI_DATAMODEL */ 21578 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21579 err = EFAULT; 21580 #endif /* _MULTI_DATAMODEL */ 21581 } 21582 #else 21583 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21584 err = ENOTTY; 21585 #endif 21586 break; 21587 } 21588 21589 case DKIOCG_PHYGEOM: { 21590 /* Return the driver's notion of the media physical geometry */ 21591 #if defined(__i386) || defined(__amd64) 21592 struct dk_geom disk_geom; 21593 struct dk_geom *dkgp = &disk_geom; 21594 21595 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21596 mutex_enter(SD_MUTEX(un)); 21597 21598 if (un->un_g.dkg_nhead != 0 && 21599 un->un_g.dkg_nsect != 0) { 21600 /* 21601 * We succeeded in getting a geometry, but 21602 * right now it is being reported as just the 21603 * Solaris fdisk partition, just like for 21604 * DKIOCGGEOM. We need to change that to be 21605 * correct for the entire disk now. 21606 */ 21607 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21608 dkgp->dkg_acyl = 0; 21609 dkgp->dkg_ncyl = un->un_blockcount / 21610 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21611 } else { 21612 bzero(dkgp, sizeof (struct dk_geom)); 21613 /* 21614 * This disk does not have a Solaris VTOC 21615 * so we must present a physical geometry 21616 * that will remain consistent regardless 21617 * of how the disk is used. This will ensure 21618 * that the geometry does not change regardless 21619 * of the fdisk partition type (ie. EFI, FAT32, 21620 * Solaris, etc). 21621 */ 21622 if (ISCD(un)) { 21623 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21624 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21625 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21626 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21627 } else { 21628 /* 21629 * Invalid un_blockcount can generate invalid 21630 * dk_geom and may result in division by zero 21631 * system failure. Should make sure blockcount 21632 * is valid before using it here. 21633 */ 21634 if (un->un_f_blockcount_is_valid == FALSE) { 21635 mutex_exit(SD_MUTEX(un)); 21636 err = EIO; 21637 21638 break; 21639 } 21640 sd_convert_geometry(un->un_blockcount, dkgp); 21641 dkgp->dkg_acyl = 0; 21642 dkgp->dkg_ncyl = un->un_blockcount / 21643 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21644 } 21645 } 21646 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21647 21648 if (ddi_copyout(dkgp, (void *)arg, 21649 sizeof (struct dk_geom), flag)) { 21650 mutex_exit(SD_MUTEX(un)); 21651 err = EFAULT; 21652 } else { 21653 mutex_exit(SD_MUTEX(un)); 21654 err = 0; 21655 } 21656 #else 21657 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21658 err = ENOTTY; 21659 #endif 21660 break; 21661 } 21662 21663 case DKIOCG_VIRTGEOM: { 21664 /* Return the driver's notion of the media's logical geometry */ 21665 #if defined(__i386) || defined(__amd64) 21666 struct dk_geom disk_geom; 21667 struct dk_geom *dkgp = &disk_geom; 21668 21669 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21670 mutex_enter(SD_MUTEX(un)); 21671 /* 21672 * If there is no HBA geometry available, or 21673 * if the HBA returned us something that doesn't 21674 * really fit into an Int 13/function 8 geometry 21675 * result, just fail the ioctl. See PSARC 1998/313. 21676 */ 21677 if (un->un_lgeom.g_nhead == 0 || 21678 un->un_lgeom.g_nsect == 0 || 21679 un->un_lgeom.g_ncyl > 1024) { 21680 mutex_exit(SD_MUTEX(un)); 21681 err = EINVAL; 21682 } else { 21683 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21684 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21685 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21686 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21687 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21688 21689 if (ddi_copyout(dkgp, (void *)arg, 21690 sizeof (struct dk_geom), flag)) { 21691 mutex_exit(SD_MUTEX(un)); 21692 err = EFAULT; 21693 } else { 21694 mutex_exit(SD_MUTEX(un)); 21695 err = 0; 21696 } 21697 } 21698 #else 21699 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21700 err = ENOTTY; 21701 #endif 21702 break; 21703 } 21704 #ifdef SDDEBUG 21705 /* RESET/ABORTS testing ioctls */ 21706 case DKIOCRESET: { 21707 int reset_level; 21708 21709 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21710 err = EFAULT; 21711 } else { 21712 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21713 "reset_level = 0x%lx\n", reset_level); 21714 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21715 err = 0; 21716 } else { 21717 err = EIO; 21718 } 21719 } 21720 break; 21721 } 21722 21723 case DKIOCABORT: 21724 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21725 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21726 err = 0; 21727 } else { 21728 err = EIO; 21729 } 21730 break; 21731 #endif 21732 21733 #ifdef SD_FAULT_INJECTION 21734 /* SDIOC FaultInjection testing ioctls */ 21735 case SDIOCSTART: 21736 case SDIOCSTOP: 21737 case SDIOCINSERTPKT: 21738 case SDIOCINSERTXB: 21739 case SDIOCINSERTUN: 21740 case SDIOCINSERTARQ: 21741 case SDIOCPUSH: 21742 case SDIOCRETRIEVE: 21743 case SDIOCRUN: 21744 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21745 "SDIOC detected cmd:0x%X:\n", cmd); 21746 /* call error generator */ 21747 sd_faultinjection_ioctl(cmd, arg, un); 21748 err = 0; 21749 break; 21750 21751 #endif /* SD_FAULT_INJECTION */ 21752 21753 case DKIOCFLUSHWRITECACHE: 21754 { 21755 struct dk_callback *dkc = (struct dk_callback *)arg; 21756 21757 mutex_enter(SD_MUTEX(un)); 21758 if (!un->un_f_sync_cache_supported || 21759 !un->un_f_write_cache_enabled) { 21760 err = un->un_f_sync_cache_supported ? 21761 0 : ENOTSUP; 21762 mutex_exit(SD_MUTEX(un)); 21763 if ((flag & FKIOCTL) && dkc != NULL && 21764 dkc->dkc_callback != NULL) { 21765 (*dkc->dkc_callback)(dkc->dkc_cookie, 21766 err); 21767 /* 21768 * Did callback and reported error. 21769 * Since we did a callback, ioctl 21770 * should return 0. 21771 */ 21772 err = 0; 21773 } 21774 break; 21775 } 21776 mutex_exit(SD_MUTEX(un)); 21777 21778 if ((flag & FKIOCTL) && dkc != NULL && 21779 dkc->dkc_callback != NULL) { 21780 /* async SYNC CACHE request */ 21781 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 21782 } else { 21783 /* synchronous SYNC CACHE request */ 21784 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21785 } 21786 } 21787 break; 21788 21789 default: 21790 err = ENOTTY; 21791 break; 21792 } 21793 mutex_enter(SD_MUTEX(un)); 21794 un->un_ncmds_in_driver--; 21795 ASSERT(un->un_ncmds_in_driver >= 0); 21796 mutex_exit(SD_MUTEX(un)); 21797 21798 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 21799 return (err); 21800 } 21801 21802 21803 /* 21804 * Function: sd_uscsi_ioctl 21805 * 21806 * Description: This routine is the driver entry point for handling USCSI ioctl 21807 * requests (USCSICMD). 21808 * 21809 * Arguments: dev - the device number 21810 * arg - user provided scsi command 21811 * flag - this argument is a pass through to ddi_copyxxx() 21812 * directly from the mode argument of ioctl(). 21813 * 21814 * Return Code: code returned by sd_send_scsi_cmd 21815 * ENXIO 21816 * EFAULT 21817 * EAGAIN 21818 */ 21819 21820 static int 21821 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 21822 { 21823 #ifdef _MULTI_DATAMODEL 21824 /* 21825 * For use when a 32 bit app makes a call into a 21826 * 64 bit ioctl 21827 */ 21828 struct uscsi_cmd32 uscsi_cmd_32_for_64; 21829 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 21830 model_t model; 21831 #endif /* _MULTI_DATAMODEL */ 21832 struct uscsi_cmd *scmd = NULL; 21833 struct sd_lun *un = NULL; 21834 enum uio_seg uioseg; 21835 char cdb[CDB_GROUP0]; 21836 int rval = 0; 21837 21838 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21839 return (ENXIO); 21840 } 21841 21842 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 21843 21844 scmd = (struct uscsi_cmd *) 21845 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21846 21847 #ifdef _MULTI_DATAMODEL 21848 switch (model = ddi_model_convert_from(flag & FMODELS)) { 21849 case DDI_MODEL_ILP32: 21850 { 21851 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 21852 rval = EFAULT; 21853 goto done; 21854 } 21855 /* 21856 * Convert the ILP32 uscsi data from the 21857 * application to LP64 for internal use. 21858 */ 21859 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 21860 break; 21861 } 21862 case DDI_MODEL_NONE: 21863 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21864 rval = EFAULT; 21865 goto done; 21866 } 21867 break; 21868 } 21869 #else /* ! _MULTI_DATAMODEL */ 21870 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21871 rval = EFAULT; 21872 goto done; 21873 } 21874 #endif /* _MULTI_DATAMODEL */ 21875 21876 scmd->uscsi_flags &= ~USCSI_NOINTR; 21877 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 21878 if (un->un_f_format_in_progress == TRUE) { 21879 rval = EAGAIN; 21880 goto done; 21881 } 21882 21883 /* 21884 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 21885 * we will have a valid cdb[0] to test. 21886 */ 21887 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 21888 (cdb[0] == SCMD_FORMAT)) { 21889 SD_TRACE(SD_LOG_IOCTL, un, 21890 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21891 mutex_enter(SD_MUTEX(un)); 21892 un->un_f_format_in_progress = TRUE; 21893 mutex_exit(SD_MUTEX(un)); 21894 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21895 SD_PATH_STANDARD); 21896 mutex_enter(SD_MUTEX(un)); 21897 un->un_f_format_in_progress = FALSE; 21898 mutex_exit(SD_MUTEX(un)); 21899 } else { 21900 SD_TRACE(SD_LOG_IOCTL, un, 21901 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21902 /* 21903 * It's OK to fall into here even if the ddi_copyin() 21904 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 21905 * does this same copyin and will return the EFAULT 21906 * if it fails. 21907 */ 21908 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21909 SD_PATH_STANDARD); 21910 } 21911 #ifdef _MULTI_DATAMODEL 21912 switch (model) { 21913 case DDI_MODEL_ILP32: 21914 /* 21915 * Convert back to ILP32 before copyout to the 21916 * application 21917 */ 21918 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 21919 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 21920 if (rval != 0) { 21921 rval = EFAULT; 21922 } 21923 } 21924 break; 21925 case DDI_MODEL_NONE: 21926 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21927 if (rval != 0) { 21928 rval = EFAULT; 21929 } 21930 } 21931 break; 21932 } 21933 #else /* ! _MULTI_DATAMODE */ 21934 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21935 if (rval != 0) { 21936 rval = EFAULT; 21937 } 21938 } 21939 #endif /* _MULTI_DATAMODE */ 21940 done: 21941 kmem_free(scmd, sizeof (struct uscsi_cmd)); 21942 21943 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 21944 21945 return (rval); 21946 } 21947 21948 21949 /* 21950 * Function: sd_dkio_ctrl_info 21951 * 21952 * Description: This routine is the driver entry point for handling controller 21953 * information ioctl requests (DKIOCINFO). 21954 * 21955 * Arguments: dev - the device number 21956 * arg - pointer to user provided dk_cinfo structure 21957 * specifying the controller type and attributes. 21958 * flag - this argument is a pass through to ddi_copyxxx() 21959 * directly from the mode argument of ioctl(). 21960 * 21961 * Return Code: 0 21962 * EFAULT 21963 * ENXIO 21964 */ 21965 21966 static int 21967 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 21968 { 21969 struct sd_lun *un = NULL; 21970 struct dk_cinfo *info; 21971 dev_info_t *pdip; 21972 int lun, tgt; 21973 21974 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21975 return (ENXIO); 21976 } 21977 21978 info = (struct dk_cinfo *) 21979 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 21980 21981 switch (un->un_ctype) { 21982 case CTYPE_CDROM: 21983 info->dki_ctype = DKC_CDROM; 21984 break; 21985 default: 21986 info->dki_ctype = DKC_SCSI_CCS; 21987 break; 21988 } 21989 pdip = ddi_get_parent(SD_DEVINFO(un)); 21990 info->dki_cnum = ddi_get_instance(pdip); 21991 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 21992 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 21993 } else { 21994 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 21995 DK_DEVLEN - 1); 21996 } 21997 21998 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 21999 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22000 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22001 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22002 22003 /* Unit Information */ 22004 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22005 info->dki_slave = ((tgt << 3) | lun); 22006 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22007 DK_DEVLEN - 1); 22008 info->dki_flags = DKI_FMTVOL; 22009 info->dki_partition = SDPART(dev); 22010 22011 /* Max Transfer size of this device in blocks */ 22012 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22013 info->dki_addr = 0; 22014 info->dki_space = 0; 22015 info->dki_prio = 0; 22016 info->dki_vec = 0; 22017 22018 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22019 kmem_free(info, sizeof (struct dk_cinfo)); 22020 return (EFAULT); 22021 } else { 22022 kmem_free(info, sizeof (struct dk_cinfo)); 22023 return (0); 22024 } 22025 } 22026 22027 22028 /* 22029 * Function: sd_get_media_info 22030 * 22031 * Description: This routine is the driver entry point for handling ioctl 22032 * requests for the media type or command set profile used by the 22033 * drive to operate on the media (DKIOCGMEDIAINFO). 22034 * 22035 * Arguments: dev - the device number 22036 * arg - pointer to user provided dk_minfo structure 22037 * specifying the media type, logical block size and 22038 * drive capacity. 22039 * flag - this argument is a pass through to ddi_copyxxx() 22040 * directly from the mode argument of ioctl(). 22041 * 22042 * Return Code: 0 22043 * EACCESS 22044 * EFAULT 22045 * ENXIO 22046 * EIO 22047 */ 22048 22049 static int 22050 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22051 { 22052 struct sd_lun *un = NULL; 22053 struct uscsi_cmd com; 22054 struct scsi_inquiry *sinq; 22055 struct dk_minfo media_info; 22056 u_longlong_t media_capacity; 22057 uint64_t capacity; 22058 uint_t lbasize; 22059 uchar_t *out_data; 22060 uchar_t *rqbuf; 22061 int rval = 0; 22062 int rtn; 22063 22064 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22065 (un->un_state == SD_STATE_OFFLINE)) { 22066 return (ENXIO); 22067 } 22068 22069 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22070 22071 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22072 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22073 22074 /* Issue a TUR to determine if the drive is ready with media present */ 22075 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22076 if (rval == ENXIO) { 22077 goto done; 22078 } 22079 22080 /* Now get configuration data */ 22081 if (ISCD(un)) { 22082 media_info.dki_media_type = DK_CDROM; 22083 22084 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22085 if (un->un_f_mmc_cap == TRUE) { 22086 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22087 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22088 22089 if (rtn) { 22090 /* 22091 * Failed for other than an illegal request 22092 * or command not supported 22093 */ 22094 if ((com.uscsi_status == STATUS_CHECK) && 22095 (com.uscsi_rqstatus == STATUS_GOOD)) { 22096 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22097 (rqbuf[12] != 0x20)) { 22098 rval = EIO; 22099 goto done; 22100 } 22101 } 22102 } else { 22103 /* 22104 * The GET CONFIGURATION command succeeded 22105 * so set the media type according to the 22106 * returned data 22107 */ 22108 media_info.dki_media_type = out_data[6]; 22109 media_info.dki_media_type <<= 8; 22110 media_info.dki_media_type |= out_data[7]; 22111 } 22112 } 22113 } else { 22114 /* 22115 * The profile list is not available, so we attempt to identify 22116 * the media type based on the inquiry data 22117 */ 22118 sinq = un->un_sd->sd_inq; 22119 if (sinq->inq_qual == 0) { 22120 /* This is a direct access device */ 22121 media_info.dki_media_type = DK_FIXED_DISK; 22122 22123 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22124 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22125 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22126 media_info.dki_media_type = DK_ZIP; 22127 } else if ( 22128 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22129 media_info.dki_media_type = DK_JAZ; 22130 } 22131 } 22132 } else { 22133 /* Not a CD or direct access so return unknown media */ 22134 media_info.dki_media_type = DK_UNKNOWN; 22135 } 22136 } 22137 22138 /* Now read the capacity so we can provide the lbasize and capacity */ 22139 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22140 SD_PATH_DIRECT)) { 22141 case 0: 22142 break; 22143 case EACCES: 22144 rval = EACCES; 22145 goto done; 22146 default: 22147 rval = EIO; 22148 goto done; 22149 } 22150 22151 media_info.dki_lbsize = lbasize; 22152 media_capacity = capacity; 22153 22154 /* 22155 * sd_send_scsi_READ_CAPACITY() reports capacity in 22156 * un->un_sys_blocksize chunks. So we need to convert it into 22157 * cap.lbasize chunks. 22158 */ 22159 media_capacity *= un->un_sys_blocksize; 22160 media_capacity /= lbasize; 22161 media_info.dki_capacity = media_capacity; 22162 22163 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22164 rval = EFAULT; 22165 /* Put goto. Anybody might add some code below in future */ 22166 goto done; 22167 } 22168 done: 22169 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22170 kmem_free(rqbuf, SENSE_LENGTH); 22171 return (rval); 22172 } 22173 22174 22175 /* 22176 * Function: sd_dkio_get_geometry 22177 * 22178 * Description: This routine is the driver entry point for handling user 22179 * requests to get the device geometry (DKIOCGGEOM). 22180 * 22181 * Arguments: dev - the device number 22182 * arg - pointer to user provided dk_geom structure specifying 22183 * the controller's notion of the current geometry. 22184 * flag - this argument is a pass through to ddi_copyxxx() 22185 * directly from the mode argument of ioctl(). 22186 * geom_validated - flag indicating if the device geometry has been 22187 * previously validated in the sdioctl routine. 22188 * 22189 * Return Code: 0 22190 * EFAULT 22191 * ENXIO 22192 * EIO 22193 */ 22194 22195 static int 22196 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22197 { 22198 struct sd_lun *un = NULL; 22199 struct dk_geom *tmp_geom = NULL; 22200 int rval = 0; 22201 22202 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22203 return (ENXIO); 22204 } 22205 22206 #if defined(__i386) || defined(__amd64) 22207 if (un->un_solaris_size == 0) { 22208 return (EIO); 22209 } 22210 #endif 22211 if (geom_validated == FALSE) { 22212 /* 22213 * sd_validate_geometry does not spin a disk up 22214 * if it was spun down. We need to make sure it 22215 * is ready. 22216 */ 22217 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22218 return (rval); 22219 } 22220 mutex_enter(SD_MUTEX(un)); 22221 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22222 mutex_exit(SD_MUTEX(un)); 22223 } 22224 if (rval) 22225 return (rval); 22226 22227 /* 22228 * Make a local copy of the soft state geometry to avoid some potential 22229 * race conditions associated with holding the mutex and updating the 22230 * write_reinstruct value 22231 */ 22232 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22233 mutex_enter(SD_MUTEX(un)); 22234 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22235 mutex_exit(SD_MUTEX(un)); 22236 22237 if (tmp_geom->dkg_write_reinstruct == 0) { 22238 tmp_geom->dkg_write_reinstruct = 22239 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22240 sd_rot_delay) / (int)60000); 22241 } 22242 22243 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22244 flag); 22245 if (rval != 0) { 22246 rval = EFAULT; 22247 } 22248 22249 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22250 return (rval); 22251 22252 } 22253 22254 22255 /* 22256 * Function: sd_dkio_set_geometry 22257 * 22258 * Description: This routine is the driver entry point for handling user 22259 * requests to set the device geometry (DKIOCSGEOM). The actual 22260 * device geometry is not updated, just the driver "notion" of it. 22261 * 22262 * Arguments: dev - the device number 22263 * arg - pointer to user provided dk_geom structure used to set 22264 * the controller's notion of the current geometry. 22265 * flag - this argument is a pass through to ddi_copyxxx() 22266 * directly from the mode argument of ioctl(). 22267 * 22268 * Return Code: 0 22269 * EFAULT 22270 * ENXIO 22271 * EIO 22272 */ 22273 22274 static int 22275 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22276 { 22277 struct sd_lun *un = NULL; 22278 struct dk_geom *tmp_geom; 22279 struct dk_map *lp; 22280 int rval = 0; 22281 int i; 22282 22283 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22284 return (ENXIO); 22285 } 22286 22287 #if defined(__i386) || defined(__amd64) 22288 if (un->un_solaris_size == 0) { 22289 return (EIO); 22290 } 22291 #endif 22292 /* 22293 * We need to copy the user specified geometry into local 22294 * storage and then update the softstate. We don't want to hold 22295 * the mutex and copyin directly from the user to the soft state 22296 */ 22297 tmp_geom = (struct dk_geom *) 22298 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22299 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22300 if (rval != 0) { 22301 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22302 return (EFAULT); 22303 } 22304 22305 mutex_enter(SD_MUTEX(un)); 22306 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22307 for (i = 0; i < NDKMAP; i++) { 22308 lp = &un->un_map[i]; 22309 un->un_offset[i] = 22310 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22311 #if defined(__i386) || defined(__amd64) 22312 un->un_offset[i] += un->un_solaris_offset; 22313 #endif 22314 } 22315 un->un_f_geometry_is_valid = FALSE; 22316 mutex_exit(SD_MUTEX(un)); 22317 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22318 22319 return (rval); 22320 } 22321 22322 22323 /* 22324 * Function: sd_dkio_get_partition 22325 * 22326 * Description: This routine is the driver entry point for handling user 22327 * requests to get the partition table (DKIOCGAPART). 22328 * 22329 * Arguments: dev - the device number 22330 * arg - pointer to user provided dk_allmap structure specifying 22331 * the controller's notion of the current partition table. 22332 * flag - this argument is a pass through to ddi_copyxxx() 22333 * directly from the mode argument of ioctl(). 22334 * geom_validated - flag indicating if the device geometry has been 22335 * previously validated in the sdioctl routine. 22336 * 22337 * Return Code: 0 22338 * EFAULT 22339 * ENXIO 22340 * EIO 22341 */ 22342 22343 static int 22344 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22345 { 22346 struct sd_lun *un = NULL; 22347 int rval = 0; 22348 int size; 22349 22350 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22351 return (ENXIO); 22352 } 22353 22354 #if defined(__i386) || defined(__amd64) 22355 if (un->un_solaris_size == 0) { 22356 return (EIO); 22357 } 22358 #endif 22359 /* 22360 * Make sure the geometry is valid before getting the partition 22361 * information. 22362 */ 22363 mutex_enter(SD_MUTEX(un)); 22364 if (geom_validated == FALSE) { 22365 /* 22366 * sd_validate_geometry does not spin a disk up 22367 * if it was spun down. We need to make sure it 22368 * is ready before validating the geometry. 22369 */ 22370 mutex_exit(SD_MUTEX(un)); 22371 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22372 return (rval); 22373 } 22374 mutex_enter(SD_MUTEX(un)); 22375 22376 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22377 mutex_exit(SD_MUTEX(un)); 22378 return (rval); 22379 } 22380 } 22381 mutex_exit(SD_MUTEX(un)); 22382 22383 #ifdef _MULTI_DATAMODEL 22384 switch (ddi_model_convert_from(flag & FMODELS)) { 22385 case DDI_MODEL_ILP32: { 22386 struct dk_map32 dk_map32[NDKMAP]; 22387 int i; 22388 22389 for (i = 0; i < NDKMAP; i++) { 22390 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22391 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22392 } 22393 size = NDKMAP * sizeof (struct dk_map32); 22394 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22395 if (rval != 0) { 22396 rval = EFAULT; 22397 } 22398 break; 22399 } 22400 case DDI_MODEL_NONE: 22401 size = NDKMAP * sizeof (struct dk_map); 22402 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22403 if (rval != 0) { 22404 rval = EFAULT; 22405 } 22406 break; 22407 } 22408 #else /* ! _MULTI_DATAMODEL */ 22409 size = NDKMAP * sizeof (struct dk_map); 22410 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22411 if (rval != 0) { 22412 rval = EFAULT; 22413 } 22414 #endif /* _MULTI_DATAMODEL */ 22415 return (rval); 22416 } 22417 22418 22419 /* 22420 * Function: sd_dkio_set_partition 22421 * 22422 * Description: This routine is the driver entry point for handling user 22423 * requests to set the partition table (DKIOCSAPART). The actual 22424 * device partition is not updated. 22425 * 22426 * Arguments: dev - the device number 22427 * arg - pointer to user provided dk_allmap structure used to set 22428 * the controller's notion of the partition table. 22429 * flag - this argument is a pass through to ddi_copyxxx() 22430 * directly from the mode argument of ioctl(). 22431 * 22432 * Return Code: 0 22433 * EINVAL 22434 * EFAULT 22435 * ENXIO 22436 * EIO 22437 */ 22438 22439 static int 22440 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22441 { 22442 struct sd_lun *un = NULL; 22443 struct dk_map dk_map[NDKMAP]; 22444 struct dk_map *lp; 22445 int rval = 0; 22446 int size; 22447 int i; 22448 #if defined(_SUNOS_VTOC_16) 22449 struct dkl_partition *vp; 22450 #endif 22451 22452 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22453 return (ENXIO); 22454 } 22455 22456 /* 22457 * Set the map for all logical partitions. We lock 22458 * the priority just to make sure an interrupt doesn't 22459 * come in while the map is half updated. 22460 */ 22461 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22462 mutex_enter(SD_MUTEX(un)); 22463 if (un->un_blockcount > DK_MAX_BLOCKS) { 22464 mutex_exit(SD_MUTEX(un)); 22465 return (ENOTSUP); 22466 } 22467 mutex_exit(SD_MUTEX(un)); 22468 if (un->un_solaris_size == 0) { 22469 return (EIO); 22470 } 22471 22472 #ifdef _MULTI_DATAMODEL 22473 switch (ddi_model_convert_from(flag & FMODELS)) { 22474 case DDI_MODEL_ILP32: { 22475 struct dk_map32 dk_map32[NDKMAP]; 22476 22477 size = NDKMAP * sizeof (struct dk_map32); 22478 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22479 if (rval != 0) { 22480 return (EFAULT); 22481 } 22482 for (i = 0; i < NDKMAP; i++) { 22483 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22484 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22485 } 22486 break; 22487 } 22488 case DDI_MODEL_NONE: 22489 size = NDKMAP * sizeof (struct dk_map); 22490 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22491 if (rval != 0) { 22492 return (EFAULT); 22493 } 22494 break; 22495 } 22496 #else /* ! _MULTI_DATAMODEL */ 22497 size = NDKMAP * sizeof (struct dk_map); 22498 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22499 if (rval != 0) { 22500 return (EFAULT); 22501 } 22502 #endif /* _MULTI_DATAMODEL */ 22503 22504 mutex_enter(SD_MUTEX(un)); 22505 /* Note: The size used in this bcopy is set based upon the data model */ 22506 bcopy(dk_map, un->un_map, size); 22507 #if defined(_SUNOS_VTOC_16) 22508 vp = (struct dkl_partition *)&(un->un_vtoc); 22509 #endif /* defined(_SUNOS_VTOC_16) */ 22510 for (i = 0; i < NDKMAP; i++) { 22511 lp = &un->un_map[i]; 22512 un->un_offset[i] = 22513 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22514 #if defined(_SUNOS_VTOC_16) 22515 vp->p_start = un->un_offset[i]; 22516 vp->p_size = lp->dkl_nblk; 22517 vp++; 22518 #endif /* defined(_SUNOS_VTOC_16) */ 22519 #if defined(__i386) || defined(__amd64) 22520 un->un_offset[i] += un->un_solaris_offset; 22521 #endif 22522 } 22523 mutex_exit(SD_MUTEX(un)); 22524 return (rval); 22525 } 22526 22527 22528 /* 22529 * Function: sd_dkio_get_vtoc 22530 * 22531 * Description: This routine is the driver entry point for handling user 22532 * requests to get the current volume table of contents 22533 * (DKIOCGVTOC). 22534 * 22535 * Arguments: dev - the device number 22536 * arg - pointer to user provided vtoc structure specifying 22537 * the current vtoc. 22538 * flag - this argument is a pass through to ddi_copyxxx() 22539 * directly from the mode argument of ioctl(). 22540 * geom_validated - flag indicating if the device geometry has been 22541 * previously validated in the sdioctl routine. 22542 * 22543 * Return Code: 0 22544 * EFAULT 22545 * ENXIO 22546 * EIO 22547 */ 22548 22549 static int 22550 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22551 { 22552 struct sd_lun *un = NULL; 22553 #if defined(_SUNOS_VTOC_8) 22554 struct vtoc user_vtoc; 22555 #endif /* defined(_SUNOS_VTOC_8) */ 22556 int rval = 0; 22557 22558 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22559 return (ENXIO); 22560 } 22561 22562 mutex_enter(SD_MUTEX(un)); 22563 if (geom_validated == FALSE) { 22564 /* 22565 * sd_validate_geometry does not spin a disk up 22566 * if it was spun down. We need to make sure it 22567 * is ready. 22568 */ 22569 mutex_exit(SD_MUTEX(un)); 22570 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22571 return (rval); 22572 } 22573 mutex_enter(SD_MUTEX(un)); 22574 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22575 mutex_exit(SD_MUTEX(un)); 22576 return (rval); 22577 } 22578 } 22579 22580 #if defined(_SUNOS_VTOC_8) 22581 sd_build_user_vtoc(un, &user_vtoc); 22582 mutex_exit(SD_MUTEX(un)); 22583 22584 #ifdef _MULTI_DATAMODEL 22585 switch (ddi_model_convert_from(flag & FMODELS)) { 22586 case DDI_MODEL_ILP32: { 22587 struct vtoc32 user_vtoc32; 22588 22589 vtoctovtoc32(user_vtoc, user_vtoc32); 22590 if (ddi_copyout(&user_vtoc32, (void *)arg, 22591 sizeof (struct vtoc32), flag)) { 22592 return (EFAULT); 22593 } 22594 break; 22595 } 22596 22597 case DDI_MODEL_NONE: 22598 if (ddi_copyout(&user_vtoc, (void *)arg, 22599 sizeof (struct vtoc), flag)) { 22600 return (EFAULT); 22601 } 22602 break; 22603 } 22604 #else /* ! _MULTI_DATAMODEL */ 22605 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22606 return (EFAULT); 22607 } 22608 #endif /* _MULTI_DATAMODEL */ 22609 22610 #elif defined(_SUNOS_VTOC_16) 22611 mutex_exit(SD_MUTEX(un)); 22612 22613 #ifdef _MULTI_DATAMODEL 22614 /* 22615 * The un_vtoc structure is a "struct dk_vtoc" which is always 22616 * 32-bit to maintain compatibility with existing on-disk 22617 * structures. Thus, we need to convert the structure when copying 22618 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22619 * program. If the target is a 32-bit program, then no conversion 22620 * is necessary. 22621 */ 22622 /* LINTED: logical expression always true: op "||" */ 22623 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22624 switch (ddi_model_convert_from(flag & FMODELS)) { 22625 case DDI_MODEL_ILP32: 22626 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22627 sizeof (un->un_vtoc), flag)) { 22628 return (EFAULT); 22629 } 22630 break; 22631 22632 case DDI_MODEL_NONE: { 22633 struct vtoc user_vtoc; 22634 22635 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22636 if (ddi_copyout(&user_vtoc, (void *)arg, 22637 sizeof (struct vtoc), flag)) { 22638 return (EFAULT); 22639 } 22640 break; 22641 } 22642 } 22643 #else /* ! _MULTI_DATAMODEL */ 22644 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22645 flag)) { 22646 return (EFAULT); 22647 } 22648 #endif /* _MULTI_DATAMODEL */ 22649 #else 22650 #error "No VTOC format defined." 22651 #endif 22652 22653 return (rval); 22654 } 22655 22656 static int 22657 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22658 { 22659 struct sd_lun *un = NULL; 22660 dk_efi_t user_efi; 22661 int rval = 0; 22662 void *buffer; 22663 22664 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22665 return (ENXIO); 22666 22667 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22668 return (EFAULT); 22669 22670 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22671 22672 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22673 (user_efi.dki_length > un->un_max_xfer_size)) 22674 return (EINVAL); 22675 22676 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22677 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22678 user_efi.dki_lba, SD_PATH_DIRECT); 22679 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22680 user_efi.dki_length, flag) != 0) 22681 rval = EFAULT; 22682 22683 kmem_free(buffer, user_efi.dki_length); 22684 return (rval); 22685 } 22686 22687 /* 22688 * Function: sd_build_user_vtoc 22689 * 22690 * Description: This routine populates a pass by reference variable with the 22691 * current volume table of contents. 22692 * 22693 * Arguments: un - driver soft state (unit) structure 22694 * user_vtoc - pointer to vtoc structure to be populated 22695 */ 22696 22697 static void 22698 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22699 { 22700 struct dk_map2 *lpart; 22701 struct dk_map *lmap; 22702 struct partition *vpart; 22703 int nblks; 22704 int i; 22705 22706 ASSERT(mutex_owned(SD_MUTEX(un))); 22707 22708 /* 22709 * Return vtoc structure fields in the provided VTOC area, addressed 22710 * by *vtoc. 22711 */ 22712 bzero(user_vtoc, sizeof (struct vtoc)); 22713 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 22714 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 22715 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 22716 user_vtoc->v_sanity = VTOC_SANE; 22717 user_vtoc->v_version = un->un_vtoc.v_version; 22718 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 22719 user_vtoc->v_sectorsz = un->un_sys_blocksize; 22720 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 22721 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 22722 sizeof (un->un_vtoc.v_reserved)); 22723 /* 22724 * Convert partitioning information. 22725 * 22726 * Note the conversion from starting cylinder number 22727 * to starting sector number. 22728 */ 22729 lmap = un->un_map; 22730 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 22731 vpart = user_vtoc->v_part; 22732 22733 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22734 22735 for (i = 0; i < V_NUMPAR; i++) { 22736 vpart->p_tag = lpart->p_tag; 22737 vpart->p_flag = lpart->p_flag; 22738 vpart->p_start = lmap->dkl_cylno * nblks; 22739 vpart->p_size = lmap->dkl_nblk; 22740 lmap++; 22741 lpart++; 22742 vpart++; 22743 22744 /* (4364927) */ 22745 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 22746 } 22747 22748 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 22749 } 22750 22751 static int 22752 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 22753 { 22754 struct sd_lun *un = NULL; 22755 struct partition64 p64; 22756 int rval = 0; 22757 uint_t nparts; 22758 efi_gpe_t *partitions; 22759 efi_gpt_t *buffer; 22760 diskaddr_t gpe_lba; 22761 22762 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22763 return (ENXIO); 22764 } 22765 22766 if (ddi_copyin((const void *)arg, &p64, 22767 sizeof (struct partition64), flag)) { 22768 return (EFAULT); 22769 } 22770 22771 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 22772 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 22773 1, SD_PATH_DIRECT); 22774 if (rval != 0) 22775 goto done_error; 22776 22777 sd_swap_efi_gpt(buffer); 22778 22779 if ((rval = sd_validate_efi(buffer)) != 0) 22780 goto done_error; 22781 22782 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 22783 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 22784 if (p64.p_partno > nparts) { 22785 /* couldn't find it */ 22786 rval = ESRCH; 22787 goto done_error; 22788 } 22789 /* 22790 * if we're dealing with a partition that's out of the normal 22791 * 16K block, adjust accordingly 22792 */ 22793 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 22794 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 22795 gpe_lba, SD_PATH_DIRECT); 22796 if (rval) { 22797 goto done_error; 22798 } 22799 partitions = (efi_gpe_t *)buffer; 22800 22801 sd_swap_efi_gpe(nparts, partitions); 22802 22803 partitions += p64.p_partno; 22804 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 22805 sizeof (struct uuid)); 22806 p64.p_start = partitions->efi_gpe_StartingLBA; 22807 p64.p_size = partitions->efi_gpe_EndingLBA - 22808 p64.p_start + 1; 22809 22810 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 22811 rval = EFAULT; 22812 22813 done_error: 22814 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 22815 return (rval); 22816 } 22817 22818 22819 /* 22820 * Function: sd_dkio_set_vtoc 22821 * 22822 * Description: This routine is the driver entry point for handling user 22823 * requests to set the current volume table of contents 22824 * (DKIOCSVTOC). 22825 * 22826 * Arguments: dev - the device number 22827 * arg - pointer to user provided vtoc structure used to set the 22828 * current vtoc. 22829 * flag - this argument is a pass through to ddi_copyxxx() 22830 * directly from the mode argument of ioctl(). 22831 * 22832 * Return Code: 0 22833 * EFAULT 22834 * ENXIO 22835 * EINVAL 22836 * ENOTSUP 22837 */ 22838 22839 static int 22840 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 22841 { 22842 struct sd_lun *un = NULL; 22843 struct vtoc user_vtoc; 22844 int rval = 0; 22845 22846 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22847 return (ENXIO); 22848 } 22849 22850 #if defined(__i386) || defined(__amd64) 22851 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 22852 return (EINVAL); 22853 } 22854 #endif 22855 22856 #ifdef _MULTI_DATAMODEL 22857 switch (ddi_model_convert_from(flag & FMODELS)) { 22858 case DDI_MODEL_ILP32: { 22859 struct vtoc32 user_vtoc32; 22860 22861 if (ddi_copyin((const void *)arg, &user_vtoc32, 22862 sizeof (struct vtoc32), flag)) { 22863 return (EFAULT); 22864 } 22865 vtoc32tovtoc(user_vtoc32, user_vtoc); 22866 break; 22867 } 22868 22869 case DDI_MODEL_NONE: 22870 if (ddi_copyin((const void *)arg, &user_vtoc, 22871 sizeof (struct vtoc), flag)) { 22872 return (EFAULT); 22873 } 22874 break; 22875 } 22876 #else /* ! _MULTI_DATAMODEL */ 22877 if (ddi_copyin((const void *)arg, &user_vtoc, 22878 sizeof (struct vtoc), flag)) { 22879 return (EFAULT); 22880 } 22881 #endif /* _MULTI_DATAMODEL */ 22882 22883 mutex_enter(SD_MUTEX(un)); 22884 if (un->un_blockcount > DK_MAX_BLOCKS) { 22885 mutex_exit(SD_MUTEX(un)); 22886 return (ENOTSUP); 22887 } 22888 if (un->un_g.dkg_ncyl == 0) { 22889 mutex_exit(SD_MUTEX(un)); 22890 return (EINVAL); 22891 } 22892 22893 mutex_exit(SD_MUTEX(un)); 22894 sd_clear_efi(un); 22895 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 22896 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 22897 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 22898 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22899 un->un_node_type, NULL); 22900 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 22901 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22902 un->un_node_type, NULL); 22903 mutex_enter(SD_MUTEX(un)); 22904 22905 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 22906 if ((rval = sd_write_label(dev)) == 0) { 22907 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 22908 != 0) { 22909 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 22910 "sd_dkio_set_vtoc: " 22911 "Failed validate geometry\n"); 22912 } 22913 } 22914 } 22915 22916 /* 22917 * If sd_build_label_vtoc, or sd_write_label failed above write the 22918 * devid anyway, what can it hurt? Also preserve the device id by 22919 * writing to the disk acyl for the case where a devid has been 22920 * fabricated. 22921 */ 22922 if (un->un_f_devid_supported && 22923 (un->un_f_opt_fab_devid == TRUE)) { 22924 if (un->un_devid == NULL) { 22925 sd_register_devid(un, SD_DEVINFO(un), 22926 SD_TARGET_IS_UNRESERVED); 22927 } else { 22928 /* 22929 * The device id for this disk has been 22930 * fabricated. Fabricated device id's are 22931 * managed by storing them in the last 2 22932 * available sectors on the drive. The device 22933 * id must be preserved by writing it back out 22934 * to this location. 22935 */ 22936 if (sd_write_deviceid(un) != 0) { 22937 ddi_devid_free(un->un_devid); 22938 un->un_devid = NULL; 22939 } 22940 } 22941 } 22942 mutex_exit(SD_MUTEX(un)); 22943 return (rval); 22944 } 22945 22946 22947 /* 22948 * Function: sd_build_label_vtoc 22949 * 22950 * Description: This routine updates the driver soft state current volume table 22951 * of contents based on a user specified vtoc. 22952 * 22953 * Arguments: un - driver soft state (unit) structure 22954 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 22955 * to update the driver soft state. 22956 * 22957 * Return Code: 0 22958 * EINVAL 22959 */ 22960 22961 static int 22962 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22963 { 22964 struct dk_map *lmap; 22965 struct partition *vpart; 22966 int nblks; 22967 #if defined(_SUNOS_VTOC_8) 22968 int ncyl; 22969 struct dk_map2 *lpart; 22970 #endif /* defined(_SUNOS_VTOC_8) */ 22971 int i; 22972 22973 ASSERT(mutex_owned(SD_MUTEX(un))); 22974 22975 /* Sanity-check the vtoc */ 22976 if (user_vtoc->v_sanity != VTOC_SANE || 22977 user_vtoc->v_sectorsz != un->un_sys_blocksize || 22978 user_vtoc->v_nparts != V_NUMPAR) { 22979 return (EINVAL); 22980 } 22981 22982 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22983 if (nblks == 0) { 22984 return (EINVAL); 22985 } 22986 22987 #if defined(_SUNOS_VTOC_8) 22988 vpart = user_vtoc->v_part; 22989 for (i = 0; i < V_NUMPAR; i++) { 22990 if ((vpart->p_start % nblks) != 0) { 22991 return (EINVAL); 22992 } 22993 ncyl = vpart->p_start / nblks; 22994 ncyl += vpart->p_size / nblks; 22995 if ((vpart->p_size % nblks) != 0) { 22996 ncyl++; 22997 } 22998 if (ncyl > (int)un->un_g.dkg_ncyl) { 22999 return (EINVAL); 23000 } 23001 vpart++; 23002 } 23003 #endif /* defined(_SUNOS_VTOC_8) */ 23004 23005 /* Put appropriate vtoc structure fields into the disk label */ 23006 #if defined(_SUNOS_VTOC_16) 23007 /* 23008 * The vtoc is always a 32bit data structure to maintain the 23009 * on-disk format. Convert "in place" instead of bcopying it. 23010 */ 23011 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23012 23013 /* 23014 * in the 16-slice vtoc, starting sectors are expressed in 23015 * numbers *relative* to the start of the Solaris fdisk partition. 23016 */ 23017 lmap = un->un_map; 23018 vpart = user_vtoc->v_part; 23019 23020 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23021 lmap->dkl_cylno = vpart->p_start / nblks; 23022 lmap->dkl_nblk = vpart->p_size; 23023 } 23024 23025 #elif defined(_SUNOS_VTOC_8) 23026 23027 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23028 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23029 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23030 23031 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23032 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23033 23034 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23035 23036 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23037 23038 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23039 sizeof (un->un_vtoc.v_reserved)); 23040 23041 /* 23042 * Note the conversion from starting sector number 23043 * to starting cylinder number. 23044 * Return error if division results in a remainder. 23045 */ 23046 lmap = un->un_map; 23047 lpart = un->un_vtoc.v_part; 23048 vpart = user_vtoc->v_part; 23049 23050 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23051 lpart->p_tag = vpart->p_tag; 23052 lpart->p_flag = vpart->p_flag; 23053 lmap->dkl_cylno = vpart->p_start / nblks; 23054 lmap->dkl_nblk = vpart->p_size; 23055 23056 lmap++; 23057 lpart++; 23058 vpart++; 23059 23060 /* (4387723) */ 23061 #ifdef _LP64 23062 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23063 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23064 } else { 23065 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23066 } 23067 #else 23068 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23069 #endif 23070 } 23071 23072 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23073 #else 23074 #error "No VTOC format defined." 23075 #endif 23076 return (0); 23077 } 23078 23079 /* 23080 * Function: sd_clear_efi 23081 * 23082 * Description: This routine clears all EFI labels. 23083 * 23084 * Arguments: un - driver soft state (unit) structure 23085 * 23086 * Return Code: void 23087 */ 23088 23089 static void 23090 sd_clear_efi(struct sd_lun *un) 23091 { 23092 efi_gpt_t *gpt; 23093 uint_t lbasize; 23094 uint64_t cap; 23095 int rval; 23096 23097 ASSERT(!mutex_owned(SD_MUTEX(un))); 23098 23099 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23100 23101 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23102 goto done; 23103 } 23104 23105 sd_swap_efi_gpt(gpt); 23106 rval = sd_validate_efi(gpt); 23107 if (rval == 0) { 23108 /* clear primary */ 23109 bzero(gpt, sizeof (efi_gpt_t)); 23110 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23111 SD_PATH_DIRECT))) { 23112 SD_INFO(SD_LOG_IO_PARTITION, un, 23113 "sd_clear_efi: clear primary label failed\n"); 23114 } 23115 } 23116 /* the backup */ 23117 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23118 SD_PATH_DIRECT); 23119 if (rval) { 23120 goto done; 23121 } 23122 /* 23123 * The MMC standard allows READ CAPACITY to be 23124 * inaccurate by a bounded amount (in the interest of 23125 * response latency). As a result, failed READs are 23126 * commonplace (due to the reading of metadata and not 23127 * data). Depending on the per-Vendor/drive Sense data, 23128 * the failed READ can cause many (unnecessary) retries. 23129 */ 23130 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23131 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23132 SD_PATH_DIRECT)) != 0) { 23133 goto done; 23134 } 23135 sd_swap_efi_gpt(gpt); 23136 rval = sd_validate_efi(gpt); 23137 if (rval == 0) { 23138 /* clear backup */ 23139 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23140 cap-1); 23141 bzero(gpt, sizeof (efi_gpt_t)); 23142 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23143 cap-1, SD_PATH_DIRECT))) { 23144 SD_INFO(SD_LOG_IO_PARTITION, un, 23145 "sd_clear_efi: clear backup label failed\n"); 23146 } 23147 } 23148 23149 done: 23150 kmem_free(gpt, sizeof (efi_gpt_t)); 23151 } 23152 23153 /* 23154 * Function: sd_set_vtoc 23155 * 23156 * Description: This routine writes data to the appropriate positions 23157 * 23158 * Arguments: un - driver soft state (unit) structure 23159 * dkl - the data to be written 23160 * 23161 * Return: void 23162 */ 23163 23164 static int 23165 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23166 { 23167 void *shadow_buf; 23168 uint_t label_addr; 23169 int sec; 23170 int blk; 23171 int head; 23172 int cyl; 23173 int rval; 23174 23175 #if defined(__i386) || defined(__amd64) 23176 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23177 #else 23178 /* Write the primary label at block 0 of the solaris partition. */ 23179 label_addr = 0; 23180 #endif 23181 23182 if (NOT_DEVBSIZE(un)) { 23183 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23184 /* 23185 * Read the target's first block. 23186 */ 23187 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23188 un->un_tgt_blocksize, label_addr, 23189 SD_PATH_STANDARD)) != 0) { 23190 goto exit; 23191 } 23192 /* 23193 * Copy the contents of the label into the shadow buffer 23194 * which is of the size of target block size. 23195 */ 23196 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23197 } 23198 23199 /* Write the primary label */ 23200 if (NOT_DEVBSIZE(un)) { 23201 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23202 label_addr, SD_PATH_STANDARD); 23203 } else { 23204 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23205 label_addr, SD_PATH_STANDARD); 23206 } 23207 if (rval != 0) { 23208 return (rval); 23209 } 23210 23211 /* 23212 * Calculate where the backup labels go. They are always on 23213 * the last alternate cylinder, but some older drives put them 23214 * on head 2 instead of the last head. They are always on the 23215 * first 5 odd sectors of the appropriate track. 23216 * 23217 * We have no choice at this point, but to believe that the 23218 * disk label is valid. Use the geometry of the disk 23219 * as described in the label. 23220 */ 23221 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23222 head = dkl->dkl_nhead - 1; 23223 23224 /* 23225 * Write and verify the backup labels. Make sure we don't try to 23226 * write past the last cylinder. 23227 */ 23228 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23229 blk = (daddr_t)( 23230 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23231 (head * dkl->dkl_nsect) + sec); 23232 #if defined(__i386) || defined(__amd64) 23233 blk += un->un_solaris_offset; 23234 #endif 23235 if (NOT_DEVBSIZE(un)) { 23236 uint64_t tblk; 23237 /* 23238 * Need to read the block first for read modify write. 23239 */ 23240 tblk = (uint64_t)blk; 23241 blk = (int)((tblk * un->un_sys_blocksize) / 23242 un->un_tgt_blocksize); 23243 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23244 un->un_tgt_blocksize, blk, 23245 SD_PATH_STANDARD)) != 0) { 23246 goto exit; 23247 } 23248 /* 23249 * Modify the shadow buffer with the label. 23250 */ 23251 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23252 rval = sd_send_scsi_WRITE(un, shadow_buf, 23253 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23254 } else { 23255 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23256 blk, SD_PATH_STANDARD); 23257 SD_INFO(SD_LOG_IO_PARTITION, un, 23258 "sd_set_vtoc: wrote backup label %d\n", blk); 23259 } 23260 if (rval != 0) { 23261 goto exit; 23262 } 23263 } 23264 exit: 23265 if (NOT_DEVBSIZE(un)) { 23266 kmem_free(shadow_buf, un->un_tgt_blocksize); 23267 } 23268 return (rval); 23269 } 23270 23271 /* 23272 * Function: sd_clear_vtoc 23273 * 23274 * Description: This routine clears out the VTOC labels. 23275 * 23276 * Arguments: un - driver soft state (unit) structure 23277 * 23278 * Return: void 23279 */ 23280 23281 static void 23282 sd_clear_vtoc(struct sd_lun *un) 23283 { 23284 struct dk_label *dkl; 23285 23286 mutex_exit(SD_MUTEX(un)); 23287 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23288 mutex_enter(SD_MUTEX(un)); 23289 /* 23290 * sd_set_vtoc uses these fields in order to figure out 23291 * where to overwrite the backup labels 23292 */ 23293 dkl->dkl_apc = un->un_g.dkg_apc; 23294 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23295 dkl->dkl_acyl = un->un_g.dkg_acyl; 23296 dkl->dkl_nhead = un->un_g.dkg_nhead; 23297 dkl->dkl_nsect = un->un_g.dkg_nsect; 23298 mutex_exit(SD_MUTEX(un)); 23299 (void) sd_set_vtoc(un, dkl); 23300 kmem_free(dkl, sizeof (struct dk_label)); 23301 23302 mutex_enter(SD_MUTEX(un)); 23303 } 23304 23305 /* 23306 * Function: sd_write_label 23307 * 23308 * Description: This routine will validate and write the driver soft state vtoc 23309 * contents to the device. 23310 * 23311 * Arguments: dev - the device number 23312 * 23313 * Return Code: the code returned by sd_send_scsi_cmd() 23314 * 0 23315 * EINVAL 23316 * ENXIO 23317 * ENOMEM 23318 */ 23319 23320 static int 23321 sd_write_label(dev_t dev) 23322 { 23323 struct sd_lun *un; 23324 struct dk_label *dkl; 23325 short sum; 23326 short *sp; 23327 int i; 23328 int rval; 23329 23330 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23331 (un->un_state == SD_STATE_OFFLINE)) { 23332 return (ENXIO); 23333 } 23334 ASSERT(mutex_owned(SD_MUTEX(un))); 23335 mutex_exit(SD_MUTEX(un)); 23336 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23337 mutex_enter(SD_MUTEX(un)); 23338 23339 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23340 dkl->dkl_rpm = un->un_g.dkg_rpm; 23341 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23342 dkl->dkl_apc = un->un_g.dkg_apc; 23343 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23344 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23345 dkl->dkl_acyl = un->un_g.dkg_acyl; 23346 dkl->dkl_nhead = un->un_g.dkg_nhead; 23347 dkl->dkl_nsect = un->un_g.dkg_nsect; 23348 23349 #if defined(_SUNOS_VTOC_8) 23350 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23351 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23352 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23353 for (i = 0; i < NDKMAP; i++) { 23354 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23355 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23356 } 23357 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23358 #elif defined(_SUNOS_VTOC_16) 23359 dkl->dkl_skew = un->un_dkg_skew; 23360 #else 23361 #error "No VTOC format defined." 23362 #endif 23363 23364 dkl->dkl_magic = DKL_MAGIC; 23365 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23366 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23367 23368 /* Construct checksum for the new disk label */ 23369 sum = 0; 23370 sp = (short *)dkl; 23371 i = sizeof (struct dk_label) / sizeof (short); 23372 while (i--) { 23373 sum ^= *sp++; 23374 } 23375 dkl->dkl_cksum = sum; 23376 23377 mutex_exit(SD_MUTEX(un)); 23378 23379 rval = sd_set_vtoc(un, dkl); 23380 exit: 23381 kmem_free(dkl, sizeof (struct dk_label)); 23382 mutex_enter(SD_MUTEX(un)); 23383 return (rval); 23384 } 23385 23386 static int 23387 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23388 { 23389 struct sd_lun *un = NULL; 23390 dk_efi_t user_efi; 23391 int rval = 0; 23392 void *buffer; 23393 23394 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23395 return (ENXIO); 23396 23397 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23398 return (EFAULT); 23399 23400 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23401 23402 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23403 (user_efi.dki_length > un->un_max_xfer_size)) 23404 return (EINVAL); 23405 23406 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23407 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23408 rval = EFAULT; 23409 } else { 23410 /* 23411 * let's clear the vtoc labels and clear the softstate 23412 * vtoc. 23413 */ 23414 mutex_enter(SD_MUTEX(un)); 23415 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23416 SD_TRACE(SD_LOG_IO_PARTITION, un, 23417 "sd_dkio_set_efi: CLEAR VTOC\n"); 23418 sd_clear_vtoc(un); 23419 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23420 mutex_exit(SD_MUTEX(un)); 23421 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23422 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23423 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23424 S_IFBLK, 23425 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23426 un->un_node_type, NULL); 23427 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23428 S_IFCHR, 23429 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23430 un->un_node_type, NULL); 23431 } else 23432 mutex_exit(SD_MUTEX(un)); 23433 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23434 user_efi.dki_lba, SD_PATH_DIRECT); 23435 if (rval == 0) { 23436 mutex_enter(SD_MUTEX(un)); 23437 un->un_f_geometry_is_valid = FALSE; 23438 mutex_exit(SD_MUTEX(un)); 23439 } 23440 } 23441 kmem_free(buffer, user_efi.dki_length); 23442 return (rval); 23443 } 23444 23445 /* 23446 * Function: sd_dkio_get_mboot 23447 * 23448 * Description: This routine is the driver entry point for handling user 23449 * requests to get the current device mboot (DKIOCGMBOOT) 23450 * 23451 * Arguments: dev - the device number 23452 * arg - pointer to user provided mboot structure specifying 23453 * the current mboot. 23454 * flag - this argument is a pass through to ddi_copyxxx() 23455 * directly from the mode argument of ioctl(). 23456 * 23457 * Return Code: 0 23458 * EINVAL 23459 * EFAULT 23460 * ENXIO 23461 */ 23462 23463 static int 23464 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23465 { 23466 struct sd_lun *un; 23467 struct mboot *mboot; 23468 int rval; 23469 size_t buffer_size; 23470 23471 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23472 (un->un_state == SD_STATE_OFFLINE)) { 23473 return (ENXIO); 23474 } 23475 23476 if (!un->un_f_mboot_supported || arg == NULL) { 23477 return (EINVAL); 23478 } 23479 23480 /* 23481 * Read the mboot block, located at absolute block 0 on the target. 23482 */ 23483 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23484 23485 SD_TRACE(SD_LOG_IO_PARTITION, un, 23486 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23487 23488 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23489 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23490 SD_PATH_STANDARD)) == 0) { 23491 if (ddi_copyout(mboot, (void *)arg, 23492 sizeof (struct mboot), flag) != 0) { 23493 rval = EFAULT; 23494 } 23495 } 23496 kmem_free(mboot, buffer_size); 23497 return (rval); 23498 } 23499 23500 23501 /* 23502 * Function: sd_dkio_set_mboot 23503 * 23504 * Description: This routine is the driver entry point for handling user 23505 * requests to validate and set the device master boot 23506 * (DKIOCSMBOOT). 23507 * 23508 * Arguments: dev - the device number 23509 * arg - pointer to user provided mboot structure used to set the 23510 * master boot. 23511 * flag - this argument is a pass through to ddi_copyxxx() 23512 * directly from the mode argument of ioctl(). 23513 * 23514 * Return Code: 0 23515 * EINVAL 23516 * EFAULT 23517 * ENXIO 23518 */ 23519 23520 static int 23521 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23522 { 23523 struct sd_lun *un = NULL; 23524 struct mboot *mboot = NULL; 23525 int rval; 23526 ushort_t magic; 23527 23528 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23529 return (ENXIO); 23530 } 23531 23532 ASSERT(!mutex_owned(SD_MUTEX(un))); 23533 23534 if (!un->un_f_mboot_supported) { 23535 return (EINVAL); 23536 } 23537 23538 if (arg == NULL) { 23539 return (EINVAL); 23540 } 23541 23542 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23543 23544 if (ddi_copyin((const void *)arg, mboot, 23545 sizeof (struct mboot), flag) != 0) { 23546 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23547 return (EFAULT); 23548 } 23549 23550 /* Is this really a master boot record? */ 23551 magic = LE_16(mboot->signature); 23552 if (magic != MBB_MAGIC) { 23553 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23554 return (EINVAL); 23555 } 23556 23557 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23558 SD_PATH_STANDARD); 23559 23560 mutex_enter(SD_MUTEX(un)); 23561 #if defined(__i386) || defined(__amd64) 23562 if (rval == 0) { 23563 /* 23564 * mboot has been written successfully. 23565 * update the fdisk and vtoc tables in memory 23566 */ 23567 rval = sd_update_fdisk_and_vtoc(un); 23568 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23569 mutex_exit(SD_MUTEX(un)); 23570 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23571 return (rval); 23572 } 23573 } 23574 23575 /* 23576 * If the mboot write fails, write the devid anyway, what can it hurt? 23577 * Also preserve the device id by writing to the disk acyl for the case 23578 * where a devid has been fabricated. 23579 */ 23580 if (un->un_f_devid_supported && un->un_f_opt_fab_devid) { 23581 if (un->un_devid == NULL) { 23582 sd_register_devid(un, SD_DEVINFO(un), 23583 SD_TARGET_IS_UNRESERVED); 23584 } else { 23585 /* 23586 * The device id for this disk has been 23587 * fabricated. Fabricated device id's are 23588 * managed by storing them in the last 2 23589 * available sectors on the drive. The device 23590 * id must be preserved by writing it back out 23591 * to this location. 23592 */ 23593 if (sd_write_deviceid(un) != 0) { 23594 ddi_devid_free(un->un_devid); 23595 un->un_devid = NULL; 23596 } 23597 } 23598 } 23599 23600 #ifdef __lock_lint 23601 sd_setup_default_geometry(un); 23602 #endif 23603 23604 #else 23605 if (rval == 0) { 23606 /* 23607 * mboot has been written successfully. 23608 * set up the default geometry and VTOC 23609 */ 23610 if (un->un_blockcount <= DK_MAX_BLOCKS) 23611 sd_setup_default_geometry(un); 23612 } 23613 #endif 23614 mutex_exit(SD_MUTEX(un)); 23615 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23616 return (rval); 23617 } 23618 23619 23620 /* 23621 * Function: sd_setup_default_geometry 23622 * 23623 * Description: This local utility routine sets the default geometry as part of 23624 * setting the device mboot. 23625 * 23626 * Arguments: un - driver soft state (unit) structure 23627 * 23628 * Note: This may be redundant with sd_build_default_label. 23629 */ 23630 23631 static void 23632 sd_setup_default_geometry(struct sd_lun *un) 23633 { 23634 /* zero out the soft state geometry and partition table. */ 23635 bzero(&un->un_g, sizeof (struct dk_geom)); 23636 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23637 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23638 un->un_asciilabel[0] = '\0'; 23639 23640 /* 23641 * For the rpm, we use the minimum for the disk. 23642 * For the head, cyl and number of sector per track, 23643 * if the capacity <= 1GB, head = 64, sect = 32. 23644 * else head = 255, sect 63 23645 * Note: the capacity should be equal to C*H*S values. 23646 * This will cause some truncation of size due to 23647 * round off errors. For CD-ROMs, this truncation can 23648 * have adverse side effects, so returning ncyl and 23649 * nhead as 1. The nsect will overflow for most of 23650 * CD-ROMs as nsect is of type ushort. 23651 */ 23652 if (ISCD(un)) { 23653 un->un_g.dkg_ncyl = 1; 23654 un->un_g.dkg_nhead = 1; 23655 un->un_g.dkg_nsect = un->un_blockcount; 23656 } else { 23657 if (un->un_blockcount <= 0x1000) { 23658 /* Needed for unlabeled SCSI floppies. */ 23659 un->un_g.dkg_nhead = 2; 23660 un->un_g.dkg_ncyl = 80; 23661 un->un_g.dkg_pcyl = 80; 23662 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 23663 } else if (un->un_blockcount <= 0x200000) { 23664 un->un_g.dkg_nhead = 64; 23665 un->un_g.dkg_nsect = 32; 23666 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 23667 } else { 23668 un->un_g.dkg_nhead = 255; 23669 un->un_g.dkg_nsect = 63; 23670 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 23671 } 23672 un->un_blockcount = un->un_g.dkg_ncyl * 23673 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 23674 } 23675 un->un_g.dkg_acyl = 0; 23676 un->un_g.dkg_bcyl = 0; 23677 un->un_g.dkg_intrlv = 1; 23678 un->un_g.dkg_rpm = 200; 23679 un->un_g.dkg_read_reinstruct = 0; 23680 un->un_g.dkg_write_reinstruct = 0; 23681 if (un->un_g.dkg_pcyl == 0) { 23682 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 23683 } 23684 23685 un->un_map['a'-'a'].dkl_cylno = 0; 23686 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 23687 un->un_map['c'-'a'].dkl_cylno = 0; 23688 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 23689 un->un_f_geometry_is_valid = FALSE; 23690 } 23691 23692 23693 #if defined(__i386) || defined(__amd64) 23694 /* 23695 * Function: sd_update_fdisk_and_vtoc 23696 * 23697 * Description: This local utility routine updates the device fdisk and vtoc 23698 * as part of setting the device mboot. 23699 * 23700 * Arguments: un - driver soft state (unit) structure 23701 * 23702 * Return Code: 0 for success or errno-type return code. 23703 * 23704 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 23705 * these did exist seperately in x86 sd.c!!! 23706 */ 23707 23708 static int 23709 sd_update_fdisk_and_vtoc(struct sd_lun *un) 23710 { 23711 static char labelstring[128]; 23712 static char buf[256]; 23713 char *label = 0; 23714 int count; 23715 int label_rc = 0; 23716 int gvalid = un->un_f_geometry_is_valid; 23717 int fdisk_rval; 23718 int lbasize; 23719 int capacity; 23720 23721 ASSERT(mutex_owned(SD_MUTEX(un))); 23722 23723 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 23724 return (EINVAL); 23725 } 23726 23727 if (un->un_f_blockcount_is_valid == FALSE) { 23728 return (EINVAL); 23729 } 23730 23731 #if defined(_SUNOS_VTOC_16) 23732 /* 23733 * Set up the "whole disk" fdisk partition; this should always 23734 * exist, regardless of whether the disk contains an fdisk table 23735 * or vtoc. 23736 */ 23737 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 23738 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 23739 #endif /* defined(_SUNOS_VTOC_16) */ 23740 23741 /* 23742 * copy the lbasize and capacity so that if they're 23743 * reset while we're not holding the SD_MUTEX(un), we will 23744 * continue to use valid values after the SD_MUTEX(un) is 23745 * reacquired. 23746 */ 23747 lbasize = un->un_tgt_blocksize; 23748 capacity = un->un_blockcount; 23749 23750 /* 23751 * refresh the logical and physical geometry caches. 23752 * (data from mode sense format/rigid disk geometry pages, 23753 * and scsi_ifgetcap("geometry"). 23754 */ 23755 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 23756 23757 /* 23758 * Only DIRECT ACCESS devices will have Sun labels. 23759 * CD's supposedly have a Sun label, too 23760 */ 23761 if (un->un_f_vtoc_label_supported) { 23762 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 23763 SD_PATH_DIRECT); 23764 if (fdisk_rval == SD_CMD_FAILURE) { 23765 ASSERT(mutex_owned(SD_MUTEX(un))); 23766 return (EIO); 23767 } 23768 23769 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 23770 ASSERT(mutex_owned(SD_MUTEX(un))); 23771 return (EACCES); 23772 } 23773 23774 if (un->un_solaris_size <= DK_LABEL_LOC) { 23775 /* 23776 * Found fdisk table but no Solaris partition entry, 23777 * so don't call sd_uselabel() and don't create 23778 * a default label. 23779 */ 23780 label_rc = 0; 23781 un->un_f_geometry_is_valid = TRUE; 23782 goto no_solaris_partition; 23783 } 23784 23785 #if defined(_SUNOS_VTOC_8) 23786 label = (char *)un->un_asciilabel; 23787 #elif defined(_SUNOS_VTOC_16) 23788 label = (char *)un->un_vtoc.v_asciilabel; 23789 #else 23790 #error "No VTOC format defined." 23791 #endif 23792 } else if (capacity < 0) { 23793 ASSERT(mutex_owned(SD_MUTEX(un))); 23794 return (EINVAL); 23795 } 23796 23797 /* 23798 * For Removable media We reach here if we have found a 23799 * SOLARIS PARTITION. 23800 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 23801 * PARTITION has changed from the previous one, hence we will setup a 23802 * default VTOC in this case. 23803 */ 23804 if (un->un_f_geometry_is_valid == FALSE) { 23805 sd_build_default_label(un); 23806 label_rc = 0; 23807 } 23808 23809 no_solaris_partition: 23810 if ((!un->un_f_has_removable_media || 23811 (un->un_f_has_removable_media && 23812 un->un_mediastate == DKIO_EJECTED)) && 23813 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 23814 /* 23815 * Print out a message indicating who and what we are. 23816 * We do this only when we happen to really validate the 23817 * geometry. We may call sd_validate_geometry() at other 23818 * times, ioctl()'s like Get VTOC in which case we 23819 * don't want to print the label. 23820 * If the geometry is valid, print the label string, 23821 * else print vendor and product info, if available 23822 */ 23823 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 23824 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 23825 } else { 23826 mutex_enter(&sd_label_mutex); 23827 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 23828 labelstring); 23829 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 23830 &labelstring[64]); 23831 (void) sprintf(buf, "?Vendor '%s', product '%s'", 23832 labelstring, &labelstring[64]); 23833 if (un->un_f_blockcount_is_valid == TRUE) { 23834 (void) sprintf(&buf[strlen(buf)], 23835 ", %" PRIu64 " %u byte blocks\n", 23836 un->un_blockcount, 23837 un->un_tgt_blocksize); 23838 } else { 23839 (void) sprintf(&buf[strlen(buf)], 23840 ", (unknown capacity)\n"); 23841 } 23842 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 23843 mutex_exit(&sd_label_mutex); 23844 } 23845 } 23846 23847 #if defined(_SUNOS_VTOC_16) 23848 /* 23849 * If we have valid geometry, set up the remaining fdisk partitions. 23850 * Note that dkl_cylno is not used for the fdisk map entries, so 23851 * we set it to an entirely bogus value. 23852 */ 23853 for (count = 0; count < FD_NUMPART; count++) { 23854 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 23855 un->un_map[FDISK_P1 + count].dkl_nblk = 23856 un->un_fmap[count].fmap_nblk; 23857 un->un_offset[FDISK_P1 + count] = 23858 un->un_fmap[count].fmap_start; 23859 } 23860 #endif 23861 23862 for (count = 0; count < NDKMAP; count++) { 23863 #if defined(_SUNOS_VTOC_8) 23864 struct dk_map *lp = &un->un_map[count]; 23865 un->un_offset[count] = 23866 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23867 #elif defined(_SUNOS_VTOC_16) 23868 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 23869 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 23870 #else 23871 #error "No VTOC format defined." 23872 #endif 23873 } 23874 23875 ASSERT(mutex_owned(SD_MUTEX(un))); 23876 return (label_rc); 23877 } 23878 #endif 23879 23880 23881 /* 23882 * Function: sd_check_media 23883 * 23884 * Description: This utility routine implements the functionality for the 23885 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23886 * driver state changes from that specified by the user 23887 * (inserted or ejected). For example, if the user specifies 23888 * DKIO_EJECTED and the current media state is inserted this 23889 * routine will immediately return DKIO_INSERTED. However, if the 23890 * current media state is not inserted the user thread will be 23891 * blocked until the drive state changes. If DKIO_NONE is specified 23892 * the user thread will block until a drive state change occurs. 23893 * 23894 * Arguments: dev - the device number 23895 * state - user pointer to a dkio_state, updated with the current 23896 * drive state at return. 23897 * 23898 * Return Code: ENXIO 23899 * EIO 23900 * EAGAIN 23901 * EINTR 23902 */ 23903 23904 static int 23905 sd_check_media(dev_t dev, enum dkio_state state) 23906 { 23907 struct sd_lun *un = NULL; 23908 enum dkio_state prev_state; 23909 opaque_t token = NULL; 23910 int rval = 0; 23911 23912 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23913 return (ENXIO); 23914 } 23915 23916 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23917 23918 mutex_enter(SD_MUTEX(un)); 23919 23920 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23921 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23922 23923 prev_state = un->un_mediastate; 23924 23925 /* is there anything to do? */ 23926 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23927 /* 23928 * submit the request to the scsi_watch service; 23929 * scsi_media_watch_cb() does the real work 23930 */ 23931 mutex_exit(SD_MUTEX(un)); 23932 23933 /* 23934 * This change handles the case where a scsi watch request is 23935 * added to a device that is powered down. To accomplish this 23936 * we power up the device before adding the scsi watch request, 23937 * since the scsi watch sends a TUR directly to the device 23938 * which the device cannot handle if it is powered down. 23939 */ 23940 if (sd_pm_entry(un) != DDI_SUCCESS) { 23941 mutex_enter(SD_MUTEX(un)); 23942 goto done; 23943 } 23944 23945 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 23946 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23947 (caddr_t)dev); 23948 23949 sd_pm_exit(un); 23950 23951 mutex_enter(SD_MUTEX(un)); 23952 if (token == NULL) { 23953 rval = EAGAIN; 23954 goto done; 23955 } 23956 23957 /* 23958 * This is a special case IOCTL that doesn't return 23959 * until the media state changes. Routine sdpower 23960 * knows about and handles this so don't count it 23961 * as an active cmd in the driver, which would 23962 * keep the device busy to the pm framework. 23963 * If the count isn't decremented the device can't 23964 * be powered down. 23965 */ 23966 un->un_ncmds_in_driver--; 23967 ASSERT(un->un_ncmds_in_driver >= 0); 23968 23969 /* 23970 * if a prior request had been made, this will be the same 23971 * token, as scsi_watch was designed that way. 23972 */ 23973 un->un_swr_token = token; 23974 un->un_specified_mediastate = state; 23975 23976 /* 23977 * now wait for media change 23978 * we will not be signalled unless mediastate == state but it is 23979 * still better to test for this condition, since there is a 23980 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23981 */ 23982 SD_TRACE(SD_LOG_COMMON, un, 23983 "sd_check_media: waiting for media state change\n"); 23984 while (un->un_mediastate == state) { 23985 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23986 SD_TRACE(SD_LOG_COMMON, un, 23987 "sd_check_media: waiting for media state " 23988 "was interrupted\n"); 23989 un->un_ncmds_in_driver++; 23990 rval = EINTR; 23991 goto done; 23992 } 23993 SD_TRACE(SD_LOG_COMMON, un, 23994 "sd_check_media: received signal, state=%x\n", 23995 un->un_mediastate); 23996 } 23997 /* 23998 * Inc the counter to indicate the device once again 23999 * has an active outstanding cmd. 24000 */ 24001 un->un_ncmds_in_driver++; 24002 } 24003 24004 /* invalidate geometry */ 24005 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24006 sr_ejected(un); 24007 } 24008 24009 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24010 uint64_t capacity; 24011 uint_t lbasize; 24012 24013 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24014 mutex_exit(SD_MUTEX(un)); 24015 /* 24016 * Since the following routines use SD_PATH_DIRECT, we must 24017 * call PM directly before the upcoming disk accesses. This 24018 * may cause the disk to be power/spin up. 24019 */ 24020 24021 if (sd_pm_entry(un) == DDI_SUCCESS) { 24022 rval = sd_send_scsi_READ_CAPACITY(un, 24023 &capacity, 24024 &lbasize, SD_PATH_DIRECT); 24025 if (rval != 0) { 24026 sd_pm_exit(un); 24027 mutex_enter(SD_MUTEX(un)); 24028 goto done; 24029 } 24030 } else { 24031 rval = EIO; 24032 mutex_enter(SD_MUTEX(un)); 24033 goto done; 24034 } 24035 mutex_enter(SD_MUTEX(un)); 24036 24037 sd_update_block_info(un, lbasize, capacity); 24038 24039 un->un_f_geometry_is_valid = FALSE; 24040 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24041 24042 mutex_exit(SD_MUTEX(un)); 24043 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24044 SD_PATH_DIRECT); 24045 sd_pm_exit(un); 24046 24047 mutex_enter(SD_MUTEX(un)); 24048 } 24049 done: 24050 un->un_f_watcht_stopped = FALSE; 24051 if (un->un_swr_token) { 24052 /* 24053 * Use of this local token and the mutex ensures that we avoid 24054 * some race conditions associated with terminating the 24055 * scsi watch. 24056 */ 24057 token = un->un_swr_token; 24058 un->un_swr_token = (opaque_t)NULL; 24059 mutex_exit(SD_MUTEX(un)); 24060 (void) scsi_watch_request_terminate(token, 24061 SCSI_WATCH_TERMINATE_WAIT); 24062 mutex_enter(SD_MUTEX(un)); 24063 } 24064 24065 /* 24066 * Update the capacity kstat value, if no media previously 24067 * (capacity kstat is 0) and a media has been inserted 24068 * (un_f_blockcount_is_valid == TRUE) 24069 */ 24070 if (un->un_errstats) { 24071 struct sd_errstats *stp = NULL; 24072 24073 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24074 if ((stp->sd_capacity.value.ui64 == 0) && 24075 (un->un_f_blockcount_is_valid == TRUE)) { 24076 stp->sd_capacity.value.ui64 = 24077 (uint64_t)((uint64_t)un->un_blockcount * 24078 un->un_sys_blocksize); 24079 } 24080 } 24081 mutex_exit(SD_MUTEX(un)); 24082 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24083 return (rval); 24084 } 24085 24086 24087 /* 24088 * Function: sd_delayed_cv_broadcast 24089 * 24090 * Description: Delayed cv_broadcast to allow for target to recover from media 24091 * insertion. 24092 * 24093 * Arguments: arg - driver soft state (unit) structure 24094 */ 24095 24096 static void 24097 sd_delayed_cv_broadcast(void *arg) 24098 { 24099 struct sd_lun *un = arg; 24100 24101 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24102 24103 mutex_enter(SD_MUTEX(un)); 24104 un->un_dcvb_timeid = NULL; 24105 cv_broadcast(&un->un_state_cv); 24106 mutex_exit(SD_MUTEX(un)); 24107 } 24108 24109 24110 /* 24111 * Function: sd_media_watch_cb 24112 * 24113 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24114 * routine processes the TUR sense data and updates the driver 24115 * state if a transition has occurred. The user thread 24116 * (sd_check_media) is then signalled. 24117 * 24118 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24119 * among multiple watches that share this callback function 24120 * resultp - scsi watch facility result packet containing scsi 24121 * packet, status byte and sense data 24122 * 24123 * Return Code: 0 for success, -1 for failure 24124 */ 24125 24126 static int 24127 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24128 { 24129 struct sd_lun *un; 24130 struct scsi_status *statusp = resultp->statusp; 24131 struct scsi_extended_sense *sensep = resultp->sensep; 24132 enum dkio_state state = DKIO_NONE; 24133 dev_t dev = (dev_t)arg; 24134 uchar_t actual_sense_length; 24135 24136 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24137 return (-1); 24138 } 24139 actual_sense_length = resultp->actual_sense_length; 24140 24141 mutex_enter(SD_MUTEX(un)); 24142 SD_TRACE(SD_LOG_COMMON, un, 24143 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24144 *((char *)statusp), (void *)sensep, actual_sense_length); 24145 24146 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24147 un->un_mediastate = DKIO_DEV_GONE; 24148 cv_broadcast(&un->un_state_cv); 24149 mutex_exit(SD_MUTEX(un)); 24150 24151 return (0); 24152 } 24153 24154 /* 24155 * If there was a check condition then sensep points to valid sense data 24156 * If status was not a check condition but a reservation or busy status 24157 * then the new state is DKIO_NONE 24158 */ 24159 if (sensep != NULL) { 24160 SD_INFO(SD_LOG_COMMON, un, 24161 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24162 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 24163 /* This routine only uses up to 13 bytes of sense data. */ 24164 if (actual_sense_length >= 13) { 24165 if (sensep->es_key == KEY_UNIT_ATTENTION) { 24166 if (sensep->es_add_code == 0x28) { 24167 state = DKIO_INSERTED; 24168 } 24169 } else { 24170 /* 24171 * if 02/04/02 means that the host 24172 * should send start command. Explicitly 24173 * leave the media state as is 24174 * (inserted) as the media is inserted 24175 * and host has stopped device for PM 24176 * reasons. Upon next true read/write 24177 * to this media will bring the 24178 * device to the right state good for 24179 * media access. 24180 */ 24181 if ((sensep->es_key == KEY_NOT_READY) && 24182 (sensep->es_add_code == 0x3a)) { 24183 state = DKIO_EJECTED; 24184 } 24185 24186 /* 24187 * If the drivge is busy with an operation 24188 * or long write, keep the media in an 24189 * inserted state. 24190 */ 24191 24192 if ((sensep->es_key == KEY_NOT_READY) && 24193 (sensep->es_add_code == 0x04) && 24194 ((sensep->es_qual_code == 0x02) || 24195 (sensep->es_qual_code == 0x07) || 24196 (sensep->es_qual_code == 0x08))) { 24197 state = DKIO_INSERTED; 24198 } 24199 } 24200 } 24201 } else if ((*((char *)statusp) == STATUS_GOOD) && 24202 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24203 state = DKIO_INSERTED; 24204 } 24205 24206 SD_TRACE(SD_LOG_COMMON, un, 24207 "sd_media_watch_cb: state=%x, specified=%x\n", 24208 state, un->un_specified_mediastate); 24209 24210 /* 24211 * now signal the waiting thread if this is *not* the specified state; 24212 * delay the signal if the state is DKIO_INSERTED to allow the target 24213 * to recover 24214 */ 24215 if (state != un->un_specified_mediastate) { 24216 un->un_mediastate = state; 24217 if (state == DKIO_INSERTED) { 24218 /* 24219 * delay the signal to give the drive a chance 24220 * to do what it apparently needs to do 24221 */ 24222 SD_TRACE(SD_LOG_COMMON, un, 24223 "sd_media_watch_cb: delayed cv_broadcast\n"); 24224 if (un->un_dcvb_timeid == NULL) { 24225 un->un_dcvb_timeid = 24226 timeout(sd_delayed_cv_broadcast, un, 24227 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24228 } 24229 } else { 24230 SD_TRACE(SD_LOG_COMMON, un, 24231 "sd_media_watch_cb: immediate cv_broadcast\n"); 24232 cv_broadcast(&un->un_state_cv); 24233 } 24234 } 24235 mutex_exit(SD_MUTEX(un)); 24236 return (0); 24237 } 24238 24239 24240 /* 24241 * Function: sd_dkio_get_temp 24242 * 24243 * Description: This routine is the driver entry point for handling ioctl 24244 * requests to get the disk temperature. 24245 * 24246 * Arguments: dev - the device number 24247 * arg - pointer to user provided dk_temperature structure. 24248 * flag - this argument is a pass through to ddi_copyxxx() 24249 * directly from the mode argument of ioctl(). 24250 * 24251 * Return Code: 0 24252 * EFAULT 24253 * ENXIO 24254 * EAGAIN 24255 */ 24256 24257 static int 24258 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24259 { 24260 struct sd_lun *un = NULL; 24261 struct dk_temperature *dktemp = NULL; 24262 uchar_t *temperature_page; 24263 int rval = 0; 24264 int path_flag = SD_PATH_STANDARD; 24265 24266 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24267 return (ENXIO); 24268 } 24269 24270 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24271 24272 /* copyin the disk temp argument to get the user flags */ 24273 if (ddi_copyin((void *)arg, dktemp, 24274 sizeof (struct dk_temperature), flag) != 0) { 24275 rval = EFAULT; 24276 goto done; 24277 } 24278 24279 /* Initialize the temperature to invalid. */ 24280 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24281 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24282 24283 /* 24284 * Note: Investigate removing the "bypass pm" semantic. 24285 * Can we just bypass PM always? 24286 */ 24287 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24288 path_flag = SD_PATH_DIRECT; 24289 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24290 mutex_enter(&un->un_pm_mutex); 24291 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24292 /* 24293 * If DKT_BYPASS_PM is set, and the drive happens to be 24294 * in low power mode, we can not wake it up, Need to 24295 * return EAGAIN. 24296 */ 24297 mutex_exit(&un->un_pm_mutex); 24298 rval = EAGAIN; 24299 goto done; 24300 } else { 24301 /* 24302 * Indicate to PM the device is busy. This is required 24303 * to avoid a race - i.e. the ioctl is issuing a 24304 * command and the pm framework brings down the device 24305 * to low power mode (possible power cut-off on some 24306 * platforms). 24307 */ 24308 mutex_exit(&un->un_pm_mutex); 24309 if (sd_pm_entry(un) != DDI_SUCCESS) { 24310 rval = EAGAIN; 24311 goto done; 24312 } 24313 } 24314 } 24315 24316 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24317 24318 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24319 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24320 goto done2; 24321 } 24322 24323 /* 24324 * For the current temperature verify that the parameter length is 0x02 24325 * and the parameter code is 0x00 24326 */ 24327 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24328 (temperature_page[5] == 0x00)) { 24329 if (temperature_page[9] == 0xFF) { 24330 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24331 } else { 24332 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24333 } 24334 } 24335 24336 /* 24337 * For the reference temperature verify that the parameter 24338 * length is 0x02 and the parameter code is 0x01 24339 */ 24340 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24341 (temperature_page[11] == 0x01)) { 24342 if (temperature_page[15] == 0xFF) { 24343 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24344 } else { 24345 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24346 } 24347 } 24348 24349 /* Do the copyout regardless of the temperature commands status. */ 24350 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24351 flag) != 0) { 24352 rval = EFAULT; 24353 } 24354 24355 done2: 24356 if (path_flag == SD_PATH_DIRECT) { 24357 sd_pm_exit(un); 24358 } 24359 24360 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24361 done: 24362 if (dktemp != NULL) { 24363 kmem_free(dktemp, sizeof (struct dk_temperature)); 24364 } 24365 24366 return (rval); 24367 } 24368 24369 24370 /* 24371 * Function: sd_log_page_supported 24372 * 24373 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24374 * supported log pages. 24375 * 24376 * Arguments: un - 24377 * log_page - 24378 * 24379 * Return Code: -1 - on error (log sense is optional and may not be supported). 24380 * 0 - log page not found. 24381 * 1 - log page found. 24382 */ 24383 24384 static int 24385 sd_log_page_supported(struct sd_lun *un, int log_page) 24386 { 24387 uchar_t *log_page_data; 24388 int i; 24389 int match = 0; 24390 int log_size; 24391 24392 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24393 24394 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24395 SD_PATH_DIRECT) != 0) { 24396 SD_ERROR(SD_LOG_COMMON, un, 24397 "sd_log_page_supported: failed log page retrieval\n"); 24398 kmem_free(log_page_data, 0xFF); 24399 return (-1); 24400 } 24401 log_size = log_page_data[3]; 24402 24403 /* 24404 * The list of supported log pages start from the fourth byte. Check 24405 * until we run out of log pages or a match is found. 24406 */ 24407 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24408 if (log_page_data[i] == log_page) { 24409 match++; 24410 } 24411 } 24412 kmem_free(log_page_data, 0xFF); 24413 return (match); 24414 } 24415 24416 24417 /* 24418 * Function: sd_mhdioc_failfast 24419 * 24420 * Description: This routine is the driver entry point for handling ioctl 24421 * requests to enable/disable the multihost failfast option. 24422 * (MHIOCENFAILFAST) 24423 * 24424 * Arguments: dev - the device number 24425 * arg - user specified probing interval. 24426 * flag - this argument is a pass through to ddi_copyxxx() 24427 * directly from the mode argument of ioctl(). 24428 * 24429 * Return Code: 0 24430 * EFAULT 24431 * ENXIO 24432 */ 24433 24434 static int 24435 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24436 { 24437 struct sd_lun *un = NULL; 24438 int mh_time; 24439 int rval = 0; 24440 24441 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24442 return (ENXIO); 24443 } 24444 24445 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24446 return (EFAULT); 24447 24448 if (mh_time) { 24449 mutex_enter(SD_MUTEX(un)); 24450 un->un_resvd_status |= SD_FAILFAST; 24451 mutex_exit(SD_MUTEX(un)); 24452 /* 24453 * If mh_time is INT_MAX, then this ioctl is being used for 24454 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24455 */ 24456 if (mh_time != INT_MAX) { 24457 rval = sd_check_mhd(dev, mh_time); 24458 } 24459 } else { 24460 (void) sd_check_mhd(dev, 0); 24461 mutex_enter(SD_MUTEX(un)); 24462 un->un_resvd_status &= ~SD_FAILFAST; 24463 mutex_exit(SD_MUTEX(un)); 24464 } 24465 return (rval); 24466 } 24467 24468 24469 /* 24470 * Function: sd_mhdioc_takeown 24471 * 24472 * Description: This routine is the driver entry point for handling ioctl 24473 * requests to forcefully acquire exclusive access rights to the 24474 * multihost disk (MHIOCTKOWN). 24475 * 24476 * Arguments: dev - the device number 24477 * arg - user provided structure specifying the delay 24478 * parameters in milliseconds 24479 * flag - this argument is a pass through to ddi_copyxxx() 24480 * directly from the mode argument of ioctl(). 24481 * 24482 * Return Code: 0 24483 * EFAULT 24484 * ENXIO 24485 */ 24486 24487 static int 24488 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24489 { 24490 struct sd_lun *un = NULL; 24491 struct mhioctkown *tkown = NULL; 24492 int rval = 0; 24493 24494 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24495 return (ENXIO); 24496 } 24497 24498 if (arg != NULL) { 24499 tkown = (struct mhioctkown *) 24500 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24501 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24502 if (rval != 0) { 24503 rval = EFAULT; 24504 goto error; 24505 } 24506 } 24507 24508 rval = sd_take_ownership(dev, tkown); 24509 mutex_enter(SD_MUTEX(un)); 24510 if (rval == 0) { 24511 un->un_resvd_status |= SD_RESERVE; 24512 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24513 sd_reinstate_resv_delay = 24514 tkown->reinstate_resv_delay * 1000; 24515 } else { 24516 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24517 } 24518 /* 24519 * Give the scsi_watch routine interval set by 24520 * the MHIOCENFAILFAST ioctl precedence here. 24521 */ 24522 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24523 mutex_exit(SD_MUTEX(un)); 24524 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24525 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24526 "sd_mhdioc_takeown : %d\n", 24527 sd_reinstate_resv_delay); 24528 } else { 24529 mutex_exit(SD_MUTEX(un)); 24530 } 24531 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24532 sd_mhd_reset_notify_cb, (caddr_t)un); 24533 } else { 24534 un->un_resvd_status &= ~SD_RESERVE; 24535 mutex_exit(SD_MUTEX(un)); 24536 } 24537 24538 error: 24539 if (tkown != NULL) { 24540 kmem_free(tkown, sizeof (struct mhioctkown)); 24541 } 24542 return (rval); 24543 } 24544 24545 24546 /* 24547 * Function: sd_mhdioc_release 24548 * 24549 * Description: This routine is the driver entry point for handling ioctl 24550 * requests to release exclusive access rights to the multihost 24551 * disk (MHIOCRELEASE). 24552 * 24553 * Arguments: dev - the device number 24554 * 24555 * Return Code: 0 24556 * ENXIO 24557 */ 24558 24559 static int 24560 sd_mhdioc_release(dev_t dev) 24561 { 24562 struct sd_lun *un = NULL; 24563 timeout_id_t resvd_timeid_save; 24564 int resvd_status_save; 24565 int rval = 0; 24566 24567 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24568 return (ENXIO); 24569 } 24570 24571 mutex_enter(SD_MUTEX(un)); 24572 resvd_status_save = un->un_resvd_status; 24573 un->un_resvd_status &= 24574 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24575 if (un->un_resvd_timeid) { 24576 resvd_timeid_save = un->un_resvd_timeid; 24577 un->un_resvd_timeid = NULL; 24578 mutex_exit(SD_MUTEX(un)); 24579 (void) untimeout(resvd_timeid_save); 24580 } else { 24581 mutex_exit(SD_MUTEX(un)); 24582 } 24583 24584 /* 24585 * destroy any pending timeout thread that may be attempting to 24586 * reinstate reservation on this device. 24587 */ 24588 sd_rmv_resv_reclaim_req(dev); 24589 24590 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24591 mutex_enter(SD_MUTEX(un)); 24592 if ((un->un_mhd_token) && 24593 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24594 mutex_exit(SD_MUTEX(un)); 24595 (void) sd_check_mhd(dev, 0); 24596 } else { 24597 mutex_exit(SD_MUTEX(un)); 24598 } 24599 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24600 sd_mhd_reset_notify_cb, (caddr_t)un); 24601 } else { 24602 /* 24603 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24604 */ 24605 mutex_enter(SD_MUTEX(un)); 24606 un->un_resvd_status = resvd_status_save; 24607 mutex_exit(SD_MUTEX(un)); 24608 } 24609 return (rval); 24610 } 24611 24612 24613 /* 24614 * Function: sd_mhdioc_register_devid 24615 * 24616 * Description: This routine is the driver entry point for handling ioctl 24617 * requests to register the device id (MHIOCREREGISTERDEVID). 24618 * 24619 * Note: The implementation for this ioctl has been updated to 24620 * be consistent with the original PSARC case (1999/357) 24621 * (4375899, 4241671, 4220005) 24622 * 24623 * Arguments: dev - the device number 24624 * 24625 * Return Code: 0 24626 * ENXIO 24627 */ 24628 24629 static int 24630 sd_mhdioc_register_devid(dev_t dev) 24631 { 24632 struct sd_lun *un = NULL; 24633 int rval = 0; 24634 24635 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24636 return (ENXIO); 24637 } 24638 24639 ASSERT(!mutex_owned(SD_MUTEX(un))); 24640 24641 mutex_enter(SD_MUTEX(un)); 24642 24643 /* If a devid already exists, de-register it */ 24644 if (un->un_devid != NULL) { 24645 ddi_devid_unregister(SD_DEVINFO(un)); 24646 /* 24647 * After unregister devid, needs to free devid memory 24648 */ 24649 ddi_devid_free(un->un_devid); 24650 un->un_devid = NULL; 24651 } 24652 24653 /* Check for reservation conflict */ 24654 mutex_exit(SD_MUTEX(un)); 24655 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24656 mutex_enter(SD_MUTEX(un)); 24657 24658 switch (rval) { 24659 case 0: 24660 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24661 break; 24662 case EACCES: 24663 break; 24664 default: 24665 rval = EIO; 24666 } 24667 24668 mutex_exit(SD_MUTEX(un)); 24669 return (rval); 24670 } 24671 24672 24673 /* 24674 * Function: sd_mhdioc_inkeys 24675 * 24676 * Description: This routine is the driver entry point for handling ioctl 24677 * requests to issue the SCSI-3 Persistent In Read Keys command 24678 * to the device (MHIOCGRP_INKEYS). 24679 * 24680 * Arguments: dev - the device number 24681 * arg - user provided in_keys structure 24682 * flag - this argument is a pass through to ddi_copyxxx() 24683 * directly from the mode argument of ioctl(). 24684 * 24685 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24686 * ENXIO 24687 * EFAULT 24688 */ 24689 24690 static int 24691 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24692 { 24693 struct sd_lun *un; 24694 mhioc_inkeys_t inkeys; 24695 int rval = 0; 24696 24697 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24698 return (ENXIO); 24699 } 24700 24701 #ifdef _MULTI_DATAMODEL 24702 switch (ddi_model_convert_from(flag & FMODELS)) { 24703 case DDI_MODEL_ILP32: { 24704 struct mhioc_inkeys32 inkeys32; 24705 24706 if (ddi_copyin(arg, &inkeys32, 24707 sizeof (struct mhioc_inkeys32), flag) != 0) { 24708 return (EFAULT); 24709 } 24710 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24711 if ((rval = sd_persistent_reservation_in_read_keys(un, 24712 &inkeys, flag)) != 0) { 24713 return (rval); 24714 } 24715 inkeys32.generation = inkeys.generation; 24716 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24717 flag) != 0) { 24718 return (EFAULT); 24719 } 24720 break; 24721 } 24722 case DDI_MODEL_NONE: 24723 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24724 flag) != 0) { 24725 return (EFAULT); 24726 } 24727 if ((rval = sd_persistent_reservation_in_read_keys(un, 24728 &inkeys, flag)) != 0) { 24729 return (rval); 24730 } 24731 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24732 flag) != 0) { 24733 return (EFAULT); 24734 } 24735 break; 24736 } 24737 24738 #else /* ! _MULTI_DATAMODEL */ 24739 24740 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24741 return (EFAULT); 24742 } 24743 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24744 if (rval != 0) { 24745 return (rval); 24746 } 24747 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24748 return (EFAULT); 24749 } 24750 24751 #endif /* _MULTI_DATAMODEL */ 24752 24753 return (rval); 24754 } 24755 24756 24757 /* 24758 * Function: sd_mhdioc_inresv 24759 * 24760 * Description: This routine is the driver entry point for handling ioctl 24761 * requests to issue the SCSI-3 Persistent In Read Reservations 24762 * command to the device (MHIOCGRP_INKEYS). 24763 * 24764 * Arguments: dev - the device number 24765 * arg - user provided in_resv structure 24766 * flag - this argument is a pass through to ddi_copyxxx() 24767 * directly from the mode argument of ioctl(). 24768 * 24769 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24770 * ENXIO 24771 * EFAULT 24772 */ 24773 24774 static int 24775 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24776 { 24777 struct sd_lun *un; 24778 mhioc_inresvs_t inresvs; 24779 int rval = 0; 24780 24781 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24782 return (ENXIO); 24783 } 24784 24785 #ifdef _MULTI_DATAMODEL 24786 24787 switch (ddi_model_convert_from(flag & FMODELS)) { 24788 case DDI_MODEL_ILP32: { 24789 struct mhioc_inresvs32 inresvs32; 24790 24791 if (ddi_copyin(arg, &inresvs32, 24792 sizeof (struct mhioc_inresvs32), flag) != 0) { 24793 return (EFAULT); 24794 } 24795 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24796 if ((rval = sd_persistent_reservation_in_read_resv(un, 24797 &inresvs, flag)) != 0) { 24798 return (rval); 24799 } 24800 inresvs32.generation = inresvs.generation; 24801 if (ddi_copyout(&inresvs32, arg, 24802 sizeof (struct mhioc_inresvs32), flag) != 0) { 24803 return (EFAULT); 24804 } 24805 break; 24806 } 24807 case DDI_MODEL_NONE: 24808 if (ddi_copyin(arg, &inresvs, 24809 sizeof (mhioc_inresvs_t), flag) != 0) { 24810 return (EFAULT); 24811 } 24812 if ((rval = sd_persistent_reservation_in_read_resv(un, 24813 &inresvs, flag)) != 0) { 24814 return (rval); 24815 } 24816 if (ddi_copyout(&inresvs, arg, 24817 sizeof (mhioc_inresvs_t), flag) != 0) { 24818 return (EFAULT); 24819 } 24820 break; 24821 } 24822 24823 #else /* ! _MULTI_DATAMODEL */ 24824 24825 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24826 return (EFAULT); 24827 } 24828 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24829 if (rval != 0) { 24830 return (rval); 24831 } 24832 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24833 return (EFAULT); 24834 } 24835 24836 #endif /* ! _MULTI_DATAMODEL */ 24837 24838 return (rval); 24839 } 24840 24841 24842 /* 24843 * The following routines support the clustering functionality described below 24844 * and implement lost reservation reclaim functionality. 24845 * 24846 * Clustering 24847 * ---------- 24848 * The clustering code uses two different, independent forms of SCSI 24849 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24850 * Persistent Group Reservations. For any particular disk, it will use either 24851 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24852 * 24853 * SCSI-2 24854 * The cluster software takes ownership of a multi-hosted disk by issuing the 24855 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24856 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 24857 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 24858 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 24859 * meaning of failfast is that if the driver (on this host) ever encounters the 24860 * scsi error return code RESERVATION_CONFLICT from the device, it should 24861 * immediately panic the host. The motivation for this ioctl is that if this 24862 * host does encounter reservation conflict, the underlying cause is that some 24863 * other host of the cluster has decided that this host is no longer in the 24864 * cluster and has seized control of the disks for itself. Since this host is no 24865 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 24866 * does two things: 24867 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24868 * error to panic the host 24869 * (b) it sets up a periodic timer to test whether this host still has 24870 * "access" (in that no other host has reserved the device): if the 24871 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24872 * purpose of that periodic timer is to handle scenarios where the host is 24873 * otherwise temporarily quiescent, temporarily doing no real i/o. 24874 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24875 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24876 * the device itself. 24877 * 24878 * SCSI-3 PGR 24879 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24880 * facility is supported through the shared multihost disk ioctls 24881 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24882 * MHIOCGRP_PREEMPTANDABORT) 24883 * 24884 * Reservation Reclaim: 24885 * -------------------- 24886 * To support the lost reservation reclaim operations this driver creates a 24887 * single thread to handle reinstating reservations on all devices that have 24888 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24889 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24890 * and the reservation reclaim thread loops through the requests to regain the 24891 * lost reservations. 24892 */ 24893 24894 /* 24895 * Function: sd_check_mhd() 24896 * 24897 * Description: This function sets up and submits a scsi watch request or 24898 * terminates an existing watch request. This routine is used in 24899 * support of reservation reclaim. 24900 * 24901 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24902 * among multiple watches that share the callback function 24903 * interval - the number of microseconds specifying the watch 24904 * interval for issuing TEST UNIT READY commands. If 24905 * set to 0 the watch should be terminated. If the 24906 * interval is set to 0 and if the device is required 24907 * to hold reservation while disabling failfast, the 24908 * watch is restarted with an interval of 24909 * reinstate_resv_delay. 24910 * 24911 * Return Code: 0 - Successful submit/terminate of scsi watch request 24912 * ENXIO - Indicates an invalid device was specified 24913 * EAGAIN - Unable to submit the scsi watch request 24914 */ 24915 24916 static int 24917 sd_check_mhd(dev_t dev, int interval) 24918 { 24919 struct sd_lun *un; 24920 opaque_t token; 24921 24922 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24923 return (ENXIO); 24924 } 24925 24926 /* is this a watch termination request? */ 24927 if (interval == 0) { 24928 mutex_enter(SD_MUTEX(un)); 24929 /* if there is an existing watch task then terminate it */ 24930 if (un->un_mhd_token) { 24931 token = un->un_mhd_token; 24932 un->un_mhd_token = NULL; 24933 mutex_exit(SD_MUTEX(un)); 24934 (void) scsi_watch_request_terminate(token, 24935 SCSI_WATCH_TERMINATE_WAIT); 24936 mutex_enter(SD_MUTEX(un)); 24937 } else { 24938 mutex_exit(SD_MUTEX(un)); 24939 /* 24940 * Note: If we return here we don't check for the 24941 * failfast case. This is the original legacy 24942 * implementation but perhaps we should be checking 24943 * the failfast case. 24944 */ 24945 return (0); 24946 } 24947 /* 24948 * If the device is required to hold reservation while 24949 * disabling failfast, we need to restart the scsi_watch 24950 * routine with an interval of reinstate_resv_delay. 24951 */ 24952 if (un->un_resvd_status & SD_RESERVE) { 24953 interval = sd_reinstate_resv_delay/1000; 24954 } else { 24955 /* no failfast so bail */ 24956 mutex_exit(SD_MUTEX(un)); 24957 return (0); 24958 } 24959 mutex_exit(SD_MUTEX(un)); 24960 } 24961 24962 /* 24963 * adjust minimum time interval to 1 second, 24964 * and convert from msecs to usecs 24965 */ 24966 if (interval > 0 && interval < 1000) { 24967 interval = 1000; 24968 } 24969 interval *= 1000; 24970 24971 /* 24972 * submit the request to the scsi_watch service 24973 */ 24974 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24975 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24976 if (token == NULL) { 24977 return (EAGAIN); 24978 } 24979 24980 /* 24981 * save token for termination later on 24982 */ 24983 mutex_enter(SD_MUTEX(un)); 24984 un->un_mhd_token = token; 24985 mutex_exit(SD_MUTEX(un)); 24986 return (0); 24987 } 24988 24989 24990 /* 24991 * Function: sd_mhd_watch_cb() 24992 * 24993 * Description: This function is the call back function used by the scsi watch 24994 * facility. The scsi watch facility sends the "Test Unit Ready" 24995 * and processes the status. If applicable (i.e. a "Unit Attention" 24996 * status and automatic "Request Sense" not used) the scsi watch 24997 * facility will send a "Request Sense" and retrieve the sense data 24998 * to be passed to this callback function. In either case the 24999 * automatic "Request Sense" or the facility submitting one, this 25000 * callback is passed the status and sense data. 25001 * 25002 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25003 * among multiple watches that share this callback function 25004 * resultp - scsi watch facility result packet containing scsi 25005 * packet, status byte and sense data 25006 * 25007 * Return Code: 0 - continue the watch task 25008 * non-zero - terminate the watch task 25009 */ 25010 25011 static int 25012 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25013 { 25014 struct sd_lun *un; 25015 struct scsi_status *statusp; 25016 struct scsi_extended_sense *sensep; 25017 struct scsi_pkt *pkt; 25018 uchar_t actual_sense_length; 25019 dev_t dev = (dev_t)arg; 25020 25021 ASSERT(resultp != NULL); 25022 statusp = resultp->statusp; 25023 sensep = resultp->sensep; 25024 pkt = resultp->pkt; 25025 actual_sense_length = resultp->actual_sense_length; 25026 25027 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25028 return (ENXIO); 25029 } 25030 25031 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25032 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25033 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25034 25035 /* Begin processing of the status and/or sense data */ 25036 if (pkt->pkt_reason != CMD_CMPLT) { 25037 /* Handle the incomplete packet */ 25038 sd_mhd_watch_incomplete(un, pkt); 25039 return (0); 25040 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25041 if (*((unsigned char *)statusp) 25042 == STATUS_RESERVATION_CONFLICT) { 25043 /* 25044 * Handle a reservation conflict by panicking if 25045 * configured for failfast or by logging the conflict 25046 * and updating the reservation status 25047 */ 25048 mutex_enter(SD_MUTEX(un)); 25049 if ((un->un_resvd_status & SD_FAILFAST) && 25050 (sd_failfast_enable)) { 25051 sd_panic_for_res_conflict(un); 25052 /*NOTREACHED*/ 25053 } 25054 SD_INFO(SD_LOG_IOCTL_MHD, un, 25055 "sd_mhd_watch_cb: Reservation Conflict\n"); 25056 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25057 mutex_exit(SD_MUTEX(un)); 25058 } 25059 } 25060 25061 if (sensep != NULL) { 25062 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25063 mutex_enter(SD_MUTEX(un)); 25064 if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) && 25065 (un->un_resvd_status & SD_RESERVE)) { 25066 /* 25067 * The additional sense code indicates a power 25068 * on or bus device reset has occurred; update 25069 * the reservation status. 25070 */ 25071 un->un_resvd_status |= 25072 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25073 SD_INFO(SD_LOG_IOCTL_MHD, un, 25074 "sd_mhd_watch_cb: Lost Reservation\n"); 25075 } 25076 } else { 25077 return (0); 25078 } 25079 } else { 25080 mutex_enter(SD_MUTEX(un)); 25081 } 25082 25083 if ((un->un_resvd_status & SD_RESERVE) && 25084 (un->un_resvd_status & SD_LOST_RESERVE)) { 25085 if (un->un_resvd_status & SD_WANT_RESERVE) { 25086 /* 25087 * A reset occurred in between the last probe and this 25088 * one so if a timeout is pending cancel it. 25089 */ 25090 if (un->un_resvd_timeid) { 25091 timeout_id_t temp_id = un->un_resvd_timeid; 25092 un->un_resvd_timeid = NULL; 25093 mutex_exit(SD_MUTEX(un)); 25094 (void) untimeout(temp_id); 25095 mutex_enter(SD_MUTEX(un)); 25096 } 25097 un->un_resvd_status &= ~SD_WANT_RESERVE; 25098 } 25099 if (un->un_resvd_timeid == 0) { 25100 /* Schedule a timeout to handle the lost reservation */ 25101 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25102 (void *)dev, 25103 drv_usectohz(sd_reinstate_resv_delay)); 25104 } 25105 } 25106 mutex_exit(SD_MUTEX(un)); 25107 return (0); 25108 } 25109 25110 25111 /* 25112 * Function: sd_mhd_watch_incomplete() 25113 * 25114 * Description: This function is used to find out why a scsi pkt sent by the 25115 * scsi watch facility was not completed. Under some scenarios this 25116 * routine will return. Otherwise it will send a bus reset to see 25117 * if the drive is still online. 25118 * 25119 * Arguments: un - driver soft state (unit) structure 25120 * pkt - incomplete scsi pkt 25121 */ 25122 25123 static void 25124 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25125 { 25126 int be_chatty; 25127 int perr; 25128 25129 ASSERT(pkt != NULL); 25130 ASSERT(un != NULL); 25131 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25132 perr = (pkt->pkt_statistics & STAT_PERR); 25133 25134 mutex_enter(SD_MUTEX(un)); 25135 if (un->un_state == SD_STATE_DUMPING) { 25136 mutex_exit(SD_MUTEX(un)); 25137 return; 25138 } 25139 25140 switch (pkt->pkt_reason) { 25141 case CMD_UNX_BUS_FREE: 25142 /* 25143 * If we had a parity error that caused the target to drop BSY*, 25144 * don't be chatty about it. 25145 */ 25146 if (perr && be_chatty) { 25147 be_chatty = 0; 25148 } 25149 break; 25150 case CMD_TAG_REJECT: 25151 /* 25152 * The SCSI-2 spec states that a tag reject will be sent by the 25153 * target if tagged queuing is not supported. A tag reject may 25154 * also be sent during certain initialization periods or to 25155 * control internal resources. For the latter case the target 25156 * may also return Queue Full. 25157 * 25158 * If this driver receives a tag reject from a target that is 25159 * going through an init period or controlling internal 25160 * resources tagged queuing will be disabled. This is a less 25161 * than optimal behavior but the driver is unable to determine 25162 * the target state and assumes tagged queueing is not supported 25163 */ 25164 pkt->pkt_flags = 0; 25165 un->un_tagflags = 0; 25166 25167 if (un->un_f_opt_queueing == TRUE) { 25168 un->un_throttle = min(un->un_throttle, 3); 25169 } else { 25170 un->un_throttle = 1; 25171 } 25172 mutex_exit(SD_MUTEX(un)); 25173 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25174 mutex_enter(SD_MUTEX(un)); 25175 break; 25176 case CMD_INCOMPLETE: 25177 /* 25178 * The transport stopped with an abnormal state, fallthrough and 25179 * reset the target and/or bus unless selection did not complete 25180 * (indicated by STATE_GOT_BUS) in which case we don't want to 25181 * go through a target/bus reset 25182 */ 25183 if (pkt->pkt_state == STATE_GOT_BUS) { 25184 break; 25185 } 25186 /*FALLTHROUGH*/ 25187 25188 case CMD_TIMEOUT: 25189 default: 25190 /* 25191 * The lun may still be running the command, so a lun reset 25192 * should be attempted. If the lun reset fails or cannot be 25193 * issued, than try a target reset. Lastly try a bus reset. 25194 */ 25195 if ((pkt->pkt_statistics & 25196 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25197 int reset_retval = 0; 25198 mutex_exit(SD_MUTEX(un)); 25199 if (un->un_f_allow_bus_device_reset == TRUE) { 25200 if (un->un_f_lun_reset_enabled == TRUE) { 25201 reset_retval = 25202 scsi_reset(SD_ADDRESS(un), 25203 RESET_LUN); 25204 } 25205 if (reset_retval == 0) { 25206 reset_retval = 25207 scsi_reset(SD_ADDRESS(un), 25208 RESET_TARGET); 25209 } 25210 } 25211 if (reset_retval == 0) { 25212 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25213 } 25214 mutex_enter(SD_MUTEX(un)); 25215 } 25216 break; 25217 } 25218 25219 /* A device/bus reset has occurred; update the reservation status. */ 25220 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25221 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25222 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25223 un->un_resvd_status |= 25224 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25225 SD_INFO(SD_LOG_IOCTL_MHD, un, 25226 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25227 } 25228 } 25229 25230 /* 25231 * The disk has been turned off; Update the device state. 25232 * 25233 * Note: Should we be offlining the disk here? 25234 */ 25235 if (pkt->pkt_state == STATE_GOT_BUS) { 25236 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25237 "Disk not responding to selection\n"); 25238 if (un->un_state != SD_STATE_OFFLINE) { 25239 New_state(un, SD_STATE_OFFLINE); 25240 } 25241 } else if (be_chatty) { 25242 /* 25243 * suppress messages if they are all the same pkt reason; 25244 * with TQ, many (up to 256) are returned with the same 25245 * pkt_reason 25246 */ 25247 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25248 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25249 "sd_mhd_watch_incomplete: " 25250 "SCSI transport failed: reason '%s'\n", 25251 scsi_rname(pkt->pkt_reason)); 25252 } 25253 } 25254 un->un_last_pkt_reason = pkt->pkt_reason; 25255 mutex_exit(SD_MUTEX(un)); 25256 } 25257 25258 25259 /* 25260 * Function: sd_sname() 25261 * 25262 * Description: This is a simple little routine to return a string containing 25263 * a printable description of command status byte for use in 25264 * logging. 25265 * 25266 * Arguments: status - pointer to a status byte 25267 * 25268 * Return Code: char * - string containing status description. 25269 */ 25270 25271 static char * 25272 sd_sname(uchar_t status) 25273 { 25274 switch (status & STATUS_MASK) { 25275 case STATUS_GOOD: 25276 return ("good status"); 25277 case STATUS_CHECK: 25278 return ("check condition"); 25279 case STATUS_MET: 25280 return ("condition met"); 25281 case STATUS_BUSY: 25282 return ("busy"); 25283 case STATUS_INTERMEDIATE: 25284 return ("intermediate"); 25285 case STATUS_INTERMEDIATE_MET: 25286 return ("intermediate - condition met"); 25287 case STATUS_RESERVATION_CONFLICT: 25288 return ("reservation_conflict"); 25289 case STATUS_TERMINATED: 25290 return ("command terminated"); 25291 case STATUS_QFULL: 25292 return ("queue full"); 25293 default: 25294 return ("<unknown status>"); 25295 } 25296 } 25297 25298 25299 /* 25300 * Function: sd_mhd_resvd_recover() 25301 * 25302 * Description: This function adds a reservation entry to the 25303 * sd_resv_reclaim_request list and signals the reservation 25304 * reclaim thread that there is work pending. If the reservation 25305 * reclaim thread has not been previously created this function 25306 * will kick it off. 25307 * 25308 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25309 * among multiple watches that share this callback function 25310 * 25311 * Context: This routine is called by timeout() and is run in interrupt 25312 * context. It must not sleep or call other functions which may 25313 * sleep. 25314 */ 25315 25316 static void 25317 sd_mhd_resvd_recover(void *arg) 25318 { 25319 dev_t dev = (dev_t)arg; 25320 struct sd_lun *un; 25321 struct sd_thr_request *sd_treq = NULL; 25322 struct sd_thr_request *sd_cur = NULL; 25323 struct sd_thr_request *sd_prev = NULL; 25324 int already_there = 0; 25325 25326 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25327 return; 25328 } 25329 25330 mutex_enter(SD_MUTEX(un)); 25331 un->un_resvd_timeid = NULL; 25332 if (un->un_resvd_status & SD_WANT_RESERVE) { 25333 /* 25334 * There was a reset so don't issue the reserve, allow the 25335 * sd_mhd_watch_cb callback function to notice this and 25336 * reschedule the timeout for reservation. 25337 */ 25338 mutex_exit(SD_MUTEX(un)); 25339 return; 25340 } 25341 mutex_exit(SD_MUTEX(un)); 25342 25343 /* 25344 * Add this device to the sd_resv_reclaim_request list and the 25345 * sd_resv_reclaim_thread should take care of the rest. 25346 * 25347 * Note: We can't sleep in this context so if the memory allocation 25348 * fails allow the sd_mhd_watch_cb callback function to notice this and 25349 * reschedule the timeout for reservation. (4378460) 25350 */ 25351 sd_treq = (struct sd_thr_request *) 25352 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25353 if (sd_treq == NULL) { 25354 return; 25355 } 25356 25357 sd_treq->sd_thr_req_next = NULL; 25358 sd_treq->dev = dev; 25359 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25360 if (sd_tr.srq_thr_req_head == NULL) { 25361 sd_tr.srq_thr_req_head = sd_treq; 25362 } else { 25363 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25364 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25365 if (sd_cur->dev == dev) { 25366 /* 25367 * already in Queue so don't log 25368 * another request for the device 25369 */ 25370 already_there = 1; 25371 break; 25372 } 25373 sd_prev = sd_cur; 25374 } 25375 if (!already_there) { 25376 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25377 "logging request for %lx\n", dev); 25378 sd_prev->sd_thr_req_next = sd_treq; 25379 } else { 25380 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25381 } 25382 } 25383 25384 /* 25385 * Create a kernel thread to do the reservation reclaim and free up this 25386 * thread. We cannot block this thread while we go away to do the 25387 * reservation reclaim 25388 */ 25389 if (sd_tr.srq_resv_reclaim_thread == NULL) 25390 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25391 sd_resv_reclaim_thread, NULL, 25392 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25393 25394 /* Tell the reservation reclaim thread that it has work to do */ 25395 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25396 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25397 } 25398 25399 /* 25400 * Function: sd_resv_reclaim_thread() 25401 * 25402 * Description: This function implements the reservation reclaim operations 25403 * 25404 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25405 * among multiple watches that share this callback function 25406 */ 25407 25408 static void 25409 sd_resv_reclaim_thread() 25410 { 25411 struct sd_lun *un; 25412 struct sd_thr_request *sd_mhreq; 25413 25414 /* Wait for work */ 25415 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25416 if (sd_tr.srq_thr_req_head == NULL) { 25417 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25418 &sd_tr.srq_resv_reclaim_mutex); 25419 } 25420 25421 /* Loop while we have work */ 25422 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25423 un = ddi_get_soft_state(sd_state, 25424 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25425 if (un == NULL) { 25426 /* 25427 * softstate structure is NULL so just 25428 * dequeue the request and continue 25429 */ 25430 sd_tr.srq_thr_req_head = 25431 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25432 kmem_free(sd_tr.srq_thr_cur_req, 25433 sizeof (struct sd_thr_request)); 25434 continue; 25435 } 25436 25437 /* dequeue the request */ 25438 sd_mhreq = sd_tr.srq_thr_cur_req; 25439 sd_tr.srq_thr_req_head = 25440 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25441 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25442 25443 /* 25444 * Reclaim reservation only if SD_RESERVE is still set. There 25445 * may have been a call to MHIOCRELEASE before we got here. 25446 */ 25447 mutex_enter(SD_MUTEX(un)); 25448 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25449 /* 25450 * Note: The SD_LOST_RESERVE flag is cleared before 25451 * reclaiming the reservation. If this is done after the 25452 * call to sd_reserve_release a reservation loss in the 25453 * window between pkt completion of reserve cmd and 25454 * mutex_enter below may not be recognized 25455 */ 25456 un->un_resvd_status &= ~SD_LOST_RESERVE; 25457 mutex_exit(SD_MUTEX(un)); 25458 25459 if (sd_reserve_release(sd_mhreq->dev, 25460 SD_RESERVE) == 0) { 25461 mutex_enter(SD_MUTEX(un)); 25462 un->un_resvd_status |= SD_RESERVE; 25463 mutex_exit(SD_MUTEX(un)); 25464 SD_INFO(SD_LOG_IOCTL_MHD, un, 25465 "sd_resv_reclaim_thread: " 25466 "Reservation Recovered\n"); 25467 } else { 25468 mutex_enter(SD_MUTEX(un)); 25469 un->un_resvd_status |= SD_LOST_RESERVE; 25470 mutex_exit(SD_MUTEX(un)); 25471 SD_INFO(SD_LOG_IOCTL_MHD, un, 25472 "sd_resv_reclaim_thread: Failed " 25473 "Reservation Recovery\n"); 25474 } 25475 } else { 25476 mutex_exit(SD_MUTEX(un)); 25477 } 25478 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25479 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25480 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25481 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25482 /* 25483 * wakeup the destroy thread if anyone is waiting on 25484 * us to complete. 25485 */ 25486 cv_signal(&sd_tr.srq_inprocess_cv); 25487 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25488 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25489 } 25490 25491 /* 25492 * cleanup the sd_tr structure now that this thread will not exist 25493 */ 25494 ASSERT(sd_tr.srq_thr_req_head == NULL); 25495 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25496 sd_tr.srq_resv_reclaim_thread = NULL; 25497 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25498 thread_exit(); 25499 } 25500 25501 25502 /* 25503 * Function: sd_rmv_resv_reclaim_req() 25504 * 25505 * Description: This function removes any pending reservation reclaim requests 25506 * for the specified device. 25507 * 25508 * Arguments: dev - the device 'dev_t' 25509 */ 25510 25511 static void 25512 sd_rmv_resv_reclaim_req(dev_t dev) 25513 { 25514 struct sd_thr_request *sd_mhreq; 25515 struct sd_thr_request *sd_prev; 25516 25517 /* Remove a reservation reclaim request from the list */ 25518 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25519 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25520 /* 25521 * We are attempting to reinstate reservation for 25522 * this device. We wait for sd_reserve_release() 25523 * to return before we return. 25524 */ 25525 cv_wait(&sd_tr.srq_inprocess_cv, 25526 &sd_tr.srq_resv_reclaim_mutex); 25527 } else { 25528 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25529 if (sd_mhreq && sd_mhreq->dev == dev) { 25530 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25531 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25532 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25533 return; 25534 } 25535 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25536 if (sd_mhreq && sd_mhreq->dev == dev) { 25537 break; 25538 } 25539 sd_prev = sd_mhreq; 25540 } 25541 if (sd_mhreq != NULL) { 25542 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25543 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25544 } 25545 } 25546 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25547 } 25548 25549 25550 /* 25551 * Function: sd_mhd_reset_notify_cb() 25552 * 25553 * Description: This is a call back function for scsi_reset_notify. This 25554 * function updates the softstate reserved status and logs the 25555 * reset. The driver scsi watch facility callback function 25556 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25557 * will reclaim the reservation. 25558 * 25559 * Arguments: arg - driver soft state (unit) structure 25560 */ 25561 25562 static void 25563 sd_mhd_reset_notify_cb(caddr_t arg) 25564 { 25565 struct sd_lun *un = (struct sd_lun *)arg; 25566 25567 mutex_enter(SD_MUTEX(un)); 25568 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25569 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25570 SD_INFO(SD_LOG_IOCTL_MHD, un, 25571 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25572 } 25573 mutex_exit(SD_MUTEX(un)); 25574 } 25575 25576 25577 /* 25578 * Function: sd_take_ownership() 25579 * 25580 * Description: This routine implements an algorithm to achieve a stable 25581 * reservation on disks which don't implement priority reserve, 25582 * and makes sure that other host lose re-reservation attempts. 25583 * This algorithm contains of a loop that keeps issuing the RESERVE 25584 * for some period of time (min_ownership_delay, default 6 seconds) 25585 * During that loop, it looks to see if there has been a bus device 25586 * reset or bus reset (both of which cause an existing reservation 25587 * to be lost). If the reservation is lost issue RESERVE until a 25588 * period of min_ownership_delay with no resets has gone by, or 25589 * until max_ownership_delay has expired. This loop ensures that 25590 * the host really did manage to reserve the device, in spite of 25591 * resets. The looping for min_ownership_delay (default six 25592 * seconds) is important to early generation clustering products, 25593 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25594 * MHIOCENFAILFAST periodic timer of two seconds. By having 25595 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25596 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25597 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25598 * have already noticed, via the MHIOCENFAILFAST polling, that it 25599 * no longer "owns" the disk and will have panicked itself. Thus, 25600 * the host issuing the MHIOCTKOWN is assured (with timing 25601 * dependencies) that by the time it actually starts to use the 25602 * disk for real work, the old owner is no longer accessing it. 25603 * 25604 * min_ownership_delay is the minimum amount of time for which the 25605 * disk must be reserved continuously devoid of resets before the 25606 * MHIOCTKOWN ioctl will return success. 25607 * 25608 * max_ownership_delay indicates the amount of time by which the 25609 * take ownership should succeed or timeout with an error. 25610 * 25611 * Arguments: dev - the device 'dev_t' 25612 * *p - struct containing timing info. 25613 * 25614 * Return Code: 0 for success or error code 25615 */ 25616 25617 static int 25618 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25619 { 25620 struct sd_lun *un; 25621 int rval; 25622 int err; 25623 int reservation_count = 0; 25624 int min_ownership_delay = 6000000; /* in usec */ 25625 int max_ownership_delay = 30000000; /* in usec */ 25626 clock_t start_time; /* starting time of this algorithm */ 25627 clock_t end_time; /* time limit for giving up */ 25628 clock_t ownership_time; /* time limit for stable ownership */ 25629 clock_t current_time; 25630 clock_t previous_current_time; 25631 25632 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25633 return (ENXIO); 25634 } 25635 25636 /* 25637 * Attempt a device reservation. A priority reservation is requested. 25638 */ 25639 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25640 != SD_SUCCESS) { 25641 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25642 "sd_take_ownership: return(1)=%d\n", rval); 25643 return (rval); 25644 } 25645 25646 /* Update the softstate reserved status to indicate the reservation */ 25647 mutex_enter(SD_MUTEX(un)); 25648 un->un_resvd_status |= SD_RESERVE; 25649 un->un_resvd_status &= 25650 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25651 mutex_exit(SD_MUTEX(un)); 25652 25653 if (p != NULL) { 25654 if (p->min_ownership_delay != 0) { 25655 min_ownership_delay = p->min_ownership_delay * 1000; 25656 } 25657 if (p->max_ownership_delay != 0) { 25658 max_ownership_delay = p->max_ownership_delay * 1000; 25659 } 25660 } 25661 SD_INFO(SD_LOG_IOCTL_MHD, un, 25662 "sd_take_ownership: min, max delays: %d, %d\n", 25663 min_ownership_delay, max_ownership_delay); 25664 25665 start_time = ddi_get_lbolt(); 25666 current_time = start_time; 25667 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25668 end_time = start_time + drv_usectohz(max_ownership_delay); 25669 25670 while (current_time - end_time < 0) { 25671 delay(drv_usectohz(500000)); 25672 25673 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25674 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25675 mutex_enter(SD_MUTEX(un)); 25676 rval = (un->un_resvd_status & 25677 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25678 mutex_exit(SD_MUTEX(un)); 25679 break; 25680 } 25681 } 25682 previous_current_time = current_time; 25683 current_time = ddi_get_lbolt(); 25684 mutex_enter(SD_MUTEX(un)); 25685 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25686 ownership_time = ddi_get_lbolt() + 25687 drv_usectohz(min_ownership_delay); 25688 reservation_count = 0; 25689 } else { 25690 reservation_count++; 25691 } 25692 un->un_resvd_status |= SD_RESERVE; 25693 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25694 mutex_exit(SD_MUTEX(un)); 25695 25696 SD_INFO(SD_LOG_IOCTL_MHD, un, 25697 "sd_take_ownership: ticks for loop iteration=%ld, " 25698 "reservation=%s\n", (current_time - previous_current_time), 25699 reservation_count ? "ok" : "reclaimed"); 25700 25701 if (current_time - ownership_time >= 0 && 25702 reservation_count >= 4) { 25703 rval = 0; /* Achieved a stable ownership */ 25704 break; 25705 } 25706 if (current_time - end_time >= 0) { 25707 rval = EACCES; /* No ownership in max possible time */ 25708 break; 25709 } 25710 } 25711 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25712 "sd_take_ownership: return(2)=%d\n", rval); 25713 return (rval); 25714 } 25715 25716 25717 /* 25718 * Function: sd_reserve_release() 25719 * 25720 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25721 * PRIORITY RESERVE commands based on a user specified command type 25722 * 25723 * Arguments: dev - the device 'dev_t' 25724 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25725 * SD_RESERVE, SD_RELEASE 25726 * 25727 * Return Code: 0 or Error Code 25728 */ 25729 25730 static int 25731 sd_reserve_release(dev_t dev, int cmd) 25732 { 25733 struct uscsi_cmd *com = NULL; 25734 struct sd_lun *un = NULL; 25735 char cdb[CDB_GROUP0]; 25736 int rval; 25737 25738 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25739 (cmd == SD_PRIORITY_RESERVE)); 25740 25741 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25742 return (ENXIO); 25743 } 25744 25745 /* instantiate and initialize the command and cdb */ 25746 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25747 bzero(cdb, CDB_GROUP0); 25748 com->uscsi_flags = USCSI_SILENT; 25749 com->uscsi_timeout = un->un_reserve_release_time; 25750 com->uscsi_cdblen = CDB_GROUP0; 25751 com->uscsi_cdb = cdb; 25752 if (cmd == SD_RELEASE) { 25753 cdb[0] = SCMD_RELEASE; 25754 } else { 25755 cdb[0] = SCMD_RESERVE; 25756 } 25757 25758 /* Send the command. */ 25759 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25760 UIO_SYSSPACE, SD_PATH_STANDARD); 25761 25762 /* 25763 * "break" a reservation that is held by another host, by issuing a 25764 * reset if priority reserve is desired, and we could not get the 25765 * device. 25766 */ 25767 if ((cmd == SD_PRIORITY_RESERVE) && 25768 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25769 /* 25770 * First try to reset the LUN. If we cannot, then try a target 25771 * reset, followed by a bus reset if the target reset fails. 25772 */ 25773 int reset_retval = 0; 25774 if (un->un_f_lun_reset_enabled == TRUE) { 25775 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25776 } 25777 if (reset_retval == 0) { 25778 /* The LUN reset either failed or was not issued */ 25779 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25780 } 25781 if ((reset_retval == 0) && 25782 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25783 rval = EIO; 25784 kmem_free(com, sizeof (*com)); 25785 return (rval); 25786 } 25787 25788 bzero(com, sizeof (struct uscsi_cmd)); 25789 com->uscsi_flags = USCSI_SILENT; 25790 com->uscsi_cdb = cdb; 25791 com->uscsi_cdblen = CDB_GROUP0; 25792 com->uscsi_timeout = 5; 25793 25794 /* 25795 * Reissue the last reserve command, this time without request 25796 * sense. Assume that it is just a regular reserve command. 25797 */ 25798 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25799 UIO_SYSSPACE, SD_PATH_STANDARD); 25800 } 25801 25802 /* Return an error if still getting a reservation conflict. */ 25803 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25804 rval = EACCES; 25805 } 25806 25807 kmem_free(com, sizeof (*com)); 25808 return (rval); 25809 } 25810 25811 25812 #define SD_NDUMP_RETRIES 12 25813 /* 25814 * System Crash Dump routine 25815 */ 25816 25817 static int 25818 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25819 { 25820 int instance; 25821 int partition; 25822 int i; 25823 int err; 25824 struct sd_lun *un; 25825 struct dk_map *lp; 25826 struct scsi_pkt *wr_pktp; 25827 struct buf *wr_bp; 25828 struct buf wr_buf; 25829 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25830 daddr_t tgt_blkno; /* rmw - blkno for target */ 25831 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25832 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25833 size_t io_start_offset; 25834 int doing_rmw = FALSE; 25835 int rval; 25836 #if defined(__i386) || defined(__amd64) 25837 ssize_t dma_resid; 25838 daddr_t oblkno; 25839 #endif 25840 25841 instance = SDUNIT(dev); 25842 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25843 (!un->un_f_geometry_is_valid) || ISCD(un)) { 25844 return (ENXIO); 25845 } 25846 25847 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25848 25849 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25850 25851 partition = SDPART(dev); 25852 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25853 25854 /* Validate blocks to dump at against partition size. */ 25855 lp = &un->un_map[partition]; 25856 if ((blkno + nblk) > lp->dkl_nblk) { 25857 SD_TRACE(SD_LOG_DUMP, un, 25858 "sddump: dump range larger than partition: " 25859 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25860 blkno, nblk, lp->dkl_nblk); 25861 return (EINVAL); 25862 } 25863 25864 mutex_enter(&un->un_pm_mutex); 25865 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25866 struct scsi_pkt *start_pktp; 25867 25868 mutex_exit(&un->un_pm_mutex); 25869 25870 /* 25871 * use pm framework to power on HBA 1st 25872 */ 25873 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 25874 25875 /* 25876 * Dump no long uses sdpower to power on a device, it's 25877 * in-line here so it can be done in polled mode. 25878 */ 25879 25880 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25881 25882 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25883 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25884 25885 if (start_pktp == NULL) { 25886 /* We were not given a SCSI packet, fail. */ 25887 return (EIO); 25888 } 25889 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25890 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25891 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25892 start_pktp->pkt_flags = FLAG_NOINTR; 25893 25894 mutex_enter(SD_MUTEX(un)); 25895 SD_FILL_SCSI1_LUN(un, start_pktp); 25896 mutex_exit(SD_MUTEX(un)); 25897 /* 25898 * Scsi_poll returns 0 (success) if the command completes and 25899 * the status block is STATUS_GOOD. 25900 */ 25901 if (sd_scsi_poll(un, start_pktp) != 0) { 25902 scsi_destroy_pkt(start_pktp); 25903 return (EIO); 25904 } 25905 scsi_destroy_pkt(start_pktp); 25906 (void) sd_ddi_pm_resume(un); 25907 } else { 25908 mutex_exit(&un->un_pm_mutex); 25909 } 25910 25911 mutex_enter(SD_MUTEX(un)); 25912 un->un_throttle = 0; 25913 25914 /* 25915 * The first time through, reset the specific target device. 25916 * However, when cpr calls sddump we know that sd is in a 25917 * a good state so no bus reset is required. 25918 * Clear sense data via Request Sense cmd. 25919 * In sddump we don't care about allow_bus_device_reset anymore 25920 */ 25921 25922 if ((un->un_state != SD_STATE_SUSPENDED) && 25923 (un->un_state != SD_STATE_DUMPING)) { 25924 25925 New_state(un, SD_STATE_DUMPING); 25926 25927 if (un->un_f_is_fibre == FALSE) { 25928 mutex_exit(SD_MUTEX(un)); 25929 /* 25930 * Attempt a bus reset for parallel scsi. 25931 * 25932 * Note: A bus reset is required because on some host 25933 * systems (i.e. E420R) a bus device reset is 25934 * insufficient to reset the state of the target. 25935 * 25936 * Note: Don't issue the reset for fibre-channel, 25937 * because this tends to hang the bus (loop) for 25938 * too long while everyone is logging out and in 25939 * and the deadman timer for dumping will fire 25940 * before the dump is complete. 25941 */ 25942 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25943 mutex_enter(SD_MUTEX(un)); 25944 Restore_state(un); 25945 mutex_exit(SD_MUTEX(un)); 25946 return (EIO); 25947 } 25948 25949 /* Delay to give the device some recovery time. */ 25950 drv_usecwait(10000); 25951 25952 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25953 SD_INFO(SD_LOG_DUMP, un, 25954 "sddump: sd_send_polled_RQS failed\n"); 25955 } 25956 mutex_enter(SD_MUTEX(un)); 25957 } 25958 } 25959 25960 /* 25961 * Convert the partition-relative block number to a 25962 * disk physical block number. 25963 */ 25964 blkno += un->un_offset[partition]; 25965 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25966 25967 25968 /* 25969 * Check if the device has a non-512 block size. 25970 */ 25971 wr_bp = NULL; 25972 if (NOT_DEVBSIZE(un)) { 25973 tgt_byte_offset = blkno * un->un_sys_blocksize; 25974 tgt_byte_count = nblk * un->un_sys_blocksize; 25975 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25976 (tgt_byte_count % un->un_tgt_blocksize)) { 25977 doing_rmw = TRUE; 25978 /* 25979 * Calculate the block number and number of block 25980 * in terms of the media block size. 25981 */ 25982 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25983 tgt_nblk = 25984 ((tgt_byte_offset + tgt_byte_count + 25985 (un->un_tgt_blocksize - 1)) / 25986 un->un_tgt_blocksize) - tgt_blkno; 25987 25988 /* 25989 * Invoke the routine which is going to do read part 25990 * of read-modify-write. 25991 * Note that this routine returns a pointer to 25992 * a valid bp in wr_bp. 25993 */ 25994 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25995 &wr_bp); 25996 if (err) { 25997 mutex_exit(SD_MUTEX(un)); 25998 return (err); 25999 } 26000 /* 26001 * Offset is being calculated as - 26002 * (original block # * system block size) - 26003 * (new block # * target block size) 26004 */ 26005 io_start_offset = 26006 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26007 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26008 26009 ASSERT((io_start_offset >= 0) && 26010 (io_start_offset < un->un_tgt_blocksize)); 26011 /* 26012 * Do the modify portion of read modify write. 26013 */ 26014 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26015 (size_t)nblk * un->un_sys_blocksize); 26016 } else { 26017 doing_rmw = FALSE; 26018 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26019 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26020 } 26021 26022 /* Convert blkno and nblk to target blocks */ 26023 blkno = tgt_blkno; 26024 nblk = tgt_nblk; 26025 } else { 26026 wr_bp = &wr_buf; 26027 bzero(wr_bp, sizeof (struct buf)); 26028 wr_bp->b_flags = B_BUSY; 26029 wr_bp->b_un.b_addr = addr; 26030 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26031 wr_bp->b_resid = 0; 26032 } 26033 26034 mutex_exit(SD_MUTEX(un)); 26035 26036 /* 26037 * Obtain a SCSI packet for the write command. 26038 * It should be safe to call the allocator here without 26039 * worrying about being locked for DVMA mapping because 26040 * the address we're passed is already a DVMA mapping 26041 * 26042 * We are also not going to worry about semaphore ownership 26043 * in the dump buffer. Dumping is single threaded at present. 26044 */ 26045 26046 wr_pktp = NULL; 26047 26048 #if defined(__i386) || defined(__amd64) 26049 dma_resid = wr_bp->b_bcount; 26050 oblkno = blkno; 26051 while (dma_resid != 0) { 26052 #endif 26053 26054 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26055 wr_bp->b_flags &= ~B_ERROR; 26056 26057 #if defined(__i386) || defined(__amd64) 26058 blkno = oblkno + 26059 ((wr_bp->b_bcount - dma_resid) / 26060 un->un_tgt_blocksize); 26061 nblk = dma_resid / un->un_tgt_blocksize; 26062 26063 if (wr_pktp) { 26064 /* Partial DMA transfers after initial transfer */ 26065 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26066 blkno, nblk); 26067 } else { 26068 /* Initial transfer */ 26069 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26070 un->un_pkt_flags, NULL_FUNC, NULL, 26071 blkno, nblk); 26072 } 26073 #else 26074 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26075 0, NULL_FUNC, NULL, blkno, nblk); 26076 #endif 26077 26078 if (rval == 0) { 26079 /* We were given a SCSI packet, continue. */ 26080 break; 26081 } 26082 26083 if (i == 0) { 26084 if (wr_bp->b_flags & B_ERROR) { 26085 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26086 "no resources for dumping; " 26087 "error code: 0x%x, retrying", 26088 geterror(wr_bp)); 26089 } else { 26090 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26091 "no resources for dumping; retrying"); 26092 } 26093 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26094 if (wr_bp->b_flags & B_ERROR) { 26095 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26096 "no resources for dumping; error code: " 26097 "0x%x, retrying\n", geterror(wr_bp)); 26098 } 26099 } else { 26100 if (wr_bp->b_flags & B_ERROR) { 26101 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26102 "no resources for dumping; " 26103 "error code: 0x%x, retries failed, " 26104 "giving up.\n", geterror(wr_bp)); 26105 } else { 26106 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26107 "no resources for dumping; " 26108 "retries failed, giving up.\n"); 26109 } 26110 mutex_enter(SD_MUTEX(un)); 26111 Restore_state(un); 26112 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26113 mutex_exit(SD_MUTEX(un)); 26114 scsi_free_consistent_buf(wr_bp); 26115 } else { 26116 mutex_exit(SD_MUTEX(un)); 26117 } 26118 return (EIO); 26119 } 26120 drv_usecwait(10000); 26121 } 26122 26123 #if defined(__i386) || defined(__amd64) 26124 /* 26125 * save the resid from PARTIAL_DMA 26126 */ 26127 dma_resid = wr_pktp->pkt_resid; 26128 if (dma_resid != 0) 26129 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26130 wr_pktp->pkt_resid = 0; 26131 #endif 26132 26133 /* SunBug 1222170 */ 26134 wr_pktp->pkt_flags = FLAG_NOINTR; 26135 26136 err = EIO; 26137 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26138 26139 /* 26140 * Scsi_poll returns 0 (success) if the command completes and 26141 * the status block is STATUS_GOOD. We should only check 26142 * errors if this condition is not true. Even then we should 26143 * send our own request sense packet only if we have a check 26144 * condition and auto request sense has not been performed by 26145 * the hba. 26146 */ 26147 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26148 26149 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26150 (wr_pktp->pkt_resid == 0)) { 26151 err = SD_SUCCESS; 26152 break; 26153 } 26154 26155 /* 26156 * Check CMD_DEV_GONE 1st, give up if device is gone. 26157 */ 26158 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26159 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26160 "Device is gone\n"); 26161 break; 26162 } 26163 26164 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26165 SD_INFO(SD_LOG_DUMP, un, 26166 "sddump: write failed with CHECK, try # %d\n", i); 26167 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26168 (void) sd_send_polled_RQS(un); 26169 } 26170 26171 continue; 26172 } 26173 26174 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26175 int reset_retval = 0; 26176 26177 SD_INFO(SD_LOG_DUMP, un, 26178 "sddump: write failed with BUSY, try # %d\n", i); 26179 26180 if (un->un_f_lun_reset_enabled == TRUE) { 26181 reset_retval = scsi_reset(SD_ADDRESS(un), 26182 RESET_LUN); 26183 } 26184 if (reset_retval == 0) { 26185 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26186 } 26187 (void) sd_send_polled_RQS(un); 26188 26189 } else { 26190 SD_INFO(SD_LOG_DUMP, un, 26191 "sddump: write failed with 0x%x, try # %d\n", 26192 SD_GET_PKT_STATUS(wr_pktp), i); 26193 mutex_enter(SD_MUTEX(un)); 26194 sd_reset_target(un, wr_pktp); 26195 mutex_exit(SD_MUTEX(un)); 26196 } 26197 26198 /* 26199 * If we are not getting anywhere with lun/target resets, 26200 * let's reset the bus. 26201 */ 26202 if (i == SD_NDUMP_RETRIES/2) { 26203 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26204 (void) sd_send_polled_RQS(un); 26205 } 26206 26207 } 26208 #if defined(__i386) || defined(__amd64) 26209 } /* dma_resid */ 26210 #endif 26211 26212 scsi_destroy_pkt(wr_pktp); 26213 mutex_enter(SD_MUTEX(un)); 26214 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26215 mutex_exit(SD_MUTEX(un)); 26216 scsi_free_consistent_buf(wr_bp); 26217 } else { 26218 mutex_exit(SD_MUTEX(un)); 26219 } 26220 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26221 return (err); 26222 } 26223 26224 /* 26225 * Function: sd_scsi_poll() 26226 * 26227 * Description: This is a wrapper for the scsi_poll call. 26228 * 26229 * Arguments: sd_lun - The unit structure 26230 * scsi_pkt - The scsi packet being sent to the device. 26231 * 26232 * Return Code: 0 - Command completed successfully with good status 26233 * -1 - Command failed. This could indicate a check condition 26234 * or other status value requiring recovery action. 26235 * 26236 */ 26237 26238 static int 26239 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26240 { 26241 int status; 26242 26243 ASSERT(un != NULL); 26244 ASSERT(!mutex_owned(SD_MUTEX(un))); 26245 ASSERT(pktp != NULL); 26246 26247 status = SD_SUCCESS; 26248 26249 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26250 pktp->pkt_flags |= un->un_tagflags; 26251 pktp->pkt_flags &= ~FLAG_NODISCON; 26252 } 26253 26254 status = sd_ddi_scsi_poll(pktp); 26255 /* 26256 * Scsi_poll returns 0 (success) if the command completes and the 26257 * status block is STATUS_GOOD. We should only check errors if this 26258 * condition is not true. Even then we should send our own request 26259 * sense packet only if we have a check condition and auto 26260 * request sense has not been performed by the hba. 26261 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26262 */ 26263 if ((status != SD_SUCCESS) && 26264 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26265 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26266 (pktp->pkt_reason != CMD_DEV_GONE)) 26267 (void) sd_send_polled_RQS(un); 26268 26269 return (status); 26270 } 26271 26272 /* 26273 * Function: sd_send_polled_RQS() 26274 * 26275 * Description: This sends the request sense command to a device. 26276 * 26277 * Arguments: sd_lun - The unit structure 26278 * 26279 * Return Code: 0 - Command completed successfully with good status 26280 * -1 - Command failed. 26281 * 26282 */ 26283 26284 static int 26285 sd_send_polled_RQS(struct sd_lun *un) 26286 { 26287 int ret_val; 26288 struct scsi_pkt *rqs_pktp; 26289 struct buf *rqs_bp; 26290 26291 ASSERT(un != NULL); 26292 ASSERT(!mutex_owned(SD_MUTEX(un))); 26293 26294 ret_val = SD_SUCCESS; 26295 26296 rqs_pktp = un->un_rqs_pktp; 26297 rqs_bp = un->un_rqs_bp; 26298 26299 mutex_enter(SD_MUTEX(un)); 26300 26301 if (un->un_sense_isbusy) { 26302 ret_val = SD_FAILURE; 26303 mutex_exit(SD_MUTEX(un)); 26304 return (ret_val); 26305 } 26306 26307 /* 26308 * If the request sense buffer (and packet) is not in use, 26309 * let's set the un_sense_isbusy and send our packet 26310 */ 26311 un->un_sense_isbusy = 1; 26312 rqs_pktp->pkt_resid = 0; 26313 rqs_pktp->pkt_reason = 0; 26314 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26315 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26316 26317 mutex_exit(SD_MUTEX(un)); 26318 26319 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26320 " 0x%p\n", rqs_bp->b_un.b_addr); 26321 26322 /* 26323 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26324 * axle - it has a call into us! 26325 */ 26326 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26327 SD_INFO(SD_LOG_COMMON, un, 26328 "sd_send_polled_RQS: RQS failed\n"); 26329 } 26330 26331 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26332 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26333 26334 mutex_enter(SD_MUTEX(un)); 26335 un->un_sense_isbusy = 0; 26336 mutex_exit(SD_MUTEX(un)); 26337 26338 return (ret_val); 26339 } 26340 26341 /* 26342 * Defines needed for localized version of the scsi_poll routine. 26343 */ 26344 #define SD_CSEC 10000 /* usecs */ 26345 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26346 26347 26348 /* 26349 * Function: sd_ddi_scsi_poll() 26350 * 26351 * Description: Localized version of the scsi_poll routine. The purpose is to 26352 * send a scsi_pkt to a device as a polled command. This version 26353 * is to ensure more robust handling of transport errors. 26354 * Specifically this routine cures not ready, coming ready 26355 * transition for power up and reset of sonoma's. This can take 26356 * up to 45 seconds for power-on and 20 seconds for reset of a 26357 * sonoma lun. 26358 * 26359 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26360 * 26361 * Return Code: 0 - Command completed successfully with good status 26362 * -1 - Command failed. 26363 * 26364 */ 26365 26366 static int 26367 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26368 { 26369 int busy_count; 26370 int timeout; 26371 int rval = SD_FAILURE; 26372 int savef; 26373 struct scsi_extended_sense *sensep; 26374 long savet; 26375 void (*savec)(); 26376 /* 26377 * The following is defined in machdep.c and is used in determining if 26378 * the scsi transport system will do polled I/O instead of interrupt 26379 * I/O when called from xx_dump(). 26380 */ 26381 extern int do_polled_io; 26382 26383 /* 26384 * save old flags in pkt, to restore at end 26385 */ 26386 savef = pkt->pkt_flags; 26387 savec = pkt->pkt_comp; 26388 savet = pkt->pkt_time; 26389 26390 pkt->pkt_flags |= FLAG_NOINTR; 26391 26392 /* 26393 * XXX there is nothing in the SCSA spec that states that we should not 26394 * do a callback for polled cmds; however, removing this will break sd 26395 * and probably other target drivers 26396 */ 26397 pkt->pkt_comp = NULL; 26398 26399 /* 26400 * we don't like a polled command without timeout. 26401 * 60 seconds seems long enough. 26402 */ 26403 if (pkt->pkt_time == 0) { 26404 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26405 } 26406 26407 /* 26408 * Send polled cmd. 26409 * 26410 * We do some error recovery for various errors. Tran_busy, 26411 * queue full, and non-dispatched commands are retried every 10 msec. 26412 * as they are typically transient failures. Busy status and Not 26413 * Ready are retried every second as this status takes a while to 26414 * change. Unit attention is retried for pkt_time (60) times 26415 * with no delay. 26416 */ 26417 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26418 26419 for (busy_count = 0; busy_count < timeout; busy_count++) { 26420 int rc; 26421 int poll_delay; 26422 26423 /* 26424 * Initialize pkt status variables. 26425 */ 26426 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26427 26428 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26429 if (rc != TRAN_BUSY) { 26430 /* Transport failed - give up. */ 26431 break; 26432 } else { 26433 /* Transport busy - try again. */ 26434 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26435 } 26436 } else { 26437 /* 26438 * Transport accepted - check pkt status. 26439 */ 26440 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26441 if (pkt->pkt_reason == CMD_CMPLT && 26442 rc == STATUS_CHECK && 26443 pkt->pkt_state & STATE_ARQ_DONE) { 26444 struct scsi_arq_status *arqstat = 26445 (struct scsi_arq_status *)(pkt->pkt_scbp); 26446 26447 sensep = &arqstat->sts_sensedata; 26448 } else { 26449 sensep = NULL; 26450 } 26451 26452 if ((pkt->pkt_reason == CMD_CMPLT) && 26453 (rc == STATUS_GOOD)) { 26454 /* No error - we're done */ 26455 rval = SD_SUCCESS; 26456 break; 26457 26458 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26459 /* Lost connection - give up */ 26460 break; 26461 26462 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26463 (pkt->pkt_state == 0)) { 26464 /* Pkt not dispatched - try again. */ 26465 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26466 26467 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26468 (rc == STATUS_QFULL)) { 26469 /* Queue full - try again. */ 26470 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26471 26472 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26473 (rc == STATUS_BUSY)) { 26474 /* Busy - try again. */ 26475 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26476 busy_count += (SD_SEC_TO_CSEC - 1); 26477 26478 } else if ((sensep != NULL) && 26479 (sensep->es_key == KEY_UNIT_ATTENTION)) { 26480 /* Unit Attention - try again */ 26481 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26482 continue; 26483 26484 } else if ((sensep != NULL) && 26485 (sensep->es_key == KEY_NOT_READY) && 26486 (sensep->es_add_code == 0x04) && 26487 (sensep->es_qual_code == 0x01)) { 26488 /* Not ready -> ready - try again. */ 26489 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26490 busy_count += (SD_SEC_TO_CSEC - 1); 26491 26492 } else { 26493 /* BAD status - give up. */ 26494 break; 26495 } 26496 } 26497 26498 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26499 !do_polled_io) { 26500 delay(drv_usectohz(poll_delay)); 26501 } else { 26502 /* we busy wait during cpr_dump or interrupt threads */ 26503 drv_usecwait(poll_delay); 26504 } 26505 } 26506 26507 pkt->pkt_flags = savef; 26508 pkt->pkt_comp = savec; 26509 pkt->pkt_time = savet; 26510 return (rval); 26511 } 26512 26513 26514 /* 26515 * Function: sd_persistent_reservation_in_read_keys 26516 * 26517 * Description: This routine is the driver entry point for handling CD-ROM 26518 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26519 * by sending the SCSI-3 PRIN commands to the device. 26520 * Processes the read keys command response by copying the 26521 * reservation key information into the user provided buffer. 26522 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26523 * 26524 * Arguments: un - Pointer to soft state struct for the target. 26525 * usrp - user provided pointer to multihost Persistent In Read 26526 * Keys structure (mhioc_inkeys_t) 26527 * flag - this argument is a pass through to ddi_copyxxx() 26528 * directly from the mode argument of ioctl(). 26529 * 26530 * Return Code: 0 - Success 26531 * EACCES 26532 * ENOTSUP 26533 * errno return code from sd_send_scsi_cmd() 26534 * 26535 * Context: Can sleep. Does not return until command is completed. 26536 */ 26537 26538 static int 26539 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26540 mhioc_inkeys_t *usrp, int flag) 26541 { 26542 #ifdef _MULTI_DATAMODEL 26543 struct mhioc_key_list32 li32; 26544 #endif 26545 sd_prin_readkeys_t *in; 26546 mhioc_inkeys_t *ptr; 26547 mhioc_key_list_t li; 26548 uchar_t *data_bufp; 26549 int data_len; 26550 int rval; 26551 size_t copysz; 26552 26553 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26554 return (EINVAL); 26555 } 26556 bzero(&li, sizeof (mhioc_key_list_t)); 26557 26558 /* 26559 * Get the listsize from user 26560 */ 26561 #ifdef _MULTI_DATAMODEL 26562 26563 switch (ddi_model_convert_from(flag & FMODELS)) { 26564 case DDI_MODEL_ILP32: 26565 copysz = sizeof (struct mhioc_key_list32); 26566 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26567 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26568 "sd_persistent_reservation_in_read_keys: " 26569 "failed ddi_copyin: mhioc_key_list32_t\n"); 26570 rval = EFAULT; 26571 goto done; 26572 } 26573 li.listsize = li32.listsize; 26574 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26575 break; 26576 26577 case DDI_MODEL_NONE: 26578 copysz = sizeof (mhioc_key_list_t); 26579 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26580 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26581 "sd_persistent_reservation_in_read_keys: " 26582 "failed ddi_copyin: mhioc_key_list_t\n"); 26583 rval = EFAULT; 26584 goto done; 26585 } 26586 break; 26587 } 26588 26589 #else /* ! _MULTI_DATAMODEL */ 26590 copysz = sizeof (mhioc_key_list_t); 26591 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26592 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26593 "sd_persistent_reservation_in_read_keys: " 26594 "failed ddi_copyin: mhioc_key_list_t\n"); 26595 rval = EFAULT; 26596 goto done; 26597 } 26598 #endif 26599 26600 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26601 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26602 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26603 26604 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26605 data_len, data_bufp)) != 0) { 26606 goto done; 26607 } 26608 in = (sd_prin_readkeys_t *)data_bufp; 26609 ptr->generation = BE_32(in->generation); 26610 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26611 26612 /* 26613 * Return the min(listsize, listlen) keys 26614 */ 26615 #ifdef _MULTI_DATAMODEL 26616 26617 switch (ddi_model_convert_from(flag & FMODELS)) { 26618 case DDI_MODEL_ILP32: 26619 li32.listlen = li.listlen; 26620 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26621 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26622 "sd_persistent_reservation_in_read_keys: " 26623 "failed ddi_copyout: mhioc_key_list32_t\n"); 26624 rval = EFAULT; 26625 goto done; 26626 } 26627 break; 26628 26629 case DDI_MODEL_NONE: 26630 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26631 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26632 "sd_persistent_reservation_in_read_keys: " 26633 "failed ddi_copyout: mhioc_key_list_t\n"); 26634 rval = EFAULT; 26635 goto done; 26636 } 26637 break; 26638 } 26639 26640 #else /* ! _MULTI_DATAMODEL */ 26641 26642 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26643 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26644 "sd_persistent_reservation_in_read_keys: " 26645 "failed ddi_copyout: mhioc_key_list_t\n"); 26646 rval = EFAULT; 26647 goto done; 26648 } 26649 26650 #endif /* _MULTI_DATAMODEL */ 26651 26652 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26653 li.listsize * MHIOC_RESV_KEY_SIZE); 26654 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26655 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26656 "sd_persistent_reservation_in_read_keys: " 26657 "failed ddi_copyout: keylist\n"); 26658 rval = EFAULT; 26659 } 26660 done: 26661 kmem_free(data_bufp, data_len); 26662 return (rval); 26663 } 26664 26665 26666 /* 26667 * Function: sd_persistent_reservation_in_read_resv 26668 * 26669 * Description: This routine is the driver entry point for handling CD-ROM 26670 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26671 * by sending the SCSI-3 PRIN commands to the device. 26672 * Process the read persistent reservations command response by 26673 * copying the reservation information into the user provided 26674 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26675 * 26676 * Arguments: un - Pointer to soft state struct for the target. 26677 * usrp - user provided pointer to multihost Persistent In Read 26678 * Keys structure (mhioc_inkeys_t) 26679 * flag - this argument is a pass through to ddi_copyxxx() 26680 * directly from the mode argument of ioctl(). 26681 * 26682 * Return Code: 0 - Success 26683 * EACCES 26684 * ENOTSUP 26685 * errno return code from sd_send_scsi_cmd() 26686 * 26687 * Context: Can sleep. Does not return until command is completed. 26688 */ 26689 26690 static int 26691 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26692 mhioc_inresvs_t *usrp, int flag) 26693 { 26694 #ifdef _MULTI_DATAMODEL 26695 struct mhioc_resv_desc_list32 resvlist32; 26696 #endif 26697 sd_prin_readresv_t *in; 26698 mhioc_inresvs_t *ptr; 26699 sd_readresv_desc_t *readresv_ptr; 26700 mhioc_resv_desc_list_t resvlist; 26701 mhioc_resv_desc_t resvdesc; 26702 uchar_t *data_bufp; 26703 int data_len; 26704 int rval; 26705 int i; 26706 size_t copysz; 26707 mhioc_resv_desc_t *bufp; 26708 26709 if ((ptr = usrp) == NULL) { 26710 return (EINVAL); 26711 } 26712 26713 /* 26714 * Get the listsize from user 26715 */ 26716 #ifdef _MULTI_DATAMODEL 26717 switch (ddi_model_convert_from(flag & FMODELS)) { 26718 case DDI_MODEL_ILP32: 26719 copysz = sizeof (struct mhioc_resv_desc_list32); 26720 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26721 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26722 "sd_persistent_reservation_in_read_resv: " 26723 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26724 rval = EFAULT; 26725 goto done; 26726 } 26727 resvlist.listsize = resvlist32.listsize; 26728 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26729 break; 26730 26731 case DDI_MODEL_NONE: 26732 copysz = sizeof (mhioc_resv_desc_list_t); 26733 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26734 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26735 "sd_persistent_reservation_in_read_resv: " 26736 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26737 rval = EFAULT; 26738 goto done; 26739 } 26740 break; 26741 } 26742 #else /* ! _MULTI_DATAMODEL */ 26743 copysz = sizeof (mhioc_resv_desc_list_t); 26744 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26745 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26746 "sd_persistent_reservation_in_read_resv: " 26747 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26748 rval = EFAULT; 26749 goto done; 26750 } 26751 #endif /* ! _MULTI_DATAMODEL */ 26752 26753 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26754 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26755 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26756 26757 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 26758 data_len, data_bufp)) != 0) { 26759 goto done; 26760 } 26761 in = (sd_prin_readresv_t *)data_bufp; 26762 ptr->generation = BE_32(in->generation); 26763 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26764 26765 /* 26766 * Return the min(listsize, listlen( keys 26767 */ 26768 #ifdef _MULTI_DATAMODEL 26769 26770 switch (ddi_model_convert_from(flag & FMODELS)) { 26771 case DDI_MODEL_ILP32: 26772 resvlist32.listlen = resvlist.listlen; 26773 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26774 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26775 "sd_persistent_reservation_in_read_resv: " 26776 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26777 rval = EFAULT; 26778 goto done; 26779 } 26780 break; 26781 26782 case DDI_MODEL_NONE: 26783 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26784 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26785 "sd_persistent_reservation_in_read_resv: " 26786 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26787 rval = EFAULT; 26788 goto done; 26789 } 26790 break; 26791 } 26792 26793 #else /* ! _MULTI_DATAMODEL */ 26794 26795 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26796 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26797 "sd_persistent_reservation_in_read_resv: " 26798 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26799 rval = EFAULT; 26800 goto done; 26801 } 26802 26803 #endif /* ! _MULTI_DATAMODEL */ 26804 26805 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26806 bufp = resvlist.list; 26807 copysz = sizeof (mhioc_resv_desc_t); 26808 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26809 i++, readresv_ptr++, bufp++) { 26810 26811 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26812 MHIOC_RESV_KEY_SIZE); 26813 resvdesc.type = readresv_ptr->type; 26814 resvdesc.scope = readresv_ptr->scope; 26815 resvdesc.scope_specific_addr = 26816 BE_32(readresv_ptr->scope_specific_addr); 26817 26818 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26819 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26820 "sd_persistent_reservation_in_read_resv: " 26821 "failed ddi_copyout: resvlist\n"); 26822 rval = EFAULT; 26823 goto done; 26824 } 26825 } 26826 done: 26827 kmem_free(data_bufp, data_len); 26828 return (rval); 26829 } 26830 26831 26832 /* 26833 * Function: sr_change_blkmode() 26834 * 26835 * Description: This routine is the driver entry point for handling CD-ROM 26836 * block mode ioctl requests. Support for returning and changing 26837 * the current block size in use by the device is implemented. The 26838 * LBA size is changed via a MODE SELECT Block Descriptor. 26839 * 26840 * This routine issues a mode sense with an allocation length of 26841 * 12 bytes for the mode page header and a single block descriptor. 26842 * 26843 * Arguments: dev - the device 'dev_t' 26844 * cmd - the request type; one of CDROMGBLKMODE (get) or 26845 * CDROMSBLKMODE (set) 26846 * data - current block size or requested block size 26847 * flag - this argument is a pass through to ddi_copyxxx() directly 26848 * from the mode argument of ioctl(). 26849 * 26850 * Return Code: the code returned by sd_send_scsi_cmd() 26851 * EINVAL if invalid arguments are provided 26852 * EFAULT if ddi_copyxxx() fails 26853 * ENXIO if fail ddi_get_soft_state 26854 * EIO if invalid mode sense block descriptor length 26855 * 26856 */ 26857 26858 static int 26859 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26860 { 26861 struct sd_lun *un = NULL; 26862 struct mode_header *sense_mhp, *select_mhp; 26863 struct block_descriptor *sense_desc, *select_desc; 26864 int current_bsize; 26865 int rval = EINVAL; 26866 uchar_t *sense = NULL; 26867 uchar_t *select = NULL; 26868 26869 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26870 26871 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26872 return (ENXIO); 26873 } 26874 26875 /* 26876 * The block length is changed via the Mode Select block descriptor, the 26877 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26878 * required as part of this routine. Therefore the mode sense allocation 26879 * length is specified to be the length of a mode page header and a 26880 * block descriptor. 26881 */ 26882 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26883 26884 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 26885 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 26886 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26887 "sr_change_blkmode: Mode Sense Failed\n"); 26888 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26889 return (rval); 26890 } 26891 26892 /* Check the block descriptor len to handle only 1 block descriptor */ 26893 sense_mhp = (struct mode_header *)sense; 26894 if ((sense_mhp->bdesc_length == 0) || 26895 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26896 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26897 "sr_change_blkmode: Mode Sense returned invalid block" 26898 " descriptor length\n"); 26899 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26900 return (EIO); 26901 } 26902 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26903 current_bsize = ((sense_desc->blksize_hi << 16) | 26904 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26905 26906 /* Process command */ 26907 switch (cmd) { 26908 case CDROMGBLKMODE: 26909 /* Return the block size obtained during the mode sense */ 26910 if (ddi_copyout(¤t_bsize, (void *)data, 26911 sizeof (int), flag) != 0) 26912 rval = EFAULT; 26913 break; 26914 case CDROMSBLKMODE: 26915 /* Validate the requested block size */ 26916 switch (data) { 26917 case CDROM_BLK_512: 26918 case CDROM_BLK_1024: 26919 case CDROM_BLK_2048: 26920 case CDROM_BLK_2056: 26921 case CDROM_BLK_2336: 26922 case CDROM_BLK_2340: 26923 case CDROM_BLK_2352: 26924 case CDROM_BLK_2368: 26925 case CDROM_BLK_2448: 26926 case CDROM_BLK_2646: 26927 case CDROM_BLK_2647: 26928 break; 26929 default: 26930 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26931 "sr_change_blkmode: " 26932 "Block Size '%ld' Not Supported\n", data); 26933 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26934 return (EINVAL); 26935 } 26936 26937 /* 26938 * The current block size matches the requested block size so 26939 * there is no need to send the mode select to change the size 26940 */ 26941 if (current_bsize == data) { 26942 break; 26943 } 26944 26945 /* Build the select data for the requested block size */ 26946 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26947 select_mhp = (struct mode_header *)select; 26948 select_desc = 26949 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26950 /* 26951 * The LBA size is changed via the block descriptor, so the 26952 * descriptor is built according to the user data 26953 */ 26954 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26955 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26956 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26957 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26958 26959 /* Send the mode select for the requested block size */ 26960 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26961 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26962 SD_PATH_STANDARD)) != 0) { 26963 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26964 "sr_change_blkmode: Mode Select Failed\n"); 26965 /* 26966 * The mode select failed for the requested block size, 26967 * so reset the data for the original block size and 26968 * send it to the target. The error is indicated by the 26969 * return value for the failed mode select. 26970 */ 26971 select_desc->blksize_hi = sense_desc->blksize_hi; 26972 select_desc->blksize_mid = sense_desc->blksize_mid; 26973 select_desc->blksize_lo = sense_desc->blksize_lo; 26974 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26975 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26976 SD_PATH_STANDARD); 26977 } else { 26978 ASSERT(!mutex_owned(SD_MUTEX(un))); 26979 mutex_enter(SD_MUTEX(un)); 26980 sd_update_block_info(un, (uint32_t)data, 0); 26981 26982 mutex_exit(SD_MUTEX(un)); 26983 } 26984 break; 26985 default: 26986 /* should not reach here, but check anyway */ 26987 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26988 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 26989 rval = EINVAL; 26990 break; 26991 } 26992 26993 if (select) { 26994 kmem_free(select, BUFLEN_CHG_BLK_MODE); 26995 } 26996 if (sense) { 26997 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26998 } 26999 return (rval); 27000 } 27001 27002 27003 /* 27004 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27005 * implement driver support for getting and setting the CD speed. The command 27006 * set used will be based on the device type. If the device has not been 27007 * identified as MMC the Toshiba vendor specific mode page will be used. If 27008 * the device is MMC but does not support the Real Time Streaming feature 27009 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27010 * be used to read the speed. 27011 */ 27012 27013 /* 27014 * Function: sr_change_speed() 27015 * 27016 * Description: This routine is the driver entry point for handling CD-ROM 27017 * drive speed ioctl requests for devices supporting the Toshiba 27018 * vendor specific drive speed mode page. Support for returning 27019 * and changing the current drive speed in use by the device is 27020 * implemented. 27021 * 27022 * Arguments: dev - the device 'dev_t' 27023 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27024 * CDROMSDRVSPEED (set) 27025 * data - current drive speed or requested drive speed 27026 * flag - this argument is a pass through to ddi_copyxxx() directly 27027 * from the mode argument of ioctl(). 27028 * 27029 * Return Code: the code returned by sd_send_scsi_cmd() 27030 * EINVAL if invalid arguments are provided 27031 * EFAULT if ddi_copyxxx() fails 27032 * ENXIO if fail ddi_get_soft_state 27033 * EIO if invalid mode sense block descriptor length 27034 */ 27035 27036 static int 27037 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27038 { 27039 struct sd_lun *un = NULL; 27040 struct mode_header *sense_mhp, *select_mhp; 27041 struct mode_speed *sense_page, *select_page; 27042 int current_speed; 27043 int rval = EINVAL; 27044 int bd_len; 27045 uchar_t *sense = NULL; 27046 uchar_t *select = NULL; 27047 27048 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27049 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27050 return (ENXIO); 27051 } 27052 27053 /* 27054 * Note: The drive speed is being modified here according to a Toshiba 27055 * vendor specific mode page (0x31). 27056 */ 27057 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27058 27059 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27060 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27061 SD_PATH_STANDARD)) != 0) { 27062 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27063 "sr_change_speed: Mode Sense Failed\n"); 27064 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27065 return (rval); 27066 } 27067 sense_mhp = (struct mode_header *)sense; 27068 27069 /* Check the block descriptor len to handle only 1 block descriptor */ 27070 bd_len = sense_mhp->bdesc_length; 27071 if (bd_len > MODE_BLK_DESC_LENGTH) { 27072 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27073 "sr_change_speed: Mode Sense returned invalid block " 27074 "descriptor length\n"); 27075 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27076 return (EIO); 27077 } 27078 27079 sense_page = (struct mode_speed *) 27080 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27081 current_speed = sense_page->speed; 27082 27083 /* Process command */ 27084 switch (cmd) { 27085 case CDROMGDRVSPEED: 27086 /* Return the drive speed obtained during the mode sense */ 27087 if (current_speed == 0x2) { 27088 current_speed = CDROM_TWELVE_SPEED; 27089 } 27090 if (ddi_copyout(¤t_speed, (void *)data, 27091 sizeof (int), flag) != 0) { 27092 rval = EFAULT; 27093 } 27094 break; 27095 case CDROMSDRVSPEED: 27096 /* Validate the requested drive speed */ 27097 switch ((uchar_t)data) { 27098 case CDROM_TWELVE_SPEED: 27099 data = 0x2; 27100 /*FALLTHROUGH*/ 27101 case CDROM_NORMAL_SPEED: 27102 case CDROM_DOUBLE_SPEED: 27103 case CDROM_QUAD_SPEED: 27104 case CDROM_MAXIMUM_SPEED: 27105 break; 27106 default: 27107 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27108 "sr_change_speed: " 27109 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27110 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27111 return (EINVAL); 27112 } 27113 27114 /* 27115 * The current drive speed matches the requested drive speed so 27116 * there is no need to send the mode select to change the speed 27117 */ 27118 if (current_speed == data) { 27119 break; 27120 } 27121 27122 /* Build the select data for the requested drive speed */ 27123 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27124 select_mhp = (struct mode_header *)select; 27125 select_mhp->bdesc_length = 0; 27126 select_page = 27127 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27128 select_page = 27129 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27130 select_page->mode_page.code = CDROM_MODE_SPEED; 27131 select_page->mode_page.length = 2; 27132 select_page->speed = (uchar_t)data; 27133 27134 /* Send the mode select for the requested block size */ 27135 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27136 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27137 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27138 /* 27139 * The mode select failed for the requested drive speed, 27140 * so reset the data for the original drive speed and 27141 * send it to the target. The error is indicated by the 27142 * return value for the failed mode select. 27143 */ 27144 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27145 "sr_drive_speed: Mode Select Failed\n"); 27146 select_page->speed = sense_page->speed; 27147 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27148 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27149 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27150 } 27151 break; 27152 default: 27153 /* should not reach here, but check anyway */ 27154 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27155 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27156 rval = EINVAL; 27157 break; 27158 } 27159 27160 if (select) { 27161 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27162 } 27163 if (sense) { 27164 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27165 } 27166 27167 return (rval); 27168 } 27169 27170 27171 /* 27172 * Function: sr_atapi_change_speed() 27173 * 27174 * Description: This routine is the driver entry point for handling CD-ROM 27175 * drive speed ioctl requests for MMC devices that do not support 27176 * the Real Time Streaming feature (0x107). 27177 * 27178 * Note: This routine will use the SET SPEED command which may not 27179 * be supported by all devices. 27180 * 27181 * Arguments: dev- the device 'dev_t' 27182 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27183 * CDROMSDRVSPEED (set) 27184 * data- current drive speed or requested drive speed 27185 * flag- this argument is a pass through to ddi_copyxxx() directly 27186 * from the mode argument of ioctl(). 27187 * 27188 * Return Code: the code returned by sd_send_scsi_cmd() 27189 * EINVAL if invalid arguments are provided 27190 * EFAULT if ddi_copyxxx() fails 27191 * ENXIO if fail ddi_get_soft_state 27192 * EIO if invalid mode sense block descriptor length 27193 */ 27194 27195 static int 27196 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27197 { 27198 struct sd_lun *un; 27199 struct uscsi_cmd *com = NULL; 27200 struct mode_header_grp2 *sense_mhp; 27201 uchar_t *sense_page; 27202 uchar_t *sense = NULL; 27203 char cdb[CDB_GROUP5]; 27204 int bd_len; 27205 int current_speed = 0; 27206 int max_speed = 0; 27207 int rval; 27208 27209 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27210 27211 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27212 return (ENXIO); 27213 } 27214 27215 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27216 27217 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27218 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27219 SD_PATH_STANDARD)) != 0) { 27220 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27221 "sr_atapi_change_speed: Mode Sense Failed\n"); 27222 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27223 return (rval); 27224 } 27225 27226 /* Check the block descriptor len to handle only 1 block descriptor */ 27227 sense_mhp = (struct mode_header_grp2 *)sense; 27228 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27229 if (bd_len > MODE_BLK_DESC_LENGTH) { 27230 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27231 "sr_atapi_change_speed: Mode Sense returned invalid " 27232 "block descriptor length\n"); 27233 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27234 return (EIO); 27235 } 27236 27237 /* Calculate the current and maximum drive speeds */ 27238 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27239 current_speed = (sense_page[14] << 8) | sense_page[15]; 27240 max_speed = (sense_page[8] << 8) | sense_page[9]; 27241 27242 /* Process the command */ 27243 switch (cmd) { 27244 case CDROMGDRVSPEED: 27245 current_speed /= SD_SPEED_1X; 27246 if (ddi_copyout(¤t_speed, (void *)data, 27247 sizeof (int), flag) != 0) 27248 rval = EFAULT; 27249 break; 27250 case CDROMSDRVSPEED: 27251 /* Convert the speed code to KB/sec */ 27252 switch ((uchar_t)data) { 27253 case CDROM_NORMAL_SPEED: 27254 current_speed = SD_SPEED_1X; 27255 break; 27256 case CDROM_DOUBLE_SPEED: 27257 current_speed = 2 * SD_SPEED_1X; 27258 break; 27259 case CDROM_QUAD_SPEED: 27260 current_speed = 4 * SD_SPEED_1X; 27261 break; 27262 case CDROM_TWELVE_SPEED: 27263 current_speed = 12 * SD_SPEED_1X; 27264 break; 27265 case CDROM_MAXIMUM_SPEED: 27266 current_speed = 0xffff; 27267 break; 27268 default: 27269 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27270 "sr_atapi_change_speed: invalid drive speed %d\n", 27271 (uchar_t)data); 27272 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27273 return (EINVAL); 27274 } 27275 27276 /* Check the request against the drive's max speed. */ 27277 if (current_speed != 0xffff) { 27278 if (current_speed > max_speed) { 27279 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27280 return (EINVAL); 27281 } 27282 } 27283 27284 /* 27285 * Build and send the SET SPEED command 27286 * 27287 * Note: The SET SPEED (0xBB) command used in this routine is 27288 * obsolete per the SCSI MMC spec but still supported in the 27289 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27290 * therefore the command is still implemented in this routine. 27291 */ 27292 bzero(cdb, sizeof (cdb)); 27293 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27294 cdb[2] = (uchar_t)(current_speed >> 8); 27295 cdb[3] = (uchar_t)current_speed; 27296 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27297 com->uscsi_cdb = (caddr_t)cdb; 27298 com->uscsi_cdblen = CDB_GROUP5; 27299 com->uscsi_bufaddr = NULL; 27300 com->uscsi_buflen = 0; 27301 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27302 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 27303 UIO_SYSSPACE, SD_PATH_STANDARD); 27304 break; 27305 default: 27306 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27307 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27308 rval = EINVAL; 27309 } 27310 27311 if (sense) { 27312 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27313 } 27314 if (com) { 27315 kmem_free(com, sizeof (*com)); 27316 } 27317 return (rval); 27318 } 27319 27320 27321 /* 27322 * Function: sr_pause_resume() 27323 * 27324 * Description: This routine is the driver entry point for handling CD-ROM 27325 * pause/resume ioctl requests. This only affects the audio play 27326 * operation. 27327 * 27328 * Arguments: dev - the device 'dev_t' 27329 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27330 * for setting the resume bit of the cdb. 27331 * 27332 * Return Code: the code returned by sd_send_scsi_cmd() 27333 * EINVAL if invalid mode specified 27334 * 27335 */ 27336 27337 static int 27338 sr_pause_resume(dev_t dev, int cmd) 27339 { 27340 struct sd_lun *un; 27341 struct uscsi_cmd *com; 27342 char cdb[CDB_GROUP1]; 27343 int rval; 27344 27345 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27346 return (ENXIO); 27347 } 27348 27349 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27350 bzero(cdb, CDB_GROUP1); 27351 cdb[0] = SCMD_PAUSE_RESUME; 27352 switch (cmd) { 27353 case CDROMRESUME: 27354 cdb[8] = 1; 27355 break; 27356 case CDROMPAUSE: 27357 cdb[8] = 0; 27358 break; 27359 default: 27360 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27361 " Command '%x' Not Supported\n", cmd); 27362 rval = EINVAL; 27363 goto done; 27364 } 27365 27366 com->uscsi_cdb = cdb; 27367 com->uscsi_cdblen = CDB_GROUP1; 27368 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27369 27370 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27371 UIO_SYSSPACE, SD_PATH_STANDARD); 27372 27373 done: 27374 kmem_free(com, sizeof (*com)); 27375 return (rval); 27376 } 27377 27378 27379 /* 27380 * Function: sr_play_msf() 27381 * 27382 * Description: This routine is the driver entry point for handling CD-ROM 27383 * ioctl requests to output the audio signals at the specified 27384 * starting address and continue the audio play until the specified 27385 * ending address (CDROMPLAYMSF) The address is in Minute Second 27386 * Frame (MSF) format. 27387 * 27388 * Arguments: dev - the device 'dev_t' 27389 * data - pointer to user provided audio msf structure, 27390 * specifying start/end addresses. 27391 * flag - this argument is a pass through to ddi_copyxxx() 27392 * directly from the mode argument of ioctl(). 27393 * 27394 * Return Code: the code returned by sd_send_scsi_cmd() 27395 * EFAULT if ddi_copyxxx() fails 27396 * ENXIO if fail ddi_get_soft_state 27397 * EINVAL if data pointer is NULL 27398 */ 27399 27400 static int 27401 sr_play_msf(dev_t dev, caddr_t data, int flag) 27402 { 27403 struct sd_lun *un; 27404 struct uscsi_cmd *com; 27405 struct cdrom_msf msf_struct; 27406 struct cdrom_msf *msf = &msf_struct; 27407 char cdb[CDB_GROUP1]; 27408 int rval; 27409 27410 if (data == NULL) { 27411 return (EINVAL); 27412 } 27413 27414 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27415 return (ENXIO); 27416 } 27417 27418 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27419 return (EFAULT); 27420 } 27421 27422 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27423 bzero(cdb, CDB_GROUP1); 27424 cdb[0] = SCMD_PLAYAUDIO_MSF; 27425 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27426 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27427 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27428 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27429 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27430 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27431 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27432 } else { 27433 cdb[3] = msf->cdmsf_min0; 27434 cdb[4] = msf->cdmsf_sec0; 27435 cdb[5] = msf->cdmsf_frame0; 27436 cdb[6] = msf->cdmsf_min1; 27437 cdb[7] = msf->cdmsf_sec1; 27438 cdb[8] = msf->cdmsf_frame1; 27439 } 27440 com->uscsi_cdb = cdb; 27441 com->uscsi_cdblen = CDB_GROUP1; 27442 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27443 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27444 UIO_SYSSPACE, SD_PATH_STANDARD); 27445 kmem_free(com, sizeof (*com)); 27446 return (rval); 27447 } 27448 27449 27450 /* 27451 * Function: sr_play_trkind() 27452 * 27453 * Description: This routine is the driver entry point for handling CD-ROM 27454 * ioctl requests to output the audio signals at the specified 27455 * starting address and continue the audio play until the specified 27456 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27457 * format. 27458 * 27459 * Arguments: dev - the device 'dev_t' 27460 * data - pointer to user provided audio track/index structure, 27461 * specifying start/end addresses. 27462 * flag - this argument is a pass through to ddi_copyxxx() 27463 * directly from the mode argument of ioctl(). 27464 * 27465 * Return Code: the code returned by sd_send_scsi_cmd() 27466 * EFAULT if ddi_copyxxx() fails 27467 * ENXIO if fail ddi_get_soft_state 27468 * EINVAL if data pointer is NULL 27469 */ 27470 27471 static int 27472 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27473 { 27474 struct cdrom_ti ti_struct; 27475 struct cdrom_ti *ti = &ti_struct; 27476 struct uscsi_cmd *com = NULL; 27477 char cdb[CDB_GROUP1]; 27478 int rval; 27479 27480 if (data == NULL) { 27481 return (EINVAL); 27482 } 27483 27484 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27485 return (EFAULT); 27486 } 27487 27488 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27489 bzero(cdb, CDB_GROUP1); 27490 cdb[0] = SCMD_PLAYAUDIO_TI; 27491 cdb[4] = ti->cdti_trk0; 27492 cdb[5] = ti->cdti_ind0; 27493 cdb[7] = ti->cdti_trk1; 27494 cdb[8] = ti->cdti_ind1; 27495 com->uscsi_cdb = cdb; 27496 com->uscsi_cdblen = CDB_GROUP1; 27497 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27498 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27499 UIO_SYSSPACE, SD_PATH_STANDARD); 27500 kmem_free(com, sizeof (*com)); 27501 return (rval); 27502 } 27503 27504 27505 /* 27506 * Function: sr_read_all_subcodes() 27507 * 27508 * Description: This routine is the driver entry point for handling CD-ROM 27509 * ioctl requests to return raw subcode data while the target is 27510 * playing audio (CDROMSUBCODE). 27511 * 27512 * Arguments: dev - the device 'dev_t' 27513 * data - pointer to user provided cdrom subcode structure, 27514 * specifying the transfer length and address. 27515 * flag - this argument is a pass through to ddi_copyxxx() 27516 * directly from the mode argument of ioctl(). 27517 * 27518 * Return Code: the code returned by sd_send_scsi_cmd() 27519 * EFAULT if ddi_copyxxx() fails 27520 * ENXIO if fail ddi_get_soft_state 27521 * EINVAL if data pointer is NULL 27522 */ 27523 27524 static int 27525 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27526 { 27527 struct sd_lun *un = NULL; 27528 struct uscsi_cmd *com = NULL; 27529 struct cdrom_subcode *subcode = NULL; 27530 int rval; 27531 size_t buflen; 27532 char cdb[CDB_GROUP5]; 27533 27534 #ifdef _MULTI_DATAMODEL 27535 /* To support ILP32 applications in an LP64 world */ 27536 struct cdrom_subcode32 cdrom_subcode32; 27537 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27538 #endif 27539 if (data == NULL) { 27540 return (EINVAL); 27541 } 27542 27543 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27544 return (ENXIO); 27545 } 27546 27547 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27548 27549 #ifdef _MULTI_DATAMODEL 27550 switch (ddi_model_convert_from(flag & FMODELS)) { 27551 case DDI_MODEL_ILP32: 27552 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27553 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27554 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27555 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27556 return (EFAULT); 27557 } 27558 /* Convert the ILP32 uscsi data from the application to LP64 */ 27559 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27560 break; 27561 case DDI_MODEL_NONE: 27562 if (ddi_copyin(data, subcode, 27563 sizeof (struct cdrom_subcode), flag)) { 27564 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27565 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27566 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27567 return (EFAULT); 27568 } 27569 break; 27570 } 27571 #else /* ! _MULTI_DATAMODEL */ 27572 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27573 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27574 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27575 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27576 return (EFAULT); 27577 } 27578 #endif /* _MULTI_DATAMODEL */ 27579 27580 /* 27581 * Since MMC-2 expects max 3 bytes for length, check if the 27582 * length input is greater than 3 bytes 27583 */ 27584 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27585 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27586 "sr_read_all_subcodes: " 27587 "cdrom transfer length too large: %d (limit %d)\n", 27588 subcode->cdsc_length, 0xFFFFFF); 27589 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27590 return (EINVAL); 27591 } 27592 27593 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27594 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27595 bzero(cdb, CDB_GROUP5); 27596 27597 if (un->un_f_mmc_cap == TRUE) { 27598 cdb[0] = (char)SCMD_READ_CD; 27599 cdb[2] = (char)0xff; 27600 cdb[3] = (char)0xff; 27601 cdb[4] = (char)0xff; 27602 cdb[5] = (char)0xff; 27603 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27604 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27605 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27606 cdb[10] = 1; 27607 } else { 27608 /* 27609 * Note: A vendor specific command (0xDF) is being used her to 27610 * request a read of all subcodes. 27611 */ 27612 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27613 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27614 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27615 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27616 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27617 } 27618 com->uscsi_cdb = cdb; 27619 com->uscsi_cdblen = CDB_GROUP5; 27620 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27621 com->uscsi_buflen = buflen; 27622 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27623 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27624 UIO_SYSSPACE, SD_PATH_STANDARD); 27625 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27626 kmem_free(com, sizeof (*com)); 27627 return (rval); 27628 } 27629 27630 27631 /* 27632 * Function: sr_read_subchannel() 27633 * 27634 * Description: This routine is the driver entry point for handling CD-ROM 27635 * ioctl requests to return the Q sub-channel data of the CD 27636 * current position block. (CDROMSUBCHNL) The data includes the 27637 * track number, index number, absolute CD-ROM address (LBA or MSF 27638 * format per the user) , track relative CD-ROM address (LBA or MSF 27639 * format per the user), control data and audio status. 27640 * 27641 * Arguments: dev - the device 'dev_t' 27642 * data - pointer to user provided cdrom sub-channel structure 27643 * flag - this argument is a pass through to ddi_copyxxx() 27644 * directly from the mode argument of ioctl(). 27645 * 27646 * Return Code: the code returned by sd_send_scsi_cmd() 27647 * EFAULT if ddi_copyxxx() fails 27648 * ENXIO if fail ddi_get_soft_state 27649 * EINVAL if data pointer is NULL 27650 */ 27651 27652 static int 27653 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27654 { 27655 struct sd_lun *un; 27656 struct uscsi_cmd *com; 27657 struct cdrom_subchnl subchanel; 27658 struct cdrom_subchnl *subchnl = &subchanel; 27659 char cdb[CDB_GROUP1]; 27660 caddr_t buffer; 27661 int rval; 27662 27663 if (data == NULL) { 27664 return (EINVAL); 27665 } 27666 27667 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27668 (un->un_state == SD_STATE_OFFLINE)) { 27669 return (ENXIO); 27670 } 27671 27672 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27673 return (EFAULT); 27674 } 27675 27676 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27677 bzero(cdb, CDB_GROUP1); 27678 cdb[0] = SCMD_READ_SUBCHANNEL; 27679 /* Set the MSF bit based on the user requested address format */ 27680 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27681 /* 27682 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27683 * returned 27684 */ 27685 cdb[2] = 0x40; 27686 /* 27687 * Set byte 3 to specify the return data format. A value of 0x01 27688 * indicates that the CD-ROM current position should be returned. 27689 */ 27690 cdb[3] = 0x01; 27691 cdb[8] = 0x10; 27692 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27693 com->uscsi_cdb = cdb; 27694 com->uscsi_cdblen = CDB_GROUP1; 27695 com->uscsi_bufaddr = buffer; 27696 com->uscsi_buflen = 16; 27697 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27698 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27699 UIO_SYSSPACE, SD_PATH_STANDARD); 27700 if (rval != 0) { 27701 kmem_free(buffer, 16); 27702 kmem_free(com, sizeof (*com)); 27703 return (rval); 27704 } 27705 27706 /* Process the returned Q sub-channel data */ 27707 subchnl->cdsc_audiostatus = buffer[1]; 27708 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27709 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27710 subchnl->cdsc_trk = buffer[6]; 27711 subchnl->cdsc_ind = buffer[7]; 27712 if (subchnl->cdsc_format & CDROM_LBA) { 27713 subchnl->cdsc_absaddr.lba = 27714 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27715 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27716 subchnl->cdsc_reladdr.lba = 27717 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27718 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27719 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27720 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27721 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27722 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27723 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27724 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27725 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27726 } else { 27727 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27728 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27729 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27730 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27731 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27732 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27733 } 27734 kmem_free(buffer, 16); 27735 kmem_free(com, sizeof (*com)); 27736 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27737 != 0) { 27738 return (EFAULT); 27739 } 27740 return (rval); 27741 } 27742 27743 27744 /* 27745 * Function: sr_read_tocentry() 27746 * 27747 * Description: This routine is the driver entry point for handling CD-ROM 27748 * ioctl requests to read from the Table of Contents (TOC) 27749 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27750 * fields, the starting address (LBA or MSF format per the user) 27751 * and the data mode if the user specified track is a data track. 27752 * 27753 * Note: The READ HEADER (0x44) command used in this routine is 27754 * obsolete per the SCSI MMC spec but still supported in the 27755 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27756 * therefore the command is still implemented in this routine. 27757 * 27758 * Arguments: dev - the device 'dev_t' 27759 * data - pointer to user provided toc entry structure, 27760 * specifying the track # and the address format 27761 * (LBA or MSF). 27762 * flag - this argument is a pass through to ddi_copyxxx() 27763 * directly from the mode argument of ioctl(). 27764 * 27765 * Return Code: the code returned by sd_send_scsi_cmd() 27766 * EFAULT if ddi_copyxxx() fails 27767 * ENXIO if fail ddi_get_soft_state 27768 * EINVAL if data pointer is NULL 27769 */ 27770 27771 static int 27772 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27773 { 27774 struct sd_lun *un = NULL; 27775 struct uscsi_cmd *com; 27776 struct cdrom_tocentry toc_entry; 27777 struct cdrom_tocentry *entry = &toc_entry; 27778 caddr_t buffer; 27779 int rval; 27780 char cdb[CDB_GROUP1]; 27781 27782 if (data == NULL) { 27783 return (EINVAL); 27784 } 27785 27786 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27787 (un->un_state == SD_STATE_OFFLINE)) { 27788 return (ENXIO); 27789 } 27790 27791 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27792 return (EFAULT); 27793 } 27794 27795 /* Validate the requested track and address format */ 27796 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27797 return (EINVAL); 27798 } 27799 27800 if (entry->cdte_track == 0) { 27801 return (EINVAL); 27802 } 27803 27804 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27805 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27806 bzero(cdb, CDB_GROUP1); 27807 27808 cdb[0] = SCMD_READ_TOC; 27809 /* Set the MSF bit based on the user requested address format */ 27810 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27811 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27812 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27813 } else { 27814 cdb[6] = entry->cdte_track; 27815 } 27816 27817 /* 27818 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27819 * (4 byte TOC response header + 8 byte track descriptor) 27820 */ 27821 cdb[8] = 12; 27822 com->uscsi_cdb = cdb; 27823 com->uscsi_cdblen = CDB_GROUP1; 27824 com->uscsi_bufaddr = buffer; 27825 com->uscsi_buflen = 0x0C; 27826 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27827 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27828 UIO_SYSSPACE, SD_PATH_STANDARD); 27829 if (rval != 0) { 27830 kmem_free(buffer, 12); 27831 kmem_free(com, sizeof (*com)); 27832 return (rval); 27833 } 27834 27835 /* Process the toc entry */ 27836 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27837 entry->cdte_ctrl = (buffer[5] & 0x0F); 27838 if (entry->cdte_format & CDROM_LBA) { 27839 entry->cdte_addr.lba = 27840 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27841 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27842 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27843 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27844 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27845 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27846 /* 27847 * Send a READ TOC command using the LBA address format to get 27848 * the LBA for the track requested so it can be used in the 27849 * READ HEADER request 27850 * 27851 * Note: The MSF bit of the READ HEADER command specifies the 27852 * output format. The block address specified in that command 27853 * must be in LBA format. 27854 */ 27855 cdb[1] = 0; 27856 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27857 UIO_SYSSPACE, SD_PATH_STANDARD); 27858 if (rval != 0) { 27859 kmem_free(buffer, 12); 27860 kmem_free(com, sizeof (*com)); 27861 return (rval); 27862 } 27863 } else { 27864 entry->cdte_addr.msf.minute = buffer[9]; 27865 entry->cdte_addr.msf.second = buffer[10]; 27866 entry->cdte_addr.msf.frame = buffer[11]; 27867 /* 27868 * Send a READ TOC command using the LBA address format to get 27869 * the LBA for the track requested so it can be used in the 27870 * READ HEADER request 27871 * 27872 * Note: The MSF bit of the READ HEADER command specifies the 27873 * output format. The block address specified in that command 27874 * must be in LBA format. 27875 */ 27876 cdb[1] = 0; 27877 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27878 UIO_SYSSPACE, SD_PATH_STANDARD); 27879 if (rval != 0) { 27880 kmem_free(buffer, 12); 27881 kmem_free(com, sizeof (*com)); 27882 return (rval); 27883 } 27884 } 27885 27886 /* 27887 * Build and send the READ HEADER command to determine the data mode of 27888 * the user specified track. 27889 */ 27890 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27891 (entry->cdte_track != CDROM_LEADOUT)) { 27892 bzero(cdb, CDB_GROUP1); 27893 cdb[0] = SCMD_READ_HEADER; 27894 cdb[2] = buffer[8]; 27895 cdb[3] = buffer[9]; 27896 cdb[4] = buffer[10]; 27897 cdb[5] = buffer[11]; 27898 cdb[8] = 0x08; 27899 com->uscsi_buflen = 0x08; 27900 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27901 UIO_SYSSPACE, SD_PATH_STANDARD); 27902 if (rval == 0) { 27903 entry->cdte_datamode = buffer[0]; 27904 } else { 27905 /* 27906 * READ HEADER command failed, since this is 27907 * obsoleted in one spec, its better to return 27908 * -1 for an invlid track so that we can still 27909 * recieve the rest of the TOC data. 27910 */ 27911 entry->cdte_datamode = (uchar_t)-1; 27912 } 27913 } else { 27914 entry->cdte_datamode = (uchar_t)-1; 27915 } 27916 27917 kmem_free(buffer, 12); 27918 kmem_free(com, sizeof (*com)); 27919 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27920 return (EFAULT); 27921 27922 return (rval); 27923 } 27924 27925 27926 /* 27927 * Function: sr_read_tochdr() 27928 * 27929 * Description: This routine is the driver entry point for handling CD-ROM 27930 * ioctl requests to read the Table of Contents (TOC) header 27931 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27932 * and ending track numbers 27933 * 27934 * Arguments: dev - the device 'dev_t' 27935 * data - pointer to user provided toc header structure, 27936 * specifying the starting and ending track numbers. 27937 * flag - this argument is a pass through to ddi_copyxxx() 27938 * directly from the mode argument of ioctl(). 27939 * 27940 * Return Code: the code returned by sd_send_scsi_cmd() 27941 * EFAULT if ddi_copyxxx() fails 27942 * ENXIO if fail ddi_get_soft_state 27943 * EINVAL if data pointer is NULL 27944 */ 27945 27946 static int 27947 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27948 { 27949 struct sd_lun *un; 27950 struct uscsi_cmd *com; 27951 struct cdrom_tochdr toc_header; 27952 struct cdrom_tochdr *hdr = &toc_header; 27953 char cdb[CDB_GROUP1]; 27954 int rval; 27955 caddr_t buffer; 27956 27957 if (data == NULL) { 27958 return (EINVAL); 27959 } 27960 27961 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27962 (un->un_state == SD_STATE_OFFLINE)) { 27963 return (ENXIO); 27964 } 27965 27966 buffer = kmem_zalloc(4, KM_SLEEP); 27967 bzero(cdb, CDB_GROUP1); 27968 cdb[0] = SCMD_READ_TOC; 27969 /* 27970 * Specifying a track number of 0x00 in the READ TOC command indicates 27971 * that the TOC header should be returned 27972 */ 27973 cdb[6] = 0x00; 27974 /* 27975 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 27976 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 27977 */ 27978 cdb[8] = 0x04; 27979 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27980 com->uscsi_cdb = cdb; 27981 com->uscsi_cdblen = CDB_GROUP1; 27982 com->uscsi_bufaddr = buffer; 27983 com->uscsi_buflen = 0x04; 27984 com->uscsi_timeout = 300; 27985 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27986 27987 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27988 UIO_SYSSPACE, SD_PATH_STANDARD); 27989 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27990 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 27991 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 27992 } else { 27993 hdr->cdth_trk0 = buffer[2]; 27994 hdr->cdth_trk1 = buffer[3]; 27995 } 27996 kmem_free(buffer, 4); 27997 kmem_free(com, sizeof (*com)); 27998 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 27999 return (EFAULT); 28000 } 28001 return (rval); 28002 } 28003 28004 28005 /* 28006 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28007 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28008 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28009 * digital audio and extended architecture digital audio. These modes are 28010 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28011 * MMC specs. 28012 * 28013 * In addition to support for the various data formats these routines also 28014 * include support for devices that implement only the direct access READ 28015 * commands (0x08, 0x28), devices that implement the READ_CD commands 28016 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28017 * READ CDXA commands (0xD8, 0xDB) 28018 */ 28019 28020 /* 28021 * Function: sr_read_mode1() 28022 * 28023 * Description: This routine is the driver entry point for handling CD-ROM 28024 * ioctl read mode1 requests (CDROMREADMODE1). 28025 * 28026 * Arguments: dev - the device 'dev_t' 28027 * data - pointer to user provided cd read structure specifying 28028 * the lba buffer address and length. 28029 * flag - this argument is a pass through to ddi_copyxxx() 28030 * directly from the mode argument of ioctl(). 28031 * 28032 * Return Code: the code returned by sd_send_scsi_cmd() 28033 * EFAULT if ddi_copyxxx() fails 28034 * ENXIO if fail ddi_get_soft_state 28035 * EINVAL if data pointer is NULL 28036 */ 28037 28038 static int 28039 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28040 { 28041 struct sd_lun *un; 28042 struct cdrom_read mode1_struct; 28043 struct cdrom_read *mode1 = &mode1_struct; 28044 int rval; 28045 #ifdef _MULTI_DATAMODEL 28046 /* To support ILP32 applications in an LP64 world */ 28047 struct cdrom_read32 cdrom_read32; 28048 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28049 #endif /* _MULTI_DATAMODEL */ 28050 28051 if (data == NULL) { 28052 return (EINVAL); 28053 } 28054 28055 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28056 (un->un_state == SD_STATE_OFFLINE)) { 28057 return (ENXIO); 28058 } 28059 28060 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28061 "sd_read_mode1: entry: un:0x%p\n", un); 28062 28063 #ifdef _MULTI_DATAMODEL 28064 switch (ddi_model_convert_from(flag & FMODELS)) { 28065 case DDI_MODEL_ILP32: 28066 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28067 return (EFAULT); 28068 } 28069 /* Convert the ILP32 uscsi data from the application to LP64 */ 28070 cdrom_read32tocdrom_read(cdrd32, mode1); 28071 break; 28072 case DDI_MODEL_NONE: 28073 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28074 return (EFAULT); 28075 } 28076 } 28077 #else /* ! _MULTI_DATAMODEL */ 28078 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28079 return (EFAULT); 28080 } 28081 #endif /* _MULTI_DATAMODEL */ 28082 28083 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28084 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28085 28086 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28087 "sd_read_mode1: exit: un:0x%p\n", un); 28088 28089 return (rval); 28090 } 28091 28092 28093 /* 28094 * Function: sr_read_cd_mode2() 28095 * 28096 * Description: This routine is the driver entry point for handling CD-ROM 28097 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28098 * support the READ CD (0xBE) command or the 1st generation 28099 * READ CD (0xD4) command. 28100 * 28101 * Arguments: dev - the device 'dev_t' 28102 * data - pointer to user provided cd read structure specifying 28103 * the lba buffer address and length. 28104 * flag - this argument is a pass through to ddi_copyxxx() 28105 * directly from the mode argument of ioctl(). 28106 * 28107 * Return Code: the code returned by sd_send_scsi_cmd() 28108 * EFAULT if ddi_copyxxx() fails 28109 * ENXIO if fail ddi_get_soft_state 28110 * EINVAL if data pointer is NULL 28111 */ 28112 28113 static int 28114 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28115 { 28116 struct sd_lun *un; 28117 struct uscsi_cmd *com; 28118 struct cdrom_read mode2_struct; 28119 struct cdrom_read *mode2 = &mode2_struct; 28120 uchar_t cdb[CDB_GROUP5]; 28121 int nblocks; 28122 int rval; 28123 #ifdef _MULTI_DATAMODEL 28124 /* To support ILP32 applications in an LP64 world */ 28125 struct cdrom_read32 cdrom_read32; 28126 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28127 #endif /* _MULTI_DATAMODEL */ 28128 28129 if (data == NULL) { 28130 return (EINVAL); 28131 } 28132 28133 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28134 (un->un_state == SD_STATE_OFFLINE)) { 28135 return (ENXIO); 28136 } 28137 28138 #ifdef _MULTI_DATAMODEL 28139 switch (ddi_model_convert_from(flag & FMODELS)) { 28140 case DDI_MODEL_ILP32: 28141 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28142 return (EFAULT); 28143 } 28144 /* Convert the ILP32 uscsi data from the application to LP64 */ 28145 cdrom_read32tocdrom_read(cdrd32, mode2); 28146 break; 28147 case DDI_MODEL_NONE: 28148 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28149 return (EFAULT); 28150 } 28151 break; 28152 } 28153 28154 #else /* ! _MULTI_DATAMODEL */ 28155 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28156 return (EFAULT); 28157 } 28158 #endif /* _MULTI_DATAMODEL */ 28159 28160 bzero(cdb, sizeof (cdb)); 28161 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28162 /* Read command supported by 1st generation atapi drives */ 28163 cdb[0] = SCMD_READ_CDD4; 28164 } else { 28165 /* Universal CD Access Command */ 28166 cdb[0] = SCMD_READ_CD; 28167 } 28168 28169 /* 28170 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28171 */ 28172 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28173 28174 /* set the start address */ 28175 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28176 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28177 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28178 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28179 28180 /* set the transfer length */ 28181 nblocks = mode2->cdread_buflen / 2336; 28182 cdb[6] = (uchar_t)(nblocks >> 16); 28183 cdb[7] = (uchar_t)(nblocks >> 8); 28184 cdb[8] = (uchar_t)nblocks; 28185 28186 /* set the filter bits */ 28187 cdb[9] = CDROM_READ_CD_USERDATA; 28188 28189 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28190 com->uscsi_cdb = (caddr_t)cdb; 28191 com->uscsi_cdblen = sizeof (cdb); 28192 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28193 com->uscsi_buflen = mode2->cdread_buflen; 28194 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28195 28196 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28197 UIO_SYSSPACE, SD_PATH_STANDARD); 28198 kmem_free(com, sizeof (*com)); 28199 return (rval); 28200 } 28201 28202 28203 /* 28204 * Function: sr_read_mode2() 28205 * 28206 * Description: This routine is the driver entry point for handling CD-ROM 28207 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28208 * do not support the READ CD (0xBE) command. 28209 * 28210 * Arguments: dev - the device 'dev_t' 28211 * data - pointer to user provided cd read structure specifying 28212 * the lba buffer address and length. 28213 * flag - this argument is a pass through to ddi_copyxxx() 28214 * directly from the mode argument of ioctl(). 28215 * 28216 * Return Code: the code returned by sd_send_scsi_cmd() 28217 * EFAULT if ddi_copyxxx() fails 28218 * ENXIO if fail ddi_get_soft_state 28219 * EINVAL if data pointer is NULL 28220 * EIO if fail to reset block size 28221 * EAGAIN if commands are in progress in the driver 28222 */ 28223 28224 static int 28225 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28226 { 28227 struct sd_lun *un; 28228 struct cdrom_read mode2_struct; 28229 struct cdrom_read *mode2 = &mode2_struct; 28230 int rval; 28231 uint32_t restore_blksize; 28232 struct uscsi_cmd *com; 28233 uchar_t cdb[CDB_GROUP0]; 28234 int nblocks; 28235 28236 #ifdef _MULTI_DATAMODEL 28237 /* To support ILP32 applications in an LP64 world */ 28238 struct cdrom_read32 cdrom_read32; 28239 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28240 #endif /* _MULTI_DATAMODEL */ 28241 28242 if (data == NULL) { 28243 return (EINVAL); 28244 } 28245 28246 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28247 (un->un_state == SD_STATE_OFFLINE)) { 28248 return (ENXIO); 28249 } 28250 28251 /* 28252 * Because this routine will update the device and driver block size 28253 * being used we want to make sure there are no commands in progress. 28254 * If commands are in progress the user will have to try again. 28255 * 28256 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28257 * in sdioctl to protect commands from sdioctl through to the top of 28258 * sd_uscsi_strategy. See sdioctl for details. 28259 */ 28260 mutex_enter(SD_MUTEX(un)); 28261 if (un->un_ncmds_in_driver != 1) { 28262 mutex_exit(SD_MUTEX(un)); 28263 return (EAGAIN); 28264 } 28265 mutex_exit(SD_MUTEX(un)); 28266 28267 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28268 "sd_read_mode2: entry: un:0x%p\n", un); 28269 28270 #ifdef _MULTI_DATAMODEL 28271 switch (ddi_model_convert_from(flag & FMODELS)) { 28272 case DDI_MODEL_ILP32: 28273 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28274 return (EFAULT); 28275 } 28276 /* Convert the ILP32 uscsi data from the application to LP64 */ 28277 cdrom_read32tocdrom_read(cdrd32, mode2); 28278 break; 28279 case DDI_MODEL_NONE: 28280 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28281 return (EFAULT); 28282 } 28283 break; 28284 } 28285 #else /* ! _MULTI_DATAMODEL */ 28286 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28287 return (EFAULT); 28288 } 28289 #endif /* _MULTI_DATAMODEL */ 28290 28291 /* Store the current target block size for restoration later */ 28292 restore_blksize = un->un_tgt_blocksize; 28293 28294 /* Change the device and soft state target block size to 2336 */ 28295 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28296 rval = EIO; 28297 goto done; 28298 } 28299 28300 28301 bzero(cdb, sizeof (cdb)); 28302 28303 /* set READ operation */ 28304 cdb[0] = SCMD_READ; 28305 28306 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28307 mode2->cdread_lba >>= 2; 28308 28309 /* set the start address */ 28310 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28311 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28312 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28313 28314 /* set the transfer length */ 28315 nblocks = mode2->cdread_buflen / 2336; 28316 cdb[4] = (uchar_t)nblocks & 0xFF; 28317 28318 /* build command */ 28319 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28320 com->uscsi_cdb = (caddr_t)cdb; 28321 com->uscsi_cdblen = sizeof (cdb); 28322 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28323 com->uscsi_buflen = mode2->cdread_buflen; 28324 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28325 28326 /* 28327 * Issue SCSI command with user space address for read buffer. 28328 * 28329 * This sends the command through main channel in the driver. 28330 * 28331 * Since this is accessed via an IOCTL call, we go through the 28332 * standard path, so that if the device was powered down, then 28333 * it would be 'awakened' to handle the command. 28334 */ 28335 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28336 UIO_SYSSPACE, SD_PATH_STANDARD); 28337 28338 kmem_free(com, sizeof (*com)); 28339 28340 /* Restore the device and soft state target block size */ 28341 if (sr_sector_mode(dev, restore_blksize) != 0) { 28342 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28343 "can't do switch back to mode 1\n"); 28344 /* 28345 * If sd_send_scsi_READ succeeded we still need to report 28346 * an error because we failed to reset the block size 28347 */ 28348 if (rval == 0) { 28349 rval = EIO; 28350 } 28351 } 28352 28353 done: 28354 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28355 "sd_read_mode2: exit: un:0x%p\n", un); 28356 28357 return (rval); 28358 } 28359 28360 28361 /* 28362 * Function: sr_sector_mode() 28363 * 28364 * Description: This utility function is used by sr_read_mode2 to set the target 28365 * block size based on the user specified size. This is a legacy 28366 * implementation based upon a vendor specific mode page 28367 * 28368 * Arguments: dev - the device 'dev_t' 28369 * data - flag indicating if block size is being set to 2336 or 28370 * 512. 28371 * 28372 * Return Code: the code returned by sd_send_scsi_cmd() 28373 * EFAULT if ddi_copyxxx() fails 28374 * ENXIO if fail ddi_get_soft_state 28375 * EINVAL if data pointer is NULL 28376 */ 28377 28378 static int 28379 sr_sector_mode(dev_t dev, uint32_t blksize) 28380 { 28381 struct sd_lun *un; 28382 uchar_t *sense; 28383 uchar_t *select; 28384 int rval; 28385 28386 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28387 (un->un_state == SD_STATE_OFFLINE)) { 28388 return (ENXIO); 28389 } 28390 28391 sense = kmem_zalloc(20, KM_SLEEP); 28392 28393 /* Note: This is a vendor specific mode page (0x81) */ 28394 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28395 SD_PATH_STANDARD)) != 0) { 28396 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28397 "sr_sector_mode: Mode Sense failed\n"); 28398 kmem_free(sense, 20); 28399 return (rval); 28400 } 28401 select = kmem_zalloc(20, KM_SLEEP); 28402 select[3] = 0x08; 28403 select[10] = ((blksize >> 8) & 0xff); 28404 select[11] = (blksize & 0xff); 28405 select[12] = 0x01; 28406 select[13] = 0x06; 28407 select[14] = sense[14]; 28408 select[15] = sense[15]; 28409 if (blksize == SD_MODE2_BLKSIZE) { 28410 select[14] |= 0x01; 28411 } 28412 28413 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28414 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28415 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28416 "sr_sector_mode: Mode Select failed\n"); 28417 } else { 28418 /* 28419 * Only update the softstate block size if we successfully 28420 * changed the device block mode. 28421 */ 28422 mutex_enter(SD_MUTEX(un)); 28423 sd_update_block_info(un, blksize, 0); 28424 mutex_exit(SD_MUTEX(un)); 28425 } 28426 kmem_free(sense, 20); 28427 kmem_free(select, 20); 28428 return (rval); 28429 } 28430 28431 28432 /* 28433 * Function: sr_read_cdda() 28434 * 28435 * Description: This routine is the driver entry point for handling CD-ROM 28436 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28437 * the target supports CDDA these requests are handled via a vendor 28438 * specific command (0xD8) If the target does not support CDDA 28439 * these requests are handled via the READ CD command (0xBE). 28440 * 28441 * Arguments: dev - the device 'dev_t' 28442 * data - pointer to user provided CD-DA structure specifying 28443 * the track starting address, transfer length, and 28444 * subcode options. 28445 * flag - this argument is a pass through to ddi_copyxxx() 28446 * directly from the mode argument of ioctl(). 28447 * 28448 * Return Code: the code returned by sd_send_scsi_cmd() 28449 * EFAULT if ddi_copyxxx() fails 28450 * ENXIO if fail ddi_get_soft_state 28451 * EINVAL if invalid arguments are provided 28452 * ENOTTY 28453 */ 28454 28455 static int 28456 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28457 { 28458 struct sd_lun *un; 28459 struct uscsi_cmd *com; 28460 struct cdrom_cdda *cdda; 28461 int rval; 28462 size_t buflen; 28463 char cdb[CDB_GROUP5]; 28464 28465 #ifdef _MULTI_DATAMODEL 28466 /* To support ILP32 applications in an LP64 world */ 28467 struct cdrom_cdda32 cdrom_cdda32; 28468 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 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 return (ENXIO); 28477 } 28478 28479 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28480 28481 #ifdef _MULTI_DATAMODEL 28482 switch (ddi_model_convert_from(flag & FMODELS)) { 28483 case DDI_MODEL_ILP32: 28484 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28485 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28486 "sr_read_cdda: ddi_copyin Failed\n"); 28487 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28488 return (EFAULT); 28489 } 28490 /* Convert the ILP32 uscsi data from the application to LP64 */ 28491 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28492 break; 28493 case DDI_MODEL_NONE: 28494 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28495 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28496 "sr_read_cdda: ddi_copyin Failed\n"); 28497 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28498 return (EFAULT); 28499 } 28500 break; 28501 } 28502 #else /* ! _MULTI_DATAMODEL */ 28503 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28504 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28505 "sr_read_cdda: ddi_copyin Failed\n"); 28506 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28507 return (EFAULT); 28508 } 28509 #endif /* _MULTI_DATAMODEL */ 28510 28511 /* 28512 * Since MMC-2 expects max 3 bytes for length, check if the 28513 * length input is greater than 3 bytes 28514 */ 28515 if ((cdda->cdda_length & 0xFF000000) != 0) { 28516 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28517 "cdrom transfer length too large: %d (limit %d)\n", 28518 cdda->cdda_length, 0xFFFFFF); 28519 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28520 return (EINVAL); 28521 } 28522 28523 switch (cdda->cdda_subcode) { 28524 case CDROM_DA_NO_SUBCODE: 28525 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28526 break; 28527 case CDROM_DA_SUBQ: 28528 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28529 break; 28530 case CDROM_DA_ALL_SUBCODE: 28531 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28532 break; 28533 case CDROM_DA_SUBCODE_ONLY: 28534 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28535 break; 28536 default: 28537 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28538 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28539 cdda->cdda_subcode); 28540 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28541 return (EINVAL); 28542 } 28543 28544 /* Build and send the command */ 28545 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28546 bzero(cdb, CDB_GROUP5); 28547 28548 if (un->un_f_cfg_cdda == TRUE) { 28549 cdb[0] = (char)SCMD_READ_CD; 28550 cdb[1] = 0x04; 28551 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28552 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28553 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28554 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28555 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28556 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28557 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28558 cdb[9] = 0x10; 28559 switch (cdda->cdda_subcode) { 28560 case CDROM_DA_NO_SUBCODE : 28561 cdb[10] = 0x0; 28562 break; 28563 case CDROM_DA_SUBQ : 28564 cdb[10] = 0x2; 28565 break; 28566 case CDROM_DA_ALL_SUBCODE : 28567 cdb[10] = 0x1; 28568 break; 28569 case CDROM_DA_SUBCODE_ONLY : 28570 /* FALLTHROUGH */ 28571 default : 28572 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28573 kmem_free(com, sizeof (*com)); 28574 return (ENOTTY); 28575 } 28576 } else { 28577 cdb[0] = (char)SCMD_READ_CDDA; 28578 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28579 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28580 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28581 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28582 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28583 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28584 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28585 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28586 cdb[10] = cdda->cdda_subcode; 28587 } 28588 28589 com->uscsi_cdb = cdb; 28590 com->uscsi_cdblen = CDB_GROUP5; 28591 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28592 com->uscsi_buflen = buflen; 28593 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28594 28595 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28596 UIO_SYSSPACE, SD_PATH_STANDARD); 28597 28598 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28599 kmem_free(com, sizeof (*com)); 28600 return (rval); 28601 } 28602 28603 28604 /* 28605 * Function: sr_read_cdxa() 28606 * 28607 * Description: This routine is the driver entry point for handling CD-ROM 28608 * ioctl requests to return CD-XA (Extended Architecture) data. 28609 * (CDROMCDXA). 28610 * 28611 * Arguments: dev - the device 'dev_t' 28612 * data - pointer to user provided CD-XA structure specifying 28613 * the data starting address, transfer length, and format 28614 * flag - this argument is a pass through to ddi_copyxxx() 28615 * directly from the mode argument of ioctl(). 28616 * 28617 * Return Code: the code returned by sd_send_scsi_cmd() 28618 * EFAULT if ddi_copyxxx() fails 28619 * ENXIO if fail ddi_get_soft_state 28620 * EINVAL if data pointer is NULL 28621 */ 28622 28623 static int 28624 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28625 { 28626 struct sd_lun *un; 28627 struct uscsi_cmd *com; 28628 struct cdrom_cdxa *cdxa; 28629 int rval; 28630 size_t buflen; 28631 char cdb[CDB_GROUP5]; 28632 uchar_t read_flags; 28633 28634 #ifdef _MULTI_DATAMODEL 28635 /* To support ILP32 applications in an LP64 world */ 28636 struct cdrom_cdxa32 cdrom_cdxa32; 28637 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28638 #endif /* _MULTI_DATAMODEL */ 28639 28640 if (data == NULL) { 28641 return (EINVAL); 28642 } 28643 28644 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28645 return (ENXIO); 28646 } 28647 28648 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28649 28650 #ifdef _MULTI_DATAMODEL 28651 switch (ddi_model_convert_from(flag & FMODELS)) { 28652 case DDI_MODEL_ILP32: 28653 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28654 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28655 return (EFAULT); 28656 } 28657 /* 28658 * Convert the ILP32 uscsi data from the 28659 * application to LP64 for internal use. 28660 */ 28661 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28662 break; 28663 case DDI_MODEL_NONE: 28664 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28665 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28666 return (EFAULT); 28667 } 28668 break; 28669 } 28670 #else /* ! _MULTI_DATAMODEL */ 28671 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28672 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28673 return (EFAULT); 28674 } 28675 #endif /* _MULTI_DATAMODEL */ 28676 28677 /* 28678 * Since MMC-2 expects max 3 bytes for length, check if the 28679 * length input is greater than 3 bytes 28680 */ 28681 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28682 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28683 "cdrom transfer length too large: %d (limit %d)\n", 28684 cdxa->cdxa_length, 0xFFFFFF); 28685 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28686 return (EINVAL); 28687 } 28688 28689 switch (cdxa->cdxa_format) { 28690 case CDROM_XA_DATA: 28691 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28692 read_flags = 0x10; 28693 break; 28694 case CDROM_XA_SECTOR_DATA: 28695 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28696 read_flags = 0xf8; 28697 break; 28698 case CDROM_XA_DATA_W_ERROR: 28699 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28700 read_flags = 0xfc; 28701 break; 28702 default: 28703 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28704 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28705 cdxa->cdxa_format); 28706 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28707 return (EINVAL); 28708 } 28709 28710 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28711 bzero(cdb, CDB_GROUP5); 28712 if (un->un_f_mmc_cap == TRUE) { 28713 cdb[0] = (char)SCMD_READ_CD; 28714 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28715 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28716 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28717 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28718 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28719 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28720 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28721 cdb[9] = (char)read_flags; 28722 } else { 28723 /* 28724 * Note: A vendor specific command (0xDB) is being used her to 28725 * request a read of all subcodes. 28726 */ 28727 cdb[0] = (char)SCMD_READ_CDXA; 28728 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28729 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28730 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28731 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28732 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28733 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28734 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28735 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28736 cdb[10] = cdxa->cdxa_format; 28737 } 28738 com->uscsi_cdb = cdb; 28739 com->uscsi_cdblen = CDB_GROUP5; 28740 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28741 com->uscsi_buflen = buflen; 28742 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28743 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28744 UIO_SYSSPACE, SD_PATH_STANDARD); 28745 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28746 kmem_free(com, sizeof (*com)); 28747 return (rval); 28748 } 28749 28750 28751 /* 28752 * Function: sr_eject() 28753 * 28754 * Description: This routine is the driver entry point for handling CD-ROM 28755 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28756 * 28757 * Arguments: dev - the device 'dev_t' 28758 * 28759 * Return Code: the code returned by sd_send_scsi_cmd() 28760 */ 28761 28762 static int 28763 sr_eject(dev_t dev) 28764 { 28765 struct sd_lun *un; 28766 int rval; 28767 28768 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28769 (un->un_state == SD_STATE_OFFLINE)) { 28770 return (ENXIO); 28771 } 28772 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 28773 SD_PATH_STANDARD)) != 0) { 28774 return (rval); 28775 } 28776 28777 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 28778 SD_PATH_STANDARD); 28779 28780 if (rval == 0) { 28781 mutex_enter(SD_MUTEX(un)); 28782 sr_ejected(un); 28783 un->un_mediastate = DKIO_EJECTED; 28784 cv_broadcast(&un->un_state_cv); 28785 mutex_exit(SD_MUTEX(un)); 28786 } 28787 return (rval); 28788 } 28789 28790 28791 /* 28792 * Function: sr_ejected() 28793 * 28794 * Description: This routine updates the soft state structure to invalidate the 28795 * geometry information after the media has been ejected or a 28796 * media eject has been detected. 28797 * 28798 * Arguments: un - driver soft state (unit) structure 28799 */ 28800 28801 static void 28802 sr_ejected(struct sd_lun *un) 28803 { 28804 struct sd_errstats *stp; 28805 28806 ASSERT(un != NULL); 28807 ASSERT(mutex_owned(SD_MUTEX(un))); 28808 28809 un->un_f_blockcount_is_valid = FALSE; 28810 un->un_f_tgt_blocksize_is_valid = FALSE; 28811 un->un_f_geometry_is_valid = FALSE; 28812 28813 if (un->un_errstats != NULL) { 28814 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28815 stp->sd_capacity.value.ui64 = 0; 28816 } 28817 } 28818 28819 28820 /* 28821 * Function: sr_check_wp() 28822 * 28823 * Description: This routine checks the write protection of a removable media 28824 * disk via the write protect bit of the Mode Page Header device 28825 * specific field. This routine has been implemented to use the 28826 * error recovery mode page for all device types. 28827 * Note: In the future use a sd_send_scsi_MODE_SENSE() routine 28828 * 28829 * Arguments: dev - the device 'dev_t' 28830 * 28831 * Return Code: int indicating if the device is write protected (1) or not (0) 28832 * 28833 * Context: Kernel thread. 28834 * 28835 */ 28836 28837 static int 28838 sr_check_wp(dev_t dev) 28839 { 28840 struct sd_lun *un; 28841 uchar_t device_specific; 28842 uchar_t *sense; 28843 int hdrlen; 28844 int rval; 28845 int retry_flag = FALSE; 28846 28847 /* 28848 * Note: The return codes for this routine should be reworked to 28849 * properly handle the case of a NULL softstate. 28850 */ 28851 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28852 return (FALSE); 28853 } 28854 28855 if (un->un_f_cfg_is_atapi == TRUE) { 28856 retry_flag = TRUE; 28857 } 28858 28859 retry: 28860 if (un->un_f_cfg_is_atapi == TRUE) { 28861 /* 28862 * The mode page contents are not required; set the allocation 28863 * length for the mode page header only 28864 */ 28865 hdrlen = MODE_HEADER_LENGTH_GRP2; 28866 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28867 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 28868 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28869 device_specific = 28870 ((struct mode_header_grp2 *)sense)->device_specific; 28871 } else { 28872 hdrlen = MODE_HEADER_LENGTH; 28873 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28874 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 28875 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28876 device_specific = 28877 ((struct mode_header *)sense)->device_specific; 28878 } 28879 28880 if (rval != 0) { 28881 if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) { 28882 /* 28883 * For an Atapi Zip drive, observed the drive 28884 * reporting check condition for the first attempt. 28885 * Sense data indicating power on or bus device/reset. 28886 * Hence in case of failure need to try at least once 28887 * for Atapi devices. 28888 */ 28889 retry_flag = FALSE; 28890 kmem_free(sense, hdrlen); 28891 goto retry; 28892 } else { 28893 /* 28894 * Write protect mode sense failed; not all disks 28895 * understand this query. Return FALSE assuming that 28896 * these devices are not writable. 28897 */ 28898 rval = FALSE; 28899 } 28900 } else { 28901 if (device_specific & WRITE_PROTECT) { 28902 rval = TRUE; 28903 } else { 28904 rval = FALSE; 28905 } 28906 } 28907 kmem_free(sense, hdrlen); 28908 return (rval); 28909 } 28910 28911 28912 /* 28913 * Function: sr_volume_ctrl() 28914 * 28915 * Description: This routine is the driver entry point for handling CD-ROM 28916 * audio output volume ioctl requests. (CDROMVOLCTRL) 28917 * 28918 * Arguments: dev - the device 'dev_t' 28919 * data - pointer to user audio volume control structure 28920 * flag - this argument is a pass through to ddi_copyxxx() 28921 * directly from the mode argument of ioctl(). 28922 * 28923 * Return Code: the code returned by sd_send_scsi_cmd() 28924 * EFAULT if ddi_copyxxx() fails 28925 * ENXIO if fail ddi_get_soft_state 28926 * EINVAL if data pointer is NULL 28927 * 28928 */ 28929 28930 static int 28931 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28932 { 28933 struct sd_lun *un; 28934 struct cdrom_volctrl volume; 28935 struct cdrom_volctrl *vol = &volume; 28936 uchar_t *sense_page; 28937 uchar_t *select_page; 28938 uchar_t *sense; 28939 uchar_t *select; 28940 int sense_buflen; 28941 int select_buflen; 28942 int rval; 28943 28944 if (data == NULL) { 28945 return (EINVAL); 28946 } 28947 28948 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28949 (un->un_state == SD_STATE_OFFLINE)) { 28950 return (ENXIO); 28951 } 28952 28953 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 28954 return (EFAULT); 28955 } 28956 28957 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28958 struct mode_header_grp2 *sense_mhp; 28959 struct mode_header_grp2 *select_mhp; 28960 int bd_len; 28961 28962 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 28963 select_buflen = MODE_HEADER_LENGTH_GRP2 + 28964 MODEPAGE_AUDIO_CTRL_LEN; 28965 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28966 select = kmem_zalloc(select_buflen, KM_SLEEP); 28967 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 28968 sense_buflen, MODEPAGE_AUDIO_CTRL, 28969 SD_PATH_STANDARD)) != 0) { 28970 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28971 "sr_volume_ctrl: Mode Sense Failed\n"); 28972 kmem_free(sense, sense_buflen); 28973 kmem_free(select, select_buflen); 28974 return (rval); 28975 } 28976 sense_mhp = (struct mode_header_grp2 *)sense; 28977 select_mhp = (struct mode_header_grp2 *)select; 28978 bd_len = (sense_mhp->bdesc_length_hi << 8) | 28979 sense_mhp->bdesc_length_lo; 28980 if (bd_len > MODE_BLK_DESC_LENGTH) { 28981 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28982 "sr_volume_ctrl: Mode Sense returned invalid " 28983 "block descriptor length\n"); 28984 kmem_free(sense, sense_buflen); 28985 kmem_free(select, select_buflen); 28986 return (EIO); 28987 } 28988 sense_page = (uchar_t *) 28989 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 28990 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 28991 select_mhp->length_msb = 0; 28992 select_mhp->length_lsb = 0; 28993 select_mhp->bdesc_length_hi = 0; 28994 select_mhp->bdesc_length_lo = 0; 28995 } else { 28996 struct mode_header *sense_mhp, *select_mhp; 28997 28998 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28999 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29000 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29001 select = kmem_zalloc(select_buflen, KM_SLEEP); 29002 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29003 sense_buflen, MODEPAGE_AUDIO_CTRL, 29004 SD_PATH_STANDARD)) != 0) { 29005 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29006 "sr_volume_ctrl: Mode Sense Failed\n"); 29007 kmem_free(sense, sense_buflen); 29008 kmem_free(select, select_buflen); 29009 return (rval); 29010 } 29011 sense_mhp = (struct mode_header *)sense; 29012 select_mhp = (struct mode_header *)select; 29013 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29014 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29015 "sr_volume_ctrl: Mode Sense returned invalid " 29016 "block descriptor length\n"); 29017 kmem_free(sense, sense_buflen); 29018 kmem_free(select, select_buflen); 29019 return (EIO); 29020 } 29021 sense_page = (uchar_t *) 29022 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29023 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29024 select_mhp->length = 0; 29025 select_mhp->bdesc_length = 0; 29026 } 29027 /* 29028 * Note: An audio control data structure could be created and overlayed 29029 * on the following in place of the array indexing method implemented. 29030 */ 29031 29032 /* Build the select data for the user volume data */ 29033 select_page[0] = MODEPAGE_AUDIO_CTRL; 29034 select_page[1] = 0xE; 29035 /* Set the immediate bit */ 29036 select_page[2] = 0x04; 29037 /* Zero out reserved fields */ 29038 select_page[3] = 0x00; 29039 select_page[4] = 0x00; 29040 /* Return sense data for fields not to be modified */ 29041 select_page[5] = sense_page[5]; 29042 select_page[6] = sense_page[6]; 29043 select_page[7] = sense_page[7]; 29044 /* Set the user specified volume levels for channel 0 and 1 */ 29045 select_page[8] = 0x01; 29046 select_page[9] = vol->channel0; 29047 select_page[10] = 0x02; 29048 select_page[11] = vol->channel1; 29049 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29050 select_page[12] = sense_page[12]; 29051 select_page[13] = sense_page[13]; 29052 select_page[14] = sense_page[14]; 29053 select_page[15] = sense_page[15]; 29054 29055 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29056 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29057 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29058 } else { 29059 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29060 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29061 } 29062 29063 kmem_free(sense, sense_buflen); 29064 kmem_free(select, select_buflen); 29065 return (rval); 29066 } 29067 29068 29069 /* 29070 * Function: sr_read_sony_session_offset() 29071 * 29072 * Description: This routine is the driver entry point for handling CD-ROM 29073 * ioctl requests for session offset information. (CDROMREADOFFSET) 29074 * The address of the first track in the last session of a 29075 * multi-session CD-ROM is returned 29076 * 29077 * Note: This routine uses a vendor specific key value in the 29078 * command control field without implementing any vendor check here 29079 * or in the ioctl routine. 29080 * 29081 * Arguments: dev - the device 'dev_t' 29082 * data - pointer to an int to hold the requested address 29083 * flag - this argument is a pass through to ddi_copyxxx() 29084 * directly from the mode argument of ioctl(). 29085 * 29086 * Return Code: the code returned by sd_send_scsi_cmd() 29087 * EFAULT if ddi_copyxxx() fails 29088 * ENXIO if fail ddi_get_soft_state 29089 * EINVAL if data pointer is NULL 29090 */ 29091 29092 static int 29093 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29094 { 29095 struct sd_lun *un; 29096 struct uscsi_cmd *com; 29097 caddr_t buffer; 29098 char cdb[CDB_GROUP1]; 29099 int session_offset = 0; 29100 int rval; 29101 29102 if (data == NULL) { 29103 return (EINVAL); 29104 } 29105 29106 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29107 (un->un_state == SD_STATE_OFFLINE)) { 29108 return (ENXIO); 29109 } 29110 29111 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29112 bzero(cdb, CDB_GROUP1); 29113 cdb[0] = SCMD_READ_TOC; 29114 /* 29115 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29116 * (4 byte TOC response header + 8 byte response data) 29117 */ 29118 cdb[8] = SONY_SESSION_OFFSET_LEN; 29119 /* Byte 9 is the control byte. A vendor specific value is used */ 29120 cdb[9] = SONY_SESSION_OFFSET_KEY; 29121 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29122 com->uscsi_cdb = cdb; 29123 com->uscsi_cdblen = CDB_GROUP1; 29124 com->uscsi_bufaddr = buffer; 29125 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29126 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29127 29128 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29129 UIO_SYSSPACE, SD_PATH_STANDARD); 29130 if (rval != 0) { 29131 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29132 kmem_free(com, sizeof (*com)); 29133 return (rval); 29134 } 29135 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29136 session_offset = 29137 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29138 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29139 /* 29140 * Offset returned offset in current lbasize block's. Convert to 29141 * 2k block's to return to the user 29142 */ 29143 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29144 session_offset >>= 2; 29145 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29146 session_offset >>= 1; 29147 } 29148 } 29149 29150 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29151 rval = EFAULT; 29152 } 29153 29154 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29155 kmem_free(com, sizeof (*com)); 29156 return (rval); 29157 } 29158 29159 29160 /* 29161 * Function: sd_wm_cache_constructor() 29162 * 29163 * Description: Cache Constructor for the wmap cache for the read/modify/write 29164 * devices. 29165 * 29166 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29167 * un - sd_lun structure for the device. 29168 * flag - the km flags passed to constructor 29169 * 29170 * Return Code: 0 on success. 29171 * -1 on failure. 29172 */ 29173 29174 /*ARGSUSED*/ 29175 static int 29176 sd_wm_cache_constructor(void *wm, void *un, int flags) 29177 { 29178 bzero(wm, sizeof (struct sd_w_map)); 29179 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29180 return (0); 29181 } 29182 29183 29184 /* 29185 * Function: sd_wm_cache_destructor() 29186 * 29187 * Description: Cache destructor for the wmap cache for the read/modify/write 29188 * devices. 29189 * 29190 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29191 * un - sd_lun structure for the device. 29192 */ 29193 /*ARGSUSED*/ 29194 static void 29195 sd_wm_cache_destructor(void *wm, void *un) 29196 { 29197 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29198 } 29199 29200 29201 /* 29202 * Function: sd_range_lock() 29203 * 29204 * Description: Lock the range of blocks specified as parameter to ensure 29205 * that read, modify write is atomic and no other i/o writes 29206 * to the same location. The range is specified in terms 29207 * of start and end blocks. Block numbers are the actual 29208 * media block numbers and not system. 29209 * 29210 * Arguments: un - sd_lun structure for the device. 29211 * startb - The starting block number 29212 * endb - The end block number 29213 * typ - type of i/o - simple/read_modify_write 29214 * 29215 * Return Code: wm - pointer to the wmap structure. 29216 * 29217 * Context: This routine can sleep. 29218 */ 29219 29220 static struct sd_w_map * 29221 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29222 { 29223 struct sd_w_map *wmp = NULL; 29224 struct sd_w_map *sl_wmp = NULL; 29225 struct sd_w_map *tmp_wmp; 29226 wm_state state = SD_WM_CHK_LIST; 29227 29228 29229 ASSERT(un != NULL); 29230 ASSERT(!mutex_owned(SD_MUTEX(un))); 29231 29232 mutex_enter(SD_MUTEX(un)); 29233 29234 while (state != SD_WM_DONE) { 29235 29236 switch (state) { 29237 case SD_WM_CHK_LIST: 29238 /* 29239 * This is the starting state. Check the wmap list 29240 * to see if the range is currently available. 29241 */ 29242 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29243 /* 29244 * If this is a simple write and no rmw 29245 * i/o is pending then try to lock the 29246 * range as the range should be available. 29247 */ 29248 state = SD_WM_LOCK_RANGE; 29249 } else { 29250 tmp_wmp = sd_get_range(un, startb, endb); 29251 if (tmp_wmp != NULL) { 29252 if ((wmp != NULL) && ONLIST(un, wmp)) { 29253 /* 29254 * Should not keep onlist wmps 29255 * while waiting this macro 29256 * will also do wmp = NULL; 29257 */ 29258 FREE_ONLIST_WMAP(un, wmp); 29259 } 29260 /* 29261 * sl_wmp is the wmap on which wait 29262 * is done, since the tmp_wmp points 29263 * to the inuse wmap, set sl_wmp to 29264 * tmp_wmp and change the state to sleep 29265 */ 29266 sl_wmp = tmp_wmp; 29267 state = SD_WM_WAIT_MAP; 29268 } else { 29269 state = SD_WM_LOCK_RANGE; 29270 } 29271 29272 } 29273 break; 29274 29275 case SD_WM_LOCK_RANGE: 29276 ASSERT(un->un_wm_cache); 29277 /* 29278 * The range need to be locked, try to get a wmap. 29279 * First attempt it with NO_SLEEP, want to avoid a sleep 29280 * if possible as we will have to release the sd mutex 29281 * if we have to sleep. 29282 */ 29283 if (wmp == NULL) 29284 wmp = kmem_cache_alloc(un->un_wm_cache, 29285 KM_NOSLEEP); 29286 if (wmp == NULL) { 29287 mutex_exit(SD_MUTEX(un)); 29288 _NOTE(DATA_READABLE_WITHOUT_LOCK 29289 (sd_lun::un_wm_cache)) 29290 wmp = kmem_cache_alloc(un->un_wm_cache, 29291 KM_SLEEP); 29292 mutex_enter(SD_MUTEX(un)); 29293 /* 29294 * we released the mutex so recheck and go to 29295 * check list state. 29296 */ 29297 state = SD_WM_CHK_LIST; 29298 } else { 29299 /* 29300 * We exit out of state machine since we 29301 * have the wmap. Do the housekeeping first. 29302 * place the wmap on the wmap list if it is not 29303 * on it already and then set the state to done. 29304 */ 29305 wmp->wm_start = startb; 29306 wmp->wm_end = endb; 29307 wmp->wm_flags = typ | SD_WM_BUSY; 29308 if (typ & SD_WTYPE_RMW) { 29309 un->un_rmw_count++; 29310 } 29311 /* 29312 * If not already on the list then link 29313 */ 29314 if (!ONLIST(un, wmp)) { 29315 wmp->wm_next = un->un_wm; 29316 wmp->wm_prev = NULL; 29317 if (wmp->wm_next) 29318 wmp->wm_next->wm_prev = wmp; 29319 un->un_wm = wmp; 29320 } 29321 state = SD_WM_DONE; 29322 } 29323 break; 29324 29325 case SD_WM_WAIT_MAP: 29326 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29327 /* 29328 * Wait is done on sl_wmp, which is set in the 29329 * check_list state. 29330 */ 29331 sl_wmp->wm_wanted_count++; 29332 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29333 sl_wmp->wm_wanted_count--; 29334 /* 29335 * We can reuse the memory from the completed sl_wmp 29336 * lock range for our new lock, but only if noone is 29337 * waiting for it. 29338 */ 29339 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29340 if (sl_wmp->wm_wanted_count == 0) { 29341 if (wmp != NULL) 29342 CHK_N_FREEWMP(un, wmp); 29343 wmp = sl_wmp; 29344 } 29345 sl_wmp = NULL; 29346 /* 29347 * After waking up, need to recheck for availability of 29348 * range. 29349 */ 29350 state = SD_WM_CHK_LIST; 29351 break; 29352 29353 default: 29354 panic("sd_range_lock: " 29355 "Unknown state %d in sd_range_lock", state); 29356 /*NOTREACHED*/ 29357 } /* switch(state) */ 29358 29359 } /* while(state != SD_WM_DONE) */ 29360 29361 mutex_exit(SD_MUTEX(un)); 29362 29363 ASSERT(wmp != NULL); 29364 29365 return (wmp); 29366 } 29367 29368 29369 /* 29370 * Function: sd_get_range() 29371 * 29372 * Description: Find if there any overlapping I/O to this one 29373 * Returns the write-map of 1st such I/O, NULL otherwise. 29374 * 29375 * Arguments: un - sd_lun structure for the device. 29376 * startb - The starting block number 29377 * endb - The end block number 29378 * 29379 * Return Code: wm - pointer to the wmap structure. 29380 */ 29381 29382 static struct sd_w_map * 29383 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29384 { 29385 struct sd_w_map *wmp; 29386 29387 ASSERT(un != NULL); 29388 29389 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29390 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29391 continue; 29392 } 29393 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29394 break; 29395 } 29396 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29397 break; 29398 } 29399 } 29400 29401 return (wmp); 29402 } 29403 29404 29405 /* 29406 * Function: sd_free_inlist_wmap() 29407 * 29408 * Description: Unlink and free a write map struct. 29409 * 29410 * Arguments: un - sd_lun structure for the device. 29411 * wmp - sd_w_map which needs to be unlinked. 29412 */ 29413 29414 static void 29415 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29416 { 29417 ASSERT(un != NULL); 29418 29419 if (un->un_wm == wmp) { 29420 un->un_wm = wmp->wm_next; 29421 } else { 29422 wmp->wm_prev->wm_next = wmp->wm_next; 29423 } 29424 29425 if (wmp->wm_next) { 29426 wmp->wm_next->wm_prev = wmp->wm_prev; 29427 } 29428 29429 wmp->wm_next = wmp->wm_prev = NULL; 29430 29431 kmem_cache_free(un->un_wm_cache, wmp); 29432 } 29433 29434 29435 /* 29436 * Function: sd_range_unlock() 29437 * 29438 * Description: Unlock the range locked by wm. 29439 * Free write map if nobody else is waiting on it. 29440 * 29441 * Arguments: un - sd_lun structure for the device. 29442 * wmp - sd_w_map which needs to be unlinked. 29443 */ 29444 29445 static void 29446 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29447 { 29448 ASSERT(un != NULL); 29449 ASSERT(wm != NULL); 29450 ASSERT(!mutex_owned(SD_MUTEX(un))); 29451 29452 mutex_enter(SD_MUTEX(un)); 29453 29454 if (wm->wm_flags & SD_WTYPE_RMW) { 29455 un->un_rmw_count--; 29456 } 29457 29458 if (wm->wm_wanted_count) { 29459 wm->wm_flags = 0; 29460 /* 29461 * Broadcast that the wmap is available now. 29462 */ 29463 cv_broadcast(&wm->wm_avail); 29464 } else { 29465 /* 29466 * If no one is waiting on the map, it should be free'ed. 29467 */ 29468 sd_free_inlist_wmap(un, wm); 29469 } 29470 29471 mutex_exit(SD_MUTEX(un)); 29472 } 29473 29474 29475 /* 29476 * Function: sd_read_modify_write_task 29477 * 29478 * Description: Called from a taskq thread to initiate the write phase of 29479 * a read-modify-write request. This is used for targets where 29480 * un->un_sys_blocksize != un->un_tgt_blocksize. 29481 * 29482 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29483 * 29484 * Context: Called under taskq thread context. 29485 */ 29486 29487 static void 29488 sd_read_modify_write_task(void *arg) 29489 { 29490 struct sd_mapblocksize_info *bsp; 29491 struct buf *bp; 29492 struct sd_xbuf *xp; 29493 struct sd_lun *un; 29494 29495 bp = arg; /* The bp is given in arg */ 29496 ASSERT(bp != NULL); 29497 29498 /* Get the pointer to the layer-private data struct */ 29499 xp = SD_GET_XBUF(bp); 29500 ASSERT(xp != NULL); 29501 bsp = xp->xb_private; 29502 ASSERT(bsp != NULL); 29503 29504 un = SD_GET_UN(bp); 29505 ASSERT(un != NULL); 29506 ASSERT(!mutex_owned(SD_MUTEX(un))); 29507 29508 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29509 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29510 29511 /* 29512 * This is the write phase of a read-modify-write request, called 29513 * under the context of a taskq thread in response to the completion 29514 * of the read portion of the rmw request completing under interrupt 29515 * context. The write request must be sent from here down the iostart 29516 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29517 * we use the layer index saved in the layer-private data area. 29518 */ 29519 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29520 29521 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29522 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29523 } 29524 29525 29526 /* 29527 * Function: sddump_do_read_of_rmw() 29528 * 29529 * Description: This routine will be called from sddump, If sddump is called 29530 * with an I/O which not aligned on device blocksize boundary 29531 * then the write has to be converted to read-modify-write. 29532 * Do the read part here in order to keep sddump simple. 29533 * Note - That the sd_mutex is held across the call to this 29534 * routine. 29535 * 29536 * Arguments: un - sd_lun 29537 * blkno - block number in terms of media block size. 29538 * nblk - number of blocks. 29539 * bpp - pointer to pointer to the buf structure. On return 29540 * from this function, *bpp points to the valid buffer 29541 * to which the write has to be done. 29542 * 29543 * Return Code: 0 for success or errno-type return code 29544 */ 29545 29546 static int 29547 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29548 struct buf **bpp) 29549 { 29550 int err; 29551 int i; 29552 int rval; 29553 struct buf *bp; 29554 struct scsi_pkt *pkt = NULL; 29555 uint32_t target_blocksize; 29556 29557 ASSERT(un != NULL); 29558 ASSERT(mutex_owned(SD_MUTEX(un))); 29559 29560 target_blocksize = un->un_tgt_blocksize; 29561 29562 mutex_exit(SD_MUTEX(un)); 29563 29564 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29565 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29566 if (bp == NULL) { 29567 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29568 "no resources for dumping; giving up"); 29569 err = ENOMEM; 29570 goto done; 29571 } 29572 29573 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29574 blkno, nblk); 29575 if (rval != 0) { 29576 scsi_free_consistent_buf(bp); 29577 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29578 "no resources for dumping; giving up"); 29579 err = ENOMEM; 29580 goto done; 29581 } 29582 29583 pkt->pkt_flags |= FLAG_NOINTR; 29584 29585 err = EIO; 29586 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29587 29588 /* 29589 * Scsi_poll returns 0 (success) if the command completes and 29590 * the status block is STATUS_GOOD. We should only check 29591 * errors if this condition is not true. Even then we should 29592 * send our own request sense packet only if we have a check 29593 * condition and auto request sense has not been performed by 29594 * the hba. 29595 */ 29596 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29597 29598 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29599 err = 0; 29600 break; 29601 } 29602 29603 /* 29604 * Check CMD_DEV_GONE 1st, give up if device is gone, 29605 * no need to read RQS data. 29606 */ 29607 if (pkt->pkt_reason == CMD_DEV_GONE) { 29608 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29609 "Device is gone\n"); 29610 break; 29611 } 29612 29613 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29614 SD_INFO(SD_LOG_DUMP, un, 29615 "sddump: read failed with CHECK, try # %d\n", i); 29616 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29617 (void) sd_send_polled_RQS(un); 29618 } 29619 29620 continue; 29621 } 29622 29623 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29624 int reset_retval = 0; 29625 29626 SD_INFO(SD_LOG_DUMP, un, 29627 "sddump: read failed with BUSY, try # %d\n", i); 29628 29629 if (un->un_f_lun_reset_enabled == TRUE) { 29630 reset_retval = scsi_reset(SD_ADDRESS(un), 29631 RESET_LUN); 29632 } 29633 if (reset_retval == 0) { 29634 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29635 } 29636 (void) sd_send_polled_RQS(un); 29637 29638 } else { 29639 SD_INFO(SD_LOG_DUMP, un, 29640 "sddump: read failed with 0x%x, try # %d\n", 29641 SD_GET_PKT_STATUS(pkt), i); 29642 mutex_enter(SD_MUTEX(un)); 29643 sd_reset_target(un, pkt); 29644 mutex_exit(SD_MUTEX(un)); 29645 } 29646 29647 /* 29648 * If we are not getting anywhere with lun/target resets, 29649 * let's reset the bus. 29650 */ 29651 if (i > SD_NDUMP_RETRIES/2) { 29652 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29653 (void) sd_send_polled_RQS(un); 29654 } 29655 29656 } 29657 scsi_destroy_pkt(pkt); 29658 29659 if (err != 0) { 29660 scsi_free_consistent_buf(bp); 29661 *bpp = NULL; 29662 } else { 29663 *bpp = bp; 29664 } 29665 29666 done: 29667 mutex_enter(SD_MUTEX(un)); 29668 return (err); 29669 } 29670 29671 29672 /* 29673 * Function: sd_failfast_flushq 29674 * 29675 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29676 * in b_flags and move them onto the failfast queue, then kick 29677 * off a thread to return all bp's on the failfast queue to 29678 * their owners with an error set. 29679 * 29680 * Arguments: un - pointer to the soft state struct for the instance. 29681 * 29682 * Context: may execute in interrupt context. 29683 */ 29684 29685 static void 29686 sd_failfast_flushq(struct sd_lun *un) 29687 { 29688 struct buf *bp; 29689 struct buf *next_waitq_bp; 29690 struct buf *prev_waitq_bp = NULL; 29691 29692 ASSERT(un != NULL); 29693 ASSERT(mutex_owned(SD_MUTEX(un))); 29694 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29695 ASSERT(un->un_failfast_bp == NULL); 29696 29697 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29698 "sd_failfast_flushq: entry: un:0x%p\n", un); 29699 29700 /* 29701 * Check if we should flush all bufs when entering failfast state, or 29702 * just those with B_FAILFAST set. 29703 */ 29704 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29705 /* 29706 * Move *all* bp's on the wait queue to the failfast flush 29707 * queue, including those that do NOT have B_FAILFAST set. 29708 */ 29709 if (un->un_failfast_headp == NULL) { 29710 ASSERT(un->un_failfast_tailp == NULL); 29711 un->un_failfast_headp = un->un_waitq_headp; 29712 } else { 29713 ASSERT(un->un_failfast_tailp != NULL); 29714 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29715 } 29716 29717 un->un_failfast_tailp = un->un_waitq_tailp; 29718 29719 /* update kstat for each bp moved out of the waitq */ 29720 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29721 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29722 } 29723 29724 /* empty the waitq */ 29725 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29726 29727 } else { 29728 /* 29729 * Go thru the wait queue, pick off all entries with 29730 * B_FAILFAST set, and move these onto the failfast queue. 29731 */ 29732 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29733 /* 29734 * Save the pointer to the next bp on the wait queue, 29735 * so we get to it on the next iteration of this loop. 29736 */ 29737 next_waitq_bp = bp->av_forw; 29738 29739 /* 29740 * If this bp from the wait queue does NOT have 29741 * B_FAILFAST set, just move on to the next element 29742 * in the wait queue. Note, this is the only place 29743 * where it is correct to set prev_waitq_bp. 29744 */ 29745 if ((bp->b_flags & B_FAILFAST) == 0) { 29746 prev_waitq_bp = bp; 29747 continue; 29748 } 29749 29750 /* 29751 * Remove the bp from the wait queue. 29752 */ 29753 if (bp == un->un_waitq_headp) { 29754 /* The bp is the first element of the waitq. */ 29755 un->un_waitq_headp = next_waitq_bp; 29756 if (un->un_waitq_headp == NULL) { 29757 /* The wait queue is now empty */ 29758 un->un_waitq_tailp = NULL; 29759 } 29760 } else { 29761 /* 29762 * The bp is either somewhere in the middle 29763 * or at the end of the wait queue. 29764 */ 29765 ASSERT(un->un_waitq_headp != NULL); 29766 ASSERT(prev_waitq_bp != NULL); 29767 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29768 == 0); 29769 if (bp == un->un_waitq_tailp) { 29770 /* bp is the last entry on the waitq. */ 29771 ASSERT(next_waitq_bp == NULL); 29772 un->un_waitq_tailp = prev_waitq_bp; 29773 } 29774 prev_waitq_bp->av_forw = next_waitq_bp; 29775 } 29776 bp->av_forw = NULL; 29777 29778 /* 29779 * update kstat since the bp is moved out of 29780 * the waitq 29781 */ 29782 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29783 29784 /* 29785 * Now put the bp onto the failfast queue. 29786 */ 29787 if (un->un_failfast_headp == NULL) { 29788 /* failfast queue is currently empty */ 29789 ASSERT(un->un_failfast_tailp == NULL); 29790 un->un_failfast_headp = 29791 un->un_failfast_tailp = bp; 29792 } else { 29793 /* Add the bp to the end of the failfast q */ 29794 ASSERT(un->un_failfast_tailp != NULL); 29795 ASSERT(un->un_failfast_tailp->b_flags & 29796 B_FAILFAST); 29797 un->un_failfast_tailp->av_forw = bp; 29798 un->un_failfast_tailp = bp; 29799 } 29800 } 29801 } 29802 29803 /* 29804 * Now return all bp's on the failfast queue to their owners. 29805 */ 29806 while ((bp = un->un_failfast_headp) != NULL) { 29807 29808 un->un_failfast_headp = bp->av_forw; 29809 if (un->un_failfast_headp == NULL) { 29810 un->un_failfast_tailp = NULL; 29811 } 29812 29813 /* 29814 * We want to return the bp with a failure error code, but 29815 * we do not want a call to sd_start_cmds() to occur here, 29816 * so use sd_return_failed_command_no_restart() instead of 29817 * sd_return_failed_command(). 29818 */ 29819 sd_return_failed_command_no_restart(un, bp, EIO); 29820 } 29821 29822 /* Flush the xbuf queues if required. */ 29823 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29824 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29825 } 29826 29827 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29828 "sd_failfast_flushq: exit: un:0x%p\n", un); 29829 } 29830 29831 29832 /* 29833 * Function: sd_failfast_flushq_callback 29834 * 29835 * Description: Return TRUE if the given bp meets the criteria for failfast 29836 * flushing. Used with ddi_xbuf_flushq(9F). 29837 * 29838 * Arguments: bp - ptr to buf struct to be examined. 29839 * 29840 * Context: Any 29841 */ 29842 29843 static int 29844 sd_failfast_flushq_callback(struct buf *bp) 29845 { 29846 /* 29847 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29848 * state is entered; OR (2) the given bp has B_FAILFAST set. 29849 */ 29850 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29851 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29852 } 29853 29854 29855 29856 #if defined(__i386) || defined(__amd64) 29857 /* 29858 * Function: sd_setup_next_xfer 29859 * 29860 * Description: Prepare next I/O operation using DMA_PARTIAL 29861 * 29862 */ 29863 29864 static int 29865 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29866 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29867 { 29868 ssize_t num_blks_not_xfered; 29869 daddr_t strt_blk_num; 29870 ssize_t bytes_not_xfered; 29871 int rval; 29872 29873 ASSERT(pkt->pkt_resid == 0); 29874 29875 /* 29876 * Calculate next block number and amount to be transferred. 29877 * 29878 * How much data NOT transfered to the HBA yet. 29879 */ 29880 bytes_not_xfered = xp->xb_dma_resid; 29881 29882 /* 29883 * figure how many blocks NOT transfered to the HBA yet. 29884 */ 29885 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29886 29887 /* 29888 * set starting block number to the end of what WAS transfered. 29889 */ 29890 strt_blk_num = xp->xb_blkno + 29891 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29892 29893 /* 29894 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29895 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29896 * the disk mutex here. 29897 */ 29898 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29899 strt_blk_num, num_blks_not_xfered); 29900 29901 if (rval == 0) { 29902 29903 /* 29904 * Success. 29905 * 29906 * Adjust things if there are still more blocks to be 29907 * transfered. 29908 */ 29909 xp->xb_dma_resid = pkt->pkt_resid; 29910 pkt->pkt_resid = 0; 29911 29912 return (1); 29913 } 29914 29915 /* 29916 * There's really only one possible return value from 29917 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29918 * returns NULL. 29919 */ 29920 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29921 29922 bp->b_resid = bp->b_bcount; 29923 bp->b_flags |= B_ERROR; 29924 29925 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29926 "Error setting up next portion of DMA transfer\n"); 29927 29928 return (0); 29929 } 29930 #endif 29931 29932 /* 29933 * Function: sd_panic_for_res_conflict 29934 * 29935 * Description: Call panic with a string formated with "Reservation Conflict" 29936 * and a human readable identifier indicating the SD instance 29937 * that experienced the reservation conflict. 29938 * 29939 * Arguments: un - pointer to the soft state struct for the instance. 29940 * 29941 * Context: may execute in interrupt context. 29942 */ 29943 29944 #define SD_RESV_CONFLICT_FMT_LEN 40 29945 void 29946 sd_panic_for_res_conflict(struct sd_lun *un) 29947 { 29948 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 29949 char path_str[MAXPATHLEN]; 29950 29951 (void) snprintf(panic_str, sizeof (panic_str), 29952 "Reservation Conflict\nDisk: %s", 29953 ddi_pathname(SD_DEVINFO(un), path_str)); 29954 29955 panic(panic_str); 29956 } 29957 29958 /* 29959 * Note: The following sd_faultinjection_ioctl( ) routines implement 29960 * driver support for handling fault injection for error analysis 29961 * causing faults in multiple layers of the driver. 29962 * 29963 */ 29964 29965 #ifdef SD_FAULT_INJECTION 29966 static uint_t sd_fault_injection_on = 0; 29967 29968 /* 29969 * Function: sd_faultinjection_ioctl() 29970 * 29971 * Description: This routine is the driver entry point for handling 29972 * faultinjection ioctls to inject errors into the 29973 * layer model 29974 * 29975 * Arguments: cmd - the ioctl cmd recieved 29976 * arg - the arguments from user and returns 29977 */ 29978 29979 static void 29980 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 29981 29982 uint_t i; 29983 uint_t rval; 29984 29985 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 29986 29987 mutex_enter(SD_MUTEX(un)); 29988 29989 switch (cmd) { 29990 case SDIOCRUN: 29991 /* Allow pushed faults to be injected */ 29992 SD_INFO(SD_LOG_SDTEST, un, 29993 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 29994 29995 sd_fault_injection_on = 1; 29996 29997 SD_INFO(SD_LOG_IOERR, un, 29998 "sd_faultinjection_ioctl: run finished\n"); 29999 break; 30000 30001 case SDIOCSTART: 30002 /* Start Injection Session */ 30003 SD_INFO(SD_LOG_SDTEST, un, 30004 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30005 30006 sd_fault_injection_on = 0; 30007 un->sd_injection_mask = 0xFFFFFFFF; 30008 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30009 un->sd_fi_fifo_pkt[i] = NULL; 30010 un->sd_fi_fifo_xb[i] = NULL; 30011 un->sd_fi_fifo_un[i] = NULL; 30012 un->sd_fi_fifo_arq[i] = NULL; 30013 } 30014 un->sd_fi_fifo_start = 0; 30015 un->sd_fi_fifo_end = 0; 30016 30017 mutex_enter(&(un->un_fi_mutex)); 30018 un->sd_fi_log[0] = '\0'; 30019 un->sd_fi_buf_len = 0; 30020 mutex_exit(&(un->un_fi_mutex)); 30021 30022 SD_INFO(SD_LOG_IOERR, un, 30023 "sd_faultinjection_ioctl: start finished\n"); 30024 break; 30025 30026 case SDIOCSTOP: 30027 /* Stop Injection Session */ 30028 SD_INFO(SD_LOG_SDTEST, un, 30029 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30030 sd_fault_injection_on = 0; 30031 un->sd_injection_mask = 0x0; 30032 30033 /* Empty stray or unuseds structs from fifo */ 30034 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30035 if (un->sd_fi_fifo_pkt[i] != NULL) { 30036 kmem_free(un->sd_fi_fifo_pkt[i], 30037 sizeof (struct sd_fi_pkt)); 30038 } 30039 if (un->sd_fi_fifo_xb[i] != NULL) { 30040 kmem_free(un->sd_fi_fifo_xb[i], 30041 sizeof (struct sd_fi_xb)); 30042 } 30043 if (un->sd_fi_fifo_un[i] != NULL) { 30044 kmem_free(un->sd_fi_fifo_un[i], 30045 sizeof (struct sd_fi_un)); 30046 } 30047 if (un->sd_fi_fifo_arq[i] != NULL) { 30048 kmem_free(un->sd_fi_fifo_arq[i], 30049 sizeof (struct sd_fi_arq)); 30050 } 30051 un->sd_fi_fifo_pkt[i] = NULL; 30052 un->sd_fi_fifo_un[i] = NULL; 30053 un->sd_fi_fifo_xb[i] = NULL; 30054 un->sd_fi_fifo_arq[i] = NULL; 30055 } 30056 un->sd_fi_fifo_start = 0; 30057 un->sd_fi_fifo_end = 0; 30058 30059 SD_INFO(SD_LOG_IOERR, un, 30060 "sd_faultinjection_ioctl: stop finished\n"); 30061 break; 30062 30063 case SDIOCINSERTPKT: 30064 /* Store a packet struct to be pushed onto fifo */ 30065 SD_INFO(SD_LOG_SDTEST, un, 30066 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30067 30068 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30069 30070 sd_fault_injection_on = 0; 30071 30072 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30073 if (un->sd_fi_fifo_pkt[i] != NULL) { 30074 kmem_free(un->sd_fi_fifo_pkt[i], 30075 sizeof (struct sd_fi_pkt)); 30076 } 30077 if (arg != NULL) { 30078 un->sd_fi_fifo_pkt[i] = 30079 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30080 if (un->sd_fi_fifo_pkt[i] == NULL) { 30081 /* Alloc failed don't store anything */ 30082 break; 30083 } 30084 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30085 sizeof (struct sd_fi_pkt), 0); 30086 if (rval == -1) { 30087 kmem_free(un->sd_fi_fifo_pkt[i], 30088 sizeof (struct sd_fi_pkt)); 30089 un->sd_fi_fifo_pkt[i] = NULL; 30090 } 30091 } else { 30092 SD_INFO(SD_LOG_IOERR, un, 30093 "sd_faultinjection_ioctl: pkt null\n"); 30094 } 30095 break; 30096 30097 case SDIOCINSERTXB: 30098 /* Store a xb struct to be pushed onto fifo */ 30099 SD_INFO(SD_LOG_SDTEST, un, 30100 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30101 30102 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30103 30104 sd_fault_injection_on = 0; 30105 30106 if (un->sd_fi_fifo_xb[i] != NULL) { 30107 kmem_free(un->sd_fi_fifo_xb[i], 30108 sizeof (struct sd_fi_xb)); 30109 un->sd_fi_fifo_xb[i] = NULL; 30110 } 30111 if (arg != NULL) { 30112 un->sd_fi_fifo_xb[i] = 30113 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30114 if (un->sd_fi_fifo_xb[i] == NULL) { 30115 /* Alloc failed don't store anything */ 30116 break; 30117 } 30118 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30119 sizeof (struct sd_fi_xb), 0); 30120 30121 if (rval == -1) { 30122 kmem_free(un->sd_fi_fifo_xb[i], 30123 sizeof (struct sd_fi_xb)); 30124 un->sd_fi_fifo_xb[i] = NULL; 30125 } 30126 } else { 30127 SD_INFO(SD_LOG_IOERR, un, 30128 "sd_faultinjection_ioctl: xb null\n"); 30129 } 30130 break; 30131 30132 case SDIOCINSERTUN: 30133 /* Store a un struct to be pushed onto fifo */ 30134 SD_INFO(SD_LOG_SDTEST, un, 30135 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30136 30137 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30138 30139 sd_fault_injection_on = 0; 30140 30141 if (un->sd_fi_fifo_un[i] != NULL) { 30142 kmem_free(un->sd_fi_fifo_un[i], 30143 sizeof (struct sd_fi_un)); 30144 un->sd_fi_fifo_un[i] = NULL; 30145 } 30146 if (arg != NULL) { 30147 un->sd_fi_fifo_un[i] = 30148 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30149 if (un->sd_fi_fifo_un[i] == NULL) { 30150 /* Alloc failed don't store anything */ 30151 break; 30152 } 30153 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30154 sizeof (struct sd_fi_un), 0); 30155 if (rval == -1) { 30156 kmem_free(un->sd_fi_fifo_un[i], 30157 sizeof (struct sd_fi_un)); 30158 un->sd_fi_fifo_un[i] = NULL; 30159 } 30160 30161 } else { 30162 SD_INFO(SD_LOG_IOERR, un, 30163 "sd_faultinjection_ioctl: un null\n"); 30164 } 30165 30166 break; 30167 30168 case SDIOCINSERTARQ: 30169 /* Store a arq struct to be pushed onto fifo */ 30170 SD_INFO(SD_LOG_SDTEST, un, 30171 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30172 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30173 30174 sd_fault_injection_on = 0; 30175 30176 if (un->sd_fi_fifo_arq[i] != NULL) { 30177 kmem_free(un->sd_fi_fifo_arq[i], 30178 sizeof (struct sd_fi_arq)); 30179 un->sd_fi_fifo_arq[i] = NULL; 30180 } 30181 if (arg != NULL) { 30182 un->sd_fi_fifo_arq[i] = 30183 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30184 if (un->sd_fi_fifo_arq[i] == NULL) { 30185 /* Alloc failed don't store anything */ 30186 break; 30187 } 30188 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30189 sizeof (struct sd_fi_arq), 0); 30190 if (rval == -1) { 30191 kmem_free(un->sd_fi_fifo_arq[i], 30192 sizeof (struct sd_fi_arq)); 30193 un->sd_fi_fifo_arq[i] = NULL; 30194 } 30195 30196 } else { 30197 SD_INFO(SD_LOG_IOERR, un, 30198 "sd_faultinjection_ioctl: arq null\n"); 30199 } 30200 30201 break; 30202 30203 case SDIOCPUSH: 30204 /* Push stored xb, pkt, un, and arq onto fifo */ 30205 sd_fault_injection_on = 0; 30206 30207 if (arg != NULL) { 30208 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30209 if (rval != -1 && 30210 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30211 un->sd_fi_fifo_end += i; 30212 } 30213 } else { 30214 SD_INFO(SD_LOG_IOERR, un, 30215 "sd_faultinjection_ioctl: push arg null\n"); 30216 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30217 un->sd_fi_fifo_end++; 30218 } 30219 } 30220 SD_INFO(SD_LOG_IOERR, un, 30221 "sd_faultinjection_ioctl: push to end=%d\n", 30222 un->sd_fi_fifo_end); 30223 break; 30224 30225 case SDIOCRETRIEVE: 30226 /* Return buffer of log from Injection session */ 30227 SD_INFO(SD_LOG_SDTEST, un, 30228 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30229 30230 sd_fault_injection_on = 0; 30231 30232 mutex_enter(&(un->un_fi_mutex)); 30233 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30234 un->sd_fi_buf_len+1, 0); 30235 mutex_exit(&(un->un_fi_mutex)); 30236 30237 if (rval == -1) { 30238 /* 30239 * arg is possibly invalid setting 30240 * it to NULL for return 30241 */ 30242 arg = NULL; 30243 } 30244 break; 30245 } 30246 30247 mutex_exit(SD_MUTEX(un)); 30248 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30249 " exit\n"); 30250 } 30251 30252 30253 /* 30254 * Function: sd_injection_log() 30255 * 30256 * Description: This routine adds buff to the already existing injection log 30257 * for retrieval via faultinjection_ioctl for use in fault 30258 * detection and recovery 30259 * 30260 * Arguments: buf - the string to add to the log 30261 */ 30262 30263 static void 30264 sd_injection_log(char *buf, struct sd_lun *un) 30265 { 30266 uint_t len; 30267 30268 ASSERT(un != NULL); 30269 ASSERT(buf != NULL); 30270 30271 mutex_enter(&(un->un_fi_mutex)); 30272 30273 len = min(strlen(buf), 255); 30274 /* Add logged value to Injection log to be returned later */ 30275 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30276 uint_t offset = strlen((char *)un->sd_fi_log); 30277 char *destp = (char *)un->sd_fi_log + offset; 30278 int i; 30279 for (i = 0; i < len; i++) { 30280 *destp++ = *buf++; 30281 } 30282 un->sd_fi_buf_len += len; 30283 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30284 } 30285 30286 mutex_exit(&(un->un_fi_mutex)); 30287 } 30288 30289 30290 /* 30291 * Function: sd_faultinjection() 30292 * 30293 * Description: This routine takes the pkt and changes its 30294 * content based on error injection scenerio. 30295 * 30296 * Arguments: pktp - packet to be changed 30297 */ 30298 30299 static void 30300 sd_faultinjection(struct scsi_pkt *pktp) 30301 { 30302 uint_t i; 30303 struct sd_fi_pkt *fi_pkt; 30304 struct sd_fi_xb *fi_xb; 30305 struct sd_fi_un *fi_un; 30306 struct sd_fi_arq *fi_arq; 30307 struct buf *bp; 30308 struct sd_xbuf *xb; 30309 struct sd_lun *un; 30310 30311 ASSERT(pktp != NULL); 30312 30313 /* pull bp xb and un from pktp */ 30314 bp = (struct buf *)pktp->pkt_private; 30315 xb = SD_GET_XBUF(bp); 30316 un = SD_GET_UN(bp); 30317 30318 ASSERT(un != NULL); 30319 30320 mutex_enter(SD_MUTEX(un)); 30321 30322 SD_TRACE(SD_LOG_SDTEST, un, 30323 "sd_faultinjection: entry Injection from sdintr\n"); 30324 30325 /* if injection is off return */ 30326 if (sd_fault_injection_on == 0 || 30327 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30328 mutex_exit(SD_MUTEX(un)); 30329 return; 30330 } 30331 30332 30333 /* take next set off fifo */ 30334 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30335 30336 fi_pkt = un->sd_fi_fifo_pkt[i]; 30337 fi_xb = un->sd_fi_fifo_xb[i]; 30338 fi_un = un->sd_fi_fifo_un[i]; 30339 fi_arq = un->sd_fi_fifo_arq[i]; 30340 30341 30342 /* set variables accordingly */ 30343 /* set pkt if it was on fifo */ 30344 if (fi_pkt != NULL) { 30345 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30346 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30347 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30348 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30349 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30350 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30351 30352 } 30353 30354 /* set xb if it was on fifo */ 30355 if (fi_xb != NULL) { 30356 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30357 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30358 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30359 SD_CONDSET(xb, xb, xb_victim_retry_count, 30360 "xb_victim_retry_count"); 30361 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30362 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30363 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30364 30365 /* copy in block data from sense */ 30366 if (fi_xb->xb_sense_data[0] != -1) { 30367 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30368 SENSE_LENGTH); 30369 } 30370 30371 /* copy in extended sense codes */ 30372 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30373 "es_code"); 30374 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30375 "es_key"); 30376 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30377 "es_add_code"); 30378 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30379 es_qual_code, "es_qual_code"); 30380 } 30381 30382 /* set un if it was on fifo */ 30383 if (fi_un != NULL) { 30384 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30385 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30386 SD_CONDSET(un, un, un_reset_retry_count, 30387 "un_reset_retry_count"); 30388 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30389 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30390 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30391 SD_CONDSET(un, un, un_f_geometry_is_valid, 30392 "un_f_geometry_is_valid"); 30393 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30394 "un_f_allow_bus_device_reset"); 30395 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30396 30397 } 30398 30399 /* copy in auto request sense if it was on fifo */ 30400 if (fi_arq != NULL) { 30401 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30402 } 30403 30404 /* free structs */ 30405 if (un->sd_fi_fifo_pkt[i] != NULL) { 30406 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30407 } 30408 if (un->sd_fi_fifo_xb[i] != NULL) { 30409 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30410 } 30411 if (un->sd_fi_fifo_un[i] != NULL) { 30412 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30413 } 30414 if (un->sd_fi_fifo_arq[i] != NULL) { 30415 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30416 } 30417 30418 /* 30419 * kmem_free does not gurantee to set to NULL 30420 * since we uses these to determine if we set 30421 * values or not lets confirm they are always 30422 * NULL after free 30423 */ 30424 un->sd_fi_fifo_pkt[i] = NULL; 30425 un->sd_fi_fifo_un[i] = NULL; 30426 un->sd_fi_fifo_xb[i] = NULL; 30427 un->sd_fi_fifo_arq[i] = NULL; 30428 30429 un->sd_fi_fifo_start++; 30430 30431 mutex_exit(SD_MUTEX(un)); 30432 30433 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30434 } 30435 30436 #endif /* SD_FAULT_INJECTION */ 30437 30438 /* 30439 * This routine is invoked in sd_unit_attach(). Before calling it, the 30440 * properties in conf file should be processed already, and "hotpluggable" 30441 * property was processed also. 30442 * 30443 * The sd driver distinguishes 3 different type of devices: removable media, 30444 * non-removable media, and hotpluggable. Below the differences are defined: 30445 * 30446 * 1. Device ID 30447 * 30448 * The device ID of a device is used to identify this device. Refer to 30449 * ddi_devid_register(9F). 30450 * 30451 * For a non-removable media disk device which can provide 0x80 or 0x83 30452 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30453 * device ID is created to identify this device. For other non-removable 30454 * media devices, a default device ID is created only if this device has 30455 * at least 2 alter cylinders. Otherwise, this device has no devid. 30456 * 30457 * ------------------------------------------------------- 30458 * removable media hotpluggable | Can Have Device ID 30459 * ------------------------------------------------------- 30460 * false false | Yes 30461 * false true | Yes 30462 * true x | No 30463 * ------------------------------------------------------ 30464 * 30465 * 30466 * 2. SCSI group 4 commands 30467 * 30468 * In SCSI specs, only some commands in group 4 command set can use 30469 * 8-byte addresses that can be used to access >2TB storage spaces. 30470 * Other commands have no such capability. Without supporting group4, 30471 * it is impossible to make full use of storage spaces of a disk with 30472 * capacity larger than 2TB. 30473 * 30474 * ----------------------------------------------- 30475 * removable media hotpluggable LP64 | Group 30476 * ----------------------------------------------- 30477 * false false false | 1 30478 * false false true | 4 30479 * false true false | 1 30480 * false true true | 4 30481 * true x x | 5 30482 * ----------------------------------------------- 30483 * 30484 * 30485 * 3. Check for VTOC Label 30486 * 30487 * If a direct-access disk has no EFI label, sd will check if it has a 30488 * valid VTOC label. Now, sd also does that check for removable media 30489 * and hotpluggable devices. 30490 * 30491 * -------------------------------------------------------------- 30492 * Direct-Access removable media hotpluggable | Check Label 30493 * ------------------------------------------------------------- 30494 * false false false | No 30495 * false false true | No 30496 * false true false | Yes 30497 * false true true | Yes 30498 * true x x | Yes 30499 * -------------------------------------------------------------- 30500 * 30501 * 30502 * 4. Building default VTOC label 30503 * 30504 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30505 * If those devices have no valid VTOC label, sd(7d) will attempt to 30506 * create default VTOC for them. Currently sd creates default VTOC label 30507 * for all devices on x86 platform (VTOC_16), but only for removable 30508 * media devices on SPARC (VTOC_8). 30509 * 30510 * ----------------------------------------------------------- 30511 * removable media hotpluggable platform | Default Label 30512 * ----------------------------------------------------------- 30513 * false false sparc | No 30514 * false true x86 | Yes 30515 * false true sparc | Yes 30516 * true x x | Yes 30517 * ---------------------------------------------------------- 30518 * 30519 * 30520 * 5. Supported blocksizes of target devices 30521 * 30522 * Sd supports non-512-byte blocksize for removable media devices only. 30523 * For other devices, only 512-byte blocksize is supported. This may be 30524 * changed in near future because some RAID devices require non-512-byte 30525 * blocksize 30526 * 30527 * ----------------------------------------------------------- 30528 * removable media hotpluggable | non-512-byte blocksize 30529 * ----------------------------------------------------------- 30530 * false false | No 30531 * false true | No 30532 * true x | Yes 30533 * ----------------------------------------------------------- 30534 * 30535 * 30536 * 6. Automatic mount & unmount (i.e. vold) 30537 * 30538 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30539 * if a device is removable media device. It return 1 for removable media 30540 * devices, and 0 for others. 30541 * 30542 * Vold treats a device as removable one only if DKIOREMOVABLE returns 1. 30543 * And it does automounting only for removable media devices. In order to 30544 * preserve users' experience and let vold continue to do automounting for 30545 * USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk 30546 * devices. 30547 * 30548 * ------------------------------------------------------ 30549 * removable media hotpluggable | automatic mount 30550 * ------------------------------------------------------ 30551 * false false | No 30552 * false true | Yes 30553 * true x | Yes 30554 * ------------------------------------------------------ 30555 * 30556 * 30557 * 7. fdisk partition management 30558 * 30559 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30560 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30561 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30562 * fdisk partitions on both x86 and SPARC platform. 30563 * 30564 * ----------------------------------------------------------- 30565 * platform removable media USB/1394 | fdisk supported 30566 * ----------------------------------------------------------- 30567 * x86 X X | true 30568 * ------------------------------------------------------------ 30569 * sparc X X | false 30570 * ------------------------------------------------------------ 30571 * 30572 * 30573 * 8. MBOOT/MBR 30574 * 30575 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30576 * read/write mboot for removable media devices on sparc platform. 30577 * 30578 * ----------------------------------------------------------- 30579 * platform removable media USB/1394 | mboot supported 30580 * ----------------------------------------------------------- 30581 * x86 X X | true 30582 * ------------------------------------------------------------ 30583 * sparc false false | false 30584 * sparc false true | true 30585 * sparc true false | true 30586 * sparc true true | true 30587 * ------------------------------------------------------------ 30588 * 30589 * 30590 * 9. error handling during opening device 30591 * 30592 * If failed to open a disk device, an errno is returned. For some kinds 30593 * of errors, different errno is returned depending on if this device is 30594 * a removable media device. This brings USB/1394 hard disks in line with 30595 * expected hard disk behavior. It is not expected that this breaks any 30596 * application. 30597 * 30598 * ------------------------------------------------------ 30599 * removable media hotpluggable | errno 30600 * ------------------------------------------------------ 30601 * false false | EIO 30602 * false true | EIO 30603 * true x | ENXIO 30604 * ------------------------------------------------------ 30605 * 30606 * 30607 * 10. off-by-1 workaround (bug 1175930, and 4996920) (x86 only) 30608 * 30609 * [ this is a bit of very ugly history, soon to be removed ] 30610 * 30611 * SCSI READ_CAPACITY command returns the last valid logical block number 30612 * which starts from 0. So real capacity is larger than the returned 30613 * value by 1. However, because scdk.c (which was EOL'ed) directly used 30614 * the logical block number as capacity of disk devices, off-by-1 work- 30615 * around was applied. This workaround causes fixed SCSI disk to loss a 30616 * sector on x86 platform, and precludes exchanging fixed hard disks 30617 * between sparc and x86. 30618 * 30619 * ------------------------------------------------------ 30620 * removable media hotplug | Off-by-1 works 30621 * ------------------------------------------------------- 30622 * false false | Yes 30623 * false true | No 30624 * true false | No 30625 * true true | No 30626 * ------------------------------------------------------ 30627 * 30628 * 30629 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30630 * 30631 * These IOCTLs are applicable only to removable media devices. 30632 * 30633 * ----------------------------------------------------------- 30634 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30635 * ----------------------------------------------------------- 30636 * false false | No 30637 * false true | No 30638 * true x | Yes 30639 * ----------------------------------------------------------- 30640 * 30641 * 30642 * 12. Kstats for partitions 30643 * 30644 * sd creates partition kstat for non-removable media devices. USB and 30645 * Firewire hard disks now have partition kstats 30646 * 30647 * ------------------------------------------------------ 30648 * removable media hotplugable | kstat 30649 * ------------------------------------------------------ 30650 * false false | Yes 30651 * false true | Yes 30652 * true x | No 30653 * ------------------------------------------------------ 30654 * 30655 * 30656 * 13. Removable media & hotpluggable properties 30657 * 30658 * Sd driver creates a "removable-media" property for removable media 30659 * devices. Parent nexus drivers create a "hotpluggable" property if 30660 * it supports hotplugging. 30661 * 30662 * --------------------------------------------------------------------- 30663 * removable media hotpluggable | "removable-media" " hotpluggable" 30664 * --------------------------------------------------------------------- 30665 * false false | No No 30666 * false true | No Yes 30667 * true false | Yes No 30668 * true true | Yes Yes 30669 * --------------------------------------------------------------------- 30670 * 30671 * 30672 * 14. Power Management 30673 * 30674 * sd only power manages removable media devices or devices that support 30675 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30676 * 30677 * A parent nexus that supports hotplugging can also set "pm-capable" 30678 * if the disk can be power managed. 30679 * 30680 * ------------------------------------------------------------ 30681 * removable media hotpluggable pm-capable | power manage 30682 * ------------------------------------------------------------ 30683 * false false false | No 30684 * false false true | Yes 30685 * false true false | No 30686 * false true true | Yes 30687 * true x x | Yes 30688 * ------------------------------------------------------------ 30689 * 30690 * USB and firewire hard disks can now be power managed independently 30691 * of the framebuffer 30692 * 30693 * 30694 * 15. Support for USB disks with capacity larger than 1TB 30695 * 30696 * Currently, sd doesn't permit a fixed disk device with capacity 30697 * larger than 1TB to be used in a 32-bit operating system environment. 30698 * However, sd doesn't do that for removable media devices. Instead, it 30699 * assumes that removable media devices cannot have a capacity larger 30700 * than 1TB. Therefore, using those devices on 32-bit system is partially 30701 * supported, which can cause some unexpected results. 30702 * 30703 * --------------------------------------------------------------------- 30704 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30705 * --------------------------------------------------------------------- 30706 * false false | true | no 30707 * false true | true | no 30708 * true false | true | Yes 30709 * true true | true | Yes 30710 * --------------------------------------------------------------------- 30711 * 30712 * 30713 * 16. Check write-protection at open time 30714 * 30715 * When a removable media device is being opened for writing without NDELAY 30716 * flag, sd will check if this device is writable. If attempting to open 30717 * without NDELAY flag a write-protected device, this operation will abort. 30718 * 30719 * ------------------------------------------------------------ 30720 * removable media USB/1394 | WP Check 30721 * ------------------------------------------------------------ 30722 * false false | No 30723 * false true | No 30724 * true false | Yes 30725 * true true | Yes 30726 * ------------------------------------------------------------ 30727 * 30728 * 30729 * 17. syslog when corrupted VTOC is encountered 30730 * 30731 * Currently, if an invalid VTOC is encountered, sd only print syslog 30732 * for fixed SCSI disks. 30733 * ------------------------------------------------------------ 30734 * removable media USB/1394 | print syslog 30735 * ------------------------------------------------------------ 30736 * false false | Yes 30737 * false true | No 30738 * true false | No 30739 * true true | No 30740 * ------------------------------------------------------------ 30741 */ 30742 static void 30743 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30744 { 30745 int pm_capable_prop; 30746 30747 ASSERT(un->un_sd); 30748 ASSERT(un->un_sd->sd_inq); 30749 30750 #if defined(_SUNOS_VTOC_16) 30751 /* 30752 * For VTOC_16 devices, the default label will be created for all 30753 * devices. (see sd_build_default_label) 30754 */ 30755 un->un_f_default_vtoc_supported = TRUE; 30756 #endif 30757 30758 if (un->un_sd->sd_inq->inq_rmb) { 30759 /* 30760 * The media of this device is removable. And for this kind 30761 * of devices, it is possible to change medium after openning 30762 * devices. Thus we should support this operation. 30763 */ 30764 un->un_f_has_removable_media = TRUE; 30765 30766 #if defined(_SUNOS_VTOC_8) 30767 /* 30768 * Note: currently, for VTOC_8 devices, default label is 30769 * created for removable and hotpluggable devices only. 30770 */ 30771 un->un_f_default_vtoc_supported = TRUE; 30772 #endif 30773 /* 30774 * support non-512-byte blocksize of removable media devices 30775 */ 30776 un->un_f_non_devbsize_supported = TRUE; 30777 30778 /* 30779 * Assume that all removable media devices support DOOR_LOCK 30780 */ 30781 un->un_f_doorlock_supported = TRUE; 30782 30783 /* 30784 * For a removable media device, it is possible to be opened 30785 * with NDELAY flag when there is no media in drive, in this 30786 * case we don't care if device is writable. But if without 30787 * NDELAY flag, we need to check if media is write-protected. 30788 */ 30789 un->un_f_chk_wp_open = TRUE; 30790 30791 /* 30792 * need to start a SCSI watch thread to monitor media state, 30793 * when media is being inserted or ejected, notify syseventd. 30794 */ 30795 un->un_f_monitor_media_state = TRUE; 30796 30797 /* 30798 * Some devices don't support START_STOP_UNIT command. 30799 * Therefore, we'd better check if a device supports it 30800 * before sending it. 30801 */ 30802 un->un_f_check_start_stop = TRUE; 30803 30804 /* 30805 * support eject media ioctl: 30806 * FDEJECT, DKIOCEJECT, CDROMEJECT 30807 */ 30808 un->un_f_eject_media_supported = TRUE; 30809 30810 /* 30811 * Because many removable-media devices don't support 30812 * LOG_SENSE, we couldn't use this command to check if 30813 * a removable media device support power-management. 30814 * We assume that they support power-management via 30815 * START_STOP_UNIT command and can be spun up and down 30816 * without limitations. 30817 */ 30818 un->un_f_pm_supported = TRUE; 30819 30820 /* 30821 * Need to create a zero length (Boolean) property 30822 * removable-media for the removable media devices. 30823 * Note that the return value of the property is not being 30824 * checked, since if unable to create the property 30825 * then do not want the attach to fail altogether. Consistent 30826 * with other property creation in attach. 30827 */ 30828 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30829 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30830 30831 } else { 30832 /* 30833 * create device ID for device 30834 */ 30835 un->un_f_devid_supported = TRUE; 30836 30837 /* 30838 * Spin up non-removable-media devices once it is attached 30839 */ 30840 un->un_f_attach_spinup = TRUE; 30841 30842 /* 30843 * According to SCSI specification, Sense data has two kinds of 30844 * format: fixed format, and descriptor format. At present, we 30845 * don't support descriptor format sense data for removable 30846 * media. 30847 */ 30848 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30849 un->un_f_descr_format_supported = TRUE; 30850 } 30851 30852 /* 30853 * kstats are created only for non-removable media devices. 30854 * 30855 * Set this in sd.conf to 0 in order to disable kstats. The 30856 * default is 1, so they are enabled by default. 30857 */ 30858 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30859 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30860 "enable-partition-kstats", 1)); 30861 30862 /* 30863 * Check if HBA has set the "pm-capable" property. 30864 * If "pm-capable" exists and is non-zero then we can 30865 * power manage the device without checking the start/stop 30866 * cycle count log sense page. 30867 * 30868 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 30869 * then we should not power manage the device. 30870 * 30871 * If "pm-capable" doesn't exist then pm_capable_prop will 30872 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 30873 * sd will check the start/stop cycle count log sense page 30874 * and power manage the device if the cycle count limit has 30875 * not been exceeded. 30876 */ 30877 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 30878 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 30879 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 30880 un->un_f_log_sense_supported = TRUE; 30881 } else { 30882 /* 30883 * pm-capable property exists. 30884 * 30885 * Convert "TRUE" values for pm_capable_prop to 30886 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 30887 * later. "TRUE" values are any values except 30888 * SD_PM_CAPABLE_FALSE (0) and 30889 * SD_PM_CAPABLE_UNDEFINED (-1) 30890 */ 30891 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 30892 un->un_f_log_sense_supported = FALSE; 30893 } else { 30894 un->un_f_pm_supported = TRUE; 30895 } 30896 30897 SD_INFO(SD_LOG_ATTACH_DETACH, un, 30898 "sd_unit_attach: un:0x%p pm-capable " 30899 "property set to %d.\n", un, un->un_f_pm_supported); 30900 } 30901 } 30902 30903 if (un->un_f_is_hotpluggable) { 30904 #if defined(_SUNOS_VTOC_8) 30905 /* 30906 * Note: currently, for VTOC_8 devices, default label is 30907 * created for removable and hotpluggable devices only. 30908 */ 30909 un->un_f_default_vtoc_supported = TRUE; 30910 #endif 30911 30912 /* 30913 * Temporarily, let hotpluggable devices pretend to be 30914 * removable-media devices for vold. 30915 */ 30916 un->un_f_monitor_media_state = TRUE; 30917 30918 un->un_f_check_start_stop = TRUE; 30919 30920 } 30921 30922 /* 30923 * By default, only DIRECT ACCESS devices and CDs will have Sun 30924 * labels. 30925 */ 30926 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 30927 (un->un_sd->sd_inq->inq_rmb)) { 30928 /* 30929 * Direct access devices have disk label 30930 */ 30931 un->un_f_vtoc_label_supported = TRUE; 30932 } 30933 30934 /* 30935 * Fdisk partitions are supported for all direct access devices on 30936 * x86 platform, and just for removable media and hotpluggable 30937 * devices on SPARC platform. Later, we will set the following flag 30938 * to FALSE if current device is not removable media or hotpluggable 30939 * device and if sd works on SAPRC platform. 30940 */ 30941 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30942 un->un_f_mboot_supported = TRUE; 30943 } 30944 30945 if (!un->un_f_is_hotpluggable && 30946 !un->un_sd->sd_inq->inq_rmb) { 30947 30948 #if defined(_SUNOS_VTOC_8) 30949 /* 30950 * Don't support fdisk on fixed disk 30951 */ 30952 un->un_f_mboot_supported = FALSE; 30953 #endif 30954 30955 /* 30956 * Fixed disk support SYNC CACHE 30957 */ 30958 un->un_f_sync_cache_supported = TRUE; 30959 30960 /* 30961 * For fixed disk, if its VTOC is not valid, we will write 30962 * errlog into system log 30963 */ 30964 if (un->un_f_vtoc_label_supported) 30965 un->un_f_vtoc_errlog_supported = TRUE; 30966 } 30967 } 30968