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 /* 23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 27 * Copyright (c) 2011 Bayard G. Bell. All rights reserved. 28 * Copyright (c) 2012 by Delphix. All rights reserved. 29 */ 30 /* 31 * Copyright 2011 cyril.galibern@opensvc.com 32 */ 33 34 /* 35 * SCSI disk target driver. 36 */ 37 #include <sys/scsi/scsi.h> 38 #include <sys/dkbad.h> 39 #include <sys/dklabel.h> 40 #include <sys/dkio.h> 41 #include <sys/fdio.h> 42 #include <sys/cdio.h> 43 #include <sys/mhd.h> 44 #include <sys/vtoc.h> 45 #include <sys/dktp/fdisk.h> 46 #include <sys/kstat.h> 47 #include <sys/vtrace.h> 48 #include <sys/note.h> 49 #include <sys/thread.h> 50 #include <sys/proc.h> 51 #include <sys/efi_partition.h> 52 #include <sys/var.h> 53 #include <sys/aio_req.h> 54 55 #ifdef __lock_lint 56 #define _LP64 57 #define __amd64 58 #endif 59 60 #if (defined(__fibre)) 61 /* Note: is there a leadville version of the following? */ 62 #include <sys/fc4/fcal_linkapp.h> 63 #endif 64 #include <sys/taskq.h> 65 #include <sys/uuid.h> 66 #include <sys/byteorder.h> 67 #include <sys/sdt.h> 68 69 #include "sd_xbuf.h" 70 71 #include <sys/scsi/targets/sddef.h> 72 #include <sys/cmlb.h> 73 #include <sys/sysevent/eventdefs.h> 74 #include <sys/sysevent/dev.h> 75 76 #include <sys/fm/protocol.h> 77 78 /* 79 * Loadable module info. 80 */ 81 #if (defined(__fibre)) 82 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver" 83 #else /* !__fibre */ 84 #define SD_MODULE_NAME "SCSI Disk Driver" 85 #endif /* !__fibre */ 86 87 /* 88 * Define the interconnect type, to allow the driver to distinguish 89 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 90 * 91 * This is really for backward compatibility. In the future, the driver 92 * should actually check the "interconnect-type" property as reported by 93 * the HBA; however at present this property is not defined by all HBAs, 94 * so we will use this #define (1) to permit the driver to run in 95 * backward-compatibility mode; and (2) to print a notification message 96 * if an FC HBA does not support the "interconnect-type" property. The 97 * behavior of the driver will be to assume parallel SCSI behaviors unless 98 * the "interconnect-type" property is defined by the HBA **AND** has a 99 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 100 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 101 * Channel behaviors (as per the old ssd). (Note that the 102 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 103 * will result in the driver assuming parallel SCSI behaviors.) 104 * 105 * (see common/sys/scsi/impl/services.h) 106 * 107 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 108 * since some FC HBAs may already support that, and there is some code in 109 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 110 * default would confuse that code, and besides things should work fine 111 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 112 * "interconnect_type" property. 113 * 114 */ 115 #if (defined(__fibre)) 116 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 117 #else 118 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 119 #endif 120 121 /* 122 * The name of the driver, established from the module name in _init. 123 */ 124 static char *sd_label = NULL; 125 126 /* 127 * Driver name is unfortunately prefixed on some driver.conf properties. 128 */ 129 #if (defined(__fibre)) 130 #define sd_max_xfer_size ssd_max_xfer_size 131 #define sd_config_list ssd_config_list 132 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 133 static char *sd_config_list = "ssd-config-list"; 134 #else 135 static char *sd_max_xfer_size = "sd_max_xfer_size"; 136 static char *sd_config_list = "sd-config-list"; 137 #endif 138 139 /* 140 * Driver global variables 141 */ 142 143 #if (defined(__fibre)) 144 /* 145 * These #defines are to avoid namespace collisions that occur because this 146 * code is currently used to compile two separate driver modules: sd and ssd. 147 * All global variables need to be treated this way (even if declared static) 148 * in order to allow the debugger to resolve the names properly. 149 * It is anticipated that in the near future the ssd module will be obsoleted, 150 * at which time this namespace issue should go away. 151 */ 152 #define sd_state ssd_state 153 #define sd_io_time ssd_io_time 154 #define sd_failfast_enable ssd_failfast_enable 155 #define sd_ua_retry_count ssd_ua_retry_count 156 #define sd_report_pfa ssd_report_pfa 157 #define sd_max_throttle ssd_max_throttle 158 #define sd_min_throttle ssd_min_throttle 159 #define sd_rot_delay ssd_rot_delay 160 161 #define sd_retry_on_reservation_conflict \ 162 ssd_retry_on_reservation_conflict 163 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 164 #define sd_resv_conflict_name ssd_resv_conflict_name 165 166 #define sd_component_mask ssd_component_mask 167 #define sd_level_mask ssd_level_mask 168 #define sd_debug_un ssd_debug_un 169 #define sd_error_level ssd_error_level 170 171 #define sd_xbuf_active_limit ssd_xbuf_active_limit 172 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 173 174 #define sd_tr ssd_tr 175 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 176 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 177 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 178 #define sd_check_media_time ssd_check_media_time 179 #define sd_wait_cmds_complete ssd_wait_cmds_complete 180 #define sd_label_mutex ssd_label_mutex 181 #define sd_detach_mutex ssd_detach_mutex 182 #define sd_log_buf ssd_log_buf 183 #define sd_log_mutex ssd_log_mutex 184 185 #define sd_disk_table ssd_disk_table 186 #define sd_disk_table_size ssd_disk_table_size 187 #define sd_sense_mutex ssd_sense_mutex 188 #define sd_cdbtab ssd_cdbtab 189 190 #define sd_cb_ops ssd_cb_ops 191 #define sd_ops ssd_ops 192 #define sd_additional_codes ssd_additional_codes 193 #define sd_tgops ssd_tgops 194 195 #define sd_minor_data ssd_minor_data 196 #define sd_minor_data_efi ssd_minor_data_efi 197 198 #define sd_tq ssd_tq 199 #define sd_wmr_tq ssd_wmr_tq 200 #define sd_taskq_name ssd_taskq_name 201 #define sd_wmr_taskq_name ssd_wmr_taskq_name 202 #define sd_taskq_minalloc ssd_taskq_minalloc 203 #define sd_taskq_maxalloc ssd_taskq_maxalloc 204 205 #define sd_dump_format_string ssd_dump_format_string 206 207 #define sd_iostart_chain ssd_iostart_chain 208 #define sd_iodone_chain ssd_iodone_chain 209 210 #define sd_pm_idletime ssd_pm_idletime 211 212 #define sd_force_pm_supported ssd_force_pm_supported 213 214 #define sd_dtype_optical_bind ssd_dtype_optical_bind 215 216 #define sd_ssc_init ssd_ssc_init 217 #define sd_ssc_send ssd_ssc_send 218 #define sd_ssc_fini ssd_ssc_fini 219 #define sd_ssc_assessment ssd_ssc_assessment 220 #define sd_ssc_post ssd_ssc_post 221 #define sd_ssc_print ssd_ssc_print 222 #define sd_ssc_ereport_post ssd_ssc_ereport_post 223 #define sd_ssc_set_info ssd_ssc_set_info 224 #define sd_ssc_extract_info ssd_ssc_extract_info 225 226 #endif 227 228 #ifdef SDDEBUG 229 int sd_force_pm_supported = 0; 230 #endif /* SDDEBUG */ 231 232 void *sd_state = NULL; 233 int sd_io_time = SD_IO_TIME; 234 int sd_failfast_enable = 1; 235 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 236 int sd_report_pfa = 1; 237 int sd_max_throttle = SD_MAX_THROTTLE; 238 int sd_min_throttle = SD_MIN_THROTTLE; 239 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 240 int sd_qfull_throttle_enable = TRUE; 241 242 int sd_retry_on_reservation_conflict = 1; 243 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 244 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 245 246 static int sd_dtype_optical_bind = -1; 247 248 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 249 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 250 251 /* 252 * Global data for debug logging. To enable debug printing, sd_component_mask 253 * and sd_level_mask should be set to the desired bit patterns as outlined in 254 * sddef.h. 255 */ 256 uint_t sd_component_mask = 0x0; 257 uint_t sd_level_mask = 0x0; 258 struct sd_lun *sd_debug_un = NULL; 259 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 260 261 /* Note: these may go away in the future... */ 262 static uint32_t sd_xbuf_active_limit = 512; 263 static uint32_t sd_xbuf_reserve_limit = 16; 264 265 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 266 267 /* 268 * Timer value used to reset the throttle after it has been reduced 269 * (typically in response to TRAN_BUSY or STATUS_QFULL) 270 */ 271 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 272 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 273 274 /* 275 * Interval value associated with the media change scsi watch. 276 */ 277 static int sd_check_media_time = 3000000; 278 279 /* 280 * Wait value used for in progress operations during a DDI_SUSPEND 281 */ 282 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 283 284 /* 285 * sd_label_mutex protects a static buffer used in the disk label 286 * component of the driver 287 */ 288 static kmutex_t sd_label_mutex; 289 290 /* 291 * sd_detach_mutex protects un_layer_count, un_detach_count, and 292 * un_opens_in_progress in the sd_lun structure. 293 */ 294 static kmutex_t sd_detach_mutex; 295 296 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 297 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 298 299 /* 300 * Global buffer and mutex for debug logging 301 */ 302 static char sd_log_buf[1024]; 303 static kmutex_t sd_log_mutex; 304 305 /* 306 * Structs and globals for recording attached lun information. 307 * This maintains a chain. Each node in the chain represents a SCSI controller. 308 * The structure records the number of luns attached to each target connected 309 * with the controller. 310 * For parallel scsi device only. 311 */ 312 struct sd_scsi_hba_tgt_lun { 313 struct sd_scsi_hba_tgt_lun *next; 314 dev_info_t *pdip; 315 int nlun[NTARGETS_WIDE]; 316 }; 317 318 /* 319 * Flag to indicate the lun is attached or detached 320 */ 321 #define SD_SCSI_LUN_ATTACH 0 322 #define SD_SCSI_LUN_DETACH 1 323 324 static kmutex_t sd_scsi_target_lun_mutex; 325 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 326 327 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 328 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 329 330 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 331 sd_scsi_target_lun_head)) 332 333 /* 334 * "Smart" Probe Caching structs, globals, #defines, etc. 335 * For parallel scsi and non-self-identify device only. 336 */ 337 338 /* 339 * The following resources and routines are implemented to support 340 * "smart" probing, which caches the scsi_probe() results in an array, 341 * in order to help avoid long probe times. 342 */ 343 struct sd_scsi_probe_cache { 344 struct sd_scsi_probe_cache *next; 345 dev_info_t *pdip; 346 int cache[NTARGETS_WIDE]; 347 }; 348 349 static kmutex_t sd_scsi_probe_cache_mutex; 350 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 351 352 /* 353 * Really we only need protection on the head of the linked list, but 354 * better safe than sorry. 355 */ 356 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 357 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 358 359 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 360 sd_scsi_probe_cache_head)) 361 362 /* 363 * Power attribute table 364 */ 365 static sd_power_attr_ss sd_pwr_ss = { 366 { "NAME=spindle-motor", "0=off", "1=on", NULL }, 367 {0, 100}, 368 {30, 0}, 369 {20000, 0} 370 }; 371 372 static sd_power_attr_pc sd_pwr_pc = { 373 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle", 374 "3=active", NULL }, 375 {0, 0, 0, 100}, 376 {90, 90, 20, 0}, 377 {15000, 15000, 1000, 0} 378 }; 379 380 /* 381 * Power level to power condition 382 */ 383 static int sd_pl2pc[] = { 384 SD_TARGET_START_VALID, 385 SD_TARGET_STANDBY, 386 SD_TARGET_IDLE, 387 SD_TARGET_ACTIVE 388 }; 389 390 /* 391 * Vendor specific data name property declarations 392 */ 393 394 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 395 396 static sd_tunables seagate_properties = { 397 SEAGATE_THROTTLE_VALUE, 398 0, 399 0, 400 0, 401 0, 402 0, 403 0, 404 0, 405 0 406 }; 407 408 409 static sd_tunables fujitsu_properties = { 410 FUJITSU_THROTTLE_VALUE, 411 0, 412 0, 413 0, 414 0, 415 0, 416 0, 417 0, 418 0 419 }; 420 421 static sd_tunables ibm_properties = { 422 IBM_THROTTLE_VALUE, 423 0, 424 0, 425 0, 426 0, 427 0, 428 0, 429 0, 430 0 431 }; 432 433 static sd_tunables purple_properties = { 434 PURPLE_THROTTLE_VALUE, 435 0, 436 0, 437 PURPLE_BUSY_RETRIES, 438 PURPLE_RESET_RETRY_COUNT, 439 PURPLE_RESERVE_RELEASE_TIME, 440 0, 441 0, 442 0 443 }; 444 445 static sd_tunables sve_properties = { 446 SVE_THROTTLE_VALUE, 447 0, 448 0, 449 SVE_BUSY_RETRIES, 450 SVE_RESET_RETRY_COUNT, 451 SVE_RESERVE_RELEASE_TIME, 452 SVE_MIN_THROTTLE_VALUE, 453 SVE_DISKSORT_DISABLED_FLAG, 454 0 455 }; 456 457 static sd_tunables maserati_properties = { 458 0, 459 0, 460 0, 461 0, 462 0, 463 0, 464 0, 465 MASERATI_DISKSORT_DISABLED_FLAG, 466 MASERATI_LUN_RESET_ENABLED_FLAG 467 }; 468 469 static sd_tunables pirus_properties = { 470 PIRUS_THROTTLE_VALUE, 471 0, 472 PIRUS_NRR_COUNT, 473 PIRUS_BUSY_RETRIES, 474 PIRUS_RESET_RETRY_COUNT, 475 0, 476 PIRUS_MIN_THROTTLE_VALUE, 477 PIRUS_DISKSORT_DISABLED_FLAG, 478 PIRUS_LUN_RESET_ENABLED_FLAG 479 }; 480 481 #endif 482 483 #if (defined(__sparc) && !defined(__fibre)) || \ 484 (defined(__i386) || defined(__amd64)) 485 486 487 static sd_tunables elite_properties = { 488 ELITE_THROTTLE_VALUE, 489 0, 490 0, 491 0, 492 0, 493 0, 494 0, 495 0, 496 0 497 }; 498 499 static sd_tunables st31200n_properties = { 500 ST31200N_THROTTLE_VALUE, 501 0, 502 0, 503 0, 504 0, 505 0, 506 0, 507 0, 508 0 509 }; 510 511 #endif /* Fibre or not */ 512 513 static sd_tunables lsi_properties_scsi = { 514 LSI_THROTTLE_VALUE, 515 0, 516 LSI_NOTREADY_RETRIES, 517 0, 518 0, 519 0, 520 0, 521 0, 522 0 523 }; 524 525 static sd_tunables symbios_properties = { 526 SYMBIOS_THROTTLE_VALUE, 527 0, 528 SYMBIOS_NOTREADY_RETRIES, 529 0, 530 0, 531 0, 532 0, 533 0, 534 0 535 }; 536 537 static sd_tunables lsi_properties = { 538 0, 539 0, 540 LSI_NOTREADY_RETRIES, 541 0, 542 0, 543 0, 544 0, 545 0, 546 0 547 }; 548 549 static sd_tunables lsi_oem_properties = { 550 0, 551 0, 552 LSI_OEM_NOTREADY_RETRIES, 553 0, 554 0, 555 0, 556 0, 557 0, 558 0, 559 1 560 }; 561 562 563 564 #if (defined(SD_PROP_TST)) 565 566 #define SD_TST_CTYPE_VAL CTYPE_CDROM 567 #define SD_TST_THROTTLE_VAL 16 568 #define SD_TST_NOTREADY_VAL 12 569 #define SD_TST_BUSY_VAL 60 570 #define SD_TST_RST_RETRY_VAL 36 571 #define SD_TST_RSV_REL_TIME 60 572 573 static sd_tunables tst_properties = { 574 SD_TST_THROTTLE_VAL, 575 SD_TST_CTYPE_VAL, 576 SD_TST_NOTREADY_VAL, 577 SD_TST_BUSY_VAL, 578 SD_TST_RST_RETRY_VAL, 579 SD_TST_RSV_REL_TIME, 580 0, 581 0, 582 0 583 }; 584 #endif 585 586 /* This is similar to the ANSI toupper implementation */ 587 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 588 589 /* 590 * Static Driver Configuration Table 591 * 592 * This is the table of disks which need throttle adjustment (or, perhaps 593 * something else as defined by the flags at a future time.) device_id 594 * is a string consisting of concatenated vid (vendor), pid (product/model) 595 * and revision strings as defined in the scsi_inquiry structure. Offsets of 596 * the parts of the string are as defined by the sizes in the scsi_inquiry 597 * structure. Device type is searched as far as the device_id string is 598 * defined. Flags defines which values are to be set in the driver from the 599 * properties list. 600 * 601 * Entries below which begin and end with a "*" are a special case. 602 * These do not have a specific vendor, and the string which follows 603 * can appear anywhere in the 16 byte PID portion of the inquiry data. 604 * 605 * Entries below which begin and end with a " " (blank) are a special 606 * case. The comparison function will treat multiple consecutive blanks 607 * as equivalent to a single blank. For example, this causes a 608 * sd_disk_table entry of " NEC CDROM " to match a device's id string 609 * of "NEC CDROM". 610 * 611 * Note: The MD21 controller type has been obsoleted. 612 * ST318202F is a Legacy device 613 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 614 * made with an FC connection. The entries here are a legacy. 615 */ 616 static sd_disk_config_t sd_disk_table[] = { 617 #if defined(__fibre) || defined(__i386) || defined(__amd64) 618 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 620 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 621 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 622 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 623 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 624 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 625 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 626 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 627 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 628 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 629 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 630 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 631 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 632 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 633 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 634 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 635 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 636 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 637 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 638 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 639 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 640 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 641 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 642 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 643 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 644 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 645 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 653 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 654 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 655 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 656 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 657 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 658 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 659 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 660 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 661 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 662 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 663 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 664 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 665 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 666 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 667 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 668 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 669 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 670 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 671 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 672 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 673 { "SUN T3", SD_CONF_BSET_THROTTLE | 674 SD_CONF_BSET_BSY_RETRY_COUNT| 675 SD_CONF_BSET_RST_RETRIES| 676 SD_CONF_BSET_RSV_REL_TIME, 677 &purple_properties }, 678 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 679 SD_CONF_BSET_BSY_RETRY_COUNT| 680 SD_CONF_BSET_RST_RETRIES| 681 SD_CONF_BSET_RSV_REL_TIME| 682 SD_CONF_BSET_MIN_THROTTLE| 683 SD_CONF_BSET_DISKSORT_DISABLED, 684 &sve_properties }, 685 { "SUN T4", SD_CONF_BSET_THROTTLE | 686 SD_CONF_BSET_BSY_RETRY_COUNT| 687 SD_CONF_BSET_RST_RETRIES| 688 SD_CONF_BSET_RSV_REL_TIME, 689 &purple_properties }, 690 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 691 SD_CONF_BSET_LUN_RESET_ENABLED, 692 &maserati_properties }, 693 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 694 SD_CONF_BSET_NRR_COUNT| 695 SD_CONF_BSET_BSY_RETRY_COUNT| 696 SD_CONF_BSET_RST_RETRIES| 697 SD_CONF_BSET_MIN_THROTTLE| 698 SD_CONF_BSET_DISKSORT_DISABLED| 699 SD_CONF_BSET_LUN_RESET_ENABLED, 700 &pirus_properties }, 701 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 702 SD_CONF_BSET_NRR_COUNT| 703 SD_CONF_BSET_BSY_RETRY_COUNT| 704 SD_CONF_BSET_RST_RETRIES| 705 SD_CONF_BSET_MIN_THROTTLE| 706 SD_CONF_BSET_DISKSORT_DISABLED| 707 SD_CONF_BSET_LUN_RESET_ENABLED, 708 &pirus_properties }, 709 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 710 SD_CONF_BSET_NRR_COUNT| 711 SD_CONF_BSET_BSY_RETRY_COUNT| 712 SD_CONF_BSET_RST_RETRIES| 713 SD_CONF_BSET_MIN_THROTTLE| 714 SD_CONF_BSET_DISKSORT_DISABLED| 715 SD_CONF_BSET_LUN_RESET_ENABLED, 716 &pirus_properties }, 717 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 718 SD_CONF_BSET_NRR_COUNT| 719 SD_CONF_BSET_BSY_RETRY_COUNT| 720 SD_CONF_BSET_RST_RETRIES| 721 SD_CONF_BSET_MIN_THROTTLE| 722 SD_CONF_BSET_DISKSORT_DISABLED| 723 SD_CONF_BSET_LUN_RESET_ENABLED, 724 &pirus_properties }, 725 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 726 SD_CONF_BSET_NRR_COUNT| 727 SD_CONF_BSET_BSY_RETRY_COUNT| 728 SD_CONF_BSET_RST_RETRIES| 729 SD_CONF_BSET_MIN_THROTTLE| 730 SD_CONF_BSET_DISKSORT_DISABLED| 731 SD_CONF_BSET_LUN_RESET_ENABLED, 732 &pirus_properties }, 733 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 734 SD_CONF_BSET_NRR_COUNT| 735 SD_CONF_BSET_BSY_RETRY_COUNT| 736 SD_CONF_BSET_RST_RETRIES| 737 SD_CONF_BSET_MIN_THROTTLE| 738 SD_CONF_BSET_DISKSORT_DISABLED| 739 SD_CONF_BSET_LUN_RESET_ENABLED, 740 &pirus_properties }, 741 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 742 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 743 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 744 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 745 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 746 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 747 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 748 #endif /* fibre or NON-sparc platforms */ 749 #if ((defined(__sparc) && !defined(__fibre)) ||\ 750 (defined(__i386) || defined(__amd64))) 751 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 752 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 753 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 754 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 755 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 756 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 757 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 758 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 759 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 760 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 761 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 762 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 763 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 764 &symbios_properties }, 765 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 766 &lsi_properties_scsi }, 767 #if defined(__i386) || defined(__amd64) 768 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 769 | SD_CONF_BSET_READSUB_BCD 770 | SD_CONF_BSET_READ_TOC_ADDR_BCD 771 | SD_CONF_BSET_NO_READ_HEADER 772 | SD_CONF_BSET_READ_CD_XD4), NULL }, 773 774 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 775 | SD_CONF_BSET_READSUB_BCD 776 | SD_CONF_BSET_READ_TOC_ADDR_BCD 777 | SD_CONF_BSET_NO_READ_HEADER 778 | SD_CONF_BSET_READ_CD_XD4), NULL }, 779 #endif /* __i386 || __amd64 */ 780 #endif /* sparc NON-fibre or NON-sparc platforms */ 781 782 #if (defined(SD_PROP_TST)) 783 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 784 | SD_CONF_BSET_CTYPE 785 | SD_CONF_BSET_NRR_COUNT 786 | SD_CONF_BSET_FAB_DEVID 787 | SD_CONF_BSET_NOCACHE 788 | SD_CONF_BSET_BSY_RETRY_COUNT 789 | SD_CONF_BSET_PLAYMSF_BCD 790 | SD_CONF_BSET_READSUB_BCD 791 | SD_CONF_BSET_READ_TOC_TRK_BCD 792 | SD_CONF_BSET_READ_TOC_ADDR_BCD 793 | SD_CONF_BSET_NO_READ_HEADER 794 | SD_CONF_BSET_READ_CD_XD4 795 | SD_CONF_BSET_RST_RETRIES 796 | SD_CONF_BSET_RSV_REL_TIME 797 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 798 #endif 799 }; 800 801 static const int sd_disk_table_size = 802 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 803 804 /* 805 * Emulation mode disk drive VID/PID table 806 */ 807 static char sd_flash_dev_table[][25] = { 808 "ATA MARVELL SD88SA02", 809 "MARVELL SD88SA02", 810 "TOSHIBA THNSNV05", 811 }; 812 813 static const int sd_flash_dev_table_size = 814 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]); 815 816 #define SD_INTERCONNECT_PARALLEL 0 817 #define SD_INTERCONNECT_FABRIC 1 818 #define SD_INTERCONNECT_FIBRE 2 819 #define SD_INTERCONNECT_SSA 3 820 #define SD_INTERCONNECT_SATA 4 821 #define SD_INTERCONNECT_SAS 5 822 823 #define SD_IS_PARALLEL_SCSI(un) \ 824 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 825 #define SD_IS_SERIAL(un) \ 826 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\ 827 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS)) 828 829 /* 830 * Definitions used by device id registration routines 831 */ 832 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 833 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 834 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 835 836 static kmutex_t sd_sense_mutex = {0}; 837 838 /* 839 * Macros for updates of the driver state 840 */ 841 #define New_state(un, s) \ 842 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 843 #define Restore_state(un) \ 844 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 845 846 static struct sd_cdbinfo sd_cdbtab[] = { 847 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 848 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 849 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 850 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 851 }; 852 853 /* 854 * Specifies the number of seconds that must have elapsed since the last 855 * cmd. has completed for a device to be declared idle to the PM framework. 856 */ 857 static int sd_pm_idletime = 1; 858 859 /* 860 * Internal function prototypes 861 */ 862 863 #if (defined(__fibre)) 864 /* 865 * These #defines are to avoid namespace collisions that occur because this 866 * code is currently used to compile two separate driver modules: sd and ssd. 867 * All function names need to be treated this way (even if declared static) 868 * in order to allow the debugger to resolve the names properly. 869 * It is anticipated that in the near future the ssd module will be obsoleted, 870 * at which time this ugliness should go away. 871 */ 872 #define sd_log_trace ssd_log_trace 873 #define sd_log_info ssd_log_info 874 #define sd_log_err ssd_log_err 875 #define sdprobe ssdprobe 876 #define sdinfo ssdinfo 877 #define sd_prop_op ssd_prop_op 878 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 879 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 880 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 881 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 882 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 883 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 884 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 885 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 886 #define sd_spin_up_unit ssd_spin_up_unit 887 #define sd_enable_descr_sense ssd_enable_descr_sense 888 #define sd_reenable_dsense_task ssd_reenable_dsense_task 889 #define sd_set_mmc_caps ssd_set_mmc_caps 890 #define sd_read_unit_properties ssd_read_unit_properties 891 #define sd_process_sdconf_file ssd_process_sdconf_file 892 #define sd_process_sdconf_table ssd_process_sdconf_table 893 #define sd_sdconf_id_match ssd_sdconf_id_match 894 #define sd_blank_cmp ssd_blank_cmp 895 #define sd_chk_vers1_data ssd_chk_vers1_data 896 #define sd_set_vers1_properties ssd_set_vers1_properties 897 #define sd_check_solid_state ssd_check_solid_state 898 #define sd_check_emulation_mode ssd_check_emulation_mode 899 900 #define sd_get_physical_geometry ssd_get_physical_geometry 901 #define sd_get_virtual_geometry ssd_get_virtual_geometry 902 #define sd_update_block_info ssd_update_block_info 903 #define sd_register_devid ssd_register_devid 904 #define sd_get_devid ssd_get_devid 905 #define sd_create_devid ssd_create_devid 906 #define sd_write_deviceid ssd_write_deviceid 907 #define sd_check_vpd_page_support ssd_check_vpd_page_support 908 #define sd_setup_pm ssd_setup_pm 909 #define sd_create_pm_components ssd_create_pm_components 910 #define sd_ddi_suspend ssd_ddi_suspend 911 #define sd_ddi_resume ssd_ddi_resume 912 #define sd_pm_state_change ssd_pm_state_change 913 #define sdpower ssdpower 914 #define sdattach ssdattach 915 #define sddetach ssddetach 916 #define sd_unit_attach ssd_unit_attach 917 #define sd_unit_detach ssd_unit_detach 918 #define sd_set_unit_attributes ssd_set_unit_attributes 919 #define sd_create_errstats ssd_create_errstats 920 #define sd_set_errstats ssd_set_errstats 921 #define sd_set_pstats ssd_set_pstats 922 #define sddump ssddump 923 #define sd_scsi_poll ssd_scsi_poll 924 #define sd_send_polled_RQS ssd_send_polled_RQS 925 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 926 #define sd_init_event_callbacks ssd_init_event_callbacks 927 #define sd_event_callback ssd_event_callback 928 #define sd_cache_control ssd_cache_control 929 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 930 #define sd_get_nv_sup ssd_get_nv_sup 931 #define sd_make_device ssd_make_device 932 #define sdopen ssdopen 933 #define sdclose ssdclose 934 #define sd_ready_and_valid ssd_ready_and_valid 935 #define sdmin ssdmin 936 #define sdread ssdread 937 #define sdwrite ssdwrite 938 #define sdaread ssdaread 939 #define sdawrite ssdawrite 940 #define sdstrategy ssdstrategy 941 #define sdioctl ssdioctl 942 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 943 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 944 #define sd_checksum_iostart ssd_checksum_iostart 945 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 946 #define sd_pm_iostart ssd_pm_iostart 947 #define sd_core_iostart ssd_core_iostart 948 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 949 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 950 #define sd_checksum_iodone ssd_checksum_iodone 951 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 952 #define sd_pm_iodone ssd_pm_iodone 953 #define sd_initpkt_for_buf ssd_initpkt_for_buf 954 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 955 #define sd_setup_rw_pkt ssd_setup_rw_pkt 956 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 957 #define sd_buf_iodone ssd_buf_iodone 958 #define sd_uscsi_strategy ssd_uscsi_strategy 959 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 960 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 961 #define sd_uscsi_iodone ssd_uscsi_iodone 962 #define sd_xbuf_strategy ssd_xbuf_strategy 963 #define sd_xbuf_init ssd_xbuf_init 964 #define sd_pm_entry ssd_pm_entry 965 #define sd_pm_exit ssd_pm_exit 966 967 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 968 #define sd_pm_timeout_handler ssd_pm_timeout_handler 969 970 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 971 #define sdintr ssdintr 972 #define sd_start_cmds ssd_start_cmds 973 #define sd_send_scsi_cmd ssd_send_scsi_cmd 974 #define sd_bioclone_alloc ssd_bioclone_alloc 975 #define sd_bioclone_free ssd_bioclone_free 976 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 977 #define sd_shadow_buf_free ssd_shadow_buf_free 978 #define sd_print_transport_rejected_message \ 979 ssd_print_transport_rejected_message 980 #define sd_retry_command ssd_retry_command 981 #define sd_set_retry_bp ssd_set_retry_bp 982 #define sd_send_request_sense_command ssd_send_request_sense_command 983 #define sd_start_retry_command ssd_start_retry_command 984 #define sd_start_direct_priority_command \ 985 ssd_start_direct_priority_command 986 #define sd_return_failed_command ssd_return_failed_command 987 #define sd_return_failed_command_no_restart \ 988 ssd_return_failed_command_no_restart 989 #define sd_return_command ssd_return_command 990 #define sd_sync_with_callback ssd_sync_with_callback 991 #define sdrunout ssdrunout 992 #define sd_mark_rqs_busy ssd_mark_rqs_busy 993 #define sd_mark_rqs_idle ssd_mark_rqs_idle 994 #define sd_reduce_throttle ssd_reduce_throttle 995 #define sd_restore_throttle ssd_restore_throttle 996 #define sd_print_incomplete_msg ssd_print_incomplete_msg 997 #define sd_init_cdb_limits ssd_init_cdb_limits 998 #define sd_pkt_status_good ssd_pkt_status_good 999 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 1000 #define sd_pkt_status_busy ssd_pkt_status_busy 1001 #define sd_pkt_status_reservation_conflict \ 1002 ssd_pkt_status_reservation_conflict 1003 #define sd_pkt_status_qfull ssd_pkt_status_qfull 1004 #define sd_handle_request_sense ssd_handle_request_sense 1005 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 1006 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 1007 #define sd_validate_sense_data ssd_validate_sense_data 1008 #define sd_decode_sense ssd_decode_sense 1009 #define sd_print_sense_msg ssd_print_sense_msg 1010 #define sd_sense_key_no_sense ssd_sense_key_no_sense 1011 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 1012 #define sd_sense_key_not_ready ssd_sense_key_not_ready 1013 #define sd_sense_key_medium_or_hardware_error \ 1014 ssd_sense_key_medium_or_hardware_error 1015 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1016 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1017 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1018 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1019 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1020 #define sd_sense_key_default ssd_sense_key_default 1021 #define sd_print_retry_msg ssd_print_retry_msg 1022 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1023 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1024 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1025 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1026 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1027 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1028 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1029 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1030 #define sd_pkt_reason_default ssd_pkt_reason_default 1031 #define sd_reset_target ssd_reset_target 1032 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1033 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1034 #define sd_taskq_create ssd_taskq_create 1035 #define sd_taskq_delete ssd_taskq_delete 1036 #define sd_target_change_task ssd_target_change_task 1037 #define sd_log_dev_status_event ssd_log_dev_status_event 1038 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event 1039 #define sd_log_eject_request_event ssd_log_eject_request_event 1040 #define sd_media_change_task ssd_media_change_task 1041 #define sd_handle_mchange ssd_handle_mchange 1042 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1043 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1044 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1045 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1046 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1047 sd_send_scsi_feature_GET_CONFIGURATION 1048 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1049 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1050 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1051 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1052 ssd_send_scsi_PERSISTENT_RESERVE_IN 1053 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1054 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1055 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1056 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1057 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1058 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1059 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1060 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1061 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1062 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \ 1063 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 1064 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid 1065 #define sd_alloc_rqs ssd_alloc_rqs 1066 #define sd_free_rqs ssd_free_rqs 1067 #define sd_dump_memory ssd_dump_memory 1068 #define sd_get_media_info_com ssd_get_media_info_com 1069 #define sd_get_media_info ssd_get_media_info 1070 #define sd_get_media_info_ext ssd_get_media_info_ext 1071 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1072 #define sd_nvpair_str_decode ssd_nvpair_str_decode 1073 #define sd_strtok_r ssd_strtok_r 1074 #define sd_set_properties ssd_set_properties 1075 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1076 #define sd_setup_next_xfer ssd_setup_next_xfer 1077 #define sd_dkio_get_temp ssd_dkio_get_temp 1078 #define sd_check_mhd ssd_check_mhd 1079 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1080 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1081 #define sd_sname ssd_sname 1082 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1083 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1084 #define sd_take_ownership ssd_take_ownership 1085 #define sd_reserve_release ssd_reserve_release 1086 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1087 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1088 #define sd_persistent_reservation_in_read_keys \ 1089 ssd_persistent_reservation_in_read_keys 1090 #define sd_persistent_reservation_in_read_resv \ 1091 ssd_persistent_reservation_in_read_resv 1092 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1093 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1094 #define sd_mhdioc_release ssd_mhdioc_release 1095 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1096 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1097 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1098 #define sr_change_blkmode ssr_change_blkmode 1099 #define sr_change_speed ssr_change_speed 1100 #define sr_atapi_change_speed ssr_atapi_change_speed 1101 #define sr_pause_resume ssr_pause_resume 1102 #define sr_play_msf ssr_play_msf 1103 #define sr_play_trkind ssr_play_trkind 1104 #define sr_read_all_subcodes ssr_read_all_subcodes 1105 #define sr_read_subchannel ssr_read_subchannel 1106 #define sr_read_tocentry ssr_read_tocentry 1107 #define sr_read_tochdr ssr_read_tochdr 1108 #define sr_read_cdda ssr_read_cdda 1109 #define sr_read_cdxa ssr_read_cdxa 1110 #define sr_read_mode1 ssr_read_mode1 1111 #define sr_read_mode2 ssr_read_mode2 1112 #define sr_read_cd_mode2 ssr_read_cd_mode2 1113 #define sr_sector_mode ssr_sector_mode 1114 #define sr_eject ssr_eject 1115 #define sr_ejected ssr_ejected 1116 #define sr_check_wp ssr_check_wp 1117 #define sd_watch_request_submit ssd_watch_request_submit 1118 #define sd_check_media ssd_check_media 1119 #define sd_media_watch_cb ssd_media_watch_cb 1120 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1121 #define sr_volume_ctrl ssr_volume_ctrl 1122 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1123 #define sd_log_page_supported ssd_log_page_supported 1124 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1125 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1126 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1127 #define sd_range_lock ssd_range_lock 1128 #define sd_get_range ssd_get_range 1129 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1130 #define sd_range_unlock ssd_range_unlock 1131 #define sd_read_modify_write_task ssd_read_modify_write_task 1132 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1133 1134 #define sd_iostart_chain ssd_iostart_chain 1135 #define sd_iodone_chain ssd_iodone_chain 1136 #define sd_initpkt_map ssd_initpkt_map 1137 #define sd_destroypkt_map ssd_destroypkt_map 1138 #define sd_chain_type_map ssd_chain_type_map 1139 #define sd_chain_index_map ssd_chain_index_map 1140 1141 #define sd_failfast_flushctl ssd_failfast_flushctl 1142 #define sd_failfast_flushq ssd_failfast_flushq 1143 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1144 1145 #define sd_is_lsi ssd_is_lsi 1146 #define sd_tg_rdwr ssd_tg_rdwr 1147 #define sd_tg_getinfo ssd_tg_getinfo 1148 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler 1149 1150 #endif /* #if (defined(__fibre)) */ 1151 1152 1153 int _init(void); 1154 int _fini(void); 1155 int _info(struct modinfo *modinfop); 1156 1157 /*PRINTFLIKE3*/ 1158 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1159 /*PRINTFLIKE3*/ 1160 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1161 /*PRINTFLIKE3*/ 1162 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1163 1164 static int sdprobe(dev_info_t *devi); 1165 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1166 void **result); 1167 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1168 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1169 1170 /* 1171 * Smart probe for parallel scsi 1172 */ 1173 static void sd_scsi_probe_cache_init(void); 1174 static void sd_scsi_probe_cache_fini(void); 1175 static void sd_scsi_clear_probe_cache(void); 1176 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1177 1178 /* 1179 * Attached luns on target for parallel scsi 1180 */ 1181 static void sd_scsi_target_lun_init(void); 1182 static void sd_scsi_target_lun_fini(void); 1183 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1184 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1185 1186 static int sd_spin_up_unit(sd_ssc_t *ssc); 1187 1188 /* 1189 * Using sd_ssc_init to establish sd_ssc_t struct 1190 * Using sd_ssc_send to send uscsi internal command 1191 * Using sd_ssc_fini to free sd_ssc_t struct 1192 */ 1193 static sd_ssc_t *sd_ssc_init(struct sd_lun *un); 1194 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, 1195 int flag, enum uio_seg dataspace, int path_flag); 1196 static void sd_ssc_fini(sd_ssc_t *ssc); 1197 1198 /* 1199 * Using sd_ssc_assessment to set correct type-of-assessment 1200 * Using sd_ssc_post to post ereport & system log 1201 * sd_ssc_post will call sd_ssc_print to print system log 1202 * sd_ssc_post will call sd_ssd_ereport_post to post ereport 1203 */ 1204 static void sd_ssc_assessment(sd_ssc_t *ssc, 1205 enum sd_type_assessment tp_assess); 1206 1207 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess); 1208 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity); 1209 static void sd_ssc_ereport_post(sd_ssc_t *ssc, 1210 enum sd_driver_assessment drv_assess); 1211 1212 /* 1213 * Using sd_ssc_set_info to mark an un-decodable-data error. 1214 * Using sd_ssc_extract_info to transfer information from internal 1215 * data structures to sd_ssc_t. 1216 */ 1217 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, 1218 const char *fmt, ...); 1219 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, 1220 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp); 1221 1222 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1223 enum uio_seg dataspace, int path_flag); 1224 1225 #ifdef _LP64 1226 static void sd_enable_descr_sense(sd_ssc_t *ssc); 1227 static void sd_reenable_dsense_task(void *arg); 1228 #endif /* _LP64 */ 1229 1230 static void sd_set_mmc_caps(sd_ssc_t *ssc); 1231 1232 static void sd_read_unit_properties(struct sd_lun *un); 1233 static int sd_process_sdconf_file(struct sd_lun *un); 1234 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str); 1235 static char *sd_strtok_r(char *string, const char *sepset, char **lasts); 1236 static void sd_set_properties(struct sd_lun *un, char *name, char *value); 1237 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1238 int *data_list, sd_tunables *values); 1239 static void sd_process_sdconf_table(struct sd_lun *un); 1240 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1241 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1242 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1243 int list_len, char *dataname_ptr); 1244 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1245 sd_tunables *prop_list); 1246 1247 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, 1248 int reservation_flag); 1249 static int sd_get_devid(sd_ssc_t *ssc); 1250 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc); 1251 static int sd_write_deviceid(sd_ssc_t *ssc); 1252 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1253 static int sd_check_vpd_page_support(sd_ssc_t *ssc); 1254 1255 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi); 1256 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1257 1258 static int sd_ddi_suspend(dev_info_t *devi); 1259 static int sd_ddi_resume(dev_info_t *devi); 1260 static int sd_pm_state_change(struct sd_lun *un, int level, int flag); 1261 static int sdpower(dev_info_t *devi, int component, int level); 1262 1263 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1264 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1265 static int sd_unit_attach(dev_info_t *devi); 1266 static int sd_unit_detach(dev_info_t *devi); 1267 1268 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1269 static void sd_create_errstats(struct sd_lun *un, int instance); 1270 static void sd_set_errstats(struct sd_lun *un); 1271 static void sd_set_pstats(struct sd_lun *un); 1272 1273 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1274 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1275 static int sd_send_polled_RQS(struct sd_lun *un); 1276 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1277 1278 #if (defined(__fibre)) 1279 /* 1280 * Event callbacks (photon) 1281 */ 1282 static void sd_init_event_callbacks(struct sd_lun *un); 1283 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1284 #endif 1285 1286 /* 1287 * Defines for sd_cache_control 1288 */ 1289 1290 #define SD_CACHE_ENABLE 1 1291 #define SD_CACHE_DISABLE 0 1292 #define SD_CACHE_NOCHANGE -1 1293 1294 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag); 1295 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled); 1296 static void sd_get_nv_sup(sd_ssc_t *ssc); 1297 static dev_t sd_make_device(dev_info_t *devi); 1298 static void sd_check_solid_state(sd_ssc_t *ssc); 1299 static void sd_check_emulation_mode(sd_ssc_t *ssc); 1300 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1301 uint64_t capacity); 1302 1303 /* 1304 * Driver entry point functions. 1305 */ 1306 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1307 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1308 static int sd_ready_and_valid(sd_ssc_t *ssc, int part); 1309 1310 static void sdmin(struct buf *bp); 1311 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1312 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1313 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1314 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1315 1316 static int sdstrategy(struct buf *bp); 1317 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1318 1319 /* 1320 * Function prototypes for layering functions in the iostart chain. 1321 */ 1322 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1323 struct buf *bp); 1324 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1325 struct buf *bp); 1326 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1327 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1328 struct buf *bp); 1329 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1330 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1331 1332 /* 1333 * Function prototypes for layering functions in the iodone chain. 1334 */ 1335 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1336 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1337 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1338 struct buf *bp); 1339 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1340 struct buf *bp); 1341 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1342 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1343 struct buf *bp); 1344 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1345 1346 /* 1347 * Prototypes for functions to support buf(9S) based IO. 1348 */ 1349 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1350 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1351 static void sd_destroypkt_for_buf(struct buf *); 1352 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1353 struct buf *bp, int flags, 1354 int (*callback)(caddr_t), caddr_t callback_arg, 1355 diskaddr_t lba, uint32_t blockcount); 1356 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1357 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1358 1359 /* 1360 * Prototypes for functions to support USCSI IO. 1361 */ 1362 static int sd_uscsi_strategy(struct buf *bp); 1363 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1364 static void sd_destroypkt_for_uscsi(struct buf *); 1365 1366 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1367 uchar_t chain_type, void *pktinfop); 1368 1369 static int sd_pm_entry(struct sd_lun *un); 1370 static void sd_pm_exit(struct sd_lun *un); 1371 1372 static void sd_pm_idletimeout_handler(void *arg); 1373 1374 /* 1375 * sd_core internal functions (used at the sd_core_io layer). 1376 */ 1377 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1378 static void sdintr(struct scsi_pkt *pktp); 1379 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1380 1381 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1382 enum uio_seg dataspace, int path_flag); 1383 1384 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1385 daddr_t blkno, int (*func)(struct buf *)); 1386 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1387 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1388 static void sd_bioclone_free(struct buf *bp); 1389 static void sd_shadow_buf_free(struct buf *bp); 1390 1391 static void sd_print_transport_rejected_message(struct sd_lun *un, 1392 struct sd_xbuf *xp, int code); 1393 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1394 void *arg, int code); 1395 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1396 void *arg, int code); 1397 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1398 void *arg, int code); 1399 1400 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1401 int retry_check_flag, 1402 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1403 int c), 1404 void *user_arg, int failure_code, clock_t retry_delay, 1405 void (*statp)(kstat_io_t *)); 1406 1407 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1408 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1409 1410 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1411 struct scsi_pkt *pktp); 1412 static void sd_start_retry_command(void *arg); 1413 static void sd_start_direct_priority_command(void *arg); 1414 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1415 int errcode); 1416 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1417 struct buf *bp, int errcode); 1418 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1419 static void sd_sync_with_callback(struct sd_lun *un); 1420 static int sdrunout(caddr_t arg); 1421 1422 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1423 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1424 1425 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1426 static void sd_restore_throttle(void *arg); 1427 1428 static void sd_init_cdb_limits(struct sd_lun *un); 1429 1430 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1431 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1432 1433 /* 1434 * Error handling functions 1435 */ 1436 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1437 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1438 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1439 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1440 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1441 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1442 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1443 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1444 1445 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1446 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1447 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1448 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1449 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1450 struct sd_xbuf *xp, size_t actual_len); 1451 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1452 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1453 1454 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1455 void *arg, int code); 1456 1457 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1458 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1459 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1460 uint8_t *sense_datap, 1461 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1462 static void sd_sense_key_not_ready(struct sd_lun *un, 1463 uint8_t *sense_datap, 1464 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1465 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1466 uint8_t *sense_datap, 1467 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1468 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1469 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1470 static void sd_sense_key_unit_attention(struct sd_lun *un, 1471 uint8_t *sense_datap, 1472 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1473 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1474 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1475 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1476 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1477 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1478 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1479 static void sd_sense_key_default(struct sd_lun *un, 1480 uint8_t *sense_datap, 1481 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1482 1483 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1484 void *arg, int flag); 1485 1486 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1487 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1488 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1489 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1490 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1491 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1492 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1493 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1494 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1495 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1496 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1497 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1498 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1499 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1500 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1501 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1502 1503 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1504 1505 static void sd_start_stop_unit_callback(void *arg); 1506 static void sd_start_stop_unit_task(void *arg); 1507 1508 static void sd_taskq_create(void); 1509 static void sd_taskq_delete(void); 1510 static void sd_target_change_task(void *arg); 1511 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag); 1512 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag); 1513 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag); 1514 static void sd_media_change_task(void *arg); 1515 1516 static int sd_handle_mchange(struct sd_lun *un); 1517 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag); 1518 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, 1519 uint32_t *lbap, int path_flag); 1520 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 1521 uint32_t *lbap, uint32_t *psp, int path_flag); 1522 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, 1523 int flag, int path_flag); 1524 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, 1525 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1526 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag); 1527 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, 1528 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1529 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, 1530 uchar_t usr_cmd, uchar_t *usr_bufp); 1531 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1532 struct dk_callback *dkc); 1533 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1534 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, 1535 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1536 uchar_t *bufaddr, uint_t buflen, int path_flag); 1537 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 1538 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1539 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1540 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, 1541 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1542 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, 1543 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1544 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 1545 size_t buflen, daddr_t start_block, int path_flag); 1546 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \ 1547 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \ 1548 path_flag) 1549 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\ 1550 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\ 1551 path_flag) 1552 1553 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, 1554 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1555 uint16_t param_ptr, int path_flag); 1556 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, 1557 uchar_t *bufaddr, size_t buflen, uchar_t class_req); 1558 static boolean_t sd_gesn_media_data_valid(uchar_t *data); 1559 1560 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1561 static void sd_free_rqs(struct sd_lun *un); 1562 1563 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1564 uchar_t *data, int len, int fmt); 1565 static void sd_panic_for_res_conflict(struct sd_lun *un); 1566 1567 /* 1568 * Disk Ioctl Function Prototypes 1569 */ 1570 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1571 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag); 1572 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1573 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1574 1575 /* 1576 * Multi-host Ioctl Prototypes 1577 */ 1578 static int sd_check_mhd(dev_t dev, int interval); 1579 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1580 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1581 static char *sd_sname(uchar_t status); 1582 static void sd_mhd_resvd_recover(void *arg); 1583 static void sd_resv_reclaim_thread(); 1584 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1585 static int sd_reserve_release(dev_t dev, int cmd); 1586 static void sd_rmv_resv_reclaim_req(dev_t dev); 1587 static void sd_mhd_reset_notify_cb(caddr_t arg); 1588 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1589 mhioc_inkeys_t *usrp, int flag); 1590 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1591 mhioc_inresvs_t *usrp, int flag); 1592 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1593 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1594 static int sd_mhdioc_release(dev_t dev); 1595 static int sd_mhdioc_register_devid(dev_t dev); 1596 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1597 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1598 1599 /* 1600 * SCSI removable prototypes 1601 */ 1602 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1603 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1604 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1605 static int sr_pause_resume(dev_t dev, int mode); 1606 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1607 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1610 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1611 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1612 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1613 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1614 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1615 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1616 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1617 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1618 static int sr_eject(dev_t dev); 1619 static void sr_ejected(register struct sd_lun *un); 1620 static int sr_check_wp(dev_t dev); 1621 static opaque_t sd_watch_request_submit(struct sd_lun *un); 1622 static int sd_check_media(dev_t dev, enum dkio_state state); 1623 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1624 static void sd_delayed_cv_broadcast(void *arg); 1625 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1626 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1627 1628 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page); 1629 1630 /* 1631 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1632 */ 1633 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag); 1634 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1635 static void sd_wm_cache_destructor(void *wm, void *un); 1636 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1637 daddr_t endb, ushort_t typ); 1638 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1639 daddr_t endb); 1640 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1641 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1642 static void sd_read_modify_write_task(void * arg); 1643 static int 1644 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1645 struct buf **bpp); 1646 1647 1648 /* 1649 * Function prototypes for failfast support. 1650 */ 1651 static void sd_failfast_flushq(struct sd_lun *un); 1652 static int sd_failfast_flushq_callback(struct buf *bp); 1653 1654 /* 1655 * Function prototypes to check for lsi devices 1656 */ 1657 static void sd_is_lsi(struct sd_lun *un); 1658 1659 /* 1660 * Function prototypes for partial DMA support 1661 */ 1662 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1663 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1664 1665 1666 /* Function prototypes for cmlb */ 1667 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1668 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1669 1670 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1671 1672 /* 1673 * For printing RMW warning message timely 1674 */ 1675 static void sd_rmw_msg_print_handler(void *arg); 1676 1677 /* 1678 * Constants for failfast support: 1679 * 1680 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1681 * failfast processing being performed. 1682 * 1683 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1684 * failfast processing on all bufs with B_FAILFAST set. 1685 */ 1686 1687 #define SD_FAILFAST_INACTIVE 0 1688 #define SD_FAILFAST_ACTIVE 1 1689 1690 /* 1691 * Bitmask to control behavior of buf(9S) flushes when a transition to 1692 * the failfast state occurs. Optional bits include: 1693 * 1694 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1695 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1696 * be flushed. 1697 * 1698 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1699 * driver, in addition to the regular wait queue. This includes the xbuf 1700 * queues. When clear, only the driver's wait queue will be flushed. 1701 */ 1702 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1703 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1704 1705 /* 1706 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1707 * to flush all queues within the driver. 1708 */ 1709 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1710 1711 1712 /* 1713 * SD Testing Fault Injection 1714 */ 1715 #ifdef SD_FAULT_INJECTION 1716 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1717 static void sd_faultinjection(struct scsi_pkt *pktp); 1718 static void sd_injection_log(char *buf, struct sd_lun *un); 1719 #endif 1720 1721 /* 1722 * Device driver ops vector 1723 */ 1724 static struct cb_ops sd_cb_ops = { 1725 sdopen, /* open */ 1726 sdclose, /* close */ 1727 sdstrategy, /* strategy */ 1728 nodev, /* print */ 1729 sddump, /* dump */ 1730 sdread, /* read */ 1731 sdwrite, /* write */ 1732 sdioctl, /* ioctl */ 1733 nodev, /* devmap */ 1734 nodev, /* mmap */ 1735 nodev, /* segmap */ 1736 nochpoll, /* poll */ 1737 sd_prop_op, /* cb_prop_op */ 1738 0, /* streamtab */ 1739 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1740 CB_REV, /* cb_rev */ 1741 sdaread, /* async I/O read entry point */ 1742 sdawrite /* async I/O write entry point */ 1743 }; 1744 1745 struct dev_ops sd_ops = { 1746 DEVO_REV, /* devo_rev, */ 1747 0, /* refcnt */ 1748 sdinfo, /* info */ 1749 nulldev, /* identify */ 1750 sdprobe, /* probe */ 1751 sdattach, /* attach */ 1752 sddetach, /* detach */ 1753 nodev, /* reset */ 1754 &sd_cb_ops, /* driver operations */ 1755 NULL, /* bus operations */ 1756 sdpower, /* power */ 1757 ddi_quiesce_not_needed, /* quiesce */ 1758 }; 1759 1760 /* 1761 * This is the loadable module wrapper. 1762 */ 1763 #include <sys/modctl.h> 1764 1765 #ifndef XPV_HVM_DRIVER 1766 static struct modldrv modldrv = { 1767 &mod_driverops, /* Type of module. This one is a driver */ 1768 SD_MODULE_NAME, /* Module name. */ 1769 &sd_ops /* driver ops */ 1770 }; 1771 1772 static struct modlinkage modlinkage = { 1773 MODREV_1, &modldrv, NULL 1774 }; 1775 1776 #else /* XPV_HVM_DRIVER */ 1777 static struct modlmisc modlmisc = { 1778 &mod_miscops, /* Type of module. This one is a misc */ 1779 "HVM " SD_MODULE_NAME, /* Module name. */ 1780 }; 1781 1782 static struct modlinkage modlinkage = { 1783 MODREV_1, &modlmisc, NULL 1784 }; 1785 1786 #endif /* XPV_HVM_DRIVER */ 1787 1788 static cmlb_tg_ops_t sd_tgops = { 1789 TG_DK_OPS_VERSION_1, 1790 sd_tg_rdwr, 1791 sd_tg_getinfo 1792 }; 1793 1794 static struct scsi_asq_key_strings sd_additional_codes[] = { 1795 0x81, 0, "Logical Unit is Reserved", 1796 0x85, 0, "Audio Address Not Valid", 1797 0xb6, 0, "Media Load Mechanism Failed", 1798 0xB9, 0, "Audio Play Operation Aborted", 1799 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1800 0x53, 2, "Medium removal prevented", 1801 0x6f, 0, "Authentication failed during key exchange", 1802 0x6f, 1, "Key not present", 1803 0x6f, 2, "Key not established", 1804 0x6f, 3, "Read without proper authentication", 1805 0x6f, 4, "Mismatched region to this logical unit", 1806 0x6f, 5, "Region reset count error", 1807 0xffff, 0x0, NULL 1808 }; 1809 1810 1811 /* 1812 * Struct for passing printing information for sense data messages 1813 */ 1814 struct sd_sense_info { 1815 int ssi_severity; 1816 int ssi_pfa_flag; 1817 }; 1818 1819 /* 1820 * Table of function pointers for iostart-side routines. Separate "chains" 1821 * of layered function calls are formed by placing the function pointers 1822 * sequentially in the desired order. Functions are called according to an 1823 * incrementing table index ordering. The last function in each chain must 1824 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1825 * in the sd_iodone_chain[] array. 1826 * 1827 * Note: It may seem more natural to organize both the iostart and iodone 1828 * functions together, into an array of structures (or some similar 1829 * organization) with a common index, rather than two separate arrays which 1830 * must be maintained in synchronization. The purpose of this division is 1831 * to achieve improved performance: individual arrays allows for more 1832 * effective cache line utilization on certain platforms. 1833 */ 1834 1835 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1836 1837 1838 static sd_chain_t sd_iostart_chain[] = { 1839 1840 /* Chain for buf IO for disk drive targets (PM enabled) */ 1841 sd_mapblockaddr_iostart, /* Index: 0 */ 1842 sd_pm_iostart, /* Index: 1 */ 1843 sd_core_iostart, /* Index: 2 */ 1844 1845 /* Chain for buf IO for disk drive targets (PM disabled) */ 1846 sd_mapblockaddr_iostart, /* Index: 3 */ 1847 sd_core_iostart, /* Index: 4 */ 1848 1849 /* 1850 * Chain for buf IO for removable-media or large sector size 1851 * disk drive targets with RMW needed (PM enabled) 1852 */ 1853 sd_mapblockaddr_iostart, /* Index: 5 */ 1854 sd_mapblocksize_iostart, /* Index: 6 */ 1855 sd_pm_iostart, /* Index: 7 */ 1856 sd_core_iostart, /* Index: 8 */ 1857 1858 /* 1859 * Chain for buf IO for removable-media or large sector size 1860 * disk drive targets with RMW needed (PM disabled) 1861 */ 1862 sd_mapblockaddr_iostart, /* Index: 9 */ 1863 sd_mapblocksize_iostart, /* Index: 10 */ 1864 sd_core_iostart, /* Index: 11 */ 1865 1866 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1867 sd_mapblockaddr_iostart, /* Index: 12 */ 1868 sd_checksum_iostart, /* Index: 13 */ 1869 sd_pm_iostart, /* Index: 14 */ 1870 sd_core_iostart, /* Index: 15 */ 1871 1872 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1873 sd_mapblockaddr_iostart, /* Index: 16 */ 1874 sd_checksum_iostart, /* Index: 17 */ 1875 sd_core_iostart, /* Index: 18 */ 1876 1877 /* Chain for USCSI commands (all targets) */ 1878 sd_pm_iostart, /* Index: 19 */ 1879 sd_core_iostart, /* Index: 20 */ 1880 1881 /* Chain for checksumming USCSI commands (all targets) */ 1882 sd_checksum_uscsi_iostart, /* Index: 21 */ 1883 sd_pm_iostart, /* Index: 22 */ 1884 sd_core_iostart, /* Index: 23 */ 1885 1886 /* Chain for "direct" USCSI commands (all targets) */ 1887 sd_core_iostart, /* Index: 24 */ 1888 1889 /* Chain for "direct priority" USCSI commands (all targets) */ 1890 sd_core_iostart, /* Index: 25 */ 1891 1892 /* 1893 * Chain for buf IO for large sector size disk drive targets 1894 * with RMW needed with checksumming (PM enabled) 1895 */ 1896 sd_mapblockaddr_iostart, /* Index: 26 */ 1897 sd_mapblocksize_iostart, /* Index: 27 */ 1898 sd_checksum_iostart, /* Index: 28 */ 1899 sd_pm_iostart, /* Index: 29 */ 1900 sd_core_iostart, /* Index: 30 */ 1901 1902 /* 1903 * Chain for buf IO for large sector size disk drive targets 1904 * with RMW needed with checksumming (PM disabled) 1905 */ 1906 sd_mapblockaddr_iostart, /* Index: 31 */ 1907 sd_mapblocksize_iostart, /* Index: 32 */ 1908 sd_checksum_iostart, /* Index: 33 */ 1909 sd_core_iostart, /* Index: 34 */ 1910 1911 }; 1912 1913 /* 1914 * Macros to locate the first function of each iostart chain in the 1915 * sd_iostart_chain[] array. These are located by the index in the array. 1916 */ 1917 #define SD_CHAIN_DISK_IOSTART 0 1918 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1919 #define SD_CHAIN_MSS_DISK_IOSTART 5 1920 #define SD_CHAIN_RMMEDIA_IOSTART 5 1921 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9 1922 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1923 #define SD_CHAIN_CHKSUM_IOSTART 12 1924 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1925 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1926 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1927 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1928 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1929 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26 1930 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31 1931 1932 1933 /* 1934 * Table of function pointers for the iodone-side routines for the driver- 1935 * internal layering mechanism. The calling sequence for iodone routines 1936 * uses a decrementing table index, so the last routine called in a chain 1937 * must be at the lowest array index location for that chain. The last 1938 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1939 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1940 * of the functions in an iodone side chain must correspond to the ordering 1941 * of the iostart routines for that chain. Note that there is no iodone 1942 * side routine that corresponds to sd_core_iostart(), so there is no 1943 * entry in the table for this. 1944 */ 1945 1946 static sd_chain_t sd_iodone_chain[] = { 1947 1948 /* Chain for buf IO for disk drive targets (PM enabled) */ 1949 sd_buf_iodone, /* Index: 0 */ 1950 sd_mapblockaddr_iodone, /* Index: 1 */ 1951 sd_pm_iodone, /* Index: 2 */ 1952 1953 /* Chain for buf IO for disk drive targets (PM disabled) */ 1954 sd_buf_iodone, /* Index: 3 */ 1955 sd_mapblockaddr_iodone, /* Index: 4 */ 1956 1957 /* 1958 * Chain for buf IO for removable-media or large sector size 1959 * disk drive targets with RMW needed (PM enabled) 1960 */ 1961 sd_buf_iodone, /* Index: 5 */ 1962 sd_mapblockaddr_iodone, /* Index: 6 */ 1963 sd_mapblocksize_iodone, /* Index: 7 */ 1964 sd_pm_iodone, /* Index: 8 */ 1965 1966 /* 1967 * Chain for buf IO for removable-media or large sector size 1968 * disk drive targets with RMW needed (PM disabled) 1969 */ 1970 sd_buf_iodone, /* Index: 9 */ 1971 sd_mapblockaddr_iodone, /* Index: 10 */ 1972 sd_mapblocksize_iodone, /* Index: 11 */ 1973 1974 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1975 sd_buf_iodone, /* Index: 12 */ 1976 sd_mapblockaddr_iodone, /* Index: 13 */ 1977 sd_checksum_iodone, /* Index: 14 */ 1978 sd_pm_iodone, /* Index: 15 */ 1979 1980 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1981 sd_buf_iodone, /* Index: 16 */ 1982 sd_mapblockaddr_iodone, /* Index: 17 */ 1983 sd_checksum_iodone, /* Index: 18 */ 1984 1985 /* Chain for USCSI commands (non-checksum targets) */ 1986 sd_uscsi_iodone, /* Index: 19 */ 1987 sd_pm_iodone, /* Index: 20 */ 1988 1989 /* Chain for USCSI commands (checksum targets) */ 1990 sd_uscsi_iodone, /* Index: 21 */ 1991 sd_checksum_uscsi_iodone, /* Index: 22 */ 1992 sd_pm_iodone, /* Index: 22 */ 1993 1994 /* Chain for "direct" USCSI commands (all targets) */ 1995 sd_uscsi_iodone, /* Index: 24 */ 1996 1997 /* Chain for "direct priority" USCSI commands (all targets) */ 1998 sd_uscsi_iodone, /* Index: 25 */ 1999 2000 /* 2001 * Chain for buf IO for large sector size disk drive targets 2002 * with checksumming (PM enabled) 2003 */ 2004 sd_buf_iodone, /* Index: 26 */ 2005 sd_mapblockaddr_iodone, /* Index: 27 */ 2006 sd_mapblocksize_iodone, /* Index: 28 */ 2007 sd_checksum_iodone, /* Index: 29 */ 2008 sd_pm_iodone, /* Index: 30 */ 2009 2010 /* 2011 * Chain for buf IO for large sector size disk drive targets 2012 * with checksumming (PM disabled) 2013 */ 2014 sd_buf_iodone, /* Index: 31 */ 2015 sd_mapblockaddr_iodone, /* Index: 32 */ 2016 sd_mapblocksize_iodone, /* Index: 33 */ 2017 sd_checksum_iodone, /* Index: 34 */ 2018 }; 2019 2020 2021 /* 2022 * Macros to locate the "first" function in the sd_iodone_chain[] array for 2023 * each iodone-side chain. These are located by the array index, but as the 2024 * iodone side functions are called in a decrementing-index order, the 2025 * highest index number in each chain must be specified (as these correspond 2026 * to the first function in the iodone chain that will be called by the core 2027 * at IO completion time). 2028 */ 2029 2030 #define SD_CHAIN_DISK_IODONE 2 2031 #define SD_CHAIN_DISK_IODONE_NO_PM 4 2032 #define SD_CHAIN_RMMEDIA_IODONE 8 2033 #define SD_CHAIN_MSS_DISK_IODONE 8 2034 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 2035 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11 2036 #define SD_CHAIN_CHKSUM_IODONE 15 2037 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 2038 #define SD_CHAIN_USCSI_CMD_IODONE 20 2039 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 2040 #define SD_CHAIN_DIRECT_CMD_IODONE 24 2041 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 2042 #define SD_CHAIN_MSS_CHKSUM_IODONE 30 2043 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34 2044 2045 2046 2047 /* 2048 * Array to map a layering chain index to the appropriate initpkt routine. 2049 * The redundant entries are present so that the index used for accessing 2050 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2051 * with this table as well. 2052 */ 2053 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 2054 2055 static sd_initpkt_t sd_initpkt_map[] = { 2056 2057 /* Chain for buf IO for disk drive targets (PM enabled) */ 2058 sd_initpkt_for_buf, /* Index: 0 */ 2059 sd_initpkt_for_buf, /* Index: 1 */ 2060 sd_initpkt_for_buf, /* Index: 2 */ 2061 2062 /* Chain for buf IO for disk drive targets (PM disabled) */ 2063 sd_initpkt_for_buf, /* Index: 3 */ 2064 sd_initpkt_for_buf, /* Index: 4 */ 2065 2066 /* 2067 * Chain for buf IO for removable-media or large sector size 2068 * disk drive targets (PM enabled) 2069 */ 2070 sd_initpkt_for_buf, /* Index: 5 */ 2071 sd_initpkt_for_buf, /* Index: 6 */ 2072 sd_initpkt_for_buf, /* Index: 7 */ 2073 sd_initpkt_for_buf, /* Index: 8 */ 2074 2075 /* 2076 * Chain for buf IO for removable-media or large sector size 2077 * disk drive targets (PM disabled) 2078 */ 2079 sd_initpkt_for_buf, /* Index: 9 */ 2080 sd_initpkt_for_buf, /* Index: 10 */ 2081 sd_initpkt_for_buf, /* Index: 11 */ 2082 2083 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2084 sd_initpkt_for_buf, /* Index: 12 */ 2085 sd_initpkt_for_buf, /* Index: 13 */ 2086 sd_initpkt_for_buf, /* Index: 14 */ 2087 sd_initpkt_for_buf, /* Index: 15 */ 2088 2089 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2090 sd_initpkt_for_buf, /* Index: 16 */ 2091 sd_initpkt_for_buf, /* Index: 17 */ 2092 sd_initpkt_for_buf, /* Index: 18 */ 2093 2094 /* Chain for USCSI commands (non-checksum targets) */ 2095 sd_initpkt_for_uscsi, /* Index: 19 */ 2096 sd_initpkt_for_uscsi, /* Index: 20 */ 2097 2098 /* Chain for USCSI commands (checksum targets) */ 2099 sd_initpkt_for_uscsi, /* Index: 21 */ 2100 sd_initpkt_for_uscsi, /* Index: 22 */ 2101 sd_initpkt_for_uscsi, /* Index: 22 */ 2102 2103 /* Chain for "direct" USCSI commands (all targets) */ 2104 sd_initpkt_for_uscsi, /* Index: 24 */ 2105 2106 /* Chain for "direct priority" USCSI commands (all targets) */ 2107 sd_initpkt_for_uscsi, /* Index: 25 */ 2108 2109 /* 2110 * Chain for buf IO for large sector size disk drive targets 2111 * with checksumming (PM enabled) 2112 */ 2113 sd_initpkt_for_buf, /* Index: 26 */ 2114 sd_initpkt_for_buf, /* Index: 27 */ 2115 sd_initpkt_for_buf, /* Index: 28 */ 2116 sd_initpkt_for_buf, /* Index: 29 */ 2117 sd_initpkt_for_buf, /* Index: 30 */ 2118 2119 /* 2120 * Chain for buf IO for large sector size disk drive targets 2121 * with checksumming (PM disabled) 2122 */ 2123 sd_initpkt_for_buf, /* Index: 31 */ 2124 sd_initpkt_for_buf, /* Index: 32 */ 2125 sd_initpkt_for_buf, /* Index: 33 */ 2126 sd_initpkt_for_buf, /* Index: 34 */ 2127 }; 2128 2129 2130 /* 2131 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2132 * The redundant entries are present so that the index used for accessing 2133 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2134 * with this table as well. 2135 */ 2136 typedef void (*sd_destroypkt_t)(struct buf *); 2137 2138 static sd_destroypkt_t sd_destroypkt_map[] = { 2139 2140 /* Chain for buf IO for disk drive targets (PM enabled) */ 2141 sd_destroypkt_for_buf, /* Index: 0 */ 2142 sd_destroypkt_for_buf, /* Index: 1 */ 2143 sd_destroypkt_for_buf, /* Index: 2 */ 2144 2145 /* Chain for buf IO for disk drive targets (PM disabled) */ 2146 sd_destroypkt_for_buf, /* Index: 3 */ 2147 sd_destroypkt_for_buf, /* Index: 4 */ 2148 2149 /* 2150 * Chain for buf IO for removable-media or large sector size 2151 * disk drive targets (PM enabled) 2152 */ 2153 sd_destroypkt_for_buf, /* Index: 5 */ 2154 sd_destroypkt_for_buf, /* Index: 6 */ 2155 sd_destroypkt_for_buf, /* Index: 7 */ 2156 sd_destroypkt_for_buf, /* Index: 8 */ 2157 2158 /* 2159 * Chain for buf IO for removable-media or large sector size 2160 * disk drive targets (PM disabled) 2161 */ 2162 sd_destroypkt_for_buf, /* Index: 9 */ 2163 sd_destroypkt_for_buf, /* Index: 10 */ 2164 sd_destroypkt_for_buf, /* Index: 11 */ 2165 2166 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2167 sd_destroypkt_for_buf, /* Index: 12 */ 2168 sd_destroypkt_for_buf, /* Index: 13 */ 2169 sd_destroypkt_for_buf, /* Index: 14 */ 2170 sd_destroypkt_for_buf, /* Index: 15 */ 2171 2172 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2173 sd_destroypkt_for_buf, /* Index: 16 */ 2174 sd_destroypkt_for_buf, /* Index: 17 */ 2175 sd_destroypkt_for_buf, /* Index: 18 */ 2176 2177 /* Chain for USCSI commands (non-checksum targets) */ 2178 sd_destroypkt_for_uscsi, /* Index: 19 */ 2179 sd_destroypkt_for_uscsi, /* Index: 20 */ 2180 2181 /* Chain for USCSI commands (checksum targets) */ 2182 sd_destroypkt_for_uscsi, /* Index: 21 */ 2183 sd_destroypkt_for_uscsi, /* Index: 22 */ 2184 sd_destroypkt_for_uscsi, /* Index: 22 */ 2185 2186 /* Chain for "direct" USCSI commands (all targets) */ 2187 sd_destroypkt_for_uscsi, /* Index: 24 */ 2188 2189 /* Chain for "direct priority" USCSI commands (all targets) */ 2190 sd_destroypkt_for_uscsi, /* Index: 25 */ 2191 2192 /* 2193 * Chain for buf IO for large sector size disk drive targets 2194 * with checksumming (PM disabled) 2195 */ 2196 sd_destroypkt_for_buf, /* Index: 26 */ 2197 sd_destroypkt_for_buf, /* Index: 27 */ 2198 sd_destroypkt_for_buf, /* Index: 28 */ 2199 sd_destroypkt_for_buf, /* Index: 29 */ 2200 sd_destroypkt_for_buf, /* Index: 30 */ 2201 2202 /* 2203 * Chain for buf IO for large sector size disk drive targets 2204 * with checksumming (PM enabled) 2205 */ 2206 sd_destroypkt_for_buf, /* Index: 31 */ 2207 sd_destroypkt_for_buf, /* Index: 32 */ 2208 sd_destroypkt_for_buf, /* Index: 33 */ 2209 sd_destroypkt_for_buf, /* Index: 34 */ 2210 }; 2211 2212 2213 2214 /* 2215 * Array to map a layering chain index to the appropriate chain "type". 2216 * The chain type indicates a specific property/usage of the chain. 2217 * The redundant entries are present so that the index used for accessing 2218 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2219 * with this table as well. 2220 */ 2221 2222 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2223 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2224 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2225 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2226 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2227 /* (for error recovery) */ 2228 2229 static int sd_chain_type_map[] = { 2230 2231 /* Chain for buf IO for disk drive targets (PM enabled) */ 2232 SD_CHAIN_BUFIO, /* Index: 0 */ 2233 SD_CHAIN_BUFIO, /* Index: 1 */ 2234 SD_CHAIN_BUFIO, /* Index: 2 */ 2235 2236 /* Chain for buf IO for disk drive targets (PM disabled) */ 2237 SD_CHAIN_BUFIO, /* Index: 3 */ 2238 SD_CHAIN_BUFIO, /* Index: 4 */ 2239 2240 /* 2241 * Chain for buf IO for removable-media or large sector size 2242 * disk drive targets (PM enabled) 2243 */ 2244 SD_CHAIN_BUFIO, /* Index: 5 */ 2245 SD_CHAIN_BUFIO, /* Index: 6 */ 2246 SD_CHAIN_BUFIO, /* Index: 7 */ 2247 SD_CHAIN_BUFIO, /* Index: 8 */ 2248 2249 /* 2250 * Chain for buf IO for removable-media or large sector size 2251 * disk drive targets (PM disabled) 2252 */ 2253 SD_CHAIN_BUFIO, /* Index: 9 */ 2254 SD_CHAIN_BUFIO, /* Index: 10 */ 2255 SD_CHAIN_BUFIO, /* Index: 11 */ 2256 2257 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2258 SD_CHAIN_BUFIO, /* Index: 12 */ 2259 SD_CHAIN_BUFIO, /* Index: 13 */ 2260 SD_CHAIN_BUFIO, /* Index: 14 */ 2261 SD_CHAIN_BUFIO, /* Index: 15 */ 2262 2263 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2264 SD_CHAIN_BUFIO, /* Index: 16 */ 2265 SD_CHAIN_BUFIO, /* Index: 17 */ 2266 SD_CHAIN_BUFIO, /* Index: 18 */ 2267 2268 /* Chain for USCSI commands (non-checksum targets) */ 2269 SD_CHAIN_USCSI, /* Index: 19 */ 2270 SD_CHAIN_USCSI, /* Index: 20 */ 2271 2272 /* Chain for USCSI commands (checksum targets) */ 2273 SD_CHAIN_USCSI, /* Index: 21 */ 2274 SD_CHAIN_USCSI, /* Index: 22 */ 2275 SD_CHAIN_USCSI, /* Index: 23 */ 2276 2277 /* Chain for "direct" USCSI commands (all targets) */ 2278 SD_CHAIN_DIRECT, /* Index: 24 */ 2279 2280 /* Chain for "direct priority" USCSI commands (all targets) */ 2281 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2282 2283 /* 2284 * Chain for buf IO for large sector size disk drive targets 2285 * with checksumming (PM enabled) 2286 */ 2287 SD_CHAIN_BUFIO, /* Index: 26 */ 2288 SD_CHAIN_BUFIO, /* Index: 27 */ 2289 SD_CHAIN_BUFIO, /* Index: 28 */ 2290 SD_CHAIN_BUFIO, /* Index: 29 */ 2291 SD_CHAIN_BUFIO, /* Index: 30 */ 2292 2293 /* 2294 * Chain for buf IO for large sector size disk drive targets 2295 * with checksumming (PM disabled) 2296 */ 2297 SD_CHAIN_BUFIO, /* Index: 31 */ 2298 SD_CHAIN_BUFIO, /* Index: 32 */ 2299 SD_CHAIN_BUFIO, /* Index: 33 */ 2300 SD_CHAIN_BUFIO, /* Index: 34 */ 2301 }; 2302 2303 2304 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2305 #define SD_IS_BUFIO(xp) \ 2306 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2307 2308 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2309 #define SD_IS_DIRECT_PRIORITY(xp) \ 2310 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2311 2312 2313 2314 /* 2315 * Struct, array, and macros to map a specific chain to the appropriate 2316 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2317 * 2318 * The sd_chain_index_map[] array is used at attach time to set the various 2319 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2320 * chain to be used with the instance. This allows different instances to use 2321 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2322 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2323 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2324 * dynamically & without the use of locking; and (2) a layer may update the 2325 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2326 * to allow for deferred processing of an IO within the same chain from a 2327 * different execution context. 2328 */ 2329 2330 struct sd_chain_index { 2331 int sci_iostart_index; 2332 int sci_iodone_index; 2333 }; 2334 2335 static struct sd_chain_index sd_chain_index_map[] = { 2336 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2337 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2338 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2339 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2340 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2341 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2342 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2343 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2344 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2345 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2346 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE }, 2347 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM }, 2348 2349 }; 2350 2351 2352 /* 2353 * The following are indexes into the sd_chain_index_map[] array. 2354 */ 2355 2356 /* un->un_buf_chain_type must be set to one of these */ 2357 #define SD_CHAIN_INFO_DISK 0 2358 #define SD_CHAIN_INFO_DISK_NO_PM 1 2359 #define SD_CHAIN_INFO_RMMEDIA 2 2360 #define SD_CHAIN_INFO_MSS_DISK 2 2361 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2362 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3 2363 #define SD_CHAIN_INFO_CHKSUM 4 2364 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2365 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10 2366 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11 2367 2368 /* un->un_uscsi_chain_type must be set to one of these */ 2369 #define SD_CHAIN_INFO_USCSI_CMD 6 2370 /* USCSI with PM disabled is the same as DIRECT */ 2371 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2372 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2373 2374 /* un->un_direct_chain_type must be set to one of these */ 2375 #define SD_CHAIN_INFO_DIRECT_CMD 8 2376 2377 /* un->un_priority_chain_type must be set to one of these */ 2378 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2379 2380 /* size for devid inquiries */ 2381 #define MAX_INQUIRY_SIZE 0xF0 2382 2383 /* 2384 * Macros used by functions to pass a given buf(9S) struct along to the 2385 * next function in the layering chain for further processing. 2386 * 2387 * In the following macros, passing more than three arguments to the called 2388 * routines causes the optimizer for the SPARC compiler to stop doing tail 2389 * call elimination which results in significant performance degradation. 2390 */ 2391 #define SD_BEGIN_IOSTART(index, un, bp) \ 2392 ((*(sd_iostart_chain[index]))(index, un, bp)) 2393 2394 #define SD_BEGIN_IODONE(index, un, bp) \ 2395 ((*(sd_iodone_chain[index]))(index, un, bp)) 2396 2397 #define SD_NEXT_IOSTART(index, un, bp) \ 2398 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2399 2400 #define SD_NEXT_IODONE(index, un, bp) \ 2401 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2402 2403 /* 2404 * Function: _init 2405 * 2406 * Description: This is the driver _init(9E) entry point. 2407 * 2408 * Return Code: Returns the value from mod_install(9F) or 2409 * ddi_soft_state_init(9F) as appropriate. 2410 * 2411 * Context: Called when driver module loaded. 2412 */ 2413 2414 int 2415 _init(void) 2416 { 2417 int err; 2418 2419 /* establish driver name from module name */ 2420 sd_label = (char *)mod_modname(&modlinkage); 2421 2422 #ifndef XPV_HVM_DRIVER 2423 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2424 SD_MAXUNIT); 2425 if (err != 0) { 2426 return (err); 2427 } 2428 2429 #else /* XPV_HVM_DRIVER */ 2430 /* Remove the leading "hvm_" from the module name */ 2431 ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0); 2432 sd_label += strlen("hvm_"); 2433 2434 #endif /* XPV_HVM_DRIVER */ 2435 2436 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2437 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2438 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2439 2440 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2441 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2442 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2443 2444 /* 2445 * it's ok to init here even for fibre device 2446 */ 2447 sd_scsi_probe_cache_init(); 2448 2449 sd_scsi_target_lun_init(); 2450 2451 /* 2452 * Creating taskq before mod_install ensures that all callers (threads) 2453 * that enter the module after a successful mod_install encounter 2454 * a valid taskq. 2455 */ 2456 sd_taskq_create(); 2457 2458 err = mod_install(&modlinkage); 2459 if (err != 0) { 2460 /* delete taskq if install fails */ 2461 sd_taskq_delete(); 2462 2463 mutex_destroy(&sd_detach_mutex); 2464 mutex_destroy(&sd_log_mutex); 2465 mutex_destroy(&sd_label_mutex); 2466 2467 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2468 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2469 cv_destroy(&sd_tr.srq_inprocess_cv); 2470 2471 sd_scsi_probe_cache_fini(); 2472 2473 sd_scsi_target_lun_fini(); 2474 2475 #ifndef XPV_HVM_DRIVER 2476 ddi_soft_state_fini(&sd_state); 2477 #endif /* !XPV_HVM_DRIVER */ 2478 return (err); 2479 } 2480 2481 return (err); 2482 } 2483 2484 2485 /* 2486 * Function: _fini 2487 * 2488 * Description: This is the driver _fini(9E) entry point. 2489 * 2490 * Return Code: Returns the value from mod_remove(9F) 2491 * 2492 * Context: Called when driver module is unloaded. 2493 */ 2494 2495 int 2496 _fini(void) 2497 { 2498 int err; 2499 2500 if ((err = mod_remove(&modlinkage)) != 0) { 2501 return (err); 2502 } 2503 2504 sd_taskq_delete(); 2505 2506 mutex_destroy(&sd_detach_mutex); 2507 mutex_destroy(&sd_log_mutex); 2508 mutex_destroy(&sd_label_mutex); 2509 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2510 2511 sd_scsi_probe_cache_fini(); 2512 2513 sd_scsi_target_lun_fini(); 2514 2515 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2516 cv_destroy(&sd_tr.srq_inprocess_cv); 2517 2518 #ifndef XPV_HVM_DRIVER 2519 ddi_soft_state_fini(&sd_state); 2520 #endif /* !XPV_HVM_DRIVER */ 2521 2522 return (err); 2523 } 2524 2525 2526 /* 2527 * Function: _info 2528 * 2529 * Description: This is the driver _info(9E) entry point. 2530 * 2531 * Arguments: modinfop - pointer to the driver modinfo structure 2532 * 2533 * Return Code: Returns the value from mod_info(9F). 2534 * 2535 * Context: Kernel thread context 2536 */ 2537 2538 int 2539 _info(struct modinfo *modinfop) 2540 { 2541 return (mod_info(&modlinkage, modinfop)); 2542 } 2543 2544 2545 /* 2546 * The following routines implement the driver message logging facility. 2547 * They provide component- and level- based debug output filtering. 2548 * Output may also be restricted to messages for a single instance by 2549 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2550 * to NULL, then messages for all instances are printed. 2551 * 2552 * These routines have been cloned from each other due to the language 2553 * constraints of macros and variable argument list processing. 2554 */ 2555 2556 2557 /* 2558 * Function: sd_log_err 2559 * 2560 * Description: This routine is called by the SD_ERROR macro for debug 2561 * logging of error conditions. 2562 * 2563 * Arguments: comp - driver component being logged 2564 * dev - pointer to driver info structure 2565 * fmt - error string and format to be logged 2566 */ 2567 2568 static void 2569 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2570 { 2571 va_list ap; 2572 dev_info_t *dev; 2573 2574 ASSERT(un != NULL); 2575 dev = SD_DEVINFO(un); 2576 ASSERT(dev != NULL); 2577 2578 /* 2579 * Filter messages based on the global component and level masks. 2580 * Also print if un matches the value of sd_debug_un, or if 2581 * sd_debug_un is set to NULL. 2582 */ 2583 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2584 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2585 mutex_enter(&sd_log_mutex); 2586 va_start(ap, fmt); 2587 (void) vsprintf(sd_log_buf, fmt, ap); 2588 va_end(ap); 2589 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2590 mutex_exit(&sd_log_mutex); 2591 } 2592 #ifdef SD_FAULT_INJECTION 2593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2594 if (un->sd_injection_mask & comp) { 2595 mutex_enter(&sd_log_mutex); 2596 va_start(ap, fmt); 2597 (void) vsprintf(sd_log_buf, fmt, ap); 2598 va_end(ap); 2599 sd_injection_log(sd_log_buf, un); 2600 mutex_exit(&sd_log_mutex); 2601 } 2602 #endif 2603 } 2604 2605 2606 /* 2607 * Function: sd_log_info 2608 * 2609 * Description: This routine is called by the SD_INFO macro for debug 2610 * logging of general purpose informational conditions. 2611 * 2612 * Arguments: comp - driver component being logged 2613 * dev - pointer to driver info structure 2614 * fmt - info string and format to be logged 2615 */ 2616 2617 static void 2618 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2619 { 2620 va_list ap; 2621 dev_info_t *dev; 2622 2623 ASSERT(un != NULL); 2624 dev = SD_DEVINFO(un); 2625 ASSERT(dev != NULL); 2626 2627 /* 2628 * Filter messages based on the global component and level masks. 2629 * Also print if un matches the value of sd_debug_un, or if 2630 * sd_debug_un is set to NULL. 2631 */ 2632 if ((sd_component_mask & component) && 2633 (sd_level_mask & SD_LOGMASK_INFO) && 2634 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2635 mutex_enter(&sd_log_mutex); 2636 va_start(ap, fmt); 2637 (void) vsprintf(sd_log_buf, fmt, ap); 2638 va_end(ap); 2639 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2640 mutex_exit(&sd_log_mutex); 2641 } 2642 #ifdef SD_FAULT_INJECTION 2643 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2644 if (un->sd_injection_mask & component) { 2645 mutex_enter(&sd_log_mutex); 2646 va_start(ap, fmt); 2647 (void) vsprintf(sd_log_buf, fmt, ap); 2648 va_end(ap); 2649 sd_injection_log(sd_log_buf, un); 2650 mutex_exit(&sd_log_mutex); 2651 } 2652 #endif 2653 } 2654 2655 2656 /* 2657 * Function: sd_log_trace 2658 * 2659 * Description: This routine is called by the SD_TRACE macro for debug 2660 * logging of trace conditions (i.e. function entry/exit). 2661 * 2662 * Arguments: comp - driver component being logged 2663 * dev - pointer to driver info structure 2664 * fmt - trace string and format to be logged 2665 */ 2666 2667 static void 2668 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2669 { 2670 va_list ap; 2671 dev_info_t *dev; 2672 2673 ASSERT(un != NULL); 2674 dev = SD_DEVINFO(un); 2675 ASSERT(dev != NULL); 2676 2677 /* 2678 * Filter messages based on the global component and level masks. 2679 * Also print if un matches the value of sd_debug_un, or if 2680 * sd_debug_un is set to NULL. 2681 */ 2682 if ((sd_component_mask & component) && 2683 (sd_level_mask & SD_LOGMASK_TRACE) && 2684 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2685 mutex_enter(&sd_log_mutex); 2686 va_start(ap, fmt); 2687 (void) vsprintf(sd_log_buf, fmt, ap); 2688 va_end(ap); 2689 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2690 mutex_exit(&sd_log_mutex); 2691 } 2692 #ifdef SD_FAULT_INJECTION 2693 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2694 if (un->sd_injection_mask & component) { 2695 mutex_enter(&sd_log_mutex); 2696 va_start(ap, fmt); 2697 (void) vsprintf(sd_log_buf, fmt, ap); 2698 va_end(ap); 2699 sd_injection_log(sd_log_buf, un); 2700 mutex_exit(&sd_log_mutex); 2701 } 2702 #endif 2703 } 2704 2705 2706 /* 2707 * Function: sdprobe 2708 * 2709 * Description: This is the driver probe(9e) entry point function. 2710 * 2711 * Arguments: devi - opaque device info handle 2712 * 2713 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2714 * DDI_PROBE_FAILURE: If the probe failed. 2715 * DDI_PROBE_PARTIAL: If the instance is not present now, 2716 * but may be present in the future. 2717 */ 2718 2719 static int 2720 sdprobe(dev_info_t *devi) 2721 { 2722 struct scsi_device *devp; 2723 int rval; 2724 #ifndef XPV_HVM_DRIVER 2725 int instance = ddi_get_instance(devi); 2726 #endif /* !XPV_HVM_DRIVER */ 2727 2728 /* 2729 * if it wasn't for pln, sdprobe could actually be nulldev 2730 * in the "__fibre" case. 2731 */ 2732 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2733 return (DDI_PROBE_DONTCARE); 2734 } 2735 2736 devp = ddi_get_driver_private(devi); 2737 2738 if (devp == NULL) { 2739 /* Ooops... nexus driver is mis-configured... */ 2740 return (DDI_PROBE_FAILURE); 2741 } 2742 2743 #ifndef XPV_HVM_DRIVER 2744 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2745 return (DDI_PROBE_PARTIAL); 2746 } 2747 #endif /* !XPV_HVM_DRIVER */ 2748 2749 /* 2750 * Call the SCSA utility probe routine to see if we actually 2751 * have a target at this SCSI nexus. 2752 */ 2753 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2754 case SCSIPROBE_EXISTS: 2755 switch (devp->sd_inq->inq_dtype) { 2756 case DTYPE_DIRECT: 2757 rval = DDI_PROBE_SUCCESS; 2758 break; 2759 case DTYPE_RODIRECT: 2760 /* CDs etc. Can be removable media */ 2761 rval = DDI_PROBE_SUCCESS; 2762 break; 2763 case DTYPE_OPTICAL: 2764 /* 2765 * Rewritable optical driver HP115AA 2766 * Can also be removable media 2767 */ 2768 2769 /* 2770 * Do not attempt to bind to DTYPE_OPTICAL if 2771 * pre solaris 9 sparc sd behavior is required 2772 * 2773 * If first time through and sd_dtype_optical_bind 2774 * has not been set in /etc/system check properties 2775 */ 2776 2777 if (sd_dtype_optical_bind < 0) { 2778 sd_dtype_optical_bind = ddi_prop_get_int 2779 (DDI_DEV_T_ANY, devi, 0, 2780 "optical-device-bind", 1); 2781 } 2782 2783 if (sd_dtype_optical_bind == 0) { 2784 rval = DDI_PROBE_FAILURE; 2785 } else { 2786 rval = DDI_PROBE_SUCCESS; 2787 } 2788 break; 2789 2790 case DTYPE_NOTPRESENT: 2791 default: 2792 rval = DDI_PROBE_FAILURE; 2793 break; 2794 } 2795 break; 2796 default: 2797 rval = DDI_PROBE_PARTIAL; 2798 break; 2799 } 2800 2801 /* 2802 * This routine checks for resource allocation prior to freeing, 2803 * so it will take care of the "smart probing" case where a 2804 * scsi_probe() may or may not have been issued and will *not* 2805 * free previously-freed resources. 2806 */ 2807 scsi_unprobe(devp); 2808 return (rval); 2809 } 2810 2811 2812 /* 2813 * Function: sdinfo 2814 * 2815 * Description: This is the driver getinfo(9e) entry point function. 2816 * Given the device number, return the devinfo pointer from 2817 * the scsi_device structure or the instance number 2818 * associated with the dev_t. 2819 * 2820 * Arguments: dip - pointer to device info structure 2821 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2822 * DDI_INFO_DEVT2INSTANCE) 2823 * arg - driver dev_t 2824 * resultp - user buffer for request response 2825 * 2826 * Return Code: DDI_SUCCESS 2827 * DDI_FAILURE 2828 */ 2829 /* ARGSUSED */ 2830 static int 2831 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2832 { 2833 struct sd_lun *un; 2834 dev_t dev; 2835 int instance; 2836 int error; 2837 2838 switch (infocmd) { 2839 case DDI_INFO_DEVT2DEVINFO: 2840 dev = (dev_t)arg; 2841 instance = SDUNIT(dev); 2842 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2843 return (DDI_FAILURE); 2844 } 2845 *result = (void *) SD_DEVINFO(un); 2846 error = DDI_SUCCESS; 2847 break; 2848 case DDI_INFO_DEVT2INSTANCE: 2849 dev = (dev_t)arg; 2850 instance = SDUNIT(dev); 2851 *result = (void *)(uintptr_t)instance; 2852 error = DDI_SUCCESS; 2853 break; 2854 default: 2855 error = DDI_FAILURE; 2856 } 2857 return (error); 2858 } 2859 2860 /* 2861 * Function: sd_prop_op 2862 * 2863 * Description: This is the driver prop_op(9e) entry point function. 2864 * Return the number of blocks for the partition in question 2865 * or forward the request to the property facilities. 2866 * 2867 * Arguments: dev - device number 2868 * dip - pointer to device info structure 2869 * prop_op - property operator 2870 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2871 * name - pointer to property name 2872 * valuep - pointer or address of the user buffer 2873 * lengthp - property length 2874 * 2875 * Return Code: DDI_PROP_SUCCESS 2876 * DDI_PROP_NOT_FOUND 2877 * DDI_PROP_UNDEFINED 2878 * DDI_PROP_NO_MEMORY 2879 * DDI_PROP_BUF_TOO_SMALL 2880 */ 2881 2882 static int 2883 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2884 char *name, caddr_t valuep, int *lengthp) 2885 { 2886 struct sd_lun *un; 2887 2888 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL) 2889 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2890 name, valuep, lengthp)); 2891 2892 return (cmlb_prop_op(un->un_cmlbhandle, 2893 dev, dip, prop_op, mod_flags, name, valuep, lengthp, 2894 SDPART(dev), (void *)SD_PATH_DIRECT)); 2895 } 2896 2897 /* 2898 * The following functions are for smart probing: 2899 * sd_scsi_probe_cache_init() 2900 * sd_scsi_probe_cache_fini() 2901 * sd_scsi_clear_probe_cache() 2902 * sd_scsi_probe_with_cache() 2903 */ 2904 2905 /* 2906 * Function: sd_scsi_probe_cache_init 2907 * 2908 * Description: Initializes the probe response cache mutex and head pointer. 2909 * 2910 * Context: Kernel thread context 2911 */ 2912 2913 static void 2914 sd_scsi_probe_cache_init(void) 2915 { 2916 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2917 sd_scsi_probe_cache_head = NULL; 2918 } 2919 2920 2921 /* 2922 * Function: sd_scsi_probe_cache_fini 2923 * 2924 * Description: Frees all resources associated with the probe response cache. 2925 * 2926 * Context: Kernel thread context 2927 */ 2928 2929 static void 2930 sd_scsi_probe_cache_fini(void) 2931 { 2932 struct sd_scsi_probe_cache *cp; 2933 struct sd_scsi_probe_cache *ncp; 2934 2935 /* Clean up our smart probing linked list */ 2936 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2937 ncp = cp->next; 2938 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2939 } 2940 sd_scsi_probe_cache_head = NULL; 2941 mutex_destroy(&sd_scsi_probe_cache_mutex); 2942 } 2943 2944 2945 /* 2946 * Function: sd_scsi_clear_probe_cache 2947 * 2948 * Description: This routine clears the probe response cache. This is 2949 * done when open() returns ENXIO so that when deferred 2950 * attach is attempted (possibly after a device has been 2951 * turned on) we will retry the probe. Since we don't know 2952 * which target we failed to open, we just clear the 2953 * entire cache. 2954 * 2955 * Context: Kernel thread context 2956 */ 2957 2958 static void 2959 sd_scsi_clear_probe_cache(void) 2960 { 2961 struct sd_scsi_probe_cache *cp; 2962 int i; 2963 2964 mutex_enter(&sd_scsi_probe_cache_mutex); 2965 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2966 /* 2967 * Reset all entries to SCSIPROBE_EXISTS. This will 2968 * force probing to be performed the next time 2969 * sd_scsi_probe_with_cache is called. 2970 */ 2971 for (i = 0; i < NTARGETS_WIDE; i++) { 2972 cp->cache[i] = SCSIPROBE_EXISTS; 2973 } 2974 } 2975 mutex_exit(&sd_scsi_probe_cache_mutex); 2976 } 2977 2978 2979 /* 2980 * Function: sd_scsi_probe_with_cache 2981 * 2982 * Description: This routine implements support for a scsi device probe 2983 * with cache. The driver maintains a cache of the target 2984 * responses to scsi probes. If we get no response from a 2985 * target during a probe inquiry, we remember that, and we 2986 * avoid additional calls to scsi_probe on non-zero LUNs 2987 * on the same target until the cache is cleared. By doing 2988 * so we avoid the 1/4 sec selection timeout for nonzero 2989 * LUNs. lun0 of a target is always probed. 2990 * 2991 * Arguments: devp - Pointer to a scsi_device(9S) structure 2992 * waitfunc - indicates what the allocator routines should 2993 * do when resources are not available. This value 2994 * is passed on to scsi_probe() when that routine 2995 * is called. 2996 * 2997 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2998 * otherwise the value returned by scsi_probe(9F). 2999 * 3000 * Context: Kernel thread context 3001 */ 3002 3003 static int 3004 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 3005 { 3006 struct sd_scsi_probe_cache *cp; 3007 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 3008 int lun, tgt; 3009 3010 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3011 SCSI_ADDR_PROP_LUN, 0); 3012 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3013 SCSI_ADDR_PROP_TARGET, -1); 3014 3015 /* Make sure caching enabled and target in range */ 3016 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 3017 /* do it the old way (no cache) */ 3018 return (scsi_probe(devp, waitfn)); 3019 } 3020 3021 mutex_enter(&sd_scsi_probe_cache_mutex); 3022 3023 /* Find the cache for this scsi bus instance */ 3024 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 3025 if (cp->pdip == pdip) { 3026 break; 3027 } 3028 } 3029 3030 /* If we can't find a cache for this pdip, create one */ 3031 if (cp == NULL) { 3032 int i; 3033 3034 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 3035 KM_SLEEP); 3036 cp->pdip = pdip; 3037 cp->next = sd_scsi_probe_cache_head; 3038 sd_scsi_probe_cache_head = cp; 3039 for (i = 0; i < NTARGETS_WIDE; i++) { 3040 cp->cache[i] = SCSIPROBE_EXISTS; 3041 } 3042 } 3043 3044 mutex_exit(&sd_scsi_probe_cache_mutex); 3045 3046 /* Recompute the cache for this target if LUN zero */ 3047 if (lun == 0) { 3048 cp->cache[tgt] = SCSIPROBE_EXISTS; 3049 } 3050 3051 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 3052 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 3053 return (SCSIPROBE_NORESP); 3054 } 3055 3056 /* Do the actual probe; save & return the result */ 3057 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 3058 } 3059 3060 3061 /* 3062 * Function: sd_scsi_target_lun_init 3063 * 3064 * Description: Initializes the attached lun chain mutex and head pointer. 3065 * 3066 * Context: Kernel thread context 3067 */ 3068 3069 static void 3070 sd_scsi_target_lun_init(void) 3071 { 3072 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 3073 sd_scsi_target_lun_head = NULL; 3074 } 3075 3076 3077 /* 3078 * Function: sd_scsi_target_lun_fini 3079 * 3080 * Description: Frees all resources associated with the attached lun 3081 * chain 3082 * 3083 * Context: Kernel thread context 3084 */ 3085 3086 static void 3087 sd_scsi_target_lun_fini(void) 3088 { 3089 struct sd_scsi_hba_tgt_lun *cp; 3090 struct sd_scsi_hba_tgt_lun *ncp; 3091 3092 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 3093 ncp = cp->next; 3094 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 3095 } 3096 sd_scsi_target_lun_head = NULL; 3097 mutex_destroy(&sd_scsi_target_lun_mutex); 3098 } 3099 3100 3101 /* 3102 * Function: sd_scsi_get_target_lun_count 3103 * 3104 * Description: This routine will check in the attached lun chain to see 3105 * how many luns are attached on the required SCSI controller 3106 * and target. Currently, some capabilities like tagged queue 3107 * are supported per target based by HBA. So all luns in a 3108 * target have the same capabilities. Based on this assumption, 3109 * sd should only set these capabilities once per target. This 3110 * function is called when sd needs to decide how many luns 3111 * already attached on a target. 3112 * 3113 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3114 * controller device. 3115 * target - The target ID on the controller's SCSI bus. 3116 * 3117 * Return Code: The number of luns attached on the required target and 3118 * controller. 3119 * -1 if target ID is not in parallel SCSI scope or the given 3120 * dip is not in the chain. 3121 * 3122 * Context: Kernel thread context 3123 */ 3124 3125 static int 3126 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 3127 { 3128 struct sd_scsi_hba_tgt_lun *cp; 3129 3130 if ((target < 0) || (target >= NTARGETS_WIDE)) { 3131 return (-1); 3132 } 3133 3134 mutex_enter(&sd_scsi_target_lun_mutex); 3135 3136 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3137 if (cp->pdip == dip) { 3138 break; 3139 } 3140 } 3141 3142 mutex_exit(&sd_scsi_target_lun_mutex); 3143 3144 if (cp == NULL) { 3145 return (-1); 3146 } 3147 3148 return (cp->nlun[target]); 3149 } 3150 3151 3152 /* 3153 * Function: sd_scsi_update_lun_on_target 3154 * 3155 * Description: This routine is used to update the attached lun chain when a 3156 * lun is attached or detached on a target. 3157 * 3158 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3159 * controller device. 3160 * target - The target ID on the controller's SCSI bus. 3161 * flag - Indicate the lun is attached or detached. 3162 * 3163 * Context: Kernel thread context 3164 */ 3165 3166 static void 3167 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3168 { 3169 struct sd_scsi_hba_tgt_lun *cp; 3170 3171 mutex_enter(&sd_scsi_target_lun_mutex); 3172 3173 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3174 if (cp->pdip == dip) { 3175 break; 3176 } 3177 } 3178 3179 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3180 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3181 KM_SLEEP); 3182 cp->pdip = dip; 3183 cp->next = sd_scsi_target_lun_head; 3184 sd_scsi_target_lun_head = cp; 3185 } 3186 3187 mutex_exit(&sd_scsi_target_lun_mutex); 3188 3189 if (cp != NULL) { 3190 if (flag == SD_SCSI_LUN_ATTACH) { 3191 cp->nlun[target] ++; 3192 } else { 3193 cp->nlun[target] --; 3194 } 3195 } 3196 } 3197 3198 3199 /* 3200 * Function: sd_spin_up_unit 3201 * 3202 * Description: Issues the following commands to spin-up the device: 3203 * START STOP UNIT, and INQUIRY. 3204 * 3205 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3206 * structure for this target. 3207 * 3208 * Return Code: 0 - success 3209 * EIO - failure 3210 * EACCES - reservation conflict 3211 * 3212 * Context: Kernel thread context 3213 */ 3214 3215 static int 3216 sd_spin_up_unit(sd_ssc_t *ssc) 3217 { 3218 size_t resid = 0; 3219 int has_conflict = FALSE; 3220 uchar_t *bufaddr; 3221 int status; 3222 struct sd_lun *un; 3223 3224 ASSERT(ssc != NULL); 3225 un = ssc->ssc_un; 3226 ASSERT(un != NULL); 3227 3228 /* 3229 * Send a throwaway START UNIT command. 3230 * 3231 * If we fail on this, we don't care presently what precisely 3232 * is wrong. EMC's arrays will also fail this with a check 3233 * condition (0x2/0x4/0x3) if the device is "inactive," but 3234 * we don't want to fail the attach because it may become 3235 * "active" later. 3236 * We don't know if power condition is supported or not at 3237 * this stage, use START STOP bit. 3238 */ 3239 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 3240 SD_TARGET_START, SD_PATH_DIRECT); 3241 3242 if (status != 0) { 3243 if (status == EACCES) 3244 has_conflict = TRUE; 3245 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3246 } 3247 3248 /* 3249 * Send another INQUIRY command to the target. This is necessary for 3250 * non-removable media direct access devices because their INQUIRY data 3251 * may not be fully qualified until they are spun up (perhaps via the 3252 * START command above). Note: This seems to be needed for some 3253 * legacy devices only.) The INQUIRY command should succeed even if a 3254 * Reservation Conflict is present. 3255 */ 3256 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3257 3258 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid) 3259 != 0) { 3260 kmem_free(bufaddr, SUN_INQSIZE); 3261 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 3262 return (EIO); 3263 } 3264 3265 /* 3266 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3267 * Note that this routine does not return a failure here even if the 3268 * INQUIRY command did not return any data. This is a legacy behavior. 3269 */ 3270 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3271 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3272 } 3273 3274 kmem_free(bufaddr, SUN_INQSIZE); 3275 3276 /* If we hit a reservation conflict above, tell the caller. */ 3277 if (has_conflict == TRUE) { 3278 return (EACCES); 3279 } 3280 3281 return (0); 3282 } 3283 3284 #ifdef _LP64 3285 /* 3286 * Function: sd_enable_descr_sense 3287 * 3288 * Description: This routine attempts to select descriptor sense format 3289 * using the Control mode page. Devices that support 64 bit 3290 * LBAs (for >2TB luns) should also implement descriptor 3291 * sense data so we will call this function whenever we see 3292 * a lun larger than 2TB. If for some reason the device 3293 * supports 64 bit LBAs but doesn't support descriptor sense 3294 * presumably the mode select will fail. Everything will 3295 * continue to work normally except that we will not get 3296 * complete sense data for commands that fail with an LBA 3297 * larger than 32 bits. 3298 * 3299 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3300 * structure for this target. 3301 * 3302 * Context: Kernel thread context only 3303 */ 3304 3305 static void 3306 sd_enable_descr_sense(sd_ssc_t *ssc) 3307 { 3308 uchar_t *header; 3309 struct mode_control_scsi3 *ctrl_bufp; 3310 size_t buflen; 3311 size_t bd_len; 3312 int status; 3313 struct sd_lun *un; 3314 3315 ASSERT(ssc != NULL); 3316 un = ssc->ssc_un; 3317 ASSERT(un != NULL); 3318 3319 /* 3320 * Read MODE SENSE page 0xA, Control Mode Page 3321 */ 3322 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3323 sizeof (struct mode_control_scsi3); 3324 header = kmem_zalloc(buflen, KM_SLEEP); 3325 3326 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 3327 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT); 3328 3329 if (status != 0) { 3330 SD_ERROR(SD_LOG_COMMON, un, 3331 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3332 goto eds_exit; 3333 } 3334 3335 /* 3336 * Determine size of Block Descriptors in order to locate 3337 * the mode page data. ATAPI devices return 0, SCSI devices 3338 * should return MODE_BLK_DESC_LENGTH. 3339 */ 3340 bd_len = ((struct mode_header *)header)->bdesc_length; 3341 3342 /* Clear the mode data length field for MODE SELECT */ 3343 ((struct mode_header *)header)->length = 0; 3344 3345 ctrl_bufp = (struct mode_control_scsi3 *) 3346 (header + MODE_HEADER_LENGTH + bd_len); 3347 3348 /* 3349 * If the page length is smaller than the expected value, 3350 * the target device doesn't support D_SENSE. Bail out here. 3351 */ 3352 if (ctrl_bufp->mode_page.length < 3353 sizeof (struct mode_control_scsi3) - 2) { 3354 SD_ERROR(SD_LOG_COMMON, un, 3355 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3356 goto eds_exit; 3357 } 3358 3359 /* 3360 * Clear PS bit for MODE SELECT 3361 */ 3362 ctrl_bufp->mode_page.ps = 0; 3363 3364 /* 3365 * Set D_SENSE to enable descriptor sense format. 3366 */ 3367 ctrl_bufp->d_sense = 1; 3368 3369 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3370 3371 /* 3372 * Use MODE SELECT to commit the change to the D_SENSE bit 3373 */ 3374 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 3375 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT); 3376 3377 if (status != 0) { 3378 SD_INFO(SD_LOG_COMMON, un, 3379 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3380 } else { 3381 kmem_free(header, buflen); 3382 return; 3383 } 3384 3385 eds_exit: 3386 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3387 kmem_free(header, buflen); 3388 } 3389 3390 /* 3391 * Function: sd_reenable_dsense_task 3392 * 3393 * Description: Re-enable descriptor sense after device or bus reset 3394 * 3395 * Context: Executes in a taskq() thread context 3396 */ 3397 static void 3398 sd_reenable_dsense_task(void *arg) 3399 { 3400 struct sd_lun *un = arg; 3401 sd_ssc_t *ssc; 3402 3403 ASSERT(un != NULL); 3404 3405 ssc = sd_ssc_init(un); 3406 sd_enable_descr_sense(ssc); 3407 sd_ssc_fini(ssc); 3408 } 3409 #endif /* _LP64 */ 3410 3411 /* 3412 * Function: sd_set_mmc_caps 3413 * 3414 * Description: This routine determines if the device is MMC compliant and if 3415 * the device supports CDDA via a mode sense of the CDVD 3416 * capabilities mode page. Also checks if the device is a 3417 * dvdram writable device. 3418 * 3419 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3420 * structure for this target. 3421 * 3422 * Context: Kernel thread context only 3423 */ 3424 3425 static void 3426 sd_set_mmc_caps(sd_ssc_t *ssc) 3427 { 3428 struct mode_header_grp2 *sense_mhp; 3429 uchar_t *sense_page; 3430 caddr_t buf; 3431 int bd_len; 3432 int status; 3433 struct uscsi_cmd com; 3434 int rtn; 3435 uchar_t *out_data_rw, *out_data_hd; 3436 uchar_t *rqbuf_rw, *rqbuf_hd; 3437 uchar_t *out_data_gesn; 3438 int gesn_len; 3439 struct sd_lun *un; 3440 3441 ASSERT(ssc != NULL); 3442 un = ssc->ssc_un; 3443 ASSERT(un != NULL); 3444 3445 /* 3446 * The flags which will be set in this function are - mmc compliant, 3447 * dvdram writable device, cdda support. Initialize them to FALSE 3448 * and if a capability is detected - it will be set to TRUE. 3449 */ 3450 un->un_f_mmc_cap = FALSE; 3451 un->un_f_dvdram_writable_device = FALSE; 3452 un->un_f_cfg_cdda = FALSE; 3453 3454 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3455 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3456 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3457 3458 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3459 3460 if (status != 0) { 3461 /* command failed; just return */ 3462 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3463 return; 3464 } 3465 /* 3466 * If the mode sense request for the CDROM CAPABILITIES 3467 * page (0x2A) succeeds the device is assumed to be MMC. 3468 */ 3469 un->un_f_mmc_cap = TRUE; 3470 3471 /* See if GET STATUS EVENT NOTIFICATION is supported */ 3472 if (un->un_f_mmc_gesn_polling) { 3473 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN; 3474 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP); 3475 3476 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc, 3477 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS); 3478 3479 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3480 3481 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) { 3482 un->un_f_mmc_gesn_polling = FALSE; 3483 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3484 "sd_set_mmc_caps: gesn not supported " 3485 "%d %x %x %x %x\n", rtn, 3486 out_data_gesn[0], out_data_gesn[1], 3487 out_data_gesn[2], out_data_gesn[3]); 3488 } 3489 3490 kmem_free(out_data_gesn, gesn_len); 3491 } 3492 3493 /* Get to the page data */ 3494 sense_mhp = (struct mode_header_grp2 *)buf; 3495 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3496 sense_mhp->bdesc_length_lo; 3497 if (bd_len > MODE_BLK_DESC_LENGTH) { 3498 /* 3499 * We did not get back the expected block descriptor 3500 * length so we cannot determine if the device supports 3501 * CDDA. However, we still indicate the device is MMC 3502 * according to the successful response to the page 3503 * 0x2A mode sense request. 3504 */ 3505 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3506 "sd_set_mmc_caps: Mode Sense returned " 3507 "invalid block descriptor length\n"); 3508 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3509 return; 3510 } 3511 3512 /* See if read CDDA is supported */ 3513 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3514 bd_len); 3515 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3516 3517 /* See if writing DVD RAM is supported. */ 3518 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3519 if (un->un_f_dvdram_writable_device == TRUE) { 3520 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3521 return; 3522 } 3523 3524 /* 3525 * If the device presents DVD or CD capabilities in the mode 3526 * page, we can return here since a RRD will not have 3527 * these capabilities. 3528 */ 3529 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3530 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3531 return; 3532 } 3533 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3534 3535 /* 3536 * If un->un_f_dvdram_writable_device is still FALSE, 3537 * check for a Removable Rigid Disk (RRD). A RRD 3538 * device is identified by the features RANDOM_WRITABLE and 3539 * HARDWARE_DEFECT_MANAGEMENT. 3540 */ 3541 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3542 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3543 3544 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3545 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3546 RANDOM_WRITABLE, SD_PATH_STANDARD); 3547 3548 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3549 3550 if (rtn != 0) { 3551 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3552 kmem_free(rqbuf_rw, SENSE_LENGTH); 3553 return; 3554 } 3555 3556 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3557 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3558 3559 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3560 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3561 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3562 3563 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3564 3565 if (rtn == 0) { 3566 /* 3567 * We have good information, check for random writable 3568 * and hardware defect features. 3569 */ 3570 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3571 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3572 un->un_f_dvdram_writable_device = TRUE; 3573 } 3574 } 3575 3576 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3577 kmem_free(rqbuf_rw, SENSE_LENGTH); 3578 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3579 kmem_free(rqbuf_hd, SENSE_LENGTH); 3580 } 3581 3582 /* 3583 * Function: sd_check_for_writable_cd 3584 * 3585 * Description: This routine determines if the media in the device is 3586 * writable or not. It uses the get configuration command (0x46) 3587 * to determine if the media is writable 3588 * 3589 * Arguments: un - driver soft state (unit) structure 3590 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3591 * chain and the normal command waitq, or 3592 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3593 * "direct" chain and bypass the normal command 3594 * waitq. 3595 * 3596 * Context: Never called at interrupt context. 3597 */ 3598 3599 static void 3600 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag) 3601 { 3602 struct uscsi_cmd com; 3603 uchar_t *out_data; 3604 uchar_t *rqbuf; 3605 int rtn; 3606 uchar_t *out_data_rw, *out_data_hd; 3607 uchar_t *rqbuf_rw, *rqbuf_hd; 3608 struct mode_header_grp2 *sense_mhp; 3609 uchar_t *sense_page; 3610 caddr_t buf; 3611 int bd_len; 3612 int status; 3613 struct sd_lun *un; 3614 3615 ASSERT(ssc != NULL); 3616 un = ssc->ssc_un; 3617 ASSERT(un != NULL); 3618 ASSERT(mutex_owned(SD_MUTEX(un))); 3619 3620 /* 3621 * Initialize the writable media to false, if configuration info. 3622 * tells us otherwise then only we will set it. 3623 */ 3624 un->un_f_mmc_writable_media = FALSE; 3625 mutex_exit(SD_MUTEX(un)); 3626 3627 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3628 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3629 3630 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH, 3631 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3632 3633 if (rtn != 0) 3634 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3635 3636 mutex_enter(SD_MUTEX(un)); 3637 if (rtn == 0) { 3638 /* 3639 * We have good information, check for writable DVD. 3640 */ 3641 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3642 un->un_f_mmc_writable_media = TRUE; 3643 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3644 kmem_free(rqbuf, SENSE_LENGTH); 3645 return; 3646 } 3647 } 3648 3649 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3650 kmem_free(rqbuf, SENSE_LENGTH); 3651 3652 /* 3653 * Determine if this is a RRD type device. 3654 */ 3655 mutex_exit(SD_MUTEX(un)); 3656 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3657 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3658 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3659 3660 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3661 3662 mutex_enter(SD_MUTEX(un)); 3663 if (status != 0) { 3664 /* command failed; just return */ 3665 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3666 return; 3667 } 3668 3669 /* Get to the page data */ 3670 sense_mhp = (struct mode_header_grp2 *)buf; 3671 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3672 if (bd_len > MODE_BLK_DESC_LENGTH) { 3673 /* 3674 * We did not get back the expected block descriptor length so 3675 * we cannot check the mode page. 3676 */ 3677 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3678 "sd_check_for_writable_cd: Mode Sense returned " 3679 "invalid block descriptor length\n"); 3680 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3681 return; 3682 } 3683 3684 /* 3685 * If the device presents DVD or CD capabilities in the mode 3686 * page, we can return here since a RRD device will not have 3687 * these capabilities. 3688 */ 3689 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3690 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3691 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3692 return; 3693 } 3694 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3695 3696 /* 3697 * If un->un_f_mmc_writable_media is still FALSE, 3698 * check for RRD type media. A RRD device is identified 3699 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3700 */ 3701 mutex_exit(SD_MUTEX(un)); 3702 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3703 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3704 3705 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3706 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3707 RANDOM_WRITABLE, path_flag); 3708 3709 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3710 if (rtn != 0) { 3711 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3712 kmem_free(rqbuf_rw, SENSE_LENGTH); 3713 mutex_enter(SD_MUTEX(un)); 3714 return; 3715 } 3716 3717 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3718 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3719 3720 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3721 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3722 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3723 3724 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3725 mutex_enter(SD_MUTEX(un)); 3726 if (rtn == 0) { 3727 /* 3728 * We have good information, check for random writable 3729 * and hardware defect features as current. 3730 */ 3731 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3732 (out_data_rw[10] & 0x1) && 3733 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3734 (out_data_hd[10] & 0x1)) { 3735 un->un_f_mmc_writable_media = TRUE; 3736 } 3737 } 3738 3739 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3740 kmem_free(rqbuf_rw, SENSE_LENGTH); 3741 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3742 kmem_free(rqbuf_hd, SENSE_LENGTH); 3743 } 3744 3745 /* 3746 * Function: sd_read_unit_properties 3747 * 3748 * Description: The following implements a property lookup mechanism. 3749 * Properties for particular disks (keyed on vendor, model 3750 * and rev numbers) are sought in the sd.conf file via 3751 * sd_process_sdconf_file(), and if not found there, are 3752 * looked for in a list hardcoded in this driver via 3753 * sd_process_sdconf_table() Once located the properties 3754 * are used to update the driver unit structure. 3755 * 3756 * Arguments: un - driver soft state (unit) structure 3757 */ 3758 3759 static void 3760 sd_read_unit_properties(struct sd_lun *un) 3761 { 3762 /* 3763 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3764 * the "sd-config-list" property (from the sd.conf file) or if 3765 * there was not a match for the inquiry vid/pid. If this event 3766 * occurs the static driver configuration table is searched for 3767 * a match. 3768 */ 3769 ASSERT(un != NULL); 3770 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3771 sd_process_sdconf_table(un); 3772 } 3773 3774 /* check for LSI device */ 3775 sd_is_lsi(un); 3776 3777 3778 } 3779 3780 3781 /* 3782 * Function: sd_process_sdconf_file 3783 * 3784 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the 3785 * driver's config file (ie, sd.conf) and update the driver 3786 * soft state structure accordingly. 3787 * 3788 * Arguments: un - driver soft state (unit) structure 3789 * 3790 * Return Code: SD_SUCCESS - The properties were successfully set according 3791 * to the driver configuration file. 3792 * SD_FAILURE - The driver config list was not obtained or 3793 * there was no vid/pid match. This indicates that 3794 * the static config table should be used. 3795 * 3796 * The config file has a property, "sd-config-list". Currently we support 3797 * two kinds of formats. For both formats, the value of this property 3798 * is a list of duplets: 3799 * 3800 * sd-config-list= 3801 * <duplet>, 3802 * [,<duplet>]*; 3803 * 3804 * For the improved format, where 3805 * 3806 * <duplet>:= "<vid+pid>","<tunable-list>" 3807 * 3808 * and 3809 * 3810 * <tunable-list>:= <tunable> [, <tunable> ]*; 3811 * <tunable> = <name> : <value> 3812 * 3813 * The <vid+pid> is the string that is returned by the target device on a 3814 * SCSI inquiry command, the <tunable-list> contains one or more tunables 3815 * to apply to all target devices with the specified <vid+pid>. 3816 * 3817 * Each <tunable> is a "<name> : <value>" pair. 3818 * 3819 * For the old format, the structure of each duplet is as follows: 3820 * 3821 * <duplet>:= "<vid+pid>","<data-property-name_list>" 3822 * 3823 * The first entry of the duplet is the device ID string (the concatenated 3824 * vid & pid; not to be confused with a device_id). This is defined in 3825 * the same way as in the sd_disk_table. 3826 * 3827 * The second part of the duplet is a string that identifies a 3828 * data-property-name-list. The data-property-name-list is defined as 3829 * follows: 3830 * 3831 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3832 * 3833 * The syntax of <data-property-name> depends on the <version> field. 3834 * 3835 * If version = SD_CONF_VERSION_1 we have the following syntax: 3836 * 3837 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3838 * 3839 * where the prop0 value will be used to set prop0 if bit0 set in the 3840 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3841 * 3842 */ 3843 3844 static int 3845 sd_process_sdconf_file(struct sd_lun *un) 3846 { 3847 char **config_list = NULL; 3848 uint_t nelements; 3849 char *vidptr; 3850 int vidlen; 3851 char *dnlist_ptr; 3852 char *dataname_ptr; 3853 char *dataname_lasts; 3854 int *data_list = NULL; 3855 uint_t data_list_len; 3856 int rval = SD_FAILURE; 3857 int i; 3858 3859 ASSERT(un != NULL); 3860 3861 /* Obtain the configuration list associated with the .conf file */ 3862 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un), 3863 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list, 3864 &config_list, &nelements) != DDI_PROP_SUCCESS) { 3865 return (SD_FAILURE); 3866 } 3867 3868 /* 3869 * Compare vids in each duplet to the inquiry vid - if a match is 3870 * made, get the data value and update the soft state structure 3871 * accordingly. 3872 * 3873 * Each duplet should show as a pair of strings, return SD_FAILURE 3874 * otherwise. 3875 */ 3876 if (nelements & 1) { 3877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3878 "sd-config-list should show as pairs of strings.\n"); 3879 if (config_list) 3880 ddi_prop_free(config_list); 3881 return (SD_FAILURE); 3882 } 3883 3884 for (i = 0; i < nelements; i += 2) { 3885 /* 3886 * Note: The assumption here is that each vid entry is on 3887 * a unique line from its associated duplet. 3888 */ 3889 vidptr = config_list[i]; 3890 vidlen = (int)strlen(vidptr); 3891 if ((vidlen == 0) || 3892 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3893 continue; 3894 } 3895 3896 /* 3897 * dnlist contains 1 or more blank separated 3898 * data-property-name entries 3899 */ 3900 dnlist_ptr = config_list[i + 1]; 3901 3902 if (strchr(dnlist_ptr, ':') != NULL) { 3903 /* 3904 * Decode the improved format sd-config-list. 3905 */ 3906 sd_nvpair_str_decode(un, dnlist_ptr); 3907 } else { 3908 /* 3909 * The old format sd-config-list, loop through all 3910 * data-property-name entries in the 3911 * data-property-name-list 3912 * setting the properties for each. 3913 */ 3914 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", 3915 &dataname_lasts); dataname_ptr != NULL; 3916 dataname_ptr = sd_strtok_r(NULL, " \t", 3917 &dataname_lasts)) { 3918 int version; 3919 3920 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3921 "sd_process_sdconf_file: disk:%s, " 3922 "data:%s\n", vidptr, dataname_ptr); 3923 3924 /* Get the data list */ 3925 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, 3926 SD_DEVINFO(un), 0, dataname_ptr, &data_list, 3927 &data_list_len) != DDI_PROP_SUCCESS) { 3928 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3929 "sd_process_sdconf_file: data " 3930 "property (%s) has no value\n", 3931 dataname_ptr); 3932 continue; 3933 } 3934 3935 version = data_list[0]; 3936 3937 if (version == SD_CONF_VERSION_1) { 3938 sd_tunables values; 3939 3940 /* Set the properties */ 3941 if (sd_chk_vers1_data(un, data_list[1], 3942 &data_list[2], data_list_len, 3943 dataname_ptr) == SD_SUCCESS) { 3944 sd_get_tunables_from_conf(un, 3945 data_list[1], &data_list[2], 3946 &values); 3947 sd_set_vers1_properties(un, 3948 data_list[1], &values); 3949 rval = SD_SUCCESS; 3950 } else { 3951 rval = SD_FAILURE; 3952 } 3953 } else { 3954 scsi_log(SD_DEVINFO(un), sd_label, 3955 CE_WARN, "data property %s version " 3956 "0x%x is invalid.", 3957 dataname_ptr, version); 3958 rval = SD_FAILURE; 3959 } 3960 if (data_list) 3961 ddi_prop_free(data_list); 3962 } 3963 } 3964 } 3965 3966 /* free up the memory allocated by ddi_prop_lookup_string_array(). */ 3967 if (config_list) { 3968 ddi_prop_free(config_list); 3969 } 3970 3971 return (rval); 3972 } 3973 3974 /* 3975 * Function: sd_nvpair_str_decode() 3976 * 3977 * Description: Parse the improved format sd-config-list to get 3978 * each entry of tunable, which includes a name-value pair. 3979 * Then call sd_set_properties() to set the property. 3980 * 3981 * Arguments: un - driver soft state (unit) structure 3982 * nvpair_str - the tunable list 3983 */ 3984 static void 3985 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) 3986 { 3987 char *nv, *name, *value, *token; 3988 char *nv_lasts, *v_lasts, *x_lasts; 3989 3990 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; 3991 nv = sd_strtok_r(NULL, ",", &nv_lasts)) { 3992 token = sd_strtok_r(nv, ":", &v_lasts); 3993 name = sd_strtok_r(token, " \t", &x_lasts); 3994 token = sd_strtok_r(NULL, ":", &v_lasts); 3995 value = sd_strtok_r(token, " \t", &x_lasts); 3996 if (name == NULL || value == NULL) { 3997 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3998 "sd_nvpair_str_decode: " 3999 "name or value is not valid!\n"); 4000 } else { 4001 sd_set_properties(un, name, value); 4002 } 4003 } 4004 } 4005 4006 /* 4007 * Function: sd_strtok_r() 4008 * 4009 * Description: This function uses strpbrk and strspn to break 4010 * string into tokens on sequentially subsequent calls. Return 4011 * NULL when no non-separator characters remain. The first 4012 * argument is NULL for subsequent calls. 4013 */ 4014 static char * 4015 sd_strtok_r(char *string, const char *sepset, char **lasts) 4016 { 4017 char *q, *r; 4018 4019 /* First or subsequent call */ 4020 if (string == NULL) 4021 string = *lasts; 4022 4023 if (string == NULL) 4024 return (NULL); 4025 4026 /* Skip leading separators */ 4027 q = string + strspn(string, sepset); 4028 4029 if (*q == '\0') 4030 return (NULL); 4031 4032 if ((r = strpbrk(q, sepset)) == NULL) 4033 *lasts = NULL; 4034 else { 4035 *r = '\0'; 4036 *lasts = r + 1; 4037 } 4038 return (q); 4039 } 4040 4041 /* 4042 * Function: sd_set_properties() 4043 * 4044 * Description: Set device properties based on the improved 4045 * format sd-config-list. 4046 * 4047 * Arguments: un - driver soft state (unit) structure 4048 * name - supported tunable name 4049 * value - tunable value 4050 */ 4051 static void 4052 sd_set_properties(struct sd_lun *un, char *name, char *value) 4053 { 4054 char *endptr = NULL; 4055 long val = 0; 4056 4057 if (strcasecmp(name, "cache-nonvolatile") == 0) { 4058 if (strcasecmp(value, "true") == 0) { 4059 un->un_f_suppress_cache_flush = TRUE; 4060 } else if (strcasecmp(value, "false") == 0) { 4061 un->un_f_suppress_cache_flush = FALSE; 4062 } else { 4063 goto value_invalid; 4064 } 4065 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4066 "suppress_cache_flush flag set to %d\n", 4067 un->un_f_suppress_cache_flush); 4068 return; 4069 } 4070 4071 if (strcasecmp(name, "controller-type") == 0) { 4072 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4073 un->un_ctype = val; 4074 } else { 4075 goto value_invalid; 4076 } 4077 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4078 "ctype set to %d\n", un->un_ctype); 4079 return; 4080 } 4081 4082 if (strcasecmp(name, "delay-busy") == 0) { 4083 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4084 un->un_busy_timeout = drv_usectohz(val / 1000); 4085 } else { 4086 goto value_invalid; 4087 } 4088 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4089 "busy_timeout set to %d\n", un->un_busy_timeout); 4090 return; 4091 } 4092 4093 if (strcasecmp(name, "disksort") == 0) { 4094 if (strcasecmp(value, "true") == 0) { 4095 un->un_f_disksort_disabled = FALSE; 4096 } else if (strcasecmp(value, "false") == 0) { 4097 un->un_f_disksort_disabled = TRUE; 4098 } else { 4099 goto value_invalid; 4100 } 4101 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4102 "disksort disabled flag set to %d\n", 4103 un->un_f_disksort_disabled); 4104 return; 4105 } 4106 4107 if (strcasecmp(name, "power-condition") == 0) { 4108 if (strcasecmp(value, "true") == 0) { 4109 un->un_f_power_condition_disabled = FALSE; 4110 } else if (strcasecmp(value, "false") == 0) { 4111 un->un_f_power_condition_disabled = TRUE; 4112 } else { 4113 goto value_invalid; 4114 } 4115 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4116 "power condition disabled flag set to %d\n", 4117 un->un_f_power_condition_disabled); 4118 return; 4119 } 4120 4121 if (strcasecmp(name, "timeout-releasereservation") == 0) { 4122 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4123 un->un_reserve_release_time = val; 4124 } else { 4125 goto value_invalid; 4126 } 4127 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4128 "reservation release timeout set to %d\n", 4129 un->un_reserve_release_time); 4130 return; 4131 } 4132 4133 if (strcasecmp(name, "reset-lun") == 0) { 4134 if (strcasecmp(value, "true") == 0) { 4135 un->un_f_lun_reset_enabled = TRUE; 4136 } else if (strcasecmp(value, "false") == 0) { 4137 un->un_f_lun_reset_enabled = FALSE; 4138 } else { 4139 goto value_invalid; 4140 } 4141 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4142 "lun reset enabled flag set to %d\n", 4143 un->un_f_lun_reset_enabled); 4144 return; 4145 } 4146 4147 if (strcasecmp(name, "retries-busy") == 0) { 4148 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4149 un->un_busy_retry_count = val; 4150 } else { 4151 goto value_invalid; 4152 } 4153 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4154 "busy retry count set to %d\n", un->un_busy_retry_count); 4155 return; 4156 } 4157 4158 if (strcasecmp(name, "retries-timeout") == 0) { 4159 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4160 un->un_retry_count = val; 4161 } else { 4162 goto value_invalid; 4163 } 4164 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4165 "timeout retry count set to %d\n", un->un_retry_count); 4166 return; 4167 } 4168 4169 if (strcasecmp(name, "retries-notready") == 0) { 4170 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4171 un->un_notready_retry_count = val; 4172 } else { 4173 goto value_invalid; 4174 } 4175 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4176 "notready retry count set to %d\n", 4177 un->un_notready_retry_count); 4178 return; 4179 } 4180 4181 if (strcasecmp(name, "retries-reset") == 0) { 4182 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4183 un->un_reset_retry_count = val; 4184 } else { 4185 goto value_invalid; 4186 } 4187 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4188 "reset retry count set to %d\n", 4189 un->un_reset_retry_count); 4190 return; 4191 } 4192 4193 if (strcasecmp(name, "throttle-max") == 0) { 4194 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4195 un->un_saved_throttle = un->un_throttle = val; 4196 } else { 4197 goto value_invalid; 4198 } 4199 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4200 "throttle set to %d\n", un->un_throttle); 4201 } 4202 4203 if (strcasecmp(name, "throttle-min") == 0) { 4204 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4205 un->un_min_throttle = val; 4206 } else { 4207 goto value_invalid; 4208 } 4209 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4210 "min throttle set to %d\n", un->un_min_throttle); 4211 } 4212 4213 if (strcasecmp(name, "rmw-type") == 0) { 4214 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4215 un->un_f_rmw_type = val; 4216 } else { 4217 goto value_invalid; 4218 } 4219 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4220 "RMW type set to %d\n", un->un_f_rmw_type); 4221 } 4222 4223 /* 4224 * Validate the throttle values. 4225 * If any of the numbers are invalid, set everything to defaults. 4226 */ 4227 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4228 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4229 (un->un_min_throttle > un->un_throttle)) { 4230 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4231 un->un_min_throttle = sd_min_throttle; 4232 } 4233 4234 if (strcasecmp(name, "mmc-gesn-polling") == 0) { 4235 if (strcasecmp(value, "true") == 0) { 4236 un->un_f_mmc_gesn_polling = TRUE; 4237 } else if (strcasecmp(value, "false") == 0) { 4238 un->un_f_mmc_gesn_polling = FALSE; 4239 } else { 4240 goto value_invalid; 4241 } 4242 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4243 "mmc-gesn-polling set to %d\n", 4244 un->un_f_mmc_gesn_polling); 4245 } 4246 4247 return; 4248 4249 value_invalid: 4250 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4251 "value of prop %s is invalid\n", name); 4252 } 4253 4254 /* 4255 * Function: sd_get_tunables_from_conf() 4256 * 4257 * 4258 * This function reads the data list from the sd.conf file and pulls 4259 * the values that can have numeric values as arguments and places 4260 * the values in the appropriate sd_tunables member. 4261 * Since the order of the data list members varies across platforms 4262 * This function reads them from the data list in a platform specific 4263 * order and places them into the correct sd_tunable member that is 4264 * consistent across all platforms. 4265 */ 4266 static void 4267 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 4268 sd_tunables *values) 4269 { 4270 int i; 4271 int mask; 4272 4273 bzero(values, sizeof (sd_tunables)); 4274 4275 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4276 4277 mask = 1 << i; 4278 if (mask > flags) { 4279 break; 4280 } 4281 4282 switch (mask & flags) { 4283 case 0: /* This mask bit not set in flags */ 4284 continue; 4285 case SD_CONF_BSET_THROTTLE: 4286 values->sdt_throttle = data_list[i]; 4287 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4288 "sd_get_tunables_from_conf: throttle = %d\n", 4289 values->sdt_throttle); 4290 break; 4291 case SD_CONF_BSET_CTYPE: 4292 values->sdt_ctype = data_list[i]; 4293 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4294 "sd_get_tunables_from_conf: ctype = %d\n", 4295 values->sdt_ctype); 4296 break; 4297 case SD_CONF_BSET_NRR_COUNT: 4298 values->sdt_not_rdy_retries = data_list[i]; 4299 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4300 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 4301 values->sdt_not_rdy_retries); 4302 break; 4303 case SD_CONF_BSET_BSY_RETRY_COUNT: 4304 values->sdt_busy_retries = data_list[i]; 4305 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4306 "sd_get_tunables_from_conf: busy_retries = %d\n", 4307 values->sdt_busy_retries); 4308 break; 4309 case SD_CONF_BSET_RST_RETRIES: 4310 values->sdt_reset_retries = data_list[i]; 4311 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4312 "sd_get_tunables_from_conf: reset_retries = %d\n", 4313 values->sdt_reset_retries); 4314 break; 4315 case SD_CONF_BSET_RSV_REL_TIME: 4316 values->sdt_reserv_rel_time = data_list[i]; 4317 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4318 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 4319 values->sdt_reserv_rel_time); 4320 break; 4321 case SD_CONF_BSET_MIN_THROTTLE: 4322 values->sdt_min_throttle = data_list[i]; 4323 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4324 "sd_get_tunables_from_conf: min_throttle = %d\n", 4325 values->sdt_min_throttle); 4326 break; 4327 case SD_CONF_BSET_DISKSORT_DISABLED: 4328 values->sdt_disk_sort_dis = data_list[i]; 4329 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4330 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 4331 values->sdt_disk_sort_dis); 4332 break; 4333 case SD_CONF_BSET_LUN_RESET_ENABLED: 4334 values->sdt_lun_reset_enable = data_list[i]; 4335 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4336 "sd_get_tunables_from_conf: lun_reset_enable = %d" 4337 "\n", values->sdt_lun_reset_enable); 4338 break; 4339 case SD_CONF_BSET_CACHE_IS_NV: 4340 values->sdt_suppress_cache_flush = data_list[i]; 4341 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4342 "sd_get_tunables_from_conf: \ 4343 suppress_cache_flush = %d" 4344 "\n", values->sdt_suppress_cache_flush); 4345 break; 4346 case SD_CONF_BSET_PC_DISABLED: 4347 values->sdt_disk_sort_dis = data_list[i]; 4348 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4349 "sd_get_tunables_from_conf: power_condition_dis = " 4350 "%d\n", values->sdt_power_condition_dis); 4351 break; 4352 } 4353 } 4354 } 4355 4356 /* 4357 * Function: sd_process_sdconf_table 4358 * 4359 * Description: Search the static configuration table for a match on the 4360 * inquiry vid/pid and update the driver soft state structure 4361 * according to the table property values for the device. 4362 * 4363 * The form of a configuration table entry is: 4364 * <vid+pid>,<flags>,<property-data> 4365 * "SEAGATE ST42400N",1,0x40000, 4366 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 4367 * 4368 * Arguments: un - driver soft state (unit) structure 4369 */ 4370 4371 static void 4372 sd_process_sdconf_table(struct sd_lun *un) 4373 { 4374 char *id = NULL; 4375 int table_index; 4376 int idlen; 4377 4378 ASSERT(un != NULL); 4379 for (table_index = 0; table_index < sd_disk_table_size; 4380 table_index++) { 4381 id = sd_disk_table[table_index].device_id; 4382 idlen = strlen(id); 4383 if (idlen == 0) { 4384 continue; 4385 } 4386 4387 /* 4388 * The static configuration table currently does not 4389 * implement version 10 properties. Additionally, 4390 * multiple data-property-name entries are not 4391 * implemented in the static configuration table. 4392 */ 4393 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4394 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4395 "sd_process_sdconf_table: disk %s\n", id); 4396 sd_set_vers1_properties(un, 4397 sd_disk_table[table_index].flags, 4398 sd_disk_table[table_index].properties); 4399 break; 4400 } 4401 } 4402 } 4403 4404 4405 /* 4406 * Function: sd_sdconf_id_match 4407 * 4408 * Description: This local function implements a case sensitive vid/pid 4409 * comparison as well as the boundary cases of wild card and 4410 * multiple blanks. 4411 * 4412 * Note: An implicit assumption made here is that the scsi 4413 * inquiry structure will always keep the vid, pid and 4414 * revision strings in consecutive sequence, so they can be 4415 * read as a single string. If this assumption is not the 4416 * case, a separate string, to be used for the check, needs 4417 * to be built with these strings concatenated. 4418 * 4419 * Arguments: un - driver soft state (unit) structure 4420 * id - table or config file vid/pid 4421 * idlen - length of the vid/pid (bytes) 4422 * 4423 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4424 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4425 */ 4426 4427 static int 4428 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 4429 { 4430 struct scsi_inquiry *sd_inq; 4431 int rval = SD_SUCCESS; 4432 4433 ASSERT(un != NULL); 4434 sd_inq = un->un_sd->sd_inq; 4435 ASSERT(id != NULL); 4436 4437 /* 4438 * We use the inq_vid as a pointer to a buffer containing the 4439 * vid and pid and use the entire vid/pid length of the table 4440 * entry for the comparison. This works because the inq_pid 4441 * data member follows inq_vid in the scsi_inquiry structure. 4442 */ 4443 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 4444 /* 4445 * The user id string is compared to the inquiry vid/pid 4446 * using a case insensitive comparison and ignoring 4447 * multiple spaces. 4448 */ 4449 rval = sd_blank_cmp(un, id, idlen); 4450 if (rval != SD_SUCCESS) { 4451 /* 4452 * User id strings that start and end with a "*" 4453 * are a special case. These do not have a 4454 * specific vendor, and the product string can 4455 * appear anywhere in the 16 byte PID portion of 4456 * the inquiry data. This is a simple strstr() 4457 * type search for the user id in the inquiry data. 4458 */ 4459 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 4460 char *pidptr = &id[1]; 4461 int i; 4462 int j; 4463 int pidstrlen = idlen - 2; 4464 j = sizeof (SD_INQUIRY(un)->inq_pid) - 4465 pidstrlen; 4466 4467 if (j < 0) { 4468 return (SD_FAILURE); 4469 } 4470 for (i = 0; i < j; i++) { 4471 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 4472 pidptr, pidstrlen) == 0) { 4473 rval = SD_SUCCESS; 4474 break; 4475 } 4476 } 4477 } 4478 } 4479 } 4480 return (rval); 4481 } 4482 4483 4484 /* 4485 * Function: sd_blank_cmp 4486 * 4487 * Description: If the id string starts and ends with a space, treat 4488 * multiple consecutive spaces as equivalent to a single 4489 * space. For example, this causes a sd_disk_table entry 4490 * of " NEC CDROM " to match a device's id string of 4491 * "NEC CDROM". 4492 * 4493 * Note: The success exit condition for this routine is if 4494 * the pointer to the table entry is '\0' and the cnt of 4495 * the inquiry length is zero. This will happen if the inquiry 4496 * string returned by the device is padded with spaces to be 4497 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 4498 * SCSI spec states that the inquiry string is to be padded with 4499 * spaces. 4500 * 4501 * Arguments: un - driver soft state (unit) structure 4502 * id - table or config file vid/pid 4503 * idlen - length of the vid/pid (bytes) 4504 * 4505 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4506 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4507 */ 4508 4509 static int 4510 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 4511 { 4512 char *p1; 4513 char *p2; 4514 int cnt; 4515 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 4516 sizeof (SD_INQUIRY(un)->inq_pid); 4517 4518 ASSERT(un != NULL); 4519 p2 = un->un_sd->sd_inq->inq_vid; 4520 ASSERT(id != NULL); 4521 p1 = id; 4522 4523 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 4524 /* 4525 * Note: string p1 is terminated by a NUL but string p2 4526 * isn't. The end of p2 is determined by cnt. 4527 */ 4528 for (;;) { 4529 /* skip over any extra blanks in both strings */ 4530 while ((*p1 != '\0') && (*p1 == ' ')) { 4531 p1++; 4532 } 4533 while ((cnt != 0) && (*p2 == ' ')) { 4534 p2++; 4535 cnt--; 4536 } 4537 4538 /* compare the two strings */ 4539 if ((cnt == 0) || 4540 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 4541 break; 4542 } 4543 while ((cnt > 0) && 4544 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 4545 p1++; 4546 p2++; 4547 cnt--; 4548 } 4549 } 4550 } 4551 4552 /* return SD_SUCCESS if both strings match */ 4553 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 4554 } 4555 4556 4557 /* 4558 * Function: sd_chk_vers1_data 4559 * 4560 * Description: Verify the version 1 device properties provided by the 4561 * user via the configuration file 4562 * 4563 * Arguments: un - driver soft state (unit) structure 4564 * flags - integer mask indicating properties to be set 4565 * prop_list - integer list of property values 4566 * list_len - number of the elements 4567 * 4568 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4569 * SD_FAILURE - Indicates the user provided data is invalid 4570 */ 4571 4572 static int 4573 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4574 int list_len, char *dataname_ptr) 4575 { 4576 int i; 4577 int mask = 1; 4578 int index = 0; 4579 4580 ASSERT(un != NULL); 4581 4582 /* Check for a NULL property name and list */ 4583 if (dataname_ptr == NULL) { 4584 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4585 "sd_chk_vers1_data: NULL data property name."); 4586 return (SD_FAILURE); 4587 } 4588 if (prop_list == NULL) { 4589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4590 "sd_chk_vers1_data: %s NULL data property list.", 4591 dataname_ptr); 4592 return (SD_FAILURE); 4593 } 4594 4595 /* Display a warning if undefined bits are set in the flags */ 4596 if (flags & ~SD_CONF_BIT_MASK) { 4597 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4598 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4599 "Properties not set.", 4600 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4601 return (SD_FAILURE); 4602 } 4603 4604 /* 4605 * Verify the length of the list by identifying the highest bit set 4606 * in the flags and validating that the property list has a length 4607 * up to the index of this bit. 4608 */ 4609 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4610 if (flags & mask) { 4611 index++; 4612 } 4613 mask = 1 << i; 4614 } 4615 if (list_len < (index + 2)) { 4616 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4617 "sd_chk_vers1_data: " 4618 "Data property list %s size is incorrect. " 4619 "Properties not set.", dataname_ptr); 4620 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4621 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4622 return (SD_FAILURE); 4623 } 4624 return (SD_SUCCESS); 4625 } 4626 4627 4628 /* 4629 * Function: sd_set_vers1_properties 4630 * 4631 * Description: Set version 1 device properties based on a property list 4632 * retrieved from the driver configuration file or static 4633 * configuration table. Version 1 properties have the format: 4634 * 4635 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4636 * 4637 * where the prop0 value will be used to set prop0 if bit0 4638 * is set in the flags 4639 * 4640 * Arguments: un - driver soft state (unit) structure 4641 * flags - integer mask indicating properties to be set 4642 * prop_list - integer list of property values 4643 */ 4644 4645 static void 4646 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4647 { 4648 ASSERT(un != NULL); 4649 4650 /* 4651 * Set the flag to indicate cache is to be disabled. An attempt 4652 * to disable the cache via sd_cache_control() will be made 4653 * later during attach once the basic initialization is complete. 4654 */ 4655 if (flags & SD_CONF_BSET_NOCACHE) { 4656 un->un_f_opt_disable_cache = TRUE; 4657 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4658 "sd_set_vers1_properties: caching disabled flag set\n"); 4659 } 4660 4661 /* CD-specific configuration parameters */ 4662 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4663 un->un_f_cfg_playmsf_bcd = TRUE; 4664 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4665 "sd_set_vers1_properties: playmsf_bcd set\n"); 4666 } 4667 if (flags & SD_CONF_BSET_READSUB_BCD) { 4668 un->un_f_cfg_readsub_bcd = TRUE; 4669 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4670 "sd_set_vers1_properties: readsub_bcd set\n"); 4671 } 4672 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4673 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4674 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4675 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4676 } 4677 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4678 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4679 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4680 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4681 } 4682 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4683 un->un_f_cfg_no_read_header = TRUE; 4684 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4685 "sd_set_vers1_properties: no_read_header set\n"); 4686 } 4687 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4688 un->un_f_cfg_read_cd_xd4 = TRUE; 4689 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4690 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4691 } 4692 4693 /* Support for devices which do not have valid/unique serial numbers */ 4694 if (flags & SD_CONF_BSET_FAB_DEVID) { 4695 un->un_f_opt_fab_devid = TRUE; 4696 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4697 "sd_set_vers1_properties: fab_devid bit set\n"); 4698 } 4699 4700 /* Support for user throttle configuration */ 4701 if (flags & SD_CONF_BSET_THROTTLE) { 4702 ASSERT(prop_list != NULL); 4703 un->un_saved_throttle = un->un_throttle = 4704 prop_list->sdt_throttle; 4705 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4706 "sd_set_vers1_properties: throttle set to %d\n", 4707 prop_list->sdt_throttle); 4708 } 4709 4710 /* Set the per disk retry count according to the conf file or table. */ 4711 if (flags & SD_CONF_BSET_NRR_COUNT) { 4712 ASSERT(prop_list != NULL); 4713 if (prop_list->sdt_not_rdy_retries) { 4714 un->un_notready_retry_count = 4715 prop_list->sdt_not_rdy_retries; 4716 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4717 "sd_set_vers1_properties: not ready retry count" 4718 " set to %d\n", un->un_notready_retry_count); 4719 } 4720 } 4721 4722 /* The controller type is reported for generic disk driver ioctls */ 4723 if (flags & SD_CONF_BSET_CTYPE) { 4724 ASSERT(prop_list != NULL); 4725 switch (prop_list->sdt_ctype) { 4726 case CTYPE_CDROM: 4727 un->un_ctype = prop_list->sdt_ctype; 4728 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4729 "sd_set_vers1_properties: ctype set to " 4730 "CTYPE_CDROM\n"); 4731 break; 4732 case CTYPE_CCS: 4733 un->un_ctype = prop_list->sdt_ctype; 4734 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4735 "sd_set_vers1_properties: ctype set to " 4736 "CTYPE_CCS\n"); 4737 break; 4738 case CTYPE_ROD: /* RW optical */ 4739 un->un_ctype = prop_list->sdt_ctype; 4740 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4741 "sd_set_vers1_properties: ctype set to " 4742 "CTYPE_ROD\n"); 4743 break; 4744 default: 4745 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4746 "sd_set_vers1_properties: Could not set " 4747 "invalid ctype value (%d)", 4748 prop_list->sdt_ctype); 4749 } 4750 } 4751 4752 /* Purple failover timeout */ 4753 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4754 ASSERT(prop_list != NULL); 4755 un->un_busy_retry_count = 4756 prop_list->sdt_busy_retries; 4757 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4758 "sd_set_vers1_properties: " 4759 "busy retry count set to %d\n", 4760 un->un_busy_retry_count); 4761 } 4762 4763 /* Purple reset retry count */ 4764 if (flags & SD_CONF_BSET_RST_RETRIES) { 4765 ASSERT(prop_list != NULL); 4766 un->un_reset_retry_count = 4767 prop_list->sdt_reset_retries; 4768 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4769 "sd_set_vers1_properties: " 4770 "reset retry count set to %d\n", 4771 un->un_reset_retry_count); 4772 } 4773 4774 /* Purple reservation release timeout */ 4775 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4776 ASSERT(prop_list != NULL); 4777 un->un_reserve_release_time = 4778 prop_list->sdt_reserv_rel_time; 4779 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4780 "sd_set_vers1_properties: " 4781 "reservation release timeout set to %d\n", 4782 un->un_reserve_release_time); 4783 } 4784 4785 /* 4786 * Driver flag telling the driver to verify that no commands are pending 4787 * for a device before issuing a Test Unit Ready. This is a workaround 4788 * for a firmware bug in some Seagate eliteI drives. 4789 */ 4790 if (flags & SD_CONF_BSET_TUR_CHECK) { 4791 un->un_f_cfg_tur_check = TRUE; 4792 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4793 "sd_set_vers1_properties: tur queue check set\n"); 4794 } 4795 4796 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4797 un->un_min_throttle = prop_list->sdt_min_throttle; 4798 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4799 "sd_set_vers1_properties: min throttle set to %d\n", 4800 un->un_min_throttle); 4801 } 4802 4803 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4804 un->un_f_disksort_disabled = 4805 (prop_list->sdt_disk_sort_dis != 0) ? 4806 TRUE : FALSE; 4807 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4808 "sd_set_vers1_properties: disksort disabled " 4809 "flag set to %d\n", 4810 prop_list->sdt_disk_sort_dis); 4811 } 4812 4813 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4814 un->un_f_lun_reset_enabled = 4815 (prop_list->sdt_lun_reset_enable != 0) ? 4816 TRUE : FALSE; 4817 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4818 "sd_set_vers1_properties: lun reset enabled " 4819 "flag set to %d\n", 4820 prop_list->sdt_lun_reset_enable); 4821 } 4822 4823 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4824 un->un_f_suppress_cache_flush = 4825 (prop_list->sdt_suppress_cache_flush != 0) ? 4826 TRUE : FALSE; 4827 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4828 "sd_set_vers1_properties: suppress_cache_flush " 4829 "flag set to %d\n", 4830 prop_list->sdt_suppress_cache_flush); 4831 } 4832 4833 if (flags & SD_CONF_BSET_PC_DISABLED) { 4834 un->un_f_power_condition_disabled = 4835 (prop_list->sdt_power_condition_dis != 0) ? 4836 TRUE : FALSE; 4837 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4838 "sd_set_vers1_properties: power_condition_disabled " 4839 "flag set to %d\n", 4840 prop_list->sdt_power_condition_dis); 4841 } 4842 4843 /* 4844 * Validate the throttle values. 4845 * If any of the numbers are invalid, set everything to defaults. 4846 */ 4847 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4848 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4849 (un->un_min_throttle > un->un_throttle)) { 4850 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4851 un->un_min_throttle = sd_min_throttle; 4852 } 4853 } 4854 4855 /* 4856 * Function: sd_is_lsi() 4857 * 4858 * Description: Check for lsi devices, step through the static device 4859 * table to match vid/pid. 4860 * 4861 * Args: un - ptr to sd_lun 4862 * 4863 * Notes: When creating new LSI property, need to add the new LSI property 4864 * to this function. 4865 */ 4866 static void 4867 sd_is_lsi(struct sd_lun *un) 4868 { 4869 char *id = NULL; 4870 int table_index; 4871 int idlen; 4872 void *prop; 4873 4874 ASSERT(un != NULL); 4875 for (table_index = 0; table_index < sd_disk_table_size; 4876 table_index++) { 4877 id = sd_disk_table[table_index].device_id; 4878 idlen = strlen(id); 4879 if (idlen == 0) { 4880 continue; 4881 } 4882 4883 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4884 prop = sd_disk_table[table_index].properties; 4885 if (prop == &lsi_properties || 4886 prop == &lsi_oem_properties || 4887 prop == &lsi_properties_scsi || 4888 prop == &symbios_properties) { 4889 un->un_f_cfg_is_lsi = TRUE; 4890 } 4891 break; 4892 } 4893 } 4894 } 4895 4896 /* 4897 * Function: sd_get_physical_geometry 4898 * 4899 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4900 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4901 * target, and use this information to initialize the physical 4902 * geometry cache specified by pgeom_p. 4903 * 4904 * MODE SENSE is an optional command, so failure in this case 4905 * does not necessarily denote an error. We want to use the 4906 * MODE SENSE commands to derive the physical geometry of the 4907 * device, but if either command fails, the logical geometry is 4908 * used as the fallback for disk label geometry in cmlb. 4909 * 4910 * This requires that un->un_blockcount and un->un_tgt_blocksize 4911 * have already been initialized for the current target and 4912 * that the current values be passed as args so that we don't 4913 * end up ever trying to use -1 as a valid value. This could 4914 * happen if either value is reset while we're not holding 4915 * the mutex. 4916 * 4917 * Arguments: un - driver soft state (unit) structure 4918 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4919 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4920 * to use the USCSI "direct" chain and bypass the normal 4921 * command waitq. 4922 * 4923 * Context: Kernel thread only (can sleep). 4924 */ 4925 4926 static int 4927 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4928 diskaddr_t capacity, int lbasize, int path_flag) 4929 { 4930 struct mode_format *page3p; 4931 struct mode_geometry *page4p; 4932 struct mode_header *headerp; 4933 int sector_size; 4934 int nsect; 4935 int nhead; 4936 int ncyl; 4937 int intrlv; 4938 int spc; 4939 diskaddr_t modesense_capacity; 4940 int rpm; 4941 int bd_len; 4942 int mode_header_length; 4943 uchar_t *p3bufp; 4944 uchar_t *p4bufp; 4945 int cdbsize; 4946 int ret = EIO; 4947 sd_ssc_t *ssc; 4948 int status; 4949 4950 ASSERT(un != NULL); 4951 4952 if (lbasize == 0) { 4953 if (ISCD(un)) { 4954 lbasize = 2048; 4955 } else { 4956 lbasize = un->un_sys_blocksize; 4957 } 4958 } 4959 pgeom_p->g_secsize = (unsigned short)lbasize; 4960 4961 /* 4962 * If the unit is a cd/dvd drive MODE SENSE page three 4963 * and MODE SENSE page four are reserved (see SBC spec 4964 * and MMC spec). To prevent soft errors just return 4965 * using the default LBA size. 4966 */ 4967 if (ISCD(un)) 4968 return (ret); 4969 4970 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4971 4972 /* 4973 * Retrieve MODE SENSE page 3 - Format Device Page 4974 */ 4975 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4976 ssc = sd_ssc_init(un); 4977 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp, 4978 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag); 4979 if (status != 0) { 4980 SD_ERROR(SD_LOG_COMMON, un, 4981 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4982 goto page3_exit; 4983 } 4984 4985 /* 4986 * Determine size of Block Descriptors in order to locate the mode 4987 * page data. ATAPI devices return 0, SCSI devices should return 4988 * MODE_BLK_DESC_LENGTH. 4989 */ 4990 headerp = (struct mode_header *)p3bufp; 4991 if (un->un_f_cfg_is_atapi == TRUE) { 4992 struct mode_header_grp2 *mhp = 4993 (struct mode_header_grp2 *)headerp; 4994 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4995 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4996 } else { 4997 mode_header_length = MODE_HEADER_LENGTH; 4998 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4999 } 5000 5001 if (bd_len > MODE_BLK_DESC_LENGTH) { 5002 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5003 "sd_get_physical_geometry: received unexpected bd_len " 5004 "of %d, page3\n", bd_len); 5005 status = EIO; 5006 goto page3_exit; 5007 } 5008 5009 page3p = (struct mode_format *) 5010 ((caddr_t)headerp + mode_header_length + bd_len); 5011 5012 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5013 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5014 "sd_get_physical_geometry: mode sense pg3 code mismatch " 5015 "%d\n", page3p->mode_page.code); 5016 status = EIO; 5017 goto page3_exit; 5018 } 5019 5020 /* 5021 * Use this physical geometry data only if BOTH MODE SENSE commands 5022 * complete successfully; otherwise, revert to the logical geometry. 5023 * So, we need to save everything in temporary variables. 5024 */ 5025 sector_size = BE_16(page3p->data_bytes_sect); 5026 5027 /* 5028 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5029 */ 5030 if (sector_size == 0) { 5031 sector_size = un->un_sys_blocksize; 5032 } else { 5033 sector_size &= ~(un->un_sys_blocksize - 1); 5034 } 5035 5036 nsect = BE_16(page3p->sect_track); 5037 intrlv = BE_16(page3p->interleave); 5038 5039 SD_INFO(SD_LOG_COMMON, un, 5040 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5041 SD_INFO(SD_LOG_COMMON, un, 5042 " mode page: %d; nsect: %d; sector size: %d;\n", 5043 page3p->mode_page.code, nsect, sector_size); 5044 SD_INFO(SD_LOG_COMMON, un, 5045 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5046 BE_16(page3p->track_skew), 5047 BE_16(page3p->cylinder_skew)); 5048 5049 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5050 5051 /* 5052 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5053 */ 5054 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5055 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp, 5056 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag); 5057 if (status != 0) { 5058 SD_ERROR(SD_LOG_COMMON, un, 5059 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5060 goto page4_exit; 5061 } 5062 5063 /* 5064 * Determine size of Block Descriptors in order to locate the mode 5065 * page data. ATAPI devices return 0, SCSI devices should return 5066 * MODE_BLK_DESC_LENGTH. 5067 */ 5068 headerp = (struct mode_header *)p4bufp; 5069 if (un->un_f_cfg_is_atapi == TRUE) { 5070 struct mode_header_grp2 *mhp = 5071 (struct mode_header_grp2 *)headerp; 5072 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5073 } else { 5074 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5075 } 5076 5077 if (bd_len > MODE_BLK_DESC_LENGTH) { 5078 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5079 "sd_get_physical_geometry: received unexpected bd_len of " 5080 "%d, page4\n", bd_len); 5081 status = EIO; 5082 goto page4_exit; 5083 } 5084 5085 page4p = (struct mode_geometry *) 5086 ((caddr_t)headerp + mode_header_length + bd_len); 5087 5088 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5089 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5090 "sd_get_physical_geometry: mode sense pg4 code mismatch " 5091 "%d\n", page4p->mode_page.code); 5092 status = EIO; 5093 goto page4_exit; 5094 } 5095 5096 /* 5097 * Stash the data now, after we know that both commands completed. 5098 */ 5099 5100 5101 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5102 spc = nhead * nsect; 5103 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5104 rpm = BE_16(page4p->rpm); 5105 5106 modesense_capacity = spc * ncyl; 5107 5108 SD_INFO(SD_LOG_COMMON, un, 5109 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5110 SD_INFO(SD_LOG_COMMON, un, 5111 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5112 SD_INFO(SD_LOG_COMMON, un, 5113 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5114 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5115 (void *)pgeom_p, capacity); 5116 5117 /* 5118 * Compensate if the drive's geometry is not rectangular, i.e., 5119 * the product of C * H * S returned by MODE SENSE >= that returned 5120 * by read capacity. This is an idiosyncrasy of the original x86 5121 * disk subsystem. 5122 */ 5123 if (modesense_capacity >= capacity) { 5124 SD_INFO(SD_LOG_COMMON, un, 5125 "sd_get_physical_geometry: adjusting acyl; " 5126 "old: %d; new: %d\n", pgeom_p->g_acyl, 5127 (modesense_capacity - capacity + spc - 1) / spc); 5128 if (sector_size != 0) { 5129 /* 1243403: NEC D38x7 drives don't support sec size */ 5130 pgeom_p->g_secsize = (unsigned short)sector_size; 5131 } 5132 pgeom_p->g_nsect = (unsigned short)nsect; 5133 pgeom_p->g_nhead = (unsigned short)nhead; 5134 pgeom_p->g_capacity = capacity; 5135 pgeom_p->g_acyl = 5136 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5137 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5138 } 5139 5140 pgeom_p->g_rpm = (unsigned short)rpm; 5141 pgeom_p->g_intrlv = (unsigned short)intrlv; 5142 ret = 0; 5143 5144 SD_INFO(SD_LOG_COMMON, un, 5145 "sd_get_physical_geometry: mode sense geometry:\n"); 5146 SD_INFO(SD_LOG_COMMON, un, 5147 " nsect: %d; sector size: %d; interlv: %d\n", 5148 nsect, sector_size, intrlv); 5149 SD_INFO(SD_LOG_COMMON, un, 5150 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5151 nhead, ncyl, rpm, modesense_capacity); 5152 SD_INFO(SD_LOG_COMMON, un, 5153 "sd_get_physical_geometry: (cached)\n"); 5154 SD_INFO(SD_LOG_COMMON, un, 5155 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5156 pgeom_p->g_ncyl, pgeom_p->g_acyl, 5157 pgeom_p->g_nhead, pgeom_p->g_nsect); 5158 SD_INFO(SD_LOG_COMMON, un, 5159 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5160 pgeom_p->g_secsize, pgeom_p->g_capacity, 5161 pgeom_p->g_intrlv, pgeom_p->g_rpm); 5162 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5163 5164 page4_exit: 5165 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5166 5167 page3_exit: 5168 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5169 5170 if (status != 0) { 5171 if (status == EIO) { 5172 /* 5173 * Some disks do not support mode sense(6), we 5174 * should ignore this kind of error(sense key is 5175 * 0x5 - illegal request). 5176 */ 5177 uint8_t *sensep; 5178 int senlen; 5179 5180 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 5181 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 5182 ssc->ssc_uscsi_cmd->uscsi_rqresid); 5183 5184 if (senlen > 0 && 5185 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 5186 sd_ssc_assessment(ssc, 5187 SD_FMT_IGNORE_COMPROMISE); 5188 } else { 5189 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 5190 } 5191 } else { 5192 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5193 } 5194 } 5195 sd_ssc_fini(ssc); 5196 return (ret); 5197 } 5198 5199 /* 5200 * Function: sd_get_virtual_geometry 5201 * 5202 * Description: Ask the controller to tell us about the target device. 5203 * 5204 * Arguments: un - pointer to softstate 5205 * capacity - disk capacity in #blocks 5206 * lbasize - disk block size in bytes 5207 * 5208 * Context: Kernel thread only 5209 */ 5210 5211 static int 5212 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 5213 diskaddr_t capacity, int lbasize) 5214 { 5215 uint_t geombuf; 5216 int spc; 5217 5218 ASSERT(un != NULL); 5219 5220 /* Set sector size, and total number of sectors */ 5221 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5222 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5223 5224 /* Let the HBA tell us its geometry */ 5225 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5226 5227 /* A value of -1 indicates an undefined "geometry" property */ 5228 if (geombuf == (-1)) { 5229 return (EINVAL); 5230 } 5231 5232 /* Initialize the logical geometry cache. */ 5233 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5234 lgeom_p->g_nsect = geombuf & 0xffff; 5235 lgeom_p->g_secsize = un->un_sys_blocksize; 5236 5237 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5238 5239 /* 5240 * Note: The driver originally converted the capacity value from 5241 * target blocks to system blocks. However, the capacity value passed 5242 * to this routine is already in terms of system blocks (this scaling 5243 * is done when the READ CAPACITY command is issued and processed). 5244 * This 'error' may have gone undetected because the usage of g_ncyl 5245 * (which is based upon g_capacity) is very limited within the driver 5246 */ 5247 lgeom_p->g_capacity = capacity; 5248 5249 /* 5250 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5251 * hba may return zero values if the device has been removed. 5252 */ 5253 if (spc == 0) { 5254 lgeom_p->g_ncyl = 0; 5255 } else { 5256 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5257 } 5258 lgeom_p->g_acyl = 0; 5259 5260 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5261 return (0); 5262 5263 } 5264 /* 5265 * Function: sd_update_block_info 5266 * 5267 * Description: Calculate a byte count to sector count bitshift value 5268 * from sector size. 5269 * 5270 * Arguments: un: unit struct. 5271 * lbasize: new target sector size 5272 * capacity: new target capacity, ie. block count 5273 * 5274 * Context: Kernel thread context 5275 */ 5276 5277 static void 5278 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5279 { 5280 if (lbasize != 0) { 5281 un->un_tgt_blocksize = lbasize; 5282 un->un_f_tgt_blocksize_is_valid = TRUE; 5283 if (!un->un_f_has_removable_media) { 5284 un->un_sys_blocksize = lbasize; 5285 } 5286 } 5287 5288 if (capacity != 0) { 5289 un->un_blockcount = capacity; 5290 un->un_f_blockcount_is_valid = TRUE; 5291 5292 /* 5293 * The capacity has changed so update the errstats. 5294 */ 5295 if (un->un_errstats != NULL) { 5296 struct sd_errstats *stp; 5297 5298 capacity *= un->un_sys_blocksize; 5299 stp = (struct sd_errstats *)un->un_errstats->ks_data; 5300 if (stp->sd_capacity.value.ui64 < capacity) 5301 stp->sd_capacity.value.ui64 = capacity; 5302 } 5303 } 5304 } 5305 5306 5307 /* 5308 * Function: sd_register_devid 5309 * 5310 * Description: This routine will obtain the device id information from the 5311 * target, obtain the serial number, and register the device 5312 * id with the ddi framework. 5313 * 5314 * Arguments: devi - the system's dev_info_t for the device. 5315 * un - driver soft state (unit) structure 5316 * reservation_flag - indicates if a reservation conflict 5317 * occurred during attach 5318 * 5319 * Context: Kernel Thread 5320 */ 5321 static void 5322 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag) 5323 { 5324 int rval = 0; 5325 uchar_t *inq80 = NULL; 5326 size_t inq80_len = MAX_INQUIRY_SIZE; 5327 size_t inq80_resid = 0; 5328 uchar_t *inq83 = NULL; 5329 size_t inq83_len = MAX_INQUIRY_SIZE; 5330 size_t inq83_resid = 0; 5331 int dlen, len; 5332 char *sn; 5333 struct sd_lun *un; 5334 5335 ASSERT(ssc != NULL); 5336 un = ssc->ssc_un; 5337 ASSERT(un != NULL); 5338 ASSERT(mutex_owned(SD_MUTEX(un))); 5339 ASSERT((SD_DEVINFO(un)) == devi); 5340 5341 5342 /* 5343 * We check the availability of the World Wide Name (0x83) and Unit 5344 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5345 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5346 * 0x83 is available, that is the best choice. Our next choice is 5347 * 0x80. If neither are available, we munge the devid from the device 5348 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5349 * to fabricate a devid for non-Sun qualified disks. 5350 */ 5351 if (sd_check_vpd_page_support(ssc) == 0) { 5352 /* collect page 80 data if available */ 5353 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5354 5355 mutex_exit(SD_MUTEX(un)); 5356 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5357 5358 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len, 5359 0x01, 0x80, &inq80_resid); 5360 5361 if (rval != 0) { 5362 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5363 kmem_free(inq80, inq80_len); 5364 inq80 = NULL; 5365 inq80_len = 0; 5366 } else if (ddi_prop_exists( 5367 DDI_DEV_T_NONE, SD_DEVINFO(un), 5368 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 5369 INQUIRY_SERIAL_NO) == 0) { 5370 /* 5371 * If we don't already have a serial number 5372 * property, do quick verify of data returned 5373 * and define property. 5374 */ 5375 dlen = inq80_len - inq80_resid; 5376 len = (size_t)inq80[3]; 5377 if ((dlen >= 4) && ((len + 4) <= dlen)) { 5378 /* 5379 * Ensure sn termination, skip leading 5380 * blanks, and create property 5381 * 'inquiry-serial-no'. 5382 */ 5383 sn = (char *)&inq80[4]; 5384 sn[len] = 0; 5385 while (*sn && (*sn == ' ')) 5386 sn++; 5387 if (*sn) { 5388 (void) ddi_prop_update_string( 5389 DDI_DEV_T_NONE, 5390 SD_DEVINFO(un), 5391 INQUIRY_SERIAL_NO, sn); 5392 } 5393 } 5394 } 5395 mutex_enter(SD_MUTEX(un)); 5396 } 5397 5398 /* collect page 83 data if available */ 5399 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5400 mutex_exit(SD_MUTEX(un)); 5401 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5402 5403 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len, 5404 0x01, 0x83, &inq83_resid); 5405 5406 if (rval != 0) { 5407 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5408 kmem_free(inq83, inq83_len); 5409 inq83 = NULL; 5410 inq83_len = 0; 5411 } 5412 mutex_enter(SD_MUTEX(un)); 5413 } 5414 } 5415 5416 /* 5417 * If transport has already registered a devid for this target 5418 * then that takes precedence over the driver's determination 5419 * of the devid. 5420 * 5421 * NOTE: The reason this check is done here instead of at the beginning 5422 * of the function is to allow the code above to create the 5423 * 'inquiry-serial-no' property. 5424 */ 5425 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 5426 ASSERT(un->un_devid); 5427 un->un_f_devid_transport_defined = TRUE; 5428 goto cleanup; /* use devid registered by the transport */ 5429 } 5430 5431 /* 5432 * This is the case of antiquated Sun disk drives that have the 5433 * FAB_DEVID property set in the disk_table. These drives 5434 * manage the devid's by storing them in last 2 available sectors 5435 * on the drive and have them fabricated by the ddi layer by calling 5436 * ddi_devid_init and passing the DEVID_FAB flag. 5437 */ 5438 if (un->un_f_opt_fab_devid == TRUE) { 5439 /* 5440 * Depending on EINVAL isn't reliable, since a reserved disk 5441 * may result in invalid geometry, so check to make sure a 5442 * reservation conflict did not occur during attach. 5443 */ 5444 if ((sd_get_devid(ssc) == EINVAL) && 5445 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5446 /* 5447 * The devid is invalid AND there is no reservation 5448 * conflict. Fabricate a new devid. 5449 */ 5450 (void) sd_create_devid(ssc); 5451 } 5452 5453 /* Register the devid if it exists */ 5454 if (un->un_devid != NULL) { 5455 (void) ddi_devid_register(SD_DEVINFO(un), 5456 un->un_devid); 5457 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5458 "sd_register_devid: Devid Fabricated\n"); 5459 } 5460 goto cleanup; 5461 } 5462 5463 /* encode best devid possible based on data available */ 5464 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5465 (char *)ddi_driver_name(SD_DEVINFO(un)), 5466 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5467 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5468 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5469 5470 /* devid successfully encoded, register devid */ 5471 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5472 5473 } else { 5474 /* 5475 * Unable to encode a devid based on data available. 5476 * This is not a Sun qualified disk. Older Sun disk 5477 * drives that have the SD_FAB_DEVID property 5478 * set in the disk_table and non Sun qualified 5479 * disks are treated in the same manner. These 5480 * drives manage the devid's by storing them in 5481 * last 2 available sectors on the drive and 5482 * have them fabricated by the ddi layer by 5483 * calling ddi_devid_init and passing the 5484 * DEVID_FAB flag. 5485 * Create a fabricate devid only if there's no 5486 * fabricate devid existed. 5487 */ 5488 if (sd_get_devid(ssc) == EINVAL) { 5489 (void) sd_create_devid(ssc); 5490 } 5491 un->un_f_opt_fab_devid = TRUE; 5492 5493 /* Register the devid if it exists */ 5494 if (un->un_devid != NULL) { 5495 (void) ddi_devid_register(SD_DEVINFO(un), 5496 un->un_devid); 5497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5498 "sd_register_devid: devid fabricated using " 5499 "ddi framework\n"); 5500 } 5501 } 5502 5503 cleanup: 5504 /* clean up resources */ 5505 if (inq80 != NULL) { 5506 kmem_free(inq80, inq80_len); 5507 } 5508 if (inq83 != NULL) { 5509 kmem_free(inq83, inq83_len); 5510 } 5511 } 5512 5513 5514 5515 /* 5516 * Function: sd_get_devid 5517 * 5518 * Description: This routine will return 0 if a valid device id has been 5519 * obtained from the target and stored in the soft state. If a 5520 * valid device id has not been previously read and stored, a 5521 * read attempt will be made. 5522 * 5523 * Arguments: un - driver soft state (unit) structure 5524 * 5525 * Return Code: 0 if we successfully get the device id 5526 * 5527 * Context: Kernel Thread 5528 */ 5529 5530 static int 5531 sd_get_devid(sd_ssc_t *ssc) 5532 { 5533 struct dk_devid *dkdevid; 5534 ddi_devid_t tmpid; 5535 uint_t *ip; 5536 size_t sz; 5537 diskaddr_t blk; 5538 int status; 5539 int chksum; 5540 int i; 5541 size_t buffer_size; 5542 struct sd_lun *un; 5543 5544 ASSERT(ssc != NULL); 5545 un = ssc->ssc_un; 5546 ASSERT(un != NULL); 5547 ASSERT(mutex_owned(SD_MUTEX(un))); 5548 5549 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5550 un); 5551 5552 if (un->un_devid != NULL) { 5553 return (0); 5554 } 5555 5556 mutex_exit(SD_MUTEX(un)); 5557 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5558 (void *)SD_PATH_DIRECT) != 0) { 5559 mutex_enter(SD_MUTEX(un)); 5560 return (EINVAL); 5561 } 5562 5563 /* 5564 * Read and verify device id, stored in the reserved cylinders at the 5565 * end of the disk. Backup label is on the odd sectors of the last 5566 * track of the last cylinder. Device id will be on track of the next 5567 * to last cylinder. 5568 */ 5569 mutex_enter(SD_MUTEX(un)); 5570 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5571 mutex_exit(SD_MUTEX(un)); 5572 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5573 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk, 5574 SD_PATH_DIRECT); 5575 5576 if (status != 0) { 5577 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5578 goto error; 5579 } 5580 5581 /* Validate the revision */ 5582 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5583 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5584 status = EINVAL; 5585 goto error; 5586 } 5587 5588 /* Calculate the checksum */ 5589 chksum = 0; 5590 ip = (uint_t *)dkdevid; 5591 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5592 i++) { 5593 chksum ^= ip[i]; 5594 } 5595 5596 /* Compare the checksums */ 5597 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5598 status = EINVAL; 5599 goto error; 5600 } 5601 5602 /* Validate the device id */ 5603 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 5604 status = EINVAL; 5605 goto error; 5606 } 5607 5608 /* 5609 * Store the device id in the driver soft state 5610 */ 5611 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 5612 tmpid = kmem_alloc(sz, KM_SLEEP); 5613 5614 mutex_enter(SD_MUTEX(un)); 5615 5616 un->un_devid = tmpid; 5617 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 5618 5619 kmem_free(dkdevid, buffer_size); 5620 5621 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 5622 5623 return (status); 5624 error: 5625 mutex_enter(SD_MUTEX(un)); 5626 kmem_free(dkdevid, buffer_size); 5627 return (status); 5628 } 5629 5630 5631 /* 5632 * Function: sd_create_devid 5633 * 5634 * Description: This routine will fabricate the device id and write it 5635 * to the disk. 5636 * 5637 * Arguments: un - driver soft state (unit) structure 5638 * 5639 * Return Code: value of the fabricated device id 5640 * 5641 * Context: Kernel Thread 5642 */ 5643 5644 static ddi_devid_t 5645 sd_create_devid(sd_ssc_t *ssc) 5646 { 5647 struct sd_lun *un; 5648 5649 ASSERT(ssc != NULL); 5650 un = ssc->ssc_un; 5651 ASSERT(un != NULL); 5652 5653 /* Fabricate the devid */ 5654 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 5655 == DDI_FAILURE) { 5656 return (NULL); 5657 } 5658 5659 /* Write the devid to disk */ 5660 if (sd_write_deviceid(ssc) != 0) { 5661 ddi_devid_free(un->un_devid); 5662 un->un_devid = NULL; 5663 } 5664 5665 return (un->un_devid); 5666 } 5667 5668 5669 /* 5670 * Function: sd_write_deviceid 5671 * 5672 * Description: This routine will write the device id to the disk 5673 * reserved sector. 5674 * 5675 * Arguments: un - driver soft state (unit) structure 5676 * 5677 * Return Code: EINVAL 5678 * value returned by sd_send_scsi_cmd 5679 * 5680 * Context: Kernel Thread 5681 */ 5682 5683 static int 5684 sd_write_deviceid(sd_ssc_t *ssc) 5685 { 5686 struct dk_devid *dkdevid; 5687 uchar_t *buf; 5688 diskaddr_t blk; 5689 uint_t *ip, chksum; 5690 int status; 5691 int i; 5692 struct sd_lun *un; 5693 5694 ASSERT(ssc != NULL); 5695 un = ssc->ssc_un; 5696 ASSERT(un != NULL); 5697 ASSERT(mutex_owned(SD_MUTEX(un))); 5698 5699 mutex_exit(SD_MUTEX(un)); 5700 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5701 (void *)SD_PATH_DIRECT) != 0) { 5702 mutex_enter(SD_MUTEX(un)); 5703 return (-1); 5704 } 5705 5706 5707 /* Allocate the buffer */ 5708 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 5709 dkdevid = (struct dk_devid *)buf; 5710 5711 /* Fill in the revision */ 5712 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5713 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5714 5715 /* Copy in the device id */ 5716 mutex_enter(SD_MUTEX(un)); 5717 bcopy(un->un_devid, &dkdevid->dkd_devid, 5718 ddi_devid_sizeof(un->un_devid)); 5719 mutex_exit(SD_MUTEX(un)); 5720 5721 /* Calculate the checksum */ 5722 chksum = 0; 5723 ip = (uint_t *)dkdevid; 5724 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5725 i++) { 5726 chksum ^= ip[i]; 5727 } 5728 5729 /* Fill-in checksum */ 5730 DKD_FORMCHKSUM(chksum, dkdevid); 5731 5732 /* Write the reserved sector */ 5733 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk, 5734 SD_PATH_DIRECT); 5735 if (status != 0) 5736 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5737 5738 kmem_free(buf, un->un_sys_blocksize); 5739 5740 mutex_enter(SD_MUTEX(un)); 5741 return (status); 5742 } 5743 5744 5745 /* 5746 * Function: sd_check_vpd_page_support 5747 * 5748 * Description: This routine sends an inquiry command with the EVPD bit set and 5749 * a page code of 0x00 to the device. It is used to determine which 5750 * vital product pages are available to find the devid. We are 5751 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1, 5752 * the device does not support that command. 5753 * 5754 * Arguments: un - driver soft state (unit) structure 5755 * 5756 * Return Code: 0 - success 5757 * 1 - check condition 5758 * 5759 * Context: This routine can sleep. 5760 */ 5761 5762 static int 5763 sd_check_vpd_page_support(sd_ssc_t *ssc) 5764 { 5765 uchar_t *page_list = NULL; 5766 uchar_t page_length = 0xff; /* Use max possible length */ 5767 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5768 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5769 int rval = 0; 5770 int counter; 5771 struct sd_lun *un; 5772 5773 ASSERT(ssc != NULL); 5774 un = ssc->ssc_un; 5775 ASSERT(un != NULL); 5776 ASSERT(mutex_owned(SD_MUTEX(un))); 5777 5778 mutex_exit(SD_MUTEX(un)); 5779 5780 /* 5781 * We'll set the page length to the maximum to save figuring it out 5782 * with an additional call. 5783 */ 5784 page_list = kmem_zalloc(page_length, KM_SLEEP); 5785 5786 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd, 5787 page_code, NULL); 5788 5789 if (rval != 0) 5790 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5791 5792 mutex_enter(SD_MUTEX(un)); 5793 5794 /* 5795 * Now we must validate that the device accepted the command, as some 5796 * drives do not support it. If the drive does support it, we will 5797 * return 0, and the supported pages will be in un_vpd_page_mask. If 5798 * not, we return -1. 5799 */ 5800 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5801 /* Loop to find one of the 2 pages we need */ 5802 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5803 5804 /* 5805 * Pages are returned in ascending order, and 0x83 is what we 5806 * are hoping for. 5807 */ 5808 while ((page_list[counter] <= 0xB1) && 5809 (counter <= (page_list[VPD_PAGE_LENGTH] + 5810 VPD_HEAD_OFFSET))) { 5811 /* 5812 * Add 3 because page_list[3] is the number of 5813 * pages minus 3 5814 */ 5815 5816 switch (page_list[counter]) { 5817 case 0x00: 5818 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5819 break; 5820 case 0x80: 5821 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5822 break; 5823 case 0x81: 5824 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5825 break; 5826 case 0x82: 5827 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5828 break; 5829 case 0x83: 5830 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5831 break; 5832 case 0x86: 5833 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5834 break; 5835 case 0xB1: 5836 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG; 5837 break; 5838 } 5839 counter++; 5840 } 5841 5842 } else { 5843 rval = -1; 5844 5845 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5846 "sd_check_vpd_page_support: This drive does not implement " 5847 "VPD pages.\n"); 5848 } 5849 5850 kmem_free(page_list, page_length); 5851 5852 return (rval); 5853 } 5854 5855 5856 /* 5857 * Function: sd_setup_pm 5858 * 5859 * Description: Initialize Power Management on the device 5860 * 5861 * Context: Kernel Thread 5862 */ 5863 5864 static void 5865 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi) 5866 { 5867 uint_t log_page_size; 5868 uchar_t *log_page_data; 5869 int rval = 0; 5870 struct sd_lun *un; 5871 5872 ASSERT(ssc != NULL); 5873 un = ssc->ssc_un; 5874 ASSERT(un != NULL); 5875 5876 /* 5877 * Since we are called from attach, holding a mutex for 5878 * un is unnecessary. Because some of the routines called 5879 * from here require SD_MUTEX to not be held, assert this 5880 * right up front. 5881 */ 5882 ASSERT(!mutex_owned(SD_MUTEX(un))); 5883 /* 5884 * Since the sd device does not have the 'reg' property, 5885 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5886 * The following code is to tell cpr that this device 5887 * DOES need to be suspended and resumed. 5888 */ 5889 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5890 "pm-hardware-state", "needs-suspend-resume"); 5891 5892 /* 5893 * This complies with the new power management framework 5894 * for certain desktop machines. Create the pm_components 5895 * property as a string array property. 5896 * If un_f_pm_supported is TRUE, that means the disk 5897 * attached HBA has set the "pm-capable" property and 5898 * the value of this property is bigger than 0. 5899 */ 5900 if (un->un_f_pm_supported) { 5901 /* 5902 * not all devices have a motor, try it first. 5903 * some devices may return ILLEGAL REQUEST, some 5904 * will hang 5905 * The following START_STOP_UNIT is used to check if target 5906 * device has a motor. 5907 */ 5908 un->un_f_start_stop_supported = TRUE; 5909 5910 if (un->un_f_power_condition_supported) { 5911 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5912 SD_POWER_CONDITION, SD_TARGET_ACTIVE, 5913 SD_PATH_DIRECT); 5914 if (rval != 0) { 5915 un->un_f_power_condition_supported = FALSE; 5916 } 5917 } 5918 if (!un->un_f_power_condition_supported) { 5919 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5920 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT); 5921 } 5922 if (rval != 0) { 5923 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5924 un->un_f_start_stop_supported = FALSE; 5925 } 5926 5927 /* 5928 * create pm properties anyways otherwise the parent can't 5929 * go to sleep 5930 */ 5931 un->un_f_pm_is_enabled = TRUE; 5932 (void) sd_create_pm_components(devi, un); 5933 5934 /* 5935 * If it claims that log sense is supported, check it out. 5936 */ 5937 if (un->un_f_log_sense_supported) { 5938 rval = sd_log_page_supported(ssc, 5939 START_STOP_CYCLE_PAGE); 5940 if (rval == 1) { 5941 /* Page found, use it. */ 5942 un->un_start_stop_cycle_page = 5943 START_STOP_CYCLE_PAGE; 5944 } else { 5945 /* 5946 * Page not found or log sense is not 5947 * supported. 5948 * Notice we do not check the old style 5949 * START_STOP_CYCLE_VU_PAGE because this 5950 * code path does not apply to old disks. 5951 */ 5952 un->un_f_log_sense_supported = FALSE; 5953 un->un_f_pm_log_sense_smart = FALSE; 5954 } 5955 } 5956 5957 return; 5958 } 5959 5960 /* 5961 * For the disk whose attached HBA has not set the "pm-capable" 5962 * property, check if it supports the power management. 5963 */ 5964 if (!un->un_f_log_sense_supported) { 5965 un->un_power_level = SD_SPINDLE_ON; 5966 un->un_f_pm_is_enabled = FALSE; 5967 return; 5968 } 5969 5970 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE); 5971 5972 #ifdef SDDEBUG 5973 if (sd_force_pm_supported) { 5974 /* Force a successful result */ 5975 rval = 1; 5976 } 5977 #endif 5978 5979 /* 5980 * If the start-stop cycle counter log page is not supported 5981 * or if the pm-capable property is set to be false (0), 5982 * then we should not create the pm_components property. 5983 */ 5984 if (rval == -1) { 5985 /* 5986 * Error. 5987 * Reading log sense failed, most likely this is 5988 * an older drive that does not support log sense. 5989 * If this fails auto-pm is not supported. 5990 */ 5991 un->un_power_level = SD_SPINDLE_ON; 5992 un->un_f_pm_is_enabled = FALSE; 5993 5994 } else if (rval == 0) { 5995 /* 5996 * Page not found. 5997 * The start stop cycle counter is implemented as page 5998 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5999 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6000 */ 6001 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) { 6002 /* 6003 * Page found, use this one. 6004 */ 6005 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6006 un->un_f_pm_is_enabled = TRUE; 6007 } else { 6008 /* 6009 * Error or page not found. 6010 * auto-pm is not supported for this device. 6011 */ 6012 un->un_power_level = SD_SPINDLE_ON; 6013 un->un_f_pm_is_enabled = FALSE; 6014 } 6015 } else { 6016 /* 6017 * Page found, use it. 6018 */ 6019 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6020 un->un_f_pm_is_enabled = TRUE; 6021 } 6022 6023 6024 if (un->un_f_pm_is_enabled == TRUE) { 6025 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6026 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6027 6028 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6029 log_page_size, un->un_start_stop_cycle_page, 6030 0x01, 0, SD_PATH_DIRECT); 6031 6032 if (rval != 0) { 6033 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6034 } 6035 6036 #ifdef SDDEBUG 6037 if (sd_force_pm_supported) { 6038 /* Force a successful result */ 6039 rval = 0; 6040 } 6041 #endif 6042 6043 /* 6044 * If the Log sense for Page( Start/stop cycle counter page) 6045 * succeeds, then power management is supported and we can 6046 * enable auto-pm. 6047 */ 6048 if (rval == 0) { 6049 (void) sd_create_pm_components(devi, un); 6050 } else { 6051 un->un_power_level = SD_SPINDLE_ON; 6052 un->un_f_pm_is_enabled = FALSE; 6053 } 6054 6055 kmem_free(log_page_data, log_page_size); 6056 } 6057 } 6058 6059 6060 /* 6061 * Function: sd_create_pm_components 6062 * 6063 * Description: Initialize PM property. 6064 * 6065 * Context: Kernel thread context 6066 */ 6067 6068 static void 6069 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6070 { 6071 ASSERT(!mutex_owned(SD_MUTEX(un))); 6072 6073 if (un->un_f_power_condition_supported) { 6074 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6075 "pm-components", sd_pwr_pc.pm_comp, 5) 6076 != DDI_PROP_SUCCESS) { 6077 un->un_power_level = SD_SPINDLE_ACTIVE; 6078 un->un_f_pm_is_enabled = FALSE; 6079 return; 6080 } 6081 } else { 6082 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6083 "pm-components", sd_pwr_ss.pm_comp, 3) 6084 != DDI_PROP_SUCCESS) { 6085 un->un_power_level = SD_SPINDLE_ON; 6086 un->un_f_pm_is_enabled = FALSE; 6087 return; 6088 } 6089 } 6090 /* 6091 * When components are initially created they are idle, 6092 * power up any non-removables. 6093 * Note: the return value of pm_raise_power can't be used 6094 * for determining if PM should be enabled for this device. 6095 * Even if you check the return values and remove this 6096 * property created above, the PM framework will not honor the 6097 * change after the first call to pm_raise_power. Hence, 6098 * removal of that property does not help if pm_raise_power 6099 * fails. In the case of removable media, the start/stop 6100 * will fail if the media is not present. 6101 */ 6102 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6103 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) { 6104 mutex_enter(SD_MUTEX(un)); 6105 un->un_power_level = SD_PM_STATE_ACTIVE(un); 6106 mutex_enter(&un->un_pm_mutex); 6107 /* Set to on and not busy. */ 6108 un->un_pm_count = 0; 6109 } else { 6110 mutex_enter(SD_MUTEX(un)); 6111 un->un_power_level = SD_PM_STATE_STOPPED(un); 6112 mutex_enter(&un->un_pm_mutex); 6113 /* Set to off. */ 6114 un->un_pm_count = -1; 6115 } 6116 mutex_exit(&un->un_pm_mutex); 6117 mutex_exit(SD_MUTEX(un)); 6118 } 6119 6120 6121 /* 6122 * Function: sd_ddi_suspend 6123 * 6124 * Description: Performs system power-down operations. This includes 6125 * setting the drive state to indicate its suspended so 6126 * that no new commands will be accepted. Also, wait for 6127 * all commands that are in transport or queued to a timer 6128 * for retry to complete. All timeout threads are cancelled. 6129 * 6130 * Return Code: DDI_FAILURE or DDI_SUCCESS 6131 * 6132 * Context: Kernel thread context 6133 */ 6134 6135 static int 6136 sd_ddi_suspend(dev_info_t *devi) 6137 { 6138 struct sd_lun *un; 6139 clock_t wait_cmds_complete; 6140 6141 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6142 if (un == NULL) { 6143 return (DDI_FAILURE); 6144 } 6145 6146 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6147 6148 mutex_enter(SD_MUTEX(un)); 6149 6150 /* Return success if the device is already suspended. */ 6151 if (un->un_state == SD_STATE_SUSPENDED) { 6152 mutex_exit(SD_MUTEX(un)); 6153 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6154 "device already suspended, exiting\n"); 6155 return (DDI_SUCCESS); 6156 } 6157 6158 /* Return failure if the device is being used by HA */ 6159 if (un->un_resvd_status & 6160 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6161 mutex_exit(SD_MUTEX(un)); 6162 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6163 "device in use by HA, exiting\n"); 6164 return (DDI_FAILURE); 6165 } 6166 6167 /* 6168 * Return failure if the device is in a resource wait 6169 * or power changing state. 6170 */ 6171 if ((un->un_state == SD_STATE_RWAIT) || 6172 (un->un_state == SD_STATE_PM_CHANGING)) { 6173 mutex_exit(SD_MUTEX(un)); 6174 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6175 "device in resource wait state, exiting\n"); 6176 return (DDI_FAILURE); 6177 } 6178 6179 6180 un->un_save_state = un->un_last_state; 6181 New_state(un, SD_STATE_SUSPENDED); 6182 6183 /* 6184 * Wait for all commands that are in transport or queued to a timer 6185 * for retry to complete. 6186 * 6187 * While waiting, no new commands will be accepted or sent because of 6188 * the new state we set above. 6189 * 6190 * Wait till current operation has completed. If we are in the resource 6191 * wait state (with an intr outstanding) then we need to wait till the 6192 * intr completes and starts the next cmd. We want to wait for 6193 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6194 */ 6195 wait_cmds_complete = ddi_get_lbolt() + 6196 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6197 6198 while (un->un_ncmds_in_transport != 0) { 6199 /* 6200 * Fail if commands do not finish in the specified time. 6201 */ 6202 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6203 wait_cmds_complete) == -1) { 6204 /* 6205 * Undo the state changes made above. Everything 6206 * must go back to it's original value. 6207 */ 6208 Restore_state(un); 6209 un->un_last_state = un->un_save_state; 6210 /* Wake up any threads that might be waiting. */ 6211 cv_broadcast(&un->un_suspend_cv); 6212 mutex_exit(SD_MUTEX(un)); 6213 SD_ERROR(SD_LOG_IO_PM, un, 6214 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6215 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6216 return (DDI_FAILURE); 6217 } 6218 } 6219 6220 /* 6221 * Cancel SCSI watch thread and timeouts, if any are active 6222 */ 6223 6224 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6225 opaque_t temp_token = un->un_swr_token; 6226 mutex_exit(SD_MUTEX(un)); 6227 scsi_watch_suspend(temp_token); 6228 mutex_enter(SD_MUTEX(un)); 6229 } 6230 6231 if (un->un_reset_throttle_timeid != NULL) { 6232 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6233 un->un_reset_throttle_timeid = NULL; 6234 mutex_exit(SD_MUTEX(un)); 6235 (void) untimeout(temp_id); 6236 mutex_enter(SD_MUTEX(un)); 6237 } 6238 6239 if (un->un_dcvb_timeid != NULL) { 6240 timeout_id_t temp_id = un->un_dcvb_timeid; 6241 un->un_dcvb_timeid = NULL; 6242 mutex_exit(SD_MUTEX(un)); 6243 (void) untimeout(temp_id); 6244 mutex_enter(SD_MUTEX(un)); 6245 } 6246 6247 mutex_enter(&un->un_pm_mutex); 6248 if (un->un_pm_timeid != NULL) { 6249 timeout_id_t temp_id = un->un_pm_timeid; 6250 un->un_pm_timeid = NULL; 6251 mutex_exit(&un->un_pm_mutex); 6252 mutex_exit(SD_MUTEX(un)); 6253 (void) untimeout(temp_id); 6254 mutex_enter(SD_MUTEX(un)); 6255 } else { 6256 mutex_exit(&un->un_pm_mutex); 6257 } 6258 6259 if (un->un_rmw_msg_timeid != NULL) { 6260 timeout_id_t temp_id = un->un_rmw_msg_timeid; 6261 un->un_rmw_msg_timeid = NULL; 6262 mutex_exit(SD_MUTEX(un)); 6263 (void) untimeout(temp_id); 6264 mutex_enter(SD_MUTEX(un)); 6265 } 6266 6267 if (un->un_retry_timeid != NULL) { 6268 timeout_id_t temp_id = un->un_retry_timeid; 6269 un->un_retry_timeid = NULL; 6270 mutex_exit(SD_MUTEX(un)); 6271 (void) untimeout(temp_id); 6272 mutex_enter(SD_MUTEX(un)); 6273 6274 if (un->un_retry_bp != NULL) { 6275 un->un_retry_bp->av_forw = un->un_waitq_headp; 6276 un->un_waitq_headp = un->un_retry_bp; 6277 if (un->un_waitq_tailp == NULL) { 6278 un->un_waitq_tailp = un->un_retry_bp; 6279 } 6280 un->un_retry_bp = NULL; 6281 un->un_retry_statp = NULL; 6282 } 6283 } 6284 6285 if (un->un_direct_priority_timeid != NULL) { 6286 timeout_id_t temp_id = un->un_direct_priority_timeid; 6287 un->un_direct_priority_timeid = NULL; 6288 mutex_exit(SD_MUTEX(un)); 6289 (void) untimeout(temp_id); 6290 mutex_enter(SD_MUTEX(un)); 6291 } 6292 6293 if (un->un_f_is_fibre == TRUE) { 6294 /* 6295 * Remove callbacks for insert and remove events 6296 */ 6297 if (un->un_insert_event != NULL) { 6298 mutex_exit(SD_MUTEX(un)); 6299 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6300 mutex_enter(SD_MUTEX(un)); 6301 un->un_insert_event = NULL; 6302 } 6303 6304 if (un->un_remove_event != NULL) { 6305 mutex_exit(SD_MUTEX(un)); 6306 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6307 mutex_enter(SD_MUTEX(un)); 6308 un->un_remove_event = NULL; 6309 } 6310 } 6311 6312 mutex_exit(SD_MUTEX(un)); 6313 6314 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6315 6316 return (DDI_SUCCESS); 6317 } 6318 6319 6320 /* 6321 * Function: sd_ddi_resume 6322 * 6323 * Description: Performs system power-up operations.. 6324 * 6325 * Return Code: DDI_SUCCESS 6326 * DDI_FAILURE 6327 * 6328 * Context: Kernel thread context 6329 */ 6330 6331 static int 6332 sd_ddi_resume(dev_info_t *devi) 6333 { 6334 struct sd_lun *un; 6335 6336 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6337 if (un == NULL) { 6338 return (DDI_FAILURE); 6339 } 6340 6341 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6342 6343 mutex_enter(SD_MUTEX(un)); 6344 Restore_state(un); 6345 6346 /* 6347 * Restore the state which was saved to give the 6348 * the right state in un_last_state 6349 */ 6350 un->un_last_state = un->un_save_state; 6351 /* 6352 * Note: throttle comes back at full. 6353 * Also note: this MUST be done before calling pm_raise_power 6354 * otherwise the system can get hung in biowait. The scenario where 6355 * this'll happen is under cpr suspend. Writing of the system 6356 * state goes through sddump, which writes 0 to un_throttle. If 6357 * writing the system state then fails, example if the partition is 6358 * too small, then cpr attempts a resume. If throttle isn't restored 6359 * from the saved value until after calling pm_raise_power then 6360 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6361 * in biowait. 6362 */ 6363 un->un_throttle = un->un_saved_throttle; 6364 6365 /* 6366 * The chance of failure is very rare as the only command done in power 6367 * entry point is START command when you transition from 0->1 or 6368 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6369 * which suspend was done. Ignore the return value as the resume should 6370 * not be failed. In the case of removable media the media need not be 6371 * inserted and hence there is a chance that raise power will fail with 6372 * media not present. 6373 */ 6374 if (un->un_f_attach_spinup) { 6375 mutex_exit(SD_MUTEX(un)); 6376 (void) pm_raise_power(SD_DEVINFO(un), 0, 6377 SD_PM_STATE_ACTIVE(un)); 6378 mutex_enter(SD_MUTEX(un)); 6379 } 6380 6381 /* 6382 * Don't broadcast to the suspend cv and therefore possibly 6383 * start I/O until after power has been restored. 6384 */ 6385 cv_broadcast(&un->un_suspend_cv); 6386 cv_broadcast(&un->un_state_cv); 6387 6388 /* restart thread */ 6389 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6390 scsi_watch_resume(un->un_swr_token); 6391 } 6392 6393 #if (defined(__fibre)) 6394 if (un->un_f_is_fibre == TRUE) { 6395 /* 6396 * Add callbacks for insert and remove events 6397 */ 6398 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6399 sd_init_event_callbacks(un); 6400 } 6401 } 6402 #endif 6403 6404 /* 6405 * Transport any pending commands to the target. 6406 * 6407 * If this is a low-activity device commands in queue will have to wait 6408 * until new commands come in, which may take awhile. Also, we 6409 * specifically don't check un_ncmds_in_transport because we know that 6410 * there really are no commands in progress after the unit was 6411 * suspended and we could have reached the throttle level, been 6412 * suspended, and have no new commands coming in for awhile. Highly 6413 * unlikely, but so is the low-activity disk scenario. 6414 */ 6415 ddi_xbuf_dispatch(un->un_xbuf_attr); 6416 6417 sd_start_cmds(un, NULL); 6418 mutex_exit(SD_MUTEX(un)); 6419 6420 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6421 6422 return (DDI_SUCCESS); 6423 } 6424 6425 6426 /* 6427 * Function: sd_pm_state_change 6428 * 6429 * Description: Change the driver power state. 6430 * Someone else is required to actually change the driver 6431 * power level. 6432 * 6433 * Arguments: un - driver soft state (unit) structure 6434 * level - the power level that is changed to 6435 * flag - to decide how to change the power state 6436 * 6437 * Return Code: DDI_SUCCESS 6438 * 6439 * Context: Kernel thread context 6440 */ 6441 static int 6442 sd_pm_state_change(struct sd_lun *un, int level, int flag) 6443 { 6444 ASSERT(un != NULL); 6445 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n"); 6446 6447 ASSERT(!mutex_owned(SD_MUTEX(un))); 6448 mutex_enter(SD_MUTEX(un)); 6449 6450 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) { 6451 un->un_power_level = level; 6452 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6453 mutex_enter(&un->un_pm_mutex); 6454 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6455 un->un_pm_count++; 6456 ASSERT(un->un_pm_count == 0); 6457 } 6458 mutex_exit(&un->un_pm_mutex); 6459 } else { 6460 /* 6461 * Exit if power management is not enabled for this device, 6462 * or if the device is being used by HA. 6463 */ 6464 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6465 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6466 mutex_exit(SD_MUTEX(un)); 6467 SD_TRACE(SD_LOG_POWER, un, 6468 "sd_pm_state_change: exiting\n"); 6469 return (DDI_FAILURE); 6470 } 6471 6472 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: " 6473 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver); 6474 6475 /* 6476 * See if the device is not busy, ie.: 6477 * - we have no commands in the driver for this device 6478 * - not waiting for resources 6479 */ 6480 if ((un->un_ncmds_in_driver == 0) && 6481 (un->un_state != SD_STATE_RWAIT)) { 6482 /* 6483 * The device is not busy, so it is OK to go to low 6484 * power state. Indicate low power, but rely on someone 6485 * else to actually change it. 6486 */ 6487 mutex_enter(&un->un_pm_mutex); 6488 un->un_pm_count = -1; 6489 mutex_exit(&un->un_pm_mutex); 6490 un->un_power_level = level; 6491 } 6492 } 6493 6494 mutex_exit(SD_MUTEX(un)); 6495 6496 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n"); 6497 6498 return (DDI_SUCCESS); 6499 } 6500 6501 6502 /* 6503 * Function: sd_pm_idletimeout_handler 6504 * 6505 * Description: A timer routine that's active only while a device is busy. 6506 * The purpose is to extend slightly the pm framework's busy 6507 * view of the device to prevent busy/idle thrashing for 6508 * back-to-back commands. Do this by comparing the current time 6509 * to the time at which the last command completed and when the 6510 * difference is greater than sd_pm_idletime, call 6511 * pm_idle_component. In addition to indicating idle to the pm 6512 * framework, update the chain type to again use the internal pm 6513 * layers of the driver. 6514 * 6515 * Arguments: arg - driver soft state (unit) structure 6516 * 6517 * Context: Executes in a timeout(9F) thread context 6518 */ 6519 6520 static void 6521 sd_pm_idletimeout_handler(void *arg) 6522 { 6523 struct sd_lun *un = arg; 6524 6525 time_t now; 6526 6527 mutex_enter(&sd_detach_mutex); 6528 if (un->un_detach_count != 0) { 6529 /* Abort if the instance is detaching */ 6530 mutex_exit(&sd_detach_mutex); 6531 return; 6532 } 6533 mutex_exit(&sd_detach_mutex); 6534 6535 now = ddi_get_time(); 6536 /* 6537 * Grab both mutexes, in the proper order, since we're accessing 6538 * both PM and softstate variables. 6539 */ 6540 mutex_enter(SD_MUTEX(un)); 6541 mutex_enter(&un->un_pm_mutex); 6542 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6543 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6544 /* 6545 * Update the chain types. 6546 * This takes affect on the next new command received. 6547 */ 6548 if (un->un_f_non_devbsize_supported) { 6549 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6550 } else { 6551 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6552 } 6553 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6554 6555 SD_TRACE(SD_LOG_IO_PM, un, 6556 "sd_pm_idletimeout_handler: idling device\n"); 6557 (void) pm_idle_component(SD_DEVINFO(un), 0); 6558 un->un_pm_idle_timeid = NULL; 6559 } else { 6560 un->un_pm_idle_timeid = 6561 timeout(sd_pm_idletimeout_handler, un, 6562 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6563 } 6564 mutex_exit(&un->un_pm_mutex); 6565 mutex_exit(SD_MUTEX(un)); 6566 } 6567 6568 6569 /* 6570 * Function: sd_pm_timeout_handler 6571 * 6572 * Description: Callback to tell framework we are idle. 6573 * 6574 * Context: timeout(9f) thread context. 6575 */ 6576 6577 static void 6578 sd_pm_timeout_handler(void *arg) 6579 { 6580 struct sd_lun *un = arg; 6581 6582 (void) pm_idle_component(SD_DEVINFO(un), 0); 6583 mutex_enter(&un->un_pm_mutex); 6584 un->un_pm_timeid = NULL; 6585 mutex_exit(&un->un_pm_mutex); 6586 } 6587 6588 6589 /* 6590 * Function: sdpower 6591 * 6592 * Description: PM entry point. 6593 * 6594 * Return Code: DDI_SUCCESS 6595 * DDI_FAILURE 6596 * 6597 * Context: Kernel thread context 6598 */ 6599 6600 static int 6601 sdpower(dev_info_t *devi, int component, int level) 6602 { 6603 struct sd_lun *un; 6604 int instance; 6605 int rval = DDI_SUCCESS; 6606 uint_t i, log_page_size, maxcycles, ncycles; 6607 uchar_t *log_page_data; 6608 int log_sense_page; 6609 int medium_present; 6610 time_t intvlp; 6611 struct pm_trans_data sd_pm_tran_data; 6612 uchar_t save_state; 6613 int sval; 6614 uchar_t state_before_pm; 6615 int got_semaphore_here; 6616 sd_ssc_t *ssc; 6617 int last_power_level; 6618 6619 instance = ddi_get_instance(devi); 6620 6621 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6622 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) { 6623 return (DDI_FAILURE); 6624 } 6625 6626 ssc = sd_ssc_init(un); 6627 6628 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6629 6630 /* 6631 * Must synchronize power down with close. 6632 * Attempt to decrement/acquire the open/close semaphore, 6633 * but do NOT wait on it. If it's not greater than zero, 6634 * ie. it can't be decremented without waiting, then 6635 * someone else, either open or close, already has it 6636 * and the try returns 0. Use that knowledge here to determine 6637 * if it's OK to change the device power level. 6638 * Also, only increment it on exit if it was decremented, ie. gotten, 6639 * here. 6640 */ 6641 got_semaphore_here = sema_tryp(&un->un_semoclose); 6642 6643 mutex_enter(SD_MUTEX(un)); 6644 6645 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 6646 un->un_ncmds_in_driver); 6647 6648 /* 6649 * If un_ncmds_in_driver is non-zero it indicates commands are 6650 * already being processed in the driver, or if the semaphore was 6651 * not gotten here it indicates an open or close is being processed. 6652 * At the same time somebody is requesting to go to a lower power 6653 * that can't perform I/O, which can't happen, therefore we need to 6654 * return failure. 6655 */ 6656 if ((!SD_PM_IS_IO_CAPABLE(un, level)) && 6657 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 6658 mutex_exit(SD_MUTEX(un)); 6659 6660 if (got_semaphore_here != 0) { 6661 sema_v(&un->un_semoclose); 6662 } 6663 SD_TRACE(SD_LOG_IO_PM, un, 6664 "sdpower: exit, device has queued cmds.\n"); 6665 6666 goto sdpower_failed; 6667 } 6668 6669 /* 6670 * if it is OFFLINE that means the disk is completely dead 6671 * in our case we have to put the disk in on or off by sending commands 6672 * Of course that will fail anyway so return back here. 6673 * 6674 * Power changes to a device that's OFFLINE or SUSPENDED 6675 * are not allowed. 6676 */ 6677 if ((un->un_state == SD_STATE_OFFLINE) || 6678 (un->un_state == SD_STATE_SUSPENDED)) { 6679 mutex_exit(SD_MUTEX(un)); 6680 6681 if (got_semaphore_here != 0) { 6682 sema_v(&un->un_semoclose); 6683 } 6684 SD_TRACE(SD_LOG_IO_PM, un, 6685 "sdpower: exit, device is off-line.\n"); 6686 6687 goto sdpower_failed; 6688 } 6689 6690 /* 6691 * Change the device's state to indicate it's power level 6692 * is being changed. Do this to prevent a power off in the 6693 * middle of commands, which is especially bad on devices 6694 * that are really powered off instead of just spun down. 6695 */ 6696 state_before_pm = un->un_state; 6697 un->un_state = SD_STATE_PM_CHANGING; 6698 6699 mutex_exit(SD_MUTEX(un)); 6700 6701 /* 6702 * If log sense command is not supported, bypass the 6703 * following checking, otherwise, check the log sense 6704 * information for this device. 6705 */ 6706 if (SD_PM_STOP_MOTOR_NEEDED(un, level) && 6707 un->un_f_log_sense_supported) { 6708 /* 6709 * Get the log sense information to understand whether the 6710 * the powercycle counts have gone beyond the threshhold. 6711 */ 6712 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6713 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6714 6715 mutex_enter(SD_MUTEX(un)); 6716 log_sense_page = un->un_start_stop_cycle_page; 6717 mutex_exit(SD_MUTEX(un)); 6718 6719 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6720 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 6721 6722 if (rval != 0) { 6723 if (rval == EIO) 6724 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6725 else 6726 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6727 } 6728 6729 #ifdef SDDEBUG 6730 if (sd_force_pm_supported) { 6731 /* Force a successful result */ 6732 rval = 0; 6733 } 6734 #endif 6735 if (rval != 0) { 6736 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 6737 "Log Sense Failed\n"); 6738 6739 kmem_free(log_page_data, log_page_size); 6740 /* Cannot support power management on those drives */ 6741 6742 if (got_semaphore_here != 0) { 6743 sema_v(&un->un_semoclose); 6744 } 6745 /* 6746 * On exit put the state back to it's original value 6747 * and broadcast to anyone waiting for the power 6748 * change completion. 6749 */ 6750 mutex_enter(SD_MUTEX(un)); 6751 un->un_state = state_before_pm; 6752 cv_broadcast(&un->un_suspend_cv); 6753 mutex_exit(SD_MUTEX(un)); 6754 SD_TRACE(SD_LOG_IO_PM, un, 6755 "sdpower: exit, Log Sense Failed.\n"); 6756 6757 goto sdpower_failed; 6758 } 6759 6760 /* 6761 * From the page data - Convert the essential information to 6762 * pm_trans_data 6763 */ 6764 maxcycles = 6765 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 6766 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 6767 6768 ncycles = 6769 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 6770 (log_page_data[0x26] << 8) | log_page_data[0x27]; 6771 6772 if (un->un_f_pm_log_sense_smart) { 6773 sd_pm_tran_data.un.smart_count.allowed = maxcycles; 6774 sd_pm_tran_data.un.smart_count.consumed = ncycles; 6775 sd_pm_tran_data.un.smart_count.flag = 0; 6776 sd_pm_tran_data.format = DC_SMART_FORMAT; 6777 } else { 6778 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 6779 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 6780 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 6781 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 6782 log_page_data[8+i]; 6783 } 6784 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6785 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6786 } 6787 6788 kmem_free(log_page_data, log_page_size); 6789 6790 /* 6791 * Call pm_trans_check routine to get the Ok from 6792 * the global policy 6793 */ 6794 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6795 #ifdef SDDEBUG 6796 if (sd_force_pm_supported) { 6797 /* Force a successful result */ 6798 rval = 1; 6799 } 6800 #endif 6801 switch (rval) { 6802 case 0: 6803 /* 6804 * Not Ok to Power cycle or error in parameters passed 6805 * Would have given the advised time to consider power 6806 * cycle. Based on the new intvlp parameter we are 6807 * supposed to pretend we are busy so that pm framework 6808 * will never call our power entry point. Because of 6809 * that install a timeout handler and wait for the 6810 * recommended time to elapse so that power management 6811 * can be effective again. 6812 * 6813 * To effect this behavior, call pm_busy_component to 6814 * indicate to the framework this device is busy. 6815 * By not adjusting un_pm_count the rest of PM in 6816 * the driver will function normally, and independent 6817 * of this but because the framework is told the device 6818 * is busy it won't attempt powering down until it gets 6819 * a matching idle. The timeout handler sends this. 6820 * Note: sd_pm_entry can't be called here to do this 6821 * because sdpower may have been called as a result 6822 * of a call to pm_raise_power from within sd_pm_entry. 6823 * 6824 * If a timeout handler is already active then 6825 * don't install another. 6826 */ 6827 mutex_enter(&un->un_pm_mutex); 6828 if (un->un_pm_timeid == NULL) { 6829 un->un_pm_timeid = 6830 timeout(sd_pm_timeout_handler, 6831 un, intvlp * drv_usectohz(1000000)); 6832 mutex_exit(&un->un_pm_mutex); 6833 (void) pm_busy_component(SD_DEVINFO(un), 0); 6834 } else { 6835 mutex_exit(&un->un_pm_mutex); 6836 } 6837 if (got_semaphore_here != 0) { 6838 sema_v(&un->un_semoclose); 6839 } 6840 /* 6841 * On exit put the state back to it's original value 6842 * and broadcast to anyone waiting for the power 6843 * change completion. 6844 */ 6845 mutex_enter(SD_MUTEX(un)); 6846 un->un_state = state_before_pm; 6847 cv_broadcast(&un->un_suspend_cv); 6848 mutex_exit(SD_MUTEX(un)); 6849 6850 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6851 "trans check Failed, not ok to power cycle.\n"); 6852 6853 goto sdpower_failed; 6854 case -1: 6855 if (got_semaphore_here != 0) { 6856 sema_v(&un->un_semoclose); 6857 } 6858 /* 6859 * On exit put the state back to it's original value 6860 * and broadcast to anyone waiting for the power 6861 * change completion. 6862 */ 6863 mutex_enter(SD_MUTEX(un)); 6864 un->un_state = state_before_pm; 6865 cv_broadcast(&un->un_suspend_cv); 6866 mutex_exit(SD_MUTEX(un)); 6867 SD_TRACE(SD_LOG_IO_PM, un, 6868 "sdpower: exit, trans check command Failed.\n"); 6869 6870 goto sdpower_failed; 6871 } 6872 } 6873 6874 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6875 /* 6876 * Save the last state... if the STOP FAILS we need it 6877 * for restoring 6878 */ 6879 mutex_enter(SD_MUTEX(un)); 6880 save_state = un->un_last_state; 6881 last_power_level = un->un_power_level; 6882 /* 6883 * There must not be any cmds. getting processed 6884 * in the driver when we get here. Power to the 6885 * device is potentially going off. 6886 */ 6887 ASSERT(un->un_ncmds_in_driver == 0); 6888 mutex_exit(SD_MUTEX(un)); 6889 6890 /* 6891 * For now PM suspend the device completely before spindle is 6892 * turned off 6893 */ 6894 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE)) 6895 == DDI_FAILURE) { 6896 if (got_semaphore_here != 0) { 6897 sema_v(&un->un_semoclose); 6898 } 6899 /* 6900 * On exit put the state back to it's original value 6901 * and broadcast to anyone waiting for the power 6902 * change completion. 6903 */ 6904 mutex_enter(SD_MUTEX(un)); 6905 un->un_state = state_before_pm; 6906 un->un_power_level = last_power_level; 6907 cv_broadcast(&un->un_suspend_cv); 6908 mutex_exit(SD_MUTEX(un)); 6909 SD_TRACE(SD_LOG_IO_PM, un, 6910 "sdpower: exit, PM suspend Failed.\n"); 6911 6912 goto sdpower_failed; 6913 } 6914 } 6915 6916 /* 6917 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6918 * close, or strategy. Dump no long uses this routine, it uses it's 6919 * own code so it can be done in polled mode. 6920 */ 6921 6922 medium_present = TRUE; 6923 6924 /* 6925 * When powering up, issue a TUR in case the device is at unit 6926 * attention. Don't do retries. Bypass the PM layer, otherwise 6927 * a deadlock on un_pm_busy_cv will occur. 6928 */ 6929 if (SD_PM_IS_IO_CAPABLE(un, level)) { 6930 sval = sd_send_scsi_TEST_UNIT_READY(ssc, 6931 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6932 if (sval != 0) 6933 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6934 } 6935 6936 if (un->un_f_power_condition_supported) { 6937 char *pm_condition_name[] = {"STOPPED", "STANDBY", 6938 "IDLE", "ACTIVE"}; 6939 SD_TRACE(SD_LOG_IO_PM, un, 6940 "sdpower: sending \'%s\' power condition", 6941 pm_condition_name[level]); 6942 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 6943 sd_pl2pc[level], SD_PATH_DIRECT); 6944 } else { 6945 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6946 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6947 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 6948 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : 6949 SD_TARGET_STOP), SD_PATH_DIRECT); 6950 } 6951 if (sval != 0) { 6952 if (sval == EIO) 6953 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6954 else 6955 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6956 } 6957 6958 /* Command failed, check for media present. */ 6959 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6960 medium_present = FALSE; 6961 } 6962 6963 /* 6964 * The conditions of interest here are: 6965 * if a spindle off with media present fails, 6966 * then restore the state and return an error. 6967 * else if a spindle on fails, 6968 * then return an error (there's no state to restore). 6969 * In all other cases we setup for the new state 6970 * and return success. 6971 */ 6972 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6973 if ((medium_present == TRUE) && (sval != 0)) { 6974 /* The stop command from above failed */ 6975 rval = DDI_FAILURE; 6976 /* 6977 * The stop command failed, and we have media 6978 * present. Put the level back by calling the 6979 * sd_pm_resume() and set the state back to 6980 * it's previous value. 6981 */ 6982 (void) sd_pm_state_change(un, last_power_level, 6983 SD_PM_STATE_ROLLBACK); 6984 mutex_enter(SD_MUTEX(un)); 6985 un->un_last_state = save_state; 6986 mutex_exit(SD_MUTEX(un)); 6987 } else if (un->un_f_monitor_media_state) { 6988 /* 6989 * The stop command from above succeeded. 6990 * Terminate watch thread in case of removable media 6991 * devices going into low power state. This is as per 6992 * the requirements of pm framework, otherwise commands 6993 * will be generated for the device (through watch 6994 * thread), even when the device is in low power state. 6995 */ 6996 mutex_enter(SD_MUTEX(un)); 6997 un->un_f_watcht_stopped = FALSE; 6998 if (un->un_swr_token != NULL) { 6999 opaque_t temp_token = un->un_swr_token; 7000 un->un_f_watcht_stopped = TRUE; 7001 un->un_swr_token = NULL; 7002 mutex_exit(SD_MUTEX(un)); 7003 (void) scsi_watch_request_terminate(temp_token, 7004 SCSI_WATCH_TERMINATE_ALL_WAIT); 7005 } else { 7006 mutex_exit(SD_MUTEX(un)); 7007 } 7008 } 7009 } else { 7010 /* 7011 * The level requested is I/O capable. 7012 * Legacy behavior: return success on a failed spinup 7013 * if there is no media in the drive. 7014 * Do this by looking at medium_present here. 7015 */ 7016 if ((sval != 0) && medium_present) { 7017 /* The start command from above failed */ 7018 rval = DDI_FAILURE; 7019 } else { 7020 /* 7021 * The start command from above succeeded 7022 * PM resume the devices now that we have 7023 * started the disks 7024 */ 7025 (void) sd_pm_state_change(un, level, 7026 SD_PM_STATE_CHANGE); 7027 7028 /* 7029 * Resume the watch thread since it was suspended 7030 * when the device went into low power mode. 7031 */ 7032 if (un->un_f_monitor_media_state) { 7033 mutex_enter(SD_MUTEX(un)); 7034 if (un->un_f_watcht_stopped == TRUE) { 7035 opaque_t temp_token; 7036 7037 un->un_f_watcht_stopped = FALSE; 7038 mutex_exit(SD_MUTEX(un)); 7039 temp_token = 7040 sd_watch_request_submit(un); 7041 mutex_enter(SD_MUTEX(un)); 7042 un->un_swr_token = temp_token; 7043 } 7044 mutex_exit(SD_MUTEX(un)); 7045 } 7046 } 7047 } 7048 7049 if (got_semaphore_here != 0) { 7050 sema_v(&un->un_semoclose); 7051 } 7052 /* 7053 * On exit put the state back to it's original value 7054 * and broadcast to anyone waiting for the power 7055 * change completion. 7056 */ 7057 mutex_enter(SD_MUTEX(un)); 7058 un->un_state = state_before_pm; 7059 cv_broadcast(&un->un_suspend_cv); 7060 mutex_exit(SD_MUTEX(un)); 7061 7062 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7063 7064 sd_ssc_fini(ssc); 7065 return (rval); 7066 7067 sdpower_failed: 7068 7069 sd_ssc_fini(ssc); 7070 return (DDI_FAILURE); 7071 } 7072 7073 7074 7075 /* 7076 * Function: sdattach 7077 * 7078 * Description: Driver's attach(9e) entry point function. 7079 * 7080 * Arguments: devi - opaque device info handle 7081 * cmd - attach type 7082 * 7083 * Return Code: DDI_SUCCESS 7084 * DDI_FAILURE 7085 * 7086 * Context: Kernel thread context 7087 */ 7088 7089 static int 7090 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7091 { 7092 switch (cmd) { 7093 case DDI_ATTACH: 7094 return (sd_unit_attach(devi)); 7095 case DDI_RESUME: 7096 return (sd_ddi_resume(devi)); 7097 default: 7098 break; 7099 } 7100 return (DDI_FAILURE); 7101 } 7102 7103 7104 /* 7105 * Function: sddetach 7106 * 7107 * Description: Driver's detach(9E) entry point function. 7108 * 7109 * Arguments: devi - opaque device info handle 7110 * cmd - detach type 7111 * 7112 * Return Code: DDI_SUCCESS 7113 * DDI_FAILURE 7114 * 7115 * Context: Kernel thread context 7116 */ 7117 7118 static int 7119 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7120 { 7121 switch (cmd) { 7122 case DDI_DETACH: 7123 return (sd_unit_detach(devi)); 7124 case DDI_SUSPEND: 7125 return (sd_ddi_suspend(devi)); 7126 default: 7127 break; 7128 } 7129 return (DDI_FAILURE); 7130 } 7131 7132 7133 /* 7134 * Function: sd_sync_with_callback 7135 * 7136 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7137 * state while the callback routine is active. 7138 * 7139 * Arguments: un: softstate structure for the instance 7140 * 7141 * Context: Kernel thread context 7142 */ 7143 7144 static void 7145 sd_sync_with_callback(struct sd_lun *un) 7146 { 7147 ASSERT(un != NULL); 7148 7149 mutex_enter(SD_MUTEX(un)); 7150 7151 ASSERT(un->un_in_callback >= 0); 7152 7153 while (un->un_in_callback > 0) { 7154 mutex_exit(SD_MUTEX(un)); 7155 delay(2); 7156 mutex_enter(SD_MUTEX(un)); 7157 } 7158 7159 mutex_exit(SD_MUTEX(un)); 7160 } 7161 7162 /* 7163 * Function: sd_unit_attach 7164 * 7165 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7166 * the soft state structure for the device and performs 7167 * all necessary structure and device initializations. 7168 * 7169 * Arguments: devi: the system's dev_info_t for the device. 7170 * 7171 * Return Code: DDI_SUCCESS if attach is successful. 7172 * DDI_FAILURE if any part of the attach fails. 7173 * 7174 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7175 * Kernel thread context only. Can sleep. 7176 */ 7177 7178 static int 7179 sd_unit_attach(dev_info_t *devi) 7180 { 7181 struct scsi_device *devp; 7182 struct sd_lun *un; 7183 char *variantp; 7184 char name_str[48]; 7185 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7186 int instance; 7187 int rval; 7188 int wc_enabled; 7189 int tgt; 7190 uint64_t capacity; 7191 uint_t lbasize = 0; 7192 dev_info_t *pdip = ddi_get_parent(devi); 7193 int offbyone = 0; 7194 int geom_label_valid = 0; 7195 sd_ssc_t *ssc; 7196 int status; 7197 struct sd_fm_internal *sfip = NULL; 7198 int max_xfer_size; 7199 7200 /* 7201 * Retrieve the target driver's private data area. This was set 7202 * up by the HBA. 7203 */ 7204 devp = ddi_get_driver_private(devi); 7205 7206 /* 7207 * Retrieve the target ID of the device. 7208 */ 7209 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7210 SCSI_ADDR_PROP_TARGET, -1); 7211 7212 /* 7213 * Since we have no idea what state things were left in by the last 7214 * user of the device, set up some 'default' settings, ie. turn 'em 7215 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7216 * Do this before the scsi_probe, which sends an inquiry. 7217 * This is a fix for bug (4430280). 7218 * Of special importance is wide-xfer. The drive could have been left 7219 * in wide transfer mode by the last driver to communicate with it, 7220 * this includes us. If that's the case, and if the following is not 7221 * setup properly or we don't re-negotiate with the drive prior to 7222 * transferring data to/from the drive, it causes bus parity errors, 7223 * data overruns, and unexpected interrupts. This first occurred when 7224 * the fix for bug (4378686) was made. 7225 */ 7226 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7227 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7228 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7229 7230 /* 7231 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 7232 * on a target. Setting it per lun instance actually sets the 7233 * capability of this target, which affects those luns already 7234 * attached on the same target. So during attach, we can only disable 7235 * this capability only when no other lun has been attached on this 7236 * target. By doing this, we assume a target has the same tagged-qing 7237 * capability for every lun. The condition can be removed when HBA 7238 * is changed to support per lun based tagged-qing capability. 7239 */ 7240 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7241 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7242 } 7243 7244 /* 7245 * Use scsi_probe() to issue an INQUIRY command to the device. 7246 * This call will allocate and fill in the scsi_inquiry structure 7247 * and point the sd_inq member of the scsi_device structure to it. 7248 * If the attach succeeds, then this memory will not be de-allocated 7249 * (via scsi_unprobe()) until the instance is detached. 7250 */ 7251 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7252 goto probe_failed; 7253 } 7254 7255 /* 7256 * Check the device type as specified in the inquiry data and 7257 * claim it if it is of a type that we support. 7258 */ 7259 switch (devp->sd_inq->inq_dtype) { 7260 case DTYPE_DIRECT: 7261 break; 7262 case DTYPE_RODIRECT: 7263 break; 7264 case DTYPE_OPTICAL: 7265 break; 7266 case DTYPE_NOTPRESENT: 7267 default: 7268 /* Unsupported device type; fail the attach. */ 7269 goto probe_failed; 7270 } 7271 7272 /* 7273 * Allocate the soft state structure for this unit. 7274 * 7275 * We rely upon this memory being set to all zeroes by 7276 * ddi_soft_state_zalloc(). We assume that any member of the 7277 * soft state structure that is not explicitly initialized by 7278 * this routine will have a value of zero. 7279 */ 7280 instance = ddi_get_instance(devp->sd_dev); 7281 #ifndef XPV_HVM_DRIVER 7282 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7283 goto probe_failed; 7284 } 7285 #endif /* !XPV_HVM_DRIVER */ 7286 7287 /* 7288 * Retrieve a pointer to the newly-allocated soft state. 7289 * 7290 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7291 * was successful, unless something has gone horribly wrong and the 7292 * ddi's soft state internals are corrupt (in which case it is 7293 * probably better to halt here than just fail the attach....) 7294 */ 7295 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7296 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7297 instance); 7298 /*NOTREACHED*/ 7299 } 7300 7301 /* 7302 * Link the back ptr of the driver soft state to the scsi_device 7303 * struct for this lun. 7304 * Save a pointer to the softstate in the driver-private area of 7305 * the scsi_device struct. 7306 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7307 * we first set un->un_sd below. 7308 */ 7309 un->un_sd = devp; 7310 devp->sd_private = (opaque_t)un; 7311 7312 /* 7313 * The following must be after devp is stored in the soft state struct. 7314 */ 7315 #ifdef SDDEBUG 7316 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7317 "%s_unit_attach: un:0x%p instance:%d\n", 7318 ddi_driver_name(devi), un, instance); 7319 #endif 7320 7321 /* 7322 * Set up the device type and node type (for the minor nodes). 7323 * By default we assume that the device can at least support the 7324 * Common Command Set. Call it a CD-ROM if it reports itself 7325 * as a RODIRECT device. 7326 */ 7327 switch (devp->sd_inq->inq_dtype) { 7328 case DTYPE_RODIRECT: 7329 un->un_node_type = DDI_NT_CD_CHAN; 7330 un->un_ctype = CTYPE_CDROM; 7331 break; 7332 case DTYPE_OPTICAL: 7333 un->un_node_type = DDI_NT_BLOCK_CHAN; 7334 un->un_ctype = CTYPE_ROD; 7335 break; 7336 default: 7337 un->un_node_type = DDI_NT_BLOCK_CHAN; 7338 un->un_ctype = CTYPE_CCS; 7339 break; 7340 } 7341 7342 /* 7343 * Try to read the interconnect type from the HBA. 7344 * 7345 * Note: This driver is currently compiled as two binaries, a parallel 7346 * scsi version (sd) and a fibre channel version (ssd). All functional 7347 * differences are determined at compile time. In the future a single 7348 * binary will be provided and the interconnect type will be used to 7349 * differentiate between fibre and parallel scsi behaviors. At that time 7350 * it will be necessary for all fibre channel HBAs to support this 7351 * property. 7352 * 7353 * set un_f_is_fiber to TRUE ( default fiber ) 7354 */ 7355 un->un_f_is_fibre = TRUE; 7356 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7357 case INTERCONNECT_SSA: 7358 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7359 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7360 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7361 break; 7362 case INTERCONNECT_PARALLEL: 7363 un->un_f_is_fibre = FALSE; 7364 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7365 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7366 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7367 break; 7368 case INTERCONNECT_SAS: 7369 un->un_f_is_fibre = FALSE; 7370 un->un_interconnect_type = SD_INTERCONNECT_SAS; 7371 un->un_node_type = DDI_NT_BLOCK_SAS; 7372 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7373 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un); 7374 break; 7375 case INTERCONNECT_SATA: 7376 un->un_f_is_fibre = FALSE; 7377 un->un_interconnect_type = SD_INTERCONNECT_SATA; 7378 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7379 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 7380 break; 7381 case INTERCONNECT_FIBRE: 7382 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7383 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7384 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7385 break; 7386 case INTERCONNECT_FABRIC: 7387 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7388 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7389 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7390 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7391 break; 7392 default: 7393 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7394 /* 7395 * The HBA does not support the "interconnect-type" property 7396 * (or did not provide a recognized type). 7397 * 7398 * Note: This will be obsoleted when a single fibre channel 7399 * and parallel scsi driver is delivered. In the meantime the 7400 * interconnect type will be set to the platform default.If that 7401 * type is not parallel SCSI, it means that we should be 7402 * assuming "ssd" semantics. However, here this also means that 7403 * the FC HBA is not supporting the "interconnect-type" property 7404 * like we expect it to, so log this occurrence. 7405 */ 7406 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7407 if (!SD_IS_PARALLEL_SCSI(un)) { 7408 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7409 "sd_unit_attach: un:0x%p Assuming " 7410 "INTERCONNECT_FIBRE\n", un); 7411 } else { 7412 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7413 "sd_unit_attach: un:0x%p Assuming " 7414 "INTERCONNECT_PARALLEL\n", un); 7415 un->un_f_is_fibre = FALSE; 7416 } 7417 #else 7418 /* 7419 * Note: This source will be implemented when a single fibre 7420 * channel and parallel scsi driver is delivered. The default 7421 * will be to assume that if a device does not support the 7422 * "interconnect-type" property it is a parallel SCSI HBA and 7423 * we will set the interconnect type for parallel scsi. 7424 */ 7425 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7426 un->un_f_is_fibre = FALSE; 7427 #endif 7428 break; 7429 } 7430 7431 if (un->un_f_is_fibre == TRUE) { 7432 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7433 SCSI_VERSION_3) { 7434 switch (un->un_interconnect_type) { 7435 case SD_INTERCONNECT_FIBRE: 7436 case SD_INTERCONNECT_SSA: 7437 un->un_node_type = DDI_NT_BLOCK_WWN; 7438 break; 7439 default: 7440 break; 7441 } 7442 } 7443 } 7444 7445 /* 7446 * Initialize the Request Sense command for the target 7447 */ 7448 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7449 goto alloc_rqs_failed; 7450 } 7451 7452 /* 7453 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7454 * with separate binary for sd and ssd. 7455 * 7456 * x86 has 1 binary, un_retry_count is set base on connection type. 7457 * The hardcoded values will go away when Sparc uses 1 binary 7458 * for sd and ssd. This hardcoded values need to match 7459 * SD_RETRY_COUNT in sddef.h 7460 * The value used is base on interconnect type. 7461 * fibre = 3, parallel = 5 7462 */ 7463 #if defined(__i386) || defined(__amd64) 7464 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7465 #else 7466 un->un_retry_count = SD_RETRY_COUNT; 7467 #endif 7468 7469 /* 7470 * Set the per disk retry count to the default number of retries 7471 * for disks and CDROMs. This value can be overridden by the 7472 * disk property list or an entry in sd.conf. 7473 */ 7474 un->un_notready_retry_count = 7475 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7476 : DISK_NOT_READY_RETRY_COUNT(un); 7477 7478 /* 7479 * Set the busy retry count to the default value of un_retry_count. 7480 * This can be overridden by entries in sd.conf or the device 7481 * config table. 7482 */ 7483 un->un_busy_retry_count = un->un_retry_count; 7484 7485 /* 7486 * Init the reset threshold for retries. This number determines 7487 * how many retries must be performed before a reset can be issued 7488 * (for certain error conditions). This can be overridden by entries 7489 * in sd.conf or the device config table. 7490 */ 7491 un->un_reset_retry_count = (un->un_retry_count / 2); 7492 7493 /* 7494 * Set the victim_retry_count to the default un_retry_count 7495 */ 7496 un->un_victim_retry_count = (2 * un->un_retry_count); 7497 7498 /* 7499 * Set the reservation release timeout to the default value of 7500 * 5 seconds. This can be overridden by entries in ssd.conf or the 7501 * device config table. 7502 */ 7503 un->un_reserve_release_time = 5; 7504 7505 /* 7506 * Set up the default maximum transfer size. Note that this may 7507 * get updated later in the attach, when setting up default wide 7508 * operations for disks. 7509 */ 7510 #if defined(__i386) || defined(__amd64) 7511 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7512 un->un_partial_dma_supported = 1; 7513 #else 7514 un->un_max_xfer_size = (uint_t)maxphys; 7515 #endif 7516 7517 /* 7518 * Get "allow bus device reset" property (defaults to "enabled" if 7519 * the property was not defined). This is to disable bus resets for 7520 * certain kinds of error recovery. Note: In the future when a run-time 7521 * fibre check is available the soft state flag should default to 7522 * enabled. 7523 */ 7524 if (un->un_f_is_fibre == TRUE) { 7525 un->un_f_allow_bus_device_reset = TRUE; 7526 } else { 7527 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7528 "allow-bus-device-reset", 1) != 0) { 7529 un->un_f_allow_bus_device_reset = TRUE; 7530 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7531 "sd_unit_attach: un:0x%p Bus device reset " 7532 "enabled\n", un); 7533 } else { 7534 un->un_f_allow_bus_device_reset = FALSE; 7535 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7536 "sd_unit_attach: un:0x%p Bus device reset " 7537 "disabled\n", un); 7538 } 7539 } 7540 7541 /* 7542 * Check if this is an ATAPI device. ATAPI devices use Group 1 7543 * Read/Write commands and Group 2 Mode Sense/Select commands. 7544 * 7545 * Note: The "obsolete" way of doing this is to check for the "atapi" 7546 * property. The new "variant" property with a value of "atapi" has been 7547 * introduced so that future 'variants' of standard SCSI behavior (like 7548 * atapi) could be specified by the underlying HBA drivers by supplying 7549 * a new value for the "variant" property, instead of having to define a 7550 * new property. 7551 */ 7552 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7553 un->un_f_cfg_is_atapi = TRUE; 7554 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7555 "sd_unit_attach: un:0x%p Atapi device\n", un); 7556 } 7557 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7558 &variantp) == DDI_PROP_SUCCESS) { 7559 if (strcmp(variantp, "atapi") == 0) { 7560 un->un_f_cfg_is_atapi = TRUE; 7561 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7562 "sd_unit_attach: un:0x%p Atapi device\n", un); 7563 } 7564 ddi_prop_free(variantp); 7565 } 7566 7567 un->un_cmd_timeout = SD_IO_TIME; 7568 7569 un->un_busy_timeout = SD_BSY_TIMEOUT; 7570 7571 /* Info on current states, statuses, etc. (Updated frequently) */ 7572 un->un_state = SD_STATE_NORMAL; 7573 un->un_last_state = SD_STATE_NORMAL; 7574 7575 /* Control & status info for command throttling */ 7576 un->un_throttle = sd_max_throttle; 7577 un->un_saved_throttle = sd_max_throttle; 7578 un->un_min_throttle = sd_min_throttle; 7579 7580 if (un->un_f_is_fibre == TRUE) { 7581 un->un_f_use_adaptive_throttle = TRUE; 7582 } else { 7583 un->un_f_use_adaptive_throttle = FALSE; 7584 } 7585 7586 /* Removable media support. */ 7587 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7588 un->un_mediastate = DKIO_NONE; 7589 un->un_specified_mediastate = DKIO_NONE; 7590 7591 /* CVs for suspend/resume (PM or DR) */ 7592 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7593 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7594 7595 /* Power management support. */ 7596 un->un_power_level = SD_SPINDLE_UNINIT; 7597 7598 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 7599 un->un_f_wcc_inprog = 0; 7600 7601 /* 7602 * The open/close semaphore is used to serialize threads executing 7603 * in the driver's open & close entry point routines for a given 7604 * instance. 7605 */ 7606 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7607 7608 /* 7609 * The conf file entry and softstate variable is a forceful override, 7610 * meaning a non-zero value must be entered to change the default. 7611 */ 7612 un->un_f_disksort_disabled = FALSE; 7613 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT; 7614 un->un_f_enable_rmw = FALSE; 7615 7616 /* 7617 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but 7618 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property. 7619 */ 7620 un->un_f_mmc_gesn_polling = TRUE; 7621 7622 /* 7623 * Retrieve the properties from the static driver table or the driver 7624 * configuration file (.conf) for this unit and update the soft state 7625 * for the device as needed for the indicated properties. 7626 * Note: the property configuration needs to occur here as some of the 7627 * following routines may have dependencies on soft state flags set 7628 * as part of the driver property configuration. 7629 */ 7630 sd_read_unit_properties(un); 7631 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7632 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7633 7634 /* 7635 * Only if a device has "hotpluggable" property, it is 7636 * treated as hotpluggable device. Otherwise, it is 7637 * regarded as non-hotpluggable one. 7638 */ 7639 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 7640 -1) != -1) { 7641 un->un_f_is_hotpluggable = TRUE; 7642 } 7643 7644 /* 7645 * set unit's attributes(flags) according to "hotpluggable" and 7646 * RMB bit in INQUIRY data. 7647 */ 7648 sd_set_unit_attributes(un, devi); 7649 7650 /* 7651 * By default, we mark the capacity, lbasize, and geometry 7652 * as invalid. Only if we successfully read a valid capacity 7653 * will we update the un_blockcount and un_tgt_blocksize with the 7654 * valid values (the geometry will be validated later). 7655 */ 7656 un->un_f_blockcount_is_valid = FALSE; 7657 un->un_f_tgt_blocksize_is_valid = FALSE; 7658 7659 /* 7660 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7661 * otherwise. 7662 */ 7663 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7664 un->un_blockcount = 0; 7665 7666 /* 7667 * physical sector size default to DEV_BSIZE currently. 7668 */ 7669 un->un_phy_blocksize = DEV_BSIZE; 7670 7671 /* 7672 * Set up the per-instance info needed to determine the correct 7673 * CDBs and other info for issuing commands to the target. 7674 */ 7675 sd_init_cdb_limits(un); 7676 7677 /* 7678 * Set up the IO chains to use, based upon the target type. 7679 */ 7680 if (un->un_f_non_devbsize_supported) { 7681 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7682 } else { 7683 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7684 } 7685 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7686 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7687 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7688 7689 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7690 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7691 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7692 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7693 7694 7695 if (ISCD(un)) { 7696 un->un_additional_codes = sd_additional_codes; 7697 } else { 7698 un->un_additional_codes = NULL; 7699 } 7700 7701 /* 7702 * Create the kstats here so they can be available for attach-time 7703 * routines that send commands to the unit (either polled or via 7704 * sd_send_scsi_cmd). 7705 * 7706 * Note: This is a critical sequence that needs to be maintained: 7707 * 1) Instantiate the kstats here, before any routines using the 7708 * iopath (i.e. sd_send_scsi_cmd). 7709 * 2) Instantiate and initialize the partition stats 7710 * (sd_set_pstats). 7711 * 3) Initialize the error stats (sd_set_errstats), following 7712 * sd_validate_geometry(),sd_register_devid(), 7713 * and sd_cache_control(). 7714 */ 7715 7716 un->un_stats = kstat_create(sd_label, instance, 7717 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7718 if (un->un_stats != NULL) { 7719 un->un_stats->ks_lock = SD_MUTEX(un); 7720 kstat_install(un->un_stats); 7721 } 7722 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7723 "sd_unit_attach: un:0x%p un_stats created\n", un); 7724 7725 sd_create_errstats(un, instance); 7726 if (un->un_errstats == NULL) { 7727 goto create_errstats_failed; 7728 } 7729 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7730 "sd_unit_attach: un:0x%p errstats created\n", un); 7731 7732 /* 7733 * The following if/else code was relocated here from below as part 7734 * of the fix for bug (4430280). However with the default setup added 7735 * on entry to this routine, it's no longer absolutely necessary for 7736 * this to be before the call to sd_spin_up_unit. 7737 */ 7738 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 7739 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 7740 (devp->sd_inq->inq_ansi == 5)) && 7741 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 7742 7743 /* 7744 * If tagged queueing is supported by the target 7745 * and by the host adapter then we will enable it 7746 */ 7747 un->un_tagflags = 0; 7748 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 7749 (un->un_f_arq_enabled == TRUE)) { 7750 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7751 1, 1) == 1) { 7752 un->un_tagflags = FLAG_STAG; 7753 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7754 "sd_unit_attach: un:0x%p tag queueing " 7755 "enabled\n", un); 7756 } else if (scsi_ifgetcap(SD_ADDRESS(un), 7757 "untagged-qing", 0) == 1) { 7758 un->un_f_opt_queueing = TRUE; 7759 un->un_saved_throttle = un->un_throttle = 7760 min(un->un_throttle, 3); 7761 } else { 7762 un->un_f_opt_queueing = FALSE; 7763 un->un_saved_throttle = un->un_throttle = 1; 7764 } 7765 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 7766 == 1) && (un->un_f_arq_enabled == TRUE)) { 7767 /* The Host Adapter supports internal queueing. */ 7768 un->un_f_opt_queueing = TRUE; 7769 un->un_saved_throttle = un->un_throttle = 7770 min(un->un_throttle, 3); 7771 } else { 7772 un->un_f_opt_queueing = FALSE; 7773 un->un_saved_throttle = un->un_throttle = 1; 7774 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7775 "sd_unit_attach: un:0x%p no tag queueing\n", un); 7776 } 7777 7778 /* 7779 * Enable large transfers for SATA/SAS drives 7780 */ 7781 if (SD_IS_SERIAL(un)) { 7782 un->un_max_xfer_size = 7783 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7784 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7785 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7786 "sd_unit_attach: un:0x%p max transfer " 7787 "size=0x%x\n", un, un->un_max_xfer_size); 7788 7789 } 7790 7791 /* Setup or tear down default wide operations for disks */ 7792 7793 /* 7794 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 7795 * and "ssd_max_xfer_size" to exist simultaneously on the same 7796 * system and be set to different values. In the future this 7797 * code may need to be updated when the ssd module is 7798 * obsoleted and removed from the system. (4299588) 7799 */ 7800 if (SD_IS_PARALLEL_SCSI(un) && 7801 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 7802 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 7803 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7804 1, 1) == 1) { 7805 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7806 "sd_unit_attach: un:0x%p Wide Transfer " 7807 "enabled\n", un); 7808 } 7809 7810 /* 7811 * If tagged queuing has also been enabled, then 7812 * enable large xfers 7813 */ 7814 if (un->un_saved_throttle == sd_max_throttle) { 7815 un->un_max_xfer_size = 7816 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7817 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7818 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7819 "sd_unit_attach: un:0x%p max transfer " 7820 "size=0x%x\n", un, un->un_max_xfer_size); 7821 } 7822 } else { 7823 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7824 0, 1) == 1) { 7825 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7826 "sd_unit_attach: un:0x%p " 7827 "Wide Transfer disabled\n", un); 7828 } 7829 } 7830 } else { 7831 un->un_tagflags = FLAG_STAG; 7832 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 7833 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 7834 } 7835 7836 /* 7837 * If this target supports LUN reset, try to enable it. 7838 */ 7839 if (un->un_f_lun_reset_enabled) { 7840 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 7841 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7842 "un:0x%p lun_reset capability set\n", un); 7843 } else { 7844 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7845 "un:0x%p lun-reset capability not set\n", un); 7846 } 7847 } 7848 7849 /* 7850 * Adjust the maximum transfer size. This is to fix 7851 * the problem of partial DMA support on SPARC. Some 7852 * HBA driver, like aac, has very small dma_attr_maxxfer 7853 * size, which requires partial DMA support on SPARC. 7854 * In the future the SPARC pci nexus driver may solve 7855 * the problem instead of this fix. 7856 */ 7857 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7858 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7859 /* We need DMA partial even on sparc to ensure sddump() works */ 7860 un->un_max_xfer_size = max_xfer_size; 7861 if (un->un_partial_dma_supported == 0) 7862 un->un_partial_dma_supported = 1; 7863 } 7864 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7865 DDI_PROP_DONTPASS, "buf_break", 0) == 1) { 7866 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr, 7867 un->un_max_xfer_size) == 1) { 7868 un->un_buf_breakup_supported = 1; 7869 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7870 "un:0x%p Buf breakup enabled\n", un); 7871 } 7872 } 7873 7874 /* 7875 * Set PKT_DMA_PARTIAL flag. 7876 */ 7877 if (un->un_partial_dma_supported == 1) { 7878 un->un_pkt_flags = PKT_DMA_PARTIAL; 7879 } else { 7880 un->un_pkt_flags = 0; 7881 } 7882 7883 /* Initialize sd_ssc_t for internal uscsi commands */ 7884 ssc = sd_ssc_init(un); 7885 scsi_fm_init(devp); 7886 7887 /* 7888 * Allocate memory for SCSI FMA stuffs. 7889 */ 7890 un->un_fm_private = 7891 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP); 7892 sfip = (struct sd_fm_internal *)un->un_fm_private; 7893 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd; 7894 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo; 7895 sfip->fm_ssc.ssc_un = un; 7896 7897 if (ISCD(un) || 7898 un->un_f_has_removable_media || 7899 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) { 7900 /* 7901 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device. 7902 * Their log are unchanged. 7903 */ 7904 sfip->fm_log_level = SD_FM_LOG_NSUP; 7905 } else { 7906 /* 7907 * If enter here, it should be non-CDROM and FM-capable 7908 * device, and it will not keep the old scsi_log as before 7909 * in /var/adm/messages. However, the property 7910 * "fm-scsi-log" will control whether the FM telemetry will 7911 * be logged in /var/adm/messages. 7912 */ 7913 int fm_scsi_log; 7914 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7915 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0); 7916 7917 if (fm_scsi_log) 7918 sfip->fm_log_level = SD_FM_LOG_EREPORT; 7919 else 7920 sfip->fm_log_level = SD_FM_LOG_SILENT; 7921 } 7922 7923 /* 7924 * At this point in the attach, we have enough info in the 7925 * soft state to be able to issue commands to the target. 7926 * 7927 * All command paths used below MUST issue their commands as 7928 * SD_PATH_DIRECT. This is important as intermediate layers 7929 * are not all initialized yet (such as PM). 7930 */ 7931 7932 /* 7933 * Send a TEST UNIT READY command to the device. This should clear 7934 * any outstanding UNIT ATTENTION that may be present. 7935 * 7936 * Note: Don't check for success, just track if there is a reservation, 7937 * this is a throw away command to clear any unit attentions. 7938 * 7939 * Note: This MUST be the first command issued to the target during 7940 * attach to ensure power on UNIT ATTENTIONS are cleared. 7941 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7942 * with attempts at spinning up a device with no media. 7943 */ 7944 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 7945 if (status != 0) { 7946 if (status == EACCES) 7947 reservation_flag = SD_TARGET_IS_RESERVED; 7948 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7949 } 7950 7951 /* 7952 * If the device is NOT a removable media device, attempt to spin 7953 * it up (using the START_STOP_UNIT command) and read its capacity 7954 * (using the READ CAPACITY command). Note, however, that either 7955 * of these could fail and in some cases we would continue with 7956 * the attach despite the failure (see below). 7957 */ 7958 if (un->un_f_descr_format_supported) { 7959 7960 switch (sd_spin_up_unit(ssc)) { 7961 case 0: 7962 /* 7963 * Spin-up was successful; now try to read the 7964 * capacity. If successful then save the results 7965 * and mark the capacity & lbasize as valid. 7966 */ 7967 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7968 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7969 7970 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 7971 &lbasize, SD_PATH_DIRECT); 7972 7973 switch (status) { 7974 case 0: { 7975 if (capacity > DK_MAX_BLOCKS) { 7976 #ifdef _LP64 7977 if ((capacity + 1) > 7978 SD_GROUP1_MAX_ADDRESS) { 7979 /* 7980 * Enable descriptor format 7981 * sense data so that we can 7982 * get 64 bit sense data 7983 * fields. 7984 */ 7985 sd_enable_descr_sense(ssc); 7986 } 7987 #else 7988 /* 32-bit kernels can't handle this */ 7989 scsi_log(SD_DEVINFO(un), 7990 sd_label, CE_WARN, 7991 "disk has %llu blocks, which " 7992 "is too large for a 32-bit " 7993 "kernel", capacity); 7994 7995 #if defined(__i386) || defined(__amd64) 7996 /* 7997 * 1TB disk was treated as (1T - 512)B 7998 * in the past, so that it might have 7999 * valid VTOC and solaris partitions, 8000 * we have to allow it to continue to 8001 * work. 8002 */ 8003 if (capacity -1 > DK_MAX_BLOCKS) 8004 #endif 8005 goto spinup_failed; 8006 #endif 8007 } 8008 8009 /* 8010 * Here it's not necessary to check the case: 8011 * the capacity of the device is bigger than 8012 * what the max hba cdb can support. Because 8013 * sd_send_scsi_READ_CAPACITY will retrieve 8014 * the capacity by sending USCSI command, which 8015 * is constrained by the max hba cdb. Actually, 8016 * sd_send_scsi_READ_CAPACITY will return 8017 * EINVAL when using bigger cdb than required 8018 * cdb length. Will handle this case in 8019 * "case EINVAL". 8020 */ 8021 8022 /* 8023 * The following relies on 8024 * sd_send_scsi_READ_CAPACITY never 8025 * returning 0 for capacity and/or lbasize. 8026 */ 8027 sd_update_block_info(un, lbasize, capacity); 8028 8029 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8030 "sd_unit_attach: un:0x%p capacity = %ld " 8031 "blocks; lbasize= %ld.\n", un, 8032 un->un_blockcount, un->un_tgt_blocksize); 8033 8034 break; 8035 } 8036 case EINVAL: 8037 /* 8038 * In the case where the max-cdb-length property 8039 * is smaller than the required CDB length for 8040 * a SCSI device, a target driver can fail to 8041 * attach to that device. 8042 */ 8043 scsi_log(SD_DEVINFO(un), 8044 sd_label, CE_WARN, 8045 "disk capacity is too large " 8046 "for current cdb length"); 8047 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8048 8049 goto spinup_failed; 8050 case EACCES: 8051 /* 8052 * Should never get here if the spin-up 8053 * succeeded, but code it in anyway. 8054 * From here, just continue with the attach... 8055 */ 8056 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8057 "sd_unit_attach: un:0x%p " 8058 "sd_send_scsi_READ_CAPACITY " 8059 "returned reservation conflict\n", un); 8060 reservation_flag = SD_TARGET_IS_RESERVED; 8061 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8062 break; 8063 default: 8064 /* 8065 * Likewise, should never get here if the 8066 * spin-up succeeded. Just continue with 8067 * the attach... 8068 */ 8069 if (status == EIO) 8070 sd_ssc_assessment(ssc, 8071 SD_FMT_STATUS_CHECK); 8072 else 8073 sd_ssc_assessment(ssc, 8074 SD_FMT_IGNORE); 8075 break; 8076 } 8077 break; 8078 case EACCES: 8079 /* 8080 * Device is reserved by another host. In this case 8081 * we could not spin it up or read the capacity, but 8082 * we continue with the attach anyway. 8083 */ 8084 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8085 "sd_unit_attach: un:0x%p spin-up reservation " 8086 "conflict.\n", un); 8087 reservation_flag = SD_TARGET_IS_RESERVED; 8088 break; 8089 default: 8090 /* Fail the attach if the spin-up failed. */ 8091 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8092 "sd_unit_attach: un:0x%p spin-up failed.", un); 8093 goto spinup_failed; 8094 } 8095 8096 } 8097 8098 /* 8099 * Check to see if this is a MMC drive 8100 */ 8101 if (ISCD(un)) { 8102 sd_set_mmc_caps(ssc); 8103 } 8104 8105 /* 8106 * Add a zero-length attribute to tell the world we support 8107 * kernel ioctls (for layered drivers) 8108 */ 8109 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8110 DDI_KERNEL_IOCTL, NULL, 0); 8111 8112 /* 8113 * Add a boolean property to tell the world we support 8114 * the B_FAILFAST flag (for layered drivers) 8115 */ 8116 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8117 "ddi-failfast-supported", NULL, 0); 8118 8119 /* 8120 * Initialize power management 8121 */ 8122 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8123 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8124 sd_setup_pm(ssc, devi); 8125 if (un->un_f_pm_is_enabled == FALSE) { 8126 /* 8127 * For performance, point to a jump table that does 8128 * not include pm. 8129 * The direct and priority chains don't change with PM. 8130 * 8131 * Note: this is currently done based on individual device 8132 * capabilities. When an interface for determining system 8133 * power enabled state becomes available, or when additional 8134 * layers are added to the command chain, these values will 8135 * have to be re-evaluated for correctness. 8136 */ 8137 if (un->un_f_non_devbsize_supported) { 8138 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8139 } else { 8140 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8141 } 8142 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8143 } 8144 8145 /* 8146 * This property is set to 0 by HA software to avoid retries 8147 * on a reserved disk. (The preferred property name is 8148 * "retry-on-reservation-conflict") (1189689) 8149 * 8150 * Note: The use of a global here can have unintended consequences. A 8151 * per instance variable is preferable to match the capabilities of 8152 * different underlying hba's (4402600) 8153 */ 8154 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8155 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8156 sd_retry_on_reservation_conflict); 8157 if (sd_retry_on_reservation_conflict != 0) { 8158 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8159 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8160 sd_retry_on_reservation_conflict); 8161 } 8162 8163 /* Set up options for QFULL handling. */ 8164 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8165 "qfull-retries", -1)) != -1) { 8166 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8167 rval, 1); 8168 } 8169 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8170 "qfull-retry-interval", -1)) != -1) { 8171 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8172 rval, 1); 8173 } 8174 8175 /* 8176 * This just prints a message that announces the existence of the 8177 * device. The message is always printed in the system logfile, but 8178 * only appears on the console if the system is booted with the 8179 * -v (verbose) argument. 8180 */ 8181 ddi_report_dev(devi); 8182 8183 un->un_mediastate = DKIO_NONE; 8184 8185 /* 8186 * Check if this is a SSD(Solid State Drive). 8187 */ 8188 sd_check_solid_state(ssc); 8189 8190 /* 8191 * Check whether the drive is in emulation mode. 8192 */ 8193 sd_check_emulation_mode(ssc); 8194 8195 cmlb_alloc_handle(&un->un_cmlbhandle); 8196 8197 #if defined(__i386) || defined(__amd64) 8198 /* 8199 * On x86, compensate for off-by-1 legacy error 8200 */ 8201 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 8202 (lbasize == un->un_sys_blocksize)) 8203 offbyone = CMLB_OFF_BY_ONE; 8204 #endif 8205 8206 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 8207 VOID2BOOLEAN(un->un_f_has_removable_media != 0), 8208 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0), 8209 un->un_node_type, offbyone, un->un_cmlbhandle, 8210 (void *)SD_PATH_DIRECT) != 0) { 8211 goto cmlb_attach_failed; 8212 } 8213 8214 8215 /* 8216 * Read and validate the device's geometry (ie, disk label) 8217 * A new unformatted drive will not have a valid geometry, but 8218 * the driver needs to successfully attach to this device so 8219 * the drive can be formatted via ioctls. 8220 */ 8221 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 8222 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 8223 8224 mutex_enter(SD_MUTEX(un)); 8225 8226 /* 8227 * Read and initialize the devid for the unit. 8228 */ 8229 if (un->un_f_devid_supported) { 8230 sd_register_devid(ssc, devi, reservation_flag); 8231 } 8232 mutex_exit(SD_MUTEX(un)); 8233 8234 #if (defined(__fibre)) 8235 /* 8236 * Register callbacks for fibre only. You can't do this solely 8237 * on the basis of the devid_type because this is hba specific. 8238 * We need to query our hba capabilities to find out whether to 8239 * register or not. 8240 */ 8241 if (un->un_f_is_fibre) { 8242 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8243 sd_init_event_callbacks(un); 8244 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8245 "sd_unit_attach: un:0x%p event callbacks inserted", 8246 un); 8247 } 8248 } 8249 #endif 8250 8251 if (un->un_f_opt_disable_cache == TRUE) { 8252 /* 8253 * Disable both read cache and write cache. This is 8254 * the historic behavior of the keywords in the config file. 8255 */ 8256 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8257 0) { 8258 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8259 "sd_unit_attach: un:0x%p Could not disable " 8260 "caching", un); 8261 goto devid_failed; 8262 } 8263 } 8264 8265 /* 8266 * Check the value of the WCE bit now and 8267 * set un_f_write_cache_enabled accordingly. 8268 */ 8269 (void) sd_get_write_cache_enabled(ssc, &wc_enabled); 8270 mutex_enter(SD_MUTEX(un)); 8271 un->un_f_write_cache_enabled = (wc_enabled != 0); 8272 mutex_exit(SD_MUTEX(un)); 8273 8274 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR && 8275 un->un_tgt_blocksize != DEV_BSIZE) || 8276 un->un_f_enable_rmw) { 8277 if (!(un->un_wm_cache)) { 8278 (void) snprintf(name_str, sizeof (name_str), 8279 "%s%d_cache", 8280 ddi_driver_name(SD_DEVINFO(un)), 8281 ddi_get_instance(SD_DEVINFO(un))); 8282 un->un_wm_cache = kmem_cache_create( 8283 name_str, sizeof (struct sd_w_map), 8284 8, sd_wm_cache_constructor, 8285 sd_wm_cache_destructor, NULL, 8286 (void *)un, NULL, 0); 8287 if (!(un->un_wm_cache)) { 8288 goto wm_cache_failed; 8289 } 8290 } 8291 } 8292 8293 /* 8294 * Check the value of the NV_SUP bit and set 8295 * un_f_suppress_cache_flush accordingly. 8296 */ 8297 sd_get_nv_sup(ssc); 8298 8299 /* 8300 * Find out what type of reservation this disk supports. 8301 */ 8302 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL); 8303 8304 switch (status) { 8305 case 0: 8306 /* 8307 * SCSI-3 reservations are supported. 8308 */ 8309 un->un_reservation_type = SD_SCSI3_RESERVATION; 8310 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8311 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8312 break; 8313 case ENOTSUP: 8314 /* 8315 * The PERSISTENT RESERVE IN command would not be recognized by 8316 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8317 */ 8318 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8319 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8320 un->un_reservation_type = SD_SCSI2_RESERVATION; 8321 8322 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8323 break; 8324 default: 8325 /* 8326 * default to SCSI-3 reservations 8327 */ 8328 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8329 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8330 un->un_reservation_type = SD_SCSI3_RESERVATION; 8331 8332 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8333 break; 8334 } 8335 8336 /* 8337 * Set the pstat and error stat values here, so data obtained during the 8338 * previous attach-time routines is available. 8339 * 8340 * Note: This is a critical sequence that needs to be maintained: 8341 * 1) Instantiate the kstats before any routines using the iopath 8342 * (i.e. sd_send_scsi_cmd). 8343 * 2) Initialize the error stats (sd_set_errstats) and partition 8344 * stats (sd_set_pstats)here, following 8345 * cmlb_validate_geometry(), sd_register_devid(), and 8346 * sd_cache_control(). 8347 */ 8348 8349 if (un->un_f_pkstats_enabled && geom_label_valid) { 8350 sd_set_pstats(un); 8351 SD_TRACE(SD_LOG_IO_PARTITION, un, 8352 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8353 } 8354 8355 sd_set_errstats(un); 8356 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8357 "sd_unit_attach: un:0x%p errstats set\n", un); 8358 8359 8360 /* 8361 * After successfully attaching an instance, we record the information 8362 * of how many luns have been attached on the relative target and 8363 * controller for parallel SCSI. This information is used when sd tries 8364 * to set the tagged queuing capability in HBA. 8365 */ 8366 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8367 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 8368 } 8369 8370 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8371 "sd_unit_attach: un:0x%p exit success\n", un); 8372 8373 /* Uninitialize sd_ssc_t pointer */ 8374 sd_ssc_fini(ssc); 8375 8376 return (DDI_SUCCESS); 8377 8378 /* 8379 * An error occurred during the attach; clean up & return failure. 8380 */ 8381 wm_cache_failed: 8382 devid_failed: 8383 8384 setup_pm_failed: 8385 ddi_remove_minor_node(devi, NULL); 8386 8387 cmlb_attach_failed: 8388 /* 8389 * Cleanup from the scsi_ifsetcap() calls (437868) 8390 */ 8391 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8392 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8393 8394 /* 8395 * Refer to the comments of setting tagged-qing in the beginning of 8396 * sd_unit_attach. We can only disable tagged queuing when there is 8397 * no lun attached on the target. 8398 */ 8399 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8400 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8401 } 8402 8403 if (un->un_f_is_fibre == FALSE) { 8404 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8405 } 8406 8407 spinup_failed: 8408 8409 /* Uninitialize sd_ssc_t pointer */ 8410 sd_ssc_fini(ssc); 8411 8412 mutex_enter(SD_MUTEX(un)); 8413 8414 /* Deallocate SCSI FMA memory spaces */ 8415 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8416 8417 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8418 if (un->un_direct_priority_timeid != NULL) { 8419 timeout_id_t temp_id = un->un_direct_priority_timeid; 8420 un->un_direct_priority_timeid = NULL; 8421 mutex_exit(SD_MUTEX(un)); 8422 (void) untimeout(temp_id); 8423 mutex_enter(SD_MUTEX(un)); 8424 } 8425 8426 /* Cancel any pending start/stop timeouts */ 8427 if (un->un_startstop_timeid != NULL) { 8428 timeout_id_t temp_id = un->un_startstop_timeid; 8429 un->un_startstop_timeid = NULL; 8430 mutex_exit(SD_MUTEX(un)); 8431 (void) untimeout(temp_id); 8432 mutex_enter(SD_MUTEX(un)); 8433 } 8434 8435 /* Cancel any pending reset-throttle timeouts */ 8436 if (un->un_reset_throttle_timeid != NULL) { 8437 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8438 un->un_reset_throttle_timeid = NULL; 8439 mutex_exit(SD_MUTEX(un)); 8440 (void) untimeout(temp_id); 8441 mutex_enter(SD_MUTEX(un)); 8442 } 8443 8444 /* Cancel rmw warning message timeouts */ 8445 if (un->un_rmw_msg_timeid != NULL) { 8446 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8447 un->un_rmw_msg_timeid = NULL; 8448 mutex_exit(SD_MUTEX(un)); 8449 (void) untimeout(temp_id); 8450 mutex_enter(SD_MUTEX(un)); 8451 } 8452 8453 /* Cancel any pending retry timeouts */ 8454 if (un->un_retry_timeid != NULL) { 8455 timeout_id_t temp_id = un->un_retry_timeid; 8456 un->un_retry_timeid = NULL; 8457 mutex_exit(SD_MUTEX(un)); 8458 (void) untimeout(temp_id); 8459 mutex_enter(SD_MUTEX(un)); 8460 } 8461 8462 /* Cancel any pending delayed cv broadcast timeouts */ 8463 if (un->un_dcvb_timeid != NULL) { 8464 timeout_id_t temp_id = un->un_dcvb_timeid; 8465 un->un_dcvb_timeid = NULL; 8466 mutex_exit(SD_MUTEX(un)); 8467 (void) untimeout(temp_id); 8468 mutex_enter(SD_MUTEX(un)); 8469 } 8470 8471 mutex_exit(SD_MUTEX(un)); 8472 8473 /* There should not be any in-progress I/O so ASSERT this check */ 8474 ASSERT(un->un_ncmds_in_transport == 0); 8475 ASSERT(un->un_ncmds_in_driver == 0); 8476 8477 /* Do not free the softstate if the callback routine is active */ 8478 sd_sync_with_callback(un); 8479 8480 /* 8481 * Partition stats apparently are not used with removables. These would 8482 * not have been created during attach, so no need to clean them up... 8483 */ 8484 if (un->un_errstats != NULL) { 8485 kstat_delete(un->un_errstats); 8486 un->un_errstats = NULL; 8487 } 8488 8489 create_errstats_failed: 8490 8491 if (un->un_stats != NULL) { 8492 kstat_delete(un->un_stats); 8493 un->un_stats = NULL; 8494 } 8495 8496 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8497 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8498 8499 ddi_prop_remove_all(devi); 8500 sema_destroy(&un->un_semoclose); 8501 cv_destroy(&un->un_state_cv); 8502 8503 getrbuf_failed: 8504 8505 sd_free_rqs(un); 8506 8507 alloc_rqs_failed: 8508 8509 devp->sd_private = NULL; 8510 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8511 8512 get_softstate_failed: 8513 /* 8514 * Note: the man pages are unclear as to whether or not doing a 8515 * ddi_soft_state_free(sd_state, instance) is the right way to 8516 * clean up after the ddi_soft_state_zalloc() if the subsequent 8517 * ddi_get_soft_state() fails. The implication seems to be 8518 * that the get_soft_state cannot fail if the zalloc succeeds. 8519 */ 8520 #ifndef XPV_HVM_DRIVER 8521 ddi_soft_state_free(sd_state, instance); 8522 #endif /* !XPV_HVM_DRIVER */ 8523 8524 probe_failed: 8525 scsi_unprobe(devp); 8526 8527 return (DDI_FAILURE); 8528 } 8529 8530 8531 /* 8532 * Function: sd_unit_detach 8533 * 8534 * Description: Performs DDI_DETACH processing for sddetach(). 8535 * 8536 * Return Code: DDI_SUCCESS 8537 * DDI_FAILURE 8538 * 8539 * Context: Kernel thread context 8540 */ 8541 8542 static int 8543 sd_unit_detach(dev_info_t *devi) 8544 { 8545 struct scsi_device *devp; 8546 struct sd_lun *un; 8547 int i; 8548 int tgt; 8549 dev_t dev; 8550 dev_info_t *pdip = ddi_get_parent(devi); 8551 #ifndef XPV_HVM_DRIVER 8552 int instance = ddi_get_instance(devi); 8553 #endif /* !XPV_HVM_DRIVER */ 8554 8555 mutex_enter(&sd_detach_mutex); 8556 8557 /* 8558 * Fail the detach for any of the following: 8559 * - Unable to get the sd_lun struct for the instance 8560 * - A layered driver has an outstanding open on the instance 8561 * - Another thread is already detaching this instance 8562 * - Another thread is currently performing an open 8563 */ 8564 devp = ddi_get_driver_private(devi); 8565 if ((devp == NULL) || 8566 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8567 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8568 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8569 mutex_exit(&sd_detach_mutex); 8570 return (DDI_FAILURE); 8571 } 8572 8573 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8574 8575 /* 8576 * Mark this instance as currently in a detach, to inhibit any 8577 * opens from a layered driver. 8578 */ 8579 un->un_detach_count++; 8580 mutex_exit(&sd_detach_mutex); 8581 8582 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8583 SCSI_ADDR_PROP_TARGET, -1); 8584 8585 dev = sd_make_device(SD_DEVINFO(un)); 8586 8587 #ifndef lint 8588 _NOTE(COMPETING_THREADS_NOW); 8589 #endif 8590 8591 mutex_enter(SD_MUTEX(un)); 8592 8593 /* 8594 * Fail the detach if there are any outstanding layered 8595 * opens on this device. 8596 */ 8597 for (i = 0; i < NDKMAP; i++) { 8598 if (un->un_ocmap.lyropen[i] != 0) { 8599 goto err_notclosed; 8600 } 8601 } 8602 8603 /* 8604 * Verify there are NO outstanding commands issued to this device. 8605 * ie, un_ncmds_in_transport == 0. 8606 * It's possible to have outstanding commands through the physio 8607 * code path, even though everything's closed. 8608 */ 8609 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8610 (un->un_direct_priority_timeid != NULL) || 8611 (un->un_state == SD_STATE_RWAIT)) { 8612 mutex_exit(SD_MUTEX(un)); 8613 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8614 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8615 goto err_stillbusy; 8616 } 8617 8618 /* 8619 * If we have the device reserved, release the reservation. 8620 */ 8621 if ((un->un_resvd_status & SD_RESERVE) && 8622 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8623 mutex_exit(SD_MUTEX(un)); 8624 /* 8625 * Note: sd_reserve_release sends a command to the device 8626 * via the sd_ioctlcmd() path, and can sleep. 8627 */ 8628 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8629 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8630 "sd_dr_detach: Cannot release reservation \n"); 8631 } 8632 } else { 8633 mutex_exit(SD_MUTEX(un)); 8634 } 8635 8636 /* 8637 * Untimeout any reserve recover, throttle reset, restart unit 8638 * and delayed broadcast timeout threads. Protect the timeout pointer 8639 * from getting nulled by their callback functions. 8640 */ 8641 mutex_enter(SD_MUTEX(un)); 8642 if (un->un_resvd_timeid != NULL) { 8643 timeout_id_t temp_id = un->un_resvd_timeid; 8644 un->un_resvd_timeid = NULL; 8645 mutex_exit(SD_MUTEX(un)); 8646 (void) untimeout(temp_id); 8647 mutex_enter(SD_MUTEX(un)); 8648 } 8649 8650 if (un->un_reset_throttle_timeid != NULL) { 8651 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8652 un->un_reset_throttle_timeid = NULL; 8653 mutex_exit(SD_MUTEX(un)); 8654 (void) untimeout(temp_id); 8655 mutex_enter(SD_MUTEX(un)); 8656 } 8657 8658 if (un->un_startstop_timeid != NULL) { 8659 timeout_id_t temp_id = un->un_startstop_timeid; 8660 un->un_startstop_timeid = NULL; 8661 mutex_exit(SD_MUTEX(un)); 8662 (void) untimeout(temp_id); 8663 mutex_enter(SD_MUTEX(un)); 8664 } 8665 8666 if (un->un_rmw_msg_timeid != NULL) { 8667 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8668 un->un_rmw_msg_timeid = NULL; 8669 mutex_exit(SD_MUTEX(un)); 8670 (void) untimeout(temp_id); 8671 mutex_enter(SD_MUTEX(un)); 8672 } 8673 8674 if (un->un_dcvb_timeid != NULL) { 8675 timeout_id_t temp_id = un->un_dcvb_timeid; 8676 un->un_dcvb_timeid = NULL; 8677 mutex_exit(SD_MUTEX(un)); 8678 (void) untimeout(temp_id); 8679 } else { 8680 mutex_exit(SD_MUTEX(un)); 8681 } 8682 8683 /* Remove any pending reservation reclaim requests for this device */ 8684 sd_rmv_resv_reclaim_req(dev); 8685 8686 mutex_enter(SD_MUTEX(un)); 8687 8688 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8689 if (un->un_direct_priority_timeid != NULL) { 8690 timeout_id_t temp_id = un->un_direct_priority_timeid; 8691 un->un_direct_priority_timeid = NULL; 8692 mutex_exit(SD_MUTEX(un)); 8693 (void) untimeout(temp_id); 8694 mutex_enter(SD_MUTEX(un)); 8695 } 8696 8697 /* Cancel any active multi-host disk watch thread requests */ 8698 if (un->un_mhd_token != NULL) { 8699 mutex_exit(SD_MUTEX(un)); 8700 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8701 if (scsi_watch_request_terminate(un->un_mhd_token, 8702 SCSI_WATCH_TERMINATE_NOWAIT)) { 8703 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8704 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8705 /* 8706 * Note: We are returning here after having removed 8707 * some driver timeouts above. This is consistent with 8708 * the legacy implementation but perhaps the watch 8709 * terminate call should be made with the wait flag set. 8710 */ 8711 goto err_stillbusy; 8712 } 8713 mutex_enter(SD_MUTEX(un)); 8714 un->un_mhd_token = NULL; 8715 } 8716 8717 if (un->un_swr_token != NULL) { 8718 mutex_exit(SD_MUTEX(un)); 8719 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8720 if (scsi_watch_request_terminate(un->un_swr_token, 8721 SCSI_WATCH_TERMINATE_NOWAIT)) { 8722 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8723 "sd_dr_detach: Cannot cancel swr watch request\n"); 8724 /* 8725 * Note: We are returning here after having removed 8726 * some driver timeouts above. This is consistent with 8727 * the legacy implementation but perhaps the watch 8728 * terminate call should be made with the wait flag set. 8729 */ 8730 goto err_stillbusy; 8731 } 8732 mutex_enter(SD_MUTEX(un)); 8733 un->un_swr_token = NULL; 8734 } 8735 8736 mutex_exit(SD_MUTEX(un)); 8737 8738 /* 8739 * Clear any scsi_reset_notifies. We clear the reset notifies 8740 * if we have not registered one. 8741 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8742 */ 8743 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8744 sd_mhd_reset_notify_cb, (caddr_t)un); 8745 8746 /* 8747 * protect the timeout pointers from getting nulled by 8748 * their callback functions during the cancellation process. 8749 * In such a scenario untimeout can be invoked with a null value. 8750 */ 8751 _NOTE(NO_COMPETING_THREADS_NOW); 8752 8753 mutex_enter(&un->un_pm_mutex); 8754 if (un->un_pm_idle_timeid != NULL) { 8755 timeout_id_t temp_id = un->un_pm_idle_timeid; 8756 un->un_pm_idle_timeid = NULL; 8757 mutex_exit(&un->un_pm_mutex); 8758 8759 /* 8760 * Timeout is active; cancel it. 8761 * Note that it'll never be active on a device 8762 * that does not support PM therefore we don't 8763 * have to check before calling pm_idle_component. 8764 */ 8765 (void) untimeout(temp_id); 8766 (void) pm_idle_component(SD_DEVINFO(un), 0); 8767 mutex_enter(&un->un_pm_mutex); 8768 } 8769 8770 /* 8771 * Check whether there is already a timeout scheduled for power 8772 * management. If yes then don't lower the power here, that's. 8773 * the timeout handler's job. 8774 */ 8775 if (un->un_pm_timeid != NULL) { 8776 timeout_id_t temp_id = un->un_pm_timeid; 8777 un->un_pm_timeid = NULL; 8778 mutex_exit(&un->un_pm_mutex); 8779 /* 8780 * Timeout is active; cancel it. 8781 * Note that it'll never be active on a device 8782 * that does not support PM therefore we don't 8783 * have to check before calling pm_idle_component. 8784 */ 8785 (void) untimeout(temp_id); 8786 (void) pm_idle_component(SD_DEVINFO(un), 0); 8787 8788 } else { 8789 mutex_exit(&un->un_pm_mutex); 8790 if ((un->un_f_pm_is_enabled == TRUE) && 8791 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un)) 8792 != DDI_SUCCESS)) { 8793 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8794 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8795 /* 8796 * Fix for bug: 4297749, item # 13 8797 * The above test now includes a check to see if PM is 8798 * supported by this device before call 8799 * pm_lower_power(). 8800 * Note, the following is not dead code. The call to 8801 * pm_lower_power above will generate a call back into 8802 * our sdpower routine which might result in a timeout 8803 * handler getting activated. Therefore the following 8804 * code is valid and necessary. 8805 */ 8806 mutex_enter(&un->un_pm_mutex); 8807 if (un->un_pm_timeid != NULL) { 8808 timeout_id_t temp_id = un->un_pm_timeid; 8809 un->un_pm_timeid = NULL; 8810 mutex_exit(&un->un_pm_mutex); 8811 (void) untimeout(temp_id); 8812 (void) pm_idle_component(SD_DEVINFO(un), 0); 8813 } else { 8814 mutex_exit(&un->un_pm_mutex); 8815 } 8816 } 8817 } 8818 8819 /* 8820 * Cleanup from the scsi_ifsetcap() calls (437868) 8821 * Relocated here from above to be after the call to 8822 * pm_lower_power, which was getting errors. 8823 */ 8824 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8825 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8826 8827 /* 8828 * Currently, tagged queuing is supported per target based by HBA. 8829 * Setting this per lun instance actually sets the capability of this 8830 * target in HBA, which affects those luns already attached on the 8831 * same target. So during detach, we can only disable this capability 8832 * only when this is the only lun left on this target. By doing 8833 * this, we assume a target has the same tagged queuing capability 8834 * for every lun. The condition can be removed when HBA is changed to 8835 * support per lun based tagged queuing capability. 8836 */ 8837 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 8838 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8839 } 8840 8841 if (un->un_f_is_fibre == FALSE) { 8842 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8843 } 8844 8845 /* 8846 * Remove any event callbacks, fibre only 8847 */ 8848 if (un->un_f_is_fibre == TRUE) { 8849 if ((un->un_insert_event != NULL) && 8850 (ddi_remove_event_handler(un->un_insert_cb_id) != 8851 DDI_SUCCESS)) { 8852 /* 8853 * Note: We are returning here after having done 8854 * substantial cleanup above. This is consistent 8855 * with the legacy implementation but this may not 8856 * be the right thing to do. 8857 */ 8858 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8859 "sd_dr_detach: Cannot cancel insert event\n"); 8860 goto err_remove_event; 8861 } 8862 un->un_insert_event = NULL; 8863 8864 if ((un->un_remove_event != NULL) && 8865 (ddi_remove_event_handler(un->un_remove_cb_id) != 8866 DDI_SUCCESS)) { 8867 /* 8868 * Note: We are returning here after having done 8869 * substantial cleanup above. This is consistent 8870 * with the legacy implementation but this may not 8871 * be the right thing to do. 8872 */ 8873 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8874 "sd_dr_detach: Cannot cancel remove event\n"); 8875 goto err_remove_event; 8876 } 8877 un->un_remove_event = NULL; 8878 } 8879 8880 /* Do not free the softstate if the callback routine is active */ 8881 sd_sync_with_callback(un); 8882 8883 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 8884 cmlb_free_handle(&un->un_cmlbhandle); 8885 8886 /* 8887 * Hold the detach mutex here, to make sure that no other threads ever 8888 * can access a (partially) freed soft state structure. 8889 */ 8890 mutex_enter(&sd_detach_mutex); 8891 8892 /* 8893 * Clean up the soft state struct. 8894 * Cleanup is done in reverse order of allocs/inits. 8895 * At this point there should be no competing threads anymore. 8896 */ 8897 8898 scsi_fm_fini(devp); 8899 8900 /* 8901 * Deallocate memory for SCSI FMA. 8902 */ 8903 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8904 8905 /* 8906 * Unregister and free device id if it was not registered 8907 * by the transport. 8908 */ 8909 if (un->un_f_devid_transport_defined == FALSE) 8910 ddi_devid_unregister(devi); 8911 8912 /* 8913 * free the devid structure if allocated before (by ddi_devid_init() 8914 * or ddi_devid_get()). 8915 */ 8916 if (un->un_devid) { 8917 ddi_devid_free(un->un_devid); 8918 un->un_devid = NULL; 8919 } 8920 8921 /* 8922 * Destroy wmap cache if it exists. 8923 */ 8924 if (un->un_wm_cache != NULL) { 8925 kmem_cache_destroy(un->un_wm_cache); 8926 un->un_wm_cache = NULL; 8927 } 8928 8929 /* 8930 * kstat cleanup is done in detach for all device types (4363169). 8931 * We do not want to fail detach if the device kstats are not deleted 8932 * since there is a confusion about the devo_refcnt for the device. 8933 * We just delete the kstats and let detach complete successfully. 8934 */ 8935 if (un->un_stats != NULL) { 8936 kstat_delete(un->un_stats); 8937 un->un_stats = NULL; 8938 } 8939 if (un->un_errstats != NULL) { 8940 kstat_delete(un->un_errstats); 8941 un->un_errstats = NULL; 8942 } 8943 8944 /* Remove partition stats */ 8945 if (un->un_f_pkstats_enabled) { 8946 for (i = 0; i < NSDMAP; i++) { 8947 if (un->un_pstats[i] != NULL) { 8948 kstat_delete(un->un_pstats[i]); 8949 un->un_pstats[i] = NULL; 8950 } 8951 } 8952 } 8953 8954 /* Remove xbuf registration */ 8955 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8956 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8957 8958 /* Remove driver properties */ 8959 ddi_prop_remove_all(devi); 8960 8961 mutex_destroy(&un->un_pm_mutex); 8962 cv_destroy(&un->un_pm_busy_cv); 8963 8964 cv_destroy(&un->un_wcc_cv); 8965 8966 /* Open/close semaphore */ 8967 sema_destroy(&un->un_semoclose); 8968 8969 /* Removable media condvar. */ 8970 cv_destroy(&un->un_state_cv); 8971 8972 /* Suspend/resume condvar. */ 8973 cv_destroy(&un->un_suspend_cv); 8974 cv_destroy(&un->un_disk_busy_cv); 8975 8976 sd_free_rqs(un); 8977 8978 /* Free up soft state */ 8979 devp->sd_private = NULL; 8980 8981 bzero(un, sizeof (struct sd_lun)); 8982 #ifndef XPV_HVM_DRIVER 8983 ddi_soft_state_free(sd_state, instance); 8984 #endif /* !XPV_HVM_DRIVER */ 8985 8986 mutex_exit(&sd_detach_mutex); 8987 8988 /* This frees up the INQUIRY data associated with the device. */ 8989 scsi_unprobe(devp); 8990 8991 /* 8992 * After successfully detaching an instance, we update the information 8993 * of how many luns have been attached in the relative target and 8994 * controller for parallel SCSI. This information is used when sd tries 8995 * to set the tagged queuing capability in HBA. 8996 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 8997 * check if the device is parallel SCSI. However, we don't need to 8998 * check here because we've already checked during attach. No device 8999 * that is not parallel SCSI is in the chain. 9000 */ 9001 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9002 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9003 } 9004 9005 return (DDI_SUCCESS); 9006 9007 err_notclosed: 9008 mutex_exit(SD_MUTEX(un)); 9009 9010 err_stillbusy: 9011 _NOTE(NO_COMPETING_THREADS_NOW); 9012 9013 err_remove_event: 9014 mutex_enter(&sd_detach_mutex); 9015 un->un_detach_count--; 9016 mutex_exit(&sd_detach_mutex); 9017 9018 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9019 return (DDI_FAILURE); 9020 } 9021 9022 9023 /* 9024 * Function: sd_create_errstats 9025 * 9026 * Description: This routine instantiates the device error stats. 9027 * 9028 * Note: During attach the stats are instantiated first so they are 9029 * available for attach-time routines that utilize the driver 9030 * iopath to send commands to the device. The stats are initialized 9031 * separately so data obtained during some attach-time routines is 9032 * available. (4362483) 9033 * 9034 * Arguments: un - driver soft state (unit) structure 9035 * instance - driver instance 9036 * 9037 * Context: Kernel thread context 9038 */ 9039 9040 static void 9041 sd_create_errstats(struct sd_lun *un, int instance) 9042 { 9043 struct sd_errstats *stp; 9044 char kstatmodule_err[KSTAT_STRLEN]; 9045 char kstatname[KSTAT_STRLEN]; 9046 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9047 9048 ASSERT(un != NULL); 9049 9050 if (un->un_errstats != NULL) { 9051 return; 9052 } 9053 9054 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9055 "%serr", sd_label); 9056 (void) snprintf(kstatname, sizeof (kstatname), 9057 "%s%d,err", sd_label, instance); 9058 9059 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9060 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9061 9062 if (un->un_errstats == NULL) { 9063 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9064 "sd_create_errstats: Failed kstat_create\n"); 9065 return; 9066 } 9067 9068 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9069 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9070 KSTAT_DATA_UINT32); 9071 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9072 KSTAT_DATA_UINT32); 9073 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9074 KSTAT_DATA_UINT32); 9075 kstat_named_init(&stp->sd_vid, "Vendor", 9076 KSTAT_DATA_CHAR); 9077 kstat_named_init(&stp->sd_pid, "Product", 9078 KSTAT_DATA_CHAR); 9079 kstat_named_init(&stp->sd_revision, "Revision", 9080 KSTAT_DATA_CHAR); 9081 kstat_named_init(&stp->sd_serial, "Serial No", 9082 KSTAT_DATA_CHAR); 9083 kstat_named_init(&stp->sd_capacity, "Size", 9084 KSTAT_DATA_ULONGLONG); 9085 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9086 KSTAT_DATA_UINT32); 9087 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9088 KSTAT_DATA_UINT32); 9089 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9090 KSTAT_DATA_UINT32); 9091 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9092 KSTAT_DATA_UINT32); 9093 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9094 KSTAT_DATA_UINT32); 9095 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9096 KSTAT_DATA_UINT32); 9097 9098 un->un_errstats->ks_private = un; 9099 un->un_errstats->ks_update = nulldev; 9100 9101 kstat_install(un->un_errstats); 9102 } 9103 9104 9105 /* 9106 * Function: sd_set_errstats 9107 * 9108 * Description: This routine sets the value of the vendor id, product id, 9109 * revision, serial number, and capacity device error stats. 9110 * 9111 * Note: During attach the stats are instantiated first so they are 9112 * available for attach-time routines that utilize the driver 9113 * iopath to send commands to the device. The stats are initialized 9114 * separately so data obtained during some attach-time routines is 9115 * available. (4362483) 9116 * 9117 * Arguments: un - driver soft state (unit) structure 9118 * 9119 * Context: Kernel thread context 9120 */ 9121 9122 static void 9123 sd_set_errstats(struct sd_lun *un) 9124 { 9125 struct sd_errstats *stp; 9126 char *sn; 9127 9128 ASSERT(un != NULL); 9129 ASSERT(un->un_errstats != NULL); 9130 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9131 ASSERT(stp != NULL); 9132 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9133 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9134 (void) strncpy(stp->sd_revision.value.c, 9135 un->un_sd->sd_inq->inq_revision, 4); 9136 9137 /* 9138 * All the errstats are persistent across detach/attach, 9139 * so reset all the errstats here in case of the hot 9140 * replacement of disk drives, except for not changed 9141 * Sun qualified drives. 9142 */ 9143 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9144 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9145 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9146 stp->sd_softerrs.value.ui32 = 0; 9147 stp->sd_harderrs.value.ui32 = 0; 9148 stp->sd_transerrs.value.ui32 = 0; 9149 stp->sd_rq_media_err.value.ui32 = 0; 9150 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9151 stp->sd_rq_nodev_err.value.ui32 = 0; 9152 stp->sd_rq_recov_err.value.ui32 = 0; 9153 stp->sd_rq_illrq_err.value.ui32 = 0; 9154 stp->sd_rq_pfa_err.value.ui32 = 0; 9155 } 9156 9157 /* 9158 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9159 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9160 * (4376302)) 9161 */ 9162 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9163 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9164 sizeof (SD_INQUIRY(un)->inq_serial)); 9165 } else { 9166 /* 9167 * Set the "Serial No" kstat for non-Sun qualified drives 9168 */ 9169 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un), 9170 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 9171 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) { 9172 (void) strlcpy(stp->sd_serial.value.c, sn, 9173 sizeof (stp->sd_serial.value.c)); 9174 ddi_prop_free(sn); 9175 } 9176 } 9177 9178 if (un->un_f_blockcount_is_valid != TRUE) { 9179 /* 9180 * Set capacity error stat to 0 for no media. This ensures 9181 * a valid capacity is displayed in response to 'iostat -E' 9182 * when no media is present in the device. 9183 */ 9184 stp->sd_capacity.value.ui64 = 0; 9185 } else { 9186 /* 9187 * Multiply un_blockcount by un->un_sys_blocksize to get 9188 * capacity. 9189 * 9190 * Note: for non-512 blocksize devices "un_blockcount" has been 9191 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9192 * (un_tgt_blocksize / un->un_sys_blocksize). 9193 */ 9194 stp->sd_capacity.value.ui64 = (uint64_t) 9195 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9196 } 9197 } 9198 9199 9200 /* 9201 * Function: sd_set_pstats 9202 * 9203 * Description: This routine instantiates and initializes the partition 9204 * stats for each partition with more than zero blocks. 9205 * (4363169) 9206 * 9207 * Arguments: un - driver soft state (unit) structure 9208 * 9209 * Context: Kernel thread context 9210 */ 9211 9212 static void 9213 sd_set_pstats(struct sd_lun *un) 9214 { 9215 char kstatname[KSTAT_STRLEN]; 9216 int instance; 9217 int i; 9218 diskaddr_t nblks = 0; 9219 char *partname = NULL; 9220 9221 ASSERT(un != NULL); 9222 9223 instance = ddi_get_instance(SD_DEVINFO(un)); 9224 9225 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9226 for (i = 0; i < NSDMAP; i++) { 9227 9228 if (cmlb_partinfo(un->un_cmlbhandle, i, 9229 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 9230 continue; 9231 mutex_enter(SD_MUTEX(un)); 9232 9233 if ((un->un_pstats[i] == NULL) && 9234 (nblks != 0)) { 9235 9236 (void) snprintf(kstatname, sizeof (kstatname), 9237 "%s%d,%s", sd_label, instance, 9238 partname); 9239 9240 un->un_pstats[i] = kstat_create(sd_label, 9241 instance, kstatname, "partition", KSTAT_TYPE_IO, 9242 1, KSTAT_FLAG_PERSISTENT); 9243 if (un->un_pstats[i] != NULL) { 9244 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9245 kstat_install(un->un_pstats[i]); 9246 } 9247 } 9248 mutex_exit(SD_MUTEX(un)); 9249 } 9250 } 9251 9252 9253 #if (defined(__fibre)) 9254 /* 9255 * Function: sd_init_event_callbacks 9256 * 9257 * Description: This routine initializes the insertion and removal event 9258 * callbacks. (fibre only) 9259 * 9260 * Arguments: un - driver soft state (unit) structure 9261 * 9262 * Context: Kernel thread context 9263 */ 9264 9265 static void 9266 sd_init_event_callbacks(struct sd_lun *un) 9267 { 9268 ASSERT(un != NULL); 9269 9270 if ((un->un_insert_event == NULL) && 9271 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9272 &un->un_insert_event) == DDI_SUCCESS)) { 9273 /* 9274 * Add the callback for an insertion event 9275 */ 9276 (void) ddi_add_event_handler(SD_DEVINFO(un), 9277 un->un_insert_event, sd_event_callback, (void *)un, 9278 &(un->un_insert_cb_id)); 9279 } 9280 9281 if ((un->un_remove_event == NULL) && 9282 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9283 &un->un_remove_event) == DDI_SUCCESS)) { 9284 /* 9285 * Add the callback for a removal event 9286 */ 9287 (void) ddi_add_event_handler(SD_DEVINFO(un), 9288 un->un_remove_event, sd_event_callback, (void *)un, 9289 &(un->un_remove_cb_id)); 9290 } 9291 } 9292 9293 9294 /* 9295 * Function: sd_event_callback 9296 * 9297 * Description: This routine handles insert/remove events (photon). The 9298 * state is changed to OFFLINE which can be used to supress 9299 * error msgs. (fibre only) 9300 * 9301 * Arguments: un - driver soft state (unit) structure 9302 * 9303 * Context: Callout thread context 9304 */ 9305 /* ARGSUSED */ 9306 static void 9307 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9308 void *bus_impldata) 9309 { 9310 struct sd_lun *un = (struct sd_lun *)arg; 9311 9312 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9313 if (event == un->un_insert_event) { 9314 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9315 mutex_enter(SD_MUTEX(un)); 9316 if (un->un_state == SD_STATE_OFFLINE) { 9317 if (un->un_last_state != SD_STATE_SUSPENDED) { 9318 un->un_state = un->un_last_state; 9319 } else { 9320 /* 9321 * We have gone through SUSPEND/RESUME while 9322 * we were offline. Restore the last state 9323 */ 9324 un->un_state = un->un_save_state; 9325 } 9326 } 9327 mutex_exit(SD_MUTEX(un)); 9328 9329 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9330 } else if (event == un->un_remove_event) { 9331 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9332 mutex_enter(SD_MUTEX(un)); 9333 /* 9334 * We need to handle an event callback that occurs during 9335 * the suspend operation, since we don't prevent it. 9336 */ 9337 if (un->un_state != SD_STATE_OFFLINE) { 9338 if (un->un_state != SD_STATE_SUSPENDED) { 9339 New_state(un, SD_STATE_OFFLINE); 9340 } else { 9341 un->un_last_state = SD_STATE_OFFLINE; 9342 } 9343 } 9344 mutex_exit(SD_MUTEX(un)); 9345 } else { 9346 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9347 "!Unknown event\n"); 9348 } 9349 9350 } 9351 #endif 9352 9353 /* 9354 * Function: sd_cache_control() 9355 * 9356 * Description: This routine is the driver entry point for setting 9357 * read and write caching by modifying the WCE (write cache 9358 * enable) and RCD (read cache disable) bits of mode 9359 * page 8 (MODEPAGE_CACHING). 9360 * 9361 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9362 * structure for this target. 9363 * rcd_flag - flag for controlling the read cache 9364 * wce_flag - flag for controlling the write cache 9365 * 9366 * Return Code: EIO 9367 * code returned by sd_send_scsi_MODE_SENSE and 9368 * sd_send_scsi_MODE_SELECT 9369 * 9370 * Context: Kernel Thread 9371 */ 9372 9373 static int 9374 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag) 9375 { 9376 struct mode_caching *mode_caching_page; 9377 uchar_t *header; 9378 size_t buflen; 9379 int hdrlen; 9380 int bd_len; 9381 int rval = 0; 9382 struct mode_header_grp2 *mhp; 9383 struct sd_lun *un; 9384 int status; 9385 9386 ASSERT(ssc != NULL); 9387 un = ssc->ssc_un; 9388 ASSERT(un != NULL); 9389 9390 /* 9391 * Do a test unit ready, otherwise a mode sense may not work if this 9392 * is the first command sent to the device after boot. 9393 */ 9394 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9395 if (status != 0) 9396 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9397 9398 if (un->un_f_cfg_is_atapi == TRUE) { 9399 hdrlen = MODE_HEADER_LENGTH_GRP2; 9400 } else { 9401 hdrlen = MODE_HEADER_LENGTH; 9402 } 9403 9404 /* 9405 * Allocate memory for the retrieved mode page and its headers. Set 9406 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9407 * we get all of the mode sense data otherwise, the mode select 9408 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9409 */ 9410 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9411 sizeof (struct mode_cache_scsi3); 9412 9413 header = kmem_zalloc(buflen, KM_SLEEP); 9414 9415 /* Get the information from the device. */ 9416 if (un->un_f_cfg_is_atapi == TRUE) { 9417 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9418 MODEPAGE_CACHING, SD_PATH_DIRECT); 9419 } else { 9420 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9421 MODEPAGE_CACHING, SD_PATH_DIRECT); 9422 } 9423 9424 if (rval != 0) { 9425 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9426 "sd_cache_control: Mode Sense Failed\n"); 9427 goto mode_sense_failed; 9428 } 9429 9430 /* 9431 * Determine size of Block Descriptors in order to locate 9432 * the mode page data. ATAPI devices return 0, SCSI devices 9433 * should return MODE_BLK_DESC_LENGTH. 9434 */ 9435 if (un->un_f_cfg_is_atapi == TRUE) { 9436 mhp = (struct mode_header_grp2 *)header; 9437 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9438 } else { 9439 bd_len = ((struct mode_header *)header)->bdesc_length; 9440 } 9441 9442 if (bd_len > MODE_BLK_DESC_LENGTH) { 9443 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9444 "sd_cache_control: Mode Sense returned invalid block " 9445 "descriptor length\n"); 9446 rval = EIO; 9447 goto mode_sense_failed; 9448 } 9449 9450 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9451 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9452 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9453 "sd_cache_control: Mode Sense caching page code mismatch " 9454 "%d\n", mode_caching_page->mode_page.code); 9455 rval = EIO; 9456 goto mode_sense_failed; 9457 } 9458 9459 /* Check the relevant bits on successful mode sense. */ 9460 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9461 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9462 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9463 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9464 9465 size_t sbuflen; 9466 uchar_t save_pg; 9467 9468 /* 9469 * Construct select buffer length based on the 9470 * length of the sense data returned. 9471 */ 9472 sbuflen = hdrlen + bd_len + 9473 sizeof (struct mode_page) + 9474 (int)mode_caching_page->mode_page.length; 9475 9476 /* 9477 * Set the caching bits as requested. 9478 */ 9479 if (rcd_flag == SD_CACHE_ENABLE) 9480 mode_caching_page->rcd = 0; 9481 else if (rcd_flag == SD_CACHE_DISABLE) 9482 mode_caching_page->rcd = 1; 9483 9484 if (wce_flag == SD_CACHE_ENABLE) 9485 mode_caching_page->wce = 1; 9486 else if (wce_flag == SD_CACHE_DISABLE) 9487 mode_caching_page->wce = 0; 9488 9489 /* 9490 * Save the page if the mode sense says the 9491 * drive supports it. 9492 */ 9493 save_pg = mode_caching_page->mode_page.ps ? 9494 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9495 9496 /* Clear reserved bits before mode select. */ 9497 mode_caching_page->mode_page.ps = 0; 9498 9499 /* 9500 * Clear out mode header for mode select. 9501 * The rest of the retrieved page will be reused. 9502 */ 9503 bzero(header, hdrlen); 9504 9505 if (un->un_f_cfg_is_atapi == TRUE) { 9506 mhp = (struct mode_header_grp2 *)header; 9507 mhp->bdesc_length_hi = bd_len >> 8; 9508 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9509 } else { 9510 ((struct mode_header *)header)->bdesc_length = bd_len; 9511 } 9512 9513 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9514 9515 /* Issue mode select to change the cache settings */ 9516 if (un->un_f_cfg_is_atapi == TRUE) { 9517 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header, 9518 sbuflen, save_pg, SD_PATH_DIRECT); 9519 } else { 9520 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 9521 sbuflen, save_pg, SD_PATH_DIRECT); 9522 } 9523 9524 } 9525 9526 9527 mode_sense_failed: 9528 9529 kmem_free(header, buflen); 9530 9531 if (rval != 0) { 9532 if (rval == EIO) 9533 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9534 else 9535 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9536 } 9537 return (rval); 9538 } 9539 9540 9541 /* 9542 * Function: sd_get_write_cache_enabled() 9543 * 9544 * Description: This routine is the driver entry point for determining if 9545 * write caching is enabled. It examines the WCE (write cache 9546 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9547 * 9548 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9549 * structure for this target. 9550 * is_enabled - pointer to int where write cache enabled state 9551 * is returned (non-zero -> write cache enabled) 9552 * 9553 * 9554 * Return Code: EIO 9555 * code returned by sd_send_scsi_MODE_SENSE 9556 * 9557 * Context: Kernel Thread 9558 * 9559 * NOTE: If ioctl is added to disable write cache, this sequence should 9560 * be followed so that no locking is required for accesses to 9561 * un->un_f_write_cache_enabled: 9562 * do mode select to clear wce 9563 * do synchronize cache to flush cache 9564 * set un->un_f_write_cache_enabled = FALSE 9565 * 9566 * Conversely, an ioctl to enable the write cache should be done 9567 * in this order: 9568 * set un->un_f_write_cache_enabled = TRUE 9569 * do mode select to set wce 9570 */ 9571 9572 static int 9573 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled) 9574 { 9575 struct mode_caching *mode_caching_page; 9576 uchar_t *header; 9577 size_t buflen; 9578 int hdrlen; 9579 int bd_len; 9580 int rval = 0; 9581 struct sd_lun *un; 9582 int status; 9583 9584 ASSERT(ssc != NULL); 9585 un = ssc->ssc_un; 9586 ASSERT(un != NULL); 9587 ASSERT(is_enabled != NULL); 9588 9589 /* in case of error, flag as enabled */ 9590 *is_enabled = TRUE; 9591 9592 /* 9593 * Do a test unit ready, otherwise a mode sense may not work if this 9594 * is the first command sent to the device after boot. 9595 */ 9596 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9597 9598 if (status != 0) 9599 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9600 9601 if (un->un_f_cfg_is_atapi == TRUE) { 9602 hdrlen = MODE_HEADER_LENGTH_GRP2; 9603 } else { 9604 hdrlen = MODE_HEADER_LENGTH; 9605 } 9606 9607 /* 9608 * Allocate memory for the retrieved mode page and its headers. Set 9609 * a pointer to the page itself. 9610 */ 9611 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9612 header = kmem_zalloc(buflen, KM_SLEEP); 9613 9614 /* Get the information from the device. */ 9615 if (un->un_f_cfg_is_atapi == TRUE) { 9616 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9617 MODEPAGE_CACHING, SD_PATH_DIRECT); 9618 } else { 9619 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9620 MODEPAGE_CACHING, SD_PATH_DIRECT); 9621 } 9622 9623 if (rval != 0) { 9624 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9625 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9626 goto mode_sense_failed; 9627 } 9628 9629 /* 9630 * Determine size of Block Descriptors in order to locate 9631 * the mode page data. ATAPI devices return 0, SCSI devices 9632 * should return MODE_BLK_DESC_LENGTH. 9633 */ 9634 if (un->un_f_cfg_is_atapi == TRUE) { 9635 struct mode_header_grp2 *mhp; 9636 mhp = (struct mode_header_grp2 *)header; 9637 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9638 } else { 9639 bd_len = ((struct mode_header *)header)->bdesc_length; 9640 } 9641 9642 if (bd_len > MODE_BLK_DESC_LENGTH) { 9643 /* FMA should make upset complain here */ 9644 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9645 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9646 "block descriptor length\n"); 9647 rval = EIO; 9648 goto mode_sense_failed; 9649 } 9650 9651 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9652 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9653 /* FMA could make upset complain here */ 9654 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9655 "sd_get_write_cache_enabled: Mode Sense caching page " 9656 "code mismatch %d\n", mode_caching_page->mode_page.code); 9657 rval = EIO; 9658 goto mode_sense_failed; 9659 } 9660 *is_enabled = mode_caching_page->wce; 9661 9662 mode_sense_failed: 9663 if (rval == 0) { 9664 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 9665 } else if (rval == EIO) { 9666 /* 9667 * Some disks do not support mode sense(6), we 9668 * should ignore this kind of error(sense key is 9669 * 0x5 - illegal request). 9670 */ 9671 uint8_t *sensep; 9672 int senlen; 9673 9674 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 9675 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 9676 ssc->ssc_uscsi_cmd->uscsi_rqresid); 9677 9678 if (senlen > 0 && 9679 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 9680 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 9681 } else { 9682 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9683 } 9684 } else { 9685 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9686 } 9687 kmem_free(header, buflen); 9688 return (rval); 9689 } 9690 9691 /* 9692 * Function: sd_get_nv_sup() 9693 * 9694 * Description: This routine is the driver entry point for 9695 * determining whether non-volatile cache is supported. This 9696 * determination process works as follows: 9697 * 9698 * 1. sd first queries sd.conf on whether 9699 * suppress_cache_flush bit is set for this device. 9700 * 9701 * 2. if not there, then queries the internal disk table. 9702 * 9703 * 3. if either sd.conf or internal disk table specifies 9704 * cache flush be suppressed, we don't bother checking 9705 * NV_SUP bit. 9706 * 9707 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 9708 * the optional INQUIRY VPD page 0x86. If the device 9709 * supports VPD page 0x86, sd examines the NV_SUP 9710 * (non-volatile cache support) bit in the INQUIRY VPD page 9711 * 0x86: 9712 * o If NV_SUP bit is set, sd assumes the device has a 9713 * non-volatile cache and set the 9714 * un_f_sync_nv_supported to TRUE. 9715 * o Otherwise cache is not non-volatile, 9716 * un_f_sync_nv_supported is set to FALSE. 9717 * 9718 * Arguments: un - driver soft state (unit) structure 9719 * 9720 * Return Code: 9721 * 9722 * Context: Kernel Thread 9723 */ 9724 9725 static void 9726 sd_get_nv_sup(sd_ssc_t *ssc) 9727 { 9728 int rval = 0; 9729 uchar_t *inq86 = NULL; 9730 size_t inq86_len = MAX_INQUIRY_SIZE; 9731 size_t inq86_resid = 0; 9732 struct dk_callback *dkc; 9733 struct sd_lun *un; 9734 9735 ASSERT(ssc != NULL); 9736 un = ssc->ssc_un; 9737 ASSERT(un != NULL); 9738 9739 mutex_enter(SD_MUTEX(un)); 9740 9741 /* 9742 * Be conservative on the device's support of 9743 * SYNC_NV bit: un_f_sync_nv_supported is 9744 * initialized to be false. 9745 */ 9746 un->un_f_sync_nv_supported = FALSE; 9747 9748 /* 9749 * If either sd.conf or internal disk table 9750 * specifies cache flush be suppressed, then 9751 * we don't bother checking NV_SUP bit. 9752 */ 9753 if (un->un_f_suppress_cache_flush == TRUE) { 9754 mutex_exit(SD_MUTEX(un)); 9755 return; 9756 } 9757 9758 if (sd_check_vpd_page_support(ssc) == 0 && 9759 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 9760 mutex_exit(SD_MUTEX(un)); 9761 /* collect page 86 data if available */ 9762 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 9763 9764 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len, 9765 0x01, 0x86, &inq86_resid); 9766 9767 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 9768 SD_TRACE(SD_LOG_COMMON, un, 9769 "sd_get_nv_sup: \ 9770 successfully get VPD page: %x \ 9771 PAGE LENGTH: %x BYTE 6: %x\n", 9772 inq86[1], inq86[3], inq86[6]); 9773 9774 mutex_enter(SD_MUTEX(un)); 9775 /* 9776 * check the value of NV_SUP bit: only if the device 9777 * reports NV_SUP bit to be 1, the 9778 * un_f_sync_nv_supported bit will be set to true. 9779 */ 9780 if (inq86[6] & SD_VPD_NV_SUP) { 9781 un->un_f_sync_nv_supported = TRUE; 9782 } 9783 mutex_exit(SD_MUTEX(un)); 9784 } else if (rval != 0) { 9785 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9786 } 9787 9788 kmem_free(inq86, inq86_len); 9789 } else { 9790 mutex_exit(SD_MUTEX(un)); 9791 } 9792 9793 /* 9794 * Send a SYNC CACHE command to check whether 9795 * SYNC_NV bit is supported. This command should have 9796 * un_f_sync_nv_supported set to correct value. 9797 */ 9798 mutex_enter(SD_MUTEX(un)); 9799 if (un->un_f_sync_nv_supported) { 9800 mutex_exit(SD_MUTEX(un)); 9801 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 9802 dkc->dkc_flag = FLUSH_VOLATILE; 9803 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 9804 9805 /* 9806 * Send a TEST UNIT READY command to the device. This should 9807 * clear any outstanding UNIT ATTENTION that may be present. 9808 */ 9809 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 9810 if (rval != 0) 9811 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9812 9813 kmem_free(dkc, sizeof (struct dk_callback)); 9814 } else { 9815 mutex_exit(SD_MUTEX(un)); 9816 } 9817 9818 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 9819 un_f_suppress_cache_flush is set to %d\n", 9820 un->un_f_suppress_cache_flush); 9821 } 9822 9823 /* 9824 * Function: sd_make_device 9825 * 9826 * Description: Utility routine to return the Solaris device number from 9827 * the data in the device's dev_info structure. 9828 * 9829 * Return Code: The Solaris device number 9830 * 9831 * Context: Any 9832 */ 9833 9834 static dev_t 9835 sd_make_device(dev_info_t *devi) 9836 { 9837 return (makedevice(ddi_driver_major(devi), 9838 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9839 } 9840 9841 9842 /* 9843 * Function: sd_pm_entry 9844 * 9845 * Description: Called at the start of a new command to manage power 9846 * and busy status of a device. This includes determining whether 9847 * the current power state of the device is sufficient for 9848 * performing the command or whether it must be changed. 9849 * The PM framework is notified appropriately. 9850 * Only with a return status of DDI_SUCCESS will the 9851 * component be busy to the framework. 9852 * 9853 * All callers of sd_pm_entry must check the return status 9854 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9855 * of DDI_FAILURE indicates the device failed to power up. 9856 * In this case un_pm_count has been adjusted so the result 9857 * on exit is still powered down, ie. count is less than 0. 9858 * Calling sd_pm_exit with this count value hits an ASSERT. 9859 * 9860 * Return Code: DDI_SUCCESS or DDI_FAILURE 9861 * 9862 * Context: Kernel thread context. 9863 */ 9864 9865 static int 9866 sd_pm_entry(struct sd_lun *un) 9867 { 9868 int return_status = DDI_SUCCESS; 9869 9870 ASSERT(!mutex_owned(SD_MUTEX(un))); 9871 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9872 9873 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9874 9875 if (un->un_f_pm_is_enabled == FALSE) { 9876 SD_TRACE(SD_LOG_IO_PM, un, 9877 "sd_pm_entry: exiting, PM not enabled\n"); 9878 return (return_status); 9879 } 9880 9881 /* 9882 * Just increment a counter if PM is enabled. On the transition from 9883 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9884 * the count with each IO and mark the device as idle when the count 9885 * hits 0. 9886 * 9887 * If the count is less than 0 the device is powered down. If a powered 9888 * down device is successfully powered up then the count must be 9889 * incremented to reflect the power up. Note that it'll get incremented 9890 * a second time to become busy. 9891 * 9892 * Because the following has the potential to change the device state 9893 * and must release the un_pm_mutex to do so, only one thread can be 9894 * allowed through at a time. 9895 */ 9896 9897 mutex_enter(&un->un_pm_mutex); 9898 while (un->un_pm_busy == TRUE) { 9899 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9900 } 9901 un->un_pm_busy = TRUE; 9902 9903 if (un->un_pm_count < 1) { 9904 9905 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9906 9907 /* 9908 * Indicate we are now busy so the framework won't attempt to 9909 * power down the device. This call will only fail if either 9910 * we passed a bad component number or the device has no 9911 * components. Neither of these should ever happen. 9912 */ 9913 mutex_exit(&un->un_pm_mutex); 9914 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9915 ASSERT(return_status == DDI_SUCCESS); 9916 9917 mutex_enter(&un->un_pm_mutex); 9918 9919 if (un->un_pm_count < 0) { 9920 mutex_exit(&un->un_pm_mutex); 9921 9922 SD_TRACE(SD_LOG_IO_PM, un, 9923 "sd_pm_entry: power up component\n"); 9924 9925 /* 9926 * pm_raise_power will cause sdpower to be called 9927 * which brings the device power level to the 9928 * desired state, If successful, un_pm_count and 9929 * un_power_level will be updated appropriately. 9930 */ 9931 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9932 SD_PM_STATE_ACTIVE(un)); 9933 9934 mutex_enter(&un->un_pm_mutex); 9935 9936 if (return_status != DDI_SUCCESS) { 9937 /* 9938 * Power up failed. 9939 * Idle the device and adjust the count 9940 * so the result on exit is that we're 9941 * still powered down, ie. count is less than 0. 9942 */ 9943 SD_TRACE(SD_LOG_IO_PM, un, 9944 "sd_pm_entry: power up failed," 9945 " idle the component\n"); 9946 9947 (void) pm_idle_component(SD_DEVINFO(un), 0); 9948 un->un_pm_count--; 9949 } else { 9950 /* 9951 * Device is powered up, verify the 9952 * count is non-negative. 9953 * This is debug only. 9954 */ 9955 ASSERT(un->un_pm_count == 0); 9956 } 9957 } 9958 9959 if (return_status == DDI_SUCCESS) { 9960 /* 9961 * For performance, now that the device has been tagged 9962 * as busy, and it's known to be powered up, update the 9963 * chain types to use jump tables that do not include 9964 * pm. This significantly lowers the overhead and 9965 * therefore improves performance. 9966 */ 9967 9968 mutex_exit(&un->un_pm_mutex); 9969 mutex_enter(SD_MUTEX(un)); 9970 SD_TRACE(SD_LOG_IO_PM, un, 9971 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9972 un->un_uscsi_chain_type); 9973 9974 if (un->un_f_non_devbsize_supported) { 9975 un->un_buf_chain_type = 9976 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9977 } else { 9978 un->un_buf_chain_type = 9979 SD_CHAIN_INFO_DISK_NO_PM; 9980 } 9981 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9982 9983 SD_TRACE(SD_LOG_IO_PM, un, 9984 " changed uscsi_chain_type to %d\n", 9985 un->un_uscsi_chain_type); 9986 mutex_exit(SD_MUTEX(un)); 9987 mutex_enter(&un->un_pm_mutex); 9988 9989 if (un->un_pm_idle_timeid == NULL) { 9990 /* 300 ms. */ 9991 un->un_pm_idle_timeid = 9992 timeout(sd_pm_idletimeout_handler, un, 9993 (drv_usectohz((clock_t)300000))); 9994 /* 9995 * Include an extra call to busy which keeps the 9996 * device busy with-respect-to the PM layer 9997 * until the timer fires, at which time it'll 9998 * get the extra idle call. 9999 */ 10000 (void) pm_busy_component(SD_DEVINFO(un), 0); 10001 } 10002 } 10003 } 10004 un->un_pm_busy = FALSE; 10005 /* Next... */ 10006 cv_signal(&un->un_pm_busy_cv); 10007 10008 un->un_pm_count++; 10009 10010 SD_TRACE(SD_LOG_IO_PM, un, 10011 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10012 10013 mutex_exit(&un->un_pm_mutex); 10014 10015 return (return_status); 10016 } 10017 10018 10019 /* 10020 * Function: sd_pm_exit 10021 * 10022 * Description: Called at the completion of a command to manage busy 10023 * status for the device. If the device becomes idle the 10024 * PM framework is notified. 10025 * 10026 * Context: Kernel thread context 10027 */ 10028 10029 static void 10030 sd_pm_exit(struct sd_lun *un) 10031 { 10032 ASSERT(!mutex_owned(SD_MUTEX(un))); 10033 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10034 10035 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10036 10037 /* 10038 * After attach the following flag is only read, so don't 10039 * take the penalty of acquiring a mutex for it. 10040 */ 10041 if (un->un_f_pm_is_enabled == TRUE) { 10042 10043 mutex_enter(&un->un_pm_mutex); 10044 un->un_pm_count--; 10045 10046 SD_TRACE(SD_LOG_IO_PM, un, 10047 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10048 10049 ASSERT(un->un_pm_count >= 0); 10050 if (un->un_pm_count == 0) { 10051 mutex_exit(&un->un_pm_mutex); 10052 10053 SD_TRACE(SD_LOG_IO_PM, un, 10054 "sd_pm_exit: idle component\n"); 10055 10056 (void) pm_idle_component(SD_DEVINFO(un), 0); 10057 10058 } else { 10059 mutex_exit(&un->un_pm_mutex); 10060 } 10061 } 10062 10063 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10064 } 10065 10066 10067 /* 10068 * Function: sdopen 10069 * 10070 * Description: Driver's open(9e) entry point function. 10071 * 10072 * Arguments: dev_i - pointer to device number 10073 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10074 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10075 * cred_p - user credential pointer 10076 * 10077 * Return Code: EINVAL 10078 * ENXIO 10079 * EIO 10080 * EROFS 10081 * EBUSY 10082 * 10083 * Context: Kernel thread context 10084 */ 10085 /* ARGSUSED */ 10086 static int 10087 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10088 { 10089 struct sd_lun *un; 10090 int nodelay; 10091 int part; 10092 uint64_t partmask; 10093 int instance; 10094 dev_t dev; 10095 int rval = EIO; 10096 diskaddr_t nblks = 0; 10097 diskaddr_t label_cap; 10098 10099 /* Validate the open type */ 10100 if (otyp >= OTYPCNT) { 10101 return (EINVAL); 10102 } 10103 10104 dev = *dev_p; 10105 instance = SDUNIT(dev); 10106 mutex_enter(&sd_detach_mutex); 10107 10108 /* 10109 * Fail the open if there is no softstate for the instance, or 10110 * if another thread somewhere is trying to detach the instance. 10111 */ 10112 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10113 (un->un_detach_count != 0)) { 10114 mutex_exit(&sd_detach_mutex); 10115 /* 10116 * The probe cache only needs to be cleared when open (9e) fails 10117 * with ENXIO (4238046). 10118 */ 10119 /* 10120 * un-conditionally clearing probe cache is ok with 10121 * separate sd/ssd binaries 10122 * x86 platform can be an issue with both parallel 10123 * and fibre in 1 binary 10124 */ 10125 sd_scsi_clear_probe_cache(); 10126 return (ENXIO); 10127 } 10128 10129 /* 10130 * The un_layer_count is to prevent another thread in specfs from 10131 * trying to detach the instance, which can happen when we are 10132 * called from a higher-layer driver instead of thru specfs. 10133 * This will not be needed when DDI provides a layered driver 10134 * interface that allows specfs to know that an instance is in 10135 * use by a layered driver & should not be detached. 10136 * 10137 * Note: the semantics for layered driver opens are exactly one 10138 * close for every open. 10139 */ 10140 if (otyp == OTYP_LYR) { 10141 un->un_layer_count++; 10142 } 10143 10144 /* 10145 * Keep a count of the current # of opens in progress. This is because 10146 * some layered drivers try to call us as a regular open. This can 10147 * cause problems that we cannot prevent, however by keeping this count 10148 * we can at least keep our open and detach routines from racing against 10149 * each other under such conditions. 10150 */ 10151 un->un_opens_in_progress++; 10152 mutex_exit(&sd_detach_mutex); 10153 10154 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10155 part = SDPART(dev); 10156 partmask = 1 << part; 10157 10158 /* 10159 * We use a semaphore here in order to serialize 10160 * open and close requests on the device. 10161 */ 10162 sema_p(&un->un_semoclose); 10163 10164 mutex_enter(SD_MUTEX(un)); 10165 10166 /* 10167 * All device accesses go thru sdstrategy() where we check 10168 * on suspend status but there could be a scsi_poll command, 10169 * which bypasses sdstrategy(), so we need to check pm 10170 * status. 10171 */ 10172 10173 if (!nodelay) { 10174 while ((un->un_state == SD_STATE_SUSPENDED) || 10175 (un->un_state == SD_STATE_PM_CHANGING)) { 10176 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10177 } 10178 10179 mutex_exit(SD_MUTEX(un)); 10180 if (sd_pm_entry(un) != DDI_SUCCESS) { 10181 rval = EIO; 10182 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10183 "sdopen: sd_pm_entry failed\n"); 10184 goto open_failed_with_pm; 10185 } 10186 mutex_enter(SD_MUTEX(un)); 10187 } 10188 10189 /* check for previous exclusive open */ 10190 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10191 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10192 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10193 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10194 10195 if (un->un_exclopen & (partmask)) { 10196 goto excl_open_fail; 10197 } 10198 10199 if (flag & FEXCL) { 10200 int i; 10201 if (un->un_ocmap.lyropen[part]) { 10202 goto excl_open_fail; 10203 } 10204 for (i = 0; i < (OTYPCNT - 1); i++) { 10205 if (un->un_ocmap.regopen[i] & (partmask)) { 10206 goto excl_open_fail; 10207 } 10208 } 10209 } 10210 10211 /* 10212 * Check the write permission if this is a removable media device, 10213 * NDELAY has not been set, and writable permission is requested. 10214 * 10215 * Note: If NDELAY was set and this is write-protected media the WRITE 10216 * attempt will fail with EIO as part of the I/O processing. This is a 10217 * more permissive implementation that allows the open to succeed and 10218 * WRITE attempts to fail when appropriate. 10219 */ 10220 if (un->un_f_chk_wp_open) { 10221 if ((flag & FWRITE) && (!nodelay)) { 10222 mutex_exit(SD_MUTEX(un)); 10223 /* 10224 * Defer the check for write permission on writable 10225 * DVD drive till sdstrategy and will not fail open even 10226 * if FWRITE is set as the device can be writable 10227 * depending upon the media and the media can change 10228 * after the call to open(). 10229 */ 10230 if (un->un_f_dvdram_writable_device == FALSE) { 10231 if (ISCD(un) || sr_check_wp(dev)) { 10232 rval = EROFS; 10233 mutex_enter(SD_MUTEX(un)); 10234 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10235 "write to cd or write protected media\n"); 10236 goto open_fail; 10237 } 10238 } 10239 mutex_enter(SD_MUTEX(un)); 10240 } 10241 } 10242 10243 /* 10244 * If opening in NDELAY/NONBLOCK mode, just return. 10245 * Check if disk is ready and has a valid geometry later. 10246 */ 10247 if (!nodelay) { 10248 sd_ssc_t *ssc; 10249 10250 mutex_exit(SD_MUTEX(un)); 10251 ssc = sd_ssc_init(un); 10252 rval = sd_ready_and_valid(ssc, part); 10253 sd_ssc_fini(ssc); 10254 mutex_enter(SD_MUTEX(un)); 10255 /* 10256 * Fail if device is not ready or if the number of disk 10257 * blocks is zero or negative for non CD devices. 10258 */ 10259 10260 nblks = 0; 10261 10262 if (rval == SD_READY_VALID && (!ISCD(un))) { 10263 /* if cmlb_partinfo fails, nblks remains 0 */ 10264 mutex_exit(SD_MUTEX(un)); 10265 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 10266 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 10267 mutex_enter(SD_MUTEX(un)); 10268 } 10269 10270 if ((rval != SD_READY_VALID) || 10271 (!ISCD(un) && nblks <= 0)) { 10272 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10273 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10274 "device not ready or invalid disk block value\n"); 10275 goto open_fail; 10276 } 10277 #if defined(__i386) || defined(__amd64) 10278 } else { 10279 uchar_t *cp; 10280 /* 10281 * x86 requires special nodelay handling, so that p0 is 10282 * always defined and accessible. 10283 * Invalidate geometry only if device is not already open. 10284 */ 10285 cp = &un->un_ocmap.chkd[0]; 10286 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10287 if (*cp != (uchar_t)0) { 10288 break; 10289 } 10290 cp++; 10291 } 10292 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10293 mutex_exit(SD_MUTEX(un)); 10294 cmlb_invalidate(un->un_cmlbhandle, 10295 (void *)SD_PATH_DIRECT); 10296 mutex_enter(SD_MUTEX(un)); 10297 } 10298 10299 #endif 10300 } 10301 10302 if (otyp == OTYP_LYR) { 10303 un->un_ocmap.lyropen[part]++; 10304 } else { 10305 un->un_ocmap.regopen[otyp] |= partmask; 10306 } 10307 10308 /* Set up open and exclusive open flags */ 10309 if (flag & FEXCL) { 10310 un->un_exclopen |= (partmask); 10311 } 10312 10313 /* 10314 * If the lun is EFI labeled and lun capacity is greater than the 10315 * capacity contained in the label, log a sys-event to notify the 10316 * interested module. 10317 * To avoid an infinite loop of logging sys-event, we only log the 10318 * event when the lun is not opened in NDELAY mode. The event handler 10319 * should open the lun in NDELAY mode. 10320 */ 10321 if (!nodelay) { 10322 mutex_exit(SD_MUTEX(un)); 10323 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 10324 (void*)SD_PATH_DIRECT) == 0) { 10325 mutex_enter(SD_MUTEX(un)); 10326 if (un->un_f_blockcount_is_valid && 10327 un->un_blockcount > label_cap && 10328 un->un_f_expnevent == B_FALSE) { 10329 un->un_f_expnevent = B_TRUE; 10330 mutex_exit(SD_MUTEX(un)); 10331 sd_log_lun_expansion_event(un, 10332 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 10333 mutex_enter(SD_MUTEX(un)); 10334 } 10335 } else { 10336 mutex_enter(SD_MUTEX(un)); 10337 } 10338 } 10339 10340 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10341 "open of part %d type %d\n", part, otyp); 10342 10343 mutex_exit(SD_MUTEX(un)); 10344 if (!nodelay) { 10345 sd_pm_exit(un); 10346 } 10347 10348 sema_v(&un->un_semoclose); 10349 10350 mutex_enter(&sd_detach_mutex); 10351 un->un_opens_in_progress--; 10352 mutex_exit(&sd_detach_mutex); 10353 10354 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10355 return (DDI_SUCCESS); 10356 10357 excl_open_fail: 10358 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10359 rval = EBUSY; 10360 10361 open_fail: 10362 mutex_exit(SD_MUTEX(un)); 10363 10364 /* 10365 * On a failed open we must exit the pm management. 10366 */ 10367 if (!nodelay) { 10368 sd_pm_exit(un); 10369 } 10370 open_failed_with_pm: 10371 sema_v(&un->un_semoclose); 10372 10373 mutex_enter(&sd_detach_mutex); 10374 un->un_opens_in_progress--; 10375 if (otyp == OTYP_LYR) { 10376 un->un_layer_count--; 10377 } 10378 mutex_exit(&sd_detach_mutex); 10379 10380 return (rval); 10381 } 10382 10383 10384 /* 10385 * Function: sdclose 10386 * 10387 * Description: Driver's close(9e) entry point function. 10388 * 10389 * Arguments: dev - device number 10390 * flag - file status flag, informational only 10391 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10392 * cred_p - user credential pointer 10393 * 10394 * Return Code: ENXIO 10395 * 10396 * Context: Kernel thread context 10397 */ 10398 /* ARGSUSED */ 10399 static int 10400 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10401 { 10402 struct sd_lun *un; 10403 uchar_t *cp; 10404 int part; 10405 int nodelay; 10406 int rval = 0; 10407 10408 /* Validate the open type */ 10409 if (otyp >= OTYPCNT) { 10410 return (ENXIO); 10411 } 10412 10413 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10414 return (ENXIO); 10415 } 10416 10417 part = SDPART(dev); 10418 nodelay = flag & (FNDELAY | FNONBLOCK); 10419 10420 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10421 "sdclose: close of part %d type %d\n", part, otyp); 10422 10423 /* 10424 * We use a semaphore here in order to serialize 10425 * open and close requests on the device. 10426 */ 10427 sema_p(&un->un_semoclose); 10428 10429 mutex_enter(SD_MUTEX(un)); 10430 10431 /* Don't proceed if power is being changed. */ 10432 while (un->un_state == SD_STATE_PM_CHANGING) { 10433 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10434 } 10435 10436 if (un->un_exclopen & (1 << part)) { 10437 un->un_exclopen &= ~(1 << part); 10438 } 10439 10440 /* Update the open partition map */ 10441 if (otyp == OTYP_LYR) { 10442 un->un_ocmap.lyropen[part] -= 1; 10443 } else { 10444 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10445 } 10446 10447 cp = &un->un_ocmap.chkd[0]; 10448 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10449 if (*cp != NULL) { 10450 break; 10451 } 10452 cp++; 10453 } 10454 10455 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10456 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10457 10458 /* 10459 * We avoid persistance upon the last close, and set 10460 * the throttle back to the maximum. 10461 */ 10462 un->un_throttle = un->un_saved_throttle; 10463 10464 if (un->un_state == SD_STATE_OFFLINE) { 10465 if (un->un_f_is_fibre == FALSE) { 10466 scsi_log(SD_DEVINFO(un), sd_label, 10467 CE_WARN, "offline\n"); 10468 } 10469 mutex_exit(SD_MUTEX(un)); 10470 cmlb_invalidate(un->un_cmlbhandle, 10471 (void *)SD_PATH_DIRECT); 10472 mutex_enter(SD_MUTEX(un)); 10473 10474 } else { 10475 /* 10476 * Flush any outstanding writes in NVRAM cache. 10477 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10478 * cmd, it may not work for non-Pluto devices. 10479 * SYNCHRONIZE CACHE is not required for removables, 10480 * except DVD-RAM drives. 10481 * 10482 * Also note: because SYNCHRONIZE CACHE is currently 10483 * the only command issued here that requires the 10484 * drive be powered up, only do the power up before 10485 * sending the Sync Cache command. If additional 10486 * commands are added which require a powered up 10487 * drive, the following sequence may have to change. 10488 * 10489 * And finally, note that parallel SCSI on SPARC 10490 * only issues a Sync Cache to DVD-RAM, a newly 10491 * supported device. 10492 */ 10493 #if defined(__i386) || defined(__amd64) 10494 if ((un->un_f_sync_cache_supported && 10495 un->un_f_sync_cache_required) || 10496 un->un_f_dvdram_writable_device == TRUE) { 10497 #else 10498 if (un->un_f_dvdram_writable_device == TRUE) { 10499 #endif 10500 mutex_exit(SD_MUTEX(un)); 10501 if (sd_pm_entry(un) == DDI_SUCCESS) { 10502 rval = 10503 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10504 NULL); 10505 /* ignore error if not supported */ 10506 if (rval == ENOTSUP) { 10507 rval = 0; 10508 } else if (rval != 0) { 10509 rval = EIO; 10510 } 10511 sd_pm_exit(un); 10512 } else { 10513 rval = EIO; 10514 } 10515 mutex_enter(SD_MUTEX(un)); 10516 } 10517 10518 /* 10519 * For devices which supports DOOR_LOCK, send an ALLOW 10520 * MEDIA REMOVAL command, but don't get upset if it 10521 * fails. We need to raise the power of the drive before 10522 * we can call sd_send_scsi_DOORLOCK() 10523 */ 10524 if (un->un_f_doorlock_supported) { 10525 mutex_exit(SD_MUTEX(un)); 10526 if (sd_pm_entry(un) == DDI_SUCCESS) { 10527 sd_ssc_t *ssc; 10528 10529 ssc = sd_ssc_init(un); 10530 rval = sd_send_scsi_DOORLOCK(ssc, 10531 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10532 if (rval != 0) 10533 sd_ssc_assessment(ssc, 10534 SD_FMT_IGNORE); 10535 sd_ssc_fini(ssc); 10536 10537 sd_pm_exit(un); 10538 if (ISCD(un) && (rval != 0) && 10539 (nodelay != 0)) { 10540 rval = ENXIO; 10541 } 10542 } else { 10543 rval = EIO; 10544 } 10545 mutex_enter(SD_MUTEX(un)); 10546 } 10547 10548 /* 10549 * If a device has removable media, invalidate all 10550 * parameters related to media, such as geometry, 10551 * blocksize, and blockcount. 10552 */ 10553 if (un->un_f_has_removable_media) { 10554 sr_ejected(un); 10555 } 10556 10557 /* 10558 * Destroy the cache (if it exists) which was 10559 * allocated for the write maps since this is 10560 * the last close for this media. 10561 */ 10562 if (un->un_wm_cache) { 10563 /* 10564 * Check if there are pending commands. 10565 * and if there are give a warning and 10566 * do not destroy the cache. 10567 */ 10568 if (un->un_ncmds_in_driver > 0) { 10569 scsi_log(SD_DEVINFO(un), 10570 sd_label, CE_WARN, 10571 "Unable to clean up memory " 10572 "because of pending I/O\n"); 10573 } else { 10574 kmem_cache_destroy( 10575 un->un_wm_cache); 10576 un->un_wm_cache = NULL; 10577 } 10578 } 10579 } 10580 } 10581 10582 mutex_exit(SD_MUTEX(un)); 10583 sema_v(&un->un_semoclose); 10584 10585 if (otyp == OTYP_LYR) { 10586 mutex_enter(&sd_detach_mutex); 10587 /* 10588 * The detach routine may run when the layer count 10589 * drops to zero. 10590 */ 10591 un->un_layer_count--; 10592 mutex_exit(&sd_detach_mutex); 10593 } 10594 10595 return (rval); 10596 } 10597 10598 10599 /* 10600 * Function: sd_ready_and_valid 10601 * 10602 * Description: Test if device is ready and has a valid geometry. 10603 * 10604 * Arguments: ssc - sd_ssc_t will contain un 10605 * un - driver soft state (unit) structure 10606 * 10607 * Return Code: SD_READY_VALID ready and valid label 10608 * SD_NOT_READY_VALID not ready, no label 10609 * SD_RESERVED_BY_OTHERS reservation conflict 10610 * 10611 * Context: Never called at interrupt context. 10612 */ 10613 10614 static int 10615 sd_ready_and_valid(sd_ssc_t *ssc, int part) 10616 { 10617 struct sd_errstats *stp; 10618 uint64_t capacity; 10619 uint_t lbasize; 10620 int rval = SD_READY_VALID; 10621 char name_str[48]; 10622 boolean_t is_valid; 10623 struct sd_lun *un; 10624 int status; 10625 10626 ASSERT(ssc != NULL); 10627 un = ssc->ssc_un; 10628 ASSERT(un != NULL); 10629 ASSERT(!mutex_owned(SD_MUTEX(un))); 10630 10631 mutex_enter(SD_MUTEX(un)); 10632 /* 10633 * If a device has removable media, we must check if media is 10634 * ready when checking if this device is ready and valid. 10635 */ 10636 if (un->un_f_has_removable_media) { 10637 mutex_exit(SD_MUTEX(un)); 10638 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10639 10640 if (status != 0) { 10641 rval = SD_NOT_READY_VALID; 10642 mutex_enter(SD_MUTEX(un)); 10643 10644 /* Ignore all failed status for removalbe media */ 10645 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10646 10647 goto done; 10648 } 10649 10650 is_valid = SD_IS_VALID_LABEL(un); 10651 mutex_enter(SD_MUTEX(un)); 10652 if (!is_valid || 10653 (un->un_f_blockcount_is_valid == FALSE) || 10654 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10655 10656 /* capacity has to be read every open. */ 10657 mutex_exit(SD_MUTEX(un)); 10658 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 10659 &lbasize, SD_PATH_DIRECT); 10660 10661 if (status != 0) { 10662 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10663 10664 cmlb_invalidate(un->un_cmlbhandle, 10665 (void *)SD_PATH_DIRECT); 10666 mutex_enter(SD_MUTEX(un)); 10667 rval = SD_NOT_READY_VALID; 10668 10669 goto done; 10670 } else { 10671 mutex_enter(SD_MUTEX(un)); 10672 sd_update_block_info(un, lbasize, capacity); 10673 } 10674 } 10675 10676 /* 10677 * Check if the media in the device is writable or not. 10678 */ 10679 if (!is_valid && ISCD(un)) { 10680 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 10681 } 10682 10683 } else { 10684 /* 10685 * Do a test unit ready to clear any unit attention from non-cd 10686 * devices. 10687 */ 10688 mutex_exit(SD_MUTEX(un)); 10689 10690 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10691 if (status != 0) { 10692 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10693 } 10694 10695 mutex_enter(SD_MUTEX(un)); 10696 } 10697 10698 10699 /* 10700 * If this is a non 512 block device, allocate space for 10701 * the wmap cache. This is being done here since every time 10702 * a media is changed this routine will be called and the 10703 * block size is a function of media rather than device. 10704 */ 10705 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR || 10706 un->un_f_non_devbsize_supported) && 10707 un->un_tgt_blocksize != DEV_BSIZE) || 10708 un->un_f_enable_rmw) { 10709 if (!(un->un_wm_cache)) { 10710 (void) snprintf(name_str, sizeof (name_str), 10711 "%s%d_cache", 10712 ddi_driver_name(SD_DEVINFO(un)), 10713 ddi_get_instance(SD_DEVINFO(un))); 10714 un->un_wm_cache = kmem_cache_create( 10715 name_str, sizeof (struct sd_w_map), 10716 8, sd_wm_cache_constructor, 10717 sd_wm_cache_destructor, NULL, 10718 (void *)un, NULL, 0); 10719 if (!(un->un_wm_cache)) { 10720 rval = ENOMEM; 10721 goto done; 10722 } 10723 } 10724 } 10725 10726 if (un->un_state == SD_STATE_NORMAL) { 10727 /* 10728 * If the target is not yet ready here (defined by a TUR 10729 * failure), invalidate the geometry and print an 'offline' 10730 * message. This is a legacy message, as the state of the 10731 * target is not actually changed to SD_STATE_OFFLINE. 10732 * 10733 * If the TUR fails for EACCES (Reservation Conflict), 10734 * SD_RESERVED_BY_OTHERS will be returned to indicate 10735 * reservation conflict. If the TUR fails for other 10736 * reasons, SD_NOT_READY_VALID will be returned. 10737 */ 10738 int err; 10739 10740 mutex_exit(SD_MUTEX(un)); 10741 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10742 mutex_enter(SD_MUTEX(un)); 10743 10744 if (err != 0) { 10745 mutex_exit(SD_MUTEX(un)); 10746 cmlb_invalidate(un->un_cmlbhandle, 10747 (void *)SD_PATH_DIRECT); 10748 mutex_enter(SD_MUTEX(un)); 10749 if (err == EACCES) { 10750 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10751 "reservation conflict\n"); 10752 rval = SD_RESERVED_BY_OTHERS; 10753 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10754 } else { 10755 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10756 "drive offline\n"); 10757 rval = SD_NOT_READY_VALID; 10758 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 10759 } 10760 goto done; 10761 } 10762 } 10763 10764 if (un->un_f_format_in_progress == FALSE) { 10765 mutex_exit(SD_MUTEX(un)); 10766 10767 (void) cmlb_validate(un->un_cmlbhandle, 0, 10768 (void *)SD_PATH_DIRECT); 10769 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL, 10770 NULL, (void *) SD_PATH_DIRECT) != 0) { 10771 rval = SD_NOT_READY_VALID; 10772 mutex_enter(SD_MUTEX(un)); 10773 10774 goto done; 10775 } 10776 if (un->un_f_pkstats_enabled) { 10777 sd_set_pstats(un); 10778 SD_TRACE(SD_LOG_IO_PARTITION, un, 10779 "sd_ready_and_valid: un:0x%p pstats created and " 10780 "set\n", un); 10781 } 10782 mutex_enter(SD_MUTEX(un)); 10783 } 10784 10785 /* 10786 * If this device supports DOOR_LOCK command, try and send 10787 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10788 * if it fails. For a CD, however, it is an error 10789 */ 10790 if (un->un_f_doorlock_supported) { 10791 mutex_exit(SD_MUTEX(un)); 10792 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 10793 SD_PATH_DIRECT); 10794 10795 if ((status != 0) && ISCD(un)) { 10796 rval = SD_NOT_READY_VALID; 10797 mutex_enter(SD_MUTEX(un)); 10798 10799 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10800 10801 goto done; 10802 } else if (status != 0) 10803 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10804 mutex_enter(SD_MUTEX(un)); 10805 } 10806 10807 /* The state has changed, inform the media watch routines */ 10808 un->un_mediastate = DKIO_INSERTED; 10809 cv_broadcast(&un->un_state_cv); 10810 rval = SD_READY_VALID; 10811 10812 done: 10813 10814 /* 10815 * Initialize the capacity kstat value, if no media previously 10816 * (capacity kstat is 0) and a media has been inserted 10817 * (un_blockcount > 0). 10818 */ 10819 if (un->un_errstats != NULL) { 10820 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10821 if ((stp->sd_capacity.value.ui64 == 0) && 10822 (un->un_f_blockcount_is_valid == TRUE)) { 10823 stp->sd_capacity.value.ui64 = 10824 (uint64_t)((uint64_t)un->un_blockcount * 10825 un->un_sys_blocksize); 10826 } 10827 } 10828 10829 mutex_exit(SD_MUTEX(un)); 10830 return (rval); 10831 } 10832 10833 10834 /* 10835 * Function: sdmin 10836 * 10837 * Description: Routine to limit the size of a data transfer. Used in 10838 * conjunction with physio(9F). 10839 * 10840 * Arguments: bp - pointer to the indicated buf(9S) struct. 10841 * 10842 * Context: Kernel thread context. 10843 */ 10844 10845 static void 10846 sdmin(struct buf *bp) 10847 { 10848 struct sd_lun *un; 10849 int instance; 10850 10851 instance = SDUNIT(bp->b_edev); 10852 10853 un = ddi_get_soft_state(sd_state, instance); 10854 ASSERT(un != NULL); 10855 10856 /* 10857 * We depend on buf breakup to restrict 10858 * IO size if it is enabled. 10859 */ 10860 if (un->un_buf_breakup_supported) { 10861 return; 10862 } 10863 10864 if (bp->b_bcount > un->un_max_xfer_size) { 10865 bp->b_bcount = un->un_max_xfer_size; 10866 } 10867 } 10868 10869 10870 /* 10871 * Function: sdread 10872 * 10873 * Description: Driver's read(9e) entry point function. 10874 * 10875 * Arguments: dev - device number 10876 * uio - structure pointer describing where data is to be stored 10877 * in user's space 10878 * cred_p - user credential pointer 10879 * 10880 * Return Code: ENXIO 10881 * EIO 10882 * EINVAL 10883 * value returned by physio 10884 * 10885 * Context: Kernel thread context. 10886 */ 10887 /* ARGSUSED */ 10888 static int 10889 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10890 { 10891 struct sd_lun *un = NULL; 10892 int secmask; 10893 int err = 0; 10894 sd_ssc_t *ssc; 10895 10896 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10897 return (ENXIO); 10898 } 10899 10900 ASSERT(!mutex_owned(SD_MUTEX(un))); 10901 10902 10903 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10904 mutex_enter(SD_MUTEX(un)); 10905 /* 10906 * Because the call to sd_ready_and_valid will issue I/O we 10907 * must wait here if either the device is suspended or 10908 * if it's power level is changing. 10909 */ 10910 while ((un->un_state == SD_STATE_SUSPENDED) || 10911 (un->un_state == SD_STATE_PM_CHANGING)) { 10912 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10913 } 10914 un->un_ncmds_in_driver++; 10915 mutex_exit(SD_MUTEX(un)); 10916 10917 /* Initialize sd_ssc_t for internal uscsi commands */ 10918 ssc = sd_ssc_init(un); 10919 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10920 err = EIO; 10921 } else { 10922 err = 0; 10923 } 10924 sd_ssc_fini(ssc); 10925 10926 mutex_enter(SD_MUTEX(un)); 10927 un->un_ncmds_in_driver--; 10928 ASSERT(un->un_ncmds_in_driver >= 0); 10929 mutex_exit(SD_MUTEX(un)); 10930 if (err != 0) 10931 return (err); 10932 } 10933 10934 /* 10935 * Read requests are restricted to multiples of the system block size. 10936 */ 10937 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10938 !un->un_f_enable_rmw) 10939 secmask = un->un_tgt_blocksize - 1; 10940 else 10941 secmask = DEV_BSIZE - 1; 10942 10943 if (uio->uio_loffset & ((offset_t)(secmask))) { 10944 SD_ERROR(SD_LOG_READ_WRITE, un, 10945 "sdread: file offset not modulo %d\n", 10946 secmask + 1); 10947 err = EINVAL; 10948 } else if (uio->uio_iov->iov_len & (secmask)) { 10949 SD_ERROR(SD_LOG_READ_WRITE, un, 10950 "sdread: transfer length not modulo %d\n", 10951 secmask + 1); 10952 err = EINVAL; 10953 } else { 10954 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10955 } 10956 10957 return (err); 10958 } 10959 10960 10961 /* 10962 * Function: sdwrite 10963 * 10964 * Description: Driver's write(9e) entry point function. 10965 * 10966 * Arguments: dev - device number 10967 * uio - structure pointer describing where data is stored in 10968 * user's space 10969 * cred_p - user credential pointer 10970 * 10971 * Return Code: ENXIO 10972 * EIO 10973 * EINVAL 10974 * value returned by physio 10975 * 10976 * Context: Kernel thread context. 10977 */ 10978 /* ARGSUSED */ 10979 static int 10980 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10981 { 10982 struct sd_lun *un = NULL; 10983 int secmask; 10984 int err = 0; 10985 sd_ssc_t *ssc; 10986 10987 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10988 return (ENXIO); 10989 } 10990 10991 ASSERT(!mutex_owned(SD_MUTEX(un))); 10992 10993 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10994 mutex_enter(SD_MUTEX(un)); 10995 /* 10996 * Because the call to sd_ready_and_valid will issue I/O we 10997 * must wait here if either the device is suspended or 10998 * if it's power level is changing. 10999 */ 11000 while ((un->un_state == SD_STATE_SUSPENDED) || 11001 (un->un_state == SD_STATE_PM_CHANGING)) { 11002 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11003 } 11004 un->un_ncmds_in_driver++; 11005 mutex_exit(SD_MUTEX(un)); 11006 11007 /* Initialize sd_ssc_t for internal uscsi commands */ 11008 ssc = sd_ssc_init(un); 11009 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11010 err = EIO; 11011 } else { 11012 err = 0; 11013 } 11014 sd_ssc_fini(ssc); 11015 11016 mutex_enter(SD_MUTEX(un)); 11017 un->un_ncmds_in_driver--; 11018 ASSERT(un->un_ncmds_in_driver >= 0); 11019 mutex_exit(SD_MUTEX(un)); 11020 if (err != 0) 11021 return (err); 11022 } 11023 11024 /* 11025 * Write requests are restricted to multiples of the system block size. 11026 */ 11027 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11028 !un->un_f_enable_rmw) 11029 secmask = un->un_tgt_blocksize - 1; 11030 else 11031 secmask = DEV_BSIZE - 1; 11032 11033 if (uio->uio_loffset & ((offset_t)(secmask))) { 11034 SD_ERROR(SD_LOG_READ_WRITE, un, 11035 "sdwrite: file offset not modulo %d\n", 11036 secmask + 1); 11037 err = EINVAL; 11038 } else if (uio->uio_iov->iov_len & (secmask)) { 11039 SD_ERROR(SD_LOG_READ_WRITE, un, 11040 "sdwrite: transfer length not modulo %d\n", 11041 secmask + 1); 11042 err = EINVAL; 11043 } else { 11044 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11045 } 11046 11047 return (err); 11048 } 11049 11050 11051 /* 11052 * Function: sdaread 11053 * 11054 * Description: Driver's aread(9e) entry point function. 11055 * 11056 * Arguments: dev - device number 11057 * aio - structure pointer describing where data is to be stored 11058 * cred_p - user credential pointer 11059 * 11060 * Return Code: ENXIO 11061 * EIO 11062 * EINVAL 11063 * value returned by aphysio 11064 * 11065 * Context: Kernel thread context. 11066 */ 11067 /* ARGSUSED */ 11068 static int 11069 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11070 { 11071 struct sd_lun *un = NULL; 11072 struct uio *uio = aio->aio_uio; 11073 int secmask; 11074 int err = 0; 11075 sd_ssc_t *ssc; 11076 11077 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11078 return (ENXIO); 11079 } 11080 11081 ASSERT(!mutex_owned(SD_MUTEX(un))); 11082 11083 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11084 mutex_enter(SD_MUTEX(un)); 11085 /* 11086 * Because the call to sd_ready_and_valid will issue I/O we 11087 * must wait here if either the device is suspended or 11088 * if it's power level is changing. 11089 */ 11090 while ((un->un_state == SD_STATE_SUSPENDED) || 11091 (un->un_state == SD_STATE_PM_CHANGING)) { 11092 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11093 } 11094 un->un_ncmds_in_driver++; 11095 mutex_exit(SD_MUTEX(un)); 11096 11097 /* Initialize sd_ssc_t for internal uscsi commands */ 11098 ssc = sd_ssc_init(un); 11099 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11100 err = EIO; 11101 } else { 11102 err = 0; 11103 } 11104 sd_ssc_fini(ssc); 11105 11106 mutex_enter(SD_MUTEX(un)); 11107 un->un_ncmds_in_driver--; 11108 ASSERT(un->un_ncmds_in_driver >= 0); 11109 mutex_exit(SD_MUTEX(un)); 11110 if (err != 0) 11111 return (err); 11112 } 11113 11114 /* 11115 * Read requests are restricted to multiples of the system block size. 11116 */ 11117 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11118 !un->un_f_enable_rmw) 11119 secmask = un->un_tgt_blocksize - 1; 11120 else 11121 secmask = DEV_BSIZE - 1; 11122 11123 if (uio->uio_loffset & ((offset_t)(secmask))) { 11124 SD_ERROR(SD_LOG_READ_WRITE, un, 11125 "sdaread: file offset not modulo %d\n", 11126 secmask + 1); 11127 err = EINVAL; 11128 } else if (uio->uio_iov->iov_len & (secmask)) { 11129 SD_ERROR(SD_LOG_READ_WRITE, un, 11130 "sdaread: transfer length not modulo %d\n", 11131 secmask + 1); 11132 err = EINVAL; 11133 } else { 11134 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11135 } 11136 11137 return (err); 11138 } 11139 11140 11141 /* 11142 * Function: sdawrite 11143 * 11144 * Description: Driver's awrite(9e) entry point function. 11145 * 11146 * Arguments: dev - device number 11147 * aio - structure pointer describing where data is stored 11148 * cred_p - user credential pointer 11149 * 11150 * Return Code: ENXIO 11151 * EIO 11152 * EINVAL 11153 * value returned by aphysio 11154 * 11155 * Context: Kernel thread context. 11156 */ 11157 /* ARGSUSED */ 11158 static int 11159 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11160 { 11161 struct sd_lun *un = NULL; 11162 struct uio *uio = aio->aio_uio; 11163 int secmask; 11164 int err = 0; 11165 sd_ssc_t *ssc; 11166 11167 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11168 return (ENXIO); 11169 } 11170 11171 ASSERT(!mutex_owned(SD_MUTEX(un))); 11172 11173 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11174 mutex_enter(SD_MUTEX(un)); 11175 /* 11176 * Because the call to sd_ready_and_valid will issue I/O we 11177 * must wait here if either the device is suspended or 11178 * if it's power level is changing. 11179 */ 11180 while ((un->un_state == SD_STATE_SUSPENDED) || 11181 (un->un_state == SD_STATE_PM_CHANGING)) { 11182 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11183 } 11184 un->un_ncmds_in_driver++; 11185 mutex_exit(SD_MUTEX(un)); 11186 11187 /* Initialize sd_ssc_t for internal uscsi commands */ 11188 ssc = sd_ssc_init(un); 11189 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11190 err = EIO; 11191 } else { 11192 err = 0; 11193 } 11194 sd_ssc_fini(ssc); 11195 11196 mutex_enter(SD_MUTEX(un)); 11197 un->un_ncmds_in_driver--; 11198 ASSERT(un->un_ncmds_in_driver >= 0); 11199 mutex_exit(SD_MUTEX(un)); 11200 if (err != 0) 11201 return (err); 11202 } 11203 11204 /* 11205 * Write requests are restricted to multiples of the system block size. 11206 */ 11207 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11208 !un->un_f_enable_rmw) 11209 secmask = un->un_tgt_blocksize - 1; 11210 else 11211 secmask = DEV_BSIZE - 1; 11212 11213 if (uio->uio_loffset & ((offset_t)(secmask))) { 11214 SD_ERROR(SD_LOG_READ_WRITE, un, 11215 "sdawrite: file offset not modulo %d\n", 11216 secmask + 1); 11217 err = EINVAL; 11218 } else if (uio->uio_iov->iov_len & (secmask)) { 11219 SD_ERROR(SD_LOG_READ_WRITE, un, 11220 "sdawrite: transfer length not modulo %d\n", 11221 secmask + 1); 11222 err = EINVAL; 11223 } else { 11224 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11225 } 11226 11227 return (err); 11228 } 11229 11230 11231 11232 11233 11234 /* 11235 * Driver IO processing follows the following sequence: 11236 * 11237 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11238 * | | ^ 11239 * v v | 11240 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11241 * | | | | 11242 * v | | | 11243 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11244 * | | ^ ^ 11245 * v v | | 11246 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11247 * | | | | 11248 * +---+ | +------------+ +-------+ 11249 * | | | | 11250 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11251 * | v | | 11252 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11253 * | | ^ | 11254 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11255 * | v | | 11256 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11257 * | | ^ | 11258 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11259 * | v | | 11260 * | sd_checksum_iostart() sd_checksum_iodone() | 11261 * | | ^ | 11262 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11263 * | v | | 11264 * | sd_pm_iostart() sd_pm_iodone() | 11265 * | | ^ | 11266 * | | | | 11267 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11268 * | ^ 11269 * v | 11270 * sd_core_iostart() | 11271 * | | 11272 * | +------>(*destroypkt)() 11273 * +-> sd_start_cmds() <-+ | | 11274 * | | | v 11275 * | | | scsi_destroy_pkt(9F) 11276 * | | | 11277 * +->(*initpkt)() +- sdintr() 11278 * | | | | 11279 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11280 * | +-> scsi_setup_cdb(9F) | 11281 * | | 11282 * +--> scsi_transport(9F) | 11283 * | | 11284 * +----> SCSA ---->+ 11285 * 11286 * 11287 * This code is based upon the following presumptions: 11288 * 11289 * - iostart and iodone functions operate on buf(9S) structures. These 11290 * functions perform the necessary operations on the buf(9S) and pass 11291 * them along to the next function in the chain by using the macros 11292 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11293 * (for iodone side functions). 11294 * 11295 * - The iostart side functions may sleep. The iodone side functions 11296 * are called under interrupt context and may NOT sleep. Therefore 11297 * iodone side functions also may not call iostart side functions. 11298 * (NOTE: iostart side functions should NOT sleep for memory, as 11299 * this could result in deadlock.) 11300 * 11301 * - An iostart side function may call its corresponding iodone side 11302 * function directly (if necessary). 11303 * 11304 * - In the event of an error, an iostart side function can return a buf(9S) 11305 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11306 * b_error in the usual way of course). 11307 * 11308 * - The taskq mechanism may be used by the iodone side functions to dispatch 11309 * requests to the iostart side functions. The iostart side functions in 11310 * this case would be called under the context of a taskq thread, so it's 11311 * OK for them to block/sleep/spin in this case. 11312 * 11313 * - iostart side functions may allocate "shadow" buf(9S) structs and 11314 * pass them along to the next function in the chain. The corresponding 11315 * iodone side functions must coalesce the "shadow" bufs and return 11316 * the "original" buf to the next higher layer. 11317 * 11318 * - The b_private field of the buf(9S) struct holds a pointer to 11319 * an sd_xbuf struct, which contains information needed to 11320 * construct the scsi_pkt for the command. 11321 * 11322 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11323 * layer must acquire & release the SD_MUTEX(un) as needed. 11324 */ 11325 11326 11327 /* 11328 * Create taskq for all targets in the system. This is created at 11329 * _init(9E) and destroyed at _fini(9E). 11330 * 11331 * Note: here we set the minalloc to a reasonably high number to ensure that 11332 * we will have an adequate supply of task entries available at interrupt time. 11333 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11334 * sd_create_taskq(). Since we do not want to sleep for allocations at 11335 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11336 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11337 * requests any one instant in time. 11338 */ 11339 #define SD_TASKQ_NUMTHREADS 8 11340 #define SD_TASKQ_MINALLOC 256 11341 #define SD_TASKQ_MAXALLOC 256 11342 11343 static taskq_t *sd_tq = NULL; 11344 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11345 11346 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11347 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11348 11349 /* 11350 * The following task queue is being created for the write part of 11351 * read-modify-write of non-512 block size devices. 11352 * Limit the number of threads to 1 for now. This number has been chosen 11353 * considering the fact that it applies only to dvd ram drives/MO drives 11354 * currently. Performance for which is not main criteria at this stage. 11355 * Note: It needs to be explored if we can use a single taskq in future 11356 */ 11357 #define SD_WMR_TASKQ_NUMTHREADS 1 11358 static taskq_t *sd_wmr_tq = NULL; 11359 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11360 11361 /* 11362 * Function: sd_taskq_create 11363 * 11364 * Description: Create taskq thread(s) and preallocate task entries 11365 * 11366 * Return Code: Returns a pointer to the allocated taskq_t. 11367 * 11368 * Context: Can sleep. Requires blockable context. 11369 * 11370 * Notes: - The taskq() facility currently is NOT part of the DDI. 11371 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11372 * - taskq_create() will block for memory, also it will panic 11373 * if it cannot create the requested number of threads. 11374 * - Currently taskq_create() creates threads that cannot be 11375 * swapped. 11376 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11377 * supply of taskq entries at interrupt time (ie, so that we 11378 * do not have to sleep for memory) 11379 */ 11380 11381 static void 11382 sd_taskq_create(void) 11383 { 11384 char taskq_name[TASKQ_NAMELEN]; 11385 11386 ASSERT(sd_tq == NULL); 11387 ASSERT(sd_wmr_tq == NULL); 11388 11389 (void) snprintf(taskq_name, sizeof (taskq_name), 11390 "%s_drv_taskq", sd_label); 11391 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11392 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11393 TASKQ_PREPOPULATE)); 11394 11395 (void) snprintf(taskq_name, sizeof (taskq_name), 11396 "%s_rmw_taskq", sd_label); 11397 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11398 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11399 TASKQ_PREPOPULATE)); 11400 } 11401 11402 11403 /* 11404 * Function: sd_taskq_delete 11405 * 11406 * Description: Complementary cleanup routine for sd_taskq_create(). 11407 * 11408 * Context: Kernel thread context. 11409 */ 11410 11411 static void 11412 sd_taskq_delete(void) 11413 { 11414 ASSERT(sd_tq != NULL); 11415 ASSERT(sd_wmr_tq != NULL); 11416 taskq_destroy(sd_tq); 11417 taskq_destroy(sd_wmr_tq); 11418 sd_tq = NULL; 11419 sd_wmr_tq = NULL; 11420 } 11421 11422 11423 /* 11424 * Function: sdstrategy 11425 * 11426 * Description: Driver's strategy (9E) entry point function. 11427 * 11428 * Arguments: bp - pointer to buf(9S) 11429 * 11430 * Return Code: Always returns zero 11431 * 11432 * Context: Kernel thread context. 11433 */ 11434 11435 static int 11436 sdstrategy(struct buf *bp) 11437 { 11438 struct sd_lun *un; 11439 11440 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11441 if (un == NULL) { 11442 bioerror(bp, EIO); 11443 bp->b_resid = bp->b_bcount; 11444 biodone(bp); 11445 return (0); 11446 } 11447 11448 /* As was done in the past, fail new cmds. if state is dumping. */ 11449 if (un->un_state == SD_STATE_DUMPING) { 11450 bioerror(bp, ENXIO); 11451 bp->b_resid = bp->b_bcount; 11452 biodone(bp); 11453 return (0); 11454 } 11455 11456 ASSERT(!mutex_owned(SD_MUTEX(un))); 11457 11458 /* 11459 * Commands may sneak in while we released the mutex in 11460 * DDI_SUSPEND, we should block new commands. However, old 11461 * commands that are still in the driver at this point should 11462 * still be allowed to drain. 11463 */ 11464 mutex_enter(SD_MUTEX(un)); 11465 /* 11466 * Must wait here if either the device is suspended or 11467 * if it's power level is changing. 11468 */ 11469 while ((un->un_state == SD_STATE_SUSPENDED) || 11470 (un->un_state == SD_STATE_PM_CHANGING)) { 11471 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11472 } 11473 11474 un->un_ncmds_in_driver++; 11475 11476 /* 11477 * atapi: Since we are running the CD for now in PIO mode we need to 11478 * call bp_mapin here to avoid bp_mapin called interrupt context under 11479 * the HBA's init_pkt routine. 11480 */ 11481 if (un->un_f_cfg_is_atapi == TRUE) { 11482 mutex_exit(SD_MUTEX(un)); 11483 bp_mapin(bp); 11484 mutex_enter(SD_MUTEX(un)); 11485 } 11486 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11487 un->un_ncmds_in_driver); 11488 11489 if (bp->b_flags & B_WRITE) 11490 un->un_f_sync_cache_required = TRUE; 11491 11492 mutex_exit(SD_MUTEX(un)); 11493 11494 /* 11495 * This will (eventually) allocate the sd_xbuf area and 11496 * call sd_xbuf_strategy(). We just want to return the 11497 * result of ddi_xbuf_qstrategy so that we have an opt- 11498 * imized tail call which saves us a stack frame. 11499 */ 11500 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11501 } 11502 11503 11504 /* 11505 * Function: sd_xbuf_strategy 11506 * 11507 * Description: Function for initiating IO operations via the 11508 * ddi_xbuf_qstrategy() mechanism. 11509 * 11510 * Context: Kernel thread context. 11511 */ 11512 11513 static void 11514 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11515 { 11516 struct sd_lun *un = arg; 11517 11518 ASSERT(bp != NULL); 11519 ASSERT(xp != NULL); 11520 ASSERT(un != NULL); 11521 ASSERT(!mutex_owned(SD_MUTEX(un))); 11522 11523 /* 11524 * Initialize the fields in the xbuf and save a pointer to the 11525 * xbuf in bp->b_private. 11526 */ 11527 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11528 11529 /* Send the buf down the iostart chain */ 11530 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11531 } 11532 11533 11534 /* 11535 * Function: sd_xbuf_init 11536 * 11537 * Description: Prepare the given sd_xbuf struct for use. 11538 * 11539 * Arguments: un - ptr to softstate 11540 * bp - ptr to associated buf(9S) 11541 * xp - ptr to associated sd_xbuf 11542 * chain_type - IO chain type to use: 11543 * SD_CHAIN_NULL 11544 * SD_CHAIN_BUFIO 11545 * SD_CHAIN_USCSI 11546 * SD_CHAIN_DIRECT 11547 * SD_CHAIN_DIRECT_PRIORITY 11548 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11549 * initialization; may be NULL if none. 11550 * 11551 * Context: Kernel thread context 11552 */ 11553 11554 static void 11555 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11556 uchar_t chain_type, void *pktinfop) 11557 { 11558 int index; 11559 11560 ASSERT(un != NULL); 11561 ASSERT(bp != NULL); 11562 ASSERT(xp != NULL); 11563 11564 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11565 bp, chain_type); 11566 11567 xp->xb_un = un; 11568 xp->xb_pktp = NULL; 11569 xp->xb_pktinfo = pktinfop; 11570 xp->xb_private = bp->b_private; 11571 xp->xb_blkno = (daddr_t)bp->b_blkno; 11572 11573 /* 11574 * Set up the iostart and iodone chain indexes in the xbuf, based 11575 * upon the specified chain type to use. 11576 */ 11577 switch (chain_type) { 11578 case SD_CHAIN_NULL: 11579 /* 11580 * Fall thru to just use the values for the buf type, even 11581 * tho for the NULL chain these values will never be used. 11582 */ 11583 /* FALLTHRU */ 11584 case SD_CHAIN_BUFIO: 11585 index = un->un_buf_chain_type; 11586 if ((!un->un_f_has_removable_media) && 11587 (un->un_tgt_blocksize != 0) && 11588 (un->un_tgt_blocksize != DEV_BSIZE || 11589 un->un_f_enable_rmw)) { 11590 int secmask = 0, blknomask = 0; 11591 if (un->un_f_enable_rmw) { 11592 blknomask = 11593 (un->un_phy_blocksize / DEV_BSIZE) - 1; 11594 secmask = un->un_phy_blocksize - 1; 11595 } else { 11596 blknomask = 11597 (un->un_tgt_blocksize / DEV_BSIZE) - 1; 11598 secmask = un->un_tgt_blocksize - 1; 11599 } 11600 11601 if ((bp->b_lblkno & (blknomask)) || 11602 (bp->b_bcount & (secmask))) { 11603 if ((un->un_f_rmw_type != 11604 SD_RMW_TYPE_RETURN_ERROR) || 11605 un->un_f_enable_rmw) { 11606 if (un->un_f_pm_is_enabled == FALSE) 11607 index = 11608 SD_CHAIN_INFO_MSS_DSK_NO_PM; 11609 else 11610 index = 11611 SD_CHAIN_INFO_MSS_DISK; 11612 } 11613 } 11614 } 11615 break; 11616 case SD_CHAIN_USCSI: 11617 index = un->un_uscsi_chain_type; 11618 break; 11619 case SD_CHAIN_DIRECT: 11620 index = un->un_direct_chain_type; 11621 break; 11622 case SD_CHAIN_DIRECT_PRIORITY: 11623 index = un->un_priority_chain_type; 11624 break; 11625 default: 11626 /* We're really broken if we ever get here... */ 11627 panic("sd_xbuf_init: illegal chain type!"); 11628 /*NOTREACHED*/ 11629 } 11630 11631 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11632 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11633 11634 /* 11635 * It might be a bit easier to simply bzero the entire xbuf above, 11636 * but it turns out that since we init a fair number of members anyway, 11637 * we save a fair number cycles by doing explicit assignment of zero. 11638 */ 11639 xp->xb_pkt_flags = 0; 11640 xp->xb_dma_resid = 0; 11641 xp->xb_retry_count = 0; 11642 xp->xb_victim_retry_count = 0; 11643 xp->xb_ua_retry_count = 0; 11644 xp->xb_nr_retry_count = 0; 11645 xp->xb_sense_bp = NULL; 11646 xp->xb_sense_status = 0; 11647 xp->xb_sense_state = 0; 11648 xp->xb_sense_resid = 0; 11649 xp->xb_ena = 0; 11650 11651 bp->b_private = xp; 11652 bp->b_flags &= ~(B_DONE | B_ERROR); 11653 bp->b_resid = 0; 11654 bp->av_forw = NULL; 11655 bp->av_back = NULL; 11656 bioerror(bp, 0); 11657 11658 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11659 } 11660 11661 11662 /* 11663 * Function: sd_uscsi_strategy 11664 * 11665 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11666 * 11667 * Arguments: bp - buf struct ptr 11668 * 11669 * Return Code: Always returns 0 11670 * 11671 * Context: Kernel thread context 11672 */ 11673 11674 static int 11675 sd_uscsi_strategy(struct buf *bp) 11676 { 11677 struct sd_lun *un; 11678 struct sd_uscsi_info *uip; 11679 struct sd_xbuf *xp; 11680 uchar_t chain_type; 11681 uchar_t cmd; 11682 11683 ASSERT(bp != NULL); 11684 11685 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11686 if (un == NULL) { 11687 bioerror(bp, EIO); 11688 bp->b_resid = bp->b_bcount; 11689 biodone(bp); 11690 return (0); 11691 } 11692 11693 ASSERT(!mutex_owned(SD_MUTEX(un))); 11694 11695 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11696 11697 /* 11698 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11699 */ 11700 ASSERT(bp->b_private != NULL); 11701 uip = (struct sd_uscsi_info *)bp->b_private; 11702 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0]; 11703 11704 mutex_enter(SD_MUTEX(un)); 11705 /* 11706 * atapi: Since we are running the CD for now in PIO mode we need to 11707 * call bp_mapin here to avoid bp_mapin called interrupt context under 11708 * the HBA's init_pkt routine. 11709 */ 11710 if (un->un_f_cfg_is_atapi == TRUE) { 11711 mutex_exit(SD_MUTEX(un)); 11712 bp_mapin(bp); 11713 mutex_enter(SD_MUTEX(un)); 11714 } 11715 un->un_ncmds_in_driver++; 11716 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11717 un->un_ncmds_in_driver); 11718 11719 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) && 11720 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1)) 11721 un->un_f_sync_cache_required = TRUE; 11722 11723 mutex_exit(SD_MUTEX(un)); 11724 11725 switch (uip->ui_flags) { 11726 case SD_PATH_DIRECT: 11727 chain_type = SD_CHAIN_DIRECT; 11728 break; 11729 case SD_PATH_DIRECT_PRIORITY: 11730 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11731 break; 11732 default: 11733 chain_type = SD_CHAIN_USCSI; 11734 break; 11735 } 11736 11737 /* 11738 * We may allocate extra buf for external USCSI commands. If the 11739 * application asks for bigger than 20-byte sense data via USCSI, 11740 * SCSA layer will allocate 252 bytes sense buf for that command. 11741 */ 11742 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 11743 SENSE_LENGTH) { 11744 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 11745 MAX_SENSE_LENGTH, KM_SLEEP); 11746 } else { 11747 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 11748 } 11749 11750 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11751 11752 /* Use the index obtained within xbuf_init */ 11753 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11754 11755 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11756 11757 return (0); 11758 } 11759 11760 /* 11761 * Function: sd_send_scsi_cmd 11762 * 11763 * Description: Runs a USCSI command for user (when called thru sdioctl), 11764 * or for the driver 11765 * 11766 * Arguments: dev - the dev_t for the device 11767 * incmd - ptr to a valid uscsi_cmd struct 11768 * flag - bit flag, indicating open settings, 32/64 bit type 11769 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11770 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11771 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11772 * to use the USCSI "direct" chain and bypass the normal 11773 * command waitq. 11774 * 11775 * Return Code: 0 - successful completion of the given command 11776 * EIO - scsi_uscsi_handle_command() failed 11777 * ENXIO - soft state not found for specified dev 11778 * EINVAL 11779 * EFAULT - copyin/copyout error 11780 * return code of scsi_uscsi_handle_command(): 11781 * EIO 11782 * ENXIO 11783 * EACCES 11784 * 11785 * Context: Waits for command to complete. Can sleep. 11786 */ 11787 11788 static int 11789 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 11790 enum uio_seg dataspace, int path_flag) 11791 { 11792 struct sd_lun *un; 11793 sd_ssc_t *ssc; 11794 int rval; 11795 11796 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11797 if (un == NULL) { 11798 return (ENXIO); 11799 } 11800 11801 /* 11802 * Using sd_ssc_send to handle uscsi cmd 11803 */ 11804 ssc = sd_ssc_init(un); 11805 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag); 11806 sd_ssc_fini(ssc); 11807 11808 return (rval); 11809 } 11810 11811 /* 11812 * Function: sd_ssc_init 11813 * 11814 * Description: Uscsi end-user call this function to initialize necessary 11815 * fields, such as uscsi_cmd and sd_uscsi_info struct. 11816 * 11817 * The return value of sd_send_scsi_cmd will be treated as a 11818 * fault in various conditions. Even it is not Zero, some 11819 * callers may ignore the return value. That is to say, we can 11820 * not make an accurate assessment in sdintr, since if a 11821 * command is failed in sdintr it does not mean the caller of 11822 * sd_send_scsi_cmd will treat it as a real failure. 11823 * 11824 * To avoid printing too many error logs for a failed uscsi 11825 * packet that the caller may not treat it as a failure, the 11826 * sd will keep silent for handling all uscsi commands. 11827 * 11828 * During detach->attach and attach-open, for some types of 11829 * problems, the driver should be providing information about 11830 * the problem encountered. Device use USCSI_SILENT, which 11831 * suppresses all driver information. The result is that no 11832 * information about the problem is available. Being 11833 * completely silent during this time is inappropriate. The 11834 * driver needs a more selective filter than USCSI_SILENT, so 11835 * that information related to faults is provided. 11836 * 11837 * To make the accurate accessment, the caller of 11838 * sd_send_scsi_USCSI_CMD should take the ownership and 11839 * get necessary information to print error messages. 11840 * 11841 * If we want to print necessary info of uscsi command, we need to 11842 * keep the uscsi_cmd and sd_uscsi_info till we can make the 11843 * assessment. We use sd_ssc_init to alloc necessary 11844 * structs for sending an uscsi command and we are also 11845 * responsible for free the memory by calling 11846 * sd_ssc_fini. 11847 * 11848 * The calling secquences will look like: 11849 * sd_ssc_init-> 11850 * 11851 * ... 11852 * 11853 * sd_send_scsi_USCSI_CMD-> 11854 * sd_ssc_send-> - - - sdintr 11855 * ... 11856 * 11857 * if we think the return value should be treated as a 11858 * failure, we make the accessment here and print out 11859 * necessary by retrieving uscsi_cmd and sd_uscsi_info' 11860 * 11861 * ... 11862 * 11863 * sd_ssc_fini 11864 * 11865 * 11866 * Arguments: un - pointer to driver soft state (unit) structure for this 11867 * target. 11868 * 11869 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains 11870 * uscsi_cmd and sd_uscsi_info. 11871 * NULL - if can not alloc memory for sd_ssc_t struct 11872 * 11873 * Context: Kernel Thread. 11874 */ 11875 static sd_ssc_t * 11876 sd_ssc_init(struct sd_lun *un) 11877 { 11878 sd_ssc_t *ssc; 11879 struct uscsi_cmd *ucmdp; 11880 struct sd_uscsi_info *uip; 11881 11882 ASSERT(un != NULL); 11883 ASSERT(!mutex_owned(SD_MUTEX(un))); 11884 11885 /* 11886 * Allocate sd_ssc_t structure 11887 */ 11888 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP); 11889 11890 /* 11891 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine 11892 */ 11893 ucmdp = scsi_uscsi_alloc(); 11894 11895 /* 11896 * Allocate sd_uscsi_info structure 11897 */ 11898 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11899 11900 ssc->ssc_uscsi_cmd = ucmdp; 11901 ssc->ssc_uscsi_info = uip; 11902 ssc->ssc_un = un; 11903 11904 return (ssc); 11905 } 11906 11907 /* 11908 * Function: sd_ssc_fini 11909 * 11910 * Description: To free sd_ssc_t and it's hanging off 11911 * 11912 * Arguments: ssc - struct pointer of sd_ssc_t. 11913 */ 11914 static void 11915 sd_ssc_fini(sd_ssc_t *ssc) 11916 { 11917 scsi_uscsi_free(ssc->ssc_uscsi_cmd); 11918 11919 if (ssc->ssc_uscsi_info != NULL) { 11920 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info)); 11921 ssc->ssc_uscsi_info = NULL; 11922 } 11923 11924 kmem_free(ssc, sizeof (sd_ssc_t)); 11925 ssc = NULL; 11926 } 11927 11928 /* 11929 * Function: sd_ssc_send 11930 * 11931 * Description: Runs a USCSI command for user when called through sdioctl, 11932 * or for the driver. 11933 * 11934 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11935 * sd_uscsi_info in. 11936 * incmd - ptr to a valid uscsi_cmd struct 11937 * flag - bit flag, indicating open settings, 32/64 bit type 11938 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11939 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11940 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11941 * to use the USCSI "direct" chain and bypass the normal 11942 * command waitq. 11943 * 11944 * Return Code: 0 - successful completion of the given command 11945 * EIO - scsi_uscsi_handle_command() failed 11946 * ENXIO - soft state not found for specified dev 11947 * ECANCELED - command cancelled due to low power 11948 * EINVAL 11949 * EFAULT - copyin/copyout error 11950 * return code of scsi_uscsi_handle_command(): 11951 * EIO 11952 * ENXIO 11953 * EACCES 11954 * 11955 * Context: Kernel Thread; 11956 * Waits for command to complete. Can sleep. 11957 */ 11958 static int 11959 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag, 11960 enum uio_seg dataspace, int path_flag) 11961 { 11962 struct sd_uscsi_info *uip; 11963 struct uscsi_cmd *uscmd; 11964 struct sd_lun *un; 11965 dev_t dev; 11966 11967 int format = 0; 11968 int rval; 11969 11970 ASSERT(ssc != NULL); 11971 un = ssc->ssc_un; 11972 ASSERT(un != NULL); 11973 uscmd = ssc->ssc_uscsi_cmd; 11974 ASSERT(uscmd != NULL); 11975 ASSERT(!mutex_owned(SD_MUTEX(un))); 11976 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11977 /* 11978 * If enter here, it indicates that the previous uscsi 11979 * command has not been processed by sd_ssc_assessment. 11980 * This is violating our rules of FMA telemetry processing. 11981 * We should print out this message and the last undisposed 11982 * uscsi command. 11983 */ 11984 if (uscmd->uscsi_cdb != NULL) { 11985 SD_INFO(SD_LOG_SDTEST, un, 11986 "sd_ssc_send is missing the alternative " 11987 "sd_ssc_assessment when running command 0x%x.\n", 11988 uscmd->uscsi_cdb[0]); 11989 } 11990 /* 11991 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be 11992 * the initial status. 11993 */ 11994 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11995 } 11996 11997 /* 11998 * We need to make sure sd_ssc_send will have sd_ssc_assessment 11999 * followed to avoid missing FMA telemetries. 12000 */ 12001 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT; 12002 12003 /* 12004 * if USCSI_PMFAILFAST is set and un is in low power, fail the 12005 * command immediately. 12006 */ 12007 mutex_enter(SD_MUTEX(un)); 12008 mutex_enter(&un->un_pm_mutex); 12009 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) && 12010 SD_DEVICE_IS_IN_LOW_POWER(un)) { 12011 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:" 12012 "un:0x%p is in low power\n", un); 12013 mutex_exit(&un->un_pm_mutex); 12014 mutex_exit(SD_MUTEX(un)); 12015 return (ECANCELED); 12016 } 12017 mutex_exit(&un->un_pm_mutex); 12018 mutex_exit(SD_MUTEX(un)); 12019 12020 #ifdef SDDEBUG 12021 switch (dataspace) { 12022 case UIO_USERSPACE: 12023 SD_TRACE(SD_LOG_IO, un, 12024 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un); 12025 break; 12026 case UIO_SYSSPACE: 12027 SD_TRACE(SD_LOG_IO, un, 12028 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un); 12029 break; 12030 default: 12031 SD_TRACE(SD_LOG_IO, un, 12032 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un); 12033 break; 12034 } 12035 #endif 12036 12037 rval = scsi_uscsi_copyin((intptr_t)incmd, flag, 12038 SD_ADDRESS(un), &uscmd); 12039 if (rval != 0) { 12040 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 12041 "scsi_uscsi_alloc_and_copyin failed\n", un); 12042 return (rval); 12043 } 12044 12045 if ((uscmd->uscsi_cdb != NULL) && 12046 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 12047 mutex_enter(SD_MUTEX(un)); 12048 un->un_f_format_in_progress = TRUE; 12049 mutex_exit(SD_MUTEX(un)); 12050 format = 1; 12051 } 12052 12053 /* 12054 * Allocate an sd_uscsi_info struct and fill it with the info 12055 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12056 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12057 * since we allocate the buf here in this function, we do not 12058 * need to preserve the prior contents of b_private. 12059 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12060 */ 12061 uip = ssc->ssc_uscsi_info; 12062 uip->ui_flags = path_flag; 12063 uip->ui_cmdp = uscmd; 12064 12065 /* 12066 * Commands sent with priority are intended for error recovery 12067 * situations, and do not have retries performed. 12068 */ 12069 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12070 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12071 } 12072 uscmd->uscsi_flags &= ~USCSI_NOINTR; 12073 12074 dev = SD_GET_DEV(un); 12075 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 12076 sd_uscsi_strategy, NULL, uip); 12077 12078 /* 12079 * mark ssc_flags right after handle_cmd to make sure 12080 * the uscsi has been sent 12081 */ 12082 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED; 12083 12084 #ifdef SDDEBUG 12085 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12086 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12087 uscmd->uscsi_status, uscmd->uscsi_resid); 12088 if (uscmd->uscsi_bufaddr != NULL) { 12089 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12090 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12091 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12092 if (dataspace == UIO_SYSSPACE) { 12093 SD_DUMP_MEMORY(un, SD_LOG_IO, 12094 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12095 uscmd->uscsi_buflen, SD_LOG_HEX); 12096 } 12097 } 12098 #endif 12099 12100 if (format == 1) { 12101 mutex_enter(SD_MUTEX(un)); 12102 un->un_f_format_in_progress = FALSE; 12103 mutex_exit(SD_MUTEX(un)); 12104 } 12105 12106 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd); 12107 12108 return (rval); 12109 } 12110 12111 /* 12112 * Function: sd_ssc_print 12113 * 12114 * Description: Print information available to the console. 12115 * 12116 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12117 * sd_uscsi_info in. 12118 * sd_severity - log level. 12119 * Context: Kernel thread or interrupt context. 12120 */ 12121 static void 12122 sd_ssc_print(sd_ssc_t *ssc, int sd_severity) 12123 { 12124 struct uscsi_cmd *ucmdp; 12125 struct scsi_device *devp; 12126 dev_info_t *devinfo; 12127 uchar_t *sensep; 12128 int senlen; 12129 union scsi_cdb *cdbp; 12130 uchar_t com; 12131 extern struct scsi_key_strings scsi_cmds[]; 12132 12133 ASSERT(ssc != NULL); 12134 ASSERT(ssc->ssc_un != NULL); 12135 12136 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT) 12137 return; 12138 ucmdp = ssc->ssc_uscsi_cmd; 12139 devp = SD_SCSI_DEVP(ssc->ssc_un); 12140 devinfo = SD_DEVINFO(ssc->ssc_un); 12141 ASSERT(ucmdp != NULL); 12142 ASSERT(devp != NULL); 12143 ASSERT(devinfo != NULL); 12144 sensep = (uint8_t *)ucmdp->uscsi_rqbuf; 12145 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid; 12146 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb; 12147 12148 /* In certain case (like DOORLOCK), the cdb could be NULL. */ 12149 if (cdbp == NULL) 12150 return; 12151 /* We don't print log if no sense data available. */ 12152 if (senlen == 0) 12153 sensep = NULL; 12154 com = cdbp->scc_cmd; 12155 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com, 12156 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL); 12157 } 12158 12159 /* 12160 * Function: sd_ssc_assessment 12161 * 12162 * Description: We use this function to make an assessment at the point 12163 * where SD driver may encounter a potential error. 12164 * 12165 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12166 * sd_uscsi_info in. 12167 * tp_assess - a hint of strategy for ereport posting. 12168 * Possible values of tp_assess include: 12169 * SD_FMT_IGNORE - we don't post any ereport because we're 12170 * sure that it is ok to ignore the underlying problems. 12171 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now 12172 * but it might be not correct to ignore the underlying hardware 12173 * error. 12174 * SD_FMT_STATUS_CHECK - we will post an ereport with the 12175 * payload driver-assessment of value "fail" or 12176 * "fatal"(depending on what information we have here). This 12177 * assessment value is usually set when SD driver think there 12178 * is a potential error occurred(Typically, when return value 12179 * of the SCSI command is EIO). 12180 * SD_FMT_STANDARD - we will post an ereport with the payload 12181 * driver-assessment of value "info". This assessment value is 12182 * set when the SCSI command returned successfully and with 12183 * sense data sent back. 12184 * 12185 * Context: Kernel thread. 12186 */ 12187 static void 12188 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess) 12189 { 12190 int senlen = 0; 12191 struct uscsi_cmd *ucmdp = NULL; 12192 struct sd_lun *un; 12193 12194 ASSERT(ssc != NULL); 12195 un = ssc->ssc_un; 12196 ASSERT(un != NULL); 12197 ucmdp = ssc->ssc_uscsi_cmd; 12198 ASSERT(ucmdp != NULL); 12199 12200 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 12201 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT; 12202 } else { 12203 /* 12204 * If enter here, it indicates that we have a wrong 12205 * calling sequence of sd_ssc_send and sd_ssc_assessment, 12206 * both of which should be called in a pair in case of 12207 * loss of FMA telemetries. 12208 */ 12209 if (ucmdp->uscsi_cdb != NULL) { 12210 SD_INFO(SD_LOG_SDTEST, un, 12211 "sd_ssc_assessment is missing the " 12212 "alternative sd_ssc_send when running 0x%x, " 12213 "or there are superfluous sd_ssc_assessment for " 12214 "the same sd_ssc_send.\n", 12215 ucmdp->uscsi_cdb[0]); 12216 } 12217 /* 12218 * Set the ssc_flags to the initial value to avoid passing 12219 * down dirty flags to the following sd_ssc_send function. 12220 */ 12221 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12222 return; 12223 } 12224 12225 /* 12226 * Only handle an issued command which is waiting for assessment. 12227 * A command which is not issued will not have 12228 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here. 12229 */ 12230 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) { 12231 sd_ssc_print(ssc, SCSI_ERR_INFO); 12232 return; 12233 } else { 12234 /* 12235 * For an issued command, we should clear this flag in 12236 * order to make the sd_ssc_t structure be used off 12237 * multiple uscsi commands. 12238 */ 12239 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED; 12240 } 12241 12242 /* 12243 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set) 12244 * commands here. And we should clear the ssc_flags before return. 12245 */ 12246 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) { 12247 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12248 return; 12249 } 12250 12251 switch (tp_assess) { 12252 case SD_FMT_IGNORE: 12253 case SD_FMT_IGNORE_COMPROMISE: 12254 break; 12255 case SD_FMT_STATUS_CHECK: 12256 /* 12257 * For a failed command(including the succeeded command 12258 * with invalid data sent back). 12259 */ 12260 sd_ssc_post(ssc, SD_FM_DRV_FATAL); 12261 break; 12262 case SD_FMT_STANDARD: 12263 /* 12264 * Always for the succeeded commands probably with sense 12265 * data sent back. 12266 * Limitation: 12267 * We can only handle a succeeded command with sense 12268 * data sent back when auto-request-sense is enabled. 12269 */ 12270 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen - 12271 ssc->ssc_uscsi_cmd->uscsi_rqresid; 12272 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) && 12273 (un->un_f_arq_enabled == TRUE) && 12274 senlen > 0 && 12275 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) { 12276 sd_ssc_post(ssc, SD_FM_DRV_NOTICE); 12277 } 12278 break; 12279 default: 12280 /* 12281 * Should not have other type of assessment. 12282 */ 12283 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 12284 "sd_ssc_assessment got wrong " 12285 "sd_type_assessment %d.\n", tp_assess); 12286 break; 12287 } 12288 /* 12289 * Clear up the ssc_flags before return. 12290 */ 12291 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12292 } 12293 12294 /* 12295 * Function: sd_ssc_post 12296 * 12297 * Description: 1. read the driver property to get fm-scsi-log flag. 12298 * 2. print log if fm_log_capable is non-zero. 12299 * 3. call sd_ssc_ereport_post to post ereport if possible. 12300 * 12301 * Context: May be called from kernel thread or interrupt context. 12302 */ 12303 static void 12304 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess) 12305 { 12306 struct sd_lun *un; 12307 int sd_severity; 12308 12309 ASSERT(ssc != NULL); 12310 un = ssc->ssc_un; 12311 ASSERT(un != NULL); 12312 12313 /* 12314 * We may enter here from sd_ssc_assessment(for USCSI command) or 12315 * by directly called from sdintr context. 12316 * We don't handle a non-disk drive(CD-ROM, removable media). 12317 * Clear the ssc_flags before return in case we've set 12318 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk 12319 * driver. 12320 */ 12321 if (ISCD(un) || un->un_f_has_removable_media) { 12322 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12323 return; 12324 } 12325 12326 switch (sd_assess) { 12327 case SD_FM_DRV_FATAL: 12328 sd_severity = SCSI_ERR_FATAL; 12329 break; 12330 case SD_FM_DRV_RECOVERY: 12331 sd_severity = SCSI_ERR_RECOVERED; 12332 break; 12333 case SD_FM_DRV_RETRY: 12334 sd_severity = SCSI_ERR_RETRYABLE; 12335 break; 12336 case SD_FM_DRV_NOTICE: 12337 sd_severity = SCSI_ERR_INFO; 12338 break; 12339 default: 12340 sd_severity = SCSI_ERR_UNKNOWN; 12341 } 12342 /* print log */ 12343 sd_ssc_print(ssc, sd_severity); 12344 12345 /* always post ereport */ 12346 sd_ssc_ereport_post(ssc, sd_assess); 12347 } 12348 12349 /* 12350 * Function: sd_ssc_set_info 12351 * 12352 * Description: Mark ssc_flags and set ssc_info which would be the 12353 * payload of uderr ereport. This function will cause 12354 * sd_ssc_ereport_post to post uderr ereport only. 12355 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI), 12356 * the function will also call SD_ERROR or scsi_log for a 12357 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device. 12358 * 12359 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12360 * sd_uscsi_info in. 12361 * ssc_flags - indicate the sub-category of a uderr. 12362 * comp - this argument is meaningful only when 12363 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible 12364 * values include: 12365 * > 0, SD_ERROR is used with comp as the driver logging 12366 * component; 12367 * = 0, scsi-log is used to log error telemetries; 12368 * < 0, no log available for this telemetry. 12369 * 12370 * Context: Kernel thread or interrupt context 12371 */ 12372 static void 12373 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...) 12374 { 12375 va_list ap; 12376 12377 ASSERT(ssc != NULL); 12378 ASSERT(ssc->ssc_un != NULL); 12379 12380 ssc->ssc_flags |= ssc_flags; 12381 va_start(ap, fmt); 12382 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap); 12383 va_end(ap); 12384 12385 /* 12386 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command 12387 * with invalid data sent back. For non-uscsi command, the 12388 * following code will be bypassed. 12389 */ 12390 if (ssc_flags & SSC_FLAGS_INVALID_DATA) { 12391 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) { 12392 /* 12393 * If the error belong to certain component and we 12394 * do not want it to show up on the console, we 12395 * will use SD_ERROR, otherwise scsi_log is 12396 * preferred. 12397 */ 12398 if (comp > 0) { 12399 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info); 12400 } else if (comp == 0) { 12401 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label, 12402 CE_WARN, ssc->ssc_info); 12403 } 12404 } 12405 } 12406 } 12407 12408 /* 12409 * Function: sd_buf_iodone 12410 * 12411 * Description: Frees the sd_xbuf & returns the buf to its originator. 12412 * 12413 * Context: May be called from interrupt context. 12414 */ 12415 /* ARGSUSED */ 12416 static void 12417 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12418 { 12419 struct sd_xbuf *xp; 12420 12421 ASSERT(un != NULL); 12422 ASSERT(bp != NULL); 12423 ASSERT(!mutex_owned(SD_MUTEX(un))); 12424 12425 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12426 12427 xp = SD_GET_XBUF(bp); 12428 ASSERT(xp != NULL); 12429 12430 /* xbuf is gone after this */ 12431 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) { 12432 mutex_enter(SD_MUTEX(un)); 12433 12434 /* 12435 * Grab time when the cmd completed. 12436 * This is used for determining if the system has been 12437 * idle long enough to make it idle to the PM framework. 12438 * This is for lowering the overhead, and therefore improving 12439 * performance per I/O operation. 12440 */ 12441 un->un_pm_idle_time = ddi_get_time(); 12442 12443 un->un_ncmds_in_driver--; 12444 ASSERT(un->un_ncmds_in_driver >= 0); 12445 SD_INFO(SD_LOG_IO, un, 12446 "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12447 un->un_ncmds_in_driver); 12448 12449 mutex_exit(SD_MUTEX(un)); 12450 } 12451 12452 biodone(bp); /* bp is gone after this */ 12453 12454 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12455 } 12456 12457 12458 /* 12459 * Function: sd_uscsi_iodone 12460 * 12461 * Description: Frees the sd_xbuf & returns the buf to its originator. 12462 * 12463 * Context: May be called from interrupt context. 12464 */ 12465 /* ARGSUSED */ 12466 static void 12467 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12468 { 12469 struct sd_xbuf *xp; 12470 12471 ASSERT(un != NULL); 12472 ASSERT(bp != NULL); 12473 12474 xp = SD_GET_XBUF(bp); 12475 ASSERT(xp != NULL); 12476 ASSERT(!mutex_owned(SD_MUTEX(un))); 12477 12478 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12479 12480 bp->b_private = xp->xb_private; 12481 12482 mutex_enter(SD_MUTEX(un)); 12483 12484 /* 12485 * Grab time when the cmd completed. 12486 * This is used for determining if the system has been 12487 * idle long enough to make it idle to the PM framework. 12488 * This is for lowering the overhead, and therefore improving 12489 * performance per I/O operation. 12490 */ 12491 un->un_pm_idle_time = ddi_get_time(); 12492 12493 un->un_ncmds_in_driver--; 12494 ASSERT(un->un_ncmds_in_driver >= 0); 12495 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12496 un->un_ncmds_in_driver); 12497 12498 mutex_exit(SD_MUTEX(un)); 12499 12500 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 12501 SENSE_LENGTH) { 12502 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 12503 MAX_SENSE_LENGTH); 12504 } else { 12505 kmem_free(xp, sizeof (struct sd_xbuf)); 12506 } 12507 12508 biodone(bp); 12509 12510 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12511 } 12512 12513 12514 /* 12515 * Function: sd_mapblockaddr_iostart 12516 * 12517 * Description: Verify request lies within the partition limits for 12518 * the indicated minor device. Issue "overrun" buf if 12519 * request would exceed partition range. Converts 12520 * partition-relative block address to absolute. 12521 * 12522 * Upon exit of this function: 12523 * 1.I/O is aligned 12524 * xp->xb_blkno represents the absolute sector address 12525 * 2.I/O is misaligned 12526 * xp->xb_blkno represents the absolute logical block address 12527 * based on DEV_BSIZE. The logical block address will be 12528 * converted to physical sector address in sd_mapblocksize_\ 12529 * iostart. 12530 * 3.I/O is misaligned but is aligned in "overrun" buf 12531 * xp->xb_blkno represents the absolute logical block address 12532 * based on DEV_BSIZE. The logical block address will be 12533 * converted to physical sector address in sd_mapblocksize_\ 12534 * iostart. But no RMW will be issued in this case. 12535 * 12536 * Context: Can sleep 12537 * 12538 * Issues: This follows what the old code did, in terms of accessing 12539 * some of the partition info in the unit struct without holding 12540 * the mutext. This is a general issue, if the partition info 12541 * can be altered while IO is in progress... as soon as we send 12542 * a buf, its partitioning can be invalid before it gets to the 12543 * device. Probably the right fix is to move partitioning out 12544 * of the driver entirely. 12545 */ 12546 12547 static void 12548 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12549 { 12550 diskaddr_t nblocks; /* #blocks in the given partition */ 12551 daddr_t blocknum; /* Block number specified by the buf */ 12552 size_t requested_nblocks; 12553 size_t available_nblocks; 12554 int partition; 12555 diskaddr_t partition_offset; 12556 struct sd_xbuf *xp; 12557 int secmask = 0, blknomask = 0; 12558 ushort_t is_aligned = TRUE; 12559 12560 ASSERT(un != NULL); 12561 ASSERT(bp != NULL); 12562 ASSERT(!mutex_owned(SD_MUTEX(un))); 12563 12564 SD_TRACE(SD_LOG_IO_PARTITION, un, 12565 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12566 12567 xp = SD_GET_XBUF(bp); 12568 ASSERT(xp != NULL); 12569 12570 /* 12571 * If the geometry is not indicated as valid, attempt to access 12572 * the unit & verify the geometry/label. This can be the case for 12573 * removable-media devices, of if the device was opened in 12574 * NDELAY/NONBLOCK mode. 12575 */ 12576 partition = SDPART(bp->b_edev); 12577 12578 if (!SD_IS_VALID_LABEL(un)) { 12579 sd_ssc_t *ssc; 12580 /* 12581 * Initialize sd_ssc_t for internal uscsi commands 12582 * In case of potential porformance issue, we need 12583 * to alloc memory only if there is invalid label 12584 */ 12585 ssc = sd_ssc_init(un); 12586 12587 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) { 12588 /* 12589 * For removable devices it is possible to start an 12590 * I/O without a media by opening the device in nodelay 12591 * mode. Also for writable CDs there can be many 12592 * scenarios where there is no geometry yet but volume 12593 * manager is trying to issue a read() just because 12594 * it can see TOC on the CD. So do not print a message 12595 * for removables. 12596 */ 12597 if (!un->un_f_has_removable_media) { 12598 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12599 "i/o to invalid geometry\n"); 12600 } 12601 bioerror(bp, EIO); 12602 bp->b_resid = bp->b_bcount; 12603 SD_BEGIN_IODONE(index, un, bp); 12604 12605 sd_ssc_fini(ssc); 12606 return; 12607 } 12608 sd_ssc_fini(ssc); 12609 } 12610 12611 nblocks = 0; 12612 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 12613 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 12614 12615 if (un->un_f_enable_rmw) { 12616 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1; 12617 secmask = un->un_phy_blocksize - 1; 12618 } else { 12619 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 12620 secmask = un->un_tgt_blocksize - 1; 12621 } 12622 12623 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) { 12624 is_aligned = FALSE; 12625 } 12626 12627 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) { 12628 /* 12629 * If I/O is aligned, no need to involve RMW(Read Modify Write) 12630 * Convert the logical block number to target's physical sector 12631 * number. 12632 */ 12633 if (is_aligned) { 12634 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno); 12635 } else { 12636 switch (un->un_f_rmw_type) { 12637 case SD_RMW_TYPE_RETURN_ERROR: 12638 if (un->un_f_enable_rmw) 12639 break; 12640 else { 12641 bp->b_flags |= B_ERROR; 12642 goto error_exit; 12643 } 12644 12645 case SD_RMW_TYPE_DEFAULT: 12646 mutex_enter(SD_MUTEX(un)); 12647 if (!un->un_f_enable_rmw && 12648 un->un_rmw_msg_timeid == NULL) { 12649 scsi_log(SD_DEVINFO(un), sd_label, 12650 CE_WARN, "I/O request is not " 12651 "aligned with %d disk sector size. " 12652 "It is handled through Read Modify " 12653 "Write but the performance is " 12654 "very low.\n", 12655 un->un_tgt_blocksize); 12656 un->un_rmw_msg_timeid = 12657 timeout(sd_rmw_msg_print_handler, 12658 un, SD_RMW_MSG_PRINT_TIMEOUT); 12659 } else { 12660 un->un_rmw_incre_count ++; 12661 } 12662 mutex_exit(SD_MUTEX(un)); 12663 break; 12664 12665 case SD_RMW_TYPE_NO_WARNING: 12666 default: 12667 break; 12668 } 12669 12670 nblocks = SD_TGT2SYSBLOCK(un, nblocks); 12671 partition_offset = SD_TGT2SYSBLOCK(un, 12672 partition_offset); 12673 } 12674 } 12675 12676 /* 12677 * blocknum is the starting block number of the request. At this 12678 * point it is still relative to the start of the minor device. 12679 */ 12680 blocknum = xp->xb_blkno; 12681 12682 /* 12683 * Legacy: If the starting block number is one past the last block 12684 * in the partition, do not set B_ERROR in the buf. 12685 */ 12686 if (blocknum == nblocks) { 12687 goto error_exit; 12688 } 12689 12690 /* 12691 * Confirm that the first block of the request lies within the 12692 * partition limits. Also the requested number of bytes must be 12693 * a multiple of the system block size. 12694 */ 12695 if ((blocknum < 0) || (blocknum >= nblocks) || 12696 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) { 12697 bp->b_flags |= B_ERROR; 12698 goto error_exit; 12699 } 12700 12701 /* 12702 * If the requsted # blocks exceeds the available # blocks, that 12703 * is an overrun of the partition. 12704 */ 12705 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12706 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 12707 } else { 12708 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount); 12709 } 12710 12711 available_nblocks = (size_t)(nblocks - blocknum); 12712 ASSERT(nblocks >= blocknum); 12713 12714 if (requested_nblocks > available_nblocks) { 12715 size_t resid; 12716 12717 /* 12718 * Allocate an "overrun" buf to allow the request to proceed 12719 * for the amount of space available in the partition. The 12720 * amount not transferred will be added into the b_resid 12721 * when the operation is complete. The overrun buf 12722 * replaces the original buf here, and the original buf 12723 * is saved inside the overrun buf, for later use. 12724 */ 12725 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12726 resid = SD_TGTBLOCKS2BYTES(un, 12727 (offset_t)(requested_nblocks - available_nblocks)); 12728 } else { 12729 resid = SD_SYSBLOCKS2BYTES( 12730 (offset_t)(requested_nblocks - available_nblocks)); 12731 } 12732 12733 size_t count = bp->b_bcount - resid; 12734 /* 12735 * Note: count is an unsigned entity thus it'll NEVER 12736 * be less than 0 so ASSERT the original values are 12737 * correct. 12738 */ 12739 ASSERT(bp->b_bcount >= resid); 12740 12741 bp = sd_bioclone_alloc(bp, count, blocknum, 12742 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12743 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12744 ASSERT(xp != NULL); 12745 } 12746 12747 /* At this point there should be no residual for this buf. */ 12748 ASSERT(bp->b_resid == 0); 12749 12750 /* Convert the block number to an absolute address. */ 12751 xp->xb_blkno += partition_offset; 12752 12753 SD_NEXT_IOSTART(index, un, bp); 12754 12755 SD_TRACE(SD_LOG_IO_PARTITION, un, 12756 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12757 12758 return; 12759 12760 error_exit: 12761 bp->b_resid = bp->b_bcount; 12762 SD_BEGIN_IODONE(index, un, bp); 12763 SD_TRACE(SD_LOG_IO_PARTITION, un, 12764 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12765 } 12766 12767 12768 /* 12769 * Function: sd_mapblockaddr_iodone 12770 * 12771 * Description: Completion-side processing for partition management. 12772 * 12773 * Context: May be called under interrupt context 12774 */ 12775 12776 static void 12777 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12778 { 12779 /* int partition; */ /* Not used, see below. */ 12780 ASSERT(un != NULL); 12781 ASSERT(bp != NULL); 12782 ASSERT(!mutex_owned(SD_MUTEX(un))); 12783 12784 SD_TRACE(SD_LOG_IO_PARTITION, un, 12785 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12786 12787 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12788 /* 12789 * We have an "overrun" buf to deal with... 12790 */ 12791 struct sd_xbuf *xp; 12792 struct buf *obp; /* ptr to the original buf */ 12793 12794 xp = SD_GET_XBUF(bp); 12795 ASSERT(xp != NULL); 12796 12797 /* Retrieve the pointer to the original buf */ 12798 obp = (struct buf *)xp->xb_private; 12799 ASSERT(obp != NULL); 12800 12801 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12802 bioerror(obp, bp->b_error); 12803 12804 sd_bioclone_free(bp); 12805 12806 /* 12807 * Get back the original buf. 12808 * Note that since the restoration of xb_blkno below 12809 * was removed, the sd_xbuf is not needed. 12810 */ 12811 bp = obp; 12812 /* 12813 * xp = SD_GET_XBUF(bp); 12814 * ASSERT(xp != NULL); 12815 */ 12816 } 12817 12818 /* 12819 * Convert sd->xb_blkno back to a minor-device relative value. 12820 * Note: this has been commented out, as it is not needed in the 12821 * current implementation of the driver (ie, since this function 12822 * is at the top of the layering chains, so the info will be 12823 * discarded) and it is in the "hot" IO path. 12824 * 12825 * partition = getminor(bp->b_edev) & SDPART_MASK; 12826 * xp->xb_blkno -= un->un_offset[partition]; 12827 */ 12828 12829 SD_NEXT_IODONE(index, un, bp); 12830 12831 SD_TRACE(SD_LOG_IO_PARTITION, un, 12832 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12833 } 12834 12835 12836 /* 12837 * Function: sd_mapblocksize_iostart 12838 * 12839 * Description: Convert between system block size (un->un_sys_blocksize) 12840 * and target block size (un->un_tgt_blocksize). 12841 * 12842 * Context: Can sleep to allocate resources. 12843 * 12844 * Assumptions: A higher layer has already performed any partition validation, 12845 * and converted the xp->xb_blkno to an absolute value relative 12846 * to the start of the device. 12847 * 12848 * It is also assumed that the higher layer has implemented 12849 * an "overrun" mechanism for the case where the request would 12850 * read/write beyond the end of a partition. In this case we 12851 * assume (and ASSERT) that bp->b_resid == 0. 12852 * 12853 * Note: The implementation for this routine assumes the target 12854 * block size remains constant between allocation and transport. 12855 */ 12856 12857 static void 12858 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12859 { 12860 struct sd_mapblocksize_info *bsp; 12861 struct sd_xbuf *xp; 12862 offset_t first_byte; 12863 daddr_t start_block, end_block; 12864 daddr_t request_bytes; 12865 ushort_t is_aligned = FALSE; 12866 12867 ASSERT(un != NULL); 12868 ASSERT(bp != NULL); 12869 ASSERT(!mutex_owned(SD_MUTEX(un))); 12870 ASSERT(bp->b_resid == 0); 12871 12872 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12873 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12874 12875 /* 12876 * For a non-writable CD, a write request is an error 12877 */ 12878 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12879 (un->un_f_mmc_writable_media == FALSE)) { 12880 bioerror(bp, EIO); 12881 bp->b_resid = bp->b_bcount; 12882 SD_BEGIN_IODONE(index, un, bp); 12883 return; 12884 } 12885 12886 /* 12887 * We do not need a shadow buf if the device is using 12888 * un->un_sys_blocksize as its block size or if bcount == 0. 12889 * In this case there is no layer-private data block allocated. 12890 */ 12891 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 12892 (bp->b_bcount == 0)) { 12893 goto done; 12894 } 12895 12896 #if defined(__i386) || defined(__amd64) 12897 /* We do not support non-block-aligned transfers for ROD devices */ 12898 ASSERT(!ISROD(un)); 12899 #endif 12900 12901 xp = SD_GET_XBUF(bp); 12902 ASSERT(xp != NULL); 12903 12904 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12905 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12906 un->un_tgt_blocksize, DEV_BSIZE); 12907 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12908 "request start block:0x%x\n", xp->xb_blkno); 12909 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12910 "request len:0x%x\n", bp->b_bcount); 12911 12912 /* 12913 * Allocate the layer-private data area for the mapblocksize layer. 12914 * Layers are allowed to use the xp_private member of the sd_xbuf 12915 * struct to store the pointer to their layer-private data block, but 12916 * each layer also has the responsibility of restoring the prior 12917 * contents of xb_private before returning the buf/xbuf to the 12918 * higher layer that sent it. 12919 * 12920 * Here we save the prior contents of xp->xb_private into the 12921 * bsp->mbs_oprivate field of our layer-private data area. This value 12922 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12923 * the layer-private area and returning the buf/xbuf to the layer 12924 * that sent it. 12925 * 12926 * Note that here we use kmem_zalloc for the allocation as there are 12927 * parts of the mapblocksize code that expect certain fields to be 12928 * zero unless explicitly set to a required value. 12929 */ 12930 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12931 bsp->mbs_oprivate = xp->xb_private; 12932 xp->xb_private = bsp; 12933 12934 /* 12935 * This treats the data on the disk (target) as an array of bytes. 12936 * first_byte is the byte offset, from the beginning of the device, 12937 * to the location of the request. This is converted from a 12938 * un->un_sys_blocksize block address to a byte offset, and then back 12939 * to a block address based upon a un->un_tgt_blocksize block size. 12940 * 12941 * xp->xb_blkno should be absolute upon entry into this function, 12942 * but, but it is based upon partitions that use the "system" 12943 * block size. It must be adjusted to reflect the block size of 12944 * the target. 12945 * 12946 * Note that end_block is actually the block that follows the last 12947 * block of the request, but that's what is needed for the computation. 12948 */ 12949 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 12950 if (un->un_f_enable_rmw) { 12951 start_block = xp->xb_blkno = 12952 (first_byte / un->un_phy_blocksize) * 12953 (un->un_phy_blocksize / DEV_BSIZE); 12954 end_block = ((first_byte + bp->b_bcount + 12955 un->un_phy_blocksize - 1) / un->un_phy_blocksize) * 12956 (un->un_phy_blocksize / DEV_BSIZE); 12957 } else { 12958 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12959 end_block = (first_byte + bp->b_bcount + 12960 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 12961 } 12962 12963 /* request_bytes is rounded up to a multiple of the target block size */ 12964 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12965 12966 /* 12967 * See if the starting address of the request and the request 12968 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12969 * then we do not need to allocate a shadow buf to handle the request. 12970 */ 12971 if (un->un_f_enable_rmw) { 12972 if (((first_byte % un->un_phy_blocksize) == 0) && 12973 ((bp->b_bcount % un->un_phy_blocksize) == 0)) { 12974 is_aligned = TRUE; 12975 } 12976 } else { 12977 if (((first_byte % un->un_tgt_blocksize) == 0) && 12978 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12979 is_aligned = TRUE; 12980 } 12981 } 12982 12983 if ((bp->b_flags & B_READ) == 0) { 12984 /* 12985 * Lock the range for a write operation. An aligned request is 12986 * considered a simple write; otherwise the request must be a 12987 * read-modify-write. 12988 */ 12989 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12990 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12991 } 12992 12993 /* 12994 * Alloc a shadow buf if the request is not aligned. Also, this is 12995 * where the READ command is generated for a read-modify-write. (The 12996 * write phase is deferred until after the read completes.) 12997 */ 12998 if (is_aligned == FALSE) { 12999 13000 struct sd_mapblocksize_info *shadow_bsp; 13001 struct sd_xbuf *shadow_xp; 13002 struct buf *shadow_bp; 13003 13004 /* 13005 * Allocate the shadow buf and it associated xbuf. Note that 13006 * after this call the xb_blkno value in both the original 13007 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 13008 * same: absolute relative to the start of the device, and 13009 * adjusted for the target block size. The b_blkno in the 13010 * shadow buf will also be set to this value. We should never 13011 * change b_blkno in the original bp however. 13012 * 13013 * Note also that the shadow buf will always need to be a 13014 * READ command, regardless of whether the incoming command 13015 * is a READ or a WRITE. 13016 */ 13017 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 13018 xp->xb_blkno, 13019 (int (*)(struct buf *)) sd_mapblocksize_iodone); 13020 13021 shadow_xp = SD_GET_XBUF(shadow_bp); 13022 13023 /* 13024 * Allocate the layer-private data for the shadow buf. 13025 * (No need to preserve xb_private in the shadow xbuf.) 13026 */ 13027 shadow_xp->xb_private = shadow_bsp = 13028 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 13029 13030 /* 13031 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 13032 * to figure out where the start of the user data is (based upon 13033 * the system block size) in the data returned by the READ 13034 * command (which will be based upon the target blocksize). Note 13035 * that this is only really used if the request is unaligned. 13036 */ 13037 if (un->un_f_enable_rmw) { 13038 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13039 ((offset_t)xp->xb_blkno * un->un_sys_blocksize)); 13040 ASSERT((bsp->mbs_copy_offset >= 0) && 13041 (bsp->mbs_copy_offset < un->un_phy_blocksize)); 13042 } else { 13043 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13044 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13045 ASSERT((bsp->mbs_copy_offset >= 0) && 13046 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13047 } 13048 13049 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13050 13051 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13052 13053 /* Transfer the wmap (if any) to the shadow buf */ 13054 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13055 bsp->mbs_wmp = NULL; 13056 13057 /* 13058 * The shadow buf goes on from here in place of the 13059 * original buf. 13060 */ 13061 shadow_bsp->mbs_orig_bp = bp; 13062 bp = shadow_bp; 13063 } 13064 13065 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13066 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13067 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13068 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13069 request_bytes); 13070 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13071 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13072 13073 done: 13074 SD_NEXT_IOSTART(index, un, bp); 13075 13076 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13077 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13078 } 13079 13080 13081 /* 13082 * Function: sd_mapblocksize_iodone 13083 * 13084 * Description: Completion side processing for block-size mapping. 13085 * 13086 * Context: May be called under interrupt context 13087 */ 13088 13089 static void 13090 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13091 { 13092 struct sd_mapblocksize_info *bsp; 13093 struct sd_xbuf *xp; 13094 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13095 struct buf *orig_bp; /* ptr to the original buf */ 13096 offset_t shadow_end; 13097 offset_t request_end; 13098 offset_t shadow_start; 13099 ssize_t copy_offset; 13100 size_t copy_length; 13101 size_t shortfall; 13102 uint_t is_write; /* TRUE if this bp is a WRITE */ 13103 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13104 13105 ASSERT(un != NULL); 13106 ASSERT(bp != NULL); 13107 13108 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13109 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13110 13111 /* 13112 * There is no shadow buf or layer-private data if the target is 13113 * using un->un_sys_blocksize as its block size or if bcount == 0. 13114 */ 13115 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 13116 (bp->b_bcount == 0)) { 13117 goto exit; 13118 } 13119 13120 xp = SD_GET_XBUF(bp); 13121 ASSERT(xp != NULL); 13122 13123 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13124 bsp = xp->xb_private; 13125 13126 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13127 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13128 13129 if (is_write) { 13130 /* 13131 * For a WRITE request we must free up the block range that 13132 * we have locked up. This holds regardless of whether this is 13133 * an aligned write request or a read-modify-write request. 13134 */ 13135 sd_range_unlock(un, bsp->mbs_wmp); 13136 bsp->mbs_wmp = NULL; 13137 } 13138 13139 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13140 /* 13141 * An aligned read or write command will have no shadow buf; 13142 * there is not much else to do with it. 13143 */ 13144 goto done; 13145 } 13146 13147 orig_bp = bsp->mbs_orig_bp; 13148 ASSERT(orig_bp != NULL); 13149 orig_xp = SD_GET_XBUF(orig_bp); 13150 ASSERT(orig_xp != NULL); 13151 ASSERT(!mutex_owned(SD_MUTEX(un))); 13152 13153 if (!is_write && has_wmap) { 13154 /* 13155 * A READ with a wmap means this is the READ phase of a 13156 * read-modify-write. If an error occurred on the READ then 13157 * we do not proceed with the WRITE phase or copy any data. 13158 * Just release the write maps and return with an error. 13159 */ 13160 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13161 orig_bp->b_resid = orig_bp->b_bcount; 13162 bioerror(orig_bp, bp->b_error); 13163 sd_range_unlock(un, bsp->mbs_wmp); 13164 goto freebuf_done; 13165 } 13166 } 13167 13168 /* 13169 * Here is where we set up to copy the data from the shadow buf 13170 * into the space associated with the original buf. 13171 * 13172 * To deal with the conversion between block sizes, these 13173 * computations treat the data as an array of bytes, with the 13174 * first byte (byte 0) corresponding to the first byte in the 13175 * first block on the disk. 13176 */ 13177 13178 /* 13179 * shadow_start and shadow_len indicate the location and size of 13180 * the data returned with the shadow IO request. 13181 */ 13182 if (un->un_f_enable_rmw) { 13183 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 13184 } else { 13185 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13186 } 13187 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13188 13189 /* 13190 * copy_offset gives the offset (in bytes) from the start of the first 13191 * block of the READ request to the beginning of the data. We retrieve 13192 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13193 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13194 * data to be copied (in bytes). 13195 */ 13196 copy_offset = bsp->mbs_copy_offset; 13197 if (un->un_f_enable_rmw) { 13198 ASSERT((copy_offset >= 0) && 13199 (copy_offset < un->un_phy_blocksize)); 13200 } else { 13201 ASSERT((copy_offset >= 0) && 13202 (copy_offset < un->un_tgt_blocksize)); 13203 } 13204 13205 copy_length = orig_bp->b_bcount; 13206 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13207 13208 /* 13209 * Set up the resid and error fields of orig_bp as appropriate. 13210 */ 13211 if (shadow_end >= request_end) { 13212 /* We got all the requested data; set resid to zero */ 13213 orig_bp->b_resid = 0; 13214 } else { 13215 /* 13216 * We failed to get enough data to fully satisfy the original 13217 * request. Just copy back whatever data we got and set 13218 * up the residual and error code as required. 13219 * 13220 * 'shortfall' is the amount by which the data received with the 13221 * shadow buf has "fallen short" of the requested amount. 13222 */ 13223 shortfall = (size_t)(request_end - shadow_end); 13224 13225 if (shortfall > orig_bp->b_bcount) { 13226 /* 13227 * We did not get enough data to even partially 13228 * fulfill the original request. The residual is 13229 * equal to the amount requested. 13230 */ 13231 orig_bp->b_resid = orig_bp->b_bcount; 13232 } else { 13233 /* 13234 * We did not get all the data that we requested 13235 * from the device, but we will try to return what 13236 * portion we did get. 13237 */ 13238 orig_bp->b_resid = shortfall; 13239 } 13240 ASSERT(copy_length >= orig_bp->b_resid); 13241 copy_length -= orig_bp->b_resid; 13242 } 13243 13244 /* Propagate the error code from the shadow buf to the original buf */ 13245 bioerror(orig_bp, bp->b_error); 13246 13247 if (is_write) { 13248 goto freebuf_done; /* No data copying for a WRITE */ 13249 } 13250 13251 if (has_wmap) { 13252 /* 13253 * This is a READ command from the READ phase of a 13254 * read-modify-write request. We have to copy the data given 13255 * by the user OVER the data returned by the READ command, 13256 * then convert the command from a READ to a WRITE and send 13257 * it back to the target. 13258 */ 13259 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13260 copy_length); 13261 13262 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13263 13264 /* 13265 * Dispatch the WRITE command to the taskq thread, which 13266 * will in turn send the command to the target. When the 13267 * WRITE command completes, we (sd_mapblocksize_iodone()) 13268 * will get called again as part of the iodone chain 13269 * processing for it. Note that we will still be dealing 13270 * with the shadow buf at that point. 13271 */ 13272 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13273 KM_NOSLEEP) != 0) { 13274 /* 13275 * Dispatch was successful so we are done. Return 13276 * without going any higher up the iodone chain. Do 13277 * not free up any layer-private data until after the 13278 * WRITE completes. 13279 */ 13280 return; 13281 } 13282 13283 /* 13284 * Dispatch of the WRITE command failed; set up the error 13285 * condition and send this IO back up the iodone chain. 13286 */ 13287 bioerror(orig_bp, EIO); 13288 orig_bp->b_resid = orig_bp->b_bcount; 13289 13290 } else { 13291 /* 13292 * This is a regular READ request (ie, not a RMW). Copy the 13293 * data from the shadow buf into the original buf. The 13294 * copy_offset compensates for any "misalignment" between the 13295 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13296 * original buf (with its un->un_sys_blocksize blocks). 13297 */ 13298 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13299 copy_length); 13300 } 13301 13302 freebuf_done: 13303 13304 /* 13305 * At this point we still have both the shadow buf AND the original 13306 * buf to deal with, as well as the layer-private data area in each. 13307 * Local variables are as follows: 13308 * 13309 * bp -- points to shadow buf 13310 * xp -- points to xbuf of shadow buf 13311 * bsp -- points to layer-private data area of shadow buf 13312 * orig_bp -- points to original buf 13313 * 13314 * First free the shadow buf and its associated xbuf, then free the 13315 * layer-private data area from the shadow buf. There is no need to 13316 * restore xb_private in the shadow xbuf. 13317 */ 13318 sd_shadow_buf_free(bp); 13319 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13320 13321 /* 13322 * Now update the local variables to point to the original buf, xbuf, 13323 * and layer-private area. 13324 */ 13325 bp = orig_bp; 13326 xp = SD_GET_XBUF(bp); 13327 ASSERT(xp != NULL); 13328 ASSERT(xp == orig_xp); 13329 bsp = xp->xb_private; 13330 ASSERT(bsp != NULL); 13331 13332 done: 13333 /* 13334 * Restore xb_private to whatever it was set to by the next higher 13335 * layer in the chain, then free the layer-private data area. 13336 */ 13337 xp->xb_private = bsp->mbs_oprivate; 13338 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13339 13340 exit: 13341 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13342 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13343 13344 SD_NEXT_IODONE(index, un, bp); 13345 } 13346 13347 13348 /* 13349 * Function: sd_checksum_iostart 13350 * 13351 * Description: A stub function for a layer that's currently not used. 13352 * For now just a placeholder. 13353 * 13354 * Context: Kernel thread context 13355 */ 13356 13357 static void 13358 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13359 { 13360 ASSERT(un != NULL); 13361 ASSERT(bp != NULL); 13362 ASSERT(!mutex_owned(SD_MUTEX(un))); 13363 SD_NEXT_IOSTART(index, un, bp); 13364 } 13365 13366 13367 /* 13368 * Function: sd_checksum_iodone 13369 * 13370 * Description: A stub function for a layer that's currently not used. 13371 * For now just a placeholder. 13372 * 13373 * Context: May be called under interrupt context 13374 */ 13375 13376 static void 13377 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13378 { 13379 ASSERT(un != NULL); 13380 ASSERT(bp != NULL); 13381 ASSERT(!mutex_owned(SD_MUTEX(un))); 13382 SD_NEXT_IODONE(index, un, bp); 13383 } 13384 13385 13386 /* 13387 * Function: sd_checksum_uscsi_iostart 13388 * 13389 * Description: A stub function for a layer that's currently not used. 13390 * For now just a placeholder. 13391 * 13392 * Context: Kernel thread context 13393 */ 13394 13395 static void 13396 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13397 { 13398 ASSERT(un != NULL); 13399 ASSERT(bp != NULL); 13400 ASSERT(!mutex_owned(SD_MUTEX(un))); 13401 SD_NEXT_IOSTART(index, un, bp); 13402 } 13403 13404 13405 /* 13406 * Function: sd_checksum_uscsi_iodone 13407 * 13408 * Description: A stub function for a layer that's currently not used. 13409 * For now just a placeholder. 13410 * 13411 * Context: May be called under interrupt context 13412 */ 13413 13414 static void 13415 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13416 { 13417 ASSERT(un != NULL); 13418 ASSERT(bp != NULL); 13419 ASSERT(!mutex_owned(SD_MUTEX(un))); 13420 SD_NEXT_IODONE(index, un, bp); 13421 } 13422 13423 13424 /* 13425 * Function: sd_pm_iostart 13426 * 13427 * Description: iostart-side routine for Power mangement. 13428 * 13429 * Context: Kernel thread context 13430 */ 13431 13432 static void 13433 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13434 { 13435 ASSERT(un != NULL); 13436 ASSERT(bp != NULL); 13437 ASSERT(!mutex_owned(SD_MUTEX(un))); 13438 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13439 13440 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13441 13442 if (sd_pm_entry(un) != DDI_SUCCESS) { 13443 /* 13444 * Set up to return the failed buf back up the 'iodone' 13445 * side of the calling chain. 13446 */ 13447 bioerror(bp, EIO); 13448 bp->b_resid = bp->b_bcount; 13449 13450 SD_BEGIN_IODONE(index, un, bp); 13451 13452 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13453 return; 13454 } 13455 13456 SD_NEXT_IOSTART(index, un, bp); 13457 13458 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13459 } 13460 13461 13462 /* 13463 * Function: sd_pm_iodone 13464 * 13465 * Description: iodone-side routine for power mangement. 13466 * 13467 * Context: may be called from interrupt context 13468 */ 13469 13470 static void 13471 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13472 { 13473 ASSERT(un != NULL); 13474 ASSERT(bp != NULL); 13475 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13476 13477 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13478 13479 /* 13480 * After attach the following flag is only read, so don't 13481 * take the penalty of acquiring a mutex for it. 13482 */ 13483 if (un->un_f_pm_is_enabled == TRUE) { 13484 sd_pm_exit(un); 13485 } 13486 13487 SD_NEXT_IODONE(index, un, bp); 13488 13489 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13490 } 13491 13492 13493 /* 13494 * Function: sd_core_iostart 13495 * 13496 * Description: Primary driver function for enqueuing buf(9S) structs from 13497 * the system and initiating IO to the target device 13498 * 13499 * Context: Kernel thread context. Can sleep. 13500 * 13501 * Assumptions: - The given xp->xb_blkno is absolute 13502 * (ie, relative to the start of the device). 13503 * - The IO is to be done using the native blocksize of 13504 * the device, as specified in un->un_tgt_blocksize. 13505 */ 13506 /* ARGSUSED */ 13507 static void 13508 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13509 { 13510 struct sd_xbuf *xp; 13511 13512 ASSERT(un != NULL); 13513 ASSERT(bp != NULL); 13514 ASSERT(!mutex_owned(SD_MUTEX(un))); 13515 ASSERT(bp->b_resid == 0); 13516 13517 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13518 13519 xp = SD_GET_XBUF(bp); 13520 ASSERT(xp != NULL); 13521 13522 mutex_enter(SD_MUTEX(un)); 13523 13524 /* 13525 * If we are currently in the failfast state, fail any new IO 13526 * that has B_FAILFAST set, then return. 13527 */ 13528 if ((bp->b_flags & B_FAILFAST) && 13529 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13530 mutex_exit(SD_MUTEX(un)); 13531 bioerror(bp, EIO); 13532 bp->b_resid = bp->b_bcount; 13533 SD_BEGIN_IODONE(index, un, bp); 13534 return; 13535 } 13536 13537 if (SD_IS_DIRECT_PRIORITY(xp)) { 13538 /* 13539 * Priority command -- transport it immediately. 13540 * 13541 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13542 * because all direct priority commands should be associated 13543 * with error recovery actions which we don't want to retry. 13544 */ 13545 sd_start_cmds(un, bp); 13546 } else { 13547 /* 13548 * Normal command -- add it to the wait queue, then start 13549 * transporting commands from the wait queue. 13550 */ 13551 sd_add_buf_to_waitq(un, bp); 13552 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13553 sd_start_cmds(un, NULL); 13554 } 13555 13556 mutex_exit(SD_MUTEX(un)); 13557 13558 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13559 } 13560 13561 13562 /* 13563 * Function: sd_init_cdb_limits 13564 * 13565 * Description: This is to handle scsi_pkt initialization differences 13566 * between the driver platforms. 13567 * 13568 * Legacy behaviors: 13569 * 13570 * If the block number or the sector count exceeds the 13571 * capabilities of a Group 0 command, shift over to a 13572 * Group 1 command. We don't blindly use Group 1 13573 * commands because a) some drives (CDC Wren IVs) get a 13574 * bit confused, and b) there is probably a fair amount 13575 * of speed difference for a target to receive and decode 13576 * a 10 byte command instead of a 6 byte command. 13577 * 13578 * The xfer time difference of 6 vs 10 byte CDBs is 13579 * still significant so this code is still worthwhile. 13580 * 10 byte CDBs are very inefficient with the fas HBA driver 13581 * and older disks. Each CDB byte took 1 usec with some 13582 * popular disks. 13583 * 13584 * Context: Must be called at attach time 13585 */ 13586 13587 static void 13588 sd_init_cdb_limits(struct sd_lun *un) 13589 { 13590 int hba_cdb_limit; 13591 13592 /* 13593 * Use CDB_GROUP1 commands for most devices except for 13594 * parallel SCSI fixed drives in which case we get better 13595 * performance using CDB_GROUP0 commands (where applicable). 13596 */ 13597 un->un_mincdb = SD_CDB_GROUP1; 13598 #if !defined(__fibre) 13599 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13600 !un->un_f_has_removable_media) { 13601 un->un_mincdb = SD_CDB_GROUP0; 13602 } 13603 #endif 13604 13605 /* 13606 * Try to read the max-cdb-length supported by HBA. 13607 */ 13608 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13609 if (0 >= un->un_max_hba_cdb) { 13610 un->un_max_hba_cdb = CDB_GROUP4; 13611 hba_cdb_limit = SD_CDB_GROUP4; 13612 } else if (0 < un->un_max_hba_cdb && 13613 un->un_max_hba_cdb < CDB_GROUP1) { 13614 hba_cdb_limit = SD_CDB_GROUP0; 13615 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13616 un->un_max_hba_cdb < CDB_GROUP5) { 13617 hba_cdb_limit = SD_CDB_GROUP1; 13618 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13619 un->un_max_hba_cdb < CDB_GROUP4) { 13620 hba_cdb_limit = SD_CDB_GROUP5; 13621 } else { 13622 hba_cdb_limit = SD_CDB_GROUP4; 13623 } 13624 13625 /* 13626 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13627 * commands for fixed disks unless we are building for a 32 bit 13628 * kernel. 13629 */ 13630 #ifdef _LP64 13631 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13632 min(hba_cdb_limit, SD_CDB_GROUP4); 13633 #else 13634 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13635 min(hba_cdb_limit, SD_CDB_GROUP1); 13636 #endif 13637 13638 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13639 ? sizeof (struct scsi_arq_status) : 1); 13640 un->un_cmd_timeout = (ushort_t)sd_io_time; 13641 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13642 } 13643 13644 13645 /* 13646 * Function: sd_initpkt_for_buf 13647 * 13648 * Description: Allocate and initialize for transport a scsi_pkt struct, 13649 * based upon the info specified in the given buf struct. 13650 * 13651 * Assumes the xb_blkno in the request is absolute (ie, 13652 * relative to the start of the device (NOT partition!). 13653 * Also assumes that the request is using the native block 13654 * size of the device (as returned by the READ CAPACITY 13655 * command). 13656 * 13657 * Return Code: SD_PKT_ALLOC_SUCCESS 13658 * SD_PKT_ALLOC_FAILURE 13659 * SD_PKT_ALLOC_FAILURE_NO_DMA 13660 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13661 * 13662 * Context: Kernel thread and may be called from software interrupt context 13663 * as part of a sdrunout callback. This function may not block or 13664 * call routines that block 13665 */ 13666 13667 static int 13668 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13669 { 13670 struct sd_xbuf *xp; 13671 struct scsi_pkt *pktp = NULL; 13672 struct sd_lun *un; 13673 size_t blockcount; 13674 daddr_t startblock; 13675 int rval; 13676 int cmd_flags; 13677 13678 ASSERT(bp != NULL); 13679 ASSERT(pktpp != NULL); 13680 xp = SD_GET_XBUF(bp); 13681 ASSERT(xp != NULL); 13682 un = SD_GET_UN(bp); 13683 ASSERT(un != NULL); 13684 ASSERT(mutex_owned(SD_MUTEX(un))); 13685 ASSERT(bp->b_resid == 0); 13686 13687 SD_TRACE(SD_LOG_IO_CORE, un, 13688 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13689 13690 mutex_exit(SD_MUTEX(un)); 13691 13692 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13693 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13694 /* 13695 * Already have a scsi_pkt -- just need DMA resources. 13696 * We must recompute the CDB in case the mapping returns 13697 * a nonzero pkt_resid. 13698 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13699 * that is being retried, the unmap/remap of the DMA resouces 13700 * will result in the entire transfer starting over again 13701 * from the very first block. 13702 */ 13703 ASSERT(xp->xb_pktp != NULL); 13704 pktp = xp->xb_pktp; 13705 } else { 13706 pktp = NULL; 13707 } 13708 #endif /* __i386 || __amd64 */ 13709 13710 startblock = xp->xb_blkno; /* Absolute block num. */ 13711 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13712 13713 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13714 13715 /* 13716 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13717 * call scsi_init_pkt, and build the CDB. 13718 */ 13719 rval = sd_setup_rw_pkt(un, &pktp, bp, 13720 cmd_flags, sdrunout, (caddr_t)un, 13721 startblock, blockcount); 13722 13723 if (rval == 0) { 13724 /* 13725 * Success. 13726 * 13727 * If partial DMA is being used and required for this transfer. 13728 * set it up here. 13729 */ 13730 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13731 (pktp->pkt_resid != 0)) { 13732 13733 /* 13734 * Save the CDB length and pkt_resid for the 13735 * next xfer 13736 */ 13737 xp->xb_dma_resid = pktp->pkt_resid; 13738 13739 /* rezero resid */ 13740 pktp->pkt_resid = 0; 13741 13742 } else { 13743 xp->xb_dma_resid = 0; 13744 } 13745 13746 pktp->pkt_flags = un->un_tagflags; 13747 pktp->pkt_time = un->un_cmd_timeout; 13748 pktp->pkt_comp = sdintr; 13749 13750 pktp->pkt_private = bp; 13751 *pktpp = pktp; 13752 13753 SD_TRACE(SD_LOG_IO_CORE, un, 13754 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13755 13756 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13757 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13758 #endif 13759 13760 mutex_enter(SD_MUTEX(un)); 13761 return (SD_PKT_ALLOC_SUCCESS); 13762 13763 } 13764 13765 /* 13766 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13767 * from sd_setup_rw_pkt. 13768 */ 13769 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13770 13771 if (rval == SD_PKT_ALLOC_FAILURE) { 13772 *pktpp = NULL; 13773 /* 13774 * Set the driver state to RWAIT to indicate the driver 13775 * is waiting on resource allocations. The driver will not 13776 * suspend, pm_suspend, or detatch while the state is RWAIT. 13777 */ 13778 mutex_enter(SD_MUTEX(un)); 13779 New_state(un, SD_STATE_RWAIT); 13780 13781 SD_ERROR(SD_LOG_IO_CORE, un, 13782 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13783 13784 if ((bp->b_flags & B_ERROR) != 0) { 13785 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13786 } 13787 return (SD_PKT_ALLOC_FAILURE); 13788 } else { 13789 /* 13790 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13791 * 13792 * This should never happen. Maybe someone messed with the 13793 * kernel's minphys? 13794 */ 13795 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13796 "Request rejected: too large for CDB: " 13797 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13798 SD_ERROR(SD_LOG_IO_CORE, un, 13799 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13800 mutex_enter(SD_MUTEX(un)); 13801 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13802 13803 } 13804 } 13805 13806 13807 /* 13808 * Function: sd_destroypkt_for_buf 13809 * 13810 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13811 * 13812 * Context: Kernel thread or interrupt context 13813 */ 13814 13815 static void 13816 sd_destroypkt_for_buf(struct buf *bp) 13817 { 13818 ASSERT(bp != NULL); 13819 ASSERT(SD_GET_UN(bp) != NULL); 13820 13821 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13822 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13823 13824 ASSERT(SD_GET_PKTP(bp) != NULL); 13825 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13826 13827 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13828 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13829 } 13830 13831 /* 13832 * Function: sd_setup_rw_pkt 13833 * 13834 * Description: Determines appropriate CDB group for the requested LBA 13835 * and transfer length, calls scsi_init_pkt, and builds 13836 * the CDB. Do not use for partial DMA transfers except 13837 * for the initial transfer since the CDB size must 13838 * remain constant. 13839 * 13840 * Context: Kernel thread and may be called from software interrupt 13841 * context as part of a sdrunout callback. This function may not 13842 * block or call routines that block 13843 */ 13844 13845 13846 int 13847 sd_setup_rw_pkt(struct sd_lun *un, 13848 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13849 int (*callback)(caddr_t), caddr_t callback_arg, 13850 diskaddr_t lba, uint32_t blockcount) 13851 { 13852 struct scsi_pkt *return_pktp; 13853 union scsi_cdb *cdbp; 13854 struct sd_cdbinfo *cp = NULL; 13855 int i; 13856 13857 /* 13858 * See which size CDB to use, based upon the request. 13859 */ 13860 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13861 13862 /* 13863 * Check lba and block count against sd_cdbtab limits. 13864 * In the partial DMA case, we have to use the same size 13865 * CDB for all the transfers. Check lba + blockcount 13866 * against the max LBA so we know that segment of the 13867 * transfer can use the CDB we select. 13868 */ 13869 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13870 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13871 13872 /* 13873 * The command will fit into the CDB type 13874 * specified by sd_cdbtab[i]. 13875 */ 13876 cp = sd_cdbtab + i; 13877 13878 /* 13879 * Call scsi_init_pkt so we can fill in the 13880 * CDB. 13881 */ 13882 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13883 bp, cp->sc_grpcode, un->un_status_len, 0, 13884 flags, callback, callback_arg); 13885 13886 if (return_pktp != NULL) { 13887 13888 /* 13889 * Return new value of pkt 13890 */ 13891 *pktpp = return_pktp; 13892 13893 /* 13894 * To be safe, zero the CDB insuring there is 13895 * no leftover data from a previous command. 13896 */ 13897 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13898 13899 /* 13900 * Handle partial DMA mapping 13901 */ 13902 if (return_pktp->pkt_resid != 0) { 13903 13904 /* 13905 * Not going to xfer as many blocks as 13906 * originally expected 13907 */ 13908 blockcount -= 13909 SD_BYTES2TGTBLOCKS(un, 13910 return_pktp->pkt_resid); 13911 } 13912 13913 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13914 13915 /* 13916 * Set command byte based on the CDB 13917 * type we matched. 13918 */ 13919 cdbp->scc_cmd = cp->sc_grpmask | 13920 ((bp->b_flags & B_READ) ? 13921 SCMD_READ : SCMD_WRITE); 13922 13923 SD_FILL_SCSI1_LUN(un, return_pktp); 13924 13925 /* 13926 * Fill in LBA and length 13927 */ 13928 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13929 (cp->sc_grpcode == CDB_GROUP4) || 13930 (cp->sc_grpcode == CDB_GROUP0) || 13931 (cp->sc_grpcode == CDB_GROUP5)); 13932 13933 if (cp->sc_grpcode == CDB_GROUP1) { 13934 FORMG1ADDR(cdbp, lba); 13935 FORMG1COUNT(cdbp, blockcount); 13936 return (0); 13937 } else if (cp->sc_grpcode == CDB_GROUP4) { 13938 FORMG4LONGADDR(cdbp, lba); 13939 FORMG4COUNT(cdbp, blockcount); 13940 return (0); 13941 } else if (cp->sc_grpcode == CDB_GROUP0) { 13942 FORMG0ADDR(cdbp, lba); 13943 FORMG0COUNT(cdbp, blockcount); 13944 return (0); 13945 } else if (cp->sc_grpcode == CDB_GROUP5) { 13946 FORMG5ADDR(cdbp, lba); 13947 FORMG5COUNT(cdbp, blockcount); 13948 return (0); 13949 } 13950 13951 /* 13952 * It should be impossible to not match one 13953 * of the CDB types above, so we should never 13954 * reach this point. Set the CDB command byte 13955 * to test-unit-ready to avoid writing 13956 * to somewhere we don't intend. 13957 */ 13958 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13959 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13960 } else { 13961 /* 13962 * Couldn't get scsi_pkt 13963 */ 13964 return (SD_PKT_ALLOC_FAILURE); 13965 } 13966 } 13967 } 13968 13969 /* 13970 * None of the available CDB types were suitable. This really 13971 * should never happen: on a 64 bit system we support 13972 * READ16/WRITE16 which will hold an entire 64 bit disk address 13973 * and on a 32 bit system we will refuse to bind to a device 13974 * larger than 2TB so addresses will never be larger than 32 bits. 13975 */ 13976 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13977 } 13978 13979 /* 13980 * Function: sd_setup_next_rw_pkt 13981 * 13982 * Description: Setup packet for partial DMA transfers, except for the 13983 * initial transfer. sd_setup_rw_pkt should be used for 13984 * the initial transfer. 13985 * 13986 * Context: Kernel thread and may be called from interrupt context. 13987 */ 13988 13989 int 13990 sd_setup_next_rw_pkt(struct sd_lun *un, 13991 struct scsi_pkt *pktp, struct buf *bp, 13992 diskaddr_t lba, uint32_t blockcount) 13993 { 13994 uchar_t com; 13995 union scsi_cdb *cdbp; 13996 uchar_t cdb_group_id; 13997 13998 ASSERT(pktp != NULL); 13999 ASSERT(pktp->pkt_cdbp != NULL); 14000 14001 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 14002 com = cdbp->scc_cmd; 14003 cdb_group_id = CDB_GROUPID(com); 14004 14005 ASSERT((cdb_group_id == CDB_GROUPID_0) || 14006 (cdb_group_id == CDB_GROUPID_1) || 14007 (cdb_group_id == CDB_GROUPID_4) || 14008 (cdb_group_id == CDB_GROUPID_5)); 14009 14010 /* 14011 * Move pkt to the next portion of the xfer. 14012 * func is NULL_FUNC so we do not have to release 14013 * the disk mutex here. 14014 */ 14015 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 14016 NULL_FUNC, NULL) == pktp) { 14017 /* Success. Handle partial DMA */ 14018 if (pktp->pkt_resid != 0) { 14019 blockcount -= 14020 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 14021 } 14022 14023 cdbp->scc_cmd = com; 14024 SD_FILL_SCSI1_LUN(un, pktp); 14025 if (cdb_group_id == CDB_GROUPID_1) { 14026 FORMG1ADDR(cdbp, lba); 14027 FORMG1COUNT(cdbp, blockcount); 14028 return (0); 14029 } else if (cdb_group_id == CDB_GROUPID_4) { 14030 FORMG4LONGADDR(cdbp, lba); 14031 FORMG4COUNT(cdbp, blockcount); 14032 return (0); 14033 } else if (cdb_group_id == CDB_GROUPID_0) { 14034 FORMG0ADDR(cdbp, lba); 14035 FORMG0COUNT(cdbp, blockcount); 14036 return (0); 14037 } else if (cdb_group_id == CDB_GROUPID_5) { 14038 FORMG5ADDR(cdbp, lba); 14039 FORMG5COUNT(cdbp, blockcount); 14040 return (0); 14041 } 14042 14043 /* Unreachable */ 14044 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14045 } 14046 14047 /* 14048 * Error setting up next portion of cmd transfer. 14049 * Something is definitely very wrong and this 14050 * should not happen. 14051 */ 14052 return (SD_PKT_ALLOC_FAILURE); 14053 } 14054 14055 /* 14056 * Function: sd_initpkt_for_uscsi 14057 * 14058 * Description: Allocate and initialize for transport a scsi_pkt struct, 14059 * based upon the info specified in the given uscsi_cmd struct. 14060 * 14061 * Return Code: SD_PKT_ALLOC_SUCCESS 14062 * SD_PKT_ALLOC_FAILURE 14063 * SD_PKT_ALLOC_FAILURE_NO_DMA 14064 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14065 * 14066 * Context: Kernel thread and may be called from software interrupt context 14067 * as part of a sdrunout callback. This function may not block or 14068 * call routines that block 14069 */ 14070 14071 static int 14072 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14073 { 14074 struct uscsi_cmd *uscmd; 14075 struct sd_xbuf *xp; 14076 struct scsi_pkt *pktp; 14077 struct sd_lun *un; 14078 uint32_t flags = 0; 14079 14080 ASSERT(bp != NULL); 14081 ASSERT(pktpp != NULL); 14082 xp = SD_GET_XBUF(bp); 14083 ASSERT(xp != NULL); 14084 un = SD_GET_UN(bp); 14085 ASSERT(un != NULL); 14086 ASSERT(mutex_owned(SD_MUTEX(un))); 14087 14088 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14089 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14090 ASSERT(uscmd != NULL); 14091 14092 SD_TRACE(SD_LOG_IO_CORE, un, 14093 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14094 14095 /* 14096 * Allocate the scsi_pkt for the command. 14097 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14098 * during scsi_init_pkt time and will continue to use the 14099 * same path as long as the same scsi_pkt is used without 14100 * intervening scsi_dma_free(). Since uscsi command does 14101 * not call scsi_dmafree() before retry failed command, it 14102 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14103 * set such that scsi_vhci can use other available path for 14104 * retry. Besides, ucsci command does not allow DMA breakup, 14105 * so there is no need to set PKT_DMA_PARTIAL flag. 14106 */ 14107 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14108 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14109 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14110 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 14111 - sizeof (struct scsi_extended_sense)), 0, 14112 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 14113 sdrunout, (caddr_t)un); 14114 } else { 14115 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14116 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14117 sizeof (struct scsi_arq_status), 0, 14118 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14119 sdrunout, (caddr_t)un); 14120 } 14121 14122 if (pktp == NULL) { 14123 *pktpp = NULL; 14124 /* 14125 * Set the driver state to RWAIT to indicate the driver 14126 * is waiting on resource allocations. The driver will not 14127 * suspend, pm_suspend, or detatch while the state is RWAIT. 14128 */ 14129 New_state(un, SD_STATE_RWAIT); 14130 14131 SD_ERROR(SD_LOG_IO_CORE, un, 14132 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14133 14134 if ((bp->b_flags & B_ERROR) != 0) { 14135 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14136 } 14137 return (SD_PKT_ALLOC_FAILURE); 14138 } 14139 14140 /* 14141 * We do not do DMA breakup for USCSI commands, so return failure 14142 * here if all the needed DMA resources were not allocated. 14143 */ 14144 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14145 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14146 scsi_destroy_pkt(pktp); 14147 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14148 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14149 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14150 } 14151 14152 /* Init the cdb from the given uscsi struct */ 14153 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14154 uscmd->uscsi_cdb[0], 0, 0, 0); 14155 14156 SD_FILL_SCSI1_LUN(un, pktp); 14157 14158 /* 14159 * Set up the optional USCSI flags. See the uscsi (7I) man page 14160 * for listing of the supported flags. 14161 */ 14162 14163 if (uscmd->uscsi_flags & USCSI_SILENT) { 14164 flags |= FLAG_SILENT; 14165 } 14166 14167 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14168 flags |= FLAG_DIAGNOSE; 14169 } 14170 14171 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14172 flags |= FLAG_ISOLATE; 14173 } 14174 14175 if (un->un_f_is_fibre == FALSE) { 14176 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14177 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14178 } 14179 } 14180 14181 /* 14182 * Set the pkt flags here so we save time later. 14183 * Note: These flags are NOT in the uscsi man page!!! 14184 */ 14185 if (uscmd->uscsi_flags & USCSI_HEAD) { 14186 flags |= FLAG_HEAD; 14187 } 14188 14189 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14190 flags |= FLAG_NOINTR; 14191 } 14192 14193 /* 14194 * For tagged queueing, things get a bit complicated. 14195 * Check first for head of queue and last for ordered queue. 14196 * If neither head nor order, use the default driver tag flags. 14197 */ 14198 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14199 if (uscmd->uscsi_flags & USCSI_HTAG) { 14200 flags |= FLAG_HTAG; 14201 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14202 flags |= FLAG_OTAG; 14203 } else { 14204 flags |= un->un_tagflags & FLAG_TAGMASK; 14205 } 14206 } 14207 14208 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14209 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14210 } 14211 14212 pktp->pkt_flags = flags; 14213 14214 /* Transfer uscsi information to scsi_pkt */ 14215 (void) scsi_uscsi_pktinit(uscmd, pktp); 14216 14217 /* Copy the caller's CDB into the pkt... */ 14218 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14219 14220 if (uscmd->uscsi_timeout == 0) { 14221 pktp->pkt_time = un->un_uscsi_timeout; 14222 } else { 14223 pktp->pkt_time = uscmd->uscsi_timeout; 14224 } 14225 14226 /* need it later to identify USCSI request in sdintr */ 14227 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14228 14229 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14230 14231 pktp->pkt_private = bp; 14232 pktp->pkt_comp = sdintr; 14233 *pktpp = pktp; 14234 14235 SD_TRACE(SD_LOG_IO_CORE, un, 14236 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14237 14238 return (SD_PKT_ALLOC_SUCCESS); 14239 } 14240 14241 14242 /* 14243 * Function: sd_destroypkt_for_uscsi 14244 * 14245 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14246 * IOs.. Also saves relevant info into the associated uscsi_cmd 14247 * struct. 14248 * 14249 * Context: May be called under interrupt context 14250 */ 14251 14252 static void 14253 sd_destroypkt_for_uscsi(struct buf *bp) 14254 { 14255 struct uscsi_cmd *uscmd; 14256 struct sd_xbuf *xp; 14257 struct scsi_pkt *pktp; 14258 struct sd_lun *un; 14259 struct sd_uscsi_info *suip; 14260 14261 ASSERT(bp != NULL); 14262 xp = SD_GET_XBUF(bp); 14263 ASSERT(xp != NULL); 14264 un = SD_GET_UN(bp); 14265 ASSERT(un != NULL); 14266 ASSERT(!mutex_owned(SD_MUTEX(un))); 14267 pktp = SD_GET_PKTP(bp); 14268 ASSERT(pktp != NULL); 14269 14270 SD_TRACE(SD_LOG_IO_CORE, un, 14271 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14272 14273 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14274 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14275 ASSERT(uscmd != NULL); 14276 14277 /* Save the status and the residual into the uscsi_cmd struct */ 14278 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14279 uscmd->uscsi_resid = bp->b_resid; 14280 14281 /* Transfer scsi_pkt information to uscsi */ 14282 (void) scsi_uscsi_pktfini(pktp, uscmd); 14283 14284 /* 14285 * If enabled, copy any saved sense data into the area specified 14286 * by the uscsi command. 14287 */ 14288 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14289 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14290 /* 14291 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14292 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14293 */ 14294 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14295 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14296 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14297 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14298 MAX_SENSE_LENGTH); 14299 } else { 14300 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14301 SENSE_LENGTH); 14302 } 14303 } 14304 /* 14305 * The following assignments are for SCSI FMA. 14306 */ 14307 ASSERT(xp->xb_private != NULL); 14308 suip = (struct sd_uscsi_info *)xp->xb_private; 14309 suip->ui_pkt_reason = pktp->pkt_reason; 14310 suip->ui_pkt_state = pktp->pkt_state; 14311 suip->ui_pkt_statistics = pktp->pkt_statistics; 14312 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 14313 14314 /* We are done with the scsi_pkt; free it now */ 14315 ASSERT(SD_GET_PKTP(bp) != NULL); 14316 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14317 14318 SD_TRACE(SD_LOG_IO_CORE, un, 14319 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14320 } 14321 14322 14323 /* 14324 * Function: sd_bioclone_alloc 14325 * 14326 * Description: Allocate a buf(9S) and init it as per the given buf 14327 * and the various arguments. The associated sd_xbuf 14328 * struct is (nearly) duplicated. The struct buf *bp 14329 * argument is saved in new_xp->xb_private. 14330 * 14331 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14332 * datalen - size of data area for the shadow bp 14333 * blkno - starting LBA 14334 * func - function pointer for b_iodone in the shadow buf. (May 14335 * be NULL if none.) 14336 * 14337 * Return Code: Pointer to allocates buf(9S) struct 14338 * 14339 * Context: Can sleep. 14340 */ 14341 14342 static struct buf * 14343 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14344 daddr_t blkno, int (*func)(struct buf *)) 14345 { 14346 struct sd_lun *un; 14347 struct sd_xbuf *xp; 14348 struct sd_xbuf *new_xp; 14349 struct buf *new_bp; 14350 14351 ASSERT(bp != NULL); 14352 xp = SD_GET_XBUF(bp); 14353 ASSERT(xp != NULL); 14354 un = SD_GET_UN(bp); 14355 ASSERT(un != NULL); 14356 ASSERT(!mutex_owned(SD_MUTEX(un))); 14357 14358 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14359 NULL, KM_SLEEP); 14360 14361 new_bp->b_lblkno = blkno; 14362 14363 /* 14364 * Allocate an xbuf for the shadow bp and copy the contents of the 14365 * original xbuf into it. 14366 */ 14367 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14368 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14369 14370 /* 14371 * The given bp is automatically saved in the xb_private member 14372 * of the new xbuf. Callers are allowed to depend on this. 14373 */ 14374 new_xp->xb_private = bp; 14375 14376 new_bp->b_private = new_xp; 14377 14378 return (new_bp); 14379 } 14380 14381 /* 14382 * Function: sd_shadow_buf_alloc 14383 * 14384 * Description: Allocate a buf(9S) and init it as per the given buf 14385 * and the various arguments. The associated sd_xbuf 14386 * struct is (nearly) duplicated. The struct buf *bp 14387 * argument is saved in new_xp->xb_private. 14388 * 14389 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14390 * datalen - size of data area for the shadow bp 14391 * bflags - B_READ or B_WRITE (pseudo flag) 14392 * blkno - starting LBA 14393 * func - function pointer for b_iodone in the shadow buf. (May 14394 * be NULL if none.) 14395 * 14396 * Return Code: Pointer to allocates buf(9S) struct 14397 * 14398 * Context: Can sleep. 14399 */ 14400 14401 static struct buf * 14402 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14403 daddr_t blkno, int (*func)(struct buf *)) 14404 { 14405 struct sd_lun *un; 14406 struct sd_xbuf *xp; 14407 struct sd_xbuf *new_xp; 14408 struct buf *new_bp; 14409 14410 ASSERT(bp != NULL); 14411 xp = SD_GET_XBUF(bp); 14412 ASSERT(xp != NULL); 14413 un = SD_GET_UN(bp); 14414 ASSERT(un != NULL); 14415 ASSERT(!mutex_owned(SD_MUTEX(un))); 14416 14417 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14418 bp_mapin(bp); 14419 } 14420 14421 bflags &= (B_READ | B_WRITE); 14422 #if defined(__i386) || defined(__amd64) 14423 new_bp = getrbuf(KM_SLEEP); 14424 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14425 new_bp->b_bcount = datalen; 14426 new_bp->b_flags = bflags | 14427 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14428 #else 14429 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14430 datalen, bflags, SLEEP_FUNC, NULL); 14431 #endif 14432 new_bp->av_forw = NULL; 14433 new_bp->av_back = NULL; 14434 new_bp->b_dev = bp->b_dev; 14435 new_bp->b_blkno = blkno; 14436 new_bp->b_iodone = func; 14437 new_bp->b_edev = bp->b_edev; 14438 new_bp->b_resid = 0; 14439 14440 /* We need to preserve the B_FAILFAST flag */ 14441 if (bp->b_flags & B_FAILFAST) { 14442 new_bp->b_flags |= B_FAILFAST; 14443 } 14444 14445 /* 14446 * Allocate an xbuf for the shadow bp and copy the contents of the 14447 * original xbuf into it. 14448 */ 14449 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14450 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14451 14452 /* Need later to copy data between the shadow buf & original buf! */ 14453 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14454 14455 /* 14456 * The given bp is automatically saved in the xb_private member 14457 * of the new xbuf. Callers are allowed to depend on this. 14458 */ 14459 new_xp->xb_private = bp; 14460 14461 new_bp->b_private = new_xp; 14462 14463 return (new_bp); 14464 } 14465 14466 /* 14467 * Function: sd_bioclone_free 14468 * 14469 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14470 * in the larger than partition operation. 14471 * 14472 * Context: May be called under interrupt context 14473 */ 14474 14475 static void 14476 sd_bioclone_free(struct buf *bp) 14477 { 14478 struct sd_xbuf *xp; 14479 14480 ASSERT(bp != NULL); 14481 xp = SD_GET_XBUF(bp); 14482 ASSERT(xp != NULL); 14483 14484 /* 14485 * Call bp_mapout() before freeing the buf, in case a lower 14486 * layer or HBA had done a bp_mapin(). we must do this here 14487 * as we are the "originator" of the shadow buf. 14488 */ 14489 bp_mapout(bp); 14490 14491 /* 14492 * Null out b_iodone before freeing the bp, to ensure that the driver 14493 * never gets confused by a stale value in this field. (Just a little 14494 * extra defensiveness here.) 14495 */ 14496 bp->b_iodone = NULL; 14497 14498 freerbuf(bp); 14499 14500 kmem_free(xp, sizeof (struct sd_xbuf)); 14501 } 14502 14503 /* 14504 * Function: sd_shadow_buf_free 14505 * 14506 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14507 * 14508 * Context: May be called under interrupt context 14509 */ 14510 14511 static void 14512 sd_shadow_buf_free(struct buf *bp) 14513 { 14514 struct sd_xbuf *xp; 14515 14516 ASSERT(bp != NULL); 14517 xp = SD_GET_XBUF(bp); 14518 ASSERT(xp != NULL); 14519 14520 #if defined(__sparc) 14521 /* 14522 * Call bp_mapout() before freeing the buf, in case a lower 14523 * layer or HBA had done a bp_mapin(). we must do this here 14524 * as we are the "originator" of the shadow buf. 14525 */ 14526 bp_mapout(bp); 14527 #endif 14528 14529 /* 14530 * Null out b_iodone before freeing the bp, to ensure that the driver 14531 * never gets confused by a stale value in this field. (Just a little 14532 * extra defensiveness here.) 14533 */ 14534 bp->b_iodone = NULL; 14535 14536 #if defined(__i386) || defined(__amd64) 14537 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14538 freerbuf(bp); 14539 #else 14540 scsi_free_consistent_buf(bp); 14541 #endif 14542 14543 kmem_free(xp, sizeof (struct sd_xbuf)); 14544 } 14545 14546 14547 /* 14548 * Function: sd_print_transport_rejected_message 14549 * 14550 * Description: This implements the ludicrously complex rules for printing 14551 * a "transport rejected" message. This is to address the 14552 * specific problem of having a flood of this error message 14553 * produced when a failover occurs. 14554 * 14555 * Context: Any. 14556 */ 14557 14558 static void 14559 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14560 int code) 14561 { 14562 ASSERT(un != NULL); 14563 ASSERT(mutex_owned(SD_MUTEX(un))); 14564 ASSERT(xp != NULL); 14565 14566 /* 14567 * Print the "transport rejected" message under the following 14568 * conditions: 14569 * 14570 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14571 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14572 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14573 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14574 * scsi_transport(9F) (which indicates that the target might have 14575 * gone off-line). This uses the un->un_tran_fatal_count 14576 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14577 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14578 * from scsi_transport(). 14579 * 14580 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14581 * the preceeding cases in order for the message to be printed. 14582 */ 14583 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) && 14584 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) { 14585 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14586 (code != TRAN_FATAL_ERROR) || 14587 (un->un_tran_fatal_count == 1)) { 14588 switch (code) { 14589 case TRAN_BADPKT: 14590 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14591 "transport rejected bad packet\n"); 14592 break; 14593 case TRAN_FATAL_ERROR: 14594 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14595 "transport rejected fatal error\n"); 14596 break; 14597 default: 14598 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14599 "transport rejected (%d)\n", code); 14600 break; 14601 } 14602 } 14603 } 14604 } 14605 14606 14607 /* 14608 * Function: sd_add_buf_to_waitq 14609 * 14610 * Description: Add the given buf(9S) struct to the wait queue for the 14611 * instance. If sorting is enabled, then the buf is added 14612 * to the queue via an elevator sort algorithm (a la 14613 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14614 * If sorting is not enabled, then the buf is just added 14615 * to the end of the wait queue. 14616 * 14617 * Return Code: void 14618 * 14619 * Context: Does not sleep/block, therefore technically can be called 14620 * from any context. However if sorting is enabled then the 14621 * execution time is indeterminate, and may take long if 14622 * the wait queue grows large. 14623 */ 14624 14625 static void 14626 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14627 { 14628 struct buf *ap; 14629 14630 ASSERT(bp != NULL); 14631 ASSERT(un != NULL); 14632 ASSERT(mutex_owned(SD_MUTEX(un))); 14633 14634 /* If the queue is empty, add the buf as the only entry & return. */ 14635 if (un->un_waitq_headp == NULL) { 14636 ASSERT(un->un_waitq_tailp == NULL); 14637 un->un_waitq_headp = un->un_waitq_tailp = bp; 14638 bp->av_forw = NULL; 14639 return; 14640 } 14641 14642 ASSERT(un->un_waitq_tailp != NULL); 14643 14644 /* 14645 * If sorting is disabled, just add the buf to the tail end of 14646 * the wait queue and return. 14647 */ 14648 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) { 14649 un->un_waitq_tailp->av_forw = bp; 14650 un->un_waitq_tailp = bp; 14651 bp->av_forw = NULL; 14652 return; 14653 } 14654 14655 /* 14656 * Sort thru the list of requests currently on the wait queue 14657 * and add the new buf request at the appropriate position. 14658 * 14659 * The un->un_waitq_headp is an activity chain pointer on which 14660 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14661 * first queue holds those requests which are positioned after 14662 * the current SD_GET_BLKNO() (in the first request); the second holds 14663 * requests which came in after their SD_GET_BLKNO() number was passed. 14664 * Thus we implement a one way scan, retracting after reaching 14665 * the end of the drive to the first request on the second 14666 * queue, at which time it becomes the first queue. 14667 * A one-way scan is natural because of the way UNIX read-ahead 14668 * blocks are allocated. 14669 * 14670 * If we lie after the first request, then we must locate the 14671 * second request list and add ourselves to it. 14672 */ 14673 ap = un->un_waitq_headp; 14674 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14675 while (ap->av_forw != NULL) { 14676 /* 14677 * Look for an "inversion" in the (normally 14678 * ascending) block numbers. This indicates 14679 * the start of the second request list. 14680 */ 14681 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14682 /* 14683 * Search the second request list for the 14684 * first request at a larger block number. 14685 * We go before that; however if there is 14686 * no such request, we go at the end. 14687 */ 14688 do { 14689 if (SD_GET_BLKNO(bp) < 14690 SD_GET_BLKNO(ap->av_forw)) { 14691 goto insert; 14692 } 14693 ap = ap->av_forw; 14694 } while (ap->av_forw != NULL); 14695 goto insert; /* after last */ 14696 } 14697 ap = ap->av_forw; 14698 } 14699 14700 /* 14701 * No inversions... we will go after the last, and 14702 * be the first request in the second request list. 14703 */ 14704 goto insert; 14705 } 14706 14707 /* 14708 * Request is at/after the current request... 14709 * sort in the first request list. 14710 */ 14711 while (ap->av_forw != NULL) { 14712 /* 14713 * We want to go after the current request (1) if 14714 * there is an inversion after it (i.e. it is the end 14715 * of the first request list), or (2) if the next 14716 * request is a larger block no. than our request. 14717 */ 14718 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14719 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14720 goto insert; 14721 } 14722 ap = ap->av_forw; 14723 } 14724 14725 /* 14726 * Neither a second list nor a larger request, therefore 14727 * we go at the end of the first list (which is the same 14728 * as the end of the whole schebang). 14729 */ 14730 insert: 14731 bp->av_forw = ap->av_forw; 14732 ap->av_forw = bp; 14733 14734 /* 14735 * If we inserted onto the tail end of the waitq, make sure the 14736 * tail pointer is updated. 14737 */ 14738 if (ap == un->un_waitq_tailp) { 14739 un->un_waitq_tailp = bp; 14740 } 14741 } 14742 14743 14744 /* 14745 * Function: sd_start_cmds 14746 * 14747 * Description: Remove and transport cmds from the driver queues. 14748 * 14749 * Arguments: un - pointer to the unit (soft state) struct for the target. 14750 * 14751 * immed_bp - ptr to a buf to be transported immediately. Only 14752 * the immed_bp is transported; bufs on the waitq are not 14753 * processed and the un_retry_bp is not checked. If immed_bp is 14754 * NULL, then normal queue processing is performed. 14755 * 14756 * Context: May be called from kernel thread context, interrupt context, 14757 * or runout callback context. This function may not block or 14758 * call routines that block. 14759 */ 14760 14761 static void 14762 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14763 { 14764 struct sd_xbuf *xp; 14765 struct buf *bp; 14766 void (*statp)(kstat_io_t *); 14767 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14768 void (*saved_statp)(kstat_io_t *); 14769 #endif 14770 int rval; 14771 struct sd_fm_internal *sfip = NULL; 14772 14773 ASSERT(un != NULL); 14774 ASSERT(mutex_owned(SD_MUTEX(un))); 14775 ASSERT(un->un_ncmds_in_transport >= 0); 14776 ASSERT(un->un_throttle >= 0); 14777 14778 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14779 14780 do { 14781 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14782 saved_statp = NULL; 14783 #endif 14784 14785 /* 14786 * If we are syncing or dumping, fail the command to 14787 * avoid recursively calling back into scsi_transport(). 14788 * The dump I/O itself uses a separate code path so this 14789 * only prevents non-dump I/O from being sent while dumping. 14790 * File system sync takes place before dumping begins. 14791 * During panic, filesystem I/O is allowed provided 14792 * un_in_callback is <= 1. This is to prevent recursion 14793 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14794 * sd_start_cmds and so on. See panic.c for more information 14795 * about the states the system can be in during panic. 14796 */ 14797 if ((un->un_state == SD_STATE_DUMPING) || 14798 (ddi_in_panic() && (un->un_in_callback > 1))) { 14799 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14800 "sd_start_cmds: panicking\n"); 14801 goto exit; 14802 } 14803 14804 if ((bp = immed_bp) != NULL) { 14805 /* 14806 * We have a bp that must be transported immediately. 14807 * It's OK to transport the immed_bp here without doing 14808 * the throttle limit check because the immed_bp is 14809 * always used in a retry/recovery case. This means 14810 * that we know we are not at the throttle limit by 14811 * virtue of the fact that to get here we must have 14812 * already gotten a command back via sdintr(). This also 14813 * relies on (1) the command on un_retry_bp preventing 14814 * further commands from the waitq from being issued; 14815 * and (2) the code in sd_retry_command checking the 14816 * throttle limit before issuing a delayed or immediate 14817 * retry. This holds even if the throttle limit is 14818 * currently ratcheted down from its maximum value. 14819 */ 14820 statp = kstat_runq_enter; 14821 if (bp == un->un_retry_bp) { 14822 ASSERT((un->un_retry_statp == NULL) || 14823 (un->un_retry_statp == kstat_waitq_enter) || 14824 (un->un_retry_statp == 14825 kstat_runq_back_to_waitq)); 14826 /* 14827 * If the waitq kstat was incremented when 14828 * sd_set_retry_bp() queued this bp for a retry, 14829 * then we must set up statp so that the waitq 14830 * count will get decremented correctly below. 14831 * Also we must clear un->un_retry_statp to 14832 * ensure that we do not act on a stale value 14833 * in this field. 14834 */ 14835 if ((un->un_retry_statp == kstat_waitq_enter) || 14836 (un->un_retry_statp == 14837 kstat_runq_back_to_waitq)) { 14838 statp = kstat_waitq_to_runq; 14839 } 14840 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14841 saved_statp = un->un_retry_statp; 14842 #endif 14843 un->un_retry_statp = NULL; 14844 14845 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14846 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14847 "un_throttle:%d un_ncmds_in_transport:%d\n", 14848 un, un->un_retry_bp, un->un_throttle, 14849 un->un_ncmds_in_transport); 14850 } else { 14851 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14852 "processing priority bp:0x%p\n", bp); 14853 } 14854 14855 } else if ((bp = un->un_waitq_headp) != NULL) { 14856 /* 14857 * A command on the waitq is ready to go, but do not 14858 * send it if: 14859 * 14860 * (1) the throttle limit has been reached, or 14861 * (2) a retry is pending, or 14862 * (3) a START_STOP_UNIT callback pending, or 14863 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14864 * command is pending. 14865 * 14866 * For all of these conditions, IO processing will 14867 * restart after the condition is cleared. 14868 */ 14869 if (un->un_ncmds_in_transport >= un->un_throttle) { 14870 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14871 "sd_start_cmds: exiting, " 14872 "throttle limit reached!\n"); 14873 goto exit; 14874 } 14875 if (un->un_retry_bp != NULL) { 14876 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14877 "sd_start_cmds: exiting, retry pending!\n"); 14878 goto exit; 14879 } 14880 if (un->un_startstop_timeid != NULL) { 14881 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14882 "sd_start_cmds: exiting, " 14883 "START_STOP pending!\n"); 14884 goto exit; 14885 } 14886 if (un->un_direct_priority_timeid != NULL) { 14887 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14888 "sd_start_cmds: exiting, " 14889 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14890 goto exit; 14891 } 14892 14893 /* Dequeue the command */ 14894 un->un_waitq_headp = bp->av_forw; 14895 if (un->un_waitq_headp == NULL) { 14896 un->un_waitq_tailp = NULL; 14897 } 14898 bp->av_forw = NULL; 14899 statp = kstat_waitq_to_runq; 14900 SD_TRACE(SD_LOG_IO_CORE, un, 14901 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14902 14903 } else { 14904 /* No work to do so bail out now */ 14905 SD_TRACE(SD_LOG_IO_CORE, un, 14906 "sd_start_cmds: no more work, exiting!\n"); 14907 goto exit; 14908 } 14909 14910 /* 14911 * Reset the state to normal. This is the mechanism by which 14912 * the state transitions from either SD_STATE_RWAIT or 14913 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14914 * If state is SD_STATE_PM_CHANGING then this command is 14915 * part of the device power control and the state must 14916 * not be put back to normal. Doing so would would 14917 * allow new commands to proceed when they shouldn't, 14918 * the device may be going off. 14919 */ 14920 if ((un->un_state != SD_STATE_SUSPENDED) && 14921 (un->un_state != SD_STATE_PM_CHANGING)) { 14922 New_state(un, SD_STATE_NORMAL); 14923 } 14924 14925 xp = SD_GET_XBUF(bp); 14926 ASSERT(xp != NULL); 14927 14928 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14929 /* 14930 * Allocate the scsi_pkt if we need one, or attach DMA 14931 * resources if we have a scsi_pkt that needs them. The 14932 * latter should only occur for commands that are being 14933 * retried. 14934 */ 14935 if ((xp->xb_pktp == NULL) || 14936 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14937 #else 14938 if (xp->xb_pktp == NULL) { 14939 #endif 14940 /* 14941 * There is no scsi_pkt allocated for this buf. Call 14942 * the initpkt function to allocate & init one. 14943 * 14944 * The scsi_init_pkt runout callback functionality is 14945 * implemented as follows: 14946 * 14947 * 1) The initpkt function always calls 14948 * scsi_init_pkt(9F) with sdrunout specified as the 14949 * callback routine. 14950 * 2) A successful packet allocation is initialized and 14951 * the I/O is transported. 14952 * 3) The I/O associated with an allocation resource 14953 * failure is left on its queue to be retried via 14954 * runout or the next I/O. 14955 * 4) The I/O associated with a DMA error is removed 14956 * from the queue and failed with EIO. Processing of 14957 * the transport queues is also halted to be 14958 * restarted via runout or the next I/O. 14959 * 5) The I/O associated with a CDB size or packet 14960 * size error is removed from the queue and failed 14961 * with EIO. Processing of the transport queues is 14962 * continued. 14963 * 14964 * Note: there is no interface for canceling a runout 14965 * callback. To prevent the driver from detaching or 14966 * suspending while a runout is pending the driver 14967 * state is set to SD_STATE_RWAIT 14968 * 14969 * Note: using the scsi_init_pkt callback facility can 14970 * result in an I/O request persisting at the head of 14971 * the list which cannot be satisfied even after 14972 * multiple retries. In the future the driver may 14973 * implement some kind of maximum runout count before 14974 * failing an I/O. 14975 * 14976 * Note: the use of funcp below may seem superfluous, 14977 * but it helps warlock figure out the correct 14978 * initpkt function calls (see [s]sd.wlcmd). 14979 */ 14980 struct scsi_pkt *pktp; 14981 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14982 14983 ASSERT(bp != un->un_rqs_bp); 14984 14985 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14986 switch ((*funcp)(bp, &pktp)) { 14987 case SD_PKT_ALLOC_SUCCESS: 14988 xp->xb_pktp = pktp; 14989 SD_TRACE(SD_LOG_IO_CORE, un, 14990 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14991 pktp); 14992 goto got_pkt; 14993 14994 case SD_PKT_ALLOC_FAILURE: 14995 /* 14996 * Temporary (hopefully) resource depletion. 14997 * Since retries and RQS commands always have a 14998 * scsi_pkt allocated, these cases should never 14999 * get here. So the only cases this needs to 15000 * handle is a bp from the waitq (which we put 15001 * back onto the waitq for sdrunout), or a bp 15002 * sent as an immed_bp (which we just fail). 15003 */ 15004 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15005 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 15006 15007 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15008 15009 if (bp == immed_bp) { 15010 /* 15011 * If SD_XB_DMA_FREED is clear, then 15012 * this is a failure to allocate a 15013 * scsi_pkt, and we must fail the 15014 * command. 15015 */ 15016 if ((xp->xb_pkt_flags & 15017 SD_XB_DMA_FREED) == 0) { 15018 break; 15019 } 15020 15021 /* 15022 * If this immediate command is NOT our 15023 * un_retry_bp, then we must fail it. 15024 */ 15025 if (bp != un->un_retry_bp) { 15026 break; 15027 } 15028 15029 /* 15030 * We get here if this cmd is our 15031 * un_retry_bp that was DMAFREED, but 15032 * scsi_init_pkt() failed to reallocate 15033 * DMA resources when we attempted to 15034 * retry it. This can happen when an 15035 * mpxio failover is in progress, but 15036 * we don't want to just fail the 15037 * command in this case. 15038 * 15039 * Use timeout(9F) to restart it after 15040 * a 100ms delay. We don't want to 15041 * let sdrunout() restart it, because 15042 * sdrunout() is just supposed to start 15043 * commands that are sitting on the 15044 * wait queue. The un_retry_bp stays 15045 * set until the command completes, but 15046 * sdrunout can be called many times 15047 * before that happens. Since sdrunout 15048 * cannot tell if the un_retry_bp is 15049 * already in the transport, it could 15050 * end up calling scsi_transport() for 15051 * the un_retry_bp multiple times. 15052 * 15053 * Also: don't schedule the callback 15054 * if some other callback is already 15055 * pending. 15056 */ 15057 if (un->un_retry_statp == NULL) { 15058 /* 15059 * restore the kstat pointer to 15060 * keep kstat counts coherent 15061 * when we do retry the command. 15062 */ 15063 un->un_retry_statp = 15064 saved_statp; 15065 } 15066 15067 if ((un->un_startstop_timeid == NULL) && 15068 (un->un_retry_timeid == NULL) && 15069 (un->un_direct_priority_timeid == 15070 NULL)) { 15071 15072 un->un_retry_timeid = 15073 timeout( 15074 sd_start_retry_command, 15075 un, SD_RESTART_TIMEOUT); 15076 } 15077 goto exit; 15078 } 15079 15080 #else 15081 if (bp == immed_bp) { 15082 break; /* Just fail the command */ 15083 } 15084 #endif 15085 15086 /* Add the buf back to the head of the waitq */ 15087 bp->av_forw = un->un_waitq_headp; 15088 un->un_waitq_headp = bp; 15089 if (un->un_waitq_tailp == NULL) { 15090 un->un_waitq_tailp = bp; 15091 } 15092 goto exit; 15093 15094 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15095 /* 15096 * HBA DMA resource failure. Fail the command 15097 * and continue processing of the queues. 15098 */ 15099 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15100 "sd_start_cmds: " 15101 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15102 break; 15103 15104 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15105 /* 15106 * Note:x86: Partial DMA mapping not supported 15107 * for USCSI commands, and all the needed DMA 15108 * resources were not allocated. 15109 */ 15110 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15111 "sd_start_cmds: " 15112 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15113 break; 15114 15115 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15116 /* 15117 * Note:x86: Request cannot fit into CDB based 15118 * on lba and len. 15119 */ 15120 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15121 "sd_start_cmds: " 15122 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15123 break; 15124 15125 default: 15126 /* Should NEVER get here! */ 15127 panic("scsi_initpkt error"); 15128 /*NOTREACHED*/ 15129 } 15130 15131 /* 15132 * Fatal error in allocating a scsi_pkt for this buf. 15133 * Update kstats & return the buf with an error code. 15134 * We must use sd_return_failed_command_no_restart() to 15135 * avoid a recursive call back into sd_start_cmds(). 15136 * However this also means that we must keep processing 15137 * the waitq here in order to avoid stalling. 15138 */ 15139 if (statp == kstat_waitq_to_runq) { 15140 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15141 } 15142 sd_return_failed_command_no_restart(un, bp, EIO); 15143 if (bp == immed_bp) { 15144 /* immed_bp is gone by now, so clear this */ 15145 immed_bp = NULL; 15146 } 15147 continue; 15148 } 15149 got_pkt: 15150 if (bp == immed_bp) { 15151 /* goto the head of the class.... */ 15152 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15153 } 15154 15155 un->un_ncmds_in_transport++; 15156 SD_UPDATE_KSTATS(un, statp, bp); 15157 15158 /* 15159 * Call scsi_transport() to send the command to the target. 15160 * According to SCSA architecture, we must drop the mutex here 15161 * before calling scsi_transport() in order to avoid deadlock. 15162 * Note that the scsi_pkt's completion routine can be executed 15163 * (from interrupt context) even before the call to 15164 * scsi_transport() returns. 15165 */ 15166 SD_TRACE(SD_LOG_IO_CORE, un, 15167 "sd_start_cmds: calling scsi_transport()\n"); 15168 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15169 15170 mutex_exit(SD_MUTEX(un)); 15171 rval = scsi_transport(xp->xb_pktp); 15172 mutex_enter(SD_MUTEX(un)); 15173 15174 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15175 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15176 15177 switch (rval) { 15178 case TRAN_ACCEPT: 15179 /* Clear this with every pkt accepted by the HBA */ 15180 un->un_tran_fatal_count = 0; 15181 break; /* Success; try the next cmd (if any) */ 15182 15183 case TRAN_BUSY: 15184 un->un_ncmds_in_transport--; 15185 ASSERT(un->un_ncmds_in_transport >= 0); 15186 15187 /* 15188 * Don't retry request sense, the sense data 15189 * is lost when another request is sent. 15190 * Free up the rqs buf and retry 15191 * the original failed cmd. Update kstat. 15192 */ 15193 if (bp == un->un_rqs_bp) { 15194 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15195 bp = sd_mark_rqs_idle(un, xp); 15196 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15197 NULL, NULL, EIO, un->un_busy_timeout / 500, 15198 kstat_waitq_enter); 15199 goto exit; 15200 } 15201 15202 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15203 /* 15204 * Free the DMA resources for the scsi_pkt. This will 15205 * allow mpxio to select another path the next time 15206 * we call scsi_transport() with this scsi_pkt. 15207 * See sdintr() for the rationalization behind this. 15208 */ 15209 if ((un->un_f_is_fibre == TRUE) && 15210 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15211 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15212 scsi_dmafree(xp->xb_pktp); 15213 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15214 } 15215 #endif 15216 15217 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15218 /* 15219 * Commands that are SD_PATH_DIRECT_PRIORITY 15220 * are for error recovery situations. These do 15221 * not use the normal command waitq, so if they 15222 * get a TRAN_BUSY we cannot put them back onto 15223 * the waitq for later retry. One possible 15224 * problem is that there could already be some 15225 * other command on un_retry_bp that is waiting 15226 * for this one to complete, so we would be 15227 * deadlocked if we put this command back onto 15228 * the waitq for later retry (since un_retry_bp 15229 * must complete before the driver gets back to 15230 * commands on the waitq). 15231 * 15232 * To avoid deadlock we must schedule a callback 15233 * that will restart this command after a set 15234 * interval. This should keep retrying for as 15235 * long as the underlying transport keeps 15236 * returning TRAN_BUSY (just like for other 15237 * commands). Use the same timeout interval as 15238 * for the ordinary TRAN_BUSY retry. 15239 */ 15240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15241 "sd_start_cmds: scsi_transport() returned " 15242 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15243 15244 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15245 un->un_direct_priority_timeid = 15246 timeout(sd_start_direct_priority_command, 15247 bp, un->un_busy_timeout / 500); 15248 15249 goto exit; 15250 } 15251 15252 /* 15253 * For TRAN_BUSY, we want to reduce the throttle value, 15254 * unless we are retrying a command. 15255 */ 15256 if (bp != un->un_retry_bp) { 15257 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15258 } 15259 15260 /* 15261 * Set up the bp to be tried again 10 ms later. 15262 * Note:x86: Is there a timeout value in the sd_lun 15263 * for this condition? 15264 */ 15265 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500, 15266 kstat_runq_back_to_waitq); 15267 goto exit; 15268 15269 case TRAN_FATAL_ERROR: 15270 un->un_tran_fatal_count++; 15271 /* FALLTHRU */ 15272 15273 case TRAN_BADPKT: 15274 default: 15275 un->un_ncmds_in_transport--; 15276 ASSERT(un->un_ncmds_in_transport >= 0); 15277 15278 /* 15279 * If this is our REQUEST SENSE command with a 15280 * transport error, we must get back the pointers 15281 * to the original buf, and mark the REQUEST 15282 * SENSE command as "available". 15283 */ 15284 if (bp == un->un_rqs_bp) { 15285 bp = sd_mark_rqs_idle(un, xp); 15286 xp = SD_GET_XBUF(bp); 15287 } else { 15288 /* 15289 * Legacy behavior: do not update transport 15290 * error count for request sense commands. 15291 */ 15292 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15293 } 15294 15295 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15296 sd_print_transport_rejected_message(un, xp, rval); 15297 15298 /* 15299 * This command will be terminated by SD driver due 15300 * to a fatal transport error. We should post 15301 * ereport.io.scsi.cmd.disk.tran with driver-assessment 15302 * of "fail" for any command to indicate this 15303 * situation. 15304 */ 15305 if (xp->xb_ena > 0) { 15306 ASSERT(un->un_fm_private != NULL); 15307 sfip = un->un_fm_private; 15308 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT; 15309 sd_ssc_extract_info(&sfip->fm_ssc, un, 15310 xp->xb_pktp, bp, xp); 15311 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15312 } 15313 15314 /* 15315 * We must use sd_return_failed_command_no_restart() to 15316 * avoid a recursive call back into sd_start_cmds(). 15317 * However this also means that we must keep processing 15318 * the waitq here in order to avoid stalling. 15319 */ 15320 sd_return_failed_command_no_restart(un, bp, EIO); 15321 15322 /* 15323 * Notify any threads waiting in sd_ddi_suspend() that 15324 * a command completion has occurred. 15325 */ 15326 if (un->un_state == SD_STATE_SUSPENDED) { 15327 cv_broadcast(&un->un_disk_busy_cv); 15328 } 15329 15330 if (bp == immed_bp) { 15331 /* immed_bp is gone by now, so clear this */ 15332 immed_bp = NULL; 15333 } 15334 break; 15335 } 15336 15337 } while (immed_bp == NULL); 15338 15339 exit: 15340 ASSERT(mutex_owned(SD_MUTEX(un))); 15341 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15342 } 15343 15344 15345 /* 15346 * Function: sd_return_command 15347 * 15348 * Description: Returns a command to its originator (with or without an 15349 * error). Also starts commands waiting to be transported 15350 * to the target. 15351 * 15352 * Context: May be called from interrupt, kernel, or timeout context 15353 */ 15354 15355 static void 15356 sd_return_command(struct sd_lun *un, struct buf *bp) 15357 { 15358 struct sd_xbuf *xp; 15359 struct scsi_pkt *pktp; 15360 struct sd_fm_internal *sfip; 15361 15362 ASSERT(bp != NULL); 15363 ASSERT(un != NULL); 15364 ASSERT(mutex_owned(SD_MUTEX(un))); 15365 ASSERT(bp != un->un_rqs_bp); 15366 xp = SD_GET_XBUF(bp); 15367 ASSERT(xp != NULL); 15368 15369 pktp = SD_GET_PKTP(bp); 15370 sfip = (struct sd_fm_internal *)un->un_fm_private; 15371 ASSERT(sfip != NULL); 15372 15373 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15374 15375 /* 15376 * Note: check for the "sdrestart failed" case. 15377 */ 15378 if ((un->un_partial_dma_supported == 1) && 15379 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15380 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15381 (xp->xb_pktp->pkt_resid == 0)) { 15382 15383 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15384 /* 15385 * Successfully set up next portion of cmd 15386 * transfer, try sending it 15387 */ 15388 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15389 NULL, NULL, 0, (clock_t)0, NULL); 15390 sd_start_cmds(un, NULL); 15391 return; /* Note:x86: need a return here? */ 15392 } 15393 } 15394 15395 /* 15396 * If this is the failfast bp, clear it from un_failfast_bp. This 15397 * can happen if upon being re-tried the failfast bp either 15398 * succeeded or encountered another error (possibly even a different 15399 * error than the one that precipitated the failfast state, but in 15400 * that case it would have had to exhaust retries as well). Regardless, 15401 * this should not occur whenever the instance is in the active 15402 * failfast state. 15403 */ 15404 if (bp == un->un_failfast_bp) { 15405 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15406 un->un_failfast_bp = NULL; 15407 } 15408 15409 /* 15410 * Clear the failfast state upon successful completion of ANY cmd. 15411 */ 15412 if (bp->b_error == 0) { 15413 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15414 /* 15415 * If this is a successful command, but used to be retried, 15416 * we will take it as a recovered command and post an 15417 * ereport with driver-assessment of "recovered". 15418 */ 15419 if (xp->xb_ena > 0) { 15420 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15421 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY); 15422 } 15423 } else { 15424 /* 15425 * If this is a failed non-USCSI command we will post an 15426 * ereport with driver-assessment set accordingly("fail" or 15427 * "fatal"). 15428 */ 15429 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15430 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15431 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15432 } 15433 } 15434 15435 /* 15436 * This is used if the command was retried one or more times. Show that 15437 * we are done with it, and allow processing of the waitq to resume. 15438 */ 15439 if (bp == un->un_retry_bp) { 15440 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15441 "sd_return_command: un:0x%p: " 15442 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15443 un->un_retry_bp = NULL; 15444 un->un_retry_statp = NULL; 15445 } 15446 15447 SD_UPDATE_RDWR_STATS(un, bp); 15448 SD_UPDATE_PARTITION_STATS(un, bp); 15449 15450 switch (un->un_state) { 15451 case SD_STATE_SUSPENDED: 15452 /* 15453 * Notify any threads waiting in sd_ddi_suspend() that 15454 * a command completion has occurred. 15455 */ 15456 cv_broadcast(&un->un_disk_busy_cv); 15457 break; 15458 default: 15459 sd_start_cmds(un, NULL); 15460 break; 15461 } 15462 15463 /* Return this command up the iodone chain to its originator. */ 15464 mutex_exit(SD_MUTEX(un)); 15465 15466 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15467 xp->xb_pktp = NULL; 15468 15469 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15470 15471 ASSERT(!mutex_owned(SD_MUTEX(un))); 15472 mutex_enter(SD_MUTEX(un)); 15473 15474 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15475 } 15476 15477 15478 /* 15479 * Function: sd_return_failed_command 15480 * 15481 * Description: Command completion when an error occurred. 15482 * 15483 * Context: May be called from interrupt context 15484 */ 15485 15486 static void 15487 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15488 { 15489 ASSERT(bp != NULL); 15490 ASSERT(un != NULL); 15491 ASSERT(mutex_owned(SD_MUTEX(un))); 15492 15493 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15494 "sd_return_failed_command: entry\n"); 15495 15496 /* 15497 * b_resid could already be nonzero due to a partial data 15498 * transfer, so do not change it here. 15499 */ 15500 SD_BIOERROR(bp, errcode); 15501 15502 sd_return_command(un, bp); 15503 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15504 "sd_return_failed_command: exit\n"); 15505 } 15506 15507 15508 /* 15509 * Function: sd_return_failed_command_no_restart 15510 * 15511 * Description: Same as sd_return_failed_command, but ensures that no 15512 * call back into sd_start_cmds will be issued. 15513 * 15514 * Context: May be called from interrupt context 15515 */ 15516 15517 static void 15518 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15519 int errcode) 15520 { 15521 struct sd_xbuf *xp; 15522 15523 ASSERT(bp != NULL); 15524 ASSERT(un != NULL); 15525 ASSERT(mutex_owned(SD_MUTEX(un))); 15526 xp = SD_GET_XBUF(bp); 15527 ASSERT(xp != NULL); 15528 ASSERT(errcode != 0); 15529 15530 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15531 "sd_return_failed_command_no_restart: entry\n"); 15532 15533 /* 15534 * b_resid could already be nonzero due to a partial data 15535 * transfer, so do not change it here. 15536 */ 15537 SD_BIOERROR(bp, errcode); 15538 15539 /* 15540 * If this is the failfast bp, clear it. This can happen if the 15541 * failfast bp encounterd a fatal error when we attempted to 15542 * re-try it (such as a scsi_transport(9F) failure). However 15543 * we should NOT be in an active failfast state if the failfast 15544 * bp is not NULL. 15545 */ 15546 if (bp == un->un_failfast_bp) { 15547 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15548 un->un_failfast_bp = NULL; 15549 } 15550 15551 if (bp == un->un_retry_bp) { 15552 /* 15553 * This command was retried one or more times. Show that we are 15554 * done with it, and allow processing of the waitq to resume. 15555 */ 15556 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15557 "sd_return_failed_command_no_restart: " 15558 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15559 un->un_retry_bp = NULL; 15560 un->un_retry_statp = NULL; 15561 } 15562 15563 SD_UPDATE_RDWR_STATS(un, bp); 15564 SD_UPDATE_PARTITION_STATS(un, bp); 15565 15566 mutex_exit(SD_MUTEX(un)); 15567 15568 if (xp->xb_pktp != NULL) { 15569 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15570 xp->xb_pktp = NULL; 15571 } 15572 15573 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15574 15575 mutex_enter(SD_MUTEX(un)); 15576 15577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15578 "sd_return_failed_command_no_restart: exit\n"); 15579 } 15580 15581 15582 /* 15583 * Function: sd_retry_command 15584 * 15585 * Description: queue up a command for retry, or (optionally) fail it 15586 * if retry counts are exhausted. 15587 * 15588 * Arguments: un - Pointer to the sd_lun struct for the target. 15589 * 15590 * bp - Pointer to the buf for the command to be retried. 15591 * 15592 * retry_check_flag - Flag to see which (if any) of the retry 15593 * counts should be decremented/checked. If the indicated 15594 * retry count is exhausted, then the command will not be 15595 * retried; it will be failed instead. This should use a 15596 * value equal to one of the following: 15597 * 15598 * SD_RETRIES_NOCHECK 15599 * SD_RESD_RETRIES_STANDARD 15600 * SD_RETRIES_VICTIM 15601 * 15602 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15603 * if the check should be made to see of FLAG_ISOLATE is set 15604 * in the pkt. If FLAG_ISOLATE is set, then the command is 15605 * not retried, it is simply failed. 15606 * 15607 * user_funcp - Ptr to function to call before dispatching the 15608 * command. May be NULL if no action needs to be performed. 15609 * (Primarily intended for printing messages.) 15610 * 15611 * user_arg - Optional argument to be passed along to 15612 * the user_funcp call. 15613 * 15614 * failure_code - errno return code to set in the bp if the 15615 * command is going to be failed. 15616 * 15617 * retry_delay - Retry delay interval in (clock_t) units. May 15618 * be zero which indicates that the retry should be retried 15619 * immediately (ie, without an intervening delay). 15620 * 15621 * statp - Ptr to kstat function to be updated if the command 15622 * is queued for a delayed retry. May be NULL if no kstat 15623 * update is desired. 15624 * 15625 * Context: May be called from interrupt context. 15626 */ 15627 15628 static void 15629 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15630 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15631 code), void *user_arg, int failure_code, clock_t retry_delay, 15632 void (*statp)(kstat_io_t *)) 15633 { 15634 struct sd_xbuf *xp; 15635 struct scsi_pkt *pktp; 15636 struct sd_fm_internal *sfip; 15637 15638 ASSERT(un != NULL); 15639 ASSERT(mutex_owned(SD_MUTEX(un))); 15640 ASSERT(bp != NULL); 15641 xp = SD_GET_XBUF(bp); 15642 ASSERT(xp != NULL); 15643 pktp = SD_GET_PKTP(bp); 15644 ASSERT(pktp != NULL); 15645 15646 sfip = (struct sd_fm_internal *)un->un_fm_private; 15647 ASSERT(sfip != NULL); 15648 15649 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15650 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15651 15652 /* 15653 * If we are syncing or dumping, fail the command to avoid 15654 * recursively calling back into scsi_transport(). 15655 */ 15656 if (ddi_in_panic()) { 15657 goto fail_command_no_log; 15658 } 15659 15660 /* 15661 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15662 * log an error and fail the command. 15663 */ 15664 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15665 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15666 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15667 sd_dump_memory(un, SD_LOG_IO, "CDB", 15668 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15669 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15670 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15671 goto fail_command; 15672 } 15673 15674 /* 15675 * If we are suspended, then put the command onto head of the 15676 * wait queue since we don't want to start more commands, and 15677 * clear the un_retry_bp. Next time when we are resumed, will 15678 * handle the command in the wait queue. 15679 */ 15680 switch (un->un_state) { 15681 case SD_STATE_SUSPENDED: 15682 case SD_STATE_DUMPING: 15683 bp->av_forw = un->un_waitq_headp; 15684 un->un_waitq_headp = bp; 15685 if (un->un_waitq_tailp == NULL) { 15686 un->un_waitq_tailp = bp; 15687 } 15688 if (bp == un->un_retry_bp) { 15689 un->un_retry_bp = NULL; 15690 un->un_retry_statp = NULL; 15691 } 15692 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15693 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15694 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15695 return; 15696 default: 15697 break; 15698 } 15699 15700 /* 15701 * If the caller wants us to check FLAG_ISOLATE, then see if that 15702 * is set; if it is then we do not want to retry the command. 15703 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15704 */ 15705 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15706 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15707 goto fail_command; 15708 } 15709 } 15710 15711 15712 /* 15713 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15714 * command timeout or a selection timeout has occurred. This means 15715 * that we were unable to establish an kind of communication with 15716 * the target, and subsequent retries and/or commands are likely 15717 * to encounter similar results and take a long time to complete. 15718 * 15719 * If this is a failfast error condition, we need to update the 15720 * failfast state, even if this bp does not have B_FAILFAST set. 15721 */ 15722 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15723 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15724 ASSERT(un->un_failfast_bp == NULL); 15725 /* 15726 * If we are already in the active failfast state, and 15727 * another failfast error condition has been detected, 15728 * then fail this command if it has B_FAILFAST set. 15729 * If B_FAILFAST is clear, then maintain the legacy 15730 * behavior of retrying heroically, even tho this will 15731 * take a lot more time to fail the command. 15732 */ 15733 if (bp->b_flags & B_FAILFAST) { 15734 goto fail_command; 15735 } 15736 } else { 15737 /* 15738 * We're not in the active failfast state, but we 15739 * have a failfast error condition, so we must begin 15740 * transition to the next state. We do this regardless 15741 * of whether or not this bp has B_FAILFAST set. 15742 */ 15743 if (un->un_failfast_bp == NULL) { 15744 /* 15745 * This is the first bp to meet a failfast 15746 * condition so save it on un_failfast_bp & 15747 * do normal retry processing. Do not enter 15748 * active failfast state yet. This marks 15749 * entry into the "failfast pending" state. 15750 */ 15751 un->un_failfast_bp = bp; 15752 15753 } else if (un->un_failfast_bp == bp) { 15754 /* 15755 * This is the second time *this* bp has 15756 * encountered a failfast error condition, 15757 * so enter active failfast state & flush 15758 * queues as appropriate. 15759 */ 15760 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15761 un->un_failfast_bp = NULL; 15762 sd_failfast_flushq(un); 15763 15764 /* 15765 * Fail this bp now if B_FAILFAST set; 15766 * otherwise continue with retries. (It would 15767 * be pretty ironic if this bp succeeded on a 15768 * subsequent retry after we just flushed all 15769 * the queues). 15770 */ 15771 if (bp->b_flags & B_FAILFAST) { 15772 goto fail_command; 15773 } 15774 15775 #if !defined(lint) && !defined(__lint) 15776 } else { 15777 /* 15778 * If neither of the preceeding conditionals 15779 * was true, it means that there is some 15780 * *other* bp that has met an inital failfast 15781 * condition and is currently either being 15782 * retried or is waiting to be retried. In 15783 * that case we should perform normal retry 15784 * processing on *this* bp, since there is a 15785 * chance that the current failfast condition 15786 * is transient and recoverable. If that does 15787 * not turn out to be the case, then retries 15788 * will be cleared when the wait queue is 15789 * flushed anyway. 15790 */ 15791 #endif 15792 } 15793 } 15794 } else { 15795 /* 15796 * SD_RETRIES_FAILFAST is clear, which indicates that we 15797 * likely were able to at least establish some level of 15798 * communication with the target and subsequent commands 15799 * and/or retries are likely to get through to the target, 15800 * In this case we want to be aggressive about clearing 15801 * the failfast state. Note that this does not affect 15802 * the "failfast pending" condition. 15803 */ 15804 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15805 } 15806 15807 15808 /* 15809 * Check the specified retry count to see if we can still do 15810 * any retries with this pkt before we should fail it. 15811 */ 15812 switch (retry_check_flag & SD_RETRIES_MASK) { 15813 case SD_RETRIES_VICTIM: 15814 /* 15815 * Check the victim retry count. If exhausted, then fall 15816 * thru & check against the standard retry count. 15817 */ 15818 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15819 /* Increment count & proceed with the retry */ 15820 xp->xb_victim_retry_count++; 15821 break; 15822 } 15823 /* Victim retries exhausted, fall back to std. retries... */ 15824 /* FALLTHRU */ 15825 15826 case SD_RETRIES_STANDARD: 15827 if (xp->xb_retry_count >= un->un_retry_count) { 15828 /* Retries exhausted, fail the command */ 15829 SD_TRACE(SD_LOG_IO_CORE, un, 15830 "sd_retry_command: retries exhausted!\n"); 15831 /* 15832 * update b_resid for failed SCMD_READ & SCMD_WRITE 15833 * commands with nonzero pkt_resid. 15834 */ 15835 if ((pktp->pkt_reason == CMD_CMPLT) && 15836 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15837 (pktp->pkt_resid != 0)) { 15838 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15839 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15840 SD_UPDATE_B_RESID(bp, pktp); 15841 } 15842 } 15843 goto fail_command; 15844 } 15845 xp->xb_retry_count++; 15846 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15847 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15848 break; 15849 15850 case SD_RETRIES_UA: 15851 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15852 /* Retries exhausted, fail the command */ 15853 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15854 "Unit Attention retries exhausted. " 15855 "Check the target.\n"); 15856 goto fail_command; 15857 } 15858 xp->xb_ua_retry_count++; 15859 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15860 "sd_retry_command: retry count:%d\n", 15861 xp->xb_ua_retry_count); 15862 break; 15863 15864 case SD_RETRIES_BUSY: 15865 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15866 /* Retries exhausted, fail the command */ 15867 SD_TRACE(SD_LOG_IO_CORE, un, 15868 "sd_retry_command: retries exhausted!\n"); 15869 goto fail_command; 15870 } 15871 xp->xb_retry_count++; 15872 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15873 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15874 break; 15875 15876 case SD_RETRIES_NOCHECK: 15877 default: 15878 /* No retry count to check. Just proceed with the retry */ 15879 break; 15880 } 15881 15882 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15883 15884 /* 15885 * If this is a non-USCSI command being retried 15886 * during execution last time, we should post an ereport with 15887 * driver-assessment of the value "retry". 15888 * For partial DMA, request sense and STATUS_QFULL, there are no 15889 * hardware errors, we bypass ereport posting. 15890 */ 15891 if (failure_code != 0) { 15892 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15893 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15894 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY); 15895 } 15896 } 15897 15898 /* 15899 * If we were given a zero timeout, we must attempt to retry the 15900 * command immediately (ie, without a delay). 15901 */ 15902 if (retry_delay == 0) { 15903 /* 15904 * Check some limiting conditions to see if we can actually 15905 * do the immediate retry. If we cannot, then we must 15906 * fall back to queueing up a delayed retry. 15907 */ 15908 if (un->un_ncmds_in_transport >= un->un_throttle) { 15909 /* 15910 * We are at the throttle limit for the target, 15911 * fall back to delayed retry. 15912 */ 15913 retry_delay = un->un_busy_timeout; 15914 statp = kstat_waitq_enter; 15915 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15916 "sd_retry_command: immed. retry hit " 15917 "throttle!\n"); 15918 } else { 15919 /* 15920 * We're clear to proceed with the immediate retry. 15921 * First call the user-provided function (if any) 15922 */ 15923 if (user_funcp != NULL) { 15924 (*user_funcp)(un, bp, user_arg, 15925 SD_IMMEDIATE_RETRY_ISSUED); 15926 #ifdef __lock_lint 15927 sd_print_incomplete_msg(un, bp, user_arg, 15928 SD_IMMEDIATE_RETRY_ISSUED); 15929 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15930 SD_IMMEDIATE_RETRY_ISSUED); 15931 sd_print_sense_failed_msg(un, bp, user_arg, 15932 SD_IMMEDIATE_RETRY_ISSUED); 15933 #endif 15934 } 15935 15936 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15937 "sd_retry_command: issuing immediate retry\n"); 15938 15939 /* 15940 * Call sd_start_cmds() to transport the command to 15941 * the target. 15942 */ 15943 sd_start_cmds(un, bp); 15944 15945 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15946 "sd_retry_command exit\n"); 15947 return; 15948 } 15949 } 15950 15951 /* 15952 * Set up to retry the command after a delay. 15953 * First call the user-provided function (if any) 15954 */ 15955 if (user_funcp != NULL) { 15956 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15957 } 15958 15959 sd_set_retry_bp(un, bp, retry_delay, statp); 15960 15961 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15962 return; 15963 15964 fail_command: 15965 15966 if (user_funcp != NULL) { 15967 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15968 } 15969 15970 fail_command_no_log: 15971 15972 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15973 "sd_retry_command: returning failed command\n"); 15974 15975 sd_return_failed_command(un, bp, failure_code); 15976 15977 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15978 } 15979 15980 15981 /* 15982 * Function: sd_set_retry_bp 15983 * 15984 * Description: Set up the given bp for retry. 15985 * 15986 * Arguments: un - ptr to associated softstate 15987 * bp - ptr to buf(9S) for the command 15988 * retry_delay - time interval before issuing retry (may be 0) 15989 * statp - optional pointer to kstat function 15990 * 15991 * Context: May be called under interrupt context 15992 */ 15993 15994 static void 15995 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15996 void (*statp)(kstat_io_t *)) 15997 { 15998 ASSERT(un != NULL); 15999 ASSERT(mutex_owned(SD_MUTEX(un))); 16000 ASSERT(bp != NULL); 16001 16002 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16003 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 16004 16005 /* 16006 * Indicate that the command is being retried. This will not allow any 16007 * other commands on the wait queue to be transported to the target 16008 * until this command has been completed (success or failure). The 16009 * "retry command" is not transported to the target until the given 16010 * time delay expires, unless the user specified a 0 retry_delay. 16011 * 16012 * Note: the timeout(9F) callback routine is what actually calls 16013 * sd_start_cmds() to transport the command, with the exception of a 16014 * zero retry_delay. The only current implementor of a zero retry delay 16015 * is the case where a START_STOP_UNIT is sent to spin-up a device. 16016 */ 16017 if (un->un_retry_bp == NULL) { 16018 ASSERT(un->un_retry_statp == NULL); 16019 un->un_retry_bp = bp; 16020 16021 /* 16022 * If the user has not specified a delay the command should 16023 * be queued and no timeout should be scheduled. 16024 */ 16025 if (retry_delay == 0) { 16026 /* 16027 * Save the kstat pointer that will be used in the 16028 * call to SD_UPDATE_KSTATS() below, so that 16029 * sd_start_cmds() can correctly decrement the waitq 16030 * count when it is time to transport this command. 16031 */ 16032 un->un_retry_statp = statp; 16033 goto done; 16034 } 16035 } 16036 16037 if (un->un_retry_bp == bp) { 16038 /* 16039 * Save the kstat pointer that will be used in the call to 16040 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 16041 * correctly decrement the waitq count when it is time to 16042 * transport this command. 16043 */ 16044 un->un_retry_statp = statp; 16045 16046 /* 16047 * Schedule a timeout if: 16048 * 1) The user has specified a delay. 16049 * 2) There is not a START_STOP_UNIT callback pending. 16050 * 16051 * If no delay has been specified, then it is up to the caller 16052 * to ensure that IO processing continues without stalling. 16053 * Effectively, this means that the caller will issue the 16054 * required call to sd_start_cmds(). The START_STOP_UNIT 16055 * callback does this after the START STOP UNIT command has 16056 * completed. In either of these cases we should not schedule 16057 * a timeout callback here. Also don't schedule the timeout if 16058 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 16059 */ 16060 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 16061 (un->un_direct_priority_timeid == NULL)) { 16062 un->un_retry_timeid = 16063 timeout(sd_start_retry_command, un, retry_delay); 16064 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16065 "sd_set_retry_bp: setting timeout: un: 0x%p" 16066 " bp:0x%p un_retry_timeid:0x%p\n", 16067 un, bp, un->un_retry_timeid); 16068 } 16069 } else { 16070 /* 16071 * We only get in here if there is already another command 16072 * waiting to be retried. In this case, we just put the 16073 * given command onto the wait queue, so it can be transported 16074 * after the current retry command has completed. 16075 * 16076 * Also we have to make sure that if the command at the head 16077 * of the wait queue is the un_failfast_bp, that we do not 16078 * put ahead of it any other commands that are to be retried. 16079 */ 16080 if ((un->un_failfast_bp != NULL) && 16081 (un->un_failfast_bp == un->un_waitq_headp)) { 16082 /* 16083 * Enqueue this command AFTER the first command on 16084 * the wait queue (which is also un_failfast_bp). 16085 */ 16086 bp->av_forw = un->un_waitq_headp->av_forw; 16087 un->un_waitq_headp->av_forw = bp; 16088 if (un->un_waitq_headp == un->un_waitq_tailp) { 16089 un->un_waitq_tailp = bp; 16090 } 16091 } else { 16092 /* Enqueue this command at the head of the waitq. */ 16093 bp->av_forw = un->un_waitq_headp; 16094 un->un_waitq_headp = bp; 16095 if (un->un_waitq_tailp == NULL) { 16096 un->un_waitq_tailp = bp; 16097 } 16098 } 16099 16100 if (statp == NULL) { 16101 statp = kstat_waitq_enter; 16102 } 16103 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16104 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 16105 } 16106 16107 done: 16108 if (statp != NULL) { 16109 SD_UPDATE_KSTATS(un, statp, bp); 16110 } 16111 16112 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16113 "sd_set_retry_bp: exit un:0x%p\n", un); 16114 } 16115 16116 16117 /* 16118 * Function: sd_start_retry_command 16119 * 16120 * Description: Start the command that has been waiting on the target's 16121 * retry queue. Called from timeout(9F) context after the 16122 * retry delay interval has expired. 16123 * 16124 * Arguments: arg - pointer to associated softstate for the device. 16125 * 16126 * Context: timeout(9F) thread context. May not sleep. 16127 */ 16128 16129 static void 16130 sd_start_retry_command(void *arg) 16131 { 16132 struct sd_lun *un = arg; 16133 16134 ASSERT(un != NULL); 16135 ASSERT(!mutex_owned(SD_MUTEX(un))); 16136 16137 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16138 "sd_start_retry_command: entry\n"); 16139 16140 mutex_enter(SD_MUTEX(un)); 16141 16142 un->un_retry_timeid = NULL; 16143 16144 if (un->un_retry_bp != NULL) { 16145 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16146 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16147 un, un->un_retry_bp); 16148 sd_start_cmds(un, un->un_retry_bp); 16149 } 16150 16151 mutex_exit(SD_MUTEX(un)); 16152 16153 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16154 "sd_start_retry_command: exit\n"); 16155 } 16156 16157 /* 16158 * Function: sd_rmw_msg_print_handler 16159 * 16160 * Description: If RMW mode is enabled and warning message is triggered 16161 * print I/O count during a fixed interval. 16162 * 16163 * Arguments: arg - pointer to associated softstate for the device. 16164 * 16165 * Context: timeout(9F) thread context. May not sleep. 16166 */ 16167 static void 16168 sd_rmw_msg_print_handler(void *arg) 16169 { 16170 struct sd_lun *un = arg; 16171 16172 ASSERT(un != NULL); 16173 ASSERT(!mutex_owned(SD_MUTEX(un))); 16174 16175 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16176 "sd_rmw_msg_print_handler: entry\n"); 16177 16178 mutex_enter(SD_MUTEX(un)); 16179 16180 if (un->un_rmw_incre_count > 0) { 16181 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16182 "%"PRIu64" I/O requests are not aligned with %d disk " 16183 "sector size in %ld seconds. They are handled through " 16184 "Read Modify Write but the performance is very low!\n", 16185 un->un_rmw_incre_count, un->un_tgt_blocksize, 16186 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000); 16187 un->un_rmw_incre_count = 0; 16188 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler, 16189 un, SD_RMW_MSG_PRINT_TIMEOUT); 16190 } else { 16191 un->un_rmw_msg_timeid = NULL; 16192 } 16193 16194 mutex_exit(SD_MUTEX(un)); 16195 16196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16197 "sd_rmw_msg_print_handler: exit\n"); 16198 } 16199 16200 /* 16201 * Function: sd_start_direct_priority_command 16202 * 16203 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16204 * received TRAN_BUSY when we called scsi_transport() to send it 16205 * to the underlying HBA. This function is called from timeout(9F) 16206 * context after the delay interval has expired. 16207 * 16208 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16209 * 16210 * Context: timeout(9F) thread context. May not sleep. 16211 */ 16212 16213 static void 16214 sd_start_direct_priority_command(void *arg) 16215 { 16216 struct buf *priority_bp = arg; 16217 struct sd_lun *un; 16218 16219 ASSERT(priority_bp != NULL); 16220 un = SD_GET_UN(priority_bp); 16221 ASSERT(un != NULL); 16222 ASSERT(!mutex_owned(SD_MUTEX(un))); 16223 16224 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16225 "sd_start_direct_priority_command: entry\n"); 16226 16227 mutex_enter(SD_MUTEX(un)); 16228 un->un_direct_priority_timeid = NULL; 16229 sd_start_cmds(un, priority_bp); 16230 mutex_exit(SD_MUTEX(un)); 16231 16232 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16233 "sd_start_direct_priority_command: exit\n"); 16234 } 16235 16236 16237 /* 16238 * Function: sd_send_request_sense_command 16239 * 16240 * Description: Sends a REQUEST SENSE command to the target 16241 * 16242 * Context: May be called from interrupt context. 16243 */ 16244 16245 static void 16246 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16247 struct scsi_pkt *pktp) 16248 { 16249 ASSERT(bp != NULL); 16250 ASSERT(un != NULL); 16251 ASSERT(mutex_owned(SD_MUTEX(un))); 16252 16253 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16254 "entry: buf:0x%p\n", bp); 16255 16256 /* 16257 * If we are syncing or dumping, then fail the command to avoid a 16258 * recursive callback into scsi_transport(). Also fail the command 16259 * if we are suspended (legacy behavior). 16260 */ 16261 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16262 (un->un_state == SD_STATE_DUMPING)) { 16263 sd_return_failed_command(un, bp, EIO); 16264 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16265 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16266 return; 16267 } 16268 16269 /* 16270 * Retry the failed command and don't issue the request sense if: 16271 * 1) the sense buf is busy 16272 * 2) we have 1 or more outstanding commands on the target 16273 * (the sense data will be cleared or invalidated any way) 16274 * 16275 * Note: There could be an issue with not checking a retry limit here, 16276 * the problem is determining which retry limit to check. 16277 */ 16278 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16279 /* Don't retry if the command is flagged as non-retryable */ 16280 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16281 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16282 NULL, NULL, 0, un->un_busy_timeout, 16283 kstat_waitq_enter); 16284 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16285 "sd_send_request_sense_command: " 16286 "at full throttle, retrying exit\n"); 16287 } else { 16288 sd_return_failed_command(un, bp, EIO); 16289 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16290 "sd_send_request_sense_command: " 16291 "at full throttle, non-retryable exit\n"); 16292 } 16293 return; 16294 } 16295 16296 sd_mark_rqs_busy(un, bp); 16297 sd_start_cmds(un, un->un_rqs_bp); 16298 16299 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16300 "sd_send_request_sense_command: exit\n"); 16301 } 16302 16303 16304 /* 16305 * Function: sd_mark_rqs_busy 16306 * 16307 * Description: Indicate that the request sense bp for this instance is 16308 * in use. 16309 * 16310 * Context: May be called under interrupt context 16311 */ 16312 16313 static void 16314 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16315 { 16316 struct sd_xbuf *sense_xp; 16317 16318 ASSERT(un != NULL); 16319 ASSERT(bp != NULL); 16320 ASSERT(mutex_owned(SD_MUTEX(un))); 16321 ASSERT(un->un_sense_isbusy == 0); 16322 16323 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16324 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16325 16326 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16327 ASSERT(sense_xp != NULL); 16328 16329 SD_INFO(SD_LOG_IO, un, 16330 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16331 16332 ASSERT(sense_xp->xb_pktp != NULL); 16333 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16334 == (FLAG_SENSING | FLAG_HEAD)); 16335 16336 un->un_sense_isbusy = 1; 16337 un->un_rqs_bp->b_resid = 0; 16338 sense_xp->xb_pktp->pkt_resid = 0; 16339 sense_xp->xb_pktp->pkt_reason = 0; 16340 16341 /* So we can get back the bp at interrupt time! */ 16342 sense_xp->xb_sense_bp = bp; 16343 16344 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16345 16346 /* 16347 * Mark this buf as awaiting sense data. (This is already set in 16348 * the pkt_flags for the RQS packet.) 16349 */ 16350 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16351 16352 /* Request sense down same path */ 16353 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) && 16354 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance) 16355 sense_xp->xb_pktp->pkt_path_instance = 16356 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance; 16357 16358 sense_xp->xb_retry_count = 0; 16359 sense_xp->xb_victim_retry_count = 0; 16360 sense_xp->xb_ua_retry_count = 0; 16361 sense_xp->xb_nr_retry_count = 0; 16362 sense_xp->xb_dma_resid = 0; 16363 16364 /* Clean up the fields for auto-request sense */ 16365 sense_xp->xb_sense_status = 0; 16366 sense_xp->xb_sense_state = 0; 16367 sense_xp->xb_sense_resid = 0; 16368 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16369 16370 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16371 } 16372 16373 16374 /* 16375 * Function: sd_mark_rqs_idle 16376 * 16377 * Description: SD_MUTEX must be held continuously through this routine 16378 * to prevent reuse of the rqs struct before the caller can 16379 * complete it's processing. 16380 * 16381 * Return Code: Pointer to the RQS buf 16382 * 16383 * Context: May be called under interrupt context 16384 */ 16385 16386 static struct buf * 16387 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16388 { 16389 struct buf *bp; 16390 ASSERT(un != NULL); 16391 ASSERT(sense_xp != NULL); 16392 ASSERT(mutex_owned(SD_MUTEX(un))); 16393 ASSERT(un->un_sense_isbusy != 0); 16394 16395 un->un_sense_isbusy = 0; 16396 bp = sense_xp->xb_sense_bp; 16397 sense_xp->xb_sense_bp = NULL; 16398 16399 /* This pkt is no longer interested in getting sense data */ 16400 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16401 16402 return (bp); 16403 } 16404 16405 16406 16407 /* 16408 * Function: sd_alloc_rqs 16409 * 16410 * Description: Set up the unit to receive auto request sense data 16411 * 16412 * Return Code: DDI_SUCCESS or DDI_FAILURE 16413 * 16414 * Context: Called under attach(9E) context 16415 */ 16416 16417 static int 16418 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16419 { 16420 struct sd_xbuf *xp; 16421 16422 ASSERT(un != NULL); 16423 ASSERT(!mutex_owned(SD_MUTEX(un))); 16424 ASSERT(un->un_rqs_bp == NULL); 16425 ASSERT(un->un_rqs_pktp == NULL); 16426 16427 /* 16428 * First allocate the required buf and scsi_pkt structs, then set up 16429 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16430 */ 16431 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16432 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16433 if (un->un_rqs_bp == NULL) { 16434 return (DDI_FAILURE); 16435 } 16436 16437 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16438 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16439 16440 if (un->un_rqs_pktp == NULL) { 16441 sd_free_rqs(un); 16442 return (DDI_FAILURE); 16443 } 16444 16445 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16446 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16447 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 16448 16449 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16450 16451 /* Set up the other needed members in the ARQ scsi_pkt. */ 16452 un->un_rqs_pktp->pkt_comp = sdintr; 16453 un->un_rqs_pktp->pkt_time = sd_io_time; 16454 un->un_rqs_pktp->pkt_flags |= 16455 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16456 16457 /* 16458 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16459 * provide any intpkt, destroypkt routines as we take care of 16460 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16461 */ 16462 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16463 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16464 xp->xb_pktp = un->un_rqs_pktp; 16465 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16466 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16467 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16468 16469 /* 16470 * Save the pointer to the request sense private bp so it can 16471 * be retrieved in sdintr. 16472 */ 16473 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16474 ASSERT(un->un_rqs_bp->b_private == xp); 16475 16476 /* 16477 * See if the HBA supports auto-request sense for the specified 16478 * target/lun. If it does, then try to enable it (if not already 16479 * enabled). 16480 * 16481 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16482 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16483 * return success. However, in both of these cases ARQ is always 16484 * enabled and scsi_ifgetcap will always return true. The best approach 16485 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16486 * 16487 * The 3rd case is the HBA (adp) always return enabled on 16488 * scsi_ifgetgetcap even when it's not enable, the best approach 16489 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16490 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16491 */ 16492 16493 if (un->un_f_is_fibre == TRUE) { 16494 un->un_f_arq_enabled = TRUE; 16495 } else { 16496 #if defined(__i386) || defined(__amd64) 16497 /* 16498 * Circumvent the Adaptec bug, remove this code when 16499 * the bug is fixed 16500 */ 16501 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16502 #endif 16503 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16504 case 0: 16505 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16506 "sd_alloc_rqs: HBA supports ARQ\n"); 16507 /* 16508 * ARQ is supported by this HBA but currently is not 16509 * enabled. Attempt to enable it and if successful then 16510 * mark this instance as ARQ enabled. 16511 */ 16512 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16513 == 1) { 16514 /* Successfully enabled ARQ in the HBA */ 16515 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16516 "sd_alloc_rqs: ARQ enabled\n"); 16517 un->un_f_arq_enabled = TRUE; 16518 } else { 16519 /* Could not enable ARQ in the HBA */ 16520 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16521 "sd_alloc_rqs: failed ARQ enable\n"); 16522 un->un_f_arq_enabled = FALSE; 16523 } 16524 break; 16525 case 1: 16526 /* 16527 * ARQ is supported by this HBA and is already enabled. 16528 * Just mark ARQ as enabled for this instance. 16529 */ 16530 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16531 "sd_alloc_rqs: ARQ already enabled\n"); 16532 un->un_f_arq_enabled = TRUE; 16533 break; 16534 default: 16535 /* 16536 * ARQ is not supported by this HBA; disable it for this 16537 * instance. 16538 */ 16539 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16540 "sd_alloc_rqs: HBA does not support ARQ\n"); 16541 un->un_f_arq_enabled = FALSE; 16542 break; 16543 } 16544 } 16545 16546 return (DDI_SUCCESS); 16547 } 16548 16549 16550 /* 16551 * Function: sd_free_rqs 16552 * 16553 * Description: Cleanup for the pre-instance RQS command. 16554 * 16555 * Context: Kernel thread context 16556 */ 16557 16558 static void 16559 sd_free_rqs(struct sd_lun *un) 16560 { 16561 ASSERT(un != NULL); 16562 16563 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16564 16565 /* 16566 * If consistent memory is bound to a scsi_pkt, the pkt 16567 * has to be destroyed *before* freeing the consistent memory. 16568 * Don't change the sequence of this operations. 16569 * scsi_destroy_pkt() might access memory, which isn't allowed, 16570 * after it was freed in scsi_free_consistent_buf(). 16571 */ 16572 if (un->un_rqs_pktp != NULL) { 16573 scsi_destroy_pkt(un->un_rqs_pktp); 16574 un->un_rqs_pktp = NULL; 16575 } 16576 16577 if (un->un_rqs_bp != NULL) { 16578 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 16579 if (xp != NULL) { 16580 kmem_free(xp, sizeof (struct sd_xbuf)); 16581 } 16582 scsi_free_consistent_buf(un->un_rqs_bp); 16583 un->un_rqs_bp = NULL; 16584 } 16585 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16586 } 16587 16588 16589 16590 /* 16591 * Function: sd_reduce_throttle 16592 * 16593 * Description: Reduces the maximum # of outstanding commands on a 16594 * target to the current number of outstanding commands. 16595 * Queues a tiemout(9F) callback to restore the limit 16596 * after a specified interval has elapsed. 16597 * Typically used when we get a TRAN_BUSY return code 16598 * back from scsi_transport(). 16599 * 16600 * Arguments: un - ptr to the sd_lun softstate struct 16601 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16602 * 16603 * Context: May be called from interrupt context 16604 */ 16605 16606 static void 16607 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16608 { 16609 ASSERT(un != NULL); 16610 ASSERT(mutex_owned(SD_MUTEX(un))); 16611 ASSERT(un->un_ncmds_in_transport >= 0); 16612 16613 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16614 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16615 un, un->un_throttle, un->un_ncmds_in_transport); 16616 16617 if (un->un_throttle > 1) { 16618 if (un->un_f_use_adaptive_throttle == TRUE) { 16619 switch (throttle_type) { 16620 case SD_THROTTLE_TRAN_BUSY: 16621 if (un->un_busy_throttle == 0) { 16622 un->un_busy_throttle = un->un_throttle; 16623 } 16624 break; 16625 case SD_THROTTLE_QFULL: 16626 un->un_busy_throttle = 0; 16627 break; 16628 default: 16629 ASSERT(FALSE); 16630 } 16631 16632 if (un->un_ncmds_in_transport > 0) { 16633 un->un_throttle = un->un_ncmds_in_transport; 16634 } 16635 16636 } else { 16637 if (un->un_ncmds_in_transport == 0) { 16638 un->un_throttle = 1; 16639 } else { 16640 un->un_throttle = un->un_ncmds_in_transport; 16641 } 16642 } 16643 } 16644 16645 /* Reschedule the timeout if none is currently active */ 16646 if (un->un_reset_throttle_timeid == NULL) { 16647 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16648 un, SD_THROTTLE_RESET_INTERVAL); 16649 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16650 "sd_reduce_throttle: timeout scheduled!\n"); 16651 } 16652 16653 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16654 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16655 } 16656 16657 16658 16659 /* 16660 * Function: sd_restore_throttle 16661 * 16662 * Description: Callback function for timeout(9F). Resets the current 16663 * value of un->un_throttle to its default. 16664 * 16665 * Arguments: arg - pointer to associated softstate for the device. 16666 * 16667 * Context: May be called from interrupt context 16668 */ 16669 16670 static void 16671 sd_restore_throttle(void *arg) 16672 { 16673 struct sd_lun *un = arg; 16674 16675 ASSERT(un != NULL); 16676 ASSERT(!mutex_owned(SD_MUTEX(un))); 16677 16678 mutex_enter(SD_MUTEX(un)); 16679 16680 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16681 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16682 16683 un->un_reset_throttle_timeid = NULL; 16684 16685 if (un->un_f_use_adaptive_throttle == TRUE) { 16686 /* 16687 * If un_busy_throttle is nonzero, then it contains the 16688 * value that un_throttle was when we got a TRAN_BUSY back 16689 * from scsi_transport(). We want to revert back to this 16690 * value. 16691 * 16692 * In the QFULL case, the throttle limit will incrementally 16693 * increase until it reaches max throttle. 16694 */ 16695 if (un->un_busy_throttle > 0) { 16696 un->un_throttle = un->un_busy_throttle; 16697 un->un_busy_throttle = 0; 16698 } else { 16699 /* 16700 * increase throttle by 10% open gate slowly, schedule 16701 * another restore if saved throttle has not been 16702 * reached 16703 */ 16704 short throttle; 16705 if (sd_qfull_throttle_enable) { 16706 throttle = un->un_throttle + 16707 max((un->un_throttle / 10), 1); 16708 un->un_throttle = 16709 (throttle < un->un_saved_throttle) ? 16710 throttle : un->un_saved_throttle; 16711 if (un->un_throttle < un->un_saved_throttle) { 16712 un->un_reset_throttle_timeid = 16713 timeout(sd_restore_throttle, 16714 un, 16715 SD_QFULL_THROTTLE_RESET_INTERVAL); 16716 } 16717 } 16718 } 16719 16720 /* 16721 * If un_throttle has fallen below the low-water mark, we 16722 * restore the maximum value here (and allow it to ratchet 16723 * down again if necessary). 16724 */ 16725 if (un->un_throttle < un->un_min_throttle) { 16726 un->un_throttle = un->un_saved_throttle; 16727 } 16728 } else { 16729 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16730 "restoring limit from 0x%x to 0x%x\n", 16731 un->un_throttle, un->un_saved_throttle); 16732 un->un_throttle = un->un_saved_throttle; 16733 } 16734 16735 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16736 "sd_restore_throttle: calling sd_start_cmds!\n"); 16737 16738 sd_start_cmds(un, NULL); 16739 16740 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16741 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16742 un, un->un_throttle); 16743 16744 mutex_exit(SD_MUTEX(un)); 16745 16746 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16747 } 16748 16749 /* 16750 * Function: sdrunout 16751 * 16752 * Description: Callback routine for scsi_init_pkt when a resource allocation 16753 * fails. 16754 * 16755 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16756 * soft state instance. 16757 * 16758 * Return Code: The scsi_init_pkt routine allows for the callback function to 16759 * return a 0 indicating the callback should be rescheduled or a 1 16760 * indicating not to reschedule. This routine always returns 1 16761 * because the driver always provides a callback function to 16762 * scsi_init_pkt. This results in a callback always being scheduled 16763 * (via the scsi_init_pkt callback implementation) if a resource 16764 * failure occurs. 16765 * 16766 * Context: This callback function may not block or call routines that block 16767 * 16768 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16769 * request persisting at the head of the list which cannot be 16770 * satisfied even after multiple retries. In the future the driver 16771 * may implement some time of maximum runout count before failing 16772 * an I/O. 16773 */ 16774 16775 static int 16776 sdrunout(caddr_t arg) 16777 { 16778 struct sd_lun *un = (struct sd_lun *)arg; 16779 16780 ASSERT(un != NULL); 16781 ASSERT(!mutex_owned(SD_MUTEX(un))); 16782 16783 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16784 16785 mutex_enter(SD_MUTEX(un)); 16786 sd_start_cmds(un, NULL); 16787 mutex_exit(SD_MUTEX(un)); 16788 /* 16789 * This callback routine always returns 1 (i.e. do not reschedule) 16790 * because we always specify sdrunout as the callback handler for 16791 * scsi_init_pkt inside the call to sd_start_cmds. 16792 */ 16793 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16794 return (1); 16795 } 16796 16797 16798 /* 16799 * Function: sdintr 16800 * 16801 * Description: Completion callback routine for scsi_pkt(9S) structs 16802 * sent to the HBA driver via scsi_transport(9F). 16803 * 16804 * Context: Interrupt context 16805 */ 16806 16807 static void 16808 sdintr(struct scsi_pkt *pktp) 16809 { 16810 struct buf *bp; 16811 struct sd_xbuf *xp; 16812 struct sd_lun *un; 16813 size_t actual_len; 16814 sd_ssc_t *sscp; 16815 16816 ASSERT(pktp != NULL); 16817 bp = (struct buf *)pktp->pkt_private; 16818 ASSERT(bp != NULL); 16819 xp = SD_GET_XBUF(bp); 16820 ASSERT(xp != NULL); 16821 ASSERT(xp->xb_pktp != NULL); 16822 un = SD_GET_UN(bp); 16823 ASSERT(un != NULL); 16824 ASSERT(!mutex_owned(SD_MUTEX(un))); 16825 16826 #ifdef SD_FAULT_INJECTION 16827 16828 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16829 /* SD FaultInjection */ 16830 sd_faultinjection(pktp); 16831 16832 #endif /* SD_FAULT_INJECTION */ 16833 16834 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16835 " xp:0x%p, un:0x%p\n", bp, xp, un); 16836 16837 mutex_enter(SD_MUTEX(un)); 16838 16839 ASSERT(un->un_fm_private != NULL); 16840 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16841 ASSERT(sscp != NULL); 16842 16843 /* Reduce the count of the #commands currently in transport */ 16844 un->un_ncmds_in_transport--; 16845 ASSERT(un->un_ncmds_in_transport >= 0); 16846 16847 /* Increment counter to indicate that the callback routine is active */ 16848 un->un_in_callback++; 16849 16850 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16851 16852 #ifdef SDDEBUG 16853 if (bp == un->un_retry_bp) { 16854 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16855 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16856 un, un->un_retry_bp, un->un_ncmds_in_transport); 16857 } 16858 #endif 16859 16860 /* 16861 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 16862 * state if needed. 16863 */ 16864 if (pktp->pkt_reason == CMD_DEV_GONE) { 16865 /* Prevent multiple console messages for the same failure. */ 16866 if (un->un_last_pkt_reason != CMD_DEV_GONE) { 16867 un->un_last_pkt_reason = CMD_DEV_GONE; 16868 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16869 "Command failed to complete...Device is gone\n"); 16870 } 16871 if (un->un_mediastate != DKIO_DEV_GONE) { 16872 un->un_mediastate = DKIO_DEV_GONE; 16873 cv_broadcast(&un->un_state_cv); 16874 } 16875 /* 16876 * If the command happens to be the REQUEST SENSE command, 16877 * free up the rqs buf and fail the original command. 16878 */ 16879 if (bp == un->un_rqs_bp) { 16880 bp = sd_mark_rqs_idle(un, xp); 16881 } 16882 sd_return_failed_command(un, bp, EIO); 16883 goto exit; 16884 } 16885 16886 if (pktp->pkt_state & STATE_XARQ_DONE) { 16887 SD_TRACE(SD_LOG_COMMON, un, 16888 "sdintr: extra sense data received. pkt=%p\n", pktp); 16889 } 16890 16891 /* 16892 * First see if the pkt has auto-request sense data with it.... 16893 * Look at the packet state first so we don't take a performance 16894 * hit looking at the arq enabled flag unless absolutely necessary. 16895 */ 16896 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16897 (un->un_f_arq_enabled == TRUE)) { 16898 /* 16899 * The HBA did an auto request sense for this command so check 16900 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16901 * driver command that should not be retried. 16902 */ 16903 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16904 /* 16905 * Save the relevant sense info into the xp for the 16906 * original cmd. 16907 */ 16908 struct scsi_arq_status *asp; 16909 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16910 xp->xb_sense_status = 16911 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16912 xp->xb_sense_state = asp->sts_rqpkt_state; 16913 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16914 if (pktp->pkt_state & STATE_XARQ_DONE) { 16915 actual_len = MAX_SENSE_LENGTH - 16916 xp->xb_sense_resid; 16917 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16918 MAX_SENSE_LENGTH); 16919 } else { 16920 if (xp->xb_sense_resid > SENSE_LENGTH) { 16921 actual_len = MAX_SENSE_LENGTH - 16922 xp->xb_sense_resid; 16923 } else { 16924 actual_len = SENSE_LENGTH - 16925 xp->xb_sense_resid; 16926 } 16927 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16928 if ((((struct uscsi_cmd *) 16929 (xp->xb_pktinfo))->uscsi_rqlen) > 16930 actual_len) { 16931 xp->xb_sense_resid = 16932 (((struct uscsi_cmd *) 16933 (xp->xb_pktinfo))-> 16934 uscsi_rqlen) - actual_len; 16935 } else { 16936 xp->xb_sense_resid = 0; 16937 } 16938 } 16939 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16940 SENSE_LENGTH); 16941 } 16942 16943 /* fail the command */ 16944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16945 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16946 sd_return_failed_command(un, bp, EIO); 16947 goto exit; 16948 } 16949 16950 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16951 /* 16952 * We want to either retry or fail this command, so free 16953 * the DMA resources here. If we retry the command then 16954 * the DMA resources will be reallocated in sd_start_cmds(). 16955 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16956 * causes the *entire* transfer to start over again from the 16957 * beginning of the request, even for PARTIAL chunks that 16958 * have already transferred successfully. 16959 */ 16960 if ((un->un_f_is_fibre == TRUE) && 16961 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16962 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16963 scsi_dmafree(pktp); 16964 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16965 } 16966 #endif 16967 16968 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16969 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16970 16971 sd_handle_auto_request_sense(un, bp, xp, pktp); 16972 goto exit; 16973 } 16974 16975 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16976 if (pktp->pkt_flags & FLAG_SENSING) { 16977 /* This pktp is from the unit's REQUEST_SENSE command */ 16978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16979 "sdintr: sd_handle_request_sense\n"); 16980 sd_handle_request_sense(un, bp, xp, pktp); 16981 goto exit; 16982 } 16983 16984 /* 16985 * Check to see if the command successfully completed as requested; 16986 * this is the most common case (and also the hot performance path). 16987 * 16988 * Requirements for successful completion are: 16989 * pkt_reason is CMD_CMPLT and packet status is status good. 16990 * In addition: 16991 * - A residual of zero indicates successful completion no matter what 16992 * the command is. 16993 * - If the residual is not zero and the command is not a read or 16994 * write, then it's still defined as successful completion. In other 16995 * words, if the command is a read or write the residual must be 16996 * zero for successful completion. 16997 * - If the residual is not zero and the command is a read or 16998 * write, and it's a USCSICMD, then it's still defined as 16999 * successful completion. 17000 */ 17001 if ((pktp->pkt_reason == CMD_CMPLT) && 17002 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 17003 17004 /* 17005 * Since this command is returned with a good status, we 17006 * can reset the count for Sonoma failover. 17007 */ 17008 un->un_sonoma_failure_count = 0; 17009 17010 /* 17011 * Return all USCSI commands on good status 17012 */ 17013 if (pktp->pkt_resid == 0) { 17014 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17015 "sdintr: returning command for resid == 0\n"); 17016 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 17017 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 17018 SD_UPDATE_B_RESID(bp, pktp); 17019 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17020 "sdintr: returning command for resid != 0\n"); 17021 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17022 SD_UPDATE_B_RESID(bp, pktp); 17023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17024 "sdintr: returning uscsi command\n"); 17025 } else { 17026 goto not_successful; 17027 } 17028 sd_return_command(un, bp); 17029 17030 /* 17031 * Decrement counter to indicate that the callback routine 17032 * is done. 17033 */ 17034 un->un_in_callback--; 17035 ASSERT(un->un_in_callback >= 0); 17036 mutex_exit(SD_MUTEX(un)); 17037 17038 return; 17039 } 17040 17041 not_successful: 17042 17043 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 17044 /* 17045 * The following is based upon knowledge of the underlying transport 17046 * and its use of DMA resources. This code should be removed when 17047 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 17048 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 17049 * and sd_start_cmds(). 17050 * 17051 * Free any DMA resources associated with this command if there 17052 * is a chance it could be retried or enqueued for later retry. 17053 * If we keep the DMA binding then mpxio cannot reissue the 17054 * command on another path whenever a path failure occurs. 17055 * 17056 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 17057 * causes the *entire* transfer to start over again from the 17058 * beginning of the request, even for PARTIAL chunks that 17059 * have already transferred successfully. 17060 * 17061 * This is only done for non-uscsi commands (and also skipped for the 17062 * driver's internal RQS command). Also just do this for Fibre Channel 17063 * devices as these are the only ones that support mpxio. 17064 */ 17065 if ((un->un_f_is_fibre == TRUE) && 17066 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 17067 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 17068 scsi_dmafree(pktp); 17069 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 17070 } 17071 #endif 17072 17073 /* 17074 * The command did not successfully complete as requested so check 17075 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 17076 * driver command that should not be retried so just return. If 17077 * FLAG_DIAGNOSE is not set the error will be processed below. 17078 */ 17079 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 17080 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17081 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 17082 /* 17083 * Issue a request sense if a check condition caused the error 17084 * (we handle the auto request sense case above), otherwise 17085 * just fail the command. 17086 */ 17087 if ((pktp->pkt_reason == CMD_CMPLT) && 17088 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 17089 sd_send_request_sense_command(un, bp, pktp); 17090 } else { 17091 sd_return_failed_command(un, bp, EIO); 17092 } 17093 goto exit; 17094 } 17095 17096 /* 17097 * The command did not successfully complete as requested so process 17098 * the error, retry, and/or attempt recovery. 17099 */ 17100 switch (pktp->pkt_reason) { 17101 case CMD_CMPLT: 17102 switch (SD_GET_PKT_STATUS(pktp)) { 17103 case STATUS_GOOD: 17104 /* 17105 * The command completed successfully with a non-zero 17106 * residual 17107 */ 17108 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17109 "sdintr: STATUS_GOOD \n"); 17110 sd_pkt_status_good(un, bp, xp, pktp); 17111 break; 17112 17113 case STATUS_CHECK: 17114 case STATUS_TERMINATED: 17115 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17116 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 17117 sd_pkt_status_check_condition(un, bp, xp, pktp); 17118 break; 17119 17120 case STATUS_BUSY: 17121 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17122 "sdintr: STATUS_BUSY\n"); 17123 sd_pkt_status_busy(un, bp, xp, pktp); 17124 break; 17125 17126 case STATUS_RESERVATION_CONFLICT: 17127 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17128 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 17129 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17130 break; 17131 17132 case STATUS_QFULL: 17133 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17134 "sdintr: STATUS_QFULL\n"); 17135 sd_pkt_status_qfull(un, bp, xp, pktp); 17136 break; 17137 17138 case STATUS_MET: 17139 case STATUS_INTERMEDIATE: 17140 case STATUS_SCSI2: 17141 case STATUS_INTERMEDIATE_MET: 17142 case STATUS_ACA_ACTIVE: 17143 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17144 "Unexpected SCSI status received: 0x%x\n", 17145 SD_GET_PKT_STATUS(pktp)); 17146 /* 17147 * Mark the ssc_flags when detected invalid status 17148 * code for non-USCSI command. 17149 */ 17150 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17151 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17152 0, "stat-code"); 17153 } 17154 sd_return_failed_command(un, bp, EIO); 17155 break; 17156 17157 default: 17158 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17159 "Invalid SCSI status received: 0x%x\n", 17160 SD_GET_PKT_STATUS(pktp)); 17161 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17162 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17163 0, "stat-code"); 17164 } 17165 sd_return_failed_command(un, bp, EIO); 17166 break; 17167 17168 } 17169 break; 17170 17171 case CMD_INCOMPLETE: 17172 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17173 "sdintr: CMD_INCOMPLETE\n"); 17174 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 17175 break; 17176 case CMD_TRAN_ERR: 17177 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17178 "sdintr: CMD_TRAN_ERR\n"); 17179 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 17180 break; 17181 case CMD_RESET: 17182 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17183 "sdintr: CMD_RESET \n"); 17184 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 17185 break; 17186 case CMD_ABORTED: 17187 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17188 "sdintr: CMD_ABORTED \n"); 17189 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 17190 break; 17191 case CMD_TIMEOUT: 17192 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17193 "sdintr: CMD_TIMEOUT\n"); 17194 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 17195 break; 17196 case CMD_UNX_BUS_FREE: 17197 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17198 "sdintr: CMD_UNX_BUS_FREE \n"); 17199 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 17200 break; 17201 case CMD_TAG_REJECT: 17202 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17203 "sdintr: CMD_TAG_REJECT\n"); 17204 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 17205 break; 17206 default: 17207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17208 "sdintr: default\n"); 17209 /* 17210 * Mark the ssc_flags for detecting invliad pkt_reason. 17211 */ 17212 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17213 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON, 17214 0, "pkt-reason"); 17215 } 17216 sd_pkt_reason_default(un, bp, xp, pktp); 17217 break; 17218 } 17219 17220 exit: 17221 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 17222 17223 /* Decrement counter to indicate that the callback routine is done. */ 17224 un->un_in_callback--; 17225 ASSERT(un->un_in_callback >= 0); 17226 17227 /* 17228 * At this point, the pkt has been dispatched, ie, it is either 17229 * being re-tried or has been returned to its caller and should 17230 * not be referenced. 17231 */ 17232 17233 mutex_exit(SD_MUTEX(un)); 17234 } 17235 17236 17237 /* 17238 * Function: sd_print_incomplete_msg 17239 * 17240 * Description: Prints the error message for a CMD_INCOMPLETE error. 17241 * 17242 * Arguments: un - ptr to associated softstate for the device. 17243 * bp - ptr to the buf(9S) for the command. 17244 * arg - message string ptr 17245 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17246 * or SD_NO_RETRY_ISSUED. 17247 * 17248 * Context: May be called under interrupt context 17249 */ 17250 17251 static void 17252 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17253 { 17254 struct scsi_pkt *pktp; 17255 char *msgp; 17256 char *cmdp = arg; 17257 17258 ASSERT(un != NULL); 17259 ASSERT(mutex_owned(SD_MUTEX(un))); 17260 ASSERT(bp != NULL); 17261 ASSERT(arg != NULL); 17262 pktp = SD_GET_PKTP(bp); 17263 ASSERT(pktp != NULL); 17264 17265 switch (code) { 17266 case SD_DELAYED_RETRY_ISSUED: 17267 case SD_IMMEDIATE_RETRY_ISSUED: 17268 msgp = "retrying"; 17269 break; 17270 case SD_NO_RETRY_ISSUED: 17271 default: 17272 msgp = "giving up"; 17273 break; 17274 } 17275 17276 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17277 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17278 "incomplete %s- %s\n", cmdp, msgp); 17279 } 17280 } 17281 17282 17283 17284 /* 17285 * Function: sd_pkt_status_good 17286 * 17287 * Description: Processing for a STATUS_GOOD code in pkt_status. 17288 * 17289 * Context: May be called under interrupt context 17290 */ 17291 17292 static void 17293 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17294 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17295 { 17296 char *cmdp; 17297 17298 ASSERT(un != NULL); 17299 ASSERT(mutex_owned(SD_MUTEX(un))); 17300 ASSERT(bp != NULL); 17301 ASSERT(xp != NULL); 17302 ASSERT(pktp != NULL); 17303 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17304 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17305 ASSERT(pktp->pkt_resid != 0); 17306 17307 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17308 17309 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17310 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17311 case SCMD_READ: 17312 cmdp = "read"; 17313 break; 17314 case SCMD_WRITE: 17315 cmdp = "write"; 17316 break; 17317 default: 17318 SD_UPDATE_B_RESID(bp, pktp); 17319 sd_return_command(un, bp); 17320 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17321 return; 17322 } 17323 17324 /* 17325 * See if we can retry the read/write, preferrably immediately. 17326 * If retries are exhaused, then sd_retry_command() will update 17327 * the b_resid count. 17328 */ 17329 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17330 cmdp, EIO, (clock_t)0, NULL); 17331 17332 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17333 } 17334 17335 17336 17337 17338 17339 /* 17340 * Function: sd_handle_request_sense 17341 * 17342 * Description: Processing for non-auto Request Sense command. 17343 * 17344 * Arguments: un - ptr to associated softstate 17345 * sense_bp - ptr to buf(9S) for the RQS command 17346 * sense_xp - ptr to the sd_xbuf for the RQS command 17347 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17348 * 17349 * Context: May be called under interrupt context 17350 */ 17351 17352 static void 17353 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17354 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17355 { 17356 struct buf *cmd_bp; /* buf for the original command */ 17357 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17358 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17359 size_t actual_len; /* actual sense data length */ 17360 17361 ASSERT(un != NULL); 17362 ASSERT(mutex_owned(SD_MUTEX(un))); 17363 ASSERT(sense_bp != NULL); 17364 ASSERT(sense_xp != NULL); 17365 ASSERT(sense_pktp != NULL); 17366 17367 /* 17368 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17369 * RQS command and not the original command. 17370 */ 17371 ASSERT(sense_pktp == un->un_rqs_pktp); 17372 ASSERT(sense_bp == un->un_rqs_bp); 17373 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17374 (FLAG_SENSING | FLAG_HEAD)); 17375 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17376 FLAG_SENSING) == FLAG_SENSING); 17377 17378 /* These are the bp, xp, and pktp for the original command */ 17379 cmd_bp = sense_xp->xb_sense_bp; 17380 cmd_xp = SD_GET_XBUF(cmd_bp); 17381 cmd_pktp = SD_GET_PKTP(cmd_bp); 17382 17383 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17384 /* 17385 * The REQUEST SENSE command failed. Release the REQUEST 17386 * SENSE command for re-use, get back the bp for the original 17387 * command, and attempt to re-try the original command if 17388 * FLAG_DIAGNOSE is not set in the original packet. 17389 */ 17390 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17391 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17392 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17393 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17394 NULL, NULL, EIO, (clock_t)0, NULL); 17395 return; 17396 } 17397 } 17398 17399 /* 17400 * Save the relevant sense info into the xp for the original cmd. 17401 * 17402 * Note: if the request sense failed the state info will be zero 17403 * as set in sd_mark_rqs_busy() 17404 */ 17405 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17406 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17407 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 17408 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 17409 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 17410 SENSE_LENGTH)) { 17411 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17412 MAX_SENSE_LENGTH); 17413 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17414 } else { 17415 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17416 SENSE_LENGTH); 17417 if (actual_len < SENSE_LENGTH) { 17418 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 17419 } else { 17420 cmd_xp->xb_sense_resid = 0; 17421 } 17422 } 17423 17424 /* 17425 * Free up the RQS command.... 17426 * NOTE: 17427 * Must do this BEFORE calling sd_validate_sense_data! 17428 * sd_validate_sense_data may return the original command in 17429 * which case the pkt will be freed and the flags can no 17430 * longer be touched. 17431 * SD_MUTEX is held through this process until the command 17432 * is dispatched based upon the sense data, so there are 17433 * no race conditions. 17434 */ 17435 (void) sd_mark_rqs_idle(un, sense_xp); 17436 17437 /* 17438 * For a retryable command see if we have valid sense data, if so then 17439 * turn it over to sd_decode_sense() to figure out the right course of 17440 * action. Just fail a non-retryable command. 17441 */ 17442 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17443 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 17444 SD_SENSE_DATA_IS_VALID) { 17445 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17446 } 17447 } else { 17448 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17449 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17450 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17451 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17452 sd_return_failed_command(un, cmd_bp, EIO); 17453 } 17454 } 17455 17456 17457 17458 17459 /* 17460 * Function: sd_handle_auto_request_sense 17461 * 17462 * Description: Processing for auto-request sense information. 17463 * 17464 * Arguments: un - ptr to associated softstate 17465 * bp - ptr to buf(9S) for the command 17466 * xp - ptr to the sd_xbuf for the command 17467 * pktp - ptr to the scsi_pkt(9S) for the command 17468 * 17469 * Context: May be called under interrupt context 17470 */ 17471 17472 static void 17473 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17474 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17475 { 17476 struct scsi_arq_status *asp; 17477 size_t actual_len; 17478 17479 ASSERT(un != NULL); 17480 ASSERT(mutex_owned(SD_MUTEX(un))); 17481 ASSERT(bp != NULL); 17482 ASSERT(xp != NULL); 17483 ASSERT(pktp != NULL); 17484 ASSERT(pktp != un->un_rqs_pktp); 17485 ASSERT(bp != un->un_rqs_bp); 17486 17487 /* 17488 * For auto-request sense, we get a scsi_arq_status back from 17489 * the HBA, with the sense data in the sts_sensedata member. 17490 * The pkt_scbp of the packet points to this scsi_arq_status. 17491 */ 17492 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17493 17494 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17495 /* 17496 * The auto REQUEST SENSE failed; see if we can re-try 17497 * the original command. 17498 */ 17499 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17500 "auto request sense failed (reason=%s)\n", 17501 scsi_rname(asp->sts_rqpkt_reason)); 17502 17503 sd_reset_target(un, pktp); 17504 17505 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17506 NULL, NULL, EIO, (clock_t)0, NULL); 17507 return; 17508 } 17509 17510 /* Save the relevant sense info into the xp for the original cmd. */ 17511 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17512 xp->xb_sense_state = asp->sts_rqpkt_state; 17513 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17514 if (xp->xb_sense_state & STATE_XARQ_DONE) { 17515 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17516 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17517 MAX_SENSE_LENGTH); 17518 } else { 17519 if (xp->xb_sense_resid > SENSE_LENGTH) { 17520 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17521 } else { 17522 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 17523 } 17524 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17525 if ((((struct uscsi_cmd *) 17526 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 17527 xp->xb_sense_resid = (((struct uscsi_cmd *) 17528 (xp->xb_pktinfo))->uscsi_rqlen) - 17529 actual_len; 17530 } else { 17531 xp->xb_sense_resid = 0; 17532 } 17533 } 17534 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 17535 } 17536 17537 /* 17538 * See if we have valid sense data, if so then turn it over to 17539 * sd_decode_sense() to figure out the right course of action. 17540 */ 17541 if (sd_validate_sense_data(un, bp, xp, actual_len) == 17542 SD_SENSE_DATA_IS_VALID) { 17543 sd_decode_sense(un, bp, xp, pktp); 17544 } 17545 } 17546 17547 17548 /* 17549 * Function: sd_print_sense_failed_msg 17550 * 17551 * Description: Print log message when RQS has failed. 17552 * 17553 * Arguments: un - ptr to associated softstate 17554 * bp - ptr to buf(9S) for the command 17555 * arg - generic message string ptr 17556 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17557 * or SD_NO_RETRY_ISSUED 17558 * 17559 * Context: May be called from interrupt context 17560 */ 17561 17562 static void 17563 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17564 int code) 17565 { 17566 char *msgp = arg; 17567 17568 ASSERT(un != NULL); 17569 ASSERT(mutex_owned(SD_MUTEX(un))); 17570 ASSERT(bp != NULL); 17571 17572 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17573 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17574 } 17575 } 17576 17577 17578 /* 17579 * Function: sd_validate_sense_data 17580 * 17581 * Description: Check the given sense data for validity. 17582 * If the sense data is not valid, the command will 17583 * be either failed or retried! 17584 * 17585 * Return Code: SD_SENSE_DATA_IS_INVALID 17586 * SD_SENSE_DATA_IS_VALID 17587 * 17588 * Context: May be called from interrupt context 17589 */ 17590 17591 static int 17592 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17593 size_t actual_len) 17594 { 17595 struct scsi_extended_sense *esp; 17596 struct scsi_pkt *pktp; 17597 char *msgp = NULL; 17598 sd_ssc_t *sscp; 17599 17600 ASSERT(un != NULL); 17601 ASSERT(mutex_owned(SD_MUTEX(un))); 17602 ASSERT(bp != NULL); 17603 ASSERT(bp != un->un_rqs_bp); 17604 ASSERT(xp != NULL); 17605 ASSERT(un->un_fm_private != NULL); 17606 17607 pktp = SD_GET_PKTP(bp); 17608 ASSERT(pktp != NULL); 17609 17610 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 17611 ASSERT(sscp != NULL); 17612 17613 /* 17614 * Check the status of the RQS command (auto or manual). 17615 */ 17616 switch (xp->xb_sense_status & STATUS_MASK) { 17617 case STATUS_GOOD: 17618 break; 17619 17620 case STATUS_RESERVATION_CONFLICT: 17621 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17622 return (SD_SENSE_DATA_IS_INVALID); 17623 17624 case STATUS_BUSY: 17625 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17626 "Busy Status on REQUEST SENSE\n"); 17627 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17628 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17629 return (SD_SENSE_DATA_IS_INVALID); 17630 17631 case STATUS_QFULL: 17632 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17633 "QFULL Status on REQUEST SENSE\n"); 17634 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17635 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17636 return (SD_SENSE_DATA_IS_INVALID); 17637 17638 case STATUS_CHECK: 17639 case STATUS_TERMINATED: 17640 msgp = "Check Condition on REQUEST SENSE\n"; 17641 goto sense_failed; 17642 17643 default: 17644 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17645 goto sense_failed; 17646 } 17647 17648 /* 17649 * See if we got the minimum required amount of sense data. 17650 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17651 * or less. 17652 */ 17653 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17654 (actual_len == 0)) { 17655 msgp = "Request Sense couldn't get sense data\n"; 17656 goto sense_failed; 17657 } 17658 17659 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17660 msgp = "Not enough sense information\n"; 17661 /* Mark the ssc_flags for detecting invalid sense data */ 17662 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17663 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17664 "sense-data"); 17665 } 17666 goto sense_failed; 17667 } 17668 17669 /* 17670 * We require the extended sense data 17671 */ 17672 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17673 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17674 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17675 static char tmp[8]; 17676 static char buf[148]; 17677 char *p = (char *)(xp->xb_sense_data); 17678 int i; 17679 17680 mutex_enter(&sd_sense_mutex); 17681 (void) strcpy(buf, "undecodable sense information:"); 17682 for (i = 0; i < actual_len; i++) { 17683 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17684 (void) strcpy(&buf[strlen(buf)], tmp); 17685 } 17686 i = strlen(buf); 17687 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17688 17689 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 17690 scsi_log(SD_DEVINFO(un), sd_label, 17691 CE_WARN, buf); 17692 } 17693 mutex_exit(&sd_sense_mutex); 17694 } 17695 17696 /* Mark the ssc_flags for detecting invalid sense data */ 17697 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17698 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17699 "sense-data"); 17700 } 17701 17702 /* Note: Legacy behavior, fail the command with no retry */ 17703 sd_return_failed_command(un, bp, EIO); 17704 return (SD_SENSE_DATA_IS_INVALID); 17705 } 17706 17707 /* 17708 * Check that es_code is valid (es_class concatenated with es_code 17709 * make up the "response code" field. es_class will always be 7, so 17710 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17711 * format. 17712 */ 17713 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17714 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17715 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17716 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17717 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17718 /* Mark the ssc_flags for detecting invalid sense data */ 17719 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17720 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17721 "sense-data"); 17722 } 17723 goto sense_failed; 17724 } 17725 17726 return (SD_SENSE_DATA_IS_VALID); 17727 17728 sense_failed: 17729 /* 17730 * If the request sense failed (for whatever reason), attempt 17731 * to retry the original command. 17732 */ 17733 #if defined(__i386) || defined(__amd64) 17734 /* 17735 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17736 * sddef.h for Sparc platform, and x86 uses 1 binary 17737 * for both SCSI/FC. 17738 * The SD_RETRY_DELAY value need to be adjusted here 17739 * when SD_RETRY_DELAY change in sddef.h 17740 */ 17741 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17742 sd_print_sense_failed_msg, msgp, EIO, 17743 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17744 #else 17745 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17746 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17747 #endif 17748 17749 return (SD_SENSE_DATA_IS_INVALID); 17750 } 17751 17752 /* 17753 * Function: sd_decode_sense 17754 * 17755 * Description: Take recovery action(s) when SCSI Sense Data is received. 17756 * 17757 * Context: Interrupt context. 17758 */ 17759 17760 static void 17761 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17762 struct scsi_pkt *pktp) 17763 { 17764 uint8_t sense_key; 17765 17766 ASSERT(un != NULL); 17767 ASSERT(mutex_owned(SD_MUTEX(un))); 17768 ASSERT(bp != NULL); 17769 ASSERT(bp != un->un_rqs_bp); 17770 ASSERT(xp != NULL); 17771 ASSERT(pktp != NULL); 17772 17773 sense_key = scsi_sense_key(xp->xb_sense_data); 17774 17775 switch (sense_key) { 17776 case KEY_NO_SENSE: 17777 sd_sense_key_no_sense(un, bp, xp, pktp); 17778 break; 17779 case KEY_RECOVERABLE_ERROR: 17780 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17781 bp, xp, pktp); 17782 break; 17783 case KEY_NOT_READY: 17784 sd_sense_key_not_ready(un, xp->xb_sense_data, 17785 bp, xp, pktp); 17786 break; 17787 case KEY_MEDIUM_ERROR: 17788 case KEY_HARDWARE_ERROR: 17789 sd_sense_key_medium_or_hardware_error(un, 17790 xp->xb_sense_data, bp, xp, pktp); 17791 break; 17792 case KEY_ILLEGAL_REQUEST: 17793 sd_sense_key_illegal_request(un, bp, xp, pktp); 17794 break; 17795 case KEY_UNIT_ATTENTION: 17796 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17797 bp, xp, pktp); 17798 break; 17799 case KEY_WRITE_PROTECT: 17800 case KEY_VOLUME_OVERFLOW: 17801 case KEY_MISCOMPARE: 17802 sd_sense_key_fail_command(un, bp, xp, pktp); 17803 break; 17804 case KEY_BLANK_CHECK: 17805 sd_sense_key_blank_check(un, bp, xp, pktp); 17806 break; 17807 case KEY_ABORTED_COMMAND: 17808 sd_sense_key_aborted_command(un, bp, xp, pktp); 17809 break; 17810 case KEY_VENDOR_UNIQUE: 17811 case KEY_COPY_ABORTED: 17812 case KEY_EQUAL: 17813 case KEY_RESERVED: 17814 default: 17815 sd_sense_key_default(un, xp->xb_sense_data, 17816 bp, xp, pktp); 17817 break; 17818 } 17819 } 17820 17821 17822 /* 17823 * Function: sd_dump_memory 17824 * 17825 * Description: Debug logging routine to print the contents of a user provided 17826 * buffer. The output of the buffer is broken up into 256 byte 17827 * segments due to a size constraint of the scsi_log. 17828 * implementation. 17829 * 17830 * Arguments: un - ptr to softstate 17831 * comp - component mask 17832 * title - "title" string to preceed data when printed 17833 * data - ptr to data block to be printed 17834 * len - size of data block to be printed 17835 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17836 * 17837 * Context: May be called from interrupt context 17838 */ 17839 17840 #define SD_DUMP_MEMORY_BUF_SIZE 256 17841 17842 static char *sd_dump_format_string[] = { 17843 " 0x%02x", 17844 " %c" 17845 }; 17846 17847 static void 17848 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17849 int len, int fmt) 17850 { 17851 int i, j; 17852 int avail_count; 17853 int start_offset; 17854 int end_offset; 17855 size_t entry_len; 17856 char *bufp; 17857 char *local_buf; 17858 char *format_string; 17859 17860 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17861 17862 /* 17863 * In the debug version of the driver, this function is called from a 17864 * number of places which are NOPs in the release driver. 17865 * The debug driver therefore has additional methods of filtering 17866 * debug output. 17867 */ 17868 #ifdef SDDEBUG 17869 /* 17870 * In the debug version of the driver we can reduce the amount of debug 17871 * messages by setting sd_error_level to something other than 17872 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17873 * sd_component_mask. 17874 */ 17875 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17876 (sd_error_level != SCSI_ERR_ALL)) { 17877 return; 17878 } 17879 if (((sd_component_mask & comp) == 0) || 17880 (sd_error_level != SCSI_ERR_ALL)) { 17881 return; 17882 } 17883 #else 17884 if (sd_error_level != SCSI_ERR_ALL) { 17885 return; 17886 } 17887 #endif 17888 17889 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17890 bufp = local_buf; 17891 /* 17892 * Available length is the length of local_buf[], minus the 17893 * length of the title string, minus one for the ":", minus 17894 * one for the newline, minus one for the NULL terminator. 17895 * This gives the #bytes available for holding the printed 17896 * values from the given data buffer. 17897 */ 17898 if (fmt == SD_LOG_HEX) { 17899 format_string = sd_dump_format_string[0]; 17900 } else /* SD_LOG_CHAR */ { 17901 format_string = sd_dump_format_string[1]; 17902 } 17903 /* 17904 * Available count is the number of elements from the given 17905 * data buffer that we can fit into the available length. 17906 * This is based upon the size of the format string used. 17907 * Make one entry and find it's size. 17908 */ 17909 (void) sprintf(bufp, format_string, data[0]); 17910 entry_len = strlen(bufp); 17911 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17912 17913 j = 0; 17914 while (j < len) { 17915 bufp = local_buf; 17916 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17917 start_offset = j; 17918 17919 end_offset = start_offset + avail_count; 17920 17921 (void) sprintf(bufp, "%s:", title); 17922 bufp += strlen(bufp); 17923 for (i = start_offset; ((i < end_offset) && (j < len)); 17924 i++, j++) { 17925 (void) sprintf(bufp, format_string, data[i]); 17926 bufp += entry_len; 17927 } 17928 (void) sprintf(bufp, "\n"); 17929 17930 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17931 } 17932 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17933 } 17934 17935 /* 17936 * Function: sd_print_sense_msg 17937 * 17938 * Description: Log a message based upon the given sense data. 17939 * 17940 * Arguments: un - ptr to associated softstate 17941 * bp - ptr to buf(9S) for the command 17942 * arg - ptr to associate sd_sense_info struct 17943 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17944 * or SD_NO_RETRY_ISSUED 17945 * 17946 * Context: May be called from interrupt context 17947 */ 17948 17949 static void 17950 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17951 { 17952 struct sd_xbuf *xp; 17953 struct scsi_pkt *pktp; 17954 uint8_t *sensep; 17955 daddr_t request_blkno; 17956 diskaddr_t err_blkno; 17957 int severity; 17958 int pfa_flag; 17959 extern struct scsi_key_strings scsi_cmds[]; 17960 17961 ASSERT(un != NULL); 17962 ASSERT(mutex_owned(SD_MUTEX(un))); 17963 ASSERT(bp != NULL); 17964 xp = SD_GET_XBUF(bp); 17965 ASSERT(xp != NULL); 17966 pktp = SD_GET_PKTP(bp); 17967 ASSERT(pktp != NULL); 17968 ASSERT(arg != NULL); 17969 17970 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17971 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17972 17973 if ((code == SD_DELAYED_RETRY_ISSUED) || 17974 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17975 severity = SCSI_ERR_RETRYABLE; 17976 } 17977 17978 /* Use absolute block number for the request block number */ 17979 request_blkno = xp->xb_blkno; 17980 17981 /* 17982 * Now try to get the error block number from the sense data 17983 */ 17984 sensep = xp->xb_sense_data; 17985 17986 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17987 (uint64_t *)&err_blkno)) { 17988 /* 17989 * We retrieved the error block number from the information 17990 * portion of the sense data. 17991 * 17992 * For USCSI commands we are better off using the error 17993 * block no. as the requested block no. (This is the best 17994 * we can estimate.) 17995 */ 17996 if ((SD_IS_BUFIO(xp) == FALSE) && 17997 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17998 request_blkno = err_blkno; 17999 } 18000 } else { 18001 /* 18002 * Without the es_valid bit set (for fixed format) or an 18003 * information descriptor (for descriptor format) we cannot 18004 * be certain of the error blkno, so just use the 18005 * request_blkno. 18006 */ 18007 err_blkno = (diskaddr_t)request_blkno; 18008 } 18009 18010 /* 18011 * The following will log the buffer contents for the release driver 18012 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 18013 * level is set to verbose. 18014 */ 18015 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 18016 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 18017 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 18018 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 18019 18020 if (pfa_flag == FALSE) { 18021 /* This is normally only set for USCSI */ 18022 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 18023 return; 18024 } 18025 18026 if ((SD_IS_BUFIO(xp) == TRUE) && 18027 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 18028 (severity < sd_error_level))) { 18029 return; 18030 } 18031 } 18032 /* 18033 * Check for Sonoma Failover and keep a count of how many failed I/O's 18034 */ 18035 if ((SD_IS_LSI(un)) && 18036 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 18037 (scsi_sense_asc(sensep) == 0x94) && 18038 (scsi_sense_ascq(sensep) == 0x01)) { 18039 un->un_sonoma_failure_count++; 18040 if (un->un_sonoma_failure_count > 1) { 18041 return; 18042 } 18043 } 18044 18045 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP || 18046 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) && 18047 (pktp->pkt_resid == 0))) { 18048 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 18049 request_blkno, err_blkno, scsi_cmds, 18050 (struct scsi_extended_sense *)sensep, 18051 un->un_additional_codes, NULL); 18052 } 18053 } 18054 18055 /* 18056 * Function: sd_sense_key_no_sense 18057 * 18058 * Description: Recovery action when sense data was not received. 18059 * 18060 * Context: May be called from interrupt context 18061 */ 18062 18063 static void 18064 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 18065 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18066 { 18067 struct sd_sense_info si; 18068 18069 ASSERT(un != NULL); 18070 ASSERT(mutex_owned(SD_MUTEX(un))); 18071 ASSERT(bp != NULL); 18072 ASSERT(xp != NULL); 18073 ASSERT(pktp != NULL); 18074 18075 si.ssi_severity = SCSI_ERR_FATAL; 18076 si.ssi_pfa_flag = FALSE; 18077 18078 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18079 18080 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18081 &si, EIO, (clock_t)0, NULL); 18082 } 18083 18084 18085 /* 18086 * Function: sd_sense_key_recoverable_error 18087 * 18088 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 18089 * 18090 * Context: May be called from interrupt context 18091 */ 18092 18093 static void 18094 sd_sense_key_recoverable_error(struct sd_lun *un, 18095 uint8_t *sense_datap, 18096 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18097 { 18098 struct sd_sense_info si; 18099 uint8_t asc = scsi_sense_asc(sense_datap); 18100 18101 ASSERT(un != NULL); 18102 ASSERT(mutex_owned(SD_MUTEX(un))); 18103 ASSERT(bp != NULL); 18104 ASSERT(xp != NULL); 18105 ASSERT(pktp != NULL); 18106 18107 /* 18108 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 18109 */ 18110 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 18111 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18112 si.ssi_severity = SCSI_ERR_INFO; 18113 si.ssi_pfa_flag = TRUE; 18114 } else { 18115 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18116 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 18117 si.ssi_severity = SCSI_ERR_RECOVERED; 18118 si.ssi_pfa_flag = FALSE; 18119 } 18120 18121 if (pktp->pkt_resid == 0) { 18122 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18123 sd_return_command(un, bp); 18124 return; 18125 } 18126 18127 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18128 &si, EIO, (clock_t)0, NULL); 18129 } 18130 18131 18132 18133 18134 /* 18135 * Function: sd_sense_key_not_ready 18136 * 18137 * Description: Recovery actions for a SCSI "Not Ready" sense key. 18138 * 18139 * Context: May be called from interrupt context 18140 */ 18141 18142 static void 18143 sd_sense_key_not_ready(struct sd_lun *un, 18144 uint8_t *sense_datap, 18145 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18146 { 18147 struct sd_sense_info si; 18148 uint8_t asc = scsi_sense_asc(sense_datap); 18149 uint8_t ascq = scsi_sense_ascq(sense_datap); 18150 18151 ASSERT(un != NULL); 18152 ASSERT(mutex_owned(SD_MUTEX(un))); 18153 ASSERT(bp != NULL); 18154 ASSERT(xp != NULL); 18155 ASSERT(pktp != NULL); 18156 18157 si.ssi_severity = SCSI_ERR_FATAL; 18158 si.ssi_pfa_flag = FALSE; 18159 18160 /* 18161 * Update error stats after first NOT READY error. Disks may have 18162 * been powered down and may need to be restarted. For CDROMs, 18163 * report NOT READY errors only if media is present. 18164 */ 18165 if ((ISCD(un) && (asc == 0x3A)) || 18166 (xp->xb_nr_retry_count > 0)) { 18167 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18168 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 18169 } 18170 18171 /* 18172 * Just fail if the "not ready" retry limit has been reached. 18173 */ 18174 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 18175 /* Special check for error message printing for removables. */ 18176 if (un->un_f_has_removable_media && (asc == 0x04) && 18177 (ascq >= 0x04)) { 18178 si.ssi_severity = SCSI_ERR_ALL; 18179 } 18180 goto fail_command; 18181 } 18182 18183 /* 18184 * Check the ASC and ASCQ in the sense data as needed, to determine 18185 * what to do. 18186 */ 18187 switch (asc) { 18188 case 0x04: /* LOGICAL UNIT NOT READY */ 18189 /* 18190 * disk drives that don't spin up result in a very long delay 18191 * in format without warning messages. We will log a message 18192 * if the error level is set to verbose. 18193 */ 18194 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18195 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18196 "logical unit not ready, resetting disk\n"); 18197 } 18198 18199 /* 18200 * There are different requirements for CDROMs and disks for 18201 * the number of retries. If a CD-ROM is giving this, it is 18202 * probably reading TOC and is in the process of getting 18203 * ready, so we should keep on trying for a long time to make 18204 * sure that all types of media are taken in account (for 18205 * some media the drive takes a long time to read TOC). For 18206 * disks we do not want to retry this too many times as this 18207 * can cause a long hang in format when the drive refuses to 18208 * spin up (a very common failure). 18209 */ 18210 switch (ascq) { 18211 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 18212 /* 18213 * Disk drives frequently refuse to spin up which 18214 * results in a very long hang in format without 18215 * warning messages. 18216 * 18217 * Note: This code preserves the legacy behavior of 18218 * comparing xb_nr_retry_count against zero for fibre 18219 * channel targets instead of comparing against the 18220 * un_reset_retry_count value. The reason for this 18221 * discrepancy has been so utterly lost beneath the 18222 * Sands of Time that even Indiana Jones could not 18223 * find it. 18224 */ 18225 if (un->un_f_is_fibre == TRUE) { 18226 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18227 (xp->xb_nr_retry_count > 0)) && 18228 (un->un_startstop_timeid == NULL)) { 18229 scsi_log(SD_DEVINFO(un), sd_label, 18230 CE_WARN, "logical unit not ready, " 18231 "resetting disk\n"); 18232 sd_reset_target(un, pktp); 18233 } 18234 } else { 18235 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18236 (xp->xb_nr_retry_count > 18237 un->un_reset_retry_count)) && 18238 (un->un_startstop_timeid == NULL)) { 18239 scsi_log(SD_DEVINFO(un), sd_label, 18240 CE_WARN, "logical unit not ready, " 18241 "resetting disk\n"); 18242 sd_reset_target(un, pktp); 18243 } 18244 } 18245 break; 18246 18247 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 18248 /* 18249 * If the target is in the process of becoming 18250 * ready, just proceed with the retry. This can 18251 * happen with CD-ROMs that take a long time to 18252 * read TOC after a power cycle or reset. 18253 */ 18254 goto do_retry; 18255 18256 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 18257 break; 18258 18259 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 18260 /* 18261 * Retries cannot help here so just fail right away. 18262 */ 18263 goto fail_command; 18264 18265 case 0x88: 18266 /* 18267 * Vendor-unique code for T3/T4: it indicates a 18268 * path problem in a mutipathed config, but as far as 18269 * the target driver is concerned it equates to a fatal 18270 * error, so we should just fail the command right away 18271 * (without printing anything to the console). If this 18272 * is not a T3/T4, fall thru to the default recovery 18273 * action. 18274 * T3/T4 is FC only, don't need to check is_fibre 18275 */ 18276 if (SD_IS_T3(un) || SD_IS_T4(un)) { 18277 sd_return_failed_command(un, bp, EIO); 18278 return; 18279 } 18280 /* FALLTHRU */ 18281 18282 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 18283 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 18284 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 18285 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 18286 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 18287 default: /* Possible future codes in SCSI spec? */ 18288 /* 18289 * For removable-media devices, do not retry if 18290 * ASCQ > 2 as these result mostly from USCSI commands 18291 * on MMC devices issued to check status of an 18292 * operation initiated in immediate mode. Also for 18293 * ASCQ >= 4 do not print console messages as these 18294 * mainly represent a user-initiated operation 18295 * instead of a system failure. 18296 */ 18297 if (un->un_f_has_removable_media) { 18298 si.ssi_severity = SCSI_ERR_ALL; 18299 goto fail_command; 18300 } 18301 break; 18302 } 18303 18304 /* 18305 * As part of our recovery attempt for the NOT READY 18306 * condition, we issue a START STOP UNIT command. However 18307 * we want to wait for a short delay before attempting this 18308 * as there may still be more commands coming back from the 18309 * target with the check condition. To do this we use 18310 * timeout(9F) to call sd_start_stop_unit_callback() after 18311 * the delay interval expires. (sd_start_stop_unit_callback() 18312 * dispatches sd_start_stop_unit_task(), which will issue 18313 * the actual START STOP UNIT command. The delay interval 18314 * is one-half of the delay that we will use to retry the 18315 * command that generated the NOT READY condition. 18316 * 18317 * Note that we could just dispatch sd_start_stop_unit_task() 18318 * from here and allow it to sleep for the delay interval, 18319 * but then we would be tying up the taskq thread 18320 * uncesessarily for the duration of the delay. 18321 * 18322 * Do not issue the START STOP UNIT if the current command 18323 * is already a START STOP UNIT. 18324 */ 18325 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18326 break; 18327 } 18328 18329 /* 18330 * Do not schedule the timeout if one is already pending. 18331 */ 18332 if (un->un_startstop_timeid != NULL) { 18333 SD_INFO(SD_LOG_ERROR, un, 18334 "sd_sense_key_not_ready: restart already issued to" 18335 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18336 ddi_get_instance(SD_DEVINFO(un))); 18337 break; 18338 } 18339 18340 /* 18341 * Schedule the START STOP UNIT command, then queue the command 18342 * for a retry. 18343 * 18344 * Note: A timeout is not scheduled for this retry because we 18345 * want the retry to be serial with the START_STOP_UNIT. The 18346 * retry will be started when the START_STOP_UNIT is completed 18347 * in sd_start_stop_unit_task. 18348 */ 18349 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18350 un, un->un_busy_timeout / 2); 18351 xp->xb_nr_retry_count++; 18352 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18353 return; 18354 18355 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18356 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18357 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18358 "unit does not respond to selection\n"); 18359 } 18360 break; 18361 18362 case 0x3A: /* MEDIUM NOT PRESENT */ 18363 if (sd_error_level >= SCSI_ERR_FATAL) { 18364 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18365 "Caddy not inserted in drive\n"); 18366 } 18367 18368 sr_ejected(un); 18369 un->un_mediastate = DKIO_EJECTED; 18370 /* The state has changed, inform the media watch routines */ 18371 cv_broadcast(&un->un_state_cv); 18372 /* Just fail if no media is present in the drive. */ 18373 goto fail_command; 18374 18375 default: 18376 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18377 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18378 "Unit not Ready. Additional sense code 0x%x\n", 18379 asc); 18380 } 18381 break; 18382 } 18383 18384 do_retry: 18385 18386 /* 18387 * Retry the command, as some targets may report NOT READY for 18388 * several seconds after being reset. 18389 */ 18390 xp->xb_nr_retry_count++; 18391 si.ssi_severity = SCSI_ERR_RETRYABLE; 18392 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18393 &si, EIO, un->un_busy_timeout, NULL); 18394 18395 return; 18396 18397 fail_command: 18398 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18399 sd_return_failed_command(un, bp, EIO); 18400 } 18401 18402 18403 18404 /* 18405 * Function: sd_sense_key_medium_or_hardware_error 18406 * 18407 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18408 * sense key. 18409 * 18410 * Context: May be called from interrupt context 18411 */ 18412 18413 static void 18414 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18415 uint8_t *sense_datap, 18416 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18417 { 18418 struct sd_sense_info si; 18419 uint8_t sense_key = scsi_sense_key(sense_datap); 18420 uint8_t asc = scsi_sense_asc(sense_datap); 18421 18422 ASSERT(un != NULL); 18423 ASSERT(mutex_owned(SD_MUTEX(un))); 18424 ASSERT(bp != NULL); 18425 ASSERT(xp != NULL); 18426 ASSERT(pktp != NULL); 18427 18428 si.ssi_severity = SCSI_ERR_FATAL; 18429 si.ssi_pfa_flag = FALSE; 18430 18431 if (sense_key == KEY_MEDIUM_ERROR) { 18432 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18433 } 18434 18435 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18436 18437 if ((un->un_reset_retry_count != 0) && 18438 (xp->xb_retry_count == un->un_reset_retry_count)) { 18439 mutex_exit(SD_MUTEX(un)); 18440 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18441 if (un->un_f_allow_bus_device_reset == TRUE) { 18442 18443 boolean_t try_resetting_target = B_TRUE; 18444 18445 /* 18446 * We need to be able to handle specific ASC when we are 18447 * handling a KEY_HARDWARE_ERROR. In particular 18448 * taking the default action of resetting the target may 18449 * not be the appropriate way to attempt recovery. 18450 * Resetting a target because of a single LUN failure 18451 * victimizes all LUNs on that target. 18452 * 18453 * This is true for the LSI arrays, if an LSI 18454 * array controller returns an ASC of 0x84 (LUN Dead) we 18455 * should trust it. 18456 */ 18457 18458 if (sense_key == KEY_HARDWARE_ERROR) { 18459 switch (asc) { 18460 case 0x84: 18461 if (SD_IS_LSI(un)) { 18462 try_resetting_target = B_FALSE; 18463 } 18464 break; 18465 default: 18466 break; 18467 } 18468 } 18469 18470 if (try_resetting_target == B_TRUE) { 18471 int reset_retval = 0; 18472 if (un->un_f_lun_reset_enabled == TRUE) { 18473 SD_TRACE(SD_LOG_IO_CORE, un, 18474 "sd_sense_key_medium_or_hardware_" 18475 "error: issuing RESET_LUN\n"); 18476 reset_retval = 18477 scsi_reset(SD_ADDRESS(un), 18478 RESET_LUN); 18479 } 18480 if (reset_retval == 0) { 18481 SD_TRACE(SD_LOG_IO_CORE, un, 18482 "sd_sense_key_medium_or_hardware_" 18483 "error: issuing RESET_TARGET\n"); 18484 (void) scsi_reset(SD_ADDRESS(un), 18485 RESET_TARGET); 18486 } 18487 } 18488 } 18489 mutex_enter(SD_MUTEX(un)); 18490 } 18491 18492 /* 18493 * This really ought to be a fatal error, but we will retry anyway 18494 * as some drives report this as a spurious error. 18495 */ 18496 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18497 &si, EIO, (clock_t)0, NULL); 18498 } 18499 18500 18501 18502 /* 18503 * Function: sd_sense_key_illegal_request 18504 * 18505 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18506 * 18507 * Context: May be called from interrupt context 18508 */ 18509 18510 static void 18511 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18512 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18513 { 18514 struct sd_sense_info si; 18515 18516 ASSERT(un != NULL); 18517 ASSERT(mutex_owned(SD_MUTEX(un))); 18518 ASSERT(bp != NULL); 18519 ASSERT(xp != NULL); 18520 ASSERT(pktp != NULL); 18521 18522 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18523 18524 si.ssi_severity = SCSI_ERR_INFO; 18525 si.ssi_pfa_flag = FALSE; 18526 18527 /* Pointless to retry if the target thinks it's an illegal request */ 18528 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18529 sd_return_failed_command(un, bp, EIO); 18530 } 18531 18532 18533 18534 18535 /* 18536 * Function: sd_sense_key_unit_attention 18537 * 18538 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18539 * 18540 * Context: May be called from interrupt context 18541 */ 18542 18543 static void 18544 sd_sense_key_unit_attention(struct sd_lun *un, 18545 uint8_t *sense_datap, 18546 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18547 { 18548 /* 18549 * For UNIT ATTENTION we allow retries for one minute. Devices 18550 * like Sonoma can return UNIT ATTENTION close to a minute 18551 * under certain conditions. 18552 */ 18553 int retry_check_flag = SD_RETRIES_UA; 18554 boolean_t kstat_updated = B_FALSE; 18555 struct sd_sense_info si; 18556 uint8_t asc = scsi_sense_asc(sense_datap); 18557 uint8_t ascq = scsi_sense_ascq(sense_datap); 18558 18559 ASSERT(un != NULL); 18560 ASSERT(mutex_owned(SD_MUTEX(un))); 18561 ASSERT(bp != NULL); 18562 ASSERT(xp != NULL); 18563 ASSERT(pktp != NULL); 18564 18565 si.ssi_severity = SCSI_ERR_INFO; 18566 si.ssi_pfa_flag = FALSE; 18567 18568 18569 switch (asc) { 18570 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18571 if (sd_report_pfa != 0) { 18572 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18573 si.ssi_pfa_flag = TRUE; 18574 retry_check_flag = SD_RETRIES_STANDARD; 18575 goto do_retry; 18576 } 18577 18578 break; 18579 18580 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18581 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18582 un->un_resvd_status |= 18583 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18584 } 18585 #ifdef _LP64 18586 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18587 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18588 un, KM_NOSLEEP) == 0) { 18589 /* 18590 * If we can't dispatch the task we'll just 18591 * live without descriptor sense. We can 18592 * try again on the next "unit attention" 18593 */ 18594 SD_ERROR(SD_LOG_ERROR, un, 18595 "sd_sense_key_unit_attention: " 18596 "Could not dispatch " 18597 "sd_reenable_dsense_task\n"); 18598 } 18599 } 18600 #endif /* _LP64 */ 18601 /* FALLTHRU */ 18602 18603 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18604 if (!un->un_f_has_removable_media) { 18605 break; 18606 } 18607 18608 /* 18609 * When we get a unit attention from a removable-media device, 18610 * it may be in a state that will take a long time to recover 18611 * (e.g., from a reset). Since we are executing in interrupt 18612 * context here, we cannot wait around for the device to come 18613 * back. So hand this command off to sd_media_change_task() 18614 * for deferred processing under taskq thread context. (Note 18615 * that the command still may be failed if a problem is 18616 * encountered at a later time.) 18617 */ 18618 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18619 KM_NOSLEEP) == 0) { 18620 /* 18621 * Cannot dispatch the request so fail the command. 18622 */ 18623 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18624 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18625 si.ssi_severity = SCSI_ERR_FATAL; 18626 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18627 sd_return_failed_command(un, bp, EIO); 18628 } 18629 18630 /* 18631 * If failed to dispatch sd_media_change_task(), we already 18632 * updated kstat. If succeed to dispatch sd_media_change_task(), 18633 * we should update kstat later if it encounters an error. So, 18634 * we update kstat_updated flag here. 18635 */ 18636 kstat_updated = B_TRUE; 18637 18638 /* 18639 * Either the command has been successfully dispatched to a 18640 * task Q for retrying, or the dispatch failed. In either case 18641 * do NOT retry again by calling sd_retry_command. This sets up 18642 * two retries of the same command and when one completes and 18643 * frees the resources the other will access freed memory, 18644 * a bad thing. 18645 */ 18646 return; 18647 18648 default: 18649 break; 18650 } 18651 18652 /* 18653 * ASC ASCQ 18654 * 2A 09 Capacity data has changed 18655 * 2A 01 Mode parameters changed 18656 * 3F 0E Reported luns data has changed 18657 * Arrays that support logical unit expansion should report 18658 * capacity changes(2Ah/09). Mode parameters changed and 18659 * reported luns data has changed are the approximation. 18660 */ 18661 if (((asc == 0x2a) && (ascq == 0x09)) || 18662 ((asc == 0x2a) && (ascq == 0x01)) || 18663 ((asc == 0x3f) && (ascq == 0x0e))) { 18664 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 18665 KM_NOSLEEP) == 0) { 18666 SD_ERROR(SD_LOG_ERROR, un, 18667 "sd_sense_key_unit_attention: " 18668 "Could not dispatch sd_target_change_task\n"); 18669 } 18670 } 18671 18672 /* 18673 * Update kstat if we haven't done that. 18674 */ 18675 if (!kstat_updated) { 18676 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18677 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18678 } 18679 18680 do_retry: 18681 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18682 EIO, SD_UA_RETRY_DELAY, NULL); 18683 } 18684 18685 18686 18687 /* 18688 * Function: sd_sense_key_fail_command 18689 * 18690 * Description: Use to fail a command when we don't like the sense key that 18691 * was returned. 18692 * 18693 * Context: May be called from interrupt context 18694 */ 18695 18696 static void 18697 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18698 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18699 { 18700 struct sd_sense_info si; 18701 18702 ASSERT(un != NULL); 18703 ASSERT(mutex_owned(SD_MUTEX(un))); 18704 ASSERT(bp != NULL); 18705 ASSERT(xp != NULL); 18706 ASSERT(pktp != NULL); 18707 18708 si.ssi_severity = SCSI_ERR_FATAL; 18709 si.ssi_pfa_flag = FALSE; 18710 18711 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18712 sd_return_failed_command(un, bp, EIO); 18713 } 18714 18715 18716 18717 /* 18718 * Function: sd_sense_key_blank_check 18719 * 18720 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18721 * Has no monetary connotation. 18722 * 18723 * Context: May be called from interrupt context 18724 */ 18725 18726 static void 18727 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18728 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18729 { 18730 struct sd_sense_info si; 18731 18732 ASSERT(un != NULL); 18733 ASSERT(mutex_owned(SD_MUTEX(un))); 18734 ASSERT(bp != NULL); 18735 ASSERT(xp != NULL); 18736 ASSERT(pktp != NULL); 18737 18738 /* 18739 * Blank check is not fatal for removable devices, therefore 18740 * it does not require a console message. 18741 */ 18742 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18743 SCSI_ERR_FATAL; 18744 si.ssi_pfa_flag = FALSE; 18745 18746 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18747 sd_return_failed_command(un, bp, EIO); 18748 } 18749 18750 18751 18752 18753 /* 18754 * Function: sd_sense_key_aborted_command 18755 * 18756 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18757 * 18758 * Context: May be called from interrupt context 18759 */ 18760 18761 static void 18762 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18763 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18764 { 18765 struct sd_sense_info si; 18766 18767 ASSERT(un != NULL); 18768 ASSERT(mutex_owned(SD_MUTEX(un))); 18769 ASSERT(bp != NULL); 18770 ASSERT(xp != NULL); 18771 ASSERT(pktp != NULL); 18772 18773 si.ssi_severity = SCSI_ERR_FATAL; 18774 si.ssi_pfa_flag = FALSE; 18775 18776 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18777 18778 /* 18779 * This really ought to be a fatal error, but we will retry anyway 18780 * as some drives report this as a spurious error. 18781 */ 18782 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18783 &si, EIO, drv_usectohz(100000), NULL); 18784 } 18785 18786 18787 18788 /* 18789 * Function: sd_sense_key_default 18790 * 18791 * Description: Default recovery action for several SCSI sense keys (basically 18792 * attempts a retry). 18793 * 18794 * Context: May be called from interrupt context 18795 */ 18796 18797 static void 18798 sd_sense_key_default(struct sd_lun *un, 18799 uint8_t *sense_datap, 18800 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18801 { 18802 struct sd_sense_info si; 18803 uint8_t sense_key = scsi_sense_key(sense_datap); 18804 18805 ASSERT(un != NULL); 18806 ASSERT(mutex_owned(SD_MUTEX(un))); 18807 ASSERT(bp != NULL); 18808 ASSERT(xp != NULL); 18809 ASSERT(pktp != NULL); 18810 18811 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18812 18813 /* 18814 * Undecoded sense key. Attempt retries and hope that will fix 18815 * the problem. Otherwise, we're dead. 18816 */ 18817 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18818 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18819 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18820 } 18821 18822 si.ssi_severity = SCSI_ERR_FATAL; 18823 si.ssi_pfa_flag = FALSE; 18824 18825 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18826 &si, EIO, (clock_t)0, NULL); 18827 } 18828 18829 18830 18831 /* 18832 * Function: sd_print_retry_msg 18833 * 18834 * Description: Print a message indicating the retry action being taken. 18835 * 18836 * Arguments: un - ptr to associated softstate 18837 * bp - ptr to buf(9S) for the command 18838 * arg - not used. 18839 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18840 * or SD_NO_RETRY_ISSUED 18841 * 18842 * Context: May be called from interrupt context 18843 */ 18844 /* ARGSUSED */ 18845 static void 18846 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18847 { 18848 struct sd_xbuf *xp; 18849 struct scsi_pkt *pktp; 18850 char *reasonp; 18851 char *msgp; 18852 18853 ASSERT(un != NULL); 18854 ASSERT(mutex_owned(SD_MUTEX(un))); 18855 ASSERT(bp != NULL); 18856 pktp = SD_GET_PKTP(bp); 18857 ASSERT(pktp != NULL); 18858 xp = SD_GET_XBUF(bp); 18859 ASSERT(xp != NULL); 18860 18861 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18862 mutex_enter(&un->un_pm_mutex); 18863 if ((un->un_state == SD_STATE_SUSPENDED) || 18864 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18865 (pktp->pkt_flags & FLAG_SILENT)) { 18866 mutex_exit(&un->un_pm_mutex); 18867 goto update_pkt_reason; 18868 } 18869 mutex_exit(&un->un_pm_mutex); 18870 18871 /* 18872 * Suppress messages if they are all the same pkt_reason; with 18873 * TQ, many (up to 256) are returned with the same pkt_reason. 18874 * If we are in panic, then suppress the retry messages. 18875 */ 18876 switch (flag) { 18877 case SD_NO_RETRY_ISSUED: 18878 msgp = "giving up"; 18879 break; 18880 case SD_IMMEDIATE_RETRY_ISSUED: 18881 case SD_DELAYED_RETRY_ISSUED: 18882 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18883 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18884 (sd_error_level != SCSI_ERR_ALL))) { 18885 return; 18886 } 18887 msgp = "retrying command"; 18888 break; 18889 default: 18890 goto update_pkt_reason; 18891 } 18892 18893 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18894 scsi_rname(pktp->pkt_reason)); 18895 18896 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 18897 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18898 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18899 } 18900 18901 update_pkt_reason: 18902 /* 18903 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18904 * This is to prevent multiple console messages for the same failure 18905 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18906 * when the command is retried successfully because there still may be 18907 * more commands coming back with the same value of pktp->pkt_reason. 18908 */ 18909 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18910 un->un_last_pkt_reason = pktp->pkt_reason; 18911 } 18912 } 18913 18914 18915 /* 18916 * Function: sd_print_cmd_incomplete_msg 18917 * 18918 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18919 * 18920 * Arguments: un - ptr to associated softstate 18921 * bp - ptr to buf(9S) for the command 18922 * arg - passed to sd_print_retry_msg() 18923 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18924 * or SD_NO_RETRY_ISSUED 18925 * 18926 * Context: May be called from interrupt context 18927 */ 18928 18929 static void 18930 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18931 int code) 18932 { 18933 dev_info_t *dip; 18934 18935 ASSERT(un != NULL); 18936 ASSERT(mutex_owned(SD_MUTEX(un))); 18937 ASSERT(bp != NULL); 18938 18939 switch (code) { 18940 case SD_NO_RETRY_ISSUED: 18941 /* Command was failed. Someone turned off this target? */ 18942 if (un->un_state != SD_STATE_OFFLINE) { 18943 /* 18944 * Suppress message if we are detaching and 18945 * device has been disconnected 18946 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18947 * private interface and not part of the DDI 18948 */ 18949 dip = un->un_sd->sd_dev; 18950 if (!(DEVI_IS_DETACHING(dip) && 18951 DEVI_IS_DEVICE_REMOVED(dip))) { 18952 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18953 "disk not responding to selection\n"); 18954 } 18955 New_state(un, SD_STATE_OFFLINE); 18956 } 18957 break; 18958 18959 case SD_DELAYED_RETRY_ISSUED: 18960 case SD_IMMEDIATE_RETRY_ISSUED: 18961 default: 18962 /* Command was successfully queued for retry */ 18963 sd_print_retry_msg(un, bp, arg, code); 18964 break; 18965 } 18966 } 18967 18968 18969 /* 18970 * Function: sd_pkt_reason_cmd_incomplete 18971 * 18972 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18973 * 18974 * Context: May be called from interrupt context 18975 */ 18976 18977 static void 18978 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18979 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18980 { 18981 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18982 18983 ASSERT(un != NULL); 18984 ASSERT(mutex_owned(SD_MUTEX(un))); 18985 ASSERT(bp != NULL); 18986 ASSERT(xp != NULL); 18987 ASSERT(pktp != NULL); 18988 18989 /* Do not do a reset if selection did not complete */ 18990 /* Note: Should this not just check the bit? */ 18991 if (pktp->pkt_state != STATE_GOT_BUS) { 18992 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18993 sd_reset_target(un, pktp); 18994 } 18995 18996 /* 18997 * If the target was not successfully selected, then set 18998 * SD_RETRIES_FAILFAST to indicate that we lost communication 18999 * with the target, and further retries and/or commands are 19000 * likely to take a long time. 19001 */ 19002 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 19003 flag |= SD_RETRIES_FAILFAST; 19004 } 19005 19006 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19007 19008 sd_retry_command(un, bp, flag, 19009 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19010 } 19011 19012 19013 19014 /* 19015 * Function: sd_pkt_reason_cmd_tran_err 19016 * 19017 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 19018 * 19019 * Context: May be called from interrupt context 19020 */ 19021 19022 static void 19023 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 19024 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19025 { 19026 ASSERT(un != NULL); 19027 ASSERT(mutex_owned(SD_MUTEX(un))); 19028 ASSERT(bp != NULL); 19029 ASSERT(xp != NULL); 19030 ASSERT(pktp != NULL); 19031 19032 /* 19033 * Do not reset if we got a parity error, or if 19034 * selection did not complete. 19035 */ 19036 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19037 /* Note: Should this not just check the bit for pkt_state? */ 19038 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 19039 (pktp->pkt_state != STATE_GOT_BUS)) { 19040 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19041 sd_reset_target(un, pktp); 19042 } 19043 19044 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19045 19046 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19047 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19048 } 19049 19050 19051 19052 /* 19053 * Function: sd_pkt_reason_cmd_reset 19054 * 19055 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 19056 * 19057 * Context: May be called from interrupt context 19058 */ 19059 19060 static void 19061 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 19062 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19063 { 19064 ASSERT(un != NULL); 19065 ASSERT(mutex_owned(SD_MUTEX(un))); 19066 ASSERT(bp != NULL); 19067 ASSERT(xp != NULL); 19068 ASSERT(pktp != NULL); 19069 19070 /* The target may still be running the command, so try to reset. */ 19071 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19072 sd_reset_target(un, pktp); 19073 19074 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19075 19076 /* 19077 * If pkt_reason is CMD_RESET chances are that this pkt got 19078 * reset because another target on this bus caused it. The target 19079 * that caused it should get CMD_TIMEOUT with pkt_statistics 19080 * of STAT_TIMEOUT/STAT_DEV_RESET. 19081 */ 19082 19083 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19084 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19085 } 19086 19087 19088 19089 19090 /* 19091 * Function: sd_pkt_reason_cmd_aborted 19092 * 19093 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 19094 * 19095 * Context: May be called from interrupt context 19096 */ 19097 19098 static void 19099 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 19100 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19101 { 19102 ASSERT(un != NULL); 19103 ASSERT(mutex_owned(SD_MUTEX(un))); 19104 ASSERT(bp != NULL); 19105 ASSERT(xp != NULL); 19106 ASSERT(pktp != NULL); 19107 19108 /* The target may still be running the command, so try to reset. */ 19109 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19110 sd_reset_target(un, pktp); 19111 19112 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19113 19114 /* 19115 * If pkt_reason is CMD_ABORTED chances are that this pkt got 19116 * aborted because another target on this bus caused it. The target 19117 * that caused it should get CMD_TIMEOUT with pkt_statistics 19118 * of STAT_TIMEOUT/STAT_DEV_RESET. 19119 */ 19120 19121 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19122 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19123 } 19124 19125 19126 19127 /* 19128 * Function: sd_pkt_reason_cmd_timeout 19129 * 19130 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 19131 * 19132 * Context: May be called from interrupt context 19133 */ 19134 19135 static void 19136 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 19137 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19138 { 19139 ASSERT(un != NULL); 19140 ASSERT(mutex_owned(SD_MUTEX(un))); 19141 ASSERT(bp != NULL); 19142 ASSERT(xp != NULL); 19143 ASSERT(pktp != NULL); 19144 19145 19146 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19147 sd_reset_target(un, pktp); 19148 19149 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19150 19151 /* 19152 * A command timeout indicates that we could not establish 19153 * communication with the target, so set SD_RETRIES_FAILFAST 19154 * as further retries/commands are likely to take a long time. 19155 */ 19156 sd_retry_command(un, bp, 19157 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 19158 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19159 } 19160 19161 19162 19163 /* 19164 * Function: sd_pkt_reason_cmd_unx_bus_free 19165 * 19166 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 19167 * 19168 * Context: May be called from interrupt context 19169 */ 19170 19171 static void 19172 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 19173 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19174 { 19175 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 19176 19177 ASSERT(un != NULL); 19178 ASSERT(mutex_owned(SD_MUTEX(un))); 19179 ASSERT(bp != NULL); 19180 ASSERT(xp != NULL); 19181 ASSERT(pktp != NULL); 19182 19183 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19184 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19185 19186 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 19187 sd_print_retry_msg : NULL; 19188 19189 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19190 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19191 } 19192 19193 19194 /* 19195 * Function: sd_pkt_reason_cmd_tag_reject 19196 * 19197 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 19198 * 19199 * Context: May be called from interrupt context 19200 */ 19201 19202 static void 19203 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 19204 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19205 { 19206 ASSERT(un != NULL); 19207 ASSERT(mutex_owned(SD_MUTEX(un))); 19208 ASSERT(bp != NULL); 19209 ASSERT(xp != NULL); 19210 ASSERT(pktp != NULL); 19211 19212 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19213 pktp->pkt_flags = 0; 19214 un->un_tagflags = 0; 19215 if (un->un_f_opt_queueing == TRUE) { 19216 un->un_throttle = min(un->un_throttle, 3); 19217 } else { 19218 un->un_throttle = 1; 19219 } 19220 mutex_exit(SD_MUTEX(un)); 19221 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 19222 mutex_enter(SD_MUTEX(un)); 19223 19224 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19225 19226 /* Legacy behavior not to check retry counts here. */ 19227 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 19228 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19229 } 19230 19231 19232 /* 19233 * Function: sd_pkt_reason_default 19234 * 19235 * Description: Default recovery actions for SCSA pkt_reason values that 19236 * do not have more explicit recovery actions. 19237 * 19238 * Context: May be called from interrupt context 19239 */ 19240 19241 static void 19242 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 19243 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19244 { 19245 ASSERT(un != NULL); 19246 ASSERT(mutex_owned(SD_MUTEX(un))); 19247 ASSERT(bp != NULL); 19248 ASSERT(xp != NULL); 19249 ASSERT(pktp != NULL); 19250 19251 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19252 sd_reset_target(un, pktp); 19253 19254 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19255 19256 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19257 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19258 } 19259 19260 19261 19262 /* 19263 * Function: sd_pkt_status_check_condition 19264 * 19265 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 19266 * 19267 * Context: May be called from interrupt context 19268 */ 19269 19270 static void 19271 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 19272 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19273 { 19274 ASSERT(un != NULL); 19275 ASSERT(mutex_owned(SD_MUTEX(un))); 19276 ASSERT(bp != NULL); 19277 ASSERT(xp != NULL); 19278 ASSERT(pktp != NULL); 19279 19280 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 19281 "entry: buf:0x%p xp:0x%p\n", bp, xp); 19282 19283 /* 19284 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 19285 * command will be retried after the request sense). Otherwise, retry 19286 * the command. Note: we are issuing the request sense even though the 19287 * retry limit may have been reached for the failed command. 19288 */ 19289 if (un->un_f_arq_enabled == FALSE) { 19290 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19291 "no ARQ, sending request sense command\n"); 19292 sd_send_request_sense_command(un, bp, pktp); 19293 } else { 19294 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19295 "ARQ,retrying request sense command\n"); 19296 #if defined(__i386) || defined(__amd64) 19297 /* 19298 * The SD_RETRY_DELAY value need to be adjusted here 19299 * when SD_RETRY_DELAY change in sddef.h 19300 */ 19301 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19302 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 19303 NULL); 19304 #else 19305 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 19306 EIO, SD_RETRY_DELAY, NULL); 19307 #endif 19308 } 19309 19310 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 19311 } 19312 19313 19314 /* 19315 * Function: sd_pkt_status_busy 19316 * 19317 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 19318 * 19319 * Context: May be called from interrupt context 19320 */ 19321 19322 static void 19323 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19324 struct scsi_pkt *pktp) 19325 { 19326 ASSERT(un != NULL); 19327 ASSERT(mutex_owned(SD_MUTEX(un))); 19328 ASSERT(bp != NULL); 19329 ASSERT(xp != NULL); 19330 ASSERT(pktp != NULL); 19331 19332 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19333 "sd_pkt_status_busy: entry\n"); 19334 19335 /* If retries are exhausted, just fail the command. */ 19336 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19337 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19338 "device busy too long\n"); 19339 sd_return_failed_command(un, bp, EIO); 19340 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19341 "sd_pkt_status_busy: exit\n"); 19342 return; 19343 } 19344 xp->xb_retry_count++; 19345 19346 /* 19347 * Try to reset the target. However, we do not want to perform 19348 * more than one reset if the device continues to fail. The reset 19349 * will be performed when the retry count reaches the reset 19350 * threshold. This threshold should be set such that at least 19351 * one retry is issued before the reset is performed. 19352 */ 19353 if (xp->xb_retry_count == 19354 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19355 int rval = 0; 19356 mutex_exit(SD_MUTEX(un)); 19357 if (un->un_f_allow_bus_device_reset == TRUE) { 19358 /* 19359 * First try to reset the LUN; if we cannot then 19360 * try to reset the target. 19361 */ 19362 if (un->un_f_lun_reset_enabled == TRUE) { 19363 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19364 "sd_pkt_status_busy: RESET_LUN\n"); 19365 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19366 } 19367 if (rval == 0) { 19368 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19369 "sd_pkt_status_busy: RESET_TARGET\n"); 19370 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19371 } 19372 } 19373 if (rval == 0) { 19374 /* 19375 * If the RESET_LUN and/or RESET_TARGET failed, 19376 * try RESET_ALL 19377 */ 19378 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19379 "sd_pkt_status_busy: RESET_ALL\n"); 19380 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19381 } 19382 mutex_enter(SD_MUTEX(un)); 19383 if (rval == 0) { 19384 /* 19385 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19386 * At this point we give up & fail the command. 19387 */ 19388 sd_return_failed_command(un, bp, EIO); 19389 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19390 "sd_pkt_status_busy: exit (failed cmd)\n"); 19391 return; 19392 } 19393 } 19394 19395 /* 19396 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19397 * we have already checked the retry counts above. 19398 */ 19399 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19400 EIO, un->un_busy_timeout, NULL); 19401 19402 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19403 "sd_pkt_status_busy: exit\n"); 19404 } 19405 19406 19407 /* 19408 * Function: sd_pkt_status_reservation_conflict 19409 * 19410 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19411 * command status. 19412 * 19413 * Context: May be called from interrupt context 19414 */ 19415 19416 static void 19417 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19418 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19419 { 19420 ASSERT(un != NULL); 19421 ASSERT(mutex_owned(SD_MUTEX(un))); 19422 ASSERT(bp != NULL); 19423 ASSERT(xp != NULL); 19424 ASSERT(pktp != NULL); 19425 19426 /* 19427 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19428 * conflict could be due to various reasons like incorrect keys, not 19429 * registered or not reserved etc. So, we return EACCES to the caller. 19430 */ 19431 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19432 int cmd = SD_GET_PKT_OPCODE(pktp); 19433 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19434 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19435 sd_return_failed_command(un, bp, EACCES); 19436 return; 19437 } 19438 } 19439 19440 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19441 19442 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19443 if (sd_failfast_enable != 0) { 19444 /* By definition, we must panic here.... */ 19445 sd_panic_for_res_conflict(un); 19446 /*NOTREACHED*/ 19447 } 19448 SD_ERROR(SD_LOG_IO, un, 19449 "sd_handle_resv_conflict: Disk Reserved\n"); 19450 sd_return_failed_command(un, bp, EACCES); 19451 return; 19452 } 19453 19454 /* 19455 * 1147670: retry only if sd_retry_on_reservation_conflict 19456 * property is set (default is 1). Retries will not succeed 19457 * on a disk reserved by another initiator. HA systems 19458 * may reset this via sd.conf to avoid these retries. 19459 * 19460 * Note: The legacy return code for this failure is EIO, however EACCES 19461 * seems more appropriate for a reservation conflict. 19462 */ 19463 if (sd_retry_on_reservation_conflict == 0) { 19464 SD_ERROR(SD_LOG_IO, un, 19465 "sd_handle_resv_conflict: Device Reserved\n"); 19466 sd_return_failed_command(un, bp, EIO); 19467 return; 19468 } 19469 19470 /* 19471 * Retry the command if we can. 19472 * 19473 * Note: The legacy return code for this failure is EIO, however EACCES 19474 * seems more appropriate for a reservation conflict. 19475 */ 19476 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19477 (clock_t)2, NULL); 19478 } 19479 19480 19481 19482 /* 19483 * Function: sd_pkt_status_qfull 19484 * 19485 * Description: Handle a QUEUE FULL condition from the target. This can 19486 * occur if the HBA does not handle the queue full condition. 19487 * (Basically this means third-party HBAs as Sun HBAs will 19488 * handle the queue full condition.) Note that if there are 19489 * some commands already in the transport, then the queue full 19490 * has occurred because the queue for this nexus is actually 19491 * full. If there are no commands in the transport, then the 19492 * queue full is resulting from some other initiator or lun 19493 * consuming all the resources at the target. 19494 * 19495 * Context: May be called from interrupt context 19496 */ 19497 19498 static void 19499 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19500 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19501 { 19502 ASSERT(un != NULL); 19503 ASSERT(mutex_owned(SD_MUTEX(un))); 19504 ASSERT(bp != NULL); 19505 ASSERT(xp != NULL); 19506 ASSERT(pktp != NULL); 19507 19508 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19509 "sd_pkt_status_qfull: entry\n"); 19510 19511 /* 19512 * Just lower the QFULL throttle and retry the command. Note that 19513 * we do not limit the number of retries here. 19514 */ 19515 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19516 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19517 SD_RESTART_TIMEOUT, NULL); 19518 19519 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19520 "sd_pkt_status_qfull: exit\n"); 19521 } 19522 19523 19524 /* 19525 * Function: sd_reset_target 19526 * 19527 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19528 * RESET_TARGET, or RESET_ALL. 19529 * 19530 * Context: May be called under interrupt context. 19531 */ 19532 19533 static void 19534 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19535 { 19536 int rval = 0; 19537 19538 ASSERT(un != NULL); 19539 ASSERT(mutex_owned(SD_MUTEX(un))); 19540 ASSERT(pktp != NULL); 19541 19542 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19543 19544 /* 19545 * No need to reset if the transport layer has already done so. 19546 */ 19547 if ((pktp->pkt_statistics & 19548 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19549 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19550 "sd_reset_target: no reset\n"); 19551 return; 19552 } 19553 19554 mutex_exit(SD_MUTEX(un)); 19555 19556 if (un->un_f_allow_bus_device_reset == TRUE) { 19557 if (un->un_f_lun_reset_enabled == TRUE) { 19558 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19559 "sd_reset_target: RESET_LUN\n"); 19560 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19561 } 19562 if (rval == 0) { 19563 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19564 "sd_reset_target: RESET_TARGET\n"); 19565 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19566 } 19567 } 19568 19569 if (rval == 0) { 19570 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19571 "sd_reset_target: RESET_ALL\n"); 19572 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19573 } 19574 19575 mutex_enter(SD_MUTEX(un)); 19576 19577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19578 } 19579 19580 /* 19581 * Function: sd_target_change_task 19582 * 19583 * Description: Handle dynamic target change 19584 * 19585 * Context: Executes in a taskq() thread context 19586 */ 19587 static void 19588 sd_target_change_task(void *arg) 19589 { 19590 struct sd_lun *un = arg; 19591 uint64_t capacity; 19592 diskaddr_t label_cap; 19593 uint_t lbasize; 19594 sd_ssc_t *ssc; 19595 19596 ASSERT(un != NULL); 19597 ASSERT(!mutex_owned(SD_MUTEX(un))); 19598 19599 if ((un->un_f_blockcount_is_valid == FALSE) || 19600 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 19601 return; 19602 } 19603 19604 ssc = sd_ssc_init(un); 19605 19606 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity, 19607 &lbasize, SD_PATH_DIRECT) != 0) { 19608 SD_ERROR(SD_LOG_ERROR, un, 19609 "sd_target_change_task: fail to read capacity\n"); 19610 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19611 goto task_exit; 19612 } 19613 19614 mutex_enter(SD_MUTEX(un)); 19615 if (capacity <= un->un_blockcount) { 19616 mutex_exit(SD_MUTEX(un)); 19617 goto task_exit; 19618 } 19619 19620 sd_update_block_info(un, lbasize, capacity); 19621 mutex_exit(SD_MUTEX(un)); 19622 19623 /* 19624 * If lun is EFI labeled and lun capacity is greater than the 19625 * capacity contained in the label, log a sys event. 19626 */ 19627 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 19628 (void*)SD_PATH_DIRECT) == 0) { 19629 mutex_enter(SD_MUTEX(un)); 19630 if (un->un_f_blockcount_is_valid && 19631 un->un_blockcount > label_cap) { 19632 mutex_exit(SD_MUTEX(un)); 19633 sd_log_lun_expansion_event(un, KM_SLEEP); 19634 } else { 19635 mutex_exit(SD_MUTEX(un)); 19636 } 19637 } 19638 19639 task_exit: 19640 sd_ssc_fini(ssc); 19641 } 19642 19643 19644 /* 19645 * Function: sd_log_dev_status_event 19646 * 19647 * Description: Log EC_dev_status sysevent 19648 * 19649 * Context: Never called from interrupt context 19650 */ 19651 static void 19652 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag) 19653 { 19654 int err; 19655 char *path; 19656 nvlist_t *attr_list; 19657 19658 /* Allocate and build sysevent attribute list */ 19659 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 19660 if (err != 0) { 19661 SD_ERROR(SD_LOG_ERROR, un, 19662 "sd_log_dev_status_event: fail to allocate space\n"); 19663 return; 19664 } 19665 19666 path = kmem_alloc(MAXPATHLEN, km_flag); 19667 if (path == NULL) { 19668 nvlist_free(attr_list); 19669 SD_ERROR(SD_LOG_ERROR, un, 19670 "sd_log_dev_status_event: fail to allocate space\n"); 19671 return; 19672 } 19673 /* 19674 * Add path attribute to identify the lun. 19675 * We are using minor node 'a' as the sysevent attribute. 19676 */ 19677 (void) snprintf(path, MAXPATHLEN, "/devices"); 19678 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 19679 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 19680 ":a"); 19681 19682 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path); 19683 if (err != 0) { 19684 nvlist_free(attr_list); 19685 kmem_free(path, MAXPATHLEN); 19686 SD_ERROR(SD_LOG_ERROR, un, 19687 "sd_log_dev_status_event: fail to add attribute\n"); 19688 return; 19689 } 19690 19691 /* Log dynamic lun expansion sysevent */ 19692 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 19693 esc, attr_list, NULL, km_flag); 19694 if (err != DDI_SUCCESS) { 19695 SD_ERROR(SD_LOG_ERROR, un, 19696 "sd_log_dev_status_event: fail to log sysevent\n"); 19697 } 19698 19699 nvlist_free(attr_list); 19700 kmem_free(path, MAXPATHLEN); 19701 } 19702 19703 19704 /* 19705 * Function: sd_log_lun_expansion_event 19706 * 19707 * Description: Log lun expansion sys event 19708 * 19709 * Context: Never called from interrupt context 19710 */ 19711 static void 19712 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 19713 { 19714 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag); 19715 } 19716 19717 19718 /* 19719 * Function: sd_log_eject_request_event 19720 * 19721 * Description: Log eject request sysevent 19722 * 19723 * Context: Never called from interrupt context 19724 */ 19725 static void 19726 sd_log_eject_request_event(struct sd_lun *un, int km_flag) 19727 { 19728 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag); 19729 } 19730 19731 19732 /* 19733 * Function: sd_media_change_task 19734 * 19735 * Description: Recovery action for CDROM to become available. 19736 * 19737 * Context: Executes in a taskq() thread context 19738 */ 19739 19740 static void 19741 sd_media_change_task(void *arg) 19742 { 19743 struct scsi_pkt *pktp = arg; 19744 struct sd_lun *un; 19745 struct buf *bp; 19746 struct sd_xbuf *xp; 19747 int err = 0; 19748 int retry_count = 0; 19749 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19750 struct sd_sense_info si; 19751 19752 ASSERT(pktp != NULL); 19753 bp = (struct buf *)pktp->pkt_private; 19754 ASSERT(bp != NULL); 19755 xp = SD_GET_XBUF(bp); 19756 ASSERT(xp != NULL); 19757 un = SD_GET_UN(bp); 19758 ASSERT(un != NULL); 19759 ASSERT(!mutex_owned(SD_MUTEX(un))); 19760 ASSERT(un->un_f_monitor_media_state); 19761 19762 si.ssi_severity = SCSI_ERR_INFO; 19763 si.ssi_pfa_flag = FALSE; 19764 19765 /* 19766 * When a reset is issued on a CDROM, it takes a long time to 19767 * recover. First few attempts to read capacity and other things 19768 * related to handling unit attention fail (with a ASC 0x4 and 19769 * ASCQ 0x1). In that case we want to do enough retries and we want 19770 * to limit the retries in other cases of genuine failures like 19771 * no media in drive. 19772 */ 19773 while (retry_count++ < retry_limit) { 19774 if ((err = sd_handle_mchange(un)) == 0) { 19775 break; 19776 } 19777 if (err == EAGAIN) { 19778 retry_limit = SD_UNIT_ATTENTION_RETRY; 19779 } 19780 /* Sleep for 0.5 sec. & try again */ 19781 delay(drv_usectohz(500000)); 19782 } 19783 19784 /* 19785 * Dispatch (retry or fail) the original command here, 19786 * along with appropriate console messages.... 19787 * 19788 * Must grab the mutex before calling sd_retry_command, 19789 * sd_print_sense_msg and sd_return_failed_command. 19790 */ 19791 mutex_enter(SD_MUTEX(un)); 19792 if (err != SD_CMD_SUCCESS) { 19793 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19794 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19795 si.ssi_severity = SCSI_ERR_FATAL; 19796 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19797 sd_return_failed_command(un, bp, EIO); 19798 } else { 19799 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg, 19800 &si, EIO, (clock_t)0, NULL); 19801 } 19802 mutex_exit(SD_MUTEX(un)); 19803 } 19804 19805 19806 19807 /* 19808 * Function: sd_handle_mchange 19809 * 19810 * Description: Perform geometry validation & other recovery when CDROM 19811 * has been removed from drive. 19812 * 19813 * Return Code: 0 for success 19814 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19815 * sd_send_scsi_READ_CAPACITY() 19816 * 19817 * Context: Executes in a taskq() thread context 19818 */ 19819 19820 static int 19821 sd_handle_mchange(struct sd_lun *un) 19822 { 19823 uint64_t capacity; 19824 uint32_t lbasize; 19825 int rval; 19826 sd_ssc_t *ssc; 19827 19828 ASSERT(!mutex_owned(SD_MUTEX(un))); 19829 ASSERT(un->un_f_monitor_media_state); 19830 19831 ssc = sd_ssc_init(un); 19832 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 19833 SD_PATH_DIRECT_PRIORITY); 19834 19835 if (rval != 0) 19836 goto failed; 19837 19838 mutex_enter(SD_MUTEX(un)); 19839 sd_update_block_info(un, lbasize, capacity); 19840 19841 if (un->un_errstats != NULL) { 19842 struct sd_errstats *stp = 19843 (struct sd_errstats *)un->un_errstats->ks_data; 19844 stp->sd_capacity.value.ui64 = (uint64_t) 19845 ((uint64_t)un->un_blockcount * 19846 (uint64_t)un->un_tgt_blocksize); 19847 } 19848 19849 /* 19850 * Check if the media in the device is writable or not 19851 */ 19852 if (ISCD(un)) { 19853 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY); 19854 } 19855 19856 /* 19857 * Note: Maybe let the strategy/partitioning chain worry about getting 19858 * valid geometry. 19859 */ 19860 mutex_exit(SD_MUTEX(un)); 19861 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 19862 19863 19864 if (cmlb_validate(un->un_cmlbhandle, 0, 19865 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 19866 sd_ssc_fini(ssc); 19867 return (EIO); 19868 } else { 19869 if (un->un_f_pkstats_enabled) { 19870 sd_set_pstats(un); 19871 SD_TRACE(SD_LOG_IO_PARTITION, un, 19872 "sd_handle_mchange: un:0x%p pstats created and " 19873 "set\n", un); 19874 } 19875 } 19876 19877 /* 19878 * Try to lock the door 19879 */ 19880 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 19881 SD_PATH_DIRECT_PRIORITY); 19882 failed: 19883 if (rval != 0) 19884 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19885 sd_ssc_fini(ssc); 19886 return (rval); 19887 } 19888 19889 19890 /* 19891 * Function: sd_send_scsi_DOORLOCK 19892 * 19893 * Description: Issue the scsi DOOR LOCK command 19894 * 19895 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19896 * structure for this target. 19897 * flag - SD_REMOVAL_ALLOW 19898 * SD_REMOVAL_PREVENT 19899 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19900 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19901 * to use the USCSI "direct" chain and bypass the normal 19902 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19903 * command is issued as part of an error recovery action. 19904 * 19905 * Return Code: 0 - Success 19906 * errno return code from sd_ssc_send() 19907 * 19908 * Context: Can sleep. 19909 */ 19910 19911 static int 19912 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag) 19913 { 19914 struct scsi_extended_sense sense_buf; 19915 union scsi_cdb cdb; 19916 struct uscsi_cmd ucmd_buf; 19917 int status; 19918 struct sd_lun *un; 19919 19920 ASSERT(ssc != NULL); 19921 un = ssc->ssc_un; 19922 ASSERT(un != NULL); 19923 ASSERT(!mutex_owned(SD_MUTEX(un))); 19924 19925 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19926 19927 /* already determined doorlock is not supported, fake success */ 19928 if (un->un_f_doorlock_supported == FALSE) { 19929 return (0); 19930 } 19931 19932 /* 19933 * If we are ejecting and see an SD_REMOVAL_PREVENT 19934 * ignore the command so we can complete the eject 19935 * operation. 19936 */ 19937 if (flag == SD_REMOVAL_PREVENT) { 19938 mutex_enter(SD_MUTEX(un)); 19939 if (un->un_f_ejecting == TRUE) { 19940 mutex_exit(SD_MUTEX(un)); 19941 return (EAGAIN); 19942 } 19943 mutex_exit(SD_MUTEX(un)); 19944 } 19945 19946 bzero(&cdb, sizeof (cdb)); 19947 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19948 19949 cdb.scc_cmd = SCMD_DOORLOCK; 19950 cdb.cdb_opaque[4] = (uchar_t)flag; 19951 19952 ucmd_buf.uscsi_cdb = (char *)&cdb; 19953 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19954 ucmd_buf.uscsi_bufaddr = NULL; 19955 ucmd_buf.uscsi_buflen = 0; 19956 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19957 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19958 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19959 ucmd_buf.uscsi_timeout = 15; 19960 19961 SD_TRACE(SD_LOG_IO, un, 19962 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n"); 19963 19964 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19965 UIO_SYSSPACE, path_flag); 19966 19967 if (status == 0) 19968 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19969 19970 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19971 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19972 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19973 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19974 19975 /* fake success and skip subsequent doorlock commands */ 19976 un->un_f_doorlock_supported = FALSE; 19977 return (0); 19978 } 19979 19980 return (status); 19981 } 19982 19983 /* 19984 * Function: sd_send_scsi_READ_CAPACITY 19985 * 19986 * Description: This routine uses the scsi READ CAPACITY command to determine 19987 * the device capacity in number of blocks and the device native 19988 * block size. If this function returns a failure, then the 19989 * values in *capp and *lbap are undefined. If the capacity 19990 * returned is 0xffffffff then the lun is too large for a 19991 * normal READ CAPACITY command and the results of a 19992 * READ CAPACITY 16 will be used instead. 19993 * 19994 * Arguments: ssc - ssc contains ptr to soft state struct for the target 19995 * capp - ptr to unsigned 64-bit variable to receive the 19996 * capacity value from the command. 19997 * lbap - ptr to unsigned 32-bit varaible to receive the 19998 * block size value from the command 19999 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20000 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20001 * to use the USCSI "direct" chain and bypass the normal 20002 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20003 * command is issued as part of an error recovery action. 20004 * 20005 * Return Code: 0 - Success 20006 * EIO - IO error 20007 * EACCES - Reservation conflict detected 20008 * EAGAIN - Device is becoming ready 20009 * errno return code from sd_ssc_send() 20010 * 20011 * Context: Can sleep. Blocks until command completes. 20012 */ 20013 20014 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 20015 20016 static int 20017 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap, 20018 int path_flag) 20019 { 20020 struct scsi_extended_sense sense_buf; 20021 struct uscsi_cmd ucmd_buf; 20022 union scsi_cdb cdb; 20023 uint32_t *capacity_buf; 20024 uint64_t capacity; 20025 uint32_t lbasize; 20026 uint32_t pbsize; 20027 int status; 20028 struct sd_lun *un; 20029 20030 ASSERT(ssc != NULL); 20031 20032 un = ssc->ssc_un; 20033 ASSERT(un != NULL); 20034 ASSERT(!mutex_owned(SD_MUTEX(un))); 20035 ASSERT(capp != NULL); 20036 ASSERT(lbap != NULL); 20037 20038 SD_TRACE(SD_LOG_IO, un, 20039 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20040 20041 /* 20042 * First send a READ_CAPACITY command to the target. 20043 * (This command is mandatory under SCSI-2.) 20044 * 20045 * Set up the CDB for the READ_CAPACITY command. The Partial 20046 * Medium Indicator bit is cleared. The address field must be 20047 * zero if the PMI bit is zero. 20048 */ 20049 bzero(&cdb, sizeof (cdb)); 20050 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20051 20052 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 20053 20054 cdb.scc_cmd = SCMD_READ_CAPACITY; 20055 20056 ucmd_buf.uscsi_cdb = (char *)&cdb; 20057 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20058 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 20059 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 20060 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20061 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20062 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20063 ucmd_buf.uscsi_timeout = 60; 20064 20065 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20066 UIO_SYSSPACE, path_flag); 20067 20068 switch (status) { 20069 case 0: 20070 /* Return failure if we did not get valid capacity data. */ 20071 if (ucmd_buf.uscsi_resid != 0) { 20072 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20073 "sd_send_scsi_READ_CAPACITY received invalid " 20074 "capacity data"); 20075 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20076 return (EIO); 20077 } 20078 /* 20079 * Read capacity and block size from the READ CAPACITY 10 data. 20080 * This data may be adjusted later due to device specific 20081 * issues. 20082 * 20083 * According to the SCSI spec, the READ CAPACITY 10 20084 * command returns the following: 20085 * 20086 * bytes 0-3: Maximum logical block address available. 20087 * (MSB in byte:0 & LSB in byte:3) 20088 * 20089 * bytes 4-7: Block length in bytes 20090 * (MSB in byte:4 & LSB in byte:7) 20091 * 20092 */ 20093 capacity = BE_32(capacity_buf[0]); 20094 lbasize = BE_32(capacity_buf[1]); 20095 20096 /* 20097 * Done with capacity_buf 20098 */ 20099 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20100 20101 /* 20102 * if the reported capacity is set to all 0xf's, then 20103 * this disk is too large and requires SBC-2 commands. 20104 * Reissue the request using READ CAPACITY 16. 20105 */ 20106 if (capacity == 0xffffffff) { 20107 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20108 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, 20109 &lbasize, &pbsize, path_flag); 20110 if (status != 0) { 20111 return (status); 20112 } else { 20113 goto rc16_done; 20114 } 20115 } 20116 break; /* Success! */ 20117 case EIO: 20118 switch (ucmd_buf.uscsi_status) { 20119 case STATUS_RESERVATION_CONFLICT: 20120 status = EACCES; 20121 break; 20122 case STATUS_CHECK: 20123 /* 20124 * Check condition; look for ASC/ASCQ of 0x04/0x01 20125 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20126 */ 20127 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20128 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20129 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20130 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20131 return (EAGAIN); 20132 } 20133 break; 20134 default: 20135 break; 20136 } 20137 /* FALLTHRU */ 20138 default: 20139 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20140 return (status); 20141 } 20142 20143 /* 20144 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20145 * (2352 and 0 are common) so for these devices always force the value 20146 * to 2048 as required by the ATAPI specs. 20147 */ 20148 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20149 lbasize = 2048; 20150 } 20151 20152 /* 20153 * Get the maximum LBA value from the READ CAPACITY data. 20154 * Here we assume that the Partial Medium Indicator (PMI) bit 20155 * was cleared when issuing the command. This means that the LBA 20156 * returned from the device is the LBA of the last logical block 20157 * on the logical unit. The actual logical block count will be 20158 * this value plus one. 20159 */ 20160 capacity += 1; 20161 20162 /* 20163 * Currently, for removable media, the capacity is saved in terms 20164 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20165 */ 20166 if (un->un_f_has_removable_media) 20167 capacity *= (lbasize / un->un_sys_blocksize); 20168 20169 rc16_done: 20170 20171 /* 20172 * Copy the values from the READ CAPACITY command into the space 20173 * provided by the caller. 20174 */ 20175 *capp = capacity; 20176 *lbap = lbasize; 20177 20178 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 20179 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 20180 20181 /* 20182 * Both the lbasize and capacity from the device must be nonzero, 20183 * otherwise we assume that the values are not valid and return 20184 * failure to the caller. (4203735) 20185 */ 20186 if ((capacity == 0) || (lbasize == 0)) { 20187 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20188 "sd_send_scsi_READ_CAPACITY received invalid value " 20189 "capacity %llu lbasize %d", capacity, lbasize); 20190 return (EIO); 20191 } 20192 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20193 return (0); 20194 } 20195 20196 /* 20197 * Function: sd_send_scsi_READ_CAPACITY_16 20198 * 20199 * Description: This routine uses the scsi READ CAPACITY 16 command to 20200 * determine the device capacity in number of blocks and the 20201 * device native block size. If this function returns a failure, 20202 * then the values in *capp and *lbap are undefined. 20203 * This routine should be called by sd_send_scsi_READ_CAPACITY 20204 * which will apply any device specific adjustments to capacity 20205 * and lbasize. One exception is it is also called by 20206 * sd_get_media_info_ext. In that function, there is no need to 20207 * adjust the capacity and lbasize. 20208 * 20209 * Arguments: ssc - ssc contains ptr to soft state struct for the target 20210 * capp - ptr to unsigned 64-bit variable to receive the 20211 * capacity value from the command. 20212 * lbap - ptr to unsigned 32-bit varaible to receive the 20213 * block size value from the command 20214 * psp - ptr to unsigned 32-bit variable to receive the 20215 * physical block size value from the command 20216 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20217 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20218 * to use the USCSI "direct" chain and bypass the normal 20219 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 20220 * this command is issued as part of an error recovery 20221 * action. 20222 * 20223 * Return Code: 0 - Success 20224 * EIO - IO error 20225 * EACCES - Reservation conflict detected 20226 * EAGAIN - Device is becoming ready 20227 * errno return code from sd_ssc_send() 20228 * 20229 * Context: Can sleep. Blocks until command completes. 20230 */ 20231 20232 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 20233 20234 static int 20235 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 20236 uint32_t *lbap, uint32_t *psp, int path_flag) 20237 { 20238 struct scsi_extended_sense sense_buf; 20239 struct uscsi_cmd ucmd_buf; 20240 union scsi_cdb cdb; 20241 uint64_t *capacity16_buf; 20242 uint64_t capacity; 20243 uint32_t lbasize; 20244 uint32_t pbsize; 20245 uint32_t lbpb_exp; 20246 int status; 20247 struct sd_lun *un; 20248 20249 ASSERT(ssc != NULL); 20250 20251 un = ssc->ssc_un; 20252 ASSERT(un != NULL); 20253 ASSERT(!mutex_owned(SD_MUTEX(un))); 20254 ASSERT(capp != NULL); 20255 ASSERT(lbap != NULL); 20256 20257 SD_TRACE(SD_LOG_IO, un, 20258 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20259 20260 /* 20261 * First send a READ_CAPACITY_16 command to the target. 20262 * 20263 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 20264 * Medium Indicator bit is cleared. The address field must be 20265 * zero if the PMI bit is zero. 20266 */ 20267 bzero(&cdb, sizeof (cdb)); 20268 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20269 20270 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 20271 20272 ucmd_buf.uscsi_cdb = (char *)&cdb; 20273 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 20274 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 20275 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 20276 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20277 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20278 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20279 ucmd_buf.uscsi_timeout = 60; 20280 20281 /* 20282 * Read Capacity (16) is a Service Action In command. One 20283 * command byte (0x9E) is overloaded for multiple operations, 20284 * with the second CDB byte specifying the desired operation 20285 */ 20286 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 20287 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 20288 20289 /* 20290 * Fill in allocation length field 20291 */ 20292 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 20293 20294 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20295 UIO_SYSSPACE, path_flag); 20296 20297 switch (status) { 20298 case 0: 20299 /* Return failure if we did not get valid capacity data. */ 20300 if (ucmd_buf.uscsi_resid > 20) { 20301 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20302 "sd_send_scsi_READ_CAPACITY_16 received invalid " 20303 "capacity data"); 20304 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20305 return (EIO); 20306 } 20307 20308 /* 20309 * Read capacity and block size from the READ CAPACITY 16 data. 20310 * This data may be adjusted later due to device specific 20311 * issues. 20312 * 20313 * According to the SCSI spec, the READ CAPACITY 16 20314 * command returns the following: 20315 * 20316 * bytes 0-7: Maximum logical block address available. 20317 * (MSB in byte:0 & LSB in byte:7) 20318 * 20319 * bytes 8-11: Block length in bytes 20320 * (MSB in byte:8 & LSB in byte:11) 20321 * 20322 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT 20323 */ 20324 capacity = BE_64(capacity16_buf[0]); 20325 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 20326 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f; 20327 20328 pbsize = lbasize << lbpb_exp; 20329 20330 /* 20331 * Done with capacity16_buf 20332 */ 20333 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20334 20335 /* 20336 * if the reported capacity is set to all 0xf's, then 20337 * this disk is too large. This could only happen with 20338 * a device that supports LBAs larger than 64 bits which 20339 * are not defined by any current T10 standards. 20340 */ 20341 if (capacity == 0xffffffffffffffff) { 20342 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20343 "disk is too large"); 20344 return (EIO); 20345 } 20346 break; /* Success! */ 20347 case EIO: 20348 switch (ucmd_buf.uscsi_status) { 20349 case STATUS_RESERVATION_CONFLICT: 20350 status = EACCES; 20351 break; 20352 case STATUS_CHECK: 20353 /* 20354 * Check condition; look for ASC/ASCQ of 0x04/0x01 20355 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20356 */ 20357 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20358 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20359 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20360 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20361 return (EAGAIN); 20362 } 20363 break; 20364 default: 20365 break; 20366 } 20367 /* FALLTHRU */ 20368 default: 20369 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20370 return (status); 20371 } 20372 20373 /* 20374 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20375 * (2352 and 0 are common) so for these devices always force the value 20376 * to 2048 as required by the ATAPI specs. 20377 */ 20378 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20379 lbasize = 2048; 20380 } 20381 20382 /* 20383 * Get the maximum LBA value from the READ CAPACITY 16 data. 20384 * Here we assume that the Partial Medium Indicator (PMI) bit 20385 * was cleared when issuing the command. This means that the LBA 20386 * returned from the device is the LBA of the last logical block 20387 * on the logical unit. The actual logical block count will be 20388 * this value plus one. 20389 */ 20390 capacity += 1; 20391 20392 /* 20393 * Currently, for removable media, the capacity is saved in terms 20394 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20395 */ 20396 if (un->un_f_has_removable_media) 20397 capacity *= (lbasize / un->un_sys_blocksize); 20398 20399 *capp = capacity; 20400 *lbap = lbasize; 20401 *psp = pbsize; 20402 20403 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 20404 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n", 20405 capacity, lbasize, pbsize); 20406 20407 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) { 20408 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20409 "sd_send_scsi_READ_CAPACITY_16 received invalid value " 20410 "capacity %llu lbasize %d pbsize %d", capacity, lbasize); 20411 return (EIO); 20412 } 20413 20414 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20415 return (0); 20416 } 20417 20418 20419 /* 20420 * Function: sd_send_scsi_START_STOP_UNIT 20421 * 20422 * Description: Issue a scsi START STOP UNIT command to the target. 20423 * 20424 * Arguments: ssc - ssc contatins pointer to driver soft state (unit) 20425 * structure for this target. 20426 * pc_flag - SD_POWER_CONDITION 20427 * SD_START_STOP 20428 * flag - SD_TARGET_START 20429 * SD_TARGET_STOP 20430 * SD_TARGET_EJECT 20431 * SD_TARGET_CLOSE 20432 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20433 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20434 * to use the USCSI "direct" chain and bypass the normal 20435 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20436 * command is issued as part of an error recovery action. 20437 * 20438 * Return Code: 0 - Success 20439 * EIO - IO error 20440 * EACCES - Reservation conflict detected 20441 * ENXIO - Not Ready, medium not present 20442 * errno return code from sd_ssc_send() 20443 * 20444 * Context: Can sleep. 20445 */ 20446 20447 static int 20448 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag, 20449 int path_flag) 20450 { 20451 struct scsi_extended_sense sense_buf; 20452 union scsi_cdb cdb; 20453 struct uscsi_cmd ucmd_buf; 20454 int status; 20455 struct sd_lun *un; 20456 20457 ASSERT(ssc != NULL); 20458 un = ssc->ssc_un; 20459 ASSERT(un != NULL); 20460 ASSERT(!mutex_owned(SD_MUTEX(un))); 20461 20462 SD_TRACE(SD_LOG_IO, un, 20463 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 20464 20465 if (un->un_f_check_start_stop && 20466 (pc_flag == SD_START_STOP) && 20467 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 20468 (un->un_f_start_stop_supported != TRUE)) { 20469 return (0); 20470 } 20471 20472 /* 20473 * If we are performing an eject operation and 20474 * we receive any command other than SD_TARGET_EJECT 20475 * we should immediately return. 20476 */ 20477 if (flag != SD_TARGET_EJECT) { 20478 mutex_enter(SD_MUTEX(un)); 20479 if (un->un_f_ejecting == TRUE) { 20480 mutex_exit(SD_MUTEX(un)); 20481 return (EAGAIN); 20482 } 20483 mutex_exit(SD_MUTEX(un)); 20484 } 20485 20486 bzero(&cdb, sizeof (cdb)); 20487 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20488 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20489 20490 cdb.scc_cmd = SCMD_START_STOP; 20491 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ? 20492 (uchar_t)(flag << 4) : (uchar_t)flag; 20493 20494 ucmd_buf.uscsi_cdb = (char *)&cdb; 20495 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20496 ucmd_buf.uscsi_bufaddr = NULL; 20497 ucmd_buf.uscsi_buflen = 0; 20498 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20499 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20500 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20501 ucmd_buf.uscsi_timeout = 200; 20502 20503 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20504 UIO_SYSSPACE, path_flag); 20505 20506 switch (status) { 20507 case 0: 20508 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20509 break; /* Success! */ 20510 case EIO: 20511 switch (ucmd_buf.uscsi_status) { 20512 case STATUS_RESERVATION_CONFLICT: 20513 status = EACCES; 20514 break; 20515 case STATUS_CHECK: 20516 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 20517 switch (scsi_sense_key( 20518 (uint8_t *)&sense_buf)) { 20519 case KEY_ILLEGAL_REQUEST: 20520 status = ENOTSUP; 20521 break; 20522 case KEY_NOT_READY: 20523 if (scsi_sense_asc( 20524 (uint8_t *)&sense_buf) 20525 == 0x3A) { 20526 status = ENXIO; 20527 } 20528 break; 20529 default: 20530 break; 20531 } 20532 } 20533 break; 20534 default: 20535 break; 20536 } 20537 break; 20538 default: 20539 break; 20540 } 20541 20542 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 20543 20544 return (status); 20545 } 20546 20547 20548 /* 20549 * Function: sd_start_stop_unit_callback 20550 * 20551 * Description: timeout(9F) callback to begin recovery process for a 20552 * device that has spun down. 20553 * 20554 * Arguments: arg - pointer to associated softstate struct. 20555 * 20556 * Context: Executes in a timeout(9F) thread context 20557 */ 20558 20559 static void 20560 sd_start_stop_unit_callback(void *arg) 20561 { 20562 struct sd_lun *un = arg; 20563 ASSERT(un != NULL); 20564 ASSERT(!mutex_owned(SD_MUTEX(un))); 20565 20566 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 20567 20568 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 20569 } 20570 20571 20572 /* 20573 * Function: sd_start_stop_unit_task 20574 * 20575 * Description: Recovery procedure when a drive is spun down. 20576 * 20577 * Arguments: arg - pointer to associated softstate struct. 20578 * 20579 * Context: Executes in a taskq() thread context 20580 */ 20581 20582 static void 20583 sd_start_stop_unit_task(void *arg) 20584 { 20585 struct sd_lun *un = arg; 20586 sd_ssc_t *ssc; 20587 int power_level; 20588 int rval; 20589 20590 ASSERT(un != NULL); 20591 ASSERT(!mutex_owned(SD_MUTEX(un))); 20592 20593 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 20594 20595 /* 20596 * Some unformatted drives report not ready error, no need to 20597 * restart if format has been initiated. 20598 */ 20599 mutex_enter(SD_MUTEX(un)); 20600 if (un->un_f_format_in_progress == TRUE) { 20601 mutex_exit(SD_MUTEX(un)); 20602 return; 20603 } 20604 mutex_exit(SD_MUTEX(un)); 20605 20606 ssc = sd_ssc_init(un); 20607 /* 20608 * When a START STOP command is issued from here, it is part of a 20609 * failure recovery operation and must be issued before any other 20610 * commands, including any pending retries. Thus it must be sent 20611 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20612 * succeeds or not, we will start I/O after the attempt. 20613 * If power condition is supported and the current power level 20614 * is capable of performing I/O, we should set the power condition 20615 * to that level. Otherwise, set the power condition to ACTIVE. 20616 */ 20617 if (un->un_f_power_condition_supported) { 20618 mutex_enter(SD_MUTEX(un)); 20619 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level)); 20620 power_level = sd_pwr_pc.ran_perf[un->un_power_level] 20621 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE; 20622 mutex_exit(SD_MUTEX(un)); 20623 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 20624 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY); 20625 } else { 20626 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 20627 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY); 20628 } 20629 20630 if (rval != 0) 20631 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20632 sd_ssc_fini(ssc); 20633 /* 20634 * The above call blocks until the START_STOP_UNIT command completes. 20635 * Now that it has completed, we must re-try the original IO that 20636 * received the NOT READY condition in the first place. There are 20637 * three possible conditions here: 20638 * 20639 * (1) The original IO is on un_retry_bp. 20640 * (2) The original IO is on the regular wait queue, and un_retry_bp 20641 * is NULL. 20642 * (3) The original IO is on the regular wait queue, and un_retry_bp 20643 * points to some other, unrelated bp. 20644 * 20645 * For each case, we must call sd_start_cmds() with un_retry_bp 20646 * as the argument. If un_retry_bp is NULL, this will initiate 20647 * processing of the regular wait queue. If un_retry_bp is not NULL, 20648 * then this will process the bp on un_retry_bp. That may or may not 20649 * be the original IO, but that does not matter: the important thing 20650 * is to keep the IO processing going at this point. 20651 * 20652 * Note: This is a very specific error recovery sequence associated 20653 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20654 * serialize the I/O with completion of the spin-up. 20655 */ 20656 mutex_enter(SD_MUTEX(un)); 20657 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20658 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20659 un, un->un_retry_bp); 20660 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20661 sd_start_cmds(un, un->un_retry_bp); 20662 mutex_exit(SD_MUTEX(un)); 20663 20664 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20665 } 20666 20667 20668 /* 20669 * Function: sd_send_scsi_INQUIRY 20670 * 20671 * Description: Issue the scsi INQUIRY command. 20672 * 20673 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20674 * structure for this target. 20675 * bufaddr 20676 * buflen 20677 * evpd 20678 * page_code 20679 * page_length 20680 * 20681 * Return Code: 0 - Success 20682 * errno return code from sd_ssc_send() 20683 * 20684 * Context: Can sleep. Does not return until command is completed. 20685 */ 20686 20687 static int 20688 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen, 20689 uchar_t evpd, uchar_t page_code, size_t *residp) 20690 { 20691 union scsi_cdb cdb; 20692 struct uscsi_cmd ucmd_buf; 20693 int status; 20694 struct sd_lun *un; 20695 20696 ASSERT(ssc != NULL); 20697 un = ssc->ssc_un; 20698 ASSERT(un != NULL); 20699 ASSERT(!mutex_owned(SD_MUTEX(un))); 20700 ASSERT(bufaddr != NULL); 20701 20702 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20703 20704 bzero(&cdb, sizeof (cdb)); 20705 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20706 bzero(bufaddr, buflen); 20707 20708 cdb.scc_cmd = SCMD_INQUIRY; 20709 cdb.cdb_opaque[1] = evpd; 20710 cdb.cdb_opaque[2] = page_code; 20711 FORMG0COUNT(&cdb, buflen); 20712 20713 ucmd_buf.uscsi_cdb = (char *)&cdb; 20714 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20715 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20716 ucmd_buf.uscsi_buflen = buflen; 20717 ucmd_buf.uscsi_rqbuf = NULL; 20718 ucmd_buf.uscsi_rqlen = 0; 20719 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20720 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20721 20722 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20723 UIO_SYSSPACE, SD_PATH_DIRECT); 20724 20725 /* 20726 * Only handle status == 0, the upper-level caller 20727 * will put different assessment based on the context. 20728 */ 20729 if (status == 0) 20730 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20731 20732 if ((status == 0) && (residp != NULL)) { 20733 *residp = ucmd_buf.uscsi_resid; 20734 } 20735 20736 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20737 20738 return (status); 20739 } 20740 20741 20742 /* 20743 * Function: sd_send_scsi_TEST_UNIT_READY 20744 * 20745 * Description: Issue the scsi TEST UNIT READY command. 20746 * This routine can be told to set the flag USCSI_DIAGNOSE to 20747 * prevent retrying failed commands. Use this when the intent 20748 * is either to check for device readiness, to clear a Unit 20749 * Attention, or to clear any outstanding sense data. 20750 * However under specific conditions the expected behavior 20751 * is for retries to bring a device ready, so use the flag 20752 * with caution. 20753 * 20754 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20755 * structure for this target. 20756 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20757 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20758 * 0: dont check for media present, do retries on cmd. 20759 * 20760 * Return Code: 0 - Success 20761 * EIO - IO error 20762 * EACCES - Reservation conflict detected 20763 * ENXIO - Not Ready, medium not present 20764 * errno return code from sd_ssc_send() 20765 * 20766 * Context: Can sleep. Does not return until command is completed. 20767 */ 20768 20769 static int 20770 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag) 20771 { 20772 struct scsi_extended_sense sense_buf; 20773 union scsi_cdb cdb; 20774 struct uscsi_cmd ucmd_buf; 20775 int status; 20776 struct sd_lun *un; 20777 20778 ASSERT(ssc != NULL); 20779 un = ssc->ssc_un; 20780 ASSERT(un != NULL); 20781 ASSERT(!mutex_owned(SD_MUTEX(un))); 20782 20783 SD_TRACE(SD_LOG_IO, un, 20784 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20785 20786 /* 20787 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20788 * timeouts when they receive a TUR and the queue is not empty. Check 20789 * the configuration flag set during attach (indicating the drive has 20790 * this firmware bug) and un_ncmds_in_transport before issuing the 20791 * TUR. If there are 20792 * pending commands return success, this is a bit arbitrary but is ok 20793 * for non-removables (i.e. the eliteI disks) and non-clustering 20794 * configurations. 20795 */ 20796 if (un->un_f_cfg_tur_check == TRUE) { 20797 mutex_enter(SD_MUTEX(un)); 20798 if (un->un_ncmds_in_transport != 0) { 20799 mutex_exit(SD_MUTEX(un)); 20800 return (0); 20801 } 20802 mutex_exit(SD_MUTEX(un)); 20803 } 20804 20805 bzero(&cdb, sizeof (cdb)); 20806 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20807 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20808 20809 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20810 20811 ucmd_buf.uscsi_cdb = (char *)&cdb; 20812 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20813 ucmd_buf.uscsi_bufaddr = NULL; 20814 ucmd_buf.uscsi_buflen = 0; 20815 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20816 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20817 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20818 20819 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20820 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20821 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20822 } 20823 ucmd_buf.uscsi_timeout = 60; 20824 20825 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20826 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 20827 SD_PATH_STANDARD)); 20828 20829 switch (status) { 20830 case 0: 20831 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20832 break; /* Success! */ 20833 case EIO: 20834 switch (ucmd_buf.uscsi_status) { 20835 case STATUS_RESERVATION_CONFLICT: 20836 status = EACCES; 20837 break; 20838 case STATUS_CHECK: 20839 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20840 break; 20841 } 20842 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20843 (scsi_sense_key((uint8_t *)&sense_buf) == 20844 KEY_NOT_READY) && 20845 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20846 status = ENXIO; 20847 } 20848 break; 20849 default: 20850 break; 20851 } 20852 break; 20853 default: 20854 break; 20855 } 20856 20857 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20858 20859 return (status); 20860 } 20861 20862 /* 20863 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20864 * 20865 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20866 * 20867 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20868 * structure for this target. 20869 * 20870 * Return Code: 0 - Success 20871 * EACCES 20872 * ENOTSUP 20873 * errno return code from sd_ssc_send() 20874 * 20875 * Context: Can sleep. Does not return until command is completed. 20876 */ 20877 20878 static int 20879 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd, 20880 uint16_t data_len, uchar_t *data_bufp) 20881 { 20882 struct scsi_extended_sense sense_buf; 20883 union scsi_cdb cdb; 20884 struct uscsi_cmd ucmd_buf; 20885 int status; 20886 int no_caller_buf = FALSE; 20887 struct sd_lun *un; 20888 20889 ASSERT(ssc != NULL); 20890 un = ssc->ssc_un; 20891 ASSERT(un != NULL); 20892 ASSERT(!mutex_owned(SD_MUTEX(un))); 20893 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20894 20895 SD_TRACE(SD_LOG_IO, un, 20896 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20897 20898 bzero(&cdb, sizeof (cdb)); 20899 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20900 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20901 if (data_bufp == NULL) { 20902 /* Allocate a default buf if the caller did not give one */ 20903 ASSERT(data_len == 0); 20904 data_len = MHIOC_RESV_KEY_SIZE; 20905 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20906 no_caller_buf = TRUE; 20907 } 20908 20909 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20910 cdb.cdb_opaque[1] = usr_cmd; 20911 FORMG1COUNT(&cdb, data_len); 20912 20913 ucmd_buf.uscsi_cdb = (char *)&cdb; 20914 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20915 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20916 ucmd_buf.uscsi_buflen = data_len; 20917 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20918 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20919 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20920 ucmd_buf.uscsi_timeout = 60; 20921 20922 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20923 UIO_SYSSPACE, SD_PATH_STANDARD); 20924 20925 switch (status) { 20926 case 0: 20927 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20928 20929 break; /* Success! */ 20930 case EIO: 20931 switch (ucmd_buf.uscsi_status) { 20932 case STATUS_RESERVATION_CONFLICT: 20933 status = EACCES; 20934 break; 20935 case STATUS_CHECK: 20936 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20937 (scsi_sense_key((uint8_t *)&sense_buf) == 20938 KEY_ILLEGAL_REQUEST)) { 20939 status = ENOTSUP; 20940 } 20941 break; 20942 default: 20943 break; 20944 } 20945 break; 20946 default: 20947 break; 20948 } 20949 20950 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20951 20952 if (no_caller_buf == TRUE) { 20953 kmem_free(data_bufp, data_len); 20954 } 20955 20956 return (status); 20957 } 20958 20959 20960 /* 20961 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20962 * 20963 * Description: This routine is the driver entry point for handling CD-ROM 20964 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20965 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20966 * device. 20967 * 20968 * Arguments: ssc - ssc contains un - pointer to soft state struct 20969 * for the target. 20970 * usr_cmd SCSI-3 reservation facility command (one of 20971 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20972 * SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR) 20973 * usr_bufp - user provided pointer register, reserve descriptor or 20974 * preempt and abort structure (mhioc_register_t, 20975 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20976 * 20977 * Return Code: 0 - Success 20978 * EACCES 20979 * ENOTSUP 20980 * errno return code from sd_ssc_send() 20981 * 20982 * Context: Can sleep. Does not return until command is completed. 20983 */ 20984 20985 static int 20986 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd, 20987 uchar_t *usr_bufp) 20988 { 20989 struct scsi_extended_sense sense_buf; 20990 union scsi_cdb cdb; 20991 struct uscsi_cmd ucmd_buf; 20992 int status; 20993 uchar_t data_len = sizeof (sd_prout_t); 20994 sd_prout_t *prp; 20995 struct sd_lun *un; 20996 20997 ASSERT(ssc != NULL); 20998 un = ssc->ssc_un; 20999 ASSERT(un != NULL); 21000 ASSERT(!mutex_owned(SD_MUTEX(un))); 21001 ASSERT(data_len == 24); /* required by scsi spec */ 21002 21003 SD_TRACE(SD_LOG_IO, un, 21004 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 21005 21006 if (usr_bufp == NULL) { 21007 return (EINVAL); 21008 } 21009 21010 bzero(&cdb, sizeof (cdb)); 21011 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21012 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21013 prp = kmem_zalloc(data_len, KM_SLEEP); 21014 21015 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 21016 cdb.cdb_opaque[1] = usr_cmd; 21017 FORMG1COUNT(&cdb, data_len); 21018 21019 ucmd_buf.uscsi_cdb = (char *)&cdb; 21020 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21021 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 21022 ucmd_buf.uscsi_buflen = data_len; 21023 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21024 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21025 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21026 ucmd_buf.uscsi_timeout = 60; 21027 21028 switch (usr_cmd) { 21029 case SD_SCSI3_REGISTER: { 21030 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 21031 21032 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21033 bcopy(ptr->newkey.key, prp->service_key, 21034 MHIOC_RESV_KEY_SIZE); 21035 prp->aptpl = ptr->aptpl; 21036 break; 21037 } 21038 case SD_SCSI3_CLEAR: { 21039 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 21040 21041 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21042 break; 21043 } 21044 case SD_SCSI3_RESERVE: 21045 case SD_SCSI3_RELEASE: { 21046 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 21047 21048 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21049 prp->scope_address = BE_32(ptr->scope_specific_addr); 21050 cdb.cdb_opaque[2] = ptr->type; 21051 break; 21052 } 21053 case SD_SCSI3_PREEMPTANDABORT: { 21054 mhioc_preemptandabort_t *ptr = 21055 (mhioc_preemptandabort_t *)usr_bufp; 21056 21057 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21058 bcopy(ptr->victim_key.key, prp->service_key, 21059 MHIOC_RESV_KEY_SIZE); 21060 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 21061 cdb.cdb_opaque[2] = ptr->resvdesc.type; 21062 ucmd_buf.uscsi_flags |= USCSI_HEAD; 21063 break; 21064 } 21065 case SD_SCSI3_REGISTERANDIGNOREKEY: 21066 { 21067 mhioc_registerandignorekey_t *ptr; 21068 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 21069 bcopy(ptr->newkey.key, 21070 prp->service_key, MHIOC_RESV_KEY_SIZE); 21071 prp->aptpl = ptr->aptpl; 21072 break; 21073 } 21074 default: 21075 ASSERT(FALSE); 21076 break; 21077 } 21078 21079 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21080 UIO_SYSSPACE, SD_PATH_STANDARD); 21081 21082 switch (status) { 21083 case 0: 21084 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21085 break; /* Success! */ 21086 case EIO: 21087 switch (ucmd_buf.uscsi_status) { 21088 case STATUS_RESERVATION_CONFLICT: 21089 status = EACCES; 21090 break; 21091 case STATUS_CHECK: 21092 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21093 (scsi_sense_key((uint8_t *)&sense_buf) == 21094 KEY_ILLEGAL_REQUEST)) { 21095 status = ENOTSUP; 21096 } 21097 break; 21098 default: 21099 break; 21100 } 21101 break; 21102 default: 21103 break; 21104 } 21105 21106 kmem_free(prp, data_len); 21107 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 21108 return (status); 21109 } 21110 21111 21112 /* 21113 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 21114 * 21115 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 21116 * 21117 * Arguments: un - pointer to the target's soft state struct 21118 * dkc - pointer to the callback structure 21119 * 21120 * Return Code: 0 - success 21121 * errno-type error code 21122 * 21123 * Context: kernel thread context only. 21124 * 21125 * _______________________________________________________________ 21126 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 21127 * |FLUSH_VOLATILE| | operation | 21128 * |______________|______________|_________________________________| 21129 * | 0 | NULL | Synchronous flush on both | 21130 * | | | volatile and non-volatile cache | 21131 * |______________|______________|_________________________________| 21132 * | 1 | NULL | Synchronous flush on volatile | 21133 * | | | cache; disk drivers may suppress| 21134 * | | | flush if disk table indicates | 21135 * | | | non-volatile cache | 21136 * |______________|______________|_________________________________| 21137 * | 0 | !NULL | Asynchronous flush on both | 21138 * | | | volatile and non-volatile cache;| 21139 * |______________|______________|_________________________________| 21140 * | 1 | !NULL | Asynchronous flush on volatile | 21141 * | | | cache; disk drivers may suppress| 21142 * | | | flush if disk table indicates | 21143 * | | | non-volatile cache | 21144 * |______________|______________|_________________________________| 21145 * 21146 */ 21147 21148 static int 21149 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 21150 { 21151 struct sd_uscsi_info *uip; 21152 struct uscsi_cmd *uscmd; 21153 union scsi_cdb *cdb; 21154 struct buf *bp; 21155 int rval = 0; 21156 int is_async; 21157 21158 SD_TRACE(SD_LOG_IO, un, 21159 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 21160 21161 ASSERT(un != NULL); 21162 ASSERT(!mutex_owned(SD_MUTEX(un))); 21163 21164 if (dkc == NULL || dkc->dkc_callback == NULL) { 21165 is_async = FALSE; 21166 } else { 21167 is_async = TRUE; 21168 } 21169 21170 mutex_enter(SD_MUTEX(un)); 21171 /* check whether cache flush should be suppressed */ 21172 if (un->un_f_suppress_cache_flush == TRUE) { 21173 mutex_exit(SD_MUTEX(un)); 21174 /* 21175 * suppress the cache flush if the device is told to do 21176 * so by sd.conf or disk table 21177 */ 21178 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 21179 skip the cache flush since suppress_cache_flush is %d!\n", 21180 un->un_f_suppress_cache_flush); 21181 21182 if (is_async == TRUE) { 21183 /* invoke callback for asynchronous flush */ 21184 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 21185 } 21186 return (rval); 21187 } 21188 mutex_exit(SD_MUTEX(un)); 21189 21190 /* 21191 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 21192 * set properly 21193 */ 21194 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 21195 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 21196 21197 mutex_enter(SD_MUTEX(un)); 21198 if (dkc != NULL && un->un_f_sync_nv_supported && 21199 (dkc->dkc_flag & FLUSH_VOLATILE)) { 21200 /* 21201 * if the device supports SYNC_NV bit, turn on 21202 * the SYNC_NV bit to only flush volatile cache 21203 */ 21204 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 21205 } 21206 mutex_exit(SD_MUTEX(un)); 21207 21208 /* 21209 * First get some memory for the uscsi_cmd struct and cdb 21210 * and initialize for SYNCHRONIZE_CACHE cmd. 21211 */ 21212 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21213 uscmd->uscsi_cdblen = CDB_GROUP1; 21214 uscmd->uscsi_cdb = (caddr_t)cdb; 21215 uscmd->uscsi_bufaddr = NULL; 21216 uscmd->uscsi_buflen = 0; 21217 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 21218 uscmd->uscsi_rqlen = SENSE_LENGTH; 21219 uscmd->uscsi_rqresid = SENSE_LENGTH; 21220 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 21221 uscmd->uscsi_timeout = sd_io_time; 21222 21223 /* 21224 * Allocate an sd_uscsi_info struct and fill it with the info 21225 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 21226 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 21227 * since we allocate the buf here in this function, we do not 21228 * need to preserve the prior contents of b_private. 21229 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 21230 */ 21231 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 21232 uip->ui_flags = SD_PATH_DIRECT; 21233 uip->ui_cmdp = uscmd; 21234 21235 bp = getrbuf(KM_SLEEP); 21236 bp->b_private = uip; 21237 21238 /* 21239 * Setup buffer to carry uscsi request. 21240 */ 21241 bp->b_flags = B_BUSY; 21242 bp->b_bcount = 0; 21243 bp->b_blkno = 0; 21244 21245 if (is_async == TRUE) { 21246 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 21247 uip->ui_dkc = *dkc; 21248 } 21249 21250 bp->b_edev = SD_GET_DEV(un); 21251 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 21252 21253 /* 21254 * Unset un_f_sync_cache_required flag 21255 */ 21256 mutex_enter(SD_MUTEX(un)); 21257 un->un_f_sync_cache_required = FALSE; 21258 mutex_exit(SD_MUTEX(un)); 21259 21260 (void) sd_uscsi_strategy(bp); 21261 21262 /* 21263 * If synchronous request, wait for completion 21264 * If async just return and let b_iodone callback 21265 * cleanup. 21266 * NOTE: On return, u_ncmds_in_driver will be decremented, 21267 * but it was also incremented in sd_uscsi_strategy(), so 21268 * we should be ok. 21269 */ 21270 if (is_async == FALSE) { 21271 (void) biowait(bp); 21272 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 21273 } 21274 21275 return (rval); 21276 } 21277 21278 21279 static int 21280 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 21281 { 21282 struct sd_uscsi_info *uip; 21283 struct uscsi_cmd *uscmd; 21284 uint8_t *sense_buf; 21285 struct sd_lun *un; 21286 int status; 21287 union scsi_cdb *cdb; 21288 21289 uip = (struct sd_uscsi_info *)(bp->b_private); 21290 ASSERT(uip != NULL); 21291 21292 uscmd = uip->ui_cmdp; 21293 ASSERT(uscmd != NULL); 21294 21295 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 21296 ASSERT(sense_buf != NULL); 21297 21298 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 21299 ASSERT(un != NULL); 21300 21301 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 21302 21303 status = geterror(bp); 21304 switch (status) { 21305 case 0: 21306 break; /* Success! */ 21307 case EIO: 21308 switch (uscmd->uscsi_status) { 21309 case STATUS_RESERVATION_CONFLICT: 21310 /* Ignore reservation conflict */ 21311 status = 0; 21312 goto done; 21313 21314 case STATUS_CHECK: 21315 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 21316 (scsi_sense_key(sense_buf) == 21317 KEY_ILLEGAL_REQUEST)) { 21318 /* Ignore Illegal Request error */ 21319 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 21320 mutex_enter(SD_MUTEX(un)); 21321 un->un_f_sync_nv_supported = FALSE; 21322 mutex_exit(SD_MUTEX(un)); 21323 status = 0; 21324 SD_TRACE(SD_LOG_IO, un, 21325 "un_f_sync_nv_supported \ 21326 is set to false.\n"); 21327 goto done; 21328 } 21329 21330 mutex_enter(SD_MUTEX(un)); 21331 un->un_f_sync_cache_supported = FALSE; 21332 mutex_exit(SD_MUTEX(un)); 21333 SD_TRACE(SD_LOG_IO, un, 21334 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 21335 un_f_sync_cache_supported set to false \ 21336 with asc = %x, ascq = %x\n", 21337 scsi_sense_asc(sense_buf), 21338 scsi_sense_ascq(sense_buf)); 21339 status = ENOTSUP; 21340 goto done; 21341 } 21342 break; 21343 default: 21344 break; 21345 } 21346 /* FALLTHRU */ 21347 default: 21348 /* 21349 * Turn on the un_f_sync_cache_required flag 21350 * since the SYNC CACHE command failed 21351 */ 21352 mutex_enter(SD_MUTEX(un)); 21353 un->un_f_sync_cache_required = TRUE; 21354 mutex_exit(SD_MUTEX(un)); 21355 21356 /* 21357 * Don't log an error message if this device 21358 * has removable media. 21359 */ 21360 if (!un->un_f_has_removable_media) { 21361 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 21362 "SYNCHRONIZE CACHE command failed (%d)\n", status); 21363 } 21364 break; 21365 } 21366 21367 done: 21368 if (uip->ui_dkc.dkc_callback != NULL) { 21369 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 21370 } 21371 21372 ASSERT((bp->b_flags & B_REMAPPED) == 0); 21373 freerbuf(bp); 21374 kmem_free(uip, sizeof (struct sd_uscsi_info)); 21375 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 21376 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 21377 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 21378 21379 return (status); 21380 } 21381 21382 21383 /* 21384 * Function: sd_send_scsi_GET_CONFIGURATION 21385 * 21386 * Description: Issues the get configuration command to the device. 21387 * Called from sd_check_for_writable_cd & sd_get_media_info 21388 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 21389 * Arguments: ssc 21390 * ucmdbuf 21391 * rqbuf 21392 * rqbuflen 21393 * bufaddr 21394 * buflen 21395 * path_flag 21396 * 21397 * Return Code: 0 - Success 21398 * errno return code from sd_ssc_send() 21399 * 21400 * Context: Can sleep. Does not return until command is completed. 21401 * 21402 */ 21403 21404 static int 21405 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf, 21406 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 21407 int path_flag) 21408 { 21409 char cdb[CDB_GROUP1]; 21410 int status; 21411 struct sd_lun *un; 21412 21413 ASSERT(ssc != NULL); 21414 un = ssc->ssc_un; 21415 ASSERT(un != NULL); 21416 ASSERT(!mutex_owned(SD_MUTEX(un))); 21417 ASSERT(bufaddr != NULL); 21418 ASSERT(ucmdbuf != NULL); 21419 ASSERT(rqbuf != NULL); 21420 21421 SD_TRACE(SD_LOG_IO, un, 21422 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 21423 21424 bzero(cdb, sizeof (cdb)); 21425 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21426 bzero(rqbuf, rqbuflen); 21427 bzero(bufaddr, buflen); 21428 21429 /* 21430 * Set up cdb field for the get configuration command. 21431 */ 21432 cdb[0] = SCMD_GET_CONFIGURATION; 21433 cdb[1] = 0x02; /* Requested Type */ 21434 cdb[8] = SD_PROFILE_HEADER_LEN; 21435 ucmdbuf->uscsi_cdb = cdb; 21436 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21437 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21438 ucmdbuf->uscsi_buflen = buflen; 21439 ucmdbuf->uscsi_timeout = sd_io_time; 21440 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21441 ucmdbuf->uscsi_rqlen = rqbuflen; 21442 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21443 21444 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21445 UIO_SYSSPACE, path_flag); 21446 21447 switch (status) { 21448 case 0: 21449 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21450 break; /* Success! */ 21451 case EIO: 21452 switch (ucmdbuf->uscsi_status) { 21453 case STATUS_RESERVATION_CONFLICT: 21454 status = EACCES; 21455 break; 21456 default: 21457 break; 21458 } 21459 break; 21460 default: 21461 break; 21462 } 21463 21464 if (status == 0) { 21465 SD_DUMP_MEMORY(un, SD_LOG_IO, 21466 "sd_send_scsi_GET_CONFIGURATION: data", 21467 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21468 } 21469 21470 SD_TRACE(SD_LOG_IO, un, 21471 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 21472 21473 return (status); 21474 } 21475 21476 /* 21477 * Function: sd_send_scsi_feature_GET_CONFIGURATION 21478 * 21479 * Description: Issues the get configuration command to the device to 21480 * retrieve a specific feature. Called from 21481 * sd_check_for_writable_cd & sd_set_mmc_caps. 21482 * Arguments: ssc 21483 * ucmdbuf 21484 * rqbuf 21485 * rqbuflen 21486 * bufaddr 21487 * buflen 21488 * feature 21489 * 21490 * Return Code: 0 - Success 21491 * errno return code from sd_ssc_send() 21492 * 21493 * Context: Can sleep. Does not return until command is completed. 21494 * 21495 */ 21496 static int 21497 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 21498 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 21499 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 21500 { 21501 char cdb[CDB_GROUP1]; 21502 int status; 21503 struct sd_lun *un; 21504 21505 ASSERT(ssc != NULL); 21506 un = ssc->ssc_un; 21507 ASSERT(un != NULL); 21508 ASSERT(!mutex_owned(SD_MUTEX(un))); 21509 ASSERT(bufaddr != NULL); 21510 ASSERT(ucmdbuf != NULL); 21511 ASSERT(rqbuf != NULL); 21512 21513 SD_TRACE(SD_LOG_IO, un, 21514 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 21515 21516 bzero(cdb, sizeof (cdb)); 21517 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21518 bzero(rqbuf, rqbuflen); 21519 bzero(bufaddr, buflen); 21520 21521 /* 21522 * Set up cdb field for the get configuration command. 21523 */ 21524 cdb[0] = SCMD_GET_CONFIGURATION; 21525 cdb[1] = 0x02; /* Requested Type */ 21526 cdb[3] = feature; 21527 cdb[8] = buflen; 21528 ucmdbuf->uscsi_cdb = cdb; 21529 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21530 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21531 ucmdbuf->uscsi_buflen = buflen; 21532 ucmdbuf->uscsi_timeout = sd_io_time; 21533 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21534 ucmdbuf->uscsi_rqlen = rqbuflen; 21535 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21536 21537 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21538 UIO_SYSSPACE, path_flag); 21539 21540 switch (status) { 21541 case 0: 21542 21543 break; /* Success! */ 21544 case EIO: 21545 switch (ucmdbuf->uscsi_status) { 21546 case STATUS_RESERVATION_CONFLICT: 21547 status = EACCES; 21548 break; 21549 default: 21550 break; 21551 } 21552 break; 21553 default: 21554 break; 21555 } 21556 21557 if (status == 0) { 21558 SD_DUMP_MEMORY(un, SD_LOG_IO, 21559 "sd_send_scsi_feature_GET_CONFIGURATION: data", 21560 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21561 } 21562 21563 SD_TRACE(SD_LOG_IO, un, 21564 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 21565 21566 return (status); 21567 } 21568 21569 21570 /* 21571 * Function: sd_send_scsi_MODE_SENSE 21572 * 21573 * Description: Utility function for issuing a scsi MODE SENSE command. 21574 * Note: This routine uses a consistent implementation for Group0, 21575 * Group1, and Group2 commands across all platforms. ATAPI devices 21576 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21577 * 21578 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21579 * structure for this target. 21580 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21581 * CDB_GROUP[1|2] (10 byte). 21582 * bufaddr - buffer for page data retrieved from the target. 21583 * buflen - size of page to be retrieved. 21584 * page_code - page code of data to be retrieved from the target. 21585 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21586 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21587 * to use the USCSI "direct" chain and bypass the normal 21588 * command waitq. 21589 * 21590 * Return Code: 0 - Success 21591 * errno return code from sd_ssc_send() 21592 * 21593 * Context: Can sleep. Does not return until command is completed. 21594 */ 21595 21596 static int 21597 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21598 size_t buflen, uchar_t page_code, int path_flag) 21599 { 21600 struct scsi_extended_sense sense_buf; 21601 union scsi_cdb cdb; 21602 struct uscsi_cmd ucmd_buf; 21603 int status; 21604 int headlen; 21605 struct sd_lun *un; 21606 21607 ASSERT(ssc != NULL); 21608 un = ssc->ssc_un; 21609 ASSERT(un != NULL); 21610 ASSERT(!mutex_owned(SD_MUTEX(un))); 21611 ASSERT(bufaddr != NULL); 21612 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21613 (cdbsize == CDB_GROUP2)); 21614 21615 SD_TRACE(SD_LOG_IO, un, 21616 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 21617 21618 bzero(&cdb, sizeof (cdb)); 21619 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21620 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21621 bzero(bufaddr, buflen); 21622 21623 if (cdbsize == CDB_GROUP0) { 21624 cdb.scc_cmd = SCMD_MODE_SENSE; 21625 cdb.cdb_opaque[2] = page_code; 21626 FORMG0COUNT(&cdb, buflen); 21627 headlen = MODE_HEADER_LENGTH; 21628 } else { 21629 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 21630 cdb.cdb_opaque[2] = page_code; 21631 FORMG1COUNT(&cdb, buflen); 21632 headlen = MODE_HEADER_LENGTH_GRP2; 21633 } 21634 21635 ASSERT(headlen <= buflen); 21636 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21637 21638 ucmd_buf.uscsi_cdb = (char *)&cdb; 21639 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21640 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21641 ucmd_buf.uscsi_buflen = buflen; 21642 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21643 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21644 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21645 ucmd_buf.uscsi_timeout = 60; 21646 21647 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21648 UIO_SYSSPACE, path_flag); 21649 21650 switch (status) { 21651 case 0: 21652 /* 21653 * sr_check_wp() uses 0x3f page code and check the header of 21654 * mode page to determine if target device is write-protected. 21655 * But some USB devices return 0 bytes for 0x3f page code. For 21656 * this case, make sure that mode page header is returned at 21657 * least. 21658 */ 21659 if (buflen - ucmd_buf.uscsi_resid < headlen) { 21660 status = EIO; 21661 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 21662 "mode page header is not returned"); 21663 } 21664 break; /* Success! */ 21665 case EIO: 21666 switch (ucmd_buf.uscsi_status) { 21667 case STATUS_RESERVATION_CONFLICT: 21668 status = EACCES; 21669 break; 21670 default: 21671 break; 21672 } 21673 break; 21674 default: 21675 break; 21676 } 21677 21678 if (status == 0) { 21679 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 21680 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21681 } 21682 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 21683 21684 return (status); 21685 } 21686 21687 21688 /* 21689 * Function: sd_send_scsi_MODE_SELECT 21690 * 21691 * Description: Utility function for issuing a scsi MODE SELECT command. 21692 * Note: This routine uses a consistent implementation for Group0, 21693 * Group1, and Group2 commands across all platforms. ATAPI devices 21694 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21695 * 21696 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21697 * structure for this target. 21698 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21699 * CDB_GROUP[1|2] (10 byte). 21700 * bufaddr - buffer for page data retrieved from the target. 21701 * buflen - size of page to be retrieved. 21702 * save_page - boolean to determin if SP bit should be set. 21703 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21704 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21705 * to use the USCSI "direct" chain and bypass the normal 21706 * command waitq. 21707 * 21708 * Return Code: 0 - Success 21709 * errno return code from sd_ssc_send() 21710 * 21711 * Context: Can sleep. Does not return until command is completed. 21712 */ 21713 21714 static int 21715 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21716 size_t buflen, uchar_t save_page, int path_flag) 21717 { 21718 struct scsi_extended_sense sense_buf; 21719 union scsi_cdb cdb; 21720 struct uscsi_cmd ucmd_buf; 21721 int status; 21722 struct sd_lun *un; 21723 21724 ASSERT(ssc != NULL); 21725 un = ssc->ssc_un; 21726 ASSERT(un != NULL); 21727 ASSERT(!mutex_owned(SD_MUTEX(un))); 21728 ASSERT(bufaddr != NULL); 21729 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21730 (cdbsize == CDB_GROUP2)); 21731 21732 SD_TRACE(SD_LOG_IO, un, 21733 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 21734 21735 bzero(&cdb, sizeof (cdb)); 21736 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21737 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21738 21739 /* Set the PF bit for many third party drives */ 21740 cdb.cdb_opaque[1] = 0x10; 21741 21742 /* Set the savepage(SP) bit if given */ 21743 if (save_page == SD_SAVE_PAGE) { 21744 cdb.cdb_opaque[1] |= 0x01; 21745 } 21746 21747 if (cdbsize == CDB_GROUP0) { 21748 cdb.scc_cmd = SCMD_MODE_SELECT; 21749 FORMG0COUNT(&cdb, buflen); 21750 } else { 21751 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 21752 FORMG1COUNT(&cdb, buflen); 21753 } 21754 21755 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21756 21757 ucmd_buf.uscsi_cdb = (char *)&cdb; 21758 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21759 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21760 ucmd_buf.uscsi_buflen = buflen; 21761 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21762 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21763 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21764 ucmd_buf.uscsi_timeout = 60; 21765 21766 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21767 UIO_SYSSPACE, path_flag); 21768 21769 switch (status) { 21770 case 0: 21771 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21772 break; /* Success! */ 21773 case EIO: 21774 switch (ucmd_buf.uscsi_status) { 21775 case STATUS_RESERVATION_CONFLICT: 21776 status = EACCES; 21777 break; 21778 default: 21779 break; 21780 } 21781 break; 21782 default: 21783 break; 21784 } 21785 21786 if (status == 0) { 21787 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21788 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21789 } 21790 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21791 21792 return (status); 21793 } 21794 21795 21796 /* 21797 * Function: sd_send_scsi_RDWR 21798 * 21799 * Description: Issue a scsi READ or WRITE command with the given parameters. 21800 * 21801 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21802 * structure for this target. 21803 * cmd: SCMD_READ or SCMD_WRITE 21804 * bufaddr: Address of caller's buffer to receive the RDWR data 21805 * buflen: Length of caller's buffer receive the RDWR data. 21806 * start_block: Block number for the start of the RDWR operation. 21807 * (Assumes target-native block size.) 21808 * residp: Pointer to variable to receive the redisual of the 21809 * RDWR operation (may be NULL of no residual requested). 21810 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21811 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21812 * to use the USCSI "direct" chain and bypass the normal 21813 * command waitq. 21814 * 21815 * Return Code: 0 - Success 21816 * errno return code from sd_ssc_send() 21817 * 21818 * Context: Can sleep. Does not return until command is completed. 21819 */ 21820 21821 static int 21822 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 21823 size_t buflen, daddr_t start_block, int path_flag) 21824 { 21825 struct scsi_extended_sense sense_buf; 21826 union scsi_cdb cdb; 21827 struct uscsi_cmd ucmd_buf; 21828 uint32_t block_count; 21829 int status; 21830 int cdbsize; 21831 uchar_t flag; 21832 struct sd_lun *un; 21833 21834 ASSERT(ssc != NULL); 21835 un = ssc->ssc_un; 21836 ASSERT(un != NULL); 21837 ASSERT(!mutex_owned(SD_MUTEX(un))); 21838 ASSERT(bufaddr != NULL); 21839 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21840 21841 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21842 21843 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21844 return (EINVAL); 21845 } 21846 21847 mutex_enter(SD_MUTEX(un)); 21848 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21849 mutex_exit(SD_MUTEX(un)); 21850 21851 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21852 21853 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21854 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21855 bufaddr, buflen, start_block, block_count); 21856 21857 bzero(&cdb, sizeof (cdb)); 21858 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21859 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21860 21861 /* Compute CDB size to use */ 21862 if (start_block > 0xffffffff) 21863 cdbsize = CDB_GROUP4; 21864 else if ((start_block & 0xFFE00000) || 21865 (un->un_f_cfg_is_atapi == TRUE)) 21866 cdbsize = CDB_GROUP1; 21867 else 21868 cdbsize = CDB_GROUP0; 21869 21870 switch (cdbsize) { 21871 case CDB_GROUP0: /* 6-byte CDBs */ 21872 cdb.scc_cmd = cmd; 21873 FORMG0ADDR(&cdb, start_block); 21874 FORMG0COUNT(&cdb, block_count); 21875 break; 21876 case CDB_GROUP1: /* 10-byte CDBs */ 21877 cdb.scc_cmd = cmd | SCMD_GROUP1; 21878 FORMG1ADDR(&cdb, start_block); 21879 FORMG1COUNT(&cdb, block_count); 21880 break; 21881 case CDB_GROUP4: /* 16-byte CDBs */ 21882 cdb.scc_cmd = cmd | SCMD_GROUP4; 21883 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21884 FORMG4COUNT(&cdb, block_count); 21885 break; 21886 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21887 default: 21888 /* All others reserved */ 21889 return (EINVAL); 21890 } 21891 21892 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21893 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21894 21895 ucmd_buf.uscsi_cdb = (char *)&cdb; 21896 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21897 ucmd_buf.uscsi_bufaddr = bufaddr; 21898 ucmd_buf.uscsi_buflen = buflen; 21899 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21900 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21901 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21902 ucmd_buf.uscsi_timeout = 60; 21903 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21904 UIO_SYSSPACE, path_flag); 21905 21906 switch (status) { 21907 case 0: 21908 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21909 break; /* Success! */ 21910 case EIO: 21911 switch (ucmd_buf.uscsi_status) { 21912 case STATUS_RESERVATION_CONFLICT: 21913 status = EACCES; 21914 break; 21915 default: 21916 break; 21917 } 21918 break; 21919 default: 21920 break; 21921 } 21922 21923 if (status == 0) { 21924 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21925 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21926 } 21927 21928 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21929 21930 return (status); 21931 } 21932 21933 21934 /* 21935 * Function: sd_send_scsi_LOG_SENSE 21936 * 21937 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21938 * 21939 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21940 * structure for this target. 21941 * 21942 * Return Code: 0 - Success 21943 * errno return code from sd_ssc_send() 21944 * 21945 * Context: Can sleep. Does not return until command is completed. 21946 */ 21947 21948 static int 21949 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen, 21950 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21951 int path_flag) 21952 21953 { 21954 struct scsi_extended_sense sense_buf; 21955 union scsi_cdb cdb; 21956 struct uscsi_cmd ucmd_buf; 21957 int status; 21958 struct sd_lun *un; 21959 21960 ASSERT(ssc != NULL); 21961 un = ssc->ssc_un; 21962 ASSERT(un != NULL); 21963 ASSERT(!mutex_owned(SD_MUTEX(un))); 21964 21965 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21966 21967 bzero(&cdb, sizeof (cdb)); 21968 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21969 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21970 21971 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21972 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21973 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21974 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21975 FORMG1COUNT(&cdb, buflen); 21976 21977 ucmd_buf.uscsi_cdb = (char *)&cdb; 21978 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21979 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21980 ucmd_buf.uscsi_buflen = buflen; 21981 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21982 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21983 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21984 ucmd_buf.uscsi_timeout = 60; 21985 21986 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21987 UIO_SYSSPACE, path_flag); 21988 21989 switch (status) { 21990 case 0: 21991 break; 21992 case EIO: 21993 switch (ucmd_buf.uscsi_status) { 21994 case STATUS_RESERVATION_CONFLICT: 21995 status = EACCES; 21996 break; 21997 case STATUS_CHECK: 21998 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21999 (scsi_sense_key((uint8_t *)&sense_buf) == 22000 KEY_ILLEGAL_REQUEST) && 22001 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 22002 /* 22003 * ASC 0x24: INVALID FIELD IN CDB 22004 */ 22005 switch (page_code) { 22006 case START_STOP_CYCLE_PAGE: 22007 /* 22008 * The start stop cycle counter is 22009 * implemented as page 0x31 in earlier 22010 * generation disks. In new generation 22011 * disks the start stop cycle counter is 22012 * implemented as page 0xE. To properly 22013 * handle this case if an attempt for 22014 * log page 0xE is made and fails we 22015 * will try again using page 0x31. 22016 * 22017 * Network storage BU committed to 22018 * maintain the page 0x31 for this 22019 * purpose and will not have any other 22020 * page implemented with page code 0x31 22021 * until all disks transition to the 22022 * standard page. 22023 */ 22024 mutex_enter(SD_MUTEX(un)); 22025 un->un_start_stop_cycle_page = 22026 START_STOP_CYCLE_VU_PAGE; 22027 cdb.cdb_opaque[2] = 22028 (char)(page_control << 6) | 22029 un->un_start_stop_cycle_page; 22030 mutex_exit(SD_MUTEX(un)); 22031 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22032 status = sd_ssc_send( 22033 ssc, &ucmd_buf, FKIOCTL, 22034 UIO_SYSSPACE, path_flag); 22035 22036 break; 22037 case TEMPERATURE_PAGE: 22038 status = ENOTTY; 22039 break; 22040 default: 22041 break; 22042 } 22043 } 22044 break; 22045 default: 22046 break; 22047 } 22048 break; 22049 default: 22050 break; 22051 } 22052 22053 if (status == 0) { 22054 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22055 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 22056 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 22057 } 22058 22059 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 22060 22061 return (status); 22062 } 22063 22064 22065 /* 22066 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 22067 * 22068 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command. 22069 * 22070 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 22071 * structure for this target. 22072 * bufaddr 22073 * buflen 22074 * class_req 22075 * 22076 * Return Code: 0 - Success 22077 * errno return code from sd_ssc_send() 22078 * 22079 * Context: Can sleep. Does not return until command is completed. 22080 */ 22081 22082 static int 22083 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr, 22084 size_t buflen, uchar_t class_req) 22085 { 22086 union scsi_cdb cdb; 22087 struct uscsi_cmd ucmd_buf; 22088 int status; 22089 struct sd_lun *un; 22090 22091 ASSERT(ssc != NULL); 22092 un = ssc->ssc_un; 22093 ASSERT(un != NULL); 22094 ASSERT(!mutex_owned(SD_MUTEX(un))); 22095 ASSERT(bufaddr != NULL); 22096 22097 SD_TRACE(SD_LOG_IO, un, 22098 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un); 22099 22100 bzero(&cdb, sizeof (cdb)); 22101 bzero(&ucmd_buf, sizeof (ucmd_buf)); 22102 bzero(bufaddr, buflen); 22103 22104 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION; 22105 cdb.cdb_opaque[1] = 1; /* polled */ 22106 cdb.cdb_opaque[4] = class_req; 22107 FORMG1COUNT(&cdb, buflen); 22108 22109 ucmd_buf.uscsi_cdb = (char *)&cdb; 22110 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 22111 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 22112 ucmd_buf.uscsi_buflen = buflen; 22113 ucmd_buf.uscsi_rqbuf = NULL; 22114 ucmd_buf.uscsi_rqlen = 0; 22115 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 22116 ucmd_buf.uscsi_timeout = 60; 22117 22118 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 22119 UIO_SYSSPACE, SD_PATH_DIRECT); 22120 22121 /* 22122 * Only handle status == 0, the upper-level caller 22123 * will put different assessment based on the context. 22124 */ 22125 if (status == 0) { 22126 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22127 22128 if (ucmd_buf.uscsi_resid != 0) { 22129 status = EIO; 22130 } 22131 } 22132 22133 SD_TRACE(SD_LOG_IO, un, 22134 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n"); 22135 22136 return (status); 22137 } 22138 22139 22140 static boolean_t 22141 sd_gesn_media_data_valid(uchar_t *data) 22142 { 22143 uint16_t len; 22144 22145 len = (data[1] << 8) | data[0]; 22146 return ((len >= 6) && 22147 ((data[2] & SD_GESN_HEADER_NEA) == 0) && 22148 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) && 22149 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0)); 22150 } 22151 22152 22153 /* 22154 * Function: sdioctl 22155 * 22156 * Description: Driver's ioctl(9e) entry point function. 22157 * 22158 * Arguments: dev - device number 22159 * cmd - ioctl operation to be performed 22160 * arg - user argument, contains data to be set or reference 22161 * parameter for get 22162 * flag - bit flag, indicating open settings, 32/64 bit type 22163 * cred_p - user credential pointer 22164 * rval_p - calling process return value (OPT) 22165 * 22166 * Return Code: EINVAL 22167 * ENOTTY 22168 * ENXIO 22169 * EIO 22170 * EFAULT 22171 * ENOTSUP 22172 * EPERM 22173 * 22174 * Context: Called from the device switch at normal priority. 22175 */ 22176 22177 static int 22178 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 22179 { 22180 struct sd_lun *un = NULL; 22181 int err = 0; 22182 int i = 0; 22183 cred_t *cr; 22184 int tmprval = EINVAL; 22185 boolean_t is_valid; 22186 sd_ssc_t *ssc; 22187 22188 /* 22189 * All device accesses go thru sdstrategy where we check on suspend 22190 * status 22191 */ 22192 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22193 return (ENXIO); 22194 } 22195 22196 ASSERT(!mutex_owned(SD_MUTEX(un))); 22197 22198 /* Initialize sd_ssc_t for internal uscsi commands */ 22199 ssc = sd_ssc_init(un); 22200 22201 is_valid = SD_IS_VALID_LABEL(un); 22202 22203 /* 22204 * Moved this wait from sd_uscsi_strategy to here for 22205 * reasons of deadlock prevention. Internal driver commands, 22206 * specifically those to change a devices power level, result 22207 * in a call to sd_uscsi_strategy. 22208 */ 22209 mutex_enter(SD_MUTEX(un)); 22210 while ((un->un_state == SD_STATE_SUSPENDED) || 22211 (un->un_state == SD_STATE_PM_CHANGING)) { 22212 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 22213 } 22214 /* 22215 * Twiddling the counter here protects commands from now 22216 * through to the top of sd_uscsi_strategy. Without the 22217 * counter inc. a power down, for example, could get in 22218 * after the above check for state is made and before 22219 * execution gets to the top of sd_uscsi_strategy. 22220 * That would cause problems. 22221 */ 22222 un->un_ncmds_in_driver++; 22223 22224 if (!is_valid && 22225 (flag & (FNDELAY | FNONBLOCK))) { 22226 switch (cmd) { 22227 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 22228 case DKIOCGVTOC: 22229 case DKIOCGEXTVTOC: 22230 case DKIOCGAPART: 22231 case DKIOCPARTINFO: 22232 case DKIOCEXTPARTINFO: 22233 case DKIOCSGEOM: 22234 case DKIOCSAPART: 22235 case DKIOCGETEFI: 22236 case DKIOCPARTITION: 22237 case DKIOCSVTOC: 22238 case DKIOCSEXTVTOC: 22239 case DKIOCSETEFI: 22240 case DKIOCGMBOOT: 22241 case DKIOCSMBOOT: 22242 case DKIOCG_PHYGEOM: 22243 case DKIOCG_VIRTGEOM: 22244 #if defined(__i386) || defined(__amd64) 22245 case DKIOCSETEXTPART: 22246 #endif 22247 /* let cmlb handle it */ 22248 goto skip_ready_valid; 22249 22250 case CDROMPAUSE: 22251 case CDROMRESUME: 22252 case CDROMPLAYMSF: 22253 case CDROMPLAYTRKIND: 22254 case CDROMREADTOCHDR: 22255 case CDROMREADTOCENTRY: 22256 case CDROMSTOP: 22257 case CDROMSTART: 22258 case CDROMVOLCTRL: 22259 case CDROMSUBCHNL: 22260 case CDROMREADMODE2: 22261 case CDROMREADMODE1: 22262 case CDROMREADOFFSET: 22263 case CDROMSBLKMODE: 22264 case CDROMGBLKMODE: 22265 case CDROMGDRVSPEED: 22266 case CDROMSDRVSPEED: 22267 case CDROMCDDA: 22268 case CDROMCDXA: 22269 case CDROMSUBCODE: 22270 if (!ISCD(un)) { 22271 un->un_ncmds_in_driver--; 22272 ASSERT(un->un_ncmds_in_driver >= 0); 22273 mutex_exit(SD_MUTEX(un)); 22274 err = ENOTTY; 22275 goto done_without_assess; 22276 } 22277 break; 22278 case FDEJECT: 22279 case DKIOCEJECT: 22280 case CDROMEJECT: 22281 if (!un->un_f_eject_media_supported) { 22282 un->un_ncmds_in_driver--; 22283 ASSERT(un->un_ncmds_in_driver >= 0); 22284 mutex_exit(SD_MUTEX(un)); 22285 err = ENOTTY; 22286 goto done_without_assess; 22287 } 22288 break; 22289 case DKIOCFLUSHWRITECACHE: 22290 mutex_exit(SD_MUTEX(un)); 22291 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22292 if (err != 0) { 22293 mutex_enter(SD_MUTEX(un)); 22294 un->un_ncmds_in_driver--; 22295 ASSERT(un->un_ncmds_in_driver >= 0); 22296 mutex_exit(SD_MUTEX(un)); 22297 err = EIO; 22298 goto done_quick_assess; 22299 } 22300 mutex_enter(SD_MUTEX(un)); 22301 /* FALLTHROUGH */ 22302 case DKIOCREMOVABLE: 22303 case DKIOCHOTPLUGGABLE: 22304 case DKIOCINFO: 22305 case DKIOCGMEDIAINFO: 22306 case DKIOCGMEDIAINFOEXT: 22307 case MHIOCENFAILFAST: 22308 case MHIOCSTATUS: 22309 case MHIOCTKOWN: 22310 case MHIOCRELEASE: 22311 case MHIOCGRP_INKEYS: 22312 case MHIOCGRP_INRESV: 22313 case MHIOCGRP_REGISTER: 22314 case MHIOCGRP_CLEAR: 22315 case MHIOCGRP_RESERVE: 22316 case MHIOCGRP_PREEMPTANDABORT: 22317 case MHIOCGRP_REGISTERANDIGNOREKEY: 22318 case CDROMCLOSETRAY: 22319 case USCSICMD: 22320 goto skip_ready_valid; 22321 default: 22322 break; 22323 } 22324 22325 mutex_exit(SD_MUTEX(un)); 22326 err = sd_ready_and_valid(ssc, SDPART(dev)); 22327 mutex_enter(SD_MUTEX(un)); 22328 22329 if (err != SD_READY_VALID) { 22330 switch (cmd) { 22331 case DKIOCSTATE: 22332 case CDROMGDRVSPEED: 22333 case CDROMSDRVSPEED: 22334 case FDEJECT: /* for eject command */ 22335 case DKIOCEJECT: 22336 case CDROMEJECT: 22337 case DKIOCREMOVABLE: 22338 case DKIOCHOTPLUGGABLE: 22339 break; 22340 default: 22341 if (un->un_f_has_removable_media) { 22342 err = ENXIO; 22343 } else { 22344 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 22345 if (err == SD_RESERVED_BY_OTHERS) { 22346 err = EACCES; 22347 } else { 22348 err = EIO; 22349 } 22350 } 22351 un->un_ncmds_in_driver--; 22352 ASSERT(un->un_ncmds_in_driver >= 0); 22353 mutex_exit(SD_MUTEX(un)); 22354 22355 goto done_without_assess; 22356 } 22357 } 22358 } 22359 22360 skip_ready_valid: 22361 mutex_exit(SD_MUTEX(un)); 22362 22363 switch (cmd) { 22364 case DKIOCINFO: 22365 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 22366 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 22367 break; 22368 22369 case DKIOCGMEDIAINFO: 22370 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 22371 err = sd_get_media_info(dev, (caddr_t)arg, flag); 22372 break; 22373 22374 case DKIOCGMEDIAINFOEXT: 22375 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n"); 22376 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag); 22377 break; 22378 22379 case DKIOCGGEOM: 22380 case DKIOCGVTOC: 22381 case DKIOCGEXTVTOC: 22382 case DKIOCGAPART: 22383 case DKIOCPARTINFO: 22384 case DKIOCEXTPARTINFO: 22385 case DKIOCSGEOM: 22386 case DKIOCSAPART: 22387 case DKIOCGETEFI: 22388 case DKIOCPARTITION: 22389 case DKIOCSVTOC: 22390 case DKIOCSEXTVTOC: 22391 case DKIOCSETEFI: 22392 case DKIOCGMBOOT: 22393 case DKIOCSMBOOT: 22394 case DKIOCG_PHYGEOM: 22395 case DKIOCG_VIRTGEOM: 22396 #if defined(__i386) || defined(__amd64) 22397 case DKIOCSETEXTPART: 22398 #endif 22399 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 22400 22401 /* TUR should spin up */ 22402 22403 if (un->un_f_has_removable_media) 22404 err = sd_send_scsi_TEST_UNIT_READY(ssc, 22405 SD_CHECK_FOR_MEDIA); 22406 22407 else 22408 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22409 22410 if (err != 0) 22411 goto done_with_assess; 22412 22413 err = cmlb_ioctl(un->un_cmlbhandle, dev, 22414 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 22415 22416 if ((err == 0) && 22417 ((cmd == DKIOCSETEFI) || 22418 (un->un_f_pkstats_enabled) && 22419 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC || 22420 cmd == DKIOCSEXTVTOC))) { 22421 22422 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 22423 (void *)SD_PATH_DIRECT); 22424 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 22425 sd_set_pstats(un); 22426 SD_TRACE(SD_LOG_IO_PARTITION, un, 22427 "sd_ioctl: un:0x%p pstats created and " 22428 "set\n", un); 22429 } 22430 } 22431 22432 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) || 22433 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 22434 22435 mutex_enter(SD_MUTEX(un)); 22436 if (un->un_f_devid_supported && 22437 (un->un_f_opt_fab_devid == TRUE)) { 22438 if (un->un_devid == NULL) { 22439 sd_register_devid(ssc, SD_DEVINFO(un), 22440 SD_TARGET_IS_UNRESERVED); 22441 } else { 22442 /* 22443 * The device id for this disk 22444 * has been fabricated. The 22445 * device id must be preserved 22446 * by writing it back out to 22447 * disk. 22448 */ 22449 if (sd_write_deviceid(ssc) != 0) { 22450 ddi_devid_free(un->un_devid); 22451 un->un_devid = NULL; 22452 } 22453 } 22454 } 22455 mutex_exit(SD_MUTEX(un)); 22456 } 22457 22458 break; 22459 22460 case DKIOCLOCK: 22461 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 22462 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 22463 SD_PATH_STANDARD); 22464 goto done_with_assess; 22465 22466 case DKIOCUNLOCK: 22467 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 22468 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 22469 SD_PATH_STANDARD); 22470 goto done_with_assess; 22471 22472 case DKIOCSTATE: { 22473 enum dkio_state state; 22474 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 22475 22476 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 22477 err = EFAULT; 22478 } else { 22479 err = sd_check_media(dev, state); 22480 if (err == 0) { 22481 if (ddi_copyout(&un->un_mediastate, (void *)arg, 22482 sizeof (int), flag) != 0) 22483 err = EFAULT; 22484 } 22485 } 22486 break; 22487 } 22488 22489 case DKIOCREMOVABLE: 22490 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 22491 i = un->un_f_has_removable_media ? 1 : 0; 22492 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22493 err = EFAULT; 22494 } else { 22495 err = 0; 22496 } 22497 break; 22498 22499 case DKIOCHOTPLUGGABLE: 22500 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 22501 i = un->un_f_is_hotpluggable ? 1 : 0; 22502 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22503 err = EFAULT; 22504 } else { 22505 err = 0; 22506 } 22507 break; 22508 22509 case DKIOCREADONLY: 22510 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n"); 22511 i = 0; 22512 if ((ISCD(un) && !un->un_f_mmc_writable_media) || 22513 (sr_check_wp(dev) != 0)) { 22514 i = 1; 22515 } 22516 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22517 err = EFAULT; 22518 } else { 22519 err = 0; 22520 } 22521 break; 22522 22523 case DKIOCGTEMPERATURE: 22524 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 22525 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 22526 break; 22527 22528 case MHIOCENFAILFAST: 22529 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 22530 if ((err = drv_priv(cred_p)) == 0) { 22531 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 22532 } 22533 break; 22534 22535 case MHIOCTKOWN: 22536 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 22537 if ((err = drv_priv(cred_p)) == 0) { 22538 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 22539 } 22540 break; 22541 22542 case MHIOCRELEASE: 22543 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 22544 if ((err = drv_priv(cred_p)) == 0) { 22545 err = sd_mhdioc_release(dev); 22546 } 22547 break; 22548 22549 case MHIOCSTATUS: 22550 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 22551 if ((err = drv_priv(cred_p)) == 0) { 22552 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) { 22553 case 0: 22554 err = 0; 22555 break; 22556 case EACCES: 22557 *rval_p = 1; 22558 err = 0; 22559 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22560 break; 22561 default: 22562 err = EIO; 22563 goto done_with_assess; 22564 } 22565 } 22566 break; 22567 22568 case MHIOCQRESERVE: 22569 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 22570 if ((err = drv_priv(cred_p)) == 0) { 22571 err = sd_reserve_release(dev, SD_RESERVE); 22572 } 22573 break; 22574 22575 case MHIOCREREGISTERDEVID: 22576 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 22577 if (drv_priv(cred_p) == EPERM) { 22578 err = EPERM; 22579 } else if (!un->un_f_devid_supported) { 22580 err = ENOTTY; 22581 } else { 22582 err = sd_mhdioc_register_devid(dev); 22583 } 22584 break; 22585 22586 case MHIOCGRP_INKEYS: 22587 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 22588 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22589 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22590 err = ENOTSUP; 22591 } else { 22592 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 22593 flag); 22594 } 22595 } 22596 break; 22597 22598 case MHIOCGRP_INRESV: 22599 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 22600 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22601 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22602 err = ENOTSUP; 22603 } else { 22604 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 22605 } 22606 } 22607 break; 22608 22609 case MHIOCGRP_REGISTER: 22610 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 22611 if ((err = drv_priv(cred_p)) != EPERM) { 22612 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22613 err = ENOTSUP; 22614 } else if (arg != NULL) { 22615 mhioc_register_t reg; 22616 if (ddi_copyin((void *)arg, ®, 22617 sizeof (mhioc_register_t), flag) != 0) { 22618 err = EFAULT; 22619 } else { 22620 err = 22621 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22622 ssc, SD_SCSI3_REGISTER, 22623 (uchar_t *)®); 22624 if (err != 0) 22625 goto done_with_assess; 22626 } 22627 } 22628 } 22629 break; 22630 22631 case MHIOCGRP_CLEAR: 22632 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n"); 22633 if ((err = drv_priv(cred_p)) != EPERM) { 22634 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22635 err = ENOTSUP; 22636 } else if (arg != NULL) { 22637 mhioc_register_t reg; 22638 if (ddi_copyin((void *)arg, ®, 22639 sizeof (mhioc_register_t), flag) != 0) { 22640 err = EFAULT; 22641 } else { 22642 err = 22643 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22644 ssc, SD_SCSI3_CLEAR, 22645 (uchar_t *)®); 22646 if (err != 0) 22647 goto done_with_assess; 22648 } 22649 } 22650 } 22651 break; 22652 22653 case MHIOCGRP_RESERVE: 22654 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 22655 if ((err = drv_priv(cred_p)) != EPERM) { 22656 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22657 err = ENOTSUP; 22658 } else if (arg != NULL) { 22659 mhioc_resv_desc_t resv_desc; 22660 if (ddi_copyin((void *)arg, &resv_desc, 22661 sizeof (mhioc_resv_desc_t), flag) != 0) { 22662 err = EFAULT; 22663 } else { 22664 err = 22665 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22666 ssc, SD_SCSI3_RESERVE, 22667 (uchar_t *)&resv_desc); 22668 if (err != 0) 22669 goto done_with_assess; 22670 } 22671 } 22672 } 22673 break; 22674 22675 case MHIOCGRP_PREEMPTANDABORT: 22676 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 22677 if ((err = drv_priv(cred_p)) != EPERM) { 22678 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22679 err = ENOTSUP; 22680 } else if (arg != NULL) { 22681 mhioc_preemptandabort_t preempt_abort; 22682 if (ddi_copyin((void *)arg, &preempt_abort, 22683 sizeof (mhioc_preemptandabort_t), 22684 flag) != 0) { 22685 err = EFAULT; 22686 } else { 22687 err = 22688 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22689 ssc, SD_SCSI3_PREEMPTANDABORT, 22690 (uchar_t *)&preempt_abort); 22691 if (err != 0) 22692 goto done_with_assess; 22693 } 22694 } 22695 } 22696 break; 22697 22698 case MHIOCGRP_REGISTERANDIGNOREKEY: 22699 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 22700 if ((err = drv_priv(cred_p)) != EPERM) { 22701 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22702 err = ENOTSUP; 22703 } else if (arg != NULL) { 22704 mhioc_registerandignorekey_t r_and_i; 22705 if (ddi_copyin((void *)arg, (void *)&r_and_i, 22706 sizeof (mhioc_registerandignorekey_t), 22707 flag) != 0) { 22708 err = EFAULT; 22709 } else { 22710 err = 22711 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22712 ssc, SD_SCSI3_REGISTERANDIGNOREKEY, 22713 (uchar_t *)&r_and_i); 22714 if (err != 0) 22715 goto done_with_assess; 22716 } 22717 } 22718 } 22719 break; 22720 22721 case USCSICMD: 22722 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 22723 cr = ddi_get_cred(); 22724 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 22725 err = EPERM; 22726 } else { 22727 enum uio_seg uioseg; 22728 22729 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 22730 UIO_USERSPACE; 22731 if (un->un_f_format_in_progress == TRUE) { 22732 err = EAGAIN; 22733 break; 22734 } 22735 22736 err = sd_ssc_send(ssc, 22737 (struct uscsi_cmd *)arg, 22738 flag, uioseg, SD_PATH_STANDARD); 22739 if (err != 0) 22740 goto done_with_assess; 22741 else 22742 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22743 } 22744 break; 22745 22746 case CDROMPAUSE: 22747 case CDROMRESUME: 22748 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 22749 if (!ISCD(un)) { 22750 err = ENOTTY; 22751 } else { 22752 err = sr_pause_resume(dev, cmd); 22753 } 22754 break; 22755 22756 case CDROMPLAYMSF: 22757 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 22758 if (!ISCD(un)) { 22759 err = ENOTTY; 22760 } else { 22761 err = sr_play_msf(dev, (caddr_t)arg, flag); 22762 } 22763 break; 22764 22765 case CDROMPLAYTRKIND: 22766 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 22767 #if defined(__i386) || defined(__amd64) 22768 /* 22769 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 22770 */ 22771 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22772 #else 22773 if (!ISCD(un)) { 22774 #endif 22775 err = ENOTTY; 22776 } else { 22777 err = sr_play_trkind(dev, (caddr_t)arg, flag); 22778 } 22779 break; 22780 22781 case CDROMREADTOCHDR: 22782 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 22783 if (!ISCD(un)) { 22784 err = ENOTTY; 22785 } else { 22786 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 22787 } 22788 break; 22789 22790 case CDROMREADTOCENTRY: 22791 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 22792 if (!ISCD(un)) { 22793 err = ENOTTY; 22794 } else { 22795 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 22796 } 22797 break; 22798 22799 case CDROMSTOP: 22800 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 22801 if (!ISCD(un)) { 22802 err = ENOTTY; 22803 } else { 22804 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22805 SD_TARGET_STOP, SD_PATH_STANDARD); 22806 goto done_with_assess; 22807 } 22808 break; 22809 22810 case CDROMSTART: 22811 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 22812 if (!ISCD(un)) { 22813 err = ENOTTY; 22814 } else { 22815 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22816 SD_TARGET_START, SD_PATH_STANDARD); 22817 goto done_with_assess; 22818 } 22819 break; 22820 22821 case CDROMCLOSETRAY: 22822 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 22823 if (!ISCD(un)) { 22824 err = ENOTTY; 22825 } else { 22826 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22827 SD_TARGET_CLOSE, SD_PATH_STANDARD); 22828 goto done_with_assess; 22829 } 22830 break; 22831 22832 case FDEJECT: /* for eject command */ 22833 case DKIOCEJECT: 22834 case CDROMEJECT: 22835 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 22836 if (!un->un_f_eject_media_supported) { 22837 err = ENOTTY; 22838 } else { 22839 err = sr_eject(dev); 22840 } 22841 break; 22842 22843 case CDROMVOLCTRL: 22844 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 22845 if (!ISCD(un)) { 22846 err = ENOTTY; 22847 } else { 22848 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 22849 } 22850 break; 22851 22852 case CDROMSUBCHNL: 22853 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 22854 if (!ISCD(un)) { 22855 err = ENOTTY; 22856 } else { 22857 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 22858 } 22859 break; 22860 22861 case CDROMREADMODE2: 22862 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 22863 if (!ISCD(un)) { 22864 err = ENOTTY; 22865 } else if (un->un_f_cfg_is_atapi == TRUE) { 22866 /* 22867 * If the drive supports READ CD, use that instead of 22868 * switching the LBA size via a MODE SELECT 22869 * Block Descriptor 22870 */ 22871 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 22872 } else { 22873 err = sr_read_mode2(dev, (caddr_t)arg, flag); 22874 } 22875 break; 22876 22877 case CDROMREADMODE1: 22878 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 22879 if (!ISCD(un)) { 22880 err = ENOTTY; 22881 } else { 22882 err = sr_read_mode1(dev, (caddr_t)arg, flag); 22883 } 22884 break; 22885 22886 case CDROMREADOFFSET: 22887 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 22888 if (!ISCD(un)) { 22889 err = ENOTTY; 22890 } else { 22891 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 22892 flag); 22893 } 22894 break; 22895 22896 case CDROMSBLKMODE: 22897 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 22898 /* 22899 * There is no means of changing block size in case of atapi 22900 * drives, thus return ENOTTY if drive type is atapi 22901 */ 22902 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22903 err = ENOTTY; 22904 } else if (un->un_f_mmc_cap == TRUE) { 22905 22906 /* 22907 * MMC Devices do not support changing the 22908 * logical block size 22909 * 22910 * Note: EINVAL is being returned instead of ENOTTY to 22911 * maintain consistancy with the original mmc 22912 * driver update. 22913 */ 22914 err = EINVAL; 22915 } else { 22916 mutex_enter(SD_MUTEX(un)); 22917 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 22918 (un->un_ncmds_in_transport > 0)) { 22919 mutex_exit(SD_MUTEX(un)); 22920 err = EINVAL; 22921 } else { 22922 mutex_exit(SD_MUTEX(un)); 22923 err = sr_change_blkmode(dev, cmd, arg, flag); 22924 } 22925 } 22926 break; 22927 22928 case CDROMGBLKMODE: 22929 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 22930 if (!ISCD(un)) { 22931 err = ENOTTY; 22932 } else if ((un->un_f_cfg_is_atapi != FALSE) && 22933 (un->un_f_blockcount_is_valid != FALSE)) { 22934 /* 22935 * Drive is an ATAPI drive so return target block 22936 * size for ATAPI drives since we cannot change the 22937 * blocksize on ATAPI drives. Used primarily to detect 22938 * if an ATAPI cdrom is present. 22939 */ 22940 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 22941 sizeof (int), flag) != 0) { 22942 err = EFAULT; 22943 } else { 22944 err = 0; 22945 } 22946 22947 } else { 22948 /* 22949 * Drive supports changing block sizes via a Mode 22950 * Select. 22951 */ 22952 err = sr_change_blkmode(dev, cmd, arg, flag); 22953 } 22954 break; 22955 22956 case CDROMGDRVSPEED: 22957 case CDROMSDRVSPEED: 22958 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22959 if (!ISCD(un)) { 22960 err = ENOTTY; 22961 } else if (un->un_f_mmc_cap == TRUE) { 22962 /* 22963 * Note: In the future the driver implementation 22964 * for getting and 22965 * setting cd speed should entail: 22966 * 1) If non-mmc try the Toshiba mode page 22967 * (sr_change_speed) 22968 * 2) If mmc but no support for Real Time Streaming try 22969 * the SET CD SPEED (0xBB) command 22970 * (sr_atapi_change_speed) 22971 * 3) If mmc and support for Real Time Streaming 22972 * try the GET PERFORMANCE and SET STREAMING 22973 * commands (not yet implemented, 4380808) 22974 */ 22975 /* 22976 * As per recent MMC spec, CD-ROM speed is variable 22977 * and changes with LBA. Since there is no such 22978 * things as drive speed now, fail this ioctl. 22979 * 22980 * Note: EINVAL is returned for consistancy of original 22981 * implementation which included support for getting 22982 * the drive speed of mmc devices but not setting 22983 * the drive speed. Thus EINVAL would be returned 22984 * if a set request was made for an mmc device. 22985 * We no longer support get or set speed for 22986 * mmc but need to remain consistent with regard 22987 * to the error code returned. 22988 */ 22989 err = EINVAL; 22990 } else if (un->un_f_cfg_is_atapi == TRUE) { 22991 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22992 } else { 22993 err = sr_change_speed(dev, cmd, arg, flag); 22994 } 22995 break; 22996 22997 case CDROMCDDA: 22998 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22999 if (!ISCD(un)) { 23000 err = ENOTTY; 23001 } else { 23002 err = sr_read_cdda(dev, (void *)arg, flag); 23003 } 23004 break; 23005 23006 case CDROMCDXA: 23007 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 23008 if (!ISCD(un)) { 23009 err = ENOTTY; 23010 } else { 23011 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 23012 } 23013 break; 23014 23015 case CDROMSUBCODE: 23016 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 23017 if (!ISCD(un)) { 23018 err = ENOTTY; 23019 } else { 23020 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 23021 } 23022 break; 23023 23024 23025 #ifdef SDDEBUG 23026 /* RESET/ABORTS testing ioctls */ 23027 case DKIOCRESET: { 23028 int reset_level; 23029 23030 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 23031 err = EFAULT; 23032 } else { 23033 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 23034 "reset_level = 0x%lx\n", reset_level); 23035 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 23036 err = 0; 23037 } else { 23038 err = EIO; 23039 } 23040 } 23041 break; 23042 } 23043 23044 case DKIOCABORT: 23045 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 23046 if (scsi_abort(SD_ADDRESS(un), NULL)) { 23047 err = 0; 23048 } else { 23049 err = EIO; 23050 } 23051 break; 23052 #endif 23053 23054 #ifdef SD_FAULT_INJECTION 23055 /* SDIOC FaultInjection testing ioctls */ 23056 case SDIOCSTART: 23057 case SDIOCSTOP: 23058 case SDIOCINSERTPKT: 23059 case SDIOCINSERTXB: 23060 case SDIOCINSERTUN: 23061 case SDIOCINSERTARQ: 23062 case SDIOCPUSH: 23063 case SDIOCRETRIEVE: 23064 case SDIOCRUN: 23065 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 23066 "SDIOC detected cmd:0x%X:\n", cmd); 23067 /* call error generator */ 23068 sd_faultinjection_ioctl(cmd, arg, un); 23069 err = 0; 23070 break; 23071 23072 #endif /* SD_FAULT_INJECTION */ 23073 23074 case DKIOCFLUSHWRITECACHE: 23075 { 23076 struct dk_callback *dkc = (struct dk_callback *)arg; 23077 23078 mutex_enter(SD_MUTEX(un)); 23079 if (!un->un_f_sync_cache_supported || 23080 !un->un_f_write_cache_enabled) { 23081 err = un->un_f_sync_cache_supported ? 23082 0 : ENOTSUP; 23083 mutex_exit(SD_MUTEX(un)); 23084 if ((flag & FKIOCTL) && dkc != NULL && 23085 dkc->dkc_callback != NULL) { 23086 (*dkc->dkc_callback)(dkc->dkc_cookie, 23087 err); 23088 /* 23089 * Did callback and reported error. 23090 * Since we did a callback, ioctl 23091 * should return 0. 23092 */ 23093 err = 0; 23094 } 23095 break; 23096 } 23097 mutex_exit(SD_MUTEX(un)); 23098 23099 if ((flag & FKIOCTL) && dkc != NULL && 23100 dkc->dkc_callback != NULL) { 23101 /* async SYNC CACHE request */ 23102 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 23103 } else { 23104 /* synchronous SYNC CACHE request */ 23105 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 23106 } 23107 } 23108 break; 23109 23110 case DKIOCGETWCE: { 23111 23112 int wce; 23113 23114 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) { 23115 break; 23116 } 23117 23118 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 23119 err = EFAULT; 23120 } 23121 break; 23122 } 23123 23124 case DKIOCSETWCE: { 23125 23126 int wce, sync_supported; 23127 int cur_wce = 0; 23128 23129 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 23130 err = EFAULT; 23131 break; 23132 } 23133 23134 /* 23135 * Synchronize multiple threads trying to enable 23136 * or disable the cache via the un_f_wcc_cv 23137 * condition variable. 23138 */ 23139 mutex_enter(SD_MUTEX(un)); 23140 23141 /* 23142 * Don't allow the cache to be enabled if the 23143 * config file has it disabled. 23144 */ 23145 if (un->un_f_opt_disable_cache && wce) { 23146 mutex_exit(SD_MUTEX(un)); 23147 err = EINVAL; 23148 break; 23149 } 23150 23151 /* 23152 * Wait for write cache change in progress 23153 * bit to be clear before proceeding. 23154 */ 23155 while (un->un_f_wcc_inprog) 23156 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 23157 23158 un->un_f_wcc_inprog = 1; 23159 23160 mutex_exit(SD_MUTEX(un)); 23161 23162 /* 23163 * Get the current write cache state 23164 */ 23165 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) { 23166 mutex_enter(SD_MUTEX(un)); 23167 un->un_f_wcc_inprog = 0; 23168 cv_broadcast(&un->un_wcc_cv); 23169 mutex_exit(SD_MUTEX(un)); 23170 break; 23171 } 23172 23173 mutex_enter(SD_MUTEX(un)); 23174 un->un_f_write_cache_enabled = (cur_wce != 0); 23175 23176 if (un->un_f_write_cache_enabled && wce == 0) { 23177 /* 23178 * Disable the write cache. Don't clear 23179 * un_f_write_cache_enabled until after 23180 * the mode select and flush are complete. 23181 */ 23182 sync_supported = un->un_f_sync_cache_supported; 23183 23184 /* 23185 * If cache flush is suppressed, we assume that the 23186 * controller firmware will take care of managing the 23187 * write cache for us: no need to explicitly 23188 * disable it. 23189 */ 23190 if (!un->un_f_suppress_cache_flush) { 23191 mutex_exit(SD_MUTEX(un)); 23192 if ((err = sd_cache_control(ssc, 23193 SD_CACHE_NOCHANGE, 23194 SD_CACHE_DISABLE)) == 0 && 23195 sync_supported) { 23196 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 23197 NULL); 23198 } 23199 } else { 23200 mutex_exit(SD_MUTEX(un)); 23201 } 23202 23203 mutex_enter(SD_MUTEX(un)); 23204 if (err == 0) { 23205 un->un_f_write_cache_enabled = 0; 23206 } 23207 23208 } else if (!un->un_f_write_cache_enabled && wce != 0) { 23209 /* 23210 * Set un_f_write_cache_enabled first, so there is 23211 * no window where the cache is enabled, but the 23212 * bit says it isn't. 23213 */ 23214 un->un_f_write_cache_enabled = 1; 23215 23216 /* 23217 * If cache flush is suppressed, we assume that the 23218 * controller firmware will take care of managing the 23219 * write cache for us: no need to explicitly 23220 * enable it. 23221 */ 23222 if (!un->un_f_suppress_cache_flush) { 23223 mutex_exit(SD_MUTEX(un)); 23224 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE, 23225 SD_CACHE_ENABLE); 23226 } else { 23227 mutex_exit(SD_MUTEX(un)); 23228 } 23229 23230 mutex_enter(SD_MUTEX(un)); 23231 23232 if (err) { 23233 un->un_f_write_cache_enabled = 0; 23234 } 23235 } 23236 23237 un->un_f_wcc_inprog = 0; 23238 cv_broadcast(&un->un_wcc_cv); 23239 mutex_exit(SD_MUTEX(un)); 23240 break; 23241 } 23242 23243 default: 23244 err = ENOTTY; 23245 break; 23246 } 23247 mutex_enter(SD_MUTEX(un)); 23248 un->un_ncmds_in_driver--; 23249 ASSERT(un->un_ncmds_in_driver >= 0); 23250 mutex_exit(SD_MUTEX(un)); 23251 23252 23253 done_without_assess: 23254 sd_ssc_fini(ssc); 23255 23256 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23257 return (err); 23258 23259 done_with_assess: 23260 mutex_enter(SD_MUTEX(un)); 23261 un->un_ncmds_in_driver--; 23262 ASSERT(un->un_ncmds_in_driver >= 0); 23263 mutex_exit(SD_MUTEX(un)); 23264 23265 done_quick_assess: 23266 if (err != 0) 23267 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23268 /* Uninitialize sd_ssc_t pointer */ 23269 sd_ssc_fini(ssc); 23270 23271 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23272 return (err); 23273 } 23274 23275 23276 /* 23277 * Function: sd_dkio_ctrl_info 23278 * 23279 * Description: This routine is the driver entry point for handling controller 23280 * information ioctl requests (DKIOCINFO). 23281 * 23282 * Arguments: dev - the device number 23283 * arg - pointer to user provided dk_cinfo structure 23284 * specifying the controller type and attributes. 23285 * flag - this argument is a pass through to ddi_copyxxx() 23286 * directly from the mode argument of ioctl(). 23287 * 23288 * Return Code: 0 23289 * EFAULT 23290 * ENXIO 23291 */ 23292 23293 static int 23294 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 23295 { 23296 struct sd_lun *un = NULL; 23297 struct dk_cinfo *info; 23298 dev_info_t *pdip; 23299 int lun, tgt; 23300 23301 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23302 return (ENXIO); 23303 } 23304 23305 info = (struct dk_cinfo *) 23306 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 23307 23308 switch (un->un_ctype) { 23309 case CTYPE_CDROM: 23310 info->dki_ctype = DKC_CDROM; 23311 break; 23312 default: 23313 info->dki_ctype = DKC_SCSI_CCS; 23314 break; 23315 } 23316 pdip = ddi_get_parent(SD_DEVINFO(un)); 23317 info->dki_cnum = ddi_get_instance(pdip); 23318 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 23319 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 23320 } else { 23321 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 23322 DK_DEVLEN - 1); 23323 } 23324 23325 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23326 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 23327 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23328 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 23329 23330 /* Unit Information */ 23331 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 23332 info->dki_slave = ((tgt << 3) | lun); 23333 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 23334 DK_DEVLEN - 1); 23335 info->dki_flags = DKI_FMTVOL; 23336 info->dki_partition = SDPART(dev); 23337 23338 /* Max Transfer size of this device in blocks */ 23339 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 23340 info->dki_addr = 0; 23341 info->dki_space = 0; 23342 info->dki_prio = 0; 23343 info->dki_vec = 0; 23344 23345 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 23346 kmem_free(info, sizeof (struct dk_cinfo)); 23347 return (EFAULT); 23348 } else { 23349 kmem_free(info, sizeof (struct dk_cinfo)); 23350 return (0); 23351 } 23352 } 23353 23354 /* 23355 * Function: sd_get_media_info_com 23356 * 23357 * Description: This routine returns the information required to populate 23358 * the fields for the dk_minfo/dk_minfo_ext structures. 23359 * 23360 * Arguments: dev - the device number 23361 * dki_media_type - media_type 23362 * dki_lbsize - logical block size 23363 * dki_capacity - capacity in blocks 23364 * dki_pbsize - physical block size (if requested) 23365 * 23366 * Return Code: 0 23367 * EACCESS 23368 * EFAULT 23369 * ENXIO 23370 * EIO 23371 */ 23372 static int 23373 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize, 23374 diskaddr_t *dki_capacity, uint_t *dki_pbsize) 23375 { 23376 struct sd_lun *un = NULL; 23377 struct uscsi_cmd com; 23378 struct scsi_inquiry *sinq; 23379 u_longlong_t media_capacity; 23380 uint64_t capacity; 23381 uint_t lbasize; 23382 uint_t pbsize; 23383 uchar_t *out_data; 23384 uchar_t *rqbuf; 23385 int rval = 0; 23386 int rtn; 23387 sd_ssc_t *ssc; 23388 23389 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 23390 (un->un_state == SD_STATE_OFFLINE)) { 23391 return (ENXIO); 23392 } 23393 23394 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n"); 23395 23396 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 23397 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 23398 ssc = sd_ssc_init(un); 23399 23400 /* Issue a TUR to determine if the drive is ready with media present */ 23401 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 23402 if (rval == ENXIO) { 23403 goto done; 23404 } else if (rval != 0) { 23405 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23406 } 23407 23408 /* Now get configuration data */ 23409 if (ISCD(un)) { 23410 *dki_media_type = DK_CDROM; 23411 23412 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 23413 if (un->un_f_mmc_cap == TRUE) { 23414 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 23415 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 23416 SD_PATH_STANDARD); 23417 23418 if (rtn) { 23419 /* 23420 * We ignore all failures for CD and need to 23421 * put the assessment before processing code 23422 * to avoid missing assessment for FMA. 23423 */ 23424 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23425 /* 23426 * Failed for other than an illegal request 23427 * or command not supported 23428 */ 23429 if ((com.uscsi_status == STATUS_CHECK) && 23430 (com.uscsi_rqstatus == STATUS_GOOD)) { 23431 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 23432 (rqbuf[12] != 0x20)) { 23433 rval = EIO; 23434 goto no_assessment; 23435 } 23436 } 23437 } else { 23438 /* 23439 * The GET CONFIGURATION command succeeded 23440 * so set the media type according to the 23441 * returned data 23442 */ 23443 *dki_media_type = out_data[6]; 23444 *dki_media_type <<= 8; 23445 *dki_media_type |= out_data[7]; 23446 } 23447 } 23448 } else { 23449 /* 23450 * The profile list is not available, so we attempt to identify 23451 * the media type based on the inquiry data 23452 */ 23453 sinq = un->un_sd->sd_inq; 23454 if ((sinq->inq_dtype == DTYPE_DIRECT) || 23455 (sinq->inq_dtype == DTYPE_OPTICAL)) { 23456 /* This is a direct access device or optical disk */ 23457 *dki_media_type = DK_FIXED_DISK; 23458 23459 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 23460 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 23461 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 23462 *dki_media_type = DK_ZIP; 23463 } else if ( 23464 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 23465 *dki_media_type = DK_JAZ; 23466 } 23467 } 23468 } else { 23469 /* 23470 * Not a CD, direct access or optical disk so return 23471 * unknown media 23472 */ 23473 *dki_media_type = DK_UNKNOWN; 23474 } 23475 } 23476 23477 /* 23478 * Now read the capacity so we can provide the lbasize, 23479 * pbsize and capacity. 23480 */ 23481 if (dki_pbsize && un->un_f_descr_format_supported) 23482 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 23483 &pbsize, SD_PATH_DIRECT); 23484 23485 if (dki_pbsize == NULL || rval != 0 || 23486 !un->un_f_descr_format_supported) { 23487 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 23488 SD_PATH_DIRECT); 23489 23490 switch (rval) { 23491 case 0: 23492 if (un->un_f_enable_rmw && 23493 un->un_phy_blocksize != 0) { 23494 pbsize = un->un_phy_blocksize; 23495 } else { 23496 pbsize = lbasize; 23497 } 23498 media_capacity = capacity; 23499 23500 /* 23501 * sd_send_scsi_READ_CAPACITY() reports capacity in 23502 * un->un_sys_blocksize chunks. So we need to convert 23503 * it into cap.lbsize chunks. 23504 */ 23505 if (un->un_f_has_removable_media) { 23506 media_capacity *= un->un_sys_blocksize; 23507 media_capacity /= lbasize; 23508 } 23509 break; 23510 case EACCES: 23511 rval = EACCES; 23512 goto done; 23513 default: 23514 rval = EIO; 23515 goto done; 23516 } 23517 } else { 23518 if (un->un_f_enable_rmw && 23519 !ISP2(pbsize % DEV_BSIZE)) { 23520 pbsize = SSD_SECSIZE; 23521 } else if (!ISP2(lbasize % DEV_BSIZE) || 23522 !ISP2(pbsize % DEV_BSIZE)) { 23523 pbsize = lbasize = DEV_BSIZE; 23524 } 23525 media_capacity = capacity; 23526 } 23527 23528 /* 23529 * If lun is expanded dynamically, update the un structure. 23530 */ 23531 mutex_enter(SD_MUTEX(un)); 23532 if ((un->un_f_blockcount_is_valid == TRUE) && 23533 (un->un_f_tgt_blocksize_is_valid == TRUE) && 23534 (capacity > un->un_blockcount)) { 23535 un->un_f_expnevent = B_FALSE; 23536 sd_update_block_info(un, lbasize, capacity); 23537 } 23538 mutex_exit(SD_MUTEX(un)); 23539 23540 *dki_lbsize = lbasize; 23541 *dki_capacity = media_capacity; 23542 if (dki_pbsize) 23543 *dki_pbsize = pbsize; 23544 23545 done: 23546 if (rval != 0) { 23547 if (rval == EIO) 23548 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23549 else 23550 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23551 } 23552 no_assessment: 23553 sd_ssc_fini(ssc); 23554 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 23555 kmem_free(rqbuf, SENSE_LENGTH); 23556 return (rval); 23557 } 23558 23559 /* 23560 * Function: sd_get_media_info 23561 * 23562 * Description: This routine is the driver entry point for handling ioctl 23563 * requests for the media type or command set profile used by the 23564 * drive to operate on the media (DKIOCGMEDIAINFO). 23565 * 23566 * Arguments: dev - the device number 23567 * arg - pointer to user provided dk_minfo structure 23568 * specifying the media type, logical block size and 23569 * drive capacity. 23570 * flag - this argument is a pass through to ddi_copyxxx() 23571 * directly from the mode argument of ioctl(). 23572 * 23573 * Return Code: returns the value from sd_get_media_info_com 23574 */ 23575 static int 23576 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 23577 { 23578 struct dk_minfo mi; 23579 int rval; 23580 23581 rval = sd_get_media_info_com(dev, &mi.dki_media_type, 23582 &mi.dki_lbsize, &mi.dki_capacity, NULL); 23583 23584 if (rval) 23585 return (rval); 23586 if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag)) 23587 rval = EFAULT; 23588 return (rval); 23589 } 23590 23591 /* 23592 * Function: sd_get_media_info_ext 23593 * 23594 * Description: This routine is the driver entry point for handling ioctl 23595 * requests for the media type or command set profile used by the 23596 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The 23597 * difference this ioctl and DKIOCGMEDIAINFO is the return value 23598 * of this ioctl contains both logical block size and physical 23599 * block size. 23600 * 23601 * 23602 * Arguments: dev - the device number 23603 * arg - pointer to user provided dk_minfo_ext structure 23604 * specifying the media type, logical block size, 23605 * physical block size and disk capacity. 23606 * flag - this argument is a pass through to ddi_copyxxx() 23607 * directly from the mode argument of ioctl(). 23608 * 23609 * Return Code: returns the value from sd_get_media_info_com 23610 */ 23611 static int 23612 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag) 23613 { 23614 struct dk_minfo_ext mie; 23615 int rval = 0; 23616 23617 rval = sd_get_media_info_com(dev, &mie.dki_media_type, 23618 &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize); 23619 23620 if (rval) 23621 return (rval); 23622 if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag)) 23623 rval = EFAULT; 23624 return (rval); 23625 23626 } 23627 23628 /* 23629 * Function: sd_watch_request_submit 23630 * 23631 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit 23632 * depending on which is supported by device. 23633 */ 23634 static opaque_t 23635 sd_watch_request_submit(struct sd_lun *un) 23636 { 23637 dev_t dev; 23638 23639 /* All submissions are unified to use same device number */ 23640 dev = sd_make_device(SD_DEVINFO(un)); 23641 23642 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23643 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un), 23644 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23645 (caddr_t)dev)); 23646 } else { 23647 return (scsi_watch_request_submit(SD_SCSI_DEVP(un), 23648 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23649 (caddr_t)dev)); 23650 } 23651 } 23652 23653 23654 /* 23655 * Function: sd_check_media 23656 * 23657 * Description: This utility routine implements the functionality for the 23658 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23659 * driver state changes from that specified by the user 23660 * (inserted or ejected). For example, if the user specifies 23661 * DKIO_EJECTED and the current media state is inserted this 23662 * routine will immediately return DKIO_INSERTED. However, if the 23663 * current media state is not inserted the user thread will be 23664 * blocked until the drive state changes. If DKIO_NONE is specified 23665 * the user thread will block until a drive state change occurs. 23666 * 23667 * Arguments: dev - the device number 23668 * state - user pointer to a dkio_state, updated with the current 23669 * drive state at return. 23670 * 23671 * Return Code: ENXIO 23672 * EIO 23673 * EAGAIN 23674 * EINTR 23675 */ 23676 23677 static int 23678 sd_check_media(dev_t dev, enum dkio_state state) 23679 { 23680 struct sd_lun *un = NULL; 23681 enum dkio_state prev_state; 23682 opaque_t token = NULL; 23683 int rval = 0; 23684 sd_ssc_t *ssc; 23685 23686 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23687 return (ENXIO); 23688 } 23689 23690 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23691 23692 ssc = sd_ssc_init(un); 23693 23694 mutex_enter(SD_MUTEX(un)); 23695 23696 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23697 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23698 23699 prev_state = un->un_mediastate; 23700 23701 /* is there anything to do? */ 23702 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23703 /* 23704 * submit the request to the scsi_watch service; 23705 * scsi_media_watch_cb() does the real work 23706 */ 23707 mutex_exit(SD_MUTEX(un)); 23708 23709 /* 23710 * This change handles the case where a scsi watch request is 23711 * added to a device that is powered down. To accomplish this 23712 * we power up the device before adding the scsi watch request, 23713 * since the scsi watch sends a TUR directly to the device 23714 * which the device cannot handle if it is powered down. 23715 */ 23716 if (sd_pm_entry(un) != DDI_SUCCESS) { 23717 mutex_enter(SD_MUTEX(un)); 23718 goto done; 23719 } 23720 23721 token = sd_watch_request_submit(un); 23722 23723 sd_pm_exit(un); 23724 23725 mutex_enter(SD_MUTEX(un)); 23726 if (token == NULL) { 23727 rval = EAGAIN; 23728 goto done; 23729 } 23730 23731 /* 23732 * This is a special case IOCTL that doesn't return 23733 * until the media state changes. Routine sdpower 23734 * knows about and handles this so don't count it 23735 * as an active cmd in the driver, which would 23736 * keep the device busy to the pm framework. 23737 * If the count isn't decremented the device can't 23738 * be powered down. 23739 */ 23740 un->un_ncmds_in_driver--; 23741 ASSERT(un->un_ncmds_in_driver >= 0); 23742 23743 /* 23744 * if a prior request had been made, this will be the same 23745 * token, as scsi_watch was designed that way. 23746 */ 23747 un->un_swr_token = token; 23748 un->un_specified_mediastate = state; 23749 23750 /* 23751 * now wait for media change 23752 * we will not be signalled unless mediastate == state but it is 23753 * still better to test for this condition, since there is a 23754 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23755 */ 23756 SD_TRACE(SD_LOG_COMMON, un, 23757 "sd_check_media: waiting for media state change\n"); 23758 while (un->un_mediastate == state) { 23759 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23760 SD_TRACE(SD_LOG_COMMON, un, 23761 "sd_check_media: waiting for media state " 23762 "was interrupted\n"); 23763 un->un_ncmds_in_driver++; 23764 rval = EINTR; 23765 goto done; 23766 } 23767 SD_TRACE(SD_LOG_COMMON, un, 23768 "sd_check_media: received signal, state=%x\n", 23769 un->un_mediastate); 23770 } 23771 /* 23772 * Inc the counter to indicate the device once again 23773 * has an active outstanding cmd. 23774 */ 23775 un->un_ncmds_in_driver++; 23776 } 23777 23778 /* invalidate geometry */ 23779 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23780 sr_ejected(un); 23781 } 23782 23783 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 23784 uint64_t capacity; 23785 uint_t lbasize; 23786 23787 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 23788 mutex_exit(SD_MUTEX(un)); 23789 /* 23790 * Since the following routines use SD_PATH_DIRECT, we must 23791 * call PM directly before the upcoming disk accesses. This 23792 * may cause the disk to be power/spin up. 23793 */ 23794 23795 if (sd_pm_entry(un) == DDI_SUCCESS) { 23796 rval = sd_send_scsi_READ_CAPACITY(ssc, 23797 &capacity, &lbasize, SD_PATH_DIRECT); 23798 if (rval != 0) { 23799 sd_pm_exit(un); 23800 if (rval == EIO) 23801 sd_ssc_assessment(ssc, 23802 SD_FMT_STATUS_CHECK); 23803 else 23804 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23805 mutex_enter(SD_MUTEX(un)); 23806 goto done; 23807 } 23808 } else { 23809 rval = EIO; 23810 mutex_enter(SD_MUTEX(un)); 23811 goto done; 23812 } 23813 mutex_enter(SD_MUTEX(un)); 23814 23815 sd_update_block_info(un, lbasize, capacity); 23816 23817 /* 23818 * Check if the media in the device is writable or not 23819 */ 23820 if (ISCD(un)) { 23821 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 23822 } 23823 23824 mutex_exit(SD_MUTEX(un)); 23825 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 23826 if ((cmlb_validate(un->un_cmlbhandle, 0, 23827 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 23828 sd_set_pstats(un); 23829 SD_TRACE(SD_LOG_IO_PARTITION, un, 23830 "sd_check_media: un:0x%p pstats created and " 23831 "set\n", un); 23832 } 23833 23834 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 23835 SD_PATH_DIRECT); 23836 23837 sd_pm_exit(un); 23838 23839 if (rval != 0) { 23840 if (rval == EIO) 23841 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23842 else 23843 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23844 } 23845 23846 mutex_enter(SD_MUTEX(un)); 23847 } 23848 done: 23849 sd_ssc_fini(ssc); 23850 un->un_f_watcht_stopped = FALSE; 23851 if (token != NULL && un->un_swr_token != NULL) { 23852 /* 23853 * Use of this local token and the mutex ensures that we avoid 23854 * some race conditions associated with terminating the 23855 * scsi watch. 23856 */ 23857 token = un->un_swr_token; 23858 mutex_exit(SD_MUTEX(un)); 23859 (void) scsi_watch_request_terminate(token, 23860 SCSI_WATCH_TERMINATE_WAIT); 23861 if (scsi_watch_get_ref_count(token) == 0) { 23862 mutex_enter(SD_MUTEX(un)); 23863 un->un_swr_token = (opaque_t)NULL; 23864 } else { 23865 mutex_enter(SD_MUTEX(un)); 23866 } 23867 } 23868 23869 /* 23870 * Update the capacity kstat value, if no media previously 23871 * (capacity kstat is 0) and a media has been inserted 23872 * (un_f_blockcount_is_valid == TRUE) 23873 */ 23874 if (un->un_errstats) { 23875 struct sd_errstats *stp = NULL; 23876 23877 stp = (struct sd_errstats *)un->un_errstats->ks_data; 23878 if ((stp->sd_capacity.value.ui64 == 0) && 23879 (un->un_f_blockcount_is_valid == TRUE)) { 23880 stp->sd_capacity.value.ui64 = 23881 (uint64_t)((uint64_t)un->un_blockcount * 23882 un->un_sys_blocksize); 23883 } 23884 } 23885 mutex_exit(SD_MUTEX(un)); 23886 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 23887 return (rval); 23888 } 23889 23890 23891 /* 23892 * Function: sd_delayed_cv_broadcast 23893 * 23894 * Description: Delayed cv_broadcast to allow for target to recover from media 23895 * insertion. 23896 * 23897 * Arguments: arg - driver soft state (unit) structure 23898 */ 23899 23900 static void 23901 sd_delayed_cv_broadcast(void *arg) 23902 { 23903 struct sd_lun *un = arg; 23904 23905 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 23906 23907 mutex_enter(SD_MUTEX(un)); 23908 un->un_dcvb_timeid = NULL; 23909 cv_broadcast(&un->un_state_cv); 23910 mutex_exit(SD_MUTEX(un)); 23911 } 23912 23913 23914 /* 23915 * Function: sd_media_watch_cb 23916 * 23917 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 23918 * routine processes the TUR sense data and updates the driver 23919 * state if a transition has occurred. The user thread 23920 * (sd_check_media) is then signalled. 23921 * 23922 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23923 * among multiple watches that share this callback function 23924 * resultp - scsi watch facility result packet containing scsi 23925 * packet, status byte and sense data 23926 * 23927 * Return Code: 0 for success, -1 for failure 23928 */ 23929 23930 static int 23931 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23932 { 23933 struct sd_lun *un; 23934 struct scsi_status *statusp = resultp->statusp; 23935 uint8_t *sensep = (uint8_t *)resultp->sensep; 23936 enum dkio_state state = DKIO_NONE; 23937 dev_t dev = (dev_t)arg; 23938 uchar_t actual_sense_length; 23939 uint8_t skey, asc, ascq; 23940 23941 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23942 return (-1); 23943 } 23944 actual_sense_length = resultp->actual_sense_length; 23945 23946 mutex_enter(SD_MUTEX(un)); 23947 SD_TRACE(SD_LOG_COMMON, un, 23948 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 23949 *((char *)statusp), (void *)sensep, actual_sense_length); 23950 23951 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 23952 un->un_mediastate = DKIO_DEV_GONE; 23953 cv_broadcast(&un->un_state_cv); 23954 mutex_exit(SD_MUTEX(un)); 23955 23956 return (0); 23957 } 23958 23959 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23960 if (sd_gesn_media_data_valid(resultp->mmc_data)) { 23961 if ((resultp->mmc_data[5] & 23962 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) { 23963 state = DKIO_INSERTED; 23964 } else { 23965 state = DKIO_EJECTED; 23966 } 23967 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) == 23968 SD_GESN_MEDIA_EVENT_EJECTREQUEST) { 23969 sd_log_eject_request_event(un, KM_NOSLEEP); 23970 } 23971 } 23972 } else if (sensep != NULL) { 23973 /* 23974 * If there was a check condition then sensep points to valid 23975 * sense data. If status was not a check condition but a 23976 * reservation or busy status then the new state is DKIO_NONE. 23977 */ 23978 skey = scsi_sense_key(sensep); 23979 asc = scsi_sense_asc(sensep); 23980 ascq = scsi_sense_ascq(sensep); 23981 23982 SD_INFO(SD_LOG_COMMON, un, 23983 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 23984 skey, asc, ascq); 23985 /* This routine only uses up to 13 bytes of sense data. */ 23986 if (actual_sense_length >= 13) { 23987 if (skey == KEY_UNIT_ATTENTION) { 23988 if (asc == 0x28) { 23989 state = DKIO_INSERTED; 23990 } 23991 } else if (skey == KEY_NOT_READY) { 23992 /* 23993 * Sense data of 02/06/00 means that the 23994 * drive could not read the media (No 23995 * reference position found). In this case 23996 * to prevent a hang on the DKIOCSTATE IOCTL 23997 * we set the media state to DKIO_INSERTED. 23998 */ 23999 if (asc == 0x06 && ascq == 0x00) 24000 state = DKIO_INSERTED; 24001 24002 /* 24003 * if 02/04/02 means that the host 24004 * should send start command. Explicitly 24005 * leave the media state as is 24006 * (inserted) as the media is inserted 24007 * and host has stopped device for PM 24008 * reasons. Upon next true read/write 24009 * to this media will bring the 24010 * device to the right state good for 24011 * media access. 24012 */ 24013 if (asc == 0x3a) { 24014 state = DKIO_EJECTED; 24015 } else { 24016 /* 24017 * If the drive is busy with an 24018 * operation or long write, keep the 24019 * media in an inserted state. 24020 */ 24021 24022 if ((asc == 0x04) && 24023 ((ascq == 0x02) || 24024 (ascq == 0x07) || 24025 (ascq == 0x08))) { 24026 state = DKIO_INSERTED; 24027 } 24028 } 24029 } else if (skey == KEY_NO_SENSE) { 24030 if ((asc == 0x00) && (ascq == 0x00)) { 24031 /* 24032 * Sense Data 00/00/00 does not provide 24033 * any information about the state of 24034 * the media. Ignore it. 24035 */ 24036 mutex_exit(SD_MUTEX(un)); 24037 return (0); 24038 } 24039 } 24040 } 24041 } else if ((*((char *)statusp) == STATUS_GOOD) && 24042 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24043 state = DKIO_INSERTED; 24044 } 24045 24046 SD_TRACE(SD_LOG_COMMON, un, 24047 "sd_media_watch_cb: state=%x, specified=%x\n", 24048 state, un->un_specified_mediastate); 24049 24050 /* 24051 * now signal the waiting thread if this is *not* the specified state; 24052 * delay the signal if the state is DKIO_INSERTED to allow the target 24053 * to recover 24054 */ 24055 if (state != un->un_specified_mediastate) { 24056 un->un_mediastate = state; 24057 if (state == DKIO_INSERTED) { 24058 /* 24059 * delay the signal to give the drive a chance 24060 * to do what it apparently needs to do 24061 */ 24062 SD_TRACE(SD_LOG_COMMON, un, 24063 "sd_media_watch_cb: delayed cv_broadcast\n"); 24064 if (un->un_dcvb_timeid == NULL) { 24065 un->un_dcvb_timeid = 24066 timeout(sd_delayed_cv_broadcast, un, 24067 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24068 } 24069 } else { 24070 SD_TRACE(SD_LOG_COMMON, un, 24071 "sd_media_watch_cb: immediate cv_broadcast\n"); 24072 cv_broadcast(&un->un_state_cv); 24073 } 24074 } 24075 mutex_exit(SD_MUTEX(un)); 24076 return (0); 24077 } 24078 24079 24080 /* 24081 * Function: sd_dkio_get_temp 24082 * 24083 * Description: This routine is the driver entry point for handling ioctl 24084 * requests to get the disk temperature. 24085 * 24086 * Arguments: dev - the device number 24087 * arg - pointer to user provided dk_temperature structure. 24088 * flag - this argument is a pass through to ddi_copyxxx() 24089 * directly from the mode argument of ioctl(). 24090 * 24091 * Return Code: 0 24092 * EFAULT 24093 * ENXIO 24094 * EAGAIN 24095 */ 24096 24097 static int 24098 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24099 { 24100 struct sd_lun *un = NULL; 24101 struct dk_temperature *dktemp = NULL; 24102 uchar_t *temperature_page; 24103 int rval = 0; 24104 int path_flag = SD_PATH_STANDARD; 24105 sd_ssc_t *ssc; 24106 24107 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24108 return (ENXIO); 24109 } 24110 24111 ssc = sd_ssc_init(un); 24112 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24113 24114 /* copyin the disk temp argument to get the user flags */ 24115 if (ddi_copyin((void *)arg, dktemp, 24116 sizeof (struct dk_temperature), flag) != 0) { 24117 rval = EFAULT; 24118 goto done; 24119 } 24120 24121 /* Initialize the temperature to invalid. */ 24122 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24123 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24124 24125 /* 24126 * Note: Investigate removing the "bypass pm" semantic. 24127 * Can we just bypass PM always? 24128 */ 24129 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24130 path_flag = SD_PATH_DIRECT; 24131 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24132 mutex_enter(&un->un_pm_mutex); 24133 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24134 /* 24135 * If DKT_BYPASS_PM is set, and the drive happens to be 24136 * in low power mode, we can not wake it up, Need to 24137 * return EAGAIN. 24138 */ 24139 mutex_exit(&un->un_pm_mutex); 24140 rval = EAGAIN; 24141 goto done; 24142 } else { 24143 /* 24144 * Indicate to PM the device is busy. This is required 24145 * to avoid a race - i.e. the ioctl is issuing a 24146 * command and the pm framework brings down the device 24147 * to low power mode (possible power cut-off on some 24148 * platforms). 24149 */ 24150 mutex_exit(&un->un_pm_mutex); 24151 if (sd_pm_entry(un) != DDI_SUCCESS) { 24152 rval = EAGAIN; 24153 goto done; 24154 } 24155 } 24156 } 24157 24158 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24159 24160 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page, 24161 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag); 24162 if (rval != 0) 24163 goto done2; 24164 24165 /* 24166 * For the current temperature verify that the parameter length is 0x02 24167 * and the parameter code is 0x00 24168 */ 24169 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24170 (temperature_page[5] == 0x00)) { 24171 if (temperature_page[9] == 0xFF) { 24172 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24173 } else { 24174 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24175 } 24176 } 24177 24178 /* 24179 * For the reference temperature verify that the parameter 24180 * length is 0x02 and the parameter code is 0x01 24181 */ 24182 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24183 (temperature_page[11] == 0x01)) { 24184 if (temperature_page[15] == 0xFF) { 24185 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24186 } else { 24187 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24188 } 24189 } 24190 24191 /* Do the copyout regardless of the temperature commands status. */ 24192 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24193 flag) != 0) { 24194 rval = EFAULT; 24195 goto done1; 24196 } 24197 24198 done2: 24199 if (rval != 0) { 24200 if (rval == EIO) 24201 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24202 else 24203 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24204 } 24205 done1: 24206 if (path_flag == SD_PATH_DIRECT) { 24207 sd_pm_exit(un); 24208 } 24209 24210 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24211 done: 24212 sd_ssc_fini(ssc); 24213 if (dktemp != NULL) { 24214 kmem_free(dktemp, sizeof (struct dk_temperature)); 24215 } 24216 24217 return (rval); 24218 } 24219 24220 24221 /* 24222 * Function: sd_log_page_supported 24223 * 24224 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24225 * supported log pages. 24226 * 24227 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 24228 * structure for this target. 24229 * log_page - 24230 * 24231 * Return Code: -1 - on error (log sense is optional and may not be supported). 24232 * 0 - log page not found. 24233 * 1 - log page found. 24234 */ 24235 24236 static int 24237 sd_log_page_supported(sd_ssc_t *ssc, int log_page) 24238 { 24239 uchar_t *log_page_data; 24240 int i; 24241 int match = 0; 24242 int log_size; 24243 int status = 0; 24244 struct sd_lun *un; 24245 24246 ASSERT(ssc != NULL); 24247 un = ssc->ssc_un; 24248 ASSERT(un != NULL); 24249 24250 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24251 24252 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0, 24253 SD_PATH_DIRECT); 24254 24255 if (status != 0) { 24256 if (status == EIO) { 24257 /* 24258 * Some disks do not support log sense, we 24259 * should ignore this kind of error(sense key is 24260 * 0x5 - illegal request). 24261 */ 24262 uint8_t *sensep; 24263 int senlen; 24264 24265 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 24266 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 24267 ssc->ssc_uscsi_cmd->uscsi_rqresid); 24268 24269 if (senlen > 0 && 24270 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 24271 sd_ssc_assessment(ssc, 24272 SD_FMT_IGNORE_COMPROMISE); 24273 } else { 24274 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24275 } 24276 } else { 24277 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24278 } 24279 24280 SD_ERROR(SD_LOG_COMMON, un, 24281 "sd_log_page_supported: failed log page retrieval\n"); 24282 kmem_free(log_page_data, 0xFF); 24283 return (-1); 24284 } 24285 24286 log_size = log_page_data[3]; 24287 24288 /* 24289 * The list of supported log pages start from the fourth byte. Check 24290 * until we run out of log pages or a match is found. 24291 */ 24292 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24293 if (log_page_data[i] == log_page) { 24294 match++; 24295 } 24296 } 24297 kmem_free(log_page_data, 0xFF); 24298 return (match); 24299 } 24300 24301 24302 /* 24303 * Function: sd_mhdioc_failfast 24304 * 24305 * Description: This routine is the driver entry point for handling ioctl 24306 * requests to enable/disable the multihost failfast option. 24307 * (MHIOCENFAILFAST) 24308 * 24309 * Arguments: dev - the device number 24310 * arg - user specified probing interval. 24311 * flag - this argument is a pass through to ddi_copyxxx() 24312 * directly from the mode argument of ioctl(). 24313 * 24314 * Return Code: 0 24315 * EFAULT 24316 * ENXIO 24317 */ 24318 24319 static int 24320 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24321 { 24322 struct sd_lun *un = NULL; 24323 int mh_time; 24324 int rval = 0; 24325 24326 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24327 return (ENXIO); 24328 } 24329 24330 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24331 return (EFAULT); 24332 24333 if (mh_time) { 24334 mutex_enter(SD_MUTEX(un)); 24335 un->un_resvd_status |= SD_FAILFAST; 24336 mutex_exit(SD_MUTEX(un)); 24337 /* 24338 * If mh_time is INT_MAX, then this ioctl is being used for 24339 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24340 */ 24341 if (mh_time != INT_MAX) { 24342 rval = sd_check_mhd(dev, mh_time); 24343 } 24344 } else { 24345 (void) sd_check_mhd(dev, 0); 24346 mutex_enter(SD_MUTEX(un)); 24347 un->un_resvd_status &= ~SD_FAILFAST; 24348 mutex_exit(SD_MUTEX(un)); 24349 } 24350 return (rval); 24351 } 24352 24353 24354 /* 24355 * Function: sd_mhdioc_takeown 24356 * 24357 * Description: This routine is the driver entry point for handling ioctl 24358 * requests to forcefully acquire exclusive access rights to the 24359 * multihost disk (MHIOCTKOWN). 24360 * 24361 * Arguments: dev - the device number 24362 * arg - user provided structure specifying the delay 24363 * parameters in milliseconds 24364 * flag - this argument is a pass through to ddi_copyxxx() 24365 * directly from the mode argument of ioctl(). 24366 * 24367 * Return Code: 0 24368 * EFAULT 24369 * ENXIO 24370 */ 24371 24372 static int 24373 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24374 { 24375 struct sd_lun *un = NULL; 24376 struct mhioctkown *tkown = NULL; 24377 int rval = 0; 24378 24379 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24380 return (ENXIO); 24381 } 24382 24383 if (arg != NULL) { 24384 tkown = (struct mhioctkown *) 24385 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24386 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24387 if (rval != 0) { 24388 rval = EFAULT; 24389 goto error; 24390 } 24391 } 24392 24393 rval = sd_take_ownership(dev, tkown); 24394 mutex_enter(SD_MUTEX(un)); 24395 if (rval == 0) { 24396 un->un_resvd_status |= SD_RESERVE; 24397 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24398 sd_reinstate_resv_delay = 24399 tkown->reinstate_resv_delay * 1000; 24400 } else { 24401 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24402 } 24403 /* 24404 * Give the scsi_watch routine interval set by 24405 * the MHIOCENFAILFAST ioctl precedence here. 24406 */ 24407 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24408 mutex_exit(SD_MUTEX(un)); 24409 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24410 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24411 "sd_mhdioc_takeown : %d\n", 24412 sd_reinstate_resv_delay); 24413 } else { 24414 mutex_exit(SD_MUTEX(un)); 24415 } 24416 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24417 sd_mhd_reset_notify_cb, (caddr_t)un); 24418 } else { 24419 un->un_resvd_status &= ~SD_RESERVE; 24420 mutex_exit(SD_MUTEX(un)); 24421 } 24422 24423 error: 24424 if (tkown != NULL) { 24425 kmem_free(tkown, sizeof (struct mhioctkown)); 24426 } 24427 return (rval); 24428 } 24429 24430 24431 /* 24432 * Function: sd_mhdioc_release 24433 * 24434 * Description: This routine is the driver entry point for handling ioctl 24435 * requests to release exclusive access rights to the multihost 24436 * disk (MHIOCRELEASE). 24437 * 24438 * Arguments: dev - the device number 24439 * 24440 * Return Code: 0 24441 * ENXIO 24442 */ 24443 24444 static int 24445 sd_mhdioc_release(dev_t dev) 24446 { 24447 struct sd_lun *un = NULL; 24448 timeout_id_t resvd_timeid_save; 24449 int resvd_status_save; 24450 int rval = 0; 24451 24452 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24453 return (ENXIO); 24454 } 24455 24456 mutex_enter(SD_MUTEX(un)); 24457 resvd_status_save = un->un_resvd_status; 24458 un->un_resvd_status &= 24459 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24460 if (un->un_resvd_timeid) { 24461 resvd_timeid_save = un->un_resvd_timeid; 24462 un->un_resvd_timeid = NULL; 24463 mutex_exit(SD_MUTEX(un)); 24464 (void) untimeout(resvd_timeid_save); 24465 } else { 24466 mutex_exit(SD_MUTEX(un)); 24467 } 24468 24469 /* 24470 * destroy any pending timeout thread that may be attempting to 24471 * reinstate reservation on this device. 24472 */ 24473 sd_rmv_resv_reclaim_req(dev); 24474 24475 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24476 mutex_enter(SD_MUTEX(un)); 24477 if ((un->un_mhd_token) && 24478 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24479 mutex_exit(SD_MUTEX(un)); 24480 (void) sd_check_mhd(dev, 0); 24481 } else { 24482 mutex_exit(SD_MUTEX(un)); 24483 } 24484 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24485 sd_mhd_reset_notify_cb, (caddr_t)un); 24486 } else { 24487 /* 24488 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24489 */ 24490 mutex_enter(SD_MUTEX(un)); 24491 un->un_resvd_status = resvd_status_save; 24492 mutex_exit(SD_MUTEX(un)); 24493 } 24494 return (rval); 24495 } 24496 24497 24498 /* 24499 * Function: sd_mhdioc_register_devid 24500 * 24501 * Description: This routine is the driver entry point for handling ioctl 24502 * requests to register the device id (MHIOCREREGISTERDEVID). 24503 * 24504 * Note: The implementation for this ioctl has been updated to 24505 * be consistent with the original PSARC case (1999/357) 24506 * (4375899, 4241671, 4220005) 24507 * 24508 * Arguments: dev - the device number 24509 * 24510 * Return Code: 0 24511 * ENXIO 24512 */ 24513 24514 static int 24515 sd_mhdioc_register_devid(dev_t dev) 24516 { 24517 struct sd_lun *un = NULL; 24518 int rval = 0; 24519 sd_ssc_t *ssc; 24520 24521 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24522 return (ENXIO); 24523 } 24524 24525 ASSERT(!mutex_owned(SD_MUTEX(un))); 24526 24527 mutex_enter(SD_MUTEX(un)); 24528 24529 /* If a devid already exists, de-register it */ 24530 if (un->un_devid != NULL) { 24531 ddi_devid_unregister(SD_DEVINFO(un)); 24532 /* 24533 * After unregister devid, needs to free devid memory 24534 */ 24535 ddi_devid_free(un->un_devid); 24536 un->un_devid = NULL; 24537 } 24538 24539 /* Check for reservation conflict */ 24540 mutex_exit(SD_MUTEX(un)); 24541 ssc = sd_ssc_init(un); 24542 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 24543 mutex_enter(SD_MUTEX(un)); 24544 24545 switch (rval) { 24546 case 0: 24547 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24548 break; 24549 case EACCES: 24550 break; 24551 default: 24552 rval = EIO; 24553 } 24554 24555 mutex_exit(SD_MUTEX(un)); 24556 if (rval != 0) { 24557 if (rval == EIO) 24558 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24559 else 24560 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24561 } 24562 sd_ssc_fini(ssc); 24563 return (rval); 24564 } 24565 24566 24567 /* 24568 * Function: sd_mhdioc_inkeys 24569 * 24570 * Description: This routine is the driver entry point for handling ioctl 24571 * requests to issue the SCSI-3 Persistent In Read Keys command 24572 * to the device (MHIOCGRP_INKEYS). 24573 * 24574 * Arguments: dev - the device number 24575 * arg - user provided in_keys structure 24576 * flag - this argument is a pass through to ddi_copyxxx() 24577 * directly from the mode argument of ioctl(). 24578 * 24579 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24580 * ENXIO 24581 * EFAULT 24582 */ 24583 24584 static int 24585 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24586 { 24587 struct sd_lun *un; 24588 mhioc_inkeys_t inkeys; 24589 int rval = 0; 24590 24591 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24592 return (ENXIO); 24593 } 24594 24595 #ifdef _MULTI_DATAMODEL 24596 switch (ddi_model_convert_from(flag & FMODELS)) { 24597 case DDI_MODEL_ILP32: { 24598 struct mhioc_inkeys32 inkeys32; 24599 24600 if (ddi_copyin(arg, &inkeys32, 24601 sizeof (struct mhioc_inkeys32), flag) != 0) { 24602 return (EFAULT); 24603 } 24604 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24605 if ((rval = sd_persistent_reservation_in_read_keys(un, 24606 &inkeys, flag)) != 0) { 24607 return (rval); 24608 } 24609 inkeys32.generation = inkeys.generation; 24610 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24611 flag) != 0) { 24612 return (EFAULT); 24613 } 24614 break; 24615 } 24616 case DDI_MODEL_NONE: 24617 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24618 flag) != 0) { 24619 return (EFAULT); 24620 } 24621 if ((rval = sd_persistent_reservation_in_read_keys(un, 24622 &inkeys, flag)) != 0) { 24623 return (rval); 24624 } 24625 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24626 flag) != 0) { 24627 return (EFAULT); 24628 } 24629 break; 24630 } 24631 24632 #else /* ! _MULTI_DATAMODEL */ 24633 24634 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24635 return (EFAULT); 24636 } 24637 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24638 if (rval != 0) { 24639 return (rval); 24640 } 24641 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24642 return (EFAULT); 24643 } 24644 24645 #endif /* _MULTI_DATAMODEL */ 24646 24647 return (rval); 24648 } 24649 24650 24651 /* 24652 * Function: sd_mhdioc_inresv 24653 * 24654 * Description: This routine is the driver entry point for handling ioctl 24655 * requests to issue the SCSI-3 Persistent In Read Reservations 24656 * command to the device (MHIOCGRP_INKEYS). 24657 * 24658 * Arguments: dev - the device number 24659 * arg - user provided in_resv structure 24660 * flag - this argument is a pass through to ddi_copyxxx() 24661 * directly from the mode argument of ioctl(). 24662 * 24663 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24664 * ENXIO 24665 * EFAULT 24666 */ 24667 24668 static int 24669 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24670 { 24671 struct sd_lun *un; 24672 mhioc_inresvs_t inresvs; 24673 int rval = 0; 24674 24675 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24676 return (ENXIO); 24677 } 24678 24679 #ifdef _MULTI_DATAMODEL 24680 24681 switch (ddi_model_convert_from(flag & FMODELS)) { 24682 case DDI_MODEL_ILP32: { 24683 struct mhioc_inresvs32 inresvs32; 24684 24685 if (ddi_copyin(arg, &inresvs32, 24686 sizeof (struct mhioc_inresvs32), flag) != 0) { 24687 return (EFAULT); 24688 } 24689 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24690 if ((rval = sd_persistent_reservation_in_read_resv(un, 24691 &inresvs, flag)) != 0) { 24692 return (rval); 24693 } 24694 inresvs32.generation = inresvs.generation; 24695 if (ddi_copyout(&inresvs32, arg, 24696 sizeof (struct mhioc_inresvs32), flag) != 0) { 24697 return (EFAULT); 24698 } 24699 break; 24700 } 24701 case DDI_MODEL_NONE: 24702 if (ddi_copyin(arg, &inresvs, 24703 sizeof (mhioc_inresvs_t), flag) != 0) { 24704 return (EFAULT); 24705 } 24706 if ((rval = sd_persistent_reservation_in_read_resv(un, 24707 &inresvs, flag)) != 0) { 24708 return (rval); 24709 } 24710 if (ddi_copyout(&inresvs, arg, 24711 sizeof (mhioc_inresvs_t), flag) != 0) { 24712 return (EFAULT); 24713 } 24714 break; 24715 } 24716 24717 #else /* ! _MULTI_DATAMODEL */ 24718 24719 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24720 return (EFAULT); 24721 } 24722 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24723 if (rval != 0) { 24724 return (rval); 24725 } 24726 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24727 return (EFAULT); 24728 } 24729 24730 #endif /* ! _MULTI_DATAMODEL */ 24731 24732 return (rval); 24733 } 24734 24735 24736 /* 24737 * The following routines support the clustering functionality described below 24738 * and implement lost reservation reclaim functionality. 24739 * 24740 * Clustering 24741 * ---------- 24742 * The clustering code uses two different, independent forms of SCSI 24743 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24744 * Persistent Group Reservations. For any particular disk, it will use either 24745 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24746 * 24747 * SCSI-2 24748 * The cluster software takes ownership of a multi-hosted disk by issuing the 24749 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24750 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 24751 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 24752 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 24753 * driver. The meaning of failfast is that if the driver (on this host) ever 24754 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 24755 * it should immediately panic the host. The motivation for this ioctl is that 24756 * if this host does encounter reservation conflict, the underlying cause is 24757 * that some other host of the cluster has decided that this host is no longer 24758 * in the cluster and has seized control of the disks for itself. Since this 24759 * host is no longer in the cluster, it ought to panic itself. The 24760 * MHIOCENFAILFAST ioctl does two things: 24761 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24762 * error to panic the host 24763 * (b) it sets up a periodic timer to test whether this host still has 24764 * "access" (in that no other host has reserved the device): if the 24765 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24766 * purpose of that periodic timer is to handle scenarios where the host is 24767 * otherwise temporarily quiescent, temporarily doing no real i/o. 24768 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24769 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24770 * the device itself. 24771 * 24772 * SCSI-3 PGR 24773 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24774 * facility is supported through the shared multihost disk ioctls 24775 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24776 * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR) 24777 * 24778 * Reservation Reclaim: 24779 * -------------------- 24780 * To support the lost reservation reclaim operations this driver creates a 24781 * single thread to handle reinstating reservations on all devices that have 24782 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24783 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24784 * and the reservation reclaim thread loops through the requests to regain the 24785 * lost reservations. 24786 */ 24787 24788 /* 24789 * Function: sd_check_mhd() 24790 * 24791 * Description: This function sets up and submits a scsi watch request or 24792 * terminates an existing watch request. This routine is used in 24793 * support of reservation reclaim. 24794 * 24795 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24796 * among multiple watches that share the callback function 24797 * interval - the number of microseconds specifying the watch 24798 * interval for issuing TEST UNIT READY commands. If 24799 * set to 0 the watch should be terminated. If the 24800 * interval is set to 0 and if the device is required 24801 * to hold reservation while disabling failfast, the 24802 * watch is restarted with an interval of 24803 * reinstate_resv_delay. 24804 * 24805 * Return Code: 0 - Successful submit/terminate of scsi watch request 24806 * ENXIO - Indicates an invalid device was specified 24807 * EAGAIN - Unable to submit the scsi watch request 24808 */ 24809 24810 static int 24811 sd_check_mhd(dev_t dev, int interval) 24812 { 24813 struct sd_lun *un; 24814 opaque_t token; 24815 24816 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24817 return (ENXIO); 24818 } 24819 24820 /* is this a watch termination request? */ 24821 if (interval == 0) { 24822 mutex_enter(SD_MUTEX(un)); 24823 /* if there is an existing watch task then terminate it */ 24824 if (un->un_mhd_token) { 24825 token = un->un_mhd_token; 24826 un->un_mhd_token = NULL; 24827 mutex_exit(SD_MUTEX(un)); 24828 (void) scsi_watch_request_terminate(token, 24829 SCSI_WATCH_TERMINATE_ALL_WAIT); 24830 mutex_enter(SD_MUTEX(un)); 24831 } else { 24832 mutex_exit(SD_MUTEX(un)); 24833 /* 24834 * Note: If we return here we don't check for the 24835 * failfast case. This is the original legacy 24836 * implementation but perhaps we should be checking 24837 * the failfast case. 24838 */ 24839 return (0); 24840 } 24841 /* 24842 * If the device is required to hold reservation while 24843 * disabling failfast, we need to restart the scsi_watch 24844 * routine with an interval of reinstate_resv_delay. 24845 */ 24846 if (un->un_resvd_status & SD_RESERVE) { 24847 interval = sd_reinstate_resv_delay/1000; 24848 } else { 24849 /* no failfast so bail */ 24850 mutex_exit(SD_MUTEX(un)); 24851 return (0); 24852 } 24853 mutex_exit(SD_MUTEX(un)); 24854 } 24855 24856 /* 24857 * adjust minimum time interval to 1 second, 24858 * and convert from msecs to usecs 24859 */ 24860 if (interval > 0 && interval < 1000) { 24861 interval = 1000; 24862 } 24863 interval *= 1000; 24864 24865 /* 24866 * submit the request to the scsi_watch service 24867 */ 24868 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24869 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24870 if (token == NULL) { 24871 return (EAGAIN); 24872 } 24873 24874 /* 24875 * save token for termination later on 24876 */ 24877 mutex_enter(SD_MUTEX(un)); 24878 un->un_mhd_token = token; 24879 mutex_exit(SD_MUTEX(un)); 24880 return (0); 24881 } 24882 24883 24884 /* 24885 * Function: sd_mhd_watch_cb() 24886 * 24887 * Description: This function is the call back function used by the scsi watch 24888 * facility. The scsi watch facility sends the "Test Unit Ready" 24889 * and processes the status. If applicable (i.e. a "Unit Attention" 24890 * status and automatic "Request Sense" not used) the scsi watch 24891 * facility will send a "Request Sense" and retrieve the sense data 24892 * to be passed to this callback function. In either case the 24893 * automatic "Request Sense" or the facility submitting one, this 24894 * callback is passed the status and sense data. 24895 * 24896 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24897 * among multiple watches that share this callback function 24898 * resultp - scsi watch facility result packet containing scsi 24899 * packet, status byte and sense data 24900 * 24901 * Return Code: 0 - continue the watch task 24902 * non-zero - terminate the watch task 24903 */ 24904 24905 static int 24906 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24907 { 24908 struct sd_lun *un; 24909 struct scsi_status *statusp; 24910 uint8_t *sensep; 24911 struct scsi_pkt *pkt; 24912 uchar_t actual_sense_length; 24913 dev_t dev = (dev_t)arg; 24914 24915 ASSERT(resultp != NULL); 24916 statusp = resultp->statusp; 24917 sensep = (uint8_t *)resultp->sensep; 24918 pkt = resultp->pkt; 24919 actual_sense_length = resultp->actual_sense_length; 24920 24921 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24922 return (ENXIO); 24923 } 24924 24925 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24926 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 24927 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 24928 24929 /* Begin processing of the status and/or sense data */ 24930 if (pkt->pkt_reason != CMD_CMPLT) { 24931 /* Handle the incomplete packet */ 24932 sd_mhd_watch_incomplete(un, pkt); 24933 return (0); 24934 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 24935 if (*((unsigned char *)statusp) 24936 == STATUS_RESERVATION_CONFLICT) { 24937 /* 24938 * Handle a reservation conflict by panicking if 24939 * configured for failfast or by logging the conflict 24940 * and updating the reservation status 24941 */ 24942 mutex_enter(SD_MUTEX(un)); 24943 if ((un->un_resvd_status & SD_FAILFAST) && 24944 (sd_failfast_enable)) { 24945 sd_panic_for_res_conflict(un); 24946 /*NOTREACHED*/ 24947 } 24948 SD_INFO(SD_LOG_IOCTL_MHD, un, 24949 "sd_mhd_watch_cb: Reservation Conflict\n"); 24950 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 24951 mutex_exit(SD_MUTEX(un)); 24952 } 24953 } 24954 24955 if (sensep != NULL) { 24956 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 24957 mutex_enter(SD_MUTEX(un)); 24958 if ((scsi_sense_asc(sensep) == 24959 SD_SCSI_RESET_SENSE_CODE) && 24960 (un->un_resvd_status & SD_RESERVE)) { 24961 /* 24962 * The additional sense code indicates a power 24963 * on or bus device reset has occurred; update 24964 * the reservation status. 24965 */ 24966 un->un_resvd_status |= 24967 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24968 SD_INFO(SD_LOG_IOCTL_MHD, un, 24969 "sd_mhd_watch_cb: Lost Reservation\n"); 24970 } 24971 } else { 24972 return (0); 24973 } 24974 } else { 24975 mutex_enter(SD_MUTEX(un)); 24976 } 24977 24978 if ((un->un_resvd_status & SD_RESERVE) && 24979 (un->un_resvd_status & SD_LOST_RESERVE)) { 24980 if (un->un_resvd_status & SD_WANT_RESERVE) { 24981 /* 24982 * A reset occurred in between the last probe and this 24983 * one so if a timeout is pending cancel it. 24984 */ 24985 if (un->un_resvd_timeid) { 24986 timeout_id_t temp_id = un->un_resvd_timeid; 24987 un->un_resvd_timeid = NULL; 24988 mutex_exit(SD_MUTEX(un)); 24989 (void) untimeout(temp_id); 24990 mutex_enter(SD_MUTEX(un)); 24991 } 24992 un->un_resvd_status &= ~SD_WANT_RESERVE; 24993 } 24994 if (un->un_resvd_timeid == 0) { 24995 /* Schedule a timeout to handle the lost reservation */ 24996 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 24997 (void *)dev, 24998 drv_usectohz(sd_reinstate_resv_delay)); 24999 } 25000 } 25001 mutex_exit(SD_MUTEX(un)); 25002 return (0); 25003 } 25004 25005 25006 /* 25007 * Function: sd_mhd_watch_incomplete() 25008 * 25009 * Description: This function is used to find out why a scsi pkt sent by the 25010 * scsi watch facility was not completed. Under some scenarios this 25011 * routine will return. Otherwise it will send a bus reset to see 25012 * if the drive is still online. 25013 * 25014 * Arguments: un - driver soft state (unit) structure 25015 * pkt - incomplete scsi pkt 25016 */ 25017 25018 static void 25019 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25020 { 25021 int be_chatty; 25022 int perr; 25023 25024 ASSERT(pkt != NULL); 25025 ASSERT(un != NULL); 25026 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25027 perr = (pkt->pkt_statistics & STAT_PERR); 25028 25029 mutex_enter(SD_MUTEX(un)); 25030 if (un->un_state == SD_STATE_DUMPING) { 25031 mutex_exit(SD_MUTEX(un)); 25032 return; 25033 } 25034 25035 switch (pkt->pkt_reason) { 25036 case CMD_UNX_BUS_FREE: 25037 /* 25038 * If we had a parity error that caused the target to drop BSY*, 25039 * don't be chatty about it. 25040 */ 25041 if (perr && be_chatty) { 25042 be_chatty = 0; 25043 } 25044 break; 25045 case CMD_TAG_REJECT: 25046 /* 25047 * The SCSI-2 spec states that a tag reject will be sent by the 25048 * target if tagged queuing is not supported. A tag reject may 25049 * also be sent during certain initialization periods or to 25050 * control internal resources. For the latter case the target 25051 * may also return Queue Full. 25052 * 25053 * If this driver receives a tag reject from a target that is 25054 * going through an init period or controlling internal 25055 * resources tagged queuing will be disabled. This is a less 25056 * than optimal behavior but the driver is unable to determine 25057 * the target state and assumes tagged queueing is not supported 25058 */ 25059 pkt->pkt_flags = 0; 25060 un->un_tagflags = 0; 25061 25062 if (un->un_f_opt_queueing == TRUE) { 25063 un->un_throttle = min(un->un_throttle, 3); 25064 } else { 25065 un->un_throttle = 1; 25066 } 25067 mutex_exit(SD_MUTEX(un)); 25068 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25069 mutex_enter(SD_MUTEX(un)); 25070 break; 25071 case CMD_INCOMPLETE: 25072 /* 25073 * The transport stopped with an abnormal state, fallthrough and 25074 * reset the target and/or bus unless selection did not complete 25075 * (indicated by STATE_GOT_BUS) in which case we don't want to 25076 * go through a target/bus reset 25077 */ 25078 if (pkt->pkt_state == STATE_GOT_BUS) { 25079 break; 25080 } 25081 /*FALLTHROUGH*/ 25082 25083 case CMD_TIMEOUT: 25084 default: 25085 /* 25086 * The lun may still be running the command, so a lun reset 25087 * should be attempted. If the lun reset fails or cannot be 25088 * issued, than try a target reset. Lastly try a bus reset. 25089 */ 25090 if ((pkt->pkt_statistics & 25091 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25092 int reset_retval = 0; 25093 mutex_exit(SD_MUTEX(un)); 25094 if (un->un_f_allow_bus_device_reset == TRUE) { 25095 if (un->un_f_lun_reset_enabled == TRUE) { 25096 reset_retval = 25097 scsi_reset(SD_ADDRESS(un), 25098 RESET_LUN); 25099 } 25100 if (reset_retval == 0) { 25101 reset_retval = 25102 scsi_reset(SD_ADDRESS(un), 25103 RESET_TARGET); 25104 } 25105 } 25106 if (reset_retval == 0) { 25107 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25108 } 25109 mutex_enter(SD_MUTEX(un)); 25110 } 25111 break; 25112 } 25113 25114 /* A device/bus reset has occurred; update the reservation status. */ 25115 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25116 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25117 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25118 un->un_resvd_status |= 25119 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25120 SD_INFO(SD_LOG_IOCTL_MHD, un, 25121 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25122 } 25123 } 25124 25125 /* 25126 * The disk has been turned off; Update the device state. 25127 * 25128 * Note: Should we be offlining the disk here? 25129 */ 25130 if (pkt->pkt_state == STATE_GOT_BUS) { 25131 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25132 "Disk not responding to selection\n"); 25133 if (un->un_state != SD_STATE_OFFLINE) { 25134 New_state(un, SD_STATE_OFFLINE); 25135 } 25136 } else if (be_chatty) { 25137 /* 25138 * suppress messages if they are all the same pkt reason; 25139 * with TQ, many (up to 256) are returned with the same 25140 * pkt_reason 25141 */ 25142 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25143 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25144 "sd_mhd_watch_incomplete: " 25145 "SCSI transport failed: reason '%s'\n", 25146 scsi_rname(pkt->pkt_reason)); 25147 } 25148 } 25149 un->un_last_pkt_reason = pkt->pkt_reason; 25150 mutex_exit(SD_MUTEX(un)); 25151 } 25152 25153 25154 /* 25155 * Function: sd_sname() 25156 * 25157 * Description: This is a simple little routine to return a string containing 25158 * a printable description of command status byte for use in 25159 * logging. 25160 * 25161 * Arguments: status - pointer to a status byte 25162 * 25163 * Return Code: char * - string containing status description. 25164 */ 25165 25166 static char * 25167 sd_sname(uchar_t status) 25168 { 25169 switch (status & STATUS_MASK) { 25170 case STATUS_GOOD: 25171 return ("good status"); 25172 case STATUS_CHECK: 25173 return ("check condition"); 25174 case STATUS_MET: 25175 return ("condition met"); 25176 case STATUS_BUSY: 25177 return ("busy"); 25178 case STATUS_INTERMEDIATE: 25179 return ("intermediate"); 25180 case STATUS_INTERMEDIATE_MET: 25181 return ("intermediate - condition met"); 25182 case STATUS_RESERVATION_CONFLICT: 25183 return ("reservation_conflict"); 25184 case STATUS_TERMINATED: 25185 return ("command terminated"); 25186 case STATUS_QFULL: 25187 return ("queue full"); 25188 default: 25189 return ("<unknown status>"); 25190 } 25191 } 25192 25193 25194 /* 25195 * Function: sd_mhd_resvd_recover() 25196 * 25197 * Description: This function adds a reservation entry to the 25198 * sd_resv_reclaim_request list and signals the reservation 25199 * reclaim thread that there is work pending. If the reservation 25200 * reclaim thread has not been previously created this function 25201 * will kick it off. 25202 * 25203 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25204 * among multiple watches that share this callback function 25205 * 25206 * Context: This routine is called by timeout() and is run in interrupt 25207 * context. It must not sleep or call other functions which may 25208 * sleep. 25209 */ 25210 25211 static void 25212 sd_mhd_resvd_recover(void *arg) 25213 { 25214 dev_t dev = (dev_t)arg; 25215 struct sd_lun *un; 25216 struct sd_thr_request *sd_treq = NULL; 25217 struct sd_thr_request *sd_cur = NULL; 25218 struct sd_thr_request *sd_prev = NULL; 25219 int already_there = 0; 25220 25221 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25222 return; 25223 } 25224 25225 mutex_enter(SD_MUTEX(un)); 25226 un->un_resvd_timeid = NULL; 25227 if (un->un_resvd_status & SD_WANT_RESERVE) { 25228 /* 25229 * There was a reset so don't issue the reserve, allow the 25230 * sd_mhd_watch_cb callback function to notice this and 25231 * reschedule the timeout for reservation. 25232 */ 25233 mutex_exit(SD_MUTEX(un)); 25234 return; 25235 } 25236 mutex_exit(SD_MUTEX(un)); 25237 25238 /* 25239 * Add this device to the sd_resv_reclaim_request list and the 25240 * sd_resv_reclaim_thread should take care of the rest. 25241 * 25242 * Note: We can't sleep in this context so if the memory allocation 25243 * fails allow the sd_mhd_watch_cb callback function to notice this and 25244 * reschedule the timeout for reservation. (4378460) 25245 */ 25246 sd_treq = (struct sd_thr_request *) 25247 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25248 if (sd_treq == NULL) { 25249 return; 25250 } 25251 25252 sd_treq->sd_thr_req_next = NULL; 25253 sd_treq->dev = dev; 25254 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25255 if (sd_tr.srq_thr_req_head == NULL) { 25256 sd_tr.srq_thr_req_head = sd_treq; 25257 } else { 25258 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25259 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25260 if (sd_cur->dev == dev) { 25261 /* 25262 * already in Queue so don't log 25263 * another request for the device 25264 */ 25265 already_there = 1; 25266 break; 25267 } 25268 sd_prev = sd_cur; 25269 } 25270 if (!already_there) { 25271 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25272 "logging request for %lx\n", dev); 25273 sd_prev->sd_thr_req_next = sd_treq; 25274 } else { 25275 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25276 } 25277 } 25278 25279 /* 25280 * Create a kernel thread to do the reservation reclaim and free up this 25281 * thread. We cannot block this thread while we go away to do the 25282 * reservation reclaim 25283 */ 25284 if (sd_tr.srq_resv_reclaim_thread == NULL) 25285 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25286 sd_resv_reclaim_thread, NULL, 25287 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25288 25289 /* Tell the reservation reclaim thread that it has work to do */ 25290 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25291 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25292 } 25293 25294 /* 25295 * Function: sd_resv_reclaim_thread() 25296 * 25297 * Description: This function implements the reservation reclaim operations 25298 * 25299 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25300 * among multiple watches that share this callback function 25301 */ 25302 25303 static void 25304 sd_resv_reclaim_thread() 25305 { 25306 struct sd_lun *un; 25307 struct sd_thr_request *sd_mhreq; 25308 25309 /* Wait for work */ 25310 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25311 if (sd_tr.srq_thr_req_head == NULL) { 25312 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25313 &sd_tr.srq_resv_reclaim_mutex); 25314 } 25315 25316 /* Loop while we have work */ 25317 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25318 un = ddi_get_soft_state(sd_state, 25319 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25320 if (un == NULL) { 25321 /* 25322 * softstate structure is NULL so just 25323 * dequeue the request and continue 25324 */ 25325 sd_tr.srq_thr_req_head = 25326 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25327 kmem_free(sd_tr.srq_thr_cur_req, 25328 sizeof (struct sd_thr_request)); 25329 continue; 25330 } 25331 25332 /* dequeue the request */ 25333 sd_mhreq = sd_tr.srq_thr_cur_req; 25334 sd_tr.srq_thr_req_head = 25335 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25336 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25337 25338 /* 25339 * Reclaim reservation only if SD_RESERVE is still set. There 25340 * may have been a call to MHIOCRELEASE before we got here. 25341 */ 25342 mutex_enter(SD_MUTEX(un)); 25343 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25344 /* 25345 * Note: The SD_LOST_RESERVE flag is cleared before 25346 * reclaiming the reservation. If this is done after the 25347 * call to sd_reserve_release a reservation loss in the 25348 * window between pkt completion of reserve cmd and 25349 * mutex_enter below may not be recognized 25350 */ 25351 un->un_resvd_status &= ~SD_LOST_RESERVE; 25352 mutex_exit(SD_MUTEX(un)); 25353 25354 if (sd_reserve_release(sd_mhreq->dev, 25355 SD_RESERVE) == 0) { 25356 mutex_enter(SD_MUTEX(un)); 25357 un->un_resvd_status |= SD_RESERVE; 25358 mutex_exit(SD_MUTEX(un)); 25359 SD_INFO(SD_LOG_IOCTL_MHD, un, 25360 "sd_resv_reclaim_thread: " 25361 "Reservation Recovered\n"); 25362 } else { 25363 mutex_enter(SD_MUTEX(un)); 25364 un->un_resvd_status |= SD_LOST_RESERVE; 25365 mutex_exit(SD_MUTEX(un)); 25366 SD_INFO(SD_LOG_IOCTL_MHD, un, 25367 "sd_resv_reclaim_thread: Failed " 25368 "Reservation Recovery\n"); 25369 } 25370 } else { 25371 mutex_exit(SD_MUTEX(un)); 25372 } 25373 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25374 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25375 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25376 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25377 /* 25378 * wakeup the destroy thread if anyone is waiting on 25379 * us to complete. 25380 */ 25381 cv_signal(&sd_tr.srq_inprocess_cv); 25382 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25383 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25384 } 25385 25386 /* 25387 * cleanup the sd_tr structure now that this thread will not exist 25388 */ 25389 ASSERT(sd_tr.srq_thr_req_head == NULL); 25390 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25391 sd_tr.srq_resv_reclaim_thread = NULL; 25392 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25393 thread_exit(); 25394 } 25395 25396 25397 /* 25398 * Function: sd_rmv_resv_reclaim_req() 25399 * 25400 * Description: This function removes any pending reservation reclaim requests 25401 * for the specified device. 25402 * 25403 * Arguments: dev - the device 'dev_t' 25404 */ 25405 25406 static void 25407 sd_rmv_resv_reclaim_req(dev_t dev) 25408 { 25409 struct sd_thr_request *sd_mhreq; 25410 struct sd_thr_request *sd_prev; 25411 25412 /* Remove a reservation reclaim request from the list */ 25413 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25414 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25415 /* 25416 * We are attempting to reinstate reservation for 25417 * this device. We wait for sd_reserve_release() 25418 * to return before we return. 25419 */ 25420 cv_wait(&sd_tr.srq_inprocess_cv, 25421 &sd_tr.srq_resv_reclaim_mutex); 25422 } else { 25423 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25424 if (sd_mhreq && sd_mhreq->dev == dev) { 25425 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25426 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25427 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25428 return; 25429 } 25430 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25431 if (sd_mhreq && sd_mhreq->dev == dev) { 25432 break; 25433 } 25434 sd_prev = sd_mhreq; 25435 } 25436 if (sd_mhreq != NULL) { 25437 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25438 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25439 } 25440 } 25441 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25442 } 25443 25444 25445 /* 25446 * Function: sd_mhd_reset_notify_cb() 25447 * 25448 * Description: This is a call back function for scsi_reset_notify. This 25449 * function updates the softstate reserved status and logs the 25450 * reset. The driver scsi watch facility callback function 25451 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25452 * will reclaim the reservation. 25453 * 25454 * Arguments: arg - driver soft state (unit) structure 25455 */ 25456 25457 static void 25458 sd_mhd_reset_notify_cb(caddr_t arg) 25459 { 25460 struct sd_lun *un = (struct sd_lun *)arg; 25461 25462 mutex_enter(SD_MUTEX(un)); 25463 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25464 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25465 SD_INFO(SD_LOG_IOCTL_MHD, un, 25466 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25467 } 25468 mutex_exit(SD_MUTEX(un)); 25469 } 25470 25471 25472 /* 25473 * Function: sd_take_ownership() 25474 * 25475 * Description: This routine implements an algorithm to achieve a stable 25476 * reservation on disks which don't implement priority reserve, 25477 * and makes sure that other host lose re-reservation attempts. 25478 * This algorithm contains of a loop that keeps issuing the RESERVE 25479 * for some period of time (min_ownership_delay, default 6 seconds) 25480 * During that loop, it looks to see if there has been a bus device 25481 * reset or bus reset (both of which cause an existing reservation 25482 * to be lost). If the reservation is lost issue RESERVE until a 25483 * period of min_ownership_delay with no resets has gone by, or 25484 * until max_ownership_delay has expired. This loop ensures that 25485 * the host really did manage to reserve the device, in spite of 25486 * resets. The looping for min_ownership_delay (default six 25487 * seconds) is important to early generation clustering products, 25488 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25489 * MHIOCENFAILFAST periodic timer of two seconds. By having 25490 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25491 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25492 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25493 * have already noticed, via the MHIOCENFAILFAST polling, that it 25494 * no longer "owns" the disk and will have panicked itself. Thus, 25495 * the host issuing the MHIOCTKOWN is assured (with timing 25496 * dependencies) that by the time it actually starts to use the 25497 * disk for real work, the old owner is no longer accessing it. 25498 * 25499 * min_ownership_delay is the minimum amount of time for which the 25500 * disk must be reserved continuously devoid of resets before the 25501 * MHIOCTKOWN ioctl will return success. 25502 * 25503 * max_ownership_delay indicates the amount of time by which the 25504 * take ownership should succeed or timeout with an error. 25505 * 25506 * Arguments: dev - the device 'dev_t' 25507 * *p - struct containing timing info. 25508 * 25509 * Return Code: 0 for success or error code 25510 */ 25511 25512 static int 25513 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25514 { 25515 struct sd_lun *un; 25516 int rval; 25517 int err; 25518 int reservation_count = 0; 25519 int min_ownership_delay = 6000000; /* in usec */ 25520 int max_ownership_delay = 30000000; /* in usec */ 25521 clock_t start_time; /* starting time of this algorithm */ 25522 clock_t end_time; /* time limit for giving up */ 25523 clock_t ownership_time; /* time limit for stable ownership */ 25524 clock_t current_time; 25525 clock_t previous_current_time; 25526 25527 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25528 return (ENXIO); 25529 } 25530 25531 /* 25532 * Attempt a device reservation. A priority reservation is requested. 25533 */ 25534 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25535 != SD_SUCCESS) { 25536 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25537 "sd_take_ownership: return(1)=%d\n", rval); 25538 return (rval); 25539 } 25540 25541 /* Update the softstate reserved status to indicate the reservation */ 25542 mutex_enter(SD_MUTEX(un)); 25543 un->un_resvd_status |= SD_RESERVE; 25544 un->un_resvd_status &= 25545 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25546 mutex_exit(SD_MUTEX(un)); 25547 25548 if (p != NULL) { 25549 if (p->min_ownership_delay != 0) { 25550 min_ownership_delay = p->min_ownership_delay * 1000; 25551 } 25552 if (p->max_ownership_delay != 0) { 25553 max_ownership_delay = p->max_ownership_delay * 1000; 25554 } 25555 } 25556 SD_INFO(SD_LOG_IOCTL_MHD, un, 25557 "sd_take_ownership: min, max delays: %d, %d\n", 25558 min_ownership_delay, max_ownership_delay); 25559 25560 start_time = ddi_get_lbolt(); 25561 current_time = start_time; 25562 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25563 end_time = start_time + drv_usectohz(max_ownership_delay); 25564 25565 while (current_time - end_time < 0) { 25566 delay(drv_usectohz(500000)); 25567 25568 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25569 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25570 mutex_enter(SD_MUTEX(un)); 25571 rval = (un->un_resvd_status & 25572 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25573 mutex_exit(SD_MUTEX(un)); 25574 break; 25575 } 25576 } 25577 previous_current_time = current_time; 25578 current_time = ddi_get_lbolt(); 25579 mutex_enter(SD_MUTEX(un)); 25580 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25581 ownership_time = ddi_get_lbolt() + 25582 drv_usectohz(min_ownership_delay); 25583 reservation_count = 0; 25584 } else { 25585 reservation_count++; 25586 } 25587 un->un_resvd_status |= SD_RESERVE; 25588 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25589 mutex_exit(SD_MUTEX(un)); 25590 25591 SD_INFO(SD_LOG_IOCTL_MHD, un, 25592 "sd_take_ownership: ticks for loop iteration=%ld, " 25593 "reservation=%s\n", (current_time - previous_current_time), 25594 reservation_count ? "ok" : "reclaimed"); 25595 25596 if (current_time - ownership_time >= 0 && 25597 reservation_count >= 4) { 25598 rval = 0; /* Achieved a stable ownership */ 25599 break; 25600 } 25601 if (current_time - end_time >= 0) { 25602 rval = EACCES; /* No ownership in max possible time */ 25603 break; 25604 } 25605 } 25606 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25607 "sd_take_ownership: return(2)=%d\n", rval); 25608 return (rval); 25609 } 25610 25611 25612 /* 25613 * Function: sd_reserve_release() 25614 * 25615 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25616 * PRIORITY RESERVE commands based on a user specified command type 25617 * 25618 * Arguments: dev - the device 'dev_t' 25619 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25620 * SD_RESERVE, SD_RELEASE 25621 * 25622 * Return Code: 0 or Error Code 25623 */ 25624 25625 static int 25626 sd_reserve_release(dev_t dev, int cmd) 25627 { 25628 struct uscsi_cmd *com = NULL; 25629 struct sd_lun *un = NULL; 25630 char cdb[CDB_GROUP0]; 25631 int rval; 25632 25633 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25634 (cmd == SD_PRIORITY_RESERVE)); 25635 25636 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25637 return (ENXIO); 25638 } 25639 25640 /* instantiate and initialize the command and cdb */ 25641 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25642 bzero(cdb, CDB_GROUP0); 25643 com->uscsi_flags = USCSI_SILENT; 25644 com->uscsi_timeout = un->un_reserve_release_time; 25645 com->uscsi_cdblen = CDB_GROUP0; 25646 com->uscsi_cdb = cdb; 25647 if (cmd == SD_RELEASE) { 25648 cdb[0] = SCMD_RELEASE; 25649 } else { 25650 cdb[0] = SCMD_RESERVE; 25651 } 25652 25653 /* Send the command. */ 25654 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25655 SD_PATH_STANDARD); 25656 25657 /* 25658 * "break" a reservation that is held by another host, by issuing a 25659 * reset if priority reserve is desired, and we could not get the 25660 * device. 25661 */ 25662 if ((cmd == SD_PRIORITY_RESERVE) && 25663 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25664 /* 25665 * First try to reset the LUN. If we cannot, then try a target 25666 * reset, followed by a bus reset if the target reset fails. 25667 */ 25668 int reset_retval = 0; 25669 if (un->un_f_lun_reset_enabled == TRUE) { 25670 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25671 } 25672 if (reset_retval == 0) { 25673 /* The LUN reset either failed or was not issued */ 25674 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25675 } 25676 if ((reset_retval == 0) && 25677 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25678 rval = EIO; 25679 kmem_free(com, sizeof (*com)); 25680 return (rval); 25681 } 25682 25683 bzero(com, sizeof (struct uscsi_cmd)); 25684 com->uscsi_flags = USCSI_SILENT; 25685 com->uscsi_cdb = cdb; 25686 com->uscsi_cdblen = CDB_GROUP0; 25687 com->uscsi_timeout = 5; 25688 25689 /* 25690 * Reissue the last reserve command, this time without request 25691 * sense. Assume that it is just a regular reserve command. 25692 */ 25693 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25694 SD_PATH_STANDARD); 25695 } 25696 25697 /* Return an error if still getting a reservation conflict. */ 25698 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25699 rval = EACCES; 25700 } 25701 25702 kmem_free(com, sizeof (*com)); 25703 return (rval); 25704 } 25705 25706 25707 #define SD_NDUMP_RETRIES 12 25708 /* 25709 * System Crash Dump routine 25710 */ 25711 25712 static int 25713 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25714 { 25715 int instance; 25716 int partition; 25717 int i; 25718 int err; 25719 struct sd_lun *un; 25720 struct scsi_pkt *wr_pktp; 25721 struct buf *wr_bp; 25722 struct buf wr_buf; 25723 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25724 daddr_t tgt_blkno; /* rmw - blkno for target */ 25725 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25726 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25727 size_t io_start_offset; 25728 int doing_rmw = FALSE; 25729 int rval; 25730 ssize_t dma_resid; 25731 daddr_t oblkno; 25732 diskaddr_t nblks = 0; 25733 diskaddr_t start_block; 25734 25735 instance = SDUNIT(dev); 25736 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25737 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 25738 return (ENXIO); 25739 } 25740 25741 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25742 25743 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25744 25745 partition = SDPART(dev); 25746 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25747 25748 if (!(NOT_DEVBSIZE(un))) { 25749 int secmask = 0; 25750 int blknomask = 0; 25751 25752 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 25753 secmask = un->un_tgt_blocksize - 1; 25754 25755 if (blkno & blknomask) { 25756 SD_TRACE(SD_LOG_DUMP, un, 25757 "sddump: dump start block not modulo %d\n", 25758 un->un_tgt_blocksize); 25759 return (EINVAL); 25760 } 25761 25762 if ((nblk * DEV_BSIZE) & secmask) { 25763 SD_TRACE(SD_LOG_DUMP, un, 25764 "sddump: dump length not modulo %d\n", 25765 un->un_tgt_blocksize); 25766 return (EINVAL); 25767 } 25768 25769 } 25770 25771 /* Validate blocks to dump at against partition size. */ 25772 25773 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 25774 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 25775 25776 if (NOT_DEVBSIZE(un)) { 25777 if ((blkno + nblk) > nblks) { 25778 SD_TRACE(SD_LOG_DUMP, un, 25779 "sddump: dump range larger than partition: " 25780 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25781 blkno, nblk, nblks); 25782 return (EINVAL); 25783 } 25784 } else { 25785 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) + 25786 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) { 25787 SD_TRACE(SD_LOG_DUMP, un, 25788 "sddump: dump range larger than partition: " 25789 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25790 blkno, nblk, nblks); 25791 return (EINVAL); 25792 } 25793 } 25794 25795 mutex_enter(&un->un_pm_mutex); 25796 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25797 struct scsi_pkt *start_pktp; 25798 25799 mutex_exit(&un->un_pm_mutex); 25800 25801 /* 25802 * use pm framework to power on HBA 1st 25803 */ 25804 (void) pm_raise_power(SD_DEVINFO(un), 0, 25805 SD_PM_STATE_ACTIVE(un)); 25806 25807 /* 25808 * Dump no long uses sdpower to power on a device, it's 25809 * in-line here so it can be done in polled mode. 25810 */ 25811 25812 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25813 25814 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25815 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25816 25817 if (start_pktp == NULL) { 25818 /* We were not given a SCSI packet, fail. */ 25819 return (EIO); 25820 } 25821 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25822 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25823 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25824 start_pktp->pkt_flags = FLAG_NOINTR; 25825 25826 mutex_enter(SD_MUTEX(un)); 25827 SD_FILL_SCSI1_LUN(un, start_pktp); 25828 mutex_exit(SD_MUTEX(un)); 25829 /* 25830 * Scsi_poll returns 0 (success) if the command completes and 25831 * the status block is STATUS_GOOD. 25832 */ 25833 if (sd_scsi_poll(un, start_pktp) != 0) { 25834 scsi_destroy_pkt(start_pktp); 25835 return (EIO); 25836 } 25837 scsi_destroy_pkt(start_pktp); 25838 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un), 25839 SD_PM_STATE_CHANGE); 25840 } else { 25841 mutex_exit(&un->un_pm_mutex); 25842 } 25843 25844 mutex_enter(SD_MUTEX(un)); 25845 un->un_throttle = 0; 25846 25847 /* 25848 * The first time through, reset the specific target device. 25849 * However, when cpr calls sddump we know that sd is in a 25850 * a good state so no bus reset is required. 25851 * Clear sense data via Request Sense cmd. 25852 * In sddump we don't care about allow_bus_device_reset anymore 25853 */ 25854 25855 if ((un->un_state != SD_STATE_SUSPENDED) && 25856 (un->un_state != SD_STATE_DUMPING)) { 25857 25858 New_state(un, SD_STATE_DUMPING); 25859 25860 if (un->un_f_is_fibre == FALSE) { 25861 mutex_exit(SD_MUTEX(un)); 25862 /* 25863 * Attempt a bus reset for parallel scsi. 25864 * 25865 * Note: A bus reset is required because on some host 25866 * systems (i.e. E420R) a bus device reset is 25867 * insufficient to reset the state of the target. 25868 * 25869 * Note: Don't issue the reset for fibre-channel, 25870 * because this tends to hang the bus (loop) for 25871 * too long while everyone is logging out and in 25872 * and the deadman timer for dumping will fire 25873 * before the dump is complete. 25874 */ 25875 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25876 mutex_enter(SD_MUTEX(un)); 25877 Restore_state(un); 25878 mutex_exit(SD_MUTEX(un)); 25879 return (EIO); 25880 } 25881 25882 /* Delay to give the device some recovery time. */ 25883 drv_usecwait(10000); 25884 25885 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25886 SD_INFO(SD_LOG_DUMP, un, 25887 "sddump: sd_send_polled_RQS failed\n"); 25888 } 25889 mutex_enter(SD_MUTEX(un)); 25890 } 25891 } 25892 25893 /* 25894 * Convert the partition-relative block number to a 25895 * disk physical block number. 25896 */ 25897 if (NOT_DEVBSIZE(un)) { 25898 blkno += start_block; 25899 } else { 25900 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE); 25901 blkno += start_block; 25902 } 25903 25904 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25905 25906 25907 /* 25908 * Check if the device has a non-512 block size. 25909 */ 25910 wr_bp = NULL; 25911 if (NOT_DEVBSIZE(un)) { 25912 tgt_byte_offset = blkno * un->un_sys_blocksize; 25913 tgt_byte_count = nblk * un->un_sys_blocksize; 25914 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25915 (tgt_byte_count % un->un_tgt_blocksize)) { 25916 doing_rmw = TRUE; 25917 /* 25918 * Calculate the block number and number of block 25919 * in terms of the media block size. 25920 */ 25921 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25922 tgt_nblk = 25923 ((tgt_byte_offset + tgt_byte_count + 25924 (un->un_tgt_blocksize - 1)) / 25925 un->un_tgt_blocksize) - tgt_blkno; 25926 25927 /* 25928 * Invoke the routine which is going to do read part 25929 * of read-modify-write. 25930 * Note that this routine returns a pointer to 25931 * a valid bp in wr_bp. 25932 */ 25933 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25934 &wr_bp); 25935 if (err) { 25936 mutex_exit(SD_MUTEX(un)); 25937 return (err); 25938 } 25939 /* 25940 * Offset is being calculated as - 25941 * (original block # * system block size) - 25942 * (new block # * target block size) 25943 */ 25944 io_start_offset = 25945 ((uint64_t)(blkno * un->un_sys_blocksize)) - 25946 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 25947 25948 ASSERT((io_start_offset >= 0) && 25949 (io_start_offset < un->un_tgt_blocksize)); 25950 /* 25951 * Do the modify portion of read modify write. 25952 */ 25953 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 25954 (size_t)nblk * un->un_sys_blocksize); 25955 } else { 25956 doing_rmw = FALSE; 25957 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25958 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 25959 } 25960 25961 /* Convert blkno and nblk to target blocks */ 25962 blkno = tgt_blkno; 25963 nblk = tgt_nblk; 25964 } else { 25965 wr_bp = &wr_buf; 25966 bzero(wr_bp, sizeof (struct buf)); 25967 wr_bp->b_flags = B_BUSY; 25968 wr_bp->b_un.b_addr = addr; 25969 wr_bp->b_bcount = nblk << DEV_BSHIFT; 25970 wr_bp->b_resid = 0; 25971 } 25972 25973 mutex_exit(SD_MUTEX(un)); 25974 25975 /* 25976 * Obtain a SCSI packet for the write command. 25977 * It should be safe to call the allocator here without 25978 * worrying about being locked for DVMA mapping because 25979 * the address we're passed is already a DVMA mapping 25980 * 25981 * We are also not going to worry about semaphore ownership 25982 * in the dump buffer. Dumping is single threaded at present. 25983 */ 25984 25985 wr_pktp = NULL; 25986 25987 dma_resid = wr_bp->b_bcount; 25988 oblkno = blkno; 25989 25990 if (!(NOT_DEVBSIZE(un))) { 25991 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE); 25992 } 25993 25994 while (dma_resid != 0) { 25995 25996 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 25997 wr_bp->b_flags &= ~B_ERROR; 25998 25999 if (un->un_partial_dma_supported == 1) { 26000 blkno = oblkno + 26001 ((wr_bp->b_bcount - dma_resid) / 26002 un->un_tgt_blocksize); 26003 nblk = dma_resid / un->un_tgt_blocksize; 26004 26005 if (wr_pktp) { 26006 /* 26007 * Partial DMA transfers after initial transfer 26008 */ 26009 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26010 blkno, nblk); 26011 } else { 26012 /* Initial transfer */ 26013 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26014 un->un_pkt_flags, NULL_FUNC, NULL, 26015 blkno, nblk); 26016 } 26017 } else { 26018 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26019 0, NULL_FUNC, NULL, blkno, nblk); 26020 } 26021 26022 if (rval == 0) { 26023 /* We were given a SCSI packet, continue. */ 26024 break; 26025 } 26026 26027 if (i == 0) { 26028 if (wr_bp->b_flags & B_ERROR) { 26029 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26030 "no resources for dumping; " 26031 "error code: 0x%x, retrying", 26032 geterror(wr_bp)); 26033 } else { 26034 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26035 "no resources for dumping; retrying"); 26036 } 26037 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26038 if (wr_bp->b_flags & B_ERROR) { 26039 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26040 "no resources for dumping; error code: " 26041 "0x%x, retrying\n", geterror(wr_bp)); 26042 } 26043 } else { 26044 if (wr_bp->b_flags & B_ERROR) { 26045 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26046 "no resources for dumping; " 26047 "error code: 0x%x, retries failed, " 26048 "giving up.\n", geterror(wr_bp)); 26049 } else { 26050 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26051 "no resources for dumping; " 26052 "retries failed, giving up.\n"); 26053 } 26054 mutex_enter(SD_MUTEX(un)); 26055 Restore_state(un); 26056 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26057 mutex_exit(SD_MUTEX(un)); 26058 scsi_free_consistent_buf(wr_bp); 26059 } else { 26060 mutex_exit(SD_MUTEX(un)); 26061 } 26062 return (EIO); 26063 } 26064 drv_usecwait(10000); 26065 } 26066 26067 if (un->un_partial_dma_supported == 1) { 26068 /* 26069 * save the resid from PARTIAL_DMA 26070 */ 26071 dma_resid = wr_pktp->pkt_resid; 26072 if (dma_resid != 0) 26073 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26074 wr_pktp->pkt_resid = 0; 26075 } else { 26076 dma_resid = 0; 26077 } 26078 26079 /* SunBug 1222170 */ 26080 wr_pktp->pkt_flags = FLAG_NOINTR; 26081 26082 err = EIO; 26083 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26084 26085 /* 26086 * Scsi_poll returns 0 (success) if the command completes and 26087 * the status block is STATUS_GOOD. We should only check 26088 * errors if this condition is not true. Even then we should 26089 * send our own request sense packet only if we have a check 26090 * condition and auto request sense has not been performed by 26091 * the hba. 26092 */ 26093 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26094 26095 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26096 (wr_pktp->pkt_resid == 0)) { 26097 err = SD_SUCCESS; 26098 break; 26099 } 26100 26101 /* 26102 * Check CMD_DEV_GONE 1st, give up if device is gone. 26103 */ 26104 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26105 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26106 "Error while dumping state...Device is gone\n"); 26107 break; 26108 } 26109 26110 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26111 SD_INFO(SD_LOG_DUMP, un, 26112 "sddump: write failed with CHECK, try # %d\n", i); 26113 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26114 (void) sd_send_polled_RQS(un); 26115 } 26116 26117 continue; 26118 } 26119 26120 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26121 int reset_retval = 0; 26122 26123 SD_INFO(SD_LOG_DUMP, un, 26124 "sddump: write failed with BUSY, try # %d\n", i); 26125 26126 if (un->un_f_lun_reset_enabled == TRUE) { 26127 reset_retval = scsi_reset(SD_ADDRESS(un), 26128 RESET_LUN); 26129 } 26130 if (reset_retval == 0) { 26131 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26132 } 26133 (void) sd_send_polled_RQS(un); 26134 26135 } else { 26136 SD_INFO(SD_LOG_DUMP, un, 26137 "sddump: write failed with 0x%x, try # %d\n", 26138 SD_GET_PKT_STATUS(wr_pktp), i); 26139 mutex_enter(SD_MUTEX(un)); 26140 sd_reset_target(un, wr_pktp); 26141 mutex_exit(SD_MUTEX(un)); 26142 } 26143 26144 /* 26145 * If we are not getting anywhere with lun/target resets, 26146 * let's reset the bus. 26147 */ 26148 if (i == SD_NDUMP_RETRIES/2) { 26149 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26150 (void) sd_send_polled_RQS(un); 26151 } 26152 } 26153 } 26154 26155 scsi_destroy_pkt(wr_pktp); 26156 mutex_enter(SD_MUTEX(un)); 26157 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26158 mutex_exit(SD_MUTEX(un)); 26159 scsi_free_consistent_buf(wr_bp); 26160 } else { 26161 mutex_exit(SD_MUTEX(un)); 26162 } 26163 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26164 return (err); 26165 } 26166 26167 /* 26168 * Function: sd_scsi_poll() 26169 * 26170 * Description: This is a wrapper for the scsi_poll call. 26171 * 26172 * Arguments: sd_lun - The unit structure 26173 * scsi_pkt - The scsi packet being sent to the device. 26174 * 26175 * Return Code: 0 - Command completed successfully with good status 26176 * -1 - Command failed. This could indicate a check condition 26177 * or other status value requiring recovery action. 26178 * 26179 * NOTE: This code is only called off sddump(). 26180 */ 26181 26182 static int 26183 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26184 { 26185 int status; 26186 26187 ASSERT(un != NULL); 26188 ASSERT(!mutex_owned(SD_MUTEX(un))); 26189 ASSERT(pktp != NULL); 26190 26191 status = SD_SUCCESS; 26192 26193 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26194 pktp->pkt_flags |= un->un_tagflags; 26195 pktp->pkt_flags &= ~FLAG_NODISCON; 26196 } 26197 26198 status = sd_ddi_scsi_poll(pktp); 26199 /* 26200 * Scsi_poll returns 0 (success) if the command completes and the 26201 * status block is STATUS_GOOD. We should only check errors if this 26202 * condition is not true. Even then we should send our own request 26203 * sense packet only if we have a check condition and auto 26204 * request sense has not been performed by the hba. 26205 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26206 */ 26207 if ((status != SD_SUCCESS) && 26208 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26209 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26210 (pktp->pkt_reason != CMD_DEV_GONE)) 26211 (void) sd_send_polled_RQS(un); 26212 26213 return (status); 26214 } 26215 26216 /* 26217 * Function: sd_send_polled_RQS() 26218 * 26219 * Description: This sends the request sense command to a device. 26220 * 26221 * Arguments: sd_lun - The unit structure 26222 * 26223 * Return Code: 0 - Command completed successfully with good status 26224 * -1 - Command failed. 26225 * 26226 */ 26227 26228 static int 26229 sd_send_polled_RQS(struct sd_lun *un) 26230 { 26231 int ret_val; 26232 struct scsi_pkt *rqs_pktp; 26233 struct buf *rqs_bp; 26234 26235 ASSERT(un != NULL); 26236 ASSERT(!mutex_owned(SD_MUTEX(un))); 26237 26238 ret_val = SD_SUCCESS; 26239 26240 rqs_pktp = un->un_rqs_pktp; 26241 rqs_bp = un->un_rqs_bp; 26242 26243 mutex_enter(SD_MUTEX(un)); 26244 26245 if (un->un_sense_isbusy) { 26246 ret_val = SD_FAILURE; 26247 mutex_exit(SD_MUTEX(un)); 26248 return (ret_val); 26249 } 26250 26251 /* 26252 * If the request sense buffer (and packet) is not in use, 26253 * let's set the un_sense_isbusy and send our packet 26254 */ 26255 un->un_sense_isbusy = 1; 26256 rqs_pktp->pkt_resid = 0; 26257 rqs_pktp->pkt_reason = 0; 26258 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26259 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26260 26261 mutex_exit(SD_MUTEX(un)); 26262 26263 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26264 " 0x%p\n", rqs_bp->b_un.b_addr); 26265 26266 /* 26267 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26268 * axle - it has a call into us! 26269 */ 26270 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26271 SD_INFO(SD_LOG_COMMON, un, 26272 "sd_send_polled_RQS: RQS failed\n"); 26273 } 26274 26275 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26276 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26277 26278 mutex_enter(SD_MUTEX(un)); 26279 un->un_sense_isbusy = 0; 26280 mutex_exit(SD_MUTEX(un)); 26281 26282 return (ret_val); 26283 } 26284 26285 /* 26286 * Defines needed for localized version of the scsi_poll routine. 26287 */ 26288 #define CSEC 10000 /* usecs */ 26289 #define SEC_TO_CSEC (1000000/CSEC) 26290 26291 /* 26292 * Function: sd_ddi_scsi_poll() 26293 * 26294 * Description: Localized version of the scsi_poll routine. The purpose is to 26295 * send a scsi_pkt to a device as a polled command. This version 26296 * is to ensure more robust handling of transport errors. 26297 * Specifically this routine cures not ready, coming ready 26298 * transition for power up and reset of sonoma's. This can take 26299 * up to 45 seconds for power-on and 20 seconds for reset of a 26300 * sonoma lun. 26301 * 26302 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26303 * 26304 * Return Code: 0 - Command completed successfully with good status 26305 * -1 - Command failed. 26306 * 26307 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 26308 * be fixed (removing this code), we need to determine how to handle the 26309 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 26310 * 26311 * NOTE: This code is only called off sddump(). 26312 */ 26313 static int 26314 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26315 { 26316 int rval = -1; 26317 int savef; 26318 long savet; 26319 void (*savec)(); 26320 int timeout; 26321 int busy_count; 26322 int poll_delay; 26323 int rc; 26324 uint8_t *sensep; 26325 struct scsi_arq_status *arqstat; 26326 extern int do_polled_io; 26327 26328 ASSERT(pkt->pkt_scbp); 26329 26330 /* 26331 * save old flags.. 26332 */ 26333 savef = pkt->pkt_flags; 26334 savec = pkt->pkt_comp; 26335 savet = pkt->pkt_time; 26336 26337 pkt->pkt_flags |= FLAG_NOINTR; 26338 26339 /* 26340 * XXX there is nothing in the SCSA spec that states that we should not 26341 * do a callback for polled cmds; however, removing this will break sd 26342 * and probably other target drivers 26343 */ 26344 pkt->pkt_comp = NULL; 26345 26346 /* 26347 * we don't like a polled command without timeout. 26348 * 60 seconds seems long enough. 26349 */ 26350 if (pkt->pkt_time == 0) 26351 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26352 26353 /* 26354 * Send polled cmd. 26355 * 26356 * We do some error recovery for various errors. Tran_busy, 26357 * queue full, and non-dispatched commands are retried every 10 msec. 26358 * as they are typically transient failures. Busy status and Not 26359 * Ready are retried every second as this status takes a while to 26360 * change. 26361 */ 26362 timeout = pkt->pkt_time * SEC_TO_CSEC; 26363 26364 for (busy_count = 0; busy_count < timeout; busy_count++) { 26365 /* 26366 * Initialize pkt status variables. 26367 */ 26368 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26369 26370 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26371 if (rc != TRAN_BUSY) { 26372 /* Transport failed - give up. */ 26373 break; 26374 } else { 26375 /* Transport busy - try again. */ 26376 poll_delay = 1 * CSEC; /* 10 msec. */ 26377 } 26378 } else { 26379 /* 26380 * Transport accepted - check pkt status. 26381 */ 26382 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26383 if ((pkt->pkt_reason == CMD_CMPLT) && 26384 (rc == STATUS_CHECK) && 26385 (pkt->pkt_state & STATE_ARQ_DONE)) { 26386 arqstat = 26387 (struct scsi_arq_status *)(pkt->pkt_scbp); 26388 sensep = (uint8_t *)&arqstat->sts_sensedata; 26389 } else { 26390 sensep = NULL; 26391 } 26392 26393 if ((pkt->pkt_reason == CMD_CMPLT) && 26394 (rc == STATUS_GOOD)) { 26395 /* No error - we're done */ 26396 rval = 0; 26397 break; 26398 26399 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26400 /* Lost connection - give up */ 26401 break; 26402 26403 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26404 (pkt->pkt_state == 0)) { 26405 /* Pkt not dispatched - try again. */ 26406 poll_delay = 1 * CSEC; /* 10 msec. */ 26407 26408 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26409 (rc == STATUS_QFULL)) { 26410 /* Queue full - try again. */ 26411 poll_delay = 1 * CSEC; /* 10 msec. */ 26412 26413 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26414 (rc == STATUS_BUSY)) { 26415 /* Busy - try again. */ 26416 poll_delay = 100 * CSEC; /* 1 sec. */ 26417 busy_count += (SEC_TO_CSEC - 1); 26418 26419 } else if ((sensep != NULL) && 26420 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 26421 /* 26422 * Unit Attention - try again. 26423 * Pretend it took 1 sec. 26424 * NOTE: 'continue' avoids poll_delay 26425 */ 26426 busy_count += (SEC_TO_CSEC - 1); 26427 continue; 26428 26429 } else if ((sensep != NULL) && 26430 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26431 (scsi_sense_asc(sensep) == 0x04) && 26432 (scsi_sense_ascq(sensep) == 0x01)) { 26433 /* 26434 * Not ready -> ready - try again. 26435 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 26436 * ...same as STATUS_BUSY 26437 */ 26438 poll_delay = 100 * CSEC; /* 1 sec. */ 26439 busy_count += (SEC_TO_CSEC - 1); 26440 26441 } else { 26442 /* BAD status - give up. */ 26443 break; 26444 } 26445 } 26446 26447 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 26448 !do_polled_io) { 26449 delay(drv_usectohz(poll_delay)); 26450 } else { 26451 /* we busy wait during cpr_dump or interrupt threads */ 26452 drv_usecwait(poll_delay); 26453 } 26454 } 26455 26456 pkt->pkt_flags = savef; 26457 pkt->pkt_comp = savec; 26458 pkt->pkt_time = savet; 26459 26460 /* return on error */ 26461 if (rval) 26462 return (rval); 26463 26464 /* 26465 * This is not a performance critical code path. 26466 * 26467 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 26468 * issues associated with looking at DMA memory prior to 26469 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 26470 */ 26471 scsi_sync_pkt(pkt); 26472 return (0); 26473 } 26474 26475 26476 26477 /* 26478 * Function: sd_persistent_reservation_in_read_keys 26479 * 26480 * Description: This routine is the driver entry point for handling CD-ROM 26481 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26482 * by sending the SCSI-3 PRIN commands to the device. 26483 * Processes the read keys command response by copying the 26484 * reservation key information into the user provided buffer. 26485 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26486 * 26487 * Arguments: un - Pointer to soft state struct for the target. 26488 * usrp - user provided pointer to multihost Persistent In Read 26489 * Keys structure (mhioc_inkeys_t) 26490 * flag - this argument is a pass through to ddi_copyxxx() 26491 * directly from the mode argument of ioctl(). 26492 * 26493 * Return Code: 0 - Success 26494 * EACCES 26495 * ENOTSUP 26496 * errno return code from sd_send_scsi_cmd() 26497 * 26498 * Context: Can sleep. Does not return until command is completed. 26499 */ 26500 26501 static int 26502 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26503 mhioc_inkeys_t *usrp, int flag) 26504 { 26505 #ifdef _MULTI_DATAMODEL 26506 struct mhioc_key_list32 li32; 26507 #endif 26508 sd_prin_readkeys_t *in; 26509 mhioc_inkeys_t *ptr; 26510 mhioc_key_list_t li; 26511 uchar_t *data_bufp; 26512 int data_len; 26513 int rval = 0; 26514 size_t copysz; 26515 sd_ssc_t *ssc; 26516 26517 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26518 return (EINVAL); 26519 } 26520 bzero(&li, sizeof (mhioc_key_list_t)); 26521 26522 ssc = sd_ssc_init(un); 26523 26524 /* 26525 * Get the listsize from user 26526 */ 26527 #ifdef _MULTI_DATAMODEL 26528 26529 switch (ddi_model_convert_from(flag & FMODELS)) { 26530 case DDI_MODEL_ILP32: 26531 copysz = sizeof (struct mhioc_key_list32); 26532 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26533 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26534 "sd_persistent_reservation_in_read_keys: " 26535 "failed ddi_copyin: mhioc_key_list32_t\n"); 26536 rval = EFAULT; 26537 goto done; 26538 } 26539 li.listsize = li32.listsize; 26540 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26541 break; 26542 26543 case DDI_MODEL_NONE: 26544 copysz = sizeof (mhioc_key_list_t); 26545 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26546 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26547 "sd_persistent_reservation_in_read_keys: " 26548 "failed ddi_copyin: mhioc_key_list_t\n"); 26549 rval = EFAULT; 26550 goto done; 26551 } 26552 break; 26553 } 26554 26555 #else /* ! _MULTI_DATAMODEL */ 26556 copysz = sizeof (mhioc_key_list_t); 26557 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26558 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26559 "sd_persistent_reservation_in_read_keys: " 26560 "failed ddi_copyin: mhioc_key_list_t\n"); 26561 rval = EFAULT; 26562 goto done; 26563 } 26564 #endif 26565 26566 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26567 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26568 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26569 26570 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 26571 data_len, data_bufp); 26572 if (rval != 0) { 26573 if (rval == EIO) 26574 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26575 else 26576 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26577 goto done; 26578 } 26579 in = (sd_prin_readkeys_t *)data_bufp; 26580 ptr->generation = BE_32(in->generation); 26581 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26582 26583 /* 26584 * Return the min(listsize, listlen) keys 26585 */ 26586 #ifdef _MULTI_DATAMODEL 26587 26588 switch (ddi_model_convert_from(flag & FMODELS)) { 26589 case DDI_MODEL_ILP32: 26590 li32.listlen = li.listlen; 26591 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26592 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26593 "sd_persistent_reservation_in_read_keys: " 26594 "failed ddi_copyout: mhioc_key_list32_t\n"); 26595 rval = EFAULT; 26596 goto done; 26597 } 26598 break; 26599 26600 case DDI_MODEL_NONE: 26601 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26602 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26603 "sd_persistent_reservation_in_read_keys: " 26604 "failed ddi_copyout: mhioc_key_list_t\n"); 26605 rval = EFAULT; 26606 goto done; 26607 } 26608 break; 26609 } 26610 26611 #else /* ! _MULTI_DATAMODEL */ 26612 26613 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26614 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26615 "sd_persistent_reservation_in_read_keys: " 26616 "failed ddi_copyout: mhioc_key_list_t\n"); 26617 rval = EFAULT; 26618 goto done; 26619 } 26620 26621 #endif /* _MULTI_DATAMODEL */ 26622 26623 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26624 li.listsize * MHIOC_RESV_KEY_SIZE); 26625 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26626 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26627 "sd_persistent_reservation_in_read_keys: " 26628 "failed ddi_copyout: keylist\n"); 26629 rval = EFAULT; 26630 } 26631 done: 26632 sd_ssc_fini(ssc); 26633 kmem_free(data_bufp, data_len); 26634 return (rval); 26635 } 26636 26637 26638 /* 26639 * Function: sd_persistent_reservation_in_read_resv 26640 * 26641 * Description: This routine is the driver entry point for handling CD-ROM 26642 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26643 * by sending the SCSI-3 PRIN commands to the device. 26644 * Process the read persistent reservations command response by 26645 * copying the reservation information into the user provided 26646 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26647 * 26648 * Arguments: un - Pointer to soft state struct for the target. 26649 * usrp - user provided pointer to multihost Persistent In Read 26650 * Keys structure (mhioc_inkeys_t) 26651 * flag - this argument is a pass through to ddi_copyxxx() 26652 * directly from the mode argument of ioctl(). 26653 * 26654 * Return Code: 0 - Success 26655 * EACCES 26656 * ENOTSUP 26657 * errno return code from sd_send_scsi_cmd() 26658 * 26659 * Context: Can sleep. Does not return until command is completed. 26660 */ 26661 26662 static int 26663 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26664 mhioc_inresvs_t *usrp, int flag) 26665 { 26666 #ifdef _MULTI_DATAMODEL 26667 struct mhioc_resv_desc_list32 resvlist32; 26668 #endif 26669 sd_prin_readresv_t *in; 26670 mhioc_inresvs_t *ptr; 26671 sd_readresv_desc_t *readresv_ptr; 26672 mhioc_resv_desc_list_t resvlist; 26673 mhioc_resv_desc_t resvdesc; 26674 uchar_t *data_bufp = NULL; 26675 int data_len; 26676 int rval = 0; 26677 int i; 26678 size_t copysz; 26679 mhioc_resv_desc_t *bufp; 26680 sd_ssc_t *ssc; 26681 26682 if ((ptr = usrp) == NULL) { 26683 return (EINVAL); 26684 } 26685 26686 ssc = sd_ssc_init(un); 26687 26688 /* 26689 * Get the listsize from user 26690 */ 26691 #ifdef _MULTI_DATAMODEL 26692 switch (ddi_model_convert_from(flag & FMODELS)) { 26693 case DDI_MODEL_ILP32: 26694 copysz = sizeof (struct mhioc_resv_desc_list32); 26695 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26696 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26697 "sd_persistent_reservation_in_read_resv: " 26698 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26699 rval = EFAULT; 26700 goto done; 26701 } 26702 resvlist.listsize = resvlist32.listsize; 26703 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26704 break; 26705 26706 case DDI_MODEL_NONE: 26707 copysz = sizeof (mhioc_resv_desc_list_t); 26708 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26709 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26710 "sd_persistent_reservation_in_read_resv: " 26711 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26712 rval = EFAULT; 26713 goto done; 26714 } 26715 break; 26716 } 26717 #else /* ! _MULTI_DATAMODEL */ 26718 copysz = sizeof (mhioc_resv_desc_list_t); 26719 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26720 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26721 "sd_persistent_reservation_in_read_resv: " 26722 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26723 rval = EFAULT; 26724 goto done; 26725 } 26726 #endif /* ! _MULTI_DATAMODEL */ 26727 26728 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26729 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26730 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26731 26732 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV, 26733 data_len, data_bufp); 26734 if (rval != 0) { 26735 if (rval == EIO) 26736 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26737 else 26738 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26739 goto done; 26740 } 26741 in = (sd_prin_readresv_t *)data_bufp; 26742 ptr->generation = BE_32(in->generation); 26743 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26744 26745 /* 26746 * Return the min(listsize, listlen( keys 26747 */ 26748 #ifdef _MULTI_DATAMODEL 26749 26750 switch (ddi_model_convert_from(flag & FMODELS)) { 26751 case DDI_MODEL_ILP32: 26752 resvlist32.listlen = resvlist.listlen; 26753 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26754 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26755 "sd_persistent_reservation_in_read_resv: " 26756 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26757 rval = EFAULT; 26758 goto done; 26759 } 26760 break; 26761 26762 case DDI_MODEL_NONE: 26763 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26764 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26765 "sd_persistent_reservation_in_read_resv: " 26766 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26767 rval = EFAULT; 26768 goto done; 26769 } 26770 break; 26771 } 26772 26773 #else /* ! _MULTI_DATAMODEL */ 26774 26775 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26776 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26777 "sd_persistent_reservation_in_read_resv: " 26778 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26779 rval = EFAULT; 26780 goto done; 26781 } 26782 26783 #endif /* ! _MULTI_DATAMODEL */ 26784 26785 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26786 bufp = resvlist.list; 26787 copysz = sizeof (mhioc_resv_desc_t); 26788 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26789 i++, readresv_ptr++, bufp++) { 26790 26791 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26792 MHIOC_RESV_KEY_SIZE); 26793 resvdesc.type = readresv_ptr->type; 26794 resvdesc.scope = readresv_ptr->scope; 26795 resvdesc.scope_specific_addr = 26796 BE_32(readresv_ptr->scope_specific_addr); 26797 26798 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26799 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26800 "sd_persistent_reservation_in_read_resv: " 26801 "failed ddi_copyout: resvlist\n"); 26802 rval = EFAULT; 26803 goto done; 26804 } 26805 } 26806 done: 26807 sd_ssc_fini(ssc); 26808 /* only if data_bufp is allocated, we need to free it */ 26809 if (data_bufp) { 26810 kmem_free(data_bufp, data_len); 26811 } 26812 return (rval); 26813 } 26814 26815 26816 /* 26817 * Function: sr_change_blkmode() 26818 * 26819 * Description: This routine is the driver entry point for handling CD-ROM 26820 * block mode ioctl requests. Support for returning and changing 26821 * the current block size in use by the device is implemented. The 26822 * LBA size is changed via a MODE SELECT Block Descriptor. 26823 * 26824 * This routine issues a mode sense with an allocation length of 26825 * 12 bytes for the mode page header and a single block descriptor. 26826 * 26827 * Arguments: dev - the device 'dev_t' 26828 * cmd - the request type; one of CDROMGBLKMODE (get) or 26829 * CDROMSBLKMODE (set) 26830 * data - current block size or requested block size 26831 * flag - this argument is a pass through to ddi_copyxxx() directly 26832 * from the mode argument of ioctl(). 26833 * 26834 * Return Code: the code returned by sd_send_scsi_cmd() 26835 * EINVAL if invalid arguments are provided 26836 * EFAULT if ddi_copyxxx() fails 26837 * ENXIO if fail ddi_get_soft_state 26838 * EIO if invalid mode sense block descriptor length 26839 * 26840 */ 26841 26842 static int 26843 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26844 { 26845 struct sd_lun *un = NULL; 26846 struct mode_header *sense_mhp, *select_mhp; 26847 struct block_descriptor *sense_desc, *select_desc; 26848 int current_bsize; 26849 int rval = EINVAL; 26850 uchar_t *sense = NULL; 26851 uchar_t *select = NULL; 26852 sd_ssc_t *ssc; 26853 26854 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26855 26856 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26857 return (ENXIO); 26858 } 26859 26860 /* 26861 * The block length is changed via the Mode Select block descriptor, the 26862 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26863 * required as part of this routine. Therefore the mode sense allocation 26864 * length is specified to be the length of a mode page header and a 26865 * block descriptor. 26866 */ 26867 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26868 26869 ssc = sd_ssc_init(un); 26870 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 26871 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 26872 sd_ssc_fini(ssc); 26873 if (rval != 0) { 26874 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26875 "sr_change_blkmode: Mode Sense Failed\n"); 26876 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26877 return (rval); 26878 } 26879 26880 /* Check the block descriptor len to handle only 1 block descriptor */ 26881 sense_mhp = (struct mode_header *)sense; 26882 if ((sense_mhp->bdesc_length == 0) || 26883 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26884 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26885 "sr_change_blkmode: Mode Sense returned invalid block" 26886 " descriptor length\n"); 26887 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26888 return (EIO); 26889 } 26890 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26891 current_bsize = ((sense_desc->blksize_hi << 16) | 26892 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26893 26894 /* Process command */ 26895 switch (cmd) { 26896 case CDROMGBLKMODE: 26897 /* Return the block size obtained during the mode sense */ 26898 if (ddi_copyout(¤t_bsize, (void *)data, 26899 sizeof (int), flag) != 0) 26900 rval = EFAULT; 26901 break; 26902 case CDROMSBLKMODE: 26903 /* Validate the requested block size */ 26904 switch (data) { 26905 case CDROM_BLK_512: 26906 case CDROM_BLK_1024: 26907 case CDROM_BLK_2048: 26908 case CDROM_BLK_2056: 26909 case CDROM_BLK_2336: 26910 case CDROM_BLK_2340: 26911 case CDROM_BLK_2352: 26912 case CDROM_BLK_2368: 26913 case CDROM_BLK_2448: 26914 case CDROM_BLK_2646: 26915 case CDROM_BLK_2647: 26916 break; 26917 default: 26918 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26919 "sr_change_blkmode: " 26920 "Block Size '%ld' Not Supported\n", data); 26921 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26922 return (EINVAL); 26923 } 26924 26925 /* 26926 * The current block size matches the requested block size so 26927 * there is no need to send the mode select to change the size 26928 */ 26929 if (current_bsize == data) { 26930 break; 26931 } 26932 26933 /* Build the select data for the requested block size */ 26934 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26935 select_mhp = (struct mode_header *)select; 26936 select_desc = 26937 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26938 /* 26939 * The LBA size is changed via the block descriptor, so the 26940 * descriptor is built according to the user data 26941 */ 26942 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26943 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26944 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26945 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26946 26947 /* Send the mode select for the requested block size */ 26948 ssc = sd_ssc_init(un); 26949 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26950 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26951 SD_PATH_STANDARD); 26952 sd_ssc_fini(ssc); 26953 if (rval != 0) { 26954 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26955 "sr_change_blkmode: Mode Select Failed\n"); 26956 /* 26957 * The mode select failed for the requested block size, 26958 * so reset the data for the original block size and 26959 * send it to the target. The error is indicated by the 26960 * return value for the failed mode select. 26961 */ 26962 select_desc->blksize_hi = sense_desc->blksize_hi; 26963 select_desc->blksize_mid = sense_desc->blksize_mid; 26964 select_desc->blksize_lo = sense_desc->blksize_lo; 26965 ssc = sd_ssc_init(un); 26966 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26967 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26968 SD_PATH_STANDARD); 26969 sd_ssc_fini(ssc); 26970 } else { 26971 ASSERT(!mutex_owned(SD_MUTEX(un))); 26972 mutex_enter(SD_MUTEX(un)); 26973 sd_update_block_info(un, (uint32_t)data, 0); 26974 mutex_exit(SD_MUTEX(un)); 26975 } 26976 break; 26977 default: 26978 /* should not reach here, but check anyway */ 26979 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26980 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 26981 rval = EINVAL; 26982 break; 26983 } 26984 26985 if (select) { 26986 kmem_free(select, BUFLEN_CHG_BLK_MODE); 26987 } 26988 if (sense) { 26989 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26990 } 26991 return (rval); 26992 } 26993 26994 26995 /* 26996 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 26997 * implement driver support for getting and setting the CD speed. The command 26998 * set used will be based on the device type. If the device has not been 26999 * identified as MMC the Toshiba vendor specific mode page will be used. If 27000 * the device is MMC but does not support the Real Time Streaming feature 27001 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27002 * be used to read the speed. 27003 */ 27004 27005 /* 27006 * Function: sr_change_speed() 27007 * 27008 * Description: This routine is the driver entry point for handling CD-ROM 27009 * drive speed ioctl requests for devices supporting the Toshiba 27010 * vendor specific drive speed mode page. Support for returning 27011 * and changing the current drive speed in use by the device is 27012 * implemented. 27013 * 27014 * Arguments: dev - the device 'dev_t' 27015 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27016 * CDROMSDRVSPEED (set) 27017 * data - current drive speed or requested drive speed 27018 * flag - this argument is a pass through to ddi_copyxxx() directly 27019 * from the mode argument of ioctl(). 27020 * 27021 * Return Code: the code returned by sd_send_scsi_cmd() 27022 * EINVAL if invalid arguments are provided 27023 * EFAULT if ddi_copyxxx() fails 27024 * ENXIO if fail ddi_get_soft_state 27025 * EIO if invalid mode sense block descriptor length 27026 */ 27027 27028 static int 27029 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27030 { 27031 struct sd_lun *un = NULL; 27032 struct mode_header *sense_mhp, *select_mhp; 27033 struct mode_speed *sense_page, *select_page; 27034 int current_speed; 27035 int rval = EINVAL; 27036 int bd_len; 27037 uchar_t *sense = NULL; 27038 uchar_t *select = NULL; 27039 sd_ssc_t *ssc; 27040 27041 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27042 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27043 return (ENXIO); 27044 } 27045 27046 /* 27047 * Note: The drive speed is being modified here according to a Toshiba 27048 * vendor specific mode page (0x31). 27049 */ 27050 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27051 27052 ssc = sd_ssc_init(un); 27053 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 27054 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27055 SD_PATH_STANDARD); 27056 sd_ssc_fini(ssc); 27057 if (rval != 0) { 27058 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27059 "sr_change_speed: Mode Sense Failed\n"); 27060 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27061 return (rval); 27062 } 27063 sense_mhp = (struct mode_header *)sense; 27064 27065 /* Check the block descriptor len to handle only 1 block descriptor */ 27066 bd_len = sense_mhp->bdesc_length; 27067 if (bd_len > MODE_BLK_DESC_LENGTH) { 27068 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27069 "sr_change_speed: Mode Sense returned invalid block " 27070 "descriptor length\n"); 27071 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27072 return (EIO); 27073 } 27074 27075 sense_page = (struct mode_speed *) 27076 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27077 current_speed = sense_page->speed; 27078 27079 /* Process command */ 27080 switch (cmd) { 27081 case CDROMGDRVSPEED: 27082 /* Return the drive speed obtained during the mode sense */ 27083 if (current_speed == 0x2) { 27084 current_speed = CDROM_TWELVE_SPEED; 27085 } 27086 if (ddi_copyout(¤t_speed, (void *)data, 27087 sizeof (int), flag) != 0) { 27088 rval = EFAULT; 27089 } 27090 break; 27091 case CDROMSDRVSPEED: 27092 /* Validate the requested drive speed */ 27093 switch ((uchar_t)data) { 27094 case CDROM_TWELVE_SPEED: 27095 data = 0x2; 27096 /*FALLTHROUGH*/ 27097 case CDROM_NORMAL_SPEED: 27098 case CDROM_DOUBLE_SPEED: 27099 case CDROM_QUAD_SPEED: 27100 case CDROM_MAXIMUM_SPEED: 27101 break; 27102 default: 27103 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27104 "sr_change_speed: " 27105 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27106 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27107 return (EINVAL); 27108 } 27109 27110 /* 27111 * The current drive speed matches the requested drive speed so 27112 * there is no need to send the mode select to change the speed 27113 */ 27114 if (current_speed == data) { 27115 break; 27116 } 27117 27118 /* Build the select data for the requested drive speed */ 27119 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27120 select_mhp = (struct mode_header *)select; 27121 select_mhp->bdesc_length = 0; 27122 select_page = 27123 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27124 select_page = 27125 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27126 select_page->mode_page.code = CDROM_MODE_SPEED; 27127 select_page->mode_page.length = 2; 27128 select_page->speed = (uchar_t)data; 27129 27130 /* Send the mode select for the requested block size */ 27131 ssc = sd_ssc_init(un); 27132 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27133 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27134 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27135 sd_ssc_fini(ssc); 27136 if (rval != 0) { 27137 /* 27138 * The mode select failed for the requested drive speed, 27139 * so reset the data for the original drive speed and 27140 * send it to the target. The error is indicated by the 27141 * return value for the failed mode select. 27142 */ 27143 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27144 "sr_drive_speed: Mode Select Failed\n"); 27145 select_page->speed = sense_page->speed; 27146 ssc = sd_ssc_init(un); 27147 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27148 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27149 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27150 sd_ssc_fini(ssc); 27151 } 27152 break; 27153 default: 27154 /* should not reach here, but check anyway */ 27155 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27156 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27157 rval = EINVAL; 27158 break; 27159 } 27160 27161 if (select) { 27162 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27163 } 27164 if (sense) { 27165 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27166 } 27167 27168 return (rval); 27169 } 27170 27171 27172 /* 27173 * Function: sr_atapi_change_speed() 27174 * 27175 * Description: This routine is the driver entry point for handling CD-ROM 27176 * drive speed ioctl requests for MMC devices that do not support 27177 * the Real Time Streaming feature (0x107). 27178 * 27179 * Note: This routine will use the SET SPEED command which may not 27180 * be supported by all devices. 27181 * 27182 * Arguments: dev- the device 'dev_t' 27183 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27184 * CDROMSDRVSPEED (set) 27185 * data- current drive speed or requested drive speed 27186 * flag- this argument is a pass through to ddi_copyxxx() directly 27187 * from the mode argument of ioctl(). 27188 * 27189 * Return Code: the code returned by sd_send_scsi_cmd() 27190 * EINVAL if invalid arguments are provided 27191 * EFAULT if ddi_copyxxx() fails 27192 * ENXIO if fail ddi_get_soft_state 27193 * EIO if invalid mode sense block descriptor length 27194 */ 27195 27196 static int 27197 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27198 { 27199 struct sd_lun *un; 27200 struct uscsi_cmd *com = NULL; 27201 struct mode_header_grp2 *sense_mhp; 27202 uchar_t *sense_page; 27203 uchar_t *sense = NULL; 27204 char cdb[CDB_GROUP5]; 27205 int bd_len; 27206 int current_speed = 0; 27207 int max_speed = 0; 27208 int rval; 27209 sd_ssc_t *ssc; 27210 27211 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27212 27213 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27214 return (ENXIO); 27215 } 27216 27217 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27218 27219 ssc = sd_ssc_init(un); 27220 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 27221 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27222 SD_PATH_STANDARD); 27223 sd_ssc_fini(ssc); 27224 if (rval != 0) { 27225 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27226 "sr_atapi_change_speed: Mode Sense Failed\n"); 27227 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27228 return (rval); 27229 } 27230 27231 /* Check the block descriptor len to handle only 1 block descriptor */ 27232 sense_mhp = (struct mode_header_grp2 *)sense; 27233 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27234 if (bd_len > MODE_BLK_DESC_LENGTH) { 27235 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27236 "sr_atapi_change_speed: Mode Sense returned invalid " 27237 "block descriptor length\n"); 27238 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27239 return (EIO); 27240 } 27241 27242 /* Calculate the current and maximum drive speeds */ 27243 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27244 current_speed = (sense_page[14] << 8) | sense_page[15]; 27245 max_speed = (sense_page[8] << 8) | sense_page[9]; 27246 27247 /* Process the command */ 27248 switch (cmd) { 27249 case CDROMGDRVSPEED: 27250 current_speed /= SD_SPEED_1X; 27251 if (ddi_copyout(¤t_speed, (void *)data, 27252 sizeof (int), flag) != 0) 27253 rval = EFAULT; 27254 break; 27255 case CDROMSDRVSPEED: 27256 /* Convert the speed code to KB/sec */ 27257 switch ((uchar_t)data) { 27258 case CDROM_NORMAL_SPEED: 27259 current_speed = SD_SPEED_1X; 27260 break; 27261 case CDROM_DOUBLE_SPEED: 27262 current_speed = 2 * SD_SPEED_1X; 27263 break; 27264 case CDROM_QUAD_SPEED: 27265 current_speed = 4 * SD_SPEED_1X; 27266 break; 27267 case CDROM_TWELVE_SPEED: 27268 current_speed = 12 * SD_SPEED_1X; 27269 break; 27270 case CDROM_MAXIMUM_SPEED: 27271 current_speed = 0xffff; 27272 break; 27273 default: 27274 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27275 "sr_atapi_change_speed: invalid drive speed %d\n", 27276 (uchar_t)data); 27277 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27278 return (EINVAL); 27279 } 27280 27281 /* Check the request against the drive's max speed. */ 27282 if (current_speed != 0xffff) { 27283 if (current_speed > max_speed) { 27284 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27285 return (EINVAL); 27286 } 27287 } 27288 27289 /* 27290 * Build and send the SET SPEED command 27291 * 27292 * Note: The SET SPEED (0xBB) command used in this routine is 27293 * obsolete per the SCSI MMC spec but still supported in the 27294 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27295 * therefore the command is still implemented in this routine. 27296 */ 27297 bzero(cdb, sizeof (cdb)); 27298 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27299 cdb[2] = (uchar_t)(current_speed >> 8); 27300 cdb[3] = (uchar_t)current_speed; 27301 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27302 com->uscsi_cdb = (caddr_t)cdb; 27303 com->uscsi_cdblen = CDB_GROUP5; 27304 com->uscsi_bufaddr = NULL; 27305 com->uscsi_buflen = 0; 27306 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27307 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 27308 break; 27309 default: 27310 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27311 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27312 rval = EINVAL; 27313 } 27314 27315 if (sense) { 27316 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27317 } 27318 if (com) { 27319 kmem_free(com, sizeof (*com)); 27320 } 27321 return (rval); 27322 } 27323 27324 27325 /* 27326 * Function: sr_pause_resume() 27327 * 27328 * Description: This routine is the driver entry point for handling CD-ROM 27329 * pause/resume ioctl requests. This only affects the audio play 27330 * operation. 27331 * 27332 * Arguments: dev - the device 'dev_t' 27333 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27334 * for setting the resume bit of the cdb. 27335 * 27336 * Return Code: the code returned by sd_send_scsi_cmd() 27337 * EINVAL if invalid mode specified 27338 * 27339 */ 27340 27341 static int 27342 sr_pause_resume(dev_t dev, int cmd) 27343 { 27344 struct sd_lun *un; 27345 struct uscsi_cmd *com; 27346 char cdb[CDB_GROUP1]; 27347 int rval; 27348 27349 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27350 return (ENXIO); 27351 } 27352 27353 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27354 bzero(cdb, CDB_GROUP1); 27355 cdb[0] = SCMD_PAUSE_RESUME; 27356 switch (cmd) { 27357 case CDROMRESUME: 27358 cdb[8] = 1; 27359 break; 27360 case CDROMPAUSE: 27361 cdb[8] = 0; 27362 break; 27363 default: 27364 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27365 " Command '%x' Not Supported\n", cmd); 27366 rval = EINVAL; 27367 goto done; 27368 } 27369 27370 com->uscsi_cdb = cdb; 27371 com->uscsi_cdblen = CDB_GROUP1; 27372 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27373 27374 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27375 SD_PATH_STANDARD); 27376 27377 done: 27378 kmem_free(com, sizeof (*com)); 27379 return (rval); 27380 } 27381 27382 27383 /* 27384 * Function: sr_play_msf() 27385 * 27386 * Description: This routine is the driver entry point for handling CD-ROM 27387 * ioctl requests to output the audio signals at the specified 27388 * starting address and continue the audio play until the specified 27389 * ending address (CDROMPLAYMSF) The address is in Minute Second 27390 * Frame (MSF) format. 27391 * 27392 * Arguments: dev - the device 'dev_t' 27393 * data - pointer to user provided audio msf structure, 27394 * specifying start/end addresses. 27395 * flag - this argument is a pass through to ddi_copyxxx() 27396 * directly from the mode argument of ioctl(). 27397 * 27398 * Return Code: the code returned by sd_send_scsi_cmd() 27399 * EFAULT if ddi_copyxxx() fails 27400 * ENXIO if fail ddi_get_soft_state 27401 * EINVAL if data pointer is NULL 27402 */ 27403 27404 static int 27405 sr_play_msf(dev_t dev, caddr_t data, int flag) 27406 { 27407 struct sd_lun *un; 27408 struct uscsi_cmd *com; 27409 struct cdrom_msf msf_struct; 27410 struct cdrom_msf *msf = &msf_struct; 27411 char cdb[CDB_GROUP1]; 27412 int rval; 27413 27414 if (data == NULL) { 27415 return (EINVAL); 27416 } 27417 27418 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27419 return (ENXIO); 27420 } 27421 27422 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27423 return (EFAULT); 27424 } 27425 27426 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27427 bzero(cdb, CDB_GROUP1); 27428 cdb[0] = SCMD_PLAYAUDIO_MSF; 27429 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27430 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27431 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27432 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27433 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27434 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27435 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27436 } else { 27437 cdb[3] = msf->cdmsf_min0; 27438 cdb[4] = msf->cdmsf_sec0; 27439 cdb[5] = msf->cdmsf_frame0; 27440 cdb[6] = msf->cdmsf_min1; 27441 cdb[7] = msf->cdmsf_sec1; 27442 cdb[8] = msf->cdmsf_frame1; 27443 } 27444 com->uscsi_cdb = cdb; 27445 com->uscsi_cdblen = CDB_GROUP1; 27446 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27447 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27448 SD_PATH_STANDARD); 27449 kmem_free(com, sizeof (*com)); 27450 return (rval); 27451 } 27452 27453 27454 /* 27455 * Function: sr_play_trkind() 27456 * 27457 * Description: This routine is the driver entry point for handling CD-ROM 27458 * ioctl requests to output the audio signals at the specified 27459 * starting address and continue the audio play until the specified 27460 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27461 * format. 27462 * 27463 * Arguments: dev - the device 'dev_t' 27464 * data - pointer to user provided audio track/index structure, 27465 * specifying start/end addresses. 27466 * flag - this argument is a pass through to ddi_copyxxx() 27467 * directly from the mode argument of ioctl(). 27468 * 27469 * Return Code: the code returned by sd_send_scsi_cmd() 27470 * EFAULT if ddi_copyxxx() fails 27471 * ENXIO if fail ddi_get_soft_state 27472 * EINVAL if data pointer is NULL 27473 */ 27474 27475 static int 27476 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27477 { 27478 struct cdrom_ti ti_struct; 27479 struct cdrom_ti *ti = &ti_struct; 27480 struct uscsi_cmd *com = NULL; 27481 char cdb[CDB_GROUP1]; 27482 int rval; 27483 27484 if (data == NULL) { 27485 return (EINVAL); 27486 } 27487 27488 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27489 return (EFAULT); 27490 } 27491 27492 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27493 bzero(cdb, CDB_GROUP1); 27494 cdb[0] = SCMD_PLAYAUDIO_TI; 27495 cdb[4] = ti->cdti_trk0; 27496 cdb[5] = ti->cdti_ind0; 27497 cdb[7] = ti->cdti_trk1; 27498 cdb[8] = ti->cdti_ind1; 27499 com->uscsi_cdb = cdb; 27500 com->uscsi_cdblen = CDB_GROUP1; 27501 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27502 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27503 SD_PATH_STANDARD); 27504 kmem_free(com, sizeof (*com)); 27505 return (rval); 27506 } 27507 27508 27509 /* 27510 * Function: sr_read_all_subcodes() 27511 * 27512 * Description: This routine is the driver entry point for handling CD-ROM 27513 * ioctl requests to return raw subcode data while the target is 27514 * playing audio (CDROMSUBCODE). 27515 * 27516 * Arguments: dev - the device 'dev_t' 27517 * data - pointer to user provided cdrom subcode structure, 27518 * specifying the transfer length and address. 27519 * flag - this argument is a pass through to ddi_copyxxx() 27520 * directly from the mode argument of ioctl(). 27521 * 27522 * Return Code: the code returned by sd_send_scsi_cmd() 27523 * EFAULT if ddi_copyxxx() fails 27524 * ENXIO if fail ddi_get_soft_state 27525 * EINVAL if data pointer is NULL 27526 */ 27527 27528 static int 27529 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27530 { 27531 struct sd_lun *un = NULL; 27532 struct uscsi_cmd *com = NULL; 27533 struct cdrom_subcode *subcode = NULL; 27534 int rval; 27535 size_t buflen; 27536 char cdb[CDB_GROUP5]; 27537 27538 #ifdef _MULTI_DATAMODEL 27539 /* To support ILP32 applications in an LP64 world */ 27540 struct cdrom_subcode32 cdrom_subcode32; 27541 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27542 #endif 27543 if (data == NULL) { 27544 return (EINVAL); 27545 } 27546 27547 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27548 return (ENXIO); 27549 } 27550 27551 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27552 27553 #ifdef _MULTI_DATAMODEL 27554 switch (ddi_model_convert_from(flag & FMODELS)) { 27555 case DDI_MODEL_ILP32: 27556 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27557 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27558 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27559 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27560 return (EFAULT); 27561 } 27562 /* Convert the ILP32 uscsi data from the application to LP64 */ 27563 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27564 break; 27565 case DDI_MODEL_NONE: 27566 if (ddi_copyin(data, subcode, 27567 sizeof (struct cdrom_subcode), flag)) { 27568 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27569 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27570 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27571 return (EFAULT); 27572 } 27573 break; 27574 } 27575 #else /* ! _MULTI_DATAMODEL */ 27576 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27577 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27578 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27579 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27580 return (EFAULT); 27581 } 27582 #endif /* _MULTI_DATAMODEL */ 27583 27584 /* 27585 * Since MMC-2 expects max 3 bytes for length, check if the 27586 * length input is greater than 3 bytes 27587 */ 27588 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27590 "sr_read_all_subcodes: " 27591 "cdrom transfer length too large: %d (limit %d)\n", 27592 subcode->cdsc_length, 0xFFFFFF); 27593 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27594 return (EINVAL); 27595 } 27596 27597 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27598 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27599 bzero(cdb, CDB_GROUP5); 27600 27601 if (un->un_f_mmc_cap == TRUE) { 27602 cdb[0] = (char)SCMD_READ_CD; 27603 cdb[2] = (char)0xff; 27604 cdb[3] = (char)0xff; 27605 cdb[4] = (char)0xff; 27606 cdb[5] = (char)0xff; 27607 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27608 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27609 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27610 cdb[10] = 1; 27611 } else { 27612 /* 27613 * Note: A vendor specific command (0xDF) is being used her to 27614 * request a read of all subcodes. 27615 */ 27616 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27617 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27618 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27619 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27620 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27621 } 27622 com->uscsi_cdb = cdb; 27623 com->uscsi_cdblen = CDB_GROUP5; 27624 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27625 com->uscsi_buflen = buflen; 27626 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27627 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27628 SD_PATH_STANDARD); 27629 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27630 kmem_free(com, sizeof (*com)); 27631 return (rval); 27632 } 27633 27634 27635 /* 27636 * Function: sr_read_subchannel() 27637 * 27638 * Description: This routine is the driver entry point for handling CD-ROM 27639 * ioctl requests to return the Q sub-channel data of the CD 27640 * current position block. (CDROMSUBCHNL) The data includes the 27641 * track number, index number, absolute CD-ROM address (LBA or MSF 27642 * format per the user) , track relative CD-ROM address (LBA or MSF 27643 * format per the user), control data and audio status. 27644 * 27645 * Arguments: dev - the device 'dev_t' 27646 * data - pointer to user provided cdrom sub-channel structure 27647 * flag - this argument is a pass through to ddi_copyxxx() 27648 * directly from the mode argument of ioctl(). 27649 * 27650 * Return Code: the code returned by sd_send_scsi_cmd() 27651 * EFAULT if ddi_copyxxx() fails 27652 * ENXIO if fail ddi_get_soft_state 27653 * EINVAL if data pointer is NULL 27654 */ 27655 27656 static int 27657 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27658 { 27659 struct sd_lun *un; 27660 struct uscsi_cmd *com; 27661 struct cdrom_subchnl subchanel; 27662 struct cdrom_subchnl *subchnl = &subchanel; 27663 char cdb[CDB_GROUP1]; 27664 caddr_t buffer; 27665 int rval; 27666 27667 if (data == NULL) { 27668 return (EINVAL); 27669 } 27670 27671 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27672 (un->un_state == SD_STATE_OFFLINE)) { 27673 return (ENXIO); 27674 } 27675 27676 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27677 return (EFAULT); 27678 } 27679 27680 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27681 bzero(cdb, CDB_GROUP1); 27682 cdb[0] = SCMD_READ_SUBCHANNEL; 27683 /* Set the MSF bit based on the user requested address format */ 27684 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27685 /* 27686 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27687 * returned 27688 */ 27689 cdb[2] = 0x40; 27690 /* 27691 * Set byte 3 to specify the return data format. A value of 0x01 27692 * indicates that the CD-ROM current position should be returned. 27693 */ 27694 cdb[3] = 0x01; 27695 cdb[8] = 0x10; 27696 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27697 com->uscsi_cdb = cdb; 27698 com->uscsi_cdblen = CDB_GROUP1; 27699 com->uscsi_bufaddr = buffer; 27700 com->uscsi_buflen = 16; 27701 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27702 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27703 SD_PATH_STANDARD); 27704 if (rval != 0) { 27705 kmem_free(buffer, 16); 27706 kmem_free(com, sizeof (*com)); 27707 return (rval); 27708 } 27709 27710 /* Process the returned Q sub-channel data */ 27711 subchnl->cdsc_audiostatus = buffer[1]; 27712 subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4; 27713 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27714 subchnl->cdsc_trk = buffer[6]; 27715 subchnl->cdsc_ind = buffer[7]; 27716 if (subchnl->cdsc_format & CDROM_LBA) { 27717 subchnl->cdsc_absaddr.lba = 27718 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27719 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27720 subchnl->cdsc_reladdr.lba = 27721 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27722 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27723 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27724 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27725 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27726 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27727 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27728 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27729 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27730 } else { 27731 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27732 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27733 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27734 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27735 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27736 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27737 } 27738 kmem_free(buffer, 16); 27739 kmem_free(com, sizeof (*com)); 27740 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27741 != 0) { 27742 return (EFAULT); 27743 } 27744 return (rval); 27745 } 27746 27747 27748 /* 27749 * Function: sr_read_tocentry() 27750 * 27751 * Description: This routine is the driver entry point for handling CD-ROM 27752 * ioctl requests to read from the Table of Contents (TOC) 27753 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27754 * fields, the starting address (LBA or MSF format per the user) 27755 * and the data mode if the user specified track is a data track. 27756 * 27757 * Note: The READ HEADER (0x44) command used in this routine is 27758 * obsolete per the SCSI MMC spec but still supported in the 27759 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27760 * therefore the command is still implemented in this routine. 27761 * 27762 * Arguments: dev - the device 'dev_t' 27763 * data - pointer to user provided toc entry structure, 27764 * specifying the track # and the address format 27765 * (LBA or MSF). 27766 * flag - this argument is a pass through to ddi_copyxxx() 27767 * directly from the mode argument of ioctl(). 27768 * 27769 * Return Code: the code returned by sd_send_scsi_cmd() 27770 * EFAULT if ddi_copyxxx() fails 27771 * ENXIO if fail ddi_get_soft_state 27772 * EINVAL if data pointer is NULL 27773 */ 27774 27775 static int 27776 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27777 { 27778 struct sd_lun *un = NULL; 27779 struct uscsi_cmd *com; 27780 struct cdrom_tocentry toc_entry; 27781 struct cdrom_tocentry *entry = &toc_entry; 27782 caddr_t buffer; 27783 int rval; 27784 char cdb[CDB_GROUP1]; 27785 27786 if (data == NULL) { 27787 return (EINVAL); 27788 } 27789 27790 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27791 (un->un_state == SD_STATE_OFFLINE)) { 27792 return (ENXIO); 27793 } 27794 27795 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27796 return (EFAULT); 27797 } 27798 27799 /* Validate the requested track and address format */ 27800 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27801 return (EINVAL); 27802 } 27803 27804 if (entry->cdte_track == 0) { 27805 return (EINVAL); 27806 } 27807 27808 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27809 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27810 bzero(cdb, CDB_GROUP1); 27811 27812 cdb[0] = SCMD_READ_TOC; 27813 /* Set the MSF bit based on the user requested address format */ 27814 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27815 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27816 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27817 } else { 27818 cdb[6] = entry->cdte_track; 27819 } 27820 27821 /* 27822 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27823 * (4 byte TOC response header + 8 byte track descriptor) 27824 */ 27825 cdb[8] = 12; 27826 com->uscsi_cdb = cdb; 27827 com->uscsi_cdblen = CDB_GROUP1; 27828 com->uscsi_bufaddr = buffer; 27829 com->uscsi_buflen = 0x0C; 27830 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27831 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27832 SD_PATH_STANDARD); 27833 if (rval != 0) { 27834 kmem_free(buffer, 12); 27835 kmem_free(com, sizeof (*com)); 27836 return (rval); 27837 } 27838 27839 /* Process the toc entry */ 27840 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27841 entry->cdte_ctrl = (buffer[5] & 0x0F); 27842 if (entry->cdte_format & CDROM_LBA) { 27843 entry->cdte_addr.lba = 27844 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27845 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27846 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27847 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27848 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27849 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27850 /* 27851 * Send a READ TOC command using the LBA address format to get 27852 * the LBA for the track requested so it can be used in the 27853 * READ HEADER request 27854 * 27855 * Note: The MSF bit of the READ HEADER command specifies the 27856 * output format. The block address specified in that command 27857 * must be in LBA format. 27858 */ 27859 cdb[1] = 0; 27860 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27861 SD_PATH_STANDARD); 27862 if (rval != 0) { 27863 kmem_free(buffer, 12); 27864 kmem_free(com, sizeof (*com)); 27865 return (rval); 27866 } 27867 } else { 27868 entry->cdte_addr.msf.minute = buffer[9]; 27869 entry->cdte_addr.msf.second = buffer[10]; 27870 entry->cdte_addr.msf.frame = buffer[11]; 27871 /* 27872 * Send a READ TOC command using the LBA address format to get 27873 * the LBA for the track requested so it can be used in the 27874 * READ HEADER request 27875 * 27876 * Note: The MSF bit of the READ HEADER command specifies the 27877 * output format. The block address specified in that command 27878 * must be in LBA format. 27879 */ 27880 cdb[1] = 0; 27881 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27882 SD_PATH_STANDARD); 27883 if (rval != 0) { 27884 kmem_free(buffer, 12); 27885 kmem_free(com, sizeof (*com)); 27886 return (rval); 27887 } 27888 } 27889 27890 /* 27891 * Build and send the READ HEADER command to determine the data mode of 27892 * the user specified track. 27893 */ 27894 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27895 (entry->cdte_track != CDROM_LEADOUT)) { 27896 bzero(cdb, CDB_GROUP1); 27897 cdb[0] = SCMD_READ_HEADER; 27898 cdb[2] = buffer[8]; 27899 cdb[3] = buffer[9]; 27900 cdb[4] = buffer[10]; 27901 cdb[5] = buffer[11]; 27902 cdb[8] = 0x08; 27903 com->uscsi_buflen = 0x08; 27904 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27905 SD_PATH_STANDARD); 27906 if (rval == 0) { 27907 entry->cdte_datamode = buffer[0]; 27908 } else { 27909 /* 27910 * READ HEADER command failed, since this is 27911 * obsoleted in one spec, its better to return 27912 * -1 for an invlid track so that we can still 27913 * receive the rest of the TOC data. 27914 */ 27915 entry->cdte_datamode = (uchar_t)-1; 27916 } 27917 } else { 27918 entry->cdte_datamode = (uchar_t)-1; 27919 } 27920 27921 kmem_free(buffer, 12); 27922 kmem_free(com, sizeof (*com)); 27923 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27924 return (EFAULT); 27925 27926 return (rval); 27927 } 27928 27929 27930 /* 27931 * Function: sr_read_tochdr() 27932 * 27933 * Description: This routine is the driver entry point for handling CD-ROM 27934 * ioctl requests to read the Table of Contents (TOC) header 27935 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27936 * and ending track numbers 27937 * 27938 * Arguments: dev - the device 'dev_t' 27939 * data - pointer to user provided toc header structure, 27940 * specifying the starting and ending track numbers. 27941 * flag - this argument is a pass through to ddi_copyxxx() 27942 * directly from the mode argument of ioctl(). 27943 * 27944 * Return Code: the code returned by sd_send_scsi_cmd() 27945 * EFAULT if ddi_copyxxx() fails 27946 * ENXIO if fail ddi_get_soft_state 27947 * EINVAL if data pointer is NULL 27948 */ 27949 27950 static int 27951 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27952 { 27953 struct sd_lun *un; 27954 struct uscsi_cmd *com; 27955 struct cdrom_tochdr toc_header; 27956 struct cdrom_tochdr *hdr = &toc_header; 27957 char cdb[CDB_GROUP1]; 27958 int rval; 27959 caddr_t buffer; 27960 27961 if (data == NULL) { 27962 return (EINVAL); 27963 } 27964 27965 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27966 (un->un_state == SD_STATE_OFFLINE)) { 27967 return (ENXIO); 27968 } 27969 27970 buffer = kmem_zalloc(4, KM_SLEEP); 27971 bzero(cdb, CDB_GROUP1); 27972 cdb[0] = SCMD_READ_TOC; 27973 /* 27974 * Specifying a track number of 0x00 in the READ TOC command indicates 27975 * that the TOC header should be returned 27976 */ 27977 cdb[6] = 0x00; 27978 /* 27979 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 27980 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 27981 */ 27982 cdb[8] = 0x04; 27983 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27984 com->uscsi_cdb = cdb; 27985 com->uscsi_cdblen = CDB_GROUP1; 27986 com->uscsi_bufaddr = buffer; 27987 com->uscsi_buflen = 0x04; 27988 com->uscsi_timeout = 300; 27989 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27990 27991 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27992 SD_PATH_STANDARD); 27993 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27994 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 27995 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 27996 } else { 27997 hdr->cdth_trk0 = buffer[2]; 27998 hdr->cdth_trk1 = buffer[3]; 27999 } 28000 kmem_free(buffer, 4); 28001 kmem_free(com, sizeof (*com)); 28002 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28003 return (EFAULT); 28004 } 28005 return (rval); 28006 } 28007 28008 28009 /* 28010 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28011 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28012 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28013 * digital audio and extended architecture digital audio. These modes are 28014 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28015 * MMC specs. 28016 * 28017 * In addition to support for the various data formats these routines also 28018 * include support for devices that implement only the direct access READ 28019 * commands (0x08, 0x28), devices that implement the READ_CD commands 28020 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28021 * READ CDXA commands (0xD8, 0xDB) 28022 */ 28023 28024 /* 28025 * Function: sr_read_mode1() 28026 * 28027 * Description: This routine is the driver entry point for handling CD-ROM 28028 * ioctl read mode1 requests (CDROMREADMODE1). 28029 * 28030 * Arguments: dev - the device 'dev_t' 28031 * data - pointer to user provided cd read structure specifying 28032 * the lba buffer address and length. 28033 * flag - this argument is a pass through to ddi_copyxxx() 28034 * directly from the mode argument of ioctl(). 28035 * 28036 * Return Code: the code returned by sd_send_scsi_cmd() 28037 * EFAULT if ddi_copyxxx() fails 28038 * ENXIO if fail ddi_get_soft_state 28039 * EINVAL if data pointer is NULL 28040 */ 28041 28042 static int 28043 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28044 { 28045 struct sd_lun *un; 28046 struct cdrom_read mode1_struct; 28047 struct cdrom_read *mode1 = &mode1_struct; 28048 int rval; 28049 sd_ssc_t *ssc; 28050 28051 #ifdef _MULTI_DATAMODEL 28052 /* To support ILP32 applications in an LP64 world */ 28053 struct cdrom_read32 cdrom_read32; 28054 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28055 #endif /* _MULTI_DATAMODEL */ 28056 28057 if (data == NULL) { 28058 return (EINVAL); 28059 } 28060 28061 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28062 (un->un_state == SD_STATE_OFFLINE)) { 28063 return (ENXIO); 28064 } 28065 28066 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28067 "sd_read_mode1: entry: un:0x%p\n", un); 28068 28069 #ifdef _MULTI_DATAMODEL 28070 switch (ddi_model_convert_from(flag & FMODELS)) { 28071 case DDI_MODEL_ILP32: 28072 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28073 return (EFAULT); 28074 } 28075 /* Convert the ILP32 uscsi data from the application to LP64 */ 28076 cdrom_read32tocdrom_read(cdrd32, mode1); 28077 break; 28078 case DDI_MODEL_NONE: 28079 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28080 return (EFAULT); 28081 } 28082 } 28083 #else /* ! _MULTI_DATAMODEL */ 28084 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28085 return (EFAULT); 28086 } 28087 #endif /* _MULTI_DATAMODEL */ 28088 28089 ssc = sd_ssc_init(un); 28090 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr, 28091 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28092 sd_ssc_fini(ssc); 28093 28094 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28095 "sd_read_mode1: exit: un:0x%p\n", un); 28096 28097 return (rval); 28098 } 28099 28100 28101 /* 28102 * Function: sr_read_cd_mode2() 28103 * 28104 * Description: This routine is the driver entry point for handling CD-ROM 28105 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28106 * support the READ CD (0xBE) command or the 1st generation 28107 * READ CD (0xD4) command. 28108 * 28109 * Arguments: dev - the device 'dev_t' 28110 * data - pointer to user provided cd read structure specifying 28111 * the lba buffer address and length. 28112 * flag - this argument is a pass through to ddi_copyxxx() 28113 * directly from the mode argument of ioctl(). 28114 * 28115 * Return Code: the code returned by sd_send_scsi_cmd() 28116 * EFAULT if ddi_copyxxx() fails 28117 * ENXIO if fail ddi_get_soft_state 28118 * EINVAL if data pointer is NULL 28119 */ 28120 28121 static int 28122 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28123 { 28124 struct sd_lun *un; 28125 struct uscsi_cmd *com; 28126 struct cdrom_read mode2_struct; 28127 struct cdrom_read *mode2 = &mode2_struct; 28128 uchar_t cdb[CDB_GROUP5]; 28129 int nblocks; 28130 int rval; 28131 #ifdef _MULTI_DATAMODEL 28132 /* To support ILP32 applications in an LP64 world */ 28133 struct cdrom_read32 cdrom_read32; 28134 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28135 #endif /* _MULTI_DATAMODEL */ 28136 28137 if (data == NULL) { 28138 return (EINVAL); 28139 } 28140 28141 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28142 (un->un_state == SD_STATE_OFFLINE)) { 28143 return (ENXIO); 28144 } 28145 28146 #ifdef _MULTI_DATAMODEL 28147 switch (ddi_model_convert_from(flag & FMODELS)) { 28148 case DDI_MODEL_ILP32: 28149 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28150 return (EFAULT); 28151 } 28152 /* Convert the ILP32 uscsi data from the application to LP64 */ 28153 cdrom_read32tocdrom_read(cdrd32, mode2); 28154 break; 28155 case DDI_MODEL_NONE: 28156 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28157 return (EFAULT); 28158 } 28159 break; 28160 } 28161 28162 #else /* ! _MULTI_DATAMODEL */ 28163 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28164 return (EFAULT); 28165 } 28166 #endif /* _MULTI_DATAMODEL */ 28167 28168 bzero(cdb, sizeof (cdb)); 28169 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28170 /* Read command supported by 1st generation atapi drives */ 28171 cdb[0] = SCMD_READ_CDD4; 28172 } else { 28173 /* Universal CD Access Command */ 28174 cdb[0] = SCMD_READ_CD; 28175 } 28176 28177 /* 28178 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28179 */ 28180 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28181 28182 /* set the start address */ 28183 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28184 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28185 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28186 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28187 28188 /* set the transfer length */ 28189 nblocks = mode2->cdread_buflen / 2336; 28190 cdb[6] = (uchar_t)(nblocks >> 16); 28191 cdb[7] = (uchar_t)(nblocks >> 8); 28192 cdb[8] = (uchar_t)nblocks; 28193 28194 /* set the filter bits */ 28195 cdb[9] = CDROM_READ_CD_USERDATA; 28196 28197 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28198 com->uscsi_cdb = (caddr_t)cdb; 28199 com->uscsi_cdblen = sizeof (cdb); 28200 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28201 com->uscsi_buflen = mode2->cdread_buflen; 28202 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28203 28204 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28205 SD_PATH_STANDARD); 28206 kmem_free(com, sizeof (*com)); 28207 return (rval); 28208 } 28209 28210 28211 /* 28212 * Function: sr_read_mode2() 28213 * 28214 * Description: This routine is the driver entry point for handling CD-ROM 28215 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28216 * do not support the READ CD (0xBE) command. 28217 * 28218 * Arguments: dev - the device 'dev_t' 28219 * data - pointer to user provided cd read structure specifying 28220 * the lba buffer address and length. 28221 * flag - this argument is a pass through to ddi_copyxxx() 28222 * directly from the mode argument of ioctl(). 28223 * 28224 * Return Code: the code returned by sd_send_scsi_cmd() 28225 * EFAULT if ddi_copyxxx() fails 28226 * ENXIO if fail ddi_get_soft_state 28227 * EINVAL if data pointer is NULL 28228 * EIO if fail to reset block size 28229 * EAGAIN if commands are in progress in the driver 28230 */ 28231 28232 static int 28233 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28234 { 28235 struct sd_lun *un; 28236 struct cdrom_read mode2_struct; 28237 struct cdrom_read *mode2 = &mode2_struct; 28238 int rval; 28239 uint32_t restore_blksize; 28240 struct uscsi_cmd *com; 28241 uchar_t cdb[CDB_GROUP0]; 28242 int nblocks; 28243 28244 #ifdef _MULTI_DATAMODEL 28245 /* To support ILP32 applications in an LP64 world */ 28246 struct cdrom_read32 cdrom_read32; 28247 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28248 #endif /* _MULTI_DATAMODEL */ 28249 28250 if (data == NULL) { 28251 return (EINVAL); 28252 } 28253 28254 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28255 (un->un_state == SD_STATE_OFFLINE)) { 28256 return (ENXIO); 28257 } 28258 28259 /* 28260 * Because this routine will update the device and driver block size 28261 * being used we want to make sure there are no commands in progress. 28262 * If commands are in progress the user will have to try again. 28263 * 28264 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28265 * in sdioctl to protect commands from sdioctl through to the top of 28266 * sd_uscsi_strategy. See sdioctl for details. 28267 */ 28268 mutex_enter(SD_MUTEX(un)); 28269 if (un->un_ncmds_in_driver != 1) { 28270 mutex_exit(SD_MUTEX(un)); 28271 return (EAGAIN); 28272 } 28273 mutex_exit(SD_MUTEX(un)); 28274 28275 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28276 "sd_read_mode2: entry: un:0x%p\n", un); 28277 28278 #ifdef _MULTI_DATAMODEL 28279 switch (ddi_model_convert_from(flag & FMODELS)) { 28280 case DDI_MODEL_ILP32: 28281 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28282 return (EFAULT); 28283 } 28284 /* Convert the ILP32 uscsi data from the application to LP64 */ 28285 cdrom_read32tocdrom_read(cdrd32, mode2); 28286 break; 28287 case DDI_MODEL_NONE: 28288 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28289 return (EFAULT); 28290 } 28291 break; 28292 } 28293 #else /* ! _MULTI_DATAMODEL */ 28294 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28295 return (EFAULT); 28296 } 28297 #endif /* _MULTI_DATAMODEL */ 28298 28299 /* Store the current target block size for restoration later */ 28300 restore_blksize = un->un_tgt_blocksize; 28301 28302 /* Change the device and soft state target block size to 2336 */ 28303 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28304 rval = EIO; 28305 goto done; 28306 } 28307 28308 28309 bzero(cdb, sizeof (cdb)); 28310 28311 /* set READ operation */ 28312 cdb[0] = SCMD_READ; 28313 28314 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28315 mode2->cdread_lba >>= 2; 28316 28317 /* set the start address */ 28318 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28319 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28320 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28321 28322 /* set the transfer length */ 28323 nblocks = mode2->cdread_buflen / 2336; 28324 cdb[4] = (uchar_t)nblocks & 0xFF; 28325 28326 /* build command */ 28327 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28328 com->uscsi_cdb = (caddr_t)cdb; 28329 com->uscsi_cdblen = sizeof (cdb); 28330 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28331 com->uscsi_buflen = mode2->cdread_buflen; 28332 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28333 28334 /* 28335 * Issue SCSI command with user space address for read buffer. 28336 * 28337 * This sends the command through main channel in the driver. 28338 * 28339 * Since this is accessed via an IOCTL call, we go through the 28340 * standard path, so that if the device was powered down, then 28341 * it would be 'awakened' to handle the command. 28342 */ 28343 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28344 SD_PATH_STANDARD); 28345 28346 kmem_free(com, sizeof (*com)); 28347 28348 /* Restore the device and soft state target block size */ 28349 if (sr_sector_mode(dev, restore_blksize) != 0) { 28350 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28351 "can't do switch back to mode 1\n"); 28352 /* 28353 * If sd_send_scsi_READ succeeded we still need to report 28354 * an error because we failed to reset the block size 28355 */ 28356 if (rval == 0) { 28357 rval = EIO; 28358 } 28359 } 28360 28361 done: 28362 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28363 "sd_read_mode2: exit: un:0x%p\n", un); 28364 28365 return (rval); 28366 } 28367 28368 28369 /* 28370 * Function: sr_sector_mode() 28371 * 28372 * Description: This utility function is used by sr_read_mode2 to set the target 28373 * block size based on the user specified size. This is a legacy 28374 * implementation based upon a vendor specific mode page 28375 * 28376 * Arguments: dev - the device 'dev_t' 28377 * data - flag indicating if block size is being set to 2336 or 28378 * 512. 28379 * 28380 * Return Code: the code returned by sd_send_scsi_cmd() 28381 * EFAULT if ddi_copyxxx() fails 28382 * ENXIO if fail ddi_get_soft_state 28383 * EINVAL if data pointer is NULL 28384 */ 28385 28386 static int 28387 sr_sector_mode(dev_t dev, uint32_t blksize) 28388 { 28389 struct sd_lun *un; 28390 uchar_t *sense; 28391 uchar_t *select; 28392 int rval; 28393 sd_ssc_t *ssc; 28394 28395 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28396 (un->un_state == SD_STATE_OFFLINE)) { 28397 return (ENXIO); 28398 } 28399 28400 sense = kmem_zalloc(20, KM_SLEEP); 28401 28402 /* Note: This is a vendor specific mode page (0x81) */ 28403 ssc = sd_ssc_init(un); 28404 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81, 28405 SD_PATH_STANDARD); 28406 sd_ssc_fini(ssc); 28407 if (rval != 0) { 28408 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28409 "sr_sector_mode: Mode Sense failed\n"); 28410 kmem_free(sense, 20); 28411 return (rval); 28412 } 28413 select = kmem_zalloc(20, KM_SLEEP); 28414 select[3] = 0x08; 28415 select[10] = ((blksize >> 8) & 0xff); 28416 select[11] = (blksize & 0xff); 28417 select[12] = 0x01; 28418 select[13] = 0x06; 28419 select[14] = sense[14]; 28420 select[15] = sense[15]; 28421 if (blksize == SD_MODE2_BLKSIZE) { 28422 select[14] |= 0x01; 28423 } 28424 28425 ssc = sd_ssc_init(un); 28426 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20, 28427 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28428 sd_ssc_fini(ssc); 28429 if (rval != 0) { 28430 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28431 "sr_sector_mode: Mode Select failed\n"); 28432 } else { 28433 /* 28434 * Only update the softstate block size if we successfully 28435 * changed the device block mode. 28436 */ 28437 mutex_enter(SD_MUTEX(un)); 28438 sd_update_block_info(un, blksize, 0); 28439 mutex_exit(SD_MUTEX(un)); 28440 } 28441 kmem_free(sense, 20); 28442 kmem_free(select, 20); 28443 return (rval); 28444 } 28445 28446 28447 /* 28448 * Function: sr_read_cdda() 28449 * 28450 * Description: This routine is the driver entry point for handling CD-ROM 28451 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28452 * the target supports CDDA these requests are handled via a vendor 28453 * specific command (0xD8) If the target does not support CDDA 28454 * these requests are handled via the READ CD command (0xBE). 28455 * 28456 * Arguments: dev - the device 'dev_t' 28457 * data - pointer to user provided CD-DA structure specifying 28458 * the track starting address, transfer length, and 28459 * subcode options. 28460 * flag - this argument is a pass through to ddi_copyxxx() 28461 * directly from the mode argument of ioctl(). 28462 * 28463 * Return Code: the code returned by sd_send_scsi_cmd() 28464 * EFAULT if ddi_copyxxx() fails 28465 * ENXIO if fail ddi_get_soft_state 28466 * EINVAL if invalid arguments are provided 28467 * ENOTTY 28468 */ 28469 28470 static int 28471 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28472 { 28473 struct sd_lun *un; 28474 struct uscsi_cmd *com; 28475 struct cdrom_cdda *cdda; 28476 int rval; 28477 size_t buflen; 28478 char cdb[CDB_GROUP5]; 28479 28480 #ifdef _MULTI_DATAMODEL 28481 /* To support ILP32 applications in an LP64 world */ 28482 struct cdrom_cdda32 cdrom_cdda32; 28483 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28484 #endif /* _MULTI_DATAMODEL */ 28485 28486 if (data == NULL) { 28487 return (EINVAL); 28488 } 28489 28490 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28491 return (ENXIO); 28492 } 28493 28494 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28495 28496 #ifdef _MULTI_DATAMODEL 28497 switch (ddi_model_convert_from(flag & FMODELS)) { 28498 case DDI_MODEL_ILP32: 28499 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28500 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28501 "sr_read_cdda: ddi_copyin Failed\n"); 28502 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28503 return (EFAULT); 28504 } 28505 /* Convert the ILP32 uscsi data from the application to LP64 */ 28506 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28507 break; 28508 case DDI_MODEL_NONE: 28509 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28510 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28511 "sr_read_cdda: ddi_copyin Failed\n"); 28512 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28513 return (EFAULT); 28514 } 28515 break; 28516 } 28517 #else /* ! _MULTI_DATAMODEL */ 28518 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28519 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28520 "sr_read_cdda: ddi_copyin Failed\n"); 28521 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28522 return (EFAULT); 28523 } 28524 #endif /* _MULTI_DATAMODEL */ 28525 28526 /* 28527 * Since MMC-2 expects max 3 bytes for length, check if the 28528 * length input is greater than 3 bytes 28529 */ 28530 if ((cdda->cdda_length & 0xFF000000) != 0) { 28531 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28532 "cdrom transfer length too large: %d (limit %d)\n", 28533 cdda->cdda_length, 0xFFFFFF); 28534 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28535 return (EINVAL); 28536 } 28537 28538 switch (cdda->cdda_subcode) { 28539 case CDROM_DA_NO_SUBCODE: 28540 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28541 break; 28542 case CDROM_DA_SUBQ: 28543 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28544 break; 28545 case CDROM_DA_ALL_SUBCODE: 28546 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28547 break; 28548 case CDROM_DA_SUBCODE_ONLY: 28549 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28550 break; 28551 default: 28552 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28553 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28554 cdda->cdda_subcode); 28555 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28556 return (EINVAL); 28557 } 28558 28559 /* Build and send the command */ 28560 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28561 bzero(cdb, CDB_GROUP5); 28562 28563 if (un->un_f_cfg_cdda == TRUE) { 28564 cdb[0] = (char)SCMD_READ_CD; 28565 cdb[1] = 0x04; 28566 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28567 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28568 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28569 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28570 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28571 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28572 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28573 cdb[9] = 0x10; 28574 switch (cdda->cdda_subcode) { 28575 case CDROM_DA_NO_SUBCODE : 28576 cdb[10] = 0x0; 28577 break; 28578 case CDROM_DA_SUBQ : 28579 cdb[10] = 0x2; 28580 break; 28581 case CDROM_DA_ALL_SUBCODE : 28582 cdb[10] = 0x1; 28583 break; 28584 case CDROM_DA_SUBCODE_ONLY : 28585 /* FALLTHROUGH */ 28586 default : 28587 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28588 kmem_free(com, sizeof (*com)); 28589 return (ENOTTY); 28590 } 28591 } else { 28592 cdb[0] = (char)SCMD_READ_CDDA; 28593 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28594 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28595 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28596 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28597 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28598 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28599 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28600 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28601 cdb[10] = cdda->cdda_subcode; 28602 } 28603 28604 com->uscsi_cdb = cdb; 28605 com->uscsi_cdblen = CDB_GROUP5; 28606 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28607 com->uscsi_buflen = buflen; 28608 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28609 28610 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28611 SD_PATH_STANDARD); 28612 28613 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28614 kmem_free(com, sizeof (*com)); 28615 return (rval); 28616 } 28617 28618 28619 /* 28620 * Function: sr_read_cdxa() 28621 * 28622 * Description: This routine is the driver entry point for handling CD-ROM 28623 * ioctl requests to return CD-XA (Extended Architecture) data. 28624 * (CDROMCDXA). 28625 * 28626 * Arguments: dev - the device 'dev_t' 28627 * data - pointer to user provided CD-XA structure specifying 28628 * the data starting address, transfer length, and format 28629 * flag - this argument is a pass through to ddi_copyxxx() 28630 * directly from the mode argument of ioctl(). 28631 * 28632 * Return Code: the code returned by sd_send_scsi_cmd() 28633 * EFAULT if ddi_copyxxx() fails 28634 * ENXIO if fail ddi_get_soft_state 28635 * EINVAL if data pointer is NULL 28636 */ 28637 28638 static int 28639 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28640 { 28641 struct sd_lun *un; 28642 struct uscsi_cmd *com; 28643 struct cdrom_cdxa *cdxa; 28644 int rval; 28645 size_t buflen; 28646 char cdb[CDB_GROUP5]; 28647 uchar_t read_flags; 28648 28649 #ifdef _MULTI_DATAMODEL 28650 /* To support ILP32 applications in an LP64 world */ 28651 struct cdrom_cdxa32 cdrom_cdxa32; 28652 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28653 #endif /* _MULTI_DATAMODEL */ 28654 28655 if (data == NULL) { 28656 return (EINVAL); 28657 } 28658 28659 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28660 return (ENXIO); 28661 } 28662 28663 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28664 28665 #ifdef _MULTI_DATAMODEL 28666 switch (ddi_model_convert_from(flag & FMODELS)) { 28667 case DDI_MODEL_ILP32: 28668 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28669 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28670 return (EFAULT); 28671 } 28672 /* 28673 * Convert the ILP32 uscsi data from the 28674 * application to LP64 for internal use. 28675 */ 28676 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28677 break; 28678 case DDI_MODEL_NONE: 28679 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28680 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28681 return (EFAULT); 28682 } 28683 break; 28684 } 28685 #else /* ! _MULTI_DATAMODEL */ 28686 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28687 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28688 return (EFAULT); 28689 } 28690 #endif /* _MULTI_DATAMODEL */ 28691 28692 /* 28693 * Since MMC-2 expects max 3 bytes for length, check if the 28694 * length input is greater than 3 bytes 28695 */ 28696 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28697 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28698 "cdrom transfer length too large: %d (limit %d)\n", 28699 cdxa->cdxa_length, 0xFFFFFF); 28700 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28701 return (EINVAL); 28702 } 28703 28704 switch (cdxa->cdxa_format) { 28705 case CDROM_XA_DATA: 28706 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28707 read_flags = 0x10; 28708 break; 28709 case CDROM_XA_SECTOR_DATA: 28710 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28711 read_flags = 0xf8; 28712 break; 28713 case CDROM_XA_DATA_W_ERROR: 28714 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28715 read_flags = 0xfc; 28716 break; 28717 default: 28718 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28719 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28720 cdxa->cdxa_format); 28721 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28722 return (EINVAL); 28723 } 28724 28725 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28726 bzero(cdb, CDB_GROUP5); 28727 if (un->un_f_mmc_cap == TRUE) { 28728 cdb[0] = (char)SCMD_READ_CD; 28729 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28730 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28731 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28732 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28733 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28734 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28735 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28736 cdb[9] = (char)read_flags; 28737 } else { 28738 /* 28739 * Note: A vendor specific command (0xDB) is being used her to 28740 * request a read of all subcodes. 28741 */ 28742 cdb[0] = (char)SCMD_READ_CDXA; 28743 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28744 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28745 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28746 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28747 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28748 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28749 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28750 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28751 cdb[10] = cdxa->cdxa_format; 28752 } 28753 com->uscsi_cdb = cdb; 28754 com->uscsi_cdblen = CDB_GROUP5; 28755 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28756 com->uscsi_buflen = buflen; 28757 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28758 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28759 SD_PATH_STANDARD); 28760 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28761 kmem_free(com, sizeof (*com)); 28762 return (rval); 28763 } 28764 28765 28766 /* 28767 * Function: sr_eject() 28768 * 28769 * Description: This routine is the driver entry point for handling CD-ROM 28770 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28771 * 28772 * Arguments: dev - the device 'dev_t' 28773 * 28774 * Return Code: the code returned by sd_send_scsi_cmd() 28775 */ 28776 28777 static int 28778 sr_eject(dev_t dev) 28779 { 28780 struct sd_lun *un; 28781 int rval; 28782 sd_ssc_t *ssc; 28783 28784 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28785 (un->un_state == SD_STATE_OFFLINE)) { 28786 return (ENXIO); 28787 } 28788 28789 /* 28790 * To prevent race conditions with the eject 28791 * command, keep track of an eject command as 28792 * it progresses. If we are already handling 28793 * an eject command in the driver for the given 28794 * unit and another request to eject is received 28795 * immediately return EAGAIN so we don't lose 28796 * the command if the current eject command fails. 28797 */ 28798 mutex_enter(SD_MUTEX(un)); 28799 if (un->un_f_ejecting == TRUE) { 28800 mutex_exit(SD_MUTEX(un)); 28801 return (EAGAIN); 28802 } 28803 un->un_f_ejecting = TRUE; 28804 mutex_exit(SD_MUTEX(un)); 28805 28806 ssc = sd_ssc_init(un); 28807 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 28808 SD_PATH_STANDARD); 28809 sd_ssc_fini(ssc); 28810 28811 if (rval != 0) { 28812 mutex_enter(SD_MUTEX(un)); 28813 un->un_f_ejecting = FALSE; 28814 mutex_exit(SD_MUTEX(un)); 28815 return (rval); 28816 } 28817 28818 ssc = sd_ssc_init(un); 28819 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 28820 SD_TARGET_EJECT, SD_PATH_STANDARD); 28821 sd_ssc_fini(ssc); 28822 28823 if (rval == 0) { 28824 mutex_enter(SD_MUTEX(un)); 28825 sr_ejected(un); 28826 un->un_mediastate = DKIO_EJECTED; 28827 un->un_f_ejecting = FALSE; 28828 cv_broadcast(&un->un_state_cv); 28829 mutex_exit(SD_MUTEX(un)); 28830 } else { 28831 mutex_enter(SD_MUTEX(un)); 28832 un->un_f_ejecting = FALSE; 28833 mutex_exit(SD_MUTEX(un)); 28834 } 28835 return (rval); 28836 } 28837 28838 28839 /* 28840 * Function: sr_ejected() 28841 * 28842 * Description: This routine updates the soft state structure to invalidate the 28843 * geometry information after the media has been ejected or a 28844 * media eject has been detected. 28845 * 28846 * Arguments: un - driver soft state (unit) structure 28847 */ 28848 28849 static void 28850 sr_ejected(struct sd_lun *un) 28851 { 28852 struct sd_errstats *stp; 28853 28854 ASSERT(un != NULL); 28855 ASSERT(mutex_owned(SD_MUTEX(un))); 28856 28857 un->un_f_blockcount_is_valid = FALSE; 28858 un->un_f_tgt_blocksize_is_valid = FALSE; 28859 mutex_exit(SD_MUTEX(un)); 28860 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 28861 mutex_enter(SD_MUTEX(un)); 28862 28863 if (un->un_errstats != NULL) { 28864 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28865 stp->sd_capacity.value.ui64 = 0; 28866 } 28867 } 28868 28869 28870 /* 28871 * Function: sr_check_wp() 28872 * 28873 * Description: This routine checks the write protection of a removable 28874 * media disk and hotpluggable devices via the write protect bit of 28875 * the Mode Page Header device specific field. Some devices choke 28876 * on unsupported mode page. In order to workaround this issue, 28877 * this routine has been implemented to use 0x3f mode page(request 28878 * for all pages) for all device types. 28879 * 28880 * Arguments: dev - the device 'dev_t' 28881 * 28882 * Return Code: int indicating if the device is write protected (1) or not (0) 28883 * 28884 * Context: Kernel thread. 28885 * 28886 */ 28887 28888 static int 28889 sr_check_wp(dev_t dev) 28890 { 28891 struct sd_lun *un; 28892 uchar_t device_specific; 28893 uchar_t *sense; 28894 int hdrlen; 28895 int rval = FALSE; 28896 int status; 28897 sd_ssc_t *ssc; 28898 28899 /* 28900 * Note: The return codes for this routine should be reworked to 28901 * properly handle the case of a NULL softstate. 28902 */ 28903 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28904 return (FALSE); 28905 } 28906 28907 if (un->un_f_cfg_is_atapi == TRUE) { 28908 /* 28909 * The mode page contents are not required; set the allocation 28910 * length for the mode page header only 28911 */ 28912 hdrlen = MODE_HEADER_LENGTH_GRP2; 28913 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28914 ssc = sd_ssc_init(un); 28915 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen, 28916 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28917 sd_ssc_fini(ssc); 28918 if (status != 0) 28919 goto err_exit; 28920 device_specific = 28921 ((struct mode_header_grp2 *)sense)->device_specific; 28922 } else { 28923 hdrlen = MODE_HEADER_LENGTH; 28924 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28925 ssc = sd_ssc_init(un); 28926 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen, 28927 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28928 sd_ssc_fini(ssc); 28929 if (status != 0) 28930 goto err_exit; 28931 device_specific = 28932 ((struct mode_header *)sense)->device_specific; 28933 } 28934 28935 28936 /* 28937 * Write protect mode sense failed; not all disks 28938 * understand this query. Return FALSE assuming that 28939 * these devices are not writable. 28940 */ 28941 if (device_specific & WRITE_PROTECT) { 28942 rval = TRUE; 28943 } 28944 28945 err_exit: 28946 kmem_free(sense, hdrlen); 28947 return (rval); 28948 } 28949 28950 /* 28951 * Function: sr_volume_ctrl() 28952 * 28953 * Description: This routine is the driver entry point for handling CD-ROM 28954 * audio output volume ioctl requests. (CDROMVOLCTRL) 28955 * 28956 * Arguments: dev - the device 'dev_t' 28957 * data - pointer to user audio volume control structure 28958 * flag - this argument is a pass through to ddi_copyxxx() 28959 * directly from the mode argument of ioctl(). 28960 * 28961 * Return Code: the code returned by sd_send_scsi_cmd() 28962 * EFAULT if ddi_copyxxx() fails 28963 * ENXIO if fail ddi_get_soft_state 28964 * EINVAL if data pointer is NULL 28965 * 28966 */ 28967 28968 static int 28969 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28970 { 28971 struct sd_lun *un; 28972 struct cdrom_volctrl volume; 28973 struct cdrom_volctrl *vol = &volume; 28974 uchar_t *sense_page; 28975 uchar_t *select_page; 28976 uchar_t *sense; 28977 uchar_t *select; 28978 int sense_buflen; 28979 int select_buflen; 28980 int rval; 28981 sd_ssc_t *ssc; 28982 28983 if (data == NULL) { 28984 return (EINVAL); 28985 } 28986 28987 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28988 (un->un_state == SD_STATE_OFFLINE)) { 28989 return (ENXIO); 28990 } 28991 28992 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 28993 return (EFAULT); 28994 } 28995 28996 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28997 struct mode_header_grp2 *sense_mhp; 28998 struct mode_header_grp2 *select_mhp; 28999 int bd_len; 29000 29001 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29002 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29003 MODEPAGE_AUDIO_CTRL_LEN; 29004 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29005 select = kmem_zalloc(select_buflen, KM_SLEEP); 29006 ssc = sd_ssc_init(un); 29007 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 29008 sense_buflen, MODEPAGE_AUDIO_CTRL, 29009 SD_PATH_STANDARD); 29010 sd_ssc_fini(ssc); 29011 29012 if (rval != 0) { 29013 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29014 "sr_volume_ctrl: Mode Sense Failed\n"); 29015 kmem_free(sense, sense_buflen); 29016 kmem_free(select, select_buflen); 29017 return (rval); 29018 } 29019 sense_mhp = (struct mode_header_grp2 *)sense; 29020 select_mhp = (struct mode_header_grp2 *)select; 29021 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29022 sense_mhp->bdesc_length_lo; 29023 if (bd_len > MODE_BLK_DESC_LENGTH) { 29024 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29025 "sr_volume_ctrl: Mode Sense returned invalid " 29026 "block descriptor length\n"); 29027 kmem_free(sense, sense_buflen); 29028 kmem_free(select, select_buflen); 29029 return (EIO); 29030 } 29031 sense_page = (uchar_t *) 29032 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29033 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29034 select_mhp->length_msb = 0; 29035 select_mhp->length_lsb = 0; 29036 select_mhp->bdesc_length_hi = 0; 29037 select_mhp->bdesc_length_lo = 0; 29038 } else { 29039 struct mode_header *sense_mhp, *select_mhp; 29040 29041 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29042 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29043 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29044 select = kmem_zalloc(select_buflen, KM_SLEEP); 29045 ssc = sd_ssc_init(un); 29046 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 29047 sense_buflen, MODEPAGE_AUDIO_CTRL, 29048 SD_PATH_STANDARD); 29049 sd_ssc_fini(ssc); 29050 29051 if (rval != 0) { 29052 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29053 "sr_volume_ctrl: Mode Sense Failed\n"); 29054 kmem_free(sense, sense_buflen); 29055 kmem_free(select, select_buflen); 29056 return (rval); 29057 } 29058 sense_mhp = (struct mode_header *)sense; 29059 select_mhp = (struct mode_header *)select; 29060 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29061 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29062 "sr_volume_ctrl: Mode Sense returned invalid " 29063 "block descriptor length\n"); 29064 kmem_free(sense, sense_buflen); 29065 kmem_free(select, select_buflen); 29066 return (EIO); 29067 } 29068 sense_page = (uchar_t *) 29069 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29070 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29071 select_mhp->length = 0; 29072 select_mhp->bdesc_length = 0; 29073 } 29074 /* 29075 * Note: An audio control data structure could be created and overlayed 29076 * on the following in place of the array indexing method implemented. 29077 */ 29078 29079 /* Build the select data for the user volume data */ 29080 select_page[0] = MODEPAGE_AUDIO_CTRL; 29081 select_page[1] = 0xE; 29082 /* Set the immediate bit */ 29083 select_page[2] = 0x04; 29084 /* Zero out reserved fields */ 29085 select_page[3] = 0x00; 29086 select_page[4] = 0x00; 29087 /* Return sense data for fields not to be modified */ 29088 select_page[5] = sense_page[5]; 29089 select_page[6] = sense_page[6]; 29090 select_page[7] = sense_page[7]; 29091 /* Set the user specified volume levels for channel 0 and 1 */ 29092 select_page[8] = 0x01; 29093 select_page[9] = vol->channel0; 29094 select_page[10] = 0x02; 29095 select_page[11] = vol->channel1; 29096 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29097 select_page[12] = sense_page[12]; 29098 select_page[13] = sense_page[13]; 29099 select_page[14] = sense_page[14]; 29100 select_page[15] = sense_page[15]; 29101 29102 ssc = sd_ssc_init(un); 29103 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29104 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select, 29105 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29106 } else { 29107 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 29108 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29109 } 29110 sd_ssc_fini(ssc); 29111 29112 kmem_free(sense, sense_buflen); 29113 kmem_free(select, select_buflen); 29114 return (rval); 29115 } 29116 29117 29118 /* 29119 * Function: sr_read_sony_session_offset() 29120 * 29121 * Description: This routine is the driver entry point for handling CD-ROM 29122 * ioctl requests for session offset information. (CDROMREADOFFSET) 29123 * The address of the first track in the last session of a 29124 * multi-session CD-ROM is returned 29125 * 29126 * Note: This routine uses a vendor specific key value in the 29127 * command control field without implementing any vendor check here 29128 * or in the ioctl routine. 29129 * 29130 * Arguments: dev - the device 'dev_t' 29131 * data - pointer to an int to hold the requested address 29132 * flag - this argument is a pass through to ddi_copyxxx() 29133 * directly from the mode argument of ioctl(). 29134 * 29135 * Return Code: the code returned by sd_send_scsi_cmd() 29136 * EFAULT if ddi_copyxxx() fails 29137 * ENXIO if fail ddi_get_soft_state 29138 * EINVAL if data pointer is NULL 29139 */ 29140 29141 static int 29142 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29143 { 29144 struct sd_lun *un; 29145 struct uscsi_cmd *com; 29146 caddr_t buffer; 29147 char cdb[CDB_GROUP1]; 29148 int session_offset = 0; 29149 int rval; 29150 29151 if (data == NULL) { 29152 return (EINVAL); 29153 } 29154 29155 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29156 (un->un_state == SD_STATE_OFFLINE)) { 29157 return (ENXIO); 29158 } 29159 29160 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29161 bzero(cdb, CDB_GROUP1); 29162 cdb[0] = SCMD_READ_TOC; 29163 /* 29164 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29165 * (4 byte TOC response header + 8 byte response data) 29166 */ 29167 cdb[8] = SONY_SESSION_OFFSET_LEN; 29168 /* Byte 9 is the control byte. A vendor specific value is used */ 29169 cdb[9] = SONY_SESSION_OFFSET_KEY; 29170 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29171 com->uscsi_cdb = cdb; 29172 com->uscsi_cdblen = CDB_GROUP1; 29173 com->uscsi_bufaddr = buffer; 29174 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29175 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29176 29177 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 29178 SD_PATH_STANDARD); 29179 if (rval != 0) { 29180 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29181 kmem_free(com, sizeof (*com)); 29182 return (rval); 29183 } 29184 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29185 session_offset = 29186 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29187 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29188 /* 29189 * Offset returned offset in current lbasize block's. Convert to 29190 * 2k block's to return to the user 29191 */ 29192 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29193 session_offset >>= 2; 29194 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29195 session_offset >>= 1; 29196 } 29197 } 29198 29199 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29200 rval = EFAULT; 29201 } 29202 29203 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29204 kmem_free(com, sizeof (*com)); 29205 return (rval); 29206 } 29207 29208 29209 /* 29210 * Function: sd_wm_cache_constructor() 29211 * 29212 * Description: Cache Constructor for the wmap cache for the read/modify/write 29213 * devices. 29214 * 29215 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29216 * un - sd_lun structure for the device. 29217 * flag - the km flags passed to constructor 29218 * 29219 * Return Code: 0 on success. 29220 * -1 on failure. 29221 */ 29222 29223 /*ARGSUSED*/ 29224 static int 29225 sd_wm_cache_constructor(void *wm, void *un, int flags) 29226 { 29227 bzero(wm, sizeof (struct sd_w_map)); 29228 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29229 return (0); 29230 } 29231 29232 29233 /* 29234 * Function: sd_wm_cache_destructor() 29235 * 29236 * Description: Cache destructor for the wmap cache for the read/modify/write 29237 * devices. 29238 * 29239 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29240 * un - sd_lun structure for the device. 29241 */ 29242 /*ARGSUSED*/ 29243 static void 29244 sd_wm_cache_destructor(void *wm, void *un) 29245 { 29246 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29247 } 29248 29249 29250 /* 29251 * Function: sd_range_lock() 29252 * 29253 * Description: Lock the range of blocks specified as parameter to ensure 29254 * that read, modify write is atomic and no other i/o writes 29255 * to the same location. The range is specified in terms 29256 * of start and end blocks. Block numbers are the actual 29257 * media block numbers and not system. 29258 * 29259 * Arguments: un - sd_lun structure for the device. 29260 * startb - The starting block number 29261 * endb - The end block number 29262 * typ - type of i/o - simple/read_modify_write 29263 * 29264 * Return Code: wm - pointer to the wmap structure. 29265 * 29266 * Context: This routine can sleep. 29267 */ 29268 29269 static struct sd_w_map * 29270 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29271 { 29272 struct sd_w_map *wmp = NULL; 29273 struct sd_w_map *sl_wmp = NULL; 29274 struct sd_w_map *tmp_wmp; 29275 wm_state state = SD_WM_CHK_LIST; 29276 29277 29278 ASSERT(un != NULL); 29279 ASSERT(!mutex_owned(SD_MUTEX(un))); 29280 29281 mutex_enter(SD_MUTEX(un)); 29282 29283 while (state != SD_WM_DONE) { 29284 29285 switch (state) { 29286 case SD_WM_CHK_LIST: 29287 /* 29288 * This is the starting state. Check the wmap list 29289 * to see if the range is currently available. 29290 */ 29291 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29292 /* 29293 * If this is a simple write and no rmw 29294 * i/o is pending then try to lock the 29295 * range as the range should be available. 29296 */ 29297 state = SD_WM_LOCK_RANGE; 29298 } else { 29299 tmp_wmp = sd_get_range(un, startb, endb); 29300 if (tmp_wmp != NULL) { 29301 if ((wmp != NULL) && ONLIST(un, wmp)) { 29302 /* 29303 * Should not keep onlist wmps 29304 * while waiting this macro 29305 * will also do wmp = NULL; 29306 */ 29307 FREE_ONLIST_WMAP(un, wmp); 29308 } 29309 /* 29310 * sl_wmp is the wmap on which wait 29311 * is done, since the tmp_wmp points 29312 * to the inuse wmap, set sl_wmp to 29313 * tmp_wmp and change the state to sleep 29314 */ 29315 sl_wmp = tmp_wmp; 29316 state = SD_WM_WAIT_MAP; 29317 } else { 29318 state = SD_WM_LOCK_RANGE; 29319 } 29320 29321 } 29322 break; 29323 29324 case SD_WM_LOCK_RANGE: 29325 ASSERT(un->un_wm_cache); 29326 /* 29327 * The range need to be locked, try to get a wmap. 29328 * First attempt it with NO_SLEEP, want to avoid a sleep 29329 * if possible as we will have to release the sd mutex 29330 * if we have to sleep. 29331 */ 29332 if (wmp == NULL) 29333 wmp = kmem_cache_alloc(un->un_wm_cache, 29334 KM_NOSLEEP); 29335 if (wmp == NULL) { 29336 mutex_exit(SD_MUTEX(un)); 29337 _NOTE(DATA_READABLE_WITHOUT_LOCK 29338 (sd_lun::un_wm_cache)) 29339 wmp = kmem_cache_alloc(un->un_wm_cache, 29340 KM_SLEEP); 29341 mutex_enter(SD_MUTEX(un)); 29342 /* 29343 * we released the mutex so recheck and go to 29344 * check list state. 29345 */ 29346 state = SD_WM_CHK_LIST; 29347 } else { 29348 /* 29349 * We exit out of state machine since we 29350 * have the wmap. Do the housekeeping first. 29351 * place the wmap on the wmap list if it is not 29352 * on it already and then set the state to done. 29353 */ 29354 wmp->wm_start = startb; 29355 wmp->wm_end = endb; 29356 wmp->wm_flags = typ | SD_WM_BUSY; 29357 if (typ & SD_WTYPE_RMW) { 29358 un->un_rmw_count++; 29359 } 29360 /* 29361 * If not already on the list then link 29362 */ 29363 if (!ONLIST(un, wmp)) { 29364 wmp->wm_next = un->un_wm; 29365 wmp->wm_prev = NULL; 29366 if (wmp->wm_next) 29367 wmp->wm_next->wm_prev = wmp; 29368 un->un_wm = wmp; 29369 } 29370 state = SD_WM_DONE; 29371 } 29372 break; 29373 29374 case SD_WM_WAIT_MAP: 29375 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29376 /* 29377 * Wait is done on sl_wmp, which is set in the 29378 * check_list state. 29379 */ 29380 sl_wmp->wm_wanted_count++; 29381 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29382 sl_wmp->wm_wanted_count--; 29383 /* 29384 * We can reuse the memory from the completed sl_wmp 29385 * lock range for our new lock, but only if noone is 29386 * waiting for it. 29387 */ 29388 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29389 if (sl_wmp->wm_wanted_count == 0) { 29390 if (wmp != NULL) 29391 CHK_N_FREEWMP(un, wmp); 29392 wmp = sl_wmp; 29393 } 29394 sl_wmp = NULL; 29395 /* 29396 * After waking up, need to recheck for availability of 29397 * range. 29398 */ 29399 state = SD_WM_CHK_LIST; 29400 break; 29401 29402 default: 29403 panic("sd_range_lock: " 29404 "Unknown state %d in sd_range_lock", state); 29405 /*NOTREACHED*/ 29406 } /* switch(state) */ 29407 29408 } /* while(state != SD_WM_DONE) */ 29409 29410 mutex_exit(SD_MUTEX(un)); 29411 29412 ASSERT(wmp != NULL); 29413 29414 return (wmp); 29415 } 29416 29417 29418 /* 29419 * Function: sd_get_range() 29420 * 29421 * Description: Find if there any overlapping I/O to this one 29422 * Returns the write-map of 1st such I/O, NULL otherwise. 29423 * 29424 * Arguments: un - sd_lun structure for the device. 29425 * startb - The starting block number 29426 * endb - The end block number 29427 * 29428 * Return Code: wm - pointer to the wmap structure. 29429 */ 29430 29431 static struct sd_w_map * 29432 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29433 { 29434 struct sd_w_map *wmp; 29435 29436 ASSERT(un != NULL); 29437 29438 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29439 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29440 continue; 29441 } 29442 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29443 break; 29444 } 29445 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29446 break; 29447 } 29448 } 29449 29450 return (wmp); 29451 } 29452 29453 29454 /* 29455 * Function: sd_free_inlist_wmap() 29456 * 29457 * Description: Unlink and free a write map struct. 29458 * 29459 * Arguments: un - sd_lun structure for the device. 29460 * wmp - sd_w_map which needs to be unlinked. 29461 */ 29462 29463 static void 29464 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29465 { 29466 ASSERT(un != NULL); 29467 29468 if (un->un_wm == wmp) { 29469 un->un_wm = wmp->wm_next; 29470 } else { 29471 wmp->wm_prev->wm_next = wmp->wm_next; 29472 } 29473 29474 if (wmp->wm_next) { 29475 wmp->wm_next->wm_prev = wmp->wm_prev; 29476 } 29477 29478 wmp->wm_next = wmp->wm_prev = NULL; 29479 29480 kmem_cache_free(un->un_wm_cache, wmp); 29481 } 29482 29483 29484 /* 29485 * Function: sd_range_unlock() 29486 * 29487 * Description: Unlock the range locked by wm. 29488 * Free write map if nobody else is waiting on it. 29489 * 29490 * Arguments: un - sd_lun structure for the device. 29491 * wmp - sd_w_map which needs to be unlinked. 29492 */ 29493 29494 static void 29495 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29496 { 29497 ASSERT(un != NULL); 29498 ASSERT(wm != NULL); 29499 ASSERT(!mutex_owned(SD_MUTEX(un))); 29500 29501 mutex_enter(SD_MUTEX(un)); 29502 29503 if (wm->wm_flags & SD_WTYPE_RMW) { 29504 un->un_rmw_count--; 29505 } 29506 29507 if (wm->wm_wanted_count) { 29508 wm->wm_flags = 0; 29509 /* 29510 * Broadcast that the wmap is available now. 29511 */ 29512 cv_broadcast(&wm->wm_avail); 29513 } else { 29514 /* 29515 * If no one is waiting on the map, it should be free'ed. 29516 */ 29517 sd_free_inlist_wmap(un, wm); 29518 } 29519 29520 mutex_exit(SD_MUTEX(un)); 29521 } 29522 29523 29524 /* 29525 * Function: sd_read_modify_write_task 29526 * 29527 * Description: Called from a taskq thread to initiate the write phase of 29528 * a read-modify-write request. This is used for targets where 29529 * un->un_sys_blocksize != un->un_tgt_blocksize. 29530 * 29531 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29532 * 29533 * Context: Called under taskq thread context. 29534 */ 29535 29536 static void 29537 sd_read_modify_write_task(void *arg) 29538 { 29539 struct sd_mapblocksize_info *bsp; 29540 struct buf *bp; 29541 struct sd_xbuf *xp; 29542 struct sd_lun *un; 29543 29544 bp = arg; /* The bp is given in arg */ 29545 ASSERT(bp != NULL); 29546 29547 /* Get the pointer to the layer-private data struct */ 29548 xp = SD_GET_XBUF(bp); 29549 ASSERT(xp != NULL); 29550 bsp = xp->xb_private; 29551 ASSERT(bsp != NULL); 29552 29553 un = SD_GET_UN(bp); 29554 ASSERT(un != NULL); 29555 ASSERT(!mutex_owned(SD_MUTEX(un))); 29556 29557 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29558 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29559 29560 /* 29561 * This is the write phase of a read-modify-write request, called 29562 * under the context of a taskq thread in response to the completion 29563 * of the read portion of the rmw request completing under interrupt 29564 * context. The write request must be sent from here down the iostart 29565 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29566 * we use the layer index saved in the layer-private data area. 29567 */ 29568 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29569 29570 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29571 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29572 } 29573 29574 29575 /* 29576 * Function: sddump_do_read_of_rmw() 29577 * 29578 * Description: This routine will be called from sddump, If sddump is called 29579 * with an I/O which not aligned on device blocksize boundary 29580 * then the write has to be converted to read-modify-write. 29581 * Do the read part here in order to keep sddump simple. 29582 * Note - That the sd_mutex is held across the call to this 29583 * routine. 29584 * 29585 * Arguments: un - sd_lun 29586 * blkno - block number in terms of media block size. 29587 * nblk - number of blocks. 29588 * bpp - pointer to pointer to the buf structure. On return 29589 * from this function, *bpp points to the valid buffer 29590 * to which the write has to be done. 29591 * 29592 * Return Code: 0 for success or errno-type return code 29593 */ 29594 29595 static int 29596 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29597 struct buf **bpp) 29598 { 29599 int err; 29600 int i; 29601 int rval; 29602 struct buf *bp; 29603 struct scsi_pkt *pkt = NULL; 29604 uint32_t target_blocksize; 29605 29606 ASSERT(un != NULL); 29607 ASSERT(mutex_owned(SD_MUTEX(un))); 29608 29609 target_blocksize = un->un_tgt_blocksize; 29610 29611 mutex_exit(SD_MUTEX(un)); 29612 29613 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29614 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29615 if (bp == NULL) { 29616 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29617 "no resources for dumping; giving up"); 29618 err = ENOMEM; 29619 goto done; 29620 } 29621 29622 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29623 blkno, nblk); 29624 if (rval != 0) { 29625 scsi_free_consistent_buf(bp); 29626 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29627 "no resources for dumping; giving up"); 29628 err = ENOMEM; 29629 goto done; 29630 } 29631 29632 pkt->pkt_flags |= FLAG_NOINTR; 29633 29634 err = EIO; 29635 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29636 29637 /* 29638 * Scsi_poll returns 0 (success) if the command completes and 29639 * the status block is STATUS_GOOD. We should only check 29640 * errors if this condition is not true. Even then we should 29641 * send our own request sense packet only if we have a check 29642 * condition and auto request sense has not been performed by 29643 * the hba. 29644 */ 29645 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29646 29647 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29648 err = 0; 29649 break; 29650 } 29651 29652 /* 29653 * Check CMD_DEV_GONE 1st, give up if device is gone, 29654 * no need to read RQS data. 29655 */ 29656 if (pkt->pkt_reason == CMD_DEV_GONE) { 29657 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29658 "Error while dumping state with rmw..." 29659 "Device is gone\n"); 29660 break; 29661 } 29662 29663 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29664 SD_INFO(SD_LOG_DUMP, un, 29665 "sddump: read failed with CHECK, try # %d\n", i); 29666 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29667 (void) sd_send_polled_RQS(un); 29668 } 29669 29670 continue; 29671 } 29672 29673 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29674 int reset_retval = 0; 29675 29676 SD_INFO(SD_LOG_DUMP, un, 29677 "sddump: read failed with BUSY, try # %d\n", i); 29678 29679 if (un->un_f_lun_reset_enabled == TRUE) { 29680 reset_retval = scsi_reset(SD_ADDRESS(un), 29681 RESET_LUN); 29682 } 29683 if (reset_retval == 0) { 29684 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29685 } 29686 (void) sd_send_polled_RQS(un); 29687 29688 } else { 29689 SD_INFO(SD_LOG_DUMP, un, 29690 "sddump: read failed with 0x%x, try # %d\n", 29691 SD_GET_PKT_STATUS(pkt), i); 29692 mutex_enter(SD_MUTEX(un)); 29693 sd_reset_target(un, pkt); 29694 mutex_exit(SD_MUTEX(un)); 29695 } 29696 29697 /* 29698 * If we are not getting anywhere with lun/target resets, 29699 * let's reset the bus. 29700 */ 29701 if (i > SD_NDUMP_RETRIES/2) { 29702 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29703 (void) sd_send_polled_RQS(un); 29704 } 29705 29706 } 29707 scsi_destroy_pkt(pkt); 29708 29709 if (err != 0) { 29710 scsi_free_consistent_buf(bp); 29711 *bpp = NULL; 29712 } else { 29713 *bpp = bp; 29714 } 29715 29716 done: 29717 mutex_enter(SD_MUTEX(un)); 29718 return (err); 29719 } 29720 29721 29722 /* 29723 * Function: sd_failfast_flushq 29724 * 29725 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29726 * in b_flags and move them onto the failfast queue, then kick 29727 * off a thread to return all bp's on the failfast queue to 29728 * their owners with an error set. 29729 * 29730 * Arguments: un - pointer to the soft state struct for the instance. 29731 * 29732 * Context: may execute in interrupt context. 29733 */ 29734 29735 static void 29736 sd_failfast_flushq(struct sd_lun *un) 29737 { 29738 struct buf *bp; 29739 struct buf *next_waitq_bp; 29740 struct buf *prev_waitq_bp = NULL; 29741 29742 ASSERT(un != NULL); 29743 ASSERT(mutex_owned(SD_MUTEX(un))); 29744 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29745 ASSERT(un->un_failfast_bp == NULL); 29746 29747 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29748 "sd_failfast_flushq: entry: un:0x%p\n", un); 29749 29750 /* 29751 * Check if we should flush all bufs when entering failfast state, or 29752 * just those with B_FAILFAST set. 29753 */ 29754 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29755 /* 29756 * Move *all* bp's on the wait queue to the failfast flush 29757 * queue, including those that do NOT have B_FAILFAST set. 29758 */ 29759 if (un->un_failfast_headp == NULL) { 29760 ASSERT(un->un_failfast_tailp == NULL); 29761 un->un_failfast_headp = un->un_waitq_headp; 29762 } else { 29763 ASSERT(un->un_failfast_tailp != NULL); 29764 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29765 } 29766 29767 un->un_failfast_tailp = un->un_waitq_tailp; 29768 29769 /* update kstat for each bp moved out of the waitq */ 29770 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29771 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29772 } 29773 29774 /* empty the waitq */ 29775 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29776 29777 } else { 29778 /* 29779 * Go thru the wait queue, pick off all entries with 29780 * B_FAILFAST set, and move these onto the failfast queue. 29781 */ 29782 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29783 /* 29784 * Save the pointer to the next bp on the wait queue, 29785 * so we get to it on the next iteration of this loop. 29786 */ 29787 next_waitq_bp = bp->av_forw; 29788 29789 /* 29790 * If this bp from the wait queue does NOT have 29791 * B_FAILFAST set, just move on to the next element 29792 * in the wait queue. Note, this is the only place 29793 * where it is correct to set prev_waitq_bp. 29794 */ 29795 if ((bp->b_flags & B_FAILFAST) == 0) { 29796 prev_waitq_bp = bp; 29797 continue; 29798 } 29799 29800 /* 29801 * Remove the bp from the wait queue. 29802 */ 29803 if (bp == un->un_waitq_headp) { 29804 /* The bp is the first element of the waitq. */ 29805 un->un_waitq_headp = next_waitq_bp; 29806 if (un->un_waitq_headp == NULL) { 29807 /* The wait queue is now empty */ 29808 un->un_waitq_tailp = NULL; 29809 } 29810 } else { 29811 /* 29812 * The bp is either somewhere in the middle 29813 * or at the end of the wait queue. 29814 */ 29815 ASSERT(un->un_waitq_headp != NULL); 29816 ASSERT(prev_waitq_bp != NULL); 29817 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29818 == 0); 29819 if (bp == un->un_waitq_tailp) { 29820 /* bp is the last entry on the waitq. */ 29821 ASSERT(next_waitq_bp == NULL); 29822 un->un_waitq_tailp = prev_waitq_bp; 29823 } 29824 prev_waitq_bp->av_forw = next_waitq_bp; 29825 } 29826 bp->av_forw = NULL; 29827 29828 /* 29829 * update kstat since the bp is moved out of 29830 * the waitq 29831 */ 29832 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29833 29834 /* 29835 * Now put the bp onto the failfast queue. 29836 */ 29837 if (un->un_failfast_headp == NULL) { 29838 /* failfast queue is currently empty */ 29839 ASSERT(un->un_failfast_tailp == NULL); 29840 un->un_failfast_headp = 29841 un->un_failfast_tailp = bp; 29842 } else { 29843 /* Add the bp to the end of the failfast q */ 29844 ASSERT(un->un_failfast_tailp != NULL); 29845 ASSERT(un->un_failfast_tailp->b_flags & 29846 B_FAILFAST); 29847 un->un_failfast_tailp->av_forw = bp; 29848 un->un_failfast_tailp = bp; 29849 } 29850 } 29851 } 29852 29853 /* 29854 * Now return all bp's on the failfast queue to their owners. 29855 */ 29856 while ((bp = un->un_failfast_headp) != NULL) { 29857 29858 un->un_failfast_headp = bp->av_forw; 29859 if (un->un_failfast_headp == NULL) { 29860 un->un_failfast_tailp = NULL; 29861 } 29862 29863 /* 29864 * We want to return the bp with a failure error code, but 29865 * we do not want a call to sd_start_cmds() to occur here, 29866 * so use sd_return_failed_command_no_restart() instead of 29867 * sd_return_failed_command(). 29868 */ 29869 sd_return_failed_command_no_restart(un, bp, EIO); 29870 } 29871 29872 /* Flush the xbuf queues if required. */ 29873 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29874 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29875 } 29876 29877 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29878 "sd_failfast_flushq: exit: un:0x%p\n", un); 29879 } 29880 29881 29882 /* 29883 * Function: sd_failfast_flushq_callback 29884 * 29885 * Description: Return TRUE if the given bp meets the criteria for failfast 29886 * flushing. Used with ddi_xbuf_flushq(9F). 29887 * 29888 * Arguments: bp - ptr to buf struct to be examined. 29889 * 29890 * Context: Any 29891 */ 29892 29893 static int 29894 sd_failfast_flushq_callback(struct buf *bp) 29895 { 29896 /* 29897 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29898 * state is entered; OR (2) the given bp has B_FAILFAST set. 29899 */ 29900 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29901 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29902 } 29903 29904 29905 29906 /* 29907 * Function: sd_setup_next_xfer 29908 * 29909 * Description: Prepare next I/O operation using DMA_PARTIAL 29910 * 29911 */ 29912 29913 static int 29914 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29915 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29916 { 29917 ssize_t num_blks_not_xfered; 29918 daddr_t strt_blk_num; 29919 ssize_t bytes_not_xfered; 29920 int rval; 29921 29922 ASSERT(pkt->pkt_resid == 0); 29923 29924 /* 29925 * Calculate next block number and amount to be transferred. 29926 * 29927 * How much data NOT transfered to the HBA yet. 29928 */ 29929 bytes_not_xfered = xp->xb_dma_resid; 29930 29931 /* 29932 * figure how many blocks NOT transfered to the HBA yet. 29933 */ 29934 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29935 29936 /* 29937 * set starting block number to the end of what WAS transfered. 29938 */ 29939 strt_blk_num = xp->xb_blkno + 29940 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29941 29942 /* 29943 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29944 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29945 * the disk mutex here. 29946 */ 29947 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29948 strt_blk_num, num_blks_not_xfered); 29949 29950 if (rval == 0) { 29951 29952 /* 29953 * Success. 29954 * 29955 * Adjust things if there are still more blocks to be 29956 * transfered. 29957 */ 29958 xp->xb_dma_resid = pkt->pkt_resid; 29959 pkt->pkt_resid = 0; 29960 29961 return (1); 29962 } 29963 29964 /* 29965 * There's really only one possible return value from 29966 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29967 * returns NULL. 29968 */ 29969 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29970 29971 bp->b_resid = bp->b_bcount; 29972 bp->b_flags |= B_ERROR; 29973 29974 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29975 "Error setting up next portion of DMA transfer\n"); 29976 29977 return (0); 29978 } 29979 29980 /* 29981 * Function: sd_panic_for_res_conflict 29982 * 29983 * Description: Call panic with a string formatted with "Reservation Conflict" 29984 * and a human readable identifier indicating the SD instance 29985 * that experienced the reservation conflict. 29986 * 29987 * Arguments: un - pointer to the soft state struct for the instance. 29988 * 29989 * Context: may execute in interrupt context. 29990 */ 29991 29992 #define SD_RESV_CONFLICT_FMT_LEN 40 29993 void 29994 sd_panic_for_res_conflict(struct sd_lun *un) 29995 { 29996 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 29997 char path_str[MAXPATHLEN]; 29998 29999 (void) snprintf(panic_str, sizeof (panic_str), 30000 "Reservation Conflict\nDisk: %s", 30001 ddi_pathname(SD_DEVINFO(un), path_str)); 30002 30003 panic(panic_str); 30004 } 30005 30006 /* 30007 * Note: The following sd_faultinjection_ioctl( ) routines implement 30008 * driver support for handling fault injection for error analysis 30009 * causing faults in multiple layers of the driver. 30010 * 30011 */ 30012 30013 #ifdef SD_FAULT_INJECTION 30014 static uint_t sd_fault_injection_on = 0; 30015 30016 /* 30017 * Function: sd_faultinjection_ioctl() 30018 * 30019 * Description: This routine is the driver entry point for handling 30020 * faultinjection ioctls to inject errors into the 30021 * layer model 30022 * 30023 * Arguments: cmd - the ioctl cmd received 30024 * arg - the arguments from user and returns 30025 */ 30026 30027 static void 30028 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30029 30030 uint_t i = 0; 30031 uint_t rval; 30032 30033 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30034 30035 mutex_enter(SD_MUTEX(un)); 30036 30037 switch (cmd) { 30038 case SDIOCRUN: 30039 /* Allow pushed faults to be injected */ 30040 SD_INFO(SD_LOG_SDTEST, un, 30041 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30042 30043 sd_fault_injection_on = 1; 30044 30045 SD_INFO(SD_LOG_IOERR, un, 30046 "sd_faultinjection_ioctl: run finished\n"); 30047 break; 30048 30049 case SDIOCSTART: 30050 /* Start Injection Session */ 30051 SD_INFO(SD_LOG_SDTEST, un, 30052 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30053 30054 sd_fault_injection_on = 0; 30055 un->sd_injection_mask = 0xFFFFFFFF; 30056 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30057 un->sd_fi_fifo_pkt[i] = NULL; 30058 un->sd_fi_fifo_xb[i] = NULL; 30059 un->sd_fi_fifo_un[i] = NULL; 30060 un->sd_fi_fifo_arq[i] = NULL; 30061 } 30062 un->sd_fi_fifo_start = 0; 30063 un->sd_fi_fifo_end = 0; 30064 30065 mutex_enter(&(un->un_fi_mutex)); 30066 un->sd_fi_log[0] = '\0'; 30067 un->sd_fi_buf_len = 0; 30068 mutex_exit(&(un->un_fi_mutex)); 30069 30070 SD_INFO(SD_LOG_IOERR, un, 30071 "sd_faultinjection_ioctl: start finished\n"); 30072 break; 30073 30074 case SDIOCSTOP: 30075 /* Stop Injection Session */ 30076 SD_INFO(SD_LOG_SDTEST, un, 30077 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30078 sd_fault_injection_on = 0; 30079 un->sd_injection_mask = 0x0; 30080 30081 /* Empty stray or unuseds structs from fifo */ 30082 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30083 if (un->sd_fi_fifo_pkt[i] != NULL) { 30084 kmem_free(un->sd_fi_fifo_pkt[i], 30085 sizeof (struct sd_fi_pkt)); 30086 } 30087 if (un->sd_fi_fifo_xb[i] != NULL) { 30088 kmem_free(un->sd_fi_fifo_xb[i], 30089 sizeof (struct sd_fi_xb)); 30090 } 30091 if (un->sd_fi_fifo_un[i] != NULL) { 30092 kmem_free(un->sd_fi_fifo_un[i], 30093 sizeof (struct sd_fi_un)); 30094 } 30095 if (un->sd_fi_fifo_arq[i] != NULL) { 30096 kmem_free(un->sd_fi_fifo_arq[i], 30097 sizeof (struct sd_fi_arq)); 30098 } 30099 un->sd_fi_fifo_pkt[i] = NULL; 30100 un->sd_fi_fifo_un[i] = NULL; 30101 un->sd_fi_fifo_xb[i] = NULL; 30102 un->sd_fi_fifo_arq[i] = NULL; 30103 } 30104 un->sd_fi_fifo_start = 0; 30105 un->sd_fi_fifo_end = 0; 30106 30107 SD_INFO(SD_LOG_IOERR, un, 30108 "sd_faultinjection_ioctl: stop finished\n"); 30109 break; 30110 30111 case SDIOCINSERTPKT: 30112 /* Store a packet struct to be pushed onto fifo */ 30113 SD_INFO(SD_LOG_SDTEST, un, 30114 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30115 30116 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30117 30118 sd_fault_injection_on = 0; 30119 30120 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30121 if (un->sd_fi_fifo_pkt[i] != NULL) { 30122 kmem_free(un->sd_fi_fifo_pkt[i], 30123 sizeof (struct sd_fi_pkt)); 30124 } 30125 if (arg != NULL) { 30126 un->sd_fi_fifo_pkt[i] = 30127 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30128 if (un->sd_fi_fifo_pkt[i] == NULL) { 30129 /* Alloc failed don't store anything */ 30130 break; 30131 } 30132 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30133 sizeof (struct sd_fi_pkt), 0); 30134 if (rval == -1) { 30135 kmem_free(un->sd_fi_fifo_pkt[i], 30136 sizeof (struct sd_fi_pkt)); 30137 un->sd_fi_fifo_pkt[i] = NULL; 30138 } 30139 } else { 30140 SD_INFO(SD_LOG_IOERR, un, 30141 "sd_faultinjection_ioctl: pkt null\n"); 30142 } 30143 break; 30144 30145 case SDIOCINSERTXB: 30146 /* Store a xb struct to be pushed onto fifo */ 30147 SD_INFO(SD_LOG_SDTEST, un, 30148 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30149 30150 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30151 30152 sd_fault_injection_on = 0; 30153 30154 if (un->sd_fi_fifo_xb[i] != NULL) { 30155 kmem_free(un->sd_fi_fifo_xb[i], 30156 sizeof (struct sd_fi_xb)); 30157 un->sd_fi_fifo_xb[i] = NULL; 30158 } 30159 if (arg != NULL) { 30160 un->sd_fi_fifo_xb[i] = 30161 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30162 if (un->sd_fi_fifo_xb[i] == NULL) { 30163 /* Alloc failed don't store anything */ 30164 break; 30165 } 30166 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30167 sizeof (struct sd_fi_xb), 0); 30168 30169 if (rval == -1) { 30170 kmem_free(un->sd_fi_fifo_xb[i], 30171 sizeof (struct sd_fi_xb)); 30172 un->sd_fi_fifo_xb[i] = NULL; 30173 } 30174 } else { 30175 SD_INFO(SD_LOG_IOERR, un, 30176 "sd_faultinjection_ioctl: xb null\n"); 30177 } 30178 break; 30179 30180 case SDIOCINSERTUN: 30181 /* Store a un struct to be pushed onto fifo */ 30182 SD_INFO(SD_LOG_SDTEST, un, 30183 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30184 30185 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30186 30187 sd_fault_injection_on = 0; 30188 30189 if (un->sd_fi_fifo_un[i] != NULL) { 30190 kmem_free(un->sd_fi_fifo_un[i], 30191 sizeof (struct sd_fi_un)); 30192 un->sd_fi_fifo_un[i] = NULL; 30193 } 30194 if (arg != NULL) { 30195 un->sd_fi_fifo_un[i] = 30196 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30197 if (un->sd_fi_fifo_un[i] == NULL) { 30198 /* Alloc failed don't store anything */ 30199 break; 30200 } 30201 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30202 sizeof (struct sd_fi_un), 0); 30203 if (rval == -1) { 30204 kmem_free(un->sd_fi_fifo_un[i], 30205 sizeof (struct sd_fi_un)); 30206 un->sd_fi_fifo_un[i] = NULL; 30207 } 30208 30209 } else { 30210 SD_INFO(SD_LOG_IOERR, un, 30211 "sd_faultinjection_ioctl: un null\n"); 30212 } 30213 30214 break; 30215 30216 case SDIOCINSERTARQ: 30217 /* Store a arq struct to be pushed onto fifo */ 30218 SD_INFO(SD_LOG_SDTEST, un, 30219 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30220 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30221 30222 sd_fault_injection_on = 0; 30223 30224 if (un->sd_fi_fifo_arq[i] != NULL) { 30225 kmem_free(un->sd_fi_fifo_arq[i], 30226 sizeof (struct sd_fi_arq)); 30227 un->sd_fi_fifo_arq[i] = NULL; 30228 } 30229 if (arg != NULL) { 30230 un->sd_fi_fifo_arq[i] = 30231 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30232 if (un->sd_fi_fifo_arq[i] == NULL) { 30233 /* Alloc failed don't store anything */ 30234 break; 30235 } 30236 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30237 sizeof (struct sd_fi_arq), 0); 30238 if (rval == -1) { 30239 kmem_free(un->sd_fi_fifo_arq[i], 30240 sizeof (struct sd_fi_arq)); 30241 un->sd_fi_fifo_arq[i] = NULL; 30242 } 30243 30244 } else { 30245 SD_INFO(SD_LOG_IOERR, un, 30246 "sd_faultinjection_ioctl: arq null\n"); 30247 } 30248 30249 break; 30250 30251 case SDIOCPUSH: 30252 /* Push stored xb, pkt, un, and arq onto fifo */ 30253 sd_fault_injection_on = 0; 30254 30255 if (arg != NULL) { 30256 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30257 if (rval != -1 && 30258 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30259 un->sd_fi_fifo_end += i; 30260 } 30261 } else { 30262 SD_INFO(SD_LOG_IOERR, un, 30263 "sd_faultinjection_ioctl: push arg null\n"); 30264 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30265 un->sd_fi_fifo_end++; 30266 } 30267 } 30268 SD_INFO(SD_LOG_IOERR, un, 30269 "sd_faultinjection_ioctl: push to end=%d\n", 30270 un->sd_fi_fifo_end); 30271 break; 30272 30273 case SDIOCRETRIEVE: 30274 /* Return buffer of log from Injection session */ 30275 SD_INFO(SD_LOG_SDTEST, un, 30276 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30277 30278 sd_fault_injection_on = 0; 30279 30280 mutex_enter(&(un->un_fi_mutex)); 30281 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30282 un->sd_fi_buf_len+1, 0); 30283 mutex_exit(&(un->un_fi_mutex)); 30284 30285 if (rval == -1) { 30286 /* 30287 * arg is possibly invalid setting 30288 * it to NULL for return 30289 */ 30290 arg = NULL; 30291 } 30292 break; 30293 } 30294 30295 mutex_exit(SD_MUTEX(un)); 30296 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30297 " exit\n"); 30298 } 30299 30300 30301 /* 30302 * Function: sd_injection_log() 30303 * 30304 * Description: This routine adds buff to the already existing injection log 30305 * for retrieval via faultinjection_ioctl for use in fault 30306 * detection and recovery 30307 * 30308 * Arguments: buf - the string to add to the log 30309 */ 30310 30311 static void 30312 sd_injection_log(char *buf, struct sd_lun *un) 30313 { 30314 uint_t len; 30315 30316 ASSERT(un != NULL); 30317 ASSERT(buf != NULL); 30318 30319 mutex_enter(&(un->un_fi_mutex)); 30320 30321 len = min(strlen(buf), 255); 30322 /* Add logged value to Injection log to be returned later */ 30323 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30324 uint_t offset = strlen((char *)un->sd_fi_log); 30325 char *destp = (char *)un->sd_fi_log + offset; 30326 int i; 30327 for (i = 0; i < len; i++) { 30328 *destp++ = *buf++; 30329 } 30330 un->sd_fi_buf_len += len; 30331 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30332 } 30333 30334 mutex_exit(&(un->un_fi_mutex)); 30335 } 30336 30337 30338 /* 30339 * Function: sd_faultinjection() 30340 * 30341 * Description: This routine takes the pkt and changes its 30342 * content based on error injection scenerio. 30343 * 30344 * Arguments: pktp - packet to be changed 30345 */ 30346 30347 static void 30348 sd_faultinjection(struct scsi_pkt *pktp) 30349 { 30350 uint_t i; 30351 struct sd_fi_pkt *fi_pkt; 30352 struct sd_fi_xb *fi_xb; 30353 struct sd_fi_un *fi_un; 30354 struct sd_fi_arq *fi_arq; 30355 struct buf *bp; 30356 struct sd_xbuf *xb; 30357 struct sd_lun *un; 30358 30359 ASSERT(pktp != NULL); 30360 30361 /* pull bp xb and un from pktp */ 30362 bp = (struct buf *)pktp->pkt_private; 30363 xb = SD_GET_XBUF(bp); 30364 un = SD_GET_UN(bp); 30365 30366 ASSERT(un != NULL); 30367 30368 mutex_enter(SD_MUTEX(un)); 30369 30370 SD_TRACE(SD_LOG_SDTEST, un, 30371 "sd_faultinjection: entry Injection from sdintr\n"); 30372 30373 /* if injection is off return */ 30374 if (sd_fault_injection_on == 0 || 30375 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30376 mutex_exit(SD_MUTEX(un)); 30377 return; 30378 } 30379 30380 SD_INFO(SD_LOG_SDTEST, un, 30381 "sd_faultinjection: is working for copying\n"); 30382 30383 /* take next set off fifo */ 30384 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30385 30386 fi_pkt = un->sd_fi_fifo_pkt[i]; 30387 fi_xb = un->sd_fi_fifo_xb[i]; 30388 fi_un = un->sd_fi_fifo_un[i]; 30389 fi_arq = un->sd_fi_fifo_arq[i]; 30390 30391 30392 /* set variables accordingly */ 30393 /* set pkt if it was on fifo */ 30394 if (fi_pkt != NULL) { 30395 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30396 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30397 if (fi_pkt->pkt_cdbp != 0xff) 30398 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30399 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30400 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30401 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30402 30403 } 30404 /* set xb if it was on fifo */ 30405 if (fi_xb != NULL) { 30406 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30407 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30408 if (fi_xb->xb_retry_count != 0) 30409 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30410 SD_CONDSET(xb, xb, xb_victim_retry_count, 30411 "xb_victim_retry_count"); 30412 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30413 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30414 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30415 30416 /* copy in block data from sense */ 30417 /* 30418 * if (fi_xb->xb_sense_data[0] != -1) { 30419 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30420 * SENSE_LENGTH); 30421 * } 30422 */ 30423 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH); 30424 30425 /* copy in extended sense codes */ 30426 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30427 xb, es_code, "es_code"); 30428 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30429 xb, es_key, "es_key"); 30430 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30431 xb, es_add_code, "es_add_code"); 30432 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30433 xb, es_qual_code, "es_qual_code"); 30434 struct scsi_extended_sense *esp; 30435 esp = (struct scsi_extended_sense *)xb->xb_sense_data; 30436 esp->es_class = CLASS_EXTENDED_SENSE; 30437 } 30438 30439 /* set un if it was on fifo */ 30440 if (fi_un != NULL) { 30441 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30442 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30443 SD_CONDSET(un, un, un_reset_retry_count, 30444 "un_reset_retry_count"); 30445 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30446 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30447 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30448 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30449 "un_f_allow_bus_device_reset"); 30450 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30451 30452 } 30453 30454 /* copy in auto request sense if it was on fifo */ 30455 if (fi_arq != NULL) { 30456 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30457 } 30458 30459 /* free structs */ 30460 if (un->sd_fi_fifo_pkt[i] != NULL) { 30461 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30462 } 30463 if (un->sd_fi_fifo_xb[i] != NULL) { 30464 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30465 } 30466 if (un->sd_fi_fifo_un[i] != NULL) { 30467 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30468 } 30469 if (un->sd_fi_fifo_arq[i] != NULL) { 30470 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30471 } 30472 30473 /* 30474 * kmem_free does not gurantee to set to NULL 30475 * since we uses these to determine if we set 30476 * values or not lets confirm they are always 30477 * NULL after free 30478 */ 30479 un->sd_fi_fifo_pkt[i] = NULL; 30480 un->sd_fi_fifo_un[i] = NULL; 30481 un->sd_fi_fifo_xb[i] = NULL; 30482 un->sd_fi_fifo_arq[i] = NULL; 30483 30484 un->sd_fi_fifo_start++; 30485 30486 mutex_exit(SD_MUTEX(un)); 30487 30488 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30489 } 30490 30491 #endif /* SD_FAULT_INJECTION */ 30492 30493 /* 30494 * This routine is invoked in sd_unit_attach(). Before calling it, the 30495 * properties in conf file should be processed already, and "hotpluggable" 30496 * property was processed also. 30497 * 30498 * The sd driver distinguishes 3 different type of devices: removable media, 30499 * non-removable media, and hotpluggable. Below the differences are defined: 30500 * 30501 * 1. Device ID 30502 * 30503 * The device ID of a device is used to identify this device. Refer to 30504 * ddi_devid_register(9F). 30505 * 30506 * For a non-removable media disk device which can provide 0x80 or 0x83 30507 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30508 * device ID is created to identify this device. For other non-removable 30509 * media devices, a default device ID is created only if this device has 30510 * at least 2 alter cylinders. Otherwise, this device has no devid. 30511 * 30512 * ------------------------------------------------------- 30513 * removable media hotpluggable | Can Have Device ID 30514 * ------------------------------------------------------- 30515 * false false | Yes 30516 * false true | Yes 30517 * true x | No 30518 * ------------------------------------------------------ 30519 * 30520 * 30521 * 2. SCSI group 4 commands 30522 * 30523 * In SCSI specs, only some commands in group 4 command set can use 30524 * 8-byte addresses that can be used to access >2TB storage spaces. 30525 * Other commands have no such capability. Without supporting group4, 30526 * it is impossible to make full use of storage spaces of a disk with 30527 * capacity larger than 2TB. 30528 * 30529 * ----------------------------------------------- 30530 * removable media hotpluggable LP64 | Group 30531 * ----------------------------------------------- 30532 * false false false | 1 30533 * false false true | 4 30534 * false true false | 1 30535 * false true true | 4 30536 * true x x | 5 30537 * ----------------------------------------------- 30538 * 30539 * 30540 * 3. Check for VTOC Label 30541 * 30542 * If a direct-access disk has no EFI label, sd will check if it has a 30543 * valid VTOC label. Now, sd also does that check for removable media 30544 * and hotpluggable devices. 30545 * 30546 * -------------------------------------------------------------- 30547 * Direct-Access removable media hotpluggable | Check Label 30548 * ------------------------------------------------------------- 30549 * false false false | No 30550 * false false true | No 30551 * false true false | Yes 30552 * false true true | Yes 30553 * true x x | Yes 30554 * -------------------------------------------------------------- 30555 * 30556 * 30557 * 4. Building default VTOC label 30558 * 30559 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30560 * If those devices have no valid VTOC label, sd(7d) will attempt to 30561 * create default VTOC for them. Currently sd creates default VTOC label 30562 * for all devices on x86 platform (VTOC_16), but only for removable 30563 * media devices on SPARC (VTOC_8). 30564 * 30565 * ----------------------------------------------------------- 30566 * removable media hotpluggable platform | Default Label 30567 * ----------------------------------------------------------- 30568 * false false sparc | No 30569 * false true x86 | Yes 30570 * false true sparc | Yes 30571 * true x x | Yes 30572 * ---------------------------------------------------------- 30573 * 30574 * 30575 * 5. Supported blocksizes of target devices 30576 * 30577 * Sd supports non-512-byte blocksize for removable media devices only. 30578 * For other devices, only 512-byte blocksize is supported. This may be 30579 * changed in near future because some RAID devices require non-512-byte 30580 * blocksize 30581 * 30582 * ----------------------------------------------------------- 30583 * removable media hotpluggable | non-512-byte blocksize 30584 * ----------------------------------------------------------- 30585 * false false | No 30586 * false true | No 30587 * true x | Yes 30588 * ----------------------------------------------------------- 30589 * 30590 * 30591 * 6. Automatic mount & unmount 30592 * 30593 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30594 * if a device is removable media device. It return 1 for removable media 30595 * devices, and 0 for others. 30596 * 30597 * The automatic mounting subsystem should distinguish between the types 30598 * of devices and apply automounting policies to each. 30599 * 30600 * 30601 * 7. fdisk partition management 30602 * 30603 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30604 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30605 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30606 * fdisk partitions on both x86 and SPARC platform. 30607 * 30608 * ----------------------------------------------------------- 30609 * platform removable media USB/1394 | fdisk supported 30610 * ----------------------------------------------------------- 30611 * x86 X X | true 30612 * ------------------------------------------------------------ 30613 * sparc X X | false 30614 * ------------------------------------------------------------ 30615 * 30616 * 30617 * 8. MBOOT/MBR 30618 * 30619 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30620 * read/write mboot for removable media devices on sparc platform. 30621 * 30622 * ----------------------------------------------------------- 30623 * platform removable media USB/1394 | mboot supported 30624 * ----------------------------------------------------------- 30625 * x86 X X | true 30626 * ------------------------------------------------------------ 30627 * sparc false false | false 30628 * sparc false true | true 30629 * sparc true false | true 30630 * sparc true true | true 30631 * ------------------------------------------------------------ 30632 * 30633 * 30634 * 9. error handling during opening device 30635 * 30636 * If failed to open a disk device, an errno is returned. For some kinds 30637 * of errors, different errno is returned depending on if this device is 30638 * a removable media device. This brings USB/1394 hard disks in line with 30639 * expected hard disk behavior. It is not expected that this breaks any 30640 * application. 30641 * 30642 * ------------------------------------------------------ 30643 * removable media hotpluggable | errno 30644 * ------------------------------------------------------ 30645 * false false | EIO 30646 * false true | EIO 30647 * true x | ENXIO 30648 * ------------------------------------------------------ 30649 * 30650 * 30651 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30652 * 30653 * These IOCTLs are applicable only to removable media devices. 30654 * 30655 * ----------------------------------------------------------- 30656 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30657 * ----------------------------------------------------------- 30658 * false false | No 30659 * false true | No 30660 * true x | Yes 30661 * ----------------------------------------------------------- 30662 * 30663 * 30664 * 12. Kstats for partitions 30665 * 30666 * sd creates partition kstat for non-removable media devices. USB and 30667 * Firewire hard disks now have partition kstats 30668 * 30669 * ------------------------------------------------------ 30670 * removable media hotpluggable | kstat 30671 * ------------------------------------------------------ 30672 * false false | Yes 30673 * false true | Yes 30674 * true x | No 30675 * ------------------------------------------------------ 30676 * 30677 * 30678 * 13. Removable media & hotpluggable properties 30679 * 30680 * Sd driver creates a "removable-media" property for removable media 30681 * devices. Parent nexus drivers create a "hotpluggable" property if 30682 * it supports hotplugging. 30683 * 30684 * --------------------------------------------------------------------- 30685 * removable media hotpluggable | "removable-media" " hotpluggable" 30686 * --------------------------------------------------------------------- 30687 * false false | No No 30688 * false true | No Yes 30689 * true false | Yes No 30690 * true true | Yes Yes 30691 * --------------------------------------------------------------------- 30692 * 30693 * 30694 * 14. Power Management 30695 * 30696 * sd only power manages removable media devices or devices that support 30697 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30698 * 30699 * A parent nexus that supports hotplugging can also set "pm-capable" 30700 * if the disk can be power managed. 30701 * 30702 * ------------------------------------------------------------ 30703 * removable media hotpluggable pm-capable | power manage 30704 * ------------------------------------------------------------ 30705 * false false false | No 30706 * false false true | Yes 30707 * false true false | No 30708 * false true true | Yes 30709 * true x x | Yes 30710 * ------------------------------------------------------------ 30711 * 30712 * USB and firewire hard disks can now be power managed independently 30713 * of the framebuffer 30714 * 30715 * 30716 * 15. Support for USB disks with capacity larger than 1TB 30717 * 30718 * Currently, sd doesn't permit a fixed disk device with capacity 30719 * larger than 1TB to be used in a 32-bit operating system environment. 30720 * However, sd doesn't do that for removable media devices. Instead, it 30721 * assumes that removable media devices cannot have a capacity larger 30722 * than 1TB. Therefore, using those devices on 32-bit system is partially 30723 * supported, which can cause some unexpected results. 30724 * 30725 * --------------------------------------------------------------------- 30726 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30727 * --------------------------------------------------------------------- 30728 * false false | true | no 30729 * false true | true | no 30730 * true false | true | Yes 30731 * true true | true | Yes 30732 * --------------------------------------------------------------------- 30733 * 30734 * 30735 * 16. Check write-protection at open time 30736 * 30737 * When a removable media device is being opened for writing without NDELAY 30738 * flag, sd will check if this device is writable. If attempting to open 30739 * without NDELAY flag a write-protected device, this operation will abort. 30740 * 30741 * ------------------------------------------------------------ 30742 * removable media USB/1394 | WP Check 30743 * ------------------------------------------------------------ 30744 * false false | No 30745 * false true | No 30746 * true false | Yes 30747 * true true | Yes 30748 * ------------------------------------------------------------ 30749 * 30750 * 30751 * 17. syslog when corrupted VTOC is encountered 30752 * 30753 * Currently, if an invalid VTOC is encountered, sd only print syslog 30754 * for fixed SCSI disks. 30755 * ------------------------------------------------------------ 30756 * removable media USB/1394 | print syslog 30757 * ------------------------------------------------------------ 30758 * false false | Yes 30759 * false true | No 30760 * true false | No 30761 * true true | No 30762 * ------------------------------------------------------------ 30763 */ 30764 static void 30765 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30766 { 30767 int pm_cap; 30768 30769 ASSERT(un->un_sd); 30770 ASSERT(un->un_sd->sd_inq); 30771 30772 /* 30773 * Enable SYNC CACHE support for all devices. 30774 */ 30775 un->un_f_sync_cache_supported = TRUE; 30776 30777 /* 30778 * Set the sync cache required flag to false. 30779 * This would ensure that there is no SYNC CACHE 30780 * sent when there are no writes 30781 */ 30782 un->un_f_sync_cache_required = FALSE; 30783 30784 if (un->un_sd->sd_inq->inq_rmb) { 30785 /* 30786 * The media of this device is removable. And for this kind 30787 * of devices, it is possible to change medium after opening 30788 * devices. Thus we should support this operation. 30789 */ 30790 un->un_f_has_removable_media = TRUE; 30791 30792 /* 30793 * support non-512-byte blocksize of removable media devices 30794 */ 30795 un->un_f_non_devbsize_supported = TRUE; 30796 30797 /* 30798 * Assume that all removable media devices support DOOR_LOCK 30799 */ 30800 un->un_f_doorlock_supported = TRUE; 30801 30802 /* 30803 * For a removable media device, it is possible to be opened 30804 * with NDELAY flag when there is no media in drive, in this 30805 * case we don't care if device is writable. But if without 30806 * NDELAY flag, we need to check if media is write-protected. 30807 */ 30808 un->un_f_chk_wp_open = TRUE; 30809 30810 /* 30811 * need to start a SCSI watch thread to monitor media state, 30812 * when media is being inserted or ejected, notify syseventd. 30813 */ 30814 un->un_f_monitor_media_state = TRUE; 30815 30816 /* 30817 * Some devices don't support START_STOP_UNIT command. 30818 * Therefore, we'd better check if a device supports it 30819 * before sending it. 30820 */ 30821 un->un_f_check_start_stop = TRUE; 30822 30823 /* 30824 * support eject media ioctl: 30825 * FDEJECT, DKIOCEJECT, CDROMEJECT 30826 */ 30827 un->un_f_eject_media_supported = TRUE; 30828 30829 /* 30830 * Because many removable-media devices don't support 30831 * LOG_SENSE, we couldn't use this command to check if 30832 * a removable media device support power-management. 30833 * We assume that they support power-management via 30834 * START_STOP_UNIT command and can be spun up and down 30835 * without limitations. 30836 */ 30837 un->un_f_pm_supported = TRUE; 30838 30839 /* 30840 * Need to create a zero length (Boolean) property 30841 * removable-media for the removable media devices. 30842 * Note that the return value of the property is not being 30843 * checked, since if unable to create the property 30844 * then do not want the attach to fail altogether. Consistent 30845 * with other property creation in attach. 30846 */ 30847 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30848 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30849 30850 } else { 30851 /* 30852 * create device ID for device 30853 */ 30854 un->un_f_devid_supported = TRUE; 30855 30856 /* 30857 * Spin up non-removable-media devices once it is attached 30858 */ 30859 un->un_f_attach_spinup = TRUE; 30860 30861 /* 30862 * According to SCSI specification, Sense data has two kinds of 30863 * format: fixed format, and descriptor format. At present, we 30864 * don't support descriptor format sense data for removable 30865 * media. 30866 */ 30867 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30868 un->un_f_descr_format_supported = TRUE; 30869 } 30870 30871 /* 30872 * kstats are created only for non-removable media devices. 30873 * 30874 * Set this in sd.conf to 0 in order to disable kstats. The 30875 * default is 1, so they are enabled by default. 30876 */ 30877 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30878 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30879 "enable-partition-kstats", 1)); 30880 30881 /* 30882 * Check if HBA has set the "pm-capable" property. 30883 * If "pm-capable" exists and is non-zero then we can 30884 * power manage the device without checking the start/stop 30885 * cycle count log sense page. 30886 * 30887 * If "pm-capable" exists and is set to be false (0), 30888 * then we should not power manage the device. 30889 * 30890 * If "pm-capable" doesn't exist then pm_cap will 30891 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 30892 * sd will check the start/stop cycle count log sense page 30893 * and power manage the device if the cycle count limit has 30894 * not been exceeded. 30895 */ 30896 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 30897 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 30898 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) { 30899 un->un_f_log_sense_supported = TRUE; 30900 if (!un->un_f_power_condition_disabled && 30901 SD_INQUIRY(un)->inq_ansi == 6) { 30902 un->un_f_power_condition_supported = TRUE; 30903 } 30904 } else { 30905 /* 30906 * pm-capable property exists. 30907 * 30908 * Convert "TRUE" values for pm_cap to 30909 * SD_PM_CAPABLE_IS_TRUE to make it easier to check 30910 * later. "TRUE" values are any values defined in 30911 * inquiry.h. 30912 */ 30913 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) { 30914 un->un_f_log_sense_supported = FALSE; 30915 } else { 30916 /* SD_PM_CAPABLE_IS_TRUE case */ 30917 un->un_f_pm_supported = TRUE; 30918 if (!un->un_f_power_condition_disabled && 30919 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) { 30920 un->un_f_power_condition_supported = 30921 TRUE; 30922 } 30923 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) { 30924 un->un_f_log_sense_supported = TRUE; 30925 un->un_f_pm_log_sense_smart = 30926 SD_PM_CAP_SMART_LOG(pm_cap); 30927 } 30928 } 30929 30930 SD_INFO(SD_LOG_ATTACH_DETACH, un, 30931 "sd_unit_attach: un:0x%p pm-capable " 30932 "property set to %d.\n", un, un->un_f_pm_supported); 30933 } 30934 } 30935 30936 if (un->un_f_is_hotpluggable) { 30937 30938 /* 30939 * Have to watch hotpluggable devices as well, since 30940 * that's the only way for userland applications to 30941 * detect hot removal while device is busy/mounted. 30942 */ 30943 un->un_f_monitor_media_state = TRUE; 30944 30945 un->un_f_check_start_stop = TRUE; 30946 30947 } 30948 } 30949 30950 /* 30951 * sd_tg_rdwr: 30952 * Provides rdwr access for cmlb via sd_tgops. The start_block is 30953 * in sys block size, req_length in bytes. 30954 * 30955 */ 30956 static int 30957 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 30958 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 30959 { 30960 struct sd_lun *un; 30961 int path_flag = (int)(uintptr_t)tg_cookie; 30962 char *dkl = NULL; 30963 diskaddr_t real_addr = start_block; 30964 diskaddr_t first_byte, end_block; 30965 30966 size_t buffer_size = reqlength; 30967 int rval = 0; 30968 diskaddr_t cap; 30969 uint32_t lbasize; 30970 sd_ssc_t *ssc; 30971 30972 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 30973 if (un == NULL) 30974 return (ENXIO); 30975 30976 if (cmd != TG_READ && cmd != TG_WRITE) 30977 return (EINVAL); 30978 30979 ssc = sd_ssc_init(un); 30980 mutex_enter(SD_MUTEX(un)); 30981 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 30982 mutex_exit(SD_MUTEX(un)); 30983 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 30984 &lbasize, path_flag); 30985 if (rval != 0) 30986 goto done1; 30987 mutex_enter(SD_MUTEX(un)); 30988 sd_update_block_info(un, lbasize, cap); 30989 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 30990 mutex_exit(SD_MUTEX(un)); 30991 rval = EIO; 30992 goto done; 30993 } 30994 } 30995 30996 if (NOT_DEVBSIZE(un)) { 30997 /* 30998 * sys_blocksize != tgt_blocksize, need to re-adjust 30999 * blkno and save the index to beginning of dk_label 31000 */ 31001 first_byte = SD_SYSBLOCKS2BYTES(start_block); 31002 real_addr = first_byte / un->un_tgt_blocksize; 31003 31004 end_block = (first_byte + reqlength + 31005 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 31006 31007 /* round up buffer size to multiple of target block size */ 31008 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 31009 31010 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 31011 "label_addr: 0x%x allocation size: 0x%x\n", 31012 real_addr, buffer_size); 31013 31014 if (((first_byte % un->un_tgt_blocksize) != 0) || 31015 (reqlength % un->un_tgt_blocksize) != 0) 31016 /* the request is not aligned */ 31017 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 31018 } 31019 31020 /* 31021 * The MMC standard allows READ CAPACITY to be 31022 * inaccurate by a bounded amount (in the interest of 31023 * response latency). As a result, failed READs are 31024 * commonplace (due to the reading of metadata and not 31025 * data). Depending on the per-Vendor/drive Sense data, 31026 * the failed READ can cause many (unnecessary) retries. 31027 */ 31028 31029 if (ISCD(un) && (cmd == TG_READ) && 31030 (un->un_f_blockcount_is_valid == TRUE) && 31031 ((start_block == (un->un_blockcount - 1))|| 31032 (start_block == (un->un_blockcount - 2)))) { 31033 path_flag = SD_PATH_DIRECT_PRIORITY; 31034 } 31035 31036 mutex_exit(SD_MUTEX(un)); 31037 if (cmd == TG_READ) { 31038 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr, 31039 buffer_size, real_addr, path_flag); 31040 if (dkl != NULL) 31041 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 31042 real_addr), bufaddr, reqlength); 31043 } else { 31044 if (dkl) { 31045 rval = sd_send_scsi_READ(ssc, dkl, buffer_size, 31046 real_addr, path_flag); 31047 if (rval) { 31048 goto done1; 31049 } 31050 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 31051 real_addr), reqlength); 31052 } 31053 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr, 31054 buffer_size, real_addr, path_flag); 31055 } 31056 31057 done1: 31058 if (dkl != NULL) 31059 kmem_free(dkl, buffer_size); 31060 31061 if (rval != 0) { 31062 if (rval == EIO) 31063 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 31064 else 31065 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31066 } 31067 done: 31068 sd_ssc_fini(ssc); 31069 return (rval); 31070 } 31071 31072 31073 static int 31074 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 31075 { 31076 31077 struct sd_lun *un; 31078 diskaddr_t cap; 31079 uint32_t lbasize; 31080 int path_flag = (int)(uintptr_t)tg_cookie; 31081 int ret = 0; 31082 31083 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 31084 if (un == NULL) 31085 return (ENXIO); 31086 31087 switch (cmd) { 31088 case TG_GETPHYGEOM: 31089 case TG_GETVIRTGEOM: 31090 case TG_GETCAPACITY: 31091 case TG_GETBLOCKSIZE: 31092 mutex_enter(SD_MUTEX(un)); 31093 31094 if ((un->un_f_blockcount_is_valid == TRUE) && 31095 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 31096 cap = un->un_blockcount; 31097 lbasize = un->un_tgt_blocksize; 31098 mutex_exit(SD_MUTEX(un)); 31099 } else { 31100 sd_ssc_t *ssc; 31101 mutex_exit(SD_MUTEX(un)); 31102 ssc = sd_ssc_init(un); 31103 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 31104 &lbasize, path_flag); 31105 if (ret != 0) { 31106 if (ret == EIO) 31107 sd_ssc_assessment(ssc, 31108 SD_FMT_STATUS_CHECK); 31109 else 31110 sd_ssc_assessment(ssc, 31111 SD_FMT_IGNORE); 31112 sd_ssc_fini(ssc); 31113 return (ret); 31114 } 31115 sd_ssc_fini(ssc); 31116 mutex_enter(SD_MUTEX(un)); 31117 sd_update_block_info(un, lbasize, cap); 31118 if ((un->un_f_blockcount_is_valid == FALSE) || 31119 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 31120 mutex_exit(SD_MUTEX(un)); 31121 return (EIO); 31122 } 31123 mutex_exit(SD_MUTEX(un)); 31124 } 31125 31126 if (cmd == TG_GETCAPACITY) { 31127 *(diskaddr_t *)arg = cap; 31128 return (0); 31129 } 31130 31131 if (cmd == TG_GETBLOCKSIZE) { 31132 *(uint32_t *)arg = lbasize; 31133 return (0); 31134 } 31135 31136 if (cmd == TG_GETPHYGEOM) 31137 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 31138 cap, lbasize, path_flag); 31139 else 31140 /* TG_GETVIRTGEOM */ 31141 ret = sd_get_virtual_geometry(un, 31142 (cmlb_geom_t *)arg, cap, lbasize); 31143 31144 return (ret); 31145 31146 case TG_GETATTR: 31147 mutex_enter(SD_MUTEX(un)); 31148 ((tg_attribute_t *)arg)->media_is_writable = 31149 un->un_f_mmc_writable_media; 31150 ((tg_attribute_t *)arg)->media_is_solid_state = 31151 un->un_f_is_solid_state; 31152 mutex_exit(SD_MUTEX(un)); 31153 return (0); 31154 default: 31155 return (ENOTTY); 31156 31157 } 31158 } 31159 31160 /* 31161 * Function: sd_ssc_ereport_post 31162 * 31163 * Description: Will be called when SD driver need to post an ereport. 31164 * 31165 * Context: Kernel thread or interrupt context. 31166 */ 31167 31168 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown" 31169 31170 static void 31171 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess) 31172 { 31173 int uscsi_path_instance = 0; 31174 uchar_t uscsi_pkt_reason; 31175 uint32_t uscsi_pkt_state; 31176 uint32_t uscsi_pkt_statistics; 31177 uint64_t uscsi_ena; 31178 uchar_t op_code; 31179 uint8_t *sensep; 31180 union scsi_cdb *cdbp; 31181 uint_t cdblen = 0; 31182 uint_t senlen = 0; 31183 struct sd_lun *un; 31184 dev_info_t *dip; 31185 char *devid; 31186 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON | 31187 SSC_FLAGS_INVALID_STATUS | 31188 SSC_FLAGS_INVALID_SENSE | 31189 SSC_FLAGS_INVALID_DATA; 31190 char assessment[16]; 31191 31192 ASSERT(ssc != NULL); 31193 ASSERT(ssc->ssc_uscsi_cmd != NULL); 31194 ASSERT(ssc->ssc_uscsi_info != NULL); 31195 31196 un = ssc->ssc_un; 31197 ASSERT(un != NULL); 31198 31199 dip = un->un_sd->sd_dev; 31200 31201 /* 31202 * Get the devid: 31203 * devid will only be passed to non-transport error reports. 31204 */ 31205 devid = DEVI(dip)->devi_devid_str; 31206 31207 /* 31208 * If we are syncing or dumping, the command will not be executed 31209 * so we bypass this situation. 31210 */ 31211 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 31212 (un->un_state == SD_STATE_DUMPING)) 31213 return; 31214 31215 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason; 31216 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance; 31217 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state; 31218 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics; 31219 uscsi_ena = ssc->ssc_uscsi_info->ui_ena; 31220 31221 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 31222 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb; 31223 31224 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */ 31225 if (cdbp == NULL) { 31226 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 31227 "sd_ssc_ereport_post meet empty cdb\n"); 31228 return; 31229 } 31230 31231 op_code = cdbp->scc_cmd; 31232 31233 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen; 31234 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 31235 ssc->ssc_uscsi_cmd->uscsi_rqresid); 31236 31237 if (senlen > 0) 31238 ASSERT(sensep != NULL); 31239 31240 /* 31241 * Initialize drv_assess to corresponding values. 31242 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending 31243 * on the sense-key returned back. 31244 */ 31245 switch (drv_assess) { 31246 case SD_FM_DRV_RECOVERY: 31247 (void) sprintf(assessment, "%s", "recovered"); 31248 break; 31249 case SD_FM_DRV_RETRY: 31250 (void) sprintf(assessment, "%s", "retry"); 31251 break; 31252 case SD_FM_DRV_NOTICE: 31253 (void) sprintf(assessment, "%s", "info"); 31254 break; 31255 case SD_FM_DRV_FATAL: 31256 default: 31257 (void) sprintf(assessment, "%s", "unknown"); 31258 } 31259 /* 31260 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered 31261 * command, we will post ereport.io.scsi.cmd.disk.recovered. 31262 * driver-assessment will always be "recovered" here. 31263 */ 31264 if (drv_assess == SD_FM_DRV_RECOVERY) { 31265 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31266 "cmd.disk.recovered", uscsi_ena, devid, NULL, 31267 DDI_NOSLEEP, NULL, 31268 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31269 DEVID_IF_KNOWN(devid), 31270 "driver-assessment", DATA_TYPE_STRING, assessment, 31271 "op-code", DATA_TYPE_UINT8, op_code, 31272 "cdb", DATA_TYPE_UINT8_ARRAY, 31273 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31274 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31275 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31276 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31277 NULL); 31278 return; 31279 } 31280 31281 /* 31282 * If there is un-expected/un-decodable data, we should post 31283 * ereport.io.scsi.cmd.disk.dev.uderr. 31284 * driver-assessment will be set based on parameter drv_assess. 31285 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back. 31286 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered. 31287 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered. 31288 * SSC_FLAGS_INVALID_DATA - invalid data sent back. 31289 */ 31290 if (ssc->ssc_flags & ssc_invalid_flags) { 31291 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) { 31292 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31293 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid, 31294 NULL, DDI_NOSLEEP, NULL, 31295 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31296 DEVID_IF_KNOWN(devid), 31297 "driver-assessment", DATA_TYPE_STRING, 31298 drv_assess == SD_FM_DRV_FATAL ? 31299 "fail" : assessment, 31300 "op-code", DATA_TYPE_UINT8, op_code, 31301 "cdb", DATA_TYPE_UINT8_ARRAY, 31302 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31303 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31304 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31305 "pkt-stats", DATA_TYPE_UINT32, 31306 uscsi_pkt_statistics, 31307 "stat-code", DATA_TYPE_UINT8, 31308 ssc->ssc_uscsi_cmd->uscsi_status, 31309 "un-decode-info", DATA_TYPE_STRING, 31310 ssc->ssc_info, 31311 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31312 senlen, sensep, 31313 NULL); 31314 } else { 31315 /* 31316 * For other type of invalid data, the 31317 * un-decode-value field would be empty because the 31318 * un-decodable content could be seen from upper 31319 * level payload or inside un-decode-info. 31320 */ 31321 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31322 NULL, 31323 "cmd.disk.dev.uderr", uscsi_ena, devid, 31324 NULL, DDI_NOSLEEP, NULL, 31325 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31326 DEVID_IF_KNOWN(devid), 31327 "driver-assessment", DATA_TYPE_STRING, 31328 drv_assess == SD_FM_DRV_FATAL ? 31329 "fail" : assessment, 31330 "op-code", DATA_TYPE_UINT8, op_code, 31331 "cdb", DATA_TYPE_UINT8_ARRAY, 31332 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31333 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31334 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31335 "pkt-stats", DATA_TYPE_UINT32, 31336 uscsi_pkt_statistics, 31337 "stat-code", DATA_TYPE_UINT8, 31338 ssc->ssc_uscsi_cmd->uscsi_status, 31339 "un-decode-info", DATA_TYPE_STRING, 31340 ssc->ssc_info, 31341 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31342 0, NULL, 31343 NULL); 31344 } 31345 ssc->ssc_flags &= ~ssc_invalid_flags; 31346 return; 31347 } 31348 31349 if (uscsi_pkt_reason != CMD_CMPLT || 31350 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) { 31351 /* 31352 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was 31353 * set inside sd_start_cmds due to errors(bad packet or 31354 * fatal transport error), we should take it as a 31355 * transport error, so we post ereport.io.scsi.cmd.disk.tran. 31356 * driver-assessment will be set based on drv_assess. 31357 * We will set devid to NULL because it is a transport 31358 * error. 31359 */ 31360 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT) 31361 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT; 31362 31363 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31364 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL, 31365 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31366 DEVID_IF_KNOWN(devid), 31367 "driver-assessment", DATA_TYPE_STRING, 31368 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31369 "op-code", DATA_TYPE_UINT8, op_code, 31370 "cdb", DATA_TYPE_UINT8_ARRAY, 31371 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31372 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31373 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state, 31374 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31375 NULL); 31376 } else { 31377 /* 31378 * If we got here, we have a completed command, and we need 31379 * to further investigate the sense data to see what kind 31380 * of ereport we should post. 31381 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr 31382 * if sense-key == 0x3. 31383 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise. 31384 * driver-assessment will be set based on the parameter 31385 * drv_assess. 31386 */ 31387 if (senlen > 0) { 31388 /* 31389 * Here we have sense data available. 31390 */ 31391 uint8_t sense_key; 31392 sense_key = scsi_sense_key(sensep); 31393 if (sense_key == 0x3) { 31394 /* 31395 * sense-key == 0x3(medium error), 31396 * driver-assessment should be "fatal" if 31397 * drv_assess is SD_FM_DRV_FATAL. 31398 */ 31399 scsi_fm_ereport_post(un->un_sd, 31400 uscsi_path_instance, NULL, 31401 "cmd.disk.dev.rqs.merr", 31402 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL, 31403 FM_VERSION, DATA_TYPE_UINT8, 31404 FM_EREPORT_VERS0, 31405 DEVID_IF_KNOWN(devid), 31406 "driver-assessment", 31407 DATA_TYPE_STRING, 31408 drv_assess == SD_FM_DRV_FATAL ? 31409 "fatal" : assessment, 31410 "op-code", 31411 DATA_TYPE_UINT8, op_code, 31412 "cdb", 31413 DATA_TYPE_UINT8_ARRAY, cdblen, 31414 ssc->ssc_uscsi_cmd->uscsi_cdb, 31415 "pkt-reason", 31416 DATA_TYPE_UINT8, uscsi_pkt_reason, 31417 "pkt-state", 31418 DATA_TYPE_UINT8, uscsi_pkt_state, 31419 "pkt-stats", 31420 DATA_TYPE_UINT32, 31421 uscsi_pkt_statistics, 31422 "stat-code", 31423 DATA_TYPE_UINT8, 31424 ssc->ssc_uscsi_cmd->uscsi_status, 31425 "key", 31426 DATA_TYPE_UINT8, 31427 scsi_sense_key(sensep), 31428 "asc", 31429 DATA_TYPE_UINT8, 31430 scsi_sense_asc(sensep), 31431 "ascq", 31432 DATA_TYPE_UINT8, 31433 scsi_sense_ascq(sensep), 31434 "sense-data", 31435 DATA_TYPE_UINT8_ARRAY, 31436 senlen, sensep, 31437 "lba", 31438 DATA_TYPE_UINT64, 31439 ssc->ssc_uscsi_info->ui_lba, 31440 NULL); 31441 } else { 31442 /* 31443 * if sense-key == 0x4(hardware 31444 * error), driver-assessment should 31445 * be "fatal" if drv_assess is 31446 * SD_FM_DRV_FATAL. 31447 */ 31448 scsi_fm_ereport_post(un->un_sd, 31449 uscsi_path_instance, NULL, 31450 "cmd.disk.dev.rqs.derr", 31451 uscsi_ena, devid, 31452 NULL, DDI_NOSLEEP, NULL, 31453 FM_VERSION, 31454 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31455 DEVID_IF_KNOWN(devid), 31456 "driver-assessment", 31457 DATA_TYPE_STRING, 31458 drv_assess == SD_FM_DRV_FATAL ? 31459 (sense_key == 0x4 ? 31460 "fatal" : "fail") : assessment, 31461 "op-code", 31462 DATA_TYPE_UINT8, op_code, 31463 "cdb", 31464 DATA_TYPE_UINT8_ARRAY, cdblen, 31465 ssc->ssc_uscsi_cmd->uscsi_cdb, 31466 "pkt-reason", 31467 DATA_TYPE_UINT8, uscsi_pkt_reason, 31468 "pkt-state", 31469 DATA_TYPE_UINT8, uscsi_pkt_state, 31470 "pkt-stats", 31471 DATA_TYPE_UINT32, 31472 uscsi_pkt_statistics, 31473 "stat-code", 31474 DATA_TYPE_UINT8, 31475 ssc->ssc_uscsi_cmd->uscsi_status, 31476 "key", 31477 DATA_TYPE_UINT8, 31478 scsi_sense_key(sensep), 31479 "asc", 31480 DATA_TYPE_UINT8, 31481 scsi_sense_asc(sensep), 31482 "ascq", 31483 DATA_TYPE_UINT8, 31484 scsi_sense_ascq(sensep), 31485 "sense-data", 31486 DATA_TYPE_UINT8_ARRAY, 31487 senlen, sensep, 31488 NULL); 31489 } 31490 } else { 31491 /* 31492 * For stat_code == STATUS_GOOD, this is not a 31493 * hardware error. 31494 */ 31495 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) 31496 return; 31497 31498 /* 31499 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the 31500 * stat-code but with sense data unavailable. 31501 * driver-assessment will be set based on parameter 31502 * drv_assess. 31503 */ 31504 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31505 NULL, 31506 "cmd.disk.dev.serr", uscsi_ena, 31507 devid, NULL, DDI_NOSLEEP, NULL, 31508 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31509 DEVID_IF_KNOWN(devid), 31510 "driver-assessment", DATA_TYPE_STRING, 31511 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31512 "op-code", DATA_TYPE_UINT8, op_code, 31513 "cdb", 31514 DATA_TYPE_UINT8_ARRAY, 31515 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31516 "pkt-reason", 31517 DATA_TYPE_UINT8, uscsi_pkt_reason, 31518 "pkt-state", 31519 DATA_TYPE_UINT8, uscsi_pkt_state, 31520 "pkt-stats", 31521 DATA_TYPE_UINT32, uscsi_pkt_statistics, 31522 "stat-code", 31523 DATA_TYPE_UINT8, 31524 ssc->ssc_uscsi_cmd->uscsi_status, 31525 NULL); 31526 } 31527 } 31528 } 31529 31530 /* 31531 * Function: sd_ssc_extract_info 31532 * 31533 * Description: Extract information available to help generate ereport. 31534 * 31535 * Context: Kernel thread or interrupt context. 31536 */ 31537 static void 31538 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp, 31539 struct buf *bp, struct sd_xbuf *xp) 31540 { 31541 size_t senlen = 0; 31542 union scsi_cdb *cdbp; 31543 int path_instance; 31544 /* 31545 * Need scsi_cdb_size array to determine the cdb length. 31546 */ 31547 extern uchar_t scsi_cdb_size[]; 31548 31549 ASSERT(un != NULL); 31550 ASSERT(pktp != NULL); 31551 ASSERT(bp != NULL); 31552 ASSERT(xp != NULL); 31553 ASSERT(ssc != NULL); 31554 ASSERT(mutex_owned(SD_MUTEX(un))); 31555 31556 /* 31557 * Transfer the cdb buffer pointer here. 31558 */ 31559 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 31560 31561 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 31562 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp; 31563 31564 /* 31565 * Transfer the sense data buffer pointer if sense data is available, 31566 * calculate the sense data length first. 31567 */ 31568 if ((xp->xb_sense_state & STATE_XARQ_DONE) || 31569 (xp->xb_sense_state & STATE_ARQ_DONE)) { 31570 /* 31571 * For arq case, we will enter here. 31572 */ 31573 if (xp->xb_sense_state & STATE_XARQ_DONE) { 31574 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid; 31575 } else { 31576 senlen = SENSE_LENGTH; 31577 } 31578 } else { 31579 /* 31580 * For non-arq case, we will enter this branch. 31581 */ 31582 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK && 31583 (xp->xb_sense_state & STATE_XFERRED_DATA)) { 31584 senlen = SENSE_LENGTH - xp->xb_sense_resid; 31585 } 31586 31587 } 31588 31589 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff); 31590 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0; 31591 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data; 31592 31593 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 31594 31595 /* 31596 * Only transfer path_instance when scsi_pkt was properly allocated. 31597 */ 31598 path_instance = pktp->pkt_path_instance; 31599 if (scsi_pkt_allocated_correctly(pktp) && path_instance) 31600 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance; 31601 else 31602 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0; 31603 31604 /* 31605 * Copy in the other fields we may need when posting ereport. 31606 */ 31607 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason; 31608 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state; 31609 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics; 31610 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 31611 31612 /* 31613 * For partially read/write command, we will not create ena 31614 * in case of a successful command be reconized as recovered. 31615 */ 31616 if ((pktp->pkt_reason == CMD_CMPLT) && 31617 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) && 31618 (senlen == 0)) { 31619 return; 31620 } 31621 31622 /* 31623 * To associate ereports of a single command execution flow, we 31624 * need a shared ena for a specific command. 31625 */ 31626 if (xp->xb_ena == 0) 31627 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1); 31628 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena; 31629 } 31630 31631 31632 /* 31633 * Function: sd_check_solid_state 31634 * 31635 * Description: Query the optional INQUIRY VPD page 0xb1. If the device 31636 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION 31637 * RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the 31638 * device is a solid state drive. 31639 * 31640 * Context: Kernel thread or interrupt context. 31641 */ 31642 31643 static void 31644 sd_check_solid_state(sd_ssc_t *ssc) 31645 { 31646 int rval = 0; 31647 uchar_t *inqb1 = NULL; 31648 size_t inqb1_len = MAX_INQUIRY_SIZE; 31649 size_t inqb1_resid = 0; 31650 struct sd_lun *un; 31651 31652 ASSERT(ssc != NULL); 31653 un = ssc->ssc_un; 31654 ASSERT(un != NULL); 31655 ASSERT(!mutex_owned(SD_MUTEX(un))); 31656 31657 mutex_enter(SD_MUTEX(un)); 31658 un->un_f_is_solid_state = FALSE; 31659 31660 if (ISCD(un)) { 31661 mutex_exit(SD_MUTEX(un)); 31662 return; 31663 } 31664 31665 if (sd_check_vpd_page_support(ssc) == 0 && 31666 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) { 31667 mutex_exit(SD_MUTEX(un)); 31668 /* collect page b1 data */ 31669 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP); 31670 31671 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len, 31672 0x01, 0xB1, &inqb1_resid); 31673 31674 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) { 31675 SD_TRACE(SD_LOG_COMMON, un, 31676 "sd_check_solid_state: \ 31677 successfully get VPD page: %x \ 31678 PAGE LENGTH: %x BYTE 4: %x \ 31679 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4], 31680 inqb1[5]); 31681 31682 mutex_enter(SD_MUTEX(un)); 31683 /* 31684 * Check the MEDIUM ROTATION RATE. If it is set 31685 * to 1, the device is a solid state drive. 31686 */ 31687 if (inqb1[4] == 0 && inqb1[5] == 1) { 31688 un->un_f_is_solid_state = TRUE; 31689 /* solid state drives don't need disksort */ 31690 un->un_f_disksort_disabled = TRUE; 31691 } 31692 mutex_exit(SD_MUTEX(un)); 31693 } else if (rval != 0) { 31694 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31695 } 31696 31697 kmem_free(inqb1, inqb1_len); 31698 } else { 31699 mutex_exit(SD_MUTEX(un)); 31700 } 31701 } 31702 31703 /* 31704 * Function: sd_check_emulation_mode 31705 * 31706 * Description: Check whether the SSD is at emulation mode 31707 * by issuing READ_CAPACITY_16 to see whether 31708 * we can get physical block size of the drive. 31709 * 31710 * Context: Kernel thread or interrupt context. 31711 */ 31712 31713 static void 31714 sd_check_emulation_mode(sd_ssc_t *ssc) 31715 { 31716 int rval = 0; 31717 uint64_t capacity; 31718 uint_t lbasize; 31719 uint_t pbsize; 31720 int i; 31721 int devid_len; 31722 struct sd_lun *un; 31723 31724 ASSERT(ssc != NULL); 31725 un = ssc->ssc_un; 31726 ASSERT(un != NULL); 31727 ASSERT(!mutex_owned(SD_MUTEX(un))); 31728 31729 mutex_enter(SD_MUTEX(un)); 31730 if (ISCD(un)) { 31731 mutex_exit(SD_MUTEX(un)); 31732 return; 31733 } 31734 31735 if (un->un_f_descr_format_supported) { 31736 mutex_exit(SD_MUTEX(un)); 31737 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 31738 &pbsize, SD_PATH_DIRECT); 31739 mutex_enter(SD_MUTEX(un)); 31740 31741 if (rval != 0) { 31742 un->un_phy_blocksize = DEV_BSIZE; 31743 } else { 31744 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) { 31745 un->un_phy_blocksize = DEV_BSIZE; 31746 } else { 31747 un->un_phy_blocksize = pbsize; 31748 } 31749 } 31750 } 31751 31752 for (i = 0; i < sd_flash_dev_table_size; i++) { 31753 devid_len = (int)strlen(sd_flash_dev_table[i]); 31754 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len) 31755 == SD_SUCCESS) { 31756 un->un_phy_blocksize = SSD_SECSIZE; 31757 if (un->un_f_is_solid_state && 31758 un->un_phy_blocksize != un->un_tgt_blocksize) 31759 un->un_f_enable_rmw = TRUE; 31760 } 31761 } 31762 31763 mutex_exit(SD_MUTEX(un)); 31764 } 31765