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 #include <sys/scsi/scsi.h> 34 #include <sys/dkbad.h> 35 #include <sys/dklabel.h> 36 #include <sys/dkio.h> 37 #include <sys/fdio.h> 38 #include <sys/cdio.h> 39 #include <sys/mhd.h> 40 #include <sys/vtoc.h> 41 #include <sys/dktp/fdisk.h> 42 #include <sys/file.h> 43 #include <sys/stat.h> 44 #include <sys/kstat.h> 45 #include <sys/vtrace.h> 46 #include <sys/note.h> 47 #include <sys/thread.h> 48 #include <sys/proc.h> 49 #include <sys/efi_partition.h> 50 #include <sys/var.h> 51 #include <sys/aio_req.h> 52 #if (defined(__fibre)) 53 /* Note: is there a leadville version of the following? */ 54 #include <sys/fc4/fcal_linkapp.h> 55 #endif 56 #include <sys/taskq.h> 57 #include <sys/uuid.h> 58 #include <sys/byteorder.h> 59 #include <sys/sdt.h> 60 61 #include "sd_xbuf.h" 62 63 #include <sys/scsi/targets/sddef.h> 64 65 /* 66 * Loadable module info. 67 */ 68 #if (defined(__fibre)) 69 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 70 char _depends_on[] = "misc/scsi drv/fcp"; 71 #else 72 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 73 char _depends_on[] = "misc/scsi"; 74 #endif 75 76 /* 77 * Define the interconnect type, to allow the driver to distinguish 78 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 79 * 80 * This is really for backward compatability. In the future, the driver 81 * should actually check the "interconnect-type" property as reported by 82 * the HBA; however at present this property is not defined by all HBAs, 83 * so we will use this #define (1) to permit the driver to run in 84 * backward-compatability mode; and (2) to print a notification message 85 * if an FC HBA does not support the "interconnect-type" property. The 86 * behavior of the driver will be to assume parallel SCSI behaviors unless 87 * the "interconnect-type" property is defined by the HBA **AND** has a 88 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 89 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 90 * Channel behaviors (as per the old ssd). (Note that the 91 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 92 * will result in the driver assuming parallel SCSI behaviors.) 93 * 94 * (see common/sys/scsi/impl/services.h) 95 * 96 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 97 * since some FC HBAs may already support that, and there is some code in 98 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 99 * default would confuse that code, and besides things should work fine 100 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 101 * "interconnect_type" property. 102 */ 103 #if (defined(__fibre)) 104 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 105 #else 106 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 107 #endif 108 109 /* 110 * The name of the driver, established from the module name in _init. 111 */ 112 static char *sd_label = NULL; 113 114 /* 115 * Driver name is unfortunately prefixed on some driver.conf properties. 116 */ 117 #if (defined(__fibre)) 118 #define sd_max_xfer_size ssd_max_xfer_size 119 #define sd_config_list ssd_config_list 120 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 121 static char *sd_config_list = "ssd-config-list"; 122 #else 123 static char *sd_max_xfer_size = "sd_max_xfer_size"; 124 static char *sd_config_list = "sd-config-list"; 125 #endif 126 127 /* 128 * Driver global variables 129 */ 130 131 #if (defined(__fibre)) 132 /* 133 * These #defines are to avoid namespace collisions that occur because this 134 * code is currently used to compile two seperate driver modules: sd and ssd. 135 * All global variables need to be treated this way (even if declared static) 136 * in order to allow the debugger to resolve the names properly. 137 * It is anticipated that in the near future the ssd module will be obsoleted, 138 * at which time this namespace issue should go away. 139 */ 140 #define sd_state ssd_state 141 #define sd_io_time ssd_io_time 142 #define sd_failfast_enable ssd_failfast_enable 143 #define sd_ua_retry_count ssd_ua_retry_count 144 #define sd_report_pfa ssd_report_pfa 145 #define sd_max_throttle ssd_max_throttle 146 #define sd_min_throttle ssd_min_throttle 147 #define sd_rot_delay ssd_rot_delay 148 149 #define sd_retry_on_reservation_conflict \ 150 ssd_retry_on_reservation_conflict 151 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 152 #define sd_resv_conflict_name ssd_resv_conflict_name 153 154 #define sd_component_mask ssd_component_mask 155 #define sd_level_mask ssd_level_mask 156 #define sd_debug_un ssd_debug_un 157 #define sd_error_level ssd_error_level 158 159 #define sd_xbuf_active_limit ssd_xbuf_active_limit 160 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 161 162 #define sd_tr ssd_tr 163 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 164 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 165 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 166 #define sd_check_media_time ssd_check_media_time 167 #define sd_wait_cmds_complete ssd_wait_cmds_complete 168 #define sd_label_mutex ssd_label_mutex 169 #define sd_detach_mutex ssd_detach_mutex 170 #define sd_log_buf ssd_log_buf 171 #define sd_log_mutex ssd_log_mutex 172 173 #define sd_disk_table ssd_disk_table 174 #define sd_disk_table_size ssd_disk_table_size 175 #define sd_sense_mutex ssd_sense_mutex 176 #define sd_cdbtab ssd_cdbtab 177 178 #define sd_cb_ops ssd_cb_ops 179 #define sd_ops ssd_ops 180 #define sd_additional_codes ssd_additional_codes 181 182 #define sd_minor_data ssd_minor_data 183 #define sd_minor_data_efi ssd_minor_data_efi 184 185 #define sd_tq ssd_tq 186 #define sd_wmr_tq ssd_wmr_tq 187 #define sd_taskq_name ssd_taskq_name 188 #define sd_wmr_taskq_name ssd_wmr_taskq_name 189 #define sd_taskq_minalloc ssd_taskq_minalloc 190 #define sd_taskq_maxalloc ssd_taskq_maxalloc 191 192 #define sd_dump_format_string ssd_dump_format_string 193 194 #define sd_iostart_chain ssd_iostart_chain 195 #define sd_iodone_chain ssd_iodone_chain 196 197 #define sd_pm_idletime ssd_pm_idletime 198 199 #define sd_force_pm_supported ssd_force_pm_supported 200 201 #define sd_dtype_optical_bind ssd_dtype_optical_bind 202 203 #endif 204 205 206 #ifdef SDDEBUG 207 int sd_force_pm_supported = 0; 208 #endif /* SDDEBUG */ 209 210 void *sd_state = NULL; 211 int sd_io_time = SD_IO_TIME; 212 int sd_failfast_enable = 1; 213 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 214 int sd_report_pfa = 1; 215 int sd_max_throttle = SD_MAX_THROTTLE; 216 int sd_min_throttle = SD_MIN_THROTTLE; 217 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 218 int sd_qfull_throttle_enable = TRUE; 219 220 int sd_retry_on_reservation_conflict = 1; 221 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 222 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 223 224 static int sd_dtype_optical_bind = -1; 225 226 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 227 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 228 229 /* 230 * Global data for debug logging. To enable debug printing, sd_component_mask 231 * and sd_level_mask should be set to the desired bit patterns as outlined in 232 * sddef.h. 233 */ 234 uint_t sd_component_mask = 0x0; 235 uint_t sd_level_mask = 0x0; 236 struct sd_lun *sd_debug_un = NULL; 237 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 238 239 /* Note: these may go away in the future... */ 240 static uint32_t sd_xbuf_active_limit = 512; 241 static uint32_t sd_xbuf_reserve_limit = 16; 242 243 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 244 245 /* 246 * Timer value used to reset the throttle after it has been reduced 247 * (typically in response to TRAN_BUSY or STATUS_QFULL) 248 */ 249 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 250 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 251 252 /* 253 * Interval value associated with the media change scsi watch. 254 */ 255 static int sd_check_media_time = 3000000; 256 257 /* 258 * Wait value used for in progress operations during a DDI_SUSPEND 259 */ 260 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 261 262 /* 263 * sd_label_mutex protects a static buffer used in the disk label 264 * component of the driver 265 */ 266 static kmutex_t sd_label_mutex; 267 268 /* 269 * sd_detach_mutex protects un_layer_count, un_detach_count, and 270 * un_opens_in_progress in the sd_lun structure. 271 */ 272 static kmutex_t sd_detach_mutex; 273 274 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 275 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 276 277 /* 278 * Global buffer and mutex for debug logging 279 */ 280 static char sd_log_buf[1024]; 281 static kmutex_t sd_log_mutex; 282 283 284 /* 285 * "Smart" Probe Caching structs, globals, #defines, etc. 286 * For parallel scsi and non-self-identify device only. 287 */ 288 289 /* 290 * The following resources and routines are implemented to support 291 * "smart" probing, which caches the scsi_probe() results in an array, 292 * in order to help avoid long probe times. 293 */ 294 struct sd_scsi_probe_cache { 295 struct sd_scsi_probe_cache *next; 296 dev_info_t *pdip; 297 int cache[NTARGETS_WIDE]; 298 }; 299 300 static kmutex_t sd_scsi_probe_cache_mutex; 301 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 302 303 /* 304 * Really we only need protection on the head of the linked list, but 305 * better safe than sorry. 306 */ 307 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 308 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 309 310 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 311 sd_scsi_probe_cache_head)) 312 313 314 /* 315 * Vendor specific data name property declarations 316 */ 317 318 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 319 320 static sd_tunables seagate_properties = { 321 SEAGATE_THROTTLE_VALUE, 322 0, 323 0, 324 0, 325 0, 326 0, 327 0, 328 0, 329 0 330 }; 331 332 333 static sd_tunables fujitsu_properties = { 334 FUJITSU_THROTTLE_VALUE, 335 0, 336 0, 337 0, 338 0, 339 0, 340 0, 341 0, 342 0 343 }; 344 345 static sd_tunables ibm_properties = { 346 IBM_THROTTLE_VALUE, 347 0, 348 0, 349 0, 350 0, 351 0, 352 0, 353 0, 354 0 355 }; 356 357 static sd_tunables purple_properties = { 358 PURPLE_THROTTLE_VALUE, 359 0, 360 0, 361 PURPLE_BUSY_RETRIES, 362 PURPLE_RESET_RETRY_COUNT, 363 PURPLE_RESERVE_RELEASE_TIME, 364 0, 365 0, 366 0 367 }; 368 369 static sd_tunables sve_properties = { 370 SVE_THROTTLE_VALUE, 371 0, 372 0, 373 SVE_BUSY_RETRIES, 374 SVE_RESET_RETRY_COUNT, 375 SVE_RESERVE_RELEASE_TIME, 376 SVE_MIN_THROTTLE_VALUE, 377 SVE_DISKSORT_DISABLED_FLAG, 378 0 379 }; 380 381 static sd_tunables maserati_properties = { 382 0, 383 0, 384 0, 385 0, 386 0, 387 0, 388 0, 389 MASERATI_DISKSORT_DISABLED_FLAG, 390 MASERATI_LUN_RESET_ENABLED_FLAG 391 }; 392 393 static sd_tunables pirus_properties = { 394 PIRUS_THROTTLE_VALUE, 395 0, 396 PIRUS_NRR_COUNT, 397 PIRUS_BUSY_RETRIES, 398 PIRUS_RESET_RETRY_COUNT, 399 0, 400 PIRUS_MIN_THROTTLE_VALUE, 401 PIRUS_DISKSORT_DISABLED_FLAG, 402 PIRUS_LUN_RESET_ENABLED_FLAG 403 }; 404 405 #endif 406 407 #if (defined(__sparc) && !defined(__fibre)) || \ 408 (defined(__i386) || defined(__amd64)) 409 410 411 static sd_tunables elite_properties = { 412 ELITE_THROTTLE_VALUE, 413 0, 414 0, 415 0, 416 0, 417 0, 418 0, 419 0, 420 0 421 }; 422 423 static sd_tunables st31200n_properties = { 424 ST31200N_THROTTLE_VALUE, 425 0, 426 0, 427 0, 428 0, 429 0, 430 0, 431 0, 432 0 433 }; 434 435 #endif /* Fibre or not */ 436 437 static sd_tunables lsi_properties_scsi = { 438 LSI_THROTTLE_VALUE, 439 0, 440 LSI_NOTREADY_RETRIES, 441 0, 442 0, 443 0, 444 0, 445 0, 446 0 447 }; 448 449 static sd_tunables symbios_properties = { 450 SYMBIOS_THROTTLE_VALUE, 451 0, 452 SYMBIOS_NOTREADY_RETRIES, 453 0, 454 0, 455 0, 456 0, 457 0, 458 0 459 }; 460 461 static sd_tunables lsi_properties = { 462 0, 463 0, 464 LSI_NOTREADY_RETRIES, 465 0, 466 0, 467 0, 468 0, 469 0, 470 0 471 }; 472 473 static sd_tunables lsi_oem_properties = { 474 0, 475 0, 476 LSI_OEM_NOTREADY_RETRIES, 477 0, 478 0, 479 0, 480 0, 481 0, 482 0 483 }; 484 485 486 487 #if (defined(SD_PROP_TST)) 488 489 #define SD_TST_CTYPE_VAL CTYPE_CDROM 490 #define SD_TST_THROTTLE_VAL 16 491 #define SD_TST_NOTREADY_VAL 12 492 #define SD_TST_BUSY_VAL 60 493 #define SD_TST_RST_RETRY_VAL 36 494 #define SD_TST_RSV_REL_TIME 60 495 496 static sd_tunables tst_properties = { 497 SD_TST_THROTTLE_VAL, 498 SD_TST_CTYPE_VAL, 499 SD_TST_NOTREADY_VAL, 500 SD_TST_BUSY_VAL, 501 SD_TST_RST_RETRY_VAL, 502 SD_TST_RSV_REL_TIME, 503 0, 504 0, 505 0 506 }; 507 #endif 508 509 /* This is similiar to the ANSI toupper implementation */ 510 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 511 512 /* 513 * Static Driver Configuration Table 514 * 515 * This is the table of disks which need throttle adjustment (or, perhaps 516 * something else as defined by the flags at a future time.) device_id 517 * is a string consisting of concatenated vid (vendor), pid (product/model) 518 * and revision strings as defined in the scsi_inquiry structure. Offsets of 519 * the parts of the string are as defined by the sizes in the scsi_inquiry 520 * structure. Device type is searched as far as the device_id string is 521 * defined. Flags defines which values are to be set in the driver from the 522 * properties list. 523 * 524 * Entries below which begin and end with a "*" are a special case. 525 * These do not have a specific vendor, and the string which follows 526 * can appear anywhere in the 16 byte PID portion of the inquiry data. 527 * 528 * Entries below which begin and end with a " " (blank) are a special 529 * case. The comparison function will treat multiple consecutive blanks 530 * as equivalent to a single blank. For example, this causes a 531 * sd_disk_table entry of " NEC CDROM " to match a device's id string 532 * of "NEC CDROM". 533 * 534 * Note: The MD21 controller type has been obsoleted. 535 * ST318202F is a Legacy device 536 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 537 * made with an FC connection. The entries here are a legacy. 538 */ 539 static sd_disk_config_t sd_disk_table[] = { 540 #if defined(__fibre) || defined(__i386) || defined(__amd64) 541 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 542 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 543 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 544 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 545 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 546 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 547 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 548 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 549 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 550 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 551 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 552 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 553 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 554 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 555 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 556 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 557 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 558 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 559 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 560 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 561 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 562 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 563 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 564 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 565 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 566 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 567 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 568 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 569 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 570 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 571 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 572 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 573 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 574 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 575 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 576 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 577 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 578 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 579 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 580 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 581 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 582 { "SUN T3", SD_CONF_BSET_THROTTLE | 583 SD_CONF_BSET_BSY_RETRY_COUNT| 584 SD_CONF_BSET_RST_RETRIES| 585 SD_CONF_BSET_RSV_REL_TIME, 586 &purple_properties }, 587 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 588 SD_CONF_BSET_BSY_RETRY_COUNT| 589 SD_CONF_BSET_RST_RETRIES| 590 SD_CONF_BSET_RSV_REL_TIME| 591 SD_CONF_BSET_MIN_THROTTLE| 592 SD_CONF_BSET_DISKSORT_DISABLED, 593 &sve_properties }, 594 { "SUN T4", SD_CONF_BSET_THROTTLE | 595 SD_CONF_BSET_BSY_RETRY_COUNT| 596 SD_CONF_BSET_RST_RETRIES| 597 SD_CONF_BSET_RSV_REL_TIME, 598 &purple_properties }, 599 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 600 SD_CONF_BSET_LUN_RESET_ENABLED, 601 &maserati_properties }, 602 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 603 SD_CONF_BSET_NRR_COUNT| 604 SD_CONF_BSET_BSY_RETRY_COUNT| 605 SD_CONF_BSET_RST_RETRIES| 606 SD_CONF_BSET_MIN_THROTTLE| 607 SD_CONF_BSET_DISKSORT_DISABLED| 608 SD_CONF_BSET_LUN_RESET_ENABLED, 609 &pirus_properties }, 610 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 611 SD_CONF_BSET_NRR_COUNT| 612 SD_CONF_BSET_BSY_RETRY_COUNT| 613 SD_CONF_BSET_RST_RETRIES| 614 SD_CONF_BSET_MIN_THROTTLE| 615 SD_CONF_BSET_DISKSORT_DISABLED| 616 SD_CONF_BSET_LUN_RESET_ENABLED, 617 &pirus_properties }, 618 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 619 SD_CONF_BSET_NRR_COUNT| 620 SD_CONF_BSET_BSY_RETRY_COUNT| 621 SD_CONF_BSET_RST_RETRIES| 622 SD_CONF_BSET_MIN_THROTTLE| 623 SD_CONF_BSET_DISKSORT_DISABLED| 624 SD_CONF_BSET_LUN_RESET_ENABLED, 625 &pirus_properties }, 626 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 627 SD_CONF_BSET_NRR_COUNT| 628 SD_CONF_BSET_BSY_RETRY_COUNT| 629 SD_CONF_BSET_RST_RETRIES| 630 SD_CONF_BSET_MIN_THROTTLE| 631 SD_CONF_BSET_DISKSORT_DISABLED| 632 SD_CONF_BSET_LUN_RESET_ENABLED, 633 &pirus_properties }, 634 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 635 SD_CONF_BSET_NRR_COUNT| 636 SD_CONF_BSET_BSY_RETRY_COUNT| 637 SD_CONF_BSET_RST_RETRIES| 638 SD_CONF_BSET_MIN_THROTTLE| 639 SD_CONF_BSET_DISKSORT_DISABLED| 640 SD_CONF_BSET_LUN_RESET_ENABLED, 641 &pirus_properties }, 642 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 643 SD_CONF_BSET_NRR_COUNT| 644 SD_CONF_BSET_BSY_RETRY_COUNT| 645 SD_CONF_BSET_RST_RETRIES| 646 SD_CONF_BSET_MIN_THROTTLE| 647 SD_CONF_BSET_DISKSORT_DISABLED| 648 SD_CONF_BSET_LUN_RESET_ENABLED, 649 &pirus_properties }, 650 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 653 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 654 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 655 #endif /* fibre or NON-sparc platforms */ 656 #if ((defined(__sparc) && !defined(__fibre)) ||\ 657 (defined(__i386) || defined(__amd64))) 658 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 659 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 660 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 661 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 662 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 663 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 664 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 665 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 666 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 667 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 668 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 669 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 670 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 671 &symbios_properties }, 672 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 673 &lsi_properties_scsi }, 674 #if defined(__i386) || defined(__amd64) 675 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 676 | SD_CONF_BSET_READSUB_BCD 677 | SD_CONF_BSET_READ_TOC_ADDR_BCD 678 | SD_CONF_BSET_NO_READ_HEADER 679 | SD_CONF_BSET_READ_CD_XD4), NULL }, 680 681 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 682 | SD_CONF_BSET_READSUB_BCD 683 | SD_CONF_BSET_READ_TOC_ADDR_BCD 684 | SD_CONF_BSET_NO_READ_HEADER 685 | SD_CONF_BSET_READ_CD_XD4), NULL }, 686 #endif /* __i386 || __amd64 */ 687 #endif /* sparc NON-fibre or NON-sparc platforms */ 688 689 #if (defined(SD_PROP_TST)) 690 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 691 | SD_CONF_BSET_CTYPE 692 | SD_CONF_BSET_NRR_COUNT 693 | SD_CONF_BSET_FAB_DEVID 694 | SD_CONF_BSET_NOCACHE 695 | SD_CONF_BSET_BSY_RETRY_COUNT 696 | SD_CONF_BSET_PLAYMSF_BCD 697 | SD_CONF_BSET_READSUB_BCD 698 | SD_CONF_BSET_READ_TOC_TRK_BCD 699 | SD_CONF_BSET_READ_TOC_ADDR_BCD 700 | SD_CONF_BSET_NO_READ_HEADER 701 | SD_CONF_BSET_READ_CD_XD4 702 | SD_CONF_BSET_RST_RETRIES 703 | SD_CONF_BSET_RSV_REL_TIME 704 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 705 #endif 706 }; 707 708 static const int sd_disk_table_size = 709 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 710 711 712 /* 713 * Return codes of sd_uselabel(). 714 */ 715 #define SD_LABEL_IS_VALID 0 716 #define SD_LABEL_IS_INVALID 1 717 718 #define SD_INTERCONNECT_PARALLEL 0 719 #define SD_INTERCONNECT_FABRIC 1 720 #define SD_INTERCONNECT_FIBRE 2 721 #define SD_INTERCONNECT_SSA 3 722 #define SD_IS_PARALLEL_SCSI(un) \ 723 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 724 725 /* 726 * Definitions used by device id registration routines 727 */ 728 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 729 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 730 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 731 #define WD_NODE 7 /* the whole disk minor */ 732 733 static kmutex_t sd_sense_mutex = {0}; 734 735 /* 736 * Macros for updates of the driver state 737 */ 738 #define New_state(un, s) \ 739 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 740 #define Restore_state(un) \ 741 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 742 743 static struct sd_cdbinfo sd_cdbtab[] = { 744 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 745 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 746 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 747 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 748 }; 749 750 /* 751 * Specifies the number of seconds that must have elapsed since the last 752 * cmd. has completed for a device to be declared idle to the PM framework. 753 */ 754 static int sd_pm_idletime = 1; 755 756 /* 757 * Internal function prototypes 758 */ 759 760 #if (defined(__fibre)) 761 /* 762 * These #defines are to avoid namespace collisions that occur because this 763 * code is currently used to compile two seperate driver modules: sd and ssd. 764 * All function names need to be treated this way (even if declared static) 765 * in order to allow the debugger to resolve the names properly. 766 * It is anticipated that in the near future the ssd module will be obsoleted, 767 * at which time this ugliness should go away. 768 */ 769 #define sd_log_trace ssd_log_trace 770 #define sd_log_info ssd_log_info 771 #define sd_log_err ssd_log_err 772 #define sdprobe ssdprobe 773 #define sdinfo ssdinfo 774 #define sd_prop_op ssd_prop_op 775 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 776 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 777 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 778 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 779 #define sd_spin_up_unit ssd_spin_up_unit 780 #define sd_enable_descr_sense ssd_enable_descr_sense 781 #define sd_set_mmc_caps ssd_set_mmc_caps 782 #define sd_read_unit_properties ssd_read_unit_properties 783 #define sd_process_sdconf_file ssd_process_sdconf_file 784 #define sd_process_sdconf_table ssd_process_sdconf_table 785 #define sd_sdconf_id_match ssd_sdconf_id_match 786 #define sd_blank_cmp ssd_blank_cmp 787 #define sd_chk_vers1_data ssd_chk_vers1_data 788 #define sd_set_vers1_properties ssd_set_vers1_properties 789 #define sd_validate_geometry ssd_validate_geometry 790 791 #if defined(_SUNOS_VTOC_16) 792 #define sd_convert_geometry ssd_convert_geometry 793 #endif 794 795 #define sd_resync_geom_caches ssd_resync_geom_caches 796 #define sd_read_fdisk ssd_read_fdisk 797 #define sd_get_physical_geometry ssd_get_physical_geometry 798 #define sd_get_virtual_geometry ssd_get_virtual_geometry 799 #define sd_update_block_info ssd_update_block_info 800 #define sd_swap_efi_gpt ssd_swap_efi_gpt 801 #define sd_swap_efi_gpe ssd_swap_efi_gpe 802 #define sd_validate_efi ssd_validate_efi 803 #define sd_use_efi ssd_use_efi 804 #define sd_uselabel ssd_uselabel 805 #define sd_build_default_label ssd_build_default_label 806 #define sd_has_max_chs_vals ssd_has_max_chs_vals 807 #define sd_inq_fill ssd_inq_fill 808 #define sd_register_devid ssd_register_devid 809 #define sd_get_devid_block ssd_get_devid_block 810 #define sd_get_devid ssd_get_devid 811 #define sd_create_devid ssd_create_devid 812 #define sd_write_deviceid ssd_write_deviceid 813 #define sd_check_vpd_page_support ssd_check_vpd_page_support 814 #define sd_setup_pm ssd_setup_pm 815 #define sd_create_pm_components ssd_create_pm_components 816 #define sd_ddi_suspend ssd_ddi_suspend 817 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 818 #define sd_ddi_resume ssd_ddi_resume 819 #define sd_ddi_pm_resume ssd_ddi_pm_resume 820 #define sdpower ssdpower 821 #define sdattach ssdattach 822 #define sddetach ssddetach 823 #define sd_unit_attach ssd_unit_attach 824 #define sd_unit_detach ssd_unit_detach 825 #define sd_create_minor_nodes ssd_create_minor_nodes 826 #define sd_create_errstats ssd_create_errstats 827 #define sd_set_errstats ssd_set_errstats 828 #define sd_set_pstats ssd_set_pstats 829 #define sddump ssddump 830 #define sd_scsi_poll ssd_scsi_poll 831 #define sd_send_polled_RQS ssd_send_polled_RQS 832 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 833 #define sd_init_event_callbacks ssd_init_event_callbacks 834 #define sd_event_callback ssd_event_callback 835 #define sd_disable_caching ssd_disable_caching 836 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 837 #define sd_make_device ssd_make_device 838 #define sdopen ssdopen 839 #define sdclose ssdclose 840 #define sd_ready_and_valid ssd_ready_and_valid 841 #define sdmin ssdmin 842 #define sdread ssdread 843 #define sdwrite ssdwrite 844 #define sdaread ssdaread 845 #define sdawrite ssdawrite 846 #define sdstrategy ssdstrategy 847 #define sdioctl ssdioctl 848 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 849 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 850 #define sd_checksum_iostart ssd_checksum_iostart 851 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 852 #define sd_pm_iostart ssd_pm_iostart 853 #define sd_core_iostart ssd_core_iostart 854 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 855 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 856 #define sd_checksum_iodone ssd_checksum_iodone 857 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 858 #define sd_pm_iodone ssd_pm_iodone 859 #define sd_initpkt_for_buf ssd_initpkt_for_buf 860 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 861 #define sd_setup_rw_pkt ssd_setup_rw_pkt 862 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 863 #define sd_buf_iodone ssd_buf_iodone 864 #define sd_uscsi_strategy ssd_uscsi_strategy 865 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 866 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 867 #define sd_uscsi_iodone ssd_uscsi_iodone 868 #define sd_xbuf_strategy ssd_xbuf_strategy 869 #define sd_xbuf_init ssd_xbuf_init 870 #define sd_pm_entry ssd_pm_entry 871 #define sd_pm_exit ssd_pm_exit 872 873 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 874 #define sd_pm_timeout_handler ssd_pm_timeout_handler 875 876 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 877 #define sdintr ssdintr 878 #define sd_start_cmds ssd_start_cmds 879 #define sd_send_scsi_cmd ssd_send_scsi_cmd 880 #define sd_bioclone_alloc ssd_bioclone_alloc 881 #define sd_bioclone_free ssd_bioclone_free 882 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 883 #define sd_shadow_buf_free ssd_shadow_buf_free 884 #define sd_print_transport_rejected_message \ 885 ssd_print_transport_rejected_message 886 #define sd_retry_command ssd_retry_command 887 #define sd_set_retry_bp ssd_set_retry_bp 888 #define sd_send_request_sense_command ssd_send_request_sense_command 889 #define sd_start_retry_command ssd_start_retry_command 890 #define sd_start_direct_priority_command \ 891 ssd_start_direct_priority_command 892 #define sd_return_failed_command ssd_return_failed_command 893 #define sd_return_failed_command_no_restart \ 894 ssd_return_failed_command_no_restart 895 #define sd_return_command ssd_return_command 896 #define sd_sync_with_callback ssd_sync_with_callback 897 #define sdrunout ssdrunout 898 #define sd_mark_rqs_busy ssd_mark_rqs_busy 899 #define sd_mark_rqs_idle ssd_mark_rqs_idle 900 #define sd_reduce_throttle ssd_reduce_throttle 901 #define sd_restore_throttle ssd_restore_throttle 902 #define sd_print_incomplete_msg ssd_print_incomplete_msg 903 #define sd_init_cdb_limits ssd_init_cdb_limits 904 #define sd_pkt_status_good ssd_pkt_status_good 905 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 906 #define sd_pkt_status_busy ssd_pkt_status_busy 907 #define sd_pkt_status_reservation_conflict \ 908 ssd_pkt_status_reservation_conflict 909 #define sd_pkt_status_qfull ssd_pkt_status_qfull 910 #define sd_handle_request_sense ssd_handle_request_sense 911 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 912 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 913 #define sd_validate_sense_data ssd_validate_sense_data 914 #define sd_decode_sense ssd_decode_sense 915 #define sd_print_sense_msg ssd_print_sense_msg 916 #define sd_extract_sense_info_descr ssd_extract_sense_info_descr 917 #define sd_sense_key_no_sense ssd_sense_key_no_sense 918 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 919 #define sd_sense_key_not_ready ssd_sense_key_not_ready 920 #define sd_sense_key_medium_or_hardware_error \ 921 ssd_sense_key_medium_or_hardware_error 922 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 923 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 924 #define sd_sense_key_fail_command ssd_sense_key_fail_command 925 #define sd_sense_key_blank_check ssd_sense_key_blank_check 926 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 927 #define sd_sense_key_default ssd_sense_key_default 928 #define sd_print_retry_msg ssd_print_retry_msg 929 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 930 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 931 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 932 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 933 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 934 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 935 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 936 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 937 #define sd_pkt_reason_default ssd_pkt_reason_default 938 #define sd_reset_target ssd_reset_target 939 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 940 #define sd_start_stop_unit_task ssd_start_stop_unit_task 941 #define sd_taskq_create ssd_taskq_create 942 #define sd_taskq_delete ssd_taskq_delete 943 #define sd_media_change_task ssd_media_change_task 944 #define sd_handle_mchange ssd_handle_mchange 945 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 946 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 947 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 948 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 949 #define sd_send_scsi_feature_GET_CONFIGURATION \ 950 sd_send_scsi_feature_GET_CONFIGURATION 951 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 952 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 953 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 954 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 955 ssd_send_scsi_PERSISTENT_RESERVE_IN 956 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 957 ssd_send_scsi_PERSISTENT_RESERVE_OUT 958 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 959 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 960 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 961 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 962 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 963 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 964 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 965 #define sd_alloc_rqs ssd_alloc_rqs 966 #define sd_free_rqs ssd_free_rqs 967 #define sd_dump_memory ssd_dump_memory 968 #define sd_uscsi_ioctl ssd_uscsi_ioctl 969 #define sd_get_media_info ssd_get_media_info 970 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 971 #define sd_dkio_get_geometry ssd_dkio_get_geometry 972 #define sd_dkio_set_geometry ssd_dkio_set_geometry 973 #define sd_dkio_get_partition ssd_dkio_get_partition 974 #define sd_dkio_set_partition ssd_dkio_set_partition 975 #define sd_dkio_partition ssd_dkio_partition 976 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 977 #define sd_dkio_get_efi ssd_dkio_get_efi 978 #define sd_build_user_vtoc ssd_build_user_vtoc 979 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 980 #define sd_dkio_set_efi ssd_dkio_set_efi 981 #define sd_build_label_vtoc ssd_build_label_vtoc 982 #define sd_write_label ssd_write_label 983 #define sd_clear_vtoc ssd_clear_vtoc 984 #define sd_clear_efi ssd_clear_efi 985 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 986 #define sd_setup_next_xfer ssd_setup_next_xfer 987 #define sd_dkio_get_temp ssd_dkio_get_temp 988 #define sd_dkio_get_mboot ssd_dkio_get_mboot 989 #define sd_dkio_set_mboot ssd_dkio_set_mboot 990 #define sd_setup_default_geometry ssd_setup_default_geometry 991 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 992 #define sd_check_mhd ssd_check_mhd 993 #define sd_mhd_watch_cb ssd_mhd_watch_cb 994 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 995 #define sd_sname ssd_sname 996 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 997 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 998 #define sd_take_ownership ssd_take_ownership 999 #define sd_reserve_release ssd_reserve_release 1000 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1001 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1002 #define sd_persistent_reservation_in_read_keys \ 1003 ssd_persistent_reservation_in_read_keys 1004 #define sd_persistent_reservation_in_read_resv \ 1005 ssd_persistent_reservation_in_read_resv 1006 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1007 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1008 #define sd_mhdioc_release ssd_mhdioc_release 1009 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1010 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1011 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1012 #define sr_change_blkmode ssr_change_blkmode 1013 #define sr_change_speed ssr_change_speed 1014 #define sr_atapi_change_speed ssr_atapi_change_speed 1015 #define sr_pause_resume ssr_pause_resume 1016 #define sr_play_msf ssr_play_msf 1017 #define sr_play_trkind ssr_play_trkind 1018 #define sr_read_all_subcodes ssr_read_all_subcodes 1019 #define sr_read_subchannel ssr_read_subchannel 1020 #define sr_read_tocentry ssr_read_tocentry 1021 #define sr_read_tochdr ssr_read_tochdr 1022 #define sr_read_cdda ssr_read_cdda 1023 #define sr_read_cdxa ssr_read_cdxa 1024 #define sr_read_mode1 ssr_read_mode1 1025 #define sr_read_mode2 ssr_read_mode2 1026 #define sr_read_cd_mode2 ssr_read_cd_mode2 1027 #define sr_sector_mode ssr_sector_mode 1028 #define sr_eject ssr_eject 1029 #define sr_ejected ssr_ejected 1030 #define sr_check_wp ssr_check_wp 1031 #define sd_check_media ssd_check_media 1032 #define sd_media_watch_cb ssd_media_watch_cb 1033 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1034 #define sr_volume_ctrl ssr_volume_ctrl 1035 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1036 #define sd_log_page_supported ssd_log_page_supported 1037 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1038 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1039 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1040 #define sd_range_lock ssd_range_lock 1041 #define sd_get_range ssd_get_range 1042 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1043 #define sd_range_unlock ssd_range_unlock 1044 #define sd_read_modify_write_task ssd_read_modify_write_task 1045 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1046 1047 #define sd_iostart_chain ssd_iostart_chain 1048 #define sd_iodone_chain ssd_iodone_chain 1049 #define sd_initpkt_map ssd_initpkt_map 1050 #define sd_destroypkt_map ssd_destroypkt_map 1051 #define sd_chain_type_map ssd_chain_type_map 1052 #define sd_chain_index_map ssd_chain_index_map 1053 1054 #define sd_failfast_flushctl ssd_failfast_flushctl 1055 #define sd_failfast_flushq ssd_failfast_flushq 1056 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1057 1058 #define sd_is_lsi ssd_is_lsi 1059 1060 #endif /* #if (defined(__fibre)) */ 1061 1062 1063 int _init(void); 1064 int _fini(void); 1065 int _info(struct modinfo *modinfop); 1066 1067 /*PRINTFLIKE3*/ 1068 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1069 /*PRINTFLIKE3*/ 1070 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1071 /*PRINTFLIKE3*/ 1072 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1073 1074 static int sdprobe(dev_info_t *devi); 1075 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1076 void **result); 1077 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1078 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1079 1080 /* 1081 * Smart probe for parallel scsi 1082 */ 1083 static void sd_scsi_probe_cache_init(void); 1084 static void sd_scsi_probe_cache_fini(void); 1085 static void sd_scsi_clear_probe_cache(void); 1086 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1087 1088 static int sd_spin_up_unit(struct sd_lun *un); 1089 #ifdef _LP64 1090 static void sd_enable_descr_sense(struct sd_lun *un); 1091 #endif /* _LP64 */ 1092 static void sd_set_mmc_caps(struct sd_lun *un); 1093 1094 static void sd_read_unit_properties(struct sd_lun *un); 1095 static int sd_process_sdconf_file(struct sd_lun *un); 1096 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1097 int *data_list, sd_tunables *values); 1098 static void sd_process_sdconf_table(struct sd_lun *un); 1099 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1100 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1101 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1102 int list_len, char *dataname_ptr); 1103 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1104 sd_tunables *prop_list); 1105 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1106 1107 #if defined(_SUNOS_VTOC_16) 1108 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1109 #endif 1110 1111 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1112 int path_flag); 1113 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1114 int path_flag); 1115 static void sd_get_physical_geometry(struct sd_lun *un, 1116 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1117 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1118 int lbasize); 1119 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1120 static void sd_swap_efi_gpt(efi_gpt_t *); 1121 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1122 static int sd_validate_efi(efi_gpt_t *); 1123 static int sd_use_efi(struct sd_lun *, int); 1124 static void sd_build_default_label(struct sd_lun *un); 1125 1126 #if defined(_FIRMWARE_NEEDS_FDISK) 1127 static int sd_has_max_chs_vals(struct ipart *fdp); 1128 #endif 1129 static void sd_inq_fill(char *p, int l, char *s); 1130 1131 1132 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1133 int reservation_flag); 1134 static daddr_t sd_get_devid_block(struct sd_lun *un); 1135 static int sd_get_devid(struct sd_lun *un); 1136 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1137 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1138 static int sd_write_deviceid(struct sd_lun *un); 1139 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1140 static int sd_check_vpd_page_support(struct sd_lun *un); 1141 1142 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1143 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1144 1145 static int sd_ddi_suspend(dev_info_t *devi); 1146 static int sd_ddi_pm_suspend(struct sd_lun *un); 1147 static int sd_ddi_resume(dev_info_t *devi); 1148 static int sd_ddi_pm_resume(struct sd_lun *un); 1149 static int sdpower(dev_info_t *devi, int component, int level); 1150 1151 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1152 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1153 static int sd_unit_attach(dev_info_t *devi); 1154 static int sd_unit_detach(dev_info_t *devi); 1155 1156 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1157 static void sd_create_errstats(struct sd_lun *un, int instance); 1158 static void sd_set_errstats(struct sd_lun *un); 1159 static void sd_set_pstats(struct sd_lun *un); 1160 1161 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1162 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1163 static int sd_send_polled_RQS(struct sd_lun *un); 1164 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1165 1166 #if (defined(__fibre)) 1167 /* 1168 * Event callbacks (photon) 1169 */ 1170 static void sd_init_event_callbacks(struct sd_lun *un); 1171 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1172 #endif 1173 1174 1175 static int sd_disable_caching(struct sd_lun *un); 1176 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1177 static dev_t sd_make_device(dev_info_t *devi); 1178 1179 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1180 uint64_t capacity); 1181 1182 /* 1183 * Driver entry point functions. 1184 */ 1185 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1186 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1187 static int sd_ready_and_valid(struct sd_lun *un); 1188 1189 static void sdmin(struct buf *bp); 1190 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1191 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1192 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1193 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1194 1195 static int sdstrategy(struct buf *bp); 1196 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1197 1198 /* 1199 * Function prototypes for layering functions in the iostart chain. 1200 */ 1201 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1202 struct buf *bp); 1203 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1204 struct buf *bp); 1205 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1206 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1207 struct buf *bp); 1208 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1209 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1210 1211 /* 1212 * Function prototypes for layering functions in the iodone chain. 1213 */ 1214 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1215 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1216 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1217 struct buf *bp); 1218 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1219 struct buf *bp); 1220 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1221 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1222 struct buf *bp); 1223 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1224 1225 /* 1226 * Prototypes for functions to support buf(9S) based IO. 1227 */ 1228 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1229 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1230 static void sd_destroypkt_for_buf(struct buf *); 1231 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1232 struct buf *bp, int flags, 1233 int (*callback)(caddr_t), caddr_t callback_arg, 1234 diskaddr_t lba, uint32_t blockcount); 1235 #if defined(__i386) || defined(__amd64) 1236 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1237 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1238 #endif /* defined(__i386) || defined(__amd64) */ 1239 1240 /* 1241 * Prototypes for functions to support USCSI IO. 1242 */ 1243 static int sd_uscsi_strategy(struct buf *bp); 1244 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1245 static void sd_destroypkt_for_uscsi(struct buf *); 1246 1247 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1248 uchar_t chain_type, void *pktinfop); 1249 1250 static int sd_pm_entry(struct sd_lun *un); 1251 static void sd_pm_exit(struct sd_lun *un); 1252 1253 static void sd_pm_idletimeout_handler(void *arg); 1254 1255 /* 1256 * sd_core internal functions (used at the sd_core_io layer). 1257 */ 1258 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1259 static void sdintr(struct scsi_pkt *pktp); 1260 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1261 1262 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1263 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1264 int path_flag); 1265 1266 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1267 daddr_t blkno, int (*func)(struct buf *)); 1268 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1269 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1270 static void sd_bioclone_free(struct buf *bp); 1271 static void sd_shadow_buf_free(struct buf *bp); 1272 1273 static void sd_print_transport_rejected_message(struct sd_lun *un, 1274 struct sd_xbuf *xp, int code); 1275 1276 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1277 int retry_check_flag, 1278 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1279 int c), 1280 void *user_arg, int failure_code, clock_t retry_delay, 1281 void (*statp)(kstat_io_t *)); 1282 1283 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1284 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1285 1286 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1287 struct scsi_pkt *pktp); 1288 static void sd_start_retry_command(void *arg); 1289 static void sd_start_direct_priority_command(void *arg); 1290 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1291 int errcode); 1292 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1293 struct buf *bp, int errcode); 1294 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1295 static void sd_sync_with_callback(struct sd_lun *un); 1296 static int sdrunout(caddr_t arg); 1297 1298 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1299 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1300 1301 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1302 static void sd_restore_throttle(void *arg); 1303 1304 static void sd_init_cdb_limits(struct sd_lun *un); 1305 1306 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1307 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1308 1309 /* 1310 * Error handling functions 1311 */ 1312 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1313 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1314 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1315 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1316 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1317 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1318 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1319 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1320 1321 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1322 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1323 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1324 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1325 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1326 struct sd_xbuf *xp); 1327 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1328 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1329 1330 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1331 void *arg, int code); 1332 static diskaddr_t sd_extract_sense_info_descr( 1333 struct scsi_descr_sense_hdr *sdsp); 1334 1335 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1336 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1337 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1338 uint8_t asc, 1339 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1340 static void sd_sense_key_not_ready(struct sd_lun *un, 1341 uint8_t asc, uint8_t ascq, 1342 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1343 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1344 int sense_key, uint8_t asc, 1345 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1346 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1347 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1348 static void sd_sense_key_unit_attention(struct sd_lun *un, 1349 uint8_t asc, 1350 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1351 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1352 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1353 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1354 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1355 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1356 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1357 static void sd_sense_key_default(struct sd_lun *un, 1358 int sense_key, 1359 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1360 1361 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1362 void *arg, int flag); 1363 1364 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1365 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1366 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1367 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1368 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1369 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1370 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1371 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1372 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1373 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1374 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1375 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1376 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1377 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1378 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1379 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1380 1381 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1382 1383 static void sd_start_stop_unit_callback(void *arg); 1384 static void sd_start_stop_unit_task(void *arg); 1385 1386 static void sd_taskq_create(void); 1387 static void sd_taskq_delete(void); 1388 static void sd_media_change_task(void *arg); 1389 1390 static int sd_handle_mchange(struct sd_lun *un); 1391 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1392 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1393 uint32_t *lbap, int path_flag); 1394 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1395 uint32_t *lbap, int path_flag); 1396 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1397 int path_flag); 1398 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1399 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1400 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1401 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1402 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1403 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1404 uchar_t usr_cmd, uchar_t *usr_bufp); 1405 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1406 struct dk_callback *dkc); 1407 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1408 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1409 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1410 uchar_t *bufaddr, uint_t buflen); 1411 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1412 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1413 uchar_t *bufaddr, uint_t buflen, char feature); 1414 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1415 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1416 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1417 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1418 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1419 size_t buflen, daddr_t start_block, int path_flag); 1420 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1421 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1422 path_flag) 1423 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1424 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1425 path_flag) 1426 1427 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1428 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1429 uint16_t param_ptr, int path_flag); 1430 1431 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1432 static void sd_free_rqs(struct sd_lun *un); 1433 1434 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1435 uchar_t *data, int len, int fmt); 1436 static void sd_panic_for_res_conflict(struct sd_lun *un); 1437 1438 /* 1439 * Disk Ioctl Function Prototypes 1440 */ 1441 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1442 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1443 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1444 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1445 int geom_validated); 1446 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1447 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1448 int geom_validated); 1449 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1450 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1451 int geom_validated); 1452 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1453 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1454 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1455 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1456 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1457 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1458 static int sd_write_label(dev_t dev); 1459 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1460 static void sd_clear_vtoc(struct sd_lun *un); 1461 static void sd_clear_efi(struct sd_lun *un); 1462 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1463 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1464 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1465 static void sd_setup_default_geometry(struct sd_lun *un); 1466 #if defined(__i386) || defined(__amd64) 1467 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1468 #endif 1469 1470 /* 1471 * Multi-host Ioctl Prototypes 1472 */ 1473 static int sd_check_mhd(dev_t dev, int interval); 1474 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1475 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1476 static char *sd_sname(uchar_t status); 1477 static void sd_mhd_resvd_recover(void *arg); 1478 static void sd_resv_reclaim_thread(); 1479 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1480 static int sd_reserve_release(dev_t dev, int cmd); 1481 static void sd_rmv_resv_reclaim_req(dev_t dev); 1482 static void sd_mhd_reset_notify_cb(caddr_t arg); 1483 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1484 mhioc_inkeys_t *usrp, int flag); 1485 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1486 mhioc_inresvs_t *usrp, int flag); 1487 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1488 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1489 static int sd_mhdioc_release(dev_t dev); 1490 static int sd_mhdioc_register_devid(dev_t dev); 1491 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1492 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1493 1494 /* 1495 * SCSI removable prototypes 1496 */ 1497 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1498 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1499 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1500 static int sr_pause_resume(dev_t dev, int mode); 1501 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1502 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1503 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1504 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1505 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1506 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1507 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1508 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1509 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1510 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1511 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1512 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1513 static int sr_eject(dev_t dev); 1514 static void sr_ejected(register struct sd_lun *un); 1515 static int sr_check_wp(dev_t dev); 1516 static int sd_check_media(dev_t dev, enum dkio_state state); 1517 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1518 static void sd_delayed_cv_broadcast(void *arg); 1519 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1520 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1521 1522 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1523 1524 /* 1525 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1526 */ 1527 static void sd_check_for_writable_cd(struct sd_lun *un); 1528 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1529 static void sd_wm_cache_destructor(void *wm, void *un); 1530 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1531 daddr_t endb, ushort_t typ); 1532 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1533 daddr_t endb); 1534 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1535 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1536 static void sd_read_modify_write_task(void * arg); 1537 static int 1538 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1539 struct buf **bpp); 1540 1541 1542 /* 1543 * Function prototypes for failfast support. 1544 */ 1545 static void sd_failfast_flushq(struct sd_lun *un); 1546 static int sd_failfast_flushq_callback(struct buf *bp); 1547 1548 /* 1549 * Function prototypes to check for lsi devices 1550 */ 1551 static void sd_is_lsi(struct sd_lun *un); 1552 1553 /* 1554 * Function prototypes for x86 support 1555 */ 1556 #if defined(__i386) || defined(__amd64) 1557 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1558 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1559 #endif 1560 1561 /* 1562 * Constants for failfast support: 1563 * 1564 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1565 * failfast processing being performed. 1566 * 1567 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1568 * failfast processing on all bufs with B_FAILFAST set. 1569 */ 1570 1571 #define SD_FAILFAST_INACTIVE 0 1572 #define SD_FAILFAST_ACTIVE 1 1573 1574 /* 1575 * Bitmask to control behavior of buf(9S) flushes when a transition to 1576 * the failfast state occurs. Optional bits include: 1577 * 1578 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1579 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1580 * be flushed. 1581 * 1582 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1583 * driver, in addition to the regular wait queue. This includes the xbuf 1584 * queues. When clear, only the driver's wait queue will be flushed. 1585 */ 1586 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1587 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1588 1589 /* 1590 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1591 * to flush all queues within the driver. 1592 */ 1593 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1594 1595 1596 /* 1597 * SD Testing Fault Injection 1598 */ 1599 #ifdef SD_FAULT_INJECTION 1600 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1601 static void sd_faultinjection(struct scsi_pkt *pktp); 1602 static void sd_injection_log(char *buf, struct sd_lun *un); 1603 #endif 1604 1605 /* 1606 * Device driver ops vector 1607 */ 1608 static struct cb_ops sd_cb_ops = { 1609 sdopen, /* open */ 1610 sdclose, /* close */ 1611 sdstrategy, /* strategy */ 1612 nodev, /* print */ 1613 sddump, /* dump */ 1614 sdread, /* read */ 1615 sdwrite, /* write */ 1616 sdioctl, /* ioctl */ 1617 nodev, /* devmap */ 1618 nodev, /* mmap */ 1619 nodev, /* segmap */ 1620 nochpoll, /* poll */ 1621 sd_prop_op, /* cb_prop_op */ 1622 0, /* streamtab */ 1623 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1624 CB_REV, /* cb_rev */ 1625 sdaread, /* async I/O read entry point */ 1626 sdawrite /* async I/O write entry point */ 1627 }; 1628 1629 static struct dev_ops sd_ops = { 1630 DEVO_REV, /* devo_rev, */ 1631 0, /* refcnt */ 1632 sdinfo, /* info */ 1633 nulldev, /* identify */ 1634 sdprobe, /* probe */ 1635 sdattach, /* attach */ 1636 sddetach, /* detach */ 1637 nodev, /* reset */ 1638 &sd_cb_ops, /* driver operations */ 1639 NULL, /* bus operations */ 1640 sdpower /* power */ 1641 }; 1642 1643 1644 /* 1645 * This is the loadable module wrapper. 1646 */ 1647 #include <sys/modctl.h> 1648 1649 static struct modldrv modldrv = { 1650 &mod_driverops, /* Type of module. This one is a driver */ 1651 SD_MODULE_NAME, /* Module name. */ 1652 &sd_ops /* driver ops */ 1653 }; 1654 1655 1656 static struct modlinkage modlinkage = { 1657 MODREV_1, 1658 &modldrv, 1659 NULL 1660 }; 1661 1662 1663 static struct scsi_asq_key_strings sd_additional_codes[] = { 1664 0x81, 0, "Logical Unit is Reserved", 1665 0x85, 0, "Audio Address Not Valid", 1666 0xb6, 0, "Media Load Mechanism Failed", 1667 0xB9, 0, "Audio Play Operation Aborted", 1668 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1669 0x53, 2, "Medium removal prevented", 1670 0x6f, 0, "Authentication failed during key exchange", 1671 0x6f, 1, "Key not present", 1672 0x6f, 2, "Key not established", 1673 0x6f, 3, "Read without proper authentication", 1674 0x6f, 4, "Mismatched region to this logical unit", 1675 0x6f, 5, "Region reset count error", 1676 0xffff, 0x0, NULL 1677 }; 1678 1679 1680 /* 1681 * Struct for passing printing information for sense data messages 1682 */ 1683 struct sd_sense_info { 1684 int ssi_severity; 1685 int ssi_pfa_flag; 1686 }; 1687 1688 /* 1689 * Table of function pointers for iostart-side routines. Seperate "chains" 1690 * of layered function calls are formed by placing the function pointers 1691 * sequentially in the desired order. Functions are called according to an 1692 * incrementing table index ordering. The last function in each chain must 1693 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1694 * in the sd_iodone_chain[] array. 1695 * 1696 * Note: It may seem more natural to organize both the iostart and iodone 1697 * functions together, into an array of structures (or some similar 1698 * organization) with a common index, rather than two seperate arrays which 1699 * must be maintained in synchronization. The purpose of this division is 1700 * to achiece improved performance: individual arrays allows for more 1701 * effective cache line utilization on certain platforms. 1702 */ 1703 1704 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1705 1706 1707 static sd_chain_t sd_iostart_chain[] = { 1708 1709 /* Chain for buf IO for disk drive targets (PM enabled) */ 1710 sd_mapblockaddr_iostart, /* Index: 0 */ 1711 sd_pm_iostart, /* Index: 1 */ 1712 sd_core_iostart, /* Index: 2 */ 1713 1714 /* Chain for buf IO for disk drive targets (PM disabled) */ 1715 sd_mapblockaddr_iostart, /* Index: 3 */ 1716 sd_core_iostart, /* Index: 4 */ 1717 1718 /* Chain for buf IO for removable-media targets (PM enabled) */ 1719 sd_mapblockaddr_iostart, /* Index: 5 */ 1720 sd_mapblocksize_iostart, /* Index: 6 */ 1721 sd_pm_iostart, /* Index: 7 */ 1722 sd_core_iostart, /* Index: 8 */ 1723 1724 /* Chain for buf IO for removable-media targets (PM disabled) */ 1725 sd_mapblockaddr_iostart, /* Index: 9 */ 1726 sd_mapblocksize_iostart, /* Index: 10 */ 1727 sd_core_iostart, /* Index: 11 */ 1728 1729 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1730 sd_mapblockaddr_iostart, /* Index: 12 */ 1731 sd_checksum_iostart, /* Index: 13 */ 1732 sd_pm_iostart, /* Index: 14 */ 1733 sd_core_iostart, /* Index: 15 */ 1734 1735 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1736 sd_mapblockaddr_iostart, /* Index: 16 */ 1737 sd_checksum_iostart, /* Index: 17 */ 1738 sd_core_iostart, /* Index: 18 */ 1739 1740 /* Chain for USCSI commands (all targets) */ 1741 sd_pm_iostart, /* Index: 19 */ 1742 sd_core_iostart, /* Index: 20 */ 1743 1744 /* Chain for checksumming USCSI commands (all targets) */ 1745 sd_checksum_uscsi_iostart, /* Index: 21 */ 1746 sd_pm_iostart, /* Index: 22 */ 1747 sd_core_iostart, /* Index: 23 */ 1748 1749 /* Chain for "direct" USCSI commands (all targets) */ 1750 sd_core_iostart, /* Index: 24 */ 1751 1752 /* Chain for "direct priority" USCSI commands (all targets) */ 1753 sd_core_iostart, /* Index: 25 */ 1754 }; 1755 1756 /* 1757 * Macros to locate the first function of each iostart chain in the 1758 * sd_iostart_chain[] array. These are located by the index in the array. 1759 */ 1760 #define SD_CHAIN_DISK_IOSTART 0 1761 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1762 #define SD_CHAIN_RMMEDIA_IOSTART 5 1763 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1764 #define SD_CHAIN_CHKSUM_IOSTART 12 1765 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1766 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1767 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1768 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1769 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1770 1771 1772 /* 1773 * Table of function pointers for the iodone-side routines for the driver- 1774 * internal layering mechanism. The calling sequence for iodone routines 1775 * uses a decrementing table index, so the last routine called in a chain 1776 * must be at the lowest array index location for that chain. The last 1777 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1778 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1779 * of the functions in an iodone side chain must correspond to the ordering 1780 * of the iostart routines for that chain. Note that there is no iodone 1781 * side routine that corresponds to sd_core_iostart(), so there is no 1782 * entry in the table for this. 1783 */ 1784 1785 static sd_chain_t sd_iodone_chain[] = { 1786 1787 /* Chain for buf IO for disk drive targets (PM enabled) */ 1788 sd_buf_iodone, /* Index: 0 */ 1789 sd_mapblockaddr_iodone, /* Index: 1 */ 1790 sd_pm_iodone, /* Index: 2 */ 1791 1792 /* Chain for buf IO for disk drive targets (PM disabled) */ 1793 sd_buf_iodone, /* Index: 3 */ 1794 sd_mapblockaddr_iodone, /* Index: 4 */ 1795 1796 /* Chain for buf IO for removable-media targets (PM enabled) */ 1797 sd_buf_iodone, /* Index: 5 */ 1798 sd_mapblockaddr_iodone, /* Index: 6 */ 1799 sd_mapblocksize_iodone, /* Index: 7 */ 1800 sd_pm_iodone, /* Index: 8 */ 1801 1802 /* Chain for buf IO for removable-media targets (PM disabled) */ 1803 sd_buf_iodone, /* Index: 9 */ 1804 sd_mapblockaddr_iodone, /* Index: 10 */ 1805 sd_mapblocksize_iodone, /* Index: 11 */ 1806 1807 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1808 sd_buf_iodone, /* Index: 12 */ 1809 sd_mapblockaddr_iodone, /* Index: 13 */ 1810 sd_checksum_iodone, /* Index: 14 */ 1811 sd_pm_iodone, /* Index: 15 */ 1812 1813 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1814 sd_buf_iodone, /* Index: 16 */ 1815 sd_mapblockaddr_iodone, /* Index: 17 */ 1816 sd_checksum_iodone, /* Index: 18 */ 1817 1818 /* Chain for USCSI commands (non-checksum targets) */ 1819 sd_uscsi_iodone, /* Index: 19 */ 1820 sd_pm_iodone, /* Index: 20 */ 1821 1822 /* Chain for USCSI commands (checksum targets) */ 1823 sd_uscsi_iodone, /* Index: 21 */ 1824 sd_checksum_uscsi_iodone, /* Index: 22 */ 1825 sd_pm_iodone, /* Index: 22 */ 1826 1827 /* Chain for "direct" USCSI commands (all targets) */ 1828 sd_uscsi_iodone, /* Index: 24 */ 1829 1830 /* Chain for "direct priority" USCSI commands (all targets) */ 1831 sd_uscsi_iodone, /* Index: 25 */ 1832 }; 1833 1834 1835 /* 1836 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1837 * each iodone-side chain. These are located by the array index, but as the 1838 * iodone side functions are called in a decrementing-index order, the 1839 * highest index number in each chain must be specified (as these correspond 1840 * to the first function in the iodone chain that will be called by the core 1841 * at IO completion time). 1842 */ 1843 1844 #define SD_CHAIN_DISK_IODONE 2 1845 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1846 #define SD_CHAIN_RMMEDIA_IODONE 8 1847 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1848 #define SD_CHAIN_CHKSUM_IODONE 15 1849 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1850 #define SD_CHAIN_USCSI_CMD_IODONE 20 1851 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1852 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1853 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1854 1855 1856 1857 1858 /* 1859 * Array to map a layering chain index to the appropriate initpkt routine. 1860 * The redundant entries are present so that the index used for accessing 1861 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1862 * with this table as well. 1863 */ 1864 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1865 1866 static sd_initpkt_t sd_initpkt_map[] = { 1867 1868 /* Chain for buf IO for disk drive targets (PM enabled) */ 1869 sd_initpkt_for_buf, /* Index: 0 */ 1870 sd_initpkt_for_buf, /* Index: 1 */ 1871 sd_initpkt_for_buf, /* Index: 2 */ 1872 1873 /* Chain for buf IO for disk drive targets (PM disabled) */ 1874 sd_initpkt_for_buf, /* Index: 3 */ 1875 sd_initpkt_for_buf, /* Index: 4 */ 1876 1877 /* Chain for buf IO for removable-media targets (PM enabled) */ 1878 sd_initpkt_for_buf, /* Index: 5 */ 1879 sd_initpkt_for_buf, /* Index: 6 */ 1880 sd_initpkt_for_buf, /* Index: 7 */ 1881 sd_initpkt_for_buf, /* Index: 8 */ 1882 1883 /* Chain for buf IO for removable-media targets (PM disabled) */ 1884 sd_initpkt_for_buf, /* Index: 9 */ 1885 sd_initpkt_for_buf, /* Index: 10 */ 1886 sd_initpkt_for_buf, /* Index: 11 */ 1887 1888 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1889 sd_initpkt_for_buf, /* Index: 12 */ 1890 sd_initpkt_for_buf, /* Index: 13 */ 1891 sd_initpkt_for_buf, /* Index: 14 */ 1892 sd_initpkt_for_buf, /* Index: 15 */ 1893 1894 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1895 sd_initpkt_for_buf, /* Index: 16 */ 1896 sd_initpkt_for_buf, /* Index: 17 */ 1897 sd_initpkt_for_buf, /* Index: 18 */ 1898 1899 /* Chain for USCSI commands (non-checksum targets) */ 1900 sd_initpkt_for_uscsi, /* Index: 19 */ 1901 sd_initpkt_for_uscsi, /* Index: 20 */ 1902 1903 /* Chain for USCSI commands (checksum targets) */ 1904 sd_initpkt_for_uscsi, /* Index: 21 */ 1905 sd_initpkt_for_uscsi, /* Index: 22 */ 1906 sd_initpkt_for_uscsi, /* Index: 22 */ 1907 1908 /* Chain for "direct" USCSI commands (all targets) */ 1909 sd_initpkt_for_uscsi, /* Index: 24 */ 1910 1911 /* Chain for "direct priority" USCSI commands (all targets) */ 1912 sd_initpkt_for_uscsi, /* Index: 25 */ 1913 1914 }; 1915 1916 1917 /* 1918 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1919 * The redundant entries are present so that the index used for accessing 1920 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1921 * with this table as well. 1922 */ 1923 typedef void (*sd_destroypkt_t)(struct buf *); 1924 1925 static sd_destroypkt_t sd_destroypkt_map[] = { 1926 1927 /* Chain for buf IO for disk drive targets (PM enabled) */ 1928 sd_destroypkt_for_buf, /* Index: 0 */ 1929 sd_destroypkt_for_buf, /* Index: 1 */ 1930 sd_destroypkt_for_buf, /* Index: 2 */ 1931 1932 /* Chain for buf IO for disk drive targets (PM disabled) */ 1933 sd_destroypkt_for_buf, /* Index: 3 */ 1934 sd_destroypkt_for_buf, /* Index: 4 */ 1935 1936 /* Chain for buf IO for removable-media targets (PM enabled) */ 1937 sd_destroypkt_for_buf, /* Index: 5 */ 1938 sd_destroypkt_for_buf, /* Index: 6 */ 1939 sd_destroypkt_for_buf, /* Index: 7 */ 1940 sd_destroypkt_for_buf, /* Index: 8 */ 1941 1942 /* Chain for buf IO for removable-media targets (PM disabled) */ 1943 sd_destroypkt_for_buf, /* Index: 9 */ 1944 sd_destroypkt_for_buf, /* Index: 10 */ 1945 sd_destroypkt_for_buf, /* Index: 11 */ 1946 1947 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1948 sd_destroypkt_for_buf, /* Index: 12 */ 1949 sd_destroypkt_for_buf, /* Index: 13 */ 1950 sd_destroypkt_for_buf, /* Index: 14 */ 1951 sd_destroypkt_for_buf, /* Index: 15 */ 1952 1953 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1954 sd_destroypkt_for_buf, /* Index: 16 */ 1955 sd_destroypkt_for_buf, /* Index: 17 */ 1956 sd_destroypkt_for_buf, /* Index: 18 */ 1957 1958 /* Chain for USCSI commands (non-checksum targets) */ 1959 sd_destroypkt_for_uscsi, /* Index: 19 */ 1960 sd_destroypkt_for_uscsi, /* Index: 20 */ 1961 1962 /* Chain for USCSI commands (checksum targets) */ 1963 sd_destroypkt_for_uscsi, /* Index: 21 */ 1964 sd_destroypkt_for_uscsi, /* Index: 22 */ 1965 sd_destroypkt_for_uscsi, /* Index: 22 */ 1966 1967 /* Chain for "direct" USCSI commands (all targets) */ 1968 sd_destroypkt_for_uscsi, /* Index: 24 */ 1969 1970 /* Chain for "direct priority" USCSI commands (all targets) */ 1971 sd_destroypkt_for_uscsi, /* Index: 25 */ 1972 1973 }; 1974 1975 1976 1977 /* 1978 * Array to map a layering chain index to the appropriate chain "type". 1979 * The chain type indicates a specific property/usage of the chain. 1980 * The redundant entries are present so that the index used for accessing 1981 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1982 * with this table as well. 1983 */ 1984 1985 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 1986 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 1987 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 1988 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 1989 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 1990 /* (for error recovery) */ 1991 1992 static int sd_chain_type_map[] = { 1993 1994 /* Chain for buf IO for disk drive targets (PM enabled) */ 1995 SD_CHAIN_BUFIO, /* Index: 0 */ 1996 SD_CHAIN_BUFIO, /* Index: 1 */ 1997 SD_CHAIN_BUFIO, /* Index: 2 */ 1998 1999 /* Chain for buf IO for disk drive targets (PM disabled) */ 2000 SD_CHAIN_BUFIO, /* Index: 3 */ 2001 SD_CHAIN_BUFIO, /* Index: 4 */ 2002 2003 /* Chain for buf IO for removable-media targets (PM enabled) */ 2004 SD_CHAIN_BUFIO, /* Index: 5 */ 2005 SD_CHAIN_BUFIO, /* Index: 6 */ 2006 SD_CHAIN_BUFIO, /* Index: 7 */ 2007 SD_CHAIN_BUFIO, /* Index: 8 */ 2008 2009 /* Chain for buf IO for removable-media targets (PM disabled) */ 2010 SD_CHAIN_BUFIO, /* Index: 9 */ 2011 SD_CHAIN_BUFIO, /* Index: 10 */ 2012 SD_CHAIN_BUFIO, /* Index: 11 */ 2013 2014 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2015 SD_CHAIN_BUFIO, /* Index: 12 */ 2016 SD_CHAIN_BUFIO, /* Index: 13 */ 2017 SD_CHAIN_BUFIO, /* Index: 14 */ 2018 SD_CHAIN_BUFIO, /* Index: 15 */ 2019 2020 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2021 SD_CHAIN_BUFIO, /* Index: 16 */ 2022 SD_CHAIN_BUFIO, /* Index: 17 */ 2023 SD_CHAIN_BUFIO, /* Index: 18 */ 2024 2025 /* Chain for USCSI commands (non-checksum targets) */ 2026 SD_CHAIN_USCSI, /* Index: 19 */ 2027 SD_CHAIN_USCSI, /* Index: 20 */ 2028 2029 /* Chain for USCSI commands (checksum targets) */ 2030 SD_CHAIN_USCSI, /* Index: 21 */ 2031 SD_CHAIN_USCSI, /* Index: 22 */ 2032 SD_CHAIN_USCSI, /* Index: 22 */ 2033 2034 /* Chain for "direct" USCSI commands (all targets) */ 2035 SD_CHAIN_DIRECT, /* Index: 24 */ 2036 2037 /* Chain for "direct priority" USCSI commands (all targets) */ 2038 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2039 }; 2040 2041 2042 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2043 #define SD_IS_BUFIO(xp) \ 2044 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2045 2046 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2047 #define SD_IS_DIRECT_PRIORITY(xp) \ 2048 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2049 2050 2051 2052 /* 2053 * Struct, array, and macros to map a specific chain to the appropriate 2054 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2055 * 2056 * The sd_chain_index_map[] array is used at attach time to set the various 2057 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2058 * chain to be used with the instance. This allows different instances to use 2059 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2060 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2061 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2062 * dynamically & without the use of locking; and (2) a layer may update the 2063 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2064 * to allow for deferred processing of an IO within the same chain from a 2065 * different execution context. 2066 */ 2067 2068 struct sd_chain_index { 2069 int sci_iostart_index; 2070 int sci_iodone_index; 2071 }; 2072 2073 static struct sd_chain_index sd_chain_index_map[] = { 2074 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2075 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2076 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2077 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2078 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2079 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2080 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2081 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2082 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2083 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2084 }; 2085 2086 2087 /* 2088 * The following are indexes into the sd_chain_index_map[] array. 2089 */ 2090 2091 /* un->un_buf_chain_type must be set to one of these */ 2092 #define SD_CHAIN_INFO_DISK 0 2093 #define SD_CHAIN_INFO_DISK_NO_PM 1 2094 #define SD_CHAIN_INFO_RMMEDIA 2 2095 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2096 #define SD_CHAIN_INFO_CHKSUM 4 2097 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2098 2099 /* un->un_uscsi_chain_type must be set to one of these */ 2100 #define SD_CHAIN_INFO_USCSI_CMD 6 2101 /* USCSI with PM disabled is the same as DIRECT */ 2102 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2103 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2104 2105 /* un->un_direct_chain_type must be set to one of these */ 2106 #define SD_CHAIN_INFO_DIRECT_CMD 8 2107 2108 /* un->un_priority_chain_type must be set to one of these */ 2109 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2110 2111 /* size for devid inquiries */ 2112 #define MAX_INQUIRY_SIZE 0xF0 2113 2114 /* 2115 * Macros used by functions to pass a given buf(9S) struct along to the 2116 * next function in the layering chain for further processing. 2117 * 2118 * In the following macros, passing more than three arguments to the called 2119 * routines causes the optimizer for the SPARC compiler to stop doing tail 2120 * call elimination which results in significant performance degradation. 2121 */ 2122 #define SD_BEGIN_IOSTART(index, un, bp) \ 2123 ((*(sd_iostart_chain[index]))(index, un, bp)) 2124 2125 #define SD_BEGIN_IODONE(index, un, bp) \ 2126 ((*(sd_iodone_chain[index]))(index, un, bp)) 2127 2128 #define SD_NEXT_IOSTART(index, un, bp) \ 2129 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2130 2131 #define SD_NEXT_IODONE(index, un, bp) \ 2132 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2133 2134 2135 /* 2136 * Function: _init 2137 * 2138 * Description: This is the driver _init(9E) entry point. 2139 * 2140 * Return Code: Returns the value from mod_install(9F) or 2141 * ddi_soft_state_init(9F) as appropriate. 2142 * 2143 * Context: Called when driver module loaded. 2144 */ 2145 2146 int 2147 _init(void) 2148 { 2149 int err; 2150 2151 /* establish driver name from module name */ 2152 sd_label = mod_modname(&modlinkage); 2153 2154 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2155 SD_MAXUNIT); 2156 2157 if (err != 0) { 2158 return (err); 2159 } 2160 2161 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2162 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2163 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2164 2165 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2166 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2167 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2168 2169 /* 2170 * it's ok to init here even for fibre device 2171 */ 2172 sd_scsi_probe_cache_init(); 2173 2174 /* 2175 * Creating taskq before mod_install ensures that all callers (threads) 2176 * that enter the module after a successfull mod_install encounter 2177 * a valid taskq. 2178 */ 2179 sd_taskq_create(); 2180 2181 err = mod_install(&modlinkage); 2182 if (err != 0) { 2183 /* delete taskq if install fails */ 2184 sd_taskq_delete(); 2185 2186 mutex_destroy(&sd_detach_mutex); 2187 mutex_destroy(&sd_log_mutex); 2188 mutex_destroy(&sd_label_mutex); 2189 2190 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2191 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2192 cv_destroy(&sd_tr.srq_inprocess_cv); 2193 2194 sd_scsi_probe_cache_fini(); 2195 2196 ddi_soft_state_fini(&sd_state); 2197 return (err); 2198 } 2199 2200 return (err); 2201 } 2202 2203 2204 /* 2205 * Function: _fini 2206 * 2207 * Description: This is the driver _fini(9E) entry point. 2208 * 2209 * Return Code: Returns the value from mod_remove(9F) 2210 * 2211 * Context: Called when driver module is unloaded. 2212 */ 2213 2214 int 2215 _fini(void) 2216 { 2217 int err; 2218 2219 if ((err = mod_remove(&modlinkage)) != 0) { 2220 return (err); 2221 } 2222 2223 sd_taskq_delete(); 2224 2225 mutex_destroy(&sd_detach_mutex); 2226 mutex_destroy(&sd_log_mutex); 2227 mutex_destroy(&sd_label_mutex); 2228 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2229 2230 sd_scsi_probe_cache_fini(); 2231 2232 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2233 cv_destroy(&sd_tr.srq_inprocess_cv); 2234 2235 ddi_soft_state_fini(&sd_state); 2236 2237 return (err); 2238 } 2239 2240 2241 /* 2242 * Function: _info 2243 * 2244 * Description: This is the driver _info(9E) entry point. 2245 * 2246 * Arguments: modinfop - pointer to the driver modinfo structure 2247 * 2248 * Return Code: Returns the value from mod_info(9F). 2249 * 2250 * Context: Kernel thread context 2251 */ 2252 2253 int 2254 _info(struct modinfo *modinfop) 2255 { 2256 return (mod_info(&modlinkage, modinfop)); 2257 } 2258 2259 2260 /* 2261 * The following routines implement the driver message logging facility. 2262 * They provide component- and level- based debug output filtering. 2263 * Output may also be restricted to messages for a single instance by 2264 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2265 * to NULL, then messages for all instances are printed. 2266 * 2267 * These routines have been cloned from each other due to the language 2268 * constraints of macros and variable argument list processing. 2269 */ 2270 2271 2272 /* 2273 * Function: sd_log_err 2274 * 2275 * Description: This routine is called by the SD_ERROR macro for debug 2276 * logging of error conditions. 2277 * 2278 * Arguments: comp - driver component being logged 2279 * dev - pointer to driver info structure 2280 * fmt - error string and format to be logged 2281 */ 2282 2283 static void 2284 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2285 { 2286 va_list ap; 2287 dev_info_t *dev; 2288 2289 ASSERT(un != NULL); 2290 dev = SD_DEVINFO(un); 2291 ASSERT(dev != NULL); 2292 2293 /* 2294 * Filter messages based on the global component and level masks. 2295 * Also print if un matches the value of sd_debug_un, or if 2296 * sd_debug_un is set to NULL. 2297 */ 2298 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2299 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2300 mutex_enter(&sd_log_mutex); 2301 va_start(ap, fmt); 2302 (void) vsprintf(sd_log_buf, fmt, ap); 2303 va_end(ap); 2304 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2305 mutex_exit(&sd_log_mutex); 2306 } 2307 #ifdef SD_FAULT_INJECTION 2308 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2309 if (un->sd_injection_mask & comp) { 2310 mutex_enter(&sd_log_mutex); 2311 va_start(ap, fmt); 2312 (void) vsprintf(sd_log_buf, fmt, ap); 2313 va_end(ap); 2314 sd_injection_log(sd_log_buf, un); 2315 mutex_exit(&sd_log_mutex); 2316 } 2317 #endif 2318 } 2319 2320 2321 /* 2322 * Function: sd_log_info 2323 * 2324 * Description: This routine is called by the SD_INFO macro for debug 2325 * logging of general purpose informational conditions. 2326 * 2327 * Arguments: comp - driver component being logged 2328 * dev - pointer to driver info structure 2329 * fmt - info string and format to be logged 2330 */ 2331 2332 static void 2333 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2334 { 2335 va_list ap; 2336 dev_info_t *dev; 2337 2338 ASSERT(un != NULL); 2339 dev = SD_DEVINFO(un); 2340 ASSERT(dev != NULL); 2341 2342 /* 2343 * Filter messages based on the global component and level masks. 2344 * Also print if un matches the value of sd_debug_un, or if 2345 * sd_debug_un is set to NULL. 2346 */ 2347 if ((sd_component_mask & component) && 2348 (sd_level_mask & SD_LOGMASK_INFO) && 2349 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2350 mutex_enter(&sd_log_mutex); 2351 va_start(ap, fmt); 2352 (void) vsprintf(sd_log_buf, fmt, ap); 2353 va_end(ap); 2354 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2355 mutex_exit(&sd_log_mutex); 2356 } 2357 #ifdef SD_FAULT_INJECTION 2358 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2359 if (un->sd_injection_mask & component) { 2360 mutex_enter(&sd_log_mutex); 2361 va_start(ap, fmt); 2362 (void) vsprintf(sd_log_buf, fmt, ap); 2363 va_end(ap); 2364 sd_injection_log(sd_log_buf, un); 2365 mutex_exit(&sd_log_mutex); 2366 } 2367 #endif 2368 } 2369 2370 2371 /* 2372 * Function: sd_log_trace 2373 * 2374 * Description: This routine is called by the SD_TRACE macro for debug 2375 * logging of trace conditions (i.e. function entry/exit). 2376 * 2377 * Arguments: comp - driver component being logged 2378 * dev - pointer to driver info structure 2379 * fmt - trace string and format to be logged 2380 */ 2381 2382 static void 2383 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2384 { 2385 va_list ap; 2386 dev_info_t *dev; 2387 2388 ASSERT(un != NULL); 2389 dev = SD_DEVINFO(un); 2390 ASSERT(dev != NULL); 2391 2392 /* 2393 * Filter messages based on the global component and level masks. 2394 * Also print if un matches the value of sd_debug_un, or if 2395 * sd_debug_un is set to NULL. 2396 */ 2397 if ((sd_component_mask & component) && 2398 (sd_level_mask & SD_LOGMASK_TRACE) && 2399 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2400 mutex_enter(&sd_log_mutex); 2401 va_start(ap, fmt); 2402 (void) vsprintf(sd_log_buf, fmt, ap); 2403 va_end(ap); 2404 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2405 mutex_exit(&sd_log_mutex); 2406 } 2407 #ifdef SD_FAULT_INJECTION 2408 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2409 if (un->sd_injection_mask & component) { 2410 mutex_enter(&sd_log_mutex); 2411 va_start(ap, fmt); 2412 (void) vsprintf(sd_log_buf, fmt, ap); 2413 va_end(ap); 2414 sd_injection_log(sd_log_buf, un); 2415 mutex_exit(&sd_log_mutex); 2416 } 2417 #endif 2418 } 2419 2420 2421 /* 2422 * Function: sdprobe 2423 * 2424 * Description: This is the driver probe(9e) entry point function. 2425 * 2426 * Arguments: devi - opaque device info handle 2427 * 2428 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2429 * DDI_PROBE_FAILURE: If the probe failed. 2430 * DDI_PROBE_PARTIAL: If the instance is not present now, 2431 * but may be present in the future. 2432 */ 2433 2434 static int 2435 sdprobe(dev_info_t *devi) 2436 { 2437 struct scsi_device *devp; 2438 int rval; 2439 int instance; 2440 2441 /* 2442 * if it wasn't for pln, sdprobe could actually be nulldev 2443 * in the "__fibre" case. 2444 */ 2445 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2446 return (DDI_PROBE_DONTCARE); 2447 } 2448 2449 devp = ddi_get_driver_private(devi); 2450 2451 if (devp == NULL) { 2452 /* Ooops... nexus driver is mis-configured... */ 2453 return (DDI_PROBE_FAILURE); 2454 } 2455 2456 instance = ddi_get_instance(devi); 2457 2458 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2459 return (DDI_PROBE_PARTIAL); 2460 } 2461 2462 /* 2463 * Call the SCSA utility probe routine to see if we actually 2464 * have a target at this SCSI nexus. 2465 */ 2466 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2467 case SCSIPROBE_EXISTS: 2468 switch (devp->sd_inq->inq_dtype) { 2469 case DTYPE_DIRECT: 2470 rval = DDI_PROBE_SUCCESS; 2471 break; 2472 case DTYPE_RODIRECT: 2473 /* CDs etc. Can be removable media */ 2474 rval = DDI_PROBE_SUCCESS; 2475 break; 2476 case DTYPE_OPTICAL: 2477 /* 2478 * Rewritable optical driver HP115AA 2479 * Can also be removable media 2480 */ 2481 2482 /* 2483 * Do not attempt to bind to DTYPE_OPTICAL if 2484 * pre solaris 9 sparc sd behavior is required 2485 * 2486 * If first time through and sd_dtype_optical_bind 2487 * has not been set in /etc/system check properties 2488 */ 2489 2490 if (sd_dtype_optical_bind < 0) { 2491 sd_dtype_optical_bind = ddi_prop_get_int 2492 (DDI_DEV_T_ANY, devi, 0, 2493 "optical-device-bind", 1); 2494 } 2495 2496 if (sd_dtype_optical_bind == 0) { 2497 rval = DDI_PROBE_FAILURE; 2498 } else { 2499 rval = DDI_PROBE_SUCCESS; 2500 } 2501 break; 2502 2503 case DTYPE_NOTPRESENT: 2504 default: 2505 rval = DDI_PROBE_FAILURE; 2506 break; 2507 } 2508 break; 2509 default: 2510 rval = DDI_PROBE_PARTIAL; 2511 break; 2512 } 2513 2514 /* 2515 * This routine checks for resource allocation prior to freeing, 2516 * so it will take care of the "smart probing" case where a 2517 * scsi_probe() may or may not have been issued and will *not* 2518 * free previously-freed resources. 2519 */ 2520 scsi_unprobe(devp); 2521 return (rval); 2522 } 2523 2524 2525 /* 2526 * Function: sdinfo 2527 * 2528 * Description: This is the driver getinfo(9e) entry point function. 2529 * Given the device number, return the devinfo pointer from 2530 * the scsi_device structure or the instance number 2531 * associated with the dev_t. 2532 * 2533 * Arguments: dip - pointer to device info structure 2534 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2535 * DDI_INFO_DEVT2INSTANCE) 2536 * arg - driver dev_t 2537 * resultp - user buffer for request response 2538 * 2539 * Return Code: DDI_SUCCESS 2540 * DDI_FAILURE 2541 */ 2542 /* ARGSUSED */ 2543 static int 2544 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2545 { 2546 struct sd_lun *un; 2547 dev_t dev; 2548 int instance; 2549 int error; 2550 2551 switch (infocmd) { 2552 case DDI_INFO_DEVT2DEVINFO: 2553 dev = (dev_t)arg; 2554 instance = SDUNIT(dev); 2555 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2556 return (DDI_FAILURE); 2557 } 2558 *result = (void *) SD_DEVINFO(un); 2559 error = DDI_SUCCESS; 2560 break; 2561 case DDI_INFO_DEVT2INSTANCE: 2562 dev = (dev_t)arg; 2563 instance = SDUNIT(dev); 2564 *result = (void *)(uintptr_t)instance; 2565 error = DDI_SUCCESS; 2566 break; 2567 default: 2568 error = DDI_FAILURE; 2569 } 2570 return (error); 2571 } 2572 2573 /* 2574 * Function: sd_prop_op 2575 * 2576 * Description: This is the driver prop_op(9e) entry point function. 2577 * Return the number of blocks for the partition in question 2578 * or forward the request to the property facilities. 2579 * 2580 * Arguments: dev - device number 2581 * dip - pointer to device info structure 2582 * prop_op - property operator 2583 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2584 * name - pointer to property name 2585 * valuep - pointer or address of the user buffer 2586 * lengthp - property length 2587 * 2588 * Return Code: DDI_PROP_SUCCESS 2589 * DDI_PROP_NOT_FOUND 2590 * DDI_PROP_UNDEFINED 2591 * DDI_PROP_NO_MEMORY 2592 * DDI_PROP_BUF_TOO_SMALL 2593 */ 2594 2595 static int 2596 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2597 char *name, caddr_t valuep, int *lengthp) 2598 { 2599 int instance = ddi_get_instance(dip); 2600 struct sd_lun *un; 2601 uint64_t nblocks64; 2602 2603 /* 2604 * Our dynamic properties are all device specific and size oriented. 2605 * Requests issued under conditions where size is valid are passed 2606 * to ddi_prop_op_nblocks with the size information, otherwise the 2607 * request is passed to ddi_prop_op. Size depends on valid geometry. 2608 */ 2609 un = ddi_get_soft_state(sd_state, instance); 2610 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2611 (un->un_f_geometry_is_valid == FALSE)) { 2612 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2613 name, valuep, lengthp)); 2614 } else { 2615 /* get nblocks value */ 2616 ASSERT(!mutex_owned(SD_MUTEX(un))); 2617 mutex_enter(SD_MUTEX(un)); 2618 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2619 mutex_exit(SD_MUTEX(un)); 2620 2621 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2622 name, valuep, lengthp, nblocks64)); 2623 } 2624 } 2625 2626 /* 2627 * The following functions are for smart probing: 2628 * sd_scsi_probe_cache_init() 2629 * sd_scsi_probe_cache_fini() 2630 * sd_scsi_clear_probe_cache() 2631 * sd_scsi_probe_with_cache() 2632 */ 2633 2634 /* 2635 * Function: sd_scsi_probe_cache_init 2636 * 2637 * Description: Initializes the probe response cache mutex and head pointer. 2638 * 2639 * Context: Kernel thread context 2640 */ 2641 2642 static void 2643 sd_scsi_probe_cache_init(void) 2644 { 2645 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2646 sd_scsi_probe_cache_head = NULL; 2647 } 2648 2649 2650 /* 2651 * Function: sd_scsi_probe_cache_fini 2652 * 2653 * Description: Frees all resources associated with the probe response cache. 2654 * 2655 * Context: Kernel thread context 2656 */ 2657 2658 static void 2659 sd_scsi_probe_cache_fini(void) 2660 { 2661 struct sd_scsi_probe_cache *cp; 2662 struct sd_scsi_probe_cache *ncp; 2663 2664 /* Clean up our smart probing linked list */ 2665 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2666 ncp = cp->next; 2667 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2668 } 2669 sd_scsi_probe_cache_head = NULL; 2670 mutex_destroy(&sd_scsi_probe_cache_mutex); 2671 } 2672 2673 2674 /* 2675 * Function: sd_scsi_clear_probe_cache 2676 * 2677 * Description: This routine clears the probe response cache. This is 2678 * done when open() returns ENXIO so that when deferred 2679 * attach is attempted (possibly after a device has been 2680 * turned on) we will retry the probe. Since we don't know 2681 * which target we failed to open, we just clear the 2682 * entire cache. 2683 * 2684 * Context: Kernel thread context 2685 */ 2686 2687 static void 2688 sd_scsi_clear_probe_cache(void) 2689 { 2690 struct sd_scsi_probe_cache *cp; 2691 int i; 2692 2693 mutex_enter(&sd_scsi_probe_cache_mutex); 2694 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2695 /* 2696 * Reset all entries to SCSIPROBE_EXISTS. This will 2697 * force probing to be performed the next time 2698 * sd_scsi_probe_with_cache is called. 2699 */ 2700 for (i = 0; i < NTARGETS_WIDE; i++) { 2701 cp->cache[i] = SCSIPROBE_EXISTS; 2702 } 2703 } 2704 mutex_exit(&sd_scsi_probe_cache_mutex); 2705 } 2706 2707 2708 /* 2709 * Function: sd_scsi_probe_with_cache 2710 * 2711 * Description: This routine implements support for a scsi device probe 2712 * with cache. The driver maintains a cache of the target 2713 * responses to scsi probes. If we get no response from a 2714 * target during a probe inquiry, we remember that, and we 2715 * avoid additional calls to scsi_probe on non-zero LUNs 2716 * on the same target until the cache is cleared. By doing 2717 * so we avoid the 1/4 sec selection timeout for nonzero 2718 * LUNs. lun0 of a target is always probed. 2719 * 2720 * Arguments: devp - Pointer to a scsi_device(9S) structure 2721 * waitfunc - indicates what the allocator routines should 2722 * do when resources are not available. This value 2723 * is passed on to scsi_probe() when that routine 2724 * is called. 2725 * 2726 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2727 * otherwise the value returned by scsi_probe(9F). 2728 * 2729 * Context: Kernel thread context 2730 */ 2731 2732 static int 2733 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2734 { 2735 struct sd_scsi_probe_cache *cp; 2736 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2737 int lun, tgt; 2738 2739 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2740 SCSI_ADDR_PROP_LUN, 0); 2741 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2742 SCSI_ADDR_PROP_TARGET, -1); 2743 2744 /* Make sure caching enabled and target in range */ 2745 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2746 /* do it the old way (no cache) */ 2747 return (scsi_probe(devp, waitfn)); 2748 } 2749 2750 mutex_enter(&sd_scsi_probe_cache_mutex); 2751 2752 /* Find the cache for this scsi bus instance */ 2753 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2754 if (cp->pdip == pdip) { 2755 break; 2756 } 2757 } 2758 2759 /* If we can't find a cache for this pdip, create one */ 2760 if (cp == NULL) { 2761 int i; 2762 2763 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2764 KM_SLEEP); 2765 cp->pdip = pdip; 2766 cp->next = sd_scsi_probe_cache_head; 2767 sd_scsi_probe_cache_head = cp; 2768 for (i = 0; i < NTARGETS_WIDE; i++) { 2769 cp->cache[i] = SCSIPROBE_EXISTS; 2770 } 2771 } 2772 2773 mutex_exit(&sd_scsi_probe_cache_mutex); 2774 2775 /* Recompute the cache for this target if LUN zero */ 2776 if (lun == 0) { 2777 cp->cache[tgt] = SCSIPROBE_EXISTS; 2778 } 2779 2780 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2781 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2782 return (SCSIPROBE_NORESP); 2783 } 2784 2785 /* Do the actual probe; save & return the result */ 2786 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2787 } 2788 2789 2790 /* 2791 * Function: sd_spin_up_unit 2792 * 2793 * Description: Issues the following commands to spin-up the device: 2794 * START STOP UNIT, and INQUIRY. 2795 * 2796 * Arguments: un - driver soft state (unit) structure 2797 * 2798 * Return Code: 0 - success 2799 * EIO - failure 2800 * EACCES - reservation conflict 2801 * 2802 * Context: Kernel thread context 2803 */ 2804 2805 static int 2806 sd_spin_up_unit(struct sd_lun *un) 2807 { 2808 size_t resid = 0; 2809 int has_conflict = FALSE; 2810 uchar_t *bufaddr; 2811 2812 ASSERT(un != NULL); 2813 2814 /* 2815 * Send a throwaway START UNIT command. 2816 * 2817 * If we fail on this, we don't care presently what precisely 2818 * is wrong. EMC's arrays will also fail this with a check 2819 * condition (0x2/0x4/0x3) if the device is "inactive," but 2820 * we don't want to fail the attach because it may become 2821 * "active" later. 2822 */ 2823 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2824 == EACCES) 2825 has_conflict = TRUE; 2826 2827 /* 2828 * Send another INQUIRY command to the target. This is necessary for 2829 * non-removable media direct access devices because their INQUIRY data 2830 * may not be fully qualified until they are spun up (perhaps via the 2831 * START command above). Note: This seems to be needed for some 2832 * legacy devices only.) The INQUIRY command should succeed even if a 2833 * Reservation Conflict is present. 2834 */ 2835 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2836 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2837 kmem_free(bufaddr, SUN_INQSIZE); 2838 return (EIO); 2839 } 2840 2841 /* 2842 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2843 * Note that this routine does not return a failure here even if the 2844 * INQUIRY command did not return any data. This is a legacy behavior. 2845 */ 2846 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2847 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2848 } 2849 2850 kmem_free(bufaddr, SUN_INQSIZE); 2851 2852 /* If we hit a reservation conflict above, tell the caller. */ 2853 if (has_conflict == TRUE) { 2854 return (EACCES); 2855 } 2856 2857 return (0); 2858 } 2859 2860 #ifdef _LP64 2861 /* 2862 * Function: sd_enable_descr_sense 2863 * 2864 * Description: This routine attempts to select descriptor sense format 2865 * using the Control mode page. Devices that support 64 bit 2866 * LBAs (for >2TB luns) should also implement descriptor 2867 * sense data so we will call this function whenever we see 2868 * a lun larger than 2TB. If for some reason the device 2869 * supports 64 bit LBAs but doesn't support descriptor sense 2870 * presumably the mode select will fail. Everything will 2871 * continue to work normally except that we will not get 2872 * complete sense data for commands that fail with an LBA 2873 * larger than 32 bits. 2874 * 2875 * Arguments: un - driver soft state (unit) structure 2876 * 2877 * Context: Kernel thread context only 2878 */ 2879 2880 static void 2881 sd_enable_descr_sense(struct sd_lun *un) 2882 { 2883 uchar_t *header; 2884 struct mode_control_scsi3 *ctrl_bufp; 2885 size_t buflen; 2886 size_t bd_len; 2887 2888 /* 2889 * Read MODE SENSE page 0xA, Control Mode Page 2890 */ 2891 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 2892 sizeof (struct mode_control_scsi3); 2893 header = kmem_zalloc(buflen, KM_SLEEP); 2894 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 2895 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 2896 SD_ERROR(SD_LOG_COMMON, un, 2897 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 2898 goto eds_exit; 2899 } 2900 2901 /* 2902 * Determine size of Block Descriptors in order to locate 2903 * the mode page data. ATAPI devices return 0, SCSI devices 2904 * should return MODE_BLK_DESC_LENGTH. 2905 */ 2906 bd_len = ((struct mode_header *)header)->bdesc_length; 2907 2908 ctrl_bufp = (struct mode_control_scsi3 *) 2909 (header + MODE_HEADER_LENGTH + bd_len); 2910 2911 /* 2912 * Clear PS bit for MODE SELECT 2913 */ 2914 ctrl_bufp->mode_page.ps = 0; 2915 2916 /* 2917 * Set D_SENSE to enable descriptor sense format. 2918 */ 2919 ctrl_bufp->d_sense = 1; 2920 2921 /* 2922 * Use MODE SELECT to commit the change to the D_SENSE bit 2923 */ 2924 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 2925 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 2926 SD_INFO(SD_LOG_COMMON, un, 2927 "sd_enable_descr_sense: mode select ctrl page failed\n"); 2928 goto eds_exit; 2929 } 2930 2931 eds_exit: 2932 kmem_free(header, buflen); 2933 } 2934 #endif /* _LP64 */ 2935 2936 2937 /* 2938 * Function: sd_set_mmc_caps 2939 * 2940 * Description: This routine determines if the device is MMC compliant and if 2941 * the device supports CDDA via a mode sense of the CDVD 2942 * capabilities mode page. Also checks if the device is a 2943 * dvdram writable device. 2944 * 2945 * Arguments: un - driver soft state (unit) structure 2946 * 2947 * Context: Kernel thread context only 2948 */ 2949 2950 static void 2951 sd_set_mmc_caps(struct sd_lun *un) 2952 { 2953 struct mode_header_grp2 *sense_mhp; 2954 uchar_t *sense_page; 2955 caddr_t buf; 2956 int bd_len; 2957 int status; 2958 struct uscsi_cmd com; 2959 int rtn; 2960 uchar_t *out_data_rw, *out_data_hd; 2961 uchar_t *rqbuf_rw, *rqbuf_hd; 2962 2963 ASSERT(un != NULL); 2964 2965 /* 2966 * The flags which will be set in this function are - mmc compliant, 2967 * dvdram writable device, cdda support. Initialize them to FALSE 2968 * and if a capability is detected - it will be set to TRUE. 2969 */ 2970 un->un_f_mmc_cap = FALSE; 2971 un->un_f_dvdram_writable_device = FALSE; 2972 un->un_f_cfg_cdda = FALSE; 2973 2974 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 2975 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 2976 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 2977 2978 if (status != 0) { 2979 /* command failed; just return */ 2980 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 2981 return; 2982 } 2983 /* 2984 * If the mode sense request for the CDROM CAPABILITIES 2985 * page (0x2A) succeeds the device is assumed to be MMC. 2986 */ 2987 un->un_f_mmc_cap = TRUE; 2988 2989 /* Get to the page data */ 2990 sense_mhp = (struct mode_header_grp2 *)buf; 2991 bd_len = (sense_mhp->bdesc_length_hi << 8) | 2992 sense_mhp->bdesc_length_lo; 2993 if (bd_len > MODE_BLK_DESC_LENGTH) { 2994 /* 2995 * We did not get back the expected block descriptor 2996 * length so we cannot determine if the device supports 2997 * CDDA. However, we still indicate the device is MMC 2998 * according to the successful response to the page 2999 * 0x2A mode sense request. 3000 */ 3001 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3002 "sd_set_mmc_caps: Mode Sense returned " 3003 "invalid block descriptor length\n"); 3004 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3005 return; 3006 } 3007 3008 /* See if read CDDA is supported */ 3009 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3010 bd_len); 3011 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3012 3013 /* See if writing DVD RAM is supported. */ 3014 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3015 if (un->un_f_dvdram_writable_device == TRUE) { 3016 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3017 return; 3018 } 3019 3020 /* 3021 * If the device presents DVD or CD capabilities in the mode 3022 * page, we can return here since a RRD will not have 3023 * these capabilities. 3024 */ 3025 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3026 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3027 return; 3028 } 3029 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3030 3031 /* 3032 * If un->un_f_dvdram_writable_device is still FALSE, 3033 * check for a Removable Rigid Disk (RRD). A RRD 3034 * device is identified by the features RANDOM_WRITABLE and 3035 * HARDWARE_DEFECT_MANAGEMENT. 3036 */ 3037 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3038 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3039 3040 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3041 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3042 RANDOM_WRITABLE); 3043 if (rtn != 0) { 3044 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3045 kmem_free(rqbuf_rw, SENSE_LENGTH); 3046 return; 3047 } 3048 3049 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3050 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3051 3052 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3053 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3054 HARDWARE_DEFECT_MANAGEMENT); 3055 if (rtn == 0) { 3056 /* 3057 * We have good information, check for random writable 3058 * and hardware defect features. 3059 */ 3060 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3061 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3062 un->un_f_dvdram_writable_device = TRUE; 3063 } 3064 } 3065 3066 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3067 kmem_free(rqbuf_rw, SENSE_LENGTH); 3068 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3069 kmem_free(rqbuf_hd, SENSE_LENGTH); 3070 } 3071 3072 /* 3073 * Function: sd_check_for_writable_cd 3074 * 3075 * Description: This routine determines if the media in the device is 3076 * writable or not. It uses the get configuration command (0x46) 3077 * to determine if the media is writable 3078 * 3079 * Arguments: un - driver soft state (unit) structure 3080 * 3081 * Context: Never called at interrupt context. 3082 */ 3083 3084 static void 3085 sd_check_for_writable_cd(struct sd_lun *un) 3086 { 3087 struct uscsi_cmd com; 3088 uchar_t *out_data; 3089 uchar_t *rqbuf; 3090 int rtn; 3091 uchar_t *out_data_rw, *out_data_hd; 3092 uchar_t *rqbuf_rw, *rqbuf_hd; 3093 struct mode_header_grp2 *sense_mhp; 3094 uchar_t *sense_page; 3095 caddr_t buf; 3096 int bd_len; 3097 int status; 3098 3099 ASSERT(un != NULL); 3100 ASSERT(mutex_owned(SD_MUTEX(un))); 3101 3102 /* 3103 * Initialize the writable media to false, if configuration info. 3104 * tells us otherwise then only we will set it. 3105 */ 3106 un->un_f_mmc_writable_media = FALSE; 3107 mutex_exit(SD_MUTEX(un)); 3108 3109 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3110 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3111 3112 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3113 out_data, SD_PROFILE_HEADER_LEN); 3114 3115 mutex_enter(SD_MUTEX(un)); 3116 if (rtn == 0) { 3117 /* 3118 * We have good information, check for writable DVD. 3119 */ 3120 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3121 un->un_f_mmc_writable_media = TRUE; 3122 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3123 kmem_free(rqbuf, SENSE_LENGTH); 3124 return; 3125 } 3126 } 3127 3128 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3129 kmem_free(rqbuf, SENSE_LENGTH); 3130 3131 /* 3132 * Determine if this is a RRD type device. 3133 */ 3134 mutex_exit(SD_MUTEX(un)); 3135 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3136 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3137 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3138 mutex_enter(SD_MUTEX(un)); 3139 if (status != 0) { 3140 /* command failed; just return */ 3141 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3142 return; 3143 } 3144 3145 /* Get to the page data */ 3146 sense_mhp = (struct mode_header_grp2 *)buf; 3147 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3148 if (bd_len > MODE_BLK_DESC_LENGTH) { 3149 /* 3150 * We did not get back the expected block descriptor length so 3151 * we cannot check the mode page. 3152 */ 3153 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3154 "sd_check_for_writable_cd: Mode Sense returned " 3155 "invalid block descriptor length\n"); 3156 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3157 return; 3158 } 3159 3160 /* 3161 * If the device presents DVD or CD capabilities in the mode 3162 * page, we can return here since a RRD device will not have 3163 * these capabilities. 3164 */ 3165 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3166 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3167 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3168 return; 3169 } 3170 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3171 3172 /* 3173 * If un->un_f_mmc_writable_media is still FALSE, 3174 * check for RRD type media. A RRD device is identified 3175 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3176 */ 3177 mutex_exit(SD_MUTEX(un)); 3178 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3179 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3180 3181 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3182 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3183 RANDOM_WRITABLE); 3184 if (rtn != 0) { 3185 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3186 kmem_free(rqbuf_rw, SENSE_LENGTH); 3187 mutex_enter(SD_MUTEX(un)); 3188 return; 3189 } 3190 3191 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3192 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3193 3194 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3195 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3196 HARDWARE_DEFECT_MANAGEMENT); 3197 mutex_enter(SD_MUTEX(un)); 3198 if (rtn == 0) { 3199 /* 3200 * We have good information, check for random writable 3201 * and hardware defect features as current. 3202 */ 3203 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3204 (out_data_rw[10] & 0x1) && 3205 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3206 (out_data_hd[10] & 0x1)) { 3207 un->un_f_mmc_writable_media = TRUE; 3208 } 3209 } 3210 3211 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3212 kmem_free(rqbuf_rw, SENSE_LENGTH); 3213 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3214 kmem_free(rqbuf_hd, SENSE_LENGTH); 3215 } 3216 3217 /* 3218 * Function: sd_read_unit_properties 3219 * 3220 * Description: The following implements a property lookup mechanism. 3221 * Properties for particular disks (keyed on vendor, model 3222 * and rev numbers) are sought in the sd.conf file via 3223 * sd_process_sdconf_file(), and if not found there, are 3224 * looked for in a list hardcoded in this driver via 3225 * sd_process_sdconf_table() Once located the properties 3226 * are used to update the driver unit structure. 3227 * 3228 * Arguments: un - driver soft state (unit) structure 3229 */ 3230 3231 static void 3232 sd_read_unit_properties(struct sd_lun *un) 3233 { 3234 /* 3235 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3236 * the "sd-config-list" property (from the sd.conf file) or if 3237 * there was not a match for the inquiry vid/pid. If this event 3238 * occurs the static driver configuration table is searched for 3239 * a match. 3240 */ 3241 ASSERT(un != NULL); 3242 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3243 sd_process_sdconf_table(un); 3244 } 3245 3246 /* check for LSI device */ 3247 sd_is_lsi(un); 3248 3249 /* 3250 * Set this in sd.conf to 0 in order to disable kstats. The default 3251 * is 1, so they are enabled by default. 3252 */ 3253 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 3254 SD_DEVINFO(un), DDI_PROP_DONTPASS, "enable-partition-kstats", 1)); 3255 } 3256 3257 3258 /* 3259 * Function: sd_process_sdconf_file 3260 * 3261 * Description: Use ddi_getlongprop to obtain the properties from the 3262 * driver's config file (ie, sd.conf) and update the driver 3263 * soft state structure accordingly. 3264 * 3265 * Arguments: un - driver soft state (unit) structure 3266 * 3267 * Return Code: SD_SUCCESS - The properties were successfully set according 3268 * to the driver configuration file. 3269 * SD_FAILURE - The driver config list was not obtained or 3270 * there was no vid/pid match. This indicates that 3271 * the static config table should be used. 3272 * 3273 * The config file has a property, "sd-config-list", which consists of 3274 * one or more duplets as follows: 3275 * 3276 * sd-config-list= 3277 * <duplet>, 3278 * [<duplet>,] 3279 * [<duplet>]; 3280 * 3281 * The structure of each duplet is as follows: 3282 * 3283 * <duplet>:= <vid+pid>,<data-property-name_list> 3284 * 3285 * The first entry of the duplet is the device ID string (the concatenated 3286 * vid & pid; not to be confused with a device_id). This is defined in 3287 * the same way as in the sd_disk_table. 3288 * 3289 * The second part of the duplet is a string that identifies a 3290 * data-property-name-list. The data-property-name-list is defined as 3291 * follows: 3292 * 3293 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3294 * 3295 * The syntax of <data-property-name> depends on the <version> field. 3296 * 3297 * If version = SD_CONF_VERSION_1 we have the following syntax: 3298 * 3299 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3300 * 3301 * where the prop0 value will be used to set prop0 if bit0 set in the 3302 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3303 * 3304 */ 3305 3306 static int 3307 sd_process_sdconf_file(struct sd_lun *un) 3308 { 3309 char *config_list = NULL; 3310 int config_list_len; 3311 int len; 3312 int dupletlen = 0; 3313 char *vidptr; 3314 int vidlen; 3315 char *dnlist_ptr; 3316 char *dataname_ptr; 3317 int dnlist_len; 3318 int dataname_len; 3319 int *data_list; 3320 int data_list_len; 3321 int rval = SD_FAILURE; 3322 int i; 3323 3324 ASSERT(un != NULL); 3325 3326 /* Obtain the configuration list associated with the .conf file */ 3327 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3328 sd_config_list, (caddr_t)&config_list, &config_list_len) 3329 != DDI_PROP_SUCCESS) { 3330 return (SD_FAILURE); 3331 } 3332 3333 /* 3334 * Compare vids in each duplet to the inquiry vid - if a match is 3335 * made, get the data value and update the soft state structure 3336 * accordingly. 3337 * 3338 * Note: This algorithm is complex and difficult to maintain. It should 3339 * be replaced with a more robust implementation. 3340 */ 3341 for (len = config_list_len, vidptr = config_list; len > 0; 3342 vidptr += dupletlen, len -= dupletlen) { 3343 /* 3344 * Note: The assumption here is that each vid entry is on 3345 * a unique line from its associated duplet. 3346 */ 3347 vidlen = dupletlen = (int)strlen(vidptr); 3348 if ((vidlen == 0) || 3349 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3350 dupletlen++; 3351 continue; 3352 } 3353 3354 /* 3355 * dnlist contains 1 or more blank separated 3356 * data-property-name entries 3357 */ 3358 dnlist_ptr = vidptr + vidlen + 1; 3359 dnlist_len = (int)strlen(dnlist_ptr); 3360 dupletlen += dnlist_len + 2; 3361 3362 /* 3363 * Set a pointer for the first data-property-name 3364 * entry in the list 3365 */ 3366 dataname_ptr = dnlist_ptr; 3367 dataname_len = 0; 3368 3369 /* 3370 * Loop through all data-property-name entries in the 3371 * data-property-name-list setting the properties for each. 3372 */ 3373 while (dataname_len < dnlist_len) { 3374 int version; 3375 3376 /* 3377 * Determine the length of the current 3378 * data-property-name entry by indexing until a 3379 * blank or NULL is encountered. When the space is 3380 * encountered reset it to a NULL for compliance 3381 * with ddi_getlongprop(). 3382 */ 3383 for (i = 0; ((dataname_ptr[i] != ' ') && 3384 (dataname_ptr[i] != '\0')); i++) { 3385 ; 3386 } 3387 3388 dataname_len += i; 3389 /* If not null terminated, Make it so */ 3390 if (dataname_ptr[i] == ' ') { 3391 dataname_ptr[i] = '\0'; 3392 } 3393 dataname_len++; 3394 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3395 "sd_process_sdconf_file: disk:%s, data:%s\n", 3396 vidptr, dataname_ptr); 3397 3398 /* Get the data list */ 3399 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3400 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3401 != DDI_PROP_SUCCESS) { 3402 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3403 "sd_process_sdconf_file: data property (%s)" 3404 " has no value\n", dataname_ptr); 3405 dataname_ptr = dnlist_ptr + dataname_len; 3406 continue; 3407 } 3408 3409 version = data_list[0]; 3410 3411 if (version == SD_CONF_VERSION_1) { 3412 sd_tunables values; 3413 3414 /* Set the properties */ 3415 if (sd_chk_vers1_data(un, data_list[1], 3416 &data_list[2], data_list_len, dataname_ptr) 3417 == SD_SUCCESS) { 3418 sd_get_tunables_from_conf(un, 3419 data_list[1], &data_list[2], 3420 &values); 3421 sd_set_vers1_properties(un, 3422 data_list[1], &values); 3423 rval = SD_SUCCESS; 3424 } else { 3425 rval = SD_FAILURE; 3426 } 3427 } else { 3428 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3429 "data property %s version 0x%x is invalid.", 3430 dataname_ptr, version); 3431 rval = SD_FAILURE; 3432 } 3433 kmem_free(data_list, data_list_len); 3434 dataname_ptr = dnlist_ptr + dataname_len; 3435 } 3436 } 3437 3438 /* free up the memory allocated by ddi_getlongprop */ 3439 if (config_list) { 3440 kmem_free(config_list, config_list_len); 3441 } 3442 3443 return (rval); 3444 } 3445 3446 /* 3447 * Function: sd_get_tunables_from_conf() 3448 * 3449 * 3450 * This function reads the data list from the sd.conf file and pulls 3451 * the values that can have numeric values as arguments and places 3452 * the values in the apropriate sd_tunables member. 3453 * Since the order of the data list members varies across platforms 3454 * This function reads them from the data list in a platform specific 3455 * order and places them into the correct sd_tunable member that is 3456 * a consistant across all platforms. 3457 */ 3458 static void 3459 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3460 sd_tunables *values) 3461 { 3462 int i; 3463 int mask; 3464 3465 bzero(values, sizeof (sd_tunables)); 3466 3467 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3468 3469 mask = 1 << i; 3470 if (mask > flags) { 3471 break; 3472 } 3473 3474 switch (mask & flags) { 3475 case 0: /* This mask bit not set in flags */ 3476 continue; 3477 case SD_CONF_BSET_THROTTLE: 3478 values->sdt_throttle = data_list[i]; 3479 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3480 "sd_get_tunables_from_conf: throttle = %d\n", 3481 values->sdt_throttle); 3482 break; 3483 case SD_CONF_BSET_CTYPE: 3484 values->sdt_ctype = data_list[i]; 3485 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3486 "sd_get_tunables_from_conf: ctype = %d\n", 3487 values->sdt_ctype); 3488 break; 3489 case SD_CONF_BSET_NRR_COUNT: 3490 values->sdt_not_rdy_retries = data_list[i]; 3491 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3492 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3493 values->sdt_not_rdy_retries); 3494 break; 3495 case SD_CONF_BSET_BSY_RETRY_COUNT: 3496 values->sdt_busy_retries = data_list[i]; 3497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3498 "sd_get_tunables_from_conf: busy_retries = %d\n", 3499 values->sdt_busy_retries); 3500 break; 3501 case SD_CONF_BSET_RST_RETRIES: 3502 values->sdt_reset_retries = data_list[i]; 3503 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3504 "sd_get_tunables_from_conf: reset_retries = %d\n", 3505 values->sdt_reset_retries); 3506 break; 3507 case SD_CONF_BSET_RSV_REL_TIME: 3508 values->sdt_reserv_rel_time = data_list[i]; 3509 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3510 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3511 values->sdt_reserv_rel_time); 3512 break; 3513 case SD_CONF_BSET_MIN_THROTTLE: 3514 values->sdt_min_throttle = data_list[i]; 3515 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3516 "sd_get_tunables_from_conf: min_throttle = %d\n", 3517 values->sdt_min_throttle); 3518 break; 3519 case SD_CONF_BSET_DISKSORT_DISABLED: 3520 values->sdt_disk_sort_dis = data_list[i]; 3521 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3522 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3523 values->sdt_disk_sort_dis); 3524 break; 3525 case SD_CONF_BSET_LUN_RESET_ENABLED: 3526 values->sdt_lun_reset_enable = data_list[i]; 3527 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3528 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3529 "\n", values->sdt_lun_reset_enable); 3530 break; 3531 } 3532 } 3533 } 3534 3535 /* 3536 * Function: sd_process_sdconf_table 3537 * 3538 * Description: Search the static configuration table for a match on the 3539 * inquiry vid/pid and update the driver soft state structure 3540 * according to the table property values for the device. 3541 * 3542 * The form of a configuration table entry is: 3543 * <vid+pid>,<flags>,<property-data> 3544 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3545 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3546 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3547 * 3548 * Arguments: un - driver soft state (unit) structure 3549 */ 3550 3551 static void 3552 sd_process_sdconf_table(struct sd_lun *un) 3553 { 3554 char *id = NULL; 3555 int table_index; 3556 int idlen; 3557 3558 ASSERT(un != NULL); 3559 for (table_index = 0; table_index < sd_disk_table_size; 3560 table_index++) { 3561 id = sd_disk_table[table_index].device_id; 3562 idlen = strlen(id); 3563 if (idlen == 0) { 3564 continue; 3565 } 3566 3567 /* 3568 * The static configuration table currently does not 3569 * implement version 10 properties. Additionally, 3570 * multiple data-property-name entries are not 3571 * implemented in the static configuration table. 3572 */ 3573 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3574 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3575 "sd_process_sdconf_table: disk %s\n", id); 3576 sd_set_vers1_properties(un, 3577 sd_disk_table[table_index].flags, 3578 sd_disk_table[table_index].properties); 3579 break; 3580 } 3581 } 3582 } 3583 3584 3585 /* 3586 * Function: sd_sdconf_id_match 3587 * 3588 * Description: This local function implements a case sensitive vid/pid 3589 * comparison as well as the boundary cases of wild card and 3590 * multiple blanks. 3591 * 3592 * Note: An implicit assumption made here is that the scsi 3593 * inquiry structure will always keep the vid, pid and 3594 * revision strings in consecutive sequence, so they can be 3595 * read as a single string. If this assumption is not the 3596 * case, a separate string, to be used for the check, needs 3597 * to be built with these strings concatenated. 3598 * 3599 * Arguments: un - driver soft state (unit) structure 3600 * id - table or config file vid/pid 3601 * idlen - length of the vid/pid (bytes) 3602 * 3603 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3604 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3605 */ 3606 3607 static int 3608 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3609 { 3610 struct scsi_inquiry *sd_inq; 3611 int rval = SD_SUCCESS; 3612 3613 ASSERT(un != NULL); 3614 sd_inq = un->un_sd->sd_inq; 3615 ASSERT(id != NULL); 3616 3617 /* 3618 * We use the inq_vid as a pointer to a buffer containing the 3619 * vid and pid and use the entire vid/pid length of the table 3620 * entry for the comparison. This works because the inq_pid 3621 * data member follows inq_vid in the scsi_inquiry structure. 3622 */ 3623 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3624 /* 3625 * The user id string is compared to the inquiry vid/pid 3626 * using a case insensitive comparison and ignoring 3627 * multiple spaces. 3628 */ 3629 rval = sd_blank_cmp(un, id, idlen); 3630 if (rval != SD_SUCCESS) { 3631 /* 3632 * User id strings that start and end with a "*" 3633 * are a special case. These do not have a 3634 * specific vendor, and the product string can 3635 * appear anywhere in the 16 byte PID portion of 3636 * the inquiry data. This is a simple strstr() 3637 * type search for the user id in the inquiry data. 3638 */ 3639 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3640 char *pidptr = &id[1]; 3641 int i; 3642 int j; 3643 int pidstrlen = idlen - 2; 3644 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3645 pidstrlen; 3646 3647 if (j < 0) { 3648 return (SD_FAILURE); 3649 } 3650 for (i = 0; i < j; i++) { 3651 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3652 pidptr, pidstrlen) == 0) { 3653 rval = SD_SUCCESS; 3654 break; 3655 } 3656 } 3657 } 3658 } 3659 } 3660 return (rval); 3661 } 3662 3663 3664 /* 3665 * Function: sd_blank_cmp 3666 * 3667 * Description: If the id string starts and ends with a space, treat 3668 * multiple consecutive spaces as equivalent to a single 3669 * space. For example, this causes a sd_disk_table entry 3670 * of " NEC CDROM " to match a device's id string of 3671 * "NEC CDROM". 3672 * 3673 * Note: The success exit condition for this routine is if 3674 * the pointer to the table entry is '\0' and the cnt of 3675 * the inquiry length is zero. This will happen if the inquiry 3676 * string returned by the device is padded with spaces to be 3677 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3678 * SCSI spec states that the inquiry string is to be padded with 3679 * spaces. 3680 * 3681 * Arguments: un - driver soft state (unit) structure 3682 * id - table or config file vid/pid 3683 * idlen - length of the vid/pid (bytes) 3684 * 3685 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3686 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3687 */ 3688 3689 static int 3690 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3691 { 3692 char *p1; 3693 char *p2; 3694 int cnt; 3695 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3696 sizeof (SD_INQUIRY(un)->inq_pid); 3697 3698 ASSERT(un != NULL); 3699 p2 = un->un_sd->sd_inq->inq_vid; 3700 ASSERT(id != NULL); 3701 p1 = id; 3702 3703 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3704 /* 3705 * Note: string p1 is terminated by a NUL but string p2 3706 * isn't. The end of p2 is determined by cnt. 3707 */ 3708 for (;;) { 3709 /* skip over any extra blanks in both strings */ 3710 while ((*p1 != '\0') && (*p1 == ' ')) { 3711 p1++; 3712 } 3713 while ((cnt != 0) && (*p2 == ' ')) { 3714 p2++; 3715 cnt--; 3716 } 3717 3718 /* compare the two strings */ 3719 if ((cnt == 0) || 3720 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3721 break; 3722 } 3723 while ((cnt > 0) && 3724 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3725 p1++; 3726 p2++; 3727 cnt--; 3728 } 3729 } 3730 } 3731 3732 /* return SD_SUCCESS if both strings match */ 3733 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3734 } 3735 3736 3737 /* 3738 * Function: sd_chk_vers1_data 3739 * 3740 * Description: Verify the version 1 device properties provided by the 3741 * user via the configuration file 3742 * 3743 * Arguments: un - driver soft state (unit) structure 3744 * flags - integer mask indicating properties to be set 3745 * prop_list - integer list of property values 3746 * list_len - length of user provided data 3747 * 3748 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3749 * SD_FAILURE - Indicates the user provided data is invalid 3750 */ 3751 3752 static int 3753 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3754 int list_len, char *dataname_ptr) 3755 { 3756 int i; 3757 int mask = 1; 3758 int index = 0; 3759 3760 ASSERT(un != NULL); 3761 3762 /* Check for a NULL property name and list */ 3763 if (dataname_ptr == NULL) { 3764 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3765 "sd_chk_vers1_data: NULL data property name."); 3766 return (SD_FAILURE); 3767 } 3768 if (prop_list == NULL) { 3769 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3770 "sd_chk_vers1_data: %s NULL data property list.", 3771 dataname_ptr); 3772 return (SD_FAILURE); 3773 } 3774 3775 /* Display a warning if undefined bits are set in the flags */ 3776 if (flags & ~SD_CONF_BIT_MASK) { 3777 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3778 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3779 "Properties not set.", 3780 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3781 return (SD_FAILURE); 3782 } 3783 3784 /* 3785 * Verify the length of the list by identifying the highest bit set 3786 * in the flags and validating that the property list has a length 3787 * up to the index of this bit. 3788 */ 3789 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3790 if (flags & mask) { 3791 index++; 3792 } 3793 mask = 1 << i; 3794 } 3795 if ((list_len / sizeof (int)) < (index + 2)) { 3796 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3797 "sd_chk_vers1_data: " 3798 "Data property list %s size is incorrect. " 3799 "Properties not set.", dataname_ptr); 3800 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3801 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3802 return (SD_FAILURE); 3803 } 3804 return (SD_SUCCESS); 3805 } 3806 3807 3808 /* 3809 * Function: sd_set_vers1_properties 3810 * 3811 * Description: Set version 1 device properties based on a property list 3812 * retrieved from the driver configuration file or static 3813 * configuration table. Version 1 properties have the format: 3814 * 3815 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3816 * 3817 * where the prop0 value will be used to set prop0 if bit0 3818 * is set in the flags 3819 * 3820 * Arguments: un - driver soft state (unit) structure 3821 * flags - integer mask indicating properties to be set 3822 * prop_list - integer list of property values 3823 */ 3824 3825 static void 3826 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3827 { 3828 ASSERT(un != NULL); 3829 3830 /* 3831 * Set the flag to indicate cache is to be disabled. An attempt 3832 * to disable the cache via sd_disable_caching() will be made 3833 * later during attach once the basic initialization is complete. 3834 */ 3835 if (flags & SD_CONF_BSET_NOCACHE) { 3836 un->un_f_opt_disable_cache = TRUE; 3837 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3838 "sd_set_vers1_properties: caching disabled flag set\n"); 3839 } 3840 3841 /* CD-specific configuration parameters */ 3842 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 3843 un->un_f_cfg_playmsf_bcd = TRUE; 3844 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3845 "sd_set_vers1_properties: playmsf_bcd set\n"); 3846 } 3847 if (flags & SD_CONF_BSET_READSUB_BCD) { 3848 un->un_f_cfg_readsub_bcd = TRUE; 3849 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3850 "sd_set_vers1_properties: readsub_bcd set\n"); 3851 } 3852 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 3853 un->un_f_cfg_read_toc_trk_bcd = TRUE; 3854 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3855 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 3856 } 3857 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 3858 un->un_f_cfg_read_toc_addr_bcd = TRUE; 3859 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3860 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 3861 } 3862 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 3863 un->un_f_cfg_no_read_header = TRUE; 3864 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3865 "sd_set_vers1_properties: no_read_header set\n"); 3866 } 3867 if (flags & SD_CONF_BSET_READ_CD_XD4) { 3868 un->un_f_cfg_read_cd_xd4 = TRUE; 3869 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3870 "sd_set_vers1_properties: read_cd_xd4 set\n"); 3871 } 3872 3873 /* Support for devices which do not have valid/unique serial numbers */ 3874 if (flags & SD_CONF_BSET_FAB_DEVID) { 3875 un->un_f_opt_fab_devid = TRUE; 3876 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3877 "sd_set_vers1_properties: fab_devid bit set\n"); 3878 } 3879 3880 /* Support for user throttle configuration */ 3881 if (flags & SD_CONF_BSET_THROTTLE) { 3882 ASSERT(prop_list != NULL); 3883 un->un_saved_throttle = un->un_throttle = 3884 prop_list->sdt_throttle; 3885 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3886 "sd_set_vers1_properties: throttle set to %d\n", 3887 prop_list->sdt_throttle); 3888 } 3889 3890 /* Set the per disk retry count according to the conf file or table. */ 3891 if (flags & SD_CONF_BSET_NRR_COUNT) { 3892 ASSERT(prop_list != NULL); 3893 if (prop_list->sdt_not_rdy_retries) { 3894 un->un_notready_retry_count = 3895 prop_list->sdt_not_rdy_retries; 3896 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3897 "sd_set_vers1_properties: not ready retry count" 3898 " set to %d\n", un->un_notready_retry_count); 3899 } 3900 } 3901 3902 /* The controller type is reported for generic disk driver ioctls */ 3903 if (flags & SD_CONF_BSET_CTYPE) { 3904 ASSERT(prop_list != NULL); 3905 switch (prop_list->sdt_ctype) { 3906 case CTYPE_CDROM: 3907 un->un_ctype = prop_list->sdt_ctype; 3908 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3909 "sd_set_vers1_properties: ctype set to " 3910 "CTYPE_CDROM\n"); 3911 break; 3912 case CTYPE_CCS: 3913 un->un_ctype = prop_list->sdt_ctype; 3914 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3915 "sd_set_vers1_properties: ctype set to " 3916 "CTYPE_CCS\n"); 3917 break; 3918 case CTYPE_ROD: /* RW optical */ 3919 un->un_ctype = prop_list->sdt_ctype; 3920 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3921 "sd_set_vers1_properties: ctype set to " 3922 "CTYPE_ROD\n"); 3923 break; 3924 default: 3925 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3926 "sd_set_vers1_properties: Could not set " 3927 "invalid ctype value (%d)", 3928 prop_list->sdt_ctype); 3929 } 3930 } 3931 3932 /* Purple failover timeout */ 3933 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 3934 ASSERT(prop_list != NULL); 3935 un->un_busy_retry_count = 3936 prop_list->sdt_busy_retries; 3937 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3938 "sd_set_vers1_properties: " 3939 "busy retry count set to %d\n", 3940 un->un_busy_retry_count); 3941 } 3942 3943 /* Purple reset retry count */ 3944 if (flags & SD_CONF_BSET_RST_RETRIES) { 3945 ASSERT(prop_list != NULL); 3946 un->un_reset_retry_count = 3947 prop_list->sdt_reset_retries; 3948 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3949 "sd_set_vers1_properties: " 3950 "reset retry count set to %d\n", 3951 un->un_reset_retry_count); 3952 } 3953 3954 /* Purple reservation release timeout */ 3955 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 3956 ASSERT(prop_list != NULL); 3957 un->un_reserve_release_time = 3958 prop_list->sdt_reserv_rel_time; 3959 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3960 "sd_set_vers1_properties: " 3961 "reservation release timeout set to %d\n", 3962 un->un_reserve_release_time); 3963 } 3964 3965 /* 3966 * Driver flag telling the driver to verify that no commands are pending 3967 * for a device before issuing a Test Unit Ready. This is a workaround 3968 * for a firmware bug in some Seagate eliteI drives. 3969 */ 3970 if (flags & SD_CONF_BSET_TUR_CHECK) { 3971 un->un_f_cfg_tur_check = TRUE; 3972 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3973 "sd_set_vers1_properties: tur queue check set\n"); 3974 } 3975 3976 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 3977 un->un_min_throttle = prop_list->sdt_min_throttle; 3978 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3979 "sd_set_vers1_properties: min throttle set to %d\n", 3980 un->un_min_throttle); 3981 } 3982 3983 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 3984 un->un_f_disksort_disabled = 3985 (prop_list->sdt_disk_sort_dis != 0) ? 3986 TRUE : FALSE; 3987 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3988 "sd_set_vers1_properties: disksort disabled " 3989 "flag set to %d\n", 3990 prop_list->sdt_disk_sort_dis); 3991 } 3992 3993 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 3994 un->un_f_lun_reset_enabled = 3995 (prop_list->sdt_lun_reset_enable != 0) ? 3996 TRUE : FALSE; 3997 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3998 "sd_set_vers1_properties: lun reset enabled " 3999 "flag set to %d\n", 4000 prop_list->sdt_lun_reset_enable); 4001 } 4002 4003 /* 4004 * Validate the throttle values. 4005 * If any of the numbers are invalid, set everything to defaults. 4006 */ 4007 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4008 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4009 (un->un_min_throttle > un->un_throttle)) { 4010 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4011 un->un_min_throttle = sd_min_throttle; 4012 } 4013 } 4014 4015 /* 4016 * Function: sd_is_lsi() 4017 * 4018 * Description: Check for lsi devices, step throught the static device 4019 * table to match vid/pid. 4020 * 4021 * Args: un - ptr to sd_lun 4022 * 4023 * Notes: When creating new LSI property, need to add the new LSI property 4024 * to this function. 4025 */ 4026 static void 4027 sd_is_lsi(struct sd_lun *un) 4028 { 4029 char *id = NULL; 4030 int table_index; 4031 int idlen; 4032 void *prop; 4033 4034 ASSERT(un != NULL); 4035 for (table_index = 0; table_index < sd_disk_table_size; 4036 table_index++) { 4037 id = sd_disk_table[table_index].device_id; 4038 idlen = strlen(id); 4039 if (idlen == 0) { 4040 continue; 4041 } 4042 4043 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4044 prop = sd_disk_table[table_index].properties; 4045 if (prop == &lsi_properties || 4046 prop == &lsi_oem_properties || 4047 prop == &lsi_properties_scsi || 4048 prop == &symbios_properties) { 4049 un->un_f_cfg_is_lsi = TRUE; 4050 } 4051 break; 4052 } 4053 } 4054 } 4055 4056 4057 /* 4058 * The following routines support reading and interpretation of disk labels, 4059 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4060 * fdisk tables. 4061 */ 4062 4063 /* 4064 * Function: sd_validate_geometry 4065 * 4066 * Description: Read the label from the disk (if present). Update the unit's 4067 * geometry and vtoc information from the data in the label. 4068 * Verify that the label is valid. 4069 * 4070 * Arguments: un - driver soft state (unit) structure 4071 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4072 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4073 * to use the USCSI "direct" chain and bypass the normal 4074 * command waitq. 4075 * 4076 * Return Code: 0 - Successful completion 4077 * EINVAL - Invalid value in un->un_tgt_blocksize or 4078 * un->un_blockcount; or label on disk is corrupted 4079 * or unreadable. 4080 * EACCES - Reservation conflict at the device. 4081 * ENOMEM - Resource allocation error 4082 * ENOTSUP - geometry not applicable 4083 * 4084 * Context: Kernel thread only (can sleep). 4085 */ 4086 4087 static int 4088 sd_validate_geometry(struct sd_lun *un, int path_flag) 4089 { 4090 static char labelstring[128]; 4091 static char buf[256]; 4092 char *label = NULL; 4093 int label_error = 0; 4094 int gvalid = un->un_f_geometry_is_valid; 4095 int lbasize; 4096 uint_t capacity; 4097 int count; 4098 4099 ASSERT(un != NULL); 4100 ASSERT(mutex_owned(SD_MUTEX(un))); 4101 4102 /* 4103 * If the required values are not valid, then try getting them 4104 * once via read capacity. If that fails, then fail this call. 4105 * This is necessary with the new mpxio failover behavior in 4106 * the T300 where we can get an attach for the inactive path 4107 * before the active path. The inactive path fails commands with 4108 * sense data of 02,04,88 which happens to the read capacity 4109 * before mpxio has had sufficient knowledge to know if it should 4110 * force a fail over or not. (Which it won't do at attach anyhow). 4111 * If the read capacity at attach time fails, un_tgt_blocksize and 4112 * un_blockcount won't be valid. 4113 */ 4114 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4115 (un->un_f_blockcount_is_valid != TRUE)) { 4116 uint64_t cap; 4117 uint32_t lbasz; 4118 int rval; 4119 4120 mutex_exit(SD_MUTEX(un)); 4121 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4122 &lbasz, SD_PATH_DIRECT); 4123 mutex_enter(SD_MUTEX(un)); 4124 if (rval == 0) { 4125 /* 4126 * The following relies on 4127 * sd_send_scsi_READ_CAPACITY never 4128 * returning 0 for capacity and/or lbasize. 4129 */ 4130 sd_update_block_info(un, lbasz, cap); 4131 } 4132 4133 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4134 (un->un_f_blockcount_is_valid != TRUE)) { 4135 return (EINVAL); 4136 } 4137 } 4138 4139 /* 4140 * Copy the lbasize and capacity so that if they're reset while we're 4141 * not holding the SD_MUTEX, we will continue to use valid values 4142 * after the SD_MUTEX is reacquired. (4119659) 4143 */ 4144 lbasize = un->un_tgt_blocksize; 4145 capacity = un->un_blockcount; 4146 4147 #if defined(_SUNOS_VTOC_16) 4148 /* 4149 * Set up the "whole disk" fdisk partition; this should always 4150 * exist, regardless of whether the disk contains an fdisk table 4151 * or vtoc. 4152 */ 4153 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4154 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4155 #endif 4156 4157 /* 4158 * Refresh the logical and physical geometry caches. 4159 * (data from MODE SENSE format/rigid disk geometry pages, 4160 * and scsi_ifgetcap("geometry"). 4161 */ 4162 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4163 4164 label_error = sd_use_efi(un, path_flag); 4165 if (label_error == 0) { 4166 /* found a valid EFI label */ 4167 SD_TRACE(SD_LOG_IO_PARTITION, un, 4168 "sd_validate_geometry: found EFI label\n"); 4169 un->un_solaris_offset = 0; 4170 un->un_solaris_size = capacity; 4171 return (ENOTSUP); 4172 } 4173 if (un->un_blockcount > DK_MAX_BLOCKS) { 4174 if (label_error == ESRCH) { 4175 /* 4176 * they've configured a LUN over 1TB, but used 4177 * format.dat to restrict format's view of the 4178 * capacity to be under 1TB 4179 */ 4180 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4181 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4182 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4183 "size to be < 1TB or relabel the disk with an EFI label"); 4184 } else { 4185 /* unlabeled disk over 1TB */ 4186 return (ENOTSUP); 4187 } 4188 } 4189 label_error = 0; 4190 4191 /* 4192 * at this point it is either labeled with a VTOC or it is 4193 * under 1TB 4194 */ 4195 4196 /* 4197 * Only DIRECT ACCESS devices will have Sun labels. 4198 * CD's supposedly have a Sun label, too 4199 */ 4200 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) { 4201 struct dk_label *dkl; 4202 offset_t dkl1; 4203 offset_t label_addr, real_addr; 4204 int rval; 4205 size_t buffer_size; 4206 4207 /* 4208 * Note: This will set up un->un_solaris_size and 4209 * un->un_solaris_offset. 4210 */ 4211 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4212 case SD_CMD_RESERVATION_CONFLICT: 4213 ASSERT(mutex_owned(SD_MUTEX(un))); 4214 return (EACCES); 4215 case SD_CMD_FAILURE: 4216 ASSERT(mutex_owned(SD_MUTEX(un))); 4217 return (ENOMEM); 4218 } 4219 4220 if (un->un_solaris_size <= DK_LABEL_LOC) { 4221 /* 4222 * Found fdisk table but no Solaris partition entry, 4223 * so don't call sd_uselabel() and don't create 4224 * a default label. 4225 */ 4226 label_error = 0; 4227 un->un_f_geometry_is_valid = TRUE; 4228 goto no_solaris_partition; 4229 } 4230 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4231 4232 /* 4233 * sys_blocksize != tgt_blocksize, need to re-adjust 4234 * blkno and save the index to beginning of dk_label 4235 */ 4236 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4237 buffer_size = SD_REQBYTES2TGTBYTES(un, 4238 sizeof (struct dk_label)); 4239 4240 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4241 "label_addr: 0x%x allocation size: 0x%x\n", 4242 label_addr, buffer_size); 4243 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4244 if (dkl == NULL) { 4245 return (ENOMEM); 4246 } 4247 4248 mutex_exit(SD_MUTEX(un)); 4249 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4250 path_flag); 4251 mutex_enter(SD_MUTEX(un)); 4252 4253 switch (rval) { 4254 case 0: 4255 /* 4256 * sd_uselabel will establish that the geometry 4257 * is valid. 4258 * For sys_blocksize != tgt_blocksize, need 4259 * to index into the beginning of dk_label 4260 */ 4261 dkl1 = (daddr_t)dkl 4262 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4263 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4264 path_flag) != SD_LABEL_IS_VALID) { 4265 label_error = EINVAL; 4266 } 4267 break; 4268 case EACCES: 4269 label_error = EACCES; 4270 break; 4271 default: 4272 label_error = EINVAL; 4273 break; 4274 } 4275 4276 kmem_free(dkl, buffer_size); 4277 4278 #if defined(_SUNOS_VTOC_8) 4279 label = (char *)un->un_asciilabel; 4280 #elif defined(_SUNOS_VTOC_16) 4281 label = (char *)un->un_vtoc.v_asciilabel; 4282 #else 4283 #error "No VTOC format defined." 4284 #endif 4285 } 4286 4287 /* 4288 * If a valid label was not found, AND if no reservation conflict 4289 * was detected, then go ahead and create a default label (4069506). 4290 * 4291 * Note: currently, for VTOC_8 devices, the default label is created 4292 * for removables only. For VTOC_16 devices, the default label will 4293 * be created for both removables and non-removables alike. 4294 * (see sd_build_default_label) 4295 */ 4296 #if defined(_SUNOS_VTOC_8) 4297 if (ISREMOVABLE(un) && (label_error != EACCES)) { 4298 #elif defined(_SUNOS_VTOC_16) 4299 if (label_error != EACCES) { 4300 #endif 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 ((!ISREMOVABLE(un) || 4309 (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) && 4310 (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) { 4311 /* 4312 * Print out a message indicating who and what we are. 4313 * We do this only when we happen to really validate the 4314 * geometry. We may call sd_validate_geometry() at other 4315 * times, e.g., ioctl()'s like Get VTOC in which case we 4316 * don't want to print the label. 4317 * If the geometry is valid, print the label string, 4318 * else print vendor and product info, if available 4319 */ 4320 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4321 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4322 } else { 4323 mutex_enter(&sd_label_mutex); 4324 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4325 labelstring); 4326 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4327 &labelstring[64]); 4328 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4329 labelstring, &labelstring[64]); 4330 if (un->un_f_blockcount_is_valid == TRUE) { 4331 (void) sprintf(&buf[strlen(buf)], 4332 ", %llu %u byte blocks\n", 4333 (longlong_t)un->un_blockcount, 4334 un->un_tgt_blocksize); 4335 } else { 4336 (void) sprintf(&buf[strlen(buf)], 4337 ", (unknown capacity)\n"); 4338 } 4339 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4340 mutex_exit(&sd_label_mutex); 4341 } 4342 } 4343 4344 #if defined(_SUNOS_VTOC_16) 4345 /* 4346 * If we have valid geometry, set up the remaining fdisk partitions. 4347 * Note that dkl_cylno is not used for the fdisk map entries, so 4348 * we set it to an entirely bogus value. 4349 */ 4350 for (count = 0; count < FD_NUMPART; count++) { 4351 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4352 un->un_map[FDISK_P1 + count].dkl_nblk = 4353 un->un_fmap[count].fmap_nblk; 4354 4355 un->un_offset[FDISK_P1 + count] = 4356 un->un_fmap[count].fmap_start; 4357 } 4358 #endif 4359 4360 for (count = 0; count < NDKMAP; count++) { 4361 #if defined(_SUNOS_VTOC_8) 4362 struct dk_map *lp = &un->un_map[count]; 4363 un->un_offset[count] = 4364 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4365 #elif defined(_SUNOS_VTOC_16) 4366 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4367 4368 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4369 #else 4370 #error "No VTOC format defined." 4371 #endif 4372 } 4373 4374 return (label_error); 4375 } 4376 4377 4378 #if defined(_SUNOS_VTOC_16) 4379 /* 4380 * Macro: MAX_BLKS 4381 * 4382 * This macro is used for table entries where we need to have the largest 4383 * possible sector value for that head & SPT (sectors per track) 4384 * combination. Other entries for some smaller disk sizes are set by 4385 * convention to match those used by X86 BIOS usage. 4386 */ 4387 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4388 4389 /* 4390 * Function: sd_convert_geometry 4391 * 4392 * Description: Convert physical geometry into a dk_geom structure. In 4393 * other words, make sure we don't wrap 16-bit values. 4394 * e.g. converting from geom_cache to dk_geom 4395 * 4396 * Context: Kernel thread only 4397 */ 4398 static void 4399 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4400 { 4401 int i; 4402 static const struct chs_values { 4403 uint_t max_cap; /* Max Capacity for this HS. */ 4404 uint_t nhead; /* Heads to use. */ 4405 uint_t nsect; /* SPT to use. */ 4406 } CHS_values[] = { 4407 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4408 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4409 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4410 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4411 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4412 }; 4413 4414 /* Unlabeled SCSI floppy device */ 4415 if (capacity <= 0x1000) { 4416 un_g->dkg_nhead = 2; 4417 un_g->dkg_ncyl = 80; 4418 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4419 return; 4420 } 4421 4422 /* 4423 * For all devices we calculate cylinders using the 4424 * heads and sectors we assign based on capacity of the 4425 * device. The table is designed to be compatible with the 4426 * way other operating systems lay out fdisk tables for X86 4427 * and to insure that the cylinders never exceed 65535 to 4428 * prevent problems with X86 ioctls that report geometry. 4429 * We use SPT that are multiples of 63, since other OSes that 4430 * are not limited to 16-bits for cylinders stop at 63 SPT 4431 * we make do by using multiples of 63 SPT. 4432 * 4433 * Note than capacities greater than or equal to 1TB will simply 4434 * get the largest geometry from the table. This should be okay 4435 * since disks this large shouldn't be using CHS values anyway. 4436 */ 4437 for (i = 0; CHS_values[i].max_cap < capacity && 4438 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4439 ; 4440 4441 un_g->dkg_nhead = CHS_values[i].nhead; 4442 un_g->dkg_nsect = CHS_values[i].nsect; 4443 } 4444 #endif 4445 4446 4447 /* 4448 * Function: sd_resync_geom_caches 4449 * 4450 * Description: (Re)initialize both geometry caches: the virtual geometry 4451 * information is extracted from the HBA (the "geometry" 4452 * capability), and the physical geometry cache data is 4453 * generated by issuing MODE SENSE commands. 4454 * 4455 * Arguments: un - driver soft state (unit) structure 4456 * capacity - disk capacity in #blocks 4457 * lbasize - disk block size in bytes 4458 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4459 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4460 * to use the USCSI "direct" chain and bypass the normal 4461 * command waitq. 4462 * 4463 * Context: Kernel thread only (can sleep). 4464 */ 4465 4466 static void 4467 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4468 int path_flag) 4469 { 4470 struct geom_cache pgeom; 4471 struct geom_cache *pgeom_p = &pgeom; 4472 int spc; 4473 unsigned short nhead; 4474 unsigned short nsect; 4475 4476 ASSERT(un != NULL); 4477 ASSERT(mutex_owned(SD_MUTEX(un))); 4478 4479 /* 4480 * Ask the controller for its logical geometry. 4481 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4482 * then the lgeom cache will be invalid. 4483 */ 4484 sd_get_virtual_geometry(un, capacity, lbasize); 4485 4486 /* 4487 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4488 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4489 */ 4490 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4491 /* 4492 * Note: Perhaps this needs to be more adaptive? The rationale 4493 * is that, if there's no HBA geometry from the HBA driver, any 4494 * guess is good, since this is the physical geometry. If MODE 4495 * SENSE fails this gives a max cylinder size for non-LBA access 4496 */ 4497 nhead = 255; 4498 nsect = 63; 4499 } else { 4500 nhead = un->un_lgeom.g_nhead; 4501 nsect = un->un_lgeom.g_nsect; 4502 } 4503 4504 if (ISCD(un)) { 4505 pgeom_p->g_nhead = 1; 4506 pgeom_p->g_nsect = nsect * nhead; 4507 } else { 4508 pgeom_p->g_nhead = nhead; 4509 pgeom_p->g_nsect = nsect; 4510 } 4511 4512 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4513 pgeom_p->g_capacity = capacity; 4514 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4515 pgeom_p->g_acyl = 0; 4516 4517 /* 4518 * Retrieve fresh geometry data from the hardware, stash it 4519 * here temporarily before we rebuild the incore label. 4520 * 4521 * We want to use the MODE SENSE commands to derive the 4522 * physical geometry of the device, but if either command 4523 * fails, the logical geometry is used as the fallback for 4524 * disk label geometry. 4525 */ 4526 mutex_exit(SD_MUTEX(un)); 4527 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4528 mutex_enter(SD_MUTEX(un)); 4529 4530 /* 4531 * Now update the real copy while holding the mutex. This 4532 * way the global copy is never in an inconsistent state. 4533 */ 4534 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4535 4536 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4537 "(cached from lgeom)\n"); 4538 SD_INFO(SD_LOG_COMMON, un, 4539 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4540 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4541 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4542 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4543 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4544 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4545 un->un_pgeom.g_rpm); 4546 } 4547 4548 4549 /* 4550 * Function: sd_read_fdisk 4551 * 4552 * Description: utility routine to read the fdisk table. 4553 * 4554 * Arguments: un - driver soft state (unit) structure 4555 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4556 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4557 * to use the USCSI "direct" chain and bypass the normal 4558 * command waitq. 4559 * 4560 * Return Code: SD_CMD_SUCCESS 4561 * SD_CMD_FAILURE 4562 * 4563 * Context: Kernel thread only (can sleep). 4564 */ 4565 /* ARGSUSED */ 4566 static int 4567 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4568 { 4569 #if defined(_NO_FDISK_PRESENT) 4570 4571 un->un_solaris_offset = 0; 4572 un->un_solaris_size = capacity; 4573 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4574 return (SD_CMD_SUCCESS); 4575 4576 #elif defined(_FIRMWARE_NEEDS_FDISK) 4577 4578 struct ipart *fdp; 4579 struct mboot *mbp; 4580 struct ipart fdisk[FD_NUMPART]; 4581 int i; 4582 char sigbuf[2]; 4583 caddr_t bufp; 4584 int uidx; 4585 int rval; 4586 int lba = 0; 4587 uint_t solaris_offset; /* offset to solaris part. */ 4588 daddr_t solaris_size; /* size of solaris partition */ 4589 uint32_t blocksize; 4590 4591 ASSERT(un != NULL); 4592 ASSERT(mutex_owned(SD_MUTEX(un))); 4593 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4594 4595 blocksize = un->un_tgt_blocksize; 4596 4597 /* 4598 * Start off assuming no fdisk table 4599 */ 4600 solaris_offset = 0; 4601 solaris_size = capacity; 4602 4603 mutex_exit(SD_MUTEX(un)); 4604 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4605 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4606 mutex_enter(SD_MUTEX(un)); 4607 4608 if (rval != 0) { 4609 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4610 "sd_read_fdisk: fdisk read err\n"); 4611 kmem_free(bufp, blocksize); 4612 return (SD_CMD_FAILURE); 4613 } 4614 4615 mbp = (struct mboot *)bufp; 4616 4617 /* 4618 * The fdisk table does not begin on a 4-byte boundary within the 4619 * master boot record, so we copy it to an aligned structure to avoid 4620 * alignment exceptions on some processors. 4621 */ 4622 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4623 4624 /* 4625 * Check for lba support before verifying sig; sig might not be 4626 * there, say on a blank disk, but the max_chs mark may still 4627 * be present. 4628 * 4629 * Note: LBA support and BEFs are an x86-only concept but this 4630 * code should work OK on SPARC as well. 4631 */ 4632 4633 /* 4634 * First, check for lba-access-ok on root node (or prom root node) 4635 * if present there, don't need to search fdisk table. 4636 */ 4637 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4638 "lba-access-ok", 0) != 0) { 4639 /* All drives do LBA; don't search fdisk table */ 4640 lba = 1; 4641 } else { 4642 /* Okay, look for mark in fdisk table */ 4643 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4644 /* accumulate "lba" value from all partitions */ 4645 lba = (lba || sd_has_max_chs_vals(fdp)); 4646 } 4647 } 4648 4649 if (lba != 0) { 4650 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4651 4652 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4653 "lba-access-ok", 0) == 0) { 4654 /* not found; create it */ 4655 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4656 "lba-access-ok", (caddr_t)NULL, 0) != 4657 DDI_PROP_SUCCESS) { 4658 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4659 "sd_read_fdisk: Can't create lba property " 4660 "for instance %d\n", 4661 ddi_get_instance(SD_DEVINFO(un))); 4662 } 4663 } 4664 } 4665 4666 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4667 4668 /* 4669 * Endian-independent signature check 4670 */ 4671 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4672 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4673 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4674 "sd_read_fdisk: no fdisk\n"); 4675 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4676 rval = SD_CMD_SUCCESS; 4677 goto done; 4678 } 4679 4680 #ifdef SDDEBUG 4681 if (sd_level_mask & SD_LOGMASK_INFO) { 4682 fdp = fdisk; 4683 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4684 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4685 "numsect sysid bootid\n"); 4686 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4687 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4688 " %d: %8d %8d 0x%08x 0x%08x\n", 4689 i, fdp->relsect, fdp->numsect, 4690 fdp->systid, fdp->bootid); 4691 } 4692 } 4693 #endif 4694 4695 /* 4696 * Try to find the unix partition 4697 */ 4698 uidx = -1; 4699 solaris_offset = 0; 4700 solaris_size = 0; 4701 4702 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4703 int relsect; 4704 int numsect; 4705 4706 if (fdp->numsect == 0) { 4707 un->un_fmap[i].fmap_start = 0; 4708 un->un_fmap[i].fmap_nblk = 0; 4709 continue; 4710 } 4711 4712 /* 4713 * Data in the fdisk table is little-endian. 4714 */ 4715 relsect = LE_32(fdp->relsect); 4716 numsect = LE_32(fdp->numsect); 4717 4718 un->un_fmap[i].fmap_start = relsect; 4719 un->un_fmap[i].fmap_nblk = numsect; 4720 4721 if (fdp->systid != SUNIXOS && 4722 fdp->systid != SUNIXOS2 && 4723 fdp->systid != EFI_PMBR) { 4724 continue; 4725 } 4726 4727 /* 4728 * use the last active solaris partition id found 4729 * (there should only be 1 active partition id) 4730 * 4731 * if there are no active solaris partition id 4732 * then use the first inactive solaris partition id 4733 */ 4734 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 4735 uidx = i; 4736 solaris_offset = relsect; 4737 solaris_size = numsect; 4738 } 4739 } 4740 4741 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 4742 un->un_solaris_offset, un->un_solaris_size); 4743 4744 rval = SD_CMD_SUCCESS; 4745 4746 done: 4747 4748 /* 4749 * Clear the VTOC info, only if the Solaris partition entry 4750 * has moved, changed size, been deleted, or if the size of 4751 * the partition is too small to even fit the label sector. 4752 */ 4753 if ((un->un_solaris_offset != solaris_offset) || 4754 (un->un_solaris_size != solaris_size) || 4755 solaris_size <= DK_LABEL_LOC) { 4756 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 4757 solaris_offset, solaris_size); 4758 bzero(&un->un_g, sizeof (struct dk_geom)); 4759 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 4760 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 4761 un->un_f_geometry_is_valid = FALSE; 4762 } 4763 un->un_solaris_offset = solaris_offset; 4764 un->un_solaris_size = solaris_size; 4765 kmem_free(bufp, blocksize); 4766 return (rval); 4767 4768 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 4769 #error "fdisk table presence undetermined for this platform." 4770 #endif /* #if defined(_NO_FDISK_PRESENT) */ 4771 } 4772 4773 4774 /* 4775 * Function: sd_get_physical_geometry 4776 * 4777 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4778 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4779 * target, and use this information to initialize the physical 4780 * geometry cache specified by pgeom_p. 4781 * 4782 * MODE SENSE is an optional command, so failure in this case 4783 * does not necessarily denote an error. We want to use the 4784 * MODE SENSE commands to derive the physical geometry of the 4785 * device, but if either command fails, the logical geometry is 4786 * used as the fallback for disk label geometry. 4787 * 4788 * This requires that un->un_blockcount and un->un_tgt_blocksize 4789 * have already been initialized for the current target and 4790 * that the current values be passed as args so that we don't 4791 * end up ever trying to use -1 as a valid value. This could 4792 * happen if either value is reset while we're not holding 4793 * the mutex. 4794 * 4795 * Arguments: un - driver soft state (unit) structure 4796 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4797 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4798 * to use the USCSI "direct" chain and bypass the normal 4799 * command waitq. 4800 * 4801 * Context: Kernel thread only (can sleep). 4802 */ 4803 4804 static void 4805 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 4806 int capacity, int lbasize, int path_flag) 4807 { 4808 struct mode_format *page3p; 4809 struct mode_geometry *page4p; 4810 struct mode_header *headerp; 4811 int sector_size; 4812 int nsect; 4813 int nhead; 4814 int ncyl; 4815 int intrlv; 4816 int spc; 4817 int modesense_capacity; 4818 int rpm; 4819 int bd_len; 4820 int mode_header_length; 4821 uchar_t *p3bufp; 4822 uchar_t *p4bufp; 4823 int cdbsize; 4824 4825 ASSERT(un != NULL); 4826 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 4827 4828 if (un->un_f_blockcount_is_valid != TRUE) { 4829 return; 4830 } 4831 4832 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 4833 return; 4834 } 4835 4836 if (lbasize == 0) { 4837 if (ISCD(un)) { 4838 lbasize = 2048; 4839 } else { 4840 lbasize = un->un_sys_blocksize; 4841 } 4842 } 4843 pgeom_p->g_secsize = (unsigned short)lbasize; 4844 4845 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4846 4847 /* 4848 * Retrieve MODE SENSE page 3 - Format Device Page 4849 */ 4850 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4851 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4852 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4853 != 0) { 4854 SD_ERROR(SD_LOG_COMMON, un, 4855 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4856 goto page3_exit; 4857 } 4858 4859 /* 4860 * Determine size of Block Descriptors in order to locate the mode 4861 * page data. ATAPI devices return 0, SCSI devices should return 4862 * MODE_BLK_DESC_LENGTH. 4863 */ 4864 headerp = (struct mode_header *)p3bufp; 4865 if (un->un_f_cfg_is_atapi == TRUE) { 4866 struct mode_header_grp2 *mhp = 4867 (struct mode_header_grp2 *)headerp; 4868 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4869 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4870 } else { 4871 mode_header_length = MODE_HEADER_LENGTH; 4872 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4873 } 4874 4875 if (bd_len > MODE_BLK_DESC_LENGTH) { 4876 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4877 "received unexpected bd_len of %d, page3\n", bd_len); 4878 goto page3_exit; 4879 } 4880 4881 page3p = (struct mode_format *) 4882 ((caddr_t)headerp + mode_header_length + bd_len); 4883 4884 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4885 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4886 "mode sense pg3 code mismatch %d\n", 4887 page3p->mode_page.code); 4888 goto page3_exit; 4889 } 4890 4891 /* 4892 * Use this physical geometry data only if BOTH MODE SENSE commands 4893 * complete successfully; otherwise, revert to the logical geometry. 4894 * So, we need to save everything in temporary variables. 4895 */ 4896 sector_size = BE_16(page3p->data_bytes_sect); 4897 4898 /* 4899 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4900 */ 4901 if (sector_size == 0) { 4902 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 4903 } else { 4904 sector_size &= ~(un->un_sys_blocksize - 1); 4905 } 4906 4907 nsect = BE_16(page3p->sect_track); 4908 intrlv = BE_16(page3p->interleave); 4909 4910 SD_INFO(SD_LOG_COMMON, un, 4911 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4912 SD_INFO(SD_LOG_COMMON, un, 4913 " mode page: %d; nsect: %d; sector size: %d;\n", 4914 page3p->mode_page.code, nsect, sector_size); 4915 SD_INFO(SD_LOG_COMMON, un, 4916 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4917 BE_16(page3p->track_skew), 4918 BE_16(page3p->cylinder_skew)); 4919 4920 4921 /* 4922 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4923 */ 4924 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4925 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4926 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4927 != 0) { 4928 SD_ERROR(SD_LOG_COMMON, un, 4929 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4930 goto page4_exit; 4931 } 4932 4933 /* 4934 * Determine size of Block Descriptors in order to locate the mode 4935 * page data. ATAPI devices return 0, SCSI devices should return 4936 * MODE_BLK_DESC_LENGTH. 4937 */ 4938 headerp = (struct mode_header *)p4bufp; 4939 if (un->un_f_cfg_is_atapi == TRUE) { 4940 struct mode_header_grp2 *mhp = 4941 (struct mode_header_grp2 *)headerp; 4942 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4943 } else { 4944 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4945 } 4946 4947 if (bd_len > MODE_BLK_DESC_LENGTH) { 4948 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4949 "received unexpected bd_len of %d, page4\n", bd_len); 4950 goto page4_exit; 4951 } 4952 4953 page4p = (struct mode_geometry *) 4954 ((caddr_t)headerp + mode_header_length + bd_len); 4955 4956 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4957 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4958 "mode sense pg4 code mismatch %d\n", 4959 page4p->mode_page.code); 4960 goto page4_exit; 4961 } 4962 4963 /* 4964 * Stash the data now, after we know that both commands completed. 4965 */ 4966 4967 mutex_enter(SD_MUTEX(un)); 4968 4969 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4970 spc = nhead * nsect; 4971 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4972 rpm = BE_16(page4p->rpm); 4973 4974 modesense_capacity = spc * ncyl; 4975 4976 SD_INFO(SD_LOG_COMMON, un, 4977 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4978 SD_INFO(SD_LOG_COMMON, un, 4979 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4980 SD_INFO(SD_LOG_COMMON, un, 4981 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4982 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4983 (void *)pgeom_p, capacity); 4984 4985 /* 4986 * Compensate if the drive's geometry is not rectangular, i.e., 4987 * the product of C * H * S returned by MODE SENSE >= that returned 4988 * by read capacity. This is an idiosyncrasy of the original x86 4989 * disk subsystem. 4990 */ 4991 if (modesense_capacity >= capacity) { 4992 SD_INFO(SD_LOG_COMMON, un, 4993 "sd_get_physical_geometry: adjusting acyl; " 4994 "old: %d; new: %d\n", pgeom_p->g_acyl, 4995 (modesense_capacity - capacity + spc - 1) / spc); 4996 if (sector_size != 0) { 4997 /* 1243403: NEC D38x7 drives don't support sec size */ 4998 pgeom_p->g_secsize = (unsigned short)sector_size; 4999 } 5000 pgeom_p->g_nsect = (unsigned short)nsect; 5001 pgeom_p->g_nhead = (unsigned short)nhead; 5002 pgeom_p->g_capacity = capacity; 5003 pgeom_p->g_acyl = 5004 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5005 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5006 } 5007 5008 pgeom_p->g_rpm = (unsigned short)rpm; 5009 pgeom_p->g_intrlv = (unsigned short)intrlv; 5010 5011 SD_INFO(SD_LOG_COMMON, un, 5012 "sd_get_physical_geometry: mode sense geometry:\n"); 5013 SD_INFO(SD_LOG_COMMON, un, 5014 " nsect: %d; sector size: %d; interlv: %d\n", 5015 nsect, sector_size, intrlv); 5016 SD_INFO(SD_LOG_COMMON, un, 5017 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5018 nhead, ncyl, rpm, modesense_capacity); 5019 SD_INFO(SD_LOG_COMMON, un, 5020 "sd_get_physical_geometry: (cached)\n"); 5021 SD_INFO(SD_LOG_COMMON, un, 5022 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5023 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5024 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5025 SD_INFO(SD_LOG_COMMON, un, 5026 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5027 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5028 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5029 5030 mutex_exit(SD_MUTEX(un)); 5031 5032 page4_exit: 5033 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5034 page3_exit: 5035 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5036 } 5037 5038 5039 /* 5040 * Function: sd_get_virtual_geometry 5041 * 5042 * Description: Ask the controller to tell us about the target device. 5043 * 5044 * Arguments: un - pointer to softstate 5045 * capacity - disk capacity in #blocks 5046 * lbasize - disk block size in bytes 5047 * 5048 * Context: Kernel thread only 5049 */ 5050 5051 static void 5052 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5053 { 5054 struct geom_cache *lgeom_p = &un->un_lgeom; 5055 uint_t geombuf; 5056 int spc; 5057 5058 ASSERT(un != NULL); 5059 ASSERT(mutex_owned(SD_MUTEX(un))); 5060 5061 mutex_exit(SD_MUTEX(un)); 5062 5063 /* Set sector size, and total number of sectors */ 5064 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5065 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5066 5067 /* Let the HBA tell us its geometry */ 5068 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5069 5070 mutex_enter(SD_MUTEX(un)); 5071 5072 /* A value of -1 indicates an undefined "geometry" property */ 5073 if (geombuf == (-1)) { 5074 return; 5075 } 5076 5077 /* Initialize the logical geometry cache. */ 5078 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5079 lgeom_p->g_nsect = geombuf & 0xffff; 5080 lgeom_p->g_secsize = un->un_sys_blocksize; 5081 5082 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5083 5084 /* 5085 * Note: The driver originally converted the capacity value from 5086 * target blocks to system blocks. However, the capacity value passed 5087 * to this routine is already in terms of system blocks (this scaling 5088 * is done when the READ CAPACITY command is issued and processed). 5089 * This 'error' may have gone undetected because the usage of g_ncyl 5090 * (which is based upon g_capacity) is very limited within the driver 5091 */ 5092 lgeom_p->g_capacity = capacity; 5093 5094 /* 5095 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5096 * hba may return zero values if the device has been removed. 5097 */ 5098 if (spc == 0) { 5099 lgeom_p->g_ncyl = 0; 5100 } else { 5101 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5102 } 5103 lgeom_p->g_acyl = 0; 5104 5105 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5106 SD_INFO(SD_LOG_COMMON, un, 5107 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5108 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5109 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5110 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5111 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5112 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5113 } 5114 5115 5116 /* 5117 * Function: sd_update_block_info 5118 * 5119 * Description: Calculate a byte count to sector count bitshift value 5120 * from sector size. 5121 * 5122 * Arguments: un: unit struct. 5123 * lbasize: new target sector size 5124 * capacity: new target capacity, ie. block count 5125 * 5126 * Context: Kernel thread context 5127 */ 5128 5129 static void 5130 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5131 { 5132 if (lbasize != 0) { 5133 un->un_tgt_blocksize = lbasize; 5134 un->un_f_tgt_blocksize_is_valid = TRUE; 5135 } 5136 5137 if (capacity != 0) { 5138 un->un_blockcount = capacity; 5139 un->un_f_blockcount_is_valid = TRUE; 5140 } 5141 } 5142 5143 5144 static void 5145 sd_swap_efi_gpt(efi_gpt_t *e) 5146 { 5147 _NOTE(ASSUMING_PROTECTED(*e)) 5148 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5149 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5150 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5151 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5152 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5153 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5154 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5155 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5156 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5157 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5158 e->efi_gpt_NumberOfPartitionEntries = 5159 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5160 e->efi_gpt_SizeOfPartitionEntry = 5161 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5162 e->efi_gpt_PartitionEntryArrayCRC32 = 5163 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5164 } 5165 5166 static void 5167 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5168 { 5169 int i; 5170 5171 _NOTE(ASSUMING_PROTECTED(*p)) 5172 for (i = 0; i < nparts; i++) { 5173 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5174 p[i].efi_gpe_PartitionTypeGUID); 5175 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5176 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5177 /* PartitionAttrs */ 5178 } 5179 } 5180 5181 static int 5182 sd_validate_efi(efi_gpt_t *labp) 5183 { 5184 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5185 return (EINVAL); 5186 /* at least 96 bytes in this version of the spec. */ 5187 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5188 labp->efi_gpt_HeaderSize) 5189 return (EINVAL); 5190 /* this should be 128 bytes */ 5191 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5192 return (EINVAL); 5193 return (0); 5194 } 5195 5196 static int 5197 sd_use_efi(struct sd_lun *un, int path_flag) 5198 { 5199 int i; 5200 int rval = 0; 5201 efi_gpe_t *partitions; 5202 uchar_t *buf; 5203 uint_t lbasize; 5204 uint64_t cap; 5205 uint_t nparts; 5206 diskaddr_t gpe_lba; 5207 5208 ASSERT(mutex_owned(SD_MUTEX(un))); 5209 lbasize = un->un_tgt_blocksize; 5210 5211 mutex_exit(SD_MUTEX(un)); 5212 5213 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5214 5215 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5216 rval = EINVAL; 5217 goto done_err; 5218 } 5219 5220 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5221 if (rval) { 5222 goto done_err; 5223 } 5224 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5225 /* not ours */ 5226 rval = ESRCH; 5227 goto done_err; 5228 } 5229 5230 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5231 if (rval) { 5232 goto done_err; 5233 } 5234 sd_swap_efi_gpt((efi_gpt_t *)buf); 5235 5236 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5237 /* 5238 * Couldn't read the primary, try the backup. Our 5239 * capacity at this point could be based on CHS, so 5240 * check what the device reports. 5241 */ 5242 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5243 path_flag); 5244 if (rval) { 5245 goto done_err; 5246 } 5247 5248 /* 5249 * The MMC standard allows READ CAPACITY to be 5250 * inaccurate by a bounded amount (in the interest of 5251 * response latency). As a result, failed READs are 5252 * commonplace (due to the reading of metadata and not 5253 * data). Depending on the per-Vendor/drive Sense data, 5254 * the failed READ can cause many (unnecessary) retries. 5255 */ 5256 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5257 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5258 path_flag)) != 0) { 5259 goto done_err; 5260 } 5261 5262 sd_swap_efi_gpt((efi_gpt_t *)buf); 5263 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5264 goto done_err; 5265 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5266 "primary label corrupt; using backup\n"); 5267 } 5268 5269 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5270 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5271 5272 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5273 path_flag); 5274 if (rval) { 5275 goto done_err; 5276 } 5277 partitions = (efi_gpe_t *)buf; 5278 5279 if (nparts > MAXPART) { 5280 nparts = MAXPART; 5281 } 5282 sd_swap_efi_gpe(nparts, partitions); 5283 5284 mutex_enter(SD_MUTEX(un)); 5285 5286 /* Fill in partition table. */ 5287 for (i = 0; i < nparts; i++) { 5288 if (partitions->efi_gpe_StartingLBA != 0 || 5289 partitions->efi_gpe_EndingLBA != 0) { 5290 un->un_map[i].dkl_cylno = 5291 partitions->efi_gpe_StartingLBA; 5292 un->un_map[i].dkl_nblk = 5293 partitions->efi_gpe_EndingLBA - 5294 partitions->efi_gpe_StartingLBA + 1; 5295 un->un_offset[i] = 5296 partitions->efi_gpe_StartingLBA; 5297 } 5298 if (i == WD_NODE) { 5299 /* 5300 * minor number 7 corresponds to the whole disk 5301 */ 5302 un->un_map[i].dkl_cylno = 0; 5303 un->un_map[i].dkl_nblk = un->un_blockcount; 5304 un->un_offset[i] = 0; 5305 } 5306 partitions++; 5307 } 5308 un->un_solaris_offset = 0; 5309 un->un_solaris_size = cap; 5310 un->un_f_geometry_is_valid = TRUE; 5311 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5312 return (0); 5313 5314 done_err: 5315 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5316 mutex_enter(SD_MUTEX(un)); 5317 /* 5318 * if we didn't find something that could look like a VTOC 5319 * and the disk is over 1TB, we know there isn't a valid label. 5320 * Otherwise let sd_uselabel decide what to do. We only 5321 * want to invalidate this if we're certain the label isn't 5322 * valid because sd_prop_op will now fail, which in turn 5323 * causes things like opens and stats on the partition to fail. 5324 */ 5325 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5326 un->un_f_geometry_is_valid = FALSE; 5327 } 5328 return (rval); 5329 } 5330 5331 5332 /* 5333 * Function: sd_uselabel 5334 * 5335 * Description: Validate the disk label and update the relevant data (geometry, 5336 * partition, vtoc, and capacity data) in the sd_lun struct. 5337 * Marks the geometry of the unit as being valid. 5338 * 5339 * Arguments: un: unit struct. 5340 * dk_label: disk label 5341 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5342 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5343 * to use the USCSI "direct" chain and bypass the normal 5344 * command waitq. 5345 * 5346 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5347 * partition, vtoc, and capacity data are good. 5348 * 5349 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5350 * label; or computed capacity does not jibe with capacity 5351 * reported from the READ CAPACITY command. 5352 * 5353 * Context: Kernel thread only (can sleep). 5354 */ 5355 5356 static int 5357 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5358 { 5359 short *sp; 5360 short sum; 5361 short count; 5362 int label_error = SD_LABEL_IS_VALID; 5363 int i; 5364 int capacity; 5365 int part_end; 5366 int track_capacity; 5367 int err; 5368 #if defined(_SUNOS_VTOC_16) 5369 struct dkl_partition *vpartp; 5370 #endif 5371 ASSERT(un != NULL); 5372 ASSERT(mutex_owned(SD_MUTEX(un))); 5373 5374 /* Validate the magic number of the label. */ 5375 if (labp->dkl_magic != DKL_MAGIC) { 5376 #if defined(__sparc) 5377 if ((un->un_state == SD_STATE_NORMAL) && 5378 !ISREMOVABLE(un)) { 5379 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5380 "Corrupt label; wrong magic number\n"); 5381 } 5382 #endif 5383 return (SD_LABEL_IS_INVALID); 5384 } 5385 5386 /* Validate the checksum of the label. */ 5387 sp = (short *)labp; 5388 sum = 0; 5389 count = sizeof (struct dk_label) / sizeof (short); 5390 while (count--) { 5391 sum ^= *sp++; 5392 } 5393 5394 if (sum != 0) { 5395 #if defined(_SUNOS_VTOC_16) 5396 if (un->un_state == SD_STATE_NORMAL && !ISCD(un)) { 5397 #elif defined(_SUNOS_VTOC_8) 5398 if (un->un_state == SD_STATE_NORMAL && !ISREMOVABLE(un)) { 5399 #endif 5400 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5401 "Corrupt label - label checksum failed\n"); 5402 } 5403 return (SD_LABEL_IS_INVALID); 5404 } 5405 5406 5407 /* 5408 * Fill in geometry structure with data from label. 5409 */ 5410 bzero(&un->un_g, sizeof (struct dk_geom)); 5411 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5412 un->un_g.dkg_acyl = labp->dkl_acyl; 5413 un->un_g.dkg_bcyl = 0; 5414 un->un_g.dkg_nhead = labp->dkl_nhead; 5415 un->un_g.dkg_nsect = labp->dkl_nsect; 5416 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5417 5418 #if defined(_SUNOS_VTOC_8) 5419 un->un_g.dkg_gap1 = labp->dkl_gap1; 5420 un->un_g.dkg_gap2 = labp->dkl_gap2; 5421 un->un_g.dkg_bhead = labp->dkl_bhead; 5422 #endif 5423 #if defined(_SUNOS_VTOC_16) 5424 un->un_dkg_skew = labp->dkl_skew; 5425 #endif 5426 5427 #if defined(__i386) || defined(__amd64) 5428 un->un_g.dkg_apc = labp->dkl_apc; 5429 #endif 5430 5431 /* 5432 * Currently we rely on the values in the label being accurate. If 5433 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5434 * 5435 * Note: In the future a MODE SENSE may be used to retrieve this data, 5436 * although this command is optional in SCSI-2. 5437 */ 5438 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5439 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5440 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5441 5442 /* 5443 * The Read and Write reinstruct values may not be valid 5444 * for older disks. 5445 */ 5446 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5447 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5448 5449 /* Fill in partition table. */ 5450 #if defined(_SUNOS_VTOC_8) 5451 for (i = 0; i < NDKMAP; i++) { 5452 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5453 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5454 } 5455 #endif 5456 #if defined(_SUNOS_VTOC_16) 5457 vpartp = labp->dkl_vtoc.v_part; 5458 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5459 5460 /* Prevent divide by zero */ 5461 if (track_capacity == 0) { 5462 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5463 "Corrupt label - zero nhead or nsect value\n"); 5464 5465 return (SD_LABEL_IS_INVALID); 5466 } 5467 5468 for (i = 0; i < NDKMAP; i++, vpartp++) { 5469 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5470 un->un_map[i].dkl_nblk = vpartp->p_size; 5471 } 5472 #endif 5473 5474 /* Fill in VTOC Structure. */ 5475 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5476 #if defined(_SUNOS_VTOC_8) 5477 /* 5478 * The 8-slice vtoc does not include the ascii label; save it into 5479 * the device's soft state structure here. 5480 */ 5481 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5482 #endif 5483 5484 /* Now look for a valid capacity. */ 5485 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5486 capacity = (un->un_g.dkg_ncyl * track_capacity); 5487 5488 if (un->un_g.dkg_acyl) { 5489 #if defined(__i386) || defined(__amd64) 5490 /* we may have > 1 alts cylinder */ 5491 capacity += (track_capacity * un->un_g.dkg_acyl); 5492 #else 5493 capacity += track_capacity; 5494 #endif 5495 } 5496 5497 /* 5498 * Force check here to ensure the computed capacity is valid. 5499 * If capacity is zero, it indicates an invalid label and 5500 * we should abort updating the relevant data then. 5501 */ 5502 if (capacity == 0) { 5503 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5504 "Corrupt label - no valid capacity could be retrieved\n"); 5505 5506 return (SD_LABEL_IS_INVALID); 5507 } 5508 5509 /* Mark the geometry as valid. */ 5510 un->un_f_geometry_is_valid = TRUE; 5511 5512 /* 5513 * At this point, un->un_blockcount should contain valid data from 5514 * the READ CAPACITY command. 5515 */ 5516 if (un->un_f_blockcount_is_valid != TRUE) { 5517 /* 5518 * We have a situation where the target didn't give us a good 5519 * READ CAPACITY value, yet there appears to be a valid label. 5520 * In this case, we'll fake the capacity. 5521 */ 5522 un->un_blockcount = capacity; 5523 un->un_f_blockcount_is_valid = TRUE; 5524 goto done; 5525 } 5526 5527 5528 if ((capacity <= un->un_blockcount) || 5529 (un->un_state != SD_STATE_NORMAL)) { 5530 #if defined(_SUNOS_VTOC_8) 5531 /* 5532 * We can't let this happen on drives that are subdivided 5533 * into logical disks (i.e., that have an fdisk table). 5534 * The un_blockcount field should always hold the full media 5535 * size in sectors, period. This code would overwrite 5536 * un_blockcount with the size of the Solaris fdisk partition. 5537 */ 5538 SD_ERROR(SD_LOG_COMMON, un, 5539 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5540 capacity, un->un_blockcount); 5541 un->un_blockcount = capacity; 5542 un->un_f_blockcount_is_valid = TRUE; 5543 #endif /* defined(_SUNOS_VTOC_8) */ 5544 goto done; 5545 } 5546 5547 if (ISCD(un)) { 5548 /* For CDROMs, we trust that the data in the label is OK. */ 5549 #if defined(_SUNOS_VTOC_8) 5550 for (i = 0; i < NDKMAP; i++) { 5551 part_end = labp->dkl_nhead * labp->dkl_nsect * 5552 labp->dkl_map[i].dkl_cylno + 5553 labp->dkl_map[i].dkl_nblk - 1; 5554 5555 if ((labp->dkl_map[i].dkl_nblk) && 5556 (part_end > un->un_blockcount)) { 5557 un->un_f_geometry_is_valid = FALSE; 5558 break; 5559 } 5560 } 5561 #endif 5562 #if defined(_SUNOS_VTOC_16) 5563 vpartp = &(labp->dkl_vtoc.v_part[0]); 5564 for (i = 0; i < NDKMAP; i++, vpartp++) { 5565 part_end = vpartp->p_start + vpartp->p_size; 5566 if ((vpartp->p_size > 0) && 5567 (part_end > un->un_blockcount)) { 5568 un->un_f_geometry_is_valid = FALSE; 5569 break; 5570 } 5571 } 5572 #endif 5573 } else { 5574 uint64_t t_capacity; 5575 uint32_t t_lbasize; 5576 5577 mutex_exit(SD_MUTEX(un)); 5578 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5579 path_flag); 5580 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5581 mutex_enter(SD_MUTEX(un)); 5582 5583 if (err == 0) { 5584 sd_update_block_info(un, t_lbasize, t_capacity); 5585 } 5586 5587 if (capacity > un->un_blockcount) { 5588 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5589 "Corrupt label - bad geometry\n"); 5590 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5591 "Label says %u blocks; Drive says %llu blocks\n", 5592 capacity, (unsigned long long)un->un_blockcount); 5593 un->un_f_geometry_is_valid = FALSE; 5594 label_error = SD_LABEL_IS_INVALID; 5595 } 5596 } 5597 5598 done: 5599 5600 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5601 SD_INFO(SD_LOG_COMMON, un, 5602 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5603 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5604 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5605 SD_INFO(SD_LOG_COMMON, un, 5606 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5607 un->un_tgt_blocksize, un->un_blockcount, 5608 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5609 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5610 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5611 5612 ASSERT(mutex_owned(SD_MUTEX(un))); 5613 5614 return (label_error); 5615 } 5616 5617 5618 /* 5619 * Function: sd_build_default_label 5620 * 5621 * Description: Generate a default label for those devices that do not have 5622 * one, e.g., new media, removable cartridges, etc.. 5623 * 5624 * Context: Kernel thread only 5625 */ 5626 5627 static void 5628 sd_build_default_label(struct sd_lun *un) 5629 { 5630 #if defined(_SUNOS_VTOC_16) 5631 uint_t phys_spc; 5632 uint_t disksize; 5633 struct dk_geom un_g; 5634 #endif 5635 5636 ASSERT(un != NULL); 5637 ASSERT(mutex_owned(SD_MUTEX(un))); 5638 5639 #if defined(_SUNOS_VTOC_8) 5640 /* 5641 * Note: This is a legacy check for non-removable devices on VTOC_8 5642 * only. This may be a valid check for VTOC_16 as well. 5643 */ 5644 if (!ISREMOVABLE(un)) { 5645 return; 5646 } 5647 #endif 5648 5649 bzero(&un->un_g, sizeof (struct dk_geom)); 5650 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5651 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5652 5653 #if defined(_SUNOS_VTOC_8) 5654 5655 /* 5656 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5657 * But it is still necessary to set up various geometry information, 5658 * and we are doing this here. 5659 */ 5660 5661 /* 5662 * For the rpm, we use the minimum for the disk. For the head, cyl, 5663 * and number of sector per track, if the capacity <= 1GB, head = 64, 5664 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5665 * equal to C*H*S values. This will cause some truncation of size due 5666 * to round off errors. For CD-ROMs, this truncation can have adverse 5667 * side effects, so returning ncyl and nhead as 1. The nsect will 5668 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5669 */ 5670 if (ISCD(un)) { 5671 /* 5672 * Preserve the old behavior for non-writable 5673 * medias. Since dkg_nsect is a ushort, it 5674 * will lose bits as cdroms have more than 5675 * 65536 sectors. So if we recalculate 5676 * capacity, it will become much shorter. 5677 * But the dkg_* information is not 5678 * used for CDROMs so it is OK. But for 5679 * Writable CDs we need this information 5680 * to be valid (for newfs say). So we 5681 * make nsect and nhead > 1 that way 5682 * nsect can still stay within ushort limit 5683 * without losing any bits. 5684 */ 5685 if (un->un_f_mmc_writable_media == TRUE) { 5686 un->un_g.dkg_nhead = 64; 5687 un->un_g.dkg_nsect = 32; 5688 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5689 un->un_blockcount = un->un_g.dkg_ncyl * 5690 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5691 } else { 5692 un->un_g.dkg_ncyl = 1; 5693 un->un_g.dkg_nhead = 1; 5694 un->un_g.dkg_nsect = un->un_blockcount; 5695 } 5696 } else { 5697 if (un->un_blockcount <= 0x1000) { 5698 /* unlabeled SCSI floppy device */ 5699 un->un_g.dkg_nhead = 2; 5700 un->un_g.dkg_ncyl = 80; 5701 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 5702 } else if (un->un_blockcount <= 0x200000) { 5703 un->un_g.dkg_nhead = 64; 5704 un->un_g.dkg_nsect = 32; 5705 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 5706 } else { 5707 un->un_g.dkg_nhead = 255; 5708 un->un_g.dkg_nsect = 63; 5709 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 5710 } 5711 un->un_blockcount = 5712 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5713 } 5714 5715 un->un_g.dkg_acyl = 0; 5716 un->un_g.dkg_bcyl = 0; 5717 un->un_g.dkg_rpm = 200; 5718 un->un_asciilabel[0] = '\0'; 5719 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 5720 5721 un->un_map[0].dkl_cylno = 0; 5722 un->un_map[0].dkl_nblk = un->un_blockcount; 5723 un->un_map[2].dkl_cylno = 0; 5724 un->un_map[2].dkl_nblk = un->un_blockcount; 5725 5726 #elif defined(_SUNOS_VTOC_16) 5727 5728 if (un->un_solaris_size == 0) { 5729 /* 5730 * Got fdisk table but no solaris entry therefore 5731 * don't create a default label 5732 */ 5733 un->un_f_geometry_is_valid = TRUE; 5734 return; 5735 } 5736 5737 /* 5738 * For CDs we continue to use the physical geometry to calculate 5739 * number of cylinders. All other devices must convert the 5740 * physical geometry (geom_cache) to values that will fit 5741 * in a dk_geom structure. 5742 */ 5743 if (ISCD(un)) { 5744 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 5745 } else { 5746 /* Convert physical geometry to disk geometry */ 5747 bzero(&un_g, sizeof (struct dk_geom)); 5748 sd_convert_geometry(un->un_blockcount, &un_g); 5749 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 5750 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 5751 } 5752 5753 ASSERT(phys_spc != 0); 5754 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 5755 un->un_g.dkg_acyl = DK_ACYL; 5756 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 5757 disksize = un->un_g.dkg_ncyl * phys_spc; 5758 5759 if (ISCD(un)) { 5760 /* 5761 * CD's don't use the "heads * sectors * cyls"-type of 5762 * geometry, but instead use the entire capacity of the media. 5763 */ 5764 disksize = un->un_solaris_size; 5765 un->un_g.dkg_nhead = 1; 5766 un->un_g.dkg_nsect = 1; 5767 un->un_g.dkg_rpm = 5768 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 5769 5770 un->un_vtoc.v_part[0].p_start = 0; 5771 un->un_vtoc.v_part[0].p_size = disksize; 5772 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 5773 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 5774 5775 un->un_map[0].dkl_cylno = 0; 5776 un->un_map[0].dkl_nblk = disksize; 5777 un->un_offset[0] = 0; 5778 5779 } else { 5780 /* 5781 * Hard disks and removable media cartridges 5782 */ 5783 un->un_g.dkg_rpm = 5784 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 5785 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 5786 5787 /* Add boot slice */ 5788 un->un_vtoc.v_part[8].p_start = 0; 5789 un->un_vtoc.v_part[8].p_size = phys_spc; 5790 un->un_vtoc.v_part[8].p_tag = V_BOOT; 5791 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 5792 5793 un->un_map[8].dkl_cylno = 0; 5794 un->un_map[8].dkl_nblk = phys_spc; 5795 un->un_offset[8] = 0; 5796 } 5797 5798 un->un_g.dkg_apc = 0; 5799 un->un_vtoc.v_nparts = V_NUMPAR; 5800 un->un_vtoc.v_version = V_VERSION; 5801 5802 /* Add backup slice */ 5803 un->un_vtoc.v_part[2].p_start = 0; 5804 un->un_vtoc.v_part[2].p_size = disksize; 5805 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 5806 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 5807 5808 un->un_map[2].dkl_cylno = 0; 5809 un->un_map[2].dkl_nblk = disksize; 5810 un->un_offset[2] = 0; 5811 5812 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 5813 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5814 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5815 5816 #else 5817 #error "No VTOC format defined." 5818 #endif 5819 5820 un->un_g.dkg_read_reinstruct = 0; 5821 un->un_g.dkg_write_reinstruct = 0; 5822 5823 un->un_g.dkg_intrlv = 1; 5824 5825 un->un_vtoc.v_sanity = VTOC_SANE; 5826 5827 un->un_f_geometry_is_valid = TRUE; 5828 5829 SD_INFO(SD_LOG_COMMON, un, 5830 "sd_build_default_label: Default label created: " 5831 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 5832 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 5833 un->un_g.dkg_nsect, un->un_blockcount); 5834 } 5835 5836 5837 #if defined(_FIRMWARE_NEEDS_FDISK) 5838 /* 5839 * Max CHS values, as they are encoded into bytes, for 1022/254/63 5840 */ 5841 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 5842 #define LBA_MAX_CYL (1022 & 0xFF) 5843 #define LBA_MAX_HEAD (254) 5844 5845 5846 /* 5847 * Function: sd_has_max_chs_vals 5848 * 5849 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 5850 * 5851 * Arguments: fdp - ptr to CHS info 5852 * 5853 * Return Code: True or false 5854 * 5855 * Context: Any. 5856 */ 5857 5858 static int 5859 sd_has_max_chs_vals(struct ipart *fdp) 5860 { 5861 return ((fdp->begcyl == LBA_MAX_CYL) && 5862 (fdp->beghead == LBA_MAX_HEAD) && 5863 (fdp->begsect == LBA_MAX_SECT) && 5864 (fdp->endcyl == LBA_MAX_CYL) && 5865 (fdp->endhead == LBA_MAX_HEAD) && 5866 (fdp->endsect == LBA_MAX_SECT)); 5867 } 5868 #endif 5869 5870 5871 /* 5872 * Function: sd_inq_fill 5873 * 5874 * Description: Print a piece of inquiry data, cleaned up for non-printable 5875 * characters and stopping at the first space character after 5876 * the beginning of the passed string; 5877 * 5878 * Arguments: p - source string 5879 * l - maximum length to copy 5880 * s - destination string 5881 * 5882 * Context: Any. 5883 */ 5884 5885 static void 5886 sd_inq_fill(char *p, int l, char *s) 5887 { 5888 unsigned i = 0; 5889 char c; 5890 5891 while (i++ < l) { 5892 if ((c = *p++) < ' ' || c >= 0x7F) { 5893 c = '*'; 5894 } else if (i != 1 && c == ' ') { 5895 break; 5896 } 5897 *s++ = c; 5898 } 5899 *s++ = 0; 5900 } 5901 5902 5903 /* 5904 * Function: sd_register_devid 5905 * 5906 * Description: This routine will obtain the device id information from the 5907 * target, obtain the serial number, and register the device 5908 * id with the ddi framework. 5909 * 5910 * Arguments: devi - the system's dev_info_t for the device. 5911 * un - driver soft state (unit) structure 5912 * reservation_flag - indicates if a reservation conflict 5913 * occurred during attach 5914 * 5915 * Context: Kernel Thread 5916 */ 5917 static void 5918 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 5919 { 5920 int rval = 0; 5921 uchar_t *inq80 = NULL; 5922 size_t inq80_len = MAX_INQUIRY_SIZE; 5923 size_t inq80_resid = 0; 5924 uchar_t *inq83 = NULL; 5925 size_t inq83_len = MAX_INQUIRY_SIZE; 5926 size_t inq83_resid = 0; 5927 5928 ASSERT(un != NULL); 5929 ASSERT(mutex_owned(SD_MUTEX(un))); 5930 ASSERT((SD_DEVINFO(un)) == devi); 5931 5932 /* 5933 * This is the case of antiquated Sun disk drives that have the 5934 * FAB_DEVID property set in the disk_table. These drives 5935 * manage the devid's by storing them in last 2 available sectors 5936 * on the drive and have them fabricated by the ddi layer by calling 5937 * ddi_devid_init and passing the DEVID_FAB flag. 5938 */ 5939 if (un->un_f_opt_fab_devid == TRUE) { 5940 /* 5941 * Depending on EINVAL isn't reliable, since a reserved disk 5942 * may result in invalid geometry, so check to make sure a 5943 * reservation conflict did not occur during attach. 5944 */ 5945 if ((sd_get_devid(un) == EINVAL) && 5946 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5947 /* 5948 * The devid is invalid AND there is no reservation 5949 * conflict. Fabricate a new devid. 5950 */ 5951 (void) sd_create_devid(un); 5952 } 5953 5954 /* Register the devid if it exists */ 5955 if (un->un_devid != NULL) { 5956 (void) ddi_devid_register(SD_DEVINFO(un), 5957 un->un_devid); 5958 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5959 "sd_register_devid: Devid Fabricated\n"); 5960 } 5961 return; 5962 } 5963 5964 /* 5965 * We check the availibility of the World Wide Name (0x83) and Unit 5966 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5967 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5968 * 0x83 is availible, that is the best choice. Our next choice is 5969 * 0x80. If neither are availible, we munge the devid from the device 5970 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5971 * to fabricate a devid for non-Sun qualified disks. 5972 */ 5973 if (sd_check_vpd_page_support(un) == 0) { 5974 /* collect page 80 data if available */ 5975 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5976 5977 mutex_exit(SD_MUTEX(un)); 5978 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5979 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 5980 0x01, 0x80, &inq80_resid); 5981 5982 if (rval != 0) { 5983 kmem_free(inq80, inq80_len); 5984 inq80 = NULL; 5985 inq80_len = 0; 5986 } 5987 mutex_enter(SD_MUTEX(un)); 5988 } 5989 5990 /* collect page 83 data if available */ 5991 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5992 5993 mutex_exit(SD_MUTEX(un)); 5994 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5995 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 5996 0x01, 0x83, &inq83_resid); 5997 5998 if (rval != 0) { 5999 kmem_free(inq83, inq83_len); 6000 inq83 = NULL; 6001 inq83_len = 0; 6002 } 6003 mutex_enter(SD_MUTEX(un)); 6004 } 6005 } 6006 6007 /* encode best devid possible based on data available */ 6008 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6009 (char *)ddi_driver_name(SD_DEVINFO(un)), 6010 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6011 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6012 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6013 6014 /* devid successfully encoded, register devid */ 6015 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6016 6017 } else { 6018 /* 6019 * Unable to encode a devid based on data available. 6020 * This is not a Sun qualified disk. Older Sun disk 6021 * drives that have the SD_FAB_DEVID property 6022 * set in the disk_table and non Sun qualified 6023 * disks are treated in the same manner. These 6024 * drives manage the devid's by storing them in 6025 * last 2 available sectors on the drive and 6026 * have them fabricated by the ddi layer by 6027 * calling ddi_devid_init and passing the 6028 * DEVID_FAB flag. 6029 * Create a fabricate devid only if there's no 6030 * fabricate devid existed. 6031 */ 6032 if (sd_get_devid(un) == EINVAL) { 6033 (void) sd_create_devid(un); 6034 un->un_f_opt_fab_devid = TRUE; 6035 } 6036 6037 /* Register the devid if it exists */ 6038 if (un->un_devid != NULL) { 6039 (void) ddi_devid_register(SD_DEVINFO(un), 6040 un->un_devid); 6041 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6042 "sd_register_devid: devid fabricated using " 6043 "ddi framework\n"); 6044 } 6045 } 6046 6047 /* clean up resources */ 6048 if (inq80 != NULL) { 6049 kmem_free(inq80, inq80_len); 6050 } 6051 if (inq83 != NULL) { 6052 kmem_free(inq83, inq83_len); 6053 } 6054 } 6055 6056 static daddr_t 6057 sd_get_devid_block(struct sd_lun *un) 6058 { 6059 daddr_t spc, blk, head, cyl; 6060 6061 if (un->un_blockcount <= DK_MAX_BLOCKS) { 6062 /* this geometry doesn't allow us to write a devid */ 6063 if (un->un_g.dkg_acyl < 2) { 6064 return (-1); 6065 } 6066 6067 /* 6068 * Subtract 2 guarantees that the next to last cylinder 6069 * is used 6070 */ 6071 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6072 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6073 head = un->un_g.dkg_nhead - 1; 6074 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6075 (head * un->un_g.dkg_nsect) + 1; 6076 } else { 6077 if (un->un_reserved != -1) { 6078 blk = un->un_map[un->un_reserved].dkl_cylno + 1; 6079 } else { 6080 return (-1); 6081 } 6082 } 6083 return (blk); 6084 } 6085 6086 /* 6087 * Function: sd_get_devid 6088 * 6089 * Description: This routine will return 0 if a valid device id has been 6090 * obtained from the target and stored in the soft state. If a 6091 * valid device id has not been previously read and stored, a 6092 * read attempt will be made. 6093 * 6094 * Arguments: un - driver soft state (unit) structure 6095 * 6096 * Return Code: 0 if we successfully get the device id 6097 * 6098 * Context: Kernel Thread 6099 */ 6100 6101 static int 6102 sd_get_devid(struct sd_lun *un) 6103 { 6104 struct dk_devid *dkdevid; 6105 ddi_devid_t tmpid; 6106 uint_t *ip; 6107 size_t sz; 6108 daddr_t blk; 6109 int status; 6110 int chksum; 6111 int i; 6112 size_t buffer_size; 6113 6114 ASSERT(un != NULL); 6115 ASSERT(mutex_owned(SD_MUTEX(un))); 6116 6117 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6118 un); 6119 6120 if (un->un_devid != NULL) { 6121 return (0); 6122 } 6123 6124 blk = sd_get_devid_block(un); 6125 if (blk < 0) 6126 return (EINVAL); 6127 6128 /* 6129 * Read and verify device id, stored in the reserved cylinders at the 6130 * end of the disk. Backup label is on the odd sectors of the last 6131 * track of the last cylinder. Device id will be on track of the next 6132 * to last cylinder. 6133 */ 6134 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6135 mutex_exit(SD_MUTEX(un)); 6136 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6137 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6138 SD_PATH_DIRECT); 6139 if (status != 0) { 6140 goto error; 6141 } 6142 6143 /* Validate the revision */ 6144 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6145 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6146 status = EINVAL; 6147 goto error; 6148 } 6149 6150 /* Calculate the checksum */ 6151 chksum = 0; 6152 ip = (uint_t *)dkdevid; 6153 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6154 i++) { 6155 chksum ^= ip[i]; 6156 } 6157 6158 /* Compare the checksums */ 6159 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6160 status = EINVAL; 6161 goto error; 6162 } 6163 6164 /* Validate the device id */ 6165 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6166 status = EINVAL; 6167 goto error; 6168 } 6169 6170 /* 6171 * Store the device id in the driver soft state 6172 */ 6173 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6174 tmpid = kmem_alloc(sz, KM_SLEEP); 6175 6176 mutex_enter(SD_MUTEX(un)); 6177 6178 un->un_devid = tmpid; 6179 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6180 6181 kmem_free(dkdevid, buffer_size); 6182 6183 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6184 6185 return (status); 6186 error: 6187 mutex_enter(SD_MUTEX(un)); 6188 kmem_free(dkdevid, buffer_size); 6189 return (status); 6190 } 6191 6192 6193 /* 6194 * Function: sd_create_devid 6195 * 6196 * Description: This routine will fabricate the device id and write it 6197 * to the disk. 6198 * 6199 * Arguments: un - driver soft state (unit) structure 6200 * 6201 * Return Code: value of the fabricated device id 6202 * 6203 * Context: Kernel Thread 6204 */ 6205 6206 static ddi_devid_t 6207 sd_create_devid(struct sd_lun *un) 6208 { 6209 ASSERT(un != NULL); 6210 6211 /* Fabricate the devid */ 6212 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6213 == DDI_FAILURE) { 6214 return (NULL); 6215 } 6216 6217 /* Write the devid to disk */ 6218 if (sd_write_deviceid(un) != 0) { 6219 ddi_devid_free(un->un_devid); 6220 un->un_devid = NULL; 6221 } 6222 6223 return (un->un_devid); 6224 } 6225 6226 6227 /* 6228 * Function: sd_write_deviceid 6229 * 6230 * Description: This routine will write the device id to the disk 6231 * reserved sector. 6232 * 6233 * Arguments: un - driver soft state (unit) structure 6234 * 6235 * Return Code: EINVAL 6236 * value returned by sd_send_scsi_cmd 6237 * 6238 * Context: Kernel Thread 6239 */ 6240 6241 static int 6242 sd_write_deviceid(struct sd_lun *un) 6243 { 6244 struct dk_devid *dkdevid; 6245 daddr_t blk; 6246 uint_t *ip, chksum; 6247 int status; 6248 int i; 6249 6250 ASSERT(mutex_owned(SD_MUTEX(un))); 6251 6252 blk = sd_get_devid_block(un); 6253 if (blk < 0) 6254 return (-1); 6255 mutex_exit(SD_MUTEX(un)); 6256 6257 /* Allocate the buffer */ 6258 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6259 6260 /* Fill in the revision */ 6261 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6262 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6263 6264 /* Copy in the device id */ 6265 mutex_enter(SD_MUTEX(un)); 6266 bcopy(un->un_devid, &dkdevid->dkd_devid, 6267 ddi_devid_sizeof(un->un_devid)); 6268 mutex_exit(SD_MUTEX(un)); 6269 6270 /* Calculate the checksum */ 6271 chksum = 0; 6272 ip = (uint_t *)dkdevid; 6273 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6274 i++) { 6275 chksum ^= ip[i]; 6276 } 6277 6278 /* Fill-in checksum */ 6279 DKD_FORMCHKSUM(chksum, dkdevid); 6280 6281 /* Write the reserved sector */ 6282 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6283 SD_PATH_DIRECT); 6284 6285 kmem_free(dkdevid, un->un_sys_blocksize); 6286 6287 mutex_enter(SD_MUTEX(un)); 6288 return (status); 6289 } 6290 6291 6292 /* 6293 * Function: sd_check_vpd_page_support 6294 * 6295 * Description: This routine sends an inquiry command with the EVPD bit set and 6296 * a page code of 0x00 to the device. It is used to determine which 6297 * vital product pages are availible to find the devid. We are 6298 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6299 * device does not support that command. 6300 * 6301 * Arguments: un - driver soft state (unit) structure 6302 * 6303 * Return Code: 0 - success 6304 * 1 - check condition 6305 * 6306 * Context: This routine can sleep. 6307 */ 6308 6309 static int 6310 sd_check_vpd_page_support(struct sd_lun *un) 6311 { 6312 uchar_t *page_list = NULL; 6313 uchar_t page_length = 0xff; /* Use max possible length */ 6314 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6315 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6316 int rval = 0; 6317 int counter; 6318 6319 ASSERT(un != NULL); 6320 ASSERT(mutex_owned(SD_MUTEX(un))); 6321 6322 mutex_exit(SD_MUTEX(un)); 6323 6324 /* 6325 * We'll set the page length to the maximum to save figuring it out 6326 * with an additional call. 6327 */ 6328 page_list = kmem_zalloc(page_length, KM_SLEEP); 6329 6330 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6331 page_code, NULL); 6332 6333 mutex_enter(SD_MUTEX(un)); 6334 6335 /* 6336 * Now we must validate that the device accepted the command, as some 6337 * drives do not support it. If the drive does support it, we will 6338 * return 0, and the supported pages will be in un_vpd_page_mask. If 6339 * not, we return -1. 6340 */ 6341 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6342 /* Loop to find one of the 2 pages we need */ 6343 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6344 6345 /* 6346 * Pages are returned in ascending order, and 0x83 is what we 6347 * are hoping for. 6348 */ 6349 while ((page_list[counter] <= 0x83) && 6350 (counter <= (page_list[VPD_PAGE_LENGTH] + 6351 VPD_HEAD_OFFSET))) { 6352 /* 6353 * Add 3 because page_list[3] is the number of 6354 * pages minus 3 6355 */ 6356 6357 switch (page_list[counter]) { 6358 case 0x00: 6359 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6360 break; 6361 case 0x80: 6362 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6363 break; 6364 case 0x81: 6365 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6366 break; 6367 case 0x82: 6368 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6369 break; 6370 case 0x83: 6371 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6372 break; 6373 } 6374 counter++; 6375 } 6376 6377 } else { 6378 rval = -1; 6379 6380 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6381 "sd_check_vpd_page_support: This drive does not implement " 6382 "VPD pages.\n"); 6383 } 6384 6385 kmem_free(page_list, page_length); 6386 6387 return (rval); 6388 } 6389 6390 6391 /* 6392 * Function: sd_setup_pm 6393 * 6394 * Description: Initialize Power Management on the device 6395 * 6396 * Context: Kernel Thread 6397 */ 6398 6399 static void 6400 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6401 { 6402 uint_t log_page_size; 6403 uchar_t *log_page_data; 6404 int rval; 6405 6406 /* 6407 * Since we are called from attach, holding a mutex for 6408 * un is unnecessary. Because some of the routines called 6409 * from here require SD_MUTEX to not be held, assert this 6410 * right up front. 6411 */ 6412 ASSERT(!mutex_owned(SD_MUTEX(un))); 6413 /* 6414 * Since the sd device does not have the 'reg' property, 6415 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6416 * The following code is to tell cpr that this device 6417 * DOES need to be suspended and resumed. 6418 */ 6419 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6420 "pm-hardware-state", "needs-suspend-resume"); 6421 6422 /* 6423 * Check if HBA has set the "pm-capable" property. 6424 * If "pm-capable" exists and is non-zero then we can 6425 * power manage the device without checking the start/stop 6426 * cycle count log sense page. 6427 * 6428 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 6429 * then we should not power manage the device. 6430 * 6431 * If "pm-capable" doesn't exist then un->un_pm_capable_prop will 6432 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, sd will 6433 * check the start/stop cycle count log sense page and power manage 6434 * the device if the cycle count limit has not been exceeded. 6435 */ 6436 un->un_pm_capable_prop = 6437 ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6438 "pm-capable", SD_PM_CAPABLE_UNDEFINED); 6439 if (un->un_pm_capable_prop != SD_PM_CAPABLE_UNDEFINED) { 6440 /* 6441 * pm-capable property exists. 6442 * 6443 * Convert "TRUE" values for un_pm_capable_prop to 6444 * SD_PM_CAPABLE_TRUE (1) to make it easier to check later. 6445 * "TRUE" values are any values except SD_PM_CAPABLE_FALSE (0) 6446 * and SD_PM_CAPABLE_UNDEFINED (-1) 6447 */ 6448 if (un->un_pm_capable_prop != SD_PM_CAPABLE_FALSE) { 6449 un->un_pm_capable_prop = SD_PM_CAPABLE_TRUE; 6450 } 6451 6452 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6453 "sd_unit_attach: un:0x%p pm-capable " 6454 "property set to %d.\n", un, un->un_pm_capable_prop); 6455 } 6456 6457 /* 6458 * This complies with the new power management framework 6459 * for certain desktop machines. Create the pm_components 6460 * property as a string array property. 6461 * 6462 * If this is a removable device or if the pm-capable property 6463 * is SD_PM_CAPABLE_TRUE (1) then we should create the 6464 * pm_components property without checking for the existance of 6465 * the start-stop cycle counter log page 6466 */ 6467 if (ISREMOVABLE(un) || 6468 un->un_pm_capable_prop == SD_PM_CAPABLE_TRUE) { 6469 /* 6470 * not all devices have a motor, try it first. 6471 * some devices may return ILLEGAL REQUEST, some 6472 * will hang 6473 */ 6474 un->un_f_start_stop_supported = TRUE; 6475 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6476 SD_PATH_DIRECT) != 0) { 6477 un->un_f_start_stop_supported = FALSE; 6478 } 6479 6480 /* 6481 * create pm properties anyways otherwise the parent can't 6482 * go to sleep 6483 */ 6484 (void) sd_create_pm_components(devi, un); 6485 un->un_f_pm_is_enabled = TRUE; 6486 6487 /* 6488 * Need to create a zero length (Boolean) property 6489 * removable-media for the removable media devices. 6490 * Note that the return value of the property is not being 6491 * checked, since if unable to create the property 6492 * then do not want the attach to fail altogether. Consistent 6493 * with other property creation in attach. 6494 */ 6495 if (ISREMOVABLE(un)) { 6496 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 6497 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 6498 } 6499 return; 6500 } 6501 6502 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6503 6504 #ifdef SDDEBUG 6505 if (sd_force_pm_supported) { 6506 /* Force a successful result */ 6507 rval = 1; 6508 } 6509 #endif 6510 6511 /* 6512 * If the start-stop cycle counter log page is not supported 6513 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6514 * then we should not create the pm_components property. 6515 */ 6516 if (rval == -1 || un->un_pm_capable_prop == SD_PM_CAPABLE_FALSE) { 6517 /* 6518 * Error. 6519 * Reading log sense failed, most likely this is 6520 * an older drive that does not support log sense. 6521 * If this fails auto-pm is not supported. 6522 */ 6523 un->un_power_level = SD_SPINDLE_ON; 6524 un->un_f_pm_is_enabled = FALSE; 6525 6526 } else if (rval == 0) { 6527 /* 6528 * Page not found. 6529 * The start stop cycle counter is implemented as page 6530 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6531 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6532 */ 6533 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6534 /* 6535 * Page found, use this one. 6536 */ 6537 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6538 un->un_f_pm_is_enabled = TRUE; 6539 } else { 6540 /* 6541 * Error or page not found. 6542 * auto-pm is not supported for this device. 6543 */ 6544 un->un_power_level = SD_SPINDLE_ON; 6545 un->un_f_pm_is_enabled = FALSE; 6546 } 6547 } else { 6548 /* 6549 * Page found, use it. 6550 */ 6551 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6552 un->un_f_pm_is_enabled = TRUE; 6553 } 6554 6555 6556 if (un->un_f_pm_is_enabled == TRUE) { 6557 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6558 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6559 6560 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6561 log_page_size, un->un_start_stop_cycle_page, 6562 0x01, 0, SD_PATH_DIRECT); 6563 #ifdef SDDEBUG 6564 if (sd_force_pm_supported) { 6565 /* Force a successful result */ 6566 rval = 0; 6567 } 6568 #endif 6569 6570 /* 6571 * If the Log sense for Page( Start/stop cycle counter page) 6572 * succeeds, then power managment is supported and we can 6573 * enable auto-pm. 6574 */ 6575 if (rval == 0) { 6576 (void) sd_create_pm_components(devi, un); 6577 } else { 6578 un->un_power_level = SD_SPINDLE_ON; 6579 un->un_f_pm_is_enabled = FALSE; 6580 } 6581 6582 kmem_free(log_page_data, log_page_size); 6583 } 6584 } 6585 6586 6587 /* 6588 * Function: sd_create_pm_components 6589 * 6590 * Description: Initialize PM property. 6591 * 6592 * Context: Kernel thread context 6593 */ 6594 6595 static void 6596 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6597 { 6598 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6599 6600 ASSERT(!mutex_owned(SD_MUTEX(un))); 6601 6602 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6603 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6604 /* 6605 * When components are initially created they are idle, 6606 * power up any non-removables. 6607 * Note: the return value of pm_raise_power can't be used 6608 * for determining if PM should be enabled for this device. 6609 * Even if you check the return values and remove this 6610 * property created above, the PM framework will not honor the 6611 * change after the first call to pm_raise_power. Hence, 6612 * removal of that property does not help if pm_raise_power 6613 * fails. In the case of removable media, the start/stop 6614 * will fail if the media is not present. 6615 */ 6616 if ((!ISREMOVABLE(un)) && (pm_raise_power(SD_DEVINFO(un), 0, 6617 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6618 mutex_enter(SD_MUTEX(un)); 6619 un->un_power_level = SD_SPINDLE_ON; 6620 mutex_enter(&un->un_pm_mutex); 6621 /* Set to on and not busy. */ 6622 un->un_pm_count = 0; 6623 } else { 6624 mutex_enter(SD_MUTEX(un)); 6625 un->un_power_level = SD_SPINDLE_OFF; 6626 mutex_enter(&un->un_pm_mutex); 6627 /* Set to off. */ 6628 un->un_pm_count = -1; 6629 } 6630 mutex_exit(&un->un_pm_mutex); 6631 mutex_exit(SD_MUTEX(un)); 6632 } else { 6633 un->un_power_level = SD_SPINDLE_ON; 6634 un->un_f_pm_is_enabled = FALSE; 6635 } 6636 } 6637 6638 6639 /* 6640 * Function: sd_ddi_suspend 6641 * 6642 * Description: Performs system power-down operations. This includes 6643 * setting the drive state to indicate its suspended so 6644 * that no new commands will be accepted. Also, wait for 6645 * all commands that are in transport or queued to a timer 6646 * for retry to complete. All timeout threads are cancelled. 6647 * 6648 * Return Code: DDI_FAILURE or DDI_SUCCESS 6649 * 6650 * Context: Kernel thread context 6651 */ 6652 6653 static int 6654 sd_ddi_suspend(dev_info_t *devi) 6655 { 6656 struct sd_lun *un; 6657 clock_t wait_cmds_complete; 6658 6659 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6660 if (un == NULL) { 6661 return (DDI_FAILURE); 6662 } 6663 6664 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6665 6666 mutex_enter(SD_MUTEX(un)); 6667 6668 /* Return success if the device is already suspended. */ 6669 if (un->un_state == SD_STATE_SUSPENDED) { 6670 mutex_exit(SD_MUTEX(un)); 6671 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6672 "device already suspended, exiting\n"); 6673 return (DDI_SUCCESS); 6674 } 6675 6676 /* Return failure if the device is being used by HA */ 6677 if (un->un_resvd_status & 6678 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6679 mutex_exit(SD_MUTEX(un)); 6680 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6681 "device in use by HA, exiting\n"); 6682 return (DDI_FAILURE); 6683 } 6684 6685 /* 6686 * Return failure if the device is in a resource wait 6687 * or power changing state. 6688 */ 6689 if ((un->un_state == SD_STATE_RWAIT) || 6690 (un->un_state == SD_STATE_PM_CHANGING)) { 6691 mutex_exit(SD_MUTEX(un)); 6692 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6693 "device in resource wait state, exiting\n"); 6694 return (DDI_FAILURE); 6695 } 6696 6697 6698 un->un_save_state = un->un_last_state; 6699 New_state(un, SD_STATE_SUSPENDED); 6700 6701 /* 6702 * Wait for all commands that are in transport or queued to a timer 6703 * for retry to complete. 6704 * 6705 * While waiting, no new commands will be accepted or sent because of 6706 * the new state we set above. 6707 * 6708 * Wait till current operation has completed. If we are in the resource 6709 * wait state (with an intr outstanding) then we need to wait till the 6710 * intr completes and starts the next cmd. We want to wait for 6711 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6712 */ 6713 wait_cmds_complete = ddi_get_lbolt() + 6714 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6715 6716 while (un->un_ncmds_in_transport != 0) { 6717 /* 6718 * Fail if commands do not finish in the specified time. 6719 */ 6720 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6721 wait_cmds_complete) == -1) { 6722 /* 6723 * Undo the state changes made above. Everything 6724 * must go back to it's original value. 6725 */ 6726 Restore_state(un); 6727 un->un_last_state = un->un_save_state; 6728 /* Wake up any threads that might be waiting. */ 6729 cv_broadcast(&un->un_suspend_cv); 6730 mutex_exit(SD_MUTEX(un)); 6731 SD_ERROR(SD_LOG_IO_PM, un, 6732 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6733 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6734 return (DDI_FAILURE); 6735 } 6736 } 6737 6738 /* 6739 * Cancel SCSI watch thread and timeouts, if any are active 6740 */ 6741 6742 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6743 opaque_t temp_token = un->un_swr_token; 6744 mutex_exit(SD_MUTEX(un)); 6745 scsi_watch_suspend(temp_token); 6746 mutex_enter(SD_MUTEX(un)); 6747 } 6748 6749 if (un->un_reset_throttle_timeid != NULL) { 6750 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6751 un->un_reset_throttle_timeid = NULL; 6752 mutex_exit(SD_MUTEX(un)); 6753 (void) untimeout(temp_id); 6754 mutex_enter(SD_MUTEX(un)); 6755 } 6756 6757 if (un->un_dcvb_timeid != NULL) { 6758 timeout_id_t temp_id = un->un_dcvb_timeid; 6759 un->un_dcvb_timeid = NULL; 6760 mutex_exit(SD_MUTEX(un)); 6761 (void) untimeout(temp_id); 6762 mutex_enter(SD_MUTEX(un)); 6763 } 6764 6765 mutex_enter(&un->un_pm_mutex); 6766 if (un->un_pm_timeid != NULL) { 6767 timeout_id_t temp_id = un->un_pm_timeid; 6768 un->un_pm_timeid = NULL; 6769 mutex_exit(&un->un_pm_mutex); 6770 mutex_exit(SD_MUTEX(un)); 6771 (void) untimeout(temp_id); 6772 mutex_enter(SD_MUTEX(un)); 6773 } else { 6774 mutex_exit(&un->un_pm_mutex); 6775 } 6776 6777 if (un->un_retry_timeid != NULL) { 6778 timeout_id_t temp_id = un->un_retry_timeid; 6779 un->un_retry_timeid = NULL; 6780 mutex_exit(SD_MUTEX(un)); 6781 (void) untimeout(temp_id); 6782 mutex_enter(SD_MUTEX(un)); 6783 } 6784 6785 if (un->un_direct_priority_timeid != NULL) { 6786 timeout_id_t temp_id = un->un_direct_priority_timeid; 6787 un->un_direct_priority_timeid = NULL; 6788 mutex_exit(SD_MUTEX(un)); 6789 (void) untimeout(temp_id); 6790 mutex_enter(SD_MUTEX(un)); 6791 } 6792 6793 if (un->un_f_is_fibre == TRUE) { 6794 /* 6795 * Remove callbacks for insert and remove events 6796 */ 6797 if (un->un_insert_event != NULL) { 6798 mutex_exit(SD_MUTEX(un)); 6799 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6800 mutex_enter(SD_MUTEX(un)); 6801 un->un_insert_event = NULL; 6802 } 6803 6804 if (un->un_remove_event != NULL) { 6805 mutex_exit(SD_MUTEX(un)); 6806 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6807 mutex_enter(SD_MUTEX(un)); 6808 un->un_remove_event = NULL; 6809 } 6810 } 6811 6812 mutex_exit(SD_MUTEX(un)); 6813 6814 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6815 6816 return (DDI_SUCCESS); 6817 } 6818 6819 6820 /* 6821 * Function: sd_ddi_pm_suspend 6822 * 6823 * Description: Set the drive state to low power. 6824 * Someone else is required to actually change the drive 6825 * power level. 6826 * 6827 * Arguments: un - driver soft state (unit) structure 6828 * 6829 * Return Code: DDI_FAILURE or DDI_SUCCESS 6830 * 6831 * Context: Kernel thread context 6832 */ 6833 6834 static int 6835 sd_ddi_pm_suspend(struct sd_lun *un) 6836 { 6837 ASSERT(un != NULL); 6838 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 6839 6840 ASSERT(!mutex_owned(SD_MUTEX(un))); 6841 mutex_enter(SD_MUTEX(un)); 6842 6843 /* 6844 * Exit if power management is not enabled for this device, or if 6845 * the device is being used by HA. 6846 */ 6847 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6848 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6849 mutex_exit(SD_MUTEX(un)); 6850 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 6851 return (DDI_SUCCESS); 6852 } 6853 6854 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 6855 un->un_ncmds_in_driver); 6856 6857 /* 6858 * See if the device is not busy, ie.: 6859 * - we have no commands in the driver for this device 6860 * - not waiting for resources 6861 */ 6862 if ((un->un_ncmds_in_driver == 0) && 6863 (un->un_state != SD_STATE_RWAIT)) { 6864 /* 6865 * The device is not busy, so it is OK to go to low power state. 6866 * Indicate low power, but rely on someone else to actually 6867 * change it. 6868 */ 6869 mutex_enter(&un->un_pm_mutex); 6870 un->un_pm_count = -1; 6871 mutex_exit(&un->un_pm_mutex); 6872 un->un_power_level = SD_SPINDLE_OFF; 6873 } 6874 6875 mutex_exit(SD_MUTEX(un)); 6876 6877 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 6878 6879 return (DDI_SUCCESS); 6880 } 6881 6882 6883 /* 6884 * Function: sd_ddi_resume 6885 * 6886 * Description: Performs system power-up operations.. 6887 * 6888 * Return Code: DDI_SUCCESS 6889 * DDI_FAILURE 6890 * 6891 * Context: Kernel thread context 6892 */ 6893 6894 static int 6895 sd_ddi_resume(dev_info_t *devi) 6896 { 6897 struct sd_lun *un; 6898 6899 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6900 if (un == NULL) { 6901 return (DDI_FAILURE); 6902 } 6903 6904 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6905 6906 mutex_enter(SD_MUTEX(un)); 6907 Restore_state(un); 6908 6909 /* 6910 * Restore the state which was saved to give the 6911 * the right state in un_last_state 6912 */ 6913 un->un_last_state = un->un_save_state; 6914 /* 6915 * Note: throttle comes back at full. 6916 * Also note: this MUST be done before calling pm_raise_power 6917 * otherwise the system can get hung in biowait. The scenario where 6918 * this'll happen is under cpr suspend. Writing of the system 6919 * state goes through sddump, which writes 0 to un_throttle. If 6920 * writing the system state then fails, example if the partition is 6921 * too small, then cpr attempts a resume. If throttle isn't restored 6922 * from the saved value until after calling pm_raise_power then 6923 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6924 * in biowait. 6925 */ 6926 un->un_throttle = un->un_saved_throttle; 6927 6928 /* 6929 * The chance of failure is very rare as the only command done in power 6930 * entry point is START command when you transition from 0->1 or 6931 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6932 * which suspend was done. Ignore the return value as the resume should 6933 * not be failed. In the case of removable media the media need not be 6934 * inserted and hence there is a chance that raise power will fail with 6935 * media not present. 6936 */ 6937 if (!ISREMOVABLE(un)) { 6938 mutex_exit(SD_MUTEX(un)); 6939 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 6940 mutex_enter(SD_MUTEX(un)); 6941 } 6942 6943 /* 6944 * Don't broadcast to the suspend cv and therefore possibly 6945 * start I/O until after power has been restored. 6946 */ 6947 cv_broadcast(&un->un_suspend_cv); 6948 cv_broadcast(&un->un_state_cv); 6949 6950 /* restart thread */ 6951 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6952 scsi_watch_resume(un->un_swr_token); 6953 } 6954 6955 #if (defined(__fibre)) 6956 if (un->un_f_is_fibre == TRUE) { 6957 /* 6958 * Add callbacks for insert and remove events 6959 */ 6960 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6961 sd_init_event_callbacks(un); 6962 } 6963 } 6964 #endif 6965 6966 /* 6967 * Transport any pending commands to the target. 6968 * 6969 * If this is a low-activity device commands in queue will have to wait 6970 * until new commands come in, which may take awhile. Also, we 6971 * specifically don't check un_ncmds_in_transport because we know that 6972 * there really are no commands in progress after the unit was 6973 * suspended and we could have reached the throttle level, been 6974 * suspended, and have no new commands coming in for awhile. Highly 6975 * unlikely, but so is the low-activity disk scenario. 6976 */ 6977 ddi_xbuf_dispatch(un->un_xbuf_attr); 6978 6979 sd_start_cmds(un, NULL); 6980 mutex_exit(SD_MUTEX(un)); 6981 6982 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6983 6984 return (DDI_SUCCESS); 6985 } 6986 6987 6988 /* 6989 * Function: sd_ddi_pm_resume 6990 * 6991 * Description: Set the drive state to powered on. 6992 * Someone else is required to actually change the drive 6993 * power level. 6994 * 6995 * Arguments: un - driver soft state (unit) structure 6996 * 6997 * Return Code: DDI_SUCCESS 6998 * 6999 * Context: Kernel thread context 7000 */ 7001 7002 static int 7003 sd_ddi_pm_resume(struct sd_lun *un) 7004 { 7005 ASSERT(un != NULL); 7006 7007 ASSERT(!mutex_owned(SD_MUTEX(un))); 7008 mutex_enter(SD_MUTEX(un)); 7009 un->un_power_level = SD_SPINDLE_ON; 7010 7011 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7012 mutex_enter(&un->un_pm_mutex); 7013 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7014 un->un_pm_count++; 7015 ASSERT(un->un_pm_count == 0); 7016 /* 7017 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7018 * un_suspend_cv is for a system resume, not a power management 7019 * device resume. (4297749) 7020 * cv_broadcast(&un->un_suspend_cv); 7021 */ 7022 } 7023 mutex_exit(&un->un_pm_mutex); 7024 mutex_exit(SD_MUTEX(un)); 7025 7026 return (DDI_SUCCESS); 7027 } 7028 7029 7030 /* 7031 * Function: sd_pm_idletimeout_handler 7032 * 7033 * Description: A timer routine that's active only while a device is busy. 7034 * The purpose is to extend slightly the pm framework's busy 7035 * view of the device to prevent busy/idle thrashing for 7036 * back-to-back commands. Do this by comparing the current time 7037 * to the time at which the last command completed and when the 7038 * difference is greater than sd_pm_idletime, call 7039 * pm_idle_component. In addition to indicating idle to the pm 7040 * framework, update the chain type to again use the internal pm 7041 * layers of the driver. 7042 * 7043 * Arguments: arg - driver soft state (unit) structure 7044 * 7045 * Context: Executes in a timeout(9F) thread context 7046 */ 7047 7048 static void 7049 sd_pm_idletimeout_handler(void *arg) 7050 { 7051 struct sd_lun *un = arg; 7052 7053 time_t now; 7054 7055 mutex_enter(&sd_detach_mutex); 7056 if (un->un_detach_count != 0) { 7057 /* Abort if the instance is detaching */ 7058 mutex_exit(&sd_detach_mutex); 7059 return; 7060 } 7061 mutex_exit(&sd_detach_mutex); 7062 7063 now = ddi_get_time(); 7064 /* 7065 * Grab both mutexes, in the proper order, since we're accessing 7066 * both PM and softstate variables. 7067 */ 7068 mutex_enter(SD_MUTEX(un)); 7069 mutex_enter(&un->un_pm_mutex); 7070 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7071 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7072 /* 7073 * Update the chain types. 7074 * This takes affect on the next new command received. 7075 */ 7076 if (ISREMOVABLE(un)) { 7077 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7078 } else { 7079 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7080 } 7081 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7082 7083 SD_TRACE(SD_LOG_IO_PM, un, 7084 "sd_pm_idletimeout_handler: idling device\n"); 7085 (void) pm_idle_component(SD_DEVINFO(un), 0); 7086 un->un_pm_idle_timeid = NULL; 7087 } else { 7088 un->un_pm_idle_timeid = 7089 timeout(sd_pm_idletimeout_handler, un, 7090 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7091 } 7092 mutex_exit(&un->un_pm_mutex); 7093 mutex_exit(SD_MUTEX(un)); 7094 } 7095 7096 7097 /* 7098 * Function: sd_pm_timeout_handler 7099 * 7100 * Description: Callback to tell framework we are idle. 7101 * 7102 * Context: timeout(9f) thread context. 7103 */ 7104 7105 static void 7106 sd_pm_timeout_handler(void *arg) 7107 { 7108 struct sd_lun *un = arg; 7109 7110 (void) pm_idle_component(SD_DEVINFO(un), 0); 7111 mutex_enter(&un->un_pm_mutex); 7112 un->un_pm_timeid = NULL; 7113 mutex_exit(&un->un_pm_mutex); 7114 } 7115 7116 7117 /* 7118 * Function: sdpower 7119 * 7120 * Description: PM entry point. 7121 * 7122 * Return Code: DDI_SUCCESS 7123 * DDI_FAILURE 7124 * 7125 * Context: Kernel thread context 7126 */ 7127 7128 static int 7129 sdpower(dev_info_t *devi, int component, int level) 7130 { 7131 struct sd_lun *un; 7132 int instance; 7133 int rval = DDI_SUCCESS; 7134 uint_t i, log_page_size, maxcycles, ncycles; 7135 uchar_t *log_page_data; 7136 int log_sense_page; 7137 int medium_present; 7138 time_t intvlp; 7139 dev_t dev; 7140 struct pm_trans_data sd_pm_tran_data; 7141 uchar_t save_state; 7142 int sval; 7143 uchar_t state_before_pm; 7144 int got_semaphore_here; 7145 7146 instance = ddi_get_instance(devi); 7147 7148 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7149 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7150 component != 0) { 7151 return (DDI_FAILURE); 7152 } 7153 7154 dev = sd_make_device(SD_DEVINFO(un)); 7155 7156 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7157 7158 /* 7159 * Must synchronize power down with close. 7160 * Attempt to decrement/acquire the open/close semaphore, 7161 * but do NOT wait on it. If it's not greater than zero, 7162 * ie. it can't be decremented without waiting, then 7163 * someone else, either open or close, already has it 7164 * and the try returns 0. Use that knowledge here to determine 7165 * if it's OK to change the device power level. 7166 * Also, only increment it on exit if it was decremented, ie. gotten, 7167 * here. 7168 */ 7169 got_semaphore_here = sema_tryp(&un->un_semoclose); 7170 7171 mutex_enter(SD_MUTEX(un)); 7172 7173 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7174 un->un_ncmds_in_driver); 7175 7176 /* 7177 * If un_ncmds_in_driver is non-zero it indicates commands are 7178 * already being processed in the driver, or if the semaphore was 7179 * not gotten here it indicates an open or close is being processed. 7180 * At the same time somebody is requesting to go low power which 7181 * can't happen, therefore we need to return failure. 7182 */ 7183 if ((level == SD_SPINDLE_OFF) && 7184 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7185 mutex_exit(SD_MUTEX(un)); 7186 7187 if (got_semaphore_here != 0) { 7188 sema_v(&un->un_semoclose); 7189 } 7190 SD_TRACE(SD_LOG_IO_PM, un, 7191 "sdpower: exit, device has queued cmds.\n"); 7192 return (DDI_FAILURE); 7193 } 7194 7195 /* 7196 * if it is OFFLINE that means the disk is completely dead 7197 * in our case we have to put the disk in on or off by sending commands 7198 * Of course that will fail anyway so return back here. 7199 * 7200 * Power changes to a device that's OFFLINE or SUSPENDED 7201 * are not allowed. 7202 */ 7203 if ((un->un_state == SD_STATE_OFFLINE) || 7204 (un->un_state == SD_STATE_SUSPENDED)) { 7205 mutex_exit(SD_MUTEX(un)); 7206 7207 if (got_semaphore_here != 0) { 7208 sema_v(&un->un_semoclose); 7209 } 7210 SD_TRACE(SD_LOG_IO_PM, un, 7211 "sdpower: exit, device is off-line.\n"); 7212 return (DDI_FAILURE); 7213 } 7214 7215 /* 7216 * Change the device's state to indicate it's power level 7217 * is being changed. Do this to prevent a power off in the 7218 * middle of commands, which is especially bad on devices 7219 * that are really powered off instead of just spun down. 7220 */ 7221 state_before_pm = un->un_state; 7222 un->un_state = SD_STATE_PM_CHANGING; 7223 7224 mutex_exit(SD_MUTEX(un)); 7225 7226 /* 7227 * Bypass checking the log sense information for removables 7228 * and devices for which the HBA set the pm-capable property. 7229 * If un->un_pm_capable_prop is SD_PM_CAPABLE_UNDEFINED (-1) 7230 * then the HBA did not create the property. 7231 */ 7232 if ((level == SD_SPINDLE_OFF) && (!ISREMOVABLE(un)) && 7233 un->un_pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 7234 /* 7235 * Get the log sense information to understand whether the 7236 * the powercycle counts have gone beyond the threshhold. 7237 */ 7238 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7239 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7240 7241 mutex_enter(SD_MUTEX(un)); 7242 log_sense_page = un->un_start_stop_cycle_page; 7243 mutex_exit(SD_MUTEX(un)); 7244 7245 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7246 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7247 #ifdef SDDEBUG 7248 if (sd_force_pm_supported) { 7249 /* Force a successful result */ 7250 rval = 0; 7251 } 7252 #endif 7253 if (rval != 0) { 7254 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7255 "Log Sense Failed\n"); 7256 kmem_free(log_page_data, log_page_size); 7257 /* Cannot support power management on those drives */ 7258 7259 if (got_semaphore_here != 0) { 7260 sema_v(&un->un_semoclose); 7261 } 7262 /* 7263 * On exit put the state back to it's original value 7264 * and broadcast to anyone waiting for the power 7265 * change completion. 7266 */ 7267 mutex_enter(SD_MUTEX(un)); 7268 un->un_state = state_before_pm; 7269 cv_broadcast(&un->un_suspend_cv); 7270 mutex_exit(SD_MUTEX(un)); 7271 SD_TRACE(SD_LOG_IO_PM, un, 7272 "sdpower: exit, Log Sense Failed.\n"); 7273 return (DDI_FAILURE); 7274 } 7275 7276 /* 7277 * From the page data - Convert the essential information to 7278 * pm_trans_data 7279 */ 7280 maxcycles = 7281 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7282 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7283 7284 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7285 7286 ncycles = 7287 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7288 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7289 7290 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7291 7292 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7293 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7294 log_page_data[8+i]; 7295 } 7296 7297 kmem_free(log_page_data, log_page_size); 7298 7299 /* 7300 * Call pm_trans_check routine to get the Ok from 7301 * the global policy 7302 */ 7303 7304 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7305 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7306 7307 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7308 #ifdef SDDEBUG 7309 if (sd_force_pm_supported) { 7310 /* Force a successful result */ 7311 rval = 1; 7312 } 7313 #endif 7314 switch (rval) { 7315 case 0: 7316 /* 7317 * Not Ok to Power cycle or error in parameters passed 7318 * Would have given the advised time to consider power 7319 * cycle. Based on the new intvlp parameter we are 7320 * supposed to pretend we are busy so that pm framework 7321 * will never call our power entry point. Because of 7322 * that install a timeout handler and wait for the 7323 * recommended time to elapse so that power management 7324 * can be effective again. 7325 * 7326 * To effect this behavior, call pm_busy_component to 7327 * indicate to the framework this device is busy. 7328 * By not adjusting un_pm_count the rest of PM in 7329 * the driver will function normally, and independant 7330 * of this but because the framework is told the device 7331 * is busy it won't attempt powering down until it gets 7332 * a matching idle. The timeout handler sends this. 7333 * Note: sd_pm_entry can't be called here to do this 7334 * because sdpower may have been called as a result 7335 * of a call to pm_raise_power from within sd_pm_entry. 7336 * 7337 * If a timeout handler is already active then 7338 * don't install another. 7339 */ 7340 mutex_enter(&un->un_pm_mutex); 7341 if (un->un_pm_timeid == NULL) { 7342 un->un_pm_timeid = 7343 timeout(sd_pm_timeout_handler, 7344 un, intvlp * drv_usectohz(1000000)); 7345 mutex_exit(&un->un_pm_mutex); 7346 (void) pm_busy_component(SD_DEVINFO(un), 0); 7347 } else { 7348 mutex_exit(&un->un_pm_mutex); 7349 } 7350 if (got_semaphore_here != 0) { 7351 sema_v(&un->un_semoclose); 7352 } 7353 /* 7354 * On exit put the state back to it's original value 7355 * and broadcast to anyone waiting for the power 7356 * change completion. 7357 */ 7358 mutex_enter(SD_MUTEX(un)); 7359 un->un_state = state_before_pm; 7360 cv_broadcast(&un->un_suspend_cv); 7361 mutex_exit(SD_MUTEX(un)); 7362 7363 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7364 "trans check Failed, not ok to power cycle.\n"); 7365 return (DDI_FAILURE); 7366 7367 case -1: 7368 if (got_semaphore_here != 0) { 7369 sema_v(&un->un_semoclose); 7370 } 7371 /* 7372 * On exit put the state back to it's original value 7373 * and broadcast to anyone waiting for the power 7374 * change completion. 7375 */ 7376 mutex_enter(SD_MUTEX(un)); 7377 un->un_state = state_before_pm; 7378 cv_broadcast(&un->un_suspend_cv); 7379 mutex_exit(SD_MUTEX(un)); 7380 SD_TRACE(SD_LOG_IO_PM, un, 7381 "sdpower: exit, trans check command Failed.\n"); 7382 return (DDI_FAILURE); 7383 } 7384 } 7385 7386 if (level == SD_SPINDLE_OFF) { 7387 /* 7388 * Save the last state... if the STOP FAILS we need it 7389 * for restoring 7390 */ 7391 mutex_enter(SD_MUTEX(un)); 7392 save_state = un->un_last_state; 7393 /* 7394 * There must not be any cmds. getting processed 7395 * in the driver when we get here. Power to the 7396 * device is potentially going off. 7397 */ 7398 ASSERT(un->un_ncmds_in_driver == 0); 7399 mutex_exit(SD_MUTEX(un)); 7400 7401 /* 7402 * For now suspend the device completely before spindle is 7403 * turned off 7404 */ 7405 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7406 if (got_semaphore_here != 0) { 7407 sema_v(&un->un_semoclose); 7408 } 7409 /* 7410 * On exit put the state back to it's original value 7411 * and broadcast to anyone waiting for the power 7412 * change completion. 7413 */ 7414 mutex_enter(SD_MUTEX(un)); 7415 un->un_state = state_before_pm; 7416 cv_broadcast(&un->un_suspend_cv); 7417 mutex_exit(SD_MUTEX(un)); 7418 SD_TRACE(SD_LOG_IO_PM, un, 7419 "sdpower: exit, PM suspend Failed.\n"); 7420 return (DDI_FAILURE); 7421 } 7422 } 7423 7424 /* 7425 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7426 * close, or strategy. Dump no long uses this routine, it uses it's 7427 * own code so it can be done in polled mode. 7428 */ 7429 7430 medium_present = TRUE; 7431 7432 /* 7433 * When powering up, issue a TUR in case the device is at unit 7434 * attention. Don't do retries. Bypass the PM layer, otherwise 7435 * a deadlock on un_pm_busy_cv will occur. 7436 */ 7437 if (level == SD_SPINDLE_ON) { 7438 (void) sd_send_scsi_TEST_UNIT_READY(un, 7439 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7440 } 7441 7442 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7443 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7444 7445 sval = sd_send_scsi_START_STOP_UNIT(un, 7446 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7447 SD_PATH_DIRECT); 7448 /* Command failed, check for media present. */ 7449 if ((sval == ENXIO) && ISREMOVABLE(un)) { 7450 medium_present = FALSE; 7451 } 7452 7453 /* 7454 * The conditions of interest here are: 7455 * if a spindle off with media present fails, 7456 * then restore the state and return an error. 7457 * else if a spindle on fails, 7458 * then return an error (there's no state to restore). 7459 * In all other cases we setup for the new state 7460 * and return success. 7461 */ 7462 switch (level) { 7463 case SD_SPINDLE_OFF: 7464 if ((medium_present == TRUE) && (sval != 0)) { 7465 /* The stop command from above failed */ 7466 rval = DDI_FAILURE; 7467 /* 7468 * The stop command failed, and we have media 7469 * present. Put the level back by calling the 7470 * sd_pm_resume() and set the state back to 7471 * it's previous value. 7472 */ 7473 (void) sd_ddi_pm_resume(un); 7474 mutex_enter(SD_MUTEX(un)); 7475 un->un_last_state = save_state; 7476 mutex_exit(SD_MUTEX(un)); 7477 break; 7478 } 7479 /* 7480 * The stop command from above succeeded. 7481 */ 7482 if (ISREMOVABLE(un)) { 7483 /* 7484 * Terminate watch thread in case of removable media 7485 * devices going into low power state. This is as per 7486 * the requirements of pm framework, otherwise commands 7487 * will be generated for the device (through watch 7488 * thread), even when the device is in low power state. 7489 */ 7490 mutex_enter(SD_MUTEX(un)); 7491 un->un_f_watcht_stopped = FALSE; 7492 if (un->un_swr_token != NULL) { 7493 opaque_t temp_token = un->un_swr_token; 7494 un->un_f_watcht_stopped = TRUE; 7495 un->un_swr_token = NULL; 7496 mutex_exit(SD_MUTEX(un)); 7497 (void) scsi_watch_request_terminate(temp_token, 7498 SCSI_WATCH_TERMINATE_WAIT); 7499 } else { 7500 mutex_exit(SD_MUTEX(un)); 7501 } 7502 } 7503 break; 7504 7505 default: /* The level requested is spindle on... */ 7506 /* 7507 * Legacy behavior: return success on a failed spinup 7508 * if there is no media in the drive. 7509 * Do this by looking at medium_present here. 7510 */ 7511 if ((sval != 0) && medium_present) { 7512 /* The start command from above failed */ 7513 rval = DDI_FAILURE; 7514 break; 7515 } 7516 /* 7517 * The start command from above succeeded 7518 * Resume the devices now that we have 7519 * started the disks 7520 */ 7521 (void) sd_ddi_pm_resume(un); 7522 7523 /* 7524 * Resume the watch thread since it was suspended 7525 * when the device went into low power mode. 7526 */ 7527 if (ISREMOVABLE(un)) { 7528 mutex_enter(SD_MUTEX(un)); 7529 if (un->un_f_watcht_stopped == TRUE) { 7530 opaque_t temp_token; 7531 7532 un->un_f_watcht_stopped = FALSE; 7533 mutex_exit(SD_MUTEX(un)); 7534 temp_token = scsi_watch_request_submit( 7535 SD_SCSI_DEVP(un), 7536 sd_check_media_time, 7537 SENSE_LENGTH, sd_media_watch_cb, 7538 (caddr_t)dev); 7539 mutex_enter(SD_MUTEX(un)); 7540 un->un_swr_token = temp_token; 7541 } 7542 mutex_exit(SD_MUTEX(un)); 7543 } 7544 } 7545 if (got_semaphore_here != 0) { 7546 sema_v(&un->un_semoclose); 7547 } 7548 /* 7549 * On exit put the state back to it's original value 7550 * and broadcast to anyone waiting for the power 7551 * change completion. 7552 */ 7553 mutex_enter(SD_MUTEX(un)); 7554 un->un_state = state_before_pm; 7555 cv_broadcast(&un->un_suspend_cv); 7556 mutex_exit(SD_MUTEX(un)); 7557 7558 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7559 7560 return (rval); 7561 } 7562 7563 7564 7565 /* 7566 * Function: sdattach 7567 * 7568 * Description: Driver's attach(9e) entry point function. 7569 * 7570 * Arguments: devi - opaque device info handle 7571 * cmd - attach type 7572 * 7573 * Return Code: DDI_SUCCESS 7574 * DDI_FAILURE 7575 * 7576 * Context: Kernel thread context 7577 */ 7578 7579 static int 7580 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7581 { 7582 switch (cmd) { 7583 case DDI_ATTACH: 7584 return (sd_unit_attach(devi)); 7585 case DDI_RESUME: 7586 return (sd_ddi_resume(devi)); 7587 default: 7588 break; 7589 } 7590 return (DDI_FAILURE); 7591 } 7592 7593 7594 /* 7595 * Function: sddetach 7596 * 7597 * Description: Driver's detach(9E) entry point function. 7598 * 7599 * Arguments: devi - opaque device info handle 7600 * cmd - detach type 7601 * 7602 * Return Code: DDI_SUCCESS 7603 * DDI_FAILURE 7604 * 7605 * Context: Kernel thread context 7606 */ 7607 7608 static int 7609 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7610 { 7611 switch (cmd) { 7612 case DDI_DETACH: 7613 return (sd_unit_detach(devi)); 7614 case DDI_SUSPEND: 7615 return (sd_ddi_suspend(devi)); 7616 default: 7617 break; 7618 } 7619 return (DDI_FAILURE); 7620 } 7621 7622 7623 /* 7624 * Function: sd_sync_with_callback 7625 * 7626 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7627 * state while the callback routine is active. 7628 * 7629 * Arguments: un: softstate structure for the instance 7630 * 7631 * Context: Kernel thread context 7632 */ 7633 7634 static void 7635 sd_sync_with_callback(struct sd_lun *un) 7636 { 7637 ASSERT(un != NULL); 7638 7639 mutex_enter(SD_MUTEX(un)); 7640 7641 ASSERT(un->un_in_callback >= 0); 7642 7643 while (un->un_in_callback > 0) { 7644 mutex_exit(SD_MUTEX(un)); 7645 delay(2); 7646 mutex_enter(SD_MUTEX(un)); 7647 } 7648 7649 mutex_exit(SD_MUTEX(un)); 7650 } 7651 7652 /* 7653 * Function: sd_unit_attach 7654 * 7655 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7656 * the soft state structure for the device and performs 7657 * all necessary structure and device initializations. 7658 * 7659 * Arguments: devi: the system's dev_info_t for the device. 7660 * 7661 * Return Code: DDI_SUCCESS if attach is successful. 7662 * DDI_FAILURE if any part of the attach fails. 7663 * 7664 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7665 * Kernel thread context only. Can sleep. 7666 */ 7667 7668 static int 7669 sd_unit_attach(dev_info_t *devi) 7670 { 7671 struct scsi_device *devp; 7672 struct sd_lun *un; 7673 char *variantp; 7674 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7675 int instance; 7676 int rval; 7677 int wc_enabled; 7678 uint64_t capacity; 7679 uint_t lbasize; 7680 7681 /* 7682 * Retrieve the target driver's private data area. This was set 7683 * up by the HBA. 7684 */ 7685 devp = ddi_get_driver_private(devi); 7686 7687 /* 7688 * Since we have no idea what state things were left in by the last 7689 * user of the device, set up some 'default' settings, ie. turn 'em 7690 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7691 * Do this before the scsi_probe, which sends an inquiry. 7692 * This is a fix for bug (4430280). 7693 * Of special importance is wide-xfer. The drive could have been left 7694 * in wide transfer mode by the last driver to communicate with it, 7695 * this includes us. If that's the case, and if the following is not 7696 * setup properly or we don't re-negotiate with the drive prior to 7697 * transferring data to/from the drive, it causes bus parity errors, 7698 * data overruns, and unexpected interrupts. This first occurred when 7699 * the fix for bug (4378686) was made. 7700 */ 7701 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7702 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7703 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7704 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7705 7706 /* 7707 * Use scsi_probe() to issue an INQUIRY command to the device. 7708 * This call will allocate and fill in the scsi_inquiry structure 7709 * and point the sd_inq member of the scsi_device structure to it. 7710 * If the attach succeeds, then this memory will not be de-allocated 7711 * (via scsi_unprobe()) until the instance is detached. 7712 */ 7713 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7714 goto probe_failed; 7715 } 7716 7717 /* 7718 * Check the device type as specified in the inquiry data and 7719 * claim it if it is of a type that we support. 7720 */ 7721 switch (devp->sd_inq->inq_dtype) { 7722 case DTYPE_DIRECT: 7723 break; 7724 case DTYPE_RODIRECT: 7725 break; 7726 case DTYPE_OPTICAL: 7727 break; 7728 case DTYPE_NOTPRESENT: 7729 default: 7730 /* Unsupported device type; fail the attach. */ 7731 goto probe_failed; 7732 } 7733 7734 /* 7735 * Allocate the soft state structure for this unit. 7736 * 7737 * We rely upon this memory being set to all zeroes by 7738 * ddi_soft_state_zalloc(). We assume that any member of the 7739 * soft state structure that is not explicitly initialized by 7740 * this routine will have a value of zero. 7741 */ 7742 instance = ddi_get_instance(devp->sd_dev); 7743 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7744 goto probe_failed; 7745 } 7746 7747 /* 7748 * Retrieve a pointer to the newly-allocated soft state. 7749 * 7750 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7751 * was successful, unless something has gone horribly wrong and the 7752 * ddi's soft state internals are corrupt (in which case it is 7753 * probably better to halt here than just fail the attach....) 7754 */ 7755 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7756 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7757 instance); 7758 /*NOTREACHED*/ 7759 } 7760 7761 /* 7762 * Link the back ptr of the driver soft state to the scsi_device 7763 * struct for this lun. 7764 * Save a pointer to the softstate in the driver-private area of 7765 * the scsi_device struct. 7766 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7767 * we first set un->un_sd below. 7768 */ 7769 un->un_sd = devp; 7770 devp->sd_private = (opaque_t)un; 7771 7772 /* 7773 * The following must be after devp is stored in the soft state struct. 7774 */ 7775 #ifdef SDDEBUG 7776 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7777 "%s_unit_attach: un:0x%p instance:%d\n", 7778 ddi_driver_name(devi), un, instance); 7779 #endif 7780 7781 /* 7782 * Set up the device type and node type (for the minor nodes). 7783 * By default we assume that the device can at least support the 7784 * Common Command Set. Call it a CD-ROM if it reports itself 7785 * as a RODIRECT device. 7786 */ 7787 switch (devp->sd_inq->inq_dtype) { 7788 case DTYPE_RODIRECT: 7789 un->un_node_type = DDI_NT_CD_CHAN; 7790 un->un_ctype = CTYPE_CDROM; 7791 break; 7792 case DTYPE_OPTICAL: 7793 un->un_node_type = DDI_NT_BLOCK_CHAN; 7794 un->un_ctype = CTYPE_ROD; 7795 break; 7796 default: 7797 un->un_node_type = DDI_NT_BLOCK_CHAN; 7798 un->un_ctype = CTYPE_CCS; 7799 break; 7800 } 7801 7802 /* 7803 * Try to read the interconnect type from the HBA. 7804 * 7805 * Note: This driver is currently compiled as two binaries, a parallel 7806 * scsi version (sd) and a fibre channel version (ssd). All functional 7807 * differences are determined at compile time. In the future a single 7808 * binary will be provided and the inteconnect type will be used to 7809 * differentiate between fibre and parallel scsi behaviors. At that time 7810 * it will be necessary for all fibre channel HBAs to support this 7811 * property. 7812 * 7813 * set un_f_is_fiber to TRUE ( default fiber ) 7814 */ 7815 un->un_f_is_fibre = TRUE; 7816 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7817 case INTERCONNECT_SSA: 7818 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7819 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7820 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7821 break; 7822 case INTERCONNECT_PARALLEL: 7823 un->un_f_is_fibre = FALSE; 7824 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7825 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7826 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7827 break; 7828 case INTERCONNECT_FIBRE: 7829 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7830 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7831 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7832 break; 7833 case INTERCONNECT_FABRIC: 7834 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7835 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7836 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7837 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7838 break; 7839 default: 7840 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7841 /* 7842 * The HBA does not support the "interconnect-type" property 7843 * (or did not provide a recognized type). 7844 * 7845 * Note: This will be obsoleted when a single fibre channel 7846 * and parallel scsi driver is delivered. In the meantime the 7847 * interconnect type will be set to the platform default.If that 7848 * type is not parallel SCSI, it means that we should be 7849 * assuming "ssd" semantics. However, here this also means that 7850 * the FC HBA is not supporting the "interconnect-type" property 7851 * like we expect it to, so log this occurrence. 7852 */ 7853 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7854 if (!SD_IS_PARALLEL_SCSI(un)) { 7855 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7856 "sd_unit_attach: un:0x%p Assuming " 7857 "INTERCONNECT_FIBRE\n", un); 7858 } else { 7859 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7860 "sd_unit_attach: un:0x%p Assuming " 7861 "INTERCONNECT_PARALLEL\n", un); 7862 un->un_f_is_fibre = FALSE; 7863 } 7864 #else 7865 /* 7866 * Note: This source will be implemented when a single fibre 7867 * channel and parallel scsi driver is delivered. The default 7868 * will be to assume that if a device does not support the 7869 * "interconnect-type" property it is a parallel SCSI HBA and 7870 * we will set the interconnect type for parallel scsi. 7871 */ 7872 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7873 un->un_f_is_fibre = FALSE; 7874 #endif 7875 break; 7876 } 7877 7878 if (un->un_f_is_fibre == TRUE) { 7879 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7880 SCSI_VERSION_3) { 7881 switch (un->un_interconnect_type) { 7882 case SD_INTERCONNECT_FIBRE: 7883 case SD_INTERCONNECT_SSA: 7884 un->un_node_type = DDI_NT_BLOCK_WWN; 7885 break; 7886 default: 7887 break; 7888 } 7889 } 7890 } 7891 7892 /* 7893 * Initialize the Request Sense command for the target 7894 */ 7895 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7896 goto alloc_rqs_failed; 7897 } 7898 7899 /* 7900 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7901 * with seperate binary for sd and ssd. 7902 * 7903 * x86 has 1 binary, un_retry_count is set base on connection type. 7904 * The hardcoded values will go away when Sparc uses 1 binary 7905 * for sd and ssd. This hardcoded values need to match 7906 * SD_RETRY_COUNT in sddef.h 7907 * The value used is base on interconnect type. 7908 * fibre = 3, parallel = 5 7909 */ 7910 #if defined(__i386) || defined(__amd64) 7911 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7912 #else 7913 un->un_retry_count = SD_RETRY_COUNT; 7914 #endif 7915 7916 /* 7917 * Set the per disk retry count to the default number of retries 7918 * for disks and CDROMs. This value can be overridden by the 7919 * disk property list or an entry in sd.conf. 7920 */ 7921 un->un_notready_retry_count = 7922 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7923 : DISK_NOT_READY_RETRY_COUNT(un); 7924 7925 /* 7926 * Set the busy retry count to the default value of un_retry_count. 7927 * This can be overridden by entries in sd.conf or the device 7928 * config table. 7929 */ 7930 un->un_busy_retry_count = un->un_retry_count; 7931 7932 /* 7933 * Init the reset threshold for retries. This number determines 7934 * how many retries must be performed before a reset can be issued 7935 * (for certain error conditions). This can be overridden by entries 7936 * in sd.conf or the device config table. 7937 */ 7938 un->un_reset_retry_count = (un->un_retry_count / 2); 7939 7940 /* 7941 * Set the victim_retry_count to the default un_retry_count 7942 */ 7943 un->un_victim_retry_count = (2 * un->un_retry_count); 7944 7945 /* 7946 * Set the reservation release timeout to the default value of 7947 * 5 seconds. This can be overridden by entries in ssd.conf or the 7948 * device config table. 7949 */ 7950 un->un_reserve_release_time = 5; 7951 7952 /* 7953 * Set up the default maximum transfer size. Note that this may 7954 * get updated later in the attach, when setting up default wide 7955 * operations for disks. 7956 */ 7957 #if defined(__i386) || defined(__amd64) 7958 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7959 #else 7960 un->un_max_xfer_size = (uint_t)maxphys; 7961 #endif 7962 7963 /* 7964 * Get "allow bus device reset" property (defaults to "enabled" if 7965 * the property was not defined). This is to disable bus resets for 7966 * certain kinds of error recovery. Note: In the future when a run-time 7967 * fibre check is available the soft state flag should default to 7968 * enabled. 7969 */ 7970 if (un->un_f_is_fibre == TRUE) { 7971 un->un_f_allow_bus_device_reset = TRUE; 7972 } else { 7973 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7974 "allow-bus-device-reset", 1) != 0) { 7975 un->un_f_allow_bus_device_reset = TRUE; 7976 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7977 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 7978 un); 7979 } else { 7980 un->un_f_allow_bus_device_reset = FALSE; 7981 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7982 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 7983 un); 7984 } 7985 } 7986 7987 /* 7988 * Check if this is an ATAPI device. ATAPI devices use Group 1 7989 * Read/Write commands and Group 2 Mode Sense/Select commands. 7990 * 7991 * Note: The "obsolete" way of doing this is to check for the "atapi" 7992 * property. The new "variant" property with a value of "atapi" has been 7993 * introduced so that future 'variants' of standard SCSI behavior (like 7994 * atapi) could be specified by the underlying HBA drivers by supplying 7995 * a new value for the "variant" property, instead of having to define a 7996 * new property. 7997 */ 7998 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7999 un->un_f_cfg_is_atapi = TRUE; 8000 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8001 "sd_unit_attach: un:0x%p Atapi device\n", un); 8002 } 8003 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8004 &variantp) == DDI_PROP_SUCCESS) { 8005 if (strcmp(variantp, "atapi") == 0) { 8006 un->un_f_cfg_is_atapi = TRUE; 8007 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8008 "sd_unit_attach: un:0x%p Atapi device\n", un); 8009 } 8010 ddi_prop_free(variantp); 8011 } 8012 8013 /* 8014 * Assume doorlock commands are supported. If not, the first 8015 * call to sd_send_scsi_DOORLOCK() will set to FALSE 8016 */ 8017 un->un_f_doorlock_supported = TRUE; 8018 8019 un->un_cmd_timeout = SD_IO_TIME; 8020 8021 /* Info on current states, statuses, etc. (Updated frequently) */ 8022 un->un_state = SD_STATE_NORMAL; 8023 un->un_last_state = SD_STATE_NORMAL; 8024 8025 /* Control & status info for command throttling */ 8026 un->un_throttle = sd_max_throttle; 8027 un->un_saved_throttle = sd_max_throttle; 8028 un->un_min_throttle = sd_min_throttle; 8029 8030 if (un->un_f_is_fibre == TRUE) { 8031 un->un_f_use_adaptive_throttle = TRUE; 8032 } else { 8033 un->un_f_use_adaptive_throttle = FALSE; 8034 } 8035 8036 /* Removable media support. */ 8037 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8038 un->un_mediastate = DKIO_NONE; 8039 un->un_specified_mediastate = DKIO_NONE; 8040 8041 /* CVs for suspend/resume (PM or DR) */ 8042 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8043 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8044 8045 /* Power management support. */ 8046 un->un_power_level = SD_SPINDLE_UNINIT; 8047 8048 /* 8049 * The open/close semaphore is used to serialize threads executing 8050 * in the driver's open & close entry point routines for a given 8051 * instance. 8052 */ 8053 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8054 8055 /* 8056 * The conf file entry and softstate variable is a forceful override, 8057 * meaning a non-zero value must be entered to change the default. 8058 */ 8059 un->un_f_disksort_disabled = FALSE; 8060 8061 /* 8062 * Retrieve the properties from the static driver table or the driver 8063 * configuration file (.conf) for this unit and update the soft state 8064 * for the device as needed for the indicated properties. 8065 * Note: the property configuration needs to occur here as some of the 8066 * following routines may have dependancies on soft state flags set 8067 * as part of the driver property configuration. 8068 */ 8069 sd_read_unit_properties(un); 8070 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8071 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8072 8073 /* 8074 * By default, we mark the capacity, lbazize, and geometry 8075 * as invalid. Only if we successfully read a valid capacity 8076 * will we update the un_blockcount and un_tgt_blocksize with the 8077 * valid values (the geometry will be validated later). 8078 */ 8079 un->un_f_blockcount_is_valid = FALSE; 8080 un->un_f_tgt_blocksize_is_valid = FALSE; 8081 un->un_f_geometry_is_valid = FALSE; 8082 8083 /* 8084 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8085 * otherwise. 8086 */ 8087 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8088 un->un_blockcount = 0; 8089 8090 /* 8091 * Set up the per-instance info needed to determine the correct 8092 * CDBs and other info for issuing commands to the target. 8093 */ 8094 sd_init_cdb_limits(un); 8095 8096 /* 8097 * Set up the IO chains to use, based upon the target type. 8098 */ 8099 if (ISREMOVABLE(un)) { 8100 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8101 } else { 8102 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8103 } 8104 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8105 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8106 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8107 8108 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8109 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8110 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8111 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8112 8113 8114 if (ISCD(un)) { 8115 un->un_additional_codes = sd_additional_codes; 8116 } else { 8117 un->un_additional_codes = NULL; 8118 } 8119 8120 /* 8121 * Create the kstats here so they can be available for attach-time 8122 * routines that send commands to the unit (either polled or via 8123 * sd_send_scsi_cmd). 8124 * 8125 * Note: This is a critical sequence that needs to be maintained: 8126 * 1) Instantiate the kstats here, before any routines using the 8127 * iopath (i.e. sd_send_scsi_cmd). 8128 * 2) Initialize the error stats (sd_set_errstats) and partition 8129 * stats (sd_set_pstats), following sd_validate_geometry(), 8130 * sd_register_devid(), and sd_disable_caching(). 8131 */ 8132 8133 un->un_stats = kstat_create(sd_label, instance, 8134 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8135 if (un->un_stats != NULL) { 8136 un->un_stats->ks_lock = SD_MUTEX(un); 8137 kstat_install(un->un_stats); 8138 } 8139 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8140 "sd_unit_attach: un:0x%p un_stats created\n", un); 8141 8142 sd_create_errstats(un, instance); 8143 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8144 "sd_unit_attach: un:0x%p errstats created\n", un); 8145 8146 /* 8147 * The following if/else code was relocated here from below as part 8148 * of the fix for bug (4430280). However with the default setup added 8149 * on entry to this routine, it's no longer absolutely necessary for 8150 * this to be before the call to sd_spin_up_unit. 8151 */ 8152 if (SD_IS_PARALLEL_SCSI(un)) { 8153 /* 8154 * If SCSI-2 tagged queueing is supported by the target 8155 * and by the host adapter then we will enable it. 8156 */ 8157 un->un_tagflags = 0; 8158 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8159 (devp->sd_inq->inq_cmdque) && 8160 (un->un_f_arq_enabled == TRUE)) { 8161 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8162 1, 1) == 1) { 8163 un->un_tagflags = FLAG_STAG; 8164 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8165 "sd_unit_attach: un:0x%p tag queueing " 8166 "enabled\n", un); 8167 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8168 "untagged-qing", 0) == 1) { 8169 un->un_f_opt_queueing = TRUE; 8170 un->un_saved_throttle = un->un_throttle = 8171 min(un->un_throttle, 3); 8172 } else { 8173 un->un_f_opt_queueing = FALSE; 8174 un->un_saved_throttle = un->un_throttle = 1; 8175 } 8176 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8177 == 1) && (un->un_f_arq_enabled == TRUE)) { 8178 /* The Host Adapter supports internal queueing. */ 8179 un->un_f_opt_queueing = TRUE; 8180 un->un_saved_throttle = un->un_throttle = 8181 min(un->un_throttle, 3); 8182 } else { 8183 un->un_f_opt_queueing = FALSE; 8184 un->un_saved_throttle = un->un_throttle = 1; 8185 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8186 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8187 } 8188 8189 8190 /* Setup or tear down default wide operations for disks */ 8191 8192 /* 8193 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8194 * and "ssd_max_xfer_size" to exist simultaneously on the same 8195 * system and be set to different values. In the future this 8196 * code may need to be updated when the ssd module is 8197 * obsoleted and removed from the system. (4299588) 8198 */ 8199 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8200 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8201 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8202 1, 1) == 1) { 8203 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8204 "sd_unit_attach: un:0x%p Wide Transfer " 8205 "enabled\n", un); 8206 } 8207 8208 /* 8209 * If tagged queuing has also been enabled, then 8210 * enable large xfers 8211 */ 8212 if (un->un_saved_throttle == sd_max_throttle) { 8213 un->un_max_xfer_size = 8214 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8215 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8216 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8217 "sd_unit_attach: un:0x%p max transfer " 8218 "size=0x%x\n", un, un->un_max_xfer_size); 8219 } 8220 } else { 8221 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8222 0, 1) == 1) { 8223 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8224 "sd_unit_attach: un:0x%p " 8225 "Wide Transfer disabled\n", un); 8226 } 8227 } 8228 } else { 8229 un->un_tagflags = FLAG_STAG; 8230 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8231 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8232 } 8233 8234 /* 8235 * If this target supports LUN reset, try to enable it. 8236 */ 8237 if (un->un_f_lun_reset_enabled) { 8238 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8239 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8240 "un:0x%p lun_reset capability set\n", un); 8241 } else { 8242 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8243 "un:0x%p lun-reset capability not set\n", un); 8244 } 8245 } 8246 8247 /* 8248 * At this point in the attach, we have enough info in the 8249 * soft state to be able to issue commands to the target. 8250 * 8251 * All command paths used below MUST issue their commands as 8252 * SD_PATH_DIRECT. This is important as intermediate layers 8253 * are not all initialized yet (such as PM). 8254 */ 8255 8256 /* 8257 * Send a TEST UNIT READY command to the device. This should clear 8258 * any outstanding UNIT ATTENTION that may be present. 8259 * 8260 * Note: Don't check for success, just track if there is a reservation, 8261 * this is a throw away command to clear any unit attentions. 8262 * 8263 * Note: This MUST be the first command issued to the target during 8264 * attach to ensure power on UNIT ATTENTIONS are cleared. 8265 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8266 * with attempts at spinning up a device with no media. 8267 */ 8268 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8269 reservation_flag = SD_TARGET_IS_RESERVED; 8270 } 8271 8272 /* 8273 * If the device is NOT a removable media device, attempt to spin 8274 * it up (using the START_STOP_UNIT command) and read its capacity 8275 * (using the READ CAPACITY command). Note, however, that either 8276 * of these could fail and in some cases we would continue with 8277 * the attach despite the failure (see below). 8278 */ 8279 if (devp->sd_inq->inq_dtype == DTYPE_DIRECT && !ISREMOVABLE(un)) { 8280 switch (sd_spin_up_unit(un)) { 8281 case 0: 8282 /* 8283 * Spin-up was successful; now try to read the 8284 * capacity. If successful then save the results 8285 * and mark the capacity & lbasize as valid. 8286 */ 8287 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8288 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8289 8290 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8291 &lbasize, SD_PATH_DIRECT)) { 8292 case 0: { 8293 if (capacity > DK_MAX_BLOCKS) { 8294 #ifdef _LP64 8295 /* 8296 * Enable descriptor format sense data 8297 * so that we can get 64 bit sense 8298 * data fields. 8299 */ 8300 sd_enable_descr_sense(un); 8301 #else 8302 /* 32-bit kernels can't handle this */ 8303 scsi_log(SD_DEVINFO(un), 8304 sd_label, CE_WARN, 8305 "disk has %llu blocks, which " 8306 "is too large for a 32-bit " 8307 "kernel", capacity); 8308 goto spinup_failed; 8309 #endif 8310 } 8311 /* 8312 * The following relies on 8313 * sd_send_scsi_READ_CAPACITY never 8314 * returning 0 for capacity and/or lbasize. 8315 */ 8316 sd_update_block_info(un, lbasize, capacity); 8317 8318 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8319 "sd_unit_attach: un:0x%p capacity = %ld " 8320 "blocks; lbasize= %ld.\n", un, 8321 un->un_blockcount, un->un_tgt_blocksize); 8322 8323 break; 8324 } 8325 case EACCES: 8326 /* 8327 * Should never get here if the spin-up 8328 * succeeded, but code it in anyway. 8329 * From here, just continue with the attach... 8330 */ 8331 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8332 "sd_unit_attach: un:0x%p " 8333 "sd_send_scsi_READ_CAPACITY " 8334 "returned reservation conflict\n", un); 8335 reservation_flag = SD_TARGET_IS_RESERVED; 8336 break; 8337 default: 8338 /* 8339 * Likewise, should never get here if the 8340 * spin-up succeeded. Just continue with 8341 * the attach... 8342 */ 8343 break; 8344 } 8345 break; 8346 case EACCES: 8347 /* 8348 * Device is reserved by another host. In this case 8349 * we could not spin it up or read the capacity, but 8350 * we continue with the attach anyway. 8351 */ 8352 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8353 "sd_unit_attach: un:0x%p spin-up reservation " 8354 "conflict.\n", un); 8355 reservation_flag = SD_TARGET_IS_RESERVED; 8356 break; 8357 default: 8358 /* Fail the attach if the spin-up failed. */ 8359 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8360 "sd_unit_attach: un:0x%p spin-up failed.", un); 8361 goto spinup_failed; 8362 } 8363 } 8364 8365 /* 8366 * Check to see if this is a MMC drive 8367 */ 8368 if (ISCD(un)) { 8369 sd_set_mmc_caps(un); 8370 } 8371 8372 /* 8373 * Create the minor nodes for the device. 8374 * Note: If we want to support fdisk on both sparc and intel, this will 8375 * have to separate out the notion that VTOC8 is always sparc, and 8376 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8377 * type will have to be determined at run-time, and the fdisk 8378 * partitioning will have to have been read & set up before we 8379 * create the minor nodes. (any other inits (such as kstats) that 8380 * also ought to be done before creating the minor nodes?) (Doesn't 8381 * setting up the minor nodes kind of imply that we're ready to 8382 * handle an open from userland?) 8383 */ 8384 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8385 goto create_minor_nodes_failed; 8386 } 8387 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8388 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8389 8390 /* 8391 * Add a zero-length attribute to tell the world we support 8392 * kernel ioctls (for layered drivers) 8393 */ 8394 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8395 DDI_KERNEL_IOCTL, NULL, 0); 8396 8397 /* 8398 * Add a boolean property to tell the world we support 8399 * the B_FAILFAST flag (for layered drivers) 8400 */ 8401 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8402 "ddi-failfast-supported", NULL, 0); 8403 8404 /* 8405 * Initialize power management 8406 */ 8407 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8408 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8409 sd_setup_pm(un, devi); 8410 if (un->un_f_pm_is_enabled == FALSE) { 8411 /* 8412 * For performance, point to a jump table that does 8413 * not include pm. 8414 * The direct and priority chains don't change with PM. 8415 * 8416 * Note: this is currently done based on individual device 8417 * capabilities. When an interface for determining system 8418 * power enabled state becomes available, or when additional 8419 * layers are added to the command chain, these values will 8420 * have to be re-evaluated for correctness. 8421 */ 8422 if (ISREMOVABLE(un)) { 8423 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8424 } else { 8425 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8426 } 8427 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8428 } 8429 8430 /* 8431 * This property is set to 0 by HA software to avoid retries 8432 * on a reserved disk. (The preferred property name is 8433 * "retry-on-reservation-conflict") (1189689) 8434 * 8435 * Note: The use of a global here can have unintended consequences. A 8436 * per instance variable is preferrable to match the capabilities of 8437 * different underlying hba's (4402600) 8438 */ 8439 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8440 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8441 sd_retry_on_reservation_conflict); 8442 if (sd_retry_on_reservation_conflict != 0) { 8443 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8444 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8445 sd_retry_on_reservation_conflict); 8446 } 8447 8448 /* Set up options for QFULL handling. */ 8449 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8450 "qfull-retries", -1)) != -1) { 8451 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8452 rval, 1); 8453 } 8454 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8455 "qfull-retry-interval", -1)) != -1) { 8456 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8457 rval, 1); 8458 } 8459 8460 /* 8461 * This just prints a message that announces the existence of the 8462 * device. The message is always printed in the system logfile, but 8463 * only appears on the console if the system is booted with the 8464 * -v (verbose) argument. 8465 */ 8466 ddi_report_dev(devi); 8467 8468 /* 8469 * The framework calls driver attach routines single-threaded 8470 * for a given instance. However we still acquire SD_MUTEX here 8471 * because this required for calling the sd_validate_geometry() 8472 * and sd_register_devid() functions. 8473 */ 8474 mutex_enter(SD_MUTEX(un)); 8475 un->un_f_geometry_is_valid = FALSE; 8476 un->un_mediastate = DKIO_NONE; 8477 un->un_reserved = -1; 8478 if (!ISREMOVABLE(un)) { 8479 /* 8480 * Read and validate the device's geometry (ie, disk label) 8481 * A new unformatted drive will not have a valid geometry, but 8482 * the driver needs to successfully attach to this device so 8483 * the drive can be formatted via ioctls. 8484 */ 8485 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8486 ENOTSUP)) && 8487 (un->un_blockcount < DK_MAX_BLOCKS)) { 8488 /* 8489 * We found a small disk with an EFI label on it; 8490 * we need to fix up the minor nodes accordingly. 8491 */ 8492 ddi_remove_minor_node(devi, "h"); 8493 ddi_remove_minor_node(devi, "h,raw"); 8494 (void) ddi_create_minor_node(devi, "wd", 8495 S_IFBLK, 8496 (instance << SDUNIT_SHIFT) | WD_NODE, 8497 un->un_node_type, NULL); 8498 (void) ddi_create_minor_node(devi, "wd,raw", 8499 S_IFCHR, 8500 (instance << SDUNIT_SHIFT) | WD_NODE, 8501 un->un_node_type, NULL); 8502 } 8503 } 8504 8505 /* 8506 * Read and initialize the devid for the unit. 8507 */ 8508 ASSERT(un->un_errstats != NULL); 8509 if (!ISREMOVABLE(un)) { 8510 sd_register_devid(un, devi, reservation_flag); 8511 } 8512 mutex_exit(SD_MUTEX(un)); 8513 8514 #if (defined(__fibre)) 8515 /* 8516 * Register callbacks for fibre only. You can't do this soley 8517 * on the basis of the devid_type because this is hba specific. 8518 * We need to query our hba capabilities to find out whether to 8519 * register or not. 8520 */ 8521 if (un->un_f_is_fibre) { 8522 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8523 sd_init_event_callbacks(un); 8524 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8525 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8526 } 8527 } 8528 #endif 8529 8530 if (un->un_f_opt_disable_cache == TRUE) { 8531 if (sd_disable_caching(un) != 0) { 8532 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8533 "sd_unit_attach: un:0x%p Could not disable " 8534 "caching", un); 8535 goto devid_failed; 8536 } 8537 } 8538 8539 /* 8540 * NOTE: Since there is currently no mechanism to 8541 * change the state of the Write Cache Enable mode select, 8542 * this code just checks the value of the WCE bit 8543 * at device attach time. If a mechanism 8544 * is added to the driver to change WCE, un_f_write_cache_enabled 8545 * must be updated appropriately. 8546 */ 8547 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8548 mutex_enter(SD_MUTEX(un)); 8549 un->un_f_write_cache_enabled = (wc_enabled != 0); 8550 mutex_exit(SD_MUTEX(un)); 8551 8552 /* 8553 * Set the pstat and error stat values here, so data obtained during the 8554 * previous attach-time routines is available. 8555 * 8556 * Note: This is a critical sequence that needs to be maintained: 8557 * 1) Instantiate the kstats before any routines using the iopath 8558 * (i.e. sd_send_scsi_cmd). 8559 * 2) Initialize the error stats (sd_set_errstats) and partition 8560 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8561 * sd_register_devid(), and sd_disable_caching(). 8562 */ 8563 if (!ISREMOVABLE(un) && (un->un_f_pkstats_enabled == TRUE)) { 8564 sd_set_pstats(un); 8565 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8566 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8567 } 8568 8569 sd_set_errstats(un); 8570 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8571 "sd_unit_attach: un:0x%p errstats set\n", un); 8572 8573 /* 8574 * Find out what type of reservation this disk supports. 8575 */ 8576 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8577 case 0: 8578 /* 8579 * SCSI-3 reservations are supported. 8580 */ 8581 un->un_reservation_type = SD_SCSI3_RESERVATION; 8582 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8583 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8584 break; 8585 case ENOTSUP: 8586 /* 8587 * The PERSISTENT RESERVE IN command would not be recognized by 8588 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8589 */ 8590 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8591 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8592 un->un_reservation_type = SD_SCSI2_RESERVATION; 8593 break; 8594 default: 8595 /* 8596 * default to SCSI-3 reservations 8597 */ 8598 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8599 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8600 un->un_reservation_type = SD_SCSI3_RESERVATION; 8601 break; 8602 } 8603 8604 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8605 "sd_unit_attach: un:0x%p exit success\n", un); 8606 8607 return (DDI_SUCCESS); 8608 8609 /* 8610 * An error occurred during the attach; clean up & return failure. 8611 */ 8612 8613 devid_failed: 8614 8615 setup_pm_failed: 8616 ddi_remove_minor_node(devi, NULL); 8617 8618 create_minor_nodes_failed: 8619 /* 8620 * Cleanup from the scsi_ifsetcap() calls (437868) 8621 */ 8622 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8623 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8624 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8625 8626 if (un->un_f_is_fibre == FALSE) { 8627 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8628 } 8629 8630 spinup_failed: 8631 8632 mutex_enter(SD_MUTEX(un)); 8633 8634 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8635 if (un->un_direct_priority_timeid != NULL) { 8636 timeout_id_t temp_id = un->un_direct_priority_timeid; 8637 un->un_direct_priority_timeid = NULL; 8638 mutex_exit(SD_MUTEX(un)); 8639 (void) untimeout(temp_id); 8640 mutex_enter(SD_MUTEX(un)); 8641 } 8642 8643 /* Cancel any pending start/stop timeouts */ 8644 if (un->un_startstop_timeid != NULL) { 8645 timeout_id_t temp_id = un->un_startstop_timeid; 8646 un->un_startstop_timeid = NULL; 8647 mutex_exit(SD_MUTEX(un)); 8648 (void) untimeout(temp_id); 8649 mutex_enter(SD_MUTEX(un)); 8650 } 8651 8652 /* Cancel any pending reset-throttle timeouts */ 8653 if (un->un_reset_throttle_timeid != NULL) { 8654 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8655 un->un_reset_throttle_timeid = NULL; 8656 mutex_exit(SD_MUTEX(un)); 8657 (void) untimeout(temp_id); 8658 mutex_enter(SD_MUTEX(un)); 8659 } 8660 8661 /* Cancel any pending retry timeouts */ 8662 if (un->un_retry_timeid != NULL) { 8663 timeout_id_t temp_id = un->un_retry_timeid; 8664 un->un_retry_timeid = NULL; 8665 mutex_exit(SD_MUTEX(un)); 8666 (void) untimeout(temp_id); 8667 mutex_enter(SD_MUTEX(un)); 8668 } 8669 8670 /* Cancel any pending delayed cv broadcast timeouts */ 8671 if (un->un_dcvb_timeid != NULL) { 8672 timeout_id_t temp_id = un->un_dcvb_timeid; 8673 un->un_dcvb_timeid = NULL; 8674 mutex_exit(SD_MUTEX(un)); 8675 (void) untimeout(temp_id); 8676 mutex_enter(SD_MUTEX(un)); 8677 } 8678 8679 mutex_exit(SD_MUTEX(un)); 8680 8681 /* There should not be any in-progress I/O so ASSERT this check */ 8682 ASSERT(un->un_ncmds_in_transport == 0); 8683 ASSERT(un->un_ncmds_in_driver == 0); 8684 8685 /* Do not free the softstate if the callback routine is active */ 8686 sd_sync_with_callback(un); 8687 8688 /* 8689 * Partition stats apparently are not used with removables. These would 8690 * not have been created during attach, so no need to clean them up... 8691 */ 8692 if (un->un_stats != NULL) { 8693 kstat_delete(un->un_stats); 8694 un->un_stats = NULL; 8695 } 8696 if (un->un_errstats != NULL) { 8697 kstat_delete(un->un_errstats); 8698 un->un_errstats = NULL; 8699 } 8700 8701 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8702 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8703 8704 ddi_prop_remove_all(devi); 8705 sema_destroy(&un->un_semoclose); 8706 cv_destroy(&un->un_state_cv); 8707 8708 getrbuf_failed: 8709 8710 sd_free_rqs(un); 8711 8712 alloc_rqs_failed: 8713 8714 devp->sd_private = NULL; 8715 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8716 8717 get_softstate_failed: 8718 /* 8719 * Note: the man pages are unclear as to whether or not doing a 8720 * ddi_soft_state_free(sd_state, instance) is the right way to 8721 * clean up after the ddi_soft_state_zalloc() if the subsequent 8722 * ddi_get_soft_state() fails. The implication seems to be 8723 * that the get_soft_state cannot fail if the zalloc succeeds. 8724 */ 8725 ddi_soft_state_free(sd_state, instance); 8726 8727 probe_failed: 8728 scsi_unprobe(devp); 8729 #ifdef SDDEBUG 8730 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 8731 (sd_level_mask & SD_LOGMASK_TRACE)) { 8732 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 8733 (void *)un); 8734 } 8735 #endif 8736 return (DDI_FAILURE); 8737 } 8738 8739 8740 /* 8741 * Function: sd_unit_detach 8742 * 8743 * Description: Performs DDI_DETACH processing for sddetach(). 8744 * 8745 * Return Code: DDI_SUCCESS 8746 * DDI_FAILURE 8747 * 8748 * Context: Kernel thread context 8749 */ 8750 8751 static int 8752 sd_unit_detach(dev_info_t *devi) 8753 { 8754 struct scsi_device *devp; 8755 struct sd_lun *un; 8756 int i; 8757 dev_t dev; 8758 int instance = ddi_get_instance(devi); 8759 8760 mutex_enter(&sd_detach_mutex); 8761 8762 /* 8763 * Fail the detach for any of the following: 8764 * - Unable to get the sd_lun struct for the instance 8765 * - A layered driver has an outstanding open on the instance 8766 * - Another thread is already detaching this instance 8767 * - Another thread is currently performing an open 8768 */ 8769 devp = ddi_get_driver_private(devi); 8770 if ((devp == NULL) || 8771 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8772 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8773 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8774 mutex_exit(&sd_detach_mutex); 8775 return (DDI_FAILURE); 8776 } 8777 8778 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8779 8780 /* 8781 * Mark this instance as currently in a detach, to inhibit any 8782 * opens from a layered driver. 8783 */ 8784 un->un_detach_count++; 8785 mutex_exit(&sd_detach_mutex); 8786 8787 dev = sd_make_device(SD_DEVINFO(un)); 8788 8789 _NOTE(COMPETING_THREADS_NOW); 8790 8791 mutex_enter(SD_MUTEX(un)); 8792 8793 /* 8794 * Fail the detach if there are any outstanding layered 8795 * opens on this device. 8796 */ 8797 for (i = 0; i < NDKMAP; i++) { 8798 if (un->un_ocmap.lyropen[i] != 0) { 8799 goto err_notclosed; 8800 } 8801 } 8802 8803 /* 8804 * Verify there are NO outstanding commands issued to this device. 8805 * ie, un_ncmds_in_transport == 0. 8806 * It's possible to have outstanding commands through the physio 8807 * code path, even though everything's closed. 8808 */ 8809 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8810 (un->un_direct_priority_timeid != NULL) || 8811 (un->un_state == SD_STATE_RWAIT)) { 8812 mutex_exit(SD_MUTEX(un)); 8813 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8814 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8815 goto err_stillbusy; 8816 } 8817 8818 /* 8819 * If we have the device reserved, release the reservation. 8820 */ 8821 if ((un->un_resvd_status & SD_RESERVE) && 8822 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8823 mutex_exit(SD_MUTEX(un)); 8824 /* 8825 * Note: sd_reserve_release sends a command to the device 8826 * via the sd_ioctlcmd() path, and can sleep. 8827 */ 8828 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8829 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8830 "sd_dr_detach: Cannot release reservation \n"); 8831 } 8832 } else { 8833 mutex_exit(SD_MUTEX(un)); 8834 } 8835 8836 /* 8837 * Untimeout any reserve recover, throttle reset, restart unit 8838 * and delayed broadcast timeout threads. Protect the timeout pointer 8839 * from getting nulled by their callback functions. 8840 */ 8841 mutex_enter(SD_MUTEX(un)); 8842 if (un->un_resvd_timeid != NULL) { 8843 timeout_id_t temp_id = un->un_resvd_timeid; 8844 un->un_resvd_timeid = NULL; 8845 mutex_exit(SD_MUTEX(un)); 8846 (void) untimeout(temp_id); 8847 mutex_enter(SD_MUTEX(un)); 8848 } 8849 8850 if (un->un_reset_throttle_timeid != NULL) { 8851 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8852 un->un_reset_throttle_timeid = NULL; 8853 mutex_exit(SD_MUTEX(un)); 8854 (void) untimeout(temp_id); 8855 mutex_enter(SD_MUTEX(un)); 8856 } 8857 8858 if (un->un_startstop_timeid != NULL) { 8859 timeout_id_t temp_id = un->un_startstop_timeid; 8860 un->un_startstop_timeid = NULL; 8861 mutex_exit(SD_MUTEX(un)); 8862 (void) untimeout(temp_id); 8863 mutex_enter(SD_MUTEX(un)); 8864 } 8865 8866 if (un->un_dcvb_timeid != NULL) { 8867 timeout_id_t temp_id = un->un_dcvb_timeid; 8868 un->un_dcvb_timeid = NULL; 8869 mutex_exit(SD_MUTEX(un)); 8870 (void) untimeout(temp_id); 8871 } else { 8872 mutex_exit(SD_MUTEX(un)); 8873 } 8874 8875 /* Remove any pending reservation reclaim requests for this device */ 8876 sd_rmv_resv_reclaim_req(dev); 8877 8878 mutex_enter(SD_MUTEX(un)); 8879 8880 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8881 if (un->un_direct_priority_timeid != NULL) { 8882 timeout_id_t temp_id = un->un_direct_priority_timeid; 8883 un->un_direct_priority_timeid = NULL; 8884 mutex_exit(SD_MUTEX(un)); 8885 (void) untimeout(temp_id); 8886 mutex_enter(SD_MUTEX(un)); 8887 } 8888 8889 /* Cancel any active multi-host disk watch thread requests */ 8890 if (un->un_mhd_token != NULL) { 8891 mutex_exit(SD_MUTEX(un)); 8892 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8893 if (scsi_watch_request_terminate(un->un_mhd_token, 8894 SCSI_WATCH_TERMINATE_NOWAIT)) { 8895 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8896 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8897 /* 8898 * Note: We are returning here after having removed 8899 * some driver timeouts above. This is consistent with 8900 * the legacy implementation but perhaps the watch 8901 * terminate call should be made with the wait flag set. 8902 */ 8903 goto err_stillbusy; 8904 } 8905 mutex_enter(SD_MUTEX(un)); 8906 un->un_mhd_token = NULL; 8907 } 8908 8909 if (un->un_swr_token != NULL) { 8910 mutex_exit(SD_MUTEX(un)); 8911 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8912 if (scsi_watch_request_terminate(un->un_swr_token, 8913 SCSI_WATCH_TERMINATE_NOWAIT)) { 8914 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8915 "sd_dr_detach: Cannot cancel swr watch request\n"); 8916 /* 8917 * Note: We are returning here after having removed 8918 * some driver timeouts above. This is consistent with 8919 * the legacy implementation but perhaps the watch 8920 * terminate call should be made with the wait flag set. 8921 */ 8922 goto err_stillbusy; 8923 } 8924 mutex_enter(SD_MUTEX(un)); 8925 un->un_swr_token = NULL; 8926 } 8927 8928 mutex_exit(SD_MUTEX(un)); 8929 8930 /* 8931 * Clear any scsi_reset_notifies. We clear the reset notifies 8932 * if we have not registered one. 8933 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8934 */ 8935 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8936 sd_mhd_reset_notify_cb, (caddr_t)un); 8937 8938 /* 8939 * protect the timeout pointers from getting nulled by 8940 * their callback functions during the cancellation process. 8941 * In such a scenario untimeout can be invoked with a null value. 8942 */ 8943 _NOTE(NO_COMPETING_THREADS_NOW); 8944 8945 mutex_enter(&un->un_pm_mutex); 8946 if (un->un_pm_idle_timeid != NULL) { 8947 timeout_id_t temp_id = un->un_pm_idle_timeid; 8948 un->un_pm_idle_timeid = NULL; 8949 mutex_exit(&un->un_pm_mutex); 8950 8951 /* 8952 * Timeout is active; cancel it. 8953 * Note that it'll never be active on a device 8954 * that does not support PM therefore we don't 8955 * have to check before calling pm_idle_component. 8956 */ 8957 (void) untimeout(temp_id); 8958 (void) pm_idle_component(SD_DEVINFO(un), 0); 8959 mutex_enter(&un->un_pm_mutex); 8960 } 8961 8962 /* 8963 * Check whether there is already a timeout scheduled for power 8964 * management. If yes then don't lower the power here, that's. 8965 * the timeout handler's job. 8966 */ 8967 if (un->un_pm_timeid != NULL) { 8968 timeout_id_t temp_id = un->un_pm_timeid; 8969 un->un_pm_timeid = NULL; 8970 mutex_exit(&un->un_pm_mutex); 8971 /* 8972 * Timeout is active; cancel it. 8973 * Note that it'll never be active on a device 8974 * that does not support PM therefore we don't 8975 * have to check before calling pm_idle_component. 8976 */ 8977 (void) untimeout(temp_id); 8978 (void) pm_idle_component(SD_DEVINFO(un), 0); 8979 8980 } else { 8981 mutex_exit(&un->un_pm_mutex); 8982 if ((un->un_f_pm_is_enabled == TRUE) && 8983 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8984 DDI_SUCCESS)) { 8985 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8986 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8987 /* 8988 * Fix for bug: 4297749, item # 13 8989 * The above test now includes a check to see if PM is 8990 * supported by this device before call 8991 * pm_lower_power(). 8992 * Note, the following is not dead code. The call to 8993 * pm_lower_power above will generate a call back into 8994 * our sdpower routine which might result in a timeout 8995 * handler getting activated. Therefore the following 8996 * code is valid and necessary. 8997 */ 8998 mutex_enter(&un->un_pm_mutex); 8999 if (un->un_pm_timeid != NULL) { 9000 timeout_id_t temp_id = un->un_pm_timeid; 9001 un->un_pm_timeid = NULL; 9002 mutex_exit(&un->un_pm_mutex); 9003 (void) untimeout(temp_id); 9004 (void) pm_idle_component(SD_DEVINFO(un), 0); 9005 } else { 9006 mutex_exit(&un->un_pm_mutex); 9007 } 9008 } 9009 } 9010 9011 /* 9012 * Cleanup from the scsi_ifsetcap() calls (437868) 9013 * Relocated here from above to be after the call to 9014 * pm_lower_power, which was getting errors. 9015 */ 9016 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9017 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9018 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9019 9020 if (un->un_f_is_fibre == FALSE) { 9021 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9022 } 9023 9024 /* 9025 * Remove any event callbacks, fibre only 9026 */ 9027 if (un->un_f_is_fibre == TRUE) { 9028 if ((un->un_insert_event != NULL) && 9029 (ddi_remove_event_handler(un->un_insert_cb_id) != 9030 DDI_SUCCESS)) { 9031 /* 9032 * Note: We are returning here after having done 9033 * substantial cleanup above. This is consistent 9034 * with the legacy implementation but this may not 9035 * be the right thing to do. 9036 */ 9037 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9038 "sd_dr_detach: Cannot cancel insert event\n"); 9039 goto err_remove_event; 9040 } 9041 un->un_insert_event = NULL; 9042 9043 if ((un->un_remove_event != NULL) && 9044 (ddi_remove_event_handler(un->un_remove_cb_id) != 9045 DDI_SUCCESS)) { 9046 /* 9047 * Note: We are returning here after having done 9048 * substantial cleanup above. This is consistent 9049 * with the legacy implementation but this may not 9050 * be the right thing to do. 9051 */ 9052 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9053 "sd_dr_detach: Cannot cancel remove event\n"); 9054 goto err_remove_event; 9055 } 9056 un->un_remove_event = NULL; 9057 } 9058 9059 /* Do not free the softstate if the callback routine is active */ 9060 sd_sync_with_callback(un); 9061 9062 /* 9063 * Hold the detach mutex here, to make sure that no other threads ever 9064 * can access a (partially) freed soft state structure. 9065 */ 9066 mutex_enter(&sd_detach_mutex); 9067 9068 /* 9069 * Clean up the soft state struct. 9070 * Cleanup is done in reverse order of allocs/inits. 9071 * At this point there should be no competing threads anymore. 9072 */ 9073 9074 /* Unregister and free device id. */ 9075 ddi_devid_unregister(devi); 9076 if (un->un_devid) { 9077 ddi_devid_free(un->un_devid); 9078 un->un_devid = NULL; 9079 } 9080 9081 /* 9082 * Destroy wmap cache if it exists. 9083 */ 9084 if (un->un_wm_cache != NULL) { 9085 kmem_cache_destroy(un->un_wm_cache); 9086 un->un_wm_cache = NULL; 9087 } 9088 9089 /* Remove minor nodes */ 9090 ddi_remove_minor_node(devi, NULL); 9091 9092 /* 9093 * kstat cleanup is done in detach for all device types (4363169). 9094 * We do not want to fail detach if the device kstats are not deleted 9095 * since there is a confusion about the devo_refcnt for the device. 9096 * We just delete the kstats and let detach complete successfully. 9097 */ 9098 if (un->un_stats != NULL) { 9099 kstat_delete(un->un_stats); 9100 un->un_stats = NULL; 9101 } 9102 if (un->un_errstats != NULL) { 9103 kstat_delete(un->un_errstats); 9104 un->un_errstats = NULL; 9105 } 9106 9107 /* Remove partition stats (not created for removables) */ 9108 if (!ISREMOVABLE(un)) { 9109 for (i = 0; i < NSDMAP; i++) { 9110 if (un->un_pstats[i] != NULL) { 9111 kstat_delete(un->un_pstats[i]); 9112 un->un_pstats[i] = NULL; 9113 } 9114 } 9115 } 9116 9117 /* Remove xbuf registration */ 9118 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9119 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9120 9121 /* Remove driver properties */ 9122 ddi_prop_remove_all(devi); 9123 9124 mutex_destroy(&un->un_pm_mutex); 9125 cv_destroy(&un->un_pm_busy_cv); 9126 9127 /* Open/close semaphore */ 9128 sema_destroy(&un->un_semoclose); 9129 9130 /* Removable media condvar. */ 9131 cv_destroy(&un->un_state_cv); 9132 9133 /* Suspend/resume condvar. */ 9134 cv_destroy(&un->un_suspend_cv); 9135 cv_destroy(&un->un_disk_busy_cv); 9136 9137 sd_free_rqs(un); 9138 9139 /* Free up soft state */ 9140 devp->sd_private = NULL; 9141 bzero(un, sizeof (struct sd_lun)); 9142 ddi_soft_state_free(sd_state, instance); 9143 9144 mutex_exit(&sd_detach_mutex); 9145 9146 /* This frees up the INQUIRY data associated with the device. */ 9147 scsi_unprobe(devp); 9148 9149 return (DDI_SUCCESS); 9150 9151 err_notclosed: 9152 mutex_exit(SD_MUTEX(un)); 9153 9154 err_stillbusy: 9155 _NOTE(NO_COMPETING_THREADS_NOW); 9156 9157 err_remove_event: 9158 mutex_enter(&sd_detach_mutex); 9159 un->un_detach_count--; 9160 mutex_exit(&sd_detach_mutex); 9161 9162 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9163 return (DDI_FAILURE); 9164 } 9165 9166 9167 /* 9168 * Driver minor node structure and data table 9169 */ 9170 struct driver_minor_data { 9171 char *name; 9172 minor_t minor; 9173 int type; 9174 }; 9175 9176 static struct driver_minor_data sd_minor_data[] = { 9177 {"a", 0, S_IFBLK}, 9178 {"b", 1, S_IFBLK}, 9179 {"c", 2, S_IFBLK}, 9180 {"d", 3, S_IFBLK}, 9181 {"e", 4, S_IFBLK}, 9182 {"f", 5, S_IFBLK}, 9183 {"g", 6, S_IFBLK}, 9184 {"h", 7, S_IFBLK}, 9185 #if defined(_SUNOS_VTOC_16) 9186 {"i", 8, S_IFBLK}, 9187 {"j", 9, S_IFBLK}, 9188 {"k", 10, S_IFBLK}, 9189 {"l", 11, S_IFBLK}, 9190 {"m", 12, S_IFBLK}, 9191 {"n", 13, S_IFBLK}, 9192 {"o", 14, S_IFBLK}, 9193 {"p", 15, S_IFBLK}, 9194 #endif /* defined(_SUNOS_VTOC_16) */ 9195 #if defined(_FIRMWARE_NEEDS_FDISK) 9196 {"q", 16, S_IFBLK}, 9197 {"r", 17, S_IFBLK}, 9198 {"s", 18, S_IFBLK}, 9199 {"t", 19, S_IFBLK}, 9200 {"u", 20, S_IFBLK}, 9201 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9202 {"a,raw", 0, S_IFCHR}, 9203 {"b,raw", 1, S_IFCHR}, 9204 {"c,raw", 2, S_IFCHR}, 9205 {"d,raw", 3, S_IFCHR}, 9206 {"e,raw", 4, S_IFCHR}, 9207 {"f,raw", 5, S_IFCHR}, 9208 {"g,raw", 6, S_IFCHR}, 9209 {"h,raw", 7, S_IFCHR}, 9210 #if defined(_SUNOS_VTOC_16) 9211 {"i,raw", 8, S_IFCHR}, 9212 {"j,raw", 9, S_IFCHR}, 9213 {"k,raw", 10, S_IFCHR}, 9214 {"l,raw", 11, S_IFCHR}, 9215 {"m,raw", 12, S_IFCHR}, 9216 {"n,raw", 13, S_IFCHR}, 9217 {"o,raw", 14, S_IFCHR}, 9218 {"p,raw", 15, S_IFCHR}, 9219 #endif /* defined(_SUNOS_VTOC_16) */ 9220 #if defined(_FIRMWARE_NEEDS_FDISK) 9221 {"q,raw", 16, S_IFCHR}, 9222 {"r,raw", 17, S_IFCHR}, 9223 {"s,raw", 18, S_IFCHR}, 9224 {"t,raw", 19, S_IFCHR}, 9225 {"u,raw", 20, S_IFCHR}, 9226 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9227 {0} 9228 }; 9229 9230 static struct driver_minor_data sd_minor_data_efi[] = { 9231 {"a", 0, S_IFBLK}, 9232 {"b", 1, S_IFBLK}, 9233 {"c", 2, S_IFBLK}, 9234 {"d", 3, S_IFBLK}, 9235 {"e", 4, S_IFBLK}, 9236 {"f", 5, S_IFBLK}, 9237 {"g", 6, S_IFBLK}, 9238 {"wd", 7, S_IFBLK}, 9239 #if defined(_FIRMWARE_NEEDS_FDISK) 9240 {"q", 16, S_IFBLK}, 9241 {"r", 17, S_IFBLK}, 9242 {"s", 18, S_IFBLK}, 9243 {"t", 19, S_IFBLK}, 9244 {"u", 20, S_IFBLK}, 9245 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9246 {"a,raw", 0, S_IFCHR}, 9247 {"b,raw", 1, S_IFCHR}, 9248 {"c,raw", 2, S_IFCHR}, 9249 {"d,raw", 3, S_IFCHR}, 9250 {"e,raw", 4, S_IFCHR}, 9251 {"f,raw", 5, S_IFCHR}, 9252 {"g,raw", 6, S_IFCHR}, 9253 {"wd,raw", 7, S_IFCHR}, 9254 #if defined(_FIRMWARE_NEEDS_FDISK) 9255 {"q,raw", 16, S_IFCHR}, 9256 {"r,raw", 17, S_IFCHR}, 9257 {"s,raw", 18, S_IFCHR}, 9258 {"t,raw", 19, S_IFCHR}, 9259 {"u,raw", 20, S_IFCHR}, 9260 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9261 {0} 9262 }; 9263 9264 9265 /* 9266 * Function: sd_create_minor_nodes 9267 * 9268 * Description: Create the minor device nodes for the instance. 9269 * 9270 * Arguments: un - driver soft state (unit) structure 9271 * devi - pointer to device info structure 9272 * 9273 * Return Code: DDI_SUCCESS 9274 * DDI_FAILURE 9275 * 9276 * Context: Kernel thread context 9277 */ 9278 9279 static int 9280 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9281 { 9282 struct driver_minor_data *dmdp; 9283 struct scsi_device *devp; 9284 int instance; 9285 char name[48]; 9286 9287 ASSERT(un != NULL); 9288 devp = ddi_get_driver_private(devi); 9289 instance = ddi_get_instance(devp->sd_dev); 9290 9291 /* 9292 * Create all the minor nodes for this target. 9293 */ 9294 if (un->un_blockcount > DK_MAX_BLOCKS) 9295 dmdp = sd_minor_data_efi; 9296 else 9297 dmdp = sd_minor_data; 9298 while (dmdp->name != NULL) { 9299 9300 (void) sprintf(name, "%s", dmdp->name); 9301 9302 if (ddi_create_minor_node(devi, name, dmdp->type, 9303 (instance << SDUNIT_SHIFT) | dmdp->minor, 9304 un->un_node_type, NULL) == DDI_FAILURE) { 9305 /* 9306 * Clean up any nodes that may have been created, in 9307 * case this fails in the middle of the loop. 9308 */ 9309 ddi_remove_minor_node(devi, NULL); 9310 return (DDI_FAILURE); 9311 } 9312 dmdp++; 9313 } 9314 9315 return (DDI_SUCCESS); 9316 } 9317 9318 9319 /* 9320 * Function: sd_create_errstats 9321 * 9322 * Description: This routine instantiates the device error stats. 9323 * 9324 * Note: During attach the stats are instantiated first so they are 9325 * available for attach-time routines that utilize the driver 9326 * iopath to send commands to the device. The stats are initialized 9327 * separately so data obtained during some attach-time routines is 9328 * available. (4362483) 9329 * 9330 * Arguments: un - driver soft state (unit) structure 9331 * instance - driver instance 9332 * 9333 * Context: Kernel thread context 9334 */ 9335 9336 static void 9337 sd_create_errstats(struct sd_lun *un, int instance) 9338 { 9339 struct sd_errstats *stp; 9340 char kstatmodule_err[KSTAT_STRLEN]; 9341 char kstatname[KSTAT_STRLEN]; 9342 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9343 9344 ASSERT(un != NULL); 9345 9346 if (un->un_errstats != NULL) { 9347 return; 9348 } 9349 9350 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9351 "%serr", sd_label); 9352 (void) snprintf(kstatname, sizeof (kstatname), 9353 "%s%d,err", sd_label, instance); 9354 9355 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9356 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9357 9358 if (un->un_errstats == NULL) { 9359 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9360 "sd_create_errstats: Failed kstat_create\n"); 9361 return; 9362 } 9363 9364 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9365 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9366 KSTAT_DATA_UINT32); 9367 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9368 KSTAT_DATA_UINT32); 9369 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9370 KSTAT_DATA_UINT32); 9371 kstat_named_init(&stp->sd_vid, "Vendor", 9372 KSTAT_DATA_CHAR); 9373 kstat_named_init(&stp->sd_pid, "Product", 9374 KSTAT_DATA_CHAR); 9375 kstat_named_init(&stp->sd_revision, "Revision", 9376 KSTAT_DATA_CHAR); 9377 kstat_named_init(&stp->sd_serial, "Serial No", 9378 KSTAT_DATA_CHAR); 9379 kstat_named_init(&stp->sd_capacity, "Size", 9380 KSTAT_DATA_ULONGLONG); 9381 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9382 KSTAT_DATA_UINT32); 9383 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9384 KSTAT_DATA_UINT32); 9385 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9386 KSTAT_DATA_UINT32); 9387 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9388 KSTAT_DATA_UINT32); 9389 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9390 KSTAT_DATA_UINT32); 9391 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9392 KSTAT_DATA_UINT32); 9393 9394 un->un_errstats->ks_private = un; 9395 un->un_errstats->ks_update = nulldev; 9396 9397 kstat_install(un->un_errstats); 9398 } 9399 9400 9401 /* 9402 * Function: sd_set_errstats 9403 * 9404 * Description: This routine sets the value of the vendor id, product id, 9405 * revision, serial number, and capacity device error stats. 9406 * 9407 * Note: During attach the stats are instantiated first so they are 9408 * available for attach-time routines that utilize the driver 9409 * iopath to send commands to the device. The stats are initialized 9410 * separately so data obtained during some attach-time routines is 9411 * available. (4362483) 9412 * 9413 * Arguments: un - driver soft state (unit) structure 9414 * 9415 * Context: Kernel thread context 9416 */ 9417 9418 static void 9419 sd_set_errstats(struct sd_lun *un) 9420 { 9421 struct sd_errstats *stp; 9422 9423 ASSERT(un != NULL); 9424 ASSERT(un->un_errstats != NULL); 9425 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9426 ASSERT(stp != NULL); 9427 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9428 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9429 (void) strncpy(stp->sd_revision.value.c, 9430 un->un_sd->sd_inq->inq_revision, 4); 9431 9432 /* 9433 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9434 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9435 * (4376302)) 9436 */ 9437 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9438 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9439 sizeof (SD_INQUIRY(un)->inq_serial)); 9440 } 9441 9442 if (un->un_f_blockcount_is_valid != TRUE) { 9443 /* 9444 * Set capacity error stat to 0 for no media. This ensures 9445 * a valid capacity is displayed in response to 'iostat -E' 9446 * when no media is present in the device. 9447 */ 9448 stp->sd_capacity.value.ui64 = 0; 9449 } else { 9450 /* 9451 * Multiply un_blockcount by un->un_sys_blocksize to get 9452 * capacity. 9453 * 9454 * Note: for non-512 blocksize devices "un_blockcount" has been 9455 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9456 * (un_tgt_blocksize / un->un_sys_blocksize). 9457 */ 9458 stp->sd_capacity.value.ui64 = (uint64_t) 9459 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9460 } 9461 } 9462 9463 9464 /* 9465 * Function: sd_set_pstats 9466 * 9467 * Description: This routine instantiates and initializes the partition 9468 * stats for each partition with more than zero blocks. 9469 * (4363169) 9470 * 9471 * Arguments: un - driver soft state (unit) structure 9472 * 9473 * Context: Kernel thread context 9474 */ 9475 9476 static void 9477 sd_set_pstats(struct sd_lun *un) 9478 { 9479 char kstatname[KSTAT_STRLEN]; 9480 int instance; 9481 int i; 9482 9483 ASSERT(un != NULL); 9484 9485 instance = ddi_get_instance(SD_DEVINFO(un)); 9486 9487 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9488 for (i = 0; i < NSDMAP; i++) { 9489 if ((un->un_pstats[i] == NULL) && 9490 (un->un_map[i].dkl_nblk != 0)) { 9491 (void) snprintf(kstatname, sizeof (kstatname), 9492 "%s%d,%s", sd_label, instance, 9493 sd_minor_data[i].name); 9494 un->un_pstats[i] = kstat_create(sd_label, 9495 instance, kstatname, "partition", KSTAT_TYPE_IO, 9496 1, KSTAT_FLAG_PERSISTENT); 9497 if (un->un_pstats[i] != NULL) { 9498 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9499 kstat_install(un->un_pstats[i]); 9500 } 9501 } 9502 } 9503 } 9504 9505 9506 #if (defined(__fibre)) 9507 /* 9508 * Function: sd_init_event_callbacks 9509 * 9510 * Description: This routine initializes the insertion and removal event 9511 * callbacks. (fibre only) 9512 * 9513 * Arguments: un - driver soft state (unit) structure 9514 * 9515 * Context: Kernel thread context 9516 */ 9517 9518 static void 9519 sd_init_event_callbacks(struct sd_lun *un) 9520 { 9521 ASSERT(un != NULL); 9522 9523 if ((un->un_insert_event == NULL) && 9524 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9525 &un->un_insert_event) == DDI_SUCCESS)) { 9526 /* 9527 * Add the callback for an insertion event 9528 */ 9529 (void) ddi_add_event_handler(SD_DEVINFO(un), 9530 un->un_insert_event, sd_event_callback, (void *)un, 9531 &(un->un_insert_cb_id)); 9532 } 9533 9534 if ((un->un_remove_event == NULL) && 9535 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9536 &un->un_remove_event) == DDI_SUCCESS)) { 9537 /* 9538 * Add the callback for a removal event 9539 */ 9540 (void) ddi_add_event_handler(SD_DEVINFO(un), 9541 un->un_remove_event, sd_event_callback, (void *)un, 9542 &(un->un_remove_cb_id)); 9543 } 9544 } 9545 9546 9547 /* 9548 * Function: sd_event_callback 9549 * 9550 * Description: This routine handles insert/remove events (photon). The 9551 * state is changed to OFFLINE which can be used to supress 9552 * error msgs. (fibre only) 9553 * 9554 * Arguments: un - driver soft state (unit) structure 9555 * 9556 * Context: Callout thread context 9557 */ 9558 /* ARGSUSED */ 9559 static void 9560 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9561 void *bus_impldata) 9562 { 9563 struct sd_lun *un = (struct sd_lun *)arg; 9564 9565 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9566 if (event == un->un_insert_event) { 9567 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9568 mutex_enter(SD_MUTEX(un)); 9569 if (un->un_state == SD_STATE_OFFLINE) { 9570 if (un->un_last_state != SD_STATE_SUSPENDED) { 9571 un->un_state = un->un_last_state; 9572 } else { 9573 /* 9574 * We have gone through SUSPEND/RESUME while 9575 * we were offline. Restore the last state 9576 */ 9577 un->un_state = un->un_save_state; 9578 } 9579 } 9580 mutex_exit(SD_MUTEX(un)); 9581 9582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9583 } else if (event == un->un_remove_event) { 9584 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9585 mutex_enter(SD_MUTEX(un)); 9586 /* 9587 * We need to handle an event callback that occurs during 9588 * the suspend operation, since we don't prevent it. 9589 */ 9590 if (un->un_state != SD_STATE_OFFLINE) { 9591 if (un->un_state != SD_STATE_SUSPENDED) { 9592 New_state(un, SD_STATE_OFFLINE); 9593 } else { 9594 un->un_last_state = SD_STATE_OFFLINE; 9595 } 9596 } 9597 mutex_exit(SD_MUTEX(un)); 9598 } else { 9599 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9600 "!Unknown event\n"); 9601 } 9602 9603 } 9604 #endif 9605 9606 9607 /* 9608 * Function: sd_disable_caching() 9609 * 9610 * Description: This routine is the driver entry point for disabling 9611 * read and write caching by modifying the WCE (write cache 9612 * enable) and RCD (read cache disable) bits of mode 9613 * page 8 (MODEPAGE_CACHING). 9614 * 9615 * Arguments: un - driver soft state (unit) structure 9616 * 9617 * Return Code: EIO 9618 * code returned by sd_send_scsi_MODE_SENSE and 9619 * sd_send_scsi_MODE_SELECT 9620 * 9621 * Context: Kernel Thread 9622 */ 9623 9624 static int 9625 sd_disable_caching(struct sd_lun *un) 9626 { 9627 struct mode_caching *mode_caching_page; 9628 uchar_t *header; 9629 size_t buflen; 9630 int hdrlen; 9631 int bd_len; 9632 int rval = 0; 9633 9634 ASSERT(un != NULL); 9635 9636 /* 9637 * Do a test unit ready, otherwise a mode sense may not work if this 9638 * is the first command sent to the device after boot. 9639 */ 9640 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9641 9642 if (un->un_f_cfg_is_atapi == TRUE) { 9643 hdrlen = MODE_HEADER_LENGTH_GRP2; 9644 } else { 9645 hdrlen = MODE_HEADER_LENGTH; 9646 } 9647 9648 /* 9649 * Allocate memory for the retrieved mode page and its headers. Set 9650 * a pointer to the page itself. 9651 */ 9652 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9653 header = kmem_zalloc(buflen, KM_SLEEP); 9654 9655 /* Get the information from the device. */ 9656 if (un->un_f_cfg_is_atapi == TRUE) { 9657 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9658 MODEPAGE_CACHING, SD_PATH_DIRECT); 9659 } else { 9660 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9661 MODEPAGE_CACHING, SD_PATH_DIRECT); 9662 } 9663 if (rval != 0) { 9664 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9665 "sd_disable_caching: Mode Sense Failed\n"); 9666 kmem_free(header, buflen); 9667 return (rval); 9668 } 9669 9670 /* 9671 * Determine size of Block Descriptors in order to locate 9672 * the mode page data. ATAPI devices return 0, SCSI devices 9673 * should return MODE_BLK_DESC_LENGTH. 9674 */ 9675 if (un->un_f_cfg_is_atapi == TRUE) { 9676 struct mode_header_grp2 *mhp; 9677 mhp = (struct mode_header_grp2 *)header; 9678 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9679 } else { 9680 bd_len = ((struct mode_header *)header)->bdesc_length; 9681 } 9682 9683 if (bd_len > MODE_BLK_DESC_LENGTH) { 9684 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9685 "sd_disable_caching: Mode Sense returned invalid " 9686 "block descriptor length\n"); 9687 kmem_free(header, buflen); 9688 return (EIO); 9689 } 9690 9691 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9692 9693 /* Check the relevant bits on successful mode sense. */ 9694 if ((mode_caching_page->wce) || !(mode_caching_page->rcd)) { 9695 /* 9696 * Read or write caching is enabled. Disable both of them. 9697 */ 9698 mode_caching_page->wce = 0; 9699 mode_caching_page->rcd = 1; 9700 9701 /* Clear reserved bits before mode select. */ 9702 mode_caching_page->mode_page.ps = 0; 9703 9704 /* 9705 * Clear out mode header for mode select. 9706 * The rest of the retrieved page will be reused. 9707 */ 9708 bzero(header, hdrlen); 9709 9710 /* Change the cache page to disable all caching. */ 9711 if (un->un_f_cfg_is_atapi == TRUE) { 9712 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 9713 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9714 } else { 9715 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 9716 buflen, SD_SAVE_PAGE, SD_PATH_DIRECT); 9717 } 9718 } 9719 9720 kmem_free(header, buflen); 9721 return (rval); 9722 } 9723 9724 9725 /* 9726 * Function: sd_get_write_cache_enabled() 9727 * 9728 * Description: This routine is the driver entry point for determining if 9729 * write caching is enabled. It examines the WCE (write cache 9730 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9731 * 9732 * Arguments: un - driver soft state (unit) structure 9733 * is_enabled - pointer to int where write cache enabled state 9734 * is returned (non-zero -> write cache enabled) 9735 * 9736 * 9737 * Return Code: EIO 9738 * code returned by sd_send_scsi_MODE_SENSE 9739 * 9740 * Context: Kernel Thread 9741 * 9742 * NOTE: If ioctl is added to disable write cache, this sequence should 9743 * be followed so that no locking is required for accesses to 9744 * un->un_f_write_cache_enabled: 9745 * do mode select to clear wce 9746 * do synchronize cache to flush cache 9747 * set un->un_f_write_cache_enabled = FALSE 9748 * 9749 * Conversely, an ioctl to enable the write cache should be done 9750 * in this order: 9751 * set un->un_f_write_cache_enabled = TRUE 9752 * do mode select to set wce 9753 */ 9754 9755 static int 9756 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 9757 { 9758 struct mode_caching *mode_caching_page; 9759 uchar_t *header; 9760 size_t buflen; 9761 int hdrlen; 9762 int bd_len; 9763 int rval = 0; 9764 9765 ASSERT(un != NULL); 9766 ASSERT(is_enabled != NULL); 9767 9768 /* in case of error, flag as enabled */ 9769 *is_enabled = TRUE; 9770 9771 /* 9772 * Do a test unit ready, otherwise a mode sense may not work if this 9773 * is the first command sent to the device after boot. 9774 */ 9775 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9776 9777 if (un->un_f_cfg_is_atapi == TRUE) { 9778 hdrlen = MODE_HEADER_LENGTH_GRP2; 9779 } else { 9780 hdrlen = MODE_HEADER_LENGTH; 9781 } 9782 9783 /* 9784 * Allocate memory for the retrieved mode page and its headers. Set 9785 * a pointer to the page itself. 9786 */ 9787 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9788 header = kmem_zalloc(buflen, KM_SLEEP); 9789 9790 /* Get the information from the device. */ 9791 if (un->un_f_cfg_is_atapi == TRUE) { 9792 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 9793 MODEPAGE_CACHING, SD_PATH_DIRECT); 9794 } else { 9795 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 9796 MODEPAGE_CACHING, SD_PATH_DIRECT); 9797 } 9798 if (rval != 0) { 9799 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9800 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9801 kmem_free(header, buflen); 9802 return (rval); 9803 } 9804 9805 /* 9806 * Determine size of Block Descriptors in order to locate 9807 * the mode page data. ATAPI devices return 0, SCSI devices 9808 * should return MODE_BLK_DESC_LENGTH. 9809 */ 9810 if (un->un_f_cfg_is_atapi == TRUE) { 9811 struct mode_header_grp2 *mhp; 9812 mhp = (struct mode_header_grp2 *)header; 9813 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9814 } else { 9815 bd_len = ((struct mode_header *)header)->bdesc_length; 9816 } 9817 9818 if (bd_len > MODE_BLK_DESC_LENGTH) { 9819 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9820 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9821 "block descriptor length\n"); 9822 kmem_free(header, buflen); 9823 return (EIO); 9824 } 9825 9826 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9827 *is_enabled = mode_caching_page->wce; 9828 9829 kmem_free(header, buflen); 9830 return (0); 9831 } 9832 9833 9834 /* 9835 * Function: sd_make_device 9836 * 9837 * Description: Utility routine to return the Solaris device number from 9838 * the data in the device's dev_info structure. 9839 * 9840 * Return Code: The Solaris device number 9841 * 9842 * Context: Any 9843 */ 9844 9845 static dev_t 9846 sd_make_device(dev_info_t *devi) 9847 { 9848 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 9849 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9850 } 9851 9852 9853 /* 9854 * Function: sd_pm_entry 9855 * 9856 * Description: Called at the start of a new command to manage power 9857 * and busy status of a device. This includes determining whether 9858 * the current power state of the device is sufficient for 9859 * performing the command or whether it must be changed. 9860 * The PM framework is notified appropriately. 9861 * Only with a return status of DDI_SUCCESS will the 9862 * component be busy to the framework. 9863 * 9864 * All callers of sd_pm_entry must check the return status 9865 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9866 * of DDI_FAILURE indicates the device failed to power up. 9867 * In this case un_pm_count has been adjusted so the result 9868 * on exit is still powered down, ie. count is less than 0. 9869 * Calling sd_pm_exit with this count value hits an ASSERT. 9870 * 9871 * Return Code: DDI_SUCCESS or DDI_FAILURE 9872 * 9873 * Context: Kernel thread context. 9874 */ 9875 9876 static int 9877 sd_pm_entry(struct sd_lun *un) 9878 { 9879 int return_status = DDI_SUCCESS; 9880 9881 ASSERT(!mutex_owned(SD_MUTEX(un))); 9882 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9883 9884 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9885 9886 if (un->un_f_pm_is_enabled == FALSE) { 9887 SD_TRACE(SD_LOG_IO_PM, un, 9888 "sd_pm_entry: exiting, PM not enabled\n"); 9889 return (return_status); 9890 } 9891 9892 /* 9893 * Just increment a counter if PM is enabled. On the transition from 9894 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9895 * the count with each IO and mark the device as idle when the count 9896 * hits 0. 9897 * 9898 * If the count is less than 0 the device is powered down. If a powered 9899 * down device is successfully powered up then the count must be 9900 * incremented to reflect the power up. Note that it'll get incremented 9901 * a second time to become busy. 9902 * 9903 * Because the following has the potential to change the device state 9904 * and must release the un_pm_mutex to do so, only one thread can be 9905 * allowed through at a time. 9906 */ 9907 9908 mutex_enter(&un->un_pm_mutex); 9909 while (un->un_pm_busy == TRUE) { 9910 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9911 } 9912 un->un_pm_busy = TRUE; 9913 9914 if (un->un_pm_count < 1) { 9915 9916 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9917 9918 /* 9919 * Indicate we are now busy so the framework won't attempt to 9920 * power down the device. This call will only fail if either 9921 * we passed a bad component number or the device has no 9922 * components. Neither of these should ever happen. 9923 */ 9924 mutex_exit(&un->un_pm_mutex); 9925 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9926 ASSERT(return_status == DDI_SUCCESS); 9927 9928 mutex_enter(&un->un_pm_mutex); 9929 9930 if (un->un_pm_count < 0) { 9931 mutex_exit(&un->un_pm_mutex); 9932 9933 SD_TRACE(SD_LOG_IO_PM, un, 9934 "sd_pm_entry: power up component\n"); 9935 9936 /* 9937 * pm_raise_power will cause sdpower to be called 9938 * which brings the device power level to the 9939 * desired state, ON in this case. If successful, 9940 * un_pm_count and un_power_level will be updated 9941 * appropriately. 9942 */ 9943 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9944 SD_SPINDLE_ON); 9945 9946 mutex_enter(&un->un_pm_mutex); 9947 9948 if (return_status != DDI_SUCCESS) { 9949 /* 9950 * Power up failed. 9951 * Idle the device and adjust the count 9952 * so the result on exit is that we're 9953 * still powered down, ie. count is less than 0. 9954 */ 9955 SD_TRACE(SD_LOG_IO_PM, un, 9956 "sd_pm_entry: power up failed," 9957 " idle the component\n"); 9958 9959 (void) pm_idle_component(SD_DEVINFO(un), 0); 9960 un->un_pm_count--; 9961 } else { 9962 /* 9963 * Device is powered up, verify the 9964 * count is non-negative. 9965 * This is debug only. 9966 */ 9967 ASSERT(un->un_pm_count == 0); 9968 } 9969 } 9970 9971 if (return_status == DDI_SUCCESS) { 9972 /* 9973 * For performance, now that the device has been tagged 9974 * as busy, and it's known to be powered up, update the 9975 * chain types to use jump tables that do not include 9976 * pm. This significantly lowers the overhead and 9977 * therefore improves performance. 9978 */ 9979 9980 mutex_exit(&un->un_pm_mutex); 9981 mutex_enter(SD_MUTEX(un)); 9982 SD_TRACE(SD_LOG_IO_PM, un, 9983 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9984 un->un_uscsi_chain_type); 9985 9986 if (ISREMOVABLE(un)) { 9987 un->un_buf_chain_type = 9988 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9989 } else { 9990 un->un_buf_chain_type = 9991 SD_CHAIN_INFO_DISK_NO_PM; 9992 } 9993 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9994 9995 SD_TRACE(SD_LOG_IO_PM, un, 9996 " changed uscsi_chain_type to %d\n", 9997 un->un_uscsi_chain_type); 9998 mutex_exit(SD_MUTEX(un)); 9999 mutex_enter(&un->un_pm_mutex); 10000 10001 if (un->un_pm_idle_timeid == NULL) { 10002 /* 300 ms. */ 10003 un->un_pm_idle_timeid = 10004 timeout(sd_pm_idletimeout_handler, un, 10005 (drv_usectohz((clock_t)300000))); 10006 /* 10007 * Include an extra call to busy which keeps the 10008 * device busy with-respect-to the PM layer 10009 * until the timer fires, at which time it'll 10010 * get the extra idle call. 10011 */ 10012 (void) pm_busy_component(SD_DEVINFO(un), 0); 10013 } 10014 } 10015 } 10016 un->un_pm_busy = FALSE; 10017 /* Next... */ 10018 cv_signal(&un->un_pm_busy_cv); 10019 10020 un->un_pm_count++; 10021 10022 SD_TRACE(SD_LOG_IO_PM, un, 10023 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10024 10025 mutex_exit(&un->un_pm_mutex); 10026 10027 return (return_status); 10028 } 10029 10030 10031 /* 10032 * Function: sd_pm_exit 10033 * 10034 * Description: Called at the completion of a command to manage busy 10035 * status for the device. If the device becomes idle the 10036 * PM framework is notified. 10037 * 10038 * Context: Kernel thread context 10039 */ 10040 10041 static void 10042 sd_pm_exit(struct sd_lun *un) 10043 { 10044 ASSERT(!mutex_owned(SD_MUTEX(un))); 10045 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10046 10047 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10048 10049 /* 10050 * After attach the following flag is only read, so don't 10051 * take the penalty of acquiring a mutex for it. 10052 */ 10053 if (un->un_f_pm_is_enabled == TRUE) { 10054 10055 mutex_enter(&un->un_pm_mutex); 10056 un->un_pm_count--; 10057 10058 SD_TRACE(SD_LOG_IO_PM, un, 10059 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10060 10061 ASSERT(un->un_pm_count >= 0); 10062 if (un->un_pm_count == 0) { 10063 mutex_exit(&un->un_pm_mutex); 10064 10065 SD_TRACE(SD_LOG_IO_PM, un, 10066 "sd_pm_exit: idle component\n"); 10067 10068 (void) pm_idle_component(SD_DEVINFO(un), 0); 10069 10070 } else { 10071 mutex_exit(&un->un_pm_mutex); 10072 } 10073 } 10074 10075 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10076 } 10077 10078 10079 /* 10080 * Function: sdopen 10081 * 10082 * Description: Driver's open(9e) entry point function. 10083 * 10084 * Arguments: dev_i - pointer to device number 10085 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10086 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10087 * cred_p - user credential pointer 10088 * 10089 * Return Code: EINVAL 10090 * ENXIO 10091 * EIO 10092 * EROFS 10093 * EBUSY 10094 * 10095 * Context: Kernel thread context 10096 */ 10097 /* ARGSUSED */ 10098 static int 10099 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10100 { 10101 struct sd_lun *un; 10102 int nodelay; 10103 int part; 10104 uint64_t partmask; 10105 int instance; 10106 dev_t dev; 10107 int rval = EIO; 10108 10109 /* Validate the open type */ 10110 if (otyp >= OTYPCNT) { 10111 return (EINVAL); 10112 } 10113 10114 dev = *dev_p; 10115 instance = SDUNIT(dev); 10116 mutex_enter(&sd_detach_mutex); 10117 10118 /* 10119 * Fail the open if there is no softstate for the instance, or 10120 * if another thread somewhere is trying to detach the instance. 10121 */ 10122 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10123 (un->un_detach_count != 0)) { 10124 mutex_exit(&sd_detach_mutex); 10125 /* 10126 * The probe cache only needs to be cleared when open (9e) fails 10127 * with ENXIO (4238046). 10128 */ 10129 /* 10130 * un-conditionally clearing probe cache is ok with 10131 * separate sd/ssd binaries 10132 * x86 platform can be an issue with both parallel 10133 * and fibre in 1 binary 10134 */ 10135 sd_scsi_clear_probe_cache(); 10136 return (ENXIO); 10137 } 10138 10139 /* 10140 * The un_layer_count is to prevent another thread in specfs from 10141 * trying to detach the instance, which can happen when we are 10142 * called from a higher-layer driver instead of thru specfs. 10143 * This will not be needed when DDI provides a layered driver 10144 * interface that allows specfs to know that an instance is in 10145 * use by a layered driver & should not be detached. 10146 * 10147 * Note: the semantics for layered driver opens are exactly one 10148 * close for every open. 10149 */ 10150 if (otyp == OTYP_LYR) { 10151 un->un_layer_count++; 10152 } 10153 10154 /* 10155 * Keep a count of the current # of opens in progress. This is because 10156 * some layered drivers try to call us as a regular open. This can 10157 * cause problems that we cannot prevent, however by keeping this count 10158 * we can at least keep our open and detach routines from racing against 10159 * each other under such conditions. 10160 */ 10161 un->un_opens_in_progress++; 10162 mutex_exit(&sd_detach_mutex); 10163 10164 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10165 part = SDPART(dev); 10166 partmask = 1 << part; 10167 10168 /* 10169 * We use a semaphore here in order to serialize 10170 * open and close requests on the device. 10171 */ 10172 sema_p(&un->un_semoclose); 10173 10174 mutex_enter(SD_MUTEX(un)); 10175 10176 /* 10177 * All device accesses go thru sdstrategy() where we check 10178 * on suspend status but there could be a scsi_poll command, 10179 * which bypasses sdstrategy(), so we need to check pm 10180 * status. 10181 */ 10182 10183 if (!nodelay) { 10184 while ((un->un_state == SD_STATE_SUSPENDED) || 10185 (un->un_state == SD_STATE_PM_CHANGING)) { 10186 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10187 } 10188 10189 mutex_exit(SD_MUTEX(un)); 10190 if (sd_pm_entry(un) != DDI_SUCCESS) { 10191 rval = EIO; 10192 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10193 "sdopen: sd_pm_entry failed\n"); 10194 goto open_failed_with_pm; 10195 } 10196 mutex_enter(SD_MUTEX(un)); 10197 } 10198 10199 /* check for previous exclusive open */ 10200 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10201 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10202 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10203 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10204 10205 if (un->un_exclopen & (partmask)) { 10206 goto excl_open_fail; 10207 } 10208 10209 if (flag & FEXCL) { 10210 int i; 10211 if (un->un_ocmap.lyropen[part]) { 10212 goto excl_open_fail; 10213 } 10214 for (i = 0; i < (OTYPCNT - 1); i++) { 10215 if (un->un_ocmap.regopen[i] & (partmask)) { 10216 goto excl_open_fail; 10217 } 10218 } 10219 } 10220 10221 /* 10222 * Check the write permission if this is a removable media device, 10223 * NDELAY has not been set, and writable permission is requested. 10224 * 10225 * Note: If NDELAY was set and this is write-protected media the WRITE 10226 * attempt will fail with EIO as part of the I/O processing. This is a 10227 * more permissive implementation that allows the open to succeed and 10228 * WRITE attempts to fail when appropriate. 10229 */ 10230 if (ISREMOVABLE(un)) { 10231 if ((flag & FWRITE) && (!nodelay)) { 10232 mutex_exit(SD_MUTEX(un)); 10233 /* 10234 * Defer the check for write permission on writable 10235 * DVD drive till sdstrategy and will not fail open even 10236 * if FWRITE is set as the device can be writable 10237 * depending upon the media and the media can change 10238 * after the call to open(). 10239 */ 10240 if (un->un_f_dvdram_writable_device == FALSE) { 10241 if (ISCD(un) || sr_check_wp(dev)) { 10242 rval = EROFS; 10243 mutex_enter(SD_MUTEX(un)); 10244 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10245 "write to cd or write protected media\n"); 10246 goto open_fail; 10247 } 10248 } 10249 mutex_enter(SD_MUTEX(un)); 10250 } 10251 } 10252 10253 /* 10254 * If opening in NDELAY/NONBLOCK mode, just return. 10255 * Check if disk is ready and has a valid geometry later. 10256 */ 10257 if (!nodelay) { 10258 mutex_exit(SD_MUTEX(un)); 10259 rval = sd_ready_and_valid(un); 10260 mutex_enter(SD_MUTEX(un)); 10261 /* 10262 * Fail if device is not ready or if the number of disk 10263 * blocks is zero or negative for non CD devices. 10264 */ 10265 if ((rval != SD_READY_VALID) || 10266 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10267 if (ISREMOVABLE(un)) { 10268 rval = ENXIO; 10269 } else { 10270 rval = EIO; 10271 } 10272 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10273 "device not ready or invalid disk block value\n"); 10274 goto open_fail; 10275 } 10276 #if defined(__i386) || defined(__amd64) 10277 } else { 10278 uchar_t *cp; 10279 /* 10280 * x86 requires special nodelay handling, so that p0 is 10281 * always defined and accessible. 10282 * Invalidate geometry only if device is not already open. 10283 */ 10284 cp = &un->un_ocmap.chkd[0]; 10285 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10286 if (*cp != (uchar_t)0) { 10287 break; 10288 } 10289 cp++; 10290 } 10291 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10292 un->un_f_geometry_is_valid = FALSE; 10293 } 10294 10295 #endif 10296 } 10297 10298 if (otyp == OTYP_LYR) { 10299 un->un_ocmap.lyropen[part]++; 10300 } else { 10301 un->un_ocmap.regopen[otyp] |= partmask; 10302 } 10303 10304 /* Set up open and exclusive open flags */ 10305 if (flag & FEXCL) { 10306 un->un_exclopen |= (partmask); 10307 } 10308 10309 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10310 "open of part %d type %d\n", part, otyp); 10311 10312 mutex_exit(SD_MUTEX(un)); 10313 if (!nodelay) { 10314 sd_pm_exit(un); 10315 } 10316 10317 sema_v(&un->un_semoclose); 10318 10319 mutex_enter(&sd_detach_mutex); 10320 un->un_opens_in_progress--; 10321 mutex_exit(&sd_detach_mutex); 10322 10323 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10324 return (DDI_SUCCESS); 10325 10326 excl_open_fail: 10327 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10328 rval = EBUSY; 10329 10330 open_fail: 10331 mutex_exit(SD_MUTEX(un)); 10332 10333 /* 10334 * On a failed open we must exit the pm management. 10335 */ 10336 if (!nodelay) { 10337 sd_pm_exit(un); 10338 } 10339 open_failed_with_pm: 10340 sema_v(&un->un_semoclose); 10341 10342 mutex_enter(&sd_detach_mutex); 10343 un->un_opens_in_progress--; 10344 if (otyp == OTYP_LYR) { 10345 un->un_layer_count--; 10346 } 10347 mutex_exit(&sd_detach_mutex); 10348 10349 return (rval); 10350 } 10351 10352 10353 /* 10354 * Function: sdclose 10355 * 10356 * Description: Driver's close(9e) entry point function. 10357 * 10358 * Arguments: dev - device number 10359 * flag - file status flag, informational only 10360 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10361 * cred_p - user credential pointer 10362 * 10363 * Return Code: ENXIO 10364 * 10365 * Context: Kernel thread context 10366 */ 10367 /* ARGSUSED */ 10368 static int 10369 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10370 { 10371 struct sd_lun *un; 10372 uchar_t *cp; 10373 int part; 10374 int nodelay; 10375 int rval = 0; 10376 10377 /* Validate the open type */ 10378 if (otyp >= OTYPCNT) { 10379 return (ENXIO); 10380 } 10381 10382 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10383 return (ENXIO); 10384 } 10385 10386 part = SDPART(dev); 10387 nodelay = flag & (FNDELAY | FNONBLOCK); 10388 10389 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10390 "sdclose: close of part %d type %d\n", part, otyp); 10391 10392 /* 10393 * We use a semaphore here in order to serialize 10394 * open and close requests on the device. 10395 */ 10396 sema_p(&un->un_semoclose); 10397 10398 mutex_enter(SD_MUTEX(un)); 10399 10400 /* Don't proceed if power is being changed. */ 10401 while (un->un_state == SD_STATE_PM_CHANGING) { 10402 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10403 } 10404 10405 if (un->un_exclopen & (1 << part)) { 10406 un->un_exclopen &= ~(1 << part); 10407 } 10408 10409 /* Update the open partition map */ 10410 if (otyp == OTYP_LYR) { 10411 un->un_ocmap.lyropen[part] -= 1; 10412 } else { 10413 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10414 } 10415 10416 cp = &un->un_ocmap.chkd[0]; 10417 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10418 if (*cp != NULL) { 10419 break; 10420 } 10421 cp++; 10422 } 10423 10424 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10425 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10426 10427 /* 10428 * We avoid persistance upon the last close, and set 10429 * the throttle back to the maximum. 10430 */ 10431 un->un_throttle = un->un_saved_throttle; 10432 10433 if (un->un_state == SD_STATE_OFFLINE) { 10434 if (un->un_f_is_fibre == FALSE) { 10435 scsi_log(SD_DEVINFO(un), sd_label, 10436 CE_WARN, "offline\n"); 10437 } 10438 un->un_f_geometry_is_valid = FALSE; 10439 10440 } else { 10441 /* 10442 * Flush any outstanding writes in NVRAM cache. 10443 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10444 * cmd, it may not work for non-Pluto devices. 10445 * SYNCHRONIZE CACHE is not required for removables, 10446 * except DVD-RAM drives. 10447 * 10448 * Also note: because SYNCHRONIZE CACHE is currently 10449 * the only command issued here that requires the 10450 * drive be powered up, only do the power up before 10451 * sending the Sync Cache command. If additional 10452 * commands are added which require a powered up 10453 * drive, the following sequence may have to change. 10454 * 10455 * And finally, note that parallel SCSI on SPARC 10456 * only issues a Sync Cache to DVD-RAM, a newly 10457 * supported device. 10458 */ 10459 #if defined(__i386) || defined(__amd64) 10460 if (!ISREMOVABLE(un) || 10461 un->un_f_dvdram_writable_device == TRUE) { 10462 #else 10463 if (un->un_f_dvdram_writable_device == TRUE) { 10464 #endif 10465 mutex_exit(SD_MUTEX(un)); 10466 if (sd_pm_entry(un) == DDI_SUCCESS) { 10467 rval = 10468 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10469 NULL); 10470 /* ignore error if not supported */ 10471 if (rval == ENOTSUP) { 10472 rval = 0; 10473 } else if (rval != 0) { 10474 rval = EIO; 10475 } 10476 sd_pm_exit(un); 10477 } else { 10478 rval = EIO; 10479 } 10480 mutex_enter(SD_MUTEX(un)); 10481 } 10482 10483 /* 10484 * For removable media devices, send an ALLOW MEDIA 10485 * REMOVAL command, but don't get upset if it fails. 10486 * Also invalidate the geometry. We need to raise 10487 * the power of the drive before we can call 10488 * sd_send_scsi_DOORLOCK() 10489 */ 10490 if (ISREMOVABLE(un)) { 10491 mutex_exit(SD_MUTEX(un)); 10492 if (sd_pm_entry(un) == DDI_SUCCESS) { 10493 rval = sd_send_scsi_DOORLOCK(un, 10494 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10495 10496 sd_pm_exit(un); 10497 if (ISCD(un) && (rval != 0) && 10498 (nodelay != 0)) { 10499 rval = ENXIO; 10500 } 10501 } else { 10502 rval = EIO; 10503 } 10504 mutex_enter(SD_MUTEX(un)); 10505 10506 sr_ejected(un); 10507 /* 10508 * Destroy the cache (if it exists) which was 10509 * allocated for the write maps since this is 10510 * the last close for this media. 10511 */ 10512 if (un->un_wm_cache) { 10513 /* 10514 * Check if there are pending commands. 10515 * and if there are give a warning and 10516 * do not destroy the cache. 10517 */ 10518 if (un->un_ncmds_in_driver > 0) { 10519 scsi_log(SD_DEVINFO(un), 10520 sd_label, CE_WARN, 10521 "Unable to clean up memory " 10522 "because of pending I/O\n"); 10523 } else { 10524 kmem_cache_destroy( 10525 un->un_wm_cache); 10526 un->un_wm_cache = NULL; 10527 } 10528 } 10529 } 10530 } 10531 } 10532 10533 mutex_exit(SD_MUTEX(un)); 10534 sema_v(&un->un_semoclose); 10535 10536 if (otyp == OTYP_LYR) { 10537 mutex_enter(&sd_detach_mutex); 10538 /* 10539 * The detach routine may run when the layer count 10540 * drops to zero. 10541 */ 10542 un->un_layer_count--; 10543 mutex_exit(&sd_detach_mutex); 10544 } 10545 10546 return (rval); 10547 } 10548 10549 10550 /* 10551 * Function: sd_ready_and_valid 10552 * 10553 * Description: Test if device is ready and has a valid geometry. 10554 * 10555 * Arguments: dev - device number 10556 * un - driver soft state (unit) structure 10557 * 10558 * Return Code: SD_READY_VALID ready and valid label 10559 * SD_READY_NOT_VALID ready, geom ops never applicable 10560 * SD_NOT_READY_VALID not ready, no label 10561 * 10562 * Context: Never called at interrupt context. 10563 */ 10564 10565 static int 10566 sd_ready_and_valid(struct sd_lun *un) 10567 { 10568 struct sd_errstats *stp; 10569 uint64_t capacity; 10570 uint_t lbasize; 10571 int rval = SD_READY_VALID; 10572 char name_str[48]; 10573 10574 ASSERT(un != NULL); 10575 ASSERT(!mutex_owned(SD_MUTEX(un))); 10576 10577 mutex_enter(SD_MUTEX(un)); 10578 if (ISREMOVABLE(un)) { 10579 mutex_exit(SD_MUTEX(un)); 10580 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 10581 rval = SD_NOT_READY_VALID; 10582 mutex_enter(SD_MUTEX(un)); 10583 goto done; 10584 } 10585 10586 mutex_enter(SD_MUTEX(un)); 10587 if ((un->un_f_geometry_is_valid == FALSE) || 10588 (un->un_f_blockcount_is_valid == FALSE) || 10589 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10590 10591 /* capacity has to be read every open. */ 10592 mutex_exit(SD_MUTEX(un)); 10593 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 10594 &lbasize, SD_PATH_DIRECT) != 0) { 10595 mutex_enter(SD_MUTEX(un)); 10596 un->un_f_geometry_is_valid = FALSE; 10597 rval = SD_NOT_READY_VALID; 10598 goto done; 10599 } else { 10600 mutex_enter(SD_MUTEX(un)); 10601 sd_update_block_info(un, lbasize, capacity); 10602 } 10603 } 10604 10605 /* 10606 * If this is a non 512 block device, allocate space for 10607 * the wmap cache. This is being done here since every time 10608 * a media is changed this routine will be called and the 10609 * block size is a function of media rather than device. 10610 */ 10611 if (NOT_DEVBSIZE(un)) { 10612 if (!(un->un_wm_cache)) { 10613 (void) snprintf(name_str, sizeof (name_str), 10614 "%s%d_cache", 10615 ddi_driver_name(SD_DEVINFO(un)), 10616 ddi_get_instance(SD_DEVINFO(un))); 10617 un->un_wm_cache = kmem_cache_create( 10618 name_str, sizeof (struct sd_w_map), 10619 8, sd_wm_cache_constructor, 10620 sd_wm_cache_destructor, NULL, 10621 (void *)un, NULL, 0); 10622 if (!(un->un_wm_cache)) { 10623 rval = ENOMEM; 10624 goto done; 10625 } 10626 } 10627 } 10628 10629 /* 10630 * Check if the media in the device is writable or not. 10631 */ 10632 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 10633 sd_check_for_writable_cd(un); 10634 } 10635 10636 } else { 10637 /* 10638 * Do a test unit ready to clear any unit attention from non-cd 10639 * devices. 10640 */ 10641 mutex_exit(SD_MUTEX(un)); 10642 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10643 mutex_enter(SD_MUTEX(un)); 10644 } 10645 10646 10647 if (un->un_state == SD_STATE_NORMAL) { 10648 /* 10649 * If the target is not yet ready here (defined by a TUR 10650 * failure), invalidate the geometry and print an 'offline' 10651 * message. This is a legacy message, as the state of the 10652 * target is not actually changed to SD_STATE_OFFLINE. 10653 * 10654 * If the TUR fails for EACCES (Reservation Conflict), it 10655 * means there actually is nothing wrong with the target that 10656 * would require invalidating the geometry, so continue in 10657 * that case as if the TUR was successful. 10658 */ 10659 int err; 10660 10661 mutex_exit(SD_MUTEX(un)); 10662 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 10663 mutex_enter(SD_MUTEX(un)); 10664 10665 if ((err != 0) && (err != EACCES)) { 10666 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10667 "offline\n"); 10668 un->un_f_geometry_is_valid = FALSE; 10669 rval = SD_NOT_READY_VALID; 10670 goto done; 10671 } 10672 } 10673 10674 if (un->un_f_format_in_progress == FALSE) { 10675 /* 10676 * Note: sd_validate_geometry may return TRUE, but that does 10677 * not necessarily mean un_f_geometry_is_valid == TRUE! 10678 */ 10679 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 10680 if (rval == ENOTSUP) { 10681 if (un->un_f_geometry_is_valid == TRUE) 10682 rval = 0; 10683 else { 10684 rval = SD_READY_NOT_VALID; 10685 goto done; 10686 } 10687 } 10688 if (rval != 0) { 10689 /* 10690 * We don't check the validity of geometry for 10691 * CDROMs. Also we assume we have a good label 10692 * even if sd_validate_geometry returned ENOMEM. 10693 */ 10694 if (!ISCD(un) && rval != ENOMEM) { 10695 rval = SD_NOT_READY_VALID; 10696 goto done; 10697 } 10698 } 10699 } 10700 10701 #ifdef DOESNTWORK /* on eliteII, see 1118607 */ 10702 /* 10703 * check to see if this disk is write protected, if it is and we have 10704 * not set read-only, then fail 10705 */ 10706 if ((flag & FWRITE) && (sr_check_wp(dev))) { 10707 New_state(un, SD_STATE_CLOSED); 10708 goto done; 10709 } 10710 #endif 10711 10712 /* 10713 * If this is a removable media device, try and send 10714 * a PREVENT MEDIA REMOVAL command, but don't get upset 10715 * if it fails. For a CD, however, it is an error 10716 */ 10717 if (ISREMOVABLE(un)) { 10718 mutex_exit(SD_MUTEX(un)); 10719 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 10720 SD_PATH_DIRECT) != 0) && ISCD(un)) { 10721 rval = SD_NOT_READY_VALID; 10722 mutex_enter(SD_MUTEX(un)); 10723 goto done; 10724 } 10725 mutex_enter(SD_MUTEX(un)); 10726 } 10727 10728 /* The state has changed, inform the media watch routines */ 10729 un->un_mediastate = DKIO_INSERTED; 10730 cv_broadcast(&un->un_state_cv); 10731 rval = SD_READY_VALID; 10732 10733 done: 10734 10735 /* 10736 * Initialize the capacity kstat value, if no media previously 10737 * (capacity kstat is 0) and a media has been inserted 10738 * (un_blockcount > 0). 10739 * This is a more generic way then checking for ISREMOVABLE. 10740 */ 10741 if (un->un_errstats != NULL) { 10742 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10743 if ((stp->sd_capacity.value.ui64 == 0) && 10744 (un->un_f_blockcount_is_valid == TRUE)) { 10745 stp->sd_capacity.value.ui64 = 10746 (uint64_t)((uint64_t)un->un_blockcount * 10747 un->un_sys_blocksize); 10748 } 10749 } 10750 10751 mutex_exit(SD_MUTEX(un)); 10752 return (rval); 10753 } 10754 10755 10756 /* 10757 * Function: sdmin 10758 * 10759 * Description: Routine to limit the size of a data transfer. Used in 10760 * conjunction with physio(9F). 10761 * 10762 * Arguments: bp - pointer to the indicated buf(9S) struct. 10763 * 10764 * Context: Kernel thread context. 10765 */ 10766 10767 static void 10768 sdmin(struct buf *bp) 10769 { 10770 struct sd_lun *un; 10771 int instance; 10772 10773 instance = SDUNIT(bp->b_edev); 10774 10775 un = ddi_get_soft_state(sd_state, instance); 10776 ASSERT(un != NULL); 10777 10778 if (bp->b_bcount > un->un_max_xfer_size) { 10779 bp->b_bcount = un->un_max_xfer_size; 10780 } 10781 } 10782 10783 10784 /* 10785 * Function: sdread 10786 * 10787 * Description: Driver's read(9e) entry point function. 10788 * 10789 * Arguments: dev - device number 10790 * uio - structure pointer describing where data is to be stored 10791 * in user's space 10792 * cred_p - user credential pointer 10793 * 10794 * Return Code: ENXIO 10795 * EIO 10796 * EINVAL 10797 * value returned by physio 10798 * 10799 * Context: Kernel thread context. 10800 */ 10801 /* ARGSUSED */ 10802 static int 10803 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10804 { 10805 struct sd_lun *un = NULL; 10806 int secmask; 10807 int err; 10808 10809 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10810 return (ENXIO); 10811 } 10812 10813 ASSERT(!mutex_owned(SD_MUTEX(un))); 10814 10815 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10816 mutex_enter(SD_MUTEX(un)); 10817 /* 10818 * Because the call to sd_ready_and_valid will issue I/O we 10819 * must wait here if either the device is suspended or 10820 * if it's power level is changing. 10821 */ 10822 while ((un->un_state == SD_STATE_SUSPENDED) || 10823 (un->un_state == SD_STATE_PM_CHANGING)) { 10824 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10825 } 10826 un->un_ncmds_in_driver++; 10827 mutex_exit(SD_MUTEX(un)); 10828 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10829 mutex_enter(SD_MUTEX(un)); 10830 un->un_ncmds_in_driver--; 10831 ASSERT(un->un_ncmds_in_driver >= 0); 10832 mutex_exit(SD_MUTEX(un)); 10833 return (EIO); 10834 } 10835 mutex_enter(SD_MUTEX(un)); 10836 un->un_ncmds_in_driver--; 10837 ASSERT(un->un_ncmds_in_driver >= 0); 10838 mutex_exit(SD_MUTEX(un)); 10839 } 10840 10841 /* 10842 * Read requests are restricted to multiples of the system block size. 10843 */ 10844 secmask = un->un_sys_blocksize - 1; 10845 10846 if (uio->uio_loffset & ((offset_t)(secmask))) { 10847 SD_ERROR(SD_LOG_READ_WRITE, un, 10848 "sdread: file offset not modulo %d\n", 10849 un->un_sys_blocksize); 10850 err = EINVAL; 10851 } else if (uio->uio_iov->iov_len & (secmask)) { 10852 SD_ERROR(SD_LOG_READ_WRITE, un, 10853 "sdread: transfer length not modulo %d\n", 10854 un->un_sys_blocksize); 10855 err = EINVAL; 10856 } else { 10857 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10858 } 10859 return (err); 10860 } 10861 10862 10863 /* 10864 * Function: sdwrite 10865 * 10866 * Description: Driver's write(9e) entry point function. 10867 * 10868 * Arguments: dev - device number 10869 * uio - structure pointer describing where data is stored in 10870 * user's space 10871 * cred_p - user credential pointer 10872 * 10873 * Return Code: ENXIO 10874 * EIO 10875 * EINVAL 10876 * value returned by physio 10877 * 10878 * Context: Kernel thread context. 10879 */ 10880 /* ARGSUSED */ 10881 static int 10882 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10883 { 10884 struct sd_lun *un = NULL; 10885 int secmask; 10886 int err; 10887 10888 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10889 return (ENXIO); 10890 } 10891 10892 ASSERT(!mutex_owned(SD_MUTEX(un))); 10893 10894 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10895 mutex_enter(SD_MUTEX(un)); 10896 /* 10897 * Because the call to sd_ready_and_valid will issue I/O we 10898 * must wait here if either the device is suspended or 10899 * if it's power level is changing. 10900 */ 10901 while ((un->un_state == SD_STATE_SUSPENDED) || 10902 (un->un_state == SD_STATE_PM_CHANGING)) { 10903 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10904 } 10905 un->un_ncmds_in_driver++; 10906 mutex_exit(SD_MUTEX(un)); 10907 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10908 mutex_enter(SD_MUTEX(un)); 10909 un->un_ncmds_in_driver--; 10910 ASSERT(un->un_ncmds_in_driver >= 0); 10911 mutex_exit(SD_MUTEX(un)); 10912 return (EIO); 10913 } 10914 mutex_enter(SD_MUTEX(un)); 10915 un->un_ncmds_in_driver--; 10916 ASSERT(un->un_ncmds_in_driver >= 0); 10917 mutex_exit(SD_MUTEX(un)); 10918 } 10919 10920 /* 10921 * Write requests are restricted to multiples of the system block size. 10922 */ 10923 secmask = un->un_sys_blocksize - 1; 10924 10925 if (uio->uio_loffset & ((offset_t)(secmask))) { 10926 SD_ERROR(SD_LOG_READ_WRITE, un, 10927 "sdwrite: file offset not modulo %d\n", 10928 un->un_sys_blocksize); 10929 err = EINVAL; 10930 } else if (uio->uio_iov->iov_len & (secmask)) { 10931 SD_ERROR(SD_LOG_READ_WRITE, un, 10932 "sdwrite: transfer length not modulo %d\n", 10933 un->un_sys_blocksize); 10934 err = EINVAL; 10935 } else { 10936 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10937 } 10938 return (err); 10939 } 10940 10941 10942 /* 10943 * Function: sdaread 10944 * 10945 * Description: Driver's aread(9e) entry point function. 10946 * 10947 * Arguments: dev - device number 10948 * aio - structure pointer describing where data is to be stored 10949 * cred_p - user credential pointer 10950 * 10951 * Return Code: ENXIO 10952 * EIO 10953 * EINVAL 10954 * value returned by aphysio 10955 * 10956 * Context: Kernel thread context. 10957 */ 10958 /* ARGSUSED */ 10959 static int 10960 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10961 { 10962 struct sd_lun *un = NULL; 10963 struct uio *uio = aio->aio_uio; 10964 int secmask; 10965 int err; 10966 10967 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10968 return (ENXIO); 10969 } 10970 10971 ASSERT(!mutex_owned(SD_MUTEX(un))); 10972 10973 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 10974 mutex_enter(SD_MUTEX(un)); 10975 /* 10976 * Because the call to sd_ready_and_valid will issue I/O we 10977 * must wait here if either the device is suspended or 10978 * if it's power level is changing. 10979 */ 10980 while ((un->un_state == SD_STATE_SUSPENDED) || 10981 (un->un_state == SD_STATE_PM_CHANGING)) { 10982 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10983 } 10984 un->un_ncmds_in_driver++; 10985 mutex_exit(SD_MUTEX(un)); 10986 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10987 mutex_enter(SD_MUTEX(un)); 10988 un->un_ncmds_in_driver--; 10989 ASSERT(un->un_ncmds_in_driver >= 0); 10990 mutex_exit(SD_MUTEX(un)); 10991 return (EIO); 10992 } 10993 mutex_enter(SD_MUTEX(un)); 10994 un->un_ncmds_in_driver--; 10995 ASSERT(un->un_ncmds_in_driver >= 0); 10996 mutex_exit(SD_MUTEX(un)); 10997 } 10998 10999 /* 11000 * Read requests are restricted to multiples of the system block size. 11001 */ 11002 secmask = un->un_sys_blocksize - 1; 11003 11004 if (uio->uio_loffset & ((offset_t)(secmask))) { 11005 SD_ERROR(SD_LOG_READ_WRITE, un, 11006 "sdaread: file offset not modulo %d\n", 11007 un->un_sys_blocksize); 11008 err = EINVAL; 11009 } else if (uio->uio_iov->iov_len & (secmask)) { 11010 SD_ERROR(SD_LOG_READ_WRITE, un, 11011 "sdaread: transfer length not modulo %d\n", 11012 un->un_sys_blocksize); 11013 err = EINVAL; 11014 } else { 11015 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11016 } 11017 return (err); 11018 } 11019 11020 11021 /* 11022 * Function: sdawrite 11023 * 11024 * Description: Driver's awrite(9e) entry point function. 11025 * 11026 * Arguments: dev - device number 11027 * aio - structure pointer describing where data is stored 11028 * cred_p - user credential pointer 11029 * 11030 * Return Code: ENXIO 11031 * EIO 11032 * EINVAL 11033 * value returned by aphysio 11034 * 11035 * Context: Kernel thread context. 11036 */ 11037 /* ARGSUSED */ 11038 static int 11039 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11040 { 11041 struct sd_lun *un = NULL; 11042 struct uio *uio = aio->aio_uio; 11043 int secmask; 11044 int err; 11045 11046 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11047 return (ENXIO); 11048 } 11049 11050 ASSERT(!mutex_owned(SD_MUTEX(un))); 11051 11052 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11053 mutex_enter(SD_MUTEX(un)); 11054 /* 11055 * Because the call to sd_ready_and_valid will issue I/O we 11056 * must wait here if either the device is suspended or 11057 * if it's power level is changing. 11058 */ 11059 while ((un->un_state == SD_STATE_SUSPENDED) || 11060 (un->un_state == SD_STATE_PM_CHANGING)) { 11061 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11062 } 11063 un->un_ncmds_in_driver++; 11064 mutex_exit(SD_MUTEX(un)); 11065 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11066 mutex_enter(SD_MUTEX(un)); 11067 un->un_ncmds_in_driver--; 11068 ASSERT(un->un_ncmds_in_driver >= 0); 11069 mutex_exit(SD_MUTEX(un)); 11070 return (EIO); 11071 } 11072 mutex_enter(SD_MUTEX(un)); 11073 un->un_ncmds_in_driver--; 11074 ASSERT(un->un_ncmds_in_driver >= 0); 11075 mutex_exit(SD_MUTEX(un)); 11076 } 11077 11078 /* 11079 * Write requests are restricted to multiples of the system block size. 11080 */ 11081 secmask = un->un_sys_blocksize - 1; 11082 11083 if (uio->uio_loffset & ((offset_t)(secmask))) { 11084 SD_ERROR(SD_LOG_READ_WRITE, un, 11085 "sdawrite: file offset not modulo %d\n", 11086 un->un_sys_blocksize); 11087 err = EINVAL; 11088 } else if (uio->uio_iov->iov_len & (secmask)) { 11089 SD_ERROR(SD_LOG_READ_WRITE, un, 11090 "sdawrite: transfer length not modulo %d\n", 11091 un->un_sys_blocksize); 11092 err = EINVAL; 11093 } else { 11094 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11095 } 11096 return (err); 11097 } 11098 11099 11100 11101 11102 11103 /* 11104 * Driver IO processing follows the following sequence: 11105 * 11106 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11107 * | | ^ 11108 * v v | 11109 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11110 * | | | | 11111 * v | | | 11112 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11113 * | | ^ ^ 11114 * v v | | 11115 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11116 * | | | | 11117 * +---+ | +------------+ +-------+ 11118 * | | | | 11119 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11120 * | v | | 11121 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11122 * | | ^ | 11123 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11124 * | v | | 11125 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11126 * | | ^ | 11127 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11128 * | v | | 11129 * | sd_checksum_iostart() sd_checksum_iodone() | 11130 * | | ^ | 11131 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11132 * | v | | 11133 * | sd_pm_iostart() sd_pm_iodone() | 11134 * | | ^ | 11135 * | | | | 11136 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11137 * | ^ 11138 * v | 11139 * sd_core_iostart() | 11140 * | | 11141 * | +------>(*destroypkt)() 11142 * +-> sd_start_cmds() <-+ | | 11143 * | | | v 11144 * | | | scsi_destroy_pkt(9F) 11145 * | | | 11146 * +->(*initpkt)() +- sdintr() 11147 * | | | | 11148 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11149 * | +-> scsi_setup_cdb(9F) | 11150 * | | 11151 * +--> scsi_transport(9F) | 11152 * | | 11153 * +----> SCSA ---->+ 11154 * 11155 * 11156 * This code is based upon the following presumtions: 11157 * 11158 * - iostart and iodone functions operate on buf(9S) structures. These 11159 * functions perform the necessary operations on the buf(9S) and pass 11160 * them along to the next function in the chain by using the macros 11161 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11162 * (for iodone side functions). 11163 * 11164 * - The iostart side functions may sleep. The iodone side functions 11165 * are called under interrupt context and may NOT sleep. Therefore 11166 * iodone side functions also may not call iostart side functions. 11167 * (NOTE: iostart side functions should NOT sleep for memory, as 11168 * this could result in deadlock.) 11169 * 11170 * - An iostart side function may call its corresponding iodone side 11171 * function directly (if necessary). 11172 * 11173 * - In the event of an error, an iostart side function can return a buf(9S) 11174 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11175 * b_error in the usual way of course). 11176 * 11177 * - The taskq mechanism may be used by the iodone side functions to dispatch 11178 * requests to the iostart side functions. The iostart side functions in 11179 * this case would be called under the context of a taskq thread, so it's 11180 * OK for them to block/sleep/spin in this case. 11181 * 11182 * - iostart side functions may allocate "shadow" buf(9S) structs and 11183 * pass them along to the next function in the chain. The corresponding 11184 * iodone side functions must coalesce the "shadow" bufs and return 11185 * the "original" buf to the next higher layer. 11186 * 11187 * - The b_private field of the buf(9S) struct holds a pointer to 11188 * an sd_xbuf struct, which contains information needed to 11189 * construct the scsi_pkt for the command. 11190 * 11191 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11192 * layer must acquire & release the SD_MUTEX(un) as needed. 11193 */ 11194 11195 11196 /* 11197 * Create taskq for all targets in the system. This is created at 11198 * _init(9E) and destroyed at _fini(9E). 11199 * 11200 * Note: here we set the minalloc to a reasonably high number to ensure that 11201 * we will have an adequate supply of task entries available at interrupt time. 11202 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11203 * sd_create_taskq(). Since we do not want to sleep for allocations at 11204 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11205 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11206 * requests any one instant in time. 11207 */ 11208 #define SD_TASKQ_NUMTHREADS 8 11209 #define SD_TASKQ_MINALLOC 256 11210 #define SD_TASKQ_MAXALLOC 256 11211 11212 static taskq_t *sd_tq = NULL; 11213 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11214 11215 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11216 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11217 11218 /* 11219 * The following task queue is being created for the write part of 11220 * read-modify-write of non-512 block size devices. 11221 * Limit the number of threads to 1 for now. This number has been choosen 11222 * considering the fact that it applies only to dvd ram drives/MO drives 11223 * currently. Performance for which is not main criteria at this stage. 11224 * Note: It needs to be explored if we can use a single taskq in future 11225 */ 11226 #define SD_WMR_TASKQ_NUMTHREADS 1 11227 static taskq_t *sd_wmr_tq = NULL; 11228 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11229 11230 /* 11231 * Function: sd_taskq_create 11232 * 11233 * Description: Create taskq thread(s) and preallocate task entries 11234 * 11235 * Return Code: Returns a pointer to the allocated taskq_t. 11236 * 11237 * Context: Can sleep. Requires blockable context. 11238 * 11239 * Notes: - The taskq() facility currently is NOT part of the DDI. 11240 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11241 * - taskq_create() will block for memory, also it will panic 11242 * if it cannot create the requested number of threads. 11243 * - Currently taskq_create() creates threads that cannot be 11244 * swapped. 11245 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11246 * supply of taskq entries at interrupt time (ie, so that we 11247 * do not have to sleep for memory) 11248 */ 11249 11250 static void 11251 sd_taskq_create(void) 11252 { 11253 char taskq_name[TASKQ_NAMELEN]; 11254 11255 ASSERT(sd_tq == NULL); 11256 ASSERT(sd_wmr_tq == NULL); 11257 11258 (void) snprintf(taskq_name, sizeof (taskq_name), 11259 "%s_drv_taskq", sd_label); 11260 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11261 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11262 TASKQ_PREPOPULATE)); 11263 11264 (void) snprintf(taskq_name, sizeof (taskq_name), 11265 "%s_rmw_taskq", sd_label); 11266 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11267 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11268 TASKQ_PREPOPULATE)); 11269 } 11270 11271 11272 /* 11273 * Function: sd_taskq_delete 11274 * 11275 * Description: Complementary cleanup routine for sd_taskq_create(). 11276 * 11277 * Context: Kernel thread context. 11278 */ 11279 11280 static void 11281 sd_taskq_delete(void) 11282 { 11283 ASSERT(sd_tq != NULL); 11284 ASSERT(sd_wmr_tq != NULL); 11285 taskq_destroy(sd_tq); 11286 taskq_destroy(sd_wmr_tq); 11287 sd_tq = NULL; 11288 sd_wmr_tq = NULL; 11289 } 11290 11291 11292 /* 11293 * Function: sdstrategy 11294 * 11295 * Description: Driver's strategy (9E) entry point function. 11296 * 11297 * Arguments: bp - pointer to buf(9S) 11298 * 11299 * Return Code: Always returns zero 11300 * 11301 * Context: Kernel thread context. 11302 */ 11303 11304 static int 11305 sdstrategy(struct buf *bp) 11306 { 11307 struct sd_lun *un; 11308 11309 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11310 if (un == NULL) { 11311 bioerror(bp, EIO); 11312 bp->b_resid = bp->b_bcount; 11313 biodone(bp); 11314 return (0); 11315 } 11316 /* As was done in the past, fail new cmds. if state is dumping. */ 11317 if (un->un_state == SD_STATE_DUMPING) { 11318 bioerror(bp, ENXIO); 11319 bp->b_resid = bp->b_bcount; 11320 biodone(bp); 11321 return (0); 11322 } 11323 11324 ASSERT(!mutex_owned(SD_MUTEX(un))); 11325 11326 /* 11327 * Commands may sneak in while we released the mutex in 11328 * DDI_SUSPEND, we should block new commands. However, old 11329 * commands that are still in the driver at this point should 11330 * still be allowed to drain. 11331 */ 11332 mutex_enter(SD_MUTEX(un)); 11333 /* 11334 * Must wait here if either the device is suspended or 11335 * if it's power level is changing. 11336 */ 11337 while ((un->un_state == SD_STATE_SUSPENDED) || 11338 (un->un_state == SD_STATE_PM_CHANGING)) { 11339 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11340 } 11341 11342 un->un_ncmds_in_driver++; 11343 11344 /* 11345 * atapi: Since we are running the CD for now in PIO mode we need to 11346 * call bp_mapin here to avoid bp_mapin called interrupt context under 11347 * the HBA's init_pkt routine. 11348 */ 11349 if (un->un_f_cfg_is_atapi == TRUE) { 11350 mutex_exit(SD_MUTEX(un)); 11351 bp_mapin(bp); 11352 mutex_enter(SD_MUTEX(un)); 11353 } 11354 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11355 un->un_ncmds_in_driver); 11356 11357 mutex_exit(SD_MUTEX(un)); 11358 11359 /* 11360 * This will (eventually) allocate the sd_xbuf area and 11361 * call sd_xbuf_strategy(). We just want to return the 11362 * result of ddi_xbuf_qstrategy so that we have an opt- 11363 * imized tail call which saves us a stack frame. 11364 */ 11365 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11366 } 11367 11368 11369 /* 11370 * Function: sd_xbuf_strategy 11371 * 11372 * Description: Function for initiating IO operations via the 11373 * ddi_xbuf_qstrategy() mechanism. 11374 * 11375 * Context: Kernel thread context. 11376 */ 11377 11378 static void 11379 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11380 { 11381 struct sd_lun *un = arg; 11382 11383 ASSERT(bp != NULL); 11384 ASSERT(xp != NULL); 11385 ASSERT(un != NULL); 11386 ASSERT(!mutex_owned(SD_MUTEX(un))); 11387 11388 /* 11389 * Initialize the fields in the xbuf and save a pointer to the 11390 * xbuf in bp->b_private. 11391 */ 11392 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11393 11394 /* Send the buf down the iostart chain */ 11395 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11396 } 11397 11398 11399 /* 11400 * Function: sd_xbuf_init 11401 * 11402 * Description: Prepare the given sd_xbuf struct for use. 11403 * 11404 * Arguments: un - ptr to softstate 11405 * bp - ptr to associated buf(9S) 11406 * xp - ptr to associated sd_xbuf 11407 * chain_type - IO chain type to use: 11408 * SD_CHAIN_NULL 11409 * SD_CHAIN_BUFIO 11410 * SD_CHAIN_USCSI 11411 * SD_CHAIN_DIRECT 11412 * SD_CHAIN_DIRECT_PRIORITY 11413 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11414 * initialization; may be NULL if none. 11415 * 11416 * Context: Kernel thread context 11417 */ 11418 11419 static void 11420 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11421 uchar_t chain_type, void *pktinfop) 11422 { 11423 int index; 11424 11425 ASSERT(un != NULL); 11426 ASSERT(bp != NULL); 11427 ASSERT(xp != NULL); 11428 11429 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11430 bp, chain_type); 11431 11432 xp->xb_un = un; 11433 xp->xb_pktp = NULL; 11434 xp->xb_pktinfo = pktinfop; 11435 xp->xb_private = bp->b_private; 11436 xp->xb_blkno = (daddr_t)bp->b_blkno; 11437 11438 /* 11439 * Set up the iostart and iodone chain indexes in the xbuf, based 11440 * upon the specified chain type to use. 11441 */ 11442 switch (chain_type) { 11443 case SD_CHAIN_NULL: 11444 /* 11445 * Fall thru to just use the values for the buf type, even 11446 * tho for the NULL chain these values will never be used. 11447 */ 11448 /* FALLTHRU */ 11449 case SD_CHAIN_BUFIO: 11450 index = un->un_buf_chain_type; 11451 break; 11452 case SD_CHAIN_USCSI: 11453 index = un->un_uscsi_chain_type; 11454 break; 11455 case SD_CHAIN_DIRECT: 11456 index = un->un_direct_chain_type; 11457 break; 11458 case SD_CHAIN_DIRECT_PRIORITY: 11459 index = un->un_priority_chain_type; 11460 break; 11461 default: 11462 /* We're really broken if we ever get here... */ 11463 panic("sd_xbuf_init: illegal chain type!"); 11464 /*NOTREACHED*/ 11465 } 11466 11467 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11468 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11469 11470 /* 11471 * It might be a bit easier to simply bzero the entire xbuf above, 11472 * but it turns out that since we init a fair number of members anyway, 11473 * we save a fair number cycles by doing explicit assignment of zero. 11474 */ 11475 xp->xb_pkt_flags = 0; 11476 xp->xb_dma_resid = 0; 11477 xp->xb_retry_count = 0; 11478 xp->xb_victim_retry_count = 0; 11479 xp->xb_ua_retry_count = 0; 11480 xp->xb_sense_bp = NULL; 11481 xp->xb_sense_status = 0; 11482 xp->xb_sense_state = 0; 11483 xp->xb_sense_resid = 0; 11484 11485 bp->b_private = xp; 11486 bp->b_flags &= ~(B_DONE | B_ERROR); 11487 bp->b_resid = 0; 11488 bp->av_forw = NULL; 11489 bp->av_back = NULL; 11490 bioerror(bp, 0); 11491 11492 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11493 } 11494 11495 11496 /* 11497 * Function: sd_uscsi_strategy 11498 * 11499 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11500 * 11501 * Arguments: bp - buf struct ptr 11502 * 11503 * Return Code: Always returns 0 11504 * 11505 * Context: Kernel thread context 11506 */ 11507 11508 static int 11509 sd_uscsi_strategy(struct buf *bp) 11510 { 11511 struct sd_lun *un; 11512 struct sd_uscsi_info *uip; 11513 struct sd_xbuf *xp; 11514 uchar_t chain_type; 11515 11516 ASSERT(bp != NULL); 11517 11518 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11519 if (un == NULL) { 11520 bioerror(bp, EIO); 11521 bp->b_resid = bp->b_bcount; 11522 biodone(bp); 11523 return (0); 11524 } 11525 11526 ASSERT(!mutex_owned(SD_MUTEX(un))); 11527 11528 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11529 11530 mutex_enter(SD_MUTEX(un)); 11531 /* 11532 * atapi: Since we are running the CD for now in PIO mode we need to 11533 * call bp_mapin here to avoid bp_mapin called interrupt context under 11534 * the HBA's init_pkt routine. 11535 */ 11536 if (un->un_f_cfg_is_atapi == TRUE) { 11537 mutex_exit(SD_MUTEX(un)); 11538 bp_mapin(bp); 11539 mutex_enter(SD_MUTEX(un)); 11540 } 11541 un->un_ncmds_in_driver++; 11542 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11543 un->un_ncmds_in_driver); 11544 mutex_exit(SD_MUTEX(un)); 11545 11546 /* 11547 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11548 */ 11549 ASSERT(bp->b_private != NULL); 11550 uip = (struct sd_uscsi_info *)bp->b_private; 11551 11552 switch (uip->ui_flags) { 11553 case SD_PATH_DIRECT: 11554 chain_type = SD_CHAIN_DIRECT; 11555 break; 11556 case SD_PATH_DIRECT_PRIORITY: 11557 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11558 break; 11559 default: 11560 chain_type = SD_CHAIN_USCSI; 11561 break; 11562 } 11563 11564 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 11565 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11566 11567 /* Use the index obtained within xbuf_init */ 11568 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11569 11570 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11571 11572 return (0); 11573 } 11574 11575 11576 /* 11577 * These routines perform raw i/o operations. 11578 */ 11579 /*ARGSUSED*/ 11580 static void 11581 sduscsimin(struct buf *bp) 11582 { 11583 /* 11584 * do not break up because the CDB count would then 11585 * be incorrect and data underruns would result (incomplete 11586 * read/writes which would be retried and then failed, see 11587 * sdintr(). 11588 */ 11589 } 11590 11591 11592 11593 /* 11594 * Function: sd_send_scsi_cmd 11595 * 11596 * Description: Runs a USCSI command for user (when called thru sdioctl), 11597 * or for the driver 11598 * 11599 * Arguments: dev - the dev_t for the device 11600 * incmd - ptr to a valid uscsi_cmd struct 11601 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 11602 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11603 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 11604 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11605 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11606 * to use the USCSI "direct" chain and bypass the normal 11607 * command waitq. 11608 * 11609 * Return Code: 0 - successful completion of the given command 11610 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 11611 * ENXIO - soft state not found for specified dev 11612 * EINVAL 11613 * EFAULT - copyin/copyout error 11614 * return code of biowait(9F) or physio(9F): 11615 * EIO - IO error, caller may check incmd->uscsi_status 11616 * ENXIO 11617 * EACCES - reservation conflict 11618 * 11619 * Context: Waits for command to complete. Can sleep. 11620 */ 11621 11622 static int 11623 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 11624 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 11625 int path_flag) 11626 { 11627 struct sd_uscsi_info *uip; 11628 struct uscsi_cmd *uscmd; 11629 struct sd_lun *un; 11630 struct buf *bp; 11631 int rval; 11632 int flags; 11633 11634 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11635 if (un == NULL) { 11636 return (ENXIO); 11637 } 11638 11639 ASSERT(!mutex_owned(SD_MUTEX(un))); 11640 11641 #ifdef SDDEBUG 11642 switch (dataspace) { 11643 case UIO_USERSPACE: 11644 SD_TRACE(SD_LOG_IO, un, 11645 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 11646 break; 11647 case UIO_SYSSPACE: 11648 SD_TRACE(SD_LOG_IO, un, 11649 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 11650 break; 11651 default: 11652 SD_TRACE(SD_LOG_IO, un, 11653 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 11654 break; 11655 } 11656 #endif 11657 11658 /* 11659 * Perform resets directly; no need to generate a command to do it. 11660 */ 11661 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 11662 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 11663 RESET_ALL : RESET_TARGET; 11664 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 11665 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 11666 /* Reset attempt was unsuccessful */ 11667 SD_TRACE(SD_LOG_IO, un, 11668 "sd_send_scsi_cmd: reset: failure\n"); 11669 return (EIO); 11670 } 11671 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 11672 return (0); 11673 } 11674 11675 /* Perfunctory sanity check... */ 11676 if (incmd->uscsi_cdblen <= 0) { 11677 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11678 "invalid uscsi_cdblen, returning EINVAL\n"); 11679 return (EINVAL); 11680 } 11681 11682 /* 11683 * In order to not worry about where the uscsi structure came from 11684 * (or where the cdb it points to came from) we're going to make 11685 * kmem_alloc'd copies of them here. This will also allow reference 11686 * to the data they contain long after this process has gone to 11687 * sleep and its kernel stack has been unmapped, etc. 11688 * 11689 * First get some memory for the uscsi_cmd struct and copy the 11690 * contents of the given uscsi_cmd struct into it. 11691 */ 11692 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11693 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 11694 11695 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 11696 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 11697 11698 /* 11699 * Now get some space for the CDB, and copy the given CDB into 11700 * it. Use ddi_copyin() in case the data is in user space. 11701 */ 11702 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 11703 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 11704 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 11705 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 11706 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 11707 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11708 return (EFAULT); 11709 } 11710 11711 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 11712 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 11713 11714 bp = getrbuf(KM_SLEEP); 11715 11716 /* 11717 * Allocate an sd_uscsi_info struct and fill it with the info 11718 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11719 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11720 * since we allocate the buf here in this function, we do not 11721 * need to preserve the prior contents of b_private. 11722 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11723 */ 11724 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11725 uip->ui_flags = path_flag; 11726 uip->ui_cmdp = uscmd; 11727 bp->b_private = uip; 11728 11729 /* 11730 * Initialize Request Sense buffering, if requested. 11731 */ 11732 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11733 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11734 /* 11735 * Here uscmd->uscsi_rqbuf currently points to the caller's 11736 * buffer, but we replace this with a kernel buffer that 11737 * we allocate to use with the sense data. The sense data 11738 * (if present) gets copied into this new buffer before the 11739 * command is completed. Then we copy the sense data from 11740 * our allocated buf into the caller's buffer below. Note 11741 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 11742 * below to perform the copy back to the caller's buf. 11743 */ 11744 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 11745 if (rqbufspace == UIO_USERSPACE) { 11746 uscmd->uscsi_rqlen = SENSE_LENGTH; 11747 uscmd->uscsi_rqresid = SENSE_LENGTH; 11748 } else { 11749 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 11750 uscmd->uscsi_rqlen = rlen; 11751 uscmd->uscsi_rqresid = rlen; 11752 } 11753 } else { 11754 uscmd->uscsi_rqbuf = NULL; 11755 uscmd->uscsi_rqlen = 0; 11756 uscmd->uscsi_rqresid = 0; 11757 } 11758 11759 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 11760 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 11761 11762 if (un->un_f_is_fibre == FALSE) { 11763 /* 11764 * Force asynchronous mode, if necessary. Doing this here 11765 * has the unfortunate effect of running other queued 11766 * commands async also, but since the main purpose of this 11767 * capability is downloading new drive firmware, we can 11768 * probably live with it. 11769 */ 11770 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 11771 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11772 == 1) { 11773 if (scsi_ifsetcap(SD_ADDRESS(un), 11774 "synchronous", 0, 1) == 1) { 11775 SD_TRACE(SD_LOG_IO, un, 11776 "sd_send_scsi_cmd: forced async ok\n"); 11777 } else { 11778 SD_TRACE(SD_LOG_IO, un, 11779 "sd_send_scsi_cmd:\ 11780 forced async failed\n"); 11781 rval = EINVAL; 11782 goto done; 11783 } 11784 } 11785 } 11786 11787 /* 11788 * Re-enable synchronous mode, if requested 11789 */ 11790 if (uscmd->uscsi_flags & USCSI_SYNC) { 11791 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 11792 == 0) { 11793 int i = scsi_ifsetcap(SD_ADDRESS(un), 11794 "synchronous", 1, 1); 11795 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11796 "re-enabled sync %s\n", 11797 (i == 1) ? "ok" : "failed"); 11798 } 11799 } 11800 } 11801 11802 /* 11803 * Commands sent with priority are intended for error recovery 11804 * situations, and do not have retries performed. 11805 */ 11806 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11807 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11808 } 11809 11810 /* 11811 * If we're going to do actual I/O, let physio do all the right things 11812 */ 11813 if (uscmd->uscsi_buflen != 0) { 11814 struct iovec aiov; 11815 struct uio auio; 11816 struct uio *uio = &auio; 11817 11818 bzero(&auio, sizeof (struct uio)); 11819 bzero(&aiov, sizeof (struct iovec)); 11820 aiov.iov_base = uscmd->uscsi_bufaddr; 11821 aiov.iov_len = uscmd->uscsi_buflen; 11822 uio->uio_iov = &aiov; 11823 11824 uio->uio_iovcnt = 1; 11825 uio->uio_resid = uscmd->uscsi_buflen; 11826 uio->uio_segflg = dataspace; 11827 11828 /* 11829 * physio() will block here until the command completes.... 11830 */ 11831 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 11832 11833 rval = physio(sd_uscsi_strategy, bp, dev, 11834 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 11835 sduscsimin, uio); 11836 11837 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11838 "returned from physio with 0x%x\n", rval); 11839 11840 } else { 11841 /* 11842 * We have to mimic what physio would do here! Argh! 11843 */ 11844 bp->b_flags = B_BUSY | 11845 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 11846 bp->b_edev = dev; 11847 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 11848 bp->b_bcount = 0; 11849 bp->b_blkno = 0; 11850 11851 SD_TRACE(SD_LOG_IO, un, 11852 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 11853 11854 (void) sd_uscsi_strategy(bp); 11855 11856 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 11857 11858 rval = biowait(bp); 11859 11860 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11861 "returned from biowait with 0x%x\n", rval); 11862 } 11863 11864 done: 11865 11866 #ifdef SDDEBUG 11867 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11868 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11869 uscmd->uscsi_status, uscmd->uscsi_resid); 11870 if (uscmd->uscsi_bufaddr != NULL) { 11871 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11872 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11873 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11874 if (dataspace == UIO_SYSSPACE) { 11875 SD_DUMP_MEMORY(un, SD_LOG_IO, 11876 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11877 uscmd->uscsi_buflen, SD_LOG_HEX); 11878 } 11879 } 11880 #endif 11881 11882 /* 11883 * Get the status and residual to return to the caller. 11884 */ 11885 incmd->uscsi_status = uscmd->uscsi_status; 11886 incmd->uscsi_resid = uscmd->uscsi_resid; 11887 11888 /* 11889 * If the caller wants sense data, copy back whatever sense data 11890 * we may have gotten, and update the relevant rqsense info. 11891 */ 11892 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 11893 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 11894 11895 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 11896 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 11897 11898 /* Update the Request Sense status and resid */ 11899 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 11900 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 11901 11902 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11903 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 11904 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 11905 11906 /* Copy out the sense data for user processes */ 11907 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 11908 int flags = 11909 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 11910 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 11911 rqlen, flags) != 0) { 11912 rval = EFAULT; 11913 } 11914 /* 11915 * Note: Can't touch incmd->uscsi_rqbuf so use 11916 * uscmd->uscsi_rqbuf instead. They're the same. 11917 */ 11918 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 11919 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 11920 incmd->uscsi_rqbuf, rqlen); 11921 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 11922 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 11923 } 11924 } 11925 11926 /* 11927 * Free allocated resources and return; mapout the buf in case it was 11928 * mapped in by a lower layer. 11929 */ 11930 bp_mapout(bp); 11931 freerbuf(bp); 11932 kmem_free(uip, sizeof (struct sd_uscsi_info)); 11933 if (uscmd->uscsi_rqbuf != NULL) { 11934 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 11935 } 11936 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 11937 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 11938 11939 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 11940 11941 return (rval); 11942 } 11943 11944 11945 /* 11946 * Function: sd_buf_iodone 11947 * 11948 * Description: Frees the sd_xbuf & returns the buf to its originator. 11949 * 11950 * Context: May be called from interrupt context. 11951 */ 11952 /* ARGSUSED */ 11953 static void 11954 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 11955 { 11956 struct sd_xbuf *xp; 11957 11958 ASSERT(un != NULL); 11959 ASSERT(bp != NULL); 11960 ASSERT(!mutex_owned(SD_MUTEX(un))); 11961 11962 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 11963 11964 xp = SD_GET_XBUF(bp); 11965 ASSERT(xp != NULL); 11966 11967 mutex_enter(SD_MUTEX(un)); 11968 11969 /* 11970 * Grab time when the cmd completed. 11971 * This is used for determining if the system has been 11972 * idle long enough to make it idle to the PM framework. 11973 * This is for lowering the overhead, and therefore improving 11974 * performance per I/O operation. 11975 */ 11976 un->un_pm_idle_time = ddi_get_time(); 11977 11978 un->un_ncmds_in_driver--; 11979 ASSERT(un->un_ncmds_in_driver >= 0); 11980 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 11981 un->un_ncmds_in_driver); 11982 11983 mutex_exit(SD_MUTEX(un)); 11984 11985 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 11986 biodone(bp); /* bp is gone after this */ 11987 11988 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 11989 } 11990 11991 11992 /* 11993 * Function: sd_uscsi_iodone 11994 * 11995 * Description: Frees the sd_xbuf & returns the buf to its originator. 11996 * 11997 * Context: May be called from interrupt context. 11998 */ 11999 /* ARGSUSED */ 12000 static void 12001 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12002 { 12003 struct sd_xbuf *xp; 12004 12005 ASSERT(un != NULL); 12006 ASSERT(bp != NULL); 12007 12008 xp = SD_GET_XBUF(bp); 12009 ASSERT(xp != NULL); 12010 ASSERT(!mutex_owned(SD_MUTEX(un))); 12011 12012 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12013 12014 bp->b_private = xp->xb_private; 12015 12016 mutex_enter(SD_MUTEX(un)); 12017 12018 /* 12019 * Grab time when the cmd completed. 12020 * This is used for determining if the system has been 12021 * idle long enough to make it idle to the PM framework. 12022 * This is for lowering the overhead, and therefore improving 12023 * performance per I/O operation. 12024 */ 12025 un->un_pm_idle_time = ddi_get_time(); 12026 12027 un->un_ncmds_in_driver--; 12028 ASSERT(un->un_ncmds_in_driver >= 0); 12029 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12030 un->un_ncmds_in_driver); 12031 12032 mutex_exit(SD_MUTEX(un)); 12033 12034 kmem_free(xp, sizeof (struct sd_xbuf)); 12035 biodone(bp); 12036 12037 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12038 } 12039 12040 12041 /* 12042 * Function: sd_mapblockaddr_iostart 12043 * 12044 * Description: Verify request lies withing the partition limits for 12045 * the indicated minor device. Issue "overrun" buf if 12046 * request would exceed partition range. Converts 12047 * partition-relative block address to absolute. 12048 * 12049 * Context: Can sleep 12050 * 12051 * Issues: This follows what the old code did, in terms of accessing 12052 * some of the partition info in the unit struct without holding 12053 * the mutext. This is a general issue, if the partition info 12054 * can be altered while IO is in progress... as soon as we send 12055 * a buf, its partitioning can be invalid before it gets to the 12056 * device. Probably the right fix is to move partitioning out 12057 * of the driver entirely. 12058 */ 12059 12060 static void 12061 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12062 { 12063 daddr_t nblocks; /* #blocks in the given partition */ 12064 daddr_t blocknum; /* Block number specified by the buf */ 12065 size_t requested_nblocks; 12066 size_t available_nblocks; 12067 int partition; 12068 diskaddr_t partition_offset; 12069 struct sd_xbuf *xp; 12070 12071 12072 ASSERT(un != NULL); 12073 ASSERT(bp != NULL); 12074 ASSERT(!mutex_owned(SD_MUTEX(un))); 12075 12076 SD_TRACE(SD_LOG_IO_PARTITION, un, 12077 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12078 12079 xp = SD_GET_XBUF(bp); 12080 ASSERT(xp != NULL); 12081 12082 /* 12083 * If the geometry is not indicated as valid, attempt to access 12084 * the unit & verify the geometry/label. This can be the case for 12085 * removable-media devices, of if the device was opened in 12086 * NDELAY/NONBLOCK mode. 12087 */ 12088 if ((un->un_f_geometry_is_valid != TRUE) && 12089 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12090 /* 12091 * For removable devices it is possible to start an I/O 12092 * without a media by opening the device in nodelay mode. 12093 * Also for writable CDs there can be many scenarios where 12094 * there is no geometry yet but volume manager is trying to 12095 * issue a read() just because it can see TOC on the CD. So 12096 * do not print a message for removables. 12097 */ 12098 if (!ISREMOVABLE(un)) { 12099 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12100 "i/o to invalid geometry\n"); 12101 } 12102 bioerror(bp, EIO); 12103 bp->b_resid = bp->b_bcount; 12104 SD_BEGIN_IODONE(index, un, bp); 12105 return; 12106 } 12107 12108 partition = SDPART(bp->b_edev); 12109 12110 /* #blocks in partition */ 12111 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12112 12113 /* Use of a local variable potentially improves performance slightly */ 12114 partition_offset = un->un_offset[partition]; 12115 12116 /* 12117 * blocknum is the starting block number of the request. At this 12118 * point it is still relative to the start of the minor device. 12119 */ 12120 blocknum = xp->xb_blkno; 12121 12122 /* 12123 * Legacy: If the starting block number is one past the last block 12124 * in the partition, do not set B_ERROR in the buf. 12125 */ 12126 if (blocknum == nblocks) { 12127 goto error_exit; 12128 } 12129 12130 /* 12131 * Confirm that the first block of the request lies within the 12132 * partition limits. Also the requested number of bytes must be 12133 * a multiple of the system block size. 12134 */ 12135 if ((blocknum < 0) || (blocknum >= nblocks) || 12136 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12137 bp->b_flags |= B_ERROR; 12138 goto error_exit; 12139 } 12140 12141 /* 12142 * If the requsted # blocks exceeds the available # blocks, that 12143 * is an overrun of the partition. 12144 */ 12145 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12146 available_nblocks = (size_t)(nblocks - blocknum); 12147 ASSERT(nblocks >= blocknum); 12148 12149 if (requested_nblocks > available_nblocks) { 12150 /* 12151 * Allocate an "overrun" buf to allow the request to proceed 12152 * for the amount of space available in the partition. The 12153 * amount not transferred will be added into the b_resid 12154 * when the operation is complete. The overrun buf 12155 * replaces the original buf here, and the original buf 12156 * is saved inside the overrun buf, for later use. 12157 */ 12158 size_t resid = SD_SYSBLOCKS2BYTES(un, 12159 (offset_t)(requested_nblocks - available_nblocks)); 12160 size_t count = bp->b_bcount - resid; 12161 /* 12162 * Note: count is an unsigned entity thus it'll NEVER 12163 * be less than 0 so ASSERT the original values are 12164 * correct. 12165 */ 12166 ASSERT(bp->b_bcount >= resid); 12167 12168 bp = sd_bioclone_alloc(bp, count, blocknum, 12169 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12170 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12171 ASSERT(xp != NULL); 12172 } 12173 12174 /* At this point there should be no residual for this buf. */ 12175 ASSERT(bp->b_resid == 0); 12176 12177 /* Convert the block number to an absolute address. */ 12178 xp->xb_blkno += partition_offset; 12179 12180 SD_NEXT_IOSTART(index, un, bp); 12181 12182 SD_TRACE(SD_LOG_IO_PARTITION, un, 12183 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12184 12185 return; 12186 12187 error_exit: 12188 bp->b_resid = bp->b_bcount; 12189 SD_BEGIN_IODONE(index, un, bp); 12190 SD_TRACE(SD_LOG_IO_PARTITION, un, 12191 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12192 } 12193 12194 12195 /* 12196 * Function: sd_mapblockaddr_iodone 12197 * 12198 * Description: Completion-side processing for partition management. 12199 * 12200 * Context: May be called under interrupt context 12201 */ 12202 12203 static void 12204 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12205 { 12206 /* int partition; */ /* Not used, see below. */ 12207 ASSERT(un != NULL); 12208 ASSERT(bp != NULL); 12209 ASSERT(!mutex_owned(SD_MUTEX(un))); 12210 12211 SD_TRACE(SD_LOG_IO_PARTITION, un, 12212 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12213 12214 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12215 /* 12216 * We have an "overrun" buf to deal with... 12217 */ 12218 struct sd_xbuf *xp; 12219 struct buf *obp; /* ptr to the original buf */ 12220 12221 xp = SD_GET_XBUF(bp); 12222 ASSERT(xp != NULL); 12223 12224 /* Retrieve the pointer to the original buf */ 12225 obp = (struct buf *)xp->xb_private; 12226 ASSERT(obp != NULL); 12227 12228 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12229 bioerror(obp, bp->b_error); 12230 12231 sd_bioclone_free(bp); 12232 12233 /* 12234 * Get back the original buf. 12235 * Note that since the restoration of xb_blkno below 12236 * was removed, the sd_xbuf is not needed. 12237 */ 12238 bp = obp; 12239 /* 12240 * xp = SD_GET_XBUF(bp); 12241 * ASSERT(xp != NULL); 12242 */ 12243 } 12244 12245 /* 12246 * Convert sd->xb_blkno back to a minor-device relative value. 12247 * Note: this has been commented out, as it is not needed in the 12248 * current implementation of the driver (ie, since this function 12249 * is at the top of the layering chains, so the info will be 12250 * discarded) and it is in the "hot" IO path. 12251 * 12252 * partition = getminor(bp->b_edev) & SDPART_MASK; 12253 * xp->xb_blkno -= un->un_offset[partition]; 12254 */ 12255 12256 SD_NEXT_IODONE(index, un, bp); 12257 12258 SD_TRACE(SD_LOG_IO_PARTITION, un, 12259 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12260 } 12261 12262 12263 /* 12264 * Function: sd_mapblocksize_iostart 12265 * 12266 * Description: Convert between system block size (un->un_sys_blocksize) 12267 * and target block size (un->un_tgt_blocksize). 12268 * 12269 * Context: Can sleep to allocate resources. 12270 * 12271 * Assumptions: A higher layer has already performed any partition validation, 12272 * and converted the xp->xb_blkno to an absolute value relative 12273 * to the start of the device. 12274 * 12275 * It is also assumed that the higher layer has implemented 12276 * an "overrun" mechanism for the case where the request would 12277 * read/write beyond the end of a partition. In this case we 12278 * assume (and ASSERT) that bp->b_resid == 0. 12279 * 12280 * Note: The implementation for this routine assumes the target 12281 * block size remains constant between allocation and transport. 12282 */ 12283 12284 static void 12285 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12286 { 12287 struct sd_mapblocksize_info *bsp; 12288 struct sd_xbuf *xp; 12289 offset_t first_byte; 12290 daddr_t start_block, end_block; 12291 daddr_t request_bytes; 12292 ushort_t is_aligned = FALSE; 12293 12294 ASSERT(un != NULL); 12295 ASSERT(bp != NULL); 12296 ASSERT(!mutex_owned(SD_MUTEX(un))); 12297 ASSERT(bp->b_resid == 0); 12298 12299 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12300 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12301 12302 /* 12303 * For a non-writable CD, a write request is an error 12304 */ 12305 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12306 (un->un_f_mmc_writable_media == FALSE)) { 12307 bioerror(bp, EIO); 12308 bp->b_resid = bp->b_bcount; 12309 SD_BEGIN_IODONE(index, un, bp); 12310 return; 12311 } 12312 12313 /* 12314 * We do not need a shadow buf if the device is using 12315 * un->un_sys_blocksize as its block size or if bcount == 0. 12316 * In this case there is no layer-private data block allocated. 12317 */ 12318 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12319 (bp->b_bcount == 0)) { 12320 goto done; 12321 } 12322 12323 #if defined(__i386) || defined(__amd64) 12324 /* We do not support non-block-aligned transfers for ROD devices */ 12325 ASSERT(!ISROD(un)); 12326 #endif 12327 12328 xp = SD_GET_XBUF(bp); 12329 ASSERT(xp != NULL); 12330 12331 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12332 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12333 un->un_tgt_blocksize, un->un_sys_blocksize); 12334 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12335 "request start block:0x%x\n", xp->xb_blkno); 12336 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12337 "request len:0x%x\n", bp->b_bcount); 12338 12339 /* 12340 * Allocate the layer-private data area for the mapblocksize layer. 12341 * Layers are allowed to use the xp_private member of the sd_xbuf 12342 * struct to store the pointer to their layer-private data block, but 12343 * each layer also has the responsibility of restoring the prior 12344 * contents of xb_private before returning the buf/xbuf to the 12345 * higher layer that sent it. 12346 * 12347 * Here we save the prior contents of xp->xb_private into the 12348 * bsp->mbs_oprivate field of our layer-private data area. This value 12349 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12350 * the layer-private area and returning the buf/xbuf to the layer 12351 * that sent it. 12352 * 12353 * Note that here we use kmem_zalloc for the allocation as there are 12354 * parts of the mapblocksize code that expect certain fields to be 12355 * zero unless explicitly set to a required value. 12356 */ 12357 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12358 bsp->mbs_oprivate = xp->xb_private; 12359 xp->xb_private = bsp; 12360 12361 /* 12362 * This treats the data on the disk (target) as an array of bytes. 12363 * first_byte is the byte offset, from the beginning of the device, 12364 * to the location of the request. This is converted from a 12365 * un->un_sys_blocksize block address to a byte offset, and then back 12366 * to a block address based upon a un->un_tgt_blocksize block size. 12367 * 12368 * xp->xb_blkno should be absolute upon entry into this function, 12369 * but, but it is based upon partitions that use the "system" 12370 * block size. It must be adjusted to reflect the block size of 12371 * the target. 12372 * 12373 * Note that end_block is actually the block that follows the last 12374 * block of the request, but that's what is needed for the computation. 12375 */ 12376 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12377 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12378 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12379 un->un_tgt_blocksize; 12380 12381 /* request_bytes is rounded up to a multiple of the target block size */ 12382 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12383 12384 /* 12385 * See if the starting address of the request and the request 12386 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12387 * then we do not need to allocate a shadow buf to handle the request. 12388 */ 12389 if (((first_byte % un->un_tgt_blocksize) == 0) && 12390 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12391 is_aligned = TRUE; 12392 } 12393 12394 if ((bp->b_flags & B_READ) == 0) { 12395 /* 12396 * Lock the range for a write operation. An aligned request is 12397 * considered a simple write; otherwise the request must be a 12398 * read-modify-write. 12399 */ 12400 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12401 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12402 } 12403 12404 /* 12405 * Alloc a shadow buf if the request is not aligned. Also, this is 12406 * where the READ command is generated for a read-modify-write. (The 12407 * write phase is deferred until after the read completes.) 12408 */ 12409 if (is_aligned == FALSE) { 12410 12411 struct sd_mapblocksize_info *shadow_bsp; 12412 struct sd_xbuf *shadow_xp; 12413 struct buf *shadow_bp; 12414 12415 /* 12416 * Allocate the shadow buf and it associated xbuf. Note that 12417 * after this call the xb_blkno value in both the original 12418 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12419 * same: absolute relative to the start of the device, and 12420 * adjusted for the target block size. The b_blkno in the 12421 * shadow buf will also be set to this value. We should never 12422 * change b_blkno in the original bp however. 12423 * 12424 * Note also that the shadow buf will always need to be a 12425 * READ command, regardless of whether the incoming command 12426 * is a READ or a WRITE. 12427 */ 12428 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12429 xp->xb_blkno, 12430 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12431 12432 shadow_xp = SD_GET_XBUF(shadow_bp); 12433 12434 /* 12435 * Allocate the layer-private data for the shadow buf. 12436 * (No need to preserve xb_private in the shadow xbuf.) 12437 */ 12438 shadow_xp->xb_private = shadow_bsp = 12439 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12440 12441 /* 12442 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12443 * to figure out where the start of the user data is (based upon 12444 * the system block size) in the data returned by the READ 12445 * command (which will be based upon the target blocksize). Note 12446 * that this is only really used if the request is unaligned. 12447 */ 12448 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12449 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12450 ASSERT((bsp->mbs_copy_offset >= 0) && 12451 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12452 12453 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12454 12455 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12456 12457 /* Transfer the wmap (if any) to the shadow buf */ 12458 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12459 bsp->mbs_wmp = NULL; 12460 12461 /* 12462 * The shadow buf goes on from here in place of the 12463 * original buf. 12464 */ 12465 shadow_bsp->mbs_orig_bp = bp; 12466 bp = shadow_bp; 12467 } 12468 12469 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12470 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12471 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12472 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12473 request_bytes); 12474 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12475 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12476 12477 done: 12478 SD_NEXT_IOSTART(index, un, bp); 12479 12480 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12481 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12482 } 12483 12484 12485 /* 12486 * Function: sd_mapblocksize_iodone 12487 * 12488 * Description: Completion side processing for block-size mapping. 12489 * 12490 * Context: May be called under interrupt context 12491 */ 12492 12493 static void 12494 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12495 { 12496 struct sd_mapblocksize_info *bsp; 12497 struct sd_xbuf *xp; 12498 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12499 struct buf *orig_bp; /* ptr to the original buf */ 12500 offset_t shadow_end; 12501 offset_t request_end; 12502 offset_t shadow_start; 12503 ssize_t copy_offset; 12504 size_t copy_length; 12505 size_t shortfall; 12506 uint_t is_write; /* TRUE if this bp is a WRITE */ 12507 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12508 12509 ASSERT(un != NULL); 12510 ASSERT(bp != NULL); 12511 12512 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12513 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12514 12515 /* 12516 * There is no shadow buf or layer-private data if the target is 12517 * using un->un_sys_blocksize as its block size or if bcount == 0. 12518 */ 12519 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12520 (bp->b_bcount == 0)) { 12521 goto exit; 12522 } 12523 12524 xp = SD_GET_XBUF(bp); 12525 ASSERT(xp != NULL); 12526 12527 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12528 bsp = xp->xb_private; 12529 12530 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12531 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12532 12533 if (is_write) { 12534 /* 12535 * For a WRITE request we must free up the block range that 12536 * we have locked up. This holds regardless of whether this is 12537 * an aligned write request or a read-modify-write request. 12538 */ 12539 sd_range_unlock(un, bsp->mbs_wmp); 12540 bsp->mbs_wmp = NULL; 12541 } 12542 12543 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12544 /* 12545 * An aligned read or write command will have no shadow buf; 12546 * there is not much else to do with it. 12547 */ 12548 goto done; 12549 } 12550 12551 orig_bp = bsp->mbs_orig_bp; 12552 ASSERT(orig_bp != NULL); 12553 orig_xp = SD_GET_XBUF(orig_bp); 12554 ASSERT(orig_xp != NULL); 12555 ASSERT(!mutex_owned(SD_MUTEX(un))); 12556 12557 if (!is_write && has_wmap) { 12558 /* 12559 * A READ with a wmap means this is the READ phase of a 12560 * read-modify-write. If an error occurred on the READ then 12561 * we do not proceed with the WRITE phase or copy any data. 12562 * Just release the write maps and return with an error. 12563 */ 12564 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12565 orig_bp->b_resid = orig_bp->b_bcount; 12566 bioerror(orig_bp, bp->b_error); 12567 sd_range_unlock(un, bsp->mbs_wmp); 12568 goto freebuf_done; 12569 } 12570 } 12571 12572 /* 12573 * Here is where we set up to copy the data from the shadow buf 12574 * into the space associated with the original buf. 12575 * 12576 * To deal with the conversion between block sizes, these 12577 * computations treat the data as an array of bytes, with the 12578 * first byte (byte 0) corresponding to the first byte in the 12579 * first block on the disk. 12580 */ 12581 12582 /* 12583 * shadow_start and shadow_len indicate the location and size of 12584 * the data returned with the shadow IO request. 12585 */ 12586 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12587 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12588 12589 /* 12590 * copy_offset gives the offset (in bytes) from the start of the first 12591 * block of the READ request to the beginning of the data. We retrieve 12592 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12593 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12594 * data to be copied (in bytes). 12595 */ 12596 copy_offset = bsp->mbs_copy_offset; 12597 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12598 copy_length = orig_bp->b_bcount; 12599 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12600 12601 /* 12602 * Set up the resid and error fields of orig_bp as appropriate. 12603 */ 12604 if (shadow_end >= request_end) { 12605 /* We got all the requested data; set resid to zero */ 12606 orig_bp->b_resid = 0; 12607 } else { 12608 /* 12609 * We failed to get enough data to fully satisfy the original 12610 * request. Just copy back whatever data we got and set 12611 * up the residual and error code as required. 12612 * 12613 * 'shortfall' is the amount by which the data received with the 12614 * shadow buf has "fallen short" of the requested amount. 12615 */ 12616 shortfall = (size_t)(request_end - shadow_end); 12617 12618 if (shortfall > orig_bp->b_bcount) { 12619 /* 12620 * We did not get enough data to even partially 12621 * fulfill the original request. The residual is 12622 * equal to the amount requested. 12623 */ 12624 orig_bp->b_resid = orig_bp->b_bcount; 12625 } else { 12626 /* 12627 * We did not get all the data that we requested 12628 * from the device, but we will try to return what 12629 * portion we did get. 12630 */ 12631 orig_bp->b_resid = shortfall; 12632 } 12633 ASSERT(copy_length >= orig_bp->b_resid); 12634 copy_length -= orig_bp->b_resid; 12635 } 12636 12637 /* Propagate the error code from the shadow buf to the original buf */ 12638 bioerror(orig_bp, bp->b_error); 12639 12640 if (is_write) { 12641 goto freebuf_done; /* No data copying for a WRITE */ 12642 } 12643 12644 if (has_wmap) { 12645 /* 12646 * This is a READ command from the READ phase of a 12647 * read-modify-write request. We have to copy the data given 12648 * by the user OVER the data returned by the READ command, 12649 * then convert the command from a READ to a WRITE and send 12650 * it back to the target. 12651 */ 12652 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12653 copy_length); 12654 12655 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12656 12657 /* 12658 * Dispatch the WRITE command to the taskq thread, which 12659 * will in turn send the command to the target. When the 12660 * WRITE command completes, we (sd_mapblocksize_iodone()) 12661 * will get called again as part of the iodone chain 12662 * processing for it. Note that we will still be dealing 12663 * with the shadow buf at that point. 12664 */ 12665 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12666 KM_NOSLEEP) != 0) { 12667 /* 12668 * Dispatch was successful so we are done. Return 12669 * without going any higher up the iodone chain. Do 12670 * not free up any layer-private data until after the 12671 * WRITE completes. 12672 */ 12673 return; 12674 } 12675 12676 /* 12677 * Dispatch of the WRITE command failed; set up the error 12678 * condition and send this IO back up the iodone chain. 12679 */ 12680 bioerror(orig_bp, EIO); 12681 orig_bp->b_resid = orig_bp->b_bcount; 12682 12683 } else { 12684 /* 12685 * This is a regular READ request (ie, not a RMW). Copy the 12686 * data from the shadow buf into the original buf. The 12687 * copy_offset compensates for any "misalignment" between the 12688 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12689 * original buf (with its un->un_sys_blocksize blocks). 12690 */ 12691 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12692 copy_length); 12693 } 12694 12695 freebuf_done: 12696 12697 /* 12698 * At this point we still have both the shadow buf AND the original 12699 * buf to deal with, as well as the layer-private data area in each. 12700 * Local variables are as follows: 12701 * 12702 * bp -- points to shadow buf 12703 * xp -- points to xbuf of shadow buf 12704 * bsp -- points to layer-private data area of shadow buf 12705 * orig_bp -- points to original buf 12706 * 12707 * First free the shadow buf and its associated xbuf, then free the 12708 * layer-private data area from the shadow buf. There is no need to 12709 * restore xb_private in the shadow xbuf. 12710 */ 12711 sd_shadow_buf_free(bp); 12712 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12713 12714 /* 12715 * Now update the local variables to point to the original buf, xbuf, 12716 * and layer-private area. 12717 */ 12718 bp = orig_bp; 12719 xp = SD_GET_XBUF(bp); 12720 ASSERT(xp != NULL); 12721 ASSERT(xp == orig_xp); 12722 bsp = xp->xb_private; 12723 ASSERT(bsp != NULL); 12724 12725 done: 12726 /* 12727 * Restore xb_private to whatever it was set to by the next higher 12728 * layer in the chain, then free the layer-private data area. 12729 */ 12730 xp->xb_private = bsp->mbs_oprivate; 12731 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12732 12733 exit: 12734 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12735 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12736 12737 SD_NEXT_IODONE(index, un, bp); 12738 } 12739 12740 12741 /* 12742 * Function: sd_checksum_iostart 12743 * 12744 * Description: A stub function for a layer that's currently not used. 12745 * For now just a placeholder. 12746 * 12747 * Context: Kernel thread context 12748 */ 12749 12750 static void 12751 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12752 { 12753 ASSERT(un != NULL); 12754 ASSERT(bp != NULL); 12755 ASSERT(!mutex_owned(SD_MUTEX(un))); 12756 SD_NEXT_IOSTART(index, un, bp); 12757 } 12758 12759 12760 /* 12761 * Function: sd_checksum_iodone 12762 * 12763 * Description: A stub function for a layer that's currently not used. 12764 * For now just a placeholder. 12765 * 12766 * Context: May be called under interrupt context 12767 */ 12768 12769 static void 12770 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12771 { 12772 ASSERT(un != NULL); 12773 ASSERT(bp != NULL); 12774 ASSERT(!mutex_owned(SD_MUTEX(un))); 12775 SD_NEXT_IODONE(index, un, bp); 12776 } 12777 12778 12779 /* 12780 * Function: sd_checksum_uscsi_iostart 12781 * 12782 * Description: A stub function for a layer that's currently not used. 12783 * For now just a placeholder. 12784 * 12785 * Context: Kernel thread context 12786 */ 12787 12788 static void 12789 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12790 { 12791 ASSERT(un != NULL); 12792 ASSERT(bp != NULL); 12793 ASSERT(!mutex_owned(SD_MUTEX(un))); 12794 SD_NEXT_IOSTART(index, un, bp); 12795 } 12796 12797 12798 /* 12799 * Function: sd_checksum_uscsi_iodone 12800 * 12801 * Description: A stub function for a layer that's currently not used. 12802 * For now just a placeholder. 12803 * 12804 * Context: May be called under interrupt context 12805 */ 12806 12807 static void 12808 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12809 { 12810 ASSERT(un != NULL); 12811 ASSERT(bp != NULL); 12812 ASSERT(!mutex_owned(SD_MUTEX(un))); 12813 SD_NEXT_IODONE(index, un, bp); 12814 } 12815 12816 12817 /* 12818 * Function: sd_pm_iostart 12819 * 12820 * Description: iostart-side routine for Power mangement. 12821 * 12822 * Context: Kernel thread context 12823 */ 12824 12825 static void 12826 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12827 { 12828 ASSERT(un != NULL); 12829 ASSERT(bp != NULL); 12830 ASSERT(!mutex_owned(SD_MUTEX(un))); 12831 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12832 12833 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12834 12835 if (sd_pm_entry(un) != DDI_SUCCESS) { 12836 /* 12837 * Set up to return the failed buf back up the 'iodone' 12838 * side of the calling chain. 12839 */ 12840 bioerror(bp, EIO); 12841 bp->b_resid = bp->b_bcount; 12842 12843 SD_BEGIN_IODONE(index, un, bp); 12844 12845 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12846 return; 12847 } 12848 12849 SD_NEXT_IOSTART(index, un, bp); 12850 12851 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12852 } 12853 12854 12855 /* 12856 * Function: sd_pm_iodone 12857 * 12858 * Description: iodone-side routine for power mangement. 12859 * 12860 * Context: may be called from interrupt context 12861 */ 12862 12863 static void 12864 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12865 { 12866 ASSERT(un != NULL); 12867 ASSERT(bp != NULL); 12868 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12869 12870 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12871 12872 /* 12873 * After attach the following flag is only read, so don't 12874 * take the penalty of acquiring a mutex for it. 12875 */ 12876 if (un->un_f_pm_is_enabled == TRUE) { 12877 sd_pm_exit(un); 12878 } 12879 12880 SD_NEXT_IODONE(index, un, bp); 12881 12882 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12883 } 12884 12885 12886 /* 12887 * Function: sd_core_iostart 12888 * 12889 * Description: Primary driver function for enqueuing buf(9S) structs from 12890 * the system and initiating IO to the target device 12891 * 12892 * Context: Kernel thread context. Can sleep. 12893 * 12894 * Assumptions: - The given xp->xb_blkno is absolute 12895 * (ie, relative to the start of the device). 12896 * - The IO is to be done using the native blocksize of 12897 * the device, as specified in un->un_tgt_blocksize. 12898 */ 12899 /* ARGSUSED */ 12900 static void 12901 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12902 { 12903 struct sd_xbuf *xp; 12904 12905 ASSERT(un != NULL); 12906 ASSERT(bp != NULL); 12907 ASSERT(!mutex_owned(SD_MUTEX(un))); 12908 ASSERT(bp->b_resid == 0); 12909 12910 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12911 12912 xp = SD_GET_XBUF(bp); 12913 ASSERT(xp != NULL); 12914 12915 mutex_enter(SD_MUTEX(un)); 12916 12917 /* 12918 * If we are currently in the failfast state, fail any new IO 12919 * that has B_FAILFAST set, then return. 12920 */ 12921 if ((bp->b_flags & B_FAILFAST) && 12922 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12923 mutex_exit(SD_MUTEX(un)); 12924 bioerror(bp, EIO); 12925 bp->b_resid = bp->b_bcount; 12926 SD_BEGIN_IODONE(index, un, bp); 12927 return; 12928 } 12929 12930 if (SD_IS_DIRECT_PRIORITY(xp)) { 12931 /* 12932 * Priority command -- transport it immediately. 12933 * 12934 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12935 * because all direct priority commands should be associated 12936 * with error recovery actions which we don't want to retry. 12937 */ 12938 sd_start_cmds(un, bp); 12939 } else { 12940 /* 12941 * Normal command -- add it to the wait queue, then start 12942 * transporting commands from the wait queue. 12943 */ 12944 sd_add_buf_to_waitq(un, bp); 12945 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12946 sd_start_cmds(un, NULL); 12947 } 12948 12949 mutex_exit(SD_MUTEX(un)); 12950 12951 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 12952 } 12953 12954 12955 /* 12956 * Function: sd_init_cdb_limits 12957 * 12958 * Description: This is to handle scsi_pkt initialization differences 12959 * between the driver platforms. 12960 * 12961 * Legacy behaviors: 12962 * 12963 * If the block number or the sector count exceeds the 12964 * capabilities of a Group 0 command, shift over to a 12965 * Group 1 command. We don't blindly use Group 1 12966 * commands because a) some drives (CDC Wren IVs) get a 12967 * bit confused, and b) there is probably a fair amount 12968 * of speed difference for a target to receive and decode 12969 * a 10 byte command instead of a 6 byte command. 12970 * 12971 * The xfer time difference of 6 vs 10 byte CDBs is 12972 * still significant so this code is still worthwhile. 12973 * 10 byte CDBs are very inefficient with the fas HBA driver 12974 * and older disks. Each CDB byte took 1 usec with some 12975 * popular disks. 12976 * 12977 * Context: Must be called at attach time 12978 */ 12979 12980 static void 12981 sd_init_cdb_limits(struct sd_lun *un) 12982 { 12983 /* 12984 * Use CDB_GROUP1 commands for most devices except for 12985 * parallel SCSI fixed drives in which case we get better 12986 * performance using CDB_GROUP0 commands (where applicable). 12987 */ 12988 un->un_mincdb = SD_CDB_GROUP1; 12989 #if !defined(__fibre) 12990 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 12991 !ISREMOVABLE(un)) { 12992 un->un_mincdb = SD_CDB_GROUP0; 12993 } 12994 #endif 12995 12996 /* 12997 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 12998 * commands for fixed disks unless we are building for a 32 bit 12999 * kernel. 13000 */ 13001 #ifdef _LP64 13002 un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP4; 13003 #else 13004 un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP1; 13005 #endif 13006 13007 /* 13008 * x86 systems require the PKT_DMA_PARTIAL flag 13009 */ 13010 #if defined(__x86) 13011 un->un_pkt_flags = PKT_DMA_PARTIAL; 13012 #else 13013 un->un_pkt_flags = 0; 13014 #endif 13015 13016 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13017 ? sizeof (struct scsi_arq_status) : 1); 13018 un->un_cmd_timeout = (ushort_t)sd_io_time; 13019 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13020 } 13021 13022 13023 /* 13024 * Function: sd_initpkt_for_buf 13025 * 13026 * Description: Allocate and initialize for transport a scsi_pkt struct, 13027 * based upon the info specified in the given buf struct. 13028 * 13029 * Assumes the xb_blkno in the request is absolute (ie, 13030 * relative to the start of the device (NOT partition!). 13031 * Also assumes that the request is using the native block 13032 * size of the device (as returned by the READ CAPACITY 13033 * command). 13034 * 13035 * Return Code: SD_PKT_ALLOC_SUCCESS 13036 * SD_PKT_ALLOC_FAILURE 13037 * SD_PKT_ALLOC_FAILURE_NO_DMA 13038 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13039 * 13040 * Context: Kernel thread and may be called from software interrupt context 13041 * as part of a sdrunout callback. This function may not block or 13042 * call routines that block 13043 */ 13044 13045 static int 13046 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13047 { 13048 struct sd_xbuf *xp; 13049 struct scsi_pkt *pktp = NULL; 13050 struct sd_lun *un; 13051 size_t blockcount; 13052 daddr_t startblock; 13053 int rval; 13054 int cmd_flags; 13055 13056 ASSERT(bp != NULL); 13057 ASSERT(pktpp != NULL); 13058 xp = SD_GET_XBUF(bp); 13059 ASSERT(xp != NULL); 13060 un = SD_GET_UN(bp); 13061 ASSERT(un != NULL); 13062 ASSERT(mutex_owned(SD_MUTEX(un))); 13063 ASSERT(bp->b_resid == 0); 13064 13065 SD_TRACE(SD_LOG_IO_CORE, un, 13066 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13067 13068 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13069 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13070 /* 13071 * Already have a scsi_pkt -- just need DMA resources. 13072 * We must recompute the CDB in case the mapping returns 13073 * a nonzero pkt_resid. 13074 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13075 * that is being retried, the unmap/remap of the DMA resouces 13076 * will result in the entire transfer starting over again 13077 * from the very first block. 13078 */ 13079 ASSERT(xp->xb_pktp != NULL); 13080 pktp = xp->xb_pktp; 13081 } else { 13082 pktp = NULL; 13083 } 13084 #endif /* __i386 || __amd64 */ 13085 13086 startblock = xp->xb_blkno; /* Absolute block num. */ 13087 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13088 13089 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13090 13091 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13092 13093 #else 13094 13095 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13096 13097 #endif 13098 13099 /* 13100 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13101 * call scsi_init_pkt, and build the CDB. 13102 */ 13103 rval = sd_setup_rw_pkt(un, &pktp, bp, 13104 cmd_flags, sdrunout, (caddr_t)un, 13105 startblock, blockcount); 13106 13107 if (rval == 0) { 13108 /* 13109 * Success. 13110 * 13111 * If partial DMA is being used and required for this transfer. 13112 * set it up here. 13113 */ 13114 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13115 (pktp->pkt_resid != 0)) { 13116 13117 /* 13118 * Save the CDB length and pkt_resid for the 13119 * next xfer 13120 */ 13121 xp->xb_dma_resid = pktp->pkt_resid; 13122 13123 /* rezero resid */ 13124 pktp->pkt_resid = 0; 13125 13126 } else { 13127 xp->xb_dma_resid = 0; 13128 } 13129 13130 pktp->pkt_flags = un->un_tagflags; 13131 pktp->pkt_time = un->un_cmd_timeout; 13132 pktp->pkt_comp = sdintr; 13133 13134 pktp->pkt_private = bp; 13135 *pktpp = pktp; 13136 13137 SD_TRACE(SD_LOG_IO_CORE, un, 13138 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13139 13140 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13141 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13142 #endif 13143 13144 return (SD_PKT_ALLOC_SUCCESS); 13145 13146 } 13147 13148 /* 13149 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13150 * from sd_setup_rw_pkt. 13151 */ 13152 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13153 13154 if (rval == SD_PKT_ALLOC_FAILURE) { 13155 *pktpp = NULL; 13156 /* 13157 * Set the driver state to RWAIT to indicate the driver 13158 * is waiting on resource allocations. The driver will not 13159 * suspend, pm_suspend, or detatch while the state is RWAIT. 13160 */ 13161 New_state(un, SD_STATE_RWAIT); 13162 13163 SD_ERROR(SD_LOG_IO_CORE, un, 13164 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13165 13166 if ((bp->b_flags & B_ERROR) != 0) { 13167 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13168 } 13169 return (SD_PKT_ALLOC_FAILURE); 13170 } else { 13171 /* 13172 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13173 * 13174 * This should never happen. Maybe someone messed with the 13175 * kernel's minphys? 13176 */ 13177 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13178 "Request rejected: too large for CDB: " 13179 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13180 SD_ERROR(SD_LOG_IO_CORE, un, 13181 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13182 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13183 13184 } 13185 } 13186 13187 13188 /* 13189 * Function: sd_destroypkt_for_buf 13190 * 13191 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13192 * 13193 * Context: Kernel thread or interrupt context 13194 */ 13195 13196 static void 13197 sd_destroypkt_for_buf(struct buf *bp) 13198 { 13199 ASSERT(bp != NULL); 13200 ASSERT(SD_GET_UN(bp) != NULL); 13201 13202 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13203 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13204 13205 ASSERT(SD_GET_PKTP(bp) != NULL); 13206 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13207 13208 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13209 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13210 } 13211 13212 /* 13213 * Function: sd_setup_rw_pkt 13214 * 13215 * Description: Determines appropriate CDB group for the requested LBA 13216 * and transfer length, calls scsi_init_pkt, and builds 13217 * the CDB. Do not use for partial DMA transfers except 13218 * for the initial transfer since the CDB size must 13219 * remain constant. 13220 * 13221 * Context: Kernel thread and may be called from software interrupt 13222 * context as part of a sdrunout callback. This function may not 13223 * block or call routines that block 13224 */ 13225 13226 13227 int 13228 sd_setup_rw_pkt(struct sd_lun *un, 13229 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13230 int (*callback)(caddr_t), caddr_t callback_arg, 13231 diskaddr_t lba, uint32_t blockcount) 13232 { 13233 struct scsi_pkt *return_pktp; 13234 union scsi_cdb *cdbp; 13235 struct sd_cdbinfo *cp = NULL; 13236 int i; 13237 13238 /* 13239 * See which size CDB to use, based upon the request. 13240 */ 13241 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13242 13243 /* 13244 * Check lba and block count against sd_cdbtab limits. 13245 * In the partial DMA case, we have to use the same size 13246 * CDB for all the transfers. Check lba + blockcount 13247 * against the max LBA so we know that segment of the 13248 * transfer can use the CDB we select. 13249 */ 13250 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13251 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13252 13253 /* 13254 * The command will fit into the CDB type 13255 * specified by sd_cdbtab[i]. 13256 */ 13257 cp = sd_cdbtab + i; 13258 13259 /* 13260 * Call scsi_init_pkt so we can fill in the 13261 * CDB. 13262 */ 13263 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13264 bp, cp->sc_grpcode, un->un_status_len, 0, 13265 flags, callback, callback_arg); 13266 13267 if (return_pktp != NULL) { 13268 13269 /* 13270 * Return new value of pkt 13271 */ 13272 *pktpp = return_pktp; 13273 13274 /* 13275 * To be safe, zero the CDB insuring there is 13276 * no leftover data from a previous command. 13277 */ 13278 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13279 13280 /* 13281 * Handle partial DMA mapping 13282 */ 13283 if (return_pktp->pkt_resid != 0) { 13284 13285 /* 13286 * Not going to xfer as many blocks as 13287 * originally expected 13288 */ 13289 blockcount -= 13290 SD_BYTES2TGTBLOCKS(un, 13291 return_pktp->pkt_resid); 13292 } 13293 13294 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13295 13296 /* 13297 * Set command byte based on the CDB 13298 * type we matched. 13299 */ 13300 cdbp->scc_cmd = cp->sc_grpmask | 13301 ((bp->b_flags & B_READ) ? 13302 SCMD_READ : SCMD_WRITE); 13303 13304 SD_FILL_SCSI1_LUN(un, return_pktp); 13305 13306 /* 13307 * Fill in LBA and length 13308 */ 13309 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13310 (cp->sc_grpcode == CDB_GROUP4) || 13311 (cp->sc_grpcode == CDB_GROUP0) || 13312 (cp->sc_grpcode == CDB_GROUP5)); 13313 13314 if (cp->sc_grpcode == CDB_GROUP1) { 13315 FORMG1ADDR(cdbp, lba); 13316 FORMG1COUNT(cdbp, blockcount); 13317 return (0); 13318 } else if (cp->sc_grpcode == CDB_GROUP4) { 13319 FORMG4LONGADDR(cdbp, lba); 13320 FORMG4COUNT(cdbp, blockcount); 13321 return (0); 13322 } else if (cp->sc_grpcode == CDB_GROUP0) { 13323 FORMG0ADDR(cdbp, lba); 13324 FORMG0COUNT(cdbp, blockcount); 13325 return (0); 13326 } else if (cp->sc_grpcode == CDB_GROUP5) { 13327 FORMG5ADDR(cdbp, lba); 13328 FORMG5COUNT(cdbp, blockcount); 13329 return (0); 13330 } 13331 13332 /* 13333 * It should be impossible to not match one 13334 * of the CDB types above, so we should never 13335 * reach this point. Set the CDB command byte 13336 * to test-unit-ready to avoid writing 13337 * to somewhere we don't intend. 13338 */ 13339 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13340 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13341 } else { 13342 /* 13343 * Couldn't get scsi_pkt 13344 */ 13345 return (SD_PKT_ALLOC_FAILURE); 13346 } 13347 } 13348 } 13349 13350 /* 13351 * None of the available CDB types were suitable. This really 13352 * should never happen: on a 64 bit system we support 13353 * READ16/WRITE16 which will hold an entire 64 bit disk address 13354 * and on a 32 bit system we will refuse to bind to a device 13355 * larger than 2TB so addresses will never be larger than 32 bits. 13356 */ 13357 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13358 } 13359 13360 #if defined(__i386) || defined(__amd64) 13361 /* 13362 * Function: sd_setup_next_rw_pkt 13363 * 13364 * Description: Setup packet for partial DMA transfers, except for the 13365 * initial transfer. sd_setup_rw_pkt should be used for 13366 * the initial transfer. 13367 * 13368 * Context: Kernel thread and may be called from interrupt context. 13369 */ 13370 13371 int 13372 sd_setup_next_rw_pkt(struct sd_lun *un, 13373 struct scsi_pkt *pktp, struct buf *bp, 13374 diskaddr_t lba, uint32_t blockcount) 13375 { 13376 uchar_t com; 13377 union scsi_cdb *cdbp; 13378 uchar_t cdb_group_id; 13379 13380 ASSERT(pktp != NULL); 13381 ASSERT(pktp->pkt_cdbp != NULL); 13382 13383 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13384 com = cdbp->scc_cmd; 13385 cdb_group_id = CDB_GROUPID(com); 13386 13387 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13388 (cdb_group_id == CDB_GROUPID_1) || 13389 (cdb_group_id == CDB_GROUPID_4) || 13390 (cdb_group_id == CDB_GROUPID_5)); 13391 13392 /* 13393 * Move pkt to the next portion of the xfer. 13394 * func is NULL_FUNC so we do not have to release 13395 * the disk mutex here. 13396 */ 13397 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13398 NULL_FUNC, NULL) == pktp) { 13399 /* Success. Handle partial DMA */ 13400 if (pktp->pkt_resid != 0) { 13401 blockcount -= 13402 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13403 } 13404 13405 cdbp->scc_cmd = com; 13406 SD_FILL_SCSI1_LUN(un, pktp); 13407 if (cdb_group_id == CDB_GROUPID_1) { 13408 FORMG1ADDR(cdbp, lba); 13409 FORMG1COUNT(cdbp, blockcount); 13410 return (0); 13411 } else if (cdb_group_id == CDB_GROUPID_4) { 13412 FORMG4LONGADDR(cdbp, lba); 13413 FORMG4COUNT(cdbp, blockcount); 13414 return (0); 13415 } else if (cdb_group_id == CDB_GROUPID_0) { 13416 FORMG0ADDR(cdbp, lba); 13417 FORMG0COUNT(cdbp, blockcount); 13418 return (0); 13419 } else if (cdb_group_id == CDB_GROUPID_5) { 13420 FORMG5ADDR(cdbp, lba); 13421 FORMG5COUNT(cdbp, blockcount); 13422 return (0); 13423 } 13424 13425 /* Unreachable */ 13426 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13427 } 13428 13429 /* 13430 * Error setting up next portion of cmd transfer. 13431 * Something is definitely very wrong and this 13432 * should not happen. 13433 */ 13434 return (SD_PKT_ALLOC_FAILURE); 13435 } 13436 #endif /* defined(__i386) || defined(__amd64) */ 13437 13438 /* 13439 * Function: sd_initpkt_for_uscsi 13440 * 13441 * Description: Allocate and initialize for transport a scsi_pkt struct, 13442 * based upon the info specified in the given uscsi_cmd struct. 13443 * 13444 * Return Code: SD_PKT_ALLOC_SUCCESS 13445 * SD_PKT_ALLOC_FAILURE 13446 * SD_PKT_ALLOC_FAILURE_NO_DMA 13447 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13448 * 13449 * Context: Kernel thread and may be called from software interrupt context 13450 * as part of a sdrunout callback. This function may not block or 13451 * call routines that block 13452 */ 13453 13454 static int 13455 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13456 { 13457 struct uscsi_cmd *uscmd; 13458 struct sd_xbuf *xp; 13459 struct scsi_pkt *pktp; 13460 struct sd_lun *un; 13461 uint32_t flags = 0; 13462 13463 ASSERT(bp != NULL); 13464 ASSERT(pktpp != NULL); 13465 xp = SD_GET_XBUF(bp); 13466 ASSERT(xp != NULL); 13467 un = SD_GET_UN(bp); 13468 ASSERT(un != NULL); 13469 ASSERT(mutex_owned(SD_MUTEX(un))); 13470 13471 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13472 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13473 ASSERT(uscmd != NULL); 13474 13475 SD_TRACE(SD_LOG_IO_CORE, un, 13476 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13477 13478 /* 13479 * Allocate the scsi_pkt for the command. 13480 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13481 * during scsi_init_pkt time and will continue to use the 13482 * same path as long as the same scsi_pkt is used without 13483 * intervening scsi_dma_free(). Since uscsi command does 13484 * not call scsi_dmafree() before retry failed command, it 13485 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13486 * set such that scsi_vhci can use other available path for 13487 * retry. Besides, ucsci command does not allow DMA breakup, 13488 * so there is no need to set PKT_DMA_PARTIAL flag. 13489 */ 13490 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13491 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13492 sizeof (struct scsi_arq_status), 0, 13493 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13494 sdrunout, (caddr_t)un); 13495 13496 if (pktp == NULL) { 13497 *pktpp = NULL; 13498 /* 13499 * Set the driver state to RWAIT to indicate the driver 13500 * is waiting on resource allocations. The driver will not 13501 * suspend, pm_suspend, or detatch while the state is RWAIT. 13502 */ 13503 New_state(un, SD_STATE_RWAIT); 13504 13505 SD_ERROR(SD_LOG_IO_CORE, un, 13506 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13507 13508 if ((bp->b_flags & B_ERROR) != 0) { 13509 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13510 } 13511 return (SD_PKT_ALLOC_FAILURE); 13512 } 13513 13514 /* 13515 * We do not do DMA breakup for USCSI commands, so return failure 13516 * here if all the needed DMA resources were not allocated. 13517 */ 13518 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13519 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13520 scsi_destroy_pkt(pktp); 13521 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13522 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13523 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13524 } 13525 13526 /* Init the cdb from the given uscsi struct */ 13527 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13528 uscmd->uscsi_cdb[0], 0, 0, 0); 13529 13530 SD_FILL_SCSI1_LUN(un, pktp); 13531 13532 /* 13533 * Set up the optional USCSI flags. See the uscsi (7I) man page 13534 * for listing of the supported flags. 13535 */ 13536 13537 if (uscmd->uscsi_flags & USCSI_SILENT) { 13538 flags |= FLAG_SILENT; 13539 } 13540 13541 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13542 flags |= FLAG_DIAGNOSE; 13543 } 13544 13545 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13546 flags |= FLAG_ISOLATE; 13547 } 13548 13549 if (un->un_f_is_fibre == FALSE) { 13550 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13551 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13552 } 13553 } 13554 13555 /* 13556 * Set the pkt flags here so we save time later. 13557 * Note: These flags are NOT in the uscsi man page!!! 13558 */ 13559 if (uscmd->uscsi_flags & USCSI_HEAD) { 13560 flags |= FLAG_HEAD; 13561 } 13562 13563 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13564 flags |= FLAG_NOINTR; 13565 } 13566 13567 /* 13568 * For tagged queueing, things get a bit complicated. 13569 * Check first for head of queue and last for ordered queue. 13570 * If neither head nor order, use the default driver tag flags. 13571 */ 13572 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13573 if (uscmd->uscsi_flags & USCSI_HTAG) { 13574 flags |= FLAG_HTAG; 13575 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13576 flags |= FLAG_OTAG; 13577 } else { 13578 flags |= un->un_tagflags & FLAG_TAGMASK; 13579 } 13580 } 13581 13582 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13583 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13584 } 13585 13586 pktp->pkt_flags = flags; 13587 13588 /* Copy the caller's CDB into the pkt... */ 13589 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13590 13591 if (uscmd->uscsi_timeout == 0) { 13592 pktp->pkt_time = un->un_uscsi_timeout; 13593 } else { 13594 pktp->pkt_time = uscmd->uscsi_timeout; 13595 } 13596 13597 /* need it later to identify USCSI request in sdintr */ 13598 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13599 13600 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13601 13602 pktp->pkt_private = bp; 13603 pktp->pkt_comp = sdintr; 13604 *pktpp = pktp; 13605 13606 SD_TRACE(SD_LOG_IO_CORE, un, 13607 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13608 13609 return (SD_PKT_ALLOC_SUCCESS); 13610 } 13611 13612 13613 /* 13614 * Function: sd_destroypkt_for_uscsi 13615 * 13616 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13617 * IOs.. Also saves relevant info into the associated uscsi_cmd 13618 * struct. 13619 * 13620 * Context: May be called under interrupt context 13621 */ 13622 13623 static void 13624 sd_destroypkt_for_uscsi(struct buf *bp) 13625 { 13626 struct uscsi_cmd *uscmd; 13627 struct sd_xbuf *xp; 13628 struct scsi_pkt *pktp; 13629 struct sd_lun *un; 13630 13631 ASSERT(bp != NULL); 13632 xp = SD_GET_XBUF(bp); 13633 ASSERT(xp != NULL); 13634 un = SD_GET_UN(bp); 13635 ASSERT(un != NULL); 13636 ASSERT(!mutex_owned(SD_MUTEX(un))); 13637 pktp = SD_GET_PKTP(bp); 13638 ASSERT(pktp != NULL); 13639 13640 SD_TRACE(SD_LOG_IO_CORE, un, 13641 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13642 13643 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13644 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13645 ASSERT(uscmd != NULL); 13646 13647 /* Save the status and the residual into the uscsi_cmd struct */ 13648 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13649 uscmd->uscsi_resid = bp->b_resid; 13650 13651 /* 13652 * If enabled, copy any saved sense data into the area specified 13653 * by the uscsi command. 13654 */ 13655 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13656 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13657 /* 13658 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13659 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13660 */ 13661 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13662 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13663 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 13664 } 13665 13666 /* We are done with the scsi_pkt; free it now */ 13667 ASSERT(SD_GET_PKTP(bp) != NULL); 13668 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13669 13670 SD_TRACE(SD_LOG_IO_CORE, un, 13671 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13672 } 13673 13674 13675 /* 13676 * Function: sd_bioclone_alloc 13677 * 13678 * Description: Allocate a buf(9S) and init it as per the given buf 13679 * and the various arguments. The associated sd_xbuf 13680 * struct is (nearly) duplicated. The struct buf *bp 13681 * argument is saved in new_xp->xb_private. 13682 * 13683 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13684 * datalen - size of data area for the shadow bp 13685 * blkno - starting LBA 13686 * func - function pointer for b_iodone in the shadow buf. (May 13687 * be NULL if none.) 13688 * 13689 * Return Code: Pointer to allocates buf(9S) struct 13690 * 13691 * Context: Can sleep. 13692 */ 13693 13694 static struct buf * 13695 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13696 daddr_t blkno, int (*func)(struct buf *)) 13697 { 13698 struct sd_lun *un; 13699 struct sd_xbuf *xp; 13700 struct sd_xbuf *new_xp; 13701 struct buf *new_bp; 13702 13703 ASSERT(bp != NULL); 13704 xp = SD_GET_XBUF(bp); 13705 ASSERT(xp != NULL); 13706 un = SD_GET_UN(bp); 13707 ASSERT(un != NULL); 13708 ASSERT(!mutex_owned(SD_MUTEX(un))); 13709 13710 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13711 NULL, KM_SLEEP); 13712 13713 new_bp->b_lblkno = blkno; 13714 13715 /* 13716 * Allocate an xbuf for the shadow bp and copy the contents of the 13717 * original xbuf into it. 13718 */ 13719 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13720 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13721 13722 /* 13723 * The given bp is automatically saved in the xb_private member 13724 * of the new xbuf. Callers are allowed to depend on this. 13725 */ 13726 new_xp->xb_private = bp; 13727 13728 new_bp->b_private = new_xp; 13729 13730 return (new_bp); 13731 } 13732 13733 /* 13734 * Function: sd_shadow_buf_alloc 13735 * 13736 * Description: Allocate a buf(9S) and init it as per the given buf 13737 * and the various arguments. The associated sd_xbuf 13738 * struct is (nearly) duplicated. The struct buf *bp 13739 * argument is saved in new_xp->xb_private. 13740 * 13741 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13742 * datalen - size of data area for the shadow bp 13743 * bflags - B_READ or B_WRITE (pseudo flag) 13744 * blkno - starting LBA 13745 * func - function pointer for b_iodone in the shadow buf. (May 13746 * be NULL if none.) 13747 * 13748 * Return Code: Pointer to allocates buf(9S) struct 13749 * 13750 * Context: Can sleep. 13751 */ 13752 13753 static struct buf * 13754 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13755 daddr_t blkno, int (*func)(struct buf *)) 13756 { 13757 struct sd_lun *un; 13758 struct sd_xbuf *xp; 13759 struct sd_xbuf *new_xp; 13760 struct buf *new_bp; 13761 13762 ASSERT(bp != NULL); 13763 xp = SD_GET_XBUF(bp); 13764 ASSERT(xp != NULL); 13765 un = SD_GET_UN(bp); 13766 ASSERT(un != NULL); 13767 ASSERT(!mutex_owned(SD_MUTEX(un))); 13768 13769 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13770 bp_mapin(bp); 13771 } 13772 13773 bflags &= (B_READ | B_WRITE); 13774 #if defined(__i386) || defined(__amd64) 13775 new_bp = getrbuf(KM_SLEEP); 13776 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13777 new_bp->b_bcount = datalen; 13778 new_bp->b_flags = bp->b_flags | bflags; 13779 #else 13780 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13781 datalen, bflags, SLEEP_FUNC, NULL); 13782 #endif 13783 new_bp->av_forw = NULL; 13784 new_bp->av_back = NULL; 13785 new_bp->b_dev = bp->b_dev; 13786 new_bp->b_blkno = blkno; 13787 new_bp->b_iodone = func; 13788 new_bp->b_edev = bp->b_edev; 13789 new_bp->b_resid = 0; 13790 13791 /* We need to preserve the B_FAILFAST flag */ 13792 if (bp->b_flags & B_FAILFAST) { 13793 new_bp->b_flags |= B_FAILFAST; 13794 } 13795 13796 /* 13797 * Allocate an xbuf for the shadow bp and copy the contents of the 13798 * original xbuf into it. 13799 */ 13800 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13801 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13802 13803 /* Need later to copy data between the shadow buf & original buf! */ 13804 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13805 13806 /* 13807 * The given bp is automatically saved in the xb_private member 13808 * of the new xbuf. Callers are allowed to depend on this. 13809 */ 13810 new_xp->xb_private = bp; 13811 13812 new_bp->b_private = new_xp; 13813 13814 return (new_bp); 13815 } 13816 13817 /* 13818 * Function: sd_bioclone_free 13819 * 13820 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13821 * in the larger than partition operation. 13822 * 13823 * Context: May be called under interrupt context 13824 */ 13825 13826 static void 13827 sd_bioclone_free(struct buf *bp) 13828 { 13829 struct sd_xbuf *xp; 13830 13831 ASSERT(bp != NULL); 13832 xp = SD_GET_XBUF(bp); 13833 ASSERT(xp != NULL); 13834 13835 /* 13836 * Call bp_mapout() before freeing the buf, in case a lower 13837 * layer or HBA had done a bp_mapin(). we must do this here 13838 * as we are the "originator" of the shadow buf. 13839 */ 13840 bp_mapout(bp); 13841 13842 /* 13843 * Null out b_iodone before freeing the bp, to ensure that the driver 13844 * never gets confused by a stale value in this field. (Just a little 13845 * extra defensiveness here.) 13846 */ 13847 bp->b_iodone = NULL; 13848 13849 freerbuf(bp); 13850 13851 kmem_free(xp, sizeof (struct sd_xbuf)); 13852 } 13853 13854 /* 13855 * Function: sd_shadow_buf_free 13856 * 13857 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13858 * 13859 * Context: May be called under interrupt context 13860 */ 13861 13862 static void 13863 sd_shadow_buf_free(struct buf *bp) 13864 { 13865 struct sd_xbuf *xp; 13866 13867 ASSERT(bp != NULL); 13868 xp = SD_GET_XBUF(bp); 13869 ASSERT(xp != NULL); 13870 13871 #if defined(__sparc) 13872 /* 13873 * Call bp_mapout() before freeing the buf, in case a lower 13874 * layer or HBA had done a bp_mapin(). we must do this here 13875 * as we are the "originator" of the shadow buf. 13876 */ 13877 bp_mapout(bp); 13878 #endif 13879 13880 /* 13881 * Null out b_iodone before freeing the bp, to ensure that the driver 13882 * never gets confused by a stale value in this field. (Just a little 13883 * extra defensiveness here.) 13884 */ 13885 bp->b_iodone = NULL; 13886 13887 #if defined(__i386) || defined(__amd64) 13888 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13889 freerbuf(bp); 13890 #else 13891 scsi_free_consistent_buf(bp); 13892 #endif 13893 13894 kmem_free(xp, sizeof (struct sd_xbuf)); 13895 } 13896 13897 13898 /* 13899 * Function: sd_print_transport_rejected_message 13900 * 13901 * Description: This implements the ludicrously complex rules for printing 13902 * a "transport rejected" message. This is to address the 13903 * specific problem of having a flood of this error message 13904 * produced when a failover occurs. 13905 * 13906 * Context: Any. 13907 */ 13908 13909 static void 13910 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13911 int code) 13912 { 13913 ASSERT(un != NULL); 13914 ASSERT(mutex_owned(SD_MUTEX(un))); 13915 ASSERT(xp != NULL); 13916 13917 /* 13918 * Print the "transport rejected" message under the following 13919 * conditions: 13920 * 13921 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13922 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13923 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 13924 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 13925 * scsi_transport(9F) (which indicates that the target might have 13926 * gone off-line). This uses the un->un_tran_fatal_count 13927 * count, which is incremented whenever a TRAN_FATAL_ERROR is 13928 * received, and reset to zero whenver a TRAN_ACCEPT is returned 13929 * from scsi_transport(). 13930 * 13931 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 13932 * the preceeding cases in order for the message to be printed. 13933 */ 13934 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 13935 if ((sd_level_mask & SD_LOGMASK_DIAG) || 13936 (code != TRAN_FATAL_ERROR) || 13937 (un->un_tran_fatal_count == 1)) { 13938 switch (code) { 13939 case TRAN_BADPKT: 13940 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13941 "transport rejected bad packet\n"); 13942 break; 13943 case TRAN_FATAL_ERROR: 13944 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13945 "transport rejected fatal error\n"); 13946 break; 13947 default: 13948 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13949 "transport rejected (%d)\n", code); 13950 break; 13951 } 13952 } 13953 } 13954 } 13955 13956 13957 /* 13958 * Function: sd_add_buf_to_waitq 13959 * 13960 * Description: Add the given buf(9S) struct to the wait queue for the 13961 * instance. If sorting is enabled, then the buf is added 13962 * to the queue via an elevator sort algorithm (a la 13963 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 13964 * If sorting is not enabled, then the buf is just added 13965 * to the end of the wait queue. 13966 * 13967 * Return Code: void 13968 * 13969 * Context: Does not sleep/block, therefore technically can be called 13970 * from any context. However if sorting is enabled then the 13971 * execution time is indeterminate, and may take long if 13972 * the wait queue grows large. 13973 */ 13974 13975 static void 13976 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 13977 { 13978 struct buf *ap; 13979 13980 ASSERT(bp != NULL); 13981 ASSERT(un != NULL); 13982 ASSERT(mutex_owned(SD_MUTEX(un))); 13983 13984 /* If the queue is empty, add the buf as the only entry & return. */ 13985 if (un->un_waitq_headp == NULL) { 13986 ASSERT(un->un_waitq_tailp == NULL); 13987 un->un_waitq_headp = un->un_waitq_tailp = bp; 13988 bp->av_forw = NULL; 13989 return; 13990 } 13991 13992 ASSERT(un->un_waitq_tailp != NULL); 13993 13994 /* 13995 * If sorting is disabled, just add the buf to the tail end of 13996 * the wait queue and return. 13997 */ 13998 if (un->un_f_disksort_disabled) { 13999 un->un_waitq_tailp->av_forw = bp; 14000 un->un_waitq_tailp = bp; 14001 bp->av_forw = NULL; 14002 return; 14003 } 14004 14005 /* 14006 * Sort thru the list of requests currently on the wait queue 14007 * and add the new buf request at the appropriate position. 14008 * 14009 * The un->un_waitq_headp is an activity chain pointer on which 14010 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14011 * first queue holds those requests which are positioned after 14012 * the current SD_GET_BLKNO() (in the first request); the second holds 14013 * requests which came in after their SD_GET_BLKNO() number was passed. 14014 * Thus we implement a one way scan, retracting after reaching 14015 * the end of the drive to the first request on the second 14016 * queue, at which time it becomes the first queue. 14017 * A one-way scan is natural because of the way UNIX read-ahead 14018 * blocks are allocated. 14019 * 14020 * If we lie after the first request, then we must locate the 14021 * second request list and add ourselves to it. 14022 */ 14023 ap = un->un_waitq_headp; 14024 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14025 while (ap->av_forw != NULL) { 14026 /* 14027 * Look for an "inversion" in the (normally 14028 * ascending) block numbers. This indicates 14029 * the start of the second request list. 14030 */ 14031 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14032 /* 14033 * Search the second request list for the 14034 * first request at a larger block number. 14035 * We go before that; however if there is 14036 * no such request, we go at the end. 14037 */ 14038 do { 14039 if (SD_GET_BLKNO(bp) < 14040 SD_GET_BLKNO(ap->av_forw)) { 14041 goto insert; 14042 } 14043 ap = ap->av_forw; 14044 } while (ap->av_forw != NULL); 14045 goto insert; /* after last */ 14046 } 14047 ap = ap->av_forw; 14048 } 14049 14050 /* 14051 * No inversions... we will go after the last, and 14052 * be the first request in the second request list. 14053 */ 14054 goto insert; 14055 } 14056 14057 /* 14058 * Request is at/after the current request... 14059 * sort in the first request list. 14060 */ 14061 while (ap->av_forw != NULL) { 14062 /* 14063 * We want to go after the current request (1) if 14064 * there is an inversion after it (i.e. it is the end 14065 * of the first request list), or (2) if the next 14066 * request is a larger block no. than our request. 14067 */ 14068 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14069 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14070 goto insert; 14071 } 14072 ap = ap->av_forw; 14073 } 14074 14075 /* 14076 * Neither a second list nor a larger request, therefore 14077 * we go at the end of the first list (which is the same 14078 * as the end of the whole schebang). 14079 */ 14080 insert: 14081 bp->av_forw = ap->av_forw; 14082 ap->av_forw = bp; 14083 14084 /* 14085 * If we inserted onto the tail end of the waitq, make sure the 14086 * tail pointer is updated. 14087 */ 14088 if (ap == un->un_waitq_tailp) { 14089 un->un_waitq_tailp = bp; 14090 } 14091 } 14092 14093 14094 /* 14095 * Function: sd_start_cmds 14096 * 14097 * Description: Remove and transport cmds from the driver queues. 14098 * 14099 * Arguments: un - pointer to the unit (soft state) struct for the target. 14100 * 14101 * immed_bp - ptr to a buf to be transported immediately. Only 14102 * the immed_bp is transported; bufs on the waitq are not 14103 * processed and the un_retry_bp is not checked. If immed_bp is 14104 * NULL, then normal queue processing is performed. 14105 * 14106 * Context: May be called from kernel thread context, interrupt context, 14107 * or runout callback context. This function may not block or 14108 * call routines that block. 14109 */ 14110 14111 static void 14112 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14113 { 14114 struct sd_xbuf *xp; 14115 struct buf *bp; 14116 void (*statp)(kstat_io_t *); 14117 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14118 void (*saved_statp)(kstat_io_t *); 14119 #endif 14120 int rval; 14121 14122 ASSERT(un != NULL); 14123 ASSERT(mutex_owned(SD_MUTEX(un))); 14124 ASSERT(un->un_ncmds_in_transport >= 0); 14125 ASSERT(un->un_throttle >= 0); 14126 14127 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14128 14129 do { 14130 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14131 saved_statp = NULL; 14132 #endif 14133 14134 /* 14135 * If we are syncing or dumping, fail the command to 14136 * avoid recursively calling back into scsi_transport(). 14137 * The dump I/O itself uses a separate code path so this 14138 * only prevents non-dump I/O from being sent while dumping. 14139 * File system sync takes place before dumping begins. 14140 * During panic, filesystem I/O is allowed provided 14141 * un_in_callback is <= 1. This is to prevent recursion 14142 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14143 * sd_start_cmds and so on. See panic.c for more information 14144 * about the states the system can be in during panic. 14145 */ 14146 if ((un->un_state == SD_STATE_DUMPING) || 14147 (ddi_in_panic() && (un->un_in_callback > 1))) { 14148 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14149 "sd_start_cmds: panicking\n"); 14150 goto exit; 14151 } 14152 14153 if ((bp = immed_bp) != NULL) { 14154 /* 14155 * We have a bp that must be transported immediately. 14156 * It's OK to transport the immed_bp here without doing 14157 * the throttle limit check because the immed_bp is 14158 * always used in a retry/recovery case. This means 14159 * that we know we are not at the throttle limit by 14160 * virtue of the fact that to get here we must have 14161 * already gotten a command back via sdintr(). This also 14162 * relies on (1) the command on un_retry_bp preventing 14163 * further commands from the waitq from being issued; 14164 * and (2) the code in sd_retry_command checking the 14165 * throttle limit before issuing a delayed or immediate 14166 * retry. This holds even if the throttle limit is 14167 * currently ratcheted down from its maximum value. 14168 */ 14169 statp = kstat_runq_enter; 14170 if (bp == un->un_retry_bp) { 14171 ASSERT((un->un_retry_statp == NULL) || 14172 (un->un_retry_statp == kstat_waitq_enter) || 14173 (un->un_retry_statp == 14174 kstat_runq_back_to_waitq)); 14175 /* 14176 * If the waitq kstat was incremented when 14177 * sd_set_retry_bp() queued this bp for a retry, 14178 * then we must set up statp so that the waitq 14179 * count will get decremented correctly below. 14180 * Also we must clear un->un_retry_statp to 14181 * ensure that we do not act on a stale value 14182 * in this field. 14183 */ 14184 if ((un->un_retry_statp == kstat_waitq_enter) || 14185 (un->un_retry_statp == 14186 kstat_runq_back_to_waitq)) { 14187 statp = kstat_waitq_to_runq; 14188 } 14189 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14190 saved_statp = un->un_retry_statp; 14191 #endif 14192 un->un_retry_statp = NULL; 14193 14194 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14195 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14196 "un_throttle:%d un_ncmds_in_transport:%d\n", 14197 un, un->un_retry_bp, un->un_throttle, 14198 un->un_ncmds_in_transport); 14199 } else { 14200 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14201 "processing priority bp:0x%p\n", bp); 14202 } 14203 14204 } else if ((bp = un->un_waitq_headp) != NULL) { 14205 /* 14206 * A command on the waitq is ready to go, but do not 14207 * send it if: 14208 * 14209 * (1) the throttle limit has been reached, or 14210 * (2) a retry is pending, or 14211 * (3) a START_STOP_UNIT callback pending, or 14212 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14213 * command is pending. 14214 * 14215 * For all of these conditions, IO processing will 14216 * restart after the condition is cleared. 14217 */ 14218 if (un->un_ncmds_in_transport >= un->un_throttle) { 14219 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14220 "sd_start_cmds: exiting, " 14221 "throttle limit reached!\n"); 14222 goto exit; 14223 } 14224 if (un->un_retry_bp != NULL) { 14225 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14226 "sd_start_cmds: exiting, retry pending!\n"); 14227 goto exit; 14228 } 14229 if (un->un_startstop_timeid != NULL) { 14230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14231 "sd_start_cmds: exiting, " 14232 "START_STOP pending!\n"); 14233 goto exit; 14234 } 14235 if (un->un_direct_priority_timeid != NULL) { 14236 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14237 "sd_start_cmds: exiting, " 14238 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14239 goto exit; 14240 } 14241 14242 /* Dequeue the command */ 14243 un->un_waitq_headp = bp->av_forw; 14244 if (un->un_waitq_headp == NULL) { 14245 un->un_waitq_tailp = NULL; 14246 } 14247 bp->av_forw = NULL; 14248 statp = kstat_waitq_to_runq; 14249 SD_TRACE(SD_LOG_IO_CORE, un, 14250 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14251 14252 } else { 14253 /* No work to do so bail out now */ 14254 SD_TRACE(SD_LOG_IO_CORE, un, 14255 "sd_start_cmds: no more work, exiting!\n"); 14256 goto exit; 14257 } 14258 14259 /* 14260 * Reset the state to normal. This is the mechanism by which 14261 * the state transitions from either SD_STATE_RWAIT or 14262 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14263 * If state is SD_STATE_PM_CHANGING then this command is 14264 * part of the device power control and the state must 14265 * not be put back to normal. Doing so would would 14266 * allow new commands to proceed when they shouldn't, 14267 * the device may be going off. 14268 */ 14269 if ((un->un_state != SD_STATE_SUSPENDED) && 14270 (un->un_state != SD_STATE_PM_CHANGING)) { 14271 New_state(un, SD_STATE_NORMAL); 14272 } 14273 14274 xp = SD_GET_XBUF(bp); 14275 ASSERT(xp != NULL); 14276 14277 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14278 /* 14279 * Allocate the scsi_pkt if we need one, or attach DMA 14280 * resources if we have a scsi_pkt that needs them. The 14281 * latter should only occur for commands that are being 14282 * retried. 14283 */ 14284 if ((xp->xb_pktp == NULL) || 14285 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14286 #else 14287 if (xp->xb_pktp == NULL) { 14288 #endif 14289 /* 14290 * There is no scsi_pkt allocated for this buf. Call 14291 * the initpkt function to allocate & init one. 14292 * 14293 * The scsi_init_pkt runout callback functionality is 14294 * implemented as follows: 14295 * 14296 * 1) The initpkt function always calls 14297 * scsi_init_pkt(9F) with sdrunout specified as the 14298 * callback routine. 14299 * 2) A successful packet allocation is initialized and 14300 * the I/O is transported. 14301 * 3) The I/O associated with an allocation resource 14302 * failure is left on its queue to be retried via 14303 * runout or the next I/O. 14304 * 4) The I/O associated with a DMA error is removed 14305 * from the queue and failed with EIO. Processing of 14306 * the transport queues is also halted to be 14307 * restarted via runout or the next I/O. 14308 * 5) The I/O associated with a CDB size or packet 14309 * size error is removed from the queue and failed 14310 * with EIO. Processing of the transport queues is 14311 * continued. 14312 * 14313 * Note: there is no interface for canceling a runout 14314 * callback. To prevent the driver from detaching or 14315 * suspending while a runout is pending the driver 14316 * state is set to SD_STATE_RWAIT 14317 * 14318 * Note: using the scsi_init_pkt callback facility can 14319 * result in an I/O request persisting at the head of 14320 * the list which cannot be satisfied even after 14321 * multiple retries. In the future the driver may 14322 * implement some kind of maximum runout count before 14323 * failing an I/O. 14324 * 14325 * Note: the use of funcp below may seem superfluous, 14326 * but it helps warlock figure out the correct 14327 * initpkt function calls (see [s]sd.wlcmd). 14328 */ 14329 struct scsi_pkt *pktp; 14330 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14331 14332 ASSERT(bp != un->un_rqs_bp); 14333 14334 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14335 switch ((*funcp)(bp, &pktp)) { 14336 case SD_PKT_ALLOC_SUCCESS: 14337 xp->xb_pktp = pktp; 14338 SD_TRACE(SD_LOG_IO_CORE, un, 14339 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14340 pktp); 14341 goto got_pkt; 14342 14343 case SD_PKT_ALLOC_FAILURE: 14344 /* 14345 * Temporary (hopefully) resource depletion. 14346 * Since retries and RQS commands always have a 14347 * scsi_pkt allocated, these cases should never 14348 * get here. So the only cases this needs to 14349 * handle is a bp from the waitq (which we put 14350 * back onto the waitq for sdrunout), or a bp 14351 * sent as an immed_bp (which we just fail). 14352 */ 14353 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14354 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14355 14356 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14357 14358 if (bp == immed_bp) { 14359 /* 14360 * If SD_XB_DMA_FREED is clear, then 14361 * this is a failure to allocate a 14362 * scsi_pkt, and we must fail the 14363 * command. 14364 */ 14365 if ((xp->xb_pkt_flags & 14366 SD_XB_DMA_FREED) == 0) { 14367 break; 14368 } 14369 14370 /* 14371 * If this immediate command is NOT our 14372 * un_retry_bp, then we must fail it. 14373 */ 14374 if (bp != un->un_retry_bp) { 14375 break; 14376 } 14377 14378 /* 14379 * We get here if this cmd is our 14380 * un_retry_bp that was DMAFREED, but 14381 * scsi_init_pkt() failed to reallocate 14382 * DMA resources when we attempted to 14383 * retry it. This can happen when an 14384 * mpxio failover is in progress, but 14385 * we don't want to just fail the 14386 * command in this case. 14387 * 14388 * Use timeout(9F) to restart it after 14389 * a 100ms delay. We don't want to 14390 * let sdrunout() restart it, because 14391 * sdrunout() is just supposed to start 14392 * commands that are sitting on the 14393 * wait queue. The un_retry_bp stays 14394 * set until the command completes, but 14395 * sdrunout can be called many times 14396 * before that happens. Since sdrunout 14397 * cannot tell if the un_retry_bp is 14398 * already in the transport, it could 14399 * end up calling scsi_transport() for 14400 * the un_retry_bp multiple times. 14401 * 14402 * Also: don't schedule the callback 14403 * if some other callback is already 14404 * pending. 14405 */ 14406 if (un->un_retry_statp == NULL) { 14407 /* 14408 * restore the kstat pointer to 14409 * keep kstat counts coherent 14410 * when we do retry the command. 14411 */ 14412 un->un_retry_statp = 14413 saved_statp; 14414 } 14415 14416 if ((un->un_startstop_timeid == NULL) && 14417 (un->un_retry_timeid == NULL) && 14418 (un->un_direct_priority_timeid == 14419 NULL)) { 14420 14421 un->un_retry_timeid = 14422 timeout( 14423 sd_start_retry_command, 14424 un, SD_RESTART_TIMEOUT); 14425 } 14426 goto exit; 14427 } 14428 14429 #else 14430 if (bp == immed_bp) { 14431 break; /* Just fail the command */ 14432 } 14433 #endif 14434 14435 /* Add the buf back to the head of the waitq */ 14436 bp->av_forw = un->un_waitq_headp; 14437 un->un_waitq_headp = bp; 14438 if (un->un_waitq_tailp == NULL) { 14439 un->un_waitq_tailp = bp; 14440 } 14441 goto exit; 14442 14443 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14444 /* 14445 * HBA DMA resource failure. Fail the command 14446 * and continue processing of the queues. 14447 */ 14448 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14449 "sd_start_cmds: " 14450 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14451 break; 14452 14453 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14454 /* 14455 * Note:x86: Partial DMA mapping not supported 14456 * for USCSI commands, and all the needed DMA 14457 * resources were not allocated. 14458 */ 14459 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14460 "sd_start_cmds: " 14461 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14462 break; 14463 14464 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14465 /* 14466 * Note:x86: Request cannot fit into CDB based 14467 * on lba and len. 14468 */ 14469 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14470 "sd_start_cmds: " 14471 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14472 break; 14473 14474 default: 14475 /* Should NEVER get here! */ 14476 panic("scsi_initpkt error"); 14477 /*NOTREACHED*/ 14478 } 14479 14480 /* 14481 * Fatal error in allocating a scsi_pkt for this buf. 14482 * Update kstats & return the buf with an error code. 14483 * We must use sd_return_failed_command_no_restart() to 14484 * avoid a recursive call back into sd_start_cmds(). 14485 * However this also means that we must keep processing 14486 * the waitq here in order to avoid stalling. 14487 */ 14488 if (statp == kstat_waitq_to_runq) { 14489 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14490 } 14491 sd_return_failed_command_no_restart(un, bp, EIO); 14492 if (bp == immed_bp) { 14493 /* immed_bp is gone by now, so clear this */ 14494 immed_bp = NULL; 14495 } 14496 continue; 14497 } 14498 got_pkt: 14499 if (bp == immed_bp) { 14500 /* goto the head of the class.... */ 14501 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14502 } 14503 14504 un->un_ncmds_in_transport++; 14505 SD_UPDATE_KSTATS(un, statp, bp); 14506 14507 /* 14508 * Call scsi_transport() to send the command to the target. 14509 * According to SCSA architecture, we must drop the mutex here 14510 * before calling scsi_transport() in order to avoid deadlock. 14511 * Note that the scsi_pkt's completion routine can be executed 14512 * (from interrupt context) even before the call to 14513 * scsi_transport() returns. 14514 */ 14515 SD_TRACE(SD_LOG_IO_CORE, un, 14516 "sd_start_cmds: calling scsi_transport()\n"); 14517 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14518 14519 mutex_exit(SD_MUTEX(un)); 14520 rval = scsi_transport(xp->xb_pktp); 14521 mutex_enter(SD_MUTEX(un)); 14522 14523 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14524 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14525 14526 switch (rval) { 14527 case TRAN_ACCEPT: 14528 /* Clear this with every pkt accepted by the HBA */ 14529 un->un_tran_fatal_count = 0; 14530 break; /* Success; try the next cmd (if any) */ 14531 14532 case TRAN_BUSY: 14533 un->un_ncmds_in_transport--; 14534 ASSERT(un->un_ncmds_in_transport >= 0); 14535 14536 /* 14537 * Don't retry request sense, the sense data 14538 * is lost when another request is sent. 14539 * Free up the rqs buf and retry 14540 * the original failed cmd. Update kstat. 14541 */ 14542 if (bp == un->un_rqs_bp) { 14543 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14544 bp = sd_mark_rqs_idle(un, xp); 14545 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14546 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 14547 kstat_waitq_enter); 14548 goto exit; 14549 } 14550 14551 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14552 /* 14553 * Free the DMA resources for the scsi_pkt. This will 14554 * allow mpxio to select another path the next time 14555 * we call scsi_transport() with this scsi_pkt. 14556 * See sdintr() for the rationalization behind this. 14557 */ 14558 if ((un->un_f_is_fibre == TRUE) && 14559 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14560 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14561 scsi_dmafree(xp->xb_pktp); 14562 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14563 } 14564 #endif 14565 14566 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14567 /* 14568 * Commands that are SD_PATH_DIRECT_PRIORITY 14569 * are for error recovery situations. These do 14570 * not use the normal command waitq, so if they 14571 * get a TRAN_BUSY we cannot put them back onto 14572 * the waitq for later retry. One possible 14573 * problem is that there could already be some 14574 * other command on un_retry_bp that is waiting 14575 * for this one to complete, so we would be 14576 * deadlocked if we put this command back onto 14577 * the waitq for later retry (since un_retry_bp 14578 * must complete before the driver gets back to 14579 * commands on the waitq). 14580 * 14581 * To avoid deadlock we must schedule a callback 14582 * that will restart this command after a set 14583 * interval. This should keep retrying for as 14584 * long as the underlying transport keeps 14585 * returning TRAN_BUSY (just like for other 14586 * commands). Use the same timeout interval as 14587 * for the ordinary TRAN_BUSY retry. 14588 */ 14589 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14590 "sd_start_cmds: scsi_transport() returned " 14591 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14592 14593 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14594 un->un_direct_priority_timeid = 14595 timeout(sd_start_direct_priority_command, 14596 bp, SD_BSY_TIMEOUT / 500); 14597 14598 goto exit; 14599 } 14600 14601 /* 14602 * For TRAN_BUSY, we want to reduce the throttle value, 14603 * unless we are retrying a command. 14604 */ 14605 if (bp != un->un_retry_bp) { 14606 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14607 } 14608 14609 /* 14610 * Set up the bp to be tried again 10 ms later. 14611 * Note:x86: Is there a timeout value in the sd_lun 14612 * for this condition? 14613 */ 14614 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 14615 kstat_runq_back_to_waitq); 14616 goto exit; 14617 14618 case TRAN_FATAL_ERROR: 14619 un->un_tran_fatal_count++; 14620 /* FALLTHRU */ 14621 14622 case TRAN_BADPKT: 14623 default: 14624 un->un_ncmds_in_transport--; 14625 ASSERT(un->un_ncmds_in_transport >= 0); 14626 14627 /* 14628 * If this is our REQUEST SENSE command with a 14629 * transport error, we must get back the pointers 14630 * to the original buf, and mark the REQUEST 14631 * SENSE command as "available". 14632 */ 14633 if (bp == un->un_rqs_bp) { 14634 bp = sd_mark_rqs_idle(un, xp); 14635 xp = SD_GET_XBUF(bp); 14636 } else { 14637 /* 14638 * Legacy behavior: do not update transport 14639 * error count for request sense commands. 14640 */ 14641 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14642 } 14643 14644 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14645 sd_print_transport_rejected_message(un, xp, rval); 14646 14647 /* 14648 * We must use sd_return_failed_command_no_restart() to 14649 * avoid a recursive call back into sd_start_cmds(). 14650 * However this also means that we must keep processing 14651 * the waitq here in order to avoid stalling. 14652 */ 14653 sd_return_failed_command_no_restart(un, bp, EIO); 14654 14655 /* 14656 * Notify any threads waiting in sd_ddi_suspend() that 14657 * a command completion has occurred. 14658 */ 14659 if (un->un_state == SD_STATE_SUSPENDED) { 14660 cv_broadcast(&un->un_disk_busy_cv); 14661 } 14662 14663 if (bp == immed_bp) { 14664 /* immed_bp is gone by now, so clear this */ 14665 immed_bp = NULL; 14666 } 14667 break; 14668 } 14669 14670 } while (immed_bp == NULL); 14671 14672 exit: 14673 ASSERT(mutex_owned(SD_MUTEX(un))); 14674 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14675 } 14676 14677 14678 /* 14679 * Function: sd_return_command 14680 * 14681 * Description: Returns a command to its originator (with or without an 14682 * error). Also starts commands waiting to be transported 14683 * to the target. 14684 * 14685 * Context: May be called from interrupt, kernel, or timeout context 14686 */ 14687 14688 static void 14689 sd_return_command(struct sd_lun *un, struct buf *bp) 14690 { 14691 struct sd_xbuf *xp; 14692 #if defined(__i386) || defined(__amd64) 14693 struct scsi_pkt *pktp; 14694 #endif 14695 14696 ASSERT(bp != NULL); 14697 ASSERT(un != NULL); 14698 ASSERT(mutex_owned(SD_MUTEX(un))); 14699 ASSERT(bp != un->un_rqs_bp); 14700 xp = SD_GET_XBUF(bp); 14701 ASSERT(xp != NULL); 14702 14703 #if defined(__i386) || defined(__amd64) 14704 pktp = SD_GET_PKTP(bp); 14705 #endif 14706 14707 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14708 14709 #if defined(__i386) || defined(__amd64) 14710 /* 14711 * Note:x86: check for the "sdrestart failed" case. 14712 */ 14713 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14714 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14715 (xp->xb_pktp->pkt_resid == 0)) { 14716 14717 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14718 /* 14719 * Successfully set up next portion of cmd 14720 * transfer, try sending it 14721 */ 14722 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14723 NULL, NULL, 0, (clock_t)0, NULL); 14724 sd_start_cmds(un, NULL); 14725 return; /* Note:x86: need a return here? */ 14726 } 14727 } 14728 #endif 14729 14730 /* 14731 * If this is the failfast bp, clear it from un_failfast_bp. This 14732 * can happen if upon being re-tried the failfast bp either 14733 * succeeded or encountered another error (possibly even a different 14734 * error than the one that precipitated the failfast state, but in 14735 * that case it would have had to exhaust retries as well). Regardless, 14736 * this should not occur whenever the instance is in the active 14737 * failfast state. 14738 */ 14739 if (bp == un->un_failfast_bp) { 14740 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14741 un->un_failfast_bp = NULL; 14742 } 14743 14744 /* 14745 * Clear the failfast state upon successful completion of ANY cmd. 14746 */ 14747 if (bp->b_error == 0) { 14748 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14749 } 14750 14751 /* 14752 * This is used if the command was retried one or more times. Show that 14753 * we are done with it, and allow processing of the waitq to resume. 14754 */ 14755 if (bp == un->un_retry_bp) { 14756 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14757 "sd_return_command: un:0x%p: " 14758 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14759 un->un_retry_bp = NULL; 14760 un->un_retry_statp = NULL; 14761 } 14762 14763 SD_UPDATE_RDWR_STATS(un, bp); 14764 SD_UPDATE_PARTITION_STATS(un, bp); 14765 14766 switch (un->un_state) { 14767 case SD_STATE_SUSPENDED: 14768 /* 14769 * Notify any threads waiting in sd_ddi_suspend() that 14770 * a command completion has occurred. 14771 */ 14772 cv_broadcast(&un->un_disk_busy_cv); 14773 break; 14774 default: 14775 sd_start_cmds(un, NULL); 14776 break; 14777 } 14778 14779 /* Return this command up the iodone chain to its originator. */ 14780 mutex_exit(SD_MUTEX(un)); 14781 14782 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14783 xp->xb_pktp = NULL; 14784 14785 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14786 14787 ASSERT(!mutex_owned(SD_MUTEX(un))); 14788 mutex_enter(SD_MUTEX(un)); 14789 14790 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14791 } 14792 14793 14794 /* 14795 * Function: sd_return_failed_command 14796 * 14797 * Description: Command completion when an error occurred. 14798 * 14799 * Context: May be called from interrupt context 14800 */ 14801 14802 static void 14803 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14804 { 14805 ASSERT(bp != NULL); 14806 ASSERT(un != NULL); 14807 ASSERT(mutex_owned(SD_MUTEX(un))); 14808 14809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14810 "sd_return_failed_command: entry\n"); 14811 14812 /* 14813 * b_resid could already be nonzero due to a partial data 14814 * transfer, so do not change it here. 14815 */ 14816 SD_BIOERROR(bp, errcode); 14817 14818 sd_return_command(un, bp); 14819 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14820 "sd_return_failed_command: exit\n"); 14821 } 14822 14823 14824 /* 14825 * Function: sd_return_failed_command_no_restart 14826 * 14827 * Description: Same as sd_return_failed_command, but ensures that no 14828 * call back into sd_start_cmds will be issued. 14829 * 14830 * Context: May be called from interrupt context 14831 */ 14832 14833 static void 14834 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14835 int errcode) 14836 { 14837 struct sd_xbuf *xp; 14838 14839 ASSERT(bp != NULL); 14840 ASSERT(un != NULL); 14841 ASSERT(mutex_owned(SD_MUTEX(un))); 14842 xp = SD_GET_XBUF(bp); 14843 ASSERT(xp != NULL); 14844 ASSERT(errcode != 0); 14845 14846 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14847 "sd_return_failed_command_no_restart: entry\n"); 14848 14849 /* 14850 * b_resid could already be nonzero due to a partial data 14851 * transfer, so do not change it here. 14852 */ 14853 SD_BIOERROR(bp, errcode); 14854 14855 /* 14856 * If this is the failfast bp, clear it. This can happen if the 14857 * failfast bp encounterd a fatal error when we attempted to 14858 * re-try it (such as a scsi_transport(9F) failure). However 14859 * we should NOT be in an active failfast state if the failfast 14860 * bp is not NULL. 14861 */ 14862 if (bp == un->un_failfast_bp) { 14863 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14864 un->un_failfast_bp = NULL; 14865 } 14866 14867 if (bp == un->un_retry_bp) { 14868 /* 14869 * This command was retried one or more times. Show that we are 14870 * done with it, and allow processing of the waitq to resume. 14871 */ 14872 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14873 "sd_return_failed_command_no_restart: " 14874 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14875 un->un_retry_bp = NULL; 14876 un->un_retry_statp = NULL; 14877 } 14878 14879 SD_UPDATE_RDWR_STATS(un, bp); 14880 SD_UPDATE_PARTITION_STATS(un, bp); 14881 14882 mutex_exit(SD_MUTEX(un)); 14883 14884 if (xp->xb_pktp != NULL) { 14885 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14886 xp->xb_pktp = NULL; 14887 } 14888 14889 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14890 14891 mutex_enter(SD_MUTEX(un)); 14892 14893 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14894 "sd_return_failed_command_no_restart: exit\n"); 14895 } 14896 14897 14898 /* 14899 * Function: sd_retry_command 14900 * 14901 * Description: queue up a command for retry, or (optionally) fail it 14902 * if retry counts are exhausted. 14903 * 14904 * Arguments: un - Pointer to the sd_lun struct for the target. 14905 * 14906 * bp - Pointer to the buf for the command to be retried. 14907 * 14908 * retry_check_flag - Flag to see which (if any) of the retry 14909 * counts should be decremented/checked. If the indicated 14910 * retry count is exhausted, then the command will not be 14911 * retried; it will be failed instead. This should use a 14912 * value equal to one of the following: 14913 * 14914 * SD_RETRIES_NOCHECK 14915 * SD_RESD_RETRIES_STANDARD 14916 * SD_RETRIES_VICTIM 14917 * 14918 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14919 * if the check should be made to see of FLAG_ISOLATE is set 14920 * in the pkt. If FLAG_ISOLATE is set, then the command is 14921 * not retried, it is simply failed. 14922 * 14923 * user_funcp - Ptr to function to call before dispatching the 14924 * command. May be NULL if no action needs to be performed. 14925 * (Primarily intended for printing messages.) 14926 * 14927 * user_arg - Optional argument to be passed along to 14928 * the user_funcp call. 14929 * 14930 * failure_code - errno return code to set in the bp if the 14931 * command is going to be failed. 14932 * 14933 * retry_delay - Retry delay interval in (clock_t) units. May 14934 * be zero which indicates that the retry should be retried 14935 * immediately (ie, without an intervening delay). 14936 * 14937 * statp - Ptr to kstat function to be updated if the command 14938 * is queued for a delayed retry. May be NULL if no kstat 14939 * update is desired. 14940 * 14941 * Context: May be called from interupt context. 14942 */ 14943 14944 static void 14945 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 14946 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 14947 code), void *user_arg, int failure_code, clock_t retry_delay, 14948 void (*statp)(kstat_io_t *)) 14949 { 14950 struct sd_xbuf *xp; 14951 struct scsi_pkt *pktp; 14952 14953 ASSERT(un != NULL); 14954 ASSERT(mutex_owned(SD_MUTEX(un))); 14955 ASSERT(bp != NULL); 14956 xp = SD_GET_XBUF(bp); 14957 ASSERT(xp != NULL); 14958 pktp = SD_GET_PKTP(bp); 14959 ASSERT(pktp != NULL); 14960 14961 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14962 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 14963 14964 /* 14965 * If we are syncing or dumping, fail the command to avoid 14966 * recursively calling back into scsi_transport(). 14967 */ 14968 if (ddi_in_panic()) { 14969 goto fail_command_no_log; 14970 } 14971 14972 /* 14973 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 14974 * log an error and fail the command. 14975 */ 14976 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14977 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 14978 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 14979 sd_dump_memory(un, SD_LOG_IO, "CDB", 14980 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 14981 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 14982 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 14983 goto fail_command; 14984 } 14985 14986 /* 14987 * If we are suspended, then put the command onto head of the 14988 * wait queue since we don't want to start more commands. 14989 */ 14990 switch (un->un_state) { 14991 case SD_STATE_SUSPENDED: 14992 case SD_STATE_DUMPING: 14993 bp->av_forw = un->un_waitq_headp; 14994 un->un_waitq_headp = bp; 14995 if (un->un_waitq_tailp == NULL) { 14996 un->un_waitq_tailp = bp; 14997 } 14998 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 14999 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15000 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15001 return; 15002 default: 15003 break; 15004 } 15005 15006 /* 15007 * If the caller wants us to check FLAG_ISOLATE, then see if that 15008 * is set; if it is then we do not want to retry the command. 15009 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15010 */ 15011 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15012 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15013 goto fail_command; 15014 } 15015 } 15016 15017 15018 /* 15019 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15020 * command timeout or a selection timeout has occurred. This means 15021 * that we were unable to establish an kind of communication with 15022 * the target, and subsequent retries and/or commands are likely 15023 * to encounter similar results and take a long time to complete. 15024 * 15025 * If this is a failfast error condition, we need to update the 15026 * failfast state, even if this bp does not have B_FAILFAST set. 15027 */ 15028 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15029 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15030 ASSERT(un->un_failfast_bp == NULL); 15031 /* 15032 * If we are already in the active failfast state, and 15033 * another failfast error condition has been detected, 15034 * then fail this command if it has B_FAILFAST set. 15035 * If B_FAILFAST is clear, then maintain the legacy 15036 * behavior of retrying heroically, even tho this will 15037 * take a lot more time to fail the command. 15038 */ 15039 if (bp->b_flags & B_FAILFAST) { 15040 goto fail_command; 15041 } 15042 } else { 15043 /* 15044 * We're not in the active failfast state, but we 15045 * have a failfast error condition, so we must begin 15046 * transition to the next state. We do this regardless 15047 * of whether or not this bp has B_FAILFAST set. 15048 */ 15049 if (un->un_failfast_bp == NULL) { 15050 /* 15051 * This is the first bp to meet a failfast 15052 * condition so save it on un_failfast_bp & 15053 * do normal retry processing. Do not enter 15054 * active failfast state yet. This marks 15055 * entry into the "failfast pending" state. 15056 */ 15057 un->un_failfast_bp = bp; 15058 15059 } else if (un->un_failfast_bp == bp) { 15060 /* 15061 * This is the second time *this* bp has 15062 * encountered a failfast error condition, 15063 * so enter active failfast state & flush 15064 * queues as appropriate. 15065 */ 15066 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15067 un->un_failfast_bp = NULL; 15068 sd_failfast_flushq(un); 15069 15070 /* 15071 * Fail this bp now if B_FAILFAST set; 15072 * otherwise continue with retries. (It would 15073 * be pretty ironic if this bp succeeded on a 15074 * subsequent retry after we just flushed all 15075 * the queues). 15076 */ 15077 if (bp->b_flags & B_FAILFAST) { 15078 goto fail_command; 15079 } 15080 15081 #if !defined(lint) && !defined(__lint) 15082 } else { 15083 /* 15084 * If neither of the preceeding conditionals 15085 * was true, it means that there is some 15086 * *other* bp that has met an inital failfast 15087 * condition and is currently either being 15088 * retried or is waiting to be retried. In 15089 * that case we should perform normal retry 15090 * processing on *this* bp, since there is a 15091 * chance that the current failfast condition 15092 * is transient and recoverable. If that does 15093 * not turn out to be the case, then retries 15094 * will be cleared when the wait queue is 15095 * flushed anyway. 15096 */ 15097 #endif 15098 } 15099 } 15100 } else { 15101 /* 15102 * SD_RETRIES_FAILFAST is clear, which indicates that we 15103 * likely were able to at least establish some level of 15104 * communication with the target and subsequent commands 15105 * and/or retries are likely to get through to the target, 15106 * In this case we want to be aggressive about clearing 15107 * the failfast state. Note that this does not affect 15108 * the "failfast pending" condition. 15109 */ 15110 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15111 } 15112 15113 15114 /* 15115 * Check the specified retry count to see if we can still do 15116 * any retries with this pkt before we should fail it. 15117 */ 15118 switch (retry_check_flag & SD_RETRIES_MASK) { 15119 case SD_RETRIES_VICTIM: 15120 /* 15121 * Check the victim retry count. If exhausted, then fall 15122 * thru & check against the standard retry count. 15123 */ 15124 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15125 /* Increment count & proceed with the retry */ 15126 xp->xb_victim_retry_count++; 15127 break; 15128 } 15129 /* Victim retries exhausted, fall back to std. retries... */ 15130 /* FALLTHRU */ 15131 15132 case SD_RETRIES_STANDARD: 15133 if (xp->xb_retry_count >= un->un_retry_count) { 15134 /* Retries exhausted, fail the command */ 15135 SD_TRACE(SD_LOG_IO_CORE, un, 15136 "sd_retry_command: retries exhausted!\n"); 15137 /* 15138 * update b_resid for failed SCMD_READ & SCMD_WRITE 15139 * commands with nonzero pkt_resid. 15140 */ 15141 if ((pktp->pkt_reason == CMD_CMPLT) && 15142 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15143 (pktp->pkt_resid != 0)) { 15144 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15145 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15146 SD_UPDATE_B_RESID(bp, pktp); 15147 } 15148 } 15149 goto fail_command; 15150 } 15151 xp->xb_retry_count++; 15152 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15153 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15154 break; 15155 15156 case SD_RETRIES_UA: 15157 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15158 /* Retries exhausted, fail the command */ 15159 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15160 "Unit Attention retries exhausted. " 15161 "Check the target.\n"); 15162 goto fail_command; 15163 } 15164 xp->xb_ua_retry_count++; 15165 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15166 "sd_retry_command: retry count:%d\n", 15167 xp->xb_ua_retry_count); 15168 break; 15169 15170 case SD_RETRIES_BUSY: 15171 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15172 /* Retries exhausted, fail the command */ 15173 SD_TRACE(SD_LOG_IO_CORE, un, 15174 "sd_retry_command: retries exhausted!\n"); 15175 goto fail_command; 15176 } 15177 xp->xb_retry_count++; 15178 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15179 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15180 break; 15181 15182 case SD_RETRIES_NOCHECK: 15183 default: 15184 /* No retry count to check. Just proceed with the retry */ 15185 break; 15186 } 15187 15188 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15189 15190 /* 15191 * If we were given a zero timeout, we must attempt to retry the 15192 * command immediately (ie, without a delay). 15193 */ 15194 if (retry_delay == 0) { 15195 /* 15196 * Check some limiting conditions to see if we can actually 15197 * do the immediate retry. If we cannot, then we must 15198 * fall back to queueing up a delayed retry. 15199 */ 15200 if (un->un_ncmds_in_transport >= un->un_throttle) { 15201 /* 15202 * We are at the throttle limit for the target, 15203 * fall back to delayed retry. 15204 */ 15205 retry_delay = SD_BSY_TIMEOUT; 15206 statp = kstat_waitq_enter; 15207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15208 "sd_retry_command: immed. retry hit " 15209 "throttle!\n"); 15210 } else { 15211 /* 15212 * We're clear to proceed with the immediate retry. 15213 * First call the user-provided function (if any) 15214 */ 15215 if (user_funcp != NULL) { 15216 (*user_funcp)(un, bp, user_arg, 15217 SD_IMMEDIATE_RETRY_ISSUED); 15218 } 15219 15220 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15221 "sd_retry_command: issuing immediate retry\n"); 15222 15223 /* 15224 * Call sd_start_cmds() to transport the command to 15225 * the target. 15226 */ 15227 sd_start_cmds(un, bp); 15228 15229 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15230 "sd_retry_command exit\n"); 15231 return; 15232 } 15233 } 15234 15235 /* 15236 * Set up to retry the command after a delay. 15237 * First call the user-provided function (if any) 15238 */ 15239 if (user_funcp != NULL) { 15240 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15241 } 15242 15243 sd_set_retry_bp(un, bp, retry_delay, statp); 15244 15245 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15246 return; 15247 15248 fail_command: 15249 15250 if (user_funcp != NULL) { 15251 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15252 } 15253 15254 fail_command_no_log: 15255 15256 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15257 "sd_retry_command: returning failed command\n"); 15258 15259 sd_return_failed_command(un, bp, failure_code); 15260 15261 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15262 } 15263 15264 15265 /* 15266 * Function: sd_set_retry_bp 15267 * 15268 * Description: Set up the given bp for retry. 15269 * 15270 * Arguments: un - ptr to associated softstate 15271 * bp - ptr to buf(9S) for the command 15272 * retry_delay - time interval before issuing retry (may be 0) 15273 * statp - optional pointer to kstat function 15274 * 15275 * Context: May be called under interrupt context 15276 */ 15277 15278 static void 15279 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15280 void (*statp)(kstat_io_t *)) 15281 { 15282 ASSERT(un != NULL); 15283 ASSERT(mutex_owned(SD_MUTEX(un))); 15284 ASSERT(bp != NULL); 15285 15286 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15287 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15288 15289 /* 15290 * Indicate that the command is being retried. This will not allow any 15291 * other commands on the wait queue to be transported to the target 15292 * until this command has been completed (success or failure). The 15293 * "retry command" is not transported to the target until the given 15294 * time delay expires, unless the user specified a 0 retry_delay. 15295 * 15296 * Note: the timeout(9F) callback routine is what actually calls 15297 * sd_start_cmds() to transport the command, with the exception of a 15298 * zero retry_delay. The only current implementor of a zero retry delay 15299 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15300 */ 15301 if (un->un_retry_bp == NULL) { 15302 ASSERT(un->un_retry_statp == NULL); 15303 un->un_retry_bp = bp; 15304 15305 /* 15306 * If the user has not specified a delay the command should 15307 * be queued and no timeout should be scheduled. 15308 */ 15309 if (retry_delay == 0) { 15310 /* 15311 * Save the kstat pointer that will be used in the 15312 * call to SD_UPDATE_KSTATS() below, so that 15313 * sd_start_cmds() can correctly decrement the waitq 15314 * count when it is time to transport this command. 15315 */ 15316 un->un_retry_statp = statp; 15317 goto done; 15318 } 15319 } 15320 15321 if (un->un_retry_bp == bp) { 15322 /* 15323 * Save the kstat pointer that will be used in the call to 15324 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15325 * correctly decrement the waitq count when it is time to 15326 * transport this command. 15327 */ 15328 un->un_retry_statp = statp; 15329 15330 /* 15331 * Schedule a timeout if: 15332 * 1) The user has specified a delay. 15333 * 2) There is not a START_STOP_UNIT callback pending. 15334 * 15335 * If no delay has been specified, then it is up to the caller 15336 * to ensure that IO processing continues without stalling. 15337 * Effectively, this means that the caller will issue the 15338 * required call to sd_start_cmds(). The START_STOP_UNIT 15339 * callback does this after the START STOP UNIT command has 15340 * completed. In either of these cases we should not schedule 15341 * a timeout callback here. Also don't schedule the timeout if 15342 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15343 */ 15344 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15345 (un->un_direct_priority_timeid == NULL)) { 15346 un->un_retry_timeid = 15347 timeout(sd_start_retry_command, un, retry_delay); 15348 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15349 "sd_set_retry_bp: setting timeout: un: 0x%p" 15350 " bp:0x%p un_retry_timeid:0x%p\n", 15351 un, bp, un->un_retry_timeid); 15352 } 15353 } else { 15354 /* 15355 * We only get in here if there is already another command 15356 * waiting to be retried. In this case, we just put the 15357 * given command onto the wait queue, so it can be transported 15358 * after the current retry command has completed. 15359 * 15360 * Also we have to make sure that if the command at the head 15361 * of the wait queue is the un_failfast_bp, that we do not 15362 * put ahead of it any other commands that are to be retried. 15363 */ 15364 if ((un->un_failfast_bp != NULL) && 15365 (un->un_failfast_bp == un->un_waitq_headp)) { 15366 /* 15367 * Enqueue this command AFTER the first command on 15368 * the wait queue (which is also un_failfast_bp). 15369 */ 15370 bp->av_forw = un->un_waitq_headp->av_forw; 15371 un->un_waitq_headp->av_forw = bp; 15372 if (un->un_waitq_headp == un->un_waitq_tailp) { 15373 un->un_waitq_tailp = bp; 15374 } 15375 } else { 15376 /* Enqueue this command at the head of the waitq. */ 15377 bp->av_forw = un->un_waitq_headp; 15378 un->un_waitq_headp = bp; 15379 if (un->un_waitq_tailp == NULL) { 15380 un->un_waitq_tailp = bp; 15381 } 15382 } 15383 15384 if (statp == NULL) { 15385 statp = kstat_waitq_enter; 15386 } 15387 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15388 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15389 } 15390 15391 done: 15392 if (statp != NULL) { 15393 SD_UPDATE_KSTATS(un, statp, bp); 15394 } 15395 15396 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15397 "sd_set_retry_bp: exit un:0x%p\n", un); 15398 } 15399 15400 15401 /* 15402 * Function: sd_start_retry_command 15403 * 15404 * Description: Start the command that has been waiting on the target's 15405 * retry queue. Called from timeout(9F) context after the 15406 * retry delay interval has expired. 15407 * 15408 * Arguments: arg - pointer to associated softstate for the device. 15409 * 15410 * Context: timeout(9F) thread context. May not sleep. 15411 */ 15412 15413 static void 15414 sd_start_retry_command(void *arg) 15415 { 15416 struct sd_lun *un = arg; 15417 15418 ASSERT(un != NULL); 15419 ASSERT(!mutex_owned(SD_MUTEX(un))); 15420 15421 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15422 "sd_start_retry_command: entry\n"); 15423 15424 mutex_enter(SD_MUTEX(un)); 15425 15426 un->un_retry_timeid = NULL; 15427 15428 if (un->un_retry_bp != NULL) { 15429 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15430 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15431 un, un->un_retry_bp); 15432 sd_start_cmds(un, un->un_retry_bp); 15433 } 15434 15435 mutex_exit(SD_MUTEX(un)); 15436 15437 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15438 "sd_start_retry_command: exit\n"); 15439 } 15440 15441 15442 /* 15443 * Function: sd_start_direct_priority_command 15444 * 15445 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15446 * received TRAN_BUSY when we called scsi_transport() to send it 15447 * to the underlying HBA. This function is called from timeout(9F) 15448 * context after the delay interval has expired. 15449 * 15450 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15451 * 15452 * Context: timeout(9F) thread context. May not sleep. 15453 */ 15454 15455 static void 15456 sd_start_direct_priority_command(void *arg) 15457 { 15458 struct buf *priority_bp = arg; 15459 struct sd_lun *un; 15460 15461 ASSERT(priority_bp != NULL); 15462 un = SD_GET_UN(priority_bp); 15463 ASSERT(un != NULL); 15464 ASSERT(!mutex_owned(SD_MUTEX(un))); 15465 15466 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15467 "sd_start_direct_priority_command: entry\n"); 15468 15469 mutex_enter(SD_MUTEX(un)); 15470 un->un_direct_priority_timeid = NULL; 15471 sd_start_cmds(un, priority_bp); 15472 mutex_exit(SD_MUTEX(un)); 15473 15474 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15475 "sd_start_direct_priority_command: exit\n"); 15476 } 15477 15478 15479 /* 15480 * Function: sd_send_request_sense_command 15481 * 15482 * Description: Sends a REQUEST SENSE command to the target 15483 * 15484 * Context: May be called from interrupt context. 15485 */ 15486 15487 static void 15488 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15489 struct scsi_pkt *pktp) 15490 { 15491 ASSERT(bp != NULL); 15492 ASSERT(un != NULL); 15493 ASSERT(mutex_owned(SD_MUTEX(un))); 15494 15495 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15496 "entry: buf:0x%p\n", bp); 15497 15498 /* 15499 * If we are syncing or dumping, then fail the command to avoid a 15500 * recursive callback into scsi_transport(). Also fail the command 15501 * if we are suspended (legacy behavior). 15502 */ 15503 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15504 (un->un_state == SD_STATE_DUMPING)) { 15505 sd_return_failed_command(un, bp, EIO); 15506 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15507 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15508 return; 15509 } 15510 15511 /* 15512 * Retry the failed command and don't issue the request sense if: 15513 * 1) the sense buf is busy 15514 * 2) we have 1 or more outstanding commands on the target 15515 * (the sense data will be cleared or invalidated any way) 15516 * 15517 * Note: There could be an issue with not checking a retry limit here, 15518 * the problem is determining which retry limit to check. 15519 */ 15520 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15521 /* Don't retry if the command is flagged as non-retryable */ 15522 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15523 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15524 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 15525 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15526 "sd_send_request_sense_command: " 15527 "at full throttle, retrying exit\n"); 15528 } else { 15529 sd_return_failed_command(un, bp, EIO); 15530 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15531 "sd_send_request_sense_command: " 15532 "at full throttle, non-retryable exit\n"); 15533 } 15534 return; 15535 } 15536 15537 sd_mark_rqs_busy(un, bp); 15538 sd_start_cmds(un, un->un_rqs_bp); 15539 15540 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15541 "sd_send_request_sense_command: exit\n"); 15542 } 15543 15544 15545 /* 15546 * Function: sd_mark_rqs_busy 15547 * 15548 * Description: Indicate that the request sense bp for this instance is 15549 * in use. 15550 * 15551 * Context: May be called under interrupt context 15552 */ 15553 15554 static void 15555 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15556 { 15557 struct sd_xbuf *sense_xp; 15558 15559 ASSERT(un != NULL); 15560 ASSERT(bp != NULL); 15561 ASSERT(mutex_owned(SD_MUTEX(un))); 15562 ASSERT(un->un_sense_isbusy == 0); 15563 15564 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15565 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15566 15567 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15568 ASSERT(sense_xp != NULL); 15569 15570 SD_INFO(SD_LOG_IO, un, 15571 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15572 15573 ASSERT(sense_xp->xb_pktp != NULL); 15574 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15575 == (FLAG_SENSING | FLAG_HEAD)); 15576 15577 un->un_sense_isbusy = 1; 15578 un->un_rqs_bp->b_resid = 0; 15579 sense_xp->xb_pktp->pkt_resid = 0; 15580 sense_xp->xb_pktp->pkt_reason = 0; 15581 15582 /* So we can get back the bp at interrupt time! */ 15583 sense_xp->xb_sense_bp = bp; 15584 15585 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15586 15587 /* 15588 * Mark this buf as awaiting sense data. (This is already set in 15589 * the pkt_flags for the RQS packet.) 15590 */ 15591 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15592 15593 sense_xp->xb_retry_count = 0; 15594 sense_xp->xb_victim_retry_count = 0; 15595 sense_xp->xb_ua_retry_count = 0; 15596 sense_xp->xb_dma_resid = 0; 15597 15598 /* Clean up the fields for auto-request sense */ 15599 sense_xp->xb_sense_status = 0; 15600 sense_xp->xb_sense_state = 0; 15601 sense_xp->xb_sense_resid = 0; 15602 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15603 15604 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15605 } 15606 15607 15608 /* 15609 * Function: sd_mark_rqs_idle 15610 * 15611 * Description: SD_MUTEX must be held continuously through this routine 15612 * to prevent reuse of the rqs struct before the caller can 15613 * complete it's processing. 15614 * 15615 * Return Code: Pointer to the RQS buf 15616 * 15617 * Context: May be called under interrupt context 15618 */ 15619 15620 static struct buf * 15621 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15622 { 15623 struct buf *bp; 15624 ASSERT(un != NULL); 15625 ASSERT(sense_xp != NULL); 15626 ASSERT(mutex_owned(SD_MUTEX(un))); 15627 ASSERT(un->un_sense_isbusy != 0); 15628 15629 un->un_sense_isbusy = 0; 15630 bp = sense_xp->xb_sense_bp; 15631 sense_xp->xb_sense_bp = NULL; 15632 15633 /* This pkt is no longer interested in getting sense data */ 15634 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15635 15636 return (bp); 15637 } 15638 15639 15640 15641 /* 15642 * Function: sd_alloc_rqs 15643 * 15644 * Description: Set up the unit to receive auto request sense data 15645 * 15646 * Return Code: DDI_SUCCESS or DDI_FAILURE 15647 * 15648 * Context: Called under attach(9E) context 15649 */ 15650 15651 static int 15652 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15653 { 15654 struct sd_xbuf *xp; 15655 15656 ASSERT(un != NULL); 15657 ASSERT(!mutex_owned(SD_MUTEX(un))); 15658 ASSERT(un->un_rqs_bp == NULL); 15659 ASSERT(un->un_rqs_pktp == NULL); 15660 15661 /* 15662 * First allocate the required buf and scsi_pkt structs, then set up 15663 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15664 */ 15665 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15666 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15667 if (un->un_rqs_bp == NULL) { 15668 return (DDI_FAILURE); 15669 } 15670 15671 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15672 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15673 15674 if (un->un_rqs_pktp == NULL) { 15675 sd_free_rqs(un); 15676 return (DDI_FAILURE); 15677 } 15678 15679 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15680 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15681 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 15682 15683 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15684 15685 /* Set up the other needed members in the ARQ scsi_pkt. */ 15686 un->un_rqs_pktp->pkt_comp = sdintr; 15687 un->un_rqs_pktp->pkt_time = sd_io_time; 15688 un->un_rqs_pktp->pkt_flags |= 15689 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15690 15691 /* 15692 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15693 * provide any intpkt, destroypkt routines as we take care of 15694 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15695 */ 15696 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15697 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15698 xp->xb_pktp = un->un_rqs_pktp; 15699 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15700 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15701 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15702 15703 /* 15704 * Save the pointer to the request sense private bp so it can 15705 * be retrieved in sdintr. 15706 */ 15707 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15708 ASSERT(un->un_rqs_bp->b_private == xp); 15709 15710 /* 15711 * See if the HBA supports auto-request sense for the specified 15712 * target/lun. If it does, then try to enable it (if not already 15713 * enabled). 15714 * 15715 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15716 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15717 * return success. However, in both of these cases ARQ is always 15718 * enabled and scsi_ifgetcap will always return true. The best approach 15719 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15720 * 15721 * The 3rd case is the HBA (adp) always return enabled on 15722 * scsi_ifgetgetcap even when it's not enable, the best approach 15723 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15724 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15725 */ 15726 15727 if (un->un_f_is_fibre == TRUE) { 15728 un->un_f_arq_enabled = TRUE; 15729 } else { 15730 #if defined(__i386) || defined(__amd64) 15731 /* 15732 * Circumvent the Adaptec bug, remove this code when 15733 * the bug is fixed 15734 */ 15735 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15736 #endif 15737 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15738 case 0: 15739 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15740 "sd_alloc_rqs: HBA supports ARQ\n"); 15741 /* 15742 * ARQ is supported by this HBA but currently is not 15743 * enabled. Attempt to enable it and if successful then 15744 * mark this instance as ARQ enabled. 15745 */ 15746 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15747 == 1) { 15748 /* Successfully enabled ARQ in the HBA */ 15749 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15750 "sd_alloc_rqs: ARQ enabled\n"); 15751 un->un_f_arq_enabled = TRUE; 15752 } else { 15753 /* Could not enable ARQ in the HBA */ 15754 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15755 "sd_alloc_rqs: failed ARQ enable\n"); 15756 un->un_f_arq_enabled = FALSE; 15757 } 15758 break; 15759 case 1: 15760 /* 15761 * ARQ is supported by this HBA and is already enabled. 15762 * Just mark ARQ as enabled for this instance. 15763 */ 15764 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15765 "sd_alloc_rqs: ARQ already enabled\n"); 15766 un->un_f_arq_enabled = TRUE; 15767 break; 15768 default: 15769 /* 15770 * ARQ is not supported by this HBA; disable it for this 15771 * instance. 15772 */ 15773 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15774 "sd_alloc_rqs: HBA does not support ARQ\n"); 15775 un->un_f_arq_enabled = FALSE; 15776 break; 15777 } 15778 } 15779 15780 return (DDI_SUCCESS); 15781 } 15782 15783 15784 /* 15785 * Function: sd_free_rqs 15786 * 15787 * Description: Cleanup for the pre-instance RQS command. 15788 * 15789 * Context: Kernel thread context 15790 */ 15791 15792 static void 15793 sd_free_rqs(struct sd_lun *un) 15794 { 15795 ASSERT(un != NULL); 15796 15797 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15798 15799 /* 15800 * If consistent memory is bound to a scsi_pkt, the pkt 15801 * has to be destroyed *before* freeing the consistent memory. 15802 * Don't change the sequence of this operations. 15803 * scsi_destroy_pkt() might access memory, which isn't allowed, 15804 * after it was freed in scsi_free_consistent_buf(). 15805 */ 15806 if (un->un_rqs_pktp != NULL) { 15807 scsi_destroy_pkt(un->un_rqs_pktp); 15808 un->un_rqs_pktp = NULL; 15809 } 15810 15811 if (un->un_rqs_bp != NULL) { 15812 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 15813 scsi_free_consistent_buf(un->un_rqs_bp); 15814 un->un_rqs_bp = NULL; 15815 } 15816 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15817 } 15818 15819 15820 15821 /* 15822 * Function: sd_reduce_throttle 15823 * 15824 * Description: Reduces the maximun # of outstanding commands on a 15825 * target to the current number of outstanding commands. 15826 * Queues a tiemout(9F) callback to restore the limit 15827 * after a specified interval has elapsed. 15828 * Typically used when we get a TRAN_BUSY return code 15829 * back from scsi_transport(). 15830 * 15831 * Arguments: un - ptr to the sd_lun softstate struct 15832 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15833 * 15834 * Context: May be called from interrupt context 15835 */ 15836 15837 static void 15838 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15839 { 15840 ASSERT(un != NULL); 15841 ASSERT(mutex_owned(SD_MUTEX(un))); 15842 ASSERT(un->un_ncmds_in_transport >= 0); 15843 15844 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15845 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15846 un, un->un_throttle, un->un_ncmds_in_transport); 15847 15848 if (un->un_throttle > 1) { 15849 if (un->un_f_use_adaptive_throttle == TRUE) { 15850 switch (throttle_type) { 15851 case SD_THROTTLE_TRAN_BUSY: 15852 if (un->un_busy_throttle == 0) { 15853 un->un_busy_throttle = un->un_throttle; 15854 } 15855 break; 15856 case SD_THROTTLE_QFULL: 15857 un->un_busy_throttle = 0; 15858 break; 15859 default: 15860 ASSERT(FALSE); 15861 } 15862 15863 if (un->un_ncmds_in_transport > 0) { 15864 un->un_throttle = un->un_ncmds_in_transport; 15865 } 15866 15867 } else { 15868 if (un->un_ncmds_in_transport == 0) { 15869 un->un_throttle = 1; 15870 } else { 15871 un->un_throttle = un->un_ncmds_in_transport; 15872 } 15873 } 15874 } 15875 15876 /* Reschedule the timeout if none is currently active */ 15877 if (un->un_reset_throttle_timeid == NULL) { 15878 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15879 un, SD_THROTTLE_RESET_INTERVAL); 15880 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15881 "sd_reduce_throttle: timeout scheduled!\n"); 15882 } 15883 15884 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15885 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15886 } 15887 15888 15889 15890 /* 15891 * Function: sd_restore_throttle 15892 * 15893 * Description: Callback function for timeout(9F). Resets the current 15894 * value of un->un_throttle to its default. 15895 * 15896 * Arguments: arg - pointer to associated softstate for the device. 15897 * 15898 * Context: May be called from interrupt context 15899 */ 15900 15901 static void 15902 sd_restore_throttle(void *arg) 15903 { 15904 struct sd_lun *un = arg; 15905 15906 ASSERT(un != NULL); 15907 ASSERT(!mutex_owned(SD_MUTEX(un))); 15908 15909 mutex_enter(SD_MUTEX(un)); 15910 15911 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15912 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15913 15914 un->un_reset_throttle_timeid = NULL; 15915 15916 if (un->un_f_use_adaptive_throttle == TRUE) { 15917 /* 15918 * If un_busy_throttle is nonzero, then it contains the 15919 * value that un_throttle was when we got a TRAN_BUSY back 15920 * from scsi_transport(). We want to revert back to this 15921 * value. 15922 * 15923 * In the QFULL case, the throttle limit will incrementally 15924 * increase until it reaches max throttle. 15925 */ 15926 if (un->un_busy_throttle > 0) { 15927 un->un_throttle = un->un_busy_throttle; 15928 un->un_busy_throttle = 0; 15929 } else { 15930 /* 15931 * increase throttle by 10% open gate slowly, schedule 15932 * another restore if saved throttle has not been 15933 * reached 15934 */ 15935 short throttle; 15936 if (sd_qfull_throttle_enable) { 15937 throttle = un->un_throttle + 15938 max((un->un_throttle / 10), 1); 15939 un->un_throttle = 15940 (throttle < un->un_saved_throttle) ? 15941 throttle : un->un_saved_throttle; 15942 if (un->un_throttle < un->un_saved_throttle) { 15943 un->un_reset_throttle_timeid = 15944 timeout(sd_restore_throttle, 15945 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 15946 } 15947 } 15948 } 15949 15950 /* 15951 * If un_throttle has fallen below the low-water mark, we 15952 * restore the maximum value here (and allow it to ratchet 15953 * down again if necessary). 15954 */ 15955 if (un->un_throttle < un->un_min_throttle) { 15956 un->un_throttle = un->un_saved_throttle; 15957 } 15958 } else { 15959 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15960 "restoring limit from 0x%x to 0x%x\n", 15961 un->un_throttle, un->un_saved_throttle); 15962 un->un_throttle = un->un_saved_throttle; 15963 } 15964 15965 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15966 "sd_restore_throttle: calling sd_start_cmds!\n"); 15967 15968 sd_start_cmds(un, NULL); 15969 15970 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15971 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 15972 un, un->un_throttle); 15973 15974 mutex_exit(SD_MUTEX(un)); 15975 15976 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 15977 } 15978 15979 /* 15980 * Function: sdrunout 15981 * 15982 * Description: Callback routine for scsi_init_pkt when a resource allocation 15983 * fails. 15984 * 15985 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 15986 * soft state instance. 15987 * 15988 * Return Code: The scsi_init_pkt routine allows for the callback function to 15989 * return a 0 indicating the callback should be rescheduled or a 1 15990 * indicating not to reschedule. This routine always returns 1 15991 * because the driver always provides a callback function to 15992 * scsi_init_pkt. This results in a callback always being scheduled 15993 * (via the scsi_init_pkt callback implementation) if a resource 15994 * failure occurs. 15995 * 15996 * Context: This callback function may not block or call routines that block 15997 * 15998 * Note: Using the scsi_init_pkt callback facility can result in an I/O 15999 * request persisting at the head of the list which cannot be 16000 * satisfied even after multiple retries. In the future the driver 16001 * may implement some time of maximum runout count before failing 16002 * an I/O. 16003 */ 16004 16005 static int 16006 sdrunout(caddr_t arg) 16007 { 16008 struct sd_lun *un = (struct sd_lun *)arg; 16009 16010 ASSERT(un != NULL); 16011 ASSERT(!mutex_owned(SD_MUTEX(un))); 16012 16013 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16014 16015 mutex_enter(SD_MUTEX(un)); 16016 sd_start_cmds(un, NULL); 16017 mutex_exit(SD_MUTEX(un)); 16018 /* 16019 * This callback routine always returns 1 (i.e. do not reschedule) 16020 * because we always specify sdrunout as the callback handler for 16021 * scsi_init_pkt inside the call to sd_start_cmds. 16022 */ 16023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16024 return (1); 16025 } 16026 16027 16028 /* 16029 * Function: sdintr 16030 * 16031 * Description: Completion callback routine for scsi_pkt(9S) structs 16032 * sent to the HBA driver via scsi_transport(9F). 16033 * 16034 * Context: Interrupt context 16035 */ 16036 16037 static void 16038 sdintr(struct scsi_pkt *pktp) 16039 { 16040 struct buf *bp; 16041 struct sd_xbuf *xp; 16042 struct sd_lun *un; 16043 16044 ASSERT(pktp != NULL); 16045 bp = (struct buf *)pktp->pkt_private; 16046 ASSERT(bp != NULL); 16047 xp = SD_GET_XBUF(bp); 16048 ASSERT(xp != NULL); 16049 ASSERT(xp->xb_pktp != NULL); 16050 un = SD_GET_UN(bp); 16051 ASSERT(un != NULL); 16052 ASSERT(!mutex_owned(SD_MUTEX(un))); 16053 16054 #ifdef SD_FAULT_INJECTION 16055 16056 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16057 /* SD FaultInjection */ 16058 sd_faultinjection(pktp); 16059 16060 #endif /* SD_FAULT_INJECTION */ 16061 16062 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16063 " xp:0x%p, un:0x%p\n", bp, xp, un); 16064 16065 mutex_enter(SD_MUTEX(un)); 16066 16067 /* Reduce the count of the #commands currently in transport */ 16068 un->un_ncmds_in_transport--; 16069 ASSERT(un->un_ncmds_in_transport >= 0); 16070 16071 /* Increment counter to indicate that the callback routine is active */ 16072 un->un_in_callback++; 16073 16074 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16075 16076 #ifdef SDDEBUG 16077 if (bp == un->un_retry_bp) { 16078 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16079 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16080 un, un->un_retry_bp, un->un_ncmds_in_transport); 16081 } 16082 #endif 16083 16084 /* 16085 * If pkt_reason is CMD_DEV_GONE, just fail the command 16086 */ 16087 if (pktp->pkt_reason == CMD_DEV_GONE) { 16088 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16089 "Device is gone\n"); 16090 sd_return_failed_command(un, bp, EIO); 16091 goto exit; 16092 } 16093 16094 /* 16095 * First see if the pkt has auto-request sense data with it.... 16096 * Look at the packet state first so we don't take a performance 16097 * hit looking at the arq enabled flag unless absolutely necessary. 16098 */ 16099 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16100 (un->un_f_arq_enabled == TRUE)) { 16101 /* 16102 * The HBA did an auto request sense for this command so check 16103 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16104 * driver command that should not be retried. 16105 */ 16106 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16107 /* 16108 * Save the relevant sense info into the xp for the 16109 * original cmd. 16110 */ 16111 struct scsi_arq_status *asp; 16112 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16113 xp->xb_sense_status = 16114 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16115 xp->xb_sense_state = asp->sts_rqpkt_state; 16116 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16117 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16118 min(sizeof (struct scsi_extended_sense), 16119 SENSE_LENGTH)); 16120 16121 /* fail the command */ 16122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16123 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16124 sd_return_failed_command(un, bp, EIO); 16125 goto exit; 16126 } 16127 16128 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16129 /* 16130 * We want to either retry or fail this command, so free 16131 * the DMA resources here. If we retry the command then 16132 * the DMA resources will be reallocated in sd_start_cmds(). 16133 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16134 * causes the *entire* transfer to start over again from the 16135 * beginning of the request, even for PARTIAL chunks that 16136 * have already transferred successfully. 16137 */ 16138 if ((un->un_f_is_fibre == TRUE) && 16139 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16140 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16141 scsi_dmafree(pktp); 16142 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16143 } 16144 #endif 16145 16146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16147 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16148 16149 sd_handle_auto_request_sense(un, bp, xp, pktp); 16150 goto exit; 16151 } 16152 16153 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16154 if (pktp->pkt_flags & FLAG_SENSING) { 16155 /* This pktp is from the unit's REQUEST_SENSE command */ 16156 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16157 "sdintr: sd_handle_request_sense\n"); 16158 sd_handle_request_sense(un, bp, xp, pktp); 16159 goto exit; 16160 } 16161 16162 /* 16163 * Check to see if the command successfully completed as requested; 16164 * this is the most common case (and also the hot performance path). 16165 * 16166 * Requirements for successful completion are: 16167 * pkt_reason is CMD_CMPLT and packet status is status good. 16168 * In addition: 16169 * - A residual of zero indicates successful completion no matter what 16170 * the command is. 16171 * - If the residual is not zero and the command is not a read or 16172 * write, then it's still defined as successful completion. In other 16173 * words, if the command is a read or write the residual must be 16174 * zero for successful completion. 16175 * - If the residual is not zero and the command is a read or 16176 * write, and it's a USCSICMD, then it's still defined as 16177 * successful completion. 16178 */ 16179 if ((pktp->pkt_reason == CMD_CMPLT) && 16180 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16181 16182 /* 16183 * Since this command is returned with a good status, we 16184 * can reset the count for Sonoma failover. 16185 */ 16186 un->un_sonoma_failure_count = 0; 16187 16188 /* 16189 * Return all USCSI commands on good status 16190 */ 16191 if (pktp->pkt_resid == 0) { 16192 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16193 "sdintr: returning command for resid == 0\n"); 16194 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16195 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16196 SD_UPDATE_B_RESID(bp, pktp); 16197 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16198 "sdintr: returning command for resid != 0\n"); 16199 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16200 SD_UPDATE_B_RESID(bp, pktp); 16201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16202 "sdintr: returning uscsi command\n"); 16203 } else { 16204 goto not_successful; 16205 } 16206 sd_return_command(un, bp); 16207 16208 /* 16209 * Decrement counter to indicate that the callback routine 16210 * is done. 16211 */ 16212 un->un_in_callback--; 16213 ASSERT(un->un_in_callback >= 0); 16214 mutex_exit(SD_MUTEX(un)); 16215 16216 return; 16217 } 16218 16219 not_successful: 16220 16221 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16222 /* 16223 * The following is based upon knowledge of the underlying transport 16224 * and its use of DMA resources. This code should be removed when 16225 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16226 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16227 * and sd_start_cmds(). 16228 * 16229 * Free any DMA resources associated with this command if there 16230 * is a chance it could be retried or enqueued for later retry. 16231 * If we keep the DMA binding then mpxio cannot reissue the 16232 * command on another path whenever a path failure occurs. 16233 * 16234 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16235 * causes the *entire* transfer to start over again from the 16236 * beginning of the request, even for PARTIAL chunks that 16237 * have already transferred successfully. 16238 * 16239 * This is only done for non-uscsi commands (and also skipped for the 16240 * driver's internal RQS command). Also just do this for Fibre Channel 16241 * devices as these are the only ones that support mpxio. 16242 */ 16243 if ((un->un_f_is_fibre == TRUE) && 16244 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16245 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16246 scsi_dmafree(pktp); 16247 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16248 } 16249 #endif 16250 16251 /* 16252 * The command did not successfully complete as requested so check 16253 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16254 * driver command that should not be retried so just return. If 16255 * FLAG_DIAGNOSE is not set the error will be processed below. 16256 */ 16257 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16258 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16259 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16260 /* 16261 * Issue a request sense if a check condition caused the error 16262 * (we handle the auto request sense case above), otherwise 16263 * just fail the command. 16264 */ 16265 if ((pktp->pkt_reason == CMD_CMPLT) && 16266 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16267 sd_send_request_sense_command(un, bp, pktp); 16268 } else { 16269 sd_return_failed_command(un, bp, EIO); 16270 } 16271 goto exit; 16272 } 16273 16274 /* 16275 * The command did not successfully complete as requested so process 16276 * the error, retry, and/or attempt recovery. 16277 */ 16278 switch (pktp->pkt_reason) { 16279 case CMD_CMPLT: 16280 switch (SD_GET_PKT_STATUS(pktp)) { 16281 case STATUS_GOOD: 16282 /* 16283 * The command completed successfully with a non-zero 16284 * residual 16285 */ 16286 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16287 "sdintr: STATUS_GOOD \n"); 16288 sd_pkt_status_good(un, bp, xp, pktp); 16289 break; 16290 16291 case STATUS_CHECK: 16292 case STATUS_TERMINATED: 16293 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16294 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16295 sd_pkt_status_check_condition(un, bp, xp, pktp); 16296 break; 16297 16298 case STATUS_BUSY: 16299 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16300 "sdintr: STATUS_BUSY\n"); 16301 sd_pkt_status_busy(un, bp, xp, pktp); 16302 break; 16303 16304 case STATUS_RESERVATION_CONFLICT: 16305 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16306 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16307 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16308 break; 16309 16310 case STATUS_QFULL: 16311 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16312 "sdintr: STATUS_QFULL\n"); 16313 sd_pkt_status_qfull(un, bp, xp, pktp); 16314 break; 16315 16316 case STATUS_MET: 16317 case STATUS_INTERMEDIATE: 16318 case STATUS_SCSI2: 16319 case STATUS_INTERMEDIATE_MET: 16320 case STATUS_ACA_ACTIVE: 16321 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16322 "Unexpected SCSI status received: 0x%x\n", 16323 SD_GET_PKT_STATUS(pktp)); 16324 sd_return_failed_command(un, bp, EIO); 16325 break; 16326 16327 default: 16328 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16329 "Invalid SCSI status received: 0x%x\n", 16330 SD_GET_PKT_STATUS(pktp)); 16331 sd_return_failed_command(un, bp, EIO); 16332 break; 16333 16334 } 16335 break; 16336 16337 case CMD_INCOMPLETE: 16338 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16339 "sdintr: CMD_INCOMPLETE\n"); 16340 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16341 break; 16342 case CMD_TRAN_ERR: 16343 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16344 "sdintr: CMD_TRAN_ERR\n"); 16345 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16346 break; 16347 case CMD_RESET: 16348 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16349 "sdintr: CMD_RESET \n"); 16350 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16351 break; 16352 case CMD_ABORTED: 16353 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16354 "sdintr: CMD_ABORTED \n"); 16355 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16356 break; 16357 case CMD_TIMEOUT: 16358 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16359 "sdintr: CMD_TIMEOUT\n"); 16360 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16361 break; 16362 case CMD_UNX_BUS_FREE: 16363 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16364 "sdintr: CMD_UNX_BUS_FREE \n"); 16365 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16366 break; 16367 case CMD_TAG_REJECT: 16368 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16369 "sdintr: CMD_TAG_REJECT\n"); 16370 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16371 break; 16372 default: 16373 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16374 "sdintr: default\n"); 16375 sd_pkt_reason_default(un, bp, xp, pktp); 16376 break; 16377 } 16378 16379 exit: 16380 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16381 16382 /* Decrement counter to indicate that the callback routine is done. */ 16383 un->un_in_callback--; 16384 ASSERT(un->un_in_callback >= 0); 16385 16386 /* 16387 * At this point, the pkt has been dispatched, ie, it is either 16388 * being re-tried or has been returned to its caller and should 16389 * not be referenced. 16390 */ 16391 16392 mutex_exit(SD_MUTEX(un)); 16393 } 16394 16395 16396 /* 16397 * Function: sd_print_incomplete_msg 16398 * 16399 * Description: Prints the error message for a CMD_INCOMPLETE error. 16400 * 16401 * Arguments: un - ptr to associated softstate for the device. 16402 * bp - ptr to the buf(9S) for the command. 16403 * arg - message string ptr 16404 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16405 * or SD_NO_RETRY_ISSUED. 16406 * 16407 * Context: May be called under interrupt context 16408 */ 16409 16410 static void 16411 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16412 { 16413 struct scsi_pkt *pktp; 16414 char *msgp; 16415 char *cmdp = arg; 16416 16417 ASSERT(un != NULL); 16418 ASSERT(mutex_owned(SD_MUTEX(un))); 16419 ASSERT(bp != NULL); 16420 ASSERT(arg != NULL); 16421 pktp = SD_GET_PKTP(bp); 16422 ASSERT(pktp != NULL); 16423 16424 switch (code) { 16425 case SD_DELAYED_RETRY_ISSUED: 16426 case SD_IMMEDIATE_RETRY_ISSUED: 16427 msgp = "retrying"; 16428 break; 16429 case SD_NO_RETRY_ISSUED: 16430 default: 16431 msgp = "giving up"; 16432 break; 16433 } 16434 16435 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16436 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16437 "incomplete %s- %s\n", cmdp, msgp); 16438 } 16439 } 16440 16441 16442 16443 /* 16444 * Function: sd_pkt_status_good 16445 * 16446 * Description: Processing for a STATUS_GOOD code in pkt_status. 16447 * 16448 * Context: May be called under interrupt context 16449 */ 16450 16451 static void 16452 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16453 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16454 { 16455 char *cmdp; 16456 16457 ASSERT(un != NULL); 16458 ASSERT(mutex_owned(SD_MUTEX(un))); 16459 ASSERT(bp != NULL); 16460 ASSERT(xp != NULL); 16461 ASSERT(pktp != NULL); 16462 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16463 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16464 ASSERT(pktp->pkt_resid != 0); 16465 16466 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16467 16468 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16469 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16470 case SCMD_READ: 16471 cmdp = "read"; 16472 break; 16473 case SCMD_WRITE: 16474 cmdp = "write"; 16475 break; 16476 default: 16477 SD_UPDATE_B_RESID(bp, pktp); 16478 sd_return_command(un, bp); 16479 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16480 return; 16481 } 16482 16483 /* 16484 * See if we can retry the read/write, preferrably immediately. 16485 * If retries are exhaused, then sd_retry_command() will update 16486 * the b_resid count. 16487 */ 16488 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16489 cmdp, EIO, (clock_t)0, NULL); 16490 16491 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16492 } 16493 16494 16495 16496 16497 16498 /* 16499 * Function: sd_handle_request_sense 16500 * 16501 * Description: Processing for non-auto Request Sense command. 16502 * 16503 * Arguments: un - ptr to associated softstate 16504 * sense_bp - ptr to buf(9S) for the RQS command 16505 * sense_xp - ptr to the sd_xbuf for the RQS command 16506 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16507 * 16508 * Context: May be called under interrupt context 16509 */ 16510 16511 static void 16512 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16513 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16514 { 16515 struct buf *cmd_bp; /* buf for the original command */ 16516 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16517 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16518 16519 ASSERT(un != NULL); 16520 ASSERT(mutex_owned(SD_MUTEX(un))); 16521 ASSERT(sense_bp != NULL); 16522 ASSERT(sense_xp != NULL); 16523 ASSERT(sense_pktp != NULL); 16524 16525 /* 16526 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16527 * RQS command and not the original command. 16528 */ 16529 ASSERT(sense_pktp == un->un_rqs_pktp); 16530 ASSERT(sense_bp == un->un_rqs_bp); 16531 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16532 (FLAG_SENSING | FLAG_HEAD)); 16533 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16534 FLAG_SENSING) == FLAG_SENSING); 16535 16536 /* These are the bp, xp, and pktp for the original command */ 16537 cmd_bp = sense_xp->xb_sense_bp; 16538 cmd_xp = SD_GET_XBUF(cmd_bp); 16539 cmd_pktp = SD_GET_PKTP(cmd_bp); 16540 16541 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16542 /* 16543 * The REQUEST SENSE command failed. Release the REQUEST 16544 * SENSE command for re-use, get back the bp for the original 16545 * command, and attempt to re-try the original command if 16546 * FLAG_DIAGNOSE is not set in the original packet. 16547 */ 16548 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16549 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16550 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16551 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16552 NULL, NULL, EIO, (clock_t)0, NULL); 16553 return; 16554 } 16555 } 16556 16557 /* 16558 * Save the relevant sense info into the xp for the original cmd. 16559 * 16560 * Note: if the request sense failed the state info will be zero 16561 * as set in sd_mark_rqs_busy() 16562 */ 16563 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16564 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16565 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16566 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 16567 16568 /* 16569 * Free up the RQS command.... 16570 * NOTE: 16571 * Must do this BEFORE calling sd_validate_sense_data! 16572 * sd_validate_sense_data may return the original command in 16573 * which case the pkt will be freed and the flags can no 16574 * longer be touched. 16575 * SD_MUTEX is held through this process until the command 16576 * is dispatched based upon the sense data, so there are 16577 * no race conditions. 16578 */ 16579 (void) sd_mark_rqs_idle(un, sense_xp); 16580 16581 /* 16582 * For a retryable command see if we have valid sense data, if so then 16583 * turn it over to sd_decode_sense() to figure out the right course of 16584 * action. Just fail a non-retryable command. 16585 */ 16586 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16587 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 16588 SD_SENSE_DATA_IS_VALID) { 16589 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16590 } 16591 } else { 16592 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16593 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16594 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16595 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16596 sd_return_failed_command(un, cmd_bp, EIO); 16597 } 16598 } 16599 16600 16601 16602 16603 /* 16604 * Function: sd_handle_auto_request_sense 16605 * 16606 * Description: Processing for auto-request sense information. 16607 * 16608 * Arguments: un - ptr to associated softstate 16609 * bp - ptr to buf(9S) for the command 16610 * xp - ptr to the sd_xbuf for the command 16611 * pktp - ptr to the scsi_pkt(9S) for the command 16612 * 16613 * Context: May be called under interrupt context 16614 */ 16615 16616 static void 16617 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16618 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16619 { 16620 struct scsi_arq_status *asp; 16621 16622 ASSERT(un != NULL); 16623 ASSERT(mutex_owned(SD_MUTEX(un))); 16624 ASSERT(bp != NULL); 16625 ASSERT(xp != NULL); 16626 ASSERT(pktp != NULL); 16627 ASSERT(pktp != un->un_rqs_pktp); 16628 ASSERT(bp != un->un_rqs_bp); 16629 16630 /* 16631 * For auto-request sense, we get a scsi_arq_status back from 16632 * the HBA, with the sense data in the sts_sensedata member. 16633 * The pkt_scbp of the packet points to this scsi_arq_status. 16634 */ 16635 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16636 16637 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16638 /* 16639 * The auto REQUEST SENSE failed; see if we can re-try 16640 * the original command. 16641 */ 16642 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16643 "auto request sense failed (reason=%s)\n", 16644 scsi_rname(asp->sts_rqpkt_reason)); 16645 16646 sd_reset_target(un, pktp); 16647 16648 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16649 NULL, NULL, EIO, (clock_t)0, NULL); 16650 return; 16651 } 16652 16653 /* Save the relevant sense info into the xp for the original cmd. */ 16654 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16655 xp->xb_sense_state = asp->sts_rqpkt_state; 16656 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16657 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16658 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 16659 16660 /* 16661 * See if we have valid sense data, if so then turn it over to 16662 * sd_decode_sense() to figure out the right course of action. 16663 */ 16664 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 16665 sd_decode_sense(un, bp, xp, pktp); 16666 } 16667 } 16668 16669 16670 /* 16671 * Function: sd_print_sense_failed_msg 16672 * 16673 * Description: Print log message when RQS has failed. 16674 * 16675 * Arguments: un - ptr to associated softstate 16676 * bp - ptr to buf(9S) for the command 16677 * arg - generic message string ptr 16678 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16679 * or SD_NO_RETRY_ISSUED 16680 * 16681 * Context: May be called from interrupt context 16682 */ 16683 16684 static void 16685 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16686 int code) 16687 { 16688 char *msgp = arg; 16689 16690 ASSERT(un != NULL); 16691 ASSERT(mutex_owned(SD_MUTEX(un))); 16692 ASSERT(bp != NULL); 16693 16694 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16695 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16696 } 16697 } 16698 16699 16700 /* 16701 * Function: sd_validate_sense_data 16702 * 16703 * Description: Check the given sense data for validity. 16704 * If the sense data is not valid, the command will 16705 * be either failed or retried! 16706 * 16707 * Return Code: SD_SENSE_DATA_IS_INVALID 16708 * SD_SENSE_DATA_IS_VALID 16709 * 16710 * Context: May be called from interrupt context 16711 */ 16712 16713 static int 16714 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 16715 { 16716 struct scsi_extended_sense *esp; 16717 struct scsi_pkt *pktp; 16718 size_t actual_len; 16719 char *msgp = NULL; 16720 16721 ASSERT(un != NULL); 16722 ASSERT(mutex_owned(SD_MUTEX(un))); 16723 ASSERT(bp != NULL); 16724 ASSERT(bp != un->un_rqs_bp); 16725 ASSERT(xp != NULL); 16726 16727 pktp = SD_GET_PKTP(bp); 16728 ASSERT(pktp != NULL); 16729 16730 /* 16731 * Check the status of the RQS command (auto or manual). 16732 */ 16733 switch (xp->xb_sense_status & STATUS_MASK) { 16734 case STATUS_GOOD: 16735 break; 16736 16737 case STATUS_RESERVATION_CONFLICT: 16738 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16739 return (SD_SENSE_DATA_IS_INVALID); 16740 16741 case STATUS_BUSY: 16742 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16743 "Busy Status on REQUEST SENSE\n"); 16744 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16745 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16746 return (SD_SENSE_DATA_IS_INVALID); 16747 16748 case STATUS_QFULL: 16749 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16750 "QFULL Status on REQUEST SENSE\n"); 16751 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16752 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 16753 return (SD_SENSE_DATA_IS_INVALID); 16754 16755 case STATUS_CHECK: 16756 case STATUS_TERMINATED: 16757 msgp = "Check Condition on REQUEST SENSE\n"; 16758 goto sense_failed; 16759 16760 default: 16761 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16762 goto sense_failed; 16763 } 16764 16765 /* 16766 * See if we got the minimum required amount of sense data. 16767 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16768 * or less. 16769 */ 16770 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 16771 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16772 (actual_len == 0)) { 16773 msgp = "Request Sense couldn't get sense data\n"; 16774 goto sense_failed; 16775 } 16776 16777 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16778 msgp = "Not enough sense information\n"; 16779 goto sense_failed; 16780 } 16781 16782 /* 16783 * We require the extended sense data 16784 */ 16785 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16786 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16787 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16788 static char tmp[8]; 16789 static char buf[148]; 16790 char *p = (char *)(xp->xb_sense_data); 16791 int i; 16792 16793 mutex_enter(&sd_sense_mutex); 16794 (void) strcpy(buf, "undecodable sense information:"); 16795 for (i = 0; i < actual_len; i++) { 16796 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16797 (void) strcpy(&buf[strlen(buf)], tmp); 16798 } 16799 i = strlen(buf); 16800 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16801 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 16802 mutex_exit(&sd_sense_mutex); 16803 } 16804 /* Note: Legacy behavior, fail the command with no retry */ 16805 sd_return_failed_command(un, bp, EIO); 16806 return (SD_SENSE_DATA_IS_INVALID); 16807 } 16808 16809 /* 16810 * Check that es_code is valid (es_class concatenated with es_code 16811 * make up the "response code" field. es_class will always be 7, so 16812 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16813 * format. 16814 */ 16815 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16816 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16817 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16818 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16819 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16820 goto sense_failed; 16821 } 16822 16823 return (SD_SENSE_DATA_IS_VALID); 16824 16825 sense_failed: 16826 /* 16827 * If the request sense failed (for whatever reason), attempt 16828 * to retry the original command. 16829 */ 16830 #if defined(__i386) || defined(__amd64) 16831 /* 16832 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 16833 * sddef.h for Sparc platform, and x86 uses 1 binary 16834 * for both SCSI/FC. 16835 * The SD_RETRY_DELAY value need to be adjusted here 16836 * when SD_RETRY_DELAY change in sddef.h 16837 */ 16838 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16839 sd_print_sense_failed_msg, msgp, EIO, 16840 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 16841 #else 16842 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16843 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 16844 #endif 16845 16846 return (SD_SENSE_DATA_IS_INVALID); 16847 } 16848 16849 16850 16851 /* 16852 * Function: sd_decode_sense 16853 * 16854 * Description: Take recovery action(s) when SCSI Sense Data is received. 16855 * 16856 * Context: Interrupt context. 16857 */ 16858 16859 static void 16860 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16861 struct scsi_pkt *pktp) 16862 { 16863 struct scsi_extended_sense *esp; 16864 struct scsi_descr_sense_hdr *sdsp; 16865 uint8_t asc, ascq, sense_key; 16866 16867 ASSERT(un != NULL); 16868 ASSERT(mutex_owned(SD_MUTEX(un))); 16869 ASSERT(bp != NULL); 16870 ASSERT(bp != un->un_rqs_bp); 16871 ASSERT(xp != NULL); 16872 ASSERT(pktp != NULL); 16873 16874 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16875 16876 switch (esp->es_code) { 16877 case CODE_FMT_DESCR_CURRENT: 16878 case CODE_FMT_DESCR_DEFERRED: 16879 sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data; 16880 sense_key = sdsp->ds_key; 16881 asc = sdsp->ds_add_code; 16882 ascq = sdsp->ds_qual_code; 16883 break; 16884 case CODE_FMT_VENDOR_SPECIFIC: 16885 case CODE_FMT_FIXED_CURRENT: 16886 case CODE_FMT_FIXED_DEFERRED: 16887 default: 16888 sense_key = esp->es_key; 16889 asc = esp->es_add_code; 16890 ascq = esp->es_qual_code; 16891 break; 16892 } 16893 16894 switch (sense_key) { 16895 case KEY_NO_SENSE: 16896 sd_sense_key_no_sense(un, bp, xp, pktp); 16897 break; 16898 case KEY_RECOVERABLE_ERROR: 16899 sd_sense_key_recoverable_error(un, asc, bp, xp, pktp); 16900 break; 16901 case KEY_NOT_READY: 16902 sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp); 16903 break; 16904 case KEY_MEDIUM_ERROR: 16905 case KEY_HARDWARE_ERROR: 16906 sd_sense_key_medium_or_hardware_error(un, 16907 sense_key, asc, bp, xp, pktp); 16908 break; 16909 case KEY_ILLEGAL_REQUEST: 16910 sd_sense_key_illegal_request(un, bp, xp, pktp); 16911 break; 16912 case KEY_UNIT_ATTENTION: 16913 sd_sense_key_unit_attention(un, asc, bp, xp, pktp); 16914 break; 16915 case KEY_WRITE_PROTECT: 16916 case KEY_VOLUME_OVERFLOW: 16917 case KEY_MISCOMPARE: 16918 sd_sense_key_fail_command(un, bp, xp, pktp); 16919 break; 16920 case KEY_BLANK_CHECK: 16921 sd_sense_key_blank_check(un, bp, xp, pktp); 16922 break; 16923 case KEY_ABORTED_COMMAND: 16924 sd_sense_key_aborted_command(un, bp, xp, pktp); 16925 break; 16926 case KEY_VENDOR_UNIQUE: 16927 case KEY_COPY_ABORTED: 16928 case KEY_EQUAL: 16929 case KEY_RESERVED: 16930 default: 16931 sd_sense_key_default(un, sense_key, bp, xp, pktp); 16932 break; 16933 } 16934 } 16935 16936 16937 /* 16938 * Function: sd_dump_memory 16939 * 16940 * Description: Debug logging routine to print the contents of a user provided 16941 * buffer. The output of the buffer is broken up into 256 byte 16942 * segments due to a size constraint of the scsi_log. 16943 * implementation. 16944 * 16945 * Arguments: un - ptr to softstate 16946 * comp - component mask 16947 * title - "title" string to preceed data when printed 16948 * data - ptr to data block to be printed 16949 * len - size of data block to be printed 16950 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 16951 * 16952 * Context: May be called from interrupt context 16953 */ 16954 16955 #define SD_DUMP_MEMORY_BUF_SIZE 256 16956 16957 static char *sd_dump_format_string[] = { 16958 " 0x%02x", 16959 " %c" 16960 }; 16961 16962 static void 16963 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 16964 int len, int fmt) 16965 { 16966 int i, j; 16967 int avail_count; 16968 int start_offset; 16969 int end_offset; 16970 size_t entry_len; 16971 char *bufp; 16972 char *local_buf; 16973 char *format_string; 16974 16975 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 16976 16977 /* 16978 * In the debug version of the driver, this function is called from a 16979 * number of places which are NOPs in the release driver. 16980 * The debug driver therefore has additional methods of filtering 16981 * debug output. 16982 */ 16983 #ifdef SDDEBUG 16984 /* 16985 * In the debug version of the driver we can reduce the amount of debug 16986 * messages by setting sd_error_level to something other than 16987 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 16988 * sd_component_mask. 16989 */ 16990 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 16991 (sd_error_level != SCSI_ERR_ALL)) { 16992 return; 16993 } 16994 if (((sd_component_mask & comp) == 0) || 16995 (sd_error_level != SCSI_ERR_ALL)) { 16996 return; 16997 } 16998 #else 16999 if (sd_error_level != SCSI_ERR_ALL) { 17000 return; 17001 } 17002 #endif 17003 17004 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17005 bufp = local_buf; 17006 /* 17007 * Available length is the length of local_buf[], minus the 17008 * length of the title string, minus one for the ":", minus 17009 * one for the newline, minus one for the NULL terminator. 17010 * This gives the #bytes available for holding the printed 17011 * values from the given data buffer. 17012 */ 17013 if (fmt == SD_LOG_HEX) { 17014 format_string = sd_dump_format_string[0]; 17015 } else /* SD_LOG_CHAR */ { 17016 format_string = sd_dump_format_string[1]; 17017 } 17018 /* 17019 * Available count is the number of elements from the given 17020 * data buffer that we can fit into the available length. 17021 * This is based upon the size of the format string used. 17022 * Make one entry and find it's size. 17023 */ 17024 (void) sprintf(bufp, format_string, data[0]); 17025 entry_len = strlen(bufp); 17026 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17027 17028 j = 0; 17029 while (j < len) { 17030 bufp = local_buf; 17031 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17032 start_offset = j; 17033 17034 end_offset = start_offset + avail_count; 17035 17036 (void) sprintf(bufp, "%s:", title); 17037 bufp += strlen(bufp); 17038 for (i = start_offset; ((i < end_offset) && (j < len)); 17039 i++, j++) { 17040 (void) sprintf(bufp, format_string, data[i]); 17041 bufp += entry_len; 17042 } 17043 (void) sprintf(bufp, "\n"); 17044 17045 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17046 } 17047 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17048 } 17049 17050 /* 17051 * Function: sd_print_sense_msg 17052 * 17053 * Description: Log a message based upon the given sense data. 17054 * 17055 * Arguments: un - ptr to associated softstate 17056 * bp - ptr to buf(9S) for the command 17057 * arg - ptr to associate sd_sense_info struct 17058 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17059 * or SD_NO_RETRY_ISSUED 17060 * 17061 * Context: May be called from interrupt context 17062 */ 17063 17064 static void 17065 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17066 { 17067 struct sd_xbuf *xp; 17068 struct scsi_pkt *pktp; 17069 struct scsi_extended_sense *sensep; 17070 daddr_t request_blkno; 17071 diskaddr_t err_blkno; 17072 int severity; 17073 int pfa_flag; 17074 int fixed_format = TRUE; 17075 extern struct scsi_key_strings scsi_cmds[]; 17076 17077 ASSERT(un != NULL); 17078 ASSERT(mutex_owned(SD_MUTEX(un))); 17079 ASSERT(bp != NULL); 17080 xp = SD_GET_XBUF(bp); 17081 ASSERT(xp != NULL); 17082 pktp = SD_GET_PKTP(bp); 17083 ASSERT(pktp != NULL); 17084 ASSERT(arg != NULL); 17085 17086 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17087 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17088 17089 if ((code == SD_DELAYED_RETRY_ISSUED) || 17090 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17091 severity = SCSI_ERR_RETRYABLE; 17092 } 17093 17094 /* Use absolute block number for the request block number */ 17095 request_blkno = xp->xb_blkno; 17096 17097 /* 17098 * Now try to get the error block number from the sense data 17099 */ 17100 sensep = (struct scsi_extended_sense *)xp->xb_sense_data; 17101 switch (sensep->es_code) { 17102 case CODE_FMT_DESCR_CURRENT: 17103 case CODE_FMT_DESCR_DEFERRED: 17104 err_blkno = 17105 sd_extract_sense_info_descr( 17106 (struct scsi_descr_sense_hdr *)sensep); 17107 fixed_format = FALSE; 17108 break; 17109 case CODE_FMT_FIXED_CURRENT: 17110 case CODE_FMT_FIXED_DEFERRED: 17111 case CODE_FMT_VENDOR_SPECIFIC: 17112 default: 17113 /* 17114 * With the es_valid bit set, we assume that the error 17115 * blkno is in the sense data. Also, if xp->xb_blkno is 17116 * greater than 0xffffffff then the target *should* have used 17117 * a descriptor sense format (or it shouldn't have set 17118 * the es_valid bit), and we may as well ignore the 17119 * 32-bit value. 17120 */ 17121 if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) { 17122 err_blkno = (diskaddr_t) 17123 ((sensep->es_info_1 << 24) | 17124 (sensep->es_info_2 << 16) | 17125 (sensep->es_info_3 << 8) | 17126 (sensep->es_info_4)); 17127 } else { 17128 err_blkno = (diskaddr_t)-1; 17129 } 17130 break; 17131 } 17132 17133 if (err_blkno == (diskaddr_t)-1) { 17134 /* 17135 * Without the es_valid bit set (for fixed format) or an 17136 * information descriptor (for descriptor format) we cannot 17137 * be certain of the error blkno, so just use the 17138 * request_blkno. 17139 */ 17140 err_blkno = (diskaddr_t)request_blkno; 17141 } else { 17142 /* 17143 * We retrieved the error block number from the information 17144 * portion of the sense data. 17145 * 17146 * For USCSI commands we are better off using the error 17147 * block no. as the requested block no. (This is the best 17148 * we can estimate.) 17149 */ 17150 if ((SD_IS_BUFIO(xp) == FALSE) && 17151 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17152 request_blkno = err_blkno; 17153 } 17154 } 17155 17156 /* 17157 * The following will log the buffer contents for the release driver 17158 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17159 * level is set to verbose. 17160 */ 17161 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17162 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17163 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17164 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17165 17166 if (pfa_flag == FALSE) { 17167 /* This is normally only set for USCSI */ 17168 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17169 return; 17170 } 17171 17172 if ((SD_IS_BUFIO(xp) == TRUE) && 17173 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17174 (severity < sd_error_level))) { 17175 return; 17176 } 17177 } 17178 17179 /* 17180 * If the data is fixed format then check for Sonoma Failover, 17181 * and keep a count of how many failed I/O's. We should not have 17182 * to worry about Sonoma returning descriptor format sense data, 17183 * and asc/ascq are in a different location in descriptor format. 17184 */ 17185 if (fixed_format && 17186 (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) && 17187 (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) { 17188 un->un_sonoma_failure_count++; 17189 if (un->un_sonoma_failure_count > 1) { 17190 return; 17191 } 17192 } 17193 17194 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17195 request_blkno, err_blkno, scsi_cmds, sensep, 17196 un->un_additional_codes, NULL); 17197 } 17198 17199 /* 17200 * Function: sd_extract_sense_info_descr 17201 * 17202 * Description: Retrieve "information" field from descriptor format 17203 * sense data. Iterates through each sense descriptor 17204 * looking for the information descriptor and returns 17205 * the information field from that descriptor. 17206 * 17207 * Context: May be called from interrupt context 17208 */ 17209 17210 static diskaddr_t 17211 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp) 17212 { 17213 diskaddr_t result; 17214 uint8_t *descr_offset; 17215 int valid_sense_length; 17216 struct scsi_information_sense_descr *isd; 17217 17218 /* 17219 * Initialize result to -1 indicating there is no information 17220 * descriptor 17221 */ 17222 result = (diskaddr_t)-1; 17223 17224 /* 17225 * The first descriptor will immediately follow the header 17226 */ 17227 descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */ 17228 17229 /* 17230 * Calculate the amount of valid sense data 17231 */ 17232 valid_sense_length = 17233 min((sizeof (struct scsi_descr_sense_hdr) + 17234 sdsp->ds_addl_sense_length), 17235 SENSE_LENGTH); 17236 17237 /* 17238 * Iterate through the list of descriptors, stopping when we 17239 * run out of sense data 17240 */ 17241 while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <= 17242 (uint8_t *)sdsp + valid_sense_length) { 17243 /* 17244 * Check if this is an information descriptor. We can 17245 * use the scsi_information_sense_descr structure as a 17246 * template sense the first two fields are always the 17247 * same 17248 */ 17249 isd = (struct scsi_information_sense_descr *)descr_offset; 17250 if (isd->isd_descr_type == DESCR_INFORMATION) { 17251 /* 17252 * Found an information descriptor. Copy the 17253 * information field. There will only be one 17254 * information descriptor so we can stop looking. 17255 */ 17256 result = 17257 (((diskaddr_t)isd->isd_information[0] << 56) | 17258 ((diskaddr_t)isd->isd_information[1] << 48) | 17259 ((diskaddr_t)isd->isd_information[2] << 40) | 17260 ((diskaddr_t)isd->isd_information[3] << 32) | 17261 ((diskaddr_t)isd->isd_information[4] << 24) | 17262 ((diskaddr_t)isd->isd_information[5] << 16) | 17263 ((diskaddr_t)isd->isd_information[6] << 8) | 17264 ((diskaddr_t)isd->isd_information[7])); 17265 break; 17266 } 17267 17268 /* 17269 * Get pointer to the next descriptor. The "additional 17270 * length" field holds the length of the descriptor except 17271 * for the "type" and "additional length" fields, so 17272 * we need to add 2 to get the total length. 17273 */ 17274 descr_offset += (isd->isd_addl_length + 2); 17275 } 17276 17277 return (result); 17278 } 17279 17280 /* 17281 * Function: sd_sense_key_no_sense 17282 * 17283 * Description: Recovery action when sense data was not received. 17284 * 17285 * Context: May be called from interrupt context 17286 */ 17287 17288 static void 17289 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17290 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17291 { 17292 struct sd_sense_info si; 17293 17294 ASSERT(un != NULL); 17295 ASSERT(mutex_owned(SD_MUTEX(un))); 17296 ASSERT(bp != NULL); 17297 ASSERT(xp != NULL); 17298 ASSERT(pktp != NULL); 17299 17300 si.ssi_severity = SCSI_ERR_FATAL; 17301 si.ssi_pfa_flag = FALSE; 17302 17303 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17304 17305 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17306 &si, EIO, (clock_t)0, NULL); 17307 } 17308 17309 17310 /* 17311 * Function: sd_sense_key_recoverable_error 17312 * 17313 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17314 * 17315 * Context: May be called from interrupt context 17316 */ 17317 17318 static void 17319 sd_sense_key_recoverable_error(struct sd_lun *un, 17320 uint8_t asc, 17321 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17322 { 17323 struct sd_sense_info si; 17324 17325 ASSERT(un != NULL); 17326 ASSERT(mutex_owned(SD_MUTEX(un))); 17327 ASSERT(bp != NULL); 17328 ASSERT(xp != NULL); 17329 ASSERT(pktp != NULL); 17330 17331 /* 17332 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17333 */ 17334 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17335 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17336 si.ssi_severity = SCSI_ERR_INFO; 17337 si.ssi_pfa_flag = TRUE; 17338 } else { 17339 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17340 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17341 si.ssi_severity = SCSI_ERR_RECOVERED; 17342 si.ssi_pfa_flag = FALSE; 17343 } 17344 17345 if (pktp->pkt_resid == 0) { 17346 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17347 sd_return_command(un, bp); 17348 return; 17349 } 17350 17351 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17352 &si, EIO, (clock_t)0, NULL); 17353 } 17354 17355 17356 17357 17358 /* 17359 * Function: sd_sense_key_not_ready 17360 * 17361 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17362 * 17363 * Context: May be called from interrupt context 17364 */ 17365 17366 static void 17367 sd_sense_key_not_ready(struct sd_lun *un, 17368 uint8_t asc, uint8_t ascq, 17369 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17370 { 17371 struct sd_sense_info si; 17372 17373 ASSERT(un != NULL); 17374 ASSERT(mutex_owned(SD_MUTEX(un))); 17375 ASSERT(bp != NULL); 17376 ASSERT(xp != NULL); 17377 ASSERT(pktp != NULL); 17378 17379 si.ssi_severity = SCSI_ERR_FATAL; 17380 si.ssi_pfa_flag = FALSE; 17381 17382 /* 17383 * Update error stats after first NOT READY error. Disks may have 17384 * been powered down and may need to be restarted. For CDROMs, 17385 * report NOT READY errors only if media is present. 17386 */ 17387 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17388 (xp->xb_retry_count > 0)) { 17389 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17390 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17391 } 17392 17393 /* 17394 * Just fail if the "not ready" retry limit has been reached. 17395 */ 17396 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17397 /* Special check for error message printing for removables. */ 17398 if ((ISREMOVABLE(un)) && (asc == 0x04) && 17399 (ascq >= 0x04)) { 17400 si.ssi_severity = SCSI_ERR_ALL; 17401 } 17402 goto fail_command; 17403 } 17404 17405 /* 17406 * Check the ASC and ASCQ in the sense data as needed, to determine 17407 * what to do. 17408 */ 17409 switch (asc) { 17410 case 0x04: /* LOGICAL UNIT NOT READY */ 17411 /* 17412 * disk drives that don't spin up result in a very long delay 17413 * in format without warning messages. We will log a message 17414 * if the error level is set to verbose. 17415 */ 17416 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17417 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17418 "logical unit not ready, resetting disk\n"); 17419 } 17420 17421 /* 17422 * There are different requirements for CDROMs and disks for 17423 * the number of retries. If a CD-ROM is giving this, it is 17424 * probably reading TOC and is in the process of getting 17425 * ready, so we should keep on trying for a long time to make 17426 * sure that all types of media are taken in account (for 17427 * some media the drive takes a long time to read TOC). For 17428 * disks we do not want to retry this too many times as this 17429 * can cause a long hang in format when the drive refuses to 17430 * spin up (a very common failure). 17431 */ 17432 switch (ascq) { 17433 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17434 /* 17435 * Disk drives frequently refuse to spin up which 17436 * results in a very long hang in format without 17437 * warning messages. 17438 * 17439 * Note: This code preserves the legacy behavior of 17440 * comparing xb_retry_count against zero for fibre 17441 * channel targets instead of comparing against the 17442 * un_reset_retry_count value. The reason for this 17443 * discrepancy has been so utterly lost beneath the 17444 * Sands of Time that even Indiana Jones could not 17445 * find it. 17446 */ 17447 if (un->un_f_is_fibre == TRUE) { 17448 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17449 (xp->xb_retry_count > 0)) && 17450 (un->un_startstop_timeid == NULL)) { 17451 scsi_log(SD_DEVINFO(un), sd_label, 17452 CE_WARN, "logical unit not ready, " 17453 "resetting disk\n"); 17454 sd_reset_target(un, pktp); 17455 } 17456 } else { 17457 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17458 (xp->xb_retry_count > 17459 un->un_reset_retry_count)) && 17460 (un->un_startstop_timeid == NULL)) { 17461 scsi_log(SD_DEVINFO(un), sd_label, 17462 CE_WARN, "logical unit not ready, " 17463 "resetting disk\n"); 17464 sd_reset_target(un, pktp); 17465 } 17466 } 17467 break; 17468 17469 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17470 /* 17471 * If the target is in the process of becoming 17472 * ready, just proceed with the retry. This can 17473 * happen with CD-ROMs that take a long time to 17474 * read TOC after a power cycle or reset. 17475 */ 17476 goto do_retry; 17477 17478 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17479 break; 17480 17481 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17482 /* 17483 * Retries cannot help here so just fail right away. 17484 */ 17485 goto fail_command; 17486 17487 case 0x88: 17488 /* 17489 * Vendor-unique code for T3/T4: it indicates a 17490 * path problem in a mutipathed config, but as far as 17491 * the target driver is concerned it equates to a fatal 17492 * error, so we should just fail the command right away 17493 * (without printing anything to the console). If this 17494 * is not a T3/T4, fall thru to the default recovery 17495 * action. 17496 * T3/T4 is FC only, don't need to check is_fibre 17497 */ 17498 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17499 sd_return_failed_command(un, bp, EIO); 17500 return; 17501 } 17502 /* FALLTHRU */ 17503 17504 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17505 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17506 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17507 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17508 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17509 default: /* Possible future codes in SCSI spec? */ 17510 /* 17511 * For removable-media devices, do not retry if 17512 * ASCQ > 2 as these result mostly from USCSI commands 17513 * on MMC devices issued to check status of an 17514 * operation initiated in immediate mode. Also for 17515 * ASCQ >= 4 do not print console messages as these 17516 * mainly represent a user-initiated operation 17517 * instead of a system failure. 17518 */ 17519 if (ISREMOVABLE(un)) { 17520 si.ssi_severity = SCSI_ERR_ALL; 17521 goto fail_command; 17522 } 17523 break; 17524 } 17525 17526 /* 17527 * As part of our recovery attempt for the NOT READY 17528 * condition, we issue a START STOP UNIT command. However 17529 * we want to wait for a short delay before attempting this 17530 * as there may still be more commands coming back from the 17531 * target with the check condition. To do this we use 17532 * timeout(9F) to call sd_start_stop_unit_callback() after 17533 * the delay interval expires. (sd_start_stop_unit_callback() 17534 * dispatches sd_start_stop_unit_task(), which will issue 17535 * the actual START STOP UNIT command. The delay interval 17536 * is one-half of the delay that we will use to retry the 17537 * command that generated the NOT READY condition. 17538 * 17539 * Note that we could just dispatch sd_start_stop_unit_task() 17540 * from here and allow it to sleep for the delay interval, 17541 * but then we would be tying up the taskq thread 17542 * uncesessarily for the duration of the delay. 17543 * 17544 * Do not issue the START STOP UNIT if the current command 17545 * is already a START STOP UNIT. 17546 */ 17547 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17548 break; 17549 } 17550 17551 /* 17552 * Do not schedule the timeout if one is already pending. 17553 */ 17554 if (un->un_startstop_timeid != NULL) { 17555 SD_INFO(SD_LOG_ERROR, un, 17556 "sd_sense_key_not_ready: restart already issued to" 17557 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17558 ddi_get_instance(SD_DEVINFO(un))); 17559 break; 17560 } 17561 17562 /* 17563 * Schedule the START STOP UNIT command, then queue the command 17564 * for a retry. 17565 * 17566 * Note: A timeout is not scheduled for this retry because we 17567 * want the retry to be serial with the START_STOP_UNIT. The 17568 * retry will be started when the START_STOP_UNIT is completed 17569 * in sd_start_stop_unit_task. 17570 */ 17571 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17572 un, SD_BSY_TIMEOUT / 2); 17573 xp->xb_retry_count++; 17574 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17575 return; 17576 17577 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17578 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17579 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17580 "unit does not respond to selection\n"); 17581 } 17582 break; 17583 17584 case 0x3A: /* MEDIUM NOT PRESENT */ 17585 if (sd_error_level >= SCSI_ERR_FATAL) { 17586 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17587 "Caddy not inserted in drive\n"); 17588 } 17589 17590 sr_ejected(un); 17591 un->un_mediastate = DKIO_EJECTED; 17592 /* The state has changed, inform the media watch routines */ 17593 cv_broadcast(&un->un_state_cv); 17594 /* Just fail if no media is present in the drive. */ 17595 goto fail_command; 17596 17597 default: 17598 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17599 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17600 "Unit not Ready. Additional sense code 0x%x\n", 17601 asc); 17602 } 17603 break; 17604 } 17605 17606 do_retry: 17607 17608 /* 17609 * Retry the command, as some targets may report NOT READY for 17610 * several seconds after being reset. 17611 */ 17612 xp->xb_retry_count++; 17613 si.ssi_severity = SCSI_ERR_RETRYABLE; 17614 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17615 &si, EIO, SD_BSY_TIMEOUT, NULL); 17616 17617 return; 17618 17619 fail_command: 17620 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17621 sd_return_failed_command(un, bp, EIO); 17622 } 17623 17624 17625 17626 /* 17627 * Function: sd_sense_key_medium_or_hardware_error 17628 * 17629 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17630 * sense key. 17631 * 17632 * Context: May be called from interrupt context 17633 */ 17634 17635 static void 17636 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17637 int sense_key, uint8_t asc, 17638 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17639 { 17640 struct sd_sense_info si; 17641 17642 ASSERT(un != NULL); 17643 ASSERT(mutex_owned(SD_MUTEX(un))); 17644 ASSERT(bp != NULL); 17645 ASSERT(xp != NULL); 17646 ASSERT(pktp != NULL); 17647 17648 si.ssi_severity = SCSI_ERR_FATAL; 17649 si.ssi_pfa_flag = FALSE; 17650 17651 if (sense_key == KEY_MEDIUM_ERROR) { 17652 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17653 } 17654 17655 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17656 17657 if ((un->un_reset_retry_count != 0) && 17658 (xp->xb_retry_count == un->un_reset_retry_count)) { 17659 mutex_exit(SD_MUTEX(un)); 17660 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17661 if (un->un_f_allow_bus_device_reset == TRUE) { 17662 17663 boolean_t try_resetting_target = B_TRUE; 17664 17665 /* 17666 * We need to be able to handle specific ASC when we are 17667 * handling a KEY_HARDWARE_ERROR. In particular 17668 * taking the default action of resetting the target may 17669 * not be the appropriate way to attempt recovery. 17670 * Resetting a target because of a single LUN failure 17671 * victimizes all LUNs on that target. 17672 * 17673 * This is true for the LSI arrays, if an LSI 17674 * array controller returns an ASC of 0x84 (LUN Dead) we 17675 * should trust it. 17676 */ 17677 17678 if (sense_key == KEY_HARDWARE_ERROR) { 17679 switch (asc) { 17680 case 0x84: 17681 if (SD_IS_LSI(un)) { 17682 try_resetting_target = B_FALSE; 17683 } 17684 break; 17685 default: 17686 break; 17687 } 17688 } 17689 17690 if (try_resetting_target == B_TRUE) { 17691 int reset_retval = 0; 17692 if (un->un_f_lun_reset_enabled == TRUE) { 17693 SD_TRACE(SD_LOG_IO_CORE, un, 17694 "sd_sense_key_medium_or_hardware_" 17695 "error: issuing RESET_LUN\n"); 17696 reset_retval = 17697 scsi_reset(SD_ADDRESS(un), 17698 RESET_LUN); 17699 } 17700 if (reset_retval == 0) { 17701 SD_TRACE(SD_LOG_IO_CORE, un, 17702 "sd_sense_key_medium_or_hardware_" 17703 "error: issuing RESET_TARGET\n"); 17704 (void) scsi_reset(SD_ADDRESS(un), 17705 RESET_TARGET); 17706 } 17707 } 17708 } 17709 mutex_enter(SD_MUTEX(un)); 17710 } 17711 17712 /* 17713 * This really ought to be a fatal error, but we will retry anyway 17714 * as some drives report this as a spurious error. 17715 */ 17716 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17717 &si, EIO, (clock_t)0, NULL); 17718 } 17719 17720 17721 17722 /* 17723 * Function: sd_sense_key_illegal_request 17724 * 17725 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17726 * 17727 * Context: May be called from interrupt context 17728 */ 17729 17730 static void 17731 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17732 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17733 { 17734 struct sd_sense_info si; 17735 17736 ASSERT(un != NULL); 17737 ASSERT(mutex_owned(SD_MUTEX(un))); 17738 ASSERT(bp != NULL); 17739 ASSERT(xp != NULL); 17740 ASSERT(pktp != NULL); 17741 17742 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17743 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17744 17745 si.ssi_severity = SCSI_ERR_INFO; 17746 si.ssi_pfa_flag = FALSE; 17747 17748 /* Pointless to retry if the target thinks it's an illegal request */ 17749 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17750 sd_return_failed_command(un, bp, EIO); 17751 } 17752 17753 17754 17755 17756 /* 17757 * Function: sd_sense_key_unit_attention 17758 * 17759 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17760 * 17761 * Context: May be called from interrupt context 17762 */ 17763 17764 static void 17765 sd_sense_key_unit_attention(struct sd_lun *un, 17766 uint8_t asc, 17767 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17768 { 17769 /* 17770 * For UNIT ATTENTION we allow retries for one minute. Devices 17771 * like Sonoma can return UNIT ATTENTION close to a minute 17772 * under certain conditions. 17773 */ 17774 int retry_check_flag = SD_RETRIES_UA; 17775 struct sd_sense_info si; 17776 17777 ASSERT(un != NULL); 17778 ASSERT(mutex_owned(SD_MUTEX(un))); 17779 ASSERT(bp != NULL); 17780 ASSERT(xp != NULL); 17781 ASSERT(pktp != NULL); 17782 17783 si.ssi_severity = SCSI_ERR_INFO; 17784 si.ssi_pfa_flag = FALSE; 17785 17786 17787 switch (asc) { 17788 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17789 if (sd_report_pfa != 0) { 17790 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17791 si.ssi_pfa_flag = TRUE; 17792 retry_check_flag = SD_RETRIES_STANDARD; 17793 goto do_retry; 17794 } 17795 break; 17796 17797 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17798 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17799 un->un_resvd_status |= 17800 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17801 } 17802 /* FALLTHRU */ 17803 17804 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17805 if (!ISREMOVABLE(un)) { 17806 break; 17807 } 17808 17809 /* 17810 * When we get a unit attention from a removable-media device, 17811 * it may be in a state that will take a long time to recover 17812 * (e.g., from a reset). Since we are executing in interrupt 17813 * context here, we cannot wait around for the device to come 17814 * back. So hand this command off to sd_media_change_task() 17815 * for deferred processing under taskq thread context. (Note 17816 * that the command still may be failed if a problem is 17817 * encountered at a later time.) 17818 */ 17819 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17820 KM_NOSLEEP) == 0) { 17821 /* 17822 * Cannot dispatch the request so fail the command. 17823 */ 17824 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17825 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17826 si.ssi_severity = SCSI_ERR_FATAL; 17827 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17828 sd_return_failed_command(un, bp, EIO); 17829 } 17830 /* 17831 * Either the command has been successfully dispatched to a 17832 * task Q for retrying, or the dispatch failed. In either case 17833 * do NOT retry again by calling sd_retry_command. This sets up 17834 * two retries of the same command and when one completes and 17835 * frees the resources the other will access freed memory, 17836 * a bad thing. 17837 */ 17838 return; 17839 17840 default: 17841 break; 17842 } 17843 17844 if (!ISREMOVABLE(un)) { 17845 /* 17846 * Do not update these here for removables. For removables 17847 * these stats are updated (1) above if we failed to dispatch 17848 * sd_media_change_task(), or (2) sd_media_change_task() may 17849 * update these later if it encounters an error. 17850 */ 17851 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17852 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17853 } 17854 17855 do_retry: 17856 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17857 EIO, SD_UA_RETRY_DELAY, NULL); 17858 } 17859 17860 17861 17862 /* 17863 * Function: sd_sense_key_fail_command 17864 * 17865 * Description: Use to fail a command when we don't like the sense key that 17866 * was returned. 17867 * 17868 * Context: May be called from interrupt context 17869 */ 17870 17871 static void 17872 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17873 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17874 { 17875 struct sd_sense_info si; 17876 17877 ASSERT(un != NULL); 17878 ASSERT(mutex_owned(SD_MUTEX(un))); 17879 ASSERT(bp != NULL); 17880 ASSERT(xp != NULL); 17881 ASSERT(pktp != NULL); 17882 17883 si.ssi_severity = SCSI_ERR_FATAL; 17884 si.ssi_pfa_flag = FALSE; 17885 17886 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17887 sd_return_failed_command(un, bp, EIO); 17888 } 17889 17890 17891 17892 /* 17893 * Function: sd_sense_key_blank_check 17894 * 17895 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17896 * Has no monetary connotation. 17897 * 17898 * Context: May be called from interrupt context 17899 */ 17900 17901 static void 17902 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17903 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17904 { 17905 struct sd_sense_info si; 17906 17907 ASSERT(un != NULL); 17908 ASSERT(mutex_owned(SD_MUTEX(un))); 17909 ASSERT(bp != NULL); 17910 ASSERT(xp != NULL); 17911 ASSERT(pktp != NULL); 17912 17913 /* 17914 * Blank check is not fatal for removable devices, therefore 17915 * it does not require a console message. 17916 */ 17917 si.ssi_severity = (ISREMOVABLE(un)) ? SCSI_ERR_ALL : SCSI_ERR_FATAL; 17918 si.ssi_pfa_flag = FALSE; 17919 17920 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17921 sd_return_failed_command(un, bp, EIO); 17922 } 17923 17924 17925 17926 17927 /* 17928 * Function: sd_sense_key_aborted_command 17929 * 17930 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 17931 * 17932 * Context: May be called from interrupt context 17933 */ 17934 17935 static void 17936 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 17937 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17938 { 17939 struct sd_sense_info si; 17940 17941 ASSERT(un != NULL); 17942 ASSERT(mutex_owned(SD_MUTEX(un))); 17943 ASSERT(bp != NULL); 17944 ASSERT(xp != NULL); 17945 ASSERT(pktp != NULL); 17946 17947 si.ssi_severity = SCSI_ERR_FATAL; 17948 si.ssi_pfa_flag = FALSE; 17949 17950 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17951 17952 /* 17953 * This really ought to be a fatal error, but we will retry anyway 17954 * as some drives report this as a spurious error. 17955 */ 17956 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17957 &si, EIO, (clock_t)0, NULL); 17958 } 17959 17960 17961 17962 /* 17963 * Function: sd_sense_key_default 17964 * 17965 * Description: Default recovery action for several SCSI sense keys (basically 17966 * attempts a retry). 17967 * 17968 * Context: May be called from interrupt context 17969 */ 17970 17971 static void 17972 sd_sense_key_default(struct sd_lun *un, 17973 int sense_key, 17974 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17975 { 17976 struct sd_sense_info si; 17977 17978 ASSERT(un != NULL); 17979 ASSERT(mutex_owned(SD_MUTEX(un))); 17980 ASSERT(bp != NULL); 17981 ASSERT(xp != NULL); 17982 ASSERT(pktp != NULL); 17983 17984 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17985 17986 /* 17987 * Undecoded sense key. Attempt retries and hope that will fix 17988 * the problem. Otherwise, we're dead. 17989 */ 17990 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17991 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17992 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 17993 } 17994 17995 si.ssi_severity = SCSI_ERR_FATAL; 17996 si.ssi_pfa_flag = FALSE; 17997 17998 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17999 &si, EIO, (clock_t)0, NULL); 18000 } 18001 18002 18003 18004 /* 18005 * Function: sd_print_retry_msg 18006 * 18007 * Description: Print a message indicating the retry action being taken. 18008 * 18009 * Arguments: un - ptr to associated softstate 18010 * bp - ptr to buf(9S) for the command 18011 * arg - not used. 18012 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18013 * or SD_NO_RETRY_ISSUED 18014 * 18015 * Context: May be called from interrupt context 18016 */ 18017 /* ARGSUSED */ 18018 static void 18019 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18020 { 18021 struct sd_xbuf *xp; 18022 struct scsi_pkt *pktp; 18023 char *reasonp; 18024 char *msgp; 18025 18026 ASSERT(un != NULL); 18027 ASSERT(mutex_owned(SD_MUTEX(un))); 18028 ASSERT(bp != NULL); 18029 pktp = SD_GET_PKTP(bp); 18030 ASSERT(pktp != NULL); 18031 xp = SD_GET_XBUF(bp); 18032 ASSERT(xp != NULL); 18033 18034 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18035 mutex_enter(&un->un_pm_mutex); 18036 if ((un->un_state == SD_STATE_SUSPENDED) || 18037 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18038 (pktp->pkt_flags & FLAG_SILENT)) { 18039 mutex_exit(&un->un_pm_mutex); 18040 goto update_pkt_reason; 18041 } 18042 mutex_exit(&un->un_pm_mutex); 18043 18044 /* 18045 * Suppress messages if they are all the same pkt_reason; with 18046 * TQ, many (up to 256) are returned with the same pkt_reason. 18047 * If we are in panic, then suppress the retry messages. 18048 */ 18049 switch (flag) { 18050 case SD_NO_RETRY_ISSUED: 18051 msgp = "giving up"; 18052 break; 18053 case SD_IMMEDIATE_RETRY_ISSUED: 18054 case SD_DELAYED_RETRY_ISSUED: 18055 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18056 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18057 (sd_error_level != SCSI_ERR_ALL))) { 18058 return; 18059 } 18060 msgp = "retrying command"; 18061 break; 18062 default: 18063 goto update_pkt_reason; 18064 } 18065 18066 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18067 scsi_rname(pktp->pkt_reason)); 18068 18069 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18070 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18071 18072 update_pkt_reason: 18073 /* 18074 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18075 * This is to prevent multiple console messages for the same failure 18076 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18077 * when the command is retried successfully because there still may be 18078 * more commands coming back with the same value of pktp->pkt_reason. 18079 */ 18080 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18081 un->un_last_pkt_reason = pktp->pkt_reason; 18082 } 18083 } 18084 18085 18086 /* 18087 * Function: sd_print_cmd_incomplete_msg 18088 * 18089 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18090 * 18091 * Arguments: un - ptr to associated softstate 18092 * bp - ptr to buf(9S) for the command 18093 * arg - passed to sd_print_retry_msg() 18094 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18095 * or SD_NO_RETRY_ISSUED 18096 * 18097 * Context: May be called from interrupt context 18098 */ 18099 18100 static void 18101 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18102 int code) 18103 { 18104 dev_info_t *dip; 18105 18106 ASSERT(un != NULL); 18107 ASSERT(mutex_owned(SD_MUTEX(un))); 18108 ASSERT(bp != NULL); 18109 18110 switch (code) { 18111 case SD_NO_RETRY_ISSUED: 18112 /* Command was failed. Someone turned off this target? */ 18113 if (un->un_state != SD_STATE_OFFLINE) { 18114 /* 18115 * Suppress message if we are detaching and 18116 * device has been disconnected 18117 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18118 * private interface and not part of the DDI 18119 */ 18120 dip = un->un_sd->sd_dev; 18121 if (!(DEVI_IS_DETACHING(dip) && 18122 DEVI_IS_DEVICE_REMOVED(dip))) { 18123 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18124 "disk not responding to selection\n"); 18125 } 18126 New_state(un, SD_STATE_OFFLINE); 18127 } 18128 break; 18129 18130 case SD_DELAYED_RETRY_ISSUED: 18131 case SD_IMMEDIATE_RETRY_ISSUED: 18132 default: 18133 /* Command was successfully queued for retry */ 18134 sd_print_retry_msg(un, bp, arg, code); 18135 break; 18136 } 18137 } 18138 18139 18140 /* 18141 * Function: sd_pkt_reason_cmd_incomplete 18142 * 18143 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18144 * 18145 * Context: May be called from interrupt context 18146 */ 18147 18148 static void 18149 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18150 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18151 { 18152 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18153 18154 ASSERT(un != NULL); 18155 ASSERT(mutex_owned(SD_MUTEX(un))); 18156 ASSERT(bp != NULL); 18157 ASSERT(xp != NULL); 18158 ASSERT(pktp != NULL); 18159 18160 /* Do not do a reset if selection did not complete */ 18161 /* Note: Should this not just check the bit? */ 18162 if (pktp->pkt_state != STATE_GOT_BUS) { 18163 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18164 sd_reset_target(un, pktp); 18165 } 18166 18167 /* 18168 * If the target was not successfully selected, then set 18169 * SD_RETRIES_FAILFAST to indicate that we lost communication 18170 * with the target, and further retries and/or commands are 18171 * likely to take a long time. 18172 */ 18173 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18174 flag |= SD_RETRIES_FAILFAST; 18175 } 18176 18177 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18178 18179 sd_retry_command(un, bp, flag, 18180 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18181 } 18182 18183 18184 18185 /* 18186 * Function: sd_pkt_reason_cmd_tran_err 18187 * 18188 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18189 * 18190 * Context: May be called from interrupt context 18191 */ 18192 18193 static void 18194 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18195 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18196 { 18197 ASSERT(un != NULL); 18198 ASSERT(mutex_owned(SD_MUTEX(un))); 18199 ASSERT(bp != NULL); 18200 ASSERT(xp != NULL); 18201 ASSERT(pktp != NULL); 18202 18203 /* 18204 * Do not reset if we got a parity error, or if 18205 * selection did not complete. 18206 */ 18207 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18208 /* Note: Should this not just check the bit for pkt_state? */ 18209 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18210 (pktp->pkt_state != STATE_GOT_BUS)) { 18211 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18212 sd_reset_target(un, pktp); 18213 } 18214 18215 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18216 18217 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18218 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18219 } 18220 18221 18222 18223 /* 18224 * Function: sd_pkt_reason_cmd_reset 18225 * 18226 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18227 * 18228 * Context: May be called from interrupt context 18229 */ 18230 18231 static void 18232 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18233 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18234 { 18235 ASSERT(un != NULL); 18236 ASSERT(mutex_owned(SD_MUTEX(un))); 18237 ASSERT(bp != NULL); 18238 ASSERT(xp != NULL); 18239 ASSERT(pktp != NULL); 18240 18241 /* The target may still be running the command, so try to reset. */ 18242 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18243 sd_reset_target(un, pktp); 18244 18245 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18246 18247 /* 18248 * If pkt_reason is CMD_RESET chances are that this pkt got 18249 * reset because another target on this bus caused it. The target 18250 * that caused it should get CMD_TIMEOUT with pkt_statistics 18251 * of STAT_TIMEOUT/STAT_DEV_RESET. 18252 */ 18253 18254 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18255 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18256 } 18257 18258 18259 18260 18261 /* 18262 * Function: sd_pkt_reason_cmd_aborted 18263 * 18264 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18265 * 18266 * Context: May be called from interrupt context 18267 */ 18268 18269 static void 18270 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18271 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18272 { 18273 ASSERT(un != NULL); 18274 ASSERT(mutex_owned(SD_MUTEX(un))); 18275 ASSERT(bp != NULL); 18276 ASSERT(xp != NULL); 18277 ASSERT(pktp != NULL); 18278 18279 /* The target may still be running the command, so try to reset. */ 18280 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18281 sd_reset_target(un, pktp); 18282 18283 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18284 18285 /* 18286 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18287 * aborted because another target on this bus caused it. The target 18288 * that caused it should get CMD_TIMEOUT with pkt_statistics 18289 * of STAT_TIMEOUT/STAT_DEV_RESET. 18290 */ 18291 18292 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18293 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18294 } 18295 18296 18297 18298 /* 18299 * Function: sd_pkt_reason_cmd_timeout 18300 * 18301 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18302 * 18303 * Context: May be called from interrupt context 18304 */ 18305 18306 static void 18307 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18308 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18309 { 18310 ASSERT(un != NULL); 18311 ASSERT(mutex_owned(SD_MUTEX(un))); 18312 ASSERT(bp != NULL); 18313 ASSERT(xp != NULL); 18314 ASSERT(pktp != NULL); 18315 18316 18317 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18318 sd_reset_target(un, pktp); 18319 18320 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18321 18322 /* 18323 * A command timeout indicates that we could not establish 18324 * communication with the target, so set SD_RETRIES_FAILFAST 18325 * as further retries/commands are likely to take a long time. 18326 */ 18327 sd_retry_command(un, bp, 18328 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18329 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18330 } 18331 18332 18333 18334 /* 18335 * Function: sd_pkt_reason_cmd_unx_bus_free 18336 * 18337 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18338 * 18339 * Context: May be called from interrupt context 18340 */ 18341 18342 static void 18343 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18344 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18345 { 18346 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18347 18348 ASSERT(un != NULL); 18349 ASSERT(mutex_owned(SD_MUTEX(un))); 18350 ASSERT(bp != NULL); 18351 ASSERT(xp != NULL); 18352 ASSERT(pktp != NULL); 18353 18354 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18355 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18356 18357 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18358 sd_print_retry_msg : NULL; 18359 18360 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18361 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18362 } 18363 18364 18365 /* 18366 * Function: sd_pkt_reason_cmd_tag_reject 18367 * 18368 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18369 * 18370 * Context: May be called from interrupt context 18371 */ 18372 18373 static void 18374 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18375 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18376 { 18377 ASSERT(un != NULL); 18378 ASSERT(mutex_owned(SD_MUTEX(un))); 18379 ASSERT(bp != NULL); 18380 ASSERT(xp != NULL); 18381 ASSERT(pktp != NULL); 18382 18383 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18384 pktp->pkt_flags = 0; 18385 un->un_tagflags = 0; 18386 if (un->un_f_opt_queueing == TRUE) { 18387 un->un_throttle = min(un->un_throttle, 3); 18388 } else { 18389 un->un_throttle = 1; 18390 } 18391 mutex_exit(SD_MUTEX(un)); 18392 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18393 mutex_enter(SD_MUTEX(un)); 18394 18395 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18396 18397 /* Legacy behavior not to check retry counts here. */ 18398 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18399 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18400 } 18401 18402 18403 /* 18404 * Function: sd_pkt_reason_default 18405 * 18406 * Description: Default recovery actions for SCSA pkt_reason values that 18407 * do not have more explicit recovery actions. 18408 * 18409 * Context: May be called from interrupt context 18410 */ 18411 18412 static void 18413 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18414 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18415 { 18416 ASSERT(un != NULL); 18417 ASSERT(mutex_owned(SD_MUTEX(un))); 18418 ASSERT(bp != NULL); 18419 ASSERT(xp != NULL); 18420 ASSERT(pktp != NULL); 18421 18422 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18423 sd_reset_target(un, pktp); 18424 18425 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18426 18427 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18428 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18429 } 18430 18431 18432 18433 /* 18434 * Function: sd_pkt_status_check_condition 18435 * 18436 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18437 * 18438 * Context: May be called from interrupt context 18439 */ 18440 18441 static void 18442 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18443 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18444 { 18445 ASSERT(un != NULL); 18446 ASSERT(mutex_owned(SD_MUTEX(un))); 18447 ASSERT(bp != NULL); 18448 ASSERT(xp != NULL); 18449 ASSERT(pktp != NULL); 18450 18451 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18452 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18453 18454 /* 18455 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18456 * command will be retried after the request sense). Otherwise, retry 18457 * the command. Note: we are issuing the request sense even though the 18458 * retry limit may have been reached for the failed command. 18459 */ 18460 if (un->un_f_arq_enabled == FALSE) { 18461 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18462 "no ARQ, sending request sense command\n"); 18463 sd_send_request_sense_command(un, bp, pktp); 18464 } else { 18465 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18466 "ARQ,retrying request sense command\n"); 18467 #if defined(__i386) || defined(__amd64) 18468 /* 18469 * The SD_RETRY_DELAY value need to be adjusted here 18470 * when SD_RETRY_DELAY change in sddef.h 18471 */ 18472 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18473 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18474 NULL); 18475 #else 18476 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18477 EIO, SD_RETRY_DELAY, NULL); 18478 #endif 18479 } 18480 18481 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18482 } 18483 18484 18485 /* 18486 * Function: sd_pkt_status_busy 18487 * 18488 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18489 * 18490 * Context: May be called from interrupt context 18491 */ 18492 18493 static void 18494 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18495 struct scsi_pkt *pktp) 18496 { 18497 ASSERT(un != NULL); 18498 ASSERT(mutex_owned(SD_MUTEX(un))); 18499 ASSERT(bp != NULL); 18500 ASSERT(xp != NULL); 18501 ASSERT(pktp != NULL); 18502 18503 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18504 "sd_pkt_status_busy: entry\n"); 18505 18506 /* If retries are exhausted, just fail the command. */ 18507 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18508 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18509 "device busy too long\n"); 18510 sd_return_failed_command(un, bp, EIO); 18511 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18512 "sd_pkt_status_busy: exit\n"); 18513 return; 18514 } 18515 xp->xb_retry_count++; 18516 18517 /* 18518 * Try to reset the target. However, we do not want to perform 18519 * more than one reset if the device continues to fail. The reset 18520 * will be performed when the retry count reaches the reset 18521 * threshold. This threshold should be set such that at least 18522 * one retry is issued before the reset is performed. 18523 */ 18524 if (xp->xb_retry_count == 18525 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18526 int rval = 0; 18527 mutex_exit(SD_MUTEX(un)); 18528 if (un->un_f_allow_bus_device_reset == TRUE) { 18529 /* 18530 * First try to reset the LUN; if we cannot then 18531 * try to reset the target. 18532 */ 18533 if (un->un_f_lun_reset_enabled == TRUE) { 18534 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18535 "sd_pkt_status_busy: RESET_LUN\n"); 18536 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18537 } 18538 if (rval == 0) { 18539 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18540 "sd_pkt_status_busy: RESET_TARGET\n"); 18541 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18542 } 18543 } 18544 if (rval == 0) { 18545 /* 18546 * If the RESET_LUN and/or RESET_TARGET failed, 18547 * try RESET_ALL 18548 */ 18549 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18550 "sd_pkt_status_busy: RESET_ALL\n"); 18551 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18552 } 18553 mutex_enter(SD_MUTEX(un)); 18554 if (rval == 0) { 18555 /* 18556 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18557 * At this point we give up & fail the command. 18558 */ 18559 sd_return_failed_command(un, bp, EIO); 18560 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18561 "sd_pkt_status_busy: exit (failed cmd)\n"); 18562 return; 18563 } 18564 } 18565 18566 /* 18567 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18568 * we have already checked the retry counts above. 18569 */ 18570 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18571 EIO, SD_BSY_TIMEOUT, NULL); 18572 18573 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18574 "sd_pkt_status_busy: exit\n"); 18575 } 18576 18577 18578 /* 18579 * Function: sd_pkt_status_reservation_conflict 18580 * 18581 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18582 * command status. 18583 * 18584 * Context: May be called from interrupt context 18585 */ 18586 18587 static void 18588 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18589 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18590 { 18591 ASSERT(un != NULL); 18592 ASSERT(mutex_owned(SD_MUTEX(un))); 18593 ASSERT(bp != NULL); 18594 ASSERT(xp != NULL); 18595 ASSERT(pktp != NULL); 18596 18597 /* 18598 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18599 * conflict could be due to various reasons like incorrect keys, not 18600 * registered or not reserved etc. So, we return EACCES to the caller. 18601 */ 18602 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18603 int cmd = SD_GET_PKT_OPCODE(pktp); 18604 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18605 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18606 sd_return_failed_command(un, bp, EACCES); 18607 return; 18608 } 18609 } 18610 18611 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18612 18613 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18614 if (sd_failfast_enable != 0) { 18615 /* By definition, we must panic here.... */ 18616 sd_panic_for_res_conflict(un); 18617 /*NOTREACHED*/ 18618 } 18619 SD_ERROR(SD_LOG_IO, un, 18620 "sd_handle_resv_conflict: Disk Reserved\n"); 18621 sd_return_failed_command(un, bp, EACCES); 18622 return; 18623 } 18624 18625 /* 18626 * 1147670: retry only if sd_retry_on_reservation_conflict 18627 * property is set (default is 1). Retries will not succeed 18628 * on a disk reserved by another initiator. HA systems 18629 * may reset this via sd.conf to avoid these retries. 18630 * 18631 * Note: The legacy return code for this failure is EIO, however EACCES 18632 * seems more appropriate for a reservation conflict. 18633 */ 18634 if (sd_retry_on_reservation_conflict == 0) { 18635 SD_ERROR(SD_LOG_IO, un, 18636 "sd_handle_resv_conflict: Device Reserved\n"); 18637 sd_return_failed_command(un, bp, EIO); 18638 return; 18639 } 18640 18641 /* 18642 * Retry the command if we can. 18643 * 18644 * Note: The legacy return code for this failure is EIO, however EACCES 18645 * seems more appropriate for a reservation conflict. 18646 */ 18647 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18648 (clock_t)2, NULL); 18649 } 18650 18651 18652 18653 /* 18654 * Function: sd_pkt_status_qfull 18655 * 18656 * Description: Handle a QUEUE FULL condition from the target. This can 18657 * occur if the HBA does not handle the queue full condition. 18658 * (Basically this means third-party HBAs as Sun HBAs will 18659 * handle the queue full condition.) Note that if there are 18660 * some commands already in the transport, then the queue full 18661 * has occurred because the queue for this nexus is actually 18662 * full. If there are no commands in the transport, then the 18663 * queue full is resulting from some other initiator or lun 18664 * consuming all the resources at the target. 18665 * 18666 * Context: May be called from interrupt context 18667 */ 18668 18669 static void 18670 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18671 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18672 { 18673 ASSERT(un != NULL); 18674 ASSERT(mutex_owned(SD_MUTEX(un))); 18675 ASSERT(bp != NULL); 18676 ASSERT(xp != NULL); 18677 ASSERT(pktp != NULL); 18678 18679 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18680 "sd_pkt_status_qfull: entry\n"); 18681 18682 /* 18683 * Just lower the QFULL throttle and retry the command. Note that 18684 * we do not limit the number of retries here. 18685 */ 18686 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18687 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18688 SD_RESTART_TIMEOUT, NULL); 18689 18690 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18691 "sd_pkt_status_qfull: exit\n"); 18692 } 18693 18694 18695 /* 18696 * Function: sd_reset_target 18697 * 18698 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18699 * RESET_TARGET, or RESET_ALL. 18700 * 18701 * Context: May be called under interrupt context. 18702 */ 18703 18704 static void 18705 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18706 { 18707 int rval = 0; 18708 18709 ASSERT(un != NULL); 18710 ASSERT(mutex_owned(SD_MUTEX(un))); 18711 ASSERT(pktp != NULL); 18712 18713 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18714 18715 /* 18716 * No need to reset if the transport layer has already done so. 18717 */ 18718 if ((pktp->pkt_statistics & 18719 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18720 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18721 "sd_reset_target: no reset\n"); 18722 return; 18723 } 18724 18725 mutex_exit(SD_MUTEX(un)); 18726 18727 if (un->un_f_allow_bus_device_reset == TRUE) { 18728 if (un->un_f_lun_reset_enabled == TRUE) { 18729 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18730 "sd_reset_target: RESET_LUN\n"); 18731 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18732 } 18733 if (rval == 0) { 18734 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18735 "sd_reset_target: RESET_TARGET\n"); 18736 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18737 } 18738 } 18739 18740 if (rval == 0) { 18741 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18742 "sd_reset_target: RESET_ALL\n"); 18743 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18744 } 18745 18746 mutex_enter(SD_MUTEX(un)); 18747 18748 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18749 } 18750 18751 18752 /* 18753 * Function: sd_media_change_task 18754 * 18755 * Description: Recovery action for CDROM to become available. 18756 * 18757 * Context: Executes in a taskq() thread context 18758 */ 18759 18760 static void 18761 sd_media_change_task(void *arg) 18762 { 18763 struct scsi_pkt *pktp = arg; 18764 struct sd_lun *un; 18765 struct buf *bp; 18766 struct sd_xbuf *xp; 18767 int err = 0; 18768 int retry_count = 0; 18769 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18770 struct sd_sense_info si; 18771 18772 ASSERT(pktp != NULL); 18773 bp = (struct buf *)pktp->pkt_private; 18774 ASSERT(bp != NULL); 18775 xp = SD_GET_XBUF(bp); 18776 ASSERT(xp != NULL); 18777 un = SD_GET_UN(bp); 18778 ASSERT(un != NULL); 18779 ASSERT(!mutex_owned(SD_MUTEX(un))); 18780 ASSERT(ISREMOVABLE(un)); 18781 18782 si.ssi_severity = SCSI_ERR_INFO; 18783 si.ssi_pfa_flag = FALSE; 18784 18785 /* 18786 * When a reset is issued on a CDROM, it takes a long time to 18787 * recover. First few attempts to read capacity and other things 18788 * related to handling unit attention fail (with a ASC 0x4 and 18789 * ASCQ 0x1). In that case we want to do enough retries and we want 18790 * to limit the retries in other cases of genuine failures like 18791 * no media in drive. 18792 */ 18793 while (retry_count++ < retry_limit) { 18794 if ((err = sd_handle_mchange(un)) == 0) { 18795 break; 18796 } 18797 if (err == EAGAIN) { 18798 retry_limit = SD_UNIT_ATTENTION_RETRY; 18799 } 18800 /* Sleep for 0.5 sec. & try again */ 18801 delay(drv_usectohz(500000)); 18802 } 18803 18804 /* 18805 * Dispatch (retry or fail) the original command here, 18806 * along with appropriate console messages.... 18807 * 18808 * Must grab the mutex before calling sd_retry_command, 18809 * sd_print_sense_msg and sd_return_failed_command. 18810 */ 18811 mutex_enter(SD_MUTEX(un)); 18812 if (err != SD_CMD_SUCCESS) { 18813 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18814 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18815 si.ssi_severity = SCSI_ERR_FATAL; 18816 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18817 sd_return_failed_command(un, bp, EIO); 18818 } else { 18819 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18820 &si, EIO, (clock_t)0, NULL); 18821 } 18822 mutex_exit(SD_MUTEX(un)); 18823 } 18824 18825 18826 18827 /* 18828 * Function: sd_handle_mchange 18829 * 18830 * Description: Perform geometry validation & other recovery when CDROM 18831 * has been removed from drive. 18832 * 18833 * Return Code: 0 for success 18834 * errno-type return code of either sd_send_scsi_DOORLOCK() or 18835 * sd_send_scsi_READ_CAPACITY() 18836 * 18837 * Context: Executes in a taskq() thread context 18838 */ 18839 18840 static int 18841 sd_handle_mchange(struct sd_lun *un) 18842 { 18843 uint64_t capacity; 18844 uint32_t lbasize; 18845 int rval; 18846 18847 ASSERT(!mutex_owned(SD_MUTEX(un))); 18848 ASSERT(ISREMOVABLE(un)); 18849 18850 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 18851 SD_PATH_DIRECT_PRIORITY)) != 0) { 18852 return (rval); 18853 } 18854 18855 mutex_enter(SD_MUTEX(un)); 18856 sd_update_block_info(un, lbasize, capacity); 18857 18858 if (un->un_errstats != NULL) { 18859 struct sd_errstats *stp = 18860 (struct sd_errstats *)un->un_errstats->ks_data; 18861 stp->sd_capacity.value.ui64 = (uint64_t) 18862 ((uint64_t)un->un_blockcount * 18863 (uint64_t)un->un_tgt_blocksize); 18864 } 18865 18866 /* 18867 * Note: Maybe let the strategy/partitioning chain worry about getting 18868 * valid geometry. 18869 */ 18870 un->un_f_geometry_is_valid = FALSE; 18871 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 18872 if (un->un_f_geometry_is_valid == FALSE) { 18873 mutex_exit(SD_MUTEX(un)); 18874 return (EIO); 18875 } 18876 18877 mutex_exit(SD_MUTEX(un)); 18878 18879 /* 18880 * Try to lock the door 18881 */ 18882 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 18883 SD_PATH_DIRECT_PRIORITY)); 18884 } 18885 18886 18887 /* 18888 * Function: sd_send_scsi_DOORLOCK 18889 * 18890 * Description: Issue the scsi DOOR LOCK command 18891 * 18892 * Arguments: un - pointer to driver soft state (unit) structure for 18893 * this target. 18894 * flag - SD_REMOVAL_ALLOW 18895 * SD_REMOVAL_PREVENT 18896 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18897 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18898 * to use the USCSI "direct" chain and bypass the normal 18899 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18900 * command is issued as part of an error recovery action. 18901 * 18902 * Return Code: 0 - Success 18903 * errno return code from sd_send_scsi_cmd() 18904 * 18905 * Context: Can sleep. 18906 */ 18907 18908 static int 18909 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 18910 { 18911 union scsi_cdb cdb; 18912 struct uscsi_cmd ucmd_buf; 18913 struct scsi_extended_sense sense_buf; 18914 int status; 18915 18916 ASSERT(un != NULL); 18917 ASSERT(!mutex_owned(SD_MUTEX(un))); 18918 18919 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 18920 18921 /* already determined doorlock is not supported, fake success */ 18922 if (un->un_f_doorlock_supported == FALSE) { 18923 return (0); 18924 } 18925 18926 bzero(&cdb, sizeof (cdb)); 18927 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18928 18929 cdb.scc_cmd = SCMD_DOORLOCK; 18930 cdb.cdb_opaque[4] = (uchar_t)flag; 18931 18932 ucmd_buf.uscsi_cdb = (char *)&cdb; 18933 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18934 ucmd_buf.uscsi_bufaddr = NULL; 18935 ucmd_buf.uscsi_buflen = 0; 18936 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18937 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18938 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18939 ucmd_buf.uscsi_timeout = 15; 18940 18941 SD_TRACE(SD_LOG_IO, un, 18942 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 18943 18944 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 18945 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 18946 18947 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 18948 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18949 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 18950 /* fake success and skip subsequent doorlock commands */ 18951 un->un_f_doorlock_supported = FALSE; 18952 return (0); 18953 } 18954 18955 return (status); 18956 } 18957 18958 /* 18959 * Function: sd_send_scsi_READ_CAPACITY 18960 * 18961 * Description: This routine uses the scsi READ CAPACITY command to determine 18962 * the device capacity in number of blocks and the device native 18963 * block size. If this function returns a failure, then the 18964 * values in *capp and *lbap are undefined. If the capacity 18965 * returned is 0xffffffff then the lun is too large for a 18966 * normal READ CAPACITY command and the results of a 18967 * READ CAPACITY 16 will be used instead. 18968 * 18969 * Arguments: un - ptr to soft state struct for the target 18970 * capp - ptr to unsigned 64-bit variable to receive the 18971 * capacity value from the command. 18972 * lbap - ptr to unsigned 32-bit varaible to receive the 18973 * block size value from the command 18974 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18975 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18976 * to use the USCSI "direct" chain and bypass the normal 18977 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18978 * command is issued as part of an error recovery action. 18979 * 18980 * Return Code: 0 - Success 18981 * EIO - IO error 18982 * EACCES - Reservation conflict detected 18983 * EAGAIN - Device is becoming ready 18984 * errno return code from sd_send_scsi_cmd() 18985 * 18986 * Context: Can sleep. Blocks until command completes. 18987 */ 18988 18989 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 18990 18991 static int 18992 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 18993 int path_flag) 18994 { 18995 struct scsi_extended_sense sense_buf; 18996 struct uscsi_cmd ucmd_buf; 18997 union scsi_cdb cdb; 18998 uint32_t *capacity_buf; 18999 uint64_t capacity; 19000 uint32_t lbasize; 19001 int status; 19002 19003 ASSERT(un != NULL); 19004 ASSERT(!mutex_owned(SD_MUTEX(un))); 19005 ASSERT(capp != NULL); 19006 ASSERT(lbap != NULL); 19007 19008 SD_TRACE(SD_LOG_IO, un, 19009 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19010 19011 /* 19012 * First send a READ_CAPACITY command to the target. 19013 * (This command is mandatory under SCSI-2.) 19014 * 19015 * Set up the CDB for the READ_CAPACITY command. The Partial 19016 * Medium Indicator bit is cleared. The address field must be 19017 * zero if the PMI bit is zero. 19018 */ 19019 bzero(&cdb, sizeof (cdb)); 19020 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19021 19022 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19023 19024 cdb.scc_cmd = SCMD_READ_CAPACITY; 19025 19026 ucmd_buf.uscsi_cdb = (char *)&cdb; 19027 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19028 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19029 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19030 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19031 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19032 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19033 ucmd_buf.uscsi_timeout = 60; 19034 19035 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19036 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19037 19038 switch (status) { 19039 case 0: 19040 /* Return failure if we did not get valid capacity data. */ 19041 if (ucmd_buf.uscsi_resid != 0) { 19042 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19043 return (EIO); 19044 } 19045 19046 /* 19047 * Read capacity and block size from the READ CAPACITY 10 data. 19048 * This data may be adjusted later due to device specific 19049 * issues. 19050 * 19051 * According to the SCSI spec, the READ CAPACITY 10 19052 * command returns the following: 19053 * 19054 * bytes 0-3: Maximum logical block address available. 19055 * (MSB in byte:0 & LSB in byte:3) 19056 * 19057 * bytes 4-7: Block length in bytes 19058 * (MSB in byte:4 & LSB in byte:7) 19059 * 19060 */ 19061 capacity = BE_32(capacity_buf[0]); 19062 lbasize = BE_32(capacity_buf[1]); 19063 19064 /* 19065 * Done with capacity_buf 19066 */ 19067 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19068 19069 /* 19070 * if the reported capacity is set to all 0xf's, then 19071 * this disk is too large and requires SBC-2 commands. 19072 * Reissue the request using READ CAPACITY 16. 19073 */ 19074 if (capacity == 0xffffffff) { 19075 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19076 &lbasize, path_flag); 19077 if (status != 0) { 19078 return (status); 19079 } 19080 } 19081 break; /* Success! */ 19082 case EIO: 19083 switch (ucmd_buf.uscsi_status) { 19084 case STATUS_RESERVATION_CONFLICT: 19085 status = EACCES; 19086 break; 19087 case STATUS_CHECK: 19088 /* 19089 * Check condition; look for ASC/ASCQ of 0x04/0x01 19090 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19091 */ 19092 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19093 (sense_buf.es_add_code == 0x04) && 19094 (sense_buf.es_qual_code == 0x01)) { 19095 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19096 return (EAGAIN); 19097 } 19098 break; 19099 default: 19100 break; 19101 } 19102 /* FALLTHRU */ 19103 default: 19104 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19105 return (status); 19106 } 19107 19108 /* 19109 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19110 * (2352 and 0 are common) so for these devices always force the value 19111 * to 2048 as required by the ATAPI specs. 19112 */ 19113 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19114 lbasize = 2048; 19115 } 19116 19117 /* 19118 * Get the maximum LBA value from the READ CAPACITY data. 19119 * Here we assume that the Partial Medium Indicator (PMI) bit 19120 * was cleared when issuing the command. This means that the LBA 19121 * returned from the device is the LBA of the last logical block 19122 * on the logical unit. The actual logical block count will be 19123 * this value plus one. 19124 * 19125 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19126 * so scale the capacity value to reflect this. 19127 */ 19128 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19129 19130 #if defined(__i386) || defined(__amd64) 19131 /* 19132 * On x86, compensate for off-by-1 error (number of sectors on 19133 * media) (1175930) 19134 */ 19135 if (!ISREMOVABLE(un) && (lbasize == un->un_sys_blocksize)) { 19136 capacity -= 1; 19137 } 19138 #endif 19139 19140 /* 19141 * Copy the values from the READ CAPACITY command into the space 19142 * provided by the caller. 19143 */ 19144 *capp = capacity; 19145 *lbap = lbasize; 19146 19147 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19148 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19149 19150 /* 19151 * Both the lbasize and capacity from the device must be nonzero, 19152 * otherwise we assume that the values are not valid and return 19153 * failure to the caller. (4203735) 19154 */ 19155 if ((capacity == 0) || (lbasize == 0)) { 19156 return (EIO); 19157 } 19158 19159 return (0); 19160 } 19161 19162 /* 19163 * Function: sd_send_scsi_READ_CAPACITY_16 19164 * 19165 * Description: This routine uses the scsi READ CAPACITY 16 command to 19166 * determine the device capacity in number of blocks and the 19167 * device native block size. If this function returns a failure, 19168 * then the values in *capp and *lbap are undefined. 19169 * This routine should always be called by 19170 * sd_send_scsi_READ_CAPACITY which will appy any device 19171 * specific adjustments to capacity and lbasize. 19172 * 19173 * Arguments: un - ptr to soft state struct for the target 19174 * capp - ptr to unsigned 64-bit variable to receive the 19175 * capacity value from the command. 19176 * lbap - ptr to unsigned 32-bit varaible to receive the 19177 * block size value from the command 19178 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19179 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19180 * to use the USCSI "direct" chain and bypass the normal 19181 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19182 * this command is issued as part of an error recovery 19183 * action. 19184 * 19185 * Return Code: 0 - Success 19186 * EIO - IO error 19187 * EACCES - Reservation conflict detected 19188 * EAGAIN - Device is becoming ready 19189 * errno return code from sd_send_scsi_cmd() 19190 * 19191 * Context: Can sleep. Blocks until command completes. 19192 */ 19193 19194 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19195 19196 static int 19197 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19198 uint32_t *lbap, int path_flag) 19199 { 19200 struct scsi_extended_sense sense_buf; 19201 struct uscsi_cmd ucmd_buf; 19202 union scsi_cdb cdb; 19203 uint64_t *capacity16_buf; 19204 uint64_t capacity; 19205 uint32_t lbasize; 19206 int status; 19207 19208 ASSERT(un != NULL); 19209 ASSERT(!mutex_owned(SD_MUTEX(un))); 19210 ASSERT(capp != NULL); 19211 ASSERT(lbap != NULL); 19212 19213 SD_TRACE(SD_LOG_IO, un, 19214 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19215 19216 /* 19217 * First send a READ_CAPACITY_16 command to the target. 19218 * 19219 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19220 * Medium Indicator bit is cleared. The address field must be 19221 * zero if the PMI bit is zero. 19222 */ 19223 bzero(&cdb, sizeof (cdb)); 19224 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19225 19226 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19227 19228 ucmd_buf.uscsi_cdb = (char *)&cdb; 19229 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19230 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19231 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19232 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19233 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19234 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19235 ucmd_buf.uscsi_timeout = 60; 19236 19237 /* 19238 * Read Capacity (16) is a Service Action In command. One 19239 * command byte (0x9E) is overloaded for multiple operations, 19240 * with the second CDB byte specifying the desired operation 19241 */ 19242 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19243 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19244 19245 /* 19246 * Fill in allocation length field 19247 */ 19248 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19249 19250 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19251 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19252 19253 switch (status) { 19254 case 0: 19255 /* Return failure if we did not get valid capacity data. */ 19256 if (ucmd_buf.uscsi_resid > 20) { 19257 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19258 return (EIO); 19259 } 19260 19261 /* 19262 * Read capacity and block size from the READ CAPACITY 10 data. 19263 * This data may be adjusted later due to device specific 19264 * issues. 19265 * 19266 * According to the SCSI spec, the READ CAPACITY 10 19267 * command returns the following: 19268 * 19269 * bytes 0-7: Maximum logical block address available. 19270 * (MSB in byte:0 & LSB in byte:7) 19271 * 19272 * bytes 8-11: Block length in bytes 19273 * (MSB in byte:8 & LSB in byte:11) 19274 * 19275 */ 19276 capacity = BE_64(capacity16_buf[0]); 19277 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19278 19279 /* 19280 * Done with capacity16_buf 19281 */ 19282 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19283 19284 /* 19285 * if the reported capacity is set to all 0xf's, then 19286 * this disk is too large. This could only happen with 19287 * a device that supports LBAs larger than 64 bits which 19288 * are not defined by any current T10 standards. 19289 */ 19290 if (capacity == 0xffffffffffffffff) { 19291 return (EIO); 19292 } 19293 break; /* Success! */ 19294 case EIO: 19295 switch (ucmd_buf.uscsi_status) { 19296 case STATUS_RESERVATION_CONFLICT: 19297 status = EACCES; 19298 break; 19299 case STATUS_CHECK: 19300 /* 19301 * Check condition; look for ASC/ASCQ of 0x04/0x01 19302 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19303 */ 19304 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19305 (sense_buf.es_add_code == 0x04) && 19306 (sense_buf.es_qual_code == 0x01)) { 19307 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19308 return (EAGAIN); 19309 } 19310 break; 19311 default: 19312 break; 19313 } 19314 /* FALLTHRU */ 19315 default: 19316 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19317 return (status); 19318 } 19319 19320 *capp = capacity; 19321 *lbap = lbasize; 19322 19323 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19324 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19325 19326 return (0); 19327 } 19328 19329 19330 /* 19331 * Function: sd_send_scsi_START_STOP_UNIT 19332 * 19333 * Description: Issue a scsi START STOP UNIT command to the target. 19334 * 19335 * Arguments: un - pointer to driver soft state (unit) structure for 19336 * this target. 19337 * flag - SD_TARGET_START 19338 * SD_TARGET_STOP 19339 * SD_TARGET_EJECT 19340 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19341 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19342 * to use the USCSI "direct" chain and bypass the normal 19343 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19344 * command is issued as part of an error recovery action. 19345 * 19346 * Return Code: 0 - Success 19347 * EIO - IO error 19348 * EACCES - Reservation conflict detected 19349 * ENXIO - Not Ready, medium not present 19350 * errno return code from sd_send_scsi_cmd() 19351 * 19352 * Context: Can sleep. 19353 */ 19354 19355 static int 19356 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19357 { 19358 struct scsi_extended_sense sense_buf; 19359 union scsi_cdb cdb; 19360 struct uscsi_cmd ucmd_buf; 19361 int status; 19362 19363 ASSERT(un != NULL); 19364 ASSERT(!mutex_owned(SD_MUTEX(un))); 19365 19366 SD_TRACE(SD_LOG_IO, un, 19367 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19368 19369 if (ISREMOVABLE(un) && 19370 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19371 (un->un_f_start_stop_supported != TRUE)) { 19372 return (0); 19373 } 19374 19375 bzero(&cdb, sizeof (cdb)); 19376 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19377 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19378 19379 cdb.scc_cmd = SCMD_START_STOP; 19380 cdb.cdb_opaque[4] = (uchar_t)flag; 19381 19382 ucmd_buf.uscsi_cdb = (char *)&cdb; 19383 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19384 ucmd_buf.uscsi_bufaddr = NULL; 19385 ucmd_buf.uscsi_buflen = 0; 19386 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19387 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19388 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19389 ucmd_buf.uscsi_timeout = 200; 19390 19391 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19392 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19393 19394 switch (status) { 19395 case 0: 19396 break; /* Success! */ 19397 case EIO: 19398 switch (ucmd_buf.uscsi_status) { 19399 case STATUS_RESERVATION_CONFLICT: 19400 status = EACCES; 19401 break; 19402 case STATUS_CHECK: 19403 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19404 switch (sense_buf.es_key) { 19405 case KEY_ILLEGAL_REQUEST: 19406 status = ENOTSUP; 19407 break; 19408 case KEY_NOT_READY: 19409 if (sense_buf.es_add_code == 0x3A) { 19410 status = ENXIO; 19411 } 19412 break; 19413 default: 19414 break; 19415 } 19416 } 19417 break; 19418 default: 19419 break; 19420 } 19421 break; 19422 default: 19423 break; 19424 } 19425 19426 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19427 19428 return (status); 19429 } 19430 19431 19432 /* 19433 * Function: sd_start_stop_unit_callback 19434 * 19435 * Description: timeout(9F) callback to begin recovery process for a 19436 * device that has spun down. 19437 * 19438 * Arguments: arg - pointer to associated softstate struct. 19439 * 19440 * Context: Executes in a timeout(9F) thread context 19441 */ 19442 19443 static void 19444 sd_start_stop_unit_callback(void *arg) 19445 { 19446 struct sd_lun *un = arg; 19447 ASSERT(un != NULL); 19448 ASSERT(!mutex_owned(SD_MUTEX(un))); 19449 19450 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19451 19452 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19453 } 19454 19455 19456 /* 19457 * Function: sd_start_stop_unit_task 19458 * 19459 * Description: Recovery procedure when a drive is spun down. 19460 * 19461 * Arguments: arg - pointer to associated softstate struct. 19462 * 19463 * Context: Executes in a taskq() thread context 19464 */ 19465 19466 static void 19467 sd_start_stop_unit_task(void *arg) 19468 { 19469 struct sd_lun *un = arg; 19470 19471 ASSERT(un != NULL); 19472 ASSERT(!mutex_owned(SD_MUTEX(un))); 19473 19474 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19475 19476 /* 19477 * Some unformatted drives report not ready error, no need to 19478 * restart if format has been initiated. 19479 */ 19480 mutex_enter(SD_MUTEX(un)); 19481 if (un->un_f_format_in_progress == TRUE) { 19482 mutex_exit(SD_MUTEX(un)); 19483 return; 19484 } 19485 mutex_exit(SD_MUTEX(un)); 19486 19487 /* 19488 * When a START STOP command is issued from here, it is part of a 19489 * failure recovery operation and must be issued before any other 19490 * commands, including any pending retries. Thus it must be sent 19491 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19492 * succeeds or not, we will start I/O after the attempt. 19493 */ 19494 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19495 SD_PATH_DIRECT_PRIORITY); 19496 19497 /* 19498 * The above call blocks until the START_STOP_UNIT command completes. 19499 * Now that it has completed, we must re-try the original IO that 19500 * received the NOT READY condition in the first place. There are 19501 * three possible conditions here: 19502 * 19503 * (1) The original IO is on un_retry_bp. 19504 * (2) The original IO is on the regular wait queue, and un_retry_bp 19505 * is NULL. 19506 * (3) The original IO is on the regular wait queue, and un_retry_bp 19507 * points to some other, unrelated bp. 19508 * 19509 * For each case, we must call sd_start_cmds() with un_retry_bp 19510 * as the argument. If un_retry_bp is NULL, this will initiate 19511 * processing of the regular wait queue. If un_retry_bp is not NULL, 19512 * then this will process the bp on un_retry_bp. That may or may not 19513 * be the original IO, but that does not matter: the important thing 19514 * is to keep the IO processing going at this point. 19515 * 19516 * Note: This is a very specific error recovery sequence associated 19517 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19518 * serialize the I/O with completion of the spin-up. 19519 */ 19520 mutex_enter(SD_MUTEX(un)); 19521 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19522 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19523 un, un->un_retry_bp); 19524 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19525 sd_start_cmds(un, un->un_retry_bp); 19526 mutex_exit(SD_MUTEX(un)); 19527 19528 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19529 } 19530 19531 19532 /* 19533 * Function: sd_send_scsi_INQUIRY 19534 * 19535 * Description: Issue the scsi INQUIRY command. 19536 * 19537 * Arguments: un 19538 * bufaddr 19539 * buflen 19540 * evpd 19541 * page_code 19542 * page_length 19543 * 19544 * Return Code: 0 - Success 19545 * errno return code from sd_send_scsi_cmd() 19546 * 19547 * Context: Can sleep. Does not return until command is completed. 19548 */ 19549 19550 static int 19551 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 19552 uchar_t evpd, uchar_t page_code, size_t *residp) 19553 { 19554 union scsi_cdb cdb; 19555 struct uscsi_cmd ucmd_buf; 19556 int status; 19557 19558 ASSERT(un != NULL); 19559 ASSERT(!mutex_owned(SD_MUTEX(un))); 19560 ASSERT(bufaddr != NULL); 19561 19562 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19563 19564 bzero(&cdb, sizeof (cdb)); 19565 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19566 bzero(bufaddr, buflen); 19567 19568 cdb.scc_cmd = SCMD_INQUIRY; 19569 cdb.cdb_opaque[1] = evpd; 19570 cdb.cdb_opaque[2] = page_code; 19571 FORMG0COUNT(&cdb, buflen); 19572 19573 ucmd_buf.uscsi_cdb = (char *)&cdb; 19574 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19575 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19576 ucmd_buf.uscsi_buflen = buflen; 19577 ucmd_buf.uscsi_rqbuf = NULL; 19578 ucmd_buf.uscsi_rqlen = 0; 19579 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19580 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19581 19582 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19583 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 19584 19585 if ((status == 0) && (residp != NULL)) { 19586 *residp = ucmd_buf.uscsi_resid; 19587 } 19588 19589 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19590 19591 return (status); 19592 } 19593 19594 19595 /* 19596 * Function: sd_send_scsi_TEST_UNIT_READY 19597 * 19598 * Description: Issue the scsi TEST UNIT READY command. 19599 * This routine can be told to set the flag USCSI_DIAGNOSE to 19600 * prevent retrying failed commands. Use this when the intent 19601 * is either to check for device readiness, to clear a Unit 19602 * Attention, or to clear any outstanding sense data. 19603 * However under specific conditions the expected behavior 19604 * is for retries to bring a device ready, so use the flag 19605 * with caution. 19606 * 19607 * Arguments: un 19608 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19609 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19610 * 0: dont check for media present, do retries on cmd. 19611 * 19612 * Return Code: 0 - Success 19613 * EIO - IO error 19614 * EACCES - Reservation conflict detected 19615 * ENXIO - Not Ready, medium not present 19616 * errno return code from sd_send_scsi_cmd() 19617 * 19618 * Context: Can sleep. Does not return until command is completed. 19619 */ 19620 19621 static int 19622 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 19623 { 19624 struct scsi_extended_sense sense_buf; 19625 union scsi_cdb cdb; 19626 struct uscsi_cmd ucmd_buf; 19627 int status; 19628 19629 ASSERT(un != NULL); 19630 ASSERT(!mutex_owned(SD_MUTEX(un))); 19631 19632 SD_TRACE(SD_LOG_IO, un, 19633 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19634 19635 /* 19636 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19637 * timeouts when they receive a TUR and the queue is not empty. Check 19638 * the configuration flag set during attach (indicating the drive has 19639 * this firmware bug) and un_ncmds_in_transport before issuing the 19640 * TUR. If there are 19641 * pending commands return success, this is a bit arbitrary but is ok 19642 * for non-removables (i.e. the eliteI disks) and non-clustering 19643 * configurations. 19644 */ 19645 if (un->un_f_cfg_tur_check == TRUE) { 19646 mutex_enter(SD_MUTEX(un)); 19647 if (un->un_ncmds_in_transport != 0) { 19648 mutex_exit(SD_MUTEX(un)); 19649 return (0); 19650 } 19651 mutex_exit(SD_MUTEX(un)); 19652 } 19653 19654 bzero(&cdb, sizeof (cdb)); 19655 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19656 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19657 19658 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19659 19660 ucmd_buf.uscsi_cdb = (char *)&cdb; 19661 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19662 ucmd_buf.uscsi_bufaddr = NULL; 19663 ucmd_buf.uscsi_buflen = 0; 19664 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19665 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19666 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19667 19668 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19669 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19670 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19671 } 19672 ucmd_buf.uscsi_timeout = 60; 19673 19674 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19675 UIO_SYSSPACE, UIO_SYSSPACE, 19676 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 19677 19678 switch (status) { 19679 case 0: 19680 break; /* Success! */ 19681 case EIO: 19682 switch (ucmd_buf.uscsi_status) { 19683 case STATUS_RESERVATION_CONFLICT: 19684 status = EACCES; 19685 break; 19686 case STATUS_CHECK: 19687 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 19688 break; 19689 } 19690 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19691 (sense_buf.es_key == KEY_NOT_READY) && 19692 (sense_buf.es_add_code == 0x3A)) { 19693 status = ENXIO; 19694 } 19695 break; 19696 default: 19697 break; 19698 } 19699 break; 19700 default: 19701 break; 19702 } 19703 19704 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 19705 19706 return (status); 19707 } 19708 19709 19710 /* 19711 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 19712 * 19713 * Description: Issue the scsi PERSISTENT RESERVE IN command. 19714 * 19715 * Arguments: un 19716 * 19717 * Return Code: 0 - Success 19718 * EACCES 19719 * ENOTSUP 19720 * errno return code from sd_send_scsi_cmd() 19721 * 19722 * Context: Can sleep. Does not return until command is completed. 19723 */ 19724 19725 static int 19726 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 19727 uint16_t data_len, uchar_t *data_bufp) 19728 { 19729 struct scsi_extended_sense sense_buf; 19730 union scsi_cdb cdb; 19731 struct uscsi_cmd ucmd_buf; 19732 int status; 19733 int no_caller_buf = FALSE; 19734 19735 ASSERT(un != NULL); 19736 ASSERT(!mutex_owned(SD_MUTEX(un))); 19737 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 19738 19739 SD_TRACE(SD_LOG_IO, un, 19740 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 19741 19742 bzero(&cdb, sizeof (cdb)); 19743 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19744 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19745 if (data_bufp == NULL) { 19746 /* Allocate a default buf if the caller did not give one */ 19747 ASSERT(data_len == 0); 19748 data_len = MHIOC_RESV_KEY_SIZE; 19749 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 19750 no_caller_buf = TRUE; 19751 } 19752 19753 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 19754 cdb.cdb_opaque[1] = usr_cmd; 19755 FORMG1COUNT(&cdb, data_len); 19756 19757 ucmd_buf.uscsi_cdb = (char *)&cdb; 19758 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19759 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 19760 ucmd_buf.uscsi_buflen = data_len; 19761 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19762 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19763 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19764 ucmd_buf.uscsi_timeout = 60; 19765 19766 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19767 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19768 19769 switch (status) { 19770 case 0: 19771 break; /* Success! */ 19772 case EIO: 19773 switch (ucmd_buf.uscsi_status) { 19774 case STATUS_RESERVATION_CONFLICT: 19775 status = EACCES; 19776 break; 19777 case STATUS_CHECK: 19778 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19779 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19780 status = ENOTSUP; 19781 } 19782 break; 19783 default: 19784 break; 19785 } 19786 break; 19787 default: 19788 break; 19789 } 19790 19791 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 19792 19793 if (no_caller_buf == TRUE) { 19794 kmem_free(data_bufp, data_len); 19795 } 19796 19797 return (status); 19798 } 19799 19800 19801 /* 19802 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 19803 * 19804 * Description: This routine is the driver entry point for handling CD-ROM 19805 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 19806 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 19807 * device. 19808 * 19809 * Arguments: un - Pointer to soft state struct for the target. 19810 * usr_cmd SCSI-3 reservation facility command (one of 19811 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 19812 * SD_SCSI3_PREEMPTANDABORT) 19813 * usr_bufp - user provided pointer register, reserve descriptor or 19814 * preempt and abort structure (mhioc_register_t, 19815 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 19816 * 19817 * Return Code: 0 - Success 19818 * EACCES 19819 * ENOTSUP 19820 * errno return code from sd_send_scsi_cmd() 19821 * 19822 * Context: Can sleep. Does not return until command is completed. 19823 */ 19824 19825 static int 19826 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 19827 uchar_t *usr_bufp) 19828 { 19829 struct scsi_extended_sense sense_buf; 19830 union scsi_cdb cdb; 19831 struct uscsi_cmd ucmd_buf; 19832 int status; 19833 uchar_t data_len = sizeof (sd_prout_t); 19834 sd_prout_t *prp; 19835 19836 ASSERT(un != NULL); 19837 ASSERT(!mutex_owned(SD_MUTEX(un))); 19838 ASSERT(data_len == 24); /* required by scsi spec */ 19839 19840 SD_TRACE(SD_LOG_IO, un, 19841 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 19842 19843 if (usr_bufp == NULL) { 19844 return (EINVAL); 19845 } 19846 19847 bzero(&cdb, sizeof (cdb)); 19848 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19849 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19850 prp = kmem_zalloc(data_len, KM_SLEEP); 19851 19852 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 19853 cdb.cdb_opaque[1] = usr_cmd; 19854 FORMG1COUNT(&cdb, data_len); 19855 19856 ucmd_buf.uscsi_cdb = (char *)&cdb; 19857 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19858 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 19859 ucmd_buf.uscsi_buflen = data_len; 19860 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19861 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19862 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19863 ucmd_buf.uscsi_timeout = 60; 19864 19865 switch (usr_cmd) { 19866 case SD_SCSI3_REGISTER: { 19867 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 19868 19869 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19870 bcopy(ptr->newkey.key, prp->service_key, 19871 MHIOC_RESV_KEY_SIZE); 19872 prp->aptpl = ptr->aptpl; 19873 break; 19874 } 19875 case SD_SCSI3_RESERVE: 19876 case SD_SCSI3_RELEASE: { 19877 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 19878 19879 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19880 prp->scope_address = BE_32(ptr->scope_specific_addr); 19881 cdb.cdb_opaque[2] = ptr->type; 19882 break; 19883 } 19884 case SD_SCSI3_PREEMPTANDABORT: { 19885 mhioc_preemptandabort_t *ptr = 19886 (mhioc_preemptandabort_t *)usr_bufp; 19887 19888 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 19889 bcopy(ptr->victim_key.key, prp->service_key, 19890 MHIOC_RESV_KEY_SIZE); 19891 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 19892 cdb.cdb_opaque[2] = ptr->resvdesc.type; 19893 ucmd_buf.uscsi_flags |= USCSI_HEAD; 19894 break; 19895 } 19896 case SD_SCSI3_REGISTERANDIGNOREKEY: 19897 { 19898 mhioc_registerandignorekey_t *ptr; 19899 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 19900 bcopy(ptr->newkey.key, 19901 prp->service_key, MHIOC_RESV_KEY_SIZE); 19902 prp->aptpl = ptr->aptpl; 19903 break; 19904 } 19905 default: 19906 ASSERT(FALSE); 19907 break; 19908 } 19909 19910 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19911 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 19912 19913 switch (status) { 19914 case 0: 19915 break; /* Success! */ 19916 case EIO: 19917 switch (ucmd_buf.uscsi_status) { 19918 case STATUS_RESERVATION_CONFLICT: 19919 status = EACCES; 19920 break; 19921 case STATUS_CHECK: 19922 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19923 (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) { 19924 status = ENOTSUP; 19925 } 19926 break; 19927 default: 19928 break; 19929 } 19930 break; 19931 default: 19932 break; 19933 } 19934 19935 kmem_free(prp, data_len); 19936 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 19937 return (status); 19938 } 19939 19940 19941 /* 19942 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 19943 * 19944 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 19945 * 19946 * Arguments: un - pointer to the target's soft state struct 19947 * 19948 * Return Code: 0 - success 19949 * errno-type error code 19950 * 19951 * Context: kernel thread context only. 19952 */ 19953 19954 static int 19955 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 19956 { 19957 struct sd_uscsi_info *uip; 19958 struct uscsi_cmd *uscmd; 19959 union scsi_cdb *cdb; 19960 struct buf *bp; 19961 int rval = 0; 19962 19963 SD_TRACE(SD_LOG_IO, un, 19964 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 19965 19966 ASSERT(un != NULL); 19967 ASSERT(!mutex_owned(SD_MUTEX(un))); 19968 19969 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 19970 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 19971 19972 /* 19973 * First get some memory for the uscsi_cmd struct and cdb 19974 * and initialize for SYNCHRONIZE_CACHE cmd. 19975 */ 19976 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 19977 uscmd->uscsi_cdblen = CDB_GROUP1; 19978 uscmd->uscsi_cdb = (caddr_t)cdb; 19979 uscmd->uscsi_bufaddr = NULL; 19980 uscmd->uscsi_buflen = 0; 19981 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 19982 uscmd->uscsi_rqlen = SENSE_LENGTH; 19983 uscmd->uscsi_rqresid = SENSE_LENGTH; 19984 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19985 uscmd->uscsi_timeout = sd_io_time; 19986 19987 /* 19988 * Allocate an sd_uscsi_info struct and fill it with the info 19989 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 19990 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 19991 * since we allocate the buf here in this function, we do not 19992 * need to preserve the prior contents of b_private. 19993 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 19994 */ 19995 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 19996 uip->ui_flags = SD_PATH_DIRECT; 19997 uip->ui_cmdp = uscmd; 19998 19999 bp = getrbuf(KM_SLEEP); 20000 bp->b_private = uip; 20001 20002 /* 20003 * Setup buffer to carry uscsi request. 20004 */ 20005 bp->b_flags = B_BUSY; 20006 bp->b_bcount = 0; 20007 bp->b_blkno = 0; 20008 20009 if (dkc != NULL) { 20010 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20011 uip->ui_dkc = *dkc; 20012 } 20013 20014 bp->b_edev = SD_GET_DEV(un); 20015 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20016 20017 (void) sd_uscsi_strategy(bp); 20018 20019 /* 20020 * If synchronous request, wait for completion 20021 * If async just return and let b_iodone callback 20022 * cleanup. 20023 * NOTE: On return, u_ncmds_in_driver will be decremented, 20024 * but it was also incremented in sd_uscsi_strategy(), so 20025 * we should be ok. 20026 */ 20027 if (dkc == NULL) { 20028 (void) biowait(bp); 20029 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20030 } 20031 20032 return (rval); 20033 } 20034 20035 20036 static int 20037 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20038 { 20039 struct sd_uscsi_info *uip; 20040 struct uscsi_cmd *uscmd; 20041 struct scsi_extended_sense *sense_buf; 20042 struct sd_lun *un; 20043 int status; 20044 20045 uip = (struct sd_uscsi_info *)(bp->b_private); 20046 ASSERT(uip != NULL); 20047 20048 uscmd = uip->ui_cmdp; 20049 ASSERT(uscmd != NULL); 20050 20051 sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf; 20052 ASSERT(sense_buf != NULL); 20053 20054 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20055 ASSERT(un != NULL); 20056 20057 status = geterror(bp); 20058 switch (status) { 20059 case 0: 20060 break; /* Success! */ 20061 case EIO: 20062 switch (uscmd->uscsi_status) { 20063 case STATUS_RESERVATION_CONFLICT: 20064 /* Ignore reservation conflict */ 20065 status = 0; 20066 goto done; 20067 20068 case STATUS_CHECK: 20069 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20070 (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) { 20071 /* Ignore Illegal Request error */ 20072 mutex_enter(SD_MUTEX(un)); 20073 un->un_f_sync_cache_unsupported = TRUE; 20074 mutex_exit(SD_MUTEX(un)); 20075 status = ENOTSUP; 20076 goto done; 20077 } 20078 break; 20079 default: 20080 break; 20081 } 20082 /* FALLTHRU */ 20083 default: 20084 /* Ignore error if the media is not present */ 20085 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 20086 status = 0; 20087 goto done; 20088 } 20089 /* If we reach this, we had an error */ 20090 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20091 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20092 break; 20093 } 20094 20095 done: 20096 if (uip->ui_dkc.dkc_callback != NULL) { 20097 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20098 } 20099 20100 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20101 freerbuf(bp); 20102 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20103 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20104 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20105 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20106 20107 return (status); 20108 } 20109 20110 20111 /* 20112 * Function: sd_send_scsi_GET_CONFIGURATION 20113 * 20114 * Description: Issues the get configuration command to the device. 20115 * Called from sd_check_for_writable_cd & sd_get_media_info 20116 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20117 * Arguments: un 20118 * ucmdbuf 20119 * rqbuf 20120 * rqbuflen 20121 * bufaddr 20122 * buflen 20123 * 20124 * Return Code: 0 - Success 20125 * errno return code from sd_send_scsi_cmd() 20126 * 20127 * Context: Can sleep. Does not return until command is completed. 20128 * 20129 */ 20130 20131 static int 20132 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20133 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20134 { 20135 char cdb[CDB_GROUP1]; 20136 int status; 20137 20138 ASSERT(un != NULL); 20139 ASSERT(!mutex_owned(SD_MUTEX(un))); 20140 ASSERT(bufaddr != NULL); 20141 ASSERT(ucmdbuf != NULL); 20142 ASSERT(rqbuf != NULL); 20143 20144 SD_TRACE(SD_LOG_IO, un, 20145 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20146 20147 bzero(cdb, sizeof (cdb)); 20148 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20149 bzero(rqbuf, rqbuflen); 20150 bzero(bufaddr, buflen); 20151 20152 /* 20153 * Set up cdb field for the get configuration command. 20154 */ 20155 cdb[0] = SCMD_GET_CONFIGURATION; 20156 cdb[1] = 0x02; /* Requested Type */ 20157 cdb[8] = SD_PROFILE_HEADER_LEN; 20158 ucmdbuf->uscsi_cdb = cdb; 20159 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20160 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20161 ucmdbuf->uscsi_buflen = buflen; 20162 ucmdbuf->uscsi_timeout = sd_io_time; 20163 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20164 ucmdbuf->uscsi_rqlen = rqbuflen; 20165 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20166 20167 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20168 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20169 20170 switch (status) { 20171 case 0: 20172 break; /* Success! */ 20173 case EIO: 20174 switch (ucmdbuf->uscsi_status) { 20175 case STATUS_RESERVATION_CONFLICT: 20176 status = EACCES; 20177 break; 20178 default: 20179 break; 20180 } 20181 break; 20182 default: 20183 break; 20184 } 20185 20186 if (status == 0) { 20187 SD_DUMP_MEMORY(un, SD_LOG_IO, 20188 "sd_send_scsi_GET_CONFIGURATION: data", 20189 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20190 } 20191 20192 SD_TRACE(SD_LOG_IO, un, 20193 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20194 20195 return (status); 20196 } 20197 20198 /* 20199 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20200 * 20201 * Description: Issues the get configuration command to the device to 20202 * retrieve a specfic feature. Called from 20203 * sd_check_for_writable_cd & sd_set_mmc_caps. 20204 * Arguments: un 20205 * ucmdbuf 20206 * rqbuf 20207 * rqbuflen 20208 * bufaddr 20209 * buflen 20210 * feature 20211 * 20212 * Return Code: 0 - Success 20213 * errno return code from sd_send_scsi_cmd() 20214 * 20215 * Context: Can sleep. Does not return until command is completed. 20216 * 20217 */ 20218 static int 20219 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20220 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20221 uchar_t *bufaddr, uint_t buflen, char feature) 20222 { 20223 char cdb[CDB_GROUP1]; 20224 int status; 20225 20226 ASSERT(un != NULL); 20227 ASSERT(!mutex_owned(SD_MUTEX(un))); 20228 ASSERT(bufaddr != NULL); 20229 ASSERT(ucmdbuf != NULL); 20230 ASSERT(rqbuf != NULL); 20231 20232 SD_TRACE(SD_LOG_IO, un, 20233 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20234 20235 bzero(cdb, sizeof (cdb)); 20236 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20237 bzero(rqbuf, rqbuflen); 20238 bzero(bufaddr, buflen); 20239 20240 /* 20241 * Set up cdb field for the get configuration command. 20242 */ 20243 cdb[0] = SCMD_GET_CONFIGURATION; 20244 cdb[1] = 0x02; /* Requested Type */ 20245 cdb[3] = feature; 20246 cdb[8] = buflen; 20247 ucmdbuf->uscsi_cdb = cdb; 20248 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20249 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20250 ucmdbuf->uscsi_buflen = buflen; 20251 ucmdbuf->uscsi_timeout = sd_io_time; 20252 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20253 ucmdbuf->uscsi_rqlen = rqbuflen; 20254 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20255 20256 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20257 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20258 20259 switch (status) { 20260 case 0: 20261 break; /* Success! */ 20262 case EIO: 20263 switch (ucmdbuf->uscsi_status) { 20264 case STATUS_RESERVATION_CONFLICT: 20265 status = EACCES; 20266 break; 20267 default: 20268 break; 20269 } 20270 break; 20271 default: 20272 break; 20273 } 20274 20275 if (status == 0) { 20276 SD_DUMP_MEMORY(un, SD_LOG_IO, 20277 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20278 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20279 } 20280 20281 SD_TRACE(SD_LOG_IO, un, 20282 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20283 20284 return (status); 20285 } 20286 20287 20288 /* 20289 * Function: sd_send_scsi_MODE_SENSE 20290 * 20291 * Description: Utility function for issuing a scsi MODE SENSE command. 20292 * Note: This routine uses a consistent implementation for Group0, 20293 * Group1, and Group2 commands across all platforms. ATAPI devices 20294 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20295 * 20296 * Arguments: un - pointer to the softstate struct for the target. 20297 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20298 * CDB_GROUP[1|2] (10 byte). 20299 * bufaddr - buffer for page data retrieved from the target. 20300 * buflen - size of page to be retrieved. 20301 * page_code - page code of data to be retrieved from the target. 20302 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20303 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20304 * to use the USCSI "direct" chain and bypass the normal 20305 * command waitq. 20306 * 20307 * Return Code: 0 - Success 20308 * errno return code from sd_send_scsi_cmd() 20309 * 20310 * Context: Can sleep. Does not return until command is completed. 20311 */ 20312 20313 static int 20314 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20315 size_t buflen, uchar_t page_code, int path_flag) 20316 { 20317 struct scsi_extended_sense sense_buf; 20318 union scsi_cdb cdb; 20319 struct uscsi_cmd ucmd_buf; 20320 int status; 20321 20322 ASSERT(un != NULL); 20323 ASSERT(!mutex_owned(SD_MUTEX(un))); 20324 ASSERT(bufaddr != NULL); 20325 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20326 (cdbsize == CDB_GROUP2)); 20327 20328 SD_TRACE(SD_LOG_IO, un, 20329 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20330 20331 bzero(&cdb, sizeof (cdb)); 20332 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20333 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20334 bzero(bufaddr, buflen); 20335 20336 if (cdbsize == CDB_GROUP0) { 20337 cdb.scc_cmd = SCMD_MODE_SENSE; 20338 cdb.cdb_opaque[2] = page_code; 20339 FORMG0COUNT(&cdb, buflen); 20340 } else { 20341 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20342 cdb.cdb_opaque[2] = page_code; 20343 FORMG1COUNT(&cdb, buflen); 20344 } 20345 20346 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20347 20348 ucmd_buf.uscsi_cdb = (char *)&cdb; 20349 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20350 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20351 ucmd_buf.uscsi_buflen = buflen; 20352 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20353 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20354 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20355 ucmd_buf.uscsi_timeout = 60; 20356 20357 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20358 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20359 20360 switch (status) { 20361 case 0: 20362 break; /* Success! */ 20363 case EIO: 20364 switch (ucmd_buf.uscsi_status) { 20365 case STATUS_RESERVATION_CONFLICT: 20366 status = EACCES; 20367 break; 20368 default: 20369 break; 20370 } 20371 break; 20372 default: 20373 break; 20374 } 20375 20376 if (status == 0) { 20377 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20378 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20379 } 20380 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20381 20382 return (status); 20383 } 20384 20385 20386 /* 20387 * Function: sd_send_scsi_MODE_SELECT 20388 * 20389 * Description: Utility function for issuing a scsi MODE SELECT command. 20390 * Note: This routine uses a consistent implementation for Group0, 20391 * Group1, and Group2 commands across all platforms. ATAPI devices 20392 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20393 * 20394 * Arguments: un - pointer to the softstate struct for the target. 20395 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20396 * CDB_GROUP[1|2] (10 byte). 20397 * bufaddr - buffer for page data retrieved from the target. 20398 * buflen - size of page to be retrieved. 20399 * save_page - boolean to determin if SP bit should be set. 20400 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20401 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20402 * to use the USCSI "direct" chain and bypass the normal 20403 * command waitq. 20404 * 20405 * Return Code: 0 - Success 20406 * errno return code from sd_send_scsi_cmd() 20407 * 20408 * Context: Can sleep. Does not return until command is completed. 20409 */ 20410 20411 static int 20412 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20413 size_t buflen, uchar_t save_page, int path_flag) 20414 { 20415 struct scsi_extended_sense sense_buf; 20416 union scsi_cdb cdb; 20417 struct uscsi_cmd ucmd_buf; 20418 int status; 20419 20420 ASSERT(un != NULL); 20421 ASSERT(!mutex_owned(SD_MUTEX(un))); 20422 ASSERT(bufaddr != NULL); 20423 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20424 (cdbsize == CDB_GROUP2)); 20425 20426 SD_TRACE(SD_LOG_IO, un, 20427 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20428 20429 bzero(&cdb, sizeof (cdb)); 20430 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20431 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20432 20433 /* Set the PF bit for many third party drives */ 20434 cdb.cdb_opaque[1] = 0x10; 20435 20436 /* Set the savepage(SP) bit if given */ 20437 if (save_page == SD_SAVE_PAGE) { 20438 cdb.cdb_opaque[1] |= 0x01; 20439 } 20440 20441 if (cdbsize == CDB_GROUP0) { 20442 cdb.scc_cmd = SCMD_MODE_SELECT; 20443 FORMG0COUNT(&cdb, buflen); 20444 } else { 20445 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20446 FORMG1COUNT(&cdb, buflen); 20447 } 20448 20449 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20450 20451 ucmd_buf.uscsi_cdb = (char *)&cdb; 20452 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20453 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20454 ucmd_buf.uscsi_buflen = buflen; 20455 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20456 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20457 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20458 ucmd_buf.uscsi_timeout = 60; 20459 20460 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20461 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20462 20463 switch (status) { 20464 case 0: 20465 break; /* Success! */ 20466 case EIO: 20467 switch (ucmd_buf.uscsi_status) { 20468 case STATUS_RESERVATION_CONFLICT: 20469 status = EACCES; 20470 break; 20471 default: 20472 break; 20473 } 20474 break; 20475 default: 20476 break; 20477 } 20478 20479 if (status == 0) { 20480 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20481 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20482 } 20483 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20484 20485 return (status); 20486 } 20487 20488 20489 /* 20490 * Function: sd_send_scsi_RDWR 20491 * 20492 * Description: Issue a scsi READ or WRITE command with the given parameters. 20493 * 20494 * Arguments: un: Pointer to the sd_lun struct for the target. 20495 * cmd: SCMD_READ or SCMD_WRITE 20496 * bufaddr: Address of caller's buffer to receive the RDWR data 20497 * buflen: Length of caller's buffer receive the RDWR data. 20498 * start_block: Block number for the start of the RDWR operation. 20499 * (Assumes target-native block size.) 20500 * residp: Pointer to variable to receive the redisual of the 20501 * RDWR operation (may be NULL of no residual requested). 20502 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20503 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20504 * to use the USCSI "direct" chain and bypass the normal 20505 * command waitq. 20506 * 20507 * Return Code: 0 - Success 20508 * errno return code from sd_send_scsi_cmd() 20509 * 20510 * Context: Can sleep. Does not return until command is completed. 20511 */ 20512 20513 static int 20514 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 20515 size_t buflen, daddr_t start_block, int path_flag) 20516 { 20517 struct scsi_extended_sense sense_buf; 20518 union scsi_cdb cdb; 20519 struct uscsi_cmd ucmd_buf; 20520 uint32_t block_count; 20521 int status; 20522 int cdbsize; 20523 uchar_t flag; 20524 20525 ASSERT(un != NULL); 20526 ASSERT(!mutex_owned(SD_MUTEX(un))); 20527 ASSERT(bufaddr != NULL); 20528 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20529 20530 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20531 20532 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20533 return (EINVAL); 20534 } 20535 20536 mutex_enter(SD_MUTEX(un)); 20537 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 20538 mutex_exit(SD_MUTEX(un)); 20539 20540 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 20541 20542 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 20543 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 20544 bufaddr, buflen, start_block, block_count); 20545 20546 bzero(&cdb, sizeof (cdb)); 20547 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20548 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20549 20550 /* Compute CDB size to use */ 20551 if (start_block > 0xffffffff) 20552 cdbsize = CDB_GROUP4; 20553 else if ((start_block & 0xFFE00000) || 20554 (un->un_f_cfg_is_atapi == TRUE)) 20555 cdbsize = CDB_GROUP1; 20556 else 20557 cdbsize = CDB_GROUP0; 20558 20559 switch (cdbsize) { 20560 case CDB_GROUP0: /* 6-byte CDBs */ 20561 cdb.scc_cmd = cmd; 20562 FORMG0ADDR(&cdb, start_block); 20563 FORMG0COUNT(&cdb, block_count); 20564 break; 20565 case CDB_GROUP1: /* 10-byte CDBs */ 20566 cdb.scc_cmd = cmd | SCMD_GROUP1; 20567 FORMG1ADDR(&cdb, start_block); 20568 FORMG1COUNT(&cdb, block_count); 20569 break; 20570 case CDB_GROUP4: /* 16-byte CDBs */ 20571 cdb.scc_cmd = cmd | SCMD_GROUP4; 20572 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 20573 FORMG4COUNT(&cdb, block_count); 20574 break; 20575 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 20576 default: 20577 /* All others reserved */ 20578 return (EINVAL); 20579 } 20580 20581 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 20582 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20583 20584 ucmd_buf.uscsi_cdb = (char *)&cdb; 20585 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20586 ucmd_buf.uscsi_bufaddr = bufaddr; 20587 ucmd_buf.uscsi_buflen = buflen; 20588 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20589 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20590 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 20591 ucmd_buf.uscsi_timeout = 60; 20592 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20593 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20594 switch (status) { 20595 case 0: 20596 break; /* Success! */ 20597 case EIO: 20598 switch (ucmd_buf.uscsi_status) { 20599 case STATUS_RESERVATION_CONFLICT: 20600 status = EACCES; 20601 break; 20602 default: 20603 break; 20604 } 20605 break; 20606 default: 20607 break; 20608 } 20609 20610 if (status == 0) { 20611 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 20612 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20613 } 20614 20615 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 20616 20617 return (status); 20618 } 20619 20620 20621 /* 20622 * Function: sd_send_scsi_LOG_SENSE 20623 * 20624 * Description: Issue a scsi LOG_SENSE command with the given parameters. 20625 * 20626 * Arguments: un: Pointer to the sd_lun struct for the target. 20627 * 20628 * Return Code: 0 - Success 20629 * errno return code from sd_send_scsi_cmd() 20630 * 20631 * Context: Can sleep. Does not return until command is completed. 20632 */ 20633 20634 static int 20635 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 20636 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 20637 int path_flag) 20638 20639 { 20640 struct scsi_extended_sense sense_buf; 20641 union scsi_cdb cdb; 20642 struct uscsi_cmd ucmd_buf; 20643 int status; 20644 20645 ASSERT(un != NULL); 20646 ASSERT(!mutex_owned(SD_MUTEX(un))); 20647 20648 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 20649 20650 bzero(&cdb, sizeof (cdb)); 20651 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20652 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20653 20654 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 20655 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 20656 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 20657 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 20658 FORMG1COUNT(&cdb, buflen); 20659 20660 ucmd_buf.uscsi_cdb = (char *)&cdb; 20661 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20662 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20663 ucmd_buf.uscsi_buflen = buflen; 20664 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20665 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20666 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20667 ucmd_buf.uscsi_timeout = 60; 20668 20669 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20670 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20671 20672 switch (status) { 20673 case 0: 20674 break; 20675 case EIO: 20676 switch (ucmd_buf.uscsi_status) { 20677 case STATUS_RESERVATION_CONFLICT: 20678 status = EACCES; 20679 break; 20680 case STATUS_CHECK: 20681 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20682 (sense_buf.es_key == KEY_ILLEGAL_REQUEST) && 20683 (sense_buf.es_add_code == 0x24)) { 20684 /* 20685 * ASC 0x24: INVALID FIELD IN CDB 20686 */ 20687 switch (page_code) { 20688 case START_STOP_CYCLE_PAGE: 20689 /* 20690 * The start stop cycle counter is 20691 * implemented as page 0x31 in earlier 20692 * generation disks. In new generation 20693 * disks the start stop cycle counter is 20694 * implemented as page 0xE. To properly 20695 * handle this case if an attempt for 20696 * log page 0xE is made and fails we 20697 * will try again using page 0x31. 20698 * 20699 * Network storage BU committed to 20700 * maintain the page 0x31 for this 20701 * purpose and will not have any other 20702 * page implemented with page code 0x31 20703 * until all disks transition to the 20704 * standard page. 20705 */ 20706 mutex_enter(SD_MUTEX(un)); 20707 un->un_start_stop_cycle_page = 20708 START_STOP_CYCLE_VU_PAGE; 20709 cdb.cdb_opaque[2] = 20710 (char)(page_control << 6) | 20711 un->un_start_stop_cycle_page; 20712 mutex_exit(SD_MUTEX(un)); 20713 status = sd_send_scsi_cmd( 20714 SD_GET_DEV(un), &ucmd_buf, 20715 UIO_SYSSPACE, UIO_SYSSPACE, 20716 UIO_SYSSPACE, path_flag); 20717 20718 break; 20719 case TEMPERATURE_PAGE: 20720 status = ENOTTY; 20721 break; 20722 default: 20723 break; 20724 } 20725 } 20726 break; 20727 default: 20728 break; 20729 } 20730 break; 20731 default: 20732 break; 20733 } 20734 20735 if (status == 0) { 20736 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 20737 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20738 } 20739 20740 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 20741 20742 return (status); 20743 } 20744 20745 20746 /* 20747 * Function: sdioctl 20748 * 20749 * Description: Driver's ioctl(9e) entry point function. 20750 * 20751 * Arguments: dev - device number 20752 * cmd - ioctl operation to be performed 20753 * arg - user argument, contains data to be set or reference 20754 * parameter for get 20755 * flag - bit flag, indicating open settings, 32/64 bit type 20756 * cred_p - user credential pointer 20757 * rval_p - calling process return value (OPT) 20758 * 20759 * Return Code: EINVAL 20760 * ENOTTY 20761 * ENXIO 20762 * EIO 20763 * EFAULT 20764 * ENOTSUP 20765 * EPERM 20766 * 20767 * Context: Called from the device switch at normal priority. 20768 */ 20769 20770 static int 20771 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 20772 { 20773 struct sd_lun *un = NULL; 20774 int geom_validated = FALSE; 20775 int err = 0; 20776 int i = 0; 20777 cred_t *cr; 20778 20779 /* 20780 * All device accesses go thru sdstrategy where we check on suspend 20781 * status 20782 */ 20783 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20784 return (ENXIO); 20785 } 20786 20787 ASSERT(!mutex_owned(SD_MUTEX(un))); 20788 20789 /* 20790 * Moved this wait from sd_uscsi_strategy to here for 20791 * reasons of deadlock prevention. Internal driver commands, 20792 * specifically those to change a devices power level, result 20793 * in a call to sd_uscsi_strategy. 20794 */ 20795 mutex_enter(SD_MUTEX(un)); 20796 while ((un->un_state == SD_STATE_SUSPENDED) || 20797 (un->un_state == SD_STATE_PM_CHANGING)) { 20798 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 20799 } 20800 /* 20801 * Twiddling the counter here protects commands from now 20802 * through to the top of sd_uscsi_strategy. Without the 20803 * counter inc. a power down, for example, could get in 20804 * after the above check for state is made and before 20805 * execution gets to the top of sd_uscsi_strategy. 20806 * That would cause problems. 20807 */ 20808 un->un_ncmds_in_driver++; 20809 20810 if ((un->un_f_geometry_is_valid == FALSE) && 20811 (flag & (FNDELAY | FNONBLOCK))) { 20812 switch (cmd) { 20813 case CDROMPAUSE: 20814 case CDROMRESUME: 20815 case CDROMPLAYMSF: 20816 case CDROMPLAYTRKIND: 20817 case CDROMREADTOCHDR: 20818 case CDROMREADTOCENTRY: 20819 case CDROMSTOP: 20820 case CDROMSTART: 20821 case CDROMVOLCTRL: 20822 case CDROMSUBCHNL: 20823 case CDROMREADMODE2: 20824 case CDROMREADMODE1: 20825 case CDROMREADOFFSET: 20826 case CDROMSBLKMODE: 20827 case CDROMGBLKMODE: 20828 case CDROMGDRVSPEED: 20829 case CDROMSDRVSPEED: 20830 case CDROMCDDA: 20831 case CDROMCDXA: 20832 case CDROMSUBCODE: 20833 if (!ISCD(un)) { 20834 un->un_ncmds_in_driver--; 20835 ASSERT(un->un_ncmds_in_driver >= 0); 20836 mutex_exit(SD_MUTEX(un)); 20837 return (ENOTTY); 20838 } 20839 break; 20840 case FDEJECT: 20841 case DKIOCEJECT: 20842 case CDROMEJECT: 20843 if (!ISREMOVABLE(un)) { 20844 un->un_ncmds_in_driver--; 20845 ASSERT(un->un_ncmds_in_driver >= 0); 20846 mutex_exit(SD_MUTEX(un)); 20847 return (ENOTTY); 20848 } 20849 break; 20850 case DKIOCSVTOC: 20851 case DKIOCSETEFI: 20852 case DKIOCSMBOOT: 20853 case DKIOCFLUSHWRITECACHE: 20854 mutex_exit(SD_MUTEX(un)); 20855 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 20856 if (err != 0) { 20857 mutex_enter(SD_MUTEX(un)); 20858 un->un_ncmds_in_driver--; 20859 ASSERT(un->un_ncmds_in_driver >= 0); 20860 mutex_exit(SD_MUTEX(un)); 20861 return (EIO); 20862 } 20863 mutex_enter(SD_MUTEX(un)); 20864 /* FALLTHROUGH */ 20865 case DKIOCREMOVABLE: 20866 case DKIOCINFO: 20867 case DKIOCGMEDIAINFO: 20868 case MHIOCENFAILFAST: 20869 case MHIOCSTATUS: 20870 case MHIOCTKOWN: 20871 case MHIOCRELEASE: 20872 case MHIOCGRP_INKEYS: 20873 case MHIOCGRP_INRESV: 20874 case MHIOCGRP_REGISTER: 20875 case MHIOCGRP_RESERVE: 20876 case MHIOCGRP_PREEMPTANDABORT: 20877 case MHIOCGRP_REGISTERANDIGNOREKEY: 20878 case CDROMCLOSETRAY: 20879 case USCSICMD: 20880 goto skip_ready_valid; 20881 default: 20882 break; 20883 } 20884 20885 mutex_exit(SD_MUTEX(un)); 20886 err = sd_ready_and_valid(un); 20887 mutex_enter(SD_MUTEX(un)); 20888 if (err == SD_READY_NOT_VALID) { 20889 switch (cmd) { 20890 case DKIOCGAPART: 20891 case DKIOCGGEOM: 20892 case DKIOCSGEOM: 20893 case DKIOCGVTOC: 20894 case DKIOCSVTOC: 20895 case DKIOCSAPART: 20896 case DKIOCG_PHYGEOM: 20897 case DKIOCG_VIRTGEOM: 20898 err = ENOTSUP; 20899 un->un_ncmds_in_driver--; 20900 ASSERT(un->un_ncmds_in_driver >= 0); 20901 mutex_exit(SD_MUTEX(un)); 20902 return (err); 20903 } 20904 } 20905 if (err != SD_READY_VALID) { 20906 switch (cmd) { 20907 case DKIOCSTATE: 20908 case CDROMGDRVSPEED: 20909 case CDROMSDRVSPEED: 20910 case FDEJECT: /* for eject command */ 20911 case DKIOCEJECT: 20912 case CDROMEJECT: 20913 case DKIOCGETEFI: 20914 case DKIOCSGEOM: 20915 case DKIOCREMOVABLE: 20916 case DKIOCSAPART: 20917 case DKIOCSETEFI: 20918 break; 20919 default: 20920 if (ISREMOVABLE(un)) { 20921 err = ENXIO; 20922 } else { 20923 /* Do not map EACCES to EIO */ 20924 if (err != EACCES) 20925 err = EIO; 20926 } 20927 un->un_ncmds_in_driver--; 20928 ASSERT(un->un_ncmds_in_driver >= 0); 20929 mutex_exit(SD_MUTEX(un)); 20930 return (err); 20931 } 20932 } 20933 geom_validated = TRUE; 20934 } 20935 if ((un->un_f_geometry_is_valid == TRUE) && 20936 (un->un_solaris_size > 0)) { 20937 /* 20938 * the "geometry_is_valid" flag could be true if we 20939 * have an fdisk table but no Solaris partition 20940 */ 20941 if (un->un_vtoc.v_sanity != VTOC_SANE) { 20942 /* it is EFI, so return ENOTSUP for these */ 20943 switch (cmd) { 20944 case DKIOCGAPART: 20945 case DKIOCGGEOM: 20946 case DKIOCGVTOC: 20947 case DKIOCSVTOC: 20948 case DKIOCSAPART: 20949 err = ENOTSUP; 20950 un->un_ncmds_in_driver--; 20951 ASSERT(un->un_ncmds_in_driver >= 0); 20952 mutex_exit(SD_MUTEX(un)); 20953 return (err); 20954 } 20955 } 20956 } 20957 20958 skip_ready_valid: 20959 mutex_exit(SD_MUTEX(un)); 20960 20961 switch (cmd) { 20962 case DKIOCINFO: 20963 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 20964 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 20965 break; 20966 20967 case DKIOCGMEDIAINFO: 20968 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 20969 err = sd_get_media_info(dev, (caddr_t)arg, flag); 20970 break; 20971 20972 case DKIOCGGEOM: 20973 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 20974 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 20975 geom_validated); 20976 break; 20977 20978 case DKIOCSGEOM: 20979 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 20980 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 20981 break; 20982 20983 case DKIOCGAPART: 20984 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 20985 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 20986 geom_validated); 20987 break; 20988 20989 case DKIOCSAPART: 20990 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 20991 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 20992 break; 20993 20994 case DKIOCGVTOC: 20995 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 20996 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 20997 geom_validated); 20998 break; 20999 21000 case DKIOCGETEFI: 21001 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21002 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21003 break; 21004 21005 case DKIOCPARTITION: 21006 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21007 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21008 break; 21009 21010 case DKIOCSVTOC: 21011 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21012 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21013 break; 21014 21015 case DKIOCSETEFI: 21016 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21017 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21018 break; 21019 21020 case DKIOCGMBOOT: 21021 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21022 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21023 break; 21024 21025 case DKIOCSMBOOT: 21026 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21027 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21028 break; 21029 21030 case DKIOCLOCK: 21031 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21032 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21033 SD_PATH_STANDARD); 21034 break; 21035 21036 case DKIOCUNLOCK: 21037 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21038 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21039 SD_PATH_STANDARD); 21040 break; 21041 21042 case DKIOCSTATE: { 21043 enum dkio_state state; 21044 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21045 21046 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21047 err = EFAULT; 21048 } else { 21049 err = sd_check_media(dev, state); 21050 if (err == 0) { 21051 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21052 sizeof (int), flag) != 0) 21053 err = EFAULT; 21054 } 21055 } 21056 break; 21057 } 21058 21059 case DKIOCREMOVABLE: 21060 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21061 if (ISREMOVABLE(un)) { 21062 i = 1; 21063 } else { 21064 i = 0; 21065 } 21066 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21067 err = EFAULT; 21068 } else { 21069 err = 0; 21070 } 21071 break; 21072 21073 case DKIOCGTEMPERATURE: 21074 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21075 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21076 break; 21077 21078 case MHIOCENFAILFAST: 21079 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21080 if ((err = drv_priv(cred_p)) == 0) { 21081 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21082 } 21083 break; 21084 21085 case MHIOCTKOWN: 21086 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21087 if ((err = drv_priv(cred_p)) == 0) { 21088 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21089 } 21090 break; 21091 21092 case MHIOCRELEASE: 21093 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21094 if ((err = drv_priv(cred_p)) == 0) { 21095 err = sd_mhdioc_release(dev); 21096 } 21097 break; 21098 21099 case MHIOCSTATUS: 21100 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21101 if ((err = drv_priv(cred_p)) == 0) { 21102 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21103 case 0: 21104 err = 0; 21105 break; 21106 case EACCES: 21107 *rval_p = 1; 21108 err = 0; 21109 break; 21110 default: 21111 err = EIO; 21112 break; 21113 } 21114 } 21115 break; 21116 21117 case MHIOCQRESERVE: 21118 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21119 if ((err = drv_priv(cred_p)) == 0) { 21120 err = sd_reserve_release(dev, SD_RESERVE); 21121 } 21122 break; 21123 21124 case MHIOCREREGISTERDEVID: 21125 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21126 if (drv_priv(cred_p) == EPERM) { 21127 err = EPERM; 21128 } else if (ISREMOVABLE(un) || ISCD(un)) { 21129 err = ENOTTY; 21130 } else { 21131 err = sd_mhdioc_register_devid(dev); 21132 } 21133 break; 21134 21135 case MHIOCGRP_INKEYS: 21136 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21137 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21138 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21139 err = ENOTSUP; 21140 } else { 21141 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21142 flag); 21143 } 21144 } 21145 break; 21146 21147 case MHIOCGRP_INRESV: 21148 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21149 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21150 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21151 err = ENOTSUP; 21152 } else { 21153 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21154 } 21155 } 21156 break; 21157 21158 case MHIOCGRP_REGISTER: 21159 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21160 if ((err = drv_priv(cred_p)) != EPERM) { 21161 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21162 err = ENOTSUP; 21163 } else if (arg != NULL) { 21164 mhioc_register_t reg; 21165 if (ddi_copyin((void *)arg, ®, 21166 sizeof (mhioc_register_t), flag) != 0) { 21167 err = EFAULT; 21168 } else { 21169 err = 21170 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21171 un, SD_SCSI3_REGISTER, 21172 (uchar_t *)®); 21173 } 21174 } 21175 } 21176 break; 21177 21178 case MHIOCGRP_RESERVE: 21179 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21180 if ((err = drv_priv(cred_p)) != EPERM) { 21181 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21182 err = ENOTSUP; 21183 } else if (arg != NULL) { 21184 mhioc_resv_desc_t resv_desc; 21185 if (ddi_copyin((void *)arg, &resv_desc, 21186 sizeof (mhioc_resv_desc_t), flag) != 0) { 21187 err = EFAULT; 21188 } else { 21189 err = 21190 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21191 un, SD_SCSI3_RESERVE, 21192 (uchar_t *)&resv_desc); 21193 } 21194 } 21195 } 21196 break; 21197 21198 case MHIOCGRP_PREEMPTANDABORT: 21199 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21200 if ((err = drv_priv(cred_p)) != EPERM) { 21201 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21202 err = ENOTSUP; 21203 } else if (arg != NULL) { 21204 mhioc_preemptandabort_t preempt_abort; 21205 if (ddi_copyin((void *)arg, &preempt_abort, 21206 sizeof (mhioc_preemptandabort_t), 21207 flag) != 0) { 21208 err = EFAULT; 21209 } else { 21210 err = 21211 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21212 un, SD_SCSI3_PREEMPTANDABORT, 21213 (uchar_t *)&preempt_abort); 21214 } 21215 } 21216 } 21217 break; 21218 21219 case MHIOCGRP_REGISTERANDIGNOREKEY: 21220 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21221 if ((err = drv_priv(cred_p)) != EPERM) { 21222 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21223 err = ENOTSUP; 21224 } else if (arg != NULL) { 21225 mhioc_registerandignorekey_t r_and_i; 21226 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21227 sizeof (mhioc_registerandignorekey_t), 21228 flag) != 0) { 21229 err = EFAULT; 21230 } else { 21231 err = 21232 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21233 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21234 (uchar_t *)&r_and_i); 21235 } 21236 } 21237 } 21238 break; 21239 21240 case USCSICMD: 21241 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21242 cr = ddi_get_cred(); 21243 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21244 err = EPERM; 21245 } else { 21246 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21247 } 21248 break; 21249 21250 case CDROMPAUSE: 21251 case CDROMRESUME: 21252 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21253 if (!ISCD(un)) { 21254 err = ENOTTY; 21255 } else { 21256 err = sr_pause_resume(dev, cmd); 21257 } 21258 break; 21259 21260 case CDROMPLAYMSF: 21261 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21262 if (!ISCD(un)) { 21263 err = ENOTTY; 21264 } else { 21265 err = sr_play_msf(dev, (caddr_t)arg, flag); 21266 } 21267 break; 21268 21269 case CDROMPLAYTRKIND: 21270 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21271 #if defined(__i386) || defined(__amd64) 21272 /* 21273 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21274 */ 21275 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21276 #else 21277 if (!ISCD(un)) { 21278 #endif 21279 err = ENOTTY; 21280 } else { 21281 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21282 } 21283 break; 21284 21285 case CDROMREADTOCHDR: 21286 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21287 if (!ISCD(un)) { 21288 err = ENOTTY; 21289 } else { 21290 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21291 } 21292 break; 21293 21294 case CDROMREADTOCENTRY: 21295 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21296 if (!ISCD(un)) { 21297 err = ENOTTY; 21298 } else { 21299 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21300 } 21301 break; 21302 21303 case CDROMSTOP: 21304 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21305 if (!ISCD(un)) { 21306 err = ENOTTY; 21307 } else { 21308 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21309 SD_PATH_STANDARD); 21310 } 21311 break; 21312 21313 case CDROMSTART: 21314 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21315 if (!ISCD(un)) { 21316 err = ENOTTY; 21317 } else { 21318 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21319 SD_PATH_STANDARD); 21320 } 21321 break; 21322 21323 case CDROMCLOSETRAY: 21324 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21325 if (!ISCD(un)) { 21326 err = ENOTTY; 21327 } else { 21328 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21329 SD_PATH_STANDARD); 21330 } 21331 break; 21332 21333 case FDEJECT: /* for eject command */ 21334 case DKIOCEJECT: 21335 case CDROMEJECT: 21336 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21337 if (!ISREMOVABLE(un)) { 21338 err = ENOTTY; 21339 } else { 21340 err = sr_eject(dev); 21341 } 21342 break; 21343 21344 case CDROMVOLCTRL: 21345 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21346 if (!ISCD(un)) { 21347 err = ENOTTY; 21348 } else { 21349 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21350 } 21351 break; 21352 21353 case CDROMSUBCHNL: 21354 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21355 if (!ISCD(un)) { 21356 err = ENOTTY; 21357 } else { 21358 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21359 } 21360 break; 21361 21362 case CDROMREADMODE2: 21363 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21364 if (!ISCD(un)) { 21365 err = ENOTTY; 21366 } else if (un->un_f_cfg_is_atapi == TRUE) { 21367 /* 21368 * If the drive supports READ CD, use that instead of 21369 * switching the LBA size via a MODE SELECT 21370 * Block Descriptor 21371 */ 21372 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21373 } else { 21374 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21375 } 21376 break; 21377 21378 case CDROMREADMODE1: 21379 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21380 if (!ISCD(un)) { 21381 err = ENOTTY; 21382 } else { 21383 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21384 } 21385 break; 21386 21387 case CDROMREADOFFSET: 21388 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21389 if (!ISCD(un)) { 21390 err = ENOTTY; 21391 } else { 21392 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21393 flag); 21394 } 21395 break; 21396 21397 case CDROMSBLKMODE: 21398 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21399 /* 21400 * There is no means of changing block size in case of atapi 21401 * drives, thus return ENOTTY if drive type is atapi 21402 */ 21403 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21404 err = ENOTTY; 21405 } else if (un->un_f_mmc_cap == TRUE) { 21406 21407 /* 21408 * MMC Devices do not support changing the 21409 * logical block size 21410 * 21411 * Note: EINVAL is being returned instead of ENOTTY to 21412 * maintain consistancy with the original mmc 21413 * driver update. 21414 */ 21415 err = EINVAL; 21416 } else { 21417 mutex_enter(SD_MUTEX(un)); 21418 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21419 (un->un_ncmds_in_transport > 0)) { 21420 mutex_exit(SD_MUTEX(un)); 21421 err = EINVAL; 21422 } else { 21423 mutex_exit(SD_MUTEX(un)); 21424 err = sr_change_blkmode(dev, cmd, arg, flag); 21425 } 21426 } 21427 break; 21428 21429 case CDROMGBLKMODE: 21430 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21431 if (!ISCD(un)) { 21432 err = ENOTTY; 21433 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21434 (un->un_f_blockcount_is_valid != FALSE)) { 21435 /* 21436 * Drive is an ATAPI drive so return target block 21437 * size for ATAPI drives since we cannot change the 21438 * blocksize on ATAPI drives. Used primarily to detect 21439 * if an ATAPI cdrom is present. 21440 */ 21441 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21442 sizeof (int), flag) != 0) { 21443 err = EFAULT; 21444 } else { 21445 err = 0; 21446 } 21447 21448 } else { 21449 /* 21450 * Drive supports changing block sizes via a Mode 21451 * Select. 21452 */ 21453 err = sr_change_blkmode(dev, cmd, arg, flag); 21454 } 21455 break; 21456 21457 case CDROMGDRVSPEED: 21458 case CDROMSDRVSPEED: 21459 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21460 if (!ISCD(un)) { 21461 err = ENOTTY; 21462 } else if (un->un_f_mmc_cap == TRUE) { 21463 /* 21464 * Note: In the future the driver implementation 21465 * for getting and 21466 * setting cd speed should entail: 21467 * 1) If non-mmc try the Toshiba mode page 21468 * (sr_change_speed) 21469 * 2) If mmc but no support for Real Time Streaming try 21470 * the SET CD SPEED (0xBB) command 21471 * (sr_atapi_change_speed) 21472 * 3) If mmc and support for Real Time Streaming 21473 * try the GET PERFORMANCE and SET STREAMING 21474 * commands (not yet implemented, 4380808) 21475 */ 21476 /* 21477 * As per recent MMC spec, CD-ROM speed is variable 21478 * and changes with LBA. Since there is no such 21479 * things as drive speed now, fail this ioctl. 21480 * 21481 * Note: EINVAL is returned for consistancy of original 21482 * implementation which included support for getting 21483 * the drive speed of mmc devices but not setting 21484 * the drive speed. Thus EINVAL would be returned 21485 * if a set request was made for an mmc device. 21486 * We no longer support get or set speed for 21487 * mmc but need to remain consistant with regard 21488 * to the error code returned. 21489 */ 21490 err = EINVAL; 21491 } else if (un->un_f_cfg_is_atapi == TRUE) { 21492 err = sr_atapi_change_speed(dev, cmd, arg, flag); 21493 } else { 21494 err = sr_change_speed(dev, cmd, arg, flag); 21495 } 21496 break; 21497 21498 case CDROMCDDA: 21499 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 21500 if (!ISCD(un)) { 21501 err = ENOTTY; 21502 } else { 21503 err = sr_read_cdda(dev, (void *)arg, flag); 21504 } 21505 break; 21506 21507 case CDROMCDXA: 21508 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 21509 if (!ISCD(un)) { 21510 err = ENOTTY; 21511 } else { 21512 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 21513 } 21514 break; 21515 21516 case CDROMSUBCODE: 21517 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 21518 if (!ISCD(un)) { 21519 err = ENOTTY; 21520 } else { 21521 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 21522 } 21523 break; 21524 21525 case DKIOCPARTINFO: { 21526 /* 21527 * Return parameters describing the selected disk slice. 21528 * Note: this ioctl is for the intel platform only 21529 */ 21530 #if defined(__i386) || defined(__amd64) 21531 int part; 21532 21533 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21534 part = SDPART(dev); 21535 21536 /* don't check un_solaris_size for pN */ 21537 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 21538 err = EIO; 21539 } else { 21540 struct part_info p; 21541 21542 p.p_start = (daddr_t)un->un_offset[part]; 21543 p.p_length = (int)un->un_map[part].dkl_nblk; 21544 #ifdef _MULTI_DATAMODEL 21545 switch (ddi_model_convert_from(flag & FMODELS)) { 21546 case DDI_MODEL_ILP32: 21547 { 21548 struct part_info32 p32; 21549 21550 p32.p_start = (daddr32_t)p.p_start; 21551 p32.p_length = p.p_length; 21552 if (ddi_copyout(&p32, (void *)arg, 21553 sizeof (p32), flag)) 21554 err = EFAULT; 21555 break; 21556 } 21557 21558 case DDI_MODEL_NONE: 21559 { 21560 if (ddi_copyout(&p, (void *)arg, sizeof (p), 21561 flag)) 21562 err = EFAULT; 21563 break; 21564 } 21565 } 21566 #else /* ! _MULTI_DATAMODEL */ 21567 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 21568 err = EFAULT; 21569 #endif /* _MULTI_DATAMODEL */ 21570 } 21571 #else 21572 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 21573 err = ENOTTY; 21574 #endif 21575 break; 21576 } 21577 21578 case DKIOCG_PHYGEOM: { 21579 /* Return the driver's notion of the media physical geometry */ 21580 #if defined(__i386) || defined(__amd64) 21581 struct dk_geom disk_geom; 21582 struct dk_geom *dkgp = &disk_geom; 21583 21584 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21585 mutex_enter(SD_MUTEX(un)); 21586 21587 if (un->un_g.dkg_nhead != 0 && 21588 un->un_g.dkg_nsect != 0) { 21589 /* 21590 * We succeeded in getting a geometry, but 21591 * right now it is being reported as just the 21592 * Solaris fdisk partition, just like for 21593 * DKIOCGGEOM. We need to change that to be 21594 * correct for the entire disk now. 21595 */ 21596 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 21597 dkgp->dkg_acyl = 0; 21598 dkgp->dkg_ncyl = un->un_blockcount / 21599 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21600 } else { 21601 bzero(dkgp, sizeof (struct dk_geom)); 21602 /* 21603 * This disk does not have a Solaris VTOC 21604 * so we must present a physical geometry 21605 * that will remain consistent regardless 21606 * of how the disk is used. This will ensure 21607 * that the geometry does not change regardless 21608 * of the fdisk partition type (ie. EFI, FAT32, 21609 * Solaris, etc). 21610 */ 21611 if (ISCD(un)) { 21612 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 21613 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 21614 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 21615 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 21616 } else { 21617 /* 21618 * Invalid un_blockcount can generate invalid 21619 * dk_geom and may result in division by zero 21620 * system failure. Should make sure blockcount 21621 * is valid before using it here. 21622 */ 21623 if (un->un_f_blockcount_is_valid == FALSE) { 21624 mutex_exit(SD_MUTEX(un)); 21625 err = EIO; 21626 21627 break; 21628 } 21629 sd_convert_geometry(un->un_blockcount, dkgp); 21630 dkgp->dkg_acyl = 0; 21631 dkgp->dkg_ncyl = un->un_blockcount / 21632 (dkgp->dkg_nhead * dkgp->dkg_nsect); 21633 } 21634 } 21635 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21636 21637 if (ddi_copyout(dkgp, (void *)arg, 21638 sizeof (struct dk_geom), flag)) { 21639 mutex_exit(SD_MUTEX(un)); 21640 err = EFAULT; 21641 } else { 21642 mutex_exit(SD_MUTEX(un)); 21643 err = 0; 21644 } 21645 #else 21646 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 21647 err = ENOTTY; 21648 #endif 21649 break; 21650 } 21651 21652 case DKIOCG_VIRTGEOM: { 21653 /* Return the driver's notion of the media's logical geometry */ 21654 #if defined(__i386) || defined(__amd64) 21655 struct dk_geom disk_geom; 21656 struct dk_geom *dkgp = &disk_geom; 21657 21658 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21659 mutex_enter(SD_MUTEX(un)); 21660 /* 21661 * If there is no HBA geometry available, or 21662 * if the HBA returned us something that doesn't 21663 * really fit into an Int 13/function 8 geometry 21664 * result, just fail the ioctl. See PSARC 1998/313. 21665 */ 21666 if (un->un_lgeom.g_nhead == 0 || 21667 un->un_lgeom.g_nsect == 0 || 21668 un->un_lgeom.g_ncyl > 1024) { 21669 mutex_exit(SD_MUTEX(un)); 21670 err = EINVAL; 21671 } else { 21672 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 21673 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 21674 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 21675 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 21676 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 21677 21678 if (ddi_copyout(dkgp, (void *)arg, 21679 sizeof (struct dk_geom), flag)) { 21680 mutex_exit(SD_MUTEX(un)); 21681 err = EFAULT; 21682 } else { 21683 mutex_exit(SD_MUTEX(un)); 21684 err = 0; 21685 } 21686 } 21687 #else 21688 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 21689 err = ENOTTY; 21690 #endif 21691 break; 21692 } 21693 #ifdef SDDEBUG 21694 /* RESET/ABORTS testing ioctls */ 21695 case DKIOCRESET: { 21696 int reset_level; 21697 21698 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 21699 err = EFAULT; 21700 } else { 21701 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 21702 "reset_level = 0x%lx\n", reset_level); 21703 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 21704 err = 0; 21705 } else { 21706 err = EIO; 21707 } 21708 } 21709 break; 21710 } 21711 21712 case DKIOCABORT: 21713 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 21714 if (scsi_abort(SD_ADDRESS(un), NULL)) { 21715 err = 0; 21716 } else { 21717 err = EIO; 21718 } 21719 break; 21720 #endif 21721 21722 #ifdef SD_FAULT_INJECTION 21723 /* SDIOC FaultInjection testing ioctls */ 21724 case SDIOCSTART: 21725 case SDIOCSTOP: 21726 case SDIOCINSERTPKT: 21727 case SDIOCINSERTXB: 21728 case SDIOCINSERTUN: 21729 case SDIOCINSERTARQ: 21730 case SDIOCPUSH: 21731 case SDIOCRETRIEVE: 21732 case SDIOCRUN: 21733 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 21734 "SDIOC detected cmd:0x%X:\n", cmd); 21735 /* call error generator */ 21736 sd_faultinjection_ioctl(cmd, arg, un); 21737 err = 0; 21738 break; 21739 21740 #endif /* SD_FAULT_INJECTION */ 21741 21742 case DKIOCFLUSHWRITECACHE: 21743 { 21744 struct dk_callback *dkc = (struct dk_callback *)arg; 21745 21746 mutex_enter(SD_MUTEX(un)); 21747 if (un->un_f_sync_cache_unsupported || 21748 ! un->un_f_write_cache_enabled) { 21749 err = un->un_f_sync_cache_unsupported ? 21750 ENOTSUP : 0; 21751 mutex_exit(SD_MUTEX(un)); 21752 if ((flag & FKIOCTL) && dkc != NULL && 21753 dkc->dkc_callback != NULL) { 21754 (*dkc->dkc_callback)(dkc->dkc_cookie, 21755 err); 21756 /* 21757 * Did callback and reported error. 21758 * Since we did a callback, ioctl 21759 * should return 0. 21760 */ 21761 err = 0; 21762 } 21763 break; 21764 } 21765 mutex_exit(SD_MUTEX(un)); 21766 21767 if ((flag & FKIOCTL) && dkc != NULL && 21768 dkc->dkc_callback != NULL) { 21769 /* async SYNC CACHE request */ 21770 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 21771 } else { 21772 /* synchronous SYNC CACHE request */ 21773 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 21774 } 21775 } 21776 break; 21777 21778 default: 21779 err = ENOTTY; 21780 break; 21781 } 21782 mutex_enter(SD_MUTEX(un)); 21783 un->un_ncmds_in_driver--; 21784 ASSERT(un->un_ncmds_in_driver >= 0); 21785 mutex_exit(SD_MUTEX(un)); 21786 21787 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 21788 return (err); 21789 } 21790 21791 21792 /* 21793 * Function: sd_uscsi_ioctl 21794 * 21795 * Description: This routine is the driver entry point for handling USCSI ioctl 21796 * requests (USCSICMD). 21797 * 21798 * Arguments: dev - the device number 21799 * arg - user provided scsi command 21800 * flag - this argument is a pass through to ddi_copyxxx() 21801 * directly from the mode argument of ioctl(). 21802 * 21803 * Return Code: code returned by sd_send_scsi_cmd 21804 * ENXIO 21805 * EFAULT 21806 * EAGAIN 21807 */ 21808 21809 static int 21810 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 21811 { 21812 #ifdef _MULTI_DATAMODEL 21813 /* 21814 * For use when a 32 bit app makes a call into a 21815 * 64 bit ioctl 21816 */ 21817 struct uscsi_cmd32 uscsi_cmd_32_for_64; 21818 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 21819 model_t model; 21820 #endif /* _MULTI_DATAMODEL */ 21821 struct uscsi_cmd *scmd = NULL; 21822 struct sd_lun *un = NULL; 21823 enum uio_seg uioseg; 21824 char cdb[CDB_GROUP0]; 21825 int rval = 0; 21826 21827 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21828 return (ENXIO); 21829 } 21830 21831 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 21832 21833 scmd = (struct uscsi_cmd *) 21834 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21835 21836 #ifdef _MULTI_DATAMODEL 21837 switch (model = ddi_model_convert_from(flag & FMODELS)) { 21838 case DDI_MODEL_ILP32: 21839 { 21840 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 21841 rval = EFAULT; 21842 goto done; 21843 } 21844 /* 21845 * Convert the ILP32 uscsi data from the 21846 * application to LP64 for internal use. 21847 */ 21848 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 21849 break; 21850 } 21851 case DDI_MODEL_NONE: 21852 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21853 rval = EFAULT; 21854 goto done; 21855 } 21856 break; 21857 } 21858 #else /* ! _MULTI_DATAMODEL */ 21859 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 21860 rval = EFAULT; 21861 goto done; 21862 } 21863 #endif /* _MULTI_DATAMODEL */ 21864 21865 scmd->uscsi_flags &= ~USCSI_NOINTR; 21866 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 21867 if (un->un_f_format_in_progress == TRUE) { 21868 rval = EAGAIN; 21869 goto done; 21870 } 21871 21872 /* 21873 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 21874 * we will have a valid cdb[0] to test. 21875 */ 21876 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 21877 (cdb[0] == SCMD_FORMAT)) { 21878 SD_TRACE(SD_LOG_IOCTL, un, 21879 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21880 mutex_enter(SD_MUTEX(un)); 21881 un->un_f_format_in_progress = TRUE; 21882 mutex_exit(SD_MUTEX(un)); 21883 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21884 SD_PATH_STANDARD); 21885 mutex_enter(SD_MUTEX(un)); 21886 un->un_f_format_in_progress = FALSE; 21887 mutex_exit(SD_MUTEX(un)); 21888 } else { 21889 SD_TRACE(SD_LOG_IOCTL, un, 21890 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 21891 /* 21892 * It's OK to fall into here even if the ddi_copyin() 21893 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 21894 * does this same copyin and will return the EFAULT 21895 * if it fails. 21896 */ 21897 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 21898 SD_PATH_STANDARD); 21899 } 21900 #ifdef _MULTI_DATAMODEL 21901 switch (model) { 21902 case DDI_MODEL_ILP32: 21903 /* 21904 * Convert back to ILP32 before copyout to the 21905 * application 21906 */ 21907 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 21908 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 21909 if (rval != 0) { 21910 rval = EFAULT; 21911 } 21912 } 21913 break; 21914 case DDI_MODEL_NONE: 21915 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21916 if (rval != 0) { 21917 rval = EFAULT; 21918 } 21919 } 21920 break; 21921 } 21922 #else /* ! _MULTI_DATAMODE */ 21923 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 21924 if (rval != 0) { 21925 rval = EFAULT; 21926 } 21927 } 21928 #endif /* _MULTI_DATAMODE */ 21929 done: 21930 kmem_free(scmd, sizeof (struct uscsi_cmd)); 21931 21932 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 21933 21934 return (rval); 21935 } 21936 21937 21938 /* 21939 * Function: sd_dkio_ctrl_info 21940 * 21941 * Description: This routine is the driver entry point for handling controller 21942 * information ioctl requests (DKIOCINFO). 21943 * 21944 * Arguments: dev - the device number 21945 * arg - pointer to user provided dk_cinfo structure 21946 * specifying the controller type and attributes. 21947 * flag - this argument is a pass through to ddi_copyxxx() 21948 * directly from the mode argument of ioctl(). 21949 * 21950 * Return Code: 0 21951 * EFAULT 21952 * ENXIO 21953 */ 21954 21955 static int 21956 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 21957 { 21958 struct sd_lun *un = NULL; 21959 struct dk_cinfo *info; 21960 dev_info_t *pdip; 21961 int lun, tgt; 21962 21963 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21964 return (ENXIO); 21965 } 21966 21967 info = (struct dk_cinfo *) 21968 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 21969 21970 switch (un->un_ctype) { 21971 case CTYPE_CDROM: 21972 info->dki_ctype = DKC_CDROM; 21973 break; 21974 default: 21975 info->dki_ctype = DKC_SCSI_CCS; 21976 break; 21977 } 21978 pdip = ddi_get_parent(SD_DEVINFO(un)); 21979 info->dki_cnum = ddi_get_instance(pdip); 21980 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 21981 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 21982 } else { 21983 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 21984 DK_DEVLEN - 1); 21985 } 21986 21987 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 21988 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 21989 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 21990 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 21991 21992 /* Unit Information */ 21993 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 21994 info->dki_slave = ((tgt << 3) | lun); 21995 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 21996 DK_DEVLEN - 1); 21997 info->dki_flags = DKI_FMTVOL; 21998 info->dki_partition = SDPART(dev); 21999 22000 /* Max Transfer size of this device in blocks */ 22001 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22002 info->dki_addr = 0; 22003 info->dki_space = 0; 22004 info->dki_prio = 0; 22005 info->dki_vec = 0; 22006 22007 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22008 kmem_free(info, sizeof (struct dk_cinfo)); 22009 return (EFAULT); 22010 } else { 22011 kmem_free(info, sizeof (struct dk_cinfo)); 22012 return (0); 22013 } 22014 } 22015 22016 22017 /* 22018 * Function: sd_get_media_info 22019 * 22020 * Description: This routine is the driver entry point for handling ioctl 22021 * requests for the media type or command set profile used by the 22022 * drive to operate on the media (DKIOCGMEDIAINFO). 22023 * 22024 * Arguments: dev - the device number 22025 * arg - pointer to user provided dk_minfo structure 22026 * specifying the media type, logical block size and 22027 * drive capacity. 22028 * flag - this argument is a pass through to ddi_copyxxx() 22029 * directly from the mode argument of ioctl(). 22030 * 22031 * Return Code: 0 22032 * EACCESS 22033 * EFAULT 22034 * ENXIO 22035 * EIO 22036 */ 22037 22038 static int 22039 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22040 { 22041 struct sd_lun *un = NULL; 22042 struct uscsi_cmd com; 22043 struct scsi_inquiry *sinq; 22044 struct dk_minfo media_info; 22045 u_longlong_t media_capacity; 22046 uint64_t capacity; 22047 uint_t lbasize; 22048 uchar_t *out_data; 22049 uchar_t *rqbuf; 22050 int rval = 0; 22051 int rtn; 22052 22053 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22054 (un->un_state == SD_STATE_OFFLINE)) { 22055 return (ENXIO); 22056 } 22057 22058 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22059 22060 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22061 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22062 22063 /* Issue a TUR to determine if the drive is ready with media present */ 22064 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22065 if (rval == ENXIO) { 22066 goto done; 22067 } 22068 22069 /* Now get configuration data */ 22070 if (ISCD(un)) { 22071 media_info.dki_media_type = DK_CDROM; 22072 22073 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22074 if (un->un_f_mmc_cap == TRUE) { 22075 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22076 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22077 22078 if (rtn) { 22079 /* 22080 * Failed for other than an illegal request 22081 * or command not supported 22082 */ 22083 if ((com.uscsi_status == STATUS_CHECK) && 22084 (com.uscsi_rqstatus == STATUS_GOOD)) { 22085 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22086 (rqbuf[12] != 0x20)) { 22087 rval = EIO; 22088 goto done; 22089 } 22090 } 22091 } else { 22092 /* 22093 * The GET CONFIGURATION command succeeded 22094 * so set the media type according to the 22095 * returned data 22096 */ 22097 media_info.dki_media_type = out_data[6]; 22098 media_info.dki_media_type <<= 8; 22099 media_info.dki_media_type |= out_data[7]; 22100 } 22101 } 22102 } else { 22103 /* 22104 * The profile list is not available, so we attempt to identify 22105 * the media type based on the inquiry data 22106 */ 22107 sinq = un->un_sd->sd_inq; 22108 if (sinq->inq_qual == 0) { 22109 /* This is a direct access device */ 22110 media_info.dki_media_type = DK_FIXED_DISK; 22111 22112 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22113 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22114 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22115 media_info.dki_media_type = DK_ZIP; 22116 } else if ( 22117 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22118 media_info.dki_media_type = DK_JAZ; 22119 } 22120 } 22121 } else { 22122 /* Not a CD or direct access so return unknown media */ 22123 media_info.dki_media_type = DK_UNKNOWN; 22124 } 22125 } 22126 22127 /* Now read the capacity so we can provide the lbasize and capacity */ 22128 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22129 SD_PATH_DIRECT)) { 22130 case 0: 22131 break; 22132 case EACCES: 22133 rval = EACCES; 22134 goto done; 22135 default: 22136 rval = EIO; 22137 goto done; 22138 } 22139 22140 media_info.dki_lbsize = lbasize; 22141 media_capacity = capacity; 22142 22143 /* 22144 * sd_send_scsi_READ_CAPACITY() reports capacity in 22145 * un->un_sys_blocksize chunks. So we need to convert it into 22146 * cap.lbasize chunks. 22147 */ 22148 media_capacity *= un->un_sys_blocksize; 22149 media_capacity /= lbasize; 22150 media_info.dki_capacity = media_capacity; 22151 22152 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22153 rval = EFAULT; 22154 /* Put goto. Anybody might add some code below in future */ 22155 goto done; 22156 } 22157 done: 22158 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22159 kmem_free(rqbuf, SENSE_LENGTH); 22160 return (rval); 22161 } 22162 22163 22164 /* 22165 * Function: sd_dkio_get_geometry 22166 * 22167 * Description: This routine is the driver entry point for handling user 22168 * requests to get the device geometry (DKIOCGGEOM). 22169 * 22170 * Arguments: dev - the device number 22171 * arg - pointer to user provided dk_geom structure specifying 22172 * the controller's notion of the current geometry. 22173 * flag - this argument is a pass through to ddi_copyxxx() 22174 * directly from the mode argument of ioctl(). 22175 * geom_validated - flag indicating if the device geometry has been 22176 * previously validated in the sdioctl routine. 22177 * 22178 * Return Code: 0 22179 * EFAULT 22180 * ENXIO 22181 * EIO 22182 */ 22183 22184 static int 22185 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22186 { 22187 struct sd_lun *un = NULL; 22188 struct dk_geom *tmp_geom = NULL; 22189 int rval = 0; 22190 22191 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22192 return (ENXIO); 22193 } 22194 22195 #if defined(__i386) || defined(__amd64) 22196 if (un->un_solaris_size == 0) { 22197 return (EIO); 22198 } 22199 #endif 22200 if (geom_validated == FALSE) { 22201 /* 22202 * sd_validate_geometry does not spin a disk up 22203 * if it was spun down. We need to make sure it 22204 * is ready. 22205 */ 22206 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22207 return (rval); 22208 } 22209 mutex_enter(SD_MUTEX(un)); 22210 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22211 mutex_exit(SD_MUTEX(un)); 22212 } 22213 if (rval) 22214 return (rval); 22215 22216 /* 22217 * Make a local copy of the soft state geometry to avoid some potential 22218 * race conditions associated with holding the mutex and updating the 22219 * write_reinstruct value 22220 */ 22221 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22222 mutex_enter(SD_MUTEX(un)); 22223 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22224 mutex_exit(SD_MUTEX(un)); 22225 22226 if (tmp_geom->dkg_write_reinstruct == 0) { 22227 tmp_geom->dkg_write_reinstruct = 22228 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22229 sd_rot_delay) / (int)60000); 22230 } 22231 22232 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22233 flag); 22234 if (rval != 0) { 22235 rval = EFAULT; 22236 } 22237 22238 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22239 return (rval); 22240 22241 } 22242 22243 22244 /* 22245 * Function: sd_dkio_set_geometry 22246 * 22247 * Description: This routine is the driver entry point for handling user 22248 * requests to set the device geometry (DKIOCSGEOM). The actual 22249 * device geometry is not updated, just the driver "notion" of it. 22250 * 22251 * Arguments: dev - the device number 22252 * arg - pointer to user provided dk_geom structure used to set 22253 * the controller's notion of the current geometry. 22254 * flag - this argument is a pass through to ddi_copyxxx() 22255 * directly from the mode argument of ioctl(). 22256 * 22257 * Return Code: 0 22258 * EFAULT 22259 * ENXIO 22260 * EIO 22261 */ 22262 22263 static int 22264 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22265 { 22266 struct sd_lun *un = NULL; 22267 struct dk_geom *tmp_geom; 22268 struct dk_map *lp; 22269 int rval = 0; 22270 int i; 22271 22272 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22273 return (ENXIO); 22274 } 22275 22276 #if defined(__i386) || defined(__amd64) 22277 if (un->un_solaris_size == 0) { 22278 return (EIO); 22279 } 22280 #endif 22281 /* 22282 * We need to copy the user specified geometry into local 22283 * storage and then update the softstate. We don't want to hold 22284 * the mutex and copyin directly from the user to the soft state 22285 */ 22286 tmp_geom = (struct dk_geom *) 22287 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22288 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22289 if (rval != 0) { 22290 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22291 return (EFAULT); 22292 } 22293 22294 mutex_enter(SD_MUTEX(un)); 22295 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22296 for (i = 0; i < NDKMAP; i++) { 22297 lp = &un->un_map[i]; 22298 un->un_offset[i] = 22299 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22300 #if defined(__i386) || defined(__amd64) 22301 un->un_offset[i] += un->un_solaris_offset; 22302 #endif 22303 } 22304 un->un_f_geometry_is_valid = FALSE; 22305 mutex_exit(SD_MUTEX(un)); 22306 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22307 22308 return (rval); 22309 } 22310 22311 22312 /* 22313 * Function: sd_dkio_get_partition 22314 * 22315 * Description: This routine is the driver entry point for handling user 22316 * requests to get the partition table (DKIOCGAPART). 22317 * 22318 * Arguments: dev - the device number 22319 * arg - pointer to user provided dk_allmap structure specifying 22320 * the controller's notion of the current partition table. 22321 * flag - this argument is a pass through to ddi_copyxxx() 22322 * directly from the mode argument of ioctl(). 22323 * geom_validated - flag indicating if the device geometry has been 22324 * previously validated in the sdioctl routine. 22325 * 22326 * Return Code: 0 22327 * EFAULT 22328 * ENXIO 22329 * EIO 22330 */ 22331 22332 static int 22333 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22334 { 22335 struct sd_lun *un = NULL; 22336 int rval = 0; 22337 int size; 22338 22339 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22340 return (ENXIO); 22341 } 22342 22343 #if defined(__i386) || defined(__amd64) 22344 if (un->un_solaris_size == 0) { 22345 return (EIO); 22346 } 22347 #endif 22348 /* 22349 * Make sure the geometry is valid before getting the partition 22350 * information. 22351 */ 22352 mutex_enter(SD_MUTEX(un)); 22353 if (geom_validated == FALSE) { 22354 /* 22355 * sd_validate_geometry does not spin a disk up 22356 * if it was spun down. We need to make sure it 22357 * is ready before validating the geometry. 22358 */ 22359 mutex_exit(SD_MUTEX(un)); 22360 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22361 return (rval); 22362 } 22363 mutex_enter(SD_MUTEX(un)); 22364 22365 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22366 mutex_exit(SD_MUTEX(un)); 22367 return (rval); 22368 } 22369 } 22370 mutex_exit(SD_MUTEX(un)); 22371 22372 #ifdef _MULTI_DATAMODEL 22373 switch (ddi_model_convert_from(flag & FMODELS)) { 22374 case DDI_MODEL_ILP32: { 22375 struct dk_map32 dk_map32[NDKMAP]; 22376 int i; 22377 22378 for (i = 0; i < NDKMAP; i++) { 22379 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 22380 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 22381 } 22382 size = NDKMAP * sizeof (struct dk_map32); 22383 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 22384 if (rval != 0) { 22385 rval = EFAULT; 22386 } 22387 break; 22388 } 22389 case DDI_MODEL_NONE: 22390 size = NDKMAP * sizeof (struct dk_map); 22391 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22392 if (rval != 0) { 22393 rval = EFAULT; 22394 } 22395 break; 22396 } 22397 #else /* ! _MULTI_DATAMODEL */ 22398 size = NDKMAP * sizeof (struct dk_map); 22399 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 22400 if (rval != 0) { 22401 rval = EFAULT; 22402 } 22403 #endif /* _MULTI_DATAMODEL */ 22404 return (rval); 22405 } 22406 22407 22408 /* 22409 * Function: sd_dkio_set_partition 22410 * 22411 * Description: This routine is the driver entry point for handling user 22412 * requests to set the partition table (DKIOCSAPART). The actual 22413 * device partition is not updated. 22414 * 22415 * Arguments: dev - the device number 22416 * arg - pointer to user provided dk_allmap structure used to set 22417 * the controller's notion of the partition table. 22418 * flag - this argument is a pass through to ddi_copyxxx() 22419 * directly from the mode argument of ioctl(). 22420 * 22421 * Return Code: 0 22422 * EINVAL 22423 * EFAULT 22424 * ENXIO 22425 * EIO 22426 */ 22427 22428 static int 22429 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 22430 { 22431 struct sd_lun *un = NULL; 22432 struct dk_map dk_map[NDKMAP]; 22433 struct dk_map *lp; 22434 int rval = 0; 22435 int size; 22436 int i; 22437 #if defined(_SUNOS_VTOC_16) 22438 struct dkl_partition *vp; 22439 #endif 22440 22441 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22442 return (ENXIO); 22443 } 22444 22445 /* 22446 * Set the map for all logical partitions. We lock 22447 * the priority just to make sure an interrupt doesn't 22448 * come in while the map is half updated. 22449 */ 22450 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 22451 mutex_enter(SD_MUTEX(un)); 22452 if (un->un_blockcount > DK_MAX_BLOCKS) { 22453 mutex_exit(SD_MUTEX(un)); 22454 return (ENOTSUP); 22455 } 22456 mutex_exit(SD_MUTEX(un)); 22457 if (un->un_solaris_size == 0) { 22458 return (EIO); 22459 } 22460 22461 #ifdef _MULTI_DATAMODEL 22462 switch (ddi_model_convert_from(flag & FMODELS)) { 22463 case DDI_MODEL_ILP32: { 22464 struct dk_map32 dk_map32[NDKMAP]; 22465 22466 size = NDKMAP * sizeof (struct dk_map32); 22467 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 22468 if (rval != 0) { 22469 return (EFAULT); 22470 } 22471 for (i = 0; i < NDKMAP; i++) { 22472 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 22473 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 22474 } 22475 break; 22476 } 22477 case DDI_MODEL_NONE: 22478 size = NDKMAP * sizeof (struct dk_map); 22479 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22480 if (rval != 0) { 22481 return (EFAULT); 22482 } 22483 break; 22484 } 22485 #else /* ! _MULTI_DATAMODEL */ 22486 size = NDKMAP * sizeof (struct dk_map); 22487 rval = ddi_copyin((void *)arg, dk_map, size, flag); 22488 if (rval != 0) { 22489 return (EFAULT); 22490 } 22491 #endif /* _MULTI_DATAMODEL */ 22492 22493 mutex_enter(SD_MUTEX(un)); 22494 /* Note: The size used in this bcopy is set based upon the data model */ 22495 bcopy(dk_map, un->un_map, size); 22496 #if defined(_SUNOS_VTOC_16) 22497 vp = (struct dkl_partition *)&(un->un_vtoc); 22498 #endif /* defined(_SUNOS_VTOC_16) */ 22499 for (i = 0; i < NDKMAP; i++) { 22500 lp = &un->un_map[i]; 22501 un->un_offset[i] = 22502 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22503 #if defined(_SUNOS_VTOC_16) 22504 vp->p_start = un->un_offset[i]; 22505 vp->p_size = lp->dkl_nblk; 22506 vp++; 22507 #endif /* defined(_SUNOS_VTOC_16) */ 22508 #if defined(__i386) || defined(__amd64) 22509 un->un_offset[i] += un->un_solaris_offset; 22510 #endif 22511 } 22512 mutex_exit(SD_MUTEX(un)); 22513 return (rval); 22514 } 22515 22516 22517 /* 22518 * Function: sd_dkio_get_vtoc 22519 * 22520 * Description: This routine is the driver entry point for handling user 22521 * requests to get the current volume table of contents 22522 * (DKIOCGVTOC). 22523 * 22524 * Arguments: dev - the device number 22525 * arg - pointer to user provided vtoc structure specifying 22526 * the current vtoc. 22527 * flag - this argument is a pass through to ddi_copyxxx() 22528 * directly from the mode argument of ioctl(). 22529 * geom_validated - flag indicating if the device geometry has been 22530 * previously validated in the sdioctl routine. 22531 * 22532 * Return Code: 0 22533 * EFAULT 22534 * ENXIO 22535 * EIO 22536 */ 22537 22538 static int 22539 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 22540 { 22541 struct sd_lun *un = NULL; 22542 #if defined(_SUNOS_VTOC_8) 22543 struct vtoc user_vtoc; 22544 #endif /* defined(_SUNOS_VTOC_8) */ 22545 int rval = 0; 22546 22547 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22548 return (ENXIO); 22549 } 22550 22551 mutex_enter(SD_MUTEX(un)); 22552 if (geom_validated == FALSE) { 22553 /* 22554 * sd_validate_geometry does not spin a disk up 22555 * if it was spun down. We need to make sure it 22556 * is ready. 22557 */ 22558 mutex_exit(SD_MUTEX(un)); 22559 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22560 return (rval); 22561 } 22562 mutex_enter(SD_MUTEX(un)); 22563 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 22564 mutex_exit(SD_MUTEX(un)); 22565 return (rval); 22566 } 22567 } 22568 22569 #if defined(_SUNOS_VTOC_8) 22570 sd_build_user_vtoc(un, &user_vtoc); 22571 mutex_exit(SD_MUTEX(un)); 22572 22573 #ifdef _MULTI_DATAMODEL 22574 switch (ddi_model_convert_from(flag & FMODELS)) { 22575 case DDI_MODEL_ILP32: { 22576 struct vtoc32 user_vtoc32; 22577 22578 vtoctovtoc32(user_vtoc, user_vtoc32); 22579 if (ddi_copyout(&user_vtoc32, (void *)arg, 22580 sizeof (struct vtoc32), flag)) { 22581 return (EFAULT); 22582 } 22583 break; 22584 } 22585 22586 case DDI_MODEL_NONE: 22587 if (ddi_copyout(&user_vtoc, (void *)arg, 22588 sizeof (struct vtoc), flag)) { 22589 return (EFAULT); 22590 } 22591 break; 22592 } 22593 #else /* ! _MULTI_DATAMODEL */ 22594 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 22595 return (EFAULT); 22596 } 22597 #endif /* _MULTI_DATAMODEL */ 22598 22599 #elif defined(_SUNOS_VTOC_16) 22600 mutex_exit(SD_MUTEX(un)); 22601 22602 #ifdef _MULTI_DATAMODEL 22603 /* 22604 * The un_vtoc structure is a "struct dk_vtoc" which is always 22605 * 32-bit to maintain compatibility with existing on-disk 22606 * structures. Thus, we need to convert the structure when copying 22607 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 22608 * program. If the target is a 32-bit program, then no conversion 22609 * is necessary. 22610 */ 22611 /* LINTED: logical expression always true: op "||" */ 22612 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 22613 switch (ddi_model_convert_from(flag & FMODELS)) { 22614 case DDI_MODEL_ILP32: 22615 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 22616 sizeof (un->un_vtoc), flag)) { 22617 return (EFAULT); 22618 } 22619 break; 22620 22621 case DDI_MODEL_NONE: { 22622 struct vtoc user_vtoc; 22623 22624 vtoc32tovtoc(un->un_vtoc, user_vtoc); 22625 if (ddi_copyout(&user_vtoc, (void *)arg, 22626 sizeof (struct vtoc), flag)) { 22627 return (EFAULT); 22628 } 22629 break; 22630 } 22631 } 22632 #else /* ! _MULTI_DATAMODEL */ 22633 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 22634 flag)) { 22635 return (EFAULT); 22636 } 22637 #endif /* _MULTI_DATAMODEL */ 22638 #else 22639 #error "No VTOC format defined." 22640 #endif 22641 22642 return (rval); 22643 } 22644 22645 static int 22646 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 22647 { 22648 struct sd_lun *un = NULL; 22649 dk_efi_t user_efi; 22650 int rval = 0; 22651 void *buffer; 22652 22653 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 22654 return (ENXIO); 22655 22656 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 22657 return (EFAULT); 22658 22659 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 22660 22661 if ((user_efi.dki_length % un->un_tgt_blocksize) || 22662 (user_efi.dki_length > un->un_max_xfer_size)) 22663 return (EINVAL); 22664 22665 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 22666 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 22667 user_efi.dki_lba, SD_PATH_DIRECT); 22668 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 22669 user_efi.dki_length, flag) != 0) 22670 rval = EFAULT; 22671 22672 kmem_free(buffer, user_efi.dki_length); 22673 return (rval); 22674 } 22675 22676 /* 22677 * Function: sd_build_user_vtoc 22678 * 22679 * Description: This routine populates a pass by reference variable with the 22680 * current volume table of contents. 22681 * 22682 * Arguments: un - driver soft state (unit) structure 22683 * user_vtoc - pointer to vtoc structure to be populated 22684 */ 22685 22686 static void 22687 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22688 { 22689 struct dk_map2 *lpart; 22690 struct dk_map *lmap; 22691 struct partition *vpart; 22692 int nblks; 22693 int i; 22694 22695 ASSERT(mutex_owned(SD_MUTEX(un))); 22696 22697 /* 22698 * Return vtoc structure fields in the provided VTOC area, addressed 22699 * by *vtoc. 22700 */ 22701 bzero(user_vtoc, sizeof (struct vtoc)); 22702 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 22703 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 22704 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 22705 user_vtoc->v_sanity = VTOC_SANE; 22706 user_vtoc->v_version = un->un_vtoc.v_version; 22707 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 22708 user_vtoc->v_sectorsz = un->un_sys_blocksize; 22709 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 22710 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 22711 sizeof (un->un_vtoc.v_reserved)); 22712 /* 22713 * Convert partitioning information. 22714 * 22715 * Note the conversion from starting cylinder number 22716 * to starting sector number. 22717 */ 22718 lmap = un->un_map; 22719 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 22720 vpart = user_vtoc->v_part; 22721 22722 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22723 22724 for (i = 0; i < V_NUMPAR; i++) { 22725 vpart->p_tag = lpart->p_tag; 22726 vpart->p_flag = lpart->p_flag; 22727 vpart->p_start = lmap->dkl_cylno * nblks; 22728 vpart->p_size = lmap->dkl_nblk; 22729 lmap++; 22730 lpart++; 22731 vpart++; 22732 22733 /* (4364927) */ 22734 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 22735 } 22736 22737 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 22738 } 22739 22740 static int 22741 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 22742 { 22743 struct sd_lun *un = NULL; 22744 struct partition64 p64; 22745 int rval = 0; 22746 uint_t nparts; 22747 efi_gpe_t *partitions; 22748 efi_gpt_t *buffer; 22749 diskaddr_t gpe_lba; 22750 22751 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22752 return (ENXIO); 22753 } 22754 22755 if (ddi_copyin((const void *)arg, &p64, 22756 sizeof (struct partition64), flag)) { 22757 return (EFAULT); 22758 } 22759 22760 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 22761 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 22762 1, SD_PATH_DIRECT); 22763 if (rval != 0) 22764 goto done_error; 22765 22766 sd_swap_efi_gpt(buffer); 22767 22768 if ((rval = sd_validate_efi(buffer)) != 0) 22769 goto done_error; 22770 22771 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 22772 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 22773 if (p64.p_partno > nparts) { 22774 /* couldn't find it */ 22775 rval = ESRCH; 22776 goto done_error; 22777 } 22778 /* 22779 * if we're dealing with a partition that's out of the normal 22780 * 16K block, adjust accordingly 22781 */ 22782 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 22783 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 22784 gpe_lba, SD_PATH_DIRECT); 22785 if (rval) { 22786 goto done_error; 22787 } 22788 partitions = (efi_gpe_t *)buffer; 22789 22790 sd_swap_efi_gpe(nparts, partitions); 22791 22792 partitions += p64.p_partno; 22793 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 22794 sizeof (struct uuid)); 22795 p64.p_start = partitions->efi_gpe_StartingLBA; 22796 p64.p_size = partitions->efi_gpe_EndingLBA - 22797 p64.p_start + 1; 22798 22799 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 22800 rval = EFAULT; 22801 22802 done_error: 22803 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 22804 return (rval); 22805 } 22806 22807 22808 /* 22809 * Function: sd_dkio_set_vtoc 22810 * 22811 * Description: This routine is the driver entry point for handling user 22812 * requests to set the current volume table of contents 22813 * (DKIOCSVTOC). 22814 * 22815 * Arguments: dev - the device number 22816 * arg - pointer to user provided vtoc structure used to set the 22817 * current vtoc. 22818 * flag - this argument is a pass through to ddi_copyxxx() 22819 * directly from the mode argument of ioctl(). 22820 * 22821 * Return Code: 0 22822 * EFAULT 22823 * ENXIO 22824 * EINVAL 22825 * ENOTSUP 22826 */ 22827 22828 static int 22829 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 22830 { 22831 struct sd_lun *un = NULL; 22832 struct vtoc user_vtoc; 22833 int rval = 0; 22834 22835 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22836 return (ENXIO); 22837 } 22838 22839 #if defined(__i386) || defined(__amd64) 22840 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 22841 return (EINVAL); 22842 } 22843 #endif 22844 22845 #ifdef _MULTI_DATAMODEL 22846 switch (ddi_model_convert_from(flag & FMODELS)) { 22847 case DDI_MODEL_ILP32: { 22848 struct vtoc32 user_vtoc32; 22849 22850 if (ddi_copyin((const void *)arg, &user_vtoc32, 22851 sizeof (struct vtoc32), flag)) { 22852 return (EFAULT); 22853 } 22854 vtoc32tovtoc(user_vtoc32, user_vtoc); 22855 break; 22856 } 22857 22858 case DDI_MODEL_NONE: 22859 if (ddi_copyin((const void *)arg, &user_vtoc, 22860 sizeof (struct vtoc), flag)) { 22861 return (EFAULT); 22862 } 22863 break; 22864 } 22865 #else /* ! _MULTI_DATAMODEL */ 22866 if (ddi_copyin((const void *)arg, &user_vtoc, 22867 sizeof (struct vtoc), flag)) { 22868 return (EFAULT); 22869 } 22870 #endif /* _MULTI_DATAMODEL */ 22871 22872 mutex_enter(SD_MUTEX(un)); 22873 if (un->un_blockcount > DK_MAX_BLOCKS) { 22874 mutex_exit(SD_MUTEX(un)); 22875 return (ENOTSUP); 22876 } 22877 if (un->un_g.dkg_ncyl == 0) { 22878 mutex_exit(SD_MUTEX(un)); 22879 return (EINVAL); 22880 } 22881 22882 mutex_exit(SD_MUTEX(un)); 22883 sd_clear_efi(un); 22884 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 22885 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 22886 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 22887 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22888 un->un_node_type, NULL); 22889 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 22890 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 22891 un->un_node_type, NULL); 22892 mutex_enter(SD_MUTEX(un)); 22893 22894 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 22895 if ((rval = sd_write_label(dev)) == 0) { 22896 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 22897 != 0) { 22898 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 22899 "sd_dkio_set_vtoc: " 22900 "Failed validate geometry\n"); 22901 } 22902 } 22903 } 22904 22905 /* 22906 * If sd_build_label_vtoc, or sd_write_label failed above write the 22907 * devid anyway, what can it hurt? Also preserve the device id by 22908 * writing to the disk acyl for the case where a devid has been 22909 * fabricated. 22910 */ 22911 if (!ISREMOVABLE(un) && !ISCD(un) && 22912 (un->un_f_opt_fab_devid == TRUE)) { 22913 if (un->un_devid == NULL) { 22914 sd_register_devid(un, SD_DEVINFO(un), 22915 SD_TARGET_IS_UNRESERVED); 22916 } else { 22917 /* 22918 * The device id for this disk has been 22919 * fabricated. Fabricated device id's are 22920 * managed by storing them in the last 2 22921 * available sectors on the drive. The device 22922 * id must be preserved by writing it back out 22923 * to this location. 22924 */ 22925 if (sd_write_deviceid(un) != 0) { 22926 ddi_devid_free(un->un_devid); 22927 un->un_devid = NULL; 22928 } 22929 } 22930 } 22931 mutex_exit(SD_MUTEX(un)); 22932 return (rval); 22933 } 22934 22935 22936 /* 22937 * Function: sd_build_label_vtoc 22938 * 22939 * Description: This routine updates the driver soft state current volume table 22940 * of contents based on a user specified vtoc. 22941 * 22942 * Arguments: un - driver soft state (unit) structure 22943 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 22944 * to update the driver soft state. 22945 * 22946 * Return Code: 0 22947 * EINVAL 22948 */ 22949 22950 static int 22951 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 22952 { 22953 struct dk_map *lmap; 22954 struct partition *vpart; 22955 int nblks; 22956 #if defined(_SUNOS_VTOC_8) 22957 int ncyl; 22958 struct dk_map2 *lpart; 22959 #endif /* defined(_SUNOS_VTOC_8) */ 22960 int i; 22961 22962 ASSERT(mutex_owned(SD_MUTEX(un))); 22963 22964 /* Sanity-check the vtoc */ 22965 if (user_vtoc->v_sanity != VTOC_SANE || 22966 user_vtoc->v_sectorsz != un->un_sys_blocksize || 22967 user_vtoc->v_nparts != V_NUMPAR) { 22968 return (EINVAL); 22969 } 22970 22971 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 22972 if (nblks == 0) { 22973 return (EINVAL); 22974 } 22975 22976 #if defined(_SUNOS_VTOC_8) 22977 vpart = user_vtoc->v_part; 22978 for (i = 0; i < V_NUMPAR; i++) { 22979 if ((vpart->p_start % nblks) != 0) { 22980 return (EINVAL); 22981 } 22982 ncyl = vpart->p_start / nblks; 22983 ncyl += vpart->p_size / nblks; 22984 if ((vpart->p_size % nblks) != 0) { 22985 ncyl++; 22986 } 22987 if (ncyl > (int)un->un_g.dkg_ncyl) { 22988 return (EINVAL); 22989 } 22990 vpart++; 22991 } 22992 #endif /* defined(_SUNOS_VTOC_8) */ 22993 22994 /* Put appropriate vtoc structure fields into the disk label */ 22995 #if defined(_SUNOS_VTOC_16) 22996 /* 22997 * The vtoc is always a 32bit data structure to maintain the 22998 * on-disk format. Convert "in place" instead of bcopying it. 22999 */ 23000 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23001 23002 /* 23003 * in the 16-slice vtoc, starting sectors are expressed in 23004 * numbers *relative* to the start of the Solaris fdisk partition. 23005 */ 23006 lmap = un->un_map; 23007 vpart = user_vtoc->v_part; 23008 23009 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23010 lmap->dkl_cylno = vpart->p_start / nblks; 23011 lmap->dkl_nblk = vpart->p_size; 23012 } 23013 23014 #elif defined(_SUNOS_VTOC_8) 23015 23016 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23017 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23018 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23019 23020 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23021 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23022 23023 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23024 23025 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23026 23027 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23028 sizeof (un->un_vtoc.v_reserved)); 23029 23030 /* 23031 * Note the conversion from starting sector number 23032 * to starting cylinder number. 23033 * Return error if division results in a remainder. 23034 */ 23035 lmap = un->un_map; 23036 lpart = un->un_vtoc.v_part; 23037 vpart = user_vtoc->v_part; 23038 23039 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23040 lpart->p_tag = vpart->p_tag; 23041 lpart->p_flag = vpart->p_flag; 23042 lmap->dkl_cylno = vpart->p_start / nblks; 23043 lmap->dkl_nblk = vpart->p_size; 23044 23045 lmap++; 23046 lpart++; 23047 vpart++; 23048 23049 /* (4387723) */ 23050 #ifdef _LP64 23051 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23052 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23053 } else { 23054 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23055 } 23056 #else 23057 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23058 #endif 23059 } 23060 23061 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23062 #else 23063 #error "No VTOC format defined." 23064 #endif 23065 return (0); 23066 } 23067 23068 /* 23069 * Function: sd_clear_efi 23070 * 23071 * Description: This routine clears all EFI labels. 23072 * 23073 * Arguments: un - driver soft state (unit) structure 23074 * 23075 * Return Code: void 23076 */ 23077 23078 static void 23079 sd_clear_efi(struct sd_lun *un) 23080 { 23081 efi_gpt_t *gpt; 23082 uint_t lbasize; 23083 uint64_t cap; 23084 int rval; 23085 23086 ASSERT(!mutex_owned(SD_MUTEX(un))); 23087 23088 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23089 23090 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23091 goto done; 23092 } 23093 23094 sd_swap_efi_gpt(gpt); 23095 rval = sd_validate_efi(gpt); 23096 if (rval == 0) { 23097 /* clear primary */ 23098 bzero(gpt, sizeof (efi_gpt_t)); 23099 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23100 SD_PATH_DIRECT))) { 23101 SD_INFO(SD_LOG_IO_PARTITION, un, 23102 "sd_clear_efi: clear primary label failed\n"); 23103 } 23104 } 23105 /* the backup */ 23106 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23107 SD_PATH_DIRECT); 23108 if (rval) { 23109 goto done; 23110 } 23111 /* 23112 * The MMC standard allows READ CAPACITY to be 23113 * inaccurate by a bounded amount (in the interest of 23114 * response latency). As a result, failed READs are 23115 * commonplace (due to the reading of metadata and not 23116 * data). Depending on the per-Vendor/drive Sense data, 23117 * the failed READ can cause many (unnecessary) retries. 23118 */ 23119 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23120 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23121 SD_PATH_DIRECT)) != 0) { 23122 goto done; 23123 } 23124 sd_swap_efi_gpt(gpt); 23125 rval = sd_validate_efi(gpt); 23126 if (rval == 0) { 23127 /* clear backup */ 23128 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23129 cap-1); 23130 bzero(gpt, sizeof (efi_gpt_t)); 23131 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23132 cap-1, SD_PATH_DIRECT))) { 23133 SD_INFO(SD_LOG_IO_PARTITION, un, 23134 "sd_clear_efi: clear backup label failed\n"); 23135 } 23136 } 23137 23138 done: 23139 kmem_free(gpt, sizeof (efi_gpt_t)); 23140 } 23141 23142 /* 23143 * Function: sd_set_vtoc 23144 * 23145 * Description: This routine writes data to the appropriate positions 23146 * 23147 * Arguments: un - driver soft state (unit) structure 23148 * dkl - the data to be written 23149 * 23150 * Return: void 23151 */ 23152 23153 static int 23154 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23155 { 23156 void *shadow_buf; 23157 uint_t label_addr; 23158 int sec; 23159 int blk; 23160 int head; 23161 int cyl; 23162 int rval; 23163 23164 #if defined(__i386) || defined(__amd64) 23165 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23166 #else 23167 /* Write the primary label at block 0 of the solaris partition. */ 23168 label_addr = 0; 23169 #endif 23170 23171 if (NOT_DEVBSIZE(un)) { 23172 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23173 /* 23174 * Read the target's first block. 23175 */ 23176 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23177 un->un_tgt_blocksize, label_addr, 23178 SD_PATH_STANDARD)) != 0) { 23179 goto exit; 23180 } 23181 /* 23182 * Copy the contents of the label into the shadow buffer 23183 * which is of the size of target block size. 23184 */ 23185 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23186 } 23187 23188 /* Write the primary label */ 23189 if (NOT_DEVBSIZE(un)) { 23190 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23191 label_addr, SD_PATH_STANDARD); 23192 } else { 23193 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23194 label_addr, SD_PATH_STANDARD); 23195 } 23196 if (rval != 0) { 23197 return (rval); 23198 } 23199 23200 /* 23201 * Calculate where the backup labels go. They are always on 23202 * the last alternate cylinder, but some older drives put them 23203 * on head 2 instead of the last head. They are always on the 23204 * first 5 odd sectors of the appropriate track. 23205 * 23206 * We have no choice at this point, but to believe that the 23207 * disk label is valid. Use the geometry of the disk 23208 * as described in the label. 23209 */ 23210 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23211 head = dkl->dkl_nhead - 1; 23212 23213 /* 23214 * Write and verify the backup labels. Make sure we don't try to 23215 * write past the last cylinder. 23216 */ 23217 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23218 blk = (daddr_t)( 23219 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23220 (head * dkl->dkl_nsect) + sec); 23221 #if defined(__i386) || defined(__amd64) 23222 blk += un->un_solaris_offset; 23223 #endif 23224 if (NOT_DEVBSIZE(un)) { 23225 uint64_t tblk; 23226 /* 23227 * Need to read the block first for read modify write. 23228 */ 23229 tblk = (uint64_t)blk; 23230 blk = (int)((tblk * un->un_sys_blocksize) / 23231 un->un_tgt_blocksize); 23232 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23233 un->un_tgt_blocksize, blk, 23234 SD_PATH_STANDARD)) != 0) { 23235 goto exit; 23236 } 23237 /* 23238 * Modify the shadow buffer with the label. 23239 */ 23240 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23241 rval = sd_send_scsi_WRITE(un, shadow_buf, 23242 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23243 } else { 23244 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23245 blk, SD_PATH_STANDARD); 23246 SD_INFO(SD_LOG_IO_PARTITION, un, 23247 "sd_set_vtoc: wrote backup label %d\n", blk); 23248 } 23249 if (rval != 0) { 23250 goto exit; 23251 } 23252 } 23253 exit: 23254 if (NOT_DEVBSIZE(un)) { 23255 kmem_free(shadow_buf, un->un_tgt_blocksize); 23256 } 23257 return (rval); 23258 } 23259 23260 /* 23261 * Function: sd_clear_vtoc 23262 * 23263 * Description: This routine clears out the VTOC labels. 23264 * 23265 * Arguments: un - driver soft state (unit) structure 23266 * 23267 * Return: void 23268 */ 23269 23270 static void 23271 sd_clear_vtoc(struct sd_lun *un) 23272 { 23273 struct dk_label *dkl; 23274 23275 mutex_exit(SD_MUTEX(un)); 23276 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23277 mutex_enter(SD_MUTEX(un)); 23278 /* 23279 * sd_set_vtoc uses these fields in order to figure out 23280 * where to overwrite the backup labels 23281 */ 23282 dkl->dkl_apc = un->un_g.dkg_apc; 23283 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23284 dkl->dkl_acyl = un->un_g.dkg_acyl; 23285 dkl->dkl_nhead = un->un_g.dkg_nhead; 23286 dkl->dkl_nsect = un->un_g.dkg_nsect; 23287 mutex_exit(SD_MUTEX(un)); 23288 (void) sd_set_vtoc(un, dkl); 23289 kmem_free(dkl, sizeof (struct dk_label)); 23290 23291 mutex_enter(SD_MUTEX(un)); 23292 } 23293 23294 /* 23295 * Function: sd_write_label 23296 * 23297 * Description: This routine will validate and write the driver soft state vtoc 23298 * contents to the device. 23299 * 23300 * Arguments: dev - the device number 23301 * 23302 * Return Code: the code returned by sd_send_scsi_cmd() 23303 * 0 23304 * EINVAL 23305 * ENXIO 23306 * ENOMEM 23307 */ 23308 23309 static int 23310 sd_write_label(dev_t dev) 23311 { 23312 struct sd_lun *un; 23313 struct dk_label *dkl; 23314 short sum; 23315 short *sp; 23316 int i; 23317 int rval; 23318 23319 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23320 (un->un_state == SD_STATE_OFFLINE)) { 23321 return (ENXIO); 23322 } 23323 ASSERT(mutex_owned(SD_MUTEX(un))); 23324 mutex_exit(SD_MUTEX(un)); 23325 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23326 mutex_enter(SD_MUTEX(un)); 23327 23328 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 23329 dkl->dkl_rpm = un->un_g.dkg_rpm; 23330 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 23331 dkl->dkl_apc = un->un_g.dkg_apc; 23332 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 23333 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23334 dkl->dkl_acyl = un->un_g.dkg_acyl; 23335 dkl->dkl_nhead = un->un_g.dkg_nhead; 23336 dkl->dkl_nsect = un->un_g.dkg_nsect; 23337 23338 #if defined(_SUNOS_VTOC_8) 23339 dkl->dkl_obs1 = un->un_g.dkg_obs1; 23340 dkl->dkl_obs2 = un->un_g.dkg_obs2; 23341 dkl->dkl_obs3 = un->un_g.dkg_obs3; 23342 for (i = 0; i < NDKMAP; i++) { 23343 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 23344 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 23345 } 23346 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 23347 #elif defined(_SUNOS_VTOC_16) 23348 dkl->dkl_skew = un->un_dkg_skew; 23349 #else 23350 #error "No VTOC format defined." 23351 #endif 23352 23353 dkl->dkl_magic = DKL_MAGIC; 23354 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 23355 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 23356 23357 /* Construct checksum for the new disk label */ 23358 sum = 0; 23359 sp = (short *)dkl; 23360 i = sizeof (struct dk_label) / sizeof (short); 23361 while (i--) { 23362 sum ^= *sp++; 23363 } 23364 dkl->dkl_cksum = sum; 23365 23366 mutex_exit(SD_MUTEX(un)); 23367 23368 rval = sd_set_vtoc(un, dkl); 23369 exit: 23370 kmem_free(dkl, sizeof (struct dk_label)); 23371 mutex_enter(SD_MUTEX(un)); 23372 return (rval); 23373 } 23374 23375 static int 23376 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 23377 { 23378 struct sd_lun *un = NULL; 23379 dk_efi_t user_efi; 23380 int rval = 0; 23381 void *buffer; 23382 23383 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23384 return (ENXIO); 23385 23386 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23387 return (EFAULT); 23388 23389 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23390 23391 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23392 (user_efi.dki_length > un->un_max_xfer_size)) 23393 return (EINVAL); 23394 23395 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23396 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 23397 rval = EFAULT; 23398 } else { 23399 /* 23400 * let's clear the vtoc labels and clear the softstate 23401 * vtoc. 23402 */ 23403 mutex_enter(SD_MUTEX(un)); 23404 if (un->un_vtoc.v_sanity == VTOC_SANE) { 23405 SD_TRACE(SD_LOG_IO_PARTITION, un, 23406 "sd_dkio_set_efi: CLEAR VTOC\n"); 23407 sd_clear_vtoc(un); 23408 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23409 mutex_exit(SD_MUTEX(un)); 23410 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 23411 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 23412 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 23413 S_IFBLK, 23414 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23415 un->un_node_type, NULL); 23416 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 23417 S_IFCHR, 23418 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23419 un->un_node_type, NULL); 23420 } else 23421 mutex_exit(SD_MUTEX(un)); 23422 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 23423 user_efi.dki_lba, SD_PATH_DIRECT); 23424 if (rval == 0) { 23425 mutex_enter(SD_MUTEX(un)); 23426 un->un_f_geometry_is_valid = FALSE; 23427 mutex_exit(SD_MUTEX(un)); 23428 } 23429 } 23430 kmem_free(buffer, user_efi.dki_length); 23431 return (rval); 23432 } 23433 23434 /* 23435 * Function: sd_dkio_get_mboot 23436 * 23437 * Description: This routine is the driver entry point for handling user 23438 * requests to get the current device mboot (DKIOCGMBOOT) 23439 * 23440 * Arguments: dev - the device number 23441 * arg - pointer to user provided mboot structure specifying 23442 * the current mboot. 23443 * flag - this argument is a pass through to ddi_copyxxx() 23444 * directly from the mode argument of ioctl(). 23445 * 23446 * Return Code: 0 23447 * EINVAL 23448 * EFAULT 23449 * ENXIO 23450 */ 23451 23452 static int 23453 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 23454 { 23455 struct sd_lun *un; 23456 struct mboot *mboot; 23457 int rval; 23458 size_t buffer_size; 23459 23460 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 23461 (un->un_state == SD_STATE_OFFLINE)) { 23462 return (ENXIO); 23463 } 23464 23465 #if defined(_SUNOS_VTOC_8) 23466 if ((!ISREMOVABLE(un)) || (arg == NULL)) { 23467 #elif defined(_SUNOS_VTOC_16) 23468 if (arg == NULL) { 23469 #endif 23470 return (EINVAL); 23471 } 23472 23473 /* 23474 * Read the mboot block, located at absolute block 0 on the target. 23475 */ 23476 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 23477 23478 SD_TRACE(SD_LOG_IO_PARTITION, un, 23479 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 23480 23481 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 23482 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 23483 SD_PATH_STANDARD)) == 0) { 23484 if (ddi_copyout(mboot, (void *)arg, 23485 sizeof (struct mboot), flag) != 0) { 23486 rval = EFAULT; 23487 } 23488 } 23489 kmem_free(mboot, buffer_size); 23490 return (rval); 23491 } 23492 23493 23494 /* 23495 * Function: sd_dkio_set_mboot 23496 * 23497 * Description: This routine is the driver entry point for handling user 23498 * requests to validate and set the device master boot 23499 * (DKIOCSMBOOT). 23500 * 23501 * Arguments: dev - the device number 23502 * arg - pointer to user provided mboot structure used to set the 23503 * master boot. 23504 * flag - this argument is a pass through to ddi_copyxxx() 23505 * directly from the mode argument of ioctl(). 23506 * 23507 * Return Code: 0 23508 * EINVAL 23509 * EFAULT 23510 * ENXIO 23511 */ 23512 23513 static int 23514 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 23515 { 23516 struct sd_lun *un = NULL; 23517 struct mboot *mboot = NULL; 23518 int rval; 23519 ushort_t magic; 23520 23521 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23522 return (ENXIO); 23523 } 23524 23525 ASSERT(!mutex_owned(SD_MUTEX(un))); 23526 23527 #if defined(_SUNOS_VTOC_8) 23528 if (!ISREMOVABLE(un)) { 23529 return (EINVAL); 23530 } 23531 #endif 23532 23533 if (arg == NULL) { 23534 return (EINVAL); 23535 } 23536 23537 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 23538 23539 if (ddi_copyin((const void *)arg, mboot, 23540 sizeof (struct mboot), flag) != 0) { 23541 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23542 return (EFAULT); 23543 } 23544 23545 /* Is this really a master boot record? */ 23546 magic = LE_16(mboot->signature); 23547 if (magic != MBB_MAGIC) { 23548 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23549 return (EINVAL); 23550 } 23551 23552 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 23553 SD_PATH_STANDARD); 23554 23555 mutex_enter(SD_MUTEX(un)); 23556 #if defined(__i386) || defined(__amd64) 23557 if (rval == 0) { 23558 /* 23559 * mboot has been written successfully. 23560 * update the fdisk and vtoc tables in memory 23561 */ 23562 rval = sd_update_fdisk_and_vtoc(un); 23563 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 23564 mutex_exit(SD_MUTEX(un)); 23565 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23566 return (rval); 23567 } 23568 } 23569 23570 /* 23571 * If the mboot write fails, write the devid anyway, what can it hurt? 23572 * Also preserve the device id by writing to the disk acyl for the case 23573 * where a devid has been fabricated. 23574 */ 23575 if (!ISREMOVABLE(un) && !ISCD(un) && 23576 (un->un_f_opt_fab_devid == TRUE)) { 23577 if (un->un_devid == NULL) { 23578 sd_register_devid(un, SD_DEVINFO(un), 23579 SD_TARGET_IS_UNRESERVED); 23580 } else { 23581 /* 23582 * The device id for this disk has been 23583 * fabricated. Fabricated device id's are 23584 * managed by storing them in the last 2 23585 * available sectors on the drive. The device 23586 * id must be preserved by writing it back out 23587 * to this location. 23588 */ 23589 if (sd_write_deviceid(un) != 0) { 23590 ddi_devid_free(un->un_devid); 23591 un->un_devid = NULL; 23592 } 23593 } 23594 } 23595 #else 23596 if (rval == 0) { 23597 /* 23598 * mboot has been written successfully. 23599 * set up the default geometry and VTOC 23600 */ 23601 if (un->un_blockcount <= DK_MAX_BLOCKS) 23602 sd_setup_default_geometry(un); 23603 } 23604 #endif 23605 mutex_exit(SD_MUTEX(un)); 23606 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 23607 return (rval); 23608 } 23609 23610 23611 /* 23612 * Function: sd_setup_default_geometry 23613 * 23614 * Description: This local utility routine sets the default geometry as part of 23615 * setting the device mboot. 23616 * 23617 * Arguments: un - driver soft state (unit) structure 23618 * 23619 * Note: This may be redundant with sd_build_default_label. 23620 */ 23621 23622 static void 23623 sd_setup_default_geometry(struct sd_lun *un) 23624 { 23625 /* zero out the soft state geometry and partition table. */ 23626 bzero(&un->un_g, sizeof (struct dk_geom)); 23627 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 23628 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 23629 un->un_asciilabel[0] = '\0'; 23630 23631 /* 23632 * For the rpm, we use the minimum for the disk. 23633 * For the head, cyl and number of sector per track, 23634 * if the capacity <= 1GB, head = 64, sect = 32. 23635 * else head = 255, sect 63 23636 * Note: the capacity should be equal to C*H*S values. 23637 * This will cause some truncation of size due to 23638 * round off errors. For CD-ROMs, this truncation can 23639 * have adverse side effects, so returning ncyl and 23640 * nhead as 1. The nsect will overflow for most of 23641 * CD-ROMs as nsect is of type ushort. 23642 */ 23643 if (ISCD(un)) { 23644 un->un_g.dkg_ncyl = 1; 23645 un->un_g.dkg_nhead = 1; 23646 un->un_g.dkg_nsect = un->un_blockcount; 23647 } else { 23648 if (un->un_blockcount <= 0x1000) { 23649 /* Needed for unlabeled SCSI floppies. */ 23650 un->un_g.dkg_nhead = 2; 23651 un->un_g.dkg_ncyl = 80; 23652 un->un_g.dkg_pcyl = 80; 23653 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 23654 } else if (un->un_blockcount <= 0x200000) { 23655 un->un_g.dkg_nhead = 64; 23656 un->un_g.dkg_nsect = 32; 23657 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 23658 } else { 23659 un->un_g.dkg_nhead = 255; 23660 un->un_g.dkg_nsect = 63; 23661 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 23662 } 23663 un->un_blockcount = un->un_g.dkg_ncyl * 23664 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 23665 } 23666 un->un_g.dkg_acyl = 0; 23667 un->un_g.dkg_bcyl = 0; 23668 un->un_g.dkg_intrlv = 1; 23669 un->un_g.dkg_rpm = 200; 23670 un->un_g.dkg_read_reinstruct = 0; 23671 un->un_g.dkg_write_reinstruct = 0; 23672 if (un->un_g.dkg_pcyl == 0) { 23673 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 23674 } 23675 23676 un->un_map['a'-'a'].dkl_cylno = 0; 23677 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 23678 un->un_map['c'-'a'].dkl_cylno = 0; 23679 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 23680 un->un_f_geometry_is_valid = FALSE; 23681 } 23682 23683 23684 #if defined(__i386) || defined(__amd64) 23685 /* 23686 * Function: sd_update_fdisk_and_vtoc 23687 * 23688 * Description: This local utility routine updates the device fdisk and vtoc 23689 * as part of setting the device mboot. 23690 * 23691 * Arguments: un - driver soft state (unit) structure 23692 * 23693 * Return Code: 0 for success or errno-type return code. 23694 * 23695 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 23696 * these did exist seperately in x86 sd.c!!! 23697 */ 23698 23699 static int 23700 sd_update_fdisk_and_vtoc(struct sd_lun *un) 23701 { 23702 static char labelstring[128]; 23703 static char buf[256]; 23704 char *label = 0; 23705 int count; 23706 int label_rc = 0; 23707 int gvalid = un->un_f_geometry_is_valid; 23708 int fdisk_rval; 23709 int lbasize; 23710 int capacity; 23711 23712 ASSERT(mutex_owned(SD_MUTEX(un))); 23713 23714 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 23715 return (EINVAL); 23716 } 23717 23718 if (un->un_f_blockcount_is_valid == FALSE) { 23719 return (EINVAL); 23720 } 23721 23722 #if defined(_SUNOS_VTOC_16) 23723 /* 23724 * Set up the "whole disk" fdisk partition; this should always 23725 * exist, regardless of whether the disk contains an fdisk table 23726 * or vtoc. 23727 */ 23728 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 23729 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 23730 #endif /* defined(_SUNOS_VTOC_16) */ 23731 23732 /* 23733 * copy the lbasize and capacity so that if they're 23734 * reset while we're not holding the SD_MUTEX(un), we will 23735 * continue to use valid values after the SD_MUTEX(un) is 23736 * reacquired. 23737 */ 23738 lbasize = un->un_tgt_blocksize; 23739 capacity = un->un_blockcount; 23740 23741 /* 23742 * refresh the logical and physical geometry caches. 23743 * (data from mode sense format/rigid disk geometry pages, 23744 * and scsi_ifgetcap("geometry"). 23745 */ 23746 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 23747 23748 /* 23749 * Only DIRECT ACCESS devices will have Sun labels. 23750 * CD's supposedly have a Sun label, too 23751 */ 23752 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) { 23753 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 23754 SD_PATH_DIRECT); 23755 if (fdisk_rval == SD_CMD_FAILURE) { 23756 ASSERT(mutex_owned(SD_MUTEX(un))); 23757 return (EIO); 23758 } 23759 23760 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 23761 ASSERT(mutex_owned(SD_MUTEX(un))); 23762 return (EACCES); 23763 } 23764 23765 if (un->un_solaris_size <= DK_LABEL_LOC) { 23766 /* 23767 * Found fdisk table but no Solaris partition entry, 23768 * so don't call sd_uselabel() and don't create 23769 * a default label. 23770 */ 23771 label_rc = 0; 23772 un->un_f_geometry_is_valid = TRUE; 23773 goto no_solaris_partition; 23774 } 23775 23776 #if defined(_SUNOS_VTOC_8) 23777 label = (char *)un->un_asciilabel; 23778 #elif defined(_SUNOS_VTOC_16) 23779 label = (char *)un->un_vtoc.v_asciilabel; 23780 #else 23781 #error "No VTOC format defined." 23782 #endif 23783 } else if (capacity < 0) { 23784 ASSERT(mutex_owned(SD_MUTEX(un))); 23785 return (EINVAL); 23786 } 23787 23788 /* 23789 * For Removable media We reach here if we have found a 23790 * SOLARIS PARTITION. 23791 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 23792 * PARTITION has changed from the previous one, hence we will setup a 23793 * default VTOC in this case. 23794 */ 23795 if (un->un_f_geometry_is_valid == FALSE) { 23796 sd_build_default_label(un); 23797 label_rc = 0; 23798 } 23799 23800 no_solaris_partition: 23801 if ((!ISREMOVABLE(un) || 23802 (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) && 23803 (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) { 23804 /* 23805 * Print out a message indicating who and what we are. 23806 * We do this only when we happen to really validate the 23807 * geometry. We may call sd_validate_geometry() at other 23808 * times, ioctl()'s like Get VTOC in which case we 23809 * don't want to print the label. 23810 * If the geometry is valid, print the label string, 23811 * else print vendor and product info, if available 23812 */ 23813 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 23814 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 23815 } else { 23816 mutex_enter(&sd_label_mutex); 23817 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 23818 labelstring); 23819 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 23820 &labelstring[64]); 23821 (void) sprintf(buf, "?Vendor '%s', product '%s'", 23822 labelstring, &labelstring[64]); 23823 if (un->un_f_blockcount_is_valid == TRUE) { 23824 (void) sprintf(&buf[strlen(buf)], 23825 ", %" PRIu64 " %u byte blocks\n", 23826 un->un_blockcount, 23827 un->un_tgt_blocksize); 23828 } else { 23829 (void) sprintf(&buf[strlen(buf)], 23830 ", (unknown capacity)\n"); 23831 } 23832 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 23833 mutex_exit(&sd_label_mutex); 23834 } 23835 } 23836 23837 #if defined(_SUNOS_VTOC_16) 23838 /* 23839 * If we have valid geometry, set up the remaining fdisk partitions. 23840 * Note that dkl_cylno is not used for the fdisk map entries, so 23841 * we set it to an entirely bogus value. 23842 */ 23843 for (count = 0; count < FD_NUMPART; count++) { 23844 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 23845 un->un_map[FDISK_P1 + count].dkl_nblk = 23846 un->un_fmap[count].fmap_nblk; 23847 un->un_offset[FDISK_P1 + count] = 23848 un->un_fmap[count].fmap_start; 23849 } 23850 #endif 23851 23852 for (count = 0; count < NDKMAP; count++) { 23853 #if defined(_SUNOS_VTOC_8) 23854 struct dk_map *lp = &un->un_map[count]; 23855 un->un_offset[count] = 23856 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23857 #elif defined(_SUNOS_VTOC_16) 23858 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 23859 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 23860 #else 23861 #error "No VTOC format defined." 23862 #endif 23863 } 23864 23865 ASSERT(mutex_owned(SD_MUTEX(un))); 23866 return (label_rc); 23867 } 23868 #endif 23869 23870 23871 /* 23872 * Function: sd_check_media 23873 * 23874 * Description: This utility routine implements the functionality for the 23875 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23876 * driver state changes from that specified by the user 23877 * (inserted or ejected). For example, if the user specifies 23878 * DKIO_EJECTED and the current media state is inserted this 23879 * routine will immediately return DKIO_INSERTED. However, if the 23880 * current media state is not inserted the user thread will be 23881 * blocked until the drive state changes. If DKIO_NONE is specified 23882 * the user thread will block until a drive state change occurs. 23883 * 23884 * Arguments: dev - the device number 23885 * state - user pointer to a dkio_state, updated with the current 23886 * drive state at return. 23887 * 23888 * Return Code: ENXIO 23889 * EIO 23890 * EAGAIN 23891 * EINTR 23892 */ 23893 23894 static int 23895 sd_check_media(dev_t dev, enum dkio_state state) 23896 { 23897 struct sd_lun *un = NULL; 23898 enum dkio_state prev_state; 23899 opaque_t token = NULL; 23900 int rval = 0; 23901 23902 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23903 return (ENXIO); 23904 } 23905 23906 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23907 23908 mutex_enter(SD_MUTEX(un)); 23909 23910 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23911 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23912 23913 prev_state = un->un_mediastate; 23914 23915 /* is there anything to do? */ 23916 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23917 /* 23918 * submit the request to the scsi_watch service; 23919 * scsi_media_watch_cb() does the real work 23920 */ 23921 mutex_exit(SD_MUTEX(un)); 23922 23923 /* 23924 * This change handles the case where a scsi watch request is 23925 * added to a device that is powered down. To accomplish this 23926 * we power up the device before adding the scsi watch request, 23927 * since the scsi watch sends a TUR directly to the device 23928 * which the device cannot handle if it is powered down. 23929 */ 23930 if (sd_pm_entry(un) != DDI_SUCCESS) { 23931 mutex_enter(SD_MUTEX(un)); 23932 goto done; 23933 } 23934 23935 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 23936 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23937 (caddr_t)dev); 23938 23939 sd_pm_exit(un); 23940 23941 mutex_enter(SD_MUTEX(un)); 23942 if (token == NULL) { 23943 rval = EAGAIN; 23944 goto done; 23945 } 23946 23947 /* 23948 * This is a special case IOCTL that doesn't return 23949 * until the media state changes. Routine sdpower 23950 * knows about and handles this so don't count it 23951 * as an active cmd in the driver, which would 23952 * keep the device busy to the pm framework. 23953 * If the count isn't decremented the device can't 23954 * be powered down. 23955 */ 23956 un->un_ncmds_in_driver--; 23957 ASSERT(un->un_ncmds_in_driver >= 0); 23958 23959 /* 23960 * if a prior request had been made, this will be the same 23961 * token, as scsi_watch was designed that way. 23962 */ 23963 un->un_swr_token = token; 23964 un->un_specified_mediastate = state; 23965 23966 /* 23967 * now wait for media change 23968 * we will not be signalled unless mediastate == state but it is 23969 * still better to test for this condition, since there is a 23970 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23971 */ 23972 SD_TRACE(SD_LOG_COMMON, un, 23973 "sd_check_media: waiting for media state change\n"); 23974 while (un->un_mediastate == state) { 23975 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23976 SD_TRACE(SD_LOG_COMMON, un, 23977 "sd_check_media: waiting for media state " 23978 "was interrupted\n"); 23979 un->un_ncmds_in_driver++; 23980 rval = EINTR; 23981 goto done; 23982 } 23983 SD_TRACE(SD_LOG_COMMON, un, 23984 "sd_check_media: received signal, state=%x\n", 23985 un->un_mediastate); 23986 } 23987 /* 23988 * Inc the counter to indicate the device once again 23989 * has an active outstanding cmd. 23990 */ 23991 un->un_ncmds_in_driver++; 23992 } 23993 23994 /* invalidate geometry */ 23995 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23996 sr_ejected(un); 23997 } 23998 23999 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24000 uint64_t capacity; 24001 uint_t lbasize; 24002 24003 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24004 mutex_exit(SD_MUTEX(un)); 24005 /* 24006 * Since the following routines use SD_PATH_DIRECT, we must 24007 * call PM directly before the upcoming disk accesses. This 24008 * may cause the disk to be power/spin up. 24009 */ 24010 24011 if (sd_pm_entry(un) == DDI_SUCCESS) { 24012 rval = sd_send_scsi_READ_CAPACITY(un, 24013 &capacity, 24014 &lbasize, SD_PATH_DIRECT); 24015 if (rval != 0) { 24016 sd_pm_exit(un); 24017 mutex_enter(SD_MUTEX(un)); 24018 goto done; 24019 } 24020 } else { 24021 rval = EIO; 24022 mutex_enter(SD_MUTEX(un)); 24023 goto done; 24024 } 24025 mutex_enter(SD_MUTEX(un)); 24026 24027 sd_update_block_info(un, lbasize, capacity); 24028 24029 un->un_f_geometry_is_valid = FALSE; 24030 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24031 24032 mutex_exit(SD_MUTEX(un)); 24033 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24034 SD_PATH_DIRECT); 24035 sd_pm_exit(un); 24036 24037 mutex_enter(SD_MUTEX(un)); 24038 } 24039 done: 24040 un->un_f_watcht_stopped = FALSE; 24041 if (un->un_swr_token) { 24042 /* 24043 * Use of this local token and the mutex ensures that we avoid 24044 * some race conditions associated with terminating the 24045 * scsi watch. 24046 */ 24047 token = un->un_swr_token; 24048 un->un_swr_token = (opaque_t)NULL; 24049 mutex_exit(SD_MUTEX(un)); 24050 (void) scsi_watch_request_terminate(token, 24051 SCSI_WATCH_TERMINATE_WAIT); 24052 mutex_enter(SD_MUTEX(un)); 24053 } 24054 24055 /* 24056 * Update the capacity kstat value, if no media previously 24057 * (capacity kstat is 0) and a media has been inserted 24058 * (un_f_blockcount_is_valid == TRUE) 24059 * This is a more generic way then checking for ISREMOVABLE. 24060 */ 24061 if (un->un_errstats) { 24062 struct sd_errstats *stp = NULL; 24063 24064 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24065 if ((stp->sd_capacity.value.ui64 == 0) && 24066 (un->un_f_blockcount_is_valid == TRUE)) { 24067 stp->sd_capacity.value.ui64 = 24068 (uint64_t)((uint64_t)un->un_blockcount * 24069 un->un_sys_blocksize); 24070 } 24071 } 24072 mutex_exit(SD_MUTEX(un)); 24073 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24074 return (rval); 24075 } 24076 24077 24078 /* 24079 * Function: sd_delayed_cv_broadcast 24080 * 24081 * Description: Delayed cv_broadcast to allow for target to recover from media 24082 * insertion. 24083 * 24084 * Arguments: arg - driver soft state (unit) structure 24085 */ 24086 24087 static void 24088 sd_delayed_cv_broadcast(void *arg) 24089 { 24090 struct sd_lun *un = arg; 24091 24092 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24093 24094 mutex_enter(SD_MUTEX(un)); 24095 un->un_dcvb_timeid = NULL; 24096 cv_broadcast(&un->un_state_cv); 24097 mutex_exit(SD_MUTEX(un)); 24098 } 24099 24100 24101 /* 24102 * Function: sd_media_watch_cb 24103 * 24104 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24105 * routine processes the TUR sense data and updates the driver 24106 * state if a transition has occurred. The user thread 24107 * (sd_check_media) is then signalled. 24108 * 24109 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24110 * among multiple watches that share this callback function 24111 * resultp - scsi watch facility result packet containing scsi 24112 * packet, status byte and sense data 24113 * 24114 * Return Code: 0 for success, -1 for failure 24115 */ 24116 24117 static int 24118 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24119 { 24120 struct sd_lun *un; 24121 struct scsi_status *statusp = resultp->statusp; 24122 struct scsi_extended_sense *sensep = resultp->sensep; 24123 enum dkio_state state = DKIO_NONE; 24124 dev_t dev = (dev_t)arg; 24125 uchar_t actual_sense_length; 24126 24127 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24128 return (-1); 24129 } 24130 actual_sense_length = resultp->actual_sense_length; 24131 24132 mutex_enter(SD_MUTEX(un)); 24133 SD_TRACE(SD_LOG_COMMON, un, 24134 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24135 *((char *)statusp), (void *)sensep, actual_sense_length); 24136 24137 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24138 un->un_mediastate = DKIO_DEV_GONE; 24139 cv_broadcast(&un->un_state_cv); 24140 mutex_exit(SD_MUTEX(un)); 24141 24142 return (0); 24143 } 24144 24145 /* 24146 * If there was a check condition then sensep points to valid sense data 24147 * If status was not a check condition but a reservation or busy status 24148 * then the new state is DKIO_NONE 24149 */ 24150 if (sensep != NULL) { 24151 SD_INFO(SD_LOG_COMMON, un, 24152 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24153 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 24154 /* This routine only uses up to 13 bytes of sense data. */ 24155 if (actual_sense_length >= 13) { 24156 if (sensep->es_key == KEY_UNIT_ATTENTION) { 24157 if (sensep->es_add_code == 0x28) { 24158 state = DKIO_INSERTED; 24159 } 24160 } else { 24161 /* 24162 * if 02/04/02 means that the host 24163 * should send start command. Explicitly 24164 * leave the media state as is 24165 * (inserted) as the media is inserted 24166 * and host has stopped device for PM 24167 * reasons. Upon next true read/write 24168 * to this media will bring the 24169 * device to the right state good for 24170 * media access. 24171 */ 24172 if ((sensep->es_key == KEY_NOT_READY) && 24173 (sensep->es_add_code == 0x3a)) { 24174 state = DKIO_EJECTED; 24175 } 24176 24177 /* 24178 * If the drivge is busy with an operation 24179 * or long write, keep the media in an 24180 * inserted state. 24181 */ 24182 24183 if ((sensep->es_key == KEY_NOT_READY) && 24184 (sensep->es_add_code == 0x04) && 24185 ((sensep->es_qual_code == 0x02) || 24186 (sensep->es_qual_code == 0x07) || 24187 (sensep->es_qual_code == 0x08))) { 24188 state = DKIO_INSERTED; 24189 } 24190 } 24191 } 24192 } else if ((*((char *)statusp) == STATUS_GOOD) && 24193 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24194 state = DKIO_INSERTED; 24195 } 24196 24197 SD_TRACE(SD_LOG_COMMON, un, 24198 "sd_media_watch_cb: state=%x, specified=%x\n", 24199 state, un->un_specified_mediastate); 24200 24201 /* 24202 * now signal the waiting thread if this is *not* the specified state; 24203 * delay the signal if the state is DKIO_INSERTED to allow the target 24204 * to recover 24205 */ 24206 if (state != un->un_specified_mediastate) { 24207 un->un_mediastate = state; 24208 if (state == DKIO_INSERTED) { 24209 /* 24210 * delay the signal to give the drive a chance 24211 * to do what it apparently needs to do 24212 */ 24213 SD_TRACE(SD_LOG_COMMON, un, 24214 "sd_media_watch_cb: delayed cv_broadcast\n"); 24215 if (un->un_dcvb_timeid == NULL) { 24216 un->un_dcvb_timeid = 24217 timeout(sd_delayed_cv_broadcast, un, 24218 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24219 } 24220 } else { 24221 SD_TRACE(SD_LOG_COMMON, un, 24222 "sd_media_watch_cb: immediate cv_broadcast\n"); 24223 cv_broadcast(&un->un_state_cv); 24224 } 24225 } 24226 mutex_exit(SD_MUTEX(un)); 24227 return (0); 24228 } 24229 24230 24231 /* 24232 * Function: sd_dkio_get_temp 24233 * 24234 * Description: This routine is the driver entry point for handling ioctl 24235 * requests to get the disk temperature. 24236 * 24237 * Arguments: dev - the device number 24238 * arg - pointer to user provided dk_temperature structure. 24239 * flag - this argument is a pass through to ddi_copyxxx() 24240 * directly from the mode argument of ioctl(). 24241 * 24242 * Return Code: 0 24243 * EFAULT 24244 * ENXIO 24245 * EAGAIN 24246 */ 24247 24248 static int 24249 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24250 { 24251 struct sd_lun *un = NULL; 24252 struct dk_temperature *dktemp = NULL; 24253 uchar_t *temperature_page; 24254 int rval = 0; 24255 int path_flag = SD_PATH_STANDARD; 24256 24257 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24258 return (ENXIO); 24259 } 24260 24261 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24262 24263 /* copyin the disk temp argument to get the user flags */ 24264 if (ddi_copyin((void *)arg, dktemp, 24265 sizeof (struct dk_temperature), flag) != 0) { 24266 rval = EFAULT; 24267 goto done; 24268 } 24269 24270 /* Initialize the temperature to invalid. */ 24271 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24272 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24273 24274 /* 24275 * Note: Investigate removing the "bypass pm" semantic. 24276 * Can we just bypass PM always? 24277 */ 24278 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24279 path_flag = SD_PATH_DIRECT; 24280 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24281 mutex_enter(&un->un_pm_mutex); 24282 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24283 /* 24284 * If DKT_BYPASS_PM is set, and the drive happens to be 24285 * in low power mode, we can not wake it up, Need to 24286 * return EAGAIN. 24287 */ 24288 mutex_exit(&un->un_pm_mutex); 24289 rval = EAGAIN; 24290 goto done; 24291 } else { 24292 /* 24293 * Indicate to PM the device is busy. This is required 24294 * to avoid a race - i.e. the ioctl is issuing a 24295 * command and the pm framework brings down the device 24296 * to low power mode (possible power cut-off on some 24297 * platforms). 24298 */ 24299 mutex_exit(&un->un_pm_mutex); 24300 if (sd_pm_entry(un) != DDI_SUCCESS) { 24301 rval = EAGAIN; 24302 goto done; 24303 } 24304 } 24305 } 24306 24307 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24308 24309 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 24310 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 24311 goto done2; 24312 } 24313 24314 /* 24315 * For the current temperature verify that the parameter length is 0x02 24316 * and the parameter code is 0x00 24317 */ 24318 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24319 (temperature_page[5] == 0x00)) { 24320 if (temperature_page[9] == 0xFF) { 24321 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24322 } else { 24323 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24324 } 24325 } 24326 24327 /* 24328 * For the reference temperature verify that the parameter 24329 * length is 0x02 and the parameter code is 0x01 24330 */ 24331 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24332 (temperature_page[11] == 0x01)) { 24333 if (temperature_page[15] == 0xFF) { 24334 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24335 } else { 24336 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24337 } 24338 } 24339 24340 /* Do the copyout regardless of the temperature commands status. */ 24341 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24342 flag) != 0) { 24343 rval = EFAULT; 24344 } 24345 24346 done2: 24347 if (path_flag == SD_PATH_DIRECT) { 24348 sd_pm_exit(un); 24349 } 24350 24351 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24352 done: 24353 if (dktemp != NULL) { 24354 kmem_free(dktemp, sizeof (struct dk_temperature)); 24355 } 24356 24357 return (rval); 24358 } 24359 24360 24361 /* 24362 * Function: sd_log_page_supported 24363 * 24364 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24365 * supported log pages. 24366 * 24367 * Arguments: un - 24368 * log_page - 24369 * 24370 * Return Code: -1 - on error (log sense is optional and may not be supported). 24371 * 0 - log page not found. 24372 * 1 - log page found. 24373 */ 24374 24375 static int 24376 sd_log_page_supported(struct sd_lun *un, int log_page) 24377 { 24378 uchar_t *log_page_data; 24379 int i; 24380 int match = 0; 24381 int log_size; 24382 24383 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24384 24385 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 24386 SD_PATH_DIRECT) != 0) { 24387 SD_ERROR(SD_LOG_COMMON, un, 24388 "sd_log_page_supported: failed log page retrieval\n"); 24389 kmem_free(log_page_data, 0xFF); 24390 return (-1); 24391 } 24392 log_size = log_page_data[3]; 24393 24394 /* 24395 * The list of supported log pages start from the fourth byte. Check 24396 * until we run out of log pages or a match is found. 24397 */ 24398 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24399 if (log_page_data[i] == log_page) { 24400 match++; 24401 } 24402 } 24403 kmem_free(log_page_data, 0xFF); 24404 return (match); 24405 } 24406 24407 24408 /* 24409 * Function: sd_mhdioc_failfast 24410 * 24411 * Description: This routine is the driver entry point for handling ioctl 24412 * requests to enable/disable the multihost failfast option. 24413 * (MHIOCENFAILFAST) 24414 * 24415 * Arguments: dev - the device number 24416 * arg - user specified probing interval. 24417 * flag - this argument is a pass through to ddi_copyxxx() 24418 * directly from the mode argument of ioctl(). 24419 * 24420 * Return Code: 0 24421 * EFAULT 24422 * ENXIO 24423 */ 24424 24425 static int 24426 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24427 { 24428 struct sd_lun *un = NULL; 24429 int mh_time; 24430 int rval = 0; 24431 24432 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24433 return (ENXIO); 24434 } 24435 24436 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24437 return (EFAULT); 24438 24439 if (mh_time) { 24440 mutex_enter(SD_MUTEX(un)); 24441 un->un_resvd_status |= SD_FAILFAST; 24442 mutex_exit(SD_MUTEX(un)); 24443 /* 24444 * If mh_time is INT_MAX, then this ioctl is being used for 24445 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24446 */ 24447 if (mh_time != INT_MAX) { 24448 rval = sd_check_mhd(dev, mh_time); 24449 } 24450 } else { 24451 (void) sd_check_mhd(dev, 0); 24452 mutex_enter(SD_MUTEX(un)); 24453 un->un_resvd_status &= ~SD_FAILFAST; 24454 mutex_exit(SD_MUTEX(un)); 24455 } 24456 return (rval); 24457 } 24458 24459 24460 /* 24461 * Function: sd_mhdioc_takeown 24462 * 24463 * Description: This routine is the driver entry point for handling ioctl 24464 * requests to forcefully acquire exclusive access rights to the 24465 * multihost disk (MHIOCTKOWN). 24466 * 24467 * Arguments: dev - the device number 24468 * arg - user provided structure specifying the delay 24469 * parameters in milliseconds 24470 * flag - this argument is a pass through to ddi_copyxxx() 24471 * directly from the mode argument of ioctl(). 24472 * 24473 * Return Code: 0 24474 * EFAULT 24475 * ENXIO 24476 */ 24477 24478 static int 24479 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24480 { 24481 struct sd_lun *un = NULL; 24482 struct mhioctkown *tkown = NULL; 24483 int rval = 0; 24484 24485 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24486 return (ENXIO); 24487 } 24488 24489 if (arg != NULL) { 24490 tkown = (struct mhioctkown *) 24491 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24492 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24493 if (rval != 0) { 24494 rval = EFAULT; 24495 goto error; 24496 } 24497 } 24498 24499 rval = sd_take_ownership(dev, tkown); 24500 mutex_enter(SD_MUTEX(un)); 24501 if (rval == 0) { 24502 un->un_resvd_status |= SD_RESERVE; 24503 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24504 sd_reinstate_resv_delay = 24505 tkown->reinstate_resv_delay * 1000; 24506 } else { 24507 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24508 } 24509 /* 24510 * Give the scsi_watch routine interval set by 24511 * the MHIOCENFAILFAST ioctl precedence here. 24512 */ 24513 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24514 mutex_exit(SD_MUTEX(un)); 24515 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24516 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24517 "sd_mhdioc_takeown : %d\n", 24518 sd_reinstate_resv_delay); 24519 } else { 24520 mutex_exit(SD_MUTEX(un)); 24521 } 24522 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24523 sd_mhd_reset_notify_cb, (caddr_t)un); 24524 } else { 24525 un->un_resvd_status &= ~SD_RESERVE; 24526 mutex_exit(SD_MUTEX(un)); 24527 } 24528 24529 error: 24530 if (tkown != NULL) { 24531 kmem_free(tkown, sizeof (struct mhioctkown)); 24532 } 24533 return (rval); 24534 } 24535 24536 24537 /* 24538 * Function: sd_mhdioc_release 24539 * 24540 * Description: This routine is the driver entry point for handling ioctl 24541 * requests to release exclusive access rights to the multihost 24542 * disk (MHIOCRELEASE). 24543 * 24544 * Arguments: dev - the device number 24545 * 24546 * Return Code: 0 24547 * ENXIO 24548 */ 24549 24550 static int 24551 sd_mhdioc_release(dev_t dev) 24552 { 24553 struct sd_lun *un = NULL; 24554 timeout_id_t resvd_timeid_save; 24555 int resvd_status_save; 24556 int rval = 0; 24557 24558 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24559 return (ENXIO); 24560 } 24561 24562 mutex_enter(SD_MUTEX(un)); 24563 resvd_status_save = un->un_resvd_status; 24564 un->un_resvd_status &= 24565 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24566 if (un->un_resvd_timeid) { 24567 resvd_timeid_save = un->un_resvd_timeid; 24568 un->un_resvd_timeid = NULL; 24569 mutex_exit(SD_MUTEX(un)); 24570 (void) untimeout(resvd_timeid_save); 24571 } else { 24572 mutex_exit(SD_MUTEX(un)); 24573 } 24574 24575 /* 24576 * destroy any pending timeout thread that may be attempting to 24577 * reinstate reservation on this device. 24578 */ 24579 sd_rmv_resv_reclaim_req(dev); 24580 24581 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24582 mutex_enter(SD_MUTEX(un)); 24583 if ((un->un_mhd_token) && 24584 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24585 mutex_exit(SD_MUTEX(un)); 24586 (void) sd_check_mhd(dev, 0); 24587 } else { 24588 mutex_exit(SD_MUTEX(un)); 24589 } 24590 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24591 sd_mhd_reset_notify_cb, (caddr_t)un); 24592 } else { 24593 /* 24594 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24595 */ 24596 mutex_enter(SD_MUTEX(un)); 24597 un->un_resvd_status = resvd_status_save; 24598 mutex_exit(SD_MUTEX(un)); 24599 } 24600 return (rval); 24601 } 24602 24603 24604 /* 24605 * Function: sd_mhdioc_register_devid 24606 * 24607 * Description: This routine is the driver entry point for handling ioctl 24608 * requests to register the device id (MHIOCREREGISTERDEVID). 24609 * 24610 * Note: The implementation for this ioctl has been updated to 24611 * be consistent with the original PSARC case (1999/357) 24612 * (4375899, 4241671, 4220005) 24613 * 24614 * Arguments: dev - the device number 24615 * 24616 * Return Code: 0 24617 * ENXIO 24618 */ 24619 24620 static int 24621 sd_mhdioc_register_devid(dev_t dev) 24622 { 24623 struct sd_lun *un = NULL; 24624 int rval = 0; 24625 24626 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24627 return (ENXIO); 24628 } 24629 24630 ASSERT(!mutex_owned(SD_MUTEX(un))); 24631 24632 mutex_enter(SD_MUTEX(un)); 24633 24634 /* If a devid already exists, de-register it */ 24635 if (un->un_devid != NULL) { 24636 ddi_devid_unregister(SD_DEVINFO(un)); 24637 /* 24638 * After unregister devid, needs to free devid memory 24639 */ 24640 ddi_devid_free(un->un_devid); 24641 un->un_devid = NULL; 24642 } 24643 24644 /* Check for reservation conflict */ 24645 mutex_exit(SD_MUTEX(un)); 24646 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 24647 mutex_enter(SD_MUTEX(un)); 24648 24649 switch (rval) { 24650 case 0: 24651 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24652 break; 24653 case EACCES: 24654 break; 24655 default: 24656 rval = EIO; 24657 } 24658 24659 mutex_exit(SD_MUTEX(un)); 24660 return (rval); 24661 } 24662 24663 24664 /* 24665 * Function: sd_mhdioc_inkeys 24666 * 24667 * Description: This routine is the driver entry point for handling ioctl 24668 * requests to issue the SCSI-3 Persistent In Read Keys command 24669 * to the device (MHIOCGRP_INKEYS). 24670 * 24671 * Arguments: dev - the device number 24672 * arg - user provided in_keys structure 24673 * flag - this argument is a pass through to ddi_copyxxx() 24674 * directly from the mode argument of ioctl(). 24675 * 24676 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24677 * ENXIO 24678 * EFAULT 24679 */ 24680 24681 static int 24682 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24683 { 24684 struct sd_lun *un; 24685 mhioc_inkeys_t inkeys; 24686 int rval = 0; 24687 24688 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24689 return (ENXIO); 24690 } 24691 24692 #ifdef _MULTI_DATAMODEL 24693 switch (ddi_model_convert_from(flag & FMODELS)) { 24694 case DDI_MODEL_ILP32: { 24695 struct mhioc_inkeys32 inkeys32; 24696 24697 if (ddi_copyin(arg, &inkeys32, 24698 sizeof (struct mhioc_inkeys32), flag) != 0) { 24699 return (EFAULT); 24700 } 24701 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24702 if ((rval = sd_persistent_reservation_in_read_keys(un, 24703 &inkeys, flag)) != 0) { 24704 return (rval); 24705 } 24706 inkeys32.generation = inkeys.generation; 24707 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24708 flag) != 0) { 24709 return (EFAULT); 24710 } 24711 break; 24712 } 24713 case DDI_MODEL_NONE: 24714 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24715 flag) != 0) { 24716 return (EFAULT); 24717 } 24718 if ((rval = sd_persistent_reservation_in_read_keys(un, 24719 &inkeys, flag)) != 0) { 24720 return (rval); 24721 } 24722 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24723 flag) != 0) { 24724 return (EFAULT); 24725 } 24726 break; 24727 } 24728 24729 #else /* ! _MULTI_DATAMODEL */ 24730 24731 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24732 return (EFAULT); 24733 } 24734 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24735 if (rval != 0) { 24736 return (rval); 24737 } 24738 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24739 return (EFAULT); 24740 } 24741 24742 #endif /* _MULTI_DATAMODEL */ 24743 24744 return (rval); 24745 } 24746 24747 24748 /* 24749 * Function: sd_mhdioc_inresv 24750 * 24751 * Description: This routine is the driver entry point for handling ioctl 24752 * requests to issue the SCSI-3 Persistent In Read Reservations 24753 * command to the device (MHIOCGRP_INKEYS). 24754 * 24755 * Arguments: dev - the device number 24756 * arg - user provided in_resv structure 24757 * flag - this argument is a pass through to ddi_copyxxx() 24758 * directly from the mode argument of ioctl(). 24759 * 24760 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24761 * ENXIO 24762 * EFAULT 24763 */ 24764 24765 static int 24766 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24767 { 24768 struct sd_lun *un; 24769 mhioc_inresvs_t inresvs; 24770 int rval = 0; 24771 24772 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24773 return (ENXIO); 24774 } 24775 24776 #ifdef _MULTI_DATAMODEL 24777 24778 switch (ddi_model_convert_from(flag & FMODELS)) { 24779 case DDI_MODEL_ILP32: { 24780 struct mhioc_inresvs32 inresvs32; 24781 24782 if (ddi_copyin(arg, &inresvs32, 24783 sizeof (struct mhioc_inresvs32), flag) != 0) { 24784 return (EFAULT); 24785 } 24786 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24787 if ((rval = sd_persistent_reservation_in_read_resv(un, 24788 &inresvs, flag)) != 0) { 24789 return (rval); 24790 } 24791 inresvs32.generation = inresvs.generation; 24792 if (ddi_copyout(&inresvs32, arg, 24793 sizeof (struct mhioc_inresvs32), flag) != 0) { 24794 return (EFAULT); 24795 } 24796 break; 24797 } 24798 case DDI_MODEL_NONE: 24799 if (ddi_copyin(arg, &inresvs, 24800 sizeof (mhioc_inresvs_t), flag) != 0) { 24801 return (EFAULT); 24802 } 24803 if ((rval = sd_persistent_reservation_in_read_resv(un, 24804 &inresvs, flag)) != 0) { 24805 return (rval); 24806 } 24807 if (ddi_copyout(&inresvs, arg, 24808 sizeof (mhioc_inresvs_t), flag) != 0) { 24809 return (EFAULT); 24810 } 24811 break; 24812 } 24813 24814 #else /* ! _MULTI_DATAMODEL */ 24815 24816 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24817 return (EFAULT); 24818 } 24819 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24820 if (rval != 0) { 24821 return (rval); 24822 } 24823 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24824 return (EFAULT); 24825 } 24826 24827 #endif /* ! _MULTI_DATAMODEL */ 24828 24829 return (rval); 24830 } 24831 24832 24833 /* 24834 * The following routines support the clustering functionality described below 24835 * and implement lost reservation reclaim functionality. 24836 * 24837 * Clustering 24838 * ---------- 24839 * The clustering code uses two different, independent forms of SCSI 24840 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24841 * Persistent Group Reservations. For any particular disk, it will use either 24842 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24843 * 24844 * SCSI-2 24845 * The cluster software takes ownership of a multi-hosted disk by issuing the 24846 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24847 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 24848 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 24849 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 24850 * meaning of failfast is that if the driver (on this host) ever encounters the 24851 * scsi error return code RESERVATION_CONFLICT from the device, it should 24852 * immediately panic the host. The motivation for this ioctl is that if this 24853 * host does encounter reservation conflict, the underlying cause is that some 24854 * other host of the cluster has decided that this host is no longer in the 24855 * cluster and has seized control of the disks for itself. Since this host is no 24856 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 24857 * does two things: 24858 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24859 * error to panic the host 24860 * (b) it sets up a periodic timer to test whether this host still has 24861 * "access" (in that no other host has reserved the device): if the 24862 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24863 * purpose of that periodic timer is to handle scenarios where the host is 24864 * otherwise temporarily quiescent, temporarily doing no real i/o. 24865 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24866 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24867 * the device itself. 24868 * 24869 * SCSI-3 PGR 24870 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24871 * facility is supported through the shared multihost disk ioctls 24872 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24873 * MHIOCGRP_PREEMPTANDABORT) 24874 * 24875 * Reservation Reclaim: 24876 * -------------------- 24877 * To support the lost reservation reclaim operations this driver creates a 24878 * single thread to handle reinstating reservations on all devices that have 24879 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24880 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24881 * and the reservation reclaim thread loops through the requests to regain the 24882 * lost reservations. 24883 */ 24884 24885 /* 24886 * Function: sd_check_mhd() 24887 * 24888 * Description: This function sets up and submits a scsi watch request or 24889 * terminates an existing watch request. This routine is used in 24890 * support of reservation reclaim. 24891 * 24892 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24893 * among multiple watches that share the callback function 24894 * interval - the number of microseconds specifying the watch 24895 * interval for issuing TEST UNIT READY commands. If 24896 * set to 0 the watch should be terminated. If the 24897 * interval is set to 0 and if the device is required 24898 * to hold reservation while disabling failfast, the 24899 * watch is restarted with an interval of 24900 * reinstate_resv_delay. 24901 * 24902 * Return Code: 0 - Successful submit/terminate of scsi watch request 24903 * ENXIO - Indicates an invalid device was specified 24904 * EAGAIN - Unable to submit the scsi watch request 24905 */ 24906 24907 static int 24908 sd_check_mhd(dev_t dev, int interval) 24909 { 24910 struct sd_lun *un; 24911 opaque_t token; 24912 24913 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24914 return (ENXIO); 24915 } 24916 24917 /* is this a watch termination request? */ 24918 if (interval == 0) { 24919 mutex_enter(SD_MUTEX(un)); 24920 /* if there is an existing watch task then terminate it */ 24921 if (un->un_mhd_token) { 24922 token = un->un_mhd_token; 24923 un->un_mhd_token = NULL; 24924 mutex_exit(SD_MUTEX(un)); 24925 (void) scsi_watch_request_terminate(token, 24926 SCSI_WATCH_TERMINATE_WAIT); 24927 mutex_enter(SD_MUTEX(un)); 24928 } else { 24929 mutex_exit(SD_MUTEX(un)); 24930 /* 24931 * Note: If we return here we don't check for the 24932 * failfast case. This is the original legacy 24933 * implementation but perhaps we should be checking 24934 * the failfast case. 24935 */ 24936 return (0); 24937 } 24938 /* 24939 * If the device is required to hold reservation while 24940 * disabling failfast, we need to restart the scsi_watch 24941 * routine with an interval of reinstate_resv_delay. 24942 */ 24943 if (un->un_resvd_status & SD_RESERVE) { 24944 interval = sd_reinstate_resv_delay/1000; 24945 } else { 24946 /* no failfast so bail */ 24947 mutex_exit(SD_MUTEX(un)); 24948 return (0); 24949 } 24950 mutex_exit(SD_MUTEX(un)); 24951 } 24952 24953 /* 24954 * adjust minimum time interval to 1 second, 24955 * and convert from msecs to usecs 24956 */ 24957 if (interval > 0 && interval < 1000) { 24958 interval = 1000; 24959 } 24960 interval *= 1000; 24961 24962 /* 24963 * submit the request to the scsi_watch service 24964 */ 24965 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24966 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24967 if (token == NULL) { 24968 return (EAGAIN); 24969 } 24970 24971 /* 24972 * save token for termination later on 24973 */ 24974 mutex_enter(SD_MUTEX(un)); 24975 un->un_mhd_token = token; 24976 mutex_exit(SD_MUTEX(un)); 24977 return (0); 24978 } 24979 24980 24981 /* 24982 * Function: sd_mhd_watch_cb() 24983 * 24984 * Description: This function is the call back function used by the scsi watch 24985 * facility. The scsi watch facility sends the "Test Unit Ready" 24986 * and processes the status. If applicable (i.e. a "Unit Attention" 24987 * status and automatic "Request Sense" not used) the scsi watch 24988 * facility will send a "Request Sense" and retrieve the sense data 24989 * to be passed to this callback function. In either case the 24990 * automatic "Request Sense" or the facility submitting one, this 24991 * callback is passed the status and sense data. 24992 * 24993 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24994 * among multiple watches that share this callback function 24995 * resultp - scsi watch facility result packet containing scsi 24996 * packet, status byte and sense data 24997 * 24998 * Return Code: 0 - continue the watch task 24999 * non-zero - terminate the watch task 25000 */ 25001 25002 static int 25003 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25004 { 25005 struct sd_lun *un; 25006 struct scsi_status *statusp; 25007 struct scsi_extended_sense *sensep; 25008 struct scsi_pkt *pkt; 25009 uchar_t actual_sense_length; 25010 dev_t dev = (dev_t)arg; 25011 25012 ASSERT(resultp != NULL); 25013 statusp = resultp->statusp; 25014 sensep = resultp->sensep; 25015 pkt = resultp->pkt; 25016 actual_sense_length = resultp->actual_sense_length; 25017 25018 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25019 return (ENXIO); 25020 } 25021 25022 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25023 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25024 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25025 25026 /* Begin processing of the status and/or sense data */ 25027 if (pkt->pkt_reason != CMD_CMPLT) { 25028 /* Handle the incomplete packet */ 25029 sd_mhd_watch_incomplete(un, pkt); 25030 return (0); 25031 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25032 if (*((unsigned char *)statusp) 25033 == STATUS_RESERVATION_CONFLICT) { 25034 /* 25035 * Handle a reservation conflict by panicking if 25036 * configured for failfast or by logging the conflict 25037 * and updating the reservation status 25038 */ 25039 mutex_enter(SD_MUTEX(un)); 25040 if ((un->un_resvd_status & SD_FAILFAST) && 25041 (sd_failfast_enable)) { 25042 sd_panic_for_res_conflict(un); 25043 /*NOTREACHED*/ 25044 } 25045 SD_INFO(SD_LOG_IOCTL_MHD, un, 25046 "sd_mhd_watch_cb: Reservation Conflict\n"); 25047 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25048 mutex_exit(SD_MUTEX(un)); 25049 } 25050 } 25051 25052 if (sensep != NULL) { 25053 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25054 mutex_enter(SD_MUTEX(un)); 25055 if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) && 25056 (un->un_resvd_status & SD_RESERVE)) { 25057 /* 25058 * The additional sense code indicates a power 25059 * on or bus device reset has occurred; update 25060 * the reservation status. 25061 */ 25062 un->un_resvd_status |= 25063 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25064 SD_INFO(SD_LOG_IOCTL_MHD, un, 25065 "sd_mhd_watch_cb: Lost Reservation\n"); 25066 } 25067 } else { 25068 return (0); 25069 } 25070 } else { 25071 mutex_enter(SD_MUTEX(un)); 25072 } 25073 25074 if ((un->un_resvd_status & SD_RESERVE) && 25075 (un->un_resvd_status & SD_LOST_RESERVE)) { 25076 if (un->un_resvd_status & SD_WANT_RESERVE) { 25077 /* 25078 * A reset occurred in between the last probe and this 25079 * one so if a timeout is pending cancel it. 25080 */ 25081 if (un->un_resvd_timeid) { 25082 timeout_id_t temp_id = un->un_resvd_timeid; 25083 un->un_resvd_timeid = NULL; 25084 mutex_exit(SD_MUTEX(un)); 25085 (void) untimeout(temp_id); 25086 mutex_enter(SD_MUTEX(un)); 25087 } 25088 un->un_resvd_status &= ~SD_WANT_RESERVE; 25089 } 25090 if (un->un_resvd_timeid == 0) { 25091 /* Schedule a timeout to handle the lost reservation */ 25092 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25093 (void *)dev, 25094 drv_usectohz(sd_reinstate_resv_delay)); 25095 } 25096 } 25097 mutex_exit(SD_MUTEX(un)); 25098 return (0); 25099 } 25100 25101 25102 /* 25103 * Function: sd_mhd_watch_incomplete() 25104 * 25105 * Description: This function is used to find out why a scsi pkt sent by the 25106 * scsi watch facility was not completed. Under some scenarios this 25107 * routine will return. Otherwise it will send a bus reset to see 25108 * if the drive is still online. 25109 * 25110 * Arguments: un - driver soft state (unit) structure 25111 * pkt - incomplete scsi pkt 25112 */ 25113 25114 static void 25115 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25116 { 25117 int be_chatty; 25118 int perr; 25119 25120 ASSERT(pkt != NULL); 25121 ASSERT(un != NULL); 25122 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25123 perr = (pkt->pkt_statistics & STAT_PERR); 25124 25125 mutex_enter(SD_MUTEX(un)); 25126 if (un->un_state == SD_STATE_DUMPING) { 25127 mutex_exit(SD_MUTEX(un)); 25128 return; 25129 } 25130 25131 switch (pkt->pkt_reason) { 25132 case CMD_UNX_BUS_FREE: 25133 /* 25134 * If we had a parity error that caused the target to drop BSY*, 25135 * don't be chatty about it. 25136 */ 25137 if (perr && be_chatty) { 25138 be_chatty = 0; 25139 } 25140 break; 25141 case CMD_TAG_REJECT: 25142 /* 25143 * The SCSI-2 spec states that a tag reject will be sent by the 25144 * target if tagged queuing is not supported. A tag reject may 25145 * also be sent during certain initialization periods or to 25146 * control internal resources. For the latter case the target 25147 * may also return Queue Full. 25148 * 25149 * If this driver receives a tag reject from a target that is 25150 * going through an init period or controlling internal 25151 * resources tagged queuing will be disabled. This is a less 25152 * than optimal behavior but the driver is unable to determine 25153 * the target state and assumes tagged queueing is not supported 25154 */ 25155 pkt->pkt_flags = 0; 25156 un->un_tagflags = 0; 25157 25158 if (un->un_f_opt_queueing == TRUE) { 25159 un->un_throttle = min(un->un_throttle, 3); 25160 } else { 25161 un->un_throttle = 1; 25162 } 25163 mutex_exit(SD_MUTEX(un)); 25164 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25165 mutex_enter(SD_MUTEX(un)); 25166 break; 25167 case CMD_INCOMPLETE: 25168 /* 25169 * The transport stopped with an abnormal state, fallthrough and 25170 * reset the target and/or bus unless selection did not complete 25171 * (indicated by STATE_GOT_BUS) in which case we don't want to 25172 * go through a target/bus reset 25173 */ 25174 if (pkt->pkt_state == STATE_GOT_BUS) { 25175 break; 25176 } 25177 /*FALLTHROUGH*/ 25178 25179 case CMD_TIMEOUT: 25180 default: 25181 /* 25182 * The lun may still be running the command, so a lun reset 25183 * should be attempted. If the lun reset fails or cannot be 25184 * issued, than try a target reset. Lastly try a bus reset. 25185 */ 25186 if ((pkt->pkt_statistics & 25187 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25188 int reset_retval = 0; 25189 mutex_exit(SD_MUTEX(un)); 25190 if (un->un_f_allow_bus_device_reset == TRUE) { 25191 if (un->un_f_lun_reset_enabled == TRUE) { 25192 reset_retval = 25193 scsi_reset(SD_ADDRESS(un), 25194 RESET_LUN); 25195 } 25196 if (reset_retval == 0) { 25197 reset_retval = 25198 scsi_reset(SD_ADDRESS(un), 25199 RESET_TARGET); 25200 } 25201 } 25202 if (reset_retval == 0) { 25203 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25204 } 25205 mutex_enter(SD_MUTEX(un)); 25206 } 25207 break; 25208 } 25209 25210 /* A device/bus reset has occurred; update the reservation status. */ 25211 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25212 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25213 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25214 un->un_resvd_status |= 25215 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25216 SD_INFO(SD_LOG_IOCTL_MHD, un, 25217 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25218 } 25219 } 25220 25221 /* 25222 * The disk has been turned off; Update the device state. 25223 * 25224 * Note: Should we be offlining the disk here? 25225 */ 25226 if (pkt->pkt_state == STATE_GOT_BUS) { 25227 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25228 "Disk not responding to selection\n"); 25229 if (un->un_state != SD_STATE_OFFLINE) { 25230 New_state(un, SD_STATE_OFFLINE); 25231 } 25232 } else if (be_chatty) { 25233 /* 25234 * suppress messages if they are all the same pkt reason; 25235 * with TQ, many (up to 256) are returned with the same 25236 * pkt_reason 25237 */ 25238 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25239 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25240 "sd_mhd_watch_incomplete: " 25241 "SCSI transport failed: reason '%s'\n", 25242 scsi_rname(pkt->pkt_reason)); 25243 } 25244 } 25245 un->un_last_pkt_reason = pkt->pkt_reason; 25246 mutex_exit(SD_MUTEX(un)); 25247 } 25248 25249 25250 /* 25251 * Function: sd_sname() 25252 * 25253 * Description: This is a simple little routine to return a string containing 25254 * a printable description of command status byte for use in 25255 * logging. 25256 * 25257 * Arguments: status - pointer to a status byte 25258 * 25259 * Return Code: char * - string containing status description. 25260 */ 25261 25262 static char * 25263 sd_sname(uchar_t status) 25264 { 25265 switch (status & STATUS_MASK) { 25266 case STATUS_GOOD: 25267 return ("good status"); 25268 case STATUS_CHECK: 25269 return ("check condition"); 25270 case STATUS_MET: 25271 return ("condition met"); 25272 case STATUS_BUSY: 25273 return ("busy"); 25274 case STATUS_INTERMEDIATE: 25275 return ("intermediate"); 25276 case STATUS_INTERMEDIATE_MET: 25277 return ("intermediate - condition met"); 25278 case STATUS_RESERVATION_CONFLICT: 25279 return ("reservation_conflict"); 25280 case STATUS_TERMINATED: 25281 return ("command terminated"); 25282 case STATUS_QFULL: 25283 return ("queue full"); 25284 default: 25285 return ("<unknown status>"); 25286 } 25287 } 25288 25289 25290 /* 25291 * Function: sd_mhd_resvd_recover() 25292 * 25293 * Description: This function adds a reservation entry to the 25294 * sd_resv_reclaim_request list and signals the reservation 25295 * reclaim thread that there is work pending. If the reservation 25296 * reclaim thread has not been previously created this function 25297 * will kick it off. 25298 * 25299 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25300 * among multiple watches that share this callback function 25301 * 25302 * Context: This routine is called by timeout() and is run in interrupt 25303 * context. It must not sleep or call other functions which may 25304 * sleep. 25305 */ 25306 25307 static void 25308 sd_mhd_resvd_recover(void *arg) 25309 { 25310 dev_t dev = (dev_t)arg; 25311 struct sd_lun *un; 25312 struct sd_thr_request *sd_treq = NULL; 25313 struct sd_thr_request *sd_cur = NULL; 25314 struct sd_thr_request *sd_prev = NULL; 25315 int already_there = 0; 25316 25317 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25318 return; 25319 } 25320 25321 mutex_enter(SD_MUTEX(un)); 25322 un->un_resvd_timeid = NULL; 25323 if (un->un_resvd_status & SD_WANT_RESERVE) { 25324 /* 25325 * There was a reset so don't issue the reserve, allow the 25326 * sd_mhd_watch_cb callback function to notice this and 25327 * reschedule the timeout for reservation. 25328 */ 25329 mutex_exit(SD_MUTEX(un)); 25330 return; 25331 } 25332 mutex_exit(SD_MUTEX(un)); 25333 25334 /* 25335 * Add this device to the sd_resv_reclaim_request list and the 25336 * sd_resv_reclaim_thread should take care of the rest. 25337 * 25338 * Note: We can't sleep in this context so if the memory allocation 25339 * fails allow the sd_mhd_watch_cb callback function to notice this and 25340 * reschedule the timeout for reservation. (4378460) 25341 */ 25342 sd_treq = (struct sd_thr_request *) 25343 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25344 if (sd_treq == NULL) { 25345 return; 25346 } 25347 25348 sd_treq->sd_thr_req_next = NULL; 25349 sd_treq->dev = dev; 25350 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25351 if (sd_tr.srq_thr_req_head == NULL) { 25352 sd_tr.srq_thr_req_head = sd_treq; 25353 } else { 25354 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25355 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25356 if (sd_cur->dev == dev) { 25357 /* 25358 * already in Queue so don't log 25359 * another request for the device 25360 */ 25361 already_there = 1; 25362 break; 25363 } 25364 sd_prev = sd_cur; 25365 } 25366 if (!already_there) { 25367 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25368 "logging request for %lx\n", dev); 25369 sd_prev->sd_thr_req_next = sd_treq; 25370 } else { 25371 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25372 } 25373 } 25374 25375 /* 25376 * Create a kernel thread to do the reservation reclaim and free up this 25377 * thread. We cannot block this thread while we go away to do the 25378 * reservation reclaim 25379 */ 25380 if (sd_tr.srq_resv_reclaim_thread == NULL) 25381 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25382 sd_resv_reclaim_thread, NULL, 25383 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25384 25385 /* Tell the reservation reclaim thread that it has work to do */ 25386 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25387 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25388 } 25389 25390 /* 25391 * Function: sd_resv_reclaim_thread() 25392 * 25393 * Description: This function implements the reservation reclaim operations 25394 * 25395 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25396 * among multiple watches that share this callback function 25397 */ 25398 25399 static void 25400 sd_resv_reclaim_thread() 25401 { 25402 struct sd_lun *un; 25403 struct sd_thr_request *sd_mhreq; 25404 25405 /* Wait for work */ 25406 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25407 if (sd_tr.srq_thr_req_head == NULL) { 25408 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25409 &sd_tr.srq_resv_reclaim_mutex); 25410 } 25411 25412 /* Loop while we have work */ 25413 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25414 un = ddi_get_soft_state(sd_state, 25415 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25416 if (un == NULL) { 25417 /* 25418 * softstate structure is NULL so just 25419 * dequeue the request and continue 25420 */ 25421 sd_tr.srq_thr_req_head = 25422 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25423 kmem_free(sd_tr.srq_thr_cur_req, 25424 sizeof (struct sd_thr_request)); 25425 continue; 25426 } 25427 25428 /* dequeue the request */ 25429 sd_mhreq = sd_tr.srq_thr_cur_req; 25430 sd_tr.srq_thr_req_head = 25431 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25432 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25433 25434 /* 25435 * Reclaim reservation only if SD_RESERVE is still set. There 25436 * may have been a call to MHIOCRELEASE before we got here. 25437 */ 25438 mutex_enter(SD_MUTEX(un)); 25439 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25440 /* 25441 * Note: The SD_LOST_RESERVE flag is cleared before 25442 * reclaiming the reservation. If this is done after the 25443 * call to sd_reserve_release a reservation loss in the 25444 * window between pkt completion of reserve cmd and 25445 * mutex_enter below may not be recognized 25446 */ 25447 un->un_resvd_status &= ~SD_LOST_RESERVE; 25448 mutex_exit(SD_MUTEX(un)); 25449 25450 if (sd_reserve_release(sd_mhreq->dev, 25451 SD_RESERVE) == 0) { 25452 mutex_enter(SD_MUTEX(un)); 25453 un->un_resvd_status |= SD_RESERVE; 25454 mutex_exit(SD_MUTEX(un)); 25455 SD_INFO(SD_LOG_IOCTL_MHD, un, 25456 "sd_resv_reclaim_thread: " 25457 "Reservation Recovered\n"); 25458 } else { 25459 mutex_enter(SD_MUTEX(un)); 25460 un->un_resvd_status |= SD_LOST_RESERVE; 25461 mutex_exit(SD_MUTEX(un)); 25462 SD_INFO(SD_LOG_IOCTL_MHD, un, 25463 "sd_resv_reclaim_thread: Failed " 25464 "Reservation Recovery\n"); 25465 } 25466 } else { 25467 mutex_exit(SD_MUTEX(un)); 25468 } 25469 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25470 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25471 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25472 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25473 /* 25474 * wakeup the destroy thread if anyone is waiting on 25475 * us to complete. 25476 */ 25477 cv_signal(&sd_tr.srq_inprocess_cv); 25478 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25479 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25480 } 25481 25482 /* 25483 * cleanup the sd_tr structure now that this thread will not exist 25484 */ 25485 ASSERT(sd_tr.srq_thr_req_head == NULL); 25486 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25487 sd_tr.srq_resv_reclaim_thread = NULL; 25488 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25489 thread_exit(); 25490 } 25491 25492 25493 /* 25494 * Function: sd_rmv_resv_reclaim_req() 25495 * 25496 * Description: This function removes any pending reservation reclaim requests 25497 * for the specified device. 25498 * 25499 * Arguments: dev - the device 'dev_t' 25500 */ 25501 25502 static void 25503 sd_rmv_resv_reclaim_req(dev_t dev) 25504 { 25505 struct sd_thr_request *sd_mhreq; 25506 struct sd_thr_request *sd_prev; 25507 25508 /* Remove a reservation reclaim request from the list */ 25509 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25510 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25511 /* 25512 * We are attempting to reinstate reservation for 25513 * this device. We wait for sd_reserve_release() 25514 * to return before we return. 25515 */ 25516 cv_wait(&sd_tr.srq_inprocess_cv, 25517 &sd_tr.srq_resv_reclaim_mutex); 25518 } else { 25519 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25520 if (sd_mhreq && sd_mhreq->dev == dev) { 25521 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25522 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25523 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25524 return; 25525 } 25526 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25527 if (sd_mhreq && sd_mhreq->dev == dev) { 25528 break; 25529 } 25530 sd_prev = sd_mhreq; 25531 } 25532 if (sd_mhreq != NULL) { 25533 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25534 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25535 } 25536 } 25537 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25538 } 25539 25540 25541 /* 25542 * Function: sd_mhd_reset_notify_cb() 25543 * 25544 * Description: This is a call back function for scsi_reset_notify. This 25545 * function updates the softstate reserved status and logs the 25546 * reset. The driver scsi watch facility callback function 25547 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25548 * will reclaim the reservation. 25549 * 25550 * Arguments: arg - driver soft state (unit) structure 25551 */ 25552 25553 static void 25554 sd_mhd_reset_notify_cb(caddr_t arg) 25555 { 25556 struct sd_lun *un = (struct sd_lun *)arg; 25557 25558 mutex_enter(SD_MUTEX(un)); 25559 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25560 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25561 SD_INFO(SD_LOG_IOCTL_MHD, un, 25562 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25563 } 25564 mutex_exit(SD_MUTEX(un)); 25565 } 25566 25567 25568 /* 25569 * Function: sd_take_ownership() 25570 * 25571 * Description: This routine implements an algorithm to achieve a stable 25572 * reservation on disks which don't implement priority reserve, 25573 * and makes sure that other host lose re-reservation attempts. 25574 * This algorithm contains of a loop that keeps issuing the RESERVE 25575 * for some period of time (min_ownership_delay, default 6 seconds) 25576 * During that loop, it looks to see if there has been a bus device 25577 * reset or bus reset (both of which cause an existing reservation 25578 * to be lost). If the reservation is lost issue RESERVE until a 25579 * period of min_ownership_delay with no resets has gone by, or 25580 * until max_ownership_delay has expired. This loop ensures that 25581 * the host really did manage to reserve the device, in spite of 25582 * resets. The looping for min_ownership_delay (default six 25583 * seconds) is important to early generation clustering products, 25584 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25585 * MHIOCENFAILFAST periodic timer of two seconds. By having 25586 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25587 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25588 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25589 * have already noticed, via the MHIOCENFAILFAST polling, that it 25590 * no longer "owns" the disk and will have panicked itself. Thus, 25591 * the host issuing the MHIOCTKOWN is assured (with timing 25592 * dependencies) that by the time it actually starts to use the 25593 * disk for real work, the old owner is no longer accessing it. 25594 * 25595 * min_ownership_delay is the minimum amount of time for which the 25596 * disk must be reserved continuously devoid of resets before the 25597 * MHIOCTKOWN ioctl will return success. 25598 * 25599 * max_ownership_delay indicates the amount of time by which the 25600 * take ownership should succeed or timeout with an error. 25601 * 25602 * Arguments: dev - the device 'dev_t' 25603 * *p - struct containing timing info. 25604 * 25605 * Return Code: 0 for success or error code 25606 */ 25607 25608 static int 25609 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25610 { 25611 struct sd_lun *un; 25612 int rval; 25613 int err; 25614 int reservation_count = 0; 25615 int min_ownership_delay = 6000000; /* in usec */ 25616 int max_ownership_delay = 30000000; /* in usec */ 25617 clock_t start_time; /* starting time of this algorithm */ 25618 clock_t end_time; /* time limit for giving up */ 25619 clock_t ownership_time; /* time limit for stable ownership */ 25620 clock_t current_time; 25621 clock_t previous_current_time; 25622 25623 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25624 return (ENXIO); 25625 } 25626 25627 /* 25628 * Attempt a device reservation. A priority reservation is requested. 25629 */ 25630 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25631 != SD_SUCCESS) { 25632 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25633 "sd_take_ownership: return(1)=%d\n", rval); 25634 return (rval); 25635 } 25636 25637 /* Update the softstate reserved status to indicate the reservation */ 25638 mutex_enter(SD_MUTEX(un)); 25639 un->un_resvd_status |= SD_RESERVE; 25640 un->un_resvd_status &= 25641 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25642 mutex_exit(SD_MUTEX(un)); 25643 25644 if (p != NULL) { 25645 if (p->min_ownership_delay != 0) { 25646 min_ownership_delay = p->min_ownership_delay * 1000; 25647 } 25648 if (p->max_ownership_delay != 0) { 25649 max_ownership_delay = p->max_ownership_delay * 1000; 25650 } 25651 } 25652 SD_INFO(SD_LOG_IOCTL_MHD, un, 25653 "sd_take_ownership: min, max delays: %d, %d\n", 25654 min_ownership_delay, max_ownership_delay); 25655 25656 start_time = ddi_get_lbolt(); 25657 current_time = start_time; 25658 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25659 end_time = start_time + drv_usectohz(max_ownership_delay); 25660 25661 while (current_time - end_time < 0) { 25662 delay(drv_usectohz(500000)); 25663 25664 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25665 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25666 mutex_enter(SD_MUTEX(un)); 25667 rval = (un->un_resvd_status & 25668 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25669 mutex_exit(SD_MUTEX(un)); 25670 break; 25671 } 25672 } 25673 previous_current_time = current_time; 25674 current_time = ddi_get_lbolt(); 25675 mutex_enter(SD_MUTEX(un)); 25676 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25677 ownership_time = ddi_get_lbolt() + 25678 drv_usectohz(min_ownership_delay); 25679 reservation_count = 0; 25680 } else { 25681 reservation_count++; 25682 } 25683 un->un_resvd_status |= SD_RESERVE; 25684 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25685 mutex_exit(SD_MUTEX(un)); 25686 25687 SD_INFO(SD_LOG_IOCTL_MHD, un, 25688 "sd_take_ownership: ticks for loop iteration=%ld, " 25689 "reservation=%s\n", (current_time - previous_current_time), 25690 reservation_count ? "ok" : "reclaimed"); 25691 25692 if (current_time - ownership_time >= 0 && 25693 reservation_count >= 4) { 25694 rval = 0; /* Achieved a stable ownership */ 25695 break; 25696 } 25697 if (current_time - end_time >= 0) { 25698 rval = EACCES; /* No ownership in max possible time */ 25699 break; 25700 } 25701 } 25702 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25703 "sd_take_ownership: return(2)=%d\n", rval); 25704 return (rval); 25705 } 25706 25707 25708 /* 25709 * Function: sd_reserve_release() 25710 * 25711 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25712 * PRIORITY RESERVE commands based on a user specified command type 25713 * 25714 * Arguments: dev - the device 'dev_t' 25715 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25716 * SD_RESERVE, SD_RELEASE 25717 * 25718 * Return Code: 0 or Error Code 25719 */ 25720 25721 static int 25722 sd_reserve_release(dev_t dev, int cmd) 25723 { 25724 struct uscsi_cmd *com = NULL; 25725 struct sd_lun *un = NULL; 25726 char cdb[CDB_GROUP0]; 25727 int rval; 25728 25729 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25730 (cmd == SD_PRIORITY_RESERVE)); 25731 25732 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25733 return (ENXIO); 25734 } 25735 25736 /* instantiate and initialize the command and cdb */ 25737 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25738 bzero(cdb, CDB_GROUP0); 25739 com->uscsi_flags = USCSI_SILENT; 25740 com->uscsi_timeout = un->un_reserve_release_time; 25741 com->uscsi_cdblen = CDB_GROUP0; 25742 com->uscsi_cdb = cdb; 25743 if (cmd == SD_RELEASE) { 25744 cdb[0] = SCMD_RELEASE; 25745 } else { 25746 cdb[0] = SCMD_RESERVE; 25747 } 25748 25749 /* Send the command. */ 25750 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25751 UIO_SYSSPACE, SD_PATH_STANDARD); 25752 25753 /* 25754 * "break" a reservation that is held by another host, by issuing a 25755 * reset if priority reserve is desired, and we could not get the 25756 * device. 25757 */ 25758 if ((cmd == SD_PRIORITY_RESERVE) && 25759 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25760 /* 25761 * First try to reset the LUN. If we cannot, then try a target 25762 * reset, followed by a bus reset if the target reset fails. 25763 */ 25764 int reset_retval = 0; 25765 if (un->un_f_lun_reset_enabled == TRUE) { 25766 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25767 } 25768 if (reset_retval == 0) { 25769 /* The LUN reset either failed or was not issued */ 25770 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25771 } 25772 if ((reset_retval == 0) && 25773 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25774 rval = EIO; 25775 kmem_free(com, sizeof (*com)); 25776 return (rval); 25777 } 25778 25779 bzero(com, sizeof (struct uscsi_cmd)); 25780 com->uscsi_flags = USCSI_SILENT; 25781 com->uscsi_cdb = cdb; 25782 com->uscsi_cdblen = CDB_GROUP0; 25783 com->uscsi_timeout = 5; 25784 25785 /* 25786 * Reissue the last reserve command, this time without request 25787 * sense. Assume that it is just a regular reserve command. 25788 */ 25789 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 25790 UIO_SYSSPACE, SD_PATH_STANDARD); 25791 } 25792 25793 /* Return an error if still getting a reservation conflict. */ 25794 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25795 rval = EACCES; 25796 } 25797 25798 kmem_free(com, sizeof (*com)); 25799 return (rval); 25800 } 25801 25802 25803 #define SD_NDUMP_RETRIES 12 25804 /* 25805 * System Crash Dump routine 25806 */ 25807 25808 static int 25809 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25810 { 25811 int instance; 25812 int partition; 25813 int i; 25814 int err; 25815 struct sd_lun *un; 25816 struct dk_map *lp; 25817 struct scsi_pkt *wr_pktp; 25818 struct buf *wr_bp; 25819 struct buf wr_buf; 25820 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25821 daddr_t tgt_blkno; /* rmw - blkno for target */ 25822 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25823 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25824 size_t io_start_offset; 25825 int doing_rmw = FALSE; 25826 int rval; 25827 #if defined(__i386) || defined(__amd64) 25828 ssize_t dma_resid; 25829 daddr_t oblkno; 25830 #endif 25831 25832 instance = SDUNIT(dev); 25833 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25834 (!un->un_f_geometry_is_valid) || ISCD(un)) { 25835 return (ENXIO); 25836 } 25837 25838 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25839 25840 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25841 25842 partition = SDPART(dev); 25843 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25844 25845 /* Validate blocks to dump at against partition size. */ 25846 lp = &un->un_map[partition]; 25847 if ((blkno + nblk) > lp->dkl_nblk) { 25848 SD_TRACE(SD_LOG_DUMP, un, 25849 "sddump: dump range larger than partition: " 25850 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25851 blkno, nblk, lp->dkl_nblk); 25852 return (EINVAL); 25853 } 25854 25855 mutex_enter(&un->un_pm_mutex); 25856 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25857 struct scsi_pkt *start_pktp; 25858 25859 mutex_exit(&un->un_pm_mutex); 25860 25861 /* 25862 * use pm framework to power on HBA 1st 25863 */ 25864 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 25865 25866 /* 25867 * Dump no long uses sdpower to power on a device, it's 25868 * in-line here so it can be done in polled mode. 25869 */ 25870 25871 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25872 25873 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25874 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25875 25876 if (start_pktp == NULL) { 25877 /* We were not given a SCSI packet, fail. */ 25878 return (EIO); 25879 } 25880 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25881 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25882 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25883 start_pktp->pkt_flags = FLAG_NOINTR; 25884 25885 mutex_enter(SD_MUTEX(un)); 25886 SD_FILL_SCSI1_LUN(un, start_pktp); 25887 mutex_exit(SD_MUTEX(un)); 25888 /* 25889 * Scsi_poll returns 0 (success) if the command completes and 25890 * the status block is STATUS_GOOD. 25891 */ 25892 if (sd_scsi_poll(un, start_pktp) != 0) { 25893 scsi_destroy_pkt(start_pktp); 25894 return (EIO); 25895 } 25896 scsi_destroy_pkt(start_pktp); 25897 (void) sd_ddi_pm_resume(un); 25898 } else { 25899 mutex_exit(&un->un_pm_mutex); 25900 } 25901 25902 mutex_enter(SD_MUTEX(un)); 25903 un->un_throttle = 0; 25904 25905 /* 25906 * The first time through, reset the specific target device. 25907 * However, when cpr calls sddump we know that sd is in a 25908 * a good state so no bus reset is required. 25909 * Clear sense data via Request Sense cmd. 25910 * In sddump we don't care about allow_bus_device_reset anymore 25911 */ 25912 25913 if ((un->un_state != SD_STATE_SUSPENDED) && 25914 (un->un_state != SD_STATE_DUMPING)) { 25915 25916 New_state(un, SD_STATE_DUMPING); 25917 25918 if (un->un_f_is_fibre == FALSE) { 25919 mutex_exit(SD_MUTEX(un)); 25920 /* 25921 * Attempt a bus reset for parallel scsi. 25922 * 25923 * Note: A bus reset is required because on some host 25924 * systems (i.e. E420R) a bus device reset is 25925 * insufficient to reset the state of the target. 25926 * 25927 * Note: Don't issue the reset for fibre-channel, 25928 * because this tends to hang the bus (loop) for 25929 * too long while everyone is logging out and in 25930 * and the deadman timer for dumping will fire 25931 * before the dump is complete. 25932 */ 25933 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25934 mutex_enter(SD_MUTEX(un)); 25935 Restore_state(un); 25936 mutex_exit(SD_MUTEX(un)); 25937 return (EIO); 25938 } 25939 25940 /* Delay to give the device some recovery time. */ 25941 drv_usecwait(10000); 25942 25943 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25944 SD_INFO(SD_LOG_DUMP, un, 25945 "sddump: sd_send_polled_RQS failed\n"); 25946 } 25947 mutex_enter(SD_MUTEX(un)); 25948 } 25949 } 25950 25951 /* 25952 * Convert the partition-relative block number to a 25953 * disk physical block number. 25954 */ 25955 blkno += un->un_offset[partition]; 25956 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25957 25958 25959 /* 25960 * Check if the device has a non-512 block size. 25961 */ 25962 wr_bp = NULL; 25963 if (NOT_DEVBSIZE(un)) { 25964 tgt_byte_offset = blkno * un->un_sys_blocksize; 25965 tgt_byte_count = nblk * un->un_sys_blocksize; 25966 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25967 (tgt_byte_count % un->un_tgt_blocksize)) { 25968 doing_rmw = TRUE; 25969 /* 25970 * Calculate the block number and number of block 25971 * in terms of the media block size. 25972 */ 25973 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25974 tgt_nblk = 25975 ((tgt_byte_offset + tgt_byte_count + 25976 (un->un_tgt_blocksize - 1)) / 25977 un->un_tgt_blocksize) - tgt_blkno; 25978 25979 /* 25980 * Invoke the routine which is going to do read part 25981 * of read-modify-write. 25982 * Note that this routine returns a pointer to 25983 * a valid bp in wr_bp. 25984 */ 25985 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25986 &wr_bp); 25987 if (err) { 25988 mutex_exit(SD_MUTEX(un)); 25989 return (err); 25990 } 25991 /* 25992 * Offset is being calculated as - 25993 * (original block # * system block size) - 25994 * (new block # * target block size) 25995 */ 25996 io_start_offset = 25997 ((uint64_t)(blkno * un->un_sys_blocksize)) - 25998 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 25999 26000 ASSERT((io_start_offset >= 0) && 26001 (io_start_offset < un->un_tgt_blocksize)); 26002 /* 26003 * Do the modify portion of read modify write. 26004 */ 26005 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26006 (size_t)nblk * un->un_sys_blocksize); 26007 } else { 26008 doing_rmw = FALSE; 26009 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26010 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26011 } 26012 26013 /* Convert blkno and nblk to target blocks */ 26014 blkno = tgt_blkno; 26015 nblk = tgt_nblk; 26016 } else { 26017 wr_bp = &wr_buf; 26018 bzero(wr_bp, sizeof (struct buf)); 26019 wr_bp->b_flags = B_BUSY; 26020 wr_bp->b_un.b_addr = addr; 26021 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26022 wr_bp->b_resid = 0; 26023 } 26024 26025 mutex_exit(SD_MUTEX(un)); 26026 26027 /* 26028 * Obtain a SCSI packet for the write command. 26029 * It should be safe to call the allocator here without 26030 * worrying about being locked for DVMA mapping because 26031 * the address we're passed is already a DVMA mapping 26032 * 26033 * We are also not going to worry about semaphore ownership 26034 * in the dump buffer. Dumping is single threaded at present. 26035 */ 26036 26037 wr_pktp = NULL; 26038 26039 #if defined(__i386) || defined(__amd64) 26040 dma_resid = wr_bp->b_bcount; 26041 oblkno = blkno; 26042 while (dma_resid != 0) { 26043 #endif 26044 26045 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26046 wr_bp->b_flags &= ~B_ERROR; 26047 26048 #if defined(__i386) || defined(__amd64) 26049 blkno = oblkno + 26050 ((wr_bp->b_bcount - dma_resid) / 26051 un->un_tgt_blocksize); 26052 nblk = dma_resid / un->un_tgt_blocksize; 26053 26054 if (wr_pktp) { 26055 /* Partial DMA transfers after initial transfer */ 26056 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26057 blkno, nblk); 26058 } else { 26059 /* Initial transfer */ 26060 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26061 un->un_pkt_flags, NULL_FUNC, NULL, 26062 blkno, nblk); 26063 } 26064 #else 26065 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26066 0, NULL_FUNC, NULL, blkno, nblk); 26067 #endif 26068 26069 if (rval == 0) { 26070 /* We were given a SCSI packet, continue. */ 26071 break; 26072 } 26073 26074 if (i == 0) { 26075 if (wr_bp->b_flags & B_ERROR) { 26076 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26077 "no resources for dumping; " 26078 "error code: 0x%x, retrying", 26079 geterror(wr_bp)); 26080 } else { 26081 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26082 "no resources for dumping; retrying"); 26083 } 26084 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26085 if (wr_bp->b_flags & B_ERROR) { 26086 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26087 "no resources for dumping; error code: " 26088 "0x%x, retrying\n", geterror(wr_bp)); 26089 } 26090 } else { 26091 if (wr_bp->b_flags & B_ERROR) { 26092 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26093 "no resources for dumping; " 26094 "error code: 0x%x, retries failed, " 26095 "giving up.\n", geterror(wr_bp)); 26096 } else { 26097 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26098 "no resources for dumping; " 26099 "retries failed, giving up.\n"); 26100 } 26101 mutex_enter(SD_MUTEX(un)); 26102 Restore_state(un); 26103 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26104 mutex_exit(SD_MUTEX(un)); 26105 scsi_free_consistent_buf(wr_bp); 26106 } else { 26107 mutex_exit(SD_MUTEX(un)); 26108 } 26109 return (EIO); 26110 } 26111 drv_usecwait(10000); 26112 } 26113 26114 #if defined(__i386) || defined(__amd64) 26115 /* 26116 * save the resid from PARTIAL_DMA 26117 */ 26118 dma_resid = wr_pktp->pkt_resid; 26119 if (dma_resid != 0) 26120 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26121 wr_pktp->pkt_resid = 0; 26122 #endif 26123 26124 /* SunBug 1222170 */ 26125 wr_pktp->pkt_flags = FLAG_NOINTR; 26126 26127 err = EIO; 26128 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26129 26130 /* 26131 * Scsi_poll returns 0 (success) if the command completes and 26132 * the status block is STATUS_GOOD. We should only check 26133 * errors if this condition is not true. Even then we should 26134 * send our own request sense packet only if we have a check 26135 * condition and auto request sense has not been performed by 26136 * the hba. 26137 */ 26138 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26139 26140 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26141 (wr_pktp->pkt_resid == 0)) { 26142 err = SD_SUCCESS; 26143 break; 26144 } 26145 26146 /* 26147 * Check CMD_DEV_GONE 1st, give up if device is gone. 26148 */ 26149 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26150 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26151 "Device is gone\n"); 26152 break; 26153 } 26154 26155 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26156 SD_INFO(SD_LOG_DUMP, un, 26157 "sddump: write failed with CHECK, try # %d\n", i); 26158 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26159 (void) sd_send_polled_RQS(un); 26160 } 26161 26162 continue; 26163 } 26164 26165 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26166 int reset_retval = 0; 26167 26168 SD_INFO(SD_LOG_DUMP, un, 26169 "sddump: write failed with BUSY, try # %d\n", i); 26170 26171 if (un->un_f_lun_reset_enabled == TRUE) { 26172 reset_retval = scsi_reset(SD_ADDRESS(un), 26173 RESET_LUN); 26174 } 26175 if (reset_retval == 0) { 26176 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26177 } 26178 (void) sd_send_polled_RQS(un); 26179 26180 } else { 26181 SD_INFO(SD_LOG_DUMP, un, 26182 "sddump: write failed with 0x%x, try # %d\n", 26183 SD_GET_PKT_STATUS(wr_pktp), i); 26184 mutex_enter(SD_MUTEX(un)); 26185 sd_reset_target(un, wr_pktp); 26186 mutex_exit(SD_MUTEX(un)); 26187 } 26188 26189 /* 26190 * If we are not getting anywhere with lun/target resets, 26191 * let's reset the bus. 26192 */ 26193 if (i == SD_NDUMP_RETRIES/2) { 26194 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26195 (void) sd_send_polled_RQS(un); 26196 } 26197 26198 } 26199 #if defined(__i386) || defined(__amd64) 26200 } /* dma_resid */ 26201 #endif 26202 26203 scsi_destroy_pkt(wr_pktp); 26204 mutex_enter(SD_MUTEX(un)); 26205 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26206 mutex_exit(SD_MUTEX(un)); 26207 scsi_free_consistent_buf(wr_bp); 26208 } else { 26209 mutex_exit(SD_MUTEX(un)); 26210 } 26211 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26212 return (err); 26213 } 26214 26215 /* 26216 * Function: sd_scsi_poll() 26217 * 26218 * Description: This is a wrapper for the scsi_poll call. 26219 * 26220 * Arguments: sd_lun - The unit structure 26221 * scsi_pkt - The scsi packet being sent to the device. 26222 * 26223 * Return Code: 0 - Command completed successfully with good status 26224 * -1 - Command failed. This could indicate a check condition 26225 * or other status value requiring recovery action. 26226 * 26227 */ 26228 26229 static int 26230 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26231 { 26232 int status; 26233 26234 ASSERT(un != NULL); 26235 ASSERT(!mutex_owned(SD_MUTEX(un))); 26236 ASSERT(pktp != NULL); 26237 26238 status = SD_SUCCESS; 26239 26240 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26241 pktp->pkt_flags |= un->un_tagflags; 26242 pktp->pkt_flags &= ~FLAG_NODISCON; 26243 } 26244 26245 status = sd_ddi_scsi_poll(pktp); 26246 /* 26247 * Scsi_poll returns 0 (success) if the command completes and the 26248 * status block is STATUS_GOOD. We should only check errors if this 26249 * condition is not true. Even then we should send our own request 26250 * sense packet only if we have a check condition and auto 26251 * request sense has not been performed by the hba. 26252 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26253 */ 26254 if ((status != SD_SUCCESS) && 26255 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26256 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26257 (pktp->pkt_reason != CMD_DEV_GONE)) 26258 (void) sd_send_polled_RQS(un); 26259 26260 return (status); 26261 } 26262 26263 /* 26264 * Function: sd_send_polled_RQS() 26265 * 26266 * Description: This sends the request sense command to a device. 26267 * 26268 * Arguments: sd_lun - The unit structure 26269 * 26270 * Return Code: 0 - Command completed successfully with good status 26271 * -1 - Command failed. 26272 * 26273 */ 26274 26275 static int 26276 sd_send_polled_RQS(struct sd_lun *un) 26277 { 26278 int ret_val; 26279 struct scsi_pkt *rqs_pktp; 26280 struct buf *rqs_bp; 26281 26282 ASSERT(un != NULL); 26283 ASSERT(!mutex_owned(SD_MUTEX(un))); 26284 26285 ret_val = SD_SUCCESS; 26286 26287 rqs_pktp = un->un_rqs_pktp; 26288 rqs_bp = un->un_rqs_bp; 26289 26290 mutex_enter(SD_MUTEX(un)); 26291 26292 if (un->un_sense_isbusy) { 26293 ret_val = SD_FAILURE; 26294 mutex_exit(SD_MUTEX(un)); 26295 return (ret_val); 26296 } 26297 26298 /* 26299 * If the request sense buffer (and packet) is not in use, 26300 * let's set the un_sense_isbusy and send our packet 26301 */ 26302 un->un_sense_isbusy = 1; 26303 rqs_pktp->pkt_resid = 0; 26304 rqs_pktp->pkt_reason = 0; 26305 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26306 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26307 26308 mutex_exit(SD_MUTEX(un)); 26309 26310 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26311 " 0x%p\n", rqs_bp->b_un.b_addr); 26312 26313 /* 26314 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26315 * axle - it has a call into us! 26316 */ 26317 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26318 SD_INFO(SD_LOG_COMMON, un, 26319 "sd_send_polled_RQS: RQS failed\n"); 26320 } 26321 26322 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26323 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26324 26325 mutex_enter(SD_MUTEX(un)); 26326 un->un_sense_isbusy = 0; 26327 mutex_exit(SD_MUTEX(un)); 26328 26329 return (ret_val); 26330 } 26331 26332 /* 26333 * Defines needed for localized version of the scsi_poll routine. 26334 */ 26335 #define SD_CSEC 10000 /* usecs */ 26336 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 26337 26338 26339 /* 26340 * Function: sd_ddi_scsi_poll() 26341 * 26342 * Description: Localized version of the scsi_poll routine. The purpose is to 26343 * send a scsi_pkt to a device as a polled command. This version 26344 * is to ensure more robust handling of transport errors. 26345 * Specifically this routine cures not ready, coming ready 26346 * transition for power up and reset of sonoma's. This can take 26347 * up to 45 seconds for power-on and 20 seconds for reset of a 26348 * sonoma lun. 26349 * 26350 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26351 * 26352 * Return Code: 0 - Command completed successfully with good status 26353 * -1 - Command failed. 26354 * 26355 */ 26356 26357 static int 26358 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26359 { 26360 int busy_count; 26361 int timeout; 26362 int rval = SD_FAILURE; 26363 int savef; 26364 struct scsi_extended_sense *sensep; 26365 long savet; 26366 void (*savec)(); 26367 /* 26368 * The following is defined in machdep.c and is used in determining if 26369 * the scsi transport system will do polled I/O instead of interrupt 26370 * I/O when called from xx_dump(). 26371 */ 26372 extern int do_polled_io; 26373 26374 /* 26375 * save old flags in pkt, to restore at end 26376 */ 26377 savef = pkt->pkt_flags; 26378 savec = pkt->pkt_comp; 26379 savet = pkt->pkt_time; 26380 26381 pkt->pkt_flags |= FLAG_NOINTR; 26382 26383 /* 26384 * XXX there is nothing in the SCSA spec that states that we should not 26385 * do a callback for polled cmds; however, removing this will break sd 26386 * and probably other target drivers 26387 */ 26388 pkt->pkt_comp = NULL; 26389 26390 /* 26391 * we don't like a polled command without timeout. 26392 * 60 seconds seems long enough. 26393 */ 26394 if (pkt->pkt_time == 0) { 26395 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26396 } 26397 26398 /* 26399 * Send polled cmd. 26400 * 26401 * We do some error recovery for various errors. Tran_busy, 26402 * queue full, and non-dispatched commands are retried every 10 msec. 26403 * as they are typically transient failures. Busy status and Not 26404 * Ready are retried every second as this status takes a while to 26405 * change. Unit attention is retried for pkt_time (60) times 26406 * with no delay. 26407 */ 26408 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 26409 26410 for (busy_count = 0; busy_count < timeout; busy_count++) { 26411 int rc; 26412 int poll_delay; 26413 26414 /* 26415 * Initialize pkt status variables. 26416 */ 26417 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26418 26419 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26420 if (rc != TRAN_BUSY) { 26421 /* Transport failed - give up. */ 26422 break; 26423 } else { 26424 /* Transport busy - try again. */ 26425 poll_delay = 1 * SD_CSEC; /* 10 msec */ 26426 } 26427 } else { 26428 /* 26429 * Transport accepted - check pkt status. 26430 */ 26431 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26432 if (pkt->pkt_reason == CMD_CMPLT && 26433 rc == STATUS_CHECK && 26434 pkt->pkt_state & STATE_ARQ_DONE) { 26435 struct scsi_arq_status *arqstat = 26436 (struct scsi_arq_status *)(pkt->pkt_scbp); 26437 26438 sensep = &arqstat->sts_sensedata; 26439 } else { 26440 sensep = NULL; 26441 } 26442 26443 if ((pkt->pkt_reason == CMD_CMPLT) && 26444 (rc == STATUS_GOOD)) { 26445 /* No error - we're done */ 26446 rval = SD_SUCCESS; 26447 break; 26448 26449 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26450 /* Lost connection - give up */ 26451 break; 26452 26453 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26454 (pkt->pkt_state == 0)) { 26455 /* Pkt not dispatched - try again. */ 26456 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26457 26458 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26459 (rc == STATUS_QFULL)) { 26460 /* Queue full - try again. */ 26461 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 26462 26463 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26464 (rc == STATUS_BUSY)) { 26465 /* Busy - try again. */ 26466 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26467 busy_count += (SD_SEC_TO_CSEC - 1); 26468 26469 } else if ((sensep != NULL) && 26470 (sensep->es_key == KEY_UNIT_ATTENTION)) { 26471 /* Unit Attention - try again */ 26472 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 26473 continue; 26474 26475 } else if ((sensep != NULL) && 26476 (sensep->es_key == KEY_NOT_READY) && 26477 (sensep->es_add_code == 0x04) && 26478 (sensep->es_qual_code == 0x01)) { 26479 /* Not ready -> ready - try again. */ 26480 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 26481 busy_count += (SD_SEC_TO_CSEC - 1); 26482 26483 } else { 26484 /* BAD status - give up. */ 26485 break; 26486 } 26487 } 26488 26489 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 26490 !do_polled_io) { 26491 delay(drv_usectohz(poll_delay)); 26492 } else { 26493 /* we busy wait during cpr_dump or interrupt threads */ 26494 drv_usecwait(poll_delay); 26495 } 26496 } 26497 26498 pkt->pkt_flags = savef; 26499 pkt->pkt_comp = savec; 26500 pkt->pkt_time = savet; 26501 return (rval); 26502 } 26503 26504 26505 /* 26506 * Function: sd_persistent_reservation_in_read_keys 26507 * 26508 * Description: This routine is the driver entry point for handling CD-ROM 26509 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26510 * by sending the SCSI-3 PRIN commands to the device. 26511 * Processes the read keys command response by copying the 26512 * reservation key information into the user provided buffer. 26513 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26514 * 26515 * Arguments: un - Pointer to soft state struct for the target. 26516 * usrp - user provided pointer to multihost Persistent In Read 26517 * Keys structure (mhioc_inkeys_t) 26518 * flag - this argument is a pass through to ddi_copyxxx() 26519 * directly from the mode argument of ioctl(). 26520 * 26521 * Return Code: 0 - Success 26522 * EACCES 26523 * ENOTSUP 26524 * errno return code from sd_send_scsi_cmd() 26525 * 26526 * Context: Can sleep. Does not return until command is completed. 26527 */ 26528 26529 static int 26530 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26531 mhioc_inkeys_t *usrp, int flag) 26532 { 26533 #ifdef _MULTI_DATAMODEL 26534 struct mhioc_key_list32 li32; 26535 #endif 26536 sd_prin_readkeys_t *in; 26537 mhioc_inkeys_t *ptr; 26538 mhioc_key_list_t li; 26539 uchar_t *data_bufp; 26540 int data_len; 26541 int rval; 26542 size_t copysz; 26543 26544 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26545 return (EINVAL); 26546 } 26547 bzero(&li, sizeof (mhioc_key_list_t)); 26548 26549 /* 26550 * Get the listsize from user 26551 */ 26552 #ifdef _MULTI_DATAMODEL 26553 26554 switch (ddi_model_convert_from(flag & FMODELS)) { 26555 case DDI_MODEL_ILP32: 26556 copysz = sizeof (struct mhioc_key_list32); 26557 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26558 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26559 "sd_persistent_reservation_in_read_keys: " 26560 "failed ddi_copyin: mhioc_key_list32_t\n"); 26561 rval = EFAULT; 26562 goto done; 26563 } 26564 li.listsize = li32.listsize; 26565 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26566 break; 26567 26568 case DDI_MODEL_NONE: 26569 copysz = sizeof (mhioc_key_list_t); 26570 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26571 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26572 "sd_persistent_reservation_in_read_keys: " 26573 "failed ddi_copyin: mhioc_key_list_t\n"); 26574 rval = EFAULT; 26575 goto done; 26576 } 26577 break; 26578 } 26579 26580 #else /* ! _MULTI_DATAMODEL */ 26581 copysz = sizeof (mhioc_key_list_t); 26582 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26583 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26584 "sd_persistent_reservation_in_read_keys: " 26585 "failed ddi_copyin: mhioc_key_list_t\n"); 26586 rval = EFAULT; 26587 goto done; 26588 } 26589 #endif 26590 26591 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26592 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26593 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26594 26595 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 26596 data_len, data_bufp)) != 0) { 26597 goto done; 26598 } 26599 in = (sd_prin_readkeys_t *)data_bufp; 26600 ptr->generation = BE_32(in->generation); 26601 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26602 26603 /* 26604 * Return the min(listsize, listlen) keys 26605 */ 26606 #ifdef _MULTI_DATAMODEL 26607 26608 switch (ddi_model_convert_from(flag & FMODELS)) { 26609 case DDI_MODEL_ILP32: 26610 li32.listlen = li.listlen; 26611 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26612 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26613 "sd_persistent_reservation_in_read_keys: " 26614 "failed ddi_copyout: mhioc_key_list32_t\n"); 26615 rval = EFAULT; 26616 goto done; 26617 } 26618 break; 26619 26620 case DDI_MODEL_NONE: 26621 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26622 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26623 "sd_persistent_reservation_in_read_keys: " 26624 "failed ddi_copyout: mhioc_key_list_t\n"); 26625 rval = EFAULT; 26626 goto done; 26627 } 26628 break; 26629 } 26630 26631 #else /* ! _MULTI_DATAMODEL */ 26632 26633 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26634 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26635 "sd_persistent_reservation_in_read_keys: " 26636 "failed ddi_copyout: mhioc_key_list_t\n"); 26637 rval = EFAULT; 26638 goto done; 26639 } 26640 26641 #endif /* _MULTI_DATAMODEL */ 26642 26643 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26644 li.listsize * MHIOC_RESV_KEY_SIZE); 26645 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26646 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26647 "sd_persistent_reservation_in_read_keys: " 26648 "failed ddi_copyout: keylist\n"); 26649 rval = EFAULT; 26650 } 26651 done: 26652 kmem_free(data_bufp, data_len); 26653 return (rval); 26654 } 26655 26656 26657 /* 26658 * Function: sd_persistent_reservation_in_read_resv 26659 * 26660 * Description: This routine is the driver entry point for handling CD-ROM 26661 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26662 * by sending the SCSI-3 PRIN commands to the device. 26663 * Process the read persistent reservations command response by 26664 * copying the reservation information into the user provided 26665 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26666 * 26667 * Arguments: un - Pointer to soft state struct for the target. 26668 * usrp - user provided pointer to multihost Persistent In Read 26669 * Keys structure (mhioc_inkeys_t) 26670 * flag - this argument is a pass through to ddi_copyxxx() 26671 * directly from the mode argument of ioctl(). 26672 * 26673 * Return Code: 0 - Success 26674 * EACCES 26675 * ENOTSUP 26676 * errno return code from sd_send_scsi_cmd() 26677 * 26678 * Context: Can sleep. Does not return until command is completed. 26679 */ 26680 26681 static int 26682 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26683 mhioc_inresvs_t *usrp, int flag) 26684 { 26685 #ifdef _MULTI_DATAMODEL 26686 struct mhioc_resv_desc_list32 resvlist32; 26687 #endif 26688 sd_prin_readresv_t *in; 26689 mhioc_inresvs_t *ptr; 26690 sd_readresv_desc_t *readresv_ptr; 26691 mhioc_resv_desc_list_t resvlist; 26692 mhioc_resv_desc_t resvdesc; 26693 uchar_t *data_bufp; 26694 int data_len; 26695 int rval; 26696 int i; 26697 size_t copysz; 26698 mhioc_resv_desc_t *bufp; 26699 26700 if ((ptr = usrp) == NULL) { 26701 return (EINVAL); 26702 } 26703 26704 /* 26705 * Get the listsize from user 26706 */ 26707 #ifdef _MULTI_DATAMODEL 26708 switch (ddi_model_convert_from(flag & FMODELS)) { 26709 case DDI_MODEL_ILP32: 26710 copysz = sizeof (struct mhioc_resv_desc_list32); 26711 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26712 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26713 "sd_persistent_reservation_in_read_resv: " 26714 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26715 rval = EFAULT; 26716 goto done; 26717 } 26718 resvlist.listsize = resvlist32.listsize; 26719 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26720 break; 26721 26722 case DDI_MODEL_NONE: 26723 copysz = sizeof (mhioc_resv_desc_list_t); 26724 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26725 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26726 "sd_persistent_reservation_in_read_resv: " 26727 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26728 rval = EFAULT; 26729 goto done; 26730 } 26731 break; 26732 } 26733 #else /* ! _MULTI_DATAMODEL */ 26734 copysz = sizeof (mhioc_resv_desc_list_t); 26735 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26736 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26737 "sd_persistent_reservation_in_read_resv: " 26738 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26739 rval = EFAULT; 26740 goto done; 26741 } 26742 #endif /* ! _MULTI_DATAMODEL */ 26743 26744 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26745 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26746 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26747 26748 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 26749 data_len, data_bufp)) != 0) { 26750 goto done; 26751 } 26752 in = (sd_prin_readresv_t *)data_bufp; 26753 ptr->generation = BE_32(in->generation); 26754 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26755 26756 /* 26757 * Return the min(listsize, listlen( keys 26758 */ 26759 #ifdef _MULTI_DATAMODEL 26760 26761 switch (ddi_model_convert_from(flag & FMODELS)) { 26762 case DDI_MODEL_ILP32: 26763 resvlist32.listlen = resvlist.listlen; 26764 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26765 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26766 "sd_persistent_reservation_in_read_resv: " 26767 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26768 rval = EFAULT; 26769 goto done; 26770 } 26771 break; 26772 26773 case DDI_MODEL_NONE: 26774 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26775 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26776 "sd_persistent_reservation_in_read_resv: " 26777 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26778 rval = EFAULT; 26779 goto done; 26780 } 26781 break; 26782 } 26783 26784 #else /* ! _MULTI_DATAMODEL */ 26785 26786 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26787 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26788 "sd_persistent_reservation_in_read_resv: " 26789 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26790 rval = EFAULT; 26791 goto done; 26792 } 26793 26794 #endif /* ! _MULTI_DATAMODEL */ 26795 26796 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26797 bufp = resvlist.list; 26798 copysz = sizeof (mhioc_resv_desc_t); 26799 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26800 i++, readresv_ptr++, bufp++) { 26801 26802 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26803 MHIOC_RESV_KEY_SIZE); 26804 resvdesc.type = readresv_ptr->type; 26805 resvdesc.scope = readresv_ptr->scope; 26806 resvdesc.scope_specific_addr = 26807 BE_32(readresv_ptr->scope_specific_addr); 26808 26809 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26810 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26811 "sd_persistent_reservation_in_read_resv: " 26812 "failed ddi_copyout: resvlist\n"); 26813 rval = EFAULT; 26814 goto done; 26815 } 26816 } 26817 done: 26818 kmem_free(data_bufp, data_len); 26819 return (rval); 26820 } 26821 26822 26823 /* 26824 * Function: sr_change_blkmode() 26825 * 26826 * Description: This routine is the driver entry point for handling CD-ROM 26827 * block mode ioctl requests. Support for returning and changing 26828 * the current block size in use by the device is implemented. The 26829 * LBA size is changed via a MODE SELECT Block Descriptor. 26830 * 26831 * This routine issues a mode sense with an allocation length of 26832 * 12 bytes for the mode page header and a single block descriptor. 26833 * 26834 * Arguments: dev - the device 'dev_t' 26835 * cmd - the request type; one of CDROMGBLKMODE (get) or 26836 * CDROMSBLKMODE (set) 26837 * data - current block size or requested block size 26838 * flag - this argument is a pass through to ddi_copyxxx() directly 26839 * from the mode argument of ioctl(). 26840 * 26841 * Return Code: the code returned by sd_send_scsi_cmd() 26842 * EINVAL if invalid arguments are provided 26843 * EFAULT if ddi_copyxxx() fails 26844 * ENXIO if fail ddi_get_soft_state 26845 * EIO if invalid mode sense block descriptor length 26846 * 26847 */ 26848 26849 static int 26850 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26851 { 26852 struct sd_lun *un = NULL; 26853 struct mode_header *sense_mhp, *select_mhp; 26854 struct block_descriptor *sense_desc, *select_desc; 26855 int current_bsize; 26856 int rval = EINVAL; 26857 uchar_t *sense = NULL; 26858 uchar_t *select = NULL; 26859 26860 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26861 26862 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26863 return (ENXIO); 26864 } 26865 26866 /* 26867 * The block length is changed via the Mode Select block descriptor, the 26868 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26869 * required as part of this routine. Therefore the mode sense allocation 26870 * length is specified to be the length of a mode page header and a 26871 * block descriptor. 26872 */ 26873 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26874 26875 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 26876 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 26877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26878 "sr_change_blkmode: Mode Sense Failed\n"); 26879 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26880 return (rval); 26881 } 26882 26883 /* Check the block descriptor len to handle only 1 block descriptor */ 26884 sense_mhp = (struct mode_header *)sense; 26885 if ((sense_mhp->bdesc_length == 0) || 26886 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26887 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26888 "sr_change_blkmode: Mode Sense returned invalid block" 26889 " descriptor length\n"); 26890 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26891 return (EIO); 26892 } 26893 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26894 current_bsize = ((sense_desc->blksize_hi << 16) | 26895 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26896 26897 /* Process command */ 26898 switch (cmd) { 26899 case CDROMGBLKMODE: 26900 /* Return the block size obtained during the mode sense */ 26901 if (ddi_copyout(¤t_bsize, (void *)data, 26902 sizeof (int), flag) != 0) 26903 rval = EFAULT; 26904 break; 26905 case CDROMSBLKMODE: 26906 /* Validate the requested block size */ 26907 switch (data) { 26908 case CDROM_BLK_512: 26909 case CDROM_BLK_1024: 26910 case CDROM_BLK_2048: 26911 case CDROM_BLK_2056: 26912 case CDROM_BLK_2336: 26913 case CDROM_BLK_2340: 26914 case CDROM_BLK_2352: 26915 case CDROM_BLK_2368: 26916 case CDROM_BLK_2448: 26917 case CDROM_BLK_2646: 26918 case CDROM_BLK_2647: 26919 break; 26920 default: 26921 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26922 "sr_change_blkmode: " 26923 "Block Size '%ld' Not Supported\n", data); 26924 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26925 return (EINVAL); 26926 } 26927 26928 /* 26929 * The current block size matches the requested block size so 26930 * there is no need to send the mode select to change the size 26931 */ 26932 if (current_bsize == data) { 26933 break; 26934 } 26935 26936 /* Build the select data for the requested block size */ 26937 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26938 select_mhp = (struct mode_header *)select; 26939 select_desc = 26940 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26941 /* 26942 * The LBA size is changed via the block descriptor, so the 26943 * descriptor is built according to the user data 26944 */ 26945 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26946 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26947 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26948 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26949 26950 /* Send the mode select for the requested block size */ 26951 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26952 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26953 SD_PATH_STANDARD)) != 0) { 26954 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26955 "sr_change_blkmode: Mode Select Failed\n"); 26956 /* 26957 * The mode select failed for the requested block size, 26958 * so reset the data for the original block size and 26959 * send it to the target. The error is indicated by the 26960 * return value for the failed mode select. 26961 */ 26962 select_desc->blksize_hi = sense_desc->blksize_hi; 26963 select_desc->blksize_mid = sense_desc->blksize_mid; 26964 select_desc->blksize_lo = sense_desc->blksize_lo; 26965 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 26966 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26967 SD_PATH_STANDARD); 26968 } else { 26969 ASSERT(!mutex_owned(SD_MUTEX(un))); 26970 mutex_enter(SD_MUTEX(un)); 26971 sd_update_block_info(un, (uint32_t)data, 0); 26972 26973 mutex_exit(SD_MUTEX(un)); 26974 } 26975 break; 26976 default: 26977 /* should not reach here, but check anyway */ 26978 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26979 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 26980 rval = EINVAL; 26981 break; 26982 } 26983 26984 if (select) { 26985 kmem_free(select, BUFLEN_CHG_BLK_MODE); 26986 } 26987 if (sense) { 26988 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26989 } 26990 return (rval); 26991 } 26992 26993 26994 /* 26995 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 26996 * implement driver support for getting and setting the CD speed. The command 26997 * set used will be based on the device type. If the device has not been 26998 * identified as MMC the Toshiba vendor specific mode page will be used. If 26999 * the device is MMC but does not support the Real Time Streaming feature 27000 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27001 * be used to read the speed. 27002 */ 27003 27004 /* 27005 * Function: sr_change_speed() 27006 * 27007 * Description: This routine is the driver entry point for handling CD-ROM 27008 * drive speed ioctl requests for devices supporting the Toshiba 27009 * vendor specific drive speed mode page. Support for returning 27010 * and changing the current drive speed in use by the device is 27011 * implemented. 27012 * 27013 * Arguments: dev - the device 'dev_t' 27014 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27015 * CDROMSDRVSPEED (set) 27016 * data - current drive speed or requested drive speed 27017 * flag - this argument is a pass through to ddi_copyxxx() directly 27018 * from the mode argument of ioctl(). 27019 * 27020 * Return Code: the code returned by sd_send_scsi_cmd() 27021 * EINVAL if invalid arguments are provided 27022 * EFAULT if ddi_copyxxx() fails 27023 * ENXIO if fail ddi_get_soft_state 27024 * EIO if invalid mode sense block descriptor length 27025 */ 27026 27027 static int 27028 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27029 { 27030 struct sd_lun *un = NULL; 27031 struct mode_header *sense_mhp, *select_mhp; 27032 struct mode_speed *sense_page, *select_page; 27033 int current_speed; 27034 int rval = EINVAL; 27035 int bd_len; 27036 uchar_t *sense = NULL; 27037 uchar_t *select = NULL; 27038 27039 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27040 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27041 return (ENXIO); 27042 } 27043 27044 /* 27045 * Note: The drive speed is being modified here according to a Toshiba 27046 * vendor specific mode page (0x31). 27047 */ 27048 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27049 27050 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27051 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27052 SD_PATH_STANDARD)) != 0) { 27053 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27054 "sr_change_speed: Mode Sense Failed\n"); 27055 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27056 return (rval); 27057 } 27058 sense_mhp = (struct mode_header *)sense; 27059 27060 /* Check the block descriptor len to handle only 1 block descriptor */ 27061 bd_len = sense_mhp->bdesc_length; 27062 if (bd_len > MODE_BLK_DESC_LENGTH) { 27063 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27064 "sr_change_speed: Mode Sense returned invalid block " 27065 "descriptor length\n"); 27066 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27067 return (EIO); 27068 } 27069 27070 sense_page = (struct mode_speed *) 27071 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27072 current_speed = sense_page->speed; 27073 27074 /* Process command */ 27075 switch (cmd) { 27076 case CDROMGDRVSPEED: 27077 /* Return the drive speed obtained during the mode sense */ 27078 if (current_speed == 0x2) { 27079 current_speed = CDROM_TWELVE_SPEED; 27080 } 27081 if (ddi_copyout(¤t_speed, (void *)data, 27082 sizeof (int), flag) != 0) { 27083 rval = EFAULT; 27084 } 27085 break; 27086 case CDROMSDRVSPEED: 27087 /* Validate the requested drive speed */ 27088 switch ((uchar_t)data) { 27089 case CDROM_TWELVE_SPEED: 27090 data = 0x2; 27091 /*FALLTHROUGH*/ 27092 case CDROM_NORMAL_SPEED: 27093 case CDROM_DOUBLE_SPEED: 27094 case CDROM_QUAD_SPEED: 27095 case CDROM_MAXIMUM_SPEED: 27096 break; 27097 default: 27098 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27099 "sr_change_speed: " 27100 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27101 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27102 return (EINVAL); 27103 } 27104 27105 /* 27106 * The current drive speed matches the requested drive speed so 27107 * there is no need to send the mode select to change the speed 27108 */ 27109 if (current_speed == data) { 27110 break; 27111 } 27112 27113 /* Build the select data for the requested drive speed */ 27114 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27115 select_mhp = (struct mode_header *)select; 27116 select_mhp->bdesc_length = 0; 27117 select_page = 27118 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27119 select_page = 27120 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27121 select_page->mode_page.code = CDROM_MODE_SPEED; 27122 select_page->mode_page.length = 2; 27123 select_page->speed = (uchar_t)data; 27124 27125 /* Send the mode select for the requested block size */ 27126 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27127 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27128 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27129 /* 27130 * The mode select failed for the requested drive speed, 27131 * so reset the data for the original drive speed and 27132 * send it to the target. The error is indicated by the 27133 * return value for the failed mode select. 27134 */ 27135 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27136 "sr_drive_speed: Mode Select Failed\n"); 27137 select_page->speed = sense_page->speed; 27138 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27139 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27140 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27141 } 27142 break; 27143 default: 27144 /* should not reach here, but check anyway */ 27145 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27146 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27147 rval = EINVAL; 27148 break; 27149 } 27150 27151 if (select) { 27152 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27153 } 27154 if (sense) { 27155 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27156 } 27157 27158 return (rval); 27159 } 27160 27161 27162 /* 27163 * Function: sr_atapi_change_speed() 27164 * 27165 * Description: This routine is the driver entry point for handling CD-ROM 27166 * drive speed ioctl requests for MMC devices that do not support 27167 * the Real Time Streaming feature (0x107). 27168 * 27169 * Note: This routine will use the SET SPEED command which may not 27170 * be supported by all devices. 27171 * 27172 * Arguments: dev- the device 'dev_t' 27173 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27174 * CDROMSDRVSPEED (set) 27175 * data- current drive speed or requested drive speed 27176 * flag- this argument is a pass through to ddi_copyxxx() directly 27177 * from the mode argument of ioctl(). 27178 * 27179 * Return Code: the code returned by sd_send_scsi_cmd() 27180 * EINVAL if invalid arguments are provided 27181 * EFAULT if ddi_copyxxx() fails 27182 * ENXIO if fail ddi_get_soft_state 27183 * EIO if invalid mode sense block descriptor length 27184 */ 27185 27186 static int 27187 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27188 { 27189 struct sd_lun *un; 27190 struct uscsi_cmd *com = NULL; 27191 struct mode_header_grp2 *sense_mhp; 27192 uchar_t *sense_page; 27193 uchar_t *sense = NULL; 27194 char cdb[CDB_GROUP5]; 27195 int bd_len; 27196 int current_speed = 0; 27197 int max_speed = 0; 27198 int rval; 27199 27200 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27201 27202 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27203 return (ENXIO); 27204 } 27205 27206 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27207 27208 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27209 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27210 SD_PATH_STANDARD)) != 0) { 27211 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27212 "sr_atapi_change_speed: Mode Sense Failed\n"); 27213 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27214 return (rval); 27215 } 27216 27217 /* Check the block descriptor len to handle only 1 block descriptor */ 27218 sense_mhp = (struct mode_header_grp2 *)sense; 27219 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27220 if (bd_len > MODE_BLK_DESC_LENGTH) { 27221 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27222 "sr_atapi_change_speed: Mode Sense returned invalid " 27223 "block descriptor length\n"); 27224 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27225 return (EIO); 27226 } 27227 27228 /* Calculate the current and maximum drive speeds */ 27229 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27230 current_speed = (sense_page[14] << 8) | sense_page[15]; 27231 max_speed = (sense_page[8] << 8) | sense_page[9]; 27232 27233 /* Process the command */ 27234 switch (cmd) { 27235 case CDROMGDRVSPEED: 27236 current_speed /= SD_SPEED_1X; 27237 if (ddi_copyout(¤t_speed, (void *)data, 27238 sizeof (int), flag) != 0) 27239 rval = EFAULT; 27240 break; 27241 case CDROMSDRVSPEED: 27242 /* Convert the speed code to KB/sec */ 27243 switch ((uchar_t)data) { 27244 case CDROM_NORMAL_SPEED: 27245 current_speed = SD_SPEED_1X; 27246 break; 27247 case CDROM_DOUBLE_SPEED: 27248 current_speed = 2 * SD_SPEED_1X; 27249 break; 27250 case CDROM_QUAD_SPEED: 27251 current_speed = 4 * SD_SPEED_1X; 27252 break; 27253 case CDROM_TWELVE_SPEED: 27254 current_speed = 12 * SD_SPEED_1X; 27255 break; 27256 case CDROM_MAXIMUM_SPEED: 27257 current_speed = 0xffff; 27258 break; 27259 default: 27260 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27261 "sr_atapi_change_speed: invalid drive speed %d\n", 27262 (uchar_t)data); 27263 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27264 return (EINVAL); 27265 } 27266 27267 /* Check the request against the drive's max speed. */ 27268 if (current_speed != 0xffff) { 27269 if (current_speed > max_speed) { 27270 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27271 return (EINVAL); 27272 } 27273 } 27274 27275 /* 27276 * Build and send the SET SPEED command 27277 * 27278 * Note: The SET SPEED (0xBB) command used in this routine is 27279 * obsolete per the SCSI MMC spec but still supported in the 27280 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27281 * therefore the command is still implemented in this routine. 27282 */ 27283 bzero(cdb, sizeof (cdb)); 27284 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27285 cdb[2] = (uchar_t)(current_speed >> 8); 27286 cdb[3] = (uchar_t)current_speed; 27287 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27288 com->uscsi_cdb = (caddr_t)cdb; 27289 com->uscsi_cdblen = CDB_GROUP5; 27290 com->uscsi_bufaddr = NULL; 27291 com->uscsi_buflen = 0; 27292 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27293 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 27294 UIO_SYSSPACE, SD_PATH_STANDARD); 27295 break; 27296 default: 27297 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27298 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27299 rval = EINVAL; 27300 } 27301 27302 if (sense) { 27303 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27304 } 27305 if (com) { 27306 kmem_free(com, sizeof (*com)); 27307 } 27308 return (rval); 27309 } 27310 27311 27312 /* 27313 * Function: sr_pause_resume() 27314 * 27315 * Description: This routine is the driver entry point for handling CD-ROM 27316 * pause/resume ioctl requests. This only affects the audio play 27317 * operation. 27318 * 27319 * Arguments: dev - the device 'dev_t' 27320 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27321 * for setting the resume bit of the cdb. 27322 * 27323 * Return Code: the code returned by sd_send_scsi_cmd() 27324 * EINVAL if invalid mode specified 27325 * 27326 */ 27327 27328 static int 27329 sr_pause_resume(dev_t dev, int cmd) 27330 { 27331 struct sd_lun *un; 27332 struct uscsi_cmd *com; 27333 char cdb[CDB_GROUP1]; 27334 int rval; 27335 27336 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27337 return (ENXIO); 27338 } 27339 27340 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27341 bzero(cdb, CDB_GROUP1); 27342 cdb[0] = SCMD_PAUSE_RESUME; 27343 switch (cmd) { 27344 case CDROMRESUME: 27345 cdb[8] = 1; 27346 break; 27347 case CDROMPAUSE: 27348 cdb[8] = 0; 27349 break; 27350 default: 27351 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27352 " Command '%x' Not Supported\n", cmd); 27353 rval = EINVAL; 27354 goto done; 27355 } 27356 27357 com->uscsi_cdb = cdb; 27358 com->uscsi_cdblen = CDB_GROUP1; 27359 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27360 27361 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27362 UIO_SYSSPACE, SD_PATH_STANDARD); 27363 27364 done: 27365 kmem_free(com, sizeof (*com)); 27366 return (rval); 27367 } 27368 27369 27370 /* 27371 * Function: sr_play_msf() 27372 * 27373 * Description: This routine is the driver entry point for handling CD-ROM 27374 * ioctl requests to output the audio signals at the specified 27375 * starting address and continue the audio play until the specified 27376 * ending address (CDROMPLAYMSF) The address is in Minute Second 27377 * Frame (MSF) format. 27378 * 27379 * Arguments: dev - the device 'dev_t' 27380 * data - pointer to user provided audio msf structure, 27381 * specifying start/end addresses. 27382 * flag - this argument is a pass through to ddi_copyxxx() 27383 * directly from the mode argument of ioctl(). 27384 * 27385 * Return Code: the code returned by sd_send_scsi_cmd() 27386 * EFAULT if ddi_copyxxx() fails 27387 * ENXIO if fail ddi_get_soft_state 27388 * EINVAL if data pointer is NULL 27389 */ 27390 27391 static int 27392 sr_play_msf(dev_t dev, caddr_t data, int flag) 27393 { 27394 struct sd_lun *un; 27395 struct uscsi_cmd *com; 27396 struct cdrom_msf msf_struct; 27397 struct cdrom_msf *msf = &msf_struct; 27398 char cdb[CDB_GROUP1]; 27399 int rval; 27400 27401 if (data == NULL) { 27402 return (EINVAL); 27403 } 27404 27405 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27406 return (ENXIO); 27407 } 27408 27409 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27410 return (EFAULT); 27411 } 27412 27413 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27414 bzero(cdb, CDB_GROUP1); 27415 cdb[0] = SCMD_PLAYAUDIO_MSF; 27416 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27417 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27418 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27419 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27420 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27421 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27422 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27423 } else { 27424 cdb[3] = msf->cdmsf_min0; 27425 cdb[4] = msf->cdmsf_sec0; 27426 cdb[5] = msf->cdmsf_frame0; 27427 cdb[6] = msf->cdmsf_min1; 27428 cdb[7] = msf->cdmsf_sec1; 27429 cdb[8] = msf->cdmsf_frame1; 27430 } 27431 com->uscsi_cdb = cdb; 27432 com->uscsi_cdblen = CDB_GROUP1; 27433 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27434 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27435 UIO_SYSSPACE, SD_PATH_STANDARD); 27436 kmem_free(com, sizeof (*com)); 27437 return (rval); 27438 } 27439 27440 27441 /* 27442 * Function: sr_play_trkind() 27443 * 27444 * Description: This routine is the driver entry point for handling CD-ROM 27445 * ioctl requests to output the audio signals at the specified 27446 * starting address and continue the audio play until the specified 27447 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27448 * format. 27449 * 27450 * Arguments: dev - the device 'dev_t' 27451 * data - pointer to user provided audio track/index structure, 27452 * specifying start/end addresses. 27453 * flag - this argument is a pass through to ddi_copyxxx() 27454 * directly from the mode argument of ioctl(). 27455 * 27456 * Return Code: the code returned by sd_send_scsi_cmd() 27457 * EFAULT if ddi_copyxxx() fails 27458 * ENXIO if fail ddi_get_soft_state 27459 * EINVAL if data pointer is NULL 27460 */ 27461 27462 static int 27463 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27464 { 27465 struct cdrom_ti ti_struct; 27466 struct cdrom_ti *ti = &ti_struct; 27467 struct uscsi_cmd *com = NULL; 27468 char cdb[CDB_GROUP1]; 27469 int rval; 27470 27471 if (data == NULL) { 27472 return (EINVAL); 27473 } 27474 27475 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27476 return (EFAULT); 27477 } 27478 27479 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27480 bzero(cdb, CDB_GROUP1); 27481 cdb[0] = SCMD_PLAYAUDIO_TI; 27482 cdb[4] = ti->cdti_trk0; 27483 cdb[5] = ti->cdti_ind0; 27484 cdb[7] = ti->cdti_trk1; 27485 cdb[8] = ti->cdti_ind1; 27486 com->uscsi_cdb = cdb; 27487 com->uscsi_cdblen = CDB_GROUP1; 27488 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27489 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27490 UIO_SYSSPACE, SD_PATH_STANDARD); 27491 kmem_free(com, sizeof (*com)); 27492 return (rval); 27493 } 27494 27495 27496 /* 27497 * Function: sr_read_all_subcodes() 27498 * 27499 * Description: This routine is the driver entry point for handling CD-ROM 27500 * ioctl requests to return raw subcode data while the target is 27501 * playing audio (CDROMSUBCODE). 27502 * 27503 * Arguments: dev - the device 'dev_t' 27504 * data - pointer to user provided cdrom subcode structure, 27505 * specifying the transfer length and address. 27506 * flag - this argument is a pass through to ddi_copyxxx() 27507 * directly from the mode argument of ioctl(). 27508 * 27509 * Return Code: the code returned by sd_send_scsi_cmd() 27510 * EFAULT if ddi_copyxxx() fails 27511 * ENXIO if fail ddi_get_soft_state 27512 * EINVAL if data pointer is NULL 27513 */ 27514 27515 static int 27516 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27517 { 27518 struct sd_lun *un = NULL; 27519 struct uscsi_cmd *com = NULL; 27520 struct cdrom_subcode *subcode = NULL; 27521 int rval; 27522 size_t buflen; 27523 char cdb[CDB_GROUP5]; 27524 27525 #ifdef _MULTI_DATAMODEL 27526 /* To support ILP32 applications in an LP64 world */ 27527 struct cdrom_subcode32 cdrom_subcode32; 27528 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27529 #endif 27530 if (data == NULL) { 27531 return (EINVAL); 27532 } 27533 27534 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27535 return (ENXIO); 27536 } 27537 27538 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27539 27540 #ifdef _MULTI_DATAMODEL 27541 switch (ddi_model_convert_from(flag & FMODELS)) { 27542 case DDI_MODEL_ILP32: 27543 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27544 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27545 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27546 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27547 return (EFAULT); 27548 } 27549 /* Convert the ILP32 uscsi data from the application to LP64 */ 27550 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27551 break; 27552 case DDI_MODEL_NONE: 27553 if (ddi_copyin(data, subcode, 27554 sizeof (struct cdrom_subcode), flag)) { 27555 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27556 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27557 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27558 return (EFAULT); 27559 } 27560 break; 27561 } 27562 #else /* ! _MULTI_DATAMODEL */ 27563 if (ddi_copyin(data, subcode, 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 #endif /* _MULTI_DATAMODEL */ 27570 27571 /* 27572 * Since MMC-2 expects max 3 bytes for length, check if the 27573 * length input is greater than 3 bytes 27574 */ 27575 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27576 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27577 "sr_read_all_subcodes: " 27578 "cdrom transfer length too large: %d (limit %d)\n", 27579 subcode->cdsc_length, 0xFFFFFF); 27580 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27581 return (EINVAL); 27582 } 27583 27584 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27585 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27586 bzero(cdb, CDB_GROUP5); 27587 27588 if (un->un_f_mmc_cap == TRUE) { 27589 cdb[0] = (char)SCMD_READ_CD; 27590 cdb[2] = (char)0xff; 27591 cdb[3] = (char)0xff; 27592 cdb[4] = (char)0xff; 27593 cdb[5] = (char)0xff; 27594 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27595 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27596 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27597 cdb[10] = 1; 27598 } else { 27599 /* 27600 * Note: A vendor specific command (0xDF) is being used her to 27601 * request a read of all subcodes. 27602 */ 27603 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27604 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27605 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27606 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27607 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27608 } 27609 com->uscsi_cdb = cdb; 27610 com->uscsi_cdblen = CDB_GROUP5; 27611 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27612 com->uscsi_buflen = buflen; 27613 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27614 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 27615 UIO_SYSSPACE, SD_PATH_STANDARD); 27616 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27617 kmem_free(com, sizeof (*com)); 27618 return (rval); 27619 } 27620 27621 27622 /* 27623 * Function: sr_read_subchannel() 27624 * 27625 * Description: This routine is the driver entry point for handling CD-ROM 27626 * ioctl requests to return the Q sub-channel data of the CD 27627 * current position block. (CDROMSUBCHNL) The data includes the 27628 * track number, index number, absolute CD-ROM address (LBA or MSF 27629 * format per the user) , track relative CD-ROM address (LBA or MSF 27630 * format per the user), control data and audio status. 27631 * 27632 * Arguments: dev - the device 'dev_t' 27633 * data - pointer to user provided cdrom sub-channel structure 27634 * flag - this argument is a pass through to ddi_copyxxx() 27635 * directly from the mode argument of ioctl(). 27636 * 27637 * Return Code: the code returned by sd_send_scsi_cmd() 27638 * EFAULT if ddi_copyxxx() fails 27639 * ENXIO if fail ddi_get_soft_state 27640 * EINVAL if data pointer is NULL 27641 */ 27642 27643 static int 27644 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27645 { 27646 struct sd_lun *un; 27647 struct uscsi_cmd *com; 27648 struct cdrom_subchnl subchanel; 27649 struct cdrom_subchnl *subchnl = &subchanel; 27650 char cdb[CDB_GROUP1]; 27651 caddr_t buffer; 27652 int rval; 27653 27654 if (data == NULL) { 27655 return (EINVAL); 27656 } 27657 27658 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27659 (un->un_state == SD_STATE_OFFLINE)) { 27660 return (ENXIO); 27661 } 27662 27663 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27664 return (EFAULT); 27665 } 27666 27667 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27668 bzero(cdb, CDB_GROUP1); 27669 cdb[0] = SCMD_READ_SUBCHANNEL; 27670 /* Set the MSF bit based on the user requested address format */ 27671 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27672 /* 27673 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27674 * returned 27675 */ 27676 cdb[2] = 0x40; 27677 /* 27678 * Set byte 3 to specify the return data format. A value of 0x01 27679 * indicates that the CD-ROM current position should be returned. 27680 */ 27681 cdb[3] = 0x01; 27682 cdb[8] = 0x10; 27683 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27684 com->uscsi_cdb = cdb; 27685 com->uscsi_cdblen = CDB_GROUP1; 27686 com->uscsi_bufaddr = buffer; 27687 com->uscsi_buflen = 16; 27688 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27689 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27690 UIO_SYSSPACE, SD_PATH_STANDARD); 27691 if (rval != 0) { 27692 kmem_free(buffer, 16); 27693 kmem_free(com, sizeof (*com)); 27694 return (rval); 27695 } 27696 27697 /* Process the returned Q sub-channel data */ 27698 subchnl->cdsc_audiostatus = buffer[1]; 27699 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27700 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27701 subchnl->cdsc_trk = buffer[6]; 27702 subchnl->cdsc_ind = buffer[7]; 27703 if (subchnl->cdsc_format & CDROM_LBA) { 27704 subchnl->cdsc_absaddr.lba = 27705 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27706 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27707 subchnl->cdsc_reladdr.lba = 27708 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27709 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27710 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27711 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27712 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27713 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27714 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27715 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27716 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27717 } else { 27718 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27719 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27720 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27721 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27722 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27723 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27724 } 27725 kmem_free(buffer, 16); 27726 kmem_free(com, sizeof (*com)); 27727 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27728 != 0) { 27729 return (EFAULT); 27730 } 27731 return (rval); 27732 } 27733 27734 27735 /* 27736 * Function: sr_read_tocentry() 27737 * 27738 * Description: This routine is the driver entry point for handling CD-ROM 27739 * ioctl requests to read from the Table of Contents (TOC) 27740 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27741 * fields, the starting address (LBA or MSF format per the user) 27742 * and the data mode if the user specified track is a data track. 27743 * 27744 * Note: The READ HEADER (0x44) command used in this routine is 27745 * obsolete per the SCSI MMC spec but still supported in the 27746 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27747 * therefore the command is still implemented in this routine. 27748 * 27749 * Arguments: dev - the device 'dev_t' 27750 * data - pointer to user provided toc entry structure, 27751 * specifying the track # and the address format 27752 * (LBA or MSF). 27753 * flag - this argument is a pass through to ddi_copyxxx() 27754 * directly from the mode argument of ioctl(). 27755 * 27756 * Return Code: the code returned by sd_send_scsi_cmd() 27757 * EFAULT if ddi_copyxxx() fails 27758 * ENXIO if fail ddi_get_soft_state 27759 * EINVAL if data pointer is NULL 27760 */ 27761 27762 static int 27763 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27764 { 27765 struct sd_lun *un = NULL; 27766 struct uscsi_cmd *com; 27767 struct cdrom_tocentry toc_entry; 27768 struct cdrom_tocentry *entry = &toc_entry; 27769 caddr_t buffer; 27770 int rval; 27771 char cdb[CDB_GROUP1]; 27772 27773 if (data == NULL) { 27774 return (EINVAL); 27775 } 27776 27777 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27778 (un->un_state == SD_STATE_OFFLINE)) { 27779 return (ENXIO); 27780 } 27781 27782 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27783 return (EFAULT); 27784 } 27785 27786 /* Validate the requested track and address format */ 27787 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27788 return (EINVAL); 27789 } 27790 27791 if (entry->cdte_track == 0) { 27792 return (EINVAL); 27793 } 27794 27795 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27796 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27797 bzero(cdb, CDB_GROUP1); 27798 27799 cdb[0] = SCMD_READ_TOC; 27800 /* Set the MSF bit based on the user requested address format */ 27801 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27802 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27803 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27804 } else { 27805 cdb[6] = entry->cdte_track; 27806 } 27807 27808 /* 27809 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27810 * (4 byte TOC response header + 8 byte track descriptor) 27811 */ 27812 cdb[8] = 12; 27813 com->uscsi_cdb = cdb; 27814 com->uscsi_cdblen = CDB_GROUP1; 27815 com->uscsi_bufaddr = buffer; 27816 com->uscsi_buflen = 0x0C; 27817 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27818 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27819 UIO_SYSSPACE, SD_PATH_STANDARD); 27820 if (rval != 0) { 27821 kmem_free(buffer, 12); 27822 kmem_free(com, sizeof (*com)); 27823 return (rval); 27824 } 27825 27826 /* Process the toc entry */ 27827 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27828 entry->cdte_ctrl = (buffer[5] & 0x0F); 27829 if (entry->cdte_format & CDROM_LBA) { 27830 entry->cdte_addr.lba = 27831 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27832 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27833 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27834 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27835 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27836 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27837 /* 27838 * Send a READ TOC command using the LBA address format to get 27839 * the LBA for the track requested so it can be used in the 27840 * READ HEADER request 27841 * 27842 * Note: The MSF bit of the READ HEADER command specifies the 27843 * output format. The block address specified in that command 27844 * must be in LBA format. 27845 */ 27846 cdb[1] = 0; 27847 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27848 UIO_SYSSPACE, SD_PATH_STANDARD); 27849 if (rval != 0) { 27850 kmem_free(buffer, 12); 27851 kmem_free(com, sizeof (*com)); 27852 return (rval); 27853 } 27854 } else { 27855 entry->cdte_addr.msf.minute = buffer[9]; 27856 entry->cdte_addr.msf.second = buffer[10]; 27857 entry->cdte_addr.msf.frame = buffer[11]; 27858 /* 27859 * Send a READ TOC command using the LBA address format to get 27860 * the LBA for the track requested so it can be used in the 27861 * READ HEADER request 27862 * 27863 * Note: The MSF bit of the READ HEADER command specifies the 27864 * output format. The block address specified in that command 27865 * must be in LBA format. 27866 */ 27867 cdb[1] = 0; 27868 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27869 UIO_SYSSPACE, SD_PATH_STANDARD); 27870 if (rval != 0) { 27871 kmem_free(buffer, 12); 27872 kmem_free(com, sizeof (*com)); 27873 return (rval); 27874 } 27875 } 27876 27877 /* 27878 * Build and send the READ HEADER command to determine the data mode of 27879 * the user specified track. 27880 */ 27881 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27882 (entry->cdte_track != CDROM_LEADOUT)) { 27883 bzero(cdb, CDB_GROUP1); 27884 cdb[0] = SCMD_READ_HEADER; 27885 cdb[2] = buffer[8]; 27886 cdb[3] = buffer[9]; 27887 cdb[4] = buffer[10]; 27888 cdb[5] = buffer[11]; 27889 cdb[8] = 0x08; 27890 com->uscsi_buflen = 0x08; 27891 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27892 UIO_SYSSPACE, SD_PATH_STANDARD); 27893 if (rval == 0) { 27894 entry->cdte_datamode = buffer[0]; 27895 } else { 27896 /* 27897 * READ HEADER command failed, since this is 27898 * obsoleted in one spec, its better to return 27899 * -1 for an invlid track so that we can still 27900 * recieve the rest of the TOC data. 27901 */ 27902 entry->cdte_datamode = (uchar_t)-1; 27903 } 27904 } else { 27905 entry->cdte_datamode = (uchar_t)-1; 27906 } 27907 27908 kmem_free(buffer, 12); 27909 kmem_free(com, sizeof (*com)); 27910 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27911 return (EFAULT); 27912 27913 return (rval); 27914 } 27915 27916 27917 /* 27918 * Function: sr_read_tochdr() 27919 * 27920 * Description: This routine is the driver entry point for handling CD-ROM 27921 * ioctl requests to read the Table of Contents (TOC) header 27922 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27923 * and ending track numbers 27924 * 27925 * Arguments: dev - the device 'dev_t' 27926 * data - pointer to user provided toc header structure, 27927 * specifying the starting and ending track numbers. 27928 * flag - this argument is a pass through to ddi_copyxxx() 27929 * directly from the mode argument of ioctl(). 27930 * 27931 * Return Code: the code returned by sd_send_scsi_cmd() 27932 * EFAULT if ddi_copyxxx() fails 27933 * ENXIO if fail ddi_get_soft_state 27934 * EINVAL if data pointer is NULL 27935 */ 27936 27937 static int 27938 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27939 { 27940 struct sd_lun *un; 27941 struct uscsi_cmd *com; 27942 struct cdrom_tochdr toc_header; 27943 struct cdrom_tochdr *hdr = &toc_header; 27944 char cdb[CDB_GROUP1]; 27945 int rval; 27946 caddr_t buffer; 27947 27948 if (data == NULL) { 27949 return (EINVAL); 27950 } 27951 27952 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27953 (un->un_state == SD_STATE_OFFLINE)) { 27954 return (ENXIO); 27955 } 27956 27957 buffer = kmem_zalloc(4, KM_SLEEP); 27958 bzero(cdb, CDB_GROUP1); 27959 cdb[0] = SCMD_READ_TOC; 27960 /* 27961 * Specifying a track number of 0x00 in the READ TOC command indicates 27962 * that the TOC header should be returned 27963 */ 27964 cdb[6] = 0x00; 27965 /* 27966 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 27967 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 27968 */ 27969 cdb[8] = 0x04; 27970 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27971 com->uscsi_cdb = cdb; 27972 com->uscsi_cdblen = CDB_GROUP1; 27973 com->uscsi_bufaddr = buffer; 27974 com->uscsi_buflen = 0x04; 27975 com->uscsi_timeout = 300; 27976 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27977 27978 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 27979 UIO_SYSSPACE, SD_PATH_STANDARD); 27980 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27981 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 27982 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 27983 } else { 27984 hdr->cdth_trk0 = buffer[2]; 27985 hdr->cdth_trk1 = buffer[3]; 27986 } 27987 kmem_free(buffer, 4); 27988 kmem_free(com, sizeof (*com)); 27989 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 27990 return (EFAULT); 27991 } 27992 return (rval); 27993 } 27994 27995 27996 /* 27997 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 27998 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 27999 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28000 * digital audio and extended architecture digital audio. These modes are 28001 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28002 * MMC specs. 28003 * 28004 * In addition to support for the various data formats these routines also 28005 * include support for devices that implement only the direct access READ 28006 * commands (0x08, 0x28), devices that implement the READ_CD commands 28007 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28008 * READ CDXA commands (0xD8, 0xDB) 28009 */ 28010 28011 /* 28012 * Function: sr_read_mode1() 28013 * 28014 * Description: This routine is the driver entry point for handling CD-ROM 28015 * ioctl read mode1 requests (CDROMREADMODE1). 28016 * 28017 * Arguments: dev - the device 'dev_t' 28018 * data - pointer to user provided cd read structure specifying 28019 * the lba buffer address and length. 28020 * flag - this argument is a pass through to ddi_copyxxx() 28021 * directly from the mode argument of ioctl(). 28022 * 28023 * Return Code: the code returned by sd_send_scsi_cmd() 28024 * EFAULT if ddi_copyxxx() fails 28025 * ENXIO if fail ddi_get_soft_state 28026 * EINVAL if data pointer is NULL 28027 */ 28028 28029 static int 28030 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28031 { 28032 struct sd_lun *un; 28033 struct cdrom_read mode1_struct; 28034 struct cdrom_read *mode1 = &mode1_struct; 28035 int rval; 28036 #ifdef _MULTI_DATAMODEL 28037 /* To support ILP32 applications in an LP64 world */ 28038 struct cdrom_read32 cdrom_read32; 28039 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28040 #endif /* _MULTI_DATAMODEL */ 28041 28042 if (data == NULL) { 28043 return (EINVAL); 28044 } 28045 28046 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28047 (un->un_state == SD_STATE_OFFLINE)) { 28048 return (ENXIO); 28049 } 28050 28051 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28052 "sd_read_mode1: entry: un:0x%p\n", un); 28053 28054 #ifdef _MULTI_DATAMODEL 28055 switch (ddi_model_convert_from(flag & FMODELS)) { 28056 case DDI_MODEL_ILP32: 28057 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28058 return (EFAULT); 28059 } 28060 /* Convert the ILP32 uscsi data from the application to LP64 */ 28061 cdrom_read32tocdrom_read(cdrd32, mode1); 28062 break; 28063 case DDI_MODEL_NONE: 28064 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28065 return (EFAULT); 28066 } 28067 } 28068 #else /* ! _MULTI_DATAMODEL */ 28069 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28070 return (EFAULT); 28071 } 28072 #endif /* _MULTI_DATAMODEL */ 28073 28074 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28075 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28076 28077 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28078 "sd_read_mode1: exit: un:0x%p\n", un); 28079 28080 return (rval); 28081 } 28082 28083 28084 /* 28085 * Function: sr_read_cd_mode2() 28086 * 28087 * Description: This routine is the driver entry point for handling CD-ROM 28088 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28089 * support the READ CD (0xBE) command or the 1st generation 28090 * READ CD (0xD4) command. 28091 * 28092 * Arguments: dev - the device 'dev_t' 28093 * data - pointer to user provided cd read structure specifying 28094 * the lba buffer address and length. 28095 * flag - this argument is a pass through to ddi_copyxxx() 28096 * directly from the mode argument of ioctl(). 28097 * 28098 * Return Code: the code returned by sd_send_scsi_cmd() 28099 * EFAULT if ddi_copyxxx() fails 28100 * ENXIO if fail ddi_get_soft_state 28101 * EINVAL if data pointer is NULL 28102 */ 28103 28104 static int 28105 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28106 { 28107 struct sd_lun *un; 28108 struct uscsi_cmd *com; 28109 struct cdrom_read mode2_struct; 28110 struct cdrom_read *mode2 = &mode2_struct; 28111 uchar_t cdb[CDB_GROUP5]; 28112 int nblocks; 28113 int rval; 28114 #ifdef _MULTI_DATAMODEL 28115 /* To support ILP32 applications in an LP64 world */ 28116 struct cdrom_read32 cdrom_read32; 28117 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28118 #endif /* _MULTI_DATAMODEL */ 28119 28120 if (data == NULL) { 28121 return (EINVAL); 28122 } 28123 28124 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28125 (un->un_state == SD_STATE_OFFLINE)) { 28126 return (ENXIO); 28127 } 28128 28129 #ifdef _MULTI_DATAMODEL 28130 switch (ddi_model_convert_from(flag & FMODELS)) { 28131 case DDI_MODEL_ILP32: 28132 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28133 return (EFAULT); 28134 } 28135 /* Convert the ILP32 uscsi data from the application to LP64 */ 28136 cdrom_read32tocdrom_read(cdrd32, mode2); 28137 break; 28138 case DDI_MODEL_NONE: 28139 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28140 return (EFAULT); 28141 } 28142 break; 28143 } 28144 28145 #else /* ! _MULTI_DATAMODEL */ 28146 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28147 return (EFAULT); 28148 } 28149 #endif /* _MULTI_DATAMODEL */ 28150 28151 bzero(cdb, sizeof (cdb)); 28152 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28153 /* Read command supported by 1st generation atapi drives */ 28154 cdb[0] = SCMD_READ_CDD4; 28155 } else { 28156 /* Universal CD Access Command */ 28157 cdb[0] = SCMD_READ_CD; 28158 } 28159 28160 /* 28161 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28162 */ 28163 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28164 28165 /* set the start address */ 28166 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28167 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28168 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28169 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28170 28171 /* set the transfer length */ 28172 nblocks = mode2->cdread_buflen / 2336; 28173 cdb[6] = (uchar_t)(nblocks >> 16); 28174 cdb[7] = (uchar_t)(nblocks >> 8); 28175 cdb[8] = (uchar_t)nblocks; 28176 28177 /* set the filter bits */ 28178 cdb[9] = CDROM_READ_CD_USERDATA; 28179 28180 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28181 com->uscsi_cdb = (caddr_t)cdb; 28182 com->uscsi_cdblen = sizeof (cdb); 28183 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28184 com->uscsi_buflen = mode2->cdread_buflen; 28185 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28186 28187 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28188 UIO_SYSSPACE, SD_PATH_STANDARD); 28189 kmem_free(com, sizeof (*com)); 28190 return (rval); 28191 } 28192 28193 28194 /* 28195 * Function: sr_read_mode2() 28196 * 28197 * Description: This routine is the driver entry point for handling CD-ROM 28198 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28199 * do not support the READ CD (0xBE) command. 28200 * 28201 * Arguments: dev - the device 'dev_t' 28202 * data - pointer to user provided cd read structure specifying 28203 * the lba buffer address and length. 28204 * flag - this argument is a pass through to ddi_copyxxx() 28205 * directly from the mode argument of ioctl(). 28206 * 28207 * Return Code: the code returned by sd_send_scsi_cmd() 28208 * EFAULT if ddi_copyxxx() fails 28209 * ENXIO if fail ddi_get_soft_state 28210 * EINVAL if data pointer is NULL 28211 * EIO if fail to reset block size 28212 * EAGAIN if commands are in progress in the driver 28213 */ 28214 28215 static int 28216 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28217 { 28218 struct sd_lun *un; 28219 struct cdrom_read mode2_struct; 28220 struct cdrom_read *mode2 = &mode2_struct; 28221 int rval; 28222 uint32_t restore_blksize; 28223 struct uscsi_cmd *com; 28224 uchar_t cdb[CDB_GROUP0]; 28225 int nblocks; 28226 28227 #ifdef _MULTI_DATAMODEL 28228 /* To support ILP32 applications in an LP64 world */ 28229 struct cdrom_read32 cdrom_read32; 28230 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28231 #endif /* _MULTI_DATAMODEL */ 28232 28233 if (data == NULL) { 28234 return (EINVAL); 28235 } 28236 28237 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28238 (un->un_state == SD_STATE_OFFLINE)) { 28239 return (ENXIO); 28240 } 28241 28242 /* 28243 * Because this routine will update the device and driver block size 28244 * being used we want to make sure there are no commands in progress. 28245 * If commands are in progress the user will have to try again. 28246 * 28247 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28248 * in sdioctl to protect commands from sdioctl through to the top of 28249 * sd_uscsi_strategy. See sdioctl for details. 28250 */ 28251 mutex_enter(SD_MUTEX(un)); 28252 if (un->un_ncmds_in_driver != 1) { 28253 mutex_exit(SD_MUTEX(un)); 28254 return (EAGAIN); 28255 } 28256 mutex_exit(SD_MUTEX(un)); 28257 28258 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28259 "sd_read_mode2: entry: un:0x%p\n", un); 28260 28261 #ifdef _MULTI_DATAMODEL 28262 switch (ddi_model_convert_from(flag & FMODELS)) { 28263 case DDI_MODEL_ILP32: 28264 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28265 return (EFAULT); 28266 } 28267 /* Convert the ILP32 uscsi data from the application to LP64 */ 28268 cdrom_read32tocdrom_read(cdrd32, mode2); 28269 break; 28270 case DDI_MODEL_NONE: 28271 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28272 return (EFAULT); 28273 } 28274 break; 28275 } 28276 #else /* ! _MULTI_DATAMODEL */ 28277 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28278 return (EFAULT); 28279 } 28280 #endif /* _MULTI_DATAMODEL */ 28281 28282 /* Store the current target block size for restoration later */ 28283 restore_blksize = un->un_tgt_blocksize; 28284 28285 /* Change the device and soft state target block size to 2336 */ 28286 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28287 rval = EIO; 28288 goto done; 28289 } 28290 28291 28292 bzero(cdb, sizeof (cdb)); 28293 28294 /* set READ operation */ 28295 cdb[0] = SCMD_READ; 28296 28297 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28298 mode2->cdread_lba >>= 2; 28299 28300 /* set the start address */ 28301 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28302 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28303 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28304 28305 /* set the transfer length */ 28306 nblocks = mode2->cdread_buflen / 2336; 28307 cdb[4] = (uchar_t)nblocks & 0xFF; 28308 28309 /* build command */ 28310 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28311 com->uscsi_cdb = (caddr_t)cdb; 28312 com->uscsi_cdblen = sizeof (cdb); 28313 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28314 com->uscsi_buflen = mode2->cdread_buflen; 28315 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28316 28317 /* 28318 * Issue SCSI command with user space address for read buffer. 28319 * 28320 * This sends the command through main channel in the driver. 28321 * 28322 * Since this is accessed via an IOCTL call, we go through the 28323 * standard path, so that if the device was powered down, then 28324 * it would be 'awakened' to handle the command. 28325 */ 28326 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28327 UIO_SYSSPACE, SD_PATH_STANDARD); 28328 28329 kmem_free(com, sizeof (*com)); 28330 28331 /* Restore the device and soft state target block size */ 28332 if (sr_sector_mode(dev, restore_blksize) != 0) { 28333 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28334 "can't do switch back to mode 1\n"); 28335 /* 28336 * If sd_send_scsi_READ succeeded we still need to report 28337 * an error because we failed to reset the block size 28338 */ 28339 if (rval == 0) { 28340 rval = EIO; 28341 } 28342 } 28343 28344 done: 28345 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28346 "sd_read_mode2: exit: un:0x%p\n", un); 28347 28348 return (rval); 28349 } 28350 28351 28352 /* 28353 * Function: sr_sector_mode() 28354 * 28355 * Description: This utility function is used by sr_read_mode2 to set the target 28356 * block size based on the user specified size. This is a legacy 28357 * implementation based upon a vendor specific mode page 28358 * 28359 * Arguments: dev - the device 'dev_t' 28360 * data - flag indicating if block size is being set to 2336 or 28361 * 512. 28362 * 28363 * Return Code: the code returned by sd_send_scsi_cmd() 28364 * EFAULT if ddi_copyxxx() fails 28365 * ENXIO if fail ddi_get_soft_state 28366 * EINVAL if data pointer is NULL 28367 */ 28368 28369 static int 28370 sr_sector_mode(dev_t dev, uint32_t blksize) 28371 { 28372 struct sd_lun *un; 28373 uchar_t *sense; 28374 uchar_t *select; 28375 int rval; 28376 28377 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28378 (un->un_state == SD_STATE_OFFLINE)) { 28379 return (ENXIO); 28380 } 28381 28382 sense = kmem_zalloc(20, KM_SLEEP); 28383 28384 /* Note: This is a vendor specific mode page (0x81) */ 28385 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 28386 SD_PATH_STANDARD)) != 0) { 28387 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28388 "sr_sector_mode: Mode Sense failed\n"); 28389 kmem_free(sense, 20); 28390 return (rval); 28391 } 28392 select = kmem_zalloc(20, KM_SLEEP); 28393 select[3] = 0x08; 28394 select[10] = ((blksize >> 8) & 0xff); 28395 select[11] = (blksize & 0xff); 28396 select[12] = 0x01; 28397 select[13] = 0x06; 28398 select[14] = sense[14]; 28399 select[15] = sense[15]; 28400 if (blksize == SD_MODE2_BLKSIZE) { 28401 select[14] |= 0x01; 28402 } 28403 28404 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 28405 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 28406 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28407 "sr_sector_mode: Mode Select failed\n"); 28408 } else { 28409 /* 28410 * Only update the softstate block size if we successfully 28411 * changed the device block mode. 28412 */ 28413 mutex_enter(SD_MUTEX(un)); 28414 sd_update_block_info(un, blksize, 0); 28415 mutex_exit(SD_MUTEX(un)); 28416 } 28417 kmem_free(sense, 20); 28418 kmem_free(select, 20); 28419 return (rval); 28420 } 28421 28422 28423 /* 28424 * Function: sr_read_cdda() 28425 * 28426 * Description: This routine is the driver entry point for handling CD-ROM 28427 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28428 * the target supports CDDA these requests are handled via a vendor 28429 * specific command (0xD8) If the target does not support CDDA 28430 * these requests are handled via the READ CD command (0xBE). 28431 * 28432 * Arguments: dev - the device 'dev_t' 28433 * data - pointer to user provided CD-DA structure specifying 28434 * the track starting address, transfer length, and 28435 * subcode options. 28436 * flag - this argument is a pass through to ddi_copyxxx() 28437 * directly from the mode argument of ioctl(). 28438 * 28439 * Return Code: the code returned by sd_send_scsi_cmd() 28440 * EFAULT if ddi_copyxxx() fails 28441 * ENXIO if fail ddi_get_soft_state 28442 * EINVAL if invalid arguments are provided 28443 * ENOTTY 28444 */ 28445 28446 static int 28447 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28448 { 28449 struct sd_lun *un; 28450 struct uscsi_cmd *com; 28451 struct cdrom_cdda *cdda; 28452 int rval; 28453 size_t buflen; 28454 char cdb[CDB_GROUP5]; 28455 28456 #ifdef _MULTI_DATAMODEL 28457 /* To support ILP32 applications in an LP64 world */ 28458 struct cdrom_cdda32 cdrom_cdda32; 28459 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28460 #endif /* _MULTI_DATAMODEL */ 28461 28462 if (data == NULL) { 28463 return (EINVAL); 28464 } 28465 28466 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28467 return (ENXIO); 28468 } 28469 28470 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28471 28472 #ifdef _MULTI_DATAMODEL 28473 switch (ddi_model_convert_from(flag & FMODELS)) { 28474 case DDI_MODEL_ILP32: 28475 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28476 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28477 "sr_read_cdda: ddi_copyin Failed\n"); 28478 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28479 return (EFAULT); 28480 } 28481 /* Convert the ILP32 uscsi data from the application to LP64 */ 28482 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28483 break; 28484 case DDI_MODEL_NONE: 28485 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28486 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28487 "sr_read_cdda: ddi_copyin Failed\n"); 28488 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28489 return (EFAULT); 28490 } 28491 break; 28492 } 28493 #else /* ! _MULTI_DATAMODEL */ 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 #endif /* _MULTI_DATAMODEL */ 28501 28502 /* 28503 * Since MMC-2 expects max 3 bytes for length, check if the 28504 * length input is greater than 3 bytes 28505 */ 28506 if ((cdda->cdda_length & 0xFF000000) != 0) { 28507 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28508 "cdrom transfer length too large: %d (limit %d)\n", 28509 cdda->cdda_length, 0xFFFFFF); 28510 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28511 return (EINVAL); 28512 } 28513 28514 switch (cdda->cdda_subcode) { 28515 case CDROM_DA_NO_SUBCODE: 28516 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28517 break; 28518 case CDROM_DA_SUBQ: 28519 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28520 break; 28521 case CDROM_DA_ALL_SUBCODE: 28522 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28523 break; 28524 case CDROM_DA_SUBCODE_ONLY: 28525 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28526 break; 28527 default: 28528 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28529 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28530 cdda->cdda_subcode); 28531 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28532 return (EINVAL); 28533 } 28534 28535 /* Build and send the command */ 28536 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28537 bzero(cdb, CDB_GROUP5); 28538 28539 if (un->un_f_cfg_cdda == TRUE) { 28540 cdb[0] = (char)SCMD_READ_CD; 28541 cdb[1] = 0x04; 28542 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28543 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28544 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28545 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28546 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28547 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28548 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28549 cdb[9] = 0x10; 28550 switch (cdda->cdda_subcode) { 28551 case CDROM_DA_NO_SUBCODE : 28552 cdb[10] = 0x0; 28553 break; 28554 case CDROM_DA_SUBQ : 28555 cdb[10] = 0x2; 28556 break; 28557 case CDROM_DA_ALL_SUBCODE : 28558 cdb[10] = 0x1; 28559 break; 28560 case CDROM_DA_SUBCODE_ONLY : 28561 /* FALLTHROUGH */ 28562 default : 28563 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28564 kmem_free(com, sizeof (*com)); 28565 return (ENOTTY); 28566 } 28567 } else { 28568 cdb[0] = (char)SCMD_READ_CDDA; 28569 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28570 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28571 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28572 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28573 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28574 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28575 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28576 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28577 cdb[10] = cdda->cdda_subcode; 28578 } 28579 28580 com->uscsi_cdb = cdb; 28581 com->uscsi_cdblen = CDB_GROUP5; 28582 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28583 com->uscsi_buflen = buflen; 28584 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28585 28586 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28587 UIO_SYSSPACE, SD_PATH_STANDARD); 28588 28589 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28590 kmem_free(com, sizeof (*com)); 28591 return (rval); 28592 } 28593 28594 28595 /* 28596 * Function: sr_read_cdxa() 28597 * 28598 * Description: This routine is the driver entry point for handling CD-ROM 28599 * ioctl requests to return CD-XA (Extended Architecture) data. 28600 * (CDROMCDXA). 28601 * 28602 * Arguments: dev - the device 'dev_t' 28603 * data - pointer to user provided CD-XA structure specifying 28604 * the data starting address, transfer length, and format 28605 * flag - this argument is a pass through to ddi_copyxxx() 28606 * directly from the mode argument of ioctl(). 28607 * 28608 * Return Code: the code returned by sd_send_scsi_cmd() 28609 * EFAULT if ddi_copyxxx() fails 28610 * ENXIO if fail ddi_get_soft_state 28611 * EINVAL if data pointer is NULL 28612 */ 28613 28614 static int 28615 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28616 { 28617 struct sd_lun *un; 28618 struct uscsi_cmd *com; 28619 struct cdrom_cdxa *cdxa; 28620 int rval; 28621 size_t buflen; 28622 char cdb[CDB_GROUP5]; 28623 uchar_t read_flags; 28624 28625 #ifdef _MULTI_DATAMODEL 28626 /* To support ILP32 applications in an LP64 world */ 28627 struct cdrom_cdxa32 cdrom_cdxa32; 28628 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28629 #endif /* _MULTI_DATAMODEL */ 28630 28631 if (data == NULL) { 28632 return (EINVAL); 28633 } 28634 28635 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28636 return (ENXIO); 28637 } 28638 28639 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28640 28641 #ifdef _MULTI_DATAMODEL 28642 switch (ddi_model_convert_from(flag & FMODELS)) { 28643 case DDI_MODEL_ILP32: 28644 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28645 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28646 return (EFAULT); 28647 } 28648 /* 28649 * Convert the ILP32 uscsi data from the 28650 * application to LP64 for internal use. 28651 */ 28652 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28653 break; 28654 case DDI_MODEL_NONE: 28655 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28656 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28657 return (EFAULT); 28658 } 28659 break; 28660 } 28661 #else /* ! _MULTI_DATAMODEL */ 28662 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28663 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28664 return (EFAULT); 28665 } 28666 #endif /* _MULTI_DATAMODEL */ 28667 28668 /* 28669 * Since MMC-2 expects max 3 bytes for length, check if the 28670 * length input is greater than 3 bytes 28671 */ 28672 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28673 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28674 "cdrom transfer length too large: %d (limit %d)\n", 28675 cdxa->cdxa_length, 0xFFFFFF); 28676 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28677 return (EINVAL); 28678 } 28679 28680 switch (cdxa->cdxa_format) { 28681 case CDROM_XA_DATA: 28682 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28683 read_flags = 0x10; 28684 break; 28685 case CDROM_XA_SECTOR_DATA: 28686 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28687 read_flags = 0xf8; 28688 break; 28689 case CDROM_XA_DATA_W_ERROR: 28690 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28691 read_flags = 0xfc; 28692 break; 28693 default: 28694 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28695 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28696 cdxa->cdxa_format); 28697 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28698 return (EINVAL); 28699 } 28700 28701 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28702 bzero(cdb, CDB_GROUP5); 28703 if (un->un_f_mmc_cap == TRUE) { 28704 cdb[0] = (char)SCMD_READ_CD; 28705 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28706 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28707 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28708 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28709 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28710 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28711 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28712 cdb[9] = (char)read_flags; 28713 } else { 28714 /* 28715 * Note: A vendor specific command (0xDB) is being used her to 28716 * request a read of all subcodes. 28717 */ 28718 cdb[0] = (char)SCMD_READ_CDXA; 28719 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28720 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28721 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28722 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28723 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28724 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28725 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28726 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28727 cdb[10] = cdxa->cdxa_format; 28728 } 28729 com->uscsi_cdb = cdb; 28730 com->uscsi_cdblen = CDB_GROUP5; 28731 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28732 com->uscsi_buflen = buflen; 28733 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28734 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28735 UIO_SYSSPACE, SD_PATH_STANDARD); 28736 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28737 kmem_free(com, sizeof (*com)); 28738 return (rval); 28739 } 28740 28741 28742 /* 28743 * Function: sr_eject() 28744 * 28745 * Description: This routine is the driver entry point for handling CD-ROM 28746 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28747 * 28748 * Arguments: dev - the device 'dev_t' 28749 * 28750 * Return Code: the code returned by sd_send_scsi_cmd() 28751 */ 28752 28753 static int 28754 sr_eject(dev_t dev) 28755 { 28756 struct sd_lun *un; 28757 int rval; 28758 28759 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28760 (un->un_state == SD_STATE_OFFLINE)) { 28761 return (ENXIO); 28762 } 28763 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 28764 SD_PATH_STANDARD)) != 0) { 28765 return (rval); 28766 } 28767 28768 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 28769 SD_PATH_STANDARD); 28770 28771 if (rval == 0) { 28772 mutex_enter(SD_MUTEX(un)); 28773 sr_ejected(un); 28774 un->un_mediastate = DKIO_EJECTED; 28775 cv_broadcast(&un->un_state_cv); 28776 mutex_exit(SD_MUTEX(un)); 28777 } 28778 return (rval); 28779 } 28780 28781 28782 /* 28783 * Function: sr_ejected() 28784 * 28785 * Description: This routine updates the soft state structure to invalidate the 28786 * geometry information after the media has been ejected or a 28787 * media eject has been detected. 28788 * 28789 * Arguments: un - driver soft state (unit) structure 28790 */ 28791 28792 static void 28793 sr_ejected(struct sd_lun *un) 28794 { 28795 struct sd_errstats *stp; 28796 28797 ASSERT(un != NULL); 28798 ASSERT(mutex_owned(SD_MUTEX(un))); 28799 28800 un->un_f_blockcount_is_valid = FALSE; 28801 un->un_f_tgt_blocksize_is_valid = FALSE; 28802 un->un_f_geometry_is_valid = FALSE; 28803 28804 if (un->un_errstats != NULL) { 28805 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28806 stp->sd_capacity.value.ui64 = 0; 28807 } 28808 } 28809 28810 28811 /* 28812 * Function: sr_check_wp() 28813 * 28814 * Description: This routine checks the write protection of a removable media 28815 * disk via the write protect bit of the Mode Page Header device 28816 * specific field. This routine has been implemented to use the 28817 * error recovery mode page for all device types. 28818 * Note: In the future use a sd_send_scsi_MODE_SENSE() routine 28819 * 28820 * Arguments: dev - the device 'dev_t' 28821 * 28822 * Return Code: int indicating if the device is write protected (1) or not (0) 28823 * 28824 * Context: Kernel thread. 28825 * 28826 */ 28827 28828 static int 28829 sr_check_wp(dev_t dev) 28830 { 28831 struct sd_lun *un; 28832 uchar_t device_specific; 28833 uchar_t *sense; 28834 int hdrlen; 28835 int rval; 28836 int retry_flag = FALSE; 28837 28838 /* 28839 * Note: The return codes for this routine should be reworked to 28840 * properly handle the case of a NULL softstate. 28841 */ 28842 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28843 return (FALSE); 28844 } 28845 28846 if (un->un_f_cfg_is_atapi == TRUE) { 28847 retry_flag = TRUE; 28848 } 28849 28850 retry: 28851 if (un->un_f_cfg_is_atapi == TRUE) { 28852 /* 28853 * The mode page contents are not required; set the allocation 28854 * length for the mode page header only 28855 */ 28856 hdrlen = MODE_HEADER_LENGTH_GRP2; 28857 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28858 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 28859 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28860 device_specific = 28861 ((struct mode_header_grp2 *)sense)->device_specific; 28862 } else { 28863 hdrlen = MODE_HEADER_LENGTH; 28864 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28865 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 28866 MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 28867 device_specific = 28868 ((struct mode_header *)sense)->device_specific; 28869 } 28870 28871 if (rval != 0) { 28872 if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) { 28873 /* 28874 * For an Atapi Zip drive, observed the drive 28875 * reporting check condition for the first attempt. 28876 * Sense data indicating power on or bus device/reset. 28877 * Hence in case of failure need to try at least once 28878 * for Atapi devices. 28879 */ 28880 retry_flag = FALSE; 28881 kmem_free(sense, hdrlen); 28882 goto retry; 28883 } else { 28884 /* 28885 * Write protect mode sense failed; not all disks 28886 * understand this query. Return FALSE assuming that 28887 * these devices are not writable. 28888 */ 28889 rval = FALSE; 28890 } 28891 } else { 28892 if (device_specific & WRITE_PROTECT) { 28893 rval = TRUE; 28894 } else { 28895 rval = FALSE; 28896 } 28897 } 28898 kmem_free(sense, hdrlen); 28899 return (rval); 28900 } 28901 28902 28903 /* 28904 * Function: sr_volume_ctrl() 28905 * 28906 * Description: This routine is the driver entry point for handling CD-ROM 28907 * audio output volume ioctl requests. (CDROMVOLCTRL) 28908 * 28909 * Arguments: dev - the device 'dev_t' 28910 * data - pointer to user audio volume control structure 28911 * flag - this argument is a pass through to ddi_copyxxx() 28912 * directly from the mode argument of ioctl(). 28913 * 28914 * Return Code: the code returned by sd_send_scsi_cmd() 28915 * EFAULT if ddi_copyxxx() fails 28916 * ENXIO if fail ddi_get_soft_state 28917 * EINVAL if data pointer is NULL 28918 * 28919 */ 28920 28921 static int 28922 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28923 { 28924 struct sd_lun *un; 28925 struct cdrom_volctrl volume; 28926 struct cdrom_volctrl *vol = &volume; 28927 uchar_t *sense_page; 28928 uchar_t *select_page; 28929 uchar_t *sense; 28930 uchar_t *select; 28931 int sense_buflen; 28932 int select_buflen; 28933 int rval; 28934 28935 if (data == NULL) { 28936 return (EINVAL); 28937 } 28938 28939 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28940 (un->un_state == SD_STATE_OFFLINE)) { 28941 return (ENXIO); 28942 } 28943 28944 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 28945 return (EFAULT); 28946 } 28947 28948 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28949 struct mode_header_grp2 *sense_mhp; 28950 struct mode_header_grp2 *select_mhp; 28951 int bd_len; 28952 28953 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 28954 select_buflen = MODE_HEADER_LENGTH_GRP2 + 28955 MODEPAGE_AUDIO_CTRL_LEN; 28956 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28957 select = kmem_zalloc(select_buflen, KM_SLEEP); 28958 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 28959 sense_buflen, MODEPAGE_AUDIO_CTRL, 28960 SD_PATH_STANDARD)) != 0) { 28961 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28962 "sr_volume_ctrl: Mode Sense Failed\n"); 28963 kmem_free(sense, sense_buflen); 28964 kmem_free(select, select_buflen); 28965 return (rval); 28966 } 28967 sense_mhp = (struct mode_header_grp2 *)sense; 28968 select_mhp = (struct mode_header_grp2 *)select; 28969 bd_len = (sense_mhp->bdesc_length_hi << 8) | 28970 sense_mhp->bdesc_length_lo; 28971 if (bd_len > MODE_BLK_DESC_LENGTH) { 28972 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28973 "sr_volume_ctrl: Mode Sense returned invalid " 28974 "block descriptor length\n"); 28975 kmem_free(sense, sense_buflen); 28976 kmem_free(select, select_buflen); 28977 return (EIO); 28978 } 28979 sense_page = (uchar_t *) 28980 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 28981 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 28982 select_mhp->length_msb = 0; 28983 select_mhp->length_lsb = 0; 28984 select_mhp->bdesc_length_hi = 0; 28985 select_mhp->bdesc_length_lo = 0; 28986 } else { 28987 struct mode_header *sense_mhp, *select_mhp; 28988 28989 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28990 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28991 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28992 select = kmem_zalloc(select_buflen, KM_SLEEP); 28993 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 28994 sense_buflen, MODEPAGE_AUDIO_CTRL, 28995 SD_PATH_STANDARD)) != 0) { 28996 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28997 "sr_volume_ctrl: Mode Sense Failed\n"); 28998 kmem_free(sense, sense_buflen); 28999 kmem_free(select, select_buflen); 29000 return (rval); 29001 } 29002 sense_mhp = (struct mode_header *)sense; 29003 select_mhp = (struct mode_header *)select; 29004 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29005 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29006 "sr_volume_ctrl: Mode Sense returned invalid " 29007 "block descriptor length\n"); 29008 kmem_free(sense, sense_buflen); 29009 kmem_free(select, select_buflen); 29010 return (EIO); 29011 } 29012 sense_page = (uchar_t *) 29013 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29014 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29015 select_mhp->length = 0; 29016 select_mhp->bdesc_length = 0; 29017 } 29018 /* 29019 * Note: An audio control data structure could be created and overlayed 29020 * on the following in place of the array indexing method implemented. 29021 */ 29022 29023 /* Build the select data for the user volume data */ 29024 select_page[0] = MODEPAGE_AUDIO_CTRL; 29025 select_page[1] = 0xE; 29026 /* Set the immediate bit */ 29027 select_page[2] = 0x04; 29028 /* Zero out reserved fields */ 29029 select_page[3] = 0x00; 29030 select_page[4] = 0x00; 29031 /* Return sense data for fields not to be modified */ 29032 select_page[5] = sense_page[5]; 29033 select_page[6] = sense_page[6]; 29034 select_page[7] = sense_page[7]; 29035 /* Set the user specified volume levels for channel 0 and 1 */ 29036 select_page[8] = 0x01; 29037 select_page[9] = vol->channel0; 29038 select_page[10] = 0x02; 29039 select_page[11] = vol->channel1; 29040 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29041 select_page[12] = sense_page[12]; 29042 select_page[13] = sense_page[13]; 29043 select_page[14] = sense_page[14]; 29044 select_page[15] = sense_page[15]; 29045 29046 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29047 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29048 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29049 } else { 29050 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29051 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29052 } 29053 29054 kmem_free(sense, sense_buflen); 29055 kmem_free(select, select_buflen); 29056 return (rval); 29057 } 29058 29059 29060 /* 29061 * Function: sr_read_sony_session_offset() 29062 * 29063 * Description: This routine is the driver entry point for handling CD-ROM 29064 * ioctl requests for session offset information. (CDROMREADOFFSET) 29065 * The address of the first track in the last session of a 29066 * multi-session CD-ROM is returned 29067 * 29068 * Note: This routine uses a vendor specific key value in the 29069 * command control field without implementing any vendor check here 29070 * or in the ioctl routine. 29071 * 29072 * Arguments: dev - the device 'dev_t' 29073 * data - pointer to an int to hold the requested address 29074 * flag - this argument is a pass through to ddi_copyxxx() 29075 * directly from the mode argument of ioctl(). 29076 * 29077 * Return Code: the code returned by sd_send_scsi_cmd() 29078 * EFAULT if ddi_copyxxx() fails 29079 * ENXIO if fail ddi_get_soft_state 29080 * EINVAL if data pointer is NULL 29081 */ 29082 29083 static int 29084 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29085 { 29086 struct sd_lun *un; 29087 struct uscsi_cmd *com; 29088 caddr_t buffer; 29089 char cdb[CDB_GROUP1]; 29090 int session_offset = 0; 29091 int rval; 29092 29093 if (data == NULL) { 29094 return (EINVAL); 29095 } 29096 29097 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29098 (un->un_state == SD_STATE_OFFLINE)) { 29099 return (ENXIO); 29100 } 29101 29102 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29103 bzero(cdb, CDB_GROUP1); 29104 cdb[0] = SCMD_READ_TOC; 29105 /* 29106 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29107 * (4 byte TOC response header + 8 byte response data) 29108 */ 29109 cdb[8] = SONY_SESSION_OFFSET_LEN; 29110 /* Byte 9 is the control byte. A vendor specific value is used */ 29111 cdb[9] = SONY_SESSION_OFFSET_KEY; 29112 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29113 com->uscsi_cdb = cdb; 29114 com->uscsi_cdblen = CDB_GROUP1; 29115 com->uscsi_bufaddr = buffer; 29116 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29117 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29118 29119 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29120 UIO_SYSSPACE, SD_PATH_STANDARD); 29121 if (rval != 0) { 29122 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29123 kmem_free(com, sizeof (*com)); 29124 return (rval); 29125 } 29126 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29127 session_offset = 29128 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29129 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29130 /* 29131 * Offset returned offset in current lbasize block's. Convert to 29132 * 2k block's to return to the user 29133 */ 29134 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29135 session_offset >>= 2; 29136 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29137 session_offset >>= 1; 29138 } 29139 } 29140 29141 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29142 rval = EFAULT; 29143 } 29144 29145 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29146 kmem_free(com, sizeof (*com)); 29147 return (rval); 29148 } 29149 29150 29151 /* 29152 * Function: sd_wm_cache_constructor() 29153 * 29154 * Description: Cache Constructor for the wmap cache for the read/modify/write 29155 * devices. 29156 * 29157 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29158 * un - sd_lun structure for the device. 29159 * flag - the km flags passed to constructor 29160 * 29161 * Return Code: 0 on success. 29162 * -1 on failure. 29163 */ 29164 29165 /*ARGSUSED*/ 29166 static int 29167 sd_wm_cache_constructor(void *wm, void *un, int flags) 29168 { 29169 bzero(wm, sizeof (struct sd_w_map)); 29170 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29171 return (0); 29172 } 29173 29174 29175 /* 29176 * Function: sd_wm_cache_destructor() 29177 * 29178 * Description: Cache destructor for the wmap cache for the read/modify/write 29179 * devices. 29180 * 29181 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29182 * un - sd_lun structure for the device. 29183 */ 29184 /*ARGSUSED*/ 29185 static void 29186 sd_wm_cache_destructor(void *wm, void *un) 29187 { 29188 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29189 } 29190 29191 29192 /* 29193 * Function: sd_range_lock() 29194 * 29195 * Description: Lock the range of blocks specified as parameter to ensure 29196 * that read, modify write is atomic and no other i/o writes 29197 * to the same location. The range is specified in terms 29198 * of start and end blocks. Block numbers are the actual 29199 * media block numbers and not system. 29200 * 29201 * Arguments: un - sd_lun structure for the device. 29202 * startb - The starting block number 29203 * endb - The end block number 29204 * typ - type of i/o - simple/read_modify_write 29205 * 29206 * Return Code: wm - pointer to the wmap structure. 29207 * 29208 * Context: This routine can sleep. 29209 */ 29210 29211 static struct sd_w_map * 29212 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29213 { 29214 struct sd_w_map *wmp = NULL; 29215 struct sd_w_map *sl_wmp = NULL; 29216 struct sd_w_map *tmp_wmp; 29217 wm_state state = SD_WM_CHK_LIST; 29218 29219 29220 ASSERT(un != NULL); 29221 ASSERT(!mutex_owned(SD_MUTEX(un))); 29222 29223 mutex_enter(SD_MUTEX(un)); 29224 29225 while (state != SD_WM_DONE) { 29226 29227 switch (state) { 29228 case SD_WM_CHK_LIST: 29229 /* 29230 * This is the starting state. Check the wmap list 29231 * to see if the range is currently available. 29232 */ 29233 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29234 /* 29235 * If this is a simple write and no rmw 29236 * i/o is pending then try to lock the 29237 * range as the range should be available. 29238 */ 29239 state = SD_WM_LOCK_RANGE; 29240 } else { 29241 tmp_wmp = sd_get_range(un, startb, endb); 29242 if (tmp_wmp != NULL) { 29243 if ((wmp != NULL) && ONLIST(un, wmp)) { 29244 /* 29245 * Should not keep onlist wmps 29246 * while waiting this macro 29247 * will also do wmp = NULL; 29248 */ 29249 FREE_ONLIST_WMAP(un, wmp); 29250 } 29251 /* 29252 * sl_wmp is the wmap on which wait 29253 * is done, since the tmp_wmp points 29254 * to the inuse wmap, set sl_wmp to 29255 * tmp_wmp and change the state to sleep 29256 */ 29257 sl_wmp = tmp_wmp; 29258 state = SD_WM_WAIT_MAP; 29259 } else { 29260 state = SD_WM_LOCK_RANGE; 29261 } 29262 29263 } 29264 break; 29265 29266 case SD_WM_LOCK_RANGE: 29267 ASSERT(un->un_wm_cache); 29268 /* 29269 * The range need to be locked, try to get a wmap. 29270 * First attempt it with NO_SLEEP, want to avoid a sleep 29271 * if possible as we will have to release the sd mutex 29272 * if we have to sleep. 29273 */ 29274 if (wmp == NULL) 29275 wmp = kmem_cache_alloc(un->un_wm_cache, 29276 KM_NOSLEEP); 29277 if (wmp == NULL) { 29278 mutex_exit(SD_MUTEX(un)); 29279 _NOTE(DATA_READABLE_WITHOUT_LOCK 29280 (sd_lun::un_wm_cache)) 29281 wmp = kmem_cache_alloc(un->un_wm_cache, 29282 KM_SLEEP); 29283 mutex_enter(SD_MUTEX(un)); 29284 /* 29285 * we released the mutex so recheck and go to 29286 * check list state. 29287 */ 29288 state = SD_WM_CHK_LIST; 29289 } else { 29290 /* 29291 * We exit out of state machine since we 29292 * have the wmap. Do the housekeeping first. 29293 * place the wmap on the wmap list if it is not 29294 * on it already and then set the state to done. 29295 */ 29296 wmp->wm_start = startb; 29297 wmp->wm_end = endb; 29298 wmp->wm_flags = typ | SD_WM_BUSY; 29299 if (typ & SD_WTYPE_RMW) { 29300 un->un_rmw_count++; 29301 } 29302 /* 29303 * If not already on the list then link 29304 */ 29305 if (!ONLIST(un, wmp)) { 29306 wmp->wm_next = un->un_wm; 29307 wmp->wm_prev = NULL; 29308 if (wmp->wm_next) 29309 wmp->wm_next->wm_prev = wmp; 29310 un->un_wm = wmp; 29311 } 29312 state = SD_WM_DONE; 29313 } 29314 break; 29315 29316 case SD_WM_WAIT_MAP: 29317 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29318 /* 29319 * Wait is done on sl_wmp, which is set in the 29320 * check_list state. 29321 */ 29322 sl_wmp->wm_wanted_count++; 29323 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29324 sl_wmp->wm_wanted_count--; 29325 if (!(sl_wmp->wm_flags & SD_WM_BUSY)) { 29326 if (wmp != NULL) 29327 CHK_N_FREEWMP(un, wmp); 29328 wmp = sl_wmp; 29329 } 29330 sl_wmp = NULL; 29331 /* 29332 * After waking up, need to recheck for availability of 29333 * range. 29334 */ 29335 state = SD_WM_CHK_LIST; 29336 break; 29337 29338 default: 29339 panic("sd_range_lock: " 29340 "Unknown state %d in sd_range_lock", state); 29341 /*NOTREACHED*/ 29342 } /* switch(state) */ 29343 29344 } /* while(state != SD_WM_DONE) */ 29345 29346 mutex_exit(SD_MUTEX(un)); 29347 29348 ASSERT(wmp != NULL); 29349 29350 return (wmp); 29351 } 29352 29353 29354 /* 29355 * Function: sd_get_range() 29356 * 29357 * Description: Find if there any overlapping I/O to this one 29358 * Returns the write-map of 1st such I/O, NULL otherwise. 29359 * 29360 * Arguments: un - sd_lun structure for the device. 29361 * startb - The starting block number 29362 * endb - The end block number 29363 * 29364 * Return Code: wm - pointer to the wmap structure. 29365 */ 29366 29367 static struct sd_w_map * 29368 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29369 { 29370 struct sd_w_map *wmp; 29371 29372 ASSERT(un != NULL); 29373 29374 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29375 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29376 continue; 29377 } 29378 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29379 break; 29380 } 29381 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29382 break; 29383 } 29384 } 29385 29386 return (wmp); 29387 } 29388 29389 29390 /* 29391 * Function: sd_free_inlist_wmap() 29392 * 29393 * Description: Unlink and free a write map struct. 29394 * 29395 * Arguments: un - sd_lun structure for the device. 29396 * wmp - sd_w_map which needs to be unlinked. 29397 */ 29398 29399 static void 29400 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29401 { 29402 ASSERT(un != NULL); 29403 29404 if (un->un_wm == wmp) { 29405 un->un_wm = wmp->wm_next; 29406 } else { 29407 wmp->wm_prev->wm_next = wmp->wm_next; 29408 } 29409 29410 if (wmp->wm_next) { 29411 wmp->wm_next->wm_prev = wmp->wm_prev; 29412 } 29413 29414 wmp->wm_next = wmp->wm_prev = NULL; 29415 29416 kmem_cache_free(un->un_wm_cache, wmp); 29417 } 29418 29419 29420 /* 29421 * Function: sd_range_unlock() 29422 * 29423 * Description: Unlock the range locked by wm. 29424 * Free write map if nobody else is waiting on it. 29425 * 29426 * Arguments: un - sd_lun structure for the device. 29427 * wmp - sd_w_map which needs to be unlinked. 29428 */ 29429 29430 static void 29431 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29432 { 29433 ASSERT(un != NULL); 29434 ASSERT(wm != NULL); 29435 ASSERT(!mutex_owned(SD_MUTEX(un))); 29436 29437 mutex_enter(SD_MUTEX(un)); 29438 29439 if (wm->wm_flags & SD_WTYPE_RMW) { 29440 un->un_rmw_count--; 29441 } 29442 29443 if (wm->wm_wanted_count) { 29444 wm->wm_flags = 0; 29445 /* 29446 * Broadcast that the wmap is available now. 29447 */ 29448 cv_broadcast(&wm->wm_avail); 29449 } else { 29450 /* 29451 * If no one is waiting on the map, it should be free'ed. 29452 */ 29453 sd_free_inlist_wmap(un, wm); 29454 } 29455 29456 mutex_exit(SD_MUTEX(un)); 29457 } 29458 29459 29460 /* 29461 * Function: sd_read_modify_write_task 29462 * 29463 * Description: Called from a taskq thread to initiate the write phase of 29464 * a read-modify-write request. This is used for targets where 29465 * un->un_sys_blocksize != un->un_tgt_blocksize. 29466 * 29467 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29468 * 29469 * Context: Called under taskq thread context. 29470 */ 29471 29472 static void 29473 sd_read_modify_write_task(void *arg) 29474 { 29475 struct sd_mapblocksize_info *bsp; 29476 struct buf *bp; 29477 struct sd_xbuf *xp; 29478 struct sd_lun *un; 29479 29480 bp = arg; /* The bp is given in arg */ 29481 ASSERT(bp != NULL); 29482 29483 /* Get the pointer to the layer-private data struct */ 29484 xp = SD_GET_XBUF(bp); 29485 ASSERT(xp != NULL); 29486 bsp = xp->xb_private; 29487 ASSERT(bsp != NULL); 29488 29489 un = SD_GET_UN(bp); 29490 ASSERT(un != NULL); 29491 ASSERT(!mutex_owned(SD_MUTEX(un))); 29492 29493 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29494 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29495 29496 /* 29497 * This is the write phase of a read-modify-write request, called 29498 * under the context of a taskq thread in response to the completion 29499 * of the read portion of the rmw request completing under interrupt 29500 * context. The write request must be sent from here down the iostart 29501 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29502 * we use the layer index saved in the layer-private data area. 29503 */ 29504 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29505 29506 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29507 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29508 } 29509 29510 29511 /* 29512 * Function: sddump_do_read_of_rmw() 29513 * 29514 * Description: This routine will be called from sddump, If sddump is called 29515 * with an I/O which not aligned on device blocksize boundary 29516 * then the write has to be converted to read-modify-write. 29517 * Do the read part here in order to keep sddump simple. 29518 * Note - That the sd_mutex is held across the call to this 29519 * routine. 29520 * 29521 * Arguments: un - sd_lun 29522 * blkno - block number in terms of media block size. 29523 * nblk - number of blocks. 29524 * bpp - pointer to pointer to the buf structure. On return 29525 * from this function, *bpp points to the valid buffer 29526 * to which the write has to be done. 29527 * 29528 * Return Code: 0 for success or errno-type return code 29529 */ 29530 29531 static int 29532 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29533 struct buf **bpp) 29534 { 29535 int err; 29536 int i; 29537 int rval; 29538 struct buf *bp; 29539 struct scsi_pkt *pkt = NULL; 29540 uint32_t target_blocksize; 29541 29542 ASSERT(un != NULL); 29543 ASSERT(mutex_owned(SD_MUTEX(un))); 29544 29545 target_blocksize = un->un_tgt_blocksize; 29546 29547 mutex_exit(SD_MUTEX(un)); 29548 29549 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29550 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29551 if (bp == NULL) { 29552 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29553 "no resources for dumping; giving up"); 29554 err = ENOMEM; 29555 goto done; 29556 } 29557 29558 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29559 blkno, nblk); 29560 if (rval != 0) { 29561 scsi_free_consistent_buf(bp); 29562 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29563 "no resources for dumping; giving up"); 29564 err = ENOMEM; 29565 goto done; 29566 } 29567 29568 pkt->pkt_flags |= FLAG_NOINTR; 29569 29570 err = EIO; 29571 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29572 29573 /* 29574 * Scsi_poll returns 0 (success) if the command completes and 29575 * the status block is STATUS_GOOD. We should only check 29576 * errors if this condition is not true. Even then we should 29577 * send our own request sense packet only if we have a check 29578 * condition and auto request sense has not been performed by 29579 * the hba. 29580 */ 29581 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29582 29583 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29584 err = 0; 29585 break; 29586 } 29587 29588 /* 29589 * Check CMD_DEV_GONE 1st, give up if device is gone, 29590 * no need to read RQS data. 29591 */ 29592 if (pkt->pkt_reason == CMD_DEV_GONE) { 29593 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 29594 "Device is gone\n"); 29595 break; 29596 } 29597 29598 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29599 SD_INFO(SD_LOG_DUMP, un, 29600 "sddump: read failed with CHECK, try # %d\n", i); 29601 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29602 (void) sd_send_polled_RQS(un); 29603 } 29604 29605 continue; 29606 } 29607 29608 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29609 int reset_retval = 0; 29610 29611 SD_INFO(SD_LOG_DUMP, un, 29612 "sddump: read failed with BUSY, try # %d\n", i); 29613 29614 if (un->un_f_lun_reset_enabled == TRUE) { 29615 reset_retval = scsi_reset(SD_ADDRESS(un), 29616 RESET_LUN); 29617 } 29618 if (reset_retval == 0) { 29619 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29620 } 29621 (void) sd_send_polled_RQS(un); 29622 29623 } else { 29624 SD_INFO(SD_LOG_DUMP, un, 29625 "sddump: read failed with 0x%x, try # %d\n", 29626 SD_GET_PKT_STATUS(pkt), i); 29627 mutex_enter(SD_MUTEX(un)); 29628 sd_reset_target(un, pkt); 29629 mutex_exit(SD_MUTEX(un)); 29630 } 29631 29632 /* 29633 * If we are not getting anywhere with lun/target resets, 29634 * let's reset the bus. 29635 */ 29636 if (i > SD_NDUMP_RETRIES/2) { 29637 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29638 (void) sd_send_polled_RQS(un); 29639 } 29640 29641 } 29642 scsi_destroy_pkt(pkt); 29643 29644 if (err != 0) { 29645 scsi_free_consistent_buf(bp); 29646 *bpp = NULL; 29647 } else { 29648 *bpp = bp; 29649 } 29650 29651 done: 29652 mutex_enter(SD_MUTEX(un)); 29653 return (err); 29654 } 29655 29656 29657 /* 29658 * Function: sd_failfast_flushq 29659 * 29660 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29661 * in b_flags and move them onto the failfast queue, then kick 29662 * off a thread to return all bp's on the failfast queue to 29663 * their owners with an error set. 29664 * 29665 * Arguments: un - pointer to the soft state struct for the instance. 29666 * 29667 * Context: may execute in interrupt context. 29668 */ 29669 29670 static void 29671 sd_failfast_flushq(struct sd_lun *un) 29672 { 29673 struct buf *bp; 29674 struct buf *next_waitq_bp; 29675 struct buf *prev_waitq_bp = NULL; 29676 29677 ASSERT(un != NULL); 29678 ASSERT(mutex_owned(SD_MUTEX(un))); 29679 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29680 ASSERT(un->un_failfast_bp == NULL); 29681 29682 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29683 "sd_failfast_flushq: entry: un:0x%p\n", un); 29684 29685 /* 29686 * Check if we should flush all bufs when entering failfast state, or 29687 * just those with B_FAILFAST set. 29688 */ 29689 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29690 /* 29691 * Move *all* bp's on the wait queue to the failfast flush 29692 * queue, including those that do NOT have B_FAILFAST set. 29693 */ 29694 if (un->un_failfast_headp == NULL) { 29695 ASSERT(un->un_failfast_tailp == NULL); 29696 un->un_failfast_headp = un->un_waitq_headp; 29697 } else { 29698 ASSERT(un->un_failfast_tailp != NULL); 29699 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29700 } 29701 29702 un->un_failfast_tailp = un->un_waitq_tailp; 29703 29704 /* update kstat for each bp moved out of the waitq */ 29705 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29706 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29707 } 29708 29709 /* empty the waitq */ 29710 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29711 29712 } else { 29713 /* 29714 * Go thru the wait queue, pick off all entries with 29715 * B_FAILFAST set, and move these onto the failfast queue. 29716 */ 29717 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29718 /* 29719 * Save the pointer to the next bp on the wait queue, 29720 * so we get to it on the next iteration of this loop. 29721 */ 29722 next_waitq_bp = bp->av_forw; 29723 29724 /* 29725 * If this bp from the wait queue does NOT have 29726 * B_FAILFAST set, just move on to the next element 29727 * in the wait queue. Note, this is the only place 29728 * where it is correct to set prev_waitq_bp. 29729 */ 29730 if ((bp->b_flags & B_FAILFAST) == 0) { 29731 prev_waitq_bp = bp; 29732 continue; 29733 } 29734 29735 /* 29736 * Remove the bp from the wait queue. 29737 */ 29738 if (bp == un->un_waitq_headp) { 29739 /* The bp is the first element of the waitq. */ 29740 un->un_waitq_headp = next_waitq_bp; 29741 if (un->un_waitq_headp == NULL) { 29742 /* The wait queue is now empty */ 29743 un->un_waitq_tailp = NULL; 29744 } 29745 } else { 29746 /* 29747 * The bp is either somewhere in the middle 29748 * or at the end of the wait queue. 29749 */ 29750 ASSERT(un->un_waitq_headp != NULL); 29751 ASSERT(prev_waitq_bp != NULL); 29752 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29753 == 0); 29754 if (bp == un->un_waitq_tailp) { 29755 /* bp is the last entry on the waitq. */ 29756 ASSERT(next_waitq_bp == NULL); 29757 un->un_waitq_tailp = prev_waitq_bp; 29758 } 29759 prev_waitq_bp->av_forw = next_waitq_bp; 29760 } 29761 bp->av_forw = NULL; 29762 29763 /* 29764 * update kstat since the bp is moved out of 29765 * the waitq 29766 */ 29767 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29768 29769 /* 29770 * Now put the bp onto the failfast queue. 29771 */ 29772 if (un->un_failfast_headp == NULL) { 29773 /* failfast queue is currently empty */ 29774 ASSERT(un->un_failfast_tailp == NULL); 29775 un->un_failfast_headp = 29776 un->un_failfast_tailp = bp; 29777 } else { 29778 /* Add the bp to the end of the failfast q */ 29779 ASSERT(un->un_failfast_tailp != NULL); 29780 ASSERT(un->un_failfast_tailp->b_flags & 29781 B_FAILFAST); 29782 un->un_failfast_tailp->av_forw = bp; 29783 un->un_failfast_tailp = bp; 29784 } 29785 } 29786 } 29787 29788 /* 29789 * Now return all bp's on the failfast queue to their owners. 29790 */ 29791 while ((bp = un->un_failfast_headp) != NULL) { 29792 29793 un->un_failfast_headp = bp->av_forw; 29794 if (un->un_failfast_headp == NULL) { 29795 un->un_failfast_tailp = NULL; 29796 } 29797 29798 /* 29799 * We want to return the bp with a failure error code, but 29800 * we do not want a call to sd_start_cmds() to occur here, 29801 * so use sd_return_failed_command_no_restart() instead of 29802 * sd_return_failed_command(). 29803 */ 29804 sd_return_failed_command_no_restart(un, bp, EIO); 29805 } 29806 29807 /* Flush the xbuf queues if required. */ 29808 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29809 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29810 } 29811 29812 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29813 "sd_failfast_flushq: exit: un:0x%p\n", un); 29814 } 29815 29816 29817 /* 29818 * Function: sd_failfast_flushq_callback 29819 * 29820 * Description: Return TRUE if the given bp meets the criteria for failfast 29821 * flushing. Used with ddi_xbuf_flushq(9F). 29822 * 29823 * Arguments: bp - ptr to buf struct to be examined. 29824 * 29825 * Context: Any 29826 */ 29827 29828 static int 29829 sd_failfast_flushq_callback(struct buf *bp) 29830 { 29831 /* 29832 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29833 * state is entered; OR (2) the given bp has B_FAILFAST set. 29834 */ 29835 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29836 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29837 } 29838 29839 29840 29841 #if defined(__i386) || defined(__amd64) 29842 /* 29843 * Function: sd_setup_next_xfer 29844 * 29845 * Description: Prepare next I/O operation using DMA_PARTIAL 29846 * 29847 */ 29848 29849 static int 29850 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29851 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29852 { 29853 ssize_t num_blks_not_xfered; 29854 daddr_t strt_blk_num; 29855 ssize_t bytes_not_xfered; 29856 int rval; 29857 29858 ASSERT(pkt->pkt_resid == 0); 29859 29860 /* 29861 * Calculate next block number and amount to be transferred. 29862 * 29863 * How much data NOT transfered to the HBA yet. 29864 */ 29865 bytes_not_xfered = xp->xb_dma_resid; 29866 29867 /* 29868 * figure how many blocks NOT transfered to the HBA yet. 29869 */ 29870 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29871 29872 /* 29873 * set starting block number to the end of what WAS transfered. 29874 */ 29875 strt_blk_num = xp->xb_blkno + 29876 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29877 29878 /* 29879 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29880 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29881 * the disk mutex here. 29882 */ 29883 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29884 strt_blk_num, num_blks_not_xfered); 29885 29886 if (rval == 0) { 29887 29888 /* 29889 * Success. 29890 * 29891 * Adjust things if there are still more blocks to be 29892 * transfered. 29893 */ 29894 xp->xb_dma_resid = pkt->pkt_resid; 29895 pkt->pkt_resid = 0; 29896 29897 return (1); 29898 } 29899 29900 /* 29901 * There's really only one possible return value from 29902 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29903 * returns NULL. 29904 */ 29905 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29906 29907 bp->b_resid = bp->b_bcount; 29908 bp->b_flags |= B_ERROR; 29909 29910 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29911 "Error setting up next portion of DMA transfer\n"); 29912 29913 return (0); 29914 } 29915 #endif 29916 29917 /* 29918 * Function: sd_panic_for_res_conflict 29919 * 29920 * Description: Call panic with a string formated with "Reservation Conflict" 29921 * and a human readable identifier indicating the SD instance 29922 * that experienced the reservation conflict. 29923 * 29924 * Arguments: un - pointer to the soft state struct for the instance. 29925 * 29926 * Context: may execute in interrupt context. 29927 */ 29928 29929 #define SD_RESV_CONFLICT_FMT_LEN 40 29930 void 29931 sd_panic_for_res_conflict(struct sd_lun *un) 29932 { 29933 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 29934 char path_str[MAXPATHLEN]; 29935 29936 (void) snprintf(panic_str, sizeof (panic_str), 29937 "Reservation Conflict\nDisk: %s", 29938 ddi_pathname(SD_DEVINFO(un), path_str)); 29939 29940 panic(panic_str); 29941 } 29942 29943 /* 29944 * Note: The following sd_faultinjection_ioctl( ) routines implement 29945 * driver support for handling fault injection for error analysis 29946 * causing faults in multiple layers of the driver. 29947 * 29948 */ 29949 29950 #ifdef SD_FAULT_INJECTION 29951 static uint_t sd_fault_injection_on = 0; 29952 29953 /* 29954 * Function: sd_faultinjection_ioctl() 29955 * 29956 * Description: This routine is the driver entry point for handling 29957 * faultinjection ioctls to inject errors into the 29958 * layer model 29959 * 29960 * Arguments: cmd - the ioctl cmd recieved 29961 * arg - the arguments from user and returns 29962 */ 29963 29964 static void 29965 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 29966 29967 uint_t i; 29968 uint_t rval; 29969 29970 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 29971 29972 mutex_enter(SD_MUTEX(un)); 29973 29974 switch (cmd) { 29975 case SDIOCRUN: 29976 /* Allow pushed faults to be injected */ 29977 SD_INFO(SD_LOG_SDTEST, un, 29978 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 29979 29980 sd_fault_injection_on = 1; 29981 29982 SD_INFO(SD_LOG_IOERR, un, 29983 "sd_faultinjection_ioctl: run finished\n"); 29984 break; 29985 29986 case SDIOCSTART: 29987 /* Start Injection Session */ 29988 SD_INFO(SD_LOG_SDTEST, un, 29989 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 29990 29991 sd_fault_injection_on = 0; 29992 un->sd_injection_mask = 0xFFFFFFFF; 29993 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 29994 un->sd_fi_fifo_pkt[i] = NULL; 29995 un->sd_fi_fifo_xb[i] = NULL; 29996 un->sd_fi_fifo_un[i] = NULL; 29997 un->sd_fi_fifo_arq[i] = NULL; 29998 } 29999 un->sd_fi_fifo_start = 0; 30000 un->sd_fi_fifo_end = 0; 30001 30002 mutex_enter(&(un->un_fi_mutex)); 30003 un->sd_fi_log[0] = '\0'; 30004 un->sd_fi_buf_len = 0; 30005 mutex_exit(&(un->un_fi_mutex)); 30006 30007 SD_INFO(SD_LOG_IOERR, un, 30008 "sd_faultinjection_ioctl: start finished\n"); 30009 break; 30010 30011 case SDIOCSTOP: 30012 /* Stop Injection Session */ 30013 SD_INFO(SD_LOG_SDTEST, un, 30014 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30015 sd_fault_injection_on = 0; 30016 un->sd_injection_mask = 0x0; 30017 30018 /* Empty stray or unuseds structs from fifo */ 30019 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30020 if (un->sd_fi_fifo_pkt[i] != NULL) { 30021 kmem_free(un->sd_fi_fifo_pkt[i], 30022 sizeof (struct sd_fi_pkt)); 30023 } 30024 if (un->sd_fi_fifo_xb[i] != NULL) { 30025 kmem_free(un->sd_fi_fifo_xb[i], 30026 sizeof (struct sd_fi_xb)); 30027 } 30028 if (un->sd_fi_fifo_un[i] != NULL) { 30029 kmem_free(un->sd_fi_fifo_un[i], 30030 sizeof (struct sd_fi_un)); 30031 } 30032 if (un->sd_fi_fifo_arq[i] != NULL) { 30033 kmem_free(un->sd_fi_fifo_arq[i], 30034 sizeof (struct sd_fi_arq)); 30035 } 30036 un->sd_fi_fifo_pkt[i] = NULL; 30037 un->sd_fi_fifo_un[i] = NULL; 30038 un->sd_fi_fifo_xb[i] = NULL; 30039 un->sd_fi_fifo_arq[i] = NULL; 30040 } 30041 un->sd_fi_fifo_start = 0; 30042 un->sd_fi_fifo_end = 0; 30043 30044 SD_INFO(SD_LOG_IOERR, un, 30045 "sd_faultinjection_ioctl: stop finished\n"); 30046 break; 30047 30048 case SDIOCINSERTPKT: 30049 /* Store a packet struct to be pushed onto fifo */ 30050 SD_INFO(SD_LOG_SDTEST, un, 30051 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30052 30053 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30054 30055 sd_fault_injection_on = 0; 30056 30057 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30058 if (un->sd_fi_fifo_pkt[i] != NULL) { 30059 kmem_free(un->sd_fi_fifo_pkt[i], 30060 sizeof (struct sd_fi_pkt)); 30061 } 30062 if (arg != NULL) { 30063 un->sd_fi_fifo_pkt[i] = 30064 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30065 if (un->sd_fi_fifo_pkt[i] == NULL) { 30066 /* Alloc failed don't store anything */ 30067 break; 30068 } 30069 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30070 sizeof (struct sd_fi_pkt), 0); 30071 if (rval == -1) { 30072 kmem_free(un->sd_fi_fifo_pkt[i], 30073 sizeof (struct sd_fi_pkt)); 30074 un->sd_fi_fifo_pkt[i] = NULL; 30075 } 30076 } else { 30077 SD_INFO(SD_LOG_IOERR, un, 30078 "sd_faultinjection_ioctl: pkt null\n"); 30079 } 30080 break; 30081 30082 case SDIOCINSERTXB: 30083 /* Store a xb struct to be pushed onto fifo */ 30084 SD_INFO(SD_LOG_SDTEST, un, 30085 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30086 30087 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30088 30089 sd_fault_injection_on = 0; 30090 30091 if (un->sd_fi_fifo_xb[i] != NULL) { 30092 kmem_free(un->sd_fi_fifo_xb[i], 30093 sizeof (struct sd_fi_xb)); 30094 un->sd_fi_fifo_xb[i] = NULL; 30095 } 30096 if (arg != NULL) { 30097 un->sd_fi_fifo_xb[i] = 30098 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30099 if (un->sd_fi_fifo_xb[i] == NULL) { 30100 /* Alloc failed don't store anything */ 30101 break; 30102 } 30103 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30104 sizeof (struct sd_fi_xb), 0); 30105 30106 if (rval == -1) { 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 } else { 30112 SD_INFO(SD_LOG_IOERR, un, 30113 "sd_faultinjection_ioctl: xb null\n"); 30114 } 30115 break; 30116 30117 case SDIOCINSERTUN: 30118 /* Store a un struct to be pushed onto fifo */ 30119 SD_INFO(SD_LOG_SDTEST, un, 30120 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30121 30122 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30123 30124 sd_fault_injection_on = 0; 30125 30126 if (un->sd_fi_fifo_un[i] != NULL) { 30127 kmem_free(un->sd_fi_fifo_un[i], 30128 sizeof (struct sd_fi_un)); 30129 un->sd_fi_fifo_un[i] = NULL; 30130 } 30131 if (arg != NULL) { 30132 un->sd_fi_fifo_un[i] = 30133 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30134 if (un->sd_fi_fifo_un[i] == NULL) { 30135 /* Alloc failed don't store anything */ 30136 break; 30137 } 30138 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30139 sizeof (struct sd_fi_un), 0); 30140 if (rval == -1) { 30141 kmem_free(un->sd_fi_fifo_un[i], 30142 sizeof (struct sd_fi_un)); 30143 un->sd_fi_fifo_un[i] = NULL; 30144 } 30145 30146 } else { 30147 SD_INFO(SD_LOG_IOERR, un, 30148 "sd_faultinjection_ioctl: un null\n"); 30149 } 30150 30151 break; 30152 30153 case SDIOCINSERTARQ: 30154 /* Store a arq struct to be pushed onto fifo */ 30155 SD_INFO(SD_LOG_SDTEST, un, 30156 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30157 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30158 30159 sd_fault_injection_on = 0; 30160 30161 if (un->sd_fi_fifo_arq[i] != NULL) { 30162 kmem_free(un->sd_fi_fifo_arq[i], 30163 sizeof (struct sd_fi_arq)); 30164 un->sd_fi_fifo_arq[i] = NULL; 30165 } 30166 if (arg != NULL) { 30167 un->sd_fi_fifo_arq[i] = 30168 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30169 if (un->sd_fi_fifo_arq[i] == NULL) { 30170 /* Alloc failed don't store anything */ 30171 break; 30172 } 30173 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30174 sizeof (struct sd_fi_arq), 0); 30175 if (rval == -1) { 30176 kmem_free(un->sd_fi_fifo_arq[i], 30177 sizeof (struct sd_fi_arq)); 30178 un->sd_fi_fifo_arq[i] = NULL; 30179 } 30180 30181 } else { 30182 SD_INFO(SD_LOG_IOERR, un, 30183 "sd_faultinjection_ioctl: arq null\n"); 30184 } 30185 30186 break; 30187 30188 case SDIOCPUSH: 30189 /* Push stored xb, pkt, un, and arq onto fifo */ 30190 sd_fault_injection_on = 0; 30191 30192 if (arg != NULL) { 30193 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30194 if (rval != -1 && 30195 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30196 un->sd_fi_fifo_end += i; 30197 } 30198 } else { 30199 SD_INFO(SD_LOG_IOERR, un, 30200 "sd_faultinjection_ioctl: push arg null\n"); 30201 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30202 un->sd_fi_fifo_end++; 30203 } 30204 } 30205 SD_INFO(SD_LOG_IOERR, un, 30206 "sd_faultinjection_ioctl: push to end=%d\n", 30207 un->sd_fi_fifo_end); 30208 break; 30209 30210 case SDIOCRETRIEVE: 30211 /* Return buffer of log from Injection session */ 30212 SD_INFO(SD_LOG_SDTEST, un, 30213 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30214 30215 sd_fault_injection_on = 0; 30216 30217 mutex_enter(&(un->un_fi_mutex)); 30218 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30219 un->sd_fi_buf_len+1, 0); 30220 mutex_exit(&(un->un_fi_mutex)); 30221 30222 if (rval == -1) { 30223 /* 30224 * arg is possibly invalid setting 30225 * it to NULL for return 30226 */ 30227 arg = NULL; 30228 } 30229 break; 30230 } 30231 30232 mutex_exit(SD_MUTEX(un)); 30233 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30234 " exit\n"); 30235 } 30236 30237 30238 /* 30239 * Function: sd_injection_log() 30240 * 30241 * Description: This routine adds buff to the already existing injection log 30242 * for retrieval via faultinjection_ioctl for use in fault 30243 * detection and recovery 30244 * 30245 * Arguments: buf - the string to add to the log 30246 */ 30247 30248 static void 30249 sd_injection_log(char *buf, struct sd_lun *un) 30250 { 30251 uint_t len; 30252 30253 ASSERT(un != NULL); 30254 ASSERT(buf != NULL); 30255 30256 mutex_enter(&(un->un_fi_mutex)); 30257 30258 len = min(strlen(buf), 255); 30259 /* Add logged value to Injection log to be returned later */ 30260 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30261 uint_t offset = strlen((char *)un->sd_fi_log); 30262 char *destp = (char *)un->sd_fi_log + offset; 30263 int i; 30264 for (i = 0; i < len; i++) { 30265 *destp++ = *buf++; 30266 } 30267 un->sd_fi_buf_len += len; 30268 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30269 } 30270 30271 mutex_exit(&(un->un_fi_mutex)); 30272 } 30273 30274 30275 /* 30276 * Function: sd_faultinjection() 30277 * 30278 * Description: This routine takes the pkt and changes its 30279 * content based on error injection scenerio. 30280 * 30281 * Arguments: pktp - packet to be changed 30282 */ 30283 30284 static void 30285 sd_faultinjection(struct scsi_pkt *pktp) 30286 { 30287 uint_t i; 30288 struct sd_fi_pkt *fi_pkt; 30289 struct sd_fi_xb *fi_xb; 30290 struct sd_fi_un *fi_un; 30291 struct sd_fi_arq *fi_arq; 30292 struct buf *bp; 30293 struct sd_xbuf *xb; 30294 struct sd_lun *un; 30295 30296 ASSERT(pktp != NULL); 30297 30298 /* pull bp xb and un from pktp */ 30299 bp = (struct buf *)pktp->pkt_private; 30300 xb = SD_GET_XBUF(bp); 30301 un = SD_GET_UN(bp); 30302 30303 ASSERT(un != NULL); 30304 30305 mutex_enter(SD_MUTEX(un)); 30306 30307 SD_TRACE(SD_LOG_SDTEST, un, 30308 "sd_faultinjection: entry Injection from sdintr\n"); 30309 30310 /* if injection is off return */ 30311 if (sd_fault_injection_on == 0 || 30312 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30313 mutex_exit(SD_MUTEX(un)); 30314 return; 30315 } 30316 30317 30318 /* take next set off fifo */ 30319 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30320 30321 fi_pkt = un->sd_fi_fifo_pkt[i]; 30322 fi_xb = un->sd_fi_fifo_xb[i]; 30323 fi_un = un->sd_fi_fifo_un[i]; 30324 fi_arq = un->sd_fi_fifo_arq[i]; 30325 30326 30327 /* set variables accordingly */ 30328 /* set pkt if it was on fifo */ 30329 if (fi_pkt != NULL) { 30330 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30331 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30332 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30333 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30334 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30335 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30336 30337 } 30338 30339 /* set xb if it was on fifo */ 30340 if (fi_xb != NULL) { 30341 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30342 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30343 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30344 SD_CONDSET(xb, xb, xb_victim_retry_count, 30345 "xb_victim_retry_count"); 30346 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30347 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30348 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30349 30350 /* copy in block data from sense */ 30351 if (fi_xb->xb_sense_data[0] != -1) { 30352 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30353 SENSE_LENGTH); 30354 } 30355 30356 /* copy in extended sense codes */ 30357 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 30358 "es_code"); 30359 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 30360 "es_key"); 30361 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 30362 "es_add_code"); 30363 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 30364 es_qual_code, "es_qual_code"); 30365 } 30366 30367 /* set un if it was on fifo */ 30368 if (fi_un != NULL) { 30369 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30370 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30371 SD_CONDSET(un, un, un_reset_retry_count, 30372 "un_reset_retry_count"); 30373 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30374 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30375 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30376 SD_CONDSET(un, un, un_f_geometry_is_valid, 30377 "un_f_geometry_is_valid"); 30378 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30379 "un_f_allow_bus_device_reset"); 30380 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30381 30382 } 30383 30384 /* copy in auto request sense if it was on fifo */ 30385 if (fi_arq != NULL) { 30386 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30387 } 30388 30389 /* free structs */ 30390 if (un->sd_fi_fifo_pkt[i] != NULL) { 30391 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30392 } 30393 if (un->sd_fi_fifo_xb[i] != NULL) { 30394 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30395 } 30396 if (un->sd_fi_fifo_un[i] != NULL) { 30397 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30398 } 30399 if (un->sd_fi_fifo_arq[i] != NULL) { 30400 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30401 } 30402 30403 /* 30404 * kmem_free does not gurantee to set to NULL 30405 * since we uses these to determine if we set 30406 * values or not lets confirm they are always 30407 * NULL after free 30408 */ 30409 un->sd_fi_fifo_pkt[i] = NULL; 30410 un->sd_fi_fifo_un[i] = NULL; 30411 un->sd_fi_fifo_xb[i] = NULL; 30412 un->sd_fi_fifo_arq[i] = NULL; 30413 30414 un->sd_fi_fifo_start++; 30415 30416 mutex_exit(SD_MUTEX(un)); 30417 30418 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30419 } 30420 30421 #endif /* SD_FAULT_INJECTION */ 30422