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 2007 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 68 69 /* 70 * Loadable module info. 71 */ 72 #if (defined(__fibre)) 73 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 74 char _depends_on[] = "misc/scsi misc/cmlb drv/fcp"; 75 #else 76 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 77 char _depends_on[] = "misc/scsi misc/cmlb"; 78 #endif 79 80 /* 81 * Define the interconnect type, to allow the driver to distinguish 82 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 83 * 84 * This is really for backward compatibility. In the future, the driver 85 * should actually check the "interconnect-type" property as reported by 86 * the HBA; however at present this property is not defined by all HBAs, 87 * so we will use this #define (1) to permit the driver to run in 88 * backward-compatibility mode; and (2) to print a notification message 89 * if an FC HBA does not support the "interconnect-type" property. The 90 * behavior of the driver will be to assume parallel SCSI behaviors unless 91 * the "interconnect-type" property is defined by the HBA **AND** has a 92 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 93 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 94 * Channel behaviors (as per the old ssd). (Note that the 95 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 96 * will result in the driver assuming parallel SCSI behaviors.) 97 * 98 * (see common/sys/scsi/impl/services.h) 99 * 100 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 101 * since some FC HBAs may already support that, and there is some code in 102 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 103 * default would confuse that code, and besides things should work fine 104 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 105 * "interconnect_type" property. 106 * 107 */ 108 #if (defined(__fibre)) 109 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 110 #else 111 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 112 #endif 113 114 /* 115 * The name of the driver, established from the module name in _init. 116 */ 117 static char *sd_label = NULL; 118 119 /* 120 * Driver name is unfortunately prefixed on some driver.conf properties. 121 */ 122 #if (defined(__fibre)) 123 #define sd_max_xfer_size ssd_max_xfer_size 124 #define sd_config_list ssd_config_list 125 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 126 static char *sd_config_list = "ssd-config-list"; 127 #else 128 static char *sd_max_xfer_size = "sd_max_xfer_size"; 129 static char *sd_config_list = "sd-config-list"; 130 #endif 131 132 /* 133 * Driver global variables 134 */ 135 136 #if (defined(__fibre)) 137 /* 138 * These #defines are to avoid namespace collisions that occur because this 139 * code is currently used to compile two separate driver modules: sd and ssd. 140 * All global variables need to be treated this way (even if declared static) 141 * in order to allow the debugger to resolve the names properly. 142 * It is anticipated that in the near future the ssd module will be obsoleted, 143 * at which time this namespace issue should go away. 144 */ 145 #define sd_state ssd_state 146 #define sd_io_time ssd_io_time 147 #define sd_failfast_enable ssd_failfast_enable 148 #define sd_ua_retry_count ssd_ua_retry_count 149 #define sd_report_pfa ssd_report_pfa 150 #define sd_max_throttle ssd_max_throttle 151 #define sd_min_throttle ssd_min_throttle 152 #define sd_rot_delay ssd_rot_delay 153 154 #define sd_retry_on_reservation_conflict \ 155 ssd_retry_on_reservation_conflict 156 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 157 #define sd_resv_conflict_name ssd_resv_conflict_name 158 159 #define sd_component_mask ssd_component_mask 160 #define sd_level_mask ssd_level_mask 161 #define sd_debug_un ssd_debug_un 162 #define sd_error_level ssd_error_level 163 164 #define sd_xbuf_active_limit ssd_xbuf_active_limit 165 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 166 167 #define sd_tr ssd_tr 168 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 169 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 170 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 171 #define sd_check_media_time ssd_check_media_time 172 #define sd_wait_cmds_complete ssd_wait_cmds_complete 173 #define sd_label_mutex ssd_label_mutex 174 #define sd_detach_mutex ssd_detach_mutex 175 #define sd_log_buf ssd_log_buf 176 #define sd_log_mutex ssd_log_mutex 177 178 #define sd_disk_table ssd_disk_table 179 #define sd_disk_table_size ssd_disk_table_size 180 #define sd_sense_mutex ssd_sense_mutex 181 #define sd_cdbtab ssd_cdbtab 182 183 #define sd_cb_ops ssd_cb_ops 184 #define sd_ops ssd_ops 185 #define sd_additional_codes ssd_additional_codes 186 #define sd_tgops ssd_tgops 187 188 #define sd_minor_data ssd_minor_data 189 #define sd_minor_data_efi ssd_minor_data_efi 190 191 #define sd_tq ssd_tq 192 #define sd_wmr_tq ssd_wmr_tq 193 #define sd_taskq_name ssd_taskq_name 194 #define sd_wmr_taskq_name ssd_wmr_taskq_name 195 #define sd_taskq_minalloc ssd_taskq_minalloc 196 #define sd_taskq_maxalloc ssd_taskq_maxalloc 197 198 #define sd_dump_format_string ssd_dump_format_string 199 200 #define sd_iostart_chain ssd_iostart_chain 201 #define sd_iodone_chain ssd_iodone_chain 202 203 #define sd_pm_idletime ssd_pm_idletime 204 205 #define sd_force_pm_supported ssd_force_pm_supported 206 207 #define sd_dtype_optical_bind ssd_dtype_optical_bind 208 209 #endif 210 211 212 #ifdef SDDEBUG 213 int sd_force_pm_supported = 0; 214 #endif /* SDDEBUG */ 215 216 void *sd_state = NULL; 217 int sd_io_time = SD_IO_TIME; 218 int sd_failfast_enable = 1; 219 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 220 int sd_report_pfa = 1; 221 int sd_max_throttle = SD_MAX_THROTTLE; 222 int sd_min_throttle = SD_MIN_THROTTLE; 223 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 224 int sd_qfull_throttle_enable = TRUE; 225 226 int sd_retry_on_reservation_conflict = 1; 227 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 228 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 229 230 static int sd_dtype_optical_bind = -1; 231 232 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 233 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 234 235 /* 236 * Global data for debug logging. To enable debug printing, sd_component_mask 237 * and sd_level_mask should be set to the desired bit patterns as outlined in 238 * sddef.h. 239 */ 240 uint_t sd_component_mask = 0x0; 241 uint_t sd_level_mask = 0x0; 242 struct sd_lun *sd_debug_un = NULL; 243 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 244 245 /* Note: these may go away in the future... */ 246 static uint32_t sd_xbuf_active_limit = 512; 247 static uint32_t sd_xbuf_reserve_limit = 16; 248 249 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 250 251 /* 252 * Timer value used to reset the throttle after it has been reduced 253 * (typically in response to TRAN_BUSY or STATUS_QFULL) 254 */ 255 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 256 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 257 258 /* 259 * Interval value associated with the media change scsi watch. 260 */ 261 static int sd_check_media_time = 3000000; 262 263 /* 264 * Wait value used for in progress operations during a DDI_SUSPEND 265 */ 266 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 267 268 /* 269 * sd_label_mutex protects a static buffer used in the disk label 270 * component of the driver 271 */ 272 static kmutex_t sd_label_mutex; 273 274 /* 275 * sd_detach_mutex protects un_layer_count, un_detach_count, and 276 * un_opens_in_progress in the sd_lun structure. 277 */ 278 static kmutex_t sd_detach_mutex; 279 280 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 281 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 282 283 /* 284 * Global buffer and mutex for debug logging 285 */ 286 static char sd_log_buf[1024]; 287 static kmutex_t sd_log_mutex; 288 289 /* 290 * Structs and globals for recording attached lun information. 291 * This maintains a chain. Each node in the chain represents a SCSI controller. 292 * The structure records the number of luns attached to each target connected 293 * with the controller. 294 * For parallel scsi device only. 295 */ 296 struct sd_scsi_hba_tgt_lun { 297 struct sd_scsi_hba_tgt_lun *next; 298 dev_info_t *pdip; 299 int nlun[NTARGETS_WIDE]; 300 }; 301 302 /* 303 * Flag to indicate the lun is attached or detached 304 */ 305 #define SD_SCSI_LUN_ATTACH 0 306 #define SD_SCSI_LUN_DETACH 1 307 308 static kmutex_t sd_scsi_target_lun_mutex; 309 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 310 311 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 312 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 313 314 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 315 sd_scsi_target_lun_head)) 316 317 /* 318 * "Smart" Probe Caching structs, globals, #defines, etc. 319 * For parallel scsi and non-self-identify device only. 320 */ 321 322 /* 323 * The following resources and routines are implemented to support 324 * "smart" probing, which caches the scsi_probe() results in an array, 325 * in order to help avoid long probe times. 326 */ 327 struct sd_scsi_probe_cache { 328 struct sd_scsi_probe_cache *next; 329 dev_info_t *pdip; 330 int cache[NTARGETS_WIDE]; 331 }; 332 333 static kmutex_t sd_scsi_probe_cache_mutex; 334 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 335 336 /* 337 * Really we only need protection on the head of the linked list, but 338 * better safe than sorry. 339 */ 340 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 341 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 342 343 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 344 sd_scsi_probe_cache_head)) 345 346 347 /* 348 * Vendor specific data name property declarations 349 */ 350 351 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 352 353 static sd_tunables seagate_properties = { 354 SEAGATE_THROTTLE_VALUE, 355 0, 356 0, 357 0, 358 0, 359 0, 360 0, 361 0, 362 0 363 }; 364 365 366 static sd_tunables fujitsu_properties = { 367 FUJITSU_THROTTLE_VALUE, 368 0, 369 0, 370 0, 371 0, 372 0, 373 0, 374 0, 375 0 376 }; 377 378 static sd_tunables ibm_properties = { 379 IBM_THROTTLE_VALUE, 380 0, 381 0, 382 0, 383 0, 384 0, 385 0, 386 0, 387 0 388 }; 389 390 static sd_tunables purple_properties = { 391 PURPLE_THROTTLE_VALUE, 392 0, 393 0, 394 PURPLE_BUSY_RETRIES, 395 PURPLE_RESET_RETRY_COUNT, 396 PURPLE_RESERVE_RELEASE_TIME, 397 0, 398 0, 399 0 400 }; 401 402 static sd_tunables sve_properties = { 403 SVE_THROTTLE_VALUE, 404 0, 405 0, 406 SVE_BUSY_RETRIES, 407 SVE_RESET_RETRY_COUNT, 408 SVE_RESERVE_RELEASE_TIME, 409 SVE_MIN_THROTTLE_VALUE, 410 SVE_DISKSORT_DISABLED_FLAG, 411 0 412 }; 413 414 static sd_tunables maserati_properties = { 415 0, 416 0, 417 0, 418 0, 419 0, 420 0, 421 0, 422 MASERATI_DISKSORT_DISABLED_FLAG, 423 MASERATI_LUN_RESET_ENABLED_FLAG 424 }; 425 426 static sd_tunables pirus_properties = { 427 PIRUS_THROTTLE_VALUE, 428 0, 429 PIRUS_NRR_COUNT, 430 PIRUS_BUSY_RETRIES, 431 PIRUS_RESET_RETRY_COUNT, 432 0, 433 PIRUS_MIN_THROTTLE_VALUE, 434 PIRUS_DISKSORT_DISABLED_FLAG, 435 PIRUS_LUN_RESET_ENABLED_FLAG 436 }; 437 438 #endif 439 440 #if (defined(__sparc) && !defined(__fibre)) || \ 441 (defined(__i386) || defined(__amd64)) 442 443 444 static sd_tunables elite_properties = { 445 ELITE_THROTTLE_VALUE, 446 0, 447 0, 448 0, 449 0, 450 0, 451 0, 452 0, 453 0 454 }; 455 456 static sd_tunables st31200n_properties = { 457 ST31200N_THROTTLE_VALUE, 458 0, 459 0, 460 0, 461 0, 462 0, 463 0, 464 0, 465 0 466 }; 467 468 #endif /* Fibre or not */ 469 470 static sd_tunables lsi_properties_scsi = { 471 LSI_THROTTLE_VALUE, 472 0, 473 LSI_NOTREADY_RETRIES, 474 0, 475 0, 476 0, 477 0, 478 0, 479 0 480 }; 481 482 static sd_tunables symbios_properties = { 483 SYMBIOS_THROTTLE_VALUE, 484 0, 485 SYMBIOS_NOTREADY_RETRIES, 486 0, 487 0, 488 0, 489 0, 490 0, 491 0 492 }; 493 494 static sd_tunables lsi_properties = { 495 0, 496 0, 497 LSI_NOTREADY_RETRIES, 498 0, 499 0, 500 0, 501 0, 502 0, 503 0 504 }; 505 506 static sd_tunables lsi_oem_properties = { 507 0, 508 0, 509 LSI_OEM_NOTREADY_RETRIES, 510 0, 511 0, 512 0, 513 0, 514 0, 515 0 516 }; 517 518 519 520 #if (defined(SD_PROP_TST)) 521 522 #define SD_TST_CTYPE_VAL CTYPE_CDROM 523 #define SD_TST_THROTTLE_VAL 16 524 #define SD_TST_NOTREADY_VAL 12 525 #define SD_TST_BUSY_VAL 60 526 #define SD_TST_RST_RETRY_VAL 36 527 #define SD_TST_RSV_REL_TIME 60 528 529 static sd_tunables tst_properties = { 530 SD_TST_THROTTLE_VAL, 531 SD_TST_CTYPE_VAL, 532 SD_TST_NOTREADY_VAL, 533 SD_TST_BUSY_VAL, 534 SD_TST_RST_RETRY_VAL, 535 SD_TST_RSV_REL_TIME, 536 0, 537 0, 538 0 539 }; 540 #endif 541 542 /* This is similar to the ANSI toupper implementation */ 543 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 544 545 /* 546 * Static Driver Configuration Table 547 * 548 * This is the table of disks which need throttle adjustment (or, perhaps 549 * something else as defined by the flags at a future time.) device_id 550 * is a string consisting of concatenated vid (vendor), pid (product/model) 551 * and revision strings as defined in the scsi_inquiry structure. Offsets of 552 * the parts of the string are as defined by the sizes in the scsi_inquiry 553 * structure. Device type is searched as far as the device_id string is 554 * defined. Flags defines which values are to be set in the driver from the 555 * properties list. 556 * 557 * Entries below which begin and end with a "*" are a special case. 558 * These do not have a specific vendor, and the string which follows 559 * can appear anywhere in the 16 byte PID portion of the inquiry data. 560 * 561 * Entries below which begin and end with a " " (blank) are a special 562 * case. The comparison function will treat multiple consecutive blanks 563 * as equivalent to a single blank. For example, this causes a 564 * sd_disk_table entry of " NEC CDROM " to match a device's id string 565 * of "NEC CDROM". 566 * 567 * Note: The MD21 controller type has been obsoleted. 568 * ST318202F is a Legacy device 569 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 570 * made with an FC connection. The entries here are a legacy. 571 */ 572 static sd_disk_config_t sd_disk_table[] = { 573 #if defined(__fibre) || defined(__i386) || defined(__amd64) 574 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 575 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 576 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 577 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 578 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 579 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 580 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 581 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 582 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 583 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 584 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 585 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 586 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 587 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 588 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 589 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 590 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 591 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 592 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 593 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 594 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 595 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 596 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 597 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 598 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 599 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 600 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 601 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 602 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 603 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 604 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 605 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 606 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 607 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 608 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 609 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 610 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 611 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 612 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 613 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 614 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 615 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 616 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 617 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 618 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 619 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 620 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 621 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 622 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 623 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 624 { "SUN T3", SD_CONF_BSET_THROTTLE | 625 SD_CONF_BSET_BSY_RETRY_COUNT| 626 SD_CONF_BSET_RST_RETRIES| 627 SD_CONF_BSET_RSV_REL_TIME, 628 &purple_properties }, 629 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 630 SD_CONF_BSET_BSY_RETRY_COUNT| 631 SD_CONF_BSET_RST_RETRIES| 632 SD_CONF_BSET_RSV_REL_TIME| 633 SD_CONF_BSET_MIN_THROTTLE| 634 SD_CONF_BSET_DISKSORT_DISABLED, 635 &sve_properties }, 636 { "SUN T4", SD_CONF_BSET_THROTTLE | 637 SD_CONF_BSET_BSY_RETRY_COUNT| 638 SD_CONF_BSET_RST_RETRIES| 639 SD_CONF_BSET_RSV_REL_TIME, 640 &purple_properties }, 641 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 642 SD_CONF_BSET_LUN_RESET_ENABLED, 643 &maserati_properties }, 644 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 645 SD_CONF_BSET_NRR_COUNT| 646 SD_CONF_BSET_BSY_RETRY_COUNT| 647 SD_CONF_BSET_RST_RETRIES| 648 SD_CONF_BSET_MIN_THROTTLE| 649 SD_CONF_BSET_DISKSORT_DISABLED| 650 SD_CONF_BSET_LUN_RESET_ENABLED, 651 &pirus_properties }, 652 { "SUN SE6940", 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 StorageTek 6920", 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 6940", 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 PSX1000", 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 SE6330", 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 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 693 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 694 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 695 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 696 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 697 #endif /* fibre or NON-sparc platforms */ 698 #if ((defined(__sparc) && !defined(__fibre)) ||\ 699 (defined(__i386) || defined(__amd64))) 700 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 701 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 702 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 703 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 704 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 705 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 706 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 707 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 708 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 709 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 710 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 711 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 712 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 713 &symbios_properties }, 714 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 715 &lsi_properties_scsi }, 716 #if defined(__i386) || defined(__amd64) 717 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 718 | SD_CONF_BSET_READSUB_BCD 719 | SD_CONF_BSET_READ_TOC_ADDR_BCD 720 | SD_CONF_BSET_NO_READ_HEADER 721 | SD_CONF_BSET_READ_CD_XD4), NULL }, 722 723 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 724 | SD_CONF_BSET_READSUB_BCD 725 | SD_CONF_BSET_READ_TOC_ADDR_BCD 726 | SD_CONF_BSET_NO_READ_HEADER 727 | SD_CONF_BSET_READ_CD_XD4), NULL }, 728 #endif /* __i386 || __amd64 */ 729 #endif /* sparc NON-fibre or NON-sparc platforms */ 730 731 #if (defined(SD_PROP_TST)) 732 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 733 | SD_CONF_BSET_CTYPE 734 | SD_CONF_BSET_NRR_COUNT 735 | SD_CONF_BSET_FAB_DEVID 736 | SD_CONF_BSET_NOCACHE 737 | SD_CONF_BSET_BSY_RETRY_COUNT 738 | SD_CONF_BSET_PLAYMSF_BCD 739 | SD_CONF_BSET_READSUB_BCD 740 | SD_CONF_BSET_READ_TOC_TRK_BCD 741 | SD_CONF_BSET_READ_TOC_ADDR_BCD 742 | SD_CONF_BSET_NO_READ_HEADER 743 | SD_CONF_BSET_READ_CD_XD4 744 | SD_CONF_BSET_RST_RETRIES 745 | SD_CONF_BSET_RSV_REL_TIME 746 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 747 #endif 748 }; 749 750 static const int sd_disk_table_size = 751 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 752 753 754 755 #define SD_INTERCONNECT_PARALLEL 0 756 #define SD_INTERCONNECT_FABRIC 1 757 #define SD_INTERCONNECT_FIBRE 2 758 #define SD_INTERCONNECT_SSA 3 759 #define SD_INTERCONNECT_SATA 4 760 #define SD_IS_PARALLEL_SCSI(un) \ 761 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 762 #define SD_IS_SERIAL(un) \ 763 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 764 765 /* 766 * Definitions used by device id registration routines 767 */ 768 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 769 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 770 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 771 772 static kmutex_t sd_sense_mutex = {0}; 773 774 /* 775 * Macros for updates of the driver state 776 */ 777 #define New_state(un, s) \ 778 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 779 #define Restore_state(un) \ 780 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 781 782 static struct sd_cdbinfo sd_cdbtab[] = { 783 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 784 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 785 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 786 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 787 }; 788 789 /* 790 * Specifies the number of seconds that must have elapsed since the last 791 * cmd. has completed for a device to be declared idle to the PM framework. 792 */ 793 static int sd_pm_idletime = 1; 794 795 /* 796 * Internal function prototypes 797 */ 798 799 #if (defined(__fibre)) 800 /* 801 * These #defines are to avoid namespace collisions that occur because this 802 * code is currently used to compile two separate driver modules: sd and ssd. 803 * All function names need to be treated this way (even if declared static) 804 * in order to allow the debugger to resolve the names properly. 805 * It is anticipated that in the near future the ssd module will be obsoleted, 806 * at which time this ugliness should go away. 807 */ 808 #define sd_log_trace ssd_log_trace 809 #define sd_log_info ssd_log_info 810 #define sd_log_err ssd_log_err 811 #define sdprobe ssdprobe 812 #define sdinfo ssdinfo 813 #define sd_prop_op ssd_prop_op 814 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 815 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 816 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 817 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 818 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 819 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 820 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 821 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 822 #define sd_spin_up_unit ssd_spin_up_unit 823 #define sd_enable_descr_sense ssd_enable_descr_sense 824 #define sd_reenable_dsense_task ssd_reenable_dsense_task 825 #define sd_set_mmc_caps ssd_set_mmc_caps 826 #define sd_read_unit_properties ssd_read_unit_properties 827 #define sd_process_sdconf_file ssd_process_sdconf_file 828 #define sd_process_sdconf_table ssd_process_sdconf_table 829 #define sd_sdconf_id_match ssd_sdconf_id_match 830 #define sd_blank_cmp ssd_blank_cmp 831 #define sd_chk_vers1_data ssd_chk_vers1_data 832 #define sd_set_vers1_properties ssd_set_vers1_properties 833 834 #define sd_get_physical_geometry ssd_get_physical_geometry 835 #define sd_get_virtual_geometry ssd_get_virtual_geometry 836 #define sd_update_block_info ssd_update_block_info 837 #define sd_register_devid ssd_register_devid 838 #define sd_get_devid ssd_get_devid 839 #define sd_create_devid ssd_create_devid 840 #define sd_write_deviceid ssd_write_deviceid 841 #define sd_check_vpd_page_support ssd_check_vpd_page_support 842 #define sd_setup_pm ssd_setup_pm 843 #define sd_create_pm_components ssd_create_pm_components 844 #define sd_ddi_suspend ssd_ddi_suspend 845 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 846 #define sd_ddi_resume ssd_ddi_resume 847 #define sd_ddi_pm_resume ssd_ddi_pm_resume 848 #define sdpower ssdpower 849 #define sdattach ssdattach 850 #define sddetach ssddetach 851 #define sd_unit_attach ssd_unit_attach 852 #define sd_unit_detach ssd_unit_detach 853 #define sd_set_unit_attributes ssd_set_unit_attributes 854 #define sd_create_errstats ssd_create_errstats 855 #define sd_set_errstats ssd_set_errstats 856 #define sd_set_pstats ssd_set_pstats 857 #define sddump ssddump 858 #define sd_scsi_poll ssd_scsi_poll 859 #define sd_send_polled_RQS ssd_send_polled_RQS 860 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 861 #define sd_init_event_callbacks ssd_init_event_callbacks 862 #define sd_event_callback ssd_event_callback 863 #define sd_cache_control ssd_cache_control 864 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 865 #define sd_make_device ssd_make_device 866 #define sdopen ssdopen 867 #define sdclose ssdclose 868 #define sd_ready_and_valid ssd_ready_and_valid 869 #define sdmin ssdmin 870 #define sdread ssdread 871 #define sdwrite ssdwrite 872 #define sdaread ssdaread 873 #define sdawrite ssdawrite 874 #define sdstrategy ssdstrategy 875 #define sdioctl ssdioctl 876 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 877 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 878 #define sd_checksum_iostart ssd_checksum_iostart 879 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 880 #define sd_pm_iostart ssd_pm_iostart 881 #define sd_core_iostart ssd_core_iostart 882 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 883 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 884 #define sd_checksum_iodone ssd_checksum_iodone 885 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 886 #define sd_pm_iodone ssd_pm_iodone 887 #define sd_initpkt_for_buf ssd_initpkt_for_buf 888 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 889 #define sd_setup_rw_pkt ssd_setup_rw_pkt 890 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 891 #define sd_buf_iodone ssd_buf_iodone 892 #define sd_uscsi_strategy ssd_uscsi_strategy 893 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 894 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 895 #define sd_uscsi_iodone ssd_uscsi_iodone 896 #define sd_xbuf_strategy ssd_xbuf_strategy 897 #define sd_xbuf_init ssd_xbuf_init 898 #define sd_pm_entry ssd_pm_entry 899 #define sd_pm_exit ssd_pm_exit 900 901 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 902 #define sd_pm_timeout_handler ssd_pm_timeout_handler 903 904 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 905 #define sdintr ssdintr 906 #define sd_start_cmds ssd_start_cmds 907 #define sd_send_scsi_cmd ssd_send_scsi_cmd 908 #define sd_bioclone_alloc ssd_bioclone_alloc 909 #define sd_bioclone_free ssd_bioclone_free 910 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 911 #define sd_shadow_buf_free ssd_shadow_buf_free 912 #define sd_print_transport_rejected_message \ 913 ssd_print_transport_rejected_message 914 #define sd_retry_command ssd_retry_command 915 #define sd_set_retry_bp ssd_set_retry_bp 916 #define sd_send_request_sense_command ssd_send_request_sense_command 917 #define sd_start_retry_command ssd_start_retry_command 918 #define sd_start_direct_priority_command \ 919 ssd_start_direct_priority_command 920 #define sd_return_failed_command ssd_return_failed_command 921 #define sd_return_failed_command_no_restart \ 922 ssd_return_failed_command_no_restart 923 #define sd_return_command ssd_return_command 924 #define sd_sync_with_callback ssd_sync_with_callback 925 #define sdrunout ssdrunout 926 #define sd_mark_rqs_busy ssd_mark_rqs_busy 927 #define sd_mark_rqs_idle ssd_mark_rqs_idle 928 #define sd_reduce_throttle ssd_reduce_throttle 929 #define sd_restore_throttle ssd_restore_throttle 930 #define sd_print_incomplete_msg ssd_print_incomplete_msg 931 #define sd_init_cdb_limits ssd_init_cdb_limits 932 #define sd_pkt_status_good ssd_pkt_status_good 933 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 934 #define sd_pkt_status_busy ssd_pkt_status_busy 935 #define sd_pkt_status_reservation_conflict \ 936 ssd_pkt_status_reservation_conflict 937 #define sd_pkt_status_qfull ssd_pkt_status_qfull 938 #define sd_handle_request_sense ssd_handle_request_sense 939 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 940 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 941 #define sd_validate_sense_data ssd_validate_sense_data 942 #define sd_decode_sense ssd_decode_sense 943 #define sd_print_sense_msg ssd_print_sense_msg 944 #define sd_sense_key_no_sense ssd_sense_key_no_sense 945 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 946 #define sd_sense_key_not_ready ssd_sense_key_not_ready 947 #define sd_sense_key_medium_or_hardware_error \ 948 ssd_sense_key_medium_or_hardware_error 949 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 950 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 951 #define sd_sense_key_fail_command ssd_sense_key_fail_command 952 #define sd_sense_key_blank_check ssd_sense_key_blank_check 953 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 954 #define sd_sense_key_default ssd_sense_key_default 955 #define sd_print_retry_msg ssd_print_retry_msg 956 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 957 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 958 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 959 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 960 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 961 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 962 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 963 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 964 #define sd_pkt_reason_default ssd_pkt_reason_default 965 #define sd_reset_target ssd_reset_target 966 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 967 #define sd_start_stop_unit_task ssd_start_stop_unit_task 968 #define sd_taskq_create ssd_taskq_create 969 #define sd_taskq_delete ssd_taskq_delete 970 #define sd_media_change_task ssd_media_change_task 971 #define sd_handle_mchange ssd_handle_mchange 972 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 973 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 974 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 975 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 976 #define sd_send_scsi_feature_GET_CONFIGURATION \ 977 sd_send_scsi_feature_GET_CONFIGURATION 978 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 979 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 980 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 981 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 982 ssd_send_scsi_PERSISTENT_RESERVE_IN 983 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 984 ssd_send_scsi_PERSISTENT_RESERVE_OUT 985 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 986 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 987 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 988 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 989 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 990 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 991 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 992 #define sd_alloc_rqs ssd_alloc_rqs 993 #define sd_free_rqs ssd_free_rqs 994 #define sd_dump_memory ssd_dump_memory 995 #define sd_get_media_info ssd_get_media_info 996 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 997 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 998 #define sd_setup_next_xfer ssd_setup_next_xfer 999 #define sd_dkio_get_temp ssd_dkio_get_temp 1000 #define sd_check_mhd ssd_check_mhd 1001 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1002 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1003 #define sd_sname ssd_sname 1004 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1005 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1006 #define sd_take_ownership ssd_take_ownership 1007 #define sd_reserve_release ssd_reserve_release 1008 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1009 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1010 #define sd_persistent_reservation_in_read_keys \ 1011 ssd_persistent_reservation_in_read_keys 1012 #define sd_persistent_reservation_in_read_resv \ 1013 ssd_persistent_reservation_in_read_resv 1014 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1015 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1016 #define sd_mhdioc_release ssd_mhdioc_release 1017 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1018 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1019 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1020 #define sr_change_blkmode ssr_change_blkmode 1021 #define sr_change_speed ssr_change_speed 1022 #define sr_atapi_change_speed ssr_atapi_change_speed 1023 #define sr_pause_resume ssr_pause_resume 1024 #define sr_play_msf ssr_play_msf 1025 #define sr_play_trkind ssr_play_trkind 1026 #define sr_read_all_subcodes ssr_read_all_subcodes 1027 #define sr_read_subchannel ssr_read_subchannel 1028 #define sr_read_tocentry ssr_read_tocentry 1029 #define sr_read_tochdr ssr_read_tochdr 1030 #define sr_read_cdda ssr_read_cdda 1031 #define sr_read_cdxa ssr_read_cdxa 1032 #define sr_read_mode1 ssr_read_mode1 1033 #define sr_read_mode2 ssr_read_mode2 1034 #define sr_read_cd_mode2 ssr_read_cd_mode2 1035 #define sr_sector_mode ssr_sector_mode 1036 #define sr_eject ssr_eject 1037 #define sr_ejected ssr_ejected 1038 #define sr_check_wp ssr_check_wp 1039 #define sd_check_media ssd_check_media 1040 #define sd_media_watch_cb ssd_media_watch_cb 1041 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1042 #define sr_volume_ctrl ssr_volume_ctrl 1043 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1044 #define sd_log_page_supported ssd_log_page_supported 1045 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1046 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1047 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1048 #define sd_range_lock ssd_range_lock 1049 #define sd_get_range ssd_get_range 1050 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1051 #define sd_range_unlock ssd_range_unlock 1052 #define sd_read_modify_write_task ssd_read_modify_write_task 1053 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1054 1055 #define sd_iostart_chain ssd_iostart_chain 1056 #define sd_iodone_chain ssd_iodone_chain 1057 #define sd_initpkt_map ssd_initpkt_map 1058 #define sd_destroypkt_map ssd_destroypkt_map 1059 #define sd_chain_type_map ssd_chain_type_map 1060 #define sd_chain_index_map ssd_chain_index_map 1061 1062 #define sd_failfast_flushctl ssd_failfast_flushctl 1063 #define sd_failfast_flushq ssd_failfast_flushq 1064 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1065 1066 #define sd_is_lsi ssd_is_lsi 1067 #define sd_tg_rdwr ssd_tg_rdwr 1068 #define sd_tg_getinfo ssd_tg_getinfo 1069 1070 #endif /* #if (defined(__fibre)) */ 1071 1072 1073 int _init(void); 1074 int _fini(void); 1075 int _info(struct modinfo *modinfop); 1076 1077 /*PRINTFLIKE3*/ 1078 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1079 /*PRINTFLIKE3*/ 1080 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1081 /*PRINTFLIKE3*/ 1082 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1083 1084 static int sdprobe(dev_info_t *devi); 1085 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1086 void **result); 1087 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1088 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1089 1090 /* 1091 * Smart probe for parallel scsi 1092 */ 1093 static void sd_scsi_probe_cache_init(void); 1094 static void sd_scsi_probe_cache_fini(void); 1095 static void sd_scsi_clear_probe_cache(void); 1096 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1097 1098 /* 1099 * Attached luns on target for parallel scsi 1100 */ 1101 static void sd_scsi_target_lun_init(void); 1102 static void sd_scsi_target_lun_fini(void); 1103 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1104 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1105 1106 static int sd_spin_up_unit(struct sd_lun *un); 1107 #ifdef _LP64 1108 static void sd_enable_descr_sense(struct sd_lun *un); 1109 static void sd_reenable_dsense_task(void *arg); 1110 #endif /* _LP64 */ 1111 1112 static void sd_set_mmc_caps(struct sd_lun *un); 1113 1114 static void sd_read_unit_properties(struct sd_lun *un); 1115 static int sd_process_sdconf_file(struct sd_lun *un); 1116 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1117 int *data_list, sd_tunables *values); 1118 static void sd_process_sdconf_table(struct sd_lun *un); 1119 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1120 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1121 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1122 int list_len, char *dataname_ptr); 1123 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1124 sd_tunables *prop_list); 1125 1126 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1127 int reservation_flag); 1128 static int sd_get_devid(struct sd_lun *un); 1129 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1130 static int sd_write_deviceid(struct sd_lun *un); 1131 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1132 static int sd_check_vpd_page_support(struct sd_lun *un); 1133 1134 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1135 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1136 1137 static int sd_ddi_suspend(dev_info_t *devi); 1138 static int sd_ddi_pm_suspend(struct sd_lun *un); 1139 static int sd_ddi_resume(dev_info_t *devi); 1140 static int sd_ddi_pm_resume(struct sd_lun *un); 1141 static int sdpower(dev_info_t *devi, int component, int level); 1142 1143 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1144 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1145 static int sd_unit_attach(dev_info_t *devi); 1146 static int sd_unit_detach(dev_info_t *devi); 1147 1148 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1149 static void sd_create_errstats(struct sd_lun *un, int instance); 1150 static void sd_set_errstats(struct sd_lun *un); 1151 static void sd_set_pstats(struct sd_lun *un); 1152 1153 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1154 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1155 static int sd_send_polled_RQS(struct sd_lun *un); 1156 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1157 1158 #if (defined(__fibre)) 1159 /* 1160 * Event callbacks (photon) 1161 */ 1162 static void sd_init_event_callbacks(struct sd_lun *un); 1163 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1164 #endif 1165 1166 /* 1167 * Defines for sd_cache_control 1168 */ 1169 1170 #define SD_CACHE_ENABLE 1 1171 #define SD_CACHE_DISABLE 0 1172 #define SD_CACHE_NOCHANGE -1 1173 1174 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1175 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1176 static dev_t sd_make_device(dev_info_t *devi); 1177 1178 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1179 uint64_t capacity); 1180 1181 /* 1182 * Driver entry point functions. 1183 */ 1184 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1185 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1186 static int sd_ready_and_valid(struct sd_lun *un); 1187 1188 static void sdmin(struct buf *bp); 1189 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1190 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1191 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1192 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1193 1194 static int sdstrategy(struct buf *bp); 1195 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1196 1197 /* 1198 * Function prototypes for layering functions in the iostart chain. 1199 */ 1200 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1201 struct buf *bp); 1202 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1203 struct buf *bp); 1204 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1205 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1206 struct buf *bp); 1207 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1208 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1209 1210 /* 1211 * Function prototypes for layering functions in the iodone chain. 1212 */ 1213 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1214 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1215 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1216 struct buf *bp); 1217 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1218 struct buf *bp); 1219 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1220 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1221 struct buf *bp); 1222 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1223 1224 /* 1225 * Prototypes for functions to support buf(9S) based IO. 1226 */ 1227 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1228 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1229 static void sd_destroypkt_for_buf(struct buf *); 1230 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1231 struct buf *bp, int flags, 1232 int (*callback)(caddr_t), caddr_t callback_arg, 1233 diskaddr_t lba, uint32_t blockcount); 1234 #if defined(__i386) || defined(__amd64) 1235 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1236 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1237 #endif /* defined(__i386) || defined(__amd64) */ 1238 1239 /* 1240 * Prototypes for functions to support USCSI IO. 1241 */ 1242 static int sd_uscsi_strategy(struct buf *bp); 1243 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1244 static void sd_destroypkt_for_uscsi(struct buf *); 1245 1246 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1247 uchar_t chain_type, void *pktinfop); 1248 1249 static int sd_pm_entry(struct sd_lun *un); 1250 static void sd_pm_exit(struct sd_lun *un); 1251 1252 static void sd_pm_idletimeout_handler(void *arg); 1253 1254 /* 1255 * sd_core internal functions (used at the sd_core_io layer). 1256 */ 1257 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1258 static void sdintr(struct scsi_pkt *pktp); 1259 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1260 1261 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1262 enum uio_seg dataspace, int path_flag); 1263 1264 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1265 daddr_t blkno, int (*func)(struct buf *)); 1266 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1267 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1268 static void sd_bioclone_free(struct buf *bp); 1269 static void sd_shadow_buf_free(struct buf *bp); 1270 1271 static void sd_print_transport_rejected_message(struct sd_lun *un, 1272 struct sd_xbuf *xp, int code); 1273 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1274 void *arg, int code); 1275 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1276 void *arg, int code); 1277 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1278 void *arg, int code); 1279 1280 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1281 int retry_check_flag, 1282 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1283 int c), 1284 void *user_arg, int failure_code, clock_t retry_delay, 1285 void (*statp)(kstat_io_t *)); 1286 1287 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1288 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1289 1290 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1291 struct scsi_pkt *pktp); 1292 static void sd_start_retry_command(void *arg); 1293 static void sd_start_direct_priority_command(void *arg); 1294 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1295 int errcode); 1296 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1297 struct buf *bp, int errcode); 1298 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1299 static void sd_sync_with_callback(struct sd_lun *un); 1300 static int sdrunout(caddr_t arg); 1301 1302 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1303 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1304 1305 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1306 static void sd_restore_throttle(void *arg); 1307 1308 static void sd_init_cdb_limits(struct sd_lun *un); 1309 1310 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1311 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1312 1313 /* 1314 * Error handling functions 1315 */ 1316 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1317 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1318 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1319 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1320 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1321 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1322 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1323 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1324 1325 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1326 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1327 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1328 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1329 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1330 struct sd_xbuf *xp); 1331 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1332 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1333 1334 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1335 void *arg, int code); 1336 1337 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1338 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1339 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1340 uint8_t *sense_datap, 1341 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1342 static void sd_sense_key_not_ready(struct sd_lun *un, 1343 uint8_t *sense_datap, 1344 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1345 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1346 uint8_t *sense_datap, 1347 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1348 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1349 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1350 static void sd_sense_key_unit_attention(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_fail_command(struct sd_lun *un, struct buf *bp, 1354 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1355 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1356 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1357 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1358 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1359 static void sd_sense_key_default(struct sd_lun *un, 1360 uint8_t *sense_datap, 1361 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1362 1363 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1364 void *arg, int flag); 1365 1366 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1367 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1368 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1369 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1370 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1371 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1372 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1373 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1374 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1375 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1376 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1377 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1378 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1379 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1380 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1381 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1382 1383 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1384 1385 static void sd_start_stop_unit_callback(void *arg); 1386 static void sd_start_stop_unit_task(void *arg); 1387 1388 static void sd_taskq_create(void); 1389 static void sd_taskq_delete(void); 1390 static void sd_media_change_task(void *arg); 1391 1392 static int sd_handle_mchange(struct sd_lun *un); 1393 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1394 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1395 uint32_t *lbap, int path_flag); 1396 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1397 uint32_t *lbap, int path_flag); 1398 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1399 int path_flag); 1400 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1401 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1402 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1403 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1404 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1405 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1406 uchar_t usr_cmd, uchar_t *usr_bufp); 1407 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1408 struct dk_callback *dkc); 1409 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1410 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1411 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1412 uchar_t *bufaddr, uint_t buflen, int path_flag); 1413 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1414 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1415 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1416 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1417 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1418 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1419 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1420 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1421 size_t buflen, daddr_t start_block, int path_flag); 1422 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1423 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1424 path_flag) 1425 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1426 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1427 path_flag) 1428 1429 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1430 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1431 uint16_t param_ptr, int path_flag); 1432 1433 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1434 static void sd_free_rqs(struct sd_lun *un); 1435 1436 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1437 uchar_t *data, int len, int fmt); 1438 static void sd_panic_for_res_conflict(struct sd_lun *un); 1439 1440 /* 1441 * Disk Ioctl Function Prototypes 1442 */ 1443 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1444 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1445 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1446 1447 /* 1448 * Multi-host Ioctl Prototypes 1449 */ 1450 static int sd_check_mhd(dev_t dev, int interval); 1451 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1452 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1453 static char *sd_sname(uchar_t status); 1454 static void sd_mhd_resvd_recover(void *arg); 1455 static void sd_resv_reclaim_thread(); 1456 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1457 static int sd_reserve_release(dev_t dev, int cmd); 1458 static void sd_rmv_resv_reclaim_req(dev_t dev); 1459 static void sd_mhd_reset_notify_cb(caddr_t arg); 1460 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1461 mhioc_inkeys_t *usrp, int flag); 1462 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1463 mhioc_inresvs_t *usrp, int flag); 1464 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1465 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1466 static int sd_mhdioc_release(dev_t dev); 1467 static int sd_mhdioc_register_devid(dev_t dev); 1468 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1469 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1470 1471 /* 1472 * SCSI removable prototypes 1473 */ 1474 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1475 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1476 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1477 static int sr_pause_resume(dev_t dev, int mode); 1478 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1479 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1480 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1481 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1482 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1483 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1484 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1485 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1486 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1487 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1488 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1489 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1490 static int sr_eject(dev_t dev); 1491 static void sr_ejected(register struct sd_lun *un); 1492 static int sr_check_wp(dev_t dev); 1493 static int sd_check_media(dev_t dev, enum dkio_state state); 1494 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1495 static void sd_delayed_cv_broadcast(void *arg); 1496 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1497 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1498 1499 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1500 1501 /* 1502 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1503 */ 1504 static void sd_check_for_writable_cd(struct sd_lun *un, int path_flag); 1505 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1506 static void sd_wm_cache_destructor(void *wm, void *un); 1507 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1508 daddr_t endb, ushort_t typ); 1509 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1510 daddr_t endb); 1511 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1512 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1513 static void sd_read_modify_write_task(void * arg); 1514 static int 1515 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1516 struct buf **bpp); 1517 1518 1519 /* 1520 * Function prototypes for failfast support. 1521 */ 1522 static void sd_failfast_flushq(struct sd_lun *un); 1523 static int sd_failfast_flushq_callback(struct buf *bp); 1524 1525 /* 1526 * Function prototypes to check for lsi devices 1527 */ 1528 static void sd_is_lsi(struct sd_lun *un); 1529 1530 /* 1531 * Function prototypes for x86 support 1532 */ 1533 #if defined(__i386) || defined(__amd64) 1534 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1535 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1536 #endif 1537 1538 1539 /* Function prototypes for cmlb */ 1540 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1541 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1542 1543 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1544 1545 /* 1546 * Constants for failfast support: 1547 * 1548 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1549 * failfast processing being performed. 1550 * 1551 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1552 * failfast processing on all bufs with B_FAILFAST set. 1553 */ 1554 1555 #define SD_FAILFAST_INACTIVE 0 1556 #define SD_FAILFAST_ACTIVE 1 1557 1558 /* 1559 * Bitmask to control behavior of buf(9S) flushes when a transition to 1560 * the failfast state occurs. Optional bits include: 1561 * 1562 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1563 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1564 * be flushed. 1565 * 1566 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1567 * driver, in addition to the regular wait queue. This includes the xbuf 1568 * queues. When clear, only the driver's wait queue will be flushed. 1569 */ 1570 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1571 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1572 1573 /* 1574 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1575 * to flush all queues within the driver. 1576 */ 1577 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1578 1579 1580 /* 1581 * SD Testing Fault Injection 1582 */ 1583 #ifdef SD_FAULT_INJECTION 1584 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1585 static void sd_faultinjection(struct scsi_pkt *pktp); 1586 static void sd_injection_log(char *buf, struct sd_lun *un); 1587 #endif 1588 1589 /* 1590 * Device driver ops vector 1591 */ 1592 static struct cb_ops sd_cb_ops = { 1593 sdopen, /* open */ 1594 sdclose, /* close */ 1595 sdstrategy, /* strategy */ 1596 nodev, /* print */ 1597 sddump, /* dump */ 1598 sdread, /* read */ 1599 sdwrite, /* write */ 1600 sdioctl, /* ioctl */ 1601 nodev, /* devmap */ 1602 nodev, /* mmap */ 1603 nodev, /* segmap */ 1604 nochpoll, /* poll */ 1605 sd_prop_op, /* cb_prop_op */ 1606 0, /* streamtab */ 1607 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1608 CB_REV, /* cb_rev */ 1609 sdaread, /* async I/O read entry point */ 1610 sdawrite /* async I/O write entry point */ 1611 }; 1612 1613 static struct dev_ops sd_ops = { 1614 DEVO_REV, /* devo_rev, */ 1615 0, /* refcnt */ 1616 sdinfo, /* info */ 1617 nulldev, /* identify */ 1618 sdprobe, /* probe */ 1619 sdattach, /* attach */ 1620 sddetach, /* detach */ 1621 nodev, /* reset */ 1622 &sd_cb_ops, /* driver operations */ 1623 NULL, /* bus operations */ 1624 sdpower /* power */ 1625 }; 1626 1627 1628 /* 1629 * This is the loadable module wrapper. 1630 */ 1631 #include <sys/modctl.h> 1632 1633 static struct modldrv modldrv = { 1634 &mod_driverops, /* Type of module. This one is a driver */ 1635 SD_MODULE_NAME, /* Module name. */ 1636 &sd_ops /* driver ops */ 1637 }; 1638 1639 1640 static struct modlinkage modlinkage = { 1641 MODREV_1, 1642 &modldrv, 1643 NULL 1644 }; 1645 1646 static cmlb_tg_ops_t sd_tgops = { 1647 TG_DK_OPS_VERSION_1, 1648 sd_tg_rdwr, 1649 sd_tg_getinfo 1650 }; 1651 1652 static struct scsi_asq_key_strings sd_additional_codes[] = { 1653 0x81, 0, "Logical Unit is Reserved", 1654 0x85, 0, "Audio Address Not Valid", 1655 0xb6, 0, "Media Load Mechanism Failed", 1656 0xB9, 0, "Audio Play Operation Aborted", 1657 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1658 0x53, 2, "Medium removal prevented", 1659 0x6f, 0, "Authentication failed during key exchange", 1660 0x6f, 1, "Key not present", 1661 0x6f, 2, "Key not established", 1662 0x6f, 3, "Read without proper authentication", 1663 0x6f, 4, "Mismatched region to this logical unit", 1664 0x6f, 5, "Region reset count error", 1665 0xffff, 0x0, NULL 1666 }; 1667 1668 1669 /* 1670 * Struct for passing printing information for sense data messages 1671 */ 1672 struct sd_sense_info { 1673 int ssi_severity; 1674 int ssi_pfa_flag; 1675 }; 1676 1677 /* 1678 * Table of function pointers for iostart-side routines. Separate "chains" 1679 * of layered function calls are formed by placing the function pointers 1680 * sequentially in the desired order. Functions are called according to an 1681 * incrementing table index ordering. The last function in each chain must 1682 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1683 * in the sd_iodone_chain[] array. 1684 * 1685 * Note: It may seem more natural to organize both the iostart and iodone 1686 * functions together, into an array of structures (or some similar 1687 * organization) with a common index, rather than two separate arrays which 1688 * must be maintained in synchronization. The purpose of this division is 1689 * to achieve improved performance: individual arrays allows for more 1690 * effective cache line utilization on certain platforms. 1691 */ 1692 1693 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1694 1695 1696 static sd_chain_t sd_iostart_chain[] = { 1697 1698 /* Chain for buf IO for disk drive targets (PM enabled) */ 1699 sd_mapblockaddr_iostart, /* Index: 0 */ 1700 sd_pm_iostart, /* Index: 1 */ 1701 sd_core_iostart, /* Index: 2 */ 1702 1703 /* Chain for buf IO for disk drive targets (PM disabled) */ 1704 sd_mapblockaddr_iostart, /* Index: 3 */ 1705 sd_core_iostart, /* Index: 4 */ 1706 1707 /* Chain for buf IO for removable-media targets (PM enabled) */ 1708 sd_mapblockaddr_iostart, /* Index: 5 */ 1709 sd_mapblocksize_iostart, /* Index: 6 */ 1710 sd_pm_iostart, /* Index: 7 */ 1711 sd_core_iostart, /* Index: 8 */ 1712 1713 /* Chain for buf IO for removable-media targets (PM disabled) */ 1714 sd_mapblockaddr_iostart, /* Index: 9 */ 1715 sd_mapblocksize_iostart, /* Index: 10 */ 1716 sd_core_iostart, /* Index: 11 */ 1717 1718 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1719 sd_mapblockaddr_iostart, /* Index: 12 */ 1720 sd_checksum_iostart, /* Index: 13 */ 1721 sd_pm_iostart, /* Index: 14 */ 1722 sd_core_iostart, /* Index: 15 */ 1723 1724 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1725 sd_mapblockaddr_iostart, /* Index: 16 */ 1726 sd_checksum_iostart, /* Index: 17 */ 1727 sd_core_iostart, /* Index: 18 */ 1728 1729 /* Chain for USCSI commands (all targets) */ 1730 sd_pm_iostart, /* Index: 19 */ 1731 sd_core_iostart, /* Index: 20 */ 1732 1733 /* Chain for checksumming USCSI commands (all targets) */ 1734 sd_checksum_uscsi_iostart, /* Index: 21 */ 1735 sd_pm_iostart, /* Index: 22 */ 1736 sd_core_iostart, /* Index: 23 */ 1737 1738 /* Chain for "direct" USCSI commands (all targets) */ 1739 sd_core_iostart, /* Index: 24 */ 1740 1741 /* Chain for "direct priority" USCSI commands (all targets) */ 1742 sd_core_iostart, /* Index: 25 */ 1743 }; 1744 1745 /* 1746 * Macros to locate the first function of each iostart chain in the 1747 * sd_iostart_chain[] array. These are located by the index in the array. 1748 */ 1749 #define SD_CHAIN_DISK_IOSTART 0 1750 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1751 #define SD_CHAIN_RMMEDIA_IOSTART 5 1752 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1753 #define SD_CHAIN_CHKSUM_IOSTART 12 1754 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1755 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1756 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1757 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1758 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1759 1760 1761 /* 1762 * Table of function pointers for the iodone-side routines for the driver- 1763 * internal layering mechanism. The calling sequence for iodone routines 1764 * uses a decrementing table index, so the last routine called in a chain 1765 * must be at the lowest array index location for that chain. The last 1766 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1767 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1768 * of the functions in an iodone side chain must correspond to the ordering 1769 * of the iostart routines for that chain. Note that there is no iodone 1770 * side routine that corresponds to sd_core_iostart(), so there is no 1771 * entry in the table for this. 1772 */ 1773 1774 static sd_chain_t sd_iodone_chain[] = { 1775 1776 /* Chain for buf IO for disk drive targets (PM enabled) */ 1777 sd_buf_iodone, /* Index: 0 */ 1778 sd_mapblockaddr_iodone, /* Index: 1 */ 1779 sd_pm_iodone, /* Index: 2 */ 1780 1781 /* Chain for buf IO for disk drive targets (PM disabled) */ 1782 sd_buf_iodone, /* Index: 3 */ 1783 sd_mapblockaddr_iodone, /* Index: 4 */ 1784 1785 /* Chain for buf IO for removable-media targets (PM enabled) */ 1786 sd_buf_iodone, /* Index: 5 */ 1787 sd_mapblockaddr_iodone, /* Index: 6 */ 1788 sd_mapblocksize_iodone, /* Index: 7 */ 1789 sd_pm_iodone, /* Index: 8 */ 1790 1791 /* Chain for buf IO for removable-media targets (PM disabled) */ 1792 sd_buf_iodone, /* Index: 9 */ 1793 sd_mapblockaddr_iodone, /* Index: 10 */ 1794 sd_mapblocksize_iodone, /* Index: 11 */ 1795 1796 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1797 sd_buf_iodone, /* Index: 12 */ 1798 sd_mapblockaddr_iodone, /* Index: 13 */ 1799 sd_checksum_iodone, /* Index: 14 */ 1800 sd_pm_iodone, /* Index: 15 */ 1801 1802 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1803 sd_buf_iodone, /* Index: 16 */ 1804 sd_mapblockaddr_iodone, /* Index: 17 */ 1805 sd_checksum_iodone, /* Index: 18 */ 1806 1807 /* Chain for USCSI commands (non-checksum targets) */ 1808 sd_uscsi_iodone, /* Index: 19 */ 1809 sd_pm_iodone, /* Index: 20 */ 1810 1811 /* Chain for USCSI commands (checksum targets) */ 1812 sd_uscsi_iodone, /* Index: 21 */ 1813 sd_checksum_uscsi_iodone, /* Index: 22 */ 1814 sd_pm_iodone, /* Index: 22 */ 1815 1816 /* Chain for "direct" USCSI commands (all targets) */ 1817 sd_uscsi_iodone, /* Index: 24 */ 1818 1819 /* Chain for "direct priority" USCSI commands (all targets) */ 1820 sd_uscsi_iodone, /* Index: 25 */ 1821 }; 1822 1823 1824 /* 1825 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1826 * each iodone-side chain. These are located by the array index, but as the 1827 * iodone side functions are called in a decrementing-index order, the 1828 * highest index number in each chain must be specified (as these correspond 1829 * to the first function in the iodone chain that will be called by the core 1830 * at IO completion time). 1831 */ 1832 1833 #define SD_CHAIN_DISK_IODONE 2 1834 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1835 #define SD_CHAIN_RMMEDIA_IODONE 8 1836 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1837 #define SD_CHAIN_CHKSUM_IODONE 15 1838 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1839 #define SD_CHAIN_USCSI_CMD_IODONE 20 1840 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1841 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1842 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1843 1844 1845 1846 1847 /* 1848 * Array to map a layering chain index to the appropriate initpkt routine. 1849 * The redundant entries are present so that the index used for accessing 1850 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1851 * with this table as well. 1852 */ 1853 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1854 1855 static sd_initpkt_t sd_initpkt_map[] = { 1856 1857 /* Chain for buf IO for disk drive targets (PM enabled) */ 1858 sd_initpkt_for_buf, /* Index: 0 */ 1859 sd_initpkt_for_buf, /* Index: 1 */ 1860 sd_initpkt_for_buf, /* Index: 2 */ 1861 1862 /* Chain for buf IO for disk drive targets (PM disabled) */ 1863 sd_initpkt_for_buf, /* Index: 3 */ 1864 sd_initpkt_for_buf, /* Index: 4 */ 1865 1866 /* Chain for buf IO for removable-media targets (PM enabled) */ 1867 sd_initpkt_for_buf, /* Index: 5 */ 1868 sd_initpkt_for_buf, /* Index: 6 */ 1869 sd_initpkt_for_buf, /* Index: 7 */ 1870 sd_initpkt_for_buf, /* Index: 8 */ 1871 1872 /* Chain for buf IO for removable-media targets (PM disabled) */ 1873 sd_initpkt_for_buf, /* Index: 9 */ 1874 sd_initpkt_for_buf, /* Index: 10 */ 1875 sd_initpkt_for_buf, /* Index: 11 */ 1876 1877 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1878 sd_initpkt_for_buf, /* Index: 12 */ 1879 sd_initpkt_for_buf, /* Index: 13 */ 1880 sd_initpkt_for_buf, /* Index: 14 */ 1881 sd_initpkt_for_buf, /* Index: 15 */ 1882 1883 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1884 sd_initpkt_for_buf, /* Index: 16 */ 1885 sd_initpkt_for_buf, /* Index: 17 */ 1886 sd_initpkt_for_buf, /* Index: 18 */ 1887 1888 /* Chain for USCSI commands (non-checksum targets) */ 1889 sd_initpkt_for_uscsi, /* Index: 19 */ 1890 sd_initpkt_for_uscsi, /* Index: 20 */ 1891 1892 /* Chain for USCSI commands (checksum targets) */ 1893 sd_initpkt_for_uscsi, /* Index: 21 */ 1894 sd_initpkt_for_uscsi, /* Index: 22 */ 1895 sd_initpkt_for_uscsi, /* Index: 22 */ 1896 1897 /* Chain for "direct" USCSI commands (all targets) */ 1898 sd_initpkt_for_uscsi, /* Index: 24 */ 1899 1900 /* Chain for "direct priority" USCSI commands (all targets) */ 1901 sd_initpkt_for_uscsi, /* Index: 25 */ 1902 1903 }; 1904 1905 1906 /* 1907 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1908 * The redundant entries are present so that the index used for accessing 1909 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1910 * with this table as well. 1911 */ 1912 typedef void (*sd_destroypkt_t)(struct buf *); 1913 1914 static sd_destroypkt_t sd_destroypkt_map[] = { 1915 1916 /* Chain for buf IO for disk drive targets (PM enabled) */ 1917 sd_destroypkt_for_buf, /* Index: 0 */ 1918 sd_destroypkt_for_buf, /* Index: 1 */ 1919 sd_destroypkt_for_buf, /* Index: 2 */ 1920 1921 /* Chain for buf IO for disk drive targets (PM disabled) */ 1922 sd_destroypkt_for_buf, /* Index: 3 */ 1923 sd_destroypkt_for_buf, /* Index: 4 */ 1924 1925 /* Chain for buf IO for removable-media targets (PM enabled) */ 1926 sd_destroypkt_for_buf, /* Index: 5 */ 1927 sd_destroypkt_for_buf, /* Index: 6 */ 1928 sd_destroypkt_for_buf, /* Index: 7 */ 1929 sd_destroypkt_for_buf, /* Index: 8 */ 1930 1931 /* Chain for buf IO for removable-media targets (PM disabled) */ 1932 sd_destroypkt_for_buf, /* Index: 9 */ 1933 sd_destroypkt_for_buf, /* Index: 10 */ 1934 sd_destroypkt_for_buf, /* Index: 11 */ 1935 1936 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1937 sd_destroypkt_for_buf, /* Index: 12 */ 1938 sd_destroypkt_for_buf, /* Index: 13 */ 1939 sd_destroypkt_for_buf, /* Index: 14 */ 1940 sd_destroypkt_for_buf, /* Index: 15 */ 1941 1942 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1943 sd_destroypkt_for_buf, /* Index: 16 */ 1944 sd_destroypkt_for_buf, /* Index: 17 */ 1945 sd_destroypkt_for_buf, /* Index: 18 */ 1946 1947 /* Chain for USCSI commands (non-checksum targets) */ 1948 sd_destroypkt_for_uscsi, /* Index: 19 */ 1949 sd_destroypkt_for_uscsi, /* Index: 20 */ 1950 1951 /* Chain for USCSI commands (checksum targets) */ 1952 sd_destroypkt_for_uscsi, /* Index: 21 */ 1953 sd_destroypkt_for_uscsi, /* Index: 22 */ 1954 sd_destroypkt_for_uscsi, /* Index: 22 */ 1955 1956 /* Chain for "direct" USCSI commands (all targets) */ 1957 sd_destroypkt_for_uscsi, /* Index: 24 */ 1958 1959 /* Chain for "direct priority" USCSI commands (all targets) */ 1960 sd_destroypkt_for_uscsi, /* Index: 25 */ 1961 1962 }; 1963 1964 1965 1966 /* 1967 * Array to map a layering chain index to the appropriate chain "type". 1968 * The chain type indicates a specific property/usage of the chain. 1969 * The redundant entries are present so that the index used for accessing 1970 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1971 * with this table as well. 1972 */ 1973 1974 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 1975 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 1976 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 1977 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 1978 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 1979 /* (for error recovery) */ 1980 1981 static int sd_chain_type_map[] = { 1982 1983 /* Chain for buf IO for disk drive targets (PM enabled) */ 1984 SD_CHAIN_BUFIO, /* Index: 0 */ 1985 SD_CHAIN_BUFIO, /* Index: 1 */ 1986 SD_CHAIN_BUFIO, /* Index: 2 */ 1987 1988 /* Chain for buf IO for disk drive targets (PM disabled) */ 1989 SD_CHAIN_BUFIO, /* Index: 3 */ 1990 SD_CHAIN_BUFIO, /* Index: 4 */ 1991 1992 /* Chain for buf IO for removable-media targets (PM enabled) */ 1993 SD_CHAIN_BUFIO, /* Index: 5 */ 1994 SD_CHAIN_BUFIO, /* Index: 6 */ 1995 SD_CHAIN_BUFIO, /* Index: 7 */ 1996 SD_CHAIN_BUFIO, /* Index: 8 */ 1997 1998 /* Chain for buf IO for removable-media targets (PM disabled) */ 1999 SD_CHAIN_BUFIO, /* Index: 9 */ 2000 SD_CHAIN_BUFIO, /* Index: 10 */ 2001 SD_CHAIN_BUFIO, /* Index: 11 */ 2002 2003 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2004 SD_CHAIN_BUFIO, /* Index: 12 */ 2005 SD_CHAIN_BUFIO, /* Index: 13 */ 2006 SD_CHAIN_BUFIO, /* Index: 14 */ 2007 SD_CHAIN_BUFIO, /* Index: 15 */ 2008 2009 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2010 SD_CHAIN_BUFIO, /* Index: 16 */ 2011 SD_CHAIN_BUFIO, /* Index: 17 */ 2012 SD_CHAIN_BUFIO, /* Index: 18 */ 2013 2014 /* Chain for USCSI commands (non-checksum targets) */ 2015 SD_CHAIN_USCSI, /* Index: 19 */ 2016 SD_CHAIN_USCSI, /* Index: 20 */ 2017 2018 /* Chain for USCSI commands (checksum targets) */ 2019 SD_CHAIN_USCSI, /* Index: 21 */ 2020 SD_CHAIN_USCSI, /* Index: 22 */ 2021 SD_CHAIN_USCSI, /* Index: 22 */ 2022 2023 /* Chain for "direct" USCSI commands (all targets) */ 2024 SD_CHAIN_DIRECT, /* Index: 24 */ 2025 2026 /* Chain for "direct priority" USCSI commands (all targets) */ 2027 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2028 }; 2029 2030 2031 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2032 #define SD_IS_BUFIO(xp) \ 2033 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2034 2035 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2036 #define SD_IS_DIRECT_PRIORITY(xp) \ 2037 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2038 2039 2040 2041 /* 2042 * Struct, array, and macros to map a specific chain to the appropriate 2043 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2044 * 2045 * The sd_chain_index_map[] array is used at attach time to set the various 2046 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2047 * chain to be used with the instance. This allows different instances to use 2048 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2049 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2050 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2051 * dynamically & without the use of locking; and (2) a layer may update the 2052 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2053 * to allow for deferred processing of an IO within the same chain from a 2054 * different execution context. 2055 */ 2056 2057 struct sd_chain_index { 2058 int sci_iostart_index; 2059 int sci_iodone_index; 2060 }; 2061 2062 static struct sd_chain_index sd_chain_index_map[] = { 2063 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2064 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2065 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2066 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2067 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2068 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2069 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2070 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2071 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2072 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2073 }; 2074 2075 2076 /* 2077 * The following are indexes into the sd_chain_index_map[] array. 2078 */ 2079 2080 /* un->un_buf_chain_type must be set to one of these */ 2081 #define SD_CHAIN_INFO_DISK 0 2082 #define SD_CHAIN_INFO_DISK_NO_PM 1 2083 #define SD_CHAIN_INFO_RMMEDIA 2 2084 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2085 #define SD_CHAIN_INFO_CHKSUM 4 2086 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2087 2088 /* un->un_uscsi_chain_type must be set to one of these */ 2089 #define SD_CHAIN_INFO_USCSI_CMD 6 2090 /* USCSI with PM disabled is the same as DIRECT */ 2091 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2092 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2093 2094 /* un->un_direct_chain_type must be set to one of these */ 2095 #define SD_CHAIN_INFO_DIRECT_CMD 8 2096 2097 /* un->un_priority_chain_type must be set to one of these */ 2098 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2099 2100 /* size for devid inquiries */ 2101 #define MAX_INQUIRY_SIZE 0xF0 2102 2103 /* 2104 * Macros used by functions to pass a given buf(9S) struct along to the 2105 * next function in the layering chain for further processing. 2106 * 2107 * In the following macros, passing more than three arguments to the called 2108 * routines causes the optimizer for the SPARC compiler to stop doing tail 2109 * call elimination which results in significant performance degradation. 2110 */ 2111 #define SD_BEGIN_IOSTART(index, un, bp) \ 2112 ((*(sd_iostart_chain[index]))(index, un, bp)) 2113 2114 #define SD_BEGIN_IODONE(index, un, bp) \ 2115 ((*(sd_iodone_chain[index]))(index, un, bp)) 2116 2117 #define SD_NEXT_IOSTART(index, un, bp) \ 2118 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2119 2120 #define SD_NEXT_IODONE(index, un, bp) \ 2121 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2122 2123 /* 2124 * Function: _init 2125 * 2126 * Description: This is the driver _init(9E) entry point. 2127 * 2128 * Return Code: Returns the value from mod_install(9F) or 2129 * ddi_soft_state_init(9F) as appropriate. 2130 * 2131 * Context: Called when driver module loaded. 2132 */ 2133 2134 int 2135 _init(void) 2136 { 2137 int err; 2138 2139 /* establish driver name from module name */ 2140 sd_label = mod_modname(&modlinkage); 2141 2142 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2143 SD_MAXUNIT); 2144 2145 if (err != 0) { 2146 return (err); 2147 } 2148 2149 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2150 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2151 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2152 2153 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2154 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2155 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2156 2157 /* 2158 * it's ok to init here even for fibre device 2159 */ 2160 sd_scsi_probe_cache_init(); 2161 2162 sd_scsi_target_lun_init(); 2163 2164 /* 2165 * Creating taskq before mod_install ensures that all callers (threads) 2166 * that enter the module after a successfull mod_install encounter 2167 * a valid taskq. 2168 */ 2169 sd_taskq_create(); 2170 2171 err = mod_install(&modlinkage); 2172 if (err != 0) { 2173 /* delete taskq if install fails */ 2174 sd_taskq_delete(); 2175 2176 mutex_destroy(&sd_detach_mutex); 2177 mutex_destroy(&sd_log_mutex); 2178 mutex_destroy(&sd_label_mutex); 2179 2180 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2181 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2182 cv_destroy(&sd_tr.srq_inprocess_cv); 2183 2184 sd_scsi_probe_cache_fini(); 2185 2186 sd_scsi_target_lun_fini(); 2187 2188 ddi_soft_state_fini(&sd_state); 2189 return (err); 2190 } 2191 2192 return (err); 2193 } 2194 2195 2196 /* 2197 * Function: _fini 2198 * 2199 * Description: This is the driver _fini(9E) entry point. 2200 * 2201 * Return Code: Returns the value from mod_remove(9F) 2202 * 2203 * Context: Called when driver module is unloaded. 2204 */ 2205 2206 int 2207 _fini(void) 2208 { 2209 int err; 2210 2211 if ((err = mod_remove(&modlinkage)) != 0) { 2212 return (err); 2213 } 2214 2215 sd_taskq_delete(); 2216 2217 mutex_destroy(&sd_detach_mutex); 2218 mutex_destroy(&sd_log_mutex); 2219 mutex_destroy(&sd_label_mutex); 2220 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2221 2222 sd_scsi_probe_cache_fini(); 2223 2224 sd_scsi_target_lun_fini(); 2225 2226 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2227 cv_destroy(&sd_tr.srq_inprocess_cv); 2228 2229 ddi_soft_state_fini(&sd_state); 2230 2231 return (err); 2232 } 2233 2234 2235 /* 2236 * Function: _info 2237 * 2238 * Description: This is the driver _info(9E) entry point. 2239 * 2240 * Arguments: modinfop - pointer to the driver modinfo structure 2241 * 2242 * Return Code: Returns the value from mod_info(9F). 2243 * 2244 * Context: Kernel thread context 2245 */ 2246 2247 int 2248 _info(struct modinfo *modinfop) 2249 { 2250 return (mod_info(&modlinkage, modinfop)); 2251 } 2252 2253 2254 /* 2255 * The following routines implement the driver message logging facility. 2256 * They provide component- and level- based debug output filtering. 2257 * Output may also be restricted to messages for a single instance by 2258 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2259 * to NULL, then messages for all instances are printed. 2260 * 2261 * These routines have been cloned from each other due to the language 2262 * constraints of macros and variable argument list processing. 2263 */ 2264 2265 2266 /* 2267 * Function: sd_log_err 2268 * 2269 * Description: This routine is called by the SD_ERROR macro for debug 2270 * logging of error conditions. 2271 * 2272 * Arguments: comp - driver component being logged 2273 * dev - pointer to driver info structure 2274 * fmt - error string and format to be logged 2275 */ 2276 2277 static void 2278 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2279 { 2280 va_list ap; 2281 dev_info_t *dev; 2282 2283 ASSERT(un != NULL); 2284 dev = SD_DEVINFO(un); 2285 ASSERT(dev != NULL); 2286 2287 /* 2288 * Filter messages based on the global component and level masks. 2289 * Also print if un matches the value of sd_debug_un, or if 2290 * sd_debug_un is set to NULL. 2291 */ 2292 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2293 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2294 mutex_enter(&sd_log_mutex); 2295 va_start(ap, fmt); 2296 (void) vsprintf(sd_log_buf, fmt, ap); 2297 va_end(ap); 2298 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2299 mutex_exit(&sd_log_mutex); 2300 } 2301 #ifdef SD_FAULT_INJECTION 2302 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2303 if (un->sd_injection_mask & comp) { 2304 mutex_enter(&sd_log_mutex); 2305 va_start(ap, fmt); 2306 (void) vsprintf(sd_log_buf, fmt, ap); 2307 va_end(ap); 2308 sd_injection_log(sd_log_buf, un); 2309 mutex_exit(&sd_log_mutex); 2310 } 2311 #endif 2312 } 2313 2314 2315 /* 2316 * Function: sd_log_info 2317 * 2318 * Description: This routine is called by the SD_INFO macro for debug 2319 * logging of general purpose informational conditions. 2320 * 2321 * Arguments: comp - driver component being logged 2322 * dev - pointer to driver info structure 2323 * fmt - info string and format to be logged 2324 */ 2325 2326 static void 2327 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2328 { 2329 va_list ap; 2330 dev_info_t *dev; 2331 2332 ASSERT(un != NULL); 2333 dev = SD_DEVINFO(un); 2334 ASSERT(dev != NULL); 2335 2336 /* 2337 * Filter messages based on the global component and level masks. 2338 * Also print if un matches the value of sd_debug_un, or if 2339 * sd_debug_un is set to NULL. 2340 */ 2341 if ((sd_component_mask & component) && 2342 (sd_level_mask & SD_LOGMASK_INFO) && 2343 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2344 mutex_enter(&sd_log_mutex); 2345 va_start(ap, fmt); 2346 (void) vsprintf(sd_log_buf, fmt, ap); 2347 va_end(ap); 2348 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2349 mutex_exit(&sd_log_mutex); 2350 } 2351 #ifdef SD_FAULT_INJECTION 2352 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2353 if (un->sd_injection_mask & component) { 2354 mutex_enter(&sd_log_mutex); 2355 va_start(ap, fmt); 2356 (void) vsprintf(sd_log_buf, fmt, ap); 2357 va_end(ap); 2358 sd_injection_log(sd_log_buf, un); 2359 mutex_exit(&sd_log_mutex); 2360 } 2361 #endif 2362 } 2363 2364 2365 /* 2366 * Function: sd_log_trace 2367 * 2368 * Description: This routine is called by the SD_TRACE macro for debug 2369 * logging of trace conditions (i.e. function entry/exit). 2370 * 2371 * Arguments: comp - driver component being logged 2372 * dev - pointer to driver info structure 2373 * fmt - trace string and format to be logged 2374 */ 2375 2376 static void 2377 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2378 { 2379 va_list ap; 2380 dev_info_t *dev; 2381 2382 ASSERT(un != NULL); 2383 dev = SD_DEVINFO(un); 2384 ASSERT(dev != NULL); 2385 2386 /* 2387 * Filter messages based on the global component and level masks. 2388 * Also print if un matches the value of sd_debug_un, or if 2389 * sd_debug_un is set to NULL. 2390 */ 2391 if ((sd_component_mask & component) && 2392 (sd_level_mask & SD_LOGMASK_TRACE) && 2393 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2394 mutex_enter(&sd_log_mutex); 2395 va_start(ap, fmt); 2396 (void) vsprintf(sd_log_buf, fmt, ap); 2397 va_end(ap); 2398 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2399 mutex_exit(&sd_log_mutex); 2400 } 2401 #ifdef SD_FAULT_INJECTION 2402 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2403 if (un->sd_injection_mask & component) { 2404 mutex_enter(&sd_log_mutex); 2405 va_start(ap, fmt); 2406 (void) vsprintf(sd_log_buf, fmt, ap); 2407 va_end(ap); 2408 sd_injection_log(sd_log_buf, un); 2409 mutex_exit(&sd_log_mutex); 2410 } 2411 #endif 2412 } 2413 2414 2415 /* 2416 * Function: sdprobe 2417 * 2418 * Description: This is the driver probe(9e) entry point function. 2419 * 2420 * Arguments: devi - opaque device info handle 2421 * 2422 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2423 * DDI_PROBE_FAILURE: If the probe failed. 2424 * DDI_PROBE_PARTIAL: If the instance is not present now, 2425 * but may be present in the future. 2426 */ 2427 2428 static int 2429 sdprobe(dev_info_t *devi) 2430 { 2431 struct scsi_device *devp; 2432 int rval; 2433 int instance; 2434 2435 /* 2436 * if it wasn't for pln, sdprobe could actually be nulldev 2437 * in the "__fibre" case. 2438 */ 2439 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2440 return (DDI_PROBE_DONTCARE); 2441 } 2442 2443 devp = ddi_get_driver_private(devi); 2444 2445 if (devp == NULL) { 2446 /* Ooops... nexus driver is mis-configured... */ 2447 return (DDI_PROBE_FAILURE); 2448 } 2449 2450 instance = ddi_get_instance(devi); 2451 2452 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2453 return (DDI_PROBE_PARTIAL); 2454 } 2455 2456 /* 2457 * Call the SCSA utility probe routine to see if we actually 2458 * have a target at this SCSI nexus. 2459 */ 2460 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2461 case SCSIPROBE_EXISTS: 2462 switch (devp->sd_inq->inq_dtype) { 2463 case DTYPE_DIRECT: 2464 rval = DDI_PROBE_SUCCESS; 2465 break; 2466 case DTYPE_RODIRECT: 2467 /* CDs etc. Can be removable media */ 2468 rval = DDI_PROBE_SUCCESS; 2469 break; 2470 case DTYPE_OPTICAL: 2471 /* 2472 * Rewritable optical driver HP115AA 2473 * Can also be removable media 2474 */ 2475 2476 /* 2477 * Do not attempt to bind to DTYPE_OPTICAL if 2478 * pre solaris 9 sparc sd behavior is required 2479 * 2480 * If first time through and sd_dtype_optical_bind 2481 * has not been set in /etc/system check properties 2482 */ 2483 2484 if (sd_dtype_optical_bind < 0) { 2485 sd_dtype_optical_bind = ddi_prop_get_int 2486 (DDI_DEV_T_ANY, devi, 0, 2487 "optical-device-bind", 1); 2488 } 2489 2490 if (sd_dtype_optical_bind == 0) { 2491 rval = DDI_PROBE_FAILURE; 2492 } else { 2493 rval = DDI_PROBE_SUCCESS; 2494 } 2495 break; 2496 2497 case DTYPE_NOTPRESENT: 2498 default: 2499 rval = DDI_PROBE_FAILURE; 2500 break; 2501 } 2502 break; 2503 default: 2504 rval = DDI_PROBE_PARTIAL; 2505 break; 2506 } 2507 2508 /* 2509 * This routine checks for resource allocation prior to freeing, 2510 * so it will take care of the "smart probing" case where a 2511 * scsi_probe() may or may not have been issued and will *not* 2512 * free previously-freed resources. 2513 */ 2514 scsi_unprobe(devp); 2515 return (rval); 2516 } 2517 2518 2519 /* 2520 * Function: sdinfo 2521 * 2522 * Description: This is the driver getinfo(9e) entry point function. 2523 * Given the device number, return the devinfo pointer from 2524 * the scsi_device structure or the instance number 2525 * associated with the dev_t. 2526 * 2527 * Arguments: dip - pointer to device info structure 2528 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2529 * DDI_INFO_DEVT2INSTANCE) 2530 * arg - driver dev_t 2531 * resultp - user buffer for request response 2532 * 2533 * Return Code: DDI_SUCCESS 2534 * DDI_FAILURE 2535 */ 2536 /* ARGSUSED */ 2537 static int 2538 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2539 { 2540 struct sd_lun *un; 2541 dev_t dev; 2542 int instance; 2543 int error; 2544 2545 switch (infocmd) { 2546 case DDI_INFO_DEVT2DEVINFO: 2547 dev = (dev_t)arg; 2548 instance = SDUNIT(dev); 2549 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2550 return (DDI_FAILURE); 2551 } 2552 *result = (void *) SD_DEVINFO(un); 2553 error = DDI_SUCCESS; 2554 break; 2555 case DDI_INFO_DEVT2INSTANCE: 2556 dev = (dev_t)arg; 2557 instance = SDUNIT(dev); 2558 *result = (void *)(uintptr_t)instance; 2559 error = DDI_SUCCESS; 2560 break; 2561 default: 2562 error = DDI_FAILURE; 2563 } 2564 return (error); 2565 } 2566 2567 /* 2568 * Function: sd_prop_op 2569 * 2570 * Description: This is the driver prop_op(9e) entry point function. 2571 * Return the number of blocks for the partition in question 2572 * or forward the request to the property facilities. 2573 * 2574 * Arguments: dev - device number 2575 * dip - pointer to device info structure 2576 * prop_op - property operator 2577 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2578 * name - pointer to property name 2579 * valuep - pointer or address of the user buffer 2580 * lengthp - property length 2581 * 2582 * Return Code: DDI_PROP_SUCCESS 2583 * DDI_PROP_NOT_FOUND 2584 * DDI_PROP_UNDEFINED 2585 * DDI_PROP_NO_MEMORY 2586 * DDI_PROP_BUF_TOO_SMALL 2587 */ 2588 2589 static int 2590 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2591 char *name, caddr_t valuep, int *lengthp) 2592 { 2593 int instance = ddi_get_instance(dip); 2594 struct sd_lun *un; 2595 uint64_t nblocks64; 2596 uint_t dblk; 2597 2598 /* 2599 * Our dynamic properties are all device specific and size oriented. 2600 * Requests issued under conditions where size is valid are passed 2601 * to ddi_prop_op_nblocks with the size information, otherwise the 2602 * request is passed to ddi_prop_op. Size depends on valid geometry. 2603 */ 2604 un = ddi_get_soft_state(sd_state, instance); 2605 if ((dev == DDI_DEV_T_ANY) || (un == NULL)) { 2606 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2607 name, valuep, lengthp)); 2608 } else if (!SD_IS_VALID_LABEL(un)) { 2609 return (ddi_prop_op(dev, dip, prop_op, mod_flags, name, 2610 valuep, lengthp)); 2611 } 2612 2613 /* get nblocks value */ 2614 ASSERT(!mutex_owned(SD_MUTEX(un))); 2615 2616 (void) cmlb_partinfo(un->un_cmlbhandle, SDPART(dev), 2617 (diskaddr_t *)&nblocks64, NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 2618 2619 /* report size in target size blocks */ 2620 dblk = un->un_tgt_blocksize / un->un_sys_blocksize; 2621 return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags, 2622 name, valuep, lengthp, nblocks64 / dblk, un->un_tgt_blocksize)); 2623 } 2624 2625 /* 2626 * The following functions are for smart probing: 2627 * sd_scsi_probe_cache_init() 2628 * sd_scsi_probe_cache_fini() 2629 * sd_scsi_clear_probe_cache() 2630 * sd_scsi_probe_with_cache() 2631 */ 2632 2633 /* 2634 * Function: sd_scsi_probe_cache_init 2635 * 2636 * Description: Initializes the probe response cache mutex and head pointer. 2637 * 2638 * Context: Kernel thread context 2639 */ 2640 2641 static void 2642 sd_scsi_probe_cache_init(void) 2643 { 2644 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2645 sd_scsi_probe_cache_head = NULL; 2646 } 2647 2648 2649 /* 2650 * Function: sd_scsi_probe_cache_fini 2651 * 2652 * Description: Frees all resources associated with the probe response cache. 2653 * 2654 * Context: Kernel thread context 2655 */ 2656 2657 static void 2658 sd_scsi_probe_cache_fini(void) 2659 { 2660 struct sd_scsi_probe_cache *cp; 2661 struct sd_scsi_probe_cache *ncp; 2662 2663 /* Clean up our smart probing linked list */ 2664 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2665 ncp = cp->next; 2666 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2667 } 2668 sd_scsi_probe_cache_head = NULL; 2669 mutex_destroy(&sd_scsi_probe_cache_mutex); 2670 } 2671 2672 2673 /* 2674 * Function: sd_scsi_clear_probe_cache 2675 * 2676 * Description: This routine clears the probe response cache. This is 2677 * done when open() returns ENXIO so that when deferred 2678 * attach is attempted (possibly after a device has been 2679 * turned on) we will retry the probe. Since we don't know 2680 * which target we failed to open, we just clear the 2681 * entire cache. 2682 * 2683 * Context: Kernel thread context 2684 */ 2685 2686 static void 2687 sd_scsi_clear_probe_cache(void) 2688 { 2689 struct sd_scsi_probe_cache *cp; 2690 int i; 2691 2692 mutex_enter(&sd_scsi_probe_cache_mutex); 2693 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2694 /* 2695 * Reset all entries to SCSIPROBE_EXISTS. This will 2696 * force probing to be performed the next time 2697 * sd_scsi_probe_with_cache is called. 2698 */ 2699 for (i = 0; i < NTARGETS_WIDE; i++) { 2700 cp->cache[i] = SCSIPROBE_EXISTS; 2701 } 2702 } 2703 mutex_exit(&sd_scsi_probe_cache_mutex); 2704 } 2705 2706 2707 /* 2708 * Function: sd_scsi_probe_with_cache 2709 * 2710 * Description: This routine implements support for a scsi device probe 2711 * with cache. The driver maintains a cache of the target 2712 * responses to scsi probes. If we get no response from a 2713 * target during a probe inquiry, we remember that, and we 2714 * avoid additional calls to scsi_probe on non-zero LUNs 2715 * on the same target until the cache is cleared. By doing 2716 * so we avoid the 1/4 sec selection timeout for nonzero 2717 * LUNs. lun0 of a target is always probed. 2718 * 2719 * Arguments: devp - Pointer to a scsi_device(9S) structure 2720 * waitfunc - indicates what the allocator routines should 2721 * do when resources are not available. This value 2722 * is passed on to scsi_probe() when that routine 2723 * is called. 2724 * 2725 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2726 * otherwise the value returned by scsi_probe(9F). 2727 * 2728 * Context: Kernel thread context 2729 */ 2730 2731 static int 2732 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2733 { 2734 struct sd_scsi_probe_cache *cp; 2735 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2736 int lun, tgt; 2737 2738 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2739 SCSI_ADDR_PROP_LUN, 0); 2740 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2741 SCSI_ADDR_PROP_TARGET, -1); 2742 2743 /* Make sure caching enabled and target in range */ 2744 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2745 /* do it the old way (no cache) */ 2746 return (scsi_probe(devp, waitfn)); 2747 } 2748 2749 mutex_enter(&sd_scsi_probe_cache_mutex); 2750 2751 /* Find the cache for this scsi bus instance */ 2752 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2753 if (cp->pdip == pdip) { 2754 break; 2755 } 2756 } 2757 2758 /* If we can't find a cache for this pdip, create one */ 2759 if (cp == NULL) { 2760 int i; 2761 2762 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2763 KM_SLEEP); 2764 cp->pdip = pdip; 2765 cp->next = sd_scsi_probe_cache_head; 2766 sd_scsi_probe_cache_head = cp; 2767 for (i = 0; i < NTARGETS_WIDE; i++) { 2768 cp->cache[i] = SCSIPROBE_EXISTS; 2769 } 2770 } 2771 2772 mutex_exit(&sd_scsi_probe_cache_mutex); 2773 2774 /* Recompute the cache for this target if LUN zero */ 2775 if (lun == 0) { 2776 cp->cache[tgt] = SCSIPROBE_EXISTS; 2777 } 2778 2779 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2780 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2781 return (SCSIPROBE_NORESP); 2782 } 2783 2784 /* Do the actual probe; save & return the result */ 2785 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2786 } 2787 2788 2789 /* 2790 * Function: sd_scsi_target_lun_init 2791 * 2792 * Description: Initializes the attached lun chain mutex and head pointer. 2793 * 2794 * Context: Kernel thread context 2795 */ 2796 2797 static void 2798 sd_scsi_target_lun_init(void) 2799 { 2800 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2801 sd_scsi_target_lun_head = NULL; 2802 } 2803 2804 2805 /* 2806 * Function: sd_scsi_target_lun_fini 2807 * 2808 * Description: Frees all resources associated with the attached lun 2809 * chain 2810 * 2811 * Context: Kernel thread context 2812 */ 2813 2814 static void 2815 sd_scsi_target_lun_fini(void) 2816 { 2817 struct sd_scsi_hba_tgt_lun *cp; 2818 struct sd_scsi_hba_tgt_lun *ncp; 2819 2820 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2821 ncp = cp->next; 2822 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2823 } 2824 sd_scsi_target_lun_head = NULL; 2825 mutex_destroy(&sd_scsi_target_lun_mutex); 2826 } 2827 2828 2829 /* 2830 * Function: sd_scsi_get_target_lun_count 2831 * 2832 * Description: This routine will check in the attached lun chain to see 2833 * how many luns are attached on the required SCSI controller 2834 * and target. Currently, some capabilities like tagged queue 2835 * are supported per target based by HBA. So all luns in a 2836 * target have the same capabilities. Based on this assumption, 2837 * sd should only set these capabilities once per target. This 2838 * function is called when sd needs to decide how many luns 2839 * already attached on a target. 2840 * 2841 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2842 * controller device. 2843 * target - The target ID on the controller's SCSI bus. 2844 * 2845 * Return Code: The number of luns attached on the required target and 2846 * controller. 2847 * -1 if target ID is not in parallel SCSI scope or the given 2848 * dip is not in the chain. 2849 * 2850 * Context: Kernel thread context 2851 */ 2852 2853 static int 2854 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2855 { 2856 struct sd_scsi_hba_tgt_lun *cp; 2857 2858 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2859 return (-1); 2860 } 2861 2862 mutex_enter(&sd_scsi_target_lun_mutex); 2863 2864 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2865 if (cp->pdip == dip) { 2866 break; 2867 } 2868 } 2869 2870 mutex_exit(&sd_scsi_target_lun_mutex); 2871 2872 if (cp == NULL) { 2873 return (-1); 2874 } 2875 2876 return (cp->nlun[target]); 2877 } 2878 2879 2880 /* 2881 * Function: sd_scsi_update_lun_on_target 2882 * 2883 * Description: This routine is used to update the attached lun chain when a 2884 * lun is attached or detached on a target. 2885 * 2886 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2887 * controller device. 2888 * target - The target ID on the controller's SCSI bus. 2889 * flag - Indicate the lun is attached or detached. 2890 * 2891 * Context: Kernel thread context 2892 */ 2893 2894 static void 2895 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 2896 { 2897 struct sd_scsi_hba_tgt_lun *cp; 2898 2899 mutex_enter(&sd_scsi_target_lun_mutex); 2900 2901 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2902 if (cp->pdip == dip) { 2903 break; 2904 } 2905 } 2906 2907 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 2908 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 2909 KM_SLEEP); 2910 cp->pdip = dip; 2911 cp->next = sd_scsi_target_lun_head; 2912 sd_scsi_target_lun_head = cp; 2913 } 2914 2915 mutex_exit(&sd_scsi_target_lun_mutex); 2916 2917 if (cp != NULL) { 2918 if (flag == SD_SCSI_LUN_ATTACH) { 2919 cp->nlun[target] ++; 2920 } else { 2921 cp->nlun[target] --; 2922 } 2923 } 2924 } 2925 2926 2927 /* 2928 * Function: sd_spin_up_unit 2929 * 2930 * Description: Issues the following commands to spin-up the device: 2931 * START STOP UNIT, and INQUIRY. 2932 * 2933 * Arguments: un - driver soft state (unit) structure 2934 * 2935 * Return Code: 0 - success 2936 * EIO - failure 2937 * EACCES - reservation conflict 2938 * 2939 * Context: Kernel thread context 2940 */ 2941 2942 static int 2943 sd_spin_up_unit(struct sd_lun *un) 2944 { 2945 size_t resid = 0; 2946 int has_conflict = FALSE; 2947 uchar_t *bufaddr; 2948 2949 ASSERT(un != NULL); 2950 2951 /* 2952 * Send a throwaway START UNIT command. 2953 * 2954 * If we fail on this, we don't care presently what precisely 2955 * is wrong. EMC's arrays will also fail this with a check 2956 * condition (0x2/0x4/0x3) if the device is "inactive," but 2957 * we don't want to fail the attach because it may become 2958 * "active" later. 2959 */ 2960 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2961 == EACCES) 2962 has_conflict = TRUE; 2963 2964 /* 2965 * Send another INQUIRY command to the target. This is necessary for 2966 * non-removable media direct access devices because their INQUIRY data 2967 * may not be fully qualified until they are spun up (perhaps via the 2968 * START command above). Note: This seems to be needed for some 2969 * legacy devices only.) The INQUIRY command should succeed even if a 2970 * Reservation Conflict is present. 2971 */ 2972 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2973 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2974 kmem_free(bufaddr, SUN_INQSIZE); 2975 return (EIO); 2976 } 2977 2978 /* 2979 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2980 * Note that this routine does not return a failure here even if the 2981 * INQUIRY command did not return any data. This is a legacy behavior. 2982 */ 2983 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2984 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2985 } 2986 2987 kmem_free(bufaddr, SUN_INQSIZE); 2988 2989 /* If we hit a reservation conflict above, tell the caller. */ 2990 if (has_conflict == TRUE) { 2991 return (EACCES); 2992 } 2993 2994 return (0); 2995 } 2996 2997 #ifdef _LP64 2998 /* 2999 * Function: sd_enable_descr_sense 3000 * 3001 * Description: This routine attempts to select descriptor sense format 3002 * using the Control mode page. Devices that support 64 bit 3003 * LBAs (for >2TB luns) should also implement descriptor 3004 * sense data so we will call this function whenever we see 3005 * a lun larger than 2TB. If for some reason the device 3006 * supports 64 bit LBAs but doesn't support descriptor sense 3007 * presumably the mode select will fail. Everything will 3008 * continue to work normally except that we will not get 3009 * complete sense data for commands that fail with an LBA 3010 * larger than 32 bits. 3011 * 3012 * Arguments: un - driver soft state (unit) structure 3013 * 3014 * Context: Kernel thread context only 3015 */ 3016 3017 static void 3018 sd_enable_descr_sense(struct sd_lun *un) 3019 { 3020 uchar_t *header; 3021 struct mode_control_scsi3 *ctrl_bufp; 3022 size_t buflen; 3023 size_t bd_len; 3024 3025 /* 3026 * Read MODE SENSE page 0xA, Control Mode Page 3027 */ 3028 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3029 sizeof (struct mode_control_scsi3); 3030 header = kmem_zalloc(buflen, KM_SLEEP); 3031 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3032 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3033 SD_ERROR(SD_LOG_COMMON, un, 3034 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3035 goto eds_exit; 3036 } 3037 3038 /* 3039 * Determine size of Block Descriptors in order to locate 3040 * the mode page data. ATAPI devices return 0, SCSI devices 3041 * should return MODE_BLK_DESC_LENGTH. 3042 */ 3043 bd_len = ((struct mode_header *)header)->bdesc_length; 3044 3045 /* Clear the mode data length field for MODE SELECT */ 3046 ((struct mode_header *)header)->length = 0; 3047 3048 ctrl_bufp = (struct mode_control_scsi3 *) 3049 (header + MODE_HEADER_LENGTH + bd_len); 3050 3051 /* 3052 * If the page length is smaller than the expected value, 3053 * the target device doesn't support D_SENSE. Bail out here. 3054 */ 3055 if (ctrl_bufp->mode_page.length < 3056 sizeof (struct mode_control_scsi3) - 2) { 3057 SD_ERROR(SD_LOG_COMMON, un, 3058 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3059 goto eds_exit; 3060 } 3061 3062 /* 3063 * Clear PS bit for MODE SELECT 3064 */ 3065 ctrl_bufp->mode_page.ps = 0; 3066 3067 /* 3068 * Set D_SENSE to enable descriptor sense format. 3069 */ 3070 ctrl_bufp->d_sense = 1; 3071 3072 /* 3073 * Use MODE SELECT to commit the change to the D_SENSE bit 3074 */ 3075 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3076 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3077 SD_INFO(SD_LOG_COMMON, un, 3078 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3079 goto eds_exit; 3080 } 3081 3082 eds_exit: 3083 kmem_free(header, buflen); 3084 } 3085 3086 /* 3087 * Function: sd_reenable_dsense_task 3088 * 3089 * Description: Re-enable descriptor sense after device or bus reset 3090 * 3091 * Context: Executes in a taskq() thread context 3092 */ 3093 static void 3094 sd_reenable_dsense_task(void *arg) 3095 { 3096 struct sd_lun *un = arg; 3097 3098 ASSERT(un != NULL); 3099 sd_enable_descr_sense(un); 3100 } 3101 #endif /* _LP64 */ 3102 3103 /* 3104 * Function: sd_set_mmc_caps 3105 * 3106 * Description: This routine determines if the device is MMC compliant and if 3107 * the device supports CDDA via a mode sense of the CDVD 3108 * capabilities mode page. Also checks if the device is a 3109 * dvdram writable device. 3110 * 3111 * Arguments: un - driver soft state (unit) structure 3112 * 3113 * Context: Kernel thread context only 3114 */ 3115 3116 static void 3117 sd_set_mmc_caps(struct sd_lun *un) 3118 { 3119 struct mode_header_grp2 *sense_mhp; 3120 uchar_t *sense_page; 3121 caddr_t buf; 3122 int bd_len; 3123 int status; 3124 struct uscsi_cmd com; 3125 int rtn; 3126 uchar_t *out_data_rw, *out_data_hd; 3127 uchar_t *rqbuf_rw, *rqbuf_hd; 3128 3129 ASSERT(un != NULL); 3130 3131 /* 3132 * The flags which will be set in this function are - mmc compliant, 3133 * dvdram writable device, cdda support. Initialize them to FALSE 3134 * and if a capability is detected - it will be set to TRUE. 3135 */ 3136 un->un_f_mmc_cap = FALSE; 3137 un->un_f_dvdram_writable_device = FALSE; 3138 un->un_f_cfg_cdda = FALSE; 3139 3140 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3141 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3142 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3143 3144 if (status != 0) { 3145 /* command failed; just return */ 3146 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3147 return; 3148 } 3149 /* 3150 * If the mode sense request for the CDROM CAPABILITIES 3151 * page (0x2A) succeeds the device is assumed to be MMC. 3152 */ 3153 un->un_f_mmc_cap = TRUE; 3154 3155 /* Get to the page data */ 3156 sense_mhp = (struct mode_header_grp2 *)buf; 3157 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3158 sense_mhp->bdesc_length_lo; 3159 if (bd_len > MODE_BLK_DESC_LENGTH) { 3160 /* 3161 * We did not get back the expected block descriptor 3162 * length so we cannot determine if the device supports 3163 * CDDA. However, we still indicate the device is MMC 3164 * according to the successful response to the page 3165 * 0x2A mode sense request. 3166 */ 3167 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3168 "sd_set_mmc_caps: Mode Sense returned " 3169 "invalid block descriptor length\n"); 3170 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3171 return; 3172 } 3173 3174 /* See if read CDDA is supported */ 3175 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3176 bd_len); 3177 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3178 3179 /* See if writing DVD RAM is supported. */ 3180 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3181 if (un->un_f_dvdram_writable_device == TRUE) { 3182 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3183 return; 3184 } 3185 3186 /* 3187 * If the device presents DVD or CD capabilities in the mode 3188 * page, we can return here since a RRD will not have 3189 * these capabilities. 3190 */ 3191 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3192 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3193 return; 3194 } 3195 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3196 3197 /* 3198 * If un->un_f_dvdram_writable_device is still FALSE, 3199 * check for a Removable Rigid Disk (RRD). A RRD 3200 * device is identified by the features RANDOM_WRITABLE and 3201 * HARDWARE_DEFECT_MANAGEMENT. 3202 */ 3203 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3204 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3205 3206 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3207 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3208 RANDOM_WRITABLE, SD_PATH_STANDARD); 3209 if (rtn != 0) { 3210 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3211 kmem_free(rqbuf_rw, SENSE_LENGTH); 3212 return; 3213 } 3214 3215 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3216 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3217 3218 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3219 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3220 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3221 if (rtn == 0) { 3222 /* 3223 * We have good information, check for random writable 3224 * and hardware defect features. 3225 */ 3226 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3227 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3228 un->un_f_dvdram_writable_device = TRUE; 3229 } 3230 } 3231 3232 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3233 kmem_free(rqbuf_rw, SENSE_LENGTH); 3234 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3235 kmem_free(rqbuf_hd, SENSE_LENGTH); 3236 } 3237 3238 /* 3239 * Function: sd_check_for_writable_cd 3240 * 3241 * Description: This routine determines if the media in the device is 3242 * writable or not. It uses the get configuration command (0x46) 3243 * to determine if the media is writable 3244 * 3245 * Arguments: un - driver soft state (unit) structure 3246 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3247 * chain and the normal command waitq, or 3248 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3249 * "direct" chain and bypass the normal command 3250 * waitq. 3251 * 3252 * Context: Never called at interrupt context. 3253 */ 3254 3255 static void 3256 sd_check_for_writable_cd(struct sd_lun *un, int path_flag) 3257 { 3258 struct uscsi_cmd com; 3259 uchar_t *out_data; 3260 uchar_t *rqbuf; 3261 int rtn; 3262 uchar_t *out_data_rw, *out_data_hd; 3263 uchar_t *rqbuf_rw, *rqbuf_hd; 3264 struct mode_header_grp2 *sense_mhp; 3265 uchar_t *sense_page; 3266 caddr_t buf; 3267 int bd_len; 3268 int status; 3269 3270 ASSERT(un != NULL); 3271 ASSERT(mutex_owned(SD_MUTEX(un))); 3272 3273 /* 3274 * Initialize the writable media to false, if configuration info. 3275 * tells us otherwise then only we will set it. 3276 */ 3277 un->un_f_mmc_writable_media = FALSE; 3278 mutex_exit(SD_MUTEX(un)); 3279 3280 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3281 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3282 3283 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3284 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3285 3286 mutex_enter(SD_MUTEX(un)); 3287 if (rtn == 0) { 3288 /* 3289 * We have good information, check for writable DVD. 3290 */ 3291 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3292 un->un_f_mmc_writable_media = TRUE; 3293 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3294 kmem_free(rqbuf, SENSE_LENGTH); 3295 return; 3296 } 3297 } 3298 3299 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3300 kmem_free(rqbuf, SENSE_LENGTH); 3301 3302 /* 3303 * Determine if this is a RRD type device. 3304 */ 3305 mutex_exit(SD_MUTEX(un)); 3306 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3307 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3308 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3309 mutex_enter(SD_MUTEX(un)); 3310 if (status != 0) { 3311 /* command failed; just return */ 3312 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3313 return; 3314 } 3315 3316 /* Get to the page data */ 3317 sense_mhp = (struct mode_header_grp2 *)buf; 3318 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3319 if (bd_len > MODE_BLK_DESC_LENGTH) { 3320 /* 3321 * We did not get back the expected block descriptor length so 3322 * we cannot check the mode page. 3323 */ 3324 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3325 "sd_check_for_writable_cd: Mode Sense returned " 3326 "invalid block descriptor length\n"); 3327 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3328 return; 3329 } 3330 3331 /* 3332 * If the device presents DVD or CD capabilities in the mode 3333 * page, we can return here since a RRD device will not have 3334 * these capabilities. 3335 */ 3336 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3337 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3338 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3339 return; 3340 } 3341 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3342 3343 /* 3344 * If un->un_f_mmc_writable_media is still FALSE, 3345 * check for RRD type media. A RRD device is identified 3346 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3347 */ 3348 mutex_exit(SD_MUTEX(un)); 3349 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3350 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3351 3352 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3353 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3354 RANDOM_WRITABLE, path_flag); 3355 if (rtn != 0) { 3356 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3357 kmem_free(rqbuf_rw, SENSE_LENGTH); 3358 mutex_enter(SD_MUTEX(un)); 3359 return; 3360 } 3361 3362 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3363 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3364 3365 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3366 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3367 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3368 mutex_enter(SD_MUTEX(un)); 3369 if (rtn == 0) { 3370 /* 3371 * We have good information, check for random writable 3372 * and hardware defect features as current. 3373 */ 3374 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3375 (out_data_rw[10] & 0x1) && 3376 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3377 (out_data_hd[10] & 0x1)) { 3378 un->un_f_mmc_writable_media = TRUE; 3379 } 3380 } 3381 3382 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3383 kmem_free(rqbuf_rw, SENSE_LENGTH); 3384 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3385 kmem_free(rqbuf_hd, SENSE_LENGTH); 3386 } 3387 3388 /* 3389 * Function: sd_read_unit_properties 3390 * 3391 * Description: The following implements a property lookup mechanism. 3392 * Properties for particular disks (keyed on vendor, model 3393 * and rev numbers) are sought in the sd.conf file via 3394 * sd_process_sdconf_file(), and if not found there, are 3395 * looked for in a list hardcoded in this driver via 3396 * sd_process_sdconf_table() Once located the properties 3397 * are used to update the driver unit structure. 3398 * 3399 * Arguments: un - driver soft state (unit) structure 3400 */ 3401 3402 static void 3403 sd_read_unit_properties(struct sd_lun *un) 3404 { 3405 /* 3406 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3407 * the "sd-config-list" property (from the sd.conf file) or if 3408 * there was not a match for the inquiry vid/pid. If this event 3409 * occurs the static driver configuration table is searched for 3410 * a match. 3411 */ 3412 ASSERT(un != NULL); 3413 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3414 sd_process_sdconf_table(un); 3415 } 3416 3417 /* check for LSI device */ 3418 sd_is_lsi(un); 3419 3420 3421 } 3422 3423 3424 /* 3425 * Function: sd_process_sdconf_file 3426 * 3427 * Description: Use ddi_getlongprop to obtain the properties from the 3428 * driver's config file (ie, sd.conf) and update the driver 3429 * soft state structure accordingly. 3430 * 3431 * Arguments: un - driver soft state (unit) structure 3432 * 3433 * Return Code: SD_SUCCESS - The properties were successfully set according 3434 * to the driver configuration file. 3435 * SD_FAILURE - The driver config list was not obtained or 3436 * there was no vid/pid match. This indicates that 3437 * the static config table should be used. 3438 * 3439 * The config file has a property, "sd-config-list", which consists of 3440 * one or more duplets as follows: 3441 * 3442 * sd-config-list= 3443 * <duplet>, 3444 * [<duplet>,] 3445 * [<duplet>]; 3446 * 3447 * The structure of each duplet is as follows: 3448 * 3449 * <duplet>:= <vid+pid>,<data-property-name_list> 3450 * 3451 * The first entry of the duplet is the device ID string (the concatenated 3452 * vid & pid; not to be confused with a device_id). This is defined in 3453 * the same way as in the sd_disk_table. 3454 * 3455 * The second part of the duplet is a string that identifies a 3456 * data-property-name-list. The data-property-name-list is defined as 3457 * follows: 3458 * 3459 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3460 * 3461 * The syntax of <data-property-name> depends on the <version> field. 3462 * 3463 * If version = SD_CONF_VERSION_1 we have the following syntax: 3464 * 3465 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3466 * 3467 * where the prop0 value will be used to set prop0 if bit0 set in the 3468 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3469 * 3470 */ 3471 3472 static int 3473 sd_process_sdconf_file(struct sd_lun *un) 3474 { 3475 char *config_list = NULL; 3476 int config_list_len; 3477 int len; 3478 int dupletlen = 0; 3479 char *vidptr; 3480 int vidlen; 3481 char *dnlist_ptr; 3482 char *dataname_ptr; 3483 int dnlist_len; 3484 int dataname_len; 3485 int *data_list; 3486 int data_list_len; 3487 int rval = SD_FAILURE; 3488 int i; 3489 3490 ASSERT(un != NULL); 3491 3492 /* Obtain the configuration list associated with the .conf file */ 3493 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3494 sd_config_list, (caddr_t)&config_list, &config_list_len) 3495 != DDI_PROP_SUCCESS) { 3496 return (SD_FAILURE); 3497 } 3498 3499 /* 3500 * Compare vids in each duplet to the inquiry vid - if a match is 3501 * made, get the data value and update the soft state structure 3502 * accordingly. 3503 * 3504 * Note: This algorithm is complex and difficult to maintain. It should 3505 * be replaced with a more robust implementation. 3506 */ 3507 for (len = config_list_len, vidptr = config_list; len > 0; 3508 vidptr += dupletlen, len -= dupletlen) { 3509 /* 3510 * Note: The assumption here is that each vid entry is on 3511 * a unique line from its associated duplet. 3512 */ 3513 vidlen = dupletlen = (int)strlen(vidptr); 3514 if ((vidlen == 0) || 3515 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3516 dupletlen++; 3517 continue; 3518 } 3519 3520 /* 3521 * dnlist contains 1 or more blank separated 3522 * data-property-name entries 3523 */ 3524 dnlist_ptr = vidptr + vidlen + 1; 3525 dnlist_len = (int)strlen(dnlist_ptr); 3526 dupletlen += dnlist_len + 2; 3527 3528 /* 3529 * Set a pointer for the first data-property-name 3530 * entry in the list 3531 */ 3532 dataname_ptr = dnlist_ptr; 3533 dataname_len = 0; 3534 3535 /* 3536 * Loop through all data-property-name entries in the 3537 * data-property-name-list setting the properties for each. 3538 */ 3539 while (dataname_len < dnlist_len) { 3540 int version; 3541 3542 /* 3543 * Determine the length of the current 3544 * data-property-name entry by indexing until a 3545 * blank or NULL is encountered. When the space is 3546 * encountered reset it to a NULL for compliance 3547 * with ddi_getlongprop(). 3548 */ 3549 for (i = 0; ((dataname_ptr[i] != ' ') && 3550 (dataname_ptr[i] != '\0')); i++) { 3551 ; 3552 } 3553 3554 dataname_len += i; 3555 /* If not null terminated, Make it so */ 3556 if (dataname_ptr[i] == ' ') { 3557 dataname_ptr[i] = '\0'; 3558 } 3559 dataname_len++; 3560 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3561 "sd_process_sdconf_file: disk:%s, data:%s\n", 3562 vidptr, dataname_ptr); 3563 3564 /* Get the data list */ 3565 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3566 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3567 != DDI_PROP_SUCCESS) { 3568 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3569 "sd_process_sdconf_file: data property (%s)" 3570 " has no value\n", dataname_ptr); 3571 dataname_ptr = dnlist_ptr + dataname_len; 3572 continue; 3573 } 3574 3575 version = data_list[0]; 3576 3577 if (version == SD_CONF_VERSION_1) { 3578 sd_tunables values; 3579 3580 /* Set the properties */ 3581 if (sd_chk_vers1_data(un, data_list[1], 3582 &data_list[2], data_list_len, dataname_ptr) 3583 == SD_SUCCESS) { 3584 sd_get_tunables_from_conf(un, 3585 data_list[1], &data_list[2], 3586 &values); 3587 sd_set_vers1_properties(un, 3588 data_list[1], &values); 3589 rval = SD_SUCCESS; 3590 } else { 3591 rval = SD_FAILURE; 3592 } 3593 } else { 3594 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3595 "data property %s version 0x%x is invalid.", 3596 dataname_ptr, version); 3597 rval = SD_FAILURE; 3598 } 3599 kmem_free(data_list, data_list_len); 3600 dataname_ptr = dnlist_ptr + dataname_len; 3601 } 3602 } 3603 3604 /* free up the memory allocated by ddi_getlongprop */ 3605 if (config_list) { 3606 kmem_free(config_list, config_list_len); 3607 } 3608 3609 return (rval); 3610 } 3611 3612 /* 3613 * Function: sd_get_tunables_from_conf() 3614 * 3615 * 3616 * This function reads the data list from the sd.conf file and pulls 3617 * the values that can have numeric values as arguments and places 3618 * the values in the appropriate sd_tunables member. 3619 * Since the order of the data list members varies across platforms 3620 * This function reads them from the data list in a platform specific 3621 * order and places them into the correct sd_tunable member that is 3622 * consistent across all platforms. 3623 */ 3624 static void 3625 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3626 sd_tunables *values) 3627 { 3628 int i; 3629 int mask; 3630 3631 bzero(values, sizeof (sd_tunables)); 3632 3633 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3634 3635 mask = 1 << i; 3636 if (mask > flags) { 3637 break; 3638 } 3639 3640 switch (mask & flags) { 3641 case 0: /* This mask bit not set in flags */ 3642 continue; 3643 case SD_CONF_BSET_THROTTLE: 3644 values->sdt_throttle = data_list[i]; 3645 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3646 "sd_get_tunables_from_conf: throttle = %d\n", 3647 values->sdt_throttle); 3648 break; 3649 case SD_CONF_BSET_CTYPE: 3650 values->sdt_ctype = data_list[i]; 3651 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3652 "sd_get_tunables_from_conf: ctype = %d\n", 3653 values->sdt_ctype); 3654 break; 3655 case SD_CONF_BSET_NRR_COUNT: 3656 values->sdt_not_rdy_retries = data_list[i]; 3657 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3658 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3659 values->sdt_not_rdy_retries); 3660 break; 3661 case SD_CONF_BSET_BSY_RETRY_COUNT: 3662 values->sdt_busy_retries = data_list[i]; 3663 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3664 "sd_get_tunables_from_conf: busy_retries = %d\n", 3665 values->sdt_busy_retries); 3666 break; 3667 case SD_CONF_BSET_RST_RETRIES: 3668 values->sdt_reset_retries = data_list[i]; 3669 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3670 "sd_get_tunables_from_conf: reset_retries = %d\n", 3671 values->sdt_reset_retries); 3672 break; 3673 case SD_CONF_BSET_RSV_REL_TIME: 3674 values->sdt_reserv_rel_time = data_list[i]; 3675 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3676 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3677 values->sdt_reserv_rel_time); 3678 break; 3679 case SD_CONF_BSET_MIN_THROTTLE: 3680 values->sdt_min_throttle = data_list[i]; 3681 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3682 "sd_get_tunables_from_conf: min_throttle = %d\n", 3683 values->sdt_min_throttle); 3684 break; 3685 case SD_CONF_BSET_DISKSORT_DISABLED: 3686 values->sdt_disk_sort_dis = data_list[i]; 3687 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3688 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3689 values->sdt_disk_sort_dis); 3690 break; 3691 case SD_CONF_BSET_LUN_RESET_ENABLED: 3692 values->sdt_lun_reset_enable = data_list[i]; 3693 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3694 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3695 "\n", values->sdt_lun_reset_enable); 3696 break; 3697 } 3698 } 3699 } 3700 3701 /* 3702 * Function: sd_process_sdconf_table 3703 * 3704 * Description: Search the static configuration table for a match on the 3705 * inquiry vid/pid and update the driver soft state structure 3706 * according to the table property values for the device. 3707 * 3708 * The form of a configuration table entry is: 3709 * <vid+pid>,<flags>,<property-data> 3710 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3711 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3712 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3713 * 3714 * Arguments: un - driver soft state (unit) structure 3715 */ 3716 3717 static void 3718 sd_process_sdconf_table(struct sd_lun *un) 3719 { 3720 char *id = NULL; 3721 int table_index; 3722 int idlen; 3723 3724 ASSERT(un != NULL); 3725 for (table_index = 0; table_index < sd_disk_table_size; 3726 table_index++) { 3727 id = sd_disk_table[table_index].device_id; 3728 idlen = strlen(id); 3729 if (idlen == 0) { 3730 continue; 3731 } 3732 3733 /* 3734 * The static configuration table currently does not 3735 * implement version 10 properties. Additionally, 3736 * multiple data-property-name entries are not 3737 * implemented in the static configuration table. 3738 */ 3739 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3740 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3741 "sd_process_sdconf_table: disk %s\n", id); 3742 sd_set_vers1_properties(un, 3743 sd_disk_table[table_index].flags, 3744 sd_disk_table[table_index].properties); 3745 break; 3746 } 3747 } 3748 } 3749 3750 3751 /* 3752 * Function: sd_sdconf_id_match 3753 * 3754 * Description: This local function implements a case sensitive vid/pid 3755 * comparison as well as the boundary cases of wild card and 3756 * multiple blanks. 3757 * 3758 * Note: An implicit assumption made here is that the scsi 3759 * inquiry structure will always keep the vid, pid and 3760 * revision strings in consecutive sequence, so they can be 3761 * read as a single string. If this assumption is not the 3762 * case, a separate string, to be used for the check, needs 3763 * to be built with these strings concatenated. 3764 * 3765 * Arguments: un - driver soft state (unit) structure 3766 * id - table or config file vid/pid 3767 * idlen - length of the vid/pid (bytes) 3768 * 3769 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3770 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3771 */ 3772 3773 static int 3774 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3775 { 3776 struct scsi_inquiry *sd_inq; 3777 int rval = SD_SUCCESS; 3778 3779 ASSERT(un != NULL); 3780 sd_inq = un->un_sd->sd_inq; 3781 ASSERT(id != NULL); 3782 3783 /* 3784 * We use the inq_vid as a pointer to a buffer containing the 3785 * vid and pid and use the entire vid/pid length of the table 3786 * entry for the comparison. This works because the inq_pid 3787 * data member follows inq_vid in the scsi_inquiry structure. 3788 */ 3789 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3790 /* 3791 * The user id string is compared to the inquiry vid/pid 3792 * using a case insensitive comparison and ignoring 3793 * multiple spaces. 3794 */ 3795 rval = sd_blank_cmp(un, id, idlen); 3796 if (rval != SD_SUCCESS) { 3797 /* 3798 * User id strings that start and end with a "*" 3799 * are a special case. These do not have a 3800 * specific vendor, and the product string can 3801 * appear anywhere in the 16 byte PID portion of 3802 * the inquiry data. This is a simple strstr() 3803 * type search for the user id in the inquiry data. 3804 */ 3805 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3806 char *pidptr = &id[1]; 3807 int i; 3808 int j; 3809 int pidstrlen = idlen - 2; 3810 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3811 pidstrlen; 3812 3813 if (j < 0) { 3814 return (SD_FAILURE); 3815 } 3816 for (i = 0; i < j; i++) { 3817 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3818 pidptr, pidstrlen) == 0) { 3819 rval = SD_SUCCESS; 3820 break; 3821 } 3822 } 3823 } 3824 } 3825 } 3826 return (rval); 3827 } 3828 3829 3830 /* 3831 * Function: sd_blank_cmp 3832 * 3833 * Description: If the id string starts and ends with a space, treat 3834 * multiple consecutive spaces as equivalent to a single 3835 * space. For example, this causes a sd_disk_table entry 3836 * of " NEC CDROM " to match a device's id string of 3837 * "NEC CDROM". 3838 * 3839 * Note: The success exit condition for this routine is if 3840 * the pointer to the table entry is '\0' and the cnt of 3841 * the inquiry length is zero. This will happen if the inquiry 3842 * string returned by the device is padded with spaces to be 3843 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3844 * SCSI spec states that the inquiry string is to be padded with 3845 * spaces. 3846 * 3847 * Arguments: un - driver soft state (unit) structure 3848 * id - table or config file vid/pid 3849 * idlen - length of the vid/pid (bytes) 3850 * 3851 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3852 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3853 */ 3854 3855 static int 3856 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3857 { 3858 char *p1; 3859 char *p2; 3860 int cnt; 3861 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3862 sizeof (SD_INQUIRY(un)->inq_pid); 3863 3864 ASSERT(un != NULL); 3865 p2 = un->un_sd->sd_inq->inq_vid; 3866 ASSERT(id != NULL); 3867 p1 = id; 3868 3869 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3870 /* 3871 * Note: string p1 is terminated by a NUL but string p2 3872 * isn't. The end of p2 is determined by cnt. 3873 */ 3874 for (;;) { 3875 /* skip over any extra blanks in both strings */ 3876 while ((*p1 != '\0') && (*p1 == ' ')) { 3877 p1++; 3878 } 3879 while ((cnt != 0) && (*p2 == ' ')) { 3880 p2++; 3881 cnt--; 3882 } 3883 3884 /* compare the two strings */ 3885 if ((cnt == 0) || 3886 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3887 break; 3888 } 3889 while ((cnt > 0) && 3890 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3891 p1++; 3892 p2++; 3893 cnt--; 3894 } 3895 } 3896 } 3897 3898 /* return SD_SUCCESS if both strings match */ 3899 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3900 } 3901 3902 3903 /* 3904 * Function: sd_chk_vers1_data 3905 * 3906 * Description: Verify the version 1 device properties provided by the 3907 * user via the configuration file 3908 * 3909 * Arguments: un - driver soft state (unit) structure 3910 * flags - integer mask indicating properties to be set 3911 * prop_list - integer list of property values 3912 * list_len - length of user provided data 3913 * 3914 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3915 * SD_FAILURE - Indicates the user provided data is invalid 3916 */ 3917 3918 static int 3919 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3920 int list_len, char *dataname_ptr) 3921 { 3922 int i; 3923 int mask = 1; 3924 int index = 0; 3925 3926 ASSERT(un != NULL); 3927 3928 /* Check for a NULL property name and list */ 3929 if (dataname_ptr == NULL) { 3930 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3931 "sd_chk_vers1_data: NULL data property name."); 3932 return (SD_FAILURE); 3933 } 3934 if (prop_list == NULL) { 3935 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3936 "sd_chk_vers1_data: %s NULL data property list.", 3937 dataname_ptr); 3938 return (SD_FAILURE); 3939 } 3940 3941 /* Display a warning if undefined bits are set in the flags */ 3942 if (flags & ~SD_CONF_BIT_MASK) { 3943 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3944 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3945 "Properties not set.", 3946 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3947 return (SD_FAILURE); 3948 } 3949 3950 /* 3951 * Verify the length of the list by identifying the highest bit set 3952 * in the flags and validating that the property list has a length 3953 * up to the index of this bit. 3954 */ 3955 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3956 if (flags & mask) { 3957 index++; 3958 } 3959 mask = 1 << i; 3960 } 3961 if ((list_len / sizeof (int)) < (index + 2)) { 3962 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3963 "sd_chk_vers1_data: " 3964 "Data property list %s size is incorrect. " 3965 "Properties not set.", dataname_ptr); 3966 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3967 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3968 return (SD_FAILURE); 3969 } 3970 return (SD_SUCCESS); 3971 } 3972 3973 3974 /* 3975 * Function: sd_set_vers1_properties 3976 * 3977 * Description: Set version 1 device properties based on a property list 3978 * retrieved from the driver configuration file or static 3979 * configuration table. Version 1 properties have the format: 3980 * 3981 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3982 * 3983 * where the prop0 value will be used to set prop0 if bit0 3984 * is set in the flags 3985 * 3986 * Arguments: un - driver soft state (unit) structure 3987 * flags - integer mask indicating properties to be set 3988 * prop_list - integer list of property values 3989 */ 3990 3991 static void 3992 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 3993 { 3994 ASSERT(un != NULL); 3995 3996 /* 3997 * Set the flag to indicate cache is to be disabled. An attempt 3998 * to disable the cache via sd_cache_control() will be made 3999 * later during attach once the basic initialization is complete. 4000 */ 4001 if (flags & SD_CONF_BSET_NOCACHE) { 4002 un->un_f_opt_disable_cache = TRUE; 4003 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4004 "sd_set_vers1_properties: caching disabled flag set\n"); 4005 } 4006 4007 /* CD-specific configuration parameters */ 4008 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4009 un->un_f_cfg_playmsf_bcd = TRUE; 4010 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4011 "sd_set_vers1_properties: playmsf_bcd set\n"); 4012 } 4013 if (flags & SD_CONF_BSET_READSUB_BCD) { 4014 un->un_f_cfg_readsub_bcd = TRUE; 4015 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4016 "sd_set_vers1_properties: readsub_bcd set\n"); 4017 } 4018 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4019 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4020 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4021 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4022 } 4023 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4024 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4025 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4026 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4027 } 4028 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4029 un->un_f_cfg_no_read_header = TRUE; 4030 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4031 "sd_set_vers1_properties: no_read_header set\n"); 4032 } 4033 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4034 un->un_f_cfg_read_cd_xd4 = TRUE; 4035 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4036 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4037 } 4038 4039 /* Support for devices which do not have valid/unique serial numbers */ 4040 if (flags & SD_CONF_BSET_FAB_DEVID) { 4041 un->un_f_opt_fab_devid = TRUE; 4042 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4043 "sd_set_vers1_properties: fab_devid bit set\n"); 4044 } 4045 4046 /* Support for user throttle configuration */ 4047 if (flags & SD_CONF_BSET_THROTTLE) { 4048 ASSERT(prop_list != NULL); 4049 un->un_saved_throttle = un->un_throttle = 4050 prop_list->sdt_throttle; 4051 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4052 "sd_set_vers1_properties: throttle set to %d\n", 4053 prop_list->sdt_throttle); 4054 } 4055 4056 /* Set the per disk retry count according to the conf file or table. */ 4057 if (flags & SD_CONF_BSET_NRR_COUNT) { 4058 ASSERT(prop_list != NULL); 4059 if (prop_list->sdt_not_rdy_retries) { 4060 un->un_notready_retry_count = 4061 prop_list->sdt_not_rdy_retries; 4062 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4063 "sd_set_vers1_properties: not ready retry count" 4064 " set to %d\n", un->un_notready_retry_count); 4065 } 4066 } 4067 4068 /* The controller type is reported for generic disk driver ioctls */ 4069 if (flags & SD_CONF_BSET_CTYPE) { 4070 ASSERT(prop_list != NULL); 4071 switch (prop_list->sdt_ctype) { 4072 case CTYPE_CDROM: 4073 un->un_ctype = prop_list->sdt_ctype; 4074 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4075 "sd_set_vers1_properties: ctype set to " 4076 "CTYPE_CDROM\n"); 4077 break; 4078 case CTYPE_CCS: 4079 un->un_ctype = prop_list->sdt_ctype; 4080 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4081 "sd_set_vers1_properties: ctype set to " 4082 "CTYPE_CCS\n"); 4083 break; 4084 case CTYPE_ROD: /* RW optical */ 4085 un->un_ctype = prop_list->sdt_ctype; 4086 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4087 "sd_set_vers1_properties: ctype set to " 4088 "CTYPE_ROD\n"); 4089 break; 4090 default: 4091 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4092 "sd_set_vers1_properties: Could not set " 4093 "invalid ctype value (%d)", 4094 prop_list->sdt_ctype); 4095 } 4096 } 4097 4098 /* Purple failover timeout */ 4099 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4100 ASSERT(prop_list != NULL); 4101 un->un_busy_retry_count = 4102 prop_list->sdt_busy_retries; 4103 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4104 "sd_set_vers1_properties: " 4105 "busy retry count set to %d\n", 4106 un->un_busy_retry_count); 4107 } 4108 4109 /* Purple reset retry count */ 4110 if (flags & SD_CONF_BSET_RST_RETRIES) { 4111 ASSERT(prop_list != NULL); 4112 un->un_reset_retry_count = 4113 prop_list->sdt_reset_retries; 4114 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4115 "sd_set_vers1_properties: " 4116 "reset retry count set to %d\n", 4117 un->un_reset_retry_count); 4118 } 4119 4120 /* Purple reservation release timeout */ 4121 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4122 ASSERT(prop_list != NULL); 4123 un->un_reserve_release_time = 4124 prop_list->sdt_reserv_rel_time; 4125 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4126 "sd_set_vers1_properties: " 4127 "reservation release timeout set to %d\n", 4128 un->un_reserve_release_time); 4129 } 4130 4131 /* 4132 * Driver flag telling the driver to verify that no commands are pending 4133 * for a device before issuing a Test Unit Ready. This is a workaround 4134 * for a firmware bug in some Seagate eliteI drives. 4135 */ 4136 if (flags & SD_CONF_BSET_TUR_CHECK) { 4137 un->un_f_cfg_tur_check = TRUE; 4138 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4139 "sd_set_vers1_properties: tur queue check set\n"); 4140 } 4141 4142 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4143 un->un_min_throttle = prop_list->sdt_min_throttle; 4144 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4145 "sd_set_vers1_properties: min throttle set to %d\n", 4146 un->un_min_throttle); 4147 } 4148 4149 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4150 un->un_f_disksort_disabled = 4151 (prop_list->sdt_disk_sort_dis != 0) ? 4152 TRUE : FALSE; 4153 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4154 "sd_set_vers1_properties: disksort disabled " 4155 "flag set to %d\n", 4156 prop_list->sdt_disk_sort_dis); 4157 } 4158 4159 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4160 un->un_f_lun_reset_enabled = 4161 (prop_list->sdt_lun_reset_enable != 0) ? 4162 TRUE : FALSE; 4163 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4164 "sd_set_vers1_properties: lun reset enabled " 4165 "flag set to %d\n", 4166 prop_list->sdt_lun_reset_enable); 4167 } 4168 4169 /* 4170 * Validate the throttle values. 4171 * If any of the numbers are invalid, set everything to defaults. 4172 */ 4173 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4174 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4175 (un->un_min_throttle > un->un_throttle)) { 4176 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4177 un->un_min_throttle = sd_min_throttle; 4178 } 4179 } 4180 4181 /* 4182 * Function: sd_is_lsi() 4183 * 4184 * Description: Check for lsi devices, step through the static device 4185 * table to match vid/pid. 4186 * 4187 * Args: un - ptr to sd_lun 4188 * 4189 * Notes: When creating new LSI property, need to add the new LSI property 4190 * to this function. 4191 */ 4192 static void 4193 sd_is_lsi(struct sd_lun *un) 4194 { 4195 char *id = NULL; 4196 int table_index; 4197 int idlen; 4198 void *prop; 4199 4200 ASSERT(un != NULL); 4201 for (table_index = 0; table_index < sd_disk_table_size; 4202 table_index++) { 4203 id = sd_disk_table[table_index].device_id; 4204 idlen = strlen(id); 4205 if (idlen == 0) { 4206 continue; 4207 } 4208 4209 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4210 prop = sd_disk_table[table_index].properties; 4211 if (prop == &lsi_properties || 4212 prop == &lsi_oem_properties || 4213 prop == &lsi_properties_scsi || 4214 prop == &symbios_properties) { 4215 un->un_f_cfg_is_lsi = TRUE; 4216 } 4217 break; 4218 } 4219 } 4220 } 4221 4222 /* 4223 * Function: sd_get_physical_geometry 4224 * 4225 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4226 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4227 * target, and use this information to initialize the physical 4228 * geometry cache specified by pgeom_p. 4229 * 4230 * MODE SENSE is an optional command, so failure in this case 4231 * does not necessarily denote an error. We want to use the 4232 * MODE SENSE commands to derive the physical geometry of the 4233 * device, but if either command fails, the logical geometry is 4234 * used as the fallback for disk label geometry in cmlb. 4235 * 4236 * This requires that un->un_blockcount and un->un_tgt_blocksize 4237 * have already been initialized for the current target and 4238 * that the current values be passed as args so that we don't 4239 * end up ever trying to use -1 as a valid value. This could 4240 * happen if either value is reset while we're not holding 4241 * the mutex. 4242 * 4243 * Arguments: un - driver soft state (unit) structure 4244 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4245 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4246 * to use the USCSI "direct" chain and bypass the normal 4247 * command waitq. 4248 * 4249 * Context: Kernel thread only (can sleep). 4250 */ 4251 4252 static int 4253 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4254 diskaddr_t capacity, int lbasize, int path_flag) 4255 { 4256 struct mode_format *page3p; 4257 struct mode_geometry *page4p; 4258 struct mode_header *headerp; 4259 int sector_size; 4260 int nsect; 4261 int nhead; 4262 int ncyl; 4263 int intrlv; 4264 int spc; 4265 diskaddr_t modesense_capacity; 4266 int rpm; 4267 int bd_len; 4268 int mode_header_length; 4269 uchar_t *p3bufp; 4270 uchar_t *p4bufp; 4271 int cdbsize; 4272 int ret = EIO; 4273 4274 ASSERT(un != NULL); 4275 4276 if (lbasize == 0) { 4277 if (ISCD(un)) { 4278 lbasize = 2048; 4279 } else { 4280 lbasize = un->un_sys_blocksize; 4281 } 4282 } 4283 pgeom_p->g_secsize = (unsigned short)lbasize; 4284 4285 /* 4286 * If the unit is a cd/dvd drive MODE SENSE page three 4287 * and MODE SENSE page four are reserved (see SBC spec 4288 * and MMC spec). To prevent soft errors just return 4289 * using the default LBA size. 4290 */ 4291 if (ISCD(un)) 4292 return (ret); 4293 4294 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4295 4296 /* 4297 * Retrieve MODE SENSE page 3 - Format Device Page 4298 */ 4299 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4300 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4301 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4302 != 0) { 4303 SD_ERROR(SD_LOG_COMMON, un, 4304 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4305 goto page3_exit; 4306 } 4307 4308 /* 4309 * Determine size of Block Descriptors in order to locate the mode 4310 * page data. ATAPI devices return 0, SCSI devices should return 4311 * MODE_BLK_DESC_LENGTH. 4312 */ 4313 headerp = (struct mode_header *)p3bufp; 4314 if (un->un_f_cfg_is_atapi == TRUE) { 4315 struct mode_header_grp2 *mhp = 4316 (struct mode_header_grp2 *)headerp; 4317 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4318 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4319 } else { 4320 mode_header_length = MODE_HEADER_LENGTH; 4321 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4322 } 4323 4324 if (bd_len > MODE_BLK_DESC_LENGTH) { 4325 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4326 "received unexpected bd_len of %d, page3\n", bd_len); 4327 goto page3_exit; 4328 } 4329 4330 page3p = (struct mode_format *) 4331 ((caddr_t)headerp + mode_header_length + bd_len); 4332 4333 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4334 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4335 "mode sense pg3 code mismatch %d\n", 4336 page3p->mode_page.code); 4337 goto page3_exit; 4338 } 4339 4340 /* 4341 * Use this physical geometry data only if BOTH MODE SENSE commands 4342 * complete successfully; otherwise, revert to the logical geometry. 4343 * So, we need to save everything in temporary variables. 4344 */ 4345 sector_size = BE_16(page3p->data_bytes_sect); 4346 4347 /* 4348 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4349 */ 4350 if (sector_size == 0) { 4351 sector_size = un->un_sys_blocksize; 4352 } else { 4353 sector_size &= ~(un->un_sys_blocksize - 1); 4354 } 4355 4356 nsect = BE_16(page3p->sect_track); 4357 intrlv = BE_16(page3p->interleave); 4358 4359 SD_INFO(SD_LOG_COMMON, un, 4360 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4361 SD_INFO(SD_LOG_COMMON, un, 4362 " mode page: %d; nsect: %d; sector size: %d;\n", 4363 page3p->mode_page.code, nsect, sector_size); 4364 SD_INFO(SD_LOG_COMMON, un, 4365 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4366 BE_16(page3p->track_skew), 4367 BE_16(page3p->cylinder_skew)); 4368 4369 4370 /* 4371 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4372 */ 4373 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4374 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4375 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4376 != 0) { 4377 SD_ERROR(SD_LOG_COMMON, un, 4378 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4379 goto page4_exit; 4380 } 4381 4382 /* 4383 * Determine size of Block Descriptors in order to locate the mode 4384 * page data. ATAPI devices return 0, SCSI devices should return 4385 * MODE_BLK_DESC_LENGTH. 4386 */ 4387 headerp = (struct mode_header *)p4bufp; 4388 if (un->un_f_cfg_is_atapi == TRUE) { 4389 struct mode_header_grp2 *mhp = 4390 (struct mode_header_grp2 *)headerp; 4391 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4392 } else { 4393 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4394 } 4395 4396 if (bd_len > MODE_BLK_DESC_LENGTH) { 4397 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4398 "received unexpected bd_len of %d, page4\n", bd_len); 4399 goto page4_exit; 4400 } 4401 4402 page4p = (struct mode_geometry *) 4403 ((caddr_t)headerp + mode_header_length + bd_len); 4404 4405 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4406 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4407 "mode sense pg4 code mismatch %d\n", 4408 page4p->mode_page.code); 4409 goto page4_exit; 4410 } 4411 4412 /* 4413 * Stash the data now, after we know that both commands completed. 4414 */ 4415 4416 4417 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4418 spc = nhead * nsect; 4419 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4420 rpm = BE_16(page4p->rpm); 4421 4422 modesense_capacity = spc * ncyl; 4423 4424 SD_INFO(SD_LOG_COMMON, un, 4425 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4426 SD_INFO(SD_LOG_COMMON, un, 4427 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4428 SD_INFO(SD_LOG_COMMON, un, 4429 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4430 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4431 (void *)pgeom_p, capacity); 4432 4433 /* 4434 * Compensate if the drive's geometry is not rectangular, i.e., 4435 * the product of C * H * S returned by MODE SENSE >= that returned 4436 * by read capacity. This is an idiosyncrasy of the original x86 4437 * disk subsystem. 4438 */ 4439 if (modesense_capacity >= capacity) { 4440 SD_INFO(SD_LOG_COMMON, un, 4441 "sd_get_physical_geometry: adjusting acyl; " 4442 "old: %d; new: %d\n", pgeom_p->g_acyl, 4443 (modesense_capacity - capacity + spc - 1) / spc); 4444 if (sector_size != 0) { 4445 /* 1243403: NEC D38x7 drives don't support sec size */ 4446 pgeom_p->g_secsize = (unsigned short)sector_size; 4447 } 4448 pgeom_p->g_nsect = (unsigned short)nsect; 4449 pgeom_p->g_nhead = (unsigned short)nhead; 4450 pgeom_p->g_capacity = capacity; 4451 pgeom_p->g_acyl = 4452 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 4453 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 4454 } 4455 4456 pgeom_p->g_rpm = (unsigned short)rpm; 4457 pgeom_p->g_intrlv = (unsigned short)intrlv; 4458 ret = 0; 4459 4460 SD_INFO(SD_LOG_COMMON, un, 4461 "sd_get_physical_geometry: mode sense geometry:\n"); 4462 SD_INFO(SD_LOG_COMMON, un, 4463 " nsect: %d; sector size: %d; interlv: %d\n", 4464 nsect, sector_size, intrlv); 4465 SD_INFO(SD_LOG_COMMON, un, 4466 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 4467 nhead, ncyl, rpm, modesense_capacity); 4468 SD_INFO(SD_LOG_COMMON, un, 4469 "sd_get_physical_geometry: (cached)\n"); 4470 SD_INFO(SD_LOG_COMMON, un, 4471 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4472 pgeom_p->g_ncyl, pgeom_p->g_acyl, 4473 pgeom_p->g_nhead, pgeom_p->g_nsect); 4474 SD_INFO(SD_LOG_COMMON, un, 4475 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 4476 pgeom_p->g_secsize, pgeom_p->g_capacity, 4477 pgeom_p->g_intrlv, pgeom_p->g_rpm); 4478 4479 page4_exit: 4480 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 4481 page3_exit: 4482 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 4483 4484 return (ret); 4485 } 4486 4487 /* 4488 * Function: sd_get_virtual_geometry 4489 * 4490 * Description: Ask the controller to tell us about the target device. 4491 * 4492 * Arguments: un - pointer to softstate 4493 * capacity - disk capacity in #blocks 4494 * lbasize - disk block size in bytes 4495 * 4496 * Context: Kernel thread only 4497 */ 4498 4499 static int 4500 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 4501 diskaddr_t capacity, int lbasize) 4502 { 4503 uint_t geombuf; 4504 int spc; 4505 4506 ASSERT(un != NULL); 4507 4508 /* Set sector size, and total number of sectors */ 4509 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 4510 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 4511 4512 /* Let the HBA tell us its geometry */ 4513 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 4514 4515 /* A value of -1 indicates an undefined "geometry" property */ 4516 if (geombuf == (-1)) { 4517 return (EINVAL); 4518 } 4519 4520 /* Initialize the logical geometry cache. */ 4521 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 4522 lgeom_p->g_nsect = geombuf & 0xffff; 4523 lgeom_p->g_secsize = un->un_sys_blocksize; 4524 4525 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 4526 4527 /* 4528 * Note: The driver originally converted the capacity value from 4529 * target blocks to system blocks. However, the capacity value passed 4530 * to this routine is already in terms of system blocks (this scaling 4531 * is done when the READ CAPACITY command is issued and processed). 4532 * This 'error' may have gone undetected because the usage of g_ncyl 4533 * (which is based upon g_capacity) is very limited within the driver 4534 */ 4535 lgeom_p->g_capacity = capacity; 4536 4537 /* 4538 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 4539 * hba may return zero values if the device has been removed. 4540 */ 4541 if (spc == 0) { 4542 lgeom_p->g_ncyl = 0; 4543 } else { 4544 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 4545 } 4546 lgeom_p->g_acyl = 0; 4547 4548 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 4549 return (0); 4550 4551 } 4552 /* 4553 * Function: sd_update_block_info 4554 * 4555 * Description: Calculate a byte count to sector count bitshift value 4556 * from sector size. 4557 * 4558 * Arguments: un: unit struct. 4559 * lbasize: new target sector size 4560 * capacity: new target capacity, ie. block count 4561 * 4562 * Context: Kernel thread context 4563 */ 4564 4565 static void 4566 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 4567 { 4568 uint_t dblk; 4569 4570 if (lbasize != 0) { 4571 un->un_tgt_blocksize = lbasize; 4572 un->un_f_tgt_blocksize_is_valid = TRUE; 4573 } 4574 4575 if (capacity != 0) { 4576 un->un_blockcount = capacity; 4577 un->un_f_blockcount_is_valid = TRUE; 4578 } 4579 4580 /* 4581 * Update device capacity properties. 4582 * 4583 * 'device-nblocks' number of blocks in target's units 4584 * 'device-blksize' data bearing size of target's block 4585 * 4586 * NOTE: math is complicated by the fact that un_tgt_blocksize may 4587 * not be a power of two for checksumming disks with 520/528 byte 4588 * sectors. 4589 */ 4590 if (un->un_f_tgt_blocksize_is_valid && 4591 un->un_f_blockcount_is_valid && 4592 un->un_sys_blocksize) { 4593 dblk = un->un_tgt_blocksize / un->un_sys_blocksize; 4594 (void) ddi_prop_update_int64(DDI_DEV_T_NONE, SD_DEVINFO(un), 4595 "device-nblocks", un->un_blockcount / dblk); 4596 /* 4597 * To save memory, only define "device-blksize" when its 4598 * value is differnet than the default DEV_BSIZE value. 4599 */ 4600 if ((un->un_sys_blocksize * dblk) != DEV_BSIZE) 4601 (void) ddi_prop_update_int(DDI_DEV_T_NONE, 4602 SD_DEVINFO(un), "device-blksize", 4603 un->un_sys_blocksize * dblk); 4604 } 4605 } 4606 4607 4608 /* 4609 * Function: sd_register_devid 4610 * 4611 * Description: This routine will obtain the device id information from the 4612 * target, obtain the serial number, and register the device 4613 * id with the ddi framework. 4614 * 4615 * Arguments: devi - the system's dev_info_t for the device. 4616 * un - driver soft state (unit) structure 4617 * reservation_flag - indicates if a reservation conflict 4618 * occurred during attach 4619 * 4620 * Context: Kernel Thread 4621 */ 4622 static void 4623 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 4624 { 4625 int rval = 0; 4626 uchar_t *inq80 = NULL; 4627 size_t inq80_len = MAX_INQUIRY_SIZE; 4628 size_t inq80_resid = 0; 4629 uchar_t *inq83 = NULL; 4630 size_t inq83_len = MAX_INQUIRY_SIZE; 4631 size_t inq83_resid = 0; 4632 int dlen, len; 4633 char *sn; 4634 4635 ASSERT(un != NULL); 4636 ASSERT(mutex_owned(SD_MUTEX(un))); 4637 ASSERT((SD_DEVINFO(un)) == devi); 4638 4639 /* 4640 * If transport has already registered a devid for this target 4641 * then that takes precedence over the driver's determination 4642 * of the devid. 4643 */ 4644 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 4645 ASSERT(un->un_devid); 4646 return; /* use devid registered by the transport */ 4647 } 4648 4649 /* 4650 * This is the case of antiquated Sun disk drives that have the 4651 * FAB_DEVID property set in the disk_table. These drives 4652 * manage the devid's by storing them in last 2 available sectors 4653 * on the drive and have them fabricated by the ddi layer by calling 4654 * ddi_devid_init and passing the DEVID_FAB flag. 4655 */ 4656 if (un->un_f_opt_fab_devid == TRUE) { 4657 /* 4658 * Depending on EINVAL isn't reliable, since a reserved disk 4659 * may result in invalid geometry, so check to make sure a 4660 * reservation conflict did not occur during attach. 4661 */ 4662 if ((sd_get_devid(un) == EINVAL) && 4663 (reservation_flag != SD_TARGET_IS_RESERVED)) { 4664 /* 4665 * The devid is invalid AND there is no reservation 4666 * conflict. Fabricate a new devid. 4667 */ 4668 (void) sd_create_devid(un); 4669 } 4670 4671 /* Register the devid if it exists */ 4672 if (un->un_devid != NULL) { 4673 (void) ddi_devid_register(SD_DEVINFO(un), 4674 un->un_devid); 4675 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4676 "sd_register_devid: Devid Fabricated\n"); 4677 } 4678 return; 4679 } 4680 4681 /* 4682 * We check the availibility of the World Wide Name (0x83) and Unit 4683 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 4684 * un_vpd_page_mask from them, we decide which way to get the WWN. If 4685 * 0x83 is availible, that is the best choice. Our next choice is 4686 * 0x80. If neither are availible, we munge the devid from the device 4687 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 4688 * to fabricate a devid for non-Sun qualified disks. 4689 */ 4690 if (sd_check_vpd_page_support(un) == 0) { 4691 /* collect page 80 data if available */ 4692 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 4693 4694 mutex_exit(SD_MUTEX(un)); 4695 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 4696 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 4697 0x01, 0x80, &inq80_resid); 4698 4699 if (rval != 0) { 4700 kmem_free(inq80, inq80_len); 4701 inq80 = NULL; 4702 inq80_len = 0; 4703 } else if (ddi_prop_exists( 4704 DDI_DEV_T_NONE, SD_DEVINFO(un), 4705 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 4706 INQUIRY_SERIAL_NO) == 0) { 4707 /* 4708 * If we don't already have a serial number 4709 * property, do quick verify of data returned 4710 * and define property. 4711 */ 4712 dlen = inq80_len - inq80_resid; 4713 len = (size_t)inq80[3]; 4714 if ((dlen >= 4) && ((len + 4) <= dlen)) { 4715 /* 4716 * Ensure sn termination, skip leading 4717 * blanks, and create property 4718 * 'inquiry-serial-no'. 4719 */ 4720 sn = (char *)&inq80[4]; 4721 sn[len] = 0; 4722 while (*sn && (*sn == ' ')) 4723 sn++; 4724 if (*sn) { 4725 (void) ddi_prop_update_string( 4726 DDI_DEV_T_NONE, 4727 SD_DEVINFO(un), 4728 INQUIRY_SERIAL_NO, sn); 4729 } 4730 } 4731 } 4732 mutex_enter(SD_MUTEX(un)); 4733 } 4734 4735 /* collect page 83 data if available */ 4736 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 4737 mutex_exit(SD_MUTEX(un)); 4738 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 4739 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 4740 0x01, 0x83, &inq83_resid); 4741 4742 if (rval != 0) { 4743 kmem_free(inq83, inq83_len); 4744 inq83 = NULL; 4745 inq83_len = 0; 4746 } 4747 mutex_enter(SD_MUTEX(un)); 4748 } 4749 } 4750 4751 /* encode best devid possible based on data available */ 4752 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 4753 (char *)ddi_driver_name(SD_DEVINFO(un)), 4754 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 4755 inq80, inq80_len - inq80_resid, inq83, inq83_len - 4756 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 4757 4758 /* devid successfully encoded, register devid */ 4759 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 4760 4761 } else { 4762 /* 4763 * Unable to encode a devid based on data available. 4764 * This is not a Sun qualified disk. Older Sun disk 4765 * drives that have the SD_FAB_DEVID property 4766 * set in the disk_table and non Sun qualified 4767 * disks are treated in the same manner. These 4768 * drives manage the devid's by storing them in 4769 * last 2 available sectors on the drive and 4770 * have them fabricated by the ddi layer by 4771 * calling ddi_devid_init and passing the 4772 * DEVID_FAB flag. 4773 * Create a fabricate devid only if there's no 4774 * fabricate devid existed. 4775 */ 4776 if (sd_get_devid(un) == EINVAL) { 4777 (void) sd_create_devid(un); 4778 } 4779 un->un_f_opt_fab_devid = TRUE; 4780 4781 /* Register the devid if it exists */ 4782 if (un->un_devid != NULL) { 4783 (void) ddi_devid_register(SD_DEVINFO(un), 4784 un->un_devid); 4785 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4786 "sd_register_devid: devid fabricated using " 4787 "ddi framework\n"); 4788 } 4789 } 4790 4791 /* clean up resources */ 4792 if (inq80 != NULL) { 4793 kmem_free(inq80, inq80_len); 4794 } 4795 if (inq83 != NULL) { 4796 kmem_free(inq83, inq83_len); 4797 } 4798 } 4799 4800 4801 4802 /* 4803 * Function: sd_get_devid 4804 * 4805 * Description: This routine will return 0 if a valid device id has been 4806 * obtained from the target and stored in the soft state. If a 4807 * valid device id has not been previously read and stored, a 4808 * read attempt will be made. 4809 * 4810 * Arguments: un - driver soft state (unit) structure 4811 * 4812 * Return Code: 0 if we successfully get the device id 4813 * 4814 * Context: Kernel Thread 4815 */ 4816 4817 static int 4818 sd_get_devid(struct sd_lun *un) 4819 { 4820 struct dk_devid *dkdevid; 4821 ddi_devid_t tmpid; 4822 uint_t *ip; 4823 size_t sz; 4824 diskaddr_t blk; 4825 int status; 4826 int chksum; 4827 int i; 4828 size_t buffer_size; 4829 4830 ASSERT(un != NULL); 4831 ASSERT(mutex_owned(SD_MUTEX(un))); 4832 4833 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 4834 un); 4835 4836 if (un->un_devid != NULL) { 4837 return (0); 4838 } 4839 4840 mutex_exit(SD_MUTEX(un)); 4841 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4842 (void *)SD_PATH_DIRECT) != 0) { 4843 mutex_enter(SD_MUTEX(un)); 4844 return (EINVAL); 4845 } 4846 4847 /* 4848 * Read and verify device id, stored in the reserved cylinders at the 4849 * end of the disk. Backup label is on the odd sectors of the last 4850 * track of the last cylinder. Device id will be on track of the next 4851 * to last cylinder. 4852 */ 4853 mutex_enter(SD_MUTEX(un)); 4854 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 4855 mutex_exit(SD_MUTEX(un)); 4856 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 4857 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 4858 SD_PATH_DIRECT); 4859 if (status != 0) { 4860 goto error; 4861 } 4862 4863 /* Validate the revision */ 4864 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 4865 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 4866 status = EINVAL; 4867 goto error; 4868 } 4869 4870 /* Calculate the checksum */ 4871 chksum = 0; 4872 ip = (uint_t *)dkdevid; 4873 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 4874 i++) { 4875 chksum ^= ip[i]; 4876 } 4877 4878 /* Compare the checksums */ 4879 if (DKD_GETCHKSUM(dkdevid) != chksum) { 4880 status = EINVAL; 4881 goto error; 4882 } 4883 4884 /* Validate the device id */ 4885 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 4886 status = EINVAL; 4887 goto error; 4888 } 4889 4890 /* 4891 * Store the device id in the driver soft state 4892 */ 4893 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 4894 tmpid = kmem_alloc(sz, KM_SLEEP); 4895 4896 mutex_enter(SD_MUTEX(un)); 4897 4898 un->un_devid = tmpid; 4899 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 4900 4901 kmem_free(dkdevid, buffer_size); 4902 4903 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 4904 4905 return (status); 4906 error: 4907 mutex_enter(SD_MUTEX(un)); 4908 kmem_free(dkdevid, buffer_size); 4909 return (status); 4910 } 4911 4912 4913 /* 4914 * Function: sd_create_devid 4915 * 4916 * Description: This routine will fabricate the device id and write it 4917 * to the disk. 4918 * 4919 * Arguments: un - driver soft state (unit) structure 4920 * 4921 * Return Code: value of the fabricated device id 4922 * 4923 * Context: Kernel Thread 4924 */ 4925 4926 static ddi_devid_t 4927 sd_create_devid(struct sd_lun *un) 4928 { 4929 ASSERT(un != NULL); 4930 4931 /* Fabricate the devid */ 4932 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 4933 == DDI_FAILURE) { 4934 return (NULL); 4935 } 4936 4937 /* Write the devid to disk */ 4938 if (sd_write_deviceid(un) != 0) { 4939 ddi_devid_free(un->un_devid); 4940 un->un_devid = NULL; 4941 } 4942 4943 return (un->un_devid); 4944 } 4945 4946 4947 /* 4948 * Function: sd_write_deviceid 4949 * 4950 * Description: This routine will write the device id to the disk 4951 * reserved sector. 4952 * 4953 * Arguments: un - driver soft state (unit) structure 4954 * 4955 * Return Code: EINVAL 4956 * value returned by sd_send_scsi_cmd 4957 * 4958 * Context: Kernel Thread 4959 */ 4960 4961 static int 4962 sd_write_deviceid(struct sd_lun *un) 4963 { 4964 struct dk_devid *dkdevid; 4965 diskaddr_t blk; 4966 uint_t *ip, chksum; 4967 int status; 4968 int i; 4969 4970 ASSERT(mutex_owned(SD_MUTEX(un))); 4971 4972 mutex_exit(SD_MUTEX(un)); 4973 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4974 (void *)SD_PATH_DIRECT) != 0) { 4975 mutex_enter(SD_MUTEX(un)); 4976 return (-1); 4977 } 4978 4979 4980 /* Allocate the buffer */ 4981 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 4982 4983 /* Fill in the revision */ 4984 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 4985 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 4986 4987 /* Copy in the device id */ 4988 mutex_enter(SD_MUTEX(un)); 4989 bcopy(un->un_devid, &dkdevid->dkd_devid, 4990 ddi_devid_sizeof(un->un_devid)); 4991 mutex_exit(SD_MUTEX(un)); 4992 4993 /* Calculate the checksum */ 4994 chksum = 0; 4995 ip = (uint_t *)dkdevid; 4996 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 4997 i++) { 4998 chksum ^= ip[i]; 4999 } 5000 5001 /* Fill-in checksum */ 5002 DKD_FORMCHKSUM(chksum, dkdevid); 5003 5004 /* Write the reserved sector */ 5005 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 5006 SD_PATH_DIRECT); 5007 5008 kmem_free(dkdevid, un->un_sys_blocksize); 5009 5010 mutex_enter(SD_MUTEX(un)); 5011 return (status); 5012 } 5013 5014 5015 /* 5016 * Function: sd_check_vpd_page_support 5017 * 5018 * Description: This routine sends an inquiry command with the EVPD bit set and 5019 * a page code of 0x00 to the device. It is used to determine which 5020 * vital product pages are availible to find the devid. We are 5021 * looking for pages 0x83 or 0x80. If we return a negative 1, the 5022 * device does not support that command. 5023 * 5024 * Arguments: un - driver soft state (unit) structure 5025 * 5026 * Return Code: 0 - success 5027 * 1 - check condition 5028 * 5029 * Context: This routine can sleep. 5030 */ 5031 5032 static int 5033 sd_check_vpd_page_support(struct sd_lun *un) 5034 { 5035 uchar_t *page_list = NULL; 5036 uchar_t page_length = 0xff; /* Use max possible length */ 5037 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5038 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5039 int rval = 0; 5040 int counter; 5041 5042 ASSERT(un != NULL); 5043 ASSERT(mutex_owned(SD_MUTEX(un))); 5044 5045 mutex_exit(SD_MUTEX(un)); 5046 5047 /* 5048 * We'll set the page length to the maximum to save figuring it out 5049 * with an additional call. 5050 */ 5051 page_list = kmem_zalloc(page_length, KM_SLEEP); 5052 5053 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 5054 page_code, NULL); 5055 5056 mutex_enter(SD_MUTEX(un)); 5057 5058 /* 5059 * Now we must validate that the device accepted the command, as some 5060 * drives do not support it. If the drive does support it, we will 5061 * return 0, and the supported pages will be in un_vpd_page_mask. If 5062 * not, we return -1. 5063 */ 5064 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5065 /* Loop to find one of the 2 pages we need */ 5066 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5067 5068 /* 5069 * Pages are returned in ascending order, and 0x83 is what we 5070 * are hoping for. 5071 */ 5072 while ((page_list[counter] <= 0x83) && 5073 (counter <= (page_list[VPD_PAGE_LENGTH] + 5074 VPD_HEAD_OFFSET))) { 5075 /* 5076 * Add 3 because page_list[3] is the number of 5077 * pages minus 3 5078 */ 5079 5080 switch (page_list[counter]) { 5081 case 0x00: 5082 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5083 break; 5084 case 0x80: 5085 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5086 break; 5087 case 0x81: 5088 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5089 break; 5090 case 0x82: 5091 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5092 break; 5093 case 0x83: 5094 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5095 break; 5096 } 5097 counter++; 5098 } 5099 5100 } else { 5101 rval = -1; 5102 5103 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5104 "sd_check_vpd_page_support: This drive does not implement " 5105 "VPD pages.\n"); 5106 } 5107 5108 kmem_free(page_list, page_length); 5109 5110 return (rval); 5111 } 5112 5113 5114 /* 5115 * Function: sd_setup_pm 5116 * 5117 * Description: Initialize Power Management on the device 5118 * 5119 * Context: Kernel Thread 5120 */ 5121 5122 static void 5123 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 5124 { 5125 uint_t log_page_size; 5126 uchar_t *log_page_data; 5127 int rval; 5128 5129 /* 5130 * Since we are called from attach, holding a mutex for 5131 * un is unnecessary. Because some of the routines called 5132 * from here require SD_MUTEX to not be held, assert this 5133 * right up front. 5134 */ 5135 ASSERT(!mutex_owned(SD_MUTEX(un))); 5136 /* 5137 * Since the sd device does not have the 'reg' property, 5138 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5139 * The following code is to tell cpr that this device 5140 * DOES need to be suspended and resumed. 5141 */ 5142 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5143 "pm-hardware-state", "needs-suspend-resume"); 5144 5145 /* 5146 * This complies with the new power management framework 5147 * for certain desktop machines. Create the pm_components 5148 * property as a string array property. 5149 */ 5150 if (un->un_f_pm_supported) { 5151 /* 5152 * not all devices have a motor, try it first. 5153 * some devices may return ILLEGAL REQUEST, some 5154 * will hang 5155 * The following START_STOP_UNIT is used to check if target 5156 * device has a motor. 5157 */ 5158 un->un_f_start_stop_supported = TRUE; 5159 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 5160 SD_PATH_DIRECT) != 0) { 5161 un->un_f_start_stop_supported = FALSE; 5162 } 5163 5164 /* 5165 * create pm properties anyways otherwise the parent can't 5166 * go to sleep 5167 */ 5168 (void) sd_create_pm_components(devi, un); 5169 un->un_f_pm_is_enabled = TRUE; 5170 return; 5171 } 5172 5173 if (!un->un_f_log_sense_supported) { 5174 un->un_power_level = SD_SPINDLE_ON; 5175 un->un_f_pm_is_enabled = FALSE; 5176 return; 5177 } 5178 5179 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 5180 5181 #ifdef SDDEBUG 5182 if (sd_force_pm_supported) { 5183 /* Force a successful result */ 5184 rval = 1; 5185 } 5186 #endif 5187 5188 /* 5189 * If the start-stop cycle counter log page is not supported 5190 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 5191 * then we should not create the pm_components property. 5192 */ 5193 if (rval == -1) { 5194 /* 5195 * Error. 5196 * Reading log sense failed, most likely this is 5197 * an older drive that does not support log sense. 5198 * If this fails auto-pm is not supported. 5199 */ 5200 un->un_power_level = SD_SPINDLE_ON; 5201 un->un_f_pm_is_enabled = FALSE; 5202 5203 } else if (rval == 0) { 5204 /* 5205 * Page not found. 5206 * The start stop cycle counter is implemented as page 5207 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5208 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5209 */ 5210 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 5211 /* 5212 * Page found, use this one. 5213 */ 5214 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5215 un->un_f_pm_is_enabled = TRUE; 5216 } else { 5217 /* 5218 * Error or page not found. 5219 * auto-pm is not supported for this device. 5220 */ 5221 un->un_power_level = SD_SPINDLE_ON; 5222 un->un_f_pm_is_enabled = FALSE; 5223 } 5224 } else { 5225 /* 5226 * Page found, use it. 5227 */ 5228 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 5229 un->un_f_pm_is_enabled = TRUE; 5230 } 5231 5232 5233 if (un->un_f_pm_is_enabled == TRUE) { 5234 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5235 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5236 5237 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5238 log_page_size, un->un_start_stop_cycle_page, 5239 0x01, 0, SD_PATH_DIRECT); 5240 #ifdef SDDEBUG 5241 if (sd_force_pm_supported) { 5242 /* Force a successful result */ 5243 rval = 0; 5244 } 5245 #endif 5246 5247 /* 5248 * If the Log sense for Page( Start/stop cycle counter page) 5249 * succeeds, then power managment is supported and we can 5250 * enable auto-pm. 5251 */ 5252 if (rval == 0) { 5253 (void) sd_create_pm_components(devi, un); 5254 } else { 5255 un->un_power_level = SD_SPINDLE_ON; 5256 un->un_f_pm_is_enabled = FALSE; 5257 } 5258 5259 kmem_free(log_page_data, log_page_size); 5260 } 5261 } 5262 5263 5264 /* 5265 * Function: sd_create_pm_components 5266 * 5267 * Description: Initialize PM property. 5268 * 5269 * Context: Kernel thread context 5270 */ 5271 5272 static void 5273 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 5274 { 5275 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 5276 5277 ASSERT(!mutex_owned(SD_MUTEX(un))); 5278 5279 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 5280 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 5281 /* 5282 * When components are initially created they are idle, 5283 * power up any non-removables. 5284 * Note: the return value of pm_raise_power can't be used 5285 * for determining if PM should be enabled for this device. 5286 * Even if you check the return values and remove this 5287 * property created above, the PM framework will not honor the 5288 * change after the first call to pm_raise_power. Hence, 5289 * removal of that property does not help if pm_raise_power 5290 * fails. In the case of removable media, the start/stop 5291 * will fail if the media is not present. 5292 */ 5293 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 5294 SD_SPINDLE_ON) == DDI_SUCCESS)) { 5295 mutex_enter(SD_MUTEX(un)); 5296 un->un_power_level = SD_SPINDLE_ON; 5297 mutex_enter(&un->un_pm_mutex); 5298 /* Set to on and not busy. */ 5299 un->un_pm_count = 0; 5300 } else { 5301 mutex_enter(SD_MUTEX(un)); 5302 un->un_power_level = SD_SPINDLE_OFF; 5303 mutex_enter(&un->un_pm_mutex); 5304 /* Set to off. */ 5305 un->un_pm_count = -1; 5306 } 5307 mutex_exit(&un->un_pm_mutex); 5308 mutex_exit(SD_MUTEX(un)); 5309 } else { 5310 un->un_power_level = SD_SPINDLE_ON; 5311 un->un_f_pm_is_enabled = FALSE; 5312 } 5313 } 5314 5315 5316 /* 5317 * Function: sd_ddi_suspend 5318 * 5319 * Description: Performs system power-down operations. This includes 5320 * setting the drive state to indicate its suspended so 5321 * that no new commands will be accepted. Also, wait for 5322 * all commands that are in transport or queued to a timer 5323 * for retry to complete. All timeout threads are cancelled. 5324 * 5325 * Return Code: DDI_FAILURE or DDI_SUCCESS 5326 * 5327 * Context: Kernel thread context 5328 */ 5329 5330 static int 5331 sd_ddi_suspend(dev_info_t *devi) 5332 { 5333 struct sd_lun *un; 5334 clock_t wait_cmds_complete; 5335 5336 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5337 if (un == NULL) { 5338 return (DDI_FAILURE); 5339 } 5340 5341 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 5342 5343 mutex_enter(SD_MUTEX(un)); 5344 5345 /* Return success if the device is already suspended. */ 5346 if (un->un_state == SD_STATE_SUSPENDED) { 5347 mutex_exit(SD_MUTEX(un)); 5348 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5349 "device already suspended, exiting\n"); 5350 return (DDI_SUCCESS); 5351 } 5352 5353 /* Return failure if the device is being used by HA */ 5354 if (un->un_resvd_status & 5355 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 5356 mutex_exit(SD_MUTEX(un)); 5357 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5358 "device in use by HA, exiting\n"); 5359 return (DDI_FAILURE); 5360 } 5361 5362 /* 5363 * Return failure if the device is in a resource wait 5364 * or power changing state. 5365 */ 5366 if ((un->un_state == SD_STATE_RWAIT) || 5367 (un->un_state == SD_STATE_PM_CHANGING)) { 5368 mutex_exit(SD_MUTEX(un)); 5369 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5370 "device in resource wait state, exiting\n"); 5371 return (DDI_FAILURE); 5372 } 5373 5374 5375 un->un_save_state = un->un_last_state; 5376 New_state(un, SD_STATE_SUSPENDED); 5377 5378 /* 5379 * Wait for all commands that are in transport or queued to a timer 5380 * for retry to complete. 5381 * 5382 * While waiting, no new commands will be accepted or sent because of 5383 * the new state we set above. 5384 * 5385 * Wait till current operation has completed. If we are in the resource 5386 * wait state (with an intr outstanding) then we need to wait till the 5387 * intr completes and starts the next cmd. We want to wait for 5388 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 5389 */ 5390 wait_cmds_complete = ddi_get_lbolt() + 5391 (sd_wait_cmds_complete * drv_usectohz(1000000)); 5392 5393 while (un->un_ncmds_in_transport != 0) { 5394 /* 5395 * Fail if commands do not finish in the specified time. 5396 */ 5397 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 5398 wait_cmds_complete) == -1) { 5399 /* 5400 * Undo the state changes made above. Everything 5401 * must go back to it's original value. 5402 */ 5403 Restore_state(un); 5404 un->un_last_state = un->un_save_state; 5405 /* Wake up any threads that might be waiting. */ 5406 cv_broadcast(&un->un_suspend_cv); 5407 mutex_exit(SD_MUTEX(un)); 5408 SD_ERROR(SD_LOG_IO_PM, un, 5409 "sd_ddi_suspend: failed due to outstanding cmds\n"); 5410 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 5411 return (DDI_FAILURE); 5412 } 5413 } 5414 5415 /* 5416 * Cancel SCSI watch thread and timeouts, if any are active 5417 */ 5418 5419 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 5420 opaque_t temp_token = un->un_swr_token; 5421 mutex_exit(SD_MUTEX(un)); 5422 scsi_watch_suspend(temp_token); 5423 mutex_enter(SD_MUTEX(un)); 5424 } 5425 5426 if (un->un_reset_throttle_timeid != NULL) { 5427 timeout_id_t temp_id = un->un_reset_throttle_timeid; 5428 un->un_reset_throttle_timeid = NULL; 5429 mutex_exit(SD_MUTEX(un)); 5430 (void) untimeout(temp_id); 5431 mutex_enter(SD_MUTEX(un)); 5432 } 5433 5434 if (un->un_dcvb_timeid != NULL) { 5435 timeout_id_t temp_id = un->un_dcvb_timeid; 5436 un->un_dcvb_timeid = NULL; 5437 mutex_exit(SD_MUTEX(un)); 5438 (void) untimeout(temp_id); 5439 mutex_enter(SD_MUTEX(un)); 5440 } 5441 5442 mutex_enter(&un->un_pm_mutex); 5443 if (un->un_pm_timeid != NULL) { 5444 timeout_id_t temp_id = un->un_pm_timeid; 5445 un->un_pm_timeid = NULL; 5446 mutex_exit(&un->un_pm_mutex); 5447 mutex_exit(SD_MUTEX(un)); 5448 (void) untimeout(temp_id); 5449 mutex_enter(SD_MUTEX(un)); 5450 } else { 5451 mutex_exit(&un->un_pm_mutex); 5452 } 5453 5454 if (un->un_retry_timeid != NULL) { 5455 timeout_id_t temp_id = un->un_retry_timeid; 5456 un->un_retry_timeid = NULL; 5457 mutex_exit(SD_MUTEX(un)); 5458 (void) untimeout(temp_id); 5459 mutex_enter(SD_MUTEX(un)); 5460 } 5461 5462 if (un->un_direct_priority_timeid != NULL) { 5463 timeout_id_t temp_id = un->un_direct_priority_timeid; 5464 un->un_direct_priority_timeid = NULL; 5465 mutex_exit(SD_MUTEX(un)); 5466 (void) untimeout(temp_id); 5467 mutex_enter(SD_MUTEX(un)); 5468 } 5469 5470 if (un->un_f_is_fibre == TRUE) { 5471 /* 5472 * Remove callbacks for insert and remove events 5473 */ 5474 if (un->un_insert_event != NULL) { 5475 mutex_exit(SD_MUTEX(un)); 5476 (void) ddi_remove_event_handler(un->un_insert_cb_id); 5477 mutex_enter(SD_MUTEX(un)); 5478 un->un_insert_event = NULL; 5479 } 5480 5481 if (un->un_remove_event != NULL) { 5482 mutex_exit(SD_MUTEX(un)); 5483 (void) ddi_remove_event_handler(un->un_remove_cb_id); 5484 mutex_enter(SD_MUTEX(un)); 5485 un->un_remove_event = NULL; 5486 } 5487 } 5488 5489 mutex_exit(SD_MUTEX(un)); 5490 5491 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 5492 5493 return (DDI_SUCCESS); 5494 } 5495 5496 5497 /* 5498 * Function: sd_ddi_pm_suspend 5499 * 5500 * Description: Set the drive state to low power. 5501 * Someone else is required to actually change the drive 5502 * power level. 5503 * 5504 * Arguments: un - driver soft state (unit) structure 5505 * 5506 * Return Code: DDI_FAILURE or DDI_SUCCESS 5507 * 5508 * Context: Kernel thread context 5509 */ 5510 5511 static int 5512 sd_ddi_pm_suspend(struct sd_lun *un) 5513 { 5514 ASSERT(un != NULL); 5515 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 5516 5517 ASSERT(!mutex_owned(SD_MUTEX(un))); 5518 mutex_enter(SD_MUTEX(un)); 5519 5520 /* 5521 * Exit if power management is not enabled for this device, or if 5522 * the device is being used by HA. 5523 */ 5524 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 5525 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 5526 mutex_exit(SD_MUTEX(un)); 5527 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 5528 return (DDI_SUCCESS); 5529 } 5530 5531 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 5532 un->un_ncmds_in_driver); 5533 5534 /* 5535 * See if the device is not busy, ie.: 5536 * - we have no commands in the driver for this device 5537 * - not waiting for resources 5538 */ 5539 if ((un->un_ncmds_in_driver == 0) && 5540 (un->un_state != SD_STATE_RWAIT)) { 5541 /* 5542 * The device is not busy, so it is OK to go to low power state. 5543 * Indicate low power, but rely on someone else to actually 5544 * change it. 5545 */ 5546 mutex_enter(&un->un_pm_mutex); 5547 un->un_pm_count = -1; 5548 mutex_exit(&un->un_pm_mutex); 5549 un->un_power_level = SD_SPINDLE_OFF; 5550 } 5551 5552 mutex_exit(SD_MUTEX(un)); 5553 5554 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 5555 5556 return (DDI_SUCCESS); 5557 } 5558 5559 5560 /* 5561 * Function: sd_ddi_resume 5562 * 5563 * Description: Performs system power-up operations.. 5564 * 5565 * Return Code: DDI_SUCCESS 5566 * DDI_FAILURE 5567 * 5568 * Context: Kernel thread context 5569 */ 5570 5571 static int 5572 sd_ddi_resume(dev_info_t *devi) 5573 { 5574 struct sd_lun *un; 5575 5576 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5577 if (un == NULL) { 5578 return (DDI_FAILURE); 5579 } 5580 5581 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 5582 5583 mutex_enter(SD_MUTEX(un)); 5584 Restore_state(un); 5585 5586 /* 5587 * Restore the state which was saved to give the 5588 * the right state in un_last_state 5589 */ 5590 un->un_last_state = un->un_save_state; 5591 /* 5592 * Note: throttle comes back at full. 5593 * Also note: this MUST be done before calling pm_raise_power 5594 * otherwise the system can get hung in biowait. The scenario where 5595 * this'll happen is under cpr suspend. Writing of the system 5596 * state goes through sddump, which writes 0 to un_throttle. If 5597 * writing the system state then fails, example if the partition is 5598 * too small, then cpr attempts a resume. If throttle isn't restored 5599 * from the saved value until after calling pm_raise_power then 5600 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 5601 * in biowait. 5602 */ 5603 un->un_throttle = un->un_saved_throttle; 5604 5605 /* 5606 * The chance of failure is very rare as the only command done in power 5607 * entry point is START command when you transition from 0->1 or 5608 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 5609 * which suspend was done. Ignore the return value as the resume should 5610 * not be failed. In the case of removable media the media need not be 5611 * inserted and hence there is a chance that raise power will fail with 5612 * media not present. 5613 */ 5614 if (un->un_f_attach_spinup) { 5615 mutex_exit(SD_MUTEX(un)); 5616 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 5617 mutex_enter(SD_MUTEX(un)); 5618 } 5619 5620 /* 5621 * Don't broadcast to the suspend cv and therefore possibly 5622 * start I/O until after power has been restored. 5623 */ 5624 cv_broadcast(&un->un_suspend_cv); 5625 cv_broadcast(&un->un_state_cv); 5626 5627 /* restart thread */ 5628 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 5629 scsi_watch_resume(un->un_swr_token); 5630 } 5631 5632 #if (defined(__fibre)) 5633 if (un->un_f_is_fibre == TRUE) { 5634 /* 5635 * Add callbacks for insert and remove events 5636 */ 5637 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 5638 sd_init_event_callbacks(un); 5639 } 5640 } 5641 #endif 5642 5643 /* 5644 * Transport any pending commands to the target. 5645 * 5646 * If this is a low-activity device commands in queue will have to wait 5647 * until new commands come in, which may take awhile. Also, we 5648 * specifically don't check un_ncmds_in_transport because we know that 5649 * there really are no commands in progress after the unit was 5650 * suspended and we could have reached the throttle level, been 5651 * suspended, and have no new commands coming in for awhile. Highly 5652 * unlikely, but so is the low-activity disk scenario. 5653 */ 5654 ddi_xbuf_dispatch(un->un_xbuf_attr); 5655 5656 sd_start_cmds(un, NULL); 5657 mutex_exit(SD_MUTEX(un)); 5658 5659 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 5660 5661 return (DDI_SUCCESS); 5662 } 5663 5664 5665 /* 5666 * Function: sd_ddi_pm_resume 5667 * 5668 * Description: Set the drive state to powered on. 5669 * Someone else is required to actually change the drive 5670 * power level. 5671 * 5672 * Arguments: un - driver soft state (unit) structure 5673 * 5674 * Return Code: DDI_SUCCESS 5675 * 5676 * Context: Kernel thread context 5677 */ 5678 5679 static int 5680 sd_ddi_pm_resume(struct sd_lun *un) 5681 { 5682 ASSERT(un != NULL); 5683 5684 ASSERT(!mutex_owned(SD_MUTEX(un))); 5685 mutex_enter(SD_MUTEX(un)); 5686 un->un_power_level = SD_SPINDLE_ON; 5687 5688 ASSERT(!mutex_owned(&un->un_pm_mutex)); 5689 mutex_enter(&un->un_pm_mutex); 5690 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 5691 un->un_pm_count++; 5692 ASSERT(un->un_pm_count == 0); 5693 /* 5694 * Note: no longer do the cv_broadcast on un_suspend_cv. The 5695 * un_suspend_cv is for a system resume, not a power management 5696 * device resume. (4297749) 5697 * cv_broadcast(&un->un_suspend_cv); 5698 */ 5699 } 5700 mutex_exit(&un->un_pm_mutex); 5701 mutex_exit(SD_MUTEX(un)); 5702 5703 return (DDI_SUCCESS); 5704 } 5705 5706 5707 /* 5708 * Function: sd_pm_idletimeout_handler 5709 * 5710 * Description: A timer routine that's active only while a device is busy. 5711 * The purpose is to extend slightly the pm framework's busy 5712 * view of the device to prevent busy/idle thrashing for 5713 * back-to-back commands. Do this by comparing the current time 5714 * to the time at which the last command completed and when the 5715 * difference is greater than sd_pm_idletime, call 5716 * pm_idle_component. In addition to indicating idle to the pm 5717 * framework, update the chain type to again use the internal pm 5718 * layers of the driver. 5719 * 5720 * Arguments: arg - driver soft state (unit) structure 5721 * 5722 * Context: Executes in a timeout(9F) thread context 5723 */ 5724 5725 static void 5726 sd_pm_idletimeout_handler(void *arg) 5727 { 5728 struct sd_lun *un = arg; 5729 5730 time_t now; 5731 5732 mutex_enter(&sd_detach_mutex); 5733 if (un->un_detach_count != 0) { 5734 /* Abort if the instance is detaching */ 5735 mutex_exit(&sd_detach_mutex); 5736 return; 5737 } 5738 mutex_exit(&sd_detach_mutex); 5739 5740 now = ddi_get_time(); 5741 /* 5742 * Grab both mutexes, in the proper order, since we're accessing 5743 * both PM and softstate variables. 5744 */ 5745 mutex_enter(SD_MUTEX(un)); 5746 mutex_enter(&un->un_pm_mutex); 5747 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 5748 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 5749 /* 5750 * Update the chain types. 5751 * This takes affect on the next new command received. 5752 */ 5753 if (un->un_f_non_devbsize_supported) { 5754 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 5755 } else { 5756 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 5757 } 5758 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 5759 5760 SD_TRACE(SD_LOG_IO_PM, un, 5761 "sd_pm_idletimeout_handler: idling device\n"); 5762 (void) pm_idle_component(SD_DEVINFO(un), 0); 5763 un->un_pm_idle_timeid = NULL; 5764 } else { 5765 un->un_pm_idle_timeid = 5766 timeout(sd_pm_idletimeout_handler, un, 5767 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 5768 } 5769 mutex_exit(&un->un_pm_mutex); 5770 mutex_exit(SD_MUTEX(un)); 5771 } 5772 5773 5774 /* 5775 * Function: sd_pm_timeout_handler 5776 * 5777 * Description: Callback to tell framework we are idle. 5778 * 5779 * Context: timeout(9f) thread context. 5780 */ 5781 5782 static void 5783 sd_pm_timeout_handler(void *arg) 5784 { 5785 struct sd_lun *un = arg; 5786 5787 (void) pm_idle_component(SD_DEVINFO(un), 0); 5788 mutex_enter(&un->un_pm_mutex); 5789 un->un_pm_timeid = NULL; 5790 mutex_exit(&un->un_pm_mutex); 5791 } 5792 5793 5794 /* 5795 * Function: sdpower 5796 * 5797 * Description: PM entry point. 5798 * 5799 * Return Code: DDI_SUCCESS 5800 * DDI_FAILURE 5801 * 5802 * Context: Kernel thread context 5803 */ 5804 5805 static int 5806 sdpower(dev_info_t *devi, int component, int level) 5807 { 5808 struct sd_lun *un; 5809 int instance; 5810 int rval = DDI_SUCCESS; 5811 uint_t i, log_page_size, maxcycles, ncycles; 5812 uchar_t *log_page_data; 5813 int log_sense_page; 5814 int medium_present; 5815 time_t intvlp; 5816 dev_t dev; 5817 struct pm_trans_data sd_pm_tran_data; 5818 uchar_t save_state; 5819 int sval; 5820 uchar_t state_before_pm; 5821 int got_semaphore_here; 5822 5823 instance = ddi_get_instance(devi); 5824 5825 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 5826 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 5827 component != 0) { 5828 return (DDI_FAILURE); 5829 } 5830 5831 dev = sd_make_device(SD_DEVINFO(un)); 5832 5833 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 5834 5835 /* 5836 * Must synchronize power down with close. 5837 * Attempt to decrement/acquire the open/close semaphore, 5838 * but do NOT wait on it. If it's not greater than zero, 5839 * ie. it can't be decremented without waiting, then 5840 * someone else, either open or close, already has it 5841 * and the try returns 0. Use that knowledge here to determine 5842 * if it's OK to change the device power level. 5843 * Also, only increment it on exit if it was decremented, ie. gotten, 5844 * here. 5845 */ 5846 got_semaphore_here = sema_tryp(&un->un_semoclose); 5847 5848 mutex_enter(SD_MUTEX(un)); 5849 5850 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 5851 un->un_ncmds_in_driver); 5852 5853 /* 5854 * If un_ncmds_in_driver is non-zero it indicates commands are 5855 * already being processed in the driver, or if the semaphore was 5856 * not gotten here it indicates an open or close is being processed. 5857 * At the same time somebody is requesting to go low power which 5858 * can't happen, therefore we need to return failure. 5859 */ 5860 if ((level == SD_SPINDLE_OFF) && 5861 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 5862 mutex_exit(SD_MUTEX(un)); 5863 5864 if (got_semaphore_here != 0) { 5865 sema_v(&un->un_semoclose); 5866 } 5867 SD_TRACE(SD_LOG_IO_PM, un, 5868 "sdpower: exit, device has queued cmds.\n"); 5869 return (DDI_FAILURE); 5870 } 5871 5872 /* 5873 * if it is OFFLINE that means the disk is completely dead 5874 * in our case we have to put the disk in on or off by sending commands 5875 * Of course that will fail anyway so return back here. 5876 * 5877 * Power changes to a device that's OFFLINE or SUSPENDED 5878 * are not allowed. 5879 */ 5880 if ((un->un_state == SD_STATE_OFFLINE) || 5881 (un->un_state == SD_STATE_SUSPENDED)) { 5882 mutex_exit(SD_MUTEX(un)); 5883 5884 if (got_semaphore_here != 0) { 5885 sema_v(&un->un_semoclose); 5886 } 5887 SD_TRACE(SD_LOG_IO_PM, un, 5888 "sdpower: exit, device is off-line.\n"); 5889 return (DDI_FAILURE); 5890 } 5891 5892 /* 5893 * Change the device's state to indicate it's power level 5894 * is being changed. Do this to prevent a power off in the 5895 * middle of commands, which is especially bad on devices 5896 * that are really powered off instead of just spun down. 5897 */ 5898 state_before_pm = un->un_state; 5899 un->un_state = SD_STATE_PM_CHANGING; 5900 5901 mutex_exit(SD_MUTEX(un)); 5902 5903 /* 5904 * If "pm-capable" property is set to TRUE by HBA drivers, 5905 * bypass the following checking, otherwise, check the log 5906 * sense information for this device 5907 */ 5908 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 5909 /* 5910 * Get the log sense information to understand whether the 5911 * the powercycle counts have gone beyond the threshhold. 5912 */ 5913 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5914 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5915 5916 mutex_enter(SD_MUTEX(un)); 5917 log_sense_page = un->un_start_stop_cycle_page; 5918 mutex_exit(SD_MUTEX(un)); 5919 5920 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5921 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 5922 #ifdef SDDEBUG 5923 if (sd_force_pm_supported) { 5924 /* Force a successful result */ 5925 rval = 0; 5926 } 5927 #endif 5928 if (rval != 0) { 5929 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5930 "Log Sense Failed\n"); 5931 kmem_free(log_page_data, log_page_size); 5932 /* Cannot support power management on those drives */ 5933 5934 if (got_semaphore_here != 0) { 5935 sema_v(&un->un_semoclose); 5936 } 5937 /* 5938 * On exit put the state back to it's original value 5939 * and broadcast to anyone waiting for the power 5940 * change completion. 5941 */ 5942 mutex_enter(SD_MUTEX(un)); 5943 un->un_state = state_before_pm; 5944 cv_broadcast(&un->un_suspend_cv); 5945 mutex_exit(SD_MUTEX(un)); 5946 SD_TRACE(SD_LOG_IO_PM, un, 5947 "sdpower: exit, Log Sense Failed.\n"); 5948 return (DDI_FAILURE); 5949 } 5950 5951 /* 5952 * From the page data - Convert the essential information to 5953 * pm_trans_data 5954 */ 5955 maxcycles = 5956 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 5957 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 5958 5959 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 5960 5961 ncycles = 5962 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 5963 (log_page_data[0x26] << 8) | log_page_data[0x27]; 5964 5965 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 5966 5967 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 5968 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 5969 log_page_data[8+i]; 5970 } 5971 5972 kmem_free(log_page_data, log_page_size); 5973 5974 /* 5975 * Call pm_trans_check routine to get the Ok from 5976 * the global policy 5977 */ 5978 5979 sd_pm_tran_data.format = DC_SCSI_FORMAT; 5980 sd_pm_tran_data.un.scsi_cycles.flag = 0; 5981 5982 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 5983 #ifdef SDDEBUG 5984 if (sd_force_pm_supported) { 5985 /* Force a successful result */ 5986 rval = 1; 5987 } 5988 #endif 5989 switch (rval) { 5990 case 0: 5991 /* 5992 * Not Ok to Power cycle or error in parameters passed 5993 * Would have given the advised time to consider power 5994 * cycle. Based on the new intvlp parameter we are 5995 * supposed to pretend we are busy so that pm framework 5996 * will never call our power entry point. Because of 5997 * that install a timeout handler and wait for the 5998 * recommended time to elapse so that power management 5999 * can be effective again. 6000 * 6001 * To effect this behavior, call pm_busy_component to 6002 * indicate to the framework this device is busy. 6003 * By not adjusting un_pm_count the rest of PM in 6004 * the driver will function normally, and independant 6005 * of this but because the framework is told the device 6006 * is busy it won't attempt powering down until it gets 6007 * a matching idle. The timeout handler sends this. 6008 * Note: sd_pm_entry can't be called here to do this 6009 * because sdpower may have been called as a result 6010 * of a call to pm_raise_power from within sd_pm_entry. 6011 * 6012 * If a timeout handler is already active then 6013 * don't install another. 6014 */ 6015 mutex_enter(&un->un_pm_mutex); 6016 if (un->un_pm_timeid == NULL) { 6017 un->un_pm_timeid = 6018 timeout(sd_pm_timeout_handler, 6019 un, intvlp * drv_usectohz(1000000)); 6020 mutex_exit(&un->un_pm_mutex); 6021 (void) pm_busy_component(SD_DEVINFO(un), 0); 6022 } else { 6023 mutex_exit(&un->un_pm_mutex); 6024 } 6025 if (got_semaphore_here != 0) { 6026 sema_v(&un->un_semoclose); 6027 } 6028 /* 6029 * On exit put the state back to it's original value 6030 * and broadcast to anyone waiting for the power 6031 * change completion. 6032 */ 6033 mutex_enter(SD_MUTEX(un)); 6034 un->un_state = state_before_pm; 6035 cv_broadcast(&un->un_suspend_cv); 6036 mutex_exit(SD_MUTEX(un)); 6037 6038 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6039 "trans check Failed, not ok to power cycle.\n"); 6040 return (DDI_FAILURE); 6041 6042 case -1: 6043 if (got_semaphore_here != 0) { 6044 sema_v(&un->un_semoclose); 6045 } 6046 /* 6047 * On exit put the state back to it's original value 6048 * and broadcast to anyone waiting for the power 6049 * change completion. 6050 */ 6051 mutex_enter(SD_MUTEX(un)); 6052 un->un_state = state_before_pm; 6053 cv_broadcast(&un->un_suspend_cv); 6054 mutex_exit(SD_MUTEX(un)); 6055 SD_TRACE(SD_LOG_IO_PM, un, 6056 "sdpower: exit, trans check command Failed.\n"); 6057 return (DDI_FAILURE); 6058 } 6059 } 6060 6061 if (level == SD_SPINDLE_OFF) { 6062 /* 6063 * Save the last state... if the STOP FAILS we need it 6064 * for restoring 6065 */ 6066 mutex_enter(SD_MUTEX(un)); 6067 save_state = un->un_last_state; 6068 /* 6069 * There must not be any cmds. getting processed 6070 * in the driver when we get here. Power to the 6071 * device is potentially going off. 6072 */ 6073 ASSERT(un->un_ncmds_in_driver == 0); 6074 mutex_exit(SD_MUTEX(un)); 6075 6076 /* 6077 * For now suspend the device completely before spindle is 6078 * turned off 6079 */ 6080 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 6081 if (got_semaphore_here != 0) { 6082 sema_v(&un->un_semoclose); 6083 } 6084 /* 6085 * On exit put the state back to it's original value 6086 * and broadcast to anyone waiting for the power 6087 * change completion. 6088 */ 6089 mutex_enter(SD_MUTEX(un)); 6090 un->un_state = state_before_pm; 6091 cv_broadcast(&un->un_suspend_cv); 6092 mutex_exit(SD_MUTEX(un)); 6093 SD_TRACE(SD_LOG_IO_PM, un, 6094 "sdpower: exit, PM suspend Failed.\n"); 6095 return (DDI_FAILURE); 6096 } 6097 } 6098 6099 /* 6100 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6101 * close, or strategy. Dump no long uses this routine, it uses it's 6102 * own code so it can be done in polled mode. 6103 */ 6104 6105 medium_present = TRUE; 6106 6107 /* 6108 * When powering up, issue a TUR in case the device is at unit 6109 * attention. Don't do retries. Bypass the PM layer, otherwise 6110 * a deadlock on un_pm_busy_cv will occur. 6111 */ 6112 if (level == SD_SPINDLE_ON) { 6113 (void) sd_send_scsi_TEST_UNIT_READY(un, 6114 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6115 } 6116 6117 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6118 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6119 6120 sval = sd_send_scsi_START_STOP_UNIT(un, 6121 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 6122 SD_PATH_DIRECT); 6123 /* Command failed, check for media present. */ 6124 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6125 medium_present = FALSE; 6126 } 6127 6128 /* 6129 * The conditions of interest here are: 6130 * if a spindle off with media present fails, 6131 * then restore the state and return an error. 6132 * else if a spindle on fails, 6133 * then return an error (there's no state to restore). 6134 * In all other cases we setup for the new state 6135 * and return success. 6136 */ 6137 switch (level) { 6138 case SD_SPINDLE_OFF: 6139 if ((medium_present == TRUE) && (sval != 0)) { 6140 /* The stop command from above failed */ 6141 rval = DDI_FAILURE; 6142 /* 6143 * The stop command failed, and we have media 6144 * present. Put the level back by calling the 6145 * sd_pm_resume() and set the state back to 6146 * it's previous value. 6147 */ 6148 (void) sd_ddi_pm_resume(un); 6149 mutex_enter(SD_MUTEX(un)); 6150 un->un_last_state = save_state; 6151 mutex_exit(SD_MUTEX(un)); 6152 break; 6153 } 6154 /* 6155 * The stop command from above succeeded. 6156 */ 6157 if (un->un_f_monitor_media_state) { 6158 /* 6159 * Terminate watch thread in case of removable media 6160 * devices going into low power state. This is as per 6161 * the requirements of pm framework, otherwise commands 6162 * will be generated for the device (through watch 6163 * thread), even when the device is in low power state. 6164 */ 6165 mutex_enter(SD_MUTEX(un)); 6166 un->un_f_watcht_stopped = FALSE; 6167 if (un->un_swr_token != NULL) { 6168 opaque_t temp_token = un->un_swr_token; 6169 un->un_f_watcht_stopped = TRUE; 6170 un->un_swr_token = NULL; 6171 mutex_exit(SD_MUTEX(un)); 6172 (void) scsi_watch_request_terminate(temp_token, 6173 SCSI_WATCH_TERMINATE_WAIT); 6174 } else { 6175 mutex_exit(SD_MUTEX(un)); 6176 } 6177 } 6178 break; 6179 6180 default: /* The level requested is spindle on... */ 6181 /* 6182 * Legacy behavior: return success on a failed spinup 6183 * if there is no media in the drive. 6184 * Do this by looking at medium_present here. 6185 */ 6186 if ((sval != 0) && medium_present) { 6187 /* The start command from above failed */ 6188 rval = DDI_FAILURE; 6189 break; 6190 } 6191 /* 6192 * The start command from above succeeded 6193 * Resume the devices now that we have 6194 * started the disks 6195 */ 6196 (void) sd_ddi_pm_resume(un); 6197 6198 /* 6199 * Resume the watch thread since it was suspended 6200 * when the device went into low power mode. 6201 */ 6202 if (un->un_f_monitor_media_state) { 6203 mutex_enter(SD_MUTEX(un)); 6204 if (un->un_f_watcht_stopped == TRUE) { 6205 opaque_t temp_token; 6206 6207 un->un_f_watcht_stopped = FALSE; 6208 mutex_exit(SD_MUTEX(un)); 6209 temp_token = scsi_watch_request_submit( 6210 SD_SCSI_DEVP(un), 6211 sd_check_media_time, 6212 SENSE_LENGTH, sd_media_watch_cb, 6213 (caddr_t)dev); 6214 mutex_enter(SD_MUTEX(un)); 6215 un->un_swr_token = temp_token; 6216 } 6217 mutex_exit(SD_MUTEX(un)); 6218 } 6219 } 6220 if (got_semaphore_here != 0) { 6221 sema_v(&un->un_semoclose); 6222 } 6223 /* 6224 * On exit put the state back to it's original value 6225 * and broadcast to anyone waiting for the power 6226 * change completion. 6227 */ 6228 mutex_enter(SD_MUTEX(un)); 6229 un->un_state = state_before_pm; 6230 cv_broadcast(&un->un_suspend_cv); 6231 mutex_exit(SD_MUTEX(un)); 6232 6233 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 6234 6235 return (rval); 6236 } 6237 6238 6239 6240 /* 6241 * Function: sdattach 6242 * 6243 * Description: Driver's attach(9e) entry point function. 6244 * 6245 * Arguments: devi - opaque device info handle 6246 * cmd - attach type 6247 * 6248 * Return Code: DDI_SUCCESS 6249 * DDI_FAILURE 6250 * 6251 * Context: Kernel thread context 6252 */ 6253 6254 static int 6255 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 6256 { 6257 switch (cmd) { 6258 case DDI_ATTACH: 6259 return (sd_unit_attach(devi)); 6260 case DDI_RESUME: 6261 return (sd_ddi_resume(devi)); 6262 default: 6263 break; 6264 } 6265 return (DDI_FAILURE); 6266 } 6267 6268 6269 /* 6270 * Function: sddetach 6271 * 6272 * Description: Driver's detach(9E) entry point function. 6273 * 6274 * Arguments: devi - opaque device info handle 6275 * cmd - detach type 6276 * 6277 * Return Code: DDI_SUCCESS 6278 * DDI_FAILURE 6279 * 6280 * Context: Kernel thread context 6281 */ 6282 6283 static int 6284 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 6285 { 6286 switch (cmd) { 6287 case DDI_DETACH: 6288 return (sd_unit_detach(devi)); 6289 case DDI_SUSPEND: 6290 return (sd_ddi_suspend(devi)); 6291 default: 6292 break; 6293 } 6294 return (DDI_FAILURE); 6295 } 6296 6297 6298 /* 6299 * Function: sd_sync_with_callback 6300 * 6301 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 6302 * state while the callback routine is active. 6303 * 6304 * Arguments: un: softstate structure for the instance 6305 * 6306 * Context: Kernel thread context 6307 */ 6308 6309 static void 6310 sd_sync_with_callback(struct sd_lun *un) 6311 { 6312 ASSERT(un != NULL); 6313 6314 mutex_enter(SD_MUTEX(un)); 6315 6316 ASSERT(un->un_in_callback >= 0); 6317 6318 while (un->un_in_callback > 0) { 6319 mutex_exit(SD_MUTEX(un)); 6320 delay(2); 6321 mutex_enter(SD_MUTEX(un)); 6322 } 6323 6324 mutex_exit(SD_MUTEX(un)); 6325 } 6326 6327 /* 6328 * Function: sd_unit_attach 6329 * 6330 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 6331 * the soft state structure for the device and performs 6332 * all necessary structure and device initializations. 6333 * 6334 * Arguments: devi: the system's dev_info_t for the device. 6335 * 6336 * Return Code: DDI_SUCCESS if attach is successful. 6337 * DDI_FAILURE if any part of the attach fails. 6338 * 6339 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 6340 * Kernel thread context only. Can sleep. 6341 */ 6342 6343 static int 6344 sd_unit_attach(dev_info_t *devi) 6345 { 6346 struct scsi_device *devp; 6347 struct sd_lun *un; 6348 char *variantp; 6349 int reservation_flag = SD_TARGET_IS_UNRESERVED; 6350 int instance; 6351 int rval; 6352 int wc_enabled; 6353 int tgt; 6354 uint64_t capacity; 6355 uint_t lbasize = 0; 6356 dev_info_t *pdip = ddi_get_parent(devi); 6357 int offbyone = 0; 6358 int geom_label_valid = 0; 6359 6360 /* 6361 * Retrieve the target driver's private data area. This was set 6362 * up by the HBA. 6363 */ 6364 devp = ddi_get_driver_private(devi); 6365 6366 /* 6367 * Retrieve the target ID of the device. 6368 */ 6369 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6370 SCSI_ADDR_PROP_TARGET, -1); 6371 6372 /* 6373 * Since we have no idea what state things were left in by the last 6374 * user of the device, set up some 'default' settings, ie. turn 'em 6375 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 6376 * Do this before the scsi_probe, which sends an inquiry. 6377 * This is a fix for bug (4430280). 6378 * Of special importance is wide-xfer. The drive could have been left 6379 * in wide transfer mode by the last driver to communicate with it, 6380 * this includes us. If that's the case, and if the following is not 6381 * setup properly or we don't re-negotiate with the drive prior to 6382 * transferring data to/from the drive, it causes bus parity errors, 6383 * data overruns, and unexpected interrupts. This first occurred when 6384 * the fix for bug (4378686) was made. 6385 */ 6386 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 6387 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 6388 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 6389 6390 /* 6391 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 6392 * on a target. Setting it per lun instance actually sets the 6393 * capability of this target, which affects those luns already 6394 * attached on the same target. So during attach, we can only disable 6395 * this capability only when no other lun has been attached on this 6396 * target. By doing this, we assume a target has the same tagged-qing 6397 * capability for every lun. The condition can be removed when HBA 6398 * is changed to support per lun based tagged-qing capability. 6399 */ 6400 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 6401 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 6402 } 6403 6404 /* 6405 * Use scsi_probe() to issue an INQUIRY command to the device. 6406 * This call will allocate and fill in the scsi_inquiry structure 6407 * and point the sd_inq member of the scsi_device structure to it. 6408 * If the attach succeeds, then this memory will not be de-allocated 6409 * (via scsi_unprobe()) until the instance is detached. 6410 */ 6411 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 6412 goto probe_failed; 6413 } 6414 6415 /* 6416 * Check the device type as specified in the inquiry data and 6417 * claim it if it is of a type that we support. 6418 */ 6419 switch (devp->sd_inq->inq_dtype) { 6420 case DTYPE_DIRECT: 6421 break; 6422 case DTYPE_RODIRECT: 6423 break; 6424 case DTYPE_OPTICAL: 6425 break; 6426 case DTYPE_NOTPRESENT: 6427 default: 6428 /* Unsupported device type; fail the attach. */ 6429 goto probe_failed; 6430 } 6431 6432 /* 6433 * Allocate the soft state structure for this unit. 6434 * 6435 * We rely upon this memory being set to all zeroes by 6436 * ddi_soft_state_zalloc(). We assume that any member of the 6437 * soft state structure that is not explicitly initialized by 6438 * this routine will have a value of zero. 6439 */ 6440 instance = ddi_get_instance(devp->sd_dev); 6441 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 6442 goto probe_failed; 6443 } 6444 6445 /* 6446 * Retrieve a pointer to the newly-allocated soft state. 6447 * 6448 * This should NEVER fail if the ddi_soft_state_zalloc() call above 6449 * was successful, unless something has gone horribly wrong and the 6450 * ddi's soft state internals are corrupt (in which case it is 6451 * probably better to halt here than just fail the attach....) 6452 */ 6453 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 6454 panic("sd_unit_attach: NULL soft state on instance:0x%x", 6455 instance); 6456 /*NOTREACHED*/ 6457 } 6458 6459 /* 6460 * Link the back ptr of the driver soft state to the scsi_device 6461 * struct for this lun. 6462 * Save a pointer to the softstate in the driver-private area of 6463 * the scsi_device struct. 6464 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 6465 * we first set un->un_sd below. 6466 */ 6467 un->un_sd = devp; 6468 devp->sd_private = (opaque_t)un; 6469 6470 /* 6471 * The following must be after devp is stored in the soft state struct. 6472 */ 6473 #ifdef SDDEBUG 6474 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6475 "%s_unit_attach: un:0x%p instance:%d\n", 6476 ddi_driver_name(devi), un, instance); 6477 #endif 6478 6479 /* 6480 * Set up the device type and node type (for the minor nodes). 6481 * By default we assume that the device can at least support the 6482 * Common Command Set. Call it a CD-ROM if it reports itself 6483 * as a RODIRECT device. 6484 */ 6485 switch (devp->sd_inq->inq_dtype) { 6486 case DTYPE_RODIRECT: 6487 un->un_node_type = DDI_NT_CD_CHAN; 6488 un->un_ctype = CTYPE_CDROM; 6489 break; 6490 case DTYPE_OPTICAL: 6491 un->un_node_type = DDI_NT_BLOCK_CHAN; 6492 un->un_ctype = CTYPE_ROD; 6493 break; 6494 default: 6495 un->un_node_type = DDI_NT_BLOCK_CHAN; 6496 un->un_ctype = CTYPE_CCS; 6497 break; 6498 } 6499 6500 /* 6501 * Try to read the interconnect type from the HBA. 6502 * 6503 * Note: This driver is currently compiled as two binaries, a parallel 6504 * scsi version (sd) and a fibre channel version (ssd). All functional 6505 * differences are determined at compile time. In the future a single 6506 * binary will be provided and the inteconnect type will be used to 6507 * differentiate between fibre and parallel scsi behaviors. At that time 6508 * it will be necessary for all fibre channel HBAs to support this 6509 * property. 6510 * 6511 * set un_f_is_fiber to TRUE ( default fiber ) 6512 */ 6513 un->un_f_is_fibre = TRUE; 6514 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 6515 case INTERCONNECT_SSA: 6516 un->un_interconnect_type = SD_INTERCONNECT_SSA; 6517 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6518 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 6519 break; 6520 case INTERCONNECT_PARALLEL: 6521 un->un_f_is_fibre = FALSE; 6522 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6523 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6524 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 6525 break; 6526 case INTERCONNECT_SATA: 6527 un->un_f_is_fibre = FALSE; 6528 un->un_interconnect_type = SD_INTERCONNECT_SATA; 6529 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6530 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 6531 break; 6532 case INTERCONNECT_FIBRE: 6533 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 6534 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6535 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 6536 break; 6537 case INTERCONNECT_FABRIC: 6538 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 6539 un->un_node_type = DDI_NT_BLOCK_FABRIC; 6540 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6541 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 6542 break; 6543 default: 6544 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 6545 /* 6546 * The HBA does not support the "interconnect-type" property 6547 * (or did not provide a recognized type). 6548 * 6549 * Note: This will be obsoleted when a single fibre channel 6550 * and parallel scsi driver is delivered. In the meantime the 6551 * interconnect type will be set to the platform default.If that 6552 * type is not parallel SCSI, it means that we should be 6553 * assuming "ssd" semantics. However, here this also means that 6554 * the FC HBA is not supporting the "interconnect-type" property 6555 * like we expect it to, so log this occurrence. 6556 */ 6557 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 6558 if (!SD_IS_PARALLEL_SCSI(un)) { 6559 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6560 "sd_unit_attach: un:0x%p Assuming " 6561 "INTERCONNECT_FIBRE\n", un); 6562 } else { 6563 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6564 "sd_unit_attach: un:0x%p Assuming " 6565 "INTERCONNECT_PARALLEL\n", un); 6566 un->un_f_is_fibre = FALSE; 6567 } 6568 #else 6569 /* 6570 * Note: This source will be implemented when a single fibre 6571 * channel and parallel scsi driver is delivered. The default 6572 * will be to assume that if a device does not support the 6573 * "interconnect-type" property it is a parallel SCSI HBA and 6574 * we will set the interconnect type for parallel scsi. 6575 */ 6576 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6577 un->un_f_is_fibre = FALSE; 6578 #endif 6579 break; 6580 } 6581 6582 if (un->un_f_is_fibre == TRUE) { 6583 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 6584 SCSI_VERSION_3) { 6585 switch (un->un_interconnect_type) { 6586 case SD_INTERCONNECT_FIBRE: 6587 case SD_INTERCONNECT_SSA: 6588 un->un_node_type = DDI_NT_BLOCK_WWN; 6589 break; 6590 default: 6591 break; 6592 } 6593 } 6594 } 6595 6596 /* 6597 * Initialize the Request Sense command for the target 6598 */ 6599 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 6600 goto alloc_rqs_failed; 6601 } 6602 6603 /* 6604 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 6605 * with separate binary for sd and ssd. 6606 * 6607 * x86 has 1 binary, un_retry_count is set base on connection type. 6608 * The hardcoded values will go away when Sparc uses 1 binary 6609 * for sd and ssd. This hardcoded values need to match 6610 * SD_RETRY_COUNT in sddef.h 6611 * The value used is base on interconnect type. 6612 * fibre = 3, parallel = 5 6613 */ 6614 #if defined(__i386) || defined(__amd64) 6615 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 6616 #else 6617 un->un_retry_count = SD_RETRY_COUNT; 6618 #endif 6619 6620 /* 6621 * Set the per disk retry count to the default number of retries 6622 * for disks and CDROMs. This value can be overridden by the 6623 * disk property list or an entry in sd.conf. 6624 */ 6625 un->un_notready_retry_count = 6626 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 6627 : DISK_NOT_READY_RETRY_COUNT(un); 6628 6629 /* 6630 * Set the busy retry count to the default value of un_retry_count. 6631 * This can be overridden by entries in sd.conf or the device 6632 * config table. 6633 */ 6634 un->un_busy_retry_count = un->un_retry_count; 6635 6636 /* 6637 * Init the reset threshold for retries. This number determines 6638 * how many retries must be performed before a reset can be issued 6639 * (for certain error conditions). This can be overridden by entries 6640 * in sd.conf or the device config table. 6641 */ 6642 un->un_reset_retry_count = (un->un_retry_count / 2); 6643 6644 /* 6645 * Set the victim_retry_count to the default un_retry_count 6646 */ 6647 un->un_victim_retry_count = (2 * un->un_retry_count); 6648 6649 /* 6650 * Set the reservation release timeout to the default value of 6651 * 5 seconds. This can be overridden by entries in ssd.conf or the 6652 * device config table. 6653 */ 6654 un->un_reserve_release_time = 5; 6655 6656 /* 6657 * Set up the default maximum transfer size. Note that this may 6658 * get updated later in the attach, when setting up default wide 6659 * operations for disks. 6660 */ 6661 #if defined(__i386) || defined(__amd64) 6662 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 6663 #else 6664 un->un_max_xfer_size = (uint_t)maxphys; 6665 #endif 6666 6667 /* 6668 * Get "allow bus device reset" property (defaults to "enabled" if 6669 * the property was not defined). This is to disable bus resets for 6670 * certain kinds of error recovery. Note: In the future when a run-time 6671 * fibre check is available the soft state flag should default to 6672 * enabled. 6673 */ 6674 if (un->un_f_is_fibre == TRUE) { 6675 un->un_f_allow_bus_device_reset = TRUE; 6676 } else { 6677 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6678 "allow-bus-device-reset", 1) != 0) { 6679 un->un_f_allow_bus_device_reset = TRUE; 6680 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6681 "sd_unit_attach: un:0x%p Bus device reset " 6682 "enabled\n", un); 6683 } else { 6684 un->un_f_allow_bus_device_reset = FALSE; 6685 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6686 "sd_unit_attach: un:0x%p Bus device reset " 6687 "disabled\n", un); 6688 } 6689 } 6690 6691 /* 6692 * Check if this is an ATAPI device. ATAPI devices use Group 1 6693 * Read/Write commands and Group 2 Mode Sense/Select commands. 6694 * 6695 * Note: The "obsolete" way of doing this is to check for the "atapi" 6696 * property. The new "variant" property with a value of "atapi" has been 6697 * introduced so that future 'variants' of standard SCSI behavior (like 6698 * atapi) could be specified by the underlying HBA drivers by supplying 6699 * a new value for the "variant" property, instead of having to define a 6700 * new property. 6701 */ 6702 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 6703 un->un_f_cfg_is_atapi = TRUE; 6704 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6705 "sd_unit_attach: un:0x%p Atapi device\n", un); 6706 } 6707 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 6708 &variantp) == DDI_PROP_SUCCESS) { 6709 if (strcmp(variantp, "atapi") == 0) { 6710 un->un_f_cfg_is_atapi = TRUE; 6711 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6712 "sd_unit_attach: un:0x%p Atapi device\n", un); 6713 } 6714 ddi_prop_free(variantp); 6715 } 6716 6717 un->un_cmd_timeout = SD_IO_TIME; 6718 6719 /* Info on current states, statuses, etc. (Updated frequently) */ 6720 un->un_state = SD_STATE_NORMAL; 6721 un->un_last_state = SD_STATE_NORMAL; 6722 6723 /* Control & status info for command throttling */ 6724 un->un_throttle = sd_max_throttle; 6725 un->un_saved_throttle = sd_max_throttle; 6726 un->un_min_throttle = sd_min_throttle; 6727 6728 if (un->un_f_is_fibre == TRUE) { 6729 un->un_f_use_adaptive_throttle = TRUE; 6730 } else { 6731 un->un_f_use_adaptive_throttle = FALSE; 6732 } 6733 6734 /* Removable media support. */ 6735 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 6736 un->un_mediastate = DKIO_NONE; 6737 un->un_specified_mediastate = DKIO_NONE; 6738 6739 /* CVs for suspend/resume (PM or DR) */ 6740 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 6741 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 6742 6743 /* Power management support. */ 6744 un->un_power_level = SD_SPINDLE_UNINIT; 6745 6746 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 6747 un->un_f_wcc_inprog = 0; 6748 6749 /* 6750 * The open/close semaphore is used to serialize threads executing 6751 * in the driver's open & close entry point routines for a given 6752 * instance. 6753 */ 6754 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 6755 6756 /* 6757 * The conf file entry and softstate variable is a forceful override, 6758 * meaning a non-zero value must be entered to change the default. 6759 */ 6760 un->un_f_disksort_disabled = FALSE; 6761 6762 /* 6763 * Retrieve the properties from the static driver table or the driver 6764 * configuration file (.conf) for this unit and update the soft state 6765 * for the device as needed for the indicated properties. 6766 * Note: the property configuration needs to occur here as some of the 6767 * following routines may have dependancies on soft state flags set 6768 * as part of the driver property configuration. 6769 */ 6770 sd_read_unit_properties(un); 6771 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6772 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 6773 6774 /* 6775 * Only if a device has "hotpluggable" property, it is 6776 * treated as hotpluggable device. Otherwise, it is 6777 * regarded as non-hotpluggable one. 6778 */ 6779 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 6780 -1) != -1) { 6781 un->un_f_is_hotpluggable = TRUE; 6782 } 6783 6784 /* 6785 * set unit's attributes(flags) according to "hotpluggable" and 6786 * RMB bit in INQUIRY data. 6787 */ 6788 sd_set_unit_attributes(un, devi); 6789 6790 /* 6791 * By default, we mark the capacity, lbasize, and geometry 6792 * as invalid. Only if we successfully read a valid capacity 6793 * will we update the un_blockcount and un_tgt_blocksize with the 6794 * valid values (the geometry will be validated later). 6795 */ 6796 un->un_f_blockcount_is_valid = FALSE; 6797 un->un_f_tgt_blocksize_is_valid = FALSE; 6798 6799 /* 6800 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 6801 * otherwise. 6802 */ 6803 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 6804 un->un_blockcount = 0; 6805 6806 /* 6807 * Set up the per-instance info needed to determine the correct 6808 * CDBs and other info for issuing commands to the target. 6809 */ 6810 sd_init_cdb_limits(un); 6811 6812 /* 6813 * Set up the IO chains to use, based upon the target type. 6814 */ 6815 if (un->un_f_non_devbsize_supported) { 6816 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6817 } else { 6818 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6819 } 6820 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6821 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 6822 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 6823 6824 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 6825 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 6826 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 6827 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 6828 6829 6830 if (ISCD(un)) { 6831 un->un_additional_codes = sd_additional_codes; 6832 } else { 6833 un->un_additional_codes = NULL; 6834 } 6835 6836 /* 6837 * Create the kstats here so they can be available for attach-time 6838 * routines that send commands to the unit (either polled or via 6839 * sd_send_scsi_cmd). 6840 * 6841 * Note: This is a critical sequence that needs to be maintained: 6842 * 1) Instantiate the kstats here, before any routines using the 6843 * iopath (i.e. sd_send_scsi_cmd). 6844 * 2) Instantiate and initialize the partition stats 6845 * (sd_set_pstats). 6846 * 3) Initialize the error stats (sd_set_errstats), following 6847 * sd_validate_geometry(),sd_register_devid(), 6848 * and sd_cache_control(). 6849 */ 6850 6851 un->un_stats = kstat_create(sd_label, instance, 6852 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 6853 if (un->un_stats != NULL) { 6854 un->un_stats->ks_lock = SD_MUTEX(un); 6855 kstat_install(un->un_stats); 6856 } 6857 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6858 "sd_unit_attach: un:0x%p un_stats created\n", un); 6859 6860 sd_create_errstats(un, instance); 6861 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6862 "sd_unit_attach: un:0x%p errstats created\n", un); 6863 6864 /* 6865 * The following if/else code was relocated here from below as part 6866 * of the fix for bug (4430280). However with the default setup added 6867 * on entry to this routine, it's no longer absolutely necessary for 6868 * this to be before the call to sd_spin_up_unit. 6869 */ 6870 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 6871 /* 6872 * If SCSI-2 tagged queueing is supported by the target 6873 * and by the host adapter then we will enable it. 6874 */ 6875 un->un_tagflags = 0; 6876 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 6877 (devp->sd_inq->inq_cmdque) && 6878 (un->un_f_arq_enabled == TRUE)) { 6879 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 6880 1, 1) == 1) { 6881 un->un_tagflags = FLAG_STAG; 6882 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6883 "sd_unit_attach: un:0x%p tag queueing " 6884 "enabled\n", un); 6885 } else if (scsi_ifgetcap(SD_ADDRESS(un), 6886 "untagged-qing", 0) == 1) { 6887 un->un_f_opt_queueing = TRUE; 6888 un->un_saved_throttle = un->un_throttle = 6889 min(un->un_throttle, 3); 6890 } else { 6891 un->un_f_opt_queueing = FALSE; 6892 un->un_saved_throttle = un->un_throttle = 1; 6893 } 6894 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 6895 == 1) && (un->un_f_arq_enabled == TRUE)) { 6896 /* The Host Adapter supports internal queueing. */ 6897 un->un_f_opt_queueing = TRUE; 6898 un->un_saved_throttle = un->un_throttle = 6899 min(un->un_throttle, 3); 6900 } else { 6901 un->un_f_opt_queueing = FALSE; 6902 un->un_saved_throttle = un->un_throttle = 1; 6903 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6904 "sd_unit_attach: un:0x%p no tag queueing\n", un); 6905 } 6906 6907 /* 6908 * Enable large transfers for SATA/SAS drives 6909 */ 6910 if (SD_IS_SERIAL(un)) { 6911 un->un_max_xfer_size = 6912 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6913 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6914 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6915 "sd_unit_attach: un:0x%p max transfer " 6916 "size=0x%x\n", un, un->un_max_xfer_size); 6917 6918 } 6919 6920 /* Setup or tear down default wide operations for disks */ 6921 6922 /* 6923 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 6924 * and "ssd_max_xfer_size" to exist simultaneously on the same 6925 * system and be set to different values. In the future this 6926 * code may need to be updated when the ssd module is 6927 * obsoleted and removed from the system. (4299588) 6928 */ 6929 if (SD_IS_PARALLEL_SCSI(un) && 6930 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 6931 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 6932 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6933 1, 1) == 1) { 6934 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6935 "sd_unit_attach: un:0x%p Wide Transfer " 6936 "enabled\n", un); 6937 } 6938 6939 /* 6940 * If tagged queuing has also been enabled, then 6941 * enable large xfers 6942 */ 6943 if (un->un_saved_throttle == sd_max_throttle) { 6944 un->un_max_xfer_size = 6945 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6946 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6947 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6948 "sd_unit_attach: un:0x%p max transfer " 6949 "size=0x%x\n", un, un->un_max_xfer_size); 6950 } 6951 } else { 6952 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6953 0, 1) == 1) { 6954 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6955 "sd_unit_attach: un:0x%p " 6956 "Wide Transfer disabled\n", un); 6957 } 6958 } 6959 } else { 6960 un->un_tagflags = FLAG_STAG; 6961 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 6962 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 6963 } 6964 6965 /* 6966 * If this target supports LUN reset, try to enable it. 6967 */ 6968 if (un->un_f_lun_reset_enabled) { 6969 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 6970 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6971 "un:0x%p lun_reset capability set\n", un); 6972 } else { 6973 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6974 "un:0x%p lun-reset capability not set\n", un); 6975 } 6976 } 6977 6978 /* 6979 * At this point in the attach, we have enough info in the 6980 * soft state to be able to issue commands to the target. 6981 * 6982 * All command paths used below MUST issue their commands as 6983 * SD_PATH_DIRECT. This is important as intermediate layers 6984 * are not all initialized yet (such as PM). 6985 */ 6986 6987 /* 6988 * Send a TEST UNIT READY command to the device. This should clear 6989 * any outstanding UNIT ATTENTION that may be present. 6990 * 6991 * Note: Don't check for success, just track if there is a reservation, 6992 * this is a throw away command to clear any unit attentions. 6993 * 6994 * Note: This MUST be the first command issued to the target during 6995 * attach to ensure power on UNIT ATTENTIONS are cleared. 6996 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 6997 * with attempts at spinning up a device with no media. 6998 */ 6999 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 7000 reservation_flag = SD_TARGET_IS_RESERVED; 7001 } 7002 7003 /* 7004 * If the device is NOT a removable media device, attempt to spin 7005 * it up (using the START_STOP_UNIT command) and read its capacity 7006 * (using the READ CAPACITY command). Note, however, that either 7007 * of these could fail and in some cases we would continue with 7008 * the attach despite the failure (see below). 7009 */ 7010 if (un->un_f_descr_format_supported) { 7011 switch (sd_spin_up_unit(un)) { 7012 case 0: 7013 /* 7014 * Spin-up was successful; now try to read the 7015 * capacity. If successful then save the results 7016 * and mark the capacity & lbasize as valid. 7017 */ 7018 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7019 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7020 7021 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 7022 &lbasize, SD_PATH_DIRECT)) { 7023 case 0: { 7024 if (capacity > DK_MAX_BLOCKS) { 7025 #ifdef _LP64 7026 if (capacity + 1 > 7027 SD_GROUP1_MAX_ADDRESS) { 7028 /* 7029 * Enable descriptor format 7030 * sense data so that we can 7031 * get 64 bit sense data 7032 * fields. 7033 */ 7034 sd_enable_descr_sense(un); 7035 } 7036 #else 7037 /* 32-bit kernels can't handle this */ 7038 scsi_log(SD_DEVINFO(un), 7039 sd_label, CE_WARN, 7040 "disk has %llu blocks, which " 7041 "is too large for a 32-bit " 7042 "kernel", capacity); 7043 7044 #if defined(__i386) || defined(__amd64) 7045 /* 7046 * 1TB disk was treated as (1T - 512)B 7047 * in the past, so that it might have 7048 * valid VTOC and solaris partitions, 7049 * we have to allow it to continue to 7050 * work. 7051 */ 7052 if (capacity -1 > DK_MAX_BLOCKS) 7053 #endif 7054 goto spinup_failed; 7055 #endif 7056 } 7057 7058 /* 7059 * Here it's not necessary to check the case: 7060 * the capacity of the device is bigger than 7061 * what the max hba cdb can support. Because 7062 * sd_send_scsi_READ_CAPACITY will retrieve 7063 * the capacity by sending USCSI command, which 7064 * is constrained by the max hba cdb. Actually, 7065 * sd_send_scsi_READ_CAPACITY will return 7066 * EINVAL when using bigger cdb than required 7067 * cdb length. Will handle this case in 7068 * "case EINVAL". 7069 */ 7070 7071 /* 7072 * The following relies on 7073 * sd_send_scsi_READ_CAPACITY never 7074 * returning 0 for capacity and/or lbasize. 7075 */ 7076 sd_update_block_info(un, lbasize, capacity); 7077 7078 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7079 "sd_unit_attach: un:0x%p capacity = %ld " 7080 "blocks; lbasize= %ld.\n", un, 7081 un->un_blockcount, un->un_tgt_blocksize); 7082 7083 break; 7084 } 7085 case EINVAL: 7086 /* 7087 * In the case where the max-cdb-length property 7088 * is smaller than the required CDB length for 7089 * a SCSI device, a target driver can fail to 7090 * attach to that device. 7091 */ 7092 scsi_log(SD_DEVINFO(un), 7093 sd_label, CE_WARN, 7094 "disk capacity is too large " 7095 "for current cdb length"); 7096 goto spinup_failed; 7097 case EACCES: 7098 /* 7099 * Should never get here if the spin-up 7100 * succeeded, but code it in anyway. 7101 * From here, just continue with the attach... 7102 */ 7103 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7104 "sd_unit_attach: un:0x%p " 7105 "sd_send_scsi_READ_CAPACITY " 7106 "returned reservation conflict\n", un); 7107 reservation_flag = SD_TARGET_IS_RESERVED; 7108 break; 7109 default: 7110 /* 7111 * Likewise, should never get here if the 7112 * spin-up succeeded. Just continue with 7113 * the attach... 7114 */ 7115 break; 7116 } 7117 break; 7118 case EACCES: 7119 /* 7120 * Device is reserved by another host. In this case 7121 * we could not spin it up or read the capacity, but 7122 * we continue with the attach anyway. 7123 */ 7124 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7125 "sd_unit_attach: un:0x%p spin-up reservation " 7126 "conflict.\n", un); 7127 reservation_flag = SD_TARGET_IS_RESERVED; 7128 break; 7129 default: 7130 /* Fail the attach if the spin-up failed. */ 7131 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7132 "sd_unit_attach: un:0x%p spin-up failed.", un); 7133 goto spinup_failed; 7134 } 7135 } 7136 7137 /* 7138 * Check to see if this is a MMC drive 7139 */ 7140 if (ISCD(un)) { 7141 sd_set_mmc_caps(un); 7142 } 7143 7144 7145 /* 7146 * Add a zero-length attribute to tell the world we support 7147 * kernel ioctls (for layered drivers) 7148 */ 7149 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7150 DDI_KERNEL_IOCTL, NULL, 0); 7151 7152 /* 7153 * Add a boolean property to tell the world we support 7154 * the B_FAILFAST flag (for layered drivers) 7155 */ 7156 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7157 "ddi-failfast-supported", NULL, 0); 7158 7159 /* 7160 * Initialize power management 7161 */ 7162 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 7163 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 7164 sd_setup_pm(un, devi); 7165 if (un->un_f_pm_is_enabled == FALSE) { 7166 /* 7167 * For performance, point to a jump table that does 7168 * not include pm. 7169 * The direct and priority chains don't change with PM. 7170 * 7171 * Note: this is currently done based on individual device 7172 * capabilities. When an interface for determining system 7173 * power enabled state becomes available, or when additional 7174 * layers are added to the command chain, these values will 7175 * have to be re-evaluated for correctness. 7176 */ 7177 if (un->un_f_non_devbsize_supported) { 7178 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 7179 } else { 7180 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 7181 } 7182 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 7183 } 7184 7185 /* 7186 * This property is set to 0 by HA software to avoid retries 7187 * on a reserved disk. (The preferred property name is 7188 * "retry-on-reservation-conflict") (1189689) 7189 * 7190 * Note: The use of a global here can have unintended consequences. A 7191 * per instance variable is preferrable to match the capabilities of 7192 * different underlying hba's (4402600) 7193 */ 7194 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 7195 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 7196 sd_retry_on_reservation_conflict); 7197 if (sd_retry_on_reservation_conflict != 0) { 7198 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 7199 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 7200 sd_retry_on_reservation_conflict); 7201 } 7202 7203 /* Set up options for QFULL handling. */ 7204 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7205 "qfull-retries", -1)) != -1) { 7206 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 7207 rval, 1); 7208 } 7209 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7210 "qfull-retry-interval", -1)) != -1) { 7211 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 7212 rval, 1); 7213 } 7214 7215 /* 7216 * This just prints a message that announces the existence of the 7217 * device. The message is always printed in the system logfile, but 7218 * only appears on the console if the system is booted with the 7219 * -v (verbose) argument. 7220 */ 7221 ddi_report_dev(devi); 7222 7223 un->un_mediastate = DKIO_NONE; 7224 7225 cmlb_alloc_handle(&un->un_cmlbhandle); 7226 7227 #if defined(__i386) || defined(__amd64) 7228 /* 7229 * On x86, compensate for off-by-1 legacy error 7230 */ 7231 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 7232 (lbasize == un->un_sys_blocksize)) 7233 offbyone = CMLB_OFF_BY_ONE; 7234 #endif 7235 7236 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 7237 un->un_f_has_removable_media, un->un_f_is_hotpluggable, 7238 un->un_node_type, offbyone, un->un_cmlbhandle, 7239 (void *)SD_PATH_DIRECT) != 0) { 7240 goto cmlb_attach_failed; 7241 } 7242 7243 7244 /* 7245 * Read and validate the device's geometry (ie, disk label) 7246 * A new unformatted drive will not have a valid geometry, but 7247 * the driver needs to successfully attach to this device so 7248 * the drive can be formatted via ioctls. 7249 */ 7250 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 7251 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 7252 7253 mutex_enter(SD_MUTEX(un)); 7254 7255 /* 7256 * Read and initialize the devid for the unit. 7257 */ 7258 ASSERT(un->un_errstats != NULL); 7259 if (un->un_f_devid_supported) { 7260 sd_register_devid(un, devi, reservation_flag); 7261 } 7262 mutex_exit(SD_MUTEX(un)); 7263 7264 #if (defined(__fibre)) 7265 /* 7266 * Register callbacks for fibre only. You can't do this soley 7267 * on the basis of the devid_type because this is hba specific. 7268 * We need to query our hba capabilities to find out whether to 7269 * register or not. 7270 */ 7271 if (un->un_f_is_fibre) { 7272 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7273 sd_init_event_callbacks(un); 7274 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7275 "sd_unit_attach: un:0x%p event callbacks inserted", 7276 un); 7277 } 7278 } 7279 #endif 7280 7281 if (un->un_f_opt_disable_cache == TRUE) { 7282 /* 7283 * Disable both read cache and write cache. This is 7284 * the historic behavior of the keywords in the config file. 7285 */ 7286 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 7287 0) { 7288 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7289 "sd_unit_attach: un:0x%p Could not disable " 7290 "caching", un); 7291 goto devid_failed; 7292 } 7293 } 7294 7295 /* 7296 * Check the value of the WCE bit now and 7297 * set un_f_write_cache_enabled accordingly. 7298 */ 7299 (void) sd_get_write_cache_enabled(un, &wc_enabled); 7300 mutex_enter(SD_MUTEX(un)); 7301 un->un_f_write_cache_enabled = (wc_enabled != 0); 7302 mutex_exit(SD_MUTEX(un)); 7303 7304 /* 7305 * Find out what type of reservation this disk supports. 7306 */ 7307 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 7308 case 0: 7309 /* 7310 * SCSI-3 reservations are supported. 7311 */ 7312 un->un_reservation_type = SD_SCSI3_RESERVATION; 7313 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7314 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 7315 break; 7316 case ENOTSUP: 7317 /* 7318 * The PERSISTENT RESERVE IN command would not be recognized by 7319 * a SCSI-2 device, so assume the reservation type is SCSI-2. 7320 */ 7321 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7322 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 7323 un->un_reservation_type = SD_SCSI2_RESERVATION; 7324 break; 7325 default: 7326 /* 7327 * default to SCSI-3 reservations 7328 */ 7329 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7330 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 7331 un->un_reservation_type = SD_SCSI3_RESERVATION; 7332 break; 7333 } 7334 7335 /* 7336 * Set the pstat and error stat values here, so data obtained during the 7337 * previous attach-time routines is available. 7338 * 7339 * Note: This is a critical sequence that needs to be maintained: 7340 * 1) Instantiate the kstats before any routines using the iopath 7341 * (i.e. sd_send_scsi_cmd). 7342 * 2) Initialize the error stats (sd_set_errstats) and partition 7343 * stats (sd_set_pstats)here, following 7344 * cmlb_validate_geometry(), sd_register_devid(), and 7345 * sd_cache_control(). 7346 */ 7347 7348 if (un->un_f_pkstats_enabled && geom_label_valid) { 7349 sd_set_pstats(un); 7350 SD_TRACE(SD_LOG_IO_PARTITION, un, 7351 "sd_unit_attach: un:0x%p pstats created and set\n", un); 7352 } 7353 7354 sd_set_errstats(un); 7355 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7356 "sd_unit_attach: un:0x%p errstats set\n", un); 7357 7358 7359 /* 7360 * After successfully attaching an instance, we record the information 7361 * of how many luns have been attached on the relative target and 7362 * controller for parallel SCSI. This information is used when sd tries 7363 * to set the tagged queuing capability in HBA. 7364 */ 7365 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7366 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 7367 } 7368 7369 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7370 "sd_unit_attach: un:0x%p exit success\n", un); 7371 7372 return (DDI_SUCCESS); 7373 7374 /* 7375 * An error occurred during the attach; clean up & return failure. 7376 */ 7377 7378 devid_failed: 7379 7380 setup_pm_failed: 7381 ddi_remove_minor_node(devi, NULL); 7382 7383 cmlb_attach_failed: 7384 /* 7385 * Cleanup from the scsi_ifsetcap() calls (437868) 7386 */ 7387 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7388 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7389 7390 /* 7391 * Refer to the comments of setting tagged-qing in the beginning of 7392 * sd_unit_attach. We can only disable tagged queuing when there is 7393 * no lun attached on the target. 7394 */ 7395 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7396 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7397 } 7398 7399 if (un->un_f_is_fibre == FALSE) { 7400 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7401 } 7402 7403 spinup_failed: 7404 7405 mutex_enter(SD_MUTEX(un)); 7406 7407 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 7408 if (un->un_direct_priority_timeid != NULL) { 7409 timeout_id_t temp_id = un->un_direct_priority_timeid; 7410 un->un_direct_priority_timeid = NULL; 7411 mutex_exit(SD_MUTEX(un)); 7412 (void) untimeout(temp_id); 7413 mutex_enter(SD_MUTEX(un)); 7414 } 7415 7416 /* Cancel any pending start/stop timeouts */ 7417 if (un->un_startstop_timeid != NULL) { 7418 timeout_id_t temp_id = un->un_startstop_timeid; 7419 un->un_startstop_timeid = NULL; 7420 mutex_exit(SD_MUTEX(un)); 7421 (void) untimeout(temp_id); 7422 mutex_enter(SD_MUTEX(un)); 7423 } 7424 7425 /* Cancel any pending reset-throttle timeouts */ 7426 if (un->un_reset_throttle_timeid != NULL) { 7427 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7428 un->un_reset_throttle_timeid = NULL; 7429 mutex_exit(SD_MUTEX(un)); 7430 (void) untimeout(temp_id); 7431 mutex_enter(SD_MUTEX(un)); 7432 } 7433 7434 /* Cancel any pending retry timeouts */ 7435 if (un->un_retry_timeid != NULL) { 7436 timeout_id_t temp_id = un->un_retry_timeid; 7437 un->un_retry_timeid = NULL; 7438 mutex_exit(SD_MUTEX(un)); 7439 (void) untimeout(temp_id); 7440 mutex_enter(SD_MUTEX(un)); 7441 } 7442 7443 /* Cancel any pending delayed cv broadcast timeouts */ 7444 if (un->un_dcvb_timeid != NULL) { 7445 timeout_id_t temp_id = un->un_dcvb_timeid; 7446 un->un_dcvb_timeid = NULL; 7447 mutex_exit(SD_MUTEX(un)); 7448 (void) untimeout(temp_id); 7449 mutex_enter(SD_MUTEX(un)); 7450 } 7451 7452 mutex_exit(SD_MUTEX(un)); 7453 7454 /* There should not be any in-progress I/O so ASSERT this check */ 7455 ASSERT(un->un_ncmds_in_transport == 0); 7456 ASSERT(un->un_ncmds_in_driver == 0); 7457 7458 /* Do not free the softstate if the callback routine is active */ 7459 sd_sync_with_callback(un); 7460 7461 /* 7462 * Partition stats apparently are not used with removables. These would 7463 * not have been created during attach, so no need to clean them up... 7464 */ 7465 if (un->un_stats != NULL) { 7466 kstat_delete(un->un_stats); 7467 un->un_stats = NULL; 7468 } 7469 if (un->un_errstats != NULL) { 7470 kstat_delete(un->un_errstats); 7471 un->un_errstats = NULL; 7472 } 7473 7474 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7475 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7476 7477 ddi_prop_remove_all(devi); 7478 sema_destroy(&un->un_semoclose); 7479 cv_destroy(&un->un_state_cv); 7480 7481 getrbuf_failed: 7482 7483 sd_free_rqs(un); 7484 7485 alloc_rqs_failed: 7486 7487 devp->sd_private = NULL; 7488 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 7489 7490 get_softstate_failed: 7491 /* 7492 * Note: the man pages are unclear as to whether or not doing a 7493 * ddi_soft_state_free(sd_state, instance) is the right way to 7494 * clean up after the ddi_soft_state_zalloc() if the subsequent 7495 * ddi_get_soft_state() fails. The implication seems to be 7496 * that the get_soft_state cannot fail if the zalloc succeeds. 7497 */ 7498 ddi_soft_state_free(sd_state, instance); 7499 7500 probe_failed: 7501 scsi_unprobe(devp); 7502 #ifdef SDDEBUG 7503 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 7504 (sd_level_mask & SD_LOGMASK_TRACE)) { 7505 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 7506 (void *)un); 7507 } 7508 #endif 7509 return (DDI_FAILURE); 7510 } 7511 7512 7513 /* 7514 * Function: sd_unit_detach 7515 * 7516 * Description: Performs DDI_DETACH processing for sddetach(). 7517 * 7518 * Return Code: DDI_SUCCESS 7519 * DDI_FAILURE 7520 * 7521 * Context: Kernel thread context 7522 */ 7523 7524 static int 7525 sd_unit_detach(dev_info_t *devi) 7526 { 7527 struct scsi_device *devp; 7528 struct sd_lun *un; 7529 int i; 7530 int tgt; 7531 dev_t dev; 7532 dev_info_t *pdip = ddi_get_parent(devi); 7533 int instance = ddi_get_instance(devi); 7534 7535 mutex_enter(&sd_detach_mutex); 7536 7537 /* 7538 * Fail the detach for any of the following: 7539 * - Unable to get the sd_lun struct for the instance 7540 * - A layered driver has an outstanding open on the instance 7541 * - Another thread is already detaching this instance 7542 * - Another thread is currently performing an open 7543 */ 7544 devp = ddi_get_driver_private(devi); 7545 if ((devp == NULL) || 7546 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 7547 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 7548 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 7549 mutex_exit(&sd_detach_mutex); 7550 return (DDI_FAILURE); 7551 } 7552 7553 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 7554 7555 /* 7556 * Mark this instance as currently in a detach, to inhibit any 7557 * opens from a layered driver. 7558 */ 7559 un->un_detach_count++; 7560 mutex_exit(&sd_detach_mutex); 7561 7562 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7563 SCSI_ADDR_PROP_TARGET, -1); 7564 7565 dev = sd_make_device(SD_DEVINFO(un)); 7566 7567 #ifndef lint 7568 _NOTE(COMPETING_THREADS_NOW); 7569 #endif 7570 7571 mutex_enter(SD_MUTEX(un)); 7572 7573 /* 7574 * Fail the detach if there are any outstanding layered 7575 * opens on this device. 7576 */ 7577 for (i = 0; i < NDKMAP; i++) { 7578 if (un->un_ocmap.lyropen[i] != 0) { 7579 goto err_notclosed; 7580 } 7581 } 7582 7583 /* 7584 * Verify there are NO outstanding commands issued to this device. 7585 * ie, un_ncmds_in_transport == 0. 7586 * It's possible to have outstanding commands through the physio 7587 * code path, even though everything's closed. 7588 */ 7589 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 7590 (un->un_direct_priority_timeid != NULL) || 7591 (un->un_state == SD_STATE_RWAIT)) { 7592 mutex_exit(SD_MUTEX(un)); 7593 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7594 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 7595 goto err_stillbusy; 7596 } 7597 7598 /* 7599 * If we have the device reserved, release the reservation. 7600 */ 7601 if ((un->un_resvd_status & SD_RESERVE) && 7602 !(un->un_resvd_status & SD_LOST_RESERVE)) { 7603 mutex_exit(SD_MUTEX(un)); 7604 /* 7605 * Note: sd_reserve_release sends a command to the device 7606 * via the sd_ioctlcmd() path, and can sleep. 7607 */ 7608 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 7609 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7610 "sd_dr_detach: Cannot release reservation \n"); 7611 } 7612 } else { 7613 mutex_exit(SD_MUTEX(un)); 7614 } 7615 7616 /* 7617 * Untimeout any reserve recover, throttle reset, restart unit 7618 * and delayed broadcast timeout threads. Protect the timeout pointer 7619 * from getting nulled by their callback functions. 7620 */ 7621 mutex_enter(SD_MUTEX(un)); 7622 if (un->un_resvd_timeid != NULL) { 7623 timeout_id_t temp_id = un->un_resvd_timeid; 7624 un->un_resvd_timeid = NULL; 7625 mutex_exit(SD_MUTEX(un)); 7626 (void) untimeout(temp_id); 7627 mutex_enter(SD_MUTEX(un)); 7628 } 7629 7630 if (un->un_reset_throttle_timeid != NULL) { 7631 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7632 un->un_reset_throttle_timeid = NULL; 7633 mutex_exit(SD_MUTEX(un)); 7634 (void) untimeout(temp_id); 7635 mutex_enter(SD_MUTEX(un)); 7636 } 7637 7638 if (un->un_startstop_timeid != NULL) { 7639 timeout_id_t temp_id = un->un_startstop_timeid; 7640 un->un_startstop_timeid = NULL; 7641 mutex_exit(SD_MUTEX(un)); 7642 (void) untimeout(temp_id); 7643 mutex_enter(SD_MUTEX(un)); 7644 } 7645 7646 if (un->un_dcvb_timeid != NULL) { 7647 timeout_id_t temp_id = un->un_dcvb_timeid; 7648 un->un_dcvb_timeid = NULL; 7649 mutex_exit(SD_MUTEX(un)); 7650 (void) untimeout(temp_id); 7651 } else { 7652 mutex_exit(SD_MUTEX(un)); 7653 } 7654 7655 /* Remove any pending reservation reclaim requests for this device */ 7656 sd_rmv_resv_reclaim_req(dev); 7657 7658 mutex_enter(SD_MUTEX(un)); 7659 7660 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 7661 if (un->un_direct_priority_timeid != NULL) { 7662 timeout_id_t temp_id = un->un_direct_priority_timeid; 7663 un->un_direct_priority_timeid = NULL; 7664 mutex_exit(SD_MUTEX(un)); 7665 (void) untimeout(temp_id); 7666 mutex_enter(SD_MUTEX(un)); 7667 } 7668 7669 /* Cancel any active multi-host disk watch thread requests */ 7670 if (un->un_mhd_token != NULL) { 7671 mutex_exit(SD_MUTEX(un)); 7672 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 7673 if (scsi_watch_request_terminate(un->un_mhd_token, 7674 SCSI_WATCH_TERMINATE_NOWAIT)) { 7675 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7676 "sd_dr_detach: Cannot cancel mhd watch request\n"); 7677 /* 7678 * Note: We are returning here after having removed 7679 * some driver timeouts above. This is consistent with 7680 * the legacy implementation but perhaps the watch 7681 * terminate call should be made with the wait flag set. 7682 */ 7683 goto err_stillbusy; 7684 } 7685 mutex_enter(SD_MUTEX(un)); 7686 un->un_mhd_token = NULL; 7687 } 7688 7689 if (un->un_swr_token != NULL) { 7690 mutex_exit(SD_MUTEX(un)); 7691 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 7692 if (scsi_watch_request_terminate(un->un_swr_token, 7693 SCSI_WATCH_TERMINATE_NOWAIT)) { 7694 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7695 "sd_dr_detach: Cannot cancel swr watch request\n"); 7696 /* 7697 * Note: We are returning here after having removed 7698 * some driver timeouts above. This is consistent with 7699 * the legacy implementation but perhaps the watch 7700 * terminate call should be made with the wait flag set. 7701 */ 7702 goto err_stillbusy; 7703 } 7704 mutex_enter(SD_MUTEX(un)); 7705 un->un_swr_token = NULL; 7706 } 7707 7708 mutex_exit(SD_MUTEX(un)); 7709 7710 /* 7711 * Clear any scsi_reset_notifies. We clear the reset notifies 7712 * if we have not registered one. 7713 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 7714 */ 7715 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 7716 sd_mhd_reset_notify_cb, (caddr_t)un); 7717 7718 /* 7719 * protect the timeout pointers from getting nulled by 7720 * their callback functions during the cancellation process. 7721 * In such a scenario untimeout can be invoked with a null value. 7722 */ 7723 _NOTE(NO_COMPETING_THREADS_NOW); 7724 7725 mutex_enter(&un->un_pm_mutex); 7726 if (un->un_pm_idle_timeid != NULL) { 7727 timeout_id_t temp_id = un->un_pm_idle_timeid; 7728 un->un_pm_idle_timeid = NULL; 7729 mutex_exit(&un->un_pm_mutex); 7730 7731 /* 7732 * Timeout is active; cancel it. 7733 * Note that it'll never be active on a device 7734 * that does not support PM therefore we don't 7735 * have to check before calling pm_idle_component. 7736 */ 7737 (void) untimeout(temp_id); 7738 (void) pm_idle_component(SD_DEVINFO(un), 0); 7739 mutex_enter(&un->un_pm_mutex); 7740 } 7741 7742 /* 7743 * Check whether there is already a timeout scheduled for power 7744 * management. If yes then don't lower the power here, that's. 7745 * the timeout handler's job. 7746 */ 7747 if (un->un_pm_timeid != NULL) { 7748 timeout_id_t temp_id = un->un_pm_timeid; 7749 un->un_pm_timeid = NULL; 7750 mutex_exit(&un->un_pm_mutex); 7751 /* 7752 * Timeout is active; cancel it. 7753 * Note that it'll never be active on a device 7754 * that does not support PM therefore we don't 7755 * have to check before calling pm_idle_component. 7756 */ 7757 (void) untimeout(temp_id); 7758 (void) pm_idle_component(SD_DEVINFO(un), 0); 7759 7760 } else { 7761 mutex_exit(&un->un_pm_mutex); 7762 if ((un->un_f_pm_is_enabled == TRUE) && 7763 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 7764 DDI_SUCCESS)) { 7765 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7766 "sd_dr_detach: Lower power request failed, ignoring.\n"); 7767 /* 7768 * Fix for bug: 4297749, item # 13 7769 * The above test now includes a check to see if PM is 7770 * supported by this device before call 7771 * pm_lower_power(). 7772 * Note, the following is not dead code. The call to 7773 * pm_lower_power above will generate a call back into 7774 * our sdpower routine which might result in a timeout 7775 * handler getting activated. Therefore the following 7776 * code is valid and necessary. 7777 */ 7778 mutex_enter(&un->un_pm_mutex); 7779 if (un->un_pm_timeid != NULL) { 7780 timeout_id_t temp_id = un->un_pm_timeid; 7781 un->un_pm_timeid = NULL; 7782 mutex_exit(&un->un_pm_mutex); 7783 (void) untimeout(temp_id); 7784 (void) pm_idle_component(SD_DEVINFO(un), 0); 7785 } else { 7786 mutex_exit(&un->un_pm_mutex); 7787 } 7788 } 7789 } 7790 7791 /* 7792 * Cleanup from the scsi_ifsetcap() calls (437868) 7793 * Relocated here from above to be after the call to 7794 * pm_lower_power, which was getting errors. 7795 */ 7796 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7797 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7798 7799 /* 7800 * Currently, tagged queuing is supported per target based by HBA. 7801 * Setting this per lun instance actually sets the capability of this 7802 * target in HBA, which affects those luns already attached on the 7803 * same target. So during detach, we can only disable this capability 7804 * only when this is the only lun left on this target. By doing 7805 * this, we assume a target has the same tagged queuing capability 7806 * for every lun. The condition can be removed when HBA is changed to 7807 * support per lun based tagged queuing capability. 7808 */ 7809 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 7810 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7811 } 7812 7813 if (un->un_f_is_fibre == FALSE) { 7814 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7815 } 7816 7817 /* 7818 * Remove any event callbacks, fibre only 7819 */ 7820 if (un->un_f_is_fibre == TRUE) { 7821 if ((un->un_insert_event != NULL) && 7822 (ddi_remove_event_handler(un->un_insert_cb_id) != 7823 DDI_SUCCESS)) { 7824 /* 7825 * Note: We are returning here after having done 7826 * substantial cleanup above. This is consistent 7827 * with the legacy implementation but this may not 7828 * be the right thing to do. 7829 */ 7830 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7831 "sd_dr_detach: Cannot cancel insert event\n"); 7832 goto err_remove_event; 7833 } 7834 un->un_insert_event = NULL; 7835 7836 if ((un->un_remove_event != NULL) && 7837 (ddi_remove_event_handler(un->un_remove_cb_id) != 7838 DDI_SUCCESS)) { 7839 /* 7840 * Note: We are returning here after having done 7841 * substantial cleanup above. This is consistent 7842 * with the legacy implementation but this may not 7843 * be the right thing to do. 7844 */ 7845 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7846 "sd_dr_detach: Cannot cancel remove event\n"); 7847 goto err_remove_event; 7848 } 7849 un->un_remove_event = NULL; 7850 } 7851 7852 /* Do not free the softstate if the callback routine is active */ 7853 sd_sync_with_callback(un); 7854 7855 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 7856 cmlb_free_handle(&un->un_cmlbhandle); 7857 7858 /* 7859 * Hold the detach mutex here, to make sure that no other threads ever 7860 * can access a (partially) freed soft state structure. 7861 */ 7862 mutex_enter(&sd_detach_mutex); 7863 7864 /* 7865 * Clean up the soft state struct. 7866 * Cleanup is done in reverse order of allocs/inits. 7867 * At this point there should be no competing threads anymore. 7868 */ 7869 7870 /* Unregister and free device id. */ 7871 ddi_devid_unregister(devi); 7872 if (un->un_devid) { 7873 ddi_devid_free(un->un_devid); 7874 un->un_devid = NULL; 7875 } 7876 7877 /* 7878 * Destroy wmap cache if it exists. 7879 */ 7880 if (un->un_wm_cache != NULL) { 7881 kmem_cache_destroy(un->un_wm_cache); 7882 un->un_wm_cache = NULL; 7883 } 7884 7885 /* 7886 * kstat cleanup is done in detach for all device types (4363169). 7887 * We do not want to fail detach if the device kstats are not deleted 7888 * since there is a confusion about the devo_refcnt for the device. 7889 * We just delete the kstats and let detach complete successfully. 7890 */ 7891 if (un->un_stats != NULL) { 7892 kstat_delete(un->un_stats); 7893 un->un_stats = NULL; 7894 } 7895 if (un->un_errstats != NULL) { 7896 kstat_delete(un->un_errstats); 7897 un->un_errstats = NULL; 7898 } 7899 7900 /* Remove partition stats */ 7901 if (un->un_f_pkstats_enabled) { 7902 for (i = 0; i < NSDMAP; i++) { 7903 if (un->un_pstats[i] != NULL) { 7904 kstat_delete(un->un_pstats[i]); 7905 un->un_pstats[i] = NULL; 7906 } 7907 } 7908 } 7909 7910 /* Remove xbuf registration */ 7911 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7912 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7913 7914 /* Remove driver properties */ 7915 ddi_prop_remove_all(devi); 7916 7917 mutex_destroy(&un->un_pm_mutex); 7918 cv_destroy(&un->un_pm_busy_cv); 7919 7920 cv_destroy(&un->un_wcc_cv); 7921 7922 /* Open/close semaphore */ 7923 sema_destroy(&un->un_semoclose); 7924 7925 /* Removable media condvar. */ 7926 cv_destroy(&un->un_state_cv); 7927 7928 /* Suspend/resume condvar. */ 7929 cv_destroy(&un->un_suspend_cv); 7930 cv_destroy(&un->un_disk_busy_cv); 7931 7932 sd_free_rqs(un); 7933 7934 /* Free up soft state */ 7935 devp->sd_private = NULL; 7936 7937 bzero(un, sizeof (struct sd_lun)); 7938 ddi_soft_state_free(sd_state, instance); 7939 7940 mutex_exit(&sd_detach_mutex); 7941 7942 /* This frees up the INQUIRY data associated with the device. */ 7943 scsi_unprobe(devp); 7944 7945 /* 7946 * After successfully detaching an instance, we update the information 7947 * of how many luns have been attached in the relative target and 7948 * controller for parallel SCSI. This information is used when sd tries 7949 * to set the tagged queuing capability in HBA. 7950 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 7951 * check if the device is parallel SCSI. However, we don't need to 7952 * check here because we've already checked during attach. No device 7953 * that is not parallel SCSI is in the chain. 7954 */ 7955 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7956 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 7957 } 7958 7959 return (DDI_SUCCESS); 7960 7961 err_notclosed: 7962 mutex_exit(SD_MUTEX(un)); 7963 7964 err_stillbusy: 7965 _NOTE(NO_COMPETING_THREADS_NOW); 7966 7967 err_remove_event: 7968 mutex_enter(&sd_detach_mutex); 7969 un->un_detach_count--; 7970 mutex_exit(&sd_detach_mutex); 7971 7972 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 7973 return (DDI_FAILURE); 7974 } 7975 7976 7977 /* 7978 * Function: sd_create_errstats 7979 * 7980 * Description: This routine instantiates the device error stats. 7981 * 7982 * Note: During attach the stats are instantiated first so they are 7983 * available for attach-time routines that utilize the driver 7984 * iopath to send commands to the device. The stats are initialized 7985 * separately so data obtained during some attach-time routines is 7986 * available. (4362483) 7987 * 7988 * Arguments: un - driver soft state (unit) structure 7989 * instance - driver instance 7990 * 7991 * Context: Kernel thread context 7992 */ 7993 7994 static void 7995 sd_create_errstats(struct sd_lun *un, int instance) 7996 { 7997 struct sd_errstats *stp; 7998 char kstatmodule_err[KSTAT_STRLEN]; 7999 char kstatname[KSTAT_STRLEN]; 8000 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 8001 8002 ASSERT(un != NULL); 8003 8004 if (un->un_errstats != NULL) { 8005 return; 8006 } 8007 8008 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 8009 "%serr", sd_label); 8010 (void) snprintf(kstatname, sizeof (kstatname), 8011 "%s%d,err", sd_label, instance); 8012 8013 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 8014 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 8015 8016 if (un->un_errstats == NULL) { 8017 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8018 "sd_create_errstats: Failed kstat_create\n"); 8019 return; 8020 } 8021 8022 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8023 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 8024 KSTAT_DATA_UINT32); 8025 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 8026 KSTAT_DATA_UINT32); 8027 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 8028 KSTAT_DATA_UINT32); 8029 kstat_named_init(&stp->sd_vid, "Vendor", 8030 KSTAT_DATA_CHAR); 8031 kstat_named_init(&stp->sd_pid, "Product", 8032 KSTAT_DATA_CHAR); 8033 kstat_named_init(&stp->sd_revision, "Revision", 8034 KSTAT_DATA_CHAR); 8035 kstat_named_init(&stp->sd_serial, "Serial No", 8036 KSTAT_DATA_CHAR); 8037 kstat_named_init(&stp->sd_capacity, "Size", 8038 KSTAT_DATA_ULONGLONG); 8039 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 8040 KSTAT_DATA_UINT32); 8041 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 8042 KSTAT_DATA_UINT32); 8043 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 8044 KSTAT_DATA_UINT32); 8045 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 8046 KSTAT_DATA_UINT32); 8047 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 8048 KSTAT_DATA_UINT32); 8049 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 8050 KSTAT_DATA_UINT32); 8051 8052 un->un_errstats->ks_private = un; 8053 un->un_errstats->ks_update = nulldev; 8054 8055 kstat_install(un->un_errstats); 8056 } 8057 8058 8059 /* 8060 * Function: sd_set_errstats 8061 * 8062 * Description: This routine sets the value of the vendor id, product id, 8063 * revision, serial number, and capacity device error stats. 8064 * 8065 * Note: During attach the stats are instantiated first so they are 8066 * available for attach-time routines that utilize the driver 8067 * iopath to send commands to the device. The stats are initialized 8068 * separately so data obtained during some attach-time routines is 8069 * available. (4362483) 8070 * 8071 * Arguments: un - driver soft state (unit) structure 8072 * 8073 * Context: Kernel thread context 8074 */ 8075 8076 static void 8077 sd_set_errstats(struct sd_lun *un) 8078 { 8079 struct sd_errstats *stp; 8080 8081 ASSERT(un != NULL); 8082 ASSERT(un->un_errstats != NULL); 8083 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8084 ASSERT(stp != NULL); 8085 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 8086 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 8087 (void) strncpy(stp->sd_revision.value.c, 8088 un->un_sd->sd_inq->inq_revision, 4); 8089 8090 /* 8091 * All the errstats are persistent across detach/attach, 8092 * so reset all the errstats here in case of the hot 8093 * replacement of disk drives, except for not changed 8094 * Sun qualified drives. 8095 */ 8096 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 8097 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8098 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 8099 stp->sd_softerrs.value.ui32 = 0; 8100 stp->sd_harderrs.value.ui32 = 0; 8101 stp->sd_transerrs.value.ui32 = 0; 8102 stp->sd_rq_media_err.value.ui32 = 0; 8103 stp->sd_rq_ntrdy_err.value.ui32 = 0; 8104 stp->sd_rq_nodev_err.value.ui32 = 0; 8105 stp->sd_rq_recov_err.value.ui32 = 0; 8106 stp->sd_rq_illrq_err.value.ui32 = 0; 8107 stp->sd_rq_pfa_err.value.ui32 = 0; 8108 } 8109 8110 /* 8111 * Set the "Serial No" kstat for Sun qualified drives (indicated by 8112 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 8113 * (4376302)) 8114 */ 8115 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 8116 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8117 sizeof (SD_INQUIRY(un)->inq_serial)); 8118 } 8119 8120 if (un->un_f_blockcount_is_valid != TRUE) { 8121 /* 8122 * Set capacity error stat to 0 for no media. This ensures 8123 * a valid capacity is displayed in response to 'iostat -E' 8124 * when no media is present in the device. 8125 */ 8126 stp->sd_capacity.value.ui64 = 0; 8127 } else { 8128 /* 8129 * Multiply un_blockcount by un->un_sys_blocksize to get 8130 * capacity. 8131 * 8132 * Note: for non-512 blocksize devices "un_blockcount" has been 8133 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 8134 * (un_tgt_blocksize / un->un_sys_blocksize). 8135 */ 8136 stp->sd_capacity.value.ui64 = (uint64_t) 8137 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 8138 } 8139 } 8140 8141 8142 /* 8143 * Function: sd_set_pstats 8144 * 8145 * Description: This routine instantiates and initializes the partition 8146 * stats for each partition with more than zero blocks. 8147 * (4363169) 8148 * 8149 * Arguments: un - driver soft state (unit) structure 8150 * 8151 * Context: Kernel thread context 8152 */ 8153 8154 static void 8155 sd_set_pstats(struct sd_lun *un) 8156 { 8157 char kstatname[KSTAT_STRLEN]; 8158 int instance; 8159 int i; 8160 diskaddr_t nblks = 0; 8161 char *partname = NULL; 8162 8163 ASSERT(un != NULL); 8164 8165 instance = ddi_get_instance(SD_DEVINFO(un)); 8166 8167 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 8168 for (i = 0; i < NSDMAP; i++) { 8169 8170 if (cmlb_partinfo(un->un_cmlbhandle, i, 8171 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 8172 continue; 8173 mutex_enter(SD_MUTEX(un)); 8174 8175 if ((un->un_pstats[i] == NULL) && 8176 (nblks != 0)) { 8177 8178 (void) snprintf(kstatname, sizeof (kstatname), 8179 "%s%d,%s", sd_label, instance, 8180 partname); 8181 8182 un->un_pstats[i] = kstat_create(sd_label, 8183 instance, kstatname, "partition", KSTAT_TYPE_IO, 8184 1, KSTAT_FLAG_PERSISTENT); 8185 if (un->un_pstats[i] != NULL) { 8186 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 8187 kstat_install(un->un_pstats[i]); 8188 } 8189 } 8190 mutex_exit(SD_MUTEX(un)); 8191 } 8192 } 8193 8194 8195 #if (defined(__fibre)) 8196 /* 8197 * Function: sd_init_event_callbacks 8198 * 8199 * Description: This routine initializes the insertion and removal event 8200 * callbacks. (fibre only) 8201 * 8202 * Arguments: un - driver soft state (unit) structure 8203 * 8204 * Context: Kernel thread context 8205 */ 8206 8207 static void 8208 sd_init_event_callbacks(struct sd_lun *un) 8209 { 8210 ASSERT(un != NULL); 8211 8212 if ((un->un_insert_event == NULL) && 8213 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 8214 &un->un_insert_event) == DDI_SUCCESS)) { 8215 /* 8216 * Add the callback for an insertion event 8217 */ 8218 (void) ddi_add_event_handler(SD_DEVINFO(un), 8219 un->un_insert_event, sd_event_callback, (void *)un, 8220 &(un->un_insert_cb_id)); 8221 } 8222 8223 if ((un->un_remove_event == NULL) && 8224 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 8225 &un->un_remove_event) == DDI_SUCCESS)) { 8226 /* 8227 * Add the callback for a removal event 8228 */ 8229 (void) ddi_add_event_handler(SD_DEVINFO(un), 8230 un->un_remove_event, sd_event_callback, (void *)un, 8231 &(un->un_remove_cb_id)); 8232 } 8233 } 8234 8235 8236 /* 8237 * Function: sd_event_callback 8238 * 8239 * Description: This routine handles insert/remove events (photon). The 8240 * state is changed to OFFLINE which can be used to supress 8241 * error msgs. (fibre only) 8242 * 8243 * Arguments: un - driver soft state (unit) structure 8244 * 8245 * Context: Callout thread context 8246 */ 8247 /* ARGSUSED */ 8248 static void 8249 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 8250 void *bus_impldata) 8251 { 8252 struct sd_lun *un = (struct sd_lun *)arg; 8253 8254 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 8255 if (event == un->un_insert_event) { 8256 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 8257 mutex_enter(SD_MUTEX(un)); 8258 if (un->un_state == SD_STATE_OFFLINE) { 8259 if (un->un_last_state != SD_STATE_SUSPENDED) { 8260 un->un_state = un->un_last_state; 8261 } else { 8262 /* 8263 * We have gone through SUSPEND/RESUME while 8264 * we were offline. Restore the last state 8265 */ 8266 un->un_state = un->un_save_state; 8267 } 8268 } 8269 mutex_exit(SD_MUTEX(un)); 8270 8271 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 8272 } else if (event == un->un_remove_event) { 8273 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 8274 mutex_enter(SD_MUTEX(un)); 8275 /* 8276 * We need to handle an event callback that occurs during 8277 * the suspend operation, since we don't prevent it. 8278 */ 8279 if (un->un_state != SD_STATE_OFFLINE) { 8280 if (un->un_state != SD_STATE_SUSPENDED) { 8281 New_state(un, SD_STATE_OFFLINE); 8282 } else { 8283 un->un_last_state = SD_STATE_OFFLINE; 8284 } 8285 } 8286 mutex_exit(SD_MUTEX(un)); 8287 } else { 8288 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 8289 "!Unknown event\n"); 8290 } 8291 8292 } 8293 #endif 8294 8295 /* 8296 * Function: sd_cache_control() 8297 * 8298 * Description: This routine is the driver entry point for setting 8299 * read and write caching by modifying the WCE (write cache 8300 * enable) and RCD (read cache disable) bits of mode 8301 * page 8 (MODEPAGE_CACHING). 8302 * 8303 * Arguments: un - driver soft state (unit) structure 8304 * rcd_flag - flag for controlling the read cache 8305 * wce_flag - flag for controlling the write cache 8306 * 8307 * Return Code: EIO 8308 * code returned by sd_send_scsi_MODE_SENSE and 8309 * sd_send_scsi_MODE_SELECT 8310 * 8311 * Context: Kernel Thread 8312 */ 8313 8314 static int 8315 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 8316 { 8317 struct mode_caching *mode_caching_page; 8318 uchar_t *header; 8319 size_t buflen; 8320 int hdrlen; 8321 int bd_len; 8322 int rval = 0; 8323 struct mode_header_grp2 *mhp; 8324 8325 ASSERT(un != NULL); 8326 8327 /* 8328 * Do a test unit ready, otherwise a mode sense may not work if this 8329 * is the first command sent to the device after boot. 8330 */ 8331 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8332 8333 if (un->un_f_cfg_is_atapi == TRUE) { 8334 hdrlen = MODE_HEADER_LENGTH_GRP2; 8335 } else { 8336 hdrlen = MODE_HEADER_LENGTH; 8337 } 8338 8339 /* 8340 * Allocate memory for the retrieved mode page and its headers. Set 8341 * a pointer to the page itself. Use mode_cache_scsi3 to insure 8342 * we get all of the mode sense data otherwise, the mode select 8343 * will fail. mode_cache_scsi3 is a superset of mode_caching. 8344 */ 8345 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 8346 sizeof (struct mode_cache_scsi3); 8347 8348 header = kmem_zalloc(buflen, KM_SLEEP); 8349 8350 /* Get the information from the device. */ 8351 if (un->un_f_cfg_is_atapi == TRUE) { 8352 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8353 MODEPAGE_CACHING, SD_PATH_DIRECT); 8354 } else { 8355 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8356 MODEPAGE_CACHING, SD_PATH_DIRECT); 8357 } 8358 if (rval != 0) { 8359 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8360 "sd_cache_control: Mode Sense Failed\n"); 8361 kmem_free(header, buflen); 8362 return (rval); 8363 } 8364 8365 /* 8366 * Determine size of Block Descriptors in order to locate 8367 * the mode page data. ATAPI devices return 0, SCSI devices 8368 * should return MODE_BLK_DESC_LENGTH. 8369 */ 8370 if (un->un_f_cfg_is_atapi == TRUE) { 8371 mhp = (struct mode_header_grp2 *)header; 8372 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8373 } else { 8374 bd_len = ((struct mode_header *)header)->bdesc_length; 8375 } 8376 8377 if (bd_len > MODE_BLK_DESC_LENGTH) { 8378 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8379 "sd_cache_control: Mode Sense returned invalid " 8380 "block descriptor length\n"); 8381 kmem_free(header, buflen); 8382 return (EIO); 8383 } 8384 8385 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8386 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8387 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8388 " caching page code mismatch %d\n", 8389 mode_caching_page->mode_page.code); 8390 kmem_free(header, buflen); 8391 return (EIO); 8392 } 8393 8394 /* Check the relevant bits on successful mode sense. */ 8395 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 8396 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 8397 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 8398 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 8399 8400 size_t sbuflen; 8401 uchar_t save_pg; 8402 8403 /* 8404 * Construct select buffer length based on the 8405 * length of the sense data returned. 8406 */ 8407 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 8408 sizeof (struct mode_page) + 8409 (int)mode_caching_page->mode_page.length; 8410 8411 /* 8412 * Set the caching bits as requested. 8413 */ 8414 if (rcd_flag == SD_CACHE_ENABLE) 8415 mode_caching_page->rcd = 0; 8416 else if (rcd_flag == SD_CACHE_DISABLE) 8417 mode_caching_page->rcd = 1; 8418 8419 if (wce_flag == SD_CACHE_ENABLE) 8420 mode_caching_page->wce = 1; 8421 else if (wce_flag == SD_CACHE_DISABLE) 8422 mode_caching_page->wce = 0; 8423 8424 /* 8425 * Save the page if the mode sense says the 8426 * drive supports it. 8427 */ 8428 save_pg = mode_caching_page->mode_page.ps ? 8429 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 8430 8431 /* Clear reserved bits before mode select. */ 8432 mode_caching_page->mode_page.ps = 0; 8433 8434 /* 8435 * Clear out mode header for mode select. 8436 * The rest of the retrieved page will be reused. 8437 */ 8438 bzero(header, hdrlen); 8439 8440 if (un->un_f_cfg_is_atapi == TRUE) { 8441 mhp = (struct mode_header_grp2 *)header; 8442 mhp->bdesc_length_hi = bd_len >> 8; 8443 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 8444 } else { 8445 ((struct mode_header *)header)->bdesc_length = bd_len; 8446 } 8447 8448 /* Issue mode select to change the cache settings */ 8449 if (un->un_f_cfg_is_atapi == TRUE) { 8450 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 8451 sbuflen, save_pg, SD_PATH_DIRECT); 8452 } else { 8453 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 8454 sbuflen, save_pg, SD_PATH_DIRECT); 8455 } 8456 } 8457 8458 kmem_free(header, buflen); 8459 return (rval); 8460 } 8461 8462 8463 /* 8464 * Function: sd_get_write_cache_enabled() 8465 * 8466 * Description: This routine is the driver entry point for determining if 8467 * write caching is enabled. It examines the WCE (write cache 8468 * enable) bits of mode page 8 (MODEPAGE_CACHING). 8469 * 8470 * Arguments: un - driver soft state (unit) structure 8471 * is_enabled - pointer to int where write cache enabled state 8472 * is returned (non-zero -> write cache enabled) 8473 * 8474 * 8475 * Return Code: EIO 8476 * code returned by sd_send_scsi_MODE_SENSE 8477 * 8478 * Context: Kernel Thread 8479 * 8480 * NOTE: If ioctl is added to disable write cache, this sequence should 8481 * be followed so that no locking is required for accesses to 8482 * un->un_f_write_cache_enabled: 8483 * do mode select to clear wce 8484 * do synchronize cache to flush cache 8485 * set un->un_f_write_cache_enabled = FALSE 8486 * 8487 * Conversely, an ioctl to enable the write cache should be done 8488 * in this order: 8489 * set un->un_f_write_cache_enabled = TRUE 8490 * do mode select to set wce 8491 */ 8492 8493 static int 8494 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 8495 { 8496 struct mode_caching *mode_caching_page; 8497 uchar_t *header; 8498 size_t buflen; 8499 int hdrlen; 8500 int bd_len; 8501 int rval = 0; 8502 8503 ASSERT(un != NULL); 8504 ASSERT(is_enabled != NULL); 8505 8506 /* in case of error, flag as enabled */ 8507 *is_enabled = TRUE; 8508 8509 /* 8510 * Do a test unit ready, otherwise a mode sense may not work if this 8511 * is the first command sent to the device after boot. 8512 */ 8513 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8514 8515 if (un->un_f_cfg_is_atapi == TRUE) { 8516 hdrlen = MODE_HEADER_LENGTH_GRP2; 8517 } else { 8518 hdrlen = MODE_HEADER_LENGTH; 8519 } 8520 8521 /* 8522 * Allocate memory for the retrieved mode page and its headers. Set 8523 * a pointer to the page itself. 8524 */ 8525 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 8526 header = kmem_zalloc(buflen, KM_SLEEP); 8527 8528 /* Get the information from the device. */ 8529 if (un->un_f_cfg_is_atapi == TRUE) { 8530 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8531 MODEPAGE_CACHING, SD_PATH_DIRECT); 8532 } else { 8533 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8534 MODEPAGE_CACHING, SD_PATH_DIRECT); 8535 } 8536 if (rval != 0) { 8537 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8538 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 8539 kmem_free(header, buflen); 8540 return (rval); 8541 } 8542 8543 /* 8544 * Determine size of Block Descriptors in order to locate 8545 * the mode page data. ATAPI devices return 0, SCSI devices 8546 * should return MODE_BLK_DESC_LENGTH. 8547 */ 8548 if (un->un_f_cfg_is_atapi == TRUE) { 8549 struct mode_header_grp2 *mhp; 8550 mhp = (struct mode_header_grp2 *)header; 8551 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8552 } else { 8553 bd_len = ((struct mode_header *)header)->bdesc_length; 8554 } 8555 8556 if (bd_len > MODE_BLK_DESC_LENGTH) { 8557 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8558 "sd_get_write_cache_enabled: Mode Sense returned invalid " 8559 "block descriptor length\n"); 8560 kmem_free(header, buflen); 8561 return (EIO); 8562 } 8563 8564 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8565 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8566 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8567 " caching page code mismatch %d\n", 8568 mode_caching_page->mode_page.code); 8569 kmem_free(header, buflen); 8570 return (EIO); 8571 } 8572 *is_enabled = mode_caching_page->wce; 8573 8574 kmem_free(header, buflen); 8575 return (0); 8576 } 8577 8578 8579 /* 8580 * Function: sd_make_device 8581 * 8582 * Description: Utility routine to return the Solaris device number from 8583 * the data in the device's dev_info structure. 8584 * 8585 * Return Code: The Solaris device number 8586 * 8587 * Context: Any 8588 */ 8589 8590 static dev_t 8591 sd_make_device(dev_info_t *devi) 8592 { 8593 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 8594 ddi_get_instance(devi) << SDUNIT_SHIFT)); 8595 } 8596 8597 8598 /* 8599 * Function: sd_pm_entry 8600 * 8601 * Description: Called at the start of a new command to manage power 8602 * and busy status of a device. This includes determining whether 8603 * the current power state of the device is sufficient for 8604 * performing the command or whether it must be changed. 8605 * The PM framework is notified appropriately. 8606 * Only with a return status of DDI_SUCCESS will the 8607 * component be busy to the framework. 8608 * 8609 * All callers of sd_pm_entry must check the return status 8610 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 8611 * of DDI_FAILURE indicates the device failed to power up. 8612 * In this case un_pm_count has been adjusted so the result 8613 * on exit is still powered down, ie. count is less than 0. 8614 * Calling sd_pm_exit with this count value hits an ASSERT. 8615 * 8616 * Return Code: DDI_SUCCESS or DDI_FAILURE 8617 * 8618 * Context: Kernel thread context. 8619 */ 8620 8621 static int 8622 sd_pm_entry(struct sd_lun *un) 8623 { 8624 int return_status = DDI_SUCCESS; 8625 8626 ASSERT(!mutex_owned(SD_MUTEX(un))); 8627 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8628 8629 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 8630 8631 if (un->un_f_pm_is_enabled == FALSE) { 8632 SD_TRACE(SD_LOG_IO_PM, un, 8633 "sd_pm_entry: exiting, PM not enabled\n"); 8634 return (return_status); 8635 } 8636 8637 /* 8638 * Just increment a counter if PM is enabled. On the transition from 8639 * 0 ==> 1, mark the device as busy. The iodone side will decrement 8640 * the count with each IO and mark the device as idle when the count 8641 * hits 0. 8642 * 8643 * If the count is less than 0 the device is powered down. If a powered 8644 * down device is successfully powered up then the count must be 8645 * incremented to reflect the power up. Note that it'll get incremented 8646 * a second time to become busy. 8647 * 8648 * Because the following has the potential to change the device state 8649 * and must release the un_pm_mutex to do so, only one thread can be 8650 * allowed through at a time. 8651 */ 8652 8653 mutex_enter(&un->un_pm_mutex); 8654 while (un->un_pm_busy == TRUE) { 8655 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 8656 } 8657 un->un_pm_busy = TRUE; 8658 8659 if (un->un_pm_count < 1) { 8660 8661 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 8662 8663 /* 8664 * Indicate we are now busy so the framework won't attempt to 8665 * power down the device. This call will only fail if either 8666 * we passed a bad component number or the device has no 8667 * components. Neither of these should ever happen. 8668 */ 8669 mutex_exit(&un->un_pm_mutex); 8670 return_status = pm_busy_component(SD_DEVINFO(un), 0); 8671 ASSERT(return_status == DDI_SUCCESS); 8672 8673 mutex_enter(&un->un_pm_mutex); 8674 8675 if (un->un_pm_count < 0) { 8676 mutex_exit(&un->un_pm_mutex); 8677 8678 SD_TRACE(SD_LOG_IO_PM, un, 8679 "sd_pm_entry: power up component\n"); 8680 8681 /* 8682 * pm_raise_power will cause sdpower to be called 8683 * which brings the device power level to the 8684 * desired state, ON in this case. If successful, 8685 * un_pm_count and un_power_level will be updated 8686 * appropriately. 8687 */ 8688 return_status = pm_raise_power(SD_DEVINFO(un), 0, 8689 SD_SPINDLE_ON); 8690 8691 mutex_enter(&un->un_pm_mutex); 8692 8693 if (return_status != DDI_SUCCESS) { 8694 /* 8695 * Power up failed. 8696 * Idle the device and adjust the count 8697 * so the result on exit is that we're 8698 * still powered down, ie. count is less than 0. 8699 */ 8700 SD_TRACE(SD_LOG_IO_PM, un, 8701 "sd_pm_entry: power up failed," 8702 " idle the component\n"); 8703 8704 (void) pm_idle_component(SD_DEVINFO(un), 0); 8705 un->un_pm_count--; 8706 } else { 8707 /* 8708 * Device is powered up, verify the 8709 * count is non-negative. 8710 * This is debug only. 8711 */ 8712 ASSERT(un->un_pm_count == 0); 8713 } 8714 } 8715 8716 if (return_status == DDI_SUCCESS) { 8717 /* 8718 * For performance, now that the device has been tagged 8719 * as busy, and it's known to be powered up, update the 8720 * chain types to use jump tables that do not include 8721 * pm. This significantly lowers the overhead and 8722 * therefore improves performance. 8723 */ 8724 8725 mutex_exit(&un->un_pm_mutex); 8726 mutex_enter(SD_MUTEX(un)); 8727 SD_TRACE(SD_LOG_IO_PM, un, 8728 "sd_pm_entry: changing uscsi_chain_type from %d\n", 8729 un->un_uscsi_chain_type); 8730 8731 if (un->un_f_non_devbsize_supported) { 8732 un->un_buf_chain_type = 8733 SD_CHAIN_INFO_RMMEDIA_NO_PM; 8734 } else { 8735 un->un_buf_chain_type = 8736 SD_CHAIN_INFO_DISK_NO_PM; 8737 } 8738 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8739 8740 SD_TRACE(SD_LOG_IO_PM, un, 8741 " changed uscsi_chain_type to %d\n", 8742 un->un_uscsi_chain_type); 8743 mutex_exit(SD_MUTEX(un)); 8744 mutex_enter(&un->un_pm_mutex); 8745 8746 if (un->un_pm_idle_timeid == NULL) { 8747 /* 300 ms. */ 8748 un->un_pm_idle_timeid = 8749 timeout(sd_pm_idletimeout_handler, un, 8750 (drv_usectohz((clock_t)300000))); 8751 /* 8752 * Include an extra call to busy which keeps the 8753 * device busy with-respect-to the PM layer 8754 * until the timer fires, at which time it'll 8755 * get the extra idle call. 8756 */ 8757 (void) pm_busy_component(SD_DEVINFO(un), 0); 8758 } 8759 } 8760 } 8761 un->un_pm_busy = FALSE; 8762 /* Next... */ 8763 cv_signal(&un->un_pm_busy_cv); 8764 8765 un->un_pm_count++; 8766 8767 SD_TRACE(SD_LOG_IO_PM, un, 8768 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 8769 8770 mutex_exit(&un->un_pm_mutex); 8771 8772 return (return_status); 8773 } 8774 8775 8776 /* 8777 * Function: sd_pm_exit 8778 * 8779 * Description: Called at the completion of a command to manage busy 8780 * status for the device. If the device becomes idle the 8781 * PM framework is notified. 8782 * 8783 * Context: Kernel thread context 8784 */ 8785 8786 static void 8787 sd_pm_exit(struct sd_lun *un) 8788 { 8789 ASSERT(!mutex_owned(SD_MUTEX(un))); 8790 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8791 8792 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 8793 8794 /* 8795 * After attach the following flag is only read, so don't 8796 * take the penalty of acquiring a mutex for it. 8797 */ 8798 if (un->un_f_pm_is_enabled == TRUE) { 8799 8800 mutex_enter(&un->un_pm_mutex); 8801 un->un_pm_count--; 8802 8803 SD_TRACE(SD_LOG_IO_PM, un, 8804 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 8805 8806 ASSERT(un->un_pm_count >= 0); 8807 if (un->un_pm_count == 0) { 8808 mutex_exit(&un->un_pm_mutex); 8809 8810 SD_TRACE(SD_LOG_IO_PM, un, 8811 "sd_pm_exit: idle component\n"); 8812 8813 (void) pm_idle_component(SD_DEVINFO(un), 0); 8814 8815 } else { 8816 mutex_exit(&un->un_pm_mutex); 8817 } 8818 } 8819 8820 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 8821 } 8822 8823 8824 /* 8825 * Function: sdopen 8826 * 8827 * Description: Driver's open(9e) entry point function. 8828 * 8829 * Arguments: dev_i - pointer to device number 8830 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 8831 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 8832 * cred_p - user credential pointer 8833 * 8834 * Return Code: EINVAL 8835 * ENXIO 8836 * EIO 8837 * EROFS 8838 * EBUSY 8839 * 8840 * Context: Kernel thread context 8841 */ 8842 /* ARGSUSED */ 8843 static int 8844 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 8845 { 8846 struct sd_lun *un; 8847 int nodelay; 8848 int part; 8849 uint64_t partmask; 8850 int instance; 8851 dev_t dev; 8852 int rval = EIO; 8853 diskaddr_t nblks = 0; 8854 8855 /* Validate the open type */ 8856 if (otyp >= OTYPCNT) { 8857 return (EINVAL); 8858 } 8859 8860 dev = *dev_p; 8861 instance = SDUNIT(dev); 8862 mutex_enter(&sd_detach_mutex); 8863 8864 /* 8865 * Fail the open if there is no softstate for the instance, or 8866 * if another thread somewhere is trying to detach the instance. 8867 */ 8868 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 8869 (un->un_detach_count != 0)) { 8870 mutex_exit(&sd_detach_mutex); 8871 /* 8872 * The probe cache only needs to be cleared when open (9e) fails 8873 * with ENXIO (4238046). 8874 */ 8875 /* 8876 * un-conditionally clearing probe cache is ok with 8877 * separate sd/ssd binaries 8878 * x86 platform can be an issue with both parallel 8879 * and fibre in 1 binary 8880 */ 8881 sd_scsi_clear_probe_cache(); 8882 return (ENXIO); 8883 } 8884 8885 /* 8886 * The un_layer_count is to prevent another thread in specfs from 8887 * trying to detach the instance, which can happen when we are 8888 * called from a higher-layer driver instead of thru specfs. 8889 * This will not be needed when DDI provides a layered driver 8890 * interface that allows specfs to know that an instance is in 8891 * use by a layered driver & should not be detached. 8892 * 8893 * Note: the semantics for layered driver opens are exactly one 8894 * close for every open. 8895 */ 8896 if (otyp == OTYP_LYR) { 8897 un->un_layer_count++; 8898 } 8899 8900 /* 8901 * Keep a count of the current # of opens in progress. This is because 8902 * some layered drivers try to call us as a regular open. This can 8903 * cause problems that we cannot prevent, however by keeping this count 8904 * we can at least keep our open and detach routines from racing against 8905 * each other under such conditions. 8906 */ 8907 un->un_opens_in_progress++; 8908 mutex_exit(&sd_detach_mutex); 8909 8910 nodelay = (flag & (FNDELAY | FNONBLOCK)); 8911 part = SDPART(dev); 8912 partmask = 1 << part; 8913 8914 /* 8915 * We use a semaphore here in order to serialize 8916 * open and close requests on the device. 8917 */ 8918 sema_p(&un->un_semoclose); 8919 8920 mutex_enter(SD_MUTEX(un)); 8921 8922 /* 8923 * All device accesses go thru sdstrategy() where we check 8924 * on suspend status but there could be a scsi_poll command, 8925 * which bypasses sdstrategy(), so we need to check pm 8926 * status. 8927 */ 8928 8929 if (!nodelay) { 8930 while ((un->un_state == SD_STATE_SUSPENDED) || 8931 (un->un_state == SD_STATE_PM_CHANGING)) { 8932 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 8933 } 8934 8935 mutex_exit(SD_MUTEX(un)); 8936 if (sd_pm_entry(un) != DDI_SUCCESS) { 8937 rval = EIO; 8938 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 8939 "sdopen: sd_pm_entry failed\n"); 8940 goto open_failed_with_pm; 8941 } 8942 mutex_enter(SD_MUTEX(un)); 8943 } 8944 8945 /* check for previous exclusive open */ 8946 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 8947 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 8948 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 8949 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 8950 8951 if (un->un_exclopen & (partmask)) { 8952 goto excl_open_fail; 8953 } 8954 8955 if (flag & FEXCL) { 8956 int i; 8957 if (un->un_ocmap.lyropen[part]) { 8958 goto excl_open_fail; 8959 } 8960 for (i = 0; i < (OTYPCNT - 1); i++) { 8961 if (un->un_ocmap.regopen[i] & (partmask)) { 8962 goto excl_open_fail; 8963 } 8964 } 8965 } 8966 8967 /* 8968 * Check the write permission if this is a removable media device, 8969 * NDELAY has not been set, and writable permission is requested. 8970 * 8971 * Note: If NDELAY was set and this is write-protected media the WRITE 8972 * attempt will fail with EIO as part of the I/O processing. This is a 8973 * more permissive implementation that allows the open to succeed and 8974 * WRITE attempts to fail when appropriate. 8975 */ 8976 if (un->un_f_chk_wp_open) { 8977 if ((flag & FWRITE) && (!nodelay)) { 8978 mutex_exit(SD_MUTEX(un)); 8979 /* 8980 * Defer the check for write permission on writable 8981 * DVD drive till sdstrategy and will not fail open even 8982 * if FWRITE is set as the device can be writable 8983 * depending upon the media and the media can change 8984 * after the call to open(). 8985 */ 8986 if (un->un_f_dvdram_writable_device == FALSE) { 8987 if (ISCD(un) || sr_check_wp(dev)) { 8988 rval = EROFS; 8989 mutex_enter(SD_MUTEX(un)); 8990 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 8991 "write to cd or write protected media\n"); 8992 goto open_fail; 8993 } 8994 } 8995 mutex_enter(SD_MUTEX(un)); 8996 } 8997 } 8998 8999 /* 9000 * If opening in NDELAY/NONBLOCK mode, just return. 9001 * Check if disk is ready and has a valid geometry later. 9002 */ 9003 if (!nodelay) { 9004 mutex_exit(SD_MUTEX(un)); 9005 rval = sd_ready_and_valid(un); 9006 mutex_enter(SD_MUTEX(un)); 9007 /* 9008 * Fail if device is not ready or if the number of disk 9009 * blocks is zero or negative for non CD devices. 9010 */ 9011 9012 nblks = 0; 9013 9014 if (rval == SD_READY_VALID && (!ISCD(un))) { 9015 /* if cmlb_partinfo fails, nblks remains 0 */ 9016 mutex_exit(SD_MUTEX(un)); 9017 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 9018 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 9019 mutex_enter(SD_MUTEX(un)); 9020 } 9021 9022 if ((rval != SD_READY_VALID) || 9023 (!ISCD(un) && nblks <= 0)) { 9024 rval = un->un_f_has_removable_media ? ENXIO : EIO; 9025 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9026 "device not ready or invalid disk block value\n"); 9027 goto open_fail; 9028 } 9029 #if defined(__i386) || defined(__amd64) 9030 } else { 9031 uchar_t *cp; 9032 /* 9033 * x86 requires special nodelay handling, so that p0 is 9034 * always defined and accessible. 9035 * Invalidate geometry only if device is not already open. 9036 */ 9037 cp = &un->un_ocmap.chkd[0]; 9038 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9039 if (*cp != (uchar_t)0) { 9040 break; 9041 } 9042 cp++; 9043 } 9044 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9045 mutex_exit(SD_MUTEX(un)); 9046 cmlb_invalidate(un->un_cmlbhandle, 9047 (void *)SD_PATH_DIRECT); 9048 mutex_enter(SD_MUTEX(un)); 9049 } 9050 9051 #endif 9052 } 9053 9054 if (otyp == OTYP_LYR) { 9055 un->un_ocmap.lyropen[part]++; 9056 } else { 9057 un->un_ocmap.regopen[otyp] |= partmask; 9058 } 9059 9060 /* Set up open and exclusive open flags */ 9061 if (flag & FEXCL) { 9062 un->un_exclopen |= (partmask); 9063 } 9064 9065 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9066 "open of part %d type %d\n", part, otyp); 9067 9068 mutex_exit(SD_MUTEX(un)); 9069 if (!nodelay) { 9070 sd_pm_exit(un); 9071 } 9072 9073 sema_v(&un->un_semoclose); 9074 9075 mutex_enter(&sd_detach_mutex); 9076 un->un_opens_in_progress--; 9077 mutex_exit(&sd_detach_mutex); 9078 9079 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 9080 return (DDI_SUCCESS); 9081 9082 excl_open_fail: 9083 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 9084 rval = EBUSY; 9085 9086 open_fail: 9087 mutex_exit(SD_MUTEX(un)); 9088 9089 /* 9090 * On a failed open we must exit the pm management. 9091 */ 9092 if (!nodelay) { 9093 sd_pm_exit(un); 9094 } 9095 open_failed_with_pm: 9096 sema_v(&un->un_semoclose); 9097 9098 mutex_enter(&sd_detach_mutex); 9099 un->un_opens_in_progress--; 9100 if (otyp == OTYP_LYR) { 9101 un->un_layer_count--; 9102 } 9103 mutex_exit(&sd_detach_mutex); 9104 9105 return (rval); 9106 } 9107 9108 9109 /* 9110 * Function: sdclose 9111 * 9112 * Description: Driver's close(9e) entry point function. 9113 * 9114 * Arguments: dev - device number 9115 * flag - file status flag, informational only 9116 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9117 * cred_p - user credential pointer 9118 * 9119 * Return Code: ENXIO 9120 * 9121 * Context: Kernel thread context 9122 */ 9123 /* ARGSUSED */ 9124 static int 9125 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 9126 { 9127 struct sd_lun *un; 9128 uchar_t *cp; 9129 int part; 9130 int nodelay; 9131 int rval = 0; 9132 9133 /* Validate the open type */ 9134 if (otyp >= OTYPCNT) { 9135 return (ENXIO); 9136 } 9137 9138 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9139 return (ENXIO); 9140 } 9141 9142 part = SDPART(dev); 9143 nodelay = flag & (FNDELAY | FNONBLOCK); 9144 9145 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9146 "sdclose: close of part %d type %d\n", part, otyp); 9147 9148 /* 9149 * We use a semaphore here in order to serialize 9150 * open and close requests on the device. 9151 */ 9152 sema_p(&un->un_semoclose); 9153 9154 mutex_enter(SD_MUTEX(un)); 9155 9156 /* Don't proceed if power is being changed. */ 9157 while (un->un_state == SD_STATE_PM_CHANGING) { 9158 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9159 } 9160 9161 if (un->un_exclopen & (1 << part)) { 9162 un->un_exclopen &= ~(1 << part); 9163 } 9164 9165 /* Update the open partition map */ 9166 if (otyp == OTYP_LYR) { 9167 un->un_ocmap.lyropen[part] -= 1; 9168 } else { 9169 un->un_ocmap.regopen[otyp] &= ~(1 << part); 9170 } 9171 9172 cp = &un->un_ocmap.chkd[0]; 9173 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9174 if (*cp != NULL) { 9175 break; 9176 } 9177 cp++; 9178 } 9179 9180 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9181 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 9182 9183 /* 9184 * We avoid persistance upon the last close, and set 9185 * the throttle back to the maximum. 9186 */ 9187 un->un_throttle = un->un_saved_throttle; 9188 9189 if (un->un_state == SD_STATE_OFFLINE) { 9190 if (un->un_f_is_fibre == FALSE) { 9191 scsi_log(SD_DEVINFO(un), sd_label, 9192 CE_WARN, "offline\n"); 9193 } 9194 mutex_exit(SD_MUTEX(un)); 9195 cmlb_invalidate(un->un_cmlbhandle, 9196 (void *)SD_PATH_DIRECT); 9197 mutex_enter(SD_MUTEX(un)); 9198 9199 } else { 9200 /* 9201 * Flush any outstanding writes in NVRAM cache. 9202 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 9203 * cmd, it may not work for non-Pluto devices. 9204 * SYNCHRONIZE CACHE is not required for removables, 9205 * except DVD-RAM drives. 9206 * 9207 * Also note: because SYNCHRONIZE CACHE is currently 9208 * the only command issued here that requires the 9209 * drive be powered up, only do the power up before 9210 * sending the Sync Cache command. If additional 9211 * commands are added which require a powered up 9212 * drive, the following sequence may have to change. 9213 * 9214 * And finally, note that parallel SCSI on SPARC 9215 * only issues a Sync Cache to DVD-RAM, a newly 9216 * supported device. 9217 */ 9218 #if defined(__i386) || defined(__amd64) 9219 if (un->un_f_sync_cache_supported || 9220 un->un_f_dvdram_writable_device == TRUE) { 9221 #else 9222 if (un->un_f_dvdram_writable_device == TRUE) { 9223 #endif 9224 mutex_exit(SD_MUTEX(un)); 9225 if (sd_pm_entry(un) == DDI_SUCCESS) { 9226 rval = 9227 sd_send_scsi_SYNCHRONIZE_CACHE(un, 9228 NULL); 9229 /* ignore error if not supported */ 9230 if (rval == ENOTSUP) { 9231 rval = 0; 9232 } else if (rval != 0) { 9233 rval = EIO; 9234 } 9235 sd_pm_exit(un); 9236 } else { 9237 rval = EIO; 9238 } 9239 mutex_enter(SD_MUTEX(un)); 9240 } 9241 9242 /* 9243 * For devices which supports DOOR_LOCK, send an ALLOW 9244 * MEDIA REMOVAL command, but don't get upset if it 9245 * fails. We need to raise the power of the drive before 9246 * we can call sd_send_scsi_DOORLOCK() 9247 */ 9248 if (un->un_f_doorlock_supported) { 9249 mutex_exit(SD_MUTEX(un)); 9250 if (sd_pm_entry(un) == DDI_SUCCESS) { 9251 rval = sd_send_scsi_DOORLOCK(un, 9252 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 9253 9254 sd_pm_exit(un); 9255 if (ISCD(un) && (rval != 0) && 9256 (nodelay != 0)) { 9257 rval = ENXIO; 9258 } 9259 } else { 9260 rval = EIO; 9261 } 9262 mutex_enter(SD_MUTEX(un)); 9263 } 9264 9265 /* 9266 * If a device has removable media, invalidate all 9267 * parameters related to media, such as geometry, 9268 * blocksize, and blockcount. 9269 */ 9270 if (un->un_f_has_removable_media) { 9271 sr_ejected(un); 9272 } 9273 9274 /* 9275 * Destroy the cache (if it exists) which was 9276 * allocated for the write maps since this is 9277 * the last close for this media. 9278 */ 9279 if (un->un_wm_cache) { 9280 /* 9281 * Check if there are pending commands. 9282 * and if there are give a warning and 9283 * do not destroy the cache. 9284 */ 9285 if (un->un_ncmds_in_driver > 0) { 9286 scsi_log(SD_DEVINFO(un), 9287 sd_label, CE_WARN, 9288 "Unable to clean up memory " 9289 "because of pending I/O\n"); 9290 } else { 9291 kmem_cache_destroy( 9292 un->un_wm_cache); 9293 un->un_wm_cache = NULL; 9294 } 9295 } 9296 } 9297 } 9298 9299 mutex_exit(SD_MUTEX(un)); 9300 sema_v(&un->un_semoclose); 9301 9302 if (otyp == OTYP_LYR) { 9303 mutex_enter(&sd_detach_mutex); 9304 /* 9305 * The detach routine may run when the layer count 9306 * drops to zero. 9307 */ 9308 un->un_layer_count--; 9309 mutex_exit(&sd_detach_mutex); 9310 } 9311 9312 return (rval); 9313 } 9314 9315 9316 /* 9317 * Function: sd_ready_and_valid 9318 * 9319 * Description: Test if device is ready and has a valid geometry. 9320 * 9321 * Arguments: dev - device number 9322 * un - driver soft state (unit) structure 9323 * 9324 * Return Code: SD_READY_VALID ready and valid label 9325 * SD_NOT_READY_VALID not ready, no label 9326 * SD_RESERVED_BY_OTHERS reservation conflict 9327 * 9328 * Context: Never called at interrupt context. 9329 */ 9330 9331 static int 9332 sd_ready_and_valid(struct sd_lun *un) 9333 { 9334 struct sd_errstats *stp; 9335 uint64_t capacity; 9336 uint_t lbasize; 9337 int rval = SD_READY_VALID; 9338 char name_str[48]; 9339 int is_valid; 9340 9341 ASSERT(un != NULL); 9342 ASSERT(!mutex_owned(SD_MUTEX(un))); 9343 9344 mutex_enter(SD_MUTEX(un)); 9345 /* 9346 * If a device has removable media, we must check if media is 9347 * ready when checking if this device is ready and valid. 9348 */ 9349 if (un->un_f_has_removable_media) { 9350 mutex_exit(SD_MUTEX(un)); 9351 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 9352 rval = SD_NOT_READY_VALID; 9353 mutex_enter(SD_MUTEX(un)); 9354 goto done; 9355 } 9356 9357 is_valid = SD_IS_VALID_LABEL(un); 9358 mutex_enter(SD_MUTEX(un)); 9359 if (!is_valid || 9360 (un->un_f_blockcount_is_valid == FALSE) || 9361 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 9362 9363 /* capacity has to be read every open. */ 9364 mutex_exit(SD_MUTEX(un)); 9365 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 9366 &lbasize, SD_PATH_DIRECT) != 0) { 9367 cmlb_invalidate(un->un_cmlbhandle, 9368 (void *)SD_PATH_DIRECT); 9369 mutex_enter(SD_MUTEX(un)); 9370 rval = SD_NOT_READY_VALID; 9371 goto done; 9372 } else { 9373 mutex_enter(SD_MUTEX(un)); 9374 sd_update_block_info(un, lbasize, capacity); 9375 } 9376 } 9377 9378 /* 9379 * Check if the media in the device is writable or not. 9380 */ 9381 if (!is_valid && ISCD(un)) { 9382 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 9383 } 9384 9385 } else { 9386 /* 9387 * Do a test unit ready to clear any unit attention from non-cd 9388 * devices. 9389 */ 9390 mutex_exit(SD_MUTEX(un)); 9391 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9392 mutex_enter(SD_MUTEX(un)); 9393 } 9394 9395 9396 /* 9397 * If this is a non 512 block device, allocate space for 9398 * the wmap cache. This is being done here since every time 9399 * a media is changed this routine will be called and the 9400 * block size is a function of media rather than device. 9401 */ 9402 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 9403 if (!(un->un_wm_cache)) { 9404 (void) snprintf(name_str, sizeof (name_str), 9405 "%s%d_cache", 9406 ddi_driver_name(SD_DEVINFO(un)), 9407 ddi_get_instance(SD_DEVINFO(un))); 9408 un->un_wm_cache = kmem_cache_create( 9409 name_str, sizeof (struct sd_w_map), 9410 8, sd_wm_cache_constructor, 9411 sd_wm_cache_destructor, NULL, 9412 (void *)un, NULL, 0); 9413 if (!(un->un_wm_cache)) { 9414 rval = ENOMEM; 9415 goto done; 9416 } 9417 } 9418 } 9419 9420 if (un->un_state == SD_STATE_NORMAL) { 9421 /* 9422 * If the target is not yet ready here (defined by a TUR 9423 * failure), invalidate the geometry and print an 'offline' 9424 * message. This is a legacy message, as the state of the 9425 * target is not actually changed to SD_STATE_OFFLINE. 9426 * 9427 * If the TUR fails for EACCES (Reservation Conflict), 9428 * SD_RESERVED_BY_OTHERS will be returned to indicate 9429 * reservation conflict. If the TUR fails for other 9430 * reasons, SD_NOT_READY_VALID will be returned. 9431 */ 9432 int err; 9433 9434 mutex_exit(SD_MUTEX(un)); 9435 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 9436 mutex_enter(SD_MUTEX(un)); 9437 9438 if (err != 0) { 9439 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9440 "offline or reservation conflict\n"); 9441 mutex_exit(SD_MUTEX(un)); 9442 cmlb_invalidate(un->un_cmlbhandle, 9443 (void *)SD_PATH_DIRECT); 9444 mutex_enter(SD_MUTEX(un)); 9445 if (err == EACCES) { 9446 rval = SD_RESERVED_BY_OTHERS; 9447 } else { 9448 rval = SD_NOT_READY_VALID; 9449 } 9450 goto done; 9451 } 9452 } 9453 9454 if (un->un_f_format_in_progress == FALSE) { 9455 mutex_exit(SD_MUTEX(un)); 9456 if (cmlb_validate(un->un_cmlbhandle, 0, 9457 (void *)SD_PATH_DIRECT) != 0) { 9458 rval = SD_NOT_READY_VALID; 9459 mutex_enter(SD_MUTEX(un)); 9460 goto done; 9461 } 9462 if (un->un_f_pkstats_enabled) { 9463 sd_set_pstats(un); 9464 SD_TRACE(SD_LOG_IO_PARTITION, un, 9465 "sd_ready_and_valid: un:0x%p pstats created and " 9466 "set\n", un); 9467 } 9468 mutex_enter(SD_MUTEX(un)); 9469 } 9470 9471 /* 9472 * If this device supports DOOR_LOCK command, try and send 9473 * this command to PREVENT MEDIA REMOVAL, but don't get upset 9474 * if it fails. For a CD, however, it is an error 9475 */ 9476 if (un->un_f_doorlock_supported) { 9477 mutex_exit(SD_MUTEX(un)); 9478 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 9479 SD_PATH_DIRECT) != 0) && ISCD(un)) { 9480 rval = SD_NOT_READY_VALID; 9481 mutex_enter(SD_MUTEX(un)); 9482 goto done; 9483 } 9484 mutex_enter(SD_MUTEX(un)); 9485 } 9486 9487 /* The state has changed, inform the media watch routines */ 9488 un->un_mediastate = DKIO_INSERTED; 9489 cv_broadcast(&un->un_state_cv); 9490 rval = SD_READY_VALID; 9491 9492 done: 9493 9494 /* 9495 * Initialize the capacity kstat value, if no media previously 9496 * (capacity kstat is 0) and a media has been inserted 9497 * (un_blockcount > 0). 9498 */ 9499 if (un->un_errstats != NULL) { 9500 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9501 if ((stp->sd_capacity.value.ui64 == 0) && 9502 (un->un_f_blockcount_is_valid == TRUE)) { 9503 stp->sd_capacity.value.ui64 = 9504 (uint64_t)((uint64_t)un->un_blockcount * 9505 un->un_sys_blocksize); 9506 } 9507 } 9508 9509 mutex_exit(SD_MUTEX(un)); 9510 return (rval); 9511 } 9512 9513 9514 /* 9515 * Function: sdmin 9516 * 9517 * Description: Routine to limit the size of a data transfer. Used in 9518 * conjunction with physio(9F). 9519 * 9520 * Arguments: bp - pointer to the indicated buf(9S) struct. 9521 * 9522 * Context: Kernel thread context. 9523 */ 9524 9525 static void 9526 sdmin(struct buf *bp) 9527 { 9528 struct sd_lun *un; 9529 int instance; 9530 9531 instance = SDUNIT(bp->b_edev); 9532 9533 un = ddi_get_soft_state(sd_state, instance); 9534 ASSERT(un != NULL); 9535 9536 if (bp->b_bcount > un->un_max_xfer_size) { 9537 bp->b_bcount = un->un_max_xfer_size; 9538 } 9539 } 9540 9541 9542 /* 9543 * Function: sdread 9544 * 9545 * Description: Driver's read(9e) entry point function. 9546 * 9547 * Arguments: dev - device number 9548 * uio - structure pointer describing where data is to be stored 9549 * in user's space 9550 * cred_p - user credential pointer 9551 * 9552 * Return Code: ENXIO 9553 * EIO 9554 * EINVAL 9555 * value returned by physio 9556 * 9557 * Context: Kernel thread context. 9558 */ 9559 /* ARGSUSED */ 9560 static int 9561 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 9562 { 9563 struct sd_lun *un = NULL; 9564 int secmask; 9565 int err; 9566 9567 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9568 return (ENXIO); 9569 } 9570 9571 ASSERT(!mutex_owned(SD_MUTEX(un))); 9572 9573 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9574 mutex_enter(SD_MUTEX(un)); 9575 /* 9576 * Because the call to sd_ready_and_valid will issue I/O we 9577 * must wait here if either the device is suspended or 9578 * if it's power level is changing. 9579 */ 9580 while ((un->un_state == SD_STATE_SUSPENDED) || 9581 (un->un_state == SD_STATE_PM_CHANGING)) { 9582 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9583 } 9584 un->un_ncmds_in_driver++; 9585 mutex_exit(SD_MUTEX(un)); 9586 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9587 mutex_enter(SD_MUTEX(un)); 9588 un->un_ncmds_in_driver--; 9589 ASSERT(un->un_ncmds_in_driver >= 0); 9590 mutex_exit(SD_MUTEX(un)); 9591 return (EIO); 9592 } 9593 mutex_enter(SD_MUTEX(un)); 9594 un->un_ncmds_in_driver--; 9595 ASSERT(un->un_ncmds_in_driver >= 0); 9596 mutex_exit(SD_MUTEX(un)); 9597 } 9598 9599 /* 9600 * Read requests are restricted to multiples of the system block size. 9601 */ 9602 secmask = un->un_sys_blocksize - 1; 9603 9604 if (uio->uio_loffset & ((offset_t)(secmask))) { 9605 SD_ERROR(SD_LOG_READ_WRITE, un, 9606 "sdread: file offset not modulo %d\n", 9607 un->un_sys_blocksize); 9608 err = EINVAL; 9609 } else if (uio->uio_iov->iov_len & (secmask)) { 9610 SD_ERROR(SD_LOG_READ_WRITE, un, 9611 "sdread: transfer length not modulo %d\n", 9612 un->un_sys_blocksize); 9613 err = EINVAL; 9614 } else { 9615 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 9616 } 9617 return (err); 9618 } 9619 9620 9621 /* 9622 * Function: sdwrite 9623 * 9624 * Description: Driver's write(9e) entry point function. 9625 * 9626 * Arguments: dev - device number 9627 * uio - structure pointer describing where data is stored in 9628 * user's space 9629 * cred_p - user credential pointer 9630 * 9631 * Return Code: ENXIO 9632 * EIO 9633 * EINVAL 9634 * value returned by physio 9635 * 9636 * Context: Kernel thread context. 9637 */ 9638 /* ARGSUSED */ 9639 static int 9640 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 9641 { 9642 struct sd_lun *un = NULL; 9643 int secmask; 9644 int err; 9645 9646 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9647 return (ENXIO); 9648 } 9649 9650 ASSERT(!mutex_owned(SD_MUTEX(un))); 9651 9652 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9653 mutex_enter(SD_MUTEX(un)); 9654 /* 9655 * Because the call to sd_ready_and_valid will issue I/O we 9656 * must wait here if either the device is suspended or 9657 * if it's power level is changing. 9658 */ 9659 while ((un->un_state == SD_STATE_SUSPENDED) || 9660 (un->un_state == SD_STATE_PM_CHANGING)) { 9661 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9662 } 9663 un->un_ncmds_in_driver++; 9664 mutex_exit(SD_MUTEX(un)); 9665 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9666 mutex_enter(SD_MUTEX(un)); 9667 un->un_ncmds_in_driver--; 9668 ASSERT(un->un_ncmds_in_driver >= 0); 9669 mutex_exit(SD_MUTEX(un)); 9670 return (EIO); 9671 } 9672 mutex_enter(SD_MUTEX(un)); 9673 un->un_ncmds_in_driver--; 9674 ASSERT(un->un_ncmds_in_driver >= 0); 9675 mutex_exit(SD_MUTEX(un)); 9676 } 9677 9678 /* 9679 * Write requests are restricted to multiples of the system block size. 9680 */ 9681 secmask = un->un_sys_blocksize - 1; 9682 9683 if (uio->uio_loffset & ((offset_t)(secmask))) { 9684 SD_ERROR(SD_LOG_READ_WRITE, un, 9685 "sdwrite: file offset not modulo %d\n", 9686 un->un_sys_blocksize); 9687 err = EINVAL; 9688 } else if (uio->uio_iov->iov_len & (secmask)) { 9689 SD_ERROR(SD_LOG_READ_WRITE, un, 9690 "sdwrite: transfer length not modulo %d\n", 9691 un->un_sys_blocksize); 9692 err = EINVAL; 9693 } else { 9694 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 9695 } 9696 return (err); 9697 } 9698 9699 9700 /* 9701 * Function: sdaread 9702 * 9703 * Description: Driver's aread(9e) entry point function. 9704 * 9705 * Arguments: dev - device number 9706 * aio - structure pointer describing where data is to be stored 9707 * cred_p - user credential pointer 9708 * 9709 * Return Code: ENXIO 9710 * EIO 9711 * EINVAL 9712 * value returned by aphysio 9713 * 9714 * Context: Kernel thread context. 9715 */ 9716 /* ARGSUSED */ 9717 static int 9718 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9719 { 9720 struct sd_lun *un = NULL; 9721 struct uio *uio = aio->aio_uio; 9722 int secmask; 9723 int err; 9724 9725 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9726 return (ENXIO); 9727 } 9728 9729 ASSERT(!mutex_owned(SD_MUTEX(un))); 9730 9731 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9732 mutex_enter(SD_MUTEX(un)); 9733 /* 9734 * Because the call to sd_ready_and_valid will issue I/O we 9735 * must wait here if either the device is suspended or 9736 * if it's power level is changing. 9737 */ 9738 while ((un->un_state == SD_STATE_SUSPENDED) || 9739 (un->un_state == SD_STATE_PM_CHANGING)) { 9740 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9741 } 9742 un->un_ncmds_in_driver++; 9743 mutex_exit(SD_MUTEX(un)); 9744 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9745 mutex_enter(SD_MUTEX(un)); 9746 un->un_ncmds_in_driver--; 9747 ASSERT(un->un_ncmds_in_driver >= 0); 9748 mutex_exit(SD_MUTEX(un)); 9749 return (EIO); 9750 } 9751 mutex_enter(SD_MUTEX(un)); 9752 un->un_ncmds_in_driver--; 9753 ASSERT(un->un_ncmds_in_driver >= 0); 9754 mutex_exit(SD_MUTEX(un)); 9755 } 9756 9757 /* 9758 * Read requests are restricted to multiples of the system block size. 9759 */ 9760 secmask = un->un_sys_blocksize - 1; 9761 9762 if (uio->uio_loffset & ((offset_t)(secmask))) { 9763 SD_ERROR(SD_LOG_READ_WRITE, un, 9764 "sdaread: file offset not modulo %d\n", 9765 un->un_sys_blocksize); 9766 err = EINVAL; 9767 } else if (uio->uio_iov->iov_len & (secmask)) { 9768 SD_ERROR(SD_LOG_READ_WRITE, un, 9769 "sdaread: transfer length not modulo %d\n", 9770 un->un_sys_blocksize); 9771 err = EINVAL; 9772 } else { 9773 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 9774 } 9775 return (err); 9776 } 9777 9778 9779 /* 9780 * Function: sdawrite 9781 * 9782 * Description: Driver's awrite(9e) entry point function. 9783 * 9784 * Arguments: dev - device number 9785 * aio - structure pointer describing where data is stored 9786 * cred_p - user credential pointer 9787 * 9788 * Return Code: ENXIO 9789 * EIO 9790 * EINVAL 9791 * value returned by aphysio 9792 * 9793 * Context: Kernel thread context. 9794 */ 9795 /* ARGSUSED */ 9796 static int 9797 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9798 { 9799 struct sd_lun *un = NULL; 9800 struct uio *uio = aio->aio_uio; 9801 int secmask; 9802 int err; 9803 9804 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9805 return (ENXIO); 9806 } 9807 9808 ASSERT(!mutex_owned(SD_MUTEX(un))); 9809 9810 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9811 mutex_enter(SD_MUTEX(un)); 9812 /* 9813 * Because the call to sd_ready_and_valid will issue I/O we 9814 * must wait here if either the device is suspended or 9815 * if it's power level is changing. 9816 */ 9817 while ((un->un_state == SD_STATE_SUSPENDED) || 9818 (un->un_state == SD_STATE_PM_CHANGING)) { 9819 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9820 } 9821 un->un_ncmds_in_driver++; 9822 mutex_exit(SD_MUTEX(un)); 9823 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9824 mutex_enter(SD_MUTEX(un)); 9825 un->un_ncmds_in_driver--; 9826 ASSERT(un->un_ncmds_in_driver >= 0); 9827 mutex_exit(SD_MUTEX(un)); 9828 return (EIO); 9829 } 9830 mutex_enter(SD_MUTEX(un)); 9831 un->un_ncmds_in_driver--; 9832 ASSERT(un->un_ncmds_in_driver >= 0); 9833 mutex_exit(SD_MUTEX(un)); 9834 } 9835 9836 /* 9837 * Write requests are restricted to multiples of the system block size. 9838 */ 9839 secmask = un->un_sys_blocksize - 1; 9840 9841 if (uio->uio_loffset & ((offset_t)(secmask))) { 9842 SD_ERROR(SD_LOG_READ_WRITE, un, 9843 "sdawrite: file offset not modulo %d\n", 9844 un->un_sys_blocksize); 9845 err = EINVAL; 9846 } else if (uio->uio_iov->iov_len & (secmask)) { 9847 SD_ERROR(SD_LOG_READ_WRITE, un, 9848 "sdawrite: transfer length not modulo %d\n", 9849 un->un_sys_blocksize); 9850 err = EINVAL; 9851 } else { 9852 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 9853 } 9854 return (err); 9855 } 9856 9857 9858 9859 9860 9861 /* 9862 * Driver IO processing follows the following sequence: 9863 * 9864 * sdioctl(9E) sdstrategy(9E) biodone(9F) 9865 * | | ^ 9866 * v v | 9867 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 9868 * | | | | 9869 * v | | | 9870 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 9871 * | | ^ ^ 9872 * v v | | 9873 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 9874 * | | | | 9875 * +---+ | +------------+ +-------+ 9876 * | | | | 9877 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 9878 * | v | | 9879 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 9880 * | | ^ | 9881 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 9882 * | v | | 9883 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 9884 * | | ^ | 9885 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 9886 * | v | | 9887 * | sd_checksum_iostart() sd_checksum_iodone() | 9888 * | | ^ | 9889 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 9890 * | v | | 9891 * | sd_pm_iostart() sd_pm_iodone() | 9892 * | | ^ | 9893 * | | | | 9894 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 9895 * | ^ 9896 * v | 9897 * sd_core_iostart() | 9898 * | | 9899 * | +------>(*destroypkt)() 9900 * +-> sd_start_cmds() <-+ | | 9901 * | | | v 9902 * | | | scsi_destroy_pkt(9F) 9903 * | | | 9904 * +->(*initpkt)() +- sdintr() 9905 * | | | | 9906 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 9907 * | +-> scsi_setup_cdb(9F) | 9908 * | | 9909 * +--> scsi_transport(9F) | 9910 * | | 9911 * +----> SCSA ---->+ 9912 * 9913 * 9914 * This code is based upon the following presumptions: 9915 * 9916 * - iostart and iodone functions operate on buf(9S) structures. These 9917 * functions perform the necessary operations on the buf(9S) and pass 9918 * them along to the next function in the chain by using the macros 9919 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 9920 * (for iodone side functions). 9921 * 9922 * - The iostart side functions may sleep. The iodone side functions 9923 * are called under interrupt context and may NOT sleep. Therefore 9924 * iodone side functions also may not call iostart side functions. 9925 * (NOTE: iostart side functions should NOT sleep for memory, as 9926 * this could result in deadlock.) 9927 * 9928 * - An iostart side function may call its corresponding iodone side 9929 * function directly (if necessary). 9930 * 9931 * - In the event of an error, an iostart side function can return a buf(9S) 9932 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 9933 * b_error in the usual way of course). 9934 * 9935 * - The taskq mechanism may be used by the iodone side functions to dispatch 9936 * requests to the iostart side functions. The iostart side functions in 9937 * this case would be called under the context of a taskq thread, so it's 9938 * OK for them to block/sleep/spin in this case. 9939 * 9940 * - iostart side functions may allocate "shadow" buf(9S) structs and 9941 * pass them along to the next function in the chain. The corresponding 9942 * iodone side functions must coalesce the "shadow" bufs and return 9943 * the "original" buf to the next higher layer. 9944 * 9945 * - The b_private field of the buf(9S) struct holds a pointer to 9946 * an sd_xbuf struct, which contains information needed to 9947 * construct the scsi_pkt for the command. 9948 * 9949 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 9950 * layer must acquire & release the SD_MUTEX(un) as needed. 9951 */ 9952 9953 9954 /* 9955 * Create taskq for all targets in the system. This is created at 9956 * _init(9E) and destroyed at _fini(9E). 9957 * 9958 * Note: here we set the minalloc to a reasonably high number to ensure that 9959 * we will have an adequate supply of task entries available at interrupt time. 9960 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 9961 * sd_create_taskq(). Since we do not want to sleep for allocations at 9962 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 9963 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 9964 * requests any one instant in time. 9965 */ 9966 #define SD_TASKQ_NUMTHREADS 8 9967 #define SD_TASKQ_MINALLOC 256 9968 #define SD_TASKQ_MAXALLOC 256 9969 9970 static taskq_t *sd_tq = NULL; 9971 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 9972 9973 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 9974 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 9975 9976 /* 9977 * The following task queue is being created for the write part of 9978 * read-modify-write of non-512 block size devices. 9979 * Limit the number of threads to 1 for now. This number has been chosen 9980 * considering the fact that it applies only to dvd ram drives/MO drives 9981 * currently. Performance for which is not main criteria at this stage. 9982 * Note: It needs to be explored if we can use a single taskq in future 9983 */ 9984 #define SD_WMR_TASKQ_NUMTHREADS 1 9985 static taskq_t *sd_wmr_tq = NULL; 9986 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 9987 9988 /* 9989 * Function: sd_taskq_create 9990 * 9991 * Description: Create taskq thread(s) and preallocate task entries 9992 * 9993 * Return Code: Returns a pointer to the allocated taskq_t. 9994 * 9995 * Context: Can sleep. Requires blockable context. 9996 * 9997 * Notes: - The taskq() facility currently is NOT part of the DDI. 9998 * (definitely NOT recommeded for 3rd-party drivers!) :-) 9999 * - taskq_create() will block for memory, also it will panic 10000 * if it cannot create the requested number of threads. 10001 * - Currently taskq_create() creates threads that cannot be 10002 * swapped. 10003 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 10004 * supply of taskq entries at interrupt time (ie, so that we 10005 * do not have to sleep for memory) 10006 */ 10007 10008 static void 10009 sd_taskq_create(void) 10010 { 10011 char taskq_name[TASKQ_NAMELEN]; 10012 10013 ASSERT(sd_tq == NULL); 10014 ASSERT(sd_wmr_tq == NULL); 10015 10016 (void) snprintf(taskq_name, sizeof (taskq_name), 10017 "%s_drv_taskq", sd_label); 10018 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 10019 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10020 TASKQ_PREPOPULATE)); 10021 10022 (void) snprintf(taskq_name, sizeof (taskq_name), 10023 "%s_rmw_taskq", sd_label); 10024 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 10025 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10026 TASKQ_PREPOPULATE)); 10027 } 10028 10029 10030 /* 10031 * Function: sd_taskq_delete 10032 * 10033 * Description: Complementary cleanup routine for sd_taskq_create(). 10034 * 10035 * Context: Kernel thread context. 10036 */ 10037 10038 static void 10039 sd_taskq_delete(void) 10040 { 10041 ASSERT(sd_tq != NULL); 10042 ASSERT(sd_wmr_tq != NULL); 10043 taskq_destroy(sd_tq); 10044 taskq_destroy(sd_wmr_tq); 10045 sd_tq = NULL; 10046 sd_wmr_tq = NULL; 10047 } 10048 10049 10050 /* 10051 * Function: sdstrategy 10052 * 10053 * Description: Driver's strategy (9E) entry point function. 10054 * 10055 * Arguments: bp - pointer to buf(9S) 10056 * 10057 * Return Code: Always returns zero 10058 * 10059 * Context: Kernel thread context. 10060 */ 10061 10062 static int 10063 sdstrategy(struct buf *bp) 10064 { 10065 struct sd_lun *un; 10066 10067 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10068 if (un == NULL) { 10069 bioerror(bp, EIO); 10070 bp->b_resid = bp->b_bcount; 10071 biodone(bp); 10072 return (0); 10073 } 10074 /* As was done in the past, fail new cmds. if state is dumping. */ 10075 if (un->un_state == SD_STATE_DUMPING) { 10076 bioerror(bp, ENXIO); 10077 bp->b_resid = bp->b_bcount; 10078 biodone(bp); 10079 return (0); 10080 } 10081 10082 ASSERT(!mutex_owned(SD_MUTEX(un))); 10083 10084 /* 10085 * Commands may sneak in while we released the mutex in 10086 * DDI_SUSPEND, we should block new commands. However, old 10087 * commands that are still in the driver at this point should 10088 * still be allowed to drain. 10089 */ 10090 mutex_enter(SD_MUTEX(un)); 10091 /* 10092 * Must wait here if either the device is suspended or 10093 * if it's power level is changing. 10094 */ 10095 while ((un->un_state == SD_STATE_SUSPENDED) || 10096 (un->un_state == SD_STATE_PM_CHANGING)) { 10097 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10098 } 10099 10100 un->un_ncmds_in_driver++; 10101 10102 /* 10103 * atapi: Since we are running the CD for now in PIO mode we need to 10104 * call bp_mapin here to avoid bp_mapin called interrupt context under 10105 * the HBA's init_pkt routine. 10106 */ 10107 if (un->un_f_cfg_is_atapi == TRUE) { 10108 mutex_exit(SD_MUTEX(un)); 10109 bp_mapin(bp); 10110 mutex_enter(SD_MUTEX(un)); 10111 } 10112 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 10113 un->un_ncmds_in_driver); 10114 10115 mutex_exit(SD_MUTEX(un)); 10116 10117 /* 10118 * This will (eventually) allocate the sd_xbuf area and 10119 * call sd_xbuf_strategy(). We just want to return the 10120 * result of ddi_xbuf_qstrategy so that we have an opt- 10121 * imized tail call which saves us a stack frame. 10122 */ 10123 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 10124 } 10125 10126 10127 /* 10128 * Function: sd_xbuf_strategy 10129 * 10130 * Description: Function for initiating IO operations via the 10131 * ddi_xbuf_qstrategy() mechanism. 10132 * 10133 * Context: Kernel thread context. 10134 */ 10135 10136 static void 10137 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 10138 { 10139 struct sd_lun *un = arg; 10140 10141 ASSERT(bp != NULL); 10142 ASSERT(xp != NULL); 10143 ASSERT(un != NULL); 10144 ASSERT(!mutex_owned(SD_MUTEX(un))); 10145 10146 /* 10147 * Initialize the fields in the xbuf and save a pointer to the 10148 * xbuf in bp->b_private. 10149 */ 10150 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 10151 10152 /* Send the buf down the iostart chain */ 10153 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 10154 } 10155 10156 10157 /* 10158 * Function: sd_xbuf_init 10159 * 10160 * Description: Prepare the given sd_xbuf struct for use. 10161 * 10162 * Arguments: un - ptr to softstate 10163 * bp - ptr to associated buf(9S) 10164 * xp - ptr to associated sd_xbuf 10165 * chain_type - IO chain type to use: 10166 * SD_CHAIN_NULL 10167 * SD_CHAIN_BUFIO 10168 * SD_CHAIN_USCSI 10169 * SD_CHAIN_DIRECT 10170 * SD_CHAIN_DIRECT_PRIORITY 10171 * pktinfop - ptr to private data struct for scsi_pkt(9S) 10172 * initialization; may be NULL if none. 10173 * 10174 * Context: Kernel thread context 10175 */ 10176 10177 static void 10178 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 10179 uchar_t chain_type, void *pktinfop) 10180 { 10181 int index; 10182 10183 ASSERT(un != NULL); 10184 ASSERT(bp != NULL); 10185 ASSERT(xp != NULL); 10186 10187 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 10188 bp, chain_type); 10189 10190 xp->xb_un = un; 10191 xp->xb_pktp = NULL; 10192 xp->xb_pktinfo = pktinfop; 10193 xp->xb_private = bp->b_private; 10194 xp->xb_blkno = (daddr_t)bp->b_blkno; 10195 10196 /* 10197 * Set up the iostart and iodone chain indexes in the xbuf, based 10198 * upon the specified chain type to use. 10199 */ 10200 switch (chain_type) { 10201 case SD_CHAIN_NULL: 10202 /* 10203 * Fall thru to just use the values for the buf type, even 10204 * tho for the NULL chain these values will never be used. 10205 */ 10206 /* FALLTHRU */ 10207 case SD_CHAIN_BUFIO: 10208 index = un->un_buf_chain_type; 10209 break; 10210 case SD_CHAIN_USCSI: 10211 index = un->un_uscsi_chain_type; 10212 break; 10213 case SD_CHAIN_DIRECT: 10214 index = un->un_direct_chain_type; 10215 break; 10216 case SD_CHAIN_DIRECT_PRIORITY: 10217 index = un->un_priority_chain_type; 10218 break; 10219 default: 10220 /* We're really broken if we ever get here... */ 10221 panic("sd_xbuf_init: illegal chain type!"); 10222 /*NOTREACHED*/ 10223 } 10224 10225 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 10226 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 10227 10228 /* 10229 * It might be a bit easier to simply bzero the entire xbuf above, 10230 * but it turns out that since we init a fair number of members anyway, 10231 * we save a fair number cycles by doing explicit assignment of zero. 10232 */ 10233 xp->xb_pkt_flags = 0; 10234 xp->xb_dma_resid = 0; 10235 xp->xb_retry_count = 0; 10236 xp->xb_victim_retry_count = 0; 10237 xp->xb_ua_retry_count = 0; 10238 xp->xb_nr_retry_count = 0; 10239 xp->xb_sense_bp = NULL; 10240 xp->xb_sense_status = 0; 10241 xp->xb_sense_state = 0; 10242 xp->xb_sense_resid = 0; 10243 10244 bp->b_private = xp; 10245 bp->b_flags &= ~(B_DONE | B_ERROR); 10246 bp->b_resid = 0; 10247 bp->av_forw = NULL; 10248 bp->av_back = NULL; 10249 bioerror(bp, 0); 10250 10251 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 10252 } 10253 10254 10255 /* 10256 * Function: sd_uscsi_strategy 10257 * 10258 * Description: Wrapper for calling into the USCSI chain via physio(9F) 10259 * 10260 * Arguments: bp - buf struct ptr 10261 * 10262 * Return Code: Always returns 0 10263 * 10264 * Context: Kernel thread context 10265 */ 10266 10267 static int 10268 sd_uscsi_strategy(struct buf *bp) 10269 { 10270 struct sd_lun *un; 10271 struct sd_uscsi_info *uip; 10272 struct sd_xbuf *xp; 10273 uchar_t chain_type; 10274 10275 ASSERT(bp != NULL); 10276 10277 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10278 if (un == NULL) { 10279 bioerror(bp, EIO); 10280 bp->b_resid = bp->b_bcount; 10281 biodone(bp); 10282 return (0); 10283 } 10284 10285 ASSERT(!mutex_owned(SD_MUTEX(un))); 10286 10287 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 10288 10289 mutex_enter(SD_MUTEX(un)); 10290 /* 10291 * atapi: Since we are running the CD for now in PIO mode we need to 10292 * call bp_mapin here to avoid bp_mapin called interrupt context under 10293 * the HBA's init_pkt routine. 10294 */ 10295 if (un->un_f_cfg_is_atapi == TRUE) { 10296 mutex_exit(SD_MUTEX(un)); 10297 bp_mapin(bp); 10298 mutex_enter(SD_MUTEX(un)); 10299 } 10300 un->un_ncmds_in_driver++; 10301 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 10302 un->un_ncmds_in_driver); 10303 mutex_exit(SD_MUTEX(un)); 10304 10305 /* 10306 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 10307 */ 10308 ASSERT(bp->b_private != NULL); 10309 uip = (struct sd_uscsi_info *)bp->b_private; 10310 10311 switch (uip->ui_flags) { 10312 case SD_PATH_DIRECT: 10313 chain_type = SD_CHAIN_DIRECT; 10314 break; 10315 case SD_PATH_DIRECT_PRIORITY: 10316 chain_type = SD_CHAIN_DIRECT_PRIORITY; 10317 break; 10318 default: 10319 chain_type = SD_CHAIN_USCSI; 10320 break; 10321 } 10322 10323 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 10324 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 10325 10326 /* Use the index obtained within xbuf_init */ 10327 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 10328 10329 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 10330 10331 return (0); 10332 } 10333 10334 /* 10335 * Function: sd_send_scsi_cmd 10336 * 10337 * Description: Runs a USCSI command for user (when called thru sdioctl), 10338 * or for the driver 10339 * 10340 * Arguments: dev - the dev_t for the device 10341 * incmd - ptr to a valid uscsi_cmd struct 10342 * flag - bit flag, indicating open settings, 32/64 bit type 10343 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 10344 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 10345 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 10346 * to use the USCSI "direct" chain and bypass the normal 10347 * command waitq. 10348 * 10349 * Return Code: 0 - successful completion of the given command 10350 * EIO - scsi_uscsi_handle_command() failed 10351 * ENXIO - soft state not found for specified dev 10352 * EINVAL 10353 * EFAULT - copyin/copyout error 10354 * return code of scsi_uscsi_handle_command(): 10355 * EIO 10356 * ENXIO 10357 * EACCES 10358 * 10359 * Context: Waits for command to complete. Can sleep. 10360 */ 10361 10362 static int 10363 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 10364 enum uio_seg dataspace, int path_flag) 10365 { 10366 struct sd_uscsi_info *uip; 10367 struct uscsi_cmd *uscmd; 10368 struct sd_lun *un; 10369 int format = 0; 10370 int rval; 10371 10372 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 10373 if (un == NULL) { 10374 return (ENXIO); 10375 } 10376 10377 ASSERT(!mutex_owned(SD_MUTEX(un))); 10378 10379 #ifdef SDDEBUG 10380 switch (dataspace) { 10381 case UIO_USERSPACE: 10382 SD_TRACE(SD_LOG_IO, un, 10383 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 10384 break; 10385 case UIO_SYSSPACE: 10386 SD_TRACE(SD_LOG_IO, un, 10387 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 10388 break; 10389 default: 10390 SD_TRACE(SD_LOG_IO, un, 10391 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 10392 break; 10393 } 10394 #endif 10395 10396 rval = scsi_uscsi_alloc_and_copyin((intptr_t)incmd, flag, 10397 SD_ADDRESS(un), &uscmd); 10398 if (rval != 0) { 10399 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 10400 "scsi_uscsi_alloc_and_copyin failed\n", un); 10401 return (rval); 10402 } 10403 10404 if ((uscmd->uscsi_cdb != NULL) && 10405 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 10406 mutex_enter(SD_MUTEX(un)); 10407 un->un_f_format_in_progress = TRUE; 10408 mutex_exit(SD_MUTEX(un)); 10409 format = 1; 10410 } 10411 10412 /* 10413 * Allocate an sd_uscsi_info struct and fill it with the info 10414 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 10415 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 10416 * since we allocate the buf here in this function, we do not 10417 * need to preserve the prior contents of b_private. 10418 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 10419 */ 10420 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 10421 uip->ui_flags = path_flag; 10422 uip->ui_cmdp = uscmd; 10423 10424 /* 10425 * Commands sent with priority are intended for error recovery 10426 * situations, and do not have retries performed. 10427 */ 10428 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 10429 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 10430 } 10431 uscmd->uscsi_flags &= ~USCSI_NOINTR; 10432 10433 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 10434 sd_uscsi_strategy, NULL, uip); 10435 10436 #ifdef SDDEBUG 10437 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10438 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 10439 uscmd->uscsi_status, uscmd->uscsi_resid); 10440 if (uscmd->uscsi_bufaddr != NULL) { 10441 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10442 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 10443 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 10444 if (dataspace == UIO_SYSSPACE) { 10445 SD_DUMP_MEMORY(un, SD_LOG_IO, 10446 "data", (uchar_t *)uscmd->uscsi_bufaddr, 10447 uscmd->uscsi_buflen, SD_LOG_HEX); 10448 } 10449 } 10450 #endif 10451 10452 if (format == 1) { 10453 mutex_enter(SD_MUTEX(un)); 10454 un->un_f_format_in_progress = FALSE; 10455 mutex_exit(SD_MUTEX(un)); 10456 } 10457 10458 (void) scsi_uscsi_copyout_and_free((intptr_t)incmd, uscmd); 10459 kmem_free(uip, sizeof (struct sd_uscsi_info)); 10460 10461 return (rval); 10462 } 10463 10464 10465 /* 10466 * Function: sd_buf_iodone 10467 * 10468 * Description: Frees the sd_xbuf & returns the buf to its originator. 10469 * 10470 * Context: May be called from interrupt context. 10471 */ 10472 /* ARGSUSED */ 10473 static void 10474 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 10475 { 10476 struct sd_xbuf *xp; 10477 10478 ASSERT(un != NULL); 10479 ASSERT(bp != NULL); 10480 ASSERT(!mutex_owned(SD_MUTEX(un))); 10481 10482 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 10483 10484 xp = SD_GET_XBUF(bp); 10485 ASSERT(xp != NULL); 10486 10487 mutex_enter(SD_MUTEX(un)); 10488 10489 /* 10490 * Grab time when the cmd completed. 10491 * This is used for determining if the system has been 10492 * idle long enough to make it idle to the PM framework. 10493 * This is for lowering the overhead, and therefore improving 10494 * performance per I/O operation. 10495 */ 10496 un->un_pm_idle_time = ddi_get_time(); 10497 10498 un->un_ncmds_in_driver--; 10499 ASSERT(un->un_ncmds_in_driver >= 0); 10500 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 10501 un->un_ncmds_in_driver); 10502 10503 mutex_exit(SD_MUTEX(un)); 10504 10505 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 10506 biodone(bp); /* bp is gone after this */ 10507 10508 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 10509 } 10510 10511 10512 /* 10513 * Function: sd_uscsi_iodone 10514 * 10515 * Description: Frees the sd_xbuf & returns the buf to its originator. 10516 * 10517 * Context: May be called from interrupt context. 10518 */ 10519 /* ARGSUSED */ 10520 static void 10521 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 10522 { 10523 struct sd_xbuf *xp; 10524 10525 ASSERT(un != NULL); 10526 ASSERT(bp != NULL); 10527 10528 xp = SD_GET_XBUF(bp); 10529 ASSERT(xp != NULL); 10530 ASSERT(!mutex_owned(SD_MUTEX(un))); 10531 10532 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 10533 10534 bp->b_private = xp->xb_private; 10535 10536 mutex_enter(SD_MUTEX(un)); 10537 10538 /* 10539 * Grab time when the cmd completed. 10540 * This is used for determining if the system has been 10541 * idle long enough to make it idle to the PM framework. 10542 * This is for lowering the overhead, and therefore improving 10543 * performance per I/O operation. 10544 */ 10545 un->un_pm_idle_time = ddi_get_time(); 10546 10547 un->un_ncmds_in_driver--; 10548 ASSERT(un->un_ncmds_in_driver >= 0); 10549 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 10550 un->un_ncmds_in_driver); 10551 10552 mutex_exit(SD_MUTEX(un)); 10553 10554 kmem_free(xp, sizeof (struct sd_xbuf)); 10555 biodone(bp); 10556 10557 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 10558 } 10559 10560 10561 /* 10562 * Function: sd_mapblockaddr_iostart 10563 * 10564 * Description: Verify request lies within the partition limits for 10565 * the indicated minor device. Issue "overrun" buf if 10566 * request would exceed partition range. Converts 10567 * partition-relative block address to absolute. 10568 * 10569 * Context: Can sleep 10570 * 10571 * Issues: This follows what the old code did, in terms of accessing 10572 * some of the partition info in the unit struct without holding 10573 * the mutext. This is a general issue, if the partition info 10574 * can be altered while IO is in progress... as soon as we send 10575 * a buf, its partitioning can be invalid before it gets to the 10576 * device. Probably the right fix is to move partitioning out 10577 * of the driver entirely. 10578 */ 10579 10580 static void 10581 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 10582 { 10583 diskaddr_t nblocks; /* #blocks in the given partition */ 10584 daddr_t blocknum; /* Block number specified by the buf */ 10585 size_t requested_nblocks; 10586 size_t available_nblocks; 10587 int partition; 10588 diskaddr_t partition_offset; 10589 struct sd_xbuf *xp; 10590 10591 10592 ASSERT(un != NULL); 10593 ASSERT(bp != NULL); 10594 ASSERT(!mutex_owned(SD_MUTEX(un))); 10595 10596 SD_TRACE(SD_LOG_IO_PARTITION, un, 10597 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 10598 10599 xp = SD_GET_XBUF(bp); 10600 ASSERT(xp != NULL); 10601 10602 /* 10603 * If the geometry is not indicated as valid, attempt to access 10604 * the unit & verify the geometry/label. This can be the case for 10605 * removable-media devices, of if the device was opened in 10606 * NDELAY/NONBLOCK mode. 10607 */ 10608 if (!SD_IS_VALID_LABEL(un) && 10609 (sd_ready_and_valid(un) != SD_READY_VALID)) { 10610 /* 10611 * For removable devices it is possible to start an I/O 10612 * without a media by opening the device in nodelay mode. 10613 * Also for writable CDs there can be many scenarios where 10614 * there is no geometry yet but volume manager is trying to 10615 * issue a read() just because it can see TOC on the CD. So 10616 * do not print a message for removables. 10617 */ 10618 if (!un->un_f_has_removable_media) { 10619 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10620 "i/o to invalid geometry\n"); 10621 } 10622 bioerror(bp, EIO); 10623 bp->b_resid = bp->b_bcount; 10624 SD_BEGIN_IODONE(index, un, bp); 10625 return; 10626 } 10627 10628 partition = SDPART(bp->b_edev); 10629 10630 nblocks = 0; 10631 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 10632 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 10633 10634 /* 10635 * blocknum is the starting block number of the request. At this 10636 * point it is still relative to the start of the minor device. 10637 */ 10638 blocknum = xp->xb_blkno; 10639 10640 /* 10641 * Legacy: If the starting block number is one past the last block 10642 * in the partition, do not set B_ERROR in the buf. 10643 */ 10644 if (blocknum == nblocks) { 10645 goto error_exit; 10646 } 10647 10648 /* 10649 * Confirm that the first block of the request lies within the 10650 * partition limits. Also the requested number of bytes must be 10651 * a multiple of the system block size. 10652 */ 10653 if ((blocknum < 0) || (blocknum >= nblocks) || 10654 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 10655 bp->b_flags |= B_ERROR; 10656 goto error_exit; 10657 } 10658 10659 /* 10660 * If the requsted # blocks exceeds the available # blocks, that 10661 * is an overrun of the partition. 10662 */ 10663 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 10664 available_nblocks = (size_t)(nblocks - blocknum); 10665 ASSERT(nblocks >= blocknum); 10666 10667 if (requested_nblocks > available_nblocks) { 10668 /* 10669 * Allocate an "overrun" buf to allow the request to proceed 10670 * for the amount of space available in the partition. The 10671 * amount not transferred will be added into the b_resid 10672 * when the operation is complete. The overrun buf 10673 * replaces the original buf here, and the original buf 10674 * is saved inside the overrun buf, for later use. 10675 */ 10676 size_t resid = SD_SYSBLOCKS2BYTES(un, 10677 (offset_t)(requested_nblocks - available_nblocks)); 10678 size_t count = bp->b_bcount - resid; 10679 /* 10680 * Note: count is an unsigned entity thus it'll NEVER 10681 * be less than 0 so ASSERT the original values are 10682 * correct. 10683 */ 10684 ASSERT(bp->b_bcount >= resid); 10685 10686 bp = sd_bioclone_alloc(bp, count, blocknum, 10687 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 10688 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 10689 ASSERT(xp != NULL); 10690 } 10691 10692 /* At this point there should be no residual for this buf. */ 10693 ASSERT(bp->b_resid == 0); 10694 10695 /* Convert the block number to an absolute address. */ 10696 xp->xb_blkno += partition_offset; 10697 10698 SD_NEXT_IOSTART(index, un, bp); 10699 10700 SD_TRACE(SD_LOG_IO_PARTITION, un, 10701 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 10702 10703 return; 10704 10705 error_exit: 10706 bp->b_resid = bp->b_bcount; 10707 SD_BEGIN_IODONE(index, un, bp); 10708 SD_TRACE(SD_LOG_IO_PARTITION, un, 10709 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 10710 } 10711 10712 10713 /* 10714 * Function: sd_mapblockaddr_iodone 10715 * 10716 * Description: Completion-side processing for partition management. 10717 * 10718 * Context: May be called under interrupt context 10719 */ 10720 10721 static void 10722 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 10723 { 10724 /* int partition; */ /* Not used, see below. */ 10725 ASSERT(un != NULL); 10726 ASSERT(bp != NULL); 10727 ASSERT(!mutex_owned(SD_MUTEX(un))); 10728 10729 SD_TRACE(SD_LOG_IO_PARTITION, un, 10730 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 10731 10732 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 10733 /* 10734 * We have an "overrun" buf to deal with... 10735 */ 10736 struct sd_xbuf *xp; 10737 struct buf *obp; /* ptr to the original buf */ 10738 10739 xp = SD_GET_XBUF(bp); 10740 ASSERT(xp != NULL); 10741 10742 /* Retrieve the pointer to the original buf */ 10743 obp = (struct buf *)xp->xb_private; 10744 ASSERT(obp != NULL); 10745 10746 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 10747 bioerror(obp, bp->b_error); 10748 10749 sd_bioclone_free(bp); 10750 10751 /* 10752 * Get back the original buf. 10753 * Note that since the restoration of xb_blkno below 10754 * was removed, the sd_xbuf is not needed. 10755 */ 10756 bp = obp; 10757 /* 10758 * xp = SD_GET_XBUF(bp); 10759 * ASSERT(xp != NULL); 10760 */ 10761 } 10762 10763 /* 10764 * Convert sd->xb_blkno back to a minor-device relative value. 10765 * Note: this has been commented out, as it is not needed in the 10766 * current implementation of the driver (ie, since this function 10767 * is at the top of the layering chains, so the info will be 10768 * discarded) and it is in the "hot" IO path. 10769 * 10770 * partition = getminor(bp->b_edev) & SDPART_MASK; 10771 * xp->xb_blkno -= un->un_offset[partition]; 10772 */ 10773 10774 SD_NEXT_IODONE(index, un, bp); 10775 10776 SD_TRACE(SD_LOG_IO_PARTITION, un, 10777 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 10778 } 10779 10780 10781 /* 10782 * Function: sd_mapblocksize_iostart 10783 * 10784 * Description: Convert between system block size (un->un_sys_blocksize) 10785 * and target block size (un->un_tgt_blocksize). 10786 * 10787 * Context: Can sleep to allocate resources. 10788 * 10789 * Assumptions: A higher layer has already performed any partition validation, 10790 * and converted the xp->xb_blkno to an absolute value relative 10791 * to the start of the device. 10792 * 10793 * It is also assumed that the higher layer has implemented 10794 * an "overrun" mechanism for the case where the request would 10795 * read/write beyond the end of a partition. In this case we 10796 * assume (and ASSERT) that bp->b_resid == 0. 10797 * 10798 * Note: The implementation for this routine assumes the target 10799 * block size remains constant between allocation and transport. 10800 */ 10801 10802 static void 10803 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 10804 { 10805 struct sd_mapblocksize_info *bsp; 10806 struct sd_xbuf *xp; 10807 offset_t first_byte; 10808 daddr_t start_block, end_block; 10809 daddr_t request_bytes; 10810 ushort_t is_aligned = FALSE; 10811 10812 ASSERT(un != NULL); 10813 ASSERT(bp != NULL); 10814 ASSERT(!mutex_owned(SD_MUTEX(un))); 10815 ASSERT(bp->b_resid == 0); 10816 10817 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 10818 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 10819 10820 /* 10821 * For a non-writable CD, a write request is an error 10822 */ 10823 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 10824 (un->un_f_mmc_writable_media == FALSE)) { 10825 bioerror(bp, EIO); 10826 bp->b_resid = bp->b_bcount; 10827 SD_BEGIN_IODONE(index, un, bp); 10828 return; 10829 } 10830 10831 /* 10832 * We do not need a shadow buf if the device is using 10833 * un->un_sys_blocksize as its block size or if bcount == 0. 10834 * In this case there is no layer-private data block allocated. 10835 */ 10836 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 10837 (bp->b_bcount == 0)) { 10838 goto done; 10839 } 10840 10841 #if defined(__i386) || defined(__amd64) 10842 /* We do not support non-block-aligned transfers for ROD devices */ 10843 ASSERT(!ISROD(un)); 10844 #endif 10845 10846 xp = SD_GET_XBUF(bp); 10847 ASSERT(xp != NULL); 10848 10849 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 10850 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 10851 un->un_tgt_blocksize, un->un_sys_blocksize); 10852 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 10853 "request start block:0x%x\n", xp->xb_blkno); 10854 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 10855 "request len:0x%x\n", bp->b_bcount); 10856 10857 /* 10858 * Allocate the layer-private data area for the mapblocksize layer. 10859 * Layers are allowed to use the xp_private member of the sd_xbuf 10860 * struct to store the pointer to their layer-private data block, but 10861 * each layer also has the responsibility of restoring the prior 10862 * contents of xb_private before returning the buf/xbuf to the 10863 * higher layer that sent it. 10864 * 10865 * Here we save the prior contents of xp->xb_private into the 10866 * bsp->mbs_oprivate field of our layer-private data area. This value 10867 * is restored by sd_mapblocksize_iodone() just prior to freeing up 10868 * the layer-private area and returning the buf/xbuf to the layer 10869 * that sent it. 10870 * 10871 * Note that here we use kmem_zalloc for the allocation as there are 10872 * parts of the mapblocksize code that expect certain fields to be 10873 * zero unless explicitly set to a required value. 10874 */ 10875 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 10876 bsp->mbs_oprivate = xp->xb_private; 10877 xp->xb_private = bsp; 10878 10879 /* 10880 * This treats the data on the disk (target) as an array of bytes. 10881 * first_byte is the byte offset, from the beginning of the device, 10882 * to the location of the request. This is converted from a 10883 * un->un_sys_blocksize block address to a byte offset, and then back 10884 * to a block address based upon a un->un_tgt_blocksize block size. 10885 * 10886 * xp->xb_blkno should be absolute upon entry into this function, 10887 * but, but it is based upon partitions that use the "system" 10888 * block size. It must be adjusted to reflect the block size of 10889 * the target. 10890 * 10891 * Note that end_block is actually the block that follows the last 10892 * block of the request, but that's what is needed for the computation. 10893 */ 10894 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 10895 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 10896 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 10897 un->un_tgt_blocksize; 10898 10899 /* request_bytes is rounded up to a multiple of the target block size */ 10900 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 10901 10902 /* 10903 * See if the starting address of the request and the request 10904 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 10905 * then we do not need to allocate a shadow buf to handle the request. 10906 */ 10907 if (((first_byte % un->un_tgt_blocksize) == 0) && 10908 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 10909 is_aligned = TRUE; 10910 } 10911 10912 if ((bp->b_flags & B_READ) == 0) { 10913 /* 10914 * Lock the range for a write operation. An aligned request is 10915 * considered a simple write; otherwise the request must be a 10916 * read-modify-write. 10917 */ 10918 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 10919 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 10920 } 10921 10922 /* 10923 * Alloc a shadow buf if the request is not aligned. Also, this is 10924 * where the READ command is generated for a read-modify-write. (The 10925 * write phase is deferred until after the read completes.) 10926 */ 10927 if (is_aligned == FALSE) { 10928 10929 struct sd_mapblocksize_info *shadow_bsp; 10930 struct sd_xbuf *shadow_xp; 10931 struct buf *shadow_bp; 10932 10933 /* 10934 * Allocate the shadow buf and it associated xbuf. Note that 10935 * after this call the xb_blkno value in both the original 10936 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 10937 * same: absolute relative to the start of the device, and 10938 * adjusted for the target block size. The b_blkno in the 10939 * shadow buf will also be set to this value. We should never 10940 * change b_blkno in the original bp however. 10941 * 10942 * Note also that the shadow buf will always need to be a 10943 * READ command, regardless of whether the incoming command 10944 * is a READ or a WRITE. 10945 */ 10946 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 10947 xp->xb_blkno, 10948 (int (*)(struct buf *)) sd_mapblocksize_iodone); 10949 10950 shadow_xp = SD_GET_XBUF(shadow_bp); 10951 10952 /* 10953 * Allocate the layer-private data for the shadow buf. 10954 * (No need to preserve xb_private in the shadow xbuf.) 10955 */ 10956 shadow_xp->xb_private = shadow_bsp = 10957 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 10958 10959 /* 10960 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 10961 * to figure out where the start of the user data is (based upon 10962 * the system block size) in the data returned by the READ 10963 * command (which will be based upon the target blocksize). Note 10964 * that this is only really used if the request is unaligned. 10965 */ 10966 bsp->mbs_copy_offset = (ssize_t)(first_byte - 10967 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 10968 ASSERT((bsp->mbs_copy_offset >= 0) && 10969 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 10970 10971 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 10972 10973 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 10974 10975 /* Transfer the wmap (if any) to the shadow buf */ 10976 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 10977 bsp->mbs_wmp = NULL; 10978 10979 /* 10980 * The shadow buf goes on from here in place of the 10981 * original buf. 10982 */ 10983 shadow_bsp->mbs_orig_bp = bp; 10984 bp = shadow_bp; 10985 } 10986 10987 SD_INFO(SD_LOG_IO_RMMEDIA, un, 10988 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 10989 SD_INFO(SD_LOG_IO_RMMEDIA, un, 10990 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 10991 request_bytes); 10992 SD_INFO(SD_LOG_IO_RMMEDIA, un, 10993 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 10994 10995 done: 10996 SD_NEXT_IOSTART(index, un, bp); 10997 10998 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 10999 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 11000 } 11001 11002 11003 /* 11004 * Function: sd_mapblocksize_iodone 11005 * 11006 * Description: Completion side processing for block-size mapping. 11007 * 11008 * Context: May be called under interrupt context 11009 */ 11010 11011 static void 11012 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 11013 { 11014 struct sd_mapblocksize_info *bsp; 11015 struct sd_xbuf *xp; 11016 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 11017 struct buf *orig_bp; /* ptr to the original buf */ 11018 offset_t shadow_end; 11019 offset_t request_end; 11020 offset_t shadow_start; 11021 ssize_t copy_offset; 11022 size_t copy_length; 11023 size_t shortfall; 11024 uint_t is_write; /* TRUE if this bp is a WRITE */ 11025 uint_t has_wmap; /* TRUE is this bp has a wmap */ 11026 11027 ASSERT(un != NULL); 11028 ASSERT(bp != NULL); 11029 11030 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11031 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 11032 11033 /* 11034 * There is no shadow buf or layer-private data if the target is 11035 * using un->un_sys_blocksize as its block size or if bcount == 0. 11036 */ 11037 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 11038 (bp->b_bcount == 0)) { 11039 goto exit; 11040 } 11041 11042 xp = SD_GET_XBUF(bp); 11043 ASSERT(xp != NULL); 11044 11045 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 11046 bsp = xp->xb_private; 11047 11048 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 11049 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 11050 11051 if (is_write) { 11052 /* 11053 * For a WRITE request we must free up the block range that 11054 * we have locked up. This holds regardless of whether this is 11055 * an aligned write request or a read-modify-write request. 11056 */ 11057 sd_range_unlock(un, bsp->mbs_wmp); 11058 bsp->mbs_wmp = NULL; 11059 } 11060 11061 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 11062 /* 11063 * An aligned read or write command will have no shadow buf; 11064 * there is not much else to do with it. 11065 */ 11066 goto done; 11067 } 11068 11069 orig_bp = bsp->mbs_orig_bp; 11070 ASSERT(orig_bp != NULL); 11071 orig_xp = SD_GET_XBUF(orig_bp); 11072 ASSERT(orig_xp != NULL); 11073 ASSERT(!mutex_owned(SD_MUTEX(un))); 11074 11075 if (!is_write && has_wmap) { 11076 /* 11077 * A READ with a wmap means this is the READ phase of a 11078 * read-modify-write. If an error occurred on the READ then 11079 * we do not proceed with the WRITE phase or copy any data. 11080 * Just release the write maps and return with an error. 11081 */ 11082 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 11083 orig_bp->b_resid = orig_bp->b_bcount; 11084 bioerror(orig_bp, bp->b_error); 11085 sd_range_unlock(un, bsp->mbs_wmp); 11086 goto freebuf_done; 11087 } 11088 } 11089 11090 /* 11091 * Here is where we set up to copy the data from the shadow buf 11092 * into the space associated with the original buf. 11093 * 11094 * To deal with the conversion between block sizes, these 11095 * computations treat the data as an array of bytes, with the 11096 * first byte (byte 0) corresponding to the first byte in the 11097 * first block on the disk. 11098 */ 11099 11100 /* 11101 * shadow_start and shadow_len indicate the location and size of 11102 * the data returned with the shadow IO request. 11103 */ 11104 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 11105 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 11106 11107 /* 11108 * copy_offset gives the offset (in bytes) from the start of the first 11109 * block of the READ request to the beginning of the data. We retrieve 11110 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 11111 * there by sd_mapblockize_iostart(). copy_length gives the amount of 11112 * data to be copied (in bytes). 11113 */ 11114 copy_offset = bsp->mbs_copy_offset; 11115 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 11116 copy_length = orig_bp->b_bcount; 11117 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 11118 11119 /* 11120 * Set up the resid and error fields of orig_bp as appropriate. 11121 */ 11122 if (shadow_end >= request_end) { 11123 /* We got all the requested data; set resid to zero */ 11124 orig_bp->b_resid = 0; 11125 } else { 11126 /* 11127 * We failed to get enough data to fully satisfy the original 11128 * request. Just copy back whatever data we got and set 11129 * up the residual and error code as required. 11130 * 11131 * 'shortfall' is the amount by which the data received with the 11132 * shadow buf has "fallen short" of the requested amount. 11133 */ 11134 shortfall = (size_t)(request_end - shadow_end); 11135 11136 if (shortfall > orig_bp->b_bcount) { 11137 /* 11138 * We did not get enough data to even partially 11139 * fulfill the original request. The residual is 11140 * equal to the amount requested. 11141 */ 11142 orig_bp->b_resid = orig_bp->b_bcount; 11143 } else { 11144 /* 11145 * We did not get all the data that we requested 11146 * from the device, but we will try to return what 11147 * portion we did get. 11148 */ 11149 orig_bp->b_resid = shortfall; 11150 } 11151 ASSERT(copy_length >= orig_bp->b_resid); 11152 copy_length -= orig_bp->b_resid; 11153 } 11154 11155 /* Propagate the error code from the shadow buf to the original buf */ 11156 bioerror(orig_bp, bp->b_error); 11157 11158 if (is_write) { 11159 goto freebuf_done; /* No data copying for a WRITE */ 11160 } 11161 11162 if (has_wmap) { 11163 /* 11164 * This is a READ command from the READ phase of a 11165 * read-modify-write request. We have to copy the data given 11166 * by the user OVER the data returned by the READ command, 11167 * then convert the command from a READ to a WRITE and send 11168 * it back to the target. 11169 */ 11170 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 11171 copy_length); 11172 11173 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 11174 11175 /* 11176 * Dispatch the WRITE command to the taskq thread, which 11177 * will in turn send the command to the target. When the 11178 * WRITE command completes, we (sd_mapblocksize_iodone()) 11179 * will get called again as part of the iodone chain 11180 * processing for it. Note that we will still be dealing 11181 * with the shadow buf at that point. 11182 */ 11183 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 11184 KM_NOSLEEP) != 0) { 11185 /* 11186 * Dispatch was successful so we are done. Return 11187 * without going any higher up the iodone chain. Do 11188 * not free up any layer-private data until after the 11189 * WRITE completes. 11190 */ 11191 return; 11192 } 11193 11194 /* 11195 * Dispatch of the WRITE command failed; set up the error 11196 * condition and send this IO back up the iodone chain. 11197 */ 11198 bioerror(orig_bp, EIO); 11199 orig_bp->b_resid = orig_bp->b_bcount; 11200 11201 } else { 11202 /* 11203 * This is a regular READ request (ie, not a RMW). Copy the 11204 * data from the shadow buf into the original buf. The 11205 * copy_offset compensates for any "misalignment" between the 11206 * shadow buf (with its un->un_tgt_blocksize blocks) and the 11207 * original buf (with its un->un_sys_blocksize blocks). 11208 */ 11209 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 11210 copy_length); 11211 } 11212 11213 freebuf_done: 11214 11215 /* 11216 * At this point we still have both the shadow buf AND the original 11217 * buf to deal with, as well as the layer-private data area in each. 11218 * Local variables are as follows: 11219 * 11220 * bp -- points to shadow buf 11221 * xp -- points to xbuf of shadow buf 11222 * bsp -- points to layer-private data area of shadow buf 11223 * orig_bp -- points to original buf 11224 * 11225 * First free the shadow buf and its associated xbuf, then free the 11226 * layer-private data area from the shadow buf. There is no need to 11227 * restore xb_private in the shadow xbuf. 11228 */ 11229 sd_shadow_buf_free(bp); 11230 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11231 11232 /* 11233 * Now update the local variables to point to the original buf, xbuf, 11234 * and layer-private area. 11235 */ 11236 bp = orig_bp; 11237 xp = SD_GET_XBUF(bp); 11238 ASSERT(xp != NULL); 11239 ASSERT(xp == orig_xp); 11240 bsp = xp->xb_private; 11241 ASSERT(bsp != NULL); 11242 11243 done: 11244 /* 11245 * Restore xb_private to whatever it was set to by the next higher 11246 * layer in the chain, then free the layer-private data area. 11247 */ 11248 xp->xb_private = bsp->mbs_oprivate; 11249 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11250 11251 exit: 11252 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 11253 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 11254 11255 SD_NEXT_IODONE(index, un, bp); 11256 } 11257 11258 11259 /* 11260 * Function: sd_checksum_iostart 11261 * 11262 * Description: A stub function for a layer that's currently not used. 11263 * For now just a placeholder. 11264 * 11265 * Context: Kernel thread context 11266 */ 11267 11268 static void 11269 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 11270 { 11271 ASSERT(un != NULL); 11272 ASSERT(bp != NULL); 11273 ASSERT(!mutex_owned(SD_MUTEX(un))); 11274 SD_NEXT_IOSTART(index, un, bp); 11275 } 11276 11277 11278 /* 11279 * Function: sd_checksum_iodone 11280 * 11281 * Description: A stub function for a layer that's currently not used. 11282 * For now just a placeholder. 11283 * 11284 * Context: May be called under interrupt context 11285 */ 11286 11287 static void 11288 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 11289 { 11290 ASSERT(un != NULL); 11291 ASSERT(bp != NULL); 11292 ASSERT(!mutex_owned(SD_MUTEX(un))); 11293 SD_NEXT_IODONE(index, un, bp); 11294 } 11295 11296 11297 /* 11298 * Function: sd_checksum_uscsi_iostart 11299 * 11300 * Description: A stub function for a layer that's currently not used. 11301 * For now just a placeholder. 11302 * 11303 * Context: Kernel thread context 11304 */ 11305 11306 static void 11307 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 11308 { 11309 ASSERT(un != NULL); 11310 ASSERT(bp != NULL); 11311 ASSERT(!mutex_owned(SD_MUTEX(un))); 11312 SD_NEXT_IOSTART(index, un, bp); 11313 } 11314 11315 11316 /* 11317 * Function: sd_checksum_uscsi_iodone 11318 * 11319 * Description: A stub function for a layer that's currently not used. 11320 * For now just a placeholder. 11321 * 11322 * Context: May be called under interrupt context 11323 */ 11324 11325 static void 11326 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11327 { 11328 ASSERT(un != NULL); 11329 ASSERT(bp != NULL); 11330 ASSERT(!mutex_owned(SD_MUTEX(un))); 11331 SD_NEXT_IODONE(index, un, bp); 11332 } 11333 11334 11335 /* 11336 * Function: sd_pm_iostart 11337 * 11338 * Description: iostart-side routine for Power mangement. 11339 * 11340 * Context: Kernel thread context 11341 */ 11342 11343 static void 11344 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 11345 { 11346 ASSERT(un != NULL); 11347 ASSERT(bp != NULL); 11348 ASSERT(!mutex_owned(SD_MUTEX(un))); 11349 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11350 11351 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 11352 11353 if (sd_pm_entry(un) != DDI_SUCCESS) { 11354 /* 11355 * Set up to return the failed buf back up the 'iodone' 11356 * side of the calling chain. 11357 */ 11358 bioerror(bp, EIO); 11359 bp->b_resid = bp->b_bcount; 11360 11361 SD_BEGIN_IODONE(index, un, bp); 11362 11363 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11364 return; 11365 } 11366 11367 SD_NEXT_IOSTART(index, un, bp); 11368 11369 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11370 } 11371 11372 11373 /* 11374 * Function: sd_pm_iodone 11375 * 11376 * Description: iodone-side routine for power mangement. 11377 * 11378 * Context: may be called from interrupt context 11379 */ 11380 11381 static void 11382 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 11383 { 11384 ASSERT(un != NULL); 11385 ASSERT(bp != NULL); 11386 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11387 11388 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 11389 11390 /* 11391 * After attach the following flag is only read, so don't 11392 * take the penalty of acquiring a mutex for it. 11393 */ 11394 if (un->un_f_pm_is_enabled == TRUE) { 11395 sd_pm_exit(un); 11396 } 11397 11398 SD_NEXT_IODONE(index, un, bp); 11399 11400 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 11401 } 11402 11403 11404 /* 11405 * Function: sd_core_iostart 11406 * 11407 * Description: Primary driver function for enqueuing buf(9S) structs from 11408 * the system and initiating IO to the target device 11409 * 11410 * Context: Kernel thread context. Can sleep. 11411 * 11412 * Assumptions: - The given xp->xb_blkno is absolute 11413 * (ie, relative to the start of the device). 11414 * - The IO is to be done using the native blocksize of 11415 * the device, as specified in un->un_tgt_blocksize. 11416 */ 11417 /* ARGSUSED */ 11418 static void 11419 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 11420 { 11421 struct sd_xbuf *xp; 11422 11423 ASSERT(un != NULL); 11424 ASSERT(bp != NULL); 11425 ASSERT(!mutex_owned(SD_MUTEX(un))); 11426 ASSERT(bp->b_resid == 0); 11427 11428 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 11429 11430 xp = SD_GET_XBUF(bp); 11431 ASSERT(xp != NULL); 11432 11433 mutex_enter(SD_MUTEX(un)); 11434 11435 /* 11436 * If we are currently in the failfast state, fail any new IO 11437 * that has B_FAILFAST set, then return. 11438 */ 11439 if ((bp->b_flags & B_FAILFAST) && 11440 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 11441 mutex_exit(SD_MUTEX(un)); 11442 bioerror(bp, EIO); 11443 bp->b_resid = bp->b_bcount; 11444 SD_BEGIN_IODONE(index, un, bp); 11445 return; 11446 } 11447 11448 if (SD_IS_DIRECT_PRIORITY(xp)) { 11449 /* 11450 * Priority command -- transport it immediately. 11451 * 11452 * Note: We may want to assert that USCSI_DIAGNOSE is set, 11453 * because all direct priority commands should be associated 11454 * with error recovery actions which we don't want to retry. 11455 */ 11456 sd_start_cmds(un, bp); 11457 } else { 11458 /* 11459 * Normal command -- add it to the wait queue, then start 11460 * transporting commands from the wait queue. 11461 */ 11462 sd_add_buf_to_waitq(un, bp); 11463 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 11464 sd_start_cmds(un, NULL); 11465 } 11466 11467 mutex_exit(SD_MUTEX(un)); 11468 11469 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 11470 } 11471 11472 11473 /* 11474 * Function: sd_init_cdb_limits 11475 * 11476 * Description: This is to handle scsi_pkt initialization differences 11477 * between the driver platforms. 11478 * 11479 * Legacy behaviors: 11480 * 11481 * If the block number or the sector count exceeds the 11482 * capabilities of a Group 0 command, shift over to a 11483 * Group 1 command. We don't blindly use Group 1 11484 * commands because a) some drives (CDC Wren IVs) get a 11485 * bit confused, and b) there is probably a fair amount 11486 * of speed difference for a target to receive and decode 11487 * a 10 byte command instead of a 6 byte command. 11488 * 11489 * The xfer time difference of 6 vs 10 byte CDBs is 11490 * still significant so this code is still worthwhile. 11491 * 10 byte CDBs are very inefficient with the fas HBA driver 11492 * and older disks. Each CDB byte took 1 usec with some 11493 * popular disks. 11494 * 11495 * Context: Must be called at attach time 11496 */ 11497 11498 static void 11499 sd_init_cdb_limits(struct sd_lun *un) 11500 { 11501 int hba_cdb_limit; 11502 11503 /* 11504 * Use CDB_GROUP1 commands for most devices except for 11505 * parallel SCSI fixed drives in which case we get better 11506 * performance using CDB_GROUP0 commands (where applicable). 11507 */ 11508 un->un_mincdb = SD_CDB_GROUP1; 11509 #if !defined(__fibre) 11510 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 11511 !un->un_f_has_removable_media) { 11512 un->un_mincdb = SD_CDB_GROUP0; 11513 } 11514 #endif 11515 11516 /* 11517 * Try to read the max-cdb-length supported by HBA. 11518 */ 11519 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 11520 if (0 >= un->un_max_hba_cdb) { 11521 un->un_max_hba_cdb = CDB_GROUP4; 11522 hba_cdb_limit = SD_CDB_GROUP4; 11523 } else if (0 < un->un_max_hba_cdb && 11524 un->un_max_hba_cdb < CDB_GROUP1) { 11525 hba_cdb_limit = SD_CDB_GROUP0; 11526 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 11527 un->un_max_hba_cdb < CDB_GROUP5) { 11528 hba_cdb_limit = SD_CDB_GROUP1; 11529 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 11530 un->un_max_hba_cdb < CDB_GROUP4) { 11531 hba_cdb_limit = SD_CDB_GROUP5; 11532 } else { 11533 hba_cdb_limit = SD_CDB_GROUP4; 11534 } 11535 11536 /* 11537 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 11538 * commands for fixed disks unless we are building for a 32 bit 11539 * kernel. 11540 */ 11541 #ifdef _LP64 11542 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11543 min(hba_cdb_limit, SD_CDB_GROUP4); 11544 #else 11545 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11546 min(hba_cdb_limit, SD_CDB_GROUP1); 11547 #endif 11548 11549 /* 11550 * x86 systems require the PKT_DMA_PARTIAL flag 11551 */ 11552 #if defined(__x86) 11553 un->un_pkt_flags = PKT_DMA_PARTIAL; 11554 #else 11555 un->un_pkt_flags = 0; 11556 #endif 11557 11558 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 11559 ? sizeof (struct scsi_arq_status) : 1); 11560 un->un_cmd_timeout = (ushort_t)sd_io_time; 11561 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 11562 } 11563 11564 11565 /* 11566 * Function: sd_initpkt_for_buf 11567 * 11568 * Description: Allocate and initialize for transport a scsi_pkt struct, 11569 * based upon the info specified in the given buf struct. 11570 * 11571 * Assumes the xb_blkno in the request is absolute (ie, 11572 * relative to the start of the device (NOT partition!). 11573 * Also assumes that the request is using the native block 11574 * size of the device (as returned by the READ CAPACITY 11575 * command). 11576 * 11577 * Return Code: SD_PKT_ALLOC_SUCCESS 11578 * SD_PKT_ALLOC_FAILURE 11579 * SD_PKT_ALLOC_FAILURE_NO_DMA 11580 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11581 * 11582 * Context: Kernel thread and may be called from software interrupt context 11583 * as part of a sdrunout callback. This function may not block or 11584 * call routines that block 11585 */ 11586 11587 static int 11588 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 11589 { 11590 struct sd_xbuf *xp; 11591 struct scsi_pkt *pktp = NULL; 11592 struct sd_lun *un; 11593 size_t blockcount; 11594 daddr_t startblock; 11595 int rval; 11596 int cmd_flags; 11597 11598 ASSERT(bp != NULL); 11599 ASSERT(pktpp != NULL); 11600 xp = SD_GET_XBUF(bp); 11601 ASSERT(xp != NULL); 11602 un = SD_GET_UN(bp); 11603 ASSERT(un != NULL); 11604 ASSERT(mutex_owned(SD_MUTEX(un))); 11605 ASSERT(bp->b_resid == 0); 11606 11607 SD_TRACE(SD_LOG_IO_CORE, un, 11608 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 11609 11610 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11611 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 11612 /* 11613 * Already have a scsi_pkt -- just need DMA resources. 11614 * We must recompute the CDB in case the mapping returns 11615 * a nonzero pkt_resid. 11616 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 11617 * that is being retried, the unmap/remap of the DMA resouces 11618 * will result in the entire transfer starting over again 11619 * from the very first block. 11620 */ 11621 ASSERT(xp->xb_pktp != NULL); 11622 pktp = xp->xb_pktp; 11623 } else { 11624 pktp = NULL; 11625 } 11626 #endif /* __i386 || __amd64 */ 11627 11628 startblock = xp->xb_blkno; /* Absolute block num. */ 11629 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 11630 11631 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11632 11633 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 11634 11635 #else 11636 11637 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 11638 11639 #endif 11640 11641 /* 11642 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 11643 * call scsi_init_pkt, and build the CDB. 11644 */ 11645 rval = sd_setup_rw_pkt(un, &pktp, bp, 11646 cmd_flags, sdrunout, (caddr_t)un, 11647 startblock, blockcount); 11648 11649 if (rval == 0) { 11650 /* 11651 * Success. 11652 * 11653 * If partial DMA is being used and required for this transfer. 11654 * set it up here. 11655 */ 11656 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 11657 (pktp->pkt_resid != 0)) { 11658 11659 /* 11660 * Save the CDB length and pkt_resid for the 11661 * next xfer 11662 */ 11663 xp->xb_dma_resid = pktp->pkt_resid; 11664 11665 /* rezero resid */ 11666 pktp->pkt_resid = 0; 11667 11668 } else { 11669 xp->xb_dma_resid = 0; 11670 } 11671 11672 pktp->pkt_flags = un->un_tagflags; 11673 pktp->pkt_time = un->un_cmd_timeout; 11674 pktp->pkt_comp = sdintr; 11675 11676 pktp->pkt_private = bp; 11677 *pktpp = pktp; 11678 11679 SD_TRACE(SD_LOG_IO_CORE, un, 11680 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 11681 11682 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11683 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 11684 #endif 11685 11686 return (SD_PKT_ALLOC_SUCCESS); 11687 11688 } 11689 11690 /* 11691 * SD_PKT_ALLOC_FAILURE is the only expected failure code 11692 * from sd_setup_rw_pkt. 11693 */ 11694 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 11695 11696 if (rval == SD_PKT_ALLOC_FAILURE) { 11697 *pktpp = NULL; 11698 /* 11699 * Set the driver state to RWAIT to indicate the driver 11700 * is waiting on resource allocations. The driver will not 11701 * suspend, pm_suspend, or detatch while the state is RWAIT. 11702 */ 11703 New_state(un, SD_STATE_RWAIT); 11704 11705 SD_ERROR(SD_LOG_IO_CORE, un, 11706 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 11707 11708 if ((bp->b_flags & B_ERROR) != 0) { 11709 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 11710 } 11711 return (SD_PKT_ALLOC_FAILURE); 11712 } else { 11713 /* 11714 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11715 * 11716 * This should never happen. Maybe someone messed with the 11717 * kernel's minphys? 11718 */ 11719 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11720 "Request rejected: too large for CDB: " 11721 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 11722 SD_ERROR(SD_LOG_IO_CORE, un, 11723 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 11724 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11725 11726 } 11727 } 11728 11729 11730 /* 11731 * Function: sd_destroypkt_for_buf 11732 * 11733 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 11734 * 11735 * Context: Kernel thread or interrupt context 11736 */ 11737 11738 static void 11739 sd_destroypkt_for_buf(struct buf *bp) 11740 { 11741 ASSERT(bp != NULL); 11742 ASSERT(SD_GET_UN(bp) != NULL); 11743 11744 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11745 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 11746 11747 ASSERT(SD_GET_PKTP(bp) != NULL); 11748 scsi_destroy_pkt(SD_GET_PKTP(bp)); 11749 11750 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11751 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 11752 } 11753 11754 /* 11755 * Function: sd_setup_rw_pkt 11756 * 11757 * Description: Determines appropriate CDB group for the requested LBA 11758 * and transfer length, calls scsi_init_pkt, and builds 11759 * the CDB. Do not use for partial DMA transfers except 11760 * for the initial transfer since the CDB size must 11761 * remain constant. 11762 * 11763 * Context: Kernel thread and may be called from software interrupt 11764 * context as part of a sdrunout callback. This function may not 11765 * block or call routines that block 11766 */ 11767 11768 11769 int 11770 sd_setup_rw_pkt(struct sd_lun *un, 11771 struct scsi_pkt **pktpp, struct buf *bp, int flags, 11772 int (*callback)(caddr_t), caddr_t callback_arg, 11773 diskaddr_t lba, uint32_t blockcount) 11774 { 11775 struct scsi_pkt *return_pktp; 11776 union scsi_cdb *cdbp; 11777 struct sd_cdbinfo *cp = NULL; 11778 int i; 11779 11780 /* 11781 * See which size CDB to use, based upon the request. 11782 */ 11783 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 11784 11785 /* 11786 * Check lba and block count against sd_cdbtab limits. 11787 * In the partial DMA case, we have to use the same size 11788 * CDB for all the transfers. Check lba + blockcount 11789 * against the max LBA so we know that segment of the 11790 * transfer can use the CDB we select. 11791 */ 11792 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 11793 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 11794 11795 /* 11796 * The command will fit into the CDB type 11797 * specified by sd_cdbtab[i]. 11798 */ 11799 cp = sd_cdbtab + i; 11800 11801 /* 11802 * Call scsi_init_pkt so we can fill in the 11803 * CDB. 11804 */ 11805 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 11806 bp, cp->sc_grpcode, un->un_status_len, 0, 11807 flags, callback, callback_arg); 11808 11809 if (return_pktp != NULL) { 11810 11811 /* 11812 * Return new value of pkt 11813 */ 11814 *pktpp = return_pktp; 11815 11816 /* 11817 * To be safe, zero the CDB insuring there is 11818 * no leftover data from a previous command. 11819 */ 11820 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 11821 11822 /* 11823 * Handle partial DMA mapping 11824 */ 11825 if (return_pktp->pkt_resid != 0) { 11826 11827 /* 11828 * Not going to xfer as many blocks as 11829 * originally expected 11830 */ 11831 blockcount -= 11832 SD_BYTES2TGTBLOCKS(un, 11833 return_pktp->pkt_resid); 11834 } 11835 11836 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 11837 11838 /* 11839 * Set command byte based on the CDB 11840 * type we matched. 11841 */ 11842 cdbp->scc_cmd = cp->sc_grpmask | 11843 ((bp->b_flags & B_READ) ? 11844 SCMD_READ : SCMD_WRITE); 11845 11846 SD_FILL_SCSI1_LUN(un, return_pktp); 11847 11848 /* 11849 * Fill in LBA and length 11850 */ 11851 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 11852 (cp->sc_grpcode == CDB_GROUP4) || 11853 (cp->sc_grpcode == CDB_GROUP0) || 11854 (cp->sc_grpcode == CDB_GROUP5)); 11855 11856 if (cp->sc_grpcode == CDB_GROUP1) { 11857 FORMG1ADDR(cdbp, lba); 11858 FORMG1COUNT(cdbp, blockcount); 11859 return (0); 11860 } else if (cp->sc_grpcode == CDB_GROUP4) { 11861 FORMG4LONGADDR(cdbp, lba); 11862 FORMG4COUNT(cdbp, blockcount); 11863 return (0); 11864 } else if (cp->sc_grpcode == CDB_GROUP0) { 11865 FORMG0ADDR(cdbp, lba); 11866 FORMG0COUNT(cdbp, blockcount); 11867 return (0); 11868 } else if (cp->sc_grpcode == CDB_GROUP5) { 11869 FORMG5ADDR(cdbp, lba); 11870 FORMG5COUNT(cdbp, blockcount); 11871 return (0); 11872 } 11873 11874 /* 11875 * It should be impossible to not match one 11876 * of the CDB types above, so we should never 11877 * reach this point. Set the CDB command byte 11878 * to test-unit-ready to avoid writing 11879 * to somewhere we don't intend. 11880 */ 11881 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 11882 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11883 } else { 11884 /* 11885 * Couldn't get scsi_pkt 11886 */ 11887 return (SD_PKT_ALLOC_FAILURE); 11888 } 11889 } 11890 } 11891 11892 /* 11893 * None of the available CDB types were suitable. This really 11894 * should never happen: on a 64 bit system we support 11895 * READ16/WRITE16 which will hold an entire 64 bit disk address 11896 * and on a 32 bit system we will refuse to bind to a device 11897 * larger than 2TB so addresses will never be larger than 32 bits. 11898 */ 11899 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11900 } 11901 11902 #if defined(__i386) || defined(__amd64) 11903 /* 11904 * Function: sd_setup_next_rw_pkt 11905 * 11906 * Description: Setup packet for partial DMA transfers, except for the 11907 * initial transfer. sd_setup_rw_pkt should be used for 11908 * the initial transfer. 11909 * 11910 * Context: Kernel thread and may be called from interrupt context. 11911 */ 11912 11913 int 11914 sd_setup_next_rw_pkt(struct sd_lun *un, 11915 struct scsi_pkt *pktp, struct buf *bp, 11916 diskaddr_t lba, uint32_t blockcount) 11917 { 11918 uchar_t com; 11919 union scsi_cdb *cdbp; 11920 uchar_t cdb_group_id; 11921 11922 ASSERT(pktp != NULL); 11923 ASSERT(pktp->pkt_cdbp != NULL); 11924 11925 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 11926 com = cdbp->scc_cmd; 11927 cdb_group_id = CDB_GROUPID(com); 11928 11929 ASSERT((cdb_group_id == CDB_GROUPID_0) || 11930 (cdb_group_id == CDB_GROUPID_1) || 11931 (cdb_group_id == CDB_GROUPID_4) || 11932 (cdb_group_id == CDB_GROUPID_5)); 11933 11934 /* 11935 * Move pkt to the next portion of the xfer. 11936 * func is NULL_FUNC so we do not have to release 11937 * the disk mutex here. 11938 */ 11939 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 11940 NULL_FUNC, NULL) == pktp) { 11941 /* Success. Handle partial DMA */ 11942 if (pktp->pkt_resid != 0) { 11943 blockcount -= 11944 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 11945 } 11946 11947 cdbp->scc_cmd = com; 11948 SD_FILL_SCSI1_LUN(un, pktp); 11949 if (cdb_group_id == CDB_GROUPID_1) { 11950 FORMG1ADDR(cdbp, lba); 11951 FORMG1COUNT(cdbp, blockcount); 11952 return (0); 11953 } else if (cdb_group_id == CDB_GROUPID_4) { 11954 FORMG4LONGADDR(cdbp, lba); 11955 FORMG4COUNT(cdbp, blockcount); 11956 return (0); 11957 } else if (cdb_group_id == CDB_GROUPID_0) { 11958 FORMG0ADDR(cdbp, lba); 11959 FORMG0COUNT(cdbp, blockcount); 11960 return (0); 11961 } else if (cdb_group_id == CDB_GROUPID_5) { 11962 FORMG5ADDR(cdbp, lba); 11963 FORMG5COUNT(cdbp, blockcount); 11964 return (0); 11965 } 11966 11967 /* Unreachable */ 11968 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11969 } 11970 11971 /* 11972 * Error setting up next portion of cmd transfer. 11973 * Something is definitely very wrong and this 11974 * should not happen. 11975 */ 11976 return (SD_PKT_ALLOC_FAILURE); 11977 } 11978 #endif /* defined(__i386) || defined(__amd64) */ 11979 11980 /* 11981 * Function: sd_initpkt_for_uscsi 11982 * 11983 * Description: Allocate and initialize for transport a scsi_pkt struct, 11984 * based upon the info specified in the given uscsi_cmd struct. 11985 * 11986 * Return Code: SD_PKT_ALLOC_SUCCESS 11987 * SD_PKT_ALLOC_FAILURE 11988 * SD_PKT_ALLOC_FAILURE_NO_DMA 11989 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11990 * 11991 * Context: Kernel thread and may be called from software interrupt context 11992 * as part of a sdrunout callback. This function may not block or 11993 * call routines that block 11994 */ 11995 11996 static int 11997 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 11998 { 11999 struct uscsi_cmd *uscmd; 12000 struct sd_xbuf *xp; 12001 struct scsi_pkt *pktp; 12002 struct sd_lun *un; 12003 uint32_t flags = 0; 12004 12005 ASSERT(bp != NULL); 12006 ASSERT(pktpp != NULL); 12007 xp = SD_GET_XBUF(bp); 12008 ASSERT(xp != NULL); 12009 un = SD_GET_UN(bp); 12010 ASSERT(un != NULL); 12011 ASSERT(mutex_owned(SD_MUTEX(un))); 12012 12013 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12014 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12015 ASSERT(uscmd != NULL); 12016 12017 SD_TRACE(SD_LOG_IO_CORE, un, 12018 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 12019 12020 /* 12021 * Allocate the scsi_pkt for the command. 12022 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 12023 * during scsi_init_pkt time and will continue to use the 12024 * same path as long as the same scsi_pkt is used without 12025 * intervening scsi_dma_free(). Since uscsi command does 12026 * not call scsi_dmafree() before retry failed command, it 12027 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 12028 * set such that scsi_vhci can use other available path for 12029 * retry. Besides, ucsci command does not allow DMA breakup, 12030 * so there is no need to set PKT_DMA_PARTIAL flag. 12031 */ 12032 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 12033 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 12034 sizeof (struct scsi_arq_status), 0, 12035 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 12036 sdrunout, (caddr_t)un); 12037 12038 if (pktp == NULL) { 12039 *pktpp = NULL; 12040 /* 12041 * Set the driver state to RWAIT to indicate the driver 12042 * is waiting on resource allocations. The driver will not 12043 * suspend, pm_suspend, or detatch while the state is RWAIT. 12044 */ 12045 New_state(un, SD_STATE_RWAIT); 12046 12047 SD_ERROR(SD_LOG_IO_CORE, un, 12048 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 12049 12050 if ((bp->b_flags & B_ERROR) != 0) { 12051 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 12052 } 12053 return (SD_PKT_ALLOC_FAILURE); 12054 } 12055 12056 /* 12057 * We do not do DMA breakup for USCSI commands, so return failure 12058 * here if all the needed DMA resources were not allocated. 12059 */ 12060 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 12061 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 12062 scsi_destroy_pkt(pktp); 12063 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 12064 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 12065 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 12066 } 12067 12068 /* Init the cdb from the given uscsi struct */ 12069 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 12070 uscmd->uscsi_cdb[0], 0, 0, 0); 12071 12072 SD_FILL_SCSI1_LUN(un, pktp); 12073 12074 /* 12075 * Set up the optional USCSI flags. See the uscsi (7I) man page 12076 * for listing of the supported flags. 12077 */ 12078 12079 if (uscmd->uscsi_flags & USCSI_SILENT) { 12080 flags |= FLAG_SILENT; 12081 } 12082 12083 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 12084 flags |= FLAG_DIAGNOSE; 12085 } 12086 12087 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 12088 flags |= FLAG_ISOLATE; 12089 } 12090 12091 if (un->un_f_is_fibre == FALSE) { 12092 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 12093 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 12094 } 12095 } 12096 12097 /* 12098 * Set the pkt flags here so we save time later. 12099 * Note: These flags are NOT in the uscsi man page!!! 12100 */ 12101 if (uscmd->uscsi_flags & USCSI_HEAD) { 12102 flags |= FLAG_HEAD; 12103 } 12104 12105 if (uscmd->uscsi_flags & USCSI_NOINTR) { 12106 flags |= FLAG_NOINTR; 12107 } 12108 12109 /* 12110 * For tagged queueing, things get a bit complicated. 12111 * Check first for head of queue and last for ordered queue. 12112 * If neither head nor order, use the default driver tag flags. 12113 */ 12114 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 12115 if (uscmd->uscsi_flags & USCSI_HTAG) { 12116 flags |= FLAG_HTAG; 12117 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 12118 flags |= FLAG_OTAG; 12119 } else { 12120 flags |= un->un_tagflags & FLAG_TAGMASK; 12121 } 12122 } 12123 12124 if (uscmd->uscsi_flags & USCSI_NODISCON) { 12125 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 12126 } 12127 12128 pktp->pkt_flags = flags; 12129 12130 /* Copy the caller's CDB into the pkt... */ 12131 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 12132 12133 if (uscmd->uscsi_timeout == 0) { 12134 pktp->pkt_time = un->un_uscsi_timeout; 12135 } else { 12136 pktp->pkt_time = uscmd->uscsi_timeout; 12137 } 12138 12139 /* need it later to identify USCSI request in sdintr */ 12140 xp->xb_pkt_flags |= SD_XB_USCSICMD; 12141 12142 xp->xb_sense_resid = uscmd->uscsi_rqresid; 12143 12144 pktp->pkt_private = bp; 12145 pktp->pkt_comp = sdintr; 12146 *pktpp = pktp; 12147 12148 SD_TRACE(SD_LOG_IO_CORE, un, 12149 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 12150 12151 return (SD_PKT_ALLOC_SUCCESS); 12152 } 12153 12154 12155 /* 12156 * Function: sd_destroypkt_for_uscsi 12157 * 12158 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 12159 * IOs.. Also saves relevant info into the associated uscsi_cmd 12160 * struct. 12161 * 12162 * Context: May be called under interrupt context 12163 */ 12164 12165 static void 12166 sd_destroypkt_for_uscsi(struct buf *bp) 12167 { 12168 struct uscsi_cmd *uscmd; 12169 struct sd_xbuf *xp; 12170 struct scsi_pkt *pktp; 12171 struct sd_lun *un; 12172 12173 ASSERT(bp != NULL); 12174 xp = SD_GET_XBUF(bp); 12175 ASSERT(xp != NULL); 12176 un = SD_GET_UN(bp); 12177 ASSERT(un != NULL); 12178 ASSERT(!mutex_owned(SD_MUTEX(un))); 12179 pktp = SD_GET_PKTP(bp); 12180 ASSERT(pktp != NULL); 12181 12182 SD_TRACE(SD_LOG_IO_CORE, un, 12183 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 12184 12185 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12186 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12187 ASSERT(uscmd != NULL); 12188 12189 /* Save the status and the residual into the uscsi_cmd struct */ 12190 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 12191 uscmd->uscsi_resid = bp->b_resid; 12192 12193 /* 12194 * If enabled, copy any saved sense data into the area specified 12195 * by the uscsi command. 12196 */ 12197 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12198 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12199 /* 12200 * Note: uscmd->uscsi_rqbuf should always point to a buffer 12201 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 12202 */ 12203 uscmd->uscsi_rqstatus = xp->xb_sense_status; 12204 uscmd->uscsi_rqresid = xp->xb_sense_resid; 12205 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 12206 } 12207 12208 /* We are done with the scsi_pkt; free it now */ 12209 ASSERT(SD_GET_PKTP(bp) != NULL); 12210 scsi_destroy_pkt(SD_GET_PKTP(bp)); 12211 12212 SD_TRACE(SD_LOG_IO_CORE, un, 12213 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 12214 } 12215 12216 12217 /* 12218 * Function: sd_bioclone_alloc 12219 * 12220 * Description: Allocate a buf(9S) and init it as per the given buf 12221 * and the various arguments. The associated sd_xbuf 12222 * struct is (nearly) duplicated. The struct buf *bp 12223 * argument is saved in new_xp->xb_private. 12224 * 12225 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12226 * datalen - size of data area for the shadow bp 12227 * blkno - starting LBA 12228 * func - function pointer for b_iodone in the shadow buf. (May 12229 * be NULL if none.) 12230 * 12231 * Return Code: Pointer to allocates buf(9S) struct 12232 * 12233 * Context: Can sleep. 12234 */ 12235 12236 static struct buf * 12237 sd_bioclone_alloc(struct buf *bp, size_t datalen, 12238 daddr_t blkno, int (*func)(struct buf *)) 12239 { 12240 struct sd_lun *un; 12241 struct sd_xbuf *xp; 12242 struct sd_xbuf *new_xp; 12243 struct buf *new_bp; 12244 12245 ASSERT(bp != NULL); 12246 xp = SD_GET_XBUF(bp); 12247 ASSERT(xp != NULL); 12248 un = SD_GET_UN(bp); 12249 ASSERT(un != NULL); 12250 ASSERT(!mutex_owned(SD_MUTEX(un))); 12251 12252 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 12253 NULL, KM_SLEEP); 12254 12255 new_bp->b_lblkno = blkno; 12256 12257 /* 12258 * Allocate an xbuf for the shadow bp and copy the contents of the 12259 * original xbuf into it. 12260 */ 12261 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12262 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12263 12264 /* 12265 * The given bp is automatically saved in the xb_private member 12266 * of the new xbuf. Callers are allowed to depend on this. 12267 */ 12268 new_xp->xb_private = bp; 12269 12270 new_bp->b_private = new_xp; 12271 12272 return (new_bp); 12273 } 12274 12275 /* 12276 * Function: sd_shadow_buf_alloc 12277 * 12278 * Description: Allocate a buf(9S) and init it as per the given buf 12279 * and the various arguments. The associated sd_xbuf 12280 * struct is (nearly) duplicated. The struct buf *bp 12281 * argument is saved in new_xp->xb_private. 12282 * 12283 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12284 * datalen - size of data area for the shadow bp 12285 * bflags - B_READ or B_WRITE (pseudo flag) 12286 * blkno - starting LBA 12287 * func - function pointer for b_iodone in the shadow buf. (May 12288 * be NULL if none.) 12289 * 12290 * Return Code: Pointer to allocates buf(9S) struct 12291 * 12292 * Context: Can sleep. 12293 */ 12294 12295 static struct buf * 12296 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 12297 daddr_t blkno, int (*func)(struct buf *)) 12298 { 12299 struct sd_lun *un; 12300 struct sd_xbuf *xp; 12301 struct sd_xbuf *new_xp; 12302 struct buf *new_bp; 12303 12304 ASSERT(bp != NULL); 12305 xp = SD_GET_XBUF(bp); 12306 ASSERT(xp != NULL); 12307 un = SD_GET_UN(bp); 12308 ASSERT(un != NULL); 12309 ASSERT(!mutex_owned(SD_MUTEX(un))); 12310 12311 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 12312 bp_mapin(bp); 12313 } 12314 12315 bflags &= (B_READ | B_WRITE); 12316 #if defined(__i386) || defined(__amd64) 12317 new_bp = getrbuf(KM_SLEEP); 12318 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 12319 new_bp->b_bcount = datalen; 12320 new_bp->b_flags = bflags | 12321 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 12322 #else 12323 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 12324 datalen, bflags, SLEEP_FUNC, NULL); 12325 #endif 12326 new_bp->av_forw = NULL; 12327 new_bp->av_back = NULL; 12328 new_bp->b_dev = bp->b_dev; 12329 new_bp->b_blkno = blkno; 12330 new_bp->b_iodone = func; 12331 new_bp->b_edev = bp->b_edev; 12332 new_bp->b_resid = 0; 12333 12334 /* We need to preserve the B_FAILFAST flag */ 12335 if (bp->b_flags & B_FAILFAST) { 12336 new_bp->b_flags |= B_FAILFAST; 12337 } 12338 12339 /* 12340 * Allocate an xbuf for the shadow bp and copy the contents of the 12341 * original xbuf into it. 12342 */ 12343 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12344 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12345 12346 /* Need later to copy data between the shadow buf & original buf! */ 12347 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 12348 12349 /* 12350 * The given bp is automatically saved in the xb_private member 12351 * of the new xbuf. Callers are allowed to depend on this. 12352 */ 12353 new_xp->xb_private = bp; 12354 12355 new_bp->b_private = new_xp; 12356 12357 return (new_bp); 12358 } 12359 12360 /* 12361 * Function: sd_bioclone_free 12362 * 12363 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 12364 * in the larger than partition operation. 12365 * 12366 * Context: May be called under interrupt context 12367 */ 12368 12369 static void 12370 sd_bioclone_free(struct buf *bp) 12371 { 12372 struct sd_xbuf *xp; 12373 12374 ASSERT(bp != NULL); 12375 xp = SD_GET_XBUF(bp); 12376 ASSERT(xp != NULL); 12377 12378 /* 12379 * Call bp_mapout() before freeing the buf, in case a lower 12380 * layer or HBA had done a bp_mapin(). we must do this here 12381 * as we are the "originator" of the shadow buf. 12382 */ 12383 bp_mapout(bp); 12384 12385 /* 12386 * Null out b_iodone before freeing the bp, to ensure that the driver 12387 * never gets confused by a stale value in this field. (Just a little 12388 * extra defensiveness here.) 12389 */ 12390 bp->b_iodone = NULL; 12391 12392 freerbuf(bp); 12393 12394 kmem_free(xp, sizeof (struct sd_xbuf)); 12395 } 12396 12397 /* 12398 * Function: sd_shadow_buf_free 12399 * 12400 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 12401 * 12402 * Context: May be called under interrupt context 12403 */ 12404 12405 static void 12406 sd_shadow_buf_free(struct buf *bp) 12407 { 12408 struct sd_xbuf *xp; 12409 12410 ASSERT(bp != NULL); 12411 xp = SD_GET_XBUF(bp); 12412 ASSERT(xp != NULL); 12413 12414 #if defined(__sparc) 12415 /* 12416 * Call bp_mapout() before freeing the buf, in case a lower 12417 * layer or HBA had done a bp_mapin(). we must do this here 12418 * as we are the "originator" of the shadow buf. 12419 */ 12420 bp_mapout(bp); 12421 #endif 12422 12423 /* 12424 * Null out b_iodone before freeing the bp, to ensure that the driver 12425 * never gets confused by a stale value in this field. (Just a little 12426 * extra defensiveness here.) 12427 */ 12428 bp->b_iodone = NULL; 12429 12430 #if defined(__i386) || defined(__amd64) 12431 kmem_free(bp->b_un.b_addr, bp->b_bcount); 12432 freerbuf(bp); 12433 #else 12434 scsi_free_consistent_buf(bp); 12435 #endif 12436 12437 kmem_free(xp, sizeof (struct sd_xbuf)); 12438 } 12439 12440 12441 /* 12442 * Function: sd_print_transport_rejected_message 12443 * 12444 * Description: This implements the ludicrously complex rules for printing 12445 * a "transport rejected" message. This is to address the 12446 * specific problem of having a flood of this error message 12447 * produced when a failover occurs. 12448 * 12449 * Context: Any. 12450 */ 12451 12452 static void 12453 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 12454 int code) 12455 { 12456 ASSERT(un != NULL); 12457 ASSERT(mutex_owned(SD_MUTEX(un))); 12458 ASSERT(xp != NULL); 12459 12460 /* 12461 * Print the "transport rejected" message under the following 12462 * conditions: 12463 * 12464 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 12465 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 12466 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 12467 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 12468 * scsi_transport(9F) (which indicates that the target might have 12469 * gone off-line). This uses the un->un_tran_fatal_count 12470 * count, which is incremented whenever a TRAN_FATAL_ERROR is 12471 * received, and reset to zero whenver a TRAN_ACCEPT is returned 12472 * from scsi_transport(). 12473 * 12474 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 12475 * the preceeding cases in order for the message to be printed. 12476 */ 12477 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 12478 if ((sd_level_mask & SD_LOGMASK_DIAG) || 12479 (code != TRAN_FATAL_ERROR) || 12480 (un->un_tran_fatal_count == 1)) { 12481 switch (code) { 12482 case TRAN_BADPKT: 12483 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12484 "transport rejected bad packet\n"); 12485 break; 12486 case TRAN_FATAL_ERROR: 12487 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12488 "transport rejected fatal error\n"); 12489 break; 12490 default: 12491 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12492 "transport rejected (%d)\n", code); 12493 break; 12494 } 12495 } 12496 } 12497 } 12498 12499 12500 /* 12501 * Function: sd_add_buf_to_waitq 12502 * 12503 * Description: Add the given buf(9S) struct to the wait queue for the 12504 * instance. If sorting is enabled, then the buf is added 12505 * to the queue via an elevator sort algorithm (a la 12506 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 12507 * If sorting is not enabled, then the buf is just added 12508 * to the end of the wait queue. 12509 * 12510 * Return Code: void 12511 * 12512 * Context: Does not sleep/block, therefore technically can be called 12513 * from any context. However if sorting is enabled then the 12514 * execution time is indeterminate, and may take long if 12515 * the wait queue grows large. 12516 */ 12517 12518 static void 12519 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 12520 { 12521 struct buf *ap; 12522 12523 ASSERT(bp != NULL); 12524 ASSERT(un != NULL); 12525 ASSERT(mutex_owned(SD_MUTEX(un))); 12526 12527 /* If the queue is empty, add the buf as the only entry & return. */ 12528 if (un->un_waitq_headp == NULL) { 12529 ASSERT(un->un_waitq_tailp == NULL); 12530 un->un_waitq_headp = un->un_waitq_tailp = bp; 12531 bp->av_forw = NULL; 12532 return; 12533 } 12534 12535 ASSERT(un->un_waitq_tailp != NULL); 12536 12537 /* 12538 * If sorting is disabled, just add the buf to the tail end of 12539 * the wait queue and return. 12540 */ 12541 if (un->un_f_disksort_disabled) { 12542 un->un_waitq_tailp->av_forw = bp; 12543 un->un_waitq_tailp = bp; 12544 bp->av_forw = NULL; 12545 return; 12546 } 12547 12548 /* 12549 * Sort thru the list of requests currently on the wait queue 12550 * and add the new buf request at the appropriate position. 12551 * 12552 * The un->un_waitq_headp is an activity chain pointer on which 12553 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 12554 * first queue holds those requests which are positioned after 12555 * the current SD_GET_BLKNO() (in the first request); the second holds 12556 * requests which came in after their SD_GET_BLKNO() number was passed. 12557 * Thus we implement a one way scan, retracting after reaching 12558 * the end of the drive to the first request on the second 12559 * queue, at which time it becomes the first queue. 12560 * A one-way scan is natural because of the way UNIX read-ahead 12561 * blocks are allocated. 12562 * 12563 * If we lie after the first request, then we must locate the 12564 * second request list and add ourselves to it. 12565 */ 12566 ap = un->un_waitq_headp; 12567 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 12568 while (ap->av_forw != NULL) { 12569 /* 12570 * Look for an "inversion" in the (normally 12571 * ascending) block numbers. This indicates 12572 * the start of the second request list. 12573 */ 12574 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 12575 /* 12576 * Search the second request list for the 12577 * first request at a larger block number. 12578 * We go before that; however if there is 12579 * no such request, we go at the end. 12580 */ 12581 do { 12582 if (SD_GET_BLKNO(bp) < 12583 SD_GET_BLKNO(ap->av_forw)) { 12584 goto insert; 12585 } 12586 ap = ap->av_forw; 12587 } while (ap->av_forw != NULL); 12588 goto insert; /* after last */ 12589 } 12590 ap = ap->av_forw; 12591 } 12592 12593 /* 12594 * No inversions... we will go after the last, and 12595 * be the first request in the second request list. 12596 */ 12597 goto insert; 12598 } 12599 12600 /* 12601 * Request is at/after the current request... 12602 * sort in the first request list. 12603 */ 12604 while (ap->av_forw != NULL) { 12605 /* 12606 * We want to go after the current request (1) if 12607 * there is an inversion after it (i.e. it is the end 12608 * of the first request list), or (2) if the next 12609 * request is a larger block no. than our request. 12610 */ 12611 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 12612 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 12613 goto insert; 12614 } 12615 ap = ap->av_forw; 12616 } 12617 12618 /* 12619 * Neither a second list nor a larger request, therefore 12620 * we go at the end of the first list (which is the same 12621 * as the end of the whole schebang). 12622 */ 12623 insert: 12624 bp->av_forw = ap->av_forw; 12625 ap->av_forw = bp; 12626 12627 /* 12628 * If we inserted onto the tail end of the waitq, make sure the 12629 * tail pointer is updated. 12630 */ 12631 if (ap == un->un_waitq_tailp) { 12632 un->un_waitq_tailp = bp; 12633 } 12634 } 12635 12636 12637 /* 12638 * Function: sd_start_cmds 12639 * 12640 * Description: Remove and transport cmds from the driver queues. 12641 * 12642 * Arguments: un - pointer to the unit (soft state) struct for the target. 12643 * 12644 * immed_bp - ptr to a buf to be transported immediately. Only 12645 * the immed_bp is transported; bufs on the waitq are not 12646 * processed and the un_retry_bp is not checked. If immed_bp is 12647 * NULL, then normal queue processing is performed. 12648 * 12649 * Context: May be called from kernel thread context, interrupt context, 12650 * or runout callback context. This function may not block or 12651 * call routines that block. 12652 */ 12653 12654 static void 12655 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 12656 { 12657 struct sd_xbuf *xp; 12658 struct buf *bp; 12659 void (*statp)(kstat_io_t *); 12660 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12661 void (*saved_statp)(kstat_io_t *); 12662 #endif 12663 int rval; 12664 12665 ASSERT(un != NULL); 12666 ASSERT(mutex_owned(SD_MUTEX(un))); 12667 ASSERT(un->un_ncmds_in_transport >= 0); 12668 ASSERT(un->un_throttle >= 0); 12669 12670 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 12671 12672 do { 12673 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12674 saved_statp = NULL; 12675 #endif 12676 12677 /* 12678 * If we are syncing or dumping, fail the command to 12679 * avoid recursively calling back into scsi_transport(). 12680 * The dump I/O itself uses a separate code path so this 12681 * only prevents non-dump I/O from being sent while dumping. 12682 * File system sync takes place before dumping begins. 12683 * During panic, filesystem I/O is allowed provided 12684 * un_in_callback is <= 1. This is to prevent recursion 12685 * such as sd_start_cmds -> scsi_transport -> sdintr -> 12686 * sd_start_cmds and so on. See panic.c for more information 12687 * about the states the system can be in during panic. 12688 */ 12689 if ((un->un_state == SD_STATE_DUMPING) || 12690 (ddi_in_panic() && (un->un_in_callback > 1))) { 12691 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12692 "sd_start_cmds: panicking\n"); 12693 goto exit; 12694 } 12695 12696 if ((bp = immed_bp) != NULL) { 12697 /* 12698 * We have a bp that must be transported immediately. 12699 * It's OK to transport the immed_bp here without doing 12700 * the throttle limit check because the immed_bp is 12701 * always used in a retry/recovery case. This means 12702 * that we know we are not at the throttle limit by 12703 * virtue of the fact that to get here we must have 12704 * already gotten a command back via sdintr(). This also 12705 * relies on (1) the command on un_retry_bp preventing 12706 * further commands from the waitq from being issued; 12707 * and (2) the code in sd_retry_command checking the 12708 * throttle limit before issuing a delayed or immediate 12709 * retry. This holds even if the throttle limit is 12710 * currently ratcheted down from its maximum value. 12711 */ 12712 statp = kstat_runq_enter; 12713 if (bp == un->un_retry_bp) { 12714 ASSERT((un->un_retry_statp == NULL) || 12715 (un->un_retry_statp == kstat_waitq_enter) || 12716 (un->un_retry_statp == 12717 kstat_runq_back_to_waitq)); 12718 /* 12719 * If the waitq kstat was incremented when 12720 * sd_set_retry_bp() queued this bp for a retry, 12721 * then we must set up statp so that the waitq 12722 * count will get decremented correctly below. 12723 * Also we must clear un->un_retry_statp to 12724 * ensure that we do not act on a stale value 12725 * in this field. 12726 */ 12727 if ((un->un_retry_statp == kstat_waitq_enter) || 12728 (un->un_retry_statp == 12729 kstat_runq_back_to_waitq)) { 12730 statp = kstat_waitq_to_runq; 12731 } 12732 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12733 saved_statp = un->un_retry_statp; 12734 #endif 12735 un->un_retry_statp = NULL; 12736 12737 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 12738 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 12739 "un_throttle:%d un_ncmds_in_transport:%d\n", 12740 un, un->un_retry_bp, un->un_throttle, 12741 un->un_ncmds_in_transport); 12742 } else { 12743 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 12744 "processing priority bp:0x%p\n", bp); 12745 } 12746 12747 } else if ((bp = un->un_waitq_headp) != NULL) { 12748 /* 12749 * A command on the waitq is ready to go, but do not 12750 * send it if: 12751 * 12752 * (1) the throttle limit has been reached, or 12753 * (2) a retry is pending, or 12754 * (3) a START_STOP_UNIT callback pending, or 12755 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 12756 * command is pending. 12757 * 12758 * For all of these conditions, IO processing will 12759 * restart after the condition is cleared. 12760 */ 12761 if (un->un_ncmds_in_transport >= un->un_throttle) { 12762 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12763 "sd_start_cmds: exiting, " 12764 "throttle limit reached!\n"); 12765 goto exit; 12766 } 12767 if (un->un_retry_bp != NULL) { 12768 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12769 "sd_start_cmds: exiting, retry pending!\n"); 12770 goto exit; 12771 } 12772 if (un->un_startstop_timeid != NULL) { 12773 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12774 "sd_start_cmds: exiting, " 12775 "START_STOP pending!\n"); 12776 goto exit; 12777 } 12778 if (un->un_direct_priority_timeid != NULL) { 12779 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12780 "sd_start_cmds: exiting, " 12781 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 12782 goto exit; 12783 } 12784 12785 /* Dequeue the command */ 12786 un->un_waitq_headp = bp->av_forw; 12787 if (un->un_waitq_headp == NULL) { 12788 un->un_waitq_tailp = NULL; 12789 } 12790 bp->av_forw = NULL; 12791 statp = kstat_waitq_to_runq; 12792 SD_TRACE(SD_LOG_IO_CORE, un, 12793 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 12794 12795 } else { 12796 /* No work to do so bail out now */ 12797 SD_TRACE(SD_LOG_IO_CORE, un, 12798 "sd_start_cmds: no more work, exiting!\n"); 12799 goto exit; 12800 } 12801 12802 /* 12803 * Reset the state to normal. This is the mechanism by which 12804 * the state transitions from either SD_STATE_RWAIT or 12805 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 12806 * If state is SD_STATE_PM_CHANGING then this command is 12807 * part of the device power control and the state must 12808 * not be put back to normal. Doing so would would 12809 * allow new commands to proceed when they shouldn't, 12810 * the device may be going off. 12811 */ 12812 if ((un->un_state != SD_STATE_SUSPENDED) && 12813 (un->un_state != SD_STATE_PM_CHANGING)) { 12814 New_state(un, SD_STATE_NORMAL); 12815 } 12816 12817 xp = SD_GET_XBUF(bp); 12818 ASSERT(xp != NULL); 12819 12820 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12821 /* 12822 * Allocate the scsi_pkt if we need one, or attach DMA 12823 * resources if we have a scsi_pkt that needs them. The 12824 * latter should only occur for commands that are being 12825 * retried. 12826 */ 12827 if ((xp->xb_pktp == NULL) || 12828 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 12829 #else 12830 if (xp->xb_pktp == NULL) { 12831 #endif 12832 /* 12833 * There is no scsi_pkt allocated for this buf. Call 12834 * the initpkt function to allocate & init one. 12835 * 12836 * The scsi_init_pkt runout callback functionality is 12837 * implemented as follows: 12838 * 12839 * 1) The initpkt function always calls 12840 * scsi_init_pkt(9F) with sdrunout specified as the 12841 * callback routine. 12842 * 2) A successful packet allocation is initialized and 12843 * the I/O is transported. 12844 * 3) The I/O associated with an allocation resource 12845 * failure is left on its queue to be retried via 12846 * runout or the next I/O. 12847 * 4) The I/O associated with a DMA error is removed 12848 * from the queue and failed with EIO. Processing of 12849 * the transport queues is also halted to be 12850 * restarted via runout or the next I/O. 12851 * 5) The I/O associated with a CDB size or packet 12852 * size error is removed from the queue and failed 12853 * with EIO. Processing of the transport queues is 12854 * continued. 12855 * 12856 * Note: there is no interface for canceling a runout 12857 * callback. To prevent the driver from detaching or 12858 * suspending while a runout is pending the driver 12859 * state is set to SD_STATE_RWAIT 12860 * 12861 * Note: using the scsi_init_pkt callback facility can 12862 * result in an I/O request persisting at the head of 12863 * the list which cannot be satisfied even after 12864 * multiple retries. In the future the driver may 12865 * implement some kind of maximum runout count before 12866 * failing an I/O. 12867 * 12868 * Note: the use of funcp below may seem superfluous, 12869 * but it helps warlock figure out the correct 12870 * initpkt function calls (see [s]sd.wlcmd). 12871 */ 12872 struct scsi_pkt *pktp; 12873 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 12874 12875 ASSERT(bp != un->un_rqs_bp); 12876 12877 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 12878 switch ((*funcp)(bp, &pktp)) { 12879 case SD_PKT_ALLOC_SUCCESS: 12880 xp->xb_pktp = pktp; 12881 SD_TRACE(SD_LOG_IO_CORE, un, 12882 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 12883 pktp); 12884 goto got_pkt; 12885 12886 case SD_PKT_ALLOC_FAILURE: 12887 /* 12888 * Temporary (hopefully) resource depletion. 12889 * Since retries and RQS commands always have a 12890 * scsi_pkt allocated, these cases should never 12891 * get here. So the only cases this needs to 12892 * handle is a bp from the waitq (which we put 12893 * back onto the waitq for sdrunout), or a bp 12894 * sent as an immed_bp (which we just fail). 12895 */ 12896 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12897 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 12898 12899 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12900 12901 if (bp == immed_bp) { 12902 /* 12903 * If SD_XB_DMA_FREED is clear, then 12904 * this is a failure to allocate a 12905 * scsi_pkt, and we must fail the 12906 * command. 12907 */ 12908 if ((xp->xb_pkt_flags & 12909 SD_XB_DMA_FREED) == 0) { 12910 break; 12911 } 12912 12913 /* 12914 * If this immediate command is NOT our 12915 * un_retry_bp, then we must fail it. 12916 */ 12917 if (bp != un->un_retry_bp) { 12918 break; 12919 } 12920 12921 /* 12922 * We get here if this cmd is our 12923 * un_retry_bp that was DMAFREED, but 12924 * scsi_init_pkt() failed to reallocate 12925 * DMA resources when we attempted to 12926 * retry it. This can happen when an 12927 * mpxio failover is in progress, but 12928 * we don't want to just fail the 12929 * command in this case. 12930 * 12931 * Use timeout(9F) to restart it after 12932 * a 100ms delay. We don't want to 12933 * let sdrunout() restart it, because 12934 * sdrunout() is just supposed to start 12935 * commands that are sitting on the 12936 * wait queue. The un_retry_bp stays 12937 * set until the command completes, but 12938 * sdrunout can be called many times 12939 * before that happens. Since sdrunout 12940 * cannot tell if the un_retry_bp is 12941 * already in the transport, it could 12942 * end up calling scsi_transport() for 12943 * the un_retry_bp multiple times. 12944 * 12945 * Also: don't schedule the callback 12946 * if some other callback is already 12947 * pending. 12948 */ 12949 if (un->un_retry_statp == NULL) { 12950 /* 12951 * restore the kstat pointer to 12952 * keep kstat counts coherent 12953 * when we do retry the command. 12954 */ 12955 un->un_retry_statp = 12956 saved_statp; 12957 } 12958 12959 if ((un->un_startstop_timeid == NULL) && 12960 (un->un_retry_timeid == NULL) && 12961 (un->un_direct_priority_timeid == 12962 NULL)) { 12963 12964 un->un_retry_timeid = 12965 timeout( 12966 sd_start_retry_command, 12967 un, SD_RESTART_TIMEOUT); 12968 } 12969 goto exit; 12970 } 12971 12972 #else 12973 if (bp == immed_bp) { 12974 break; /* Just fail the command */ 12975 } 12976 #endif 12977 12978 /* Add the buf back to the head of the waitq */ 12979 bp->av_forw = un->un_waitq_headp; 12980 un->un_waitq_headp = bp; 12981 if (un->un_waitq_tailp == NULL) { 12982 un->un_waitq_tailp = bp; 12983 } 12984 goto exit; 12985 12986 case SD_PKT_ALLOC_FAILURE_NO_DMA: 12987 /* 12988 * HBA DMA resource failure. Fail the command 12989 * and continue processing of the queues. 12990 */ 12991 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12992 "sd_start_cmds: " 12993 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 12994 break; 12995 12996 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 12997 /* 12998 * Note:x86: Partial DMA mapping not supported 12999 * for USCSI commands, and all the needed DMA 13000 * resources were not allocated. 13001 */ 13002 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13003 "sd_start_cmds: " 13004 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 13005 break; 13006 13007 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 13008 /* 13009 * Note:x86: Request cannot fit into CDB based 13010 * on lba and len. 13011 */ 13012 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13013 "sd_start_cmds: " 13014 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 13015 break; 13016 13017 default: 13018 /* Should NEVER get here! */ 13019 panic("scsi_initpkt error"); 13020 /*NOTREACHED*/ 13021 } 13022 13023 /* 13024 * Fatal error in allocating a scsi_pkt for this buf. 13025 * Update kstats & return the buf with an error code. 13026 * We must use sd_return_failed_command_no_restart() to 13027 * avoid a recursive call back into sd_start_cmds(). 13028 * However this also means that we must keep processing 13029 * the waitq here in order to avoid stalling. 13030 */ 13031 if (statp == kstat_waitq_to_runq) { 13032 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 13033 } 13034 sd_return_failed_command_no_restart(un, bp, EIO); 13035 if (bp == immed_bp) { 13036 /* immed_bp is gone by now, so clear this */ 13037 immed_bp = NULL; 13038 } 13039 continue; 13040 } 13041 got_pkt: 13042 if (bp == immed_bp) { 13043 /* goto the head of the class.... */ 13044 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13045 } 13046 13047 un->un_ncmds_in_transport++; 13048 SD_UPDATE_KSTATS(un, statp, bp); 13049 13050 /* 13051 * Call scsi_transport() to send the command to the target. 13052 * According to SCSA architecture, we must drop the mutex here 13053 * before calling scsi_transport() in order to avoid deadlock. 13054 * Note that the scsi_pkt's completion routine can be executed 13055 * (from interrupt context) even before the call to 13056 * scsi_transport() returns. 13057 */ 13058 SD_TRACE(SD_LOG_IO_CORE, un, 13059 "sd_start_cmds: calling scsi_transport()\n"); 13060 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 13061 13062 mutex_exit(SD_MUTEX(un)); 13063 rval = scsi_transport(xp->xb_pktp); 13064 mutex_enter(SD_MUTEX(un)); 13065 13066 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13067 "sd_start_cmds: scsi_transport() returned %d\n", rval); 13068 13069 switch (rval) { 13070 case TRAN_ACCEPT: 13071 /* Clear this with every pkt accepted by the HBA */ 13072 un->un_tran_fatal_count = 0; 13073 break; /* Success; try the next cmd (if any) */ 13074 13075 case TRAN_BUSY: 13076 un->un_ncmds_in_transport--; 13077 ASSERT(un->un_ncmds_in_transport >= 0); 13078 13079 /* 13080 * Don't retry request sense, the sense data 13081 * is lost when another request is sent. 13082 * Free up the rqs buf and retry 13083 * the original failed cmd. Update kstat. 13084 */ 13085 if (bp == un->un_rqs_bp) { 13086 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13087 bp = sd_mark_rqs_idle(un, xp); 13088 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 13089 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 13090 kstat_waitq_enter); 13091 goto exit; 13092 } 13093 13094 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13095 /* 13096 * Free the DMA resources for the scsi_pkt. This will 13097 * allow mpxio to select another path the next time 13098 * we call scsi_transport() with this scsi_pkt. 13099 * See sdintr() for the rationalization behind this. 13100 */ 13101 if ((un->un_f_is_fibre == TRUE) && 13102 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 13103 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 13104 scsi_dmafree(xp->xb_pktp); 13105 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 13106 } 13107 #endif 13108 13109 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 13110 /* 13111 * Commands that are SD_PATH_DIRECT_PRIORITY 13112 * are for error recovery situations. These do 13113 * not use the normal command waitq, so if they 13114 * get a TRAN_BUSY we cannot put them back onto 13115 * the waitq for later retry. One possible 13116 * problem is that there could already be some 13117 * other command on un_retry_bp that is waiting 13118 * for this one to complete, so we would be 13119 * deadlocked if we put this command back onto 13120 * the waitq for later retry (since un_retry_bp 13121 * must complete before the driver gets back to 13122 * commands on the waitq). 13123 * 13124 * To avoid deadlock we must schedule a callback 13125 * that will restart this command after a set 13126 * interval. This should keep retrying for as 13127 * long as the underlying transport keeps 13128 * returning TRAN_BUSY (just like for other 13129 * commands). Use the same timeout interval as 13130 * for the ordinary TRAN_BUSY retry. 13131 */ 13132 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13133 "sd_start_cmds: scsi_transport() returned " 13134 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 13135 13136 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13137 un->un_direct_priority_timeid = 13138 timeout(sd_start_direct_priority_command, 13139 bp, SD_BSY_TIMEOUT / 500); 13140 13141 goto exit; 13142 } 13143 13144 /* 13145 * For TRAN_BUSY, we want to reduce the throttle value, 13146 * unless we are retrying a command. 13147 */ 13148 if (bp != un->un_retry_bp) { 13149 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 13150 } 13151 13152 /* 13153 * Set up the bp to be tried again 10 ms later. 13154 * Note:x86: Is there a timeout value in the sd_lun 13155 * for this condition? 13156 */ 13157 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 13158 kstat_runq_back_to_waitq); 13159 goto exit; 13160 13161 case TRAN_FATAL_ERROR: 13162 un->un_tran_fatal_count++; 13163 /* FALLTHRU */ 13164 13165 case TRAN_BADPKT: 13166 default: 13167 un->un_ncmds_in_transport--; 13168 ASSERT(un->un_ncmds_in_transport >= 0); 13169 13170 /* 13171 * If this is our REQUEST SENSE command with a 13172 * transport error, we must get back the pointers 13173 * to the original buf, and mark the REQUEST 13174 * SENSE command as "available". 13175 */ 13176 if (bp == un->un_rqs_bp) { 13177 bp = sd_mark_rqs_idle(un, xp); 13178 xp = SD_GET_XBUF(bp); 13179 } else { 13180 /* 13181 * Legacy behavior: do not update transport 13182 * error count for request sense commands. 13183 */ 13184 SD_UPDATE_ERRSTATS(un, sd_transerrs); 13185 } 13186 13187 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13188 sd_print_transport_rejected_message(un, xp, rval); 13189 13190 /* 13191 * We must use sd_return_failed_command_no_restart() to 13192 * avoid a recursive call back into sd_start_cmds(). 13193 * However this also means that we must keep processing 13194 * the waitq here in order to avoid stalling. 13195 */ 13196 sd_return_failed_command_no_restart(un, bp, EIO); 13197 13198 /* 13199 * Notify any threads waiting in sd_ddi_suspend() that 13200 * a command completion has occurred. 13201 */ 13202 if (un->un_state == SD_STATE_SUSPENDED) { 13203 cv_broadcast(&un->un_disk_busy_cv); 13204 } 13205 13206 if (bp == immed_bp) { 13207 /* immed_bp is gone by now, so clear this */ 13208 immed_bp = NULL; 13209 } 13210 break; 13211 } 13212 13213 } while (immed_bp == NULL); 13214 13215 exit: 13216 ASSERT(mutex_owned(SD_MUTEX(un))); 13217 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 13218 } 13219 13220 13221 /* 13222 * Function: sd_return_command 13223 * 13224 * Description: Returns a command to its originator (with or without an 13225 * error). Also starts commands waiting to be transported 13226 * to the target. 13227 * 13228 * Context: May be called from interrupt, kernel, or timeout context 13229 */ 13230 13231 static void 13232 sd_return_command(struct sd_lun *un, struct buf *bp) 13233 { 13234 struct sd_xbuf *xp; 13235 #if defined(__i386) || defined(__amd64) 13236 struct scsi_pkt *pktp; 13237 #endif 13238 13239 ASSERT(bp != NULL); 13240 ASSERT(un != NULL); 13241 ASSERT(mutex_owned(SD_MUTEX(un))); 13242 ASSERT(bp != un->un_rqs_bp); 13243 xp = SD_GET_XBUF(bp); 13244 ASSERT(xp != NULL); 13245 13246 #if defined(__i386) || defined(__amd64) 13247 pktp = SD_GET_PKTP(bp); 13248 #endif 13249 13250 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 13251 13252 #if defined(__i386) || defined(__amd64) 13253 /* 13254 * Note:x86: check for the "sdrestart failed" case. 13255 */ 13256 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 13257 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 13258 (xp->xb_pktp->pkt_resid == 0)) { 13259 13260 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 13261 /* 13262 * Successfully set up next portion of cmd 13263 * transfer, try sending it 13264 */ 13265 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 13266 NULL, NULL, 0, (clock_t)0, NULL); 13267 sd_start_cmds(un, NULL); 13268 return; /* Note:x86: need a return here? */ 13269 } 13270 } 13271 #endif 13272 13273 /* 13274 * If this is the failfast bp, clear it from un_failfast_bp. This 13275 * can happen if upon being re-tried the failfast bp either 13276 * succeeded or encountered another error (possibly even a different 13277 * error than the one that precipitated the failfast state, but in 13278 * that case it would have had to exhaust retries as well). Regardless, 13279 * this should not occur whenever the instance is in the active 13280 * failfast state. 13281 */ 13282 if (bp == un->un_failfast_bp) { 13283 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13284 un->un_failfast_bp = NULL; 13285 } 13286 13287 /* 13288 * Clear the failfast state upon successful completion of ANY cmd. 13289 */ 13290 if (bp->b_error == 0) { 13291 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13292 } 13293 13294 /* 13295 * This is used if the command was retried one or more times. Show that 13296 * we are done with it, and allow processing of the waitq to resume. 13297 */ 13298 if (bp == un->un_retry_bp) { 13299 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13300 "sd_return_command: un:0x%p: " 13301 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13302 un->un_retry_bp = NULL; 13303 un->un_retry_statp = NULL; 13304 } 13305 13306 SD_UPDATE_RDWR_STATS(un, bp); 13307 SD_UPDATE_PARTITION_STATS(un, bp); 13308 13309 switch (un->un_state) { 13310 case SD_STATE_SUSPENDED: 13311 /* 13312 * Notify any threads waiting in sd_ddi_suspend() that 13313 * a command completion has occurred. 13314 */ 13315 cv_broadcast(&un->un_disk_busy_cv); 13316 break; 13317 default: 13318 sd_start_cmds(un, NULL); 13319 break; 13320 } 13321 13322 /* Return this command up the iodone chain to its originator. */ 13323 mutex_exit(SD_MUTEX(un)); 13324 13325 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13326 xp->xb_pktp = NULL; 13327 13328 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13329 13330 ASSERT(!mutex_owned(SD_MUTEX(un))); 13331 mutex_enter(SD_MUTEX(un)); 13332 13333 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 13334 } 13335 13336 13337 /* 13338 * Function: sd_return_failed_command 13339 * 13340 * Description: Command completion when an error occurred. 13341 * 13342 * Context: May be called from interrupt context 13343 */ 13344 13345 static void 13346 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 13347 { 13348 ASSERT(bp != NULL); 13349 ASSERT(un != NULL); 13350 ASSERT(mutex_owned(SD_MUTEX(un))); 13351 13352 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13353 "sd_return_failed_command: entry\n"); 13354 13355 /* 13356 * b_resid could already be nonzero due to a partial data 13357 * transfer, so do not change it here. 13358 */ 13359 SD_BIOERROR(bp, errcode); 13360 13361 sd_return_command(un, bp); 13362 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13363 "sd_return_failed_command: exit\n"); 13364 } 13365 13366 13367 /* 13368 * Function: sd_return_failed_command_no_restart 13369 * 13370 * Description: Same as sd_return_failed_command, but ensures that no 13371 * call back into sd_start_cmds will be issued. 13372 * 13373 * Context: May be called from interrupt context 13374 */ 13375 13376 static void 13377 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 13378 int errcode) 13379 { 13380 struct sd_xbuf *xp; 13381 13382 ASSERT(bp != NULL); 13383 ASSERT(un != NULL); 13384 ASSERT(mutex_owned(SD_MUTEX(un))); 13385 xp = SD_GET_XBUF(bp); 13386 ASSERT(xp != NULL); 13387 ASSERT(errcode != 0); 13388 13389 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13390 "sd_return_failed_command_no_restart: entry\n"); 13391 13392 /* 13393 * b_resid could already be nonzero due to a partial data 13394 * transfer, so do not change it here. 13395 */ 13396 SD_BIOERROR(bp, errcode); 13397 13398 /* 13399 * If this is the failfast bp, clear it. This can happen if the 13400 * failfast bp encounterd a fatal error when we attempted to 13401 * re-try it (such as a scsi_transport(9F) failure). However 13402 * we should NOT be in an active failfast state if the failfast 13403 * bp is not NULL. 13404 */ 13405 if (bp == un->un_failfast_bp) { 13406 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13407 un->un_failfast_bp = NULL; 13408 } 13409 13410 if (bp == un->un_retry_bp) { 13411 /* 13412 * This command was retried one or more times. Show that we are 13413 * done with it, and allow processing of the waitq to resume. 13414 */ 13415 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13416 "sd_return_failed_command_no_restart: " 13417 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13418 un->un_retry_bp = NULL; 13419 un->un_retry_statp = NULL; 13420 } 13421 13422 SD_UPDATE_RDWR_STATS(un, bp); 13423 SD_UPDATE_PARTITION_STATS(un, bp); 13424 13425 mutex_exit(SD_MUTEX(un)); 13426 13427 if (xp->xb_pktp != NULL) { 13428 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13429 xp->xb_pktp = NULL; 13430 } 13431 13432 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13433 13434 mutex_enter(SD_MUTEX(un)); 13435 13436 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13437 "sd_return_failed_command_no_restart: exit\n"); 13438 } 13439 13440 13441 /* 13442 * Function: sd_retry_command 13443 * 13444 * Description: queue up a command for retry, or (optionally) fail it 13445 * if retry counts are exhausted. 13446 * 13447 * Arguments: un - Pointer to the sd_lun struct for the target. 13448 * 13449 * bp - Pointer to the buf for the command to be retried. 13450 * 13451 * retry_check_flag - Flag to see which (if any) of the retry 13452 * counts should be decremented/checked. If the indicated 13453 * retry count is exhausted, then the command will not be 13454 * retried; it will be failed instead. This should use a 13455 * value equal to one of the following: 13456 * 13457 * SD_RETRIES_NOCHECK 13458 * SD_RESD_RETRIES_STANDARD 13459 * SD_RETRIES_VICTIM 13460 * 13461 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 13462 * if the check should be made to see of FLAG_ISOLATE is set 13463 * in the pkt. If FLAG_ISOLATE is set, then the command is 13464 * not retried, it is simply failed. 13465 * 13466 * user_funcp - Ptr to function to call before dispatching the 13467 * command. May be NULL if no action needs to be performed. 13468 * (Primarily intended for printing messages.) 13469 * 13470 * user_arg - Optional argument to be passed along to 13471 * the user_funcp call. 13472 * 13473 * failure_code - errno return code to set in the bp if the 13474 * command is going to be failed. 13475 * 13476 * retry_delay - Retry delay interval in (clock_t) units. May 13477 * be zero which indicates that the retry should be retried 13478 * immediately (ie, without an intervening delay). 13479 * 13480 * statp - Ptr to kstat function to be updated if the command 13481 * is queued for a delayed retry. May be NULL if no kstat 13482 * update is desired. 13483 * 13484 * Context: May be called from interrupt context. 13485 */ 13486 13487 static void 13488 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 13489 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 13490 code), void *user_arg, int failure_code, clock_t retry_delay, 13491 void (*statp)(kstat_io_t *)) 13492 { 13493 struct sd_xbuf *xp; 13494 struct scsi_pkt *pktp; 13495 13496 ASSERT(un != NULL); 13497 ASSERT(mutex_owned(SD_MUTEX(un))); 13498 ASSERT(bp != NULL); 13499 xp = SD_GET_XBUF(bp); 13500 ASSERT(xp != NULL); 13501 pktp = SD_GET_PKTP(bp); 13502 ASSERT(pktp != NULL); 13503 13504 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 13505 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 13506 13507 /* 13508 * If we are syncing or dumping, fail the command to avoid 13509 * recursively calling back into scsi_transport(). 13510 */ 13511 if (ddi_in_panic()) { 13512 goto fail_command_no_log; 13513 } 13514 13515 /* 13516 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 13517 * log an error and fail the command. 13518 */ 13519 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 13520 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 13521 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 13522 sd_dump_memory(un, SD_LOG_IO, "CDB", 13523 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 13524 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 13525 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 13526 goto fail_command; 13527 } 13528 13529 /* 13530 * If we are suspended, then put the command onto head of the 13531 * wait queue since we don't want to start more commands, and 13532 * clear the un_retry_bp. Next time when we are resumed, will 13533 * handle the command in the wait queue. 13534 */ 13535 switch (un->un_state) { 13536 case SD_STATE_SUSPENDED: 13537 case SD_STATE_DUMPING: 13538 bp->av_forw = un->un_waitq_headp; 13539 un->un_waitq_headp = bp; 13540 if (un->un_waitq_tailp == NULL) { 13541 un->un_waitq_tailp = bp; 13542 } 13543 if (bp == un->un_retry_bp) { 13544 un->un_retry_bp = NULL; 13545 un->un_retry_statp = NULL; 13546 } 13547 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13548 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 13549 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 13550 return; 13551 default: 13552 break; 13553 } 13554 13555 /* 13556 * If the caller wants us to check FLAG_ISOLATE, then see if that 13557 * is set; if it is then we do not want to retry the command. 13558 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 13559 */ 13560 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 13561 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 13562 goto fail_command; 13563 } 13564 } 13565 13566 13567 /* 13568 * If SD_RETRIES_FAILFAST is set, it indicates that either a 13569 * command timeout or a selection timeout has occurred. This means 13570 * that we were unable to establish an kind of communication with 13571 * the target, and subsequent retries and/or commands are likely 13572 * to encounter similar results and take a long time to complete. 13573 * 13574 * If this is a failfast error condition, we need to update the 13575 * failfast state, even if this bp does not have B_FAILFAST set. 13576 */ 13577 if (retry_check_flag & SD_RETRIES_FAILFAST) { 13578 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 13579 ASSERT(un->un_failfast_bp == NULL); 13580 /* 13581 * If we are already in the active failfast state, and 13582 * another failfast error condition has been detected, 13583 * then fail this command if it has B_FAILFAST set. 13584 * If B_FAILFAST is clear, then maintain the legacy 13585 * behavior of retrying heroically, even tho this will 13586 * take a lot more time to fail the command. 13587 */ 13588 if (bp->b_flags & B_FAILFAST) { 13589 goto fail_command; 13590 } 13591 } else { 13592 /* 13593 * We're not in the active failfast state, but we 13594 * have a failfast error condition, so we must begin 13595 * transition to the next state. We do this regardless 13596 * of whether or not this bp has B_FAILFAST set. 13597 */ 13598 if (un->un_failfast_bp == NULL) { 13599 /* 13600 * This is the first bp to meet a failfast 13601 * condition so save it on un_failfast_bp & 13602 * do normal retry processing. Do not enter 13603 * active failfast state yet. This marks 13604 * entry into the "failfast pending" state. 13605 */ 13606 un->un_failfast_bp = bp; 13607 13608 } else if (un->un_failfast_bp == bp) { 13609 /* 13610 * This is the second time *this* bp has 13611 * encountered a failfast error condition, 13612 * so enter active failfast state & flush 13613 * queues as appropriate. 13614 */ 13615 un->un_failfast_state = SD_FAILFAST_ACTIVE; 13616 un->un_failfast_bp = NULL; 13617 sd_failfast_flushq(un); 13618 13619 /* 13620 * Fail this bp now if B_FAILFAST set; 13621 * otherwise continue with retries. (It would 13622 * be pretty ironic if this bp succeeded on a 13623 * subsequent retry after we just flushed all 13624 * the queues). 13625 */ 13626 if (bp->b_flags & B_FAILFAST) { 13627 goto fail_command; 13628 } 13629 13630 #if !defined(lint) && !defined(__lint) 13631 } else { 13632 /* 13633 * If neither of the preceeding conditionals 13634 * was true, it means that there is some 13635 * *other* bp that has met an inital failfast 13636 * condition and is currently either being 13637 * retried or is waiting to be retried. In 13638 * that case we should perform normal retry 13639 * processing on *this* bp, since there is a 13640 * chance that the current failfast condition 13641 * is transient and recoverable. If that does 13642 * not turn out to be the case, then retries 13643 * will be cleared when the wait queue is 13644 * flushed anyway. 13645 */ 13646 #endif 13647 } 13648 } 13649 } else { 13650 /* 13651 * SD_RETRIES_FAILFAST is clear, which indicates that we 13652 * likely were able to at least establish some level of 13653 * communication with the target and subsequent commands 13654 * and/or retries are likely to get through to the target, 13655 * In this case we want to be aggressive about clearing 13656 * the failfast state. Note that this does not affect 13657 * the "failfast pending" condition. 13658 */ 13659 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13660 } 13661 13662 13663 /* 13664 * Check the specified retry count to see if we can still do 13665 * any retries with this pkt before we should fail it. 13666 */ 13667 switch (retry_check_flag & SD_RETRIES_MASK) { 13668 case SD_RETRIES_VICTIM: 13669 /* 13670 * Check the victim retry count. If exhausted, then fall 13671 * thru & check against the standard retry count. 13672 */ 13673 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 13674 /* Increment count & proceed with the retry */ 13675 xp->xb_victim_retry_count++; 13676 break; 13677 } 13678 /* Victim retries exhausted, fall back to std. retries... */ 13679 /* FALLTHRU */ 13680 13681 case SD_RETRIES_STANDARD: 13682 if (xp->xb_retry_count >= un->un_retry_count) { 13683 /* Retries exhausted, fail the command */ 13684 SD_TRACE(SD_LOG_IO_CORE, un, 13685 "sd_retry_command: retries exhausted!\n"); 13686 /* 13687 * update b_resid for failed SCMD_READ & SCMD_WRITE 13688 * commands with nonzero pkt_resid. 13689 */ 13690 if ((pktp->pkt_reason == CMD_CMPLT) && 13691 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 13692 (pktp->pkt_resid != 0)) { 13693 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 13694 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 13695 SD_UPDATE_B_RESID(bp, pktp); 13696 } 13697 } 13698 goto fail_command; 13699 } 13700 xp->xb_retry_count++; 13701 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13702 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13703 break; 13704 13705 case SD_RETRIES_UA: 13706 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 13707 /* Retries exhausted, fail the command */ 13708 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13709 "Unit Attention retries exhausted. " 13710 "Check the target.\n"); 13711 goto fail_command; 13712 } 13713 xp->xb_ua_retry_count++; 13714 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13715 "sd_retry_command: retry count:%d\n", 13716 xp->xb_ua_retry_count); 13717 break; 13718 13719 case SD_RETRIES_BUSY: 13720 if (xp->xb_retry_count >= un->un_busy_retry_count) { 13721 /* Retries exhausted, fail the command */ 13722 SD_TRACE(SD_LOG_IO_CORE, un, 13723 "sd_retry_command: retries exhausted!\n"); 13724 goto fail_command; 13725 } 13726 xp->xb_retry_count++; 13727 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13728 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13729 break; 13730 13731 case SD_RETRIES_NOCHECK: 13732 default: 13733 /* No retry count to check. Just proceed with the retry */ 13734 break; 13735 } 13736 13737 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13738 13739 /* 13740 * If we were given a zero timeout, we must attempt to retry the 13741 * command immediately (ie, without a delay). 13742 */ 13743 if (retry_delay == 0) { 13744 /* 13745 * Check some limiting conditions to see if we can actually 13746 * do the immediate retry. If we cannot, then we must 13747 * fall back to queueing up a delayed retry. 13748 */ 13749 if (un->un_ncmds_in_transport >= un->un_throttle) { 13750 /* 13751 * We are at the throttle limit for the target, 13752 * fall back to delayed retry. 13753 */ 13754 retry_delay = SD_BSY_TIMEOUT; 13755 statp = kstat_waitq_enter; 13756 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13757 "sd_retry_command: immed. retry hit " 13758 "throttle!\n"); 13759 } else { 13760 /* 13761 * We're clear to proceed with the immediate retry. 13762 * First call the user-provided function (if any) 13763 */ 13764 if (user_funcp != NULL) { 13765 (*user_funcp)(un, bp, user_arg, 13766 SD_IMMEDIATE_RETRY_ISSUED); 13767 #ifdef __lock_lint 13768 sd_print_incomplete_msg(un, bp, user_arg, 13769 SD_IMMEDIATE_RETRY_ISSUED); 13770 sd_print_cmd_incomplete_msg(un, bp, user_arg, 13771 SD_IMMEDIATE_RETRY_ISSUED); 13772 sd_print_sense_failed_msg(un, bp, user_arg, 13773 SD_IMMEDIATE_RETRY_ISSUED); 13774 #endif 13775 } 13776 13777 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13778 "sd_retry_command: issuing immediate retry\n"); 13779 13780 /* 13781 * Call sd_start_cmds() to transport the command to 13782 * the target. 13783 */ 13784 sd_start_cmds(un, bp); 13785 13786 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13787 "sd_retry_command exit\n"); 13788 return; 13789 } 13790 } 13791 13792 /* 13793 * Set up to retry the command after a delay. 13794 * First call the user-provided function (if any) 13795 */ 13796 if (user_funcp != NULL) { 13797 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 13798 } 13799 13800 sd_set_retry_bp(un, bp, retry_delay, statp); 13801 13802 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 13803 return; 13804 13805 fail_command: 13806 13807 if (user_funcp != NULL) { 13808 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 13809 } 13810 13811 fail_command_no_log: 13812 13813 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13814 "sd_retry_command: returning failed command\n"); 13815 13816 sd_return_failed_command(un, bp, failure_code); 13817 13818 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 13819 } 13820 13821 13822 /* 13823 * Function: sd_set_retry_bp 13824 * 13825 * Description: Set up the given bp for retry. 13826 * 13827 * Arguments: un - ptr to associated softstate 13828 * bp - ptr to buf(9S) for the command 13829 * retry_delay - time interval before issuing retry (may be 0) 13830 * statp - optional pointer to kstat function 13831 * 13832 * Context: May be called under interrupt context 13833 */ 13834 13835 static void 13836 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 13837 void (*statp)(kstat_io_t *)) 13838 { 13839 ASSERT(un != NULL); 13840 ASSERT(mutex_owned(SD_MUTEX(un))); 13841 ASSERT(bp != NULL); 13842 13843 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 13844 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 13845 13846 /* 13847 * Indicate that the command is being retried. This will not allow any 13848 * other commands on the wait queue to be transported to the target 13849 * until this command has been completed (success or failure). The 13850 * "retry command" is not transported to the target until the given 13851 * time delay expires, unless the user specified a 0 retry_delay. 13852 * 13853 * Note: the timeout(9F) callback routine is what actually calls 13854 * sd_start_cmds() to transport the command, with the exception of a 13855 * zero retry_delay. The only current implementor of a zero retry delay 13856 * is the case where a START_STOP_UNIT is sent to spin-up a device. 13857 */ 13858 if (un->un_retry_bp == NULL) { 13859 ASSERT(un->un_retry_statp == NULL); 13860 un->un_retry_bp = bp; 13861 13862 /* 13863 * If the user has not specified a delay the command should 13864 * be queued and no timeout should be scheduled. 13865 */ 13866 if (retry_delay == 0) { 13867 /* 13868 * Save the kstat pointer that will be used in the 13869 * call to SD_UPDATE_KSTATS() below, so that 13870 * sd_start_cmds() can correctly decrement the waitq 13871 * count when it is time to transport this command. 13872 */ 13873 un->un_retry_statp = statp; 13874 goto done; 13875 } 13876 } 13877 13878 if (un->un_retry_bp == bp) { 13879 /* 13880 * Save the kstat pointer that will be used in the call to 13881 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 13882 * correctly decrement the waitq count when it is time to 13883 * transport this command. 13884 */ 13885 un->un_retry_statp = statp; 13886 13887 /* 13888 * Schedule a timeout if: 13889 * 1) The user has specified a delay. 13890 * 2) There is not a START_STOP_UNIT callback pending. 13891 * 13892 * If no delay has been specified, then it is up to the caller 13893 * to ensure that IO processing continues without stalling. 13894 * Effectively, this means that the caller will issue the 13895 * required call to sd_start_cmds(). The START_STOP_UNIT 13896 * callback does this after the START STOP UNIT command has 13897 * completed. In either of these cases we should not schedule 13898 * a timeout callback here. Also don't schedule the timeout if 13899 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 13900 */ 13901 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 13902 (un->un_direct_priority_timeid == NULL)) { 13903 un->un_retry_timeid = 13904 timeout(sd_start_retry_command, un, retry_delay); 13905 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13906 "sd_set_retry_bp: setting timeout: un: 0x%p" 13907 " bp:0x%p un_retry_timeid:0x%p\n", 13908 un, bp, un->un_retry_timeid); 13909 } 13910 } else { 13911 /* 13912 * We only get in here if there is already another command 13913 * waiting to be retried. In this case, we just put the 13914 * given command onto the wait queue, so it can be transported 13915 * after the current retry command has completed. 13916 * 13917 * Also we have to make sure that if the command at the head 13918 * of the wait queue is the un_failfast_bp, that we do not 13919 * put ahead of it any other commands that are to be retried. 13920 */ 13921 if ((un->un_failfast_bp != NULL) && 13922 (un->un_failfast_bp == un->un_waitq_headp)) { 13923 /* 13924 * Enqueue this command AFTER the first command on 13925 * the wait queue (which is also un_failfast_bp). 13926 */ 13927 bp->av_forw = un->un_waitq_headp->av_forw; 13928 un->un_waitq_headp->av_forw = bp; 13929 if (un->un_waitq_headp == un->un_waitq_tailp) { 13930 un->un_waitq_tailp = bp; 13931 } 13932 } else { 13933 /* Enqueue this command at the head of the waitq. */ 13934 bp->av_forw = un->un_waitq_headp; 13935 un->un_waitq_headp = bp; 13936 if (un->un_waitq_tailp == NULL) { 13937 un->un_waitq_tailp = bp; 13938 } 13939 } 13940 13941 if (statp == NULL) { 13942 statp = kstat_waitq_enter; 13943 } 13944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13945 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 13946 } 13947 13948 done: 13949 if (statp != NULL) { 13950 SD_UPDATE_KSTATS(un, statp, bp); 13951 } 13952 13953 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13954 "sd_set_retry_bp: exit un:0x%p\n", un); 13955 } 13956 13957 13958 /* 13959 * Function: sd_start_retry_command 13960 * 13961 * Description: Start the command that has been waiting on the target's 13962 * retry queue. Called from timeout(9F) context after the 13963 * retry delay interval has expired. 13964 * 13965 * Arguments: arg - pointer to associated softstate for the device. 13966 * 13967 * Context: timeout(9F) thread context. May not sleep. 13968 */ 13969 13970 static void 13971 sd_start_retry_command(void *arg) 13972 { 13973 struct sd_lun *un = arg; 13974 13975 ASSERT(un != NULL); 13976 ASSERT(!mutex_owned(SD_MUTEX(un))); 13977 13978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13979 "sd_start_retry_command: entry\n"); 13980 13981 mutex_enter(SD_MUTEX(un)); 13982 13983 un->un_retry_timeid = NULL; 13984 13985 if (un->un_retry_bp != NULL) { 13986 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13987 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 13988 un, un->un_retry_bp); 13989 sd_start_cmds(un, un->un_retry_bp); 13990 } 13991 13992 mutex_exit(SD_MUTEX(un)); 13993 13994 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13995 "sd_start_retry_command: exit\n"); 13996 } 13997 13998 13999 /* 14000 * Function: sd_start_direct_priority_command 14001 * 14002 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 14003 * received TRAN_BUSY when we called scsi_transport() to send it 14004 * to the underlying HBA. This function is called from timeout(9F) 14005 * context after the delay interval has expired. 14006 * 14007 * Arguments: arg - pointer to associated buf(9S) to be restarted. 14008 * 14009 * Context: timeout(9F) thread context. May not sleep. 14010 */ 14011 14012 static void 14013 sd_start_direct_priority_command(void *arg) 14014 { 14015 struct buf *priority_bp = arg; 14016 struct sd_lun *un; 14017 14018 ASSERT(priority_bp != NULL); 14019 un = SD_GET_UN(priority_bp); 14020 ASSERT(un != NULL); 14021 ASSERT(!mutex_owned(SD_MUTEX(un))); 14022 14023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14024 "sd_start_direct_priority_command: entry\n"); 14025 14026 mutex_enter(SD_MUTEX(un)); 14027 un->un_direct_priority_timeid = NULL; 14028 sd_start_cmds(un, priority_bp); 14029 mutex_exit(SD_MUTEX(un)); 14030 14031 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14032 "sd_start_direct_priority_command: exit\n"); 14033 } 14034 14035 14036 /* 14037 * Function: sd_send_request_sense_command 14038 * 14039 * Description: Sends a REQUEST SENSE command to the target 14040 * 14041 * Context: May be called from interrupt context. 14042 */ 14043 14044 static void 14045 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 14046 struct scsi_pkt *pktp) 14047 { 14048 ASSERT(bp != NULL); 14049 ASSERT(un != NULL); 14050 ASSERT(mutex_owned(SD_MUTEX(un))); 14051 14052 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 14053 "entry: buf:0x%p\n", bp); 14054 14055 /* 14056 * If we are syncing or dumping, then fail the command to avoid a 14057 * recursive callback into scsi_transport(). Also fail the command 14058 * if we are suspended (legacy behavior). 14059 */ 14060 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 14061 (un->un_state == SD_STATE_DUMPING)) { 14062 sd_return_failed_command(un, bp, EIO); 14063 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14064 "sd_send_request_sense_command: syncing/dumping, exit\n"); 14065 return; 14066 } 14067 14068 /* 14069 * Retry the failed command and don't issue the request sense if: 14070 * 1) the sense buf is busy 14071 * 2) we have 1 or more outstanding commands on the target 14072 * (the sense data will be cleared or invalidated any way) 14073 * 14074 * Note: There could be an issue with not checking a retry limit here, 14075 * the problem is determining which retry limit to check. 14076 */ 14077 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 14078 /* Don't retry if the command is flagged as non-retryable */ 14079 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 14080 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14081 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 14082 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14083 "sd_send_request_sense_command: " 14084 "at full throttle, retrying exit\n"); 14085 } else { 14086 sd_return_failed_command(un, bp, EIO); 14087 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14088 "sd_send_request_sense_command: " 14089 "at full throttle, non-retryable exit\n"); 14090 } 14091 return; 14092 } 14093 14094 sd_mark_rqs_busy(un, bp); 14095 sd_start_cmds(un, un->un_rqs_bp); 14096 14097 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14098 "sd_send_request_sense_command: exit\n"); 14099 } 14100 14101 14102 /* 14103 * Function: sd_mark_rqs_busy 14104 * 14105 * Description: Indicate that the request sense bp for this instance is 14106 * in use. 14107 * 14108 * Context: May be called under interrupt context 14109 */ 14110 14111 static void 14112 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 14113 { 14114 struct sd_xbuf *sense_xp; 14115 14116 ASSERT(un != NULL); 14117 ASSERT(bp != NULL); 14118 ASSERT(mutex_owned(SD_MUTEX(un))); 14119 ASSERT(un->un_sense_isbusy == 0); 14120 14121 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 14122 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 14123 14124 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 14125 ASSERT(sense_xp != NULL); 14126 14127 SD_INFO(SD_LOG_IO, un, 14128 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 14129 14130 ASSERT(sense_xp->xb_pktp != NULL); 14131 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 14132 == (FLAG_SENSING | FLAG_HEAD)); 14133 14134 un->un_sense_isbusy = 1; 14135 un->un_rqs_bp->b_resid = 0; 14136 sense_xp->xb_pktp->pkt_resid = 0; 14137 sense_xp->xb_pktp->pkt_reason = 0; 14138 14139 /* So we can get back the bp at interrupt time! */ 14140 sense_xp->xb_sense_bp = bp; 14141 14142 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 14143 14144 /* 14145 * Mark this buf as awaiting sense data. (This is already set in 14146 * the pkt_flags for the RQS packet.) 14147 */ 14148 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 14149 14150 sense_xp->xb_retry_count = 0; 14151 sense_xp->xb_victim_retry_count = 0; 14152 sense_xp->xb_ua_retry_count = 0; 14153 sense_xp->xb_nr_retry_count = 0; 14154 sense_xp->xb_dma_resid = 0; 14155 14156 /* Clean up the fields for auto-request sense */ 14157 sense_xp->xb_sense_status = 0; 14158 sense_xp->xb_sense_state = 0; 14159 sense_xp->xb_sense_resid = 0; 14160 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 14161 14162 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 14163 } 14164 14165 14166 /* 14167 * Function: sd_mark_rqs_idle 14168 * 14169 * Description: SD_MUTEX must be held continuously through this routine 14170 * to prevent reuse of the rqs struct before the caller can 14171 * complete it's processing. 14172 * 14173 * Return Code: Pointer to the RQS buf 14174 * 14175 * Context: May be called under interrupt context 14176 */ 14177 14178 static struct buf * 14179 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 14180 { 14181 struct buf *bp; 14182 ASSERT(un != NULL); 14183 ASSERT(sense_xp != NULL); 14184 ASSERT(mutex_owned(SD_MUTEX(un))); 14185 ASSERT(un->un_sense_isbusy != 0); 14186 14187 un->un_sense_isbusy = 0; 14188 bp = sense_xp->xb_sense_bp; 14189 sense_xp->xb_sense_bp = NULL; 14190 14191 /* This pkt is no longer interested in getting sense data */ 14192 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 14193 14194 return (bp); 14195 } 14196 14197 14198 14199 /* 14200 * Function: sd_alloc_rqs 14201 * 14202 * Description: Set up the unit to receive auto request sense data 14203 * 14204 * Return Code: DDI_SUCCESS or DDI_FAILURE 14205 * 14206 * Context: Called under attach(9E) context 14207 */ 14208 14209 static int 14210 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 14211 { 14212 struct sd_xbuf *xp; 14213 14214 ASSERT(un != NULL); 14215 ASSERT(!mutex_owned(SD_MUTEX(un))); 14216 ASSERT(un->un_rqs_bp == NULL); 14217 ASSERT(un->un_rqs_pktp == NULL); 14218 14219 /* 14220 * First allocate the required buf and scsi_pkt structs, then set up 14221 * the CDB in the scsi_pkt for a REQUEST SENSE command. 14222 */ 14223 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 14224 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 14225 if (un->un_rqs_bp == NULL) { 14226 return (DDI_FAILURE); 14227 } 14228 14229 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 14230 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 14231 14232 if (un->un_rqs_pktp == NULL) { 14233 sd_free_rqs(un); 14234 return (DDI_FAILURE); 14235 } 14236 14237 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 14238 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 14239 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 14240 14241 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 14242 14243 /* Set up the other needed members in the ARQ scsi_pkt. */ 14244 un->un_rqs_pktp->pkt_comp = sdintr; 14245 un->un_rqs_pktp->pkt_time = sd_io_time; 14246 un->un_rqs_pktp->pkt_flags |= 14247 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 14248 14249 /* 14250 * Allocate & init the sd_xbuf struct for the RQS command. Do not 14251 * provide any intpkt, destroypkt routines as we take care of 14252 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 14253 */ 14254 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14255 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 14256 xp->xb_pktp = un->un_rqs_pktp; 14257 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14258 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 14259 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 14260 14261 /* 14262 * Save the pointer to the request sense private bp so it can 14263 * be retrieved in sdintr. 14264 */ 14265 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 14266 ASSERT(un->un_rqs_bp->b_private == xp); 14267 14268 /* 14269 * See if the HBA supports auto-request sense for the specified 14270 * target/lun. If it does, then try to enable it (if not already 14271 * enabled). 14272 * 14273 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 14274 * failure, while for other HBAs (pln) scsi_ifsetcap will always 14275 * return success. However, in both of these cases ARQ is always 14276 * enabled and scsi_ifgetcap will always return true. The best approach 14277 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 14278 * 14279 * The 3rd case is the HBA (adp) always return enabled on 14280 * scsi_ifgetgetcap even when it's not enable, the best approach 14281 * is issue a scsi_ifsetcap then a scsi_ifgetcap 14282 * Note: this case is to circumvent the Adaptec bug. (x86 only) 14283 */ 14284 14285 if (un->un_f_is_fibre == TRUE) { 14286 un->un_f_arq_enabled = TRUE; 14287 } else { 14288 #if defined(__i386) || defined(__amd64) 14289 /* 14290 * Circumvent the Adaptec bug, remove this code when 14291 * the bug is fixed 14292 */ 14293 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 14294 #endif 14295 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 14296 case 0: 14297 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14298 "sd_alloc_rqs: HBA supports ARQ\n"); 14299 /* 14300 * ARQ is supported by this HBA but currently is not 14301 * enabled. Attempt to enable it and if successful then 14302 * mark this instance as ARQ enabled. 14303 */ 14304 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 14305 == 1) { 14306 /* Successfully enabled ARQ in the HBA */ 14307 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14308 "sd_alloc_rqs: ARQ enabled\n"); 14309 un->un_f_arq_enabled = TRUE; 14310 } else { 14311 /* Could not enable ARQ in the HBA */ 14312 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14313 "sd_alloc_rqs: failed ARQ enable\n"); 14314 un->un_f_arq_enabled = FALSE; 14315 } 14316 break; 14317 case 1: 14318 /* 14319 * ARQ is supported by this HBA and is already enabled. 14320 * Just mark ARQ as enabled for this instance. 14321 */ 14322 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14323 "sd_alloc_rqs: ARQ already enabled\n"); 14324 un->un_f_arq_enabled = TRUE; 14325 break; 14326 default: 14327 /* 14328 * ARQ is not supported by this HBA; disable it for this 14329 * instance. 14330 */ 14331 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14332 "sd_alloc_rqs: HBA does not support ARQ\n"); 14333 un->un_f_arq_enabled = FALSE; 14334 break; 14335 } 14336 } 14337 14338 return (DDI_SUCCESS); 14339 } 14340 14341 14342 /* 14343 * Function: sd_free_rqs 14344 * 14345 * Description: Cleanup for the pre-instance RQS command. 14346 * 14347 * Context: Kernel thread context 14348 */ 14349 14350 static void 14351 sd_free_rqs(struct sd_lun *un) 14352 { 14353 ASSERT(un != NULL); 14354 14355 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 14356 14357 /* 14358 * If consistent memory is bound to a scsi_pkt, the pkt 14359 * has to be destroyed *before* freeing the consistent memory. 14360 * Don't change the sequence of this operations. 14361 * scsi_destroy_pkt() might access memory, which isn't allowed, 14362 * after it was freed in scsi_free_consistent_buf(). 14363 */ 14364 if (un->un_rqs_pktp != NULL) { 14365 scsi_destroy_pkt(un->un_rqs_pktp); 14366 un->un_rqs_pktp = NULL; 14367 } 14368 14369 if (un->un_rqs_bp != NULL) { 14370 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 14371 scsi_free_consistent_buf(un->un_rqs_bp); 14372 un->un_rqs_bp = NULL; 14373 } 14374 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 14375 } 14376 14377 14378 14379 /* 14380 * Function: sd_reduce_throttle 14381 * 14382 * Description: Reduces the maximum # of outstanding commands on a 14383 * target to the current number of outstanding commands. 14384 * Queues a tiemout(9F) callback to restore the limit 14385 * after a specified interval has elapsed. 14386 * Typically used when we get a TRAN_BUSY return code 14387 * back from scsi_transport(). 14388 * 14389 * Arguments: un - ptr to the sd_lun softstate struct 14390 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 14391 * 14392 * Context: May be called from interrupt context 14393 */ 14394 14395 static void 14396 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 14397 { 14398 ASSERT(un != NULL); 14399 ASSERT(mutex_owned(SD_MUTEX(un))); 14400 ASSERT(un->un_ncmds_in_transport >= 0); 14401 14402 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14403 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 14404 un, un->un_throttle, un->un_ncmds_in_transport); 14405 14406 if (un->un_throttle > 1) { 14407 if (un->un_f_use_adaptive_throttle == TRUE) { 14408 switch (throttle_type) { 14409 case SD_THROTTLE_TRAN_BUSY: 14410 if (un->un_busy_throttle == 0) { 14411 un->un_busy_throttle = un->un_throttle; 14412 } 14413 break; 14414 case SD_THROTTLE_QFULL: 14415 un->un_busy_throttle = 0; 14416 break; 14417 default: 14418 ASSERT(FALSE); 14419 } 14420 14421 if (un->un_ncmds_in_transport > 0) { 14422 un->un_throttle = un->un_ncmds_in_transport; 14423 } 14424 14425 } else { 14426 if (un->un_ncmds_in_transport == 0) { 14427 un->un_throttle = 1; 14428 } else { 14429 un->un_throttle = un->un_ncmds_in_transport; 14430 } 14431 } 14432 } 14433 14434 /* Reschedule the timeout if none is currently active */ 14435 if (un->un_reset_throttle_timeid == NULL) { 14436 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 14437 un, SD_THROTTLE_RESET_INTERVAL); 14438 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14439 "sd_reduce_throttle: timeout scheduled!\n"); 14440 } 14441 14442 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14443 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14444 } 14445 14446 14447 14448 /* 14449 * Function: sd_restore_throttle 14450 * 14451 * Description: Callback function for timeout(9F). Resets the current 14452 * value of un->un_throttle to its default. 14453 * 14454 * Arguments: arg - pointer to associated softstate for the device. 14455 * 14456 * Context: May be called from interrupt context 14457 */ 14458 14459 static void 14460 sd_restore_throttle(void *arg) 14461 { 14462 struct sd_lun *un = arg; 14463 14464 ASSERT(un != NULL); 14465 ASSERT(!mutex_owned(SD_MUTEX(un))); 14466 14467 mutex_enter(SD_MUTEX(un)); 14468 14469 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14470 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14471 14472 un->un_reset_throttle_timeid = NULL; 14473 14474 if (un->un_f_use_adaptive_throttle == TRUE) { 14475 /* 14476 * If un_busy_throttle is nonzero, then it contains the 14477 * value that un_throttle was when we got a TRAN_BUSY back 14478 * from scsi_transport(). We want to revert back to this 14479 * value. 14480 * 14481 * In the QFULL case, the throttle limit will incrementally 14482 * increase until it reaches max throttle. 14483 */ 14484 if (un->un_busy_throttle > 0) { 14485 un->un_throttle = un->un_busy_throttle; 14486 un->un_busy_throttle = 0; 14487 } else { 14488 /* 14489 * increase throttle by 10% open gate slowly, schedule 14490 * another restore if saved throttle has not been 14491 * reached 14492 */ 14493 short throttle; 14494 if (sd_qfull_throttle_enable) { 14495 throttle = un->un_throttle + 14496 max((un->un_throttle / 10), 1); 14497 un->un_throttle = 14498 (throttle < un->un_saved_throttle) ? 14499 throttle : un->un_saved_throttle; 14500 if (un->un_throttle < un->un_saved_throttle) { 14501 un->un_reset_throttle_timeid = 14502 timeout(sd_restore_throttle, 14503 un, 14504 SD_QFULL_THROTTLE_RESET_INTERVAL); 14505 } 14506 } 14507 } 14508 14509 /* 14510 * If un_throttle has fallen below the low-water mark, we 14511 * restore the maximum value here (and allow it to ratchet 14512 * down again if necessary). 14513 */ 14514 if (un->un_throttle < un->un_min_throttle) { 14515 un->un_throttle = un->un_saved_throttle; 14516 } 14517 } else { 14518 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14519 "restoring limit from 0x%x to 0x%x\n", 14520 un->un_throttle, un->un_saved_throttle); 14521 un->un_throttle = un->un_saved_throttle; 14522 } 14523 14524 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14525 "sd_restore_throttle: calling sd_start_cmds!\n"); 14526 14527 sd_start_cmds(un, NULL); 14528 14529 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14530 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 14531 un, un->un_throttle); 14532 14533 mutex_exit(SD_MUTEX(un)); 14534 14535 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 14536 } 14537 14538 /* 14539 * Function: sdrunout 14540 * 14541 * Description: Callback routine for scsi_init_pkt when a resource allocation 14542 * fails. 14543 * 14544 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 14545 * soft state instance. 14546 * 14547 * Return Code: The scsi_init_pkt routine allows for the callback function to 14548 * return a 0 indicating the callback should be rescheduled or a 1 14549 * indicating not to reschedule. This routine always returns 1 14550 * because the driver always provides a callback function to 14551 * scsi_init_pkt. This results in a callback always being scheduled 14552 * (via the scsi_init_pkt callback implementation) if a resource 14553 * failure occurs. 14554 * 14555 * Context: This callback function may not block or call routines that block 14556 * 14557 * Note: Using the scsi_init_pkt callback facility can result in an I/O 14558 * request persisting at the head of the list which cannot be 14559 * satisfied even after multiple retries. In the future the driver 14560 * may implement some time of maximum runout count before failing 14561 * an I/O. 14562 */ 14563 14564 static int 14565 sdrunout(caddr_t arg) 14566 { 14567 struct sd_lun *un = (struct sd_lun *)arg; 14568 14569 ASSERT(un != NULL); 14570 ASSERT(!mutex_owned(SD_MUTEX(un))); 14571 14572 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 14573 14574 mutex_enter(SD_MUTEX(un)); 14575 sd_start_cmds(un, NULL); 14576 mutex_exit(SD_MUTEX(un)); 14577 /* 14578 * This callback routine always returns 1 (i.e. do not reschedule) 14579 * because we always specify sdrunout as the callback handler for 14580 * scsi_init_pkt inside the call to sd_start_cmds. 14581 */ 14582 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 14583 return (1); 14584 } 14585 14586 14587 /* 14588 * Function: sdintr 14589 * 14590 * Description: Completion callback routine for scsi_pkt(9S) structs 14591 * sent to the HBA driver via scsi_transport(9F). 14592 * 14593 * Context: Interrupt context 14594 */ 14595 14596 static void 14597 sdintr(struct scsi_pkt *pktp) 14598 { 14599 struct buf *bp; 14600 struct sd_xbuf *xp; 14601 struct sd_lun *un; 14602 14603 ASSERT(pktp != NULL); 14604 bp = (struct buf *)pktp->pkt_private; 14605 ASSERT(bp != NULL); 14606 xp = SD_GET_XBUF(bp); 14607 ASSERT(xp != NULL); 14608 ASSERT(xp->xb_pktp != NULL); 14609 un = SD_GET_UN(bp); 14610 ASSERT(un != NULL); 14611 ASSERT(!mutex_owned(SD_MUTEX(un))); 14612 14613 #ifdef SD_FAULT_INJECTION 14614 14615 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 14616 /* SD FaultInjection */ 14617 sd_faultinjection(pktp); 14618 14619 #endif /* SD_FAULT_INJECTION */ 14620 14621 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 14622 " xp:0x%p, un:0x%p\n", bp, xp, un); 14623 14624 mutex_enter(SD_MUTEX(un)); 14625 14626 /* Reduce the count of the #commands currently in transport */ 14627 un->un_ncmds_in_transport--; 14628 ASSERT(un->un_ncmds_in_transport >= 0); 14629 14630 /* Increment counter to indicate that the callback routine is active */ 14631 un->un_in_callback++; 14632 14633 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14634 14635 #ifdef SDDEBUG 14636 if (bp == un->un_retry_bp) { 14637 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 14638 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 14639 un, un->un_retry_bp, un->un_ncmds_in_transport); 14640 } 14641 #endif 14642 14643 /* 14644 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 14645 * state if needed. 14646 */ 14647 if (pktp->pkt_reason == CMD_DEV_GONE) { 14648 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 14649 "Device is gone\n"); 14650 if (un->un_mediastate != DKIO_DEV_GONE) { 14651 un->un_mediastate = DKIO_DEV_GONE; 14652 cv_broadcast(&un->un_state_cv); 14653 } 14654 sd_return_failed_command(un, bp, EIO); 14655 goto exit; 14656 } 14657 14658 /* 14659 * First see if the pkt has auto-request sense data with it.... 14660 * Look at the packet state first so we don't take a performance 14661 * hit looking at the arq enabled flag unless absolutely necessary. 14662 */ 14663 if ((pktp->pkt_state & STATE_ARQ_DONE) && 14664 (un->un_f_arq_enabled == TRUE)) { 14665 /* 14666 * The HBA did an auto request sense for this command so check 14667 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 14668 * driver command that should not be retried. 14669 */ 14670 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14671 /* 14672 * Save the relevant sense info into the xp for the 14673 * original cmd. 14674 */ 14675 struct scsi_arq_status *asp; 14676 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 14677 xp->xb_sense_status = 14678 *((uchar_t *)(&(asp->sts_rqpkt_status))); 14679 xp->xb_sense_state = asp->sts_rqpkt_state; 14680 xp->xb_sense_resid = asp->sts_rqpkt_resid; 14681 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 14682 min(sizeof (struct scsi_extended_sense), 14683 SENSE_LENGTH)); 14684 14685 /* fail the command */ 14686 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14687 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 14688 sd_return_failed_command(un, bp, EIO); 14689 goto exit; 14690 } 14691 14692 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 14693 /* 14694 * We want to either retry or fail this command, so free 14695 * the DMA resources here. If we retry the command then 14696 * the DMA resources will be reallocated in sd_start_cmds(). 14697 * Note that when PKT_DMA_PARTIAL is used, this reallocation 14698 * causes the *entire* transfer to start over again from the 14699 * beginning of the request, even for PARTIAL chunks that 14700 * have already transferred successfully. 14701 */ 14702 if ((un->un_f_is_fibre == TRUE) && 14703 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14704 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 14705 scsi_dmafree(pktp); 14706 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14707 } 14708 #endif 14709 14710 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14711 "sdintr: arq done, sd_handle_auto_request_sense\n"); 14712 14713 sd_handle_auto_request_sense(un, bp, xp, pktp); 14714 goto exit; 14715 } 14716 14717 /* Next see if this is the REQUEST SENSE pkt for the instance */ 14718 if (pktp->pkt_flags & FLAG_SENSING) { 14719 /* This pktp is from the unit's REQUEST_SENSE command */ 14720 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14721 "sdintr: sd_handle_request_sense\n"); 14722 sd_handle_request_sense(un, bp, xp, pktp); 14723 goto exit; 14724 } 14725 14726 /* 14727 * Check to see if the command successfully completed as requested; 14728 * this is the most common case (and also the hot performance path). 14729 * 14730 * Requirements for successful completion are: 14731 * pkt_reason is CMD_CMPLT and packet status is status good. 14732 * In addition: 14733 * - A residual of zero indicates successful completion no matter what 14734 * the command is. 14735 * - If the residual is not zero and the command is not a read or 14736 * write, then it's still defined as successful completion. In other 14737 * words, if the command is a read or write the residual must be 14738 * zero for successful completion. 14739 * - If the residual is not zero and the command is a read or 14740 * write, and it's a USCSICMD, then it's still defined as 14741 * successful completion. 14742 */ 14743 if ((pktp->pkt_reason == CMD_CMPLT) && 14744 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 14745 14746 /* 14747 * Since this command is returned with a good status, we 14748 * can reset the count for Sonoma failover. 14749 */ 14750 un->un_sonoma_failure_count = 0; 14751 14752 /* 14753 * Return all USCSI commands on good status 14754 */ 14755 if (pktp->pkt_resid == 0) { 14756 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14757 "sdintr: returning command for resid == 0\n"); 14758 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 14759 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 14760 SD_UPDATE_B_RESID(bp, pktp); 14761 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14762 "sdintr: returning command for resid != 0\n"); 14763 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 14764 SD_UPDATE_B_RESID(bp, pktp); 14765 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14766 "sdintr: returning uscsi command\n"); 14767 } else { 14768 goto not_successful; 14769 } 14770 sd_return_command(un, bp); 14771 14772 /* 14773 * Decrement counter to indicate that the callback routine 14774 * is done. 14775 */ 14776 un->un_in_callback--; 14777 ASSERT(un->un_in_callback >= 0); 14778 mutex_exit(SD_MUTEX(un)); 14779 14780 return; 14781 } 14782 14783 not_successful: 14784 14785 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 14786 /* 14787 * The following is based upon knowledge of the underlying transport 14788 * and its use of DMA resources. This code should be removed when 14789 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 14790 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 14791 * and sd_start_cmds(). 14792 * 14793 * Free any DMA resources associated with this command if there 14794 * is a chance it could be retried or enqueued for later retry. 14795 * If we keep the DMA binding then mpxio cannot reissue the 14796 * command on another path whenever a path failure occurs. 14797 * 14798 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 14799 * causes the *entire* transfer to start over again from the 14800 * beginning of the request, even for PARTIAL chunks that 14801 * have already transferred successfully. 14802 * 14803 * This is only done for non-uscsi commands (and also skipped for the 14804 * driver's internal RQS command). Also just do this for Fibre Channel 14805 * devices as these are the only ones that support mpxio. 14806 */ 14807 if ((un->un_f_is_fibre == TRUE) && 14808 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14809 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 14810 scsi_dmafree(pktp); 14811 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14812 } 14813 #endif 14814 14815 /* 14816 * The command did not successfully complete as requested so check 14817 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 14818 * driver command that should not be retried so just return. If 14819 * FLAG_DIAGNOSE is not set the error will be processed below. 14820 */ 14821 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14822 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14823 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 14824 /* 14825 * Issue a request sense if a check condition caused the error 14826 * (we handle the auto request sense case above), otherwise 14827 * just fail the command. 14828 */ 14829 if ((pktp->pkt_reason == CMD_CMPLT) && 14830 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 14831 sd_send_request_sense_command(un, bp, pktp); 14832 } else { 14833 sd_return_failed_command(un, bp, EIO); 14834 } 14835 goto exit; 14836 } 14837 14838 /* 14839 * The command did not successfully complete as requested so process 14840 * the error, retry, and/or attempt recovery. 14841 */ 14842 switch (pktp->pkt_reason) { 14843 case CMD_CMPLT: 14844 switch (SD_GET_PKT_STATUS(pktp)) { 14845 case STATUS_GOOD: 14846 /* 14847 * The command completed successfully with a non-zero 14848 * residual 14849 */ 14850 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14851 "sdintr: STATUS_GOOD \n"); 14852 sd_pkt_status_good(un, bp, xp, pktp); 14853 break; 14854 14855 case STATUS_CHECK: 14856 case STATUS_TERMINATED: 14857 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14858 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 14859 sd_pkt_status_check_condition(un, bp, xp, pktp); 14860 break; 14861 14862 case STATUS_BUSY: 14863 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14864 "sdintr: STATUS_BUSY\n"); 14865 sd_pkt_status_busy(un, bp, xp, pktp); 14866 break; 14867 14868 case STATUS_RESERVATION_CONFLICT: 14869 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14870 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 14871 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 14872 break; 14873 14874 case STATUS_QFULL: 14875 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14876 "sdintr: STATUS_QFULL\n"); 14877 sd_pkt_status_qfull(un, bp, xp, pktp); 14878 break; 14879 14880 case STATUS_MET: 14881 case STATUS_INTERMEDIATE: 14882 case STATUS_SCSI2: 14883 case STATUS_INTERMEDIATE_MET: 14884 case STATUS_ACA_ACTIVE: 14885 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 14886 "Unexpected SCSI status received: 0x%x\n", 14887 SD_GET_PKT_STATUS(pktp)); 14888 sd_return_failed_command(un, bp, EIO); 14889 break; 14890 14891 default: 14892 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 14893 "Invalid SCSI status received: 0x%x\n", 14894 SD_GET_PKT_STATUS(pktp)); 14895 sd_return_failed_command(un, bp, EIO); 14896 break; 14897 14898 } 14899 break; 14900 14901 case CMD_INCOMPLETE: 14902 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14903 "sdintr: CMD_INCOMPLETE\n"); 14904 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 14905 break; 14906 case CMD_TRAN_ERR: 14907 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14908 "sdintr: CMD_TRAN_ERR\n"); 14909 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 14910 break; 14911 case CMD_RESET: 14912 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14913 "sdintr: CMD_RESET \n"); 14914 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 14915 break; 14916 case CMD_ABORTED: 14917 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14918 "sdintr: CMD_ABORTED \n"); 14919 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 14920 break; 14921 case CMD_TIMEOUT: 14922 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14923 "sdintr: CMD_TIMEOUT\n"); 14924 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 14925 break; 14926 case CMD_UNX_BUS_FREE: 14927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14928 "sdintr: CMD_UNX_BUS_FREE \n"); 14929 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 14930 break; 14931 case CMD_TAG_REJECT: 14932 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14933 "sdintr: CMD_TAG_REJECT\n"); 14934 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 14935 break; 14936 default: 14937 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14938 "sdintr: default\n"); 14939 sd_pkt_reason_default(un, bp, xp, pktp); 14940 break; 14941 } 14942 14943 exit: 14944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 14945 14946 /* Decrement counter to indicate that the callback routine is done. */ 14947 un->un_in_callback--; 14948 ASSERT(un->un_in_callback >= 0); 14949 14950 /* 14951 * At this point, the pkt has been dispatched, ie, it is either 14952 * being re-tried or has been returned to its caller and should 14953 * not be referenced. 14954 */ 14955 14956 mutex_exit(SD_MUTEX(un)); 14957 } 14958 14959 14960 /* 14961 * Function: sd_print_incomplete_msg 14962 * 14963 * Description: Prints the error message for a CMD_INCOMPLETE error. 14964 * 14965 * Arguments: un - ptr to associated softstate for the device. 14966 * bp - ptr to the buf(9S) for the command. 14967 * arg - message string ptr 14968 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 14969 * or SD_NO_RETRY_ISSUED. 14970 * 14971 * Context: May be called under interrupt context 14972 */ 14973 14974 static void 14975 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 14976 { 14977 struct scsi_pkt *pktp; 14978 char *msgp; 14979 char *cmdp = arg; 14980 14981 ASSERT(un != NULL); 14982 ASSERT(mutex_owned(SD_MUTEX(un))); 14983 ASSERT(bp != NULL); 14984 ASSERT(arg != NULL); 14985 pktp = SD_GET_PKTP(bp); 14986 ASSERT(pktp != NULL); 14987 14988 switch (code) { 14989 case SD_DELAYED_RETRY_ISSUED: 14990 case SD_IMMEDIATE_RETRY_ISSUED: 14991 msgp = "retrying"; 14992 break; 14993 case SD_NO_RETRY_ISSUED: 14994 default: 14995 msgp = "giving up"; 14996 break; 14997 } 14998 14999 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15000 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15001 "incomplete %s- %s\n", cmdp, msgp); 15002 } 15003 } 15004 15005 15006 15007 /* 15008 * Function: sd_pkt_status_good 15009 * 15010 * Description: Processing for a STATUS_GOOD code in pkt_status. 15011 * 15012 * Context: May be called under interrupt context 15013 */ 15014 15015 static void 15016 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 15017 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15018 { 15019 char *cmdp; 15020 15021 ASSERT(un != NULL); 15022 ASSERT(mutex_owned(SD_MUTEX(un))); 15023 ASSERT(bp != NULL); 15024 ASSERT(xp != NULL); 15025 ASSERT(pktp != NULL); 15026 ASSERT(pktp->pkt_reason == CMD_CMPLT); 15027 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 15028 ASSERT(pktp->pkt_resid != 0); 15029 15030 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 15031 15032 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15033 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 15034 case SCMD_READ: 15035 cmdp = "read"; 15036 break; 15037 case SCMD_WRITE: 15038 cmdp = "write"; 15039 break; 15040 default: 15041 SD_UPDATE_B_RESID(bp, pktp); 15042 sd_return_command(un, bp); 15043 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15044 return; 15045 } 15046 15047 /* 15048 * See if we can retry the read/write, preferrably immediately. 15049 * If retries are exhaused, then sd_retry_command() will update 15050 * the b_resid count. 15051 */ 15052 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 15053 cmdp, EIO, (clock_t)0, NULL); 15054 15055 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15056 } 15057 15058 15059 15060 15061 15062 /* 15063 * Function: sd_handle_request_sense 15064 * 15065 * Description: Processing for non-auto Request Sense command. 15066 * 15067 * Arguments: un - ptr to associated softstate 15068 * sense_bp - ptr to buf(9S) for the RQS command 15069 * sense_xp - ptr to the sd_xbuf for the RQS command 15070 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 15071 * 15072 * Context: May be called under interrupt context 15073 */ 15074 15075 static void 15076 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 15077 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 15078 { 15079 struct buf *cmd_bp; /* buf for the original command */ 15080 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 15081 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 15082 15083 ASSERT(un != NULL); 15084 ASSERT(mutex_owned(SD_MUTEX(un))); 15085 ASSERT(sense_bp != NULL); 15086 ASSERT(sense_xp != NULL); 15087 ASSERT(sense_pktp != NULL); 15088 15089 /* 15090 * Note the sense_bp, sense_xp, and sense_pktp here are for the 15091 * RQS command and not the original command. 15092 */ 15093 ASSERT(sense_pktp == un->un_rqs_pktp); 15094 ASSERT(sense_bp == un->un_rqs_bp); 15095 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 15096 (FLAG_SENSING | FLAG_HEAD)); 15097 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 15098 FLAG_SENSING) == FLAG_SENSING); 15099 15100 /* These are the bp, xp, and pktp for the original command */ 15101 cmd_bp = sense_xp->xb_sense_bp; 15102 cmd_xp = SD_GET_XBUF(cmd_bp); 15103 cmd_pktp = SD_GET_PKTP(cmd_bp); 15104 15105 if (sense_pktp->pkt_reason != CMD_CMPLT) { 15106 /* 15107 * The REQUEST SENSE command failed. Release the REQUEST 15108 * SENSE command for re-use, get back the bp for the original 15109 * command, and attempt to re-try the original command if 15110 * FLAG_DIAGNOSE is not set in the original packet. 15111 */ 15112 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15113 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15114 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 15115 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 15116 NULL, NULL, EIO, (clock_t)0, NULL); 15117 return; 15118 } 15119 } 15120 15121 /* 15122 * Save the relevant sense info into the xp for the original cmd. 15123 * 15124 * Note: if the request sense failed the state info will be zero 15125 * as set in sd_mark_rqs_busy() 15126 */ 15127 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 15128 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 15129 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 15130 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 15131 15132 /* 15133 * Free up the RQS command.... 15134 * NOTE: 15135 * Must do this BEFORE calling sd_validate_sense_data! 15136 * sd_validate_sense_data may return the original command in 15137 * which case the pkt will be freed and the flags can no 15138 * longer be touched. 15139 * SD_MUTEX is held through this process until the command 15140 * is dispatched based upon the sense data, so there are 15141 * no race conditions. 15142 */ 15143 (void) sd_mark_rqs_idle(un, sense_xp); 15144 15145 /* 15146 * For a retryable command see if we have valid sense data, if so then 15147 * turn it over to sd_decode_sense() to figure out the right course of 15148 * action. Just fail a non-retryable command. 15149 */ 15150 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15151 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 15152 SD_SENSE_DATA_IS_VALID) { 15153 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 15154 } 15155 } else { 15156 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 15157 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15158 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 15159 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15160 sd_return_failed_command(un, cmd_bp, EIO); 15161 } 15162 } 15163 15164 15165 15166 15167 /* 15168 * Function: sd_handle_auto_request_sense 15169 * 15170 * Description: Processing for auto-request sense information. 15171 * 15172 * Arguments: un - ptr to associated softstate 15173 * bp - ptr to buf(9S) for the command 15174 * xp - ptr to the sd_xbuf for the command 15175 * pktp - ptr to the scsi_pkt(9S) for the command 15176 * 15177 * Context: May be called under interrupt context 15178 */ 15179 15180 static void 15181 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 15182 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15183 { 15184 struct scsi_arq_status *asp; 15185 15186 ASSERT(un != NULL); 15187 ASSERT(mutex_owned(SD_MUTEX(un))); 15188 ASSERT(bp != NULL); 15189 ASSERT(xp != NULL); 15190 ASSERT(pktp != NULL); 15191 ASSERT(pktp != un->un_rqs_pktp); 15192 ASSERT(bp != un->un_rqs_bp); 15193 15194 /* 15195 * For auto-request sense, we get a scsi_arq_status back from 15196 * the HBA, with the sense data in the sts_sensedata member. 15197 * The pkt_scbp of the packet points to this scsi_arq_status. 15198 */ 15199 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 15200 15201 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 15202 /* 15203 * The auto REQUEST SENSE failed; see if we can re-try 15204 * the original command. 15205 */ 15206 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15207 "auto request sense failed (reason=%s)\n", 15208 scsi_rname(asp->sts_rqpkt_reason)); 15209 15210 sd_reset_target(un, pktp); 15211 15212 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15213 NULL, NULL, EIO, (clock_t)0, NULL); 15214 return; 15215 } 15216 15217 /* Save the relevant sense info into the xp for the original cmd. */ 15218 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 15219 xp->xb_sense_state = asp->sts_rqpkt_state; 15220 xp->xb_sense_resid = asp->sts_rqpkt_resid; 15221 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 15222 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 15223 15224 /* 15225 * See if we have valid sense data, if so then turn it over to 15226 * sd_decode_sense() to figure out the right course of action. 15227 */ 15228 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 15229 sd_decode_sense(un, bp, xp, pktp); 15230 } 15231 } 15232 15233 15234 /* 15235 * Function: sd_print_sense_failed_msg 15236 * 15237 * Description: Print log message when RQS has failed. 15238 * 15239 * Arguments: un - ptr to associated softstate 15240 * bp - ptr to buf(9S) for the command 15241 * arg - generic message string ptr 15242 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15243 * or SD_NO_RETRY_ISSUED 15244 * 15245 * Context: May be called from interrupt context 15246 */ 15247 15248 static void 15249 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 15250 int code) 15251 { 15252 char *msgp = arg; 15253 15254 ASSERT(un != NULL); 15255 ASSERT(mutex_owned(SD_MUTEX(un))); 15256 ASSERT(bp != NULL); 15257 15258 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 15259 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 15260 } 15261 } 15262 15263 15264 /* 15265 * Function: sd_validate_sense_data 15266 * 15267 * Description: Check the given sense data for validity. 15268 * If the sense data is not valid, the command will 15269 * be either failed or retried! 15270 * 15271 * Return Code: SD_SENSE_DATA_IS_INVALID 15272 * SD_SENSE_DATA_IS_VALID 15273 * 15274 * Context: May be called from interrupt context 15275 */ 15276 15277 static int 15278 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 15279 { 15280 struct scsi_extended_sense *esp; 15281 struct scsi_pkt *pktp; 15282 size_t actual_len; 15283 char *msgp = NULL; 15284 15285 ASSERT(un != NULL); 15286 ASSERT(mutex_owned(SD_MUTEX(un))); 15287 ASSERT(bp != NULL); 15288 ASSERT(bp != un->un_rqs_bp); 15289 ASSERT(xp != NULL); 15290 15291 pktp = SD_GET_PKTP(bp); 15292 ASSERT(pktp != NULL); 15293 15294 /* 15295 * Check the status of the RQS command (auto or manual). 15296 */ 15297 switch (xp->xb_sense_status & STATUS_MASK) { 15298 case STATUS_GOOD: 15299 break; 15300 15301 case STATUS_RESERVATION_CONFLICT: 15302 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15303 return (SD_SENSE_DATA_IS_INVALID); 15304 15305 case STATUS_BUSY: 15306 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15307 "Busy Status on REQUEST SENSE\n"); 15308 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 15309 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15310 return (SD_SENSE_DATA_IS_INVALID); 15311 15312 case STATUS_QFULL: 15313 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15314 "QFULL Status on REQUEST SENSE\n"); 15315 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 15316 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15317 return (SD_SENSE_DATA_IS_INVALID); 15318 15319 case STATUS_CHECK: 15320 case STATUS_TERMINATED: 15321 msgp = "Check Condition on REQUEST SENSE\n"; 15322 goto sense_failed; 15323 15324 default: 15325 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 15326 goto sense_failed; 15327 } 15328 15329 /* 15330 * See if we got the minimum required amount of sense data. 15331 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 15332 * or less. 15333 */ 15334 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 15335 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 15336 (actual_len == 0)) { 15337 msgp = "Request Sense couldn't get sense data\n"; 15338 goto sense_failed; 15339 } 15340 15341 if (actual_len < SUN_MIN_SENSE_LENGTH) { 15342 msgp = "Not enough sense information\n"; 15343 goto sense_failed; 15344 } 15345 15346 /* 15347 * We require the extended sense data 15348 */ 15349 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 15350 if (esp->es_class != CLASS_EXTENDED_SENSE) { 15351 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15352 static char tmp[8]; 15353 static char buf[148]; 15354 char *p = (char *)(xp->xb_sense_data); 15355 int i; 15356 15357 mutex_enter(&sd_sense_mutex); 15358 (void) strcpy(buf, "undecodable sense information:"); 15359 for (i = 0; i < actual_len; i++) { 15360 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 15361 (void) strcpy(&buf[strlen(buf)], tmp); 15362 } 15363 i = strlen(buf); 15364 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 15365 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 15366 mutex_exit(&sd_sense_mutex); 15367 } 15368 /* Note: Legacy behavior, fail the command with no retry */ 15369 sd_return_failed_command(un, bp, EIO); 15370 return (SD_SENSE_DATA_IS_INVALID); 15371 } 15372 15373 /* 15374 * Check that es_code is valid (es_class concatenated with es_code 15375 * make up the "response code" field. es_class will always be 7, so 15376 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 15377 * format. 15378 */ 15379 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 15380 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 15381 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 15382 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 15383 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 15384 goto sense_failed; 15385 } 15386 15387 return (SD_SENSE_DATA_IS_VALID); 15388 15389 sense_failed: 15390 /* 15391 * If the request sense failed (for whatever reason), attempt 15392 * to retry the original command. 15393 */ 15394 #if defined(__i386) || defined(__amd64) 15395 /* 15396 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 15397 * sddef.h for Sparc platform, and x86 uses 1 binary 15398 * for both SCSI/FC. 15399 * The SD_RETRY_DELAY value need to be adjusted here 15400 * when SD_RETRY_DELAY change in sddef.h 15401 */ 15402 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15403 sd_print_sense_failed_msg, msgp, EIO, 15404 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 15405 #else 15406 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15407 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 15408 #endif 15409 15410 return (SD_SENSE_DATA_IS_INVALID); 15411 } 15412 15413 15414 15415 /* 15416 * Function: sd_decode_sense 15417 * 15418 * Description: Take recovery action(s) when SCSI Sense Data is received. 15419 * 15420 * Context: Interrupt context. 15421 */ 15422 15423 static void 15424 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 15425 struct scsi_pkt *pktp) 15426 { 15427 uint8_t sense_key; 15428 15429 ASSERT(un != NULL); 15430 ASSERT(mutex_owned(SD_MUTEX(un))); 15431 ASSERT(bp != NULL); 15432 ASSERT(bp != un->un_rqs_bp); 15433 ASSERT(xp != NULL); 15434 ASSERT(pktp != NULL); 15435 15436 sense_key = scsi_sense_key(xp->xb_sense_data); 15437 15438 switch (sense_key) { 15439 case KEY_NO_SENSE: 15440 sd_sense_key_no_sense(un, bp, xp, pktp); 15441 break; 15442 case KEY_RECOVERABLE_ERROR: 15443 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 15444 bp, xp, pktp); 15445 break; 15446 case KEY_NOT_READY: 15447 sd_sense_key_not_ready(un, xp->xb_sense_data, 15448 bp, xp, pktp); 15449 break; 15450 case KEY_MEDIUM_ERROR: 15451 case KEY_HARDWARE_ERROR: 15452 sd_sense_key_medium_or_hardware_error(un, 15453 xp->xb_sense_data, bp, xp, pktp); 15454 break; 15455 case KEY_ILLEGAL_REQUEST: 15456 sd_sense_key_illegal_request(un, bp, xp, pktp); 15457 break; 15458 case KEY_UNIT_ATTENTION: 15459 sd_sense_key_unit_attention(un, xp->xb_sense_data, 15460 bp, xp, pktp); 15461 break; 15462 case KEY_WRITE_PROTECT: 15463 case KEY_VOLUME_OVERFLOW: 15464 case KEY_MISCOMPARE: 15465 sd_sense_key_fail_command(un, bp, xp, pktp); 15466 break; 15467 case KEY_BLANK_CHECK: 15468 sd_sense_key_blank_check(un, bp, xp, pktp); 15469 break; 15470 case KEY_ABORTED_COMMAND: 15471 sd_sense_key_aborted_command(un, bp, xp, pktp); 15472 break; 15473 case KEY_VENDOR_UNIQUE: 15474 case KEY_COPY_ABORTED: 15475 case KEY_EQUAL: 15476 case KEY_RESERVED: 15477 default: 15478 sd_sense_key_default(un, xp->xb_sense_data, 15479 bp, xp, pktp); 15480 break; 15481 } 15482 } 15483 15484 15485 /* 15486 * Function: sd_dump_memory 15487 * 15488 * Description: Debug logging routine to print the contents of a user provided 15489 * buffer. The output of the buffer is broken up into 256 byte 15490 * segments due to a size constraint of the scsi_log. 15491 * implementation. 15492 * 15493 * Arguments: un - ptr to softstate 15494 * comp - component mask 15495 * title - "title" string to preceed data when printed 15496 * data - ptr to data block to be printed 15497 * len - size of data block to be printed 15498 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 15499 * 15500 * Context: May be called from interrupt context 15501 */ 15502 15503 #define SD_DUMP_MEMORY_BUF_SIZE 256 15504 15505 static char *sd_dump_format_string[] = { 15506 " 0x%02x", 15507 " %c" 15508 }; 15509 15510 static void 15511 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 15512 int len, int fmt) 15513 { 15514 int i, j; 15515 int avail_count; 15516 int start_offset; 15517 int end_offset; 15518 size_t entry_len; 15519 char *bufp; 15520 char *local_buf; 15521 char *format_string; 15522 15523 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 15524 15525 /* 15526 * In the debug version of the driver, this function is called from a 15527 * number of places which are NOPs in the release driver. 15528 * The debug driver therefore has additional methods of filtering 15529 * debug output. 15530 */ 15531 #ifdef SDDEBUG 15532 /* 15533 * In the debug version of the driver we can reduce the amount of debug 15534 * messages by setting sd_error_level to something other than 15535 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 15536 * sd_component_mask. 15537 */ 15538 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 15539 (sd_error_level != SCSI_ERR_ALL)) { 15540 return; 15541 } 15542 if (((sd_component_mask & comp) == 0) || 15543 (sd_error_level != SCSI_ERR_ALL)) { 15544 return; 15545 } 15546 #else 15547 if (sd_error_level != SCSI_ERR_ALL) { 15548 return; 15549 } 15550 #endif 15551 15552 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 15553 bufp = local_buf; 15554 /* 15555 * Available length is the length of local_buf[], minus the 15556 * length of the title string, minus one for the ":", minus 15557 * one for the newline, minus one for the NULL terminator. 15558 * This gives the #bytes available for holding the printed 15559 * values from the given data buffer. 15560 */ 15561 if (fmt == SD_LOG_HEX) { 15562 format_string = sd_dump_format_string[0]; 15563 } else /* SD_LOG_CHAR */ { 15564 format_string = sd_dump_format_string[1]; 15565 } 15566 /* 15567 * Available count is the number of elements from the given 15568 * data buffer that we can fit into the available length. 15569 * This is based upon the size of the format string used. 15570 * Make one entry and find it's size. 15571 */ 15572 (void) sprintf(bufp, format_string, data[0]); 15573 entry_len = strlen(bufp); 15574 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 15575 15576 j = 0; 15577 while (j < len) { 15578 bufp = local_buf; 15579 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 15580 start_offset = j; 15581 15582 end_offset = start_offset + avail_count; 15583 15584 (void) sprintf(bufp, "%s:", title); 15585 bufp += strlen(bufp); 15586 for (i = start_offset; ((i < end_offset) && (j < len)); 15587 i++, j++) { 15588 (void) sprintf(bufp, format_string, data[i]); 15589 bufp += entry_len; 15590 } 15591 (void) sprintf(bufp, "\n"); 15592 15593 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 15594 } 15595 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 15596 } 15597 15598 /* 15599 * Function: sd_print_sense_msg 15600 * 15601 * Description: Log a message based upon the given sense data. 15602 * 15603 * Arguments: un - ptr to associated softstate 15604 * bp - ptr to buf(9S) for the command 15605 * arg - ptr to associate sd_sense_info struct 15606 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15607 * or SD_NO_RETRY_ISSUED 15608 * 15609 * Context: May be called from interrupt context 15610 */ 15611 15612 static void 15613 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 15614 { 15615 struct sd_xbuf *xp; 15616 struct scsi_pkt *pktp; 15617 uint8_t *sensep; 15618 daddr_t request_blkno; 15619 diskaddr_t err_blkno; 15620 int severity; 15621 int pfa_flag; 15622 extern struct scsi_key_strings scsi_cmds[]; 15623 15624 ASSERT(un != NULL); 15625 ASSERT(mutex_owned(SD_MUTEX(un))); 15626 ASSERT(bp != NULL); 15627 xp = SD_GET_XBUF(bp); 15628 ASSERT(xp != NULL); 15629 pktp = SD_GET_PKTP(bp); 15630 ASSERT(pktp != NULL); 15631 ASSERT(arg != NULL); 15632 15633 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 15634 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 15635 15636 if ((code == SD_DELAYED_RETRY_ISSUED) || 15637 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 15638 severity = SCSI_ERR_RETRYABLE; 15639 } 15640 15641 /* Use absolute block number for the request block number */ 15642 request_blkno = xp->xb_blkno; 15643 15644 /* 15645 * Now try to get the error block number from the sense data 15646 */ 15647 sensep = xp->xb_sense_data; 15648 15649 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 15650 (uint64_t *)&err_blkno)) { 15651 /* 15652 * We retrieved the error block number from the information 15653 * portion of the sense data. 15654 * 15655 * For USCSI commands we are better off using the error 15656 * block no. as the requested block no. (This is the best 15657 * we can estimate.) 15658 */ 15659 if ((SD_IS_BUFIO(xp) == FALSE) && 15660 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 15661 request_blkno = err_blkno; 15662 } 15663 } else { 15664 /* 15665 * Without the es_valid bit set (for fixed format) or an 15666 * information descriptor (for descriptor format) we cannot 15667 * be certain of the error blkno, so just use the 15668 * request_blkno. 15669 */ 15670 err_blkno = (diskaddr_t)request_blkno; 15671 } 15672 15673 /* 15674 * The following will log the buffer contents for the release driver 15675 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 15676 * level is set to verbose. 15677 */ 15678 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 15679 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15680 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15681 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 15682 15683 if (pfa_flag == FALSE) { 15684 /* This is normally only set for USCSI */ 15685 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 15686 return; 15687 } 15688 15689 if ((SD_IS_BUFIO(xp) == TRUE) && 15690 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 15691 (severity < sd_error_level))) { 15692 return; 15693 } 15694 } 15695 15696 /* 15697 * Check for Sonoma Failover and keep a count of how many failed I/O's 15698 */ 15699 if ((SD_IS_LSI(un)) && 15700 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 15701 (scsi_sense_asc(sensep) == 0x94) && 15702 (scsi_sense_ascq(sensep) == 0x01)) { 15703 un->un_sonoma_failure_count++; 15704 if (un->un_sonoma_failure_count > 1) { 15705 return; 15706 } 15707 } 15708 15709 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 15710 request_blkno, err_blkno, scsi_cmds, 15711 (struct scsi_extended_sense *)sensep, 15712 un->un_additional_codes, NULL); 15713 } 15714 15715 /* 15716 * Function: sd_sense_key_no_sense 15717 * 15718 * Description: Recovery action when sense data was not received. 15719 * 15720 * Context: May be called from interrupt context 15721 */ 15722 15723 static void 15724 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 15725 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15726 { 15727 struct sd_sense_info si; 15728 15729 ASSERT(un != NULL); 15730 ASSERT(mutex_owned(SD_MUTEX(un))); 15731 ASSERT(bp != NULL); 15732 ASSERT(xp != NULL); 15733 ASSERT(pktp != NULL); 15734 15735 si.ssi_severity = SCSI_ERR_FATAL; 15736 si.ssi_pfa_flag = FALSE; 15737 15738 SD_UPDATE_ERRSTATS(un, sd_softerrs); 15739 15740 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 15741 &si, EIO, (clock_t)0, NULL); 15742 } 15743 15744 15745 /* 15746 * Function: sd_sense_key_recoverable_error 15747 * 15748 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 15749 * 15750 * Context: May be called from interrupt context 15751 */ 15752 15753 static void 15754 sd_sense_key_recoverable_error(struct sd_lun *un, 15755 uint8_t *sense_datap, 15756 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 15757 { 15758 struct sd_sense_info si; 15759 uint8_t asc = scsi_sense_asc(sense_datap); 15760 15761 ASSERT(un != NULL); 15762 ASSERT(mutex_owned(SD_MUTEX(un))); 15763 ASSERT(bp != NULL); 15764 ASSERT(xp != NULL); 15765 ASSERT(pktp != NULL); 15766 15767 /* 15768 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 15769 */ 15770 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 15771 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 15772 si.ssi_severity = SCSI_ERR_INFO; 15773 si.ssi_pfa_flag = TRUE; 15774 } else { 15775 SD_UPDATE_ERRSTATS(un, sd_softerrs); 15776 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 15777 si.ssi_severity = SCSI_ERR_RECOVERED; 15778 si.ssi_pfa_flag = FALSE; 15779 } 15780 15781 if (pktp->pkt_resid == 0) { 15782 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 15783 sd_return_command(un, bp); 15784 return; 15785 } 15786 15787 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 15788 &si, EIO, (clock_t)0, NULL); 15789 } 15790 15791 15792 15793 15794 /* 15795 * Function: sd_sense_key_not_ready 15796 * 15797 * Description: Recovery actions for a SCSI "Not Ready" sense key. 15798 * 15799 * Context: May be called from interrupt context 15800 */ 15801 15802 static void 15803 sd_sense_key_not_ready(struct sd_lun *un, 15804 uint8_t *sense_datap, 15805 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 15806 { 15807 struct sd_sense_info si; 15808 uint8_t asc = scsi_sense_asc(sense_datap); 15809 uint8_t ascq = scsi_sense_ascq(sense_datap); 15810 15811 ASSERT(un != NULL); 15812 ASSERT(mutex_owned(SD_MUTEX(un))); 15813 ASSERT(bp != NULL); 15814 ASSERT(xp != NULL); 15815 ASSERT(pktp != NULL); 15816 15817 si.ssi_severity = SCSI_ERR_FATAL; 15818 si.ssi_pfa_flag = FALSE; 15819 15820 /* 15821 * Update error stats after first NOT READY error. Disks may have 15822 * been powered down and may need to be restarted. For CDROMs, 15823 * report NOT READY errors only if media is present. 15824 */ 15825 if ((ISCD(un) && (asc == 0x3A)) || 15826 (xp->xb_nr_retry_count > 0)) { 15827 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15828 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 15829 } 15830 15831 /* 15832 * Just fail if the "not ready" retry limit has been reached. 15833 */ 15834 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 15835 /* Special check for error message printing for removables. */ 15836 if (un->un_f_has_removable_media && (asc == 0x04) && 15837 (ascq >= 0x04)) { 15838 si.ssi_severity = SCSI_ERR_ALL; 15839 } 15840 goto fail_command; 15841 } 15842 15843 /* 15844 * Check the ASC and ASCQ in the sense data as needed, to determine 15845 * what to do. 15846 */ 15847 switch (asc) { 15848 case 0x04: /* LOGICAL UNIT NOT READY */ 15849 /* 15850 * disk drives that don't spin up result in a very long delay 15851 * in format without warning messages. We will log a message 15852 * if the error level is set to verbose. 15853 */ 15854 if (sd_error_level < SCSI_ERR_RETRYABLE) { 15855 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15856 "logical unit not ready, resetting disk\n"); 15857 } 15858 15859 /* 15860 * There are different requirements for CDROMs and disks for 15861 * the number of retries. If a CD-ROM is giving this, it is 15862 * probably reading TOC and is in the process of getting 15863 * ready, so we should keep on trying for a long time to make 15864 * sure that all types of media are taken in account (for 15865 * some media the drive takes a long time to read TOC). For 15866 * disks we do not want to retry this too many times as this 15867 * can cause a long hang in format when the drive refuses to 15868 * spin up (a very common failure). 15869 */ 15870 switch (ascq) { 15871 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 15872 /* 15873 * Disk drives frequently refuse to spin up which 15874 * results in a very long hang in format without 15875 * warning messages. 15876 * 15877 * Note: This code preserves the legacy behavior of 15878 * comparing xb_nr_retry_count against zero for fibre 15879 * channel targets instead of comparing against the 15880 * un_reset_retry_count value. The reason for this 15881 * discrepancy has been so utterly lost beneath the 15882 * Sands of Time that even Indiana Jones could not 15883 * find it. 15884 */ 15885 if (un->un_f_is_fibre == TRUE) { 15886 if (((sd_level_mask & SD_LOGMASK_DIAG) || 15887 (xp->xb_nr_retry_count > 0)) && 15888 (un->un_startstop_timeid == NULL)) { 15889 scsi_log(SD_DEVINFO(un), sd_label, 15890 CE_WARN, "logical unit not ready, " 15891 "resetting disk\n"); 15892 sd_reset_target(un, pktp); 15893 } 15894 } else { 15895 if (((sd_level_mask & SD_LOGMASK_DIAG) || 15896 (xp->xb_nr_retry_count > 15897 un->un_reset_retry_count)) && 15898 (un->un_startstop_timeid == NULL)) { 15899 scsi_log(SD_DEVINFO(un), sd_label, 15900 CE_WARN, "logical unit not ready, " 15901 "resetting disk\n"); 15902 sd_reset_target(un, pktp); 15903 } 15904 } 15905 break; 15906 15907 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 15908 /* 15909 * If the target is in the process of becoming 15910 * ready, just proceed with the retry. This can 15911 * happen with CD-ROMs that take a long time to 15912 * read TOC after a power cycle or reset. 15913 */ 15914 goto do_retry; 15915 15916 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 15917 break; 15918 15919 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 15920 /* 15921 * Retries cannot help here so just fail right away. 15922 */ 15923 goto fail_command; 15924 15925 case 0x88: 15926 /* 15927 * Vendor-unique code for T3/T4: it indicates a 15928 * path problem in a mutipathed config, but as far as 15929 * the target driver is concerned it equates to a fatal 15930 * error, so we should just fail the command right away 15931 * (without printing anything to the console). If this 15932 * is not a T3/T4, fall thru to the default recovery 15933 * action. 15934 * T3/T4 is FC only, don't need to check is_fibre 15935 */ 15936 if (SD_IS_T3(un) || SD_IS_T4(un)) { 15937 sd_return_failed_command(un, bp, EIO); 15938 return; 15939 } 15940 /* FALLTHRU */ 15941 15942 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 15943 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 15944 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 15945 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 15946 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 15947 default: /* Possible future codes in SCSI spec? */ 15948 /* 15949 * For removable-media devices, do not retry if 15950 * ASCQ > 2 as these result mostly from USCSI commands 15951 * on MMC devices issued to check status of an 15952 * operation initiated in immediate mode. Also for 15953 * ASCQ >= 4 do not print console messages as these 15954 * mainly represent a user-initiated operation 15955 * instead of a system failure. 15956 */ 15957 if (un->un_f_has_removable_media) { 15958 si.ssi_severity = SCSI_ERR_ALL; 15959 goto fail_command; 15960 } 15961 break; 15962 } 15963 15964 /* 15965 * As part of our recovery attempt for the NOT READY 15966 * condition, we issue a START STOP UNIT command. However 15967 * we want to wait for a short delay before attempting this 15968 * as there may still be more commands coming back from the 15969 * target with the check condition. To do this we use 15970 * timeout(9F) to call sd_start_stop_unit_callback() after 15971 * the delay interval expires. (sd_start_stop_unit_callback() 15972 * dispatches sd_start_stop_unit_task(), which will issue 15973 * the actual START STOP UNIT command. The delay interval 15974 * is one-half of the delay that we will use to retry the 15975 * command that generated the NOT READY condition. 15976 * 15977 * Note that we could just dispatch sd_start_stop_unit_task() 15978 * from here and allow it to sleep for the delay interval, 15979 * but then we would be tying up the taskq thread 15980 * uncesessarily for the duration of the delay. 15981 * 15982 * Do not issue the START STOP UNIT if the current command 15983 * is already a START STOP UNIT. 15984 */ 15985 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 15986 break; 15987 } 15988 15989 /* 15990 * Do not schedule the timeout if one is already pending. 15991 */ 15992 if (un->un_startstop_timeid != NULL) { 15993 SD_INFO(SD_LOG_ERROR, un, 15994 "sd_sense_key_not_ready: restart already issued to" 15995 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 15996 ddi_get_instance(SD_DEVINFO(un))); 15997 break; 15998 } 15999 16000 /* 16001 * Schedule the START STOP UNIT command, then queue the command 16002 * for a retry. 16003 * 16004 * Note: A timeout is not scheduled for this retry because we 16005 * want the retry to be serial with the START_STOP_UNIT. The 16006 * retry will be started when the START_STOP_UNIT is completed 16007 * in sd_start_stop_unit_task. 16008 */ 16009 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 16010 un, SD_BSY_TIMEOUT / 2); 16011 xp->xb_nr_retry_count++; 16012 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 16013 return; 16014 16015 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 16016 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16017 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16018 "unit does not respond to selection\n"); 16019 } 16020 break; 16021 16022 case 0x3A: /* MEDIUM NOT PRESENT */ 16023 if (sd_error_level >= SCSI_ERR_FATAL) { 16024 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16025 "Caddy not inserted in drive\n"); 16026 } 16027 16028 sr_ejected(un); 16029 un->un_mediastate = DKIO_EJECTED; 16030 /* The state has changed, inform the media watch routines */ 16031 cv_broadcast(&un->un_state_cv); 16032 /* Just fail if no media is present in the drive. */ 16033 goto fail_command; 16034 16035 default: 16036 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16037 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 16038 "Unit not Ready. Additional sense code 0x%x\n", 16039 asc); 16040 } 16041 break; 16042 } 16043 16044 do_retry: 16045 16046 /* 16047 * Retry the command, as some targets may report NOT READY for 16048 * several seconds after being reset. 16049 */ 16050 xp->xb_nr_retry_count++; 16051 si.ssi_severity = SCSI_ERR_RETRYABLE; 16052 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 16053 &si, EIO, SD_BSY_TIMEOUT, NULL); 16054 16055 return; 16056 16057 fail_command: 16058 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16059 sd_return_failed_command(un, bp, EIO); 16060 } 16061 16062 16063 16064 /* 16065 * Function: sd_sense_key_medium_or_hardware_error 16066 * 16067 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 16068 * sense key. 16069 * 16070 * Context: May be called from interrupt context 16071 */ 16072 16073 static void 16074 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 16075 uint8_t *sense_datap, 16076 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16077 { 16078 struct sd_sense_info si; 16079 uint8_t sense_key = scsi_sense_key(sense_datap); 16080 uint8_t asc = scsi_sense_asc(sense_datap); 16081 16082 ASSERT(un != NULL); 16083 ASSERT(mutex_owned(SD_MUTEX(un))); 16084 ASSERT(bp != NULL); 16085 ASSERT(xp != NULL); 16086 ASSERT(pktp != NULL); 16087 16088 si.ssi_severity = SCSI_ERR_FATAL; 16089 si.ssi_pfa_flag = FALSE; 16090 16091 if (sense_key == KEY_MEDIUM_ERROR) { 16092 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 16093 } 16094 16095 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16096 16097 if ((un->un_reset_retry_count != 0) && 16098 (xp->xb_retry_count == un->un_reset_retry_count)) { 16099 mutex_exit(SD_MUTEX(un)); 16100 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 16101 if (un->un_f_allow_bus_device_reset == TRUE) { 16102 16103 boolean_t try_resetting_target = B_TRUE; 16104 16105 /* 16106 * We need to be able to handle specific ASC when we are 16107 * handling a KEY_HARDWARE_ERROR. In particular 16108 * taking the default action of resetting the target may 16109 * not be the appropriate way to attempt recovery. 16110 * Resetting a target because of a single LUN failure 16111 * victimizes all LUNs on that target. 16112 * 16113 * This is true for the LSI arrays, if an LSI 16114 * array controller returns an ASC of 0x84 (LUN Dead) we 16115 * should trust it. 16116 */ 16117 16118 if (sense_key == KEY_HARDWARE_ERROR) { 16119 switch (asc) { 16120 case 0x84: 16121 if (SD_IS_LSI(un)) { 16122 try_resetting_target = B_FALSE; 16123 } 16124 break; 16125 default: 16126 break; 16127 } 16128 } 16129 16130 if (try_resetting_target == B_TRUE) { 16131 int reset_retval = 0; 16132 if (un->un_f_lun_reset_enabled == TRUE) { 16133 SD_TRACE(SD_LOG_IO_CORE, un, 16134 "sd_sense_key_medium_or_hardware_" 16135 "error: issuing RESET_LUN\n"); 16136 reset_retval = 16137 scsi_reset(SD_ADDRESS(un), 16138 RESET_LUN); 16139 } 16140 if (reset_retval == 0) { 16141 SD_TRACE(SD_LOG_IO_CORE, un, 16142 "sd_sense_key_medium_or_hardware_" 16143 "error: issuing RESET_TARGET\n"); 16144 (void) scsi_reset(SD_ADDRESS(un), 16145 RESET_TARGET); 16146 } 16147 } 16148 } 16149 mutex_enter(SD_MUTEX(un)); 16150 } 16151 16152 /* 16153 * This really ought to be a fatal error, but we will retry anyway 16154 * as some drives report this as a spurious error. 16155 */ 16156 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16157 &si, EIO, (clock_t)0, NULL); 16158 } 16159 16160 16161 16162 /* 16163 * Function: sd_sense_key_illegal_request 16164 * 16165 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 16166 * 16167 * Context: May be called from interrupt context 16168 */ 16169 16170 static void 16171 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 16172 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16173 { 16174 struct sd_sense_info si; 16175 16176 ASSERT(un != NULL); 16177 ASSERT(mutex_owned(SD_MUTEX(un))); 16178 ASSERT(bp != NULL); 16179 ASSERT(xp != NULL); 16180 ASSERT(pktp != NULL); 16181 16182 SD_UPDATE_ERRSTATS(un, sd_softerrs); 16183 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 16184 16185 si.ssi_severity = SCSI_ERR_INFO; 16186 si.ssi_pfa_flag = FALSE; 16187 16188 /* Pointless to retry if the target thinks it's an illegal request */ 16189 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16190 sd_return_failed_command(un, bp, EIO); 16191 } 16192 16193 16194 16195 16196 /* 16197 * Function: sd_sense_key_unit_attention 16198 * 16199 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 16200 * 16201 * Context: May be called from interrupt context 16202 */ 16203 16204 static void 16205 sd_sense_key_unit_attention(struct sd_lun *un, 16206 uint8_t *sense_datap, 16207 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16208 { 16209 /* 16210 * For UNIT ATTENTION we allow retries for one minute. Devices 16211 * like Sonoma can return UNIT ATTENTION close to a minute 16212 * under certain conditions. 16213 */ 16214 int retry_check_flag = SD_RETRIES_UA; 16215 boolean_t kstat_updated = B_FALSE; 16216 struct sd_sense_info si; 16217 uint8_t asc = scsi_sense_asc(sense_datap); 16218 16219 ASSERT(un != NULL); 16220 ASSERT(mutex_owned(SD_MUTEX(un))); 16221 ASSERT(bp != NULL); 16222 ASSERT(xp != NULL); 16223 ASSERT(pktp != NULL); 16224 16225 si.ssi_severity = SCSI_ERR_INFO; 16226 si.ssi_pfa_flag = FALSE; 16227 16228 16229 switch (asc) { 16230 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 16231 if (sd_report_pfa != 0) { 16232 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16233 si.ssi_pfa_flag = TRUE; 16234 retry_check_flag = SD_RETRIES_STANDARD; 16235 goto do_retry; 16236 } 16237 16238 break; 16239 16240 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 16241 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 16242 un->un_resvd_status |= 16243 (SD_LOST_RESERVE | SD_WANT_RESERVE); 16244 } 16245 #ifdef _LP64 16246 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 16247 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 16248 un, KM_NOSLEEP) == 0) { 16249 /* 16250 * If we can't dispatch the task we'll just 16251 * live without descriptor sense. We can 16252 * try again on the next "unit attention" 16253 */ 16254 SD_ERROR(SD_LOG_ERROR, un, 16255 "sd_sense_key_unit_attention: " 16256 "Could not dispatch " 16257 "sd_reenable_dsense_task\n"); 16258 } 16259 } 16260 #endif /* _LP64 */ 16261 /* FALLTHRU */ 16262 16263 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 16264 if (!un->un_f_has_removable_media) { 16265 break; 16266 } 16267 16268 /* 16269 * When we get a unit attention from a removable-media device, 16270 * it may be in a state that will take a long time to recover 16271 * (e.g., from a reset). Since we are executing in interrupt 16272 * context here, we cannot wait around for the device to come 16273 * back. So hand this command off to sd_media_change_task() 16274 * for deferred processing under taskq thread context. (Note 16275 * that the command still may be failed if a problem is 16276 * encountered at a later time.) 16277 */ 16278 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 16279 KM_NOSLEEP) == 0) { 16280 /* 16281 * Cannot dispatch the request so fail the command. 16282 */ 16283 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16284 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16285 si.ssi_severity = SCSI_ERR_FATAL; 16286 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16287 sd_return_failed_command(un, bp, EIO); 16288 } 16289 16290 /* 16291 * If failed to dispatch sd_media_change_task(), we already 16292 * updated kstat. If succeed to dispatch sd_media_change_task(), 16293 * we should update kstat later if it encounters an error. So, 16294 * we update kstat_updated flag here. 16295 */ 16296 kstat_updated = B_TRUE; 16297 16298 /* 16299 * Either the command has been successfully dispatched to a 16300 * task Q for retrying, or the dispatch failed. In either case 16301 * do NOT retry again by calling sd_retry_command. This sets up 16302 * two retries of the same command and when one completes and 16303 * frees the resources the other will access freed memory, 16304 * a bad thing. 16305 */ 16306 return; 16307 16308 default: 16309 break; 16310 } 16311 16312 /* 16313 * Update kstat if we haven't done that. 16314 */ 16315 if (!kstat_updated) { 16316 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16317 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16318 } 16319 16320 do_retry: 16321 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 16322 EIO, SD_UA_RETRY_DELAY, NULL); 16323 } 16324 16325 16326 16327 /* 16328 * Function: sd_sense_key_fail_command 16329 * 16330 * Description: Use to fail a command when we don't like the sense key that 16331 * was returned. 16332 * 16333 * Context: May be called from interrupt context 16334 */ 16335 16336 static void 16337 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 16338 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16339 { 16340 struct sd_sense_info si; 16341 16342 ASSERT(un != NULL); 16343 ASSERT(mutex_owned(SD_MUTEX(un))); 16344 ASSERT(bp != NULL); 16345 ASSERT(xp != NULL); 16346 ASSERT(pktp != NULL); 16347 16348 si.ssi_severity = SCSI_ERR_FATAL; 16349 si.ssi_pfa_flag = FALSE; 16350 16351 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16352 sd_return_failed_command(un, bp, EIO); 16353 } 16354 16355 16356 16357 /* 16358 * Function: sd_sense_key_blank_check 16359 * 16360 * Description: Recovery actions for a SCSI "Blank Check" sense key. 16361 * Has no monetary connotation. 16362 * 16363 * Context: May be called from interrupt context 16364 */ 16365 16366 static void 16367 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 16368 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16369 { 16370 struct sd_sense_info si; 16371 16372 ASSERT(un != NULL); 16373 ASSERT(mutex_owned(SD_MUTEX(un))); 16374 ASSERT(bp != NULL); 16375 ASSERT(xp != NULL); 16376 ASSERT(pktp != NULL); 16377 16378 /* 16379 * Blank check is not fatal for removable devices, therefore 16380 * it does not require a console message. 16381 */ 16382 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 16383 SCSI_ERR_FATAL; 16384 si.ssi_pfa_flag = FALSE; 16385 16386 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16387 sd_return_failed_command(un, bp, EIO); 16388 } 16389 16390 16391 16392 16393 /* 16394 * Function: sd_sense_key_aborted_command 16395 * 16396 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 16397 * 16398 * Context: May be called from interrupt context 16399 */ 16400 16401 static void 16402 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 16403 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16404 { 16405 struct sd_sense_info si; 16406 16407 ASSERT(un != NULL); 16408 ASSERT(mutex_owned(SD_MUTEX(un))); 16409 ASSERT(bp != NULL); 16410 ASSERT(xp != NULL); 16411 ASSERT(pktp != NULL); 16412 16413 si.ssi_severity = SCSI_ERR_FATAL; 16414 si.ssi_pfa_flag = FALSE; 16415 16416 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16417 16418 /* 16419 * This really ought to be a fatal error, but we will retry anyway 16420 * as some drives report this as a spurious error. 16421 */ 16422 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16423 &si, EIO, drv_usectohz(100000), NULL); 16424 } 16425 16426 16427 16428 /* 16429 * Function: sd_sense_key_default 16430 * 16431 * Description: Default recovery action for several SCSI sense keys (basically 16432 * attempts a retry). 16433 * 16434 * Context: May be called from interrupt context 16435 */ 16436 16437 static void 16438 sd_sense_key_default(struct sd_lun *un, 16439 uint8_t *sense_datap, 16440 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16441 { 16442 struct sd_sense_info si; 16443 uint8_t sense_key = scsi_sense_key(sense_datap); 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_harderrs); 16452 16453 /* 16454 * Undecoded sense key. Attempt retries and hope that will fix 16455 * the problem. Otherwise, we're dead. 16456 */ 16457 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16458 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16459 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 16460 } 16461 16462 si.ssi_severity = SCSI_ERR_FATAL; 16463 si.ssi_pfa_flag = FALSE; 16464 16465 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16466 &si, EIO, (clock_t)0, NULL); 16467 } 16468 16469 16470 16471 /* 16472 * Function: sd_print_retry_msg 16473 * 16474 * Description: Print a message indicating the retry action being taken. 16475 * 16476 * Arguments: un - ptr to associated softstate 16477 * bp - ptr to buf(9S) for the command 16478 * arg - not used. 16479 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16480 * or SD_NO_RETRY_ISSUED 16481 * 16482 * Context: May be called from interrupt context 16483 */ 16484 /* ARGSUSED */ 16485 static void 16486 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 16487 { 16488 struct sd_xbuf *xp; 16489 struct scsi_pkt *pktp; 16490 char *reasonp; 16491 char *msgp; 16492 16493 ASSERT(un != NULL); 16494 ASSERT(mutex_owned(SD_MUTEX(un))); 16495 ASSERT(bp != NULL); 16496 pktp = SD_GET_PKTP(bp); 16497 ASSERT(pktp != NULL); 16498 xp = SD_GET_XBUF(bp); 16499 ASSERT(xp != NULL); 16500 16501 ASSERT(!mutex_owned(&un->un_pm_mutex)); 16502 mutex_enter(&un->un_pm_mutex); 16503 if ((un->un_state == SD_STATE_SUSPENDED) || 16504 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 16505 (pktp->pkt_flags & FLAG_SILENT)) { 16506 mutex_exit(&un->un_pm_mutex); 16507 goto update_pkt_reason; 16508 } 16509 mutex_exit(&un->un_pm_mutex); 16510 16511 /* 16512 * Suppress messages if they are all the same pkt_reason; with 16513 * TQ, many (up to 256) are returned with the same pkt_reason. 16514 * If we are in panic, then suppress the retry messages. 16515 */ 16516 switch (flag) { 16517 case SD_NO_RETRY_ISSUED: 16518 msgp = "giving up"; 16519 break; 16520 case SD_IMMEDIATE_RETRY_ISSUED: 16521 case SD_DELAYED_RETRY_ISSUED: 16522 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 16523 ((pktp->pkt_reason == un->un_last_pkt_reason) && 16524 (sd_error_level != SCSI_ERR_ALL))) { 16525 return; 16526 } 16527 msgp = "retrying command"; 16528 break; 16529 default: 16530 goto update_pkt_reason; 16531 } 16532 16533 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 16534 scsi_rname(pktp->pkt_reason)); 16535 16536 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16537 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 16538 16539 update_pkt_reason: 16540 /* 16541 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 16542 * This is to prevent multiple console messages for the same failure 16543 * condition. Note that un->un_last_pkt_reason is NOT restored if & 16544 * when the command is retried successfully because there still may be 16545 * more commands coming back with the same value of pktp->pkt_reason. 16546 */ 16547 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 16548 un->un_last_pkt_reason = pktp->pkt_reason; 16549 } 16550 } 16551 16552 16553 /* 16554 * Function: sd_print_cmd_incomplete_msg 16555 * 16556 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 16557 * 16558 * Arguments: un - ptr to associated softstate 16559 * bp - ptr to buf(9S) for the command 16560 * arg - passed to sd_print_retry_msg() 16561 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16562 * or SD_NO_RETRY_ISSUED 16563 * 16564 * Context: May be called from interrupt context 16565 */ 16566 16567 static void 16568 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 16569 int code) 16570 { 16571 dev_info_t *dip; 16572 16573 ASSERT(un != NULL); 16574 ASSERT(mutex_owned(SD_MUTEX(un))); 16575 ASSERT(bp != NULL); 16576 16577 switch (code) { 16578 case SD_NO_RETRY_ISSUED: 16579 /* Command was failed. Someone turned off this target? */ 16580 if (un->un_state != SD_STATE_OFFLINE) { 16581 /* 16582 * Suppress message if we are detaching and 16583 * device has been disconnected 16584 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 16585 * private interface and not part of the DDI 16586 */ 16587 dip = un->un_sd->sd_dev; 16588 if (!(DEVI_IS_DETACHING(dip) && 16589 DEVI_IS_DEVICE_REMOVED(dip))) { 16590 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16591 "disk not responding to selection\n"); 16592 } 16593 New_state(un, SD_STATE_OFFLINE); 16594 } 16595 break; 16596 16597 case SD_DELAYED_RETRY_ISSUED: 16598 case SD_IMMEDIATE_RETRY_ISSUED: 16599 default: 16600 /* Command was successfully queued for retry */ 16601 sd_print_retry_msg(un, bp, arg, code); 16602 break; 16603 } 16604 } 16605 16606 16607 /* 16608 * Function: sd_pkt_reason_cmd_incomplete 16609 * 16610 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 16611 * 16612 * Context: May be called from interrupt context 16613 */ 16614 16615 static void 16616 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 16617 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16618 { 16619 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 16620 16621 ASSERT(un != NULL); 16622 ASSERT(mutex_owned(SD_MUTEX(un))); 16623 ASSERT(bp != NULL); 16624 ASSERT(xp != NULL); 16625 ASSERT(pktp != NULL); 16626 16627 /* Do not do a reset if selection did not complete */ 16628 /* Note: Should this not just check the bit? */ 16629 if (pktp->pkt_state != STATE_GOT_BUS) { 16630 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16631 sd_reset_target(un, pktp); 16632 } 16633 16634 /* 16635 * If the target was not successfully selected, then set 16636 * SD_RETRIES_FAILFAST to indicate that we lost communication 16637 * with the target, and further retries and/or commands are 16638 * likely to take a long time. 16639 */ 16640 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 16641 flag |= SD_RETRIES_FAILFAST; 16642 } 16643 16644 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16645 16646 sd_retry_command(un, bp, flag, 16647 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16648 } 16649 16650 16651 16652 /* 16653 * Function: sd_pkt_reason_cmd_tran_err 16654 * 16655 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 16656 * 16657 * Context: May be called from interrupt context 16658 */ 16659 16660 static void 16661 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 16662 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16663 { 16664 ASSERT(un != NULL); 16665 ASSERT(mutex_owned(SD_MUTEX(un))); 16666 ASSERT(bp != NULL); 16667 ASSERT(xp != NULL); 16668 ASSERT(pktp != NULL); 16669 16670 /* 16671 * Do not reset if we got a parity error, or if 16672 * selection did not complete. 16673 */ 16674 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16675 /* Note: Should this not just check the bit for pkt_state? */ 16676 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 16677 (pktp->pkt_state != STATE_GOT_BUS)) { 16678 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16679 sd_reset_target(un, pktp); 16680 } 16681 16682 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16683 16684 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16685 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16686 } 16687 16688 16689 16690 /* 16691 * Function: sd_pkt_reason_cmd_reset 16692 * 16693 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 16694 * 16695 * Context: May be called from interrupt context 16696 */ 16697 16698 static void 16699 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 16700 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16701 { 16702 ASSERT(un != NULL); 16703 ASSERT(mutex_owned(SD_MUTEX(un))); 16704 ASSERT(bp != NULL); 16705 ASSERT(xp != NULL); 16706 ASSERT(pktp != NULL); 16707 16708 /* The target may still be running the command, so try to reset. */ 16709 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16710 sd_reset_target(un, pktp); 16711 16712 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16713 16714 /* 16715 * If pkt_reason is CMD_RESET chances are that this pkt got 16716 * reset because another target on this bus caused it. The target 16717 * that caused it should get CMD_TIMEOUT with pkt_statistics 16718 * of STAT_TIMEOUT/STAT_DEV_RESET. 16719 */ 16720 16721 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 16722 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16723 } 16724 16725 16726 16727 16728 /* 16729 * Function: sd_pkt_reason_cmd_aborted 16730 * 16731 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 16732 * 16733 * Context: May be called from interrupt context 16734 */ 16735 16736 static void 16737 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 16738 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16739 { 16740 ASSERT(un != NULL); 16741 ASSERT(mutex_owned(SD_MUTEX(un))); 16742 ASSERT(bp != NULL); 16743 ASSERT(xp != NULL); 16744 ASSERT(pktp != NULL); 16745 16746 /* The target may still be running the command, so try to reset. */ 16747 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16748 sd_reset_target(un, pktp); 16749 16750 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16751 16752 /* 16753 * If pkt_reason is CMD_ABORTED chances are that this pkt got 16754 * aborted because another target on this bus caused it. The target 16755 * that caused it should get CMD_TIMEOUT with pkt_statistics 16756 * of STAT_TIMEOUT/STAT_DEV_RESET. 16757 */ 16758 16759 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 16760 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16761 } 16762 16763 16764 16765 /* 16766 * Function: sd_pkt_reason_cmd_timeout 16767 * 16768 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 16769 * 16770 * Context: May be called from interrupt context 16771 */ 16772 16773 static void 16774 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 16775 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16776 { 16777 ASSERT(un != NULL); 16778 ASSERT(mutex_owned(SD_MUTEX(un))); 16779 ASSERT(bp != NULL); 16780 ASSERT(xp != NULL); 16781 ASSERT(pktp != NULL); 16782 16783 16784 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16785 sd_reset_target(un, pktp); 16786 16787 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16788 16789 /* 16790 * A command timeout indicates that we could not establish 16791 * communication with the target, so set SD_RETRIES_FAILFAST 16792 * as further retries/commands are likely to take a long time. 16793 */ 16794 sd_retry_command(un, bp, 16795 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 16796 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16797 } 16798 16799 16800 16801 /* 16802 * Function: sd_pkt_reason_cmd_unx_bus_free 16803 * 16804 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 16805 * 16806 * Context: May be called from interrupt context 16807 */ 16808 16809 static void 16810 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 16811 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16812 { 16813 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 16814 16815 ASSERT(un != NULL); 16816 ASSERT(mutex_owned(SD_MUTEX(un))); 16817 ASSERT(bp != NULL); 16818 ASSERT(xp != NULL); 16819 ASSERT(pktp != NULL); 16820 16821 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16822 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16823 16824 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 16825 sd_print_retry_msg : NULL; 16826 16827 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16828 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16829 } 16830 16831 16832 /* 16833 * Function: sd_pkt_reason_cmd_tag_reject 16834 * 16835 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 16836 * 16837 * Context: May be called from interrupt context 16838 */ 16839 16840 static void 16841 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 16842 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16843 { 16844 ASSERT(un != NULL); 16845 ASSERT(mutex_owned(SD_MUTEX(un))); 16846 ASSERT(bp != NULL); 16847 ASSERT(xp != NULL); 16848 ASSERT(pktp != NULL); 16849 16850 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16851 pktp->pkt_flags = 0; 16852 un->un_tagflags = 0; 16853 if (un->un_f_opt_queueing == TRUE) { 16854 un->un_throttle = min(un->un_throttle, 3); 16855 } else { 16856 un->un_throttle = 1; 16857 } 16858 mutex_exit(SD_MUTEX(un)); 16859 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 16860 mutex_enter(SD_MUTEX(un)); 16861 16862 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16863 16864 /* Legacy behavior not to check retry counts here. */ 16865 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 16866 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16867 } 16868 16869 16870 /* 16871 * Function: sd_pkt_reason_default 16872 * 16873 * Description: Default recovery actions for SCSA pkt_reason values that 16874 * do not have more explicit recovery actions. 16875 * 16876 * Context: May be called from interrupt context 16877 */ 16878 16879 static void 16880 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 16881 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16882 { 16883 ASSERT(un != NULL); 16884 ASSERT(mutex_owned(SD_MUTEX(un))); 16885 ASSERT(bp != NULL); 16886 ASSERT(xp != NULL); 16887 ASSERT(pktp != NULL); 16888 16889 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16890 sd_reset_target(un, pktp); 16891 16892 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16893 16894 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16895 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16896 } 16897 16898 16899 16900 /* 16901 * Function: sd_pkt_status_check_condition 16902 * 16903 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 16904 * 16905 * Context: May be called from interrupt context 16906 */ 16907 16908 static void 16909 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 16910 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16911 { 16912 ASSERT(un != NULL); 16913 ASSERT(mutex_owned(SD_MUTEX(un))); 16914 ASSERT(bp != NULL); 16915 ASSERT(xp != NULL); 16916 ASSERT(pktp != NULL); 16917 16918 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 16919 "entry: buf:0x%p xp:0x%p\n", bp, xp); 16920 16921 /* 16922 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 16923 * command will be retried after the request sense). Otherwise, retry 16924 * the command. Note: we are issuing the request sense even though the 16925 * retry limit may have been reached for the failed command. 16926 */ 16927 if (un->un_f_arq_enabled == FALSE) { 16928 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 16929 "no ARQ, sending request sense command\n"); 16930 sd_send_request_sense_command(un, bp, pktp); 16931 } else { 16932 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 16933 "ARQ,retrying request sense command\n"); 16934 #if defined(__i386) || defined(__amd64) 16935 /* 16936 * The SD_RETRY_DELAY value need to be adjusted here 16937 * when SD_RETRY_DELAY change in sddef.h 16938 */ 16939 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 16940 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 16941 NULL); 16942 #else 16943 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 16944 EIO, SD_RETRY_DELAY, NULL); 16945 #endif 16946 } 16947 16948 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 16949 } 16950 16951 16952 /* 16953 * Function: sd_pkt_status_busy 16954 * 16955 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 16956 * 16957 * Context: May be called from interrupt context 16958 */ 16959 16960 static void 16961 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16962 struct scsi_pkt *pktp) 16963 { 16964 ASSERT(un != NULL); 16965 ASSERT(mutex_owned(SD_MUTEX(un))); 16966 ASSERT(bp != NULL); 16967 ASSERT(xp != NULL); 16968 ASSERT(pktp != NULL); 16969 16970 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16971 "sd_pkt_status_busy: entry\n"); 16972 16973 /* If retries are exhausted, just fail the command. */ 16974 if (xp->xb_retry_count >= un->un_busy_retry_count) { 16975 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16976 "device busy too long\n"); 16977 sd_return_failed_command(un, bp, EIO); 16978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16979 "sd_pkt_status_busy: exit\n"); 16980 return; 16981 } 16982 xp->xb_retry_count++; 16983 16984 /* 16985 * Try to reset the target. However, we do not want to perform 16986 * more than one reset if the device continues to fail. The reset 16987 * will be performed when the retry count reaches the reset 16988 * threshold. This threshold should be set such that at least 16989 * one retry is issued before the reset is performed. 16990 */ 16991 if (xp->xb_retry_count == 16992 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 16993 int rval = 0; 16994 mutex_exit(SD_MUTEX(un)); 16995 if (un->un_f_allow_bus_device_reset == TRUE) { 16996 /* 16997 * First try to reset the LUN; if we cannot then 16998 * try to reset the target. 16999 */ 17000 if (un->un_f_lun_reset_enabled == TRUE) { 17001 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17002 "sd_pkt_status_busy: RESET_LUN\n"); 17003 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17004 } 17005 if (rval == 0) { 17006 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17007 "sd_pkt_status_busy: RESET_TARGET\n"); 17008 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17009 } 17010 } 17011 if (rval == 0) { 17012 /* 17013 * If the RESET_LUN and/or RESET_TARGET failed, 17014 * try RESET_ALL 17015 */ 17016 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17017 "sd_pkt_status_busy: RESET_ALL\n"); 17018 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 17019 } 17020 mutex_enter(SD_MUTEX(un)); 17021 if (rval == 0) { 17022 /* 17023 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 17024 * At this point we give up & fail the command. 17025 */ 17026 sd_return_failed_command(un, bp, EIO); 17027 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17028 "sd_pkt_status_busy: exit (failed cmd)\n"); 17029 return; 17030 } 17031 } 17032 17033 /* 17034 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 17035 * we have already checked the retry counts above. 17036 */ 17037 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 17038 EIO, SD_BSY_TIMEOUT, NULL); 17039 17040 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17041 "sd_pkt_status_busy: exit\n"); 17042 } 17043 17044 17045 /* 17046 * Function: sd_pkt_status_reservation_conflict 17047 * 17048 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 17049 * command status. 17050 * 17051 * Context: May be called from interrupt context 17052 */ 17053 17054 static void 17055 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 17056 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17057 { 17058 ASSERT(un != NULL); 17059 ASSERT(mutex_owned(SD_MUTEX(un))); 17060 ASSERT(bp != NULL); 17061 ASSERT(xp != NULL); 17062 ASSERT(pktp != NULL); 17063 17064 /* 17065 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 17066 * conflict could be due to various reasons like incorrect keys, not 17067 * registered or not reserved etc. So, we return EACCES to the caller. 17068 */ 17069 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 17070 int cmd = SD_GET_PKT_OPCODE(pktp); 17071 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 17072 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 17073 sd_return_failed_command(un, bp, EACCES); 17074 return; 17075 } 17076 } 17077 17078 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 17079 17080 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 17081 if (sd_failfast_enable != 0) { 17082 /* By definition, we must panic here.... */ 17083 sd_panic_for_res_conflict(un); 17084 /*NOTREACHED*/ 17085 } 17086 SD_ERROR(SD_LOG_IO, un, 17087 "sd_handle_resv_conflict: Disk Reserved\n"); 17088 sd_return_failed_command(un, bp, EACCES); 17089 return; 17090 } 17091 17092 /* 17093 * 1147670: retry only if sd_retry_on_reservation_conflict 17094 * property is set (default is 1). Retries will not succeed 17095 * on a disk reserved by another initiator. HA systems 17096 * may reset this via sd.conf to avoid these retries. 17097 * 17098 * Note: The legacy return code for this failure is EIO, however EACCES 17099 * seems more appropriate for a reservation conflict. 17100 */ 17101 if (sd_retry_on_reservation_conflict == 0) { 17102 SD_ERROR(SD_LOG_IO, un, 17103 "sd_handle_resv_conflict: Device Reserved\n"); 17104 sd_return_failed_command(un, bp, EIO); 17105 return; 17106 } 17107 17108 /* 17109 * Retry the command if we can. 17110 * 17111 * Note: The legacy return code for this failure is EIO, however EACCES 17112 * seems more appropriate for a reservation conflict. 17113 */ 17114 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17115 (clock_t)2, NULL); 17116 } 17117 17118 17119 17120 /* 17121 * Function: sd_pkt_status_qfull 17122 * 17123 * Description: Handle a QUEUE FULL condition from the target. This can 17124 * occur if the HBA does not handle the queue full condition. 17125 * (Basically this means third-party HBAs as Sun HBAs will 17126 * handle the queue full condition.) Note that if there are 17127 * some commands already in the transport, then the queue full 17128 * has occurred because the queue for this nexus is actually 17129 * full. If there are no commands in the transport, then the 17130 * queue full is resulting from some other initiator or lun 17131 * consuming all the resources at the target. 17132 * 17133 * Context: May be called from interrupt context 17134 */ 17135 17136 static void 17137 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 17138 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17139 { 17140 ASSERT(un != NULL); 17141 ASSERT(mutex_owned(SD_MUTEX(un))); 17142 ASSERT(bp != NULL); 17143 ASSERT(xp != NULL); 17144 ASSERT(pktp != NULL); 17145 17146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17147 "sd_pkt_status_qfull: entry\n"); 17148 17149 /* 17150 * Just lower the QFULL throttle and retry the command. Note that 17151 * we do not limit the number of retries here. 17152 */ 17153 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 17154 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 17155 SD_RESTART_TIMEOUT, NULL); 17156 17157 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17158 "sd_pkt_status_qfull: exit\n"); 17159 } 17160 17161 17162 /* 17163 * Function: sd_reset_target 17164 * 17165 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 17166 * RESET_TARGET, or RESET_ALL. 17167 * 17168 * Context: May be called under interrupt context. 17169 */ 17170 17171 static void 17172 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 17173 { 17174 int rval = 0; 17175 17176 ASSERT(un != NULL); 17177 ASSERT(mutex_owned(SD_MUTEX(un))); 17178 ASSERT(pktp != NULL); 17179 17180 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 17181 17182 /* 17183 * No need to reset if the transport layer has already done so. 17184 */ 17185 if ((pktp->pkt_statistics & 17186 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 17187 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17188 "sd_reset_target: no reset\n"); 17189 return; 17190 } 17191 17192 mutex_exit(SD_MUTEX(un)); 17193 17194 if (un->un_f_allow_bus_device_reset == TRUE) { 17195 if (un->un_f_lun_reset_enabled == TRUE) { 17196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17197 "sd_reset_target: RESET_LUN\n"); 17198 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17199 } 17200 if (rval == 0) { 17201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17202 "sd_reset_target: RESET_TARGET\n"); 17203 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17204 } 17205 } 17206 17207 if (rval == 0) { 17208 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17209 "sd_reset_target: RESET_ALL\n"); 17210 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 17211 } 17212 17213 mutex_enter(SD_MUTEX(un)); 17214 17215 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 17216 } 17217 17218 17219 /* 17220 * Function: sd_media_change_task 17221 * 17222 * Description: Recovery action for CDROM to become available. 17223 * 17224 * Context: Executes in a taskq() thread context 17225 */ 17226 17227 static void 17228 sd_media_change_task(void *arg) 17229 { 17230 struct scsi_pkt *pktp = arg; 17231 struct sd_lun *un; 17232 struct buf *bp; 17233 struct sd_xbuf *xp; 17234 int err = 0; 17235 int retry_count = 0; 17236 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 17237 struct sd_sense_info si; 17238 17239 ASSERT(pktp != NULL); 17240 bp = (struct buf *)pktp->pkt_private; 17241 ASSERT(bp != NULL); 17242 xp = SD_GET_XBUF(bp); 17243 ASSERT(xp != NULL); 17244 un = SD_GET_UN(bp); 17245 ASSERT(un != NULL); 17246 ASSERT(!mutex_owned(SD_MUTEX(un))); 17247 ASSERT(un->un_f_monitor_media_state); 17248 17249 si.ssi_severity = SCSI_ERR_INFO; 17250 si.ssi_pfa_flag = FALSE; 17251 17252 /* 17253 * When a reset is issued on a CDROM, it takes a long time to 17254 * recover. First few attempts to read capacity and other things 17255 * related to handling unit attention fail (with a ASC 0x4 and 17256 * ASCQ 0x1). In that case we want to do enough retries and we want 17257 * to limit the retries in other cases of genuine failures like 17258 * no media in drive. 17259 */ 17260 while (retry_count++ < retry_limit) { 17261 if ((err = sd_handle_mchange(un)) == 0) { 17262 break; 17263 } 17264 if (err == EAGAIN) { 17265 retry_limit = SD_UNIT_ATTENTION_RETRY; 17266 } 17267 /* Sleep for 0.5 sec. & try again */ 17268 delay(drv_usectohz(500000)); 17269 } 17270 17271 /* 17272 * Dispatch (retry or fail) the original command here, 17273 * along with appropriate console messages.... 17274 * 17275 * Must grab the mutex before calling sd_retry_command, 17276 * sd_print_sense_msg and sd_return_failed_command. 17277 */ 17278 mutex_enter(SD_MUTEX(un)); 17279 if (err != SD_CMD_SUCCESS) { 17280 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17281 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17282 si.ssi_severity = SCSI_ERR_FATAL; 17283 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17284 sd_return_failed_command(un, bp, EIO); 17285 } else { 17286 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17287 &si, EIO, (clock_t)0, NULL); 17288 } 17289 mutex_exit(SD_MUTEX(un)); 17290 } 17291 17292 17293 17294 /* 17295 * Function: sd_handle_mchange 17296 * 17297 * Description: Perform geometry validation & other recovery when CDROM 17298 * has been removed from drive. 17299 * 17300 * Return Code: 0 for success 17301 * errno-type return code of either sd_send_scsi_DOORLOCK() or 17302 * sd_send_scsi_READ_CAPACITY() 17303 * 17304 * Context: Executes in a taskq() thread context 17305 */ 17306 17307 static int 17308 sd_handle_mchange(struct sd_lun *un) 17309 { 17310 uint64_t capacity; 17311 uint32_t lbasize; 17312 int rval; 17313 17314 ASSERT(!mutex_owned(SD_MUTEX(un))); 17315 ASSERT(un->un_f_monitor_media_state); 17316 17317 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 17318 SD_PATH_DIRECT_PRIORITY)) != 0) { 17319 return (rval); 17320 } 17321 17322 mutex_enter(SD_MUTEX(un)); 17323 sd_update_block_info(un, lbasize, capacity); 17324 17325 if (un->un_errstats != NULL) { 17326 struct sd_errstats *stp = 17327 (struct sd_errstats *)un->un_errstats->ks_data; 17328 stp->sd_capacity.value.ui64 = (uint64_t) 17329 ((uint64_t)un->un_blockcount * 17330 (uint64_t)un->un_tgt_blocksize); 17331 } 17332 17333 17334 /* 17335 * Check if the media in the device is writable or not 17336 */ 17337 if (ISCD(un)) 17338 sd_check_for_writable_cd(un, SD_PATH_DIRECT_PRIORITY); 17339 17340 /* 17341 * Note: Maybe let the strategy/partitioning chain worry about getting 17342 * valid geometry. 17343 */ 17344 mutex_exit(SD_MUTEX(un)); 17345 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 17346 17347 17348 if (cmlb_validate(un->un_cmlbhandle, 0, 17349 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 17350 return (EIO); 17351 } else { 17352 if (un->un_f_pkstats_enabled) { 17353 sd_set_pstats(un); 17354 SD_TRACE(SD_LOG_IO_PARTITION, un, 17355 "sd_handle_mchange: un:0x%p pstats created and " 17356 "set\n", un); 17357 } 17358 } 17359 17360 17361 /* 17362 * Try to lock the door 17363 */ 17364 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 17365 SD_PATH_DIRECT_PRIORITY)); 17366 } 17367 17368 17369 /* 17370 * Function: sd_send_scsi_DOORLOCK 17371 * 17372 * Description: Issue the scsi DOOR LOCK command 17373 * 17374 * Arguments: un - pointer to driver soft state (unit) structure for 17375 * this target. 17376 * flag - SD_REMOVAL_ALLOW 17377 * SD_REMOVAL_PREVENT 17378 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17379 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17380 * to use the USCSI "direct" chain and bypass the normal 17381 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17382 * command is issued as part of an error recovery action. 17383 * 17384 * Return Code: 0 - Success 17385 * errno return code from sd_send_scsi_cmd() 17386 * 17387 * Context: Can sleep. 17388 */ 17389 17390 static int 17391 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 17392 { 17393 union scsi_cdb cdb; 17394 struct uscsi_cmd ucmd_buf; 17395 struct scsi_extended_sense sense_buf; 17396 int status; 17397 17398 ASSERT(un != NULL); 17399 ASSERT(!mutex_owned(SD_MUTEX(un))); 17400 17401 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 17402 17403 /* already determined doorlock is not supported, fake success */ 17404 if (un->un_f_doorlock_supported == FALSE) { 17405 return (0); 17406 } 17407 17408 /* 17409 * If we are ejecting and see an SD_REMOVAL_PREVENT 17410 * ignore the command so we can complete the eject 17411 * operation. 17412 */ 17413 if (flag == SD_REMOVAL_PREVENT) { 17414 mutex_enter(SD_MUTEX(un)); 17415 if (un->un_f_ejecting == TRUE) { 17416 mutex_exit(SD_MUTEX(un)); 17417 return (EAGAIN); 17418 } 17419 mutex_exit(SD_MUTEX(un)); 17420 } 17421 17422 bzero(&cdb, sizeof (cdb)); 17423 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17424 17425 cdb.scc_cmd = SCMD_DOORLOCK; 17426 cdb.cdb_opaque[4] = (uchar_t)flag; 17427 17428 ucmd_buf.uscsi_cdb = (char *)&cdb; 17429 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 17430 ucmd_buf.uscsi_bufaddr = NULL; 17431 ucmd_buf.uscsi_buflen = 0; 17432 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17433 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17434 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 17435 ucmd_buf.uscsi_timeout = 15; 17436 17437 SD_TRACE(SD_LOG_IO, un, 17438 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 17439 17440 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17441 UIO_SYSSPACE, path_flag); 17442 17443 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 17444 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17445 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 17446 /* fake success and skip subsequent doorlock commands */ 17447 un->un_f_doorlock_supported = FALSE; 17448 return (0); 17449 } 17450 17451 return (status); 17452 } 17453 17454 /* 17455 * Function: sd_send_scsi_READ_CAPACITY 17456 * 17457 * Description: This routine uses the scsi READ CAPACITY command to determine 17458 * the device capacity in number of blocks and the device native 17459 * block size. If this function returns a failure, then the 17460 * values in *capp and *lbap are undefined. If the capacity 17461 * returned is 0xffffffff then the lun is too large for a 17462 * normal READ CAPACITY command and the results of a 17463 * READ CAPACITY 16 will be used instead. 17464 * 17465 * Arguments: un - ptr to soft state struct for the target 17466 * capp - ptr to unsigned 64-bit variable to receive the 17467 * capacity value from the command. 17468 * lbap - ptr to unsigned 32-bit varaible to receive the 17469 * block size value from the command 17470 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17471 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17472 * to use the USCSI "direct" chain and bypass the normal 17473 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17474 * command is issued as part of an error recovery action. 17475 * 17476 * Return Code: 0 - Success 17477 * EIO - IO error 17478 * EACCES - Reservation conflict detected 17479 * EAGAIN - Device is becoming ready 17480 * errno return code from sd_send_scsi_cmd() 17481 * 17482 * Context: Can sleep. Blocks until command completes. 17483 */ 17484 17485 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 17486 17487 static int 17488 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 17489 int path_flag) 17490 { 17491 struct scsi_extended_sense sense_buf; 17492 struct uscsi_cmd ucmd_buf; 17493 union scsi_cdb cdb; 17494 uint32_t *capacity_buf; 17495 uint64_t capacity; 17496 uint32_t lbasize; 17497 int status; 17498 17499 ASSERT(un != NULL); 17500 ASSERT(!mutex_owned(SD_MUTEX(un))); 17501 ASSERT(capp != NULL); 17502 ASSERT(lbap != NULL); 17503 17504 SD_TRACE(SD_LOG_IO, un, 17505 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17506 17507 /* 17508 * First send a READ_CAPACITY command to the target. 17509 * (This command is mandatory under SCSI-2.) 17510 * 17511 * Set up the CDB for the READ_CAPACITY command. The Partial 17512 * Medium Indicator bit is cleared. The address field must be 17513 * zero if the PMI bit is zero. 17514 */ 17515 bzero(&cdb, sizeof (cdb)); 17516 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17517 17518 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 17519 17520 cdb.scc_cmd = SCMD_READ_CAPACITY; 17521 17522 ucmd_buf.uscsi_cdb = (char *)&cdb; 17523 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 17524 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 17525 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 17526 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17527 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17528 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17529 ucmd_buf.uscsi_timeout = 60; 17530 17531 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17532 UIO_SYSSPACE, path_flag); 17533 17534 switch (status) { 17535 case 0: 17536 /* Return failure if we did not get valid capacity data. */ 17537 if (ucmd_buf.uscsi_resid != 0) { 17538 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17539 return (EIO); 17540 } 17541 17542 /* 17543 * Read capacity and block size from the READ CAPACITY 10 data. 17544 * This data may be adjusted later due to device specific 17545 * issues. 17546 * 17547 * According to the SCSI spec, the READ CAPACITY 10 17548 * command returns the following: 17549 * 17550 * bytes 0-3: Maximum logical block address available. 17551 * (MSB in byte:0 & LSB in byte:3) 17552 * 17553 * bytes 4-7: Block length in bytes 17554 * (MSB in byte:4 & LSB in byte:7) 17555 * 17556 */ 17557 capacity = BE_32(capacity_buf[0]); 17558 lbasize = BE_32(capacity_buf[1]); 17559 17560 /* 17561 * Done with capacity_buf 17562 */ 17563 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17564 17565 /* 17566 * if the reported capacity is set to all 0xf's, then 17567 * this disk is too large and requires SBC-2 commands. 17568 * Reissue the request using READ CAPACITY 16. 17569 */ 17570 if (capacity == 0xffffffff) { 17571 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 17572 &lbasize, path_flag); 17573 if (status != 0) { 17574 return (status); 17575 } 17576 } 17577 break; /* Success! */ 17578 case EIO: 17579 switch (ucmd_buf.uscsi_status) { 17580 case STATUS_RESERVATION_CONFLICT: 17581 status = EACCES; 17582 break; 17583 case STATUS_CHECK: 17584 /* 17585 * Check condition; look for ASC/ASCQ of 0x04/0x01 17586 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 17587 */ 17588 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17589 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 17590 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 17591 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17592 return (EAGAIN); 17593 } 17594 break; 17595 default: 17596 break; 17597 } 17598 /* FALLTHRU */ 17599 default: 17600 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17601 return (status); 17602 } 17603 17604 /* 17605 * Some ATAPI CD-ROM drives report inaccurate LBA size values 17606 * (2352 and 0 are common) so for these devices always force the value 17607 * to 2048 as required by the ATAPI specs. 17608 */ 17609 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 17610 lbasize = 2048; 17611 } 17612 17613 /* 17614 * Get the maximum LBA value from the READ CAPACITY data. 17615 * Here we assume that the Partial Medium Indicator (PMI) bit 17616 * was cleared when issuing the command. This means that the LBA 17617 * returned from the device is the LBA of the last logical block 17618 * on the logical unit. The actual logical block count will be 17619 * this value plus one. 17620 * 17621 * Currently the capacity is saved in terms of un->un_sys_blocksize, 17622 * so scale the capacity value to reflect this. 17623 */ 17624 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 17625 17626 /* 17627 * Copy the values from the READ CAPACITY command into the space 17628 * provided by the caller. 17629 */ 17630 *capp = capacity; 17631 *lbap = lbasize; 17632 17633 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 17634 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 17635 17636 /* 17637 * Both the lbasize and capacity from the device must be nonzero, 17638 * otherwise we assume that the values are not valid and return 17639 * failure to the caller. (4203735) 17640 */ 17641 if ((capacity == 0) || (lbasize == 0)) { 17642 return (EIO); 17643 } 17644 17645 return (0); 17646 } 17647 17648 /* 17649 * Function: sd_send_scsi_READ_CAPACITY_16 17650 * 17651 * Description: This routine uses the scsi READ CAPACITY 16 command to 17652 * determine the device capacity in number of blocks and the 17653 * device native block size. If this function returns a failure, 17654 * then the values in *capp and *lbap are undefined. 17655 * This routine should always be called by 17656 * sd_send_scsi_READ_CAPACITY which will appy any device 17657 * specific adjustments to capacity and lbasize. 17658 * 17659 * Arguments: un - ptr to soft state struct for the target 17660 * capp - ptr to unsigned 64-bit variable to receive the 17661 * capacity value from the command. 17662 * lbap - ptr to unsigned 32-bit varaible to receive the 17663 * block size value from the command 17664 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17665 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17666 * to use the USCSI "direct" chain and bypass the normal 17667 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 17668 * this command is issued as part of an error recovery 17669 * action. 17670 * 17671 * Return Code: 0 - Success 17672 * EIO - IO error 17673 * EACCES - Reservation conflict detected 17674 * EAGAIN - Device is becoming ready 17675 * errno return code from sd_send_scsi_cmd() 17676 * 17677 * Context: Can sleep. Blocks until command completes. 17678 */ 17679 17680 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 17681 17682 static int 17683 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 17684 uint32_t *lbap, int path_flag) 17685 { 17686 struct scsi_extended_sense sense_buf; 17687 struct uscsi_cmd ucmd_buf; 17688 union scsi_cdb cdb; 17689 uint64_t *capacity16_buf; 17690 uint64_t capacity; 17691 uint32_t lbasize; 17692 int status; 17693 17694 ASSERT(un != NULL); 17695 ASSERT(!mutex_owned(SD_MUTEX(un))); 17696 ASSERT(capp != NULL); 17697 ASSERT(lbap != NULL); 17698 17699 SD_TRACE(SD_LOG_IO, un, 17700 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17701 17702 /* 17703 * First send a READ_CAPACITY_16 command to the target. 17704 * 17705 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 17706 * Medium Indicator bit is cleared. The address field must be 17707 * zero if the PMI bit is zero. 17708 */ 17709 bzero(&cdb, sizeof (cdb)); 17710 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17711 17712 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 17713 17714 ucmd_buf.uscsi_cdb = (char *)&cdb; 17715 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 17716 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 17717 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 17718 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17719 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17720 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17721 ucmd_buf.uscsi_timeout = 60; 17722 17723 /* 17724 * Read Capacity (16) is a Service Action In command. One 17725 * command byte (0x9E) is overloaded for multiple operations, 17726 * with the second CDB byte specifying the desired operation 17727 */ 17728 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 17729 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 17730 17731 /* 17732 * Fill in allocation length field 17733 */ 17734 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 17735 17736 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17737 UIO_SYSSPACE, path_flag); 17738 17739 switch (status) { 17740 case 0: 17741 /* Return failure if we did not get valid capacity data. */ 17742 if (ucmd_buf.uscsi_resid > 20) { 17743 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17744 return (EIO); 17745 } 17746 17747 /* 17748 * Read capacity and block size from the READ CAPACITY 10 data. 17749 * This data may be adjusted later due to device specific 17750 * issues. 17751 * 17752 * According to the SCSI spec, the READ CAPACITY 10 17753 * command returns the following: 17754 * 17755 * bytes 0-7: Maximum logical block address available. 17756 * (MSB in byte:0 & LSB in byte:7) 17757 * 17758 * bytes 8-11: Block length in bytes 17759 * (MSB in byte:8 & LSB in byte:11) 17760 * 17761 */ 17762 capacity = BE_64(capacity16_buf[0]); 17763 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 17764 17765 /* 17766 * Done with capacity16_buf 17767 */ 17768 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17769 17770 /* 17771 * if the reported capacity is set to all 0xf's, then 17772 * this disk is too large. This could only happen with 17773 * a device that supports LBAs larger than 64 bits which 17774 * are not defined by any current T10 standards. 17775 */ 17776 if (capacity == 0xffffffffffffffff) { 17777 return (EIO); 17778 } 17779 break; /* Success! */ 17780 case EIO: 17781 switch (ucmd_buf.uscsi_status) { 17782 case STATUS_RESERVATION_CONFLICT: 17783 status = EACCES; 17784 break; 17785 case STATUS_CHECK: 17786 /* 17787 * Check condition; look for ASC/ASCQ of 0x04/0x01 17788 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 17789 */ 17790 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17791 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 17792 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 17793 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17794 return (EAGAIN); 17795 } 17796 break; 17797 default: 17798 break; 17799 } 17800 /* FALLTHRU */ 17801 default: 17802 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17803 return (status); 17804 } 17805 17806 *capp = capacity; 17807 *lbap = lbasize; 17808 17809 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 17810 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 17811 17812 return (0); 17813 } 17814 17815 17816 /* 17817 * Function: sd_send_scsi_START_STOP_UNIT 17818 * 17819 * Description: Issue a scsi START STOP UNIT command to the target. 17820 * 17821 * Arguments: un - pointer to driver soft state (unit) structure for 17822 * this target. 17823 * flag - SD_TARGET_START 17824 * SD_TARGET_STOP 17825 * SD_TARGET_EJECT 17826 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17827 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17828 * to use the USCSI "direct" chain and bypass the normal 17829 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17830 * command is issued as part of an error recovery action. 17831 * 17832 * Return Code: 0 - Success 17833 * EIO - IO error 17834 * EACCES - Reservation conflict detected 17835 * ENXIO - Not Ready, medium not present 17836 * errno return code from sd_send_scsi_cmd() 17837 * 17838 * Context: Can sleep. 17839 */ 17840 17841 static int 17842 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 17843 { 17844 struct scsi_extended_sense sense_buf; 17845 union scsi_cdb cdb; 17846 struct uscsi_cmd ucmd_buf; 17847 int status; 17848 17849 ASSERT(un != NULL); 17850 ASSERT(!mutex_owned(SD_MUTEX(un))); 17851 17852 SD_TRACE(SD_LOG_IO, un, 17853 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 17854 17855 if (un->un_f_check_start_stop && 17856 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 17857 (un->un_f_start_stop_supported != TRUE)) { 17858 return (0); 17859 } 17860 17861 /* 17862 * If we are performing an eject operation and 17863 * we receive any command other than SD_TARGET_EJECT 17864 * we should immediately return. 17865 */ 17866 if (flag != SD_TARGET_EJECT) { 17867 mutex_enter(SD_MUTEX(un)); 17868 if (un->un_f_ejecting == TRUE) { 17869 mutex_exit(SD_MUTEX(un)); 17870 return (EAGAIN); 17871 } 17872 mutex_exit(SD_MUTEX(un)); 17873 } 17874 17875 bzero(&cdb, sizeof (cdb)); 17876 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17877 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 17878 17879 cdb.scc_cmd = SCMD_START_STOP; 17880 cdb.cdb_opaque[4] = (uchar_t)flag; 17881 17882 ucmd_buf.uscsi_cdb = (char *)&cdb; 17883 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 17884 ucmd_buf.uscsi_bufaddr = NULL; 17885 ucmd_buf.uscsi_buflen = 0; 17886 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17887 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 17888 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 17889 ucmd_buf.uscsi_timeout = 200; 17890 17891 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17892 UIO_SYSSPACE, path_flag); 17893 17894 switch (status) { 17895 case 0: 17896 break; /* Success! */ 17897 case EIO: 17898 switch (ucmd_buf.uscsi_status) { 17899 case STATUS_RESERVATION_CONFLICT: 17900 status = EACCES; 17901 break; 17902 case STATUS_CHECK: 17903 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 17904 switch (scsi_sense_key( 17905 (uint8_t *)&sense_buf)) { 17906 case KEY_ILLEGAL_REQUEST: 17907 status = ENOTSUP; 17908 break; 17909 case KEY_NOT_READY: 17910 if (scsi_sense_asc( 17911 (uint8_t *)&sense_buf) 17912 == 0x3A) { 17913 status = ENXIO; 17914 } 17915 break; 17916 default: 17917 break; 17918 } 17919 } 17920 break; 17921 default: 17922 break; 17923 } 17924 break; 17925 default: 17926 break; 17927 } 17928 17929 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 17930 17931 return (status); 17932 } 17933 17934 17935 /* 17936 * Function: sd_start_stop_unit_callback 17937 * 17938 * Description: timeout(9F) callback to begin recovery process for a 17939 * device that has spun down. 17940 * 17941 * Arguments: arg - pointer to associated softstate struct. 17942 * 17943 * Context: Executes in a timeout(9F) thread context 17944 */ 17945 17946 static void 17947 sd_start_stop_unit_callback(void *arg) 17948 { 17949 struct sd_lun *un = arg; 17950 ASSERT(un != NULL); 17951 ASSERT(!mutex_owned(SD_MUTEX(un))); 17952 17953 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 17954 17955 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 17956 } 17957 17958 17959 /* 17960 * Function: sd_start_stop_unit_task 17961 * 17962 * Description: Recovery procedure when a drive is spun down. 17963 * 17964 * Arguments: arg - pointer to associated softstate struct. 17965 * 17966 * Context: Executes in a taskq() thread context 17967 */ 17968 17969 static void 17970 sd_start_stop_unit_task(void *arg) 17971 { 17972 struct sd_lun *un = arg; 17973 17974 ASSERT(un != NULL); 17975 ASSERT(!mutex_owned(SD_MUTEX(un))); 17976 17977 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 17978 17979 /* 17980 * Some unformatted drives report not ready error, no need to 17981 * restart if format has been initiated. 17982 */ 17983 mutex_enter(SD_MUTEX(un)); 17984 if (un->un_f_format_in_progress == TRUE) { 17985 mutex_exit(SD_MUTEX(un)); 17986 return; 17987 } 17988 mutex_exit(SD_MUTEX(un)); 17989 17990 /* 17991 * When a START STOP command is issued from here, it is part of a 17992 * failure recovery operation and must be issued before any other 17993 * commands, including any pending retries. Thus it must be sent 17994 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 17995 * succeeds or not, we will start I/O after the attempt. 17996 */ 17997 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 17998 SD_PATH_DIRECT_PRIORITY); 17999 18000 /* 18001 * The above call blocks until the START_STOP_UNIT command completes. 18002 * Now that it has completed, we must re-try the original IO that 18003 * received the NOT READY condition in the first place. There are 18004 * three possible conditions here: 18005 * 18006 * (1) The original IO is on un_retry_bp. 18007 * (2) The original IO is on the regular wait queue, and un_retry_bp 18008 * is NULL. 18009 * (3) The original IO is on the regular wait queue, and un_retry_bp 18010 * points to some other, unrelated bp. 18011 * 18012 * For each case, we must call sd_start_cmds() with un_retry_bp 18013 * as the argument. If un_retry_bp is NULL, this will initiate 18014 * processing of the regular wait queue. If un_retry_bp is not NULL, 18015 * then this will process the bp on un_retry_bp. That may or may not 18016 * be the original IO, but that does not matter: the important thing 18017 * is to keep the IO processing going at this point. 18018 * 18019 * Note: This is a very specific error recovery sequence associated 18020 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 18021 * serialize the I/O with completion of the spin-up. 18022 */ 18023 mutex_enter(SD_MUTEX(un)); 18024 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18025 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 18026 un, un->un_retry_bp); 18027 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 18028 sd_start_cmds(un, un->un_retry_bp); 18029 mutex_exit(SD_MUTEX(un)); 18030 18031 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 18032 } 18033 18034 18035 /* 18036 * Function: sd_send_scsi_INQUIRY 18037 * 18038 * Description: Issue the scsi INQUIRY command. 18039 * 18040 * Arguments: un 18041 * bufaddr 18042 * buflen 18043 * evpd 18044 * page_code 18045 * page_length 18046 * 18047 * Return Code: 0 - Success 18048 * errno return code from sd_send_scsi_cmd() 18049 * 18050 * Context: Can sleep. Does not return until command is completed. 18051 */ 18052 18053 static int 18054 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 18055 uchar_t evpd, uchar_t page_code, size_t *residp) 18056 { 18057 union scsi_cdb cdb; 18058 struct uscsi_cmd ucmd_buf; 18059 int status; 18060 18061 ASSERT(un != NULL); 18062 ASSERT(!mutex_owned(SD_MUTEX(un))); 18063 ASSERT(bufaddr != NULL); 18064 18065 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 18066 18067 bzero(&cdb, sizeof (cdb)); 18068 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18069 bzero(bufaddr, buflen); 18070 18071 cdb.scc_cmd = SCMD_INQUIRY; 18072 cdb.cdb_opaque[1] = evpd; 18073 cdb.cdb_opaque[2] = page_code; 18074 FORMG0COUNT(&cdb, buflen); 18075 18076 ucmd_buf.uscsi_cdb = (char *)&cdb; 18077 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18078 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18079 ucmd_buf.uscsi_buflen = buflen; 18080 ucmd_buf.uscsi_rqbuf = NULL; 18081 ucmd_buf.uscsi_rqlen = 0; 18082 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 18083 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 18084 18085 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18086 UIO_SYSSPACE, SD_PATH_DIRECT); 18087 18088 if ((status == 0) && (residp != NULL)) { 18089 *residp = ucmd_buf.uscsi_resid; 18090 } 18091 18092 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 18093 18094 return (status); 18095 } 18096 18097 18098 /* 18099 * Function: sd_send_scsi_TEST_UNIT_READY 18100 * 18101 * Description: Issue the scsi TEST UNIT READY command. 18102 * This routine can be told to set the flag USCSI_DIAGNOSE to 18103 * prevent retrying failed commands. Use this when the intent 18104 * is either to check for device readiness, to clear a Unit 18105 * Attention, or to clear any outstanding sense data. 18106 * However under specific conditions the expected behavior 18107 * is for retries to bring a device ready, so use the flag 18108 * with caution. 18109 * 18110 * Arguments: un 18111 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 18112 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 18113 * 0: dont check for media present, do retries on cmd. 18114 * 18115 * Return Code: 0 - Success 18116 * EIO - IO error 18117 * EACCES - Reservation conflict detected 18118 * ENXIO - Not Ready, medium not present 18119 * errno return code from sd_send_scsi_cmd() 18120 * 18121 * Context: Can sleep. Does not return until command is completed. 18122 */ 18123 18124 static int 18125 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 18126 { 18127 struct scsi_extended_sense sense_buf; 18128 union scsi_cdb cdb; 18129 struct uscsi_cmd ucmd_buf; 18130 int status; 18131 18132 ASSERT(un != NULL); 18133 ASSERT(!mutex_owned(SD_MUTEX(un))); 18134 18135 SD_TRACE(SD_LOG_IO, un, 18136 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 18137 18138 /* 18139 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 18140 * timeouts when they receive a TUR and the queue is not empty. Check 18141 * the configuration flag set during attach (indicating the drive has 18142 * this firmware bug) and un_ncmds_in_transport before issuing the 18143 * TUR. If there are 18144 * pending commands return success, this is a bit arbitrary but is ok 18145 * for non-removables (i.e. the eliteI disks) and non-clustering 18146 * configurations. 18147 */ 18148 if (un->un_f_cfg_tur_check == TRUE) { 18149 mutex_enter(SD_MUTEX(un)); 18150 if (un->un_ncmds_in_transport != 0) { 18151 mutex_exit(SD_MUTEX(un)); 18152 return (0); 18153 } 18154 mutex_exit(SD_MUTEX(un)); 18155 } 18156 18157 bzero(&cdb, sizeof (cdb)); 18158 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18159 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18160 18161 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 18162 18163 ucmd_buf.uscsi_cdb = (char *)&cdb; 18164 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18165 ucmd_buf.uscsi_bufaddr = NULL; 18166 ucmd_buf.uscsi_buflen = 0; 18167 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18168 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18169 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18170 18171 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 18172 if ((flag & SD_DONT_RETRY_TUR) != 0) { 18173 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 18174 } 18175 ucmd_buf.uscsi_timeout = 60; 18176 18177 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18178 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 18179 SD_PATH_STANDARD)); 18180 18181 switch (status) { 18182 case 0: 18183 break; /* Success! */ 18184 case EIO: 18185 switch (ucmd_buf.uscsi_status) { 18186 case STATUS_RESERVATION_CONFLICT: 18187 status = EACCES; 18188 break; 18189 case STATUS_CHECK: 18190 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 18191 break; 18192 } 18193 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18194 (scsi_sense_key((uint8_t *)&sense_buf) == 18195 KEY_NOT_READY) && 18196 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 18197 status = ENXIO; 18198 } 18199 break; 18200 default: 18201 break; 18202 } 18203 break; 18204 default: 18205 break; 18206 } 18207 18208 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 18209 18210 return (status); 18211 } 18212 18213 18214 /* 18215 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 18216 * 18217 * Description: Issue the scsi PERSISTENT RESERVE IN command. 18218 * 18219 * Arguments: un 18220 * 18221 * Return Code: 0 - Success 18222 * EACCES 18223 * ENOTSUP 18224 * errno return code from sd_send_scsi_cmd() 18225 * 18226 * Context: Can sleep. Does not return until command is completed. 18227 */ 18228 18229 static int 18230 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 18231 uint16_t data_len, uchar_t *data_bufp) 18232 { 18233 struct scsi_extended_sense sense_buf; 18234 union scsi_cdb cdb; 18235 struct uscsi_cmd ucmd_buf; 18236 int status; 18237 int no_caller_buf = FALSE; 18238 18239 ASSERT(un != NULL); 18240 ASSERT(!mutex_owned(SD_MUTEX(un))); 18241 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 18242 18243 SD_TRACE(SD_LOG_IO, un, 18244 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 18245 18246 bzero(&cdb, sizeof (cdb)); 18247 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18248 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18249 if (data_bufp == NULL) { 18250 /* Allocate a default buf if the caller did not give one */ 18251 ASSERT(data_len == 0); 18252 data_len = MHIOC_RESV_KEY_SIZE; 18253 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 18254 no_caller_buf = TRUE; 18255 } 18256 18257 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 18258 cdb.cdb_opaque[1] = usr_cmd; 18259 FORMG1COUNT(&cdb, data_len); 18260 18261 ucmd_buf.uscsi_cdb = (char *)&cdb; 18262 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18263 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 18264 ucmd_buf.uscsi_buflen = data_len; 18265 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18266 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18267 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18268 ucmd_buf.uscsi_timeout = 60; 18269 18270 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18271 UIO_SYSSPACE, SD_PATH_STANDARD); 18272 18273 switch (status) { 18274 case 0: 18275 break; /* Success! */ 18276 case EIO: 18277 switch (ucmd_buf.uscsi_status) { 18278 case STATUS_RESERVATION_CONFLICT: 18279 status = EACCES; 18280 break; 18281 case STATUS_CHECK: 18282 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18283 (scsi_sense_key((uint8_t *)&sense_buf) == 18284 KEY_ILLEGAL_REQUEST)) { 18285 status = ENOTSUP; 18286 } 18287 break; 18288 default: 18289 break; 18290 } 18291 break; 18292 default: 18293 break; 18294 } 18295 18296 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 18297 18298 if (no_caller_buf == TRUE) { 18299 kmem_free(data_bufp, data_len); 18300 } 18301 18302 return (status); 18303 } 18304 18305 18306 /* 18307 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 18308 * 18309 * Description: This routine is the driver entry point for handling CD-ROM 18310 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 18311 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 18312 * device. 18313 * 18314 * Arguments: un - Pointer to soft state struct for the target. 18315 * usr_cmd SCSI-3 reservation facility command (one of 18316 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 18317 * SD_SCSI3_PREEMPTANDABORT) 18318 * usr_bufp - user provided pointer register, reserve descriptor or 18319 * preempt and abort structure (mhioc_register_t, 18320 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 18321 * 18322 * Return Code: 0 - Success 18323 * EACCES 18324 * ENOTSUP 18325 * errno return code from sd_send_scsi_cmd() 18326 * 18327 * Context: Can sleep. Does not return until command is completed. 18328 */ 18329 18330 static int 18331 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 18332 uchar_t *usr_bufp) 18333 { 18334 struct scsi_extended_sense sense_buf; 18335 union scsi_cdb cdb; 18336 struct uscsi_cmd ucmd_buf; 18337 int status; 18338 uchar_t data_len = sizeof (sd_prout_t); 18339 sd_prout_t *prp; 18340 18341 ASSERT(un != NULL); 18342 ASSERT(!mutex_owned(SD_MUTEX(un))); 18343 ASSERT(data_len == 24); /* required by scsi spec */ 18344 18345 SD_TRACE(SD_LOG_IO, un, 18346 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 18347 18348 if (usr_bufp == NULL) { 18349 return (EINVAL); 18350 } 18351 18352 bzero(&cdb, sizeof (cdb)); 18353 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18354 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18355 prp = kmem_zalloc(data_len, KM_SLEEP); 18356 18357 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 18358 cdb.cdb_opaque[1] = usr_cmd; 18359 FORMG1COUNT(&cdb, data_len); 18360 18361 ucmd_buf.uscsi_cdb = (char *)&cdb; 18362 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18363 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 18364 ucmd_buf.uscsi_buflen = data_len; 18365 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18366 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18367 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 18368 ucmd_buf.uscsi_timeout = 60; 18369 18370 switch (usr_cmd) { 18371 case SD_SCSI3_REGISTER: { 18372 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 18373 18374 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18375 bcopy(ptr->newkey.key, prp->service_key, 18376 MHIOC_RESV_KEY_SIZE); 18377 prp->aptpl = ptr->aptpl; 18378 break; 18379 } 18380 case SD_SCSI3_RESERVE: 18381 case SD_SCSI3_RELEASE: { 18382 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 18383 18384 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18385 prp->scope_address = BE_32(ptr->scope_specific_addr); 18386 cdb.cdb_opaque[2] = ptr->type; 18387 break; 18388 } 18389 case SD_SCSI3_PREEMPTANDABORT: { 18390 mhioc_preemptandabort_t *ptr = 18391 (mhioc_preemptandabort_t *)usr_bufp; 18392 18393 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18394 bcopy(ptr->victim_key.key, prp->service_key, 18395 MHIOC_RESV_KEY_SIZE); 18396 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 18397 cdb.cdb_opaque[2] = ptr->resvdesc.type; 18398 ucmd_buf.uscsi_flags |= USCSI_HEAD; 18399 break; 18400 } 18401 case SD_SCSI3_REGISTERANDIGNOREKEY: 18402 { 18403 mhioc_registerandignorekey_t *ptr; 18404 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 18405 bcopy(ptr->newkey.key, 18406 prp->service_key, MHIOC_RESV_KEY_SIZE); 18407 prp->aptpl = ptr->aptpl; 18408 break; 18409 } 18410 default: 18411 ASSERT(FALSE); 18412 break; 18413 } 18414 18415 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18416 UIO_SYSSPACE, SD_PATH_STANDARD); 18417 18418 switch (status) { 18419 case 0: 18420 break; /* Success! */ 18421 case EIO: 18422 switch (ucmd_buf.uscsi_status) { 18423 case STATUS_RESERVATION_CONFLICT: 18424 status = EACCES; 18425 break; 18426 case STATUS_CHECK: 18427 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18428 (scsi_sense_key((uint8_t *)&sense_buf) == 18429 KEY_ILLEGAL_REQUEST)) { 18430 status = ENOTSUP; 18431 } 18432 break; 18433 default: 18434 break; 18435 } 18436 break; 18437 default: 18438 break; 18439 } 18440 18441 kmem_free(prp, data_len); 18442 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 18443 return (status); 18444 } 18445 18446 18447 /* 18448 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 18449 * 18450 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 18451 * 18452 * Arguments: un - pointer to the target's soft state struct 18453 * 18454 * Return Code: 0 - success 18455 * errno-type error code 18456 * 18457 * Context: kernel thread context only. 18458 */ 18459 18460 static int 18461 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 18462 { 18463 struct sd_uscsi_info *uip; 18464 struct uscsi_cmd *uscmd; 18465 union scsi_cdb *cdb; 18466 struct buf *bp; 18467 int rval = 0; 18468 18469 SD_TRACE(SD_LOG_IO, un, 18470 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 18471 18472 ASSERT(un != NULL); 18473 ASSERT(!mutex_owned(SD_MUTEX(un))); 18474 18475 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 18476 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 18477 18478 /* 18479 * First get some memory for the uscsi_cmd struct and cdb 18480 * and initialize for SYNCHRONIZE_CACHE cmd. 18481 */ 18482 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 18483 uscmd->uscsi_cdblen = CDB_GROUP1; 18484 uscmd->uscsi_cdb = (caddr_t)cdb; 18485 uscmd->uscsi_bufaddr = NULL; 18486 uscmd->uscsi_buflen = 0; 18487 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 18488 uscmd->uscsi_rqlen = SENSE_LENGTH; 18489 uscmd->uscsi_rqresid = SENSE_LENGTH; 18490 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18491 uscmd->uscsi_timeout = sd_io_time; 18492 18493 /* 18494 * Allocate an sd_uscsi_info struct and fill it with the info 18495 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 18496 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 18497 * since we allocate the buf here in this function, we do not 18498 * need to preserve the prior contents of b_private. 18499 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 18500 */ 18501 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 18502 uip->ui_flags = SD_PATH_DIRECT; 18503 uip->ui_cmdp = uscmd; 18504 18505 bp = getrbuf(KM_SLEEP); 18506 bp->b_private = uip; 18507 18508 /* 18509 * Setup buffer to carry uscsi request. 18510 */ 18511 bp->b_flags = B_BUSY; 18512 bp->b_bcount = 0; 18513 bp->b_blkno = 0; 18514 18515 if (dkc != NULL) { 18516 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 18517 uip->ui_dkc = *dkc; 18518 } 18519 18520 bp->b_edev = SD_GET_DEV(un); 18521 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 18522 18523 (void) sd_uscsi_strategy(bp); 18524 18525 /* 18526 * If synchronous request, wait for completion 18527 * If async just return and let b_iodone callback 18528 * cleanup. 18529 * NOTE: On return, u_ncmds_in_driver will be decremented, 18530 * but it was also incremented in sd_uscsi_strategy(), so 18531 * we should be ok. 18532 */ 18533 if (dkc == NULL) { 18534 (void) biowait(bp); 18535 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 18536 } 18537 18538 return (rval); 18539 } 18540 18541 18542 static int 18543 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 18544 { 18545 struct sd_uscsi_info *uip; 18546 struct uscsi_cmd *uscmd; 18547 uint8_t *sense_buf; 18548 struct sd_lun *un; 18549 int status; 18550 18551 uip = (struct sd_uscsi_info *)(bp->b_private); 18552 ASSERT(uip != NULL); 18553 18554 uscmd = uip->ui_cmdp; 18555 ASSERT(uscmd != NULL); 18556 18557 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 18558 ASSERT(sense_buf != NULL); 18559 18560 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 18561 ASSERT(un != NULL); 18562 18563 status = geterror(bp); 18564 switch (status) { 18565 case 0: 18566 break; /* Success! */ 18567 case EIO: 18568 switch (uscmd->uscsi_status) { 18569 case STATUS_RESERVATION_CONFLICT: 18570 /* Ignore reservation conflict */ 18571 status = 0; 18572 goto done; 18573 18574 case STATUS_CHECK: 18575 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 18576 (scsi_sense_key(sense_buf) == 18577 KEY_ILLEGAL_REQUEST)) { 18578 /* Ignore Illegal Request error */ 18579 mutex_enter(SD_MUTEX(un)); 18580 un->un_f_sync_cache_supported = FALSE; 18581 mutex_exit(SD_MUTEX(un)); 18582 status = ENOTSUP; 18583 goto done; 18584 } 18585 break; 18586 default: 18587 break; 18588 } 18589 /* FALLTHRU */ 18590 default: 18591 /* 18592 * Don't log an error message if this device 18593 * has removable media. 18594 */ 18595 if (!un->un_f_has_removable_media) { 18596 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18597 "SYNCHRONIZE CACHE command failed (%d)\n", status); 18598 } 18599 break; 18600 } 18601 18602 done: 18603 if (uip->ui_dkc.dkc_callback != NULL) { 18604 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 18605 } 18606 18607 ASSERT((bp->b_flags & B_REMAPPED) == 0); 18608 freerbuf(bp); 18609 kmem_free(uip, sizeof (struct sd_uscsi_info)); 18610 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 18611 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 18612 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 18613 18614 return (status); 18615 } 18616 18617 18618 /* 18619 * Function: sd_send_scsi_GET_CONFIGURATION 18620 * 18621 * Description: Issues the get configuration command to the device. 18622 * Called from sd_check_for_writable_cd & sd_get_media_info 18623 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 18624 * Arguments: un 18625 * ucmdbuf 18626 * rqbuf 18627 * rqbuflen 18628 * bufaddr 18629 * buflen 18630 * path_flag 18631 * 18632 * Return Code: 0 - Success 18633 * errno return code from sd_send_scsi_cmd() 18634 * 18635 * Context: Can sleep. Does not return until command is completed. 18636 * 18637 */ 18638 18639 static int 18640 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 18641 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 18642 int path_flag) 18643 { 18644 char cdb[CDB_GROUP1]; 18645 int status; 18646 18647 ASSERT(un != NULL); 18648 ASSERT(!mutex_owned(SD_MUTEX(un))); 18649 ASSERT(bufaddr != NULL); 18650 ASSERT(ucmdbuf != NULL); 18651 ASSERT(rqbuf != NULL); 18652 18653 SD_TRACE(SD_LOG_IO, un, 18654 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 18655 18656 bzero(cdb, sizeof (cdb)); 18657 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 18658 bzero(rqbuf, rqbuflen); 18659 bzero(bufaddr, buflen); 18660 18661 /* 18662 * Set up cdb field for the get configuration command. 18663 */ 18664 cdb[0] = SCMD_GET_CONFIGURATION; 18665 cdb[1] = 0x02; /* Requested Type */ 18666 cdb[8] = SD_PROFILE_HEADER_LEN; 18667 ucmdbuf->uscsi_cdb = cdb; 18668 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 18669 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 18670 ucmdbuf->uscsi_buflen = buflen; 18671 ucmdbuf->uscsi_timeout = sd_io_time; 18672 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 18673 ucmdbuf->uscsi_rqlen = rqbuflen; 18674 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 18675 18676 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 18677 UIO_SYSSPACE, path_flag); 18678 18679 switch (status) { 18680 case 0: 18681 break; /* Success! */ 18682 case EIO: 18683 switch (ucmdbuf->uscsi_status) { 18684 case STATUS_RESERVATION_CONFLICT: 18685 status = EACCES; 18686 break; 18687 default: 18688 break; 18689 } 18690 break; 18691 default: 18692 break; 18693 } 18694 18695 if (status == 0) { 18696 SD_DUMP_MEMORY(un, SD_LOG_IO, 18697 "sd_send_scsi_GET_CONFIGURATION: data", 18698 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 18699 } 18700 18701 SD_TRACE(SD_LOG_IO, un, 18702 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 18703 18704 return (status); 18705 } 18706 18707 /* 18708 * Function: sd_send_scsi_feature_GET_CONFIGURATION 18709 * 18710 * Description: Issues the get configuration command to the device to 18711 * retrieve a specific feature. Called from 18712 * sd_check_for_writable_cd & sd_set_mmc_caps. 18713 * Arguments: un 18714 * ucmdbuf 18715 * rqbuf 18716 * rqbuflen 18717 * bufaddr 18718 * buflen 18719 * feature 18720 * 18721 * Return Code: 0 - Success 18722 * errno return code from sd_send_scsi_cmd() 18723 * 18724 * Context: Can sleep. Does not return until command is completed. 18725 * 18726 */ 18727 static int 18728 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 18729 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 18730 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 18731 { 18732 char cdb[CDB_GROUP1]; 18733 int status; 18734 18735 ASSERT(un != NULL); 18736 ASSERT(!mutex_owned(SD_MUTEX(un))); 18737 ASSERT(bufaddr != NULL); 18738 ASSERT(ucmdbuf != NULL); 18739 ASSERT(rqbuf != NULL); 18740 18741 SD_TRACE(SD_LOG_IO, un, 18742 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 18743 18744 bzero(cdb, sizeof (cdb)); 18745 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 18746 bzero(rqbuf, rqbuflen); 18747 bzero(bufaddr, buflen); 18748 18749 /* 18750 * Set up cdb field for the get configuration command. 18751 */ 18752 cdb[0] = SCMD_GET_CONFIGURATION; 18753 cdb[1] = 0x02; /* Requested Type */ 18754 cdb[3] = feature; 18755 cdb[8] = buflen; 18756 ucmdbuf->uscsi_cdb = cdb; 18757 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 18758 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 18759 ucmdbuf->uscsi_buflen = buflen; 18760 ucmdbuf->uscsi_timeout = sd_io_time; 18761 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 18762 ucmdbuf->uscsi_rqlen = rqbuflen; 18763 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 18764 18765 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 18766 UIO_SYSSPACE, path_flag); 18767 18768 switch (status) { 18769 case 0: 18770 break; /* Success! */ 18771 case EIO: 18772 switch (ucmdbuf->uscsi_status) { 18773 case STATUS_RESERVATION_CONFLICT: 18774 status = EACCES; 18775 break; 18776 default: 18777 break; 18778 } 18779 break; 18780 default: 18781 break; 18782 } 18783 18784 if (status == 0) { 18785 SD_DUMP_MEMORY(un, SD_LOG_IO, 18786 "sd_send_scsi_feature_GET_CONFIGURATION: data", 18787 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 18788 } 18789 18790 SD_TRACE(SD_LOG_IO, un, 18791 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 18792 18793 return (status); 18794 } 18795 18796 18797 /* 18798 * Function: sd_send_scsi_MODE_SENSE 18799 * 18800 * Description: Utility function for issuing a scsi MODE SENSE command. 18801 * Note: This routine uses a consistent implementation for Group0, 18802 * Group1, and Group2 commands across all platforms. ATAPI devices 18803 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 18804 * 18805 * Arguments: un - pointer to the softstate struct for the target. 18806 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 18807 * CDB_GROUP[1|2] (10 byte). 18808 * bufaddr - buffer for page data retrieved from the target. 18809 * buflen - size of page to be retrieved. 18810 * page_code - page code of data to be retrieved from the target. 18811 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18812 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18813 * to use the USCSI "direct" chain and bypass the normal 18814 * command waitq. 18815 * 18816 * Return Code: 0 - Success 18817 * errno return code from sd_send_scsi_cmd() 18818 * 18819 * Context: Can sleep. Does not return until command is completed. 18820 */ 18821 18822 static int 18823 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 18824 size_t buflen, uchar_t page_code, int path_flag) 18825 { 18826 struct scsi_extended_sense sense_buf; 18827 union scsi_cdb cdb; 18828 struct uscsi_cmd ucmd_buf; 18829 int status; 18830 int headlen; 18831 18832 ASSERT(un != NULL); 18833 ASSERT(!mutex_owned(SD_MUTEX(un))); 18834 ASSERT(bufaddr != NULL); 18835 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 18836 (cdbsize == CDB_GROUP2)); 18837 18838 SD_TRACE(SD_LOG_IO, un, 18839 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 18840 18841 bzero(&cdb, sizeof (cdb)); 18842 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18843 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18844 bzero(bufaddr, buflen); 18845 18846 if (cdbsize == CDB_GROUP0) { 18847 cdb.scc_cmd = SCMD_MODE_SENSE; 18848 cdb.cdb_opaque[2] = page_code; 18849 FORMG0COUNT(&cdb, buflen); 18850 headlen = MODE_HEADER_LENGTH; 18851 } else { 18852 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 18853 cdb.cdb_opaque[2] = page_code; 18854 FORMG1COUNT(&cdb, buflen); 18855 headlen = MODE_HEADER_LENGTH_GRP2; 18856 } 18857 18858 ASSERT(headlen <= buflen); 18859 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 18860 18861 ucmd_buf.uscsi_cdb = (char *)&cdb; 18862 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 18863 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18864 ucmd_buf.uscsi_buflen = buflen; 18865 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18866 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18867 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18868 ucmd_buf.uscsi_timeout = 60; 18869 18870 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18871 UIO_SYSSPACE, path_flag); 18872 18873 switch (status) { 18874 case 0: 18875 /* 18876 * sr_check_wp() uses 0x3f page code and check the header of 18877 * mode page to determine if target device is write-protected. 18878 * But some USB devices return 0 bytes for 0x3f page code. For 18879 * this case, make sure that mode page header is returned at 18880 * least. 18881 */ 18882 if (buflen - ucmd_buf.uscsi_resid < headlen) 18883 status = EIO; 18884 break; /* Success! */ 18885 case EIO: 18886 switch (ucmd_buf.uscsi_status) { 18887 case STATUS_RESERVATION_CONFLICT: 18888 status = EACCES; 18889 break; 18890 default: 18891 break; 18892 } 18893 break; 18894 default: 18895 break; 18896 } 18897 18898 if (status == 0) { 18899 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 18900 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 18901 } 18902 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 18903 18904 return (status); 18905 } 18906 18907 18908 /* 18909 * Function: sd_send_scsi_MODE_SELECT 18910 * 18911 * Description: Utility function for issuing a scsi MODE SELECT command. 18912 * Note: This routine uses a consistent implementation for Group0, 18913 * Group1, and Group2 commands across all platforms. ATAPI devices 18914 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 18915 * 18916 * Arguments: un - pointer to the softstate struct for the target. 18917 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 18918 * CDB_GROUP[1|2] (10 byte). 18919 * bufaddr - buffer for page data retrieved from the target. 18920 * buflen - size of page to be retrieved. 18921 * save_page - boolean to determin if SP bit should be set. 18922 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18923 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18924 * to use the USCSI "direct" chain and bypass the normal 18925 * command waitq. 18926 * 18927 * Return Code: 0 - Success 18928 * errno return code from sd_send_scsi_cmd() 18929 * 18930 * Context: Can sleep. Does not return until command is completed. 18931 */ 18932 18933 static int 18934 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 18935 size_t buflen, uchar_t save_page, int path_flag) 18936 { 18937 struct scsi_extended_sense sense_buf; 18938 union scsi_cdb cdb; 18939 struct uscsi_cmd ucmd_buf; 18940 int status; 18941 18942 ASSERT(un != NULL); 18943 ASSERT(!mutex_owned(SD_MUTEX(un))); 18944 ASSERT(bufaddr != NULL); 18945 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 18946 (cdbsize == CDB_GROUP2)); 18947 18948 SD_TRACE(SD_LOG_IO, un, 18949 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 18950 18951 bzero(&cdb, sizeof (cdb)); 18952 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18953 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18954 18955 /* Set the PF bit for many third party drives */ 18956 cdb.cdb_opaque[1] = 0x10; 18957 18958 /* Set the savepage(SP) bit if given */ 18959 if (save_page == SD_SAVE_PAGE) { 18960 cdb.cdb_opaque[1] |= 0x01; 18961 } 18962 18963 if (cdbsize == CDB_GROUP0) { 18964 cdb.scc_cmd = SCMD_MODE_SELECT; 18965 FORMG0COUNT(&cdb, buflen); 18966 } else { 18967 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 18968 FORMG1COUNT(&cdb, buflen); 18969 } 18970 18971 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 18972 18973 ucmd_buf.uscsi_cdb = (char *)&cdb; 18974 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 18975 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18976 ucmd_buf.uscsi_buflen = buflen; 18977 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18978 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18979 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 18980 ucmd_buf.uscsi_timeout = 60; 18981 18982 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18983 UIO_SYSSPACE, path_flag); 18984 18985 switch (status) { 18986 case 0: 18987 break; /* Success! */ 18988 case EIO: 18989 switch (ucmd_buf.uscsi_status) { 18990 case STATUS_RESERVATION_CONFLICT: 18991 status = EACCES; 18992 break; 18993 default: 18994 break; 18995 } 18996 break; 18997 default: 18998 break; 18999 } 19000 19001 if (status == 0) { 19002 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 19003 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19004 } 19005 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 19006 19007 return (status); 19008 } 19009 19010 19011 /* 19012 * Function: sd_send_scsi_RDWR 19013 * 19014 * Description: Issue a scsi READ or WRITE command with the given parameters. 19015 * 19016 * Arguments: un: Pointer to the sd_lun struct for the target. 19017 * cmd: SCMD_READ or SCMD_WRITE 19018 * bufaddr: Address of caller's buffer to receive the RDWR data 19019 * buflen: Length of caller's buffer receive the RDWR data. 19020 * start_block: Block number for the start of the RDWR operation. 19021 * (Assumes target-native block size.) 19022 * residp: Pointer to variable to receive the redisual of the 19023 * RDWR operation (may be NULL of no residual requested). 19024 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19025 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19026 * to use the USCSI "direct" chain and bypass the normal 19027 * command waitq. 19028 * 19029 * Return Code: 0 - Success 19030 * errno return code from sd_send_scsi_cmd() 19031 * 19032 * Context: Can sleep. Does not return until command is completed. 19033 */ 19034 19035 static int 19036 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 19037 size_t buflen, daddr_t start_block, int path_flag) 19038 { 19039 struct scsi_extended_sense sense_buf; 19040 union scsi_cdb cdb; 19041 struct uscsi_cmd ucmd_buf; 19042 uint32_t block_count; 19043 int status; 19044 int cdbsize; 19045 uchar_t flag; 19046 19047 ASSERT(un != NULL); 19048 ASSERT(!mutex_owned(SD_MUTEX(un))); 19049 ASSERT(bufaddr != NULL); 19050 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 19051 19052 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 19053 19054 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 19055 return (EINVAL); 19056 } 19057 19058 mutex_enter(SD_MUTEX(un)); 19059 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 19060 mutex_exit(SD_MUTEX(un)); 19061 19062 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 19063 19064 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 19065 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 19066 bufaddr, buflen, start_block, block_count); 19067 19068 bzero(&cdb, sizeof (cdb)); 19069 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19070 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19071 19072 /* Compute CDB size to use */ 19073 if (start_block > 0xffffffff) 19074 cdbsize = CDB_GROUP4; 19075 else if ((start_block & 0xFFE00000) || 19076 (un->un_f_cfg_is_atapi == TRUE)) 19077 cdbsize = CDB_GROUP1; 19078 else 19079 cdbsize = CDB_GROUP0; 19080 19081 switch (cdbsize) { 19082 case CDB_GROUP0: /* 6-byte CDBs */ 19083 cdb.scc_cmd = cmd; 19084 FORMG0ADDR(&cdb, start_block); 19085 FORMG0COUNT(&cdb, block_count); 19086 break; 19087 case CDB_GROUP1: /* 10-byte CDBs */ 19088 cdb.scc_cmd = cmd | SCMD_GROUP1; 19089 FORMG1ADDR(&cdb, start_block); 19090 FORMG1COUNT(&cdb, block_count); 19091 break; 19092 case CDB_GROUP4: /* 16-byte CDBs */ 19093 cdb.scc_cmd = cmd | SCMD_GROUP4; 19094 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 19095 FORMG4COUNT(&cdb, block_count); 19096 break; 19097 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 19098 default: 19099 /* All others reserved */ 19100 return (EINVAL); 19101 } 19102 19103 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 19104 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19105 19106 ucmd_buf.uscsi_cdb = (char *)&cdb; 19107 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19108 ucmd_buf.uscsi_bufaddr = bufaddr; 19109 ucmd_buf.uscsi_buflen = buflen; 19110 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19111 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19112 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 19113 ucmd_buf.uscsi_timeout = 60; 19114 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19115 UIO_SYSSPACE, path_flag); 19116 switch (status) { 19117 case 0: 19118 break; /* Success! */ 19119 case EIO: 19120 switch (ucmd_buf.uscsi_status) { 19121 case STATUS_RESERVATION_CONFLICT: 19122 status = EACCES; 19123 break; 19124 default: 19125 break; 19126 } 19127 break; 19128 default: 19129 break; 19130 } 19131 19132 if (status == 0) { 19133 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 19134 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19135 } 19136 19137 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 19138 19139 return (status); 19140 } 19141 19142 19143 /* 19144 * Function: sd_send_scsi_LOG_SENSE 19145 * 19146 * Description: Issue a scsi LOG_SENSE command with the given parameters. 19147 * 19148 * Arguments: un: Pointer to the sd_lun struct for the target. 19149 * 19150 * Return Code: 0 - Success 19151 * errno return code from sd_send_scsi_cmd() 19152 * 19153 * Context: Can sleep. Does not return until command is completed. 19154 */ 19155 19156 static int 19157 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 19158 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 19159 int path_flag) 19160 19161 { 19162 struct scsi_extended_sense sense_buf; 19163 union scsi_cdb cdb; 19164 struct uscsi_cmd ucmd_buf; 19165 int status; 19166 19167 ASSERT(un != NULL); 19168 ASSERT(!mutex_owned(SD_MUTEX(un))); 19169 19170 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 19171 19172 bzero(&cdb, sizeof (cdb)); 19173 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19174 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19175 19176 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 19177 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 19178 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 19179 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 19180 FORMG1COUNT(&cdb, buflen); 19181 19182 ucmd_buf.uscsi_cdb = (char *)&cdb; 19183 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19184 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19185 ucmd_buf.uscsi_buflen = buflen; 19186 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19187 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19188 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19189 ucmd_buf.uscsi_timeout = 60; 19190 19191 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19192 UIO_SYSSPACE, path_flag); 19193 19194 switch (status) { 19195 case 0: 19196 break; 19197 case EIO: 19198 switch (ucmd_buf.uscsi_status) { 19199 case STATUS_RESERVATION_CONFLICT: 19200 status = EACCES; 19201 break; 19202 case STATUS_CHECK: 19203 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19204 (scsi_sense_key((uint8_t *)&sense_buf) == 19205 KEY_ILLEGAL_REQUEST) && 19206 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 19207 /* 19208 * ASC 0x24: INVALID FIELD IN CDB 19209 */ 19210 switch (page_code) { 19211 case START_STOP_CYCLE_PAGE: 19212 /* 19213 * The start stop cycle counter is 19214 * implemented as page 0x31 in earlier 19215 * generation disks. In new generation 19216 * disks the start stop cycle counter is 19217 * implemented as page 0xE. To properly 19218 * handle this case if an attempt for 19219 * log page 0xE is made and fails we 19220 * will try again using page 0x31. 19221 * 19222 * Network storage BU committed to 19223 * maintain the page 0x31 for this 19224 * purpose and will not have any other 19225 * page implemented with page code 0x31 19226 * until all disks transition to the 19227 * standard page. 19228 */ 19229 mutex_enter(SD_MUTEX(un)); 19230 un->un_start_stop_cycle_page = 19231 START_STOP_CYCLE_VU_PAGE; 19232 cdb.cdb_opaque[2] = 19233 (char)(page_control << 6) | 19234 un->un_start_stop_cycle_page; 19235 mutex_exit(SD_MUTEX(un)); 19236 status = sd_send_scsi_cmd( 19237 SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19238 UIO_SYSSPACE, path_flag); 19239 19240 break; 19241 case TEMPERATURE_PAGE: 19242 status = ENOTTY; 19243 break; 19244 default: 19245 break; 19246 } 19247 } 19248 break; 19249 default: 19250 break; 19251 } 19252 break; 19253 default: 19254 break; 19255 } 19256 19257 if (status == 0) { 19258 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 19259 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19260 } 19261 19262 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 19263 19264 return (status); 19265 } 19266 19267 19268 /* 19269 * Function: sdioctl 19270 * 19271 * Description: Driver's ioctl(9e) entry point function. 19272 * 19273 * Arguments: dev - device number 19274 * cmd - ioctl operation to be performed 19275 * arg - user argument, contains data to be set or reference 19276 * parameter for get 19277 * flag - bit flag, indicating open settings, 32/64 bit type 19278 * cred_p - user credential pointer 19279 * rval_p - calling process return value (OPT) 19280 * 19281 * Return Code: EINVAL 19282 * ENOTTY 19283 * ENXIO 19284 * EIO 19285 * EFAULT 19286 * ENOTSUP 19287 * EPERM 19288 * 19289 * Context: Called from the device switch at normal priority. 19290 */ 19291 19292 static int 19293 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 19294 { 19295 struct sd_lun *un = NULL; 19296 int err = 0; 19297 int i = 0; 19298 cred_t *cr; 19299 int tmprval = EINVAL; 19300 int is_valid; 19301 19302 /* 19303 * All device accesses go thru sdstrategy where we check on suspend 19304 * status 19305 */ 19306 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 19307 return (ENXIO); 19308 } 19309 19310 ASSERT(!mutex_owned(SD_MUTEX(un))); 19311 19312 19313 is_valid = SD_IS_VALID_LABEL(un); 19314 19315 /* 19316 * Moved this wait from sd_uscsi_strategy to here for 19317 * reasons of deadlock prevention. Internal driver commands, 19318 * specifically those to change a devices power level, result 19319 * in a call to sd_uscsi_strategy. 19320 */ 19321 mutex_enter(SD_MUTEX(un)); 19322 while ((un->un_state == SD_STATE_SUSPENDED) || 19323 (un->un_state == SD_STATE_PM_CHANGING)) { 19324 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 19325 } 19326 /* 19327 * Twiddling the counter here protects commands from now 19328 * through to the top of sd_uscsi_strategy. Without the 19329 * counter inc. a power down, for example, could get in 19330 * after the above check for state is made and before 19331 * execution gets to the top of sd_uscsi_strategy. 19332 * That would cause problems. 19333 */ 19334 un->un_ncmds_in_driver++; 19335 19336 if (!is_valid && 19337 (flag & (FNDELAY | FNONBLOCK))) { 19338 switch (cmd) { 19339 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 19340 case DKIOCGVTOC: 19341 case DKIOCGAPART: 19342 case DKIOCPARTINFO: 19343 case DKIOCSGEOM: 19344 case DKIOCSAPART: 19345 case DKIOCGETEFI: 19346 case DKIOCPARTITION: 19347 case DKIOCSVTOC: 19348 case DKIOCSETEFI: 19349 case DKIOCGMBOOT: 19350 case DKIOCSMBOOT: 19351 case DKIOCG_PHYGEOM: 19352 case DKIOCG_VIRTGEOM: 19353 /* let cmlb handle it */ 19354 goto skip_ready_valid; 19355 19356 case CDROMPAUSE: 19357 case CDROMRESUME: 19358 case CDROMPLAYMSF: 19359 case CDROMPLAYTRKIND: 19360 case CDROMREADTOCHDR: 19361 case CDROMREADTOCENTRY: 19362 case CDROMSTOP: 19363 case CDROMSTART: 19364 case CDROMVOLCTRL: 19365 case CDROMSUBCHNL: 19366 case CDROMREADMODE2: 19367 case CDROMREADMODE1: 19368 case CDROMREADOFFSET: 19369 case CDROMSBLKMODE: 19370 case CDROMGBLKMODE: 19371 case CDROMGDRVSPEED: 19372 case CDROMSDRVSPEED: 19373 case CDROMCDDA: 19374 case CDROMCDXA: 19375 case CDROMSUBCODE: 19376 if (!ISCD(un)) { 19377 un->un_ncmds_in_driver--; 19378 ASSERT(un->un_ncmds_in_driver >= 0); 19379 mutex_exit(SD_MUTEX(un)); 19380 return (ENOTTY); 19381 } 19382 break; 19383 case FDEJECT: 19384 case DKIOCEJECT: 19385 case CDROMEJECT: 19386 if (!un->un_f_eject_media_supported) { 19387 un->un_ncmds_in_driver--; 19388 ASSERT(un->un_ncmds_in_driver >= 0); 19389 mutex_exit(SD_MUTEX(un)); 19390 return (ENOTTY); 19391 } 19392 break; 19393 case DKIOCFLUSHWRITECACHE: 19394 mutex_exit(SD_MUTEX(un)); 19395 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19396 if (err != 0) { 19397 mutex_enter(SD_MUTEX(un)); 19398 un->un_ncmds_in_driver--; 19399 ASSERT(un->un_ncmds_in_driver >= 0); 19400 mutex_exit(SD_MUTEX(un)); 19401 return (EIO); 19402 } 19403 mutex_enter(SD_MUTEX(un)); 19404 /* FALLTHROUGH */ 19405 case DKIOCREMOVABLE: 19406 case DKIOCHOTPLUGGABLE: 19407 case DKIOCINFO: 19408 case DKIOCGMEDIAINFO: 19409 case MHIOCENFAILFAST: 19410 case MHIOCSTATUS: 19411 case MHIOCTKOWN: 19412 case MHIOCRELEASE: 19413 case MHIOCGRP_INKEYS: 19414 case MHIOCGRP_INRESV: 19415 case MHIOCGRP_REGISTER: 19416 case MHIOCGRP_RESERVE: 19417 case MHIOCGRP_PREEMPTANDABORT: 19418 case MHIOCGRP_REGISTERANDIGNOREKEY: 19419 case CDROMCLOSETRAY: 19420 case USCSICMD: 19421 goto skip_ready_valid; 19422 default: 19423 break; 19424 } 19425 19426 mutex_exit(SD_MUTEX(un)); 19427 err = sd_ready_and_valid(un); 19428 mutex_enter(SD_MUTEX(un)); 19429 19430 if (err != SD_READY_VALID) { 19431 switch (cmd) { 19432 case DKIOCSTATE: 19433 case CDROMGDRVSPEED: 19434 case CDROMSDRVSPEED: 19435 case FDEJECT: /* for eject command */ 19436 case DKIOCEJECT: 19437 case CDROMEJECT: 19438 case DKIOCREMOVABLE: 19439 case DKIOCHOTPLUGGABLE: 19440 break; 19441 default: 19442 if (un->un_f_has_removable_media) { 19443 err = ENXIO; 19444 } else { 19445 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 19446 if (err == SD_RESERVED_BY_OTHERS) { 19447 err = EACCES; 19448 } else { 19449 err = EIO; 19450 } 19451 } 19452 un->un_ncmds_in_driver--; 19453 ASSERT(un->un_ncmds_in_driver >= 0); 19454 mutex_exit(SD_MUTEX(un)); 19455 return (err); 19456 } 19457 } 19458 } 19459 19460 skip_ready_valid: 19461 mutex_exit(SD_MUTEX(un)); 19462 19463 switch (cmd) { 19464 case DKIOCINFO: 19465 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 19466 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 19467 break; 19468 19469 case DKIOCGMEDIAINFO: 19470 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 19471 err = sd_get_media_info(dev, (caddr_t)arg, flag); 19472 break; 19473 19474 case DKIOCGGEOM: 19475 case DKIOCGVTOC: 19476 case DKIOCGAPART: 19477 case DKIOCPARTINFO: 19478 case DKIOCSGEOM: 19479 case DKIOCSAPART: 19480 case DKIOCGETEFI: 19481 case DKIOCPARTITION: 19482 case DKIOCSVTOC: 19483 case DKIOCSETEFI: 19484 case DKIOCGMBOOT: 19485 case DKIOCSMBOOT: 19486 case DKIOCG_PHYGEOM: 19487 case DKIOCG_VIRTGEOM: 19488 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 19489 19490 /* TUR should spin up */ 19491 19492 if (un->un_f_has_removable_media) 19493 err = sd_send_scsi_TEST_UNIT_READY(un, 19494 SD_CHECK_FOR_MEDIA); 19495 else 19496 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19497 19498 if (err != 0) 19499 break; 19500 19501 err = cmlb_ioctl(un->un_cmlbhandle, dev, 19502 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 19503 19504 if ((err == 0) && 19505 ((cmd == DKIOCSETEFI) || 19506 (un->un_f_pkstats_enabled) && 19507 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC))) { 19508 19509 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 19510 (void *)SD_PATH_DIRECT); 19511 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 19512 sd_set_pstats(un); 19513 SD_TRACE(SD_LOG_IO_PARTITION, un, 19514 "sd_ioctl: un:0x%p pstats created and " 19515 "set\n", un); 19516 } 19517 } 19518 19519 if ((cmd == DKIOCSVTOC) || 19520 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 19521 19522 mutex_enter(SD_MUTEX(un)); 19523 if (un->un_f_devid_supported && 19524 (un->un_f_opt_fab_devid == TRUE)) { 19525 if (un->un_devid == NULL) { 19526 sd_register_devid(un, SD_DEVINFO(un), 19527 SD_TARGET_IS_UNRESERVED); 19528 } else { 19529 /* 19530 * The device id for this disk 19531 * has been fabricated. The 19532 * device id must be preserved 19533 * by writing it back out to 19534 * disk. 19535 */ 19536 if (sd_write_deviceid(un) != 0) { 19537 ddi_devid_free(un->un_devid); 19538 un->un_devid = NULL; 19539 } 19540 } 19541 } 19542 mutex_exit(SD_MUTEX(un)); 19543 } 19544 19545 break; 19546 19547 case DKIOCLOCK: 19548 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 19549 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19550 SD_PATH_STANDARD); 19551 break; 19552 19553 case DKIOCUNLOCK: 19554 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 19555 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 19556 SD_PATH_STANDARD); 19557 break; 19558 19559 case DKIOCSTATE: { 19560 enum dkio_state state; 19561 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 19562 19563 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 19564 err = EFAULT; 19565 } else { 19566 err = sd_check_media(dev, state); 19567 if (err == 0) { 19568 if (ddi_copyout(&un->un_mediastate, (void *)arg, 19569 sizeof (int), flag) != 0) 19570 err = EFAULT; 19571 } 19572 } 19573 break; 19574 } 19575 19576 case DKIOCREMOVABLE: 19577 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 19578 i = un->un_f_has_removable_media ? 1 : 0; 19579 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19580 err = EFAULT; 19581 } else { 19582 err = 0; 19583 } 19584 break; 19585 19586 case DKIOCHOTPLUGGABLE: 19587 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 19588 i = un->un_f_is_hotpluggable ? 1 : 0; 19589 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19590 err = EFAULT; 19591 } else { 19592 err = 0; 19593 } 19594 break; 19595 19596 case DKIOCGTEMPERATURE: 19597 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 19598 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 19599 break; 19600 19601 case MHIOCENFAILFAST: 19602 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 19603 if ((err = drv_priv(cred_p)) == 0) { 19604 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 19605 } 19606 break; 19607 19608 case MHIOCTKOWN: 19609 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 19610 if ((err = drv_priv(cred_p)) == 0) { 19611 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 19612 } 19613 break; 19614 19615 case MHIOCRELEASE: 19616 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 19617 if ((err = drv_priv(cred_p)) == 0) { 19618 err = sd_mhdioc_release(dev); 19619 } 19620 break; 19621 19622 case MHIOCSTATUS: 19623 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 19624 if ((err = drv_priv(cred_p)) == 0) { 19625 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 19626 case 0: 19627 err = 0; 19628 break; 19629 case EACCES: 19630 *rval_p = 1; 19631 err = 0; 19632 break; 19633 default: 19634 err = EIO; 19635 break; 19636 } 19637 } 19638 break; 19639 19640 case MHIOCQRESERVE: 19641 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 19642 if ((err = drv_priv(cred_p)) == 0) { 19643 err = sd_reserve_release(dev, SD_RESERVE); 19644 } 19645 break; 19646 19647 case MHIOCREREGISTERDEVID: 19648 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 19649 if (drv_priv(cred_p) == EPERM) { 19650 err = EPERM; 19651 } else if (!un->un_f_devid_supported) { 19652 err = ENOTTY; 19653 } else { 19654 err = sd_mhdioc_register_devid(dev); 19655 } 19656 break; 19657 19658 case MHIOCGRP_INKEYS: 19659 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 19660 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 19661 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19662 err = ENOTSUP; 19663 } else { 19664 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 19665 flag); 19666 } 19667 } 19668 break; 19669 19670 case MHIOCGRP_INRESV: 19671 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 19672 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 19673 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19674 err = ENOTSUP; 19675 } else { 19676 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 19677 } 19678 } 19679 break; 19680 19681 case MHIOCGRP_REGISTER: 19682 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 19683 if ((err = drv_priv(cred_p)) != EPERM) { 19684 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19685 err = ENOTSUP; 19686 } else if (arg != NULL) { 19687 mhioc_register_t reg; 19688 if (ddi_copyin((void *)arg, ®, 19689 sizeof (mhioc_register_t), flag) != 0) { 19690 err = EFAULT; 19691 } else { 19692 err = 19693 sd_send_scsi_PERSISTENT_RESERVE_OUT( 19694 un, SD_SCSI3_REGISTER, 19695 (uchar_t *)®); 19696 } 19697 } 19698 } 19699 break; 19700 19701 case MHIOCGRP_RESERVE: 19702 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 19703 if ((err = drv_priv(cred_p)) != EPERM) { 19704 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19705 err = ENOTSUP; 19706 } else if (arg != NULL) { 19707 mhioc_resv_desc_t resv_desc; 19708 if (ddi_copyin((void *)arg, &resv_desc, 19709 sizeof (mhioc_resv_desc_t), flag) != 0) { 19710 err = EFAULT; 19711 } else { 19712 err = 19713 sd_send_scsi_PERSISTENT_RESERVE_OUT( 19714 un, SD_SCSI3_RESERVE, 19715 (uchar_t *)&resv_desc); 19716 } 19717 } 19718 } 19719 break; 19720 19721 case MHIOCGRP_PREEMPTANDABORT: 19722 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 19723 if ((err = drv_priv(cred_p)) != EPERM) { 19724 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19725 err = ENOTSUP; 19726 } else if (arg != NULL) { 19727 mhioc_preemptandabort_t preempt_abort; 19728 if (ddi_copyin((void *)arg, &preempt_abort, 19729 sizeof (mhioc_preemptandabort_t), 19730 flag) != 0) { 19731 err = EFAULT; 19732 } else { 19733 err = 19734 sd_send_scsi_PERSISTENT_RESERVE_OUT( 19735 un, SD_SCSI3_PREEMPTANDABORT, 19736 (uchar_t *)&preempt_abort); 19737 } 19738 } 19739 } 19740 break; 19741 19742 case MHIOCGRP_REGISTERANDIGNOREKEY: 19743 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 19744 if ((err = drv_priv(cred_p)) != EPERM) { 19745 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19746 err = ENOTSUP; 19747 } else if (arg != NULL) { 19748 mhioc_registerandignorekey_t r_and_i; 19749 if (ddi_copyin((void *)arg, (void *)&r_and_i, 19750 sizeof (mhioc_registerandignorekey_t), 19751 flag) != 0) { 19752 err = EFAULT; 19753 } else { 19754 err = 19755 sd_send_scsi_PERSISTENT_RESERVE_OUT( 19756 un, SD_SCSI3_REGISTERANDIGNOREKEY, 19757 (uchar_t *)&r_and_i); 19758 } 19759 } 19760 } 19761 break; 19762 19763 case USCSICMD: 19764 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 19765 cr = ddi_get_cred(); 19766 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 19767 err = EPERM; 19768 } else { 19769 enum uio_seg uioseg; 19770 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 19771 UIO_USERSPACE; 19772 if (un->un_f_format_in_progress == TRUE) { 19773 err = EAGAIN; 19774 break; 19775 } 19776 err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg, 19777 flag, uioseg, SD_PATH_STANDARD); 19778 } 19779 break; 19780 19781 case CDROMPAUSE: 19782 case CDROMRESUME: 19783 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 19784 if (!ISCD(un)) { 19785 err = ENOTTY; 19786 } else { 19787 err = sr_pause_resume(dev, cmd); 19788 } 19789 break; 19790 19791 case CDROMPLAYMSF: 19792 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 19793 if (!ISCD(un)) { 19794 err = ENOTTY; 19795 } else { 19796 err = sr_play_msf(dev, (caddr_t)arg, flag); 19797 } 19798 break; 19799 19800 case CDROMPLAYTRKIND: 19801 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 19802 #if defined(__i386) || defined(__amd64) 19803 /* 19804 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 19805 */ 19806 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 19807 #else 19808 if (!ISCD(un)) { 19809 #endif 19810 err = ENOTTY; 19811 } else { 19812 err = sr_play_trkind(dev, (caddr_t)arg, flag); 19813 } 19814 break; 19815 19816 case CDROMREADTOCHDR: 19817 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 19818 if (!ISCD(un)) { 19819 err = ENOTTY; 19820 } else { 19821 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 19822 } 19823 break; 19824 19825 case CDROMREADTOCENTRY: 19826 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 19827 if (!ISCD(un)) { 19828 err = ENOTTY; 19829 } else { 19830 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 19831 } 19832 break; 19833 19834 case CDROMSTOP: 19835 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 19836 if (!ISCD(un)) { 19837 err = ENOTTY; 19838 } else { 19839 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 19840 SD_PATH_STANDARD); 19841 } 19842 break; 19843 19844 case CDROMSTART: 19845 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 19846 if (!ISCD(un)) { 19847 err = ENOTTY; 19848 } else { 19849 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19850 SD_PATH_STANDARD); 19851 } 19852 break; 19853 19854 case CDROMCLOSETRAY: 19855 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 19856 if (!ISCD(un)) { 19857 err = ENOTTY; 19858 } else { 19859 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 19860 SD_PATH_STANDARD); 19861 } 19862 break; 19863 19864 case FDEJECT: /* for eject command */ 19865 case DKIOCEJECT: 19866 case CDROMEJECT: 19867 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 19868 if (!un->un_f_eject_media_supported) { 19869 err = ENOTTY; 19870 } else { 19871 err = sr_eject(dev); 19872 } 19873 break; 19874 19875 case CDROMVOLCTRL: 19876 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 19877 if (!ISCD(un)) { 19878 err = ENOTTY; 19879 } else { 19880 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 19881 } 19882 break; 19883 19884 case CDROMSUBCHNL: 19885 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 19886 if (!ISCD(un)) { 19887 err = ENOTTY; 19888 } else { 19889 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 19890 } 19891 break; 19892 19893 case CDROMREADMODE2: 19894 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 19895 if (!ISCD(un)) { 19896 err = ENOTTY; 19897 } else if (un->un_f_cfg_is_atapi == TRUE) { 19898 /* 19899 * If the drive supports READ CD, use that instead of 19900 * switching the LBA size via a MODE SELECT 19901 * Block Descriptor 19902 */ 19903 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 19904 } else { 19905 err = sr_read_mode2(dev, (caddr_t)arg, flag); 19906 } 19907 break; 19908 19909 case CDROMREADMODE1: 19910 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 19911 if (!ISCD(un)) { 19912 err = ENOTTY; 19913 } else { 19914 err = sr_read_mode1(dev, (caddr_t)arg, flag); 19915 } 19916 break; 19917 19918 case CDROMREADOFFSET: 19919 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 19920 if (!ISCD(un)) { 19921 err = ENOTTY; 19922 } else { 19923 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 19924 flag); 19925 } 19926 break; 19927 19928 case CDROMSBLKMODE: 19929 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 19930 /* 19931 * There is no means of changing block size in case of atapi 19932 * drives, thus return ENOTTY if drive type is atapi 19933 */ 19934 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 19935 err = ENOTTY; 19936 } else if (un->un_f_mmc_cap == TRUE) { 19937 19938 /* 19939 * MMC Devices do not support changing the 19940 * logical block size 19941 * 19942 * Note: EINVAL is being returned instead of ENOTTY to 19943 * maintain consistancy with the original mmc 19944 * driver update. 19945 */ 19946 err = EINVAL; 19947 } else { 19948 mutex_enter(SD_MUTEX(un)); 19949 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 19950 (un->un_ncmds_in_transport > 0)) { 19951 mutex_exit(SD_MUTEX(un)); 19952 err = EINVAL; 19953 } else { 19954 mutex_exit(SD_MUTEX(un)); 19955 err = sr_change_blkmode(dev, cmd, arg, flag); 19956 } 19957 } 19958 break; 19959 19960 case CDROMGBLKMODE: 19961 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 19962 if (!ISCD(un)) { 19963 err = ENOTTY; 19964 } else if ((un->un_f_cfg_is_atapi != FALSE) && 19965 (un->un_f_blockcount_is_valid != FALSE)) { 19966 /* 19967 * Drive is an ATAPI drive so return target block 19968 * size for ATAPI drives since we cannot change the 19969 * blocksize on ATAPI drives. Used primarily to detect 19970 * if an ATAPI cdrom is present. 19971 */ 19972 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 19973 sizeof (int), flag) != 0) { 19974 err = EFAULT; 19975 } else { 19976 err = 0; 19977 } 19978 19979 } else { 19980 /* 19981 * Drive supports changing block sizes via a Mode 19982 * Select. 19983 */ 19984 err = sr_change_blkmode(dev, cmd, arg, flag); 19985 } 19986 break; 19987 19988 case CDROMGDRVSPEED: 19989 case CDROMSDRVSPEED: 19990 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 19991 if (!ISCD(un)) { 19992 err = ENOTTY; 19993 } else if (un->un_f_mmc_cap == TRUE) { 19994 /* 19995 * Note: In the future the driver implementation 19996 * for getting and 19997 * setting cd speed should entail: 19998 * 1) If non-mmc try the Toshiba mode page 19999 * (sr_change_speed) 20000 * 2) If mmc but no support for Real Time Streaming try 20001 * the SET CD SPEED (0xBB) command 20002 * (sr_atapi_change_speed) 20003 * 3) If mmc and support for Real Time Streaming 20004 * try the GET PERFORMANCE and SET STREAMING 20005 * commands (not yet implemented, 4380808) 20006 */ 20007 /* 20008 * As per recent MMC spec, CD-ROM speed is variable 20009 * and changes with LBA. Since there is no such 20010 * things as drive speed now, fail this ioctl. 20011 * 20012 * Note: EINVAL is returned for consistancy of original 20013 * implementation which included support for getting 20014 * the drive speed of mmc devices but not setting 20015 * the drive speed. Thus EINVAL would be returned 20016 * if a set request was made for an mmc device. 20017 * We no longer support get or set speed for 20018 * mmc but need to remain consistent with regard 20019 * to the error code returned. 20020 */ 20021 err = EINVAL; 20022 } else if (un->un_f_cfg_is_atapi == TRUE) { 20023 err = sr_atapi_change_speed(dev, cmd, arg, flag); 20024 } else { 20025 err = sr_change_speed(dev, cmd, arg, flag); 20026 } 20027 break; 20028 20029 case CDROMCDDA: 20030 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 20031 if (!ISCD(un)) { 20032 err = ENOTTY; 20033 } else { 20034 err = sr_read_cdda(dev, (void *)arg, flag); 20035 } 20036 break; 20037 20038 case CDROMCDXA: 20039 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 20040 if (!ISCD(un)) { 20041 err = ENOTTY; 20042 } else { 20043 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 20044 } 20045 break; 20046 20047 case CDROMSUBCODE: 20048 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 20049 if (!ISCD(un)) { 20050 err = ENOTTY; 20051 } else { 20052 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 20053 } 20054 break; 20055 20056 20057 #ifdef SDDEBUG 20058 /* RESET/ABORTS testing ioctls */ 20059 case DKIOCRESET: { 20060 int reset_level; 20061 20062 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 20063 err = EFAULT; 20064 } else { 20065 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 20066 "reset_level = 0x%lx\n", reset_level); 20067 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 20068 err = 0; 20069 } else { 20070 err = EIO; 20071 } 20072 } 20073 break; 20074 } 20075 20076 case DKIOCABORT: 20077 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 20078 if (scsi_abort(SD_ADDRESS(un), NULL)) { 20079 err = 0; 20080 } else { 20081 err = EIO; 20082 } 20083 break; 20084 #endif 20085 20086 #ifdef SD_FAULT_INJECTION 20087 /* SDIOC FaultInjection testing ioctls */ 20088 case SDIOCSTART: 20089 case SDIOCSTOP: 20090 case SDIOCINSERTPKT: 20091 case SDIOCINSERTXB: 20092 case SDIOCINSERTUN: 20093 case SDIOCINSERTARQ: 20094 case SDIOCPUSH: 20095 case SDIOCRETRIEVE: 20096 case SDIOCRUN: 20097 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 20098 "SDIOC detected cmd:0x%X:\n", cmd); 20099 /* call error generator */ 20100 sd_faultinjection_ioctl(cmd, arg, un); 20101 err = 0; 20102 break; 20103 20104 #endif /* SD_FAULT_INJECTION */ 20105 20106 case DKIOCFLUSHWRITECACHE: 20107 { 20108 struct dk_callback *dkc = (struct dk_callback *)arg; 20109 20110 mutex_enter(SD_MUTEX(un)); 20111 if (!un->un_f_sync_cache_supported || 20112 !un->un_f_write_cache_enabled) { 20113 err = un->un_f_sync_cache_supported ? 20114 0 : ENOTSUP; 20115 mutex_exit(SD_MUTEX(un)); 20116 if ((flag & FKIOCTL) && dkc != NULL && 20117 dkc->dkc_callback != NULL) { 20118 (*dkc->dkc_callback)(dkc->dkc_cookie, 20119 err); 20120 /* 20121 * Did callback and reported error. 20122 * Since we did a callback, ioctl 20123 * should return 0. 20124 */ 20125 err = 0; 20126 } 20127 break; 20128 } 20129 mutex_exit(SD_MUTEX(un)); 20130 20131 if ((flag & FKIOCTL) && dkc != NULL && 20132 dkc->dkc_callback != NULL) { 20133 /* async SYNC CACHE request */ 20134 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 20135 } else { 20136 /* synchronous SYNC CACHE request */ 20137 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 20138 } 20139 } 20140 break; 20141 20142 case DKIOCGETWCE: { 20143 20144 int wce; 20145 20146 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 20147 break; 20148 } 20149 20150 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 20151 err = EFAULT; 20152 } 20153 break; 20154 } 20155 20156 case DKIOCSETWCE: { 20157 20158 int wce, sync_supported; 20159 20160 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 20161 err = EFAULT; 20162 break; 20163 } 20164 20165 /* 20166 * Synchronize multiple threads trying to enable 20167 * or disable the cache via the un_f_wcc_cv 20168 * condition variable. 20169 */ 20170 mutex_enter(SD_MUTEX(un)); 20171 20172 /* 20173 * Don't allow the cache to be enabled if the 20174 * config file has it disabled. 20175 */ 20176 if (un->un_f_opt_disable_cache && wce) { 20177 mutex_exit(SD_MUTEX(un)); 20178 err = EINVAL; 20179 break; 20180 } 20181 20182 /* 20183 * Wait for write cache change in progress 20184 * bit to be clear before proceeding. 20185 */ 20186 while (un->un_f_wcc_inprog) 20187 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 20188 20189 un->un_f_wcc_inprog = 1; 20190 20191 if (un->un_f_write_cache_enabled && wce == 0) { 20192 /* 20193 * Disable the write cache. Don't clear 20194 * un_f_write_cache_enabled until after 20195 * the mode select and flush are complete. 20196 */ 20197 sync_supported = un->un_f_sync_cache_supported; 20198 mutex_exit(SD_MUTEX(un)); 20199 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 20200 SD_CACHE_DISABLE)) == 0 && sync_supported) { 20201 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 20202 } 20203 20204 mutex_enter(SD_MUTEX(un)); 20205 if (err == 0) { 20206 un->un_f_write_cache_enabled = 0; 20207 } 20208 20209 } else if (!un->un_f_write_cache_enabled && wce != 0) { 20210 /* 20211 * Set un_f_write_cache_enabled first, so there is 20212 * no window where the cache is enabled, but the 20213 * bit says it isn't. 20214 */ 20215 un->un_f_write_cache_enabled = 1; 20216 mutex_exit(SD_MUTEX(un)); 20217 20218 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 20219 SD_CACHE_ENABLE); 20220 20221 mutex_enter(SD_MUTEX(un)); 20222 20223 if (err) { 20224 un->un_f_write_cache_enabled = 0; 20225 } 20226 } 20227 20228 un->un_f_wcc_inprog = 0; 20229 cv_broadcast(&un->un_wcc_cv); 20230 mutex_exit(SD_MUTEX(un)); 20231 break; 20232 } 20233 20234 default: 20235 err = ENOTTY; 20236 break; 20237 } 20238 mutex_enter(SD_MUTEX(un)); 20239 un->un_ncmds_in_driver--; 20240 ASSERT(un->un_ncmds_in_driver >= 0); 20241 mutex_exit(SD_MUTEX(un)); 20242 20243 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 20244 return (err); 20245 } 20246 20247 20248 /* 20249 * Function: sd_dkio_ctrl_info 20250 * 20251 * Description: This routine is the driver entry point for handling controller 20252 * information ioctl requests (DKIOCINFO). 20253 * 20254 * Arguments: dev - the device number 20255 * arg - pointer to user provided dk_cinfo structure 20256 * specifying the controller type and attributes. 20257 * flag - this argument is a pass through to ddi_copyxxx() 20258 * directly from the mode argument of ioctl(). 20259 * 20260 * Return Code: 0 20261 * EFAULT 20262 * ENXIO 20263 */ 20264 20265 static int 20266 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 20267 { 20268 struct sd_lun *un = NULL; 20269 struct dk_cinfo *info; 20270 dev_info_t *pdip; 20271 int lun, tgt; 20272 20273 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20274 return (ENXIO); 20275 } 20276 20277 info = (struct dk_cinfo *) 20278 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 20279 20280 switch (un->un_ctype) { 20281 case CTYPE_CDROM: 20282 info->dki_ctype = DKC_CDROM; 20283 break; 20284 default: 20285 info->dki_ctype = DKC_SCSI_CCS; 20286 break; 20287 } 20288 pdip = ddi_get_parent(SD_DEVINFO(un)); 20289 info->dki_cnum = ddi_get_instance(pdip); 20290 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 20291 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 20292 } else { 20293 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 20294 DK_DEVLEN - 1); 20295 } 20296 20297 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20298 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 20299 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20300 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 20301 20302 /* Unit Information */ 20303 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 20304 info->dki_slave = ((tgt << 3) | lun); 20305 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 20306 DK_DEVLEN - 1); 20307 info->dki_flags = DKI_FMTVOL; 20308 info->dki_partition = SDPART(dev); 20309 20310 /* Max Transfer size of this device in blocks */ 20311 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 20312 info->dki_addr = 0; 20313 info->dki_space = 0; 20314 info->dki_prio = 0; 20315 info->dki_vec = 0; 20316 20317 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 20318 kmem_free(info, sizeof (struct dk_cinfo)); 20319 return (EFAULT); 20320 } else { 20321 kmem_free(info, sizeof (struct dk_cinfo)); 20322 return (0); 20323 } 20324 } 20325 20326 20327 /* 20328 * Function: sd_get_media_info 20329 * 20330 * Description: This routine is the driver entry point for handling ioctl 20331 * requests for the media type or command set profile used by the 20332 * drive to operate on the media (DKIOCGMEDIAINFO). 20333 * 20334 * Arguments: dev - the device number 20335 * arg - pointer to user provided dk_minfo structure 20336 * specifying the media type, logical block size and 20337 * drive capacity. 20338 * flag - this argument is a pass through to ddi_copyxxx() 20339 * directly from the mode argument of ioctl(). 20340 * 20341 * Return Code: 0 20342 * EACCESS 20343 * EFAULT 20344 * ENXIO 20345 * EIO 20346 */ 20347 20348 static int 20349 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 20350 { 20351 struct sd_lun *un = NULL; 20352 struct uscsi_cmd com; 20353 struct scsi_inquiry *sinq; 20354 struct dk_minfo media_info; 20355 u_longlong_t media_capacity; 20356 uint64_t capacity; 20357 uint_t lbasize; 20358 uchar_t *out_data; 20359 uchar_t *rqbuf; 20360 int rval = 0; 20361 int rtn; 20362 20363 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 20364 (un->un_state == SD_STATE_OFFLINE)) { 20365 return (ENXIO); 20366 } 20367 20368 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 20369 20370 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 20371 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20372 20373 /* Issue a TUR to determine if the drive is ready with media present */ 20374 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 20375 if (rval == ENXIO) { 20376 goto done; 20377 } 20378 20379 /* Now get configuration data */ 20380 if (ISCD(un)) { 20381 media_info.dki_media_type = DK_CDROM; 20382 20383 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 20384 if (un->un_f_mmc_cap == TRUE) { 20385 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 20386 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 20387 SD_PATH_STANDARD); 20388 20389 if (rtn) { 20390 /* 20391 * Failed for other than an illegal request 20392 * or command not supported 20393 */ 20394 if ((com.uscsi_status == STATUS_CHECK) && 20395 (com.uscsi_rqstatus == STATUS_GOOD)) { 20396 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 20397 (rqbuf[12] != 0x20)) { 20398 rval = EIO; 20399 goto done; 20400 } 20401 } 20402 } else { 20403 /* 20404 * The GET CONFIGURATION command succeeded 20405 * so set the media type according to the 20406 * returned data 20407 */ 20408 media_info.dki_media_type = out_data[6]; 20409 media_info.dki_media_type <<= 8; 20410 media_info.dki_media_type |= out_data[7]; 20411 } 20412 } 20413 } else { 20414 /* 20415 * The profile list is not available, so we attempt to identify 20416 * the media type based on the inquiry data 20417 */ 20418 sinq = un->un_sd->sd_inq; 20419 if ((sinq->inq_dtype == DTYPE_DIRECT) || 20420 (sinq->inq_dtype == DTYPE_OPTICAL)) { 20421 /* This is a direct access device or optical disk */ 20422 media_info.dki_media_type = DK_FIXED_DISK; 20423 20424 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 20425 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 20426 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 20427 media_info.dki_media_type = DK_ZIP; 20428 } else if ( 20429 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 20430 media_info.dki_media_type = DK_JAZ; 20431 } 20432 } 20433 } else { 20434 /* 20435 * Not a CD, direct access or optical disk so return 20436 * unknown media 20437 */ 20438 media_info.dki_media_type = DK_UNKNOWN; 20439 } 20440 } 20441 20442 /* Now read the capacity so we can provide the lbasize and capacity */ 20443 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 20444 SD_PATH_DIRECT)) { 20445 case 0: 20446 break; 20447 case EACCES: 20448 rval = EACCES; 20449 goto done; 20450 default: 20451 rval = EIO; 20452 goto done; 20453 } 20454 20455 media_info.dki_lbsize = lbasize; 20456 media_capacity = capacity; 20457 20458 /* 20459 * sd_send_scsi_READ_CAPACITY() reports capacity in 20460 * un->un_sys_blocksize chunks. So we need to convert it into 20461 * cap.lbasize chunks. 20462 */ 20463 media_capacity *= un->un_sys_blocksize; 20464 media_capacity /= lbasize; 20465 media_info.dki_capacity = media_capacity; 20466 20467 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 20468 rval = EFAULT; 20469 /* Put goto. Anybody might add some code below in future */ 20470 goto done; 20471 } 20472 done: 20473 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 20474 kmem_free(rqbuf, SENSE_LENGTH); 20475 return (rval); 20476 } 20477 20478 20479 /* 20480 * Function: sd_check_media 20481 * 20482 * Description: This utility routine implements the functionality for the 20483 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 20484 * driver state changes from that specified by the user 20485 * (inserted or ejected). For example, if the user specifies 20486 * DKIO_EJECTED and the current media state is inserted this 20487 * routine will immediately return DKIO_INSERTED. However, if the 20488 * current media state is not inserted the user thread will be 20489 * blocked until the drive state changes. If DKIO_NONE is specified 20490 * the user thread will block until a drive state change occurs. 20491 * 20492 * Arguments: dev - the device number 20493 * state - user pointer to a dkio_state, updated with the current 20494 * drive state at return. 20495 * 20496 * Return Code: ENXIO 20497 * EIO 20498 * EAGAIN 20499 * EINTR 20500 */ 20501 20502 static int 20503 sd_check_media(dev_t dev, enum dkio_state state) 20504 { 20505 struct sd_lun *un = NULL; 20506 enum dkio_state prev_state; 20507 opaque_t token = NULL; 20508 int rval = 0; 20509 20510 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20511 return (ENXIO); 20512 } 20513 20514 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 20515 20516 mutex_enter(SD_MUTEX(un)); 20517 20518 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 20519 "state=%x, mediastate=%x\n", state, un->un_mediastate); 20520 20521 prev_state = un->un_mediastate; 20522 20523 /* is there anything to do? */ 20524 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 20525 /* 20526 * submit the request to the scsi_watch service; 20527 * scsi_media_watch_cb() does the real work 20528 */ 20529 mutex_exit(SD_MUTEX(un)); 20530 20531 /* 20532 * This change handles the case where a scsi watch request is 20533 * added to a device that is powered down. To accomplish this 20534 * we power up the device before adding the scsi watch request, 20535 * since the scsi watch sends a TUR directly to the device 20536 * which the device cannot handle if it is powered down. 20537 */ 20538 if (sd_pm_entry(un) != DDI_SUCCESS) { 20539 mutex_enter(SD_MUTEX(un)); 20540 goto done; 20541 } 20542 20543 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 20544 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 20545 (caddr_t)dev); 20546 20547 sd_pm_exit(un); 20548 20549 mutex_enter(SD_MUTEX(un)); 20550 if (token == NULL) { 20551 rval = EAGAIN; 20552 goto done; 20553 } 20554 20555 /* 20556 * This is a special case IOCTL that doesn't return 20557 * until the media state changes. Routine sdpower 20558 * knows about and handles this so don't count it 20559 * as an active cmd in the driver, which would 20560 * keep the device busy to the pm framework. 20561 * If the count isn't decremented the device can't 20562 * be powered down. 20563 */ 20564 un->un_ncmds_in_driver--; 20565 ASSERT(un->un_ncmds_in_driver >= 0); 20566 20567 /* 20568 * if a prior request had been made, this will be the same 20569 * token, as scsi_watch was designed that way. 20570 */ 20571 un->un_swr_token = token; 20572 un->un_specified_mediastate = state; 20573 20574 /* 20575 * now wait for media change 20576 * we will not be signalled unless mediastate == state but it is 20577 * still better to test for this condition, since there is a 20578 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 20579 */ 20580 SD_TRACE(SD_LOG_COMMON, un, 20581 "sd_check_media: waiting for media state change\n"); 20582 while (un->un_mediastate == state) { 20583 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 20584 SD_TRACE(SD_LOG_COMMON, un, 20585 "sd_check_media: waiting for media state " 20586 "was interrupted\n"); 20587 un->un_ncmds_in_driver++; 20588 rval = EINTR; 20589 goto done; 20590 } 20591 SD_TRACE(SD_LOG_COMMON, un, 20592 "sd_check_media: received signal, state=%x\n", 20593 un->un_mediastate); 20594 } 20595 /* 20596 * Inc the counter to indicate the device once again 20597 * has an active outstanding cmd. 20598 */ 20599 un->un_ncmds_in_driver++; 20600 } 20601 20602 /* invalidate geometry */ 20603 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 20604 sr_ejected(un); 20605 } 20606 20607 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 20608 uint64_t capacity; 20609 uint_t lbasize; 20610 20611 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 20612 mutex_exit(SD_MUTEX(un)); 20613 /* 20614 * Since the following routines use SD_PATH_DIRECT, we must 20615 * call PM directly before the upcoming disk accesses. This 20616 * may cause the disk to be power/spin up. 20617 */ 20618 20619 if (sd_pm_entry(un) == DDI_SUCCESS) { 20620 rval = sd_send_scsi_READ_CAPACITY(un, 20621 &capacity, 20622 &lbasize, SD_PATH_DIRECT); 20623 if (rval != 0) { 20624 sd_pm_exit(un); 20625 mutex_enter(SD_MUTEX(un)); 20626 goto done; 20627 } 20628 } else { 20629 rval = EIO; 20630 mutex_enter(SD_MUTEX(un)); 20631 goto done; 20632 } 20633 mutex_enter(SD_MUTEX(un)); 20634 20635 sd_update_block_info(un, lbasize, capacity); 20636 20637 /* 20638 * Check if the media in the device is writable or not 20639 */ 20640 if (ISCD(un)) 20641 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 20642 20643 mutex_exit(SD_MUTEX(un)); 20644 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 20645 if ((cmlb_validate(un->un_cmlbhandle, 0, 20646 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 20647 sd_set_pstats(un); 20648 SD_TRACE(SD_LOG_IO_PARTITION, un, 20649 "sd_check_media: un:0x%p pstats created and " 20650 "set\n", un); 20651 } 20652 20653 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 20654 SD_PATH_DIRECT); 20655 sd_pm_exit(un); 20656 20657 mutex_enter(SD_MUTEX(un)); 20658 } 20659 done: 20660 un->un_f_watcht_stopped = FALSE; 20661 if (un->un_swr_token) { 20662 /* 20663 * Use of this local token and the mutex ensures that we avoid 20664 * some race conditions associated with terminating the 20665 * scsi watch. 20666 */ 20667 token = un->un_swr_token; 20668 un->un_swr_token = (opaque_t)NULL; 20669 mutex_exit(SD_MUTEX(un)); 20670 (void) scsi_watch_request_terminate(token, 20671 SCSI_WATCH_TERMINATE_WAIT); 20672 mutex_enter(SD_MUTEX(un)); 20673 } 20674 20675 /* 20676 * Update the capacity kstat value, if no media previously 20677 * (capacity kstat is 0) and a media has been inserted 20678 * (un_f_blockcount_is_valid == TRUE) 20679 */ 20680 if (un->un_errstats) { 20681 struct sd_errstats *stp = NULL; 20682 20683 stp = (struct sd_errstats *)un->un_errstats->ks_data; 20684 if ((stp->sd_capacity.value.ui64 == 0) && 20685 (un->un_f_blockcount_is_valid == TRUE)) { 20686 stp->sd_capacity.value.ui64 = 20687 (uint64_t)((uint64_t)un->un_blockcount * 20688 un->un_sys_blocksize); 20689 } 20690 } 20691 mutex_exit(SD_MUTEX(un)); 20692 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 20693 return (rval); 20694 } 20695 20696 20697 /* 20698 * Function: sd_delayed_cv_broadcast 20699 * 20700 * Description: Delayed cv_broadcast to allow for target to recover from media 20701 * insertion. 20702 * 20703 * Arguments: arg - driver soft state (unit) structure 20704 */ 20705 20706 static void 20707 sd_delayed_cv_broadcast(void *arg) 20708 { 20709 struct sd_lun *un = arg; 20710 20711 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 20712 20713 mutex_enter(SD_MUTEX(un)); 20714 un->un_dcvb_timeid = NULL; 20715 cv_broadcast(&un->un_state_cv); 20716 mutex_exit(SD_MUTEX(un)); 20717 } 20718 20719 20720 /* 20721 * Function: sd_media_watch_cb 20722 * 20723 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 20724 * routine processes the TUR sense data and updates the driver 20725 * state if a transition has occurred. The user thread 20726 * (sd_check_media) is then signalled. 20727 * 20728 * Arguments: arg - the device 'dev_t' is used for context to discriminate 20729 * among multiple watches that share this callback function 20730 * resultp - scsi watch facility result packet containing scsi 20731 * packet, status byte and sense data 20732 * 20733 * Return Code: 0 for success, -1 for failure 20734 */ 20735 20736 static int 20737 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 20738 { 20739 struct sd_lun *un; 20740 struct scsi_status *statusp = resultp->statusp; 20741 uint8_t *sensep = (uint8_t *)resultp->sensep; 20742 enum dkio_state state = DKIO_NONE; 20743 dev_t dev = (dev_t)arg; 20744 uchar_t actual_sense_length; 20745 uint8_t skey, asc, ascq; 20746 20747 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20748 return (-1); 20749 } 20750 actual_sense_length = resultp->actual_sense_length; 20751 20752 mutex_enter(SD_MUTEX(un)); 20753 SD_TRACE(SD_LOG_COMMON, un, 20754 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 20755 *((char *)statusp), (void *)sensep, actual_sense_length); 20756 20757 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 20758 un->un_mediastate = DKIO_DEV_GONE; 20759 cv_broadcast(&un->un_state_cv); 20760 mutex_exit(SD_MUTEX(un)); 20761 20762 return (0); 20763 } 20764 20765 /* 20766 * If there was a check condition then sensep points to valid sense data 20767 * If status was not a check condition but a reservation or busy status 20768 * then the new state is DKIO_NONE 20769 */ 20770 if (sensep != NULL) { 20771 skey = scsi_sense_key(sensep); 20772 asc = scsi_sense_asc(sensep); 20773 ascq = scsi_sense_ascq(sensep); 20774 20775 SD_INFO(SD_LOG_COMMON, un, 20776 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 20777 skey, asc, ascq); 20778 /* This routine only uses up to 13 bytes of sense data. */ 20779 if (actual_sense_length >= 13) { 20780 if (skey == KEY_UNIT_ATTENTION) { 20781 if (asc == 0x28) { 20782 state = DKIO_INSERTED; 20783 } 20784 } else if (skey == KEY_NOT_READY) { 20785 /* 20786 * if 02/04/02 means that the host 20787 * should send start command. Explicitly 20788 * leave the media state as is 20789 * (inserted) as the media is inserted 20790 * and host has stopped device for PM 20791 * reasons. Upon next true read/write 20792 * to this media will bring the 20793 * device to the right state good for 20794 * media access. 20795 */ 20796 if (asc == 0x3a) { 20797 state = DKIO_EJECTED; 20798 } else { 20799 /* 20800 * If the drive is busy with an 20801 * operation or long write, keep the 20802 * media in an inserted state. 20803 */ 20804 20805 if ((asc == 0x04) && 20806 ((ascq == 0x02) || 20807 (ascq == 0x07) || 20808 (ascq == 0x08))) { 20809 state = DKIO_INSERTED; 20810 } 20811 } 20812 } else if (skey == KEY_NO_SENSE) { 20813 if ((asc == 0x00) && (ascq == 0x00)) { 20814 /* 20815 * Sense Data 00/00/00 does not provide 20816 * any information about the state of 20817 * the media. Ignore it. 20818 */ 20819 mutex_exit(SD_MUTEX(un)); 20820 return (0); 20821 } 20822 } 20823 } 20824 } else if ((*((char *)statusp) == STATUS_GOOD) && 20825 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 20826 state = DKIO_INSERTED; 20827 } 20828 20829 SD_TRACE(SD_LOG_COMMON, un, 20830 "sd_media_watch_cb: state=%x, specified=%x\n", 20831 state, un->un_specified_mediastate); 20832 20833 /* 20834 * now signal the waiting thread if this is *not* the specified state; 20835 * delay the signal if the state is DKIO_INSERTED to allow the target 20836 * to recover 20837 */ 20838 if (state != un->un_specified_mediastate) { 20839 un->un_mediastate = state; 20840 if (state == DKIO_INSERTED) { 20841 /* 20842 * delay the signal to give the drive a chance 20843 * to do what it apparently needs to do 20844 */ 20845 SD_TRACE(SD_LOG_COMMON, un, 20846 "sd_media_watch_cb: delayed cv_broadcast\n"); 20847 if (un->un_dcvb_timeid == NULL) { 20848 un->un_dcvb_timeid = 20849 timeout(sd_delayed_cv_broadcast, un, 20850 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 20851 } 20852 } else { 20853 SD_TRACE(SD_LOG_COMMON, un, 20854 "sd_media_watch_cb: immediate cv_broadcast\n"); 20855 cv_broadcast(&un->un_state_cv); 20856 } 20857 } 20858 mutex_exit(SD_MUTEX(un)); 20859 return (0); 20860 } 20861 20862 20863 /* 20864 * Function: sd_dkio_get_temp 20865 * 20866 * Description: This routine is the driver entry point for handling ioctl 20867 * requests to get the disk temperature. 20868 * 20869 * Arguments: dev - the device number 20870 * arg - pointer to user provided dk_temperature structure. 20871 * flag - this argument is a pass through to ddi_copyxxx() 20872 * directly from the mode argument of ioctl(). 20873 * 20874 * Return Code: 0 20875 * EFAULT 20876 * ENXIO 20877 * EAGAIN 20878 */ 20879 20880 static int 20881 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 20882 { 20883 struct sd_lun *un = NULL; 20884 struct dk_temperature *dktemp = NULL; 20885 uchar_t *temperature_page; 20886 int rval = 0; 20887 int path_flag = SD_PATH_STANDARD; 20888 20889 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20890 return (ENXIO); 20891 } 20892 20893 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 20894 20895 /* copyin the disk temp argument to get the user flags */ 20896 if (ddi_copyin((void *)arg, dktemp, 20897 sizeof (struct dk_temperature), flag) != 0) { 20898 rval = EFAULT; 20899 goto done; 20900 } 20901 20902 /* Initialize the temperature to invalid. */ 20903 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 20904 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 20905 20906 /* 20907 * Note: Investigate removing the "bypass pm" semantic. 20908 * Can we just bypass PM always? 20909 */ 20910 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 20911 path_flag = SD_PATH_DIRECT; 20912 ASSERT(!mutex_owned(&un->un_pm_mutex)); 20913 mutex_enter(&un->un_pm_mutex); 20914 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 20915 /* 20916 * If DKT_BYPASS_PM is set, and the drive happens to be 20917 * in low power mode, we can not wake it up, Need to 20918 * return EAGAIN. 20919 */ 20920 mutex_exit(&un->un_pm_mutex); 20921 rval = EAGAIN; 20922 goto done; 20923 } else { 20924 /* 20925 * Indicate to PM the device is busy. This is required 20926 * to avoid a race - i.e. the ioctl is issuing a 20927 * command and the pm framework brings down the device 20928 * to low power mode (possible power cut-off on some 20929 * platforms). 20930 */ 20931 mutex_exit(&un->un_pm_mutex); 20932 if (sd_pm_entry(un) != DDI_SUCCESS) { 20933 rval = EAGAIN; 20934 goto done; 20935 } 20936 } 20937 } 20938 20939 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 20940 20941 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 20942 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 20943 goto done2; 20944 } 20945 20946 /* 20947 * For the current temperature verify that the parameter length is 0x02 20948 * and the parameter code is 0x00 20949 */ 20950 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 20951 (temperature_page[5] == 0x00)) { 20952 if (temperature_page[9] == 0xFF) { 20953 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 20954 } else { 20955 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 20956 } 20957 } 20958 20959 /* 20960 * For the reference temperature verify that the parameter 20961 * length is 0x02 and the parameter code is 0x01 20962 */ 20963 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 20964 (temperature_page[11] == 0x01)) { 20965 if (temperature_page[15] == 0xFF) { 20966 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 20967 } else { 20968 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 20969 } 20970 } 20971 20972 /* Do the copyout regardless of the temperature commands status. */ 20973 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 20974 flag) != 0) { 20975 rval = EFAULT; 20976 } 20977 20978 done2: 20979 if (path_flag == SD_PATH_DIRECT) { 20980 sd_pm_exit(un); 20981 } 20982 20983 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 20984 done: 20985 if (dktemp != NULL) { 20986 kmem_free(dktemp, sizeof (struct dk_temperature)); 20987 } 20988 20989 return (rval); 20990 } 20991 20992 20993 /* 20994 * Function: sd_log_page_supported 20995 * 20996 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 20997 * supported log pages. 20998 * 20999 * Arguments: un - 21000 * log_page - 21001 * 21002 * Return Code: -1 - on error (log sense is optional and may not be supported). 21003 * 0 - log page not found. 21004 * 1 - log page found. 21005 */ 21006 21007 static int 21008 sd_log_page_supported(struct sd_lun *un, int log_page) 21009 { 21010 uchar_t *log_page_data; 21011 int i; 21012 int match = 0; 21013 int log_size; 21014 21015 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 21016 21017 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 21018 SD_PATH_DIRECT) != 0) { 21019 SD_ERROR(SD_LOG_COMMON, un, 21020 "sd_log_page_supported: failed log page retrieval\n"); 21021 kmem_free(log_page_data, 0xFF); 21022 return (-1); 21023 } 21024 log_size = log_page_data[3]; 21025 21026 /* 21027 * The list of supported log pages start from the fourth byte. Check 21028 * until we run out of log pages or a match is found. 21029 */ 21030 for (i = 4; (i < (log_size + 4)) && !match; i++) { 21031 if (log_page_data[i] == log_page) { 21032 match++; 21033 } 21034 } 21035 kmem_free(log_page_data, 0xFF); 21036 return (match); 21037 } 21038 21039 21040 /* 21041 * Function: sd_mhdioc_failfast 21042 * 21043 * Description: This routine is the driver entry point for handling ioctl 21044 * requests to enable/disable the multihost failfast option. 21045 * (MHIOCENFAILFAST) 21046 * 21047 * Arguments: dev - the device number 21048 * arg - user specified probing interval. 21049 * flag - this argument is a pass through to ddi_copyxxx() 21050 * directly from the mode argument of ioctl(). 21051 * 21052 * Return Code: 0 21053 * EFAULT 21054 * ENXIO 21055 */ 21056 21057 static int 21058 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 21059 { 21060 struct sd_lun *un = NULL; 21061 int mh_time; 21062 int rval = 0; 21063 21064 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21065 return (ENXIO); 21066 } 21067 21068 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 21069 return (EFAULT); 21070 21071 if (mh_time) { 21072 mutex_enter(SD_MUTEX(un)); 21073 un->un_resvd_status |= SD_FAILFAST; 21074 mutex_exit(SD_MUTEX(un)); 21075 /* 21076 * If mh_time is INT_MAX, then this ioctl is being used for 21077 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 21078 */ 21079 if (mh_time != INT_MAX) { 21080 rval = sd_check_mhd(dev, mh_time); 21081 } 21082 } else { 21083 (void) sd_check_mhd(dev, 0); 21084 mutex_enter(SD_MUTEX(un)); 21085 un->un_resvd_status &= ~SD_FAILFAST; 21086 mutex_exit(SD_MUTEX(un)); 21087 } 21088 return (rval); 21089 } 21090 21091 21092 /* 21093 * Function: sd_mhdioc_takeown 21094 * 21095 * Description: This routine is the driver entry point for handling ioctl 21096 * requests to forcefully acquire exclusive access rights to the 21097 * multihost disk (MHIOCTKOWN). 21098 * 21099 * Arguments: dev - the device number 21100 * arg - user provided structure specifying the delay 21101 * parameters in milliseconds 21102 * flag - this argument is a pass through to ddi_copyxxx() 21103 * directly from the mode argument of ioctl(). 21104 * 21105 * Return Code: 0 21106 * EFAULT 21107 * ENXIO 21108 */ 21109 21110 static int 21111 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 21112 { 21113 struct sd_lun *un = NULL; 21114 struct mhioctkown *tkown = NULL; 21115 int rval = 0; 21116 21117 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21118 return (ENXIO); 21119 } 21120 21121 if (arg != NULL) { 21122 tkown = (struct mhioctkown *) 21123 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 21124 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 21125 if (rval != 0) { 21126 rval = EFAULT; 21127 goto error; 21128 } 21129 } 21130 21131 rval = sd_take_ownership(dev, tkown); 21132 mutex_enter(SD_MUTEX(un)); 21133 if (rval == 0) { 21134 un->un_resvd_status |= SD_RESERVE; 21135 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 21136 sd_reinstate_resv_delay = 21137 tkown->reinstate_resv_delay * 1000; 21138 } else { 21139 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 21140 } 21141 /* 21142 * Give the scsi_watch routine interval set by 21143 * the MHIOCENFAILFAST ioctl precedence here. 21144 */ 21145 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 21146 mutex_exit(SD_MUTEX(un)); 21147 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 21148 SD_TRACE(SD_LOG_IOCTL_MHD, un, 21149 "sd_mhdioc_takeown : %d\n", 21150 sd_reinstate_resv_delay); 21151 } else { 21152 mutex_exit(SD_MUTEX(un)); 21153 } 21154 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 21155 sd_mhd_reset_notify_cb, (caddr_t)un); 21156 } else { 21157 un->un_resvd_status &= ~SD_RESERVE; 21158 mutex_exit(SD_MUTEX(un)); 21159 } 21160 21161 error: 21162 if (tkown != NULL) { 21163 kmem_free(tkown, sizeof (struct mhioctkown)); 21164 } 21165 return (rval); 21166 } 21167 21168 21169 /* 21170 * Function: sd_mhdioc_release 21171 * 21172 * Description: This routine is the driver entry point for handling ioctl 21173 * requests to release exclusive access rights to the multihost 21174 * disk (MHIOCRELEASE). 21175 * 21176 * Arguments: dev - the device number 21177 * 21178 * Return Code: 0 21179 * ENXIO 21180 */ 21181 21182 static int 21183 sd_mhdioc_release(dev_t dev) 21184 { 21185 struct sd_lun *un = NULL; 21186 timeout_id_t resvd_timeid_save; 21187 int resvd_status_save; 21188 int rval = 0; 21189 21190 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21191 return (ENXIO); 21192 } 21193 21194 mutex_enter(SD_MUTEX(un)); 21195 resvd_status_save = un->un_resvd_status; 21196 un->un_resvd_status &= 21197 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 21198 if (un->un_resvd_timeid) { 21199 resvd_timeid_save = un->un_resvd_timeid; 21200 un->un_resvd_timeid = NULL; 21201 mutex_exit(SD_MUTEX(un)); 21202 (void) untimeout(resvd_timeid_save); 21203 } else { 21204 mutex_exit(SD_MUTEX(un)); 21205 } 21206 21207 /* 21208 * destroy any pending timeout thread that may be attempting to 21209 * reinstate reservation on this device. 21210 */ 21211 sd_rmv_resv_reclaim_req(dev); 21212 21213 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 21214 mutex_enter(SD_MUTEX(un)); 21215 if ((un->un_mhd_token) && 21216 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 21217 mutex_exit(SD_MUTEX(un)); 21218 (void) sd_check_mhd(dev, 0); 21219 } else { 21220 mutex_exit(SD_MUTEX(un)); 21221 } 21222 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 21223 sd_mhd_reset_notify_cb, (caddr_t)un); 21224 } else { 21225 /* 21226 * sd_mhd_watch_cb will restart the resvd recover timeout thread 21227 */ 21228 mutex_enter(SD_MUTEX(un)); 21229 un->un_resvd_status = resvd_status_save; 21230 mutex_exit(SD_MUTEX(un)); 21231 } 21232 return (rval); 21233 } 21234 21235 21236 /* 21237 * Function: sd_mhdioc_register_devid 21238 * 21239 * Description: This routine is the driver entry point for handling ioctl 21240 * requests to register the device id (MHIOCREREGISTERDEVID). 21241 * 21242 * Note: The implementation for this ioctl has been updated to 21243 * be consistent with the original PSARC case (1999/357) 21244 * (4375899, 4241671, 4220005) 21245 * 21246 * Arguments: dev - the device number 21247 * 21248 * Return Code: 0 21249 * ENXIO 21250 */ 21251 21252 static int 21253 sd_mhdioc_register_devid(dev_t dev) 21254 { 21255 struct sd_lun *un = NULL; 21256 int rval = 0; 21257 21258 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21259 return (ENXIO); 21260 } 21261 21262 ASSERT(!mutex_owned(SD_MUTEX(un))); 21263 21264 mutex_enter(SD_MUTEX(un)); 21265 21266 /* If a devid already exists, de-register it */ 21267 if (un->un_devid != NULL) { 21268 ddi_devid_unregister(SD_DEVINFO(un)); 21269 /* 21270 * After unregister devid, needs to free devid memory 21271 */ 21272 ddi_devid_free(un->un_devid); 21273 un->un_devid = NULL; 21274 } 21275 21276 /* Check for reservation conflict */ 21277 mutex_exit(SD_MUTEX(un)); 21278 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 21279 mutex_enter(SD_MUTEX(un)); 21280 21281 switch (rval) { 21282 case 0: 21283 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 21284 break; 21285 case EACCES: 21286 break; 21287 default: 21288 rval = EIO; 21289 } 21290 21291 mutex_exit(SD_MUTEX(un)); 21292 return (rval); 21293 } 21294 21295 21296 /* 21297 * Function: sd_mhdioc_inkeys 21298 * 21299 * Description: This routine is the driver entry point for handling ioctl 21300 * requests to issue the SCSI-3 Persistent In Read Keys command 21301 * to the device (MHIOCGRP_INKEYS). 21302 * 21303 * Arguments: dev - the device number 21304 * arg - user provided in_keys structure 21305 * flag - this argument is a pass through to ddi_copyxxx() 21306 * directly from the mode argument of ioctl(). 21307 * 21308 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 21309 * ENXIO 21310 * EFAULT 21311 */ 21312 21313 static int 21314 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 21315 { 21316 struct sd_lun *un; 21317 mhioc_inkeys_t inkeys; 21318 int rval = 0; 21319 21320 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21321 return (ENXIO); 21322 } 21323 21324 #ifdef _MULTI_DATAMODEL 21325 switch (ddi_model_convert_from(flag & FMODELS)) { 21326 case DDI_MODEL_ILP32: { 21327 struct mhioc_inkeys32 inkeys32; 21328 21329 if (ddi_copyin(arg, &inkeys32, 21330 sizeof (struct mhioc_inkeys32), flag) != 0) { 21331 return (EFAULT); 21332 } 21333 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 21334 if ((rval = sd_persistent_reservation_in_read_keys(un, 21335 &inkeys, flag)) != 0) { 21336 return (rval); 21337 } 21338 inkeys32.generation = inkeys.generation; 21339 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 21340 flag) != 0) { 21341 return (EFAULT); 21342 } 21343 break; 21344 } 21345 case DDI_MODEL_NONE: 21346 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 21347 flag) != 0) { 21348 return (EFAULT); 21349 } 21350 if ((rval = sd_persistent_reservation_in_read_keys(un, 21351 &inkeys, flag)) != 0) { 21352 return (rval); 21353 } 21354 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 21355 flag) != 0) { 21356 return (EFAULT); 21357 } 21358 break; 21359 } 21360 21361 #else /* ! _MULTI_DATAMODEL */ 21362 21363 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 21364 return (EFAULT); 21365 } 21366 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 21367 if (rval != 0) { 21368 return (rval); 21369 } 21370 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 21371 return (EFAULT); 21372 } 21373 21374 #endif /* _MULTI_DATAMODEL */ 21375 21376 return (rval); 21377 } 21378 21379 21380 /* 21381 * Function: sd_mhdioc_inresv 21382 * 21383 * Description: This routine is the driver entry point for handling ioctl 21384 * requests to issue the SCSI-3 Persistent In Read Reservations 21385 * command to the device (MHIOCGRP_INKEYS). 21386 * 21387 * Arguments: dev - the device number 21388 * arg - user provided in_resv structure 21389 * flag - this argument is a pass through to ddi_copyxxx() 21390 * directly from the mode argument of ioctl(). 21391 * 21392 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 21393 * ENXIO 21394 * EFAULT 21395 */ 21396 21397 static int 21398 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 21399 { 21400 struct sd_lun *un; 21401 mhioc_inresvs_t inresvs; 21402 int rval = 0; 21403 21404 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21405 return (ENXIO); 21406 } 21407 21408 #ifdef _MULTI_DATAMODEL 21409 21410 switch (ddi_model_convert_from(flag & FMODELS)) { 21411 case DDI_MODEL_ILP32: { 21412 struct mhioc_inresvs32 inresvs32; 21413 21414 if (ddi_copyin(arg, &inresvs32, 21415 sizeof (struct mhioc_inresvs32), flag) != 0) { 21416 return (EFAULT); 21417 } 21418 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 21419 if ((rval = sd_persistent_reservation_in_read_resv(un, 21420 &inresvs, flag)) != 0) { 21421 return (rval); 21422 } 21423 inresvs32.generation = inresvs.generation; 21424 if (ddi_copyout(&inresvs32, arg, 21425 sizeof (struct mhioc_inresvs32), flag) != 0) { 21426 return (EFAULT); 21427 } 21428 break; 21429 } 21430 case DDI_MODEL_NONE: 21431 if (ddi_copyin(arg, &inresvs, 21432 sizeof (mhioc_inresvs_t), flag) != 0) { 21433 return (EFAULT); 21434 } 21435 if ((rval = sd_persistent_reservation_in_read_resv(un, 21436 &inresvs, flag)) != 0) { 21437 return (rval); 21438 } 21439 if (ddi_copyout(&inresvs, arg, 21440 sizeof (mhioc_inresvs_t), flag) != 0) { 21441 return (EFAULT); 21442 } 21443 break; 21444 } 21445 21446 #else /* ! _MULTI_DATAMODEL */ 21447 21448 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 21449 return (EFAULT); 21450 } 21451 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 21452 if (rval != 0) { 21453 return (rval); 21454 } 21455 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 21456 return (EFAULT); 21457 } 21458 21459 #endif /* ! _MULTI_DATAMODEL */ 21460 21461 return (rval); 21462 } 21463 21464 21465 /* 21466 * The following routines support the clustering functionality described below 21467 * and implement lost reservation reclaim functionality. 21468 * 21469 * Clustering 21470 * ---------- 21471 * The clustering code uses two different, independent forms of SCSI 21472 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 21473 * Persistent Group Reservations. For any particular disk, it will use either 21474 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 21475 * 21476 * SCSI-2 21477 * The cluster software takes ownership of a multi-hosted disk by issuing the 21478 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 21479 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 21480 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 21481 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 21482 * driver. The meaning of failfast is that if the driver (on this host) ever 21483 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 21484 * it should immediately panic the host. The motivation for this ioctl is that 21485 * if this host does encounter reservation conflict, the underlying cause is 21486 * that some other host of the cluster has decided that this host is no longer 21487 * in the cluster and has seized control of the disks for itself. Since this 21488 * host is no longer in the cluster, it ought to panic itself. The 21489 * MHIOCENFAILFAST ioctl does two things: 21490 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 21491 * error to panic the host 21492 * (b) it sets up a periodic timer to test whether this host still has 21493 * "access" (in that no other host has reserved the device): if the 21494 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 21495 * purpose of that periodic timer is to handle scenarios where the host is 21496 * otherwise temporarily quiescent, temporarily doing no real i/o. 21497 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 21498 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 21499 * the device itself. 21500 * 21501 * SCSI-3 PGR 21502 * A direct semantic implementation of the SCSI-3 Persistent Reservation 21503 * facility is supported through the shared multihost disk ioctls 21504 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 21505 * MHIOCGRP_PREEMPTANDABORT) 21506 * 21507 * Reservation Reclaim: 21508 * -------------------- 21509 * To support the lost reservation reclaim operations this driver creates a 21510 * single thread to handle reinstating reservations on all devices that have 21511 * lost reservations sd_resv_reclaim_requests are logged for all devices that 21512 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 21513 * and the reservation reclaim thread loops through the requests to regain the 21514 * lost reservations. 21515 */ 21516 21517 /* 21518 * Function: sd_check_mhd() 21519 * 21520 * Description: This function sets up and submits a scsi watch request or 21521 * terminates an existing watch request. This routine is used in 21522 * support of reservation reclaim. 21523 * 21524 * Arguments: dev - the device 'dev_t' is used for context to discriminate 21525 * among multiple watches that share the callback function 21526 * interval - the number of microseconds specifying the watch 21527 * interval for issuing TEST UNIT READY commands. If 21528 * set to 0 the watch should be terminated. If the 21529 * interval is set to 0 and if the device is required 21530 * to hold reservation while disabling failfast, the 21531 * watch is restarted with an interval of 21532 * reinstate_resv_delay. 21533 * 21534 * Return Code: 0 - Successful submit/terminate of scsi watch request 21535 * ENXIO - Indicates an invalid device was specified 21536 * EAGAIN - Unable to submit the scsi watch request 21537 */ 21538 21539 static int 21540 sd_check_mhd(dev_t dev, int interval) 21541 { 21542 struct sd_lun *un; 21543 opaque_t token; 21544 21545 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21546 return (ENXIO); 21547 } 21548 21549 /* is this a watch termination request? */ 21550 if (interval == 0) { 21551 mutex_enter(SD_MUTEX(un)); 21552 /* if there is an existing watch task then terminate it */ 21553 if (un->un_mhd_token) { 21554 token = un->un_mhd_token; 21555 un->un_mhd_token = NULL; 21556 mutex_exit(SD_MUTEX(un)); 21557 (void) scsi_watch_request_terminate(token, 21558 SCSI_WATCH_TERMINATE_WAIT); 21559 mutex_enter(SD_MUTEX(un)); 21560 } else { 21561 mutex_exit(SD_MUTEX(un)); 21562 /* 21563 * Note: If we return here we don't check for the 21564 * failfast case. This is the original legacy 21565 * implementation but perhaps we should be checking 21566 * the failfast case. 21567 */ 21568 return (0); 21569 } 21570 /* 21571 * If the device is required to hold reservation while 21572 * disabling failfast, we need to restart the scsi_watch 21573 * routine with an interval of reinstate_resv_delay. 21574 */ 21575 if (un->un_resvd_status & SD_RESERVE) { 21576 interval = sd_reinstate_resv_delay/1000; 21577 } else { 21578 /* no failfast so bail */ 21579 mutex_exit(SD_MUTEX(un)); 21580 return (0); 21581 } 21582 mutex_exit(SD_MUTEX(un)); 21583 } 21584 21585 /* 21586 * adjust minimum time interval to 1 second, 21587 * and convert from msecs to usecs 21588 */ 21589 if (interval > 0 && interval < 1000) { 21590 interval = 1000; 21591 } 21592 interval *= 1000; 21593 21594 /* 21595 * submit the request to the scsi_watch service 21596 */ 21597 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 21598 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 21599 if (token == NULL) { 21600 return (EAGAIN); 21601 } 21602 21603 /* 21604 * save token for termination later on 21605 */ 21606 mutex_enter(SD_MUTEX(un)); 21607 un->un_mhd_token = token; 21608 mutex_exit(SD_MUTEX(un)); 21609 return (0); 21610 } 21611 21612 21613 /* 21614 * Function: sd_mhd_watch_cb() 21615 * 21616 * Description: This function is the call back function used by the scsi watch 21617 * facility. The scsi watch facility sends the "Test Unit Ready" 21618 * and processes the status. If applicable (i.e. a "Unit Attention" 21619 * status and automatic "Request Sense" not used) the scsi watch 21620 * facility will send a "Request Sense" and retrieve the sense data 21621 * to be passed to this callback function. In either case the 21622 * automatic "Request Sense" or the facility submitting one, this 21623 * callback is passed the status and sense data. 21624 * 21625 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21626 * among multiple watches that share this callback function 21627 * resultp - scsi watch facility result packet containing scsi 21628 * packet, status byte and sense data 21629 * 21630 * Return Code: 0 - continue the watch task 21631 * non-zero - terminate the watch task 21632 */ 21633 21634 static int 21635 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21636 { 21637 struct sd_lun *un; 21638 struct scsi_status *statusp; 21639 uint8_t *sensep; 21640 struct scsi_pkt *pkt; 21641 uchar_t actual_sense_length; 21642 dev_t dev = (dev_t)arg; 21643 21644 ASSERT(resultp != NULL); 21645 statusp = resultp->statusp; 21646 sensep = (uint8_t *)resultp->sensep; 21647 pkt = resultp->pkt; 21648 actual_sense_length = resultp->actual_sense_length; 21649 21650 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21651 return (ENXIO); 21652 } 21653 21654 SD_TRACE(SD_LOG_IOCTL_MHD, un, 21655 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 21656 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 21657 21658 /* Begin processing of the status and/or sense data */ 21659 if (pkt->pkt_reason != CMD_CMPLT) { 21660 /* Handle the incomplete packet */ 21661 sd_mhd_watch_incomplete(un, pkt); 21662 return (0); 21663 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 21664 if (*((unsigned char *)statusp) 21665 == STATUS_RESERVATION_CONFLICT) { 21666 /* 21667 * Handle a reservation conflict by panicking if 21668 * configured for failfast or by logging the conflict 21669 * and updating the reservation status 21670 */ 21671 mutex_enter(SD_MUTEX(un)); 21672 if ((un->un_resvd_status & SD_FAILFAST) && 21673 (sd_failfast_enable)) { 21674 sd_panic_for_res_conflict(un); 21675 /*NOTREACHED*/ 21676 } 21677 SD_INFO(SD_LOG_IOCTL_MHD, un, 21678 "sd_mhd_watch_cb: Reservation Conflict\n"); 21679 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 21680 mutex_exit(SD_MUTEX(un)); 21681 } 21682 } 21683 21684 if (sensep != NULL) { 21685 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 21686 mutex_enter(SD_MUTEX(un)); 21687 if ((scsi_sense_asc(sensep) == 21688 SD_SCSI_RESET_SENSE_CODE) && 21689 (un->un_resvd_status & SD_RESERVE)) { 21690 /* 21691 * The additional sense code indicates a power 21692 * on or bus device reset has occurred; update 21693 * the reservation status. 21694 */ 21695 un->un_resvd_status |= 21696 (SD_LOST_RESERVE | SD_WANT_RESERVE); 21697 SD_INFO(SD_LOG_IOCTL_MHD, un, 21698 "sd_mhd_watch_cb: Lost Reservation\n"); 21699 } 21700 } else { 21701 return (0); 21702 } 21703 } else { 21704 mutex_enter(SD_MUTEX(un)); 21705 } 21706 21707 if ((un->un_resvd_status & SD_RESERVE) && 21708 (un->un_resvd_status & SD_LOST_RESERVE)) { 21709 if (un->un_resvd_status & SD_WANT_RESERVE) { 21710 /* 21711 * A reset occurred in between the last probe and this 21712 * one so if a timeout is pending cancel it. 21713 */ 21714 if (un->un_resvd_timeid) { 21715 timeout_id_t temp_id = un->un_resvd_timeid; 21716 un->un_resvd_timeid = NULL; 21717 mutex_exit(SD_MUTEX(un)); 21718 (void) untimeout(temp_id); 21719 mutex_enter(SD_MUTEX(un)); 21720 } 21721 un->un_resvd_status &= ~SD_WANT_RESERVE; 21722 } 21723 if (un->un_resvd_timeid == 0) { 21724 /* Schedule a timeout to handle the lost reservation */ 21725 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 21726 (void *)dev, 21727 drv_usectohz(sd_reinstate_resv_delay)); 21728 } 21729 } 21730 mutex_exit(SD_MUTEX(un)); 21731 return (0); 21732 } 21733 21734 21735 /* 21736 * Function: sd_mhd_watch_incomplete() 21737 * 21738 * Description: This function is used to find out why a scsi pkt sent by the 21739 * scsi watch facility was not completed. Under some scenarios this 21740 * routine will return. Otherwise it will send a bus reset to see 21741 * if the drive is still online. 21742 * 21743 * Arguments: un - driver soft state (unit) structure 21744 * pkt - incomplete scsi pkt 21745 */ 21746 21747 static void 21748 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 21749 { 21750 int be_chatty; 21751 int perr; 21752 21753 ASSERT(pkt != NULL); 21754 ASSERT(un != NULL); 21755 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 21756 perr = (pkt->pkt_statistics & STAT_PERR); 21757 21758 mutex_enter(SD_MUTEX(un)); 21759 if (un->un_state == SD_STATE_DUMPING) { 21760 mutex_exit(SD_MUTEX(un)); 21761 return; 21762 } 21763 21764 switch (pkt->pkt_reason) { 21765 case CMD_UNX_BUS_FREE: 21766 /* 21767 * If we had a parity error that caused the target to drop BSY*, 21768 * don't be chatty about it. 21769 */ 21770 if (perr && be_chatty) { 21771 be_chatty = 0; 21772 } 21773 break; 21774 case CMD_TAG_REJECT: 21775 /* 21776 * The SCSI-2 spec states that a tag reject will be sent by the 21777 * target if tagged queuing is not supported. A tag reject may 21778 * also be sent during certain initialization periods or to 21779 * control internal resources. For the latter case the target 21780 * may also return Queue Full. 21781 * 21782 * If this driver receives a tag reject from a target that is 21783 * going through an init period or controlling internal 21784 * resources tagged queuing will be disabled. This is a less 21785 * than optimal behavior but the driver is unable to determine 21786 * the target state and assumes tagged queueing is not supported 21787 */ 21788 pkt->pkt_flags = 0; 21789 un->un_tagflags = 0; 21790 21791 if (un->un_f_opt_queueing == TRUE) { 21792 un->un_throttle = min(un->un_throttle, 3); 21793 } else { 21794 un->un_throttle = 1; 21795 } 21796 mutex_exit(SD_MUTEX(un)); 21797 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 21798 mutex_enter(SD_MUTEX(un)); 21799 break; 21800 case CMD_INCOMPLETE: 21801 /* 21802 * The transport stopped with an abnormal state, fallthrough and 21803 * reset the target and/or bus unless selection did not complete 21804 * (indicated by STATE_GOT_BUS) in which case we don't want to 21805 * go through a target/bus reset 21806 */ 21807 if (pkt->pkt_state == STATE_GOT_BUS) { 21808 break; 21809 } 21810 /*FALLTHROUGH*/ 21811 21812 case CMD_TIMEOUT: 21813 default: 21814 /* 21815 * The lun may still be running the command, so a lun reset 21816 * should be attempted. If the lun reset fails or cannot be 21817 * issued, than try a target reset. Lastly try a bus reset. 21818 */ 21819 if ((pkt->pkt_statistics & 21820 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 21821 int reset_retval = 0; 21822 mutex_exit(SD_MUTEX(un)); 21823 if (un->un_f_allow_bus_device_reset == TRUE) { 21824 if (un->un_f_lun_reset_enabled == TRUE) { 21825 reset_retval = 21826 scsi_reset(SD_ADDRESS(un), 21827 RESET_LUN); 21828 } 21829 if (reset_retval == 0) { 21830 reset_retval = 21831 scsi_reset(SD_ADDRESS(un), 21832 RESET_TARGET); 21833 } 21834 } 21835 if (reset_retval == 0) { 21836 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 21837 } 21838 mutex_enter(SD_MUTEX(un)); 21839 } 21840 break; 21841 } 21842 21843 /* A device/bus reset has occurred; update the reservation status. */ 21844 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 21845 (STAT_BUS_RESET | STAT_DEV_RESET))) { 21846 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 21847 un->un_resvd_status |= 21848 (SD_LOST_RESERVE | SD_WANT_RESERVE); 21849 SD_INFO(SD_LOG_IOCTL_MHD, un, 21850 "sd_mhd_watch_incomplete: Lost Reservation\n"); 21851 } 21852 } 21853 21854 /* 21855 * The disk has been turned off; Update the device state. 21856 * 21857 * Note: Should we be offlining the disk here? 21858 */ 21859 if (pkt->pkt_state == STATE_GOT_BUS) { 21860 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 21861 "Disk not responding to selection\n"); 21862 if (un->un_state != SD_STATE_OFFLINE) { 21863 New_state(un, SD_STATE_OFFLINE); 21864 } 21865 } else if (be_chatty) { 21866 /* 21867 * suppress messages if they are all the same pkt reason; 21868 * with TQ, many (up to 256) are returned with the same 21869 * pkt_reason 21870 */ 21871 if (pkt->pkt_reason != un->un_last_pkt_reason) { 21872 SD_ERROR(SD_LOG_IOCTL_MHD, un, 21873 "sd_mhd_watch_incomplete: " 21874 "SCSI transport failed: reason '%s'\n", 21875 scsi_rname(pkt->pkt_reason)); 21876 } 21877 } 21878 un->un_last_pkt_reason = pkt->pkt_reason; 21879 mutex_exit(SD_MUTEX(un)); 21880 } 21881 21882 21883 /* 21884 * Function: sd_sname() 21885 * 21886 * Description: This is a simple little routine to return a string containing 21887 * a printable description of command status byte for use in 21888 * logging. 21889 * 21890 * Arguments: status - pointer to a status byte 21891 * 21892 * Return Code: char * - string containing status description. 21893 */ 21894 21895 static char * 21896 sd_sname(uchar_t status) 21897 { 21898 switch (status & STATUS_MASK) { 21899 case STATUS_GOOD: 21900 return ("good status"); 21901 case STATUS_CHECK: 21902 return ("check condition"); 21903 case STATUS_MET: 21904 return ("condition met"); 21905 case STATUS_BUSY: 21906 return ("busy"); 21907 case STATUS_INTERMEDIATE: 21908 return ("intermediate"); 21909 case STATUS_INTERMEDIATE_MET: 21910 return ("intermediate - condition met"); 21911 case STATUS_RESERVATION_CONFLICT: 21912 return ("reservation_conflict"); 21913 case STATUS_TERMINATED: 21914 return ("command terminated"); 21915 case STATUS_QFULL: 21916 return ("queue full"); 21917 default: 21918 return ("<unknown status>"); 21919 } 21920 } 21921 21922 21923 /* 21924 * Function: sd_mhd_resvd_recover() 21925 * 21926 * Description: This function adds a reservation entry to the 21927 * sd_resv_reclaim_request list and signals the reservation 21928 * reclaim thread that there is work pending. If the reservation 21929 * reclaim thread has not been previously created this function 21930 * will kick it off. 21931 * 21932 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21933 * among multiple watches that share this callback function 21934 * 21935 * Context: This routine is called by timeout() and is run in interrupt 21936 * context. It must not sleep or call other functions which may 21937 * sleep. 21938 */ 21939 21940 static void 21941 sd_mhd_resvd_recover(void *arg) 21942 { 21943 dev_t dev = (dev_t)arg; 21944 struct sd_lun *un; 21945 struct sd_thr_request *sd_treq = NULL; 21946 struct sd_thr_request *sd_cur = NULL; 21947 struct sd_thr_request *sd_prev = NULL; 21948 int already_there = 0; 21949 21950 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21951 return; 21952 } 21953 21954 mutex_enter(SD_MUTEX(un)); 21955 un->un_resvd_timeid = NULL; 21956 if (un->un_resvd_status & SD_WANT_RESERVE) { 21957 /* 21958 * There was a reset so don't issue the reserve, allow the 21959 * sd_mhd_watch_cb callback function to notice this and 21960 * reschedule the timeout for reservation. 21961 */ 21962 mutex_exit(SD_MUTEX(un)); 21963 return; 21964 } 21965 mutex_exit(SD_MUTEX(un)); 21966 21967 /* 21968 * Add this device to the sd_resv_reclaim_request list and the 21969 * sd_resv_reclaim_thread should take care of the rest. 21970 * 21971 * Note: We can't sleep in this context so if the memory allocation 21972 * fails allow the sd_mhd_watch_cb callback function to notice this and 21973 * reschedule the timeout for reservation. (4378460) 21974 */ 21975 sd_treq = (struct sd_thr_request *) 21976 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 21977 if (sd_treq == NULL) { 21978 return; 21979 } 21980 21981 sd_treq->sd_thr_req_next = NULL; 21982 sd_treq->dev = dev; 21983 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 21984 if (sd_tr.srq_thr_req_head == NULL) { 21985 sd_tr.srq_thr_req_head = sd_treq; 21986 } else { 21987 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 21988 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 21989 if (sd_cur->dev == dev) { 21990 /* 21991 * already in Queue so don't log 21992 * another request for the device 21993 */ 21994 already_there = 1; 21995 break; 21996 } 21997 sd_prev = sd_cur; 21998 } 21999 if (!already_there) { 22000 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 22001 "logging request for %lx\n", dev); 22002 sd_prev->sd_thr_req_next = sd_treq; 22003 } else { 22004 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 22005 } 22006 } 22007 22008 /* 22009 * Create a kernel thread to do the reservation reclaim and free up this 22010 * thread. We cannot block this thread while we go away to do the 22011 * reservation reclaim 22012 */ 22013 if (sd_tr.srq_resv_reclaim_thread == NULL) 22014 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 22015 sd_resv_reclaim_thread, NULL, 22016 0, &p0, TS_RUN, v.v_maxsyspri - 2); 22017 22018 /* Tell the reservation reclaim thread that it has work to do */ 22019 cv_signal(&sd_tr.srq_resv_reclaim_cv); 22020 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22021 } 22022 22023 /* 22024 * Function: sd_resv_reclaim_thread() 22025 * 22026 * Description: This function implements the reservation reclaim operations 22027 * 22028 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22029 * among multiple watches that share this callback function 22030 */ 22031 22032 static void 22033 sd_resv_reclaim_thread() 22034 { 22035 struct sd_lun *un; 22036 struct sd_thr_request *sd_mhreq; 22037 22038 /* Wait for work */ 22039 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22040 if (sd_tr.srq_thr_req_head == NULL) { 22041 cv_wait(&sd_tr.srq_resv_reclaim_cv, 22042 &sd_tr.srq_resv_reclaim_mutex); 22043 } 22044 22045 /* Loop while we have work */ 22046 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 22047 un = ddi_get_soft_state(sd_state, 22048 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 22049 if (un == NULL) { 22050 /* 22051 * softstate structure is NULL so just 22052 * dequeue the request and continue 22053 */ 22054 sd_tr.srq_thr_req_head = 22055 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22056 kmem_free(sd_tr.srq_thr_cur_req, 22057 sizeof (struct sd_thr_request)); 22058 continue; 22059 } 22060 22061 /* dequeue the request */ 22062 sd_mhreq = sd_tr.srq_thr_cur_req; 22063 sd_tr.srq_thr_req_head = 22064 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22065 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22066 22067 /* 22068 * Reclaim reservation only if SD_RESERVE is still set. There 22069 * may have been a call to MHIOCRELEASE before we got here. 22070 */ 22071 mutex_enter(SD_MUTEX(un)); 22072 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22073 /* 22074 * Note: The SD_LOST_RESERVE flag is cleared before 22075 * reclaiming the reservation. If this is done after the 22076 * call to sd_reserve_release a reservation loss in the 22077 * window between pkt completion of reserve cmd and 22078 * mutex_enter below may not be recognized 22079 */ 22080 un->un_resvd_status &= ~SD_LOST_RESERVE; 22081 mutex_exit(SD_MUTEX(un)); 22082 22083 if (sd_reserve_release(sd_mhreq->dev, 22084 SD_RESERVE) == 0) { 22085 mutex_enter(SD_MUTEX(un)); 22086 un->un_resvd_status |= SD_RESERVE; 22087 mutex_exit(SD_MUTEX(un)); 22088 SD_INFO(SD_LOG_IOCTL_MHD, un, 22089 "sd_resv_reclaim_thread: " 22090 "Reservation Recovered\n"); 22091 } else { 22092 mutex_enter(SD_MUTEX(un)); 22093 un->un_resvd_status |= SD_LOST_RESERVE; 22094 mutex_exit(SD_MUTEX(un)); 22095 SD_INFO(SD_LOG_IOCTL_MHD, un, 22096 "sd_resv_reclaim_thread: Failed " 22097 "Reservation Recovery\n"); 22098 } 22099 } else { 22100 mutex_exit(SD_MUTEX(un)); 22101 } 22102 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22103 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 22104 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22105 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 22106 /* 22107 * wakeup the destroy thread if anyone is waiting on 22108 * us to complete. 22109 */ 22110 cv_signal(&sd_tr.srq_inprocess_cv); 22111 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22112 "sd_resv_reclaim_thread: cv_signalling current request \n"); 22113 } 22114 22115 /* 22116 * cleanup the sd_tr structure now that this thread will not exist 22117 */ 22118 ASSERT(sd_tr.srq_thr_req_head == NULL); 22119 ASSERT(sd_tr.srq_thr_cur_req == NULL); 22120 sd_tr.srq_resv_reclaim_thread = NULL; 22121 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22122 thread_exit(); 22123 } 22124 22125 22126 /* 22127 * Function: sd_rmv_resv_reclaim_req() 22128 * 22129 * Description: This function removes any pending reservation reclaim requests 22130 * for the specified device. 22131 * 22132 * Arguments: dev - the device 'dev_t' 22133 */ 22134 22135 static void 22136 sd_rmv_resv_reclaim_req(dev_t dev) 22137 { 22138 struct sd_thr_request *sd_mhreq; 22139 struct sd_thr_request *sd_prev; 22140 22141 /* Remove a reservation reclaim request from the list */ 22142 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22143 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 22144 /* 22145 * We are attempting to reinstate reservation for 22146 * this device. We wait for sd_reserve_release() 22147 * to return before we return. 22148 */ 22149 cv_wait(&sd_tr.srq_inprocess_cv, 22150 &sd_tr.srq_resv_reclaim_mutex); 22151 } else { 22152 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 22153 if (sd_mhreq && sd_mhreq->dev == dev) { 22154 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 22155 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22156 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22157 return; 22158 } 22159 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 22160 if (sd_mhreq && sd_mhreq->dev == dev) { 22161 break; 22162 } 22163 sd_prev = sd_mhreq; 22164 } 22165 if (sd_mhreq != NULL) { 22166 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 22167 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22168 } 22169 } 22170 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22171 } 22172 22173 22174 /* 22175 * Function: sd_mhd_reset_notify_cb() 22176 * 22177 * Description: This is a call back function for scsi_reset_notify. This 22178 * function updates the softstate reserved status and logs the 22179 * reset. The driver scsi watch facility callback function 22180 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 22181 * will reclaim the reservation. 22182 * 22183 * Arguments: arg - driver soft state (unit) structure 22184 */ 22185 22186 static void 22187 sd_mhd_reset_notify_cb(caddr_t arg) 22188 { 22189 struct sd_lun *un = (struct sd_lun *)arg; 22190 22191 mutex_enter(SD_MUTEX(un)); 22192 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22193 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 22194 SD_INFO(SD_LOG_IOCTL_MHD, un, 22195 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 22196 } 22197 mutex_exit(SD_MUTEX(un)); 22198 } 22199 22200 22201 /* 22202 * Function: sd_take_ownership() 22203 * 22204 * Description: This routine implements an algorithm to achieve a stable 22205 * reservation on disks which don't implement priority reserve, 22206 * and makes sure that other host lose re-reservation attempts. 22207 * This algorithm contains of a loop that keeps issuing the RESERVE 22208 * for some period of time (min_ownership_delay, default 6 seconds) 22209 * During that loop, it looks to see if there has been a bus device 22210 * reset or bus reset (both of which cause an existing reservation 22211 * to be lost). If the reservation is lost issue RESERVE until a 22212 * period of min_ownership_delay with no resets has gone by, or 22213 * until max_ownership_delay has expired. This loop ensures that 22214 * the host really did manage to reserve the device, in spite of 22215 * resets. The looping for min_ownership_delay (default six 22216 * seconds) is important to early generation clustering products, 22217 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 22218 * MHIOCENFAILFAST periodic timer of two seconds. By having 22219 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 22220 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 22221 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 22222 * have already noticed, via the MHIOCENFAILFAST polling, that it 22223 * no longer "owns" the disk and will have panicked itself. Thus, 22224 * the host issuing the MHIOCTKOWN is assured (with timing 22225 * dependencies) that by the time it actually starts to use the 22226 * disk for real work, the old owner is no longer accessing it. 22227 * 22228 * min_ownership_delay is the minimum amount of time for which the 22229 * disk must be reserved continuously devoid of resets before the 22230 * MHIOCTKOWN ioctl will return success. 22231 * 22232 * max_ownership_delay indicates the amount of time by which the 22233 * take ownership should succeed or timeout with an error. 22234 * 22235 * Arguments: dev - the device 'dev_t' 22236 * *p - struct containing timing info. 22237 * 22238 * Return Code: 0 for success or error code 22239 */ 22240 22241 static int 22242 sd_take_ownership(dev_t dev, struct mhioctkown *p) 22243 { 22244 struct sd_lun *un; 22245 int rval; 22246 int err; 22247 int reservation_count = 0; 22248 int min_ownership_delay = 6000000; /* in usec */ 22249 int max_ownership_delay = 30000000; /* in usec */ 22250 clock_t start_time; /* starting time of this algorithm */ 22251 clock_t end_time; /* time limit for giving up */ 22252 clock_t ownership_time; /* time limit for stable ownership */ 22253 clock_t current_time; 22254 clock_t previous_current_time; 22255 22256 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22257 return (ENXIO); 22258 } 22259 22260 /* 22261 * Attempt a device reservation. A priority reservation is requested. 22262 */ 22263 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 22264 != SD_SUCCESS) { 22265 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22266 "sd_take_ownership: return(1)=%d\n", rval); 22267 return (rval); 22268 } 22269 22270 /* Update the softstate reserved status to indicate the reservation */ 22271 mutex_enter(SD_MUTEX(un)); 22272 un->un_resvd_status |= SD_RESERVE; 22273 un->un_resvd_status &= 22274 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 22275 mutex_exit(SD_MUTEX(un)); 22276 22277 if (p != NULL) { 22278 if (p->min_ownership_delay != 0) { 22279 min_ownership_delay = p->min_ownership_delay * 1000; 22280 } 22281 if (p->max_ownership_delay != 0) { 22282 max_ownership_delay = p->max_ownership_delay * 1000; 22283 } 22284 } 22285 SD_INFO(SD_LOG_IOCTL_MHD, un, 22286 "sd_take_ownership: min, max delays: %d, %d\n", 22287 min_ownership_delay, max_ownership_delay); 22288 22289 start_time = ddi_get_lbolt(); 22290 current_time = start_time; 22291 ownership_time = current_time + drv_usectohz(min_ownership_delay); 22292 end_time = start_time + drv_usectohz(max_ownership_delay); 22293 22294 while (current_time - end_time < 0) { 22295 delay(drv_usectohz(500000)); 22296 22297 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 22298 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 22299 mutex_enter(SD_MUTEX(un)); 22300 rval = (un->un_resvd_status & 22301 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 22302 mutex_exit(SD_MUTEX(un)); 22303 break; 22304 } 22305 } 22306 previous_current_time = current_time; 22307 current_time = ddi_get_lbolt(); 22308 mutex_enter(SD_MUTEX(un)); 22309 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 22310 ownership_time = ddi_get_lbolt() + 22311 drv_usectohz(min_ownership_delay); 22312 reservation_count = 0; 22313 } else { 22314 reservation_count++; 22315 } 22316 un->un_resvd_status |= SD_RESERVE; 22317 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 22318 mutex_exit(SD_MUTEX(un)); 22319 22320 SD_INFO(SD_LOG_IOCTL_MHD, un, 22321 "sd_take_ownership: ticks for loop iteration=%ld, " 22322 "reservation=%s\n", (current_time - previous_current_time), 22323 reservation_count ? "ok" : "reclaimed"); 22324 22325 if (current_time - ownership_time >= 0 && 22326 reservation_count >= 4) { 22327 rval = 0; /* Achieved a stable ownership */ 22328 break; 22329 } 22330 if (current_time - end_time >= 0) { 22331 rval = EACCES; /* No ownership in max possible time */ 22332 break; 22333 } 22334 } 22335 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22336 "sd_take_ownership: return(2)=%d\n", rval); 22337 return (rval); 22338 } 22339 22340 22341 /* 22342 * Function: sd_reserve_release() 22343 * 22344 * Description: This function builds and sends scsi RESERVE, RELEASE, and 22345 * PRIORITY RESERVE commands based on a user specified command type 22346 * 22347 * Arguments: dev - the device 'dev_t' 22348 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 22349 * SD_RESERVE, SD_RELEASE 22350 * 22351 * Return Code: 0 or Error Code 22352 */ 22353 22354 static int 22355 sd_reserve_release(dev_t dev, int cmd) 22356 { 22357 struct uscsi_cmd *com = NULL; 22358 struct sd_lun *un = NULL; 22359 char cdb[CDB_GROUP0]; 22360 int rval; 22361 22362 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 22363 (cmd == SD_PRIORITY_RESERVE)); 22364 22365 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22366 return (ENXIO); 22367 } 22368 22369 /* instantiate and initialize the command and cdb */ 22370 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 22371 bzero(cdb, CDB_GROUP0); 22372 com->uscsi_flags = USCSI_SILENT; 22373 com->uscsi_timeout = un->un_reserve_release_time; 22374 com->uscsi_cdblen = CDB_GROUP0; 22375 com->uscsi_cdb = cdb; 22376 if (cmd == SD_RELEASE) { 22377 cdb[0] = SCMD_RELEASE; 22378 } else { 22379 cdb[0] = SCMD_RESERVE; 22380 } 22381 22382 /* Send the command. */ 22383 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22384 SD_PATH_STANDARD); 22385 22386 /* 22387 * "break" a reservation that is held by another host, by issuing a 22388 * reset if priority reserve is desired, and we could not get the 22389 * device. 22390 */ 22391 if ((cmd == SD_PRIORITY_RESERVE) && 22392 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22393 /* 22394 * First try to reset the LUN. If we cannot, then try a target 22395 * reset, followed by a bus reset if the target reset fails. 22396 */ 22397 int reset_retval = 0; 22398 if (un->un_f_lun_reset_enabled == TRUE) { 22399 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 22400 } 22401 if (reset_retval == 0) { 22402 /* The LUN reset either failed or was not issued */ 22403 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 22404 } 22405 if ((reset_retval == 0) && 22406 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 22407 rval = EIO; 22408 kmem_free(com, sizeof (*com)); 22409 return (rval); 22410 } 22411 22412 bzero(com, sizeof (struct uscsi_cmd)); 22413 com->uscsi_flags = USCSI_SILENT; 22414 com->uscsi_cdb = cdb; 22415 com->uscsi_cdblen = CDB_GROUP0; 22416 com->uscsi_timeout = 5; 22417 22418 /* 22419 * Reissue the last reserve command, this time without request 22420 * sense. Assume that it is just a regular reserve command. 22421 */ 22422 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22423 SD_PATH_STANDARD); 22424 } 22425 22426 /* Return an error if still getting a reservation conflict. */ 22427 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22428 rval = EACCES; 22429 } 22430 22431 kmem_free(com, sizeof (*com)); 22432 return (rval); 22433 } 22434 22435 22436 #define SD_NDUMP_RETRIES 12 22437 /* 22438 * System Crash Dump routine 22439 */ 22440 22441 static int 22442 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 22443 { 22444 int instance; 22445 int partition; 22446 int i; 22447 int err; 22448 struct sd_lun *un; 22449 struct scsi_pkt *wr_pktp; 22450 struct buf *wr_bp; 22451 struct buf wr_buf; 22452 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 22453 daddr_t tgt_blkno; /* rmw - blkno for target */ 22454 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 22455 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 22456 size_t io_start_offset; 22457 int doing_rmw = FALSE; 22458 int rval; 22459 #if defined(__i386) || defined(__amd64) 22460 ssize_t dma_resid; 22461 daddr_t oblkno; 22462 #endif 22463 diskaddr_t nblks = 0; 22464 diskaddr_t start_block; 22465 22466 instance = SDUNIT(dev); 22467 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 22468 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 22469 return (ENXIO); 22470 } 22471 22472 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 22473 22474 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 22475 22476 partition = SDPART(dev); 22477 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 22478 22479 /* Validate blocks to dump at against partition size. */ 22480 22481 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 22482 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 22483 22484 if ((blkno + nblk) > nblks) { 22485 SD_TRACE(SD_LOG_DUMP, un, 22486 "sddump: dump range larger than partition: " 22487 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 22488 blkno, nblk, nblks); 22489 return (EINVAL); 22490 } 22491 22492 mutex_enter(&un->un_pm_mutex); 22493 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 22494 struct scsi_pkt *start_pktp; 22495 22496 mutex_exit(&un->un_pm_mutex); 22497 22498 /* 22499 * use pm framework to power on HBA 1st 22500 */ 22501 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 22502 22503 /* 22504 * Dump no long uses sdpower to power on a device, it's 22505 * in-line here so it can be done in polled mode. 22506 */ 22507 22508 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 22509 22510 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 22511 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 22512 22513 if (start_pktp == NULL) { 22514 /* We were not given a SCSI packet, fail. */ 22515 return (EIO); 22516 } 22517 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 22518 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 22519 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 22520 start_pktp->pkt_flags = FLAG_NOINTR; 22521 22522 mutex_enter(SD_MUTEX(un)); 22523 SD_FILL_SCSI1_LUN(un, start_pktp); 22524 mutex_exit(SD_MUTEX(un)); 22525 /* 22526 * Scsi_poll returns 0 (success) if the command completes and 22527 * the status block is STATUS_GOOD. 22528 */ 22529 if (sd_scsi_poll(un, start_pktp) != 0) { 22530 scsi_destroy_pkt(start_pktp); 22531 return (EIO); 22532 } 22533 scsi_destroy_pkt(start_pktp); 22534 (void) sd_ddi_pm_resume(un); 22535 } else { 22536 mutex_exit(&un->un_pm_mutex); 22537 } 22538 22539 mutex_enter(SD_MUTEX(un)); 22540 un->un_throttle = 0; 22541 22542 /* 22543 * The first time through, reset the specific target device. 22544 * However, when cpr calls sddump we know that sd is in a 22545 * a good state so no bus reset is required. 22546 * Clear sense data via Request Sense cmd. 22547 * In sddump we don't care about allow_bus_device_reset anymore 22548 */ 22549 22550 if ((un->un_state != SD_STATE_SUSPENDED) && 22551 (un->un_state != SD_STATE_DUMPING)) { 22552 22553 New_state(un, SD_STATE_DUMPING); 22554 22555 if (un->un_f_is_fibre == FALSE) { 22556 mutex_exit(SD_MUTEX(un)); 22557 /* 22558 * Attempt a bus reset for parallel scsi. 22559 * 22560 * Note: A bus reset is required because on some host 22561 * systems (i.e. E420R) a bus device reset is 22562 * insufficient to reset the state of the target. 22563 * 22564 * Note: Don't issue the reset for fibre-channel, 22565 * because this tends to hang the bus (loop) for 22566 * too long while everyone is logging out and in 22567 * and the deadman timer for dumping will fire 22568 * before the dump is complete. 22569 */ 22570 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 22571 mutex_enter(SD_MUTEX(un)); 22572 Restore_state(un); 22573 mutex_exit(SD_MUTEX(un)); 22574 return (EIO); 22575 } 22576 22577 /* Delay to give the device some recovery time. */ 22578 drv_usecwait(10000); 22579 22580 if (sd_send_polled_RQS(un) == SD_FAILURE) { 22581 SD_INFO(SD_LOG_DUMP, un, 22582 "sddump: sd_send_polled_RQS failed\n"); 22583 } 22584 mutex_enter(SD_MUTEX(un)); 22585 } 22586 } 22587 22588 /* 22589 * Convert the partition-relative block number to a 22590 * disk physical block number. 22591 */ 22592 blkno += start_block; 22593 22594 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 22595 22596 22597 /* 22598 * Check if the device has a non-512 block size. 22599 */ 22600 wr_bp = NULL; 22601 if (NOT_DEVBSIZE(un)) { 22602 tgt_byte_offset = blkno * un->un_sys_blocksize; 22603 tgt_byte_count = nblk * un->un_sys_blocksize; 22604 if ((tgt_byte_offset % un->un_tgt_blocksize) || 22605 (tgt_byte_count % un->un_tgt_blocksize)) { 22606 doing_rmw = TRUE; 22607 /* 22608 * Calculate the block number and number of block 22609 * in terms of the media block size. 22610 */ 22611 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 22612 tgt_nblk = 22613 ((tgt_byte_offset + tgt_byte_count + 22614 (un->un_tgt_blocksize - 1)) / 22615 un->un_tgt_blocksize) - tgt_blkno; 22616 22617 /* 22618 * Invoke the routine which is going to do read part 22619 * of read-modify-write. 22620 * Note that this routine returns a pointer to 22621 * a valid bp in wr_bp. 22622 */ 22623 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 22624 &wr_bp); 22625 if (err) { 22626 mutex_exit(SD_MUTEX(un)); 22627 return (err); 22628 } 22629 /* 22630 * Offset is being calculated as - 22631 * (original block # * system block size) - 22632 * (new block # * target block size) 22633 */ 22634 io_start_offset = 22635 ((uint64_t)(blkno * un->un_sys_blocksize)) - 22636 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 22637 22638 ASSERT((io_start_offset >= 0) && 22639 (io_start_offset < un->un_tgt_blocksize)); 22640 /* 22641 * Do the modify portion of read modify write. 22642 */ 22643 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 22644 (size_t)nblk * un->un_sys_blocksize); 22645 } else { 22646 doing_rmw = FALSE; 22647 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 22648 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 22649 } 22650 22651 /* Convert blkno and nblk to target blocks */ 22652 blkno = tgt_blkno; 22653 nblk = tgt_nblk; 22654 } else { 22655 wr_bp = &wr_buf; 22656 bzero(wr_bp, sizeof (struct buf)); 22657 wr_bp->b_flags = B_BUSY; 22658 wr_bp->b_un.b_addr = addr; 22659 wr_bp->b_bcount = nblk << DEV_BSHIFT; 22660 wr_bp->b_resid = 0; 22661 } 22662 22663 mutex_exit(SD_MUTEX(un)); 22664 22665 /* 22666 * Obtain a SCSI packet for the write command. 22667 * It should be safe to call the allocator here without 22668 * worrying about being locked for DVMA mapping because 22669 * the address we're passed is already a DVMA mapping 22670 * 22671 * We are also not going to worry about semaphore ownership 22672 * in the dump buffer. Dumping is single threaded at present. 22673 */ 22674 22675 wr_pktp = NULL; 22676 22677 #if defined(__i386) || defined(__amd64) 22678 dma_resid = wr_bp->b_bcount; 22679 oblkno = blkno; 22680 while (dma_resid != 0) { 22681 #endif 22682 22683 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 22684 wr_bp->b_flags &= ~B_ERROR; 22685 22686 #if defined(__i386) || defined(__amd64) 22687 blkno = oblkno + 22688 ((wr_bp->b_bcount - dma_resid) / 22689 un->un_tgt_blocksize); 22690 nblk = dma_resid / un->un_tgt_blocksize; 22691 22692 if (wr_pktp) { 22693 /* Partial DMA transfers after initial transfer */ 22694 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 22695 blkno, nblk); 22696 } else { 22697 /* Initial transfer */ 22698 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 22699 un->un_pkt_flags, NULL_FUNC, NULL, 22700 blkno, nblk); 22701 } 22702 #else 22703 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 22704 0, NULL_FUNC, NULL, blkno, nblk); 22705 #endif 22706 22707 if (rval == 0) { 22708 /* We were given a SCSI packet, continue. */ 22709 break; 22710 } 22711 22712 if (i == 0) { 22713 if (wr_bp->b_flags & B_ERROR) { 22714 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 22715 "no resources for dumping; " 22716 "error code: 0x%x, retrying", 22717 geterror(wr_bp)); 22718 } else { 22719 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 22720 "no resources for dumping; retrying"); 22721 } 22722 } else if (i != (SD_NDUMP_RETRIES - 1)) { 22723 if (wr_bp->b_flags & B_ERROR) { 22724 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 22725 "no resources for dumping; error code: " 22726 "0x%x, retrying\n", geterror(wr_bp)); 22727 } 22728 } else { 22729 if (wr_bp->b_flags & B_ERROR) { 22730 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 22731 "no resources for dumping; " 22732 "error code: 0x%x, retries failed, " 22733 "giving up.\n", geterror(wr_bp)); 22734 } else { 22735 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 22736 "no resources for dumping; " 22737 "retries failed, giving up.\n"); 22738 } 22739 mutex_enter(SD_MUTEX(un)); 22740 Restore_state(un); 22741 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 22742 mutex_exit(SD_MUTEX(un)); 22743 scsi_free_consistent_buf(wr_bp); 22744 } else { 22745 mutex_exit(SD_MUTEX(un)); 22746 } 22747 return (EIO); 22748 } 22749 drv_usecwait(10000); 22750 } 22751 22752 #if defined(__i386) || defined(__amd64) 22753 /* 22754 * save the resid from PARTIAL_DMA 22755 */ 22756 dma_resid = wr_pktp->pkt_resid; 22757 if (dma_resid != 0) 22758 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 22759 wr_pktp->pkt_resid = 0; 22760 #endif 22761 22762 /* SunBug 1222170 */ 22763 wr_pktp->pkt_flags = FLAG_NOINTR; 22764 22765 err = EIO; 22766 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 22767 22768 /* 22769 * Scsi_poll returns 0 (success) if the command completes and 22770 * the status block is STATUS_GOOD. We should only check 22771 * errors if this condition is not true. Even then we should 22772 * send our own request sense packet only if we have a check 22773 * condition and auto request sense has not been performed by 22774 * the hba. 22775 */ 22776 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 22777 22778 if ((sd_scsi_poll(un, wr_pktp) == 0) && 22779 (wr_pktp->pkt_resid == 0)) { 22780 err = SD_SUCCESS; 22781 break; 22782 } 22783 22784 /* 22785 * Check CMD_DEV_GONE 1st, give up if device is gone. 22786 */ 22787 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 22788 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 22789 "Device is gone\n"); 22790 break; 22791 } 22792 22793 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 22794 SD_INFO(SD_LOG_DUMP, un, 22795 "sddump: write failed with CHECK, try # %d\n", i); 22796 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 22797 (void) sd_send_polled_RQS(un); 22798 } 22799 22800 continue; 22801 } 22802 22803 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 22804 int reset_retval = 0; 22805 22806 SD_INFO(SD_LOG_DUMP, un, 22807 "sddump: write failed with BUSY, try # %d\n", i); 22808 22809 if (un->un_f_lun_reset_enabled == TRUE) { 22810 reset_retval = scsi_reset(SD_ADDRESS(un), 22811 RESET_LUN); 22812 } 22813 if (reset_retval == 0) { 22814 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 22815 } 22816 (void) sd_send_polled_RQS(un); 22817 22818 } else { 22819 SD_INFO(SD_LOG_DUMP, un, 22820 "sddump: write failed with 0x%x, try # %d\n", 22821 SD_GET_PKT_STATUS(wr_pktp), i); 22822 mutex_enter(SD_MUTEX(un)); 22823 sd_reset_target(un, wr_pktp); 22824 mutex_exit(SD_MUTEX(un)); 22825 } 22826 22827 /* 22828 * If we are not getting anywhere with lun/target resets, 22829 * let's reset the bus. 22830 */ 22831 if (i == SD_NDUMP_RETRIES/2) { 22832 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 22833 (void) sd_send_polled_RQS(un); 22834 } 22835 22836 } 22837 #if defined(__i386) || defined(__amd64) 22838 } /* dma_resid */ 22839 #endif 22840 22841 scsi_destroy_pkt(wr_pktp); 22842 mutex_enter(SD_MUTEX(un)); 22843 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 22844 mutex_exit(SD_MUTEX(un)); 22845 scsi_free_consistent_buf(wr_bp); 22846 } else { 22847 mutex_exit(SD_MUTEX(un)); 22848 } 22849 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 22850 return (err); 22851 } 22852 22853 /* 22854 * Function: sd_scsi_poll() 22855 * 22856 * Description: This is a wrapper for the scsi_poll call. 22857 * 22858 * Arguments: sd_lun - The unit structure 22859 * scsi_pkt - The scsi packet being sent to the device. 22860 * 22861 * Return Code: 0 - Command completed successfully with good status 22862 * -1 - Command failed. This could indicate a check condition 22863 * or other status value requiring recovery action. 22864 * 22865 */ 22866 22867 static int 22868 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 22869 { 22870 int status; 22871 22872 ASSERT(un != NULL); 22873 ASSERT(!mutex_owned(SD_MUTEX(un))); 22874 ASSERT(pktp != NULL); 22875 22876 status = SD_SUCCESS; 22877 22878 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 22879 pktp->pkt_flags |= un->un_tagflags; 22880 pktp->pkt_flags &= ~FLAG_NODISCON; 22881 } 22882 22883 status = sd_ddi_scsi_poll(pktp); 22884 /* 22885 * Scsi_poll returns 0 (success) if the command completes and the 22886 * status block is STATUS_GOOD. We should only check errors if this 22887 * condition is not true. Even then we should send our own request 22888 * sense packet only if we have a check condition and auto 22889 * request sense has not been performed by the hba. 22890 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 22891 */ 22892 if ((status != SD_SUCCESS) && 22893 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 22894 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 22895 (pktp->pkt_reason != CMD_DEV_GONE)) 22896 (void) sd_send_polled_RQS(un); 22897 22898 return (status); 22899 } 22900 22901 /* 22902 * Function: sd_send_polled_RQS() 22903 * 22904 * Description: This sends the request sense command to a device. 22905 * 22906 * Arguments: sd_lun - The unit structure 22907 * 22908 * Return Code: 0 - Command completed successfully with good status 22909 * -1 - Command failed. 22910 * 22911 */ 22912 22913 static int 22914 sd_send_polled_RQS(struct sd_lun *un) 22915 { 22916 int ret_val; 22917 struct scsi_pkt *rqs_pktp; 22918 struct buf *rqs_bp; 22919 22920 ASSERT(un != NULL); 22921 ASSERT(!mutex_owned(SD_MUTEX(un))); 22922 22923 ret_val = SD_SUCCESS; 22924 22925 rqs_pktp = un->un_rqs_pktp; 22926 rqs_bp = un->un_rqs_bp; 22927 22928 mutex_enter(SD_MUTEX(un)); 22929 22930 if (un->un_sense_isbusy) { 22931 ret_val = SD_FAILURE; 22932 mutex_exit(SD_MUTEX(un)); 22933 return (ret_val); 22934 } 22935 22936 /* 22937 * If the request sense buffer (and packet) is not in use, 22938 * let's set the un_sense_isbusy and send our packet 22939 */ 22940 un->un_sense_isbusy = 1; 22941 rqs_pktp->pkt_resid = 0; 22942 rqs_pktp->pkt_reason = 0; 22943 rqs_pktp->pkt_flags |= FLAG_NOINTR; 22944 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 22945 22946 mutex_exit(SD_MUTEX(un)); 22947 22948 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 22949 " 0x%p\n", rqs_bp->b_un.b_addr); 22950 22951 /* 22952 * Can't send this to sd_scsi_poll, we wrap ourselves around the 22953 * axle - it has a call into us! 22954 */ 22955 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 22956 SD_INFO(SD_LOG_COMMON, un, 22957 "sd_send_polled_RQS: RQS failed\n"); 22958 } 22959 22960 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 22961 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 22962 22963 mutex_enter(SD_MUTEX(un)); 22964 un->un_sense_isbusy = 0; 22965 mutex_exit(SD_MUTEX(un)); 22966 22967 return (ret_val); 22968 } 22969 22970 /* 22971 * Defines needed for localized version of the scsi_poll routine. 22972 */ 22973 #define SD_CSEC 10000 /* usecs */ 22974 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 22975 22976 22977 /* 22978 * Function: sd_ddi_scsi_poll() 22979 * 22980 * Description: Localized version of the scsi_poll routine. The purpose is to 22981 * send a scsi_pkt to a device as a polled command. This version 22982 * is to ensure more robust handling of transport errors. 22983 * Specifically this routine cures not ready, coming ready 22984 * transition for power up and reset of sonoma's. This can take 22985 * up to 45 seconds for power-on and 20 seconds for reset of a 22986 * sonoma lun. 22987 * 22988 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 22989 * 22990 * Return Code: 0 - Command completed successfully with good status 22991 * -1 - Command failed. 22992 * 22993 */ 22994 22995 static int 22996 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 22997 { 22998 int busy_count; 22999 int timeout; 23000 int rval = SD_FAILURE; 23001 int savef; 23002 uint8_t *sensep; 23003 long savet; 23004 void (*savec)(); 23005 /* 23006 * The following is defined in machdep.c and is used in determining if 23007 * the scsi transport system will do polled I/O instead of interrupt 23008 * I/O when called from xx_dump(). 23009 */ 23010 extern int do_polled_io; 23011 23012 /* 23013 * save old flags in pkt, to restore at end 23014 */ 23015 savef = pkt->pkt_flags; 23016 savec = pkt->pkt_comp; 23017 savet = pkt->pkt_time; 23018 23019 pkt->pkt_flags |= FLAG_NOINTR; 23020 23021 /* 23022 * XXX there is nothing in the SCSA spec that states that we should not 23023 * do a callback for polled cmds; however, removing this will break sd 23024 * and probably other target drivers 23025 */ 23026 pkt->pkt_comp = NULL; 23027 23028 /* 23029 * we don't like a polled command without timeout. 23030 * 60 seconds seems long enough. 23031 */ 23032 if (pkt->pkt_time == 0) { 23033 pkt->pkt_time = SCSI_POLL_TIMEOUT; 23034 } 23035 23036 /* 23037 * Send polled cmd. 23038 * 23039 * We do some error recovery for various errors. Tran_busy, 23040 * queue full, and non-dispatched commands are retried every 10 msec. 23041 * as they are typically transient failures. Busy status and Not 23042 * Ready are retried every second as this status takes a while to 23043 * change. Unit attention is retried for pkt_time (60) times 23044 * with no delay. 23045 */ 23046 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 23047 23048 for (busy_count = 0; busy_count < timeout; busy_count++) { 23049 int rc; 23050 int poll_delay; 23051 23052 /* 23053 * Initialize pkt status variables. 23054 */ 23055 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 23056 23057 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 23058 if (rc != TRAN_BUSY) { 23059 /* Transport failed - give up. */ 23060 break; 23061 } else { 23062 /* Transport busy - try again. */ 23063 poll_delay = 1 * SD_CSEC; /* 10 msec */ 23064 } 23065 } else { 23066 /* 23067 * Transport accepted - check pkt status. 23068 */ 23069 rc = (*pkt->pkt_scbp) & STATUS_MASK; 23070 if (pkt->pkt_reason == CMD_CMPLT && 23071 rc == STATUS_CHECK && 23072 pkt->pkt_state & STATE_ARQ_DONE) { 23073 struct scsi_arq_status *arqstat = 23074 (struct scsi_arq_status *)(pkt->pkt_scbp); 23075 23076 sensep = (uint8_t *)&arqstat->sts_sensedata; 23077 } else { 23078 sensep = NULL; 23079 } 23080 23081 if ((pkt->pkt_reason == CMD_CMPLT) && 23082 (rc == STATUS_GOOD)) { 23083 /* No error - we're done */ 23084 rval = SD_SUCCESS; 23085 break; 23086 23087 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 23088 /* Lost connection - give up */ 23089 break; 23090 23091 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 23092 (pkt->pkt_state == 0)) { 23093 /* Pkt not dispatched - try again. */ 23094 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23095 23096 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23097 (rc == STATUS_QFULL)) { 23098 /* Queue full - try again. */ 23099 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23100 23101 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23102 (rc == STATUS_BUSY)) { 23103 /* Busy - try again. */ 23104 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23105 busy_count += (SD_SEC_TO_CSEC - 1); 23106 23107 } else if ((sensep != NULL) && 23108 (scsi_sense_key(sensep) == 23109 KEY_UNIT_ATTENTION)) { 23110 /* Unit Attention - try again */ 23111 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 23112 continue; 23113 23114 } else if ((sensep != NULL) && 23115 (scsi_sense_key(sensep) == KEY_NOT_READY) && 23116 (scsi_sense_asc(sensep) == 0x04) && 23117 (scsi_sense_ascq(sensep) == 0x01)) { 23118 /* Not ready -> ready - try again. */ 23119 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23120 busy_count += (SD_SEC_TO_CSEC - 1); 23121 23122 } else { 23123 /* BAD status - give up. */ 23124 break; 23125 } 23126 } 23127 23128 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 23129 !do_polled_io) { 23130 delay(drv_usectohz(poll_delay)); 23131 } else { 23132 /* we busy wait during cpr_dump or interrupt threads */ 23133 drv_usecwait(poll_delay); 23134 } 23135 } 23136 23137 pkt->pkt_flags = savef; 23138 pkt->pkt_comp = savec; 23139 pkt->pkt_time = savet; 23140 return (rval); 23141 } 23142 23143 23144 /* 23145 * Function: sd_persistent_reservation_in_read_keys 23146 * 23147 * Description: This routine is the driver entry point for handling CD-ROM 23148 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 23149 * by sending the SCSI-3 PRIN commands to the device. 23150 * Processes the read keys command response by copying the 23151 * reservation key information into the user provided buffer. 23152 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 23153 * 23154 * Arguments: un - Pointer to soft state struct for the target. 23155 * usrp - user provided pointer to multihost Persistent In Read 23156 * Keys structure (mhioc_inkeys_t) 23157 * flag - this argument is a pass through to ddi_copyxxx() 23158 * directly from the mode argument of ioctl(). 23159 * 23160 * Return Code: 0 - Success 23161 * EACCES 23162 * ENOTSUP 23163 * errno return code from sd_send_scsi_cmd() 23164 * 23165 * Context: Can sleep. Does not return until command is completed. 23166 */ 23167 23168 static int 23169 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 23170 mhioc_inkeys_t *usrp, int flag) 23171 { 23172 #ifdef _MULTI_DATAMODEL 23173 struct mhioc_key_list32 li32; 23174 #endif 23175 sd_prin_readkeys_t *in; 23176 mhioc_inkeys_t *ptr; 23177 mhioc_key_list_t li; 23178 uchar_t *data_bufp; 23179 int data_len; 23180 int rval; 23181 size_t copysz; 23182 23183 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 23184 return (EINVAL); 23185 } 23186 bzero(&li, sizeof (mhioc_key_list_t)); 23187 23188 /* 23189 * Get the listsize from user 23190 */ 23191 #ifdef _MULTI_DATAMODEL 23192 23193 switch (ddi_model_convert_from(flag & FMODELS)) { 23194 case DDI_MODEL_ILP32: 23195 copysz = sizeof (struct mhioc_key_list32); 23196 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 23197 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23198 "sd_persistent_reservation_in_read_keys: " 23199 "failed ddi_copyin: mhioc_key_list32_t\n"); 23200 rval = EFAULT; 23201 goto done; 23202 } 23203 li.listsize = li32.listsize; 23204 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 23205 break; 23206 23207 case DDI_MODEL_NONE: 23208 copysz = sizeof (mhioc_key_list_t); 23209 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23210 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23211 "sd_persistent_reservation_in_read_keys: " 23212 "failed ddi_copyin: mhioc_key_list_t\n"); 23213 rval = EFAULT; 23214 goto done; 23215 } 23216 break; 23217 } 23218 23219 #else /* ! _MULTI_DATAMODEL */ 23220 copysz = sizeof (mhioc_key_list_t); 23221 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23222 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23223 "sd_persistent_reservation_in_read_keys: " 23224 "failed ddi_copyin: mhioc_key_list_t\n"); 23225 rval = EFAULT; 23226 goto done; 23227 } 23228 #endif 23229 23230 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 23231 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 23232 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23233 23234 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 23235 data_len, data_bufp)) != 0) { 23236 goto done; 23237 } 23238 in = (sd_prin_readkeys_t *)data_bufp; 23239 ptr->generation = BE_32(in->generation); 23240 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 23241 23242 /* 23243 * Return the min(listsize, listlen) keys 23244 */ 23245 #ifdef _MULTI_DATAMODEL 23246 23247 switch (ddi_model_convert_from(flag & FMODELS)) { 23248 case DDI_MODEL_ILP32: 23249 li32.listlen = li.listlen; 23250 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 23251 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23252 "sd_persistent_reservation_in_read_keys: " 23253 "failed ddi_copyout: mhioc_key_list32_t\n"); 23254 rval = EFAULT; 23255 goto done; 23256 } 23257 break; 23258 23259 case DDI_MODEL_NONE: 23260 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23261 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23262 "sd_persistent_reservation_in_read_keys: " 23263 "failed ddi_copyout: mhioc_key_list_t\n"); 23264 rval = EFAULT; 23265 goto done; 23266 } 23267 break; 23268 } 23269 23270 #else /* ! _MULTI_DATAMODEL */ 23271 23272 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23273 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23274 "sd_persistent_reservation_in_read_keys: " 23275 "failed ddi_copyout: mhioc_key_list_t\n"); 23276 rval = EFAULT; 23277 goto done; 23278 } 23279 23280 #endif /* _MULTI_DATAMODEL */ 23281 23282 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 23283 li.listsize * MHIOC_RESV_KEY_SIZE); 23284 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 23285 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23286 "sd_persistent_reservation_in_read_keys: " 23287 "failed ddi_copyout: keylist\n"); 23288 rval = EFAULT; 23289 } 23290 done: 23291 kmem_free(data_bufp, data_len); 23292 return (rval); 23293 } 23294 23295 23296 /* 23297 * Function: sd_persistent_reservation_in_read_resv 23298 * 23299 * Description: This routine is the driver entry point for handling CD-ROM 23300 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 23301 * by sending the SCSI-3 PRIN commands to the device. 23302 * Process the read persistent reservations command response by 23303 * copying the reservation information into the user provided 23304 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 23305 * 23306 * Arguments: un - Pointer to soft state struct for the target. 23307 * usrp - user provided pointer to multihost Persistent In Read 23308 * Keys structure (mhioc_inkeys_t) 23309 * flag - this argument is a pass through to ddi_copyxxx() 23310 * directly from the mode argument of ioctl(). 23311 * 23312 * Return Code: 0 - Success 23313 * EACCES 23314 * ENOTSUP 23315 * errno return code from sd_send_scsi_cmd() 23316 * 23317 * Context: Can sleep. Does not return until command is completed. 23318 */ 23319 23320 static int 23321 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 23322 mhioc_inresvs_t *usrp, int flag) 23323 { 23324 #ifdef _MULTI_DATAMODEL 23325 struct mhioc_resv_desc_list32 resvlist32; 23326 #endif 23327 sd_prin_readresv_t *in; 23328 mhioc_inresvs_t *ptr; 23329 sd_readresv_desc_t *readresv_ptr; 23330 mhioc_resv_desc_list_t resvlist; 23331 mhioc_resv_desc_t resvdesc; 23332 uchar_t *data_bufp; 23333 int data_len; 23334 int rval; 23335 int i; 23336 size_t copysz; 23337 mhioc_resv_desc_t *bufp; 23338 23339 if ((ptr = usrp) == NULL) { 23340 return (EINVAL); 23341 } 23342 23343 /* 23344 * Get the listsize from user 23345 */ 23346 #ifdef _MULTI_DATAMODEL 23347 switch (ddi_model_convert_from(flag & FMODELS)) { 23348 case DDI_MODEL_ILP32: 23349 copysz = sizeof (struct mhioc_resv_desc_list32); 23350 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 23351 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23352 "sd_persistent_reservation_in_read_resv: " 23353 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23354 rval = EFAULT; 23355 goto done; 23356 } 23357 resvlist.listsize = resvlist32.listsize; 23358 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 23359 break; 23360 23361 case DDI_MODEL_NONE: 23362 copysz = sizeof (mhioc_resv_desc_list_t); 23363 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23364 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23365 "sd_persistent_reservation_in_read_resv: " 23366 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23367 rval = EFAULT; 23368 goto done; 23369 } 23370 break; 23371 } 23372 #else /* ! _MULTI_DATAMODEL */ 23373 copysz = sizeof (mhioc_resv_desc_list_t); 23374 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23375 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23376 "sd_persistent_reservation_in_read_resv: " 23377 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23378 rval = EFAULT; 23379 goto done; 23380 } 23381 #endif /* ! _MULTI_DATAMODEL */ 23382 23383 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 23384 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 23385 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23386 23387 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 23388 data_len, data_bufp)) != 0) { 23389 goto done; 23390 } 23391 in = (sd_prin_readresv_t *)data_bufp; 23392 ptr->generation = BE_32(in->generation); 23393 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 23394 23395 /* 23396 * Return the min(listsize, listlen( keys 23397 */ 23398 #ifdef _MULTI_DATAMODEL 23399 23400 switch (ddi_model_convert_from(flag & FMODELS)) { 23401 case DDI_MODEL_ILP32: 23402 resvlist32.listlen = resvlist.listlen; 23403 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 23404 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23405 "sd_persistent_reservation_in_read_resv: " 23406 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23407 rval = EFAULT; 23408 goto done; 23409 } 23410 break; 23411 23412 case DDI_MODEL_NONE: 23413 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23414 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23415 "sd_persistent_reservation_in_read_resv: " 23416 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23417 rval = EFAULT; 23418 goto done; 23419 } 23420 break; 23421 } 23422 23423 #else /* ! _MULTI_DATAMODEL */ 23424 23425 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23426 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23427 "sd_persistent_reservation_in_read_resv: " 23428 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23429 rval = EFAULT; 23430 goto done; 23431 } 23432 23433 #endif /* ! _MULTI_DATAMODEL */ 23434 23435 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 23436 bufp = resvlist.list; 23437 copysz = sizeof (mhioc_resv_desc_t); 23438 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 23439 i++, readresv_ptr++, bufp++) { 23440 23441 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 23442 MHIOC_RESV_KEY_SIZE); 23443 resvdesc.type = readresv_ptr->type; 23444 resvdesc.scope = readresv_ptr->scope; 23445 resvdesc.scope_specific_addr = 23446 BE_32(readresv_ptr->scope_specific_addr); 23447 23448 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 23449 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23450 "sd_persistent_reservation_in_read_resv: " 23451 "failed ddi_copyout: resvlist\n"); 23452 rval = EFAULT; 23453 goto done; 23454 } 23455 } 23456 done: 23457 kmem_free(data_bufp, data_len); 23458 return (rval); 23459 } 23460 23461 23462 /* 23463 * Function: sr_change_blkmode() 23464 * 23465 * Description: This routine is the driver entry point for handling CD-ROM 23466 * block mode ioctl requests. Support for returning and changing 23467 * the current block size in use by the device is implemented. The 23468 * LBA size is changed via a MODE SELECT Block Descriptor. 23469 * 23470 * This routine issues a mode sense with an allocation length of 23471 * 12 bytes for the mode page header and a single block descriptor. 23472 * 23473 * Arguments: dev - the device 'dev_t' 23474 * cmd - the request type; one of CDROMGBLKMODE (get) or 23475 * CDROMSBLKMODE (set) 23476 * data - current block size or requested block size 23477 * flag - this argument is a pass through to ddi_copyxxx() directly 23478 * from the mode argument of ioctl(). 23479 * 23480 * Return Code: the code returned by sd_send_scsi_cmd() 23481 * EINVAL if invalid arguments are provided 23482 * EFAULT if ddi_copyxxx() fails 23483 * ENXIO if fail ddi_get_soft_state 23484 * EIO if invalid mode sense block descriptor length 23485 * 23486 */ 23487 23488 static int 23489 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 23490 { 23491 struct sd_lun *un = NULL; 23492 struct mode_header *sense_mhp, *select_mhp; 23493 struct block_descriptor *sense_desc, *select_desc; 23494 int current_bsize; 23495 int rval = EINVAL; 23496 uchar_t *sense = NULL; 23497 uchar_t *select = NULL; 23498 23499 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 23500 23501 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23502 return (ENXIO); 23503 } 23504 23505 /* 23506 * The block length is changed via the Mode Select block descriptor, the 23507 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 23508 * required as part of this routine. Therefore the mode sense allocation 23509 * length is specified to be the length of a mode page header and a 23510 * block descriptor. 23511 */ 23512 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23513 23514 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 23515 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 23516 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23517 "sr_change_blkmode: Mode Sense Failed\n"); 23518 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23519 return (rval); 23520 } 23521 23522 /* Check the block descriptor len to handle only 1 block descriptor */ 23523 sense_mhp = (struct mode_header *)sense; 23524 if ((sense_mhp->bdesc_length == 0) || 23525 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 23526 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23527 "sr_change_blkmode: Mode Sense returned invalid block" 23528 " descriptor length\n"); 23529 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23530 return (EIO); 23531 } 23532 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 23533 current_bsize = ((sense_desc->blksize_hi << 16) | 23534 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 23535 23536 /* Process command */ 23537 switch (cmd) { 23538 case CDROMGBLKMODE: 23539 /* Return the block size obtained during the mode sense */ 23540 if (ddi_copyout(¤t_bsize, (void *)data, 23541 sizeof (int), flag) != 0) 23542 rval = EFAULT; 23543 break; 23544 case CDROMSBLKMODE: 23545 /* Validate the requested block size */ 23546 switch (data) { 23547 case CDROM_BLK_512: 23548 case CDROM_BLK_1024: 23549 case CDROM_BLK_2048: 23550 case CDROM_BLK_2056: 23551 case CDROM_BLK_2336: 23552 case CDROM_BLK_2340: 23553 case CDROM_BLK_2352: 23554 case CDROM_BLK_2368: 23555 case CDROM_BLK_2448: 23556 case CDROM_BLK_2646: 23557 case CDROM_BLK_2647: 23558 break; 23559 default: 23560 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23561 "sr_change_blkmode: " 23562 "Block Size '%ld' Not Supported\n", data); 23563 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23564 return (EINVAL); 23565 } 23566 23567 /* 23568 * The current block size matches the requested block size so 23569 * there is no need to send the mode select to change the size 23570 */ 23571 if (current_bsize == data) { 23572 break; 23573 } 23574 23575 /* Build the select data for the requested block size */ 23576 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23577 select_mhp = (struct mode_header *)select; 23578 select_desc = 23579 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 23580 /* 23581 * The LBA size is changed via the block descriptor, so the 23582 * descriptor is built according to the user data 23583 */ 23584 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 23585 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 23586 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 23587 select_desc->blksize_lo = (char)((data) & 0x000000ff); 23588 23589 /* Send the mode select for the requested block size */ 23590 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23591 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23592 SD_PATH_STANDARD)) != 0) { 23593 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23594 "sr_change_blkmode: Mode Select Failed\n"); 23595 /* 23596 * The mode select failed for the requested block size, 23597 * so reset the data for the original block size and 23598 * send it to the target. The error is indicated by the 23599 * return value for the failed mode select. 23600 */ 23601 select_desc->blksize_hi = sense_desc->blksize_hi; 23602 select_desc->blksize_mid = sense_desc->blksize_mid; 23603 select_desc->blksize_lo = sense_desc->blksize_lo; 23604 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23605 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23606 SD_PATH_STANDARD); 23607 } else { 23608 ASSERT(!mutex_owned(SD_MUTEX(un))); 23609 mutex_enter(SD_MUTEX(un)); 23610 sd_update_block_info(un, (uint32_t)data, 0); 23611 mutex_exit(SD_MUTEX(un)); 23612 } 23613 break; 23614 default: 23615 /* should not reach here, but check anyway */ 23616 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23617 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 23618 rval = EINVAL; 23619 break; 23620 } 23621 23622 if (select) { 23623 kmem_free(select, BUFLEN_CHG_BLK_MODE); 23624 } 23625 if (sense) { 23626 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23627 } 23628 return (rval); 23629 } 23630 23631 23632 /* 23633 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 23634 * implement driver support for getting and setting the CD speed. The command 23635 * set used will be based on the device type. If the device has not been 23636 * identified as MMC the Toshiba vendor specific mode page will be used. If 23637 * the device is MMC but does not support the Real Time Streaming feature 23638 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 23639 * be used to read the speed. 23640 */ 23641 23642 /* 23643 * Function: sr_change_speed() 23644 * 23645 * Description: This routine is the driver entry point for handling CD-ROM 23646 * drive speed ioctl requests for devices supporting the Toshiba 23647 * vendor specific drive speed mode page. Support for returning 23648 * and changing the current drive speed in use by the device is 23649 * implemented. 23650 * 23651 * Arguments: dev - the device 'dev_t' 23652 * cmd - the request type; one of CDROMGDRVSPEED (get) or 23653 * CDROMSDRVSPEED (set) 23654 * data - current drive speed or requested drive speed 23655 * flag - this argument is a pass through to ddi_copyxxx() directly 23656 * from the mode argument of ioctl(). 23657 * 23658 * Return Code: the code returned by sd_send_scsi_cmd() 23659 * EINVAL if invalid arguments are provided 23660 * EFAULT if ddi_copyxxx() fails 23661 * ENXIO if fail ddi_get_soft_state 23662 * EIO if invalid mode sense block descriptor length 23663 */ 23664 23665 static int 23666 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 23667 { 23668 struct sd_lun *un = NULL; 23669 struct mode_header *sense_mhp, *select_mhp; 23670 struct mode_speed *sense_page, *select_page; 23671 int current_speed; 23672 int rval = EINVAL; 23673 int bd_len; 23674 uchar_t *sense = NULL; 23675 uchar_t *select = NULL; 23676 23677 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 23678 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23679 return (ENXIO); 23680 } 23681 23682 /* 23683 * Note: The drive speed is being modified here according to a Toshiba 23684 * vendor specific mode page (0x31). 23685 */ 23686 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 23687 23688 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 23689 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 23690 SD_PATH_STANDARD)) != 0) { 23691 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23692 "sr_change_speed: Mode Sense Failed\n"); 23693 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 23694 return (rval); 23695 } 23696 sense_mhp = (struct mode_header *)sense; 23697 23698 /* Check the block descriptor len to handle only 1 block descriptor */ 23699 bd_len = sense_mhp->bdesc_length; 23700 if (bd_len > MODE_BLK_DESC_LENGTH) { 23701 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23702 "sr_change_speed: Mode Sense returned invalid block " 23703 "descriptor length\n"); 23704 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 23705 return (EIO); 23706 } 23707 23708 sense_page = (struct mode_speed *) 23709 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 23710 current_speed = sense_page->speed; 23711 23712 /* Process command */ 23713 switch (cmd) { 23714 case CDROMGDRVSPEED: 23715 /* Return the drive speed obtained during the mode sense */ 23716 if (current_speed == 0x2) { 23717 current_speed = CDROM_TWELVE_SPEED; 23718 } 23719 if (ddi_copyout(¤t_speed, (void *)data, 23720 sizeof (int), flag) != 0) { 23721 rval = EFAULT; 23722 } 23723 break; 23724 case CDROMSDRVSPEED: 23725 /* Validate the requested drive speed */ 23726 switch ((uchar_t)data) { 23727 case CDROM_TWELVE_SPEED: 23728 data = 0x2; 23729 /*FALLTHROUGH*/ 23730 case CDROM_NORMAL_SPEED: 23731 case CDROM_DOUBLE_SPEED: 23732 case CDROM_QUAD_SPEED: 23733 case CDROM_MAXIMUM_SPEED: 23734 break; 23735 default: 23736 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23737 "sr_change_speed: " 23738 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 23739 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 23740 return (EINVAL); 23741 } 23742 23743 /* 23744 * The current drive speed matches the requested drive speed so 23745 * there is no need to send the mode select to change the speed 23746 */ 23747 if (current_speed == data) { 23748 break; 23749 } 23750 23751 /* Build the select data for the requested drive speed */ 23752 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 23753 select_mhp = (struct mode_header *)select; 23754 select_mhp->bdesc_length = 0; 23755 select_page = 23756 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 23757 select_page = 23758 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 23759 select_page->mode_page.code = CDROM_MODE_SPEED; 23760 select_page->mode_page.length = 2; 23761 select_page->speed = (uchar_t)data; 23762 23763 /* Send the mode select for the requested block size */ 23764 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 23765 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 23766 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 23767 /* 23768 * The mode select failed for the requested drive speed, 23769 * so reset the data for the original drive speed and 23770 * send it to the target. The error is indicated by the 23771 * return value for the failed mode select. 23772 */ 23773 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23774 "sr_drive_speed: Mode Select Failed\n"); 23775 select_page->speed = sense_page->speed; 23776 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 23777 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 23778 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 23779 } 23780 break; 23781 default: 23782 /* should not reach here, but check anyway */ 23783 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23784 "sr_change_speed: Command '%x' Not Supported\n", cmd); 23785 rval = EINVAL; 23786 break; 23787 } 23788 23789 if (select) { 23790 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 23791 } 23792 if (sense) { 23793 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 23794 } 23795 23796 return (rval); 23797 } 23798 23799 23800 /* 23801 * Function: sr_atapi_change_speed() 23802 * 23803 * Description: This routine is the driver entry point for handling CD-ROM 23804 * drive speed ioctl requests for MMC devices that do not support 23805 * the Real Time Streaming feature (0x107). 23806 * 23807 * Note: This routine will use the SET SPEED command which may not 23808 * be supported by all devices. 23809 * 23810 * Arguments: dev- the device 'dev_t' 23811 * cmd- the request type; one of CDROMGDRVSPEED (get) or 23812 * CDROMSDRVSPEED (set) 23813 * data- current drive speed or requested drive speed 23814 * flag- this argument is a pass through to ddi_copyxxx() directly 23815 * from the mode argument of ioctl(). 23816 * 23817 * Return Code: the code returned by sd_send_scsi_cmd() 23818 * EINVAL if invalid arguments are provided 23819 * EFAULT if ddi_copyxxx() fails 23820 * ENXIO if fail ddi_get_soft_state 23821 * EIO if invalid mode sense block descriptor length 23822 */ 23823 23824 static int 23825 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 23826 { 23827 struct sd_lun *un; 23828 struct uscsi_cmd *com = NULL; 23829 struct mode_header_grp2 *sense_mhp; 23830 uchar_t *sense_page; 23831 uchar_t *sense = NULL; 23832 char cdb[CDB_GROUP5]; 23833 int bd_len; 23834 int current_speed = 0; 23835 int max_speed = 0; 23836 int rval; 23837 23838 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 23839 23840 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23841 return (ENXIO); 23842 } 23843 23844 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 23845 23846 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 23847 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 23848 SD_PATH_STANDARD)) != 0) { 23849 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23850 "sr_atapi_change_speed: Mode Sense Failed\n"); 23851 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 23852 return (rval); 23853 } 23854 23855 /* Check the block descriptor len to handle only 1 block descriptor */ 23856 sense_mhp = (struct mode_header_grp2 *)sense; 23857 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 23858 if (bd_len > MODE_BLK_DESC_LENGTH) { 23859 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23860 "sr_atapi_change_speed: Mode Sense returned invalid " 23861 "block descriptor length\n"); 23862 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 23863 return (EIO); 23864 } 23865 23866 /* Calculate the current and maximum drive speeds */ 23867 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 23868 current_speed = (sense_page[14] << 8) | sense_page[15]; 23869 max_speed = (sense_page[8] << 8) | sense_page[9]; 23870 23871 /* Process the command */ 23872 switch (cmd) { 23873 case CDROMGDRVSPEED: 23874 current_speed /= SD_SPEED_1X; 23875 if (ddi_copyout(¤t_speed, (void *)data, 23876 sizeof (int), flag) != 0) 23877 rval = EFAULT; 23878 break; 23879 case CDROMSDRVSPEED: 23880 /* Convert the speed code to KB/sec */ 23881 switch ((uchar_t)data) { 23882 case CDROM_NORMAL_SPEED: 23883 current_speed = SD_SPEED_1X; 23884 break; 23885 case CDROM_DOUBLE_SPEED: 23886 current_speed = 2 * SD_SPEED_1X; 23887 break; 23888 case CDROM_QUAD_SPEED: 23889 current_speed = 4 * SD_SPEED_1X; 23890 break; 23891 case CDROM_TWELVE_SPEED: 23892 current_speed = 12 * SD_SPEED_1X; 23893 break; 23894 case CDROM_MAXIMUM_SPEED: 23895 current_speed = 0xffff; 23896 break; 23897 default: 23898 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23899 "sr_atapi_change_speed: invalid drive speed %d\n", 23900 (uchar_t)data); 23901 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 23902 return (EINVAL); 23903 } 23904 23905 /* Check the request against the drive's max speed. */ 23906 if (current_speed != 0xffff) { 23907 if (current_speed > max_speed) { 23908 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 23909 return (EINVAL); 23910 } 23911 } 23912 23913 /* 23914 * Build and send the SET SPEED command 23915 * 23916 * Note: The SET SPEED (0xBB) command used in this routine is 23917 * obsolete per the SCSI MMC spec but still supported in the 23918 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 23919 * therefore the command is still implemented in this routine. 23920 */ 23921 bzero(cdb, sizeof (cdb)); 23922 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 23923 cdb[2] = (uchar_t)(current_speed >> 8); 23924 cdb[3] = (uchar_t)current_speed; 23925 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 23926 com->uscsi_cdb = (caddr_t)cdb; 23927 com->uscsi_cdblen = CDB_GROUP5; 23928 com->uscsi_bufaddr = NULL; 23929 com->uscsi_buflen = 0; 23930 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 23931 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 23932 break; 23933 default: 23934 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23935 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 23936 rval = EINVAL; 23937 } 23938 23939 if (sense) { 23940 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 23941 } 23942 if (com) { 23943 kmem_free(com, sizeof (*com)); 23944 } 23945 return (rval); 23946 } 23947 23948 23949 /* 23950 * Function: sr_pause_resume() 23951 * 23952 * Description: This routine is the driver entry point for handling CD-ROM 23953 * pause/resume ioctl requests. This only affects the audio play 23954 * operation. 23955 * 23956 * Arguments: dev - the device 'dev_t' 23957 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 23958 * for setting the resume bit of the cdb. 23959 * 23960 * Return Code: the code returned by sd_send_scsi_cmd() 23961 * EINVAL if invalid mode specified 23962 * 23963 */ 23964 23965 static int 23966 sr_pause_resume(dev_t dev, int cmd) 23967 { 23968 struct sd_lun *un; 23969 struct uscsi_cmd *com; 23970 char cdb[CDB_GROUP1]; 23971 int rval; 23972 23973 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23974 return (ENXIO); 23975 } 23976 23977 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 23978 bzero(cdb, CDB_GROUP1); 23979 cdb[0] = SCMD_PAUSE_RESUME; 23980 switch (cmd) { 23981 case CDROMRESUME: 23982 cdb[8] = 1; 23983 break; 23984 case CDROMPAUSE: 23985 cdb[8] = 0; 23986 break; 23987 default: 23988 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 23989 " Command '%x' Not Supported\n", cmd); 23990 rval = EINVAL; 23991 goto done; 23992 } 23993 23994 com->uscsi_cdb = cdb; 23995 com->uscsi_cdblen = CDB_GROUP1; 23996 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 23997 23998 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 23999 SD_PATH_STANDARD); 24000 24001 done: 24002 kmem_free(com, sizeof (*com)); 24003 return (rval); 24004 } 24005 24006 24007 /* 24008 * Function: sr_play_msf() 24009 * 24010 * Description: This routine is the driver entry point for handling CD-ROM 24011 * ioctl requests to output the audio signals at the specified 24012 * starting address and continue the audio play until the specified 24013 * ending address (CDROMPLAYMSF) The address is in Minute Second 24014 * Frame (MSF) format. 24015 * 24016 * Arguments: dev - the device 'dev_t' 24017 * data - pointer to user provided audio msf structure, 24018 * specifying start/end addresses. 24019 * flag - this argument is a pass through to ddi_copyxxx() 24020 * directly from the mode argument of ioctl(). 24021 * 24022 * Return Code: the code returned by sd_send_scsi_cmd() 24023 * EFAULT if ddi_copyxxx() fails 24024 * ENXIO if fail ddi_get_soft_state 24025 * EINVAL if data pointer is NULL 24026 */ 24027 24028 static int 24029 sr_play_msf(dev_t dev, caddr_t data, int flag) 24030 { 24031 struct sd_lun *un; 24032 struct uscsi_cmd *com; 24033 struct cdrom_msf msf_struct; 24034 struct cdrom_msf *msf = &msf_struct; 24035 char cdb[CDB_GROUP1]; 24036 int rval; 24037 24038 if (data == NULL) { 24039 return (EINVAL); 24040 } 24041 24042 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24043 return (ENXIO); 24044 } 24045 24046 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 24047 return (EFAULT); 24048 } 24049 24050 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24051 bzero(cdb, CDB_GROUP1); 24052 cdb[0] = SCMD_PLAYAUDIO_MSF; 24053 if (un->un_f_cfg_playmsf_bcd == TRUE) { 24054 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 24055 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 24056 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 24057 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 24058 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 24059 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 24060 } else { 24061 cdb[3] = msf->cdmsf_min0; 24062 cdb[4] = msf->cdmsf_sec0; 24063 cdb[5] = msf->cdmsf_frame0; 24064 cdb[6] = msf->cdmsf_min1; 24065 cdb[7] = msf->cdmsf_sec1; 24066 cdb[8] = msf->cdmsf_frame1; 24067 } 24068 com->uscsi_cdb = cdb; 24069 com->uscsi_cdblen = CDB_GROUP1; 24070 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24071 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24072 SD_PATH_STANDARD); 24073 kmem_free(com, sizeof (*com)); 24074 return (rval); 24075 } 24076 24077 24078 /* 24079 * Function: sr_play_trkind() 24080 * 24081 * Description: This routine is the driver entry point for handling CD-ROM 24082 * ioctl requests to output the audio signals at the specified 24083 * starting address and continue the audio play until the specified 24084 * ending address (CDROMPLAYTRKIND). The address is in Track Index 24085 * format. 24086 * 24087 * Arguments: dev - the device 'dev_t' 24088 * data - pointer to user provided audio track/index structure, 24089 * specifying start/end addresses. 24090 * flag - this argument is a pass through to ddi_copyxxx() 24091 * directly from the mode argument of ioctl(). 24092 * 24093 * Return Code: the code returned by sd_send_scsi_cmd() 24094 * EFAULT if ddi_copyxxx() fails 24095 * ENXIO if fail ddi_get_soft_state 24096 * EINVAL if data pointer is NULL 24097 */ 24098 24099 static int 24100 sr_play_trkind(dev_t dev, caddr_t data, int flag) 24101 { 24102 struct cdrom_ti ti_struct; 24103 struct cdrom_ti *ti = &ti_struct; 24104 struct uscsi_cmd *com = NULL; 24105 char cdb[CDB_GROUP1]; 24106 int rval; 24107 24108 if (data == NULL) { 24109 return (EINVAL); 24110 } 24111 24112 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 24113 return (EFAULT); 24114 } 24115 24116 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24117 bzero(cdb, CDB_GROUP1); 24118 cdb[0] = SCMD_PLAYAUDIO_TI; 24119 cdb[4] = ti->cdti_trk0; 24120 cdb[5] = ti->cdti_ind0; 24121 cdb[7] = ti->cdti_trk1; 24122 cdb[8] = ti->cdti_ind1; 24123 com->uscsi_cdb = cdb; 24124 com->uscsi_cdblen = CDB_GROUP1; 24125 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24126 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24127 SD_PATH_STANDARD); 24128 kmem_free(com, sizeof (*com)); 24129 return (rval); 24130 } 24131 24132 24133 /* 24134 * Function: sr_read_all_subcodes() 24135 * 24136 * Description: This routine is the driver entry point for handling CD-ROM 24137 * ioctl requests to return raw subcode data while the target is 24138 * playing audio (CDROMSUBCODE). 24139 * 24140 * Arguments: dev - the device 'dev_t' 24141 * data - pointer to user provided cdrom subcode structure, 24142 * specifying the transfer length and address. 24143 * flag - this argument is a pass through to ddi_copyxxx() 24144 * directly from the mode argument of ioctl(). 24145 * 24146 * Return Code: the code returned by sd_send_scsi_cmd() 24147 * EFAULT if ddi_copyxxx() fails 24148 * ENXIO if fail ddi_get_soft_state 24149 * EINVAL if data pointer is NULL 24150 */ 24151 24152 static int 24153 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 24154 { 24155 struct sd_lun *un = NULL; 24156 struct uscsi_cmd *com = NULL; 24157 struct cdrom_subcode *subcode = NULL; 24158 int rval; 24159 size_t buflen; 24160 char cdb[CDB_GROUP5]; 24161 24162 #ifdef _MULTI_DATAMODEL 24163 /* To support ILP32 applications in an LP64 world */ 24164 struct cdrom_subcode32 cdrom_subcode32; 24165 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 24166 #endif 24167 if (data == NULL) { 24168 return (EINVAL); 24169 } 24170 24171 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24172 return (ENXIO); 24173 } 24174 24175 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 24176 24177 #ifdef _MULTI_DATAMODEL 24178 switch (ddi_model_convert_from(flag & FMODELS)) { 24179 case DDI_MODEL_ILP32: 24180 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 24181 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24182 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24183 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24184 return (EFAULT); 24185 } 24186 /* Convert the ILP32 uscsi data from the application to LP64 */ 24187 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 24188 break; 24189 case DDI_MODEL_NONE: 24190 if (ddi_copyin(data, subcode, 24191 sizeof (struct cdrom_subcode), flag)) { 24192 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24193 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24194 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24195 return (EFAULT); 24196 } 24197 break; 24198 } 24199 #else /* ! _MULTI_DATAMODEL */ 24200 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 24201 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24202 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24203 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24204 return (EFAULT); 24205 } 24206 #endif /* _MULTI_DATAMODEL */ 24207 24208 /* 24209 * Since MMC-2 expects max 3 bytes for length, check if the 24210 * length input is greater than 3 bytes 24211 */ 24212 if ((subcode->cdsc_length & 0xFF000000) != 0) { 24213 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24214 "sr_read_all_subcodes: " 24215 "cdrom transfer length too large: %d (limit %d)\n", 24216 subcode->cdsc_length, 0xFFFFFF); 24217 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24218 return (EINVAL); 24219 } 24220 24221 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 24222 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24223 bzero(cdb, CDB_GROUP5); 24224 24225 if (un->un_f_mmc_cap == TRUE) { 24226 cdb[0] = (char)SCMD_READ_CD; 24227 cdb[2] = (char)0xff; 24228 cdb[3] = (char)0xff; 24229 cdb[4] = (char)0xff; 24230 cdb[5] = (char)0xff; 24231 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24232 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24233 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 24234 cdb[10] = 1; 24235 } else { 24236 /* 24237 * Note: A vendor specific command (0xDF) is being used her to 24238 * request a read of all subcodes. 24239 */ 24240 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 24241 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 24242 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24243 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24244 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 24245 } 24246 com->uscsi_cdb = cdb; 24247 com->uscsi_cdblen = CDB_GROUP5; 24248 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 24249 com->uscsi_buflen = buflen; 24250 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24251 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24252 SD_PATH_STANDARD); 24253 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24254 kmem_free(com, sizeof (*com)); 24255 return (rval); 24256 } 24257 24258 24259 /* 24260 * Function: sr_read_subchannel() 24261 * 24262 * Description: This routine is the driver entry point for handling CD-ROM 24263 * ioctl requests to return the Q sub-channel data of the CD 24264 * current position block. (CDROMSUBCHNL) The data includes the 24265 * track number, index number, absolute CD-ROM address (LBA or MSF 24266 * format per the user) , track relative CD-ROM address (LBA or MSF 24267 * format per the user), control data and audio status. 24268 * 24269 * Arguments: dev - the device 'dev_t' 24270 * data - pointer to user provided cdrom sub-channel structure 24271 * flag - this argument is a pass through to ddi_copyxxx() 24272 * directly from the mode argument of ioctl(). 24273 * 24274 * Return Code: the code returned by sd_send_scsi_cmd() 24275 * EFAULT if ddi_copyxxx() fails 24276 * ENXIO if fail ddi_get_soft_state 24277 * EINVAL if data pointer is NULL 24278 */ 24279 24280 static int 24281 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 24282 { 24283 struct sd_lun *un; 24284 struct uscsi_cmd *com; 24285 struct cdrom_subchnl subchanel; 24286 struct cdrom_subchnl *subchnl = &subchanel; 24287 char cdb[CDB_GROUP1]; 24288 caddr_t buffer; 24289 int rval; 24290 24291 if (data == NULL) { 24292 return (EINVAL); 24293 } 24294 24295 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24296 (un->un_state == SD_STATE_OFFLINE)) { 24297 return (ENXIO); 24298 } 24299 24300 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 24301 return (EFAULT); 24302 } 24303 24304 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 24305 bzero(cdb, CDB_GROUP1); 24306 cdb[0] = SCMD_READ_SUBCHANNEL; 24307 /* Set the MSF bit based on the user requested address format */ 24308 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 24309 /* 24310 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 24311 * returned 24312 */ 24313 cdb[2] = 0x40; 24314 /* 24315 * Set byte 3 to specify the return data format. A value of 0x01 24316 * indicates that the CD-ROM current position should be returned. 24317 */ 24318 cdb[3] = 0x01; 24319 cdb[8] = 0x10; 24320 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24321 com->uscsi_cdb = cdb; 24322 com->uscsi_cdblen = CDB_GROUP1; 24323 com->uscsi_bufaddr = buffer; 24324 com->uscsi_buflen = 16; 24325 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24326 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24327 SD_PATH_STANDARD); 24328 if (rval != 0) { 24329 kmem_free(buffer, 16); 24330 kmem_free(com, sizeof (*com)); 24331 return (rval); 24332 } 24333 24334 /* Process the returned Q sub-channel data */ 24335 subchnl->cdsc_audiostatus = buffer[1]; 24336 subchnl->cdsc_adr = (buffer[5] & 0xF0); 24337 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 24338 subchnl->cdsc_trk = buffer[6]; 24339 subchnl->cdsc_ind = buffer[7]; 24340 if (subchnl->cdsc_format & CDROM_LBA) { 24341 subchnl->cdsc_absaddr.lba = 24342 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24343 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24344 subchnl->cdsc_reladdr.lba = 24345 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 24346 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 24347 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 24348 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 24349 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 24350 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 24351 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 24352 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 24353 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 24354 } else { 24355 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 24356 subchnl->cdsc_absaddr.msf.second = buffer[10]; 24357 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 24358 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 24359 subchnl->cdsc_reladdr.msf.second = buffer[14]; 24360 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 24361 } 24362 kmem_free(buffer, 16); 24363 kmem_free(com, sizeof (*com)); 24364 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 24365 != 0) { 24366 return (EFAULT); 24367 } 24368 return (rval); 24369 } 24370 24371 24372 /* 24373 * Function: sr_read_tocentry() 24374 * 24375 * Description: This routine is the driver entry point for handling CD-ROM 24376 * ioctl requests to read from the Table of Contents (TOC) 24377 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 24378 * fields, the starting address (LBA or MSF format per the user) 24379 * and the data mode if the user specified track is a data track. 24380 * 24381 * Note: The READ HEADER (0x44) command used in this routine is 24382 * obsolete per the SCSI MMC spec but still supported in the 24383 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24384 * therefore the command is still implemented in this routine. 24385 * 24386 * Arguments: dev - the device 'dev_t' 24387 * data - pointer to user provided toc entry structure, 24388 * specifying the track # and the address format 24389 * (LBA or MSF). 24390 * flag - this argument is a pass through to ddi_copyxxx() 24391 * directly from the mode argument of ioctl(). 24392 * 24393 * Return Code: the code returned by sd_send_scsi_cmd() 24394 * EFAULT if ddi_copyxxx() fails 24395 * ENXIO if fail ddi_get_soft_state 24396 * EINVAL if data pointer is NULL 24397 */ 24398 24399 static int 24400 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 24401 { 24402 struct sd_lun *un = NULL; 24403 struct uscsi_cmd *com; 24404 struct cdrom_tocentry toc_entry; 24405 struct cdrom_tocentry *entry = &toc_entry; 24406 caddr_t buffer; 24407 int rval; 24408 char cdb[CDB_GROUP1]; 24409 24410 if (data == NULL) { 24411 return (EINVAL); 24412 } 24413 24414 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24415 (un->un_state == SD_STATE_OFFLINE)) { 24416 return (ENXIO); 24417 } 24418 24419 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 24420 return (EFAULT); 24421 } 24422 24423 /* Validate the requested track and address format */ 24424 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 24425 return (EINVAL); 24426 } 24427 24428 if (entry->cdte_track == 0) { 24429 return (EINVAL); 24430 } 24431 24432 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 24433 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24434 bzero(cdb, CDB_GROUP1); 24435 24436 cdb[0] = SCMD_READ_TOC; 24437 /* Set the MSF bit based on the user requested address format */ 24438 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 24439 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24440 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 24441 } else { 24442 cdb[6] = entry->cdte_track; 24443 } 24444 24445 /* 24446 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 24447 * (4 byte TOC response header + 8 byte track descriptor) 24448 */ 24449 cdb[8] = 12; 24450 com->uscsi_cdb = cdb; 24451 com->uscsi_cdblen = CDB_GROUP1; 24452 com->uscsi_bufaddr = buffer; 24453 com->uscsi_buflen = 0x0C; 24454 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 24455 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24456 SD_PATH_STANDARD); 24457 if (rval != 0) { 24458 kmem_free(buffer, 12); 24459 kmem_free(com, sizeof (*com)); 24460 return (rval); 24461 } 24462 24463 /* Process the toc entry */ 24464 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 24465 entry->cdte_ctrl = (buffer[5] & 0x0F); 24466 if (entry->cdte_format & CDROM_LBA) { 24467 entry->cdte_addr.lba = 24468 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24469 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24470 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 24471 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 24472 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 24473 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 24474 /* 24475 * Send a READ TOC command using the LBA address format to get 24476 * the LBA for the track requested so it can be used in the 24477 * READ HEADER request 24478 * 24479 * Note: The MSF bit of the READ HEADER command specifies the 24480 * output format. The block address specified in that command 24481 * must be in LBA format. 24482 */ 24483 cdb[1] = 0; 24484 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24485 SD_PATH_STANDARD); 24486 if (rval != 0) { 24487 kmem_free(buffer, 12); 24488 kmem_free(com, sizeof (*com)); 24489 return (rval); 24490 } 24491 } else { 24492 entry->cdte_addr.msf.minute = buffer[9]; 24493 entry->cdte_addr.msf.second = buffer[10]; 24494 entry->cdte_addr.msf.frame = buffer[11]; 24495 /* 24496 * Send a READ TOC command using the LBA address format to get 24497 * the LBA for the track requested so it can be used in the 24498 * READ HEADER request 24499 * 24500 * Note: The MSF bit of the READ HEADER command specifies the 24501 * output format. The block address specified in that command 24502 * must be in LBA format. 24503 */ 24504 cdb[1] = 0; 24505 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24506 SD_PATH_STANDARD); 24507 if (rval != 0) { 24508 kmem_free(buffer, 12); 24509 kmem_free(com, sizeof (*com)); 24510 return (rval); 24511 } 24512 } 24513 24514 /* 24515 * Build and send the READ HEADER command to determine the data mode of 24516 * the user specified track. 24517 */ 24518 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 24519 (entry->cdte_track != CDROM_LEADOUT)) { 24520 bzero(cdb, CDB_GROUP1); 24521 cdb[0] = SCMD_READ_HEADER; 24522 cdb[2] = buffer[8]; 24523 cdb[3] = buffer[9]; 24524 cdb[4] = buffer[10]; 24525 cdb[5] = buffer[11]; 24526 cdb[8] = 0x08; 24527 com->uscsi_buflen = 0x08; 24528 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24529 SD_PATH_STANDARD); 24530 if (rval == 0) { 24531 entry->cdte_datamode = buffer[0]; 24532 } else { 24533 /* 24534 * READ HEADER command failed, since this is 24535 * obsoleted in one spec, its better to return 24536 * -1 for an invlid track so that we can still 24537 * receive the rest of the TOC data. 24538 */ 24539 entry->cdte_datamode = (uchar_t)-1; 24540 } 24541 } else { 24542 entry->cdte_datamode = (uchar_t)-1; 24543 } 24544 24545 kmem_free(buffer, 12); 24546 kmem_free(com, sizeof (*com)); 24547 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 24548 return (EFAULT); 24549 24550 return (rval); 24551 } 24552 24553 24554 /* 24555 * Function: sr_read_tochdr() 24556 * 24557 * Description: This routine is the driver entry point for handling CD-ROM 24558 * ioctl requests to read the Table of Contents (TOC) header 24559 * (CDROMREADTOHDR). The TOC header consists of the disk starting 24560 * and ending track numbers 24561 * 24562 * Arguments: dev - the device 'dev_t' 24563 * data - pointer to user provided toc header structure, 24564 * specifying the starting and ending track numbers. 24565 * flag - this argument is a pass through to ddi_copyxxx() 24566 * directly from the mode argument of ioctl(). 24567 * 24568 * Return Code: the code returned by sd_send_scsi_cmd() 24569 * EFAULT if ddi_copyxxx() fails 24570 * ENXIO if fail ddi_get_soft_state 24571 * EINVAL if data pointer is NULL 24572 */ 24573 24574 static int 24575 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 24576 { 24577 struct sd_lun *un; 24578 struct uscsi_cmd *com; 24579 struct cdrom_tochdr toc_header; 24580 struct cdrom_tochdr *hdr = &toc_header; 24581 char cdb[CDB_GROUP1]; 24582 int rval; 24583 caddr_t buffer; 24584 24585 if (data == NULL) { 24586 return (EINVAL); 24587 } 24588 24589 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24590 (un->un_state == SD_STATE_OFFLINE)) { 24591 return (ENXIO); 24592 } 24593 24594 buffer = kmem_zalloc(4, KM_SLEEP); 24595 bzero(cdb, CDB_GROUP1); 24596 cdb[0] = SCMD_READ_TOC; 24597 /* 24598 * Specifying a track number of 0x00 in the READ TOC command indicates 24599 * that the TOC header should be returned 24600 */ 24601 cdb[6] = 0x00; 24602 /* 24603 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 24604 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 24605 */ 24606 cdb[8] = 0x04; 24607 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24608 com->uscsi_cdb = cdb; 24609 com->uscsi_cdblen = CDB_GROUP1; 24610 com->uscsi_bufaddr = buffer; 24611 com->uscsi_buflen = 0x04; 24612 com->uscsi_timeout = 300; 24613 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24614 24615 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24616 SD_PATH_STANDARD); 24617 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24618 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 24619 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 24620 } else { 24621 hdr->cdth_trk0 = buffer[2]; 24622 hdr->cdth_trk1 = buffer[3]; 24623 } 24624 kmem_free(buffer, 4); 24625 kmem_free(com, sizeof (*com)); 24626 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 24627 return (EFAULT); 24628 } 24629 return (rval); 24630 } 24631 24632 24633 /* 24634 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 24635 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 24636 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 24637 * digital audio and extended architecture digital audio. These modes are 24638 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 24639 * MMC specs. 24640 * 24641 * In addition to support for the various data formats these routines also 24642 * include support for devices that implement only the direct access READ 24643 * commands (0x08, 0x28), devices that implement the READ_CD commands 24644 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 24645 * READ CDXA commands (0xD8, 0xDB) 24646 */ 24647 24648 /* 24649 * Function: sr_read_mode1() 24650 * 24651 * Description: This routine is the driver entry point for handling CD-ROM 24652 * ioctl read mode1 requests (CDROMREADMODE1). 24653 * 24654 * Arguments: dev - the device 'dev_t' 24655 * data - pointer to user provided cd read structure specifying 24656 * the lba buffer address and length. 24657 * flag - this argument is a pass through to ddi_copyxxx() 24658 * directly from the mode argument of ioctl(). 24659 * 24660 * Return Code: the code returned by sd_send_scsi_cmd() 24661 * EFAULT if ddi_copyxxx() fails 24662 * ENXIO if fail ddi_get_soft_state 24663 * EINVAL if data pointer is NULL 24664 */ 24665 24666 static int 24667 sr_read_mode1(dev_t dev, caddr_t data, int flag) 24668 { 24669 struct sd_lun *un; 24670 struct cdrom_read mode1_struct; 24671 struct cdrom_read *mode1 = &mode1_struct; 24672 int rval; 24673 #ifdef _MULTI_DATAMODEL 24674 /* To support ILP32 applications in an LP64 world */ 24675 struct cdrom_read32 cdrom_read32; 24676 struct cdrom_read32 *cdrd32 = &cdrom_read32; 24677 #endif /* _MULTI_DATAMODEL */ 24678 24679 if (data == NULL) { 24680 return (EINVAL); 24681 } 24682 24683 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24684 (un->un_state == SD_STATE_OFFLINE)) { 24685 return (ENXIO); 24686 } 24687 24688 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 24689 "sd_read_mode1: entry: un:0x%p\n", un); 24690 24691 #ifdef _MULTI_DATAMODEL 24692 switch (ddi_model_convert_from(flag & FMODELS)) { 24693 case DDI_MODEL_ILP32: 24694 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 24695 return (EFAULT); 24696 } 24697 /* Convert the ILP32 uscsi data from the application to LP64 */ 24698 cdrom_read32tocdrom_read(cdrd32, mode1); 24699 break; 24700 case DDI_MODEL_NONE: 24701 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 24702 return (EFAULT); 24703 } 24704 } 24705 #else /* ! _MULTI_DATAMODEL */ 24706 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 24707 return (EFAULT); 24708 } 24709 #endif /* _MULTI_DATAMODEL */ 24710 24711 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 24712 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 24713 24714 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 24715 "sd_read_mode1: exit: un:0x%p\n", un); 24716 24717 return (rval); 24718 } 24719 24720 24721 /* 24722 * Function: sr_read_cd_mode2() 24723 * 24724 * Description: This routine is the driver entry point for handling CD-ROM 24725 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 24726 * support the READ CD (0xBE) command or the 1st generation 24727 * READ CD (0xD4) command. 24728 * 24729 * Arguments: dev - the device 'dev_t' 24730 * data - pointer to user provided cd read structure specifying 24731 * the lba buffer address and length. 24732 * flag - this argument is a pass through to ddi_copyxxx() 24733 * directly from the mode argument of ioctl(). 24734 * 24735 * Return Code: the code returned by sd_send_scsi_cmd() 24736 * EFAULT if ddi_copyxxx() fails 24737 * ENXIO if fail ddi_get_soft_state 24738 * EINVAL if data pointer is NULL 24739 */ 24740 24741 static int 24742 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 24743 { 24744 struct sd_lun *un; 24745 struct uscsi_cmd *com; 24746 struct cdrom_read mode2_struct; 24747 struct cdrom_read *mode2 = &mode2_struct; 24748 uchar_t cdb[CDB_GROUP5]; 24749 int nblocks; 24750 int rval; 24751 #ifdef _MULTI_DATAMODEL 24752 /* To support ILP32 applications in an LP64 world */ 24753 struct cdrom_read32 cdrom_read32; 24754 struct cdrom_read32 *cdrd32 = &cdrom_read32; 24755 #endif /* _MULTI_DATAMODEL */ 24756 24757 if (data == NULL) { 24758 return (EINVAL); 24759 } 24760 24761 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24762 (un->un_state == SD_STATE_OFFLINE)) { 24763 return (ENXIO); 24764 } 24765 24766 #ifdef _MULTI_DATAMODEL 24767 switch (ddi_model_convert_from(flag & FMODELS)) { 24768 case DDI_MODEL_ILP32: 24769 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 24770 return (EFAULT); 24771 } 24772 /* Convert the ILP32 uscsi data from the application to LP64 */ 24773 cdrom_read32tocdrom_read(cdrd32, mode2); 24774 break; 24775 case DDI_MODEL_NONE: 24776 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 24777 return (EFAULT); 24778 } 24779 break; 24780 } 24781 24782 #else /* ! _MULTI_DATAMODEL */ 24783 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 24784 return (EFAULT); 24785 } 24786 #endif /* _MULTI_DATAMODEL */ 24787 24788 bzero(cdb, sizeof (cdb)); 24789 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 24790 /* Read command supported by 1st generation atapi drives */ 24791 cdb[0] = SCMD_READ_CDD4; 24792 } else { 24793 /* Universal CD Access Command */ 24794 cdb[0] = SCMD_READ_CD; 24795 } 24796 24797 /* 24798 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 24799 */ 24800 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 24801 24802 /* set the start address */ 24803 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 24804 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 24805 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 24806 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 24807 24808 /* set the transfer length */ 24809 nblocks = mode2->cdread_buflen / 2336; 24810 cdb[6] = (uchar_t)(nblocks >> 16); 24811 cdb[7] = (uchar_t)(nblocks >> 8); 24812 cdb[8] = (uchar_t)nblocks; 24813 24814 /* set the filter bits */ 24815 cdb[9] = CDROM_READ_CD_USERDATA; 24816 24817 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24818 com->uscsi_cdb = (caddr_t)cdb; 24819 com->uscsi_cdblen = sizeof (cdb); 24820 com->uscsi_bufaddr = mode2->cdread_bufaddr; 24821 com->uscsi_buflen = mode2->cdread_buflen; 24822 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24823 24824 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24825 SD_PATH_STANDARD); 24826 kmem_free(com, sizeof (*com)); 24827 return (rval); 24828 } 24829 24830 24831 /* 24832 * Function: sr_read_mode2() 24833 * 24834 * Description: This routine is the driver entry point for handling CD-ROM 24835 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 24836 * do not support the READ CD (0xBE) command. 24837 * 24838 * Arguments: dev - the device 'dev_t' 24839 * data - pointer to user provided cd read structure specifying 24840 * the lba buffer address and length. 24841 * flag - this argument is a pass through to ddi_copyxxx() 24842 * directly from the mode argument of ioctl(). 24843 * 24844 * Return Code: the code returned by sd_send_scsi_cmd() 24845 * EFAULT if ddi_copyxxx() fails 24846 * ENXIO if fail ddi_get_soft_state 24847 * EINVAL if data pointer is NULL 24848 * EIO if fail to reset block size 24849 * EAGAIN if commands are in progress in the driver 24850 */ 24851 24852 static int 24853 sr_read_mode2(dev_t dev, caddr_t data, int flag) 24854 { 24855 struct sd_lun *un; 24856 struct cdrom_read mode2_struct; 24857 struct cdrom_read *mode2 = &mode2_struct; 24858 int rval; 24859 uint32_t restore_blksize; 24860 struct uscsi_cmd *com; 24861 uchar_t cdb[CDB_GROUP0]; 24862 int nblocks; 24863 24864 #ifdef _MULTI_DATAMODEL 24865 /* To support ILP32 applications in an LP64 world */ 24866 struct cdrom_read32 cdrom_read32; 24867 struct cdrom_read32 *cdrd32 = &cdrom_read32; 24868 #endif /* _MULTI_DATAMODEL */ 24869 24870 if (data == NULL) { 24871 return (EINVAL); 24872 } 24873 24874 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24875 (un->un_state == SD_STATE_OFFLINE)) { 24876 return (ENXIO); 24877 } 24878 24879 /* 24880 * Because this routine will update the device and driver block size 24881 * being used we want to make sure there are no commands in progress. 24882 * If commands are in progress the user will have to try again. 24883 * 24884 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 24885 * in sdioctl to protect commands from sdioctl through to the top of 24886 * sd_uscsi_strategy. See sdioctl for details. 24887 */ 24888 mutex_enter(SD_MUTEX(un)); 24889 if (un->un_ncmds_in_driver != 1) { 24890 mutex_exit(SD_MUTEX(un)); 24891 return (EAGAIN); 24892 } 24893 mutex_exit(SD_MUTEX(un)); 24894 24895 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 24896 "sd_read_mode2: entry: un:0x%p\n", un); 24897 24898 #ifdef _MULTI_DATAMODEL 24899 switch (ddi_model_convert_from(flag & FMODELS)) { 24900 case DDI_MODEL_ILP32: 24901 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 24902 return (EFAULT); 24903 } 24904 /* Convert the ILP32 uscsi data from the application to LP64 */ 24905 cdrom_read32tocdrom_read(cdrd32, mode2); 24906 break; 24907 case DDI_MODEL_NONE: 24908 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 24909 return (EFAULT); 24910 } 24911 break; 24912 } 24913 #else /* ! _MULTI_DATAMODEL */ 24914 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 24915 return (EFAULT); 24916 } 24917 #endif /* _MULTI_DATAMODEL */ 24918 24919 /* Store the current target block size for restoration later */ 24920 restore_blksize = un->un_tgt_blocksize; 24921 24922 /* Change the device and soft state target block size to 2336 */ 24923 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 24924 rval = EIO; 24925 goto done; 24926 } 24927 24928 24929 bzero(cdb, sizeof (cdb)); 24930 24931 /* set READ operation */ 24932 cdb[0] = SCMD_READ; 24933 24934 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 24935 mode2->cdread_lba >>= 2; 24936 24937 /* set the start address */ 24938 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 24939 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 24940 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 24941 24942 /* set the transfer length */ 24943 nblocks = mode2->cdread_buflen / 2336; 24944 cdb[4] = (uchar_t)nblocks & 0xFF; 24945 24946 /* build command */ 24947 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24948 com->uscsi_cdb = (caddr_t)cdb; 24949 com->uscsi_cdblen = sizeof (cdb); 24950 com->uscsi_bufaddr = mode2->cdread_bufaddr; 24951 com->uscsi_buflen = mode2->cdread_buflen; 24952 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24953 24954 /* 24955 * Issue SCSI command with user space address for read buffer. 24956 * 24957 * This sends the command through main channel in the driver. 24958 * 24959 * Since this is accessed via an IOCTL call, we go through the 24960 * standard path, so that if the device was powered down, then 24961 * it would be 'awakened' to handle the command. 24962 */ 24963 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24964 SD_PATH_STANDARD); 24965 24966 kmem_free(com, sizeof (*com)); 24967 24968 /* Restore the device and soft state target block size */ 24969 if (sr_sector_mode(dev, restore_blksize) != 0) { 24970 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24971 "can't do switch back to mode 1\n"); 24972 /* 24973 * If sd_send_scsi_READ succeeded we still need to report 24974 * an error because we failed to reset the block size 24975 */ 24976 if (rval == 0) { 24977 rval = EIO; 24978 } 24979 } 24980 24981 done: 24982 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 24983 "sd_read_mode2: exit: un:0x%p\n", un); 24984 24985 return (rval); 24986 } 24987 24988 24989 /* 24990 * Function: sr_sector_mode() 24991 * 24992 * Description: This utility function is used by sr_read_mode2 to set the target 24993 * block size based on the user specified size. This is a legacy 24994 * implementation based upon a vendor specific mode page 24995 * 24996 * Arguments: dev - the device 'dev_t' 24997 * data - flag indicating if block size is being set to 2336 or 24998 * 512. 24999 * 25000 * Return Code: the code returned by sd_send_scsi_cmd() 25001 * EFAULT if ddi_copyxxx() fails 25002 * ENXIO if fail ddi_get_soft_state 25003 * EINVAL if data pointer is NULL 25004 */ 25005 25006 static int 25007 sr_sector_mode(dev_t dev, uint32_t blksize) 25008 { 25009 struct sd_lun *un; 25010 uchar_t *sense; 25011 uchar_t *select; 25012 int rval; 25013 25014 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25015 (un->un_state == SD_STATE_OFFLINE)) { 25016 return (ENXIO); 25017 } 25018 25019 sense = kmem_zalloc(20, KM_SLEEP); 25020 25021 /* Note: This is a vendor specific mode page (0x81) */ 25022 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 25023 SD_PATH_STANDARD)) != 0) { 25024 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25025 "sr_sector_mode: Mode Sense failed\n"); 25026 kmem_free(sense, 20); 25027 return (rval); 25028 } 25029 select = kmem_zalloc(20, KM_SLEEP); 25030 select[3] = 0x08; 25031 select[10] = ((blksize >> 8) & 0xff); 25032 select[11] = (blksize & 0xff); 25033 select[12] = 0x01; 25034 select[13] = 0x06; 25035 select[14] = sense[14]; 25036 select[15] = sense[15]; 25037 if (blksize == SD_MODE2_BLKSIZE) { 25038 select[14] |= 0x01; 25039 } 25040 25041 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 25042 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 25043 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25044 "sr_sector_mode: Mode Select failed\n"); 25045 } else { 25046 /* 25047 * Only update the softstate block size if we successfully 25048 * changed the device block mode. 25049 */ 25050 mutex_enter(SD_MUTEX(un)); 25051 sd_update_block_info(un, blksize, 0); 25052 mutex_exit(SD_MUTEX(un)); 25053 } 25054 kmem_free(sense, 20); 25055 kmem_free(select, 20); 25056 return (rval); 25057 } 25058 25059 25060 /* 25061 * Function: sr_read_cdda() 25062 * 25063 * Description: This routine is the driver entry point for handling CD-ROM 25064 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 25065 * the target supports CDDA these requests are handled via a vendor 25066 * specific command (0xD8) If the target does not support CDDA 25067 * these requests are handled via the READ CD command (0xBE). 25068 * 25069 * Arguments: dev - the device 'dev_t' 25070 * data - pointer to user provided CD-DA structure specifying 25071 * the track starting address, transfer length, and 25072 * subcode options. 25073 * flag - this argument is a pass through to ddi_copyxxx() 25074 * directly from the mode argument of ioctl(). 25075 * 25076 * Return Code: the code returned by sd_send_scsi_cmd() 25077 * EFAULT if ddi_copyxxx() fails 25078 * ENXIO if fail ddi_get_soft_state 25079 * EINVAL if invalid arguments are provided 25080 * ENOTTY 25081 */ 25082 25083 static int 25084 sr_read_cdda(dev_t dev, caddr_t data, int flag) 25085 { 25086 struct sd_lun *un; 25087 struct uscsi_cmd *com; 25088 struct cdrom_cdda *cdda; 25089 int rval; 25090 size_t buflen; 25091 char cdb[CDB_GROUP5]; 25092 25093 #ifdef _MULTI_DATAMODEL 25094 /* To support ILP32 applications in an LP64 world */ 25095 struct cdrom_cdda32 cdrom_cdda32; 25096 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 25097 #endif /* _MULTI_DATAMODEL */ 25098 25099 if (data == NULL) { 25100 return (EINVAL); 25101 } 25102 25103 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25104 return (ENXIO); 25105 } 25106 25107 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 25108 25109 #ifdef _MULTI_DATAMODEL 25110 switch (ddi_model_convert_from(flag & FMODELS)) { 25111 case DDI_MODEL_ILP32: 25112 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 25113 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25114 "sr_read_cdda: ddi_copyin Failed\n"); 25115 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25116 return (EFAULT); 25117 } 25118 /* Convert the ILP32 uscsi data from the application to LP64 */ 25119 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 25120 break; 25121 case DDI_MODEL_NONE: 25122 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25123 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25124 "sr_read_cdda: ddi_copyin Failed\n"); 25125 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25126 return (EFAULT); 25127 } 25128 break; 25129 } 25130 #else /* ! _MULTI_DATAMODEL */ 25131 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25132 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25133 "sr_read_cdda: ddi_copyin Failed\n"); 25134 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25135 return (EFAULT); 25136 } 25137 #endif /* _MULTI_DATAMODEL */ 25138 25139 /* 25140 * Since MMC-2 expects max 3 bytes for length, check if the 25141 * length input is greater than 3 bytes 25142 */ 25143 if ((cdda->cdda_length & 0xFF000000) != 0) { 25144 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 25145 "cdrom transfer length too large: %d (limit %d)\n", 25146 cdda->cdda_length, 0xFFFFFF); 25147 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25148 return (EINVAL); 25149 } 25150 25151 switch (cdda->cdda_subcode) { 25152 case CDROM_DA_NO_SUBCODE: 25153 buflen = CDROM_BLK_2352 * cdda->cdda_length; 25154 break; 25155 case CDROM_DA_SUBQ: 25156 buflen = CDROM_BLK_2368 * cdda->cdda_length; 25157 break; 25158 case CDROM_DA_ALL_SUBCODE: 25159 buflen = CDROM_BLK_2448 * cdda->cdda_length; 25160 break; 25161 case CDROM_DA_SUBCODE_ONLY: 25162 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 25163 break; 25164 default: 25165 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25166 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 25167 cdda->cdda_subcode); 25168 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25169 return (EINVAL); 25170 } 25171 25172 /* Build and send the command */ 25173 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25174 bzero(cdb, CDB_GROUP5); 25175 25176 if (un->un_f_cfg_cdda == TRUE) { 25177 cdb[0] = (char)SCMD_READ_CD; 25178 cdb[1] = 0x04; 25179 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25180 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25181 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25182 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25183 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25184 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25185 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 25186 cdb[9] = 0x10; 25187 switch (cdda->cdda_subcode) { 25188 case CDROM_DA_NO_SUBCODE : 25189 cdb[10] = 0x0; 25190 break; 25191 case CDROM_DA_SUBQ : 25192 cdb[10] = 0x2; 25193 break; 25194 case CDROM_DA_ALL_SUBCODE : 25195 cdb[10] = 0x1; 25196 break; 25197 case CDROM_DA_SUBCODE_ONLY : 25198 /* FALLTHROUGH */ 25199 default : 25200 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25201 kmem_free(com, sizeof (*com)); 25202 return (ENOTTY); 25203 } 25204 } else { 25205 cdb[0] = (char)SCMD_READ_CDDA; 25206 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25207 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25208 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25209 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25210 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 25211 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25212 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25213 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 25214 cdb[10] = cdda->cdda_subcode; 25215 } 25216 25217 com->uscsi_cdb = cdb; 25218 com->uscsi_cdblen = CDB_GROUP5; 25219 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 25220 com->uscsi_buflen = buflen; 25221 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25222 25223 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25224 SD_PATH_STANDARD); 25225 25226 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25227 kmem_free(com, sizeof (*com)); 25228 return (rval); 25229 } 25230 25231 25232 /* 25233 * Function: sr_read_cdxa() 25234 * 25235 * Description: This routine is the driver entry point for handling CD-ROM 25236 * ioctl requests to return CD-XA (Extended Architecture) data. 25237 * (CDROMCDXA). 25238 * 25239 * Arguments: dev - the device 'dev_t' 25240 * data - pointer to user provided CD-XA structure specifying 25241 * the data starting address, transfer length, and format 25242 * flag - this argument is a pass through to ddi_copyxxx() 25243 * directly from the mode argument of ioctl(). 25244 * 25245 * Return Code: the code returned by sd_send_scsi_cmd() 25246 * EFAULT if ddi_copyxxx() fails 25247 * ENXIO if fail ddi_get_soft_state 25248 * EINVAL if data pointer is NULL 25249 */ 25250 25251 static int 25252 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 25253 { 25254 struct sd_lun *un; 25255 struct uscsi_cmd *com; 25256 struct cdrom_cdxa *cdxa; 25257 int rval; 25258 size_t buflen; 25259 char cdb[CDB_GROUP5]; 25260 uchar_t read_flags; 25261 25262 #ifdef _MULTI_DATAMODEL 25263 /* To support ILP32 applications in an LP64 world */ 25264 struct cdrom_cdxa32 cdrom_cdxa32; 25265 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 25266 #endif /* _MULTI_DATAMODEL */ 25267 25268 if (data == NULL) { 25269 return (EINVAL); 25270 } 25271 25272 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25273 return (ENXIO); 25274 } 25275 25276 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 25277 25278 #ifdef _MULTI_DATAMODEL 25279 switch (ddi_model_convert_from(flag & FMODELS)) { 25280 case DDI_MODEL_ILP32: 25281 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 25282 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25283 return (EFAULT); 25284 } 25285 /* 25286 * Convert the ILP32 uscsi data from the 25287 * application to LP64 for internal use. 25288 */ 25289 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 25290 break; 25291 case DDI_MODEL_NONE: 25292 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25293 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25294 return (EFAULT); 25295 } 25296 break; 25297 } 25298 #else /* ! _MULTI_DATAMODEL */ 25299 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25300 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25301 return (EFAULT); 25302 } 25303 #endif /* _MULTI_DATAMODEL */ 25304 25305 /* 25306 * Since MMC-2 expects max 3 bytes for length, check if the 25307 * length input is greater than 3 bytes 25308 */ 25309 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 25310 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 25311 "cdrom transfer length too large: %d (limit %d)\n", 25312 cdxa->cdxa_length, 0xFFFFFF); 25313 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25314 return (EINVAL); 25315 } 25316 25317 switch (cdxa->cdxa_format) { 25318 case CDROM_XA_DATA: 25319 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 25320 read_flags = 0x10; 25321 break; 25322 case CDROM_XA_SECTOR_DATA: 25323 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 25324 read_flags = 0xf8; 25325 break; 25326 case CDROM_XA_DATA_W_ERROR: 25327 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 25328 read_flags = 0xfc; 25329 break; 25330 default: 25331 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25332 "sr_read_cdxa: Format '0x%x' Not Supported\n", 25333 cdxa->cdxa_format); 25334 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25335 return (EINVAL); 25336 } 25337 25338 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25339 bzero(cdb, CDB_GROUP5); 25340 if (un->un_f_mmc_cap == TRUE) { 25341 cdb[0] = (char)SCMD_READ_CD; 25342 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25343 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25344 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25345 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25346 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25347 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25348 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 25349 cdb[9] = (char)read_flags; 25350 } else { 25351 /* 25352 * Note: A vendor specific command (0xDB) is being used her to 25353 * request a read of all subcodes. 25354 */ 25355 cdb[0] = (char)SCMD_READ_CDXA; 25356 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25357 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25358 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25359 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25360 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 25361 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25362 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25363 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 25364 cdb[10] = cdxa->cdxa_format; 25365 } 25366 com->uscsi_cdb = cdb; 25367 com->uscsi_cdblen = CDB_GROUP5; 25368 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 25369 com->uscsi_buflen = buflen; 25370 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25371 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25372 SD_PATH_STANDARD); 25373 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25374 kmem_free(com, sizeof (*com)); 25375 return (rval); 25376 } 25377 25378 25379 /* 25380 * Function: sr_eject() 25381 * 25382 * Description: This routine is the driver entry point for handling CD-ROM 25383 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 25384 * 25385 * Arguments: dev - the device 'dev_t' 25386 * 25387 * Return Code: the code returned by sd_send_scsi_cmd() 25388 */ 25389 25390 static int 25391 sr_eject(dev_t dev) 25392 { 25393 struct sd_lun *un; 25394 int rval; 25395 25396 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25397 (un->un_state == SD_STATE_OFFLINE)) { 25398 return (ENXIO); 25399 } 25400 25401 /* 25402 * To prevent race conditions with the eject 25403 * command, keep track of an eject command as 25404 * it progresses. If we are already handling 25405 * an eject command in the driver for the given 25406 * unit and another request to eject is received 25407 * immediately return EAGAIN so we don't lose 25408 * the command if the current eject command fails. 25409 */ 25410 mutex_enter(SD_MUTEX(un)); 25411 if (un->un_f_ejecting == TRUE) { 25412 mutex_exit(SD_MUTEX(un)); 25413 return (EAGAIN); 25414 } 25415 un->un_f_ejecting = TRUE; 25416 mutex_exit(SD_MUTEX(un)); 25417 25418 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 25419 SD_PATH_STANDARD)) != 0) { 25420 mutex_enter(SD_MUTEX(un)); 25421 un->un_f_ejecting = FALSE; 25422 mutex_exit(SD_MUTEX(un)); 25423 return (rval); 25424 } 25425 25426 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 25427 SD_PATH_STANDARD); 25428 25429 if (rval == 0) { 25430 mutex_enter(SD_MUTEX(un)); 25431 sr_ejected(un); 25432 un->un_mediastate = DKIO_EJECTED; 25433 un->un_f_ejecting = FALSE; 25434 cv_broadcast(&un->un_state_cv); 25435 mutex_exit(SD_MUTEX(un)); 25436 } else { 25437 mutex_enter(SD_MUTEX(un)); 25438 un->un_f_ejecting = FALSE; 25439 mutex_exit(SD_MUTEX(un)); 25440 } 25441 return (rval); 25442 } 25443 25444 25445 /* 25446 * Function: sr_ejected() 25447 * 25448 * Description: This routine updates the soft state structure to invalidate the 25449 * geometry information after the media has been ejected or a 25450 * media eject has been detected. 25451 * 25452 * Arguments: un - driver soft state (unit) structure 25453 */ 25454 25455 static void 25456 sr_ejected(struct sd_lun *un) 25457 { 25458 struct sd_errstats *stp; 25459 25460 ASSERT(un != NULL); 25461 ASSERT(mutex_owned(SD_MUTEX(un))); 25462 25463 un->un_f_blockcount_is_valid = FALSE; 25464 un->un_f_tgt_blocksize_is_valid = FALSE; 25465 mutex_exit(SD_MUTEX(un)); 25466 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 25467 mutex_enter(SD_MUTEX(un)); 25468 25469 if (un->un_errstats != NULL) { 25470 stp = (struct sd_errstats *)un->un_errstats->ks_data; 25471 stp->sd_capacity.value.ui64 = 0; 25472 } 25473 25474 /* remove "capacity-of-device" properties */ 25475 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25476 "device-nblocks"); 25477 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25478 "device-blksize"); 25479 } 25480 25481 25482 /* 25483 * Function: sr_check_wp() 25484 * 25485 * Description: This routine checks the write protection of a removable 25486 * media disk and hotpluggable devices via the write protect bit of 25487 * the Mode Page Header device specific field. Some devices choke 25488 * on unsupported mode page. In order to workaround this issue, 25489 * this routine has been implemented to use 0x3f mode page(request 25490 * for all pages) for all device types. 25491 * 25492 * Arguments: dev - the device 'dev_t' 25493 * 25494 * Return Code: int indicating if the device is write protected (1) or not (0) 25495 * 25496 * Context: Kernel thread. 25497 * 25498 */ 25499 25500 static int 25501 sr_check_wp(dev_t dev) 25502 { 25503 struct sd_lun *un; 25504 uchar_t device_specific; 25505 uchar_t *sense; 25506 int hdrlen; 25507 int rval = FALSE; 25508 25509 /* 25510 * Note: The return codes for this routine should be reworked to 25511 * properly handle the case of a NULL softstate. 25512 */ 25513 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25514 return (FALSE); 25515 } 25516 25517 if (un->un_f_cfg_is_atapi == TRUE) { 25518 /* 25519 * The mode page contents are not required; set the allocation 25520 * length for the mode page header only 25521 */ 25522 hdrlen = MODE_HEADER_LENGTH_GRP2; 25523 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25524 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 25525 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25526 goto err_exit; 25527 device_specific = 25528 ((struct mode_header_grp2 *)sense)->device_specific; 25529 } else { 25530 hdrlen = MODE_HEADER_LENGTH; 25531 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25532 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 25533 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25534 goto err_exit; 25535 device_specific = 25536 ((struct mode_header *)sense)->device_specific; 25537 } 25538 25539 /* 25540 * Write protect mode sense failed; not all disks 25541 * understand this query. Return FALSE assuming that 25542 * these devices are not writable. 25543 */ 25544 if (device_specific & WRITE_PROTECT) { 25545 rval = TRUE; 25546 } 25547 25548 err_exit: 25549 kmem_free(sense, hdrlen); 25550 return (rval); 25551 } 25552 25553 /* 25554 * Function: sr_volume_ctrl() 25555 * 25556 * Description: This routine is the driver entry point for handling CD-ROM 25557 * audio output volume ioctl requests. (CDROMVOLCTRL) 25558 * 25559 * Arguments: dev - the device 'dev_t' 25560 * data - pointer to user audio volume control structure 25561 * flag - this argument is a pass through to ddi_copyxxx() 25562 * directly from the mode argument of ioctl(). 25563 * 25564 * Return Code: the code returned by sd_send_scsi_cmd() 25565 * EFAULT if ddi_copyxxx() fails 25566 * ENXIO if fail ddi_get_soft_state 25567 * EINVAL if data pointer is NULL 25568 * 25569 */ 25570 25571 static int 25572 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 25573 { 25574 struct sd_lun *un; 25575 struct cdrom_volctrl volume; 25576 struct cdrom_volctrl *vol = &volume; 25577 uchar_t *sense_page; 25578 uchar_t *select_page; 25579 uchar_t *sense; 25580 uchar_t *select; 25581 int sense_buflen; 25582 int select_buflen; 25583 int rval; 25584 25585 if (data == NULL) { 25586 return (EINVAL); 25587 } 25588 25589 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25590 (un->un_state == SD_STATE_OFFLINE)) { 25591 return (ENXIO); 25592 } 25593 25594 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 25595 return (EFAULT); 25596 } 25597 25598 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 25599 struct mode_header_grp2 *sense_mhp; 25600 struct mode_header_grp2 *select_mhp; 25601 int bd_len; 25602 25603 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 25604 select_buflen = MODE_HEADER_LENGTH_GRP2 + 25605 MODEPAGE_AUDIO_CTRL_LEN; 25606 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25607 select = kmem_zalloc(select_buflen, KM_SLEEP); 25608 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 25609 sense_buflen, MODEPAGE_AUDIO_CTRL, 25610 SD_PATH_STANDARD)) != 0) { 25611 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25612 "sr_volume_ctrl: Mode Sense Failed\n"); 25613 kmem_free(sense, sense_buflen); 25614 kmem_free(select, select_buflen); 25615 return (rval); 25616 } 25617 sense_mhp = (struct mode_header_grp2 *)sense; 25618 select_mhp = (struct mode_header_grp2 *)select; 25619 bd_len = (sense_mhp->bdesc_length_hi << 8) | 25620 sense_mhp->bdesc_length_lo; 25621 if (bd_len > MODE_BLK_DESC_LENGTH) { 25622 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25623 "sr_volume_ctrl: Mode Sense returned invalid " 25624 "block descriptor length\n"); 25625 kmem_free(sense, sense_buflen); 25626 kmem_free(select, select_buflen); 25627 return (EIO); 25628 } 25629 sense_page = (uchar_t *) 25630 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 25631 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 25632 select_mhp->length_msb = 0; 25633 select_mhp->length_lsb = 0; 25634 select_mhp->bdesc_length_hi = 0; 25635 select_mhp->bdesc_length_lo = 0; 25636 } else { 25637 struct mode_header *sense_mhp, *select_mhp; 25638 25639 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25640 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25641 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25642 select = kmem_zalloc(select_buflen, KM_SLEEP); 25643 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 25644 sense_buflen, MODEPAGE_AUDIO_CTRL, 25645 SD_PATH_STANDARD)) != 0) { 25646 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25647 "sr_volume_ctrl: Mode Sense Failed\n"); 25648 kmem_free(sense, sense_buflen); 25649 kmem_free(select, select_buflen); 25650 return (rval); 25651 } 25652 sense_mhp = (struct mode_header *)sense; 25653 select_mhp = (struct mode_header *)select; 25654 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 25655 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25656 "sr_volume_ctrl: Mode Sense returned invalid " 25657 "block descriptor length\n"); 25658 kmem_free(sense, sense_buflen); 25659 kmem_free(select, select_buflen); 25660 return (EIO); 25661 } 25662 sense_page = (uchar_t *) 25663 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 25664 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 25665 select_mhp->length = 0; 25666 select_mhp->bdesc_length = 0; 25667 } 25668 /* 25669 * Note: An audio control data structure could be created and overlayed 25670 * on the following in place of the array indexing method implemented. 25671 */ 25672 25673 /* Build the select data for the user volume data */ 25674 select_page[0] = MODEPAGE_AUDIO_CTRL; 25675 select_page[1] = 0xE; 25676 /* Set the immediate bit */ 25677 select_page[2] = 0x04; 25678 /* Zero out reserved fields */ 25679 select_page[3] = 0x00; 25680 select_page[4] = 0x00; 25681 /* Return sense data for fields not to be modified */ 25682 select_page[5] = sense_page[5]; 25683 select_page[6] = sense_page[6]; 25684 select_page[7] = sense_page[7]; 25685 /* Set the user specified volume levels for channel 0 and 1 */ 25686 select_page[8] = 0x01; 25687 select_page[9] = vol->channel0; 25688 select_page[10] = 0x02; 25689 select_page[11] = vol->channel1; 25690 /* Channel 2 and 3 are currently unsupported so return the sense data */ 25691 select_page[12] = sense_page[12]; 25692 select_page[13] = sense_page[13]; 25693 select_page[14] = sense_page[14]; 25694 select_page[15] = sense_page[15]; 25695 25696 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 25697 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 25698 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 25699 } else { 25700 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 25701 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 25702 } 25703 25704 kmem_free(sense, sense_buflen); 25705 kmem_free(select, select_buflen); 25706 return (rval); 25707 } 25708 25709 25710 /* 25711 * Function: sr_read_sony_session_offset() 25712 * 25713 * Description: This routine is the driver entry point for handling CD-ROM 25714 * ioctl requests for session offset information. (CDROMREADOFFSET) 25715 * The address of the first track in the last session of a 25716 * multi-session CD-ROM is returned 25717 * 25718 * Note: This routine uses a vendor specific key value in the 25719 * command control field without implementing any vendor check here 25720 * or in the ioctl routine. 25721 * 25722 * Arguments: dev - the device 'dev_t' 25723 * data - pointer to an int to hold the requested address 25724 * flag - this argument is a pass through to ddi_copyxxx() 25725 * directly from the mode argument of ioctl(). 25726 * 25727 * Return Code: the code returned by sd_send_scsi_cmd() 25728 * EFAULT if ddi_copyxxx() fails 25729 * ENXIO if fail ddi_get_soft_state 25730 * EINVAL if data pointer is NULL 25731 */ 25732 25733 static int 25734 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 25735 { 25736 struct sd_lun *un; 25737 struct uscsi_cmd *com; 25738 caddr_t buffer; 25739 char cdb[CDB_GROUP1]; 25740 int session_offset = 0; 25741 int rval; 25742 25743 if (data == NULL) { 25744 return (EINVAL); 25745 } 25746 25747 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25748 (un->un_state == SD_STATE_OFFLINE)) { 25749 return (ENXIO); 25750 } 25751 25752 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 25753 bzero(cdb, CDB_GROUP1); 25754 cdb[0] = SCMD_READ_TOC; 25755 /* 25756 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 25757 * (4 byte TOC response header + 8 byte response data) 25758 */ 25759 cdb[8] = SONY_SESSION_OFFSET_LEN; 25760 /* Byte 9 is the control byte. A vendor specific value is used */ 25761 cdb[9] = SONY_SESSION_OFFSET_KEY; 25762 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25763 com->uscsi_cdb = cdb; 25764 com->uscsi_cdblen = CDB_GROUP1; 25765 com->uscsi_bufaddr = buffer; 25766 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 25767 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25768 25769 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25770 SD_PATH_STANDARD); 25771 if (rval != 0) { 25772 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 25773 kmem_free(com, sizeof (*com)); 25774 return (rval); 25775 } 25776 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 25777 session_offset = 25778 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 25779 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 25780 /* 25781 * Offset returned offset in current lbasize block's. Convert to 25782 * 2k block's to return to the user 25783 */ 25784 if (un->un_tgt_blocksize == CDROM_BLK_512) { 25785 session_offset >>= 2; 25786 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 25787 session_offset >>= 1; 25788 } 25789 } 25790 25791 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 25792 rval = EFAULT; 25793 } 25794 25795 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 25796 kmem_free(com, sizeof (*com)); 25797 return (rval); 25798 } 25799 25800 25801 /* 25802 * Function: sd_wm_cache_constructor() 25803 * 25804 * Description: Cache Constructor for the wmap cache for the read/modify/write 25805 * devices. 25806 * 25807 * Arguments: wm - A pointer to the sd_w_map to be initialized. 25808 * un - sd_lun structure for the device. 25809 * flag - the km flags passed to constructor 25810 * 25811 * Return Code: 0 on success. 25812 * -1 on failure. 25813 */ 25814 25815 /*ARGSUSED*/ 25816 static int 25817 sd_wm_cache_constructor(void *wm, void *un, int flags) 25818 { 25819 bzero(wm, sizeof (struct sd_w_map)); 25820 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 25821 return (0); 25822 } 25823 25824 25825 /* 25826 * Function: sd_wm_cache_destructor() 25827 * 25828 * Description: Cache destructor for the wmap cache for the read/modify/write 25829 * devices. 25830 * 25831 * Arguments: wm - A pointer to the sd_w_map to be initialized. 25832 * un - sd_lun structure for the device. 25833 */ 25834 /*ARGSUSED*/ 25835 static void 25836 sd_wm_cache_destructor(void *wm, void *un) 25837 { 25838 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 25839 } 25840 25841 25842 /* 25843 * Function: sd_range_lock() 25844 * 25845 * Description: Lock the range of blocks specified as parameter to ensure 25846 * that read, modify write is atomic and no other i/o writes 25847 * to the same location. The range is specified in terms 25848 * of start and end blocks. Block numbers are the actual 25849 * media block numbers and not system. 25850 * 25851 * Arguments: un - sd_lun structure for the device. 25852 * startb - The starting block number 25853 * endb - The end block number 25854 * typ - type of i/o - simple/read_modify_write 25855 * 25856 * Return Code: wm - pointer to the wmap structure. 25857 * 25858 * Context: This routine can sleep. 25859 */ 25860 25861 static struct sd_w_map * 25862 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 25863 { 25864 struct sd_w_map *wmp = NULL; 25865 struct sd_w_map *sl_wmp = NULL; 25866 struct sd_w_map *tmp_wmp; 25867 wm_state state = SD_WM_CHK_LIST; 25868 25869 25870 ASSERT(un != NULL); 25871 ASSERT(!mutex_owned(SD_MUTEX(un))); 25872 25873 mutex_enter(SD_MUTEX(un)); 25874 25875 while (state != SD_WM_DONE) { 25876 25877 switch (state) { 25878 case SD_WM_CHK_LIST: 25879 /* 25880 * This is the starting state. Check the wmap list 25881 * to see if the range is currently available. 25882 */ 25883 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 25884 /* 25885 * If this is a simple write and no rmw 25886 * i/o is pending then try to lock the 25887 * range as the range should be available. 25888 */ 25889 state = SD_WM_LOCK_RANGE; 25890 } else { 25891 tmp_wmp = sd_get_range(un, startb, endb); 25892 if (tmp_wmp != NULL) { 25893 if ((wmp != NULL) && ONLIST(un, wmp)) { 25894 /* 25895 * Should not keep onlist wmps 25896 * while waiting this macro 25897 * will also do wmp = NULL; 25898 */ 25899 FREE_ONLIST_WMAP(un, wmp); 25900 } 25901 /* 25902 * sl_wmp is the wmap on which wait 25903 * is done, since the tmp_wmp points 25904 * to the inuse wmap, set sl_wmp to 25905 * tmp_wmp and change the state to sleep 25906 */ 25907 sl_wmp = tmp_wmp; 25908 state = SD_WM_WAIT_MAP; 25909 } else { 25910 state = SD_WM_LOCK_RANGE; 25911 } 25912 25913 } 25914 break; 25915 25916 case SD_WM_LOCK_RANGE: 25917 ASSERT(un->un_wm_cache); 25918 /* 25919 * The range need to be locked, try to get a wmap. 25920 * First attempt it with NO_SLEEP, want to avoid a sleep 25921 * if possible as we will have to release the sd mutex 25922 * if we have to sleep. 25923 */ 25924 if (wmp == NULL) 25925 wmp = kmem_cache_alloc(un->un_wm_cache, 25926 KM_NOSLEEP); 25927 if (wmp == NULL) { 25928 mutex_exit(SD_MUTEX(un)); 25929 _NOTE(DATA_READABLE_WITHOUT_LOCK 25930 (sd_lun::un_wm_cache)) 25931 wmp = kmem_cache_alloc(un->un_wm_cache, 25932 KM_SLEEP); 25933 mutex_enter(SD_MUTEX(un)); 25934 /* 25935 * we released the mutex so recheck and go to 25936 * check list state. 25937 */ 25938 state = SD_WM_CHK_LIST; 25939 } else { 25940 /* 25941 * We exit out of state machine since we 25942 * have the wmap. Do the housekeeping first. 25943 * place the wmap on the wmap list if it is not 25944 * on it already and then set the state to done. 25945 */ 25946 wmp->wm_start = startb; 25947 wmp->wm_end = endb; 25948 wmp->wm_flags = typ | SD_WM_BUSY; 25949 if (typ & SD_WTYPE_RMW) { 25950 un->un_rmw_count++; 25951 } 25952 /* 25953 * If not already on the list then link 25954 */ 25955 if (!ONLIST(un, wmp)) { 25956 wmp->wm_next = un->un_wm; 25957 wmp->wm_prev = NULL; 25958 if (wmp->wm_next) 25959 wmp->wm_next->wm_prev = wmp; 25960 un->un_wm = wmp; 25961 } 25962 state = SD_WM_DONE; 25963 } 25964 break; 25965 25966 case SD_WM_WAIT_MAP: 25967 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 25968 /* 25969 * Wait is done on sl_wmp, which is set in the 25970 * check_list state. 25971 */ 25972 sl_wmp->wm_wanted_count++; 25973 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 25974 sl_wmp->wm_wanted_count--; 25975 /* 25976 * We can reuse the memory from the completed sl_wmp 25977 * lock range for our new lock, but only if noone is 25978 * waiting for it. 25979 */ 25980 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 25981 if (sl_wmp->wm_wanted_count == 0) { 25982 if (wmp != NULL) 25983 CHK_N_FREEWMP(un, wmp); 25984 wmp = sl_wmp; 25985 } 25986 sl_wmp = NULL; 25987 /* 25988 * After waking up, need to recheck for availability of 25989 * range. 25990 */ 25991 state = SD_WM_CHK_LIST; 25992 break; 25993 25994 default: 25995 panic("sd_range_lock: " 25996 "Unknown state %d in sd_range_lock", state); 25997 /*NOTREACHED*/ 25998 } /* switch(state) */ 25999 26000 } /* while(state != SD_WM_DONE) */ 26001 26002 mutex_exit(SD_MUTEX(un)); 26003 26004 ASSERT(wmp != NULL); 26005 26006 return (wmp); 26007 } 26008 26009 26010 /* 26011 * Function: sd_get_range() 26012 * 26013 * Description: Find if there any overlapping I/O to this one 26014 * Returns the write-map of 1st such I/O, NULL otherwise. 26015 * 26016 * Arguments: un - sd_lun structure for the device. 26017 * startb - The starting block number 26018 * endb - The end block number 26019 * 26020 * Return Code: wm - pointer to the wmap structure. 26021 */ 26022 26023 static struct sd_w_map * 26024 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 26025 { 26026 struct sd_w_map *wmp; 26027 26028 ASSERT(un != NULL); 26029 26030 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 26031 if (!(wmp->wm_flags & SD_WM_BUSY)) { 26032 continue; 26033 } 26034 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 26035 break; 26036 } 26037 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 26038 break; 26039 } 26040 } 26041 26042 return (wmp); 26043 } 26044 26045 26046 /* 26047 * Function: sd_free_inlist_wmap() 26048 * 26049 * Description: Unlink and free a write map struct. 26050 * 26051 * Arguments: un - sd_lun structure for the device. 26052 * wmp - sd_w_map which needs to be unlinked. 26053 */ 26054 26055 static void 26056 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 26057 { 26058 ASSERT(un != NULL); 26059 26060 if (un->un_wm == wmp) { 26061 un->un_wm = wmp->wm_next; 26062 } else { 26063 wmp->wm_prev->wm_next = wmp->wm_next; 26064 } 26065 26066 if (wmp->wm_next) { 26067 wmp->wm_next->wm_prev = wmp->wm_prev; 26068 } 26069 26070 wmp->wm_next = wmp->wm_prev = NULL; 26071 26072 kmem_cache_free(un->un_wm_cache, wmp); 26073 } 26074 26075 26076 /* 26077 * Function: sd_range_unlock() 26078 * 26079 * Description: Unlock the range locked by wm. 26080 * Free write map if nobody else is waiting on it. 26081 * 26082 * Arguments: un - sd_lun structure for the device. 26083 * wmp - sd_w_map which needs to be unlinked. 26084 */ 26085 26086 static void 26087 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 26088 { 26089 ASSERT(un != NULL); 26090 ASSERT(wm != NULL); 26091 ASSERT(!mutex_owned(SD_MUTEX(un))); 26092 26093 mutex_enter(SD_MUTEX(un)); 26094 26095 if (wm->wm_flags & SD_WTYPE_RMW) { 26096 un->un_rmw_count--; 26097 } 26098 26099 if (wm->wm_wanted_count) { 26100 wm->wm_flags = 0; 26101 /* 26102 * Broadcast that the wmap is available now. 26103 */ 26104 cv_broadcast(&wm->wm_avail); 26105 } else { 26106 /* 26107 * If no one is waiting on the map, it should be free'ed. 26108 */ 26109 sd_free_inlist_wmap(un, wm); 26110 } 26111 26112 mutex_exit(SD_MUTEX(un)); 26113 } 26114 26115 26116 /* 26117 * Function: sd_read_modify_write_task 26118 * 26119 * Description: Called from a taskq thread to initiate the write phase of 26120 * a read-modify-write request. This is used for targets where 26121 * un->un_sys_blocksize != un->un_tgt_blocksize. 26122 * 26123 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 26124 * 26125 * Context: Called under taskq thread context. 26126 */ 26127 26128 static void 26129 sd_read_modify_write_task(void *arg) 26130 { 26131 struct sd_mapblocksize_info *bsp; 26132 struct buf *bp; 26133 struct sd_xbuf *xp; 26134 struct sd_lun *un; 26135 26136 bp = arg; /* The bp is given in arg */ 26137 ASSERT(bp != NULL); 26138 26139 /* Get the pointer to the layer-private data struct */ 26140 xp = SD_GET_XBUF(bp); 26141 ASSERT(xp != NULL); 26142 bsp = xp->xb_private; 26143 ASSERT(bsp != NULL); 26144 26145 un = SD_GET_UN(bp); 26146 ASSERT(un != NULL); 26147 ASSERT(!mutex_owned(SD_MUTEX(un))); 26148 26149 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26150 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 26151 26152 /* 26153 * This is the write phase of a read-modify-write request, called 26154 * under the context of a taskq thread in response to the completion 26155 * of the read portion of the rmw request completing under interrupt 26156 * context. The write request must be sent from here down the iostart 26157 * chain as if it were being sent from sd_mapblocksize_iostart(), so 26158 * we use the layer index saved in the layer-private data area. 26159 */ 26160 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 26161 26162 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26163 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 26164 } 26165 26166 26167 /* 26168 * Function: sddump_do_read_of_rmw() 26169 * 26170 * Description: This routine will be called from sddump, If sddump is called 26171 * with an I/O which not aligned on device blocksize boundary 26172 * then the write has to be converted to read-modify-write. 26173 * Do the read part here in order to keep sddump simple. 26174 * Note - That the sd_mutex is held across the call to this 26175 * routine. 26176 * 26177 * Arguments: un - sd_lun 26178 * blkno - block number in terms of media block size. 26179 * nblk - number of blocks. 26180 * bpp - pointer to pointer to the buf structure. On return 26181 * from this function, *bpp points to the valid buffer 26182 * to which the write has to be done. 26183 * 26184 * Return Code: 0 for success or errno-type return code 26185 */ 26186 26187 static int 26188 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 26189 struct buf **bpp) 26190 { 26191 int err; 26192 int i; 26193 int rval; 26194 struct buf *bp; 26195 struct scsi_pkt *pkt = NULL; 26196 uint32_t target_blocksize; 26197 26198 ASSERT(un != NULL); 26199 ASSERT(mutex_owned(SD_MUTEX(un))); 26200 26201 target_blocksize = un->un_tgt_blocksize; 26202 26203 mutex_exit(SD_MUTEX(un)); 26204 26205 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 26206 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 26207 if (bp == NULL) { 26208 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26209 "no resources for dumping; giving up"); 26210 err = ENOMEM; 26211 goto done; 26212 } 26213 26214 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 26215 blkno, nblk); 26216 if (rval != 0) { 26217 scsi_free_consistent_buf(bp); 26218 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26219 "no resources for dumping; giving up"); 26220 err = ENOMEM; 26221 goto done; 26222 } 26223 26224 pkt->pkt_flags |= FLAG_NOINTR; 26225 26226 err = EIO; 26227 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26228 26229 /* 26230 * Scsi_poll returns 0 (success) if the command completes and 26231 * the status block is STATUS_GOOD. We should only check 26232 * errors if this condition is not true. Even then we should 26233 * send our own request sense packet only if we have a check 26234 * condition and auto request sense has not been performed by 26235 * the hba. 26236 */ 26237 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 26238 26239 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 26240 err = 0; 26241 break; 26242 } 26243 26244 /* 26245 * Check CMD_DEV_GONE 1st, give up if device is gone, 26246 * no need to read RQS data. 26247 */ 26248 if (pkt->pkt_reason == CMD_DEV_GONE) { 26249 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26250 "Device is gone\n"); 26251 break; 26252 } 26253 26254 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 26255 SD_INFO(SD_LOG_DUMP, un, 26256 "sddump: read failed with CHECK, try # %d\n", i); 26257 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 26258 (void) sd_send_polled_RQS(un); 26259 } 26260 26261 continue; 26262 } 26263 26264 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 26265 int reset_retval = 0; 26266 26267 SD_INFO(SD_LOG_DUMP, un, 26268 "sddump: read failed with BUSY, try # %d\n", i); 26269 26270 if (un->un_f_lun_reset_enabled == TRUE) { 26271 reset_retval = scsi_reset(SD_ADDRESS(un), 26272 RESET_LUN); 26273 } 26274 if (reset_retval == 0) { 26275 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26276 } 26277 (void) sd_send_polled_RQS(un); 26278 26279 } else { 26280 SD_INFO(SD_LOG_DUMP, un, 26281 "sddump: read failed with 0x%x, try # %d\n", 26282 SD_GET_PKT_STATUS(pkt), i); 26283 mutex_enter(SD_MUTEX(un)); 26284 sd_reset_target(un, pkt); 26285 mutex_exit(SD_MUTEX(un)); 26286 } 26287 26288 /* 26289 * If we are not getting anywhere with lun/target resets, 26290 * let's reset the bus. 26291 */ 26292 if (i > SD_NDUMP_RETRIES/2) { 26293 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26294 (void) sd_send_polled_RQS(un); 26295 } 26296 26297 } 26298 scsi_destroy_pkt(pkt); 26299 26300 if (err != 0) { 26301 scsi_free_consistent_buf(bp); 26302 *bpp = NULL; 26303 } else { 26304 *bpp = bp; 26305 } 26306 26307 done: 26308 mutex_enter(SD_MUTEX(un)); 26309 return (err); 26310 } 26311 26312 26313 /* 26314 * Function: sd_failfast_flushq 26315 * 26316 * Description: Take all bp's on the wait queue that have B_FAILFAST set 26317 * in b_flags and move them onto the failfast queue, then kick 26318 * off a thread to return all bp's on the failfast queue to 26319 * their owners with an error set. 26320 * 26321 * Arguments: un - pointer to the soft state struct for the instance. 26322 * 26323 * Context: may execute in interrupt context. 26324 */ 26325 26326 static void 26327 sd_failfast_flushq(struct sd_lun *un) 26328 { 26329 struct buf *bp; 26330 struct buf *next_waitq_bp; 26331 struct buf *prev_waitq_bp = NULL; 26332 26333 ASSERT(un != NULL); 26334 ASSERT(mutex_owned(SD_MUTEX(un))); 26335 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 26336 ASSERT(un->un_failfast_bp == NULL); 26337 26338 SD_TRACE(SD_LOG_IO_FAILFAST, un, 26339 "sd_failfast_flushq: entry: un:0x%p\n", un); 26340 26341 /* 26342 * Check if we should flush all bufs when entering failfast state, or 26343 * just those with B_FAILFAST set. 26344 */ 26345 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 26346 /* 26347 * Move *all* bp's on the wait queue to the failfast flush 26348 * queue, including those that do NOT have B_FAILFAST set. 26349 */ 26350 if (un->un_failfast_headp == NULL) { 26351 ASSERT(un->un_failfast_tailp == NULL); 26352 un->un_failfast_headp = un->un_waitq_headp; 26353 } else { 26354 ASSERT(un->un_failfast_tailp != NULL); 26355 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 26356 } 26357 26358 un->un_failfast_tailp = un->un_waitq_tailp; 26359 26360 /* update kstat for each bp moved out of the waitq */ 26361 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 26362 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26363 } 26364 26365 /* empty the waitq */ 26366 un->un_waitq_headp = un->un_waitq_tailp = NULL; 26367 26368 } else { 26369 /* 26370 * Go thru the wait queue, pick off all entries with 26371 * B_FAILFAST set, and move these onto the failfast queue. 26372 */ 26373 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 26374 /* 26375 * Save the pointer to the next bp on the wait queue, 26376 * so we get to it on the next iteration of this loop. 26377 */ 26378 next_waitq_bp = bp->av_forw; 26379 26380 /* 26381 * If this bp from the wait queue does NOT have 26382 * B_FAILFAST set, just move on to the next element 26383 * in the wait queue. Note, this is the only place 26384 * where it is correct to set prev_waitq_bp. 26385 */ 26386 if ((bp->b_flags & B_FAILFAST) == 0) { 26387 prev_waitq_bp = bp; 26388 continue; 26389 } 26390 26391 /* 26392 * Remove the bp from the wait queue. 26393 */ 26394 if (bp == un->un_waitq_headp) { 26395 /* The bp is the first element of the waitq. */ 26396 un->un_waitq_headp = next_waitq_bp; 26397 if (un->un_waitq_headp == NULL) { 26398 /* The wait queue is now empty */ 26399 un->un_waitq_tailp = NULL; 26400 } 26401 } else { 26402 /* 26403 * The bp is either somewhere in the middle 26404 * or at the end of the wait queue. 26405 */ 26406 ASSERT(un->un_waitq_headp != NULL); 26407 ASSERT(prev_waitq_bp != NULL); 26408 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 26409 == 0); 26410 if (bp == un->un_waitq_tailp) { 26411 /* bp is the last entry on the waitq. */ 26412 ASSERT(next_waitq_bp == NULL); 26413 un->un_waitq_tailp = prev_waitq_bp; 26414 } 26415 prev_waitq_bp->av_forw = next_waitq_bp; 26416 } 26417 bp->av_forw = NULL; 26418 26419 /* 26420 * update kstat since the bp is moved out of 26421 * the waitq 26422 */ 26423 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26424 26425 /* 26426 * Now put the bp onto the failfast queue. 26427 */ 26428 if (un->un_failfast_headp == NULL) { 26429 /* failfast queue is currently empty */ 26430 ASSERT(un->un_failfast_tailp == NULL); 26431 un->un_failfast_headp = 26432 un->un_failfast_tailp = bp; 26433 } else { 26434 /* Add the bp to the end of the failfast q */ 26435 ASSERT(un->un_failfast_tailp != NULL); 26436 ASSERT(un->un_failfast_tailp->b_flags & 26437 B_FAILFAST); 26438 un->un_failfast_tailp->av_forw = bp; 26439 un->un_failfast_tailp = bp; 26440 } 26441 } 26442 } 26443 26444 /* 26445 * Now return all bp's on the failfast queue to their owners. 26446 */ 26447 while ((bp = un->un_failfast_headp) != NULL) { 26448 26449 un->un_failfast_headp = bp->av_forw; 26450 if (un->un_failfast_headp == NULL) { 26451 un->un_failfast_tailp = NULL; 26452 } 26453 26454 /* 26455 * We want to return the bp with a failure error code, but 26456 * we do not want a call to sd_start_cmds() to occur here, 26457 * so use sd_return_failed_command_no_restart() instead of 26458 * sd_return_failed_command(). 26459 */ 26460 sd_return_failed_command_no_restart(un, bp, EIO); 26461 } 26462 26463 /* Flush the xbuf queues if required. */ 26464 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 26465 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 26466 } 26467 26468 SD_TRACE(SD_LOG_IO_FAILFAST, un, 26469 "sd_failfast_flushq: exit: un:0x%p\n", un); 26470 } 26471 26472 26473 /* 26474 * Function: sd_failfast_flushq_callback 26475 * 26476 * Description: Return TRUE if the given bp meets the criteria for failfast 26477 * flushing. Used with ddi_xbuf_flushq(9F). 26478 * 26479 * Arguments: bp - ptr to buf struct to be examined. 26480 * 26481 * Context: Any 26482 */ 26483 26484 static int 26485 sd_failfast_flushq_callback(struct buf *bp) 26486 { 26487 /* 26488 * Return TRUE if (1) we want to flush ALL bufs when the failfast 26489 * state is entered; OR (2) the given bp has B_FAILFAST set. 26490 */ 26491 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 26492 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 26493 } 26494 26495 26496 26497 #if defined(__i386) || defined(__amd64) 26498 /* 26499 * Function: sd_setup_next_xfer 26500 * 26501 * Description: Prepare next I/O operation using DMA_PARTIAL 26502 * 26503 */ 26504 26505 static int 26506 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 26507 struct scsi_pkt *pkt, struct sd_xbuf *xp) 26508 { 26509 ssize_t num_blks_not_xfered; 26510 daddr_t strt_blk_num; 26511 ssize_t bytes_not_xfered; 26512 int rval; 26513 26514 ASSERT(pkt->pkt_resid == 0); 26515 26516 /* 26517 * Calculate next block number and amount to be transferred. 26518 * 26519 * How much data NOT transfered to the HBA yet. 26520 */ 26521 bytes_not_xfered = xp->xb_dma_resid; 26522 26523 /* 26524 * figure how many blocks NOT transfered to the HBA yet. 26525 */ 26526 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 26527 26528 /* 26529 * set starting block number to the end of what WAS transfered. 26530 */ 26531 strt_blk_num = xp->xb_blkno + 26532 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 26533 26534 /* 26535 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 26536 * will call scsi_initpkt with NULL_FUNC so we do not have to release 26537 * the disk mutex here. 26538 */ 26539 rval = sd_setup_next_rw_pkt(un, pkt, bp, 26540 strt_blk_num, num_blks_not_xfered); 26541 26542 if (rval == 0) { 26543 26544 /* 26545 * Success. 26546 * 26547 * Adjust things if there are still more blocks to be 26548 * transfered. 26549 */ 26550 xp->xb_dma_resid = pkt->pkt_resid; 26551 pkt->pkt_resid = 0; 26552 26553 return (1); 26554 } 26555 26556 /* 26557 * There's really only one possible return value from 26558 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 26559 * returns NULL. 26560 */ 26561 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 26562 26563 bp->b_resid = bp->b_bcount; 26564 bp->b_flags |= B_ERROR; 26565 26566 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26567 "Error setting up next portion of DMA transfer\n"); 26568 26569 return (0); 26570 } 26571 #endif 26572 26573 /* 26574 * Function: sd_panic_for_res_conflict 26575 * 26576 * Description: Call panic with a string formatted with "Reservation Conflict" 26577 * and a human readable identifier indicating the SD instance 26578 * that experienced the reservation conflict. 26579 * 26580 * Arguments: un - pointer to the soft state struct for the instance. 26581 * 26582 * Context: may execute in interrupt context. 26583 */ 26584 26585 #define SD_RESV_CONFLICT_FMT_LEN 40 26586 void 26587 sd_panic_for_res_conflict(struct sd_lun *un) 26588 { 26589 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 26590 char path_str[MAXPATHLEN]; 26591 26592 (void) snprintf(panic_str, sizeof (panic_str), 26593 "Reservation Conflict\nDisk: %s", 26594 ddi_pathname(SD_DEVINFO(un), path_str)); 26595 26596 panic(panic_str); 26597 } 26598 26599 /* 26600 * Note: The following sd_faultinjection_ioctl( ) routines implement 26601 * driver support for handling fault injection for error analysis 26602 * causing faults in multiple layers of the driver. 26603 * 26604 */ 26605 26606 #ifdef SD_FAULT_INJECTION 26607 static uint_t sd_fault_injection_on = 0; 26608 26609 /* 26610 * Function: sd_faultinjection_ioctl() 26611 * 26612 * Description: This routine is the driver entry point for handling 26613 * faultinjection ioctls to inject errors into the 26614 * layer model 26615 * 26616 * Arguments: cmd - the ioctl cmd received 26617 * arg - the arguments from user and returns 26618 */ 26619 26620 static void 26621 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 26622 26623 uint_t i; 26624 uint_t rval; 26625 26626 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 26627 26628 mutex_enter(SD_MUTEX(un)); 26629 26630 switch (cmd) { 26631 case SDIOCRUN: 26632 /* Allow pushed faults to be injected */ 26633 SD_INFO(SD_LOG_SDTEST, un, 26634 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 26635 26636 sd_fault_injection_on = 1; 26637 26638 SD_INFO(SD_LOG_IOERR, un, 26639 "sd_faultinjection_ioctl: run finished\n"); 26640 break; 26641 26642 case SDIOCSTART: 26643 /* Start Injection Session */ 26644 SD_INFO(SD_LOG_SDTEST, un, 26645 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 26646 26647 sd_fault_injection_on = 0; 26648 un->sd_injection_mask = 0xFFFFFFFF; 26649 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 26650 un->sd_fi_fifo_pkt[i] = NULL; 26651 un->sd_fi_fifo_xb[i] = NULL; 26652 un->sd_fi_fifo_un[i] = NULL; 26653 un->sd_fi_fifo_arq[i] = NULL; 26654 } 26655 un->sd_fi_fifo_start = 0; 26656 un->sd_fi_fifo_end = 0; 26657 26658 mutex_enter(&(un->un_fi_mutex)); 26659 un->sd_fi_log[0] = '\0'; 26660 un->sd_fi_buf_len = 0; 26661 mutex_exit(&(un->un_fi_mutex)); 26662 26663 SD_INFO(SD_LOG_IOERR, un, 26664 "sd_faultinjection_ioctl: start finished\n"); 26665 break; 26666 26667 case SDIOCSTOP: 26668 /* Stop Injection Session */ 26669 SD_INFO(SD_LOG_SDTEST, un, 26670 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 26671 sd_fault_injection_on = 0; 26672 un->sd_injection_mask = 0x0; 26673 26674 /* Empty stray or unuseds structs from fifo */ 26675 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 26676 if (un->sd_fi_fifo_pkt[i] != NULL) { 26677 kmem_free(un->sd_fi_fifo_pkt[i], 26678 sizeof (struct sd_fi_pkt)); 26679 } 26680 if (un->sd_fi_fifo_xb[i] != NULL) { 26681 kmem_free(un->sd_fi_fifo_xb[i], 26682 sizeof (struct sd_fi_xb)); 26683 } 26684 if (un->sd_fi_fifo_un[i] != NULL) { 26685 kmem_free(un->sd_fi_fifo_un[i], 26686 sizeof (struct sd_fi_un)); 26687 } 26688 if (un->sd_fi_fifo_arq[i] != NULL) { 26689 kmem_free(un->sd_fi_fifo_arq[i], 26690 sizeof (struct sd_fi_arq)); 26691 } 26692 un->sd_fi_fifo_pkt[i] = NULL; 26693 un->sd_fi_fifo_un[i] = NULL; 26694 un->sd_fi_fifo_xb[i] = NULL; 26695 un->sd_fi_fifo_arq[i] = NULL; 26696 } 26697 un->sd_fi_fifo_start = 0; 26698 un->sd_fi_fifo_end = 0; 26699 26700 SD_INFO(SD_LOG_IOERR, un, 26701 "sd_faultinjection_ioctl: stop finished\n"); 26702 break; 26703 26704 case SDIOCINSERTPKT: 26705 /* Store a packet struct to be pushed onto fifo */ 26706 SD_INFO(SD_LOG_SDTEST, un, 26707 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 26708 26709 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 26710 26711 sd_fault_injection_on = 0; 26712 26713 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 26714 if (un->sd_fi_fifo_pkt[i] != NULL) { 26715 kmem_free(un->sd_fi_fifo_pkt[i], 26716 sizeof (struct sd_fi_pkt)); 26717 } 26718 if (arg != NULL) { 26719 un->sd_fi_fifo_pkt[i] = 26720 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 26721 if (un->sd_fi_fifo_pkt[i] == NULL) { 26722 /* Alloc failed don't store anything */ 26723 break; 26724 } 26725 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 26726 sizeof (struct sd_fi_pkt), 0); 26727 if (rval == -1) { 26728 kmem_free(un->sd_fi_fifo_pkt[i], 26729 sizeof (struct sd_fi_pkt)); 26730 un->sd_fi_fifo_pkt[i] = NULL; 26731 } 26732 } else { 26733 SD_INFO(SD_LOG_IOERR, un, 26734 "sd_faultinjection_ioctl: pkt null\n"); 26735 } 26736 break; 26737 26738 case SDIOCINSERTXB: 26739 /* Store a xb struct to be pushed onto fifo */ 26740 SD_INFO(SD_LOG_SDTEST, un, 26741 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 26742 26743 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 26744 26745 sd_fault_injection_on = 0; 26746 26747 if (un->sd_fi_fifo_xb[i] != NULL) { 26748 kmem_free(un->sd_fi_fifo_xb[i], 26749 sizeof (struct sd_fi_xb)); 26750 un->sd_fi_fifo_xb[i] = NULL; 26751 } 26752 if (arg != NULL) { 26753 un->sd_fi_fifo_xb[i] = 26754 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 26755 if (un->sd_fi_fifo_xb[i] == NULL) { 26756 /* Alloc failed don't store anything */ 26757 break; 26758 } 26759 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 26760 sizeof (struct sd_fi_xb), 0); 26761 26762 if (rval == -1) { 26763 kmem_free(un->sd_fi_fifo_xb[i], 26764 sizeof (struct sd_fi_xb)); 26765 un->sd_fi_fifo_xb[i] = NULL; 26766 } 26767 } else { 26768 SD_INFO(SD_LOG_IOERR, un, 26769 "sd_faultinjection_ioctl: xb null\n"); 26770 } 26771 break; 26772 26773 case SDIOCINSERTUN: 26774 /* Store a un struct to be pushed onto fifo */ 26775 SD_INFO(SD_LOG_SDTEST, un, 26776 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 26777 26778 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 26779 26780 sd_fault_injection_on = 0; 26781 26782 if (un->sd_fi_fifo_un[i] != NULL) { 26783 kmem_free(un->sd_fi_fifo_un[i], 26784 sizeof (struct sd_fi_un)); 26785 un->sd_fi_fifo_un[i] = NULL; 26786 } 26787 if (arg != NULL) { 26788 un->sd_fi_fifo_un[i] = 26789 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 26790 if (un->sd_fi_fifo_un[i] == NULL) { 26791 /* Alloc failed don't store anything */ 26792 break; 26793 } 26794 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 26795 sizeof (struct sd_fi_un), 0); 26796 if (rval == -1) { 26797 kmem_free(un->sd_fi_fifo_un[i], 26798 sizeof (struct sd_fi_un)); 26799 un->sd_fi_fifo_un[i] = NULL; 26800 } 26801 26802 } else { 26803 SD_INFO(SD_LOG_IOERR, un, 26804 "sd_faultinjection_ioctl: un null\n"); 26805 } 26806 26807 break; 26808 26809 case SDIOCINSERTARQ: 26810 /* Store a arq struct to be pushed onto fifo */ 26811 SD_INFO(SD_LOG_SDTEST, un, 26812 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 26813 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 26814 26815 sd_fault_injection_on = 0; 26816 26817 if (un->sd_fi_fifo_arq[i] != NULL) { 26818 kmem_free(un->sd_fi_fifo_arq[i], 26819 sizeof (struct sd_fi_arq)); 26820 un->sd_fi_fifo_arq[i] = NULL; 26821 } 26822 if (arg != NULL) { 26823 un->sd_fi_fifo_arq[i] = 26824 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 26825 if (un->sd_fi_fifo_arq[i] == NULL) { 26826 /* Alloc failed don't store anything */ 26827 break; 26828 } 26829 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 26830 sizeof (struct sd_fi_arq), 0); 26831 if (rval == -1) { 26832 kmem_free(un->sd_fi_fifo_arq[i], 26833 sizeof (struct sd_fi_arq)); 26834 un->sd_fi_fifo_arq[i] = NULL; 26835 } 26836 26837 } else { 26838 SD_INFO(SD_LOG_IOERR, un, 26839 "sd_faultinjection_ioctl: arq null\n"); 26840 } 26841 26842 break; 26843 26844 case SDIOCPUSH: 26845 /* Push stored xb, pkt, un, and arq onto fifo */ 26846 sd_fault_injection_on = 0; 26847 26848 if (arg != NULL) { 26849 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 26850 if (rval != -1 && 26851 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 26852 un->sd_fi_fifo_end += i; 26853 } 26854 } else { 26855 SD_INFO(SD_LOG_IOERR, un, 26856 "sd_faultinjection_ioctl: push arg null\n"); 26857 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 26858 un->sd_fi_fifo_end++; 26859 } 26860 } 26861 SD_INFO(SD_LOG_IOERR, un, 26862 "sd_faultinjection_ioctl: push to end=%d\n", 26863 un->sd_fi_fifo_end); 26864 break; 26865 26866 case SDIOCRETRIEVE: 26867 /* Return buffer of log from Injection session */ 26868 SD_INFO(SD_LOG_SDTEST, un, 26869 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 26870 26871 sd_fault_injection_on = 0; 26872 26873 mutex_enter(&(un->un_fi_mutex)); 26874 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 26875 un->sd_fi_buf_len+1, 0); 26876 mutex_exit(&(un->un_fi_mutex)); 26877 26878 if (rval == -1) { 26879 /* 26880 * arg is possibly invalid setting 26881 * it to NULL for return 26882 */ 26883 arg = NULL; 26884 } 26885 break; 26886 } 26887 26888 mutex_exit(SD_MUTEX(un)); 26889 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 26890 " exit\n"); 26891 } 26892 26893 26894 /* 26895 * Function: sd_injection_log() 26896 * 26897 * Description: This routine adds buff to the already existing injection log 26898 * for retrieval via faultinjection_ioctl for use in fault 26899 * detection and recovery 26900 * 26901 * Arguments: buf - the string to add to the log 26902 */ 26903 26904 static void 26905 sd_injection_log(char *buf, struct sd_lun *un) 26906 { 26907 uint_t len; 26908 26909 ASSERT(un != NULL); 26910 ASSERT(buf != NULL); 26911 26912 mutex_enter(&(un->un_fi_mutex)); 26913 26914 len = min(strlen(buf), 255); 26915 /* Add logged value to Injection log to be returned later */ 26916 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 26917 uint_t offset = strlen((char *)un->sd_fi_log); 26918 char *destp = (char *)un->sd_fi_log + offset; 26919 int i; 26920 for (i = 0; i < len; i++) { 26921 *destp++ = *buf++; 26922 } 26923 un->sd_fi_buf_len += len; 26924 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 26925 } 26926 26927 mutex_exit(&(un->un_fi_mutex)); 26928 } 26929 26930 26931 /* 26932 * Function: sd_faultinjection() 26933 * 26934 * Description: This routine takes the pkt and changes its 26935 * content based on error injection scenerio. 26936 * 26937 * Arguments: pktp - packet to be changed 26938 */ 26939 26940 static void 26941 sd_faultinjection(struct scsi_pkt *pktp) 26942 { 26943 uint_t i; 26944 struct sd_fi_pkt *fi_pkt; 26945 struct sd_fi_xb *fi_xb; 26946 struct sd_fi_un *fi_un; 26947 struct sd_fi_arq *fi_arq; 26948 struct buf *bp; 26949 struct sd_xbuf *xb; 26950 struct sd_lun *un; 26951 26952 ASSERT(pktp != NULL); 26953 26954 /* pull bp xb and un from pktp */ 26955 bp = (struct buf *)pktp->pkt_private; 26956 xb = SD_GET_XBUF(bp); 26957 un = SD_GET_UN(bp); 26958 26959 ASSERT(un != NULL); 26960 26961 mutex_enter(SD_MUTEX(un)); 26962 26963 SD_TRACE(SD_LOG_SDTEST, un, 26964 "sd_faultinjection: entry Injection from sdintr\n"); 26965 26966 /* if injection is off return */ 26967 if (sd_fault_injection_on == 0 || 26968 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 26969 mutex_exit(SD_MUTEX(un)); 26970 return; 26971 } 26972 26973 26974 /* take next set off fifo */ 26975 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 26976 26977 fi_pkt = un->sd_fi_fifo_pkt[i]; 26978 fi_xb = un->sd_fi_fifo_xb[i]; 26979 fi_un = un->sd_fi_fifo_un[i]; 26980 fi_arq = un->sd_fi_fifo_arq[i]; 26981 26982 26983 /* set variables accordingly */ 26984 /* set pkt if it was on fifo */ 26985 if (fi_pkt != NULL) { 26986 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 26987 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 26988 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 26989 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 26990 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 26991 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 26992 26993 } 26994 26995 /* set xb if it was on fifo */ 26996 if (fi_xb != NULL) { 26997 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 26998 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 26999 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 27000 SD_CONDSET(xb, xb, xb_victim_retry_count, 27001 "xb_victim_retry_count"); 27002 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 27003 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 27004 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 27005 27006 /* copy in block data from sense */ 27007 if (fi_xb->xb_sense_data[0] != -1) { 27008 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 27009 SENSE_LENGTH); 27010 } 27011 27012 /* copy in extended sense codes */ 27013 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 27014 "es_code"); 27015 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 27016 "es_key"); 27017 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 27018 "es_add_code"); 27019 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 27020 es_qual_code, "es_qual_code"); 27021 } 27022 27023 /* set un if it was on fifo */ 27024 if (fi_un != NULL) { 27025 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 27026 SD_CONDSET(un, un, un_ctype, "un_ctype"); 27027 SD_CONDSET(un, un, un_reset_retry_count, 27028 "un_reset_retry_count"); 27029 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 27030 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 27031 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 27032 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 27033 "un_f_allow_bus_device_reset"); 27034 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 27035 27036 } 27037 27038 /* copy in auto request sense if it was on fifo */ 27039 if (fi_arq != NULL) { 27040 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 27041 } 27042 27043 /* free structs */ 27044 if (un->sd_fi_fifo_pkt[i] != NULL) { 27045 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 27046 } 27047 if (un->sd_fi_fifo_xb[i] != NULL) { 27048 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 27049 } 27050 if (un->sd_fi_fifo_un[i] != NULL) { 27051 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 27052 } 27053 if (un->sd_fi_fifo_arq[i] != NULL) { 27054 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 27055 } 27056 27057 /* 27058 * kmem_free does not gurantee to set to NULL 27059 * since we uses these to determine if we set 27060 * values or not lets confirm they are always 27061 * NULL after free 27062 */ 27063 un->sd_fi_fifo_pkt[i] = NULL; 27064 un->sd_fi_fifo_un[i] = NULL; 27065 un->sd_fi_fifo_xb[i] = NULL; 27066 un->sd_fi_fifo_arq[i] = NULL; 27067 27068 un->sd_fi_fifo_start++; 27069 27070 mutex_exit(SD_MUTEX(un)); 27071 27072 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 27073 } 27074 27075 #endif /* SD_FAULT_INJECTION */ 27076 27077 /* 27078 * This routine is invoked in sd_unit_attach(). Before calling it, the 27079 * properties in conf file should be processed already, and "hotpluggable" 27080 * property was processed also. 27081 * 27082 * The sd driver distinguishes 3 different type of devices: removable media, 27083 * non-removable media, and hotpluggable. Below the differences are defined: 27084 * 27085 * 1. Device ID 27086 * 27087 * The device ID of a device is used to identify this device. Refer to 27088 * ddi_devid_register(9F). 27089 * 27090 * For a non-removable media disk device which can provide 0x80 or 0x83 27091 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 27092 * device ID is created to identify this device. For other non-removable 27093 * media devices, a default device ID is created only if this device has 27094 * at least 2 alter cylinders. Otherwise, this device has no devid. 27095 * 27096 * ------------------------------------------------------- 27097 * removable media hotpluggable | Can Have Device ID 27098 * ------------------------------------------------------- 27099 * false false | Yes 27100 * false true | Yes 27101 * true x | No 27102 * ------------------------------------------------------ 27103 * 27104 * 27105 * 2. SCSI group 4 commands 27106 * 27107 * In SCSI specs, only some commands in group 4 command set can use 27108 * 8-byte addresses that can be used to access >2TB storage spaces. 27109 * Other commands have no such capability. Without supporting group4, 27110 * it is impossible to make full use of storage spaces of a disk with 27111 * capacity larger than 2TB. 27112 * 27113 * ----------------------------------------------- 27114 * removable media hotpluggable LP64 | Group 27115 * ----------------------------------------------- 27116 * false false false | 1 27117 * false false true | 4 27118 * false true false | 1 27119 * false true true | 4 27120 * true x x | 5 27121 * ----------------------------------------------- 27122 * 27123 * 27124 * 3. Check for VTOC Label 27125 * 27126 * If a direct-access disk has no EFI label, sd will check if it has a 27127 * valid VTOC label. Now, sd also does that check for removable media 27128 * and hotpluggable devices. 27129 * 27130 * -------------------------------------------------------------- 27131 * Direct-Access removable media hotpluggable | Check Label 27132 * ------------------------------------------------------------- 27133 * false false false | No 27134 * false false true | No 27135 * false true false | Yes 27136 * false true true | Yes 27137 * true x x | Yes 27138 * -------------------------------------------------------------- 27139 * 27140 * 27141 * 4. Building default VTOC label 27142 * 27143 * As section 3 says, sd checks if some kinds of devices have VTOC label. 27144 * If those devices have no valid VTOC label, sd(7d) will attempt to 27145 * create default VTOC for them. Currently sd creates default VTOC label 27146 * for all devices on x86 platform (VTOC_16), but only for removable 27147 * media devices on SPARC (VTOC_8). 27148 * 27149 * ----------------------------------------------------------- 27150 * removable media hotpluggable platform | Default Label 27151 * ----------------------------------------------------------- 27152 * false false sparc | No 27153 * false true x86 | Yes 27154 * false true sparc | Yes 27155 * true x x | Yes 27156 * ---------------------------------------------------------- 27157 * 27158 * 27159 * 5. Supported blocksizes of target devices 27160 * 27161 * Sd supports non-512-byte blocksize for removable media devices only. 27162 * For other devices, only 512-byte blocksize is supported. This may be 27163 * changed in near future because some RAID devices require non-512-byte 27164 * blocksize 27165 * 27166 * ----------------------------------------------------------- 27167 * removable media hotpluggable | non-512-byte blocksize 27168 * ----------------------------------------------------------- 27169 * false false | No 27170 * false true | No 27171 * true x | Yes 27172 * ----------------------------------------------------------- 27173 * 27174 * 27175 * 6. Automatic mount & unmount 27176 * 27177 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 27178 * if a device is removable media device. It return 1 for removable media 27179 * devices, and 0 for others. 27180 * 27181 * The automatic mounting subsystem should distinguish between the types 27182 * of devices and apply automounting policies to each. 27183 * 27184 * 27185 * 7. fdisk partition management 27186 * 27187 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 27188 * just supports fdisk partitions on x86 platform. On sparc platform, sd 27189 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 27190 * fdisk partitions on both x86 and SPARC platform. 27191 * 27192 * ----------------------------------------------------------- 27193 * platform removable media USB/1394 | fdisk supported 27194 * ----------------------------------------------------------- 27195 * x86 X X | true 27196 * ------------------------------------------------------------ 27197 * sparc X X | false 27198 * ------------------------------------------------------------ 27199 * 27200 * 27201 * 8. MBOOT/MBR 27202 * 27203 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 27204 * read/write mboot for removable media devices on sparc platform. 27205 * 27206 * ----------------------------------------------------------- 27207 * platform removable media USB/1394 | mboot supported 27208 * ----------------------------------------------------------- 27209 * x86 X X | true 27210 * ------------------------------------------------------------ 27211 * sparc false false | false 27212 * sparc false true | true 27213 * sparc true false | true 27214 * sparc true true | true 27215 * ------------------------------------------------------------ 27216 * 27217 * 27218 * 9. error handling during opening device 27219 * 27220 * If failed to open a disk device, an errno is returned. For some kinds 27221 * of errors, different errno is returned depending on if this device is 27222 * a removable media device. This brings USB/1394 hard disks in line with 27223 * expected hard disk behavior. It is not expected that this breaks any 27224 * application. 27225 * 27226 * ------------------------------------------------------ 27227 * removable media hotpluggable | errno 27228 * ------------------------------------------------------ 27229 * false false | EIO 27230 * false true | EIO 27231 * true x | ENXIO 27232 * ------------------------------------------------------ 27233 * 27234 * 27235 * 11. ioctls: DKIOCEJECT, CDROMEJECT 27236 * 27237 * These IOCTLs are applicable only to removable media devices. 27238 * 27239 * ----------------------------------------------------------- 27240 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 27241 * ----------------------------------------------------------- 27242 * false false | No 27243 * false true | No 27244 * true x | Yes 27245 * ----------------------------------------------------------- 27246 * 27247 * 27248 * 12. Kstats for partitions 27249 * 27250 * sd creates partition kstat for non-removable media devices. USB and 27251 * Firewire hard disks now have partition kstats 27252 * 27253 * ------------------------------------------------------ 27254 * removable media hotpluggable | kstat 27255 * ------------------------------------------------------ 27256 * false false | Yes 27257 * false true | Yes 27258 * true x | No 27259 * ------------------------------------------------------ 27260 * 27261 * 27262 * 13. Removable media & hotpluggable properties 27263 * 27264 * Sd driver creates a "removable-media" property for removable media 27265 * devices. Parent nexus drivers create a "hotpluggable" property if 27266 * it supports hotplugging. 27267 * 27268 * --------------------------------------------------------------------- 27269 * removable media hotpluggable | "removable-media" " hotpluggable" 27270 * --------------------------------------------------------------------- 27271 * false false | No No 27272 * false true | No Yes 27273 * true false | Yes No 27274 * true true | Yes Yes 27275 * --------------------------------------------------------------------- 27276 * 27277 * 27278 * 14. Power Management 27279 * 27280 * sd only power manages removable media devices or devices that support 27281 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 27282 * 27283 * A parent nexus that supports hotplugging can also set "pm-capable" 27284 * if the disk can be power managed. 27285 * 27286 * ------------------------------------------------------------ 27287 * removable media hotpluggable pm-capable | power manage 27288 * ------------------------------------------------------------ 27289 * false false false | No 27290 * false false true | Yes 27291 * false true false | No 27292 * false true true | Yes 27293 * true x x | Yes 27294 * ------------------------------------------------------------ 27295 * 27296 * USB and firewire hard disks can now be power managed independently 27297 * of the framebuffer 27298 * 27299 * 27300 * 15. Support for USB disks with capacity larger than 1TB 27301 * 27302 * Currently, sd doesn't permit a fixed disk device with capacity 27303 * larger than 1TB to be used in a 32-bit operating system environment. 27304 * However, sd doesn't do that for removable media devices. Instead, it 27305 * assumes that removable media devices cannot have a capacity larger 27306 * than 1TB. Therefore, using those devices on 32-bit system is partially 27307 * supported, which can cause some unexpected results. 27308 * 27309 * --------------------------------------------------------------------- 27310 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 27311 * --------------------------------------------------------------------- 27312 * false false | true | no 27313 * false true | true | no 27314 * true false | true | Yes 27315 * true true | true | Yes 27316 * --------------------------------------------------------------------- 27317 * 27318 * 27319 * 16. Check write-protection at open time 27320 * 27321 * When a removable media device is being opened for writing without NDELAY 27322 * flag, sd will check if this device is writable. If attempting to open 27323 * without NDELAY flag a write-protected device, this operation will abort. 27324 * 27325 * ------------------------------------------------------------ 27326 * removable media USB/1394 | WP Check 27327 * ------------------------------------------------------------ 27328 * false false | No 27329 * false true | No 27330 * true false | Yes 27331 * true true | Yes 27332 * ------------------------------------------------------------ 27333 * 27334 * 27335 * 17. syslog when corrupted VTOC is encountered 27336 * 27337 * Currently, if an invalid VTOC is encountered, sd only print syslog 27338 * for fixed SCSI disks. 27339 * ------------------------------------------------------------ 27340 * removable media USB/1394 | print syslog 27341 * ------------------------------------------------------------ 27342 * false false | Yes 27343 * false true | No 27344 * true false | No 27345 * true true | No 27346 * ------------------------------------------------------------ 27347 */ 27348 static void 27349 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 27350 { 27351 int pm_capable_prop; 27352 27353 ASSERT(un->un_sd); 27354 ASSERT(un->un_sd->sd_inq); 27355 27356 /* 27357 * Enable SYNC CACHE support for all devices. 27358 */ 27359 un->un_f_sync_cache_supported = TRUE; 27360 27361 if (un->un_sd->sd_inq->inq_rmb) { 27362 /* 27363 * The media of this device is removable. And for this kind 27364 * of devices, it is possible to change medium after opening 27365 * devices. Thus we should support this operation. 27366 */ 27367 un->un_f_has_removable_media = TRUE; 27368 27369 /* 27370 * support non-512-byte blocksize of removable media devices 27371 */ 27372 un->un_f_non_devbsize_supported = TRUE; 27373 27374 /* 27375 * Assume that all removable media devices support DOOR_LOCK 27376 */ 27377 un->un_f_doorlock_supported = TRUE; 27378 27379 /* 27380 * For a removable media device, it is possible to be opened 27381 * with NDELAY flag when there is no media in drive, in this 27382 * case we don't care if device is writable. But if without 27383 * NDELAY flag, we need to check if media is write-protected. 27384 */ 27385 un->un_f_chk_wp_open = TRUE; 27386 27387 /* 27388 * need to start a SCSI watch thread to monitor media state, 27389 * when media is being inserted or ejected, notify syseventd. 27390 */ 27391 un->un_f_monitor_media_state = TRUE; 27392 27393 /* 27394 * Some devices don't support START_STOP_UNIT command. 27395 * Therefore, we'd better check if a device supports it 27396 * before sending it. 27397 */ 27398 un->un_f_check_start_stop = TRUE; 27399 27400 /* 27401 * support eject media ioctl: 27402 * FDEJECT, DKIOCEJECT, CDROMEJECT 27403 */ 27404 un->un_f_eject_media_supported = TRUE; 27405 27406 /* 27407 * Because many removable-media devices don't support 27408 * LOG_SENSE, we couldn't use this command to check if 27409 * a removable media device support power-management. 27410 * We assume that they support power-management via 27411 * START_STOP_UNIT command and can be spun up and down 27412 * without limitations. 27413 */ 27414 un->un_f_pm_supported = TRUE; 27415 27416 /* 27417 * Need to create a zero length (Boolean) property 27418 * removable-media for the removable media devices. 27419 * Note that the return value of the property is not being 27420 * checked, since if unable to create the property 27421 * then do not want the attach to fail altogether. Consistent 27422 * with other property creation in attach. 27423 */ 27424 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 27425 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 27426 27427 } else { 27428 /* 27429 * create device ID for device 27430 */ 27431 un->un_f_devid_supported = TRUE; 27432 27433 /* 27434 * Spin up non-removable-media devices once it is attached 27435 */ 27436 un->un_f_attach_spinup = TRUE; 27437 27438 /* 27439 * According to SCSI specification, Sense data has two kinds of 27440 * format: fixed format, and descriptor format. At present, we 27441 * don't support descriptor format sense data for removable 27442 * media. 27443 */ 27444 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 27445 un->un_f_descr_format_supported = TRUE; 27446 } 27447 27448 /* 27449 * kstats are created only for non-removable media devices. 27450 * 27451 * Set this in sd.conf to 0 in order to disable kstats. The 27452 * default is 1, so they are enabled by default. 27453 */ 27454 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 27455 SD_DEVINFO(un), DDI_PROP_DONTPASS, 27456 "enable-partition-kstats", 1)); 27457 27458 /* 27459 * Check if HBA has set the "pm-capable" property. 27460 * If "pm-capable" exists and is non-zero then we can 27461 * power manage the device without checking the start/stop 27462 * cycle count log sense page. 27463 * 27464 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 27465 * then we should not power manage the device. 27466 * 27467 * If "pm-capable" doesn't exist then pm_capable_prop will 27468 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 27469 * sd will check the start/stop cycle count log sense page 27470 * and power manage the device if the cycle count limit has 27471 * not been exceeded. 27472 */ 27473 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 27474 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 27475 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 27476 un->un_f_log_sense_supported = TRUE; 27477 } else { 27478 /* 27479 * pm-capable property exists. 27480 * 27481 * Convert "TRUE" values for pm_capable_prop to 27482 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 27483 * later. "TRUE" values are any values except 27484 * SD_PM_CAPABLE_FALSE (0) and 27485 * SD_PM_CAPABLE_UNDEFINED (-1) 27486 */ 27487 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 27488 un->un_f_log_sense_supported = FALSE; 27489 } else { 27490 un->un_f_pm_supported = TRUE; 27491 } 27492 27493 SD_INFO(SD_LOG_ATTACH_DETACH, un, 27494 "sd_unit_attach: un:0x%p pm-capable " 27495 "property set to %d.\n", un, un->un_f_pm_supported); 27496 } 27497 } 27498 27499 if (un->un_f_is_hotpluggable) { 27500 27501 /* 27502 * Have to watch hotpluggable devices as well, since 27503 * that's the only way for userland applications to 27504 * detect hot removal while device is busy/mounted. 27505 */ 27506 un->un_f_monitor_media_state = TRUE; 27507 27508 un->un_f_check_start_stop = TRUE; 27509 27510 } 27511 } 27512 27513 /* 27514 * sd_tg_rdwr: 27515 * Provides rdwr access for cmlb via sd_tgops. The start_block is 27516 * in sys block size, req_length in bytes. 27517 * 27518 */ 27519 static int 27520 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 27521 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 27522 { 27523 struct sd_lun *un; 27524 int path_flag = (int)(uintptr_t)tg_cookie; 27525 char *dkl = NULL; 27526 diskaddr_t real_addr = start_block; 27527 diskaddr_t first_byte, end_block; 27528 27529 size_t buffer_size = reqlength; 27530 int rval; 27531 diskaddr_t cap; 27532 uint32_t lbasize; 27533 27534 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 27535 if (un == NULL) 27536 return (ENXIO); 27537 27538 if (cmd != TG_READ && cmd != TG_WRITE) 27539 return (EINVAL); 27540 27541 mutex_enter(SD_MUTEX(un)); 27542 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 27543 mutex_exit(SD_MUTEX(un)); 27544 rval = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 27545 &lbasize, path_flag); 27546 if (rval != 0) 27547 return (rval); 27548 mutex_enter(SD_MUTEX(un)); 27549 sd_update_block_info(un, lbasize, cap); 27550 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 27551 mutex_exit(SD_MUTEX(un)); 27552 return (EIO); 27553 } 27554 } 27555 27556 if (NOT_DEVBSIZE(un)) { 27557 /* 27558 * sys_blocksize != tgt_blocksize, need to re-adjust 27559 * blkno and save the index to beginning of dk_label 27560 */ 27561 first_byte = SD_SYSBLOCKS2BYTES(un, start_block); 27562 real_addr = first_byte / un->un_tgt_blocksize; 27563 27564 end_block = (first_byte + reqlength + 27565 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 27566 27567 /* round up buffer size to multiple of target block size */ 27568 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 27569 27570 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 27571 "label_addr: 0x%x allocation size: 0x%x\n", 27572 real_addr, buffer_size); 27573 27574 if (((first_byte % un->un_tgt_blocksize) != 0) || 27575 (reqlength % un->un_tgt_blocksize) != 0) 27576 /* the request is not aligned */ 27577 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 27578 } 27579 27580 /* 27581 * The MMC standard allows READ CAPACITY to be 27582 * inaccurate by a bounded amount (in the interest of 27583 * response latency). As a result, failed READs are 27584 * commonplace (due to the reading of metadata and not 27585 * data). Depending on the per-Vendor/drive Sense data, 27586 * the failed READ can cause many (unnecessary) retries. 27587 */ 27588 27589 if (ISCD(un) && (cmd == TG_READ) && 27590 (un->un_f_blockcount_is_valid == TRUE) && 27591 ((start_block == (un->un_blockcount - 1))|| 27592 (start_block == (un->un_blockcount - 2)))) { 27593 path_flag = SD_PATH_DIRECT_PRIORITY; 27594 } 27595 27596 mutex_exit(SD_MUTEX(un)); 27597 if (cmd == TG_READ) { 27598 rval = sd_send_scsi_READ(un, (dkl != NULL)? dkl: bufaddr, 27599 buffer_size, real_addr, path_flag); 27600 if (dkl != NULL) 27601 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 27602 real_addr), bufaddr, reqlength); 27603 } else { 27604 if (dkl) { 27605 rval = sd_send_scsi_READ(un, dkl, buffer_size, 27606 real_addr, path_flag); 27607 if (rval) { 27608 kmem_free(dkl, buffer_size); 27609 return (rval); 27610 } 27611 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 27612 real_addr), reqlength); 27613 } 27614 rval = sd_send_scsi_WRITE(un, (dkl != NULL)? dkl: bufaddr, 27615 buffer_size, real_addr, path_flag); 27616 } 27617 27618 if (dkl != NULL) 27619 kmem_free(dkl, buffer_size); 27620 27621 return (rval); 27622 } 27623 27624 27625 static int 27626 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 27627 { 27628 27629 struct sd_lun *un; 27630 diskaddr_t cap; 27631 uint32_t lbasize; 27632 int path_flag = (int)(uintptr_t)tg_cookie; 27633 int ret = 0; 27634 27635 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 27636 if (un == NULL) 27637 return (ENXIO); 27638 27639 switch (cmd) { 27640 case TG_GETPHYGEOM: 27641 case TG_GETVIRTGEOM: 27642 case TG_GETCAPACITY: 27643 case TG_GETBLOCKSIZE: 27644 mutex_enter(SD_MUTEX(un)); 27645 27646 if ((un->un_f_blockcount_is_valid == TRUE) && 27647 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 27648 cap = un->un_blockcount; 27649 lbasize = un->un_tgt_blocksize; 27650 mutex_exit(SD_MUTEX(un)); 27651 } else { 27652 mutex_exit(SD_MUTEX(un)); 27653 ret = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 27654 &lbasize, path_flag); 27655 if (ret != 0) 27656 return (ret); 27657 mutex_enter(SD_MUTEX(un)); 27658 sd_update_block_info(un, lbasize, cap); 27659 if ((un->un_f_blockcount_is_valid == FALSE) || 27660 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 27661 mutex_exit(SD_MUTEX(un)); 27662 return (EIO); 27663 } 27664 mutex_exit(SD_MUTEX(un)); 27665 } 27666 27667 if (cmd == TG_GETCAPACITY) { 27668 *(diskaddr_t *)arg = cap; 27669 return (0); 27670 } 27671 27672 if (cmd == TG_GETBLOCKSIZE) { 27673 *(uint32_t *)arg = lbasize; 27674 return (0); 27675 } 27676 27677 if (cmd == TG_GETPHYGEOM) 27678 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 27679 cap, lbasize, path_flag); 27680 else 27681 /* TG_GETVIRTGEOM */ 27682 ret = sd_get_virtual_geometry(un, 27683 (cmlb_geom_t *)arg, cap, lbasize); 27684 27685 return (ret); 27686 27687 case TG_GETATTR: 27688 mutex_enter(SD_MUTEX(un)); 27689 ((tg_attribute_t *)arg)->media_is_writable = 27690 un->un_f_mmc_writable_media; 27691 mutex_exit(SD_MUTEX(un)); 27692 return (0); 27693 default: 27694 return (ENOTTY); 27695 27696 } 27697 27698 } 27699