1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * SCSI disk target driver. 30 */ 31 #include <sys/scsi/scsi.h> 32 #include <sys/dkbad.h> 33 #include <sys/dklabel.h> 34 #include <sys/dkio.h> 35 #include <sys/fdio.h> 36 #include <sys/cdio.h> 37 #include <sys/mhd.h> 38 #include <sys/vtoc.h> 39 #include <sys/dktp/fdisk.h> 40 #include <sys/kstat.h> 41 #include <sys/vtrace.h> 42 #include <sys/note.h> 43 #include <sys/thread.h> 44 #include <sys/proc.h> 45 #include <sys/efi_partition.h> 46 #include <sys/var.h> 47 #include <sys/aio_req.h> 48 49 #ifdef __lock_lint 50 #define _LP64 51 #define __amd64 52 #endif 53 54 #if (defined(__fibre)) 55 /* Note: is there a leadville version of the following? */ 56 #include <sys/fc4/fcal_linkapp.h> 57 #endif 58 #include <sys/taskq.h> 59 #include <sys/uuid.h> 60 #include <sys/byteorder.h> 61 #include <sys/sdt.h> 62 63 #include "sd_xbuf.h" 64 65 #include <sys/scsi/targets/sddef.h> 66 #include <sys/cmlb.h> 67 #include <sys/sysevent/eventdefs.h> 68 #include <sys/sysevent/dev.h> 69 70 71 /* 72 * Loadable module info. 73 */ 74 #if (defined(__fibre)) 75 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 76 char _depends_on[] = "misc/scsi misc/cmlb drv/fcp"; 77 #else 78 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 79 char _depends_on[] = "misc/scsi misc/cmlb"; 80 #endif 81 82 /* 83 * Define the interconnect type, to allow the driver to distinguish 84 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 85 * 86 * This is really for backward compatibility. In the future, the driver 87 * should actually check the "interconnect-type" property as reported by 88 * the HBA; however at present this property is not defined by all HBAs, 89 * so we will use this #define (1) to permit the driver to run in 90 * backward-compatibility mode; and (2) to print a notification message 91 * if an FC HBA does not support the "interconnect-type" property. The 92 * behavior of the driver will be to assume parallel SCSI behaviors unless 93 * the "interconnect-type" property is defined by the HBA **AND** has a 94 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 95 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 96 * Channel behaviors (as per the old ssd). (Note that the 97 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 98 * will result in the driver assuming parallel SCSI behaviors.) 99 * 100 * (see common/sys/scsi/impl/services.h) 101 * 102 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 103 * since some FC HBAs may already support that, and there is some code in 104 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 105 * default would confuse that code, and besides things should work fine 106 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 107 * "interconnect_type" property. 108 * 109 */ 110 #if (defined(__fibre)) 111 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 112 #else 113 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 114 #endif 115 116 /* 117 * The name of the driver, established from the module name in _init. 118 */ 119 static char *sd_label = NULL; 120 121 /* 122 * Driver name is unfortunately prefixed on some driver.conf properties. 123 */ 124 #if (defined(__fibre)) 125 #define sd_max_xfer_size ssd_max_xfer_size 126 #define sd_config_list ssd_config_list 127 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 128 static char *sd_config_list = "ssd-config-list"; 129 #else 130 static char *sd_max_xfer_size = "sd_max_xfer_size"; 131 static char *sd_config_list = "sd-config-list"; 132 #endif 133 134 /* 135 * Driver global variables 136 */ 137 138 #if (defined(__fibre)) 139 /* 140 * These #defines are to avoid namespace collisions that occur because this 141 * code is currently used to compile two separate driver modules: sd and ssd. 142 * All global variables need to be treated this way (even if declared static) 143 * in order to allow the debugger to resolve the names properly. 144 * It is anticipated that in the near future the ssd module will be obsoleted, 145 * at which time this namespace issue should go away. 146 */ 147 #define sd_state ssd_state 148 #define sd_io_time ssd_io_time 149 #define sd_failfast_enable ssd_failfast_enable 150 #define sd_ua_retry_count ssd_ua_retry_count 151 #define sd_report_pfa ssd_report_pfa 152 #define sd_max_throttle ssd_max_throttle 153 #define sd_min_throttle ssd_min_throttle 154 #define sd_rot_delay ssd_rot_delay 155 156 #define sd_retry_on_reservation_conflict \ 157 ssd_retry_on_reservation_conflict 158 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 159 #define sd_resv_conflict_name ssd_resv_conflict_name 160 161 #define sd_component_mask ssd_component_mask 162 #define sd_level_mask ssd_level_mask 163 #define sd_debug_un ssd_debug_un 164 #define sd_error_level ssd_error_level 165 166 #define sd_xbuf_active_limit ssd_xbuf_active_limit 167 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 168 169 #define sd_tr ssd_tr 170 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 171 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 172 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 173 #define sd_check_media_time ssd_check_media_time 174 #define sd_wait_cmds_complete ssd_wait_cmds_complete 175 #define sd_label_mutex ssd_label_mutex 176 #define sd_detach_mutex ssd_detach_mutex 177 #define sd_log_buf ssd_log_buf 178 #define sd_log_mutex ssd_log_mutex 179 180 #define sd_disk_table ssd_disk_table 181 #define sd_disk_table_size ssd_disk_table_size 182 #define sd_sense_mutex ssd_sense_mutex 183 #define sd_cdbtab ssd_cdbtab 184 185 #define sd_cb_ops ssd_cb_ops 186 #define sd_ops ssd_ops 187 #define sd_additional_codes ssd_additional_codes 188 #define sd_tgops ssd_tgops 189 190 #define sd_minor_data ssd_minor_data 191 #define sd_minor_data_efi ssd_minor_data_efi 192 193 #define sd_tq ssd_tq 194 #define sd_wmr_tq ssd_wmr_tq 195 #define sd_taskq_name ssd_taskq_name 196 #define sd_wmr_taskq_name ssd_wmr_taskq_name 197 #define sd_taskq_minalloc ssd_taskq_minalloc 198 #define sd_taskq_maxalloc ssd_taskq_maxalloc 199 200 #define sd_dump_format_string ssd_dump_format_string 201 202 #define sd_iostart_chain ssd_iostart_chain 203 #define sd_iodone_chain ssd_iodone_chain 204 205 #define sd_pm_idletime ssd_pm_idletime 206 207 #define sd_force_pm_supported ssd_force_pm_supported 208 209 #define sd_dtype_optical_bind ssd_dtype_optical_bind 210 211 #endif 212 213 214 #ifdef SDDEBUG 215 int sd_force_pm_supported = 0; 216 #endif /* SDDEBUG */ 217 218 void *sd_state = NULL; 219 int sd_io_time = SD_IO_TIME; 220 int sd_failfast_enable = 1; 221 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 222 int sd_report_pfa = 1; 223 int sd_max_throttle = SD_MAX_THROTTLE; 224 int sd_min_throttle = SD_MIN_THROTTLE; 225 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 226 int sd_qfull_throttle_enable = TRUE; 227 228 int sd_retry_on_reservation_conflict = 1; 229 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 230 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 231 232 static int sd_dtype_optical_bind = -1; 233 234 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 235 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 236 237 /* 238 * Global data for debug logging. To enable debug printing, sd_component_mask 239 * and sd_level_mask should be set to the desired bit patterns as outlined in 240 * sddef.h. 241 */ 242 uint_t sd_component_mask = 0x0; 243 uint_t sd_level_mask = 0x0; 244 struct sd_lun *sd_debug_un = NULL; 245 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 246 247 /* Note: these may go away in the future... */ 248 static uint32_t sd_xbuf_active_limit = 512; 249 static uint32_t sd_xbuf_reserve_limit = 16; 250 251 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 252 253 /* 254 * Timer value used to reset the throttle after it has been reduced 255 * (typically in response to TRAN_BUSY or STATUS_QFULL) 256 */ 257 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 258 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 259 260 /* 261 * Interval value associated with the media change scsi watch. 262 */ 263 static int sd_check_media_time = 3000000; 264 265 /* 266 * Wait value used for in progress operations during a DDI_SUSPEND 267 */ 268 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 269 270 /* 271 * sd_label_mutex protects a static buffer used in the disk label 272 * component of the driver 273 */ 274 static kmutex_t sd_label_mutex; 275 276 /* 277 * sd_detach_mutex protects un_layer_count, un_detach_count, and 278 * un_opens_in_progress in the sd_lun structure. 279 */ 280 static kmutex_t sd_detach_mutex; 281 282 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 283 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 284 285 /* 286 * Global buffer and mutex for debug logging 287 */ 288 static char sd_log_buf[1024]; 289 static kmutex_t sd_log_mutex; 290 291 /* 292 * Structs and globals for recording attached lun information. 293 * This maintains a chain. Each node in the chain represents a SCSI controller. 294 * The structure records the number of luns attached to each target connected 295 * with the controller. 296 * For parallel scsi device only. 297 */ 298 struct sd_scsi_hba_tgt_lun { 299 struct sd_scsi_hba_tgt_lun *next; 300 dev_info_t *pdip; 301 int nlun[NTARGETS_WIDE]; 302 }; 303 304 /* 305 * Flag to indicate the lun is attached or detached 306 */ 307 #define SD_SCSI_LUN_ATTACH 0 308 #define SD_SCSI_LUN_DETACH 1 309 310 static kmutex_t sd_scsi_target_lun_mutex; 311 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 312 313 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 314 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 315 316 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 317 sd_scsi_target_lun_head)) 318 319 /* 320 * "Smart" Probe Caching structs, globals, #defines, etc. 321 * For parallel scsi and non-self-identify device only. 322 */ 323 324 /* 325 * The following resources and routines are implemented to support 326 * "smart" probing, which caches the scsi_probe() results in an array, 327 * in order to help avoid long probe times. 328 */ 329 struct sd_scsi_probe_cache { 330 struct sd_scsi_probe_cache *next; 331 dev_info_t *pdip; 332 int cache[NTARGETS_WIDE]; 333 }; 334 335 static kmutex_t sd_scsi_probe_cache_mutex; 336 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 337 338 /* 339 * Really we only need protection on the head of the linked list, but 340 * better safe than sorry. 341 */ 342 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 343 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 344 345 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 346 sd_scsi_probe_cache_head)) 347 348 349 /* 350 * Vendor specific data name property declarations 351 */ 352 353 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 354 355 static sd_tunables seagate_properties = { 356 SEAGATE_THROTTLE_VALUE, 357 0, 358 0, 359 0, 360 0, 361 0, 362 0, 363 0, 364 0 365 }; 366 367 368 static sd_tunables fujitsu_properties = { 369 FUJITSU_THROTTLE_VALUE, 370 0, 371 0, 372 0, 373 0, 374 0, 375 0, 376 0, 377 0 378 }; 379 380 static sd_tunables ibm_properties = { 381 IBM_THROTTLE_VALUE, 382 0, 383 0, 384 0, 385 0, 386 0, 387 0, 388 0, 389 0 390 }; 391 392 static sd_tunables purple_properties = { 393 PURPLE_THROTTLE_VALUE, 394 0, 395 0, 396 PURPLE_BUSY_RETRIES, 397 PURPLE_RESET_RETRY_COUNT, 398 PURPLE_RESERVE_RELEASE_TIME, 399 0, 400 0, 401 0 402 }; 403 404 static sd_tunables sve_properties = { 405 SVE_THROTTLE_VALUE, 406 0, 407 0, 408 SVE_BUSY_RETRIES, 409 SVE_RESET_RETRY_COUNT, 410 SVE_RESERVE_RELEASE_TIME, 411 SVE_MIN_THROTTLE_VALUE, 412 SVE_DISKSORT_DISABLED_FLAG, 413 0 414 }; 415 416 static sd_tunables maserati_properties = { 417 0, 418 0, 419 0, 420 0, 421 0, 422 0, 423 0, 424 MASERATI_DISKSORT_DISABLED_FLAG, 425 MASERATI_LUN_RESET_ENABLED_FLAG 426 }; 427 428 static sd_tunables pirus_properties = { 429 PIRUS_THROTTLE_VALUE, 430 0, 431 PIRUS_NRR_COUNT, 432 PIRUS_BUSY_RETRIES, 433 PIRUS_RESET_RETRY_COUNT, 434 0, 435 PIRUS_MIN_THROTTLE_VALUE, 436 PIRUS_DISKSORT_DISABLED_FLAG, 437 PIRUS_LUN_RESET_ENABLED_FLAG 438 }; 439 440 #endif 441 442 #if (defined(__sparc) && !defined(__fibre)) || \ 443 (defined(__i386) || defined(__amd64)) 444 445 446 static sd_tunables elite_properties = { 447 ELITE_THROTTLE_VALUE, 448 0, 449 0, 450 0, 451 0, 452 0, 453 0, 454 0, 455 0 456 }; 457 458 static sd_tunables st31200n_properties = { 459 ST31200N_THROTTLE_VALUE, 460 0, 461 0, 462 0, 463 0, 464 0, 465 0, 466 0, 467 0 468 }; 469 470 #endif /* Fibre or not */ 471 472 static sd_tunables lsi_properties_scsi = { 473 LSI_THROTTLE_VALUE, 474 0, 475 LSI_NOTREADY_RETRIES, 476 0, 477 0, 478 0, 479 0, 480 0, 481 0 482 }; 483 484 static sd_tunables symbios_properties = { 485 SYMBIOS_THROTTLE_VALUE, 486 0, 487 SYMBIOS_NOTREADY_RETRIES, 488 0, 489 0, 490 0, 491 0, 492 0, 493 0 494 }; 495 496 static sd_tunables lsi_properties = { 497 0, 498 0, 499 LSI_NOTREADY_RETRIES, 500 0, 501 0, 502 0, 503 0, 504 0, 505 0 506 }; 507 508 static sd_tunables lsi_oem_properties = { 509 0, 510 0, 511 LSI_OEM_NOTREADY_RETRIES, 512 0, 513 0, 514 0, 515 0, 516 0, 517 0, 518 1 519 }; 520 521 522 523 #if (defined(SD_PROP_TST)) 524 525 #define SD_TST_CTYPE_VAL CTYPE_CDROM 526 #define SD_TST_THROTTLE_VAL 16 527 #define SD_TST_NOTREADY_VAL 12 528 #define SD_TST_BUSY_VAL 60 529 #define SD_TST_RST_RETRY_VAL 36 530 #define SD_TST_RSV_REL_TIME 60 531 532 static sd_tunables tst_properties = { 533 SD_TST_THROTTLE_VAL, 534 SD_TST_CTYPE_VAL, 535 SD_TST_NOTREADY_VAL, 536 SD_TST_BUSY_VAL, 537 SD_TST_RST_RETRY_VAL, 538 SD_TST_RSV_REL_TIME, 539 0, 540 0, 541 0 542 }; 543 #endif 544 545 /* This is similar to the ANSI toupper implementation */ 546 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 547 548 /* 549 * Static Driver Configuration Table 550 * 551 * This is the table of disks which need throttle adjustment (or, perhaps 552 * something else as defined by the flags at a future time.) device_id 553 * is a string consisting of concatenated vid (vendor), pid (product/model) 554 * and revision strings as defined in the scsi_inquiry structure. Offsets of 555 * the parts of the string are as defined by the sizes in the scsi_inquiry 556 * structure. Device type is searched as far as the device_id string is 557 * defined. Flags defines which values are to be set in the driver from the 558 * properties list. 559 * 560 * Entries below which begin and end with a "*" are a special case. 561 * These do not have a specific vendor, and the string which follows 562 * can appear anywhere in the 16 byte PID portion of the inquiry data. 563 * 564 * Entries below which begin and end with a " " (blank) are a special 565 * case. The comparison function will treat multiple consecutive blanks 566 * as equivalent to a single blank. For example, this causes a 567 * sd_disk_table entry of " NEC CDROM " to match a device's id string 568 * of "NEC CDROM". 569 * 570 * Note: The MD21 controller type has been obsoleted. 571 * ST318202F is a Legacy device 572 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 573 * made with an FC connection. The entries here are a legacy. 574 */ 575 static sd_disk_config_t sd_disk_table[] = { 576 #if defined(__fibre) || defined(__i386) || defined(__amd64) 577 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 578 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 579 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 580 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 581 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 582 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 583 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 584 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 585 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 586 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 587 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 588 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 589 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 590 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 591 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 592 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 593 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 594 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 595 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 596 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 597 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 598 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 599 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 600 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 601 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 602 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 603 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 604 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 605 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 606 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 607 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 608 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 609 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 610 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 611 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 612 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 613 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 614 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 615 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 616 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 617 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 618 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 619 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 620 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 621 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 622 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 623 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 624 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 625 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 626 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 627 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 628 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 629 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 630 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 631 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 632 { "SUN T3", SD_CONF_BSET_THROTTLE | 633 SD_CONF_BSET_BSY_RETRY_COUNT| 634 SD_CONF_BSET_RST_RETRIES| 635 SD_CONF_BSET_RSV_REL_TIME, 636 &purple_properties }, 637 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 638 SD_CONF_BSET_BSY_RETRY_COUNT| 639 SD_CONF_BSET_RST_RETRIES| 640 SD_CONF_BSET_RSV_REL_TIME| 641 SD_CONF_BSET_MIN_THROTTLE| 642 SD_CONF_BSET_DISKSORT_DISABLED, 643 &sve_properties }, 644 { "SUN T4", SD_CONF_BSET_THROTTLE | 645 SD_CONF_BSET_BSY_RETRY_COUNT| 646 SD_CONF_BSET_RST_RETRIES| 647 SD_CONF_BSET_RSV_REL_TIME, 648 &purple_properties }, 649 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 650 SD_CONF_BSET_LUN_RESET_ENABLED, 651 &maserati_properties }, 652 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 653 SD_CONF_BSET_NRR_COUNT| 654 SD_CONF_BSET_BSY_RETRY_COUNT| 655 SD_CONF_BSET_RST_RETRIES| 656 SD_CONF_BSET_MIN_THROTTLE| 657 SD_CONF_BSET_DISKSORT_DISABLED| 658 SD_CONF_BSET_LUN_RESET_ENABLED, 659 &pirus_properties }, 660 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 661 SD_CONF_BSET_NRR_COUNT| 662 SD_CONF_BSET_BSY_RETRY_COUNT| 663 SD_CONF_BSET_RST_RETRIES| 664 SD_CONF_BSET_MIN_THROTTLE| 665 SD_CONF_BSET_DISKSORT_DISABLED| 666 SD_CONF_BSET_LUN_RESET_ENABLED, 667 &pirus_properties }, 668 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 669 SD_CONF_BSET_NRR_COUNT| 670 SD_CONF_BSET_BSY_RETRY_COUNT| 671 SD_CONF_BSET_RST_RETRIES| 672 SD_CONF_BSET_MIN_THROTTLE| 673 SD_CONF_BSET_DISKSORT_DISABLED| 674 SD_CONF_BSET_LUN_RESET_ENABLED, 675 &pirus_properties }, 676 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 677 SD_CONF_BSET_NRR_COUNT| 678 SD_CONF_BSET_BSY_RETRY_COUNT| 679 SD_CONF_BSET_RST_RETRIES| 680 SD_CONF_BSET_MIN_THROTTLE| 681 SD_CONF_BSET_DISKSORT_DISABLED| 682 SD_CONF_BSET_LUN_RESET_ENABLED, 683 &pirus_properties }, 684 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 685 SD_CONF_BSET_NRR_COUNT| 686 SD_CONF_BSET_BSY_RETRY_COUNT| 687 SD_CONF_BSET_RST_RETRIES| 688 SD_CONF_BSET_MIN_THROTTLE| 689 SD_CONF_BSET_DISKSORT_DISABLED| 690 SD_CONF_BSET_LUN_RESET_ENABLED, 691 &pirus_properties }, 692 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 693 SD_CONF_BSET_NRR_COUNT| 694 SD_CONF_BSET_BSY_RETRY_COUNT| 695 SD_CONF_BSET_RST_RETRIES| 696 SD_CONF_BSET_MIN_THROTTLE| 697 SD_CONF_BSET_DISKSORT_DISABLED| 698 SD_CONF_BSET_LUN_RESET_ENABLED, 699 &pirus_properties }, 700 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 701 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 702 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 703 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 704 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 705 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 706 #endif /* fibre or NON-sparc platforms */ 707 #if ((defined(__sparc) && !defined(__fibre)) ||\ 708 (defined(__i386) || defined(__amd64))) 709 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 710 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 711 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 712 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 713 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 714 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 715 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 716 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 717 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 718 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 719 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 720 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 721 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 722 &symbios_properties }, 723 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 724 &lsi_properties_scsi }, 725 #if defined(__i386) || defined(__amd64) 726 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 727 | SD_CONF_BSET_READSUB_BCD 728 | SD_CONF_BSET_READ_TOC_ADDR_BCD 729 | SD_CONF_BSET_NO_READ_HEADER 730 | SD_CONF_BSET_READ_CD_XD4), NULL }, 731 732 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 733 | SD_CONF_BSET_READSUB_BCD 734 | SD_CONF_BSET_READ_TOC_ADDR_BCD 735 | SD_CONF_BSET_NO_READ_HEADER 736 | SD_CONF_BSET_READ_CD_XD4), NULL }, 737 #endif /* __i386 || __amd64 */ 738 #endif /* sparc NON-fibre or NON-sparc platforms */ 739 740 #if (defined(SD_PROP_TST)) 741 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 742 | SD_CONF_BSET_CTYPE 743 | SD_CONF_BSET_NRR_COUNT 744 | SD_CONF_BSET_FAB_DEVID 745 | SD_CONF_BSET_NOCACHE 746 | SD_CONF_BSET_BSY_RETRY_COUNT 747 | SD_CONF_BSET_PLAYMSF_BCD 748 | SD_CONF_BSET_READSUB_BCD 749 | SD_CONF_BSET_READ_TOC_TRK_BCD 750 | SD_CONF_BSET_READ_TOC_ADDR_BCD 751 | SD_CONF_BSET_NO_READ_HEADER 752 | SD_CONF_BSET_READ_CD_XD4 753 | SD_CONF_BSET_RST_RETRIES 754 | SD_CONF_BSET_RSV_REL_TIME 755 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 756 #endif 757 }; 758 759 static const int sd_disk_table_size = 760 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 761 762 763 764 #define SD_INTERCONNECT_PARALLEL 0 765 #define SD_INTERCONNECT_FABRIC 1 766 #define SD_INTERCONNECT_FIBRE 2 767 #define SD_INTERCONNECT_SSA 3 768 #define SD_INTERCONNECT_SATA 4 769 #define SD_IS_PARALLEL_SCSI(un) \ 770 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 771 #define SD_IS_SERIAL(un) \ 772 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 773 774 /* 775 * Definitions used by device id registration routines 776 */ 777 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 778 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 779 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 780 781 static kmutex_t sd_sense_mutex = {0}; 782 783 /* 784 * Macros for updates of the driver state 785 */ 786 #define New_state(un, s) \ 787 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 788 #define Restore_state(un) \ 789 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 790 791 static struct sd_cdbinfo sd_cdbtab[] = { 792 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 793 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 794 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 795 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 796 }; 797 798 /* 799 * Specifies the number of seconds that must have elapsed since the last 800 * cmd. has completed for a device to be declared idle to the PM framework. 801 */ 802 static int sd_pm_idletime = 1; 803 804 /* 805 * Internal function prototypes 806 */ 807 808 #if (defined(__fibre)) 809 /* 810 * These #defines are to avoid namespace collisions that occur because this 811 * code is currently used to compile two separate driver modules: sd and ssd. 812 * All function names need to be treated this way (even if declared static) 813 * in order to allow the debugger to resolve the names properly. 814 * It is anticipated that in the near future the ssd module will be obsoleted, 815 * at which time this ugliness should go away. 816 */ 817 #define sd_log_trace ssd_log_trace 818 #define sd_log_info ssd_log_info 819 #define sd_log_err ssd_log_err 820 #define sdprobe ssdprobe 821 #define sdinfo ssdinfo 822 #define sd_prop_op ssd_prop_op 823 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 824 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 825 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 826 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 827 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 828 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 829 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 830 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 831 #define sd_spin_up_unit ssd_spin_up_unit 832 #define sd_enable_descr_sense ssd_enable_descr_sense 833 #define sd_reenable_dsense_task ssd_reenable_dsense_task 834 #define sd_set_mmc_caps ssd_set_mmc_caps 835 #define sd_read_unit_properties ssd_read_unit_properties 836 #define sd_process_sdconf_file ssd_process_sdconf_file 837 #define sd_process_sdconf_table ssd_process_sdconf_table 838 #define sd_sdconf_id_match ssd_sdconf_id_match 839 #define sd_blank_cmp ssd_blank_cmp 840 #define sd_chk_vers1_data ssd_chk_vers1_data 841 #define sd_set_vers1_properties ssd_set_vers1_properties 842 843 #define sd_get_physical_geometry ssd_get_physical_geometry 844 #define sd_get_virtual_geometry ssd_get_virtual_geometry 845 #define sd_update_block_info ssd_update_block_info 846 #define sd_register_devid ssd_register_devid 847 #define sd_get_devid ssd_get_devid 848 #define sd_create_devid ssd_create_devid 849 #define sd_write_deviceid ssd_write_deviceid 850 #define sd_check_vpd_page_support ssd_check_vpd_page_support 851 #define sd_setup_pm ssd_setup_pm 852 #define sd_create_pm_components ssd_create_pm_components 853 #define sd_ddi_suspend ssd_ddi_suspend 854 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 855 #define sd_ddi_resume ssd_ddi_resume 856 #define sd_ddi_pm_resume ssd_ddi_pm_resume 857 #define sdpower ssdpower 858 #define sdattach ssdattach 859 #define sddetach ssddetach 860 #define sd_unit_attach ssd_unit_attach 861 #define sd_unit_detach ssd_unit_detach 862 #define sd_set_unit_attributes ssd_set_unit_attributes 863 #define sd_create_errstats ssd_create_errstats 864 #define sd_set_errstats ssd_set_errstats 865 #define sd_set_pstats ssd_set_pstats 866 #define sddump ssddump 867 #define sd_scsi_poll ssd_scsi_poll 868 #define sd_send_polled_RQS ssd_send_polled_RQS 869 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 870 #define sd_init_event_callbacks ssd_init_event_callbacks 871 #define sd_event_callback ssd_event_callback 872 #define sd_cache_control ssd_cache_control 873 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 874 #define sd_get_nv_sup ssd_get_nv_sup 875 #define sd_make_device ssd_make_device 876 #define sdopen ssdopen 877 #define sdclose ssdclose 878 #define sd_ready_and_valid ssd_ready_and_valid 879 #define sdmin ssdmin 880 #define sdread ssdread 881 #define sdwrite ssdwrite 882 #define sdaread ssdaread 883 #define sdawrite ssdawrite 884 #define sdstrategy ssdstrategy 885 #define sdioctl ssdioctl 886 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 887 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 888 #define sd_checksum_iostart ssd_checksum_iostart 889 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 890 #define sd_pm_iostart ssd_pm_iostart 891 #define sd_core_iostart ssd_core_iostart 892 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 893 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 894 #define sd_checksum_iodone ssd_checksum_iodone 895 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 896 #define sd_pm_iodone ssd_pm_iodone 897 #define sd_initpkt_for_buf ssd_initpkt_for_buf 898 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 899 #define sd_setup_rw_pkt ssd_setup_rw_pkt 900 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 901 #define sd_buf_iodone ssd_buf_iodone 902 #define sd_uscsi_strategy ssd_uscsi_strategy 903 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 904 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 905 #define sd_uscsi_iodone ssd_uscsi_iodone 906 #define sd_xbuf_strategy ssd_xbuf_strategy 907 #define sd_xbuf_init ssd_xbuf_init 908 #define sd_pm_entry ssd_pm_entry 909 #define sd_pm_exit ssd_pm_exit 910 911 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 912 #define sd_pm_timeout_handler ssd_pm_timeout_handler 913 914 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 915 #define sdintr ssdintr 916 #define sd_start_cmds ssd_start_cmds 917 #define sd_send_scsi_cmd ssd_send_scsi_cmd 918 #define sd_bioclone_alloc ssd_bioclone_alloc 919 #define sd_bioclone_free ssd_bioclone_free 920 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 921 #define sd_shadow_buf_free ssd_shadow_buf_free 922 #define sd_print_transport_rejected_message \ 923 ssd_print_transport_rejected_message 924 #define sd_retry_command ssd_retry_command 925 #define sd_set_retry_bp ssd_set_retry_bp 926 #define sd_send_request_sense_command ssd_send_request_sense_command 927 #define sd_start_retry_command ssd_start_retry_command 928 #define sd_start_direct_priority_command \ 929 ssd_start_direct_priority_command 930 #define sd_return_failed_command ssd_return_failed_command 931 #define sd_return_failed_command_no_restart \ 932 ssd_return_failed_command_no_restart 933 #define sd_return_command ssd_return_command 934 #define sd_sync_with_callback ssd_sync_with_callback 935 #define sdrunout ssdrunout 936 #define sd_mark_rqs_busy ssd_mark_rqs_busy 937 #define sd_mark_rqs_idle ssd_mark_rqs_idle 938 #define sd_reduce_throttle ssd_reduce_throttle 939 #define sd_restore_throttle ssd_restore_throttle 940 #define sd_print_incomplete_msg ssd_print_incomplete_msg 941 #define sd_init_cdb_limits ssd_init_cdb_limits 942 #define sd_pkt_status_good ssd_pkt_status_good 943 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 944 #define sd_pkt_status_busy ssd_pkt_status_busy 945 #define sd_pkt_status_reservation_conflict \ 946 ssd_pkt_status_reservation_conflict 947 #define sd_pkt_status_qfull ssd_pkt_status_qfull 948 #define sd_handle_request_sense ssd_handle_request_sense 949 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 950 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 951 #define sd_validate_sense_data ssd_validate_sense_data 952 #define sd_decode_sense ssd_decode_sense 953 #define sd_print_sense_msg ssd_print_sense_msg 954 #define sd_sense_key_no_sense ssd_sense_key_no_sense 955 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 956 #define sd_sense_key_not_ready ssd_sense_key_not_ready 957 #define sd_sense_key_medium_or_hardware_error \ 958 ssd_sense_key_medium_or_hardware_error 959 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 960 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 961 #define sd_sense_key_fail_command ssd_sense_key_fail_command 962 #define sd_sense_key_blank_check ssd_sense_key_blank_check 963 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 964 #define sd_sense_key_default ssd_sense_key_default 965 #define sd_print_retry_msg ssd_print_retry_msg 966 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 967 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 968 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 969 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 970 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 971 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 972 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 973 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 974 #define sd_pkt_reason_default ssd_pkt_reason_default 975 #define sd_reset_target ssd_reset_target 976 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 977 #define sd_start_stop_unit_task ssd_start_stop_unit_task 978 #define sd_taskq_create ssd_taskq_create 979 #define sd_taskq_delete ssd_taskq_delete 980 #define sd_target_change_task ssd_target_change_task 981 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event 982 #define sd_media_change_task ssd_media_change_task 983 #define sd_handle_mchange ssd_handle_mchange 984 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 985 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 986 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 987 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 988 #define sd_send_scsi_feature_GET_CONFIGURATION \ 989 sd_send_scsi_feature_GET_CONFIGURATION 990 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 991 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 992 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 993 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 994 ssd_send_scsi_PERSISTENT_RESERVE_IN 995 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 996 ssd_send_scsi_PERSISTENT_RESERVE_OUT 997 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 998 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 999 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1000 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1001 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1002 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1003 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1004 #define sd_alloc_rqs ssd_alloc_rqs 1005 #define sd_free_rqs ssd_free_rqs 1006 #define sd_dump_memory ssd_dump_memory 1007 #define sd_get_media_info ssd_get_media_info 1008 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1009 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1010 #define sd_setup_next_xfer ssd_setup_next_xfer 1011 #define sd_dkio_get_temp ssd_dkio_get_temp 1012 #define sd_check_mhd ssd_check_mhd 1013 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1014 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1015 #define sd_sname ssd_sname 1016 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1017 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1018 #define sd_take_ownership ssd_take_ownership 1019 #define sd_reserve_release ssd_reserve_release 1020 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1021 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1022 #define sd_persistent_reservation_in_read_keys \ 1023 ssd_persistent_reservation_in_read_keys 1024 #define sd_persistent_reservation_in_read_resv \ 1025 ssd_persistent_reservation_in_read_resv 1026 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1027 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1028 #define sd_mhdioc_release ssd_mhdioc_release 1029 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1030 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1031 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1032 #define sr_change_blkmode ssr_change_blkmode 1033 #define sr_change_speed ssr_change_speed 1034 #define sr_atapi_change_speed ssr_atapi_change_speed 1035 #define sr_pause_resume ssr_pause_resume 1036 #define sr_play_msf ssr_play_msf 1037 #define sr_play_trkind ssr_play_trkind 1038 #define sr_read_all_subcodes ssr_read_all_subcodes 1039 #define sr_read_subchannel ssr_read_subchannel 1040 #define sr_read_tocentry ssr_read_tocentry 1041 #define sr_read_tochdr ssr_read_tochdr 1042 #define sr_read_cdda ssr_read_cdda 1043 #define sr_read_cdxa ssr_read_cdxa 1044 #define sr_read_mode1 ssr_read_mode1 1045 #define sr_read_mode2 ssr_read_mode2 1046 #define sr_read_cd_mode2 ssr_read_cd_mode2 1047 #define sr_sector_mode ssr_sector_mode 1048 #define sr_eject ssr_eject 1049 #define sr_ejected ssr_ejected 1050 #define sr_check_wp ssr_check_wp 1051 #define sd_check_media ssd_check_media 1052 #define sd_media_watch_cb ssd_media_watch_cb 1053 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1054 #define sr_volume_ctrl ssr_volume_ctrl 1055 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1056 #define sd_log_page_supported ssd_log_page_supported 1057 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1058 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1059 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1060 #define sd_range_lock ssd_range_lock 1061 #define sd_get_range ssd_get_range 1062 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1063 #define sd_range_unlock ssd_range_unlock 1064 #define sd_read_modify_write_task ssd_read_modify_write_task 1065 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1066 1067 #define sd_iostart_chain ssd_iostart_chain 1068 #define sd_iodone_chain ssd_iodone_chain 1069 #define sd_initpkt_map ssd_initpkt_map 1070 #define sd_destroypkt_map ssd_destroypkt_map 1071 #define sd_chain_type_map ssd_chain_type_map 1072 #define sd_chain_index_map ssd_chain_index_map 1073 1074 #define sd_failfast_flushctl ssd_failfast_flushctl 1075 #define sd_failfast_flushq ssd_failfast_flushq 1076 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1077 1078 #define sd_is_lsi ssd_is_lsi 1079 #define sd_tg_rdwr ssd_tg_rdwr 1080 #define sd_tg_getinfo ssd_tg_getinfo 1081 1082 #endif /* #if (defined(__fibre)) */ 1083 1084 1085 int _init(void); 1086 int _fini(void); 1087 int _info(struct modinfo *modinfop); 1088 1089 /*PRINTFLIKE3*/ 1090 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1091 /*PRINTFLIKE3*/ 1092 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1093 /*PRINTFLIKE3*/ 1094 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1095 1096 static int sdprobe(dev_info_t *devi); 1097 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1098 void **result); 1099 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1100 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1101 1102 /* 1103 * Smart probe for parallel scsi 1104 */ 1105 static void sd_scsi_probe_cache_init(void); 1106 static void sd_scsi_probe_cache_fini(void); 1107 static void sd_scsi_clear_probe_cache(void); 1108 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1109 1110 /* 1111 * Attached luns on target for parallel scsi 1112 */ 1113 static void sd_scsi_target_lun_init(void); 1114 static void sd_scsi_target_lun_fini(void); 1115 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1116 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1117 1118 static int sd_spin_up_unit(struct sd_lun *un); 1119 #ifdef _LP64 1120 static void sd_enable_descr_sense(struct sd_lun *un); 1121 static void sd_reenable_dsense_task(void *arg); 1122 #endif /* _LP64 */ 1123 1124 static void sd_set_mmc_caps(struct sd_lun *un); 1125 1126 static void sd_read_unit_properties(struct sd_lun *un); 1127 static int sd_process_sdconf_file(struct sd_lun *un); 1128 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1129 int *data_list, sd_tunables *values); 1130 static void sd_process_sdconf_table(struct sd_lun *un); 1131 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1132 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1133 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1134 int list_len, char *dataname_ptr); 1135 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1136 sd_tunables *prop_list); 1137 1138 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1139 int reservation_flag); 1140 static int sd_get_devid(struct sd_lun *un); 1141 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1142 static int sd_write_deviceid(struct sd_lun *un); 1143 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1144 static int sd_check_vpd_page_support(struct sd_lun *un); 1145 1146 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1147 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1148 1149 static int sd_ddi_suspend(dev_info_t *devi); 1150 static int sd_ddi_pm_suspend(struct sd_lun *un); 1151 static int sd_ddi_resume(dev_info_t *devi); 1152 static int sd_ddi_pm_resume(struct sd_lun *un); 1153 static int sdpower(dev_info_t *devi, int component, int level); 1154 1155 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1156 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1157 static int sd_unit_attach(dev_info_t *devi); 1158 static int sd_unit_detach(dev_info_t *devi); 1159 1160 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1161 static void sd_create_errstats(struct sd_lun *un, int instance); 1162 static void sd_set_errstats(struct sd_lun *un); 1163 static void sd_set_pstats(struct sd_lun *un); 1164 1165 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1166 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1167 static int sd_send_polled_RQS(struct sd_lun *un); 1168 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1169 1170 #if (defined(__fibre)) 1171 /* 1172 * Event callbacks (photon) 1173 */ 1174 static void sd_init_event_callbacks(struct sd_lun *un); 1175 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1176 #endif 1177 1178 /* 1179 * Defines for sd_cache_control 1180 */ 1181 1182 #define SD_CACHE_ENABLE 1 1183 #define SD_CACHE_DISABLE 0 1184 #define SD_CACHE_NOCHANGE -1 1185 1186 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1187 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1188 static void sd_get_nv_sup(struct sd_lun *un); 1189 static dev_t sd_make_device(dev_info_t *devi); 1190 1191 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1192 uint64_t capacity); 1193 1194 /* 1195 * Driver entry point functions. 1196 */ 1197 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1198 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1199 static int sd_ready_and_valid(struct sd_lun *un); 1200 1201 static void sdmin(struct buf *bp); 1202 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1203 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1204 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1205 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1206 1207 static int sdstrategy(struct buf *bp); 1208 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1209 1210 /* 1211 * Function prototypes for layering functions in the iostart chain. 1212 */ 1213 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1214 struct buf *bp); 1215 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1216 struct buf *bp); 1217 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1218 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1219 struct buf *bp); 1220 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1221 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1222 1223 /* 1224 * Function prototypes for layering functions in the iodone chain. 1225 */ 1226 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1227 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1228 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1229 struct buf *bp); 1230 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1231 struct buf *bp); 1232 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1233 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1234 struct buf *bp); 1235 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1236 1237 /* 1238 * Prototypes for functions to support buf(9S) based IO. 1239 */ 1240 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1241 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1242 static void sd_destroypkt_for_buf(struct buf *); 1243 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1244 struct buf *bp, int flags, 1245 int (*callback)(caddr_t), caddr_t callback_arg, 1246 diskaddr_t lba, uint32_t blockcount); 1247 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1248 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1249 1250 /* 1251 * Prototypes for functions to support USCSI IO. 1252 */ 1253 static int sd_uscsi_strategy(struct buf *bp); 1254 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1255 static void sd_destroypkt_for_uscsi(struct buf *); 1256 1257 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1258 uchar_t chain_type, void *pktinfop); 1259 1260 static int sd_pm_entry(struct sd_lun *un); 1261 static void sd_pm_exit(struct sd_lun *un); 1262 1263 static void sd_pm_idletimeout_handler(void *arg); 1264 1265 /* 1266 * sd_core internal functions (used at the sd_core_io layer). 1267 */ 1268 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1269 static void sdintr(struct scsi_pkt *pktp); 1270 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1271 1272 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1273 enum uio_seg dataspace, int path_flag); 1274 1275 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1276 daddr_t blkno, int (*func)(struct buf *)); 1277 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1278 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1279 static void sd_bioclone_free(struct buf *bp); 1280 static void sd_shadow_buf_free(struct buf *bp); 1281 1282 static void sd_print_transport_rejected_message(struct sd_lun *un, 1283 struct sd_xbuf *xp, int code); 1284 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1285 void *arg, int code); 1286 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1287 void *arg, int code); 1288 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1289 void *arg, int code); 1290 1291 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1292 int retry_check_flag, 1293 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1294 int c), 1295 void *user_arg, int failure_code, clock_t retry_delay, 1296 void (*statp)(kstat_io_t *)); 1297 1298 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1299 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1300 1301 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1302 struct scsi_pkt *pktp); 1303 static void sd_start_retry_command(void *arg); 1304 static void sd_start_direct_priority_command(void *arg); 1305 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1306 int errcode); 1307 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1308 struct buf *bp, int errcode); 1309 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1310 static void sd_sync_with_callback(struct sd_lun *un); 1311 static int sdrunout(caddr_t arg); 1312 1313 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1314 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1315 1316 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1317 static void sd_restore_throttle(void *arg); 1318 1319 static void sd_init_cdb_limits(struct sd_lun *un); 1320 1321 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1322 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1323 1324 /* 1325 * Error handling functions 1326 */ 1327 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1328 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1329 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1330 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1331 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1332 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1333 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1334 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1335 1336 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1337 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1338 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1339 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1340 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1341 struct sd_xbuf *xp, size_t actual_len); 1342 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1343 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1344 1345 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1346 void *arg, int code); 1347 1348 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1349 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1350 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1351 uint8_t *sense_datap, 1352 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1353 static void sd_sense_key_not_ready(struct sd_lun *un, 1354 uint8_t *sense_datap, 1355 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1356 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1357 uint8_t *sense_datap, 1358 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1359 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1360 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1361 static void sd_sense_key_unit_attention(struct sd_lun *un, 1362 uint8_t *sense_datap, 1363 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1364 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1365 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1366 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1367 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1368 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1369 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1370 static void sd_sense_key_default(struct sd_lun *un, 1371 uint8_t *sense_datap, 1372 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1373 1374 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1375 void *arg, int flag); 1376 1377 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1378 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1379 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1380 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1381 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1382 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1383 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1384 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1385 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1386 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1387 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1388 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1389 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1390 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1391 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1392 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1393 1394 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1395 1396 static void sd_start_stop_unit_callback(void *arg); 1397 static void sd_start_stop_unit_task(void *arg); 1398 1399 static void sd_taskq_create(void); 1400 static void sd_taskq_delete(void); 1401 static void sd_target_change_task(void *arg); 1402 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag); 1403 static void sd_media_change_task(void *arg); 1404 1405 static int sd_handle_mchange(struct sd_lun *un); 1406 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1407 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1408 uint32_t *lbap, int path_flag); 1409 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1410 uint32_t *lbap, int path_flag); 1411 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1412 int path_flag); 1413 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1414 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1415 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1416 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1417 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1418 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1419 uchar_t usr_cmd, uchar_t *usr_bufp); 1420 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1421 struct dk_callback *dkc); 1422 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1423 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1424 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1425 uchar_t *bufaddr, uint_t buflen, int path_flag); 1426 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1427 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1428 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1429 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1430 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1431 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1432 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1433 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1434 size_t buflen, daddr_t start_block, int path_flag); 1435 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1436 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1437 path_flag) 1438 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1439 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1440 path_flag) 1441 1442 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1443 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1444 uint16_t param_ptr, int path_flag); 1445 1446 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1447 static void sd_free_rqs(struct sd_lun *un); 1448 1449 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1450 uchar_t *data, int len, int fmt); 1451 static void sd_panic_for_res_conflict(struct sd_lun *un); 1452 1453 /* 1454 * Disk Ioctl Function Prototypes 1455 */ 1456 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1457 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1458 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1459 1460 /* 1461 * Multi-host Ioctl Prototypes 1462 */ 1463 static int sd_check_mhd(dev_t dev, int interval); 1464 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1465 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1466 static char *sd_sname(uchar_t status); 1467 static void sd_mhd_resvd_recover(void *arg); 1468 static void sd_resv_reclaim_thread(); 1469 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1470 static int sd_reserve_release(dev_t dev, int cmd); 1471 static void sd_rmv_resv_reclaim_req(dev_t dev); 1472 static void sd_mhd_reset_notify_cb(caddr_t arg); 1473 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1474 mhioc_inkeys_t *usrp, int flag); 1475 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1476 mhioc_inresvs_t *usrp, int flag); 1477 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1478 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1479 static int sd_mhdioc_release(dev_t dev); 1480 static int sd_mhdioc_register_devid(dev_t dev); 1481 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1482 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1483 1484 /* 1485 * SCSI removable prototypes 1486 */ 1487 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1488 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1489 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1490 static int sr_pause_resume(dev_t dev, int mode); 1491 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1492 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1493 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1494 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1495 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1496 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1497 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1498 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1499 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1500 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1501 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1502 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1503 static int sr_eject(dev_t dev); 1504 static void sr_ejected(register struct sd_lun *un); 1505 static int sr_check_wp(dev_t dev); 1506 static int sd_check_media(dev_t dev, enum dkio_state state); 1507 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1508 static void sd_delayed_cv_broadcast(void *arg); 1509 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1510 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1511 1512 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1513 1514 /* 1515 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1516 */ 1517 static void sd_check_for_writable_cd(struct sd_lun *un, int path_flag); 1518 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1519 static void sd_wm_cache_destructor(void *wm, void *un); 1520 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1521 daddr_t endb, ushort_t typ); 1522 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1523 daddr_t endb); 1524 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1525 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1526 static void sd_read_modify_write_task(void * arg); 1527 static int 1528 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1529 struct buf **bpp); 1530 1531 1532 /* 1533 * Function prototypes for failfast support. 1534 */ 1535 static void sd_failfast_flushq(struct sd_lun *un); 1536 static int sd_failfast_flushq_callback(struct buf *bp); 1537 1538 /* 1539 * Function prototypes to check for lsi devices 1540 */ 1541 static void sd_is_lsi(struct sd_lun *un); 1542 1543 /* 1544 * Function prototypes for partial DMA support 1545 */ 1546 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1547 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1548 1549 1550 /* Function prototypes for cmlb */ 1551 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1552 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1553 1554 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1555 1556 /* 1557 * Constants for failfast support: 1558 * 1559 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1560 * failfast processing being performed. 1561 * 1562 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1563 * failfast processing on all bufs with B_FAILFAST set. 1564 */ 1565 1566 #define SD_FAILFAST_INACTIVE 0 1567 #define SD_FAILFAST_ACTIVE 1 1568 1569 /* 1570 * Bitmask to control behavior of buf(9S) flushes when a transition to 1571 * the failfast state occurs. Optional bits include: 1572 * 1573 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1574 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1575 * be flushed. 1576 * 1577 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1578 * driver, in addition to the regular wait queue. This includes the xbuf 1579 * queues. When clear, only the driver's wait queue will be flushed. 1580 */ 1581 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1582 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1583 1584 /* 1585 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1586 * to flush all queues within the driver. 1587 */ 1588 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1589 1590 1591 /* 1592 * SD Testing Fault Injection 1593 */ 1594 #ifdef SD_FAULT_INJECTION 1595 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1596 static void sd_faultinjection(struct scsi_pkt *pktp); 1597 static void sd_injection_log(char *buf, struct sd_lun *un); 1598 #endif 1599 1600 /* 1601 * Device driver ops vector 1602 */ 1603 static struct cb_ops sd_cb_ops = { 1604 sdopen, /* open */ 1605 sdclose, /* close */ 1606 sdstrategy, /* strategy */ 1607 nodev, /* print */ 1608 sddump, /* dump */ 1609 sdread, /* read */ 1610 sdwrite, /* write */ 1611 sdioctl, /* ioctl */ 1612 nodev, /* devmap */ 1613 nodev, /* mmap */ 1614 nodev, /* segmap */ 1615 nochpoll, /* poll */ 1616 sd_prop_op, /* cb_prop_op */ 1617 0, /* streamtab */ 1618 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1619 CB_REV, /* cb_rev */ 1620 sdaread, /* async I/O read entry point */ 1621 sdawrite /* async I/O write entry point */ 1622 }; 1623 1624 static struct dev_ops sd_ops = { 1625 DEVO_REV, /* devo_rev, */ 1626 0, /* refcnt */ 1627 sdinfo, /* info */ 1628 nulldev, /* identify */ 1629 sdprobe, /* probe */ 1630 sdattach, /* attach */ 1631 sddetach, /* detach */ 1632 nodev, /* reset */ 1633 &sd_cb_ops, /* driver operations */ 1634 NULL, /* bus operations */ 1635 sdpower /* power */ 1636 }; 1637 1638 1639 /* 1640 * This is the loadable module wrapper. 1641 */ 1642 #include <sys/modctl.h> 1643 1644 static struct modldrv modldrv = { 1645 &mod_driverops, /* Type of module. This one is a driver */ 1646 SD_MODULE_NAME, /* Module name. */ 1647 &sd_ops /* driver ops */ 1648 }; 1649 1650 1651 static struct modlinkage modlinkage = { 1652 MODREV_1, 1653 &modldrv, 1654 NULL 1655 }; 1656 1657 static cmlb_tg_ops_t sd_tgops = { 1658 TG_DK_OPS_VERSION_1, 1659 sd_tg_rdwr, 1660 sd_tg_getinfo 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. Separate "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 separate arrays which 1699 * must be maintained in synchronization. The purpose of this division is 1700 * to achieve 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 * Function: _init 2136 * 2137 * Description: This is the driver _init(9E) entry point. 2138 * 2139 * Return Code: Returns the value from mod_install(9F) or 2140 * ddi_soft_state_init(9F) as appropriate. 2141 * 2142 * Context: Called when driver module loaded. 2143 */ 2144 2145 int 2146 _init(void) 2147 { 2148 int err; 2149 2150 /* establish driver name from module name */ 2151 sd_label = (char *)mod_modname(&modlinkage); 2152 2153 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2154 SD_MAXUNIT); 2155 2156 if (err != 0) { 2157 return (err); 2158 } 2159 2160 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2161 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2162 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2163 2164 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2165 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2166 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2167 2168 /* 2169 * it's ok to init here even for fibre device 2170 */ 2171 sd_scsi_probe_cache_init(); 2172 2173 sd_scsi_target_lun_init(); 2174 2175 /* 2176 * Creating taskq before mod_install ensures that all callers (threads) 2177 * that enter the module after a successful mod_install encounter 2178 * a valid taskq. 2179 */ 2180 sd_taskq_create(); 2181 2182 err = mod_install(&modlinkage); 2183 if (err != 0) { 2184 /* delete taskq if install fails */ 2185 sd_taskq_delete(); 2186 2187 mutex_destroy(&sd_detach_mutex); 2188 mutex_destroy(&sd_log_mutex); 2189 mutex_destroy(&sd_label_mutex); 2190 2191 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2192 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2193 cv_destroy(&sd_tr.srq_inprocess_cv); 2194 2195 sd_scsi_probe_cache_fini(); 2196 2197 sd_scsi_target_lun_fini(); 2198 2199 ddi_soft_state_fini(&sd_state); 2200 return (err); 2201 } 2202 2203 return (err); 2204 } 2205 2206 2207 /* 2208 * Function: _fini 2209 * 2210 * Description: This is the driver _fini(9E) entry point. 2211 * 2212 * Return Code: Returns the value from mod_remove(9F) 2213 * 2214 * Context: Called when driver module is unloaded. 2215 */ 2216 2217 int 2218 _fini(void) 2219 { 2220 int err; 2221 2222 if ((err = mod_remove(&modlinkage)) != 0) { 2223 return (err); 2224 } 2225 2226 sd_taskq_delete(); 2227 2228 mutex_destroy(&sd_detach_mutex); 2229 mutex_destroy(&sd_log_mutex); 2230 mutex_destroy(&sd_label_mutex); 2231 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2232 2233 sd_scsi_probe_cache_fini(); 2234 2235 sd_scsi_target_lun_fini(); 2236 2237 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2238 cv_destroy(&sd_tr.srq_inprocess_cv); 2239 2240 ddi_soft_state_fini(&sd_state); 2241 2242 return (err); 2243 } 2244 2245 2246 /* 2247 * Function: _info 2248 * 2249 * Description: This is the driver _info(9E) entry point. 2250 * 2251 * Arguments: modinfop - pointer to the driver modinfo structure 2252 * 2253 * Return Code: Returns the value from mod_info(9F). 2254 * 2255 * Context: Kernel thread context 2256 */ 2257 2258 int 2259 _info(struct modinfo *modinfop) 2260 { 2261 return (mod_info(&modlinkage, modinfop)); 2262 } 2263 2264 2265 /* 2266 * The following routines implement the driver message logging facility. 2267 * They provide component- and level- based debug output filtering. 2268 * Output may also be restricted to messages for a single instance by 2269 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2270 * to NULL, then messages for all instances are printed. 2271 * 2272 * These routines have been cloned from each other due to the language 2273 * constraints of macros and variable argument list processing. 2274 */ 2275 2276 2277 /* 2278 * Function: sd_log_err 2279 * 2280 * Description: This routine is called by the SD_ERROR macro for debug 2281 * logging of error conditions. 2282 * 2283 * Arguments: comp - driver component being logged 2284 * dev - pointer to driver info structure 2285 * fmt - error string and format to be logged 2286 */ 2287 2288 static void 2289 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2290 { 2291 va_list ap; 2292 dev_info_t *dev; 2293 2294 ASSERT(un != NULL); 2295 dev = SD_DEVINFO(un); 2296 ASSERT(dev != NULL); 2297 2298 /* 2299 * Filter messages based on the global component and level masks. 2300 * Also print if un matches the value of sd_debug_un, or if 2301 * sd_debug_un is set to NULL. 2302 */ 2303 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2304 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2305 mutex_enter(&sd_log_mutex); 2306 va_start(ap, fmt); 2307 (void) vsprintf(sd_log_buf, fmt, ap); 2308 va_end(ap); 2309 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2310 mutex_exit(&sd_log_mutex); 2311 } 2312 #ifdef SD_FAULT_INJECTION 2313 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2314 if (un->sd_injection_mask & comp) { 2315 mutex_enter(&sd_log_mutex); 2316 va_start(ap, fmt); 2317 (void) vsprintf(sd_log_buf, fmt, ap); 2318 va_end(ap); 2319 sd_injection_log(sd_log_buf, un); 2320 mutex_exit(&sd_log_mutex); 2321 } 2322 #endif 2323 } 2324 2325 2326 /* 2327 * Function: sd_log_info 2328 * 2329 * Description: This routine is called by the SD_INFO macro for debug 2330 * logging of general purpose informational conditions. 2331 * 2332 * Arguments: comp - driver component being logged 2333 * dev - pointer to driver info structure 2334 * fmt - info string and format to be logged 2335 */ 2336 2337 static void 2338 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2339 { 2340 va_list ap; 2341 dev_info_t *dev; 2342 2343 ASSERT(un != NULL); 2344 dev = SD_DEVINFO(un); 2345 ASSERT(dev != NULL); 2346 2347 /* 2348 * Filter messages based on the global component and level masks. 2349 * Also print if un matches the value of sd_debug_un, or if 2350 * sd_debug_un is set to NULL. 2351 */ 2352 if ((sd_component_mask & component) && 2353 (sd_level_mask & SD_LOGMASK_INFO) && 2354 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2355 mutex_enter(&sd_log_mutex); 2356 va_start(ap, fmt); 2357 (void) vsprintf(sd_log_buf, fmt, ap); 2358 va_end(ap); 2359 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2360 mutex_exit(&sd_log_mutex); 2361 } 2362 #ifdef SD_FAULT_INJECTION 2363 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2364 if (un->sd_injection_mask & component) { 2365 mutex_enter(&sd_log_mutex); 2366 va_start(ap, fmt); 2367 (void) vsprintf(sd_log_buf, fmt, ap); 2368 va_end(ap); 2369 sd_injection_log(sd_log_buf, un); 2370 mutex_exit(&sd_log_mutex); 2371 } 2372 #endif 2373 } 2374 2375 2376 /* 2377 * Function: sd_log_trace 2378 * 2379 * Description: This routine is called by the SD_TRACE macro for debug 2380 * logging of trace conditions (i.e. function entry/exit). 2381 * 2382 * Arguments: comp - driver component being logged 2383 * dev - pointer to driver info structure 2384 * fmt - trace string and format to be logged 2385 */ 2386 2387 static void 2388 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2389 { 2390 va_list ap; 2391 dev_info_t *dev; 2392 2393 ASSERT(un != NULL); 2394 dev = SD_DEVINFO(un); 2395 ASSERT(dev != NULL); 2396 2397 /* 2398 * Filter messages based on the global component and level masks. 2399 * Also print if un matches the value of sd_debug_un, or if 2400 * sd_debug_un is set to NULL. 2401 */ 2402 if ((sd_component_mask & component) && 2403 (sd_level_mask & SD_LOGMASK_TRACE) && 2404 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2405 mutex_enter(&sd_log_mutex); 2406 va_start(ap, fmt); 2407 (void) vsprintf(sd_log_buf, fmt, ap); 2408 va_end(ap); 2409 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2410 mutex_exit(&sd_log_mutex); 2411 } 2412 #ifdef SD_FAULT_INJECTION 2413 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2414 if (un->sd_injection_mask & component) { 2415 mutex_enter(&sd_log_mutex); 2416 va_start(ap, fmt); 2417 (void) vsprintf(sd_log_buf, fmt, ap); 2418 va_end(ap); 2419 sd_injection_log(sd_log_buf, un); 2420 mutex_exit(&sd_log_mutex); 2421 } 2422 #endif 2423 } 2424 2425 2426 /* 2427 * Function: sdprobe 2428 * 2429 * Description: This is the driver probe(9e) entry point function. 2430 * 2431 * Arguments: devi - opaque device info handle 2432 * 2433 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2434 * DDI_PROBE_FAILURE: If the probe failed. 2435 * DDI_PROBE_PARTIAL: If the instance is not present now, 2436 * but may be present in the future. 2437 */ 2438 2439 static int 2440 sdprobe(dev_info_t *devi) 2441 { 2442 struct scsi_device *devp; 2443 int rval; 2444 int instance; 2445 2446 /* 2447 * if it wasn't for pln, sdprobe could actually be nulldev 2448 * in the "__fibre" case. 2449 */ 2450 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2451 return (DDI_PROBE_DONTCARE); 2452 } 2453 2454 devp = ddi_get_driver_private(devi); 2455 2456 if (devp == NULL) { 2457 /* Ooops... nexus driver is mis-configured... */ 2458 return (DDI_PROBE_FAILURE); 2459 } 2460 2461 instance = ddi_get_instance(devi); 2462 2463 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2464 return (DDI_PROBE_PARTIAL); 2465 } 2466 2467 /* 2468 * Call the SCSA utility probe routine to see if we actually 2469 * have a target at this SCSI nexus. 2470 */ 2471 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2472 case SCSIPROBE_EXISTS: 2473 switch (devp->sd_inq->inq_dtype) { 2474 case DTYPE_DIRECT: 2475 rval = DDI_PROBE_SUCCESS; 2476 break; 2477 case DTYPE_RODIRECT: 2478 /* CDs etc. Can be removable media */ 2479 rval = DDI_PROBE_SUCCESS; 2480 break; 2481 case DTYPE_OPTICAL: 2482 /* 2483 * Rewritable optical driver HP115AA 2484 * Can also be removable media 2485 */ 2486 2487 /* 2488 * Do not attempt to bind to DTYPE_OPTICAL if 2489 * pre solaris 9 sparc sd behavior is required 2490 * 2491 * If first time through and sd_dtype_optical_bind 2492 * has not been set in /etc/system check properties 2493 */ 2494 2495 if (sd_dtype_optical_bind < 0) { 2496 sd_dtype_optical_bind = ddi_prop_get_int 2497 (DDI_DEV_T_ANY, devi, 0, 2498 "optical-device-bind", 1); 2499 } 2500 2501 if (sd_dtype_optical_bind == 0) { 2502 rval = DDI_PROBE_FAILURE; 2503 } else { 2504 rval = DDI_PROBE_SUCCESS; 2505 } 2506 break; 2507 2508 case DTYPE_NOTPRESENT: 2509 default: 2510 rval = DDI_PROBE_FAILURE; 2511 break; 2512 } 2513 break; 2514 default: 2515 rval = DDI_PROBE_PARTIAL; 2516 break; 2517 } 2518 2519 /* 2520 * This routine checks for resource allocation prior to freeing, 2521 * so it will take care of the "smart probing" case where a 2522 * scsi_probe() may or may not have been issued and will *not* 2523 * free previously-freed resources. 2524 */ 2525 scsi_unprobe(devp); 2526 return (rval); 2527 } 2528 2529 2530 /* 2531 * Function: sdinfo 2532 * 2533 * Description: This is the driver getinfo(9e) entry point function. 2534 * Given the device number, return the devinfo pointer from 2535 * the scsi_device structure or the instance number 2536 * associated with the dev_t. 2537 * 2538 * Arguments: dip - pointer to device info structure 2539 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2540 * DDI_INFO_DEVT2INSTANCE) 2541 * arg - driver dev_t 2542 * resultp - user buffer for request response 2543 * 2544 * Return Code: DDI_SUCCESS 2545 * DDI_FAILURE 2546 */ 2547 /* ARGSUSED */ 2548 static int 2549 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2550 { 2551 struct sd_lun *un; 2552 dev_t dev; 2553 int instance; 2554 int error; 2555 2556 switch (infocmd) { 2557 case DDI_INFO_DEVT2DEVINFO: 2558 dev = (dev_t)arg; 2559 instance = SDUNIT(dev); 2560 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2561 return (DDI_FAILURE); 2562 } 2563 *result = (void *) SD_DEVINFO(un); 2564 error = DDI_SUCCESS; 2565 break; 2566 case DDI_INFO_DEVT2INSTANCE: 2567 dev = (dev_t)arg; 2568 instance = SDUNIT(dev); 2569 *result = (void *)(uintptr_t)instance; 2570 error = DDI_SUCCESS; 2571 break; 2572 default: 2573 error = DDI_FAILURE; 2574 } 2575 return (error); 2576 } 2577 2578 /* 2579 * Function: sd_prop_op 2580 * 2581 * Description: This is the driver prop_op(9e) entry point function. 2582 * Return the number of blocks for the partition in question 2583 * or forward the request to the property facilities. 2584 * 2585 * Arguments: dev - device number 2586 * dip - pointer to device info structure 2587 * prop_op - property operator 2588 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2589 * name - pointer to property name 2590 * valuep - pointer or address of the user buffer 2591 * lengthp - property length 2592 * 2593 * Return Code: DDI_PROP_SUCCESS 2594 * DDI_PROP_NOT_FOUND 2595 * DDI_PROP_UNDEFINED 2596 * DDI_PROP_NO_MEMORY 2597 * DDI_PROP_BUF_TOO_SMALL 2598 */ 2599 2600 static int 2601 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2602 char *name, caddr_t valuep, int *lengthp) 2603 { 2604 struct sd_lun *un; 2605 2606 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL) 2607 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2608 name, valuep, lengthp)); 2609 2610 return (cmlb_prop_op(un->un_cmlbhandle, 2611 dev, dip, prop_op, mod_flags, name, valuep, lengthp, 2612 SDPART(dev), (void *)SD_PATH_DIRECT)); 2613 } 2614 2615 /* 2616 * The following functions are for smart probing: 2617 * sd_scsi_probe_cache_init() 2618 * sd_scsi_probe_cache_fini() 2619 * sd_scsi_clear_probe_cache() 2620 * sd_scsi_probe_with_cache() 2621 */ 2622 2623 /* 2624 * Function: sd_scsi_probe_cache_init 2625 * 2626 * Description: Initializes the probe response cache mutex and head pointer. 2627 * 2628 * Context: Kernel thread context 2629 */ 2630 2631 static void 2632 sd_scsi_probe_cache_init(void) 2633 { 2634 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2635 sd_scsi_probe_cache_head = NULL; 2636 } 2637 2638 2639 /* 2640 * Function: sd_scsi_probe_cache_fini 2641 * 2642 * Description: Frees all resources associated with the probe response cache. 2643 * 2644 * Context: Kernel thread context 2645 */ 2646 2647 static void 2648 sd_scsi_probe_cache_fini(void) 2649 { 2650 struct sd_scsi_probe_cache *cp; 2651 struct sd_scsi_probe_cache *ncp; 2652 2653 /* Clean up our smart probing linked list */ 2654 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2655 ncp = cp->next; 2656 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2657 } 2658 sd_scsi_probe_cache_head = NULL; 2659 mutex_destroy(&sd_scsi_probe_cache_mutex); 2660 } 2661 2662 2663 /* 2664 * Function: sd_scsi_clear_probe_cache 2665 * 2666 * Description: This routine clears the probe response cache. This is 2667 * done when open() returns ENXIO so that when deferred 2668 * attach is attempted (possibly after a device has been 2669 * turned on) we will retry the probe. Since we don't know 2670 * which target we failed to open, we just clear the 2671 * entire cache. 2672 * 2673 * Context: Kernel thread context 2674 */ 2675 2676 static void 2677 sd_scsi_clear_probe_cache(void) 2678 { 2679 struct sd_scsi_probe_cache *cp; 2680 int i; 2681 2682 mutex_enter(&sd_scsi_probe_cache_mutex); 2683 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2684 /* 2685 * Reset all entries to SCSIPROBE_EXISTS. This will 2686 * force probing to be performed the next time 2687 * sd_scsi_probe_with_cache is called. 2688 */ 2689 for (i = 0; i < NTARGETS_WIDE; i++) { 2690 cp->cache[i] = SCSIPROBE_EXISTS; 2691 } 2692 } 2693 mutex_exit(&sd_scsi_probe_cache_mutex); 2694 } 2695 2696 2697 /* 2698 * Function: sd_scsi_probe_with_cache 2699 * 2700 * Description: This routine implements support for a scsi device probe 2701 * with cache. The driver maintains a cache of the target 2702 * responses to scsi probes. If we get no response from a 2703 * target during a probe inquiry, we remember that, and we 2704 * avoid additional calls to scsi_probe on non-zero LUNs 2705 * on the same target until the cache is cleared. By doing 2706 * so we avoid the 1/4 sec selection timeout for nonzero 2707 * LUNs. lun0 of a target is always probed. 2708 * 2709 * Arguments: devp - Pointer to a scsi_device(9S) structure 2710 * waitfunc - indicates what the allocator routines should 2711 * do when resources are not available. This value 2712 * is passed on to scsi_probe() when that routine 2713 * is called. 2714 * 2715 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2716 * otherwise the value returned by scsi_probe(9F). 2717 * 2718 * Context: Kernel thread context 2719 */ 2720 2721 static int 2722 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2723 { 2724 struct sd_scsi_probe_cache *cp; 2725 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2726 int lun, tgt; 2727 2728 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2729 SCSI_ADDR_PROP_LUN, 0); 2730 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2731 SCSI_ADDR_PROP_TARGET, -1); 2732 2733 /* Make sure caching enabled and target in range */ 2734 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2735 /* do it the old way (no cache) */ 2736 return (scsi_probe(devp, waitfn)); 2737 } 2738 2739 mutex_enter(&sd_scsi_probe_cache_mutex); 2740 2741 /* Find the cache for this scsi bus instance */ 2742 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2743 if (cp->pdip == pdip) { 2744 break; 2745 } 2746 } 2747 2748 /* If we can't find a cache for this pdip, create one */ 2749 if (cp == NULL) { 2750 int i; 2751 2752 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2753 KM_SLEEP); 2754 cp->pdip = pdip; 2755 cp->next = sd_scsi_probe_cache_head; 2756 sd_scsi_probe_cache_head = cp; 2757 for (i = 0; i < NTARGETS_WIDE; i++) { 2758 cp->cache[i] = SCSIPROBE_EXISTS; 2759 } 2760 } 2761 2762 mutex_exit(&sd_scsi_probe_cache_mutex); 2763 2764 /* Recompute the cache for this target if LUN zero */ 2765 if (lun == 0) { 2766 cp->cache[tgt] = SCSIPROBE_EXISTS; 2767 } 2768 2769 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2770 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2771 return (SCSIPROBE_NORESP); 2772 } 2773 2774 /* Do the actual probe; save & return the result */ 2775 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2776 } 2777 2778 2779 /* 2780 * Function: sd_scsi_target_lun_init 2781 * 2782 * Description: Initializes the attached lun chain mutex and head pointer. 2783 * 2784 * Context: Kernel thread context 2785 */ 2786 2787 static void 2788 sd_scsi_target_lun_init(void) 2789 { 2790 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2791 sd_scsi_target_lun_head = NULL; 2792 } 2793 2794 2795 /* 2796 * Function: sd_scsi_target_lun_fini 2797 * 2798 * Description: Frees all resources associated with the attached lun 2799 * chain 2800 * 2801 * Context: Kernel thread context 2802 */ 2803 2804 static void 2805 sd_scsi_target_lun_fini(void) 2806 { 2807 struct sd_scsi_hba_tgt_lun *cp; 2808 struct sd_scsi_hba_tgt_lun *ncp; 2809 2810 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2811 ncp = cp->next; 2812 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2813 } 2814 sd_scsi_target_lun_head = NULL; 2815 mutex_destroy(&sd_scsi_target_lun_mutex); 2816 } 2817 2818 2819 /* 2820 * Function: sd_scsi_get_target_lun_count 2821 * 2822 * Description: This routine will check in the attached lun chain to see 2823 * how many luns are attached on the required SCSI controller 2824 * and target. Currently, some capabilities like tagged queue 2825 * are supported per target based by HBA. So all luns in a 2826 * target have the same capabilities. Based on this assumption, 2827 * sd should only set these capabilities once per target. This 2828 * function is called when sd needs to decide how many luns 2829 * already attached on a target. 2830 * 2831 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2832 * controller device. 2833 * target - The target ID on the controller's SCSI bus. 2834 * 2835 * Return Code: The number of luns attached on the required target and 2836 * controller. 2837 * -1 if target ID is not in parallel SCSI scope or the given 2838 * dip is not in the chain. 2839 * 2840 * Context: Kernel thread context 2841 */ 2842 2843 static int 2844 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2845 { 2846 struct sd_scsi_hba_tgt_lun *cp; 2847 2848 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2849 return (-1); 2850 } 2851 2852 mutex_enter(&sd_scsi_target_lun_mutex); 2853 2854 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2855 if (cp->pdip == dip) { 2856 break; 2857 } 2858 } 2859 2860 mutex_exit(&sd_scsi_target_lun_mutex); 2861 2862 if (cp == NULL) { 2863 return (-1); 2864 } 2865 2866 return (cp->nlun[target]); 2867 } 2868 2869 2870 /* 2871 * Function: sd_scsi_update_lun_on_target 2872 * 2873 * Description: This routine is used to update the attached lun chain when a 2874 * lun is attached or detached on a target. 2875 * 2876 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2877 * controller device. 2878 * target - The target ID on the controller's SCSI bus. 2879 * flag - Indicate the lun is attached or detached. 2880 * 2881 * Context: Kernel thread context 2882 */ 2883 2884 static void 2885 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 2886 { 2887 struct sd_scsi_hba_tgt_lun *cp; 2888 2889 mutex_enter(&sd_scsi_target_lun_mutex); 2890 2891 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2892 if (cp->pdip == dip) { 2893 break; 2894 } 2895 } 2896 2897 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 2898 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 2899 KM_SLEEP); 2900 cp->pdip = dip; 2901 cp->next = sd_scsi_target_lun_head; 2902 sd_scsi_target_lun_head = cp; 2903 } 2904 2905 mutex_exit(&sd_scsi_target_lun_mutex); 2906 2907 if (cp != NULL) { 2908 if (flag == SD_SCSI_LUN_ATTACH) { 2909 cp->nlun[target] ++; 2910 } else { 2911 cp->nlun[target] --; 2912 } 2913 } 2914 } 2915 2916 2917 /* 2918 * Function: sd_spin_up_unit 2919 * 2920 * Description: Issues the following commands to spin-up the device: 2921 * START STOP UNIT, and INQUIRY. 2922 * 2923 * Arguments: un - driver soft state (unit) structure 2924 * 2925 * Return Code: 0 - success 2926 * EIO - failure 2927 * EACCES - reservation conflict 2928 * 2929 * Context: Kernel thread context 2930 */ 2931 2932 static int 2933 sd_spin_up_unit(struct sd_lun *un) 2934 { 2935 size_t resid = 0; 2936 int has_conflict = FALSE; 2937 uchar_t *bufaddr; 2938 2939 ASSERT(un != NULL); 2940 2941 /* 2942 * Send a throwaway START UNIT command. 2943 * 2944 * If we fail on this, we don't care presently what precisely 2945 * is wrong. EMC's arrays will also fail this with a check 2946 * condition (0x2/0x4/0x3) if the device is "inactive," but 2947 * we don't want to fail the attach because it may become 2948 * "active" later. 2949 */ 2950 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2951 == EACCES) 2952 has_conflict = TRUE; 2953 2954 /* 2955 * Send another INQUIRY command to the target. This is necessary for 2956 * non-removable media direct access devices because their INQUIRY data 2957 * may not be fully qualified until they are spun up (perhaps via the 2958 * START command above). Note: This seems to be needed for some 2959 * legacy devices only.) The INQUIRY command should succeed even if a 2960 * Reservation Conflict is present. 2961 */ 2962 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2963 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2964 kmem_free(bufaddr, SUN_INQSIZE); 2965 return (EIO); 2966 } 2967 2968 /* 2969 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2970 * Note that this routine does not return a failure here even if the 2971 * INQUIRY command did not return any data. This is a legacy behavior. 2972 */ 2973 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2974 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2975 } 2976 2977 kmem_free(bufaddr, SUN_INQSIZE); 2978 2979 /* If we hit a reservation conflict above, tell the caller. */ 2980 if (has_conflict == TRUE) { 2981 return (EACCES); 2982 } 2983 2984 return (0); 2985 } 2986 2987 #ifdef _LP64 2988 /* 2989 * Function: sd_enable_descr_sense 2990 * 2991 * Description: This routine attempts to select descriptor sense format 2992 * using the Control mode page. Devices that support 64 bit 2993 * LBAs (for >2TB luns) should also implement descriptor 2994 * sense data so we will call this function whenever we see 2995 * a lun larger than 2TB. If for some reason the device 2996 * supports 64 bit LBAs but doesn't support descriptor sense 2997 * presumably the mode select will fail. Everything will 2998 * continue to work normally except that we will not get 2999 * complete sense data for commands that fail with an LBA 3000 * larger than 32 bits. 3001 * 3002 * Arguments: un - driver soft state (unit) structure 3003 * 3004 * Context: Kernel thread context only 3005 */ 3006 3007 static void 3008 sd_enable_descr_sense(struct sd_lun *un) 3009 { 3010 uchar_t *header; 3011 struct mode_control_scsi3 *ctrl_bufp; 3012 size_t buflen; 3013 size_t bd_len; 3014 3015 /* 3016 * Read MODE SENSE page 0xA, Control Mode Page 3017 */ 3018 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3019 sizeof (struct mode_control_scsi3); 3020 header = kmem_zalloc(buflen, KM_SLEEP); 3021 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3022 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3023 SD_ERROR(SD_LOG_COMMON, un, 3024 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3025 goto eds_exit; 3026 } 3027 3028 /* 3029 * Determine size of Block Descriptors in order to locate 3030 * the mode page data. ATAPI devices return 0, SCSI devices 3031 * should return MODE_BLK_DESC_LENGTH. 3032 */ 3033 bd_len = ((struct mode_header *)header)->bdesc_length; 3034 3035 /* Clear the mode data length field for MODE SELECT */ 3036 ((struct mode_header *)header)->length = 0; 3037 3038 ctrl_bufp = (struct mode_control_scsi3 *) 3039 (header + MODE_HEADER_LENGTH + bd_len); 3040 3041 /* 3042 * If the page length is smaller than the expected value, 3043 * the target device doesn't support D_SENSE. Bail out here. 3044 */ 3045 if (ctrl_bufp->mode_page.length < 3046 sizeof (struct mode_control_scsi3) - 2) { 3047 SD_ERROR(SD_LOG_COMMON, un, 3048 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3049 goto eds_exit; 3050 } 3051 3052 /* 3053 * Clear PS bit for MODE SELECT 3054 */ 3055 ctrl_bufp->mode_page.ps = 0; 3056 3057 /* 3058 * Set D_SENSE to enable descriptor sense format. 3059 */ 3060 ctrl_bufp->d_sense = 1; 3061 3062 /* 3063 * Use MODE SELECT to commit the change to the D_SENSE bit 3064 */ 3065 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3066 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3067 SD_INFO(SD_LOG_COMMON, un, 3068 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3069 goto eds_exit; 3070 } 3071 3072 eds_exit: 3073 kmem_free(header, buflen); 3074 } 3075 3076 /* 3077 * Function: sd_reenable_dsense_task 3078 * 3079 * Description: Re-enable descriptor sense after device or bus reset 3080 * 3081 * Context: Executes in a taskq() thread context 3082 */ 3083 static void 3084 sd_reenable_dsense_task(void *arg) 3085 { 3086 struct sd_lun *un = arg; 3087 3088 ASSERT(un != NULL); 3089 sd_enable_descr_sense(un); 3090 } 3091 #endif /* _LP64 */ 3092 3093 /* 3094 * Function: sd_set_mmc_caps 3095 * 3096 * Description: This routine determines if the device is MMC compliant and if 3097 * the device supports CDDA via a mode sense of the CDVD 3098 * capabilities mode page. Also checks if the device is a 3099 * dvdram writable device. 3100 * 3101 * Arguments: un - driver soft state (unit) structure 3102 * 3103 * Context: Kernel thread context only 3104 */ 3105 3106 static void 3107 sd_set_mmc_caps(struct sd_lun *un) 3108 { 3109 struct mode_header_grp2 *sense_mhp; 3110 uchar_t *sense_page; 3111 caddr_t buf; 3112 int bd_len; 3113 int status; 3114 struct uscsi_cmd com; 3115 int rtn; 3116 uchar_t *out_data_rw, *out_data_hd; 3117 uchar_t *rqbuf_rw, *rqbuf_hd; 3118 3119 ASSERT(un != NULL); 3120 3121 /* 3122 * The flags which will be set in this function are - mmc compliant, 3123 * dvdram writable device, cdda support. Initialize them to FALSE 3124 * and if a capability is detected - it will be set to TRUE. 3125 */ 3126 un->un_f_mmc_cap = FALSE; 3127 un->un_f_dvdram_writable_device = FALSE; 3128 un->un_f_cfg_cdda = FALSE; 3129 3130 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3131 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3132 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3133 3134 if (status != 0) { 3135 /* command failed; just return */ 3136 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3137 return; 3138 } 3139 /* 3140 * If the mode sense request for the CDROM CAPABILITIES 3141 * page (0x2A) succeeds the device is assumed to be MMC. 3142 */ 3143 un->un_f_mmc_cap = TRUE; 3144 3145 /* Get to the page data */ 3146 sense_mhp = (struct mode_header_grp2 *)buf; 3147 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3148 sense_mhp->bdesc_length_lo; 3149 if (bd_len > MODE_BLK_DESC_LENGTH) { 3150 /* 3151 * We did not get back the expected block descriptor 3152 * length so we cannot determine if the device supports 3153 * CDDA. However, we still indicate the device is MMC 3154 * according to the successful response to the page 3155 * 0x2A mode sense request. 3156 */ 3157 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3158 "sd_set_mmc_caps: Mode Sense returned " 3159 "invalid block descriptor length\n"); 3160 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3161 return; 3162 } 3163 3164 /* See if read CDDA is supported */ 3165 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3166 bd_len); 3167 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3168 3169 /* See if writing DVD RAM is supported. */ 3170 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3171 if (un->un_f_dvdram_writable_device == TRUE) { 3172 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3173 return; 3174 } 3175 3176 /* 3177 * If the device presents DVD or CD capabilities in the mode 3178 * page, we can return here since a RRD will not have 3179 * these capabilities. 3180 */ 3181 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3182 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3183 return; 3184 } 3185 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3186 3187 /* 3188 * If un->un_f_dvdram_writable_device is still FALSE, 3189 * check for a Removable Rigid Disk (RRD). A RRD 3190 * device is identified by the features RANDOM_WRITABLE and 3191 * HARDWARE_DEFECT_MANAGEMENT. 3192 */ 3193 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3194 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3195 3196 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3197 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3198 RANDOM_WRITABLE, SD_PATH_STANDARD); 3199 if (rtn != 0) { 3200 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3201 kmem_free(rqbuf_rw, SENSE_LENGTH); 3202 return; 3203 } 3204 3205 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3206 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3207 3208 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3209 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3210 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3211 if (rtn == 0) { 3212 /* 3213 * We have good information, check for random writable 3214 * and hardware defect features. 3215 */ 3216 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3217 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3218 un->un_f_dvdram_writable_device = TRUE; 3219 } 3220 } 3221 3222 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3223 kmem_free(rqbuf_rw, SENSE_LENGTH); 3224 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3225 kmem_free(rqbuf_hd, SENSE_LENGTH); 3226 } 3227 3228 /* 3229 * Function: sd_check_for_writable_cd 3230 * 3231 * Description: This routine determines if the media in the device is 3232 * writable or not. It uses the get configuration command (0x46) 3233 * to determine if the media is writable 3234 * 3235 * Arguments: un - driver soft state (unit) structure 3236 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3237 * chain and the normal command waitq, or 3238 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3239 * "direct" chain and bypass the normal command 3240 * waitq. 3241 * 3242 * Context: Never called at interrupt context. 3243 */ 3244 3245 static void 3246 sd_check_for_writable_cd(struct sd_lun *un, int path_flag) 3247 { 3248 struct uscsi_cmd com; 3249 uchar_t *out_data; 3250 uchar_t *rqbuf; 3251 int rtn; 3252 uchar_t *out_data_rw, *out_data_hd; 3253 uchar_t *rqbuf_rw, *rqbuf_hd; 3254 struct mode_header_grp2 *sense_mhp; 3255 uchar_t *sense_page; 3256 caddr_t buf; 3257 int bd_len; 3258 int status; 3259 3260 ASSERT(un != NULL); 3261 ASSERT(mutex_owned(SD_MUTEX(un))); 3262 3263 /* 3264 * Initialize the writable media to false, if configuration info. 3265 * tells us otherwise then only we will set it. 3266 */ 3267 un->un_f_mmc_writable_media = FALSE; 3268 mutex_exit(SD_MUTEX(un)); 3269 3270 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3271 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3272 3273 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3274 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3275 3276 mutex_enter(SD_MUTEX(un)); 3277 if (rtn == 0) { 3278 /* 3279 * We have good information, check for writable DVD. 3280 */ 3281 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3282 un->un_f_mmc_writable_media = TRUE; 3283 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3284 kmem_free(rqbuf, SENSE_LENGTH); 3285 return; 3286 } 3287 } 3288 3289 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3290 kmem_free(rqbuf, SENSE_LENGTH); 3291 3292 /* 3293 * Determine if this is a RRD type device. 3294 */ 3295 mutex_exit(SD_MUTEX(un)); 3296 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3297 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3298 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3299 mutex_enter(SD_MUTEX(un)); 3300 if (status != 0) { 3301 /* command failed; just return */ 3302 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3303 return; 3304 } 3305 3306 /* Get to the page data */ 3307 sense_mhp = (struct mode_header_grp2 *)buf; 3308 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3309 if (bd_len > MODE_BLK_DESC_LENGTH) { 3310 /* 3311 * We did not get back the expected block descriptor length so 3312 * we cannot check the mode page. 3313 */ 3314 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3315 "sd_check_for_writable_cd: Mode Sense returned " 3316 "invalid block descriptor length\n"); 3317 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3318 return; 3319 } 3320 3321 /* 3322 * If the device presents DVD or CD capabilities in the mode 3323 * page, we can return here since a RRD device will not have 3324 * these capabilities. 3325 */ 3326 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3327 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3328 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3329 return; 3330 } 3331 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3332 3333 /* 3334 * If un->un_f_mmc_writable_media is still FALSE, 3335 * check for RRD type media. A RRD device is identified 3336 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3337 */ 3338 mutex_exit(SD_MUTEX(un)); 3339 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3340 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3341 3342 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3343 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3344 RANDOM_WRITABLE, path_flag); 3345 if (rtn != 0) { 3346 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3347 kmem_free(rqbuf_rw, SENSE_LENGTH); 3348 mutex_enter(SD_MUTEX(un)); 3349 return; 3350 } 3351 3352 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3353 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3354 3355 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3356 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3357 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3358 mutex_enter(SD_MUTEX(un)); 3359 if (rtn == 0) { 3360 /* 3361 * We have good information, check for random writable 3362 * and hardware defect features as current. 3363 */ 3364 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3365 (out_data_rw[10] & 0x1) && 3366 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3367 (out_data_hd[10] & 0x1)) { 3368 un->un_f_mmc_writable_media = TRUE; 3369 } 3370 } 3371 3372 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3373 kmem_free(rqbuf_rw, SENSE_LENGTH); 3374 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3375 kmem_free(rqbuf_hd, SENSE_LENGTH); 3376 } 3377 3378 /* 3379 * Function: sd_read_unit_properties 3380 * 3381 * Description: The following implements a property lookup mechanism. 3382 * Properties for particular disks (keyed on vendor, model 3383 * and rev numbers) are sought in the sd.conf file via 3384 * sd_process_sdconf_file(), and if not found there, are 3385 * looked for in a list hardcoded in this driver via 3386 * sd_process_sdconf_table() Once located the properties 3387 * are used to update the driver unit structure. 3388 * 3389 * Arguments: un - driver soft state (unit) structure 3390 */ 3391 3392 static void 3393 sd_read_unit_properties(struct sd_lun *un) 3394 { 3395 /* 3396 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3397 * the "sd-config-list" property (from the sd.conf file) or if 3398 * there was not a match for the inquiry vid/pid. If this event 3399 * occurs the static driver configuration table is searched for 3400 * a match. 3401 */ 3402 ASSERT(un != NULL); 3403 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3404 sd_process_sdconf_table(un); 3405 } 3406 3407 /* check for LSI device */ 3408 sd_is_lsi(un); 3409 3410 3411 } 3412 3413 3414 /* 3415 * Function: sd_process_sdconf_file 3416 * 3417 * Description: Use ddi_getlongprop to obtain the properties from the 3418 * driver's config file (ie, sd.conf) and update the driver 3419 * soft state structure accordingly. 3420 * 3421 * Arguments: un - driver soft state (unit) structure 3422 * 3423 * Return Code: SD_SUCCESS - The properties were successfully set according 3424 * to the driver configuration file. 3425 * SD_FAILURE - The driver config list was not obtained or 3426 * there was no vid/pid match. This indicates that 3427 * the static config table should be used. 3428 * 3429 * The config file has a property, "sd-config-list", which consists of 3430 * one or more duplets as follows: 3431 * 3432 * sd-config-list= 3433 * <duplet>, 3434 * [<duplet>,] 3435 * [<duplet>]; 3436 * 3437 * The structure of each duplet is as follows: 3438 * 3439 * <duplet>:= <vid+pid>,<data-property-name_list> 3440 * 3441 * The first entry of the duplet is the device ID string (the concatenated 3442 * vid & pid; not to be confused with a device_id). This is defined in 3443 * the same way as in the sd_disk_table. 3444 * 3445 * The second part of the duplet is a string that identifies a 3446 * data-property-name-list. The data-property-name-list is defined as 3447 * follows: 3448 * 3449 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3450 * 3451 * The syntax of <data-property-name> depends on the <version> field. 3452 * 3453 * If version = SD_CONF_VERSION_1 we have the following syntax: 3454 * 3455 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3456 * 3457 * where the prop0 value will be used to set prop0 if bit0 set in the 3458 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3459 * 3460 */ 3461 3462 static int 3463 sd_process_sdconf_file(struct sd_lun *un) 3464 { 3465 char *config_list = NULL; 3466 int config_list_len; 3467 int len; 3468 int dupletlen = 0; 3469 char *vidptr; 3470 int vidlen; 3471 char *dnlist_ptr; 3472 char *dataname_ptr; 3473 int dnlist_len; 3474 int dataname_len; 3475 int *data_list; 3476 int data_list_len; 3477 int rval = SD_FAILURE; 3478 int i; 3479 3480 ASSERT(un != NULL); 3481 3482 /* Obtain the configuration list associated with the .conf file */ 3483 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3484 sd_config_list, (caddr_t)&config_list, &config_list_len) 3485 != DDI_PROP_SUCCESS) { 3486 return (SD_FAILURE); 3487 } 3488 3489 /* 3490 * Compare vids in each duplet to the inquiry vid - if a match is 3491 * made, get the data value and update the soft state structure 3492 * accordingly. 3493 * 3494 * Note: This algorithm is complex and difficult to maintain. It should 3495 * be replaced with a more robust implementation. 3496 */ 3497 for (len = config_list_len, vidptr = config_list; len > 0; 3498 vidptr += dupletlen, len -= dupletlen) { 3499 /* 3500 * Note: The assumption here is that each vid entry is on 3501 * a unique line from its associated duplet. 3502 */ 3503 vidlen = dupletlen = (int)strlen(vidptr); 3504 if ((vidlen == 0) || 3505 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3506 dupletlen++; 3507 continue; 3508 } 3509 3510 /* 3511 * dnlist contains 1 or more blank separated 3512 * data-property-name entries 3513 */ 3514 dnlist_ptr = vidptr + vidlen + 1; 3515 dnlist_len = (int)strlen(dnlist_ptr); 3516 dupletlen += dnlist_len + 2; 3517 3518 /* 3519 * Set a pointer for the first data-property-name 3520 * entry in the list 3521 */ 3522 dataname_ptr = dnlist_ptr; 3523 dataname_len = 0; 3524 3525 /* 3526 * Loop through all data-property-name entries in the 3527 * data-property-name-list setting the properties for each. 3528 */ 3529 while (dataname_len < dnlist_len) { 3530 int version; 3531 3532 /* 3533 * Determine the length of the current 3534 * data-property-name entry by indexing until a 3535 * blank or NULL is encountered. When the space is 3536 * encountered reset it to a NULL for compliance 3537 * with ddi_getlongprop(). 3538 */ 3539 for (i = 0; ((dataname_ptr[i] != ' ') && 3540 (dataname_ptr[i] != '\0')); i++) { 3541 ; 3542 } 3543 3544 dataname_len += i; 3545 /* If not null terminated, Make it so */ 3546 if (dataname_ptr[i] == ' ') { 3547 dataname_ptr[i] = '\0'; 3548 } 3549 dataname_len++; 3550 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3551 "sd_process_sdconf_file: disk:%s, data:%s\n", 3552 vidptr, dataname_ptr); 3553 3554 /* Get the data list */ 3555 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3556 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3557 != DDI_PROP_SUCCESS) { 3558 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3559 "sd_process_sdconf_file: data property (%s)" 3560 " has no value\n", dataname_ptr); 3561 dataname_ptr = dnlist_ptr + dataname_len; 3562 continue; 3563 } 3564 3565 version = data_list[0]; 3566 3567 if (version == SD_CONF_VERSION_1) { 3568 sd_tunables values; 3569 3570 /* Set the properties */ 3571 if (sd_chk_vers1_data(un, data_list[1], 3572 &data_list[2], data_list_len, dataname_ptr) 3573 == SD_SUCCESS) { 3574 sd_get_tunables_from_conf(un, 3575 data_list[1], &data_list[2], 3576 &values); 3577 sd_set_vers1_properties(un, 3578 data_list[1], &values); 3579 rval = SD_SUCCESS; 3580 } else { 3581 rval = SD_FAILURE; 3582 } 3583 } else { 3584 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3585 "data property %s version 0x%x is invalid.", 3586 dataname_ptr, version); 3587 rval = SD_FAILURE; 3588 } 3589 kmem_free(data_list, data_list_len); 3590 dataname_ptr = dnlist_ptr + dataname_len; 3591 } 3592 } 3593 3594 /* free up the memory allocated by ddi_getlongprop */ 3595 if (config_list) { 3596 kmem_free(config_list, config_list_len); 3597 } 3598 3599 return (rval); 3600 } 3601 3602 /* 3603 * Function: sd_get_tunables_from_conf() 3604 * 3605 * 3606 * This function reads the data list from the sd.conf file and pulls 3607 * the values that can have numeric values as arguments and places 3608 * the values in the appropriate sd_tunables member. 3609 * Since the order of the data list members varies across platforms 3610 * This function reads them from the data list in a platform specific 3611 * order and places them into the correct sd_tunable member that is 3612 * consistent across all platforms. 3613 */ 3614 static void 3615 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3616 sd_tunables *values) 3617 { 3618 int i; 3619 int mask; 3620 3621 bzero(values, sizeof (sd_tunables)); 3622 3623 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3624 3625 mask = 1 << i; 3626 if (mask > flags) { 3627 break; 3628 } 3629 3630 switch (mask & flags) { 3631 case 0: /* This mask bit not set in flags */ 3632 continue; 3633 case SD_CONF_BSET_THROTTLE: 3634 values->sdt_throttle = data_list[i]; 3635 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3636 "sd_get_tunables_from_conf: throttle = %d\n", 3637 values->sdt_throttle); 3638 break; 3639 case SD_CONF_BSET_CTYPE: 3640 values->sdt_ctype = data_list[i]; 3641 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3642 "sd_get_tunables_from_conf: ctype = %d\n", 3643 values->sdt_ctype); 3644 break; 3645 case SD_CONF_BSET_NRR_COUNT: 3646 values->sdt_not_rdy_retries = data_list[i]; 3647 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3648 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3649 values->sdt_not_rdy_retries); 3650 break; 3651 case SD_CONF_BSET_BSY_RETRY_COUNT: 3652 values->sdt_busy_retries = data_list[i]; 3653 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3654 "sd_get_tunables_from_conf: busy_retries = %d\n", 3655 values->sdt_busy_retries); 3656 break; 3657 case SD_CONF_BSET_RST_RETRIES: 3658 values->sdt_reset_retries = data_list[i]; 3659 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3660 "sd_get_tunables_from_conf: reset_retries = %d\n", 3661 values->sdt_reset_retries); 3662 break; 3663 case SD_CONF_BSET_RSV_REL_TIME: 3664 values->sdt_reserv_rel_time = data_list[i]; 3665 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3666 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3667 values->sdt_reserv_rel_time); 3668 break; 3669 case SD_CONF_BSET_MIN_THROTTLE: 3670 values->sdt_min_throttle = data_list[i]; 3671 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3672 "sd_get_tunables_from_conf: min_throttle = %d\n", 3673 values->sdt_min_throttle); 3674 break; 3675 case SD_CONF_BSET_DISKSORT_DISABLED: 3676 values->sdt_disk_sort_dis = data_list[i]; 3677 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3678 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3679 values->sdt_disk_sort_dis); 3680 break; 3681 case SD_CONF_BSET_LUN_RESET_ENABLED: 3682 values->sdt_lun_reset_enable = data_list[i]; 3683 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3684 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3685 "\n", values->sdt_lun_reset_enable); 3686 break; 3687 case SD_CONF_BSET_CACHE_IS_NV: 3688 values->sdt_suppress_cache_flush = data_list[i]; 3689 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3690 "sd_get_tunables_from_conf: \ 3691 suppress_cache_flush = %d" 3692 "\n", values->sdt_suppress_cache_flush); 3693 break; 3694 } 3695 } 3696 } 3697 3698 /* 3699 * Function: sd_process_sdconf_table 3700 * 3701 * Description: Search the static configuration table for a match on the 3702 * inquiry vid/pid and update the driver soft state structure 3703 * according to the table property values for the device. 3704 * 3705 * The form of a configuration table entry is: 3706 * <vid+pid>,<flags>,<property-data> 3707 * "SEAGATE ST42400N",1,0x40000, 3708 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 3709 * 3710 * Arguments: un - driver soft state (unit) structure 3711 */ 3712 3713 static void 3714 sd_process_sdconf_table(struct sd_lun *un) 3715 { 3716 char *id = NULL; 3717 int table_index; 3718 int idlen; 3719 3720 ASSERT(un != NULL); 3721 for (table_index = 0; table_index < sd_disk_table_size; 3722 table_index++) { 3723 id = sd_disk_table[table_index].device_id; 3724 idlen = strlen(id); 3725 if (idlen == 0) { 3726 continue; 3727 } 3728 3729 /* 3730 * The static configuration table currently does not 3731 * implement version 10 properties. Additionally, 3732 * multiple data-property-name entries are not 3733 * implemented in the static configuration table. 3734 */ 3735 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3736 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3737 "sd_process_sdconf_table: disk %s\n", id); 3738 sd_set_vers1_properties(un, 3739 sd_disk_table[table_index].flags, 3740 sd_disk_table[table_index].properties); 3741 break; 3742 } 3743 } 3744 } 3745 3746 3747 /* 3748 * Function: sd_sdconf_id_match 3749 * 3750 * Description: This local function implements a case sensitive vid/pid 3751 * comparison as well as the boundary cases of wild card and 3752 * multiple blanks. 3753 * 3754 * Note: An implicit assumption made here is that the scsi 3755 * inquiry structure will always keep the vid, pid and 3756 * revision strings in consecutive sequence, so they can be 3757 * read as a single string. If this assumption is not the 3758 * case, a separate string, to be used for the check, needs 3759 * to be built with these strings concatenated. 3760 * 3761 * Arguments: un - driver soft state (unit) structure 3762 * id - table or config file vid/pid 3763 * idlen - length of the vid/pid (bytes) 3764 * 3765 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3766 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3767 */ 3768 3769 static int 3770 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3771 { 3772 struct scsi_inquiry *sd_inq; 3773 int rval = SD_SUCCESS; 3774 3775 ASSERT(un != NULL); 3776 sd_inq = un->un_sd->sd_inq; 3777 ASSERT(id != NULL); 3778 3779 /* 3780 * We use the inq_vid as a pointer to a buffer containing the 3781 * vid and pid and use the entire vid/pid length of the table 3782 * entry for the comparison. This works because the inq_pid 3783 * data member follows inq_vid in the scsi_inquiry structure. 3784 */ 3785 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3786 /* 3787 * The user id string is compared to the inquiry vid/pid 3788 * using a case insensitive comparison and ignoring 3789 * multiple spaces. 3790 */ 3791 rval = sd_blank_cmp(un, id, idlen); 3792 if (rval != SD_SUCCESS) { 3793 /* 3794 * User id strings that start and end with a "*" 3795 * are a special case. These do not have a 3796 * specific vendor, and the product string can 3797 * appear anywhere in the 16 byte PID portion of 3798 * the inquiry data. This is a simple strstr() 3799 * type search for the user id in the inquiry data. 3800 */ 3801 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3802 char *pidptr = &id[1]; 3803 int i; 3804 int j; 3805 int pidstrlen = idlen - 2; 3806 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3807 pidstrlen; 3808 3809 if (j < 0) { 3810 return (SD_FAILURE); 3811 } 3812 for (i = 0; i < j; i++) { 3813 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3814 pidptr, pidstrlen) == 0) { 3815 rval = SD_SUCCESS; 3816 break; 3817 } 3818 } 3819 } 3820 } 3821 } 3822 return (rval); 3823 } 3824 3825 3826 /* 3827 * Function: sd_blank_cmp 3828 * 3829 * Description: If the id string starts and ends with a space, treat 3830 * multiple consecutive spaces as equivalent to a single 3831 * space. For example, this causes a sd_disk_table entry 3832 * of " NEC CDROM " to match a device's id string of 3833 * "NEC CDROM". 3834 * 3835 * Note: The success exit condition for this routine is if 3836 * the pointer to the table entry is '\0' and the cnt of 3837 * the inquiry length is zero. This will happen if the inquiry 3838 * string returned by the device is padded with spaces to be 3839 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3840 * SCSI spec states that the inquiry string is to be padded with 3841 * spaces. 3842 * 3843 * Arguments: un - driver soft state (unit) structure 3844 * id - table or config file vid/pid 3845 * idlen - length of the vid/pid (bytes) 3846 * 3847 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3848 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3849 */ 3850 3851 static int 3852 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3853 { 3854 char *p1; 3855 char *p2; 3856 int cnt; 3857 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3858 sizeof (SD_INQUIRY(un)->inq_pid); 3859 3860 ASSERT(un != NULL); 3861 p2 = un->un_sd->sd_inq->inq_vid; 3862 ASSERT(id != NULL); 3863 p1 = id; 3864 3865 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3866 /* 3867 * Note: string p1 is terminated by a NUL but string p2 3868 * isn't. The end of p2 is determined by cnt. 3869 */ 3870 for (;;) { 3871 /* skip over any extra blanks in both strings */ 3872 while ((*p1 != '\0') && (*p1 == ' ')) { 3873 p1++; 3874 } 3875 while ((cnt != 0) && (*p2 == ' ')) { 3876 p2++; 3877 cnt--; 3878 } 3879 3880 /* compare the two strings */ 3881 if ((cnt == 0) || 3882 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3883 break; 3884 } 3885 while ((cnt > 0) && 3886 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3887 p1++; 3888 p2++; 3889 cnt--; 3890 } 3891 } 3892 } 3893 3894 /* return SD_SUCCESS if both strings match */ 3895 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3896 } 3897 3898 3899 /* 3900 * Function: sd_chk_vers1_data 3901 * 3902 * Description: Verify the version 1 device properties provided by the 3903 * user via the configuration file 3904 * 3905 * Arguments: un - driver soft state (unit) structure 3906 * flags - integer mask indicating properties to be set 3907 * prop_list - integer list of property values 3908 * list_len - length of user provided data 3909 * 3910 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3911 * SD_FAILURE - Indicates the user provided data is invalid 3912 */ 3913 3914 static int 3915 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3916 int list_len, char *dataname_ptr) 3917 { 3918 int i; 3919 int mask = 1; 3920 int index = 0; 3921 3922 ASSERT(un != NULL); 3923 3924 /* Check for a NULL property name and list */ 3925 if (dataname_ptr == NULL) { 3926 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3927 "sd_chk_vers1_data: NULL data property name."); 3928 return (SD_FAILURE); 3929 } 3930 if (prop_list == NULL) { 3931 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3932 "sd_chk_vers1_data: %s NULL data property list.", 3933 dataname_ptr); 3934 return (SD_FAILURE); 3935 } 3936 3937 /* Display a warning if undefined bits are set in the flags */ 3938 if (flags & ~SD_CONF_BIT_MASK) { 3939 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3940 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3941 "Properties not set.", 3942 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3943 return (SD_FAILURE); 3944 } 3945 3946 /* 3947 * Verify the length of the list by identifying the highest bit set 3948 * in the flags and validating that the property list has a length 3949 * up to the index of this bit. 3950 */ 3951 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3952 if (flags & mask) { 3953 index++; 3954 } 3955 mask = 1 << i; 3956 } 3957 if ((list_len / sizeof (int)) < (index + 2)) { 3958 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3959 "sd_chk_vers1_data: " 3960 "Data property list %s size is incorrect. " 3961 "Properties not set.", dataname_ptr); 3962 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3963 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3964 return (SD_FAILURE); 3965 } 3966 return (SD_SUCCESS); 3967 } 3968 3969 3970 /* 3971 * Function: sd_set_vers1_properties 3972 * 3973 * Description: Set version 1 device properties based on a property list 3974 * retrieved from the driver configuration file or static 3975 * configuration table. Version 1 properties have the format: 3976 * 3977 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3978 * 3979 * where the prop0 value will be used to set prop0 if bit0 3980 * is set in the flags 3981 * 3982 * Arguments: un - driver soft state (unit) structure 3983 * flags - integer mask indicating properties to be set 3984 * prop_list - integer list of property values 3985 */ 3986 3987 static void 3988 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3989 { 3990 ASSERT(un != NULL); 3991 3992 /* 3993 * Set the flag to indicate cache is to be disabled. An attempt 3994 * to disable the cache via sd_cache_control() will be made 3995 * later during attach once the basic initialization is complete. 3996 */ 3997 if (flags & SD_CONF_BSET_NOCACHE) { 3998 un->un_f_opt_disable_cache = TRUE; 3999 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4000 "sd_set_vers1_properties: caching disabled flag set\n"); 4001 } 4002 4003 /* CD-specific configuration parameters */ 4004 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4005 un->un_f_cfg_playmsf_bcd = TRUE; 4006 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4007 "sd_set_vers1_properties: playmsf_bcd set\n"); 4008 } 4009 if (flags & SD_CONF_BSET_READSUB_BCD) { 4010 un->un_f_cfg_readsub_bcd = TRUE; 4011 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4012 "sd_set_vers1_properties: readsub_bcd set\n"); 4013 } 4014 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4015 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4016 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4017 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4018 } 4019 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4020 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4021 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4022 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4023 } 4024 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4025 un->un_f_cfg_no_read_header = TRUE; 4026 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4027 "sd_set_vers1_properties: no_read_header set\n"); 4028 } 4029 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4030 un->un_f_cfg_read_cd_xd4 = TRUE; 4031 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4032 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4033 } 4034 4035 /* Support for devices which do not have valid/unique serial numbers */ 4036 if (flags & SD_CONF_BSET_FAB_DEVID) { 4037 un->un_f_opt_fab_devid = TRUE; 4038 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4039 "sd_set_vers1_properties: fab_devid bit set\n"); 4040 } 4041 4042 /* Support for user throttle configuration */ 4043 if (flags & SD_CONF_BSET_THROTTLE) { 4044 ASSERT(prop_list != NULL); 4045 un->un_saved_throttle = un->un_throttle = 4046 prop_list->sdt_throttle; 4047 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4048 "sd_set_vers1_properties: throttle set to %d\n", 4049 prop_list->sdt_throttle); 4050 } 4051 4052 /* Set the per disk retry count according to the conf file or table. */ 4053 if (flags & SD_CONF_BSET_NRR_COUNT) { 4054 ASSERT(prop_list != NULL); 4055 if (prop_list->sdt_not_rdy_retries) { 4056 un->un_notready_retry_count = 4057 prop_list->sdt_not_rdy_retries; 4058 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4059 "sd_set_vers1_properties: not ready retry count" 4060 " set to %d\n", un->un_notready_retry_count); 4061 } 4062 } 4063 4064 /* The controller type is reported for generic disk driver ioctls */ 4065 if (flags & SD_CONF_BSET_CTYPE) { 4066 ASSERT(prop_list != NULL); 4067 switch (prop_list->sdt_ctype) { 4068 case CTYPE_CDROM: 4069 un->un_ctype = prop_list->sdt_ctype; 4070 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4071 "sd_set_vers1_properties: ctype set to " 4072 "CTYPE_CDROM\n"); 4073 break; 4074 case CTYPE_CCS: 4075 un->un_ctype = prop_list->sdt_ctype; 4076 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4077 "sd_set_vers1_properties: ctype set to " 4078 "CTYPE_CCS\n"); 4079 break; 4080 case CTYPE_ROD: /* RW optical */ 4081 un->un_ctype = prop_list->sdt_ctype; 4082 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4083 "sd_set_vers1_properties: ctype set to " 4084 "CTYPE_ROD\n"); 4085 break; 4086 default: 4087 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4088 "sd_set_vers1_properties: Could not set " 4089 "invalid ctype value (%d)", 4090 prop_list->sdt_ctype); 4091 } 4092 } 4093 4094 /* Purple failover timeout */ 4095 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4096 ASSERT(prop_list != NULL); 4097 un->un_busy_retry_count = 4098 prop_list->sdt_busy_retries; 4099 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4100 "sd_set_vers1_properties: " 4101 "busy retry count set to %d\n", 4102 un->un_busy_retry_count); 4103 } 4104 4105 /* Purple reset retry count */ 4106 if (flags & SD_CONF_BSET_RST_RETRIES) { 4107 ASSERT(prop_list != NULL); 4108 un->un_reset_retry_count = 4109 prop_list->sdt_reset_retries; 4110 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4111 "sd_set_vers1_properties: " 4112 "reset retry count set to %d\n", 4113 un->un_reset_retry_count); 4114 } 4115 4116 /* Purple reservation release timeout */ 4117 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4118 ASSERT(prop_list != NULL); 4119 un->un_reserve_release_time = 4120 prop_list->sdt_reserv_rel_time; 4121 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4122 "sd_set_vers1_properties: " 4123 "reservation release timeout set to %d\n", 4124 un->un_reserve_release_time); 4125 } 4126 4127 /* 4128 * Driver flag telling the driver to verify that no commands are pending 4129 * for a device before issuing a Test Unit Ready. This is a workaround 4130 * for a firmware bug in some Seagate eliteI drives. 4131 */ 4132 if (flags & SD_CONF_BSET_TUR_CHECK) { 4133 un->un_f_cfg_tur_check = TRUE; 4134 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4135 "sd_set_vers1_properties: tur queue check set\n"); 4136 } 4137 4138 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4139 un->un_min_throttle = prop_list->sdt_min_throttle; 4140 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4141 "sd_set_vers1_properties: min throttle set to %d\n", 4142 un->un_min_throttle); 4143 } 4144 4145 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4146 un->un_f_disksort_disabled = 4147 (prop_list->sdt_disk_sort_dis != 0) ? 4148 TRUE : FALSE; 4149 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4150 "sd_set_vers1_properties: disksort disabled " 4151 "flag set to %d\n", 4152 prop_list->sdt_disk_sort_dis); 4153 } 4154 4155 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4156 un->un_f_lun_reset_enabled = 4157 (prop_list->sdt_lun_reset_enable != 0) ? 4158 TRUE : FALSE; 4159 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4160 "sd_set_vers1_properties: lun reset enabled " 4161 "flag set to %d\n", 4162 prop_list->sdt_lun_reset_enable); 4163 } 4164 4165 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4166 un->un_f_suppress_cache_flush = 4167 (prop_list->sdt_suppress_cache_flush != 0) ? 4168 TRUE : FALSE; 4169 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4170 "sd_set_vers1_properties: suppress_cache_flush " 4171 "flag set to %d\n", 4172 prop_list->sdt_suppress_cache_flush); 4173 } 4174 4175 /* 4176 * Validate the throttle values. 4177 * If any of the numbers are invalid, set everything to defaults. 4178 */ 4179 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4180 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4181 (un->un_min_throttle > un->un_throttle)) { 4182 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4183 un->un_min_throttle = sd_min_throttle; 4184 } 4185 } 4186 4187 /* 4188 * Function: sd_is_lsi() 4189 * 4190 * Description: Check for lsi devices, step through the static device 4191 * table to match vid/pid. 4192 * 4193 * Args: un - ptr to sd_lun 4194 * 4195 * Notes: When creating new LSI property, need to add the new LSI property 4196 * to this function. 4197 */ 4198 static void 4199 sd_is_lsi(struct sd_lun *un) 4200 { 4201 char *id = NULL; 4202 int table_index; 4203 int idlen; 4204 void *prop; 4205 4206 ASSERT(un != NULL); 4207 for (table_index = 0; table_index < sd_disk_table_size; 4208 table_index++) { 4209 id = sd_disk_table[table_index].device_id; 4210 idlen = strlen(id); 4211 if (idlen == 0) { 4212 continue; 4213 } 4214 4215 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4216 prop = sd_disk_table[table_index].properties; 4217 if (prop == &lsi_properties || 4218 prop == &lsi_oem_properties || 4219 prop == &lsi_properties_scsi || 4220 prop == &symbios_properties) { 4221 un->un_f_cfg_is_lsi = TRUE; 4222 } 4223 break; 4224 } 4225 } 4226 } 4227 4228 /* 4229 * Function: sd_get_physical_geometry 4230 * 4231 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4232 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4233 * target, and use this information to initialize the physical 4234 * geometry cache specified by pgeom_p. 4235 * 4236 * MODE SENSE is an optional command, so failure in this case 4237 * does not necessarily denote an error. We want to use the 4238 * MODE SENSE commands to derive the physical geometry of the 4239 * device, but if either command fails, the logical geometry is 4240 * used as the fallback for disk label geometry in cmlb. 4241 * 4242 * This requires that un->un_blockcount and un->un_tgt_blocksize 4243 * have already been initialized for the current target and 4244 * that the current values be passed as args so that we don't 4245 * end up ever trying to use -1 as a valid value. This could 4246 * happen if either value is reset while we're not holding 4247 * the mutex. 4248 * 4249 * Arguments: un - driver soft state (unit) structure 4250 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4251 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4252 * to use the USCSI "direct" chain and bypass the normal 4253 * command waitq. 4254 * 4255 * Context: Kernel thread only (can sleep). 4256 */ 4257 4258 static int 4259 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4260 diskaddr_t capacity, int lbasize, int path_flag) 4261 { 4262 struct mode_format *page3p; 4263 struct mode_geometry *page4p; 4264 struct mode_header *headerp; 4265 int sector_size; 4266 int nsect; 4267 int nhead; 4268 int ncyl; 4269 int intrlv; 4270 int spc; 4271 diskaddr_t modesense_capacity; 4272 int rpm; 4273 int bd_len; 4274 int mode_header_length; 4275 uchar_t *p3bufp; 4276 uchar_t *p4bufp; 4277 int cdbsize; 4278 int ret = EIO; 4279 4280 ASSERT(un != NULL); 4281 4282 if (lbasize == 0) { 4283 if (ISCD(un)) { 4284 lbasize = 2048; 4285 } else { 4286 lbasize = un->un_sys_blocksize; 4287 } 4288 } 4289 pgeom_p->g_secsize = (unsigned short)lbasize; 4290 4291 /* 4292 * If the unit is a cd/dvd drive MODE SENSE page three 4293 * and MODE SENSE page four are reserved (see SBC spec 4294 * and MMC spec). To prevent soft errors just return 4295 * using the default LBA size. 4296 */ 4297 if (ISCD(un)) 4298 return (ret); 4299 4300 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4301 4302 /* 4303 * Retrieve MODE SENSE page 3 - Format Device Page 4304 */ 4305 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4306 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4307 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4308 != 0) { 4309 SD_ERROR(SD_LOG_COMMON, un, 4310 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4311 goto page3_exit; 4312 } 4313 4314 /* 4315 * Determine size of Block Descriptors in order to locate the mode 4316 * page data. ATAPI devices return 0, SCSI devices should return 4317 * MODE_BLK_DESC_LENGTH. 4318 */ 4319 headerp = (struct mode_header *)p3bufp; 4320 if (un->un_f_cfg_is_atapi == TRUE) { 4321 struct mode_header_grp2 *mhp = 4322 (struct mode_header_grp2 *)headerp; 4323 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4324 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4325 } else { 4326 mode_header_length = MODE_HEADER_LENGTH; 4327 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4328 } 4329 4330 if (bd_len > MODE_BLK_DESC_LENGTH) { 4331 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4332 "received unexpected bd_len of %d, page3\n", bd_len); 4333 goto page3_exit; 4334 } 4335 4336 page3p = (struct mode_format *) 4337 ((caddr_t)headerp + mode_header_length + bd_len); 4338 4339 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4340 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4341 "mode sense pg3 code mismatch %d\n", 4342 page3p->mode_page.code); 4343 goto page3_exit; 4344 } 4345 4346 /* 4347 * Use this physical geometry data only if BOTH MODE SENSE commands 4348 * complete successfully; otherwise, revert to the logical geometry. 4349 * So, we need to save everything in temporary variables. 4350 */ 4351 sector_size = BE_16(page3p->data_bytes_sect); 4352 4353 /* 4354 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4355 */ 4356 if (sector_size == 0) { 4357 sector_size = un->un_sys_blocksize; 4358 } else { 4359 sector_size &= ~(un->un_sys_blocksize - 1); 4360 } 4361 4362 nsect = BE_16(page3p->sect_track); 4363 intrlv = BE_16(page3p->interleave); 4364 4365 SD_INFO(SD_LOG_COMMON, un, 4366 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4367 SD_INFO(SD_LOG_COMMON, un, 4368 " mode page: %d; nsect: %d; sector size: %d;\n", 4369 page3p->mode_page.code, nsect, sector_size); 4370 SD_INFO(SD_LOG_COMMON, un, 4371 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4372 BE_16(page3p->track_skew), 4373 BE_16(page3p->cylinder_skew)); 4374 4375 4376 /* 4377 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4378 */ 4379 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4380 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4381 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4382 != 0) { 4383 SD_ERROR(SD_LOG_COMMON, un, 4384 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4385 goto page4_exit; 4386 } 4387 4388 /* 4389 * Determine size of Block Descriptors in order to locate the mode 4390 * page data. ATAPI devices return 0, SCSI devices should return 4391 * MODE_BLK_DESC_LENGTH. 4392 */ 4393 headerp = (struct mode_header *)p4bufp; 4394 if (un->un_f_cfg_is_atapi == TRUE) { 4395 struct mode_header_grp2 *mhp = 4396 (struct mode_header_grp2 *)headerp; 4397 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4398 } else { 4399 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4400 } 4401 4402 if (bd_len > MODE_BLK_DESC_LENGTH) { 4403 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4404 "received unexpected bd_len of %d, page4\n", bd_len); 4405 goto page4_exit; 4406 } 4407 4408 page4p = (struct mode_geometry *) 4409 ((caddr_t)headerp + mode_header_length + bd_len); 4410 4411 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4412 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4413 "mode sense pg4 code mismatch %d\n", 4414 page4p->mode_page.code); 4415 goto page4_exit; 4416 } 4417 4418 /* 4419 * Stash the data now, after we know that both commands completed. 4420 */ 4421 4422 4423 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4424 spc = nhead * nsect; 4425 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4426 rpm = BE_16(page4p->rpm); 4427 4428 modesense_capacity = spc * ncyl; 4429 4430 SD_INFO(SD_LOG_COMMON, un, 4431 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4432 SD_INFO(SD_LOG_COMMON, un, 4433 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4434 SD_INFO(SD_LOG_COMMON, un, 4435 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4436 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4437 (void *)pgeom_p, capacity); 4438 4439 /* 4440 * Compensate if the drive's geometry is not rectangular, i.e., 4441 * the product of C * H * S returned by MODE SENSE >= that returned 4442 * by read capacity. This is an idiosyncrasy of the original x86 4443 * disk subsystem. 4444 */ 4445 if (modesense_capacity >= capacity) { 4446 SD_INFO(SD_LOG_COMMON, un, 4447 "sd_get_physical_geometry: adjusting acyl; " 4448 "old: %d; new: %d\n", pgeom_p->g_acyl, 4449 (modesense_capacity - capacity + spc - 1) / spc); 4450 if (sector_size != 0) { 4451 /* 1243403: NEC D38x7 drives don't support sec size */ 4452 pgeom_p->g_secsize = (unsigned short)sector_size; 4453 } 4454 pgeom_p->g_nsect = (unsigned short)nsect; 4455 pgeom_p->g_nhead = (unsigned short)nhead; 4456 pgeom_p->g_capacity = capacity; 4457 pgeom_p->g_acyl = 4458 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 4459 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 4460 } 4461 4462 pgeom_p->g_rpm = (unsigned short)rpm; 4463 pgeom_p->g_intrlv = (unsigned short)intrlv; 4464 ret = 0; 4465 4466 SD_INFO(SD_LOG_COMMON, un, 4467 "sd_get_physical_geometry: mode sense geometry:\n"); 4468 SD_INFO(SD_LOG_COMMON, un, 4469 " nsect: %d; sector size: %d; interlv: %d\n", 4470 nsect, sector_size, intrlv); 4471 SD_INFO(SD_LOG_COMMON, un, 4472 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 4473 nhead, ncyl, rpm, modesense_capacity); 4474 SD_INFO(SD_LOG_COMMON, un, 4475 "sd_get_physical_geometry: (cached)\n"); 4476 SD_INFO(SD_LOG_COMMON, un, 4477 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4478 pgeom_p->g_ncyl, pgeom_p->g_acyl, 4479 pgeom_p->g_nhead, pgeom_p->g_nsect); 4480 SD_INFO(SD_LOG_COMMON, un, 4481 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 4482 pgeom_p->g_secsize, pgeom_p->g_capacity, 4483 pgeom_p->g_intrlv, pgeom_p->g_rpm); 4484 4485 page4_exit: 4486 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 4487 page3_exit: 4488 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 4489 4490 return (ret); 4491 } 4492 4493 /* 4494 * Function: sd_get_virtual_geometry 4495 * 4496 * Description: Ask the controller to tell us about the target device. 4497 * 4498 * Arguments: un - pointer to softstate 4499 * capacity - disk capacity in #blocks 4500 * lbasize - disk block size in bytes 4501 * 4502 * Context: Kernel thread only 4503 */ 4504 4505 static int 4506 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 4507 diskaddr_t capacity, int lbasize) 4508 { 4509 uint_t geombuf; 4510 int spc; 4511 4512 ASSERT(un != NULL); 4513 4514 /* Set sector size, and total number of sectors */ 4515 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 4516 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 4517 4518 /* Let the HBA tell us its geometry */ 4519 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 4520 4521 /* A value of -1 indicates an undefined "geometry" property */ 4522 if (geombuf == (-1)) { 4523 return (EINVAL); 4524 } 4525 4526 /* Initialize the logical geometry cache. */ 4527 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 4528 lgeom_p->g_nsect = geombuf & 0xffff; 4529 lgeom_p->g_secsize = un->un_sys_blocksize; 4530 4531 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 4532 4533 /* 4534 * Note: The driver originally converted the capacity value from 4535 * target blocks to system blocks. However, the capacity value passed 4536 * to this routine is already in terms of system blocks (this scaling 4537 * is done when the READ CAPACITY command is issued and processed). 4538 * This 'error' may have gone undetected because the usage of g_ncyl 4539 * (which is based upon g_capacity) is very limited within the driver 4540 */ 4541 lgeom_p->g_capacity = capacity; 4542 4543 /* 4544 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 4545 * hba may return zero values if the device has been removed. 4546 */ 4547 if (spc == 0) { 4548 lgeom_p->g_ncyl = 0; 4549 } else { 4550 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 4551 } 4552 lgeom_p->g_acyl = 0; 4553 4554 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 4555 return (0); 4556 4557 } 4558 /* 4559 * Function: sd_update_block_info 4560 * 4561 * Description: Calculate a byte count to sector count bitshift value 4562 * from sector size. 4563 * 4564 * Arguments: un: unit struct. 4565 * lbasize: new target sector size 4566 * capacity: new target capacity, ie. block count 4567 * 4568 * Context: Kernel thread context 4569 */ 4570 4571 static void 4572 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 4573 { 4574 if (lbasize != 0) { 4575 un->un_tgt_blocksize = lbasize; 4576 un->un_f_tgt_blocksize_is_valid = TRUE; 4577 } 4578 4579 if (capacity != 0) { 4580 un->un_blockcount = capacity; 4581 un->un_f_blockcount_is_valid = TRUE; 4582 } 4583 } 4584 4585 4586 /* 4587 * Function: sd_register_devid 4588 * 4589 * Description: This routine will obtain the device id information from the 4590 * target, obtain the serial number, and register the device 4591 * id with the ddi framework. 4592 * 4593 * Arguments: devi - the system's dev_info_t for the device. 4594 * un - driver soft state (unit) structure 4595 * reservation_flag - indicates if a reservation conflict 4596 * occurred during attach 4597 * 4598 * Context: Kernel Thread 4599 */ 4600 static void 4601 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 4602 { 4603 int rval = 0; 4604 uchar_t *inq80 = NULL; 4605 size_t inq80_len = MAX_INQUIRY_SIZE; 4606 size_t inq80_resid = 0; 4607 uchar_t *inq83 = NULL; 4608 size_t inq83_len = MAX_INQUIRY_SIZE; 4609 size_t inq83_resid = 0; 4610 int dlen, len; 4611 char *sn; 4612 4613 ASSERT(un != NULL); 4614 ASSERT(mutex_owned(SD_MUTEX(un))); 4615 ASSERT((SD_DEVINFO(un)) == devi); 4616 4617 /* 4618 * If transport has already registered a devid for this target 4619 * then that takes precedence over the driver's determination 4620 * of the devid. 4621 */ 4622 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 4623 ASSERT(un->un_devid); 4624 return; /* use devid registered by the transport */ 4625 } 4626 4627 /* 4628 * This is the case of antiquated Sun disk drives that have the 4629 * FAB_DEVID property set in the disk_table. These drives 4630 * manage the devid's by storing them in last 2 available sectors 4631 * on the drive and have them fabricated by the ddi layer by calling 4632 * ddi_devid_init and passing the DEVID_FAB flag. 4633 */ 4634 if (un->un_f_opt_fab_devid == TRUE) { 4635 /* 4636 * Depending on EINVAL isn't reliable, since a reserved disk 4637 * may result in invalid geometry, so check to make sure a 4638 * reservation conflict did not occur during attach. 4639 */ 4640 if ((sd_get_devid(un) == EINVAL) && 4641 (reservation_flag != SD_TARGET_IS_RESERVED)) { 4642 /* 4643 * The devid is invalid AND there is no reservation 4644 * conflict. Fabricate a new devid. 4645 */ 4646 (void) sd_create_devid(un); 4647 } 4648 4649 /* Register the devid if it exists */ 4650 if (un->un_devid != NULL) { 4651 (void) ddi_devid_register(SD_DEVINFO(un), 4652 un->un_devid); 4653 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4654 "sd_register_devid: Devid Fabricated\n"); 4655 } 4656 return; 4657 } 4658 4659 /* 4660 * We check the availability of the World Wide Name (0x83) and Unit 4661 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 4662 * un_vpd_page_mask from them, we decide which way to get the WWN. If 4663 * 0x83 is available, that is the best choice. Our next choice is 4664 * 0x80. If neither are available, we munge the devid from the device 4665 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 4666 * to fabricate a devid for non-Sun qualified disks. 4667 */ 4668 if (sd_check_vpd_page_support(un) == 0) { 4669 /* collect page 80 data if available */ 4670 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 4671 4672 mutex_exit(SD_MUTEX(un)); 4673 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 4674 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 4675 0x01, 0x80, &inq80_resid); 4676 4677 if (rval != 0) { 4678 kmem_free(inq80, inq80_len); 4679 inq80 = NULL; 4680 inq80_len = 0; 4681 } else if (ddi_prop_exists( 4682 DDI_DEV_T_NONE, SD_DEVINFO(un), 4683 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 4684 INQUIRY_SERIAL_NO) == 0) { 4685 /* 4686 * If we don't already have a serial number 4687 * property, do quick verify of data returned 4688 * and define property. 4689 */ 4690 dlen = inq80_len - inq80_resid; 4691 len = (size_t)inq80[3]; 4692 if ((dlen >= 4) && ((len + 4) <= dlen)) { 4693 /* 4694 * Ensure sn termination, skip leading 4695 * blanks, and create property 4696 * 'inquiry-serial-no'. 4697 */ 4698 sn = (char *)&inq80[4]; 4699 sn[len] = 0; 4700 while (*sn && (*sn == ' ')) 4701 sn++; 4702 if (*sn) { 4703 (void) ddi_prop_update_string( 4704 DDI_DEV_T_NONE, 4705 SD_DEVINFO(un), 4706 INQUIRY_SERIAL_NO, sn); 4707 } 4708 } 4709 } 4710 mutex_enter(SD_MUTEX(un)); 4711 } 4712 4713 /* collect page 83 data if available */ 4714 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 4715 mutex_exit(SD_MUTEX(un)); 4716 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 4717 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 4718 0x01, 0x83, &inq83_resid); 4719 4720 if (rval != 0) { 4721 kmem_free(inq83, inq83_len); 4722 inq83 = NULL; 4723 inq83_len = 0; 4724 } 4725 mutex_enter(SD_MUTEX(un)); 4726 } 4727 } 4728 4729 /* encode best devid possible based on data available */ 4730 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 4731 (char *)ddi_driver_name(SD_DEVINFO(un)), 4732 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 4733 inq80, inq80_len - inq80_resid, inq83, inq83_len - 4734 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 4735 4736 /* devid successfully encoded, register devid */ 4737 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 4738 4739 } else { 4740 /* 4741 * Unable to encode a devid based on data available. 4742 * This is not a Sun qualified disk. Older Sun disk 4743 * drives that have the SD_FAB_DEVID property 4744 * set in the disk_table and non Sun qualified 4745 * disks are treated in the same manner. These 4746 * drives manage the devid's by storing them in 4747 * last 2 available sectors on the drive and 4748 * have them fabricated by the ddi layer by 4749 * calling ddi_devid_init and passing the 4750 * DEVID_FAB flag. 4751 * Create a fabricate devid only if there's no 4752 * fabricate devid existed. 4753 */ 4754 if (sd_get_devid(un) == EINVAL) { 4755 (void) sd_create_devid(un); 4756 } 4757 un->un_f_opt_fab_devid = TRUE; 4758 4759 /* Register the devid if it exists */ 4760 if (un->un_devid != NULL) { 4761 (void) ddi_devid_register(SD_DEVINFO(un), 4762 un->un_devid); 4763 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4764 "sd_register_devid: devid fabricated using " 4765 "ddi framework\n"); 4766 } 4767 } 4768 4769 /* clean up resources */ 4770 if (inq80 != NULL) { 4771 kmem_free(inq80, inq80_len); 4772 } 4773 if (inq83 != NULL) { 4774 kmem_free(inq83, inq83_len); 4775 } 4776 } 4777 4778 4779 4780 /* 4781 * Function: sd_get_devid 4782 * 4783 * Description: This routine will return 0 if a valid device id has been 4784 * obtained from the target and stored in the soft state. If a 4785 * valid device id has not been previously read and stored, a 4786 * read attempt will be made. 4787 * 4788 * Arguments: un - driver soft state (unit) structure 4789 * 4790 * Return Code: 0 if we successfully get the device id 4791 * 4792 * Context: Kernel Thread 4793 */ 4794 4795 static int 4796 sd_get_devid(struct sd_lun *un) 4797 { 4798 struct dk_devid *dkdevid; 4799 ddi_devid_t tmpid; 4800 uint_t *ip; 4801 size_t sz; 4802 diskaddr_t blk; 4803 int status; 4804 int chksum; 4805 int i; 4806 size_t buffer_size; 4807 4808 ASSERT(un != NULL); 4809 ASSERT(mutex_owned(SD_MUTEX(un))); 4810 4811 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 4812 un); 4813 4814 if (un->un_devid != NULL) { 4815 return (0); 4816 } 4817 4818 mutex_exit(SD_MUTEX(un)); 4819 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4820 (void *)SD_PATH_DIRECT) != 0) { 4821 mutex_enter(SD_MUTEX(un)); 4822 return (EINVAL); 4823 } 4824 4825 /* 4826 * Read and verify device id, stored in the reserved cylinders at the 4827 * end of the disk. Backup label is on the odd sectors of the last 4828 * track of the last cylinder. Device id will be on track of the next 4829 * to last cylinder. 4830 */ 4831 mutex_enter(SD_MUTEX(un)); 4832 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 4833 mutex_exit(SD_MUTEX(un)); 4834 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 4835 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 4836 SD_PATH_DIRECT); 4837 if (status != 0) { 4838 goto error; 4839 } 4840 4841 /* Validate the revision */ 4842 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 4843 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 4844 status = EINVAL; 4845 goto error; 4846 } 4847 4848 /* Calculate the checksum */ 4849 chksum = 0; 4850 ip = (uint_t *)dkdevid; 4851 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 4852 i++) { 4853 chksum ^= ip[i]; 4854 } 4855 4856 /* Compare the checksums */ 4857 if (DKD_GETCHKSUM(dkdevid) != chksum) { 4858 status = EINVAL; 4859 goto error; 4860 } 4861 4862 /* Validate the device id */ 4863 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 4864 status = EINVAL; 4865 goto error; 4866 } 4867 4868 /* 4869 * Store the device id in the driver soft state 4870 */ 4871 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 4872 tmpid = kmem_alloc(sz, KM_SLEEP); 4873 4874 mutex_enter(SD_MUTEX(un)); 4875 4876 un->un_devid = tmpid; 4877 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 4878 4879 kmem_free(dkdevid, buffer_size); 4880 4881 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 4882 4883 return (status); 4884 error: 4885 mutex_enter(SD_MUTEX(un)); 4886 kmem_free(dkdevid, buffer_size); 4887 return (status); 4888 } 4889 4890 4891 /* 4892 * Function: sd_create_devid 4893 * 4894 * Description: This routine will fabricate the device id and write it 4895 * to the disk. 4896 * 4897 * Arguments: un - driver soft state (unit) structure 4898 * 4899 * Return Code: value of the fabricated device id 4900 * 4901 * Context: Kernel Thread 4902 */ 4903 4904 static ddi_devid_t 4905 sd_create_devid(struct sd_lun *un) 4906 { 4907 ASSERT(un != NULL); 4908 4909 /* Fabricate the devid */ 4910 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 4911 == DDI_FAILURE) { 4912 return (NULL); 4913 } 4914 4915 /* Write the devid to disk */ 4916 if (sd_write_deviceid(un) != 0) { 4917 ddi_devid_free(un->un_devid); 4918 un->un_devid = NULL; 4919 } 4920 4921 return (un->un_devid); 4922 } 4923 4924 4925 /* 4926 * Function: sd_write_deviceid 4927 * 4928 * Description: This routine will write the device id to the disk 4929 * reserved sector. 4930 * 4931 * Arguments: un - driver soft state (unit) structure 4932 * 4933 * Return Code: EINVAL 4934 * value returned by sd_send_scsi_cmd 4935 * 4936 * Context: Kernel Thread 4937 */ 4938 4939 static int 4940 sd_write_deviceid(struct sd_lun *un) 4941 { 4942 struct dk_devid *dkdevid; 4943 diskaddr_t blk; 4944 uint_t *ip, chksum; 4945 int status; 4946 int i; 4947 4948 ASSERT(mutex_owned(SD_MUTEX(un))); 4949 4950 mutex_exit(SD_MUTEX(un)); 4951 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4952 (void *)SD_PATH_DIRECT) != 0) { 4953 mutex_enter(SD_MUTEX(un)); 4954 return (-1); 4955 } 4956 4957 4958 /* Allocate the buffer */ 4959 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 4960 4961 /* Fill in the revision */ 4962 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 4963 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 4964 4965 /* Copy in the device id */ 4966 mutex_enter(SD_MUTEX(un)); 4967 bcopy(un->un_devid, &dkdevid->dkd_devid, 4968 ddi_devid_sizeof(un->un_devid)); 4969 mutex_exit(SD_MUTEX(un)); 4970 4971 /* Calculate the checksum */ 4972 chksum = 0; 4973 ip = (uint_t *)dkdevid; 4974 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 4975 i++) { 4976 chksum ^= ip[i]; 4977 } 4978 4979 /* Fill-in checksum */ 4980 DKD_FORMCHKSUM(chksum, dkdevid); 4981 4982 /* Write the reserved sector */ 4983 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 4984 SD_PATH_DIRECT); 4985 4986 kmem_free(dkdevid, un->un_sys_blocksize); 4987 4988 mutex_enter(SD_MUTEX(un)); 4989 return (status); 4990 } 4991 4992 4993 /* 4994 * Function: sd_check_vpd_page_support 4995 * 4996 * Description: This routine sends an inquiry command with the EVPD bit set and 4997 * a page code of 0x00 to the device. It is used to determine which 4998 * vital product pages are available to find the devid. We are 4999 * looking for pages 0x83 or 0x80. If we return a negative 1, the 5000 * device does not support that command. 5001 * 5002 * Arguments: un - driver soft state (unit) structure 5003 * 5004 * Return Code: 0 - success 5005 * 1 - check condition 5006 * 5007 * Context: This routine can sleep. 5008 */ 5009 5010 static int 5011 sd_check_vpd_page_support(struct sd_lun *un) 5012 { 5013 uchar_t *page_list = NULL; 5014 uchar_t page_length = 0xff; /* Use max possible length */ 5015 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5016 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5017 int rval = 0; 5018 int counter; 5019 5020 ASSERT(un != NULL); 5021 ASSERT(mutex_owned(SD_MUTEX(un))); 5022 5023 mutex_exit(SD_MUTEX(un)); 5024 5025 /* 5026 * We'll set the page length to the maximum to save figuring it out 5027 * with an additional call. 5028 */ 5029 page_list = kmem_zalloc(page_length, KM_SLEEP); 5030 5031 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 5032 page_code, NULL); 5033 5034 mutex_enter(SD_MUTEX(un)); 5035 5036 /* 5037 * Now we must validate that the device accepted the command, as some 5038 * drives do not support it. If the drive does support it, we will 5039 * return 0, and the supported pages will be in un_vpd_page_mask. If 5040 * not, we return -1. 5041 */ 5042 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5043 /* Loop to find one of the 2 pages we need */ 5044 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5045 5046 /* 5047 * Pages are returned in ascending order, and 0x83 is what we 5048 * are hoping for. 5049 */ 5050 while ((page_list[counter] <= 0x86) && 5051 (counter <= (page_list[VPD_PAGE_LENGTH] + 5052 VPD_HEAD_OFFSET))) { 5053 /* 5054 * Add 3 because page_list[3] is the number of 5055 * pages minus 3 5056 */ 5057 5058 switch (page_list[counter]) { 5059 case 0x00: 5060 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5061 break; 5062 case 0x80: 5063 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5064 break; 5065 case 0x81: 5066 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5067 break; 5068 case 0x82: 5069 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5070 break; 5071 case 0x83: 5072 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5073 break; 5074 case 0x86: 5075 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5076 break; 5077 } 5078 counter++; 5079 } 5080 5081 } else { 5082 rval = -1; 5083 5084 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5085 "sd_check_vpd_page_support: This drive does not implement " 5086 "VPD pages.\n"); 5087 } 5088 5089 kmem_free(page_list, page_length); 5090 5091 return (rval); 5092 } 5093 5094 5095 /* 5096 * Function: sd_setup_pm 5097 * 5098 * Description: Initialize Power Management on the device 5099 * 5100 * Context: Kernel Thread 5101 */ 5102 5103 static void 5104 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 5105 { 5106 uint_t log_page_size; 5107 uchar_t *log_page_data; 5108 int rval; 5109 5110 /* 5111 * Since we are called from attach, holding a mutex for 5112 * un is unnecessary. Because some of the routines called 5113 * from here require SD_MUTEX to not be held, assert this 5114 * right up front. 5115 */ 5116 ASSERT(!mutex_owned(SD_MUTEX(un))); 5117 /* 5118 * Since the sd device does not have the 'reg' property, 5119 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5120 * The following code is to tell cpr that this device 5121 * DOES need to be suspended and resumed. 5122 */ 5123 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5124 "pm-hardware-state", "needs-suspend-resume"); 5125 5126 /* 5127 * This complies with the new power management framework 5128 * for certain desktop machines. Create the pm_components 5129 * property as a string array property. 5130 */ 5131 if (un->un_f_pm_supported) { 5132 /* 5133 * not all devices have a motor, try it first. 5134 * some devices may return ILLEGAL REQUEST, some 5135 * will hang 5136 * The following START_STOP_UNIT is used to check if target 5137 * device has a motor. 5138 */ 5139 un->un_f_start_stop_supported = TRUE; 5140 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 5141 SD_PATH_DIRECT) != 0) { 5142 un->un_f_start_stop_supported = FALSE; 5143 } 5144 5145 /* 5146 * create pm properties anyways otherwise the parent can't 5147 * go to sleep 5148 */ 5149 (void) sd_create_pm_components(devi, un); 5150 un->un_f_pm_is_enabled = TRUE; 5151 return; 5152 } 5153 5154 if (!un->un_f_log_sense_supported) { 5155 un->un_power_level = SD_SPINDLE_ON; 5156 un->un_f_pm_is_enabled = FALSE; 5157 return; 5158 } 5159 5160 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 5161 5162 #ifdef SDDEBUG 5163 if (sd_force_pm_supported) { 5164 /* Force a successful result */ 5165 rval = 1; 5166 } 5167 #endif 5168 5169 /* 5170 * If the start-stop cycle counter log page is not supported 5171 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 5172 * then we should not create the pm_components property. 5173 */ 5174 if (rval == -1) { 5175 /* 5176 * Error. 5177 * Reading log sense failed, most likely this is 5178 * an older drive that does not support log sense. 5179 * If this fails auto-pm is not supported. 5180 */ 5181 un->un_power_level = SD_SPINDLE_ON; 5182 un->un_f_pm_is_enabled = FALSE; 5183 5184 } else if (rval == 0) { 5185 /* 5186 * Page not found. 5187 * The start stop cycle counter is implemented as page 5188 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5189 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5190 */ 5191 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 5192 /* 5193 * Page found, use this one. 5194 */ 5195 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5196 un->un_f_pm_is_enabled = TRUE; 5197 } else { 5198 /* 5199 * Error or page not found. 5200 * auto-pm is not supported for this device. 5201 */ 5202 un->un_power_level = SD_SPINDLE_ON; 5203 un->un_f_pm_is_enabled = FALSE; 5204 } 5205 } else { 5206 /* 5207 * Page found, use it. 5208 */ 5209 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 5210 un->un_f_pm_is_enabled = TRUE; 5211 } 5212 5213 5214 if (un->un_f_pm_is_enabled == TRUE) { 5215 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5216 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5217 5218 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5219 log_page_size, un->un_start_stop_cycle_page, 5220 0x01, 0, SD_PATH_DIRECT); 5221 #ifdef SDDEBUG 5222 if (sd_force_pm_supported) { 5223 /* Force a successful result */ 5224 rval = 0; 5225 } 5226 #endif 5227 5228 /* 5229 * If the Log sense for Page( Start/stop cycle counter page) 5230 * succeeds, then power management is supported and we can 5231 * enable auto-pm. 5232 */ 5233 if (rval == 0) { 5234 (void) sd_create_pm_components(devi, un); 5235 } else { 5236 un->un_power_level = SD_SPINDLE_ON; 5237 un->un_f_pm_is_enabled = FALSE; 5238 } 5239 5240 kmem_free(log_page_data, log_page_size); 5241 } 5242 } 5243 5244 5245 /* 5246 * Function: sd_create_pm_components 5247 * 5248 * Description: Initialize PM property. 5249 * 5250 * Context: Kernel thread context 5251 */ 5252 5253 static void 5254 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 5255 { 5256 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 5257 5258 ASSERT(!mutex_owned(SD_MUTEX(un))); 5259 5260 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 5261 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 5262 /* 5263 * When components are initially created they are idle, 5264 * power up any non-removables. 5265 * Note: the return value of pm_raise_power can't be used 5266 * for determining if PM should be enabled for this device. 5267 * Even if you check the return values and remove this 5268 * property created above, the PM framework will not honor the 5269 * change after the first call to pm_raise_power. Hence, 5270 * removal of that property does not help if pm_raise_power 5271 * fails. In the case of removable media, the start/stop 5272 * will fail if the media is not present. 5273 */ 5274 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 5275 SD_SPINDLE_ON) == DDI_SUCCESS)) { 5276 mutex_enter(SD_MUTEX(un)); 5277 un->un_power_level = SD_SPINDLE_ON; 5278 mutex_enter(&un->un_pm_mutex); 5279 /* Set to on and not busy. */ 5280 un->un_pm_count = 0; 5281 } else { 5282 mutex_enter(SD_MUTEX(un)); 5283 un->un_power_level = SD_SPINDLE_OFF; 5284 mutex_enter(&un->un_pm_mutex); 5285 /* Set to off. */ 5286 un->un_pm_count = -1; 5287 } 5288 mutex_exit(&un->un_pm_mutex); 5289 mutex_exit(SD_MUTEX(un)); 5290 } else { 5291 un->un_power_level = SD_SPINDLE_ON; 5292 un->un_f_pm_is_enabled = FALSE; 5293 } 5294 } 5295 5296 5297 /* 5298 * Function: sd_ddi_suspend 5299 * 5300 * Description: Performs system power-down operations. This includes 5301 * setting the drive state to indicate its suspended so 5302 * that no new commands will be accepted. Also, wait for 5303 * all commands that are in transport or queued to a timer 5304 * for retry to complete. All timeout threads are cancelled. 5305 * 5306 * Return Code: DDI_FAILURE or DDI_SUCCESS 5307 * 5308 * Context: Kernel thread context 5309 */ 5310 5311 static int 5312 sd_ddi_suspend(dev_info_t *devi) 5313 { 5314 struct sd_lun *un; 5315 clock_t wait_cmds_complete; 5316 5317 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5318 if (un == NULL) { 5319 return (DDI_FAILURE); 5320 } 5321 5322 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 5323 5324 mutex_enter(SD_MUTEX(un)); 5325 5326 /* Return success if the device is already suspended. */ 5327 if (un->un_state == SD_STATE_SUSPENDED) { 5328 mutex_exit(SD_MUTEX(un)); 5329 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5330 "device already suspended, exiting\n"); 5331 return (DDI_SUCCESS); 5332 } 5333 5334 /* Return failure if the device is being used by HA */ 5335 if (un->un_resvd_status & 5336 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 5337 mutex_exit(SD_MUTEX(un)); 5338 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5339 "device in use by HA, exiting\n"); 5340 return (DDI_FAILURE); 5341 } 5342 5343 /* 5344 * Return failure if the device is in a resource wait 5345 * or power changing state. 5346 */ 5347 if ((un->un_state == SD_STATE_RWAIT) || 5348 (un->un_state == SD_STATE_PM_CHANGING)) { 5349 mutex_exit(SD_MUTEX(un)); 5350 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5351 "device in resource wait state, exiting\n"); 5352 return (DDI_FAILURE); 5353 } 5354 5355 5356 un->un_save_state = un->un_last_state; 5357 New_state(un, SD_STATE_SUSPENDED); 5358 5359 /* 5360 * Wait for all commands that are in transport or queued to a timer 5361 * for retry to complete. 5362 * 5363 * While waiting, no new commands will be accepted or sent because of 5364 * the new state we set above. 5365 * 5366 * Wait till current operation has completed. If we are in the resource 5367 * wait state (with an intr outstanding) then we need to wait till the 5368 * intr completes and starts the next cmd. We want to wait for 5369 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 5370 */ 5371 wait_cmds_complete = ddi_get_lbolt() + 5372 (sd_wait_cmds_complete * drv_usectohz(1000000)); 5373 5374 while (un->un_ncmds_in_transport != 0) { 5375 /* 5376 * Fail if commands do not finish in the specified time. 5377 */ 5378 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 5379 wait_cmds_complete) == -1) { 5380 /* 5381 * Undo the state changes made above. Everything 5382 * must go back to it's original value. 5383 */ 5384 Restore_state(un); 5385 un->un_last_state = un->un_save_state; 5386 /* Wake up any threads that might be waiting. */ 5387 cv_broadcast(&un->un_suspend_cv); 5388 mutex_exit(SD_MUTEX(un)); 5389 SD_ERROR(SD_LOG_IO_PM, un, 5390 "sd_ddi_suspend: failed due to outstanding cmds\n"); 5391 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 5392 return (DDI_FAILURE); 5393 } 5394 } 5395 5396 /* 5397 * Cancel SCSI watch thread and timeouts, if any are active 5398 */ 5399 5400 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 5401 opaque_t temp_token = un->un_swr_token; 5402 mutex_exit(SD_MUTEX(un)); 5403 scsi_watch_suspend(temp_token); 5404 mutex_enter(SD_MUTEX(un)); 5405 } 5406 5407 if (un->un_reset_throttle_timeid != NULL) { 5408 timeout_id_t temp_id = un->un_reset_throttle_timeid; 5409 un->un_reset_throttle_timeid = NULL; 5410 mutex_exit(SD_MUTEX(un)); 5411 (void) untimeout(temp_id); 5412 mutex_enter(SD_MUTEX(un)); 5413 } 5414 5415 if (un->un_dcvb_timeid != NULL) { 5416 timeout_id_t temp_id = un->un_dcvb_timeid; 5417 un->un_dcvb_timeid = NULL; 5418 mutex_exit(SD_MUTEX(un)); 5419 (void) untimeout(temp_id); 5420 mutex_enter(SD_MUTEX(un)); 5421 } 5422 5423 mutex_enter(&un->un_pm_mutex); 5424 if (un->un_pm_timeid != NULL) { 5425 timeout_id_t temp_id = un->un_pm_timeid; 5426 un->un_pm_timeid = NULL; 5427 mutex_exit(&un->un_pm_mutex); 5428 mutex_exit(SD_MUTEX(un)); 5429 (void) untimeout(temp_id); 5430 mutex_enter(SD_MUTEX(un)); 5431 } else { 5432 mutex_exit(&un->un_pm_mutex); 5433 } 5434 5435 if (un->un_retry_timeid != NULL) { 5436 timeout_id_t temp_id = un->un_retry_timeid; 5437 un->un_retry_timeid = NULL; 5438 mutex_exit(SD_MUTEX(un)); 5439 (void) untimeout(temp_id); 5440 mutex_enter(SD_MUTEX(un)); 5441 5442 if (un->un_retry_bp != NULL) { 5443 un->un_retry_bp->av_forw = un->un_waitq_headp; 5444 un->un_waitq_headp = un->un_retry_bp; 5445 if (un->un_waitq_tailp == NULL) { 5446 un->un_waitq_tailp = un->un_retry_bp; 5447 } 5448 un->un_retry_bp = NULL; 5449 un->un_retry_statp = NULL; 5450 } 5451 } 5452 5453 if (un->un_direct_priority_timeid != NULL) { 5454 timeout_id_t temp_id = un->un_direct_priority_timeid; 5455 un->un_direct_priority_timeid = NULL; 5456 mutex_exit(SD_MUTEX(un)); 5457 (void) untimeout(temp_id); 5458 mutex_enter(SD_MUTEX(un)); 5459 } 5460 5461 if (un->un_f_is_fibre == TRUE) { 5462 /* 5463 * Remove callbacks for insert and remove events 5464 */ 5465 if (un->un_insert_event != NULL) { 5466 mutex_exit(SD_MUTEX(un)); 5467 (void) ddi_remove_event_handler(un->un_insert_cb_id); 5468 mutex_enter(SD_MUTEX(un)); 5469 un->un_insert_event = NULL; 5470 } 5471 5472 if (un->un_remove_event != NULL) { 5473 mutex_exit(SD_MUTEX(un)); 5474 (void) ddi_remove_event_handler(un->un_remove_cb_id); 5475 mutex_enter(SD_MUTEX(un)); 5476 un->un_remove_event = NULL; 5477 } 5478 } 5479 5480 mutex_exit(SD_MUTEX(un)); 5481 5482 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 5483 5484 return (DDI_SUCCESS); 5485 } 5486 5487 5488 /* 5489 * Function: sd_ddi_pm_suspend 5490 * 5491 * Description: Set the drive state to low power. 5492 * Someone else is required to actually change the drive 5493 * power level. 5494 * 5495 * Arguments: un - driver soft state (unit) structure 5496 * 5497 * Return Code: DDI_FAILURE or DDI_SUCCESS 5498 * 5499 * Context: Kernel thread context 5500 */ 5501 5502 static int 5503 sd_ddi_pm_suspend(struct sd_lun *un) 5504 { 5505 ASSERT(un != NULL); 5506 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 5507 5508 ASSERT(!mutex_owned(SD_MUTEX(un))); 5509 mutex_enter(SD_MUTEX(un)); 5510 5511 /* 5512 * Exit if power management is not enabled for this device, or if 5513 * the device is being used by HA. 5514 */ 5515 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 5516 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 5517 mutex_exit(SD_MUTEX(un)); 5518 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 5519 return (DDI_SUCCESS); 5520 } 5521 5522 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 5523 un->un_ncmds_in_driver); 5524 5525 /* 5526 * See if the device is not busy, ie.: 5527 * - we have no commands in the driver for this device 5528 * - not waiting for resources 5529 */ 5530 if ((un->un_ncmds_in_driver == 0) && 5531 (un->un_state != SD_STATE_RWAIT)) { 5532 /* 5533 * The device is not busy, so it is OK to go to low power state. 5534 * Indicate low power, but rely on someone else to actually 5535 * change it. 5536 */ 5537 mutex_enter(&un->un_pm_mutex); 5538 un->un_pm_count = -1; 5539 mutex_exit(&un->un_pm_mutex); 5540 un->un_power_level = SD_SPINDLE_OFF; 5541 } 5542 5543 mutex_exit(SD_MUTEX(un)); 5544 5545 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 5546 5547 return (DDI_SUCCESS); 5548 } 5549 5550 5551 /* 5552 * Function: sd_ddi_resume 5553 * 5554 * Description: Performs system power-up operations.. 5555 * 5556 * Return Code: DDI_SUCCESS 5557 * DDI_FAILURE 5558 * 5559 * Context: Kernel thread context 5560 */ 5561 5562 static int 5563 sd_ddi_resume(dev_info_t *devi) 5564 { 5565 struct sd_lun *un; 5566 5567 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5568 if (un == NULL) { 5569 return (DDI_FAILURE); 5570 } 5571 5572 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 5573 5574 mutex_enter(SD_MUTEX(un)); 5575 Restore_state(un); 5576 5577 /* 5578 * Restore the state which was saved to give the 5579 * the right state in un_last_state 5580 */ 5581 un->un_last_state = un->un_save_state; 5582 /* 5583 * Note: throttle comes back at full. 5584 * Also note: this MUST be done before calling pm_raise_power 5585 * otherwise the system can get hung in biowait. The scenario where 5586 * this'll happen is under cpr suspend. Writing of the system 5587 * state goes through sddump, which writes 0 to un_throttle. If 5588 * writing the system state then fails, example if the partition is 5589 * too small, then cpr attempts a resume. If throttle isn't restored 5590 * from the saved value until after calling pm_raise_power then 5591 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 5592 * in biowait. 5593 */ 5594 un->un_throttle = un->un_saved_throttle; 5595 5596 /* 5597 * The chance of failure is very rare as the only command done in power 5598 * entry point is START command when you transition from 0->1 or 5599 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 5600 * which suspend was done. Ignore the return value as the resume should 5601 * not be failed. In the case of removable media the media need not be 5602 * inserted and hence there is a chance that raise power will fail with 5603 * media not present. 5604 */ 5605 if (un->un_f_attach_spinup) { 5606 mutex_exit(SD_MUTEX(un)); 5607 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 5608 mutex_enter(SD_MUTEX(un)); 5609 } 5610 5611 /* 5612 * Don't broadcast to the suspend cv and therefore possibly 5613 * start I/O until after power has been restored. 5614 */ 5615 cv_broadcast(&un->un_suspend_cv); 5616 cv_broadcast(&un->un_state_cv); 5617 5618 /* restart thread */ 5619 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 5620 scsi_watch_resume(un->un_swr_token); 5621 } 5622 5623 #if (defined(__fibre)) 5624 if (un->un_f_is_fibre == TRUE) { 5625 /* 5626 * Add callbacks for insert and remove events 5627 */ 5628 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 5629 sd_init_event_callbacks(un); 5630 } 5631 } 5632 #endif 5633 5634 /* 5635 * Transport any pending commands to the target. 5636 * 5637 * If this is a low-activity device commands in queue will have to wait 5638 * until new commands come in, which may take awhile. Also, we 5639 * specifically don't check un_ncmds_in_transport because we know that 5640 * there really are no commands in progress after the unit was 5641 * suspended and we could have reached the throttle level, been 5642 * suspended, and have no new commands coming in for awhile. Highly 5643 * unlikely, but so is the low-activity disk scenario. 5644 */ 5645 ddi_xbuf_dispatch(un->un_xbuf_attr); 5646 5647 sd_start_cmds(un, NULL); 5648 mutex_exit(SD_MUTEX(un)); 5649 5650 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 5651 5652 return (DDI_SUCCESS); 5653 } 5654 5655 5656 /* 5657 * Function: sd_ddi_pm_resume 5658 * 5659 * Description: Set the drive state to powered on. 5660 * Someone else is required to actually change the drive 5661 * power level. 5662 * 5663 * Arguments: un - driver soft state (unit) structure 5664 * 5665 * Return Code: DDI_SUCCESS 5666 * 5667 * Context: Kernel thread context 5668 */ 5669 5670 static int 5671 sd_ddi_pm_resume(struct sd_lun *un) 5672 { 5673 ASSERT(un != NULL); 5674 5675 ASSERT(!mutex_owned(SD_MUTEX(un))); 5676 mutex_enter(SD_MUTEX(un)); 5677 un->un_power_level = SD_SPINDLE_ON; 5678 5679 ASSERT(!mutex_owned(&un->un_pm_mutex)); 5680 mutex_enter(&un->un_pm_mutex); 5681 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 5682 un->un_pm_count++; 5683 ASSERT(un->un_pm_count == 0); 5684 /* 5685 * Note: no longer do the cv_broadcast on un_suspend_cv. The 5686 * un_suspend_cv is for a system resume, not a power management 5687 * device resume. (4297749) 5688 * cv_broadcast(&un->un_suspend_cv); 5689 */ 5690 } 5691 mutex_exit(&un->un_pm_mutex); 5692 mutex_exit(SD_MUTEX(un)); 5693 5694 return (DDI_SUCCESS); 5695 } 5696 5697 5698 /* 5699 * Function: sd_pm_idletimeout_handler 5700 * 5701 * Description: A timer routine that's active only while a device is busy. 5702 * The purpose is to extend slightly the pm framework's busy 5703 * view of the device to prevent busy/idle thrashing for 5704 * back-to-back commands. Do this by comparing the current time 5705 * to the time at which the last command completed and when the 5706 * difference is greater than sd_pm_idletime, call 5707 * pm_idle_component. In addition to indicating idle to the pm 5708 * framework, update the chain type to again use the internal pm 5709 * layers of the driver. 5710 * 5711 * Arguments: arg - driver soft state (unit) structure 5712 * 5713 * Context: Executes in a timeout(9F) thread context 5714 */ 5715 5716 static void 5717 sd_pm_idletimeout_handler(void *arg) 5718 { 5719 struct sd_lun *un = arg; 5720 5721 time_t now; 5722 5723 mutex_enter(&sd_detach_mutex); 5724 if (un->un_detach_count != 0) { 5725 /* Abort if the instance is detaching */ 5726 mutex_exit(&sd_detach_mutex); 5727 return; 5728 } 5729 mutex_exit(&sd_detach_mutex); 5730 5731 now = ddi_get_time(); 5732 /* 5733 * Grab both mutexes, in the proper order, since we're accessing 5734 * both PM and softstate variables. 5735 */ 5736 mutex_enter(SD_MUTEX(un)); 5737 mutex_enter(&un->un_pm_mutex); 5738 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 5739 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 5740 /* 5741 * Update the chain types. 5742 * This takes affect on the next new command received. 5743 */ 5744 if (un->un_f_non_devbsize_supported) { 5745 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 5746 } else { 5747 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 5748 } 5749 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 5750 5751 SD_TRACE(SD_LOG_IO_PM, un, 5752 "sd_pm_idletimeout_handler: idling device\n"); 5753 (void) pm_idle_component(SD_DEVINFO(un), 0); 5754 un->un_pm_idle_timeid = NULL; 5755 } else { 5756 un->un_pm_idle_timeid = 5757 timeout(sd_pm_idletimeout_handler, un, 5758 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 5759 } 5760 mutex_exit(&un->un_pm_mutex); 5761 mutex_exit(SD_MUTEX(un)); 5762 } 5763 5764 5765 /* 5766 * Function: sd_pm_timeout_handler 5767 * 5768 * Description: Callback to tell framework we are idle. 5769 * 5770 * Context: timeout(9f) thread context. 5771 */ 5772 5773 static void 5774 sd_pm_timeout_handler(void *arg) 5775 { 5776 struct sd_lun *un = arg; 5777 5778 (void) pm_idle_component(SD_DEVINFO(un), 0); 5779 mutex_enter(&un->un_pm_mutex); 5780 un->un_pm_timeid = NULL; 5781 mutex_exit(&un->un_pm_mutex); 5782 } 5783 5784 5785 /* 5786 * Function: sdpower 5787 * 5788 * Description: PM entry point. 5789 * 5790 * Return Code: DDI_SUCCESS 5791 * DDI_FAILURE 5792 * 5793 * Context: Kernel thread context 5794 */ 5795 5796 static int 5797 sdpower(dev_info_t *devi, int component, int level) 5798 { 5799 struct sd_lun *un; 5800 int instance; 5801 int rval = DDI_SUCCESS; 5802 uint_t i, log_page_size, maxcycles, ncycles; 5803 uchar_t *log_page_data; 5804 int log_sense_page; 5805 int medium_present; 5806 time_t intvlp; 5807 dev_t dev; 5808 struct pm_trans_data sd_pm_tran_data; 5809 uchar_t save_state; 5810 int sval; 5811 uchar_t state_before_pm; 5812 int got_semaphore_here; 5813 5814 instance = ddi_get_instance(devi); 5815 5816 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 5817 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 5818 component != 0) { 5819 return (DDI_FAILURE); 5820 } 5821 5822 dev = sd_make_device(SD_DEVINFO(un)); 5823 5824 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 5825 5826 /* 5827 * Must synchronize power down with close. 5828 * Attempt to decrement/acquire the open/close semaphore, 5829 * but do NOT wait on it. If it's not greater than zero, 5830 * ie. it can't be decremented without waiting, then 5831 * someone else, either open or close, already has it 5832 * and the try returns 0. Use that knowledge here to determine 5833 * if it's OK to change the device power level. 5834 * Also, only increment it on exit if it was decremented, ie. gotten, 5835 * here. 5836 */ 5837 got_semaphore_here = sema_tryp(&un->un_semoclose); 5838 5839 mutex_enter(SD_MUTEX(un)); 5840 5841 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 5842 un->un_ncmds_in_driver); 5843 5844 /* 5845 * If un_ncmds_in_driver is non-zero it indicates commands are 5846 * already being processed in the driver, or if the semaphore was 5847 * not gotten here it indicates an open or close is being processed. 5848 * At the same time somebody is requesting to go low power which 5849 * can't happen, therefore we need to return failure. 5850 */ 5851 if ((level == SD_SPINDLE_OFF) && 5852 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 5853 mutex_exit(SD_MUTEX(un)); 5854 5855 if (got_semaphore_here != 0) { 5856 sema_v(&un->un_semoclose); 5857 } 5858 SD_TRACE(SD_LOG_IO_PM, un, 5859 "sdpower: exit, device has queued cmds.\n"); 5860 return (DDI_FAILURE); 5861 } 5862 5863 /* 5864 * if it is OFFLINE that means the disk is completely dead 5865 * in our case we have to put the disk in on or off by sending commands 5866 * Of course that will fail anyway so return back here. 5867 * 5868 * Power changes to a device that's OFFLINE or SUSPENDED 5869 * are not allowed. 5870 */ 5871 if ((un->un_state == SD_STATE_OFFLINE) || 5872 (un->un_state == SD_STATE_SUSPENDED)) { 5873 mutex_exit(SD_MUTEX(un)); 5874 5875 if (got_semaphore_here != 0) { 5876 sema_v(&un->un_semoclose); 5877 } 5878 SD_TRACE(SD_LOG_IO_PM, un, 5879 "sdpower: exit, device is off-line.\n"); 5880 return (DDI_FAILURE); 5881 } 5882 5883 /* 5884 * Change the device's state to indicate it's power level 5885 * is being changed. Do this to prevent a power off in the 5886 * middle of commands, which is especially bad on devices 5887 * that are really powered off instead of just spun down. 5888 */ 5889 state_before_pm = un->un_state; 5890 un->un_state = SD_STATE_PM_CHANGING; 5891 5892 mutex_exit(SD_MUTEX(un)); 5893 5894 /* 5895 * If "pm-capable" property is set to TRUE by HBA drivers, 5896 * bypass the following checking, otherwise, check the log 5897 * sense information for this device 5898 */ 5899 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 5900 /* 5901 * Get the log sense information to understand whether the 5902 * the powercycle counts have gone beyond the threshhold. 5903 */ 5904 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5905 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5906 5907 mutex_enter(SD_MUTEX(un)); 5908 log_sense_page = un->un_start_stop_cycle_page; 5909 mutex_exit(SD_MUTEX(un)); 5910 5911 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5912 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 5913 #ifdef SDDEBUG 5914 if (sd_force_pm_supported) { 5915 /* Force a successful result */ 5916 rval = 0; 5917 } 5918 #endif 5919 if (rval != 0) { 5920 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5921 "Log Sense Failed\n"); 5922 kmem_free(log_page_data, log_page_size); 5923 /* Cannot support power management on those drives */ 5924 5925 if (got_semaphore_here != 0) { 5926 sema_v(&un->un_semoclose); 5927 } 5928 /* 5929 * On exit put the state back to it's original value 5930 * and broadcast to anyone waiting for the power 5931 * change completion. 5932 */ 5933 mutex_enter(SD_MUTEX(un)); 5934 un->un_state = state_before_pm; 5935 cv_broadcast(&un->un_suspend_cv); 5936 mutex_exit(SD_MUTEX(un)); 5937 SD_TRACE(SD_LOG_IO_PM, un, 5938 "sdpower: exit, Log Sense Failed.\n"); 5939 return (DDI_FAILURE); 5940 } 5941 5942 /* 5943 * From the page data - Convert the essential information to 5944 * pm_trans_data 5945 */ 5946 maxcycles = 5947 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 5948 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 5949 5950 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 5951 5952 ncycles = 5953 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 5954 (log_page_data[0x26] << 8) | log_page_data[0x27]; 5955 5956 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 5957 5958 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 5959 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 5960 log_page_data[8+i]; 5961 } 5962 5963 kmem_free(log_page_data, log_page_size); 5964 5965 /* 5966 * Call pm_trans_check routine to get the Ok from 5967 * the global policy 5968 */ 5969 5970 sd_pm_tran_data.format = DC_SCSI_FORMAT; 5971 sd_pm_tran_data.un.scsi_cycles.flag = 0; 5972 5973 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 5974 #ifdef SDDEBUG 5975 if (sd_force_pm_supported) { 5976 /* Force a successful result */ 5977 rval = 1; 5978 } 5979 #endif 5980 switch (rval) { 5981 case 0: 5982 /* 5983 * Not Ok to Power cycle or error in parameters passed 5984 * Would have given the advised time to consider power 5985 * cycle. Based on the new intvlp parameter we are 5986 * supposed to pretend we are busy so that pm framework 5987 * will never call our power entry point. Because of 5988 * that install a timeout handler and wait for the 5989 * recommended time to elapse so that power management 5990 * can be effective again. 5991 * 5992 * To effect this behavior, call pm_busy_component to 5993 * indicate to the framework this device is busy. 5994 * By not adjusting un_pm_count the rest of PM in 5995 * the driver will function normally, and independent 5996 * of this but because the framework is told the device 5997 * is busy it won't attempt powering down until it gets 5998 * a matching idle. The timeout handler sends this. 5999 * Note: sd_pm_entry can't be called here to do this 6000 * because sdpower may have been called as a result 6001 * of a call to pm_raise_power from within sd_pm_entry. 6002 * 6003 * If a timeout handler is already active then 6004 * don't install another. 6005 */ 6006 mutex_enter(&un->un_pm_mutex); 6007 if (un->un_pm_timeid == NULL) { 6008 un->un_pm_timeid = 6009 timeout(sd_pm_timeout_handler, 6010 un, intvlp * drv_usectohz(1000000)); 6011 mutex_exit(&un->un_pm_mutex); 6012 (void) pm_busy_component(SD_DEVINFO(un), 0); 6013 } else { 6014 mutex_exit(&un->un_pm_mutex); 6015 } 6016 if (got_semaphore_here != 0) { 6017 sema_v(&un->un_semoclose); 6018 } 6019 /* 6020 * On exit put the state back to it's original value 6021 * and broadcast to anyone waiting for the power 6022 * change completion. 6023 */ 6024 mutex_enter(SD_MUTEX(un)); 6025 un->un_state = state_before_pm; 6026 cv_broadcast(&un->un_suspend_cv); 6027 mutex_exit(SD_MUTEX(un)); 6028 6029 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6030 "trans check Failed, not ok to power cycle.\n"); 6031 return (DDI_FAILURE); 6032 6033 case -1: 6034 if (got_semaphore_here != 0) { 6035 sema_v(&un->un_semoclose); 6036 } 6037 /* 6038 * On exit put the state back to it's original value 6039 * and broadcast to anyone waiting for the power 6040 * change completion. 6041 */ 6042 mutex_enter(SD_MUTEX(un)); 6043 un->un_state = state_before_pm; 6044 cv_broadcast(&un->un_suspend_cv); 6045 mutex_exit(SD_MUTEX(un)); 6046 SD_TRACE(SD_LOG_IO_PM, un, 6047 "sdpower: exit, trans check command Failed.\n"); 6048 return (DDI_FAILURE); 6049 } 6050 } 6051 6052 if (level == SD_SPINDLE_OFF) { 6053 /* 6054 * Save the last state... if the STOP FAILS we need it 6055 * for restoring 6056 */ 6057 mutex_enter(SD_MUTEX(un)); 6058 save_state = un->un_last_state; 6059 /* 6060 * There must not be any cmds. getting processed 6061 * in the driver when we get here. Power to the 6062 * device is potentially going off. 6063 */ 6064 ASSERT(un->un_ncmds_in_driver == 0); 6065 mutex_exit(SD_MUTEX(un)); 6066 6067 /* 6068 * For now suspend the device completely before spindle is 6069 * turned off 6070 */ 6071 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 6072 if (got_semaphore_here != 0) { 6073 sema_v(&un->un_semoclose); 6074 } 6075 /* 6076 * On exit put the state back to it's original value 6077 * and broadcast to anyone waiting for the power 6078 * change completion. 6079 */ 6080 mutex_enter(SD_MUTEX(un)); 6081 un->un_state = state_before_pm; 6082 cv_broadcast(&un->un_suspend_cv); 6083 mutex_exit(SD_MUTEX(un)); 6084 SD_TRACE(SD_LOG_IO_PM, un, 6085 "sdpower: exit, PM suspend Failed.\n"); 6086 return (DDI_FAILURE); 6087 } 6088 } 6089 6090 /* 6091 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6092 * close, or strategy. Dump no long uses this routine, it uses it's 6093 * own code so it can be done in polled mode. 6094 */ 6095 6096 medium_present = TRUE; 6097 6098 /* 6099 * When powering up, issue a TUR in case the device is at unit 6100 * attention. Don't do retries. Bypass the PM layer, otherwise 6101 * a deadlock on un_pm_busy_cv will occur. 6102 */ 6103 if (level == SD_SPINDLE_ON) { 6104 (void) sd_send_scsi_TEST_UNIT_READY(un, 6105 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6106 } 6107 6108 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6109 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6110 6111 sval = sd_send_scsi_START_STOP_UNIT(un, 6112 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 6113 SD_PATH_DIRECT); 6114 /* Command failed, check for media present. */ 6115 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6116 medium_present = FALSE; 6117 } 6118 6119 /* 6120 * The conditions of interest here are: 6121 * if a spindle off with media present fails, 6122 * then restore the state and return an error. 6123 * else if a spindle on fails, 6124 * then return an error (there's no state to restore). 6125 * In all other cases we setup for the new state 6126 * and return success. 6127 */ 6128 switch (level) { 6129 case SD_SPINDLE_OFF: 6130 if ((medium_present == TRUE) && (sval != 0)) { 6131 /* The stop command from above failed */ 6132 rval = DDI_FAILURE; 6133 /* 6134 * The stop command failed, and we have media 6135 * present. Put the level back by calling the 6136 * sd_pm_resume() and set the state back to 6137 * it's previous value. 6138 */ 6139 (void) sd_ddi_pm_resume(un); 6140 mutex_enter(SD_MUTEX(un)); 6141 un->un_last_state = save_state; 6142 mutex_exit(SD_MUTEX(un)); 6143 break; 6144 } 6145 /* 6146 * The stop command from above succeeded. 6147 */ 6148 if (un->un_f_monitor_media_state) { 6149 /* 6150 * Terminate watch thread in case of removable media 6151 * devices going into low power state. This is as per 6152 * the requirements of pm framework, otherwise commands 6153 * will be generated for the device (through watch 6154 * thread), even when the device is in low power state. 6155 */ 6156 mutex_enter(SD_MUTEX(un)); 6157 un->un_f_watcht_stopped = FALSE; 6158 if (un->un_swr_token != NULL) { 6159 opaque_t temp_token = un->un_swr_token; 6160 un->un_f_watcht_stopped = TRUE; 6161 un->un_swr_token = NULL; 6162 mutex_exit(SD_MUTEX(un)); 6163 (void) scsi_watch_request_terminate(temp_token, 6164 SCSI_WATCH_TERMINATE_ALL_WAIT); 6165 } else { 6166 mutex_exit(SD_MUTEX(un)); 6167 } 6168 } 6169 break; 6170 6171 default: /* The level requested is spindle on... */ 6172 /* 6173 * Legacy behavior: return success on a failed spinup 6174 * if there is no media in the drive. 6175 * Do this by looking at medium_present here. 6176 */ 6177 if ((sval != 0) && medium_present) { 6178 /* The start command from above failed */ 6179 rval = DDI_FAILURE; 6180 break; 6181 } 6182 /* 6183 * The start command from above succeeded 6184 * Resume the devices now that we have 6185 * started the disks 6186 */ 6187 (void) sd_ddi_pm_resume(un); 6188 6189 /* 6190 * Resume the watch thread since it was suspended 6191 * when the device went into low power mode. 6192 */ 6193 if (un->un_f_monitor_media_state) { 6194 mutex_enter(SD_MUTEX(un)); 6195 if (un->un_f_watcht_stopped == TRUE) { 6196 opaque_t temp_token; 6197 6198 un->un_f_watcht_stopped = FALSE; 6199 mutex_exit(SD_MUTEX(un)); 6200 temp_token = scsi_watch_request_submit( 6201 SD_SCSI_DEVP(un), 6202 sd_check_media_time, 6203 SENSE_LENGTH, sd_media_watch_cb, 6204 (caddr_t)dev); 6205 mutex_enter(SD_MUTEX(un)); 6206 un->un_swr_token = temp_token; 6207 } 6208 mutex_exit(SD_MUTEX(un)); 6209 } 6210 } 6211 if (got_semaphore_here != 0) { 6212 sema_v(&un->un_semoclose); 6213 } 6214 /* 6215 * On exit put the state back to it's original value 6216 * and broadcast to anyone waiting for the power 6217 * change completion. 6218 */ 6219 mutex_enter(SD_MUTEX(un)); 6220 un->un_state = state_before_pm; 6221 cv_broadcast(&un->un_suspend_cv); 6222 mutex_exit(SD_MUTEX(un)); 6223 6224 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 6225 6226 return (rval); 6227 } 6228 6229 6230 6231 /* 6232 * Function: sdattach 6233 * 6234 * Description: Driver's attach(9e) entry point function. 6235 * 6236 * Arguments: devi - opaque device info handle 6237 * cmd - attach type 6238 * 6239 * Return Code: DDI_SUCCESS 6240 * DDI_FAILURE 6241 * 6242 * Context: Kernel thread context 6243 */ 6244 6245 static int 6246 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 6247 { 6248 switch (cmd) { 6249 case DDI_ATTACH: 6250 return (sd_unit_attach(devi)); 6251 case DDI_RESUME: 6252 return (sd_ddi_resume(devi)); 6253 default: 6254 break; 6255 } 6256 return (DDI_FAILURE); 6257 } 6258 6259 6260 /* 6261 * Function: sddetach 6262 * 6263 * Description: Driver's detach(9E) entry point function. 6264 * 6265 * Arguments: devi - opaque device info handle 6266 * cmd - detach type 6267 * 6268 * Return Code: DDI_SUCCESS 6269 * DDI_FAILURE 6270 * 6271 * Context: Kernel thread context 6272 */ 6273 6274 static int 6275 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 6276 { 6277 switch (cmd) { 6278 case DDI_DETACH: 6279 return (sd_unit_detach(devi)); 6280 case DDI_SUSPEND: 6281 return (sd_ddi_suspend(devi)); 6282 default: 6283 break; 6284 } 6285 return (DDI_FAILURE); 6286 } 6287 6288 6289 /* 6290 * Function: sd_sync_with_callback 6291 * 6292 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 6293 * state while the callback routine is active. 6294 * 6295 * Arguments: un: softstate structure for the instance 6296 * 6297 * Context: Kernel thread context 6298 */ 6299 6300 static void 6301 sd_sync_with_callback(struct sd_lun *un) 6302 { 6303 ASSERT(un != NULL); 6304 6305 mutex_enter(SD_MUTEX(un)); 6306 6307 ASSERT(un->un_in_callback >= 0); 6308 6309 while (un->un_in_callback > 0) { 6310 mutex_exit(SD_MUTEX(un)); 6311 delay(2); 6312 mutex_enter(SD_MUTEX(un)); 6313 } 6314 6315 mutex_exit(SD_MUTEX(un)); 6316 } 6317 6318 /* 6319 * Function: sd_unit_attach 6320 * 6321 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 6322 * the soft state structure for the device and performs 6323 * all necessary structure and device initializations. 6324 * 6325 * Arguments: devi: the system's dev_info_t for the device. 6326 * 6327 * Return Code: DDI_SUCCESS if attach is successful. 6328 * DDI_FAILURE if any part of the attach fails. 6329 * 6330 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 6331 * Kernel thread context only. Can sleep. 6332 */ 6333 6334 static int 6335 sd_unit_attach(dev_info_t *devi) 6336 { 6337 struct scsi_device *devp; 6338 struct sd_lun *un; 6339 char *variantp; 6340 int reservation_flag = SD_TARGET_IS_UNRESERVED; 6341 int instance; 6342 int rval; 6343 int wc_enabled; 6344 int tgt; 6345 uint64_t capacity; 6346 uint_t lbasize = 0; 6347 dev_info_t *pdip = ddi_get_parent(devi); 6348 int offbyone = 0; 6349 int geom_label_valid = 0; 6350 #if defined(__sparc) 6351 int max_xfer_size; 6352 #endif 6353 6354 /* 6355 * Retrieve the target driver's private data area. This was set 6356 * up by the HBA. 6357 */ 6358 devp = ddi_get_driver_private(devi); 6359 6360 /* 6361 * Retrieve the target ID of the device. 6362 */ 6363 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6364 SCSI_ADDR_PROP_TARGET, -1); 6365 6366 /* 6367 * Since we have no idea what state things were left in by the last 6368 * user of the device, set up some 'default' settings, ie. turn 'em 6369 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 6370 * Do this before the scsi_probe, which sends an inquiry. 6371 * This is a fix for bug (4430280). 6372 * Of special importance is wide-xfer. The drive could have been left 6373 * in wide transfer mode by the last driver to communicate with it, 6374 * this includes us. If that's the case, and if the following is not 6375 * setup properly or we don't re-negotiate with the drive prior to 6376 * transferring data to/from the drive, it causes bus parity errors, 6377 * data overruns, and unexpected interrupts. This first occurred when 6378 * the fix for bug (4378686) was made. 6379 */ 6380 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 6381 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 6382 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 6383 6384 /* 6385 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 6386 * on a target. Setting it per lun instance actually sets the 6387 * capability of this target, which affects those luns already 6388 * attached on the same target. So during attach, we can only disable 6389 * this capability only when no other lun has been attached on this 6390 * target. By doing this, we assume a target has the same tagged-qing 6391 * capability for every lun. The condition can be removed when HBA 6392 * is changed to support per lun based tagged-qing capability. 6393 */ 6394 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 6395 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 6396 } 6397 6398 /* 6399 * Use scsi_probe() to issue an INQUIRY command to the device. 6400 * This call will allocate and fill in the scsi_inquiry structure 6401 * and point the sd_inq member of the scsi_device structure to it. 6402 * If the attach succeeds, then this memory will not be de-allocated 6403 * (via scsi_unprobe()) until the instance is detached. 6404 */ 6405 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 6406 goto probe_failed; 6407 } 6408 6409 /* 6410 * Check the device type as specified in the inquiry data and 6411 * claim it if it is of a type that we support. 6412 */ 6413 switch (devp->sd_inq->inq_dtype) { 6414 case DTYPE_DIRECT: 6415 break; 6416 case DTYPE_RODIRECT: 6417 break; 6418 case DTYPE_OPTICAL: 6419 break; 6420 case DTYPE_NOTPRESENT: 6421 default: 6422 /* Unsupported device type; fail the attach. */ 6423 goto probe_failed; 6424 } 6425 6426 /* 6427 * Allocate the soft state structure for this unit. 6428 * 6429 * We rely upon this memory being set to all zeroes by 6430 * ddi_soft_state_zalloc(). We assume that any member of the 6431 * soft state structure that is not explicitly initialized by 6432 * this routine will have a value of zero. 6433 */ 6434 instance = ddi_get_instance(devp->sd_dev); 6435 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 6436 goto probe_failed; 6437 } 6438 6439 /* 6440 * Retrieve a pointer to the newly-allocated soft state. 6441 * 6442 * This should NEVER fail if the ddi_soft_state_zalloc() call above 6443 * was successful, unless something has gone horribly wrong and the 6444 * ddi's soft state internals are corrupt (in which case it is 6445 * probably better to halt here than just fail the attach....) 6446 */ 6447 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 6448 panic("sd_unit_attach: NULL soft state on instance:0x%x", 6449 instance); 6450 /*NOTREACHED*/ 6451 } 6452 6453 /* 6454 * Link the back ptr of the driver soft state to the scsi_device 6455 * struct for this lun. 6456 * Save a pointer to the softstate in the driver-private area of 6457 * the scsi_device struct. 6458 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 6459 * we first set un->un_sd below. 6460 */ 6461 un->un_sd = devp; 6462 devp->sd_private = (opaque_t)un; 6463 6464 /* 6465 * The following must be after devp is stored in the soft state struct. 6466 */ 6467 #ifdef SDDEBUG 6468 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6469 "%s_unit_attach: un:0x%p instance:%d\n", 6470 ddi_driver_name(devi), un, instance); 6471 #endif 6472 6473 /* 6474 * Set up the device type and node type (for the minor nodes). 6475 * By default we assume that the device can at least support the 6476 * Common Command Set. Call it a CD-ROM if it reports itself 6477 * as a RODIRECT device. 6478 */ 6479 switch (devp->sd_inq->inq_dtype) { 6480 case DTYPE_RODIRECT: 6481 un->un_node_type = DDI_NT_CD_CHAN; 6482 un->un_ctype = CTYPE_CDROM; 6483 break; 6484 case DTYPE_OPTICAL: 6485 un->un_node_type = DDI_NT_BLOCK_CHAN; 6486 un->un_ctype = CTYPE_ROD; 6487 break; 6488 default: 6489 un->un_node_type = DDI_NT_BLOCK_CHAN; 6490 un->un_ctype = CTYPE_CCS; 6491 break; 6492 } 6493 6494 /* 6495 * Try to read the interconnect type from the HBA. 6496 * 6497 * Note: This driver is currently compiled as two binaries, a parallel 6498 * scsi version (sd) and a fibre channel version (ssd). All functional 6499 * differences are determined at compile time. In the future a single 6500 * binary will be provided and the interconnect type will be used to 6501 * differentiate between fibre and parallel scsi behaviors. At that time 6502 * it will be necessary for all fibre channel HBAs to support this 6503 * property. 6504 * 6505 * set un_f_is_fiber to TRUE ( default fiber ) 6506 */ 6507 un->un_f_is_fibre = TRUE; 6508 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 6509 case INTERCONNECT_SSA: 6510 un->un_interconnect_type = SD_INTERCONNECT_SSA; 6511 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6512 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 6513 break; 6514 case INTERCONNECT_PARALLEL: 6515 un->un_f_is_fibre = FALSE; 6516 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6517 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6518 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 6519 break; 6520 case INTERCONNECT_SATA: 6521 un->un_f_is_fibre = FALSE; 6522 un->un_interconnect_type = SD_INTERCONNECT_SATA; 6523 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6524 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 6525 break; 6526 case INTERCONNECT_FIBRE: 6527 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 6528 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6529 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 6530 break; 6531 case INTERCONNECT_FABRIC: 6532 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 6533 un->un_node_type = DDI_NT_BLOCK_FABRIC; 6534 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6535 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 6536 break; 6537 default: 6538 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 6539 /* 6540 * The HBA does not support the "interconnect-type" property 6541 * (or did not provide a recognized type). 6542 * 6543 * Note: This will be obsoleted when a single fibre channel 6544 * and parallel scsi driver is delivered. In the meantime the 6545 * interconnect type will be set to the platform default.If that 6546 * type is not parallel SCSI, it means that we should be 6547 * assuming "ssd" semantics. However, here this also means that 6548 * the FC HBA is not supporting the "interconnect-type" property 6549 * like we expect it to, so log this occurrence. 6550 */ 6551 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 6552 if (!SD_IS_PARALLEL_SCSI(un)) { 6553 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6554 "sd_unit_attach: un:0x%p Assuming " 6555 "INTERCONNECT_FIBRE\n", un); 6556 } else { 6557 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6558 "sd_unit_attach: un:0x%p Assuming " 6559 "INTERCONNECT_PARALLEL\n", un); 6560 un->un_f_is_fibre = FALSE; 6561 } 6562 #else 6563 /* 6564 * Note: This source will be implemented when a single fibre 6565 * channel and parallel scsi driver is delivered. The default 6566 * will be to assume that if a device does not support the 6567 * "interconnect-type" property it is a parallel SCSI HBA and 6568 * we will set the interconnect type for parallel scsi. 6569 */ 6570 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6571 un->un_f_is_fibre = FALSE; 6572 #endif 6573 break; 6574 } 6575 6576 if (un->un_f_is_fibre == TRUE) { 6577 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 6578 SCSI_VERSION_3) { 6579 switch (un->un_interconnect_type) { 6580 case SD_INTERCONNECT_FIBRE: 6581 case SD_INTERCONNECT_SSA: 6582 un->un_node_type = DDI_NT_BLOCK_WWN; 6583 break; 6584 default: 6585 break; 6586 } 6587 } 6588 } 6589 6590 /* 6591 * Initialize the Request Sense command for the target 6592 */ 6593 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 6594 goto alloc_rqs_failed; 6595 } 6596 6597 /* 6598 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 6599 * with separate binary for sd and ssd. 6600 * 6601 * x86 has 1 binary, un_retry_count is set base on connection type. 6602 * The hardcoded values will go away when Sparc uses 1 binary 6603 * for sd and ssd. This hardcoded values need to match 6604 * SD_RETRY_COUNT in sddef.h 6605 * The value used is base on interconnect type. 6606 * fibre = 3, parallel = 5 6607 */ 6608 #if defined(__i386) || defined(__amd64) 6609 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 6610 #else 6611 un->un_retry_count = SD_RETRY_COUNT; 6612 #endif 6613 6614 /* 6615 * Set the per disk retry count to the default number of retries 6616 * for disks and CDROMs. This value can be overridden by the 6617 * disk property list or an entry in sd.conf. 6618 */ 6619 un->un_notready_retry_count = 6620 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 6621 : DISK_NOT_READY_RETRY_COUNT(un); 6622 6623 /* 6624 * Set the busy retry count to the default value of un_retry_count. 6625 * This can be overridden by entries in sd.conf or the device 6626 * config table. 6627 */ 6628 un->un_busy_retry_count = un->un_retry_count; 6629 6630 /* 6631 * Init the reset threshold for retries. This number determines 6632 * how many retries must be performed before a reset can be issued 6633 * (for certain error conditions). This can be overridden by entries 6634 * in sd.conf or the device config table. 6635 */ 6636 un->un_reset_retry_count = (un->un_retry_count / 2); 6637 6638 /* 6639 * Set the victim_retry_count to the default un_retry_count 6640 */ 6641 un->un_victim_retry_count = (2 * un->un_retry_count); 6642 6643 /* 6644 * Set the reservation release timeout to the default value of 6645 * 5 seconds. This can be overridden by entries in ssd.conf or the 6646 * device config table. 6647 */ 6648 un->un_reserve_release_time = 5; 6649 6650 /* 6651 * Set up the default maximum transfer size. Note that this may 6652 * get updated later in the attach, when setting up default wide 6653 * operations for disks. 6654 */ 6655 #if defined(__i386) || defined(__amd64) 6656 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 6657 un->un_partial_dma_supported = 1; 6658 #else 6659 un->un_max_xfer_size = (uint_t)maxphys; 6660 #endif 6661 6662 /* 6663 * Get "allow bus device reset" property (defaults to "enabled" if 6664 * the property was not defined). This is to disable bus resets for 6665 * certain kinds of error recovery. Note: In the future when a run-time 6666 * fibre check is available the soft state flag should default to 6667 * enabled. 6668 */ 6669 if (un->un_f_is_fibre == TRUE) { 6670 un->un_f_allow_bus_device_reset = TRUE; 6671 } else { 6672 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6673 "allow-bus-device-reset", 1) != 0) { 6674 un->un_f_allow_bus_device_reset = TRUE; 6675 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6676 "sd_unit_attach: un:0x%p Bus device reset " 6677 "enabled\n", un); 6678 } else { 6679 un->un_f_allow_bus_device_reset = FALSE; 6680 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6681 "sd_unit_attach: un:0x%p Bus device reset " 6682 "disabled\n", un); 6683 } 6684 } 6685 6686 /* 6687 * Check if this is an ATAPI device. ATAPI devices use Group 1 6688 * Read/Write commands and Group 2 Mode Sense/Select commands. 6689 * 6690 * Note: The "obsolete" way of doing this is to check for the "atapi" 6691 * property. The new "variant" property with a value of "atapi" has been 6692 * introduced so that future 'variants' of standard SCSI behavior (like 6693 * atapi) could be specified by the underlying HBA drivers by supplying 6694 * a new value for the "variant" property, instead of having to define a 6695 * new property. 6696 */ 6697 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 6698 un->un_f_cfg_is_atapi = TRUE; 6699 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6700 "sd_unit_attach: un:0x%p Atapi device\n", un); 6701 } 6702 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 6703 &variantp) == DDI_PROP_SUCCESS) { 6704 if (strcmp(variantp, "atapi") == 0) { 6705 un->un_f_cfg_is_atapi = TRUE; 6706 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6707 "sd_unit_attach: un:0x%p Atapi device\n", un); 6708 } 6709 ddi_prop_free(variantp); 6710 } 6711 6712 un->un_cmd_timeout = SD_IO_TIME; 6713 6714 /* Info on current states, statuses, etc. (Updated frequently) */ 6715 un->un_state = SD_STATE_NORMAL; 6716 un->un_last_state = SD_STATE_NORMAL; 6717 6718 /* Control & status info for command throttling */ 6719 un->un_throttle = sd_max_throttle; 6720 un->un_saved_throttle = sd_max_throttle; 6721 un->un_min_throttle = sd_min_throttle; 6722 6723 if (un->un_f_is_fibre == TRUE) { 6724 un->un_f_use_adaptive_throttle = TRUE; 6725 } else { 6726 un->un_f_use_adaptive_throttle = FALSE; 6727 } 6728 6729 /* Removable media support. */ 6730 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 6731 un->un_mediastate = DKIO_NONE; 6732 un->un_specified_mediastate = DKIO_NONE; 6733 6734 /* CVs for suspend/resume (PM or DR) */ 6735 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 6736 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 6737 6738 /* Power management support. */ 6739 un->un_power_level = SD_SPINDLE_UNINIT; 6740 6741 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 6742 un->un_f_wcc_inprog = 0; 6743 6744 /* 6745 * The open/close semaphore is used to serialize threads executing 6746 * in the driver's open & close entry point routines for a given 6747 * instance. 6748 */ 6749 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 6750 6751 /* 6752 * The conf file entry and softstate variable is a forceful override, 6753 * meaning a non-zero value must be entered to change the default. 6754 */ 6755 un->un_f_disksort_disabled = FALSE; 6756 6757 /* 6758 * Retrieve the properties from the static driver table or the driver 6759 * configuration file (.conf) for this unit and update the soft state 6760 * for the device as needed for the indicated properties. 6761 * Note: the property configuration needs to occur here as some of the 6762 * following routines may have dependencies on soft state flags set 6763 * as part of the driver property configuration. 6764 */ 6765 sd_read_unit_properties(un); 6766 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6767 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 6768 6769 /* 6770 * Only if a device has "hotpluggable" property, it is 6771 * treated as hotpluggable device. Otherwise, it is 6772 * regarded as non-hotpluggable one. 6773 */ 6774 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 6775 -1) != -1) { 6776 un->un_f_is_hotpluggable = TRUE; 6777 } 6778 6779 /* 6780 * set unit's attributes(flags) according to "hotpluggable" and 6781 * RMB bit in INQUIRY data. 6782 */ 6783 sd_set_unit_attributes(un, devi); 6784 6785 /* 6786 * By default, we mark the capacity, lbasize, and geometry 6787 * as invalid. Only if we successfully read a valid capacity 6788 * will we update the un_blockcount and un_tgt_blocksize with the 6789 * valid values (the geometry will be validated later). 6790 */ 6791 un->un_f_blockcount_is_valid = FALSE; 6792 un->un_f_tgt_blocksize_is_valid = FALSE; 6793 6794 /* 6795 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 6796 * otherwise. 6797 */ 6798 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 6799 un->un_blockcount = 0; 6800 6801 /* 6802 * Set up the per-instance info needed to determine the correct 6803 * CDBs and other info for issuing commands to the target. 6804 */ 6805 sd_init_cdb_limits(un); 6806 6807 /* 6808 * Set up the IO chains to use, based upon the target type. 6809 */ 6810 if (un->un_f_non_devbsize_supported) { 6811 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6812 } else { 6813 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6814 } 6815 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6816 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 6817 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 6818 6819 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 6820 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 6821 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 6822 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 6823 6824 6825 if (ISCD(un)) { 6826 un->un_additional_codes = sd_additional_codes; 6827 } else { 6828 un->un_additional_codes = NULL; 6829 } 6830 6831 /* 6832 * Create the kstats here so they can be available for attach-time 6833 * routines that send commands to the unit (either polled or via 6834 * sd_send_scsi_cmd). 6835 * 6836 * Note: This is a critical sequence that needs to be maintained: 6837 * 1) Instantiate the kstats here, before any routines using the 6838 * iopath (i.e. sd_send_scsi_cmd). 6839 * 2) Instantiate and initialize the partition stats 6840 * (sd_set_pstats). 6841 * 3) Initialize the error stats (sd_set_errstats), following 6842 * sd_validate_geometry(),sd_register_devid(), 6843 * and sd_cache_control(). 6844 */ 6845 6846 un->un_stats = kstat_create(sd_label, instance, 6847 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 6848 if (un->un_stats != NULL) { 6849 un->un_stats->ks_lock = SD_MUTEX(un); 6850 kstat_install(un->un_stats); 6851 } 6852 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6853 "sd_unit_attach: un:0x%p un_stats created\n", un); 6854 6855 sd_create_errstats(un, instance); 6856 if (un->un_errstats == NULL) { 6857 goto create_errstats_failed; 6858 } 6859 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6860 "sd_unit_attach: un:0x%p errstats created\n", un); 6861 6862 /* 6863 * The following if/else code was relocated here from below as part 6864 * of the fix for bug (4430280). However with the default setup added 6865 * on entry to this routine, it's no longer absolutely necessary for 6866 * this to be before the call to sd_spin_up_unit. 6867 */ 6868 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 6869 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 6870 (devp->sd_inq->inq_ansi == 5)) && 6871 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 6872 6873 /* 6874 * If tagged queueing is supported by the target 6875 * and by the host adapter then we will enable it 6876 */ 6877 un->un_tagflags = 0; 6878 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 6879 (un->un_f_arq_enabled == TRUE)) { 6880 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 6881 1, 1) == 1) { 6882 un->un_tagflags = FLAG_STAG; 6883 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6884 "sd_unit_attach: un:0x%p tag queueing " 6885 "enabled\n", un); 6886 } else if (scsi_ifgetcap(SD_ADDRESS(un), 6887 "untagged-qing", 0) == 1) { 6888 un->un_f_opt_queueing = TRUE; 6889 un->un_saved_throttle = un->un_throttle = 6890 min(un->un_throttle, 3); 6891 } else { 6892 un->un_f_opt_queueing = FALSE; 6893 un->un_saved_throttle = un->un_throttle = 1; 6894 } 6895 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 6896 == 1) && (un->un_f_arq_enabled == TRUE)) { 6897 /* The Host Adapter supports internal queueing. */ 6898 un->un_f_opt_queueing = TRUE; 6899 un->un_saved_throttle = un->un_throttle = 6900 min(un->un_throttle, 3); 6901 } else { 6902 un->un_f_opt_queueing = FALSE; 6903 un->un_saved_throttle = un->un_throttle = 1; 6904 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6905 "sd_unit_attach: un:0x%p no tag queueing\n", un); 6906 } 6907 6908 /* 6909 * Enable large transfers for SATA/SAS drives 6910 */ 6911 if (SD_IS_SERIAL(un)) { 6912 un->un_max_xfer_size = 6913 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6914 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6915 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6916 "sd_unit_attach: un:0x%p max transfer " 6917 "size=0x%x\n", un, un->un_max_xfer_size); 6918 6919 } 6920 6921 /* Setup or tear down default wide operations for disks */ 6922 6923 /* 6924 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 6925 * and "ssd_max_xfer_size" to exist simultaneously on the same 6926 * system and be set to different values. In the future this 6927 * code may need to be updated when the ssd module is 6928 * obsoleted and removed from the system. (4299588) 6929 */ 6930 if (SD_IS_PARALLEL_SCSI(un) && 6931 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 6932 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 6933 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6934 1, 1) == 1) { 6935 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6936 "sd_unit_attach: un:0x%p Wide Transfer " 6937 "enabled\n", un); 6938 } 6939 6940 /* 6941 * If tagged queuing has also been enabled, then 6942 * enable large xfers 6943 */ 6944 if (un->un_saved_throttle == sd_max_throttle) { 6945 un->un_max_xfer_size = 6946 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6947 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6948 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6949 "sd_unit_attach: un:0x%p max transfer " 6950 "size=0x%x\n", un, un->un_max_xfer_size); 6951 } 6952 } else { 6953 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6954 0, 1) == 1) { 6955 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6956 "sd_unit_attach: un:0x%p " 6957 "Wide Transfer disabled\n", un); 6958 } 6959 } 6960 } else { 6961 un->un_tagflags = FLAG_STAG; 6962 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 6963 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 6964 } 6965 6966 /* 6967 * If this target supports LUN reset, try to enable it. 6968 */ 6969 if (un->un_f_lun_reset_enabled) { 6970 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 6971 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6972 "un:0x%p lun_reset capability set\n", un); 6973 } else { 6974 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6975 "un:0x%p lun-reset capability not set\n", un); 6976 } 6977 } 6978 6979 /* 6980 * Adjust the maximum transfer size. This is to fix 6981 * the problem of partial DMA support on SPARC. Some 6982 * HBA driver, like aac, has very small dma_attr_maxxfer 6983 * size, which requires partial DMA support on SPARC. 6984 * In the future the SPARC pci nexus driver may solve 6985 * the problem instead of this fix. 6986 */ 6987 #if defined(__sparc) 6988 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 6989 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 6990 un->un_max_xfer_size = max_xfer_size; 6991 un->un_partial_dma_supported = 1; 6992 } 6993 #endif 6994 6995 /* 6996 * Set PKT_DMA_PARTIAL flag. 6997 */ 6998 if (un->un_partial_dma_supported == 1) { 6999 un->un_pkt_flags = PKT_DMA_PARTIAL; 7000 } else { 7001 un->un_pkt_flags = 0; 7002 } 7003 7004 /* 7005 * At this point in the attach, we have enough info in the 7006 * soft state to be able to issue commands to the target. 7007 * 7008 * All command paths used below MUST issue their commands as 7009 * SD_PATH_DIRECT. This is important as intermediate layers 7010 * are not all initialized yet (such as PM). 7011 */ 7012 7013 /* 7014 * Send a TEST UNIT READY command to the device. This should clear 7015 * any outstanding UNIT ATTENTION that may be present. 7016 * 7017 * Note: Don't check for success, just track if there is a reservation, 7018 * this is a throw away command to clear any unit attentions. 7019 * 7020 * Note: This MUST be the first command issued to the target during 7021 * attach to ensure power on UNIT ATTENTIONS are cleared. 7022 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7023 * with attempts at spinning up a device with no media. 7024 */ 7025 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 7026 reservation_flag = SD_TARGET_IS_RESERVED; 7027 } 7028 7029 /* 7030 * If the device is NOT a removable media device, attempt to spin 7031 * it up (using the START_STOP_UNIT command) and read its capacity 7032 * (using the READ CAPACITY command). Note, however, that either 7033 * of these could fail and in some cases we would continue with 7034 * the attach despite the failure (see below). 7035 */ 7036 if (un->un_f_descr_format_supported) { 7037 switch (sd_spin_up_unit(un)) { 7038 case 0: 7039 /* 7040 * Spin-up was successful; now try to read the 7041 * capacity. If successful then save the results 7042 * and mark the capacity & lbasize as valid. 7043 */ 7044 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7045 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7046 7047 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 7048 &lbasize, SD_PATH_DIRECT)) { 7049 case 0: { 7050 if (capacity > DK_MAX_BLOCKS) { 7051 #ifdef _LP64 7052 if (capacity + 1 > 7053 SD_GROUP1_MAX_ADDRESS) { 7054 /* 7055 * Enable descriptor format 7056 * sense data so that we can 7057 * get 64 bit sense data 7058 * fields. 7059 */ 7060 sd_enable_descr_sense(un); 7061 } 7062 #else 7063 /* 32-bit kernels can't handle this */ 7064 scsi_log(SD_DEVINFO(un), 7065 sd_label, CE_WARN, 7066 "disk has %llu blocks, which " 7067 "is too large for a 32-bit " 7068 "kernel", capacity); 7069 7070 #if defined(__i386) || defined(__amd64) 7071 /* 7072 * 1TB disk was treated as (1T - 512)B 7073 * in the past, so that it might have 7074 * valid VTOC and solaris partitions, 7075 * we have to allow it to continue to 7076 * work. 7077 */ 7078 if (capacity -1 > DK_MAX_BLOCKS) 7079 #endif 7080 goto spinup_failed; 7081 #endif 7082 } 7083 7084 /* 7085 * Here it's not necessary to check the case: 7086 * the capacity of the device is bigger than 7087 * what the max hba cdb can support. Because 7088 * sd_send_scsi_READ_CAPACITY will retrieve 7089 * the capacity by sending USCSI command, which 7090 * is constrained by the max hba cdb. Actually, 7091 * sd_send_scsi_READ_CAPACITY will return 7092 * EINVAL when using bigger cdb than required 7093 * cdb length. Will handle this case in 7094 * "case EINVAL". 7095 */ 7096 7097 /* 7098 * The following relies on 7099 * sd_send_scsi_READ_CAPACITY never 7100 * returning 0 for capacity and/or lbasize. 7101 */ 7102 sd_update_block_info(un, lbasize, capacity); 7103 7104 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7105 "sd_unit_attach: un:0x%p capacity = %ld " 7106 "blocks; lbasize= %ld.\n", un, 7107 un->un_blockcount, un->un_tgt_blocksize); 7108 7109 break; 7110 } 7111 case EINVAL: 7112 /* 7113 * In the case where the max-cdb-length property 7114 * is smaller than the required CDB length for 7115 * a SCSI device, a target driver can fail to 7116 * attach to that device. 7117 */ 7118 scsi_log(SD_DEVINFO(un), 7119 sd_label, CE_WARN, 7120 "disk capacity is too large " 7121 "for current cdb length"); 7122 goto spinup_failed; 7123 case EACCES: 7124 /* 7125 * Should never get here if the spin-up 7126 * succeeded, but code it in anyway. 7127 * From here, just continue with the attach... 7128 */ 7129 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7130 "sd_unit_attach: un:0x%p " 7131 "sd_send_scsi_READ_CAPACITY " 7132 "returned reservation conflict\n", un); 7133 reservation_flag = SD_TARGET_IS_RESERVED; 7134 break; 7135 default: 7136 /* 7137 * Likewise, should never get here if the 7138 * spin-up succeeded. Just continue with 7139 * the attach... 7140 */ 7141 break; 7142 } 7143 break; 7144 case EACCES: 7145 /* 7146 * Device is reserved by another host. In this case 7147 * we could not spin it up or read the capacity, but 7148 * we continue with the attach anyway. 7149 */ 7150 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7151 "sd_unit_attach: un:0x%p spin-up reservation " 7152 "conflict.\n", un); 7153 reservation_flag = SD_TARGET_IS_RESERVED; 7154 break; 7155 default: 7156 /* Fail the attach if the spin-up failed. */ 7157 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7158 "sd_unit_attach: un:0x%p spin-up failed.", un); 7159 goto spinup_failed; 7160 } 7161 } 7162 7163 /* 7164 * Check to see if this is a MMC drive 7165 */ 7166 if (ISCD(un)) { 7167 sd_set_mmc_caps(un); 7168 } 7169 7170 7171 /* 7172 * Add a zero-length attribute to tell the world we support 7173 * kernel ioctls (for layered drivers) 7174 */ 7175 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7176 DDI_KERNEL_IOCTL, NULL, 0); 7177 7178 /* 7179 * Add a boolean property to tell the world we support 7180 * the B_FAILFAST flag (for layered drivers) 7181 */ 7182 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7183 "ddi-failfast-supported", NULL, 0); 7184 7185 /* 7186 * Initialize power management 7187 */ 7188 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 7189 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 7190 sd_setup_pm(un, devi); 7191 if (un->un_f_pm_is_enabled == FALSE) { 7192 /* 7193 * For performance, point to a jump table that does 7194 * not include pm. 7195 * The direct and priority chains don't change with PM. 7196 * 7197 * Note: this is currently done based on individual device 7198 * capabilities. When an interface for determining system 7199 * power enabled state becomes available, or when additional 7200 * layers are added to the command chain, these values will 7201 * have to be re-evaluated for correctness. 7202 */ 7203 if (un->un_f_non_devbsize_supported) { 7204 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 7205 } else { 7206 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 7207 } 7208 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 7209 } 7210 7211 /* 7212 * This property is set to 0 by HA software to avoid retries 7213 * on a reserved disk. (The preferred property name is 7214 * "retry-on-reservation-conflict") (1189689) 7215 * 7216 * Note: The use of a global here can have unintended consequences. A 7217 * per instance variable is preferable to match the capabilities of 7218 * different underlying hba's (4402600) 7219 */ 7220 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 7221 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 7222 sd_retry_on_reservation_conflict); 7223 if (sd_retry_on_reservation_conflict != 0) { 7224 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 7225 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 7226 sd_retry_on_reservation_conflict); 7227 } 7228 7229 /* Set up options for QFULL handling. */ 7230 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7231 "qfull-retries", -1)) != -1) { 7232 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 7233 rval, 1); 7234 } 7235 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7236 "qfull-retry-interval", -1)) != -1) { 7237 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 7238 rval, 1); 7239 } 7240 7241 /* 7242 * This just prints a message that announces the existence of the 7243 * device. The message is always printed in the system logfile, but 7244 * only appears on the console if the system is booted with the 7245 * -v (verbose) argument. 7246 */ 7247 ddi_report_dev(devi); 7248 7249 un->un_mediastate = DKIO_NONE; 7250 7251 cmlb_alloc_handle(&un->un_cmlbhandle); 7252 7253 #if defined(__i386) || defined(__amd64) 7254 /* 7255 * On x86, compensate for off-by-1 legacy error 7256 */ 7257 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 7258 (lbasize == un->un_sys_blocksize)) 7259 offbyone = CMLB_OFF_BY_ONE; 7260 #endif 7261 7262 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 7263 un->un_f_has_removable_media, un->un_f_is_hotpluggable, 7264 un->un_node_type, offbyone, un->un_cmlbhandle, 7265 (void *)SD_PATH_DIRECT) != 0) { 7266 goto cmlb_attach_failed; 7267 } 7268 7269 7270 /* 7271 * Read and validate the device's geometry (ie, disk label) 7272 * A new unformatted drive will not have a valid geometry, but 7273 * the driver needs to successfully attach to this device so 7274 * the drive can be formatted via ioctls. 7275 */ 7276 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 7277 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 7278 7279 mutex_enter(SD_MUTEX(un)); 7280 7281 /* 7282 * Read and initialize the devid for the unit. 7283 */ 7284 if (un->un_f_devid_supported) { 7285 sd_register_devid(un, devi, reservation_flag); 7286 } 7287 mutex_exit(SD_MUTEX(un)); 7288 7289 #if (defined(__fibre)) 7290 /* 7291 * Register callbacks for fibre only. You can't do this solely 7292 * on the basis of the devid_type because this is hba specific. 7293 * We need to query our hba capabilities to find out whether to 7294 * register or not. 7295 */ 7296 if (un->un_f_is_fibre) { 7297 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7298 sd_init_event_callbacks(un); 7299 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7300 "sd_unit_attach: un:0x%p event callbacks inserted", 7301 un); 7302 } 7303 } 7304 #endif 7305 7306 if (un->un_f_opt_disable_cache == TRUE) { 7307 /* 7308 * Disable both read cache and write cache. This is 7309 * the historic behavior of the keywords in the config file. 7310 */ 7311 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 7312 0) { 7313 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7314 "sd_unit_attach: un:0x%p Could not disable " 7315 "caching", un); 7316 goto devid_failed; 7317 } 7318 } 7319 7320 /* 7321 * Check the value of the WCE bit now and 7322 * set un_f_write_cache_enabled accordingly. 7323 */ 7324 (void) sd_get_write_cache_enabled(un, &wc_enabled); 7325 mutex_enter(SD_MUTEX(un)); 7326 un->un_f_write_cache_enabled = (wc_enabled != 0); 7327 mutex_exit(SD_MUTEX(un)); 7328 7329 /* 7330 * Check the value of the NV_SUP bit and set 7331 * un_f_suppress_cache_flush accordingly. 7332 */ 7333 sd_get_nv_sup(un); 7334 7335 /* 7336 * Find out what type of reservation this disk supports. 7337 */ 7338 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 7339 case 0: 7340 /* 7341 * SCSI-3 reservations are supported. 7342 */ 7343 un->un_reservation_type = SD_SCSI3_RESERVATION; 7344 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7345 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 7346 break; 7347 case ENOTSUP: 7348 /* 7349 * The PERSISTENT RESERVE IN command would not be recognized by 7350 * a SCSI-2 device, so assume the reservation type is SCSI-2. 7351 */ 7352 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7353 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 7354 un->un_reservation_type = SD_SCSI2_RESERVATION; 7355 break; 7356 default: 7357 /* 7358 * default to SCSI-3 reservations 7359 */ 7360 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7361 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 7362 un->un_reservation_type = SD_SCSI3_RESERVATION; 7363 break; 7364 } 7365 7366 /* 7367 * Set the pstat and error stat values here, so data obtained during the 7368 * previous attach-time routines is available. 7369 * 7370 * Note: This is a critical sequence that needs to be maintained: 7371 * 1) Instantiate the kstats before any routines using the iopath 7372 * (i.e. sd_send_scsi_cmd). 7373 * 2) Initialize the error stats (sd_set_errstats) and partition 7374 * stats (sd_set_pstats)here, following 7375 * cmlb_validate_geometry(), sd_register_devid(), and 7376 * sd_cache_control(). 7377 */ 7378 7379 if (un->un_f_pkstats_enabled && geom_label_valid) { 7380 sd_set_pstats(un); 7381 SD_TRACE(SD_LOG_IO_PARTITION, un, 7382 "sd_unit_attach: un:0x%p pstats created and set\n", un); 7383 } 7384 7385 sd_set_errstats(un); 7386 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7387 "sd_unit_attach: un:0x%p errstats set\n", un); 7388 7389 7390 /* 7391 * After successfully attaching an instance, we record the information 7392 * of how many luns have been attached on the relative target and 7393 * controller for parallel SCSI. This information is used when sd tries 7394 * to set the tagged queuing capability in HBA. 7395 */ 7396 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7397 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 7398 } 7399 7400 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7401 "sd_unit_attach: un:0x%p exit success\n", un); 7402 7403 return (DDI_SUCCESS); 7404 7405 /* 7406 * An error occurred during the attach; clean up & return failure. 7407 */ 7408 7409 devid_failed: 7410 7411 setup_pm_failed: 7412 ddi_remove_minor_node(devi, NULL); 7413 7414 cmlb_attach_failed: 7415 /* 7416 * Cleanup from the scsi_ifsetcap() calls (437868) 7417 */ 7418 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7419 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7420 7421 /* 7422 * Refer to the comments of setting tagged-qing in the beginning of 7423 * sd_unit_attach. We can only disable tagged queuing when there is 7424 * no lun attached on the target. 7425 */ 7426 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7427 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7428 } 7429 7430 if (un->un_f_is_fibre == FALSE) { 7431 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7432 } 7433 7434 spinup_failed: 7435 7436 mutex_enter(SD_MUTEX(un)); 7437 7438 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 7439 if (un->un_direct_priority_timeid != NULL) { 7440 timeout_id_t temp_id = un->un_direct_priority_timeid; 7441 un->un_direct_priority_timeid = NULL; 7442 mutex_exit(SD_MUTEX(un)); 7443 (void) untimeout(temp_id); 7444 mutex_enter(SD_MUTEX(un)); 7445 } 7446 7447 /* Cancel any pending start/stop timeouts */ 7448 if (un->un_startstop_timeid != NULL) { 7449 timeout_id_t temp_id = un->un_startstop_timeid; 7450 un->un_startstop_timeid = NULL; 7451 mutex_exit(SD_MUTEX(un)); 7452 (void) untimeout(temp_id); 7453 mutex_enter(SD_MUTEX(un)); 7454 } 7455 7456 /* Cancel any pending reset-throttle timeouts */ 7457 if (un->un_reset_throttle_timeid != NULL) { 7458 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7459 un->un_reset_throttle_timeid = NULL; 7460 mutex_exit(SD_MUTEX(un)); 7461 (void) untimeout(temp_id); 7462 mutex_enter(SD_MUTEX(un)); 7463 } 7464 7465 /* Cancel any pending retry timeouts */ 7466 if (un->un_retry_timeid != NULL) { 7467 timeout_id_t temp_id = un->un_retry_timeid; 7468 un->un_retry_timeid = NULL; 7469 mutex_exit(SD_MUTEX(un)); 7470 (void) untimeout(temp_id); 7471 mutex_enter(SD_MUTEX(un)); 7472 } 7473 7474 /* Cancel any pending delayed cv broadcast timeouts */ 7475 if (un->un_dcvb_timeid != NULL) { 7476 timeout_id_t temp_id = un->un_dcvb_timeid; 7477 un->un_dcvb_timeid = NULL; 7478 mutex_exit(SD_MUTEX(un)); 7479 (void) untimeout(temp_id); 7480 mutex_enter(SD_MUTEX(un)); 7481 } 7482 7483 mutex_exit(SD_MUTEX(un)); 7484 7485 /* There should not be any in-progress I/O so ASSERT this check */ 7486 ASSERT(un->un_ncmds_in_transport == 0); 7487 ASSERT(un->un_ncmds_in_driver == 0); 7488 7489 /* Do not free the softstate if the callback routine is active */ 7490 sd_sync_with_callback(un); 7491 7492 /* 7493 * Partition stats apparently are not used with removables. These would 7494 * not have been created during attach, so no need to clean them up... 7495 */ 7496 if (un->un_errstats != NULL) { 7497 kstat_delete(un->un_errstats); 7498 un->un_errstats = NULL; 7499 } 7500 7501 create_errstats_failed: 7502 7503 if (un->un_stats != NULL) { 7504 kstat_delete(un->un_stats); 7505 un->un_stats = NULL; 7506 } 7507 7508 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7509 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7510 7511 ddi_prop_remove_all(devi); 7512 sema_destroy(&un->un_semoclose); 7513 cv_destroy(&un->un_state_cv); 7514 7515 getrbuf_failed: 7516 7517 sd_free_rqs(un); 7518 7519 alloc_rqs_failed: 7520 7521 devp->sd_private = NULL; 7522 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 7523 7524 get_softstate_failed: 7525 /* 7526 * Note: the man pages are unclear as to whether or not doing a 7527 * ddi_soft_state_free(sd_state, instance) is the right way to 7528 * clean up after the ddi_soft_state_zalloc() if the subsequent 7529 * ddi_get_soft_state() fails. The implication seems to be 7530 * that the get_soft_state cannot fail if the zalloc succeeds. 7531 */ 7532 ddi_soft_state_free(sd_state, instance); 7533 7534 probe_failed: 7535 scsi_unprobe(devp); 7536 7537 return (DDI_FAILURE); 7538 } 7539 7540 7541 /* 7542 * Function: sd_unit_detach 7543 * 7544 * Description: Performs DDI_DETACH processing for sddetach(). 7545 * 7546 * Return Code: DDI_SUCCESS 7547 * DDI_FAILURE 7548 * 7549 * Context: Kernel thread context 7550 */ 7551 7552 static int 7553 sd_unit_detach(dev_info_t *devi) 7554 { 7555 struct scsi_device *devp; 7556 struct sd_lun *un; 7557 int i; 7558 int tgt; 7559 dev_t dev; 7560 dev_info_t *pdip = ddi_get_parent(devi); 7561 int instance = ddi_get_instance(devi); 7562 7563 mutex_enter(&sd_detach_mutex); 7564 7565 /* 7566 * Fail the detach for any of the following: 7567 * - Unable to get the sd_lun struct for the instance 7568 * - A layered driver has an outstanding open on the instance 7569 * - Another thread is already detaching this instance 7570 * - Another thread is currently performing an open 7571 */ 7572 devp = ddi_get_driver_private(devi); 7573 if ((devp == NULL) || 7574 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 7575 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 7576 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 7577 mutex_exit(&sd_detach_mutex); 7578 return (DDI_FAILURE); 7579 } 7580 7581 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 7582 7583 /* 7584 * Mark this instance as currently in a detach, to inhibit any 7585 * opens from a layered driver. 7586 */ 7587 un->un_detach_count++; 7588 mutex_exit(&sd_detach_mutex); 7589 7590 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7591 SCSI_ADDR_PROP_TARGET, -1); 7592 7593 dev = sd_make_device(SD_DEVINFO(un)); 7594 7595 #ifndef lint 7596 _NOTE(COMPETING_THREADS_NOW); 7597 #endif 7598 7599 mutex_enter(SD_MUTEX(un)); 7600 7601 /* 7602 * Fail the detach if there are any outstanding layered 7603 * opens on this device. 7604 */ 7605 for (i = 0; i < NDKMAP; i++) { 7606 if (un->un_ocmap.lyropen[i] != 0) { 7607 goto err_notclosed; 7608 } 7609 } 7610 7611 /* 7612 * Verify there are NO outstanding commands issued to this device. 7613 * ie, un_ncmds_in_transport == 0. 7614 * It's possible to have outstanding commands through the physio 7615 * code path, even though everything's closed. 7616 */ 7617 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 7618 (un->un_direct_priority_timeid != NULL) || 7619 (un->un_state == SD_STATE_RWAIT)) { 7620 mutex_exit(SD_MUTEX(un)); 7621 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7622 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 7623 goto err_stillbusy; 7624 } 7625 7626 /* 7627 * If we have the device reserved, release the reservation. 7628 */ 7629 if ((un->un_resvd_status & SD_RESERVE) && 7630 !(un->un_resvd_status & SD_LOST_RESERVE)) { 7631 mutex_exit(SD_MUTEX(un)); 7632 /* 7633 * Note: sd_reserve_release sends a command to the device 7634 * via the sd_ioctlcmd() path, and can sleep. 7635 */ 7636 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 7637 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7638 "sd_dr_detach: Cannot release reservation \n"); 7639 } 7640 } else { 7641 mutex_exit(SD_MUTEX(un)); 7642 } 7643 7644 /* 7645 * Untimeout any reserve recover, throttle reset, restart unit 7646 * and delayed broadcast timeout threads. Protect the timeout pointer 7647 * from getting nulled by their callback functions. 7648 */ 7649 mutex_enter(SD_MUTEX(un)); 7650 if (un->un_resvd_timeid != NULL) { 7651 timeout_id_t temp_id = un->un_resvd_timeid; 7652 un->un_resvd_timeid = NULL; 7653 mutex_exit(SD_MUTEX(un)); 7654 (void) untimeout(temp_id); 7655 mutex_enter(SD_MUTEX(un)); 7656 } 7657 7658 if (un->un_reset_throttle_timeid != NULL) { 7659 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7660 un->un_reset_throttle_timeid = NULL; 7661 mutex_exit(SD_MUTEX(un)); 7662 (void) untimeout(temp_id); 7663 mutex_enter(SD_MUTEX(un)); 7664 } 7665 7666 if (un->un_startstop_timeid != NULL) { 7667 timeout_id_t temp_id = un->un_startstop_timeid; 7668 un->un_startstop_timeid = NULL; 7669 mutex_exit(SD_MUTEX(un)); 7670 (void) untimeout(temp_id); 7671 mutex_enter(SD_MUTEX(un)); 7672 } 7673 7674 if (un->un_dcvb_timeid != NULL) { 7675 timeout_id_t temp_id = un->un_dcvb_timeid; 7676 un->un_dcvb_timeid = NULL; 7677 mutex_exit(SD_MUTEX(un)); 7678 (void) untimeout(temp_id); 7679 } else { 7680 mutex_exit(SD_MUTEX(un)); 7681 } 7682 7683 /* Remove any pending reservation reclaim requests for this device */ 7684 sd_rmv_resv_reclaim_req(dev); 7685 7686 mutex_enter(SD_MUTEX(un)); 7687 7688 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 7689 if (un->un_direct_priority_timeid != NULL) { 7690 timeout_id_t temp_id = un->un_direct_priority_timeid; 7691 un->un_direct_priority_timeid = NULL; 7692 mutex_exit(SD_MUTEX(un)); 7693 (void) untimeout(temp_id); 7694 mutex_enter(SD_MUTEX(un)); 7695 } 7696 7697 /* Cancel any active multi-host disk watch thread requests */ 7698 if (un->un_mhd_token != NULL) { 7699 mutex_exit(SD_MUTEX(un)); 7700 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 7701 if (scsi_watch_request_terminate(un->un_mhd_token, 7702 SCSI_WATCH_TERMINATE_NOWAIT)) { 7703 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7704 "sd_dr_detach: Cannot cancel mhd watch request\n"); 7705 /* 7706 * Note: We are returning here after having removed 7707 * some driver timeouts above. This is consistent with 7708 * the legacy implementation but perhaps the watch 7709 * terminate call should be made with the wait flag set. 7710 */ 7711 goto err_stillbusy; 7712 } 7713 mutex_enter(SD_MUTEX(un)); 7714 un->un_mhd_token = NULL; 7715 } 7716 7717 if (un->un_swr_token != NULL) { 7718 mutex_exit(SD_MUTEX(un)); 7719 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 7720 if (scsi_watch_request_terminate(un->un_swr_token, 7721 SCSI_WATCH_TERMINATE_NOWAIT)) { 7722 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7723 "sd_dr_detach: Cannot cancel swr watch request\n"); 7724 /* 7725 * Note: We are returning here after having removed 7726 * some driver timeouts above. This is consistent with 7727 * the legacy implementation but perhaps the watch 7728 * terminate call should be made with the wait flag set. 7729 */ 7730 goto err_stillbusy; 7731 } 7732 mutex_enter(SD_MUTEX(un)); 7733 un->un_swr_token = NULL; 7734 } 7735 7736 mutex_exit(SD_MUTEX(un)); 7737 7738 /* 7739 * Clear any scsi_reset_notifies. We clear the reset notifies 7740 * if we have not registered one. 7741 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 7742 */ 7743 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 7744 sd_mhd_reset_notify_cb, (caddr_t)un); 7745 7746 /* 7747 * protect the timeout pointers from getting nulled by 7748 * their callback functions during the cancellation process. 7749 * In such a scenario untimeout can be invoked with a null value. 7750 */ 7751 _NOTE(NO_COMPETING_THREADS_NOW); 7752 7753 mutex_enter(&un->un_pm_mutex); 7754 if (un->un_pm_idle_timeid != NULL) { 7755 timeout_id_t temp_id = un->un_pm_idle_timeid; 7756 un->un_pm_idle_timeid = NULL; 7757 mutex_exit(&un->un_pm_mutex); 7758 7759 /* 7760 * Timeout is active; cancel it. 7761 * Note that it'll never be active on a device 7762 * that does not support PM therefore we don't 7763 * have to check before calling pm_idle_component. 7764 */ 7765 (void) untimeout(temp_id); 7766 (void) pm_idle_component(SD_DEVINFO(un), 0); 7767 mutex_enter(&un->un_pm_mutex); 7768 } 7769 7770 /* 7771 * Check whether there is already a timeout scheduled for power 7772 * management. If yes then don't lower the power here, that's. 7773 * the timeout handler's job. 7774 */ 7775 if (un->un_pm_timeid != NULL) { 7776 timeout_id_t temp_id = un->un_pm_timeid; 7777 un->un_pm_timeid = NULL; 7778 mutex_exit(&un->un_pm_mutex); 7779 /* 7780 * Timeout is active; cancel it. 7781 * Note that it'll never be active on a device 7782 * that does not support PM therefore we don't 7783 * have to check before calling pm_idle_component. 7784 */ 7785 (void) untimeout(temp_id); 7786 (void) pm_idle_component(SD_DEVINFO(un), 0); 7787 7788 } else { 7789 mutex_exit(&un->un_pm_mutex); 7790 if ((un->un_f_pm_is_enabled == TRUE) && 7791 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 7792 DDI_SUCCESS)) { 7793 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7794 "sd_dr_detach: Lower power request failed, ignoring.\n"); 7795 /* 7796 * Fix for bug: 4297749, item # 13 7797 * The above test now includes a check to see if PM is 7798 * supported by this device before call 7799 * pm_lower_power(). 7800 * Note, the following is not dead code. The call to 7801 * pm_lower_power above will generate a call back into 7802 * our sdpower routine which might result in a timeout 7803 * handler getting activated. Therefore the following 7804 * code is valid and necessary. 7805 */ 7806 mutex_enter(&un->un_pm_mutex); 7807 if (un->un_pm_timeid != NULL) { 7808 timeout_id_t temp_id = un->un_pm_timeid; 7809 un->un_pm_timeid = NULL; 7810 mutex_exit(&un->un_pm_mutex); 7811 (void) untimeout(temp_id); 7812 (void) pm_idle_component(SD_DEVINFO(un), 0); 7813 } else { 7814 mutex_exit(&un->un_pm_mutex); 7815 } 7816 } 7817 } 7818 7819 /* 7820 * Cleanup from the scsi_ifsetcap() calls (437868) 7821 * Relocated here from above to be after the call to 7822 * pm_lower_power, which was getting errors. 7823 */ 7824 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7825 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7826 7827 /* 7828 * Currently, tagged queuing is supported per target based by HBA. 7829 * Setting this per lun instance actually sets the capability of this 7830 * target in HBA, which affects those luns already attached on the 7831 * same target. So during detach, we can only disable this capability 7832 * only when this is the only lun left on this target. By doing 7833 * this, we assume a target has the same tagged queuing capability 7834 * for every lun. The condition can be removed when HBA is changed to 7835 * support per lun based tagged queuing capability. 7836 */ 7837 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 7838 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7839 } 7840 7841 if (un->un_f_is_fibre == FALSE) { 7842 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7843 } 7844 7845 /* 7846 * Remove any event callbacks, fibre only 7847 */ 7848 if (un->un_f_is_fibre == TRUE) { 7849 if ((un->un_insert_event != NULL) && 7850 (ddi_remove_event_handler(un->un_insert_cb_id) != 7851 DDI_SUCCESS)) { 7852 /* 7853 * Note: We are returning here after having done 7854 * substantial cleanup above. This is consistent 7855 * with the legacy implementation but this may not 7856 * be the right thing to do. 7857 */ 7858 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7859 "sd_dr_detach: Cannot cancel insert event\n"); 7860 goto err_remove_event; 7861 } 7862 un->un_insert_event = NULL; 7863 7864 if ((un->un_remove_event != NULL) && 7865 (ddi_remove_event_handler(un->un_remove_cb_id) != 7866 DDI_SUCCESS)) { 7867 /* 7868 * Note: We are returning here after having done 7869 * substantial cleanup above. This is consistent 7870 * with the legacy implementation but this may not 7871 * be the right thing to do. 7872 */ 7873 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7874 "sd_dr_detach: Cannot cancel remove event\n"); 7875 goto err_remove_event; 7876 } 7877 un->un_remove_event = NULL; 7878 } 7879 7880 /* Do not free the softstate if the callback routine is active */ 7881 sd_sync_with_callback(un); 7882 7883 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 7884 cmlb_free_handle(&un->un_cmlbhandle); 7885 7886 /* 7887 * Hold the detach mutex here, to make sure that no other threads ever 7888 * can access a (partially) freed soft state structure. 7889 */ 7890 mutex_enter(&sd_detach_mutex); 7891 7892 /* 7893 * Clean up the soft state struct. 7894 * Cleanup is done in reverse order of allocs/inits. 7895 * At this point there should be no competing threads anymore. 7896 */ 7897 7898 /* Unregister and free device id. */ 7899 ddi_devid_unregister(devi); 7900 if (un->un_devid) { 7901 ddi_devid_free(un->un_devid); 7902 un->un_devid = NULL; 7903 } 7904 7905 /* 7906 * Destroy wmap cache if it exists. 7907 */ 7908 if (un->un_wm_cache != NULL) { 7909 kmem_cache_destroy(un->un_wm_cache); 7910 un->un_wm_cache = NULL; 7911 } 7912 7913 /* 7914 * kstat cleanup is done in detach for all device types (4363169). 7915 * We do not want to fail detach if the device kstats are not deleted 7916 * since there is a confusion about the devo_refcnt for the device. 7917 * We just delete the kstats and let detach complete successfully. 7918 */ 7919 if (un->un_stats != NULL) { 7920 kstat_delete(un->un_stats); 7921 un->un_stats = NULL; 7922 } 7923 if (un->un_errstats != NULL) { 7924 kstat_delete(un->un_errstats); 7925 un->un_errstats = NULL; 7926 } 7927 7928 /* Remove partition stats */ 7929 if (un->un_f_pkstats_enabled) { 7930 for (i = 0; i < NSDMAP; i++) { 7931 if (un->un_pstats[i] != NULL) { 7932 kstat_delete(un->un_pstats[i]); 7933 un->un_pstats[i] = NULL; 7934 } 7935 } 7936 } 7937 7938 /* Remove xbuf registration */ 7939 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7940 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7941 7942 /* Remove driver properties */ 7943 ddi_prop_remove_all(devi); 7944 7945 mutex_destroy(&un->un_pm_mutex); 7946 cv_destroy(&un->un_pm_busy_cv); 7947 7948 cv_destroy(&un->un_wcc_cv); 7949 7950 /* Open/close semaphore */ 7951 sema_destroy(&un->un_semoclose); 7952 7953 /* Removable media condvar. */ 7954 cv_destroy(&un->un_state_cv); 7955 7956 /* Suspend/resume condvar. */ 7957 cv_destroy(&un->un_suspend_cv); 7958 cv_destroy(&un->un_disk_busy_cv); 7959 7960 sd_free_rqs(un); 7961 7962 /* Free up soft state */ 7963 devp->sd_private = NULL; 7964 7965 bzero(un, sizeof (struct sd_lun)); 7966 ddi_soft_state_free(sd_state, instance); 7967 7968 mutex_exit(&sd_detach_mutex); 7969 7970 /* This frees up the INQUIRY data associated with the device. */ 7971 scsi_unprobe(devp); 7972 7973 /* 7974 * After successfully detaching an instance, we update the information 7975 * of how many luns have been attached in the relative target and 7976 * controller for parallel SCSI. This information is used when sd tries 7977 * to set the tagged queuing capability in HBA. 7978 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 7979 * check if the device is parallel SCSI. However, we don't need to 7980 * check here because we've already checked during attach. No device 7981 * that is not parallel SCSI is in the chain. 7982 */ 7983 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7984 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 7985 } 7986 7987 return (DDI_SUCCESS); 7988 7989 err_notclosed: 7990 mutex_exit(SD_MUTEX(un)); 7991 7992 err_stillbusy: 7993 _NOTE(NO_COMPETING_THREADS_NOW); 7994 7995 err_remove_event: 7996 mutex_enter(&sd_detach_mutex); 7997 un->un_detach_count--; 7998 mutex_exit(&sd_detach_mutex); 7999 8000 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 8001 return (DDI_FAILURE); 8002 } 8003 8004 8005 /* 8006 * Function: sd_create_errstats 8007 * 8008 * Description: This routine instantiates the device error stats. 8009 * 8010 * Note: During attach the stats are instantiated first so they are 8011 * available for attach-time routines that utilize the driver 8012 * iopath to send commands to the device. The stats are initialized 8013 * separately so data obtained during some attach-time routines is 8014 * available. (4362483) 8015 * 8016 * Arguments: un - driver soft state (unit) structure 8017 * instance - driver instance 8018 * 8019 * Context: Kernel thread context 8020 */ 8021 8022 static void 8023 sd_create_errstats(struct sd_lun *un, int instance) 8024 { 8025 struct sd_errstats *stp; 8026 char kstatmodule_err[KSTAT_STRLEN]; 8027 char kstatname[KSTAT_STRLEN]; 8028 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 8029 8030 ASSERT(un != NULL); 8031 8032 if (un->un_errstats != NULL) { 8033 return; 8034 } 8035 8036 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 8037 "%serr", sd_label); 8038 (void) snprintf(kstatname, sizeof (kstatname), 8039 "%s%d,err", sd_label, instance); 8040 8041 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 8042 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 8043 8044 if (un->un_errstats == NULL) { 8045 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8046 "sd_create_errstats: Failed kstat_create\n"); 8047 return; 8048 } 8049 8050 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8051 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 8052 KSTAT_DATA_UINT32); 8053 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 8054 KSTAT_DATA_UINT32); 8055 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 8056 KSTAT_DATA_UINT32); 8057 kstat_named_init(&stp->sd_vid, "Vendor", 8058 KSTAT_DATA_CHAR); 8059 kstat_named_init(&stp->sd_pid, "Product", 8060 KSTAT_DATA_CHAR); 8061 kstat_named_init(&stp->sd_revision, "Revision", 8062 KSTAT_DATA_CHAR); 8063 kstat_named_init(&stp->sd_serial, "Serial No", 8064 KSTAT_DATA_CHAR); 8065 kstat_named_init(&stp->sd_capacity, "Size", 8066 KSTAT_DATA_ULONGLONG); 8067 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 8068 KSTAT_DATA_UINT32); 8069 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 8070 KSTAT_DATA_UINT32); 8071 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 8072 KSTAT_DATA_UINT32); 8073 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 8074 KSTAT_DATA_UINT32); 8075 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 8076 KSTAT_DATA_UINT32); 8077 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 8078 KSTAT_DATA_UINT32); 8079 8080 un->un_errstats->ks_private = un; 8081 un->un_errstats->ks_update = nulldev; 8082 8083 kstat_install(un->un_errstats); 8084 } 8085 8086 8087 /* 8088 * Function: sd_set_errstats 8089 * 8090 * Description: This routine sets the value of the vendor id, product id, 8091 * revision, serial number, and capacity device error stats. 8092 * 8093 * Note: During attach the stats are instantiated first so they are 8094 * available for attach-time routines that utilize the driver 8095 * iopath to send commands to the device. The stats are initialized 8096 * separately so data obtained during some attach-time routines is 8097 * available. (4362483) 8098 * 8099 * Arguments: un - driver soft state (unit) structure 8100 * 8101 * Context: Kernel thread context 8102 */ 8103 8104 static void 8105 sd_set_errstats(struct sd_lun *un) 8106 { 8107 struct sd_errstats *stp; 8108 8109 ASSERT(un != NULL); 8110 ASSERT(un->un_errstats != NULL); 8111 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8112 ASSERT(stp != NULL); 8113 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 8114 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 8115 (void) strncpy(stp->sd_revision.value.c, 8116 un->un_sd->sd_inq->inq_revision, 4); 8117 8118 /* 8119 * All the errstats are persistent across detach/attach, 8120 * so reset all the errstats here in case of the hot 8121 * replacement of disk drives, except for not changed 8122 * Sun qualified drives. 8123 */ 8124 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 8125 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8126 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 8127 stp->sd_softerrs.value.ui32 = 0; 8128 stp->sd_harderrs.value.ui32 = 0; 8129 stp->sd_transerrs.value.ui32 = 0; 8130 stp->sd_rq_media_err.value.ui32 = 0; 8131 stp->sd_rq_ntrdy_err.value.ui32 = 0; 8132 stp->sd_rq_nodev_err.value.ui32 = 0; 8133 stp->sd_rq_recov_err.value.ui32 = 0; 8134 stp->sd_rq_illrq_err.value.ui32 = 0; 8135 stp->sd_rq_pfa_err.value.ui32 = 0; 8136 } 8137 8138 /* 8139 * Set the "Serial No" kstat for Sun qualified drives (indicated by 8140 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 8141 * (4376302)) 8142 */ 8143 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 8144 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8145 sizeof (SD_INQUIRY(un)->inq_serial)); 8146 } 8147 8148 if (un->un_f_blockcount_is_valid != TRUE) { 8149 /* 8150 * Set capacity error stat to 0 for no media. This ensures 8151 * a valid capacity is displayed in response to 'iostat -E' 8152 * when no media is present in the device. 8153 */ 8154 stp->sd_capacity.value.ui64 = 0; 8155 } else { 8156 /* 8157 * Multiply un_blockcount by un->un_sys_blocksize to get 8158 * capacity. 8159 * 8160 * Note: for non-512 blocksize devices "un_blockcount" has been 8161 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 8162 * (un_tgt_blocksize / un->un_sys_blocksize). 8163 */ 8164 stp->sd_capacity.value.ui64 = (uint64_t) 8165 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 8166 } 8167 } 8168 8169 8170 /* 8171 * Function: sd_set_pstats 8172 * 8173 * Description: This routine instantiates and initializes the partition 8174 * stats for each partition with more than zero blocks. 8175 * (4363169) 8176 * 8177 * Arguments: un - driver soft state (unit) structure 8178 * 8179 * Context: Kernel thread context 8180 */ 8181 8182 static void 8183 sd_set_pstats(struct sd_lun *un) 8184 { 8185 char kstatname[KSTAT_STRLEN]; 8186 int instance; 8187 int i; 8188 diskaddr_t nblks = 0; 8189 char *partname = NULL; 8190 8191 ASSERT(un != NULL); 8192 8193 instance = ddi_get_instance(SD_DEVINFO(un)); 8194 8195 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 8196 for (i = 0; i < NSDMAP; i++) { 8197 8198 if (cmlb_partinfo(un->un_cmlbhandle, i, 8199 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 8200 continue; 8201 mutex_enter(SD_MUTEX(un)); 8202 8203 if ((un->un_pstats[i] == NULL) && 8204 (nblks != 0)) { 8205 8206 (void) snprintf(kstatname, sizeof (kstatname), 8207 "%s%d,%s", sd_label, instance, 8208 partname); 8209 8210 un->un_pstats[i] = kstat_create(sd_label, 8211 instance, kstatname, "partition", KSTAT_TYPE_IO, 8212 1, KSTAT_FLAG_PERSISTENT); 8213 if (un->un_pstats[i] != NULL) { 8214 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 8215 kstat_install(un->un_pstats[i]); 8216 } 8217 } 8218 mutex_exit(SD_MUTEX(un)); 8219 } 8220 } 8221 8222 8223 #if (defined(__fibre)) 8224 /* 8225 * Function: sd_init_event_callbacks 8226 * 8227 * Description: This routine initializes the insertion and removal event 8228 * callbacks. (fibre only) 8229 * 8230 * Arguments: un - driver soft state (unit) structure 8231 * 8232 * Context: Kernel thread context 8233 */ 8234 8235 static void 8236 sd_init_event_callbacks(struct sd_lun *un) 8237 { 8238 ASSERT(un != NULL); 8239 8240 if ((un->un_insert_event == NULL) && 8241 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 8242 &un->un_insert_event) == DDI_SUCCESS)) { 8243 /* 8244 * Add the callback for an insertion event 8245 */ 8246 (void) ddi_add_event_handler(SD_DEVINFO(un), 8247 un->un_insert_event, sd_event_callback, (void *)un, 8248 &(un->un_insert_cb_id)); 8249 } 8250 8251 if ((un->un_remove_event == NULL) && 8252 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 8253 &un->un_remove_event) == DDI_SUCCESS)) { 8254 /* 8255 * Add the callback for a removal event 8256 */ 8257 (void) ddi_add_event_handler(SD_DEVINFO(un), 8258 un->un_remove_event, sd_event_callback, (void *)un, 8259 &(un->un_remove_cb_id)); 8260 } 8261 } 8262 8263 8264 /* 8265 * Function: sd_event_callback 8266 * 8267 * Description: This routine handles insert/remove events (photon). The 8268 * state is changed to OFFLINE which can be used to supress 8269 * error msgs. (fibre only) 8270 * 8271 * Arguments: un - driver soft state (unit) structure 8272 * 8273 * Context: Callout thread context 8274 */ 8275 /* ARGSUSED */ 8276 static void 8277 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 8278 void *bus_impldata) 8279 { 8280 struct sd_lun *un = (struct sd_lun *)arg; 8281 8282 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 8283 if (event == un->un_insert_event) { 8284 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 8285 mutex_enter(SD_MUTEX(un)); 8286 if (un->un_state == SD_STATE_OFFLINE) { 8287 if (un->un_last_state != SD_STATE_SUSPENDED) { 8288 un->un_state = un->un_last_state; 8289 } else { 8290 /* 8291 * We have gone through SUSPEND/RESUME while 8292 * we were offline. Restore the last state 8293 */ 8294 un->un_state = un->un_save_state; 8295 } 8296 } 8297 mutex_exit(SD_MUTEX(un)); 8298 8299 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 8300 } else if (event == un->un_remove_event) { 8301 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 8302 mutex_enter(SD_MUTEX(un)); 8303 /* 8304 * We need to handle an event callback that occurs during 8305 * the suspend operation, since we don't prevent it. 8306 */ 8307 if (un->un_state != SD_STATE_OFFLINE) { 8308 if (un->un_state != SD_STATE_SUSPENDED) { 8309 New_state(un, SD_STATE_OFFLINE); 8310 } else { 8311 un->un_last_state = SD_STATE_OFFLINE; 8312 } 8313 } 8314 mutex_exit(SD_MUTEX(un)); 8315 } else { 8316 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 8317 "!Unknown event\n"); 8318 } 8319 8320 } 8321 #endif 8322 8323 /* 8324 * Function: sd_cache_control() 8325 * 8326 * Description: This routine is the driver entry point for setting 8327 * read and write caching by modifying the WCE (write cache 8328 * enable) and RCD (read cache disable) bits of mode 8329 * page 8 (MODEPAGE_CACHING). 8330 * 8331 * Arguments: un - driver soft state (unit) structure 8332 * rcd_flag - flag for controlling the read cache 8333 * wce_flag - flag for controlling the write cache 8334 * 8335 * Return Code: EIO 8336 * code returned by sd_send_scsi_MODE_SENSE and 8337 * sd_send_scsi_MODE_SELECT 8338 * 8339 * Context: Kernel Thread 8340 */ 8341 8342 static int 8343 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 8344 { 8345 struct mode_caching *mode_caching_page; 8346 uchar_t *header; 8347 size_t buflen; 8348 int hdrlen; 8349 int bd_len; 8350 int rval = 0; 8351 struct mode_header_grp2 *mhp; 8352 8353 ASSERT(un != NULL); 8354 8355 /* 8356 * Do a test unit ready, otherwise a mode sense may not work if this 8357 * is the first command sent to the device after boot. 8358 */ 8359 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8360 8361 if (un->un_f_cfg_is_atapi == TRUE) { 8362 hdrlen = MODE_HEADER_LENGTH_GRP2; 8363 } else { 8364 hdrlen = MODE_HEADER_LENGTH; 8365 } 8366 8367 /* 8368 * Allocate memory for the retrieved mode page and its headers. Set 8369 * a pointer to the page itself. Use mode_cache_scsi3 to insure 8370 * we get all of the mode sense data otherwise, the mode select 8371 * will fail. mode_cache_scsi3 is a superset of mode_caching. 8372 */ 8373 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 8374 sizeof (struct mode_cache_scsi3); 8375 8376 header = kmem_zalloc(buflen, KM_SLEEP); 8377 8378 /* Get the information from the device. */ 8379 if (un->un_f_cfg_is_atapi == TRUE) { 8380 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8381 MODEPAGE_CACHING, SD_PATH_DIRECT); 8382 } else { 8383 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8384 MODEPAGE_CACHING, SD_PATH_DIRECT); 8385 } 8386 if (rval != 0) { 8387 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8388 "sd_cache_control: Mode Sense Failed\n"); 8389 kmem_free(header, buflen); 8390 return (rval); 8391 } 8392 8393 /* 8394 * Determine size of Block Descriptors in order to locate 8395 * the mode page data. ATAPI devices return 0, SCSI devices 8396 * should return MODE_BLK_DESC_LENGTH. 8397 */ 8398 if (un->un_f_cfg_is_atapi == TRUE) { 8399 mhp = (struct mode_header_grp2 *)header; 8400 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8401 } else { 8402 bd_len = ((struct mode_header *)header)->bdesc_length; 8403 } 8404 8405 if (bd_len > MODE_BLK_DESC_LENGTH) { 8406 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8407 "sd_cache_control: Mode Sense returned invalid " 8408 "block descriptor length\n"); 8409 kmem_free(header, buflen); 8410 return (EIO); 8411 } 8412 8413 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8414 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8415 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8416 " caching page code mismatch %d\n", 8417 mode_caching_page->mode_page.code); 8418 kmem_free(header, buflen); 8419 return (EIO); 8420 } 8421 8422 /* Check the relevant bits on successful mode sense. */ 8423 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 8424 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 8425 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 8426 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 8427 8428 size_t sbuflen; 8429 uchar_t save_pg; 8430 8431 /* 8432 * Construct select buffer length based on the 8433 * length of the sense data returned. 8434 */ 8435 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 8436 sizeof (struct mode_page) + 8437 (int)mode_caching_page->mode_page.length; 8438 8439 /* 8440 * Set the caching bits as requested. 8441 */ 8442 if (rcd_flag == SD_CACHE_ENABLE) 8443 mode_caching_page->rcd = 0; 8444 else if (rcd_flag == SD_CACHE_DISABLE) 8445 mode_caching_page->rcd = 1; 8446 8447 if (wce_flag == SD_CACHE_ENABLE) 8448 mode_caching_page->wce = 1; 8449 else if (wce_flag == SD_CACHE_DISABLE) 8450 mode_caching_page->wce = 0; 8451 8452 /* 8453 * Save the page if the mode sense says the 8454 * drive supports it. 8455 */ 8456 save_pg = mode_caching_page->mode_page.ps ? 8457 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 8458 8459 /* Clear reserved bits before mode select. */ 8460 mode_caching_page->mode_page.ps = 0; 8461 8462 /* 8463 * Clear out mode header for mode select. 8464 * The rest of the retrieved page will be reused. 8465 */ 8466 bzero(header, hdrlen); 8467 8468 if (un->un_f_cfg_is_atapi == TRUE) { 8469 mhp = (struct mode_header_grp2 *)header; 8470 mhp->bdesc_length_hi = bd_len >> 8; 8471 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 8472 } else { 8473 ((struct mode_header *)header)->bdesc_length = bd_len; 8474 } 8475 8476 /* Issue mode select to change the cache settings */ 8477 if (un->un_f_cfg_is_atapi == TRUE) { 8478 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 8479 sbuflen, save_pg, SD_PATH_DIRECT); 8480 } else { 8481 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 8482 sbuflen, save_pg, SD_PATH_DIRECT); 8483 } 8484 } 8485 8486 kmem_free(header, buflen); 8487 return (rval); 8488 } 8489 8490 8491 /* 8492 * Function: sd_get_write_cache_enabled() 8493 * 8494 * Description: This routine is the driver entry point for determining if 8495 * write caching is enabled. It examines the WCE (write cache 8496 * enable) bits of mode page 8 (MODEPAGE_CACHING). 8497 * 8498 * Arguments: un - driver soft state (unit) structure 8499 * is_enabled - pointer to int where write cache enabled state 8500 * is returned (non-zero -> write cache enabled) 8501 * 8502 * 8503 * Return Code: EIO 8504 * code returned by sd_send_scsi_MODE_SENSE 8505 * 8506 * Context: Kernel Thread 8507 * 8508 * NOTE: If ioctl is added to disable write cache, this sequence should 8509 * be followed so that no locking is required for accesses to 8510 * un->un_f_write_cache_enabled: 8511 * do mode select to clear wce 8512 * do synchronize cache to flush cache 8513 * set un->un_f_write_cache_enabled = FALSE 8514 * 8515 * Conversely, an ioctl to enable the write cache should be done 8516 * in this order: 8517 * set un->un_f_write_cache_enabled = TRUE 8518 * do mode select to set wce 8519 */ 8520 8521 static int 8522 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 8523 { 8524 struct mode_caching *mode_caching_page; 8525 uchar_t *header; 8526 size_t buflen; 8527 int hdrlen; 8528 int bd_len; 8529 int rval = 0; 8530 8531 ASSERT(un != NULL); 8532 ASSERT(is_enabled != NULL); 8533 8534 /* in case of error, flag as enabled */ 8535 *is_enabled = TRUE; 8536 8537 /* 8538 * Do a test unit ready, otherwise a mode sense may not work if this 8539 * is the first command sent to the device after boot. 8540 */ 8541 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8542 8543 if (un->un_f_cfg_is_atapi == TRUE) { 8544 hdrlen = MODE_HEADER_LENGTH_GRP2; 8545 } else { 8546 hdrlen = MODE_HEADER_LENGTH; 8547 } 8548 8549 /* 8550 * Allocate memory for the retrieved mode page and its headers. Set 8551 * a pointer to the page itself. 8552 */ 8553 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 8554 header = kmem_zalloc(buflen, KM_SLEEP); 8555 8556 /* Get the information from the device. */ 8557 if (un->un_f_cfg_is_atapi == TRUE) { 8558 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8559 MODEPAGE_CACHING, SD_PATH_DIRECT); 8560 } else { 8561 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8562 MODEPAGE_CACHING, SD_PATH_DIRECT); 8563 } 8564 if (rval != 0) { 8565 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8566 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 8567 kmem_free(header, buflen); 8568 return (rval); 8569 } 8570 8571 /* 8572 * Determine size of Block Descriptors in order to locate 8573 * the mode page data. ATAPI devices return 0, SCSI devices 8574 * should return MODE_BLK_DESC_LENGTH. 8575 */ 8576 if (un->un_f_cfg_is_atapi == TRUE) { 8577 struct mode_header_grp2 *mhp; 8578 mhp = (struct mode_header_grp2 *)header; 8579 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8580 } else { 8581 bd_len = ((struct mode_header *)header)->bdesc_length; 8582 } 8583 8584 if (bd_len > MODE_BLK_DESC_LENGTH) { 8585 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8586 "sd_get_write_cache_enabled: Mode Sense returned invalid " 8587 "block descriptor length\n"); 8588 kmem_free(header, buflen); 8589 return (EIO); 8590 } 8591 8592 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8593 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8594 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8595 " caching page code mismatch %d\n", 8596 mode_caching_page->mode_page.code); 8597 kmem_free(header, buflen); 8598 return (EIO); 8599 } 8600 *is_enabled = mode_caching_page->wce; 8601 8602 kmem_free(header, buflen); 8603 return (0); 8604 } 8605 8606 /* 8607 * Function: sd_get_nv_sup() 8608 * 8609 * Description: This routine is the driver entry point for 8610 * determining whether non-volatile cache is supported. This 8611 * determination process works as follows: 8612 * 8613 * 1. sd first queries sd.conf on whether 8614 * suppress_cache_flush bit is set for this device. 8615 * 8616 * 2. if not there, then queries the internal disk table. 8617 * 8618 * 3. if either sd.conf or internal disk table specifies 8619 * cache flush be suppressed, we don't bother checking 8620 * NV_SUP bit. 8621 * 8622 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 8623 * the optional INQUIRY VPD page 0x86. If the device 8624 * supports VPD page 0x86, sd examines the NV_SUP 8625 * (non-volatile cache support) bit in the INQUIRY VPD page 8626 * 0x86: 8627 * o If NV_SUP bit is set, sd assumes the device has a 8628 * non-volatile cache and set the 8629 * un_f_sync_nv_supported to TRUE. 8630 * o Otherwise cache is not non-volatile, 8631 * un_f_sync_nv_supported is set to FALSE. 8632 * 8633 * Arguments: un - driver soft state (unit) structure 8634 * 8635 * Return Code: 8636 * 8637 * Context: Kernel Thread 8638 */ 8639 8640 static void 8641 sd_get_nv_sup(struct sd_lun *un) 8642 { 8643 int rval = 0; 8644 uchar_t *inq86 = NULL; 8645 size_t inq86_len = MAX_INQUIRY_SIZE; 8646 size_t inq86_resid = 0; 8647 struct dk_callback *dkc; 8648 8649 ASSERT(un != NULL); 8650 8651 mutex_enter(SD_MUTEX(un)); 8652 8653 /* 8654 * Be conservative on the device's support of 8655 * SYNC_NV bit: un_f_sync_nv_supported is 8656 * initialized to be false. 8657 */ 8658 un->un_f_sync_nv_supported = FALSE; 8659 8660 /* 8661 * If either sd.conf or internal disk table 8662 * specifies cache flush be suppressed, then 8663 * we don't bother checking NV_SUP bit. 8664 */ 8665 if (un->un_f_suppress_cache_flush == TRUE) { 8666 mutex_exit(SD_MUTEX(un)); 8667 return; 8668 } 8669 8670 if (sd_check_vpd_page_support(un) == 0 && 8671 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 8672 mutex_exit(SD_MUTEX(un)); 8673 /* collect page 86 data if available */ 8674 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 8675 rval = sd_send_scsi_INQUIRY(un, inq86, inq86_len, 8676 0x01, 0x86, &inq86_resid); 8677 8678 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 8679 SD_TRACE(SD_LOG_COMMON, un, 8680 "sd_get_nv_sup: \ 8681 successfully get VPD page: %x \ 8682 PAGE LENGTH: %x BYTE 6: %x\n", 8683 inq86[1], inq86[3], inq86[6]); 8684 8685 mutex_enter(SD_MUTEX(un)); 8686 /* 8687 * check the value of NV_SUP bit: only if the device 8688 * reports NV_SUP bit to be 1, the 8689 * un_f_sync_nv_supported bit will be set to true. 8690 */ 8691 if (inq86[6] & SD_VPD_NV_SUP) { 8692 un->un_f_sync_nv_supported = TRUE; 8693 } 8694 mutex_exit(SD_MUTEX(un)); 8695 } 8696 kmem_free(inq86, inq86_len); 8697 } else { 8698 mutex_exit(SD_MUTEX(un)); 8699 } 8700 8701 /* 8702 * Send a SYNC CACHE command to check whether 8703 * SYNC_NV bit is supported. This command should have 8704 * un_f_sync_nv_supported set to correct value. 8705 */ 8706 mutex_enter(SD_MUTEX(un)); 8707 if (un->un_f_sync_nv_supported) { 8708 mutex_exit(SD_MUTEX(un)); 8709 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 8710 dkc->dkc_flag = FLUSH_VOLATILE; 8711 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 8712 8713 /* 8714 * Send a TEST UNIT READY command to the device. This should 8715 * clear any outstanding UNIT ATTENTION that may be present. 8716 */ 8717 (void) sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR); 8718 8719 kmem_free(dkc, sizeof (struct dk_callback)); 8720 } else { 8721 mutex_exit(SD_MUTEX(un)); 8722 } 8723 8724 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 8725 un_f_suppress_cache_flush is set to %d\n", 8726 un->un_f_suppress_cache_flush); 8727 } 8728 8729 /* 8730 * Function: sd_make_device 8731 * 8732 * Description: Utility routine to return the Solaris device number from 8733 * the data in the device's dev_info structure. 8734 * 8735 * Return Code: The Solaris device number 8736 * 8737 * Context: Any 8738 */ 8739 8740 static dev_t 8741 sd_make_device(dev_info_t *devi) 8742 { 8743 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 8744 ddi_get_instance(devi) << SDUNIT_SHIFT)); 8745 } 8746 8747 8748 /* 8749 * Function: sd_pm_entry 8750 * 8751 * Description: Called at the start of a new command to manage power 8752 * and busy status of a device. This includes determining whether 8753 * the current power state of the device is sufficient for 8754 * performing the command or whether it must be changed. 8755 * The PM framework is notified appropriately. 8756 * Only with a return status of DDI_SUCCESS will the 8757 * component be busy to the framework. 8758 * 8759 * All callers of sd_pm_entry must check the return status 8760 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 8761 * of DDI_FAILURE indicates the device failed to power up. 8762 * In this case un_pm_count has been adjusted so the result 8763 * on exit is still powered down, ie. count is less than 0. 8764 * Calling sd_pm_exit with this count value hits an ASSERT. 8765 * 8766 * Return Code: DDI_SUCCESS or DDI_FAILURE 8767 * 8768 * Context: Kernel thread context. 8769 */ 8770 8771 static int 8772 sd_pm_entry(struct sd_lun *un) 8773 { 8774 int return_status = DDI_SUCCESS; 8775 8776 ASSERT(!mutex_owned(SD_MUTEX(un))); 8777 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8778 8779 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 8780 8781 if (un->un_f_pm_is_enabled == FALSE) { 8782 SD_TRACE(SD_LOG_IO_PM, un, 8783 "sd_pm_entry: exiting, PM not enabled\n"); 8784 return (return_status); 8785 } 8786 8787 /* 8788 * Just increment a counter if PM is enabled. On the transition from 8789 * 0 ==> 1, mark the device as busy. The iodone side will decrement 8790 * the count with each IO and mark the device as idle when the count 8791 * hits 0. 8792 * 8793 * If the count is less than 0 the device is powered down. If a powered 8794 * down device is successfully powered up then the count must be 8795 * incremented to reflect the power up. Note that it'll get incremented 8796 * a second time to become busy. 8797 * 8798 * Because the following has the potential to change the device state 8799 * and must release the un_pm_mutex to do so, only one thread can be 8800 * allowed through at a time. 8801 */ 8802 8803 mutex_enter(&un->un_pm_mutex); 8804 while (un->un_pm_busy == TRUE) { 8805 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 8806 } 8807 un->un_pm_busy = TRUE; 8808 8809 if (un->un_pm_count < 1) { 8810 8811 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 8812 8813 /* 8814 * Indicate we are now busy so the framework won't attempt to 8815 * power down the device. This call will only fail if either 8816 * we passed a bad component number or the device has no 8817 * components. Neither of these should ever happen. 8818 */ 8819 mutex_exit(&un->un_pm_mutex); 8820 return_status = pm_busy_component(SD_DEVINFO(un), 0); 8821 ASSERT(return_status == DDI_SUCCESS); 8822 8823 mutex_enter(&un->un_pm_mutex); 8824 8825 if (un->un_pm_count < 0) { 8826 mutex_exit(&un->un_pm_mutex); 8827 8828 SD_TRACE(SD_LOG_IO_PM, un, 8829 "sd_pm_entry: power up component\n"); 8830 8831 /* 8832 * pm_raise_power will cause sdpower to be called 8833 * which brings the device power level to the 8834 * desired state, ON in this case. If successful, 8835 * un_pm_count and un_power_level will be updated 8836 * appropriately. 8837 */ 8838 return_status = pm_raise_power(SD_DEVINFO(un), 0, 8839 SD_SPINDLE_ON); 8840 8841 mutex_enter(&un->un_pm_mutex); 8842 8843 if (return_status != DDI_SUCCESS) { 8844 /* 8845 * Power up failed. 8846 * Idle the device and adjust the count 8847 * so the result on exit is that we're 8848 * still powered down, ie. count is less than 0. 8849 */ 8850 SD_TRACE(SD_LOG_IO_PM, un, 8851 "sd_pm_entry: power up failed," 8852 " idle the component\n"); 8853 8854 (void) pm_idle_component(SD_DEVINFO(un), 0); 8855 un->un_pm_count--; 8856 } else { 8857 /* 8858 * Device is powered up, verify the 8859 * count is non-negative. 8860 * This is debug only. 8861 */ 8862 ASSERT(un->un_pm_count == 0); 8863 } 8864 } 8865 8866 if (return_status == DDI_SUCCESS) { 8867 /* 8868 * For performance, now that the device has been tagged 8869 * as busy, and it's known to be powered up, update the 8870 * chain types to use jump tables that do not include 8871 * pm. This significantly lowers the overhead and 8872 * therefore improves performance. 8873 */ 8874 8875 mutex_exit(&un->un_pm_mutex); 8876 mutex_enter(SD_MUTEX(un)); 8877 SD_TRACE(SD_LOG_IO_PM, un, 8878 "sd_pm_entry: changing uscsi_chain_type from %d\n", 8879 un->un_uscsi_chain_type); 8880 8881 if (un->un_f_non_devbsize_supported) { 8882 un->un_buf_chain_type = 8883 SD_CHAIN_INFO_RMMEDIA_NO_PM; 8884 } else { 8885 un->un_buf_chain_type = 8886 SD_CHAIN_INFO_DISK_NO_PM; 8887 } 8888 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8889 8890 SD_TRACE(SD_LOG_IO_PM, un, 8891 " changed uscsi_chain_type to %d\n", 8892 un->un_uscsi_chain_type); 8893 mutex_exit(SD_MUTEX(un)); 8894 mutex_enter(&un->un_pm_mutex); 8895 8896 if (un->un_pm_idle_timeid == NULL) { 8897 /* 300 ms. */ 8898 un->un_pm_idle_timeid = 8899 timeout(sd_pm_idletimeout_handler, un, 8900 (drv_usectohz((clock_t)300000))); 8901 /* 8902 * Include an extra call to busy which keeps the 8903 * device busy with-respect-to the PM layer 8904 * until the timer fires, at which time it'll 8905 * get the extra idle call. 8906 */ 8907 (void) pm_busy_component(SD_DEVINFO(un), 0); 8908 } 8909 } 8910 } 8911 un->un_pm_busy = FALSE; 8912 /* Next... */ 8913 cv_signal(&un->un_pm_busy_cv); 8914 8915 un->un_pm_count++; 8916 8917 SD_TRACE(SD_LOG_IO_PM, un, 8918 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 8919 8920 mutex_exit(&un->un_pm_mutex); 8921 8922 return (return_status); 8923 } 8924 8925 8926 /* 8927 * Function: sd_pm_exit 8928 * 8929 * Description: Called at the completion of a command to manage busy 8930 * status for the device. If the device becomes idle the 8931 * PM framework is notified. 8932 * 8933 * Context: Kernel thread context 8934 */ 8935 8936 static void 8937 sd_pm_exit(struct sd_lun *un) 8938 { 8939 ASSERT(!mutex_owned(SD_MUTEX(un))); 8940 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8941 8942 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 8943 8944 /* 8945 * After attach the following flag is only read, so don't 8946 * take the penalty of acquiring a mutex for it. 8947 */ 8948 if (un->un_f_pm_is_enabled == TRUE) { 8949 8950 mutex_enter(&un->un_pm_mutex); 8951 un->un_pm_count--; 8952 8953 SD_TRACE(SD_LOG_IO_PM, un, 8954 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 8955 8956 ASSERT(un->un_pm_count >= 0); 8957 if (un->un_pm_count == 0) { 8958 mutex_exit(&un->un_pm_mutex); 8959 8960 SD_TRACE(SD_LOG_IO_PM, un, 8961 "sd_pm_exit: idle component\n"); 8962 8963 (void) pm_idle_component(SD_DEVINFO(un), 0); 8964 8965 } else { 8966 mutex_exit(&un->un_pm_mutex); 8967 } 8968 } 8969 8970 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 8971 } 8972 8973 8974 /* 8975 * Function: sdopen 8976 * 8977 * Description: Driver's open(9e) entry point function. 8978 * 8979 * Arguments: dev_i - pointer to device number 8980 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 8981 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 8982 * cred_p - user credential pointer 8983 * 8984 * Return Code: EINVAL 8985 * ENXIO 8986 * EIO 8987 * EROFS 8988 * EBUSY 8989 * 8990 * Context: Kernel thread context 8991 */ 8992 /* ARGSUSED */ 8993 static int 8994 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 8995 { 8996 struct sd_lun *un; 8997 int nodelay; 8998 int part; 8999 uint64_t partmask; 9000 int instance; 9001 dev_t dev; 9002 int rval = EIO; 9003 diskaddr_t nblks = 0; 9004 diskaddr_t label_cap; 9005 9006 /* Validate the open type */ 9007 if (otyp >= OTYPCNT) { 9008 return (EINVAL); 9009 } 9010 9011 dev = *dev_p; 9012 instance = SDUNIT(dev); 9013 mutex_enter(&sd_detach_mutex); 9014 9015 /* 9016 * Fail the open if there is no softstate for the instance, or 9017 * if another thread somewhere is trying to detach the instance. 9018 */ 9019 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 9020 (un->un_detach_count != 0)) { 9021 mutex_exit(&sd_detach_mutex); 9022 /* 9023 * The probe cache only needs to be cleared when open (9e) fails 9024 * with ENXIO (4238046). 9025 */ 9026 /* 9027 * un-conditionally clearing probe cache is ok with 9028 * separate sd/ssd binaries 9029 * x86 platform can be an issue with both parallel 9030 * and fibre in 1 binary 9031 */ 9032 sd_scsi_clear_probe_cache(); 9033 return (ENXIO); 9034 } 9035 9036 /* 9037 * The un_layer_count is to prevent another thread in specfs from 9038 * trying to detach the instance, which can happen when we are 9039 * called from a higher-layer driver instead of thru specfs. 9040 * This will not be needed when DDI provides a layered driver 9041 * interface that allows specfs to know that an instance is in 9042 * use by a layered driver & should not be detached. 9043 * 9044 * Note: the semantics for layered driver opens are exactly one 9045 * close for every open. 9046 */ 9047 if (otyp == OTYP_LYR) { 9048 un->un_layer_count++; 9049 } 9050 9051 /* 9052 * Keep a count of the current # of opens in progress. This is because 9053 * some layered drivers try to call us as a regular open. This can 9054 * cause problems that we cannot prevent, however by keeping this count 9055 * we can at least keep our open and detach routines from racing against 9056 * each other under such conditions. 9057 */ 9058 un->un_opens_in_progress++; 9059 mutex_exit(&sd_detach_mutex); 9060 9061 nodelay = (flag & (FNDELAY | FNONBLOCK)); 9062 part = SDPART(dev); 9063 partmask = 1 << part; 9064 9065 /* 9066 * We use a semaphore here in order to serialize 9067 * open and close requests on the device. 9068 */ 9069 sema_p(&un->un_semoclose); 9070 9071 mutex_enter(SD_MUTEX(un)); 9072 9073 /* 9074 * All device accesses go thru sdstrategy() where we check 9075 * on suspend status but there could be a scsi_poll command, 9076 * which bypasses sdstrategy(), so we need to check pm 9077 * status. 9078 */ 9079 9080 if (!nodelay) { 9081 while ((un->un_state == SD_STATE_SUSPENDED) || 9082 (un->un_state == SD_STATE_PM_CHANGING)) { 9083 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9084 } 9085 9086 mutex_exit(SD_MUTEX(un)); 9087 if (sd_pm_entry(un) != DDI_SUCCESS) { 9088 rval = EIO; 9089 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 9090 "sdopen: sd_pm_entry failed\n"); 9091 goto open_failed_with_pm; 9092 } 9093 mutex_enter(SD_MUTEX(un)); 9094 } 9095 9096 /* check for previous exclusive open */ 9097 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 9098 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9099 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 9100 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 9101 9102 if (un->un_exclopen & (partmask)) { 9103 goto excl_open_fail; 9104 } 9105 9106 if (flag & FEXCL) { 9107 int i; 9108 if (un->un_ocmap.lyropen[part]) { 9109 goto excl_open_fail; 9110 } 9111 for (i = 0; i < (OTYPCNT - 1); i++) { 9112 if (un->un_ocmap.regopen[i] & (partmask)) { 9113 goto excl_open_fail; 9114 } 9115 } 9116 } 9117 9118 /* 9119 * Check the write permission if this is a removable media device, 9120 * NDELAY has not been set, and writable permission is requested. 9121 * 9122 * Note: If NDELAY was set and this is write-protected media the WRITE 9123 * attempt will fail with EIO as part of the I/O processing. This is a 9124 * more permissive implementation that allows the open to succeed and 9125 * WRITE attempts to fail when appropriate. 9126 */ 9127 if (un->un_f_chk_wp_open) { 9128 if ((flag & FWRITE) && (!nodelay)) { 9129 mutex_exit(SD_MUTEX(un)); 9130 /* 9131 * Defer the check for write permission on writable 9132 * DVD drive till sdstrategy and will not fail open even 9133 * if FWRITE is set as the device can be writable 9134 * depending upon the media and the media can change 9135 * after the call to open(). 9136 */ 9137 if (un->un_f_dvdram_writable_device == FALSE) { 9138 if (ISCD(un) || sr_check_wp(dev)) { 9139 rval = EROFS; 9140 mutex_enter(SD_MUTEX(un)); 9141 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9142 "write to cd or write protected media\n"); 9143 goto open_fail; 9144 } 9145 } 9146 mutex_enter(SD_MUTEX(un)); 9147 } 9148 } 9149 9150 /* 9151 * If opening in NDELAY/NONBLOCK mode, just return. 9152 * Check if disk is ready and has a valid geometry later. 9153 */ 9154 if (!nodelay) { 9155 mutex_exit(SD_MUTEX(un)); 9156 rval = sd_ready_and_valid(un); 9157 mutex_enter(SD_MUTEX(un)); 9158 /* 9159 * Fail if device is not ready or if the number of disk 9160 * blocks is zero or negative for non CD devices. 9161 */ 9162 9163 nblks = 0; 9164 9165 if (rval == SD_READY_VALID && (!ISCD(un))) { 9166 /* if cmlb_partinfo fails, nblks remains 0 */ 9167 mutex_exit(SD_MUTEX(un)); 9168 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 9169 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 9170 mutex_enter(SD_MUTEX(un)); 9171 } 9172 9173 if ((rval != SD_READY_VALID) || 9174 (!ISCD(un) && nblks <= 0)) { 9175 rval = un->un_f_has_removable_media ? ENXIO : EIO; 9176 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9177 "device not ready or invalid disk block value\n"); 9178 goto open_fail; 9179 } 9180 #if defined(__i386) || defined(__amd64) 9181 } else { 9182 uchar_t *cp; 9183 /* 9184 * x86 requires special nodelay handling, so that p0 is 9185 * always defined and accessible. 9186 * Invalidate geometry only if device is not already open. 9187 */ 9188 cp = &un->un_ocmap.chkd[0]; 9189 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9190 if (*cp != (uchar_t)0) { 9191 break; 9192 } 9193 cp++; 9194 } 9195 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9196 mutex_exit(SD_MUTEX(un)); 9197 cmlb_invalidate(un->un_cmlbhandle, 9198 (void *)SD_PATH_DIRECT); 9199 mutex_enter(SD_MUTEX(un)); 9200 } 9201 9202 #endif 9203 } 9204 9205 if (otyp == OTYP_LYR) { 9206 un->un_ocmap.lyropen[part]++; 9207 } else { 9208 un->un_ocmap.regopen[otyp] |= partmask; 9209 } 9210 9211 /* Set up open and exclusive open flags */ 9212 if (flag & FEXCL) { 9213 un->un_exclopen |= (partmask); 9214 } 9215 9216 /* 9217 * If the lun is EFI labeled and lun capacity is greater than the 9218 * capacity contained in the label, log a sys-event to notify the 9219 * interested module. 9220 * To avoid an infinite loop of logging sys-event, we only log the 9221 * event when the lun is not opened in NDELAY mode. The event handler 9222 * should open the lun in NDELAY mode. 9223 */ 9224 if (!(flag & FNDELAY)) { 9225 mutex_exit(SD_MUTEX(un)); 9226 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 9227 (void*)SD_PATH_DIRECT) == 0) { 9228 mutex_enter(SD_MUTEX(un)); 9229 if (un->un_f_blockcount_is_valid && 9230 un->un_blockcount > label_cap) { 9231 mutex_exit(SD_MUTEX(un)); 9232 sd_log_lun_expansion_event(un, 9233 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 9234 mutex_enter(SD_MUTEX(un)); 9235 } 9236 } else { 9237 mutex_enter(SD_MUTEX(un)); 9238 } 9239 } 9240 9241 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9242 "open of part %d type %d\n", part, otyp); 9243 9244 mutex_exit(SD_MUTEX(un)); 9245 if (!nodelay) { 9246 sd_pm_exit(un); 9247 } 9248 9249 sema_v(&un->un_semoclose); 9250 9251 mutex_enter(&sd_detach_mutex); 9252 un->un_opens_in_progress--; 9253 mutex_exit(&sd_detach_mutex); 9254 9255 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 9256 return (DDI_SUCCESS); 9257 9258 excl_open_fail: 9259 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 9260 rval = EBUSY; 9261 9262 open_fail: 9263 mutex_exit(SD_MUTEX(un)); 9264 9265 /* 9266 * On a failed open we must exit the pm management. 9267 */ 9268 if (!nodelay) { 9269 sd_pm_exit(un); 9270 } 9271 open_failed_with_pm: 9272 sema_v(&un->un_semoclose); 9273 9274 mutex_enter(&sd_detach_mutex); 9275 un->un_opens_in_progress--; 9276 if (otyp == OTYP_LYR) { 9277 un->un_layer_count--; 9278 } 9279 mutex_exit(&sd_detach_mutex); 9280 9281 return (rval); 9282 } 9283 9284 9285 /* 9286 * Function: sdclose 9287 * 9288 * Description: Driver's close(9e) entry point function. 9289 * 9290 * Arguments: dev - device number 9291 * flag - file status flag, informational only 9292 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9293 * cred_p - user credential pointer 9294 * 9295 * Return Code: ENXIO 9296 * 9297 * Context: Kernel thread context 9298 */ 9299 /* ARGSUSED */ 9300 static int 9301 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 9302 { 9303 struct sd_lun *un; 9304 uchar_t *cp; 9305 int part; 9306 int nodelay; 9307 int rval = 0; 9308 9309 /* Validate the open type */ 9310 if (otyp >= OTYPCNT) { 9311 return (ENXIO); 9312 } 9313 9314 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9315 return (ENXIO); 9316 } 9317 9318 part = SDPART(dev); 9319 nodelay = flag & (FNDELAY | FNONBLOCK); 9320 9321 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9322 "sdclose: close of part %d type %d\n", part, otyp); 9323 9324 /* 9325 * We use a semaphore here in order to serialize 9326 * open and close requests on the device. 9327 */ 9328 sema_p(&un->un_semoclose); 9329 9330 mutex_enter(SD_MUTEX(un)); 9331 9332 /* Don't proceed if power is being changed. */ 9333 while (un->un_state == SD_STATE_PM_CHANGING) { 9334 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9335 } 9336 9337 if (un->un_exclopen & (1 << part)) { 9338 un->un_exclopen &= ~(1 << part); 9339 } 9340 9341 /* Update the open partition map */ 9342 if (otyp == OTYP_LYR) { 9343 un->un_ocmap.lyropen[part] -= 1; 9344 } else { 9345 un->un_ocmap.regopen[otyp] &= ~(1 << part); 9346 } 9347 9348 cp = &un->un_ocmap.chkd[0]; 9349 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9350 if (*cp != NULL) { 9351 break; 9352 } 9353 cp++; 9354 } 9355 9356 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9357 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 9358 9359 /* 9360 * We avoid persistance upon the last close, and set 9361 * the throttle back to the maximum. 9362 */ 9363 un->un_throttle = un->un_saved_throttle; 9364 9365 if (un->un_state == SD_STATE_OFFLINE) { 9366 if (un->un_f_is_fibre == FALSE) { 9367 scsi_log(SD_DEVINFO(un), sd_label, 9368 CE_WARN, "offline\n"); 9369 } 9370 mutex_exit(SD_MUTEX(un)); 9371 cmlb_invalidate(un->un_cmlbhandle, 9372 (void *)SD_PATH_DIRECT); 9373 mutex_enter(SD_MUTEX(un)); 9374 9375 } else { 9376 /* 9377 * Flush any outstanding writes in NVRAM cache. 9378 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 9379 * cmd, it may not work for non-Pluto devices. 9380 * SYNCHRONIZE CACHE is not required for removables, 9381 * except DVD-RAM drives. 9382 * 9383 * Also note: because SYNCHRONIZE CACHE is currently 9384 * the only command issued here that requires the 9385 * drive be powered up, only do the power up before 9386 * sending the Sync Cache command. If additional 9387 * commands are added which require a powered up 9388 * drive, the following sequence may have to change. 9389 * 9390 * And finally, note that parallel SCSI on SPARC 9391 * only issues a Sync Cache to DVD-RAM, a newly 9392 * supported device. 9393 */ 9394 #if defined(__i386) || defined(__amd64) 9395 if (un->un_f_sync_cache_supported || 9396 un->un_f_dvdram_writable_device == TRUE) { 9397 #else 9398 if (un->un_f_dvdram_writable_device == TRUE) { 9399 #endif 9400 mutex_exit(SD_MUTEX(un)); 9401 if (sd_pm_entry(un) == DDI_SUCCESS) { 9402 rval = 9403 sd_send_scsi_SYNCHRONIZE_CACHE(un, 9404 NULL); 9405 /* ignore error if not supported */ 9406 if (rval == ENOTSUP) { 9407 rval = 0; 9408 } else if (rval != 0) { 9409 rval = EIO; 9410 } 9411 sd_pm_exit(un); 9412 } else { 9413 rval = EIO; 9414 } 9415 mutex_enter(SD_MUTEX(un)); 9416 } 9417 9418 /* 9419 * For devices which supports DOOR_LOCK, send an ALLOW 9420 * MEDIA REMOVAL command, but don't get upset if it 9421 * fails. We need to raise the power of the drive before 9422 * we can call sd_send_scsi_DOORLOCK() 9423 */ 9424 if (un->un_f_doorlock_supported) { 9425 mutex_exit(SD_MUTEX(un)); 9426 if (sd_pm_entry(un) == DDI_SUCCESS) { 9427 rval = sd_send_scsi_DOORLOCK(un, 9428 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 9429 9430 sd_pm_exit(un); 9431 if (ISCD(un) && (rval != 0) && 9432 (nodelay != 0)) { 9433 rval = ENXIO; 9434 } 9435 } else { 9436 rval = EIO; 9437 } 9438 mutex_enter(SD_MUTEX(un)); 9439 } 9440 9441 /* 9442 * If a device has removable media, invalidate all 9443 * parameters related to media, such as geometry, 9444 * blocksize, and blockcount. 9445 */ 9446 if (un->un_f_has_removable_media) { 9447 sr_ejected(un); 9448 } 9449 9450 /* 9451 * Destroy the cache (if it exists) which was 9452 * allocated for the write maps since this is 9453 * the last close for this media. 9454 */ 9455 if (un->un_wm_cache) { 9456 /* 9457 * Check if there are pending commands. 9458 * and if there are give a warning and 9459 * do not destroy the cache. 9460 */ 9461 if (un->un_ncmds_in_driver > 0) { 9462 scsi_log(SD_DEVINFO(un), 9463 sd_label, CE_WARN, 9464 "Unable to clean up memory " 9465 "because of pending I/O\n"); 9466 } else { 9467 kmem_cache_destroy( 9468 un->un_wm_cache); 9469 un->un_wm_cache = NULL; 9470 } 9471 } 9472 } 9473 } 9474 9475 mutex_exit(SD_MUTEX(un)); 9476 sema_v(&un->un_semoclose); 9477 9478 if (otyp == OTYP_LYR) { 9479 mutex_enter(&sd_detach_mutex); 9480 /* 9481 * The detach routine may run when the layer count 9482 * drops to zero. 9483 */ 9484 un->un_layer_count--; 9485 mutex_exit(&sd_detach_mutex); 9486 } 9487 9488 return (rval); 9489 } 9490 9491 9492 /* 9493 * Function: sd_ready_and_valid 9494 * 9495 * Description: Test if device is ready and has a valid geometry. 9496 * 9497 * Arguments: dev - device number 9498 * un - driver soft state (unit) structure 9499 * 9500 * Return Code: SD_READY_VALID ready and valid label 9501 * SD_NOT_READY_VALID not ready, no label 9502 * SD_RESERVED_BY_OTHERS reservation conflict 9503 * 9504 * Context: Never called at interrupt context. 9505 */ 9506 9507 static int 9508 sd_ready_and_valid(struct sd_lun *un) 9509 { 9510 struct sd_errstats *stp; 9511 uint64_t capacity; 9512 uint_t lbasize; 9513 int rval = SD_READY_VALID; 9514 char name_str[48]; 9515 int is_valid; 9516 9517 ASSERT(un != NULL); 9518 ASSERT(!mutex_owned(SD_MUTEX(un))); 9519 9520 mutex_enter(SD_MUTEX(un)); 9521 /* 9522 * If a device has removable media, we must check if media is 9523 * ready when checking if this device is ready and valid. 9524 */ 9525 if (un->un_f_has_removable_media) { 9526 mutex_exit(SD_MUTEX(un)); 9527 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 9528 rval = SD_NOT_READY_VALID; 9529 mutex_enter(SD_MUTEX(un)); 9530 goto done; 9531 } 9532 9533 is_valid = SD_IS_VALID_LABEL(un); 9534 mutex_enter(SD_MUTEX(un)); 9535 if (!is_valid || 9536 (un->un_f_blockcount_is_valid == FALSE) || 9537 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 9538 9539 /* capacity has to be read every open. */ 9540 mutex_exit(SD_MUTEX(un)); 9541 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 9542 &lbasize, SD_PATH_DIRECT) != 0) { 9543 cmlb_invalidate(un->un_cmlbhandle, 9544 (void *)SD_PATH_DIRECT); 9545 mutex_enter(SD_MUTEX(un)); 9546 rval = SD_NOT_READY_VALID; 9547 goto done; 9548 } else { 9549 mutex_enter(SD_MUTEX(un)); 9550 sd_update_block_info(un, lbasize, capacity); 9551 } 9552 } 9553 9554 /* 9555 * Check if the media in the device is writable or not. 9556 */ 9557 if (!is_valid && ISCD(un)) { 9558 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 9559 } 9560 9561 } else { 9562 /* 9563 * Do a test unit ready to clear any unit attention from non-cd 9564 * devices. 9565 */ 9566 mutex_exit(SD_MUTEX(un)); 9567 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9568 mutex_enter(SD_MUTEX(un)); 9569 } 9570 9571 9572 /* 9573 * If this is a non 512 block device, allocate space for 9574 * the wmap cache. This is being done here since every time 9575 * a media is changed this routine will be called and the 9576 * block size is a function of media rather than device. 9577 */ 9578 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 9579 if (!(un->un_wm_cache)) { 9580 (void) snprintf(name_str, sizeof (name_str), 9581 "%s%d_cache", 9582 ddi_driver_name(SD_DEVINFO(un)), 9583 ddi_get_instance(SD_DEVINFO(un))); 9584 un->un_wm_cache = kmem_cache_create( 9585 name_str, sizeof (struct sd_w_map), 9586 8, sd_wm_cache_constructor, 9587 sd_wm_cache_destructor, NULL, 9588 (void *)un, NULL, 0); 9589 if (!(un->un_wm_cache)) { 9590 rval = ENOMEM; 9591 goto done; 9592 } 9593 } 9594 } 9595 9596 if (un->un_state == SD_STATE_NORMAL) { 9597 /* 9598 * If the target is not yet ready here (defined by a TUR 9599 * failure), invalidate the geometry and print an 'offline' 9600 * message. This is a legacy message, as the state of the 9601 * target is not actually changed to SD_STATE_OFFLINE. 9602 * 9603 * If the TUR fails for EACCES (Reservation Conflict), 9604 * SD_RESERVED_BY_OTHERS will be returned to indicate 9605 * reservation conflict. If the TUR fails for other 9606 * reasons, SD_NOT_READY_VALID will be returned. 9607 */ 9608 int err; 9609 9610 mutex_exit(SD_MUTEX(un)); 9611 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 9612 mutex_enter(SD_MUTEX(un)); 9613 9614 if (err != 0) { 9615 mutex_exit(SD_MUTEX(un)); 9616 cmlb_invalidate(un->un_cmlbhandle, 9617 (void *)SD_PATH_DIRECT); 9618 mutex_enter(SD_MUTEX(un)); 9619 if (err == EACCES) { 9620 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9621 "reservation conflict\n"); 9622 rval = SD_RESERVED_BY_OTHERS; 9623 } else { 9624 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9625 "drive offline\n"); 9626 rval = SD_NOT_READY_VALID; 9627 } 9628 goto done; 9629 } 9630 } 9631 9632 if (un->un_f_format_in_progress == FALSE) { 9633 mutex_exit(SD_MUTEX(un)); 9634 if (cmlb_validate(un->un_cmlbhandle, 0, 9635 (void *)SD_PATH_DIRECT) != 0) { 9636 rval = SD_NOT_READY_VALID; 9637 mutex_enter(SD_MUTEX(un)); 9638 goto done; 9639 } 9640 if (un->un_f_pkstats_enabled) { 9641 sd_set_pstats(un); 9642 SD_TRACE(SD_LOG_IO_PARTITION, un, 9643 "sd_ready_and_valid: un:0x%p pstats created and " 9644 "set\n", un); 9645 } 9646 mutex_enter(SD_MUTEX(un)); 9647 } 9648 9649 /* 9650 * If this device supports DOOR_LOCK command, try and send 9651 * this command to PREVENT MEDIA REMOVAL, but don't get upset 9652 * if it fails. For a CD, however, it is an error 9653 */ 9654 if (un->un_f_doorlock_supported) { 9655 mutex_exit(SD_MUTEX(un)); 9656 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 9657 SD_PATH_DIRECT) != 0) && ISCD(un)) { 9658 rval = SD_NOT_READY_VALID; 9659 mutex_enter(SD_MUTEX(un)); 9660 goto done; 9661 } 9662 mutex_enter(SD_MUTEX(un)); 9663 } 9664 9665 /* The state has changed, inform the media watch routines */ 9666 un->un_mediastate = DKIO_INSERTED; 9667 cv_broadcast(&un->un_state_cv); 9668 rval = SD_READY_VALID; 9669 9670 done: 9671 9672 /* 9673 * Initialize the capacity kstat value, if no media previously 9674 * (capacity kstat is 0) and a media has been inserted 9675 * (un_blockcount > 0). 9676 */ 9677 if (un->un_errstats != NULL) { 9678 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9679 if ((stp->sd_capacity.value.ui64 == 0) && 9680 (un->un_f_blockcount_is_valid == TRUE)) { 9681 stp->sd_capacity.value.ui64 = 9682 (uint64_t)((uint64_t)un->un_blockcount * 9683 un->un_sys_blocksize); 9684 } 9685 } 9686 9687 mutex_exit(SD_MUTEX(un)); 9688 return (rval); 9689 } 9690 9691 9692 /* 9693 * Function: sdmin 9694 * 9695 * Description: Routine to limit the size of a data transfer. Used in 9696 * conjunction with physio(9F). 9697 * 9698 * Arguments: bp - pointer to the indicated buf(9S) struct. 9699 * 9700 * Context: Kernel thread context. 9701 */ 9702 9703 static void 9704 sdmin(struct buf *bp) 9705 { 9706 struct sd_lun *un; 9707 int instance; 9708 9709 instance = SDUNIT(bp->b_edev); 9710 9711 un = ddi_get_soft_state(sd_state, instance); 9712 ASSERT(un != NULL); 9713 9714 if (bp->b_bcount > un->un_max_xfer_size) { 9715 bp->b_bcount = un->un_max_xfer_size; 9716 } 9717 } 9718 9719 9720 /* 9721 * Function: sdread 9722 * 9723 * Description: Driver's read(9e) entry point function. 9724 * 9725 * Arguments: dev - device number 9726 * uio - structure pointer describing where data is to be stored 9727 * in user's space 9728 * cred_p - user credential pointer 9729 * 9730 * Return Code: ENXIO 9731 * EIO 9732 * EINVAL 9733 * value returned by physio 9734 * 9735 * Context: Kernel thread context. 9736 */ 9737 /* ARGSUSED */ 9738 static int 9739 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 9740 { 9741 struct sd_lun *un = NULL; 9742 int secmask; 9743 int err; 9744 9745 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9746 return (ENXIO); 9747 } 9748 9749 ASSERT(!mutex_owned(SD_MUTEX(un))); 9750 9751 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9752 mutex_enter(SD_MUTEX(un)); 9753 /* 9754 * Because the call to sd_ready_and_valid will issue I/O we 9755 * must wait here if either the device is suspended or 9756 * if it's power level is changing. 9757 */ 9758 while ((un->un_state == SD_STATE_SUSPENDED) || 9759 (un->un_state == SD_STATE_PM_CHANGING)) { 9760 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9761 } 9762 un->un_ncmds_in_driver++; 9763 mutex_exit(SD_MUTEX(un)); 9764 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9765 mutex_enter(SD_MUTEX(un)); 9766 un->un_ncmds_in_driver--; 9767 ASSERT(un->un_ncmds_in_driver >= 0); 9768 mutex_exit(SD_MUTEX(un)); 9769 return (EIO); 9770 } 9771 mutex_enter(SD_MUTEX(un)); 9772 un->un_ncmds_in_driver--; 9773 ASSERT(un->un_ncmds_in_driver >= 0); 9774 mutex_exit(SD_MUTEX(un)); 9775 } 9776 9777 /* 9778 * Read requests are restricted to multiples of the system block size. 9779 */ 9780 secmask = un->un_sys_blocksize - 1; 9781 9782 if (uio->uio_loffset & ((offset_t)(secmask))) { 9783 SD_ERROR(SD_LOG_READ_WRITE, un, 9784 "sdread: file offset not modulo %d\n", 9785 un->un_sys_blocksize); 9786 err = EINVAL; 9787 } else if (uio->uio_iov->iov_len & (secmask)) { 9788 SD_ERROR(SD_LOG_READ_WRITE, un, 9789 "sdread: transfer length not modulo %d\n", 9790 un->un_sys_blocksize); 9791 err = EINVAL; 9792 } else { 9793 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 9794 } 9795 return (err); 9796 } 9797 9798 9799 /* 9800 * Function: sdwrite 9801 * 9802 * Description: Driver's write(9e) entry point function. 9803 * 9804 * Arguments: dev - device number 9805 * uio - structure pointer describing where data is stored in 9806 * user's space 9807 * cred_p - user credential pointer 9808 * 9809 * Return Code: ENXIO 9810 * EIO 9811 * EINVAL 9812 * value returned by physio 9813 * 9814 * Context: Kernel thread context. 9815 */ 9816 /* ARGSUSED */ 9817 static int 9818 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 9819 { 9820 struct sd_lun *un = NULL; 9821 int secmask; 9822 int err; 9823 9824 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9825 return (ENXIO); 9826 } 9827 9828 ASSERT(!mutex_owned(SD_MUTEX(un))); 9829 9830 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9831 mutex_enter(SD_MUTEX(un)); 9832 /* 9833 * Because the call to sd_ready_and_valid will issue I/O we 9834 * must wait here if either the device is suspended or 9835 * if it's power level is changing. 9836 */ 9837 while ((un->un_state == SD_STATE_SUSPENDED) || 9838 (un->un_state == SD_STATE_PM_CHANGING)) { 9839 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9840 } 9841 un->un_ncmds_in_driver++; 9842 mutex_exit(SD_MUTEX(un)); 9843 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9844 mutex_enter(SD_MUTEX(un)); 9845 un->un_ncmds_in_driver--; 9846 ASSERT(un->un_ncmds_in_driver >= 0); 9847 mutex_exit(SD_MUTEX(un)); 9848 return (EIO); 9849 } 9850 mutex_enter(SD_MUTEX(un)); 9851 un->un_ncmds_in_driver--; 9852 ASSERT(un->un_ncmds_in_driver >= 0); 9853 mutex_exit(SD_MUTEX(un)); 9854 } 9855 9856 /* 9857 * Write requests are restricted to multiples of the system block size. 9858 */ 9859 secmask = un->un_sys_blocksize - 1; 9860 9861 if (uio->uio_loffset & ((offset_t)(secmask))) { 9862 SD_ERROR(SD_LOG_READ_WRITE, un, 9863 "sdwrite: file offset not modulo %d\n", 9864 un->un_sys_blocksize); 9865 err = EINVAL; 9866 } else if (uio->uio_iov->iov_len & (secmask)) { 9867 SD_ERROR(SD_LOG_READ_WRITE, un, 9868 "sdwrite: transfer length not modulo %d\n", 9869 un->un_sys_blocksize); 9870 err = EINVAL; 9871 } else { 9872 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 9873 } 9874 return (err); 9875 } 9876 9877 9878 /* 9879 * Function: sdaread 9880 * 9881 * Description: Driver's aread(9e) entry point function. 9882 * 9883 * Arguments: dev - device number 9884 * aio - structure pointer describing where data is to be stored 9885 * cred_p - user credential pointer 9886 * 9887 * Return Code: ENXIO 9888 * EIO 9889 * EINVAL 9890 * value returned by aphysio 9891 * 9892 * Context: Kernel thread context. 9893 */ 9894 /* ARGSUSED */ 9895 static int 9896 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9897 { 9898 struct sd_lun *un = NULL; 9899 struct uio *uio = aio->aio_uio; 9900 int secmask; 9901 int err; 9902 9903 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9904 return (ENXIO); 9905 } 9906 9907 ASSERT(!mutex_owned(SD_MUTEX(un))); 9908 9909 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9910 mutex_enter(SD_MUTEX(un)); 9911 /* 9912 * Because the call to sd_ready_and_valid will issue I/O we 9913 * must wait here if either the device is suspended or 9914 * if it's power level is changing. 9915 */ 9916 while ((un->un_state == SD_STATE_SUSPENDED) || 9917 (un->un_state == SD_STATE_PM_CHANGING)) { 9918 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9919 } 9920 un->un_ncmds_in_driver++; 9921 mutex_exit(SD_MUTEX(un)); 9922 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9923 mutex_enter(SD_MUTEX(un)); 9924 un->un_ncmds_in_driver--; 9925 ASSERT(un->un_ncmds_in_driver >= 0); 9926 mutex_exit(SD_MUTEX(un)); 9927 return (EIO); 9928 } 9929 mutex_enter(SD_MUTEX(un)); 9930 un->un_ncmds_in_driver--; 9931 ASSERT(un->un_ncmds_in_driver >= 0); 9932 mutex_exit(SD_MUTEX(un)); 9933 } 9934 9935 /* 9936 * Read requests are restricted to multiples of the system block size. 9937 */ 9938 secmask = un->un_sys_blocksize - 1; 9939 9940 if (uio->uio_loffset & ((offset_t)(secmask))) { 9941 SD_ERROR(SD_LOG_READ_WRITE, un, 9942 "sdaread: file offset not modulo %d\n", 9943 un->un_sys_blocksize); 9944 err = EINVAL; 9945 } else if (uio->uio_iov->iov_len & (secmask)) { 9946 SD_ERROR(SD_LOG_READ_WRITE, un, 9947 "sdaread: transfer length not modulo %d\n", 9948 un->un_sys_blocksize); 9949 err = EINVAL; 9950 } else { 9951 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 9952 } 9953 return (err); 9954 } 9955 9956 9957 /* 9958 * Function: sdawrite 9959 * 9960 * Description: Driver's awrite(9e) entry point function. 9961 * 9962 * Arguments: dev - device number 9963 * aio - structure pointer describing where data is stored 9964 * cred_p - user credential pointer 9965 * 9966 * Return Code: ENXIO 9967 * EIO 9968 * EINVAL 9969 * value returned by aphysio 9970 * 9971 * Context: Kernel thread context. 9972 */ 9973 /* ARGSUSED */ 9974 static int 9975 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9976 { 9977 struct sd_lun *un = NULL; 9978 struct uio *uio = aio->aio_uio; 9979 int secmask; 9980 int err; 9981 9982 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9983 return (ENXIO); 9984 } 9985 9986 ASSERT(!mutex_owned(SD_MUTEX(un))); 9987 9988 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9989 mutex_enter(SD_MUTEX(un)); 9990 /* 9991 * Because the call to sd_ready_and_valid will issue I/O we 9992 * must wait here if either the device is suspended or 9993 * if it's power level is changing. 9994 */ 9995 while ((un->un_state == SD_STATE_SUSPENDED) || 9996 (un->un_state == SD_STATE_PM_CHANGING)) { 9997 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9998 } 9999 un->un_ncmds_in_driver++; 10000 mutex_exit(SD_MUTEX(un)); 10001 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 10002 mutex_enter(SD_MUTEX(un)); 10003 un->un_ncmds_in_driver--; 10004 ASSERT(un->un_ncmds_in_driver >= 0); 10005 mutex_exit(SD_MUTEX(un)); 10006 return (EIO); 10007 } 10008 mutex_enter(SD_MUTEX(un)); 10009 un->un_ncmds_in_driver--; 10010 ASSERT(un->un_ncmds_in_driver >= 0); 10011 mutex_exit(SD_MUTEX(un)); 10012 } 10013 10014 /* 10015 * Write requests are restricted to multiples of the system block size. 10016 */ 10017 secmask = un->un_sys_blocksize - 1; 10018 10019 if (uio->uio_loffset & ((offset_t)(secmask))) { 10020 SD_ERROR(SD_LOG_READ_WRITE, un, 10021 "sdawrite: file offset not modulo %d\n", 10022 un->un_sys_blocksize); 10023 err = EINVAL; 10024 } else if (uio->uio_iov->iov_len & (secmask)) { 10025 SD_ERROR(SD_LOG_READ_WRITE, un, 10026 "sdawrite: transfer length not modulo %d\n", 10027 un->un_sys_blocksize); 10028 err = EINVAL; 10029 } else { 10030 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 10031 } 10032 return (err); 10033 } 10034 10035 10036 10037 10038 10039 /* 10040 * Driver IO processing follows the following sequence: 10041 * 10042 * sdioctl(9E) sdstrategy(9E) biodone(9F) 10043 * | | ^ 10044 * v v | 10045 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 10046 * | | | | 10047 * v | | | 10048 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 10049 * | | ^ ^ 10050 * v v | | 10051 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 10052 * | | | | 10053 * +---+ | +------------+ +-------+ 10054 * | | | | 10055 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10056 * | v | | 10057 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 10058 * | | ^ | 10059 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10060 * | v | | 10061 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 10062 * | | ^ | 10063 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10064 * | v | | 10065 * | sd_checksum_iostart() sd_checksum_iodone() | 10066 * | | ^ | 10067 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 10068 * | v | | 10069 * | sd_pm_iostart() sd_pm_iodone() | 10070 * | | ^ | 10071 * | | | | 10072 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 10073 * | ^ 10074 * v | 10075 * sd_core_iostart() | 10076 * | | 10077 * | +------>(*destroypkt)() 10078 * +-> sd_start_cmds() <-+ | | 10079 * | | | v 10080 * | | | scsi_destroy_pkt(9F) 10081 * | | | 10082 * +->(*initpkt)() +- sdintr() 10083 * | | | | 10084 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 10085 * | +-> scsi_setup_cdb(9F) | 10086 * | | 10087 * +--> scsi_transport(9F) | 10088 * | | 10089 * +----> SCSA ---->+ 10090 * 10091 * 10092 * This code is based upon the following presumptions: 10093 * 10094 * - iostart and iodone functions operate on buf(9S) structures. These 10095 * functions perform the necessary operations on the buf(9S) and pass 10096 * them along to the next function in the chain by using the macros 10097 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 10098 * (for iodone side functions). 10099 * 10100 * - The iostart side functions may sleep. The iodone side functions 10101 * are called under interrupt context and may NOT sleep. Therefore 10102 * iodone side functions also may not call iostart side functions. 10103 * (NOTE: iostart side functions should NOT sleep for memory, as 10104 * this could result in deadlock.) 10105 * 10106 * - An iostart side function may call its corresponding iodone side 10107 * function directly (if necessary). 10108 * 10109 * - In the event of an error, an iostart side function can return a buf(9S) 10110 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 10111 * b_error in the usual way of course). 10112 * 10113 * - The taskq mechanism may be used by the iodone side functions to dispatch 10114 * requests to the iostart side functions. The iostart side functions in 10115 * this case would be called under the context of a taskq thread, so it's 10116 * OK for them to block/sleep/spin in this case. 10117 * 10118 * - iostart side functions may allocate "shadow" buf(9S) structs and 10119 * pass them along to the next function in the chain. The corresponding 10120 * iodone side functions must coalesce the "shadow" bufs and return 10121 * the "original" buf to the next higher layer. 10122 * 10123 * - The b_private field of the buf(9S) struct holds a pointer to 10124 * an sd_xbuf struct, which contains information needed to 10125 * construct the scsi_pkt for the command. 10126 * 10127 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 10128 * layer must acquire & release the SD_MUTEX(un) as needed. 10129 */ 10130 10131 10132 /* 10133 * Create taskq for all targets in the system. This is created at 10134 * _init(9E) and destroyed at _fini(9E). 10135 * 10136 * Note: here we set the minalloc to a reasonably high number to ensure that 10137 * we will have an adequate supply of task entries available at interrupt time. 10138 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 10139 * sd_create_taskq(). Since we do not want to sleep for allocations at 10140 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 10141 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 10142 * requests any one instant in time. 10143 */ 10144 #define SD_TASKQ_NUMTHREADS 8 10145 #define SD_TASKQ_MINALLOC 256 10146 #define SD_TASKQ_MAXALLOC 256 10147 10148 static taskq_t *sd_tq = NULL; 10149 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 10150 10151 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 10152 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 10153 10154 /* 10155 * The following task queue is being created for the write part of 10156 * read-modify-write of non-512 block size devices. 10157 * Limit the number of threads to 1 for now. This number has been chosen 10158 * considering the fact that it applies only to dvd ram drives/MO drives 10159 * currently. Performance for which is not main criteria at this stage. 10160 * Note: It needs to be explored if we can use a single taskq in future 10161 */ 10162 #define SD_WMR_TASKQ_NUMTHREADS 1 10163 static taskq_t *sd_wmr_tq = NULL; 10164 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 10165 10166 /* 10167 * Function: sd_taskq_create 10168 * 10169 * Description: Create taskq thread(s) and preallocate task entries 10170 * 10171 * Return Code: Returns a pointer to the allocated taskq_t. 10172 * 10173 * Context: Can sleep. Requires blockable context. 10174 * 10175 * Notes: - The taskq() facility currently is NOT part of the DDI. 10176 * (definitely NOT recommeded for 3rd-party drivers!) :-) 10177 * - taskq_create() will block for memory, also it will panic 10178 * if it cannot create the requested number of threads. 10179 * - Currently taskq_create() creates threads that cannot be 10180 * swapped. 10181 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 10182 * supply of taskq entries at interrupt time (ie, so that we 10183 * do not have to sleep for memory) 10184 */ 10185 10186 static void 10187 sd_taskq_create(void) 10188 { 10189 char taskq_name[TASKQ_NAMELEN]; 10190 10191 ASSERT(sd_tq == NULL); 10192 ASSERT(sd_wmr_tq == NULL); 10193 10194 (void) snprintf(taskq_name, sizeof (taskq_name), 10195 "%s_drv_taskq", sd_label); 10196 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 10197 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10198 TASKQ_PREPOPULATE)); 10199 10200 (void) snprintf(taskq_name, sizeof (taskq_name), 10201 "%s_rmw_taskq", sd_label); 10202 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 10203 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10204 TASKQ_PREPOPULATE)); 10205 } 10206 10207 10208 /* 10209 * Function: sd_taskq_delete 10210 * 10211 * Description: Complementary cleanup routine for sd_taskq_create(). 10212 * 10213 * Context: Kernel thread context. 10214 */ 10215 10216 static void 10217 sd_taskq_delete(void) 10218 { 10219 ASSERT(sd_tq != NULL); 10220 ASSERT(sd_wmr_tq != NULL); 10221 taskq_destroy(sd_tq); 10222 taskq_destroy(sd_wmr_tq); 10223 sd_tq = NULL; 10224 sd_wmr_tq = NULL; 10225 } 10226 10227 10228 /* 10229 * Function: sdstrategy 10230 * 10231 * Description: Driver's strategy (9E) entry point function. 10232 * 10233 * Arguments: bp - pointer to buf(9S) 10234 * 10235 * Return Code: Always returns zero 10236 * 10237 * Context: Kernel thread context. 10238 */ 10239 10240 static int 10241 sdstrategy(struct buf *bp) 10242 { 10243 struct sd_lun *un; 10244 10245 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10246 if (un == NULL) { 10247 bioerror(bp, EIO); 10248 bp->b_resid = bp->b_bcount; 10249 biodone(bp); 10250 return (0); 10251 } 10252 /* As was done in the past, fail new cmds. if state is dumping. */ 10253 if (un->un_state == SD_STATE_DUMPING) { 10254 bioerror(bp, ENXIO); 10255 bp->b_resid = bp->b_bcount; 10256 biodone(bp); 10257 return (0); 10258 } 10259 10260 ASSERT(!mutex_owned(SD_MUTEX(un))); 10261 10262 /* 10263 * Commands may sneak in while we released the mutex in 10264 * DDI_SUSPEND, we should block new commands. However, old 10265 * commands that are still in the driver at this point should 10266 * still be allowed to drain. 10267 */ 10268 mutex_enter(SD_MUTEX(un)); 10269 /* 10270 * Must wait here if either the device is suspended or 10271 * if it's power level is changing. 10272 */ 10273 while ((un->un_state == SD_STATE_SUSPENDED) || 10274 (un->un_state == SD_STATE_PM_CHANGING)) { 10275 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10276 } 10277 10278 un->un_ncmds_in_driver++; 10279 10280 /* 10281 * atapi: Since we are running the CD for now in PIO mode we need to 10282 * call bp_mapin here to avoid bp_mapin called interrupt context under 10283 * the HBA's init_pkt routine. 10284 */ 10285 if (un->un_f_cfg_is_atapi == TRUE) { 10286 mutex_exit(SD_MUTEX(un)); 10287 bp_mapin(bp); 10288 mutex_enter(SD_MUTEX(un)); 10289 } 10290 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 10291 un->un_ncmds_in_driver); 10292 10293 mutex_exit(SD_MUTEX(un)); 10294 10295 /* 10296 * This will (eventually) allocate the sd_xbuf area and 10297 * call sd_xbuf_strategy(). We just want to return the 10298 * result of ddi_xbuf_qstrategy so that we have an opt- 10299 * imized tail call which saves us a stack frame. 10300 */ 10301 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 10302 } 10303 10304 10305 /* 10306 * Function: sd_xbuf_strategy 10307 * 10308 * Description: Function for initiating IO operations via the 10309 * ddi_xbuf_qstrategy() mechanism. 10310 * 10311 * Context: Kernel thread context. 10312 */ 10313 10314 static void 10315 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 10316 { 10317 struct sd_lun *un = arg; 10318 10319 ASSERT(bp != NULL); 10320 ASSERT(xp != NULL); 10321 ASSERT(un != NULL); 10322 ASSERT(!mutex_owned(SD_MUTEX(un))); 10323 10324 /* 10325 * Initialize the fields in the xbuf and save a pointer to the 10326 * xbuf in bp->b_private. 10327 */ 10328 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 10329 10330 /* Send the buf down the iostart chain */ 10331 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 10332 } 10333 10334 10335 /* 10336 * Function: sd_xbuf_init 10337 * 10338 * Description: Prepare the given sd_xbuf struct for use. 10339 * 10340 * Arguments: un - ptr to softstate 10341 * bp - ptr to associated buf(9S) 10342 * xp - ptr to associated sd_xbuf 10343 * chain_type - IO chain type to use: 10344 * SD_CHAIN_NULL 10345 * SD_CHAIN_BUFIO 10346 * SD_CHAIN_USCSI 10347 * SD_CHAIN_DIRECT 10348 * SD_CHAIN_DIRECT_PRIORITY 10349 * pktinfop - ptr to private data struct for scsi_pkt(9S) 10350 * initialization; may be NULL if none. 10351 * 10352 * Context: Kernel thread context 10353 */ 10354 10355 static void 10356 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 10357 uchar_t chain_type, void *pktinfop) 10358 { 10359 int index; 10360 10361 ASSERT(un != NULL); 10362 ASSERT(bp != NULL); 10363 ASSERT(xp != NULL); 10364 10365 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 10366 bp, chain_type); 10367 10368 xp->xb_un = un; 10369 xp->xb_pktp = NULL; 10370 xp->xb_pktinfo = pktinfop; 10371 xp->xb_private = bp->b_private; 10372 xp->xb_blkno = (daddr_t)bp->b_blkno; 10373 10374 /* 10375 * Set up the iostart and iodone chain indexes in the xbuf, based 10376 * upon the specified chain type to use. 10377 */ 10378 switch (chain_type) { 10379 case SD_CHAIN_NULL: 10380 /* 10381 * Fall thru to just use the values for the buf type, even 10382 * tho for the NULL chain these values will never be used. 10383 */ 10384 /* FALLTHRU */ 10385 case SD_CHAIN_BUFIO: 10386 index = un->un_buf_chain_type; 10387 break; 10388 case SD_CHAIN_USCSI: 10389 index = un->un_uscsi_chain_type; 10390 break; 10391 case SD_CHAIN_DIRECT: 10392 index = un->un_direct_chain_type; 10393 break; 10394 case SD_CHAIN_DIRECT_PRIORITY: 10395 index = un->un_priority_chain_type; 10396 break; 10397 default: 10398 /* We're really broken if we ever get here... */ 10399 panic("sd_xbuf_init: illegal chain type!"); 10400 /*NOTREACHED*/ 10401 } 10402 10403 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 10404 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 10405 10406 /* 10407 * It might be a bit easier to simply bzero the entire xbuf above, 10408 * but it turns out that since we init a fair number of members anyway, 10409 * we save a fair number cycles by doing explicit assignment of zero. 10410 */ 10411 xp->xb_pkt_flags = 0; 10412 xp->xb_dma_resid = 0; 10413 xp->xb_retry_count = 0; 10414 xp->xb_victim_retry_count = 0; 10415 xp->xb_ua_retry_count = 0; 10416 xp->xb_nr_retry_count = 0; 10417 xp->xb_sense_bp = NULL; 10418 xp->xb_sense_status = 0; 10419 xp->xb_sense_state = 0; 10420 xp->xb_sense_resid = 0; 10421 10422 bp->b_private = xp; 10423 bp->b_flags &= ~(B_DONE | B_ERROR); 10424 bp->b_resid = 0; 10425 bp->av_forw = NULL; 10426 bp->av_back = NULL; 10427 bioerror(bp, 0); 10428 10429 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 10430 } 10431 10432 10433 /* 10434 * Function: sd_uscsi_strategy 10435 * 10436 * Description: Wrapper for calling into the USCSI chain via physio(9F) 10437 * 10438 * Arguments: bp - buf struct ptr 10439 * 10440 * Return Code: Always returns 0 10441 * 10442 * Context: Kernel thread context 10443 */ 10444 10445 static int 10446 sd_uscsi_strategy(struct buf *bp) 10447 { 10448 struct sd_lun *un; 10449 struct sd_uscsi_info *uip; 10450 struct sd_xbuf *xp; 10451 uchar_t chain_type; 10452 10453 ASSERT(bp != NULL); 10454 10455 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10456 if (un == NULL) { 10457 bioerror(bp, EIO); 10458 bp->b_resid = bp->b_bcount; 10459 biodone(bp); 10460 return (0); 10461 } 10462 10463 ASSERT(!mutex_owned(SD_MUTEX(un))); 10464 10465 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 10466 10467 mutex_enter(SD_MUTEX(un)); 10468 /* 10469 * atapi: Since we are running the CD for now in PIO mode we need to 10470 * call bp_mapin here to avoid bp_mapin called interrupt context under 10471 * the HBA's init_pkt routine. 10472 */ 10473 if (un->un_f_cfg_is_atapi == TRUE) { 10474 mutex_exit(SD_MUTEX(un)); 10475 bp_mapin(bp); 10476 mutex_enter(SD_MUTEX(un)); 10477 } 10478 un->un_ncmds_in_driver++; 10479 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 10480 un->un_ncmds_in_driver); 10481 mutex_exit(SD_MUTEX(un)); 10482 10483 /* 10484 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 10485 */ 10486 ASSERT(bp->b_private != NULL); 10487 uip = (struct sd_uscsi_info *)bp->b_private; 10488 10489 switch (uip->ui_flags) { 10490 case SD_PATH_DIRECT: 10491 chain_type = SD_CHAIN_DIRECT; 10492 break; 10493 case SD_PATH_DIRECT_PRIORITY: 10494 chain_type = SD_CHAIN_DIRECT_PRIORITY; 10495 break; 10496 default: 10497 chain_type = SD_CHAIN_USCSI; 10498 break; 10499 } 10500 10501 /* 10502 * We may allocate extra buf for external USCSI commands. If the 10503 * application asks for bigger than 20-byte sense data via USCSI, 10504 * SCSA layer will allocate 252 bytes sense buf for that command. 10505 */ 10506 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 10507 SENSE_LENGTH) { 10508 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 10509 MAX_SENSE_LENGTH, KM_SLEEP); 10510 } else { 10511 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 10512 } 10513 10514 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 10515 10516 /* Use the index obtained within xbuf_init */ 10517 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 10518 10519 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 10520 10521 return (0); 10522 } 10523 10524 /* 10525 * Function: sd_send_scsi_cmd 10526 * 10527 * Description: Runs a USCSI command for user (when called thru sdioctl), 10528 * or for the driver 10529 * 10530 * Arguments: dev - the dev_t for the device 10531 * incmd - ptr to a valid uscsi_cmd struct 10532 * flag - bit flag, indicating open settings, 32/64 bit type 10533 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 10534 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 10535 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 10536 * to use the USCSI "direct" chain and bypass the normal 10537 * command waitq. 10538 * 10539 * Return Code: 0 - successful completion of the given command 10540 * EIO - scsi_uscsi_handle_command() failed 10541 * ENXIO - soft state not found for specified dev 10542 * EINVAL 10543 * EFAULT - copyin/copyout error 10544 * return code of scsi_uscsi_handle_command(): 10545 * EIO 10546 * ENXIO 10547 * EACCES 10548 * 10549 * Context: Waits for command to complete. Can sleep. 10550 */ 10551 10552 static int 10553 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 10554 enum uio_seg dataspace, int path_flag) 10555 { 10556 struct sd_uscsi_info *uip; 10557 struct uscsi_cmd *uscmd; 10558 struct sd_lun *un; 10559 int format = 0; 10560 int rval; 10561 10562 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 10563 if (un == NULL) { 10564 return (ENXIO); 10565 } 10566 10567 ASSERT(!mutex_owned(SD_MUTEX(un))); 10568 10569 #ifdef SDDEBUG 10570 switch (dataspace) { 10571 case UIO_USERSPACE: 10572 SD_TRACE(SD_LOG_IO, un, 10573 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 10574 break; 10575 case UIO_SYSSPACE: 10576 SD_TRACE(SD_LOG_IO, un, 10577 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 10578 break; 10579 default: 10580 SD_TRACE(SD_LOG_IO, un, 10581 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 10582 break; 10583 } 10584 #endif 10585 10586 rval = scsi_uscsi_alloc_and_copyin((intptr_t)incmd, flag, 10587 SD_ADDRESS(un), &uscmd); 10588 if (rval != 0) { 10589 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 10590 "scsi_uscsi_alloc_and_copyin failed\n", un); 10591 return (rval); 10592 } 10593 10594 if ((uscmd->uscsi_cdb != NULL) && 10595 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 10596 mutex_enter(SD_MUTEX(un)); 10597 un->un_f_format_in_progress = TRUE; 10598 mutex_exit(SD_MUTEX(un)); 10599 format = 1; 10600 } 10601 10602 /* 10603 * Allocate an sd_uscsi_info struct and fill it with the info 10604 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 10605 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 10606 * since we allocate the buf here in this function, we do not 10607 * need to preserve the prior contents of b_private. 10608 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 10609 */ 10610 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 10611 uip->ui_flags = path_flag; 10612 uip->ui_cmdp = uscmd; 10613 10614 /* 10615 * Commands sent with priority are intended for error recovery 10616 * situations, and do not have retries performed. 10617 */ 10618 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 10619 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 10620 } 10621 uscmd->uscsi_flags &= ~USCSI_NOINTR; 10622 10623 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 10624 sd_uscsi_strategy, NULL, uip); 10625 10626 #ifdef SDDEBUG 10627 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10628 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 10629 uscmd->uscsi_status, uscmd->uscsi_resid); 10630 if (uscmd->uscsi_bufaddr != NULL) { 10631 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10632 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 10633 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 10634 if (dataspace == UIO_SYSSPACE) { 10635 SD_DUMP_MEMORY(un, SD_LOG_IO, 10636 "data", (uchar_t *)uscmd->uscsi_bufaddr, 10637 uscmd->uscsi_buflen, SD_LOG_HEX); 10638 } 10639 } 10640 #endif 10641 10642 if (format == 1) { 10643 mutex_enter(SD_MUTEX(un)); 10644 un->un_f_format_in_progress = FALSE; 10645 mutex_exit(SD_MUTEX(un)); 10646 } 10647 10648 (void) scsi_uscsi_copyout_and_free((intptr_t)incmd, uscmd); 10649 kmem_free(uip, sizeof (struct sd_uscsi_info)); 10650 10651 return (rval); 10652 } 10653 10654 10655 /* 10656 * Function: sd_buf_iodone 10657 * 10658 * Description: Frees the sd_xbuf & returns the buf to its originator. 10659 * 10660 * Context: May be called from interrupt context. 10661 */ 10662 /* ARGSUSED */ 10663 static void 10664 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 10665 { 10666 struct sd_xbuf *xp; 10667 10668 ASSERT(un != NULL); 10669 ASSERT(bp != NULL); 10670 ASSERT(!mutex_owned(SD_MUTEX(un))); 10671 10672 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 10673 10674 xp = SD_GET_XBUF(bp); 10675 ASSERT(xp != NULL); 10676 10677 mutex_enter(SD_MUTEX(un)); 10678 10679 /* 10680 * Grab time when the cmd completed. 10681 * This is used for determining if the system has been 10682 * idle long enough to make it idle to the PM framework. 10683 * This is for lowering the overhead, and therefore improving 10684 * performance per I/O operation. 10685 */ 10686 un->un_pm_idle_time = ddi_get_time(); 10687 10688 un->un_ncmds_in_driver--; 10689 ASSERT(un->un_ncmds_in_driver >= 0); 10690 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 10691 un->un_ncmds_in_driver); 10692 10693 mutex_exit(SD_MUTEX(un)); 10694 10695 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 10696 biodone(bp); /* bp is gone after this */ 10697 10698 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 10699 } 10700 10701 10702 /* 10703 * Function: sd_uscsi_iodone 10704 * 10705 * Description: Frees the sd_xbuf & returns the buf to its originator. 10706 * 10707 * Context: May be called from interrupt context. 10708 */ 10709 /* ARGSUSED */ 10710 static void 10711 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 10712 { 10713 struct sd_xbuf *xp; 10714 10715 ASSERT(un != NULL); 10716 ASSERT(bp != NULL); 10717 10718 xp = SD_GET_XBUF(bp); 10719 ASSERT(xp != NULL); 10720 ASSERT(!mutex_owned(SD_MUTEX(un))); 10721 10722 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 10723 10724 bp->b_private = xp->xb_private; 10725 10726 mutex_enter(SD_MUTEX(un)); 10727 10728 /* 10729 * Grab time when the cmd completed. 10730 * This is used for determining if the system has been 10731 * idle long enough to make it idle to the PM framework. 10732 * This is for lowering the overhead, and therefore improving 10733 * performance per I/O operation. 10734 */ 10735 un->un_pm_idle_time = ddi_get_time(); 10736 10737 un->un_ncmds_in_driver--; 10738 ASSERT(un->un_ncmds_in_driver >= 0); 10739 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 10740 un->un_ncmds_in_driver); 10741 10742 mutex_exit(SD_MUTEX(un)); 10743 10744 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 10745 SENSE_LENGTH) { 10746 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 10747 MAX_SENSE_LENGTH); 10748 } else { 10749 kmem_free(xp, sizeof (struct sd_xbuf)); 10750 } 10751 10752 biodone(bp); 10753 10754 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 10755 } 10756 10757 10758 /* 10759 * Function: sd_mapblockaddr_iostart 10760 * 10761 * Description: Verify request lies within the partition limits for 10762 * the indicated minor device. Issue "overrun" buf if 10763 * request would exceed partition range. Converts 10764 * partition-relative block address to absolute. 10765 * 10766 * Context: Can sleep 10767 * 10768 * Issues: This follows what the old code did, in terms of accessing 10769 * some of the partition info in the unit struct without holding 10770 * the mutext. This is a general issue, if the partition info 10771 * can be altered while IO is in progress... as soon as we send 10772 * a buf, its partitioning can be invalid before it gets to the 10773 * device. Probably the right fix is to move partitioning out 10774 * of the driver entirely. 10775 */ 10776 10777 static void 10778 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 10779 { 10780 diskaddr_t nblocks; /* #blocks in the given partition */ 10781 daddr_t blocknum; /* Block number specified by the buf */ 10782 size_t requested_nblocks; 10783 size_t available_nblocks; 10784 int partition; 10785 diskaddr_t partition_offset; 10786 struct sd_xbuf *xp; 10787 10788 10789 ASSERT(un != NULL); 10790 ASSERT(bp != NULL); 10791 ASSERT(!mutex_owned(SD_MUTEX(un))); 10792 10793 SD_TRACE(SD_LOG_IO_PARTITION, un, 10794 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 10795 10796 xp = SD_GET_XBUF(bp); 10797 ASSERT(xp != NULL); 10798 10799 /* 10800 * If the geometry is not indicated as valid, attempt to access 10801 * the unit & verify the geometry/label. This can be the case for 10802 * removable-media devices, of if the device was opened in 10803 * NDELAY/NONBLOCK mode. 10804 */ 10805 if (!SD_IS_VALID_LABEL(un) && 10806 (sd_ready_and_valid(un) != SD_READY_VALID)) { 10807 /* 10808 * For removable devices it is possible to start an I/O 10809 * without a media by opening the device in nodelay mode. 10810 * Also for writable CDs there can be many scenarios where 10811 * there is no geometry yet but volume manager is trying to 10812 * issue a read() just because it can see TOC on the CD. So 10813 * do not print a message for removables. 10814 */ 10815 if (!un->un_f_has_removable_media) { 10816 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10817 "i/o to invalid geometry\n"); 10818 } 10819 bioerror(bp, EIO); 10820 bp->b_resid = bp->b_bcount; 10821 SD_BEGIN_IODONE(index, un, bp); 10822 return; 10823 } 10824 10825 partition = SDPART(bp->b_edev); 10826 10827 nblocks = 0; 10828 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 10829 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 10830 10831 /* 10832 * blocknum is the starting block number of the request. At this 10833 * point it is still relative to the start of the minor device. 10834 */ 10835 blocknum = xp->xb_blkno; 10836 10837 /* 10838 * Legacy: If the starting block number is one past the last block 10839 * in the partition, do not set B_ERROR in the buf. 10840 */ 10841 if (blocknum == nblocks) { 10842 goto error_exit; 10843 } 10844 10845 /* 10846 * Confirm that the first block of the request lies within the 10847 * partition limits. Also the requested number of bytes must be 10848 * a multiple of the system block size. 10849 */ 10850 if ((blocknum < 0) || (blocknum >= nblocks) || 10851 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 10852 bp->b_flags |= B_ERROR; 10853 goto error_exit; 10854 } 10855 10856 /* 10857 * If the requsted # blocks exceeds the available # blocks, that 10858 * is an overrun of the partition. 10859 */ 10860 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 10861 available_nblocks = (size_t)(nblocks - blocknum); 10862 ASSERT(nblocks >= blocknum); 10863 10864 if (requested_nblocks > available_nblocks) { 10865 /* 10866 * Allocate an "overrun" buf to allow the request to proceed 10867 * for the amount of space available in the partition. The 10868 * amount not transferred will be added into the b_resid 10869 * when the operation is complete. The overrun buf 10870 * replaces the original buf here, and the original buf 10871 * is saved inside the overrun buf, for later use. 10872 */ 10873 size_t resid = SD_SYSBLOCKS2BYTES(un, 10874 (offset_t)(requested_nblocks - available_nblocks)); 10875 size_t count = bp->b_bcount - resid; 10876 /* 10877 * Note: count is an unsigned entity thus it'll NEVER 10878 * be less than 0 so ASSERT the original values are 10879 * correct. 10880 */ 10881 ASSERT(bp->b_bcount >= resid); 10882 10883 bp = sd_bioclone_alloc(bp, count, blocknum, 10884 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 10885 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 10886 ASSERT(xp != NULL); 10887 } 10888 10889 /* At this point there should be no residual for this buf. */ 10890 ASSERT(bp->b_resid == 0); 10891 10892 /* Convert the block number to an absolute address. */ 10893 xp->xb_blkno += partition_offset; 10894 10895 SD_NEXT_IOSTART(index, un, bp); 10896 10897 SD_TRACE(SD_LOG_IO_PARTITION, un, 10898 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 10899 10900 return; 10901 10902 error_exit: 10903 bp->b_resid = bp->b_bcount; 10904 SD_BEGIN_IODONE(index, un, bp); 10905 SD_TRACE(SD_LOG_IO_PARTITION, un, 10906 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 10907 } 10908 10909 10910 /* 10911 * Function: sd_mapblockaddr_iodone 10912 * 10913 * Description: Completion-side processing for partition management. 10914 * 10915 * Context: May be called under interrupt context 10916 */ 10917 10918 static void 10919 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 10920 { 10921 /* int partition; */ /* Not used, see below. */ 10922 ASSERT(un != NULL); 10923 ASSERT(bp != NULL); 10924 ASSERT(!mutex_owned(SD_MUTEX(un))); 10925 10926 SD_TRACE(SD_LOG_IO_PARTITION, un, 10927 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 10928 10929 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 10930 /* 10931 * We have an "overrun" buf to deal with... 10932 */ 10933 struct sd_xbuf *xp; 10934 struct buf *obp; /* ptr to the original buf */ 10935 10936 xp = SD_GET_XBUF(bp); 10937 ASSERT(xp != NULL); 10938 10939 /* Retrieve the pointer to the original buf */ 10940 obp = (struct buf *)xp->xb_private; 10941 ASSERT(obp != NULL); 10942 10943 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 10944 bioerror(obp, bp->b_error); 10945 10946 sd_bioclone_free(bp); 10947 10948 /* 10949 * Get back the original buf. 10950 * Note that since the restoration of xb_blkno below 10951 * was removed, the sd_xbuf is not needed. 10952 */ 10953 bp = obp; 10954 /* 10955 * xp = SD_GET_XBUF(bp); 10956 * ASSERT(xp != NULL); 10957 */ 10958 } 10959 10960 /* 10961 * Convert sd->xb_blkno back to a minor-device relative value. 10962 * Note: this has been commented out, as it is not needed in the 10963 * current implementation of the driver (ie, since this function 10964 * is at the top of the layering chains, so the info will be 10965 * discarded) and it is in the "hot" IO path. 10966 * 10967 * partition = getminor(bp->b_edev) & SDPART_MASK; 10968 * xp->xb_blkno -= un->un_offset[partition]; 10969 */ 10970 10971 SD_NEXT_IODONE(index, un, bp); 10972 10973 SD_TRACE(SD_LOG_IO_PARTITION, un, 10974 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 10975 } 10976 10977 10978 /* 10979 * Function: sd_mapblocksize_iostart 10980 * 10981 * Description: Convert between system block size (un->un_sys_blocksize) 10982 * and target block size (un->un_tgt_blocksize). 10983 * 10984 * Context: Can sleep to allocate resources. 10985 * 10986 * Assumptions: A higher layer has already performed any partition validation, 10987 * and converted the xp->xb_blkno to an absolute value relative 10988 * to the start of the device. 10989 * 10990 * It is also assumed that the higher layer has implemented 10991 * an "overrun" mechanism for the case where the request would 10992 * read/write beyond the end of a partition. In this case we 10993 * assume (and ASSERT) that bp->b_resid == 0. 10994 * 10995 * Note: The implementation for this routine assumes the target 10996 * block size remains constant between allocation and transport. 10997 */ 10998 10999 static void 11000 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 11001 { 11002 struct sd_mapblocksize_info *bsp; 11003 struct sd_xbuf *xp; 11004 offset_t first_byte; 11005 daddr_t start_block, end_block; 11006 daddr_t request_bytes; 11007 ushort_t is_aligned = FALSE; 11008 11009 ASSERT(un != NULL); 11010 ASSERT(bp != NULL); 11011 ASSERT(!mutex_owned(SD_MUTEX(un))); 11012 ASSERT(bp->b_resid == 0); 11013 11014 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11015 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 11016 11017 /* 11018 * For a non-writable CD, a write request is an error 11019 */ 11020 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 11021 (un->un_f_mmc_writable_media == FALSE)) { 11022 bioerror(bp, EIO); 11023 bp->b_resid = bp->b_bcount; 11024 SD_BEGIN_IODONE(index, un, bp); 11025 return; 11026 } 11027 11028 /* 11029 * We do not need a shadow buf if the device is using 11030 * un->un_sys_blocksize as its block size or if bcount == 0. 11031 * In this case there is no layer-private data block allocated. 11032 */ 11033 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 11034 (bp->b_bcount == 0)) { 11035 goto done; 11036 } 11037 11038 #if defined(__i386) || defined(__amd64) 11039 /* We do not support non-block-aligned transfers for ROD devices */ 11040 ASSERT(!ISROD(un)); 11041 #endif 11042 11043 xp = SD_GET_XBUF(bp); 11044 ASSERT(xp != NULL); 11045 11046 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11047 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 11048 un->un_tgt_blocksize, un->un_sys_blocksize); 11049 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11050 "request start block:0x%x\n", xp->xb_blkno); 11051 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11052 "request len:0x%x\n", bp->b_bcount); 11053 11054 /* 11055 * Allocate the layer-private data area for the mapblocksize layer. 11056 * Layers are allowed to use the xp_private member of the sd_xbuf 11057 * struct to store the pointer to their layer-private data block, but 11058 * each layer also has the responsibility of restoring the prior 11059 * contents of xb_private before returning the buf/xbuf to the 11060 * higher layer that sent it. 11061 * 11062 * Here we save the prior contents of xp->xb_private into the 11063 * bsp->mbs_oprivate field of our layer-private data area. This value 11064 * is restored by sd_mapblocksize_iodone() just prior to freeing up 11065 * the layer-private area and returning the buf/xbuf to the layer 11066 * that sent it. 11067 * 11068 * Note that here we use kmem_zalloc for the allocation as there are 11069 * parts of the mapblocksize code that expect certain fields to be 11070 * zero unless explicitly set to a required value. 11071 */ 11072 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 11073 bsp->mbs_oprivate = xp->xb_private; 11074 xp->xb_private = bsp; 11075 11076 /* 11077 * This treats the data on the disk (target) as an array of bytes. 11078 * first_byte is the byte offset, from the beginning of the device, 11079 * to the location of the request. This is converted from a 11080 * un->un_sys_blocksize block address to a byte offset, and then back 11081 * to a block address based upon a un->un_tgt_blocksize block size. 11082 * 11083 * xp->xb_blkno should be absolute upon entry into this function, 11084 * but, but it is based upon partitions that use the "system" 11085 * block size. It must be adjusted to reflect the block size of 11086 * the target. 11087 * 11088 * Note that end_block is actually the block that follows the last 11089 * block of the request, but that's what is needed for the computation. 11090 */ 11091 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 11092 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 11093 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 11094 un->un_tgt_blocksize; 11095 11096 /* request_bytes is rounded up to a multiple of the target block size */ 11097 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 11098 11099 /* 11100 * See if the starting address of the request and the request 11101 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 11102 * then we do not need to allocate a shadow buf to handle the request. 11103 */ 11104 if (((first_byte % un->un_tgt_blocksize) == 0) && 11105 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 11106 is_aligned = TRUE; 11107 } 11108 11109 if ((bp->b_flags & B_READ) == 0) { 11110 /* 11111 * Lock the range for a write operation. An aligned request is 11112 * considered a simple write; otherwise the request must be a 11113 * read-modify-write. 11114 */ 11115 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 11116 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 11117 } 11118 11119 /* 11120 * Alloc a shadow buf if the request is not aligned. Also, this is 11121 * where the READ command is generated for a read-modify-write. (The 11122 * write phase is deferred until after the read completes.) 11123 */ 11124 if (is_aligned == FALSE) { 11125 11126 struct sd_mapblocksize_info *shadow_bsp; 11127 struct sd_xbuf *shadow_xp; 11128 struct buf *shadow_bp; 11129 11130 /* 11131 * Allocate the shadow buf and it associated xbuf. Note that 11132 * after this call the xb_blkno value in both the original 11133 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 11134 * same: absolute relative to the start of the device, and 11135 * adjusted for the target block size. The b_blkno in the 11136 * shadow buf will also be set to this value. We should never 11137 * change b_blkno in the original bp however. 11138 * 11139 * Note also that the shadow buf will always need to be a 11140 * READ command, regardless of whether the incoming command 11141 * is a READ or a WRITE. 11142 */ 11143 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 11144 xp->xb_blkno, 11145 (int (*)(struct buf *)) sd_mapblocksize_iodone); 11146 11147 shadow_xp = SD_GET_XBUF(shadow_bp); 11148 11149 /* 11150 * Allocate the layer-private data for the shadow buf. 11151 * (No need to preserve xb_private in the shadow xbuf.) 11152 */ 11153 shadow_xp->xb_private = shadow_bsp = 11154 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 11155 11156 /* 11157 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 11158 * to figure out where the start of the user data is (based upon 11159 * the system block size) in the data returned by the READ 11160 * command (which will be based upon the target blocksize). Note 11161 * that this is only really used if the request is unaligned. 11162 */ 11163 bsp->mbs_copy_offset = (ssize_t)(first_byte - 11164 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 11165 ASSERT((bsp->mbs_copy_offset >= 0) && 11166 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 11167 11168 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 11169 11170 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 11171 11172 /* Transfer the wmap (if any) to the shadow buf */ 11173 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 11174 bsp->mbs_wmp = NULL; 11175 11176 /* 11177 * The shadow buf goes on from here in place of the 11178 * original buf. 11179 */ 11180 shadow_bsp->mbs_orig_bp = bp; 11181 bp = shadow_bp; 11182 } 11183 11184 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11185 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 11186 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11187 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 11188 request_bytes); 11189 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11190 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 11191 11192 done: 11193 SD_NEXT_IOSTART(index, un, bp); 11194 11195 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11196 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 11197 } 11198 11199 11200 /* 11201 * Function: sd_mapblocksize_iodone 11202 * 11203 * Description: Completion side processing for block-size mapping. 11204 * 11205 * Context: May be called under interrupt context 11206 */ 11207 11208 static void 11209 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 11210 { 11211 struct sd_mapblocksize_info *bsp; 11212 struct sd_xbuf *xp; 11213 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 11214 struct buf *orig_bp; /* ptr to the original buf */ 11215 offset_t shadow_end; 11216 offset_t request_end; 11217 offset_t shadow_start; 11218 ssize_t copy_offset; 11219 size_t copy_length; 11220 size_t shortfall; 11221 uint_t is_write; /* TRUE if this bp is a WRITE */ 11222 uint_t has_wmap; /* TRUE is this bp has a wmap */ 11223 11224 ASSERT(un != NULL); 11225 ASSERT(bp != NULL); 11226 11227 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11228 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 11229 11230 /* 11231 * There is no shadow buf or layer-private data if the target is 11232 * using un->un_sys_blocksize as its block size or if bcount == 0. 11233 */ 11234 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 11235 (bp->b_bcount == 0)) { 11236 goto exit; 11237 } 11238 11239 xp = SD_GET_XBUF(bp); 11240 ASSERT(xp != NULL); 11241 11242 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 11243 bsp = xp->xb_private; 11244 11245 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 11246 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 11247 11248 if (is_write) { 11249 /* 11250 * For a WRITE request we must free up the block range that 11251 * we have locked up. This holds regardless of whether this is 11252 * an aligned write request or a read-modify-write request. 11253 */ 11254 sd_range_unlock(un, bsp->mbs_wmp); 11255 bsp->mbs_wmp = NULL; 11256 } 11257 11258 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 11259 /* 11260 * An aligned read or write command will have no shadow buf; 11261 * there is not much else to do with it. 11262 */ 11263 goto done; 11264 } 11265 11266 orig_bp = bsp->mbs_orig_bp; 11267 ASSERT(orig_bp != NULL); 11268 orig_xp = SD_GET_XBUF(orig_bp); 11269 ASSERT(orig_xp != NULL); 11270 ASSERT(!mutex_owned(SD_MUTEX(un))); 11271 11272 if (!is_write && has_wmap) { 11273 /* 11274 * A READ with a wmap means this is the READ phase of a 11275 * read-modify-write. If an error occurred on the READ then 11276 * we do not proceed with the WRITE phase or copy any data. 11277 * Just release the write maps and return with an error. 11278 */ 11279 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 11280 orig_bp->b_resid = orig_bp->b_bcount; 11281 bioerror(orig_bp, bp->b_error); 11282 sd_range_unlock(un, bsp->mbs_wmp); 11283 goto freebuf_done; 11284 } 11285 } 11286 11287 /* 11288 * Here is where we set up to copy the data from the shadow buf 11289 * into the space associated with the original buf. 11290 * 11291 * To deal with the conversion between block sizes, these 11292 * computations treat the data as an array of bytes, with the 11293 * first byte (byte 0) corresponding to the first byte in the 11294 * first block on the disk. 11295 */ 11296 11297 /* 11298 * shadow_start and shadow_len indicate the location and size of 11299 * the data returned with the shadow IO request. 11300 */ 11301 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 11302 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 11303 11304 /* 11305 * copy_offset gives the offset (in bytes) from the start of the first 11306 * block of the READ request to the beginning of the data. We retrieve 11307 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 11308 * there by sd_mapblockize_iostart(). copy_length gives the amount of 11309 * data to be copied (in bytes). 11310 */ 11311 copy_offset = bsp->mbs_copy_offset; 11312 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 11313 copy_length = orig_bp->b_bcount; 11314 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 11315 11316 /* 11317 * Set up the resid and error fields of orig_bp as appropriate. 11318 */ 11319 if (shadow_end >= request_end) { 11320 /* We got all the requested data; set resid to zero */ 11321 orig_bp->b_resid = 0; 11322 } else { 11323 /* 11324 * We failed to get enough data to fully satisfy the original 11325 * request. Just copy back whatever data we got and set 11326 * up the residual and error code as required. 11327 * 11328 * 'shortfall' is the amount by which the data received with the 11329 * shadow buf has "fallen short" of the requested amount. 11330 */ 11331 shortfall = (size_t)(request_end - shadow_end); 11332 11333 if (shortfall > orig_bp->b_bcount) { 11334 /* 11335 * We did not get enough data to even partially 11336 * fulfill the original request. The residual is 11337 * equal to the amount requested. 11338 */ 11339 orig_bp->b_resid = orig_bp->b_bcount; 11340 } else { 11341 /* 11342 * We did not get all the data that we requested 11343 * from the device, but we will try to return what 11344 * portion we did get. 11345 */ 11346 orig_bp->b_resid = shortfall; 11347 } 11348 ASSERT(copy_length >= orig_bp->b_resid); 11349 copy_length -= orig_bp->b_resid; 11350 } 11351 11352 /* Propagate the error code from the shadow buf to the original buf */ 11353 bioerror(orig_bp, bp->b_error); 11354 11355 if (is_write) { 11356 goto freebuf_done; /* No data copying for a WRITE */ 11357 } 11358 11359 if (has_wmap) { 11360 /* 11361 * This is a READ command from the READ phase of a 11362 * read-modify-write request. We have to copy the data given 11363 * by the user OVER the data returned by the READ command, 11364 * then convert the command from a READ to a WRITE and send 11365 * it back to the target. 11366 */ 11367 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 11368 copy_length); 11369 11370 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 11371 11372 /* 11373 * Dispatch the WRITE command to the taskq thread, which 11374 * will in turn send the command to the target. When the 11375 * WRITE command completes, we (sd_mapblocksize_iodone()) 11376 * will get called again as part of the iodone chain 11377 * processing for it. Note that we will still be dealing 11378 * with the shadow buf at that point. 11379 */ 11380 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 11381 KM_NOSLEEP) != 0) { 11382 /* 11383 * Dispatch was successful so we are done. Return 11384 * without going any higher up the iodone chain. Do 11385 * not free up any layer-private data until after the 11386 * WRITE completes. 11387 */ 11388 return; 11389 } 11390 11391 /* 11392 * Dispatch of the WRITE command failed; set up the error 11393 * condition and send this IO back up the iodone chain. 11394 */ 11395 bioerror(orig_bp, EIO); 11396 orig_bp->b_resid = orig_bp->b_bcount; 11397 11398 } else { 11399 /* 11400 * This is a regular READ request (ie, not a RMW). Copy the 11401 * data from the shadow buf into the original buf. The 11402 * copy_offset compensates for any "misalignment" between the 11403 * shadow buf (with its un->un_tgt_blocksize blocks) and the 11404 * original buf (with its un->un_sys_blocksize blocks). 11405 */ 11406 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 11407 copy_length); 11408 } 11409 11410 freebuf_done: 11411 11412 /* 11413 * At this point we still have both the shadow buf AND the original 11414 * buf to deal with, as well as the layer-private data area in each. 11415 * Local variables are as follows: 11416 * 11417 * bp -- points to shadow buf 11418 * xp -- points to xbuf of shadow buf 11419 * bsp -- points to layer-private data area of shadow buf 11420 * orig_bp -- points to original buf 11421 * 11422 * First free the shadow buf and its associated xbuf, then free the 11423 * layer-private data area from the shadow buf. There is no need to 11424 * restore xb_private in the shadow xbuf. 11425 */ 11426 sd_shadow_buf_free(bp); 11427 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11428 11429 /* 11430 * Now update the local variables to point to the original buf, xbuf, 11431 * and layer-private area. 11432 */ 11433 bp = orig_bp; 11434 xp = SD_GET_XBUF(bp); 11435 ASSERT(xp != NULL); 11436 ASSERT(xp == orig_xp); 11437 bsp = xp->xb_private; 11438 ASSERT(bsp != NULL); 11439 11440 done: 11441 /* 11442 * Restore xb_private to whatever it was set to by the next higher 11443 * layer in the chain, then free the layer-private data area. 11444 */ 11445 xp->xb_private = bsp->mbs_oprivate; 11446 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11447 11448 exit: 11449 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 11450 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 11451 11452 SD_NEXT_IODONE(index, un, bp); 11453 } 11454 11455 11456 /* 11457 * Function: sd_checksum_iostart 11458 * 11459 * Description: A stub function for a layer that's currently not used. 11460 * For now just a placeholder. 11461 * 11462 * Context: Kernel thread context 11463 */ 11464 11465 static void 11466 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 11467 { 11468 ASSERT(un != NULL); 11469 ASSERT(bp != NULL); 11470 ASSERT(!mutex_owned(SD_MUTEX(un))); 11471 SD_NEXT_IOSTART(index, un, bp); 11472 } 11473 11474 11475 /* 11476 * Function: sd_checksum_iodone 11477 * 11478 * Description: A stub function for a layer that's currently not used. 11479 * For now just a placeholder. 11480 * 11481 * Context: May be called under interrupt context 11482 */ 11483 11484 static void 11485 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 11486 { 11487 ASSERT(un != NULL); 11488 ASSERT(bp != NULL); 11489 ASSERT(!mutex_owned(SD_MUTEX(un))); 11490 SD_NEXT_IODONE(index, un, bp); 11491 } 11492 11493 11494 /* 11495 * Function: sd_checksum_uscsi_iostart 11496 * 11497 * Description: A stub function for a layer that's currently not used. 11498 * For now just a placeholder. 11499 * 11500 * Context: Kernel thread context 11501 */ 11502 11503 static void 11504 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 11505 { 11506 ASSERT(un != NULL); 11507 ASSERT(bp != NULL); 11508 ASSERT(!mutex_owned(SD_MUTEX(un))); 11509 SD_NEXT_IOSTART(index, un, bp); 11510 } 11511 11512 11513 /* 11514 * Function: sd_checksum_uscsi_iodone 11515 * 11516 * Description: A stub function for a layer that's currently not used. 11517 * For now just a placeholder. 11518 * 11519 * Context: May be called under interrupt context 11520 */ 11521 11522 static void 11523 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11524 { 11525 ASSERT(un != NULL); 11526 ASSERT(bp != NULL); 11527 ASSERT(!mutex_owned(SD_MUTEX(un))); 11528 SD_NEXT_IODONE(index, un, bp); 11529 } 11530 11531 11532 /* 11533 * Function: sd_pm_iostart 11534 * 11535 * Description: iostart-side routine for Power mangement. 11536 * 11537 * Context: Kernel thread context 11538 */ 11539 11540 static void 11541 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 11542 { 11543 ASSERT(un != NULL); 11544 ASSERT(bp != NULL); 11545 ASSERT(!mutex_owned(SD_MUTEX(un))); 11546 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11547 11548 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 11549 11550 if (sd_pm_entry(un) != DDI_SUCCESS) { 11551 /* 11552 * Set up to return the failed buf back up the 'iodone' 11553 * side of the calling chain. 11554 */ 11555 bioerror(bp, EIO); 11556 bp->b_resid = bp->b_bcount; 11557 11558 SD_BEGIN_IODONE(index, un, bp); 11559 11560 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11561 return; 11562 } 11563 11564 SD_NEXT_IOSTART(index, un, bp); 11565 11566 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11567 } 11568 11569 11570 /* 11571 * Function: sd_pm_iodone 11572 * 11573 * Description: iodone-side routine for power mangement. 11574 * 11575 * Context: may be called from interrupt context 11576 */ 11577 11578 static void 11579 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 11580 { 11581 ASSERT(un != NULL); 11582 ASSERT(bp != NULL); 11583 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11584 11585 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 11586 11587 /* 11588 * After attach the following flag is only read, so don't 11589 * take the penalty of acquiring a mutex for it. 11590 */ 11591 if (un->un_f_pm_is_enabled == TRUE) { 11592 sd_pm_exit(un); 11593 } 11594 11595 SD_NEXT_IODONE(index, un, bp); 11596 11597 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 11598 } 11599 11600 11601 /* 11602 * Function: sd_core_iostart 11603 * 11604 * Description: Primary driver function for enqueuing buf(9S) structs from 11605 * the system and initiating IO to the target device 11606 * 11607 * Context: Kernel thread context. Can sleep. 11608 * 11609 * Assumptions: - The given xp->xb_blkno is absolute 11610 * (ie, relative to the start of the device). 11611 * - The IO is to be done using the native blocksize of 11612 * the device, as specified in un->un_tgt_blocksize. 11613 */ 11614 /* ARGSUSED */ 11615 static void 11616 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 11617 { 11618 struct sd_xbuf *xp; 11619 11620 ASSERT(un != NULL); 11621 ASSERT(bp != NULL); 11622 ASSERT(!mutex_owned(SD_MUTEX(un))); 11623 ASSERT(bp->b_resid == 0); 11624 11625 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 11626 11627 xp = SD_GET_XBUF(bp); 11628 ASSERT(xp != NULL); 11629 11630 mutex_enter(SD_MUTEX(un)); 11631 11632 /* 11633 * If we are currently in the failfast state, fail any new IO 11634 * that has B_FAILFAST set, then return. 11635 */ 11636 if ((bp->b_flags & B_FAILFAST) && 11637 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 11638 mutex_exit(SD_MUTEX(un)); 11639 bioerror(bp, EIO); 11640 bp->b_resid = bp->b_bcount; 11641 SD_BEGIN_IODONE(index, un, bp); 11642 return; 11643 } 11644 11645 if (SD_IS_DIRECT_PRIORITY(xp)) { 11646 /* 11647 * Priority command -- transport it immediately. 11648 * 11649 * Note: We may want to assert that USCSI_DIAGNOSE is set, 11650 * because all direct priority commands should be associated 11651 * with error recovery actions which we don't want to retry. 11652 */ 11653 sd_start_cmds(un, bp); 11654 } else { 11655 /* 11656 * Normal command -- add it to the wait queue, then start 11657 * transporting commands from the wait queue. 11658 */ 11659 sd_add_buf_to_waitq(un, bp); 11660 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 11661 sd_start_cmds(un, NULL); 11662 } 11663 11664 mutex_exit(SD_MUTEX(un)); 11665 11666 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 11667 } 11668 11669 11670 /* 11671 * Function: sd_init_cdb_limits 11672 * 11673 * Description: This is to handle scsi_pkt initialization differences 11674 * between the driver platforms. 11675 * 11676 * Legacy behaviors: 11677 * 11678 * If the block number or the sector count exceeds the 11679 * capabilities of a Group 0 command, shift over to a 11680 * Group 1 command. We don't blindly use Group 1 11681 * commands because a) some drives (CDC Wren IVs) get a 11682 * bit confused, and b) there is probably a fair amount 11683 * of speed difference for a target to receive and decode 11684 * a 10 byte command instead of a 6 byte command. 11685 * 11686 * The xfer time difference of 6 vs 10 byte CDBs is 11687 * still significant so this code is still worthwhile. 11688 * 10 byte CDBs are very inefficient with the fas HBA driver 11689 * and older disks. Each CDB byte took 1 usec with some 11690 * popular disks. 11691 * 11692 * Context: Must be called at attach time 11693 */ 11694 11695 static void 11696 sd_init_cdb_limits(struct sd_lun *un) 11697 { 11698 int hba_cdb_limit; 11699 11700 /* 11701 * Use CDB_GROUP1 commands for most devices except for 11702 * parallel SCSI fixed drives in which case we get better 11703 * performance using CDB_GROUP0 commands (where applicable). 11704 */ 11705 un->un_mincdb = SD_CDB_GROUP1; 11706 #if !defined(__fibre) 11707 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 11708 !un->un_f_has_removable_media) { 11709 un->un_mincdb = SD_CDB_GROUP0; 11710 } 11711 #endif 11712 11713 /* 11714 * Try to read the max-cdb-length supported by HBA. 11715 */ 11716 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 11717 if (0 >= un->un_max_hba_cdb) { 11718 un->un_max_hba_cdb = CDB_GROUP4; 11719 hba_cdb_limit = SD_CDB_GROUP4; 11720 } else if (0 < un->un_max_hba_cdb && 11721 un->un_max_hba_cdb < CDB_GROUP1) { 11722 hba_cdb_limit = SD_CDB_GROUP0; 11723 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 11724 un->un_max_hba_cdb < CDB_GROUP5) { 11725 hba_cdb_limit = SD_CDB_GROUP1; 11726 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 11727 un->un_max_hba_cdb < CDB_GROUP4) { 11728 hba_cdb_limit = SD_CDB_GROUP5; 11729 } else { 11730 hba_cdb_limit = SD_CDB_GROUP4; 11731 } 11732 11733 /* 11734 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 11735 * commands for fixed disks unless we are building for a 32 bit 11736 * kernel. 11737 */ 11738 #ifdef _LP64 11739 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11740 min(hba_cdb_limit, SD_CDB_GROUP4); 11741 #else 11742 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11743 min(hba_cdb_limit, SD_CDB_GROUP1); 11744 #endif 11745 11746 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 11747 ? sizeof (struct scsi_arq_status) : 1); 11748 un->un_cmd_timeout = (ushort_t)sd_io_time; 11749 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 11750 } 11751 11752 11753 /* 11754 * Function: sd_initpkt_for_buf 11755 * 11756 * Description: Allocate and initialize for transport a scsi_pkt struct, 11757 * based upon the info specified in the given buf struct. 11758 * 11759 * Assumes the xb_blkno in the request is absolute (ie, 11760 * relative to the start of the device (NOT partition!). 11761 * Also assumes that the request is using the native block 11762 * size of the device (as returned by the READ CAPACITY 11763 * command). 11764 * 11765 * Return Code: SD_PKT_ALLOC_SUCCESS 11766 * SD_PKT_ALLOC_FAILURE 11767 * SD_PKT_ALLOC_FAILURE_NO_DMA 11768 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11769 * 11770 * Context: Kernel thread and may be called from software interrupt context 11771 * as part of a sdrunout callback. This function may not block or 11772 * call routines that block 11773 */ 11774 11775 static int 11776 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 11777 { 11778 struct sd_xbuf *xp; 11779 struct scsi_pkt *pktp = NULL; 11780 struct sd_lun *un; 11781 size_t blockcount; 11782 daddr_t startblock; 11783 int rval; 11784 int cmd_flags; 11785 11786 ASSERT(bp != NULL); 11787 ASSERT(pktpp != NULL); 11788 xp = SD_GET_XBUF(bp); 11789 ASSERT(xp != NULL); 11790 un = SD_GET_UN(bp); 11791 ASSERT(un != NULL); 11792 ASSERT(mutex_owned(SD_MUTEX(un))); 11793 ASSERT(bp->b_resid == 0); 11794 11795 SD_TRACE(SD_LOG_IO_CORE, un, 11796 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 11797 11798 mutex_exit(SD_MUTEX(un)); 11799 11800 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11801 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 11802 /* 11803 * Already have a scsi_pkt -- just need DMA resources. 11804 * We must recompute the CDB in case the mapping returns 11805 * a nonzero pkt_resid. 11806 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 11807 * that is being retried, the unmap/remap of the DMA resouces 11808 * will result in the entire transfer starting over again 11809 * from the very first block. 11810 */ 11811 ASSERT(xp->xb_pktp != NULL); 11812 pktp = xp->xb_pktp; 11813 } else { 11814 pktp = NULL; 11815 } 11816 #endif /* __i386 || __amd64 */ 11817 11818 startblock = xp->xb_blkno; /* Absolute block num. */ 11819 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 11820 11821 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 11822 11823 /* 11824 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 11825 * call scsi_init_pkt, and build the CDB. 11826 */ 11827 rval = sd_setup_rw_pkt(un, &pktp, bp, 11828 cmd_flags, sdrunout, (caddr_t)un, 11829 startblock, blockcount); 11830 11831 if (rval == 0) { 11832 /* 11833 * Success. 11834 * 11835 * If partial DMA is being used and required for this transfer. 11836 * set it up here. 11837 */ 11838 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 11839 (pktp->pkt_resid != 0)) { 11840 11841 /* 11842 * Save the CDB length and pkt_resid for the 11843 * next xfer 11844 */ 11845 xp->xb_dma_resid = pktp->pkt_resid; 11846 11847 /* rezero resid */ 11848 pktp->pkt_resid = 0; 11849 11850 } else { 11851 xp->xb_dma_resid = 0; 11852 } 11853 11854 pktp->pkt_flags = un->un_tagflags; 11855 pktp->pkt_time = un->un_cmd_timeout; 11856 pktp->pkt_comp = sdintr; 11857 11858 pktp->pkt_private = bp; 11859 *pktpp = pktp; 11860 11861 SD_TRACE(SD_LOG_IO_CORE, un, 11862 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 11863 11864 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11865 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 11866 #endif 11867 11868 mutex_enter(SD_MUTEX(un)); 11869 return (SD_PKT_ALLOC_SUCCESS); 11870 11871 } 11872 11873 /* 11874 * SD_PKT_ALLOC_FAILURE is the only expected failure code 11875 * from sd_setup_rw_pkt. 11876 */ 11877 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 11878 11879 if (rval == SD_PKT_ALLOC_FAILURE) { 11880 *pktpp = NULL; 11881 /* 11882 * Set the driver state to RWAIT to indicate the driver 11883 * is waiting on resource allocations. The driver will not 11884 * suspend, pm_suspend, or detatch while the state is RWAIT. 11885 */ 11886 mutex_enter(SD_MUTEX(un)); 11887 New_state(un, SD_STATE_RWAIT); 11888 11889 SD_ERROR(SD_LOG_IO_CORE, un, 11890 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 11891 11892 if ((bp->b_flags & B_ERROR) != 0) { 11893 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 11894 } 11895 return (SD_PKT_ALLOC_FAILURE); 11896 } else { 11897 /* 11898 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11899 * 11900 * This should never happen. Maybe someone messed with the 11901 * kernel's minphys? 11902 */ 11903 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11904 "Request rejected: too large for CDB: " 11905 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 11906 SD_ERROR(SD_LOG_IO_CORE, un, 11907 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 11908 mutex_enter(SD_MUTEX(un)); 11909 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11910 11911 } 11912 } 11913 11914 11915 /* 11916 * Function: sd_destroypkt_for_buf 11917 * 11918 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 11919 * 11920 * Context: Kernel thread or interrupt context 11921 */ 11922 11923 static void 11924 sd_destroypkt_for_buf(struct buf *bp) 11925 { 11926 ASSERT(bp != NULL); 11927 ASSERT(SD_GET_UN(bp) != NULL); 11928 11929 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11930 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 11931 11932 ASSERT(SD_GET_PKTP(bp) != NULL); 11933 scsi_destroy_pkt(SD_GET_PKTP(bp)); 11934 11935 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11936 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 11937 } 11938 11939 /* 11940 * Function: sd_setup_rw_pkt 11941 * 11942 * Description: Determines appropriate CDB group for the requested LBA 11943 * and transfer length, calls scsi_init_pkt, and builds 11944 * the CDB. Do not use for partial DMA transfers except 11945 * for the initial transfer since the CDB size must 11946 * remain constant. 11947 * 11948 * Context: Kernel thread and may be called from software interrupt 11949 * context as part of a sdrunout callback. This function may not 11950 * block or call routines that block 11951 */ 11952 11953 11954 int 11955 sd_setup_rw_pkt(struct sd_lun *un, 11956 struct scsi_pkt **pktpp, struct buf *bp, int flags, 11957 int (*callback)(caddr_t), caddr_t callback_arg, 11958 diskaddr_t lba, uint32_t blockcount) 11959 { 11960 struct scsi_pkt *return_pktp; 11961 union scsi_cdb *cdbp; 11962 struct sd_cdbinfo *cp = NULL; 11963 int i; 11964 11965 /* 11966 * See which size CDB to use, based upon the request. 11967 */ 11968 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 11969 11970 /* 11971 * Check lba and block count against sd_cdbtab limits. 11972 * In the partial DMA case, we have to use the same size 11973 * CDB for all the transfers. Check lba + blockcount 11974 * against the max LBA so we know that segment of the 11975 * transfer can use the CDB we select. 11976 */ 11977 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 11978 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 11979 11980 /* 11981 * The command will fit into the CDB type 11982 * specified by sd_cdbtab[i]. 11983 */ 11984 cp = sd_cdbtab + i; 11985 11986 /* 11987 * Call scsi_init_pkt so we can fill in the 11988 * CDB. 11989 */ 11990 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 11991 bp, cp->sc_grpcode, un->un_status_len, 0, 11992 flags, callback, callback_arg); 11993 11994 if (return_pktp != NULL) { 11995 11996 /* 11997 * Return new value of pkt 11998 */ 11999 *pktpp = return_pktp; 12000 12001 /* 12002 * To be safe, zero the CDB insuring there is 12003 * no leftover data from a previous command. 12004 */ 12005 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 12006 12007 /* 12008 * Handle partial DMA mapping 12009 */ 12010 if (return_pktp->pkt_resid != 0) { 12011 12012 /* 12013 * Not going to xfer as many blocks as 12014 * originally expected 12015 */ 12016 blockcount -= 12017 SD_BYTES2TGTBLOCKS(un, 12018 return_pktp->pkt_resid); 12019 } 12020 12021 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 12022 12023 /* 12024 * Set command byte based on the CDB 12025 * type we matched. 12026 */ 12027 cdbp->scc_cmd = cp->sc_grpmask | 12028 ((bp->b_flags & B_READ) ? 12029 SCMD_READ : SCMD_WRITE); 12030 12031 SD_FILL_SCSI1_LUN(un, return_pktp); 12032 12033 /* 12034 * Fill in LBA and length 12035 */ 12036 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 12037 (cp->sc_grpcode == CDB_GROUP4) || 12038 (cp->sc_grpcode == CDB_GROUP0) || 12039 (cp->sc_grpcode == CDB_GROUP5)); 12040 12041 if (cp->sc_grpcode == CDB_GROUP1) { 12042 FORMG1ADDR(cdbp, lba); 12043 FORMG1COUNT(cdbp, blockcount); 12044 return (0); 12045 } else if (cp->sc_grpcode == CDB_GROUP4) { 12046 FORMG4LONGADDR(cdbp, lba); 12047 FORMG4COUNT(cdbp, blockcount); 12048 return (0); 12049 } else if (cp->sc_grpcode == CDB_GROUP0) { 12050 FORMG0ADDR(cdbp, lba); 12051 FORMG0COUNT(cdbp, blockcount); 12052 return (0); 12053 } else if (cp->sc_grpcode == CDB_GROUP5) { 12054 FORMG5ADDR(cdbp, lba); 12055 FORMG5COUNT(cdbp, blockcount); 12056 return (0); 12057 } 12058 12059 /* 12060 * It should be impossible to not match one 12061 * of the CDB types above, so we should never 12062 * reach this point. Set the CDB command byte 12063 * to test-unit-ready to avoid writing 12064 * to somewhere we don't intend. 12065 */ 12066 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 12067 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12068 } else { 12069 /* 12070 * Couldn't get scsi_pkt 12071 */ 12072 return (SD_PKT_ALLOC_FAILURE); 12073 } 12074 } 12075 } 12076 12077 /* 12078 * None of the available CDB types were suitable. This really 12079 * should never happen: on a 64 bit system we support 12080 * READ16/WRITE16 which will hold an entire 64 bit disk address 12081 * and on a 32 bit system we will refuse to bind to a device 12082 * larger than 2TB so addresses will never be larger than 32 bits. 12083 */ 12084 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12085 } 12086 12087 /* 12088 * Function: sd_setup_next_rw_pkt 12089 * 12090 * Description: Setup packet for partial DMA transfers, except for the 12091 * initial transfer. sd_setup_rw_pkt should be used for 12092 * the initial transfer. 12093 * 12094 * Context: Kernel thread and may be called from interrupt context. 12095 */ 12096 12097 int 12098 sd_setup_next_rw_pkt(struct sd_lun *un, 12099 struct scsi_pkt *pktp, struct buf *bp, 12100 diskaddr_t lba, uint32_t blockcount) 12101 { 12102 uchar_t com; 12103 union scsi_cdb *cdbp; 12104 uchar_t cdb_group_id; 12105 12106 ASSERT(pktp != NULL); 12107 ASSERT(pktp->pkt_cdbp != NULL); 12108 12109 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 12110 com = cdbp->scc_cmd; 12111 cdb_group_id = CDB_GROUPID(com); 12112 12113 ASSERT((cdb_group_id == CDB_GROUPID_0) || 12114 (cdb_group_id == CDB_GROUPID_1) || 12115 (cdb_group_id == CDB_GROUPID_4) || 12116 (cdb_group_id == CDB_GROUPID_5)); 12117 12118 /* 12119 * Move pkt to the next portion of the xfer. 12120 * func is NULL_FUNC so we do not have to release 12121 * the disk mutex here. 12122 */ 12123 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 12124 NULL_FUNC, NULL) == pktp) { 12125 /* Success. Handle partial DMA */ 12126 if (pktp->pkt_resid != 0) { 12127 blockcount -= 12128 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 12129 } 12130 12131 cdbp->scc_cmd = com; 12132 SD_FILL_SCSI1_LUN(un, pktp); 12133 if (cdb_group_id == CDB_GROUPID_1) { 12134 FORMG1ADDR(cdbp, lba); 12135 FORMG1COUNT(cdbp, blockcount); 12136 return (0); 12137 } else if (cdb_group_id == CDB_GROUPID_4) { 12138 FORMG4LONGADDR(cdbp, lba); 12139 FORMG4COUNT(cdbp, blockcount); 12140 return (0); 12141 } else if (cdb_group_id == CDB_GROUPID_0) { 12142 FORMG0ADDR(cdbp, lba); 12143 FORMG0COUNT(cdbp, blockcount); 12144 return (0); 12145 } else if (cdb_group_id == CDB_GROUPID_5) { 12146 FORMG5ADDR(cdbp, lba); 12147 FORMG5COUNT(cdbp, blockcount); 12148 return (0); 12149 } 12150 12151 /* Unreachable */ 12152 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12153 } 12154 12155 /* 12156 * Error setting up next portion of cmd transfer. 12157 * Something is definitely very wrong and this 12158 * should not happen. 12159 */ 12160 return (SD_PKT_ALLOC_FAILURE); 12161 } 12162 12163 /* 12164 * Function: sd_initpkt_for_uscsi 12165 * 12166 * Description: Allocate and initialize for transport a scsi_pkt struct, 12167 * based upon the info specified in the given uscsi_cmd struct. 12168 * 12169 * Return Code: SD_PKT_ALLOC_SUCCESS 12170 * SD_PKT_ALLOC_FAILURE 12171 * SD_PKT_ALLOC_FAILURE_NO_DMA 12172 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 12173 * 12174 * Context: Kernel thread and may be called from software interrupt context 12175 * as part of a sdrunout callback. This function may not block or 12176 * call routines that block 12177 */ 12178 12179 static int 12180 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 12181 { 12182 struct uscsi_cmd *uscmd; 12183 struct sd_xbuf *xp; 12184 struct scsi_pkt *pktp; 12185 struct sd_lun *un; 12186 uint32_t flags = 0; 12187 12188 ASSERT(bp != NULL); 12189 ASSERT(pktpp != NULL); 12190 xp = SD_GET_XBUF(bp); 12191 ASSERT(xp != NULL); 12192 un = SD_GET_UN(bp); 12193 ASSERT(un != NULL); 12194 ASSERT(mutex_owned(SD_MUTEX(un))); 12195 12196 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12197 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12198 ASSERT(uscmd != NULL); 12199 12200 SD_TRACE(SD_LOG_IO_CORE, un, 12201 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 12202 12203 /* 12204 * Allocate the scsi_pkt for the command. 12205 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 12206 * during scsi_init_pkt time and will continue to use the 12207 * same path as long as the same scsi_pkt is used without 12208 * intervening scsi_dma_free(). Since uscsi command does 12209 * not call scsi_dmafree() before retry failed command, it 12210 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 12211 * set such that scsi_vhci can use other available path for 12212 * retry. Besides, ucsci command does not allow DMA breakup, 12213 * so there is no need to set PKT_DMA_PARTIAL flag. 12214 */ 12215 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 12216 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 12217 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 12218 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 12219 - sizeof (struct scsi_extended_sense)), 0, 12220 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 12221 sdrunout, (caddr_t)un); 12222 } else { 12223 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 12224 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 12225 sizeof (struct scsi_arq_status), 0, 12226 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 12227 sdrunout, (caddr_t)un); 12228 } 12229 12230 if (pktp == NULL) { 12231 *pktpp = NULL; 12232 /* 12233 * Set the driver state to RWAIT to indicate the driver 12234 * is waiting on resource allocations. The driver will not 12235 * suspend, pm_suspend, or detatch while the state is RWAIT. 12236 */ 12237 New_state(un, SD_STATE_RWAIT); 12238 12239 SD_ERROR(SD_LOG_IO_CORE, un, 12240 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 12241 12242 if ((bp->b_flags & B_ERROR) != 0) { 12243 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 12244 } 12245 return (SD_PKT_ALLOC_FAILURE); 12246 } 12247 12248 /* 12249 * We do not do DMA breakup for USCSI commands, so return failure 12250 * here if all the needed DMA resources were not allocated. 12251 */ 12252 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 12253 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 12254 scsi_destroy_pkt(pktp); 12255 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 12256 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 12257 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 12258 } 12259 12260 /* Init the cdb from the given uscsi struct */ 12261 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 12262 uscmd->uscsi_cdb[0], 0, 0, 0); 12263 12264 SD_FILL_SCSI1_LUN(un, pktp); 12265 12266 /* 12267 * Set up the optional USCSI flags. See the uscsi (7I) man page 12268 * for listing of the supported flags. 12269 */ 12270 12271 if (uscmd->uscsi_flags & USCSI_SILENT) { 12272 flags |= FLAG_SILENT; 12273 } 12274 12275 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 12276 flags |= FLAG_DIAGNOSE; 12277 } 12278 12279 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 12280 flags |= FLAG_ISOLATE; 12281 } 12282 12283 if (un->un_f_is_fibre == FALSE) { 12284 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 12285 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 12286 } 12287 } 12288 12289 /* 12290 * Set the pkt flags here so we save time later. 12291 * Note: These flags are NOT in the uscsi man page!!! 12292 */ 12293 if (uscmd->uscsi_flags & USCSI_HEAD) { 12294 flags |= FLAG_HEAD; 12295 } 12296 12297 if (uscmd->uscsi_flags & USCSI_NOINTR) { 12298 flags |= FLAG_NOINTR; 12299 } 12300 12301 /* 12302 * For tagged queueing, things get a bit complicated. 12303 * Check first for head of queue and last for ordered queue. 12304 * If neither head nor order, use the default driver tag flags. 12305 */ 12306 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 12307 if (uscmd->uscsi_flags & USCSI_HTAG) { 12308 flags |= FLAG_HTAG; 12309 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 12310 flags |= FLAG_OTAG; 12311 } else { 12312 flags |= un->un_tagflags & FLAG_TAGMASK; 12313 } 12314 } 12315 12316 if (uscmd->uscsi_flags & USCSI_NODISCON) { 12317 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 12318 } 12319 12320 pktp->pkt_flags = flags; 12321 12322 /* Transfer uscsi information to scsi_pkt */ 12323 (void) scsi_uscsi_pktinit(uscmd, pktp); 12324 12325 /* Copy the caller's CDB into the pkt... */ 12326 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 12327 12328 if (uscmd->uscsi_timeout == 0) { 12329 pktp->pkt_time = un->un_uscsi_timeout; 12330 } else { 12331 pktp->pkt_time = uscmd->uscsi_timeout; 12332 } 12333 12334 /* need it later to identify USCSI request in sdintr */ 12335 xp->xb_pkt_flags |= SD_XB_USCSICMD; 12336 12337 xp->xb_sense_resid = uscmd->uscsi_rqresid; 12338 12339 pktp->pkt_private = bp; 12340 pktp->pkt_comp = sdintr; 12341 *pktpp = pktp; 12342 12343 SD_TRACE(SD_LOG_IO_CORE, un, 12344 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 12345 12346 return (SD_PKT_ALLOC_SUCCESS); 12347 } 12348 12349 12350 /* 12351 * Function: sd_destroypkt_for_uscsi 12352 * 12353 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 12354 * IOs.. Also saves relevant info into the associated uscsi_cmd 12355 * struct. 12356 * 12357 * Context: May be called under interrupt context 12358 */ 12359 12360 static void 12361 sd_destroypkt_for_uscsi(struct buf *bp) 12362 { 12363 struct uscsi_cmd *uscmd; 12364 struct sd_xbuf *xp; 12365 struct scsi_pkt *pktp; 12366 struct sd_lun *un; 12367 12368 ASSERT(bp != NULL); 12369 xp = SD_GET_XBUF(bp); 12370 ASSERT(xp != NULL); 12371 un = SD_GET_UN(bp); 12372 ASSERT(un != NULL); 12373 ASSERT(!mutex_owned(SD_MUTEX(un))); 12374 pktp = SD_GET_PKTP(bp); 12375 ASSERT(pktp != NULL); 12376 12377 SD_TRACE(SD_LOG_IO_CORE, un, 12378 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 12379 12380 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12381 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12382 ASSERT(uscmd != NULL); 12383 12384 /* Save the status and the residual into the uscsi_cmd struct */ 12385 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 12386 uscmd->uscsi_resid = bp->b_resid; 12387 12388 /* Transfer scsi_pkt information to uscsi */ 12389 (void) scsi_uscsi_pktfini(pktp, uscmd); 12390 12391 /* 12392 * If enabled, copy any saved sense data into the area specified 12393 * by the uscsi command. 12394 */ 12395 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12396 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12397 /* 12398 * Note: uscmd->uscsi_rqbuf should always point to a buffer 12399 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 12400 */ 12401 uscmd->uscsi_rqstatus = xp->xb_sense_status; 12402 uscmd->uscsi_rqresid = xp->xb_sense_resid; 12403 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 12404 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 12405 MAX_SENSE_LENGTH); 12406 } else { 12407 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 12408 SENSE_LENGTH); 12409 } 12410 } 12411 12412 /* We are done with the scsi_pkt; free it now */ 12413 ASSERT(SD_GET_PKTP(bp) != NULL); 12414 scsi_destroy_pkt(SD_GET_PKTP(bp)); 12415 12416 SD_TRACE(SD_LOG_IO_CORE, un, 12417 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 12418 } 12419 12420 12421 /* 12422 * Function: sd_bioclone_alloc 12423 * 12424 * Description: Allocate a buf(9S) and init it as per the given buf 12425 * and the various arguments. The associated sd_xbuf 12426 * struct is (nearly) duplicated. The struct buf *bp 12427 * argument is saved in new_xp->xb_private. 12428 * 12429 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12430 * datalen - size of data area for the shadow bp 12431 * blkno - starting LBA 12432 * func - function pointer for b_iodone in the shadow buf. (May 12433 * be NULL if none.) 12434 * 12435 * Return Code: Pointer to allocates buf(9S) struct 12436 * 12437 * Context: Can sleep. 12438 */ 12439 12440 static struct buf * 12441 sd_bioclone_alloc(struct buf *bp, size_t datalen, 12442 daddr_t blkno, int (*func)(struct buf *)) 12443 { 12444 struct sd_lun *un; 12445 struct sd_xbuf *xp; 12446 struct sd_xbuf *new_xp; 12447 struct buf *new_bp; 12448 12449 ASSERT(bp != NULL); 12450 xp = SD_GET_XBUF(bp); 12451 ASSERT(xp != NULL); 12452 un = SD_GET_UN(bp); 12453 ASSERT(un != NULL); 12454 ASSERT(!mutex_owned(SD_MUTEX(un))); 12455 12456 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 12457 NULL, KM_SLEEP); 12458 12459 new_bp->b_lblkno = blkno; 12460 12461 /* 12462 * Allocate an xbuf for the shadow bp and copy the contents of the 12463 * original xbuf into it. 12464 */ 12465 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12466 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12467 12468 /* 12469 * The given bp is automatically saved in the xb_private member 12470 * of the new xbuf. Callers are allowed to depend on this. 12471 */ 12472 new_xp->xb_private = bp; 12473 12474 new_bp->b_private = new_xp; 12475 12476 return (new_bp); 12477 } 12478 12479 /* 12480 * Function: sd_shadow_buf_alloc 12481 * 12482 * Description: Allocate a buf(9S) and init it as per the given buf 12483 * and the various arguments. The associated sd_xbuf 12484 * struct is (nearly) duplicated. The struct buf *bp 12485 * argument is saved in new_xp->xb_private. 12486 * 12487 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12488 * datalen - size of data area for the shadow bp 12489 * bflags - B_READ or B_WRITE (pseudo flag) 12490 * blkno - starting LBA 12491 * func - function pointer for b_iodone in the shadow buf. (May 12492 * be NULL if none.) 12493 * 12494 * Return Code: Pointer to allocates buf(9S) struct 12495 * 12496 * Context: Can sleep. 12497 */ 12498 12499 static struct buf * 12500 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 12501 daddr_t blkno, int (*func)(struct buf *)) 12502 { 12503 struct sd_lun *un; 12504 struct sd_xbuf *xp; 12505 struct sd_xbuf *new_xp; 12506 struct buf *new_bp; 12507 12508 ASSERT(bp != NULL); 12509 xp = SD_GET_XBUF(bp); 12510 ASSERT(xp != NULL); 12511 un = SD_GET_UN(bp); 12512 ASSERT(un != NULL); 12513 ASSERT(!mutex_owned(SD_MUTEX(un))); 12514 12515 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 12516 bp_mapin(bp); 12517 } 12518 12519 bflags &= (B_READ | B_WRITE); 12520 #if defined(__i386) || defined(__amd64) 12521 new_bp = getrbuf(KM_SLEEP); 12522 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 12523 new_bp->b_bcount = datalen; 12524 new_bp->b_flags = bflags | 12525 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 12526 #else 12527 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 12528 datalen, bflags, SLEEP_FUNC, NULL); 12529 #endif 12530 new_bp->av_forw = NULL; 12531 new_bp->av_back = NULL; 12532 new_bp->b_dev = bp->b_dev; 12533 new_bp->b_blkno = blkno; 12534 new_bp->b_iodone = func; 12535 new_bp->b_edev = bp->b_edev; 12536 new_bp->b_resid = 0; 12537 12538 /* We need to preserve the B_FAILFAST flag */ 12539 if (bp->b_flags & B_FAILFAST) { 12540 new_bp->b_flags |= B_FAILFAST; 12541 } 12542 12543 /* 12544 * Allocate an xbuf for the shadow bp and copy the contents of the 12545 * original xbuf into it. 12546 */ 12547 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12548 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12549 12550 /* Need later to copy data between the shadow buf & original buf! */ 12551 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 12552 12553 /* 12554 * The given bp is automatically saved in the xb_private member 12555 * of the new xbuf. Callers are allowed to depend on this. 12556 */ 12557 new_xp->xb_private = bp; 12558 12559 new_bp->b_private = new_xp; 12560 12561 return (new_bp); 12562 } 12563 12564 /* 12565 * Function: sd_bioclone_free 12566 * 12567 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 12568 * in the larger than partition operation. 12569 * 12570 * Context: May be called under interrupt context 12571 */ 12572 12573 static void 12574 sd_bioclone_free(struct buf *bp) 12575 { 12576 struct sd_xbuf *xp; 12577 12578 ASSERT(bp != NULL); 12579 xp = SD_GET_XBUF(bp); 12580 ASSERT(xp != NULL); 12581 12582 /* 12583 * Call bp_mapout() before freeing the buf, in case a lower 12584 * layer or HBA had done a bp_mapin(). we must do this here 12585 * as we are the "originator" of the shadow buf. 12586 */ 12587 bp_mapout(bp); 12588 12589 /* 12590 * Null out b_iodone before freeing the bp, to ensure that the driver 12591 * never gets confused by a stale value in this field. (Just a little 12592 * extra defensiveness here.) 12593 */ 12594 bp->b_iodone = NULL; 12595 12596 freerbuf(bp); 12597 12598 kmem_free(xp, sizeof (struct sd_xbuf)); 12599 } 12600 12601 /* 12602 * Function: sd_shadow_buf_free 12603 * 12604 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 12605 * 12606 * Context: May be called under interrupt context 12607 */ 12608 12609 static void 12610 sd_shadow_buf_free(struct buf *bp) 12611 { 12612 struct sd_xbuf *xp; 12613 12614 ASSERT(bp != NULL); 12615 xp = SD_GET_XBUF(bp); 12616 ASSERT(xp != NULL); 12617 12618 #if defined(__sparc) 12619 /* 12620 * Call bp_mapout() before freeing the buf, in case a lower 12621 * layer or HBA had done a bp_mapin(). we must do this here 12622 * as we are the "originator" of the shadow buf. 12623 */ 12624 bp_mapout(bp); 12625 #endif 12626 12627 /* 12628 * Null out b_iodone before freeing the bp, to ensure that the driver 12629 * never gets confused by a stale value in this field. (Just a little 12630 * extra defensiveness here.) 12631 */ 12632 bp->b_iodone = NULL; 12633 12634 #if defined(__i386) || defined(__amd64) 12635 kmem_free(bp->b_un.b_addr, bp->b_bcount); 12636 freerbuf(bp); 12637 #else 12638 scsi_free_consistent_buf(bp); 12639 #endif 12640 12641 kmem_free(xp, sizeof (struct sd_xbuf)); 12642 } 12643 12644 12645 /* 12646 * Function: sd_print_transport_rejected_message 12647 * 12648 * Description: This implements the ludicrously complex rules for printing 12649 * a "transport rejected" message. This is to address the 12650 * specific problem of having a flood of this error message 12651 * produced when a failover occurs. 12652 * 12653 * Context: Any. 12654 */ 12655 12656 static void 12657 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 12658 int code) 12659 { 12660 ASSERT(un != NULL); 12661 ASSERT(mutex_owned(SD_MUTEX(un))); 12662 ASSERT(xp != NULL); 12663 12664 /* 12665 * Print the "transport rejected" message under the following 12666 * conditions: 12667 * 12668 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 12669 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 12670 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 12671 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 12672 * scsi_transport(9F) (which indicates that the target might have 12673 * gone off-line). This uses the un->un_tran_fatal_count 12674 * count, which is incremented whenever a TRAN_FATAL_ERROR is 12675 * received, and reset to zero whenver a TRAN_ACCEPT is returned 12676 * from scsi_transport(). 12677 * 12678 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 12679 * the preceeding cases in order for the message to be printed. 12680 */ 12681 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 12682 if ((sd_level_mask & SD_LOGMASK_DIAG) || 12683 (code != TRAN_FATAL_ERROR) || 12684 (un->un_tran_fatal_count == 1)) { 12685 switch (code) { 12686 case TRAN_BADPKT: 12687 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12688 "transport rejected bad packet\n"); 12689 break; 12690 case TRAN_FATAL_ERROR: 12691 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12692 "transport rejected fatal error\n"); 12693 break; 12694 default: 12695 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12696 "transport rejected (%d)\n", code); 12697 break; 12698 } 12699 } 12700 } 12701 } 12702 12703 12704 /* 12705 * Function: sd_add_buf_to_waitq 12706 * 12707 * Description: Add the given buf(9S) struct to the wait queue for the 12708 * instance. If sorting is enabled, then the buf is added 12709 * to the queue via an elevator sort algorithm (a la 12710 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 12711 * If sorting is not enabled, then the buf is just added 12712 * to the end of the wait queue. 12713 * 12714 * Return Code: void 12715 * 12716 * Context: Does not sleep/block, therefore technically can be called 12717 * from any context. However if sorting is enabled then the 12718 * execution time is indeterminate, and may take long if 12719 * the wait queue grows large. 12720 */ 12721 12722 static void 12723 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 12724 { 12725 struct buf *ap; 12726 12727 ASSERT(bp != NULL); 12728 ASSERT(un != NULL); 12729 ASSERT(mutex_owned(SD_MUTEX(un))); 12730 12731 /* If the queue is empty, add the buf as the only entry & return. */ 12732 if (un->un_waitq_headp == NULL) { 12733 ASSERT(un->un_waitq_tailp == NULL); 12734 un->un_waitq_headp = un->un_waitq_tailp = bp; 12735 bp->av_forw = NULL; 12736 return; 12737 } 12738 12739 ASSERT(un->un_waitq_tailp != NULL); 12740 12741 /* 12742 * If sorting is disabled, just add the buf to the tail end of 12743 * the wait queue and return. 12744 */ 12745 if (un->un_f_disksort_disabled) { 12746 un->un_waitq_tailp->av_forw = bp; 12747 un->un_waitq_tailp = bp; 12748 bp->av_forw = NULL; 12749 return; 12750 } 12751 12752 /* 12753 * Sort thru the list of requests currently on the wait queue 12754 * and add the new buf request at the appropriate position. 12755 * 12756 * The un->un_waitq_headp is an activity chain pointer on which 12757 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 12758 * first queue holds those requests which are positioned after 12759 * the current SD_GET_BLKNO() (in the first request); the second holds 12760 * requests which came in after their SD_GET_BLKNO() number was passed. 12761 * Thus we implement a one way scan, retracting after reaching 12762 * the end of the drive to the first request on the second 12763 * queue, at which time it becomes the first queue. 12764 * A one-way scan is natural because of the way UNIX read-ahead 12765 * blocks are allocated. 12766 * 12767 * If we lie after the first request, then we must locate the 12768 * second request list and add ourselves to it. 12769 */ 12770 ap = un->un_waitq_headp; 12771 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 12772 while (ap->av_forw != NULL) { 12773 /* 12774 * Look for an "inversion" in the (normally 12775 * ascending) block numbers. This indicates 12776 * the start of the second request list. 12777 */ 12778 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 12779 /* 12780 * Search the second request list for the 12781 * first request at a larger block number. 12782 * We go before that; however if there is 12783 * no such request, we go at the end. 12784 */ 12785 do { 12786 if (SD_GET_BLKNO(bp) < 12787 SD_GET_BLKNO(ap->av_forw)) { 12788 goto insert; 12789 } 12790 ap = ap->av_forw; 12791 } while (ap->av_forw != NULL); 12792 goto insert; /* after last */ 12793 } 12794 ap = ap->av_forw; 12795 } 12796 12797 /* 12798 * No inversions... we will go after the last, and 12799 * be the first request in the second request list. 12800 */ 12801 goto insert; 12802 } 12803 12804 /* 12805 * Request is at/after the current request... 12806 * sort in the first request list. 12807 */ 12808 while (ap->av_forw != NULL) { 12809 /* 12810 * We want to go after the current request (1) if 12811 * there is an inversion after it (i.e. it is the end 12812 * of the first request list), or (2) if the next 12813 * request is a larger block no. than our request. 12814 */ 12815 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 12816 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 12817 goto insert; 12818 } 12819 ap = ap->av_forw; 12820 } 12821 12822 /* 12823 * Neither a second list nor a larger request, therefore 12824 * we go at the end of the first list (which is the same 12825 * as the end of the whole schebang). 12826 */ 12827 insert: 12828 bp->av_forw = ap->av_forw; 12829 ap->av_forw = bp; 12830 12831 /* 12832 * If we inserted onto the tail end of the waitq, make sure the 12833 * tail pointer is updated. 12834 */ 12835 if (ap == un->un_waitq_tailp) { 12836 un->un_waitq_tailp = bp; 12837 } 12838 } 12839 12840 12841 /* 12842 * Function: sd_start_cmds 12843 * 12844 * Description: Remove and transport cmds from the driver queues. 12845 * 12846 * Arguments: un - pointer to the unit (soft state) struct for the target. 12847 * 12848 * immed_bp - ptr to a buf to be transported immediately. Only 12849 * the immed_bp is transported; bufs on the waitq are not 12850 * processed and the un_retry_bp is not checked. If immed_bp is 12851 * NULL, then normal queue processing is performed. 12852 * 12853 * Context: May be called from kernel thread context, interrupt context, 12854 * or runout callback context. This function may not block or 12855 * call routines that block. 12856 */ 12857 12858 static void 12859 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 12860 { 12861 struct sd_xbuf *xp; 12862 struct buf *bp; 12863 void (*statp)(kstat_io_t *); 12864 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12865 void (*saved_statp)(kstat_io_t *); 12866 #endif 12867 int rval; 12868 12869 ASSERT(un != NULL); 12870 ASSERT(mutex_owned(SD_MUTEX(un))); 12871 ASSERT(un->un_ncmds_in_transport >= 0); 12872 ASSERT(un->un_throttle >= 0); 12873 12874 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 12875 12876 do { 12877 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12878 saved_statp = NULL; 12879 #endif 12880 12881 /* 12882 * If we are syncing or dumping, fail the command to 12883 * avoid recursively calling back into scsi_transport(). 12884 * The dump I/O itself uses a separate code path so this 12885 * only prevents non-dump I/O from being sent while dumping. 12886 * File system sync takes place before dumping begins. 12887 * During panic, filesystem I/O is allowed provided 12888 * un_in_callback is <= 1. This is to prevent recursion 12889 * such as sd_start_cmds -> scsi_transport -> sdintr -> 12890 * sd_start_cmds and so on. See panic.c for more information 12891 * about the states the system can be in during panic. 12892 */ 12893 if ((un->un_state == SD_STATE_DUMPING) || 12894 (ddi_in_panic() && (un->un_in_callback > 1))) { 12895 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12896 "sd_start_cmds: panicking\n"); 12897 goto exit; 12898 } 12899 12900 if ((bp = immed_bp) != NULL) { 12901 /* 12902 * We have a bp that must be transported immediately. 12903 * It's OK to transport the immed_bp here without doing 12904 * the throttle limit check because the immed_bp is 12905 * always used in a retry/recovery case. This means 12906 * that we know we are not at the throttle limit by 12907 * virtue of the fact that to get here we must have 12908 * already gotten a command back via sdintr(). This also 12909 * relies on (1) the command on un_retry_bp preventing 12910 * further commands from the waitq from being issued; 12911 * and (2) the code in sd_retry_command checking the 12912 * throttle limit before issuing a delayed or immediate 12913 * retry. This holds even if the throttle limit is 12914 * currently ratcheted down from its maximum value. 12915 */ 12916 statp = kstat_runq_enter; 12917 if (bp == un->un_retry_bp) { 12918 ASSERT((un->un_retry_statp == NULL) || 12919 (un->un_retry_statp == kstat_waitq_enter) || 12920 (un->un_retry_statp == 12921 kstat_runq_back_to_waitq)); 12922 /* 12923 * If the waitq kstat was incremented when 12924 * sd_set_retry_bp() queued this bp for a retry, 12925 * then we must set up statp so that the waitq 12926 * count will get decremented correctly below. 12927 * Also we must clear un->un_retry_statp to 12928 * ensure that we do not act on a stale value 12929 * in this field. 12930 */ 12931 if ((un->un_retry_statp == kstat_waitq_enter) || 12932 (un->un_retry_statp == 12933 kstat_runq_back_to_waitq)) { 12934 statp = kstat_waitq_to_runq; 12935 } 12936 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12937 saved_statp = un->un_retry_statp; 12938 #endif 12939 un->un_retry_statp = NULL; 12940 12941 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 12942 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 12943 "un_throttle:%d un_ncmds_in_transport:%d\n", 12944 un, un->un_retry_bp, un->un_throttle, 12945 un->un_ncmds_in_transport); 12946 } else { 12947 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 12948 "processing priority bp:0x%p\n", bp); 12949 } 12950 12951 } else if ((bp = un->un_waitq_headp) != NULL) { 12952 /* 12953 * A command on the waitq is ready to go, but do not 12954 * send it if: 12955 * 12956 * (1) the throttle limit has been reached, or 12957 * (2) a retry is pending, or 12958 * (3) a START_STOP_UNIT callback pending, or 12959 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 12960 * command is pending. 12961 * 12962 * For all of these conditions, IO processing will 12963 * restart after the condition is cleared. 12964 */ 12965 if (un->un_ncmds_in_transport >= un->un_throttle) { 12966 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12967 "sd_start_cmds: exiting, " 12968 "throttle limit reached!\n"); 12969 goto exit; 12970 } 12971 if (un->un_retry_bp != NULL) { 12972 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12973 "sd_start_cmds: exiting, retry pending!\n"); 12974 goto exit; 12975 } 12976 if (un->un_startstop_timeid != NULL) { 12977 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12978 "sd_start_cmds: exiting, " 12979 "START_STOP pending!\n"); 12980 goto exit; 12981 } 12982 if (un->un_direct_priority_timeid != NULL) { 12983 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12984 "sd_start_cmds: exiting, " 12985 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 12986 goto exit; 12987 } 12988 12989 /* Dequeue the command */ 12990 un->un_waitq_headp = bp->av_forw; 12991 if (un->un_waitq_headp == NULL) { 12992 un->un_waitq_tailp = NULL; 12993 } 12994 bp->av_forw = NULL; 12995 statp = kstat_waitq_to_runq; 12996 SD_TRACE(SD_LOG_IO_CORE, un, 12997 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 12998 12999 } else { 13000 /* No work to do so bail out now */ 13001 SD_TRACE(SD_LOG_IO_CORE, un, 13002 "sd_start_cmds: no more work, exiting!\n"); 13003 goto exit; 13004 } 13005 13006 /* 13007 * Reset the state to normal. This is the mechanism by which 13008 * the state transitions from either SD_STATE_RWAIT or 13009 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 13010 * If state is SD_STATE_PM_CHANGING then this command is 13011 * part of the device power control and the state must 13012 * not be put back to normal. Doing so would would 13013 * allow new commands to proceed when they shouldn't, 13014 * the device may be going off. 13015 */ 13016 if ((un->un_state != SD_STATE_SUSPENDED) && 13017 (un->un_state != SD_STATE_PM_CHANGING)) { 13018 New_state(un, SD_STATE_NORMAL); 13019 } 13020 13021 xp = SD_GET_XBUF(bp); 13022 ASSERT(xp != NULL); 13023 13024 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13025 /* 13026 * Allocate the scsi_pkt if we need one, or attach DMA 13027 * resources if we have a scsi_pkt that needs them. The 13028 * latter should only occur for commands that are being 13029 * retried. 13030 */ 13031 if ((xp->xb_pktp == NULL) || 13032 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 13033 #else 13034 if (xp->xb_pktp == NULL) { 13035 #endif 13036 /* 13037 * There is no scsi_pkt allocated for this buf. Call 13038 * the initpkt function to allocate & init one. 13039 * 13040 * The scsi_init_pkt runout callback functionality is 13041 * implemented as follows: 13042 * 13043 * 1) The initpkt function always calls 13044 * scsi_init_pkt(9F) with sdrunout specified as the 13045 * callback routine. 13046 * 2) A successful packet allocation is initialized and 13047 * the I/O is transported. 13048 * 3) The I/O associated with an allocation resource 13049 * failure is left on its queue to be retried via 13050 * runout or the next I/O. 13051 * 4) The I/O associated with a DMA error is removed 13052 * from the queue and failed with EIO. Processing of 13053 * the transport queues is also halted to be 13054 * restarted via runout or the next I/O. 13055 * 5) The I/O associated with a CDB size or packet 13056 * size error is removed from the queue and failed 13057 * with EIO. Processing of the transport queues is 13058 * continued. 13059 * 13060 * Note: there is no interface for canceling a runout 13061 * callback. To prevent the driver from detaching or 13062 * suspending while a runout is pending the driver 13063 * state is set to SD_STATE_RWAIT 13064 * 13065 * Note: using the scsi_init_pkt callback facility can 13066 * result in an I/O request persisting at the head of 13067 * the list which cannot be satisfied even after 13068 * multiple retries. In the future the driver may 13069 * implement some kind of maximum runout count before 13070 * failing an I/O. 13071 * 13072 * Note: the use of funcp below may seem superfluous, 13073 * but it helps warlock figure out the correct 13074 * initpkt function calls (see [s]sd.wlcmd). 13075 */ 13076 struct scsi_pkt *pktp; 13077 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 13078 13079 ASSERT(bp != un->un_rqs_bp); 13080 13081 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 13082 switch ((*funcp)(bp, &pktp)) { 13083 case SD_PKT_ALLOC_SUCCESS: 13084 xp->xb_pktp = pktp; 13085 SD_TRACE(SD_LOG_IO_CORE, un, 13086 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 13087 pktp); 13088 goto got_pkt; 13089 13090 case SD_PKT_ALLOC_FAILURE: 13091 /* 13092 * Temporary (hopefully) resource depletion. 13093 * Since retries and RQS commands always have a 13094 * scsi_pkt allocated, these cases should never 13095 * get here. So the only cases this needs to 13096 * handle is a bp from the waitq (which we put 13097 * back onto the waitq for sdrunout), or a bp 13098 * sent as an immed_bp (which we just fail). 13099 */ 13100 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13101 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 13102 13103 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13104 13105 if (bp == immed_bp) { 13106 /* 13107 * If SD_XB_DMA_FREED is clear, then 13108 * this is a failure to allocate a 13109 * scsi_pkt, and we must fail the 13110 * command. 13111 */ 13112 if ((xp->xb_pkt_flags & 13113 SD_XB_DMA_FREED) == 0) { 13114 break; 13115 } 13116 13117 /* 13118 * If this immediate command is NOT our 13119 * un_retry_bp, then we must fail it. 13120 */ 13121 if (bp != un->un_retry_bp) { 13122 break; 13123 } 13124 13125 /* 13126 * We get here if this cmd is our 13127 * un_retry_bp that was DMAFREED, but 13128 * scsi_init_pkt() failed to reallocate 13129 * DMA resources when we attempted to 13130 * retry it. This can happen when an 13131 * mpxio failover is in progress, but 13132 * we don't want to just fail the 13133 * command in this case. 13134 * 13135 * Use timeout(9F) to restart it after 13136 * a 100ms delay. We don't want to 13137 * let sdrunout() restart it, because 13138 * sdrunout() is just supposed to start 13139 * commands that are sitting on the 13140 * wait queue. The un_retry_bp stays 13141 * set until the command completes, but 13142 * sdrunout can be called many times 13143 * before that happens. Since sdrunout 13144 * cannot tell if the un_retry_bp is 13145 * already in the transport, it could 13146 * end up calling scsi_transport() for 13147 * the un_retry_bp multiple times. 13148 * 13149 * Also: don't schedule the callback 13150 * if some other callback is already 13151 * pending. 13152 */ 13153 if (un->un_retry_statp == NULL) { 13154 /* 13155 * restore the kstat pointer to 13156 * keep kstat counts coherent 13157 * when we do retry the command. 13158 */ 13159 un->un_retry_statp = 13160 saved_statp; 13161 } 13162 13163 if ((un->un_startstop_timeid == NULL) && 13164 (un->un_retry_timeid == NULL) && 13165 (un->un_direct_priority_timeid == 13166 NULL)) { 13167 13168 un->un_retry_timeid = 13169 timeout( 13170 sd_start_retry_command, 13171 un, SD_RESTART_TIMEOUT); 13172 } 13173 goto exit; 13174 } 13175 13176 #else 13177 if (bp == immed_bp) { 13178 break; /* Just fail the command */ 13179 } 13180 #endif 13181 13182 /* Add the buf back to the head of the waitq */ 13183 bp->av_forw = un->un_waitq_headp; 13184 un->un_waitq_headp = bp; 13185 if (un->un_waitq_tailp == NULL) { 13186 un->un_waitq_tailp = bp; 13187 } 13188 goto exit; 13189 13190 case SD_PKT_ALLOC_FAILURE_NO_DMA: 13191 /* 13192 * HBA DMA resource failure. Fail the command 13193 * and continue processing of the queues. 13194 */ 13195 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13196 "sd_start_cmds: " 13197 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 13198 break; 13199 13200 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 13201 /* 13202 * Note:x86: Partial DMA mapping not supported 13203 * for USCSI commands, and all the needed DMA 13204 * resources were not allocated. 13205 */ 13206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13207 "sd_start_cmds: " 13208 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 13209 break; 13210 13211 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 13212 /* 13213 * Note:x86: Request cannot fit into CDB based 13214 * on lba and len. 13215 */ 13216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13217 "sd_start_cmds: " 13218 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 13219 break; 13220 13221 default: 13222 /* Should NEVER get here! */ 13223 panic("scsi_initpkt error"); 13224 /*NOTREACHED*/ 13225 } 13226 13227 /* 13228 * Fatal error in allocating a scsi_pkt for this buf. 13229 * Update kstats & return the buf with an error code. 13230 * We must use sd_return_failed_command_no_restart() to 13231 * avoid a recursive call back into sd_start_cmds(). 13232 * However this also means that we must keep processing 13233 * the waitq here in order to avoid stalling. 13234 */ 13235 if (statp == kstat_waitq_to_runq) { 13236 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 13237 } 13238 sd_return_failed_command_no_restart(un, bp, EIO); 13239 if (bp == immed_bp) { 13240 /* immed_bp is gone by now, so clear this */ 13241 immed_bp = NULL; 13242 } 13243 continue; 13244 } 13245 got_pkt: 13246 if (bp == immed_bp) { 13247 /* goto the head of the class.... */ 13248 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13249 } 13250 13251 un->un_ncmds_in_transport++; 13252 SD_UPDATE_KSTATS(un, statp, bp); 13253 13254 /* 13255 * Call scsi_transport() to send the command to the target. 13256 * According to SCSA architecture, we must drop the mutex here 13257 * before calling scsi_transport() in order to avoid deadlock. 13258 * Note that the scsi_pkt's completion routine can be executed 13259 * (from interrupt context) even before the call to 13260 * scsi_transport() returns. 13261 */ 13262 SD_TRACE(SD_LOG_IO_CORE, un, 13263 "sd_start_cmds: calling scsi_transport()\n"); 13264 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 13265 13266 mutex_exit(SD_MUTEX(un)); 13267 rval = scsi_transport(xp->xb_pktp); 13268 mutex_enter(SD_MUTEX(un)); 13269 13270 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13271 "sd_start_cmds: scsi_transport() returned %d\n", rval); 13272 13273 switch (rval) { 13274 case TRAN_ACCEPT: 13275 /* Clear this with every pkt accepted by the HBA */ 13276 un->un_tran_fatal_count = 0; 13277 break; /* Success; try the next cmd (if any) */ 13278 13279 case TRAN_BUSY: 13280 un->un_ncmds_in_transport--; 13281 ASSERT(un->un_ncmds_in_transport >= 0); 13282 13283 /* 13284 * Don't retry request sense, the sense data 13285 * is lost when another request is sent. 13286 * Free up the rqs buf and retry 13287 * the original failed cmd. Update kstat. 13288 */ 13289 if (bp == un->un_rqs_bp) { 13290 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13291 bp = sd_mark_rqs_idle(un, xp); 13292 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 13293 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 13294 kstat_waitq_enter); 13295 goto exit; 13296 } 13297 13298 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13299 /* 13300 * Free the DMA resources for the scsi_pkt. This will 13301 * allow mpxio to select another path the next time 13302 * we call scsi_transport() with this scsi_pkt. 13303 * See sdintr() for the rationalization behind this. 13304 */ 13305 if ((un->un_f_is_fibre == TRUE) && 13306 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 13307 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 13308 scsi_dmafree(xp->xb_pktp); 13309 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 13310 } 13311 #endif 13312 13313 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 13314 /* 13315 * Commands that are SD_PATH_DIRECT_PRIORITY 13316 * are for error recovery situations. These do 13317 * not use the normal command waitq, so if they 13318 * get a TRAN_BUSY we cannot put them back onto 13319 * the waitq for later retry. One possible 13320 * problem is that there could already be some 13321 * other command on un_retry_bp that is waiting 13322 * for this one to complete, so we would be 13323 * deadlocked if we put this command back onto 13324 * the waitq for later retry (since un_retry_bp 13325 * must complete before the driver gets back to 13326 * commands on the waitq). 13327 * 13328 * To avoid deadlock we must schedule a callback 13329 * that will restart this command after a set 13330 * interval. This should keep retrying for as 13331 * long as the underlying transport keeps 13332 * returning TRAN_BUSY (just like for other 13333 * commands). Use the same timeout interval as 13334 * for the ordinary TRAN_BUSY retry. 13335 */ 13336 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13337 "sd_start_cmds: scsi_transport() returned " 13338 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 13339 13340 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13341 un->un_direct_priority_timeid = 13342 timeout(sd_start_direct_priority_command, 13343 bp, SD_BSY_TIMEOUT / 500); 13344 13345 goto exit; 13346 } 13347 13348 /* 13349 * For TRAN_BUSY, we want to reduce the throttle value, 13350 * unless we are retrying a command. 13351 */ 13352 if (bp != un->un_retry_bp) { 13353 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 13354 } 13355 13356 /* 13357 * Set up the bp to be tried again 10 ms later. 13358 * Note:x86: Is there a timeout value in the sd_lun 13359 * for this condition? 13360 */ 13361 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 13362 kstat_runq_back_to_waitq); 13363 goto exit; 13364 13365 case TRAN_FATAL_ERROR: 13366 un->un_tran_fatal_count++; 13367 /* FALLTHRU */ 13368 13369 case TRAN_BADPKT: 13370 default: 13371 un->un_ncmds_in_transport--; 13372 ASSERT(un->un_ncmds_in_transport >= 0); 13373 13374 /* 13375 * If this is our REQUEST SENSE command with a 13376 * transport error, we must get back the pointers 13377 * to the original buf, and mark the REQUEST 13378 * SENSE command as "available". 13379 */ 13380 if (bp == un->un_rqs_bp) { 13381 bp = sd_mark_rqs_idle(un, xp); 13382 xp = SD_GET_XBUF(bp); 13383 } else { 13384 /* 13385 * Legacy behavior: do not update transport 13386 * error count for request sense commands. 13387 */ 13388 SD_UPDATE_ERRSTATS(un, sd_transerrs); 13389 } 13390 13391 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13392 sd_print_transport_rejected_message(un, xp, rval); 13393 13394 /* 13395 * We must use sd_return_failed_command_no_restart() to 13396 * avoid a recursive call back into sd_start_cmds(). 13397 * However this also means that we must keep processing 13398 * the waitq here in order to avoid stalling. 13399 */ 13400 sd_return_failed_command_no_restart(un, bp, EIO); 13401 13402 /* 13403 * Notify any threads waiting in sd_ddi_suspend() that 13404 * a command completion has occurred. 13405 */ 13406 if (un->un_state == SD_STATE_SUSPENDED) { 13407 cv_broadcast(&un->un_disk_busy_cv); 13408 } 13409 13410 if (bp == immed_bp) { 13411 /* immed_bp is gone by now, so clear this */ 13412 immed_bp = NULL; 13413 } 13414 break; 13415 } 13416 13417 } while (immed_bp == NULL); 13418 13419 exit: 13420 ASSERT(mutex_owned(SD_MUTEX(un))); 13421 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 13422 } 13423 13424 13425 /* 13426 * Function: sd_return_command 13427 * 13428 * Description: Returns a command to its originator (with or without an 13429 * error). Also starts commands waiting to be transported 13430 * to the target. 13431 * 13432 * Context: May be called from interrupt, kernel, or timeout context 13433 */ 13434 13435 static void 13436 sd_return_command(struct sd_lun *un, struct buf *bp) 13437 { 13438 struct sd_xbuf *xp; 13439 struct scsi_pkt *pktp; 13440 13441 ASSERT(bp != NULL); 13442 ASSERT(un != NULL); 13443 ASSERT(mutex_owned(SD_MUTEX(un))); 13444 ASSERT(bp != un->un_rqs_bp); 13445 xp = SD_GET_XBUF(bp); 13446 ASSERT(xp != NULL); 13447 13448 pktp = SD_GET_PKTP(bp); 13449 13450 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 13451 13452 /* 13453 * Note: check for the "sdrestart failed" case. 13454 */ 13455 if ((un->un_partial_dma_supported == 1) && 13456 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 13457 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 13458 (xp->xb_pktp->pkt_resid == 0)) { 13459 13460 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 13461 /* 13462 * Successfully set up next portion of cmd 13463 * transfer, try sending it 13464 */ 13465 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 13466 NULL, NULL, 0, (clock_t)0, NULL); 13467 sd_start_cmds(un, NULL); 13468 return; /* Note:x86: need a return here? */ 13469 } 13470 } 13471 13472 /* 13473 * If this is the failfast bp, clear it from un_failfast_bp. This 13474 * can happen if upon being re-tried the failfast bp either 13475 * succeeded or encountered another error (possibly even a different 13476 * error than the one that precipitated the failfast state, but in 13477 * that case it would have had to exhaust retries as well). Regardless, 13478 * this should not occur whenever the instance is in the active 13479 * failfast state. 13480 */ 13481 if (bp == un->un_failfast_bp) { 13482 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13483 un->un_failfast_bp = NULL; 13484 } 13485 13486 /* 13487 * Clear the failfast state upon successful completion of ANY cmd. 13488 */ 13489 if (bp->b_error == 0) { 13490 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13491 } 13492 13493 /* 13494 * This is used if the command was retried one or more times. Show that 13495 * we are done with it, and allow processing of the waitq to resume. 13496 */ 13497 if (bp == un->un_retry_bp) { 13498 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13499 "sd_return_command: un:0x%p: " 13500 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13501 un->un_retry_bp = NULL; 13502 un->un_retry_statp = NULL; 13503 } 13504 13505 SD_UPDATE_RDWR_STATS(un, bp); 13506 SD_UPDATE_PARTITION_STATS(un, bp); 13507 13508 switch (un->un_state) { 13509 case SD_STATE_SUSPENDED: 13510 /* 13511 * Notify any threads waiting in sd_ddi_suspend() that 13512 * a command completion has occurred. 13513 */ 13514 cv_broadcast(&un->un_disk_busy_cv); 13515 break; 13516 default: 13517 sd_start_cmds(un, NULL); 13518 break; 13519 } 13520 13521 /* Return this command up the iodone chain to its originator. */ 13522 mutex_exit(SD_MUTEX(un)); 13523 13524 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13525 xp->xb_pktp = NULL; 13526 13527 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13528 13529 ASSERT(!mutex_owned(SD_MUTEX(un))); 13530 mutex_enter(SD_MUTEX(un)); 13531 13532 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 13533 } 13534 13535 13536 /* 13537 * Function: sd_return_failed_command 13538 * 13539 * Description: Command completion when an error occurred. 13540 * 13541 * Context: May be called from interrupt context 13542 */ 13543 13544 static void 13545 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 13546 { 13547 ASSERT(bp != NULL); 13548 ASSERT(un != NULL); 13549 ASSERT(mutex_owned(SD_MUTEX(un))); 13550 13551 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13552 "sd_return_failed_command: entry\n"); 13553 13554 /* 13555 * b_resid could already be nonzero due to a partial data 13556 * transfer, so do not change it here. 13557 */ 13558 SD_BIOERROR(bp, errcode); 13559 13560 sd_return_command(un, bp); 13561 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13562 "sd_return_failed_command: exit\n"); 13563 } 13564 13565 13566 /* 13567 * Function: sd_return_failed_command_no_restart 13568 * 13569 * Description: Same as sd_return_failed_command, but ensures that no 13570 * call back into sd_start_cmds will be issued. 13571 * 13572 * Context: May be called from interrupt context 13573 */ 13574 13575 static void 13576 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 13577 int errcode) 13578 { 13579 struct sd_xbuf *xp; 13580 13581 ASSERT(bp != NULL); 13582 ASSERT(un != NULL); 13583 ASSERT(mutex_owned(SD_MUTEX(un))); 13584 xp = SD_GET_XBUF(bp); 13585 ASSERT(xp != NULL); 13586 ASSERT(errcode != 0); 13587 13588 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13589 "sd_return_failed_command_no_restart: entry\n"); 13590 13591 /* 13592 * b_resid could already be nonzero due to a partial data 13593 * transfer, so do not change it here. 13594 */ 13595 SD_BIOERROR(bp, errcode); 13596 13597 /* 13598 * If this is the failfast bp, clear it. This can happen if the 13599 * failfast bp encounterd a fatal error when we attempted to 13600 * re-try it (such as a scsi_transport(9F) failure). However 13601 * we should NOT be in an active failfast state if the failfast 13602 * bp is not NULL. 13603 */ 13604 if (bp == un->un_failfast_bp) { 13605 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13606 un->un_failfast_bp = NULL; 13607 } 13608 13609 if (bp == un->un_retry_bp) { 13610 /* 13611 * This command was retried one or more times. Show that we are 13612 * done with it, and allow processing of the waitq to resume. 13613 */ 13614 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13615 "sd_return_failed_command_no_restart: " 13616 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13617 un->un_retry_bp = NULL; 13618 un->un_retry_statp = NULL; 13619 } 13620 13621 SD_UPDATE_RDWR_STATS(un, bp); 13622 SD_UPDATE_PARTITION_STATS(un, bp); 13623 13624 mutex_exit(SD_MUTEX(un)); 13625 13626 if (xp->xb_pktp != NULL) { 13627 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13628 xp->xb_pktp = NULL; 13629 } 13630 13631 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13632 13633 mutex_enter(SD_MUTEX(un)); 13634 13635 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13636 "sd_return_failed_command_no_restart: exit\n"); 13637 } 13638 13639 13640 /* 13641 * Function: sd_retry_command 13642 * 13643 * Description: queue up a command for retry, or (optionally) fail it 13644 * if retry counts are exhausted. 13645 * 13646 * Arguments: un - Pointer to the sd_lun struct for the target. 13647 * 13648 * bp - Pointer to the buf for the command to be retried. 13649 * 13650 * retry_check_flag - Flag to see which (if any) of the retry 13651 * counts should be decremented/checked. If the indicated 13652 * retry count is exhausted, then the command will not be 13653 * retried; it will be failed instead. This should use a 13654 * value equal to one of the following: 13655 * 13656 * SD_RETRIES_NOCHECK 13657 * SD_RESD_RETRIES_STANDARD 13658 * SD_RETRIES_VICTIM 13659 * 13660 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 13661 * if the check should be made to see of FLAG_ISOLATE is set 13662 * in the pkt. If FLAG_ISOLATE is set, then the command is 13663 * not retried, it is simply failed. 13664 * 13665 * user_funcp - Ptr to function to call before dispatching the 13666 * command. May be NULL if no action needs to be performed. 13667 * (Primarily intended for printing messages.) 13668 * 13669 * user_arg - Optional argument to be passed along to 13670 * the user_funcp call. 13671 * 13672 * failure_code - errno return code to set in the bp if the 13673 * command is going to be failed. 13674 * 13675 * retry_delay - Retry delay interval in (clock_t) units. May 13676 * be zero which indicates that the retry should be retried 13677 * immediately (ie, without an intervening delay). 13678 * 13679 * statp - Ptr to kstat function to be updated if the command 13680 * is queued for a delayed retry. May be NULL if no kstat 13681 * update is desired. 13682 * 13683 * Context: May be called from interrupt context. 13684 */ 13685 13686 static void 13687 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 13688 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 13689 code), void *user_arg, int failure_code, clock_t retry_delay, 13690 void (*statp)(kstat_io_t *)) 13691 { 13692 struct sd_xbuf *xp; 13693 struct scsi_pkt *pktp; 13694 13695 ASSERT(un != NULL); 13696 ASSERT(mutex_owned(SD_MUTEX(un))); 13697 ASSERT(bp != NULL); 13698 xp = SD_GET_XBUF(bp); 13699 ASSERT(xp != NULL); 13700 pktp = SD_GET_PKTP(bp); 13701 ASSERT(pktp != NULL); 13702 13703 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 13704 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 13705 13706 /* 13707 * If we are syncing or dumping, fail the command to avoid 13708 * recursively calling back into scsi_transport(). 13709 */ 13710 if (ddi_in_panic()) { 13711 goto fail_command_no_log; 13712 } 13713 13714 /* 13715 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 13716 * log an error and fail the command. 13717 */ 13718 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 13719 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 13720 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 13721 sd_dump_memory(un, SD_LOG_IO, "CDB", 13722 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 13723 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 13724 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 13725 goto fail_command; 13726 } 13727 13728 /* 13729 * If we are suspended, then put the command onto head of the 13730 * wait queue since we don't want to start more commands, and 13731 * clear the un_retry_bp. Next time when we are resumed, will 13732 * handle the command in the wait queue. 13733 */ 13734 switch (un->un_state) { 13735 case SD_STATE_SUSPENDED: 13736 case SD_STATE_DUMPING: 13737 bp->av_forw = un->un_waitq_headp; 13738 un->un_waitq_headp = bp; 13739 if (un->un_waitq_tailp == NULL) { 13740 un->un_waitq_tailp = bp; 13741 } 13742 if (bp == un->un_retry_bp) { 13743 un->un_retry_bp = NULL; 13744 un->un_retry_statp = NULL; 13745 } 13746 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13747 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 13748 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 13749 return; 13750 default: 13751 break; 13752 } 13753 13754 /* 13755 * If the caller wants us to check FLAG_ISOLATE, then see if that 13756 * is set; if it is then we do not want to retry the command. 13757 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 13758 */ 13759 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 13760 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 13761 goto fail_command; 13762 } 13763 } 13764 13765 13766 /* 13767 * If SD_RETRIES_FAILFAST is set, it indicates that either a 13768 * command timeout or a selection timeout has occurred. This means 13769 * that we were unable to establish an kind of communication with 13770 * the target, and subsequent retries and/or commands are likely 13771 * to encounter similar results and take a long time to complete. 13772 * 13773 * If this is a failfast error condition, we need to update the 13774 * failfast state, even if this bp does not have B_FAILFAST set. 13775 */ 13776 if (retry_check_flag & SD_RETRIES_FAILFAST) { 13777 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 13778 ASSERT(un->un_failfast_bp == NULL); 13779 /* 13780 * If we are already in the active failfast state, and 13781 * another failfast error condition has been detected, 13782 * then fail this command if it has B_FAILFAST set. 13783 * If B_FAILFAST is clear, then maintain the legacy 13784 * behavior of retrying heroically, even tho this will 13785 * take a lot more time to fail the command. 13786 */ 13787 if (bp->b_flags & B_FAILFAST) { 13788 goto fail_command; 13789 } 13790 } else { 13791 /* 13792 * We're not in the active failfast state, but we 13793 * have a failfast error condition, so we must begin 13794 * transition to the next state. We do this regardless 13795 * of whether or not this bp has B_FAILFAST set. 13796 */ 13797 if (un->un_failfast_bp == NULL) { 13798 /* 13799 * This is the first bp to meet a failfast 13800 * condition so save it on un_failfast_bp & 13801 * do normal retry processing. Do not enter 13802 * active failfast state yet. This marks 13803 * entry into the "failfast pending" state. 13804 */ 13805 un->un_failfast_bp = bp; 13806 13807 } else if (un->un_failfast_bp == bp) { 13808 /* 13809 * This is the second time *this* bp has 13810 * encountered a failfast error condition, 13811 * so enter active failfast state & flush 13812 * queues as appropriate. 13813 */ 13814 un->un_failfast_state = SD_FAILFAST_ACTIVE; 13815 un->un_failfast_bp = NULL; 13816 sd_failfast_flushq(un); 13817 13818 /* 13819 * Fail this bp now if B_FAILFAST set; 13820 * otherwise continue with retries. (It would 13821 * be pretty ironic if this bp succeeded on a 13822 * subsequent retry after we just flushed all 13823 * the queues). 13824 */ 13825 if (bp->b_flags & B_FAILFAST) { 13826 goto fail_command; 13827 } 13828 13829 #if !defined(lint) && !defined(__lint) 13830 } else { 13831 /* 13832 * If neither of the preceeding conditionals 13833 * was true, it means that there is some 13834 * *other* bp that has met an inital failfast 13835 * condition and is currently either being 13836 * retried or is waiting to be retried. In 13837 * that case we should perform normal retry 13838 * processing on *this* bp, since there is a 13839 * chance that the current failfast condition 13840 * is transient and recoverable. If that does 13841 * not turn out to be the case, then retries 13842 * will be cleared when the wait queue is 13843 * flushed anyway. 13844 */ 13845 #endif 13846 } 13847 } 13848 } else { 13849 /* 13850 * SD_RETRIES_FAILFAST is clear, which indicates that we 13851 * likely were able to at least establish some level of 13852 * communication with the target and subsequent commands 13853 * and/or retries are likely to get through to the target, 13854 * In this case we want to be aggressive about clearing 13855 * the failfast state. Note that this does not affect 13856 * the "failfast pending" condition. 13857 */ 13858 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13859 } 13860 13861 13862 /* 13863 * Check the specified retry count to see if we can still do 13864 * any retries with this pkt before we should fail it. 13865 */ 13866 switch (retry_check_flag & SD_RETRIES_MASK) { 13867 case SD_RETRIES_VICTIM: 13868 /* 13869 * Check the victim retry count. If exhausted, then fall 13870 * thru & check against the standard retry count. 13871 */ 13872 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 13873 /* Increment count & proceed with the retry */ 13874 xp->xb_victim_retry_count++; 13875 break; 13876 } 13877 /* Victim retries exhausted, fall back to std. retries... */ 13878 /* FALLTHRU */ 13879 13880 case SD_RETRIES_STANDARD: 13881 if (xp->xb_retry_count >= un->un_retry_count) { 13882 /* Retries exhausted, fail the command */ 13883 SD_TRACE(SD_LOG_IO_CORE, un, 13884 "sd_retry_command: retries exhausted!\n"); 13885 /* 13886 * update b_resid for failed SCMD_READ & SCMD_WRITE 13887 * commands with nonzero pkt_resid. 13888 */ 13889 if ((pktp->pkt_reason == CMD_CMPLT) && 13890 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 13891 (pktp->pkt_resid != 0)) { 13892 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 13893 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 13894 SD_UPDATE_B_RESID(bp, pktp); 13895 } 13896 } 13897 goto fail_command; 13898 } 13899 xp->xb_retry_count++; 13900 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13901 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13902 break; 13903 13904 case SD_RETRIES_UA: 13905 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 13906 /* Retries exhausted, fail the command */ 13907 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13908 "Unit Attention retries exhausted. " 13909 "Check the target.\n"); 13910 goto fail_command; 13911 } 13912 xp->xb_ua_retry_count++; 13913 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13914 "sd_retry_command: retry count:%d\n", 13915 xp->xb_ua_retry_count); 13916 break; 13917 13918 case SD_RETRIES_BUSY: 13919 if (xp->xb_retry_count >= un->un_busy_retry_count) { 13920 /* Retries exhausted, fail the command */ 13921 SD_TRACE(SD_LOG_IO_CORE, un, 13922 "sd_retry_command: retries exhausted!\n"); 13923 goto fail_command; 13924 } 13925 xp->xb_retry_count++; 13926 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13927 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13928 break; 13929 13930 case SD_RETRIES_NOCHECK: 13931 default: 13932 /* No retry count to check. Just proceed with the retry */ 13933 break; 13934 } 13935 13936 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13937 13938 /* 13939 * If we were given a zero timeout, we must attempt to retry the 13940 * command immediately (ie, without a delay). 13941 */ 13942 if (retry_delay == 0) { 13943 /* 13944 * Check some limiting conditions to see if we can actually 13945 * do the immediate retry. If we cannot, then we must 13946 * fall back to queueing up a delayed retry. 13947 */ 13948 if (un->un_ncmds_in_transport >= un->un_throttle) { 13949 /* 13950 * We are at the throttle limit for the target, 13951 * fall back to delayed retry. 13952 */ 13953 retry_delay = SD_BSY_TIMEOUT; 13954 statp = kstat_waitq_enter; 13955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13956 "sd_retry_command: immed. retry hit " 13957 "throttle!\n"); 13958 } else { 13959 /* 13960 * We're clear to proceed with the immediate retry. 13961 * First call the user-provided function (if any) 13962 */ 13963 if (user_funcp != NULL) { 13964 (*user_funcp)(un, bp, user_arg, 13965 SD_IMMEDIATE_RETRY_ISSUED); 13966 #ifdef __lock_lint 13967 sd_print_incomplete_msg(un, bp, user_arg, 13968 SD_IMMEDIATE_RETRY_ISSUED); 13969 sd_print_cmd_incomplete_msg(un, bp, user_arg, 13970 SD_IMMEDIATE_RETRY_ISSUED); 13971 sd_print_sense_failed_msg(un, bp, user_arg, 13972 SD_IMMEDIATE_RETRY_ISSUED); 13973 #endif 13974 } 13975 13976 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13977 "sd_retry_command: issuing immediate retry\n"); 13978 13979 /* 13980 * Call sd_start_cmds() to transport the command to 13981 * the target. 13982 */ 13983 sd_start_cmds(un, bp); 13984 13985 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13986 "sd_retry_command exit\n"); 13987 return; 13988 } 13989 } 13990 13991 /* 13992 * Set up to retry the command after a delay. 13993 * First call the user-provided function (if any) 13994 */ 13995 if (user_funcp != NULL) { 13996 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 13997 } 13998 13999 sd_set_retry_bp(un, bp, retry_delay, statp); 14000 14001 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 14002 return; 14003 14004 fail_command: 14005 14006 if (user_funcp != NULL) { 14007 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 14008 } 14009 14010 fail_command_no_log: 14011 14012 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14013 "sd_retry_command: returning failed command\n"); 14014 14015 sd_return_failed_command(un, bp, failure_code); 14016 14017 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 14018 } 14019 14020 14021 /* 14022 * Function: sd_set_retry_bp 14023 * 14024 * Description: Set up the given bp for retry. 14025 * 14026 * Arguments: un - ptr to associated softstate 14027 * bp - ptr to buf(9S) for the command 14028 * retry_delay - time interval before issuing retry (may be 0) 14029 * statp - optional pointer to kstat function 14030 * 14031 * Context: May be called under interrupt context 14032 */ 14033 14034 static void 14035 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 14036 void (*statp)(kstat_io_t *)) 14037 { 14038 ASSERT(un != NULL); 14039 ASSERT(mutex_owned(SD_MUTEX(un))); 14040 ASSERT(bp != NULL); 14041 14042 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14043 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 14044 14045 /* 14046 * Indicate that the command is being retried. This will not allow any 14047 * other commands on the wait queue to be transported to the target 14048 * until this command has been completed (success or failure). The 14049 * "retry command" is not transported to the target until the given 14050 * time delay expires, unless the user specified a 0 retry_delay. 14051 * 14052 * Note: the timeout(9F) callback routine is what actually calls 14053 * sd_start_cmds() to transport the command, with the exception of a 14054 * zero retry_delay. The only current implementor of a zero retry delay 14055 * is the case where a START_STOP_UNIT is sent to spin-up a device. 14056 */ 14057 if (un->un_retry_bp == NULL) { 14058 ASSERT(un->un_retry_statp == NULL); 14059 un->un_retry_bp = bp; 14060 14061 /* 14062 * If the user has not specified a delay the command should 14063 * be queued and no timeout should be scheduled. 14064 */ 14065 if (retry_delay == 0) { 14066 /* 14067 * Save the kstat pointer that will be used in the 14068 * call to SD_UPDATE_KSTATS() below, so that 14069 * sd_start_cmds() can correctly decrement the waitq 14070 * count when it is time to transport this command. 14071 */ 14072 un->un_retry_statp = statp; 14073 goto done; 14074 } 14075 } 14076 14077 if (un->un_retry_bp == bp) { 14078 /* 14079 * Save the kstat pointer that will be used in the call to 14080 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 14081 * correctly decrement the waitq count when it is time to 14082 * transport this command. 14083 */ 14084 un->un_retry_statp = statp; 14085 14086 /* 14087 * Schedule a timeout if: 14088 * 1) The user has specified a delay. 14089 * 2) There is not a START_STOP_UNIT callback pending. 14090 * 14091 * If no delay has been specified, then it is up to the caller 14092 * to ensure that IO processing continues without stalling. 14093 * Effectively, this means that the caller will issue the 14094 * required call to sd_start_cmds(). The START_STOP_UNIT 14095 * callback does this after the START STOP UNIT command has 14096 * completed. In either of these cases we should not schedule 14097 * a timeout callback here. Also don't schedule the timeout if 14098 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 14099 */ 14100 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 14101 (un->un_direct_priority_timeid == NULL)) { 14102 un->un_retry_timeid = 14103 timeout(sd_start_retry_command, un, retry_delay); 14104 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14105 "sd_set_retry_bp: setting timeout: un: 0x%p" 14106 " bp:0x%p un_retry_timeid:0x%p\n", 14107 un, bp, un->un_retry_timeid); 14108 } 14109 } else { 14110 /* 14111 * We only get in here if there is already another command 14112 * waiting to be retried. In this case, we just put the 14113 * given command onto the wait queue, so it can be transported 14114 * after the current retry command has completed. 14115 * 14116 * Also we have to make sure that if the command at the head 14117 * of the wait queue is the un_failfast_bp, that we do not 14118 * put ahead of it any other commands that are to be retried. 14119 */ 14120 if ((un->un_failfast_bp != NULL) && 14121 (un->un_failfast_bp == un->un_waitq_headp)) { 14122 /* 14123 * Enqueue this command AFTER the first command on 14124 * the wait queue (which is also un_failfast_bp). 14125 */ 14126 bp->av_forw = un->un_waitq_headp->av_forw; 14127 un->un_waitq_headp->av_forw = bp; 14128 if (un->un_waitq_headp == un->un_waitq_tailp) { 14129 un->un_waitq_tailp = bp; 14130 } 14131 } else { 14132 /* Enqueue this command at the head of the waitq. */ 14133 bp->av_forw = un->un_waitq_headp; 14134 un->un_waitq_headp = bp; 14135 if (un->un_waitq_tailp == NULL) { 14136 un->un_waitq_tailp = bp; 14137 } 14138 } 14139 14140 if (statp == NULL) { 14141 statp = kstat_waitq_enter; 14142 } 14143 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14144 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 14145 } 14146 14147 done: 14148 if (statp != NULL) { 14149 SD_UPDATE_KSTATS(un, statp, bp); 14150 } 14151 14152 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14153 "sd_set_retry_bp: exit un:0x%p\n", un); 14154 } 14155 14156 14157 /* 14158 * Function: sd_start_retry_command 14159 * 14160 * Description: Start the command that has been waiting on the target's 14161 * retry queue. Called from timeout(9F) context after the 14162 * retry delay interval has expired. 14163 * 14164 * Arguments: arg - pointer to associated softstate for the device. 14165 * 14166 * Context: timeout(9F) thread context. May not sleep. 14167 */ 14168 14169 static void 14170 sd_start_retry_command(void *arg) 14171 { 14172 struct sd_lun *un = arg; 14173 14174 ASSERT(un != NULL); 14175 ASSERT(!mutex_owned(SD_MUTEX(un))); 14176 14177 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14178 "sd_start_retry_command: entry\n"); 14179 14180 mutex_enter(SD_MUTEX(un)); 14181 14182 un->un_retry_timeid = NULL; 14183 14184 if (un->un_retry_bp != NULL) { 14185 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14186 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 14187 un, un->un_retry_bp); 14188 sd_start_cmds(un, un->un_retry_bp); 14189 } 14190 14191 mutex_exit(SD_MUTEX(un)); 14192 14193 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14194 "sd_start_retry_command: exit\n"); 14195 } 14196 14197 14198 /* 14199 * Function: sd_start_direct_priority_command 14200 * 14201 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 14202 * received TRAN_BUSY when we called scsi_transport() to send it 14203 * to the underlying HBA. This function is called from timeout(9F) 14204 * context after the delay interval has expired. 14205 * 14206 * Arguments: arg - pointer to associated buf(9S) to be restarted. 14207 * 14208 * Context: timeout(9F) thread context. May not sleep. 14209 */ 14210 14211 static void 14212 sd_start_direct_priority_command(void *arg) 14213 { 14214 struct buf *priority_bp = arg; 14215 struct sd_lun *un; 14216 14217 ASSERT(priority_bp != NULL); 14218 un = SD_GET_UN(priority_bp); 14219 ASSERT(un != NULL); 14220 ASSERT(!mutex_owned(SD_MUTEX(un))); 14221 14222 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14223 "sd_start_direct_priority_command: entry\n"); 14224 14225 mutex_enter(SD_MUTEX(un)); 14226 un->un_direct_priority_timeid = NULL; 14227 sd_start_cmds(un, priority_bp); 14228 mutex_exit(SD_MUTEX(un)); 14229 14230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14231 "sd_start_direct_priority_command: exit\n"); 14232 } 14233 14234 14235 /* 14236 * Function: sd_send_request_sense_command 14237 * 14238 * Description: Sends a REQUEST SENSE command to the target 14239 * 14240 * Context: May be called from interrupt context. 14241 */ 14242 14243 static void 14244 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 14245 struct scsi_pkt *pktp) 14246 { 14247 ASSERT(bp != NULL); 14248 ASSERT(un != NULL); 14249 ASSERT(mutex_owned(SD_MUTEX(un))); 14250 14251 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 14252 "entry: buf:0x%p\n", bp); 14253 14254 /* 14255 * If we are syncing or dumping, then fail the command to avoid a 14256 * recursive callback into scsi_transport(). Also fail the command 14257 * if we are suspended (legacy behavior). 14258 */ 14259 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 14260 (un->un_state == SD_STATE_DUMPING)) { 14261 sd_return_failed_command(un, bp, EIO); 14262 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14263 "sd_send_request_sense_command: syncing/dumping, exit\n"); 14264 return; 14265 } 14266 14267 /* 14268 * Retry the failed command and don't issue the request sense if: 14269 * 1) the sense buf is busy 14270 * 2) we have 1 or more outstanding commands on the target 14271 * (the sense data will be cleared or invalidated any way) 14272 * 14273 * Note: There could be an issue with not checking a retry limit here, 14274 * the problem is determining which retry limit to check. 14275 */ 14276 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 14277 /* Don't retry if the command is flagged as non-retryable */ 14278 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 14279 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14280 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 14281 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14282 "sd_send_request_sense_command: " 14283 "at full throttle, retrying exit\n"); 14284 } else { 14285 sd_return_failed_command(un, bp, EIO); 14286 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14287 "sd_send_request_sense_command: " 14288 "at full throttle, non-retryable exit\n"); 14289 } 14290 return; 14291 } 14292 14293 sd_mark_rqs_busy(un, bp); 14294 sd_start_cmds(un, un->un_rqs_bp); 14295 14296 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14297 "sd_send_request_sense_command: exit\n"); 14298 } 14299 14300 14301 /* 14302 * Function: sd_mark_rqs_busy 14303 * 14304 * Description: Indicate that the request sense bp for this instance is 14305 * in use. 14306 * 14307 * Context: May be called under interrupt context 14308 */ 14309 14310 static void 14311 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 14312 { 14313 struct sd_xbuf *sense_xp; 14314 14315 ASSERT(un != NULL); 14316 ASSERT(bp != NULL); 14317 ASSERT(mutex_owned(SD_MUTEX(un))); 14318 ASSERT(un->un_sense_isbusy == 0); 14319 14320 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 14321 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 14322 14323 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 14324 ASSERT(sense_xp != NULL); 14325 14326 SD_INFO(SD_LOG_IO, un, 14327 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 14328 14329 ASSERT(sense_xp->xb_pktp != NULL); 14330 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 14331 == (FLAG_SENSING | FLAG_HEAD)); 14332 14333 un->un_sense_isbusy = 1; 14334 un->un_rqs_bp->b_resid = 0; 14335 sense_xp->xb_pktp->pkt_resid = 0; 14336 sense_xp->xb_pktp->pkt_reason = 0; 14337 14338 /* So we can get back the bp at interrupt time! */ 14339 sense_xp->xb_sense_bp = bp; 14340 14341 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 14342 14343 /* 14344 * Mark this buf as awaiting sense data. (This is already set in 14345 * the pkt_flags for the RQS packet.) 14346 */ 14347 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 14348 14349 sense_xp->xb_retry_count = 0; 14350 sense_xp->xb_victim_retry_count = 0; 14351 sense_xp->xb_ua_retry_count = 0; 14352 sense_xp->xb_nr_retry_count = 0; 14353 sense_xp->xb_dma_resid = 0; 14354 14355 /* Clean up the fields for auto-request sense */ 14356 sense_xp->xb_sense_status = 0; 14357 sense_xp->xb_sense_state = 0; 14358 sense_xp->xb_sense_resid = 0; 14359 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 14360 14361 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 14362 } 14363 14364 14365 /* 14366 * Function: sd_mark_rqs_idle 14367 * 14368 * Description: SD_MUTEX must be held continuously through this routine 14369 * to prevent reuse of the rqs struct before the caller can 14370 * complete it's processing. 14371 * 14372 * Return Code: Pointer to the RQS buf 14373 * 14374 * Context: May be called under interrupt context 14375 */ 14376 14377 static struct buf * 14378 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 14379 { 14380 struct buf *bp; 14381 ASSERT(un != NULL); 14382 ASSERT(sense_xp != NULL); 14383 ASSERT(mutex_owned(SD_MUTEX(un))); 14384 ASSERT(un->un_sense_isbusy != 0); 14385 14386 un->un_sense_isbusy = 0; 14387 bp = sense_xp->xb_sense_bp; 14388 sense_xp->xb_sense_bp = NULL; 14389 14390 /* This pkt is no longer interested in getting sense data */ 14391 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 14392 14393 return (bp); 14394 } 14395 14396 14397 14398 /* 14399 * Function: sd_alloc_rqs 14400 * 14401 * Description: Set up the unit to receive auto request sense data 14402 * 14403 * Return Code: DDI_SUCCESS or DDI_FAILURE 14404 * 14405 * Context: Called under attach(9E) context 14406 */ 14407 14408 static int 14409 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 14410 { 14411 struct sd_xbuf *xp; 14412 14413 ASSERT(un != NULL); 14414 ASSERT(!mutex_owned(SD_MUTEX(un))); 14415 ASSERT(un->un_rqs_bp == NULL); 14416 ASSERT(un->un_rqs_pktp == NULL); 14417 14418 /* 14419 * First allocate the required buf and scsi_pkt structs, then set up 14420 * the CDB in the scsi_pkt for a REQUEST SENSE command. 14421 */ 14422 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 14423 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 14424 if (un->un_rqs_bp == NULL) { 14425 return (DDI_FAILURE); 14426 } 14427 14428 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 14429 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 14430 14431 if (un->un_rqs_pktp == NULL) { 14432 sd_free_rqs(un); 14433 return (DDI_FAILURE); 14434 } 14435 14436 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 14437 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 14438 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 14439 14440 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 14441 14442 /* Set up the other needed members in the ARQ scsi_pkt. */ 14443 un->un_rqs_pktp->pkt_comp = sdintr; 14444 un->un_rqs_pktp->pkt_time = sd_io_time; 14445 un->un_rqs_pktp->pkt_flags |= 14446 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 14447 14448 /* 14449 * Allocate & init the sd_xbuf struct for the RQS command. Do not 14450 * provide any intpkt, destroypkt routines as we take care of 14451 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 14452 */ 14453 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14454 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 14455 xp->xb_pktp = un->un_rqs_pktp; 14456 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14457 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 14458 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 14459 14460 /* 14461 * Save the pointer to the request sense private bp so it can 14462 * be retrieved in sdintr. 14463 */ 14464 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 14465 ASSERT(un->un_rqs_bp->b_private == xp); 14466 14467 /* 14468 * See if the HBA supports auto-request sense for the specified 14469 * target/lun. If it does, then try to enable it (if not already 14470 * enabled). 14471 * 14472 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 14473 * failure, while for other HBAs (pln) scsi_ifsetcap will always 14474 * return success. However, in both of these cases ARQ is always 14475 * enabled and scsi_ifgetcap will always return true. The best approach 14476 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 14477 * 14478 * The 3rd case is the HBA (adp) always return enabled on 14479 * scsi_ifgetgetcap even when it's not enable, the best approach 14480 * is issue a scsi_ifsetcap then a scsi_ifgetcap 14481 * Note: this case is to circumvent the Adaptec bug. (x86 only) 14482 */ 14483 14484 if (un->un_f_is_fibre == TRUE) { 14485 un->un_f_arq_enabled = TRUE; 14486 } else { 14487 #if defined(__i386) || defined(__amd64) 14488 /* 14489 * Circumvent the Adaptec bug, remove this code when 14490 * the bug is fixed 14491 */ 14492 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 14493 #endif 14494 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 14495 case 0: 14496 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14497 "sd_alloc_rqs: HBA supports ARQ\n"); 14498 /* 14499 * ARQ is supported by this HBA but currently is not 14500 * enabled. Attempt to enable it and if successful then 14501 * mark this instance as ARQ enabled. 14502 */ 14503 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 14504 == 1) { 14505 /* Successfully enabled ARQ in the HBA */ 14506 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14507 "sd_alloc_rqs: ARQ enabled\n"); 14508 un->un_f_arq_enabled = TRUE; 14509 } else { 14510 /* Could not enable ARQ in the HBA */ 14511 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14512 "sd_alloc_rqs: failed ARQ enable\n"); 14513 un->un_f_arq_enabled = FALSE; 14514 } 14515 break; 14516 case 1: 14517 /* 14518 * ARQ is supported by this HBA and is already enabled. 14519 * Just mark ARQ as enabled for this instance. 14520 */ 14521 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14522 "sd_alloc_rqs: ARQ already enabled\n"); 14523 un->un_f_arq_enabled = TRUE; 14524 break; 14525 default: 14526 /* 14527 * ARQ is not supported by this HBA; disable it for this 14528 * instance. 14529 */ 14530 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14531 "sd_alloc_rqs: HBA does not support ARQ\n"); 14532 un->un_f_arq_enabled = FALSE; 14533 break; 14534 } 14535 } 14536 14537 return (DDI_SUCCESS); 14538 } 14539 14540 14541 /* 14542 * Function: sd_free_rqs 14543 * 14544 * Description: Cleanup for the pre-instance RQS command. 14545 * 14546 * Context: Kernel thread context 14547 */ 14548 14549 static void 14550 sd_free_rqs(struct sd_lun *un) 14551 { 14552 ASSERT(un != NULL); 14553 14554 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 14555 14556 /* 14557 * If consistent memory is bound to a scsi_pkt, the pkt 14558 * has to be destroyed *before* freeing the consistent memory. 14559 * Don't change the sequence of this operations. 14560 * scsi_destroy_pkt() might access memory, which isn't allowed, 14561 * after it was freed in scsi_free_consistent_buf(). 14562 */ 14563 if (un->un_rqs_pktp != NULL) { 14564 scsi_destroy_pkt(un->un_rqs_pktp); 14565 un->un_rqs_pktp = NULL; 14566 } 14567 14568 if (un->un_rqs_bp != NULL) { 14569 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 14570 if (xp != NULL) { 14571 kmem_free(xp, sizeof (struct sd_xbuf)); 14572 } 14573 scsi_free_consistent_buf(un->un_rqs_bp); 14574 un->un_rqs_bp = NULL; 14575 } 14576 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 14577 } 14578 14579 14580 14581 /* 14582 * Function: sd_reduce_throttle 14583 * 14584 * Description: Reduces the maximum # of outstanding commands on a 14585 * target to the current number of outstanding commands. 14586 * Queues a tiemout(9F) callback to restore the limit 14587 * after a specified interval has elapsed. 14588 * Typically used when we get a TRAN_BUSY return code 14589 * back from scsi_transport(). 14590 * 14591 * Arguments: un - ptr to the sd_lun softstate struct 14592 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 14593 * 14594 * Context: May be called from interrupt context 14595 */ 14596 14597 static void 14598 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 14599 { 14600 ASSERT(un != NULL); 14601 ASSERT(mutex_owned(SD_MUTEX(un))); 14602 ASSERT(un->un_ncmds_in_transport >= 0); 14603 14604 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14605 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 14606 un, un->un_throttle, un->un_ncmds_in_transport); 14607 14608 if (un->un_throttle > 1) { 14609 if (un->un_f_use_adaptive_throttle == TRUE) { 14610 switch (throttle_type) { 14611 case SD_THROTTLE_TRAN_BUSY: 14612 if (un->un_busy_throttle == 0) { 14613 un->un_busy_throttle = un->un_throttle; 14614 } 14615 break; 14616 case SD_THROTTLE_QFULL: 14617 un->un_busy_throttle = 0; 14618 break; 14619 default: 14620 ASSERT(FALSE); 14621 } 14622 14623 if (un->un_ncmds_in_transport > 0) { 14624 un->un_throttle = un->un_ncmds_in_transport; 14625 } 14626 14627 } else { 14628 if (un->un_ncmds_in_transport == 0) { 14629 un->un_throttle = 1; 14630 } else { 14631 un->un_throttle = un->un_ncmds_in_transport; 14632 } 14633 } 14634 } 14635 14636 /* Reschedule the timeout if none is currently active */ 14637 if (un->un_reset_throttle_timeid == NULL) { 14638 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 14639 un, SD_THROTTLE_RESET_INTERVAL); 14640 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14641 "sd_reduce_throttle: timeout scheduled!\n"); 14642 } 14643 14644 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14645 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14646 } 14647 14648 14649 14650 /* 14651 * Function: sd_restore_throttle 14652 * 14653 * Description: Callback function for timeout(9F). Resets the current 14654 * value of un->un_throttle to its default. 14655 * 14656 * Arguments: arg - pointer to associated softstate for the device. 14657 * 14658 * Context: May be called from interrupt context 14659 */ 14660 14661 static void 14662 sd_restore_throttle(void *arg) 14663 { 14664 struct sd_lun *un = arg; 14665 14666 ASSERT(un != NULL); 14667 ASSERT(!mutex_owned(SD_MUTEX(un))); 14668 14669 mutex_enter(SD_MUTEX(un)); 14670 14671 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14672 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14673 14674 un->un_reset_throttle_timeid = NULL; 14675 14676 if (un->un_f_use_adaptive_throttle == TRUE) { 14677 /* 14678 * If un_busy_throttle is nonzero, then it contains the 14679 * value that un_throttle was when we got a TRAN_BUSY back 14680 * from scsi_transport(). We want to revert back to this 14681 * value. 14682 * 14683 * In the QFULL case, the throttle limit will incrementally 14684 * increase until it reaches max throttle. 14685 */ 14686 if (un->un_busy_throttle > 0) { 14687 un->un_throttle = un->un_busy_throttle; 14688 un->un_busy_throttle = 0; 14689 } else { 14690 /* 14691 * increase throttle by 10% open gate slowly, schedule 14692 * another restore if saved throttle has not been 14693 * reached 14694 */ 14695 short throttle; 14696 if (sd_qfull_throttle_enable) { 14697 throttle = un->un_throttle + 14698 max((un->un_throttle / 10), 1); 14699 un->un_throttle = 14700 (throttle < un->un_saved_throttle) ? 14701 throttle : un->un_saved_throttle; 14702 if (un->un_throttle < un->un_saved_throttle) { 14703 un->un_reset_throttle_timeid = 14704 timeout(sd_restore_throttle, 14705 un, 14706 SD_QFULL_THROTTLE_RESET_INTERVAL); 14707 } 14708 } 14709 } 14710 14711 /* 14712 * If un_throttle has fallen below the low-water mark, we 14713 * restore the maximum value here (and allow it to ratchet 14714 * down again if necessary). 14715 */ 14716 if (un->un_throttle < un->un_min_throttle) { 14717 un->un_throttle = un->un_saved_throttle; 14718 } 14719 } else { 14720 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14721 "restoring limit from 0x%x to 0x%x\n", 14722 un->un_throttle, un->un_saved_throttle); 14723 un->un_throttle = un->un_saved_throttle; 14724 } 14725 14726 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14727 "sd_restore_throttle: calling sd_start_cmds!\n"); 14728 14729 sd_start_cmds(un, NULL); 14730 14731 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14732 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 14733 un, un->un_throttle); 14734 14735 mutex_exit(SD_MUTEX(un)); 14736 14737 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 14738 } 14739 14740 /* 14741 * Function: sdrunout 14742 * 14743 * Description: Callback routine for scsi_init_pkt when a resource allocation 14744 * fails. 14745 * 14746 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 14747 * soft state instance. 14748 * 14749 * Return Code: The scsi_init_pkt routine allows for the callback function to 14750 * return a 0 indicating the callback should be rescheduled or a 1 14751 * indicating not to reschedule. This routine always returns 1 14752 * because the driver always provides a callback function to 14753 * scsi_init_pkt. This results in a callback always being scheduled 14754 * (via the scsi_init_pkt callback implementation) if a resource 14755 * failure occurs. 14756 * 14757 * Context: This callback function may not block or call routines that block 14758 * 14759 * Note: Using the scsi_init_pkt callback facility can result in an I/O 14760 * request persisting at the head of the list which cannot be 14761 * satisfied even after multiple retries. In the future the driver 14762 * may implement some time of maximum runout count before failing 14763 * an I/O. 14764 */ 14765 14766 static int 14767 sdrunout(caddr_t arg) 14768 { 14769 struct sd_lun *un = (struct sd_lun *)arg; 14770 14771 ASSERT(un != NULL); 14772 ASSERT(!mutex_owned(SD_MUTEX(un))); 14773 14774 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 14775 14776 mutex_enter(SD_MUTEX(un)); 14777 sd_start_cmds(un, NULL); 14778 mutex_exit(SD_MUTEX(un)); 14779 /* 14780 * This callback routine always returns 1 (i.e. do not reschedule) 14781 * because we always specify sdrunout as the callback handler for 14782 * scsi_init_pkt inside the call to sd_start_cmds. 14783 */ 14784 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 14785 return (1); 14786 } 14787 14788 14789 /* 14790 * Function: sdintr 14791 * 14792 * Description: Completion callback routine for scsi_pkt(9S) structs 14793 * sent to the HBA driver via scsi_transport(9F). 14794 * 14795 * Context: Interrupt context 14796 */ 14797 14798 static void 14799 sdintr(struct scsi_pkt *pktp) 14800 { 14801 struct buf *bp; 14802 struct sd_xbuf *xp; 14803 struct sd_lun *un; 14804 size_t actual_len; 14805 14806 ASSERT(pktp != NULL); 14807 bp = (struct buf *)pktp->pkt_private; 14808 ASSERT(bp != NULL); 14809 xp = SD_GET_XBUF(bp); 14810 ASSERT(xp != NULL); 14811 ASSERT(xp->xb_pktp != NULL); 14812 un = SD_GET_UN(bp); 14813 ASSERT(un != NULL); 14814 ASSERT(!mutex_owned(SD_MUTEX(un))); 14815 14816 #ifdef SD_FAULT_INJECTION 14817 14818 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 14819 /* SD FaultInjection */ 14820 sd_faultinjection(pktp); 14821 14822 #endif /* SD_FAULT_INJECTION */ 14823 14824 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 14825 " xp:0x%p, un:0x%p\n", bp, xp, un); 14826 14827 mutex_enter(SD_MUTEX(un)); 14828 14829 /* Reduce the count of the #commands currently in transport */ 14830 un->un_ncmds_in_transport--; 14831 ASSERT(un->un_ncmds_in_transport >= 0); 14832 14833 /* Increment counter to indicate that the callback routine is active */ 14834 un->un_in_callback++; 14835 14836 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14837 14838 #ifdef SDDEBUG 14839 if (bp == un->un_retry_bp) { 14840 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 14841 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 14842 un, un->un_retry_bp, un->un_ncmds_in_transport); 14843 } 14844 #endif 14845 14846 /* 14847 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 14848 * state if needed. 14849 */ 14850 if (pktp->pkt_reason == CMD_DEV_GONE) { 14851 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14852 "Command failed to complete...Device is gone\n"); 14853 if (un->un_mediastate != DKIO_DEV_GONE) { 14854 un->un_mediastate = DKIO_DEV_GONE; 14855 cv_broadcast(&un->un_state_cv); 14856 } 14857 sd_return_failed_command(un, bp, EIO); 14858 goto exit; 14859 } 14860 14861 if (pktp->pkt_state & STATE_XARQ_DONE) { 14862 SD_TRACE(SD_LOG_COMMON, un, 14863 "sdintr: extra sense data received. pkt=%p\n", pktp); 14864 } 14865 14866 /* 14867 * First see if the pkt has auto-request sense data with it.... 14868 * Look at the packet state first so we don't take a performance 14869 * hit looking at the arq enabled flag unless absolutely necessary. 14870 */ 14871 if ((pktp->pkt_state & STATE_ARQ_DONE) && 14872 (un->un_f_arq_enabled == TRUE)) { 14873 /* 14874 * The HBA did an auto request sense for this command so check 14875 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 14876 * driver command that should not be retried. 14877 */ 14878 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14879 /* 14880 * Save the relevant sense info into the xp for the 14881 * original cmd. 14882 */ 14883 struct scsi_arq_status *asp; 14884 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 14885 xp->xb_sense_status = 14886 *((uchar_t *)(&(asp->sts_rqpkt_status))); 14887 xp->xb_sense_state = asp->sts_rqpkt_state; 14888 xp->xb_sense_resid = asp->sts_rqpkt_resid; 14889 if (pktp->pkt_state & STATE_XARQ_DONE) { 14890 actual_len = MAX_SENSE_LENGTH - 14891 xp->xb_sense_resid; 14892 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 14893 MAX_SENSE_LENGTH); 14894 } else { 14895 if (xp->xb_sense_resid > SENSE_LENGTH) { 14896 actual_len = MAX_SENSE_LENGTH - 14897 xp->xb_sense_resid; 14898 } else { 14899 actual_len = SENSE_LENGTH - 14900 xp->xb_sense_resid; 14901 } 14902 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 14903 if ((((struct uscsi_cmd *) 14904 (xp->xb_pktinfo))->uscsi_rqlen) > 14905 actual_len) { 14906 xp->xb_sense_resid = 14907 (((struct uscsi_cmd *) 14908 (xp->xb_pktinfo))-> 14909 uscsi_rqlen) - actual_len; 14910 } else { 14911 xp->xb_sense_resid = 0; 14912 } 14913 } 14914 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 14915 SENSE_LENGTH); 14916 } 14917 14918 /* fail the command */ 14919 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14920 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 14921 sd_return_failed_command(un, bp, EIO); 14922 goto exit; 14923 } 14924 14925 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 14926 /* 14927 * We want to either retry or fail this command, so free 14928 * the DMA resources here. If we retry the command then 14929 * the DMA resources will be reallocated in sd_start_cmds(). 14930 * Note that when PKT_DMA_PARTIAL is used, this reallocation 14931 * causes the *entire* transfer to start over again from the 14932 * beginning of the request, even for PARTIAL chunks that 14933 * have already transferred successfully. 14934 */ 14935 if ((un->un_f_is_fibre == TRUE) && 14936 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14937 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 14938 scsi_dmafree(pktp); 14939 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14940 } 14941 #endif 14942 14943 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14944 "sdintr: arq done, sd_handle_auto_request_sense\n"); 14945 14946 sd_handle_auto_request_sense(un, bp, xp, pktp); 14947 goto exit; 14948 } 14949 14950 /* Next see if this is the REQUEST SENSE pkt for the instance */ 14951 if (pktp->pkt_flags & FLAG_SENSING) { 14952 /* This pktp is from the unit's REQUEST_SENSE command */ 14953 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14954 "sdintr: sd_handle_request_sense\n"); 14955 sd_handle_request_sense(un, bp, xp, pktp); 14956 goto exit; 14957 } 14958 14959 /* 14960 * Check to see if the command successfully completed as requested; 14961 * this is the most common case (and also the hot performance path). 14962 * 14963 * Requirements for successful completion are: 14964 * pkt_reason is CMD_CMPLT and packet status is status good. 14965 * In addition: 14966 * - A residual of zero indicates successful completion no matter what 14967 * the command is. 14968 * - If the residual is not zero and the command is not a read or 14969 * write, then it's still defined as successful completion. In other 14970 * words, if the command is a read or write the residual must be 14971 * zero for successful completion. 14972 * - If the residual is not zero and the command is a read or 14973 * write, and it's a USCSICMD, then it's still defined as 14974 * successful completion. 14975 */ 14976 if ((pktp->pkt_reason == CMD_CMPLT) && 14977 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 14978 14979 /* 14980 * Since this command is returned with a good status, we 14981 * can reset the count for Sonoma failover. 14982 */ 14983 un->un_sonoma_failure_count = 0; 14984 14985 /* 14986 * Return all USCSI commands on good status 14987 */ 14988 if (pktp->pkt_resid == 0) { 14989 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14990 "sdintr: returning command for resid == 0\n"); 14991 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 14992 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 14993 SD_UPDATE_B_RESID(bp, pktp); 14994 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14995 "sdintr: returning command for resid != 0\n"); 14996 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 14997 SD_UPDATE_B_RESID(bp, pktp); 14998 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14999 "sdintr: returning uscsi command\n"); 15000 } else { 15001 goto not_successful; 15002 } 15003 sd_return_command(un, bp); 15004 15005 /* 15006 * Decrement counter to indicate that the callback routine 15007 * is done. 15008 */ 15009 un->un_in_callback--; 15010 ASSERT(un->un_in_callback >= 0); 15011 mutex_exit(SD_MUTEX(un)); 15012 15013 return; 15014 } 15015 15016 not_successful: 15017 15018 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 15019 /* 15020 * The following is based upon knowledge of the underlying transport 15021 * and its use of DMA resources. This code should be removed when 15022 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 15023 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 15024 * and sd_start_cmds(). 15025 * 15026 * Free any DMA resources associated with this command if there 15027 * is a chance it could be retried or enqueued for later retry. 15028 * If we keep the DMA binding then mpxio cannot reissue the 15029 * command on another path whenever a path failure occurs. 15030 * 15031 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 15032 * causes the *entire* transfer to start over again from the 15033 * beginning of the request, even for PARTIAL chunks that 15034 * have already transferred successfully. 15035 * 15036 * This is only done for non-uscsi commands (and also skipped for the 15037 * driver's internal RQS command). Also just do this for Fibre Channel 15038 * devices as these are the only ones that support mpxio. 15039 */ 15040 if ((un->un_f_is_fibre == TRUE) && 15041 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15042 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 15043 scsi_dmafree(pktp); 15044 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15045 } 15046 #endif 15047 15048 /* 15049 * The command did not successfully complete as requested so check 15050 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 15051 * driver command that should not be retried so just return. If 15052 * FLAG_DIAGNOSE is not set the error will be processed below. 15053 */ 15054 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15055 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15056 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 15057 /* 15058 * Issue a request sense if a check condition caused the error 15059 * (we handle the auto request sense case above), otherwise 15060 * just fail the command. 15061 */ 15062 if ((pktp->pkt_reason == CMD_CMPLT) && 15063 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 15064 sd_send_request_sense_command(un, bp, pktp); 15065 } else { 15066 sd_return_failed_command(un, bp, EIO); 15067 } 15068 goto exit; 15069 } 15070 15071 /* 15072 * The command did not successfully complete as requested so process 15073 * the error, retry, and/or attempt recovery. 15074 */ 15075 switch (pktp->pkt_reason) { 15076 case CMD_CMPLT: 15077 switch (SD_GET_PKT_STATUS(pktp)) { 15078 case STATUS_GOOD: 15079 /* 15080 * The command completed successfully with a non-zero 15081 * residual 15082 */ 15083 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15084 "sdintr: STATUS_GOOD \n"); 15085 sd_pkt_status_good(un, bp, xp, pktp); 15086 break; 15087 15088 case STATUS_CHECK: 15089 case STATUS_TERMINATED: 15090 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15091 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 15092 sd_pkt_status_check_condition(un, bp, xp, pktp); 15093 break; 15094 15095 case STATUS_BUSY: 15096 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15097 "sdintr: STATUS_BUSY\n"); 15098 sd_pkt_status_busy(un, bp, xp, pktp); 15099 break; 15100 15101 case STATUS_RESERVATION_CONFLICT: 15102 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15103 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 15104 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15105 break; 15106 15107 case STATUS_QFULL: 15108 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15109 "sdintr: STATUS_QFULL\n"); 15110 sd_pkt_status_qfull(un, bp, xp, pktp); 15111 break; 15112 15113 case STATUS_MET: 15114 case STATUS_INTERMEDIATE: 15115 case STATUS_SCSI2: 15116 case STATUS_INTERMEDIATE_MET: 15117 case STATUS_ACA_ACTIVE: 15118 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 15119 "Unexpected SCSI status received: 0x%x\n", 15120 SD_GET_PKT_STATUS(pktp)); 15121 sd_return_failed_command(un, bp, EIO); 15122 break; 15123 15124 default: 15125 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 15126 "Invalid SCSI status received: 0x%x\n", 15127 SD_GET_PKT_STATUS(pktp)); 15128 sd_return_failed_command(un, bp, EIO); 15129 break; 15130 15131 } 15132 break; 15133 15134 case CMD_INCOMPLETE: 15135 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15136 "sdintr: CMD_INCOMPLETE\n"); 15137 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 15138 break; 15139 case CMD_TRAN_ERR: 15140 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15141 "sdintr: CMD_TRAN_ERR\n"); 15142 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 15143 break; 15144 case CMD_RESET: 15145 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15146 "sdintr: CMD_RESET \n"); 15147 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 15148 break; 15149 case CMD_ABORTED: 15150 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15151 "sdintr: CMD_ABORTED \n"); 15152 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 15153 break; 15154 case CMD_TIMEOUT: 15155 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15156 "sdintr: CMD_TIMEOUT\n"); 15157 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 15158 break; 15159 case CMD_UNX_BUS_FREE: 15160 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15161 "sdintr: CMD_UNX_BUS_FREE \n"); 15162 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 15163 break; 15164 case CMD_TAG_REJECT: 15165 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15166 "sdintr: CMD_TAG_REJECT\n"); 15167 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 15168 break; 15169 default: 15170 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15171 "sdintr: default\n"); 15172 sd_pkt_reason_default(un, bp, xp, pktp); 15173 break; 15174 } 15175 15176 exit: 15177 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 15178 15179 /* Decrement counter to indicate that the callback routine is done. */ 15180 un->un_in_callback--; 15181 ASSERT(un->un_in_callback >= 0); 15182 15183 /* 15184 * At this point, the pkt has been dispatched, ie, it is either 15185 * being re-tried or has been returned to its caller and should 15186 * not be referenced. 15187 */ 15188 15189 mutex_exit(SD_MUTEX(un)); 15190 } 15191 15192 15193 /* 15194 * Function: sd_print_incomplete_msg 15195 * 15196 * Description: Prints the error message for a CMD_INCOMPLETE error. 15197 * 15198 * Arguments: un - ptr to associated softstate for the device. 15199 * bp - ptr to the buf(9S) for the command. 15200 * arg - message string ptr 15201 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 15202 * or SD_NO_RETRY_ISSUED. 15203 * 15204 * Context: May be called under interrupt context 15205 */ 15206 15207 static void 15208 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 15209 { 15210 struct scsi_pkt *pktp; 15211 char *msgp; 15212 char *cmdp = arg; 15213 15214 ASSERT(un != NULL); 15215 ASSERT(mutex_owned(SD_MUTEX(un))); 15216 ASSERT(bp != NULL); 15217 ASSERT(arg != NULL); 15218 pktp = SD_GET_PKTP(bp); 15219 ASSERT(pktp != NULL); 15220 15221 switch (code) { 15222 case SD_DELAYED_RETRY_ISSUED: 15223 case SD_IMMEDIATE_RETRY_ISSUED: 15224 msgp = "retrying"; 15225 break; 15226 case SD_NO_RETRY_ISSUED: 15227 default: 15228 msgp = "giving up"; 15229 break; 15230 } 15231 15232 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15233 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15234 "incomplete %s- %s\n", cmdp, msgp); 15235 } 15236 } 15237 15238 15239 15240 /* 15241 * Function: sd_pkt_status_good 15242 * 15243 * Description: Processing for a STATUS_GOOD code in pkt_status. 15244 * 15245 * Context: May be called under interrupt context 15246 */ 15247 15248 static void 15249 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 15250 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15251 { 15252 char *cmdp; 15253 15254 ASSERT(un != NULL); 15255 ASSERT(mutex_owned(SD_MUTEX(un))); 15256 ASSERT(bp != NULL); 15257 ASSERT(xp != NULL); 15258 ASSERT(pktp != NULL); 15259 ASSERT(pktp->pkt_reason == CMD_CMPLT); 15260 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 15261 ASSERT(pktp->pkt_resid != 0); 15262 15263 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 15264 15265 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15266 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 15267 case SCMD_READ: 15268 cmdp = "read"; 15269 break; 15270 case SCMD_WRITE: 15271 cmdp = "write"; 15272 break; 15273 default: 15274 SD_UPDATE_B_RESID(bp, pktp); 15275 sd_return_command(un, bp); 15276 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15277 return; 15278 } 15279 15280 /* 15281 * See if we can retry the read/write, preferrably immediately. 15282 * If retries are exhaused, then sd_retry_command() will update 15283 * the b_resid count. 15284 */ 15285 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 15286 cmdp, EIO, (clock_t)0, NULL); 15287 15288 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15289 } 15290 15291 15292 15293 15294 15295 /* 15296 * Function: sd_handle_request_sense 15297 * 15298 * Description: Processing for non-auto Request Sense command. 15299 * 15300 * Arguments: un - ptr to associated softstate 15301 * sense_bp - ptr to buf(9S) for the RQS command 15302 * sense_xp - ptr to the sd_xbuf for the RQS command 15303 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 15304 * 15305 * Context: May be called under interrupt context 15306 */ 15307 15308 static void 15309 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 15310 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 15311 { 15312 struct buf *cmd_bp; /* buf for the original command */ 15313 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 15314 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 15315 size_t actual_len; /* actual sense data length */ 15316 15317 ASSERT(un != NULL); 15318 ASSERT(mutex_owned(SD_MUTEX(un))); 15319 ASSERT(sense_bp != NULL); 15320 ASSERT(sense_xp != NULL); 15321 ASSERT(sense_pktp != NULL); 15322 15323 /* 15324 * Note the sense_bp, sense_xp, and sense_pktp here are for the 15325 * RQS command and not the original command. 15326 */ 15327 ASSERT(sense_pktp == un->un_rqs_pktp); 15328 ASSERT(sense_bp == un->un_rqs_bp); 15329 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 15330 (FLAG_SENSING | FLAG_HEAD)); 15331 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 15332 FLAG_SENSING) == FLAG_SENSING); 15333 15334 /* These are the bp, xp, and pktp for the original command */ 15335 cmd_bp = sense_xp->xb_sense_bp; 15336 cmd_xp = SD_GET_XBUF(cmd_bp); 15337 cmd_pktp = SD_GET_PKTP(cmd_bp); 15338 15339 if (sense_pktp->pkt_reason != CMD_CMPLT) { 15340 /* 15341 * The REQUEST SENSE command failed. Release the REQUEST 15342 * SENSE command for re-use, get back the bp for the original 15343 * command, and attempt to re-try the original command if 15344 * FLAG_DIAGNOSE is not set in the original packet. 15345 */ 15346 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15347 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15348 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 15349 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 15350 NULL, NULL, EIO, (clock_t)0, NULL); 15351 return; 15352 } 15353 } 15354 15355 /* 15356 * Save the relevant sense info into the xp for the original cmd. 15357 * 15358 * Note: if the request sense failed the state info will be zero 15359 * as set in sd_mark_rqs_busy() 15360 */ 15361 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 15362 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 15363 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 15364 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 15365 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 15366 SENSE_LENGTH)) { 15367 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 15368 MAX_SENSE_LENGTH); 15369 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 15370 } else { 15371 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 15372 SENSE_LENGTH); 15373 if (actual_len < SENSE_LENGTH) { 15374 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 15375 } else { 15376 cmd_xp->xb_sense_resid = 0; 15377 } 15378 } 15379 15380 /* 15381 * Free up the RQS command.... 15382 * NOTE: 15383 * Must do this BEFORE calling sd_validate_sense_data! 15384 * sd_validate_sense_data may return the original command in 15385 * which case the pkt will be freed and the flags can no 15386 * longer be touched. 15387 * SD_MUTEX is held through this process until the command 15388 * is dispatched based upon the sense data, so there are 15389 * no race conditions. 15390 */ 15391 (void) sd_mark_rqs_idle(un, sense_xp); 15392 15393 /* 15394 * For a retryable command see if we have valid sense data, if so then 15395 * turn it over to sd_decode_sense() to figure out the right course of 15396 * action. Just fail a non-retryable command. 15397 */ 15398 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15399 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 15400 SD_SENSE_DATA_IS_VALID) { 15401 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 15402 } 15403 } else { 15404 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 15405 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15406 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 15407 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15408 sd_return_failed_command(un, cmd_bp, EIO); 15409 } 15410 } 15411 15412 15413 15414 15415 /* 15416 * Function: sd_handle_auto_request_sense 15417 * 15418 * Description: Processing for auto-request sense information. 15419 * 15420 * Arguments: un - ptr to associated softstate 15421 * bp - ptr to buf(9S) for the command 15422 * xp - ptr to the sd_xbuf for the command 15423 * pktp - ptr to the scsi_pkt(9S) for the command 15424 * 15425 * Context: May be called under interrupt context 15426 */ 15427 15428 static void 15429 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 15430 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15431 { 15432 struct scsi_arq_status *asp; 15433 size_t actual_len; 15434 15435 ASSERT(un != NULL); 15436 ASSERT(mutex_owned(SD_MUTEX(un))); 15437 ASSERT(bp != NULL); 15438 ASSERT(xp != NULL); 15439 ASSERT(pktp != NULL); 15440 ASSERT(pktp != un->un_rqs_pktp); 15441 ASSERT(bp != un->un_rqs_bp); 15442 15443 /* 15444 * For auto-request sense, we get a scsi_arq_status back from 15445 * the HBA, with the sense data in the sts_sensedata member. 15446 * The pkt_scbp of the packet points to this scsi_arq_status. 15447 */ 15448 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 15449 15450 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 15451 /* 15452 * The auto REQUEST SENSE failed; see if we can re-try 15453 * the original command. 15454 */ 15455 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15456 "auto request sense failed (reason=%s)\n", 15457 scsi_rname(asp->sts_rqpkt_reason)); 15458 15459 sd_reset_target(un, pktp); 15460 15461 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15462 NULL, NULL, EIO, (clock_t)0, NULL); 15463 return; 15464 } 15465 15466 /* Save the relevant sense info into the xp for the original cmd. */ 15467 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 15468 xp->xb_sense_state = asp->sts_rqpkt_state; 15469 xp->xb_sense_resid = asp->sts_rqpkt_resid; 15470 if (xp->xb_sense_state & STATE_XARQ_DONE) { 15471 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 15472 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 15473 MAX_SENSE_LENGTH); 15474 } else { 15475 if (xp->xb_sense_resid > SENSE_LENGTH) { 15476 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 15477 } else { 15478 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 15479 } 15480 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 15481 if ((((struct uscsi_cmd *) 15482 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 15483 xp->xb_sense_resid = (((struct uscsi_cmd *) 15484 (xp->xb_pktinfo))->uscsi_rqlen) - 15485 actual_len; 15486 } else { 15487 xp->xb_sense_resid = 0; 15488 } 15489 } 15490 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 15491 } 15492 15493 /* 15494 * See if we have valid sense data, if so then turn it over to 15495 * sd_decode_sense() to figure out the right course of action. 15496 */ 15497 if (sd_validate_sense_data(un, bp, xp, actual_len) == 15498 SD_SENSE_DATA_IS_VALID) { 15499 sd_decode_sense(un, bp, xp, pktp); 15500 } 15501 } 15502 15503 15504 /* 15505 * Function: sd_print_sense_failed_msg 15506 * 15507 * Description: Print log message when RQS has failed. 15508 * 15509 * Arguments: un - ptr to associated softstate 15510 * bp - ptr to buf(9S) for the command 15511 * arg - generic message string ptr 15512 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15513 * or SD_NO_RETRY_ISSUED 15514 * 15515 * Context: May be called from interrupt context 15516 */ 15517 15518 static void 15519 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 15520 int code) 15521 { 15522 char *msgp = arg; 15523 15524 ASSERT(un != NULL); 15525 ASSERT(mutex_owned(SD_MUTEX(un))); 15526 ASSERT(bp != NULL); 15527 15528 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 15529 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 15530 } 15531 } 15532 15533 15534 /* 15535 * Function: sd_validate_sense_data 15536 * 15537 * Description: Check the given sense data for validity. 15538 * If the sense data is not valid, the command will 15539 * be either failed or retried! 15540 * 15541 * Return Code: SD_SENSE_DATA_IS_INVALID 15542 * SD_SENSE_DATA_IS_VALID 15543 * 15544 * Context: May be called from interrupt context 15545 */ 15546 15547 static int 15548 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 15549 size_t actual_len) 15550 { 15551 struct scsi_extended_sense *esp; 15552 struct scsi_pkt *pktp; 15553 char *msgp = NULL; 15554 15555 ASSERT(un != NULL); 15556 ASSERT(mutex_owned(SD_MUTEX(un))); 15557 ASSERT(bp != NULL); 15558 ASSERT(bp != un->un_rqs_bp); 15559 ASSERT(xp != NULL); 15560 15561 pktp = SD_GET_PKTP(bp); 15562 ASSERT(pktp != NULL); 15563 15564 /* 15565 * Check the status of the RQS command (auto or manual). 15566 */ 15567 switch (xp->xb_sense_status & STATUS_MASK) { 15568 case STATUS_GOOD: 15569 break; 15570 15571 case STATUS_RESERVATION_CONFLICT: 15572 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15573 return (SD_SENSE_DATA_IS_INVALID); 15574 15575 case STATUS_BUSY: 15576 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15577 "Busy Status on REQUEST SENSE\n"); 15578 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 15579 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15580 return (SD_SENSE_DATA_IS_INVALID); 15581 15582 case STATUS_QFULL: 15583 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15584 "QFULL Status on REQUEST SENSE\n"); 15585 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 15586 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15587 return (SD_SENSE_DATA_IS_INVALID); 15588 15589 case STATUS_CHECK: 15590 case STATUS_TERMINATED: 15591 msgp = "Check Condition on REQUEST SENSE\n"; 15592 goto sense_failed; 15593 15594 default: 15595 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 15596 goto sense_failed; 15597 } 15598 15599 /* 15600 * See if we got the minimum required amount of sense data. 15601 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 15602 * or less. 15603 */ 15604 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 15605 (actual_len == 0)) { 15606 msgp = "Request Sense couldn't get sense data\n"; 15607 goto sense_failed; 15608 } 15609 15610 if (actual_len < SUN_MIN_SENSE_LENGTH) { 15611 msgp = "Not enough sense information\n"; 15612 goto sense_failed; 15613 } 15614 15615 /* 15616 * We require the extended sense data 15617 */ 15618 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 15619 if (esp->es_class != CLASS_EXTENDED_SENSE) { 15620 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15621 static char tmp[8]; 15622 static char buf[148]; 15623 char *p = (char *)(xp->xb_sense_data); 15624 int i; 15625 15626 mutex_enter(&sd_sense_mutex); 15627 (void) strcpy(buf, "undecodable sense information:"); 15628 for (i = 0; i < actual_len; i++) { 15629 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 15630 (void) strcpy(&buf[strlen(buf)], tmp); 15631 } 15632 i = strlen(buf); 15633 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 15634 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 15635 mutex_exit(&sd_sense_mutex); 15636 } 15637 /* Note: Legacy behavior, fail the command with no retry */ 15638 sd_return_failed_command(un, bp, EIO); 15639 return (SD_SENSE_DATA_IS_INVALID); 15640 } 15641 15642 /* 15643 * Check that es_code is valid (es_class concatenated with es_code 15644 * make up the "response code" field. es_class will always be 7, so 15645 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 15646 * format. 15647 */ 15648 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 15649 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 15650 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 15651 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 15652 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 15653 goto sense_failed; 15654 } 15655 15656 return (SD_SENSE_DATA_IS_VALID); 15657 15658 sense_failed: 15659 /* 15660 * If the request sense failed (for whatever reason), attempt 15661 * to retry the original command. 15662 */ 15663 #if defined(__i386) || defined(__amd64) 15664 /* 15665 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 15666 * sddef.h for Sparc platform, and x86 uses 1 binary 15667 * for both SCSI/FC. 15668 * The SD_RETRY_DELAY value need to be adjusted here 15669 * when SD_RETRY_DELAY change in sddef.h 15670 */ 15671 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15672 sd_print_sense_failed_msg, msgp, EIO, 15673 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 15674 #else 15675 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15676 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 15677 #endif 15678 15679 return (SD_SENSE_DATA_IS_INVALID); 15680 } 15681 15682 15683 15684 /* 15685 * Function: sd_decode_sense 15686 * 15687 * Description: Take recovery action(s) when SCSI Sense Data is received. 15688 * 15689 * Context: Interrupt context. 15690 */ 15691 15692 static void 15693 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 15694 struct scsi_pkt *pktp) 15695 { 15696 uint8_t sense_key; 15697 15698 ASSERT(un != NULL); 15699 ASSERT(mutex_owned(SD_MUTEX(un))); 15700 ASSERT(bp != NULL); 15701 ASSERT(bp != un->un_rqs_bp); 15702 ASSERT(xp != NULL); 15703 ASSERT(pktp != NULL); 15704 15705 sense_key = scsi_sense_key(xp->xb_sense_data); 15706 15707 switch (sense_key) { 15708 case KEY_NO_SENSE: 15709 sd_sense_key_no_sense(un, bp, xp, pktp); 15710 break; 15711 case KEY_RECOVERABLE_ERROR: 15712 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 15713 bp, xp, pktp); 15714 break; 15715 case KEY_NOT_READY: 15716 sd_sense_key_not_ready(un, xp->xb_sense_data, 15717 bp, xp, pktp); 15718 break; 15719 case KEY_MEDIUM_ERROR: 15720 case KEY_HARDWARE_ERROR: 15721 sd_sense_key_medium_or_hardware_error(un, 15722 xp->xb_sense_data, bp, xp, pktp); 15723 break; 15724 case KEY_ILLEGAL_REQUEST: 15725 sd_sense_key_illegal_request(un, bp, xp, pktp); 15726 break; 15727 case KEY_UNIT_ATTENTION: 15728 sd_sense_key_unit_attention(un, xp->xb_sense_data, 15729 bp, xp, pktp); 15730 break; 15731 case KEY_WRITE_PROTECT: 15732 case KEY_VOLUME_OVERFLOW: 15733 case KEY_MISCOMPARE: 15734 sd_sense_key_fail_command(un, bp, xp, pktp); 15735 break; 15736 case KEY_BLANK_CHECK: 15737 sd_sense_key_blank_check(un, bp, xp, pktp); 15738 break; 15739 case KEY_ABORTED_COMMAND: 15740 sd_sense_key_aborted_command(un, bp, xp, pktp); 15741 break; 15742 case KEY_VENDOR_UNIQUE: 15743 case KEY_COPY_ABORTED: 15744 case KEY_EQUAL: 15745 case KEY_RESERVED: 15746 default: 15747 sd_sense_key_default(un, xp->xb_sense_data, 15748 bp, xp, pktp); 15749 break; 15750 } 15751 } 15752 15753 15754 /* 15755 * Function: sd_dump_memory 15756 * 15757 * Description: Debug logging routine to print the contents of a user provided 15758 * buffer. The output of the buffer is broken up into 256 byte 15759 * segments due to a size constraint of the scsi_log. 15760 * implementation. 15761 * 15762 * Arguments: un - ptr to softstate 15763 * comp - component mask 15764 * title - "title" string to preceed data when printed 15765 * data - ptr to data block to be printed 15766 * len - size of data block to be printed 15767 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 15768 * 15769 * Context: May be called from interrupt context 15770 */ 15771 15772 #define SD_DUMP_MEMORY_BUF_SIZE 256 15773 15774 static char *sd_dump_format_string[] = { 15775 " 0x%02x", 15776 " %c" 15777 }; 15778 15779 static void 15780 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 15781 int len, int fmt) 15782 { 15783 int i, j; 15784 int avail_count; 15785 int start_offset; 15786 int end_offset; 15787 size_t entry_len; 15788 char *bufp; 15789 char *local_buf; 15790 char *format_string; 15791 15792 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 15793 15794 /* 15795 * In the debug version of the driver, this function is called from a 15796 * number of places which are NOPs in the release driver. 15797 * The debug driver therefore has additional methods of filtering 15798 * debug output. 15799 */ 15800 #ifdef SDDEBUG 15801 /* 15802 * In the debug version of the driver we can reduce the amount of debug 15803 * messages by setting sd_error_level to something other than 15804 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 15805 * sd_component_mask. 15806 */ 15807 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 15808 (sd_error_level != SCSI_ERR_ALL)) { 15809 return; 15810 } 15811 if (((sd_component_mask & comp) == 0) || 15812 (sd_error_level != SCSI_ERR_ALL)) { 15813 return; 15814 } 15815 #else 15816 if (sd_error_level != SCSI_ERR_ALL) { 15817 return; 15818 } 15819 #endif 15820 15821 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 15822 bufp = local_buf; 15823 /* 15824 * Available length is the length of local_buf[], minus the 15825 * length of the title string, minus one for the ":", minus 15826 * one for the newline, minus one for the NULL terminator. 15827 * This gives the #bytes available for holding the printed 15828 * values from the given data buffer. 15829 */ 15830 if (fmt == SD_LOG_HEX) { 15831 format_string = sd_dump_format_string[0]; 15832 } else /* SD_LOG_CHAR */ { 15833 format_string = sd_dump_format_string[1]; 15834 } 15835 /* 15836 * Available count is the number of elements from the given 15837 * data buffer that we can fit into the available length. 15838 * This is based upon the size of the format string used. 15839 * Make one entry and find it's size. 15840 */ 15841 (void) sprintf(bufp, format_string, data[0]); 15842 entry_len = strlen(bufp); 15843 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 15844 15845 j = 0; 15846 while (j < len) { 15847 bufp = local_buf; 15848 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 15849 start_offset = j; 15850 15851 end_offset = start_offset + avail_count; 15852 15853 (void) sprintf(bufp, "%s:", title); 15854 bufp += strlen(bufp); 15855 for (i = start_offset; ((i < end_offset) && (j < len)); 15856 i++, j++) { 15857 (void) sprintf(bufp, format_string, data[i]); 15858 bufp += entry_len; 15859 } 15860 (void) sprintf(bufp, "\n"); 15861 15862 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 15863 } 15864 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 15865 } 15866 15867 /* 15868 * Function: sd_print_sense_msg 15869 * 15870 * Description: Log a message based upon the given sense data. 15871 * 15872 * Arguments: un - ptr to associated softstate 15873 * bp - ptr to buf(9S) for the command 15874 * arg - ptr to associate sd_sense_info struct 15875 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15876 * or SD_NO_RETRY_ISSUED 15877 * 15878 * Context: May be called from interrupt context 15879 */ 15880 15881 static void 15882 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 15883 { 15884 struct sd_xbuf *xp; 15885 struct scsi_pkt *pktp; 15886 uint8_t *sensep; 15887 daddr_t request_blkno; 15888 diskaddr_t err_blkno; 15889 int severity; 15890 int pfa_flag; 15891 extern struct scsi_key_strings scsi_cmds[]; 15892 15893 ASSERT(un != NULL); 15894 ASSERT(mutex_owned(SD_MUTEX(un))); 15895 ASSERT(bp != NULL); 15896 xp = SD_GET_XBUF(bp); 15897 ASSERT(xp != NULL); 15898 pktp = SD_GET_PKTP(bp); 15899 ASSERT(pktp != NULL); 15900 ASSERT(arg != NULL); 15901 15902 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 15903 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 15904 15905 if ((code == SD_DELAYED_RETRY_ISSUED) || 15906 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 15907 severity = SCSI_ERR_RETRYABLE; 15908 } 15909 15910 /* Use absolute block number for the request block number */ 15911 request_blkno = xp->xb_blkno; 15912 15913 /* 15914 * Now try to get the error block number from the sense data 15915 */ 15916 sensep = xp->xb_sense_data; 15917 15918 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 15919 (uint64_t *)&err_blkno)) { 15920 /* 15921 * We retrieved the error block number from the information 15922 * portion of the sense data. 15923 * 15924 * For USCSI commands we are better off using the error 15925 * block no. as the requested block no. (This is the best 15926 * we can estimate.) 15927 */ 15928 if ((SD_IS_BUFIO(xp) == FALSE) && 15929 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 15930 request_blkno = err_blkno; 15931 } 15932 } else { 15933 /* 15934 * Without the es_valid bit set (for fixed format) or an 15935 * information descriptor (for descriptor format) we cannot 15936 * be certain of the error blkno, so just use the 15937 * request_blkno. 15938 */ 15939 err_blkno = (diskaddr_t)request_blkno; 15940 } 15941 15942 /* 15943 * The following will log the buffer contents for the release driver 15944 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 15945 * level is set to verbose. 15946 */ 15947 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 15948 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15949 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15950 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 15951 15952 if (pfa_flag == FALSE) { 15953 /* This is normally only set for USCSI */ 15954 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 15955 return; 15956 } 15957 15958 if ((SD_IS_BUFIO(xp) == TRUE) && 15959 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 15960 (severity < sd_error_level))) { 15961 return; 15962 } 15963 } 15964 15965 /* 15966 * Check for Sonoma Failover and keep a count of how many failed I/O's 15967 */ 15968 if ((SD_IS_LSI(un)) && 15969 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 15970 (scsi_sense_asc(sensep) == 0x94) && 15971 (scsi_sense_ascq(sensep) == 0x01)) { 15972 un->un_sonoma_failure_count++; 15973 if (un->un_sonoma_failure_count > 1) { 15974 return; 15975 } 15976 } 15977 15978 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 15979 request_blkno, err_blkno, scsi_cmds, 15980 (struct scsi_extended_sense *)sensep, 15981 un->un_additional_codes, NULL); 15982 } 15983 15984 /* 15985 * Function: sd_sense_key_no_sense 15986 * 15987 * Description: Recovery action when sense data was not received. 15988 * 15989 * Context: May be called from interrupt context 15990 */ 15991 15992 static void 15993 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 15994 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15995 { 15996 struct sd_sense_info si; 15997 15998 ASSERT(un != NULL); 15999 ASSERT(mutex_owned(SD_MUTEX(un))); 16000 ASSERT(bp != NULL); 16001 ASSERT(xp != NULL); 16002 ASSERT(pktp != NULL); 16003 16004 si.ssi_severity = SCSI_ERR_FATAL; 16005 si.ssi_pfa_flag = FALSE; 16006 16007 SD_UPDATE_ERRSTATS(un, sd_softerrs); 16008 16009 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16010 &si, EIO, (clock_t)0, NULL); 16011 } 16012 16013 16014 /* 16015 * Function: sd_sense_key_recoverable_error 16016 * 16017 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 16018 * 16019 * Context: May be called from interrupt context 16020 */ 16021 16022 static void 16023 sd_sense_key_recoverable_error(struct sd_lun *un, 16024 uint8_t *sense_datap, 16025 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16026 { 16027 struct sd_sense_info si; 16028 uint8_t asc = scsi_sense_asc(sense_datap); 16029 16030 ASSERT(un != NULL); 16031 ASSERT(mutex_owned(SD_MUTEX(un))); 16032 ASSERT(bp != NULL); 16033 ASSERT(xp != NULL); 16034 ASSERT(pktp != NULL); 16035 16036 /* 16037 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 16038 */ 16039 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 16040 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16041 si.ssi_severity = SCSI_ERR_INFO; 16042 si.ssi_pfa_flag = TRUE; 16043 } else { 16044 SD_UPDATE_ERRSTATS(un, sd_softerrs); 16045 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 16046 si.ssi_severity = SCSI_ERR_RECOVERED; 16047 si.ssi_pfa_flag = FALSE; 16048 } 16049 16050 if (pktp->pkt_resid == 0) { 16051 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16052 sd_return_command(un, bp); 16053 return; 16054 } 16055 16056 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16057 &si, EIO, (clock_t)0, NULL); 16058 } 16059 16060 16061 16062 16063 /* 16064 * Function: sd_sense_key_not_ready 16065 * 16066 * Description: Recovery actions for a SCSI "Not Ready" sense key. 16067 * 16068 * Context: May be called from interrupt context 16069 */ 16070 16071 static void 16072 sd_sense_key_not_ready(struct sd_lun *un, 16073 uint8_t *sense_datap, 16074 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16075 { 16076 struct sd_sense_info si; 16077 uint8_t asc = scsi_sense_asc(sense_datap); 16078 uint8_t ascq = scsi_sense_ascq(sense_datap); 16079 16080 ASSERT(un != NULL); 16081 ASSERT(mutex_owned(SD_MUTEX(un))); 16082 ASSERT(bp != NULL); 16083 ASSERT(xp != NULL); 16084 ASSERT(pktp != NULL); 16085 16086 si.ssi_severity = SCSI_ERR_FATAL; 16087 si.ssi_pfa_flag = FALSE; 16088 16089 /* 16090 * Update error stats after first NOT READY error. Disks may have 16091 * been powered down and may need to be restarted. For CDROMs, 16092 * report NOT READY errors only if media is present. 16093 */ 16094 if ((ISCD(un) && (asc == 0x3A)) || 16095 (xp->xb_nr_retry_count > 0)) { 16096 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16097 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 16098 } 16099 16100 /* 16101 * Just fail if the "not ready" retry limit has been reached. 16102 */ 16103 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 16104 /* Special check for error message printing for removables. */ 16105 if (un->un_f_has_removable_media && (asc == 0x04) && 16106 (ascq >= 0x04)) { 16107 si.ssi_severity = SCSI_ERR_ALL; 16108 } 16109 goto fail_command; 16110 } 16111 16112 /* 16113 * Check the ASC and ASCQ in the sense data as needed, to determine 16114 * what to do. 16115 */ 16116 switch (asc) { 16117 case 0x04: /* LOGICAL UNIT NOT READY */ 16118 /* 16119 * disk drives that don't spin up result in a very long delay 16120 * in format without warning messages. We will log a message 16121 * if the error level is set to verbose. 16122 */ 16123 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16124 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16125 "logical unit not ready, resetting disk\n"); 16126 } 16127 16128 /* 16129 * There are different requirements for CDROMs and disks for 16130 * the number of retries. If a CD-ROM is giving this, it is 16131 * probably reading TOC and is in the process of getting 16132 * ready, so we should keep on trying for a long time to make 16133 * sure that all types of media are taken in account (for 16134 * some media the drive takes a long time to read TOC). For 16135 * disks we do not want to retry this too many times as this 16136 * can cause a long hang in format when the drive refuses to 16137 * spin up (a very common failure). 16138 */ 16139 switch (ascq) { 16140 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 16141 /* 16142 * Disk drives frequently refuse to spin up which 16143 * results in a very long hang in format without 16144 * warning messages. 16145 * 16146 * Note: This code preserves the legacy behavior of 16147 * comparing xb_nr_retry_count against zero for fibre 16148 * channel targets instead of comparing against the 16149 * un_reset_retry_count value. The reason for this 16150 * discrepancy has been so utterly lost beneath the 16151 * Sands of Time that even Indiana Jones could not 16152 * find it. 16153 */ 16154 if (un->un_f_is_fibre == TRUE) { 16155 if (((sd_level_mask & SD_LOGMASK_DIAG) || 16156 (xp->xb_nr_retry_count > 0)) && 16157 (un->un_startstop_timeid == NULL)) { 16158 scsi_log(SD_DEVINFO(un), sd_label, 16159 CE_WARN, "logical unit not ready, " 16160 "resetting disk\n"); 16161 sd_reset_target(un, pktp); 16162 } 16163 } else { 16164 if (((sd_level_mask & SD_LOGMASK_DIAG) || 16165 (xp->xb_nr_retry_count > 16166 un->un_reset_retry_count)) && 16167 (un->un_startstop_timeid == NULL)) { 16168 scsi_log(SD_DEVINFO(un), sd_label, 16169 CE_WARN, "logical unit not ready, " 16170 "resetting disk\n"); 16171 sd_reset_target(un, pktp); 16172 } 16173 } 16174 break; 16175 16176 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 16177 /* 16178 * If the target is in the process of becoming 16179 * ready, just proceed with the retry. This can 16180 * happen with CD-ROMs that take a long time to 16181 * read TOC after a power cycle or reset. 16182 */ 16183 goto do_retry; 16184 16185 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 16186 break; 16187 16188 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 16189 /* 16190 * Retries cannot help here so just fail right away. 16191 */ 16192 goto fail_command; 16193 16194 case 0x88: 16195 /* 16196 * Vendor-unique code for T3/T4: it indicates a 16197 * path problem in a mutipathed config, but as far as 16198 * the target driver is concerned it equates to a fatal 16199 * error, so we should just fail the command right away 16200 * (without printing anything to the console). If this 16201 * is not a T3/T4, fall thru to the default recovery 16202 * action. 16203 * T3/T4 is FC only, don't need to check is_fibre 16204 */ 16205 if (SD_IS_T3(un) || SD_IS_T4(un)) { 16206 sd_return_failed_command(un, bp, EIO); 16207 return; 16208 } 16209 /* FALLTHRU */ 16210 16211 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 16212 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 16213 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 16214 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 16215 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 16216 default: /* Possible future codes in SCSI spec? */ 16217 /* 16218 * For removable-media devices, do not retry if 16219 * ASCQ > 2 as these result mostly from USCSI commands 16220 * on MMC devices issued to check status of an 16221 * operation initiated in immediate mode. Also for 16222 * ASCQ >= 4 do not print console messages as these 16223 * mainly represent a user-initiated operation 16224 * instead of a system failure. 16225 */ 16226 if (un->un_f_has_removable_media) { 16227 si.ssi_severity = SCSI_ERR_ALL; 16228 goto fail_command; 16229 } 16230 break; 16231 } 16232 16233 /* 16234 * As part of our recovery attempt for the NOT READY 16235 * condition, we issue a START STOP UNIT command. However 16236 * we want to wait for a short delay before attempting this 16237 * as there may still be more commands coming back from the 16238 * target with the check condition. To do this we use 16239 * timeout(9F) to call sd_start_stop_unit_callback() after 16240 * the delay interval expires. (sd_start_stop_unit_callback() 16241 * dispatches sd_start_stop_unit_task(), which will issue 16242 * the actual START STOP UNIT command. The delay interval 16243 * is one-half of the delay that we will use to retry the 16244 * command that generated the NOT READY condition. 16245 * 16246 * Note that we could just dispatch sd_start_stop_unit_task() 16247 * from here and allow it to sleep for the delay interval, 16248 * but then we would be tying up the taskq thread 16249 * uncesessarily for the duration of the delay. 16250 * 16251 * Do not issue the START STOP UNIT if the current command 16252 * is already a START STOP UNIT. 16253 */ 16254 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 16255 break; 16256 } 16257 16258 /* 16259 * Do not schedule the timeout if one is already pending. 16260 */ 16261 if (un->un_startstop_timeid != NULL) { 16262 SD_INFO(SD_LOG_ERROR, un, 16263 "sd_sense_key_not_ready: restart already issued to" 16264 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 16265 ddi_get_instance(SD_DEVINFO(un))); 16266 break; 16267 } 16268 16269 /* 16270 * Schedule the START STOP UNIT command, then queue the command 16271 * for a retry. 16272 * 16273 * Note: A timeout is not scheduled for this retry because we 16274 * want the retry to be serial with the START_STOP_UNIT. The 16275 * retry will be started when the START_STOP_UNIT is completed 16276 * in sd_start_stop_unit_task. 16277 */ 16278 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 16279 un, SD_BSY_TIMEOUT / 2); 16280 xp->xb_nr_retry_count++; 16281 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 16282 return; 16283 16284 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 16285 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16286 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16287 "unit does not respond to selection\n"); 16288 } 16289 break; 16290 16291 case 0x3A: /* MEDIUM NOT PRESENT */ 16292 if (sd_error_level >= SCSI_ERR_FATAL) { 16293 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16294 "Caddy not inserted in drive\n"); 16295 } 16296 16297 sr_ejected(un); 16298 un->un_mediastate = DKIO_EJECTED; 16299 /* The state has changed, inform the media watch routines */ 16300 cv_broadcast(&un->un_state_cv); 16301 /* Just fail if no media is present in the drive. */ 16302 goto fail_command; 16303 16304 default: 16305 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16306 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 16307 "Unit not Ready. Additional sense code 0x%x\n", 16308 asc); 16309 } 16310 break; 16311 } 16312 16313 do_retry: 16314 16315 /* 16316 * Retry the command, as some targets may report NOT READY for 16317 * several seconds after being reset. 16318 */ 16319 xp->xb_nr_retry_count++; 16320 si.ssi_severity = SCSI_ERR_RETRYABLE; 16321 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 16322 &si, EIO, SD_BSY_TIMEOUT, NULL); 16323 16324 return; 16325 16326 fail_command: 16327 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16328 sd_return_failed_command(un, bp, EIO); 16329 } 16330 16331 16332 16333 /* 16334 * Function: sd_sense_key_medium_or_hardware_error 16335 * 16336 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 16337 * sense key. 16338 * 16339 * Context: May be called from interrupt context 16340 */ 16341 16342 static void 16343 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 16344 uint8_t *sense_datap, 16345 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16346 { 16347 struct sd_sense_info si; 16348 uint8_t sense_key = scsi_sense_key(sense_datap); 16349 uint8_t asc = scsi_sense_asc(sense_datap); 16350 16351 ASSERT(un != NULL); 16352 ASSERT(mutex_owned(SD_MUTEX(un))); 16353 ASSERT(bp != NULL); 16354 ASSERT(xp != NULL); 16355 ASSERT(pktp != NULL); 16356 16357 si.ssi_severity = SCSI_ERR_FATAL; 16358 si.ssi_pfa_flag = FALSE; 16359 16360 if (sense_key == KEY_MEDIUM_ERROR) { 16361 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 16362 } 16363 16364 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16365 16366 if ((un->un_reset_retry_count != 0) && 16367 (xp->xb_retry_count == un->un_reset_retry_count)) { 16368 mutex_exit(SD_MUTEX(un)); 16369 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 16370 if (un->un_f_allow_bus_device_reset == TRUE) { 16371 16372 boolean_t try_resetting_target = B_TRUE; 16373 16374 /* 16375 * We need to be able to handle specific ASC when we are 16376 * handling a KEY_HARDWARE_ERROR. In particular 16377 * taking the default action of resetting the target may 16378 * not be the appropriate way to attempt recovery. 16379 * Resetting a target because of a single LUN failure 16380 * victimizes all LUNs on that target. 16381 * 16382 * This is true for the LSI arrays, if an LSI 16383 * array controller returns an ASC of 0x84 (LUN Dead) we 16384 * should trust it. 16385 */ 16386 16387 if (sense_key == KEY_HARDWARE_ERROR) { 16388 switch (asc) { 16389 case 0x84: 16390 if (SD_IS_LSI(un)) { 16391 try_resetting_target = B_FALSE; 16392 } 16393 break; 16394 default: 16395 break; 16396 } 16397 } 16398 16399 if (try_resetting_target == B_TRUE) { 16400 int reset_retval = 0; 16401 if (un->un_f_lun_reset_enabled == TRUE) { 16402 SD_TRACE(SD_LOG_IO_CORE, un, 16403 "sd_sense_key_medium_or_hardware_" 16404 "error: issuing RESET_LUN\n"); 16405 reset_retval = 16406 scsi_reset(SD_ADDRESS(un), 16407 RESET_LUN); 16408 } 16409 if (reset_retval == 0) { 16410 SD_TRACE(SD_LOG_IO_CORE, un, 16411 "sd_sense_key_medium_or_hardware_" 16412 "error: issuing RESET_TARGET\n"); 16413 (void) scsi_reset(SD_ADDRESS(un), 16414 RESET_TARGET); 16415 } 16416 } 16417 } 16418 mutex_enter(SD_MUTEX(un)); 16419 } 16420 16421 /* 16422 * This really ought to be a fatal error, but we will retry anyway 16423 * as some drives report this as a spurious error. 16424 */ 16425 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16426 &si, EIO, (clock_t)0, NULL); 16427 } 16428 16429 16430 16431 /* 16432 * Function: sd_sense_key_illegal_request 16433 * 16434 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 16435 * 16436 * Context: May be called from interrupt context 16437 */ 16438 16439 static void 16440 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 16441 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16442 { 16443 struct sd_sense_info si; 16444 16445 ASSERT(un != NULL); 16446 ASSERT(mutex_owned(SD_MUTEX(un))); 16447 ASSERT(bp != NULL); 16448 ASSERT(xp != NULL); 16449 ASSERT(pktp != NULL); 16450 16451 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 16452 16453 si.ssi_severity = SCSI_ERR_INFO; 16454 si.ssi_pfa_flag = FALSE; 16455 16456 /* Pointless to retry if the target thinks it's an illegal request */ 16457 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16458 sd_return_failed_command(un, bp, EIO); 16459 } 16460 16461 16462 16463 16464 /* 16465 * Function: sd_sense_key_unit_attention 16466 * 16467 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 16468 * 16469 * Context: May be called from interrupt context 16470 */ 16471 16472 static void 16473 sd_sense_key_unit_attention(struct sd_lun *un, 16474 uint8_t *sense_datap, 16475 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16476 { 16477 /* 16478 * For UNIT ATTENTION we allow retries for one minute. Devices 16479 * like Sonoma can return UNIT ATTENTION close to a minute 16480 * under certain conditions. 16481 */ 16482 int retry_check_flag = SD_RETRIES_UA; 16483 boolean_t kstat_updated = B_FALSE; 16484 struct sd_sense_info si; 16485 uint8_t asc = scsi_sense_asc(sense_datap); 16486 uint8_t ascq = scsi_sense_ascq(sense_datap); 16487 16488 ASSERT(un != NULL); 16489 ASSERT(mutex_owned(SD_MUTEX(un))); 16490 ASSERT(bp != NULL); 16491 ASSERT(xp != NULL); 16492 ASSERT(pktp != NULL); 16493 16494 si.ssi_severity = SCSI_ERR_INFO; 16495 si.ssi_pfa_flag = FALSE; 16496 16497 16498 switch (asc) { 16499 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 16500 if (sd_report_pfa != 0) { 16501 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16502 si.ssi_pfa_flag = TRUE; 16503 retry_check_flag = SD_RETRIES_STANDARD; 16504 goto do_retry; 16505 } 16506 16507 break; 16508 16509 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 16510 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 16511 un->un_resvd_status |= 16512 (SD_LOST_RESERVE | SD_WANT_RESERVE); 16513 } 16514 #ifdef _LP64 16515 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 16516 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 16517 un, KM_NOSLEEP) == 0) { 16518 /* 16519 * If we can't dispatch the task we'll just 16520 * live without descriptor sense. We can 16521 * try again on the next "unit attention" 16522 */ 16523 SD_ERROR(SD_LOG_ERROR, un, 16524 "sd_sense_key_unit_attention: " 16525 "Could not dispatch " 16526 "sd_reenable_dsense_task\n"); 16527 } 16528 } 16529 #endif /* _LP64 */ 16530 /* FALLTHRU */ 16531 16532 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 16533 if (!un->un_f_has_removable_media) { 16534 break; 16535 } 16536 16537 /* 16538 * When we get a unit attention from a removable-media device, 16539 * it may be in a state that will take a long time to recover 16540 * (e.g., from a reset). Since we are executing in interrupt 16541 * context here, we cannot wait around for the device to come 16542 * back. So hand this command off to sd_media_change_task() 16543 * for deferred processing under taskq thread context. (Note 16544 * that the command still may be failed if a problem is 16545 * encountered at a later time.) 16546 */ 16547 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 16548 KM_NOSLEEP) == 0) { 16549 /* 16550 * Cannot dispatch the request so fail the command. 16551 */ 16552 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16553 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16554 si.ssi_severity = SCSI_ERR_FATAL; 16555 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16556 sd_return_failed_command(un, bp, EIO); 16557 } 16558 16559 /* 16560 * If failed to dispatch sd_media_change_task(), we already 16561 * updated kstat. If succeed to dispatch sd_media_change_task(), 16562 * we should update kstat later if it encounters an error. So, 16563 * we update kstat_updated flag here. 16564 */ 16565 kstat_updated = B_TRUE; 16566 16567 /* 16568 * Either the command has been successfully dispatched to a 16569 * task Q for retrying, or the dispatch failed. In either case 16570 * do NOT retry again by calling sd_retry_command. This sets up 16571 * two retries of the same command and when one completes and 16572 * frees the resources the other will access freed memory, 16573 * a bad thing. 16574 */ 16575 return; 16576 16577 default: 16578 break; 16579 } 16580 16581 /* 16582 * ASC ASCQ 16583 * 2A 09 Capacity data has changed 16584 * 2A 01 Mode parameters changed 16585 * 3F 0E Reported luns data has changed 16586 * Arrays that support logical unit expansion should report 16587 * capacity changes(2Ah/09). Mode parameters changed and 16588 * reported luns data has changed are the approximation. 16589 */ 16590 if (((asc == 0x2a) && (ascq == 0x09)) || 16591 ((asc == 0x2a) && (ascq == 0x01)) || 16592 ((asc == 0x3f) && (ascq == 0x0e))) { 16593 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 16594 KM_NOSLEEP) == 0) { 16595 SD_ERROR(SD_LOG_ERROR, un, 16596 "sd_sense_key_unit_attention: " 16597 "Could not dispatch sd_target_change_task\n"); 16598 } 16599 } 16600 16601 /* 16602 * Update kstat if we haven't done that. 16603 */ 16604 if (!kstat_updated) { 16605 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16606 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16607 } 16608 16609 do_retry: 16610 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 16611 EIO, SD_UA_RETRY_DELAY, NULL); 16612 } 16613 16614 16615 16616 /* 16617 * Function: sd_sense_key_fail_command 16618 * 16619 * Description: Use to fail a command when we don't like the sense key that 16620 * was returned. 16621 * 16622 * Context: May be called from interrupt context 16623 */ 16624 16625 static void 16626 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 16627 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16628 { 16629 struct sd_sense_info si; 16630 16631 ASSERT(un != NULL); 16632 ASSERT(mutex_owned(SD_MUTEX(un))); 16633 ASSERT(bp != NULL); 16634 ASSERT(xp != NULL); 16635 ASSERT(pktp != NULL); 16636 16637 si.ssi_severity = SCSI_ERR_FATAL; 16638 si.ssi_pfa_flag = FALSE; 16639 16640 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16641 sd_return_failed_command(un, bp, EIO); 16642 } 16643 16644 16645 16646 /* 16647 * Function: sd_sense_key_blank_check 16648 * 16649 * Description: Recovery actions for a SCSI "Blank Check" sense key. 16650 * Has no monetary connotation. 16651 * 16652 * Context: May be called from interrupt context 16653 */ 16654 16655 static void 16656 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 16657 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16658 { 16659 struct sd_sense_info si; 16660 16661 ASSERT(un != NULL); 16662 ASSERT(mutex_owned(SD_MUTEX(un))); 16663 ASSERT(bp != NULL); 16664 ASSERT(xp != NULL); 16665 ASSERT(pktp != NULL); 16666 16667 /* 16668 * Blank check is not fatal for removable devices, therefore 16669 * it does not require a console message. 16670 */ 16671 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 16672 SCSI_ERR_FATAL; 16673 si.ssi_pfa_flag = FALSE; 16674 16675 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16676 sd_return_failed_command(un, bp, EIO); 16677 } 16678 16679 16680 16681 16682 /* 16683 * Function: sd_sense_key_aborted_command 16684 * 16685 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 16686 * 16687 * Context: May be called from interrupt context 16688 */ 16689 16690 static void 16691 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 16692 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16693 { 16694 struct sd_sense_info si; 16695 16696 ASSERT(un != NULL); 16697 ASSERT(mutex_owned(SD_MUTEX(un))); 16698 ASSERT(bp != NULL); 16699 ASSERT(xp != NULL); 16700 ASSERT(pktp != NULL); 16701 16702 si.ssi_severity = SCSI_ERR_FATAL; 16703 si.ssi_pfa_flag = FALSE; 16704 16705 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16706 16707 /* 16708 * This really ought to be a fatal error, but we will retry anyway 16709 * as some drives report this as a spurious error. 16710 */ 16711 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16712 &si, EIO, drv_usectohz(100000), NULL); 16713 } 16714 16715 16716 16717 /* 16718 * Function: sd_sense_key_default 16719 * 16720 * Description: Default recovery action for several SCSI sense keys (basically 16721 * attempts a retry). 16722 * 16723 * Context: May be called from interrupt context 16724 */ 16725 16726 static void 16727 sd_sense_key_default(struct sd_lun *un, 16728 uint8_t *sense_datap, 16729 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16730 { 16731 struct sd_sense_info si; 16732 uint8_t sense_key = scsi_sense_key(sense_datap); 16733 16734 ASSERT(un != NULL); 16735 ASSERT(mutex_owned(SD_MUTEX(un))); 16736 ASSERT(bp != NULL); 16737 ASSERT(xp != NULL); 16738 ASSERT(pktp != NULL); 16739 16740 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16741 16742 /* 16743 * Undecoded sense key. Attempt retries and hope that will fix 16744 * the problem. Otherwise, we're dead. 16745 */ 16746 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16747 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16748 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 16749 } 16750 16751 si.ssi_severity = SCSI_ERR_FATAL; 16752 si.ssi_pfa_flag = FALSE; 16753 16754 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16755 &si, EIO, (clock_t)0, NULL); 16756 } 16757 16758 16759 16760 /* 16761 * Function: sd_print_retry_msg 16762 * 16763 * Description: Print a message indicating the retry action being taken. 16764 * 16765 * Arguments: un - ptr to associated softstate 16766 * bp - ptr to buf(9S) for the command 16767 * arg - not used. 16768 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16769 * or SD_NO_RETRY_ISSUED 16770 * 16771 * Context: May be called from interrupt context 16772 */ 16773 /* ARGSUSED */ 16774 static void 16775 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 16776 { 16777 struct sd_xbuf *xp; 16778 struct scsi_pkt *pktp; 16779 char *reasonp; 16780 char *msgp; 16781 16782 ASSERT(un != NULL); 16783 ASSERT(mutex_owned(SD_MUTEX(un))); 16784 ASSERT(bp != NULL); 16785 pktp = SD_GET_PKTP(bp); 16786 ASSERT(pktp != NULL); 16787 xp = SD_GET_XBUF(bp); 16788 ASSERT(xp != NULL); 16789 16790 ASSERT(!mutex_owned(&un->un_pm_mutex)); 16791 mutex_enter(&un->un_pm_mutex); 16792 if ((un->un_state == SD_STATE_SUSPENDED) || 16793 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 16794 (pktp->pkt_flags & FLAG_SILENT)) { 16795 mutex_exit(&un->un_pm_mutex); 16796 goto update_pkt_reason; 16797 } 16798 mutex_exit(&un->un_pm_mutex); 16799 16800 /* 16801 * Suppress messages if they are all the same pkt_reason; with 16802 * TQ, many (up to 256) are returned with the same pkt_reason. 16803 * If we are in panic, then suppress the retry messages. 16804 */ 16805 switch (flag) { 16806 case SD_NO_RETRY_ISSUED: 16807 msgp = "giving up"; 16808 break; 16809 case SD_IMMEDIATE_RETRY_ISSUED: 16810 case SD_DELAYED_RETRY_ISSUED: 16811 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 16812 ((pktp->pkt_reason == un->un_last_pkt_reason) && 16813 (sd_error_level != SCSI_ERR_ALL))) { 16814 return; 16815 } 16816 msgp = "retrying command"; 16817 break; 16818 default: 16819 goto update_pkt_reason; 16820 } 16821 16822 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 16823 scsi_rname(pktp->pkt_reason)); 16824 16825 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16826 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 16827 16828 update_pkt_reason: 16829 /* 16830 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 16831 * This is to prevent multiple console messages for the same failure 16832 * condition. Note that un->un_last_pkt_reason is NOT restored if & 16833 * when the command is retried successfully because there still may be 16834 * more commands coming back with the same value of pktp->pkt_reason. 16835 */ 16836 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 16837 un->un_last_pkt_reason = pktp->pkt_reason; 16838 } 16839 } 16840 16841 16842 /* 16843 * Function: sd_print_cmd_incomplete_msg 16844 * 16845 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 16846 * 16847 * Arguments: un - ptr to associated softstate 16848 * bp - ptr to buf(9S) for the command 16849 * arg - passed to sd_print_retry_msg() 16850 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16851 * or SD_NO_RETRY_ISSUED 16852 * 16853 * Context: May be called from interrupt context 16854 */ 16855 16856 static void 16857 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 16858 int code) 16859 { 16860 dev_info_t *dip; 16861 16862 ASSERT(un != NULL); 16863 ASSERT(mutex_owned(SD_MUTEX(un))); 16864 ASSERT(bp != NULL); 16865 16866 switch (code) { 16867 case SD_NO_RETRY_ISSUED: 16868 /* Command was failed. Someone turned off this target? */ 16869 if (un->un_state != SD_STATE_OFFLINE) { 16870 /* 16871 * Suppress message if we are detaching and 16872 * device has been disconnected 16873 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 16874 * private interface and not part of the DDI 16875 */ 16876 dip = un->un_sd->sd_dev; 16877 if (!(DEVI_IS_DETACHING(dip) && 16878 DEVI_IS_DEVICE_REMOVED(dip))) { 16879 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16880 "disk not responding to selection\n"); 16881 } 16882 New_state(un, SD_STATE_OFFLINE); 16883 } 16884 break; 16885 16886 case SD_DELAYED_RETRY_ISSUED: 16887 case SD_IMMEDIATE_RETRY_ISSUED: 16888 default: 16889 /* Command was successfully queued for retry */ 16890 sd_print_retry_msg(un, bp, arg, code); 16891 break; 16892 } 16893 } 16894 16895 16896 /* 16897 * Function: sd_pkt_reason_cmd_incomplete 16898 * 16899 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 16900 * 16901 * Context: May be called from interrupt context 16902 */ 16903 16904 static void 16905 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 16906 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16907 { 16908 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 16909 16910 ASSERT(un != NULL); 16911 ASSERT(mutex_owned(SD_MUTEX(un))); 16912 ASSERT(bp != NULL); 16913 ASSERT(xp != NULL); 16914 ASSERT(pktp != NULL); 16915 16916 /* Do not do a reset if selection did not complete */ 16917 /* Note: Should this not just check the bit? */ 16918 if (pktp->pkt_state != STATE_GOT_BUS) { 16919 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16920 sd_reset_target(un, pktp); 16921 } 16922 16923 /* 16924 * If the target was not successfully selected, then set 16925 * SD_RETRIES_FAILFAST to indicate that we lost communication 16926 * with the target, and further retries and/or commands are 16927 * likely to take a long time. 16928 */ 16929 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 16930 flag |= SD_RETRIES_FAILFAST; 16931 } 16932 16933 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16934 16935 sd_retry_command(un, bp, flag, 16936 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16937 } 16938 16939 16940 16941 /* 16942 * Function: sd_pkt_reason_cmd_tran_err 16943 * 16944 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 16945 * 16946 * Context: May be called from interrupt context 16947 */ 16948 16949 static void 16950 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 16951 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16952 { 16953 ASSERT(un != NULL); 16954 ASSERT(mutex_owned(SD_MUTEX(un))); 16955 ASSERT(bp != NULL); 16956 ASSERT(xp != NULL); 16957 ASSERT(pktp != NULL); 16958 16959 /* 16960 * Do not reset if we got a parity error, or if 16961 * selection did not complete. 16962 */ 16963 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16964 /* Note: Should this not just check the bit for pkt_state? */ 16965 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 16966 (pktp->pkt_state != STATE_GOT_BUS)) { 16967 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16968 sd_reset_target(un, pktp); 16969 } 16970 16971 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16972 16973 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16974 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16975 } 16976 16977 16978 16979 /* 16980 * Function: sd_pkt_reason_cmd_reset 16981 * 16982 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 16983 * 16984 * Context: May be called from interrupt context 16985 */ 16986 16987 static void 16988 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 16989 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16990 { 16991 ASSERT(un != NULL); 16992 ASSERT(mutex_owned(SD_MUTEX(un))); 16993 ASSERT(bp != NULL); 16994 ASSERT(xp != NULL); 16995 ASSERT(pktp != NULL); 16996 16997 /* The target may still be running the command, so try to reset. */ 16998 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16999 sd_reset_target(un, pktp); 17000 17001 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17002 17003 /* 17004 * If pkt_reason is CMD_RESET chances are that this pkt got 17005 * reset because another target on this bus caused it. The target 17006 * that caused it should get CMD_TIMEOUT with pkt_statistics 17007 * of STAT_TIMEOUT/STAT_DEV_RESET. 17008 */ 17009 17010 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17011 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17012 } 17013 17014 17015 17016 17017 /* 17018 * Function: sd_pkt_reason_cmd_aborted 17019 * 17020 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 17021 * 17022 * Context: May be called from interrupt context 17023 */ 17024 17025 static void 17026 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 17027 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17028 { 17029 ASSERT(un != NULL); 17030 ASSERT(mutex_owned(SD_MUTEX(un))); 17031 ASSERT(bp != NULL); 17032 ASSERT(xp != NULL); 17033 ASSERT(pktp != NULL); 17034 17035 /* The target may still be running the command, so try to reset. */ 17036 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17037 sd_reset_target(un, pktp); 17038 17039 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17040 17041 /* 17042 * If pkt_reason is CMD_ABORTED chances are that this pkt got 17043 * aborted because another target on this bus caused it. The target 17044 * that caused it should get CMD_TIMEOUT with pkt_statistics 17045 * of STAT_TIMEOUT/STAT_DEV_RESET. 17046 */ 17047 17048 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17049 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17050 } 17051 17052 17053 17054 /* 17055 * Function: sd_pkt_reason_cmd_timeout 17056 * 17057 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 17058 * 17059 * Context: May be called from interrupt context 17060 */ 17061 17062 static void 17063 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 17064 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17065 { 17066 ASSERT(un != NULL); 17067 ASSERT(mutex_owned(SD_MUTEX(un))); 17068 ASSERT(bp != NULL); 17069 ASSERT(xp != NULL); 17070 ASSERT(pktp != NULL); 17071 17072 17073 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17074 sd_reset_target(un, pktp); 17075 17076 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17077 17078 /* 17079 * A command timeout indicates that we could not establish 17080 * communication with the target, so set SD_RETRIES_FAILFAST 17081 * as further retries/commands are likely to take a long time. 17082 */ 17083 sd_retry_command(un, bp, 17084 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 17085 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17086 } 17087 17088 17089 17090 /* 17091 * Function: sd_pkt_reason_cmd_unx_bus_free 17092 * 17093 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 17094 * 17095 * Context: May be called from interrupt context 17096 */ 17097 17098 static void 17099 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 17100 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17101 { 17102 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 17103 17104 ASSERT(un != NULL); 17105 ASSERT(mutex_owned(SD_MUTEX(un))); 17106 ASSERT(bp != NULL); 17107 ASSERT(xp != NULL); 17108 ASSERT(pktp != NULL); 17109 17110 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17111 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17112 17113 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 17114 sd_print_retry_msg : NULL; 17115 17116 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17117 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17118 } 17119 17120 17121 /* 17122 * Function: sd_pkt_reason_cmd_tag_reject 17123 * 17124 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 17125 * 17126 * Context: May be called from interrupt context 17127 */ 17128 17129 static void 17130 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 17131 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17132 { 17133 ASSERT(un != NULL); 17134 ASSERT(mutex_owned(SD_MUTEX(un))); 17135 ASSERT(bp != NULL); 17136 ASSERT(xp != NULL); 17137 ASSERT(pktp != NULL); 17138 17139 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17140 pktp->pkt_flags = 0; 17141 un->un_tagflags = 0; 17142 if (un->un_f_opt_queueing == TRUE) { 17143 un->un_throttle = min(un->un_throttle, 3); 17144 } else { 17145 un->un_throttle = 1; 17146 } 17147 mutex_exit(SD_MUTEX(un)); 17148 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 17149 mutex_enter(SD_MUTEX(un)); 17150 17151 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17152 17153 /* Legacy behavior not to check retry counts here. */ 17154 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 17155 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17156 } 17157 17158 17159 /* 17160 * Function: sd_pkt_reason_default 17161 * 17162 * Description: Default recovery actions for SCSA pkt_reason values that 17163 * do not have more explicit recovery actions. 17164 * 17165 * Context: May be called from interrupt context 17166 */ 17167 17168 static void 17169 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 17170 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17171 { 17172 ASSERT(un != NULL); 17173 ASSERT(mutex_owned(SD_MUTEX(un))); 17174 ASSERT(bp != NULL); 17175 ASSERT(xp != NULL); 17176 ASSERT(pktp != NULL); 17177 17178 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17179 sd_reset_target(un, pktp); 17180 17181 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17182 17183 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17184 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17185 } 17186 17187 17188 17189 /* 17190 * Function: sd_pkt_status_check_condition 17191 * 17192 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 17193 * 17194 * Context: May be called from interrupt context 17195 */ 17196 17197 static void 17198 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 17199 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17200 { 17201 ASSERT(un != NULL); 17202 ASSERT(mutex_owned(SD_MUTEX(un))); 17203 ASSERT(bp != NULL); 17204 ASSERT(xp != NULL); 17205 ASSERT(pktp != NULL); 17206 17207 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 17208 "entry: buf:0x%p xp:0x%p\n", bp, xp); 17209 17210 /* 17211 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 17212 * command will be retried after the request sense). Otherwise, retry 17213 * the command. Note: we are issuing the request sense even though the 17214 * retry limit may have been reached for the failed command. 17215 */ 17216 if (un->un_f_arq_enabled == FALSE) { 17217 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17218 "no ARQ, sending request sense command\n"); 17219 sd_send_request_sense_command(un, bp, pktp); 17220 } else { 17221 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17222 "ARQ,retrying request sense command\n"); 17223 #if defined(__i386) || defined(__amd64) 17224 /* 17225 * The SD_RETRY_DELAY value need to be adjusted here 17226 * when SD_RETRY_DELAY change in sddef.h 17227 */ 17228 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17229 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 17230 NULL); 17231 #else 17232 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 17233 EIO, SD_RETRY_DELAY, NULL); 17234 #endif 17235 } 17236 17237 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 17238 } 17239 17240 17241 /* 17242 * Function: sd_pkt_status_busy 17243 * 17244 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 17245 * 17246 * Context: May be called from interrupt context 17247 */ 17248 17249 static void 17250 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17251 struct scsi_pkt *pktp) 17252 { 17253 ASSERT(un != NULL); 17254 ASSERT(mutex_owned(SD_MUTEX(un))); 17255 ASSERT(bp != NULL); 17256 ASSERT(xp != NULL); 17257 ASSERT(pktp != NULL); 17258 17259 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17260 "sd_pkt_status_busy: entry\n"); 17261 17262 /* If retries are exhausted, just fail the command. */ 17263 if (xp->xb_retry_count >= un->un_busy_retry_count) { 17264 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17265 "device busy too long\n"); 17266 sd_return_failed_command(un, bp, EIO); 17267 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17268 "sd_pkt_status_busy: exit\n"); 17269 return; 17270 } 17271 xp->xb_retry_count++; 17272 17273 /* 17274 * Try to reset the target. However, we do not want to perform 17275 * more than one reset if the device continues to fail. The reset 17276 * will be performed when the retry count reaches the reset 17277 * threshold. This threshold should be set such that at least 17278 * one retry is issued before the reset is performed. 17279 */ 17280 if (xp->xb_retry_count == 17281 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 17282 int rval = 0; 17283 mutex_exit(SD_MUTEX(un)); 17284 if (un->un_f_allow_bus_device_reset == TRUE) { 17285 /* 17286 * First try to reset the LUN; if we cannot then 17287 * try to reset the target. 17288 */ 17289 if (un->un_f_lun_reset_enabled == TRUE) { 17290 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17291 "sd_pkt_status_busy: RESET_LUN\n"); 17292 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17293 } 17294 if (rval == 0) { 17295 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17296 "sd_pkt_status_busy: RESET_TARGET\n"); 17297 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17298 } 17299 } 17300 if (rval == 0) { 17301 /* 17302 * If the RESET_LUN and/or RESET_TARGET failed, 17303 * try RESET_ALL 17304 */ 17305 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17306 "sd_pkt_status_busy: RESET_ALL\n"); 17307 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 17308 } 17309 mutex_enter(SD_MUTEX(un)); 17310 if (rval == 0) { 17311 /* 17312 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 17313 * At this point we give up & fail the command. 17314 */ 17315 sd_return_failed_command(un, bp, EIO); 17316 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17317 "sd_pkt_status_busy: exit (failed cmd)\n"); 17318 return; 17319 } 17320 } 17321 17322 /* 17323 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 17324 * we have already checked the retry counts above. 17325 */ 17326 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 17327 EIO, SD_BSY_TIMEOUT, NULL); 17328 17329 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17330 "sd_pkt_status_busy: exit\n"); 17331 } 17332 17333 17334 /* 17335 * Function: sd_pkt_status_reservation_conflict 17336 * 17337 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 17338 * command status. 17339 * 17340 * Context: May be called from interrupt context 17341 */ 17342 17343 static void 17344 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 17345 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17346 { 17347 ASSERT(un != NULL); 17348 ASSERT(mutex_owned(SD_MUTEX(un))); 17349 ASSERT(bp != NULL); 17350 ASSERT(xp != NULL); 17351 ASSERT(pktp != NULL); 17352 17353 /* 17354 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 17355 * conflict could be due to various reasons like incorrect keys, not 17356 * registered or not reserved etc. So, we return EACCES to the caller. 17357 */ 17358 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 17359 int cmd = SD_GET_PKT_OPCODE(pktp); 17360 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 17361 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 17362 sd_return_failed_command(un, bp, EACCES); 17363 return; 17364 } 17365 } 17366 17367 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 17368 17369 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 17370 if (sd_failfast_enable != 0) { 17371 /* By definition, we must panic here.... */ 17372 sd_panic_for_res_conflict(un); 17373 /*NOTREACHED*/ 17374 } 17375 SD_ERROR(SD_LOG_IO, un, 17376 "sd_handle_resv_conflict: Disk Reserved\n"); 17377 sd_return_failed_command(un, bp, EACCES); 17378 return; 17379 } 17380 17381 /* 17382 * 1147670: retry only if sd_retry_on_reservation_conflict 17383 * property is set (default is 1). Retries will not succeed 17384 * on a disk reserved by another initiator. HA systems 17385 * may reset this via sd.conf to avoid these retries. 17386 * 17387 * Note: The legacy return code for this failure is EIO, however EACCES 17388 * seems more appropriate for a reservation conflict. 17389 */ 17390 if (sd_retry_on_reservation_conflict == 0) { 17391 SD_ERROR(SD_LOG_IO, un, 17392 "sd_handle_resv_conflict: Device Reserved\n"); 17393 sd_return_failed_command(un, bp, EIO); 17394 return; 17395 } 17396 17397 /* 17398 * Retry the command if we can. 17399 * 17400 * Note: The legacy return code for this failure is EIO, however EACCES 17401 * seems more appropriate for a reservation conflict. 17402 */ 17403 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17404 (clock_t)2, NULL); 17405 } 17406 17407 17408 17409 /* 17410 * Function: sd_pkt_status_qfull 17411 * 17412 * Description: Handle a QUEUE FULL condition from the target. This can 17413 * occur if the HBA does not handle the queue full condition. 17414 * (Basically this means third-party HBAs as Sun HBAs will 17415 * handle the queue full condition.) Note that if there are 17416 * some commands already in the transport, then the queue full 17417 * has occurred because the queue for this nexus is actually 17418 * full. If there are no commands in the transport, then the 17419 * queue full is resulting from some other initiator or lun 17420 * consuming all the resources at the target. 17421 * 17422 * Context: May be called from interrupt context 17423 */ 17424 17425 static void 17426 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 17427 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17428 { 17429 ASSERT(un != NULL); 17430 ASSERT(mutex_owned(SD_MUTEX(un))); 17431 ASSERT(bp != NULL); 17432 ASSERT(xp != NULL); 17433 ASSERT(pktp != NULL); 17434 17435 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17436 "sd_pkt_status_qfull: entry\n"); 17437 17438 /* 17439 * Just lower the QFULL throttle and retry the command. Note that 17440 * we do not limit the number of retries here. 17441 */ 17442 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 17443 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 17444 SD_RESTART_TIMEOUT, NULL); 17445 17446 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17447 "sd_pkt_status_qfull: exit\n"); 17448 } 17449 17450 17451 /* 17452 * Function: sd_reset_target 17453 * 17454 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 17455 * RESET_TARGET, or RESET_ALL. 17456 * 17457 * Context: May be called under interrupt context. 17458 */ 17459 17460 static void 17461 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 17462 { 17463 int rval = 0; 17464 17465 ASSERT(un != NULL); 17466 ASSERT(mutex_owned(SD_MUTEX(un))); 17467 ASSERT(pktp != NULL); 17468 17469 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 17470 17471 /* 17472 * No need to reset if the transport layer has already done so. 17473 */ 17474 if ((pktp->pkt_statistics & 17475 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 17476 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17477 "sd_reset_target: no reset\n"); 17478 return; 17479 } 17480 17481 mutex_exit(SD_MUTEX(un)); 17482 17483 if (un->un_f_allow_bus_device_reset == TRUE) { 17484 if (un->un_f_lun_reset_enabled == TRUE) { 17485 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17486 "sd_reset_target: RESET_LUN\n"); 17487 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17488 } 17489 if (rval == 0) { 17490 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17491 "sd_reset_target: RESET_TARGET\n"); 17492 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17493 } 17494 } 17495 17496 if (rval == 0) { 17497 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17498 "sd_reset_target: RESET_ALL\n"); 17499 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 17500 } 17501 17502 mutex_enter(SD_MUTEX(un)); 17503 17504 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 17505 } 17506 17507 /* 17508 * Function: sd_target_change_task 17509 * 17510 * Description: Handle dynamic target change 17511 * 17512 * Context: Executes in a taskq() thread context 17513 */ 17514 static void 17515 sd_target_change_task(void *arg) 17516 { 17517 struct sd_lun *un = arg; 17518 uint64_t capacity; 17519 diskaddr_t label_cap; 17520 uint_t lbasize; 17521 17522 ASSERT(un != NULL); 17523 ASSERT(!mutex_owned(SD_MUTEX(un))); 17524 17525 if ((un->un_f_blockcount_is_valid == FALSE) || 17526 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 17527 return; 17528 } 17529 17530 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 17531 &lbasize, SD_PATH_DIRECT) != 0) { 17532 SD_ERROR(SD_LOG_ERROR, un, 17533 "sd_target_change_task: fail to read capacity\n"); 17534 return; 17535 } 17536 17537 mutex_enter(SD_MUTEX(un)); 17538 if (capacity <= un->un_blockcount) { 17539 mutex_exit(SD_MUTEX(un)); 17540 return; 17541 } 17542 17543 sd_update_block_info(un, lbasize, capacity); 17544 mutex_exit(SD_MUTEX(un)); 17545 17546 /* 17547 * If lun is EFI labeled and lun capacity is greater than the 17548 * capacity contained in the label, log a sys event. 17549 */ 17550 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 17551 (void*)SD_PATH_DIRECT) == 0) { 17552 mutex_enter(SD_MUTEX(un)); 17553 if (un->un_f_blockcount_is_valid && 17554 un->un_blockcount > label_cap) { 17555 mutex_exit(SD_MUTEX(un)); 17556 sd_log_lun_expansion_event(un, KM_SLEEP); 17557 } else { 17558 mutex_exit(SD_MUTEX(un)); 17559 } 17560 } 17561 } 17562 17563 /* 17564 * Function: sd_log_lun_expansion_event 17565 * 17566 * Description: Log lun expansion sys event 17567 * 17568 * Context: Never called from interrupt context 17569 */ 17570 static void 17571 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 17572 { 17573 int err; 17574 char *path; 17575 nvlist_t *dle_attr_list; 17576 17577 /* Allocate and build sysevent attribute list */ 17578 err = nvlist_alloc(&dle_attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 17579 if (err != 0) { 17580 SD_ERROR(SD_LOG_ERROR, un, 17581 "sd_log_lun_expansion_event: fail to allocate space\n"); 17582 return; 17583 } 17584 17585 path = kmem_alloc(MAXPATHLEN, km_flag); 17586 if (path == NULL) { 17587 nvlist_free(dle_attr_list); 17588 SD_ERROR(SD_LOG_ERROR, un, 17589 "sd_log_lun_expansion_event: fail to allocate space\n"); 17590 return; 17591 } 17592 /* 17593 * Add path attribute to identify the lun. 17594 * We are using minor node 'a' as the sysevent attribute. 17595 */ 17596 (void) snprintf(path, MAXPATHLEN, "/devices"); 17597 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 17598 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 17599 ":a"); 17600 17601 err = nvlist_add_string(dle_attr_list, DEV_PHYS_PATH, path); 17602 if (err != 0) { 17603 nvlist_free(dle_attr_list); 17604 kmem_free(path, MAXPATHLEN); 17605 SD_ERROR(SD_LOG_ERROR, un, 17606 "sd_log_lun_expansion_event: fail to add attribute\n"); 17607 return; 17608 } 17609 17610 /* Log dynamic lun expansion sysevent */ 17611 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 17612 ESC_DEV_DLE, dle_attr_list, NULL, km_flag); 17613 if (err != DDI_SUCCESS) { 17614 SD_ERROR(SD_LOG_ERROR, un, 17615 "sd_log_lun_expansion_event: fail to log sysevent\n"); 17616 } 17617 17618 nvlist_free(dle_attr_list); 17619 kmem_free(path, MAXPATHLEN); 17620 } 17621 17622 /* 17623 * Function: sd_media_change_task 17624 * 17625 * Description: Recovery action for CDROM to become available. 17626 * 17627 * Context: Executes in a taskq() thread context 17628 */ 17629 17630 static void 17631 sd_media_change_task(void *arg) 17632 { 17633 struct scsi_pkt *pktp = arg; 17634 struct sd_lun *un; 17635 struct buf *bp; 17636 struct sd_xbuf *xp; 17637 int err = 0; 17638 int retry_count = 0; 17639 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 17640 struct sd_sense_info si; 17641 17642 ASSERT(pktp != NULL); 17643 bp = (struct buf *)pktp->pkt_private; 17644 ASSERT(bp != NULL); 17645 xp = SD_GET_XBUF(bp); 17646 ASSERT(xp != NULL); 17647 un = SD_GET_UN(bp); 17648 ASSERT(un != NULL); 17649 ASSERT(!mutex_owned(SD_MUTEX(un))); 17650 ASSERT(un->un_f_monitor_media_state); 17651 17652 si.ssi_severity = SCSI_ERR_INFO; 17653 si.ssi_pfa_flag = FALSE; 17654 17655 /* 17656 * When a reset is issued on a CDROM, it takes a long time to 17657 * recover. First few attempts to read capacity and other things 17658 * related to handling unit attention fail (with a ASC 0x4 and 17659 * ASCQ 0x1). In that case we want to do enough retries and we want 17660 * to limit the retries in other cases of genuine failures like 17661 * no media in drive. 17662 */ 17663 while (retry_count++ < retry_limit) { 17664 if ((err = sd_handle_mchange(un)) == 0) { 17665 break; 17666 } 17667 if (err == EAGAIN) { 17668 retry_limit = SD_UNIT_ATTENTION_RETRY; 17669 } 17670 /* Sleep for 0.5 sec. & try again */ 17671 delay(drv_usectohz(500000)); 17672 } 17673 17674 /* 17675 * Dispatch (retry or fail) the original command here, 17676 * along with appropriate console messages.... 17677 * 17678 * Must grab the mutex before calling sd_retry_command, 17679 * sd_print_sense_msg and sd_return_failed_command. 17680 */ 17681 mutex_enter(SD_MUTEX(un)); 17682 if (err != SD_CMD_SUCCESS) { 17683 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17684 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17685 si.ssi_severity = SCSI_ERR_FATAL; 17686 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17687 sd_return_failed_command(un, bp, EIO); 17688 } else { 17689 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17690 &si, EIO, (clock_t)0, NULL); 17691 } 17692 mutex_exit(SD_MUTEX(un)); 17693 } 17694 17695 17696 17697 /* 17698 * Function: sd_handle_mchange 17699 * 17700 * Description: Perform geometry validation & other recovery when CDROM 17701 * has been removed from drive. 17702 * 17703 * Return Code: 0 for success 17704 * errno-type return code of either sd_send_scsi_DOORLOCK() or 17705 * sd_send_scsi_READ_CAPACITY() 17706 * 17707 * Context: Executes in a taskq() thread context 17708 */ 17709 17710 static int 17711 sd_handle_mchange(struct sd_lun *un) 17712 { 17713 uint64_t capacity; 17714 uint32_t lbasize; 17715 int rval; 17716 17717 ASSERT(!mutex_owned(SD_MUTEX(un))); 17718 ASSERT(un->un_f_monitor_media_state); 17719 17720 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 17721 SD_PATH_DIRECT_PRIORITY)) != 0) { 17722 return (rval); 17723 } 17724 17725 mutex_enter(SD_MUTEX(un)); 17726 sd_update_block_info(un, lbasize, capacity); 17727 17728 if (un->un_errstats != NULL) { 17729 struct sd_errstats *stp = 17730 (struct sd_errstats *)un->un_errstats->ks_data; 17731 stp->sd_capacity.value.ui64 = (uint64_t) 17732 ((uint64_t)un->un_blockcount * 17733 (uint64_t)un->un_tgt_blocksize); 17734 } 17735 17736 17737 /* 17738 * Check if the media in the device is writable or not 17739 */ 17740 if (ISCD(un)) 17741 sd_check_for_writable_cd(un, SD_PATH_DIRECT_PRIORITY); 17742 17743 /* 17744 * Note: Maybe let the strategy/partitioning chain worry about getting 17745 * valid geometry. 17746 */ 17747 mutex_exit(SD_MUTEX(un)); 17748 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 17749 17750 17751 if (cmlb_validate(un->un_cmlbhandle, 0, 17752 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 17753 return (EIO); 17754 } else { 17755 if (un->un_f_pkstats_enabled) { 17756 sd_set_pstats(un); 17757 SD_TRACE(SD_LOG_IO_PARTITION, un, 17758 "sd_handle_mchange: un:0x%p pstats created and " 17759 "set\n", un); 17760 } 17761 } 17762 17763 17764 /* 17765 * Try to lock the door 17766 */ 17767 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 17768 SD_PATH_DIRECT_PRIORITY)); 17769 } 17770 17771 17772 /* 17773 * Function: sd_send_scsi_DOORLOCK 17774 * 17775 * Description: Issue the scsi DOOR LOCK command 17776 * 17777 * Arguments: un - pointer to driver soft state (unit) structure for 17778 * this target. 17779 * flag - SD_REMOVAL_ALLOW 17780 * SD_REMOVAL_PREVENT 17781 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17782 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17783 * to use the USCSI "direct" chain and bypass the normal 17784 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17785 * command is issued as part of an error recovery action. 17786 * 17787 * Return Code: 0 - Success 17788 * errno return code from sd_send_scsi_cmd() 17789 * 17790 * Context: Can sleep. 17791 */ 17792 17793 static int 17794 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 17795 { 17796 union scsi_cdb cdb; 17797 struct uscsi_cmd ucmd_buf; 17798 struct scsi_extended_sense sense_buf; 17799 int status; 17800 17801 ASSERT(un != NULL); 17802 ASSERT(!mutex_owned(SD_MUTEX(un))); 17803 17804 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 17805 17806 /* already determined doorlock is not supported, fake success */ 17807 if (un->un_f_doorlock_supported == FALSE) { 17808 return (0); 17809 } 17810 17811 /* 17812 * If we are ejecting and see an SD_REMOVAL_PREVENT 17813 * ignore the command so we can complete the eject 17814 * operation. 17815 */ 17816 if (flag == SD_REMOVAL_PREVENT) { 17817 mutex_enter(SD_MUTEX(un)); 17818 if (un->un_f_ejecting == TRUE) { 17819 mutex_exit(SD_MUTEX(un)); 17820 return (EAGAIN); 17821 } 17822 mutex_exit(SD_MUTEX(un)); 17823 } 17824 17825 bzero(&cdb, sizeof (cdb)); 17826 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17827 17828 cdb.scc_cmd = SCMD_DOORLOCK; 17829 cdb.cdb_opaque[4] = (uchar_t)flag; 17830 17831 ucmd_buf.uscsi_cdb = (char *)&cdb; 17832 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 17833 ucmd_buf.uscsi_bufaddr = NULL; 17834 ucmd_buf.uscsi_buflen = 0; 17835 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17836 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17837 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 17838 ucmd_buf.uscsi_timeout = 15; 17839 17840 SD_TRACE(SD_LOG_IO, un, 17841 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 17842 17843 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17844 UIO_SYSSPACE, path_flag); 17845 17846 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 17847 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17848 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 17849 /* fake success and skip subsequent doorlock commands */ 17850 un->un_f_doorlock_supported = FALSE; 17851 return (0); 17852 } 17853 17854 return (status); 17855 } 17856 17857 /* 17858 * Function: sd_send_scsi_READ_CAPACITY 17859 * 17860 * Description: This routine uses the scsi READ CAPACITY command to determine 17861 * the device capacity in number of blocks and the device native 17862 * block size. If this function returns a failure, then the 17863 * values in *capp and *lbap are undefined. If the capacity 17864 * returned is 0xffffffff then the lun is too large for a 17865 * normal READ CAPACITY command and the results of a 17866 * READ CAPACITY 16 will be used instead. 17867 * 17868 * Arguments: un - ptr to soft state struct for the target 17869 * capp - ptr to unsigned 64-bit variable to receive the 17870 * capacity value from the command. 17871 * lbap - ptr to unsigned 32-bit varaible to receive the 17872 * block size value from the command 17873 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17874 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17875 * to use the USCSI "direct" chain and bypass the normal 17876 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17877 * command is issued as part of an error recovery action. 17878 * 17879 * Return Code: 0 - Success 17880 * EIO - IO error 17881 * EACCES - Reservation conflict detected 17882 * EAGAIN - Device is becoming ready 17883 * errno return code from sd_send_scsi_cmd() 17884 * 17885 * Context: Can sleep. Blocks until command completes. 17886 */ 17887 17888 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 17889 17890 static int 17891 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 17892 int path_flag) 17893 { 17894 struct scsi_extended_sense sense_buf; 17895 struct uscsi_cmd ucmd_buf; 17896 union scsi_cdb cdb; 17897 uint32_t *capacity_buf; 17898 uint64_t capacity; 17899 uint32_t lbasize; 17900 int status; 17901 17902 ASSERT(un != NULL); 17903 ASSERT(!mutex_owned(SD_MUTEX(un))); 17904 ASSERT(capp != NULL); 17905 ASSERT(lbap != NULL); 17906 17907 SD_TRACE(SD_LOG_IO, un, 17908 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17909 17910 /* 17911 * First send a READ_CAPACITY command to the target. 17912 * (This command is mandatory under SCSI-2.) 17913 * 17914 * Set up the CDB for the READ_CAPACITY command. The Partial 17915 * Medium Indicator bit is cleared. The address field must be 17916 * zero if the PMI bit is zero. 17917 */ 17918 bzero(&cdb, sizeof (cdb)); 17919 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17920 17921 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 17922 17923 cdb.scc_cmd = SCMD_READ_CAPACITY; 17924 17925 ucmd_buf.uscsi_cdb = (char *)&cdb; 17926 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 17927 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 17928 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 17929 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17930 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17931 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17932 ucmd_buf.uscsi_timeout = 60; 17933 17934 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17935 UIO_SYSSPACE, path_flag); 17936 17937 switch (status) { 17938 case 0: 17939 /* Return failure if we did not get valid capacity data. */ 17940 if (ucmd_buf.uscsi_resid != 0) { 17941 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17942 return (EIO); 17943 } 17944 17945 /* 17946 * Read capacity and block size from the READ CAPACITY 10 data. 17947 * This data may be adjusted later due to device specific 17948 * issues. 17949 * 17950 * According to the SCSI spec, the READ CAPACITY 10 17951 * command returns the following: 17952 * 17953 * bytes 0-3: Maximum logical block address available. 17954 * (MSB in byte:0 & LSB in byte:3) 17955 * 17956 * bytes 4-7: Block length in bytes 17957 * (MSB in byte:4 & LSB in byte:7) 17958 * 17959 */ 17960 capacity = BE_32(capacity_buf[0]); 17961 lbasize = BE_32(capacity_buf[1]); 17962 17963 /* 17964 * Done with capacity_buf 17965 */ 17966 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17967 17968 /* 17969 * if the reported capacity is set to all 0xf's, then 17970 * this disk is too large and requires SBC-2 commands. 17971 * Reissue the request using READ CAPACITY 16. 17972 */ 17973 if (capacity == 0xffffffff) { 17974 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 17975 &lbasize, path_flag); 17976 if (status != 0) { 17977 return (status); 17978 } 17979 } 17980 break; /* Success! */ 17981 case EIO: 17982 switch (ucmd_buf.uscsi_status) { 17983 case STATUS_RESERVATION_CONFLICT: 17984 status = EACCES; 17985 break; 17986 case STATUS_CHECK: 17987 /* 17988 * Check condition; look for ASC/ASCQ of 0x04/0x01 17989 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 17990 */ 17991 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17992 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 17993 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 17994 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17995 return (EAGAIN); 17996 } 17997 break; 17998 default: 17999 break; 18000 } 18001 /* FALLTHRU */ 18002 default: 18003 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 18004 return (status); 18005 } 18006 18007 /* 18008 * Some ATAPI CD-ROM drives report inaccurate LBA size values 18009 * (2352 and 0 are common) so for these devices always force the value 18010 * to 2048 as required by the ATAPI specs. 18011 */ 18012 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 18013 lbasize = 2048; 18014 } 18015 18016 /* 18017 * Get the maximum LBA value from the READ CAPACITY data. 18018 * Here we assume that the Partial Medium Indicator (PMI) bit 18019 * was cleared when issuing the command. This means that the LBA 18020 * returned from the device is the LBA of the last logical block 18021 * on the logical unit. The actual logical block count will be 18022 * this value plus one. 18023 * 18024 * Currently the capacity is saved in terms of un->un_sys_blocksize, 18025 * so scale the capacity value to reflect this. 18026 */ 18027 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 18028 18029 /* 18030 * Copy the values from the READ CAPACITY command into the space 18031 * provided by the caller. 18032 */ 18033 *capp = capacity; 18034 *lbap = lbasize; 18035 18036 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 18037 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 18038 18039 /* 18040 * Both the lbasize and capacity from the device must be nonzero, 18041 * otherwise we assume that the values are not valid and return 18042 * failure to the caller. (4203735) 18043 */ 18044 if ((capacity == 0) || (lbasize == 0)) { 18045 return (EIO); 18046 } 18047 18048 return (0); 18049 } 18050 18051 /* 18052 * Function: sd_send_scsi_READ_CAPACITY_16 18053 * 18054 * Description: This routine uses the scsi READ CAPACITY 16 command to 18055 * determine the device capacity in number of blocks and the 18056 * device native block size. If this function returns a failure, 18057 * then the values in *capp and *lbap are undefined. 18058 * This routine should always be called by 18059 * sd_send_scsi_READ_CAPACITY which will appy any device 18060 * specific adjustments to capacity and lbasize. 18061 * 18062 * Arguments: un - ptr to soft state struct for the target 18063 * capp - ptr to unsigned 64-bit variable to receive the 18064 * capacity value from the command. 18065 * lbap - ptr to unsigned 32-bit varaible to receive the 18066 * block size value from the command 18067 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18068 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18069 * to use the USCSI "direct" chain and bypass the normal 18070 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 18071 * this command is issued as part of an error recovery 18072 * action. 18073 * 18074 * Return Code: 0 - Success 18075 * EIO - IO error 18076 * EACCES - Reservation conflict detected 18077 * EAGAIN - Device is becoming ready 18078 * errno return code from sd_send_scsi_cmd() 18079 * 18080 * Context: Can sleep. Blocks until command completes. 18081 */ 18082 18083 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 18084 18085 static int 18086 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 18087 uint32_t *lbap, int path_flag) 18088 { 18089 struct scsi_extended_sense sense_buf; 18090 struct uscsi_cmd ucmd_buf; 18091 union scsi_cdb cdb; 18092 uint64_t *capacity16_buf; 18093 uint64_t capacity; 18094 uint32_t lbasize; 18095 int status; 18096 18097 ASSERT(un != NULL); 18098 ASSERT(!mutex_owned(SD_MUTEX(un))); 18099 ASSERT(capp != NULL); 18100 ASSERT(lbap != NULL); 18101 18102 SD_TRACE(SD_LOG_IO, un, 18103 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 18104 18105 /* 18106 * First send a READ_CAPACITY_16 command to the target. 18107 * 18108 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 18109 * Medium Indicator bit is cleared. The address field must be 18110 * zero if the PMI bit is zero. 18111 */ 18112 bzero(&cdb, sizeof (cdb)); 18113 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18114 18115 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 18116 18117 ucmd_buf.uscsi_cdb = (char *)&cdb; 18118 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 18119 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 18120 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 18121 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18122 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 18123 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18124 ucmd_buf.uscsi_timeout = 60; 18125 18126 /* 18127 * Read Capacity (16) is a Service Action In command. One 18128 * command byte (0x9E) is overloaded for multiple operations, 18129 * with the second CDB byte specifying the desired operation 18130 */ 18131 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 18132 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 18133 18134 /* 18135 * Fill in allocation length field 18136 */ 18137 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 18138 18139 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18140 UIO_SYSSPACE, path_flag); 18141 18142 switch (status) { 18143 case 0: 18144 /* Return failure if we did not get valid capacity data. */ 18145 if (ucmd_buf.uscsi_resid > 20) { 18146 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18147 return (EIO); 18148 } 18149 18150 /* 18151 * Read capacity and block size from the READ CAPACITY 10 data. 18152 * This data may be adjusted later due to device specific 18153 * issues. 18154 * 18155 * According to the SCSI spec, the READ CAPACITY 10 18156 * command returns the following: 18157 * 18158 * bytes 0-7: Maximum logical block address available. 18159 * (MSB in byte:0 & LSB in byte:7) 18160 * 18161 * bytes 8-11: Block length in bytes 18162 * (MSB in byte:8 & LSB in byte:11) 18163 * 18164 */ 18165 capacity = BE_64(capacity16_buf[0]); 18166 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 18167 18168 /* 18169 * Done with capacity16_buf 18170 */ 18171 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18172 18173 /* 18174 * if the reported capacity is set to all 0xf's, then 18175 * this disk is too large. This could only happen with 18176 * a device that supports LBAs larger than 64 bits which 18177 * are not defined by any current T10 standards. 18178 */ 18179 if (capacity == 0xffffffffffffffff) { 18180 return (EIO); 18181 } 18182 break; /* Success! */ 18183 case EIO: 18184 switch (ucmd_buf.uscsi_status) { 18185 case STATUS_RESERVATION_CONFLICT: 18186 status = EACCES; 18187 break; 18188 case STATUS_CHECK: 18189 /* 18190 * Check condition; look for ASC/ASCQ of 0x04/0x01 18191 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 18192 */ 18193 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18194 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 18195 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 18196 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18197 return (EAGAIN); 18198 } 18199 break; 18200 default: 18201 break; 18202 } 18203 /* FALLTHRU */ 18204 default: 18205 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18206 return (status); 18207 } 18208 18209 *capp = capacity; 18210 *lbap = lbasize; 18211 18212 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 18213 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 18214 18215 return (0); 18216 } 18217 18218 18219 /* 18220 * Function: sd_send_scsi_START_STOP_UNIT 18221 * 18222 * Description: Issue a scsi START STOP UNIT command to the target. 18223 * 18224 * Arguments: un - pointer to driver soft state (unit) structure for 18225 * this target. 18226 * flag - SD_TARGET_START 18227 * SD_TARGET_STOP 18228 * SD_TARGET_EJECT 18229 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18230 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18231 * to use the USCSI "direct" chain and bypass the normal 18232 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18233 * command is issued as part of an error recovery action. 18234 * 18235 * Return Code: 0 - Success 18236 * EIO - IO error 18237 * EACCES - Reservation conflict detected 18238 * ENXIO - Not Ready, medium not present 18239 * errno return code from sd_send_scsi_cmd() 18240 * 18241 * Context: Can sleep. 18242 */ 18243 18244 static int 18245 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 18246 { 18247 struct scsi_extended_sense sense_buf; 18248 union scsi_cdb cdb; 18249 struct uscsi_cmd ucmd_buf; 18250 int status; 18251 18252 ASSERT(un != NULL); 18253 ASSERT(!mutex_owned(SD_MUTEX(un))); 18254 18255 SD_TRACE(SD_LOG_IO, un, 18256 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 18257 18258 if (un->un_f_check_start_stop && 18259 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 18260 (un->un_f_start_stop_supported != TRUE)) { 18261 return (0); 18262 } 18263 18264 /* 18265 * If we are performing an eject operation and 18266 * we receive any command other than SD_TARGET_EJECT 18267 * we should immediately return. 18268 */ 18269 if (flag != SD_TARGET_EJECT) { 18270 mutex_enter(SD_MUTEX(un)); 18271 if (un->un_f_ejecting == TRUE) { 18272 mutex_exit(SD_MUTEX(un)); 18273 return (EAGAIN); 18274 } 18275 mutex_exit(SD_MUTEX(un)); 18276 } 18277 18278 bzero(&cdb, sizeof (cdb)); 18279 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18280 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18281 18282 cdb.scc_cmd = SCMD_START_STOP; 18283 cdb.cdb_opaque[4] = (uchar_t)flag; 18284 18285 ucmd_buf.uscsi_cdb = (char *)&cdb; 18286 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18287 ucmd_buf.uscsi_bufaddr = NULL; 18288 ucmd_buf.uscsi_buflen = 0; 18289 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18290 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18291 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18292 ucmd_buf.uscsi_timeout = 200; 18293 18294 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18295 UIO_SYSSPACE, path_flag); 18296 18297 switch (status) { 18298 case 0: 18299 break; /* Success! */ 18300 case EIO: 18301 switch (ucmd_buf.uscsi_status) { 18302 case STATUS_RESERVATION_CONFLICT: 18303 status = EACCES; 18304 break; 18305 case STATUS_CHECK: 18306 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 18307 switch (scsi_sense_key( 18308 (uint8_t *)&sense_buf)) { 18309 case KEY_ILLEGAL_REQUEST: 18310 status = ENOTSUP; 18311 break; 18312 case KEY_NOT_READY: 18313 if (scsi_sense_asc( 18314 (uint8_t *)&sense_buf) 18315 == 0x3A) { 18316 status = ENXIO; 18317 } 18318 break; 18319 default: 18320 break; 18321 } 18322 } 18323 break; 18324 default: 18325 break; 18326 } 18327 break; 18328 default: 18329 break; 18330 } 18331 18332 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 18333 18334 return (status); 18335 } 18336 18337 18338 /* 18339 * Function: sd_start_stop_unit_callback 18340 * 18341 * Description: timeout(9F) callback to begin recovery process for a 18342 * device that has spun down. 18343 * 18344 * Arguments: arg - pointer to associated softstate struct. 18345 * 18346 * Context: Executes in a timeout(9F) thread context 18347 */ 18348 18349 static void 18350 sd_start_stop_unit_callback(void *arg) 18351 { 18352 struct sd_lun *un = arg; 18353 ASSERT(un != NULL); 18354 ASSERT(!mutex_owned(SD_MUTEX(un))); 18355 18356 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 18357 18358 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 18359 } 18360 18361 18362 /* 18363 * Function: sd_start_stop_unit_task 18364 * 18365 * Description: Recovery procedure when a drive is spun down. 18366 * 18367 * Arguments: arg - pointer to associated softstate struct. 18368 * 18369 * Context: Executes in a taskq() thread context 18370 */ 18371 18372 static void 18373 sd_start_stop_unit_task(void *arg) 18374 { 18375 struct sd_lun *un = arg; 18376 18377 ASSERT(un != NULL); 18378 ASSERT(!mutex_owned(SD_MUTEX(un))); 18379 18380 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 18381 18382 /* 18383 * Some unformatted drives report not ready error, no need to 18384 * restart if format has been initiated. 18385 */ 18386 mutex_enter(SD_MUTEX(un)); 18387 if (un->un_f_format_in_progress == TRUE) { 18388 mutex_exit(SD_MUTEX(un)); 18389 return; 18390 } 18391 mutex_exit(SD_MUTEX(un)); 18392 18393 /* 18394 * When a START STOP command is issued from here, it is part of a 18395 * failure recovery operation and must be issued before any other 18396 * commands, including any pending retries. Thus it must be sent 18397 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 18398 * succeeds or not, we will start I/O after the attempt. 18399 */ 18400 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 18401 SD_PATH_DIRECT_PRIORITY); 18402 18403 /* 18404 * The above call blocks until the START_STOP_UNIT command completes. 18405 * Now that it has completed, we must re-try the original IO that 18406 * received the NOT READY condition in the first place. There are 18407 * three possible conditions here: 18408 * 18409 * (1) The original IO is on un_retry_bp. 18410 * (2) The original IO is on the regular wait queue, and un_retry_bp 18411 * is NULL. 18412 * (3) The original IO is on the regular wait queue, and un_retry_bp 18413 * points to some other, unrelated bp. 18414 * 18415 * For each case, we must call sd_start_cmds() with un_retry_bp 18416 * as the argument. If un_retry_bp is NULL, this will initiate 18417 * processing of the regular wait queue. If un_retry_bp is not NULL, 18418 * then this will process the bp on un_retry_bp. That may or may not 18419 * be the original IO, but that does not matter: the important thing 18420 * is to keep the IO processing going at this point. 18421 * 18422 * Note: This is a very specific error recovery sequence associated 18423 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 18424 * serialize the I/O with completion of the spin-up. 18425 */ 18426 mutex_enter(SD_MUTEX(un)); 18427 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18428 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 18429 un, un->un_retry_bp); 18430 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 18431 sd_start_cmds(un, un->un_retry_bp); 18432 mutex_exit(SD_MUTEX(un)); 18433 18434 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 18435 } 18436 18437 18438 /* 18439 * Function: sd_send_scsi_INQUIRY 18440 * 18441 * Description: Issue the scsi INQUIRY command. 18442 * 18443 * Arguments: un 18444 * bufaddr 18445 * buflen 18446 * evpd 18447 * page_code 18448 * page_length 18449 * 18450 * Return Code: 0 - Success 18451 * errno return code from sd_send_scsi_cmd() 18452 * 18453 * Context: Can sleep. Does not return until command is completed. 18454 */ 18455 18456 static int 18457 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 18458 uchar_t evpd, uchar_t page_code, size_t *residp) 18459 { 18460 union scsi_cdb cdb; 18461 struct uscsi_cmd ucmd_buf; 18462 int status; 18463 18464 ASSERT(un != NULL); 18465 ASSERT(!mutex_owned(SD_MUTEX(un))); 18466 ASSERT(bufaddr != NULL); 18467 18468 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 18469 18470 bzero(&cdb, sizeof (cdb)); 18471 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18472 bzero(bufaddr, buflen); 18473 18474 cdb.scc_cmd = SCMD_INQUIRY; 18475 cdb.cdb_opaque[1] = evpd; 18476 cdb.cdb_opaque[2] = page_code; 18477 FORMG0COUNT(&cdb, buflen); 18478 18479 ucmd_buf.uscsi_cdb = (char *)&cdb; 18480 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18481 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18482 ucmd_buf.uscsi_buflen = buflen; 18483 ucmd_buf.uscsi_rqbuf = NULL; 18484 ucmd_buf.uscsi_rqlen = 0; 18485 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 18486 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 18487 18488 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18489 UIO_SYSSPACE, SD_PATH_DIRECT); 18490 18491 if ((status == 0) && (residp != NULL)) { 18492 *residp = ucmd_buf.uscsi_resid; 18493 } 18494 18495 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 18496 18497 return (status); 18498 } 18499 18500 18501 /* 18502 * Function: sd_send_scsi_TEST_UNIT_READY 18503 * 18504 * Description: Issue the scsi TEST UNIT READY command. 18505 * This routine can be told to set the flag USCSI_DIAGNOSE to 18506 * prevent retrying failed commands. Use this when the intent 18507 * is either to check for device readiness, to clear a Unit 18508 * Attention, or to clear any outstanding sense data. 18509 * However under specific conditions the expected behavior 18510 * is for retries to bring a device ready, so use the flag 18511 * with caution. 18512 * 18513 * Arguments: un 18514 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 18515 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 18516 * 0: dont check for media present, do retries on cmd. 18517 * 18518 * Return Code: 0 - Success 18519 * EIO - IO error 18520 * EACCES - Reservation conflict detected 18521 * ENXIO - Not Ready, medium not present 18522 * errno return code from sd_send_scsi_cmd() 18523 * 18524 * Context: Can sleep. Does not return until command is completed. 18525 */ 18526 18527 static int 18528 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 18529 { 18530 struct scsi_extended_sense sense_buf; 18531 union scsi_cdb cdb; 18532 struct uscsi_cmd ucmd_buf; 18533 int status; 18534 18535 ASSERT(un != NULL); 18536 ASSERT(!mutex_owned(SD_MUTEX(un))); 18537 18538 SD_TRACE(SD_LOG_IO, un, 18539 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 18540 18541 /* 18542 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 18543 * timeouts when they receive a TUR and the queue is not empty. Check 18544 * the configuration flag set during attach (indicating the drive has 18545 * this firmware bug) and un_ncmds_in_transport before issuing the 18546 * TUR. If there are 18547 * pending commands return success, this is a bit arbitrary but is ok 18548 * for non-removables (i.e. the eliteI disks) and non-clustering 18549 * configurations. 18550 */ 18551 if (un->un_f_cfg_tur_check == TRUE) { 18552 mutex_enter(SD_MUTEX(un)); 18553 if (un->un_ncmds_in_transport != 0) { 18554 mutex_exit(SD_MUTEX(un)); 18555 return (0); 18556 } 18557 mutex_exit(SD_MUTEX(un)); 18558 } 18559 18560 bzero(&cdb, sizeof (cdb)); 18561 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18562 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18563 18564 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 18565 18566 ucmd_buf.uscsi_cdb = (char *)&cdb; 18567 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18568 ucmd_buf.uscsi_bufaddr = NULL; 18569 ucmd_buf.uscsi_buflen = 0; 18570 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18571 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18572 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18573 18574 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 18575 if ((flag & SD_DONT_RETRY_TUR) != 0) { 18576 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 18577 } 18578 ucmd_buf.uscsi_timeout = 60; 18579 18580 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18581 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 18582 SD_PATH_STANDARD)); 18583 18584 switch (status) { 18585 case 0: 18586 break; /* Success! */ 18587 case EIO: 18588 switch (ucmd_buf.uscsi_status) { 18589 case STATUS_RESERVATION_CONFLICT: 18590 status = EACCES; 18591 break; 18592 case STATUS_CHECK: 18593 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 18594 break; 18595 } 18596 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18597 (scsi_sense_key((uint8_t *)&sense_buf) == 18598 KEY_NOT_READY) && 18599 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 18600 status = ENXIO; 18601 } 18602 break; 18603 default: 18604 break; 18605 } 18606 break; 18607 default: 18608 break; 18609 } 18610 18611 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 18612 18613 return (status); 18614 } 18615 18616 18617 /* 18618 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 18619 * 18620 * Description: Issue the scsi PERSISTENT RESERVE IN command. 18621 * 18622 * Arguments: un 18623 * 18624 * Return Code: 0 - Success 18625 * EACCES 18626 * ENOTSUP 18627 * errno return code from sd_send_scsi_cmd() 18628 * 18629 * Context: Can sleep. Does not return until command is completed. 18630 */ 18631 18632 static int 18633 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 18634 uint16_t data_len, uchar_t *data_bufp) 18635 { 18636 struct scsi_extended_sense sense_buf; 18637 union scsi_cdb cdb; 18638 struct uscsi_cmd ucmd_buf; 18639 int status; 18640 int no_caller_buf = FALSE; 18641 18642 ASSERT(un != NULL); 18643 ASSERT(!mutex_owned(SD_MUTEX(un))); 18644 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 18645 18646 SD_TRACE(SD_LOG_IO, un, 18647 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 18648 18649 bzero(&cdb, sizeof (cdb)); 18650 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18651 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18652 if (data_bufp == NULL) { 18653 /* Allocate a default buf if the caller did not give one */ 18654 ASSERT(data_len == 0); 18655 data_len = MHIOC_RESV_KEY_SIZE; 18656 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 18657 no_caller_buf = TRUE; 18658 } 18659 18660 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 18661 cdb.cdb_opaque[1] = usr_cmd; 18662 FORMG1COUNT(&cdb, data_len); 18663 18664 ucmd_buf.uscsi_cdb = (char *)&cdb; 18665 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18666 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 18667 ucmd_buf.uscsi_buflen = data_len; 18668 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18669 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18670 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18671 ucmd_buf.uscsi_timeout = 60; 18672 18673 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18674 UIO_SYSSPACE, SD_PATH_STANDARD); 18675 18676 switch (status) { 18677 case 0: 18678 break; /* Success! */ 18679 case EIO: 18680 switch (ucmd_buf.uscsi_status) { 18681 case STATUS_RESERVATION_CONFLICT: 18682 status = EACCES; 18683 break; 18684 case STATUS_CHECK: 18685 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18686 (scsi_sense_key((uint8_t *)&sense_buf) == 18687 KEY_ILLEGAL_REQUEST)) { 18688 status = ENOTSUP; 18689 } 18690 break; 18691 default: 18692 break; 18693 } 18694 break; 18695 default: 18696 break; 18697 } 18698 18699 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 18700 18701 if (no_caller_buf == TRUE) { 18702 kmem_free(data_bufp, data_len); 18703 } 18704 18705 return (status); 18706 } 18707 18708 18709 /* 18710 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 18711 * 18712 * Description: This routine is the driver entry point for handling CD-ROM 18713 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 18714 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 18715 * device. 18716 * 18717 * Arguments: un - Pointer to soft state struct for the target. 18718 * usr_cmd SCSI-3 reservation facility command (one of 18719 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 18720 * SD_SCSI3_PREEMPTANDABORT) 18721 * usr_bufp - user provided pointer register, reserve descriptor or 18722 * preempt and abort structure (mhioc_register_t, 18723 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 18724 * 18725 * Return Code: 0 - Success 18726 * EACCES 18727 * ENOTSUP 18728 * errno return code from sd_send_scsi_cmd() 18729 * 18730 * Context: Can sleep. Does not return until command is completed. 18731 */ 18732 18733 static int 18734 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 18735 uchar_t *usr_bufp) 18736 { 18737 struct scsi_extended_sense sense_buf; 18738 union scsi_cdb cdb; 18739 struct uscsi_cmd ucmd_buf; 18740 int status; 18741 uchar_t data_len = sizeof (sd_prout_t); 18742 sd_prout_t *prp; 18743 18744 ASSERT(un != NULL); 18745 ASSERT(!mutex_owned(SD_MUTEX(un))); 18746 ASSERT(data_len == 24); /* required by scsi spec */ 18747 18748 SD_TRACE(SD_LOG_IO, un, 18749 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 18750 18751 if (usr_bufp == NULL) { 18752 return (EINVAL); 18753 } 18754 18755 bzero(&cdb, sizeof (cdb)); 18756 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18757 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18758 prp = kmem_zalloc(data_len, KM_SLEEP); 18759 18760 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 18761 cdb.cdb_opaque[1] = usr_cmd; 18762 FORMG1COUNT(&cdb, data_len); 18763 18764 ucmd_buf.uscsi_cdb = (char *)&cdb; 18765 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18766 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 18767 ucmd_buf.uscsi_buflen = data_len; 18768 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18769 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18770 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 18771 ucmd_buf.uscsi_timeout = 60; 18772 18773 switch (usr_cmd) { 18774 case SD_SCSI3_REGISTER: { 18775 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 18776 18777 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18778 bcopy(ptr->newkey.key, prp->service_key, 18779 MHIOC_RESV_KEY_SIZE); 18780 prp->aptpl = ptr->aptpl; 18781 break; 18782 } 18783 case SD_SCSI3_RESERVE: 18784 case SD_SCSI3_RELEASE: { 18785 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 18786 18787 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18788 prp->scope_address = BE_32(ptr->scope_specific_addr); 18789 cdb.cdb_opaque[2] = ptr->type; 18790 break; 18791 } 18792 case SD_SCSI3_PREEMPTANDABORT: { 18793 mhioc_preemptandabort_t *ptr = 18794 (mhioc_preemptandabort_t *)usr_bufp; 18795 18796 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18797 bcopy(ptr->victim_key.key, prp->service_key, 18798 MHIOC_RESV_KEY_SIZE); 18799 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 18800 cdb.cdb_opaque[2] = ptr->resvdesc.type; 18801 ucmd_buf.uscsi_flags |= USCSI_HEAD; 18802 break; 18803 } 18804 case SD_SCSI3_REGISTERANDIGNOREKEY: 18805 { 18806 mhioc_registerandignorekey_t *ptr; 18807 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 18808 bcopy(ptr->newkey.key, 18809 prp->service_key, MHIOC_RESV_KEY_SIZE); 18810 prp->aptpl = ptr->aptpl; 18811 break; 18812 } 18813 default: 18814 ASSERT(FALSE); 18815 break; 18816 } 18817 18818 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18819 UIO_SYSSPACE, SD_PATH_STANDARD); 18820 18821 switch (status) { 18822 case 0: 18823 break; /* Success! */ 18824 case EIO: 18825 switch (ucmd_buf.uscsi_status) { 18826 case STATUS_RESERVATION_CONFLICT: 18827 status = EACCES; 18828 break; 18829 case STATUS_CHECK: 18830 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18831 (scsi_sense_key((uint8_t *)&sense_buf) == 18832 KEY_ILLEGAL_REQUEST)) { 18833 status = ENOTSUP; 18834 } 18835 break; 18836 default: 18837 break; 18838 } 18839 break; 18840 default: 18841 break; 18842 } 18843 18844 kmem_free(prp, data_len); 18845 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 18846 return (status); 18847 } 18848 18849 18850 /* 18851 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 18852 * 18853 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 18854 * 18855 * Arguments: un - pointer to the target's soft state struct 18856 * dkc - pointer to the callback structure 18857 * 18858 * Return Code: 0 - success 18859 * errno-type error code 18860 * 18861 * Context: kernel thread context only. 18862 * 18863 * _______________________________________________________________ 18864 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 18865 * |FLUSH_VOLATILE| | operation | 18866 * |______________|______________|_________________________________| 18867 * | 0 | NULL | Synchronous flush on both | 18868 * | | | volatile and non-volatile cache | 18869 * |______________|______________|_________________________________| 18870 * | 1 | NULL | Synchronous flush on volatile | 18871 * | | | cache; disk drivers may suppress| 18872 * | | | flush if disk table indicates | 18873 * | | | non-volatile cache | 18874 * |______________|______________|_________________________________| 18875 * | 0 | !NULL | Asynchronous flush on both | 18876 * | | | volatile and non-volatile cache;| 18877 * |______________|______________|_________________________________| 18878 * | 1 | !NULL | Asynchronous flush on volatile | 18879 * | | | cache; disk drivers may suppress| 18880 * | | | flush if disk table indicates | 18881 * | | | non-volatile cache | 18882 * |______________|______________|_________________________________| 18883 * 18884 */ 18885 18886 static int 18887 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 18888 { 18889 struct sd_uscsi_info *uip; 18890 struct uscsi_cmd *uscmd; 18891 union scsi_cdb *cdb; 18892 struct buf *bp; 18893 int rval = 0; 18894 int is_async; 18895 18896 SD_TRACE(SD_LOG_IO, un, 18897 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 18898 18899 ASSERT(un != NULL); 18900 ASSERT(!mutex_owned(SD_MUTEX(un))); 18901 18902 if (dkc == NULL || dkc->dkc_callback == NULL) { 18903 is_async = FALSE; 18904 } else { 18905 is_async = TRUE; 18906 } 18907 18908 mutex_enter(SD_MUTEX(un)); 18909 /* check whether cache flush should be suppressed */ 18910 if (un->un_f_suppress_cache_flush == TRUE) { 18911 mutex_exit(SD_MUTEX(un)); 18912 /* 18913 * suppress the cache flush if the device is told to do 18914 * so by sd.conf or disk table 18915 */ 18916 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 18917 skip the cache flush since suppress_cache_flush is %d!\n", 18918 un->un_f_suppress_cache_flush); 18919 18920 if (is_async == TRUE) { 18921 /* invoke callback for asynchronous flush */ 18922 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 18923 } 18924 return (rval); 18925 } 18926 mutex_exit(SD_MUTEX(un)); 18927 18928 /* 18929 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 18930 * set properly 18931 */ 18932 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 18933 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 18934 18935 mutex_enter(SD_MUTEX(un)); 18936 if (dkc != NULL && un->un_f_sync_nv_supported && 18937 (dkc->dkc_flag & FLUSH_VOLATILE)) { 18938 /* 18939 * if the device supports SYNC_NV bit, turn on 18940 * the SYNC_NV bit to only flush volatile cache 18941 */ 18942 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 18943 } 18944 mutex_exit(SD_MUTEX(un)); 18945 18946 /* 18947 * First get some memory for the uscsi_cmd struct and cdb 18948 * and initialize for SYNCHRONIZE_CACHE cmd. 18949 */ 18950 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 18951 uscmd->uscsi_cdblen = CDB_GROUP1; 18952 uscmd->uscsi_cdb = (caddr_t)cdb; 18953 uscmd->uscsi_bufaddr = NULL; 18954 uscmd->uscsi_buflen = 0; 18955 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 18956 uscmd->uscsi_rqlen = SENSE_LENGTH; 18957 uscmd->uscsi_rqresid = SENSE_LENGTH; 18958 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18959 uscmd->uscsi_timeout = sd_io_time; 18960 18961 /* 18962 * Allocate an sd_uscsi_info struct and fill it with the info 18963 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 18964 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 18965 * since we allocate the buf here in this function, we do not 18966 * need to preserve the prior contents of b_private. 18967 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 18968 */ 18969 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 18970 uip->ui_flags = SD_PATH_DIRECT; 18971 uip->ui_cmdp = uscmd; 18972 18973 bp = getrbuf(KM_SLEEP); 18974 bp->b_private = uip; 18975 18976 /* 18977 * Setup buffer to carry uscsi request. 18978 */ 18979 bp->b_flags = B_BUSY; 18980 bp->b_bcount = 0; 18981 bp->b_blkno = 0; 18982 18983 if (is_async == TRUE) { 18984 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 18985 uip->ui_dkc = *dkc; 18986 } 18987 18988 bp->b_edev = SD_GET_DEV(un); 18989 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 18990 18991 (void) sd_uscsi_strategy(bp); 18992 18993 /* 18994 * If synchronous request, wait for completion 18995 * If async just return and let b_iodone callback 18996 * cleanup. 18997 * NOTE: On return, u_ncmds_in_driver will be decremented, 18998 * but it was also incremented in sd_uscsi_strategy(), so 18999 * we should be ok. 19000 */ 19001 if (is_async == FALSE) { 19002 (void) biowait(bp); 19003 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 19004 } 19005 19006 return (rval); 19007 } 19008 19009 19010 static int 19011 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 19012 { 19013 struct sd_uscsi_info *uip; 19014 struct uscsi_cmd *uscmd; 19015 uint8_t *sense_buf; 19016 struct sd_lun *un; 19017 int status; 19018 union scsi_cdb *cdb; 19019 19020 uip = (struct sd_uscsi_info *)(bp->b_private); 19021 ASSERT(uip != NULL); 19022 19023 uscmd = uip->ui_cmdp; 19024 ASSERT(uscmd != NULL); 19025 19026 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 19027 ASSERT(sense_buf != NULL); 19028 19029 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 19030 ASSERT(un != NULL); 19031 19032 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 19033 19034 status = geterror(bp); 19035 switch (status) { 19036 case 0: 19037 break; /* Success! */ 19038 case EIO: 19039 switch (uscmd->uscsi_status) { 19040 case STATUS_RESERVATION_CONFLICT: 19041 /* Ignore reservation conflict */ 19042 status = 0; 19043 goto done; 19044 19045 case STATUS_CHECK: 19046 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 19047 (scsi_sense_key(sense_buf) == 19048 KEY_ILLEGAL_REQUEST)) { 19049 /* Ignore Illegal Request error */ 19050 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 19051 mutex_enter(SD_MUTEX(un)); 19052 un->un_f_sync_nv_supported = FALSE; 19053 mutex_exit(SD_MUTEX(un)); 19054 status = 0; 19055 SD_TRACE(SD_LOG_IO, un, 19056 "un_f_sync_nv_supported \ 19057 is set to false.\n"); 19058 goto done; 19059 } 19060 19061 mutex_enter(SD_MUTEX(un)); 19062 un->un_f_sync_cache_supported = FALSE; 19063 mutex_exit(SD_MUTEX(un)); 19064 SD_TRACE(SD_LOG_IO, un, 19065 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 19066 un_f_sync_cache_supported set to false \ 19067 with asc = %x, ascq = %x\n", 19068 scsi_sense_asc(sense_buf), 19069 scsi_sense_ascq(sense_buf)); 19070 status = ENOTSUP; 19071 goto done; 19072 } 19073 break; 19074 default: 19075 break; 19076 } 19077 /* FALLTHRU */ 19078 default: 19079 /* 19080 * Don't log an error message if this device 19081 * has removable media. 19082 */ 19083 if (!un->un_f_has_removable_media) { 19084 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19085 "SYNCHRONIZE CACHE command failed (%d)\n", status); 19086 } 19087 break; 19088 } 19089 19090 done: 19091 if (uip->ui_dkc.dkc_callback != NULL) { 19092 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 19093 } 19094 19095 ASSERT((bp->b_flags & B_REMAPPED) == 0); 19096 freerbuf(bp); 19097 kmem_free(uip, sizeof (struct sd_uscsi_info)); 19098 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 19099 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 19100 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 19101 19102 return (status); 19103 } 19104 19105 19106 /* 19107 * Function: sd_send_scsi_GET_CONFIGURATION 19108 * 19109 * Description: Issues the get configuration command to the device. 19110 * Called from sd_check_for_writable_cd & sd_get_media_info 19111 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 19112 * Arguments: un 19113 * ucmdbuf 19114 * rqbuf 19115 * rqbuflen 19116 * bufaddr 19117 * buflen 19118 * path_flag 19119 * 19120 * Return Code: 0 - Success 19121 * errno return code from sd_send_scsi_cmd() 19122 * 19123 * Context: Can sleep. Does not return until command is completed. 19124 * 19125 */ 19126 19127 static int 19128 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 19129 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 19130 int path_flag) 19131 { 19132 char cdb[CDB_GROUP1]; 19133 int status; 19134 19135 ASSERT(un != NULL); 19136 ASSERT(!mutex_owned(SD_MUTEX(un))); 19137 ASSERT(bufaddr != NULL); 19138 ASSERT(ucmdbuf != NULL); 19139 ASSERT(rqbuf != NULL); 19140 19141 SD_TRACE(SD_LOG_IO, un, 19142 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 19143 19144 bzero(cdb, sizeof (cdb)); 19145 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19146 bzero(rqbuf, rqbuflen); 19147 bzero(bufaddr, buflen); 19148 19149 /* 19150 * Set up cdb field for the get configuration command. 19151 */ 19152 cdb[0] = SCMD_GET_CONFIGURATION; 19153 cdb[1] = 0x02; /* Requested Type */ 19154 cdb[8] = SD_PROFILE_HEADER_LEN; 19155 ucmdbuf->uscsi_cdb = cdb; 19156 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19157 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19158 ucmdbuf->uscsi_buflen = buflen; 19159 ucmdbuf->uscsi_timeout = sd_io_time; 19160 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19161 ucmdbuf->uscsi_rqlen = rqbuflen; 19162 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19163 19164 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19165 UIO_SYSSPACE, path_flag); 19166 19167 switch (status) { 19168 case 0: 19169 break; /* Success! */ 19170 case EIO: 19171 switch (ucmdbuf->uscsi_status) { 19172 case STATUS_RESERVATION_CONFLICT: 19173 status = EACCES; 19174 break; 19175 default: 19176 break; 19177 } 19178 break; 19179 default: 19180 break; 19181 } 19182 19183 if (status == 0) { 19184 SD_DUMP_MEMORY(un, SD_LOG_IO, 19185 "sd_send_scsi_GET_CONFIGURATION: data", 19186 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19187 } 19188 19189 SD_TRACE(SD_LOG_IO, un, 19190 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 19191 19192 return (status); 19193 } 19194 19195 /* 19196 * Function: sd_send_scsi_feature_GET_CONFIGURATION 19197 * 19198 * Description: Issues the get configuration command to the device to 19199 * retrieve a specific feature. Called from 19200 * sd_check_for_writable_cd & sd_set_mmc_caps. 19201 * Arguments: un 19202 * ucmdbuf 19203 * rqbuf 19204 * rqbuflen 19205 * bufaddr 19206 * buflen 19207 * feature 19208 * 19209 * Return Code: 0 - Success 19210 * errno return code from sd_send_scsi_cmd() 19211 * 19212 * Context: Can sleep. Does not return until command is completed. 19213 * 19214 */ 19215 static int 19216 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 19217 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 19218 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 19219 { 19220 char cdb[CDB_GROUP1]; 19221 int status; 19222 19223 ASSERT(un != NULL); 19224 ASSERT(!mutex_owned(SD_MUTEX(un))); 19225 ASSERT(bufaddr != NULL); 19226 ASSERT(ucmdbuf != NULL); 19227 ASSERT(rqbuf != NULL); 19228 19229 SD_TRACE(SD_LOG_IO, un, 19230 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 19231 19232 bzero(cdb, sizeof (cdb)); 19233 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19234 bzero(rqbuf, rqbuflen); 19235 bzero(bufaddr, buflen); 19236 19237 /* 19238 * Set up cdb field for the get configuration command. 19239 */ 19240 cdb[0] = SCMD_GET_CONFIGURATION; 19241 cdb[1] = 0x02; /* Requested Type */ 19242 cdb[3] = feature; 19243 cdb[8] = buflen; 19244 ucmdbuf->uscsi_cdb = cdb; 19245 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19246 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19247 ucmdbuf->uscsi_buflen = buflen; 19248 ucmdbuf->uscsi_timeout = sd_io_time; 19249 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19250 ucmdbuf->uscsi_rqlen = rqbuflen; 19251 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19252 19253 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19254 UIO_SYSSPACE, path_flag); 19255 19256 switch (status) { 19257 case 0: 19258 break; /* Success! */ 19259 case EIO: 19260 switch (ucmdbuf->uscsi_status) { 19261 case STATUS_RESERVATION_CONFLICT: 19262 status = EACCES; 19263 break; 19264 default: 19265 break; 19266 } 19267 break; 19268 default: 19269 break; 19270 } 19271 19272 if (status == 0) { 19273 SD_DUMP_MEMORY(un, SD_LOG_IO, 19274 "sd_send_scsi_feature_GET_CONFIGURATION: data", 19275 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19276 } 19277 19278 SD_TRACE(SD_LOG_IO, un, 19279 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 19280 19281 return (status); 19282 } 19283 19284 19285 /* 19286 * Function: sd_send_scsi_MODE_SENSE 19287 * 19288 * Description: Utility function for issuing a scsi MODE SENSE command. 19289 * Note: This routine uses a consistent implementation for Group0, 19290 * Group1, and Group2 commands across all platforms. ATAPI devices 19291 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19292 * 19293 * Arguments: un - pointer to the softstate struct for the target. 19294 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19295 * CDB_GROUP[1|2] (10 byte). 19296 * bufaddr - buffer for page data retrieved from the target. 19297 * buflen - size of page to be retrieved. 19298 * page_code - page code of data to be retrieved from the target. 19299 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19300 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19301 * to use the USCSI "direct" chain and bypass the normal 19302 * command waitq. 19303 * 19304 * Return Code: 0 - Success 19305 * errno return code from sd_send_scsi_cmd() 19306 * 19307 * Context: Can sleep. Does not return until command is completed. 19308 */ 19309 19310 static int 19311 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19312 size_t buflen, uchar_t page_code, int path_flag) 19313 { 19314 struct scsi_extended_sense sense_buf; 19315 union scsi_cdb cdb; 19316 struct uscsi_cmd ucmd_buf; 19317 int status; 19318 int headlen; 19319 19320 ASSERT(un != NULL); 19321 ASSERT(!mutex_owned(SD_MUTEX(un))); 19322 ASSERT(bufaddr != NULL); 19323 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19324 (cdbsize == CDB_GROUP2)); 19325 19326 SD_TRACE(SD_LOG_IO, un, 19327 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 19328 19329 bzero(&cdb, sizeof (cdb)); 19330 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19331 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19332 bzero(bufaddr, buflen); 19333 19334 if (cdbsize == CDB_GROUP0) { 19335 cdb.scc_cmd = SCMD_MODE_SENSE; 19336 cdb.cdb_opaque[2] = page_code; 19337 FORMG0COUNT(&cdb, buflen); 19338 headlen = MODE_HEADER_LENGTH; 19339 } else { 19340 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 19341 cdb.cdb_opaque[2] = page_code; 19342 FORMG1COUNT(&cdb, buflen); 19343 headlen = MODE_HEADER_LENGTH_GRP2; 19344 } 19345 19346 ASSERT(headlen <= buflen); 19347 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19348 19349 ucmd_buf.uscsi_cdb = (char *)&cdb; 19350 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19351 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19352 ucmd_buf.uscsi_buflen = buflen; 19353 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19354 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19355 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19356 ucmd_buf.uscsi_timeout = 60; 19357 19358 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19359 UIO_SYSSPACE, path_flag); 19360 19361 switch (status) { 19362 case 0: 19363 /* 19364 * sr_check_wp() uses 0x3f page code and check the header of 19365 * mode page to determine if target device is write-protected. 19366 * But some USB devices return 0 bytes for 0x3f page code. For 19367 * this case, make sure that mode page header is returned at 19368 * least. 19369 */ 19370 if (buflen - ucmd_buf.uscsi_resid < headlen) 19371 status = EIO; 19372 break; /* Success! */ 19373 case EIO: 19374 switch (ucmd_buf.uscsi_status) { 19375 case STATUS_RESERVATION_CONFLICT: 19376 status = EACCES; 19377 break; 19378 default: 19379 break; 19380 } 19381 break; 19382 default: 19383 break; 19384 } 19385 19386 if (status == 0) { 19387 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 19388 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19389 } 19390 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 19391 19392 return (status); 19393 } 19394 19395 19396 /* 19397 * Function: sd_send_scsi_MODE_SELECT 19398 * 19399 * Description: Utility function for issuing a scsi MODE SELECT command. 19400 * Note: This routine uses a consistent implementation for Group0, 19401 * Group1, and Group2 commands across all platforms. ATAPI devices 19402 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19403 * 19404 * Arguments: un - pointer to the softstate struct for the target. 19405 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19406 * CDB_GROUP[1|2] (10 byte). 19407 * bufaddr - buffer for page data retrieved from the target. 19408 * buflen - size of page to be retrieved. 19409 * save_page - boolean to determin if SP bit should be set. 19410 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19411 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19412 * to use the USCSI "direct" chain and bypass the normal 19413 * command waitq. 19414 * 19415 * Return Code: 0 - Success 19416 * errno return code from sd_send_scsi_cmd() 19417 * 19418 * Context: Can sleep. Does not return until command is completed. 19419 */ 19420 19421 static int 19422 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19423 size_t buflen, uchar_t save_page, int path_flag) 19424 { 19425 struct scsi_extended_sense sense_buf; 19426 union scsi_cdb cdb; 19427 struct uscsi_cmd ucmd_buf; 19428 int status; 19429 19430 ASSERT(un != NULL); 19431 ASSERT(!mutex_owned(SD_MUTEX(un))); 19432 ASSERT(bufaddr != NULL); 19433 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19434 (cdbsize == CDB_GROUP2)); 19435 19436 SD_TRACE(SD_LOG_IO, un, 19437 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 19438 19439 bzero(&cdb, sizeof (cdb)); 19440 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19441 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19442 19443 /* Set the PF bit for many third party drives */ 19444 cdb.cdb_opaque[1] = 0x10; 19445 19446 /* Set the savepage(SP) bit if given */ 19447 if (save_page == SD_SAVE_PAGE) { 19448 cdb.cdb_opaque[1] |= 0x01; 19449 } 19450 19451 if (cdbsize == CDB_GROUP0) { 19452 cdb.scc_cmd = SCMD_MODE_SELECT; 19453 FORMG0COUNT(&cdb, buflen); 19454 } else { 19455 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 19456 FORMG1COUNT(&cdb, buflen); 19457 } 19458 19459 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19460 19461 ucmd_buf.uscsi_cdb = (char *)&cdb; 19462 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19463 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19464 ucmd_buf.uscsi_buflen = buflen; 19465 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19466 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19467 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19468 ucmd_buf.uscsi_timeout = 60; 19469 19470 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19471 UIO_SYSSPACE, path_flag); 19472 19473 switch (status) { 19474 case 0: 19475 break; /* Success! */ 19476 case EIO: 19477 switch (ucmd_buf.uscsi_status) { 19478 case STATUS_RESERVATION_CONFLICT: 19479 status = EACCES; 19480 break; 19481 default: 19482 break; 19483 } 19484 break; 19485 default: 19486 break; 19487 } 19488 19489 if (status == 0) { 19490 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 19491 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19492 } 19493 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 19494 19495 return (status); 19496 } 19497 19498 19499 /* 19500 * Function: sd_send_scsi_RDWR 19501 * 19502 * Description: Issue a scsi READ or WRITE command with the given parameters. 19503 * 19504 * Arguments: un: Pointer to the sd_lun struct for the target. 19505 * cmd: SCMD_READ or SCMD_WRITE 19506 * bufaddr: Address of caller's buffer to receive the RDWR data 19507 * buflen: Length of caller's buffer receive the RDWR data. 19508 * start_block: Block number for the start of the RDWR operation. 19509 * (Assumes target-native block size.) 19510 * residp: Pointer to variable to receive the redisual of the 19511 * RDWR operation (may be NULL of no residual requested). 19512 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19513 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19514 * to use the USCSI "direct" chain and bypass the normal 19515 * command waitq. 19516 * 19517 * Return Code: 0 - Success 19518 * errno return code from sd_send_scsi_cmd() 19519 * 19520 * Context: Can sleep. Does not return until command is completed. 19521 */ 19522 19523 static int 19524 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 19525 size_t buflen, daddr_t start_block, int path_flag) 19526 { 19527 struct scsi_extended_sense sense_buf; 19528 union scsi_cdb cdb; 19529 struct uscsi_cmd ucmd_buf; 19530 uint32_t block_count; 19531 int status; 19532 int cdbsize; 19533 uchar_t flag; 19534 19535 ASSERT(un != NULL); 19536 ASSERT(!mutex_owned(SD_MUTEX(un))); 19537 ASSERT(bufaddr != NULL); 19538 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 19539 19540 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 19541 19542 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 19543 return (EINVAL); 19544 } 19545 19546 mutex_enter(SD_MUTEX(un)); 19547 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 19548 mutex_exit(SD_MUTEX(un)); 19549 19550 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 19551 19552 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 19553 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 19554 bufaddr, buflen, start_block, block_count); 19555 19556 bzero(&cdb, sizeof (cdb)); 19557 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19558 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19559 19560 /* Compute CDB size to use */ 19561 if (start_block > 0xffffffff) 19562 cdbsize = CDB_GROUP4; 19563 else if ((start_block & 0xFFE00000) || 19564 (un->un_f_cfg_is_atapi == TRUE)) 19565 cdbsize = CDB_GROUP1; 19566 else 19567 cdbsize = CDB_GROUP0; 19568 19569 switch (cdbsize) { 19570 case CDB_GROUP0: /* 6-byte CDBs */ 19571 cdb.scc_cmd = cmd; 19572 FORMG0ADDR(&cdb, start_block); 19573 FORMG0COUNT(&cdb, block_count); 19574 break; 19575 case CDB_GROUP1: /* 10-byte CDBs */ 19576 cdb.scc_cmd = cmd | SCMD_GROUP1; 19577 FORMG1ADDR(&cdb, start_block); 19578 FORMG1COUNT(&cdb, block_count); 19579 break; 19580 case CDB_GROUP4: /* 16-byte CDBs */ 19581 cdb.scc_cmd = cmd | SCMD_GROUP4; 19582 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 19583 FORMG4COUNT(&cdb, block_count); 19584 break; 19585 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 19586 default: 19587 /* All others reserved */ 19588 return (EINVAL); 19589 } 19590 19591 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 19592 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19593 19594 ucmd_buf.uscsi_cdb = (char *)&cdb; 19595 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19596 ucmd_buf.uscsi_bufaddr = bufaddr; 19597 ucmd_buf.uscsi_buflen = buflen; 19598 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19599 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19600 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 19601 ucmd_buf.uscsi_timeout = 60; 19602 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19603 UIO_SYSSPACE, path_flag); 19604 switch (status) { 19605 case 0: 19606 break; /* Success! */ 19607 case EIO: 19608 switch (ucmd_buf.uscsi_status) { 19609 case STATUS_RESERVATION_CONFLICT: 19610 status = EACCES; 19611 break; 19612 default: 19613 break; 19614 } 19615 break; 19616 default: 19617 break; 19618 } 19619 19620 if (status == 0) { 19621 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 19622 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19623 } 19624 19625 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 19626 19627 return (status); 19628 } 19629 19630 19631 /* 19632 * Function: sd_send_scsi_LOG_SENSE 19633 * 19634 * Description: Issue a scsi LOG_SENSE command with the given parameters. 19635 * 19636 * Arguments: un: Pointer to the sd_lun struct for the target. 19637 * 19638 * Return Code: 0 - Success 19639 * errno return code from sd_send_scsi_cmd() 19640 * 19641 * Context: Can sleep. Does not return until command is completed. 19642 */ 19643 19644 static int 19645 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 19646 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 19647 int path_flag) 19648 19649 { 19650 struct scsi_extended_sense sense_buf; 19651 union scsi_cdb cdb; 19652 struct uscsi_cmd ucmd_buf; 19653 int status; 19654 19655 ASSERT(un != NULL); 19656 ASSERT(!mutex_owned(SD_MUTEX(un))); 19657 19658 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 19659 19660 bzero(&cdb, sizeof (cdb)); 19661 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19662 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19663 19664 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 19665 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 19666 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 19667 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 19668 FORMG1COUNT(&cdb, buflen); 19669 19670 ucmd_buf.uscsi_cdb = (char *)&cdb; 19671 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19672 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19673 ucmd_buf.uscsi_buflen = buflen; 19674 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19675 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19676 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19677 ucmd_buf.uscsi_timeout = 60; 19678 19679 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19680 UIO_SYSSPACE, path_flag); 19681 19682 switch (status) { 19683 case 0: 19684 break; 19685 case EIO: 19686 switch (ucmd_buf.uscsi_status) { 19687 case STATUS_RESERVATION_CONFLICT: 19688 status = EACCES; 19689 break; 19690 case STATUS_CHECK: 19691 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19692 (scsi_sense_key((uint8_t *)&sense_buf) == 19693 KEY_ILLEGAL_REQUEST) && 19694 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 19695 /* 19696 * ASC 0x24: INVALID FIELD IN CDB 19697 */ 19698 switch (page_code) { 19699 case START_STOP_CYCLE_PAGE: 19700 /* 19701 * The start stop cycle counter is 19702 * implemented as page 0x31 in earlier 19703 * generation disks. In new generation 19704 * disks the start stop cycle counter is 19705 * implemented as page 0xE. To properly 19706 * handle this case if an attempt for 19707 * log page 0xE is made and fails we 19708 * will try again using page 0x31. 19709 * 19710 * Network storage BU committed to 19711 * maintain the page 0x31 for this 19712 * purpose and will not have any other 19713 * page implemented with page code 0x31 19714 * until all disks transition to the 19715 * standard page. 19716 */ 19717 mutex_enter(SD_MUTEX(un)); 19718 un->un_start_stop_cycle_page = 19719 START_STOP_CYCLE_VU_PAGE; 19720 cdb.cdb_opaque[2] = 19721 (char)(page_control << 6) | 19722 un->un_start_stop_cycle_page; 19723 mutex_exit(SD_MUTEX(un)); 19724 status = sd_send_scsi_cmd( 19725 SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19726 UIO_SYSSPACE, path_flag); 19727 19728 break; 19729 case TEMPERATURE_PAGE: 19730 status = ENOTTY; 19731 break; 19732 default: 19733 break; 19734 } 19735 } 19736 break; 19737 default: 19738 break; 19739 } 19740 break; 19741 default: 19742 break; 19743 } 19744 19745 if (status == 0) { 19746 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 19747 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19748 } 19749 19750 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 19751 19752 return (status); 19753 } 19754 19755 19756 /* 19757 * Function: sdioctl 19758 * 19759 * Description: Driver's ioctl(9e) entry point function. 19760 * 19761 * Arguments: dev - device number 19762 * cmd - ioctl operation to be performed 19763 * arg - user argument, contains data to be set or reference 19764 * parameter for get 19765 * flag - bit flag, indicating open settings, 32/64 bit type 19766 * cred_p - user credential pointer 19767 * rval_p - calling process return value (OPT) 19768 * 19769 * Return Code: EINVAL 19770 * ENOTTY 19771 * ENXIO 19772 * EIO 19773 * EFAULT 19774 * ENOTSUP 19775 * EPERM 19776 * 19777 * Context: Called from the device switch at normal priority. 19778 */ 19779 19780 static int 19781 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 19782 { 19783 struct sd_lun *un = NULL; 19784 int err = 0; 19785 int i = 0; 19786 cred_t *cr; 19787 int tmprval = EINVAL; 19788 int is_valid; 19789 19790 /* 19791 * All device accesses go thru sdstrategy where we check on suspend 19792 * status 19793 */ 19794 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 19795 return (ENXIO); 19796 } 19797 19798 ASSERT(!mutex_owned(SD_MUTEX(un))); 19799 19800 19801 is_valid = SD_IS_VALID_LABEL(un); 19802 19803 /* 19804 * Moved this wait from sd_uscsi_strategy to here for 19805 * reasons of deadlock prevention. Internal driver commands, 19806 * specifically those to change a devices power level, result 19807 * in a call to sd_uscsi_strategy. 19808 */ 19809 mutex_enter(SD_MUTEX(un)); 19810 while ((un->un_state == SD_STATE_SUSPENDED) || 19811 (un->un_state == SD_STATE_PM_CHANGING)) { 19812 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 19813 } 19814 /* 19815 * Twiddling the counter here protects commands from now 19816 * through to the top of sd_uscsi_strategy. Without the 19817 * counter inc. a power down, for example, could get in 19818 * after the above check for state is made and before 19819 * execution gets to the top of sd_uscsi_strategy. 19820 * That would cause problems. 19821 */ 19822 un->un_ncmds_in_driver++; 19823 19824 if (!is_valid && 19825 (flag & (FNDELAY | FNONBLOCK))) { 19826 switch (cmd) { 19827 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 19828 case DKIOCGVTOC: 19829 case DKIOCGAPART: 19830 case DKIOCPARTINFO: 19831 case DKIOCSGEOM: 19832 case DKIOCSAPART: 19833 case DKIOCGETEFI: 19834 case DKIOCPARTITION: 19835 case DKIOCSVTOC: 19836 case DKIOCSETEFI: 19837 case DKIOCGMBOOT: 19838 case DKIOCSMBOOT: 19839 case DKIOCG_PHYGEOM: 19840 case DKIOCG_VIRTGEOM: 19841 /* let cmlb handle it */ 19842 goto skip_ready_valid; 19843 19844 case CDROMPAUSE: 19845 case CDROMRESUME: 19846 case CDROMPLAYMSF: 19847 case CDROMPLAYTRKIND: 19848 case CDROMREADTOCHDR: 19849 case CDROMREADTOCENTRY: 19850 case CDROMSTOP: 19851 case CDROMSTART: 19852 case CDROMVOLCTRL: 19853 case CDROMSUBCHNL: 19854 case CDROMREADMODE2: 19855 case CDROMREADMODE1: 19856 case CDROMREADOFFSET: 19857 case CDROMSBLKMODE: 19858 case CDROMGBLKMODE: 19859 case CDROMGDRVSPEED: 19860 case CDROMSDRVSPEED: 19861 case CDROMCDDA: 19862 case CDROMCDXA: 19863 case CDROMSUBCODE: 19864 if (!ISCD(un)) { 19865 un->un_ncmds_in_driver--; 19866 ASSERT(un->un_ncmds_in_driver >= 0); 19867 mutex_exit(SD_MUTEX(un)); 19868 return (ENOTTY); 19869 } 19870 break; 19871 case FDEJECT: 19872 case DKIOCEJECT: 19873 case CDROMEJECT: 19874 if (!un->un_f_eject_media_supported) { 19875 un->un_ncmds_in_driver--; 19876 ASSERT(un->un_ncmds_in_driver >= 0); 19877 mutex_exit(SD_MUTEX(un)); 19878 return (ENOTTY); 19879 } 19880 break; 19881 case DKIOCFLUSHWRITECACHE: 19882 mutex_exit(SD_MUTEX(un)); 19883 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19884 if (err != 0) { 19885 mutex_enter(SD_MUTEX(un)); 19886 un->un_ncmds_in_driver--; 19887 ASSERT(un->un_ncmds_in_driver >= 0); 19888 mutex_exit(SD_MUTEX(un)); 19889 return (EIO); 19890 } 19891 mutex_enter(SD_MUTEX(un)); 19892 /* FALLTHROUGH */ 19893 case DKIOCREMOVABLE: 19894 case DKIOCHOTPLUGGABLE: 19895 case DKIOCINFO: 19896 case DKIOCGMEDIAINFO: 19897 case MHIOCENFAILFAST: 19898 case MHIOCSTATUS: 19899 case MHIOCTKOWN: 19900 case MHIOCRELEASE: 19901 case MHIOCGRP_INKEYS: 19902 case MHIOCGRP_INRESV: 19903 case MHIOCGRP_REGISTER: 19904 case MHIOCGRP_RESERVE: 19905 case MHIOCGRP_PREEMPTANDABORT: 19906 case MHIOCGRP_REGISTERANDIGNOREKEY: 19907 case CDROMCLOSETRAY: 19908 case USCSICMD: 19909 goto skip_ready_valid; 19910 default: 19911 break; 19912 } 19913 19914 mutex_exit(SD_MUTEX(un)); 19915 err = sd_ready_and_valid(un); 19916 mutex_enter(SD_MUTEX(un)); 19917 19918 if (err != SD_READY_VALID) { 19919 switch (cmd) { 19920 case DKIOCSTATE: 19921 case CDROMGDRVSPEED: 19922 case CDROMSDRVSPEED: 19923 case FDEJECT: /* for eject command */ 19924 case DKIOCEJECT: 19925 case CDROMEJECT: 19926 case DKIOCREMOVABLE: 19927 case DKIOCHOTPLUGGABLE: 19928 break; 19929 default: 19930 if (un->un_f_has_removable_media) { 19931 err = ENXIO; 19932 } else { 19933 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 19934 if (err == SD_RESERVED_BY_OTHERS) { 19935 err = EACCES; 19936 } else { 19937 err = EIO; 19938 } 19939 } 19940 un->un_ncmds_in_driver--; 19941 ASSERT(un->un_ncmds_in_driver >= 0); 19942 mutex_exit(SD_MUTEX(un)); 19943 return (err); 19944 } 19945 } 19946 } 19947 19948 skip_ready_valid: 19949 mutex_exit(SD_MUTEX(un)); 19950 19951 switch (cmd) { 19952 case DKIOCINFO: 19953 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 19954 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 19955 break; 19956 19957 case DKIOCGMEDIAINFO: 19958 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 19959 err = sd_get_media_info(dev, (caddr_t)arg, flag); 19960 break; 19961 19962 case DKIOCGGEOM: 19963 case DKIOCGVTOC: 19964 case DKIOCGAPART: 19965 case DKIOCPARTINFO: 19966 case DKIOCSGEOM: 19967 case DKIOCSAPART: 19968 case DKIOCGETEFI: 19969 case DKIOCPARTITION: 19970 case DKIOCSVTOC: 19971 case DKIOCSETEFI: 19972 case DKIOCGMBOOT: 19973 case DKIOCSMBOOT: 19974 case DKIOCG_PHYGEOM: 19975 case DKIOCG_VIRTGEOM: 19976 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 19977 19978 /* TUR should spin up */ 19979 19980 if (un->un_f_has_removable_media) 19981 err = sd_send_scsi_TEST_UNIT_READY(un, 19982 SD_CHECK_FOR_MEDIA); 19983 else 19984 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19985 19986 if (err != 0) 19987 break; 19988 19989 err = cmlb_ioctl(un->un_cmlbhandle, dev, 19990 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 19991 19992 if ((err == 0) && 19993 ((cmd == DKIOCSETEFI) || 19994 (un->un_f_pkstats_enabled) && 19995 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC))) { 19996 19997 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 19998 (void *)SD_PATH_DIRECT); 19999 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 20000 sd_set_pstats(un); 20001 SD_TRACE(SD_LOG_IO_PARTITION, un, 20002 "sd_ioctl: un:0x%p pstats created and " 20003 "set\n", un); 20004 } 20005 } 20006 20007 if ((cmd == DKIOCSVTOC) || 20008 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 20009 20010 mutex_enter(SD_MUTEX(un)); 20011 if (un->un_f_devid_supported && 20012 (un->un_f_opt_fab_devid == TRUE)) { 20013 if (un->un_devid == NULL) { 20014 sd_register_devid(un, SD_DEVINFO(un), 20015 SD_TARGET_IS_UNRESERVED); 20016 } else { 20017 /* 20018 * The device id for this disk 20019 * has been fabricated. The 20020 * device id must be preserved 20021 * by writing it back out to 20022 * disk. 20023 */ 20024 if (sd_write_deviceid(un) != 0) { 20025 ddi_devid_free(un->un_devid); 20026 un->un_devid = NULL; 20027 } 20028 } 20029 } 20030 mutex_exit(SD_MUTEX(un)); 20031 } 20032 20033 break; 20034 20035 case DKIOCLOCK: 20036 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 20037 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 20038 SD_PATH_STANDARD); 20039 break; 20040 20041 case DKIOCUNLOCK: 20042 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 20043 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 20044 SD_PATH_STANDARD); 20045 break; 20046 20047 case DKIOCSTATE: { 20048 enum dkio_state state; 20049 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 20050 20051 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 20052 err = EFAULT; 20053 } else { 20054 err = sd_check_media(dev, state); 20055 if (err == 0) { 20056 if (ddi_copyout(&un->un_mediastate, (void *)arg, 20057 sizeof (int), flag) != 0) 20058 err = EFAULT; 20059 } 20060 } 20061 break; 20062 } 20063 20064 case DKIOCREMOVABLE: 20065 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 20066 i = un->un_f_has_removable_media ? 1 : 0; 20067 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 20068 err = EFAULT; 20069 } else { 20070 err = 0; 20071 } 20072 break; 20073 20074 case DKIOCHOTPLUGGABLE: 20075 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 20076 i = un->un_f_is_hotpluggable ? 1 : 0; 20077 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 20078 err = EFAULT; 20079 } else { 20080 err = 0; 20081 } 20082 break; 20083 20084 case DKIOCGTEMPERATURE: 20085 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 20086 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 20087 break; 20088 20089 case MHIOCENFAILFAST: 20090 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 20091 if ((err = drv_priv(cred_p)) == 0) { 20092 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 20093 } 20094 break; 20095 20096 case MHIOCTKOWN: 20097 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 20098 if ((err = drv_priv(cred_p)) == 0) { 20099 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 20100 } 20101 break; 20102 20103 case MHIOCRELEASE: 20104 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 20105 if ((err = drv_priv(cred_p)) == 0) { 20106 err = sd_mhdioc_release(dev); 20107 } 20108 break; 20109 20110 case MHIOCSTATUS: 20111 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 20112 if ((err = drv_priv(cred_p)) == 0) { 20113 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 20114 case 0: 20115 err = 0; 20116 break; 20117 case EACCES: 20118 *rval_p = 1; 20119 err = 0; 20120 break; 20121 default: 20122 err = EIO; 20123 break; 20124 } 20125 } 20126 break; 20127 20128 case MHIOCQRESERVE: 20129 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 20130 if ((err = drv_priv(cred_p)) == 0) { 20131 err = sd_reserve_release(dev, SD_RESERVE); 20132 } 20133 break; 20134 20135 case MHIOCREREGISTERDEVID: 20136 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 20137 if (drv_priv(cred_p) == EPERM) { 20138 err = EPERM; 20139 } else if (!un->un_f_devid_supported) { 20140 err = ENOTTY; 20141 } else { 20142 err = sd_mhdioc_register_devid(dev); 20143 } 20144 break; 20145 20146 case MHIOCGRP_INKEYS: 20147 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 20148 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20149 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20150 err = ENOTSUP; 20151 } else { 20152 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 20153 flag); 20154 } 20155 } 20156 break; 20157 20158 case MHIOCGRP_INRESV: 20159 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 20160 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20161 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20162 err = ENOTSUP; 20163 } else { 20164 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 20165 } 20166 } 20167 break; 20168 20169 case MHIOCGRP_REGISTER: 20170 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 20171 if ((err = drv_priv(cred_p)) != EPERM) { 20172 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20173 err = ENOTSUP; 20174 } else if (arg != NULL) { 20175 mhioc_register_t reg; 20176 if (ddi_copyin((void *)arg, ®, 20177 sizeof (mhioc_register_t), flag) != 0) { 20178 err = EFAULT; 20179 } else { 20180 err = 20181 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20182 un, SD_SCSI3_REGISTER, 20183 (uchar_t *)®); 20184 } 20185 } 20186 } 20187 break; 20188 20189 case MHIOCGRP_RESERVE: 20190 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 20191 if ((err = drv_priv(cred_p)) != EPERM) { 20192 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20193 err = ENOTSUP; 20194 } else if (arg != NULL) { 20195 mhioc_resv_desc_t resv_desc; 20196 if (ddi_copyin((void *)arg, &resv_desc, 20197 sizeof (mhioc_resv_desc_t), flag) != 0) { 20198 err = EFAULT; 20199 } else { 20200 err = 20201 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20202 un, SD_SCSI3_RESERVE, 20203 (uchar_t *)&resv_desc); 20204 } 20205 } 20206 } 20207 break; 20208 20209 case MHIOCGRP_PREEMPTANDABORT: 20210 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 20211 if ((err = drv_priv(cred_p)) != EPERM) { 20212 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20213 err = ENOTSUP; 20214 } else if (arg != NULL) { 20215 mhioc_preemptandabort_t preempt_abort; 20216 if (ddi_copyin((void *)arg, &preempt_abort, 20217 sizeof (mhioc_preemptandabort_t), 20218 flag) != 0) { 20219 err = EFAULT; 20220 } else { 20221 err = 20222 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20223 un, SD_SCSI3_PREEMPTANDABORT, 20224 (uchar_t *)&preempt_abort); 20225 } 20226 } 20227 } 20228 break; 20229 20230 case MHIOCGRP_REGISTERANDIGNOREKEY: 20231 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 20232 if ((err = drv_priv(cred_p)) != EPERM) { 20233 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20234 err = ENOTSUP; 20235 } else if (arg != NULL) { 20236 mhioc_registerandignorekey_t r_and_i; 20237 if (ddi_copyin((void *)arg, (void *)&r_and_i, 20238 sizeof (mhioc_registerandignorekey_t), 20239 flag) != 0) { 20240 err = EFAULT; 20241 } else { 20242 err = 20243 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20244 un, SD_SCSI3_REGISTERANDIGNOREKEY, 20245 (uchar_t *)&r_and_i); 20246 } 20247 } 20248 } 20249 break; 20250 20251 case USCSICMD: 20252 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 20253 cr = ddi_get_cred(); 20254 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 20255 err = EPERM; 20256 } else { 20257 enum uio_seg uioseg; 20258 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 20259 UIO_USERSPACE; 20260 if (un->un_f_format_in_progress == TRUE) { 20261 err = EAGAIN; 20262 break; 20263 } 20264 err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg, 20265 flag, uioseg, SD_PATH_STANDARD); 20266 } 20267 break; 20268 20269 case CDROMPAUSE: 20270 case CDROMRESUME: 20271 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 20272 if (!ISCD(un)) { 20273 err = ENOTTY; 20274 } else { 20275 err = sr_pause_resume(dev, cmd); 20276 } 20277 break; 20278 20279 case CDROMPLAYMSF: 20280 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 20281 if (!ISCD(un)) { 20282 err = ENOTTY; 20283 } else { 20284 err = sr_play_msf(dev, (caddr_t)arg, flag); 20285 } 20286 break; 20287 20288 case CDROMPLAYTRKIND: 20289 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 20290 #if defined(__i386) || defined(__amd64) 20291 /* 20292 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 20293 */ 20294 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20295 #else 20296 if (!ISCD(un)) { 20297 #endif 20298 err = ENOTTY; 20299 } else { 20300 err = sr_play_trkind(dev, (caddr_t)arg, flag); 20301 } 20302 break; 20303 20304 case CDROMREADTOCHDR: 20305 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 20306 if (!ISCD(un)) { 20307 err = ENOTTY; 20308 } else { 20309 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 20310 } 20311 break; 20312 20313 case CDROMREADTOCENTRY: 20314 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 20315 if (!ISCD(un)) { 20316 err = ENOTTY; 20317 } else { 20318 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 20319 } 20320 break; 20321 20322 case CDROMSTOP: 20323 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 20324 if (!ISCD(un)) { 20325 err = ENOTTY; 20326 } else { 20327 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 20328 SD_PATH_STANDARD); 20329 } 20330 break; 20331 20332 case CDROMSTART: 20333 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 20334 if (!ISCD(un)) { 20335 err = ENOTTY; 20336 } else { 20337 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20338 SD_PATH_STANDARD); 20339 } 20340 break; 20341 20342 case CDROMCLOSETRAY: 20343 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 20344 if (!ISCD(un)) { 20345 err = ENOTTY; 20346 } else { 20347 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 20348 SD_PATH_STANDARD); 20349 } 20350 break; 20351 20352 case FDEJECT: /* for eject command */ 20353 case DKIOCEJECT: 20354 case CDROMEJECT: 20355 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 20356 if (!un->un_f_eject_media_supported) { 20357 err = ENOTTY; 20358 } else { 20359 err = sr_eject(dev); 20360 } 20361 break; 20362 20363 case CDROMVOLCTRL: 20364 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 20365 if (!ISCD(un)) { 20366 err = ENOTTY; 20367 } else { 20368 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 20369 } 20370 break; 20371 20372 case CDROMSUBCHNL: 20373 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 20374 if (!ISCD(un)) { 20375 err = ENOTTY; 20376 } else { 20377 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 20378 } 20379 break; 20380 20381 case CDROMREADMODE2: 20382 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 20383 if (!ISCD(un)) { 20384 err = ENOTTY; 20385 } else if (un->un_f_cfg_is_atapi == TRUE) { 20386 /* 20387 * If the drive supports READ CD, use that instead of 20388 * switching the LBA size via a MODE SELECT 20389 * Block Descriptor 20390 */ 20391 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 20392 } else { 20393 err = sr_read_mode2(dev, (caddr_t)arg, flag); 20394 } 20395 break; 20396 20397 case CDROMREADMODE1: 20398 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 20399 if (!ISCD(un)) { 20400 err = ENOTTY; 20401 } else { 20402 err = sr_read_mode1(dev, (caddr_t)arg, flag); 20403 } 20404 break; 20405 20406 case CDROMREADOFFSET: 20407 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 20408 if (!ISCD(un)) { 20409 err = ENOTTY; 20410 } else { 20411 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 20412 flag); 20413 } 20414 break; 20415 20416 case CDROMSBLKMODE: 20417 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 20418 /* 20419 * There is no means of changing block size in case of atapi 20420 * drives, thus return ENOTTY if drive type is atapi 20421 */ 20422 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20423 err = ENOTTY; 20424 } else if (un->un_f_mmc_cap == TRUE) { 20425 20426 /* 20427 * MMC Devices do not support changing the 20428 * logical block size 20429 * 20430 * Note: EINVAL is being returned instead of ENOTTY to 20431 * maintain consistancy with the original mmc 20432 * driver update. 20433 */ 20434 err = EINVAL; 20435 } else { 20436 mutex_enter(SD_MUTEX(un)); 20437 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 20438 (un->un_ncmds_in_transport > 0)) { 20439 mutex_exit(SD_MUTEX(un)); 20440 err = EINVAL; 20441 } else { 20442 mutex_exit(SD_MUTEX(un)); 20443 err = sr_change_blkmode(dev, cmd, arg, flag); 20444 } 20445 } 20446 break; 20447 20448 case CDROMGBLKMODE: 20449 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 20450 if (!ISCD(un)) { 20451 err = ENOTTY; 20452 } else if ((un->un_f_cfg_is_atapi != FALSE) && 20453 (un->un_f_blockcount_is_valid != FALSE)) { 20454 /* 20455 * Drive is an ATAPI drive so return target block 20456 * size for ATAPI drives since we cannot change the 20457 * blocksize on ATAPI drives. Used primarily to detect 20458 * if an ATAPI cdrom is present. 20459 */ 20460 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 20461 sizeof (int), flag) != 0) { 20462 err = EFAULT; 20463 } else { 20464 err = 0; 20465 } 20466 20467 } else { 20468 /* 20469 * Drive supports changing block sizes via a Mode 20470 * Select. 20471 */ 20472 err = sr_change_blkmode(dev, cmd, arg, flag); 20473 } 20474 break; 20475 20476 case CDROMGDRVSPEED: 20477 case CDROMSDRVSPEED: 20478 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 20479 if (!ISCD(un)) { 20480 err = ENOTTY; 20481 } else if (un->un_f_mmc_cap == TRUE) { 20482 /* 20483 * Note: In the future the driver implementation 20484 * for getting and 20485 * setting cd speed should entail: 20486 * 1) If non-mmc try the Toshiba mode page 20487 * (sr_change_speed) 20488 * 2) If mmc but no support for Real Time Streaming try 20489 * the SET CD SPEED (0xBB) command 20490 * (sr_atapi_change_speed) 20491 * 3) If mmc and support for Real Time Streaming 20492 * try the GET PERFORMANCE and SET STREAMING 20493 * commands (not yet implemented, 4380808) 20494 */ 20495 /* 20496 * As per recent MMC spec, CD-ROM speed is variable 20497 * and changes with LBA. Since there is no such 20498 * things as drive speed now, fail this ioctl. 20499 * 20500 * Note: EINVAL is returned for consistancy of original 20501 * implementation which included support for getting 20502 * the drive speed of mmc devices but not setting 20503 * the drive speed. Thus EINVAL would be returned 20504 * if a set request was made for an mmc device. 20505 * We no longer support get or set speed for 20506 * mmc but need to remain consistent with regard 20507 * to the error code returned. 20508 */ 20509 err = EINVAL; 20510 } else if (un->un_f_cfg_is_atapi == TRUE) { 20511 err = sr_atapi_change_speed(dev, cmd, arg, flag); 20512 } else { 20513 err = sr_change_speed(dev, cmd, arg, flag); 20514 } 20515 break; 20516 20517 case CDROMCDDA: 20518 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 20519 if (!ISCD(un)) { 20520 err = ENOTTY; 20521 } else { 20522 err = sr_read_cdda(dev, (void *)arg, flag); 20523 } 20524 break; 20525 20526 case CDROMCDXA: 20527 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 20528 if (!ISCD(un)) { 20529 err = ENOTTY; 20530 } else { 20531 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 20532 } 20533 break; 20534 20535 case CDROMSUBCODE: 20536 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 20537 if (!ISCD(un)) { 20538 err = ENOTTY; 20539 } else { 20540 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 20541 } 20542 break; 20543 20544 20545 #ifdef SDDEBUG 20546 /* RESET/ABORTS testing ioctls */ 20547 case DKIOCRESET: { 20548 int reset_level; 20549 20550 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 20551 err = EFAULT; 20552 } else { 20553 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 20554 "reset_level = 0x%lx\n", reset_level); 20555 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 20556 err = 0; 20557 } else { 20558 err = EIO; 20559 } 20560 } 20561 break; 20562 } 20563 20564 case DKIOCABORT: 20565 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 20566 if (scsi_abort(SD_ADDRESS(un), NULL)) { 20567 err = 0; 20568 } else { 20569 err = EIO; 20570 } 20571 break; 20572 #endif 20573 20574 #ifdef SD_FAULT_INJECTION 20575 /* SDIOC FaultInjection testing ioctls */ 20576 case SDIOCSTART: 20577 case SDIOCSTOP: 20578 case SDIOCINSERTPKT: 20579 case SDIOCINSERTXB: 20580 case SDIOCINSERTUN: 20581 case SDIOCINSERTARQ: 20582 case SDIOCPUSH: 20583 case SDIOCRETRIEVE: 20584 case SDIOCRUN: 20585 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 20586 "SDIOC detected cmd:0x%X:\n", cmd); 20587 /* call error generator */ 20588 sd_faultinjection_ioctl(cmd, arg, un); 20589 err = 0; 20590 break; 20591 20592 #endif /* SD_FAULT_INJECTION */ 20593 20594 case DKIOCFLUSHWRITECACHE: 20595 { 20596 struct dk_callback *dkc = (struct dk_callback *)arg; 20597 20598 mutex_enter(SD_MUTEX(un)); 20599 if (!un->un_f_sync_cache_supported || 20600 !un->un_f_write_cache_enabled) { 20601 err = un->un_f_sync_cache_supported ? 20602 0 : ENOTSUP; 20603 mutex_exit(SD_MUTEX(un)); 20604 if ((flag & FKIOCTL) && dkc != NULL && 20605 dkc->dkc_callback != NULL) { 20606 (*dkc->dkc_callback)(dkc->dkc_cookie, 20607 err); 20608 /* 20609 * Did callback and reported error. 20610 * Since we did a callback, ioctl 20611 * should return 0. 20612 */ 20613 err = 0; 20614 } 20615 break; 20616 } 20617 mutex_exit(SD_MUTEX(un)); 20618 20619 if ((flag & FKIOCTL) && dkc != NULL && 20620 dkc->dkc_callback != NULL) { 20621 /* async SYNC CACHE request */ 20622 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 20623 } else { 20624 /* synchronous SYNC CACHE request */ 20625 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 20626 } 20627 } 20628 break; 20629 20630 case DKIOCGETWCE: { 20631 20632 int wce; 20633 20634 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 20635 break; 20636 } 20637 20638 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 20639 err = EFAULT; 20640 } 20641 break; 20642 } 20643 20644 case DKIOCSETWCE: { 20645 20646 int wce, sync_supported; 20647 20648 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 20649 err = EFAULT; 20650 break; 20651 } 20652 20653 /* 20654 * Synchronize multiple threads trying to enable 20655 * or disable the cache via the un_f_wcc_cv 20656 * condition variable. 20657 */ 20658 mutex_enter(SD_MUTEX(un)); 20659 20660 /* 20661 * Don't allow the cache to be enabled if the 20662 * config file has it disabled. 20663 */ 20664 if (un->un_f_opt_disable_cache && wce) { 20665 mutex_exit(SD_MUTEX(un)); 20666 err = EINVAL; 20667 break; 20668 } 20669 20670 /* 20671 * Wait for write cache change in progress 20672 * bit to be clear before proceeding. 20673 */ 20674 while (un->un_f_wcc_inprog) 20675 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 20676 20677 un->un_f_wcc_inprog = 1; 20678 20679 if (un->un_f_write_cache_enabled && wce == 0) { 20680 /* 20681 * Disable the write cache. Don't clear 20682 * un_f_write_cache_enabled until after 20683 * the mode select and flush are complete. 20684 */ 20685 sync_supported = un->un_f_sync_cache_supported; 20686 20687 /* 20688 * If cache flush is suppressed, we assume that the 20689 * controller firmware will take care of managing the 20690 * write cache for us: no need to explicitly 20691 * disable it. 20692 */ 20693 if (!un->un_f_suppress_cache_flush) { 20694 mutex_exit(SD_MUTEX(un)); 20695 if ((err = sd_cache_control(un, 20696 SD_CACHE_NOCHANGE, 20697 SD_CACHE_DISABLE)) == 0 && 20698 sync_supported) { 20699 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 20700 NULL); 20701 } 20702 } else { 20703 mutex_exit(SD_MUTEX(un)); 20704 } 20705 20706 mutex_enter(SD_MUTEX(un)); 20707 if (err == 0) { 20708 un->un_f_write_cache_enabled = 0; 20709 } 20710 20711 } else if (!un->un_f_write_cache_enabled && wce != 0) { 20712 /* 20713 * Set un_f_write_cache_enabled first, so there is 20714 * no window where the cache is enabled, but the 20715 * bit says it isn't. 20716 */ 20717 un->un_f_write_cache_enabled = 1; 20718 20719 /* 20720 * If cache flush is suppressed, we assume that the 20721 * controller firmware will take care of managing the 20722 * write cache for us: no need to explicitly 20723 * enable it. 20724 */ 20725 if (!un->un_f_suppress_cache_flush) { 20726 mutex_exit(SD_MUTEX(un)); 20727 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 20728 SD_CACHE_ENABLE); 20729 } else { 20730 mutex_exit(SD_MUTEX(un)); 20731 } 20732 20733 mutex_enter(SD_MUTEX(un)); 20734 20735 if (err) { 20736 un->un_f_write_cache_enabled = 0; 20737 } 20738 } 20739 20740 un->un_f_wcc_inprog = 0; 20741 cv_broadcast(&un->un_wcc_cv); 20742 mutex_exit(SD_MUTEX(un)); 20743 break; 20744 } 20745 20746 default: 20747 err = ENOTTY; 20748 break; 20749 } 20750 mutex_enter(SD_MUTEX(un)); 20751 un->un_ncmds_in_driver--; 20752 ASSERT(un->un_ncmds_in_driver >= 0); 20753 mutex_exit(SD_MUTEX(un)); 20754 20755 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 20756 return (err); 20757 } 20758 20759 20760 /* 20761 * Function: sd_dkio_ctrl_info 20762 * 20763 * Description: This routine is the driver entry point for handling controller 20764 * information ioctl requests (DKIOCINFO). 20765 * 20766 * Arguments: dev - the device number 20767 * arg - pointer to user provided dk_cinfo structure 20768 * specifying the controller type and attributes. 20769 * flag - this argument is a pass through to ddi_copyxxx() 20770 * directly from the mode argument of ioctl(). 20771 * 20772 * Return Code: 0 20773 * EFAULT 20774 * ENXIO 20775 */ 20776 20777 static int 20778 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 20779 { 20780 struct sd_lun *un = NULL; 20781 struct dk_cinfo *info; 20782 dev_info_t *pdip; 20783 int lun, tgt; 20784 20785 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20786 return (ENXIO); 20787 } 20788 20789 info = (struct dk_cinfo *) 20790 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 20791 20792 switch (un->un_ctype) { 20793 case CTYPE_CDROM: 20794 info->dki_ctype = DKC_CDROM; 20795 break; 20796 default: 20797 info->dki_ctype = DKC_SCSI_CCS; 20798 break; 20799 } 20800 pdip = ddi_get_parent(SD_DEVINFO(un)); 20801 info->dki_cnum = ddi_get_instance(pdip); 20802 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 20803 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 20804 } else { 20805 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 20806 DK_DEVLEN - 1); 20807 } 20808 20809 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20810 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 20811 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20812 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 20813 20814 /* Unit Information */ 20815 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 20816 info->dki_slave = ((tgt << 3) | lun); 20817 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 20818 DK_DEVLEN - 1); 20819 info->dki_flags = DKI_FMTVOL; 20820 info->dki_partition = SDPART(dev); 20821 20822 /* Max Transfer size of this device in blocks */ 20823 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 20824 info->dki_addr = 0; 20825 info->dki_space = 0; 20826 info->dki_prio = 0; 20827 info->dki_vec = 0; 20828 20829 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 20830 kmem_free(info, sizeof (struct dk_cinfo)); 20831 return (EFAULT); 20832 } else { 20833 kmem_free(info, sizeof (struct dk_cinfo)); 20834 return (0); 20835 } 20836 } 20837 20838 20839 /* 20840 * Function: sd_get_media_info 20841 * 20842 * Description: This routine is the driver entry point for handling ioctl 20843 * requests for the media type or command set profile used by the 20844 * drive to operate on the media (DKIOCGMEDIAINFO). 20845 * 20846 * Arguments: dev - the device number 20847 * arg - pointer to user provided dk_minfo structure 20848 * specifying the media type, logical block size and 20849 * drive capacity. 20850 * flag - this argument is a pass through to ddi_copyxxx() 20851 * directly from the mode argument of ioctl(). 20852 * 20853 * Return Code: 0 20854 * EACCESS 20855 * EFAULT 20856 * ENXIO 20857 * EIO 20858 */ 20859 20860 static int 20861 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 20862 { 20863 struct sd_lun *un = NULL; 20864 struct uscsi_cmd com; 20865 struct scsi_inquiry *sinq; 20866 struct dk_minfo media_info; 20867 u_longlong_t media_capacity; 20868 uint64_t capacity; 20869 uint_t lbasize; 20870 uchar_t *out_data; 20871 uchar_t *rqbuf; 20872 int rval = 0; 20873 int rtn; 20874 20875 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 20876 (un->un_state == SD_STATE_OFFLINE)) { 20877 return (ENXIO); 20878 } 20879 20880 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 20881 20882 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 20883 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20884 20885 /* Issue a TUR to determine if the drive is ready with media present */ 20886 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 20887 if (rval == ENXIO) { 20888 goto done; 20889 } 20890 20891 /* Now get configuration data */ 20892 if (ISCD(un)) { 20893 media_info.dki_media_type = DK_CDROM; 20894 20895 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 20896 if (un->un_f_mmc_cap == TRUE) { 20897 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 20898 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 20899 SD_PATH_STANDARD); 20900 20901 if (rtn) { 20902 /* 20903 * Failed for other than an illegal request 20904 * or command not supported 20905 */ 20906 if ((com.uscsi_status == STATUS_CHECK) && 20907 (com.uscsi_rqstatus == STATUS_GOOD)) { 20908 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 20909 (rqbuf[12] != 0x20)) { 20910 rval = EIO; 20911 goto done; 20912 } 20913 } 20914 } else { 20915 /* 20916 * The GET CONFIGURATION command succeeded 20917 * so set the media type according to the 20918 * returned data 20919 */ 20920 media_info.dki_media_type = out_data[6]; 20921 media_info.dki_media_type <<= 8; 20922 media_info.dki_media_type |= out_data[7]; 20923 } 20924 } 20925 } else { 20926 /* 20927 * The profile list is not available, so we attempt to identify 20928 * the media type based on the inquiry data 20929 */ 20930 sinq = un->un_sd->sd_inq; 20931 if ((sinq->inq_dtype == DTYPE_DIRECT) || 20932 (sinq->inq_dtype == DTYPE_OPTICAL)) { 20933 /* This is a direct access device or optical disk */ 20934 media_info.dki_media_type = DK_FIXED_DISK; 20935 20936 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 20937 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 20938 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 20939 media_info.dki_media_type = DK_ZIP; 20940 } else if ( 20941 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 20942 media_info.dki_media_type = DK_JAZ; 20943 } 20944 } 20945 } else { 20946 /* 20947 * Not a CD, direct access or optical disk so return 20948 * unknown media 20949 */ 20950 media_info.dki_media_type = DK_UNKNOWN; 20951 } 20952 } 20953 20954 /* Now read the capacity so we can provide the lbasize and capacity */ 20955 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 20956 SD_PATH_DIRECT)) { 20957 case 0: 20958 break; 20959 case EACCES: 20960 rval = EACCES; 20961 goto done; 20962 default: 20963 rval = EIO; 20964 goto done; 20965 } 20966 20967 /* 20968 * If lun is expanded dynamically, update the un structure. 20969 */ 20970 mutex_enter(SD_MUTEX(un)); 20971 if ((un->un_f_blockcount_is_valid == TRUE) && 20972 (un->un_f_tgt_blocksize_is_valid == TRUE) && 20973 (capacity > un->un_blockcount)) { 20974 sd_update_block_info(un, lbasize, capacity); 20975 } 20976 mutex_exit(SD_MUTEX(un)); 20977 20978 media_info.dki_lbsize = lbasize; 20979 media_capacity = capacity; 20980 20981 /* 20982 * sd_send_scsi_READ_CAPACITY() reports capacity in 20983 * un->un_sys_blocksize chunks. So we need to convert it into 20984 * cap.lbasize chunks. 20985 */ 20986 media_capacity *= un->un_sys_blocksize; 20987 media_capacity /= lbasize; 20988 media_info.dki_capacity = media_capacity; 20989 20990 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 20991 rval = EFAULT; 20992 /* Put goto. Anybody might add some code below in future */ 20993 goto done; 20994 } 20995 done: 20996 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 20997 kmem_free(rqbuf, SENSE_LENGTH); 20998 return (rval); 20999 } 21000 21001 21002 /* 21003 * Function: sd_check_media 21004 * 21005 * Description: This utility routine implements the functionality for the 21006 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 21007 * driver state changes from that specified by the user 21008 * (inserted or ejected). For example, if the user specifies 21009 * DKIO_EJECTED and the current media state is inserted this 21010 * routine will immediately return DKIO_INSERTED. However, if the 21011 * current media state is not inserted the user thread will be 21012 * blocked until the drive state changes. If DKIO_NONE is specified 21013 * the user thread will block until a drive state change occurs. 21014 * 21015 * Arguments: dev - the device number 21016 * state - user pointer to a dkio_state, updated with the current 21017 * drive state at return. 21018 * 21019 * Return Code: ENXIO 21020 * EIO 21021 * EAGAIN 21022 * EINTR 21023 */ 21024 21025 static int 21026 sd_check_media(dev_t dev, enum dkio_state state) 21027 { 21028 struct sd_lun *un = NULL; 21029 enum dkio_state prev_state; 21030 opaque_t token = NULL; 21031 int rval = 0; 21032 21033 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21034 return (ENXIO); 21035 } 21036 21037 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 21038 21039 mutex_enter(SD_MUTEX(un)); 21040 21041 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 21042 "state=%x, mediastate=%x\n", state, un->un_mediastate); 21043 21044 prev_state = un->un_mediastate; 21045 21046 /* is there anything to do? */ 21047 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 21048 /* 21049 * submit the request to the scsi_watch service; 21050 * scsi_media_watch_cb() does the real work 21051 */ 21052 mutex_exit(SD_MUTEX(un)); 21053 21054 /* 21055 * This change handles the case where a scsi watch request is 21056 * added to a device that is powered down. To accomplish this 21057 * we power up the device before adding the scsi watch request, 21058 * since the scsi watch sends a TUR directly to the device 21059 * which the device cannot handle if it is powered down. 21060 */ 21061 if (sd_pm_entry(un) != DDI_SUCCESS) { 21062 mutex_enter(SD_MUTEX(un)); 21063 goto done; 21064 } 21065 21066 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 21067 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 21068 (caddr_t)dev); 21069 21070 sd_pm_exit(un); 21071 21072 mutex_enter(SD_MUTEX(un)); 21073 if (token == NULL) { 21074 rval = EAGAIN; 21075 goto done; 21076 } 21077 21078 /* 21079 * This is a special case IOCTL that doesn't return 21080 * until the media state changes. Routine sdpower 21081 * knows about and handles this so don't count it 21082 * as an active cmd in the driver, which would 21083 * keep the device busy to the pm framework. 21084 * If the count isn't decremented the device can't 21085 * be powered down. 21086 */ 21087 un->un_ncmds_in_driver--; 21088 ASSERT(un->un_ncmds_in_driver >= 0); 21089 21090 /* 21091 * if a prior request had been made, this will be the same 21092 * token, as scsi_watch was designed that way. 21093 */ 21094 un->un_swr_token = token; 21095 un->un_specified_mediastate = state; 21096 21097 /* 21098 * now wait for media change 21099 * we will not be signalled unless mediastate == state but it is 21100 * still better to test for this condition, since there is a 21101 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 21102 */ 21103 SD_TRACE(SD_LOG_COMMON, un, 21104 "sd_check_media: waiting for media state change\n"); 21105 while (un->un_mediastate == state) { 21106 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 21107 SD_TRACE(SD_LOG_COMMON, un, 21108 "sd_check_media: waiting for media state " 21109 "was interrupted\n"); 21110 un->un_ncmds_in_driver++; 21111 rval = EINTR; 21112 goto done; 21113 } 21114 SD_TRACE(SD_LOG_COMMON, un, 21115 "sd_check_media: received signal, state=%x\n", 21116 un->un_mediastate); 21117 } 21118 /* 21119 * Inc the counter to indicate the device once again 21120 * has an active outstanding cmd. 21121 */ 21122 un->un_ncmds_in_driver++; 21123 } 21124 21125 /* invalidate geometry */ 21126 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 21127 sr_ejected(un); 21128 } 21129 21130 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 21131 uint64_t capacity; 21132 uint_t lbasize; 21133 21134 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 21135 mutex_exit(SD_MUTEX(un)); 21136 /* 21137 * Since the following routines use SD_PATH_DIRECT, we must 21138 * call PM directly before the upcoming disk accesses. This 21139 * may cause the disk to be power/spin up. 21140 */ 21141 21142 if (sd_pm_entry(un) == DDI_SUCCESS) { 21143 rval = sd_send_scsi_READ_CAPACITY(un, 21144 &capacity, 21145 &lbasize, SD_PATH_DIRECT); 21146 if (rval != 0) { 21147 sd_pm_exit(un); 21148 mutex_enter(SD_MUTEX(un)); 21149 goto done; 21150 } 21151 } else { 21152 rval = EIO; 21153 mutex_enter(SD_MUTEX(un)); 21154 goto done; 21155 } 21156 mutex_enter(SD_MUTEX(un)); 21157 21158 sd_update_block_info(un, lbasize, capacity); 21159 21160 /* 21161 * Check if the media in the device is writable or not 21162 */ 21163 if (ISCD(un)) 21164 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 21165 21166 mutex_exit(SD_MUTEX(un)); 21167 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 21168 if ((cmlb_validate(un->un_cmlbhandle, 0, 21169 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 21170 sd_set_pstats(un); 21171 SD_TRACE(SD_LOG_IO_PARTITION, un, 21172 "sd_check_media: un:0x%p pstats created and " 21173 "set\n", un); 21174 } 21175 21176 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21177 SD_PATH_DIRECT); 21178 sd_pm_exit(un); 21179 21180 mutex_enter(SD_MUTEX(un)); 21181 } 21182 done: 21183 un->un_f_watcht_stopped = FALSE; 21184 /* 21185 * Use of this local token and the mutex ensures that we avoid 21186 * some race conditions associated with terminating the 21187 * scsi watch. 21188 */ 21189 if (token) { 21190 un->un_swr_token = (opaque_t)NULL; 21191 mutex_exit(SD_MUTEX(un)); 21192 (void) scsi_watch_request_terminate(token, 21193 SCSI_WATCH_TERMINATE_WAIT); 21194 mutex_enter(SD_MUTEX(un)); 21195 } 21196 21197 /* 21198 * Update the capacity kstat value, if no media previously 21199 * (capacity kstat is 0) and a media has been inserted 21200 * (un_f_blockcount_is_valid == TRUE) 21201 */ 21202 if (un->un_errstats) { 21203 struct sd_errstats *stp = NULL; 21204 21205 stp = (struct sd_errstats *)un->un_errstats->ks_data; 21206 if ((stp->sd_capacity.value.ui64 == 0) && 21207 (un->un_f_blockcount_is_valid == TRUE)) { 21208 stp->sd_capacity.value.ui64 = 21209 (uint64_t)((uint64_t)un->un_blockcount * 21210 un->un_sys_blocksize); 21211 } 21212 } 21213 mutex_exit(SD_MUTEX(un)); 21214 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 21215 return (rval); 21216 } 21217 21218 21219 /* 21220 * Function: sd_delayed_cv_broadcast 21221 * 21222 * Description: Delayed cv_broadcast to allow for target to recover from media 21223 * insertion. 21224 * 21225 * Arguments: arg - driver soft state (unit) structure 21226 */ 21227 21228 static void 21229 sd_delayed_cv_broadcast(void *arg) 21230 { 21231 struct sd_lun *un = arg; 21232 21233 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 21234 21235 mutex_enter(SD_MUTEX(un)); 21236 un->un_dcvb_timeid = NULL; 21237 cv_broadcast(&un->un_state_cv); 21238 mutex_exit(SD_MUTEX(un)); 21239 } 21240 21241 21242 /* 21243 * Function: sd_media_watch_cb 21244 * 21245 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 21246 * routine processes the TUR sense data and updates the driver 21247 * state if a transition has occurred. The user thread 21248 * (sd_check_media) is then signalled. 21249 * 21250 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21251 * among multiple watches that share this callback function 21252 * resultp - scsi watch facility result packet containing scsi 21253 * packet, status byte and sense data 21254 * 21255 * Return Code: 0 for success, -1 for failure 21256 */ 21257 21258 static int 21259 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21260 { 21261 struct sd_lun *un; 21262 struct scsi_status *statusp = resultp->statusp; 21263 uint8_t *sensep = (uint8_t *)resultp->sensep; 21264 enum dkio_state state = DKIO_NONE; 21265 dev_t dev = (dev_t)arg; 21266 uchar_t actual_sense_length; 21267 uint8_t skey, asc, ascq; 21268 21269 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21270 return (-1); 21271 } 21272 actual_sense_length = resultp->actual_sense_length; 21273 21274 mutex_enter(SD_MUTEX(un)); 21275 SD_TRACE(SD_LOG_COMMON, un, 21276 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 21277 *((char *)statusp), (void *)sensep, actual_sense_length); 21278 21279 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 21280 un->un_mediastate = DKIO_DEV_GONE; 21281 cv_broadcast(&un->un_state_cv); 21282 mutex_exit(SD_MUTEX(un)); 21283 21284 return (0); 21285 } 21286 21287 /* 21288 * If there was a check condition then sensep points to valid sense data 21289 * If status was not a check condition but a reservation or busy status 21290 * then the new state is DKIO_NONE 21291 */ 21292 if (sensep != NULL) { 21293 skey = scsi_sense_key(sensep); 21294 asc = scsi_sense_asc(sensep); 21295 ascq = scsi_sense_ascq(sensep); 21296 21297 SD_INFO(SD_LOG_COMMON, un, 21298 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 21299 skey, asc, ascq); 21300 /* This routine only uses up to 13 bytes of sense data. */ 21301 if (actual_sense_length >= 13) { 21302 if (skey == KEY_UNIT_ATTENTION) { 21303 if (asc == 0x28) { 21304 state = DKIO_INSERTED; 21305 } 21306 } else if (skey == KEY_NOT_READY) { 21307 /* 21308 * if 02/04/02 means that the host 21309 * should send start command. Explicitly 21310 * leave the media state as is 21311 * (inserted) as the media is inserted 21312 * and host has stopped device for PM 21313 * reasons. Upon next true read/write 21314 * to this media will bring the 21315 * device to the right state good for 21316 * media access. 21317 */ 21318 if (asc == 0x3a) { 21319 state = DKIO_EJECTED; 21320 } else { 21321 /* 21322 * If the drive is busy with an 21323 * operation or long write, keep the 21324 * media in an inserted state. 21325 */ 21326 21327 if ((asc == 0x04) && 21328 ((ascq == 0x02) || 21329 (ascq == 0x07) || 21330 (ascq == 0x08))) { 21331 state = DKIO_INSERTED; 21332 } 21333 } 21334 } else if (skey == KEY_NO_SENSE) { 21335 if ((asc == 0x00) && (ascq == 0x00)) { 21336 /* 21337 * Sense Data 00/00/00 does not provide 21338 * any information about the state of 21339 * the media. Ignore it. 21340 */ 21341 mutex_exit(SD_MUTEX(un)); 21342 return (0); 21343 } 21344 } 21345 } 21346 } else if ((*((char *)statusp) == STATUS_GOOD) && 21347 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 21348 state = DKIO_INSERTED; 21349 } 21350 21351 SD_TRACE(SD_LOG_COMMON, un, 21352 "sd_media_watch_cb: state=%x, specified=%x\n", 21353 state, un->un_specified_mediastate); 21354 21355 /* 21356 * now signal the waiting thread if this is *not* the specified state; 21357 * delay the signal if the state is DKIO_INSERTED to allow the target 21358 * to recover 21359 */ 21360 if (state != un->un_specified_mediastate) { 21361 un->un_mediastate = state; 21362 if (state == DKIO_INSERTED) { 21363 /* 21364 * delay the signal to give the drive a chance 21365 * to do what it apparently needs to do 21366 */ 21367 SD_TRACE(SD_LOG_COMMON, un, 21368 "sd_media_watch_cb: delayed cv_broadcast\n"); 21369 if (un->un_dcvb_timeid == NULL) { 21370 un->un_dcvb_timeid = 21371 timeout(sd_delayed_cv_broadcast, un, 21372 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 21373 } 21374 } else { 21375 SD_TRACE(SD_LOG_COMMON, un, 21376 "sd_media_watch_cb: immediate cv_broadcast\n"); 21377 cv_broadcast(&un->un_state_cv); 21378 } 21379 } 21380 mutex_exit(SD_MUTEX(un)); 21381 return (0); 21382 } 21383 21384 21385 /* 21386 * Function: sd_dkio_get_temp 21387 * 21388 * Description: This routine is the driver entry point for handling ioctl 21389 * requests to get the disk temperature. 21390 * 21391 * Arguments: dev - the device number 21392 * arg - pointer to user provided dk_temperature structure. 21393 * flag - this argument is a pass through to ddi_copyxxx() 21394 * directly from the mode argument of ioctl(). 21395 * 21396 * Return Code: 0 21397 * EFAULT 21398 * ENXIO 21399 * EAGAIN 21400 */ 21401 21402 static int 21403 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 21404 { 21405 struct sd_lun *un = NULL; 21406 struct dk_temperature *dktemp = NULL; 21407 uchar_t *temperature_page; 21408 int rval = 0; 21409 int path_flag = SD_PATH_STANDARD; 21410 21411 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21412 return (ENXIO); 21413 } 21414 21415 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 21416 21417 /* copyin the disk temp argument to get the user flags */ 21418 if (ddi_copyin((void *)arg, dktemp, 21419 sizeof (struct dk_temperature), flag) != 0) { 21420 rval = EFAULT; 21421 goto done; 21422 } 21423 21424 /* Initialize the temperature to invalid. */ 21425 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21426 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21427 21428 /* 21429 * Note: Investigate removing the "bypass pm" semantic. 21430 * Can we just bypass PM always? 21431 */ 21432 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 21433 path_flag = SD_PATH_DIRECT; 21434 ASSERT(!mutex_owned(&un->un_pm_mutex)); 21435 mutex_enter(&un->un_pm_mutex); 21436 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 21437 /* 21438 * If DKT_BYPASS_PM is set, and the drive happens to be 21439 * in low power mode, we can not wake it up, Need to 21440 * return EAGAIN. 21441 */ 21442 mutex_exit(&un->un_pm_mutex); 21443 rval = EAGAIN; 21444 goto done; 21445 } else { 21446 /* 21447 * Indicate to PM the device is busy. This is required 21448 * to avoid a race - i.e. the ioctl is issuing a 21449 * command and the pm framework brings down the device 21450 * to low power mode (possible power cut-off on some 21451 * platforms). 21452 */ 21453 mutex_exit(&un->un_pm_mutex); 21454 if (sd_pm_entry(un) != DDI_SUCCESS) { 21455 rval = EAGAIN; 21456 goto done; 21457 } 21458 } 21459 } 21460 21461 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 21462 21463 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 21464 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 21465 goto done2; 21466 } 21467 21468 /* 21469 * For the current temperature verify that the parameter length is 0x02 21470 * and the parameter code is 0x00 21471 */ 21472 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 21473 (temperature_page[5] == 0x00)) { 21474 if (temperature_page[9] == 0xFF) { 21475 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21476 } else { 21477 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 21478 } 21479 } 21480 21481 /* 21482 * For the reference temperature verify that the parameter 21483 * length is 0x02 and the parameter code is 0x01 21484 */ 21485 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 21486 (temperature_page[11] == 0x01)) { 21487 if (temperature_page[15] == 0xFF) { 21488 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21489 } else { 21490 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 21491 } 21492 } 21493 21494 /* Do the copyout regardless of the temperature commands status. */ 21495 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 21496 flag) != 0) { 21497 rval = EFAULT; 21498 } 21499 21500 done2: 21501 if (path_flag == SD_PATH_DIRECT) { 21502 sd_pm_exit(un); 21503 } 21504 21505 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 21506 done: 21507 if (dktemp != NULL) { 21508 kmem_free(dktemp, sizeof (struct dk_temperature)); 21509 } 21510 21511 return (rval); 21512 } 21513 21514 21515 /* 21516 * Function: sd_log_page_supported 21517 * 21518 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 21519 * supported log pages. 21520 * 21521 * Arguments: un - 21522 * log_page - 21523 * 21524 * Return Code: -1 - on error (log sense is optional and may not be supported). 21525 * 0 - log page not found. 21526 * 1 - log page found. 21527 */ 21528 21529 static int 21530 sd_log_page_supported(struct sd_lun *un, int log_page) 21531 { 21532 uchar_t *log_page_data; 21533 int i; 21534 int match = 0; 21535 int log_size; 21536 21537 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 21538 21539 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 21540 SD_PATH_DIRECT) != 0) { 21541 SD_ERROR(SD_LOG_COMMON, un, 21542 "sd_log_page_supported: failed log page retrieval\n"); 21543 kmem_free(log_page_data, 0xFF); 21544 return (-1); 21545 } 21546 log_size = log_page_data[3]; 21547 21548 /* 21549 * The list of supported log pages start from the fourth byte. Check 21550 * until we run out of log pages or a match is found. 21551 */ 21552 for (i = 4; (i < (log_size + 4)) && !match; i++) { 21553 if (log_page_data[i] == log_page) { 21554 match++; 21555 } 21556 } 21557 kmem_free(log_page_data, 0xFF); 21558 return (match); 21559 } 21560 21561 21562 /* 21563 * Function: sd_mhdioc_failfast 21564 * 21565 * Description: This routine is the driver entry point for handling ioctl 21566 * requests to enable/disable the multihost failfast option. 21567 * (MHIOCENFAILFAST) 21568 * 21569 * Arguments: dev - the device number 21570 * arg - user specified probing interval. 21571 * flag - this argument is a pass through to ddi_copyxxx() 21572 * directly from the mode argument of ioctl(). 21573 * 21574 * Return Code: 0 21575 * EFAULT 21576 * ENXIO 21577 */ 21578 21579 static int 21580 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 21581 { 21582 struct sd_lun *un = NULL; 21583 int mh_time; 21584 int rval = 0; 21585 21586 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21587 return (ENXIO); 21588 } 21589 21590 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 21591 return (EFAULT); 21592 21593 if (mh_time) { 21594 mutex_enter(SD_MUTEX(un)); 21595 un->un_resvd_status |= SD_FAILFAST; 21596 mutex_exit(SD_MUTEX(un)); 21597 /* 21598 * If mh_time is INT_MAX, then this ioctl is being used for 21599 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 21600 */ 21601 if (mh_time != INT_MAX) { 21602 rval = sd_check_mhd(dev, mh_time); 21603 } 21604 } else { 21605 (void) sd_check_mhd(dev, 0); 21606 mutex_enter(SD_MUTEX(un)); 21607 un->un_resvd_status &= ~SD_FAILFAST; 21608 mutex_exit(SD_MUTEX(un)); 21609 } 21610 return (rval); 21611 } 21612 21613 21614 /* 21615 * Function: sd_mhdioc_takeown 21616 * 21617 * Description: This routine is the driver entry point for handling ioctl 21618 * requests to forcefully acquire exclusive access rights to the 21619 * multihost disk (MHIOCTKOWN). 21620 * 21621 * Arguments: dev - the device number 21622 * arg - user provided structure specifying the delay 21623 * parameters in milliseconds 21624 * flag - this argument is a pass through to ddi_copyxxx() 21625 * directly from the mode argument of ioctl(). 21626 * 21627 * Return Code: 0 21628 * EFAULT 21629 * ENXIO 21630 */ 21631 21632 static int 21633 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 21634 { 21635 struct sd_lun *un = NULL; 21636 struct mhioctkown *tkown = NULL; 21637 int rval = 0; 21638 21639 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21640 return (ENXIO); 21641 } 21642 21643 if (arg != NULL) { 21644 tkown = (struct mhioctkown *) 21645 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 21646 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 21647 if (rval != 0) { 21648 rval = EFAULT; 21649 goto error; 21650 } 21651 } 21652 21653 rval = sd_take_ownership(dev, tkown); 21654 mutex_enter(SD_MUTEX(un)); 21655 if (rval == 0) { 21656 un->un_resvd_status |= SD_RESERVE; 21657 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 21658 sd_reinstate_resv_delay = 21659 tkown->reinstate_resv_delay * 1000; 21660 } else { 21661 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 21662 } 21663 /* 21664 * Give the scsi_watch routine interval set by 21665 * the MHIOCENFAILFAST ioctl precedence here. 21666 */ 21667 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 21668 mutex_exit(SD_MUTEX(un)); 21669 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 21670 SD_TRACE(SD_LOG_IOCTL_MHD, un, 21671 "sd_mhdioc_takeown : %d\n", 21672 sd_reinstate_resv_delay); 21673 } else { 21674 mutex_exit(SD_MUTEX(un)); 21675 } 21676 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 21677 sd_mhd_reset_notify_cb, (caddr_t)un); 21678 } else { 21679 un->un_resvd_status &= ~SD_RESERVE; 21680 mutex_exit(SD_MUTEX(un)); 21681 } 21682 21683 error: 21684 if (tkown != NULL) { 21685 kmem_free(tkown, sizeof (struct mhioctkown)); 21686 } 21687 return (rval); 21688 } 21689 21690 21691 /* 21692 * Function: sd_mhdioc_release 21693 * 21694 * Description: This routine is the driver entry point for handling ioctl 21695 * requests to release exclusive access rights to the multihost 21696 * disk (MHIOCRELEASE). 21697 * 21698 * Arguments: dev - the device number 21699 * 21700 * Return Code: 0 21701 * ENXIO 21702 */ 21703 21704 static int 21705 sd_mhdioc_release(dev_t dev) 21706 { 21707 struct sd_lun *un = NULL; 21708 timeout_id_t resvd_timeid_save; 21709 int resvd_status_save; 21710 int rval = 0; 21711 21712 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21713 return (ENXIO); 21714 } 21715 21716 mutex_enter(SD_MUTEX(un)); 21717 resvd_status_save = un->un_resvd_status; 21718 un->un_resvd_status &= 21719 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 21720 if (un->un_resvd_timeid) { 21721 resvd_timeid_save = un->un_resvd_timeid; 21722 un->un_resvd_timeid = NULL; 21723 mutex_exit(SD_MUTEX(un)); 21724 (void) untimeout(resvd_timeid_save); 21725 } else { 21726 mutex_exit(SD_MUTEX(un)); 21727 } 21728 21729 /* 21730 * destroy any pending timeout thread that may be attempting to 21731 * reinstate reservation on this device. 21732 */ 21733 sd_rmv_resv_reclaim_req(dev); 21734 21735 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 21736 mutex_enter(SD_MUTEX(un)); 21737 if ((un->un_mhd_token) && 21738 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 21739 mutex_exit(SD_MUTEX(un)); 21740 (void) sd_check_mhd(dev, 0); 21741 } else { 21742 mutex_exit(SD_MUTEX(un)); 21743 } 21744 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 21745 sd_mhd_reset_notify_cb, (caddr_t)un); 21746 } else { 21747 /* 21748 * sd_mhd_watch_cb will restart the resvd recover timeout thread 21749 */ 21750 mutex_enter(SD_MUTEX(un)); 21751 un->un_resvd_status = resvd_status_save; 21752 mutex_exit(SD_MUTEX(un)); 21753 } 21754 return (rval); 21755 } 21756 21757 21758 /* 21759 * Function: sd_mhdioc_register_devid 21760 * 21761 * Description: This routine is the driver entry point for handling ioctl 21762 * requests to register the device id (MHIOCREREGISTERDEVID). 21763 * 21764 * Note: The implementation for this ioctl has been updated to 21765 * be consistent with the original PSARC case (1999/357) 21766 * (4375899, 4241671, 4220005) 21767 * 21768 * Arguments: dev - the device number 21769 * 21770 * Return Code: 0 21771 * ENXIO 21772 */ 21773 21774 static int 21775 sd_mhdioc_register_devid(dev_t dev) 21776 { 21777 struct sd_lun *un = NULL; 21778 int rval = 0; 21779 21780 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21781 return (ENXIO); 21782 } 21783 21784 ASSERT(!mutex_owned(SD_MUTEX(un))); 21785 21786 mutex_enter(SD_MUTEX(un)); 21787 21788 /* If a devid already exists, de-register it */ 21789 if (un->un_devid != NULL) { 21790 ddi_devid_unregister(SD_DEVINFO(un)); 21791 /* 21792 * After unregister devid, needs to free devid memory 21793 */ 21794 ddi_devid_free(un->un_devid); 21795 un->un_devid = NULL; 21796 } 21797 21798 /* Check for reservation conflict */ 21799 mutex_exit(SD_MUTEX(un)); 21800 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 21801 mutex_enter(SD_MUTEX(un)); 21802 21803 switch (rval) { 21804 case 0: 21805 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 21806 break; 21807 case EACCES: 21808 break; 21809 default: 21810 rval = EIO; 21811 } 21812 21813 mutex_exit(SD_MUTEX(un)); 21814 return (rval); 21815 } 21816 21817 21818 /* 21819 * Function: sd_mhdioc_inkeys 21820 * 21821 * Description: This routine is the driver entry point for handling ioctl 21822 * requests to issue the SCSI-3 Persistent In Read Keys command 21823 * to the device (MHIOCGRP_INKEYS). 21824 * 21825 * Arguments: dev - the device number 21826 * arg - user provided in_keys structure 21827 * flag - this argument is a pass through to ddi_copyxxx() 21828 * directly from the mode argument of ioctl(). 21829 * 21830 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 21831 * ENXIO 21832 * EFAULT 21833 */ 21834 21835 static int 21836 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 21837 { 21838 struct sd_lun *un; 21839 mhioc_inkeys_t inkeys; 21840 int rval = 0; 21841 21842 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21843 return (ENXIO); 21844 } 21845 21846 #ifdef _MULTI_DATAMODEL 21847 switch (ddi_model_convert_from(flag & FMODELS)) { 21848 case DDI_MODEL_ILP32: { 21849 struct mhioc_inkeys32 inkeys32; 21850 21851 if (ddi_copyin(arg, &inkeys32, 21852 sizeof (struct mhioc_inkeys32), flag) != 0) { 21853 return (EFAULT); 21854 } 21855 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 21856 if ((rval = sd_persistent_reservation_in_read_keys(un, 21857 &inkeys, flag)) != 0) { 21858 return (rval); 21859 } 21860 inkeys32.generation = inkeys.generation; 21861 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 21862 flag) != 0) { 21863 return (EFAULT); 21864 } 21865 break; 21866 } 21867 case DDI_MODEL_NONE: 21868 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 21869 flag) != 0) { 21870 return (EFAULT); 21871 } 21872 if ((rval = sd_persistent_reservation_in_read_keys(un, 21873 &inkeys, flag)) != 0) { 21874 return (rval); 21875 } 21876 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 21877 flag) != 0) { 21878 return (EFAULT); 21879 } 21880 break; 21881 } 21882 21883 #else /* ! _MULTI_DATAMODEL */ 21884 21885 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 21886 return (EFAULT); 21887 } 21888 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 21889 if (rval != 0) { 21890 return (rval); 21891 } 21892 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 21893 return (EFAULT); 21894 } 21895 21896 #endif /* _MULTI_DATAMODEL */ 21897 21898 return (rval); 21899 } 21900 21901 21902 /* 21903 * Function: sd_mhdioc_inresv 21904 * 21905 * Description: This routine is the driver entry point for handling ioctl 21906 * requests to issue the SCSI-3 Persistent In Read Reservations 21907 * command to the device (MHIOCGRP_INKEYS). 21908 * 21909 * Arguments: dev - the device number 21910 * arg - user provided in_resv structure 21911 * flag - this argument is a pass through to ddi_copyxxx() 21912 * directly from the mode argument of ioctl(). 21913 * 21914 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 21915 * ENXIO 21916 * EFAULT 21917 */ 21918 21919 static int 21920 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 21921 { 21922 struct sd_lun *un; 21923 mhioc_inresvs_t inresvs; 21924 int rval = 0; 21925 21926 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21927 return (ENXIO); 21928 } 21929 21930 #ifdef _MULTI_DATAMODEL 21931 21932 switch (ddi_model_convert_from(flag & FMODELS)) { 21933 case DDI_MODEL_ILP32: { 21934 struct mhioc_inresvs32 inresvs32; 21935 21936 if (ddi_copyin(arg, &inresvs32, 21937 sizeof (struct mhioc_inresvs32), flag) != 0) { 21938 return (EFAULT); 21939 } 21940 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 21941 if ((rval = sd_persistent_reservation_in_read_resv(un, 21942 &inresvs, flag)) != 0) { 21943 return (rval); 21944 } 21945 inresvs32.generation = inresvs.generation; 21946 if (ddi_copyout(&inresvs32, arg, 21947 sizeof (struct mhioc_inresvs32), flag) != 0) { 21948 return (EFAULT); 21949 } 21950 break; 21951 } 21952 case DDI_MODEL_NONE: 21953 if (ddi_copyin(arg, &inresvs, 21954 sizeof (mhioc_inresvs_t), flag) != 0) { 21955 return (EFAULT); 21956 } 21957 if ((rval = sd_persistent_reservation_in_read_resv(un, 21958 &inresvs, flag)) != 0) { 21959 return (rval); 21960 } 21961 if (ddi_copyout(&inresvs, arg, 21962 sizeof (mhioc_inresvs_t), flag) != 0) { 21963 return (EFAULT); 21964 } 21965 break; 21966 } 21967 21968 #else /* ! _MULTI_DATAMODEL */ 21969 21970 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 21971 return (EFAULT); 21972 } 21973 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 21974 if (rval != 0) { 21975 return (rval); 21976 } 21977 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 21978 return (EFAULT); 21979 } 21980 21981 #endif /* ! _MULTI_DATAMODEL */ 21982 21983 return (rval); 21984 } 21985 21986 21987 /* 21988 * The following routines support the clustering functionality described below 21989 * and implement lost reservation reclaim functionality. 21990 * 21991 * Clustering 21992 * ---------- 21993 * The clustering code uses two different, independent forms of SCSI 21994 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 21995 * Persistent Group Reservations. For any particular disk, it will use either 21996 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 21997 * 21998 * SCSI-2 21999 * The cluster software takes ownership of a multi-hosted disk by issuing the 22000 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 22001 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 22002 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 22003 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 22004 * driver. The meaning of failfast is that if the driver (on this host) ever 22005 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 22006 * it should immediately panic the host. The motivation for this ioctl is that 22007 * if this host does encounter reservation conflict, the underlying cause is 22008 * that some other host of the cluster has decided that this host is no longer 22009 * in the cluster and has seized control of the disks for itself. Since this 22010 * host is no longer in the cluster, it ought to panic itself. The 22011 * MHIOCENFAILFAST ioctl does two things: 22012 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 22013 * error to panic the host 22014 * (b) it sets up a periodic timer to test whether this host still has 22015 * "access" (in that no other host has reserved the device): if the 22016 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 22017 * purpose of that periodic timer is to handle scenarios where the host is 22018 * otherwise temporarily quiescent, temporarily doing no real i/o. 22019 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 22020 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 22021 * the device itself. 22022 * 22023 * SCSI-3 PGR 22024 * A direct semantic implementation of the SCSI-3 Persistent Reservation 22025 * facility is supported through the shared multihost disk ioctls 22026 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 22027 * MHIOCGRP_PREEMPTANDABORT) 22028 * 22029 * Reservation Reclaim: 22030 * -------------------- 22031 * To support the lost reservation reclaim operations this driver creates a 22032 * single thread to handle reinstating reservations on all devices that have 22033 * lost reservations sd_resv_reclaim_requests are logged for all devices that 22034 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 22035 * and the reservation reclaim thread loops through the requests to regain the 22036 * lost reservations. 22037 */ 22038 22039 /* 22040 * Function: sd_check_mhd() 22041 * 22042 * Description: This function sets up and submits a scsi watch request or 22043 * terminates an existing watch request. This routine is used in 22044 * support of reservation reclaim. 22045 * 22046 * Arguments: dev - the device 'dev_t' is used for context to discriminate 22047 * among multiple watches that share the callback function 22048 * interval - the number of microseconds specifying the watch 22049 * interval for issuing TEST UNIT READY commands. If 22050 * set to 0 the watch should be terminated. If the 22051 * interval is set to 0 and if the device is required 22052 * to hold reservation while disabling failfast, the 22053 * watch is restarted with an interval of 22054 * reinstate_resv_delay. 22055 * 22056 * Return Code: 0 - Successful submit/terminate of scsi watch request 22057 * ENXIO - Indicates an invalid device was specified 22058 * EAGAIN - Unable to submit the scsi watch request 22059 */ 22060 22061 static int 22062 sd_check_mhd(dev_t dev, int interval) 22063 { 22064 struct sd_lun *un; 22065 opaque_t token; 22066 22067 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22068 return (ENXIO); 22069 } 22070 22071 /* is this a watch termination request? */ 22072 if (interval == 0) { 22073 mutex_enter(SD_MUTEX(un)); 22074 /* if there is an existing watch task then terminate it */ 22075 if (un->un_mhd_token) { 22076 token = un->un_mhd_token; 22077 un->un_mhd_token = NULL; 22078 mutex_exit(SD_MUTEX(un)); 22079 (void) scsi_watch_request_terminate(token, 22080 SCSI_WATCH_TERMINATE_ALL_WAIT); 22081 mutex_enter(SD_MUTEX(un)); 22082 } else { 22083 mutex_exit(SD_MUTEX(un)); 22084 /* 22085 * Note: If we return here we don't check for the 22086 * failfast case. This is the original legacy 22087 * implementation but perhaps we should be checking 22088 * the failfast case. 22089 */ 22090 return (0); 22091 } 22092 /* 22093 * If the device is required to hold reservation while 22094 * disabling failfast, we need to restart the scsi_watch 22095 * routine with an interval of reinstate_resv_delay. 22096 */ 22097 if (un->un_resvd_status & SD_RESERVE) { 22098 interval = sd_reinstate_resv_delay/1000; 22099 } else { 22100 /* no failfast so bail */ 22101 mutex_exit(SD_MUTEX(un)); 22102 return (0); 22103 } 22104 mutex_exit(SD_MUTEX(un)); 22105 } 22106 22107 /* 22108 * adjust minimum time interval to 1 second, 22109 * and convert from msecs to usecs 22110 */ 22111 if (interval > 0 && interval < 1000) { 22112 interval = 1000; 22113 } 22114 interval *= 1000; 22115 22116 /* 22117 * submit the request to the scsi_watch service 22118 */ 22119 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 22120 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 22121 if (token == NULL) { 22122 return (EAGAIN); 22123 } 22124 22125 /* 22126 * save token for termination later on 22127 */ 22128 mutex_enter(SD_MUTEX(un)); 22129 un->un_mhd_token = token; 22130 mutex_exit(SD_MUTEX(un)); 22131 return (0); 22132 } 22133 22134 22135 /* 22136 * Function: sd_mhd_watch_cb() 22137 * 22138 * Description: This function is the call back function used by the scsi watch 22139 * facility. The scsi watch facility sends the "Test Unit Ready" 22140 * and processes the status. If applicable (i.e. a "Unit Attention" 22141 * status and automatic "Request Sense" not used) the scsi watch 22142 * facility will send a "Request Sense" and retrieve the sense data 22143 * to be passed to this callback function. In either case the 22144 * automatic "Request Sense" or the facility submitting one, this 22145 * callback is passed the status and sense data. 22146 * 22147 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22148 * among multiple watches that share this callback function 22149 * resultp - scsi watch facility result packet containing scsi 22150 * packet, status byte and sense data 22151 * 22152 * Return Code: 0 - continue the watch task 22153 * non-zero - terminate the watch task 22154 */ 22155 22156 static int 22157 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 22158 { 22159 struct sd_lun *un; 22160 struct scsi_status *statusp; 22161 uint8_t *sensep; 22162 struct scsi_pkt *pkt; 22163 uchar_t actual_sense_length; 22164 dev_t dev = (dev_t)arg; 22165 22166 ASSERT(resultp != NULL); 22167 statusp = resultp->statusp; 22168 sensep = (uint8_t *)resultp->sensep; 22169 pkt = resultp->pkt; 22170 actual_sense_length = resultp->actual_sense_length; 22171 22172 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22173 return (ENXIO); 22174 } 22175 22176 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22177 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 22178 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 22179 22180 /* Begin processing of the status and/or sense data */ 22181 if (pkt->pkt_reason != CMD_CMPLT) { 22182 /* Handle the incomplete packet */ 22183 sd_mhd_watch_incomplete(un, pkt); 22184 return (0); 22185 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 22186 if (*((unsigned char *)statusp) 22187 == STATUS_RESERVATION_CONFLICT) { 22188 /* 22189 * Handle a reservation conflict by panicking if 22190 * configured for failfast or by logging the conflict 22191 * and updating the reservation status 22192 */ 22193 mutex_enter(SD_MUTEX(un)); 22194 if ((un->un_resvd_status & SD_FAILFAST) && 22195 (sd_failfast_enable)) { 22196 sd_panic_for_res_conflict(un); 22197 /*NOTREACHED*/ 22198 } 22199 SD_INFO(SD_LOG_IOCTL_MHD, un, 22200 "sd_mhd_watch_cb: Reservation Conflict\n"); 22201 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 22202 mutex_exit(SD_MUTEX(un)); 22203 } 22204 } 22205 22206 if (sensep != NULL) { 22207 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 22208 mutex_enter(SD_MUTEX(un)); 22209 if ((scsi_sense_asc(sensep) == 22210 SD_SCSI_RESET_SENSE_CODE) && 22211 (un->un_resvd_status & SD_RESERVE)) { 22212 /* 22213 * The additional sense code indicates a power 22214 * on or bus device reset has occurred; update 22215 * the reservation status. 22216 */ 22217 un->un_resvd_status |= 22218 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22219 SD_INFO(SD_LOG_IOCTL_MHD, un, 22220 "sd_mhd_watch_cb: Lost Reservation\n"); 22221 } 22222 } else { 22223 return (0); 22224 } 22225 } else { 22226 mutex_enter(SD_MUTEX(un)); 22227 } 22228 22229 if ((un->un_resvd_status & SD_RESERVE) && 22230 (un->un_resvd_status & SD_LOST_RESERVE)) { 22231 if (un->un_resvd_status & SD_WANT_RESERVE) { 22232 /* 22233 * A reset occurred in between the last probe and this 22234 * one so if a timeout is pending cancel it. 22235 */ 22236 if (un->un_resvd_timeid) { 22237 timeout_id_t temp_id = un->un_resvd_timeid; 22238 un->un_resvd_timeid = NULL; 22239 mutex_exit(SD_MUTEX(un)); 22240 (void) untimeout(temp_id); 22241 mutex_enter(SD_MUTEX(un)); 22242 } 22243 un->un_resvd_status &= ~SD_WANT_RESERVE; 22244 } 22245 if (un->un_resvd_timeid == 0) { 22246 /* Schedule a timeout to handle the lost reservation */ 22247 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 22248 (void *)dev, 22249 drv_usectohz(sd_reinstate_resv_delay)); 22250 } 22251 } 22252 mutex_exit(SD_MUTEX(un)); 22253 return (0); 22254 } 22255 22256 22257 /* 22258 * Function: sd_mhd_watch_incomplete() 22259 * 22260 * Description: This function is used to find out why a scsi pkt sent by the 22261 * scsi watch facility was not completed. Under some scenarios this 22262 * routine will return. Otherwise it will send a bus reset to see 22263 * if the drive is still online. 22264 * 22265 * Arguments: un - driver soft state (unit) structure 22266 * pkt - incomplete scsi pkt 22267 */ 22268 22269 static void 22270 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 22271 { 22272 int be_chatty; 22273 int perr; 22274 22275 ASSERT(pkt != NULL); 22276 ASSERT(un != NULL); 22277 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 22278 perr = (pkt->pkt_statistics & STAT_PERR); 22279 22280 mutex_enter(SD_MUTEX(un)); 22281 if (un->un_state == SD_STATE_DUMPING) { 22282 mutex_exit(SD_MUTEX(un)); 22283 return; 22284 } 22285 22286 switch (pkt->pkt_reason) { 22287 case CMD_UNX_BUS_FREE: 22288 /* 22289 * If we had a parity error that caused the target to drop BSY*, 22290 * don't be chatty about it. 22291 */ 22292 if (perr && be_chatty) { 22293 be_chatty = 0; 22294 } 22295 break; 22296 case CMD_TAG_REJECT: 22297 /* 22298 * The SCSI-2 spec states that a tag reject will be sent by the 22299 * target if tagged queuing is not supported. A tag reject may 22300 * also be sent during certain initialization periods or to 22301 * control internal resources. For the latter case the target 22302 * may also return Queue Full. 22303 * 22304 * If this driver receives a tag reject from a target that is 22305 * going through an init period or controlling internal 22306 * resources tagged queuing will be disabled. This is a less 22307 * than optimal behavior but the driver is unable to determine 22308 * the target state and assumes tagged queueing is not supported 22309 */ 22310 pkt->pkt_flags = 0; 22311 un->un_tagflags = 0; 22312 22313 if (un->un_f_opt_queueing == TRUE) { 22314 un->un_throttle = min(un->un_throttle, 3); 22315 } else { 22316 un->un_throttle = 1; 22317 } 22318 mutex_exit(SD_MUTEX(un)); 22319 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 22320 mutex_enter(SD_MUTEX(un)); 22321 break; 22322 case CMD_INCOMPLETE: 22323 /* 22324 * The transport stopped with an abnormal state, fallthrough and 22325 * reset the target and/or bus unless selection did not complete 22326 * (indicated by STATE_GOT_BUS) in which case we don't want to 22327 * go through a target/bus reset 22328 */ 22329 if (pkt->pkt_state == STATE_GOT_BUS) { 22330 break; 22331 } 22332 /*FALLTHROUGH*/ 22333 22334 case CMD_TIMEOUT: 22335 default: 22336 /* 22337 * The lun may still be running the command, so a lun reset 22338 * should be attempted. If the lun reset fails or cannot be 22339 * issued, than try a target reset. Lastly try a bus reset. 22340 */ 22341 if ((pkt->pkt_statistics & 22342 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 22343 int reset_retval = 0; 22344 mutex_exit(SD_MUTEX(un)); 22345 if (un->un_f_allow_bus_device_reset == TRUE) { 22346 if (un->un_f_lun_reset_enabled == TRUE) { 22347 reset_retval = 22348 scsi_reset(SD_ADDRESS(un), 22349 RESET_LUN); 22350 } 22351 if (reset_retval == 0) { 22352 reset_retval = 22353 scsi_reset(SD_ADDRESS(un), 22354 RESET_TARGET); 22355 } 22356 } 22357 if (reset_retval == 0) { 22358 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 22359 } 22360 mutex_enter(SD_MUTEX(un)); 22361 } 22362 break; 22363 } 22364 22365 /* A device/bus reset has occurred; update the reservation status. */ 22366 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 22367 (STAT_BUS_RESET | STAT_DEV_RESET))) { 22368 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22369 un->un_resvd_status |= 22370 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22371 SD_INFO(SD_LOG_IOCTL_MHD, un, 22372 "sd_mhd_watch_incomplete: Lost Reservation\n"); 22373 } 22374 } 22375 22376 /* 22377 * The disk has been turned off; Update the device state. 22378 * 22379 * Note: Should we be offlining the disk here? 22380 */ 22381 if (pkt->pkt_state == STATE_GOT_BUS) { 22382 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 22383 "Disk not responding to selection\n"); 22384 if (un->un_state != SD_STATE_OFFLINE) { 22385 New_state(un, SD_STATE_OFFLINE); 22386 } 22387 } else if (be_chatty) { 22388 /* 22389 * suppress messages if they are all the same pkt reason; 22390 * with TQ, many (up to 256) are returned with the same 22391 * pkt_reason 22392 */ 22393 if (pkt->pkt_reason != un->un_last_pkt_reason) { 22394 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22395 "sd_mhd_watch_incomplete: " 22396 "SCSI transport failed: reason '%s'\n", 22397 scsi_rname(pkt->pkt_reason)); 22398 } 22399 } 22400 un->un_last_pkt_reason = pkt->pkt_reason; 22401 mutex_exit(SD_MUTEX(un)); 22402 } 22403 22404 22405 /* 22406 * Function: sd_sname() 22407 * 22408 * Description: This is a simple little routine to return a string containing 22409 * a printable description of command status byte for use in 22410 * logging. 22411 * 22412 * Arguments: status - pointer to a status byte 22413 * 22414 * Return Code: char * - string containing status description. 22415 */ 22416 22417 static char * 22418 sd_sname(uchar_t status) 22419 { 22420 switch (status & STATUS_MASK) { 22421 case STATUS_GOOD: 22422 return ("good status"); 22423 case STATUS_CHECK: 22424 return ("check condition"); 22425 case STATUS_MET: 22426 return ("condition met"); 22427 case STATUS_BUSY: 22428 return ("busy"); 22429 case STATUS_INTERMEDIATE: 22430 return ("intermediate"); 22431 case STATUS_INTERMEDIATE_MET: 22432 return ("intermediate - condition met"); 22433 case STATUS_RESERVATION_CONFLICT: 22434 return ("reservation_conflict"); 22435 case STATUS_TERMINATED: 22436 return ("command terminated"); 22437 case STATUS_QFULL: 22438 return ("queue full"); 22439 default: 22440 return ("<unknown status>"); 22441 } 22442 } 22443 22444 22445 /* 22446 * Function: sd_mhd_resvd_recover() 22447 * 22448 * Description: This function adds a reservation entry to the 22449 * sd_resv_reclaim_request list and signals the reservation 22450 * reclaim thread that there is work pending. If the reservation 22451 * reclaim thread has not been previously created this function 22452 * will kick it off. 22453 * 22454 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22455 * among multiple watches that share this callback function 22456 * 22457 * Context: This routine is called by timeout() and is run in interrupt 22458 * context. It must not sleep or call other functions which may 22459 * sleep. 22460 */ 22461 22462 static void 22463 sd_mhd_resvd_recover(void *arg) 22464 { 22465 dev_t dev = (dev_t)arg; 22466 struct sd_lun *un; 22467 struct sd_thr_request *sd_treq = NULL; 22468 struct sd_thr_request *sd_cur = NULL; 22469 struct sd_thr_request *sd_prev = NULL; 22470 int already_there = 0; 22471 22472 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22473 return; 22474 } 22475 22476 mutex_enter(SD_MUTEX(un)); 22477 un->un_resvd_timeid = NULL; 22478 if (un->un_resvd_status & SD_WANT_RESERVE) { 22479 /* 22480 * There was a reset so don't issue the reserve, allow the 22481 * sd_mhd_watch_cb callback function to notice this and 22482 * reschedule the timeout for reservation. 22483 */ 22484 mutex_exit(SD_MUTEX(un)); 22485 return; 22486 } 22487 mutex_exit(SD_MUTEX(un)); 22488 22489 /* 22490 * Add this device to the sd_resv_reclaim_request list and the 22491 * sd_resv_reclaim_thread should take care of the rest. 22492 * 22493 * Note: We can't sleep in this context so if the memory allocation 22494 * fails allow the sd_mhd_watch_cb callback function to notice this and 22495 * reschedule the timeout for reservation. (4378460) 22496 */ 22497 sd_treq = (struct sd_thr_request *) 22498 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 22499 if (sd_treq == NULL) { 22500 return; 22501 } 22502 22503 sd_treq->sd_thr_req_next = NULL; 22504 sd_treq->dev = dev; 22505 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22506 if (sd_tr.srq_thr_req_head == NULL) { 22507 sd_tr.srq_thr_req_head = sd_treq; 22508 } else { 22509 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 22510 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 22511 if (sd_cur->dev == dev) { 22512 /* 22513 * already in Queue so don't log 22514 * another request for the device 22515 */ 22516 already_there = 1; 22517 break; 22518 } 22519 sd_prev = sd_cur; 22520 } 22521 if (!already_there) { 22522 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 22523 "logging request for %lx\n", dev); 22524 sd_prev->sd_thr_req_next = sd_treq; 22525 } else { 22526 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 22527 } 22528 } 22529 22530 /* 22531 * Create a kernel thread to do the reservation reclaim and free up this 22532 * thread. We cannot block this thread while we go away to do the 22533 * reservation reclaim 22534 */ 22535 if (sd_tr.srq_resv_reclaim_thread == NULL) 22536 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 22537 sd_resv_reclaim_thread, NULL, 22538 0, &p0, TS_RUN, v.v_maxsyspri - 2); 22539 22540 /* Tell the reservation reclaim thread that it has work to do */ 22541 cv_signal(&sd_tr.srq_resv_reclaim_cv); 22542 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22543 } 22544 22545 /* 22546 * Function: sd_resv_reclaim_thread() 22547 * 22548 * Description: This function implements the reservation reclaim operations 22549 * 22550 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22551 * among multiple watches that share this callback function 22552 */ 22553 22554 static void 22555 sd_resv_reclaim_thread() 22556 { 22557 struct sd_lun *un; 22558 struct sd_thr_request *sd_mhreq; 22559 22560 /* Wait for work */ 22561 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22562 if (sd_tr.srq_thr_req_head == NULL) { 22563 cv_wait(&sd_tr.srq_resv_reclaim_cv, 22564 &sd_tr.srq_resv_reclaim_mutex); 22565 } 22566 22567 /* Loop while we have work */ 22568 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 22569 un = ddi_get_soft_state(sd_state, 22570 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 22571 if (un == NULL) { 22572 /* 22573 * softstate structure is NULL so just 22574 * dequeue the request and continue 22575 */ 22576 sd_tr.srq_thr_req_head = 22577 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22578 kmem_free(sd_tr.srq_thr_cur_req, 22579 sizeof (struct sd_thr_request)); 22580 continue; 22581 } 22582 22583 /* dequeue the request */ 22584 sd_mhreq = sd_tr.srq_thr_cur_req; 22585 sd_tr.srq_thr_req_head = 22586 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22587 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22588 22589 /* 22590 * Reclaim reservation only if SD_RESERVE is still set. There 22591 * may have been a call to MHIOCRELEASE before we got here. 22592 */ 22593 mutex_enter(SD_MUTEX(un)); 22594 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22595 /* 22596 * Note: The SD_LOST_RESERVE flag is cleared before 22597 * reclaiming the reservation. If this is done after the 22598 * call to sd_reserve_release a reservation loss in the 22599 * window between pkt completion of reserve cmd and 22600 * mutex_enter below may not be recognized 22601 */ 22602 un->un_resvd_status &= ~SD_LOST_RESERVE; 22603 mutex_exit(SD_MUTEX(un)); 22604 22605 if (sd_reserve_release(sd_mhreq->dev, 22606 SD_RESERVE) == 0) { 22607 mutex_enter(SD_MUTEX(un)); 22608 un->un_resvd_status |= SD_RESERVE; 22609 mutex_exit(SD_MUTEX(un)); 22610 SD_INFO(SD_LOG_IOCTL_MHD, un, 22611 "sd_resv_reclaim_thread: " 22612 "Reservation Recovered\n"); 22613 } else { 22614 mutex_enter(SD_MUTEX(un)); 22615 un->un_resvd_status |= SD_LOST_RESERVE; 22616 mutex_exit(SD_MUTEX(un)); 22617 SD_INFO(SD_LOG_IOCTL_MHD, un, 22618 "sd_resv_reclaim_thread: Failed " 22619 "Reservation Recovery\n"); 22620 } 22621 } else { 22622 mutex_exit(SD_MUTEX(un)); 22623 } 22624 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22625 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 22626 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22627 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 22628 /* 22629 * wakeup the destroy thread if anyone is waiting on 22630 * us to complete. 22631 */ 22632 cv_signal(&sd_tr.srq_inprocess_cv); 22633 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22634 "sd_resv_reclaim_thread: cv_signalling current request \n"); 22635 } 22636 22637 /* 22638 * cleanup the sd_tr structure now that this thread will not exist 22639 */ 22640 ASSERT(sd_tr.srq_thr_req_head == NULL); 22641 ASSERT(sd_tr.srq_thr_cur_req == NULL); 22642 sd_tr.srq_resv_reclaim_thread = NULL; 22643 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22644 thread_exit(); 22645 } 22646 22647 22648 /* 22649 * Function: sd_rmv_resv_reclaim_req() 22650 * 22651 * Description: This function removes any pending reservation reclaim requests 22652 * for the specified device. 22653 * 22654 * Arguments: dev - the device 'dev_t' 22655 */ 22656 22657 static void 22658 sd_rmv_resv_reclaim_req(dev_t dev) 22659 { 22660 struct sd_thr_request *sd_mhreq; 22661 struct sd_thr_request *sd_prev; 22662 22663 /* Remove a reservation reclaim request from the list */ 22664 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22665 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 22666 /* 22667 * We are attempting to reinstate reservation for 22668 * this device. We wait for sd_reserve_release() 22669 * to return before we return. 22670 */ 22671 cv_wait(&sd_tr.srq_inprocess_cv, 22672 &sd_tr.srq_resv_reclaim_mutex); 22673 } else { 22674 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 22675 if (sd_mhreq && sd_mhreq->dev == dev) { 22676 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 22677 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22678 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22679 return; 22680 } 22681 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 22682 if (sd_mhreq && sd_mhreq->dev == dev) { 22683 break; 22684 } 22685 sd_prev = sd_mhreq; 22686 } 22687 if (sd_mhreq != NULL) { 22688 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 22689 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22690 } 22691 } 22692 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22693 } 22694 22695 22696 /* 22697 * Function: sd_mhd_reset_notify_cb() 22698 * 22699 * Description: This is a call back function for scsi_reset_notify. This 22700 * function updates the softstate reserved status and logs the 22701 * reset. The driver scsi watch facility callback function 22702 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 22703 * will reclaim the reservation. 22704 * 22705 * Arguments: arg - driver soft state (unit) structure 22706 */ 22707 22708 static void 22709 sd_mhd_reset_notify_cb(caddr_t arg) 22710 { 22711 struct sd_lun *un = (struct sd_lun *)arg; 22712 22713 mutex_enter(SD_MUTEX(un)); 22714 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22715 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 22716 SD_INFO(SD_LOG_IOCTL_MHD, un, 22717 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 22718 } 22719 mutex_exit(SD_MUTEX(un)); 22720 } 22721 22722 22723 /* 22724 * Function: sd_take_ownership() 22725 * 22726 * Description: This routine implements an algorithm to achieve a stable 22727 * reservation on disks which don't implement priority reserve, 22728 * and makes sure that other host lose re-reservation attempts. 22729 * This algorithm contains of a loop that keeps issuing the RESERVE 22730 * for some period of time (min_ownership_delay, default 6 seconds) 22731 * During that loop, it looks to see if there has been a bus device 22732 * reset or bus reset (both of which cause an existing reservation 22733 * to be lost). If the reservation is lost issue RESERVE until a 22734 * period of min_ownership_delay with no resets has gone by, or 22735 * until max_ownership_delay has expired. This loop ensures that 22736 * the host really did manage to reserve the device, in spite of 22737 * resets. The looping for min_ownership_delay (default six 22738 * seconds) is important to early generation clustering products, 22739 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 22740 * MHIOCENFAILFAST periodic timer of two seconds. By having 22741 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 22742 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 22743 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 22744 * have already noticed, via the MHIOCENFAILFAST polling, that it 22745 * no longer "owns" the disk and will have panicked itself. Thus, 22746 * the host issuing the MHIOCTKOWN is assured (with timing 22747 * dependencies) that by the time it actually starts to use the 22748 * disk for real work, the old owner is no longer accessing it. 22749 * 22750 * min_ownership_delay is the minimum amount of time for which the 22751 * disk must be reserved continuously devoid of resets before the 22752 * MHIOCTKOWN ioctl will return success. 22753 * 22754 * max_ownership_delay indicates the amount of time by which the 22755 * take ownership should succeed or timeout with an error. 22756 * 22757 * Arguments: dev - the device 'dev_t' 22758 * *p - struct containing timing info. 22759 * 22760 * Return Code: 0 for success or error code 22761 */ 22762 22763 static int 22764 sd_take_ownership(dev_t dev, struct mhioctkown *p) 22765 { 22766 struct sd_lun *un; 22767 int rval; 22768 int err; 22769 int reservation_count = 0; 22770 int min_ownership_delay = 6000000; /* in usec */ 22771 int max_ownership_delay = 30000000; /* in usec */ 22772 clock_t start_time; /* starting time of this algorithm */ 22773 clock_t end_time; /* time limit for giving up */ 22774 clock_t ownership_time; /* time limit for stable ownership */ 22775 clock_t current_time; 22776 clock_t previous_current_time; 22777 22778 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22779 return (ENXIO); 22780 } 22781 22782 /* 22783 * Attempt a device reservation. A priority reservation is requested. 22784 */ 22785 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 22786 != SD_SUCCESS) { 22787 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22788 "sd_take_ownership: return(1)=%d\n", rval); 22789 return (rval); 22790 } 22791 22792 /* Update the softstate reserved status to indicate the reservation */ 22793 mutex_enter(SD_MUTEX(un)); 22794 un->un_resvd_status |= SD_RESERVE; 22795 un->un_resvd_status &= 22796 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 22797 mutex_exit(SD_MUTEX(un)); 22798 22799 if (p != NULL) { 22800 if (p->min_ownership_delay != 0) { 22801 min_ownership_delay = p->min_ownership_delay * 1000; 22802 } 22803 if (p->max_ownership_delay != 0) { 22804 max_ownership_delay = p->max_ownership_delay * 1000; 22805 } 22806 } 22807 SD_INFO(SD_LOG_IOCTL_MHD, un, 22808 "sd_take_ownership: min, max delays: %d, %d\n", 22809 min_ownership_delay, max_ownership_delay); 22810 22811 start_time = ddi_get_lbolt(); 22812 current_time = start_time; 22813 ownership_time = current_time + drv_usectohz(min_ownership_delay); 22814 end_time = start_time + drv_usectohz(max_ownership_delay); 22815 22816 while (current_time - end_time < 0) { 22817 delay(drv_usectohz(500000)); 22818 22819 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 22820 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 22821 mutex_enter(SD_MUTEX(un)); 22822 rval = (un->un_resvd_status & 22823 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 22824 mutex_exit(SD_MUTEX(un)); 22825 break; 22826 } 22827 } 22828 previous_current_time = current_time; 22829 current_time = ddi_get_lbolt(); 22830 mutex_enter(SD_MUTEX(un)); 22831 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 22832 ownership_time = ddi_get_lbolt() + 22833 drv_usectohz(min_ownership_delay); 22834 reservation_count = 0; 22835 } else { 22836 reservation_count++; 22837 } 22838 un->un_resvd_status |= SD_RESERVE; 22839 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 22840 mutex_exit(SD_MUTEX(un)); 22841 22842 SD_INFO(SD_LOG_IOCTL_MHD, un, 22843 "sd_take_ownership: ticks for loop iteration=%ld, " 22844 "reservation=%s\n", (current_time - previous_current_time), 22845 reservation_count ? "ok" : "reclaimed"); 22846 22847 if (current_time - ownership_time >= 0 && 22848 reservation_count >= 4) { 22849 rval = 0; /* Achieved a stable ownership */ 22850 break; 22851 } 22852 if (current_time - end_time >= 0) { 22853 rval = EACCES; /* No ownership in max possible time */ 22854 break; 22855 } 22856 } 22857 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22858 "sd_take_ownership: return(2)=%d\n", rval); 22859 return (rval); 22860 } 22861 22862 22863 /* 22864 * Function: sd_reserve_release() 22865 * 22866 * Description: This function builds and sends scsi RESERVE, RELEASE, and 22867 * PRIORITY RESERVE commands based on a user specified command type 22868 * 22869 * Arguments: dev - the device 'dev_t' 22870 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 22871 * SD_RESERVE, SD_RELEASE 22872 * 22873 * Return Code: 0 or Error Code 22874 */ 22875 22876 static int 22877 sd_reserve_release(dev_t dev, int cmd) 22878 { 22879 struct uscsi_cmd *com = NULL; 22880 struct sd_lun *un = NULL; 22881 char cdb[CDB_GROUP0]; 22882 int rval; 22883 22884 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 22885 (cmd == SD_PRIORITY_RESERVE)); 22886 22887 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22888 return (ENXIO); 22889 } 22890 22891 /* instantiate and initialize the command and cdb */ 22892 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 22893 bzero(cdb, CDB_GROUP0); 22894 com->uscsi_flags = USCSI_SILENT; 22895 com->uscsi_timeout = un->un_reserve_release_time; 22896 com->uscsi_cdblen = CDB_GROUP0; 22897 com->uscsi_cdb = cdb; 22898 if (cmd == SD_RELEASE) { 22899 cdb[0] = SCMD_RELEASE; 22900 } else { 22901 cdb[0] = SCMD_RESERVE; 22902 } 22903 22904 /* Send the command. */ 22905 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22906 SD_PATH_STANDARD); 22907 22908 /* 22909 * "break" a reservation that is held by another host, by issuing a 22910 * reset if priority reserve is desired, and we could not get the 22911 * device. 22912 */ 22913 if ((cmd == SD_PRIORITY_RESERVE) && 22914 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22915 /* 22916 * First try to reset the LUN. If we cannot, then try a target 22917 * reset, followed by a bus reset if the target reset fails. 22918 */ 22919 int reset_retval = 0; 22920 if (un->un_f_lun_reset_enabled == TRUE) { 22921 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 22922 } 22923 if (reset_retval == 0) { 22924 /* The LUN reset either failed or was not issued */ 22925 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 22926 } 22927 if ((reset_retval == 0) && 22928 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 22929 rval = EIO; 22930 kmem_free(com, sizeof (*com)); 22931 return (rval); 22932 } 22933 22934 bzero(com, sizeof (struct uscsi_cmd)); 22935 com->uscsi_flags = USCSI_SILENT; 22936 com->uscsi_cdb = cdb; 22937 com->uscsi_cdblen = CDB_GROUP0; 22938 com->uscsi_timeout = 5; 22939 22940 /* 22941 * Reissue the last reserve command, this time without request 22942 * sense. Assume that it is just a regular reserve command. 22943 */ 22944 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22945 SD_PATH_STANDARD); 22946 } 22947 22948 /* Return an error if still getting a reservation conflict. */ 22949 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22950 rval = EACCES; 22951 } 22952 22953 kmem_free(com, sizeof (*com)); 22954 return (rval); 22955 } 22956 22957 22958 #define SD_NDUMP_RETRIES 12 22959 /* 22960 * System Crash Dump routine 22961 */ 22962 22963 static int 22964 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 22965 { 22966 int instance; 22967 int partition; 22968 int i; 22969 int err; 22970 struct sd_lun *un; 22971 struct scsi_pkt *wr_pktp; 22972 struct buf *wr_bp; 22973 struct buf wr_buf; 22974 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 22975 daddr_t tgt_blkno; /* rmw - blkno for target */ 22976 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 22977 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 22978 size_t io_start_offset; 22979 int doing_rmw = FALSE; 22980 int rval; 22981 ssize_t dma_resid; 22982 daddr_t oblkno; 22983 diskaddr_t nblks = 0; 22984 diskaddr_t start_block; 22985 22986 instance = SDUNIT(dev); 22987 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 22988 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 22989 return (ENXIO); 22990 } 22991 22992 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 22993 22994 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 22995 22996 partition = SDPART(dev); 22997 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 22998 22999 /* Validate blocks to dump at against partition size. */ 23000 23001 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 23002 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 23003 23004 if ((blkno + nblk) > nblks) { 23005 SD_TRACE(SD_LOG_DUMP, un, 23006 "sddump: dump range larger than partition: " 23007 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 23008 blkno, nblk, nblks); 23009 return (EINVAL); 23010 } 23011 23012 mutex_enter(&un->un_pm_mutex); 23013 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 23014 struct scsi_pkt *start_pktp; 23015 23016 mutex_exit(&un->un_pm_mutex); 23017 23018 /* 23019 * use pm framework to power on HBA 1st 23020 */ 23021 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 23022 23023 /* 23024 * Dump no long uses sdpower to power on a device, it's 23025 * in-line here so it can be done in polled mode. 23026 */ 23027 23028 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 23029 23030 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 23031 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 23032 23033 if (start_pktp == NULL) { 23034 /* We were not given a SCSI packet, fail. */ 23035 return (EIO); 23036 } 23037 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 23038 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 23039 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 23040 start_pktp->pkt_flags = FLAG_NOINTR; 23041 23042 mutex_enter(SD_MUTEX(un)); 23043 SD_FILL_SCSI1_LUN(un, start_pktp); 23044 mutex_exit(SD_MUTEX(un)); 23045 /* 23046 * Scsi_poll returns 0 (success) if the command completes and 23047 * the status block is STATUS_GOOD. 23048 */ 23049 if (sd_scsi_poll(un, start_pktp) != 0) { 23050 scsi_destroy_pkt(start_pktp); 23051 return (EIO); 23052 } 23053 scsi_destroy_pkt(start_pktp); 23054 (void) sd_ddi_pm_resume(un); 23055 } else { 23056 mutex_exit(&un->un_pm_mutex); 23057 } 23058 23059 mutex_enter(SD_MUTEX(un)); 23060 un->un_throttle = 0; 23061 23062 /* 23063 * The first time through, reset the specific target device. 23064 * However, when cpr calls sddump we know that sd is in a 23065 * a good state so no bus reset is required. 23066 * Clear sense data via Request Sense cmd. 23067 * In sddump we don't care about allow_bus_device_reset anymore 23068 */ 23069 23070 if ((un->un_state != SD_STATE_SUSPENDED) && 23071 (un->un_state != SD_STATE_DUMPING)) { 23072 23073 New_state(un, SD_STATE_DUMPING); 23074 23075 if (un->un_f_is_fibre == FALSE) { 23076 mutex_exit(SD_MUTEX(un)); 23077 /* 23078 * Attempt a bus reset for parallel scsi. 23079 * 23080 * Note: A bus reset is required because on some host 23081 * systems (i.e. E420R) a bus device reset is 23082 * insufficient to reset the state of the target. 23083 * 23084 * Note: Don't issue the reset for fibre-channel, 23085 * because this tends to hang the bus (loop) for 23086 * too long while everyone is logging out and in 23087 * and the deadman timer for dumping will fire 23088 * before the dump is complete. 23089 */ 23090 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 23091 mutex_enter(SD_MUTEX(un)); 23092 Restore_state(un); 23093 mutex_exit(SD_MUTEX(un)); 23094 return (EIO); 23095 } 23096 23097 /* Delay to give the device some recovery time. */ 23098 drv_usecwait(10000); 23099 23100 if (sd_send_polled_RQS(un) == SD_FAILURE) { 23101 SD_INFO(SD_LOG_DUMP, un, 23102 "sddump: sd_send_polled_RQS failed\n"); 23103 } 23104 mutex_enter(SD_MUTEX(un)); 23105 } 23106 } 23107 23108 /* 23109 * Convert the partition-relative block number to a 23110 * disk physical block number. 23111 */ 23112 blkno += start_block; 23113 23114 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 23115 23116 23117 /* 23118 * Check if the device has a non-512 block size. 23119 */ 23120 wr_bp = NULL; 23121 if (NOT_DEVBSIZE(un)) { 23122 tgt_byte_offset = blkno * un->un_sys_blocksize; 23123 tgt_byte_count = nblk * un->un_sys_blocksize; 23124 if ((tgt_byte_offset % un->un_tgt_blocksize) || 23125 (tgt_byte_count % un->un_tgt_blocksize)) { 23126 doing_rmw = TRUE; 23127 /* 23128 * Calculate the block number and number of block 23129 * in terms of the media block size. 23130 */ 23131 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 23132 tgt_nblk = 23133 ((tgt_byte_offset + tgt_byte_count + 23134 (un->un_tgt_blocksize - 1)) / 23135 un->un_tgt_blocksize) - tgt_blkno; 23136 23137 /* 23138 * Invoke the routine which is going to do read part 23139 * of read-modify-write. 23140 * Note that this routine returns a pointer to 23141 * a valid bp in wr_bp. 23142 */ 23143 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 23144 &wr_bp); 23145 if (err) { 23146 mutex_exit(SD_MUTEX(un)); 23147 return (err); 23148 } 23149 /* 23150 * Offset is being calculated as - 23151 * (original block # * system block size) - 23152 * (new block # * target block size) 23153 */ 23154 io_start_offset = 23155 ((uint64_t)(blkno * un->un_sys_blocksize)) - 23156 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 23157 23158 ASSERT((io_start_offset >= 0) && 23159 (io_start_offset < un->un_tgt_blocksize)); 23160 /* 23161 * Do the modify portion of read modify write. 23162 */ 23163 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 23164 (size_t)nblk * un->un_sys_blocksize); 23165 } else { 23166 doing_rmw = FALSE; 23167 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 23168 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 23169 } 23170 23171 /* Convert blkno and nblk to target blocks */ 23172 blkno = tgt_blkno; 23173 nblk = tgt_nblk; 23174 } else { 23175 wr_bp = &wr_buf; 23176 bzero(wr_bp, sizeof (struct buf)); 23177 wr_bp->b_flags = B_BUSY; 23178 wr_bp->b_un.b_addr = addr; 23179 wr_bp->b_bcount = nblk << DEV_BSHIFT; 23180 wr_bp->b_resid = 0; 23181 } 23182 23183 mutex_exit(SD_MUTEX(un)); 23184 23185 /* 23186 * Obtain a SCSI packet for the write command. 23187 * It should be safe to call the allocator here without 23188 * worrying about being locked for DVMA mapping because 23189 * the address we're passed is already a DVMA mapping 23190 * 23191 * We are also not going to worry about semaphore ownership 23192 * in the dump buffer. Dumping is single threaded at present. 23193 */ 23194 23195 wr_pktp = NULL; 23196 23197 dma_resid = wr_bp->b_bcount; 23198 oblkno = blkno; 23199 23200 while (dma_resid != 0) { 23201 23202 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23203 wr_bp->b_flags &= ~B_ERROR; 23204 23205 if (un->un_partial_dma_supported == 1) { 23206 blkno = oblkno + 23207 ((wr_bp->b_bcount - dma_resid) / 23208 un->un_tgt_blocksize); 23209 nblk = dma_resid / un->un_tgt_blocksize; 23210 23211 if (wr_pktp) { 23212 /* 23213 * Partial DMA transfers after initial transfer 23214 */ 23215 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 23216 blkno, nblk); 23217 } else { 23218 /* Initial transfer */ 23219 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23220 un->un_pkt_flags, NULL_FUNC, NULL, 23221 blkno, nblk); 23222 } 23223 } else { 23224 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23225 0, NULL_FUNC, NULL, blkno, nblk); 23226 } 23227 23228 if (rval == 0) { 23229 /* We were given a SCSI packet, continue. */ 23230 break; 23231 } 23232 23233 if (i == 0) { 23234 if (wr_bp->b_flags & B_ERROR) { 23235 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23236 "no resources for dumping; " 23237 "error code: 0x%x, retrying", 23238 geterror(wr_bp)); 23239 } else { 23240 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23241 "no resources for dumping; retrying"); 23242 } 23243 } else if (i != (SD_NDUMP_RETRIES - 1)) { 23244 if (wr_bp->b_flags & B_ERROR) { 23245 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23246 "no resources for dumping; error code: " 23247 "0x%x, retrying\n", geterror(wr_bp)); 23248 } 23249 } else { 23250 if (wr_bp->b_flags & B_ERROR) { 23251 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23252 "no resources for dumping; " 23253 "error code: 0x%x, retries failed, " 23254 "giving up.\n", geterror(wr_bp)); 23255 } else { 23256 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23257 "no resources for dumping; " 23258 "retries failed, giving up.\n"); 23259 } 23260 mutex_enter(SD_MUTEX(un)); 23261 Restore_state(un); 23262 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 23263 mutex_exit(SD_MUTEX(un)); 23264 scsi_free_consistent_buf(wr_bp); 23265 } else { 23266 mutex_exit(SD_MUTEX(un)); 23267 } 23268 return (EIO); 23269 } 23270 drv_usecwait(10000); 23271 } 23272 23273 if (un->un_partial_dma_supported == 1) { 23274 /* 23275 * save the resid from PARTIAL_DMA 23276 */ 23277 dma_resid = wr_pktp->pkt_resid; 23278 if (dma_resid != 0) 23279 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 23280 wr_pktp->pkt_resid = 0; 23281 } else { 23282 dma_resid = 0; 23283 } 23284 23285 /* SunBug 1222170 */ 23286 wr_pktp->pkt_flags = FLAG_NOINTR; 23287 23288 err = EIO; 23289 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23290 23291 /* 23292 * Scsi_poll returns 0 (success) if the command completes and 23293 * the status block is STATUS_GOOD. We should only check 23294 * errors if this condition is not true. Even then we should 23295 * send our own request sense packet only if we have a check 23296 * condition and auto request sense has not been performed by 23297 * the hba. 23298 */ 23299 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 23300 23301 if ((sd_scsi_poll(un, wr_pktp) == 0) && 23302 (wr_pktp->pkt_resid == 0)) { 23303 err = SD_SUCCESS; 23304 break; 23305 } 23306 23307 /* 23308 * Check CMD_DEV_GONE 1st, give up if device is gone. 23309 */ 23310 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 23311 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23312 "Error while dumping state...Device is gone\n"); 23313 break; 23314 } 23315 23316 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 23317 SD_INFO(SD_LOG_DUMP, un, 23318 "sddump: write failed with CHECK, try # %d\n", i); 23319 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 23320 (void) sd_send_polled_RQS(un); 23321 } 23322 23323 continue; 23324 } 23325 23326 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 23327 int reset_retval = 0; 23328 23329 SD_INFO(SD_LOG_DUMP, un, 23330 "sddump: write failed with BUSY, try # %d\n", i); 23331 23332 if (un->un_f_lun_reset_enabled == TRUE) { 23333 reset_retval = scsi_reset(SD_ADDRESS(un), 23334 RESET_LUN); 23335 } 23336 if (reset_retval == 0) { 23337 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 23338 } 23339 (void) sd_send_polled_RQS(un); 23340 23341 } else { 23342 SD_INFO(SD_LOG_DUMP, un, 23343 "sddump: write failed with 0x%x, try # %d\n", 23344 SD_GET_PKT_STATUS(wr_pktp), i); 23345 mutex_enter(SD_MUTEX(un)); 23346 sd_reset_target(un, wr_pktp); 23347 mutex_exit(SD_MUTEX(un)); 23348 } 23349 23350 /* 23351 * If we are not getting anywhere with lun/target resets, 23352 * let's reset the bus. 23353 */ 23354 if (i == SD_NDUMP_RETRIES/2) { 23355 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 23356 (void) sd_send_polled_RQS(un); 23357 } 23358 } 23359 } 23360 23361 scsi_destroy_pkt(wr_pktp); 23362 mutex_enter(SD_MUTEX(un)); 23363 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 23364 mutex_exit(SD_MUTEX(un)); 23365 scsi_free_consistent_buf(wr_bp); 23366 } else { 23367 mutex_exit(SD_MUTEX(un)); 23368 } 23369 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 23370 return (err); 23371 } 23372 23373 /* 23374 * Function: sd_scsi_poll() 23375 * 23376 * Description: This is a wrapper for the scsi_poll call. 23377 * 23378 * Arguments: sd_lun - The unit structure 23379 * scsi_pkt - The scsi packet being sent to the device. 23380 * 23381 * Return Code: 0 - Command completed successfully with good status 23382 * -1 - Command failed. This could indicate a check condition 23383 * or other status value requiring recovery action. 23384 * 23385 * NOTE: This code is only called off sddump(). 23386 */ 23387 23388 static int 23389 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 23390 { 23391 int status; 23392 23393 ASSERT(un != NULL); 23394 ASSERT(!mutex_owned(SD_MUTEX(un))); 23395 ASSERT(pktp != NULL); 23396 23397 status = SD_SUCCESS; 23398 23399 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 23400 pktp->pkt_flags |= un->un_tagflags; 23401 pktp->pkt_flags &= ~FLAG_NODISCON; 23402 } 23403 23404 status = sd_ddi_scsi_poll(pktp); 23405 /* 23406 * Scsi_poll returns 0 (success) if the command completes and the 23407 * status block is STATUS_GOOD. We should only check errors if this 23408 * condition is not true. Even then we should send our own request 23409 * sense packet only if we have a check condition and auto 23410 * request sense has not been performed by the hba. 23411 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 23412 */ 23413 if ((status != SD_SUCCESS) && 23414 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 23415 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 23416 (pktp->pkt_reason != CMD_DEV_GONE)) 23417 (void) sd_send_polled_RQS(un); 23418 23419 return (status); 23420 } 23421 23422 /* 23423 * Function: sd_send_polled_RQS() 23424 * 23425 * Description: This sends the request sense command to a device. 23426 * 23427 * Arguments: sd_lun - The unit structure 23428 * 23429 * Return Code: 0 - Command completed successfully with good status 23430 * -1 - Command failed. 23431 * 23432 */ 23433 23434 static int 23435 sd_send_polled_RQS(struct sd_lun *un) 23436 { 23437 int ret_val; 23438 struct scsi_pkt *rqs_pktp; 23439 struct buf *rqs_bp; 23440 23441 ASSERT(un != NULL); 23442 ASSERT(!mutex_owned(SD_MUTEX(un))); 23443 23444 ret_val = SD_SUCCESS; 23445 23446 rqs_pktp = un->un_rqs_pktp; 23447 rqs_bp = un->un_rqs_bp; 23448 23449 mutex_enter(SD_MUTEX(un)); 23450 23451 if (un->un_sense_isbusy) { 23452 ret_val = SD_FAILURE; 23453 mutex_exit(SD_MUTEX(un)); 23454 return (ret_val); 23455 } 23456 23457 /* 23458 * If the request sense buffer (and packet) is not in use, 23459 * let's set the un_sense_isbusy and send our packet 23460 */ 23461 un->un_sense_isbusy = 1; 23462 rqs_pktp->pkt_resid = 0; 23463 rqs_pktp->pkt_reason = 0; 23464 rqs_pktp->pkt_flags |= FLAG_NOINTR; 23465 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 23466 23467 mutex_exit(SD_MUTEX(un)); 23468 23469 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 23470 " 0x%p\n", rqs_bp->b_un.b_addr); 23471 23472 /* 23473 * Can't send this to sd_scsi_poll, we wrap ourselves around the 23474 * axle - it has a call into us! 23475 */ 23476 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 23477 SD_INFO(SD_LOG_COMMON, un, 23478 "sd_send_polled_RQS: RQS failed\n"); 23479 } 23480 23481 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 23482 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 23483 23484 mutex_enter(SD_MUTEX(un)); 23485 un->un_sense_isbusy = 0; 23486 mutex_exit(SD_MUTEX(un)); 23487 23488 return (ret_val); 23489 } 23490 23491 /* 23492 * Defines needed for localized version of the scsi_poll routine. 23493 */ 23494 #define CSEC 10000 /* usecs */ 23495 #define SEC_TO_CSEC (1000000/CSEC) 23496 23497 /* 23498 * Function: sd_ddi_scsi_poll() 23499 * 23500 * Description: Localized version of the scsi_poll routine. The purpose is to 23501 * send a scsi_pkt to a device as a polled command. This version 23502 * is to ensure more robust handling of transport errors. 23503 * Specifically this routine cures not ready, coming ready 23504 * transition for power up and reset of sonoma's. This can take 23505 * up to 45 seconds for power-on and 20 seconds for reset of a 23506 * sonoma lun. 23507 * 23508 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 23509 * 23510 * Return Code: 0 - Command completed successfully with good status 23511 * -1 - Command failed. 23512 * 23513 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 23514 * be fixed (removing this code), we need to determine how to handle the 23515 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 23516 * 23517 * NOTE: This code is only called off sddump(). 23518 */ 23519 static int 23520 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 23521 { 23522 int rval = -1; 23523 int savef; 23524 long savet; 23525 void (*savec)(); 23526 int timeout; 23527 int busy_count; 23528 int poll_delay; 23529 int rc; 23530 uint8_t *sensep; 23531 struct scsi_arq_status *arqstat; 23532 extern int do_polled_io; 23533 23534 ASSERT(pkt->pkt_scbp); 23535 23536 /* 23537 * save old flags.. 23538 */ 23539 savef = pkt->pkt_flags; 23540 savec = pkt->pkt_comp; 23541 savet = pkt->pkt_time; 23542 23543 pkt->pkt_flags |= FLAG_NOINTR; 23544 23545 /* 23546 * XXX there is nothing in the SCSA spec that states that we should not 23547 * do a callback for polled cmds; however, removing this will break sd 23548 * and probably other target drivers 23549 */ 23550 pkt->pkt_comp = NULL; 23551 23552 /* 23553 * we don't like a polled command without timeout. 23554 * 60 seconds seems long enough. 23555 */ 23556 if (pkt->pkt_time == 0) 23557 pkt->pkt_time = SCSI_POLL_TIMEOUT; 23558 23559 /* 23560 * Send polled cmd. 23561 * 23562 * We do some error recovery for various errors. Tran_busy, 23563 * queue full, and non-dispatched commands are retried every 10 msec. 23564 * as they are typically transient failures. Busy status and Not 23565 * Ready are retried every second as this status takes a while to 23566 * change. 23567 */ 23568 timeout = pkt->pkt_time * SEC_TO_CSEC; 23569 23570 for (busy_count = 0; busy_count < timeout; busy_count++) { 23571 /* 23572 * Initialize pkt status variables. 23573 */ 23574 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 23575 23576 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 23577 if (rc != TRAN_BUSY) { 23578 /* Transport failed - give up. */ 23579 break; 23580 } else { 23581 /* Transport busy - try again. */ 23582 poll_delay = 1 * CSEC; /* 10 msec. */ 23583 } 23584 } else { 23585 /* 23586 * Transport accepted - check pkt status. 23587 */ 23588 rc = (*pkt->pkt_scbp) & STATUS_MASK; 23589 if ((pkt->pkt_reason == CMD_CMPLT) && 23590 (rc == STATUS_CHECK) && 23591 (pkt->pkt_state & STATE_ARQ_DONE)) { 23592 arqstat = 23593 (struct scsi_arq_status *)(pkt->pkt_scbp); 23594 sensep = (uint8_t *)&arqstat->sts_sensedata; 23595 } else { 23596 sensep = NULL; 23597 } 23598 23599 if ((pkt->pkt_reason == CMD_CMPLT) && 23600 (rc == STATUS_GOOD)) { 23601 /* No error - we're done */ 23602 rval = 0; 23603 break; 23604 23605 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 23606 /* Lost connection - give up */ 23607 break; 23608 23609 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 23610 (pkt->pkt_state == 0)) { 23611 /* Pkt not dispatched - try again. */ 23612 poll_delay = 1 * CSEC; /* 10 msec. */ 23613 23614 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23615 (rc == STATUS_QFULL)) { 23616 /* Queue full - try again. */ 23617 poll_delay = 1 * CSEC; /* 10 msec. */ 23618 23619 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23620 (rc == STATUS_BUSY)) { 23621 /* Busy - try again. */ 23622 poll_delay = 100 * CSEC; /* 1 sec. */ 23623 busy_count += (SEC_TO_CSEC - 1); 23624 23625 } else if ((sensep != NULL) && 23626 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 23627 /* 23628 * Unit Attention - try again. 23629 * Pretend it took 1 sec. 23630 * NOTE: 'continue' avoids poll_delay 23631 */ 23632 busy_count += (SEC_TO_CSEC - 1); 23633 continue; 23634 23635 } else if ((sensep != NULL) && 23636 (scsi_sense_key(sensep) == KEY_NOT_READY) && 23637 (scsi_sense_asc(sensep) == 0x04) && 23638 (scsi_sense_ascq(sensep) == 0x01)) { 23639 /* 23640 * Not ready -> ready - try again. 23641 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 23642 * ...same as STATUS_BUSY 23643 */ 23644 poll_delay = 100 * CSEC; /* 1 sec. */ 23645 busy_count += (SEC_TO_CSEC - 1); 23646 23647 } else { 23648 /* BAD status - give up. */ 23649 break; 23650 } 23651 } 23652 23653 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 23654 !do_polled_io) { 23655 delay(drv_usectohz(poll_delay)); 23656 } else { 23657 /* we busy wait during cpr_dump or interrupt threads */ 23658 drv_usecwait(poll_delay); 23659 } 23660 } 23661 23662 pkt->pkt_flags = savef; 23663 pkt->pkt_comp = savec; 23664 pkt->pkt_time = savet; 23665 23666 /* return on error */ 23667 if (rval) 23668 return (rval); 23669 23670 /* 23671 * This is not a performance critical code path. 23672 * 23673 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 23674 * issues associated with looking at DMA memory prior to 23675 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 23676 */ 23677 scsi_sync_pkt(pkt); 23678 return (0); 23679 } 23680 23681 23682 23683 /* 23684 * Function: sd_persistent_reservation_in_read_keys 23685 * 23686 * Description: This routine is the driver entry point for handling CD-ROM 23687 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 23688 * by sending the SCSI-3 PRIN commands to the device. 23689 * Processes the read keys command response by copying the 23690 * reservation key information into the user provided buffer. 23691 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 23692 * 23693 * Arguments: un - Pointer to soft state struct for the target. 23694 * usrp - user provided pointer to multihost Persistent In Read 23695 * Keys structure (mhioc_inkeys_t) 23696 * flag - this argument is a pass through to ddi_copyxxx() 23697 * directly from the mode argument of ioctl(). 23698 * 23699 * Return Code: 0 - Success 23700 * EACCES 23701 * ENOTSUP 23702 * errno return code from sd_send_scsi_cmd() 23703 * 23704 * Context: Can sleep. Does not return until command is completed. 23705 */ 23706 23707 static int 23708 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 23709 mhioc_inkeys_t *usrp, int flag) 23710 { 23711 #ifdef _MULTI_DATAMODEL 23712 struct mhioc_key_list32 li32; 23713 #endif 23714 sd_prin_readkeys_t *in; 23715 mhioc_inkeys_t *ptr; 23716 mhioc_key_list_t li; 23717 uchar_t *data_bufp; 23718 int data_len; 23719 int rval; 23720 size_t copysz; 23721 23722 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 23723 return (EINVAL); 23724 } 23725 bzero(&li, sizeof (mhioc_key_list_t)); 23726 23727 /* 23728 * Get the listsize from user 23729 */ 23730 #ifdef _MULTI_DATAMODEL 23731 23732 switch (ddi_model_convert_from(flag & FMODELS)) { 23733 case DDI_MODEL_ILP32: 23734 copysz = sizeof (struct mhioc_key_list32); 23735 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 23736 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23737 "sd_persistent_reservation_in_read_keys: " 23738 "failed ddi_copyin: mhioc_key_list32_t\n"); 23739 rval = EFAULT; 23740 goto done; 23741 } 23742 li.listsize = li32.listsize; 23743 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 23744 break; 23745 23746 case DDI_MODEL_NONE: 23747 copysz = sizeof (mhioc_key_list_t); 23748 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23749 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23750 "sd_persistent_reservation_in_read_keys: " 23751 "failed ddi_copyin: mhioc_key_list_t\n"); 23752 rval = EFAULT; 23753 goto done; 23754 } 23755 break; 23756 } 23757 23758 #else /* ! _MULTI_DATAMODEL */ 23759 copysz = sizeof (mhioc_key_list_t); 23760 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23761 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23762 "sd_persistent_reservation_in_read_keys: " 23763 "failed ddi_copyin: mhioc_key_list_t\n"); 23764 rval = EFAULT; 23765 goto done; 23766 } 23767 #endif 23768 23769 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 23770 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 23771 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23772 23773 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 23774 data_len, data_bufp)) != 0) { 23775 goto done; 23776 } 23777 in = (sd_prin_readkeys_t *)data_bufp; 23778 ptr->generation = BE_32(in->generation); 23779 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 23780 23781 /* 23782 * Return the min(listsize, listlen) keys 23783 */ 23784 #ifdef _MULTI_DATAMODEL 23785 23786 switch (ddi_model_convert_from(flag & FMODELS)) { 23787 case DDI_MODEL_ILP32: 23788 li32.listlen = li.listlen; 23789 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 23790 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23791 "sd_persistent_reservation_in_read_keys: " 23792 "failed ddi_copyout: mhioc_key_list32_t\n"); 23793 rval = EFAULT; 23794 goto done; 23795 } 23796 break; 23797 23798 case DDI_MODEL_NONE: 23799 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23800 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23801 "sd_persistent_reservation_in_read_keys: " 23802 "failed ddi_copyout: mhioc_key_list_t\n"); 23803 rval = EFAULT; 23804 goto done; 23805 } 23806 break; 23807 } 23808 23809 #else /* ! _MULTI_DATAMODEL */ 23810 23811 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23812 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23813 "sd_persistent_reservation_in_read_keys: " 23814 "failed ddi_copyout: mhioc_key_list_t\n"); 23815 rval = EFAULT; 23816 goto done; 23817 } 23818 23819 #endif /* _MULTI_DATAMODEL */ 23820 23821 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 23822 li.listsize * MHIOC_RESV_KEY_SIZE); 23823 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 23824 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23825 "sd_persistent_reservation_in_read_keys: " 23826 "failed ddi_copyout: keylist\n"); 23827 rval = EFAULT; 23828 } 23829 done: 23830 kmem_free(data_bufp, data_len); 23831 return (rval); 23832 } 23833 23834 23835 /* 23836 * Function: sd_persistent_reservation_in_read_resv 23837 * 23838 * Description: This routine is the driver entry point for handling CD-ROM 23839 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 23840 * by sending the SCSI-3 PRIN commands to the device. 23841 * Process the read persistent reservations command response by 23842 * copying the reservation information into the user provided 23843 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 23844 * 23845 * Arguments: un - Pointer to soft state struct for the target. 23846 * usrp - user provided pointer to multihost Persistent In Read 23847 * Keys structure (mhioc_inkeys_t) 23848 * flag - this argument is a pass through to ddi_copyxxx() 23849 * directly from the mode argument of ioctl(). 23850 * 23851 * Return Code: 0 - Success 23852 * EACCES 23853 * ENOTSUP 23854 * errno return code from sd_send_scsi_cmd() 23855 * 23856 * Context: Can sleep. Does not return until command is completed. 23857 */ 23858 23859 static int 23860 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 23861 mhioc_inresvs_t *usrp, int flag) 23862 { 23863 #ifdef _MULTI_DATAMODEL 23864 struct mhioc_resv_desc_list32 resvlist32; 23865 #endif 23866 sd_prin_readresv_t *in; 23867 mhioc_inresvs_t *ptr; 23868 sd_readresv_desc_t *readresv_ptr; 23869 mhioc_resv_desc_list_t resvlist; 23870 mhioc_resv_desc_t resvdesc; 23871 uchar_t *data_bufp; 23872 int data_len; 23873 int rval; 23874 int i; 23875 size_t copysz; 23876 mhioc_resv_desc_t *bufp; 23877 23878 if ((ptr = usrp) == NULL) { 23879 return (EINVAL); 23880 } 23881 23882 /* 23883 * Get the listsize from user 23884 */ 23885 #ifdef _MULTI_DATAMODEL 23886 switch (ddi_model_convert_from(flag & FMODELS)) { 23887 case DDI_MODEL_ILP32: 23888 copysz = sizeof (struct mhioc_resv_desc_list32); 23889 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 23890 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23891 "sd_persistent_reservation_in_read_resv: " 23892 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23893 rval = EFAULT; 23894 goto done; 23895 } 23896 resvlist.listsize = resvlist32.listsize; 23897 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 23898 break; 23899 23900 case DDI_MODEL_NONE: 23901 copysz = sizeof (mhioc_resv_desc_list_t); 23902 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23903 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23904 "sd_persistent_reservation_in_read_resv: " 23905 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23906 rval = EFAULT; 23907 goto done; 23908 } 23909 break; 23910 } 23911 #else /* ! _MULTI_DATAMODEL */ 23912 copysz = sizeof (mhioc_resv_desc_list_t); 23913 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23914 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23915 "sd_persistent_reservation_in_read_resv: " 23916 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23917 rval = EFAULT; 23918 goto done; 23919 } 23920 #endif /* ! _MULTI_DATAMODEL */ 23921 23922 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 23923 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 23924 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23925 23926 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 23927 data_len, data_bufp)) != 0) { 23928 goto done; 23929 } 23930 in = (sd_prin_readresv_t *)data_bufp; 23931 ptr->generation = BE_32(in->generation); 23932 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 23933 23934 /* 23935 * Return the min(listsize, listlen( keys 23936 */ 23937 #ifdef _MULTI_DATAMODEL 23938 23939 switch (ddi_model_convert_from(flag & FMODELS)) { 23940 case DDI_MODEL_ILP32: 23941 resvlist32.listlen = resvlist.listlen; 23942 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 23943 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23944 "sd_persistent_reservation_in_read_resv: " 23945 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23946 rval = EFAULT; 23947 goto done; 23948 } 23949 break; 23950 23951 case DDI_MODEL_NONE: 23952 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23953 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23954 "sd_persistent_reservation_in_read_resv: " 23955 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23956 rval = EFAULT; 23957 goto done; 23958 } 23959 break; 23960 } 23961 23962 #else /* ! _MULTI_DATAMODEL */ 23963 23964 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23965 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23966 "sd_persistent_reservation_in_read_resv: " 23967 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23968 rval = EFAULT; 23969 goto done; 23970 } 23971 23972 #endif /* ! _MULTI_DATAMODEL */ 23973 23974 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 23975 bufp = resvlist.list; 23976 copysz = sizeof (mhioc_resv_desc_t); 23977 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 23978 i++, readresv_ptr++, bufp++) { 23979 23980 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 23981 MHIOC_RESV_KEY_SIZE); 23982 resvdesc.type = readresv_ptr->type; 23983 resvdesc.scope = readresv_ptr->scope; 23984 resvdesc.scope_specific_addr = 23985 BE_32(readresv_ptr->scope_specific_addr); 23986 23987 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 23988 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23989 "sd_persistent_reservation_in_read_resv: " 23990 "failed ddi_copyout: resvlist\n"); 23991 rval = EFAULT; 23992 goto done; 23993 } 23994 } 23995 done: 23996 kmem_free(data_bufp, data_len); 23997 return (rval); 23998 } 23999 24000 24001 /* 24002 * Function: sr_change_blkmode() 24003 * 24004 * Description: This routine is the driver entry point for handling CD-ROM 24005 * block mode ioctl requests. Support for returning and changing 24006 * the current block size in use by the device is implemented. The 24007 * LBA size is changed via a MODE SELECT Block Descriptor. 24008 * 24009 * This routine issues a mode sense with an allocation length of 24010 * 12 bytes for the mode page header and a single block descriptor. 24011 * 24012 * Arguments: dev - the device 'dev_t' 24013 * cmd - the request type; one of CDROMGBLKMODE (get) or 24014 * CDROMSBLKMODE (set) 24015 * data - current block size or requested block size 24016 * flag - this argument is a pass through to ddi_copyxxx() directly 24017 * from the mode argument of ioctl(). 24018 * 24019 * Return Code: the code returned by sd_send_scsi_cmd() 24020 * EINVAL if invalid arguments are provided 24021 * EFAULT if ddi_copyxxx() fails 24022 * ENXIO if fail ddi_get_soft_state 24023 * EIO if invalid mode sense block descriptor length 24024 * 24025 */ 24026 24027 static int 24028 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 24029 { 24030 struct sd_lun *un = NULL; 24031 struct mode_header *sense_mhp, *select_mhp; 24032 struct block_descriptor *sense_desc, *select_desc; 24033 int current_bsize; 24034 int rval = EINVAL; 24035 uchar_t *sense = NULL; 24036 uchar_t *select = NULL; 24037 24038 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 24039 24040 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24041 return (ENXIO); 24042 } 24043 24044 /* 24045 * The block length is changed via the Mode Select block descriptor, the 24046 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 24047 * required as part of this routine. Therefore the mode sense allocation 24048 * length is specified to be the length of a mode page header and a 24049 * block descriptor. 24050 */ 24051 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 24052 24053 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 24054 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 24055 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24056 "sr_change_blkmode: Mode Sense Failed\n"); 24057 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 24058 return (rval); 24059 } 24060 24061 /* Check the block descriptor len to handle only 1 block descriptor */ 24062 sense_mhp = (struct mode_header *)sense; 24063 if ((sense_mhp->bdesc_length == 0) || 24064 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 24065 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24066 "sr_change_blkmode: Mode Sense returned invalid block" 24067 " descriptor length\n"); 24068 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 24069 return (EIO); 24070 } 24071 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 24072 current_bsize = ((sense_desc->blksize_hi << 16) | 24073 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 24074 24075 /* Process command */ 24076 switch (cmd) { 24077 case CDROMGBLKMODE: 24078 /* Return the block size obtained during the mode sense */ 24079 if (ddi_copyout(¤t_bsize, (void *)data, 24080 sizeof (int), flag) != 0) 24081 rval = EFAULT; 24082 break; 24083 case CDROMSBLKMODE: 24084 /* Validate the requested block size */ 24085 switch (data) { 24086 case CDROM_BLK_512: 24087 case CDROM_BLK_1024: 24088 case CDROM_BLK_2048: 24089 case CDROM_BLK_2056: 24090 case CDROM_BLK_2336: 24091 case CDROM_BLK_2340: 24092 case CDROM_BLK_2352: 24093 case CDROM_BLK_2368: 24094 case CDROM_BLK_2448: 24095 case CDROM_BLK_2646: 24096 case CDROM_BLK_2647: 24097 break; 24098 default: 24099 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24100 "sr_change_blkmode: " 24101 "Block Size '%ld' Not Supported\n", data); 24102 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 24103 return (EINVAL); 24104 } 24105 24106 /* 24107 * The current block size matches the requested block size so 24108 * there is no need to send the mode select to change the size 24109 */ 24110 if (current_bsize == data) { 24111 break; 24112 } 24113 24114 /* Build the select data for the requested block size */ 24115 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 24116 select_mhp = (struct mode_header *)select; 24117 select_desc = 24118 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 24119 /* 24120 * The LBA size is changed via the block descriptor, so the 24121 * descriptor is built according to the user data 24122 */ 24123 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 24124 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 24125 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 24126 select_desc->blksize_lo = (char)((data) & 0x000000ff); 24127 24128 /* Send the mode select for the requested block size */ 24129 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 24130 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 24131 SD_PATH_STANDARD)) != 0) { 24132 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24133 "sr_change_blkmode: Mode Select Failed\n"); 24134 /* 24135 * The mode select failed for the requested block size, 24136 * so reset the data for the original block size and 24137 * send it to the target. The error is indicated by the 24138 * return value for the failed mode select. 24139 */ 24140 select_desc->blksize_hi = sense_desc->blksize_hi; 24141 select_desc->blksize_mid = sense_desc->blksize_mid; 24142 select_desc->blksize_lo = sense_desc->blksize_lo; 24143 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 24144 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 24145 SD_PATH_STANDARD); 24146 } else { 24147 ASSERT(!mutex_owned(SD_MUTEX(un))); 24148 mutex_enter(SD_MUTEX(un)); 24149 sd_update_block_info(un, (uint32_t)data, 0); 24150 mutex_exit(SD_MUTEX(un)); 24151 } 24152 break; 24153 default: 24154 /* should not reach here, but check anyway */ 24155 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24156 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 24157 rval = EINVAL; 24158 break; 24159 } 24160 24161 if (select) { 24162 kmem_free(select, BUFLEN_CHG_BLK_MODE); 24163 } 24164 if (sense) { 24165 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 24166 } 24167 return (rval); 24168 } 24169 24170 24171 /* 24172 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 24173 * implement driver support for getting and setting the CD speed. The command 24174 * set used will be based on the device type. If the device has not been 24175 * identified as MMC the Toshiba vendor specific mode page will be used. If 24176 * the device is MMC but does not support the Real Time Streaming feature 24177 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 24178 * be used to read the speed. 24179 */ 24180 24181 /* 24182 * Function: sr_change_speed() 24183 * 24184 * Description: This routine is the driver entry point for handling CD-ROM 24185 * drive speed ioctl requests for devices supporting the Toshiba 24186 * vendor specific drive speed mode page. Support for returning 24187 * and changing the current drive speed in use by the device is 24188 * implemented. 24189 * 24190 * Arguments: dev - the device 'dev_t' 24191 * cmd - the request type; one of CDROMGDRVSPEED (get) or 24192 * CDROMSDRVSPEED (set) 24193 * data - current drive speed or requested drive speed 24194 * flag - this argument is a pass through to ddi_copyxxx() directly 24195 * from the mode argument of ioctl(). 24196 * 24197 * Return Code: the code returned by sd_send_scsi_cmd() 24198 * EINVAL if invalid arguments are provided 24199 * EFAULT if ddi_copyxxx() fails 24200 * ENXIO if fail ddi_get_soft_state 24201 * EIO if invalid mode sense block descriptor length 24202 */ 24203 24204 static int 24205 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24206 { 24207 struct sd_lun *un = NULL; 24208 struct mode_header *sense_mhp, *select_mhp; 24209 struct mode_speed *sense_page, *select_page; 24210 int current_speed; 24211 int rval = EINVAL; 24212 int bd_len; 24213 uchar_t *sense = NULL; 24214 uchar_t *select = NULL; 24215 24216 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24217 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24218 return (ENXIO); 24219 } 24220 24221 /* 24222 * Note: The drive speed is being modified here according to a Toshiba 24223 * vendor specific mode page (0x31). 24224 */ 24225 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24226 24227 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 24228 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 24229 SD_PATH_STANDARD)) != 0) { 24230 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24231 "sr_change_speed: Mode Sense Failed\n"); 24232 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24233 return (rval); 24234 } 24235 sense_mhp = (struct mode_header *)sense; 24236 24237 /* Check the block descriptor len to handle only 1 block descriptor */ 24238 bd_len = sense_mhp->bdesc_length; 24239 if (bd_len > MODE_BLK_DESC_LENGTH) { 24240 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24241 "sr_change_speed: Mode Sense returned invalid block " 24242 "descriptor length\n"); 24243 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24244 return (EIO); 24245 } 24246 24247 sense_page = (struct mode_speed *) 24248 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 24249 current_speed = sense_page->speed; 24250 24251 /* Process command */ 24252 switch (cmd) { 24253 case CDROMGDRVSPEED: 24254 /* Return the drive speed obtained during the mode sense */ 24255 if (current_speed == 0x2) { 24256 current_speed = CDROM_TWELVE_SPEED; 24257 } 24258 if (ddi_copyout(¤t_speed, (void *)data, 24259 sizeof (int), flag) != 0) { 24260 rval = EFAULT; 24261 } 24262 break; 24263 case CDROMSDRVSPEED: 24264 /* Validate the requested drive speed */ 24265 switch ((uchar_t)data) { 24266 case CDROM_TWELVE_SPEED: 24267 data = 0x2; 24268 /*FALLTHROUGH*/ 24269 case CDROM_NORMAL_SPEED: 24270 case CDROM_DOUBLE_SPEED: 24271 case CDROM_QUAD_SPEED: 24272 case CDROM_MAXIMUM_SPEED: 24273 break; 24274 default: 24275 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24276 "sr_change_speed: " 24277 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 24278 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24279 return (EINVAL); 24280 } 24281 24282 /* 24283 * The current drive speed matches the requested drive speed so 24284 * there is no need to send the mode select to change the speed 24285 */ 24286 if (current_speed == data) { 24287 break; 24288 } 24289 24290 /* Build the select data for the requested drive speed */ 24291 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24292 select_mhp = (struct mode_header *)select; 24293 select_mhp->bdesc_length = 0; 24294 select_page = 24295 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24296 select_page = 24297 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24298 select_page->mode_page.code = CDROM_MODE_SPEED; 24299 select_page->mode_page.length = 2; 24300 select_page->speed = (uchar_t)data; 24301 24302 /* Send the mode select for the requested block size */ 24303 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24304 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24305 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 24306 /* 24307 * The mode select failed for the requested drive speed, 24308 * so reset the data for the original drive speed and 24309 * send it to the target. The error is indicated by the 24310 * return value for the failed mode select. 24311 */ 24312 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24313 "sr_drive_speed: Mode Select Failed\n"); 24314 select_page->speed = sense_page->speed; 24315 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24316 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24317 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 24318 } 24319 break; 24320 default: 24321 /* should not reach here, but check anyway */ 24322 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24323 "sr_change_speed: Command '%x' Not Supported\n", cmd); 24324 rval = EINVAL; 24325 break; 24326 } 24327 24328 if (select) { 24329 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 24330 } 24331 if (sense) { 24332 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24333 } 24334 24335 return (rval); 24336 } 24337 24338 24339 /* 24340 * Function: sr_atapi_change_speed() 24341 * 24342 * Description: This routine is the driver entry point for handling CD-ROM 24343 * drive speed ioctl requests for MMC devices that do not support 24344 * the Real Time Streaming feature (0x107). 24345 * 24346 * Note: This routine will use the SET SPEED command which may not 24347 * be supported by all devices. 24348 * 24349 * Arguments: dev- the device 'dev_t' 24350 * cmd- the request type; one of CDROMGDRVSPEED (get) or 24351 * CDROMSDRVSPEED (set) 24352 * data- current drive speed or requested drive speed 24353 * flag- this argument is a pass through to ddi_copyxxx() directly 24354 * from the mode argument of ioctl(). 24355 * 24356 * Return Code: the code returned by sd_send_scsi_cmd() 24357 * EINVAL if invalid arguments are provided 24358 * EFAULT if ddi_copyxxx() fails 24359 * ENXIO if fail ddi_get_soft_state 24360 * EIO if invalid mode sense block descriptor length 24361 */ 24362 24363 static int 24364 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24365 { 24366 struct sd_lun *un; 24367 struct uscsi_cmd *com = NULL; 24368 struct mode_header_grp2 *sense_mhp; 24369 uchar_t *sense_page; 24370 uchar_t *sense = NULL; 24371 char cdb[CDB_GROUP5]; 24372 int bd_len; 24373 int current_speed = 0; 24374 int max_speed = 0; 24375 int rval; 24376 24377 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24378 24379 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24380 return (ENXIO); 24381 } 24382 24383 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 24384 24385 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 24386 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 24387 SD_PATH_STANDARD)) != 0) { 24388 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24389 "sr_atapi_change_speed: Mode Sense Failed\n"); 24390 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24391 return (rval); 24392 } 24393 24394 /* Check the block descriptor len to handle only 1 block descriptor */ 24395 sense_mhp = (struct mode_header_grp2 *)sense; 24396 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 24397 if (bd_len > MODE_BLK_DESC_LENGTH) { 24398 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24399 "sr_atapi_change_speed: Mode Sense returned invalid " 24400 "block descriptor length\n"); 24401 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24402 return (EIO); 24403 } 24404 24405 /* Calculate the current and maximum drive speeds */ 24406 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 24407 current_speed = (sense_page[14] << 8) | sense_page[15]; 24408 max_speed = (sense_page[8] << 8) | sense_page[9]; 24409 24410 /* Process the command */ 24411 switch (cmd) { 24412 case CDROMGDRVSPEED: 24413 current_speed /= SD_SPEED_1X; 24414 if (ddi_copyout(¤t_speed, (void *)data, 24415 sizeof (int), flag) != 0) 24416 rval = EFAULT; 24417 break; 24418 case CDROMSDRVSPEED: 24419 /* Convert the speed code to KB/sec */ 24420 switch ((uchar_t)data) { 24421 case CDROM_NORMAL_SPEED: 24422 current_speed = SD_SPEED_1X; 24423 break; 24424 case CDROM_DOUBLE_SPEED: 24425 current_speed = 2 * SD_SPEED_1X; 24426 break; 24427 case CDROM_QUAD_SPEED: 24428 current_speed = 4 * SD_SPEED_1X; 24429 break; 24430 case CDROM_TWELVE_SPEED: 24431 current_speed = 12 * SD_SPEED_1X; 24432 break; 24433 case CDROM_MAXIMUM_SPEED: 24434 current_speed = 0xffff; 24435 break; 24436 default: 24437 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24438 "sr_atapi_change_speed: invalid drive speed %d\n", 24439 (uchar_t)data); 24440 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24441 return (EINVAL); 24442 } 24443 24444 /* Check the request against the drive's max speed. */ 24445 if (current_speed != 0xffff) { 24446 if (current_speed > max_speed) { 24447 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24448 return (EINVAL); 24449 } 24450 } 24451 24452 /* 24453 * Build and send the SET SPEED command 24454 * 24455 * Note: The SET SPEED (0xBB) command used in this routine is 24456 * obsolete per the SCSI MMC spec but still supported in the 24457 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24458 * therefore the command is still implemented in this routine. 24459 */ 24460 bzero(cdb, sizeof (cdb)); 24461 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 24462 cdb[2] = (uchar_t)(current_speed >> 8); 24463 cdb[3] = (uchar_t)current_speed; 24464 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24465 com->uscsi_cdb = (caddr_t)cdb; 24466 com->uscsi_cdblen = CDB_GROUP5; 24467 com->uscsi_bufaddr = NULL; 24468 com->uscsi_buflen = 0; 24469 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24470 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 24471 break; 24472 default: 24473 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24474 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 24475 rval = EINVAL; 24476 } 24477 24478 if (sense) { 24479 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24480 } 24481 if (com) { 24482 kmem_free(com, sizeof (*com)); 24483 } 24484 return (rval); 24485 } 24486 24487 24488 /* 24489 * Function: sr_pause_resume() 24490 * 24491 * Description: This routine is the driver entry point for handling CD-ROM 24492 * pause/resume ioctl requests. This only affects the audio play 24493 * operation. 24494 * 24495 * Arguments: dev - the device 'dev_t' 24496 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 24497 * for setting the resume bit of the cdb. 24498 * 24499 * Return Code: the code returned by sd_send_scsi_cmd() 24500 * EINVAL if invalid mode specified 24501 * 24502 */ 24503 24504 static int 24505 sr_pause_resume(dev_t dev, int cmd) 24506 { 24507 struct sd_lun *un; 24508 struct uscsi_cmd *com; 24509 char cdb[CDB_GROUP1]; 24510 int rval; 24511 24512 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24513 return (ENXIO); 24514 } 24515 24516 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24517 bzero(cdb, CDB_GROUP1); 24518 cdb[0] = SCMD_PAUSE_RESUME; 24519 switch (cmd) { 24520 case CDROMRESUME: 24521 cdb[8] = 1; 24522 break; 24523 case CDROMPAUSE: 24524 cdb[8] = 0; 24525 break; 24526 default: 24527 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 24528 " Command '%x' Not Supported\n", cmd); 24529 rval = EINVAL; 24530 goto done; 24531 } 24532 24533 com->uscsi_cdb = cdb; 24534 com->uscsi_cdblen = CDB_GROUP1; 24535 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24536 24537 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24538 SD_PATH_STANDARD); 24539 24540 done: 24541 kmem_free(com, sizeof (*com)); 24542 return (rval); 24543 } 24544 24545 24546 /* 24547 * Function: sr_play_msf() 24548 * 24549 * Description: This routine is the driver entry point for handling CD-ROM 24550 * ioctl requests to output the audio signals at the specified 24551 * starting address and continue the audio play until the specified 24552 * ending address (CDROMPLAYMSF) The address is in Minute Second 24553 * Frame (MSF) format. 24554 * 24555 * Arguments: dev - the device 'dev_t' 24556 * data - pointer to user provided audio msf structure, 24557 * specifying start/end addresses. 24558 * flag - this argument is a pass through to ddi_copyxxx() 24559 * directly from the mode argument of ioctl(). 24560 * 24561 * Return Code: the code returned by sd_send_scsi_cmd() 24562 * EFAULT if ddi_copyxxx() fails 24563 * ENXIO if fail ddi_get_soft_state 24564 * EINVAL if data pointer is NULL 24565 */ 24566 24567 static int 24568 sr_play_msf(dev_t dev, caddr_t data, int flag) 24569 { 24570 struct sd_lun *un; 24571 struct uscsi_cmd *com; 24572 struct cdrom_msf msf_struct; 24573 struct cdrom_msf *msf = &msf_struct; 24574 char cdb[CDB_GROUP1]; 24575 int rval; 24576 24577 if (data == NULL) { 24578 return (EINVAL); 24579 } 24580 24581 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24582 return (ENXIO); 24583 } 24584 24585 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 24586 return (EFAULT); 24587 } 24588 24589 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24590 bzero(cdb, CDB_GROUP1); 24591 cdb[0] = SCMD_PLAYAUDIO_MSF; 24592 if (un->un_f_cfg_playmsf_bcd == TRUE) { 24593 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 24594 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 24595 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 24596 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 24597 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 24598 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 24599 } else { 24600 cdb[3] = msf->cdmsf_min0; 24601 cdb[4] = msf->cdmsf_sec0; 24602 cdb[5] = msf->cdmsf_frame0; 24603 cdb[6] = msf->cdmsf_min1; 24604 cdb[7] = msf->cdmsf_sec1; 24605 cdb[8] = msf->cdmsf_frame1; 24606 } 24607 com->uscsi_cdb = cdb; 24608 com->uscsi_cdblen = CDB_GROUP1; 24609 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24610 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24611 SD_PATH_STANDARD); 24612 kmem_free(com, sizeof (*com)); 24613 return (rval); 24614 } 24615 24616 24617 /* 24618 * Function: sr_play_trkind() 24619 * 24620 * Description: This routine is the driver entry point for handling CD-ROM 24621 * ioctl requests to output the audio signals at the specified 24622 * starting address and continue the audio play until the specified 24623 * ending address (CDROMPLAYTRKIND). The address is in Track Index 24624 * format. 24625 * 24626 * Arguments: dev - the device 'dev_t' 24627 * data - pointer to user provided audio track/index structure, 24628 * specifying start/end addresses. 24629 * flag - this argument is a pass through to ddi_copyxxx() 24630 * directly from the mode argument of ioctl(). 24631 * 24632 * Return Code: the code returned by sd_send_scsi_cmd() 24633 * EFAULT if ddi_copyxxx() fails 24634 * ENXIO if fail ddi_get_soft_state 24635 * EINVAL if data pointer is NULL 24636 */ 24637 24638 static int 24639 sr_play_trkind(dev_t dev, caddr_t data, int flag) 24640 { 24641 struct cdrom_ti ti_struct; 24642 struct cdrom_ti *ti = &ti_struct; 24643 struct uscsi_cmd *com = NULL; 24644 char cdb[CDB_GROUP1]; 24645 int rval; 24646 24647 if (data == NULL) { 24648 return (EINVAL); 24649 } 24650 24651 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 24652 return (EFAULT); 24653 } 24654 24655 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24656 bzero(cdb, CDB_GROUP1); 24657 cdb[0] = SCMD_PLAYAUDIO_TI; 24658 cdb[4] = ti->cdti_trk0; 24659 cdb[5] = ti->cdti_ind0; 24660 cdb[7] = ti->cdti_trk1; 24661 cdb[8] = ti->cdti_ind1; 24662 com->uscsi_cdb = cdb; 24663 com->uscsi_cdblen = CDB_GROUP1; 24664 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24665 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24666 SD_PATH_STANDARD); 24667 kmem_free(com, sizeof (*com)); 24668 return (rval); 24669 } 24670 24671 24672 /* 24673 * Function: sr_read_all_subcodes() 24674 * 24675 * Description: This routine is the driver entry point for handling CD-ROM 24676 * ioctl requests to return raw subcode data while the target is 24677 * playing audio (CDROMSUBCODE). 24678 * 24679 * Arguments: dev - the device 'dev_t' 24680 * data - pointer to user provided cdrom subcode structure, 24681 * specifying the transfer length and address. 24682 * flag - this argument is a pass through to ddi_copyxxx() 24683 * directly from the mode argument of ioctl(). 24684 * 24685 * Return Code: the code returned by sd_send_scsi_cmd() 24686 * EFAULT if ddi_copyxxx() fails 24687 * ENXIO if fail ddi_get_soft_state 24688 * EINVAL if data pointer is NULL 24689 */ 24690 24691 static int 24692 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 24693 { 24694 struct sd_lun *un = NULL; 24695 struct uscsi_cmd *com = NULL; 24696 struct cdrom_subcode *subcode = NULL; 24697 int rval; 24698 size_t buflen; 24699 char cdb[CDB_GROUP5]; 24700 24701 #ifdef _MULTI_DATAMODEL 24702 /* To support ILP32 applications in an LP64 world */ 24703 struct cdrom_subcode32 cdrom_subcode32; 24704 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 24705 #endif 24706 if (data == NULL) { 24707 return (EINVAL); 24708 } 24709 24710 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24711 return (ENXIO); 24712 } 24713 24714 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 24715 24716 #ifdef _MULTI_DATAMODEL 24717 switch (ddi_model_convert_from(flag & FMODELS)) { 24718 case DDI_MODEL_ILP32: 24719 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 24720 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24721 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24722 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24723 return (EFAULT); 24724 } 24725 /* Convert the ILP32 uscsi data from the application to LP64 */ 24726 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 24727 break; 24728 case DDI_MODEL_NONE: 24729 if (ddi_copyin(data, subcode, 24730 sizeof (struct cdrom_subcode), flag)) { 24731 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24732 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24733 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24734 return (EFAULT); 24735 } 24736 break; 24737 } 24738 #else /* ! _MULTI_DATAMODEL */ 24739 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 24740 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24741 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24742 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24743 return (EFAULT); 24744 } 24745 #endif /* _MULTI_DATAMODEL */ 24746 24747 /* 24748 * Since MMC-2 expects max 3 bytes for length, check if the 24749 * length input is greater than 3 bytes 24750 */ 24751 if ((subcode->cdsc_length & 0xFF000000) != 0) { 24752 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24753 "sr_read_all_subcodes: " 24754 "cdrom transfer length too large: %d (limit %d)\n", 24755 subcode->cdsc_length, 0xFFFFFF); 24756 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24757 return (EINVAL); 24758 } 24759 24760 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 24761 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24762 bzero(cdb, CDB_GROUP5); 24763 24764 if (un->un_f_mmc_cap == TRUE) { 24765 cdb[0] = (char)SCMD_READ_CD; 24766 cdb[2] = (char)0xff; 24767 cdb[3] = (char)0xff; 24768 cdb[4] = (char)0xff; 24769 cdb[5] = (char)0xff; 24770 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24771 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24772 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 24773 cdb[10] = 1; 24774 } else { 24775 /* 24776 * Note: A vendor specific command (0xDF) is being used her to 24777 * request a read of all subcodes. 24778 */ 24779 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 24780 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 24781 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24782 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24783 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 24784 } 24785 com->uscsi_cdb = cdb; 24786 com->uscsi_cdblen = CDB_GROUP5; 24787 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 24788 com->uscsi_buflen = buflen; 24789 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24790 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24791 SD_PATH_STANDARD); 24792 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24793 kmem_free(com, sizeof (*com)); 24794 return (rval); 24795 } 24796 24797 24798 /* 24799 * Function: sr_read_subchannel() 24800 * 24801 * Description: This routine is the driver entry point for handling CD-ROM 24802 * ioctl requests to return the Q sub-channel data of the CD 24803 * current position block. (CDROMSUBCHNL) The data includes the 24804 * track number, index number, absolute CD-ROM address (LBA or MSF 24805 * format per the user) , track relative CD-ROM address (LBA or MSF 24806 * format per the user), control data and audio status. 24807 * 24808 * Arguments: dev - the device 'dev_t' 24809 * data - pointer to user provided cdrom sub-channel structure 24810 * flag - this argument is a pass through to ddi_copyxxx() 24811 * directly from the mode argument of ioctl(). 24812 * 24813 * Return Code: the code returned by sd_send_scsi_cmd() 24814 * EFAULT if ddi_copyxxx() fails 24815 * ENXIO if fail ddi_get_soft_state 24816 * EINVAL if data pointer is NULL 24817 */ 24818 24819 static int 24820 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 24821 { 24822 struct sd_lun *un; 24823 struct uscsi_cmd *com; 24824 struct cdrom_subchnl subchanel; 24825 struct cdrom_subchnl *subchnl = &subchanel; 24826 char cdb[CDB_GROUP1]; 24827 caddr_t buffer; 24828 int rval; 24829 24830 if (data == NULL) { 24831 return (EINVAL); 24832 } 24833 24834 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24835 (un->un_state == SD_STATE_OFFLINE)) { 24836 return (ENXIO); 24837 } 24838 24839 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 24840 return (EFAULT); 24841 } 24842 24843 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 24844 bzero(cdb, CDB_GROUP1); 24845 cdb[0] = SCMD_READ_SUBCHANNEL; 24846 /* Set the MSF bit based on the user requested address format */ 24847 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 24848 /* 24849 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 24850 * returned 24851 */ 24852 cdb[2] = 0x40; 24853 /* 24854 * Set byte 3 to specify the return data format. A value of 0x01 24855 * indicates that the CD-ROM current position should be returned. 24856 */ 24857 cdb[3] = 0x01; 24858 cdb[8] = 0x10; 24859 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24860 com->uscsi_cdb = cdb; 24861 com->uscsi_cdblen = CDB_GROUP1; 24862 com->uscsi_bufaddr = buffer; 24863 com->uscsi_buflen = 16; 24864 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24865 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24866 SD_PATH_STANDARD); 24867 if (rval != 0) { 24868 kmem_free(buffer, 16); 24869 kmem_free(com, sizeof (*com)); 24870 return (rval); 24871 } 24872 24873 /* Process the returned Q sub-channel data */ 24874 subchnl->cdsc_audiostatus = buffer[1]; 24875 subchnl->cdsc_adr = (buffer[5] & 0xF0); 24876 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 24877 subchnl->cdsc_trk = buffer[6]; 24878 subchnl->cdsc_ind = buffer[7]; 24879 if (subchnl->cdsc_format & CDROM_LBA) { 24880 subchnl->cdsc_absaddr.lba = 24881 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24882 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24883 subchnl->cdsc_reladdr.lba = 24884 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 24885 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 24886 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 24887 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 24888 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 24889 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 24890 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 24891 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 24892 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 24893 } else { 24894 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 24895 subchnl->cdsc_absaddr.msf.second = buffer[10]; 24896 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 24897 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 24898 subchnl->cdsc_reladdr.msf.second = buffer[14]; 24899 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 24900 } 24901 kmem_free(buffer, 16); 24902 kmem_free(com, sizeof (*com)); 24903 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 24904 != 0) { 24905 return (EFAULT); 24906 } 24907 return (rval); 24908 } 24909 24910 24911 /* 24912 * Function: sr_read_tocentry() 24913 * 24914 * Description: This routine is the driver entry point for handling CD-ROM 24915 * ioctl requests to read from the Table of Contents (TOC) 24916 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 24917 * fields, the starting address (LBA or MSF format per the user) 24918 * and the data mode if the user specified track is a data track. 24919 * 24920 * Note: The READ HEADER (0x44) command used in this routine is 24921 * obsolete per the SCSI MMC spec but still supported in the 24922 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24923 * therefore the command is still implemented in this routine. 24924 * 24925 * Arguments: dev - the device 'dev_t' 24926 * data - pointer to user provided toc entry structure, 24927 * specifying the track # and the address format 24928 * (LBA or MSF). 24929 * flag - this argument is a pass through to ddi_copyxxx() 24930 * directly from the mode argument of ioctl(). 24931 * 24932 * Return Code: the code returned by sd_send_scsi_cmd() 24933 * EFAULT if ddi_copyxxx() fails 24934 * ENXIO if fail ddi_get_soft_state 24935 * EINVAL if data pointer is NULL 24936 */ 24937 24938 static int 24939 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 24940 { 24941 struct sd_lun *un = NULL; 24942 struct uscsi_cmd *com; 24943 struct cdrom_tocentry toc_entry; 24944 struct cdrom_tocentry *entry = &toc_entry; 24945 caddr_t buffer; 24946 int rval; 24947 char cdb[CDB_GROUP1]; 24948 24949 if (data == NULL) { 24950 return (EINVAL); 24951 } 24952 24953 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24954 (un->un_state == SD_STATE_OFFLINE)) { 24955 return (ENXIO); 24956 } 24957 24958 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 24959 return (EFAULT); 24960 } 24961 24962 /* Validate the requested track and address format */ 24963 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 24964 return (EINVAL); 24965 } 24966 24967 if (entry->cdte_track == 0) { 24968 return (EINVAL); 24969 } 24970 24971 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 24972 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24973 bzero(cdb, CDB_GROUP1); 24974 24975 cdb[0] = SCMD_READ_TOC; 24976 /* Set the MSF bit based on the user requested address format */ 24977 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 24978 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24979 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 24980 } else { 24981 cdb[6] = entry->cdte_track; 24982 } 24983 24984 /* 24985 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 24986 * (4 byte TOC response header + 8 byte track descriptor) 24987 */ 24988 cdb[8] = 12; 24989 com->uscsi_cdb = cdb; 24990 com->uscsi_cdblen = CDB_GROUP1; 24991 com->uscsi_bufaddr = buffer; 24992 com->uscsi_buflen = 0x0C; 24993 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 24994 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24995 SD_PATH_STANDARD); 24996 if (rval != 0) { 24997 kmem_free(buffer, 12); 24998 kmem_free(com, sizeof (*com)); 24999 return (rval); 25000 } 25001 25002 /* Process the toc entry */ 25003 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 25004 entry->cdte_ctrl = (buffer[5] & 0x0F); 25005 if (entry->cdte_format & CDROM_LBA) { 25006 entry->cdte_addr.lba = 25007 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 25008 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 25009 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 25010 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 25011 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 25012 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 25013 /* 25014 * Send a READ TOC command using the LBA address format to get 25015 * the LBA for the track requested so it can be used in the 25016 * READ HEADER request 25017 * 25018 * Note: The MSF bit of the READ HEADER command specifies the 25019 * output format. The block address specified in that command 25020 * must be in LBA format. 25021 */ 25022 cdb[1] = 0; 25023 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25024 SD_PATH_STANDARD); 25025 if (rval != 0) { 25026 kmem_free(buffer, 12); 25027 kmem_free(com, sizeof (*com)); 25028 return (rval); 25029 } 25030 } else { 25031 entry->cdte_addr.msf.minute = buffer[9]; 25032 entry->cdte_addr.msf.second = buffer[10]; 25033 entry->cdte_addr.msf.frame = buffer[11]; 25034 /* 25035 * Send a READ TOC command using the LBA address format to get 25036 * the LBA for the track requested so it can be used in the 25037 * READ HEADER request 25038 * 25039 * Note: The MSF bit of the READ HEADER command specifies the 25040 * output format. The block address specified in that command 25041 * must be in LBA format. 25042 */ 25043 cdb[1] = 0; 25044 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25045 SD_PATH_STANDARD); 25046 if (rval != 0) { 25047 kmem_free(buffer, 12); 25048 kmem_free(com, sizeof (*com)); 25049 return (rval); 25050 } 25051 } 25052 25053 /* 25054 * Build and send the READ HEADER command to determine the data mode of 25055 * the user specified track. 25056 */ 25057 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 25058 (entry->cdte_track != CDROM_LEADOUT)) { 25059 bzero(cdb, CDB_GROUP1); 25060 cdb[0] = SCMD_READ_HEADER; 25061 cdb[2] = buffer[8]; 25062 cdb[3] = buffer[9]; 25063 cdb[4] = buffer[10]; 25064 cdb[5] = buffer[11]; 25065 cdb[8] = 0x08; 25066 com->uscsi_buflen = 0x08; 25067 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25068 SD_PATH_STANDARD); 25069 if (rval == 0) { 25070 entry->cdte_datamode = buffer[0]; 25071 } else { 25072 /* 25073 * READ HEADER command failed, since this is 25074 * obsoleted in one spec, its better to return 25075 * -1 for an invlid track so that we can still 25076 * receive the rest of the TOC data. 25077 */ 25078 entry->cdte_datamode = (uchar_t)-1; 25079 } 25080 } else { 25081 entry->cdte_datamode = (uchar_t)-1; 25082 } 25083 25084 kmem_free(buffer, 12); 25085 kmem_free(com, sizeof (*com)); 25086 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 25087 return (EFAULT); 25088 25089 return (rval); 25090 } 25091 25092 25093 /* 25094 * Function: sr_read_tochdr() 25095 * 25096 * Description: This routine is the driver entry point for handling CD-ROM 25097 * ioctl requests to read the Table of Contents (TOC) header 25098 * (CDROMREADTOHDR). The TOC header consists of the disk starting 25099 * and ending track numbers 25100 * 25101 * Arguments: dev - the device 'dev_t' 25102 * data - pointer to user provided toc header structure, 25103 * specifying the starting and ending track numbers. 25104 * flag - this argument is a pass through to ddi_copyxxx() 25105 * directly from the mode argument of ioctl(). 25106 * 25107 * Return Code: the code returned by sd_send_scsi_cmd() 25108 * EFAULT if ddi_copyxxx() fails 25109 * ENXIO if fail ddi_get_soft_state 25110 * EINVAL if data pointer is NULL 25111 */ 25112 25113 static int 25114 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 25115 { 25116 struct sd_lun *un; 25117 struct uscsi_cmd *com; 25118 struct cdrom_tochdr toc_header; 25119 struct cdrom_tochdr *hdr = &toc_header; 25120 char cdb[CDB_GROUP1]; 25121 int rval; 25122 caddr_t buffer; 25123 25124 if (data == NULL) { 25125 return (EINVAL); 25126 } 25127 25128 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25129 (un->un_state == SD_STATE_OFFLINE)) { 25130 return (ENXIO); 25131 } 25132 25133 buffer = kmem_zalloc(4, KM_SLEEP); 25134 bzero(cdb, CDB_GROUP1); 25135 cdb[0] = SCMD_READ_TOC; 25136 /* 25137 * Specifying a track number of 0x00 in the READ TOC command indicates 25138 * that the TOC header should be returned 25139 */ 25140 cdb[6] = 0x00; 25141 /* 25142 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 25143 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 25144 */ 25145 cdb[8] = 0x04; 25146 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25147 com->uscsi_cdb = cdb; 25148 com->uscsi_cdblen = CDB_GROUP1; 25149 com->uscsi_bufaddr = buffer; 25150 com->uscsi_buflen = 0x04; 25151 com->uscsi_timeout = 300; 25152 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25153 25154 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25155 SD_PATH_STANDARD); 25156 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 25157 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 25158 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 25159 } else { 25160 hdr->cdth_trk0 = buffer[2]; 25161 hdr->cdth_trk1 = buffer[3]; 25162 } 25163 kmem_free(buffer, 4); 25164 kmem_free(com, sizeof (*com)); 25165 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 25166 return (EFAULT); 25167 } 25168 return (rval); 25169 } 25170 25171 25172 /* 25173 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 25174 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 25175 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 25176 * digital audio and extended architecture digital audio. These modes are 25177 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 25178 * MMC specs. 25179 * 25180 * In addition to support for the various data formats these routines also 25181 * include support for devices that implement only the direct access READ 25182 * commands (0x08, 0x28), devices that implement the READ_CD commands 25183 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 25184 * READ CDXA commands (0xD8, 0xDB) 25185 */ 25186 25187 /* 25188 * Function: sr_read_mode1() 25189 * 25190 * Description: This routine is the driver entry point for handling CD-ROM 25191 * ioctl read mode1 requests (CDROMREADMODE1). 25192 * 25193 * Arguments: dev - the device 'dev_t' 25194 * data - pointer to user provided cd read structure specifying 25195 * the lba buffer address and length. 25196 * flag - this argument is a pass through to ddi_copyxxx() 25197 * directly from the mode argument of ioctl(). 25198 * 25199 * Return Code: the code returned by sd_send_scsi_cmd() 25200 * EFAULT if ddi_copyxxx() fails 25201 * ENXIO if fail ddi_get_soft_state 25202 * EINVAL if data pointer is NULL 25203 */ 25204 25205 static int 25206 sr_read_mode1(dev_t dev, caddr_t data, int flag) 25207 { 25208 struct sd_lun *un; 25209 struct cdrom_read mode1_struct; 25210 struct cdrom_read *mode1 = &mode1_struct; 25211 int rval; 25212 #ifdef _MULTI_DATAMODEL 25213 /* To support ILP32 applications in an LP64 world */ 25214 struct cdrom_read32 cdrom_read32; 25215 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25216 #endif /* _MULTI_DATAMODEL */ 25217 25218 if (data == NULL) { 25219 return (EINVAL); 25220 } 25221 25222 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25223 (un->un_state == SD_STATE_OFFLINE)) { 25224 return (ENXIO); 25225 } 25226 25227 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25228 "sd_read_mode1: entry: un:0x%p\n", un); 25229 25230 #ifdef _MULTI_DATAMODEL 25231 switch (ddi_model_convert_from(flag & FMODELS)) { 25232 case DDI_MODEL_ILP32: 25233 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25234 return (EFAULT); 25235 } 25236 /* Convert the ILP32 uscsi data from the application to LP64 */ 25237 cdrom_read32tocdrom_read(cdrd32, mode1); 25238 break; 25239 case DDI_MODEL_NONE: 25240 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25241 return (EFAULT); 25242 } 25243 } 25244 #else /* ! _MULTI_DATAMODEL */ 25245 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25246 return (EFAULT); 25247 } 25248 #endif /* _MULTI_DATAMODEL */ 25249 25250 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 25251 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 25252 25253 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25254 "sd_read_mode1: exit: un:0x%p\n", un); 25255 25256 return (rval); 25257 } 25258 25259 25260 /* 25261 * Function: sr_read_cd_mode2() 25262 * 25263 * Description: This routine is the driver entry point for handling CD-ROM 25264 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25265 * support the READ CD (0xBE) command or the 1st generation 25266 * READ CD (0xD4) command. 25267 * 25268 * Arguments: dev - the device 'dev_t' 25269 * data - pointer to user provided cd read structure specifying 25270 * the lba buffer address and length. 25271 * flag - this argument is a pass through to ddi_copyxxx() 25272 * directly from the mode argument of ioctl(). 25273 * 25274 * Return Code: the code returned by sd_send_scsi_cmd() 25275 * EFAULT if ddi_copyxxx() fails 25276 * ENXIO if fail ddi_get_soft_state 25277 * EINVAL if data pointer is NULL 25278 */ 25279 25280 static int 25281 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 25282 { 25283 struct sd_lun *un; 25284 struct uscsi_cmd *com; 25285 struct cdrom_read mode2_struct; 25286 struct cdrom_read *mode2 = &mode2_struct; 25287 uchar_t cdb[CDB_GROUP5]; 25288 int nblocks; 25289 int rval; 25290 #ifdef _MULTI_DATAMODEL 25291 /* To support ILP32 applications in an LP64 world */ 25292 struct cdrom_read32 cdrom_read32; 25293 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25294 #endif /* _MULTI_DATAMODEL */ 25295 25296 if (data == NULL) { 25297 return (EINVAL); 25298 } 25299 25300 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25301 (un->un_state == SD_STATE_OFFLINE)) { 25302 return (ENXIO); 25303 } 25304 25305 #ifdef _MULTI_DATAMODEL 25306 switch (ddi_model_convert_from(flag & FMODELS)) { 25307 case DDI_MODEL_ILP32: 25308 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25309 return (EFAULT); 25310 } 25311 /* Convert the ILP32 uscsi data from the application to LP64 */ 25312 cdrom_read32tocdrom_read(cdrd32, mode2); 25313 break; 25314 case DDI_MODEL_NONE: 25315 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25316 return (EFAULT); 25317 } 25318 break; 25319 } 25320 25321 #else /* ! _MULTI_DATAMODEL */ 25322 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25323 return (EFAULT); 25324 } 25325 #endif /* _MULTI_DATAMODEL */ 25326 25327 bzero(cdb, sizeof (cdb)); 25328 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 25329 /* Read command supported by 1st generation atapi drives */ 25330 cdb[0] = SCMD_READ_CDD4; 25331 } else { 25332 /* Universal CD Access Command */ 25333 cdb[0] = SCMD_READ_CD; 25334 } 25335 25336 /* 25337 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 25338 */ 25339 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 25340 25341 /* set the start address */ 25342 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 25343 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 25344 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25345 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 25346 25347 /* set the transfer length */ 25348 nblocks = mode2->cdread_buflen / 2336; 25349 cdb[6] = (uchar_t)(nblocks >> 16); 25350 cdb[7] = (uchar_t)(nblocks >> 8); 25351 cdb[8] = (uchar_t)nblocks; 25352 25353 /* set the filter bits */ 25354 cdb[9] = CDROM_READ_CD_USERDATA; 25355 25356 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25357 com->uscsi_cdb = (caddr_t)cdb; 25358 com->uscsi_cdblen = sizeof (cdb); 25359 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25360 com->uscsi_buflen = mode2->cdread_buflen; 25361 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25362 25363 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25364 SD_PATH_STANDARD); 25365 kmem_free(com, sizeof (*com)); 25366 return (rval); 25367 } 25368 25369 25370 /* 25371 * Function: sr_read_mode2() 25372 * 25373 * Description: This routine is the driver entry point for handling CD-ROM 25374 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25375 * do not support the READ CD (0xBE) command. 25376 * 25377 * Arguments: dev - the device 'dev_t' 25378 * data - pointer to user provided cd read structure specifying 25379 * the lba buffer address and length. 25380 * flag - this argument is a pass through to ddi_copyxxx() 25381 * directly from the mode argument of ioctl(). 25382 * 25383 * Return Code: the code returned by sd_send_scsi_cmd() 25384 * EFAULT if ddi_copyxxx() fails 25385 * ENXIO if fail ddi_get_soft_state 25386 * EINVAL if data pointer is NULL 25387 * EIO if fail to reset block size 25388 * EAGAIN if commands are in progress in the driver 25389 */ 25390 25391 static int 25392 sr_read_mode2(dev_t dev, caddr_t data, int flag) 25393 { 25394 struct sd_lun *un; 25395 struct cdrom_read mode2_struct; 25396 struct cdrom_read *mode2 = &mode2_struct; 25397 int rval; 25398 uint32_t restore_blksize; 25399 struct uscsi_cmd *com; 25400 uchar_t cdb[CDB_GROUP0]; 25401 int nblocks; 25402 25403 #ifdef _MULTI_DATAMODEL 25404 /* To support ILP32 applications in an LP64 world */ 25405 struct cdrom_read32 cdrom_read32; 25406 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25407 #endif /* _MULTI_DATAMODEL */ 25408 25409 if (data == NULL) { 25410 return (EINVAL); 25411 } 25412 25413 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25414 (un->un_state == SD_STATE_OFFLINE)) { 25415 return (ENXIO); 25416 } 25417 25418 /* 25419 * Because this routine will update the device and driver block size 25420 * being used we want to make sure there are no commands in progress. 25421 * If commands are in progress the user will have to try again. 25422 * 25423 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 25424 * in sdioctl to protect commands from sdioctl through to the top of 25425 * sd_uscsi_strategy. See sdioctl for details. 25426 */ 25427 mutex_enter(SD_MUTEX(un)); 25428 if (un->un_ncmds_in_driver != 1) { 25429 mutex_exit(SD_MUTEX(un)); 25430 return (EAGAIN); 25431 } 25432 mutex_exit(SD_MUTEX(un)); 25433 25434 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25435 "sd_read_mode2: entry: un:0x%p\n", un); 25436 25437 #ifdef _MULTI_DATAMODEL 25438 switch (ddi_model_convert_from(flag & FMODELS)) { 25439 case DDI_MODEL_ILP32: 25440 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25441 return (EFAULT); 25442 } 25443 /* Convert the ILP32 uscsi data from the application to LP64 */ 25444 cdrom_read32tocdrom_read(cdrd32, mode2); 25445 break; 25446 case DDI_MODEL_NONE: 25447 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25448 return (EFAULT); 25449 } 25450 break; 25451 } 25452 #else /* ! _MULTI_DATAMODEL */ 25453 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 25454 return (EFAULT); 25455 } 25456 #endif /* _MULTI_DATAMODEL */ 25457 25458 /* Store the current target block size for restoration later */ 25459 restore_blksize = un->un_tgt_blocksize; 25460 25461 /* Change the device and soft state target block size to 2336 */ 25462 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 25463 rval = EIO; 25464 goto done; 25465 } 25466 25467 25468 bzero(cdb, sizeof (cdb)); 25469 25470 /* set READ operation */ 25471 cdb[0] = SCMD_READ; 25472 25473 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 25474 mode2->cdread_lba >>= 2; 25475 25476 /* set the start address */ 25477 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 25478 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25479 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 25480 25481 /* set the transfer length */ 25482 nblocks = mode2->cdread_buflen / 2336; 25483 cdb[4] = (uchar_t)nblocks & 0xFF; 25484 25485 /* build command */ 25486 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25487 com->uscsi_cdb = (caddr_t)cdb; 25488 com->uscsi_cdblen = sizeof (cdb); 25489 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25490 com->uscsi_buflen = mode2->cdread_buflen; 25491 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25492 25493 /* 25494 * Issue SCSI command with user space address for read buffer. 25495 * 25496 * This sends the command through main channel in the driver. 25497 * 25498 * Since this is accessed via an IOCTL call, we go through the 25499 * standard path, so that if the device was powered down, then 25500 * it would be 'awakened' to handle the command. 25501 */ 25502 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25503 SD_PATH_STANDARD); 25504 25505 kmem_free(com, sizeof (*com)); 25506 25507 /* Restore the device and soft state target block size */ 25508 if (sr_sector_mode(dev, restore_blksize) != 0) { 25509 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25510 "can't do switch back to mode 1\n"); 25511 /* 25512 * If sd_send_scsi_READ succeeded we still need to report 25513 * an error because we failed to reset the block size 25514 */ 25515 if (rval == 0) { 25516 rval = EIO; 25517 } 25518 } 25519 25520 done: 25521 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25522 "sd_read_mode2: exit: un:0x%p\n", un); 25523 25524 return (rval); 25525 } 25526 25527 25528 /* 25529 * Function: sr_sector_mode() 25530 * 25531 * Description: This utility function is used by sr_read_mode2 to set the target 25532 * block size based on the user specified size. This is a legacy 25533 * implementation based upon a vendor specific mode page 25534 * 25535 * Arguments: dev - the device 'dev_t' 25536 * data - flag indicating if block size is being set to 2336 or 25537 * 512. 25538 * 25539 * Return Code: the code returned by sd_send_scsi_cmd() 25540 * EFAULT if ddi_copyxxx() fails 25541 * ENXIO if fail ddi_get_soft_state 25542 * EINVAL if data pointer is NULL 25543 */ 25544 25545 static int 25546 sr_sector_mode(dev_t dev, uint32_t blksize) 25547 { 25548 struct sd_lun *un; 25549 uchar_t *sense; 25550 uchar_t *select; 25551 int rval; 25552 25553 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25554 (un->un_state == SD_STATE_OFFLINE)) { 25555 return (ENXIO); 25556 } 25557 25558 sense = kmem_zalloc(20, KM_SLEEP); 25559 25560 /* Note: This is a vendor specific mode page (0x81) */ 25561 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 25562 SD_PATH_STANDARD)) != 0) { 25563 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25564 "sr_sector_mode: Mode Sense failed\n"); 25565 kmem_free(sense, 20); 25566 return (rval); 25567 } 25568 select = kmem_zalloc(20, KM_SLEEP); 25569 select[3] = 0x08; 25570 select[10] = ((blksize >> 8) & 0xff); 25571 select[11] = (blksize & 0xff); 25572 select[12] = 0x01; 25573 select[13] = 0x06; 25574 select[14] = sense[14]; 25575 select[15] = sense[15]; 25576 if (blksize == SD_MODE2_BLKSIZE) { 25577 select[14] |= 0x01; 25578 } 25579 25580 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 25581 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 25582 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25583 "sr_sector_mode: Mode Select failed\n"); 25584 } else { 25585 /* 25586 * Only update the softstate block size if we successfully 25587 * changed the device block mode. 25588 */ 25589 mutex_enter(SD_MUTEX(un)); 25590 sd_update_block_info(un, blksize, 0); 25591 mutex_exit(SD_MUTEX(un)); 25592 } 25593 kmem_free(sense, 20); 25594 kmem_free(select, 20); 25595 return (rval); 25596 } 25597 25598 25599 /* 25600 * Function: sr_read_cdda() 25601 * 25602 * Description: This routine is the driver entry point for handling CD-ROM 25603 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 25604 * the target supports CDDA these requests are handled via a vendor 25605 * specific command (0xD8) If the target does not support CDDA 25606 * these requests are handled via the READ CD command (0xBE). 25607 * 25608 * Arguments: dev - the device 'dev_t' 25609 * data - pointer to user provided CD-DA structure specifying 25610 * the track starting address, transfer length, and 25611 * subcode options. 25612 * flag - this argument is a pass through to ddi_copyxxx() 25613 * directly from the mode argument of ioctl(). 25614 * 25615 * Return Code: the code returned by sd_send_scsi_cmd() 25616 * EFAULT if ddi_copyxxx() fails 25617 * ENXIO if fail ddi_get_soft_state 25618 * EINVAL if invalid arguments are provided 25619 * ENOTTY 25620 */ 25621 25622 static int 25623 sr_read_cdda(dev_t dev, caddr_t data, int flag) 25624 { 25625 struct sd_lun *un; 25626 struct uscsi_cmd *com; 25627 struct cdrom_cdda *cdda; 25628 int rval; 25629 size_t buflen; 25630 char cdb[CDB_GROUP5]; 25631 25632 #ifdef _MULTI_DATAMODEL 25633 /* To support ILP32 applications in an LP64 world */ 25634 struct cdrom_cdda32 cdrom_cdda32; 25635 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 25636 #endif /* _MULTI_DATAMODEL */ 25637 25638 if (data == NULL) { 25639 return (EINVAL); 25640 } 25641 25642 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25643 return (ENXIO); 25644 } 25645 25646 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 25647 25648 #ifdef _MULTI_DATAMODEL 25649 switch (ddi_model_convert_from(flag & FMODELS)) { 25650 case DDI_MODEL_ILP32: 25651 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 25652 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25653 "sr_read_cdda: ddi_copyin Failed\n"); 25654 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25655 return (EFAULT); 25656 } 25657 /* Convert the ILP32 uscsi data from the application to LP64 */ 25658 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 25659 break; 25660 case DDI_MODEL_NONE: 25661 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25662 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25663 "sr_read_cdda: ddi_copyin Failed\n"); 25664 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25665 return (EFAULT); 25666 } 25667 break; 25668 } 25669 #else /* ! _MULTI_DATAMODEL */ 25670 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25671 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25672 "sr_read_cdda: ddi_copyin Failed\n"); 25673 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25674 return (EFAULT); 25675 } 25676 #endif /* _MULTI_DATAMODEL */ 25677 25678 /* 25679 * Since MMC-2 expects max 3 bytes for length, check if the 25680 * length input is greater than 3 bytes 25681 */ 25682 if ((cdda->cdda_length & 0xFF000000) != 0) { 25683 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 25684 "cdrom transfer length too large: %d (limit %d)\n", 25685 cdda->cdda_length, 0xFFFFFF); 25686 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25687 return (EINVAL); 25688 } 25689 25690 switch (cdda->cdda_subcode) { 25691 case CDROM_DA_NO_SUBCODE: 25692 buflen = CDROM_BLK_2352 * cdda->cdda_length; 25693 break; 25694 case CDROM_DA_SUBQ: 25695 buflen = CDROM_BLK_2368 * cdda->cdda_length; 25696 break; 25697 case CDROM_DA_ALL_SUBCODE: 25698 buflen = CDROM_BLK_2448 * cdda->cdda_length; 25699 break; 25700 case CDROM_DA_SUBCODE_ONLY: 25701 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 25702 break; 25703 default: 25704 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25705 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 25706 cdda->cdda_subcode); 25707 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25708 return (EINVAL); 25709 } 25710 25711 /* Build and send the command */ 25712 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25713 bzero(cdb, CDB_GROUP5); 25714 25715 if (un->un_f_cfg_cdda == TRUE) { 25716 cdb[0] = (char)SCMD_READ_CD; 25717 cdb[1] = 0x04; 25718 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25719 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25720 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25721 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25722 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25723 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25724 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 25725 cdb[9] = 0x10; 25726 switch (cdda->cdda_subcode) { 25727 case CDROM_DA_NO_SUBCODE : 25728 cdb[10] = 0x0; 25729 break; 25730 case CDROM_DA_SUBQ : 25731 cdb[10] = 0x2; 25732 break; 25733 case CDROM_DA_ALL_SUBCODE : 25734 cdb[10] = 0x1; 25735 break; 25736 case CDROM_DA_SUBCODE_ONLY : 25737 /* FALLTHROUGH */ 25738 default : 25739 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25740 kmem_free(com, sizeof (*com)); 25741 return (ENOTTY); 25742 } 25743 } else { 25744 cdb[0] = (char)SCMD_READ_CDDA; 25745 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25746 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25747 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25748 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25749 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 25750 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25751 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25752 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 25753 cdb[10] = cdda->cdda_subcode; 25754 } 25755 25756 com->uscsi_cdb = cdb; 25757 com->uscsi_cdblen = CDB_GROUP5; 25758 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 25759 com->uscsi_buflen = buflen; 25760 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25761 25762 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25763 SD_PATH_STANDARD); 25764 25765 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25766 kmem_free(com, sizeof (*com)); 25767 return (rval); 25768 } 25769 25770 25771 /* 25772 * Function: sr_read_cdxa() 25773 * 25774 * Description: This routine is the driver entry point for handling CD-ROM 25775 * ioctl requests to return CD-XA (Extended Architecture) data. 25776 * (CDROMCDXA). 25777 * 25778 * Arguments: dev - the device 'dev_t' 25779 * data - pointer to user provided CD-XA structure specifying 25780 * the data starting address, transfer length, and format 25781 * flag - this argument is a pass through to ddi_copyxxx() 25782 * directly from the mode argument of ioctl(). 25783 * 25784 * Return Code: the code returned by sd_send_scsi_cmd() 25785 * EFAULT if ddi_copyxxx() fails 25786 * ENXIO if fail ddi_get_soft_state 25787 * EINVAL if data pointer is NULL 25788 */ 25789 25790 static int 25791 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 25792 { 25793 struct sd_lun *un; 25794 struct uscsi_cmd *com; 25795 struct cdrom_cdxa *cdxa; 25796 int rval; 25797 size_t buflen; 25798 char cdb[CDB_GROUP5]; 25799 uchar_t read_flags; 25800 25801 #ifdef _MULTI_DATAMODEL 25802 /* To support ILP32 applications in an LP64 world */ 25803 struct cdrom_cdxa32 cdrom_cdxa32; 25804 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 25805 #endif /* _MULTI_DATAMODEL */ 25806 25807 if (data == NULL) { 25808 return (EINVAL); 25809 } 25810 25811 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25812 return (ENXIO); 25813 } 25814 25815 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 25816 25817 #ifdef _MULTI_DATAMODEL 25818 switch (ddi_model_convert_from(flag & FMODELS)) { 25819 case DDI_MODEL_ILP32: 25820 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 25821 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25822 return (EFAULT); 25823 } 25824 /* 25825 * Convert the ILP32 uscsi data from the 25826 * application to LP64 for internal use. 25827 */ 25828 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 25829 break; 25830 case DDI_MODEL_NONE: 25831 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25832 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25833 return (EFAULT); 25834 } 25835 break; 25836 } 25837 #else /* ! _MULTI_DATAMODEL */ 25838 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25839 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25840 return (EFAULT); 25841 } 25842 #endif /* _MULTI_DATAMODEL */ 25843 25844 /* 25845 * Since MMC-2 expects max 3 bytes for length, check if the 25846 * length input is greater than 3 bytes 25847 */ 25848 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 25849 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 25850 "cdrom transfer length too large: %d (limit %d)\n", 25851 cdxa->cdxa_length, 0xFFFFFF); 25852 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25853 return (EINVAL); 25854 } 25855 25856 switch (cdxa->cdxa_format) { 25857 case CDROM_XA_DATA: 25858 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 25859 read_flags = 0x10; 25860 break; 25861 case CDROM_XA_SECTOR_DATA: 25862 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 25863 read_flags = 0xf8; 25864 break; 25865 case CDROM_XA_DATA_W_ERROR: 25866 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 25867 read_flags = 0xfc; 25868 break; 25869 default: 25870 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25871 "sr_read_cdxa: Format '0x%x' Not Supported\n", 25872 cdxa->cdxa_format); 25873 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25874 return (EINVAL); 25875 } 25876 25877 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25878 bzero(cdb, CDB_GROUP5); 25879 if (un->un_f_mmc_cap == TRUE) { 25880 cdb[0] = (char)SCMD_READ_CD; 25881 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25882 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25883 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25884 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25885 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25886 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25887 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 25888 cdb[9] = (char)read_flags; 25889 } else { 25890 /* 25891 * Note: A vendor specific command (0xDB) is being used her to 25892 * request a read of all subcodes. 25893 */ 25894 cdb[0] = (char)SCMD_READ_CDXA; 25895 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25896 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25897 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25898 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25899 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 25900 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25901 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25902 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 25903 cdb[10] = cdxa->cdxa_format; 25904 } 25905 com->uscsi_cdb = cdb; 25906 com->uscsi_cdblen = CDB_GROUP5; 25907 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 25908 com->uscsi_buflen = buflen; 25909 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25910 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25911 SD_PATH_STANDARD); 25912 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25913 kmem_free(com, sizeof (*com)); 25914 return (rval); 25915 } 25916 25917 25918 /* 25919 * Function: sr_eject() 25920 * 25921 * Description: This routine is the driver entry point for handling CD-ROM 25922 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 25923 * 25924 * Arguments: dev - the device 'dev_t' 25925 * 25926 * Return Code: the code returned by sd_send_scsi_cmd() 25927 */ 25928 25929 static int 25930 sr_eject(dev_t dev) 25931 { 25932 struct sd_lun *un; 25933 int rval; 25934 25935 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25936 (un->un_state == SD_STATE_OFFLINE)) { 25937 return (ENXIO); 25938 } 25939 25940 /* 25941 * To prevent race conditions with the eject 25942 * command, keep track of an eject command as 25943 * it progresses. If we are already handling 25944 * an eject command in the driver for the given 25945 * unit and another request to eject is received 25946 * immediately return EAGAIN so we don't lose 25947 * the command if the current eject command fails. 25948 */ 25949 mutex_enter(SD_MUTEX(un)); 25950 if (un->un_f_ejecting == TRUE) { 25951 mutex_exit(SD_MUTEX(un)); 25952 return (EAGAIN); 25953 } 25954 un->un_f_ejecting = TRUE; 25955 mutex_exit(SD_MUTEX(un)); 25956 25957 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 25958 SD_PATH_STANDARD)) != 0) { 25959 mutex_enter(SD_MUTEX(un)); 25960 un->un_f_ejecting = FALSE; 25961 mutex_exit(SD_MUTEX(un)); 25962 return (rval); 25963 } 25964 25965 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 25966 SD_PATH_STANDARD); 25967 25968 if (rval == 0) { 25969 mutex_enter(SD_MUTEX(un)); 25970 sr_ejected(un); 25971 un->un_mediastate = DKIO_EJECTED; 25972 un->un_f_ejecting = FALSE; 25973 cv_broadcast(&un->un_state_cv); 25974 mutex_exit(SD_MUTEX(un)); 25975 } else { 25976 mutex_enter(SD_MUTEX(un)); 25977 un->un_f_ejecting = FALSE; 25978 mutex_exit(SD_MUTEX(un)); 25979 } 25980 return (rval); 25981 } 25982 25983 25984 /* 25985 * Function: sr_ejected() 25986 * 25987 * Description: This routine updates the soft state structure to invalidate the 25988 * geometry information after the media has been ejected or a 25989 * media eject has been detected. 25990 * 25991 * Arguments: un - driver soft state (unit) structure 25992 */ 25993 25994 static void 25995 sr_ejected(struct sd_lun *un) 25996 { 25997 struct sd_errstats *stp; 25998 25999 ASSERT(un != NULL); 26000 ASSERT(mutex_owned(SD_MUTEX(un))); 26001 26002 un->un_f_blockcount_is_valid = FALSE; 26003 un->un_f_tgt_blocksize_is_valid = FALSE; 26004 mutex_exit(SD_MUTEX(un)); 26005 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 26006 mutex_enter(SD_MUTEX(un)); 26007 26008 if (un->un_errstats != NULL) { 26009 stp = (struct sd_errstats *)un->un_errstats->ks_data; 26010 stp->sd_capacity.value.ui64 = 0; 26011 } 26012 } 26013 26014 26015 /* 26016 * Function: sr_check_wp() 26017 * 26018 * Description: This routine checks the write protection of a removable 26019 * media disk and hotpluggable devices via the write protect bit of 26020 * the Mode Page Header device specific field. Some devices choke 26021 * on unsupported mode page. In order to workaround this issue, 26022 * this routine has been implemented to use 0x3f mode page(request 26023 * for all pages) for all device types. 26024 * 26025 * Arguments: dev - the device 'dev_t' 26026 * 26027 * Return Code: int indicating if the device is write protected (1) or not (0) 26028 * 26029 * Context: Kernel thread. 26030 * 26031 */ 26032 26033 static int 26034 sr_check_wp(dev_t dev) 26035 { 26036 struct sd_lun *un; 26037 uchar_t device_specific; 26038 uchar_t *sense; 26039 int hdrlen; 26040 int rval = FALSE; 26041 26042 /* 26043 * Note: The return codes for this routine should be reworked to 26044 * properly handle the case of a NULL softstate. 26045 */ 26046 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26047 return (FALSE); 26048 } 26049 26050 if (un->un_f_cfg_is_atapi == TRUE) { 26051 /* 26052 * The mode page contents are not required; set the allocation 26053 * length for the mode page header only 26054 */ 26055 hdrlen = MODE_HEADER_LENGTH_GRP2; 26056 sense = kmem_zalloc(hdrlen, KM_SLEEP); 26057 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 26058 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 26059 goto err_exit; 26060 device_specific = 26061 ((struct mode_header_grp2 *)sense)->device_specific; 26062 } else { 26063 hdrlen = MODE_HEADER_LENGTH; 26064 sense = kmem_zalloc(hdrlen, KM_SLEEP); 26065 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 26066 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 26067 goto err_exit; 26068 device_specific = 26069 ((struct mode_header *)sense)->device_specific; 26070 } 26071 26072 /* 26073 * Write protect mode sense failed; not all disks 26074 * understand this query. Return FALSE assuming that 26075 * these devices are not writable. 26076 */ 26077 if (device_specific & WRITE_PROTECT) { 26078 rval = TRUE; 26079 } 26080 26081 err_exit: 26082 kmem_free(sense, hdrlen); 26083 return (rval); 26084 } 26085 26086 /* 26087 * Function: sr_volume_ctrl() 26088 * 26089 * Description: This routine is the driver entry point for handling CD-ROM 26090 * audio output volume ioctl requests. (CDROMVOLCTRL) 26091 * 26092 * Arguments: dev - the device 'dev_t' 26093 * data - pointer to user audio volume control structure 26094 * flag - this argument is a pass through to ddi_copyxxx() 26095 * directly from the mode argument of ioctl(). 26096 * 26097 * Return Code: the code returned by sd_send_scsi_cmd() 26098 * EFAULT if ddi_copyxxx() fails 26099 * ENXIO if fail ddi_get_soft_state 26100 * EINVAL if data pointer is NULL 26101 * 26102 */ 26103 26104 static int 26105 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 26106 { 26107 struct sd_lun *un; 26108 struct cdrom_volctrl volume; 26109 struct cdrom_volctrl *vol = &volume; 26110 uchar_t *sense_page; 26111 uchar_t *select_page; 26112 uchar_t *sense; 26113 uchar_t *select; 26114 int sense_buflen; 26115 int select_buflen; 26116 int rval; 26117 26118 if (data == NULL) { 26119 return (EINVAL); 26120 } 26121 26122 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26123 (un->un_state == SD_STATE_OFFLINE)) { 26124 return (ENXIO); 26125 } 26126 26127 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 26128 return (EFAULT); 26129 } 26130 26131 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 26132 struct mode_header_grp2 *sense_mhp; 26133 struct mode_header_grp2 *select_mhp; 26134 int bd_len; 26135 26136 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 26137 select_buflen = MODE_HEADER_LENGTH_GRP2 + 26138 MODEPAGE_AUDIO_CTRL_LEN; 26139 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 26140 select = kmem_zalloc(select_buflen, KM_SLEEP); 26141 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 26142 sense_buflen, MODEPAGE_AUDIO_CTRL, 26143 SD_PATH_STANDARD)) != 0) { 26144 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 26145 "sr_volume_ctrl: Mode Sense Failed\n"); 26146 kmem_free(sense, sense_buflen); 26147 kmem_free(select, select_buflen); 26148 return (rval); 26149 } 26150 sense_mhp = (struct mode_header_grp2 *)sense; 26151 select_mhp = (struct mode_header_grp2 *)select; 26152 bd_len = (sense_mhp->bdesc_length_hi << 8) | 26153 sense_mhp->bdesc_length_lo; 26154 if (bd_len > MODE_BLK_DESC_LENGTH) { 26155 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26156 "sr_volume_ctrl: Mode Sense returned invalid " 26157 "block descriptor length\n"); 26158 kmem_free(sense, sense_buflen); 26159 kmem_free(select, select_buflen); 26160 return (EIO); 26161 } 26162 sense_page = (uchar_t *) 26163 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 26164 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 26165 select_mhp->length_msb = 0; 26166 select_mhp->length_lsb = 0; 26167 select_mhp->bdesc_length_hi = 0; 26168 select_mhp->bdesc_length_lo = 0; 26169 } else { 26170 struct mode_header *sense_mhp, *select_mhp; 26171 26172 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 26173 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 26174 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 26175 select = kmem_zalloc(select_buflen, KM_SLEEP); 26176 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 26177 sense_buflen, MODEPAGE_AUDIO_CTRL, 26178 SD_PATH_STANDARD)) != 0) { 26179 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26180 "sr_volume_ctrl: Mode Sense Failed\n"); 26181 kmem_free(sense, sense_buflen); 26182 kmem_free(select, select_buflen); 26183 return (rval); 26184 } 26185 sense_mhp = (struct mode_header *)sense; 26186 select_mhp = (struct mode_header *)select; 26187 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 26188 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26189 "sr_volume_ctrl: Mode Sense returned invalid " 26190 "block descriptor length\n"); 26191 kmem_free(sense, sense_buflen); 26192 kmem_free(select, select_buflen); 26193 return (EIO); 26194 } 26195 sense_page = (uchar_t *) 26196 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 26197 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 26198 select_mhp->length = 0; 26199 select_mhp->bdesc_length = 0; 26200 } 26201 /* 26202 * Note: An audio control data structure could be created and overlayed 26203 * on the following in place of the array indexing method implemented. 26204 */ 26205 26206 /* Build the select data for the user volume data */ 26207 select_page[0] = MODEPAGE_AUDIO_CTRL; 26208 select_page[1] = 0xE; 26209 /* Set the immediate bit */ 26210 select_page[2] = 0x04; 26211 /* Zero out reserved fields */ 26212 select_page[3] = 0x00; 26213 select_page[4] = 0x00; 26214 /* Return sense data for fields not to be modified */ 26215 select_page[5] = sense_page[5]; 26216 select_page[6] = sense_page[6]; 26217 select_page[7] = sense_page[7]; 26218 /* Set the user specified volume levels for channel 0 and 1 */ 26219 select_page[8] = 0x01; 26220 select_page[9] = vol->channel0; 26221 select_page[10] = 0x02; 26222 select_page[11] = vol->channel1; 26223 /* Channel 2 and 3 are currently unsupported so return the sense data */ 26224 select_page[12] = sense_page[12]; 26225 select_page[13] = sense_page[13]; 26226 select_page[14] = sense_page[14]; 26227 select_page[15] = sense_page[15]; 26228 26229 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 26230 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 26231 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26232 } else { 26233 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 26234 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26235 } 26236 26237 kmem_free(sense, sense_buflen); 26238 kmem_free(select, select_buflen); 26239 return (rval); 26240 } 26241 26242 26243 /* 26244 * Function: sr_read_sony_session_offset() 26245 * 26246 * Description: This routine is the driver entry point for handling CD-ROM 26247 * ioctl requests for session offset information. (CDROMREADOFFSET) 26248 * The address of the first track in the last session of a 26249 * multi-session CD-ROM is returned 26250 * 26251 * Note: This routine uses a vendor specific key value in the 26252 * command control field without implementing any vendor check here 26253 * or in the ioctl routine. 26254 * 26255 * Arguments: dev - the device 'dev_t' 26256 * data - pointer to an int to hold the requested address 26257 * flag - this argument is a pass through to ddi_copyxxx() 26258 * directly from the mode argument of ioctl(). 26259 * 26260 * Return Code: the code returned by sd_send_scsi_cmd() 26261 * EFAULT if ddi_copyxxx() fails 26262 * ENXIO if fail ddi_get_soft_state 26263 * EINVAL if data pointer is NULL 26264 */ 26265 26266 static int 26267 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 26268 { 26269 struct sd_lun *un; 26270 struct uscsi_cmd *com; 26271 caddr_t buffer; 26272 char cdb[CDB_GROUP1]; 26273 int session_offset = 0; 26274 int rval; 26275 26276 if (data == NULL) { 26277 return (EINVAL); 26278 } 26279 26280 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26281 (un->un_state == SD_STATE_OFFLINE)) { 26282 return (ENXIO); 26283 } 26284 26285 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 26286 bzero(cdb, CDB_GROUP1); 26287 cdb[0] = SCMD_READ_TOC; 26288 /* 26289 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 26290 * (4 byte TOC response header + 8 byte response data) 26291 */ 26292 cdb[8] = SONY_SESSION_OFFSET_LEN; 26293 /* Byte 9 is the control byte. A vendor specific value is used */ 26294 cdb[9] = SONY_SESSION_OFFSET_KEY; 26295 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26296 com->uscsi_cdb = cdb; 26297 com->uscsi_cdblen = CDB_GROUP1; 26298 com->uscsi_bufaddr = buffer; 26299 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 26300 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26301 26302 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26303 SD_PATH_STANDARD); 26304 if (rval != 0) { 26305 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26306 kmem_free(com, sizeof (*com)); 26307 return (rval); 26308 } 26309 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 26310 session_offset = 26311 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 26312 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 26313 /* 26314 * Offset returned offset in current lbasize block's. Convert to 26315 * 2k block's to return to the user 26316 */ 26317 if (un->un_tgt_blocksize == CDROM_BLK_512) { 26318 session_offset >>= 2; 26319 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 26320 session_offset >>= 1; 26321 } 26322 } 26323 26324 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 26325 rval = EFAULT; 26326 } 26327 26328 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26329 kmem_free(com, sizeof (*com)); 26330 return (rval); 26331 } 26332 26333 26334 /* 26335 * Function: sd_wm_cache_constructor() 26336 * 26337 * Description: Cache Constructor for the wmap cache for the read/modify/write 26338 * devices. 26339 * 26340 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26341 * un - sd_lun structure for the device. 26342 * flag - the km flags passed to constructor 26343 * 26344 * Return Code: 0 on success. 26345 * -1 on failure. 26346 */ 26347 26348 /*ARGSUSED*/ 26349 static int 26350 sd_wm_cache_constructor(void *wm, void *un, int flags) 26351 { 26352 bzero(wm, sizeof (struct sd_w_map)); 26353 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 26354 return (0); 26355 } 26356 26357 26358 /* 26359 * Function: sd_wm_cache_destructor() 26360 * 26361 * Description: Cache destructor for the wmap cache for the read/modify/write 26362 * devices. 26363 * 26364 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26365 * un - sd_lun structure for the device. 26366 */ 26367 /*ARGSUSED*/ 26368 static void 26369 sd_wm_cache_destructor(void *wm, void *un) 26370 { 26371 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 26372 } 26373 26374 26375 /* 26376 * Function: sd_range_lock() 26377 * 26378 * Description: Lock the range of blocks specified as parameter to ensure 26379 * that read, modify write is atomic and no other i/o writes 26380 * to the same location. The range is specified in terms 26381 * of start and end blocks. Block numbers are the actual 26382 * media block numbers and not system. 26383 * 26384 * Arguments: un - sd_lun structure for the device. 26385 * startb - The starting block number 26386 * endb - The end block number 26387 * typ - type of i/o - simple/read_modify_write 26388 * 26389 * Return Code: wm - pointer to the wmap structure. 26390 * 26391 * Context: This routine can sleep. 26392 */ 26393 26394 static struct sd_w_map * 26395 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 26396 { 26397 struct sd_w_map *wmp = NULL; 26398 struct sd_w_map *sl_wmp = NULL; 26399 struct sd_w_map *tmp_wmp; 26400 wm_state state = SD_WM_CHK_LIST; 26401 26402 26403 ASSERT(un != NULL); 26404 ASSERT(!mutex_owned(SD_MUTEX(un))); 26405 26406 mutex_enter(SD_MUTEX(un)); 26407 26408 while (state != SD_WM_DONE) { 26409 26410 switch (state) { 26411 case SD_WM_CHK_LIST: 26412 /* 26413 * This is the starting state. Check the wmap list 26414 * to see if the range is currently available. 26415 */ 26416 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 26417 /* 26418 * If this is a simple write and no rmw 26419 * i/o is pending then try to lock the 26420 * range as the range should be available. 26421 */ 26422 state = SD_WM_LOCK_RANGE; 26423 } else { 26424 tmp_wmp = sd_get_range(un, startb, endb); 26425 if (tmp_wmp != NULL) { 26426 if ((wmp != NULL) && ONLIST(un, wmp)) { 26427 /* 26428 * Should not keep onlist wmps 26429 * while waiting this macro 26430 * will also do wmp = NULL; 26431 */ 26432 FREE_ONLIST_WMAP(un, wmp); 26433 } 26434 /* 26435 * sl_wmp is the wmap on which wait 26436 * is done, since the tmp_wmp points 26437 * to the inuse wmap, set sl_wmp to 26438 * tmp_wmp and change the state to sleep 26439 */ 26440 sl_wmp = tmp_wmp; 26441 state = SD_WM_WAIT_MAP; 26442 } else { 26443 state = SD_WM_LOCK_RANGE; 26444 } 26445 26446 } 26447 break; 26448 26449 case SD_WM_LOCK_RANGE: 26450 ASSERT(un->un_wm_cache); 26451 /* 26452 * The range need to be locked, try to get a wmap. 26453 * First attempt it with NO_SLEEP, want to avoid a sleep 26454 * if possible as we will have to release the sd mutex 26455 * if we have to sleep. 26456 */ 26457 if (wmp == NULL) 26458 wmp = kmem_cache_alloc(un->un_wm_cache, 26459 KM_NOSLEEP); 26460 if (wmp == NULL) { 26461 mutex_exit(SD_MUTEX(un)); 26462 _NOTE(DATA_READABLE_WITHOUT_LOCK 26463 (sd_lun::un_wm_cache)) 26464 wmp = kmem_cache_alloc(un->un_wm_cache, 26465 KM_SLEEP); 26466 mutex_enter(SD_MUTEX(un)); 26467 /* 26468 * we released the mutex so recheck and go to 26469 * check list state. 26470 */ 26471 state = SD_WM_CHK_LIST; 26472 } else { 26473 /* 26474 * We exit out of state machine since we 26475 * have the wmap. Do the housekeeping first. 26476 * place the wmap on the wmap list if it is not 26477 * on it already and then set the state to done. 26478 */ 26479 wmp->wm_start = startb; 26480 wmp->wm_end = endb; 26481 wmp->wm_flags = typ | SD_WM_BUSY; 26482 if (typ & SD_WTYPE_RMW) { 26483 un->un_rmw_count++; 26484 } 26485 /* 26486 * If not already on the list then link 26487 */ 26488 if (!ONLIST(un, wmp)) { 26489 wmp->wm_next = un->un_wm; 26490 wmp->wm_prev = NULL; 26491 if (wmp->wm_next) 26492 wmp->wm_next->wm_prev = wmp; 26493 un->un_wm = wmp; 26494 } 26495 state = SD_WM_DONE; 26496 } 26497 break; 26498 26499 case SD_WM_WAIT_MAP: 26500 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 26501 /* 26502 * Wait is done on sl_wmp, which is set in the 26503 * check_list state. 26504 */ 26505 sl_wmp->wm_wanted_count++; 26506 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 26507 sl_wmp->wm_wanted_count--; 26508 /* 26509 * We can reuse the memory from the completed sl_wmp 26510 * lock range for our new lock, but only if noone is 26511 * waiting for it. 26512 */ 26513 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 26514 if (sl_wmp->wm_wanted_count == 0) { 26515 if (wmp != NULL) 26516 CHK_N_FREEWMP(un, wmp); 26517 wmp = sl_wmp; 26518 } 26519 sl_wmp = NULL; 26520 /* 26521 * After waking up, need to recheck for availability of 26522 * range. 26523 */ 26524 state = SD_WM_CHK_LIST; 26525 break; 26526 26527 default: 26528 panic("sd_range_lock: " 26529 "Unknown state %d in sd_range_lock", state); 26530 /*NOTREACHED*/ 26531 } /* switch(state) */ 26532 26533 } /* while(state != SD_WM_DONE) */ 26534 26535 mutex_exit(SD_MUTEX(un)); 26536 26537 ASSERT(wmp != NULL); 26538 26539 return (wmp); 26540 } 26541 26542 26543 /* 26544 * Function: sd_get_range() 26545 * 26546 * Description: Find if there any overlapping I/O to this one 26547 * Returns the write-map of 1st such I/O, NULL otherwise. 26548 * 26549 * Arguments: un - sd_lun structure for the device. 26550 * startb - The starting block number 26551 * endb - The end block number 26552 * 26553 * Return Code: wm - pointer to the wmap structure. 26554 */ 26555 26556 static struct sd_w_map * 26557 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 26558 { 26559 struct sd_w_map *wmp; 26560 26561 ASSERT(un != NULL); 26562 26563 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 26564 if (!(wmp->wm_flags & SD_WM_BUSY)) { 26565 continue; 26566 } 26567 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 26568 break; 26569 } 26570 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 26571 break; 26572 } 26573 } 26574 26575 return (wmp); 26576 } 26577 26578 26579 /* 26580 * Function: sd_free_inlist_wmap() 26581 * 26582 * Description: Unlink and free a write map struct. 26583 * 26584 * Arguments: un - sd_lun structure for the device. 26585 * wmp - sd_w_map which needs to be unlinked. 26586 */ 26587 26588 static void 26589 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 26590 { 26591 ASSERT(un != NULL); 26592 26593 if (un->un_wm == wmp) { 26594 un->un_wm = wmp->wm_next; 26595 } else { 26596 wmp->wm_prev->wm_next = wmp->wm_next; 26597 } 26598 26599 if (wmp->wm_next) { 26600 wmp->wm_next->wm_prev = wmp->wm_prev; 26601 } 26602 26603 wmp->wm_next = wmp->wm_prev = NULL; 26604 26605 kmem_cache_free(un->un_wm_cache, wmp); 26606 } 26607 26608 26609 /* 26610 * Function: sd_range_unlock() 26611 * 26612 * Description: Unlock the range locked by wm. 26613 * Free write map if nobody else is waiting on it. 26614 * 26615 * Arguments: un - sd_lun structure for the device. 26616 * wmp - sd_w_map which needs to be unlinked. 26617 */ 26618 26619 static void 26620 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 26621 { 26622 ASSERT(un != NULL); 26623 ASSERT(wm != NULL); 26624 ASSERT(!mutex_owned(SD_MUTEX(un))); 26625 26626 mutex_enter(SD_MUTEX(un)); 26627 26628 if (wm->wm_flags & SD_WTYPE_RMW) { 26629 un->un_rmw_count--; 26630 } 26631 26632 if (wm->wm_wanted_count) { 26633 wm->wm_flags = 0; 26634 /* 26635 * Broadcast that the wmap is available now. 26636 */ 26637 cv_broadcast(&wm->wm_avail); 26638 } else { 26639 /* 26640 * If no one is waiting on the map, it should be free'ed. 26641 */ 26642 sd_free_inlist_wmap(un, wm); 26643 } 26644 26645 mutex_exit(SD_MUTEX(un)); 26646 } 26647 26648 26649 /* 26650 * Function: sd_read_modify_write_task 26651 * 26652 * Description: Called from a taskq thread to initiate the write phase of 26653 * a read-modify-write request. This is used for targets where 26654 * un->un_sys_blocksize != un->un_tgt_blocksize. 26655 * 26656 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 26657 * 26658 * Context: Called under taskq thread context. 26659 */ 26660 26661 static void 26662 sd_read_modify_write_task(void *arg) 26663 { 26664 struct sd_mapblocksize_info *bsp; 26665 struct buf *bp; 26666 struct sd_xbuf *xp; 26667 struct sd_lun *un; 26668 26669 bp = arg; /* The bp is given in arg */ 26670 ASSERT(bp != NULL); 26671 26672 /* Get the pointer to the layer-private data struct */ 26673 xp = SD_GET_XBUF(bp); 26674 ASSERT(xp != NULL); 26675 bsp = xp->xb_private; 26676 ASSERT(bsp != NULL); 26677 26678 un = SD_GET_UN(bp); 26679 ASSERT(un != NULL); 26680 ASSERT(!mutex_owned(SD_MUTEX(un))); 26681 26682 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26683 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 26684 26685 /* 26686 * This is the write phase of a read-modify-write request, called 26687 * under the context of a taskq thread in response to the completion 26688 * of the read portion of the rmw request completing under interrupt 26689 * context. The write request must be sent from here down the iostart 26690 * chain as if it were being sent from sd_mapblocksize_iostart(), so 26691 * we use the layer index saved in the layer-private data area. 26692 */ 26693 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 26694 26695 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26696 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 26697 } 26698 26699 26700 /* 26701 * Function: sddump_do_read_of_rmw() 26702 * 26703 * Description: This routine will be called from sddump, If sddump is called 26704 * with an I/O which not aligned on device blocksize boundary 26705 * then the write has to be converted to read-modify-write. 26706 * Do the read part here in order to keep sddump simple. 26707 * Note - That the sd_mutex is held across the call to this 26708 * routine. 26709 * 26710 * Arguments: un - sd_lun 26711 * blkno - block number in terms of media block size. 26712 * nblk - number of blocks. 26713 * bpp - pointer to pointer to the buf structure. On return 26714 * from this function, *bpp points to the valid buffer 26715 * to which the write has to be done. 26716 * 26717 * Return Code: 0 for success or errno-type return code 26718 */ 26719 26720 static int 26721 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 26722 struct buf **bpp) 26723 { 26724 int err; 26725 int i; 26726 int rval; 26727 struct buf *bp; 26728 struct scsi_pkt *pkt = NULL; 26729 uint32_t target_blocksize; 26730 26731 ASSERT(un != NULL); 26732 ASSERT(mutex_owned(SD_MUTEX(un))); 26733 26734 target_blocksize = un->un_tgt_blocksize; 26735 26736 mutex_exit(SD_MUTEX(un)); 26737 26738 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 26739 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 26740 if (bp == NULL) { 26741 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26742 "no resources for dumping; giving up"); 26743 err = ENOMEM; 26744 goto done; 26745 } 26746 26747 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 26748 blkno, nblk); 26749 if (rval != 0) { 26750 scsi_free_consistent_buf(bp); 26751 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26752 "no resources for dumping; giving up"); 26753 err = ENOMEM; 26754 goto done; 26755 } 26756 26757 pkt->pkt_flags |= FLAG_NOINTR; 26758 26759 err = EIO; 26760 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26761 26762 /* 26763 * Scsi_poll returns 0 (success) if the command completes and 26764 * the status block is STATUS_GOOD. We should only check 26765 * errors if this condition is not true. Even then we should 26766 * send our own request sense packet only if we have a check 26767 * condition and auto request sense has not been performed by 26768 * the hba. 26769 */ 26770 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 26771 26772 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 26773 err = 0; 26774 break; 26775 } 26776 26777 /* 26778 * Check CMD_DEV_GONE 1st, give up if device is gone, 26779 * no need to read RQS data. 26780 */ 26781 if (pkt->pkt_reason == CMD_DEV_GONE) { 26782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26783 "Error while dumping state with rmw..." 26784 "Device is gone\n"); 26785 break; 26786 } 26787 26788 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 26789 SD_INFO(SD_LOG_DUMP, un, 26790 "sddump: read failed with CHECK, try # %d\n", i); 26791 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 26792 (void) sd_send_polled_RQS(un); 26793 } 26794 26795 continue; 26796 } 26797 26798 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 26799 int reset_retval = 0; 26800 26801 SD_INFO(SD_LOG_DUMP, un, 26802 "sddump: read failed with BUSY, try # %d\n", i); 26803 26804 if (un->un_f_lun_reset_enabled == TRUE) { 26805 reset_retval = scsi_reset(SD_ADDRESS(un), 26806 RESET_LUN); 26807 } 26808 if (reset_retval == 0) { 26809 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26810 } 26811 (void) sd_send_polled_RQS(un); 26812 26813 } else { 26814 SD_INFO(SD_LOG_DUMP, un, 26815 "sddump: read failed with 0x%x, try # %d\n", 26816 SD_GET_PKT_STATUS(pkt), i); 26817 mutex_enter(SD_MUTEX(un)); 26818 sd_reset_target(un, pkt); 26819 mutex_exit(SD_MUTEX(un)); 26820 } 26821 26822 /* 26823 * If we are not getting anywhere with lun/target resets, 26824 * let's reset the bus. 26825 */ 26826 if (i > SD_NDUMP_RETRIES/2) { 26827 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26828 (void) sd_send_polled_RQS(un); 26829 } 26830 26831 } 26832 scsi_destroy_pkt(pkt); 26833 26834 if (err != 0) { 26835 scsi_free_consistent_buf(bp); 26836 *bpp = NULL; 26837 } else { 26838 *bpp = bp; 26839 } 26840 26841 done: 26842 mutex_enter(SD_MUTEX(un)); 26843 return (err); 26844 } 26845 26846 26847 /* 26848 * Function: sd_failfast_flushq 26849 * 26850 * Description: Take all bp's on the wait queue that have B_FAILFAST set 26851 * in b_flags and move them onto the failfast queue, then kick 26852 * off a thread to return all bp's on the failfast queue to 26853 * their owners with an error set. 26854 * 26855 * Arguments: un - pointer to the soft state struct for the instance. 26856 * 26857 * Context: may execute in interrupt context. 26858 */ 26859 26860 static void 26861 sd_failfast_flushq(struct sd_lun *un) 26862 { 26863 struct buf *bp; 26864 struct buf *next_waitq_bp; 26865 struct buf *prev_waitq_bp = NULL; 26866 26867 ASSERT(un != NULL); 26868 ASSERT(mutex_owned(SD_MUTEX(un))); 26869 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 26870 ASSERT(un->un_failfast_bp == NULL); 26871 26872 SD_TRACE(SD_LOG_IO_FAILFAST, un, 26873 "sd_failfast_flushq: entry: un:0x%p\n", un); 26874 26875 /* 26876 * Check if we should flush all bufs when entering failfast state, or 26877 * just those with B_FAILFAST set. 26878 */ 26879 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 26880 /* 26881 * Move *all* bp's on the wait queue to the failfast flush 26882 * queue, including those that do NOT have B_FAILFAST set. 26883 */ 26884 if (un->un_failfast_headp == NULL) { 26885 ASSERT(un->un_failfast_tailp == NULL); 26886 un->un_failfast_headp = un->un_waitq_headp; 26887 } else { 26888 ASSERT(un->un_failfast_tailp != NULL); 26889 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 26890 } 26891 26892 un->un_failfast_tailp = un->un_waitq_tailp; 26893 26894 /* update kstat for each bp moved out of the waitq */ 26895 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 26896 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26897 } 26898 26899 /* empty the waitq */ 26900 un->un_waitq_headp = un->un_waitq_tailp = NULL; 26901 26902 } else { 26903 /* 26904 * Go thru the wait queue, pick off all entries with 26905 * B_FAILFAST set, and move these onto the failfast queue. 26906 */ 26907 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 26908 /* 26909 * Save the pointer to the next bp on the wait queue, 26910 * so we get to it on the next iteration of this loop. 26911 */ 26912 next_waitq_bp = bp->av_forw; 26913 26914 /* 26915 * If this bp from the wait queue does NOT have 26916 * B_FAILFAST set, just move on to the next element 26917 * in the wait queue. Note, this is the only place 26918 * where it is correct to set prev_waitq_bp. 26919 */ 26920 if ((bp->b_flags & B_FAILFAST) == 0) { 26921 prev_waitq_bp = bp; 26922 continue; 26923 } 26924 26925 /* 26926 * Remove the bp from the wait queue. 26927 */ 26928 if (bp == un->un_waitq_headp) { 26929 /* The bp is the first element of the waitq. */ 26930 un->un_waitq_headp = next_waitq_bp; 26931 if (un->un_waitq_headp == NULL) { 26932 /* The wait queue is now empty */ 26933 un->un_waitq_tailp = NULL; 26934 } 26935 } else { 26936 /* 26937 * The bp is either somewhere in the middle 26938 * or at the end of the wait queue. 26939 */ 26940 ASSERT(un->un_waitq_headp != NULL); 26941 ASSERT(prev_waitq_bp != NULL); 26942 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 26943 == 0); 26944 if (bp == un->un_waitq_tailp) { 26945 /* bp is the last entry on the waitq. */ 26946 ASSERT(next_waitq_bp == NULL); 26947 un->un_waitq_tailp = prev_waitq_bp; 26948 } 26949 prev_waitq_bp->av_forw = next_waitq_bp; 26950 } 26951 bp->av_forw = NULL; 26952 26953 /* 26954 * update kstat since the bp is moved out of 26955 * the waitq 26956 */ 26957 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26958 26959 /* 26960 * Now put the bp onto the failfast queue. 26961 */ 26962 if (un->un_failfast_headp == NULL) { 26963 /* failfast queue is currently empty */ 26964 ASSERT(un->un_failfast_tailp == NULL); 26965 un->un_failfast_headp = 26966 un->un_failfast_tailp = bp; 26967 } else { 26968 /* Add the bp to the end of the failfast q */ 26969 ASSERT(un->un_failfast_tailp != NULL); 26970 ASSERT(un->un_failfast_tailp->b_flags & 26971 B_FAILFAST); 26972 un->un_failfast_tailp->av_forw = bp; 26973 un->un_failfast_tailp = bp; 26974 } 26975 } 26976 } 26977 26978 /* 26979 * Now return all bp's on the failfast queue to their owners. 26980 */ 26981 while ((bp = un->un_failfast_headp) != NULL) { 26982 26983 un->un_failfast_headp = bp->av_forw; 26984 if (un->un_failfast_headp == NULL) { 26985 un->un_failfast_tailp = NULL; 26986 } 26987 26988 /* 26989 * We want to return the bp with a failure error code, but 26990 * we do not want a call to sd_start_cmds() to occur here, 26991 * so use sd_return_failed_command_no_restart() instead of 26992 * sd_return_failed_command(). 26993 */ 26994 sd_return_failed_command_no_restart(un, bp, EIO); 26995 } 26996 26997 /* Flush the xbuf queues if required. */ 26998 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 26999 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 27000 } 27001 27002 SD_TRACE(SD_LOG_IO_FAILFAST, un, 27003 "sd_failfast_flushq: exit: un:0x%p\n", un); 27004 } 27005 27006 27007 /* 27008 * Function: sd_failfast_flushq_callback 27009 * 27010 * Description: Return TRUE if the given bp meets the criteria for failfast 27011 * flushing. Used with ddi_xbuf_flushq(9F). 27012 * 27013 * Arguments: bp - ptr to buf struct to be examined. 27014 * 27015 * Context: Any 27016 */ 27017 27018 static int 27019 sd_failfast_flushq_callback(struct buf *bp) 27020 { 27021 /* 27022 * Return TRUE if (1) we want to flush ALL bufs when the failfast 27023 * state is entered; OR (2) the given bp has B_FAILFAST set. 27024 */ 27025 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 27026 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 27027 } 27028 27029 27030 27031 /* 27032 * Function: sd_setup_next_xfer 27033 * 27034 * Description: Prepare next I/O operation using DMA_PARTIAL 27035 * 27036 */ 27037 27038 static int 27039 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 27040 struct scsi_pkt *pkt, struct sd_xbuf *xp) 27041 { 27042 ssize_t num_blks_not_xfered; 27043 daddr_t strt_blk_num; 27044 ssize_t bytes_not_xfered; 27045 int rval; 27046 27047 ASSERT(pkt->pkt_resid == 0); 27048 27049 /* 27050 * Calculate next block number and amount to be transferred. 27051 * 27052 * How much data NOT transfered to the HBA yet. 27053 */ 27054 bytes_not_xfered = xp->xb_dma_resid; 27055 27056 /* 27057 * figure how many blocks NOT transfered to the HBA yet. 27058 */ 27059 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 27060 27061 /* 27062 * set starting block number to the end of what WAS transfered. 27063 */ 27064 strt_blk_num = xp->xb_blkno + 27065 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 27066 27067 /* 27068 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 27069 * will call scsi_initpkt with NULL_FUNC so we do not have to release 27070 * the disk mutex here. 27071 */ 27072 rval = sd_setup_next_rw_pkt(un, pkt, bp, 27073 strt_blk_num, num_blks_not_xfered); 27074 27075 if (rval == 0) { 27076 27077 /* 27078 * Success. 27079 * 27080 * Adjust things if there are still more blocks to be 27081 * transfered. 27082 */ 27083 xp->xb_dma_resid = pkt->pkt_resid; 27084 pkt->pkt_resid = 0; 27085 27086 return (1); 27087 } 27088 27089 /* 27090 * There's really only one possible return value from 27091 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 27092 * returns NULL. 27093 */ 27094 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 27095 27096 bp->b_resid = bp->b_bcount; 27097 bp->b_flags |= B_ERROR; 27098 27099 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27100 "Error setting up next portion of DMA transfer\n"); 27101 27102 return (0); 27103 } 27104 27105 /* 27106 * Function: sd_panic_for_res_conflict 27107 * 27108 * Description: Call panic with a string formatted with "Reservation Conflict" 27109 * and a human readable identifier indicating the SD instance 27110 * that experienced the reservation conflict. 27111 * 27112 * Arguments: un - pointer to the soft state struct for the instance. 27113 * 27114 * Context: may execute in interrupt context. 27115 */ 27116 27117 #define SD_RESV_CONFLICT_FMT_LEN 40 27118 void 27119 sd_panic_for_res_conflict(struct sd_lun *un) 27120 { 27121 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 27122 char path_str[MAXPATHLEN]; 27123 27124 (void) snprintf(panic_str, sizeof (panic_str), 27125 "Reservation Conflict\nDisk: %s", 27126 ddi_pathname(SD_DEVINFO(un), path_str)); 27127 27128 panic(panic_str); 27129 } 27130 27131 /* 27132 * Note: The following sd_faultinjection_ioctl( ) routines implement 27133 * driver support for handling fault injection for error analysis 27134 * causing faults in multiple layers of the driver. 27135 * 27136 */ 27137 27138 #ifdef SD_FAULT_INJECTION 27139 static uint_t sd_fault_injection_on = 0; 27140 27141 /* 27142 * Function: sd_faultinjection_ioctl() 27143 * 27144 * Description: This routine is the driver entry point for handling 27145 * faultinjection ioctls to inject errors into the 27146 * layer model 27147 * 27148 * Arguments: cmd - the ioctl cmd received 27149 * arg - the arguments from user and returns 27150 */ 27151 27152 static void 27153 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 27154 27155 uint_t i; 27156 uint_t rval; 27157 27158 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 27159 27160 mutex_enter(SD_MUTEX(un)); 27161 27162 switch (cmd) { 27163 case SDIOCRUN: 27164 /* Allow pushed faults to be injected */ 27165 SD_INFO(SD_LOG_SDTEST, un, 27166 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 27167 27168 sd_fault_injection_on = 1; 27169 27170 SD_INFO(SD_LOG_IOERR, un, 27171 "sd_faultinjection_ioctl: run finished\n"); 27172 break; 27173 27174 case SDIOCSTART: 27175 /* Start Injection Session */ 27176 SD_INFO(SD_LOG_SDTEST, un, 27177 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 27178 27179 sd_fault_injection_on = 0; 27180 un->sd_injection_mask = 0xFFFFFFFF; 27181 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 27182 un->sd_fi_fifo_pkt[i] = NULL; 27183 un->sd_fi_fifo_xb[i] = NULL; 27184 un->sd_fi_fifo_un[i] = NULL; 27185 un->sd_fi_fifo_arq[i] = NULL; 27186 } 27187 un->sd_fi_fifo_start = 0; 27188 un->sd_fi_fifo_end = 0; 27189 27190 mutex_enter(&(un->un_fi_mutex)); 27191 un->sd_fi_log[0] = '\0'; 27192 un->sd_fi_buf_len = 0; 27193 mutex_exit(&(un->un_fi_mutex)); 27194 27195 SD_INFO(SD_LOG_IOERR, un, 27196 "sd_faultinjection_ioctl: start finished\n"); 27197 break; 27198 27199 case SDIOCSTOP: 27200 /* Stop Injection Session */ 27201 SD_INFO(SD_LOG_SDTEST, un, 27202 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 27203 sd_fault_injection_on = 0; 27204 un->sd_injection_mask = 0x0; 27205 27206 /* Empty stray or unuseds structs from fifo */ 27207 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 27208 if (un->sd_fi_fifo_pkt[i] != NULL) { 27209 kmem_free(un->sd_fi_fifo_pkt[i], 27210 sizeof (struct sd_fi_pkt)); 27211 } 27212 if (un->sd_fi_fifo_xb[i] != NULL) { 27213 kmem_free(un->sd_fi_fifo_xb[i], 27214 sizeof (struct sd_fi_xb)); 27215 } 27216 if (un->sd_fi_fifo_un[i] != NULL) { 27217 kmem_free(un->sd_fi_fifo_un[i], 27218 sizeof (struct sd_fi_un)); 27219 } 27220 if (un->sd_fi_fifo_arq[i] != NULL) { 27221 kmem_free(un->sd_fi_fifo_arq[i], 27222 sizeof (struct sd_fi_arq)); 27223 } 27224 un->sd_fi_fifo_pkt[i] = NULL; 27225 un->sd_fi_fifo_un[i] = NULL; 27226 un->sd_fi_fifo_xb[i] = NULL; 27227 un->sd_fi_fifo_arq[i] = NULL; 27228 } 27229 un->sd_fi_fifo_start = 0; 27230 un->sd_fi_fifo_end = 0; 27231 27232 SD_INFO(SD_LOG_IOERR, un, 27233 "sd_faultinjection_ioctl: stop finished\n"); 27234 break; 27235 27236 case SDIOCINSERTPKT: 27237 /* Store a packet struct to be pushed onto fifo */ 27238 SD_INFO(SD_LOG_SDTEST, un, 27239 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 27240 27241 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27242 27243 sd_fault_injection_on = 0; 27244 27245 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 27246 if (un->sd_fi_fifo_pkt[i] != NULL) { 27247 kmem_free(un->sd_fi_fifo_pkt[i], 27248 sizeof (struct sd_fi_pkt)); 27249 } 27250 if (arg != NULL) { 27251 un->sd_fi_fifo_pkt[i] = 27252 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 27253 if (un->sd_fi_fifo_pkt[i] == NULL) { 27254 /* Alloc failed don't store anything */ 27255 break; 27256 } 27257 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 27258 sizeof (struct sd_fi_pkt), 0); 27259 if (rval == -1) { 27260 kmem_free(un->sd_fi_fifo_pkt[i], 27261 sizeof (struct sd_fi_pkt)); 27262 un->sd_fi_fifo_pkt[i] = NULL; 27263 } 27264 } else { 27265 SD_INFO(SD_LOG_IOERR, un, 27266 "sd_faultinjection_ioctl: pkt null\n"); 27267 } 27268 break; 27269 27270 case SDIOCINSERTXB: 27271 /* Store a xb struct to be pushed onto fifo */ 27272 SD_INFO(SD_LOG_SDTEST, un, 27273 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 27274 27275 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27276 27277 sd_fault_injection_on = 0; 27278 27279 if (un->sd_fi_fifo_xb[i] != NULL) { 27280 kmem_free(un->sd_fi_fifo_xb[i], 27281 sizeof (struct sd_fi_xb)); 27282 un->sd_fi_fifo_xb[i] = NULL; 27283 } 27284 if (arg != NULL) { 27285 un->sd_fi_fifo_xb[i] = 27286 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 27287 if (un->sd_fi_fifo_xb[i] == NULL) { 27288 /* Alloc failed don't store anything */ 27289 break; 27290 } 27291 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 27292 sizeof (struct sd_fi_xb), 0); 27293 27294 if (rval == -1) { 27295 kmem_free(un->sd_fi_fifo_xb[i], 27296 sizeof (struct sd_fi_xb)); 27297 un->sd_fi_fifo_xb[i] = NULL; 27298 } 27299 } else { 27300 SD_INFO(SD_LOG_IOERR, un, 27301 "sd_faultinjection_ioctl: xb null\n"); 27302 } 27303 break; 27304 27305 case SDIOCINSERTUN: 27306 /* Store a un struct to be pushed onto fifo */ 27307 SD_INFO(SD_LOG_SDTEST, un, 27308 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 27309 27310 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27311 27312 sd_fault_injection_on = 0; 27313 27314 if (un->sd_fi_fifo_un[i] != NULL) { 27315 kmem_free(un->sd_fi_fifo_un[i], 27316 sizeof (struct sd_fi_un)); 27317 un->sd_fi_fifo_un[i] = NULL; 27318 } 27319 if (arg != NULL) { 27320 un->sd_fi_fifo_un[i] = 27321 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 27322 if (un->sd_fi_fifo_un[i] == NULL) { 27323 /* Alloc failed don't store anything */ 27324 break; 27325 } 27326 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 27327 sizeof (struct sd_fi_un), 0); 27328 if (rval == -1) { 27329 kmem_free(un->sd_fi_fifo_un[i], 27330 sizeof (struct sd_fi_un)); 27331 un->sd_fi_fifo_un[i] = NULL; 27332 } 27333 27334 } else { 27335 SD_INFO(SD_LOG_IOERR, un, 27336 "sd_faultinjection_ioctl: un null\n"); 27337 } 27338 27339 break; 27340 27341 case SDIOCINSERTARQ: 27342 /* Store a arq struct to be pushed onto fifo */ 27343 SD_INFO(SD_LOG_SDTEST, un, 27344 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 27345 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27346 27347 sd_fault_injection_on = 0; 27348 27349 if (un->sd_fi_fifo_arq[i] != NULL) { 27350 kmem_free(un->sd_fi_fifo_arq[i], 27351 sizeof (struct sd_fi_arq)); 27352 un->sd_fi_fifo_arq[i] = NULL; 27353 } 27354 if (arg != NULL) { 27355 un->sd_fi_fifo_arq[i] = 27356 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 27357 if (un->sd_fi_fifo_arq[i] == NULL) { 27358 /* Alloc failed don't store anything */ 27359 break; 27360 } 27361 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 27362 sizeof (struct sd_fi_arq), 0); 27363 if (rval == -1) { 27364 kmem_free(un->sd_fi_fifo_arq[i], 27365 sizeof (struct sd_fi_arq)); 27366 un->sd_fi_fifo_arq[i] = NULL; 27367 } 27368 27369 } else { 27370 SD_INFO(SD_LOG_IOERR, un, 27371 "sd_faultinjection_ioctl: arq null\n"); 27372 } 27373 27374 break; 27375 27376 case SDIOCPUSH: 27377 /* Push stored xb, pkt, un, and arq onto fifo */ 27378 sd_fault_injection_on = 0; 27379 27380 if (arg != NULL) { 27381 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 27382 if (rval != -1 && 27383 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 27384 un->sd_fi_fifo_end += i; 27385 } 27386 } else { 27387 SD_INFO(SD_LOG_IOERR, un, 27388 "sd_faultinjection_ioctl: push arg null\n"); 27389 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 27390 un->sd_fi_fifo_end++; 27391 } 27392 } 27393 SD_INFO(SD_LOG_IOERR, un, 27394 "sd_faultinjection_ioctl: push to end=%d\n", 27395 un->sd_fi_fifo_end); 27396 break; 27397 27398 case SDIOCRETRIEVE: 27399 /* Return buffer of log from Injection session */ 27400 SD_INFO(SD_LOG_SDTEST, un, 27401 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 27402 27403 sd_fault_injection_on = 0; 27404 27405 mutex_enter(&(un->un_fi_mutex)); 27406 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 27407 un->sd_fi_buf_len+1, 0); 27408 mutex_exit(&(un->un_fi_mutex)); 27409 27410 if (rval == -1) { 27411 /* 27412 * arg is possibly invalid setting 27413 * it to NULL for return 27414 */ 27415 arg = NULL; 27416 } 27417 break; 27418 } 27419 27420 mutex_exit(SD_MUTEX(un)); 27421 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 27422 " exit\n"); 27423 } 27424 27425 27426 /* 27427 * Function: sd_injection_log() 27428 * 27429 * Description: This routine adds buff to the already existing injection log 27430 * for retrieval via faultinjection_ioctl for use in fault 27431 * detection and recovery 27432 * 27433 * Arguments: buf - the string to add to the log 27434 */ 27435 27436 static void 27437 sd_injection_log(char *buf, struct sd_lun *un) 27438 { 27439 uint_t len; 27440 27441 ASSERT(un != NULL); 27442 ASSERT(buf != NULL); 27443 27444 mutex_enter(&(un->un_fi_mutex)); 27445 27446 len = min(strlen(buf), 255); 27447 /* Add logged value to Injection log to be returned later */ 27448 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 27449 uint_t offset = strlen((char *)un->sd_fi_log); 27450 char *destp = (char *)un->sd_fi_log + offset; 27451 int i; 27452 for (i = 0; i < len; i++) { 27453 *destp++ = *buf++; 27454 } 27455 un->sd_fi_buf_len += len; 27456 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 27457 } 27458 27459 mutex_exit(&(un->un_fi_mutex)); 27460 } 27461 27462 27463 /* 27464 * Function: sd_faultinjection() 27465 * 27466 * Description: This routine takes the pkt and changes its 27467 * content based on error injection scenerio. 27468 * 27469 * Arguments: pktp - packet to be changed 27470 */ 27471 27472 static void 27473 sd_faultinjection(struct scsi_pkt *pktp) 27474 { 27475 uint_t i; 27476 struct sd_fi_pkt *fi_pkt; 27477 struct sd_fi_xb *fi_xb; 27478 struct sd_fi_un *fi_un; 27479 struct sd_fi_arq *fi_arq; 27480 struct buf *bp; 27481 struct sd_xbuf *xb; 27482 struct sd_lun *un; 27483 27484 ASSERT(pktp != NULL); 27485 27486 /* pull bp xb and un from pktp */ 27487 bp = (struct buf *)pktp->pkt_private; 27488 xb = SD_GET_XBUF(bp); 27489 un = SD_GET_UN(bp); 27490 27491 ASSERT(un != NULL); 27492 27493 mutex_enter(SD_MUTEX(un)); 27494 27495 SD_TRACE(SD_LOG_SDTEST, un, 27496 "sd_faultinjection: entry Injection from sdintr\n"); 27497 27498 /* if injection is off return */ 27499 if (sd_fault_injection_on == 0 || 27500 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 27501 mutex_exit(SD_MUTEX(un)); 27502 return; 27503 } 27504 27505 27506 /* take next set off fifo */ 27507 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 27508 27509 fi_pkt = un->sd_fi_fifo_pkt[i]; 27510 fi_xb = un->sd_fi_fifo_xb[i]; 27511 fi_un = un->sd_fi_fifo_un[i]; 27512 fi_arq = un->sd_fi_fifo_arq[i]; 27513 27514 27515 /* set variables accordingly */ 27516 /* set pkt if it was on fifo */ 27517 if (fi_pkt != NULL) { 27518 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 27519 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 27520 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 27521 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 27522 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 27523 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 27524 27525 } 27526 27527 /* set xb if it was on fifo */ 27528 if (fi_xb != NULL) { 27529 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 27530 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 27531 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 27532 SD_CONDSET(xb, xb, xb_victim_retry_count, 27533 "xb_victim_retry_count"); 27534 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 27535 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 27536 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 27537 27538 /* copy in block data from sense */ 27539 if (fi_xb->xb_sense_data[0] != -1) { 27540 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 27541 SENSE_LENGTH); 27542 } 27543 27544 /* copy in extended sense codes */ 27545 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 27546 "es_code"); 27547 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 27548 "es_key"); 27549 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 27550 "es_add_code"); 27551 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 27552 es_qual_code, "es_qual_code"); 27553 } 27554 27555 /* set un if it was on fifo */ 27556 if (fi_un != NULL) { 27557 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 27558 SD_CONDSET(un, un, un_ctype, "un_ctype"); 27559 SD_CONDSET(un, un, un_reset_retry_count, 27560 "un_reset_retry_count"); 27561 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 27562 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 27563 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 27564 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 27565 "un_f_allow_bus_device_reset"); 27566 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 27567 27568 } 27569 27570 /* copy in auto request sense if it was on fifo */ 27571 if (fi_arq != NULL) { 27572 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 27573 } 27574 27575 /* free structs */ 27576 if (un->sd_fi_fifo_pkt[i] != NULL) { 27577 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 27578 } 27579 if (un->sd_fi_fifo_xb[i] != NULL) { 27580 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 27581 } 27582 if (un->sd_fi_fifo_un[i] != NULL) { 27583 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 27584 } 27585 if (un->sd_fi_fifo_arq[i] != NULL) { 27586 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 27587 } 27588 27589 /* 27590 * kmem_free does not gurantee to set to NULL 27591 * since we uses these to determine if we set 27592 * values or not lets confirm they are always 27593 * NULL after free 27594 */ 27595 un->sd_fi_fifo_pkt[i] = NULL; 27596 un->sd_fi_fifo_un[i] = NULL; 27597 un->sd_fi_fifo_xb[i] = NULL; 27598 un->sd_fi_fifo_arq[i] = NULL; 27599 27600 un->sd_fi_fifo_start++; 27601 27602 mutex_exit(SD_MUTEX(un)); 27603 27604 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 27605 } 27606 27607 #endif /* SD_FAULT_INJECTION */ 27608 27609 /* 27610 * This routine is invoked in sd_unit_attach(). Before calling it, the 27611 * properties in conf file should be processed already, and "hotpluggable" 27612 * property was processed also. 27613 * 27614 * The sd driver distinguishes 3 different type of devices: removable media, 27615 * non-removable media, and hotpluggable. Below the differences are defined: 27616 * 27617 * 1. Device ID 27618 * 27619 * The device ID of a device is used to identify this device. Refer to 27620 * ddi_devid_register(9F). 27621 * 27622 * For a non-removable media disk device which can provide 0x80 or 0x83 27623 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 27624 * device ID is created to identify this device. For other non-removable 27625 * media devices, a default device ID is created only if this device has 27626 * at least 2 alter cylinders. Otherwise, this device has no devid. 27627 * 27628 * ------------------------------------------------------- 27629 * removable media hotpluggable | Can Have Device ID 27630 * ------------------------------------------------------- 27631 * false false | Yes 27632 * false true | Yes 27633 * true x | No 27634 * ------------------------------------------------------ 27635 * 27636 * 27637 * 2. SCSI group 4 commands 27638 * 27639 * In SCSI specs, only some commands in group 4 command set can use 27640 * 8-byte addresses that can be used to access >2TB storage spaces. 27641 * Other commands have no such capability. Without supporting group4, 27642 * it is impossible to make full use of storage spaces of a disk with 27643 * capacity larger than 2TB. 27644 * 27645 * ----------------------------------------------- 27646 * removable media hotpluggable LP64 | Group 27647 * ----------------------------------------------- 27648 * false false false | 1 27649 * false false true | 4 27650 * false true false | 1 27651 * false true true | 4 27652 * true x x | 5 27653 * ----------------------------------------------- 27654 * 27655 * 27656 * 3. Check for VTOC Label 27657 * 27658 * If a direct-access disk has no EFI label, sd will check if it has a 27659 * valid VTOC label. Now, sd also does that check for removable media 27660 * and hotpluggable devices. 27661 * 27662 * -------------------------------------------------------------- 27663 * Direct-Access removable media hotpluggable | Check Label 27664 * ------------------------------------------------------------- 27665 * false false false | No 27666 * false false true | No 27667 * false true false | Yes 27668 * false true true | Yes 27669 * true x x | Yes 27670 * -------------------------------------------------------------- 27671 * 27672 * 27673 * 4. Building default VTOC label 27674 * 27675 * As section 3 says, sd checks if some kinds of devices have VTOC label. 27676 * If those devices have no valid VTOC label, sd(7d) will attempt to 27677 * create default VTOC for them. Currently sd creates default VTOC label 27678 * for all devices on x86 platform (VTOC_16), but only for removable 27679 * media devices on SPARC (VTOC_8). 27680 * 27681 * ----------------------------------------------------------- 27682 * removable media hotpluggable platform | Default Label 27683 * ----------------------------------------------------------- 27684 * false false sparc | No 27685 * false true x86 | Yes 27686 * false true sparc | Yes 27687 * true x x | Yes 27688 * ---------------------------------------------------------- 27689 * 27690 * 27691 * 5. Supported blocksizes of target devices 27692 * 27693 * Sd supports non-512-byte blocksize for removable media devices only. 27694 * For other devices, only 512-byte blocksize is supported. This may be 27695 * changed in near future because some RAID devices require non-512-byte 27696 * blocksize 27697 * 27698 * ----------------------------------------------------------- 27699 * removable media hotpluggable | non-512-byte blocksize 27700 * ----------------------------------------------------------- 27701 * false false | No 27702 * false true | No 27703 * true x | Yes 27704 * ----------------------------------------------------------- 27705 * 27706 * 27707 * 6. Automatic mount & unmount 27708 * 27709 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 27710 * if a device is removable media device. It return 1 for removable media 27711 * devices, and 0 for others. 27712 * 27713 * The automatic mounting subsystem should distinguish between the types 27714 * of devices and apply automounting policies to each. 27715 * 27716 * 27717 * 7. fdisk partition management 27718 * 27719 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 27720 * just supports fdisk partitions on x86 platform. On sparc platform, sd 27721 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 27722 * fdisk partitions on both x86 and SPARC platform. 27723 * 27724 * ----------------------------------------------------------- 27725 * platform removable media USB/1394 | fdisk supported 27726 * ----------------------------------------------------------- 27727 * x86 X X | true 27728 * ------------------------------------------------------------ 27729 * sparc X X | false 27730 * ------------------------------------------------------------ 27731 * 27732 * 27733 * 8. MBOOT/MBR 27734 * 27735 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 27736 * read/write mboot for removable media devices on sparc platform. 27737 * 27738 * ----------------------------------------------------------- 27739 * platform removable media USB/1394 | mboot supported 27740 * ----------------------------------------------------------- 27741 * x86 X X | true 27742 * ------------------------------------------------------------ 27743 * sparc false false | false 27744 * sparc false true | true 27745 * sparc true false | true 27746 * sparc true true | true 27747 * ------------------------------------------------------------ 27748 * 27749 * 27750 * 9. error handling during opening device 27751 * 27752 * If failed to open a disk device, an errno is returned. For some kinds 27753 * of errors, different errno is returned depending on if this device is 27754 * a removable media device. This brings USB/1394 hard disks in line with 27755 * expected hard disk behavior. It is not expected that this breaks any 27756 * application. 27757 * 27758 * ------------------------------------------------------ 27759 * removable media hotpluggable | errno 27760 * ------------------------------------------------------ 27761 * false false | EIO 27762 * false true | EIO 27763 * true x | ENXIO 27764 * ------------------------------------------------------ 27765 * 27766 * 27767 * 11. ioctls: DKIOCEJECT, CDROMEJECT 27768 * 27769 * These IOCTLs are applicable only to removable media devices. 27770 * 27771 * ----------------------------------------------------------- 27772 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 27773 * ----------------------------------------------------------- 27774 * false false | No 27775 * false true | No 27776 * true x | Yes 27777 * ----------------------------------------------------------- 27778 * 27779 * 27780 * 12. Kstats for partitions 27781 * 27782 * sd creates partition kstat for non-removable media devices. USB and 27783 * Firewire hard disks now have partition kstats 27784 * 27785 * ------------------------------------------------------ 27786 * removable media hotpluggable | kstat 27787 * ------------------------------------------------------ 27788 * false false | Yes 27789 * false true | Yes 27790 * true x | No 27791 * ------------------------------------------------------ 27792 * 27793 * 27794 * 13. Removable media & hotpluggable properties 27795 * 27796 * Sd driver creates a "removable-media" property for removable media 27797 * devices. Parent nexus drivers create a "hotpluggable" property if 27798 * it supports hotplugging. 27799 * 27800 * --------------------------------------------------------------------- 27801 * removable media hotpluggable | "removable-media" " hotpluggable" 27802 * --------------------------------------------------------------------- 27803 * false false | No No 27804 * false true | No Yes 27805 * true false | Yes No 27806 * true true | Yes Yes 27807 * --------------------------------------------------------------------- 27808 * 27809 * 27810 * 14. Power Management 27811 * 27812 * sd only power manages removable media devices or devices that support 27813 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 27814 * 27815 * A parent nexus that supports hotplugging can also set "pm-capable" 27816 * if the disk can be power managed. 27817 * 27818 * ------------------------------------------------------------ 27819 * removable media hotpluggable pm-capable | power manage 27820 * ------------------------------------------------------------ 27821 * false false false | No 27822 * false false true | Yes 27823 * false true false | No 27824 * false true true | Yes 27825 * true x x | Yes 27826 * ------------------------------------------------------------ 27827 * 27828 * USB and firewire hard disks can now be power managed independently 27829 * of the framebuffer 27830 * 27831 * 27832 * 15. Support for USB disks with capacity larger than 1TB 27833 * 27834 * Currently, sd doesn't permit a fixed disk device with capacity 27835 * larger than 1TB to be used in a 32-bit operating system environment. 27836 * However, sd doesn't do that for removable media devices. Instead, it 27837 * assumes that removable media devices cannot have a capacity larger 27838 * than 1TB. Therefore, using those devices on 32-bit system is partially 27839 * supported, which can cause some unexpected results. 27840 * 27841 * --------------------------------------------------------------------- 27842 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 27843 * --------------------------------------------------------------------- 27844 * false false | true | no 27845 * false true | true | no 27846 * true false | true | Yes 27847 * true true | true | Yes 27848 * --------------------------------------------------------------------- 27849 * 27850 * 27851 * 16. Check write-protection at open time 27852 * 27853 * When a removable media device is being opened for writing without NDELAY 27854 * flag, sd will check if this device is writable. If attempting to open 27855 * without NDELAY flag a write-protected device, this operation will abort. 27856 * 27857 * ------------------------------------------------------------ 27858 * removable media USB/1394 | WP Check 27859 * ------------------------------------------------------------ 27860 * false false | No 27861 * false true | No 27862 * true false | Yes 27863 * true true | Yes 27864 * ------------------------------------------------------------ 27865 * 27866 * 27867 * 17. syslog when corrupted VTOC is encountered 27868 * 27869 * Currently, if an invalid VTOC is encountered, sd only print syslog 27870 * for fixed SCSI disks. 27871 * ------------------------------------------------------------ 27872 * removable media USB/1394 | print syslog 27873 * ------------------------------------------------------------ 27874 * false false | Yes 27875 * false true | No 27876 * true false | No 27877 * true true | No 27878 * ------------------------------------------------------------ 27879 */ 27880 static void 27881 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 27882 { 27883 int pm_capable_prop; 27884 27885 ASSERT(un->un_sd); 27886 ASSERT(un->un_sd->sd_inq); 27887 27888 /* 27889 * Enable SYNC CACHE support for all devices. 27890 */ 27891 un->un_f_sync_cache_supported = TRUE; 27892 27893 if (un->un_sd->sd_inq->inq_rmb) { 27894 /* 27895 * The media of this device is removable. And for this kind 27896 * of devices, it is possible to change medium after opening 27897 * devices. Thus we should support this operation. 27898 */ 27899 un->un_f_has_removable_media = TRUE; 27900 27901 /* 27902 * support non-512-byte blocksize of removable media devices 27903 */ 27904 un->un_f_non_devbsize_supported = TRUE; 27905 27906 /* 27907 * Assume that all removable media devices support DOOR_LOCK 27908 */ 27909 un->un_f_doorlock_supported = TRUE; 27910 27911 /* 27912 * For a removable media device, it is possible to be opened 27913 * with NDELAY flag when there is no media in drive, in this 27914 * case we don't care if device is writable. But if without 27915 * NDELAY flag, we need to check if media is write-protected. 27916 */ 27917 un->un_f_chk_wp_open = TRUE; 27918 27919 /* 27920 * need to start a SCSI watch thread to monitor media state, 27921 * when media is being inserted or ejected, notify syseventd. 27922 */ 27923 un->un_f_monitor_media_state = TRUE; 27924 27925 /* 27926 * Some devices don't support START_STOP_UNIT command. 27927 * Therefore, we'd better check if a device supports it 27928 * before sending it. 27929 */ 27930 un->un_f_check_start_stop = TRUE; 27931 27932 /* 27933 * support eject media ioctl: 27934 * FDEJECT, DKIOCEJECT, CDROMEJECT 27935 */ 27936 un->un_f_eject_media_supported = TRUE; 27937 27938 /* 27939 * Because many removable-media devices don't support 27940 * LOG_SENSE, we couldn't use this command to check if 27941 * a removable media device support power-management. 27942 * We assume that they support power-management via 27943 * START_STOP_UNIT command and can be spun up and down 27944 * without limitations. 27945 */ 27946 un->un_f_pm_supported = TRUE; 27947 27948 /* 27949 * Need to create a zero length (Boolean) property 27950 * removable-media for the removable media devices. 27951 * Note that the return value of the property is not being 27952 * checked, since if unable to create the property 27953 * then do not want the attach to fail altogether. Consistent 27954 * with other property creation in attach. 27955 */ 27956 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 27957 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 27958 27959 } else { 27960 /* 27961 * create device ID for device 27962 */ 27963 un->un_f_devid_supported = TRUE; 27964 27965 /* 27966 * Spin up non-removable-media devices once it is attached 27967 */ 27968 un->un_f_attach_spinup = TRUE; 27969 27970 /* 27971 * According to SCSI specification, Sense data has two kinds of 27972 * format: fixed format, and descriptor format. At present, we 27973 * don't support descriptor format sense data for removable 27974 * media. 27975 */ 27976 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 27977 un->un_f_descr_format_supported = TRUE; 27978 } 27979 27980 /* 27981 * kstats are created only for non-removable media devices. 27982 * 27983 * Set this in sd.conf to 0 in order to disable kstats. The 27984 * default is 1, so they are enabled by default. 27985 */ 27986 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 27987 SD_DEVINFO(un), DDI_PROP_DONTPASS, 27988 "enable-partition-kstats", 1)); 27989 27990 /* 27991 * Check if HBA has set the "pm-capable" property. 27992 * If "pm-capable" exists and is non-zero then we can 27993 * power manage the device without checking the start/stop 27994 * cycle count log sense page. 27995 * 27996 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 27997 * then we should not power manage the device. 27998 * 27999 * If "pm-capable" doesn't exist then pm_capable_prop will 28000 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 28001 * sd will check the start/stop cycle count log sense page 28002 * and power manage the device if the cycle count limit has 28003 * not been exceeded. 28004 */ 28005 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 28006 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 28007 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 28008 un->un_f_log_sense_supported = TRUE; 28009 } else { 28010 /* 28011 * pm-capable property exists. 28012 * 28013 * Convert "TRUE" values for pm_capable_prop to 28014 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 28015 * later. "TRUE" values are any values except 28016 * SD_PM_CAPABLE_FALSE (0) and 28017 * SD_PM_CAPABLE_UNDEFINED (-1) 28018 */ 28019 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 28020 un->un_f_log_sense_supported = FALSE; 28021 } else { 28022 un->un_f_pm_supported = TRUE; 28023 } 28024 28025 SD_INFO(SD_LOG_ATTACH_DETACH, un, 28026 "sd_unit_attach: un:0x%p pm-capable " 28027 "property set to %d.\n", un, un->un_f_pm_supported); 28028 } 28029 } 28030 28031 if (un->un_f_is_hotpluggable) { 28032 28033 /* 28034 * Have to watch hotpluggable devices as well, since 28035 * that's the only way for userland applications to 28036 * detect hot removal while device is busy/mounted. 28037 */ 28038 un->un_f_monitor_media_state = TRUE; 28039 28040 un->un_f_check_start_stop = TRUE; 28041 28042 } 28043 } 28044 28045 /* 28046 * sd_tg_rdwr: 28047 * Provides rdwr access for cmlb via sd_tgops. The start_block is 28048 * in sys block size, req_length in bytes. 28049 * 28050 */ 28051 static int 28052 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 28053 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 28054 { 28055 struct sd_lun *un; 28056 int path_flag = (int)(uintptr_t)tg_cookie; 28057 char *dkl = NULL; 28058 diskaddr_t real_addr = start_block; 28059 diskaddr_t first_byte, end_block; 28060 28061 size_t buffer_size = reqlength; 28062 int rval; 28063 diskaddr_t cap; 28064 uint32_t lbasize; 28065 28066 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 28067 if (un == NULL) 28068 return (ENXIO); 28069 28070 if (cmd != TG_READ && cmd != TG_WRITE) 28071 return (EINVAL); 28072 28073 mutex_enter(SD_MUTEX(un)); 28074 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 28075 mutex_exit(SD_MUTEX(un)); 28076 rval = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 28077 &lbasize, path_flag); 28078 if (rval != 0) 28079 return (rval); 28080 mutex_enter(SD_MUTEX(un)); 28081 sd_update_block_info(un, lbasize, cap); 28082 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 28083 mutex_exit(SD_MUTEX(un)); 28084 return (EIO); 28085 } 28086 } 28087 28088 if (NOT_DEVBSIZE(un)) { 28089 /* 28090 * sys_blocksize != tgt_blocksize, need to re-adjust 28091 * blkno and save the index to beginning of dk_label 28092 */ 28093 first_byte = SD_SYSBLOCKS2BYTES(un, start_block); 28094 real_addr = first_byte / un->un_tgt_blocksize; 28095 28096 end_block = (first_byte + reqlength + 28097 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 28098 28099 /* round up buffer size to multiple of target block size */ 28100 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 28101 28102 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 28103 "label_addr: 0x%x allocation size: 0x%x\n", 28104 real_addr, buffer_size); 28105 28106 if (((first_byte % un->un_tgt_blocksize) != 0) || 28107 (reqlength % un->un_tgt_blocksize) != 0) 28108 /* the request is not aligned */ 28109 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 28110 } 28111 28112 /* 28113 * The MMC standard allows READ CAPACITY to be 28114 * inaccurate by a bounded amount (in the interest of 28115 * response latency). As a result, failed READs are 28116 * commonplace (due to the reading of metadata and not 28117 * data). Depending on the per-Vendor/drive Sense data, 28118 * the failed READ can cause many (unnecessary) retries. 28119 */ 28120 28121 if (ISCD(un) && (cmd == TG_READ) && 28122 (un->un_f_blockcount_is_valid == TRUE) && 28123 ((start_block == (un->un_blockcount - 1))|| 28124 (start_block == (un->un_blockcount - 2)))) { 28125 path_flag = SD_PATH_DIRECT_PRIORITY; 28126 } 28127 28128 mutex_exit(SD_MUTEX(un)); 28129 if (cmd == TG_READ) { 28130 rval = sd_send_scsi_READ(un, (dkl != NULL)? dkl: bufaddr, 28131 buffer_size, real_addr, path_flag); 28132 if (dkl != NULL) 28133 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 28134 real_addr), bufaddr, reqlength); 28135 } else { 28136 if (dkl) { 28137 rval = sd_send_scsi_READ(un, dkl, buffer_size, 28138 real_addr, path_flag); 28139 if (rval) { 28140 kmem_free(dkl, buffer_size); 28141 return (rval); 28142 } 28143 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 28144 real_addr), reqlength); 28145 } 28146 rval = sd_send_scsi_WRITE(un, (dkl != NULL)? dkl: bufaddr, 28147 buffer_size, real_addr, path_flag); 28148 } 28149 28150 if (dkl != NULL) 28151 kmem_free(dkl, buffer_size); 28152 28153 return (rval); 28154 } 28155 28156 28157 static int 28158 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 28159 { 28160 28161 struct sd_lun *un; 28162 diskaddr_t cap; 28163 uint32_t lbasize; 28164 int path_flag = (int)(uintptr_t)tg_cookie; 28165 int ret = 0; 28166 28167 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 28168 if (un == NULL) 28169 return (ENXIO); 28170 28171 switch (cmd) { 28172 case TG_GETPHYGEOM: 28173 case TG_GETVIRTGEOM: 28174 case TG_GETCAPACITY: 28175 case TG_GETBLOCKSIZE: 28176 mutex_enter(SD_MUTEX(un)); 28177 28178 if ((un->un_f_blockcount_is_valid == TRUE) && 28179 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 28180 cap = un->un_blockcount; 28181 lbasize = un->un_tgt_blocksize; 28182 mutex_exit(SD_MUTEX(un)); 28183 } else { 28184 mutex_exit(SD_MUTEX(un)); 28185 ret = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 28186 &lbasize, path_flag); 28187 if (ret != 0) 28188 return (ret); 28189 mutex_enter(SD_MUTEX(un)); 28190 sd_update_block_info(un, lbasize, cap); 28191 if ((un->un_f_blockcount_is_valid == FALSE) || 28192 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 28193 mutex_exit(SD_MUTEX(un)); 28194 return (EIO); 28195 } 28196 mutex_exit(SD_MUTEX(un)); 28197 } 28198 28199 if (cmd == TG_GETCAPACITY) { 28200 *(diskaddr_t *)arg = cap; 28201 return (0); 28202 } 28203 28204 if (cmd == TG_GETBLOCKSIZE) { 28205 *(uint32_t *)arg = lbasize; 28206 return (0); 28207 } 28208 28209 if (cmd == TG_GETPHYGEOM) 28210 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 28211 cap, lbasize, path_flag); 28212 else 28213 /* TG_GETVIRTGEOM */ 28214 ret = sd_get_virtual_geometry(un, 28215 (cmlb_geom_t *)arg, cap, lbasize); 28216 28217 return (ret); 28218 28219 case TG_GETATTR: 28220 mutex_enter(SD_MUTEX(un)); 28221 ((tg_attribute_t *)arg)->media_is_writable = 28222 un->un_f_mmc_writable_media; 28223 mutex_exit(SD_MUTEX(un)); 28224 return (0); 28225 default: 28226 return (ENOTTY); 28227 28228 } 28229 28230 } 28231