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 (c) 2011 Bayard G. Bell. All rights reserved. 27 * Copyright (c) 2012 by Delphix. All rights reserved. 28 * Copyright 2013 Nexenta Systems, Inc. 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 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) { 3892 continue; 3893 } 3894 3895 /* 3896 * dnlist contains 1 or more blank separated 3897 * data-property-name entries 3898 */ 3899 dnlist_ptr = config_list[i + 1]; 3900 3901 if (strchr(dnlist_ptr, ':') != NULL) { 3902 /* 3903 * Decode the improved format sd-config-list. 3904 */ 3905 sd_nvpair_str_decode(un, dnlist_ptr); 3906 } else { 3907 /* 3908 * The old format sd-config-list, loop through all 3909 * data-property-name entries in the 3910 * data-property-name-list 3911 * setting the properties for each. 3912 */ 3913 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", 3914 &dataname_lasts); dataname_ptr != NULL; 3915 dataname_ptr = sd_strtok_r(NULL, " \t", 3916 &dataname_lasts)) { 3917 int version; 3918 3919 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3920 "sd_process_sdconf_file: disk:%s, " 3921 "data:%s\n", vidptr, dataname_ptr); 3922 3923 /* Get the data list */ 3924 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, 3925 SD_DEVINFO(un), 0, dataname_ptr, &data_list, 3926 &data_list_len) != DDI_PROP_SUCCESS) { 3927 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3928 "sd_process_sdconf_file: data " 3929 "property (%s) has no value\n", 3930 dataname_ptr); 3931 continue; 3932 } 3933 3934 version = data_list[0]; 3935 3936 if (version == SD_CONF_VERSION_1) { 3937 sd_tunables values; 3938 3939 /* Set the properties */ 3940 if (sd_chk_vers1_data(un, data_list[1], 3941 &data_list[2], data_list_len, 3942 dataname_ptr) == SD_SUCCESS) { 3943 sd_get_tunables_from_conf(un, 3944 data_list[1], &data_list[2], 3945 &values); 3946 sd_set_vers1_properties(un, 3947 data_list[1], &values); 3948 rval = SD_SUCCESS; 3949 } else { 3950 rval = SD_FAILURE; 3951 } 3952 } else { 3953 scsi_log(SD_DEVINFO(un), sd_label, 3954 CE_WARN, "data property %s version " 3955 "0x%x is invalid.", 3956 dataname_ptr, version); 3957 rval = SD_FAILURE; 3958 } 3959 if (data_list) 3960 ddi_prop_free(data_list); 3961 } 3962 } 3963 } 3964 3965 /* free up the memory allocated by ddi_prop_lookup_string_array(). */ 3966 if (config_list) { 3967 ddi_prop_free(config_list); 3968 } 3969 3970 return (rval); 3971 } 3972 3973 /* 3974 * Function: sd_nvpair_str_decode() 3975 * 3976 * Description: Parse the improved format sd-config-list to get 3977 * each entry of tunable, which includes a name-value pair. 3978 * Then call sd_set_properties() to set the property. 3979 * 3980 * Arguments: un - driver soft state (unit) structure 3981 * nvpair_str - the tunable list 3982 */ 3983 static void 3984 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) 3985 { 3986 char *nv, *name, *value, *token; 3987 char *nv_lasts, *v_lasts, *x_lasts; 3988 3989 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; 3990 nv = sd_strtok_r(NULL, ",", &nv_lasts)) { 3991 token = sd_strtok_r(nv, ":", &v_lasts); 3992 name = sd_strtok_r(token, " \t", &x_lasts); 3993 token = sd_strtok_r(NULL, ":", &v_lasts); 3994 value = sd_strtok_r(token, " \t", &x_lasts); 3995 if (name == NULL || value == NULL) { 3996 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3997 "sd_nvpair_str_decode: " 3998 "name or value is not valid!\n"); 3999 } else { 4000 sd_set_properties(un, name, value); 4001 } 4002 } 4003 } 4004 4005 /* 4006 * Function: sd_strtok_r() 4007 * 4008 * Description: This function uses strpbrk and strspn to break 4009 * string into tokens on sequentially subsequent calls. Return 4010 * NULL when no non-separator characters remain. The first 4011 * argument is NULL for subsequent calls. 4012 */ 4013 static char * 4014 sd_strtok_r(char *string, const char *sepset, char **lasts) 4015 { 4016 char *q, *r; 4017 4018 /* First or subsequent call */ 4019 if (string == NULL) 4020 string = *lasts; 4021 4022 if (string == NULL) 4023 return (NULL); 4024 4025 /* Skip leading separators */ 4026 q = string + strspn(string, sepset); 4027 4028 if (*q == '\0') 4029 return (NULL); 4030 4031 if ((r = strpbrk(q, sepset)) == NULL) 4032 *lasts = NULL; 4033 else { 4034 *r = '\0'; 4035 *lasts = r + 1; 4036 } 4037 return (q); 4038 } 4039 4040 /* 4041 * Function: sd_set_properties() 4042 * 4043 * Description: Set device properties based on the improved 4044 * format sd-config-list. 4045 * 4046 * Arguments: un - driver soft state (unit) structure 4047 * name - supported tunable name 4048 * value - tunable value 4049 */ 4050 static void 4051 sd_set_properties(struct sd_lun *un, char *name, char *value) 4052 { 4053 char *endptr = NULL; 4054 long val = 0; 4055 4056 if (strcasecmp(name, "cache-nonvolatile") == 0) { 4057 if (strcasecmp(value, "true") == 0) { 4058 un->un_f_suppress_cache_flush = TRUE; 4059 } else if (strcasecmp(value, "false") == 0) { 4060 un->un_f_suppress_cache_flush = FALSE; 4061 } else { 4062 goto value_invalid; 4063 } 4064 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4065 "suppress_cache_flush flag set to %d\n", 4066 un->un_f_suppress_cache_flush); 4067 return; 4068 } 4069 4070 if (strcasecmp(name, "controller-type") == 0) { 4071 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4072 un->un_ctype = val; 4073 } else { 4074 goto value_invalid; 4075 } 4076 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4077 "ctype set to %d\n", un->un_ctype); 4078 return; 4079 } 4080 4081 if (strcasecmp(name, "delay-busy") == 0) { 4082 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4083 un->un_busy_timeout = drv_usectohz(val / 1000); 4084 } else { 4085 goto value_invalid; 4086 } 4087 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4088 "busy_timeout set to %d\n", un->un_busy_timeout); 4089 return; 4090 } 4091 4092 if (strcasecmp(name, "disksort") == 0) { 4093 if (strcasecmp(value, "true") == 0) { 4094 un->un_f_disksort_disabled = FALSE; 4095 } else if (strcasecmp(value, "false") == 0) { 4096 un->un_f_disksort_disabled = TRUE; 4097 } else { 4098 goto value_invalid; 4099 } 4100 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4101 "disksort disabled flag set to %d\n", 4102 un->un_f_disksort_disabled); 4103 return; 4104 } 4105 4106 if (strcasecmp(name, "power-condition") == 0) { 4107 if (strcasecmp(value, "true") == 0) { 4108 un->un_f_power_condition_disabled = FALSE; 4109 } else if (strcasecmp(value, "false") == 0) { 4110 un->un_f_power_condition_disabled = TRUE; 4111 } else { 4112 goto value_invalid; 4113 } 4114 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4115 "power condition disabled flag set to %d\n", 4116 un->un_f_power_condition_disabled); 4117 return; 4118 } 4119 4120 if (strcasecmp(name, "timeout-releasereservation") == 0) { 4121 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4122 un->un_reserve_release_time = val; 4123 } else { 4124 goto value_invalid; 4125 } 4126 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4127 "reservation release timeout set to %d\n", 4128 un->un_reserve_release_time); 4129 return; 4130 } 4131 4132 if (strcasecmp(name, "reset-lun") == 0) { 4133 if (strcasecmp(value, "true") == 0) { 4134 un->un_f_lun_reset_enabled = TRUE; 4135 } else if (strcasecmp(value, "false") == 0) { 4136 un->un_f_lun_reset_enabled = FALSE; 4137 } else { 4138 goto value_invalid; 4139 } 4140 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4141 "lun reset enabled flag set to %d\n", 4142 un->un_f_lun_reset_enabled); 4143 return; 4144 } 4145 4146 if (strcasecmp(name, "retries-busy") == 0) { 4147 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4148 un->un_busy_retry_count = val; 4149 } else { 4150 goto value_invalid; 4151 } 4152 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4153 "busy retry count set to %d\n", un->un_busy_retry_count); 4154 return; 4155 } 4156 4157 if (strcasecmp(name, "retries-timeout") == 0) { 4158 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4159 un->un_retry_count = val; 4160 } else { 4161 goto value_invalid; 4162 } 4163 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4164 "timeout retry count set to %d\n", un->un_retry_count); 4165 return; 4166 } 4167 4168 if (strcasecmp(name, "retries-notready") == 0) { 4169 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4170 un->un_notready_retry_count = val; 4171 } else { 4172 goto value_invalid; 4173 } 4174 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4175 "notready retry count set to %d\n", 4176 un->un_notready_retry_count); 4177 return; 4178 } 4179 4180 if (strcasecmp(name, "retries-reset") == 0) { 4181 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4182 un->un_reset_retry_count = val; 4183 } else { 4184 goto value_invalid; 4185 } 4186 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4187 "reset retry count set to %d\n", 4188 un->un_reset_retry_count); 4189 return; 4190 } 4191 4192 if (strcasecmp(name, "throttle-max") == 0) { 4193 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4194 un->un_saved_throttle = un->un_throttle = val; 4195 } else { 4196 goto value_invalid; 4197 } 4198 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4199 "throttle set to %d\n", un->un_throttle); 4200 } 4201 4202 if (strcasecmp(name, "throttle-min") == 0) { 4203 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4204 un->un_min_throttle = val; 4205 } else { 4206 goto value_invalid; 4207 } 4208 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4209 "min throttle set to %d\n", un->un_min_throttle); 4210 } 4211 4212 if (strcasecmp(name, "rmw-type") == 0) { 4213 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4214 un->un_f_rmw_type = val; 4215 } else { 4216 goto value_invalid; 4217 } 4218 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4219 "RMW type set to %d\n", un->un_f_rmw_type); 4220 } 4221 4222 if (strcasecmp(name, "physical-block-size") == 0) { 4223 if (ddi_strtol(value, &endptr, 0, &val) == 0 && 4224 ISP2(val) && val >= un->un_tgt_blocksize && 4225 val >= un->un_sys_blocksize) { 4226 un->un_phy_blocksize = val; 4227 } else { 4228 goto value_invalid; 4229 } 4230 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4231 "physical block size set to %d\n", un->un_phy_blocksize); 4232 } 4233 4234 if (strcasecmp(name, "retries-victim") == 0) { 4235 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4236 un->un_victim_retry_count = val; 4237 } else { 4238 goto value_invalid; 4239 } 4240 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4241 "victim retry count set to %d\n", 4242 un->un_victim_retry_count); 4243 return; 4244 } 4245 4246 /* 4247 * Validate the throttle values. 4248 * If any of the numbers are invalid, set everything to defaults. 4249 */ 4250 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4251 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4252 (un->un_min_throttle > un->un_throttle)) { 4253 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4254 un->un_min_throttle = sd_min_throttle; 4255 } 4256 4257 if (strcasecmp(name, "mmc-gesn-polling") == 0) { 4258 if (strcasecmp(value, "true") == 0) { 4259 un->un_f_mmc_gesn_polling = TRUE; 4260 } else if (strcasecmp(value, "false") == 0) { 4261 un->un_f_mmc_gesn_polling = FALSE; 4262 } else { 4263 goto value_invalid; 4264 } 4265 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4266 "mmc-gesn-polling set to %d\n", 4267 un->un_f_mmc_gesn_polling); 4268 } 4269 4270 return; 4271 4272 value_invalid: 4273 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4274 "value of prop %s is invalid\n", name); 4275 } 4276 4277 /* 4278 * Function: sd_get_tunables_from_conf() 4279 * 4280 * 4281 * This function reads the data list from the sd.conf file and pulls 4282 * the values that can have numeric values as arguments and places 4283 * the values in the appropriate sd_tunables member. 4284 * Since the order of the data list members varies across platforms 4285 * This function reads them from the data list in a platform specific 4286 * order and places them into the correct sd_tunable member that is 4287 * consistent across all platforms. 4288 */ 4289 static void 4290 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 4291 sd_tunables *values) 4292 { 4293 int i; 4294 int mask; 4295 4296 bzero(values, sizeof (sd_tunables)); 4297 4298 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4299 4300 mask = 1 << i; 4301 if (mask > flags) { 4302 break; 4303 } 4304 4305 switch (mask & flags) { 4306 case 0: /* This mask bit not set in flags */ 4307 continue; 4308 case SD_CONF_BSET_THROTTLE: 4309 values->sdt_throttle = data_list[i]; 4310 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4311 "sd_get_tunables_from_conf: throttle = %d\n", 4312 values->sdt_throttle); 4313 break; 4314 case SD_CONF_BSET_CTYPE: 4315 values->sdt_ctype = data_list[i]; 4316 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4317 "sd_get_tunables_from_conf: ctype = %d\n", 4318 values->sdt_ctype); 4319 break; 4320 case SD_CONF_BSET_NRR_COUNT: 4321 values->sdt_not_rdy_retries = data_list[i]; 4322 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4323 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 4324 values->sdt_not_rdy_retries); 4325 break; 4326 case SD_CONF_BSET_BSY_RETRY_COUNT: 4327 values->sdt_busy_retries = data_list[i]; 4328 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4329 "sd_get_tunables_from_conf: busy_retries = %d\n", 4330 values->sdt_busy_retries); 4331 break; 4332 case SD_CONF_BSET_RST_RETRIES: 4333 values->sdt_reset_retries = data_list[i]; 4334 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4335 "sd_get_tunables_from_conf: reset_retries = %d\n", 4336 values->sdt_reset_retries); 4337 break; 4338 case SD_CONF_BSET_RSV_REL_TIME: 4339 values->sdt_reserv_rel_time = data_list[i]; 4340 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4341 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 4342 values->sdt_reserv_rel_time); 4343 break; 4344 case SD_CONF_BSET_MIN_THROTTLE: 4345 values->sdt_min_throttle = data_list[i]; 4346 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4347 "sd_get_tunables_from_conf: min_throttle = %d\n", 4348 values->sdt_min_throttle); 4349 break; 4350 case SD_CONF_BSET_DISKSORT_DISABLED: 4351 values->sdt_disk_sort_dis = data_list[i]; 4352 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4353 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 4354 values->sdt_disk_sort_dis); 4355 break; 4356 case SD_CONF_BSET_LUN_RESET_ENABLED: 4357 values->sdt_lun_reset_enable = data_list[i]; 4358 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4359 "sd_get_tunables_from_conf: lun_reset_enable = %d" 4360 "\n", values->sdt_lun_reset_enable); 4361 break; 4362 case SD_CONF_BSET_CACHE_IS_NV: 4363 values->sdt_suppress_cache_flush = data_list[i]; 4364 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4365 "sd_get_tunables_from_conf: \ 4366 suppress_cache_flush = %d" 4367 "\n", values->sdt_suppress_cache_flush); 4368 break; 4369 case SD_CONF_BSET_PC_DISABLED: 4370 values->sdt_disk_sort_dis = data_list[i]; 4371 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4372 "sd_get_tunables_from_conf: power_condition_dis = " 4373 "%d\n", values->sdt_power_condition_dis); 4374 break; 4375 } 4376 } 4377 } 4378 4379 /* 4380 * Function: sd_process_sdconf_table 4381 * 4382 * Description: Search the static configuration table for a match on the 4383 * inquiry vid/pid and update the driver soft state structure 4384 * according to the table property values for the device. 4385 * 4386 * The form of a configuration table entry is: 4387 * <vid+pid>,<flags>,<property-data> 4388 * "SEAGATE ST42400N",1,0x40000, 4389 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 4390 * 4391 * Arguments: un - driver soft state (unit) structure 4392 */ 4393 4394 static void 4395 sd_process_sdconf_table(struct sd_lun *un) 4396 { 4397 char *id = NULL; 4398 int table_index; 4399 int idlen; 4400 4401 ASSERT(un != NULL); 4402 for (table_index = 0; table_index < sd_disk_table_size; 4403 table_index++) { 4404 id = sd_disk_table[table_index].device_id; 4405 idlen = strlen(id); 4406 4407 /* 4408 * The static configuration table currently does not 4409 * implement version 10 properties. Additionally, 4410 * multiple data-property-name entries are not 4411 * implemented in the static configuration table. 4412 */ 4413 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4414 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4415 "sd_process_sdconf_table: disk %s\n", id); 4416 sd_set_vers1_properties(un, 4417 sd_disk_table[table_index].flags, 4418 sd_disk_table[table_index].properties); 4419 break; 4420 } 4421 } 4422 } 4423 4424 4425 /* 4426 * Function: sd_sdconf_id_match 4427 * 4428 * Description: This local function implements a case sensitive vid/pid 4429 * comparison as well as the boundary cases of wild card and 4430 * multiple blanks. 4431 * 4432 * Note: An implicit assumption made here is that the scsi 4433 * inquiry structure will always keep the vid, pid and 4434 * revision strings in consecutive sequence, so they can be 4435 * read as a single string. If this assumption is not the 4436 * case, a separate string, to be used for the check, needs 4437 * to be built with these strings concatenated. 4438 * 4439 * Arguments: un - driver soft state (unit) structure 4440 * id - table or config file vid/pid 4441 * idlen - length of the vid/pid (bytes) 4442 * 4443 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4444 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4445 */ 4446 4447 static int 4448 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 4449 { 4450 struct scsi_inquiry *sd_inq; 4451 int rval = SD_SUCCESS; 4452 4453 ASSERT(un != NULL); 4454 sd_inq = un->un_sd->sd_inq; 4455 ASSERT(id != NULL); 4456 4457 /* 4458 * We use the inq_vid as a pointer to a buffer containing the 4459 * vid and pid and use the entire vid/pid length of the table 4460 * entry for the comparison. This works because the inq_pid 4461 * data member follows inq_vid in the scsi_inquiry structure. 4462 */ 4463 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 4464 /* 4465 * The user id string is compared to the inquiry vid/pid 4466 * using a case insensitive comparison and ignoring 4467 * multiple spaces. 4468 */ 4469 rval = sd_blank_cmp(un, id, idlen); 4470 if (rval != SD_SUCCESS) { 4471 /* 4472 * User id strings that start and end with a "*" 4473 * are a special case. These do not have a 4474 * specific vendor, and the product string can 4475 * appear anywhere in the 16 byte PID portion of 4476 * the inquiry data. This is a simple strstr() 4477 * type search for the user id in the inquiry data. 4478 */ 4479 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 4480 char *pidptr = &id[1]; 4481 int i; 4482 int j; 4483 int pidstrlen = idlen - 2; 4484 j = sizeof (SD_INQUIRY(un)->inq_pid) - 4485 pidstrlen; 4486 4487 if (j < 0) { 4488 return (SD_FAILURE); 4489 } 4490 for (i = 0; i < j; i++) { 4491 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 4492 pidptr, pidstrlen) == 0) { 4493 rval = SD_SUCCESS; 4494 break; 4495 } 4496 } 4497 } 4498 } 4499 } 4500 return (rval); 4501 } 4502 4503 4504 /* 4505 * Function: sd_blank_cmp 4506 * 4507 * Description: If the id string starts and ends with a space, treat 4508 * multiple consecutive spaces as equivalent to a single 4509 * space. For example, this causes a sd_disk_table entry 4510 * of " NEC CDROM " to match a device's id string of 4511 * "NEC CDROM". 4512 * 4513 * Note: The success exit condition for this routine is if 4514 * the pointer to the table entry is '\0' and the cnt of 4515 * the inquiry length is zero. This will happen if the inquiry 4516 * string returned by the device is padded with spaces to be 4517 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 4518 * SCSI spec states that the inquiry string is to be padded with 4519 * spaces. 4520 * 4521 * Arguments: un - driver soft state (unit) structure 4522 * id - table or config file vid/pid 4523 * idlen - length of the vid/pid (bytes) 4524 * 4525 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4526 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4527 */ 4528 4529 static int 4530 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 4531 { 4532 char *p1; 4533 char *p2; 4534 int cnt; 4535 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 4536 sizeof (SD_INQUIRY(un)->inq_pid); 4537 4538 ASSERT(un != NULL); 4539 p2 = un->un_sd->sd_inq->inq_vid; 4540 ASSERT(id != NULL); 4541 p1 = id; 4542 4543 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 4544 /* 4545 * Note: string p1 is terminated by a NUL but string p2 4546 * isn't. The end of p2 is determined by cnt. 4547 */ 4548 for (;;) { 4549 /* skip over any extra blanks in both strings */ 4550 while ((*p1 != '\0') && (*p1 == ' ')) { 4551 p1++; 4552 } 4553 while ((cnt != 0) && (*p2 == ' ')) { 4554 p2++; 4555 cnt--; 4556 } 4557 4558 /* compare the two strings */ 4559 if ((cnt == 0) || 4560 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 4561 break; 4562 } 4563 while ((cnt > 0) && 4564 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 4565 p1++; 4566 p2++; 4567 cnt--; 4568 } 4569 } 4570 } 4571 4572 /* return SD_SUCCESS if both strings match */ 4573 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 4574 } 4575 4576 4577 /* 4578 * Function: sd_chk_vers1_data 4579 * 4580 * Description: Verify the version 1 device properties provided by the 4581 * user via the configuration file 4582 * 4583 * Arguments: un - driver soft state (unit) structure 4584 * flags - integer mask indicating properties to be set 4585 * prop_list - integer list of property values 4586 * list_len - number of the elements 4587 * 4588 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4589 * SD_FAILURE - Indicates the user provided data is invalid 4590 */ 4591 4592 static int 4593 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4594 int list_len, char *dataname_ptr) 4595 { 4596 int i; 4597 int mask = 1; 4598 int index = 0; 4599 4600 ASSERT(un != NULL); 4601 4602 /* Check for a NULL property name and list */ 4603 if (dataname_ptr == NULL) { 4604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4605 "sd_chk_vers1_data: NULL data property name."); 4606 return (SD_FAILURE); 4607 } 4608 if (prop_list == NULL) { 4609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4610 "sd_chk_vers1_data: %s NULL data property list.", 4611 dataname_ptr); 4612 return (SD_FAILURE); 4613 } 4614 4615 /* Display a warning if undefined bits are set in the flags */ 4616 if (flags & ~SD_CONF_BIT_MASK) { 4617 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4618 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4619 "Properties not set.", 4620 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4621 return (SD_FAILURE); 4622 } 4623 4624 /* 4625 * Verify the length of the list by identifying the highest bit set 4626 * in the flags and validating that the property list has a length 4627 * up to the index of this bit. 4628 */ 4629 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4630 if (flags & mask) { 4631 index++; 4632 } 4633 mask = 1 << i; 4634 } 4635 if (list_len < (index + 2)) { 4636 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4637 "sd_chk_vers1_data: " 4638 "Data property list %s size is incorrect. " 4639 "Properties not set.", dataname_ptr); 4640 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4641 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4642 return (SD_FAILURE); 4643 } 4644 return (SD_SUCCESS); 4645 } 4646 4647 4648 /* 4649 * Function: sd_set_vers1_properties 4650 * 4651 * Description: Set version 1 device properties based on a property list 4652 * retrieved from the driver configuration file or static 4653 * configuration table. Version 1 properties have the format: 4654 * 4655 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4656 * 4657 * where the prop0 value will be used to set prop0 if bit0 4658 * is set in the flags 4659 * 4660 * Arguments: un - driver soft state (unit) structure 4661 * flags - integer mask indicating properties to be set 4662 * prop_list - integer list of property values 4663 */ 4664 4665 static void 4666 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4667 { 4668 ASSERT(un != NULL); 4669 4670 /* 4671 * Set the flag to indicate cache is to be disabled. An attempt 4672 * to disable the cache via sd_cache_control() will be made 4673 * later during attach once the basic initialization is complete. 4674 */ 4675 if (flags & SD_CONF_BSET_NOCACHE) { 4676 un->un_f_opt_disable_cache = TRUE; 4677 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4678 "sd_set_vers1_properties: caching disabled flag set\n"); 4679 } 4680 4681 /* CD-specific configuration parameters */ 4682 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4683 un->un_f_cfg_playmsf_bcd = TRUE; 4684 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4685 "sd_set_vers1_properties: playmsf_bcd set\n"); 4686 } 4687 if (flags & SD_CONF_BSET_READSUB_BCD) { 4688 un->un_f_cfg_readsub_bcd = TRUE; 4689 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4690 "sd_set_vers1_properties: readsub_bcd set\n"); 4691 } 4692 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4693 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4694 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4695 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4696 } 4697 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4698 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4699 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4700 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4701 } 4702 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4703 un->un_f_cfg_no_read_header = TRUE; 4704 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4705 "sd_set_vers1_properties: no_read_header set\n"); 4706 } 4707 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4708 un->un_f_cfg_read_cd_xd4 = TRUE; 4709 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4710 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4711 } 4712 4713 /* Support for devices which do not have valid/unique serial numbers */ 4714 if (flags & SD_CONF_BSET_FAB_DEVID) { 4715 un->un_f_opt_fab_devid = TRUE; 4716 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4717 "sd_set_vers1_properties: fab_devid bit set\n"); 4718 } 4719 4720 /* Support for user throttle configuration */ 4721 if (flags & SD_CONF_BSET_THROTTLE) { 4722 ASSERT(prop_list != NULL); 4723 un->un_saved_throttle = un->un_throttle = 4724 prop_list->sdt_throttle; 4725 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4726 "sd_set_vers1_properties: throttle set to %d\n", 4727 prop_list->sdt_throttle); 4728 } 4729 4730 /* Set the per disk retry count according to the conf file or table. */ 4731 if (flags & SD_CONF_BSET_NRR_COUNT) { 4732 ASSERT(prop_list != NULL); 4733 if (prop_list->sdt_not_rdy_retries) { 4734 un->un_notready_retry_count = 4735 prop_list->sdt_not_rdy_retries; 4736 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4737 "sd_set_vers1_properties: not ready retry count" 4738 " set to %d\n", un->un_notready_retry_count); 4739 } 4740 } 4741 4742 /* The controller type is reported for generic disk driver ioctls */ 4743 if (flags & SD_CONF_BSET_CTYPE) { 4744 ASSERT(prop_list != NULL); 4745 switch (prop_list->sdt_ctype) { 4746 case CTYPE_CDROM: 4747 un->un_ctype = prop_list->sdt_ctype; 4748 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4749 "sd_set_vers1_properties: ctype set to " 4750 "CTYPE_CDROM\n"); 4751 break; 4752 case CTYPE_CCS: 4753 un->un_ctype = prop_list->sdt_ctype; 4754 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4755 "sd_set_vers1_properties: ctype set to " 4756 "CTYPE_CCS\n"); 4757 break; 4758 case CTYPE_ROD: /* RW optical */ 4759 un->un_ctype = prop_list->sdt_ctype; 4760 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4761 "sd_set_vers1_properties: ctype set to " 4762 "CTYPE_ROD\n"); 4763 break; 4764 default: 4765 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4766 "sd_set_vers1_properties: Could not set " 4767 "invalid ctype value (%d)", 4768 prop_list->sdt_ctype); 4769 } 4770 } 4771 4772 /* Purple failover timeout */ 4773 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4774 ASSERT(prop_list != NULL); 4775 un->un_busy_retry_count = 4776 prop_list->sdt_busy_retries; 4777 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4778 "sd_set_vers1_properties: " 4779 "busy retry count set to %d\n", 4780 un->un_busy_retry_count); 4781 } 4782 4783 /* Purple reset retry count */ 4784 if (flags & SD_CONF_BSET_RST_RETRIES) { 4785 ASSERT(prop_list != NULL); 4786 un->un_reset_retry_count = 4787 prop_list->sdt_reset_retries; 4788 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4789 "sd_set_vers1_properties: " 4790 "reset retry count set to %d\n", 4791 un->un_reset_retry_count); 4792 } 4793 4794 /* Purple reservation release timeout */ 4795 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4796 ASSERT(prop_list != NULL); 4797 un->un_reserve_release_time = 4798 prop_list->sdt_reserv_rel_time; 4799 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4800 "sd_set_vers1_properties: " 4801 "reservation release timeout set to %d\n", 4802 un->un_reserve_release_time); 4803 } 4804 4805 /* 4806 * Driver flag telling the driver to verify that no commands are pending 4807 * for a device before issuing a Test Unit Ready. This is a workaround 4808 * for a firmware bug in some Seagate eliteI drives. 4809 */ 4810 if (flags & SD_CONF_BSET_TUR_CHECK) { 4811 un->un_f_cfg_tur_check = TRUE; 4812 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4813 "sd_set_vers1_properties: tur queue check set\n"); 4814 } 4815 4816 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4817 un->un_min_throttle = prop_list->sdt_min_throttle; 4818 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4819 "sd_set_vers1_properties: min throttle set to %d\n", 4820 un->un_min_throttle); 4821 } 4822 4823 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4824 un->un_f_disksort_disabled = 4825 (prop_list->sdt_disk_sort_dis != 0) ? 4826 TRUE : FALSE; 4827 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4828 "sd_set_vers1_properties: disksort disabled " 4829 "flag set to %d\n", 4830 prop_list->sdt_disk_sort_dis); 4831 } 4832 4833 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4834 un->un_f_lun_reset_enabled = 4835 (prop_list->sdt_lun_reset_enable != 0) ? 4836 TRUE : FALSE; 4837 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4838 "sd_set_vers1_properties: lun reset enabled " 4839 "flag set to %d\n", 4840 prop_list->sdt_lun_reset_enable); 4841 } 4842 4843 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4844 un->un_f_suppress_cache_flush = 4845 (prop_list->sdt_suppress_cache_flush != 0) ? 4846 TRUE : FALSE; 4847 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4848 "sd_set_vers1_properties: suppress_cache_flush " 4849 "flag set to %d\n", 4850 prop_list->sdt_suppress_cache_flush); 4851 } 4852 4853 if (flags & SD_CONF_BSET_PC_DISABLED) { 4854 un->un_f_power_condition_disabled = 4855 (prop_list->sdt_power_condition_dis != 0) ? 4856 TRUE : FALSE; 4857 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4858 "sd_set_vers1_properties: power_condition_disabled " 4859 "flag set to %d\n", 4860 prop_list->sdt_power_condition_dis); 4861 } 4862 4863 /* 4864 * Validate the throttle values. 4865 * If any of the numbers are invalid, set everything to defaults. 4866 */ 4867 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4868 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4869 (un->un_min_throttle > un->un_throttle)) { 4870 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4871 un->un_min_throttle = sd_min_throttle; 4872 } 4873 } 4874 4875 /* 4876 * Function: sd_is_lsi() 4877 * 4878 * Description: Check for lsi devices, step through the static device 4879 * table to match vid/pid. 4880 * 4881 * Args: un - ptr to sd_lun 4882 * 4883 * Notes: When creating new LSI property, need to add the new LSI property 4884 * to this function. 4885 */ 4886 static void 4887 sd_is_lsi(struct sd_lun *un) 4888 { 4889 char *id = NULL; 4890 int table_index; 4891 int idlen; 4892 void *prop; 4893 4894 ASSERT(un != NULL); 4895 for (table_index = 0; table_index < sd_disk_table_size; 4896 table_index++) { 4897 id = sd_disk_table[table_index].device_id; 4898 idlen = strlen(id); 4899 if (idlen == 0) { 4900 continue; 4901 } 4902 4903 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4904 prop = sd_disk_table[table_index].properties; 4905 if (prop == &lsi_properties || 4906 prop == &lsi_oem_properties || 4907 prop == &lsi_properties_scsi || 4908 prop == &symbios_properties) { 4909 un->un_f_cfg_is_lsi = TRUE; 4910 } 4911 break; 4912 } 4913 } 4914 } 4915 4916 /* 4917 * Function: sd_get_physical_geometry 4918 * 4919 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4920 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4921 * target, and use this information to initialize the physical 4922 * geometry cache specified by pgeom_p. 4923 * 4924 * MODE SENSE is an optional command, so failure in this case 4925 * does not necessarily denote an error. We want to use the 4926 * MODE SENSE commands to derive the physical geometry of the 4927 * device, but if either command fails, the logical geometry is 4928 * used as the fallback for disk label geometry in cmlb. 4929 * 4930 * This requires that un->un_blockcount and un->un_tgt_blocksize 4931 * have already been initialized for the current target and 4932 * that the current values be passed as args so that we don't 4933 * end up ever trying to use -1 as a valid value. This could 4934 * happen if either value is reset while we're not holding 4935 * the mutex. 4936 * 4937 * Arguments: un - driver soft state (unit) structure 4938 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4939 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4940 * to use the USCSI "direct" chain and bypass the normal 4941 * command waitq. 4942 * 4943 * Context: Kernel thread only (can sleep). 4944 */ 4945 4946 static int 4947 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4948 diskaddr_t capacity, int lbasize, int path_flag) 4949 { 4950 struct mode_format *page3p; 4951 struct mode_geometry *page4p; 4952 struct mode_header *headerp; 4953 int sector_size; 4954 int nsect; 4955 int nhead; 4956 int ncyl; 4957 int intrlv; 4958 int spc; 4959 diskaddr_t modesense_capacity; 4960 int rpm; 4961 int bd_len; 4962 int mode_header_length; 4963 uchar_t *p3bufp; 4964 uchar_t *p4bufp; 4965 int cdbsize; 4966 int ret = EIO; 4967 sd_ssc_t *ssc; 4968 int status; 4969 4970 ASSERT(un != NULL); 4971 4972 if (lbasize == 0) { 4973 if (ISCD(un)) { 4974 lbasize = 2048; 4975 } else { 4976 lbasize = un->un_sys_blocksize; 4977 } 4978 } 4979 pgeom_p->g_secsize = (unsigned short)lbasize; 4980 4981 /* 4982 * If the unit is a cd/dvd drive MODE SENSE page three 4983 * and MODE SENSE page four are reserved (see SBC spec 4984 * and MMC spec). To prevent soft errors just return 4985 * using the default LBA size. 4986 */ 4987 if (ISCD(un)) 4988 return (ret); 4989 4990 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4991 4992 /* 4993 * Retrieve MODE SENSE page 3 - Format Device Page 4994 */ 4995 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4996 ssc = sd_ssc_init(un); 4997 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp, 4998 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag); 4999 if (status != 0) { 5000 SD_ERROR(SD_LOG_COMMON, un, 5001 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5002 goto page3_exit; 5003 } 5004 5005 /* 5006 * Determine size of Block Descriptors in order to locate the mode 5007 * page data. ATAPI devices return 0, SCSI devices should return 5008 * MODE_BLK_DESC_LENGTH. 5009 */ 5010 headerp = (struct mode_header *)p3bufp; 5011 if (un->un_f_cfg_is_atapi == TRUE) { 5012 struct mode_header_grp2 *mhp = 5013 (struct mode_header_grp2 *)headerp; 5014 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5015 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5016 } else { 5017 mode_header_length = MODE_HEADER_LENGTH; 5018 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5019 } 5020 5021 if (bd_len > MODE_BLK_DESC_LENGTH) { 5022 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5023 "sd_get_physical_geometry: received unexpected bd_len " 5024 "of %d, page3\n", bd_len); 5025 status = EIO; 5026 goto page3_exit; 5027 } 5028 5029 page3p = (struct mode_format *) 5030 ((caddr_t)headerp + mode_header_length + bd_len); 5031 5032 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5033 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5034 "sd_get_physical_geometry: mode sense pg3 code mismatch " 5035 "%d\n", page3p->mode_page.code); 5036 status = EIO; 5037 goto page3_exit; 5038 } 5039 5040 /* 5041 * Use this physical geometry data only if BOTH MODE SENSE commands 5042 * complete successfully; otherwise, revert to the logical geometry. 5043 * So, we need to save everything in temporary variables. 5044 */ 5045 sector_size = BE_16(page3p->data_bytes_sect); 5046 5047 /* 5048 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5049 */ 5050 if (sector_size == 0) { 5051 sector_size = un->un_sys_blocksize; 5052 } else { 5053 sector_size &= ~(un->un_sys_blocksize - 1); 5054 } 5055 5056 nsect = BE_16(page3p->sect_track); 5057 intrlv = BE_16(page3p->interleave); 5058 5059 SD_INFO(SD_LOG_COMMON, un, 5060 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5061 SD_INFO(SD_LOG_COMMON, un, 5062 " mode page: %d; nsect: %d; sector size: %d;\n", 5063 page3p->mode_page.code, nsect, sector_size); 5064 SD_INFO(SD_LOG_COMMON, un, 5065 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5066 BE_16(page3p->track_skew), 5067 BE_16(page3p->cylinder_skew)); 5068 5069 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5070 5071 /* 5072 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5073 */ 5074 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5075 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp, 5076 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag); 5077 if (status != 0) { 5078 SD_ERROR(SD_LOG_COMMON, un, 5079 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5080 goto page4_exit; 5081 } 5082 5083 /* 5084 * Determine size of Block Descriptors in order to locate the mode 5085 * page data. ATAPI devices return 0, SCSI devices should return 5086 * MODE_BLK_DESC_LENGTH. 5087 */ 5088 headerp = (struct mode_header *)p4bufp; 5089 if (un->un_f_cfg_is_atapi == TRUE) { 5090 struct mode_header_grp2 *mhp = 5091 (struct mode_header_grp2 *)headerp; 5092 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5093 } else { 5094 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5095 } 5096 5097 if (bd_len > MODE_BLK_DESC_LENGTH) { 5098 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5099 "sd_get_physical_geometry: received unexpected bd_len of " 5100 "%d, page4\n", bd_len); 5101 status = EIO; 5102 goto page4_exit; 5103 } 5104 5105 page4p = (struct mode_geometry *) 5106 ((caddr_t)headerp + mode_header_length + bd_len); 5107 5108 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5109 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5110 "sd_get_physical_geometry: mode sense pg4 code mismatch " 5111 "%d\n", page4p->mode_page.code); 5112 status = EIO; 5113 goto page4_exit; 5114 } 5115 5116 /* 5117 * Stash the data now, after we know that both commands completed. 5118 */ 5119 5120 5121 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5122 spc = nhead * nsect; 5123 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5124 rpm = BE_16(page4p->rpm); 5125 5126 modesense_capacity = spc * ncyl; 5127 5128 SD_INFO(SD_LOG_COMMON, un, 5129 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5130 SD_INFO(SD_LOG_COMMON, un, 5131 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5132 SD_INFO(SD_LOG_COMMON, un, 5133 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5134 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5135 (void *)pgeom_p, capacity); 5136 5137 /* 5138 * Compensate if the drive's geometry is not rectangular, i.e., 5139 * the product of C * H * S returned by MODE SENSE >= that returned 5140 * by read capacity. This is an idiosyncrasy of the original x86 5141 * disk subsystem. 5142 */ 5143 if (modesense_capacity >= capacity) { 5144 SD_INFO(SD_LOG_COMMON, un, 5145 "sd_get_physical_geometry: adjusting acyl; " 5146 "old: %d; new: %d\n", pgeom_p->g_acyl, 5147 (modesense_capacity - capacity + spc - 1) / spc); 5148 if (sector_size != 0) { 5149 /* 1243403: NEC D38x7 drives don't support sec size */ 5150 pgeom_p->g_secsize = (unsigned short)sector_size; 5151 } 5152 pgeom_p->g_nsect = (unsigned short)nsect; 5153 pgeom_p->g_nhead = (unsigned short)nhead; 5154 pgeom_p->g_capacity = capacity; 5155 pgeom_p->g_acyl = 5156 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5157 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5158 } 5159 5160 pgeom_p->g_rpm = (unsigned short)rpm; 5161 pgeom_p->g_intrlv = (unsigned short)intrlv; 5162 ret = 0; 5163 5164 SD_INFO(SD_LOG_COMMON, un, 5165 "sd_get_physical_geometry: mode sense geometry:\n"); 5166 SD_INFO(SD_LOG_COMMON, un, 5167 " nsect: %d; sector size: %d; interlv: %d\n", 5168 nsect, sector_size, intrlv); 5169 SD_INFO(SD_LOG_COMMON, un, 5170 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5171 nhead, ncyl, rpm, modesense_capacity); 5172 SD_INFO(SD_LOG_COMMON, un, 5173 "sd_get_physical_geometry: (cached)\n"); 5174 SD_INFO(SD_LOG_COMMON, un, 5175 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5176 pgeom_p->g_ncyl, pgeom_p->g_acyl, 5177 pgeom_p->g_nhead, pgeom_p->g_nsect); 5178 SD_INFO(SD_LOG_COMMON, un, 5179 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5180 pgeom_p->g_secsize, pgeom_p->g_capacity, 5181 pgeom_p->g_intrlv, pgeom_p->g_rpm); 5182 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5183 5184 page4_exit: 5185 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5186 5187 page3_exit: 5188 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5189 5190 if (status != 0) { 5191 if (status == EIO) { 5192 /* 5193 * Some disks do not support mode sense(6), we 5194 * should ignore this kind of error(sense key is 5195 * 0x5 - illegal request). 5196 */ 5197 uint8_t *sensep; 5198 int senlen; 5199 5200 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 5201 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 5202 ssc->ssc_uscsi_cmd->uscsi_rqresid); 5203 5204 if (senlen > 0 && 5205 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 5206 sd_ssc_assessment(ssc, 5207 SD_FMT_IGNORE_COMPROMISE); 5208 } else { 5209 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 5210 } 5211 } else { 5212 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5213 } 5214 } 5215 sd_ssc_fini(ssc); 5216 return (ret); 5217 } 5218 5219 /* 5220 * Function: sd_get_virtual_geometry 5221 * 5222 * Description: Ask the controller to tell us about the target device. 5223 * 5224 * Arguments: un - pointer to softstate 5225 * capacity - disk capacity in #blocks 5226 * lbasize - disk block size in bytes 5227 * 5228 * Context: Kernel thread only 5229 */ 5230 5231 static int 5232 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 5233 diskaddr_t capacity, int lbasize) 5234 { 5235 uint_t geombuf; 5236 int spc; 5237 5238 ASSERT(un != NULL); 5239 5240 /* Set sector size, and total number of sectors */ 5241 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5242 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5243 5244 /* Let the HBA tell us its geometry */ 5245 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5246 5247 /* A value of -1 indicates an undefined "geometry" property */ 5248 if (geombuf == (-1)) { 5249 return (EINVAL); 5250 } 5251 5252 /* Initialize the logical geometry cache. */ 5253 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5254 lgeom_p->g_nsect = geombuf & 0xffff; 5255 lgeom_p->g_secsize = un->un_sys_blocksize; 5256 5257 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5258 5259 /* 5260 * Note: The driver originally converted the capacity value from 5261 * target blocks to system blocks. However, the capacity value passed 5262 * to this routine is already in terms of system blocks (this scaling 5263 * is done when the READ CAPACITY command is issued and processed). 5264 * This 'error' may have gone undetected because the usage of g_ncyl 5265 * (which is based upon g_capacity) is very limited within the driver 5266 */ 5267 lgeom_p->g_capacity = capacity; 5268 5269 /* 5270 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5271 * hba may return zero values if the device has been removed. 5272 */ 5273 if (spc == 0) { 5274 lgeom_p->g_ncyl = 0; 5275 } else { 5276 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5277 } 5278 lgeom_p->g_acyl = 0; 5279 5280 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5281 return (0); 5282 5283 } 5284 /* 5285 * Function: sd_update_block_info 5286 * 5287 * Description: Calculate a byte count to sector count bitshift value 5288 * from sector size. 5289 * 5290 * Arguments: un: unit struct. 5291 * lbasize: new target sector size 5292 * capacity: new target capacity, ie. block count 5293 * 5294 * Context: Kernel thread context 5295 */ 5296 5297 static void 5298 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5299 { 5300 if (lbasize != 0) { 5301 un->un_tgt_blocksize = lbasize; 5302 un->un_f_tgt_blocksize_is_valid = TRUE; 5303 if (!un->un_f_has_removable_media) { 5304 un->un_sys_blocksize = lbasize; 5305 } 5306 } 5307 5308 if (capacity != 0) { 5309 un->un_blockcount = capacity; 5310 un->un_f_blockcount_is_valid = TRUE; 5311 5312 /* 5313 * The capacity has changed so update the errstats. 5314 */ 5315 if (un->un_errstats != NULL) { 5316 struct sd_errstats *stp; 5317 5318 capacity *= un->un_sys_blocksize; 5319 stp = (struct sd_errstats *)un->un_errstats->ks_data; 5320 if (stp->sd_capacity.value.ui64 < capacity) 5321 stp->sd_capacity.value.ui64 = capacity; 5322 } 5323 } 5324 } 5325 5326 5327 /* 5328 * Function: sd_register_devid 5329 * 5330 * Description: This routine will obtain the device id information from the 5331 * target, obtain the serial number, and register the device 5332 * id with the ddi framework. 5333 * 5334 * Arguments: devi - the system's dev_info_t for the device. 5335 * un - driver soft state (unit) structure 5336 * reservation_flag - indicates if a reservation conflict 5337 * occurred during attach 5338 * 5339 * Context: Kernel Thread 5340 */ 5341 static void 5342 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag) 5343 { 5344 int rval = 0; 5345 uchar_t *inq80 = NULL; 5346 size_t inq80_len = MAX_INQUIRY_SIZE; 5347 size_t inq80_resid = 0; 5348 uchar_t *inq83 = NULL; 5349 size_t inq83_len = MAX_INQUIRY_SIZE; 5350 size_t inq83_resid = 0; 5351 int dlen, len; 5352 char *sn; 5353 struct sd_lun *un; 5354 5355 ASSERT(ssc != NULL); 5356 un = ssc->ssc_un; 5357 ASSERT(un != NULL); 5358 ASSERT(mutex_owned(SD_MUTEX(un))); 5359 ASSERT((SD_DEVINFO(un)) == devi); 5360 5361 5362 /* 5363 * We check the availability of the World Wide Name (0x83) and Unit 5364 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5365 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5366 * 0x83 is available, that is the best choice. Our next choice is 5367 * 0x80. If neither are available, we munge the devid from the device 5368 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5369 * to fabricate a devid for non-Sun qualified disks. 5370 */ 5371 if (sd_check_vpd_page_support(ssc) == 0) { 5372 /* collect page 80 data if available */ 5373 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5374 5375 mutex_exit(SD_MUTEX(un)); 5376 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5377 5378 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len, 5379 0x01, 0x80, &inq80_resid); 5380 5381 if (rval != 0) { 5382 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5383 kmem_free(inq80, inq80_len); 5384 inq80 = NULL; 5385 inq80_len = 0; 5386 } else if (ddi_prop_exists( 5387 DDI_DEV_T_NONE, SD_DEVINFO(un), 5388 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 5389 INQUIRY_SERIAL_NO) == 0) { 5390 /* 5391 * If we don't already have a serial number 5392 * property, do quick verify of data returned 5393 * and define property. 5394 */ 5395 dlen = inq80_len - inq80_resid; 5396 len = (size_t)inq80[3]; 5397 if ((dlen >= 4) && ((len + 4) <= dlen)) { 5398 /* 5399 * Ensure sn termination, skip leading 5400 * blanks, and create property 5401 * 'inquiry-serial-no'. 5402 */ 5403 sn = (char *)&inq80[4]; 5404 sn[len] = 0; 5405 while (*sn && (*sn == ' ')) 5406 sn++; 5407 if (*sn) { 5408 (void) ddi_prop_update_string( 5409 DDI_DEV_T_NONE, 5410 SD_DEVINFO(un), 5411 INQUIRY_SERIAL_NO, sn); 5412 } 5413 } 5414 } 5415 mutex_enter(SD_MUTEX(un)); 5416 } 5417 5418 /* collect page 83 data if available */ 5419 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5420 mutex_exit(SD_MUTEX(un)); 5421 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5422 5423 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len, 5424 0x01, 0x83, &inq83_resid); 5425 5426 if (rval != 0) { 5427 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5428 kmem_free(inq83, inq83_len); 5429 inq83 = NULL; 5430 inq83_len = 0; 5431 } 5432 mutex_enter(SD_MUTEX(un)); 5433 } 5434 } 5435 5436 /* 5437 * If transport has already registered a devid for this target 5438 * then that takes precedence over the driver's determination 5439 * of the devid. 5440 * 5441 * NOTE: The reason this check is done here instead of at the beginning 5442 * of the function is to allow the code above to create the 5443 * 'inquiry-serial-no' property. 5444 */ 5445 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 5446 ASSERT(un->un_devid); 5447 un->un_f_devid_transport_defined = TRUE; 5448 goto cleanup; /* use devid registered by the transport */ 5449 } 5450 5451 /* 5452 * This is the case of antiquated Sun disk drives that have the 5453 * FAB_DEVID property set in the disk_table. These drives 5454 * manage the devid's by storing them in last 2 available sectors 5455 * on the drive and have them fabricated by the ddi layer by calling 5456 * ddi_devid_init and passing the DEVID_FAB flag. 5457 */ 5458 if (un->un_f_opt_fab_devid == TRUE) { 5459 /* 5460 * Depending on EINVAL isn't reliable, since a reserved disk 5461 * may result in invalid geometry, so check to make sure a 5462 * reservation conflict did not occur during attach. 5463 */ 5464 if ((sd_get_devid(ssc) == EINVAL) && 5465 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5466 /* 5467 * The devid is invalid AND there is no reservation 5468 * conflict. Fabricate a new devid. 5469 */ 5470 (void) sd_create_devid(ssc); 5471 } 5472 5473 /* Register the devid if it exists */ 5474 if (un->un_devid != NULL) { 5475 (void) ddi_devid_register(SD_DEVINFO(un), 5476 un->un_devid); 5477 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5478 "sd_register_devid: Devid Fabricated\n"); 5479 } 5480 goto cleanup; 5481 } 5482 5483 /* encode best devid possible based on data available */ 5484 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5485 (char *)ddi_driver_name(SD_DEVINFO(un)), 5486 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5487 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5488 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5489 5490 /* devid successfully encoded, register devid */ 5491 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5492 5493 } else { 5494 /* 5495 * Unable to encode a devid based on data available. 5496 * This is not a Sun qualified disk. Older Sun disk 5497 * drives that have the SD_FAB_DEVID property 5498 * set in the disk_table and non Sun qualified 5499 * disks are treated in the same manner. These 5500 * drives manage the devid's by storing them in 5501 * last 2 available sectors on the drive and 5502 * have them fabricated by the ddi layer by 5503 * calling ddi_devid_init and passing the 5504 * DEVID_FAB flag. 5505 * Create a fabricate devid only if there's no 5506 * fabricate devid existed. 5507 */ 5508 if (sd_get_devid(ssc) == EINVAL) { 5509 (void) sd_create_devid(ssc); 5510 } 5511 un->un_f_opt_fab_devid = TRUE; 5512 5513 /* Register the devid if it exists */ 5514 if (un->un_devid != NULL) { 5515 (void) ddi_devid_register(SD_DEVINFO(un), 5516 un->un_devid); 5517 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5518 "sd_register_devid: devid fabricated using " 5519 "ddi framework\n"); 5520 } 5521 } 5522 5523 cleanup: 5524 /* clean up resources */ 5525 if (inq80 != NULL) { 5526 kmem_free(inq80, inq80_len); 5527 } 5528 if (inq83 != NULL) { 5529 kmem_free(inq83, inq83_len); 5530 } 5531 } 5532 5533 5534 5535 /* 5536 * Function: sd_get_devid 5537 * 5538 * Description: This routine will return 0 if a valid device id has been 5539 * obtained from the target and stored in the soft state. If a 5540 * valid device id has not been previously read and stored, a 5541 * read attempt will be made. 5542 * 5543 * Arguments: un - driver soft state (unit) structure 5544 * 5545 * Return Code: 0 if we successfully get the device id 5546 * 5547 * Context: Kernel Thread 5548 */ 5549 5550 static int 5551 sd_get_devid(sd_ssc_t *ssc) 5552 { 5553 struct dk_devid *dkdevid; 5554 ddi_devid_t tmpid; 5555 uint_t *ip; 5556 size_t sz; 5557 diskaddr_t blk; 5558 int status; 5559 int chksum; 5560 int i; 5561 size_t buffer_size; 5562 struct sd_lun *un; 5563 5564 ASSERT(ssc != NULL); 5565 un = ssc->ssc_un; 5566 ASSERT(un != NULL); 5567 ASSERT(mutex_owned(SD_MUTEX(un))); 5568 5569 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5570 un); 5571 5572 if (un->un_devid != NULL) { 5573 return (0); 5574 } 5575 5576 mutex_exit(SD_MUTEX(un)); 5577 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5578 (void *)SD_PATH_DIRECT) != 0) { 5579 mutex_enter(SD_MUTEX(un)); 5580 return (EINVAL); 5581 } 5582 5583 /* 5584 * Read and verify device id, stored in the reserved cylinders at the 5585 * end of the disk. Backup label is on the odd sectors of the last 5586 * track of the last cylinder. Device id will be on track of the next 5587 * to last cylinder. 5588 */ 5589 mutex_enter(SD_MUTEX(un)); 5590 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5591 mutex_exit(SD_MUTEX(un)); 5592 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5593 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk, 5594 SD_PATH_DIRECT); 5595 5596 if (status != 0) { 5597 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5598 goto error; 5599 } 5600 5601 /* Validate the revision */ 5602 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5603 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5604 status = EINVAL; 5605 goto error; 5606 } 5607 5608 /* Calculate the checksum */ 5609 chksum = 0; 5610 ip = (uint_t *)dkdevid; 5611 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5612 i++) { 5613 chksum ^= ip[i]; 5614 } 5615 5616 /* Compare the checksums */ 5617 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5618 status = EINVAL; 5619 goto error; 5620 } 5621 5622 /* Validate the device id */ 5623 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 5624 status = EINVAL; 5625 goto error; 5626 } 5627 5628 /* 5629 * Store the device id in the driver soft state 5630 */ 5631 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 5632 tmpid = kmem_alloc(sz, KM_SLEEP); 5633 5634 mutex_enter(SD_MUTEX(un)); 5635 5636 un->un_devid = tmpid; 5637 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 5638 5639 kmem_free(dkdevid, buffer_size); 5640 5641 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 5642 5643 return (status); 5644 error: 5645 mutex_enter(SD_MUTEX(un)); 5646 kmem_free(dkdevid, buffer_size); 5647 return (status); 5648 } 5649 5650 5651 /* 5652 * Function: sd_create_devid 5653 * 5654 * Description: This routine will fabricate the device id and write it 5655 * to the disk. 5656 * 5657 * Arguments: un - driver soft state (unit) structure 5658 * 5659 * Return Code: value of the fabricated device id 5660 * 5661 * Context: Kernel Thread 5662 */ 5663 5664 static ddi_devid_t 5665 sd_create_devid(sd_ssc_t *ssc) 5666 { 5667 struct sd_lun *un; 5668 5669 ASSERT(ssc != NULL); 5670 un = ssc->ssc_un; 5671 ASSERT(un != NULL); 5672 5673 /* Fabricate the devid */ 5674 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 5675 == DDI_FAILURE) { 5676 return (NULL); 5677 } 5678 5679 /* Write the devid to disk */ 5680 if (sd_write_deviceid(ssc) != 0) { 5681 ddi_devid_free(un->un_devid); 5682 un->un_devid = NULL; 5683 } 5684 5685 return (un->un_devid); 5686 } 5687 5688 5689 /* 5690 * Function: sd_write_deviceid 5691 * 5692 * Description: This routine will write the device id to the disk 5693 * reserved sector. 5694 * 5695 * Arguments: un - driver soft state (unit) structure 5696 * 5697 * Return Code: EINVAL 5698 * value returned by sd_send_scsi_cmd 5699 * 5700 * Context: Kernel Thread 5701 */ 5702 5703 static int 5704 sd_write_deviceid(sd_ssc_t *ssc) 5705 { 5706 struct dk_devid *dkdevid; 5707 uchar_t *buf; 5708 diskaddr_t blk; 5709 uint_t *ip, chksum; 5710 int status; 5711 int i; 5712 struct sd_lun *un; 5713 5714 ASSERT(ssc != NULL); 5715 un = ssc->ssc_un; 5716 ASSERT(un != NULL); 5717 ASSERT(mutex_owned(SD_MUTEX(un))); 5718 5719 mutex_exit(SD_MUTEX(un)); 5720 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5721 (void *)SD_PATH_DIRECT) != 0) { 5722 mutex_enter(SD_MUTEX(un)); 5723 return (-1); 5724 } 5725 5726 5727 /* Allocate the buffer */ 5728 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 5729 dkdevid = (struct dk_devid *)buf; 5730 5731 /* Fill in the revision */ 5732 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5733 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5734 5735 /* Copy in the device id */ 5736 mutex_enter(SD_MUTEX(un)); 5737 bcopy(un->un_devid, &dkdevid->dkd_devid, 5738 ddi_devid_sizeof(un->un_devid)); 5739 mutex_exit(SD_MUTEX(un)); 5740 5741 /* Calculate the checksum */ 5742 chksum = 0; 5743 ip = (uint_t *)dkdevid; 5744 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5745 i++) { 5746 chksum ^= ip[i]; 5747 } 5748 5749 /* Fill-in checksum */ 5750 DKD_FORMCHKSUM(chksum, dkdevid); 5751 5752 /* Write the reserved sector */ 5753 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk, 5754 SD_PATH_DIRECT); 5755 if (status != 0) 5756 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5757 5758 kmem_free(buf, un->un_sys_blocksize); 5759 5760 mutex_enter(SD_MUTEX(un)); 5761 return (status); 5762 } 5763 5764 5765 /* 5766 * Function: sd_check_vpd_page_support 5767 * 5768 * Description: This routine sends an inquiry command with the EVPD bit set and 5769 * a page code of 0x00 to the device. It is used to determine which 5770 * vital product pages are available to find the devid. We are 5771 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1, 5772 * the device does not support that command. 5773 * 5774 * Arguments: un - driver soft state (unit) structure 5775 * 5776 * Return Code: 0 - success 5777 * 1 - check condition 5778 * 5779 * Context: This routine can sleep. 5780 */ 5781 5782 static int 5783 sd_check_vpd_page_support(sd_ssc_t *ssc) 5784 { 5785 uchar_t *page_list = NULL; 5786 uchar_t page_length = 0xff; /* Use max possible length */ 5787 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5788 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5789 int rval = 0; 5790 int counter; 5791 struct sd_lun *un; 5792 5793 ASSERT(ssc != NULL); 5794 un = ssc->ssc_un; 5795 ASSERT(un != NULL); 5796 ASSERT(mutex_owned(SD_MUTEX(un))); 5797 5798 mutex_exit(SD_MUTEX(un)); 5799 5800 /* 5801 * We'll set the page length to the maximum to save figuring it out 5802 * with an additional call. 5803 */ 5804 page_list = kmem_zalloc(page_length, KM_SLEEP); 5805 5806 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd, 5807 page_code, NULL); 5808 5809 if (rval != 0) 5810 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5811 5812 mutex_enter(SD_MUTEX(un)); 5813 5814 /* 5815 * Now we must validate that the device accepted the command, as some 5816 * drives do not support it. If the drive does support it, we will 5817 * return 0, and the supported pages will be in un_vpd_page_mask. If 5818 * not, we return -1. 5819 */ 5820 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5821 /* Loop to find one of the 2 pages we need */ 5822 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5823 5824 /* 5825 * Pages are returned in ascending order, and 0x83 is what we 5826 * are hoping for. 5827 */ 5828 while ((page_list[counter] <= 0xB1) && 5829 (counter <= (page_list[VPD_PAGE_LENGTH] + 5830 VPD_HEAD_OFFSET))) { 5831 /* 5832 * Add 3 because page_list[3] is the number of 5833 * pages minus 3 5834 */ 5835 5836 switch (page_list[counter]) { 5837 case 0x00: 5838 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5839 break; 5840 case 0x80: 5841 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5842 break; 5843 case 0x81: 5844 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5845 break; 5846 case 0x82: 5847 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5848 break; 5849 case 0x83: 5850 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5851 break; 5852 case 0x86: 5853 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5854 break; 5855 case 0xB1: 5856 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG; 5857 break; 5858 } 5859 counter++; 5860 } 5861 5862 } else { 5863 rval = -1; 5864 5865 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5866 "sd_check_vpd_page_support: This drive does not implement " 5867 "VPD pages.\n"); 5868 } 5869 5870 kmem_free(page_list, page_length); 5871 5872 return (rval); 5873 } 5874 5875 5876 /* 5877 * Function: sd_setup_pm 5878 * 5879 * Description: Initialize Power Management on the device 5880 * 5881 * Context: Kernel Thread 5882 */ 5883 5884 static void 5885 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi) 5886 { 5887 uint_t log_page_size; 5888 uchar_t *log_page_data; 5889 int rval = 0; 5890 struct sd_lun *un; 5891 5892 ASSERT(ssc != NULL); 5893 un = ssc->ssc_un; 5894 ASSERT(un != NULL); 5895 5896 /* 5897 * Since we are called from attach, holding a mutex for 5898 * un is unnecessary. Because some of the routines called 5899 * from here require SD_MUTEX to not be held, assert this 5900 * right up front. 5901 */ 5902 ASSERT(!mutex_owned(SD_MUTEX(un))); 5903 /* 5904 * Since the sd device does not have the 'reg' property, 5905 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5906 * The following code is to tell cpr that this device 5907 * DOES need to be suspended and resumed. 5908 */ 5909 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5910 "pm-hardware-state", "needs-suspend-resume"); 5911 5912 /* 5913 * This complies with the new power management framework 5914 * for certain desktop machines. Create the pm_components 5915 * property as a string array property. 5916 * If un_f_pm_supported is TRUE, that means the disk 5917 * attached HBA has set the "pm-capable" property and 5918 * the value of this property is bigger than 0. 5919 */ 5920 if (un->un_f_pm_supported) { 5921 /* 5922 * not all devices have a motor, try it first. 5923 * some devices may return ILLEGAL REQUEST, some 5924 * will hang 5925 * The following START_STOP_UNIT is used to check if target 5926 * device has a motor. 5927 */ 5928 un->un_f_start_stop_supported = TRUE; 5929 5930 if (un->un_f_power_condition_supported) { 5931 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5932 SD_POWER_CONDITION, SD_TARGET_ACTIVE, 5933 SD_PATH_DIRECT); 5934 if (rval != 0) { 5935 un->un_f_power_condition_supported = FALSE; 5936 } 5937 } 5938 if (!un->un_f_power_condition_supported) { 5939 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5940 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT); 5941 } 5942 if (rval != 0) { 5943 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5944 un->un_f_start_stop_supported = FALSE; 5945 } 5946 5947 /* 5948 * create pm properties anyways otherwise the parent can't 5949 * go to sleep 5950 */ 5951 un->un_f_pm_is_enabled = TRUE; 5952 (void) sd_create_pm_components(devi, un); 5953 5954 /* 5955 * If it claims that log sense is supported, check it out. 5956 */ 5957 if (un->un_f_log_sense_supported) { 5958 rval = sd_log_page_supported(ssc, 5959 START_STOP_CYCLE_PAGE); 5960 if (rval == 1) { 5961 /* Page found, use it. */ 5962 un->un_start_stop_cycle_page = 5963 START_STOP_CYCLE_PAGE; 5964 } else { 5965 /* 5966 * Page not found or log sense is not 5967 * supported. 5968 * Notice we do not check the old style 5969 * START_STOP_CYCLE_VU_PAGE because this 5970 * code path does not apply to old disks. 5971 */ 5972 un->un_f_log_sense_supported = FALSE; 5973 un->un_f_pm_log_sense_smart = FALSE; 5974 } 5975 } 5976 5977 return; 5978 } 5979 5980 /* 5981 * For the disk whose attached HBA has not set the "pm-capable" 5982 * property, check if it supports the power management. 5983 */ 5984 if (!un->un_f_log_sense_supported) { 5985 un->un_power_level = SD_SPINDLE_ON; 5986 un->un_f_pm_is_enabled = FALSE; 5987 return; 5988 } 5989 5990 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE); 5991 5992 #ifdef SDDEBUG 5993 if (sd_force_pm_supported) { 5994 /* Force a successful result */ 5995 rval = 1; 5996 } 5997 #endif 5998 5999 /* 6000 * If the start-stop cycle counter log page is not supported 6001 * or if the pm-capable property is set to be false (0), 6002 * then we should not create the pm_components property. 6003 */ 6004 if (rval == -1) { 6005 /* 6006 * Error. 6007 * Reading log sense failed, most likely this is 6008 * an older drive that does not support log sense. 6009 * If this fails auto-pm is not supported. 6010 */ 6011 un->un_power_level = SD_SPINDLE_ON; 6012 un->un_f_pm_is_enabled = FALSE; 6013 6014 } else if (rval == 0) { 6015 /* 6016 * Page not found. 6017 * The start stop cycle counter is implemented as page 6018 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6019 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6020 */ 6021 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) { 6022 /* 6023 * Page found, use this one. 6024 */ 6025 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6026 un->un_f_pm_is_enabled = TRUE; 6027 } else { 6028 /* 6029 * Error or page not found. 6030 * auto-pm is not supported for this device. 6031 */ 6032 un->un_power_level = SD_SPINDLE_ON; 6033 un->un_f_pm_is_enabled = FALSE; 6034 } 6035 } else { 6036 /* 6037 * Page found, use it. 6038 */ 6039 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6040 un->un_f_pm_is_enabled = TRUE; 6041 } 6042 6043 6044 if (un->un_f_pm_is_enabled == TRUE) { 6045 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6046 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6047 6048 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6049 log_page_size, un->un_start_stop_cycle_page, 6050 0x01, 0, SD_PATH_DIRECT); 6051 6052 if (rval != 0) { 6053 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6054 } 6055 6056 #ifdef SDDEBUG 6057 if (sd_force_pm_supported) { 6058 /* Force a successful result */ 6059 rval = 0; 6060 } 6061 #endif 6062 6063 /* 6064 * If the Log sense for Page( Start/stop cycle counter page) 6065 * succeeds, then power management is supported and we can 6066 * enable auto-pm. 6067 */ 6068 if (rval == 0) { 6069 (void) sd_create_pm_components(devi, un); 6070 } else { 6071 un->un_power_level = SD_SPINDLE_ON; 6072 un->un_f_pm_is_enabled = FALSE; 6073 } 6074 6075 kmem_free(log_page_data, log_page_size); 6076 } 6077 } 6078 6079 6080 /* 6081 * Function: sd_create_pm_components 6082 * 6083 * Description: Initialize PM property. 6084 * 6085 * Context: Kernel thread context 6086 */ 6087 6088 static void 6089 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6090 { 6091 ASSERT(!mutex_owned(SD_MUTEX(un))); 6092 6093 if (un->un_f_power_condition_supported) { 6094 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6095 "pm-components", sd_pwr_pc.pm_comp, 5) 6096 != DDI_PROP_SUCCESS) { 6097 un->un_power_level = SD_SPINDLE_ACTIVE; 6098 un->un_f_pm_is_enabled = FALSE; 6099 return; 6100 } 6101 } else { 6102 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6103 "pm-components", sd_pwr_ss.pm_comp, 3) 6104 != DDI_PROP_SUCCESS) { 6105 un->un_power_level = SD_SPINDLE_ON; 6106 un->un_f_pm_is_enabled = FALSE; 6107 return; 6108 } 6109 } 6110 /* 6111 * When components are initially created they are idle, 6112 * power up any non-removables. 6113 * Note: the return value of pm_raise_power can't be used 6114 * for determining if PM should be enabled for this device. 6115 * Even if you check the return values and remove this 6116 * property created above, the PM framework will not honor the 6117 * change after the first call to pm_raise_power. Hence, 6118 * removal of that property does not help if pm_raise_power 6119 * fails. In the case of removable media, the start/stop 6120 * will fail if the media is not present. 6121 */ 6122 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6123 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) { 6124 mutex_enter(SD_MUTEX(un)); 6125 un->un_power_level = SD_PM_STATE_ACTIVE(un); 6126 mutex_enter(&un->un_pm_mutex); 6127 /* Set to on and not busy. */ 6128 un->un_pm_count = 0; 6129 } else { 6130 mutex_enter(SD_MUTEX(un)); 6131 un->un_power_level = SD_PM_STATE_STOPPED(un); 6132 mutex_enter(&un->un_pm_mutex); 6133 /* Set to off. */ 6134 un->un_pm_count = -1; 6135 } 6136 mutex_exit(&un->un_pm_mutex); 6137 mutex_exit(SD_MUTEX(un)); 6138 } 6139 6140 6141 /* 6142 * Function: sd_ddi_suspend 6143 * 6144 * Description: Performs system power-down operations. This includes 6145 * setting the drive state to indicate its suspended so 6146 * that no new commands will be accepted. Also, wait for 6147 * all commands that are in transport or queued to a timer 6148 * for retry to complete. All timeout threads are cancelled. 6149 * 6150 * Return Code: DDI_FAILURE or DDI_SUCCESS 6151 * 6152 * Context: Kernel thread context 6153 */ 6154 6155 static int 6156 sd_ddi_suspend(dev_info_t *devi) 6157 { 6158 struct sd_lun *un; 6159 clock_t wait_cmds_complete; 6160 6161 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6162 if (un == NULL) { 6163 return (DDI_FAILURE); 6164 } 6165 6166 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6167 6168 mutex_enter(SD_MUTEX(un)); 6169 6170 /* Return success if the device is already suspended. */ 6171 if (un->un_state == SD_STATE_SUSPENDED) { 6172 mutex_exit(SD_MUTEX(un)); 6173 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6174 "device already suspended, exiting\n"); 6175 return (DDI_SUCCESS); 6176 } 6177 6178 /* Return failure if the device is being used by HA */ 6179 if (un->un_resvd_status & 6180 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6181 mutex_exit(SD_MUTEX(un)); 6182 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6183 "device in use by HA, exiting\n"); 6184 return (DDI_FAILURE); 6185 } 6186 6187 /* 6188 * Return failure if the device is in a resource wait 6189 * or power changing state. 6190 */ 6191 if ((un->un_state == SD_STATE_RWAIT) || 6192 (un->un_state == SD_STATE_PM_CHANGING)) { 6193 mutex_exit(SD_MUTEX(un)); 6194 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6195 "device in resource wait state, exiting\n"); 6196 return (DDI_FAILURE); 6197 } 6198 6199 6200 un->un_save_state = un->un_last_state; 6201 New_state(un, SD_STATE_SUSPENDED); 6202 6203 /* 6204 * Wait for all commands that are in transport or queued to a timer 6205 * for retry to complete. 6206 * 6207 * While waiting, no new commands will be accepted or sent because of 6208 * the new state we set above. 6209 * 6210 * Wait till current operation has completed. If we are in the resource 6211 * wait state (with an intr outstanding) then we need to wait till the 6212 * intr completes and starts the next cmd. We want to wait for 6213 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6214 */ 6215 wait_cmds_complete = ddi_get_lbolt() + 6216 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6217 6218 while (un->un_ncmds_in_transport != 0) { 6219 /* 6220 * Fail if commands do not finish in the specified time. 6221 */ 6222 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6223 wait_cmds_complete) == -1) { 6224 /* 6225 * Undo the state changes made above. Everything 6226 * must go back to it's original value. 6227 */ 6228 Restore_state(un); 6229 un->un_last_state = un->un_save_state; 6230 /* Wake up any threads that might be waiting. */ 6231 cv_broadcast(&un->un_suspend_cv); 6232 mutex_exit(SD_MUTEX(un)); 6233 SD_ERROR(SD_LOG_IO_PM, un, 6234 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6235 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6236 return (DDI_FAILURE); 6237 } 6238 } 6239 6240 /* 6241 * Cancel SCSI watch thread and timeouts, if any are active 6242 */ 6243 6244 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6245 opaque_t temp_token = un->un_swr_token; 6246 mutex_exit(SD_MUTEX(un)); 6247 scsi_watch_suspend(temp_token); 6248 mutex_enter(SD_MUTEX(un)); 6249 } 6250 6251 if (un->un_reset_throttle_timeid != NULL) { 6252 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6253 un->un_reset_throttle_timeid = NULL; 6254 mutex_exit(SD_MUTEX(un)); 6255 (void) untimeout(temp_id); 6256 mutex_enter(SD_MUTEX(un)); 6257 } 6258 6259 if (un->un_dcvb_timeid != NULL) { 6260 timeout_id_t temp_id = un->un_dcvb_timeid; 6261 un->un_dcvb_timeid = NULL; 6262 mutex_exit(SD_MUTEX(un)); 6263 (void) untimeout(temp_id); 6264 mutex_enter(SD_MUTEX(un)); 6265 } 6266 6267 mutex_enter(&un->un_pm_mutex); 6268 if (un->un_pm_timeid != NULL) { 6269 timeout_id_t temp_id = un->un_pm_timeid; 6270 un->un_pm_timeid = NULL; 6271 mutex_exit(&un->un_pm_mutex); 6272 mutex_exit(SD_MUTEX(un)); 6273 (void) untimeout(temp_id); 6274 mutex_enter(SD_MUTEX(un)); 6275 } else { 6276 mutex_exit(&un->un_pm_mutex); 6277 } 6278 6279 if (un->un_rmw_msg_timeid != NULL) { 6280 timeout_id_t temp_id = un->un_rmw_msg_timeid; 6281 un->un_rmw_msg_timeid = NULL; 6282 mutex_exit(SD_MUTEX(un)); 6283 (void) untimeout(temp_id); 6284 mutex_enter(SD_MUTEX(un)); 6285 } 6286 6287 if (un->un_retry_timeid != NULL) { 6288 timeout_id_t temp_id = un->un_retry_timeid; 6289 un->un_retry_timeid = NULL; 6290 mutex_exit(SD_MUTEX(un)); 6291 (void) untimeout(temp_id); 6292 mutex_enter(SD_MUTEX(un)); 6293 6294 if (un->un_retry_bp != NULL) { 6295 un->un_retry_bp->av_forw = un->un_waitq_headp; 6296 un->un_waitq_headp = un->un_retry_bp; 6297 if (un->un_waitq_tailp == NULL) { 6298 un->un_waitq_tailp = un->un_retry_bp; 6299 } 6300 un->un_retry_bp = NULL; 6301 un->un_retry_statp = NULL; 6302 } 6303 } 6304 6305 if (un->un_direct_priority_timeid != NULL) { 6306 timeout_id_t temp_id = un->un_direct_priority_timeid; 6307 un->un_direct_priority_timeid = NULL; 6308 mutex_exit(SD_MUTEX(un)); 6309 (void) untimeout(temp_id); 6310 mutex_enter(SD_MUTEX(un)); 6311 } 6312 6313 if (un->un_f_is_fibre == TRUE) { 6314 /* 6315 * Remove callbacks for insert and remove events 6316 */ 6317 if (un->un_insert_event != NULL) { 6318 mutex_exit(SD_MUTEX(un)); 6319 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6320 mutex_enter(SD_MUTEX(un)); 6321 un->un_insert_event = NULL; 6322 } 6323 6324 if (un->un_remove_event != NULL) { 6325 mutex_exit(SD_MUTEX(un)); 6326 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6327 mutex_enter(SD_MUTEX(un)); 6328 un->un_remove_event = NULL; 6329 } 6330 } 6331 6332 mutex_exit(SD_MUTEX(un)); 6333 6334 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6335 6336 return (DDI_SUCCESS); 6337 } 6338 6339 6340 /* 6341 * Function: sd_ddi_resume 6342 * 6343 * Description: Performs system power-up operations.. 6344 * 6345 * Return Code: DDI_SUCCESS 6346 * DDI_FAILURE 6347 * 6348 * Context: Kernel thread context 6349 */ 6350 6351 static int 6352 sd_ddi_resume(dev_info_t *devi) 6353 { 6354 struct sd_lun *un; 6355 6356 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6357 if (un == NULL) { 6358 return (DDI_FAILURE); 6359 } 6360 6361 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6362 6363 mutex_enter(SD_MUTEX(un)); 6364 Restore_state(un); 6365 6366 /* 6367 * Restore the state which was saved to give the 6368 * the right state in un_last_state 6369 */ 6370 un->un_last_state = un->un_save_state; 6371 /* 6372 * Note: throttle comes back at full. 6373 * Also note: this MUST be done before calling pm_raise_power 6374 * otherwise the system can get hung in biowait. The scenario where 6375 * this'll happen is under cpr suspend. Writing of the system 6376 * state goes through sddump, which writes 0 to un_throttle. If 6377 * writing the system state then fails, example if the partition is 6378 * too small, then cpr attempts a resume. If throttle isn't restored 6379 * from the saved value until after calling pm_raise_power then 6380 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6381 * in biowait. 6382 */ 6383 un->un_throttle = un->un_saved_throttle; 6384 6385 /* 6386 * The chance of failure is very rare as the only command done in power 6387 * entry point is START command when you transition from 0->1 or 6388 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6389 * which suspend was done. Ignore the return value as the resume should 6390 * not be failed. In the case of removable media the media need not be 6391 * inserted and hence there is a chance that raise power will fail with 6392 * media not present. 6393 */ 6394 if (un->un_f_attach_spinup) { 6395 mutex_exit(SD_MUTEX(un)); 6396 (void) pm_raise_power(SD_DEVINFO(un), 0, 6397 SD_PM_STATE_ACTIVE(un)); 6398 mutex_enter(SD_MUTEX(un)); 6399 } 6400 6401 /* 6402 * Don't broadcast to the suspend cv and therefore possibly 6403 * start I/O until after power has been restored. 6404 */ 6405 cv_broadcast(&un->un_suspend_cv); 6406 cv_broadcast(&un->un_state_cv); 6407 6408 /* restart thread */ 6409 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6410 scsi_watch_resume(un->un_swr_token); 6411 } 6412 6413 #if (defined(__fibre)) 6414 if (un->un_f_is_fibre == TRUE) { 6415 /* 6416 * Add callbacks for insert and remove events 6417 */ 6418 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6419 sd_init_event_callbacks(un); 6420 } 6421 } 6422 #endif 6423 6424 /* 6425 * Transport any pending commands to the target. 6426 * 6427 * If this is a low-activity device commands in queue will have to wait 6428 * until new commands come in, which may take awhile. Also, we 6429 * specifically don't check un_ncmds_in_transport because we know that 6430 * there really are no commands in progress after the unit was 6431 * suspended and we could have reached the throttle level, been 6432 * suspended, and have no new commands coming in for awhile. Highly 6433 * unlikely, but so is the low-activity disk scenario. 6434 */ 6435 ddi_xbuf_dispatch(un->un_xbuf_attr); 6436 6437 sd_start_cmds(un, NULL); 6438 mutex_exit(SD_MUTEX(un)); 6439 6440 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6441 6442 return (DDI_SUCCESS); 6443 } 6444 6445 6446 /* 6447 * Function: sd_pm_state_change 6448 * 6449 * Description: Change the driver power state. 6450 * Someone else is required to actually change the driver 6451 * power level. 6452 * 6453 * Arguments: un - driver soft state (unit) structure 6454 * level - the power level that is changed to 6455 * flag - to decide how to change the power state 6456 * 6457 * Return Code: DDI_SUCCESS 6458 * 6459 * Context: Kernel thread context 6460 */ 6461 static int 6462 sd_pm_state_change(struct sd_lun *un, int level, int flag) 6463 { 6464 ASSERT(un != NULL); 6465 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n"); 6466 6467 ASSERT(!mutex_owned(SD_MUTEX(un))); 6468 mutex_enter(SD_MUTEX(un)); 6469 6470 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) { 6471 un->un_power_level = level; 6472 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6473 mutex_enter(&un->un_pm_mutex); 6474 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6475 un->un_pm_count++; 6476 ASSERT(un->un_pm_count == 0); 6477 } 6478 mutex_exit(&un->un_pm_mutex); 6479 } else { 6480 /* 6481 * Exit if power management is not enabled for this device, 6482 * or if the device is being used by HA. 6483 */ 6484 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6485 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6486 mutex_exit(SD_MUTEX(un)); 6487 SD_TRACE(SD_LOG_POWER, un, 6488 "sd_pm_state_change: exiting\n"); 6489 return (DDI_FAILURE); 6490 } 6491 6492 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: " 6493 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver); 6494 6495 /* 6496 * See if the device is not busy, ie.: 6497 * - we have no commands in the driver for this device 6498 * - not waiting for resources 6499 */ 6500 if ((un->un_ncmds_in_driver == 0) && 6501 (un->un_state != SD_STATE_RWAIT)) { 6502 /* 6503 * The device is not busy, so it is OK to go to low 6504 * power state. Indicate low power, but rely on someone 6505 * else to actually change it. 6506 */ 6507 mutex_enter(&un->un_pm_mutex); 6508 un->un_pm_count = -1; 6509 mutex_exit(&un->un_pm_mutex); 6510 un->un_power_level = level; 6511 } 6512 } 6513 6514 mutex_exit(SD_MUTEX(un)); 6515 6516 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n"); 6517 6518 return (DDI_SUCCESS); 6519 } 6520 6521 6522 /* 6523 * Function: sd_pm_idletimeout_handler 6524 * 6525 * Description: A timer routine that's active only while a device is busy. 6526 * The purpose is to extend slightly the pm framework's busy 6527 * view of the device to prevent busy/idle thrashing for 6528 * back-to-back commands. Do this by comparing the current time 6529 * to the time at which the last command completed and when the 6530 * difference is greater than sd_pm_idletime, call 6531 * pm_idle_component. In addition to indicating idle to the pm 6532 * framework, update the chain type to again use the internal pm 6533 * layers of the driver. 6534 * 6535 * Arguments: arg - driver soft state (unit) structure 6536 * 6537 * Context: Executes in a timeout(9F) thread context 6538 */ 6539 6540 static void 6541 sd_pm_idletimeout_handler(void *arg) 6542 { 6543 struct sd_lun *un = arg; 6544 6545 time_t now; 6546 6547 mutex_enter(&sd_detach_mutex); 6548 if (un->un_detach_count != 0) { 6549 /* Abort if the instance is detaching */ 6550 mutex_exit(&sd_detach_mutex); 6551 return; 6552 } 6553 mutex_exit(&sd_detach_mutex); 6554 6555 now = ddi_get_time(); 6556 /* 6557 * Grab both mutexes, in the proper order, since we're accessing 6558 * both PM and softstate variables. 6559 */ 6560 mutex_enter(SD_MUTEX(un)); 6561 mutex_enter(&un->un_pm_mutex); 6562 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6563 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6564 /* 6565 * Update the chain types. 6566 * This takes affect on the next new command received. 6567 */ 6568 if (un->un_f_non_devbsize_supported) { 6569 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6570 } else { 6571 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6572 } 6573 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6574 6575 SD_TRACE(SD_LOG_IO_PM, un, 6576 "sd_pm_idletimeout_handler: idling device\n"); 6577 (void) pm_idle_component(SD_DEVINFO(un), 0); 6578 un->un_pm_idle_timeid = NULL; 6579 } else { 6580 un->un_pm_idle_timeid = 6581 timeout(sd_pm_idletimeout_handler, un, 6582 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6583 } 6584 mutex_exit(&un->un_pm_mutex); 6585 mutex_exit(SD_MUTEX(un)); 6586 } 6587 6588 6589 /* 6590 * Function: sd_pm_timeout_handler 6591 * 6592 * Description: Callback to tell framework we are idle. 6593 * 6594 * Context: timeout(9f) thread context. 6595 */ 6596 6597 static void 6598 sd_pm_timeout_handler(void *arg) 6599 { 6600 struct sd_lun *un = arg; 6601 6602 (void) pm_idle_component(SD_DEVINFO(un), 0); 6603 mutex_enter(&un->un_pm_mutex); 6604 un->un_pm_timeid = NULL; 6605 mutex_exit(&un->un_pm_mutex); 6606 } 6607 6608 6609 /* 6610 * Function: sdpower 6611 * 6612 * Description: PM entry point. 6613 * 6614 * Return Code: DDI_SUCCESS 6615 * DDI_FAILURE 6616 * 6617 * Context: Kernel thread context 6618 */ 6619 6620 static int 6621 sdpower(dev_info_t *devi, int component, int level) 6622 { 6623 struct sd_lun *un; 6624 int instance; 6625 int rval = DDI_SUCCESS; 6626 uint_t i, log_page_size, maxcycles, ncycles; 6627 uchar_t *log_page_data; 6628 int log_sense_page; 6629 int medium_present; 6630 time_t intvlp; 6631 struct pm_trans_data sd_pm_tran_data; 6632 uchar_t save_state; 6633 int sval; 6634 uchar_t state_before_pm; 6635 int got_semaphore_here; 6636 sd_ssc_t *ssc; 6637 int last_power_level; 6638 6639 instance = ddi_get_instance(devi); 6640 6641 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6642 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) { 6643 return (DDI_FAILURE); 6644 } 6645 6646 ssc = sd_ssc_init(un); 6647 6648 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6649 6650 /* 6651 * Must synchronize power down with close. 6652 * Attempt to decrement/acquire the open/close semaphore, 6653 * but do NOT wait on it. If it's not greater than zero, 6654 * ie. it can't be decremented without waiting, then 6655 * someone else, either open or close, already has it 6656 * and the try returns 0. Use that knowledge here to determine 6657 * if it's OK to change the device power level. 6658 * Also, only increment it on exit if it was decremented, ie. gotten, 6659 * here. 6660 */ 6661 got_semaphore_here = sema_tryp(&un->un_semoclose); 6662 6663 mutex_enter(SD_MUTEX(un)); 6664 6665 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 6666 un->un_ncmds_in_driver); 6667 6668 /* 6669 * If un_ncmds_in_driver is non-zero it indicates commands are 6670 * already being processed in the driver, or if the semaphore was 6671 * not gotten here it indicates an open or close is being processed. 6672 * At the same time somebody is requesting to go to a lower power 6673 * that can't perform I/O, which can't happen, therefore we need to 6674 * return failure. 6675 */ 6676 if ((!SD_PM_IS_IO_CAPABLE(un, level)) && 6677 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 6678 mutex_exit(SD_MUTEX(un)); 6679 6680 if (got_semaphore_here != 0) { 6681 sema_v(&un->un_semoclose); 6682 } 6683 SD_TRACE(SD_LOG_IO_PM, un, 6684 "sdpower: exit, device has queued cmds.\n"); 6685 6686 goto sdpower_failed; 6687 } 6688 6689 /* 6690 * if it is OFFLINE that means the disk is completely dead 6691 * in our case we have to put the disk in on or off by sending commands 6692 * Of course that will fail anyway so return back here. 6693 * 6694 * Power changes to a device that's OFFLINE or SUSPENDED 6695 * are not allowed. 6696 */ 6697 if ((un->un_state == SD_STATE_OFFLINE) || 6698 (un->un_state == SD_STATE_SUSPENDED)) { 6699 mutex_exit(SD_MUTEX(un)); 6700 6701 if (got_semaphore_here != 0) { 6702 sema_v(&un->un_semoclose); 6703 } 6704 SD_TRACE(SD_LOG_IO_PM, un, 6705 "sdpower: exit, device is off-line.\n"); 6706 6707 goto sdpower_failed; 6708 } 6709 6710 /* 6711 * Change the device's state to indicate it's power level 6712 * is being changed. Do this to prevent a power off in the 6713 * middle of commands, which is especially bad on devices 6714 * that are really powered off instead of just spun down. 6715 */ 6716 state_before_pm = un->un_state; 6717 un->un_state = SD_STATE_PM_CHANGING; 6718 6719 mutex_exit(SD_MUTEX(un)); 6720 6721 /* 6722 * If log sense command is not supported, bypass the 6723 * following checking, otherwise, check the log sense 6724 * information for this device. 6725 */ 6726 if (SD_PM_STOP_MOTOR_NEEDED(un, level) && 6727 un->un_f_log_sense_supported) { 6728 /* 6729 * Get the log sense information to understand whether the 6730 * the powercycle counts have gone beyond the threshhold. 6731 */ 6732 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6733 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6734 6735 mutex_enter(SD_MUTEX(un)); 6736 log_sense_page = un->un_start_stop_cycle_page; 6737 mutex_exit(SD_MUTEX(un)); 6738 6739 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6740 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 6741 6742 if (rval != 0) { 6743 if (rval == EIO) 6744 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6745 else 6746 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6747 } 6748 6749 #ifdef SDDEBUG 6750 if (sd_force_pm_supported) { 6751 /* Force a successful result */ 6752 rval = 0; 6753 } 6754 #endif 6755 if (rval != 0) { 6756 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 6757 "Log Sense Failed\n"); 6758 6759 kmem_free(log_page_data, log_page_size); 6760 /* Cannot support power management on those drives */ 6761 6762 if (got_semaphore_here != 0) { 6763 sema_v(&un->un_semoclose); 6764 } 6765 /* 6766 * On exit put the state back to it's original value 6767 * and broadcast to anyone waiting for the power 6768 * change completion. 6769 */ 6770 mutex_enter(SD_MUTEX(un)); 6771 un->un_state = state_before_pm; 6772 cv_broadcast(&un->un_suspend_cv); 6773 mutex_exit(SD_MUTEX(un)); 6774 SD_TRACE(SD_LOG_IO_PM, un, 6775 "sdpower: exit, Log Sense Failed.\n"); 6776 6777 goto sdpower_failed; 6778 } 6779 6780 /* 6781 * From the page data - Convert the essential information to 6782 * pm_trans_data 6783 */ 6784 maxcycles = 6785 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 6786 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 6787 6788 ncycles = 6789 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 6790 (log_page_data[0x26] << 8) | log_page_data[0x27]; 6791 6792 if (un->un_f_pm_log_sense_smart) { 6793 sd_pm_tran_data.un.smart_count.allowed = maxcycles; 6794 sd_pm_tran_data.un.smart_count.consumed = ncycles; 6795 sd_pm_tran_data.un.smart_count.flag = 0; 6796 sd_pm_tran_data.format = DC_SMART_FORMAT; 6797 } else { 6798 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 6799 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 6800 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 6801 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 6802 log_page_data[8+i]; 6803 } 6804 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6805 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6806 } 6807 6808 kmem_free(log_page_data, log_page_size); 6809 6810 /* 6811 * Call pm_trans_check routine to get the Ok from 6812 * the global policy 6813 */ 6814 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6815 #ifdef SDDEBUG 6816 if (sd_force_pm_supported) { 6817 /* Force a successful result */ 6818 rval = 1; 6819 } 6820 #endif 6821 switch (rval) { 6822 case 0: 6823 /* 6824 * Not Ok to Power cycle or error in parameters passed 6825 * Would have given the advised time to consider power 6826 * cycle. Based on the new intvlp parameter we are 6827 * supposed to pretend we are busy so that pm framework 6828 * will never call our power entry point. Because of 6829 * that install a timeout handler and wait for the 6830 * recommended time to elapse so that power management 6831 * can be effective again. 6832 * 6833 * To effect this behavior, call pm_busy_component to 6834 * indicate to the framework this device is busy. 6835 * By not adjusting un_pm_count the rest of PM in 6836 * the driver will function normally, and independent 6837 * of this but because the framework is told the device 6838 * is busy it won't attempt powering down until it gets 6839 * a matching idle. The timeout handler sends this. 6840 * Note: sd_pm_entry can't be called here to do this 6841 * because sdpower may have been called as a result 6842 * of a call to pm_raise_power from within sd_pm_entry. 6843 * 6844 * If a timeout handler is already active then 6845 * don't install another. 6846 */ 6847 mutex_enter(&un->un_pm_mutex); 6848 if (un->un_pm_timeid == NULL) { 6849 un->un_pm_timeid = 6850 timeout(sd_pm_timeout_handler, 6851 un, intvlp * drv_usectohz(1000000)); 6852 mutex_exit(&un->un_pm_mutex); 6853 (void) pm_busy_component(SD_DEVINFO(un), 0); 6854 } else { 6855 mutex_exit(&un->un_pm_mutex); 6856 } 6857 if (got_semaphore_here != 0) { 6858 sema_v(&un->un_semoclose); 6859 } 6860 /* 6861 * On exit put the state back to it's original value 6862 * and broadcast to anyone waiting for the power 6863 * change completion. 6864 */ 6865 mutex_enter(SD_MUTEX(un)); 6866 un->un_state = state_before_pm; 6867 cv_broadcast(&un->un_suspend_cv); 6868 mutex_exit(SD_MUTEX(un)); 6869 6870 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6871 "trans check Failed, not ok to power cycle.\n"); 6872 6873 goto sdpower_failed; 6874 case -1: 6875 if (got_semaphore_here != 0) { 6876 sema_v(&un->un_semoclose); 6877 } 6878 /* 6879 * On exit put the state back to it's original value 6880 * and broadcast to anyone waiting for the power 6881 * change completion. 6882 */ 6883 mutex_enter(SD_MUTEX(un)); 6884 un->un_state = state_before_pm; 6885 cv_broadcast(&un->un_suspend_cv); 6886 mutex_exit(SD_MUTEX(un)); 6887 SD_TRACE(SD_LOG_IO_PM, un, 6888 "sdpower: exit, trans check command Failed.\n"); 6889 6890 goto sdpower_failed; 6891 } 6892 } 6893 6894 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6895 /* 6896 * Save the last state... if the STOP FAILS we need it 6897 * for restoring 6898 */ 6899 mutex_enter(SD_MUTEX(un)); 6900 save_state = un->un_last_state; 6901 last_power_level = un->un_power_level; 6902 /* 6903 * There must not be any cmds. getting processed 6904 * in the driver when we get here. Power to the 6905 * device is potentially going off. 6906 */ 6907 ASSERT(un->un_ncmds_in_driver == 0); 6908 mutex_exit(SD_MUTEX(un)); 6909 6910 /* 6911 * For now PM suspend the device completely before spindle is 6912 * turned off 6913 */ 6914 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE)) 6915 == DDI_FAILURE) { 6916 if (got_semaphore_here != 0) { 6917 sema_v(&un->un_semoclose); 6918 } 6919 /* 6920 * On exit put the state back to it's original value 6921 * and broadcast to anyone waiting for the power 6922 * change completion. 6923 */ 6924 mutex_enter(SD_MUTEX(un)); 6925 un->un_state = state_before_pm; 6926 un->un_power_level = last_power_level; 6927 cv_broadcast(&un->un_suspend_cv); 6928 mutex_exit(SD_MUTEX(un)); 6929 SD_TRACE(SD_LOG_IO_PM, un, 6930 "sdpower: exit, PM suspend Failed.\n"); 6931 6932 goto sdpower_failed; 6933 } 6934 } 6935 6936 /* 6937 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6938 * close, or strategy. Dump no long uses this routine, it uses it's 6939 * own code so it can be done in polled mode. 6940 */ 6941 6942 medium_present = TRUE; 6943 6944 /* 6945 * When powering up, issue a TUR in case the device is at unit 6946 * attention. Don't do retries. Bypass the PM layer, otherwise 6947 * a deadlock on un_pm_busy_cv will occur. 6948 */ 6949 if (SD_PM_IS_IO_CAPABLE(un, level)) { 6950 sval = sd_send_scsi_TEST_UNIT_READY(ssc, 6951 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6952 if (sval != 0) 6953 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6954 } 6955 6956 if (un->un_f_power_condition_supported) { 6957 char *pm_condition_name[] = {"STOPPED", "STANDBY", 6958 "IDLE", "ACTIVE"}; 6959 SD_TRACE(SD_LOG_IO_PM, un, 6960 "sdpower: sending \'%s\' power condition", 6961 pm_condition_name[level]); 6962 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 6963 sd_pl2pc[level], SD_PATH_DIRECT); 6964 } else { 6965 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6966 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6967 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 6968 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : 6969 SD_TARGET_STOP), SD_PATH_DIRECT); 6970 } 6971 if (sval != 0) { 6972 if (sval == EIO) 6973 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6974 else 6975 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6976 } 6977 6978 /* Command failed, check for media present. */ 6979 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6980 medium_present = FALSE; 6981 } 6982 6983 /* 6984 * The conditions of interest here are: 6985 * if a spindle off with media present fails, 6986 * then restore the state and return an error. 6987 * else if a spindle on fails, 6988 * then return an error (there's no state to restore). 6989 * In all other cases we setup for the new state 6990 * and return success. 6991 */ 6992 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6993 if ((medium_present == TRUE) && (sval != 0)) { 6994 /* The stop command from above failed */ 6995 rval = DDI_FAILURE; 6996 /* 6997 * The stop command failed, and we have media 6998 * present. Put the level back by calling the 6999 * sd_pm_resume() and set the state back to 7000 * it's previous value. 7001 */ 7002 (void) sd_pm_state_change(un, last_power_level, 7003 SD_PM_STATE_ROLLBACK); 7004 mutex_enter(SD_MUTEX(un)); 7005 un->un_last_state = save_state; 7006 mutex_exit(SD_MUTEX(un)); 7007 } else if (un->un_f_monitor_media_state) { 7008 /* 7009 * The stop command from above succeeded. 7010 * Terminate watch thread in case of removable media 7011 * devices going into low power state. This is as per 7012 * the requirements of pm framework, otherwise commands 7013 * will be generated for the device (through watch 7014 * thread), even when the device is in low power state. 7015 */ 7016 mutex_enter(SD_MUTEX(un)); 7017 un->un_f_watcht_stopped = FALSE; 7018 if (un->un_swr_token != NULL) { 7019 opaque_t temp_token = un->un_swr_token; 7020 un->un_f_watcht_stopped = TRUE; 7021 un->un_swr_token = NULL; 7022 mutex_exit(SD_MUTEX(un)); 7023 (void) scsi_watch_request_terminate(temp_token, 7024 SCSI_WATCH_TERMINATE_ALL_WAIT); 7025 } else { 7026 mutex_exit(SD_MUTEX(un)); 7027 } 7028 } 7029 } else { 7030 /* 7031 * The level requested is I/O capable. 7032 * Legacy behavior: return success on a failed spinup 7033 * if there is no media in the drive. 7034 * Do this by looking at medium_present here. 7035 */ 7036 if ((sval != 0) && medium_present) { 7037 /* The start command from above failed */ 7038 rval = DDI_FAILURE; 7039 } else { 7040 /* 7041 * The start command from above succeeded 7042 * PM resume the devices now that we have 7043 * started the disks 7044 */ 7045 (void) sd_pm_state_change(un, level, 7046 SD_PM_STATE_CHANGE); 7047 7048 /* 7049 * Resume the watch thread since it was suspended 7050 * when the device went into low power mode. 7051 */ 7052 if (un->un_f_monitor_media_state) { 7053 mutex_enter(SD_MUTEX(un)); 7054 if (un->un_f_watcht_stopped == TRUE) { 7055 opaque_t temp_token; 7056 7057 un->un_f_watcht_stopped = FALSE; 7058 mutex_exit(SD_MUTEX(un)); 7059 temp_token = 7060 sd_watch_request_submit(un); 7061 mutex_enter(SD_MUTEX(un)); 7062 un->un_swr_token = temp_token; 7063 } 7064 mutex_exit(SD_MUTEX(un)); 7065 } 7066 } 7067 } 7068 7069 if (got_semaphore_here != 0) { 7070 sema_v(&un->un_semoclose); 7071 } 7072 /* 7073 * On exit put the state back to it's original value 7074 * and broadcast to anyone waiting for the power 7075 * change completion. 7076 */ 7077 mutex_enter(SD_MUTEX(un)); 7078 un->un_state = state_before_pm; 7079 cv_broadcast(&un->un_suspend_cv); 7080 mutex_exit(SD_MUTEX(un)); 7081 7082 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7083 7084 sd_ssc_fini(ssc); 7085 return (rval); 7086 7087 sdpower_failed: 7088 7089 sd_ssc_fini(ssc); 7090 return (DDI_FAILURE); 7091 } 7092 7093 7094 7095 /* 7096 * Function: sdattach 7097 * 7098 * Description: Driver's attach(9e) entry point function. 7099 * 7100 * Arguments: devi - opaque device info handle 7101 * cmd - attach type 7102 * 7103 * Return Code: DDI_SUCCESS 7104 * DDI_FAILURE 7105 * 7106 * Context: Kernel thread context 7107 */ 7108 7109 static int 7110 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7111 { 7112 switch (cmd) { 7113 case DDI_ATTACH: 7114 return (sd_unit_attach(devi)); 7115 case DDI_RESUME: 7116 return (sd_ddi_resume(devi)); 7117 default: 7118 break; 7119 } 7120 return (DDI_FAILURE); 7121 } 7122 7123 7124 /* 7125 * Function: sddetach 7126 * 7127 * Description: Driver's detach(9E) entry point function. 7128 * 7129 * Arguments: devi - opaque device info handle 7130 * cmd - detach type 7131 * 7132 * Return Code: DDI_SUCCESS 7133 * DDI_FAILURE 7134 * 7135 * Context: Kernel thread context 7136 */ 7137 7138 static int 7139 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7140 { 7141 switch (cmd) { 7142 case DDI_DETACH: 7143 return (sd_unit_detach(devi)); 7144 case DDI_SUSPEND: 7145 return (sd_ddi_suspend(devi)); 7146 default: 7147 break; 7148 } 7149 return (DDI_FAILURE); 7150 } 7151 7152 7153 /* 7154 * Function: sd_sync_with_callback 7155 * 7156 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7157 * state while the callback routine is active. 7158 * 7159 * Arguments: un: softstate structure for the instance 7160 * 7161 * Context: Kernel thread context 7162 */ 7163 7164 static void 7165 sd_sync_with_callback(struct sd_lun *un) 7166 { 7167 ASSERT(un != NULL); 7168 7169 mutex_enter(SD_MUTEX(un)); 7170 7171 ASSERT(un->un_in_callback >= 0); 7172 7173 while (un->un_in_callback > 0) { 7174 mutex_exit(SD_MUTEX(un)); 7175 delay(2); 7176 mutex_enter(SD_MUTEX(un)); 7177 } 7178 7179 mutex_exit(SD_MUTEX(un)); 7180 } 7181 7182 /* 7183 * Function: sd_unit_attach 7184 * 7185 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7186 * the soft state structure for the device and performs 7187 * all necessary structure and device initializations. 7188 * 7189 * Arguments: devi: the system's dev_info_t for the device. 7190 * 7191 * Return Code: DDI_SUCCESS if attach is successful. 7192 * DDI_FAILURE if any part of the attach fails. 7193 * 7194 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7195 * Kernel thread context only. Can sleep. 7196 */ 7197 7198 static int 7199 sd_unit_attach(dev_info_t *devi) 7200 { 7201 struct scsi_device *devp; 7202 struct sd_lun *un; 7203 char *variantp; 7204 char name_str[48]; 7205 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7206 int instance; 7207 int rval; 7208 int wc_enabled; 7209 int tgt; 7210 uint64_t capacity; 7211 uint_t lbasize = 0; 7212 dev_info_t *pdip = ddi_get_parent(devi); 7213 int offbyone = 0; 7214 int geom_label_valid = 0; 7215 sd_ssc_t *ssc; 7216 int status; 7217 struct sd_fm_internal *sfip = NULL; 7218 int max_xfer_size; 7219 7220 /* 7221 * Retrieve the target driver's private data area. This was set 7222 * up by the HBA. 7223 */ 7224 devp = ddi_get_driver_private(devi); 7225 7226 /* 7227 * Retrieve the target ID of the device. 7228 */ 7229 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7230 SCSI_ADDR_PROP_TARGET, -1); 7231 7232 /* 7233 * Since we have no idea what state things were left in by the last 7234 * user of the device, set up some 'default' settings, ie. turn 'em 7235 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7236 * Do this before the scsi_probe, which sends an inquiry. 7237 * This is a fix for bug (4430280). 7238 * Of special importance is wide-xfer. The drive could have been left 7239 * in wide transfer mode by the last driver to communicate with it, 7240 * this includes us. If that's the case, and if the following is not 7241 * setup properly or we don't re-negotiate with the drive prior to 7242 * transferring data to/from the drive, it causes bus parity errors, 7243 * data overruns, and unexpected interrupts. This first occurred when 7244 * the fix for bug (4378686) was made. 7245 */ 7246 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7247 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7248 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7249 7250 /* 7251 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 7252 * on a target. Setting it per lun instance actually sets the 7253 * capability of this target, which affects those luns already 7254 * attached on the same target. So during attach, we can only disable 7255 * this capability only when no other lun has been attached on this 7256 * target. By doing this, we assume a target has the same tagged-qing 7257 * capability for every lun. The condition can be removed when HBA 7258 * is changed to support per lun based tagged-qing capability. 7259 */ 7260 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7261 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7262 } 7263 7264 /* 7265 * Use scsi_probe() to issue an INQUIRY command to the device. 7266 * This call will allocate and fill in the scsi_inquiry structure 7267 * and point the sd_inq member of the scsi_device structure to it. 7268 * If the attach succeeds, then this memory will not be de-allocated 7269 * (via scsi_unprobe()) until the instance is detached. 7270 */ 7271 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7272 goto probe_failed; 7273 } 7274 7275 /* 7276 * Check the device type as specified in the inquiry data and 7277 * claim it if it is of a type that we support. 7278 */ 7279 switch (devp->sd_inq->inq_dtype) { 7280 case DTYPE_DIRECT: 7281 break; 7282 case DTYPE_RODIRECT: 7283 break; 7284 case DTYPE_OPTICAL: 7285 break; 7286 case DTYPE_NOTPRESENT: 7287 default: 7288 /* Unsupported device type; fail the attach. */ 7289 goto probe_failed; 7290 } 7291 7292 /* 7293 * Allocate the soft state structure for this unit. 7294 * 7295 * We rely upon this memory being set to all zeroes by 7296 * ddi_soft_state_zalloc(). We assume that any member of the 7297 * soft state structure that is not explicitly initialized by 7298 * this routine will have a value of zero. 7299 */ 7300 instance = ddi_get_instance(devp->sd_dev); 7301 #ifndef XPV_HVM_DRIVER 7302 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7303 goto probe_failed; 7304 } 7305 #endif /* !XPV_HVM_DRIVER */ 7306 7307 /* 7308 * Retrieve a pointer to the newly-allocated soft state. 7309 * 7310 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7311 * was successful, unless something has gone horribly wrong and the 7312 * ddi's soft state internals are corrupt (in which case it is 7313 * probably better to halt here than just fail the attach....) 7314 */ 7315 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7316 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7317 instance); 7318 /*NOTREACHED*/ 7319 } 7320 7321 /* 7322 * Link the back ptr of the driver soft state to the scsi_device 7323 * struct for this lun. 7324 * Save a pointer to the softstate in the driver-private area of 7325 * the scsi_device struct. 7326 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7327 * we first set un->un_sd below. 7328 */ 7329 un->un_sd = devp; 7330 devp->sd_private = (opaque_t)un; 7331 7332 /* 7333 * The following must be after devp is stored in the soft state struct. 7334 */ 7335 #ifdef SDDEBUG 7336 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7337 "%s_unit_attach: un:0x%p instance:%d\n", 7338 ddi_driver_name(devi), un, instance); 7339 #endif 7340 7341 /* 7342 * Set up the device type and node type (for the minor nodes). 7343 * By default we assume that the device can at least support the 7344 * Common Command Set. Call it a CD-ROM if it reports itself 7345 * as a RODIRECT device. 7346 */ 7347 switch (devp->sd_inq->inq_dtype) { 7348 case DTYPE_RODIRECT: 7349 un->un_node_type = DDI_NT_CD_CHAN; 7350 un->un_ctype = CTYPE_CDROM; 7351 break; 7352 case DTYPE_OPTICAL: 7353 un->un_node_type = DDI_NT_BLOCK_CHAN; 7354 un->un_ctype = CTYPE_ROD; 7355 break; 7356 default: 7357 un->un_node_type = DDI_NT_BLOCK_CHAN; 7358 un->un_ctype = CTYPE_CCS; 7359 break; 7360 } 7361 7362 /* 7363 * Try to read the interconnect type from the HBA. 7364 * 7365 * Note: This driver is currently compiled as two binaries, a parallel 7366 * scsi version (sd) and a fibre channel version (ssd). All functional 7367 * differences are determined at compile time. In the future a single 7368 * binary will be provided and the interconnect type will be used to 7369 * differentiate between fibre and parallel scsi behaviors. At that time 7370 * it will be necessary for all fibre channel HBAs to support this 7371 * property. 7372 * 7373 * set un_f_is_fiber to TRUE ( default fiber ) 7374 */ 7375 un->un_f_is_fibre = TRUE; 7376 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7377 case INTERCONNECT_SSA: 7378 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7379 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7380 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7381 break; 7382 case INTERCONNECT_PARALLEL: 7383 un->un_f_is_fibre = FALSE; 7384 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7385 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7386 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7387 break; 7388 case INTERCONNECT_SAS: 7389 un->un_f_is_fibre = FALSE; 7390 un->un_interconnect_type = SD_INTERCONNECT_SAS; 7391 un->un_node_type = DDI_NT_BLOCK_SAS; 7392 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7393 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un); 7394 break; 7395 case INTERCONNECT_SATA: 7396 un->un_f_is_fibre = FALSE; 7397 un->un_interconnect_type = SD_INTERCONNECT_SATA; 7398 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7399 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 7400 break; 7401 case INTERCONNECT_FIBRE: 7402 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7403 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7404 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7405 break; 7406 case INTERCONNECT_FABRIC: 7407 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7408 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7409 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7410 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7411 break; 7412 default: 7413 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7414 /* 7415 * The HBA does not support the "interconnect-type" property 7416 * (or did not provide a recognized type). 7417 * 7418 * Note: This will be obsoleted when a single fibre channel 7419 * and parallel scsi driver is delivered. In the meantime the 7420 * interconnect type will be set to the platform default.If that 7421 * type is not parallel SCSI, it means that we should be 7422 * assuming "ssd" semantics. However, here this also means that 7423 * the FC HBA is not supporting the "interconnect-type" property 7424 * like we expect it to, so log this occurrence. 7425 */ 7426 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7427 if (!SD_IS_PARALLEL_SCSI(un)) { 7428 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7429 "sd_unit_attach: un:0x%p Assuming " 7430 "INTERCONNECT_FIBRE\n", un); 7431 } else { 7432 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7433 "sd_unit_attach: un:0x%p Assuming " 7434 "INTERCONNECT_PARALLEL\n", un); 7435 un->un_f_is_fibre = FALSE; 7436 } 7437 #else 7438 /* 7439 * Note: This source will be implemented when a single fibre 7440 * channel and parallel scsi driver is delivered. The default 7441 * will be to assume that if a device does not support the 7442 * "interconnect-type" property it is a parallel SCSI HBA and 7443 * we will set the interconnect type for parallel scsi. 7444 */ 7445 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7446 un->un_f_is_fibre = FALSE; 7447 #endif 7448 break; 7449 } 7450 7451 if (un->un_f_is_fibre == TRUE) { 7452 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7453 SCSI_VERSION_3) { 7454 switch (un->un_interconnect_type) { 7455 case SD_INTERCONNECT_FIBRE: 7456 case SD_INTERCONNECT_SSA: 7457 un->un_node_type = DDI_NT_BLOCK_WWN; 7458 break; 7459 default: 7460 break; 7461 } 7462 } 7463 } 7464 7465 /* 7466 * Initialize the Request Sense command for the target 7467 */ 7468 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7469 goto alloc_rqs_failed; 7470 } 7471 7472 /* 7473 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7474 * with separate binary for sd and ssd. 7475 * 7476 * x86 has 1 binary, un_retry_count is set base on connection type. 7477 * The hardcoded values will go away when Sparc uses 1 binary 7478 * for sd and ssd. This hardcoded values need to match 7479 * SD_RETRY_COUNT in sddef.h 7480 * The value used is base on interconnect type. 7481 * fibre = 3, parallel = 5 7482 */ 7483 #if defined(__i386) || defined(__amd64) 7484 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7485 #else 7486 un->un_retry_count = SD_RETRY_COUNT; 7487 #endif 7488 7489 /* 7490 * Set the per disk retry count to the default number of retries 7491 * for disks and CDROMs. This value can be overridden by the 7492 * disk property list or an entry in sd.conf. 7493 */ 7494 un->un_notready_retry_count = 7495 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7496 : DISK_NOT_READY_RETRY_COUNT(un); 7497 7498 /* 7499 * Set the busy retry count to the default value of un_retry_count. 7500 * This can be overridden by entries in sd.conf or the device 7501 * config table. 7502 */ 7503 un->un_busy_retry_count = un->un_retry_count; 7504 7505 /* 7506 * Init the reset threshold for retries. This number determines 7507 * how many retries must be performed before a reset can be issued 7508 * (for certain error conditions). This can be overridden by entries 7509 * in sd.conf or the device config table. 7510 */ 7511 un->un_reset_retry_count = (un->un_retry_count / 2); 7512 7513 /* 7514 * Set the victim_retry_count to the default un_retry_count 7515 */ 7516 un->un_victim_retry_count = (2 * un->un_retry_count); 7517 7518 /* 7519 * Set the reservation release timeout to the default value of 7520 * 5 seconds. This can be overridden by entries in ssd.conf or the 7521 * device config table. 7522 */ 7523 un->un_reserve_release_time = 5; 7524 7525 /* 7526 * Set up the default maximum transfer size. Note that this may 7527 * get updated later in the attach, when setting up default wide 7528 * operations for disks. 7529 */ 7530 #if defined(__i386) || defined(__amd64) 7531 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7532 un->un_partial_dma_supported = 1; 7533 #else 7534 un->un_max_xfer_size = (uint_t)maxphys; 7535 #endif 7536 7537 /* 7538 * Get "allow bus device reset" property (defaults to "enabled" if 7539 * the property was not defined). This is to disable bus resets for 7540 * certain kinds of error recovery. Note: In the future when a run-time 7541 * fibre check is available the soft state flag should default to 7542 * enabled. 7543 */ 7544 if (un->un_f_is_fibre == TRUE) { 7545 un->un_f_allow_bus_device_reset = TRUE; 7546 } else { 7547 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7548 "allow-bus-device-reset", 1) != 0) { 7549 un->un_f_allow_bus_device_reset = TRUE; 7550 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7551 "sd_unit_attach: un:0x%p Bus device reset " 7552 "enabled\n", un); 7553 } else { 7554 un->un_f_allow_bus_device_reset = FALSE; 7555 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7556 "sd_unit_attach: un:0x%p Bus device reset " 7557 "disabled\n", un); 7558 } 7559 } 7560 7561 /* 7562 * Check if this is an ATAPI device. ATAPI devices use Group 1 7563 * Read/Write commands and Group 2 Mode Sense/Select commands. 7564 * 7565 * Note: The "obsolete" way of doing this is to check for the "atapi" 7566 * property. The new "variant" property with a value of "atapi" has been 7567 * introduced so that future 'variants' of standard SCSI behavior (like 7568 * atapi) could be specified by the underlying HBA drivers by supplying 7569 * a new value for the "variant" property, instead of having to define a 7570 * new property. 7571 */ 7572 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7573 un->un_f_cfg_is_atapi = TRUE; 7574 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7575 "sd_unit_attach: un:0x%p Atapi device\n", un); 7576 } 7577 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7578 &variantp) == DDI_PROP_SUCCESS) { 7579 if (strcmp(variantp, "atapi") == 0) { 7580 un->un_f_cfg_is_atapi = TRUE; 7581 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7582 "sd_unit_attach: un:0x%p Atapi device\n", un); 7583 } 7584 ddi_prop_free(variantp); 7585 } 7586 7587 un->un_cmd_timeout = SD_IO_TIME; 7588 7589 un->un_busy_timeout = SD_BSY_TIMEOUT; 7590 7591 /* Info on current states, statuses, etc. (Updated frequently) */ 7592 un->un_state = SD_STATE_NORMAL; 7593 un->un_last_state = SD_STATE_NORMAL; 7594 7595 /* Control & status info for command throttling */ 7596 un->un_throttle = sd_max_throttle; 7597 un->un_saved_throttle = sd_max_throttle; 7598 un->un_min_throttle = sd_min_throttle; 7599 7600 if (un->un_f_is_fibre == TRUE) { 7601 un->un_f_use_adaptive_throttle = TRUE; 7602 } else { 7603 un->un_f_use_adaptive_throttle = FALSE; 7604 } 7605 7606 /* Removable media support. */ 7607 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7608 un->un_mediastate = DKIO_NONE; 7609 un->un_specified_mediastate = DKIO_NONE; 7610 7611 /* CVs for suspend/resume (PM or DR) */ 7612 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7613 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7614 7615 /* Power management support. */ 7616 un->un_power_level = SD_SPINDLE_UNINIT; 7617 7618 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 7619 un->un_f_wcc_inprog = 0; 7620 7621 /* 7622 * The open/close semaphore is used to serialize threads executing 7623 * in the driver's open & close entry point routines for a given 7624 * instance. 7625 */ 7626 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7627 7628 /* 7629 * The conf file entry and softstate variable is a forceful override, 7630 * meaning a non-zero value must be entered to change the default. 7631 */ 7632 un->un_f_disksort_disabled = FALSE; 7633 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT; 7634 un->un_f_enable_rmw = FALSE; 7635 7636 /* 7637 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but 7638 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property. 7639 */ 7640 un->un_f_mmc_gesn_polling = TRUE; 7641 7642 /* 7643 * physical sector size defaults to DEV_BSIZE currently. We can 7644 * override this value via the driver configuration file so we must 7645 * set it before calling sd_read_unit_properties(). 7646 */ 7647 un->un_phy_blocksize = DEV_BSIZE; 7648 7649 /* 7650 * Retrieve the properties from the static driver table or the driver 7651 * configuration file (.conf) for this unit and update the soft state 7652 * for the device as needed for the indicated properties. 7653 * Note: the property configuration needs to occur here as some of the 7654 * following routines may have dependencies on soft state flags set 7655 * as part of the driver property configuration. 7656 */ 7657 sd_read_unit_properties(un); 7658 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7659 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7660 7661 /* 7662 * Only if a device has "hotpluggable" property, it is 7663 * treated as hotpluggable device. Otherwise, it is 7664 * regarded as non-hotpluggable one. 7665 */ 7666 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 7667 -1) != -1) { 7668 un->un_f_is_hotpluggable = TRUE; 7669 } 7670 7671 /* 7672 * set unit's attributes(flags) according to "hotpluggable" and 7673 * RMB bit in INQUIRY data. 7674 */ 7675 sd_set_unit_attributes(un, devi); 7676 7677 /* 7678 * By default, we mark the capacity, lbasize, and geometry 7679 * as invalid. Only if we successfully read a valid capacity 7680 * will we update the un_blockcount and un_tgt_blocksize with the 7681 * valid values (the geometry will be validated later). 7682 */ 7683 un->un_f_blockcount_is_valid = FALSE; 7684 un->un_f_tgt_blocksize_is_valid = FALSE; 7685 7686 /* 7687 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7688 * otherwise. 7689 */ 7690 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7691 un->un_blockcount = 0; 7692 7693 /* 7694 * Set up the per-instance info needed to determine the correct 7695 * CDBs and other info for issuing commands to the target. 7696 */ 7697 sd_init_cdb_limits(un); 7698 7699 /* 7700 * Set up the IO chains to use, based upon the target type. 7701 */ 7702 if (un->un_f_non_devbsize_supported) { 7703 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7704 } else { 7705 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7706 } 7707 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7708 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7709 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7710 7711 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7712 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7713 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7714 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7715 7716 7717 if (ISCD(un)) { 7718 un->un_additional_codes = sd_additional_codes; 7719 } else { 7720 un->un_additional_codes = NULL; 7721 } 7722 7723 /* 7724 * Create the kstats here so they can be available for attach-time 7725 * routines that send commands to the unit (either polled or via 7726 * sd_send_scsi_cmd). 7727 * 7728 * Note: This is a critical sequence that needs to be maintained: 7729 * 1) Instantiate the kstats here, before any routines using the 7730 * iopath (i.e. sd_send_scsi_cmd). 7731 * 2) Instantiate and initialize the partition stats 7732 * (sd_set_pstats). 7733 * 3) Initialize the error stats (sd_set_errstats), following 7734 * sd_validate_geometry(),sd_register_devid(), 7735 * and sd_cache_control(). 7736 */ 7737 7738 un->un_stats = kstat_create(sd_label, instance, 7739 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7740 if (un->un_stats != NULL) { 7741 un->un_stats->ks_lock = SD_MUTEX(un); 7742 kstat_install(un->un_stats); 7743 } 7744 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7745 "sd_unit_attach: un:0x%p un_stats created\n", un); 7746 7747 sd_create_errstats(un, instance); 7748 if (un->un_errstats == NULL) { 7749 goto create_errstats_failed; 7750 } 7751 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7752 "sd_unit_attach: un:0x%p errstats created\n", un); 7753 7754 /* 7755 * The following if/else code was relocated here from below as part 7756 * of the fix for bug (4430280). However with the default setup added 7757 * on entry to this routine, it's no longer absolutely necessary for 7758 * this to be before the call to sd_spin_up_unit. 7759 */ 7760 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 7761 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 7762 (devp->sd_inq->inq_ansi == 5)) && 7763 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 7764 7765 /* 7766 * If tagged queueing is supported by the target 7767 * and by the host adapter then we will enable it 7768 */ 7769 un->un_tagflags = 0; 7770 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 7771 (un->un_f_arq_enabled == TRUE)) { 7772 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7773 1, 1) == 1) { 7774 un->un_tagflags = FLAG_STAG; 7775 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7776 "sd_unit_attach: un:0x%p tag queueing " 7777 "enabled\n", un); 7778 } else if (scsi_ifgetcap(SD_ADDRESS(un), 7779 "untagged-qing", 0) == 1) { 7780 un->un_f_opt_queueing = TRUE; 7781 un->un_saved_throttle = un->un_throttle = 7782 min(un->un_throttle, 3); 7783 } else { 7784 un->un_f_opt_queueing = FALSE; 7785 un->un_saved_throttle = un->un_throttle = 1; 7786 } 7787 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 7788 == 1) && (un->un_f_arq_enabled == TRUE)) { 7789 /* The Host Adapter supports internal queueing. */ 7790 un->un_f_opt_queueing = TRUE; 7791 un->un_saved_throttle = un->un_throttle = 7792 min(un->un_throttle, 3); 7793 } else { 7794 un->un_f_opt_queueing = FALSE; 7795 un->un_saved_throttle = un->un_throttle = 1; 7796 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7797 "sd_unit_attach: un:0x%p no tag queueing\n", un); 7798 } 7799 7800 /* 7801 * Enable large transfers for SATA/SAS drives 7802 */ 7803 if (SD_IS_SERIAL(un)) { 7804 un->un_max_xfer_size = 7805 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7806 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7807 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7808 "sd_unit_attach: un:0x%p max transfer " 7809 "size=0x%x\n", un, un->un_max_xfer_size); 7810 7811 } 7812 7813 /* Setup or tear down default wide operations for disks */ 7814 7815 /* 7816 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 7817 * and "ssd_max_xfer_size" to exist simultaneously on the same 7818 * system and be set to different values. In the future this 7819 * code may need to be updated when the ssd module is 7820 * obsoleted and removed from the system. (4299588) 7821 */ 7822 if (SD_IS_PARALLEL_SCSI(un) && 7823 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 7824 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 7825 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7826 1, 1) == 1) { 7827 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7828 "sd_unit_attach: un:0x%p Wide Transfer " 7829 "enabled\n", un); 7830 } 7831 7832 /* 7833 * If tagged queuing has also been enabled, then 7834 * enable large xfers 7835 */ 7836 if (un->un_saved_throttle == sd_max_throttle) { 7837 un->un_max_xfer_size = 7838 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7839 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7840 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7841 "sd_unit_attach: un:0x%p max transfer " 7842 "size=0x%x\n", un, un->un_max_xfer_size); 7843 } 7844 } else { 7845 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7846 0, 1) == 1) { 7847 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7848 "sd_unit_attach: un:0x%p " 7849 "Wide Transfer disabled\n", un); 7850 } 7851 } 7852 } else { 7853 un->un_tagflags = FLAG_STAG; 7854 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 7855 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 7856 } 7857 7858 /* 7859 * If this target supports LUN reset, try to enable it. 7860 */ 7861 if (un->un_f_lun_reset_enabled) { 7862 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 7863 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7864 "un:0x%p lun_reset capability set\n", un); 7865 } else { 7866 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7867 "un:0x%p lun-reset capability not set\n", un); 7868 } 7869 } 7870 7871 /* 7872 * Adjust the maximum transfer size. This is to fix 7873 * the problem of partial DMA support on SPARC. Some 7874 * HBA driver, like aac, has very small dma_attr_maxxfer 7875 * size, which requires partial DMA support on SPARC. 7876 * In the future the SPARC pci nexus driver may solve 7877 * the problem instead of this fix. 7878 */ 7879 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7880 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7881 /* We need DMA partial even on sparc to ensure sddump() works */ 7882 un->un_max_xfer_size = max_xfer_size; 7883 if (un->un_partial_dma_supported == 0) 7884 un->un_partial_dma_supported = 1; 7885 } 7886 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7887 DDI_PROP_DONTPASS, "buf_break", 0) == 1) { 7888 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr, 7889 un->un_max_xfer_size) == 1) { 7890 un->un_buf_breakup_supported = 1; 7891 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7892 "un:0x%p Buf breakup enabled\n", un); 7893 } 7894 } 7895 7896 /* 7897 * Set PKT_DMA_PARTIAL flag. 7898 */ 7899 if (un->un_partial_dma_supported == 1) { 7900 un->un_pkt_flags = PKT_DMA_PARTIAL; 7901 } else { 7902 un->un_pkt_flags = 0; 7903 } 7904 7905 /* Initialize sd_ssc_t for internal uscsi commands */ 7906 ssc = sd_ssc_init(un); 7907 scsi_fm_init(devp); 7908 7909 /* 7910 * Allocate memory for SCSI FMA stuffs. 7911 */ 7912 un->un_fm_private = 7913 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP); 7914 sfip = (struct sd_fm_internal *)un->un_fm_private; 7915 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd; 7916 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo; 7917 sfip->fm_ssc.ssc_un = un; 7918 7919 if (ISCD(un) || 7920 un->un_f_has_removable_media || 7921 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) { 7922 /* 7923 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device. 7924 * Their log are unchanged. 7925 */ 7926 sfip->fm_log_level = SD_FM_LOG_NSUP; 7927 } else { 7928 /* 7929 * If enter here, it should be non-CDROM and FM-capable 7930 * device, and it will not keep the old scsi_log as before 7931 * in /var/adm/messages. However, the property 7932 * "fm-scsi-log" will control whether the FM telemetry will 7933 * be logged in /var/adm/messages. 7934 */ 7935 int fm_scsi_log; 7936 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7937 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0); 7938 7939 if (fm_scsi_log) 7940 sfip->fm_log_level = SD_FM_LOG_EREPORT; 7941 else 7942 sfip->fm_log_level = SD_FM_LOG_SILENT; 7943 } 7944 7945 /* 7946 * At this point in the attach, we have enough info in the 7947 * soft state to be able to issue commands to the target. 7948 * 7949 * All command paths used below MUST issue their commands as 7950 * SD_PATH_DIRECT. This is important as intermediate layers 7951 * are not all initialized yet (such as PM). 7952 */ 7953 7954 /* 7955 * Send a TEST UNIT READY command to the device. This should clear 7956 * any outstanding UNIT ATTENTION that may be present. 7957 * 7958 * Note: Don't check for success, just track if there is a reservation, 7959 * this is a throw away command to clear any unit attentions. 7960 * 7961 * Note: This MUST be the first command issued to the target during 7962 * attach to ensure power on UNIT ATTENTIONS are cleared. 7963 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7964 * with attempts at spinning up a device with no media. 7965 */ 7966 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 7967 if (status != 0) { 7968 if (status == EACCES) 7969 reservation_flag = SD_TARGET_IS_RESERVED; 7970 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7971 } 7972 7973 /* 7974 * If the device is NOT a removable media device, attempt to spin 7975 * it up (using the START_STOP_UNIT command) and read its capacity 7976 * (using the READ CAPACITY command). Note, however, that either 7977 * of these could fail and in some cases we would continue with 7978 * the attach despite the failure (see below). 7979 */ 7980 if (un->un_f_descr_format_supported) { 7981 7982 switch (sd_spin_up_unit(ssc)) { 7983 case 0: 7984 /* 7985 * Spin-up was successful; now try to read the 7986 * capacity. If successful then save the results 7987 * and mark the capacity & lbasize as valid. 7988 */ 7989 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7990 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7991 7992 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 7993 &lbasize, SD_PATH_DIRECT); 7994 7995 switch (status) { 7996 case 0: { 7997 if (capacity > DK_MAX_BLOCKS) { 7998 #ifdef _LP64 7999 if ((capacity + 1) > 8000 SD_GROUP1_MAX_ADDRESS) { 8001 /* 8002 * Enable descriptor format 8003 * sense data so that we can 8004 * get 64 bit sense data 8005 * fields. 8006 */ 8007 sd_enable_descr_sense(ssc); 8008 } 8009 #else 8010 /* 32-bit kernels can't handle this */ 8011 scsi_log(SD_DEVINFO(un), 8012 sd_label, CE_WARN, 8013 "disk has %llu blocks, which " 8014 "is too large for a 32-bit " 8015 "kernel", capacity); 8016 8017 #if defined(__i386) || defined(__amd64) 8018 /* 8019 * 1TB disk was treated as (1T - 512)B 8020 * in the past, so that it might have 8021 * valid VTOC and solaris partitions, 8022 * we have to allow it to continue to 8023 * work. 8024 */ 8025 if (capacity -1 > DK_MAX_BLOCKS) 8026 #endif 8027 goto spinup_failed; 8028 #endif 8029 } 8030 8031 /* 8032 * Here it's not necessary to check the case: 8033 * the capacity of the device is bigger than 8034 * what the max hba cdb can support. Because 8035 * sd_send_scsi_READ_CAPACITY will retrieve 8036 * the capacity by sending USCSI command, which 8037 * is constrained by the max hba cdb. Actually, 8038 * sd_send_scsi_READ_CAPACITY will return 8039 * EINVAL when using bigger cdb than required 8040 * cdb length. Will handle this case in 8041 * "case EINVAL". 8042 */ 8043 8044 /* 8045 * The following relies on 8046 * sd_send_scsi_READ_CAPACITY never 8047 * returning 0 for capacity and/or lbasize. 8048 */ 8049 sd_update_block_info(un, lbasize, capacity); 8050 8051 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8052 "sd_unit_attach: un:0x%p capacity = %ld " 8053 "blocks; lbasize= %ld.\n", un, 8054 un->un_blockcount, un->un_tgt_blocksize); 8055 8056 break; 8057 } 8058 case EINVAL: 8059 /* 8060 * In the case where the max-cdb-length property 8061 * is smaller than the required CDB length for 8062 * a SCSI device, a target driver can fail to 8063 * attach to that device. 8064 */ 8065 scsi_log(SD_DEVINFO(un), 8066 sd_label, CE_WARN, 8067 "disk capacity is too large " 8068 "for current cdb length"); 8069 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8070 8071 goto spinup_failed; 8072 case EACCES: 8073 /* 8074 * Should never get here if the spin-up 8075 * succeeded, but code it in anyway. 8076 * From here, just continue with the attach... 8077 */ 8078 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8079 "sd_unit_attach: un:0x%p " 8080 "sd_send_scsi_READ_CAPACITY " 8081 "returned reservation conflict\n", un); 8082 reservation_flag = SD_TARGET_IS_RESERVED; 8083 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8084 break; 8085 default: 8086 /* 8087 * Likewise, should never get here if the 8088 * spin-up succeeded. Just continue with 8089 * the attach... 8090 */ 8091 if (status == EIO) 8092 sd_ssc_assessment(ssc, 8093 SD_FMT_STATUS_CHECK); 8094 else 8095 sd_ssc_assessment(ssc, 8096 SD_FMT_IGNORE); 8097 break; 8098 } 8099 break; 8100 case EACCES: 8101 /* 8102 * Device is reserved by another host. In this case 8103 * we could not spin it up or read the capacity, but 8104 * we continue with the attach anyway. 8105 */ 8106 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8107 "sd_unit_attach: un:0x%p spin-up reservation " 8108 "conflict.\n", un); 8109 reservation_flag = SD_TARGET_IS_RESERVED; 8110 break; 8111 default: 8112 /* Fail the attach if the spin-up failed. */ 8113 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8114 "sd_unit_attach: un:0x%p spin-up failed.", un); 8115 goto spinup_failed; 8116 } 8117 8118 } 8119 8120 /* 8121 * Check to see if this is a MMC drive 8122 */ 8123 if (ISCD(un)) { 8124 sd_set_mmc_caps(ssc); 8125 } 8126 8127 /* 8128 * Add a zero-length attribute to tell the world we support 8129 * kernel ioctls (for layered drivers) 8130 */ 8131 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8132 DDI_KERNEL_IOCTL, NULL, 0); 8133 8134 /* 8135 * Add a boolean property to tell the world we support 8136 * the B_FAILFAST flag (for layered drivers) 8137 */ 8138 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8139 "ddi-failfast-supported", NULL, 0); 8140 8141 /* 8142 * Initialize power management 8143 */ 8144 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8145 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8146 sd_setup_pm(ssc, devi); 8147 if (un->un_f_pm_is_enabled == FALSE) { 8148 /* 8149 * For performance, point to a jump table that does 8150 * not include pm. 8151 * The direct and priority chains don't change with PM. 8152 * 8153 * Note: this is currently done based on individual device 8154 * capabilities. When an interface for determining system 8155 * power enabled state becomes available, or when additional 8156 * layers are added to the command chain, these values will 8157 * have to be re-evaluated for correctness. 8158 */ 8159 if (un->un_f_non_devbsize_supported) { 8160 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8161 } else { 8162 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8163 } 8164 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8165 } 8166 8167 /* 8168 * This property is set to 0 by HA software to avoid retries 8169 * on a reserved disk. (The preferred property name is 8170 * "retry-on-reservation-conflict") (1189689) 8171 * 8172 * Note: The use of a global here can have unintended consequences. A 8173 * per instance variable is preferable to match the capabilities of 8174 * different underlying hba's (4402600) 8175 */ 8176 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8177 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8178 sd_retry_on_reservation_conflict); 8179 if (sd_retry_on_reservation_conflict != 0) { 8180 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8181 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8182 sd_retry_on_reservation_conflict); 8183 } 8184 8185 /* Set up options for QFULL handling. */ 8186 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8187 "qfull-retries", -1)) != -1) { 8188 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8189 rval, 1); 8190 } 8191 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8192 "qfull-retry-interval", -1)) != -1) { 8193 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8194 rval, 1); 8195 } 8196 8197 /* 8198 * This just prints a message that announces the existence of the 8199 * device. The message is always printed in the system logfile, but 8200 * only appears on the console if the system is booted with the 8201 * -v (verbose) argument. 8202 */ 8203 ddi_report_dev(devi); 8204 8205 un->un_mediastate = DKIO_NONE; 8206 8207 /* 8208 * Check if this is a SSD(Solid State Drive). 8209 */ 8210 sd_check_solid_state(ssc); 8211 8212 /* 8213 * Check whether the drive is in emulation mode. 8214 */ 8215 sd_check_emulation_mode(ssc); 8216 8217 cmlb_alloc_handle(&un->un_cmlbhandle); 8218 8219 #if defined(__i386) || defined(__amd64) 8220 /* 8221 * On x86, compensate for off-by-1 legacy error 8222 */ 8223 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 8224 (lbasize == un->un_sys_blocksize)) 8225 offbyone = CMLB_OFF_BY_ONE; 8226 #endif 8227 8228 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 8229 VOID2BOOLEAN(un->un_f_has_removable_media != 0), 8230 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0), 8231 un->un_node_type, offbyone, un->un_cmlbhandle, 8232 (void *)SD_PATH_DIRECT) != 0) { 8233 goto cmlb_attach_failed; 8234 } 8235 8236 8237 /* 8238 * Read and validate the device's geometry (ie, disk label) 8239 * A new unformatted drive will not have a valid geometry, but 8240 * the driver needs to successfully attach to this device so 8241 * the drive can be formatted via ioctls. 8242 */ 8243 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 8244 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 8245 8246 mutex_enter(SD_MUTEX(un)); 8247 8248 /* 8249 * Read and initialize the devid for the unit. 8250 */ 8251 if (un->un_f_devid_supported) { 8252 sd_register_devid(ssc, devi, reservation_flag); 8253 } 8254 mutex_exit(SD_MUTEX(un)); 8255 8256 #if (defined(__fibre)) 8257 /* 8258 * Register callbacks for fibre only. You can't do this solely 8259 * on the basis of the devid_type because this is hba specific. 8260 * We need to query our hba capabilities to find out whether to 8261 * register or not. 8262 */ 8263 if (un->un_f_is_fibre) { 8264 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8265 sd_init_event_callbacks(un); 8266 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8267 "sd_unit_attach: un:0x%p event callbacks inserted", 8268 un); 8269 } 8270 } 8271 #endif 8272 8273 if (un->un_f_opt_disable_cache == TRUE) { 8274 /* 8275 * Disable both read cache and write cache. This is 8276 * the historic behavior of the keywords in the config file. 8277 */ 8278 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8279 0) { 8280 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8281 "sd_unit_attach: un:0x%p Could not disable " 8282 "caching", un); 8283 goto devid_failed; 8284 } 8285 } 8286 8287 /* 8288 * Check the value of the WCE bit now and 8289 * set un_f_write_cache_enabled accordingly. 8290 */ 8291 (void) sd_get_write_cache_enabled(ssc, &wc_enabled); 8292 mutex_enter(SD_MUTEX(un)); 8293 un->un_f_write_cache_enabled = (wc_enabled != 0); 8294 mutex_exit(SD_MUTEX(un)); 8295 8296 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR && 8297 un->un_tgt_blocksize != DEV_BSIZE) || 8298 un->un_f_enable_rmw) { 8299 if (!(un->un_wm_cache)) { 8300 (void) snprintf(name_str, sizeof (name_str), 8301 "%s%d_cache", 8302 ddi_driver_name(SD_DEVINFO(un)), 8303 ddi_get_instance(SD_DEVINFO(un))); 8304 un->un_wm_cache = kmem_cache_create( 8305 name_str, sizeof (struct sd_w_map), 8306 8, sd_wm_cache_constructor, 8307 sd_wm_cache_destructor, NULL, 8308 (void *)un, NULL, 0); 8309 if (!(un->un_wm_cache)) { 8310 goto wm_cache_failed; 8311 } 8312 } 8313 } 8314 8315 /* 8316 * Check the value of the NV_SUP bit and set 8317 * un_f_suppress_cache_flush accordingly. 8318 */ 8319 sd_get_nv_sup(ssc); 8320 8321 /* 8322 * Find out what type of reservation this disk supports. 8323 */ 8324 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL); 8325 8326 switch (status) { 8327 case 0: 8328 /* 8329 * SCSI-3 reservations are supported. 8330 */ 8331 un->un_reservation_type = SD_SCSI3_RESERVATION; 8332 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8333 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8334 break; 8335 case ENOTSUP: 8336 /* 8337 * The PERSISTENT RESERVE IN command would not be recognized by 8338 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8339 */ 8340 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8341 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8342 un->un_reservation_type = SD_SCSI2_RESERVATION; 8343 8344 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8345 break; 8346 default: 8347 /* 8348 * default to SCSI-3 reservations 8349 */ 8350 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8351 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8352 un->un_reservation_type = SD_SCSI3_RESERVATION; 8353 8354 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8355 break; 8356 } 8357 8358 /* 8359 * Set the pstat and error stat values here, so data obtained during the 8360 * previous attach-time routines is available. 8361 * 8362 * Note: This is a critical sequence that needs to be maintained: 8363 * 1) Instantiate the kstats before any routines using the iopath 8364 * (i.e. sd_send_scsi_cmd). 8365 * 2) Initialize the error stats (sd_set_errstats) and partition 8366 * stats (sd_set_pstats)here, following 8367 * cmlb_validate_geometry(), sd_register_devid(), and 8368 * sd_cache_control(). 8369 */ 8370 8371 if (un->un_f_pkstats_enabled && geom_label_valid) { 8372 sd_set_pstats(un); 8373 SD_TRACE(SD_LOG_IO_PARTITION, un, 8374 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8375 } 8376 8377 sd_set_errstats(un); 8378 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8379 "sd_unit_attach: un:0x%p errstats set\n", un); 8380 8381 8382 /* 8383 * After successfully attaching an instance, we record the information 8384 * of how many luns have been attached on the relative target and 8385 * controller for parallel SCSI. This information is used when sd tries 8386 * to set the tagged queuing capability in HBA. 8387 */ 8388 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8389 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 8390 } 8391 8392 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8393 "sd_unit_attach: un:0x%p exit success\n", un); 8394 8395 /* Uninitialize sd_ssc_t pointer */ 8396 sd_ssc_fini(ssc); 8397 8398 return (DDI_SUCCESS); 8399 8400 /* 8401 * An error occurred during the attach; clean up & return failure. 8402 */ 8403 wm_cache_failed: 8404 devid_failed: 8405 8406 setup_pm_failed: 8407 ddi_remove_minor_node(devi, NULL); 8408 8409 cmlb_attach_failed: 8410 /* 8411 * Cleanup from the scsi_ifsetcap() calls (437868) 8412 */ 8413 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8414 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8415 8416 /* 8417 * Refer to the comments of setting tagged-qing in the beginning of 8418 * sd_unit_attach. We can only disable tagged queuing when there is 8419 * no lun attached on the target. 8420 */ 8421 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8422 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8423 } 8424 8425 if (un->un_f_is_fibre == FALSE) { 8426 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8427 } 8428 8429 spinup_failed: 8430 8431 /* Uninitialize sd_ssc_t pointer */ 8432 sd_ssc_fini(ssc); 8433 8434 mutex_enter(SD_MUTEX(un)); 8435 8436 /* Deallocate SCSI FMA memory spaces */ 8437 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8438 8439 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8440 if (un->un_direct_priority_timeid != NULL) { 8441 timeout_id_t temp_id = un->un_direct_priority_timeid; 8442 un->un_direct_priority_timeid = NULL; 8443 mutex_exit(SD_MUTEX(un)); 8444 (void) untimeout(temp_id); 8445 mutex_enter(SD_MUTEX(un)); 8446 } 8447 8448 /* Cancel any pending start/stop timeouts */ 8449 if (un->un_startstop_timeid != NULL) { 8450 timeout_id_t temp_id = un->un_startstop_timeid; 8451 un->un_startstop_timeid = NULL; 8452 mutex_exit(SD_MUTEX(un)); 8453 (void) untimeout(temp_id); 8454 mutex_enter(SD_MUTEX(un)); 8455 } 8456 8457 /* Cancel any pending reset-throttle timeouts */ 8458 if (un->un_reset_throttle_timeid != NULL) { 8459 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8460 un->un_reset_throttle_timeid = NULL; 8461 mutex_exit(SD_MUTEX(un)); 8462 (void) untimeout(temp_id); 8463 mutex_enter(SD_MUTEX(un)); 8464 } 8465 8466 /* Cancel rmw warning message timeouts */ 8467 if (un->un_rmw_msg_timeid != NULL) { 8468 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8469 un->un_rmw_msg_timeid = NULL; 8470 mutex_exit(SD_MUTEX(un)); 8471 (void) untimeout(temp_id); 8472 mutex_enter(SD_MUTEX(un)); 8473 } 8474 8475 /* Cancel any pending retry timeouts */ 8476 if (un->un_retry_timeid != NULL) { 8477 timeout_id_t temp_id = un->un_retry_timeid; 8478 un->un_retry_timeid = NULL; 8479 mutex_exit(SD_MUTEX(un)); 8480 (void) untimeout(temp_id); 8481 mutex_enter(SD_MUTEX(un)); 8482 } 8483 8484 /* Cancel any pending delayed cv broadcast timeouts */ 8485 if (un->un_dcvb_timeid != NULL) { 8486 timeout_id_t temp_id = un->un_dcvb_timeid; 8487 un->un_dcvb_timeid = NULL; 8488 mutex_exit(SD_MUTEX(un)); 8489 (void) untimeout(temp_id); 8490 mutex_enter(SD_MUTEX(un)); 8491 } 8492 8493 mutex_exit(SD_MUTEX(un)); 8494 8495 /* There should not be any in-progress I/O so ASSERT this check */ 8496 ASSERT(un->un_ncmds_in_transport == 0); 8497 ASSERT(un->un_ncmds_in_driver == 0); 8498 8499 /* Do not free the softstate if the callback routine is active */ 8500 sd_sync_with_callback(un); 8501 8502 /* 8503 * Partition stats apparently are not used with removables. These would 8504 * not have been created during attach, so no need to clean them up... 8505 */ 8506 if (un->un_errstats != NULL) { 8507 kstat_delete(un->un_errstats); 8508 un->un_errstats = NULL; 8509 } 8510 8511 create_errstats_failed: 8512 8513 if (un->un_stats != NULL) { 8514 kstat_delete(un->un_stats); 8515 un->un_stats = NULL; 8516 } 8517 8518 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8519 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8520 8521 ddi_prop_remove_all(devi); 8522 sema_destroy(&un->un_semoclose); 8523 cv_destroy(&un->un_state_cv); 8524 8525 getrbuf_failed: 8526 8527 sd_free_rqs(un); 8528 8529 alloc_rqs_failed: 8530 8531 devp->sd_private = NULL; 8532 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8533 8534 get_softstate_failed: 8535 /* 8536 * Note: the man pages are unclear as to whether or not doing a 8537 * ddi_soft_state_free(sd_state, instance) is the right way to 8538 * clean up after the ddi_soft_state_zalloc() if the subsequent 8539 * ddi_get_soft_state() fails. The implication seems to be 8540 * that the get_soft_state cannot fail if the zalloc succeeds. 8541 */ 8542 #ifndef XPV_HVM_DRIVER 8543 ddi_soft_state_free(sd_state, instance); 8544 #endif /* !XPV_HVM_DRIVER */ 8545 8546 probe_failed: 8547 scsi_unprobe(devp); 8548 8549 return (DDI_FAILURE); 8550 } 8551 8552 8553 /* 8554 * Function: sd_unit_detach 8555 * 8556 * Description: Performs DDI_DETACH processing for sddetach(). 8557 * 8558 * Return Code: DDI_SUCCESS 8559 * DDI_FAILURE 8560 * 8561 * Context: Kernel thread context 8562 */ 8563 8564 static int 8565 sd_unit_detach(dev_info_t *devi) 8566 { 8567 struct scsi_device *devp; 8568 struct sd_lun *un; 8569 int i; 8570 int tgt; 8571 dev_t dev; 8572 dev_info_t *pdip = ddi_get_parent(devi); 8573 #ifndef XPV_HVM_DRIVER 8574 int instance = ddi_get_instance(devi); 8575 #endif /* !XPV_HVM_DRIVER */ 8576 8577 mutex_enter(&sd_detach_mutex); 8578 8579 /* 8580 * Fail the detach for any of the following: 8581 * - Unable to get the sd_lun struct for the instance 8582 * - A layered driver has an outstanding open on the instance 8583 * - Another thread is already detaching this instance 8584 * - Another thread is currently performing an open 8585 */ 8586 devp = ddi_get_driver_private(devi); 8587 if ((devp == NULL) || 8588 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8589 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8590 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8591 mutex_exit(&sd_detach_mutex); 8592 return (DDI_FAILURE); 8593 } 8594 8595 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8596 8597 /* 8598 * Mark this instance as currently in a detach, to inhibit any 8599 * opens from a layered driver. 8600 */ 8601 un->un_detach_count++; 8602 mutex_exit(&sd_detach_mutex); 8603 8604 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8605 SCSI_ADDR_PROP_TARGET, -1); 8606 8607 dev = sd_make_device(SD_DEVINFO(un)); 8608 8609 #ifndef lint 8610 _NOTE(COMPETING_THREADS_NOW); 8611 #endif 8612 8613 mutex_enter(SD_MUTEX(un)); 8614 8615 /* 8616 * Fail the detach if there are any outstanding layered 8617 * opens on this device. 8618 */ 8619 for (i = 0; i < NDKMAP; i++) { 8620 if (un->un_ocmap.lyropen[i] != 0) { 8621 goto err_notclosed; 8622 } 8623 } 8624 8625 /* 8626 * Verify there are NO outstanding commands issued to this device. 8627 * ie, un_ncmds_in_transport == 0. 8628 * It's possible to have outstanding commands through the physio 8629 * code path, even though everything's closed. 8630 */ 8631 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8632 (un->un_direct_priority_timeid != NULL) || 8633 (un->un_state == SD_STATE_RWAIT)) { 8634 mutex_exit(SD_MUTEX(un)); 8635 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8636 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8637 goto err_stillbusy; 8638 } 8639 8640 /* 8641 * If we have the device reserved, release the reservation. 8642 */ 8643 if ((un->un_resvd_status & SD_RESERVE) && 8644 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8645 mutex_exit(SD_MUTEX(un)); 8646 /* 8647 * Note: sd_reserve_release sends a command to the device 8648 * via the sd_ioctlcmd() path, and can sleep. 8649 */ 8650 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8651 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8652 "sd_dr_detach: Cannot release reservation \n"); 8653 } 8654 } else { 8655 mutex_exit(SD_MUTEX(un)); 8656 } 8657 8658 /* 8659 * Untimeout any reserve recover, throttle reset, restart unit 8660 * and delayed broadcast timeout threads. Protect the timeout pointer 8661 * from getting nulled by their callback functions. 8662 */ 8663 mutex_enter(SD_MUTEX(un)); 8664 if (un->un_resvd_timeid != NULL) { 8665 timeout_id_t temp_id = un->un_resvd_timeid; 8666 un->un_resvd_timeid = NULL; 8667 mutex_exit(SD_MUTEX(un)); 8668 (void) untimeout(temp_id); 8669 mutex_enter(SD_MUTEX(un)); 8670 } 8671 8672 if (un->un_reset_throttle_timeid != NULL) { 8673 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8674 un->un_reset_throttle_timeid = NULL; 8675 mutex_exit(SD_MUTEX(un)); 8676 (void) untimeout(temp_id); 8677 mutex_enter(SD_MUTEX(un)); 8678 } 8679 8680 if (un->un_startstop_timeid != NULL) { 8681 timeout_id_t temp_id = un->un_startstop_timeid; 8682 un->un_startstop_timeid = NULL; 8683 mutex_exit(SD_MUTEX(un)); 8684 (void) untimeout(temp_id); 8685 mutex_enter(SD_MUTEX(un)); 8686 } 8687 8688 if (un->un_rmw_msg_timeid != NULL) { 8689 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8690 un->un_rmw_msg_timeid = NULL; 8691 mutex_exit(SD_MUTEX(un)); 8692 (void) untimeout(temp_id); 8693 mutex_enter(SD_MUTEX(un)); 8694 } 8695 8696 if (un->un_dcvb_timeid != NULL) { 8697 timeout_id_t temp_id = un->un_dcvb_timeid; 8698 un->un_dcvb_timeid = NULL; 8699 mutex_exit(SD_MUTEX(un)); 8700 (void) untimeout(temp_id); 8701 } else { 8702 mutex_exit(SD_MUTEX(un)); 8703 } 8704 8705 /* Remove any pending reservation reclaim requests for this device */ 8706 sd_rmv_resv_reclaim_req(dev); 8707 8708 mutex_enter(SD_MUTEX(un)); 8709 8710 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8711 if (un->un_direct_priority_timeid != NULL) { 8712 timeout_id_t temp_id = un->un_direct_priority_timeid; 8713 un->un_direct_priority_timeid = NULL; 8714 mutex_exit(SD_MUTEX(un)); 8715 (void) untimeout(temp_id); 8716 mutex_enter(SD_MUTEX(un)); 8717 } 8718 8719 /* Cancel any active multi-host disk watch thread requests */ 8720 if (un->un_mhd_token != NULL) { 8721 mutex_exit(SD_MUTEX(un)); 8722 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8723 if (scsi_watch_request_terminate(un->un_mhd_token, 8724 SCSI_WATCH_TERMINATE_NOWAIT)) { 8725 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8726 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8727 /* 8728 * Note: We are returning here after having removed 8729 * some driver timeouts above. This is consistent with 8730 * the legacy implementation but perhaps the watch 8731 * terminate call should be made with the wait flag set. 8732 */ 8733 goto err_stillbusy; 8734 } 8735 mutex_enter(SD_MUTEX(un)); 8736 un->un_mhd_token = NULL; 8737 } 8738 8739 if (un->un_swr_token != NULL) { 8740 mutex_exit(SD_MUTEX(un)); 8741 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8742 if (scsi_watch_request_terminate(un->un_swr_token, 8743 SCSI_WATCH_TERMINATE_NOWAIT)) { 8744 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8745 "sd_dr_detach: Cannot cancel swr watch request\n"); 8746 /* 8747 * Note: We are returning here after having removed 8748 * some driver timeouts above. This is consistent with 8749 * the legacy implementation but perhaps the watch 8750 * terminate call should be made with the wait flag set. 8751 */ 8752 goto err_stillbusy; 8753 } 8754 mutex_enter(SD_MUTEX(un)); 8755 un->un_swr_token = NULL; 8756 } 8757 8758 mutex_exit(SD_MUTEX(un)); 8759 8760 /* 8761 * Clear any scsi_reset_notifies. We clear the reset notifies 8762 * if we have not registered one. 8763 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8764 */ 8765 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8766 sd_mhd_reset_notify_cb, (caddr_t)un); 8767 8768 /* 8769 * protect the timeout pointers from getting nulled by 8770 * their callback functions during the cancellation process. 8771 * In such a scenario untimeout can be invoked with a null value. 8772 */ 8773 _NOTE(NO_COMPETING_THREADS_NOW); 8774 8775 mutex_enter(&un->un_pm_mutex); 8776 if (un->un_pm_idle_timeid != NULL) { 8777 timeout_id_t temp_id = un->un_pm_idle_timeid; 8778 un->un_pm_idle_timeid = NULL; 8779 mutex_exit(&un->un_pm_mutex); 8780 8781 /* 8782 * Timeout is active; cancel it. 8783 * Note that it'll never be active on a device 8784 * that does not support PM therefore we don't 8785 * have to check before calling pm_idle_component. 8786 */ 8787 (void) untimeout(temp_id); 8788 (void) pm_idle_component(SD_DEVINFO(un), 0); 8789 mutex_enter(&un->un_pm_mutex); 8790 } 8791 8792 /* 8793 * Check whether there is already a timeout scheduled for power 8794 * management. If yes then don't lower the power here, that's. 8795 * the timeout handler's job. 8796 */ 8797 if (un->un_pm_timeid != NULL) { 8798 timeout_id_t temp_id = un->un_pm_timeid; 8799 un->un_pm_timeid = NULL; 8800 mutex_exit(&un->un_pm_mutex); 8801 /* 8802 * Timeout is active; cancel it. 8803 * Note that it'll never be active on a device 8804 * that does not support PM therefore we don't 8805 * have to check before calling pm_idle_component. 8806 */ 8807 (void) untimeout(temp_id); 8808 (void) pm_idle_component(SD_DEVINFO(un), 0); 8809 8810 } else { 8811 mutex_exit(&un->un_pm_mutex); 8812 if ((un->un_f_pm_is_enabled == TRUE) && 8813 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un)) 8814 != DDI_SUCCESS)) { 8815 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8816 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8817 /* 8818 * Fix for bug: 4297749, item # 13 8819 * The above test now includes a check to see if PM is 8820 * supported by this device before call 8821 * pm_lower_power(). 8822 * Note, the following is not dead code. The call to 8823 * pm_lower_power above will generate a call back into 8824 * our sdpower routine which might result in a timeout 8825 * handler getting activated. Therefore the following 8826 * code is valid and necessary. 8827 */ 8828 mutex_enter(&un->un_pm_mutex); 8829 if (un->un_pm_timeid != NULL) { 8830 timeout_id_t temp_id = un->un_pm_timeid; 8831 un->un_pm_timeid = NULL; 8832 mutex_exit(&un->un_pm_mutex); 8833 (void) untimeout(temp_id); 8834 (void) pm_idle_component(SD_DEVINFO(un), 0); 8835 } else { 8836 mutex_exit(&un->un_pm_mutex); 8837 } 8838 } 8839 } 8840 8841 /* 8842 * Cleanup from the scsi_ifsetcap() calls (437868) 8843 * Relocated here from above to be after the call to 8844 * pm_lower_power, which was getting errors. 8845 */ 8846 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8847 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8848 8849 /* 8850 * Currently, tagged queuing is supported per target based by HBA. 8851 * Setting this per lun instance actually sets the capability of this 8852 * target in HBA, which affects those luns already attached on the 8853 * same target. So during detach, we can only disable this capability 8854 * only when this is the only lun left on this target. By doing 8855 * this, we assume a target has the same tagged queuing capability 8856 * for every lun. The condition can be removed when HBA is changed to 8857 * support per lun based tagged queuing capability. 8858 */ 8859 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 8860 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8861 } 8862 8863 if (un->un_f_is_fibre == FALSE) { 8864 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8865 } 8866 8867 /* 8868 * Remove any event callbacks, fibre only 8869 */ 8870 if (un->un_f_is_fibre == TRUE) { 8871 if ((un->un_insert_event != NULL) && 8872 (ddi_remove_event_handler(un->un_insert_cb_id) != 8873 DDI_SUCCESS)) { 8874 /* 8875 * Note: We are returning here after having done 8876 * substantial cleanup above. This is consistent 8877 * with the legacy implementation but this may not 8878 * be the right thing to do. 8879 */ 8880 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8881 "sd_dr_detach: Cannot cancel insert event\n"); 8882 goto err_remove_event; 8883 } 8884 un->un_insert_event = NULL; 8885 8886 if ((un->un_remove_event != NULL) && 8887 (ddi_remove_event_handler(un->un_remove_cb_id) != 8888 DDI_SUCCESS)) { 8889 /* 8890 * Note: We are returning here after having done 8891 * substantial cleanup above. This is consistent 8892 * with the legacy implementation but this may not 8893 * be the right thing to do. 8894 */ 8895 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8896 "sd_dr_detach: Cannot cancel remove event\n"); 8897 goto err_remove_event; 8898 } 8899 un->un_remove_event = NULL; 8900 } 8901 8902 /* Do not free the softstate if the callback routine is active */ 8903 sd_sync_with_callback(un); 8904 8905 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 8906 cmlb_free_handle(&un->un_cmlbhandle); 8907 8908 /* 8909 * Hold the detach mutex here, to make sure that no other threads ever 8910 * can access a (partially) freed soft state structure. 8911 */ 8912 mutex_enter(&sd_detach_mutex); 8913 8914 /* 8915 * Clean up the soft state struct. 8916 * Cleanup is done in reverse order of allocs/inits. 8917 * At this point there should be no competing threads anymore. 8918 */ 8919 8920 scsi_fm_fini(devp); 8921 8922 /* 8923 * Deallocate memory for SCSI FMA. 8924 */ 8925 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8926 8927 /* 8928 * Unregister and free device id if it was not registered 8929 * by the transport. 8930 */ 8931 if (un->un_f_devid_transport_defined == FALSE) 8932 ddi_devid_unregister(devi); 8933 8934 /* 8935 * free the devid structure if allocated before (by ddi_devid_init() 8936 * or ddi_devid_get()). 8937 */ 8938 if (un->un_devid) { 8939 ddi_devid_free(un->un_devid); 8940 un->un_devid = NULL; 8941 } 8942 8943 /* 8944 * Destroy wmap cache if it exists. 8945 */ 8946 if (un->un_wm_cache != NULL) { 8947 kmem_cache_destroy(un->un_wm_cache); 8948 un->un_wm_cache = NULL; 8949 } 8950 8951 /* 8952 * kstat cleanup is done in detach for all device types (4363169). 8953 * We do not want to fail detach if the device kstats are not deleted 8954 * since there is a confusion about the devo_refcnt for the device. 8955 * We just delete the kstats and let detach complete successfully. 8956 */ 8957 if (un->un_stats != NULL) { 8958 kstat_delete(un->un_stats); 8959 un->un_stats = NULL; 8960 } 8961 if (un->un_errstats != NULL) { 8962 kstat_delete(un->un_errstats); 8963 un->un_errstats = NULL; 8964 } 8965 8966 /* Remove partition stats */ 8967 if (un->un_f_pkstats_enabled) { 8968 for (i = 0; i < NSDMAP; i++) { 8969 if (un->un_pstats[i] != NULL) { 8970 kstat_delete(un->un_pstats[i]); 8971 un->un_pstats[i] = NULL; 8972 } 8973 } 8974 } 8975 8976 /* Remove xbuf registration */ 8977 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8978 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8979 8980 /* Remove driver properties */ 8981 ddi_prop_remove_all(devi); 8982 8983 mutex_destroy(&un->un_pm_mutex); 8984 cv_destroy(&un->un_pm_busy_cv); 8985 8986 cv_destroy(&un->un_wcc_cv); 8987 8988 /* Open/close semaphore */ 8989 sema_destroy(&un->un_semoclose); 8990 8991 /* Removable media condvar. */ 8992 cv_destroy(&un->un_state_cv); 8993 8994 /* Suspend/resume condvar. */ 8995 cv_destroy(&un->un_suspend_cv); 8996 cv_destroy(&un->un_disk_busy_cv); 8997 8998 sd_free_rqs(un); 8999 9000 /* Free up soft state */ 9001 devp->sd_private = NULL; 9002 9003 bzero(un, sizeof (struct sd_lun)); 9004 #ifndef XPV_HVM_DRIVER 9005 ddi_soft_state_free(sd_state, instance); 9006 #endif /* !XPV_HVM_DRIVER */ 9007 9008 mutex_exit(&sd_detach_mutex); 9009 9010 /* This frees up the INQUIRY data associated with the device. */ 9011 scsi_unprobe(devp); 9012 9013 /* 9014 * After successfully detaching an instance, we update the information 9015 * of how many luns have been attached in the relative target and 9016 * controller for parallel SCSI. This information is used when sd tries 9017 * to set the tagged queuing capability in HBA. 9018 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9019 * check if the device is parallel SCSI. However, we don't need to 9020 * check here because we've already checked during attach. No device 9021 * that is not parallel SCSI is in the chain. 9022 */ 9023 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9024 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9025 } 9026 9027 return (DDI_SUCCESS); 9028 9029 err_notclosed: 9030 mutex_exit(SD_MUTEX(un)); 9031 9032 err_stillbusy: 9033 _NOTE(NO_COMPETING_THREADS_NOW); 9034 9035 err_remove_event: 9036 mutex_enter(&sd_detach_mutex); 9037 un->un_detach_count--; 9038 mutex_exit(&sd_detach_mutex); 9039 9040 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9041 return (DDI_FAILURE); 9042 } 9043 9044 9045 /* 9046 * Function: sd_create_errstats 9047 * 9048 * Description: This routine instantiates the device error stats. 9049 * 9050 * Note: During attach the stats are instantiated first so they are 9051 * available for attach-time routines that utilize the driver 9052 * iopath to send commands to the device. The stats are initialized 9053 * separately so data obtained during some attach-time routines is 9054 * available. (4362483) 9055 * 9056 * Arguments: un - driver soft state (unit) structure 9057 * instance - driver instance 9058 * 9059 * Context: Kernel thread context 9060 */ 9061 9062 static void 9063 sd_create_errstats(struct sd_lun *un, int instance) 9064 { 9065 struct sd_errstats *stp; 9066 char kstatmodule_err[KSTAT_STRLEN]; 9067 char kstatname[KSTAT_STRLEN]; 9068 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9069 9070 ASSERT(un != NULL); 9071 9072 if (un->un_errstats != NULL) { 9073 return; 9074 } 9075 9076 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9077 "%serr", sd_label); 9078 (void) snprintf(kstatname, sizeof (kstatname), 9079 "%s%d,err", sd_label, instance); 9080 9081 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9082 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9083 9084 if (un->un_errstats == NULL) { 9085 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9086 "sd_create_errstats: Failed kstat_create\n"); 9087 return; 9088 } 9089 9090 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9091 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9092 KSTAT_DATA_UINT32); 9093 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9094 KSTAT_DATA_UINT32); 9095 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9096 KSTAT_DATA_UINT32); 9097 kstat_named_init(&stp->sd_vid, "Vendor", 9098 KSTAT_DATA_CHAR); 9099 kstat_named_init(&stp->sd_pid, "Product", 9100 KSTAT_DATA_CHAR); 9101 kstat_named_init(&stp->sd_revision, "Revision", 9102 KSTAT_DATA_CHAR); 9103 kstat_named_init(&stp->sd_serial, "Serial No", 9104 KSTAT_DATA_CHAR); 9105 kstat_named_init(&stp->sd_capacity, "Size", 9106 KSTAT_DATA_ULONGLONG); 9107 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9108 KSTAT_DATA_UINT32); 9109 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9110 KSTAT_DATA_UINT32); 9111 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9112 KSTAT_DATA_UINT32); 9113 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9114 KSTAT_DATA_UINT32); 9115 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9116 KSTAT_DATA_UINT32); 9117 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9118 KSTAT_DATA_UINT32); 9119 9120 un->un_errstats->ks_private = un; 9121 un->un_errstats->ks_update = nulldev; 9122 9123 kstat_install(un->un_errstats); 9124 } 9125 9126 9127 /* 9128 * Function: sd_set_errstats 9129 * 9130 * Description: This routine sets the value of the vendor id, product id, 9131 * revision, serial number, and capacity device error stats. 9132 * 9133 * Note: During attach the stats are instantiated first so they are 9134 * available for attach-time routines that utilize the driver 9135 * iopath to send commands to the device. The stats are initialized 9136 * separately so data obtained during some attach-time routines is 9137 * available. (4362483) 9138 * 9139 * Arguments: un - driver soft state (unit) structure 9140 * 9141 * Context: Kernel thread context 9142 */ 9143 9144 static void 9145 sd_set_errstats(struct sd_lun *un) 9146 { 9147 struct sd_errstats *stp; 9148 char *sn; 9149 9150 ASSERT(un != NULL); 9151 ASSERT(un->un_errstats != NULL); 9152 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9153 ASSERT(stp != NULL); 9154 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9155 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9156 (void) strncpy(stp->sd_revision.value.c, 9157 un->un_sd->sd_inq->inq_revision, 4); 9158 9159 /* 9160 * All the errstats are persistent across detach/attach, 9161 * so reset all the errstats here in case of the hot 9162 * replacement of disk drives, except for not changed 9163 * Sun qualified drives. 9164 */ 9165 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9166 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9167 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9168 stp->sd_softerrs.value.ui32 = 0; 9169 stp->sd_harderrs.value.ui32 = 0; 9170 stp->sd_transerrs.value.ui32 = 0; 9171 stp->sd_rq_media_err.value.ui32 = 0; 9172 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9173 stp->sd_rq_nodev_err.value.ui32 = 0; 9174 stp->sd_rq_recov_err.value.ui32 = 0; 9175 stp->sd_rq_illrq_err.value.ui32 = 0; 9176 stp->sd_rq_pfa_err.value.ui32 = 0; 9177 } 9178 9179 /* 9180 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9181 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9182 * (4376302)) 9183 */ 9184 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9185 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9186 sizeof (SD_INQUIRY(un)->inq_serial)); 9187 } else { 9188 /* 9189 * Set the "Serial No" kstat for non-Sun qualified drives 9190 */ 9191 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un), 9192 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 9193 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) { 9194 (void) strlcpy(stp->sd_serial.value.c, sn, 9195 sizeof (stp->sd_serial.value.c)); 9196 ddi_prop_free(sn); 9197 } 9198 } 9199 9200 if (un->un_f_blockcount_is_valid != TRUE) { 9201 /* 9202 * Set capacity error stat to 0 for no media. This ensures 9203 * a valid capacity is displayed in response to 'iostat -E' 9204 * when no media is present in the device. 9205 */ 9206 stp->sd_capacity.value.ui64 = 0; 9207 } else { 9208 /* 9209 * Multiply un_blockcount by un->un_sys_blocksize to get 9210 * capacity. 9211 * 9212 * Note: for non-512 blocksize devices "un_blockcount" has been 9213 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9214 * (un_tgt_blocksize / un->un_sys_blocksize). 9215 */ 9216 stp->sd_capacity.value.ui64 = (uint64_t) 9217 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9218 } 9219 } 9220 9221 9222 /* 9223 * Function: sd_set_pstats 9224 * 9225 * Description: This routine instantiates and initializes the partition 9226 * stats for each partition with more than zero blocks. 9227 * (4363169) 9228 * 9229 * Arguments: un - driver soft state (unit) structure 9230 * 9231 * Context: Kernel thread context 9232 */ 9233 9234 static void 9235 sd_set_pstats(struct sd_lun *un) 9236 { 9237 char kstatname[KSTAT_STRLEN]; 9238 int instance; 9239 int i; 9240 diskaddr_t nblks = 0; 9241 char *partname = NULL; 9242 9243 ASSERT(un != NULL); 9244 9245 instance = ddi_get_instance(SD_DEVINFO(un)); 9246 9247 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9248 for (i = 0; i < NSDMAP; i++) { 9249 9250 if (cmlb_partinfo(un->un_cmlbhandle, i, 9251 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 9252 continue; 9253 mutex_enter(SD_MUTEX(un)); 9254 9255 if ((un->un_pstats[i] == NULL) && 9256 (nblks != 0)) { 9257 9258 (void) snprintf(kstatname, sizeof (kstatname), 9259 "%s%d,%s", sd_label, instance, 9260 partname); 9261 9262 un->un_pstats[i] = kstat_create(sd_label, 9263 instance, kstatname, "partition", KSTAT_TYPE_IO, 9264 1, KSTAT_FLAG_PERSISTENT); 9265 if (un->un_pstats[i] != NULL) { 9266 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9267 kstat_install(un->un_pstats[i]); 9268 } 9269 } 9270 mutex_exit(SD_MUTEX(un)); 9271 } 9272 } 9273 9274 9275 #if (defined(__fibre)) 9276 /* 9277 * Function: sd_init_event_callbacks 9278 * 9279 * Description: This routine initializes the insertion and removal event 9280 * callbacks. (fibre only) 9281 * 9282 * Arguments: un - driver soft state (unit) structure 9283 * 9284 * Context: Kernel thread context 9285 */ 9286 9287 static void 9288 sd_init_event_callbacks(struct sd_lun *un) 9289 { 9290 ASSERT(un != NULL); 9291 9292 if ((un->un_insert_event == NULL) && 9293 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9294 &un->un_insert_event) == DDI_SUCCESS)) { 9295 /* 9296 * Add the callback for an insertion event 9297 */ 9298 (void) ddi_add_event_handler(SD_DEVINFO(un), 9299 un->un_insert_event, sd_event_callback, (void *)un, 9300 &(un->un_insert_cb_id)); 9301 } 9302 9303 if ((un->un_remove_event == NULL) && 9304 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9305 &un->un_remove_event) == DDI_SUCCESS)) { 9306 /* 9307 * Add the callback for a removal event 9308 */ 9309 (void) ddi_add_event_handler(SD_DEVINFO(un), 9310 un->un_remove_event, sd_event_callback, (void *)un, 9311 &(un->un_remove_cb_id)); 9312 } 9313 } 9314 9315 9316 /* 9317 * Function: sd_event_callback 9318 * 9319 * Description: This routine handles insert/remove events (photon). The 9320 * state is changed to OFFLINE which can be used to supress 9321 * error msgs. (fibre only) 9322 * 9323 * Arguments: un - driver soft state (unit) structure 9324 * 9325 * Context: Callout thread context 9326 */ 9327 /* ARGSUSED */ 9328 static void 9329 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9330 void *bus_impldata) 9331 { 9332 struct sd_lun *un = (struct sd_lun *)arg; 9333 9334 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9335 if (event == un->un_insert_event) { 9336 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9337 mutex_enter(SD_MUTEX(un)); 9338 if (un->un_state == SD_STATE_OFFLINE) { 9339 if (un->un_last_state != SD_STATE_SUSPENDED) { 9340 un->un_state = un->un_last_state; 9341 } else { 9342 /* 9343 * We have gone through SUSPEND/RESUME while 9344 * we were offline. Restore the last state 9345 */ 9346 un->un_state = un->un_save_state; 9347 } 9348 } 9349 mutex_exit(SD_MUTEX(un)); 9350 9351 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9352 } else if (event == un->un_remove_event) { 9353 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9354 mutex_enter(SD_MUTEX(un)); 9355 /* 9356 * We need to handle an event callback that occurs during 9357 * the suspend operation, since we don't prevent it. 9358 */ 9359 if (un->un_state != SD_STATE_OFFLINE) { 9360 if (un->un_state != SD_STATE_SUSPENDED) { 9361 New_state(un, SD_STATE_OFFLINE); 9362 } else { 9363 un->un_last_state = SD_STATE_OFFLINE; 9364 } 9365 } 9366 mutex_exit(SD_MUTEX(un)); 9367 } else { 9368 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9369 "!Unknown event\n"); 9370 } 9371 9372 } 9373 #endif 9374 9375 /* 9376 * Function: sd_cache_control() 9377 * 9378 * Description: This routine is the driver entry point for setting 9379 * read and write caching by modifying the WCE (write cache 9380 * enable) and RCD (read cache disable) bits of mode 9381 * page 8 (MODEPAGE_CACHING). 9382 * 9383 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9384 * structure for this target. 9385 * rcd_flag - flag for controlling the read cache 9386 * wce_flag - flag for controlling the write cache 9387 * 9388 * Return Code: EIO 9389 * code returned by sd_send_scsi_MODE_SENSE and 9390 * sd_send_scsi_MODE_SELECT 9391 * 9392 * Context: Kernel Thread 9393 */ 9394 9395 static int 9396 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag) 9397 { 9398 struct mode_caching *mode_caching_page; 9399 uchar_t *header; 9400 size_t buflen; 9401 int hdrlen; 9402 int bd_len; 9403 int rval = 0; 9404 struct mode_header_grp2 *mhp; 9405 struct sd_lun *un; 9406 int status; 9407 9408 ASSERT(ssc != NULL); 9409 un = ssc->ssc_un; 9410 ASSERT(un != NULL); 9411 9412 /* 9413 * Do a test unit ready, otherwise a mode sense may not work if this 9414 * is the first command sent to the device after boot. 9415 */ 9416 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9417 if (status != 0) 9418 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9419 9420 if (un->un_f_cfg_is_atapi == TRUE) { 9421 hdrlen = MODE_HEADER_LENGTH_GRP2; 9422 } else { 9423 hdrlen = MODE_HEADER_LENGTH; 9424 } 9425 9426 /* 9427 * Allocate memory for the retrieved mode page and its headers. Set 9428 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9429 * we get all of the mode sense data otherwise, the mode select 9430 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9431 */ 9432 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9433 sizeof (struct mode_cache_scsi3); 9434 9435 header = kmem_zalloc(buflen, KM_SLEEP); 9436 9437 /* Get the information from the device. */ 9438 if (un->un_f_cfg_is_atapi == TRUE) { 9439 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9440 MODEPAGE_CACHING, SD_PATH_DIRECT); 9441 } else { 9442 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9443 MODEPAGE_CACHING, SD_PATH_DIRECT); 9444 } 9445 9446 if (rval != 0) { 9447 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9448 "sd_cache_control: Mode Sense Failed\n"); 9449 goto mode_sense_failed; 9450 } 9451 9452 /* 9453 * Determine size of Block Descriptors in order to locate 9454 * the mode page data. ATAPI devices return 0, SCSI devices 9455 * should return MODE_BLK_DESC_LENGTH. 9456 */ 9457 if (un->un_f_cfg_is_atapi == TRUE) { 9458 mhp = (struct mode_header_grp2 *)header; 9459 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9460 } else { 9461 bd_len = ((struct mode_header *)header)->bdesc_length; 9462 } 9463 9464 if (bd_len > MODE_BLK_DESC_LENGTH) { 9465 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9466 "sd_cache_control: Mode Sense returned invalid block " 9467 "descriptor length\n"); 9468 rval = EIO; 9469 goto mode_sense_failed; 9470 } 9471 9472 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9473 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9474 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9475 "sd_cache_control: Mode Sense caching page code mismatch " 9476 "%d\n", mode_caching_page->mode_page.code); 9477 rval = EIO; 9478 goto mode_sense_failed; 9479 } 9480 9481 /* Check the relevant bits on successful mode sense. */ 9482 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9483 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9484 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9485 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9486 9487 size_t sbuflen; 9488 uchar_t save_pg; 9489 9490 /* 9491 * Construct select buffer length based on the 9492 * length of the sense data returned. 9493 */ 9494 sbuflen = hdrlen + bd_len + 9495 sizeof (struct mode_page) + 9496 (int)mode_caching_page->mode_page.length; 9497 9498 /* 9499 * Set the caching bits as requested. 9500 */ 9501 if (rcd_flag == SD_CACHE_ENABLE) 9502 mode_caching_page->rcd = 0; 9503 else if (rcd_flag == SD_CACHE_DISABLE) 9504 mode_caching_page->rcd = 1; 9505 9506 if (wce_flag == SD_CACHE_ENABLE) 9507 mode_caching_page->wce = 1; 9508 else if (wce_flag == SD_CACHE_DISABLE) 9509 mode_caching_page->wce = 0; 9510 9511 /* 9512 * Save the page if the mode sense says the 9513 * drive supports it. 9514 */ 9515 save_pg = mode_caching_page->mode_page.ps ? 9516 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9517 9518 /* Clear reserved bits before mode select. */ 9519 mode_caching_page->mode_page.ps = 0; 9520 9521 /* 9522 * Clear out mode header for mode select. 9523 * The rest of the retrieved page will be reused. 9524 */ 9525 bzero(header, hdrlen); 9526 9527 if (un->un_f_cfg_is_atapi == TRUE) { 9528 mhp = (struct mode_header_grp2 *)header; 9529 mhp->bdesc_length_hi = bd_len >> 8; 9530 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9531 } else { 9532 ((struct mode_header *)header)->bdesc_length = bd_len; 9533 } 9534 9535 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9536 9537 /* Issue mode select to change the cache settings */ 9538 if (un->un_f_cfg_is_atapi == TRUE) { 9539 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header, 9540 sbuflen, save_pg, SD_PATH_DIRECT); 9541 } else { 9542 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 9543 sbuflen, save_pg, SD_PATH_DIRECT); 9544 } 9545 9546 } 9547 9548 9549 mode_sense_failed: 9550 9551 kmem_free(header, buflen); 9552 9553 if (rval != 0) { 9554 if (rval == EIO) 9555 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9556 else 9557 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9558 } 9559 return (rval); 9560 } 9561 9562 9563 /* 9564 * Function: sd_get_write_cache_enabled() 9565 * 9566 * Description: This routine is the driver entry point for determining if 9567 * write caching is enabled. It examines the WCE (write cache 9568 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9569 * 9570 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9571 * structure for this target. 9572 * is_enabled - pointer to int where write cache enabled state 9573 * is returned (non-zero -> write cache enabled) 9574 * 9575 * 9576 * Return Code: EIO 9577 * code returned by sd_send_scsi_MODE_SENSE 9578 * 9579 * Context: Kernel Thread 9580 * 9581 * NOTE: If ioctl is added to disable write cache, this sequence should 9582 * be followed so that no locking is required for accesses to 9583 * un->un_f_write_cache_enabled: 9584 * do mode select to clear wce 9585 * do synchronize cache to flush cache 9586 * set un->un_f_write_cache_enabled = FALSE 9587 * 9588 * Conversely, an ioctl to enable the write cache should be done 9589 * in this order: 9590 * set un->un_f_write_cache_enabled = TRUE 9591 * do mode select to set wce 9592 */ 9593 9594 static int 9595 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled) 9596 { 9597 struct mode_caching *mode_caching_page; 9598 uchar_t *header; 9599 size_t buflen; 9600 int hdrlen; 9601 int bd_len; 9602 int rval = 0; 9603 struct sd_lun *un; 9604 int status; 9605 9606 ASSERT(ssc != NULL); 9607 un = ssc->ssc_un; 9608 ASSERT(un != NULL); 9609 ASSERT(is_enabled != NULL); 9610 9611 /* in case of error, flag as enabled */ 9612 *is_enabled = TRUE; 9613 9614 /* 9615 * Do a test unit ready, otherwise a mode sense may not work if this 9616 * is the first command sent to the device after boot. 9617 */ 9618 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9619 9620 if (status != 0) 9621 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9622 9623 if (un->un_f_cfg_is_atapi == TRUE) { 9624 hdrlen = MODE_HEADER_LENGTH_GRP2; 9625 } else { 9626 hdrlen = MODE_HEADER_LENGTH; 9627 } 9628 9629 /* 9630 * Allocate memory for the retrieved mode page and its headers. Set 9631 * a pointer to the page itself. 9632 */ 9633 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9634 header = kmem_zalloc(buflen, KM_SLEEP); 9635 9636 /* Get the information from the device. */ 9637 if (un->un_f_cfg_is_atapi == TRUE) { 9638 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9639 MODEPAGE_CACHING, SD_PATH_DIRECT); 9640 } else { 9641 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9642 MODEPAGE_CACHING, SD_PATH_DIRECT); 9643 } 9644 9645 if (rval != 0) { 9646 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9647 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9648 goto mode_sense_failed; 9649 } 9650 9651 /* 9652 * Determine size of Block Descriptors in order to locate 9653 * the mode page data. ATAPI devices return 0, SCSI devices 9654 * should return MODE_BLK_DESC_LENGTH. 9655 */ 9656 if (un->un_f_cfg_is_atapi == TRUE) { 9657 struct mode_header_grp2 *mhp; 9658 mhp = (struct mode_header_grp2 *)header; 9659 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9660 } else { 9661 bd_len = ((struct mode_header *)header)->bdesc_length; 9662 } 9663 9664 if (bd_len > MODE_BLK_DESC_LENGTH) { 9665 /* FMA should make upset complain here */ 9666 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9667 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9668 "block descriptor length\n"); 9669 rval = EIO; 9670 goto mode_sense_failed; 9671 } 9672 9673 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9674 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9675 /* FMA could make upset complain here */ 9676 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9677 "sd_get_write_cache_enabled: Mode Sense caching page " 9678 "code mismatch %d\n", mode_caching_page->mode_page.code); 9679 rval = EIO; 9680 goto mode_sense_failed; 9681 } 9682 *is_enabled = mode_caching_page->wce; 9683 9684 mode_sense_failed: 9685 if (rval == 0) { 9686 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 9687 } else if (rval == EIO) { 9688 /* 9689 * Some disks do not support mode sense(6), we 9690 * should ignore this kind of error(sense key is 9691 * 0x5 - illegal request). 9692 */ 9693 uint8_t *sensep; 9694 int senlen; 9695 9696 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 9697 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 9698 ssc->ssc_uscsi_cmd->uscsi_rqresid); 9699 9700 if (senlen > 0 && 9701 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 9702 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 9703 } else { 9704 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9705 } 9706 } else { 9707 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9708 } 9709 kmem_free(header, buflen); 9710 return (rval); 9711 } 9712 9713 /* 9714 * Function: sd_get_nv_sup() 9715 * 9716 * Description: This routine is the driver entry point for 9717 * determining whether non-volatile cache is supported. This 9718 * determination process works as follows: 9719 * 9720 * 1. sd first queries sd.conf on whether 9721 * suppress_cache_flush bit is set for this device. 9722 * 9723 * 2. if not there, then queries the internal disk table. 9724 * 9725 * 3. if either sd.conf or internal disk table specifies 9726 * cache flush be suppressed, we don't bother checking 9727 * NV_SUP bit. 9728 * 9729 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 9730 * the optional INQUIRY VPD page 0x86. If the device 9731 * supports VPD page 0x86, sd examines the NV_SUP 9732 * (non-volatile cache support) bit in the INQUIRY VPD page 9733 * 0x86: 9734 * o If NV_SUP bit is set, sd assumes the device has a 9735 * non-volatile cache and set the 9736 * un_f_sync_nv_supported to TRUE. 9737 * o Otherwise cache is not non-volatile, 9738 * un_f_sync_nv_supported is set to FALSE. 9739 * 9740 * Arguments: un - driver soft state (unit) structure 9741 * 9742 * Return Code: 9743 * 9744 * Context: Kernel Thread 9745 */ 9746 9747 static void 9748 sd_get_nv_sup(sd_ssc_t *ssc) 9749 { 9750 int rval = 0; 9751 uchar_t *inq86 = NULL; 9752 size_t inq86_len = MAX_INQUIRY_SIZE; 9753 size_t inq86_resid = 0; 9754 struct dk_callback *dkc; 9755 struct sd_lun *un; 9756 9757 ASSERT(ssc != NULL); 9758 un = ssc->ssc_un; 9759 ASSERT(un != NULL); 9760 9761 mutex_enter(SD_MUTEX(un)); 9762 9763 /* 9764 * Be conservative on the device's support of 9765 * SYNC_NV bit: un_f_sync_nv_supported is 9766 * initialized to be false. 9767 */ 9768 un->un_f_sync_nv_supported = FALSE; 9769 9770 /* 9771 * If either sd.conf or internal disk table 9772 * specifies cache flush be suppressed, then 9773 * we don't bother checking NV_SUP bit. 9774 */ 9775 if (un->un_f_suppress_cache_flush == TRUE) { 9776 mutex_exit(SD_MUTEX(un)); 9777 return; 9778 } 9779 9780 if (sd_check_vpd_page_support(ssc) == 0 && 9781 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 9782 mutex_exit(SD_MUTEX(un)); 9783 /* collect page 86 data if available */ 9784 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 9785 9786 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len, 9787 0x01, 0x86, &inq86_resid); 9788 9789 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 9790 SD_TRACE(SD_LOG_COMMON, un, 9791 "sd_get_nv_sup: \ 9792 successfully get VPD page: %x \ 9793 PAGE LENGTH: %x BYTE 6: %x\n", 9794 inq86[1], inq86[3], inq86[6]); 9795 9796 mutex_enter(SD_MUTEX(un)); 9797 /* 9798 * check the value of NV_SUP bit: only if the device 9799 * reports NV_SUP bit to be 1, the 9800 * un_f_sync_nv_supported bit will be set to true. 9801 */ 9802 if (inq86[6] & SD_VPD_NV_SUP) { 9803 un->un_f_sync_nv_supported = TRUE; 9804 } 9805 mutex_exit(SD_MUTEX(un)); 9806 } else if (rval != 0) { 9807 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9808 } 9809 9810 kmem_free(inq86, inq86_len); 9811 } else { 9812 mutex_exit(SD_MUTEX(un)); 9813 } 9814 9815 /* 9816 * Send a SYNC CACHE command to check whether 9817 * SYNC_NV bit is supported. This command should have 9818 * un_f_sync_nv_supported set to correct value. 9819 */ 9820 mutex_enter(SD_MUTEX(un)); 9821 if (un->un_f_sync_nv_supported) { 9822 mutex_exit(SD_MUTEX(un)); 9823 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 9824 dkc->dkc_flag = FLUSH_VOLATILE; 9825 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 9826 9827 /* 9828 * Send a TEST UNIT READY command to the device. This should 9829 * clear any outstanding UNIT ATTENTION that may be present. 9830 */ 9831 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 9832 if (rval != 0) 9833 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9834 9835 kmem_free(dkc, sizeof (struct dk_callback)); 9836 } else { 9837 mutex_exit(SD_MUTEX(un)); 9838 } 9839 9840 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 9841 un_f_suppress_cache_flush is set to %d\n", 9842 un->un_f_suppress_cache_flush); 9843 } 9844 9845 /* 9846 * Function: sd_make_device 9847 * 9848 * Description: Utility routine to return the Solaris device number from 9849 * the data in the device's dev_info structure. 9850 * 9851 * Return Code: The Solaris device number 9852 * 9853 * Context: Any 9854 */ 9855 9856 static dev_t 9857 sd_make_device(dev_info_t *devi) 9858 { 9859 return (makedevice(ddi_driver_major(devi), 9860 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9861 } 9862 9863 9864 /* 9865 * Function: sd_pm_entry 9866 * 9867 * Description: Called at the start of a new command to manage power 9868 * and busy status of a device. This includes determining whether 9869 * the current power state of the device is sufficient for 9870 * performing the command or whether it must be changed. 9871 * The PM framework is notified appropriately. 9872 * Only with a return status of DDI_SUCCESS will the 9873 * component be busy to the framework. 9874 * 9875 * All callers of sd_pm_entry must check the return status 9876 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9877 * of DDI_FAILURE indicates the device failed to power up. 9878 * In this case un_pm_count has been adjusted so the result 9879 * on exit is still powered down, ie. count is less than 0. 9880 * Calling sd_pm_exit with this count value hits an ASSERT. 9881 * 9882 * Return Code: DDI_SUCCESS or DDI_FAILURE 9883 * 9884 * Context: Kernel thread context. 9885 */ 9886 9887 static int 9888 sd_pm_entry(struct sd_lun *un) 9889 { 9890 int return_status = DDI_SUCCESS; 9891 9892 ASSERT(!mutex_owned(SD_MUTEX(un))); 9893 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9894 9895 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9896 9897 if (un->un_f_pm_is_enabled == FALSE) { 9898 SD_TRACE(SD_LOG_IO_PM, un, 9899 "sd_pm_entry: exiting, PM not enabled\n"); 9900 return (return_status); 9901 } 9902 9903 /* 9904 * Just increment a counter if PM is enabled. On the transition from 9905 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9906 * the count with each IO and mark the device as idle when the count 9907 * hits 0. 9908 * 9909 * If the count is less than 0 the device is powered down. If a powered 9910 * down device is successfully powered up then the count must be 9911 * incremented to reflect the power up. Note that it'll get incremented 9912 * a second time to become busy. 9913 * 9914 * Because the following has the potential to change the device state 9915 * and must release the un_pm_mutex to do so, only one thread can be 9916 * allowed through at a time. 9917 */ 9918 9919 mutex_enter(&un->un_pm_mutex); 9920 while (un->un_pm_busy == TRUE) { 9921 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9922 } 9923 un->un_pm_busy = TRUE; 9924 9925 if (un->un_pm_count < 1) { 9926 9927 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9928 9929 /* 9930 * Indicate we are now busy so the framework won't attempt to 9931 * power down the device. This call will only fail if either 9932 * we passed a bad component number or the device has no 9933 * components. Neither of these should ever happen. 9934 */ 9935 mutex_exit(&un->un_pm_mutex); 9936 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9937 ASSERT(return_status == DDI_SUCCESS); 9938 9939 mutex_enter(&un->un_pm_mutex); 9940 9941 if (un->un_pm_count < 0) { 9942 mutex_exit(&un->un_pm_mutex); 9943 9944 SD_TRACE(SD_LOG_IO_PM, un, 9945 "sd_pm_entry: power up component\n"); 9946 9947 /* 9948 * pm_raise_power will cause sdpower to be called 9949 * which brings the device power level to the 9950 * desired state, If successful, un_pm_count and 9951 * un_power_level will be updated appropriately. 9952 */ 9953 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9954 SD_PM_STATE_ACTIVE(un)); 9955 9956 mutex_enter(&un->un_pm_mutex); 9957 9958 if (return_status != DDI_SUCCESS) { 9959 /* 9960 * Power up failed. 9961 * Idle the device and adjust the count 9962 * so the result on exit is that we're 9963 * still powered down, ie. count is less than 0. 9964 */ 9965 SD_TRACE(SD_LOG_IO_PM, un, 9966 "sd_pm_entry: power up failed," 9967 " idle the component\n"); 9968 9969 (void) pm_idle_component(SD_DEVINFO(un), 0); 9970 un->un_pm_count--; 9971 } else { 9972 /* 9973 * Device is powered up, verify the 9974 * count is non-negative. 9975 * This is debug only. 9976 */ 9977 ASSERT(un->un_pm_count == 0); 9978 } 9979 } 9980 9981 if (return_status == DDI_SUCCESS) { 9982 /* 9983 * For performance, now that the device has been tagged 9984 * as busy, and it's known to be powered up, update the 9985 * chain types to use jump tables that do not include 9986 * pm. This significantly lowers the overhead and 9987 * therefore improves performance. 9988 */ 9989 9990 mutex_exit(&un->un_pm_mutex); 9991 mutex_enter(SD_MUTEX(un)); 9992 SD_TRACE(SD_LOG_IO_PM, un, 9993 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9994 un->un_uscsi_chain_type); 9995 9996 if (un->un_f_non_devbsize_supported) { 9997 un->un_buf_chain_type = 9998 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9999 } else { 10000 un->un_buf_chain_type = 10001 SD_CHAIN_INFO_DISK_NO_PM; 10002 } 10003 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10004 10005 SD_TRACE(SD_LOG_IO_PM, un, 10006 " changed uscsi_chain_type to %d\n", 10007 un->un_uscsi_chain_type); 10008 mutex_exit(SD_MUTEX(un)); 10009 mutex_enter(&un->un_pm_mutex); 10010 10011 if (un->un_pm_idle_timeid == NULL) { 10012 /* 300 ms. */ 10013 un->un_pm_idle_timeid = 10014 timeout(sd_pm_idletimeout_handler, un, 10015 (drv_usectohz((clock_t)300000))); 10016 /* 10017 * Include an extra call to busy which keeps the 10018 * device busy with-respect-to the PM layer 10019 * until the timer fires, at which time it'll 10020 * get the extra idle call. 10021 */ 10022 (void) pm_busy_component(SD_DEVINFO(un), 0); 10023 } 10024 } 10025 } 10026 un->un_pm_busy = FALSE; 10027 /* Next... */ 10028 cv_signal(&un->un_pm_busy_cv); 10029 10030 un->un_pm_count++; 10031 10032 SD_TRACE(SD_LOG_IO_PM, un, 10033 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10034 10035 mutex_exit(&un->un_pm_mutex); 10036 10037 return (return_status); 10038 } 10039 10040 10041 /* 10042 * Function: sd_pm_exit 10043 * 10044 * Description: Called at the completion of a command to manage busy 10045 * status for the device. If the device becomes idle the 10046 * PM framework is notified. 10047 * 10048 * Context: Kernel thread context 10049 */ 10050 10051 static void 10052 sd_pm_exit(struct sd_lun *un) 10053 { 10054 ASSERT(!mutex_owned(SD_MUTEX(un))); 10055 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10056 10057 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10058 10059 /* 10060 * After attach the following flag is only read, so don't 10061 * take the penalty of acquiring a mutex for it. 10062 */ 10063 if (un->un_f_pm_is_enabled == TRUE) { 10064 10065 mutex_enter(&un->un_pm_mutex); 10066 un->un_pm_count--; 10067 10068 SD_TRACE(SD_LOG_IO_PM, un, 10069 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10070 10071 ASSERT(un->un_pm_count >= 0); 10072 if (un->un_pm_count == 0) { 10073 mutex_exit(&un->un_pm_mutex); 10074 10075 SD_TRACE(SD_LOG_IO_PM, un, 10076 "sd_pm_exit: idle component\n"); 10077 10078 (void) pm_idle_component(SD_DEVINFO(un), 0); 10079 10080 } else { 10081 mutex_exit(&un->un_pm_mutex); 10082 } 10083 } 10084 10085 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10086 } 10087 10088 10089 /* 10090 * Function: sdopen 10091 * 10092 * Description: Driver's open(9e) entry point function. 10093 * 10094 * Arguments: dev_i - pointer to device number 10095 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10096 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10097 * cred_p - user credential pointer 10098 * 10099 * Return Code: EINVAL 10100 * ENXIO 10101 * EIO 10102 * EROFS 10103 * EBUSY 10104 * 10105 * Context: Kernel thread context 10106 */ 10107 /* ARGSUSED */ 10108 static int 10109 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10110 { 10111 struct sd_lun *un; 10112 int nodelay; 10113 int part; 10114 uint64_t partmask; 10115 int instance; 10116 dev_t dev; 10117 int rval = EIO; 10118 diskaddr_t nblks = 0; 10119 diskaddr_t label_cap; 10120 10121 /* Validate the open type */ 10122 if (otyp >= OTYPCNT) { 10123 return (EINVAL); 10124 } 10125 10126 dev = *dev_p; 10127 instance = SDUNIT(dev); 10128 mutex_enter(&sd_detach_mutex); 10129 10130 /* 10131 * Fail the open if there is no softstate for the instance, or 10132 * if another thread somewhere is trying to detach the instance. 10133 */ 10134 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10135 (un->un_detach_count != 0)) { 10136 mutex_exit(&sd_detach_mutex); 10137 /* 10138 * The probe cache only needs to be cleared when open (9e) fails 10139 * with ENXIO (4238046). 10140 */ 10141 /* 10142 * un-conditionally clearing probe cache is ok with 10143 * separate sd/ssd binaries 10144 * x86 platform can be an issue with both parallel 10145 * and fibre in 1 binary 10146 */ 10147 sd_scsi_clear_probe_cache(); 10148 return (ENXIO); 10149 } 10150 10151 /* 10152 * The un_layer_count is to prevent another thread in specfs from 10153 * trying to detach the instance, which can happen when we are 10154 * called from a higher-layer driver instead of thru specfs. 10155 * This will not be needed when DDI provides a layered driver 10156 * interface that allows specfs to know that an instance is in 10157 * use by a layered driver & should not be detached. 10158 * 10159 * Note: the semantics for layered driver opens are exactly one 10160 * close for every open. 10161 */ 10162 if (otyp == OTYP_LYR) { 10163 un->un_layer_count++; 10164 } 10165 10166 /* 10167 * Keep a count of the current # of opens in progress. This is because 10168 * some layered drivers try to call us as a regular open. This can 10169 * cause problems that we cannot prevent, however by keeping this count 10170 * we can at least keep our open and detach routines from racing against 10171 * each other under such conditions. 10172 */ 10173 un->un_opens_in_progress++; 10174 mutex_exit(&sd_detach_mutex); 10175 10176 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10177 part = SDPART(dev); 10178 partmask = 1 << part; 10179 10180 /* 10181 * We use a semaphore here in order to serialize 10182 * open and close requests on the device. 10183 */ 10184 sema_p(&un->un_semoclose); 10185 10186 mutex_enter(SD_MUTEX(un)); 10187 10188 /* 10189 * All device accesses go thru sdstrategy() where we check 10190 * on suspend status but there could be a scsi_poll command, 10191 * which bypasses sdstrategy(), so we need to check pm 10192 * status. 10193 */ 10194 10195 if (!nodelay) { 10196 while ((un->un_state == SD_STATE_SUSPENDED) || 10197 (un->un_state == SD_STATE_PM_CHANGING)) { 10198 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10199 } 10200 10201 mutex_exit(SD_MUTEX(un)); 10202 if (sd_pm_entry(un) != DDI_SUCCESS) { 10203 rval = EIO; 10204 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10205 "sdopen: sd_pm_entry failed\n"); 10206 goto open_failed_with_pm; 10207 } 10208 mutex_enter(SD_MUTEX(un)); 10209 } 10210 10211 /* check for previous exclusive open */ 10212 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10213 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10214 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10215 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10216 10217 if (un->un_exclopen & (partmask)) { 10218 goto excl_open_fail; 10219 } 10220 10221 if (flag & FEXCL) { 10222 int i; 10223 if (un->un_ocmap.lyropen[part]) { 10224 goto excl_open_fail; 10225 } 10226 for (i = 0; i < (OTYPCNT - 1); i++) { 10227 if (un->un_ocmap.regopen[i] & (partmask)) { 10228 goto excl_open_fail; 10229 } 10230 } 10231 } 10232 10233 /* 10234 * Check the write permission if this is a removable media device, 10235 * NDELAY has not been set, and writable permission is requested. 10236 * 10237 * Note: If NDELAY was set and this is write-protected media the WRITE 10238 * attempt will fail with EIO as part of the I/O processing. This is a 10239 * more permissive implementation that allows the open to succeed and 10240 * WRITE attempts to fail when appropriate. 10241 */ 10242 if (un->un_f_chk_wp_open) { 10243 if ((flag & FWRITE) && (!nodelay)) { 10244 mutex_exit(SD_MUTEX(un)); 10245 /* 10246 * Defer the check for write permission on writable 10247 * DVD drive till sdstrategy and will not fail open even 10248 * if FWRITE is set as the device can be writable 10249 * depending upon the media and the media can change 10250 * after the call to open(). 10251 */ 10252 if (un->un_f_dvdram_writable_device == FALSE) { 10253 if (ISCD(un) || sr_check_wp(dev)) { 10254 rval = EROFS; 10255 mutex_enter(SD_MUTEX(un)); 10256 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10257 "write to cd or write protected media\n"); 10258 goto open_fail; 10259 } 10260 } 10261 mutex_enter(SD_MUTEX(un)); 10262 } 10263 } 10264 10265 /* 10266 * If opening in NDELAY/NONBLOCK mode, just return. 10267 * Check if disk is ready and has a valid geometry later. 10268 */ 10269 if (!nodelay) { 10270 sd_ssc_t *ssc; 10271 10272 mutex_exit(SD_MUTEX(un)); 10273 ssc = sd_ssc_init(un); 10274 rval = sd_ready_and_valid(ssc, part); 10275 sd_ssc_fini(ssc); 10276 mutex_enter(SD_MUTEX(un)); 10277 /* 10278 * Fail if device is not ready or if the number of disk 10279 * blocks is zero or negative for non CD devices. 10280 */ 10281 10282 nblks = 0; 10283 10284 if (rval == SD_READY_VALID && (!ISCD(un))) { 10285 /* if cmlb_partinfo fails, nblks remains 0 */ 10286 mutex_exit(SD_MUTEX(un)); 10287 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 10288 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 10289 mutex_enter(SD_MUTEX(un)); 10290 } 10291 10292 if ((rval != SD_READY_VALID) || 10293 (!ISCD(un) && nblks <= 0)) { 10294 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10295 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10296 "device not ready or invalid disk block value\n"); 10297 goto open_fail; 10298 } 10299 #if defined(__i386) || defined(__amd64) 10300 } else { 10301 uchar_t *cp; 10302 /* 10303 * x86 requires special nodelay handling, so that p0 is 10304 * always defined and accessible. 10305 * Invalidate geometry only if device is not already open. 10306 */ 10307 cp = &un->un_ocmap.chkd[0]; 10308 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10309 if (*cp != (uchar_t)0) { 10310 break; 10311 } 10312 cp++; 10313 } 10314 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10315 mutex_exit(SD_MUTEX(un)); 10316 cmlb_invalidate(un->un_cmlbhandle, 10317 (void *)SD_PATH_DIRECT); 10318 mutex_enter(SD_MUTEX(un)); 10319 } 10320 10321 #endif 10322 } 10323 10324 if (otyp == OTYP_LYR) { 10325 un->un_ocmap.lyropen[part]++; 10326 } else { 10327 un->un_ocmap.regopen[otyp] |= partmask; 10328 } 10329 10330 /* Set up open and exclusive open flags */ 10331 if (flag & FEXCL) { 10332 un->un_exclopen |= (partmask); 10333 } 10334 10335 /* 10336 * If the lun is EFI labeled and lun capacity is greater than the 10337 * capacity contained in the label, log a sys-event to notify the 10338 * interested module. 10339 * To avoid an infinite loop of logging sys-event, we only log the 10340 * event when the lun is not opened in NDELAY mode. The event handler 10341 * should open the lun in NDELAY mode. 10342 */ 10343 if (!nodelay) { 10344 mutex_exit(SD_MUTEX(un)); 10345 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 10346 (void*)SD_PATH_DIRECT) == 0) { 10347 mutex_enter(SD_MUTEX(un)); 10348 if (un->un_f_blockcount_is_valid && 10349 un->un_blockcount > label_cap && 10350 un->un_f_expnevent == B_FALSE) { 10351 un->un_f_expnevent = B_TRUE; 10352 mutex_exit(SD_MUTEX(un)); 10353 sd_log_lun_expansion_event(un, 10354 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 10355 mutex_enter(SD_MUTEX(un)); 10356 } 10357 } else { 10358 mutex_enter(SD_MUTEX(un)); 10359 } 10360 } 10361 10362 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10363 "open of part %d type %d\n", part, otyp); 10364 10365 mutex_exit(SD_MUTEX(un)); 10366 if (!nodelay) { 10367 sd_pm_exit(un); 10368 } 10369 10370 sema_v(&un->un_semoclose); 10371 10372 mutex_enter(&sd_detach_mutex); 10373 un->un_opens_in_progress--; 10374 mutex_exit(&sd_detach_mutex); 10375 10376 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10377 return (DDI_SUCCESS); 10378 10379 excl_open_fail: 10380 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10381 rval = EBUSY; 10382 10383 open_fail: 10384 mutex_exit(SD_MUTEX(un)); 10385 10386 /* 10387 * On a failed open we must exit the pm management. 10388 */ 10389 if (!nodelay) { 10390 sd_pm_exit(un); 10391 } 10392 open_failed_with_pm: 10393 sema_v(&un->un_semoclose); 10394 10395 mutex_enter(&sd_detach_mutex); 10396 un->un_opens_in_progress--; 10397 if (otyp == OTYP_LYR) { 10398 un->un_layer_count--; 10399 } 10400 mutex_exit(&sd_detach_mutex); 10401 10402 return (rval); 10403 } 10404 10405 10406 /* 10407 * Function: sdclose 10408 * 10409 * Description: Driver's close(9e) entry point function. 10410 * 10411 * Arguments: dev - device number 10412 * flag - file status flag, informational only 10413 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10414 * cred_p - user credential pointer 10415 * 10416 * Return Code: ENXIO 10417 * 10418 * Context: Kernel thread context 10419 */ 10420 /* ARGSUSED */ 10421 static int 10422 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10423 { 10424 struct sd_lun *un; 10425 uchar_t *cp; 10426 int part; 10427 int nodelay; 10428 int rval = 0; 10429 10430 /* Validate the open type */ 10431 if (otyp >= OTYPCNT) { 10432 return (ENXIO); 10433 } 10434 10435 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10436 return (ENXIO); 10437 } 10438 10439 part = SDPART(dev); 10440 nodelay = flag & (FNDELAY | FNONBLOCK); 10441 10442 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10443 "sdclose: close of part %d type %d\n", part, otyp); 10444 10445 /* 10446 * We use a semaphore here in order to serialize 10447 * open and close requests on the device. 10448 */ 10449 sema_p(&un->un_semoclose); 10450 10451 mutex_enter(SD_MUTEX(un)); 10452 10453 /* Don't proceed if power is being changed. */ 10454 while (un->un_state == SD_STATE_PM_CHANGING) { 10455 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10456 } 10457 10458 if (un->un_exclopen & (1 << part)) { 10459 un->un_exclopen &= ~(1 << part); 10460 } 10461 10462 /* Update the open partition map */ 10463 if (otyp == OTYP_LYR) { 10464 un->un_ocmap.lyropen[part] -= 1; 10465 } else { 10466 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10467 } 10468 10469 cp = &un->un_ocmap.chkd[0]; 10470 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10471 if (*cp != NULL) { 10472 break; 10473 } 10474 cp++; 10475 } 10476 10477 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10478 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10479 10480 /* 10481 * We avoid persistance upon the last close, and set 10482 * the throttle back to the maximum. 10483 */ 10484 un->un_throttle = un->un_saved_throttle; 10485 10486 if (un->un_state == SD_STATE_OFFLINE) { 10487 if (un->un_f_is_fibre == FALSE) { 10488 scsi_log(SD_DEVINFO(un), sd_label, 10489 CE_WARN, "offline\n"); 10490 } 10491 mutex_exit(SD_MUTEX(un)); 10492 cmlb_invalidate(un->un_cmlbhandle, 10493 (void *)SD_PATH_DIRECT); 10494 mutex_enter(SD_MUTEX(un)); 10495 10496 } else { 10497 /* 10498 * Flush any outstanding writes in NVRAM cache. 10499 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10500 * cmd, it may not work for non-Pluto devices. 10501 * SYNCHRONIZE CACHE is not required for removables, 10502 * except DVD-RAM drives. 10503 * 10504 * Also note: because SYNCHRONIZE CACHE is currently 10505 * the only command issued here that requires the 10506 * drive be powered up, only do the power up before 10507 * sending the Sync Cache command. If additional 10508 * commands are added which require a powered up 10509 * drive, the following sequence may have to change. 10510 * 10511 * And finally, note that parallel SCSI on SPARC 10512 * only issues a Sync Cache to DVD-RAM, a newly 10513 * supported device. 10514 */ 10515 #if defined(__i386) || defined(__amd64) 10516 if ((un->un_f_sync_cache_supported && 10517 un->un_f_sync_cache_required) || 10518 un->un_f_dvdram_writable_device == TRUE) { 10519 #else 10520 if (un->un_f_dvdram_writable_device == TRUE) { 10521 #endif 10522 mutex_exit(SD_MUTEX(un)); 10523 if (sd_pm_entry(un) == DDI_SUCCESS) { 10524 rval = 10525 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10526 NULL); 10527 /* ignore error if not supported */ 10528 if (rval == ENOTSUP) { 10529 rval = 0; 10530 } else if (rval != 0) { 10531 rval = EIO; 10532 } 10533 sd_pm_exit(un); 10534 } else { 10535 rval = EIO; 10536 } 10537 mutex_enter(SD_MUTEX(un)); 10538 } 10539 10540 /* 10541 * For devices which supports DOOR_LOCK, send an ALLOW 10542 * MEDIA REMOVAL command, but don't get upset if it 10543 * fails. We need to raise the power of the drive before 10544 * we can call sd_send_scsi_DOORLOCK() 10545 */ 10546 if (un->un_f_doorlock_supported) { 10547 mutex_exit(SD_MUTEX(un)); 10548 if (sd_pm_entry(un) == DDI_SUCCESS) { 10549 sd_ssc_t *ssc; 10550 10551 ssc = sd_ssc_init(un); 10552 rval = sd_send_scsi_DOORLOCK(ssc, 10553 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10554 if (rval != 0) 10555 sd_ssc_assessment(ssc, 10556 SD_FMT_IGNORE); 10557 sd_ssc_fini(ssc); 10558 10559 sd_pm_exit(un); 10560 if (ISCD(un) && (rval != 0) && 10561 (nodelay != 0)) { 10562 rval = ENXIO; 10563 } 10564 } else { 10565 rval = EIO; 10566 } 10567 mutex_enter(SD_MUTEX(un)); 10568 } 10569 10570 /* 10571 * If a device has removable media, invalidate all 10572 * parameters related to media, such as geometry, 10573 * blocksize, and blockcount. 10574 */ 10575 if (un->un_f_has_removable_media) { 10576 sr_ejected(un); 10577 } 10578 10579 /* 10580 * Destroy the cache (if it exists) which was 10581 * allocated for the write maps since this is 10582 * the last close for this media. 10583 */ 10584 if (un->un_wm_cache) { 10585 /* 10586 * Check if there are pending commands. 10587 * and if there are give a warning and 10588 * do not destroy the cache. 10589 */ 10590 if (un->un_ncmds_in_driver > 0) { 10591 scsi_log(SD_DEVINFO(un), 10592 sd_label, CE_WARN, 10593 "Unable to clean up memory " 10594 "because of pending I/O\n"); 10595 } else { 10596 kmem_cache_destroy( 10597 un->un_wm_cache); 10598 un->un_wm_cache = NULL; 10599 } 10600 } 10601 } 10602 } 10603 10604 mutex_exit(SD_MUTEX(un)); 10605 sema_v(&un->un_semoclose); 10606 10607 if (otyp == OTYP_LYR) { 10608 mutex_enter(&sd_detach_mutex); 10609 /* 10610 * The detach routine may run when the layer count 10611 * drops to zero. 10612 */ 10613 un->un_layer_count--; 10614 mutex_exit(&sd_detach_mutex); 10615 } 10616 10617 return (rval); 10618 } 10619 10620 10621 /* 10622 * Function: sd_ready_and_valid 10623 * 10624 * Description: Test if device is ready and has a valid geometry. 10625 * 10626 * Arguments: ssc - sd_ssc_t will contain un 10627 * un - driver soft state (unit) structure 10628 * 10629 * Return Code: SD_READY_VALID ready and valid label 10630 * SD_NOT_READY_VALID not ready, no label 10631 * SD_RESERVED_BY_OTHERS reservation conflict 10632 * 10633 * Context: Never called at interrupt context. 10634 */ 10635 10636 static int 10637 sd_ready_and_valid(sd_ssc_t *ssc, int part) 10638 { 10639 struct sd_errstats *stp; 10640 uint64_t capacity; 10641 uint_t lbasize; 10642 int rval = SD_READY_VALID; 10643 char name_str[48]; 10644 boolean_t is_valid; 10645 struct sd_lun *un; 10646 int status; 10647 10648 ASSERT(ssc != NULL); 10649 un = ssc->ssc_un; 10650 ASSERT(un != NULL); 10651 ASSERT(!mutex_owned(SD_MUTEX(un))); 10652 10653 mutex_enter(SD_MUTEX(un)); 10654 /* 10655 * If a device has removable media, we must check if media is 10656 * ready when checking if this device is ready and valid. 10657 */ 10658 if (un->un_f_has_removable_media) { 10659 mutex_exit(SD_MUTEX(un)); 10660 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10661 10662 if (status != 0) { 10663 rval = SD_NOT_READY_VALID; 10664 mutex_enter(SD_MUTEX(un)); 10665 10666 /* Ignore all failed status for removalbe media */ 10667 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10668 10669 goto done; 10670 } 10671 10672 is_valid = SD_IS_VALID_LABEL(un); 10673 mutex_enter(SD_MUTEX(un)); 10674 if (!is_valid || 10675 (un->un_f_blockcount_is_valid == FALSE) || 10676 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10677 10678 /* capacity has to be read every open. */ 10679 mutex_exit(SD_MUTEX(un)); 10680 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 10681 &lbasize, SD_PATH_DIRECT); 10682 10683 if (status != 0) { 10684 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10685 10686 cmlb_invalidate(un->un_cmlbhandle, 10687 (void *)SD_PATH_DIRECT); 10688 mutex_enter(SD_MUTEX(un)); 10689 rval = SD_NOT_READY_VALID; 10690 10691 goto done; 10692 } else { 10693 mutex_enter(SD_MUTEX(un)); 10694 sd_update_block_info(un, lbasize, capacity); 10695 } 10696 } 10697 10698 /* 10699 * Check if the media in the device is writable or not. 10700 */ 10701 if (!is_valid && ISCD(un)) { 10702 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 10703 } 10704 10705 } else { 10706 /* 10707 * Do a test unit ready to clear any unit attention from non-cd 10708 * devices. 10709 */ 10710 mutex_exit(SD_MUTEX(un)); 10711 10712 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10713 if (status != 0) { 10714 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10715 } 10716 10717 mutex_enter(SD_MUTEX(un)); 10718 } 10719 10720 10721 /* 10722 * If this is a non 512 block device, allocate space for 10723 * the wmap cache. This is being done here since every time 10724 * a media is changed this routine will be called and the 10725 * block size is a function of media rather than device. 10726 */ 10727 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR || 10728 un->un_f_non_devbsize_supported) && 10729 un->un_tgt_blocksize != DEV_BSIZE) || 10730 un->un_f_enable_rmw) { 10731 if (!(un->un_wm_cache)) { 10732 (void) snprintf(name_str, sizeof (name_str), 10733 "%s%d_cache", 10734 ddi_driver_name(SD_DEVINFO(un)), 10735 ddi_get_instance(SD_DEVINFO(un))); 10736 un->un_wm_cache = kmem_cache_create( 10737 name_str, sizeof (struct sd_w_map), 10738 8, sd_wm_cache_constructor, 10739 sd_wm_cache_destructor, NULL, 10740 (void *)un, NULL, 0); 10741 if (!(un->un_wm_cache)) { 10742 rval = ENOMEM; 10743 goto done; 10744 } 10745 } 10746 } 10747 10748 if (un->un_state == SD_STATE_NORMAL) { 10749 /* 10750 * If the target is not yet ready here (defined by a TUR 10751 * failure), invalidate the geometry and print an 'offline' 10752 * message. This is a legacy message, as the state of the 10753 * target is not actually changed to SD_STATE_OFFLINE. 10754 * 10755 * If the TUR fails for EACCES (Reservation Conflict), 10756 * SD_RESERVED_BY_OTHERS will be returned to indicate 10757 * reservation conflict. If the TUR fails for other 10758 * reasons, SD_NOT_READY_VALID will be returned. 10759 */ 10760 int err; 10761 10762 mutex_exit(SD_MUTEX(un)); 10763 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10764 mutex_enter(SD_MUTEX(un)); 10765 10766 if (err != 0) { 10767 mutex_exit(SD_MUTEX(un)); 10768 cmlb_invalidate(un->un_cmlbhandle, 10769 (void *)SD_PATH_DIRECT); 10770 mutex_enter(SD_MUTEX(un)); 10771 if (err == EACCES) { 10772 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10773 "reservation conflict\n"); 10774 rval = SD_RESERVED_BY_OTHERS; 10775 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10776 } else { 10777 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10778 "drive offline\n"); 10779 rval = SD_NOT_READY_VALID; 10780 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 10781 } 10782 goto done; 10783 } 10784 } 10785 10786 if (un->un_f_format_in_progress == FALSE) { 10787 mutex_exit(SD_MUTEX(un)); 10788 10789 (void) cmlb_validate(un->un_cmlbhandle, 0, 10790 (void *)SD_PATH_DIRECT); 10791 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL, 10792 NULL, (void *) SD_PATH_DIRECT) != 0) { 10793 rval = SD_NOT_READY_VALID; 10794 mutex_enter(SD_MUTEX(un)); 10795 10796 goto done; 10797 } 10798 if (un->un_f_pkstats_enabled) { 10799 sd_set_pstats(un); 10800 SD_TRACE(SD_LOG_IO_PARTITION, un, 10801 "sd_ready_and_valid: un:0x%p pstats created and " 10802 "set\n", un); 10803 } 10804 mutex_enter(SD_MUTEX(un)); 10805 } 10806 10807 /* 10808 * If this device supports DOOR_LOCK command, try and send 10809 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10810 * if it fails. For a CD, however, it is an error 10811 */ 10812 if (un->un_f_doorlock_supported) { 10813 mutex_exit(SD_MUTEX(un)); 10814 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 10815 SD_PATH_DIRECT); 10816 10817 if ((status != 0) && ISCD(un)) { 10818 rval = SD_NOT_READY_VALID; 10819 mutex_enter(SD_MUTEX(un)); 10820 10821 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10822 10823 goto done; 10824 } else if (status != 0) 10825 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10826 mutex_enter(SD_MUTEX(un)); 10827 } 10828 10829 /* The state has changed, inform the media watch routines */ 10830 un->un_mediastate = DKIO_INSERTED; 10831 cv_broadcast(&un->un_state_cv); 10832 rval = SD_READY_VALID; 10833 10834 done: 10835 10836 /* 10837 * Initialize the capacity kstat value, if no media previously 10838 * (capacity kstat is 0) and a media has been inserted 10839 * (un_blockcount > 0). 10840 */ 10841 if (un->un_errstats != NULL) { 10842 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10843 if ((stp->sd_capacity.value.ui64 == 0) && 10844 (un->un_f_blockcount_is_valid == TRUE)) { 10845 stp->sd_capacity.value.ui64 = 10846 (uint64_t)((uint64_t)un->un_blockcount * 10847 un->un_sys_blocksize); 10848 } 10849 } 10850 10851 mutex_exit(SD_MUTEX(un)); 10852 return (rval); 10853 } 10854 10855 10856 /* 10857 * Function: sdmin 10858 * 10859 * Description: Routine to limit the size of a data transfer. Used in 10860 * conjunction with physio(9F). 10861 * 10862 * Arguments: bp - pointer to the indicated buf(9S) struct. 10863 * 10864 * Context: Kernel thread context. 10865 */ 10866 10867 static void 10868 sdmin(struct buf *bp) 10869 { 10870 struct sd_lun *un; 10871 int instance; 10872 10873 instance = SDUNIT(bp->b_edev); 10874 10875 un = ddi_get_soft_state(sd_state, instance); 10876 ASSERT(un != NULL); 10877 10878 /* 10879 * We depend on buf breakup to restrict 10880 * IO size if it is enabled. 10881 */ 10882 if (un->un_buf_breakup_supported) { 10883 return; 10884 } 10885 10886 if (bp->b_bcount > un->un_max_xfer_size) { 10887 bp->b_bcount = un->un_max_xfer_size; 10888 } 10889 } 10890 10891 10892 /* 10893 * Function: sdread 10894 * 10895 * Description: Driver's read(9e) entry point function. 10896 * 10897 * Arguments: dev - device number 10898 * uio - structure pointer describing where data is to be stored 10899 * in user's space 10900 * cred_p - user credential pointer 10901 * 10902 * Return Code: ENXIO 10903 * EIO 10904 * EINVAL 10905 * value returned by physio 10906 * 10907 * Context: Kernel thread context. 10908 */ 10909 /* ARGSUSED */ 10910 static int 10911 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10912 { 10913 struct sd_lun *un = NULL; 10914 int secmask; 10915 int err = 0; 10916 sd_ssc_t *ssc; 10917 10918 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10919 return (ENXIO); 10920 } 10921 10922 ASSERT(!mutex_owned(SD_MUTEX(un))); 10923 10924 10925 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10926 mutex_enter(SD_MUTEX(un)); 10927 /* 10928 * Because the call to sd_ready_and_valid will issue I/O we 10929 * must wait here if either the device is suspended or 10930 * if it's power level is changing. 10931 */ 10932 while ((un->un_state == SD_STATE_SUSPENDED) || 10933 (un->un_state == SD_STATE_PM_CHANGING)) { 10934 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10935 } 10936 un->un_ncmds_in_driver++; 10937 mutex_exit(SD_MUTEX(un)); 10938 10939 /* Initialize sd_ssc_t for internal uscsi commands */ 10940 ssc = sd_ssc_init(un); 10941 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10942 err = EIO; 10943 } else { 10944 err = 0; 10945 } 10946 sd_ssc_fini(ssc); 10947 10948 mutex_enter(SD_MUTEX(un)); 10949 un->un_ncmds_in_driver--; 10950 ASSERT(un->un_ncmds_in_driver >= 0); 10951 mutex_exit(SD_MUTEX(un)); 10952 if (err != 0) 10953 return (err); 10954 } 10955 10956 /* 10957 * Read requests are restricted to multiples of the system block size. 10958 */ 10959 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10960 !un->un_f_enable_rmw) 10961 secmask = un->un_tgt_blocksize - 1; 10962 else 10963 secmask = DEV_BSIZE - 1; 10964 10965 if (uio->uio_loffset & ((offset_t)(secmask))) { 10966 SD_ERROR(SD_LOG_READ_WRITE, un, 10967 "sdread: file offset not modulo %d\n", 10968 secmask + 1); 10969 err = EINVAL; 10970 } else if (uio->uio_iov->iov_len & (secmask)) { 10971 SD_ERROR(SD_LOG_READ_WRITE, un, 10972 "sdread: transfer length not modulo %d\n", 10973 secmask + 1); 10974 err = EINVAL; 10975 } else { 10976 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10977 } 10978 10979 return (err); 10980 } 10981 10982 10983 /* 10984 * Function: sdwrite 10985 * 10986 * Description: Driver's write(9e) entry point function. 10987 * 10988 * Arguments: dev - device number 10989 * uio - structure pointer describing where data is stored in 10990 * user's space 10991 * cred_p - user credential pointer 10992 * 10993 * Return Code: ENXIO 10994 * EIO 10995 * EINVAL 10996 * value returned by physio 10997 * 10998 * Context: Kernel thread context. 10999 */ 11000 /* ARGSUSED */ 11001 static int 11002 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11003 { 11004 struct sd_lun *un = NULL; 11005 int secmask; 11006 int err = 0; 11007 sd_ssc_t *ssc; 11008 11009 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11010 return (ENXIO); 11011 } 11012 11013 ASSERT(!mutex_owned(SD_MUTEX(un))); 11014 11015 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11016 mutex_enter(SD_MUTEX(un)); 11017 /* 11018 * Because the call to sd_ready_and_valid will issue I/O we 11019 * must wait here if either the device is suspended or 11020 * if it's power level is changing. 11021 */ 11022 while ((un->un_state == SD_STATE_SUSPENDED) || 11023 (un->un_state == SD_STATE_PM_CHANGING)) { 11024 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11025 } 11026 un->un_ncmds_in_driver++; 11027 mutex_exit(SD_MUTEX(un)); 11028 11029 /* Initialize sd_ssc_t for internal uscsi commands */ 11030 ssc = sd_ssc_init(un); 11031 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11032 err = EIO; 11033 } else { 11034 err = 0; 11035 } 11036 sd_ssc_fini(ssc); 11037 11038 mutex_enter(SD_MUTEX(un)); 11039 un->un_ncmds_in_driver--; 11040 ASSERT(un->un_ncmds_in_driver >= 0); 11041 mutex_exit(SD_MUTEX(un)); 11042 if (err != 0) 11043 return (err); 11044 } 11045 11046 /* 11047 * Write requests are restricted to multiples of the system block size. 11048 */ 11049 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11050 !un->un_f_enable_rmw) 11051 secmask = un->un_tgt_blocksize - 1; 11052 else 11053 secmask = DEV_BSIZE - 1; 11054 11055 if (uio->uio_loffset & ((offset_t)(secmask))) { 11056 SD_ERROR(SD_LOG_READ_WRITE, un, 11057 "sdwrite: file offset not modulo %d\n", 11058 secmask + 1); 11059 err = EINVAL; 11060 } else if (uio->uio_iov->iov_len & (secmask)) { 11061 SD_ERROR(SD_LOG_READ_WRITE, un, 11062 "sdwrite: transfer length not modulo %d\n", 11063 secmask + 1); 11064 err = EINVAL; 11065 } else { 11066 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11067 } 11068 11069 return (err); 11070 } 11071 11072 11073 /* 11074 * Function: sdaread 11075 * 11076 * Description: Driver's aread(9e) entry point function. 11077 * 11078 * Arguments: dev - device number 11079 * aio - structure pointer describing where data is to be stored 11080 * cred_p - user credential pointer 11081 * 11082 * Return Code: ENXIO 11083 * EIO 11084 * EINVAL 11085 * value returned by aphysio 11086 * 11087 * Context: Kernel thread context. 11088 */ 11089 /* ARGSUSED */ 11090 static int 11091 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11092 { 11093 struct sd_lun *un = NULL; 11094 struct uio *uio = aio->aio_uio; 11095 int secmask; 11096 int err = 0; 11097 sd_ssc_t *ssc; 11098 11099 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11100 return (ENXIO); 11101 } 11102 11103 ASSERT(!mutex_owned(SD_MUTEX(un))); 11104 11105 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11106 mutex_enter(SD_MUTEX(un)); 11107 /* 11108 * Because the call to sd_ready_and_valid will issue I/O we 11109 * must wait here if either the device is suspended or 11110 * if it's power level is changing. 11111 */ 11112 while ((un->un_state == SD_STATE_SUSPENDED) || 11113 (un->un_state == SD_STATE_PM_CHANGING)) { 11114 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11115 } 11116 un->un_ncmds_in_driver++; 11117 mutex_exit(SD_MUTEX(un)); 11118 11119 /* Initialize sd_ssc_t for internal uscsi commands */ 11120 ssc = sd_ssc_init(un); 11121 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11122 err = EIO; 11123 } else { 11124 err = 0; 11125 } 11126 sd_ssc_fini(ssc); 11127 11128 mutex_enter(SD_MUTEX(un)); 11129 un->un_ncmds_in_driver--; 11130 ASSERT(un->un_ncmds_in_driver >= 0); 11131 mutex_exit(SD_MUTEX(un)); 11132 if (err != 0) 11133 return (err); 11134 } 11135 11136 /* 11137 * Read requests are restricted to multiples of the system block size. 11138 */ 11139 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11140 !un->un_f_enable_rmw) 11141 secmask = un->un_tgt_blocksize - 1; 11142 else 11143 secmask = DEV_BSIZE - 1; 11144 11145 if (uio->uio_loffset & ((offset_t)(secmask))) { 11146 SD_ERROR(SD_LOG_READ_WRITE, un, 11147 "sdaread: file offset not modulo %d\n", 11148 secmask + 1); 11149 err = EINVAL; 11150 } else if (uio->uio_iov->iov_len & (secmask)) { 11151 SD_ERROR(SD_LOG_READ_WRITE, un, 11152 "sdaread: transfer length not modulo %d\n", 11153 secmask + 1); 11154 err = EINVAL; 11155 } else { 11156 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11157 } 11158 11159 return (err); 11160 } 11161 11162 11163 /* 11164 * Function: sdawrite 11165 * 11166 * Description: Driver's awrite(9e) entry point function. 11167 * 11168 * Arguments: dev - device number 11169 * aio - structure pointer describing where data is stored 11170 * cred_p - user credential pointer 11171 * 11172 * Return Code: ENXIO 11173 * EIO 11174 * EINVAL 11175 * value returned by aphysio 11176 * 11177 * Context: Kernel thread context. 11178 */ 11179 /* ARGSUSED */ 11180 static int 11181 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11182 { 11183 struct sd_lun *un = NULL; 11184 struct uio *uio = aio->aio_uio; 11185 int secmask; 11186 int err = 0; 11187 sd_ssc_t *ssc; 11188 11189 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11190 return (ENXIO); 11191 } 11192 11193 ASSERT(!mutex_owned(SD_MUTEX(un))); 11194 11195 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11196 mutex_enter(SD_MUTEX(un)); 11197 /* 11198 * Because the call to sd_ready_and_valid will issue I/O we 11199 * must wait here if either the device is suspended or 11200 * if it's power level is changing. 11201 */ 11202 while ((un->un_state == SD_STATE_SUSPENDED) || 11203 (un->un_state == SD_STATE_PM_CHANGING)) { 11204 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11205 } 11206 un->un_ncmds_in_driver++; 11207 mutex_exit(SD_MUTEX(un)); 11208 11209 /* Initialize sd_ssc_t for internal uscsi commands */ 11210 ssc = sd_ssc_init(un); 11211 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11212 err = EIO; 11213 } else { 11214 err = 0; 11215 } 11216 sd_ssc_fini(ssc); 11217 11218 mutex_enter(SD_MUTEX(un)); 11219 un->un_ncmds_in_driver--; 11220 ASSERT(un->un_ncmds_in_driver >= 0); 11221 mutex_exit(SD_MUTEX(un)); 11222 if (err != 0) 11223 return (err); 11224 } 11225 11226 /* 11227 * Write requests are restricted to multiples of the system block size. 11228 */ 11229 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11230 !un->un_f_enable_rmw) 11231 secmask = un->un_tgt_blocksize - 1; 11232 else 11233 secmask = DEV_BSIZE - 1; 11234 11235 if (uio->uio_loffset & ((offset_t)(secmask))) { 11236 SD_ERROR(SD_LOG_READ_WRITE, un, 11237 "sdawrite: file offset not modulo %d\n", 11238 secmask + 1); 11239 err = EINVAL; 11240 } else if (uio->uio_iov->iov_len & (secmask)) { 11241 SD_ERROR(SD_LOG_READ_WRITE, un, 11242 "sdawrite: transfer length not modulo %d\n", 11243 secmask + 1); 11244 err = EINVAL; 11245 } else { 11246 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11247 } 11248 11249 return (err); 11250 } 11251 11252 11253 11254 11255 11256 /* 11257 * Driver IO processing follows the following sequence: 11258 * 11259 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11260 * | | ^ 11261 * v v | 11262 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11263 * | | | | 11264 * v | | | 11265 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11266 * | | ^ ^ 11267 * v v | | 11268 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11269 * | | | | 11270 * +---+ | +------------+ +-------+ 11271 * | | | | 11272 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11273 * | v | | 11274 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11275 * | | ^ | 11276 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11277 * | v | | 11278 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11279 * | | ^ | 11280 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11281 * | v | | 11282 * | sd_checksum_iostart() sd_checksum_iodone() | 11283 * | | ^ | 11284 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11285 * | v | | 11286 * | sd_pm_iostart() sd_pm_iodone() | 11287 * | | ^ | 11288 * | | | | 11289 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11290 * | ^ 11291 * v | 11292 * sd_core_iostart() | 11293 * | | 11294 * | +------>(*destroypkt)() 11295 * +-> sd_start_cmds() <-+ | | 11296 * | | | v 11297 * | | | scsi_destroy_pkt(9F) 11298 * | | | 11299 * +->(*initpkt)() +- sdintr() 11300 * | | | | 11301 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11302 * | +-> scsi_setup_cdb(9F) | 11303 * | | 11304 * +--> scsi_transport(9F) | 11305 * | | 11306 * +----> SCSA ---->+ 11307 * 11308 * 11309 * This code is based upon the following presumptions: 11310 * 11311 * - iostart and iodone functions operate on buf(9S) structures. These 11312 * functions perform the necessary operations on the buf(9S) and pass 11313 * them along to the next function in the chain by using the macros 11314 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11315 * (for iodone side functions). 11316 * 11317 * - The iostart side functions may sleep. The iodone side functions 11318 * are called under interrupt context and may NOT sleep. Therefore 11319 * iodone side functions also may not call iostart side functions. 11320 * (NOTE: iostart side functions should NOT sleep for memory, as 11321 * this could result in deadlock.) 11322 * 11323 * - An iostart side function may call its corresponding iodone side 11324 * function directly (if necessary). 11325 * 11326 * - In the event of an error, an iostart side function can return a buf(9S) 11327 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11328 * b_error in the usual way of course). 11329 * 11330 * - The taskq mechanism may be used by the iodone side functions to dispatch 11331 * requests to the iostart side functions. The iostart side functions in 11332 * this case would be called under the context of a taskq thread, so it's 11333 * OK for them to block/sleep/spin in this case. 11334 * 11335 * - iostart side functions may allocate "shadow" buf(9S) structs and 11336 * pass them along to the next function in the chain. The corresponding 11337 * iodone side functions must coalesce the "shadow" bufs and return 11338 * the "original" buf to the next higher layer. 11339 * 11340 * - The b_private field of the buf(9S) struct holds a pointer to 11341 * an sd_xbuf struct, which contains information needed to 11342 * construct the scsi_pkt for the command. 11343 * 11344 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11345 * layer must acquire & release the SD_MUTEX(un) as needed. 11346 */ 11347 11348 11349 /* 11350 * Create taskq for all targets in the system. This is created at 11351 * _init(9E) and destroyed at _fini(9E). 11352 * 11353 * Note: here we set the minalloc to a reasonably high number to ensure that 11354 * we will have an adequate supply of task entries available at interrupt time. 11355 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11356 * sd_create_taskq(). Since we do not want to sleep for allocations at 11357 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11358 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11359 * requests any one instant in time. 11360 */ 11361 #define SD_TASKQ_NUMTHREADS 8 11362 #define SD_TASKQ_MINALLOC 256 11363 #define SD_TASKQ_MAXALLOC 256 11364 11365 static taskq_t *sd_tq = NULL; 11366 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11367 11368 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11369 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11370 11371 /* 11372 * The following task queue is being created for the write part of 11373 * read-modify-write of non-512 block size devices. 11374 * Limit the number of threads to 1 for now. This number has been chosen 11375 * considering the fact that it applies only to dvd ram drives/MO drives 11376 * currently. Performance for which is not main criteria at this stage. 11377 * Note: It needs to be explored if we can use a single taskq in future 11378 */ 11379 #define SD_WMR_TASKQ_NUMTHREADS 1 11380 static taskq_t *sd_wmr_tq = NULL; 11381 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11382 11383 /* 11384 * Function: sd_taskq_create 11385 * 11386 * Description: Create taskq thread(s) and preallocate task entries 11387 * 11388 * Return Code: Returns a pointer to the allocated taskq_t. 11389 * 11390 * Context: Can sleep. Requires blockable context. 11391 * 11392 * Notes: - The taskq() facility currently is NOT part of the DDI. 11393 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11394 * - taskq_create() will block for memory, also it will panic 11395 * if it cannot create the requested number of threads. 11396 * - Currently taskq_create() creates threads that cannot be 11397 * swapped. 11398 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11399 * supply of taskq entries at interrupt time (ie, so that we 11400 * do not have to sleep for memory) 11401 */ 11402 11403 static void 11404 sd_taskq_create(void) 11405 { 11406 char taskq_name[TASKQ_NAMELEN]; 11407 11408 ASSERT(sd_tq == NULL); 11409 ASSERT(sd_wmr_tq == NULL); 11410 11411 (void) snprintf(taskq_name, sizeof (taskq_name), 11412 "%s_drv_taskq", sd_label); 11413 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11414 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11415 TASKQ_PREPOPULATE)); 11416 11417 (void) snprintf(taskq_name, sizeof (taskq_name), 11418 "%s_rmw_taskq", sd_label); 11419 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11420 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11421 TASKQ_PREPOPULATE)); 11422 } 11423 11424 11425 /* 11426 * Function: sd_taskq_delete 11427 * 11428 * Description: Complementary cleanup routine for sd_taskq_create(). 11429 * 11430 * Context: Kernel thread context. 11431 */ 11432 11433 static void 11434 sd_taskq_delete(void) 11435 { 11436 ASSERT(sd_tq != NULL); 11437 ASSERT(sd_wmr_tq != NULL); 11438 taskq_destroy(sd_tq); 11439 taskq_destroy(sd_wmr_tq); 11440 sd_tq = NULL; 11441 sd_wmr_tq = NULL; 11442 } 11443 11444 11445 /* 11446 * Function: sdstrategy 11447 * 11448 * Description: Driver's strategy (9E) entry point function. 11449 * 11450 * Arguments: bp - pointer to buf(9S) 11451 * 11452 * Return Code: Always returns zero 11453 * 11454 * Context: Kernel thread context. 11455 */ 11456 11457 static int 11458 sdstrategy(struct buf *bp) 11459 { 11460 struct sd_lun *un; 11461 11462 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11463 if (un == NULL) { 11464 bioerror(bp, EIO); 11465 bp->b_resid = bp->b_bcount; 11466 biodone(bp); 11467 return (0); 11468 } 11469 11470 /* As was done in the past, fail new cmds. if state is dumping. */ 11471 if (un->un_state == SD_STATE_DUMPING) { 11472 bioerror(bp, ENXIO); 11473 bp->b_resid = bp->b_bcount; 11474 biodone(bp); 11475 return (0); 11476 } 11477 11478 ASSERT(!mutex_owned(SD_MUTEX(un))); 11479 11480 /* 11481 * Commands may sneak in while we released the mutex in 11482 * DDI_SUSPEND, we should block new commands. However, old 11483 * commands that are still in the driver at this point should 11484 * still be allowed to drain. 11485 */ 11486 mutex_enter(SD_MUTEX(un)); 11487 /* 11488 * Must wait here if either the device is suspended or 11489 * if it's power level is changing. 11490 */ 11491 while ((un->un_state == SD_STATE_SUSPENDED) || 11492 (un->un_state == SD_STATE_PM_CHANGING)) { 11493 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11494 } 11495 11496 un->un_ncmds_in_driver++; 11497 11498 /* 11499 * atapi: Since we are running the CD for now in PIO mode we need to 11500 * call bp_mapin here to avoid bp_mapin called interrupt context under 11501 * the HBA's init_pkt routine. 11502 */ 11503 if (un->un_f_cfg_is_atapi == TRUE) { 11504 mutex_exit(SD_MUTEX(un)); 11505 bp_mapin(bp); 11506 mutex_enter(SD_MUTEX(un)); 11507 } 11508 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11509 un->un_ncmds_in_driver); 11510 11511 if (bp->b_flags & B_WRITE) 11512 un->un_f_sync_cache_required = TRUE; 11513 11514 mutex_exit(SD_MUTEX(un)); 11515 11516 /* 11517 * This will (eventually) allocate the sd_xbuf area and 11518 * call sd_xbuf_strategy(). We just want to return the 11519 * result of ddi_xbuf_qstrategy so that we have an opt- 11520 * imized tail call which saves us a stack frame. 11521 */ 11522 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11523 } 11524 11525 11526 /* 11527 * Function: sd_xbuf_strategy 11528 * 11529 * Description: Function for initiating IO operations via the 11530 * ddi_xbuf_qstrategy() mechanism. 11531 * 11532 * Context: Kernel thread context. 11533 */ 11534 11535 static void 11536 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11537 { 11538 struct sd_lun *un = arg; 11539 11540 ASSERT(bp != NULL); 11541 ASSERT(xp != NULL); 11542 ASSERT(un != NULL); 11543 ASSERT(!mutex_owned(SD_MUTEX(un))); 11544 11545 /* 11546 * Initialize the fields in the xbuf and save a pointer to the 11547 * xbuf in bp->b_private. 11548 */ 11549 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11550 11551 /* Send the buf down the iostart chain */ 11552 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11553 } 11554 11555 11556 /* 11557 * Function: sd_xbuf_init 11558 * 11559 * Description: Prepare the given sd_xbuf struct for use. 11560 * 11561 * Arguments: un - ptr to softstate 11562 * bp - ptr to associated buf(9S) 11563 * xp - ptr to associated sd_xbuf 11564 * chain_type - IO chain type to use: 11565 * SD_CHAIN_NULL 11566 * SD_CHAIN_BUFIO 11567 * SD_CHAIN_USCSI 11568 * SD_CHAIN_DIRECT 11569 * SD_CHAIN_DIRECT_PRIORITY 11570 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11571 * initialization; may be NULL if none. 11572 * 11573 * Context: Kernel thread context 11574 */ 11575 11576 static void 11577 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11578 uchar_t chain_type, void *pktinfop) 11579 { 11580 int index; 11581 11582 ASSERT(un != NULL); 11583 ASSERT(bp != NULL); 11584 ASSERT(xp != NULL); 11585 11586 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11587 bp, chain_type); 11588 11589 xp->xb_un = un; 11590 xp->xb_pktp = NULL; 11591 xp->xb_pktinfo = pktinfop; 11592 xp->xb_private = bp->b_private; 11593 xp->xb_blkno = (daddr_t)bp->b_blkno; 11594 11595 /* 11596 * Set up the iostart and iodone chain indexes in the xbuf, based 11597 * upon the specified chain type to use. 11598 */ 11599 switch (chain_type) { 11600 case SD_CHAIN_NULL: 11601 /* 11602 * Fall thru to just use the values for the buf type, even 11603 * tho for the NULL chain these values will never be used. 11604 */ 11605 /* FALLTHRU */ 11606 case SD_CHAIN_BUFIO: 11607 index = un->un_buf_chain_type; 11608 if ((!un->un_f_has_removable_media) && 11609 (un->un_tgt_blocksize != 0) && 11610 (un->un_tgt_blocksize != DEV_BSIZE || 11611 un->un_f_enable_rmw)) { 11612 int secmask = 0, blknomask = 0; 11613 if (un->un_f_enable_rmw) { 11614 blknomask = 11615 (un->un_phy_blocksize / DEV_BSIZE) - 1; 11616 secmask = un->un_phy_blocksize - 1; 11617 } else { 11618 blknomask = 11619 (un->un_tgt_blocksize / DEV_BSIZE) - 1; 11620 secmask = un->un_tgt_blocksize - 1; 11621 } 11622 11623 if ((bp->b_lblkno & (blknomask)) || 11624 (bp->b_bcount & (secmask))) { 11625 if ((un->un_f_rmw_type != 11626 SD_RMW_TYPE_RETURN_ERROR) || 11627 un->un_f_enable_rmw) { 11628 if (un->un_f_pm_is_enabled == FALSE) 11629 index = 11630 SD_CHAIN_INFO_MSS_DSK_NO_PM; 11631 else 11632 index = 11633 SD_CHAIN_INFO_MSS_DISK; 11634 } 11635 } 11636 } 11637 break; 11638 case SD_CHAIN_USCSI: 11639 index = un->un_uscsi_chain_type; 11640 break; 11641 case SD_CHAIN_DIRECT: 11642 index = un->un_direct_chain_type; 11643 break; 11644 case SD_CHAIN_DIRECT_PRIORITY: 11645 index = un->un_priority_chain_type; 11646 break; 11647 default: 11648 /* We're really broken if we ever get here... */ 11649 panic("sd_xbuf_init: illegal chain type!"); 11650 /*NOTREACHED*/ 11651 } 11652 11653 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11654 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11655 11656 /* 11657 * It might be a bit easier to simply bzero the entire xbuf above, 11658 * but it turns out that since we init a fair number of members anyway, 11659 * we save a fair number cycles by doing explicit assignment of zero. 11660 */ 11661 xp->xb_pkt_flags = 0; 11662 xp->xb_dma_resid = 0; 11663 xp->xb_retry_count = 0; 11664 xp->xb_victim_retry_count = 0; 11665 xp->xb_ua_retry_count = 0; 11666 xp->xb_nr_retry_count = 0; 11667 xp->xb_sense_bp = NULL; 11668 xp->xb_sense_status = 0; 11669 xp->xb_sense_state = 0; 11670 xp->xb_sense_resid = 0; 11671 xp->xb_ena = 0; 11672 11673 bp->b_private = xp; 11674 bp->b_flags &= ~(B_DONE | B_ERROR); 11675 bp->b_resid = 0; 11676 bp->av_forw = NULL; 11677 bp->av_back = NULL; 11678 bioerror(bp, 0); 11679 11680 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11681 } 11682 11683 11684 /* 11685 * Function: sd_uscsi_strategy 11686 * 11687 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11688 * 11689 * Arguments: bp - buf struct ptr 11690 * 11691 * Return Code: Always returns 0 11692 * 11693 * Context: Kernel thread context 11694 */ 11695 11696 static int 11697 sd_uscsi_strategy(struct buf *bp) 11698 { 11699 struct sd_lun *un; 11700 struct sd_uscsi_info *uip; 11701 struct sd_xbuf *xp; 11702 uchar_t chain_type; 11703 uchar_t cmd; 11704 11705 ASSERT(bp != NULL); 11706 11707 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11708 if (un == NULL) { 11709 bioerror(bp, EIO); 11710 bp->b_resid = bp->b_bcount; 11711 biodone(bp); 11712 return (0); 11713 } 11714 11715 ASSERT(!mutex_owned(SD_MUTEX(un))); 11716 11717 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11718 11719 /* 11720 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11721 */ 11722 ASSERT(bp->b_private != NULL); 11723 uip = (struct sd_uscsi_info *)bp->b_private; 11724 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0]; 11725 11726 mutex_enter(SD_MUTEX(un)); 11727 /* 11728 * atapi: Since we are running the CD for now in PIO mode we need to 11729 * call bp_mapin here to avoid bp_mapin called interrupt context under 11730 * the HBA's init_pkt routine. 11731 */ 11732 if (un->un_f_cfg_is_atapi == TRUE) { 11733 mutex_exit(SD_MUTEX(un)); 11734 bp_mapin(bp); 11735 mutex_enter(SD_MUTEX(un)); 11736 } 11737 un->un_ncmds_in_driver++; 11738 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11739 un->un_ncmds_in_driver); 11740 11741 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) && 11742 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1)) 11743 un->un_f_sync_cache_required = TRUE; 11744 11745 mutex_exit(SD_MUTEX(un)); 11746 11747 switch (uip->ui_flags) { 11748 case SD_PATH_DIRECT: 11749 chain_type = SD_CHAIN_DIRECT; 11750 break; 11751 case SD_PATH_DIRECT_PRIORITY: 11752 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11753 break; 11754 default: 11755 chain_type = SD_CHAIN_USCSI; 11756 break; 11757 } 11758 11759 /* 11760 * We may allocate extra buf for external USCSI commands. If the 11761 * application asks for bigger than 20-byte sense data via USCSI, 11762 * SCSA layer will allocate 252 bytes sense buf for that command. 11763 */ 11764 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 11765 SENSE_LENGTH) { 11766 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 11767 MAX_SENSE_LENGTH, KM_SLEEP); 11768 } else { 11769 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 11770 } 11771 11772 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11773 11774 /* Use the index obtained within xbuf_init */ 11775 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11776 11777 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11778 11779 return (0); 11780 } 11781 11782 /* 11783 * Function: sd_send_scsi_cmd 11784 * 11785 * Description: Runs a USCSI command for user (when called thru sdioctl), 11786 * or for the driver 11787 * 11788 * Arguments: dev - the dev_t for the device 11789 * incmd - ptr to a valid uscsi_cmd struct 11790 * flag - bit flag, indicating open settings, 32/64 bit type 11791 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11792 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11793 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11794 * to use the USCSI "direct" chain and bypass the normal 11795 * command waitq. 11796 * 11797 * Return Code: 0 - successful completion of the given command 11798 * EIO - scsi_uscsi_handle_command() failed 11799 * ENXIO - soft state not found for specified dev 11800 * EINVAL 11801 * EFAULT - copyin/copyout error 11802 * return code of scsi_uscsi_handle_command(): 11803 * EIO 11804 * ENXIO 11805 * EACCES 11806 * 11807 * Context: Waits for command to complete. Can sleep. 11808 */ 11809 11810 static int 11811 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 11812 enum uio_seg dataspace, int path_flag) 11813 { 11814 struct sd_lun *un; 11815 sd_ssc_t *ssc; 11816 int rval; 11817 11818 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11819 if (un == NULL) { 11820 return (ENXIO); 11821 } 11822 11823 /* 11824 * Using sd_ssc_send to handle uscsi cmd 11825 */ 11826 ssc = sd_ssc_init(un); 11827 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag); 11828 sd_ssc_fini(ssc); 11829 11830 return (rval); 11831 } 11832 11833 /* 11834 * Function: sd_ssc_init 11835 * 11836 * Description: Uscsi end-user call this function to initialize necessary 11837 * fields, such as uscsi_cmd and sd_uscsi_info struct. 11838 * 11839 * The return value of sd_send_scsi_cmd will be treated as a 11840 * fault in various conditions. Even it is not Zero, some 11841 * callers may ignore the return value. That is to say, we can 11842 * not make an accurate assessment in sdintr, since if a 11843 * command is failed in sdintr it does not mean the caller of 11844 * sd_send_scsi_cmd will treat it as a real failure. 11845 * 11846 * To avoid printing too many error logs for a failed uscsi 11847 * packet that the caller may not treat it as a failure, the 11848 * sd will keep silent for handling all uscsi commands. 11849 * 11850 * During detach->attach and attach-open, for some types of 11851 * problems, the driver should be providing information about 11852 * the problem encountered. Device use USCSI_SILENT, which 11853 * suppresses all driver information. The result is that no 11854 * information about the problem is available. Being 11855 * completely silent during this time is inappropriate. The 11856 * driver needs a more selective filter than USCSI_SILENT, so 11857 * that information related to faults is provided. 11858 * 11859 * To make the accurate accessment, the caller of 11860 * sd_send_scsi_USCSI_CMD should take the ownership and 11861 * get necessary information to print error messages. 11862 * 11863 * If we want to print necessary info of uscsi command, we need to 11864 * keep the uscsi_cmd and sd_uscsi_info till we can make the 11865 * assessment. We use sd_ssc_init to alloc necessary 11866 * structs for sending an uscsi command and we are also 11867 * responsible for free the memory by calling 11868 * sd_ssc_fini. 11869 * 11870 * The calling secquences will look like: 11871 * sd_ssc_init-> 11872 * 11873 * ... 11874 * 11875 * sd_send_scsi_USCSI_CMD-> 11876 * sd_ssc_send-> - - - sdintr 11877 * ... 11878 * 11879 * if we think the return value should be treated as a 11880 * failure, we make the accessment here and print out 11881 * necessary by retrieving uscsi_cmd and sd_uscsi_info' 11882 * 11883 * ... 11884 * 11885 * sd_ssc_fini 11886 * 11887 * 11888 * Arguments: un - pointer to driver soft state (unit) structure for this 11889 * target. 11890 * 11891 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains 11892 * uscsi_cmd and sd_uscsi_info. 11893 * NULL - if can not alloc memory for sd_ssc_t struct 11894 * 11895 * Context: Kernel Thread. 11896 */ 11897 static sd_ssc_t * 11898 sd_ssc_init(struct sd_lun *un) 11899 { 11900 sd_ssc_t *ssc; 11901 struct uscsi_cmd *ucmdp; 11902 struct sd_uscsi_info *uip; 11903 11904 ASSERT(un != NULL); 11905 ASSERT(!mutex_owned(SD_MUTEX(un))); 11906 11907 /* 11908 * Allocate sd_ssc_t structure 11909 */ 11910 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP); 11911 11912 /* 11913 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine 11914 */ 11915 ucmdp = scsi_uscsi_alloc(); 11916 11917 /* 11918 * Allocate sd_uscsi_info structure 11919 */ 11920 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11921 11922 ssc->ssc_uscsi_cmd = ucmdp; 11923 ssc->ssc_uscsi_info = uip; 11924 ssc->ssc_un = un; 11925 11926 return (ssc); 11927 } 11928 11929 /* 11930 * Function: sd_ssc_fini 11931 * 11932 * Description: To free sd_ssc_t and it's hanging off 11933 * 11934 * Arguments: ssc - struct pointer of sd_ssc_t. 11935 */ 11936 static void 11937 sd_ssc_fini(sd_ssc_t *ssc) 11938 { 11939 scsi_uscsi_free(ssc->ssc_uscsi_cmd); 11940 11941 if (ssc->ssc_uscsi_info != NULL) { 11942 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info)); 11943 ssc->ssc_uscsi_info = NULL; 11944 } 11945 11946 kmem_free(ssc, sizeof (sd_ssc_t)); 11947 ssc = NULL; 11948 } 11949 11950 /* 11951 * Function: sd_ssc_send 11952 * 11953 * Description: Runs a USCSI command for user when called through sdioctl, 11954 * or for the driver. 11955 * 11956 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11957 * sd_uscsi_info in. 11958 * incmd - ptr to a valid uscsi_cmd struct 11959 * flag - bit flag, indicating open settings, 32/64 bit type 11960 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11961 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11962 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11963 * to use the USCSI "direct" chain and bypass the normal 11964 * command waitq. 11965 * 11966 * Return Code: 0 - successful completion of the given command 11967 * EIO - scsi_uscsi_handle_command() failed 11968 * ENXIO - soft state not found for specified dev 11969 * ECANCELED - command cancelled due to low power 11970 * EINVAL 11971 * EFAULT - copyin/copyout error 11972 * return code of scsi_uscsi_handle_command(): 11973 * EIO 11974 * ENXIO 11975 * EACCES 11976 * 11977 * Context: Kernel Thread; 11978 * Waits for command to complete. Can sleep. 11979 */ 11980 static int 11981 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag, 11982 enum uio_seg dataspace, int path_flag) 11983 { 11984 struct sd_uscsi_info *uip; 11985 struct uscsi_cmd *uscmd; 11986 struct sd_lun *un; 11987 dev_t dev; 11988 11989 int format = 0; 11990 int rval; 11991 11992 ASSERT(ssc != NULL); 11993 un = ssc->ssc_un; 11994 ASSERT(un != NULL); 11995 uscmd = ssc->ssc_uscsi_cmd; 11996 ASSERT(uscmd != NULL); 11997 ASSERT(!mutex_owned(SD_MUTEX(un))); 11998 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11999 /* 12000 * If enter here, it indicates that the previous uscsi 12001 * command has not been processed by sd_ssc_assessment. 12002 * This is violating our rules of FMA telemetry processing. 12003 * We should print out this message and the last undisposed 12004 * uscsi command. 12005 */ 12006 if (uscmd->uscsi_cdb != NULL) { 12007 SD_INFO(SD_LOG_SDTEST, un, 12008 "sd_ssc_send is missing the alternative " 12009 "sd_ssc_assessment when running command 0x%x.\n", 12010 uscmd->uscsi_cdb[0]); 12011 } 12012 /* 12013 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be 12014 * the initial status. 12015 */ 12016 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12017 } 12018 12019 /* 12020 * We need to make sure sd_ssc_send will have sd_ssc_assessment 12021 * followed to avoid missing FMA telemetries. 12022 */ 12023 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT; 12024 12025 /* 12026 * if USCSI_PMFAILFAST is set and un is in low power, fail the 12027 * command immediately. 12028 */ 12029 mutex_enter(SD_MUTEX(un)); 12030 mutex_enter(&un->un_pm_mutex); 12031 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) && 12032 SD_DEVICE_IS_IN_LOW_POWER(un)) { 12033 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:" 12034 "un:0x%p is in low power\n", un); 12035 mutex_exit(&un->un_pm_mutex); 12036 mutex_exit(SD_MUTEX(un)); 12037 return (ECANCELED); 12038 } 12039 mutex_exit(&un->un_pm_mutex); 12040 mutex_exit(SD_MUTEX(un)); 12041 12042 #ifdef SDDEBUG 12043 switch (dataspace) { 12044 case UIO_USERSPACE: 12045 SD_TRACE(SD_LOG_IO, un, 12046 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un); 12047 break; 12048 case UIO_SYSSPACE: 12049 SD_TRACE(SD_LOG_IO, un, 12050 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un); 12051 break; 12052 default: 12053 SD_TRACE(SD_LOG_IO, un, 12054 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un); 12055 break; 12056 } 12057 #endif 12058 12059 rval = scsi_uscsi_copyin((intptr_t)incmd, flag, 12060 SD_ADDRESS(un), &uscmd); 12061 if (rval != 0) { 12062 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 12063 "scsi_uscsi_alloc_and_copyin failed\n", un); 12064 return (rval); 12065 } 12066 12067 if ((uscmd->uscsi_cdb != NULL) && 12068 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 12069 mutex_enter(SD_MUTEX(un)); 12070 un->un_f_format_in_progress = TRUE; 12071 mutex_exit(SD_MUTEX(un)); 12072 format = 1; 12073 } 12074 12075 /* 12076 * Allocate an sd_uscsi_info struct and fill it with the info 12077 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12078 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12079 * since we allocate the buf here in this function, we do not 12080 * need to preserve the prior contents of b_private. 12081 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12082 */ 12083 uip = ssc->ssc_uscsi_info; 12084 uip->ui_flags = path_flag; 12085 uip->ui_cmdp = uscmd; 12086 12087 /* 12088 * Commands sent with priority are intended for error recovery 12089 * situations, and do not have retries performed. 12090 */ 12091 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12092 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12093 } 12094 uscmd->uscsi_flags &= ~USCSI_NOINTR; 12095 12096 dev = SD_GET_DEV(un); 12097 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 12098 sd_uscsi_strategy, NULL, uip); 12099 12100 /* 12101 * mark ssc_flags right after handle_cmd to make sure 12102 * the uscsi has been sent 12103 */ 12104 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED; 12105 12106 #ifdef SDDEBUG 12107 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12108 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12109 uscmd->uscsi_status, uscmd->uscsi_resid); 12110 if (uscmd->uscsi_bufaddr != NULL) { 12111 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12112 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12113 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12114 if (dataspace == UIO_SYSSPACE) { 12115 SD_DUMP_MEMORY(un, SD_LOG_IO, 12116 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12117 uscmd->uscsi_buflen, SD_LOG_HEX); 12118 } 12119 } 12120 #endif 12121 12122 if (format == 1) { 12123 mutex_enter(SD_MUTEX(un)); 12124 un->un_f_format_in_progress = FALSE; 12125 mutex_exit(SD_MUTEX(un)); 12126 } 12127 12128 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd); 12129 12130 return (rval); 12131 } 12132 12133 /* 12134 * Function: sd_ssc_print 12135 * 12136 * Description: Print information available to the console. 12137 * 12138 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12139 * sd_uscsi_info in. 12140 * sd_severity - log level. 12141 * Context: Kernel thread or interrupt context. 12142 */ 12143 static void 12144 sd_ssc_print(sd_ssc_t *ssc, int sd_severity) 12145 { 12146 struct uscsi_cmd *ucmdp; 12147 struct scsi_device *devp; 12148 dev_info_t *devinfo; 12149 uchar_t *sensep; 12150 int senlen; 12151 union scsi_cdb *cdbp; 12152 uchar_t com; 12153 extern struct scsi_key_strings scsi_cmds[]; 12154 12155 ASSERT(ssc != NULL); 12156 ASSERT(ssc->ssc_un != NULL); 12157 12158 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT) 12159 return; 12160 ucmdp = ssc->ssc_uscsi_cmd; 12161 devp = SD_SCSI_DEVP(ssc->ssc_un); 12162 devinfo = SD_DEVINFO(ssc->ssc_un); 12163 ASSERT(ucmdp != NULL); 12164 ASSERT(devp != NULL); 12165 ASSERT(devinfo != NULL); 12166 sensep = (uint8_t *)ucmdp->uscsi_rqbuf; 12167 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid; 12168 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb; 12169 12170 /* In certain case (like DOORLOCK), the cdb could be NULL. */ 12171 if (cdbp == NULL) 12172 return; 12173 /* We don't print log if no sense data available. */ 12174 if (senlen == 0) 12175 sensep = NULL; 12176 com = cdbp->scc_cmd; 12177 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com, 12178 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL); 12179 } 12180 12181 /* 12182 * Function: sd_ssc_assessment 12183 * 12184 * Description: We use this function to make an assessment at the point 12185 * where SD driver may encounter a potential error. 12186 * 12187 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12188 * sd_uscsi_info in. 12189 * tp_assess - a hint of strategy for ereport posting. 12190 * Possible values of tp_assess include: 12191 * SD_FMT_IGNORE - we don't post any ereport because we're 12192 * sure that it is ok to ignore the underlying problems. 12193 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now 12194 * but it might be not correct to ignore the underlying hardware 12195 * error. 12196 * SD_FMT_STATUS_CHECK - we will post an ereport with the 12197 * payload driver-assessment of value "fail" or 12198 * "fatal"(depending on what information we have here). This 12199 * assessment value is usually set when SD driver think there 12200 * is a potential error occurred(Typically, when return value 12201 * of the SCSI command is EIO). 12202 * SD_FMT_STANDARD - we will post an ereport with the payload 12203 * driver-assessment of value "info". This assessment value is 12204 * set when the SCSI command returned successfully and with 12205 * sense data sent back. 12206 * 12207 * Context: Kernel thread. 12208 */ 12209 static void 12210 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess) 12211 { 12212 int senlen = 0; 12213 struct uscsi_cmd *ucmdp = NULL; 12214 struct sd_lun *un; 12215 12216 ASSERT(ssc != NULL); 12217 un = ssc->ssc_un; 12218 ASSERT(un != NULL); 12219 ucmdp = ssc->ssc_uscsi_cmd; 12220 ASSERT(ucmdp != NULL); 12221 12222 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 12223 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT; 12224 } else { 12225 /* 12226 * If enter here, it indicates that we have a wrong 12227 * calling sequence of sd_ssc_send and sd_ssc_assessment, 12228 * both of which should be called in a pair in case of 12229 * loss of FMA telemetries. 12230 */ 12231 if (ucmdp->uscsi_cdb != NULL) { 12232 SD_INFO(SD_LOG_SDTEST, un, 12233 "sd_ssc_assessment is missing the " 12234 "alternative sd_ssc_send when running 0x%x, " 12235 "or there are superfluous sd_ssc_assessment for " 12236 "the same sd_ssc_send.\n", 12237 ucmdp->uscsi_cdb[0]); 12238 } 12239 /* 12240 * Set the ssc_flags to the initial value to avoid passing 12241 * down dirty flags to the following sd_ssc_send function. 12242 */ 12243 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12244 return; 12245 } 12246 12247 /* 12248 * Only handle an issued command which is waiting for assessment. 12249 * A command which is not issued will not have 12250 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here. 12251 */ 12252 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) { 12253 sd_ssc_print(ssc, SCSI_ERR_INFO); 12254 return; 12255 } else { 12256 /* 12257 * For an issued command, we should clear this flag in 12258 * order to make the sd_ssc_t structure be used off 12259 * multiple uscsi commands. 12260 */ 12261 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED; 12262 } 12263 12264 /* 12265 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set) 12266 * commands here. And we should clear the ssc_flags before return. 12267 */ 12268 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) { 12269 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12270 return; 12271 } 12272 12273 switch (tp_assess) { 12274 case SD_FMT_IGNORE: 12275 case SD_FMT_IGNORE_COMPROMISE: 12276 break; 12277 case SD_FMT_STATUS_CHECK: 12278 /* 12279 * For a failed command(including the succeeded command 12280 * with invalid data sent back). 12281 */ 12282 sd_ssc_post(ssc, SD_FM_DRV_FATAL); 12283 break; 12284 case SD_FMT_STANDARD: 12285 /* 12286 * Always for the succeeded commands probably with sense 12287 * data sent back. 12288 * Limitation: 12289 * We can only handle a succeeded command with sense 12290 * data sent back when auto-request-sense is enabled. 12291 */ 12292 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen - 12293 ssc->ssc_uscsi_cmd->uscsi_rqresid; 12294 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) && 12295 (un->un_f_arq_enabled == TRUE) && 12296 senlen > 0 && 12297 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) { 12298 sd_ssc_post(ssc, SD_FM_DRV_NOTICE); 12299 } 12300 break; 12301 default: 12302 /* 12303 * Should not have other type of assessment. 12304 */ 12305 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 12306 "sd_ssc_assessment got wrong " 12307 "sd_type_assessment %d.\n", tp_assess); 12308 break; 12309 } 12310 /* 12311 * Clear up the ssc_flags before return. 12312 */ 12313 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12314 } 12315 12316 /* 12317 * Function: sd_ssc_post 12318 * 12319 * Description: 1. read the driver property to get fm-scsi-log flag. 12320 * 2. print log if fm_log_capable is non-zero. 12321 * 3. call sd_ssc_ereport_post to post ereport if possible. 12322 * 12323 * Context: May be called from kernel thread or interrupt context. 12324 */ 12325 static void 12326 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess) 12327 { 12328 struct sd_lun *un; 12329 int sd_severity; 12330 12331 ASSERT(ssc != NULL); 12332 un = ssc->ssc_un; 12333 ASSERT(un != NULL); 12334 12335 /* 12336 * We may enter here from sd_ssc_assessment(for USCSI command) or 12337 * by directly called from sdintr context. 12338 * We don't handle a non-disk drive(CD-ROM, removable media). 12339 * Clear the ssc_flags before return in case we've set 12340 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk 12341 * driver. 12342 */ 12343 if (ISCD(un) || un->un_f_has_removable_media) { 12344 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12345 return; 12346 } 12347 12348 switch (sd_assess) { 12349 case SD_FM_DRV_FATAL: 12350 sd_severity = SCSI_ERR_FATAL; 12351 break; 12352 case SD_FM_DRV_RECOVERY: 12353 sd_severity = SCSI_ERR_RECOVERED; 12354 break; 12355 case SD_FM_DRV_RETRY: 12356 sd_severity = SCSI_ERR_RETRYABLE; 12357 break; 12358 case SD_FM_DRV_NOTICE: 12359 sd_severity = SCSI_ERR_INFO; 12360 break; 12361 default: 12362 sd_severity = SCSI_ERR_UNKNOWN; 12363 } 12364 /* print log */ 12365 sd_ssc_print(ssc, sd_severity); 12366 12367 /* always post ereport */ 12368 sd_ssc_ereport_post(ssc, sd_assess); 12369 } 12370 12371 /* 12372 * Function: sd_ssc_set_info 12373 * 12374 * Description: Mark ssc_flags and set ssc_info which would be the 12375 * payload of uderr ereport. This function will cause 12376 * sd_ssc_ereport_post to post uderr ereport only. 12377 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI), 12378 * the function will also call SD_ERROR or scsi_log for a 12379 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device. 12380 * 12381 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12382 * sd_uscsi_info in. 12383 * ssc_flags - indicate the sub-category of a uderr. 12384 * comp - this argument is meaningful only when 12385 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible 12386 * values include: 12387 * > 0, SD_ERROR is used with comp as the driver logging 12388 * component; 12389 * = 0, scsi-log is used to log error telemetries; 12390 * < 0, no log available for this telemetry. 12391 * 12392 * Context: Kernel thread or interrupt context 12393 */ 12394 static void 12395 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...) 12396 { 12397 va_list ap; 12398 12399 ASSERT(ssc != NULL); 12400 ASSERT(ssc->ssc_un != NULL); 12401 12402 ssc->ssc_flags |= ssc_flags; 12403 va_start(ap, fmt); 12404 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap); 12405 va_end(ap); 12406 12407 /* 12408 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command 12409 * with invalid data sent back. For non-uscsi command, the 12410 * following code will be bypassed. 12411 */ 12412 if (ssc_flags & SSC_FLAGS_INVALID_DATA) { 12413 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) { 12414 /* 12415 * If the error belong to certain component and we 12416 * do not want it to show up on the console, we 12417 * will use SD_ERROR, otherwise scsi_log is 12418 * preferred. 12419 */ 12420 if (comp > 0) { 12421 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info); 12422 } else if (comp == 0) { 12423 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label, 12424 CE_WARN, ssc->ssc_info); 12425 } 12426 } 12427 } 12428 } 12429 12430 /* 12431 * Function: sd_buf_iodone 12432 * 12433 * Description: Frees the sd_xbuf & returns the buf to its originator. 12434 * 12435 * Context: May be called from interrupt context. 12436 */ 12437 /* ARGSUSED */ 12438 static void 12439 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12440 { 12441 struct sd_xbuf *xp; 12442 12443 ASSERT(un != NULL); 12444 ASSERT(bp != NULL); 12445 ASSERT(!mutex_owned(SD_MUTEX(un))); 12446 12447 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12448 12449 xp = SD_GET_XBUF(bp); 12450 ASSERT(xp != NULL); 12451 12452 /* xbuf is gone after this */ 12453 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) { 12454 mutex_enter(SD_MUTEX(un)); 12455 12456 /* 12457 * Grab time when the cmd completed. 12458 * This is used for determining if the system has been 12459 * idle long enough to make it idle to the PM framework. 12460 * This is for lowering the overhead, and therefore improving 12461 * performance per I/O operation. 12462 */ 12463 un->un_pm_idle_time = ddi_get_time(); 12464 12465 un->un_ncmds_in_driver--; 12466 ASSERT(un->un_ncmds_in_driver >= 0); 12467 SD_INFO(SD_LOG_IO, un, 12468 "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12469 un->un_ncmds_in_driver); 12470 12471 mutex_exit(SD_MUTEX(un)); 12472 } 12473 12474 biodone(bp); /* bp is gone after this */ 12475 12476 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12477 } 12478 12479 12480 /* 12481 * Function: sd_uscsi_iodone 12482 * 12483 * Description: Frees the sd_xbuf & returns the buf to its originator. 12484 * 12485 * Context: May be called from interrupt context. 12486 */ 12487 /* ARGSUSED */ 12488 static void 12489 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12490 { 12491 struct sd_xbuf *xp; 12492 12493 ASSERT(un != NULL); 12494 ASSERT(bp != NULL); 12495 12496 xp = SD_GET_XBUF(bp); 12497 ASSERT(xp != NULL); 12498 ASSERT(!mutex_owned(SD_MUTEX(un))); 12499 12500 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12501 12502 bp->b_private = xp->xb_private; 12503 12504 mutex_enter(SD_MUTEX(un)); 12505 12506 /* 12507 * Grab time when the cmd completed. 12508 * This is used for determining if the system has been 12509 * idle long enough to make it idle to the PM framework. 12510 * This is for lowering the overhead, and therefore improving 12511 * performance per I/O operation. 12512 */ 12513 un->un_pm_idle_time = ddi_get_time(); 12514 12515 un->un_ncmds_in_driver--; 12516 ASSERT(un->un_ncmds_in_driver >= 0); 12517 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12518 un->un_ncmds_in_driver); 12519 12520 mutex_exit(SD_MUTEX(un)); 12521 12522 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 12523 SENSE_LENGTH) { 12524 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 12525 MAX_SENSE_LENGTH); 12526 } else { 12527 kmem_free(xp, sizeof (struct sd_xbuf)); 12528 } 12529 12530 biodone(bp); 12531 12532 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12533 } 12534 12535 12536 /* 12537 * Function: sd_mapblockaddr_iostart 12538 * 12539 * Description: Verify request lies within the partition limits for 12540 * the indicated minor device. Issue "overrun" buf if 12541 * request would exceed partition range. Converts 12542 * partition-relative block address to absolute. 12543 * 12544 * Upon exit of this function: 12545 * 1.I/O is aligned 12546 * xp->xb_blkno represents the absolute sector address 12547 * 2.I/O is misaligned 12548 * xp->xb_blkno represents the absolute logical block address 12549 * based on DEV_BSIZE. The logical block address will be 12550 * converted to physical sector address in sd_mapblocksize_\ 12551 * iostart. 12552 * 3.I/O is misaligned but is aligned in "overrun" buf 12553 * xp->xb_blkno represents the absolute logical block address 12554 * based on DEV_BSIZE. The logical block address will be 12555 * converted to physical sector address in sd_mapblocksize_\ 12556 * iostart. But no RMW will be issued in this case. 12557 * 12558 * Context: Can sleep 12559 * 12560 * Issues: This follows what the old code did, in terms of accessing 12561 * some of the partition info in the unit struct without holding 12562 * the mutext. This is a general issue, if the partition info 12563 * can be altered while IO is in progress... as soon as we send 12564 * a buf, its partitioning can be invalid before it gets to the 12565 * device. Probably the right fix is to move partitioning out 12566 * of the driver entirely. 12567 */ 12568 12569 static void 12570 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12571 { 12572 diskaddr_t nblocks; /* #blocks in the given partition */ 12573 daddr_t blocknum; /* Block number specified by the buf */ 12574 size_t requested_nblocks; 12575 size_t available_nblocks; 12576 int partition; 12577 diskaddr_t partition_offset; 12578 struct sd_xbuf *xp; 12579 int secmask = 0, blknomask = 0; 12580 ushort_t is_aligned = TRUE; 12581 12582 ASSERT(un != NULL); 12583 ASSERT(bp != NULL); 12584 ASSERT(!mutex_owned(SD_MUTEX(un))); 12585 12586 SD_TRACE(SD_LOG_IO_PARTITION, un, 12587 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12588 12589 xp = SD_GET_XBUF(bp); 12590 ASSERT(xp != NULL); 12591 12592 /* 12593 * If the geometry is not indicated as valid, attempt to access 12594 * the unit & verify the geometry/label. This can be the case for 12595 * removable-media devices, of if the device was opened in 12596 * NDELAY/NONBLOCK mode. 12597 */ 12598 partition = SDPART(bp->b_edev); 12599 12600 if (!SD_IS_VALID_LABEL(un)) { 12601 sd_ssc_t *ssc; 12602 /* 12603 * Initialize sd_ssc_t for internal uscsi commands 12604 * In case of potential porformance issue, we need 12605 * to alloc memory only if there is invalid label 12606 */ 12607 ssc = sd_ssc_init(un); 12608 12609 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) { 12610 /* 12611 * For removable devices it is possible to start an 12612 * I/O without a media by opening the device in nodelay 12613 * mode. Also for writable CDs there can be many 12614 * scenarios where there is no geometry yet but volume 12615 * manager is trying to issue a read() just because 12616 * it can see TOC on the CD. So do not print a message 12617 * for removables. 12618 */ 12619 if (!un->un_f_has_removable_media) { 12620 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12621 "i/o to invalid geometry\n"); 12622 } 12623 bioerror(bp, EIO); 12624 bp->b_resid = bp->b_bcount; 12625 SD_BEGIN_IODONE(index, un, bp); 12626 12627 sd_ssc_fini(ssc); 12628 return; 12629 } 12630 sd_ssc_fini(ssc); 12631 } 12632 12633 nblocks = 0; 12634 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 12635 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 12636 12637 if (un->un_f_enable_rmw) { 12638 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1; 12639 secmask = un->un_phy_blocksize - 1; 12640 } else { 12641 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 12642 secmask = un->un_tgt_blocksize - 1; 12643 } 12644 12645 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) { 12646 is_aligned = FALSE; 12647 } 12648 12649 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) { 12650 /* 12651 * If I/O is aligned, no need to involve RMW(Read Modify Write) 12652 * Convert the logical block number to target's physical sector 12653 * number. 12654 */ 12655 if (is_aligned) { 12656 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno); 12657 } else { 12658 /* 12659 * There is no RMW if we're just reading, so don't 12660 * warn or error out because of it. 12661 */ 12662 if (bp->b_flags & B_READ) { 12663 /*EMPTY*/ 12664 } else if (!un->un_f_enable_rmw && 12665 un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) { 12666 bp->b_flags |= B_ERROR; 12667 goto error_exit; 12668 } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) { 12669 mutex_enter(SD_MUTEX(un)); 12670 if (!un->un_f_enable_rmw && 12671 un->un_rmw_msg_timeid == NULL) { 12672 scsi_log(SD_DEVINFO(un), sd_label, 12673 CE_WARN, "I/O request is not " 12674 "aligned with %d disk sector size. " 12675 "It is handled through Read Modify " 12676 "Write but the performance is " 12677 "very low.\n", 12678 un->un_tgt_blocksize); 12679 un->un_rmw_msg_timeid = 12680 timeout(sd_rmw_msg_print_handler, 12681 un, SD_RMW_MSG_PRINT_TIMEOUT); 12682 } else { 12683 un->un_rmw_incre_count ++; 12684 } 12685 mutex_exit(SD_MUTEX(un)); 12686 } 12687 12688 nblocks = SD_TGT2SYSBLOCK(un, nblocks); 12689 partition_offset = SD_TGT2SYSBLOCK(un, 12690 partition_offset); 12691 } 12692 } 12693 12694 /* 12695 * blocknum is the starting block number of the request. At this 12696 * point it is still relative to the start of the minor device. 12697 */ 12698 blocknum = xp->xb_blkno; 12699 12700 /* 12701 * Legacy: If the starting block number is one past the last block 12702 * in the partition, do not set B_ERROR in the buf. 12703 */ 12704 if (blocknum == nblocks) { 12705 goto error_exit; 12706 } 12707 12708 /* 12709 * Confirm that the first block of the request lies within the 12710 * partition limits. Also the requested number of bytes must be 12711 * a multiple of the system block size. 12712 */ 12713 if ((blocknum < 0) || (blocknum >= nblocks) || 12714 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) { 12715 bp->b_flags |= B_ERROR; 12716 goto error_exit; 12717 } 12718 12719 /* 12720 * If the requsted # blocks exceeds the available # blocks, that 12721 * is an overrun of the partition. 12722 */ 12723 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12724 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 12725 } else { 12726 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount); 12727 } 12728 12729 available_nblocks = (size_t)(nblocks - blocknum); 12730 ASSERT(nblocks >= blocknum); 12731 12732 if (requested_nblocks > available_nblocks) { 12733 size_t resid; 12734 12735 /* 12736 * Allocate an "overrun" buf to allow the request to proceed 12737 * for the amount of space available in the partition. The 12738 * amount not transferred will be added into the b_resid 12739 * when the operation is complete. The overrun buf 12740 * replaces the original buf here, and the original buf 12741 * is saved inside the overrun buf, for later use. 12742 */ 12743 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12744 resid = SD_TGTBLOCKS2BYTES(un, 12745 (offset_t)(requested_nblocks - available_nblocks)); 12746 } else { 12747 resid = SD_SYSBLOCKS2BYTES( 12748 (offset_t)(requested_nblocks - available_nblocks)); 12749 } 12750 12751 size_t count = bp->b_bcount - resid; 12752 /* 12753 * Note: count is an unsigned entity thus it'll NEVER 12754 * be less than 0 so ASSERT the original values are 12755 * correct. 12756 */ 12757 ASSERT(bp->b_bcount >= resid); 12758 12759 bp = sd_bioclone_alloc(bp, count, blocknum, 12760 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12761 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12762 ASSERT(xp != NULL); 12763 } 12764 12765 /* At this point there should be no residual for this buf. */ 12766 ASSERT(bp->b_resid == 0); 12767 12768 /* Convert the block number to an absolute address. */ 12769 xp->xb_blkno += partition_offset; 12770 12771 SD_NEXT_IOSTART(index, un, bp); 12772 12773 SD_TRACE(SD_LOG_IO_PARTITION, un, 12774 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12775 12776 return; 12777 12778 error_exit: 12779 bp->b_resid = bp->b_bcount; 12780 SD_BEGIN_IODONE(index, un, bp); 12781 SD_TRACE(SD_LOG_IO_PARTITION, un, 12782 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12783 } 12784 12785 12786 /* 12787 * Function: sd_mapblockaddr_iodone 12788 * 12789 * Description: Completion-side processing for partition management. 12790 * 12791 * Context: May be called under interrupt context 12792 */ 12793 12794 static void 12795 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12796 { 12797 /* int partition; */ /* Not used, see below. */ 12798 ASSERT(un != NULL); 12799 ASSERT(bp != NULL); 12800 ASSERT(!mutex_owned(SD_MUTEX(un))); 12801 12802 SD_TRACE(SD_LOG_IO_PARTITION, un, 12803 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12804 12805 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12806 /* 12807 * We have an "overrun" buf to deal with... 12808 */ 12809 struct sd_xbuf *xp; 12810 struct buf *obp; /* ptr to the original buf */ 12811 12812 xp = SD_GET_XBUF(bp); 12813 ASSERT(xp != NULL); 12814 12815 /* Retrieve the pointer to the original buf */ 12816 obp = (struct buf *)xp->xb_private; 12817 ASSERT(obp != NULL); 12818 12819 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12820 bioerror(obp, bp->b_error); 12821 12822 sd_bioclone_free(bp); 12823 12824 /* 12825 * Get back the original buf. 12826 * Note that since the restoration of xb_blkno below 12827 * was removed, the sd_xbuf is not needed. 12828 */ 12829 bp = obp; 12830 /* 12831 * xp = SD_GET_XBUF(bp); 12832 * ASSERT(xp != NULL); 12833 */ 12834 } 12835 12836 /* 12837 * Convert sd->xb_blkno back to a minor-device relative value. 12838 * Note: this has been commented out, as it is not needed in the 12839 * current implementation of the driver (ie, since this function 12840 * is at the top of the layering chains, so the info will be 12841 * discarded) and it is in the "hot" IO path. 12842 * 12843 * partition = getminor(bp->b_edev) & SDPART_MASK; 12844 * xp->xb_blkno -= un->un_offset[partition]; 12845 */ 12846 12847 SD_NEXT_IODONE(index, un, bp); 12848 12849 SD_TRACE(SD_LOG_IO_PARTITION, un, 12850 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12851 } 12852 12853 12854 /* 12855 * Function: sd_mapblocksize_iostart 12856 * 12857 * Description: Convert between system block size (un->un_sys_blocksize) 12858 * and target block size (un->un_tgt_blocksize). 12859 * 12860 * Context: Can sleep to allocate resources. 12861 * 12862 * Assumptions: A higher layer has already performed any partition validation, 12863 * and converted the xp->xb_blkno to an absolute value relative 12864 * to the start of the device. 12865 * 12866 * It is also assumed that the higher layer has implemented 12867 * an "overrun" mechanism for the case where the request would 12868 * read/write beyond the end of a partition. In this case we 12869 * assume (and ASSERT) that bp->b_resid == 0. 12870 * 12871 * Note: The implementation for this routine assumes the target 12872 * block size remains constant between allocation and transport. 12873 */ 12874 12875 static void 12876 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12877 { 12878 struct sd_mapblocksize_info *bsp; 12879 struct sd_xbuf *xp; 12880 offset_t first_byte; 12881 daddr_t start_block, end_block; 12882 daddr_t request_bytes; 12883 ushort_t is_aligned = FALSE; 12884 12885 ASSERT(un != NULL); 12886 ASSERT(bp != NULL); 12887 ASSERT(!mutex_owned(SD_MUTEX(un))); 12888 ASSERT(bp->b_resid == 0); 12889 12890 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12891 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12892 12893 /* 12894 * For a non-writable CD, a write request is an error 12895 */ 12896 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12897 (un->un_f_mmc_writable_media == FALSE)) { 12898 bioerror(bp, EIO); 12899 bp->b_resid = bp->b_bcount; 12900 SD_BEGIN_IODONE(index, un, bp); 12901 return; 12902 } 12903 12904 /* 12905 * We do not need a shadow buf if the device is using 12906 * un->un_sys_blocksize as its block size or if bcount == 0. 12907 * In this case there is no layer-private data block allocated. 12908 */ 12909 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 12910 (bp->b_bcount == 0)) { 12911 goto done; 12912 } 12913 12914 #if defined(__i386) || defined(__amd64) 12915 /* We do not support non-block-aligned transfers for ROD devices */ 12916 ASSERT(!ISROD(un)); 12917 #endif 12918 12919 xp = SD_GET_XBUF(bp); 12920 ASSERT(xp != NULL); 12921 12922 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12923 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12924 un->un_tgt_blocksize, DEV_BSIZE); 12925 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12926 "request start block:0x%x\n", xp->xb_blkno); 12927 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12928 "request len:0x%x\n", bp->b_bcount); 12929 12930 /* 12931 * Allocate the layer-private data area for the mapblocksize layer. 12932 * Layers are allowed to use the xp_private member of the sd_xbuf 12933 * struct to store the pointer to their layer-private data block, but 12934 * each layer also has the responsibility of restoring the prior 12935 * contents of xb_private before returning the buf/xbuf to the 12936 * higher layer that sent it. 12937 * 12938 * Here we save the prior contents of xp->xb_private into the 12939 * bsp->mbs_oprivate field of our layer-private data area. This value 12940 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12941 * the layer-private area and returning the buf/xbuf to the layer 12942 * that sent it. 12943 * 12944 * Note that here we use kmem_zalloc for the allocation as there are 12945 * parts of the mapblocksize code that expect certain fields to be 12946 * zero unless explicitly set to a required value. 12947 */ 12948 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12949 bsp->mbs_oprivate = xp->xb_private; 12950 xp->xb_private = bsp; 12951 12952 /* 12953 * This treats the data on the disk (target) as an array of bytes. 12954 * first_byte is the byte offset, from the beginning of the device, 12955 * to the location of the request. This is converted from a 12956 * un->un_sys_blocksize block address to a byte offset, and then back 12957 * to a block address based upon a un->un_tgt_blocksize block size. 12958 * 12959 * xp->xb_blkno should be absolute upon entry into this function, 12960 * but, but it is based upon partitions that use the "system" 12961 * block size. It must be adjusted to reflect the block size of 12962 * the target. 12963 * 12964 * Note that end_block is actually the block that follows the last 12965 * block of the request, but that's what is needed for the computation. 12966 */ 12967 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 12968 if (un->un_f_enable_rmw) { 12969 start_block = xp->xb_blkno = 12970 (first_byte / un->un_phy_blocksize) * 12971 (un->un_phy_blocksize / DEV_BSIZE); 12972 end_block = ((first_byte + bp->b_bcount + 12973 un->un_phy_blocksize - 1) / un->un_phy_blocksize) * 12974 (un->un_phy_blocksize / DEV_BSIZE); 12975 } else { 12976 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12977 end_block = (first_byte + bp->b_bcount + 12978 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 12979 } 12980 12981 /* request_bytes is rounded up to a multiple of the target block size */ 12982 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12983 12984 /* 12985 * See if the starting address of the request and the request 12986 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12987 * then we do not need to allocate a shadow buf to handle the request. 12988 */ 12989 if (un->un_f_enable_rmw) { 12990 if (((first_byte % un->un_phy_blocksize) == 0) && 12991 ((bp->b_bcount % un->un_phy_blocksize) == 0)) { 12992 is_aligned = TRUE; 12993 } 12994 } else { 12995 if (((first_byte % un->un_tgt_blocksize) == 0) && 12996 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12997 is_aligned = TRUE; 12998 } 12999 } 13000 13001 if ((bp->b_flags & B_READ) == 0) { 13002 /* 13003 * Lock the range for a write operation. An aligned request is 13004 * considered a simple write; otherwise the request must be a 13005 * read-modify-write. 13006 */ 13007 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 13008 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 13009 } 13010 13011 /* 13012 * Alloc a shadow buf if the request is not aligned. Also, this is 13013 * where the READ command is generated for a read-modify-write. (The 13014 * write phase is deferred until after the read completes.) 13015 */ 13016 if (is_aligned == FALSE) { 13017 13018 struct sd_mapblocksize_info *shadow_bsp; 13019 struct sd_xbuf *shadow_xp; 13020 struct buf *shadow_bp; 13021 13022 /* 13023 * Allocate the shadow buf and it associated xbuf. Note that 13024 * after this call the xb_blkno value in both the original 13025 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 13026 * same: absolute relative to the start of the device, and 13027 * adjusted for the target block size. The b_blkno in the 13028 * shadow buf will also be set to this value. We should never 13029 * change b_blkno in the original bp however. 13030 * 13031 * Note also that the shadow buf will always need to be a 13032 * READ command, regardless of whether the incoming command 13033 * is a READ or a WRITE. 13034 */ 13035 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 13036 xp->xb_blkno, 13037 (int (*)(struct buf *)) sd_mapblocksize_iodone); 13038 13039 shadow_xp = SD_GET_XBUF(shadow_bp); 13040 13041 /* 13042 * Allocate the layer-private data for the shadow buf. 13043 * (No need to preserve xb_private in the shadow xbuf.) 13044 */ 13045 shadow_xp->xb_private = shadow_bsp = 13046 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 13047 13048 /* 13049 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 13050 * to figure out where the start of the user data is (based upon 13051 * the system block size) in the data returned by the READ 13052 * command (which will be based upon the target blocksize). Note 13053 * that this is only really used if the request is unaligned. 13054 */ 13055 if (un->un_f_enable_rmw) { 13056 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13057 ((offset_t)xp->xb_blkno * un->un_sys_blocksize)); 13058 ASSERT((bsp->mbs_copy_offset >= 0) && 13059 (bsp->mbs_copy_offset < un->un_phy_blocksize)); 13060 } else { 13061 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13062 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13063 ASSERT((bsp->mbs_copy_offset >= 0) && 13064 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13065 } 13066 13067 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13068 13069 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13070 13071 /* Transfer the wmap (if any) to the shadow buf */ 13072 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13073 bsp->mbs_wmp = NULL; 13074 13075 /* 13076 * The shadow buf goes on from here in place of the 13077 * original buf. 13078 */ 13079 shadow_bsp->mbs_orig_bp = bp; 13080 bp = shadow_bp; 13081 } 13082 13083 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13084 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13085 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13086 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13087 request_bytes); 13088 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13089 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13090 13091 done: 13092 SD_NEXT_IOSTART(index, un, bp); 13093 13094 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13095 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13096 } 13097 13098 13099 /* 13100 * Function: sd_mapblocksize_iodone 13101 * 13102 * Description: Completion side processing for block-size mapping. 13103 * 13104 * Context: May be called under interrupt context 13105 */ 13106 13107 static void 13108 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13109 { 13110 struct sd_mapblocksize_info *bsp; 13111 struct sd_xbuf *xp; 13112 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13113 struct buf *orig_bp; /* ptr to the original buf */ 13114 offset_t shadow_end; 13115 offset_t request_end; 13116 offset_t shadow_start; 13117 ssize_t copy_offset; 13118 size_t copy_length; 13119 size_t shortfall; 13120 uint_t is_write; /* TRUE if this bp is a WRITE */ 13121 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13122 13123 ASSERT(un != NULL); 13124 ASSERT(bp != NULL); 13125 13126 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13127 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13128 13129 /* 13130 * There is no shadow buf or layer-private data if the target is 13131 * using un->un_sys_blocksize as its block size or if bcount == 0. 13132 */ 13133 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 13134 (bp->b_bcount == 0)) { 13135 goto exit; 13136 } 13137 13138 xp = SD_GET_XBUF(bp); 13139 ASSERT(xp != NULL); 13140 13141 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13142 bsp = xp->xb_private; 13143 13144 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13145 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13146 13147 if (is_write) { 13148 /* 13149 * For a WRITE request we must free up the block range that 13150 * we have locked up. This holds regardless of whether this is 13151 * an aligned write request or a read-modify-write request. 13152 */ 13153 sd_range_unlock(un, bsp->mbs_wmp); 13154 bsp->mbs_wmp = NULL; 13155 } 13156 13157 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13158 /* 13159 * An aligned read or write command will have no shadow buf; 13160 * there is not much else to do with it. 13161 */ 13162 goto done; 13163 } 13164 13165 orig_bp = bsp->mbs_orig_bp; 13166 ASSERT(orig_bp != NULL); 13167 orig_xp = SD_GET_XBUF(orig_bp); 13168 ASSERT(orig_xp != NULL); 13169 ASSERT(!mutex_owned(SD_MUTEX(un))); 13170 13171 if (!is_write && has_wmap) { 13172 /* 13173 * A READ with a wmap means this is the READ phase of a 13174 * read-modify-write. If an error occurred on the READ then 13175 * we do not proceed with the WRITE phase or copy any data. 13176 * Just release the write maps and return with an error. 13177 */ 13178 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13179 orig_bp->b_resid = orig_bp->b_bcount; 13180 bioerror(orig_bp, bp->b_error); 13181 sd_range_unlock(un, bsp->mbs_wmp); 13182 goto freebuf_done; 13183 } 13184 } 13185 13186 /* 13187 * Here is where we set up to copy the data from the shadow buf 13188 * into the space associated with the original buf. 13189 * 13190 * To deal with the conversion between block sizes, these 13191 * computations treat the data as an array of bytes, with the 13192 * first byte (byte 0) corresponding to the first byte in the 13193 * first block on the disk. 13194 */ 13195 13196 /* 13197 * shadow_start and shadow_len indicate the location and size of 13198 * the data returned with the shadow IO request. 13199 */ 13200 if (un->un_f_enable_rmw) { 13201 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 13202 } else { 13203 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13204 } 13205 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13206 13207 /* 13208 * copy_offset gives the offset (in bytes) from the start of the first 13209 * block of the READ request to the beginning of the data. We retrieve 13210 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13211 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13212 * data to be copied (in bytes). 13213 */ 13214 copy_offset = bsp->mbs_copy_offset; 13215 if (un->un_f_enable_rmw) { 13216 ASSERT((copy_offset >= 0) && 13217 (copy_offset < un->un_phy_blocksize)); 13218 } else { 13219 ASSERT((copy_offset >= 0) && 13220 (copy_offset < un->un_tgt_blocksize)); 13221 } 13222 13223 copy_length = orig_bp->b_bcount; 13224 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13225 13226 /* 13227 * Set up the resid and error fields of orig_bp as appropriate. 13228 */ 13229 if (shadow_end >= request_end) { 13230 /* We got all the requested data; set resid to zero */ 13231 orig_bp->b_resid = 0; 13232 } else { 13233 /* 13234 * We failed to get enough data to fully satisfy the original 13235 * request. Just copy back whatever data we got and set 13236 * up the residual and error code as required. 13237 * 13238 * 'shortfall' is the amount by which the data received with the 13239 * shadow buf has "fallen short" of the requested amount. 13240 */ 13241 shortfall = (size_t)(request_end - shadow_end); 13242 13243 if (shortfall > orig_bp->b_bcount) { 13244 /* 13245 * We did not get enough data to even partially 13246 * fulfill the original request. The residual is 13247 * equal to the amount requested. 13248 */ 13249 orig_bp->b_resid = orig_bp->b_bcount; 13250 } else { 13251 /* 13252 * We did not get all the data that we requested 13253 * from the device, but we will try to return what 13254 * portion we did get. 13255 */ 13256 orig_bp->b_resid = shortfall; 13257 } 13258 ASSERT(copy_length >= orig_bp->b_resid); 13259 copy_length -= orig_bp->b_resid; 13260 } 13261 13262 /* Propagate the error code from the shadow buf to the original buf */ 13263 bioerror(orig_bp, bp->b_error); 13264 13265 if (is_write) { 13266 goto freebuf_done; /* No data copying for a WRITE */ 13267 } 13268 13269 if (has_wmap) { 13270 /* 13271 * This is a READ command from the READ phase of a 13272 * read-modify-write request. We have to copy the data given 13273 * by the user OVER the data returned by the READ command, 13274 * then convert the command from a READ to a WRITE and send 13275 * it back to the target. 13276 */ 13277 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13278 copy_length); 13279 13280 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13281 13282 /* 13283 * Dispatch the WRITE command to the taskq thread, which 13284 * will in turn send the command to the target. When the 13285 * WRITE command completes, we (sd_mapblocksize_iodone()) 13286 * will get called again as part of the iodone chain 13287 * processing for it. Note that we will still be dealing 13288 * with the shadow buf at that point. 13289 */ 13290 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13291 KM_NOSLEEP) != 0) { 13292 /* 13293 * Dispatch was successful so we are done. Return 13294 * without going any higher up the iodone chain. Do 13295 * not free up any layer-private data until after the 13296 * WRITE completes. 13297 */ 13298 return; 13299 } 13300 13301 /* 13302 * Dispatch of the WRITE command failed; set up the error 13303 * condition and send this IO back up the iodone chain. 13304 */ 13305 bioerror(orig_bp, EIO); 13306 orig_bp->b_resid = orig_bp->b_bcount; 13307 13308 } else { 13309 /* 13310 * This is a regular READ request (ie, not a RMW). Copy the 13311 * data from the shadow buf into the original buf. The 13312 * copy_offset compensates for any "misalignment" between the 13313 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13314 * original buf (with its un->un_sys_blocksize blocks). 13315 */ 13316 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13317 copy_length); 13318 } 13319 13320 freebuf_done: 13321 13322 /* 13323 * At this point we still have both the shadow buf AND the original 13324 * buf to deal with, as well as the layer-private data area in each. 13325 * Local variables are as follows: 13326 * 13327 * bp -- points to shadow buf 13328 * xp -- points to xbuf of shadow buf 13329 * bsp -- points to layer-private data area of shadow buf 13330 * orig_bp -- points to original buf 13331 * 13332 * First free the shadow buf and its associated xbuf, then free the 13333 * layer-private data area from the shadow buf. There is no need to 13334 * restore xb_private in the shadow xbuf. 13335 */ 13336 sd_shadow_buf_free(bp); 13337 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13338 13339 /* 13340 * Now update the local variables to point to the original buf, xbuf, 13341 * and layer-private area. 13342 */ 13343 bp = orig_bp; 13344 xp = SD_GET_XBUF(bp); 13345 ASSERT(xp != NULL); 13346 ASSERT(xp == orig_xp); 13347 bsp = xp->xb_private; 13348 ASSERT(bsp != NULL); 13349 13350 done: 13351 /* 13352 * Restore xb_private to whatever it was set to by the next higher 13353 * layer in the chain, then free the layer-private data area. 13354 */ 13355 xp->xb_private = bsp->mbs_oprivate; 13356 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13357 13358 exit: 13359 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13360 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13361 13362 SD_NEXT_IODONE(index, un, bp); 13363 } 13364 13365 13366 /* 13367 * Function: sd_checksum_iostart 13368 * 13369 * Description: A stub function for a layer that's currently not used. 13370 * For now just a placeholder. 13371 * 13372 * Context: Kernel thread context 13373 */ 13374 13375 static void 13376 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13377 { 13378 ASSERT(un != NULL); 13379 ASSERT(bp != NULL); 13380 ASSERT(!mutex_owned(SD_MUTEX(un))); 13381 SD_NEXT_IOSTART(index, un, bp); 13382 } 13383 13384 13385 /* 13386 * Function: sd_checksum_iodone 13387 * 13388 * Description: A stub function for a layer that's currently not used. 13389 * For now just a placeholder. 13390 * 13391 * Context: May be called under interrupt context 13392 */ 13393 13394 static void 13395 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13396 { 13397 ASSERT(un != NULL); 13398 ASSERT(bp != NULL); 13399 ASSERT(!mutex_owned(SD_MUTEX(un))); 13400 SD_NEXT_IODONE(index, un, bp); 13401 } 13402 13403 13404 /* 13405 * Function: sd_checksum_uscsi_iostart 13406 * 13407 * Description: A stub function for a layer that's currently not used. 13408 * For now just a placeholder. 13409 * 13410 * Context: Kernel thread context 13411 */ 13412 13413 static void 13414 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13415 { 13416 ASSERT(un != NULL); 13417 ASSERT(bp != NULL); 13418 ASSERT(!mutex_owned(SD_MUTEX(un))); 13419 SD_NEXT_IOSTART(index, un, bp); 13420 } 13421 13422 13423 /* 13424 * Function: sd_checksum_uscsi_iodone 13425 * 13426 * Description: A stub function for a layer that's currently not used. 13427 * For now just a placeholder. 13428 * 13429 * Context: May be called under interrupt context 13430 */ 13431 13432 static void 13433 sd_checksum_uscsi_iodone(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 SD_NEXT_IODONE(index, un, bp); 13439 } 13440 13441 13442 /* 13443 * Function: sd_pm_iostart 13444 * 13445 * Description: iostart-side routine for Power mangement. 13446 * 13447 * Context: Kernel thread context 13448 */ 13449 13450 static void 13451 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13452 { 13453 ASSERT(un != NULL); 13454 ASSERT(bp != NULL); 13455 ASSERT(!mutex_owned(SD_MUTEX(un))); 13456 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13457 13458 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13459 13460 if (sd_pm_entry(un) != DDI_SUCCESS) { 13461 /* 13462 * Set up to return the failed buf back up the 'iodone' 13463 * side of the calling chain. 13464 */ 13465 bioerror(bp, EIO); 13466 bp->b_resid = bp->b_bcount; 13467 13468 SD_BEGIN_IODONE(index, un, bp); 13469 13470 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13471 return; 13472 } 13473 13474 SD_NEXT_IOSTART(index, un, bp); 13475 13476 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13477 } 13478 13479 13480 /* 13481 * Function: sd_pm_iodone 13482 * 13483 * Description: iodone-side routine for power mangement. 13484 * 13485 * Context: may be called from interrupt context 13486 */ 13487 13488 static void 13489 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13490 { 13491 ASSERT(un != NULL); 13492 ASSERT(bp != NULL); 13493 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13494 13495 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13496 13497 /* 13498 * After attach the following flag is only read, so don't 13499 * take the penalty of acquiring a mutex for it. 13500 */ 13501 if (un->un_f_pm_is_enabled == TRUE) { 13502 sd_pm_exit(un); 13503 } 13504 13505 SD_NEXT_IODONE(index, un, bp); 13506 13507 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13508 } 13509 13510 13511 /* 13512 * Function: sd_core_iostart 13513 * 13514 * Description: Primary driver function for enqueuing buf(9S) structs from 13515 * the system and initiating IO to the target device 13516 * 13517 * Context: Kernel thread context. Can sleep. 13518 * 13519 * Assumptions: - The given xp->xb_blkno is absolute 13520 * (ie, relative to the start of the device). 13521 * - The IO is to be done using the native blocksize of 13522 * the device, as specified in un->un_tgt_blocksize. 13523 */ 13524 /* ARGSUSED */ 13525 static void 13526 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13527 { 13528 struct sd_xbuf *xp; 13529 13530 ASSERT(un != NULL); 13531 ASSERT(bp != NULL); 13532 ASSERT(!mutex_owned(SD_MUTEX(un))); 13533 ASSERT(bp->b_resid == 0); 13534 13535 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13536 13537 xp = SD_GET_XBUF(bp); 13538 ASSERT(xp != NULL); 13539 13540 mutex_enter(SD_MUTEX(un)); 13541 13542 /* 13543 * If we are currently in the failfast state, fail any new IO 13544 * that has B_FAILFAST set, then return. 13545 */ 13546 if ((bp->b_flags & B_FAILFAST) && 13547 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13548 mutex_exit(SD_MUTEX(un)); 13549 bioerror(bp, EIO); 13550 bp->b_resid = bp->b_bcount; 13551 SD_BEGIN_IODONE(index, un, bp); 13552 return; 13553 } 13554 13555 if (SD_IS_DIRECT_PRIORITY(xp)) { 13556 /* 13557 * Priority command -- transport it immediately. 13558 * 13559 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13560 * because all direct priority commands should be associated 13561 * with error recovery actions which we don't want to retry. 13562 */ 13563 sd_start_cmds(un, bp); 13564 } else { 13565 /* 13566 * Normal command -- add it to the wait queue, then start 13567 * transporting commands from the wait queue. 13568 */ 13569 sd_add_buf_to_waitq(un, bp); 13570 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13571 sd_start_cmds(un, NULL); 13572 } 13573 13574 mutex_exit(SD_MUTEX(un)); 13575 13576 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13577 } 13578 13579 13580 /* 13581 * Function: sd_init_cdb_limits 13582 * 13583 * Description: This is to handle scsi_pkt initialization differences 13584 * between the driver platforms. 13585 * 13586 * Legacy behaviors: 13587 * 13588 * If the block number or the sector count exceeds the 13589 * capabilities of a Group 0 command, shift over to a 13590 * Group 1 command. We don't blindly use Group 1 13591 * commands because a) some drives (CDC Wren IVs) get a 13592 * bit confused, and b) there is probably a fair amount 13593 * of speed difference for a target to receive and decode 13594 * a 10 byte command instead of a 6 byte command. 13595 * 13596 * The xfer time difference of 6 vs 10 byte CDBs is 13597 * still significant so this code is still worthwhile. 13598 * 10 byte CDBs are very inefficient with the fas HBA driver 13599 * and older disks. Each CDB byte took 1 usec with some 13600 * popular disks. 13601 * 13602 * Context: Must be called at attach time 13603 */ 13604 13605 static void 13606 sd_init_cdb_limits(struct sd_lun *un) 13607 { 13608 int hba_cdb_limit; 13609 13610 /* 13611 * Use CDB_GROUP1 commands for most devices except for 13612 * parallel SCSI fixed drives in which case we get better 13613 * performance using CDB_GROUP0 commands (where applicable). 13614 */ 13615 un->un_mincdb = SD_CDB_GROUP1; 13616 #if !defined(__fibre) 13617 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13618 !un->un_f_has_removable_media) { 13619 un->un_mincdb = SD_CDB_GROUP0; 13620 } 13621 #endif 13622 13623 /* 13624 * Try to read the max-cdb-length supported by HBA. 13625 */ 13626 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13627 if (0 >= un->un_max_hba_cdb) { 13628 un->un_max_hba_cdb = CDB_GROUP4; 13629 hba_cdb_limit = SD_CDB_GROUP4; 13630 } else if (0 < un->un_max_hba_cdb && 13631 un->un_max_hba_cdb < CDB_GROUP1) { 13632 hba_cdb_limit = SD_CDB_GROUP0; 13633 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13634 un->un_max_hba_cdb < CDB_GROUP5) { 13635 hba_cdb_limit = SD_CDB_GROUP1; 13636 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13637 un->un_max_hba_cdb < CDB_GROUP4) { 13638 hba_cdb_limit = SD_CDB_GROUP5; 13639 } else { 13640 hba_cdb_limit = SD_CDB_GROUP4; 13641 } 13642 13643 /* 13644 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13645 * commands for fixed disks unless we are building for a 32 bit 13646 * kernel. 13647 */ 13648 #ifdef _LP64 13649 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13650 min(hba_cdb_limit, SD_CDB_GROUP4); 13651 #else 13652 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13653 min(hba_cdb_limit, SD_CDB_GROUP1); 13654 #endif 13655 13656 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13657 ? sizeof (struct scsi_arq_status) : 1); 13658 if (!ISCD(un)) 13659 un->un_cmd_timeout = (ushort_t)sd_io_time; 13660 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13661 } 13662 13663 13664 /* 13665 * Function: sd_initpkt_for_buf 13666 * 13667 * Description: Allocate and initialize for transport a scsi_pkt struct, 13668 * based upon the info specified in the given buf struct. 13669 * 13670 * Assumes the xb_blkno in the request is absolute (ie, 13671 * relative to the start of the device (NOT partition!). 13672 * Also assumes that the request is using the native block 13673 * size of the device (as returned by the READ CAPACITY 13674 * command). 13675 * 13676 * Return Code: SD_PKT_ALLOC_SUCCESS 13677 * SD_PKT_ALLOC_FAILURE 13678 * SD_PKT_ALLOC_FAILURE_NO_DMA 13679 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13680 * 13681 * Context: Kernel thread and may be called from software interrupt context 13682 * as part of a sdrunout callback. This function may not block or 13683 * call routines that block 13684 */ 13685 13686 static int 13687 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13688 { 13689 struct sd_xbuf *xp; 13690 struct scsi_pkt *pktp = NULL; 13691 struct sd_lun *un; 13692 size_t blockcount; 13693 daddr_t startblock; 13694 int rval; 13695 int cmd_flags; 13696 13697 ASSERT(bp != NULL); 13698 ASSERT(pktpp != NULL); 13699 xp = SD_GET_XBUF(bp); 13700 ASSERT(xp != NULL); 13701 un = SD_GET_UN(bp); 13702 ASSERT(un != NULL); 13703 ASSERT(mutex_owned(SD_MUTEX(un))); 13704 ASSERT(bp->b_resid == 0); 13705 13706 SD_TRACE(SD_LOG_IO_CORE, un, 13707 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13708 13709 mutex_exit(SD_MUTEX(un)); 13710 13711 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13712 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13713 /* 13714 * Already have a scsi_pkt -- just need DMA resources. 13715 * We must recompute the CDB in case the mapping returns 13716 * a nonzero pkt_resid. 13717 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13718 * that is being retried, the unmap/remap of the DMA resouces 13719 * will result in the entire transfer starting over again 13720 * from the very first block. 13721 */ 13722 ASSERT(xp->xb_pktp != NULL); 13723 pktp = xp->xb_pktp; 13724 } else { 13725 pktp = NULL; 13726 } 13727 #endif /* __i386 || __amd64 */ 13728 13729 startblock = xp->xb_blkno; /* Absolute block num. */ 13730 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13731 13732 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13733 13734 /* 13735 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13736 * call scsi_init_pkt, and build the CDB. 13737 */ 13738 rval = sd_setup_rw_pkt(un, &pktp, bp, 13739 cmd_flags, sdrunout, (caddr_t)un, 13740 startblock, blockcount); 13741 13742 if (rval == 0) { 13743 /* 13744 * Success. 13745 * 13746 * If partial DMA is being used and required for this transfer. 13747 * set it up here. 13748 */ 13749 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13750 (pktp->pkt_resid != 0)) { 13751 13752 /* 13753 * Save the CDB length and pkt_resid for the 13754 * next xfer 13755 */ 13756 xp->xb_dma_resid = pktp->pkt_resid; 13757 13758 /* rezero resid */ 13759 pktp->pkt_resid = 0; 13760 13761 } else { 13762 xp->xb_dma_resid = 0; 13763 } 13764 13765 pktp->pkt_flags = un->un_tagflags; 13766 pktp->pkt_time = un->un_cmd_timeout; 13767 pktp->pkt_comp = sdintr; 13768 13769 pktp->pkt_private = bp; 13770 *pktpp = pktp; 13771 13772 SD_TRACE(SD_LOG_IO_CORE, un, 13773 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13774 13775 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13776 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13777 #endif 13778 13779 mutex_enter(SD_MUTEX(un)); 13780 return (SD_PKT_ALLOC_SUCCESS); 13781 13782 } 13783 13784 /* 13785 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13786 * from sd_setup_rw_pkt. 13787 */ 13788 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13789 13790 if (rval == SD_PKT_ALLOC_FAILURE) { 13791 *pktpp = NULL; 13792 /* 13793 * Set the driver state to RWAIT to indicate the driver 13794 * is waiting on resource allocations. The driver will not 13795 * suspend, pm_suspend, or detatch while the state is RWAIT. 13796 */ 13797 mutex_enter(SD_MUTEX(un)); 13798 New_state(un, SD_STATE_RWAIT); 13799 13800 SD_ERROR(SD_LOG_IO_CORE, un, 13801 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13802 13803 if ((bp->b_flags & B_ERROR) != 0) { 13804 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13805 } 13806 return (SD_PKT_ALLOC_FAILURE); 13807 } else { 13808 /* 13809 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13810 * 13811 * This should never happen. Maybe someone messed with the 13812 * kernel's minphys? 13813 */ 13814 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13815 "Request rejected: too large for CDB: " 13816 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13817 SD_ERROR(SD_LOG_IO_CORE, un, 13818 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13819 mutex_enter(SD_MUTEX(un)); 13820 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13821 13822 } 13823 } 13824 13825 13826 /* 13827 * Function: sd_destroypkt_for_buf 13828 * 13829 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13830 * 13831 * Context: Kernel thread or interrupt context 13832 */ 13833 13834 static void 13835 sd_destroypkt_for_buf(struct buf *bp) 13836 { 13837 ASSERT(bp != NULL); 13838 ASSERT(SD_GET_UN(bp) != NULL); 13839 13840 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13841 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13842 13843 ASSERT(SD_GET_PKTP(bp) != NULL); 13844 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13845 13846 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13847 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13848 } 13849 13850 /* 13851 * Function: sd_setup_rw_pkt 13852 * 13853 * Description: Determines appropriate CDB group for the requested LBA 13854 * and transfer length, calls scsi_init_pkt, and builds 13855 * the CDB. Do not use for partial DMA transfers except 13856 * for the initial transfer since the CDB size must 13857 * remain constant. 13858 * 13859 * Context: Kernel thread and may be called from software interrupt 13860 * context as part of a sdrunout callback. This function may not 13861 * block or call routines that block 13862 */ 13863 13864 13865 int 13866 sd_setup_rw_pkt(struct sd_lun *un, 13867 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13868 int (*callback)(caddr_t), caddr_t callback_arg, 13869 diskaddr_t lba, uint32_t blockcount) 13870 { 13871 struct scsi_pkt *return_pktp; 13872 union scsi_cdb *cdbp; 13873 struct sd_cdbinfo *cp = NULL; 13874 int i; 13875 13876 /* 13877 * See which size CDB to use, based upon the request. 13878 */ 13879 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13880 13881 /* 13882 * Check lba and block count against sd_cdbtab limits. 13883 * In the partial DMA case, we have to use the same size 13884 * CDB for all the transfers. Check lba + blockcount 13885 * against the max LBA so we know that segment of the 13886 * transfer can use the CDB we select. 13887 */ 13888 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13889 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13890 13891 /* 13892 * The command will fit into the CDB type 13893 * specified by sd_cdbtab[i]. 13894 */ 13895 cp = sd_cdbtab + i; 13896 13897 /* 13898 * Call scsi_init_pkt so we can fill in the 13899 * CDB. 13900 */ 13901 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13902 bp, cp->sc_grpcode, un->un_status_len, 0, 13903 flags, callback, callback_arg); 13904 13905 if (return_pktp != NULL) { 13906 13907 /* 13908 * Return new value of pkt 13909 */ 13910 *pktpp = return_pktp; 13911 13912 /* 13913 * To be safe, zero the CDB insuring there is 13914 * no leftover data from a previous command. 13915 */ 13916 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13917 13918 /* 13919 * Handle partial DMA mapping 13920 */ 13921 if (return_pktp->pkt_resid != 0) { 13922 13923 /* 13924 * Not going to xfer as many blocks as 13925 * originally expected 13926 */ 13927 blockcount -= 13928 SD_BYTES2TGTBLOCKS(un, 13929 return_pktp->pkt_resid); 13930 } 13931 13932 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13933 13934 /* 13935 * Set command byte based on the CDB 13936 * type we matched. 13937 */ 13938 cdbp->scc_cmd = cp->sc_grpmask | 13939 ((bp->b_flags & B_READ) ? 13940 SCMD_READ : SCMD_WRITE); 13941 13942 SD_FILL_SCSI1_LUN(un, return_pktp); 13943 13944 /* 13945 * Fill in LBA and length 13946 */ 13947 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13948 (cp->sc_grpcode == CDB_GROUP4) || 13949 (cp->sc_grpcode == CDB_GROUP0) || 13950 (cp->sc_grpcode == CDB_GROUP5)); 13951 13952 if (cp->sc_grpcode == CDB_GROUP1) { 13953 FORMG1ADDR(cdbp, lba); 13954 FORMG1COUNT(cdbp, blockcount); 13955 return (0); 13956 } else if (cp->sc_grpcode == CDB_GROUP4) { 13957 FORMG4LONGADDR(cdbp, lba); 13958 FORMG4COUNT(cdbp, blockcount); 13959 return (0); 13960 } else if (cp->sc_grpcode == CDB_GROUP0) { 13961 FORMG0ADDR(cdbp, lba); 13962 FORMG0COUNT(cdbp, blockcount); 13963 return (0); 13964 } else if (cp->sc_grpcode == CDB_GROUP5) { 13965 FORMG5ADDR(cdbp, lba); 13966 FORMG5COUNT(cdbp, blockcount); 13967 return (0); 13968 } 13969 13970 /* 13971 * It should be impossible to not match one 13972 * of the CDB types above, so we should never 13973 * reach this point. Set the CDB command byte 13974 * to test-unit-ready to avoid writing 13975 * to somewhere we don't intend. 13976 */ 13977 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13978 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13979 } else { 13980 /* 13981 * Couldn't get scsi_pkt 13982 */ 13983 return (SD_PKT_ALLOC_FAILURE); 13984 } 13985 } 13986 } 13987 13988 /* 13989 * None of the available CDB types were suitable. This really 13990 * should never happen: on a 64 bit system we support 13991 * READ16/WRITE16 which will hold an entire 64 bit disk address 13992 * and on a 32 bit system we will refuse to bind to a device 13993 * larger than 2TB so addresses will never be larger than 32 bits. 13994 */ 13995 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13996 } 13997 13998 /* 13999 * Function: sd_setup_next_rw_pkt 14000 * 14001 * Description: Setup packet for partial DMA transfers, except for the 14002 * initial transfer. sd_setup_rw_pkt should be used for 14003 * the initial transfer. 14004 * 14005 * Context: Kernel thread and may be called from interrupt context. 14006 */ 14007 14008 int 14009 sd_setup_next_rw_pkt(struct sd_lun *un, 14010 struct scsi_pkt *pktp, struct buf *bp, 14011 diskaddr_t lba, uint32_t blockcount) 14012 { 14013 uchar_t com; 14014 union scsi_cdb *cdbp; 14015 uchar_t cdb_group_id; 14016 14017 ASSERT(pktp != NULL); 14018 ASSERT(pktp->pkt_cdbp != NULL); 14019 14020 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 14021 com = cdbp->scc_cmd; 14022 cdb_group_id = CDB_GROUPID(com); 14023 14024 ASSERT((cdb_group_id == CDB_GROUPID_0) || 14025 (cdb_group_id == CDB_GROUPID_1) || 14026 (cdb_group_id == CDB_GROUPID_4) || 14027 (cdb_group_id == CDB_GROUPID_5)); 14028 14029 /* 14030 * Move pkt to the next portion of the xfer. 14031 * func is NULL_FUNC so we do not have to release 14032 * the disk mutex here. 14033 */ 14034 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 14035 NULL_FUNC, NULL) == pktp) { 14036 /* Success. Handle partial DMA */ 14037 if (pktp->pkt_resid != 0) { 14038 blockcount -= 14039 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 14040 } 14041 14042 cdbp->scc_cmd = com; 14043 SD_FILL_SCSI1_LUN(un, pktp); 14044 if (cdb_group_id == CDB_GROUPID_1) { 14045 FORMG1ADDR(cdbp, lba); 14046 FORMG1COUNT(cdbp, blockcount); 14047 return (0); 14048 } else if (cdb_group_id == CDB_GROUPID_4) { 14049 FORMG4LONGADDR(cdbp, lba); 14050 FORMG4COUNT(cdbp, blockcount); 14051 return (0); 14052 } else if (cdb_group_id == CDB_GROUPID_0) { 14053 FORMG0ADDR(cdbp, lba); 14054 FORMG0COUNT(cdbp, blockcount); 14055 return (0); 14056 } else if (cdb_group_id == CDB_GROUPID_5) { 14057 FORMG5ADDR(cdbp, lba); 14058 FORMG5COUNT(cdbp, blockcount); 14059 return (0); 14060 } 14061 14062 /* Unreachable */ 14063 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14064 } 14065 14066 /* 14067 * Error setting up next portion of cmd transfer. 14068 * Something is definitely very wrong and this 14069 * should not happen. 14070 */ 14071 return (SD_PKT_ALLOC_FAILURE); 14072 } 14073 14074 /* 14075 * Function: sd_initpkt_for_uscsi 14076 * 14077 * Description: Allocate and initialize for transport a scsi_pkt struct, 14078 * based upon the info specified in the given uscsi_cmd struct. 14079 * 14080 * Return Code: SD_PKT_ALLOC_SUCCESS 14081 * SD_PKT_ALLOC_FAILURE 14082 * SD_PKT_ALLOC_FAILURE_NO_DMA 14083 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14084 * 14085 * Context: Kernel thread and may be called from software interrupt context 14086 * as part of a sdrunout callback. This function may not block or 14087 * call routines that block 14088 */ 14089 14090 static int 14091 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14092 { 14093 struct uscsi_cmd *uscmd; 14094 struct sd_xbuf *xp; 14095 struct scsi_pkt *pktp; 14096 struct sd_lun *un; 14097 uint32_t flags = 0; 14098 14099 ASSERT(bp != NULL); 14100 ASSERT(pktpp != NULL); 14101 xp = SD_GET_XBUF(bp); 14102 ASSERT(xp != NULL); 14103 un = SD_GET_UN(bp); 14104 ASSERT(un != NULL); 14105 ASSERT(mutex_owned(SD_MUTEX(un))); 14106 14107 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14108 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14109 ASSERT(uscmd != NULL); 14110 14111 SD_TRACE(SD_LOG_IO_CORE, un, 14112 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14113 14114 /* 14115 * Allocate the scsi_pkt for the command. 14116 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14117 * during scsi_init_pkt time and will continue to use the 14118 * same path as long as the same scsi_pkt is used without 14119 * intervening scsi_dma_free(). Since uscsi command does 14120 * not call scsi_dmafree() before retry failed command, it 14121 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14122 * set such that scsi_vhci can use other available path for 14123 * retry. Besides, ucsci command does not allow DMA breakup, 14124 * so there is no need to set PKT_DMA_PARTIAL flag. 14125 */ 14126 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14127 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14128 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14129 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 14130 - sizeof (struct scsi_extended_sense)), 0, 14131 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 14132 sdrunout, (caddr_t)un); 14133 } else { 14134 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14135 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14136 sizeof (struct scsi_arq_status), 0, 14137 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14138 sdrunout, (caddr_t)un); 14139 } 14140 14141 if (pktp == NULL) { 14142 *pktpp = NULL; 14143 /* 14144 * Set the driver state to RWAIT to indicate the driver 14145 * is waiting on resource allocations. The driver will not 14146 * suspend, pm_suspend, or detatch while the state is RWAIT. 14147 */ 14148 New_state(un, SD_STATE_RWAIT); 14149 14150 SD_ERROR(SD_LOG_IO_CORE, un, 14151 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14152 14153 if ((bp->b_flags & B_ERROR) != 0) { 14154 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14155 } 14156 return (SD_PKT_ALLOC_FAILURE); 14157 } 14158 14159 /* 14160 * We do not do DMA breakup for USCSI commands, so return failure 14161 * here if all the needed DMA resources were not allocated. 14162 */ 14163 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14164 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14165 scsi_destroy_pkt(pktp); 14166 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14167 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14168 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14169 } 14170 14171 /* Init the cdb from the given uscsi struct */ 14172 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14173 uscmd->uscsi_cdb[0], 0, 0, 0); 14174 14175 SD_FILL_SCSI1_LUN(un, pktp); 14176 14177 /* 14178 * Set up the optional USCSI flags. See the uscsi (7I) man page 14179 * for listing of the supported flags. 14180 */ 14181 14182 if (uscmd->uscsi_flags & USCSI_SILENT) { 14183 flags |= FLAG_SILENT; 14184 } 14185 14186 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14187 flags |= FLAG_DIAGNOSE; 14188 } 14189 14190 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14191 flags |= FLAG_ISOLATE; 14192 } 14193 14194 if (un->un_f_is_fibre == FALSE) { 14195 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14196 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14197 } 14198 } 14199 14200 /* 14201 * Set the pkt flags here so we save time later. 14202 * Note: These flags are NOT in the uscsi man page!!! 14203 */ 14204 if (uscmd->uscsi_flags & USCSI_HEAD) { 14205 flags |= FLAG_HEAD; 14206 } 14207 14208 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14209 flags |= FLAG_NOINTR; 14210 } 14211 14212 /* 14213 * For tagged queueing, things get a bit complicated. 14214 * Check first for head of queue and last for ordered queue. 14215 * If neither head nor order, use the default driver tag flags. 14216 */ 14217 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14218 if (uscmd->uscsi_flags & USCSI_HTAG) { 14219 flags |= FLAG_HTAG; 14220 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14221 flags |= FLAG_OTAG; 14222 } else { 14223 flags |= un->un_tagflags & FLAG_TAGMASK; 14224 } 14225 } 14226 14227 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14228 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14229 } 14230 14231 pktp->pkt_flags = flags; 14232 14233 /* Transfer uscsi information to scsi_pkt */ 14234 (void) scsi_uscsi_pktinit(uscmd, pktp); 14235 14236 /* Copy the caller's CDB into the pkt... */ 14237 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14238 14239 if (uscmd->uscsi_timeout == 0) { 14240 pktp->pkt_time = un->un_uscsi_timeout; 14241 } else { 14242 pktp->pkt_time = uscmd->uscsi_timeout; 14243 } 14244 14245 /* need it later to identify USCSI request in sdintr */ 14246 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14247 14248 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14249 14250 pktp->pkt_private = bp; 14251 pktp->pkt_comp = sdintr; 14252 *pktpp = pktp; 14253 14254 SD_TRACE(SD_LOG_IO_CORE, un, 14255 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14256 14257 return (SD_PKT_ALLOC_SUCCESS); 14258 } 14259 14260 14261 /* 14262 * Function: sd_destroypkt_for_uscsi 14263 * 14264 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14265 * IOs.. Also saves relevant info into the associated uscsi_cmd 14266 * struct. 14267 * 14268 * Context: May be called under interrupt context 14269 */ 14270 14271 static void 14272 sd_destroypkt_for_uscsi(struct buf *bp) 14273 { 14274 struct uscsi_cmd *uscmd; 14275 struct sd_xbuf *xp; 14276 struct scsi_pkt *pktp; 14277 struct sd_lun *un; 14278 struct sd_uscsi_info *suip; 14279 14280 ASSERT(bp != NULL); 14281 xp = SD_GET_XBUF(bp); 14282 ASSERT(xp != NULL); 14283 un = SD_GET_UN(bp); 14284 ASSERT(un != NULL); 14285 ASSERT(!mutex_owned(SD_MUTEX(un))); 14286 pktp = SD_GET_PKTP(bp); 14287 ASSERT(pktp != NULL); 14288 14289 SD_TRACE(SD_LOG_IO_CORE, un, 14290 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14291 14292 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14293 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14294 ASSERT(uscmd != NULL); 14295 14296 /* Save the status and the residual into the uscsi_cmd struct */ 14297 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14298 uscmd->uscsi_resid = bp->b_resid; 14299 14300 /* Transfer scsi_pkt information to uscsi */ 14301 (void) scsi_uscsi_pktfini(pktp, uscmd); 14302 14303 /* 14304 * If enabled, copy any saved sense data into the area specified 14305 * by the uscsi command. 14306 */ 14307 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14308 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14309 /* 14310 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14311 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14312 */ 14313 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14314 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14315 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14316 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14317 MAX_SENSE_LENGTH); 14318 } else { 14319 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14320 SENSE_LENGTH); 14321 } 14322 } 14323 /* 14324 * The following assignments are for SCSI FMA. 14325 */ 14326 ASSERT(xp->xb_private != NULL); 14327 suip = (struct sd_uscsi_info *)xp->xb_private; 14328 suip->ui_pkt_reason = pktp->pkt_reason; 14329 suip->ui_pkt_state = pktp->pkt_state; 14330 suip->ui_pkt_statistics = pktp->pkt_statistics; 14331 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 14332 14333 /* We are done with the scsi_pkt; free it now */ 14334 ASSERT(SD_GET_PKTP(bp) != NULL); 14335 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14336 14337 SD_TRACE(SD_LOG_IO_CORE, un, 14338 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14339 } 14340 14341 14342 /* 14343 * Function: sd_bioclone_alloc 14344 * 14345 * Description: Allocate a buf(9S) and init it as per the given buf 14346 * and the various arguments. The associated sd_xbuf 14347 * struct is (nearly) duplicated. The struct buf *bp 14348 * argument is saved in new_xp->xb_private. 14349 * 14350 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14351 * datalen - size of data area for the shadow bp 14352 * blkno - starting LBA 14353 * func - function pointer for b_iodone in the shadow buf. (May 14354 * be NULL if none.) 14355 * 14356 * Return Code: Pointer to allocates buf(9S) struct 14357 * 14358 * Context: Can sleep. 14359 */ 14360 14361 static struct buf * 14362 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14363 daddr_t blkno, int (*func)(struct buf *)) 14364 { 14365 struct sd_lun *un; 14366 struct sd_xbuf *xp; 14367 struct sd_xbuf *new_xp; 14368 struct buf *new_bp; 14369 14370 ASSERT(bp != NULL); 14371 xp = SD_GET_XBUF(bp); 14372 ASSERT(xp != NULL); 14373 un = SD_GET_UN(bp); 14374 ASSERT(un != NULL); 14375 ASSERT(!mutex_owned(SD_MUTEX(un))); 14376 14377 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14378 NULL, KM_SLEEP); 14379 14380 new_bp->b_lblkno = blkno; 14381 14382 /* 14383 * Allocate an xbuf for the shadow bp and copy the contents of the 14384 * original xbuf into it. 14385 */ 14386 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14387 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14388 14389 /* 14390 * The given bp is automatically saved in the xb_private member 14391 * of the new xbuf. Callers are allowed to depend on this. 14392 */ 14393 new_xp->xb_private = bp; 14394 14395 new_bp->b_private = new_xp; 14396 14397 return (new_bp); 14398 } 14399 14400 /* 14401 * Function: sd_shadow_buf_alloc 14402 * 14403 * Description: Allocate a buf(9S) and init it as per the given buf 14404 * and the various arguments. The associated sd_xbuf 14405 * struct is (nearly) duplicated. The struct buf *bp 14406 * argument is saved in new_xp->xb_private. 14407 * 14408 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14409 * datalen - size of data area for the shadow bp 14410 * bflags - B_READ or B_WRITE (pseudo flag) 14411 * blkno - starting LBA 14412 * func - function pointer for b_iodone in the shadow buf. (May 14413 * be NULL if none.) 14414 * 14415 * Return Code: Pointer to allocates buf(9S) struct 14416 * 14417 * Context: Can sleep. 14418 */ 14419 14420 static struct buf * 14421 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14422 daddr_t blkno, int (*func)(struct buf *)) 14423 { 14424 struct sd_lun *un; 14425 struct sd_xbuf *xp; 14426 struct sd_xbuf *new_xp; 14427 struct buf *new_bp; 14428 14429 ASSERT(bp != NULL); 14430 xp = SD_GET_XBUF(bp); 14431 ASSERT(xp != NULL); 14432 un = SD_GET_UN(bp); 14433 ASSERT(un != NULL); 14434 ASSERT(!mutex_owned(SD_MUTEX(un))); 14435 14436 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14437 bp_mapin(bp); 14438 } 14439 14440 bflags &= (B_READ | B_WRITE); 14441 #if defined(__i386) || defined(__amd64) 14442 new_bp = getrbuf(KM_SLEEP); 14443 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14444 new_bp->b_bcount = datalen; 14445 new_bp->b_flags = bflags | 14446 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14447 #else 14448 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14449 datalen, bflags, SLEEP_FUNC, NULL); 14450 #endif 14451 new_bp->av_forw = NULL; 14452 new_bp->av_back = NULL; 14453 new_bp->b_dev = bp->b_dev; 14454 new_bp->b_blkno = blkno; 14455 new_bp->b_iodone = func; 14456 new_bp->b_edev = bp->b_edev; 14457 new_bp->b_resid = 0; 14458 14459 /* We need to preserve the B_FAILFAST flag */ 14460 if (bp->b_flags & B_FAILFAST) { 14461 new_bp->b_flags |= B_FAILFAST; 14462 } 14463 14464 /* 14465 * Allocate an xbuf for the shadow bp and copy the contents of the 14466 * original xbuf into it. 14467 */ 14468 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14469 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14470 14471 /* Need later to copy data between the shadow buf & original buf! */ 14472 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14473 14474 /* 14475 * The given bp is automatically saved in the xb_private member 14476 * of the new xbuf. Callers are allowed to depend on this. 14477 */ 14478 new_xp->xb_private = bp; 14479 14480 new_bp->b_private = new_xp; 14481 14482 return (new_bp); 14483 } 14484 14485 /* 14486 * Function: sd_bioclone_free 14487 * 14488 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14489 * in the larger than partition operation. 14490 * 14491 * Context: May be called under interrupt context 14492 */ 14493 14494 static void 14495 sd_bioclone_free(struct buf *bp) 14496 { 14497 struct sd_xbuf *xp; 14498 14499 ASSERT(bp != NULL); 14500 xp = SD_GET_XBUF(bp); 14501 ASSERT(xp != NULL); 14502 14503 /* 14504 * Call bp_mapout() before freeing the buf, in case a lower 14505 * layer or HBA had done a bp_mapin(). we must do this here 14506 * as we are the "originator" of the shadow buf. 14507 */ 14508 bp_mapout(bp); 14509 14510 /* 14511 * Null out b_iodone before freeing the bp, to ensure that the driver 14512 * never gets confused by a stale value in this field. (Just a little 14513 * extra defensiveness here.) 14514 */ 14515 bp->b_iodone = NULL; 14516 14517 freerbuf(bp); 14518 14519 kmem_free(xp, sizeof (struct sd_xbuf)); 14520 } 14521 14522 /* 14523 * Function: sd_shadow_buf_free 14524 * 14525 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14526 * 14527 * Context: May be called under interrupt context 14528 */ 14529 14530 static void 14531 sd_shadow_buf_free(struct buf *bp) 14532 { 14533 struct sd_xbuf *xp; 14534 14535 ASSERT(bp != NULL); 14536 xp = SD_GET_XBUF(bp); 14537 ASSERT(xp != NULL); 14538 14539 #if defined(__sparc) 14540 /* 14541 * Call bp_mapout() before freeing the buf, in case a lower 14542 * layer or HBA had done a bp_mapin(). we must do this here 14543 * as we are the "originator" of the shadow buf. 14544 */ 14545 bp_mapout(bp); 14546 #endif 14547 14548 /* 14549 * Null out b_iodone before freeing the bp, to ensure that the driver 14550 * never gets confused by a stale value in this field. (Just a little 14551 * extra defensiveness here.) 14552 */ 14553 bp->b_iodone = NULL; 14554 14555 #if defined(__i386) || defined(__amd64) 14556 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14557 freerbuf(bp); 14558 #else 14559 scsi_free_consistent_buf(bp); 14560 #endif 14561 14562 kmem_free(xp, sizeof (struct sd_xbuf)); 14563 } 14564 14565 14566 /* 14567 * Function: sd_print_transport_rejected_message 14568 * 14569 * Description: This implements the ludicrously complex rules for printing 14570 * a "transport rejected" message. This is to address the 14571 * specific problem of having a flood of this error message 14572 * produced when a failover occurs. 14573 * 14574 * Context: Any. 14575 */ 14576 14577 static void 14578 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14579 int code) 14580 { 14581 ASSERT(un != NULL); 14582 ASSERT(mutex_owned(SD_MUTEX(un))); 14583 ASSERT(xp != NULL); 14584 14585 /* 14586 * Print the "transport rejected" message under the following 14587 * conditions: 14588 * 14589 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14590 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14591 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14592 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14593 * scsi_transport(9F) (which indicates that the target might have 14594 * gone off-line). This uses the un->un_tran_fatal_count 14595 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14596 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14597 * from scsi_transport(). 14598 * 14599 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14600 * the preceeding cases in order for the message to be printed. 14601 */ 14602 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) && 14603 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) { 14604 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14605 (code != TRAN_FATAL_ERROR) || 14606 (un->un_tran_fatal_count == 1)) { 14607 switch (code) { 14608 case TRAN_BADPKT: 14609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14610 "transport rejected bad packet\n"); 14611 break; 14612 case TRAN_FATAL_ERROR: 14613 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14614 "transport rejected fatal error\n"); 14615 break; 14616 default: 14617 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14618 "transport rejected (%d)\n", code); 14619 break; 14620 } 14621 } 14622 } 14623 } 14624 14625 14626 /* 14627 * Function: sd_add_buf_to_waitq 14628 * 14629 * Description: Add the given buf(9S) struct to the wait queue for the 14630 * instance. If sorting is enabled, then the buf is added 14631 * to the queue via an elevator sort algorithm (a la 14632 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14633 * If sorting is not enabled, then the buf is just added 14634 * to the end of the wait queue. 14635 * 14636 * Return Code: void 14637 * 14638 * Context: Does not sleep/block, therefore technically can be called 14639 * from any context. However if sorting is enabled then the 14640 * execution time is indeterminate, and may take long if 14641 * the wait queue grows large. 14642 */ 14643 14644 static void 14645 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14646 { 14647 struct buf *ap; 14648 14649 ASSERT(bp != NULL); 14650 ASSERT(un != NULL); 14651 ASSERT(mutex_owned(SD_MUTEX(un))); 14652 14653 /* If the queue is empty, add the buf as the only entry & return. */ 14654 if (un->un_waitq_headp == NULL) { 14655 ASSERT(un->un_waitq_tailp == NULL); 14656 un->un_waitq_headp = un->un_waitq_tailp = bp; 14657 bp->av_forw = NULL; 14658 return; 14659 } 14660 14661 ASSERT(un->un_waitq_tailp != NULL); 14662 14663 /* 14664 * If sorting is disabled, just add the buf to the tail end of 14665 * the wait queue and return. 14666 */ 14667 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) { 14668 un->un_waitq_tailp->av_forw = bp; 14669 un->un_waitq_tailp = bp; 14670 bp->av_forw = NULL; 14671 return; 14672 } 14673 14674 /* 14675 * Sort thru the list of requests currently on the wait queue 14676 * and add the new buf request at the appropriate position. 14677 * 14678 * The un->un_waitq_headp is an activity chain pointer on which 14679 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14680 * first queue holds those requests which are positioned after 14681 * the current SD_GET_BLKNO() (in the first request); the second holds 14682 * requests which came in after their SD_GET_BLKNO() number was passed. 14683 * Thus we implement a one way scan, retracting after reaching 14684 * the end of the drive to the first request on the second 14685 * queue, at which time it becomes the first queue. 14686 * A one-way scan is natural because of the way UNIX read-ahead 14687 * blocks are allocated. 14688 * 14689 * If we lie after the first request, then we must locate the 14690 * second request list and add ourselves to it. 14691 */ 14692 ap = un->un_waitq_headp; 14693 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14694 while (ap->av_forw != NULL) { 14695 /* 14696 * Look for an "inversion" in the (normally 14697 * ascending) block numbers. This indicates 14698 * the start of the second request list. 14699 */ 14700 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14701 /* 14702 * Search the second request list for the 14703 * first request at a larger block number. 14704 * We go before that; however if there is 14705 * no such request, we go at the end. 14706 */ 14707 do { 14708 if (SD_GET_BLKNO(bp) < 14709 SD_GET_BLKNO(ap->av_forw)) { 14710 goto insert; 14711 } 14712 ap = ap->av_forw; 14713 } while (ap->av_forw != NULL); 14714 goto insert; /* after last */ 14715 } 14716 ap = ap->av_forw; 14717 } 14718 14719 /* 14720 * No inversions... we will go after the last, and 14721 * be the first request in the second request list. 14722 */ 14723 goto insert; 14724 } 14725 14726 /* 14727 * Request is at/after the current request... 14728 * sort in the first request list. 14729 */ 14730 while (ap->av_forw != NULL) { 14731 /* 14732 * We want to go after the current request (1) if 14733 * there is an inversion after it (i.e. it is the end 14734 * of the first request list), or (2) if the next 14735 * request is a larger block no. than our request. 14736 */ 14737 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14738 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14739 goto insert; 14740 } 14741 ap = ap->av_forw; 14742 } 14743 14744 /* 14745 * Neither a second list nor a larger request, therefore 14746 * we go at the end of the first list (which is the same 14747 * as the end of the whole schebang). 14748 */ 14749 insert: 14750 bp->av_forw = ap->av_forw; 14751 ap->av_forw = bp; 14752 14753 /* 14754 * If we inserted onto the tail end of the waitq, make sure the 14755 * tail pointer is updated. 14756 */ 14757 if (ap == un->un_waitq_tailp) { 14758 un->un_waitq_tailp = bp; 14759 } 14760 } 14761 14762 14763 /* 14764 * Function: sd_start_cmds 14765 * 14766 * Description: Remove and transport cmds from the driver queues. 14767 * 14768 * Arguments: un - pointer to the unit (soft state) struct for the target. 14769 * 14770 * immed_bp - ptr to a buf to be transported immediately. Only 14771 * the immed_bp is transported; bufs on the waitq are not 14772 * processed and the un_retry_bp is not checked. If immed_bp is 14773 * NULL, then normal queue processing is performed. 14774 * 14775 * Context: May be called from kernel thread context, interrupt context, 14776 * or runout callback context. This function may not block or 14777 * call routines that block. 14778 */ 14779 14780 static void 14781 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14782 { 14783 struct sd_xbuf *xp; 14784 struct buf *bp; 14785 void (*statp)(kstat_io_t *); 14786 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14787 void (*saved_statp)(kstat_io_t *); 14788 #endif 14789 int rval; 14790 struct sd_fm_internal *sfip = NULL; 14791 14792 ASSERT(un != NULL); 14793 ASSERT(mutex_owned(SD_MUTEX(un))); 14794 ASSERT(un->un_ncmds_in_transport >= 0); 14795 ASSERT(un->un_throttle >= 0); 14796 14797 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14798 14799 do { 14800 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14801 saved_statp = NULL; 14802 #endif 14803 14804 /* 14805 * If we are syncing or dumping, fail the command to 14806 * avoid recursively calling back into scsi_transport(). 14807 * The dump I/O itself uses a separate code path so this 14808 * only prevents non-dump I/O from being sent while dumping. 14809 * File system sync takes place before dumping begins. 14810 * During panic, filesystem I/O is allowed provided 14811 * un_in_callback is <= 1. This is to prevent recursion 14812 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14813 * sd_start_cmds and so on. See panic.c for more information 14814 * about the states the system can be in during panic. 14815 */ 14816 if ((un->un_state == SD_STATE_DUMPING) || 14817 (ddi_in_panic() && (un->un_in_callback > 1))) { 14818 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14819 "sd_start_cmds: panicking\n"); 14820 goto exit; 14821 } 14822 14823 if ((bp = immed_bp) != NULL) { 14824 /* 14825 * We have a bp that must be transported immediately. 14826 * It's OK to transport the immed_bp here without doing 14827 * the throttle limit check because the immed_bp is 14828 * always used in a retry/recovery case. This means 14829 * that we know we are not at the throttle limit by 14830 * virtue of the fact that to get here we must have 14831 * already gotten a command back via sdintr(). This also 14832 * relies on (1) the command on un_retry_bp preventing 14833 * further commands from the waitq from being issued; 14834 * and (2) the code in sd_retry_command checking the 14835 * throttle limit before issuing a delayed or immediate 14836 * retry. This holds even if the throttle limit is 14837 * currently ratcheted down from its maximum value. 14838 */ 14839 statp = kstat_runq_enter; 14840 if (bp == un->un_retry_bp) { 14841 ASSERT((un->un_retry_statp == NULL) || 14842 (un->un_retry_statp == kstat_waitq_enter) || 14843 (un->un_retry_statp == 14844 kstat_runq_back_to_waitq)); 14845 /* 14846 * If the waitq kstat was incremented when 14847 * sd_set_retry_bp() queued this bp for a retry, 14848 * then we must set up statp so that the waitq 14849 * count will get decremented correctly below. 14850 * Also we must clear un->un_retry_statp to 14851 * ensure that we do not act on a stale value 14852 * in this field. 14853 */ 14854 if ((un->un_retry_statp == kstat_waitq_enter) || 14855 (un->un_retry_statp == 14856 kstat_runq_back_to_waitq)) { 14857 statp = kstat_waitq_to_runq; 14858 } 14859 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14860 saved_statp = un->un_retry_statp; 14861 #endif 14862 un->un_retry_statp = NULL; 14863 14864 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14865 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14866 "un_throttle:%d un_ncmds_in_transport:%d\n", 14867 un, un->un_retry_bp, un->un_throttle, 14868 un->un_ncmds_in_transport); 14869 } else { 14870 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14871 "processing priority bp:0x%p\n", bp); 14872 } 14873 14874 } else if ((bp = un->un_waitq_headp) != NULL) { 14875 /* 14876 * A command on the waitq is ready to go, but do not 14877 * send it if: 14878 * 14879 * (1) the throttle limit has been reached, or 14880 * (2) a retry is pending, or 14881 * (3) a START_STOP_UNIT callback pending, or 14882 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14883 * command is pending. 14884 * 14885 * For all of these conditions, IO processing will 14886 * restart after the condition is cleared. 14887 */ 14888 if (un->un_ncmds_in_transport >= un->un_throttle) { 14889 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14890 "sd_start_cmds: exiting, " 14891 "throttle limit reached!\n"); 14892 goto exit; 14893 } 14894 if (un->un_retry_bp != NULL) { 14895 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14896 "sd_start_cmds: exiting, retry pending!\n"); 14897 goto exit; 14898 } 14899 if (un->un_startstop_timeid != NULL) { 14900 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14901 "sd_start_cmds: exiting, " 14902 "START_STOP pending!\n"); 14903 goto exit; 14904 } 14905 if (un->un_direct_priority_timeid != NULL) { 14906 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14907 "sd_start_cmds: exiting, " 14908 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14909 goto exit; 14910 } 14911 14912 /* Dequeue the command */ 14913 un->un_waitq_headp = bp->av_forw; 14914 if (un->un_waitq_headp == NULL) { 14915 un->un_waitq_tailp = NULL; 14916 } 14917 bp->av_forw = NULL; 14918 statp = kstat_waitq_to_runq; 14919 SD_TRACE(SD_LOG_IO_CORE, un, 14920 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14921 14922 } else { 14923 /* No work to do so bail out now */ 14924 SD_TRACE(SD_LOG_IO_CORE, un, 14925 "sd_start_cmds: no more work, exiting!\n"); 14926 goto exit; 14927 } 14928 14929 /* 14930 * Reset the state to normal. This is the mechanism by which 14931 * the state transitions from either SD_STATE_RWAIT or 14932 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14933 * If state is SD_STATE_PM_CHANGING then this command is 14934 * part of the device power control and the state must 14935 * not be put back to normal. Doing so would would 14936 * allow new commands to proceed when they shouldn't, 14937 * the device may be going off. 14938 */ 14939 if ((un->un_state != SD_STATE_SUSPENDED) && 14940 (un->un_state != SD_STATE_PM_CHANGING)) { 14941 New_state(un, SD_STATE_NORMAL); 14942 } 14943 14944 xp = SD_GET_XBUF(bp); 14945 ASSERT(xp != NULL); 14946 14947 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14948 /* 14949 * Allocate the scsi_pkt if we need one, or attach DMA 14950 * resources if we have a scsi_pkt that needs them. The 14951 * latter should only occur for commands that are being 14952 * retried. 14953 */ 14954 if ((xp->xb_pktp == NULL) || 14955 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14956 #else 14957 if (xp->xb_pktp == NULL) { 14958 #endif 14959 /* 14960 * There is no scsi_pkt allocated for this buf. Call 14961 * the initpkt function to allocate & init one. 14962 * 14963 * The scsi_init_pkt runout callback functionality is 14964 * implemented as follows: 14965 * 14966 * 1) The initpkt function always calls 14967 * scsi_init_pkt(9F) with sdrunout specified as the 14968 * callback routine. 14969 * 2) A successful packet allocation is initialized and 14970 * the I/O is transported. 14971 * 3) The I/O associated with an allocation resource 14972 * failure is left on its queue to be retried via 14973 * runout or the next I/O. 14974 * 4) The I/O associated with a DMA error is removed 14975 * from the queue and failed with EIO. Processing of 14976 * the transport queues is also halted to be 14977 * restarted via runout or the next I/O. 14978 * 5) The I/O associated with a CDB size or packet 14979 * size error is removed from the queue and failed 14980 * with EIO. Processing of the transport queues is 14981 * continued. 14982 * 14983 * Note: there is no interface for canceling a runout 14984 * callback. To prevent the driver from detaching or 14985 * suspending while a runout is pending the driver 14986 * state is set to SD_STATE_RWAIT 14987 * 14988 * Note: using the scsi_init_pkt callback facility can 14989 * result in an I/O request persisting at the head of 14990 * the list which cannot be satisfied even after 14991 * multiple retries. In the future the driver may 14992 * implement some kind of maximum runout count before 14993 * failing an I/O. 14994 * 14995 * Note: the use of funcp below may seem superfluous, 14996 * but it helps warlock figure out the correct 14997 * initpkt function calls (see [s]sd.wlcmd). 14998 */ 14999 struct scsi_pkt *pktp; 15000 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 15001 15002 ASSERT(bp != un->un_rqs_bp); 15003 15004 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 15005 switch ((*funcp)(bp, &pktp)) { 15006 case SD_PKT_ALLOC_SUCCESS: 15007 xp->xb_pktp = pktp; 15008 SD_TRACE(SD_LOG_IO_CORE, un, 15009 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 15010 pktp); 15011 goto got_pkt; 15012 15013 case SD_PKT_ALLOC_FAILURE: 15014 /* 15015 * Temporary (hopefully) resource depletion. 15016 * Since retries and RQS commands always have a 15017 * scsi_pkt allocated, these cases should never 15018 * get here. So the only cases this needs to 15019 * handle is a bp from the waitq (which we put 15020 * back onto the waitq for sdrunout), or a bp 15021 * sent as an immed_bp (which we just fail). 15022 */ 15023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15024 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 15025 15026 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15027 15028 if (bp == immed_bp) { 15029 /* 15030 * If SD_XB_DMA_FREED is clear, then 15031 * this is a failure to allocate a 15032 * scsi_pkt, and we must fail the 15033 * command. 15034 */ 15035 if ((xp->xb_pkt_flags & 15036 SD_XB_DMA_FREED) == 0) { 15037 break; 15038 } 15039 15040 /* 15041 * If this immediate command is NOT our 15042 * un_retry_bp, then we must fail it. 15043 */ 15044 if (bp != un->un_retry_bp) { 15045 break; 15046 } 15047 15048 /* 15049 * We get here if this cmd is our 15050 * un_retry_bp that was DMAFREED, but 15051 * scsi_init_pkt() failed to reallocate 15052 * DMA resources when we attempted to 15053 * retry it. This can happen when an 15054 * mpxio failover is in progress, but 15055 * we don't want to just fail the 15056 * command in this case. 15057 * 15058 * Use timeout(9F) to restart it after 15059 * a 100ms delay. We don't want to 15060 * let sdrunout() restart it, because 15061 * sdrunout() is just supposed to start 15062 * commands that are sitting on the 15063 * wait queue. The un_retry_bp stays 15064 * set until the command completes, but 15065 * sdrunout can be called many times 15066 * before that happens. Since sdrunout 15067 * cannot tell if the un_retry_bp is 15068 * already in the transport, it could 15069 * end up calling scsi_transport() for 15070 * the un_retry_bp multiple times. 15071 * 15072 * Also: don't schedule the callback 15073 * if some other callback is already 15074 * pending. 15075 */ 15076 if (un->un_retry_statp == NULL) { 15077 /* 15078 * restore the kstat pointer to 15079 * keep kstat counts coherent 15080 * when we do retry the command. 15081 */ 15082 un->un_retry_statp = 15083 saved_statp; 15084 } 15085 15086 if ((un->un_startstop_timeid == NULL) && 15087 (un->un_retry_timeid == NULL) && 15088 (un->un_direct_priority_timeid == 15089 NULL)) { 15090 15091 un->un_retry_timeid = 15092 timeout( 15093 sd_start_retry_command, 15094 un, SD_RESTART_TIMEOUT); 15095 } 15096 goto exit; 15097 } 15098 15099 #else 15100 if (bp == immed_bp) { 15101 break; /* Just fail the command */ 15102 } 15103 #endif 15104 15105 /* Add the buf back to the head of the waitq */ 15106 bp->av_forw = un->un_waitq_headp; 15107 un->un_waitq_headp = bp; 15108 if (un->un_waitq_tailp == NULL) { 15109 un->un_waitq_tailp = bp; 15110 } 15111 goto exit; 15112 15113 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15114 /* 15115 * HBA DMA resource failure. Fail the command 15116 * and continue processing of the queues. 15117 */ 15118 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15119 "sd_start_cmds: " 15120 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15121 break; 15122 15123 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15124 /* 15125 * Note:x86: Partial DMA mapping not supported 15126 * for USCSI commands, and all the needed DMA 15127 * resources were not allocated. 15128 */ 15129 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15130 "sd_start_cmds: " 15131 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15132 break; 15133 15134 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15135 /* 15136 * Note:x86: Request cannot fit into CDB based 15137 * on lba and len. 15138 */ 15139 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15140 "sd_start_cmds: " 15141 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15142 break; 15143 15144 default: 15145 /* Should NEVER get here! */ 15146 panic("scsi_initpkt error"); 15147 /*NOTREACHED*/ 15148 } 15149 15150 /* 15151 * Fatal error in allocating a scsi_pkt for this buf. 15152 * Update kstats & return the buf with an error code. 15153 * We must use sd_return_failed_command_no_restart() to 15154 * avoid a recursive call back into sd_start_cmds(). 15155 * However this also means that we must keep processing 15156 * the waitq here in order to avoid stalling. 15157 */ 15158 if (statp == kstat_waitq_to_runq) { 15159 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15160 } 15161 sd_return_failed_command_no_restart(un, bp, EIO); 15162 if (bp == immed_bp) { 15163 /* immed_bp is gone by now, so clear this */ 15164 immed_bp = NULL; 15165 } 15166 continue; 15167 } 15168 got_pkt: 15169 if (bp == immed_bp) { 15170 /* goto the head of the class.... */ 15171 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15172 } 15173 15174 un->un_ncmds_in_transport++; 15175 SD_UPDATE_KSTATS(un, statp, bp); 15176 15177 /* 15178 * Call scsi_transport() to send the command to the target. 15179 * According to SCSA architecture, we must drop the mutex here 15180 * before calling scsi_transport() in order to avoid deadlock. 15181 * Note that the scsi_pkt's completion routine can be executed 15182 * (from interrupt context) even before the call to 15183 * scsi_transport() returns. 15184 */ 15185 SD_TRACE(SD_LOG_IO_CORE, un, 15186 "sd_start_cmds: calling scsi_transport()\n"); 15187 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15188 15189 mutex_exit(SD_MUTEX(un)); 15190 rval = scsi_transport(xp->xb_pktp); 15191 mutex_enter(SD_MUTEX(un)); 15192 15193 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15194 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15195 15196 switch (rval) { 15197 case TRAN_ACCEPT: 15198 /* Clear this with every pkt accepted by the HBA */ 15199 un->un_tran_fatal_count = 0; 15200 break; /* Success; try the next cmd (if any) */ 15201 15202 case TRAN_BUSY: 15203 un->un_ncmds_in_transport--; 15204 ASSERT(un->un_ncmds_in_transport >= 0); 15205 15206 /* 15207 * Don't retry request sense, the sense data 15208 * is lost when another request is sent. 15209 * Free up the rqs buf and retry 15210 * the original failed cmd. Update kstat. 15211 */ 15212 if (bp == un->un_rqs_bp) { 15213 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15214 bp = sd_mark_rqs_idle(un, xp); 15215 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15216 NULL, NULL, EIO, un->un_busy_timeout / 500, 15217 kstat_waitq_enter); 15218 goto exit; 15219 } 15220 15221 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15222 /* 15223 * Free the DMA resources for the scsi_pkt. This will 15224 * allow mpxio to select another path the next time 15225 * we call scsi_transport() with this scsi_pkt. 15226 * See sdintr() for the rationalization behind this. 15227 */ 15228 if ((un->un_f_is_fibre == TRUE) && 15229 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15230 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15231 scsi_dmafree(xp->xb_pktp); 15232 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15233 } 15234 #endif 15235 15236 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15237 /* 15238 * Commands that are SD_PATH_DIRECT_PRIORITY 15239 * are for error recovery situations. These do 15240 * not use the normal command waitq, so if they 15241 * get a TRAN_BUSY we cannot put them back onto 15242 * the waitq for later retry. One possible 15243 * problem is that there could already be some 15244 * other command on un_retry_bp that is waiting 15245 * for this one to complete, so we would be 15246 * deadlocked if we put this command back onto 15247 * the waitq for later retry (since un_retry_bp 15248 * must complete before the driver gets back to 15249 * commands on the waitq). 15250 * 15251 * To avoid deadlock we must schedule a callback 15252 * that will restart this command after a set 15253 * interval. This should keep retrying for as 15254 * long as the underlying transport keeps 15255 * returning TRAN_BUSY (just like for other 15256 * commands). Use the same timeout interval as 15257 * for the ordinary TRAN_BUSY retry. 15258 */ 15259 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15260 "sd_start_cmds: scsi_transport() returned " 15261 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15262 15263 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15264 un->un_direct_priority_timeid = 15265 timeout(sd_start_direct_priority_command, 15266 bp, un->un_busy_timeout / 500); 15267 15268 goto exit; 15269 } 15270 15271 /* 15272 * For TRAN_BUSY, we want to reduce the throttle value, 15273 * unless we are retrying a command. 15274 */ 15275 if (bp != un->un_retry_bp) { 15276 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15277 } 15278 15279 /* 15280 * Set up the bp to be tried again 10 ms later. 15281 * Note:x86: Is there a timeout value in the sd_lun 15282 * for this condition? 15283 */ 15284 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500, 15285 kstat_runq_back_to_waitq); 15286 goto exit; 15287 15288 case TRAN_FATAL_ERROR: 15289 un->un_tran_fatal_count++; 15290 /* FALLTHRU */ 15291 15292 case TRAN_BADPKT: 15293 default: 15294 un->un_ncmds_in_transport--; 15295 ASSERT(un->un_ncmds_in_transport >= 0); 15296 15297 /* 15298 * If this is our REQUEST SENSE command with a 15299 * transport error, we must get back the pointers 15300 * to the original buf, and mark the REQUEST 15301 * SENSE command as "available". 15302 */ 15303 if (bp == un->un_rqs_bp) { 15304 bp = sd_mark_rqs_idle(un, xp); 15305 xp = SD_GET_XBUF(bp); 15306 } else { 15307 /* 15308 * Legacy behavior: do not update transport 15309 * error count for request sense commands. 15310 */ 15311 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15312 } 15313 15314 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15315 sd_print_transport_rejected_message(un, xp, rval); 15316 15317 /* 15318 * This command will be terminated by SD driver due 15319 * to a fatal transport error. We should post 15320 * ereport.io.scsi.cmd.disk.tran with driver-assessment 15321 * of "fail" for any command to indicate this 15322 * situation. 15323 */ 15324 if (xp->xb_ena > 0) { 15325 ASSERT(un->un_fm_private != NULL); 15326 sfip = un->un_fm_private; 15327 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT; 15328 sd_ssc_extract_info(&sfip->fm_ssc, un, 15329 xp->xb_pktp, bp, xp); 15330 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15331 } 15332 15333 /* 15334 * We must use sd_return_failed_command_no_restart() to 15335 * avoid a recursive call back into sd_start_cmds(). 15336 * However this also means that we must keep processing 15337 * the waitq here in order to avoid stalling. 15338 */ 15339 sd_return_failed_command_no_restart(un, bp, EIO); 15340 15341 /* 15342 * Notify any threads waiting in sd_ddi_suspend() that 15343 * a command completion has occurred. 15344 */ 15345 if (un->un_state == SD_STATE_SUSPENDED) { 15346 cv_broadcast(&un->un_disk_busy_cv); 15347 } 15348 15349 if (bp == immed_bp) { 15350 /* immed_bp is gone by now, so clear this */ 15351 immed_bp = NULL; 15352 } 15353 break; 15354 } 15355 15356 } while (immed_bp == NULL); 15357 15358 exit: 15359 ASSERT(mutex_owned(SD_MUTEX(un))); 15360 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15361 } 15362 15363 15364 /* 15365 * Function: sd_return_command 15366 * 15367 * Description: Returns a command to its originator (with or without an 15368 * error). Also starts commands waiting to be transported 15369 * to the target. 15370 * 15371 * Context: May be called from interrupt, kernel, or timeout context 15372 */ 15373 15374 static void 15375 sd_return_command(struct sd_lun *un, struct buf *bp) 15376 { 15377 struct sd_xbuf *xp; 15378 struct scsi_pkt *pktp; 15379 struct sd_fm_internal *sfip; 15380 15381 ASSERT(bp != NULL); 15382 ASSERT(un != NULL); 15383 ASSERT(mutex_owned(SD_MUTEX(un))); 15384 ASSERT(bp != un->un_rqs_bp); 15385 xp = SD_GET_XBUF(bp); 15386 ASSERT(xp != NULL); 15387 15388 pktp = SD_GET_PKTP(bp); 15389 sfip = (struct sd_fm_internal *)un->un_fm_private; 15390 ASSERT(sfip != NULL); 15391 15392 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15393 15394 /* 15395 * Note: check for the "sdrestart failed" case. 15396 */ 15397 if ((un->un_partial_dma_supported == 1) && 15398 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15399 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15400 (xp->xb_pktp->pkt_resid == 0)) { 15401 15402 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15403 /* 15404 * Successfully set up next portion of cmd 15405 * transfer, try sending it 15406 */ 15407 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15408 NULL, NULL, 0, (clock_t)0, NULL); 15409 sd_start_cmds(un, NULL); 15410 return; /* Note:x86: need a return here? */ 15411 } 15412 } 15413 15414 /* 15415 * If this is the failfast bp, clear it from un_failfast_bp. This 15416 * can happen if upon being re-tried the failfast bp either 15417 * succeeded or encountered another error (possibly even a different 15418 * error than the one that precipitated the failfast state, but in 15419 * that case it would have had to exhaust retries as well). Regardless, 15420 * this should not occur whenever the instance is in the active 15421 * failfast state. 15422 */ 15423 if (bp == un->un_failfast_bp) { 15424 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15425 un->un_failfast_bp = NULL; 15426 } 15427 15428 /* 15429 * Clear the failfast state upon successful completion of ANY cmd. 15430 */ 15431 if (bp->b_error == 0) { 15432 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15433 /* 15434 * If this is a successful command, but used to be retried, 15435 * we will take it as a recovered command and post an 15436 * ereport with driver-assessment of "recovered". 15437 */ 15438 if (xp->xb_ena > 0) { 15439 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15440 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY); 15441 } 15442 } else { 15443 /* 15444 * If this is a failed non-USCSI command we will post an 15445 * ereport with driver-assessment set accordingly("fail" or 15446 * "fatal"). 15447 */ 15448 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15449 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15450 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15451 } 15452 } 15453 15454 /* 15455 * This is used if the command was retried one or more times. Show that 15456 * we are done with it, and allow processing of the waitq to resume. 15457 */ 15458 if (bp == un->un_retry_bp) { 15459 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15460 "sd_return_command: un:0x%p: " 15461 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15462 un->un_retry_bp = NULL; 15463 un->un_retry_statp = NULL; 15464 } 15465 15466 SD_UPDATE_RDWR_STATS(un, bp); 15467 SD_UPDATE_PARTITION_STATS(un, bp); 15468 15469 switch (un->un_state) { 15470 case SD_STATE_SUSPENDED: 15471 /* 15472 * Notify any threads waiting in sd_ddi_suspend() that 15473 * a command completion has occurred. 15474 */ 15475 cv_broadcast(&un->un_disk_busy_cv); 15476 break; 15477 default: 15478 sd_start_cmds(un, NULL); 15479 break; 15480 } 15481 15482 /* Return this command up the iodone chain to its originator. */ 15483 mutex_exit(SD_MUTEX(un)); 15484 15485 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15486 xp->xb_pktp = NULL; 15487 15488 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15489 15490 ASSERT(!mutex_owned(SD_MUTEX(un))); 15491 mutex_enter(SD_MUTEX(un)); 15492 15493 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15494 } 15495 15496 15497 /* 15498 * Function: sd_return_failed_command 15499 * 15500 * Description: Command completion when an error occurred. 15501 * 15502 * Context: May be called from interrupt context 15503 */ 15504 15505 static void 15506 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15507 { 15508 ASSERT(bp != NULL); 15509 ASSERT(un != NULL); 15510 ASSERT(mutex_owned(SD_MUTEX(un))); 15511 15512 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15513 "sd_return_failed_command: entry\n"); 15514 15515 /* 15516 * b_resid could already be nonzero due to a partial data 15517 * transfer, so do not change it here. 15518 */ 15519 SD_BIOERROR(bp, errcode); 15520 15521 sd_return_command(un, bp); 15522 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15523 "sd_return_failed_command: exit\n"); 15524 } 15525 15526 15527 /* 15528 * Function: sd_return_failed_command_no_restart 15529 * 15530 * Description: Same as sd_return_failed_command, but ensures that no 15531 * call back into sd_start_cmds will be issued. 15532 * 15533 * Context: May be called from interrupt context 15534 */ 15535 15536 static void 15537 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15538 int errcode) 15539 { 15540 struct sd_xbuf *xp; 15541 15542 ASSERT(bp != NULL); 15543 ASSERT(un != NULL); 15544 ASSERT(mutex_owned(SD_MUTEX(un))); 15545 xp = SD_GET_XBUF(bp); 15546 ASSERT(xp != NULL); 15547 ASSERT(errcode != 0); 15548 15549 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15550 "sd_return_failed_command_no_restart: entry\n"); 15551 15552 /* 15553 * b_resid could already be nonzero due to a partial data 15554 * transfer, so do not change it here. 15555 */ 15556 SD_BIOERROR(bp, errcode); 15557 15558 /* 15559 * If this is the failfast bp, clear it. This can happen if the 15560 * failfast bp encounterd a fatal error when we attempted to 15561 * re-try it (such as a scsi_transport(9F) failure). However 15562 * we should NOT be in an active failfast state if the failfast 15563 * bp is not NULL. 15564 */ 15565 if (bp == un->un_failfast_bp) { 15566 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15567 un->un_failfast_bp = NULL; 15568 } 15569 15570 if (bp == un->un_retry_bp) { 15571 /* 15572 * This command was retried one or more times. Show that we are 15573 * done with it, and allow processing of the waitq to resume. 15574 */ 15575 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15576 "sd_return_failed_command_no_restart: " 15577 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15578 un->un_retry_bp = NULL; 15579 un->un_retry_statp = NULL; 15580 } 15581 15582 SD_UPDATE_RDWR_STATS(un, bp); 15583 SD_UPDATE_PARTITION_STATS(un, bp); 15584 15585 mutex_exit(SD_MUTEX(un)); 15586 15587 if (xp->xb_pktp != NULL) { 15588 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15589 xp->xb_pktp = NULL; 15590 } 15591 15592 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15593 15594 mutex_enter(SD_MUTEX(un)); 15595 15596 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15597 "sd_return_failed_command_no_restart: exit\n"); 15598 } 15599 15600 15601 /* 15602 * Function: sd_retry_command 15603 * 15604 * Description: queue up a command for retry, or (optionally) fail it 15605 * if retry counts are exhausted. 15606 * 15607 * Arguments: un - Pointer to the sd_lun struct for the target. 15608 * 15609 * bp - Pointer to the buf for the command to be retried. 15610 * 15611 * retry_check_flag - Flag to see which (if any) of the retry 15612 * counts should be decremented/checked. If the indicated 15613 * retry count is exhausted, then the command will not be 15614 * retried; it will be failed instead. This should use a 15615 * value equal to one of the following: 15616 * 15617 * SD_RETRIES_NOCHECK 15618 * SD_RESD_RETRIES_STANDARD 15619 * SD_RETRIES_VICTIM 15620 * 15621 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15622 * if the check should be made to see of FLAG_ISOLATE is set 15623 * in the pkt. If FLAG_ISOLATE is set, then the command is 15624 * not retried, it is simply failed. 15625 * 15626 * user_funcp - Ptr to function to call before dispatching the 15627 * command. May be NULL if no action needs to be performed. 15628 * (Primarily intended for printing messages.) 15629 * 15630 * user_arg - Optional argument to be passed along to 15631 * the user_funcp call. 15632 * 15633 * failure_code - errno return code to set in the bp if the 15634 * command is going to be failed. 15635 * 15636 * retry_delay - Retry delay interval in (clock_t) units. May 15637 * be zero which indicates that the retry should be retried 15638 * immediately (ie, without an intervening delay). 15639 * 15640 * statp - Ptr to kstat function to be updated if the command 15641 * is queued for a delayed retry. May be NULL if no kstat 15642 * update is desired. 15643 * 15644 * Context: May be called from interrupt context. 15645 */ 15646 15647 static void 15648 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15649 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15650 code), void *user_arg, int failure_code, clock_t retry_delay, 15651 void (*statp)(kstat_io_t *)) 15652 { 15653 struct sd_xbuf *xp; 15654 struct scsi_pkt *pktp; 15655 struct sd_fm_internal *sfip; 15656 15657 ASSERT(un != NULL); 15658 ASSERT(mutex_owned(SD_MUTEX(un))); 15659 ASSERT(bp != NULL); 15660 xp = SD_GET_XBUF(bp); 15661 ASSERT(xp != NULL); 15662 pktp = SD_GET_PKTP(bp); 15663 ASSERT(pktp != NULL); 15664 15665 sfip = (struct sd_fm_internal *)un->un_fm_private; 15666 ASSERT(sfip != NULL); 15667 15668 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15669 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15670 15671 /* 15672 * If we are syncing or dumping, fail the command to avoid 15673 * recursively calling back into scsi_transport(). 15674 */ 15675 if (ddi_in_panic()) { 15676 goto fail_command_no_log; 15677 } 15678 15679 /* 15680 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15681 * log an error and fail the command. 15682 */ 15683 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15684 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15685 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15686 sd_dump_memory(un, SD_LOG_IO, "CDB", 15687 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15688 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15689 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15690 goto fail_command; 15691 } 15692 15693 /* 15694 * If we are suspended, then put the command onto head of the 15695 * wait queue since we don't want to start more commands, and 15696 * clear the un_retry_bp. Next time when we are resumed, will 15697 * handle the command in the wait queue. 15698 */ 15699 switch (un->un_state) { 15700 case SD_STATE_SUSPENDED: 15701 case SD_STATE_DUMPING: 15702 bp->av_forw = un->un_waitq_headp; 15703 un->un_waitq_headp = bp; 15704 if (un->un_waitq_tailp == NULL) { 15705 un->un_waitq_tailp = bp; 15706 } 15707 if (bp == un->un_retry_bp) { 15708 un->un_retry_bp = NULL; 15709 un->un_retry_statp = NULL; 15710 } 15711 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15712 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15713 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15714 return; 15715 default: 15716 break; 15717 } 15718 15719 /* 15720 * If the caller wants us to check FLAG_ISOLATE, then see if that 15721 * is set; if it is then we do not want to retry the command. 15722 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15723 */ 15724 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15725 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15726 goto fail_command; 15727 } 15728 } 15729 15730 15731 /* 15732 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15733 * command timeout or a selection timeout has occurred. This means 15734 * that we were unable to establish an kind of communication with 15735 * the target, and subsequent retries and/or commands are likely 15736 * to encounter similar results and take a long time to complete. 15737 * 15738 * If this is a failfast error condition, we need to update the 15739 * failfast state, even if this bp does not have B_FAILFAST set. 15740 */ 15741 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15742 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15743 ASSERT(un->un_failfast_bp == NULL); 15744 /* 15745 * If we are already in the active failfast state, and 15746 * another failfast error condition has been detected, 15747 * then fail this command if it has B_FAILFAST set. 15748 * If B_FAILFAST is clear, then maintain the legacy 15749 * behavior of retrying heroically, even tho this will 15750 * take a lot more time to fail the command. 15751 */ 15752 if (bp->b_flags & B_FAILFAST) { 15753 goto fail_command; 15754 } 15755 } else { 15756 /* 15757 * We're not in the active failfast state, but we 15758 * have a failfast error condition, so we must begin 15759 * transition to the next state. We do this regardless 15760 * of whether or not this bp has B_FAILFAST set. 15761 */ 15762 if (un->un_failfast_bp == NULL) { 15763 /* 15764 * This is the first bp to meet a failfast 15765 * condition so save it on un_failfast_bp & 15766 * do normal retry processing. Do not enter 15767 * active failfast state yet. This marks 15768 * entry into the "failfast pending" state. 15769 */ 15770 un->un_failfast_bp = bp; 15771 15772 } else if (un->un_failfast_bp == bp) { 15773 /* 15774 * This is the second time *this* bp has 15775 * encountered a failfast error condition, 15776 * so enter active failfast state & flush 15777 * queues as appropriate. 15778 */ 15779 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15780 un->un_failfast_bp = NULL; 15781 sd_failfast_flushq(un); 15782 15783 /* 15784 * Fail this bp now if B_FAILFAST set; 15785 * otherwise continue with retries. (It would 15786 * be pretty ironic if this bp succeeded on a 15787 * subsequent retry after we just flushed all 15788 * the queues). 15789 */ 15790 if (bp->b_flags & B_FAILFAST) { 15791 goto fail_command; 15792 } 15793 15794 #if !defined(lint) && !defined(__lint) 15795 } else { 15796 /* 15797 * If neither of the preceeding conditionals 15798 * was true, it means that there is some 15799 * *other* bp that has met an inital failfast 15800 * condition and is currently either being 15801 * retried or is waiting to be retried. In 15802 * that case we should perform normal retry 15803 * processing on *this* bp, since there is a 15804 * chance that the current failfast condition 15805 * is transient and recoverable. If that does 15806 * not turn out to be the case, then retries 15807 * will be cleared when the wait queue is 15808 * flushed anyway. 15809 */ 15810 #endif 15811 } 15812 } 15813 } else { 15814 /* 15815 * SD_RETRIES_FAILFAST is clear, which indicates that we 15816 * likely were able to at least establish some level of 15817 * communication with the target and subsequent commands 15818 * and/or retries are likely to get through to the target, 15819 * In this case we want to be aggressive about clearing 15820 * the failfast state. Note that this does not affect 15821 * the "failfast pending" condition. 15822 */ 15823 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15824 } 15825 15826 15827 /* 15828 * Check the specified retry count to see if we can still do 15829 * any retries with this pkt before we should fail it. 15830 */ 15831 switch (retry_check_flag & SD_RETRIES_MASK) { 15832 case SD_RETRIES_VICTIM: 15833 /* 15834 * Check the victim retry count. If exhausted, then fall 15835 * thru & check against the standard retry count. 15836 */ 15837 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15838 /* Increment count & proceed with the retry */ 15839 xp->xb_victim_retry_count++; 15840 break; 15841 } 15842 /* Victim retries exhausted, fall back to std. retries... */ 15843 /* FALLTHRU */ 15844 15845 case SD_RETRIES_STANDARD: 15846 if (xp->xb_retry_count >= un->un_retry_count) { 15847 /* Retries exhausted, fail the command */ 15848 SD_TRACE(SD_LOG_IO_CORE, un, 15849 "sd_retry_command: retries exhausted!\n"); 15850 /* 15851 * update b_resid for failed SCMD_READ & SCMD_WRITE 15852 * commands with nonzero pkt_resid. 15853 */ 15854 if ((pktp->pkt_reason == CMD_CMPLT) && 15855 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15856 (pktp->pkt_resid != 0)) { 15857 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15858 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15859 SD_UPDATE_B_RESID(bp, pktp); 15860 } 15861 } 15862 goto fail_command; 15863 } 15864 xp->xb_retry_count++; 15865 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15866 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15867 break; 15868 15869 case SD_RETRIES_UA: 15870 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15871 /* Retries exhausted, fail the command */ 15872 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15873 "Unit Attention retries exhausted. " 15874 "Check the target.\n"); 15875 goto fail_command; 15876 } 15877 xp->xb_ua_retry_count++; 15878 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15879 "sd_retry_command: retry count:%d\n", 15880 xp->xb_ua_retry_count); 15881 break; 15882 15883 case SD_RETRIES_BUSY: 15884 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15885 /* Retries exhausted, fail the command */ 15886 SD_TRACE(SD_LOG_IO_CORE, un, 15887 "sd_retry_command: retries exhausted!\n"); 15888 goto fail_command; 15889 } 15890 xp->xb_retry_count++; 15891 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15892 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15893 break; 15894 15895 case SD_RETRIES_NOCHECK: 15896 default: 15897 /* No retry count to check. Just proceed with the retry */ 15898 break; 15899 } 15900 15901 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15902 15903 /* 15904 * If this is a non-USCSI command being retried 15905 * during execution last time, we should post an ereport with 15906 * driver-assessment of the value "retry". 15907 * For partial DMA, request sense and STATUS_QFULL, there are no 15908 * hardware errors, we bypass ereport posting. 15909 */ 15910 if (failure_code != 0) { 15911 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15912 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15913 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY); 15914 } 15915 } 15916 15917 /* 15918 * If we were given a zero timeout, we must attempt to retry the 15919 * command immediately (ie, without a delay). 15920 */ 15921 if (retry_delay == 0) { 15922 /* 15923 * Check some limiting conditions to see if we can actually 15924 * do the immediate retry. If we cannot, then we must 15925 * fall back to queueing up a delayed retry. 15926 */ 15927 if (un->un_ncmds_in_transport >= un->un_throttle) { 15928 /* 15929 * We are at the throttle limit for the target, 15930 * fall back to delayed retry. 15931 */ 15932 retry_delay = un->un_busy_timeout; 15933 statp = kstat_waitq_enter; 15934 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15935 "sd_retry_command: immed. retry hit " 15936 "throttle!\n"); 15937 } else { 15938 /* 15939 * We're clear to proceed with the immediate retry. 15940 * First call the user-provided function (if any) 15941 */ 15942 if (user_funcp != NULL) { 15943 (*user_funcp)(un, bp, user_arg, 15944 SD_IMMEDIATE_RETRY_ISSUED); 15945 #ifdef __lock_lint 15946 sd_print_incomplete_msg(un, bp, user_arg, 15947 SD_IMMEDIATE_RETRY_ISSUED); 15948 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15949 SD_IMMEDIATE_RETRY_ISSUED); 15950 sd_print_sense_failed_msg(un, bp, user_arg, 15951 SD_IMMEDIATE_RETRY_ISSUED); 15952 #endif 15953 } 15954 15955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15956 "sd_retry_command: issuing immediate retry\n"); 15957 15958 /* 15959 * Call sd_start_cmds() to transport the command to 15960 * the target. 15961 */ 15962 sd_start_cmds(un, bp); 15963 15964 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15965 "sd_retry_command exit\n"); 15966 return; 15967 } 15968 } 15969 15970 /* 15971 * Set up to retry the command after a delay. 15972 * First call the user-provided function (if any) 15973 */ 15974 if (user_funcp != NULL) { 15975 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15976 } 15977 15978 sd_set_retry_bp(un, bp, retry_delay, statp); 15979 15980 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15981 return; 15982 15983 fail_command: 15984 15985 if (user_funcp != NULL) { 15986 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15987 } 15988 15989 fail_command_no_log: 15990 15991 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15992 "sd_retry_command: returning failed command\n"); 15993 15994 sd_return_failed_command(un, bp, failure_code); 15995 15996 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15997 } 15998 15999 16000 /* 16001 * Function: sd_set_retry_bp 16002 * 16003 * Description: Set up the given bp for retry. 16004 * 16005 * Arguments: un - ptr to associated softstate 16006 * bp - ptr to buf(9S) for the command 16007 * retry_delay - time interval before issuing retry (may be 0) 16008 * statp - optional pointer to kstat function 16009 * 16010 * Context: May be called under interrupt context 16011 */ 16012 16013 static void 16014 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 16015 void (*statp)(kstat_io_t *)) 16016 { 16017 ASSERT(un != NULL); 16018 ASSERT(mutex_owned(SD_MUTEX(un))); 16019 ASSERT(bp != NULL); 16020 16021 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16022 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 16023 16024 /* 16025 * Indicate that the command is being retried. This will not allow any 16026 * other commands on the wait queue to be transported to the target 16027 * until this command has been completed (success or failure). The 16028 * "retry command" is not transported to the target until the given 16029 * time delay expires, unless the user specified a 0 retry_delay. 16030 * 16031 * Note: the timeout(9F) callback routine is what actually calls 16032 * sd_start_cmds() to transport the command, with the exception of a 16033 * zero retry_delay. The only current implementor of a zero retry delay 16034 * is the case where a START_STOP_UNIT is sent to spin-up a device. 16035 */ 16036 if (un->un_retry_bp == NULL) { 16037 ASSERT(un->un_retry_statp == NULL); 16038 un->un_retry_bp = bp; 16039 16040 /* 16041 * If the user has not specified a delay the command should 16042 * be queued and no timeout should be scheduled. 16043 */ 16044 if (retry_delay == 0) { 16045 /* 16046 * Save the kstat pointer that will be used in the 16047 * call to SD_UPDATE_KSTATS() below, so that 16048 * sd_start_cmds() can correctly decrement the waitq 16049 * count when it is time to transport this command. 16050 */ 16051 un->un_retry_statp = statp; 16052 goto done; 16053 } 16054 } 16055 16056 if (un->un_retry_bp == bp) { 16057 /* 16058 * Save the kstat pointer that will be used in the call to 16059 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 16060 * correctly decrement the waitq count when it is time to 16061 * transport this command. 16062 */ 16063 un->un_retry_statp = statp; 16064 16065 /* 16066 * Schedule a timeout if: 16067 * 1) The user has specified a delay. 16068 * 2) There is not a START_STOP_UNIT callback pending. 16069 * 16070 * If no delay has been specified, then it is up to the caller 16071 * to ensure that IO processing continues without stalling. 16072 * Effectively, this means that the caller will issue the 16073 * required call to sd_start_cmds(). The START_STOP_UNIT 16074 * callback does this after the START STOP UNIT command has 16075 * completed. In either of these cases we should not schedule 16076 * a timeout callback here. Also don't schedule the timeout if 16077 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 16078 */ 16079 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 16080 (un->un_direct_priority_timeid == NULL)) { 16081 un->un_retry_timeid = 16082 timeout(sd_start_retry_command, un, retry_delay); 16083 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16084 "sd_set_retry_bp: setting timeout: un: 0x%p" 16085 " bp:0x%p un_retry_timeid:0x%p\n", 16086 un, bp, un->un_retry_timeid); 16087 } 16088 } else { 16089 /* 16090 * We only get in here if there is already another command 16091 * waiting to be retried. In this case, we just put the 16092 * given command onto the wait queue, so it can be transported 16093 * after the current retry command has completed. 16094 * 16095 * Also we have to make sure that if the command at the head 16096 * of the wait queue is the un_failfast_bp, that we do not 16097 * put ahead of it any other commands that are to be retried. 16098 */ 16099 if ((un->un_failfast_bp != NULL) && 16100 (un->un_failfast_bp == un->un_waitq_headp)) { 16101 /* 16102 * Enqueue this command AFTER the first command on 16103 * the wait queue (which is also un_failfast_bp). 16104 */ 16105 bp->av_forw = un->un_waitq_headp->av_forw; 16106 un->un_waitq_headp->av_forw = bp; 16107 if (un->un_waitq_headp == un->un_waitq_tailp) { 16108 un->un_waitq_tailp = bp; 16109 } 16110 } else { 16111 /* Enqueue this command at the head of the waitq. */ 16112 bp->av_forw = un->un_waitq_headp; 16113 un->un_waitq_headp = bp; 16114 if (un->un_waitq_tailp == NULL) { 16115 un->un_waitq_tailp = bp; 16116 } 16117 } 16118 16119 if (statp == NULL) { 16120 statp = kstat_waitq_enter; 16121 } 16122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16123 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 16124 } 16125 16126 done: 16127 if (statp != NULL) { 16128 SD_UPDATE_KSTATS(un, statp, bp); 16129 } 16130 16131 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16132 "sd_set_retry_bp: exit un:0x%p\n", un); 16133 } 16134 16135 16136 /* 16137 * Function: sd_start_retry_command 16138 * 16139 * Description: Start the command that has been waiting on the target's 16140 * retry queue. Called from timeout(9F) context after the 16141 * retry delay interval has expired. 16142 * 16143 * Arguments: arg - pointer to associated softstate for the device. 16144 * 16145 * Context: timeout(9F) thread context. May not sleep. 16146 */ 16147 16148 static void 16149 sd_start_retry_command(void *arg) 16150 { 16151 struct sd_lun *un = arg; 16152 16153 ASSERT(un != NULL); 16154 ASSERT(!mutex_owned(SD_MUTEX(un))); 16155 16156 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16157 "sd_start_retry_command: entry\n"); 16158 16159 mutex_enter(SD_MUTEX(un)); 16160 16161 un->un_retry_timeid = NULL; 16162 16163 if (un->un_retry_bp != NULL) { 16164 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16165 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16166 un, un->un_retry_bp); 16167 sd_start_cmds(un, un->un_retry_bp); 16168 } 16169 16170 mutex_exit(SD_MUTEX(un)); 16171 16172 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16173 "sd_start_retry_command: exit\n"); 16174 } 16175 16176 /* 16177 * Function: sd_rmw_msg_print_handler 16178 * 16179 * Description: If RMW mode is enabled and warning message is triggered 16180 * print I/O count during a fixed interval. 16181 * 16182 * Arguments: arg - pointer to associated softstate for the device. 16183 * 16184 * Context: timeout(9F) thread context. May not sleep. 16185 */ 16186 static void 16187 sd_rmw_msg_print_handler(void *arg) 16188 { 16189 struct sd_lun *un = arg; 16190 16191 ASSERT(un != NULL); 16192 ASSERT(!mutex_owned(SD_MUTEX(un))); 16193 16194 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16195 "sd_rmw_msg_print_handler: entry\n"); 16196 16197 mutex_enter(SD_MUTEX(un)); 16198 16199 if (un->un_rmw_incre_count > 0) { 16200 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16201 "%"PRIu64" I/O requests are not aligned with %d disk " 16202 "sector size in %ld seconds. They are handled through " 16203 "Read Modify Write but the performance is very low!\n", 16204 un->un_rmw_incre_count, un->un_tgt_blocksize, 16205 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000); 16206 un->un_rmw_incre_count = 0; 16207 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler, 16208 un, SD_RMW_MSG_PRINT_TIMEOUT); 16209 } else { 16210 un->un_rmw_msg_timeid = NULL; 16211 } 16212 16213 mutex_exit(SD_MUTEX(un)); 16214 16215 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16216 "sd_rmw_msg_print_handler: exit\n"); 16217 } 16218 16219 /* 16220 * Function: sd_start_direct_priority_command 16221 * 16222 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16223 * received TRAN_BUSY when we called scsi_transport() to send it 16224 * to the underlying HBA. This function is called from timeout(9F) 16225 * context after the delay interval has expired. 16226 * 16227 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16228 * 16229 * Context: timeout(9F) thread context. May not sleep. 16230 */ 16231 16232 static void 16233 sd_start_direct_priority_command(void *arg) 16234 { 16235 struct buf *priority_bp = arg; 16236 struct sd_lun *un; 16237 16238 ASSERT(priority_bp != NULL); 16239 un = SD_GET_UN(priority_bp); 16240 ASSERT(un != NULL); 16241 ASSERT(!mutex_owned(SD_MUTEX(un))); 16242 16243 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16244 "sd_start_direct_priority_command: entry\n"); 16245 16246 mutex_enter(SD_MUTEX(un)); 16247 un->un_direct_priority_timeid = NULL; 16248 sd_start_cmds(un, priority_bp); 16249 mutex_exit(SD_MUTEX(un)); 16250 16251 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16252 "sd_start_direct_priority_command: exit\n"); 16253 } 16254 16255 16256 /* 16257 * Function: sd_send_request_sense_command 16258 * 16259 * Description: Sends a REQUEST SENSE command to the target 16260 * 16261 * Context: May be called from interrupt context. 16262 */ 16263 16264 static void 16265 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16266 struct scsi_pkt *pktp) 16267 { 16268 ASSERT(bp != NULL); 16269 ASSERT(un != NULL); 16270 ASSERT(mutex_owned(SD_MUTEX(un))); 16271 16272 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16273 "entry: buf:0x%p\n", bp); 16274 16275 /* 16276 * If we are syncing or dumping, then fail the command to avoid a 16277 * recursive callback into scsi_transport(). Also fail the command 16278 * if we are suspended (legacy behavior). 16279 */ 16280 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16281 (un->un_state == SD_STATE_DUMPING)) { 16282 sd_return_failed_command(un, bp, EIO); 16283 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16284 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16285 return; 16286 } 16287 16288 /* 16289 * Retry the failed command and don't issue the request sense if: 16290 * 1) the sense buf is busy 16291 * 2) we have 1 or more outstanding commands on the target 16292 * (the sense data will be cleared or invalidated any way) 16293 * 16294 * Note: There could be an issue with not checking a retry limit here, 16295 * the problem is determining which retry limit to check. 16296 */ 16297 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16298 /* Don't retry if the command is flagged as non-retryable */ 16299 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16300 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16301 NULL, NULL, 0, un->un_busy_timeout, 16302 kstat_waitq_enter); 16303 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16304 "sd_send_request_sense_command: " 16305 "at full throttle, retrying exit\n"); 16306 } else { 16307 sd_return_failed_command(un, bp, EIO); 16308 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16309 "sd_send_request_sense_command: " 16310 "at full throttle, non-retryable exit\n"); 16311 } 16312 return; 16313 } 16314 16315 sd_mark_rqs_busy(un, bp); 16316 sd_start_cmds(un, un->un_rqs_bp); 16317 16318 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16319 "sd_send_request_sense_command: exit\n"); 16320 } 16321 16322 16323 /* 16324 * Function: sd_mark_rqs_busy 16325 * 16326 * Description: Indicate that the request sense bp for this instance is 16327 * in use. 16328 * 16329 * Context: May be called under interrupt context 16330 */ 16331 16332 static void 16333 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16334 { 16335 struct sd_xbuf *sense_xp; 16336 16337 ASSERT(un != NULL); 16338 ASSERT(bp != NULL); 16339 ASSERT(mutex_owned(SD_MUTEX(un))); 16340 ASSERT(un->un_sense_isbusy == 0); 16341 16342 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16343 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16344 16345 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16346 ASSERT(sense_xp != NULL); 16347 16348 SD_INFO(SD_LOG_IO, un, 16349 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16350 16351 ASSERT(sense_xp->xb_pktp != NULL); 16352 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16353 == (FLAG_SENSING | FLAG_HEAD)); 16354 16355 un->un_sense_isbusy = 1; 16356 un->un_rqs_bp->b_resid = 0; 16357 sense_xp->xb_pktp->pkt_resid = 0; 16358 sense_xp->xb_pktp->pkt_reason = 0; 16359 16360 /* So we can get back the bp at interrupt time! */ 16361 sense_xp->xb_sense_bp = bp; 16362 16363 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16364 16365 /* 16366 * Mark this buf as awaiting sense data. (This is already set in 16367 * the pkt_flags for the RQS packet.) 16368 */ 16369 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16370 16371 /* Request sense down same path */ 16372 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) && 16373 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance) 16374 sense_xp->xb_pktp->pkt_path_instance = 16375 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance; 16376 16377 sense_xp->xb_retry_count = 0; 16378 sense_xp->xb_victim_retry_count = 0; 16379 sense_xp->xb_ua_retry_count = 0; 16380 sense_xp->xb_nr_retry_count = 0; 16381 sense_xp->xb_dma_resid = 0; 16382 16383 /* Clean up the fields for auto-request sense */ 16384 sense_xp->xb_sense_status = 0; 16385 sense_xp->xb_sense_state = 0; 16386 sense_xp->xb_sense_resid = 0; 16387 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16388 16389 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16390 } 16391 16392 16393 /* 16394 * Function: sd_mark_rqs_idle 16395 * 16396 * Description: SD_MUTEX must be held continuously through this routine 16397 * to prevent reuse of the rqs struct before the caller can 16398 * complete it's processing. 16399 * 16400 * Return Code: Pointer to the RQS buf 16401 * 16402 * Context: May be called under interrupt context 16403 */ 16404 16405 static struct buf * 16406 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16407 { 16408 struct buf *bp; 16409 ASSERT(un != NULL); 16410 ASSERT(sense_xp != NULL); 16411 ASSERT(mutex_owned(SD_MUTEX(un))); 16412 ASSERT(un->un_sense_isbusy != 0); 16413 16414 un->un_sense_isbusy = 0; 16415 bp = sense_xp->xb_sense_bp; 16416 sense_xp->xb_sense_bp = NULL; 16417 16418 /* This pkt is no longer interested in getting sense data */ 16419 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16420 16421 return (bp); 16422 } 16423 16424 16425 16426 /* 16427 * Function: sd_alloc_rqs 16428 * 16429 * Description: Set up the unit to receive auto request sense data 16430 * 16431 * Return Code: DDI_SUCCESS or DDI_FAILURE 16432 * 16433 * Context: Called under attach(9E) context 16434 */ 16435 16436 static int 16437 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16438 { 16439 struct sd_xbuf *xp; 16440 16441 ASSERT(un != NULL); 16442 ASSERT(!mutex_owned(SD_MUTEX(un))); 16443 ASSERT(un->un_rqs_bp == NULL); 16444 ASSERT(un->un_rqs_pktp == NULL); 16445 16446 /* 16447 * First allocate the required buf and scsi_pkt structs, then set up 16448 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16449 */ 16450 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16451 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16452 if (un->un_rqs_bp == NULL) { 16453 return (DDI_FAILURE); 16454 } 16455 16456 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16457 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16458 16459 if (un->un_rqs_pktp == NULL) { 16460 sd_free_rqs(un); 16461 return (DDI_FAILURE); 16462 } 16463 16464 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16465 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16466 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 16467 16468 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16469 16470 /* Set up the other needed members in the ARQ scsi_pkt. */ 16471 un->un_rqs_pktp->pkt_comp = sdintr; 16472 un->un_rqs_pktp->pkt_time = sd_io_time; 16473 un->un_rqs_pktp->pkt_flags |= 16474 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16475 16476 /* 16477 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16478 * provide any intpkt, destroypkt routines as we take care of 16479 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16480 */ 16481 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16482 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16483 xp->xb_pktp = un->un_rqs_pktp; 16484 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16485 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16486 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16487 16488 /* 16489 * Save the pointer to the request sense private bp so it can 16490 * be retrieved in sdintr. 16491 */ 16492 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16493 ASSERT(un->un_rqs_bp->b_private == xp); 16494 16495 /* 16496 * See if the HBA supports auto-request sense for the specified 16497 * target/lun. If it does, then try to enable it (if not already 16498 * enabled). 16499 * 16500 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16501 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16502 * return success. However, in both of these cases ARQ is always 16503 * enabled and scsi_ifgetcap will always return true. The best approach 16504 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16505 * 16506 * The 3rd case is the HBA (adp) always return enabled on 16507 * scsi_ifgetgetcap even when it's not enable, the best approach 16508 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16509 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16510 */ 16511 16512 if (un->un_f_is_fibre == TRUE) { 16513 un->un_f_arq_enabled = TRUE; 16514 } else { 16515 #if defined(__i386) || defined(__amd64) 16516 /* 16517 * Circumvent the Adaptec bug, remove this code when 16518 * the bug is fixed 16519 */ 16520 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16521 #endif 16522 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16523 case 0: 16524 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16525 "sd_alloc_rqs: HBA supports ARQ\n"); 16526 /* 16527 * ARQ is supported by this HBA but currently is not 16528 * enabled. Attempt to enable it and if successful then 16529 * mark this instance as ARQ enabled. 16530 */ 16531 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16532 == 1) { 16533 /* Successfully enabled ARQ in the HBA */ 16534 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16535 "sd_alloc_rqs: ARQ enabled\n"); 16536 un->un_f_arq_enabled = TRUE; 16537 } else { 16538 /* Could not enable ARQ in the HBA */ 16539 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16540 "sd_alloc_rqs: failed ARQ enable\n"); 16541 un->un_f_arq_enabled = FALSE; 16542 } 16543 break; 16544 case 1: 16545 /* 16546 * ARQ is supported by this HBA and is already enabled. 16547 * Just mark ARQ as enabled for this instance. 16548 */ 16549 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16550 "sd_alloc_rqs: ARQ already enabled\n"); 16551 un->un_f_arq_enabled = TRUE; 16552 break; 16553 default: 16554 /* 16555 * ARQ is not supported by this HBA; disable it for this 16556 * instance. 16557 */ 16558 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16559 "sd_alloc_rqs: HBA does not support ARQ\n"); 16560 un->un_f_arq_enabled = FALSE; 16561 break; 16562 } 16563 } 16564 16565 return (DDI_SUCCESS); 16566 } 16567 16568 16569 /* 16570 * Function: sd_free_rqs 16571 * 16572 * Description: Cleanup for the pre-instance RQS command. 16573 * 16574 * Context: Kernel thread context 16575 */ 16576 16577 static void 16578 sd_free_rqs(struct sd_lun *un) 16579 { 16580 ASSERT(un != NULL); 16581 16582 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16583 16584 /* 16585 * If consistent memory is bound to a scsi_pkt, the pkt 16586 * has to be destroyed *before* freeing the consistent memory. 16587 * Don't change the sequence of this operations. 16588 * scsi_destroy_pkt() might access memory, which isn't allowed, 16589 * after it was freed in scsi_free_consistent_buf(). 16590 */ 16591 if (un->un_rqs_pktp != NULL) { 16592 scsi_destroy_pkt(un->un_rqs_pktp); 16593 un->un_rqs_pktp = NULL; 16594 } 16595 16596 if (un->un_rqs_bp != NULL) { 16597 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 16598 if (xp != NULL) { 16599 kmem_free(xp, sizeof (struct sd_xbuf)); 16600 } 16601 scsi_free_consistent_buf(un->un_rqs_bp); 16602 un->un_rqs_bp = NULL; 16603 } 16604 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16605 } 16606 16607 16608 16609 /* 16610 * Function: sd_reduce_throttle 16611 * 16612 * Description: Reduces the maximum # of outstanding commands on a 16613 * target to the current number of outstanding commands. 16614 * Queues a tiemout(9F) callback to restore the limit 16615 * after a specified interval has elapsed. 16616 * Typically used when we get a TRAN_BUSY return code 16617 * back from scsi_transport(). 16618 * 16619 * Arguments: un - ptr to the sd_lun softstate struct 16620 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16621 * 16622 * Context: May be called from interrupt context 16623 */ 16624 16625 static void 16626 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16627 { 16628 ASSERT(un != NULL); 16629 ASSERT(mutex_owned(SD_MUTEX(un))); 16630 ASSERT(un->un_ncmds_in_transport >= 0); 16631 16632 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16633 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16634 un, un->un_throttle, un->un_ncmds_in_transport); 16635 16636 if (un->un_throttle > 1) { 16637 if (un->un_f_use_adaptive_throttle == TRUE) { 16638 switch (throttle_type) { 16639 case SD_THROTTLE_TRAN_BUSY: 16640 if (un->un_busy_throttle == 0) { 16641 un->un_busy_throttle = un->un_throttle; 16642 } 16643 break; 16644 case SD_THROTTLE_QFULL: 16645 un->un_busy_throttle = 0; 16646 break; 16647 default: 16648 ASSERT(FALSE); 16649 } 16650 16651 if (un->un_ncmds_in_transport > 0) { 16652 un->un_throttle = un->un_ncmds_in_transport; 16653 } 16654 16655 } else { 16656 if (un->un_ncmds_in_transport == 0) { 16657 un->un_throttle = 1; 16658 } else { 16659 un->un_throttle = un->un_ncmds_in_transport; 16660 } 16661 } 16662 } 16663 16664 /* Reschedule the timeout if none is currently active */ 16665 if (un->un_reset_throttle_timeid == NULL) { 16666 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16667 un, SD_THROTTLE_RESET_INTERVAL); 16668 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16669 "sd_reduce_throttle: timeout scheduled!\n"); 16670 } 16671 16672 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16673 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16674 } 16675 16676 16677 16678 /* 16679 * Function: sd_restore_throttle 16680 * 16681 * Description: Callback function for timeout(9F). Resets the current 16682 * value of un->un_throttle to its default. 16683 * 16684 * Arguments: arg - pointer to associated softstate for the device. 16685 * 16686 * Context: May be called from interrupt context 16687 */ 16688 16689 static void 16690 sd_restore_throttle(void *arg) 16691 { 16692 struct sd_lun *un = arg; 16693 16694 ASSERT(un != NULL); 16695 ASSERT(!mutex_owned(SD_MUTEX(un))); 16696 16697 mutex_enter(SD_MUTEX(un)); 16698 16699 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16700 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16701 16702 un->un_reset_throttle_timeid = NULL; 16703 16704 if (un->un_f_use_adaptive_throttle == TRUE) { 16705 /* 16706 * If un_busy_throttle is nonzero, then it contains the 16707 * value that un_throttle was when we got a TRAN_BUSY back 16708 * from scsi_transport(). We want to revert back to this 16709 * value. 16710 * 16711 * In the QFULL case, the throttle limit will incrementally 16712 * increase until it reaches max throttle. 16713 */ 16714 if (un->un_busy_throttle > 0) { 16715 un->un_throttle = un->un_busy_throttle; 16716 un->un_busy_throttle = 0; 16717 } else { 16718 /* 16719 * increase throttle by 10% open gate slowly, schedule 16720 * another restore if saved throttle has not been 16721 * reached 16722 */ 16723 short throttle; 16724 if (sd_qfull_throttle_enable) { 16725 throttle = un->un_throttle + 16726 max((un->un_throttle / 10), 1); 16727 un->un_throttle = 16728 (throttle < un->un_saved_throttle) ? 16729 throttle : un->un_saved_throttle; 16730 if (un->un_throttle < un->un_saved_throttle) { 16731 un->un_reset_throttle_timeid = 16732 timeout(sd_restore_throttle, 16733 un, 16734 SD_QFULL_THROTTLE_RESET_INTERVAL); 16735 } 16736 } 16737 } 16738 16739 /* 16740 * If un_throttle has fallen below the low-water mark, we 16741 * restore the maximum value here (and allow it to ratchet 16742 * down again if necessary). 16743 */ 16744 if (un->un_throttle < un->un_min_throttle) { 16745 un->un_throttle = un->un_saved_throttle; 16746 } 16747 } else { 16748 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16749 "restoring limit from 0x%x to 0x%x\n", 16750 un->un_throttle, un->un_saved_throttle); 16751 un->un_throttle = un->un_saved_throttle; 16752 } 16753 16754 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16755 "sd_restore_throttle: calling sd_start_cmds!\n"); 16756 16757 sd_start_cmds(un, NULL); 16758 16759 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16760 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16761 un, un->un_throttle); 16762 16763 mutex_exit(SD_MUTEX(un)); 16764 16765 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16766 } 16767 16768 /* 16769 * Function: sdrunout 16770 * 16771 * Description: Callback routine for scsi_init_pkt when a resource allocation 16772 * fails. 16773 * 16774 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16775 * soft state instance. 16776 * 16777 * Return Code: The scsi_init_pkt routine allows for the callback function to 16778 * return a 0 indicating the callback should be rescheduled or a 1 16779 * indicating not to reschedule. This routine always returns 1 16780 * because the driver always provides a callback function to 16781 * scsi_init_pkt. This results in a callback always being scheduled 16782 * (via the scsi_init_pkt callback implementation) if a resource 16783 * failure occurs. 16784 * 16785 * Context: This callback function may not block or call routines that block 16786 * 16787 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16788 * request persisting at the head of the list which cannot be 16789 * satisfied even after multiple retries. In the future the driver 16790 * may implement some time of maximum runout count before failing 16791 * an I/O. 16792 */ 16793 16794 static int 16795 sdrunout(caddr_t arg) 16796 { 16797 struct sd_lun *un = (struct sd_lun *)arg; 16798 16799 ASSERT(un != NULL); 16800 ASSERT(!mutex_owned(SD_MUTEX(un))); 16801 16802 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16803 16804 mutex_enter(SD_MUTEX(un)); 16805 sd_start_cmds(un, NULL); 16806 mutex_exit(SD_MUTEX(un)); 16807 /* 16808 * This callback routine always returns 1 (i.e. do not reschedule) 16809 * because we always specify sdrunout as the callback handler for 16810 * scsi_init_pkt inside the call to sd_start_cmds. 16811 */ 16812 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16813 return (1); 16814 } 16815 16816 16817 /* 16818 * Function: sdintr 16819 * 16820 * Description: Completion callback routine for scsi_pkt(9S) structs 16821 * sent to the HBA driver via scsi_transport(9F). 16822 * 16823 * Context: Interrupt context 16824 */ 16825 16826 static void 16827 sdintr(struct scsi_pkt *pktp) 16828 { 16829 struct buf *bp; 16830 struct sd_xbuf *xp; 16831 struct sd_lun *un; 16832 size_t actual_len; 16833 sd_ssc_t *sscp; 16834 16835 ASSERT(pktp != NULL); 16836 bp = (struct buf *)pktp->pkt_private; 16837 ASSERT(bp != NULL); 16838 xp = SD_GET_XBUF(bp); 16839 ASSERT(xp != NULL); 16840 ASSERT(xp->xb_pktp != NULL); 16841 un = SD_GET_UN(bp); 16842 ASSERT(un != NULL); 16843 ASSERT(!mutex_owned(SD_MUTEX(un))); 16844 16845 #ifdef SD_FAULT_INJECTION 16846 16847 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16848 /* SD FaultInjection */ 16849 sd_faultinjection(pktp); 16850 16851 #endif /* SD_FAULT_INJECTION */ 16852 16853 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16854 " xp:0x%p, un:0x%p\n", bp, xp, un); 16855 16856 mutex_enter(SD_MUTEX(un)); 16857 16858 ASSERT(un->un_fm_private != NULL); 16859 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16860 ASSERT(sscp != NULL); 16861 16862 /* Reduce the count of the #commands currently in transport */ 16863 un->un_ncmds_in_transport--; 16864 ASSERT(un->un_ncmds_in_transport >= 0); 16865 16866 /* Increment counter to indicate that the callback routine is active */ 16867 un->un_in_callback++; 16868 16869 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16870 16871 #ifdef SDDEBUG 16872 if (bp == un->un_retry_bp) { 16873 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16874 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16875 un, un->un_retry_bp, un->un_ncmds_in_transport); 16876 } 16877 #endif 16878 16879 /* 16880 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 16881 * state if needed. 16882 */ 16883 if (pktp->pkt_reason == CMD_DEV_GONE) { 16884 /* Prevent multiple console messages for the same failure. */ 16885 if (un->un_last_pkt_reason != CMD_DEV_GONE) { 16886 un->un_last_pkt_reason = CMD_DEV_GONE; 16887 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16888 "Command failed to complete...Device is gone\n"); 16889 } 16890 if (un->un_mediastate != DKIO_DEV_GONE) { 16891 un->un_mediastate = DKIO_DEV_GONE; 16892 cv_broadcast(&un->un_state_cv); 16893 } 16894 /* 16895 * If the command happens to be the REQUEST SENSE command, 16896 * free up the rqs buf and fail the original command. 16897 */ 16898 if (bp == un->un_rqs_bp) { 16899 bp = sd_mark_rqs_idle(un, xp); 16900 } 16901 sd_return_failed_command(un, bp, EIO); 16902 goto exit; 16903 } 16904 16905 if (pktp->pkt_state & STATE_XARQ_DONE) { 16906 SD_TRACE(SD_LOG_COMMON, un, 16907 "sdintr: extra sense data received. pkt=%p\n", pktp); 16908 } 16909 16910 /* 16911 * First see if the pkt has auto-request sense data with it.... 16912 * Look at the packet state first so we don't take a performance 16913 * hit looking at the arq enabled flag unless absolutely necessary. 16914 */ 16915 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16916 (un->un_f_arq_enabled == TRUE)) { 16917 /* 16918 * The HBA did an auto request sense for this command so check 16919 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16920 * driver command that should not be retried. 16921 */ 16922 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16923 /* 16924 * Save the relevant sense info into the xp for the 16925 * original cmd. 16926 */ 16927 struct scsi_arq_status *asp; 16928 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16929 xp->xb_sense_status = 16930 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16931 xp->xb_sense_state = asp->sts_rqpkt_state; 16932 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16933 if (pktp->pkt_state & STATE_XARQ_DONE) { 16934 actual_len = MAX_SENSE_LENGTH - 16935 xp->xb_sense_resid; 16936 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16937 MAX_SENSE_LENGTH); 16938 } else { 16939 if (xp->xb_sense_resid > SENSE_LENGTH) { 16940 actual_len = MAX_SENSE_LENGTH - 16941 xp->xb_sense_resid; 16942 } else { 16943 actual_len = SENSE_LENGTH - 16944 xp->xb_sense_resid; 16945 } 16946 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16947 if ((((struct uscsi_cmd *) 16948 (xp->xb_pktinfo))->uscsi_rqlen) > 16949 actual_len) { 16950 xp->xb_sense_resid = 16951 (((struct uscsi_cmd *) 16952 (xp->xb_pktinfo))-> 16953 uscsi_rqlen) - actual_len; 16954 } else { 16955 xp->xb_sense_resid = 0; 16956 } 16957 } 16958 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16959 SENSE_LENGTH); 16960 } 16961 16962 /* fail the command */ 16963 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16964 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16965 sd_return_failed_command(un, bp, EIO); 16966 goto exit; 16967 } 16968 16969 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16970 /* 16971 * We want to either retry or fail this command, so free 16972 * the DMA resources here. If we retry the command then 16973 * the DMA resources will be reallocated in sd_start_cmds(). 16974 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16975 * causes the *entire* transfer to start over again from the 16976 * beginning of the request, even for PARTIAL chunks that 16977 * have already transferred successfully. 16978 */ 16979 if ((un->un_f_is_fibre == TRUE) && 16980 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16981 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16982 scsi_dmafree(pktp); 16983 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16984 } 16985 #endif 16986 16987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16988 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16989 16990 sd_handle_auto_request_sense(un, bp, xp, pktp); 16991 goto exit; 16992 } 16993 16994 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16995 if (pktp->pkt_flags & FLAG_SENSING) { 16996 /* This pktp is from the unit's REQUEST_SENSE command */ 16997 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16998 "sdintr: sd_handle_request_sense\n"); 16999 sd_handle_request_sense(un, bp, xp, pktp); 17000 goto exit; 17001 } 17002 17003 /* 17004 * Check to see if the command successfully completed as requested; 17005 * this is the most common case (and also the hot performance path). 17006 * 17007 * Requirements for successful completion are: 17008 * pkt_reason is CMD_CMPLT and packet status is status good. 17009 * In addition: 17010 * - A residual of zero indicates successful completion no matter what 17011 * the command is. 17012 * - If the residual is not zero and the command is not a read or 17013 * write, then it's still defined as successful completion. In other 17014 * words, if the command is a read or write the residual must be 17015 * zero for successful completion. 17016 * - If the residual is not zero and the command is a read or 17017 * write, and it's a USCSICMD, then it's still defined as 17018 * successful completion. 17019 */ 17020 if ((pktp->pkt_reason == CMD_CMPLT) && 17021 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 17022 17023 /* 17024 * Since this command is returned with a good status, we 17025 * can reset the count for Sonoma failover. 17026 */ 17027 un->un_sonoma_failure_count = 0; 17028 17029 /* 17030 * Return all USCSI commands on good status 17031 */ 17032 if (pktp->pkt_resid == 0) { 17033 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17034 "sdintr: returning command for resid == 0\n"); 17035 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 17036 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 17037 SD_UPDATE_B_RESID(bp, pktp); 17038 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17039 "sdintr: returning command for resid != 0\n"); 17040 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17041 SD_UPDATE_B_RESID(bp, pktp); 17042 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17043 "sdintr: returning uscsi command\n"); 17044 } else { 17045 goto not_successful; 17046 } 17047 sd_return_command(un, bp); 17048 17049 /* 17050 * Decrement counter to indicate that the callback routine 17051 * is done. 17052 */ 17053 un->un_in_callback--; 17054 ASSERT(un->un_in_callback >= 0); 17055 mutex_exit(SD_MUTEX(un)); 17056 17057 return; 17058 } 17059 17060 not_successful: 17061 17062 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 17063 /* 17064 * The following is based upon knowledge of the underlying transport 17065 * and its use of DMA resources. This code should be removed when 17066 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 17067 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 17068 * and sd_start_cmds(). 17069 * 17070 * Free any DMA resources associated with this command if there 17071 * is a chance it could be retried or enqueued for later retry. 17072 * If we keep the DMA binding then mpxio cannot reissue the 17073 * command on another path whenever a path failure occurs. 17074 * 17075 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 17076 * causes the *entire* transfer to start over again from the 17077 * beginning of the request, even for PARTIAL chunks that 17078 * have already transferred successfully. 17079 * 17080 * This is only done for non-uscsi commands (and also skipped for the 17081 * driver's internal RQS command). Also just do this for Fibre Channel 17082 * devices as these are the only ones that support mpxio. 17083 */ 17084 if ((un->un_f_is_fibre == TRUE) && 17085 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 17086 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 17087 scsi_dmafree(pktp); 17088 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 17089 } 17090 #endif 17091 17092 /* 17093 * The command did not successfully complete as requested so check 17094 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 17095 * driver command that should not be retried so just return. If 17096 * FLAG_DIAGNOSE is not set the error will be processed below. 17097 */ 17098 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 17099 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17100 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 17101 /* 17102 * Issue a request sense if a check condition caused the error 17103 * (we handle the auto request sense case above), otherwise 17104 * just fail the command. 17105 */ 17106 if ((pktp->pkt_reason == CMD_CMPLT) && 17107 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 17108 sd_send_request_sense_command(un, bp, pktp); 17109 } else { 17110 sd_return_failed_command(un, bp, EIO); 17111 } 17112 goto exit; 17113 } 17114 17115 /* 17116 * The command did not successfully complete as requested so process 17117 * the error, retry, and/or attempt recovery. 17118 */ 17119 switch (pktp->pkt_reason) { 17120 case CMD_CMPLT: 17121 switch (SD_GET_PKT_STATUS(pktp)) { 17122 case STATUS_GOOD: 17123 /* 17124 * The command completed successfully with a non-zero 17125 * residual 17126 */ 17127 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17128 "sdintr: STATUS_GOOD \n"); 17129 sd_pkt_status_good(un, bp, xp, pktp); 17130 break; 17131 17132 case STATUS_CHECK: 17133 case STATUS_TERMINATED: 17134 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17135 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 17136 sd_pkt_status_check_condition(un, bp, xp, pktp); 17137 break; 17138 17139 case STATUS_BUSY: 17140 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17141 "sdintr: STATUS_BUSY\n"); 17142 sd_pkt_status_busy(un, bp, xp, pktp); 17143 break; 17144 17145 case STATUS_RESERVATION_CONFLICT: 17146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17147 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 17148 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17149 break; 17150 17151 case STATUS_QFULL: 17152 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17153 "sdintr: STATUS_QFULL\n"); 17154 sd_pkt_status_qfull(un, bp, xp, pktp); 17155 break; 17156 17157 case STATUS_MET: 17158 case STATUS_INTERMEDIATE: 17159 case STATUS_SCSI2: 17160 case STATUS_INTERMEDIATE_MET: 17161 case STATUS_ACA_ACTIVE: 17162 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17163 "Unexpected SCSI status received: 0x%x\n", 17164 SD_GET_PKT_STATUS(pktp)); 17165 /* 17166 * Mark the ssc_flags when detected invalid status 17167 * code for non-USCSI command. 17168 */ 17169 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17170 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17171 0, "stat-code"); 17172 } 17173 sd_return_failed_command(un, bp, EIO); 17174 break; 17175 17176 default: 17177 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17178 "Invalid SCSI status received: 0x%x\n", 17179 SD_GET_PKT_STATUS(pktp)); 17180 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17181 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17182 0, "stat-code"); 17183 } 17184 sd_return_failed_command(un, bp, EIO); 17185 break; 17186 17187 } 17188 break; 17189 17190 case CMD_INCOMPLETE: 17191 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17192 "sdintr: CMD_INCOMPLETE\n"); 17193 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 17194 break; 17195 case CMD_TRAN_ERR: 17196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17197 "sdintr: CMD_TRAN_ERR\n"); 17198 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 17199 break; 17200 case CMD_RESET: 17201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17202 "sdintr: CMD_RESET \n"); 17203 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 17204 break; 17205 case CMD_ABORTED: 17206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17207 "sdintr: CMD_ABORTED \n"); 17208 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 17209 break; 17210 case CMD_TIMEOUT: 17211 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17212 "sdintr: CMD_TIMEOUT\n"); 17213 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 17214 break; 17215 case CMD_UNX_BUS_FREE: 17216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17217 "sdintr: CMD_UNX_BUS_FREE \n"); 17218 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 17219 break; 17220 case CMD_TAG_REJECT: 17221 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17222 "sdintr: CMD_TAG_REJECT\n"); 17223 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 17224 break; 17225 default: 17226 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17227 "sdintr: default\n"); 17228 /* 17229 * Mark the ssc_flags for detecting invliad pkt_reason. 17230 */ 17231 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17232 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON, 17233 0, "pkt-reason"); 17234 } 17235 sd_pkt_reason_default(un, bp, xp, pktp); 17236 break; 17237 } 17238 17239 exit: 17240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 17241 17242 /* Decrement counter to indicate that the callback routine is done. */ 17243 un->un_in_callback--; 17244 ASSERT(un->un_in_callback >= 0); 17245 17246 /* 17247 * At this point, the pkt has been dispatched, ie, it is either 17248 * being re-tried or has been returned to its caller and should 17249 * not be referenced. 17250 */ 17251 17252 mutex_exit(SD_MUTEX(un)); 17253 } 17254 17255 17256 /* 17257 * Function: sd_print_incomplete_msg 17258 * 17259 * Description: Prints the error message for a CMD_INCOMPLETE error. 17260 * 17261 * Arguments: un - ptr to associated softstate for the device. 17262 * bp - ptr to the buf(9S) for the command. 17263 * arg - message string ptr 17264 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17265 * or SD_NO_RETRY_ISSUED. 17266 * 17267 * Context: May be called under interrupt context 17268 */ 17269 17270 static void 17271 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17272 { 17273 struct scsi_pkt *pktp; 17274 char *msgp; 17275 char *cmdp = arg; 17276 17277 ASSERT(un != NULL); 17278 ASSERT(mutex_owned(SD_MUTEX(un))); 17279 ASSERT(bp != NULL); 17280 ASSERT(arg != NULL); 17281 pktp = SD_GET_PKTP(bp); 17282 ASSERT(pktp != NULL); 17283 17284 switch (code) { 17285 case SD_DELAYED_RETRY_ISSUED: 17286 case SD_IMMEDIATE_RETRY_ISSUED: 17287 msgp = "retrying"; 17288 break; 17289 case SD_NO_RETRY_ISSUED: 17290 default: 17291 msgp = "giving up"; 17292 break; 17293 } 17294 17295 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17296 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17297 "incomplete %s- %s\n", cmdp, msgp); 17298 } 17299 } 17300 17301 17302 17303 /* 17304 * Function: sd_pkt_status_good 17305 * 17306 * Description: Processing for a STATUS_GOOD code in pkt_status. 17307 * 17308 * Context: May be called under interrupt context 17309 */ 17310 17311 static void 17312 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17313 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17314 { 17315 char *cmdp; 17316 17317 ASSERT(un != NULL); 17318 ASSERT(mutex_owned(SD_MUTEX(un))); 17319 ASSERT(bp != NULL); 17320 ASSERT(xp != NULL); 17321 ASSERT(pktp != NULL); 17322 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17323 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17324 ASSERT(pktp->pkt_resid != 0); 17325 17326 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17327 17328 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17329 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17330 case SCMD_READ: 17331 cmdp = "read"; 17332 break; 17333 case SCMD_WRITE: 17334 cmdp = "write"; 17335 break; 17336 default: 17337 SD_UPDATE_B_RESID(bp, pktp); 17338 sd_return_command(un, bp); 17339 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17340 return; 17341 } 17342 17343 /* 17344 * See if we can retry the read/write, preferrably immediately. 17345 * If retries are exhaused, then sd_retry_command() will update 17346 * the b_resid count. 17347 */ 17348 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17349 cmdp, EIO, (clock_t)0, NULL); 17350 17351 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17352 } 17353 17354 17355 17356 17357 17358 /* 17359 * Function: sd_handle_request_sense 17360 * 17361 * Description: Processing for non-auto Request Sense command. 17362 * 17363 * Arguments: un - ptr to associated softstate 17364 * sense_bp - ptr to buf(9S) for the RQS command 17365 * sense_xp - ptr to the sd_xbuf for the RQS command 17366 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17367 * 17368 * Context: May be called under interrupt context 17369 */ 17370 17371 static void 17372 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17373 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17374 { 17375 struct buf *cmd_bp; /* buf for the original command */ 17376 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17377 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17378 size_t actual_len; /* actual sense data length */ 17379 17380 ASSERT(un != NULL); 17381 ASSERT(mutex_owned(SD_MUTEX(un))); 17382 ASSERT(sense_bp != NULL); 17383 ASSERT(sense_xp != NULL); 17384 ASSERT(sense_pktp != NULL); 17385 17386 /* 17387 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17388 * RQS command and not the original command. 17389 */ 17390 ASSERT(sense_pktp == un->un_rqs_pktp); 17391 ASSERT(sense_bp == un->un_rqs_bp); 17392 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17393 (FLAG_SENSING | FLAG_HEAD)); 17394 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17395 FLAG_SENSING) == FLAG_SENSING); 17396 17397 /* These are the bp, xp, and pktp for the original command */ 17398 cmd_bp = sense_xp->xb_sense_bp; 17399 cmd_xp = SD_GET_XBUF(cmd_bp); 17400 cmd_pktp = SD_GET_PKTP(cmd_bp); 17401 17402 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17403 /* 17404 * The REQUEST SENSE command failed. Release the REQUEST 17405 * SENSE command for re-use, get back the bp for the original 17406 * command, and attempt to re-try the original command if 17407 * FLAG_DIAGNOSE is not set in the original packet. 17408 */ 17409 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17410 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17411 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17412 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17413 NULL, NULL, EIO, (clock_t)0, NULL); 17414 return; 17415 } 17416 } 17417 17418 /* 17419 * Save the relevant sense info into the xp for the original cmd. 17420 * 17421 * Note: if the request sense failed the state info will be zero 17422 * as set in sd_mark_rqs_busy() 17423 */ 17424 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17425 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17426 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 17427 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 17428 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 17429 SENSE_LENGTH)) { 17430 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17431 MAX_SENSE_LENGTH); 17432 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17433 } else { 17434 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17435 SENSE_LENGTH); 17436 if (actual_len < SENSE_LENGTH) { 17437 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 17438 } else { 17439 cmd_xp->xb_sense_resid = 0; 17440 } 17441 } 17442 17443 /* 17444 * Free up the RQS command.... 17445 * NOTE: 17446 * Must do this BEFORE calling sd_validate_sense_data! 17447 * sd_validate_sense_data may return the original command in 17448 * which case the pkt will be freed and the flags can no 17449 * longer be touched. 17450 * SD_MUTEX is held through this process until the command 17451 * is dispatched based upon the sense data, so there are 17452 * no race conditions. 17453 */ 17454 (void) sd_mark_rqs_idle(un, sense_xp); 17455 17456 /* 17457 * For a retryable command see if we have valid sense data, if so then 17458 * turn it over to sd_decode_sense() to figure out the right course of 17459 * action. Just fail a non-retryable command. 17460 */ 17461 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17462 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 17463 SD_SENSE_DATA_IS_VALID) { 17464 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17465 } 17466 } else { 17467 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17468 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17469 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17470 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17471 sd_return_failed_command(un, cmd_bp, EIO); 17472 } 17473 } 17474 17475 17476 17477 17478 /* 17479 * Function: sd_handle_auto_request_sense 17480 * 17481 * Description: Processing for auto-request sense information. 17482 * 17483 * Arguments: un - ptr to associated softstate 17484 * bp - ptr to buf(9S) for the command 17485 * xp - ptr to the sd_xbuf for the command 17486 * pktp - ptr to the scsi_pkt(9S) for the command 17487 * 17488 * Context: May be called under interrupt context 17489 */ 17490 17491 static void 17492 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17493 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17494 { 17495 struct scsi_arq_status *asp; 17496 size_t actual_len; 17497 17498 ASSERT(un != NULL); 17499 ASSERT(mutex_owned(SD_MUTEX(un))); 17500 ASSERT(bp != NULL); 17501 ASSERT(xp != NULL); 17502 ASSERT(pktp != NULL); 17503 ASSERT(pktp != un->un_rqs_pktp); 17504 ASSERT(bp != un->un_rqs_bp); 17505 17506 /* 17507 * For auto-request sense, we get a scsi_arq_status back from 17508 * the HBA, with the sense data in the sts_sensedata member. 17509 * The pkt_scbp of the packet points to this scsi_arq_status. 17510 */ 17511 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17512 17513 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17514 /* 17515 * The auto REQUEST SENSE failed; see if we can re-try 17516 * the original command. 17517 */ 17518 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17519 "auto request sense failed (reason=%s)\n", 17520 scsi_rname(asp->sts_rqpkt_reason)); 17521 17522 sd_reset_target(un, pktp); 17523 17524 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17525 NULL, NULL, EIO, (clock_t)0, NULL); 17526 return; 17527 } 17528 17529 /* Save the relevant sense info into the xp for the original cmd. */ 17530 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17531 xp->xb_sense_state = asp->sts_rqpkt_state; 17532 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17533 if (xp->xb_sense_state & STATE_XARQ_DONE) { 17534 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17535 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17536 MAX_SENSE_LENGTH); 17537 } else { 17538 if (xp->xb_sense_resid > SENSE_LENGTH) { 17539 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17540 } else { 17541 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 17542 } 17543 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17544 if ((((struct uscsi_cmd *) 17545 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 17546 xp->xb_sense_resid = (((struct uscsi_cmd *) 17547 (xp->xb_pktinfo))->uscsi_rqlen) - 17548 actual_len; 17549 } else { 17550 xp->xb_sense_resid = 0; 17551 } 17552 } 17553 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 17554 } 17555 17556 /* 17557 * See if we have valid sense data, if so then turn it over to 17558 * sd_decode_sense() to figure out the right course of action. 17559 */ 17560 if (sd_validate_sense_data(un, bp, xp, actual_len) == 17561 SD_SENSE_DATA_IS_VALID) { 17562 sd_decode_sense(un, bp, xp, pktp); 17563 } 17564 } 17565 17566 17567 /* 17568 * Function: sd_print_sense_failed_msg 17569 * 17570 * Description: Print log message when RQS has failed. 17571 * 17572 * Arguments: un - ptr to associated softstate 17573 * bp - ptr to buf(9S) for the command 17574 * arg - generic message string ptr 17575 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17576 * or SD_NO_RETRY_ISSUED 17577 * 17578 * Context: May be called from interrupt context 17579 */ 17580 17581 static void 17582 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17583 int code) 17584 { 17585 char *msgp = arg; 17586 17587 ASSERT(un != NULL); 17588 ASSERT(mutex_owned(SD_MUTEX(un))); 17589 ASSERT(bp != NULL); 17590 17591 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17592 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17593 } 17594 } 17595 17596 17597 /* 17598 * Function: sd_validate_sense_data 17599 * 17600 * Description: Check the given sense data for validity. 17601 * If the sense data is not valid, the command will 17602 * be either failed or retried! 17603 * 17604 * Return Code: SD_SENSE_DATA_IS_INVALID 17605 * SD_SENSE_DATA_IS_VALID 17606 * 17607 * Context: May be called from interrupt context 17608 */ 17609 17610 static int 17611 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17612 size_t actual_len) 17613 { 17614 struct scsi_extended_sense *esp; 17615 struct scsi_pkt *pktp; 17616 char *msgp = NULL; 17617 sd_ssc_t *sscp; 17618 17619 ASSERT(un != NULL); 17620 ASSERT(mutex_owned(SD_MUTEX(un))); 17621 ASSERT(bp != NULL); 17622 ASSERT(bp != un->un_rqs_bp); 17623 ASSERT(xp != NULL); 17624 ASSERT(un->un_fm_private != NULL); 17625 17626 pktp = SD_GET_PKTP(bp); 17627 ASSERT(pktp != NULL); 17628 17629 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 17630 ASSERT(sscp != NULL); 17631 17632 /* 17633 * Check the status of the RQS command (auto or manual). 17634 */ 17635 switch (xp->xb_sense_status & STATUS_MASK) { 17636 case STATUS_GOOD: 17637 break; 17638 17639 case STATUS_RESERVATION_CONFLICT: 17640 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17641 return (SD_SENSE_DATA_IS_INVALID); 17642 17643 case STATUS_BUSY: 17644 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17645 "Busy Status on REQUEST SENSE\n"); 17646 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17647 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17648 return (SD_SENSE_DATA_IS_INVALID); 17649 17650 case STATUS_QFULL: 17651 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17652 "QFULL Status on REQUEST SENSE\n"); 17653 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17654 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17655 return (SD_SENSE_DATA_IS_INVALID); 17656 17657 case STATUS_CHECK: 17658 case STATUS_TERMINATED: 17659 msgp = "Check Condition on REQUEST SENSE\n"; 17660 goto sense_failed; 17661 17662 default: 17663 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17664 goto sense_failed; 17665 } 17666 17667 /* 17668 * See if we got the minimum required amount of sense data. 17669 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17670 * or less. 17671 */ 17672 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17673 (actual_len == 0)) { 17674 msgp = "Request Sense couldn't get sense data\n"; 17675 goto sense_failed; 17676 } 17677 17678 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17679 msgp = "Not enough sense information\n"; 17680 /* Mark the ssc_flags for detecting invalid sense data */ 17681 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17682 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17683 "sense-data"); 17684 } 17685 goto sense_failed; 17686 } 17687 17688 /* 17689 * We require the extended sense data 17690 */ 17691 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17692 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17693 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17694 static char tmp[8]; 17695 static char buf[148]; 17696 char *p = (char *)(xp->xb_sense_data); 17697 int i; 17698 17699 mutex_enter(&sd_sense_mutex); 17700 (void) strcpy(buf, "undecodable sense information:"); 17701 for (i = 0; i < actual_len; i++) { 17702 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17703 (void) strcpy(&buf[strlen(buf)], tmp); 17704 } 17705 i = strlen(buf); 17706 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17707 17708 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 17709 scsi_log(SD_DEVINFO(un), sd_label, 17710 CE_WARN, buf); 17711 } 17712 mutex_exit(&sd_sense_mutex); 17713 } 17714 17715 /* Mark the ssc_flags for detecting invalid sense data */ 17716 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17717 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17718 "sense-data"); 17719 } 17720 17721 /* Note: Legacy behavior, fail the command with no retry */ 17722 sd_return_failed_command(un, bp, EIO); 17723 return (SD_SENSE_DATA_IS_INVALID); 17724 } 17725 17726 /* 17727 * Check that es_code is valid (es_class concatenated with es_code 17728 * make up the "response code" field. es_class will always be 7, so 17729 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17730 * format. 17731 */ 17732 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17733 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17734 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17735 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17736 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17737 /* Mark the ssc_flags for detecting invalid sense data */ 17738 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17739 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17740 "sense-data"); 17741 } 17742 goto sense_failed; 17743 } 17744 17745 return (SD_SENSE_DATA_IS_VALID); 17746 17747 sense_failed: 17748 /* 17749 * If the request sense failed (for whatever reason), attempt 17750 * to retry the original command. 17751 */ 17752 #if defined(__i386) || defined(__amd64) 17753 /* 17754 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17755 * sddef.h for Sparc platform, and x86 uses 1 binary 17756 * for both SCSI/FC. 17757 * The SD_RETRY_DELAY value need to be adjusted here 17758 * when SD_RETRY_DELAY change in sddef.h 17759 */ 17760 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17761 sd_print_sense_failed_msg, msgp, EIO, 17762 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17763 #else 17764 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17765 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17766 #endif 17767 17768 return (SD_SENSE_DATA_IS_INVALID); 17769 } 17770 17771 /* 17772 * Function: sd_decode_sense 17773 * 17774 * Description: Take recovery action(s) when SCSI Sense Data is received. 17775 * 17776 * Context: Interrupt context. 17777 */ 17778 17779 static void 17780 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17781 struct scsi_pkt *pktp) 17782 { 17783 uint8_t sense_key; 17784 17785 ASSERT(un != NULL); 17786 ASSERT(mutex_owned(SD_MUTEX(un))); 17787 ASSERT(bp != NULL); 17788 ASSERT(bp != un->un_rqs_bp); 17789 ASSERT(xp != NULL); 17790 ASSERT(pktp != NULL); 17791 17792 sense_key = scsi_sense_key(xp->xb_sense_data); 17793 17794 switch (sense_key) { 17795 case KEY_NO_SENSE: 17796 sd_sense_key_no_sense(un, bp, xp, pktp); 17797 break; 17798 case KEY_RECOVERABLE_ERROR: 17799 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17800 bp, xp, pktp); 17801 break; 17802 case KEY_NOT_READY: 17803 sd_sense_key_not_ready(un, xp->xb_sense_data, 17804 bp, xp, pktp); 17805 break; 17806 case KEY_MEDIUM_ERROR: 17807 case KEY_HARDWARE_ERROR: 17808 sd_sense_key_medium_or_hardware_error(un, 17809 xp->xb_sense_data, bp, xp, pktp); 17810 break; 17811 case KEY_ILLEGAL_REQUEST: 17812 sd_sense_key_illegal_request(un, bp, xp, pktp); 17813 break; 17814 case KEY_UNIT_ATTENTION: 17815 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17816 bp, xp, pktp); 17817 break; 17818 case KEY_WRITE_PROTECT: 17819 case KEY_VOLUME_OVERFLOW: 17820 case KEY_MISCOMPARE: 17821 sd_sense_key_fail_command(un, bp, xp, pktp); 17822 break; 17823 case KEY_BLANK_CHECK: 17824 sd_sense_key_blank_check(un, bp, xp, pktp); 17825 break; 17826 case KEY_ABORTED_COMMAND: 17827 sd_sense_key_aborted_command(un, bp, xp, pktp); 17828 break; 17829 case KEY_VENDOR_UNIQUE: 17830 case KEY_COPY_ABORTED: 17831 case KEY_EQUAL: 17832 case KEY_RESERVED: 17833 default: 17834 sd_sense_key_default(un, xp->xb_sense_data, 17835 bp, xp, pktp); 17836 break; 17837 } 17838 } 17839 17840 17841 /* 17842 * Function: sd_dump_memory 17843 * 17844 * Description: Debug logging routine to print the contents of a user provided 17845 * buffer. The output of the buffer is broken up into 256 byte 17846 * segments due to a size constraint of the scsi_log. 17847 * implementation. 17848 * 17849 * Arguments: un - ptr to softstate 17850 * comp - component mask 17851 * title - "title" string to preceed data when printed 17852 * data - ptr to data block to be printed 17853 * len - size of data block to be printed 17854 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17855 * 17856 * Context: May be called from interrupt context 17857 */ 17858 17859 #define SD_DUMP_MEMORY_BUF_SIZE 256 17860 17861 static char *sd_dump_format_string[] = { 17862 " 0x%02x", 17863 " %c" 17864 }; 17865 17866 static void 17867 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17868 int len, int fmt) 17869 { 17870 int i, j; 17871 int avail_count; 17872 int start_offset; 17873 int end_offset; 17874 size_t entry_len; 17875 char *bufp; 17876 char *local_buf; 17877 char *format_string; 17878 17879 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17880 17881 /* 17882 * In the debug version of the driver, this function is called from a 17883 * number of places which are NOPs in the release driver. 17884 * The debug driver therefore has additional methods of filtering 17885 * debug output. 17886 */ 17887 #ifdef SDDEBUG 17888 /* 17889 * In the debug version of the driver we can reduce the amount of debug 17890 * messages by setting sd_error_level to something other than 17891 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17892 * sd_component_mask. 17893 */ 17894 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17895 (sd_error_level != SCSI_ERR_ALL)) { 17896 return; 17897 } 17898 if (((sd_component_mask & comp) == 0) || 17899 (sd_error_level != SCSI_ERR_ALL)) { 17900 return; 17901 } 17902 #else 17903 if (sd_error_level != SCSI_ERR_ALL) { 17904 return; 17905 } 17906 #endif 17907 17908 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17909 bufp = local_buf; 17910 /* 17911 * Available length is the length of local_buf[], minus the 17912 * length of the title string, minus one for the ":", minus 17913 * one for the newline, minus one for the NULL terminator. 17914 * This gives the #bytes available for holding the printed 17915 * values from the given data buffer. 17916 */ 17917 if (fmt == SD_LOG_HEX) { 17918 format_string = sd_dump_format_string[0]; 17919 } else /* SD_LOG_CHAR */ { 17920 format_string = sd_dump_format_string[1]; 17921 } 17922 /* 17923 * Available count is the number of elements from the given 17924 * data buffer that we can fit into the available length. 17925 * This is based upon the size of the format string used. 17926 * Make one entry and find it's size. 17927 */ 17928 (void) sprintf(bufp, format_string, data[0]); 17929 entry_len = strlen(bufp); 17930 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17931 17932 j = 0; 17933 while (j < len) { 17934 bufp = local_buf; 17935 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17936 start_offset = j; 17937 17938 end_offset = start_offset + avail_count; 17939 17940 (void) sprintf(bufp, "%s:", title); 17941 bufp += strlen(bufp); 17942 for (i = start_offset; ((i < end_offset) && (j < len)); 17943 i++, j++) { 17944 (void) sprintf(bufp, format_string, data[i]); 17945 bufp += entry_len; 17946 } 17947 (void) sprintf(bufp, "\n"); 17948 17949 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17950 } 17951 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17952 } 17953 17954 /* 17955 * Function: sd_print_sense_msg 17956 * 17957 * Description: Log a message based upon the given sense data. 17958 * 17959 * Arguments: un - ptr to associated softstate 17960 * bp - ptr to buf(9S) for the command 17961 * arg - ptr to associate sd_sense_info struct 17962 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17963 * or SD_NO_RETRY_ISSUED 17964 * 17965 * Context: May be called from interrupt context 17966 */ 17967 17968 static void 17969 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17970 { 17971 struct sd_xbuf *xp; 17972 struct scsi_pkt *pktp; 17973 uint8_t *sensep; 17974 daddr_t request_blkno; 17975 diskaddr_t err_blkno; 17976 int severity; 17977 int pfa_flag; 17978 extern struct scsi_key_strings scsi_cmds[]; 17979 17980 ASSERT(un != NULL); 17981 ASSERT(mutex_owned(SD_MUTEX(un))); 17982 ASSERT(bp != NULL); 17983 xp = SD_GET_XBUF(bp); 17984 ASSERT(xp != NULL); 17985 pktp = SD_GET_PKTP(bp); 17986 ASSERT(pktp != NULL); 17987 ASSERT(arg != NULL); 17988 17989 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17990 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17991 17992 if ((code == SD_DELAYED_RETRY_ISSUED) || 17993 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17994 severity = SCSI_ERR_RETRYABLE; 17995 } 17996 17997 /* Use absolute block number for the request block number */ 17998 request_blkno = xp->xb_blkno; 17999 18000 /* 18001 * Now try to get the error block number from the sense data 18002 */ 18003 sensep = xp->xb_sense_data; 18004 18005 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 18006 (uint64_t *)&err_blkno)) { 18007 /* 18008 * We retrieved the error block number from the information 18009 * portion of the sense data. 18010 * 18011 * For USCSI commands we are better off using the error 18012 * block no. as the requested block no. (This is the best 18013 * we can estimate.) 18014 */ 18015 if ((SD_IS_BUFIO(xp) == FALSE) && 18016 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 18017 request_blkno = err_blkno; 18018 } 18019 } else { 18020 /* 18021 * Without the es_valid bit set (for fixed format) or an 18022 * information descriptor (for descriptor format) we cannot 18023 * be certain of the error blkno, so just use the 18024 * request_blkno. 18025 */ 18026 err_blkno = (diskaddr_t)request_blkno; 18027 } 18028 18029 /* 18030 * The following will log the buffer contents for the release driver 18031 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 18032 * level is set to verbose. 18033 */ 18034 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 18035 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 18036 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 18037 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 18038 18039 if (pfa_flag == FALSE) { 18040 /* This is normally only set for USCSI */ 18041 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 18042 return; 18043 } 18044 18045 if ((SD_IS_BUFIO(xp) == TRUE) && 18046 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 18047 (severity < sd_error_level))) { 18048 return; 18049 } 18050 } 18051 /* 18052 * Check for Sonoma Failover and keep a count of how many failed I/O's 18053 */ 18054 if ((SD_IS_LSI(un)) && 18055 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 18056 (scsi_sense_asc(sensep) == 0x94) && 18057 (scsi_sense_ascq(sensep) == 0x01)) { 18058 un->un_sonoma_failure_count++; 18059 if (un->un_sonoma_failure_count > 1) { 18060 return; 18061 } 18062 } 18063 18064 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP || 18065 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) && 18066 (pktp->pkt_resid == 0))) { 18067 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 18068 request_blkno, err_blkno, scsi_cmds, 18069 (struct scsi_extended_sense *)sensep, 18070 un->un_additional_codes, NULL); 18071 } 18072 } 18073 18074 /* 18075 * Function: sd_sense_key_no_sense 18076 * 18077 * Description: Recovery action when sense data was not received. 18078 * 18079 * Context: May be called from interrupt context 18080 */ 18081 18082 static void 18083 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 18084 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18085 { 18086 struct sd_sense_info si; 18087 18088 ASSERT(un != NULL); 18089 ASSERT(mutex_owned(SD_MUTEX(un))); 18090 ASSERT(bp != NULL); 18091 ASSERT(xp != NULL); 18092 ASSERT(pktp != NULL); 18093 18094 si.ssi_severity = SCSI_ERR_FATAL; 18095 si.ssi_pfa_flag = FALSE; 18096 18097 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18098 18099 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18100 &si, EIO, (clock_t)0, NULL); 18101 } 18102 18103 18104 /* 18105 * Function: sd_sense_key_recoverable_error 18106 * 18107 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 18108 * 18109 * Context: May be called from interrupt context 18110 */ 18111 18112 static void 18113 sd_sense_key_recoverable_error(struct sd_lun *un, 18114 uint8_t *sense_datap, 18115 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18116 { 18117 struct sd_sense_info si; 18118 uint8_t asc = scsi_sense_asc(sense_datap); 18119 18120 ASSERT(un != NULL); 18121 ASSERT(mutex_owned(SD_MUTEX(un))); 18122 ASSERT(bp != NULL); 18123 ASSERT(xp != NULL); 18124 ASSERT(pktp != NULL); 18125 18126 /* 18127 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 18128 */ 18129 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 18130 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18131 si.ssi_severity = SCSI_ERR_INFO; 18132 si.ssi_pfa_flag = TRUE; 18133 } else { 18134 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18135 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 18136 si.ssi_severity = SCSI_ERR_RECOVERED; 18137 si.ssi_pfa_flag = FALSE; 18138 } 18139 18140 if (pktp->pkt_resid == 0) { 18141 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18142 sd_return_command(un, bp); 18143 return; 18144 } 18145 18146 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18147 &si, EIO, (clock_t)0, NULL); 18148 } 18149 18150 18151 18152 18153 /* 18154 * Function: sd_sense_key_not_ready 18155 * 18156 * Description: Recovery actions for a SCSI "Not Ready" sense key. 18157 * 18158 * Context: May be called from interrupt context 18159 */ 18160 18161 static void 18162 sd_sense_key_not_ready(struct sd_lun *un, 18163 uint8_t *sense_datap, 18164 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18165 { 18166 struct sd_sense_info si; 18167 uint8_t asc = scsi_sense_asc(sense_datap); 18168 uint8_t ascq = scsi_sense_ascq(sense_datap); 18169 18170 ASSERT(un != NULL); 18171 ASSERT(mutex_owned(SD_MUTEX(un))); 18172 ASSERT(bp != NULL); 18173 ASSERT(xp != NULL); 18174 ASSERT(pktp != NULL); 18175 18176 si.ssi_severity = SCSI_ERR_FATAL; 18177 si.ssi_pfa_flag = FALSE; 18178 18179 /* 18180 * Update error stats after first NOT READY error. Disks may have 18181 * been powered down and may need to be restarted. For CDROMs, 18182 * report NOT READY errors only if media is present. 18183 */ 18184 if ((ISCD(un) && (asc == 0x3A)) || 18185 (xp->xb_nr_retry_count > 0)) { 18186 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18187 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 18188 } 18189 18190 /* 18191 * Just fail if the "not ready" retry limit has been reached. 18192 */ 18193 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 18194 /* Special check for error message printing for removables. */ 18195 if (un->un_f_has_removable_media && (asc == 0x04) && 18196 (ascq >= 0x04)) { 18197 si.ssi_severity = SCSI_ERR_ALL; 18198 } 18199 goto fail_command; 18200 } 18201 18202 /* 18203 * Check the ASC and ASCQ in the sense data as needed, to determine 18204 * what to do. 18205 */ 18206 switch (asc) { 18207 case 0x04: /* LOGICAL UNIT NOT READY */ 18208 /* 18209 * disk drives that don't spin up result in a very long delay 18210 * in format without warning messages. We will log a message 18211 * if the error level is set to verbose. 18212 */ 18213 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18214 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18215 "logical unit not ready, resetting disk\n"); 18216 } 18217 18218 /* 18219 * There are different requirements for CDROMs and disks for 18220 * the number of retries. If a CD-ROM is giving this, it is 18221 * probably reading TOC and is in the process of getting 18222 * ready, so we should keep on trying for a long time to make 18223 * sure that all types of media are taken in account (for 18224 * some media the drive takes a long time to read TOC). For 18225 * disks we do not want to retry this too many times as this 18226 * can cause a long hang in format when the drive refuses to 18227 * spin up (a very common failure). 18228 */ 18229 switch (ascq) { 18230 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 18231 /* 18232 * Disk drives frequently refuse to spin up which 18233 * results in a very long hang in format without 18234 * warning messages. 18235 * 18236 * Note: This code preserves the legacy behavior of 18237 * comparing xb_nr_retry_count against zero for fibre 18238 * channel targets instead of comparing against the 18239 * un_reset_retry_count value. The reason for this 18240 * discrepancy has been so utterly lost beneath the 18241 * Sands of Time that even Indiana Jones could not 18242 * find it. 18243 */ 18244 if (un->un_f_is_fibre == TRUE) { 18245 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18246 (xp->xb_nr_retry_count > 0)) && 18247 (un->un_startstop_timeid == NULL)) { 18248 scsi_log(SD_DEVINFO(un), sd_label, 18249 CE_WARN, "logical unit not ready, " 18250 "resetting disk\n"); 18251 sd_reset_target(un, pktp); 18252 } 18253 } else { 18254 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18255 (xp->xb_nr_retry_count > 18256 un->un_reset_retry_count)) && 18257 (un->un_startstop_timeid == NULL)) { 18258 scsi_log(SD_DEVINFO(un), sd_label, 18259 CE_WARN, "logical unit not ready, " 18260 "resetting disk\n"); 18261 sd_reset_target(un, pktp); 18262 } 18263 } 18264 break; 18265 18266 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 18267 /* 18268 * If the target is in the process of becoming 18269 * ready, just proceed with the retry. This can 18270 * happen with CD-ROMs that take a long time to 18271 * read TOC after a power cycle or reset. 18272 */ 18273 goto do_retry; 18274 18275 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 18276 break; 18277 18278 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 18279 /* 18280 * Retries cannot help here so just fail right away. 18281 */ 18282 goto fail_command; 18283 18284 case 0x88: 18285 /* 18286 * Vendor-unique code for T3/T4: it indicates a 18287 * path problem in a mutipathed config, but as far as 18288 * the target driver is concerned it equates to a fatal 18289 * error, so we should just fail the command right away 18290 * (without printing anything to the console). If this 18291 * is not a T3/T4, fall thru to the default recovery 18292 * action. 18293 * T3/T4 is FC only, don't need to check is_fibre 18294 */ 18295 if (SD_IS_T3(un) || SD_IS_T4(un)) { 18296 sd_return_failed_command(un, bp, EIO); 18297 return; 18298 } 18299 /* FALLTHRU */ 18300 18301 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 18302 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 18303 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 18304 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 18305 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 18306 default: /* Possible future codes in SCSI spec? */ 18307 /* 18308 * For removable-media devices, do not retry if 18309 * ASCQ > 2 as these result mostly from USCSI commands 18310 * on MMC devices issued to check status of an 18311 * operation initiated in immediate mode. Also for 18312 * ASCQ >= 4 do not print console messages as these 18313 * mainly represent a user-initiated operation 18314 * instead of a system failure. 18315 */ 18316 if (un->un_f_has_removable_media) { 18317 si.ssi_severity = SCSI_ERR_ALL; 18318 goto fail_command; 18319 } 18320 break; 18321 } 18322 18323 /* 18324 * As part of our recovery attempt for the NOT READY 18325 * condition, we issue a START STOP UNIT command. However 18326 * we want to wait for a short delay before attempting this 18327 * as there may still be more commands coming back from the 18328 * target with the check condition. To do this we use 18329 * timeout(9F) to call sd_start_stop_unit_callback() after 18330 * the delay interval expires. (sd_start_stop_unit_callback() 18331 * dispatches sd_start_stop_unit_task(), which will issue 18332 * the actual START STOP UNIT command. The delay interval 18333 * is one-half of the delay that we will use to retry the 18334 * command that generated the NOT READY condition. 18335 * 18336 * Note that we could just dispatch sd_start_stop_unit_task() 18337 * from here and allow it to sleep for the delay interval, 18338 * but then we would be tying up the taskq thread 18339 * uncesessarily for the duration of the delay. 18340 * 18341 * Do not issue the START STOP UNIT if the current command 18342 * is already a START STOP UNIT. 18343 */ 18344 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18345 break; 18346 } 18347 18348 /* 18349 * Do not schedule the timeout if one is already pending. 18350 */ 18351 if (un->un_startstop_timeid != NULL) { 18352 SD_INFO(SD_LOG_ERROR, un, 18353 "sd_sense_key_not_ready: restart already issued to" 18354 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18355 ddi_get_instance(SD_DEVINFO(un))); 18356 break; 18357 } 18358 18359 /* 18360 * Schedule the START STOP UNIT command, then queue the command 18361 * for a retry. 18362 * 18363 * Note: A timeout is not scheduled for this retry because we 18364 * want the retry to be serial with the START_STOP_UNIT. The 18365 * retry will be started when the START_STOP_UNIT is completed 18366 * in sd_start_stop_unit_task. 18367 */ 18368 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18369 un, un->un_busy_timeout / 2); 18370 xp->xb_nr_retry_count++; 18371 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18372 return; 18373 18374 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18375 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18376 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18377 "unit does not respond to selection\n"); 18378 } 18379 break; 18380 18381 case 0x3A: /* MEDIUM NOT PRESENT */ 18382 if (sd_error_level >= SCSI_ERR_FATAL) { 18383 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18384 "Caddy not inserted in drive\n"); 18385 } 18386 18387 sr_ejected(un); 18388 un->un_mediastate = DKIO_EJECTED; 18389 /* The state has changed, inform the media watch routines */ 18390 cv_broadcast(&un->un_state_cv); 18391 /* Just fail if no media is present in the drive. */ 18392 goto fail_command; 18393 18394 default: 18395 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18396 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18397 "Unit not Ready. Additional sense code 0x%x\n", 18398 asc); 18399 } 18400 break; 18401 } 18402 18403 do_retry: 18404 18405 /* 18406 * Retry the command, as some targets may report NOT READY for 18407 * several seconds after being reset. 18408 */ 18409 xp->xb_nr_retry_count++; 18410 si.ssi_severity = SCSI_ERR_RETRYABLE; 18411 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18412 &si, EIO, un->un_busy_timeout, NULL); 18413 18414 return; 18415 18416 fail_command: 18417 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18418 sd_return_failed_command(un, bp, EIO); 18419 } 18420 18421 18422 18423 /* 18424 * Function: sd_sense_key_medium_or_hardware_error 18425 * 18426 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18427 * sense key. 18428 * 18429 * Context: May be called from interrupt context 18430 */ 18431 18432 static void 18433 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18434 uint8_t *sense_datap, 18435 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18436 { 18437 struct sd_sense_info si; 18438 uint8_t sense_key = scsi_sense_key(sense_datap); 18439 uint8_t asc = scsi_sense_asc(sense_datap); 18440 18441 ASSERT(un != NULL); 18442 ASSERT(mutex_owned(SD_MUTEX(un))); 18443 ASSERT(bp != NULL); 18444 ASSERT(xp != NULL); 18445 ASSERT(pktp != NULL); 18446 18447 si.ssi_severity = SCSI_ERR_FATAL; 18448 si.ssi_pfa_flag = FALSE; 18449 18450 if (sense_key == KEY_MEDIUM_ERROR) { 18451 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18452 } 18453 18454 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18455 18456 if ((un->un_reset_retry_count != 0) && 18457 (xp->xb_retry_count == un->un_reset_retry_count)) { 18458 mutex_exit(SD_MUTEX(un)); 18459 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18460 if (un->un_f_allow_bus_device_reset == TRUE) { 18461 18462 boolean_t try_resetting_target = B_TRUE; 18463 18464 /* 18465 * We need to be able to handle specific ASC when we are 18466 * handling a KEY_HARDWARE_ERROR. In particular 18467 * taking the default action of resetting the target may 18468 * not be the appropriate way to attempt recovery. 18469 * Resetting a target because of a single LUN failure 18470 * victimizes all LUNs on that target. 18471 * 18472 * This is true for the LSI arrays, if an LSI 18473 * array controller returns an ASC of 0x84 (LUN Dead) we 18474 * should trust it. 18475 */ 18476 18477 if (sense_key == KEY_HARDWARE_ERROR) { 18478 switch (asc) { 18479 case 0x84: 18480 if (SD_IS_LSI(un)) { 18481 try_resetting_target = B_FALSE; 18482 } 18483 break; 18484 default: 18485 break; 18486 } 18487 } 18488 18489 if (try_resetting_target == B_TRUE) { 18490 int reset_retval = 0; 18491 if (un->un_f_lun_reset_enabled == TRUE) { 18492 SD_TRACE(SD_LOG_IO_CORE, un, 18493 "sd_sense_key_medium_or_hardware_" 18494 "error: issuing RESET_LUN\n"); 18495 reset_retval = 18496 scsi_reset(SD_ADDRESS(un), 18497 RESET_LUN); 18498 } 18499 if (reset_retval == 0) { 18500 SD_TRACE(SD_LOG_IO_CORE, un, 18501 "sd_sense_key_medium_or_hardware_" 18502 "error: issuing RESET_TARGET\n"); 18503 (void) scsi_reset(SD_ADDRESS(un), 18504 RESET_TARGET); 18505 } 18506 } 18507 } 18508 mutex_enter(SD_MUTEX(un)); 18509 } 18510 18511 /* 18512 * This really ought to be a fatal error, but we will retry anyway 18513 * as some drives report this as a spurious error. 18514 */ 18515 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18516 &si, EIO, (clock_t)0, NULL); 18517 } 18518 18519 18520 18521 /* 18522 * Function: sd_sense_key_illegal_request 18523 * 18524 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18525 * 18526 * Context: May be called from interrupt context 18527 */ 18528 18529 static void 18530 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18531 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18532 { 18533 struct sd_sense_info si; 18534 18535 ASSERT(un != NULL); 18536 ASSERT(mutex_owned(SD_MUTEX(un))); 18537 ASSERT(bp != NULL); 18538 ASSERT(xp != NULL); 18539 ASSERT(pktp != NULL); 18540 18541 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18542 18543 si.ssi_severity = SCSI_ERR_INFO; 18544 si.ssi_pfa_flag = FALSE; 18545 18546 /* Pointless to retry if the target thinks it's an illegal request */ 18547 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18548 sd_return_failed_command(un, bp, EIO); 18549 } 18550 18551 18552 18553 18554 /* 18555 * Function: sd_sense_key_unit_attention 18556 * 18557 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18558 * 18559 * Context: May be called from interrupt context 18560 */ 18561 18562 static void 18563 sd_sense_key_unit_attention(struct sd_lun *un, 18564 uint8_t *sense_datap, 18565 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18566 { 18567 /* 18568 * For UNIT ATTENTION we allow retries for one minute. Devices 18569 * like Sonoma can return UNIT ATTENTION close to a minute 18570 * under certain conditions. 18571 */ 18572 int retry_check_flag = SD_RETRIES_UA; 18573 boolean_t kstat_updated = B_FALSE; 18574 struct sd_sense_info si; 18575 uint8_t asc = scsi_sense_asc(sense_datap); 18576 uint8_t ascq = scsi_sense_ascq(sense_datap); 18577 18578 ASSERT(un != NULL); 18579 ASSERT(mutex_owned(SD_MUTEX(un))); 18580 ASSERT(bp != NULL); 18581 ASSERT(xp != NULL); 18582 ASSERT(pktp != NULL); 18583 18584 si.ssi_severity = SCSI_ERR_INFO; 18585 si.ssi_pfa_flag = FALSE; 18586 18587 18588 switch (asc) { 18589 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18590 if (sd_report_pfa != 0) { 18591 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18592 si.ssi_pfa_flag = TRUE; 18593 retry_check_flag = SD_RETRIES_STANDARD; 18594 goto do_retry; 18595 } 18596 18597 break; 18598 18599 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18600 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18601 un->un_resvd_status |= 18602 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18603 } 18604 #ifdef _LP64 18605 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18606 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18607 un, KM_NOSLEEP) == 0) { 18608 /* 18609 * If we can't dispatch the task we'll just 18610 * live without descriptor sense. We can 18611 * try again on the next "unit attention" 18612 */ 18613 SD_ERROR(SD_LOG_ERROR, un, 18614 "sd_sense_key_unit_attention: " 18615 "Could not dispatch " 18616 "sd_reenable_dsense_task\n"); 18617 } 18618 } 18619 #endif /* _LP64 */ 18620 /* FALLTHRU */ 18621 18622 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18623 if (!un->un_f_has_removable_media) { 18624 break; 18625 } 18626 18627 /* 18628 * When we get a unit attention from a removable-media device, 18629 * it may be in a state that will take a long time to recover 18630 * (e.g., from a reset). Since we are executing in interrupt 18631 * context here, we cannot wait around for the device to come 18632 * back. So hand this command off to sd_media_change_task() 18633 * for deferred processing under taskq thread context. (Note 18634 * that the command still may be failed if a problem is 18635 * encountered at a later time.) 18636 */ 18637 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18638 KM_NOSLEEP) == 0) { 18639 /* 18640 * Cannot dispatch the request so fail the command. 18641 */ 18642 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18643 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18644 si.ssi_severity = SCSI_ERR_FATAL; 18645 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18646 sd_return_failed_command(un, bp, EIO); 18647 } 18648 18649 /* 18650 * If failed to dispatch sd_media_change_task(), we already 18651 * updated kstat. If succeed to dispatch sd_media_change_task(), 18652 * we should update kstat later if it encounters an error. So, 18653 * we update kstat_updated flag here. 18654 */ 18655 kstat_updated = B_TRUE; 18656 18657 /* 18658 * Either the command has been successfully dispatched to a 18659 * task Q for retrying, or the dispatch failed. In either case 18660 * do NOT retry again by calling sd_retry_command. This sets up 18661 * two retries of the same command and when one completes and 18662 * frees the resources the other will access freed memory, 18663 * a bad thing. 18664 */ 18665 return; 18666 18667 default: 18668 break; 18669 } 18670 18671 /* 18672 * ASC ASCQ 18673 * 2A 09 Capacity data has changed 18674 * 2A 01 Mode parameters changed 18675 * 3F 0E Reported luns data has changed 18676 * Arrays that support logical unit expansion should report 18677 * capacity changes(2Ah/09). Mode parameters changed and 18678 * reported luns data has changed are the approximation. 18679 */ 18680 if (((asc == 0x2a) && (ascq == 0x09)) || 18681 ((asc == 0x2a) && (ascq == 0x01)) || 18682 ((asc == 0x3f) && (ascq == 0x0e))) { 18683 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 18684 KM_NOSLEEP) == 0) { 18685 SD_ERROR(SD_LOG_ERROR, un, 18686 "sd_sense_key_unit_attention: " 18687 "Could not dispatch sd_target_change_task\n"); 18688 } 18689 } 18690 18691 /* 18692 * Update kstat if we haven't done that. 18693 */ 18694 if (!kstat_updated) { 18695 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18696 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18697 } 18698 18699 do_retry: 18700 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18701 EIO, SD_UA_RETRY_DELAY, NULL); 18702 } 18703 18704 18705 18706 /* 18707 * Function: sd_sense_key_fail_command 18708 * 18709 * Description: Use to fail a command when we don't like the sense key that 18710 * was returned. 18711 * 18712 * Context: May be called from interrupt context 18713 */ 18714 18715 static void 18716 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18717 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18718 { 18719 struct sd_sense_info si; 18720 18721 ASSERT(un != NULL); 18722 ASSERT(mutex_owned(SD_MUTEX(un))); 18723 ASSERT(bp != NULL); 18724 ASSERT(xp != NULL); 18725 ASSERT(pktp != NULL); 18726 18727 si.ssi_severity = SCSI_ERR_FATAL; 18728 si.ssi_pfa_flag = FALSE; 18729 18730 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18731 sd_return_failed_command(un, bp, EIO); 18732 } 18733 18734 18735 18736 /* 18737 * Function: sd_sense_key_blank_check 18738 * 18739 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18740 * Has no monetary connotation. 18741 * 18742 * Context: May be called from interrupt context 18743 */ 18744 18745 static void 18746 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18747 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18748 { 18749 struct sd_sense_info si; 18750 18751 ASSERT(un != NULL); 18752 ASSERT(mutex_owned(SD_MUTEX(un))); 18753 ASSERT(bp != NULL); 18754 ASSERT(xp != NULL); 18755 ASSERT(pktp != NULL); 18756 18757 /* 18758 * Blank check is not fatal for removable devices, therefore 18759 * it does not require a console message. 18760 */ 18761 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18762 SCSI_ERR_FATAL; 18763 si.ssi_pfa_flag = FALSE; 18764 18765 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18766 sd_return_failed_command(un, bp, EIO); 18767 } 18768 18769 18770 18771 18772 /* 18773 * Function: sd_sense_key_aborted_command 18774 * 18775 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18776 * 18777 * Context: May be called from interrupt context 18778 */ 18779 18780 static void 18781 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18782 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18783 { 18784 struct sd_sense_info si; 18785 18786 ASSERT(un != NULL); 18787 ASSERT(mutex_owned(SD_MUTEX(un))); 18788 ASSERT(bp != NULL); 18789 ASSERT(xp != NULL); 18790 ASSERT(pktp != NULL); 18791 18792 si.ssi_severity = SCSI_ERR_FATAL; 18793 si.ssi_pfa_flag = FALSE; 18794 18795 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18796 18797 /* 18798 * This really ought to be a fatal error, but we will retry anyway 18799 * as some drives report this as a spurious error. 18800 */ 18801 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18802 &si, EIO, drv_usectohz(100000), NULL); 18803 } 18804 18805 18806 18807 /* 18808 * Function: sd_sense_key_default 18809 * 18810 * Description: Default recovery action for several SCSI sense keys (basically 18811 * attempts a retry). 18812 * 18813 * Context: May be called from interrupt context 18814 */ 18815 18816 static void 18817 sd_sense_key_default(struct sd_lun *un, 18818 uint8_t *sense_datap, 18819 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18820 { 18821 struct sd_sense_info si; 18822 uint8_t sense_key = scsi_sense_key(sense_datap); 18823 18824 ASSERT(un != NULL); 18825 ASSERT(mutex_owned(SD_MUTEX(un))); 18826 ASSERT(bp != NULL); 18827 ASSERT(xp != NULL); 18828 ASSERT(pktp != NULL); 18829 18830 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18831 18832 /* 18833 * Undecoded sense key. Attempt retries and hope that will fix 18834 * the problem. Otherwise, we're dead. 18835 */ 18836 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18837 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18838 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18839 } 18840 18841 si.ssi_severity = SCSI_ERR_FATAL; 18842 si.ssi_pfa_flag = FALSE; 18843 18844 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18845 &si, EIO, (clock_t)0, NULL); 18846 } 18847 18848 18849 18850 /* 18851 * Function: sd_print_retry_msg 18852 * 18853 * Description: Print a message indicating the retry action being taken. 18854 * 18855 * Arguments: un - ptr to associated softstate 18856 * bp - ptr to buf(9S) for the command 18857 * arg - not used. 18858 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18859 * or SD_NO_RETRY_ISSUED 18860 * 18861 * Context: May be called from interrupt context 18862 */ 18863 /* ARGSUSED */ 18864 static void 18865 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18866 { 18867 struct sd_xbuf *xp; 18868 struct scsi_pkt *pktp; 18869 char *reasonp; 18870 char *msgp; 18871 18872 ASSERT(un != NULL); 18873 ASSERT(mutex_owned(SD_MUTEX(un))); 18874 ASSERT(bp != NULL); 18875 pktp = SD_GET_PKTP(bp); 18876 ASSERT(pktp != NULL); 18877 xp = SD_GET_XBUF(bp); 18878 ASSERT(xp != NULL); 18879 18880 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18881 mutex_enter(&un->un_pm_mutex); 18882 if ((un->un_state == SD_STATE_SUSPENDED) || 18883 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18884 (pktp->pkt_flags & FLAG_SILENT)) { 18885 mutex_exit(&un->un_pm_mutex); 18886 goto update_pkt_reason; 18887 } 18888 mutex_exit(&un->un_pm_mutex); 18889 18890 /* 18891 * Suppress messages if they are all the same pkt_reason; with 18892 * TQ, many (up to 256) are returned with the same pkt_reason. 18893 * If we are in panic, then suppress the retry messages. 18894 */ 18895 switch (flag) { 18896 case SD_NO_RETRY_ISSUED: 18897 msgp = "giving up"; 18898 break; 18899 case SD_IMMEDIATE_RETRY_ISSUED: 18900 case SD_DELAYED_RETRY_ISSUED: 18901 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18902 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18903 (sd_error_level != SCSI_ERR_ALL))) { 18904 return; 18905 } 18906 msgp = "retrying command"; 18907 break; 18908 default: 18909 goto update_pkt_reason; 18910 } 18911 18912 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18913 scsi_rname(pktp->pkt_reason)); 18914 18915 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 18916 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18917 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18918 } 18919 18920 update_pkt_reason: 18921 /* 18922 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18923 * This is to prevent multiple console messages for the same failure 18924 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18925 * when the command is retried successfully because there still may be 18926 * more commands coming back with the same value of pktp->pkt_reason. 18927 */ 18928 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18929 un->un_last_pkt_reason = pktp->pkt_reason; 18930 } 18931 } 18932 18933 18934 /* 18935 * Function: sd_print_cmd_incomplete_msg 18936 * 18937 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18938 * 18939 * Arguments: un - ptr to associated softstate 18940 * bp - ptr to buf(9S) for the command 18941 * arg - passed to sd_print_retry_msg() 18942 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18943 * or SD_NO_RETRY_ISSUED 18944 * 18945 * Context: May be called from interrupt context 18946 */ 18947 18948 static void 18949 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18950 int code) 18951 { 18952 dev_info_t *dip; 18953 18954 ASSERT(un != NULL); 18955 ASSERT(mutex_owned(SD_MUTEX(un))); 18956 ASSERT(bp != NULL); 18957 18958 switch (code) { 18959 case SD_NO_RETRY_ISSUED: 18960 /* Command was failed. Someone turned off this target? */ 18961 if (un->un_state != SD_STATE_OFFLINE) { 18962 /* 18963 * Suppress message if we are detaching and 18964 * device has been disconnected 18965 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18966 * private interface and not part of the DDI 18967 */ 18968 dip = un->un_sd->sd_dev; 18969 if (!(DEVI_IS_DETACHING(dip) && 18970 DEVI_IS_DEVICE_REMOVED(dip))) { 18971 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18972 "disk not responding to selection\n"); 18973 } 18974 New_state(un, SD_STATE_OFFLINE); 18975 } 18976 break; 18977 18978 case SD_DELAYED_RETRY_ISSUED: 18979 case SD_IMMEDIATE_RETRY_ISSUED: 18980 default: 18981 /* Command was successfully queued for retry */ 18982 sd_print_retry_msg(un, bp, arg, code); 18983 break; 18984 } 18985 } 18986 18987 18988 /* 18989 * Function: sd_pkt_reason_cmd_incomplete 18990 * 18991 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18992 * 18993 * Context: May be called from interrupt context 18994 */ 18995 18996 static void 18997 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18998 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18999 { 19000 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 19001 19002 ASSERT(un != NULL); 19003 ASSERT(mutex_owned(SD_MUTEX(un))); 19004 ASSERT(bp != NULL); 19005 ASSERT(xp != NULL); 19006 ASSERT(pktp != NULL); 19007 19008 /* Do not do a reset if selection did not complete */ 19009 /* Note: Should this not just check the bit? */ 19010 if (pktp->pkt_state != STATE_GOT_BUS) { 19011 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19012 sd_reset_target(un, pktp); 19013 } 19014 19015 /* 19016 * If the target was not successfully selected, then set 19017 * SD_RETRIES_FAILFAST to indicate that we lost communication 19018 * with the target, and further retries and/or commands are 19019 * likely to take a long time. 19020 */ 19021 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 19022 flag |= SD_RETRIES_FAILFAST; 19023 } 19024 19025 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19026 19027 sd_retry_command(un, bp, flag, 19028 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19029 } 19030 19031 19032 19033 /* 19034 * Function: sd_pkt_reason_cmd_tran_err 19035 * 19036 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 19037 * 19038 * Context: May be called from interrupt context 19039 */ 19040 19041 static void 19042 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 19043 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19044 { 19045 ASSERT(un != NULL); 19046 ASSERT(mutex_owned(SD_MUTEX(un))); 19047 ASSERT(bp != NULL); 19048 ASSERT(xp != NULL); 19049 ASSERT(pktp != NULL); 19050 19051 /* 19052 * Do not reset if we got a parity error, or if 19053 * selection did not complete. 19054 */ 19055 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19056 /* Note: Should this not just check the bit for pkt_state? */ 19057 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 19058 (pktp->pkt_state != STATE_GOT_BUS)) { 19059 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19060 sd_reset_target(un, pktp); 19061 } 19062 19063 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19064 19065 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19066 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19067 } 19068 19069 19070 19071 /* 19072 * Function: sd_pkt_reason_cmd_reset 19073 * 19074 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 19075 * 19076 * Context: May be called from interrupt context 19077 */ 19078 19079 static void 19080 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 19081 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19082 { 19083 ASSERT(un != NULL); 19084 ASSERT(mutex_owned(SD_MUTEX(un))); 19085 ASSERT(bp != NULL); 19086 ASSERT(xp != NULL); 19087 ASSERT(pktp != NULL); 19088 19089 /* The target may still be running the command, so try to reset. */ 19090 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19091 sd_reset_target(un, pktp); 19092 19093 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19094 19095 /* 19096 * If pkt_reason is CMD_RESET chances are that this pkt got 19097 * reset because another target on this bus caused it. The target 19098 * that caused it should get CMD_TIMEOUT with pkt_statistics 19099 * of STAT_TIMEOUT/STAT_DEV_RESET. 19100 */ 19101 19102 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19103 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19104 } 19105 19106 19107 19108 19109 /* 19110 * Function: sd_pkt_reason_cmd_aborted 19111 * 19112 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 19113 * 19114 * Context: May be called from interrupt context 19115 */ 19116 19117 static void 19118 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 19119 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19120 { 19121 ASSERT(un != NULL); 19122 ASSERT(mutex_owned(SD_MUTEX(un))); 19123 ASSERT(bp != NULL); 19124 ASSERT(xp != NULL); 19125 ASSERT(pktp != NULL); 19126 19127 /* The target may still be running the command, so try to reset. */ 19128 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19129 sd_reset_target(un, pktp); 19130 19131 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19132 19133 /* 19134 * If pkt_reason is CMD_ABORTED chances are that this pkt got 19135 * aborted because another target on this bus caused it. The target 19136 * that caused it should get CMD_TIMEOUT with pkt_statistics 19137 * of STAT_TIMEOUT/STAT_DEV_RESET. 19138 */ 19139 19140 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19141 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19142 } 19143 19144 19145 19146 /* 19147 * Function: sd_pkt_reason_cmd_timeout 19148 * 19149 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 19150 * 19151 * Context: May be called from interrupt context 19152 */ 19153 19154 static void 19155 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 19156 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19157 { 19158 ASSERT(un != NULL); 19159 ASSERT(mutex_owned(SD_MUTEX(un))); 19160 ASSERT(bp != NULL); 19161 ASSERT(xp != NULL); 19162 ASSERT(pktp != NULL); 19163 19164 19165 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19166 sd_reset_target(un, pktp); 19167 19168 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19169 19170 /* 19171 * A command timeout indicates that we could not establish 19172 * communication with the target, so set SD_RETRIES_FAILFAST 19173 * as further retries/commands are likely to take a long time. 19174 */ 19175 sd_retry_command(un, bp, 19176 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 19177 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19178 } 19179 19180 19181 19182 /* 19183 * Function: sd_pkt_reason_cmd_unx_bus_free 19184 * 19185 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 19186 * 19187 * Context: May be called from interrupt context 19188 */ 19189 19190 static void 19191 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 19192 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19193 { 19194 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 19195 19196 ASSERT(un != NULL); 19197 ASSERT(mutex_owned(SD_MUTEX(un))); 19198 ASSERT(bp != NULL); 19199 ASSERT(xp != NULL); 19200 ASSERT(pktp != NULL); 19201 19202 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19203 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19204 19205 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 19206 sd_print_retry_msg : NULL; 19207 19208 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19209 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19210 } 19211 19212 19213 /* 19214 * Function: sd_pkt_reason_cmd_tag_reject 19215 * 19216 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 19217 * 19218 * Context: May be called from interrupt context 19219 */ 19220 19221 static void 19222 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 19223 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19224 { 19225 ASSERT(un != NULL); 19226 ASSERT(mutex_owned(SD_MUTEX(un))); 19227 ASSERT(bp != NULL); 19228 ASSERT(xp != NULL); 19229 ASSERT(pktp != NULL); 19230 19231 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19232 pktp->pkt_flags = 0; 19233 un->un_tagflags = 0; 19234 if (un->un_f_opt_queueing == TRUE) { 19235 un->un_throttle = min(un->un_throttle, 3); 19236 } else { 19237 un->un_throttle = 1; 19238 } 19239 mutex_exit(SD_MUTEX(un)); 19240 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 19241 mutex_enter(SD_MUTEX(un)); 19242 19243 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19244 19245 /* Legacy behavior not to check retry counts here. */ 19246 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 19247 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19248 } 19249 19250 19251 /* 19252 * Function: sd_pkt_reason_default 19253 * 19254 * Description: Default recovery actions for SCSA pkt_reason values that 19255 * do not have more explicit recovery actions. 19256 * 19257 * Context: May be called from interrupt context 19258 */ 19259 19260 static void 19261 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 19262 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19263 { 19264 ASSERT(un != NULL); 19265 ASSERT(mutex_owned(SD_MUTEX(un))); 19266 ASSERT(bp != NULL); 19267 ASSERT(xp != NULL); 19268 ASSERT(pktp != NULL); 19269 19270 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19271 sd_reset_target(un, pktp); 19272 19273 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19274 19275 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19276 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19277 } 19278 19279 19280 19281 /* 19282 * Function: sd_pkt_status_check_condition 19283 * 19284 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 19285 * 19286 * Context: May be called from interrupt context 19287 */ 19288 19289 static void 19290 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 19291 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19292 { 19293 ASSERT(un != NULL); 19294 ASSERT(mutex_owned(SD_MUTEX(un))); 19295 ASSERT(bp != NULL); 19296 ASSERT(xp != NULL); 19297 ASSERT(pktp != NULL); 19298 19299 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 19300 "entry: buf:0x%p xp:0x%p\n", bp, xp); 19301 19302 /* 19303 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 19304 * command will be retried after the request sense). Otherwise, retry 19305 * the command. Note: we are issuing the request sense even though the 19306 * retry limit may have been reached for the failed command. 19307 */ 19308 if (un->un_f_arq_enabled == FALSE) { 19309 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19310 "no ARQ, sending request sense command\n"); 19311 sd_send_request_sense_command(un, bp, pktp); 19312 } else { 19313 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19314 "ARQ,retrying request sense command\n"); 19315 #if defined(__i386) || defined(__amd64) 19316 /* 19317 * The SD_RETRY_DELAY value need to be adjusted here 19318 * when SD_RETRY_DELAY change in sddef.h 19319 */ 19320 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19321 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 19322 NULL); 19323 #else 19324 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 19325 EIO, SD_RETRY_DELAY, NULL); 19326 #endif 19327 } 19328 19329 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 19330 } 19331 19332 19333 /* 19334 * Function: sd_pkt_status_busy 19335 * 19336 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 19337 * 19338 * Context: May be called from interrupt context 19339 */ 19340 19341 static void 19342 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19343 struct scsi_pkt *pktp) 19344 { 19345 ASSERT(un != NULL); 19346 ASSERT(mutex_owned(SD_MUTEX(un))); 19347 ASSERT(bp != NULL); 19348 ASSERT(xp != NULL); 19349 ASSERT(pktp != NULL); 19350 19351 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19352 "sd_pkt_status_busy: entry\n"); 19353 19354 /* If retries are exhausted, just fail the command. */ 19355 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19356 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19357 "device busy too long\n"); 19358 sd_return_failed_command(un, bp, EIO); 19359 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19360 "sd_pkt_status_busy: exit\n"); 19361 return; 19362 } 19363 xp->xb_retry_count++; 19364 19365 /* 19366 * Try to reset the target. However, we do not want to perform 19367 * more than one reset if the device continues to fail. The reset 19368 * will be performed when the retry count reaches the reset 19369 * threshold. This threshold should be set such that at least 19370 * one retry is issued before the reset is performed. 19371 */ 19372 if (xp->xb_retry_count == 19373 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19374 int rval = 0; 19375 mutex_exit(SD_MUTEX(un)); 19376 if (un->un_f_allow_bus_device_reset == TRUE) { 19377 /* 19378 * First try to reset the LUN; if we cannot then 19379 * try to reset the target. 19380 */ 19381 if (un->un_f_lun_reset_enabled == TRUE) { 19382 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19383 "sd_pkt_status_busy: RESET_LUN\n"); 19384 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19385 } 19386 if (rval == 0) { 19387 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19388 "sd_pkt_status_busy: RESET_TARGET\n"); 19389 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19390 } 19391 } 19392 if (rval == 0) { 19393 /* 19394 * If the RESET_LUN and/or RESET_TARGET failed, 19395 * try RESET_ALL 19396 */ 19397 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19398 "sd_pkt_status_busy: RESET_ALL\n"); 19399 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19400 } 19401 mutex_enter(SD_MUTEX(un)); 19402 if (rval == 0) { 19403 /* 19404 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19405 * At this point we give up & fail the command. 19406 */ 19407 sd_return_failed_command(un, bp, EIO); 19408 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19409 "sd_pkt_status_busy: exit (failed cmd)\n"); 19410 return; 19411 } 19412 } 19413 19414 /* 19415 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19416 * we have already checked the retry counts above. 19417 */ 19418 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19419 EIO, un->un_busy_timeout, NULL); 19420 19421 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19422 "sd_pkt_status_busy: exit\n"); 19423 } 19424 19425 19426 /* 19427 * Function: sd_pkt_status_reservation_conflict 19428 * 19429 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19430 * command status. 19431 * 19432 * Context: May be called from interrupt context 19433 */ 19434 19435 static void 19436 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19437 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19438 { 19439 ASSERT(un != NULL); 19440 ASSERT(mutex_owned(SD_MUTEX(un))); 19441 ASSERT(bp != NULL); 19442 ASSERT(xp != NULL); 19443 ASSERT(pktp != NULL); 19444 19445 /* 19446 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19447 * conflict could be due to various reasons like incorrect keys, not 19448 * registered or not reserved etc. So, we return EACCES to the caller. 19449 */ 19450 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19451 int cmd = SD_GET_PKT_OPCODE(pktp); 19452 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19453 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19454 sd_return_failed_command(un, bp, EACCES); 19455 return; 19456 } 19457 } 19458 19459 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19460 19461 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19462 if (sd_failfast_enable != 0) { 19463 /* By definition, we must panic here.... */ 19464 sd_panic_for_res_conflict(un); 19465 /*NOTREACHED*/ 19466 } 19467 SD_ERROR(SD_LOG_IO, un, 19468 "sd_handle_resv_conflict: Disk Reserved\n"); 19469 sd_return_failed_command(un, bp, EACCES); 19470 return; 19471 } 19472 19473 /* 19474 * 1147670: retry only if sd_retry_on_reservation_conflict 19475 * property is set (default is 1). Retries will not succeed 19476 * on a disk reserved by another initiator. HA systems 19477 * may reset this via sd.conf to avoid these retries. 19478 * 19479 * Note: The legacy return code for this failure is EIO, however EACCES 19480 * seems more appropriate for a reservation conflict. 19481 */ 19482 if (sd_retry_on_reservation_conflict == 0) { 19483 SD_ERROR(SD_LOG_IO, un, 19484 "sd_handle_resv_conflict: Device Reserved\n"); 19485 sd_return_failed_command(un, bp, EIO); 19486 return; 19487 } 19488 19489 /* 19490 * Retry the command if we can. 19491 * 19492 * Note: The legacy return code for this failure is EIO, however EACCES 19493 * seems more appropriate for a reservation conflict. 19494 */ 19495 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19496 (clock_t)2, NULL); 19497 } 19498 19499 19500 19501 /* 19502 * Function: sd_pkt_status_qfull 19503 * 19504 * Description: Handle a QUEUE FULL condition from the target. This can 19505 * occur if the HBA does not handle the queue full condition. 19506 * (Basically this means third-party HBAs as Sun HBAs will 19507 * handle the queue full condition.) Note that if there are 19508 * some commands already in the transport, then the queue full 19509 * has occurred because the queue for this nexus is actually 19510 * full. If there are no commands in the transport, then the 19511 * queue full is resulting from some other initiator or lun 19512 * consuming all the resources at the target. 19513 * 19514 * Context: May be called from interrupt context 19515 */ 19516 19517 static void 19518 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19519 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19520 { 19521 ASSERT(un != NULL); 19522 ASSERT(mutex_owned(SD_MUTEX(un))); 19523 ASSERT(bp != NULL); 19524 ASSERT(xp != NULL); 19525 ASSERT(pktp != NULL); 19526 19527 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19528 "sd_pkt_status_qfull: entry\n"); 19529 19530 /* 19531 * Just lower the QFULL throttle and retry the command. Note that 19532 * we do not limit the number of retries here. 19533 */ 19534 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19535 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19536 SD_RESTART_TIMEOUT, NULL); 19537 19538 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19539 "sd_pkt_status_qfull: exit\n"); 19540 } 19541 19542 19543 /* 19544 * Function: sd_reset_target 19545 * 19546 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19547 * RESET_TARGET, or RESET_ALL. 19548 * 19549 * Context: May be called under interrupt context. 19550 */ 19551 19552 static void 19553 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19554 { 19555 int rval = 0; 19556 19557 ASSERT(un != NULL); 19558 ASSERT(mutex_owned(SD_MUTEX(un))); 19559 ASSERT(pktp != NULL); 19560 19561 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19562 19563 /* 19564 * No need to reset if the transport layer has already done so. 19565 */ 19566 if ((pktp->pkt_statistics & 19567 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19568 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19569 "sd_reset_target: no reset\n"); 19570 return; 19571 } 19572 19573 mutex_exit(SD_MUTEX(un)); 19574 19575 if (un->un_f_allow_bus_device_reset == TRUE) { 19576 if (un->un_f_lun_reset_enabled == TRUE) { 19577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19578 "sd_reset_target: RESET_LUN\n"); 19579 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19580 } 19581 if (rval == 0) { 19582 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19583 "sd_reset_target: RESET_TARGET\n"); 19584 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19585 } 19586 } 19587 19588 if (rval == 0) { 19589 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19590 "sd_reset_target: RESET_ALL\n"); 19591 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19592 } 19593 19594 mutex_enter(SD_MUTEX(un)); 19595 19596 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19597 } 19598 19599 /* 19600 * Function: sd_target_change_task 19601 * 19602 * Description: Handle dynamic target change 19603 * 19604 * Context: Executes in a taskq() thread context 19605 */ 19606 static void 19607 sd_target_change_task(void *arg) 19608 { 19609 struct sd_lun *un = arg; 19610 uint64_t capacity; 19611 diskaddr_t label_cap; 19612 uint_t lbasize; 19613 sd_ssc_t *ssc; 19614 19615 ASSERT(un != NULL); 19616 ASSERT(!mutex_owned(SD_MUTEX(un))); 19617 19618 if ((un->un_f_blockcount_is_valid == FALSE) || 19619 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 19620 return; 19621 } 19622 19623 ssc = sd_ssc_init(un); 19624 19625 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity, 19626 &lbasize, SD_PATH_DIRECT) != 0) { 19627 SD_ERROR(SD_LOG_ERROR, un, 19628 "sd_target_change_task: fail to read capacity\n"); 19629 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19630 goto task_exit; 19631 } 19632 19633 mutex_enter(SD_MUTEX(un)); 19634 if (capacity <= un->un_blockcount) { 19635 mutex_exit(SD_MUTEX(un)); 19636 goto task_exit; 19637 } 19638 19639 sd_update_block_info(un, lbasize, capacity); 19640 mutex_exit(SD_MUTEX(un)); 19641 19642 /* 19643 * If lun is EFI labeled and lun capacity is greater than the 19644 * capacity contained in the label, log a sys event. 19645 */ 19646 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 19647 (void*)SD_PATH_DIRECT) == 0) { 19648 mutex_enter(SD_MUTEX(un)); 19649 if (un->un_f_blockcount_is_valid && 19650 un->un_blockcount > label_cap) { 19651 mutex_exit(SD_MUTEX(un)); 19652 sd_log_lun_expansion_event(un, KM_SLEEP); 19653 } else { 19654 mutex_exit(SD_MUTEX(un)); 19655 } 19656 } 19657 19658 task_exit: 19659 sd_ssc_fini(ssc); 19660 } 19661 19662 19663 /* 19664 * Function: sd_log_dev_status_event 19665 * 19666 * Description: Log EC_dev_status sysevent 19667 * 19668 * Context: Never called from interrupt context 19669 */ 19670 static void 19671 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag) 19672 { 19673 int err; 19674 char *path; 19675 nvlist_t *attr_list; 19676 19677 /* Allocate and build sysevent attribute list */ 19678 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 19679 if (err != 0) { 19680 SD_ERROR(SD_LOG_ERROR, un, 19681 "sd_log_dev_status_event: fail to allocate space\n"); 19682 return; 19683 } 19684 19685 path = kmem_alloc(MAXPATHLEN, km_flag); 19686 if (path == NULL) { 19687 nvlist_free(attr_list); 19688 SD_ERROR(SD_LOG_ERROR, un, 19689 "sd_log_dev_status_event: fail to allocate space\n"); 19690 return; 19691 } 19692 /* 19693 * Add path attribute to identify the lun. 19694 * We are using minor node 'a' as the sysevent attribute. 19695 */ 19696 (void) snprintf(path, MAXPATHLEN, "/devices"); 19697 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 19698 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 19699 ":a"); 19700 19701 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path); 19702 if (err != 0) { 19703 nvlist_free(attr_list); 19704 kmem_free(path, MAXPATHLEN); 19705 SD_ERROR(SD_LOG_ERROR, un, 19706 "sd_log_dev_status_event: fail to add attribute\n"); 19707 return; 19708 } 19709 19710 /* Log dynamic lun expansion sysevent */ 19711 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 19712 esc, attr_list, NULL, km_flag); 19713 if (err != DDI_SUCCESS) { 19714 SD_ERROR(SD_LOG_ERROR, un, 19715 "sd_log_dev_status_event: fail to log sysevent\n"); 19716 } 19717 19718 nvlist_free(attr_list); 19719 kmem_free(path, MAXPATHLEN); 19720 } 19721 19722 19723 /* 19724 * Function: sd_log_lun_expansion_event 19725 * 19726 * Description: Log lun expansion sys event 19727 * 19728 * Context: Never called from interrupt context 19729 */ 19730 static void 19731 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 19732 { 19733 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag); 19734 } 19735 19736 19737 /* 19738 * Function: sd_log_eject_request_event 19739 * 19740 * Description: Log eject request sysevent 19741 * 19742 * Context: Never called from interrupt context 19743 */ 19744 static void 19745 sd_log_eject_request_event(struct sd_lun *un, int km_flag) 19746 { 19747 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag); 19748 } 19749 19750 19751 /* 19752 * Function: sd_media_change_task 19753 * 19754 * Description: Recovery action for CDROM to become available. 19755 * 19756 * Context: Executes in a taskq() thread context 19757 */ 19758 19759 static void 19760 sd_media_change_task(void *arg) 19761 { 19762 struct scsi_pkt *pktp = arg; 19763 struct sd_lun *un; 19764 struct buf *bp; 19765 struct sd_xbuf *xp; 19766 int err = 0; 19767 int retry_count = 0; 19768 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19769 struct sd_sense_info si; 19770 19771 ASSERT(pktp != NULL); 19772 bp = (struct buf *)pktp->pkt_private; 19773 ASSERT(bp != NULL); 19774 xp = SD_GET_XBUF(bp); 19775 ASSERT(xp != NULL); 19776 un = SD_GET_UN(bp); 19777 ASSERT(un != NULL); 19778 ASSERT(!mutex_owned(SD_MUTEX(un))); 19779 ASSERT(un->un_f_monitor_media_state); 19780 19781 si.ssi_severity = SCSI_ERR_INFO; 19782 si.ssi_pfa_flag = FALSE; 19783 19784 /* 19785 * When a reset is issued on a CDROM, it takes a long time to 19786 * recover. First few attempts to read capacity and other things 19787 * related to handling unit attention fail (with a ASC 0x4 and 19788 * ASCQ 0x1). In that case we want to do enough retries and we want 19789 * to limit the retries in other cases of genuine failures like 19790 * no media in drive. 19791 */ 19792 while (retry_count++ < retry_limit) { 19793 if ((err = sd_handle_mchange(un)) == 0) { 19794 break; 19795 } 19796 if (err == EAGAIN) { 19797 retry_limit = SD_UNIT_ATTENTION_RETRY; 19798 } 19799 /* Sleep for 0.5 sec. & try again */ 19800 delay(drv_usectohz(500000)); 19801 } 19802 19803 /* 19804 * Dispatch (retry or fail) the original command here, 19805 * along with appropriate console messages.... 19806 * 19807 * Must grab the mutex before calling sd_retry_command, 19808 * sd_print_sense_msg and sd_return_failed_command. 19809 */ 19810 mutex_enter(SD_MUTEX(un)); 19811 if (err != SD_CMD_SUCCESS) { 19812 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19813 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19814 si.ssi_severity = SCSI_ERR_FATAL; 19815 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19816 sd_return_failed_command(un, bp, EIO); 19817 } else { 19818 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg, 19819 &si, EIO, (clock_t)0, NULL); 19820 } 19821 mutex_exit(SD_MUTEX(un)); 19822 } 19823 19824 19825 19826 /* 19827 * Function: sd_handle_mchange 19828 * 19829 * Description: Perform geometry validation & other recovery when CDROM 19830 * has been removed from drive. 19831 * 19832 * Return Code: 0 for success 19833 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19834 * sd_send_scsi_READ_CAPACITY() 19835 * 19836 * Context: Executes in a taskq() thread context 19837 */ 19838 19839 static int 19840 sd_handle_mchange(struct sd_lun *un) 19841 { 19842 uint64_t capacity; 19843 uint32_t lbasize; 19844 int rval; 19845 sd_ssc_t *ssc; 19846 19847 ASSERT(!mutex_owned(SD_MUTEX(un))); 19848 ASSERT(un->un_f_monitor_media_state); 19849 19850 ssc = sd_ssc_init(un); 19851 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 19852 SD_PATH_DIRECT_PRIORITY); 19853 19854 if (rval != 0) 19855 goto failed; 19856 19857 mutex_enter(SD_MUTEX(un)); 19858 sd_update_block_info(un, lbasize, capacity); 19859 19860 if (un->un_errstats != NULL) { 19861 struct sd_errstats *stp = 19862 (struct sd_errstats *)un->un_errstats->ks_data; 19863 stp->sd_capacity.value.ui64 = (uint64_t) 19864 ((uint64_t)un->un_blockcount * 19865 (uint64_t)un->un_tgt_blocksize); 19866 } 19867 19868 /* 19869 * Check if the media in the device is writable or not 19870 */ 19871 if (ISCD(un)) { 19872 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY); 19873 } 19874 19875 /* 19876 * Note: Maybe let the strategy/partitioning chain worry about getting 19877 * valid geometry. 19878 */ 19879 mutex_exit(SD_MUTEX(un)); 19880 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 19881 19882 19883 if (cmlb_validate(un->un_cmlbhandle, 0, 19884 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 19885 sd_ssc_fini(ssc); 19886 return (EIO); 19887 } else { 19888 if (un->un_f_pkstats_enabled) { 19889 sd_set_pstats(un); 19890 SD_TRACE(SD_LOG_IO_PARTITION, un, 19891 "sd_handle_mchange: un:0x%p pstats created and " 19892 "set\n", un); 19893 } 19894 } 19895 19896 /* 19897 * Try to lock the door 19898 */ 19899 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 19900 SD_PATH_DIRECT_PRIORITY); 19901 failed: 19902 if (rval != 0) 19903 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19904 sd_ssc_fini(ssc); 19905 return (rval); 19906 } 19907 19908 19909 /* 19910 * Function: sd_send_scsi_DOORLOCK 19911 * 19912 * Description: Issue the scsi DOOR LOCK command 19913 * 19914 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19915 * structure for this target. 19916 * flag - SD_REMOVAL_ALLOW 19917 * SD_REMOVAL_PREVENT 19918 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19919 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19920 * to use the USCSI "direct" chain and bypass the normal 19921 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19922 * command is issued as part of an error recovery action. 19923 * 19924 * Return Code: 0 - Success 19925 * errno return code from sd_ssc_send() 19926 * 19927 * Context: Can sleep. 19928 */ 19929 19930 static int 19931 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag) 19932 { 19933 struct scsi_extended_sense sense_buf; 19934 union scsi_cdb cdb; 19935 struct uscsi_cmd ucmd_buf; 19936 int status; 19937 struct sd_lun *un; 19938 19939 ASSERT(ssc != NULL); 19940 un = ssc->ssc_un; 19941 ASSERT(un != NULL); 19942 ASSERT(!mutex_owned(SD_MUTEX(un))); 19943 19944 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19945 19946 /* already determined doorlock is not supported, fake success */ 19947 if (un->un_f_doorlock_supported == FALSE) { 19948 return (0); 19949 } 19950 19951 /* 19952 * If we are ejecting and see an SD_REMOVAL_PREVENT 19953 * ignore the command so we can complete the eject 19954 * operation. 19955 */ 19956 if (flag == SD_REMOVAL_PREVENT) { 19957 mutex_enter(SD_MUTEX(un)); 19958 if (un->un_f_ejecting == TRUE) { 19959 mutex_exit(SD_MUTEX(un)); 19960 return (EAGAIN); 19961 } 19962 mutex_exit(SD_MUTEX(un)); 19963 } 19964 19965 bzero(&cdb, sizeof (cdb)); 19966 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19967 19968 cdb.scc_cmd = SCMD_DOORLOCK; 19969 cdb.cdb_opaque[4] = (uchar_t)flag; 19970 19971 ucmd_buf.uscsi_cdb = (char *)&cdb; 19972 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19973 ucmd_buf.uscsi_bufaddr = NULL; 19974 ucmd_buf.uscsi_buflen = 0; 19975 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19976 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19977 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19978 ucmd_buf.uscsi_timeout = 15; 19979 19980 SD_TRACE(SD_LOG_IO, un, 19981 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n"); 19982 19983 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19984 UIO_SYSSPACE, path_flag); 19985 19986 if (status == 0) 19987 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19988 19989 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19990 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19991 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19992 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19993 19994 /* fake success and skip subsequent doorlock commands */ 19995 un->un_f_doorlock_supported = FALSE; 19996 return (0); 19997 } 19998 19999 return (status); 20000 } 20001 20002 /* 20003 * Function: sd_send_scsi_READ_CAPACITY 20004 * 20005 * Description: This routine uses the scsi READ CAPACITY command to determine 20006 * the device capacity in number of blocks and the device native 20007 * block size. If this function returns a failure, then the 20008 * values in *capp and *lbap are undefined. If the capacity 20009 * returned is 0xffffffff then the lun is too large for a 20010 * normal READ CAPACITY command and the results of a 20011 * READ CAPACITY 16 will be used instead. 20012 * 20013 * Arguments: ssc - ssc contains ptr to soft state struct for the target 20014 * capp - ptr to unsigned 64-bit variable to receive the 20015 * capacity value from the command. 20016 * lbap - ptr to unsigned 32-bit varaible to receive the 20017 * block size value from the command 20018 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20019 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20020 * to use the USCSI "direct" chain and bypass the normal 20021 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20022 * command is issued as part of an error recovery action. 20023 * 20024 * Return Code: 0 - Success 20025 * EIO - IO error 20026 * EACCES - Reservation conflict detected 20027 * EAGAIN - Device is becoming ready 20028 * errno return code from sd_ssc_send() 20029 * 20030 * Context: Can sleep. Blocks until command completes. 20031 */ 20032 20033 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 20034 20035 static int 20036 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap, 20037 int path_flag) 20038 { 20039 struct scsi_extended_sense sense_buf; 20040 struct uscsi_cmd ucmd_buf; 20041 union scsi_cdb cdb; 20042 uint32_t *capacity_buf; 20043 uint64_t capacity; 20044 uint32_t lbasize; 20045 uint32_t pbsize; 20046 int status; 20047 struct sd_lun *un; 20048 20049 ASSERT(ssc != NULL); 20050 20051 un = ssc->ssc_un; 20052 ASSERT(un != NULL); 20053 ASSERT(!mutex_owned(SD_MUTEX(un))); 20054 ASSERT(capp != NULL); 20055 ASSERT(lbap != NULL); 20056 20057 SD_TRACE(SD_LOG_IO, un, 20058 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20059 20060 /* 20061 * First send a READ_CAPACITY command to the target. 20062 * (This command is mandatory under SCSI-2.) 20063 * 20064 * Set up the CDB for the READ_CAPACITY command. The Partial 20065 * Medium Indicator bit is cleared. The address field must be 20066 * zero if the PMI bit is zero. 20067 */ 20068 bzero(&cdb, sizeof (cdb)); 20069 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20070 20071 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 20072 20073 cdb.scc_cmd = SCMD_READ_CAPACITY; 20074 20075 ucmd_buf.uscsi_cdb = (char *)&cdb; 20076 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20077 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 20078 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 20079 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20080 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20081 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20082 ucmd_buf.uscsi_timeout = 60; 20083 20084 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20085 UIO_SYSSPACE, path_flag); 20086 20087 switch (status) { 20088 case 0: 20089 /* Return failure if we did not get valid capacity data. */ 20090 if (ucmd_buf.uscsi_resid != 0) { 20091 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20092 "sd_send_scsi_READ_CAPACITY received invalid " 20093 "capacity data"); 20094 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20095 return (EIO); 20096 } 20097 /* 20098 * Read capacity and block size from the READ CAPACITY 10 data. 20099 * This data may be adjusted later due to device specific 20100 * issues. 20101 * 20102 * According to the SCSI spec, the READ CAPACITY 10 20103 * command returns the following: 20104 * 20105 * bytes 0-3: Maximum logical block address available. 20106 * (MSB in byte:0 & LSB in byte:3) 20107 * 20108 * bytes 4-7: Block length in bytes 20109 * (MSB in byte:4 & LSB in byte:7) 20110 * 20111 */ 20112 capacity = BE_32(capacity_buf[0]); 20113 lbasize = BE_32(capacity_buf[1]); 20114 20115 /* 20116 * Done with capacity_buf 20117 */ 20118 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20119 20120 /* 20121 * if the reported capacity is set to all 0xf's, then 20122 * this disk is too large and requires SBC-2 commands. 20123 * Reissue the request using READ CAPACITY 16. 20124 */ 20125 if (capacity == 0xffffffff) { 20126 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20127 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, 20128 &lbasize, &pbsize, path_flag); 20129 if (status != 0) { 20130 return (status); 20131 } else { 20132 goto rc16_done; 20133 } 20134 } 20135 break; /* Success! */ 20136 case EIO: 20137 switch (ucmd_buf.uscsi_status) { 20138 case STATUS_RESERVATION_CONFLICT: 20139 status = EACCES; 20140 break; 20141 case STATUS_CHECK: 20142 /* 20143 * Check condition; look for ASC/ASCQ of 0x04/0x01 20144 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20145 */ 20146 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20147 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20148 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20149 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20150 return (EAGAIN); 20151 } 20152 break; 20153 default: 20154 break; 20155 } 20156 /* FALLTHRU */ 20157 default: 20158 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20159 return (status); 20160 } 20161 20162 /* 20163 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20164 * (2352 and 0 are common) so for these devices always force the value 20165 * to 2048 as required by the ATAPI specs. 20166 */ 20167 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20168 lbasize = 2048; 20169 } 20170 20171 /* 20172 * Get the maximum LBA value from the READ CAPACITY data. 20173 * Here we assume that the Partial Medium Indicator (PMI) bit 20174 * was cleared when issuing the command. This means that the LBA 20175 * returned from the device is the LBA of the last logical block 20176 * on the logical unit. The actual logical block count will be 20177 * this value plus one. 20178 */ 20179 capacity += 1; 20180 20181 /* 20182 * Currently, for removable media, the capacity is saved in terms 20183 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20184 */ 20185 if (un->un_f_has_removable_media) 20186 capacity *= (lbasize / un->un_sys_blocksize); 20187 20188 rc16_done: 20189 20190 /* 20191 * Copy the values from the READ CAPACITY command into the space 20192 * provided by the caller. 20193 */ 20194 *capp = capacity; 20195 *lbap = lbasize; 20196 20197 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 20198 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 20199 20200 /* 20201 * Both the lbasize and capacity from the device must be nonzero, 20202 * otherwise we assume that the values are not valid and return 20203 * failure to the caller. (4203735) 20204 */ 20205 if ((capacity == 0) || (lbasize == 0)) { 20206 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20207 "sd_send_scsi_READ_CAPACITY received invalid value " 20208 "capacity %llu lbasize %d", capacity, lbasize); 20209 return (EIO); 20210 } 20211 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20212 return (0); 20213 } 20214 20215 /* 20216 * Function: sd_send_scsi_READ_CAPACITY_16 20217 * 20218 * Description: This routine uses the scsi READ CAPACITY 16 command to 20219 * determine the device capacity in number of blocks and the 20220 * device native block size. If this function returns a failure, 20221 * then the values in *capp and *lbap are undefined. 20222 * This routine should be called by sd_send_scsi_READ_CAPACITY 20223 * which will apply any device specific adjustments to capacity 20224 * and lbasize. One exception is it is also called by 20225 * sd_get_media_info_ext. In that function, there is no need to 20226 * adjust the capacity and lbasize. 20227 * 20228 * Arguments: ssc - ssc contains ptr to soft state struct for the target 20229 * capp - ptr to unsigned 64-bit variable to receive the 20230 * capacity value from the command. 20231 * lbap - ptr to unsigned 32-bit varaible to receive the 20232 * block size value from the command 20233 * psp - ptr to unsigned 32-bit variable to receive the 20234 * physical block size value from the command 20235 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20236 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20237 * to use the USCSI "direct" chain and bypass the normal 20238 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 20239 * this command is issued as part of an error recovery 20240 * action. 20241 * 20242 * Return Code: 0 - Success 20243 * EIO - IO error 20244 * EACCES - Reservation conflict detected 20245 * EAGAIN - Device is becoming ready 20246 * errno return code from sd_ssc_send() 20247 * 20248 * Context: Can sleep. Blocks until command completes. 20249 */ 20250 20251 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 20252 20253 static int 20254 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 20255 uint32_t *lbap, uint32_t *psp, int path_flag) 20256 { 20257 struct scsi_extended_sense sense_buf; 20258 struct uscsi_cmd ucmd_buf; 20259 union scsi_cdb cdb; 20260 uint64_t *capacity16_buf; 20261 uint64_t capacity; 20262 uint32_t lbasize; 20263 uint32_t pbsize; 20264 uint32_t lbpb_exp; 20265 int status; 20266 struct sd_lun *un; 20267 20268 ASSERT(ssc != NULL); 20269 20270 un = ssc->ssc_un; 20271 ASSERT(un != NULL); 20272 ASSERT(!mutex_owned(SD_MUTEX(un))); 20273 ASSERT(capp != NULL); 20274 ASSERT(lbap != NULL); 20275 20276 SD_TRACE(SD_LOG_IO, un, 20277 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20278 20279 /* 20280 * First send a READ_CAPACITY_16 command to the target. 20281 * 20282 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 20283 * Medium Indicator bit is cleared. The address field must be 20284 * zero if the PMI bit is zero. 20285 */ 20286 bzero(&cdb, sizeof (cdb)); 20287 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20288 20289 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 20290 20291 ucmd_buf.uscsi_cdb = (char *)&cdb; 20292 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 20293 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 20294 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 20295 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20296 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20297 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20298 ucmd_buf.uscsi_timeout = 60; 20299 20300 /* 20301 * Read Capacity (16) is a Service Action In command. One 20302 * command byte (0x9E) is overloaded for multiple operations, 20303 * with the second CDB byte specifying the desired operation 20304 */ 20305 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 20306 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 20307 20308 /* 20309 * Fill in allocation length field 20310 */ 20311 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 20312 20313 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20314 UIO_SYSSPACE, path_flag); 20315 20316 switch (status) { 20317 case 0: 20318 /* Return failure if we did not get valid capacity data. */ 20319 if (ucmd_buf.uscsi_resid > 20) { 20320 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20321 "sd_send_scsi_READ_CAPACITY_16 received invalid " 20322 "capacity data"); 20323 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20324 return (EIO); 20325 } 20326 20327 /* 20328 * Read capacity and block size from the READ CAPACITY 16 data. 20329 * This data may be adjusted later due to device specific 20330 * issues. 20331 * 20332 * According to the SCSI spec, the READ CAPACITY 16 20333 * command returns the following: 20334 * 20335 * bytes 0-7: Maximum logical block address available. 20336 * (MSB in byte:0 & LSB in byte:7) 20337 * 20338 * bytes 8-11: Block length in bytes 20339 * (MSB in byte:8 & LSB in byte:11) 20340 * 20341 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT 20342 */ 20343 capacity = BE_64(capacity16_buf[0]); 20344 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 20345 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f; 20346 20347 pbsize = lbasize << lbpb_exp; 20348 20349 /* 20350 * Done with capacity16_buf 20351 */ 20352 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20353 20354 /* 20355 * if the reported capacity is set to all 0xf's, then 20356 * this disk is too large. This could only happen with 20357 * a device that supports LBAs larger than 64 bits which 20358 * are not defined by any current T10 standards. 20359 */ 20360 if (capacity == 0xffffffffffffffff) { 20361 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20362 "disk is too large"); 20363 return (EIO); 20364 } 20365 break; /* Success! */ 20366 case EIO: 20367 switch (ucmd_buf.uscsi_status) { 20368 case STATUS_RESERVATION_CONFLICT: 20369 status = EACCES; 20370 break; 20371 case STATUS_CHECK: 20372 /* 20373 * Check condition; look for ASC/ASCQ of 0x04/0x01 20374 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20375 */ 20376 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20377 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20378 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20379 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20380 return (EAGAIN); 20381 } 20382 break; 20383 default: 20384 break; 20385 } 20386 /* FALLTHRU */ 20387 default: 20388 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20389 return (status); 20390 } 20391 20392 /* 20393 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20394 * (2352 and 0 are common) so for these devices always force the value 20395 * to 2048 as required by the ATAPI specs. 20396 */ 20397 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20398 lbasize = 2048; 20399 } 20400 20401 /* 20402 * Get the maximum LBA value from the READ CAPACITY 16 data. 20403 * Here we assume that the Partial Medium Indicator (PMI) bit 20404 * was cleared when issuing the command. This means that the LBA 20405 * returned from the device is the LBA of the last logical block 20406 * on the logical unit. The actual logical block count will be 20407 * this value plus one. 20408 */ 20409 capacity += 1; 20410 20411 /* 20412 * Currently, for removable media, the capacity is saved in terms 20413 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20414 */ 20415 if (un->un_f_has_removable_media) 20416 capacity *= (lbasize / un->un_sys_blocksize); 20417 20418 *capp = capacity; 20419 *lbap = lbasize; 20420 *psp = pbsize; 20421 20422 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 20423 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n", 20424 capacity, lbasize, pbsize); 20425 20426 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) { 20427 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20428 "sd_send_scsi_READ_CAPACITY_16 received invalid value " 20429 "capacity %llu lbasize %d pbsize %d", capacity, lbasize); 20430 return (EIO); 20431 } 20432 20433 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20434 return (0); 20435 } 20436 20437 20438 /* 20439 * Function: sd_send_scsi_START_STOP_UNIT 20440 * 20441 * Description: Issue a scsi START STOP UNIT command to the target. 20442 * 20443 * Arguments: ssc - ssc contatins pointer to driver soft state (unit) 20444 * structure for this target. 20445 * pc_flag - SD_POWER_CONDITION 20446 * SD_START_STOP 20447 * flag - SD_TARGET_START 20448 * SD_TARGET_STOP 20449 * SD_TARGET_EJECT 20450 * SD_TARGET_CLOSE 20451 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20452 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20453 * to use the USCSI "direct" chain and bypass the normal 20454 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20455 * command is issued as part of an error recovery action. 20456 * 20457 * Return Code: 0 - Success 20458 * EIO - IO error 20459 * EACCES - Reservation conflict detected 20460 * ENXIO - Not Ready, medium not present 20461 * errno return code from sd_ssc_send() 20462 * 20463 * Context: Can sleep. 20464 */ 20465 20466 static int 20467 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag, 20468 int path_flag) 20469 { 20470 struct scsi_extended_sense sense_buf; 20471 union scsi_cdb cdb; 20472 struct uscsi_cmd ucmd_buf; 20473 int status; 20474 struct sd_lun *un; 20475 20476 ASSERT(ssc != NULL); 20477 un = ssc->ssc_un; 20478 ASSERT(un != NULL); 20479 ASSERT(!mutex_owned(SD_MUTEX(un))); 20480 20481 SD_TRACE(SD_LOG_IO, un, 20482 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 20483 20484 if (un->un_f_check_start_stop && 20485 (pc_flag == SD_START_STOP) && 20486 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 20487 (un->un_f_start_stop_supported != TRUE)) { 20488 return (0); 20489 } 20490 20491 /* 20492 * If we are performing an eject operation and 20493 * we receive any command other than SD_TARGET_EJECT 20494 * we should immediately return. 20495 */ 20496 if (flag != SD_TARGET_EJECT) { 20497 mutex_enter(SD_MUTEX(un)); 20498 if (un->un_f_ejecting == TRUE) { 20499 mutex_exit(SD_MUTEX(un)); 20500 return (EAGAIN); 20501 } 20502 mutex_exit(SD_MUTEX(un)); 20503 } 20504 20505 bzero(&cdb, sizeof (cdb)); 20506 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20507 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20508 20509 cdb.scc_cmd = SCMD_START_STOP; 20510 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ? 20511 (uchar_t)(flag << 4) : (uchar_t)flag; 20512 20513 ucmd_buf.uscsi_cdb = (char *)&cdb; 20514 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20515 ucmd_buf.uscsi_bufaddr = NULL; 20516 ucmd_buf.uscsi_buflen = 0; 20517 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20518 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20519 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20520 ucmd_buf.uscsi_timeout = 200; 20521 20522 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20523 UIO_SYSSPACE, path_flag); 20524 20525 switch (status) { 20526 case 0: 20527 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20528 break; /* Success! */ 20529 case EIO: 20530 switch (ucmd_buf.uscsi_status) { 20531 case STATUS_RESERVATION_CONFLICT: 20532 status = EACCES; 20533 break; 20534 case STATUS_CHECK: 20535 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 20536 switch (scsi_sense_key( 20537 (uint8_t *)&sense_buf)) { 20538 case KEY_ILLEGAL_REQUEST: 20539 status = ENOTSUP; 20540 break; 20541 case KEY_NOT_READY: 20542 if (scsi_sense_asc( 20543 (uint8_t *)&sense_buf) 20544 == 0x3A) { 20545 status = ENXIO; 20546 } 20547 break; 20548 default: 20549 break; 20550 } 20551 } 20552 break; 20553 default: 20554 break; 20555 } 20556 break; 20557 default: 20558 break; 20559 } 20560 20561 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 20562 20563 return (status); 20564 } 20565 20566 20567 /* 20568 * Function: sd_start_stop_unit_callback 20569 * 20570 * Description: timeout(9F) callback to begin recovery process for a 20571 * device that has spun down. 20572 * 20573 * Arguments: arg - pointer to associated softstate struct. 20574 * 20575 * Context: Executes in a timeout(9F) thread context 20576 */ 20577 20578 static void 20579 sd_start_stop_unit_callback(void *arg) 20580 { 20581 struct sd_lun *un = arg; 20582 ASSERT(un != NULL); 20583 ASSERT(!mutex_owned(SD_MUTEX(un))); 20584 20585 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 20586 20587 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 20588 } 20589 20590 20591 /* 20592 * Function: sd_start_stop_unit_task 20593 * 20594 * Description: Recovery procedure when a drive is spun down. 20595 * 20596 * Arguments: arg - pointer to associated softstate struct. 20597 * 20598 * Context: Executes in a taskq() thread context 20599 */ 20600 20601 static void 20602 sd_start_stop_unit_task(void *arg) 20603 { 20604 struct sd_lun *un = arg; 20605 sd_ssc_t *ssc; 20606 int power_level; 20607 int rval; 20608 20609 ASSERT(un != NULL); 20610 ASSERT(!mutex_owned(SD_MUTEX(un))); 20611 20612 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 20613 20614 /* 20615 * Some unformatted drives report not ready error, no need to 20616 * restart if format has been initiated. 20617 */ 20618 mutex_enter(SD_MUTEX(un)); 20619 if (un->un_f_format_in_progress == TRUE) { 20620 mutex_exit(SD_MUTEX(un)); 20621 return; 20622 } 20623 mutex_exit(SD_MUTEX(un)); 20624 20625 ssc = sd_ssc_init(un); 20626 /* 20627 * When a START STOP command is issued from here, it is part of a 20628 * failure recovery operation and must be issued before any other 20629 * commands, including any pending retries. Thus it must be sent 20630 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20631 * succeeds or not, we will start I/O after the attempt. 20632 * If power condition is supported and the current power level 20633 * is capable of performing I/O, we should set the power condition 20634 * to that level. Otherwise, set the power condition to ACTIVE. 20635 */ 20636 if (un->un_f_power_condition_supported) { 20637 mutex_enter(SD_MUTEX(un)); 20638 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level)); 20639 power_level = sd_pwr_pc.ran_perf[un->un_power_level] 20640 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE; 20641 mutex_exit(SD_MUTEX(un)); 20642 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 20643 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY); 20644 } else { 20645 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 20646 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY); 20647 } 20648 20649 if (rval != 0) 20650 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20651 sd_ssc_fini(ssc); 20652 /* 20653 * The above call blocks until the START_STOP_UNIT command completes. 20654 * Now that it has completed, we must re-try the original IO that 20655 * received the NOT READY condition in the first place. There are 20656 * three possible conditions here: 20657 * 20658 * (1) The original IO is on un_retry_bp. 20659 * (2) The original IO is on the regular wait queue, and un_retry_bp 20660 * is NULL. 20661 * (3) The original IO is on the regular wait queue, and un_retry_bp 20662 * points to some other, unrelated bp. 20663 * 20664 * For each case, we must call sd_start_cmds() with un_retry_bp 20665 * as the argument. If un_retry_bp is NULL, this will initiate 20666 * processing of the regular wait queue. If un_retry_bp is not NULL, 20667 * then this will process the bp on un_retry_bp. That may or may not 20668 * be the original IO, but that does not matter: the important thing 20669 * is to keep the IO processing going at this point. 20670 * 20671 * Note: This is a very specific error recovery sequence associated 20672 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20673 * serialize the I/O with completion of the spin-up. 20674 */ 20675 mutex_enter(SD_MUTEX(un)); 20676 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20677 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20678 un, un->un_retry_bp); 20679 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20680 sd_start_cmds(un, un->un_retry_bp); 20681 mutex_exit(SD_MUTEX(un)); 20682 20683 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20684 } 20685 20686 20687 /* 20688 * Function: sd_send_scsi_INQUIRY 20689 * 20690 * Description: Issue the scsi INQUIRY command. 20691 * 20692 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20693 * structure for this target. 20694 * bufaddr 20695 * buflen 20696 * evpd 20697 * page_code 20698 * page_length 20699 * 20700 * Return Code: 0 - Success 20701 * errno return code from sd_ssc_send() 20702 * 20703 * Context: Can sleep. Does not return until command is completed. 20704 */ 20705 20706 static int 20707 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen, 20708 uchar_t evpd, uchar_t page_code, size_t *residp) 20709 { 20710 union scsi_cdb cdb; 20711 struct uscsi_cmd ucmd_buf; 20712 int status; 20713 struct sd_lun *un; 20714 20715 ASSERT(ssc != NULL); 20716 un = ssc->ssc_un; 20717 ASSERT(un != NULL); 20718 ASSERT(!mutex_owned(SD_MUTEX(un))); 20719 ASSERT(bufaddr != NULL); 20720 20721 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20722 20723 bzero(&cdb, sizeof (cdb)); 20724 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20725 bzero(bufaddr, buflen); 20726 20727 cdb.scc_cmd = SCMD_INQUIRY; 20728 cdb.cdb_opaque[1] = evpd; 20729 cdb.cdb_opaque[2] = page_code; 20730 FORMG0COUNT(&cdb, buflen); 20731 20732 ucmd_buf.uscsi_cdb = (char *)&cdb; 20733 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20734 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20735 ucmd_buf.uscsi_buflen = buflen; 20736 ucmd_buf.uscsi_rqbuf = NULL; 20737 ucmd_buf.uscsi_rqlen = 0; 20738 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20739 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20740 20741 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20742 UIO_SYSSPACE, SD_PATH_DIRECT); 20743 20744 /* 20745 * Only handle status == 0, the upper-level caller 20746 * will put different assessment based on the context. 20747 */ 20748 if (status == 0) 20749 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20750 20751 if ((status == 0) && (residp != NULL)) { 20752 *residp = ucmd_buf.uscsi_resid; 20753 } 20754 20755 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20756 20757 return (status); 20758 } 20759 20760 20761 /* 20762 * Function: sd_send_scsi_TEST_UNIT_READY 20763 * 20764 * Description: Issue the scsi TEST UNIT READY command. 20765 * This routine can be told to set the flag USCSI_DIAGNOSE to 20766 * prevent retrying failed commands. Use this when the intent 20767 * is either to check for device readiness, to clear a Unit 20768 * Attention, or to clear any outstanding sense data. 20769 * However under specific conditions the expected behavior 20770 * is for retries to bring a device ready, so use the flag 20771 * with caution. 20772 * 20773 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20774 * structure for this target. 20775 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20776 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20777 * 0: dont check for media present, do retries on cmd. 20778 * 20779 * Return Code: 0 - Success 20780 * EIO - IO error 20781 * EACCES - Reservation conflict detected 20782 * ENXIO - Not Ready, medium not present 20783 * errno return code from sd_ssc_send() 20784 * 20785 * Context: Can sleep. Does not return until command is completed. 20786 */ 20787 20788 static int 20789 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag) 20790 { 20791 struct scsi_extended_sense sense_buf; 20792 union scsi_cdb cdb; 20793 struct uscsi_cmd ucmd_buf; 20794 int status; 20795 struct sd_lun *un; 20796 20797 ASSERT(ssc != NULL); 20798 un = ssc->ssc_un; 20799 ASSERT(un != NULL); 20800 ASSERT(!mutex_owned(SD_MUTEX(un))); 20801 20802 SD_TRACE(SD_LOG_IO, un, 20803 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20804 20805 /* 20806 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20807 * timeouts when they receive a TUR and the queue is not empty. Check 20808 * the configuration flag set during attach (indicating the drive has 20809 * this firmware bug) and un_ncmds_in_transport before issuing the 20810 * TUR. If there are 20811 * pending commands return success, this is a bit arbitrary but is ok 20812 * for non-removables (i.e. the eliteI disks) and non-clustering 20813 * configurations. 20814 */ 20815 if (un->un_f_cfg_tur_check == TRUE) { 20816 mutex_enter(SD_MUTEX(un)); 20817 if (un->un_ncmds_in_transport != 0) { 20818 mutex_exit(SD_MUTEX(un)); 20819 return (0); 20820 } 20821 mutex_exit(SD_MUTEX(un)); 20822 } 20823 20824 bzero(&cdb, sizeof (cdb)); 20825 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20826 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20827 20828 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20829 20830 ucmd_buf.uscsi_cdb = (char *)&cdb; 20831 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20832 ucmd_buf.uscsi_bufaddr = NULL; 20833 ucmd_buf.uscsi_buflen = 0; 20834 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20835 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20836 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20837 20838 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20839 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20840 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20841 } 20842 ucmd_buf.uscsi_timeout = 60; 20843 20844 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20845 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 20846 SD_PATH_STANDARD)); 20847 20848 switch (status) { 20849 case 0: 20850 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20851 break; /* Success! */ 20852 case EIO: 20853 switch (ucmd_buf.uscsi_status) { 20854 case STATUS_RESERVATION_CONFLICT: 20855 status = EACCES; 20856 break; 20857 case STATUS_CHECK: 20858 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20859 break; 20860 } 20861 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20862 (scsi_sense_key((uint8_t *)&sense_buf) == 20863 KEY_NOT_READY) && 20864 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20865 status = ENXIO; 20866 } 20867 break; 20868 default: 20869 break; 20870 } 20871 break; 20872 default: 20873 break; 20874 } 20875 20876 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20877 20878 return (status); 20879 } 20880 20881 /* 20882 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20883 * 20884 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20885 * 20886 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20887 * structure for this target. 20888 * 20889 * Return Code: 0 - Success 20890 * EACCES 20891 * ENOTSUP 20892 * errno return code from sd_ssc_send() 20893 * 20894 * Context: Can sleep. Does not return until command is completed. 20895 */ 20896 20897 static int 20898 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd, 20899 uint16_t data_len, uchar_t *data_bufp) 20900 { 20901 struct scsi_extended_sense sense_buf; 20902 union scsi_cdb cdb; 20903 struct uscsi_cmd ucmd_buf; 20904 int status; 20905 int no_caller_buf = FALSE; 20906 struct sd_lun *un; 20907 20908 ASSERT(ssc != NULL); 20909 un = ssc->ssc_un; 20910 ASSERT(un != NULL); 20911 ASSERT(!mutex_owned(SD_MUTEX(un))); 20912 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20913 20914 SD_TRACE(SD_LOG_IO, un, 20915 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20916 20917 bzero(&cdb, sizeof (cdb)); 20918 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20919 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20920 if (data_bufp == NULL) { 20921 /* Allocate a default buf if the caller did not give one */ 20922 ASSERT(data_len == 0); 20923 data_len = MHIOC_RESV_KEY_SIZE; 20924 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20925 no_caller_buf = TRUE; 20926 } 20927 20928 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20929 cdb.cdb_opaque[1] = usr_cmd; 20930 FORMG1COUNT(&cdb, data_len); 20931 20932 ucmd_buf.uscsi_cdb = (char *)&cdb; 20933 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20934 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20935 ucmd_buf.uscsi_buflen = data_len; 20936 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20937 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20938 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20939 ucmd_buf.uscsi_timeout = 60; 20940 20941 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20942 UIO_SYSSPACE, SD_PATH_STANDARD); 20943 20944 switch (status) { 20945 case 0: 20946 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20947 20948 break; /* Success! */ 20949 case EIO: 20950 switch (ucmd_buf.uscsi_status) { 20951 case STATUS_RESERVATION_CONFLICT: 20952 status = EACCES; 20953 break; 20954 case STATUS_CHECK: 20955 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20956 (scsi_sense_key((uint8_t *)&sense_buf) == 20957 KEY_ILLEGAL_REQUEST)) { 20958 status = ENOTSUP; 20959 } 20960 break; 20961 default: 20962 break; 20963 } 20964 break; 20965 default: 20966 break; 20967 } 20968 20969 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20970 20971 if (no_caller_buf == TRUE) { 20972 kmem_free(data_bufp, data_len); 20973 } 20974 20975 return (status); 20976 } 20977 20978 20979 /* 20980 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20981 * 20982 * Description: This routine is the driver entry point for handling CD-ROM 20983 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20984 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20985 * device. 20986 * 20987 * Arguments: ssc - ssc contains un - pointer to soft state struct 20988 * for the target. 20989 * usr_cmd SCSI-3 reservation facility command (one of 20990 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20991 * SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR) 20992 * usr_bufp - user provided pointer register, reserve descriptor or 20993 * preempt and abort structure (mhioc_register_t, 20994 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20995 * 20996 * Return Code: 0 - Success 20997 * EACCES 20998 * ENOTSUP 20999 * errno return code from sd_ssc_send() 21000 * 21001 * Context: Can sleep. Does not return until command is completed. 21002 */ 21003 21004 static int 21005 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd, 21006 uchar_t *usr_bufp) 21007 { 21008 struct scsi_extended_sense sense_buf; 21009 union scsi_cdb cdb; 21010 struct uscsi_cmd ucmd_buf; 21011 int status; 21012 uchar_t data_len = sizeof (sd_prout_t); 21013 sd_prout_t *prp; 21014 struct sd_lun *un; 21015 21016 ASSERT(ssc != NULL); 21017 un = ssc->ssc_un; 21018 ASSERT(un != NULL); 21019 ASSERT(!mutex_owned(SD_MUTEX(un))); 21020 ASSERT(data_len == 24); /* required by scsi spec */ 21021 21022 SD_TRACE(SD_LOG_IO, un, 21023 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 21024 21025 if (usr_bufp == NULL) { 21026 return (EINVAL); 21027 } 21028 21029 bzero(&cdb, sizeof (cdb)); 21030 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21031 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21032 prp = kmem_zalloc(data_len, KM_SLEEP); 21033 21034 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 21035 cdb.cdb_opaque[1] = usr_cmd; 21036 FORMG1COUNT(&cdb, data_len); 21037 21038 ucmd_buf.uscsi_cdb = (char *)&cdb; 21039 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21040 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 21041 ucmd_buf.uscsi_buflen = data_len; 21042 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21043 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21044 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21045 ucmd_buf.uscsi_timeout = 60; 21046 21047 switch (usr_cmd) { 21048 case SD_SCSI3_REGISTER: { 21049 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 21050 21051 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21052 bcopy(ptr->newkey.key, prp->service_key, 21053 MHIOC_RESV_KEY_SIZE); 21054 prp->aptpl = ptr->aptpl; 21055 break; 21056 } 21057 case SD_SCSI3_CLEAR: { 21058 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 21059 21060 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21061 break; 21062 } 21063 case SD_SCSI3_RESERVE: 21064 case SD_SCSI3_RELEASE: { 21065 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 21066 21067 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21068 prp->scope_address = BE_32(ptr->scope_specific_addr); 21069 cdb.cdb_opaque[2] = ptr->type; 21070 break; 21071 } 21072 case SD_SCSI3_PREEMPTANDABORT: { 21073 mhioc_preemptandabort_t *ptr = 21074 (mhioc_preemptandabort_t *)usr_bufp; 21075 21076 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21077 bcopy(ptr->victim_key.key, prp->service_key, 21078 MHIOC_RESV_KEY_SIZE); 21079 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 21080 cdb.cdb_opaque[2] = ptr->resvdesc.type; 21081 ucmd_buf.uscsi_flags |= USCSI_HEAD; 21082 break; 21083 } 21084 case SD_SCSI3_REGISTERANDIGNOREKEY: 21085 { 21086 mhioc_registerandignorekey_t *ptr; 21087 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 21088 bcopy(ptr->newkey.key, 21089 prp->service_key, MHIOC_RESV_KEY_SIZE); 21090 prp->aptpl = ptr->aptpl; 21091 break; 21092 } 21093 default: 21094 ASSERT(FALSE); 21095 break; 21096 } 21097 21098 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21099 UIO_SYSSPACE, SD_PATH_STANDARD); 21100 21101 switch (status) { 21102 case 0: 21103 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21104 break; /* Success! */ 21105 case EIO: 21106 switch (ucmd_buf.uscsi_status) { 21107 case STATUS_RESERVATION_CONFLICT: 21108 status = EACCES; 21109 break; 21110 case STATUS_CHECK: 21111 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21112 (scsi_sense_key((uint8_t *)&sense_buf) == 21113 KEY_ILLEGAL_REQUEST)) { 21114 status = ENOTSUP; 21115 } 21116 break; 21117 default: 21118 break; 21119 } 21120 break; 21121 default: 21122 break; 21123 } 21124 21125 kmem_free(prp, data_len); 21126 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 21127 return (status); 21128 } 21129 21130 21131 /* 21132 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 21133 * 21134 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 21135 * 21136 * Arguments: un - pointer to the target's soft state struct 21137 * dkc - pointer to the callback structure 21138 * 21139 * Return Code: 0 - success 21140 * errno-type error code 21141 * 21142 * Context: kernel thread context only. 21143 * 21144 * _______________________________________________________________ 21145 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 21146 * |FLUSH_VOLATILE| | operation | 21147 * |______________|______________|_________________________________| 21148 * | 0 | NULL | Synchronous flush on both | 21149 * | | | volatile and non-volatile cache | 21150 * |______________|______________|_________________________________| 21151 * | 1 | NULL | Synchronous flush on volatile | 21152 * | | | cache; disk drivers may suppress| 21153 * | | | flush if disk table indicates | 21154 * | | | non-volatile cache | 21155 * |______________|______________|_________________________________| 21156 * | 0 | !NULL | Asynchronous flush on both | 21157 * | | | volatile and non-volatile cache;| 21158 * |______________|______________|_________________________________| 21159 * | 1 | !NULL | Asynchronous flush on volatile | 21160 * | | | cache; disk drivers may suppress| 21161 * | | | flush if disk table indicates | 21162 * | | | non-volatile cache | 21163 * |______________|______________|_________________________________| 21164 * 21165 */ 21166 21167 static int 21168 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 21169 { 21170 struct sd_uscsi_info *uip; 21171 struct uscsi_cmd *uscmd; 21172 union scsi_cdb *cdb; 21173 struct buf *bp; 21174 int rval = 0; 21175 int is_async; 21176 21177 SD_TRACE(SD_LOG_IO, un, 21178 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 21179 21180 ASSERT(un != NULL); 21181 ASSERT(!mutex_owned(SD_MUTEX(un))); 21182 21183 if (dkc == NULL || dkc->dkc_callback == NULL) { 21184 is_async = FALSE; 21185 } else { 21186 is_async = TRUE; 21187 } 21188 21189 mutex_enter(SD_MUTEX(un)); 21190 /* check whether cache flush should be suppressed */ 21191 if (un->un_f_suppress_cache_flush == TRUE) { 21192 mutex_exit(SD_MUTEX(un)); 21193 /* 21194 * suppress the cache flush if the device is told to do 21195 * so by sd.conf or disk table 21196 */ 21197 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 21198 skip the cache flush since suppress_cache_flush is %d!\n", 21199 un->un_f_suppress_cache_flush); 21200 21201 if (is_async == TRUE) { 21202 /* invoke callback for asynchronous flush */ 21203 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 21204 } 21205 return (rval); 21206 } 21207 mutex_exit(SD_MUTEX(un)); 21208 21209 /* 21210 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 21211 * set properly 21212 */ 21213 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 21214 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 21215 21216 mutex_enter(SD_MUTEX(un)); 21217 if (dkc != NULL && un->un_f_sync_nv_supported && 21218 (dkc->dkc_flag & FLUSH_VOLATILE)) { 21219 /* 21220 * if the device supports SYNC_NV bit, turn on 21221 * the SYNC_NV bit to only flush volatile cache 21222 */ 21223 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 21224 } 21225 mutex_exit(SD_MUTEX(un)); 21226 21227 /* 21228 * First get some memory for the uscsi_cmd struct and cdb 21229 * and initialize for SYNCHRONIZE_CACHE cmd. 21230 */ 21231 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21232 uscmd->uscsi_cdblen = CDB_GROUP1; 21233 uscmd->uscsi_cdb = (caddr_t)cdb; 21234 uscmd->uscsi_bufaddr = NULL; 21235 uscmd->uscsi_buflen = 0; 21236 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 21237 uscmd->uscsi_rqlen = SENSE_LENGTH; 21238 uscmd->uscsi_rqresid = SENSE_LENGTH; 21239 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 21240 uscmd->uscsi_timeout = sd_io_time; 21241 21242 /* 21243 * Allocate an sd_uscsi_info struct and fill it with the info 21244 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 21245 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 21246 * since we allocate the buf here in this function, we do not 21247 * need to preserve the prior contents of b_private. 21248 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 21249 */ 21250 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 21251 uip->ui_flags = SD_PATH_DIRECT; 21252 uip->ui_cmdp = uscmd; 21253 21254 bp = getrbuf(KM_SLEEP); 21255 bp->b_private = uip; 21256 21257 /* 21258 * Setup buffer to carry uscsi request. 21259 */ 21260 bp->b_flags = B_BUSY; 21261 bp->b_bcount = 0; 21262 bp->b_blkno = 0; 21263 21264 if (is_async == TRUE) { 21265 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 21266 uip->ui_dkc = *dkc; 21267 } 21268 21269 bp->b_edev = SD_GET_DEV(un); 21270 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 21271 21272 /* 21273 * Unset un_f_sync_cache_required flag 21274 */ 21275 mutex_enter(SD_MUTEX(un)); 21276 un->un_f_sync_cache_required = FALSE; 21277 mutex_exit(SD_MUTEX(un)); 21278 21279 (void) sd_uscsi_strategy(bp); 21280 21281 /* 21282 * If synchronous request, wait for completion 21283 * If async just return and let b_iodone callback 21284 * cleanup. 21285 * NOTE: On return, u_ncmds_in_driver will be decremented, 21286 * but it was also incremented in sd_uscsi_strategy(), so 21287 * we should be ok. 21288 */ 21289 if (is_async == FALSE) { 21290 (void) biowait(bp); 21291 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 21292 } 21293 21294 return (rval); 21295 } 21296 21297 21298 static int 21299 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 21300 { 21301 struct sd_uscsi_info *uip; 21302 struct uscsi_cmd *uscmd; 21303 uint8_t *sense_buf; 21304 struct sd_lun *un; 21305 int status; 21306 union scsi_cdb *cdb; 21307 21308 uip = (struct sd_uscsi_info *)(bp->b_private); 21309 ASSERT(uip != NULL); 21310 21311 uscmd = uip->ui_cmdp; 21312 ASSERT(uscmd != NULL); 21313 21314 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 21315 ASSERT(sense_buf != NULL); 21316 21317 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 21318 ASSERT(un != NULL); 21319 21320 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 21321 21322 status = geterror(bp); 21323 switch (status) { 21324 case 0: 21325 break; /* Success! */ 21326 case EIO: 21327 switch (uscmd->uscsi_status) { 21328 case STATUS_RESERVATION_CONFLICT: 21329 /* Ignore reservation conflict */ 21330 status = 0; 21331 goto done; 21332 21333 case STATUS_CHECK: 21334 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 21335 (scsi_sense_key(sense_buf) == 21336 KEY_ILLEGAL_REQUEST)) { 21337 /* Ignore Illegal Request error */ 21338 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 21339 mutex_enter(SD_MUTEX(un)); 21340 un->un_f_sync_nv_supported = FALSE; 21341 mutex_exit(SD_MUTEX(un)); 21342 status = 0; 21343 SD_TRACE(SD_LOG_IO, un, 21344 "un_f_sync_nv_supported \ 21345 is set to false.\n"); 21346 goto done; 21347 } 21348 21349 mutex_enter(SD_MUTEX(un)); 21350 un->un_f_sync_cache_supported = FALSE; 21351 mutex_exit(SD_MUTEX(un)); 21352 SD_TRACE(SD_LOG_IO, un, 21353 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 21354 un_f_sync_cache_supported set to false \ 21355 with asc = %x, ascq = %x\n", 21356 scsi_sense_asc(sense_buf), 21357 scsi_sense_ascq(sense_buf)); 21358 status = ENOTSUP; 21359 goto done; 21360 } 21361 break; 21362 default: 21363 break; 21364 } 21365 /* FALLTHRU */ 21366 default: 21367 /* 21368 * Turn on the un_f_sync_cache_required flag 21369 * since the SYNC CACHE command failed 21370 */ 21371 mutex_enter(SD_MUTEX(un)); 21372 un->un_f_sync_cache_required = TRUE; 21373 mutex_exit(SD_MUTEX(un)); 21374 21375 /* 21376 * Don't log an error message if this device 21377 * has removable media. 21378 */ 21379 if (!un->un_f_has_removable_media) { 21380 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 21381 "SYNCHRONIZE CACHE command failed (%d)\n", status); 21382 } 21383 break; 21384 } 21385 21386 done: 21387 if (uip->ui_dkc.dkc_callback != NULL) { 21388 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 21389 } 21390 21391 ASSERT((bp->b_flags & B_REMAPPED) == 0); 21392 freerbuf(bp); 21393 kmem_free(uip, sizeof (struct sd_uscsi_info)); 21394 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 21395 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 21396 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 21397 21398 return (status); 21399 } 21400 21401 21402 /* 21403 * Function: sd_send_scsi_GET_CONFIGURATION 21404 * 21405 * Description: Issues the get configuration command to the device. 21406 * Called from sd_check_for_writable_cd & sd_get_media_info 21407 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 21408 * Arguments: ssc 21409 * ucmdbuf 21410 * rqbuf 21411 * rqbuflen 21412 * bufaddr 21413 * buflen 21414 * path_flag 21415 * 21416 * Return Code: 0 - Success 21417 * errno return code from sd_ssc_send() 21418 * 21419 * Context: Can sleep. Does not return until command is completed. 21420 * 21421 */ 21422 21423 static int 21424 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf, 21425 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 21426 int path_flag) 21427 { 21428 char cdb[CDB_GROUP1]; 21429 int status; 21430 struct sd_lun *un; 21431 21432 ASSERT(ssc != NULL); 21433 un = ssc->ssc_un; 21434 ASSERT(un != NULL); 21435 ASSERT(!mutex_owned(SD_MUTEX(un))); 21436 ASSERT(bufaddr != NULL); 21437 ASSERT(ucmdbuf != NULL); 21438 ASSERT(rqbuf != NULL); 21439 21440 SD_TRACE(SD_LOG_IO, un, 21441 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 21442 21443 bzero(cdb, sizeof (cdb)); 21444 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21445 bzero(rqbuf, rqbuflen); 21446 bzero(bufaddr, buflen); 21447 21448 /* 21449 * Set up cdb field for the get configuration command. 21450 */ 21451 cdb[0] = SCMD_GET_CONFIGURATION; 21452 cdb[1] = 0x02; /* Requested Type */ 21453 cdb[8] = SD_PROFILE_HEADER_LEN; 21454 ucmdbuf->uscsi_cdb = cdb; 21455 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21456 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21457 ucmdbuf->uscsi_buflen = buflen; 21458 ucmdbuf->uscsi_timeout = sd_io_time; 21459 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21460 ucmdbuf->uscsi_rqlen = rqbuflen; 21461 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21462 21463 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21464 UIO_SYSSPACE, path_flag); 21465 21466 switch (status) { 21467 case 0: 21468 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21469 break; /* Success! */ 21470 case EIO: 21471 switch (ucmdbuf->uscsi_status) { 21472 case STATUS_RESERVATION_CONFLICT: 21473 status = EACCES; 21474 break; 21475 default: 21476 break; 21477 } 21478 break; 21479 default: 21480 break; 21481 } 21482 21483 if (status == 0) { 21484 SD_DUMP_MEMORY(un, SD_LOG_IO, 21485 "sd_send_scsi_GET_CONFIGURATION: data", 21486 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21487 } 21488 21489 SD_TRACE(SD_LOG_IO, un, 21490 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 21491 21492 return (status); 21493 } 21494 21495 /* 21496 * Function: sd_send_scsi_feature_GET_CONFIGURATION 21497 * 21498 * Description: Issues the get configuration command to the device to 21499 * retrieve a specific feature. Called from 21500 * sd_check_for_writable_cd & sd_set_mmc_caps. 21501 * Arguments: ssc 21502 * ucmdbuf 21503 * rqbuf 21504 * rqbuflen 21505 * bufaddr 21506 * buflen 21507 * feature 21508 * 21509 * Return Code: 0 - Success 21510 * errno return code from sd_ssc_send() 21511 * 21512 * Context: Can sleep. Does not return until command is completed. 21513 * 21514 */ 21515 static int 21516 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 21517 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 21518 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 21519 { 21520 char cdb[CDB_GROUP1]; 21521 int status; 21522 struct sd_lun *un; 21523 21524 ASSERT(ssc != NULL); 21525 un = ssc->ssc_un; 21526 ASSERT(un != NULL); 21527 ASSERT(!mutex_owned(SD_MUTEX(un))); 21528 ASSERT(bufaddr != NULL); 21529 ASSERT(ucmdbuf != NULL); 21530 ASSERT(rqbuf != NULL); 21531 21532 SD_TRACE(SD_LOG_IO, un, 21533 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 21534 21535 bzero(cdb, sizeof (cdb)); 21536 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21537 bzero(rqbuf, rqbuflen); 21538 bzero(bufaddr, buflen); 21539 21540 /* 21541 * Set up cdb field for the get configuration command. 21542 */ 21543 cdb[0] = SCMD_GET_CONFIGURATION; 21544 cdb[1] = 0x02; /* Requested Type */ 21545 cdb[3] = feature; 21546 cdb[8] = buflen; 21547 ucmdbuf->uscsi_cdb = cdb; 21548 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21549 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21550 ucmdbuf->uscsi_buflen = buflen; 21551 ucmdbuf->uscsi_timeout = sd_io_time; 21552 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21553 ucmdbuf->uscsi_rqlen = rqbuflen; 21554 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21555 21556 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21557 UIO_SYSSPACE, path_flag); 21558 21559 switch (status) { 21560 case 0: 21561 21562 break; /* Success! */ 21563 case EIO: 21564 switch (ucmdbuf->uscsi_status) { 21565 case STATUS_RESERVATION_CONFLICT: 21566 status = EACCES; 21567 break; 21568 default: 21569 break; 21570 } 21571 break; 21572 default: 21573 break; 21574 } 21575 21576 if (status == 0) { 21577 SD_DUMP_MEMORY(un, SD_LOG_IO, 21578 "sd_send_scsi_feature_GET_CONFIGURATION: data", 21579 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21580 } 21581 21582 SD_TRACE(SD_LOG_IO, un, 21583 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 21584 21585 return (status); 21586 } 21587 21588 21589 /* 21590 * Function: sd_send_scsi_MODE_SENSE 21591 * 21592 * Description: Utility function for issuing a scsi MODE SENSE command. 21593 * Note: This routine uses a consistent implementation for Group0, 21594 * Group1, and Group2 commands across all platforms. ATAPI devices 21595 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21596 * 21597 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21598 * structure for this target. 21599 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21600 * CDB_GROUP[1|2] (10 byte). 21601 * bufaddr - buffer for page data retrieved from the target. 21602 * buflen - size of page to be retrieved. 21603 * page_code - page code of data to be retrieved from the target. 21604 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21605 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21606 * to use the USCSI "direct" chain and bypass the normal 21607 * command waitq. 21608 * 21609 * Return Code: 0 - Success 21610 * errno return code from sd_ssc_send() 21611 * 21612 * Context: Can sleep. Does not return until command is completed. 21613 */ 21614 21615 static int 21616 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21617 size_t buflen, uchar_t page_code, int path_flag) 21618 { 21619 struct scsi_extended_sense sense_buf; 21620 union scsi_cdb cdb; 21621 struct uscsi_cmd ucmd_buf; 21622 int status; 21623 int headlen; 21624 struct sd_lun *un; 21625 21626 ASSERT(ssc != NULL); 21627 un = ssc->ssc_un; 21628 ASSERT(un != NULL); 21629 ASSERT(!mutex_owned(SD_MUTEX(un))); 21630 ASSERT(bufaddr != NULL); 21631 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21632 (cdbsize == CDB_GROUP2)); 21633 21634 SD_TRACE(SD_LOG_IO, un, 21635 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 21636 21637 bzero(&cdb, sizeof (cdb)); 21638 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21639 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21640 bzero(bufaddr, buflen); 21641 21642 if (cdbsize == CDB_GROUP0) { 21643 cdb.scc_cmd = SCMD_MODE_SENSE; 21644 cdb.cdb_opaque[2] = page_code; 21645 FORMG0COUNT(&cdb, buflen); 21646 headlen = MODE_HEADER_LENGTH; 21647 } else { 21648 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 21649 cdb.cdb_opaque[2] = page_code; 21650 FORMG1COUNT(&cdb, buflen); 21651 headlen = MODE_HEADER_LENGTH_GRP2; 21652 } 21653 21654 ASSERT(headlen <= buflen); 21655 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21656 21657 ucmd_buf.uscsi_cdb = (char *)&cdb; 21658 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21659 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21660 ucmd_buf.uscsi_buflen = buflen; 21661 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21662 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21663 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21664 ucmd_buf.uscsi_timeout = 60; 21665 21666 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21667 UIO_SYSSPACE, path_flag); 21668 21669 switch (status) { 21670 case 0: 21671 /* 21672 * sr_check_wp() uses 0x3f page code and check the header of 21673 * mode page to determine if target device is write-protected. 21674 * But some USB devices return 0 bytes for 0x3f page code. For 21675 * this case, make sure that mode page header is returned at 21676 * least. 21677 */ 21678 if (buflen - ucmd_buf.uscsi_resid < headlen) { 21679 status = EIO; 21680 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 21681 "mode page header is not returned"); 21682 } 21683 break; /* Success! */ 21684 case EIO: 21685 switch (ucmd_buf.uscsi_status) { 21686 case STATUS_RESERVATION_CONFLICT: 21687 status = EACCES; 21688 break; 21689 default: 21690 break; 21691 } 21692 break; 21693 default: 21694 break; 21695 } 21696 21697 if (status == 0) { 21698 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 21699 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21700 } 21701 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 21702 21703 return (status); 21704 } 21705 21706 21707 /* 21708 * Function: sd_send_scsi_MODE_SELECT 21709 * 21710 * Description: Utility function for issuing a scsi MODE SELECT command. 21711 * Note: This routine uses a consistent implementation for Group0, 21712 * Group1, and Group2 commands across all platforms. ATAPI devices 21713 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21714 * 21715 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21716 * structure for this target. 21717 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21718 * CDB_GROUP[1|2] (10 byte). 21719 * bufaddr - buffer for page data retrieved from the target. 21720 * buflen - size of page to be retrieved. 21721 * save_page - boolean to determin if SP bit should be set. 21722 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21723 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21724 * to use the USCSI "direct" chain and bypass the normal 21725 * command waitq. 21726 * 21727 * Return Code: 0 - Success 21728 * errno return code from sd_ssc_send() 21729 * 21730 * Context: Can sleep. Does not return until command is completed. 21731 */ 21732 21733 static int 21734 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21735 size_t buflen, uchar_t save_page, int path_flag) 21736 { 21737 struct scsi_extended_sense sense_buf; 21738 union scsi_cdb cdb; 21739 struct uscsi_cmd ucmd_buf; 21740 int status; 21741 struct sd_lun *un; 21742 21743 ASSERT(ssc != NULL); 21744 un = ssc->ssc_un; 21745 ASSERT(un != NULL); 21746 ASSERT(!mutex_owned(SD_MUTEX(un))); 21747 ASSERT(bufaddr != NULL); 21748 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21749 (cdbsize == CDB_GROUP2)); 21750 21751 SD_TRACE(SD_LOG_IO, un, 21752 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 21753 21754 bzero(&cdb, sizeof (cdb)); 21755 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21756 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21757 21758 /* Set the PF bit for many third party drives */ 21759 cdb.cdb_opaque[1] = 0x10; 21760 21761 /* Set the savepage(SP) bit if given */ 21762 if (save_page == SD_SAVE_PAGE) { 21763 cdb.cdb_opaque[1] |= 0x01; 21764 } 21765 21766 if (cdbsize == CDB_GROUP0) { 21767 cdb.scc_cmd = SCMD_MODE_SELECT; 21768 FORMG0COUNT(&cdb, buflen); 21769 } else { 21770 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 21771 FORMG1COUNT(&cdb, buflen); 21772 } 21773 21774 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21775 21776 ucmd_buf.uscsi_cdb = (char *)&cdb; 21777 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21778 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21779 ucmd_buf.uscsi_buflen = buflen; 21780 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21781 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21782 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21783 ucmd_buf.uscsi_timeout = 60; 21784 21785 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21786 UIO_SYSSPACE, path_flag); 21787 21788 switch (status) { 21789 case 0: 21790 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21791 break; /* Success! */ 21792 case EIO: 21793 switch (ucmd_buf.uscsi_status) { 21794 case STATUS_RESERVATION_CONFLICT: 21795 status = EACCES; 21796 break; 21797 default: 21798 break; 21799 } 21800 break; 21801 default: 21802 break; 21803 } 21804 21805 if (status == 0) { 21806 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21807 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21808 } 21809 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21810 21811 return (status); 21812 } 21813 21814 21815 /* 21816 * Function: sd_send_scsi_RDWR 21817 * 21818 * Description: Issue a scsi READ or WRITE command with the given parameters. 21819 * 21820 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21821 * structure for this target. 21822 * cmd: SCMD_READ or SCMD_WRITE 21823 * bufaddr: Address of caller's buffer to receive the RDWR data 21824 * buflen: Length of caller's buffer receive the RDWR data. 21825 * start_block: Block number for the start of the RDWR operation. 21826 * (Assumes target-native block size.) 21827 * residp: Pointer to variable to receive the redisual of the 21828 * RDWR operation (may be NULL of no residual requested). 21829 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21830 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21831 * to use the USCSI "direct" chain and bypass the normal 21832 * command waitq. 21833 * 21834 * Return Code: 0 - Success 21835 * errno return code from sd_ssc_send() 21836 * 21837 * Context: Can sleep. Does not return until command is completed. 21838 */ 21839 21840 static int 21841 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 21842 size_t buflen, daddr_t start_block, int path_flag) 21843 { 21844 struct scsi_extended_sense sense_buf; 21845 union scsi_cdb cdb; 21846 struct uscsi_cmd ucmd_buf; 21847 uint32_t block_count; 21848 int status; 21849 int cdbsize; 21850 uchar_t flag; 21851 struct sd_lun *un; 21852 21853 ASSERT(ssc != NULL); 21854 un = ssc->ssc_un; 21855 ASSERT(un != NULL); 21856 ASSERT(!mutex_owned(SD_MUTEX(un))); 21857 ASSERT(bufaddr != NULL); 21858 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21859 21860 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21861 21862 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21863 return (EINVAL); 21864 } 21865 21866 mutex_enter(SD_MUTEX(un)); 21867 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21868 mutex_exit(SD_MUTEX(un)); 21869 21870 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21871 21872 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21873 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21874 bufaddr, buflen, start_block, block_count); 21875 21876 bzero(&cdb, sizeof (cdb)); 21877 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21878 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21879 21880 /* Compute CDB size to use */ 21881 if (start_block > 0xffffffff) 21882 cdbsize = CDB_GROUP4; 21883 else if ((start_block & 0xFFE00000) || 21884 (un->un_f_cfg_is_atapi == TRUE)) 21885 cdbsize = CDB_GROUP1; 21886 else 21887 cdbsize = CDB_GROUP0; 21888 21889 switch (cdbsize) { 21890 case CDB_GROUP0: /* 6-byte CDBs */ 21891 cdb.scc_cmd = cmd; 21892 FORMG0ADDR(&cdb, start_block); 21893 FORMG0COUNT(&cdb, block_count); 21894 break; 21895 case CDB_GROUP1: /* 10-byte CDBs */ 21896 cdb.scc_cmd = cmd | SCMD_GROUP1; 21897 FORMG1ADDR(&cdb, start_block); 21898 FORMG1COUNT(&cdb, block_count); 21899 break; 21900 case CDB_GROUP4: /* 16-byte CDBs */ 21901 cdb.scc_cmd = cmd | SCMD_GROUP4; 21902 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21903 FORMG4COUNT(&cdb, block_count); 21904 break; 21905 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21906 default: 21907 /* All others reserved */ 21908 return (EINVAL); 21909 } 21910 21911 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21912 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21913 21914 ucmd_buf.uscsi_cdb = (char *)&cdb; 21915 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21916 ucmd_buf.uscsi_bufaddr = bufaddr; 21917 ucmd_buf.uscsi_buflen = buflen; 21918 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21919 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21920 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21921 ucmd_buf.uscsi_timeout = 60; 21922 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21923 UIO_SYSSPACE, path_flag); 21924 21925 switch (status) { 21926 case 0: 21927 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21928 break; /* Success! */ 21929 case EIO: 21930 switch (ucmd_buf.uscsi_status) { 21931 case STATUS_RESERVATION_CONFLICT: 21932 status = EACCES; 21933 break; 21934 default: 21935 break; 21936 } 21937 break; 21938 default: 21939 break; 21940 } 21941 21942 if (status == 0) { 21943 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21944 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21945 } 21946 21947 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21948 21949 return (status); 21950 } 21951 21952 21953 /* 21954 * Function: sd_send_scsi_LOG_SENSE 21955 * 21956 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21957 * 21958 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21959 * structure for this target. 21960 * 21961 * Return Code: 0 - Success 21962 * errno return code from sd_ssc_send() 21963 * 21964 * Context: Can sleep. Does not return until command is completed. 21965 */ 21966 21967 static int 21968 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen, 21969 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21970 int path_flag) 21971 21972 { 21973 struct scsi_extended_sense sense_buf; 21974 union scsi_cdb cdb; 21975 struct uscsi_cmd ucmd_buf; 21976 int status; 21977 struct sd_lun *un; 21978 21979 ASSERT(ssc != NULL); 21980 un = ssc->ssc_un; 21981 ASSERT(un != NULL); 21982 ASSERT(!mutex_owned(SD_MUTEX(un))); 21983 21984 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21985 21986 bzero(&cdb, sizeof (cdb)); 21987 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21988 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21989 21990 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21991 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21992 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21993 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21994 FORMG1COUNT(&cdb, buflen); 21995 21996 ucmd_buf.uscsi_cdb = (char *)&cdb; 21997 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21998 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21999 ucmd_buf.uscsi_buflen = buflen; 22000 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 22001 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 22002 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 22003 ucmd_buf.uscsi_timeout = 60; 22004 22005 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 22006 UIO_SYSSPACE, path_flag); 22007 22008 switch (status) { 22009 case 0: 22010 break; 22011 case EIO: 22012 switch (ucmd_buf.uscsi_status) { 22013 case STATUS_RESERVATION_CONFLICT: 22014 status = EACCES; 22015 break; 22016 case STATUS_CHECK: 22017 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 22018 (scsi_sense_key((uint8_t *)&sense_buf) == 22019 KEY_ILLEGAL_REQUEST) && 22020 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 22021 /* 22022 * ASC 0x24: INVALID FIELD IN CDB 22023 */ 22024 switch (page_code) { 22025 case START_STOP_CYCLE_PAGE: 22026 /* 22027 * The start stop cycle counter is 22028 * implemented as page 0x31 in earlier 22029 * generation disks. In new generation 22030 * disks the start stop cycle counter is 22031 * implemented as page 0xE. To properly 22032 * handle this case if an attempt for 22033 * log page 0xE is made and fails we 22034 * will try again using page 0x31. 22035 * 22036 * Network storage BU committed to 22037 * maintain the page 0x31 for this 22038 * purpose and will not have any other 22039 * page implemented with page code 0x31 22040 * until all disks transition to the 22041 * standard page. 22042 */ 22043 mutex_enter(SD_MUTEX(un)); 22044 un->un_start_stop_cycle_page = 22045 START_STOP_CYCLE_VU_PAGE; 22046 cdb.cdb_opaque[2] = 22047 (char)(page_control << 6) | 22048 un->un_start_stop_cycle_page; 22049 mutex_exit(SD_MUTEX(un)); 22050 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22051 status = sd_ssc_send( 22052 ssc, &ucmd_buf, FKIOCTL, 22053 UIO_SYSSPACE, path_flag); 22054 22055 break; 22056 case TEMPERATURE_PAGE: 22057 status = ENOTTY; 22058 break; 22059 default: 22060 break; 22061 } 22062 } 22063 break; 22064 default: 22065 break; 22066 } 22067 break; 22068 default: 22069 break; 22070 } 22071 22072 if (status == 0) { 22073 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22074 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 22075 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 22076 } 22077 22078 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 22079 22080 return (status); 22081 } 22082 22083 22084 /* 22085 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 22086 * 22087 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command. 22088 * 22089 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 22090 * structure for this target. 22091 * bufaddr 22092 * buflen 22093 * class_req 22094 * 22095 * Return Code: 0 - Success 22096 * errno return code from sd_ssc_send() 22097 * 22098 * Context: Can sleep. Does not return until command is completed. 22099 */ 22100 22101 static int 22102 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr, 22103 size_t buflen, uchar_t class_req) 22104 { 22105 union scsi_cdb cdb; 22106 struct uscsi_cmd ucmd_buf; 22107 int status; 22108 struct sd_lun *un; 22109 22110 ASSERT(ssc != NULL); 22111 un = ssc->ssc_un; 22112 ASSERT(un != NULL); 22113 ASSERT(!mutex_owned(SD_MUTEX(un))); 22114 ASSERT(bufaddr != NULL); 22115 22116 SD_TRACE(SD_LOG_IO, un, 22117 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un); 22118 22119 bzero(&cdb, sizeof (cdb)); 22120 bzero(&ucmd_buf, sizeof (ucmd_buf)); 22121 bzero(bufaddr, buflen); 22122 22123 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION; 22124 cdb.cdb_opaque[1] = 1; /* polled */ 22125 cdb.cdb_opaque[4] = class_req; 22126 FORMG1COUNT(&cdb, buflen); 22127 22128 ucmd_buf.uscsi_cdb = (char *)&cdb; 22129 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 22130 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 22131 ucmd_buf.uscsi_buflen = buflen; 22132 ucmd_buf.uscsi_rqbuf = NULL; 22133 ucmd_buf.uscsi_rqlen = 0; 22134 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 22135 ucmd_buf.uscsi_timeout = 60; 22136 22137 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 22138 UIO_SYSSPACE, SD_PATH_DIRECT); 22139 22140 /* 22141 * Only handle status == 0, the upper-level caller 22142 * will put different assessment based on the context. 22143 */ 22144 if (status == 0) { 22145 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22146 22147 if (ucmd_buf.uscsi_resid != 0) { 22148 status = EIO; 22149 } 22150 } 22151 22152 SD_TRACE(SD_LOG_IO, un, 22153 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n"); 22154 22155 return (status); 22156 } 22157 22158 22159 static boolean_t 22160 sd_gesn_media_data_valid(uchar_t *data) 22161 { 22162 uint16_t len; 22163 22164 len = (data[1] << 8) | data[0]; 22165 return ((len >= 6) && 22166 ((data[2] & SD_GESN_HEADER_NEA) == 0) && 22167 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) && 22168 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0)); 22169 } 22170 22171 22172 /* 22173 * Function: sdioctl 22174 * 22175 * Description: Driver's ioctl(9e) entry point function. 22176 * 22177 * Arguments: dev - device number 22178 * cmd - ioctl operation to be performed 22179 * arg - user argument, contains data to be set or reference 22180 * parameter for get 22181 * flag - bit flag, indicating open settings, 32/64 bit type 22182 * cred_p - user credential pointer 22183 * rval_p - calling process return value (OPT) 22184 * 22185 * Return Code: EINVAL 22186 * ENOTTY 22187 * ENXIO 22188 * EIO 22189 * EFAULT 22190 * ENOTSUP 22191 * EPERM 22192 * 22193 * Context: Called from the device switch at normal priority. 22194 */ 22195 22196 static int 22197 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 22198 { 22199 struct sd_lun *un = NULL; 22200 int err = 0; 22201 int i = 0; 22202 cred_t *cr; 22203 int tmprval = EINVAL; 22204 boolean_t is_valid; 22205 sd_ssc_t *ssc; 22206 22207 /* 22208 * All device accesses go thru sdstrategy where we check on suspend 22209 * status 22210 */ 22211 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22212 return (ENXIO); 22213 } 22214 22215 ASSERT(!mutex_owned(SD_MUTEX(un))); 22216 22217 /* Initialize sd_ssc_t for internal uscsi commands */ 22218 ssc = sd_ssc_init(un); 22219 22220 is_valid = SD_IS_VALID_LABEL(un); 22221 22222 /* 22223 * Moved this wait from sd_uscsi_strategy to here for 22224 * reasons of deadlock prevention. Internal driver commands, 22225 * specifically those to change a devices power level, result 22226 * in a call to sd_uscsi_strategy. 22227 */ 22228 mutex_enter(SD_MUTEX(un)); 22229 while ((un->un_state == SD_STATE_SUSPENDED) || 22230 (un->un_state == SD_STATE_PM_CHANGING)) { 22231 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 22232 } 22233 /* 22234 * Twiddling the counter here protects commands from now 22235 * through to the top of sd_uscsi_strategy. Without the 22236 * counter inc. a power down, for example, could get in 22237 * after the above check for state is made and before 22238 * execution gets to the top of sd_uscsi_strategy. 22239 * That would cause problems. 22240 */ 22241 un->un_ncmds_in_driver++; 22242 22243 if (!is_valid && 22244 (flag & (FNDELAY | FNONBLOCK))) { 22245 switch (cmd) { 22246 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 22247 case DKIOCGVTOC: 22248 case DKIOCGEXTVTOC: 22249 case DKIOCGAPART: 22250 case DKIOCPARTINFO: 22251 case DKIOCEXTPARTINFO: 22252 case DKIOCSGEOM: 22253 case DKIOCSAPART: 22254 case DKIOCGETEFI: 22255 case DKIOCPARTITION: 22256 case DKIOCSVTOC: 22257 case DKIOCSEXTVTOC: 22258 case DKIOCSETEFI: 22259 case DKIOCGMBOOT: 22260 case DKIOCSMBOOT: 22261 case DKIOCG_PHYGEOM: 22262 case DKIOCG_VIRTGEOM: 22263 #if defined(__i386) || defined(__amd64) 22264 case DKIOCSETEXTPART: 22265 #endif 22266 /* let cmlb handle it */ 22267 goto skip_ready_valid; 22268 22269 case CDROMPAUSE: 22270 case CDROMRESUME: 22271 case CDROMPLAYMSF: 22272 case CDROMPLAYTRKIND: 22273 case CDROMREADTOCHDR: 22274 case CDROMREADTOCENTRY: 22275 case CDROMSTOP: 22276 case CDROMSTART: 22277 case CDROMVOLCTRL: 22278 case CDROMSUBCHNL: 22279 case CDROMREADMODE2: 22280 case CDROMREADMODE1: 22281 case CDROMREADOFFSET: 22282 case CDROMSBLKMODE: 22283 case CDROMGBLKMODE: 22284 case CDROMGDRVSPEED: 22285 case CDROMSDRVSPEED: 22286 case CDROMCDDA: 22287 case CDROMCDXA: 22288 case CDROMSUBCODE: 22289 if (!ISCD(un)) { 22290 un->un_ncmds_in_driver--; 22291 ASSERT(un->un_ncmds_in_driver >= 0); 22292 mutex_exit(SD_MUTEX(un)); 22293 err = ENOTTY; 22294 goto done_without_assess; 22295 } 22296 break; 22297 case FDEJECT: 22298 case DKIOCEJECT: 22299 case CDROMEJECT: 22300 if (!un->un_f_eject_media_supported) { 22301 un->un_ncmds_in_driver--; 22302 ASSERT(un->un_ncmds_in_driver >= 0); 22303 mutex_exit(SD_MUTEX(un)); 22304 err = ENOTTY; 22305 goto done_without_assess; 22306 } 22307 break; 22308 case DKIOCFLUSHWRITECACHE: 22309 mutex_exit(SD_MUTEX(un)); 22310 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22311 if (err != 0) { 22312 mutex_enter(SD_MUTEX(un)); 22313 un->un_ncmds_in_driver--; 22314 ASSERT(un->un_ncmds_in_driver >= 0); 22315 mutex_exit(SD_MUTEX(un)); 22316 err = EIO; 22317 goto done_quick_assess; 22318 } 22319 mutex_enter(SD_MUTEX(un)); 22320 /* FALLTHROUGH */ 22321 case DKIOCREMOVABLE: 22322 case DKIOCHOTPLUGGABLE: 22323 case DKIOCINFO: 22324 case DKIOCGMEDIAINFO: 22325 case DKIOCGMEDIAINFOEXT: 22326 case MHIOCENFAILFAST: 22327 case MHIOCSTATUS: 22328 case MHIOCTKOWN: 22329 case MHIOCRELEASE: 22330 case MHIOCGRP_INKEYS: 22331 case MHIOCGRP_INRESV: 22332 case MHIOCGRP_REGISTER: 22333 case MHIOCGRP_CLEAR: 22334 case MHIOCGRP_RESERVE: 22335 case MHIOCGRP_PREEMPTANDABORT: 22336 case MHIOCGRP_REGISTERANDIGNOREKEY: 22337 case CDROMCLOSETRAY: 22338 case USCSICMD: 22339 goto skip_ready_valid; 22340 default: 22341 break; 22342 } 22343 22344 mutex_exit(SD_MUTEX(un)); 22345 err = sd_ready_and_valid(ssc, SDPART(dev)); 22346 mutex_enter(SD_MUTEX(un)); 22347 22348 if (err != SD_READY_VALID) { 22349 switch (cmd) { 22350 case DKIOCSTATE: 22351 case CDROMGDRVSPEED: 22352 case CDROMSDRVSPEED: 22353 case FDEJECT: /* for eject command */ 22354 case DKIOCEJECT: 22355 case CDROMEJECT: 22356 case DKIOCREMOVABLE: 22357 case DKIOCHOTPLUGGABLE: 22358 break; 22359 default: 22360 if (un->un_f_has_removable_media) { 22361 err = ENXIO; 22362 } else { 22363 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 22364 if (err == SD_RESERVED_BY_OTHERS) { 22365 err = EACCES; 22366 } else { 22367 err = EIO; 22368 } 22369 } 22370 un->un_ncmds_in_driver--; 22371 ASSERT(un->un_ncmds_in_driver >= 0); 22372 mutex_exit(SD_MUTEX(un)); 22373 22374 goto done_without_assess; 22375 } 22376 } 22377 } 22378 22379 skip_ready_valid: 22380 mutex_exit(SD_MUTEX(un)); 22381 22382 switch (cmd) { 22383 case DKIOCINFO: 22384 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 22385 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 22386 break; 22387 22388 case DKIOCGMEDIAINFO: 22389 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 22390 err = sd_get_media_info(dev, (caddr_t)arg, flag); 22391 break; 22392 22393 case DKIOCGMEDIAINFOEXT: 22394 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n"); 22395 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag); 22396 break; 22397 22398 case DKIOCGGEOM: 22399 case DKIOCGVTOC: 22400 case DKIOCGEXTVTOC: 22401 case DKIOCGAPART: 22402 case DKIOCPARTINFO: 22403 case DKIOCEXTPARTINFO: 22404 case DKIOCSGEOM: 22405 case DKIOCSAPART: 22406 case DKIOCGETEFI: 22407 case DKIOCPARTITION: 22408 case DKIOCSVTOC: 22409 case DKIOCSEXTVTOC: 22410 case DKIOCSETEFI: 22411 case DKIOCGMBOOT: 22412 case DKIOCSMBOOT: 22413 case DKIOCG_PHYGEOM: 22414 case DKIOCG_VIRTGEOM: 22415 #if defined(__i386) || defined(__amd64) 22416 case DKIOCSETEXTPART: 22417 #endif 22418 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 22419 22420 /* TUR should spin up */ 22421 22422 if (un->un_f_has_removable_media) 22423 err = sd_send_scsi_TEST_UNIT_READY(ssc, 22424 SD_CHECK_FOR_MEDIA); 22425 22426 else 22427 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22428 22429 if (err != 0) 22430 goto done_with_assess; 22431 22432 err = cmlb_ioctl(un->un_cmlbhandle, dev, 22433 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 22434 22435 if ((err == 0) && 22436 ((cmd == DKIOCSETEFI) || 22437 (un->un_f_pkstats_enabled) && 22438 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC || 22439 cmd == DKIOCSEXTVTOC))) { 22440 22441 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 22442 (void *)SD_PATH_DIRECT); 22443 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 22444 sd_set_pstats(un); 22445 SD_TRACE(SD_LOG_IO_PARTITION, un, 22446 "sd_ioctl: un:0x%p pstats created and " 22447 "set\n", un); 22448 } 22449 } 22450 22451 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) || 22452 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 22453 22454 mutex_enter(SD_MUTEX(un)); 22455 if (un->un_f_devid_supported && 22456 (un->un_f_opt_fab_devid == TRUE)) { 22457 if (un->un_devid == NULL) { 22458 sd_register_devid(ssc, SD_DEVINFO(un), 22459 SD_TARGET_IS_UNRESERVED); 22460 } else { 22461 /* 22462 * The device id for this disk 22463 * has been fabricated. The 22464 * device id must be preserved 22465 * by writing it back out to 22466 * disk. 22467 */ 22468 if (sd_write_deviceid(ssc) != 0) { 22469 ddi_devid_free(un->un_devid); 22470 un->un_devid = NULL; 22471 } 22472 } 22473 } 22474 mutex_exit(SD_MUTEX(un)); 22475 } 22476 22477 break; 22478 22479 case DKIOCLOCK: 22480 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 22481 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 22482 SD_PATH_STANDARD); 22483 goto done_with_assess; 22484 22485 case DKIOCUNLOCK: 22486 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 22487 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 22488 SD_PATH_STANDARD); 22489 goto done_with_assess; 22490 22491 case DKIOCSTATE: { 22492 enum dkio_state state; 22493 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 22494 22495 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 22496 err = EFAULT; 22497 } else { 22498 err = sd_check_media(dev, state); 22499 if (err == 0) { 22500 if (ddi_copyout(&un->un_mediastate, (void *)arg, 22501 sizeof (int), flag) != 0) 22502 err = EFAULT; 22503 } 22504 } 22505 break; 22506 } 22507 22508 case DKIOCREMOVABLE: 22509 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 22510 i = un->un_f_has_removable_media ? 1 : 0; 22511 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22512 err = EFAULT; 22513 } else { 22514 err = 0; 22515 } 22516 break; 22517 22518 case DKIOCHOTPLUGGABLE: 22519 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 22520 i = un->un_f_is_hotpluggable ? 1 : 0; 22521 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22522 err = EFAULT; 22523 } else { 22524 err = 0; 22525 } 22526 break; 22527 22528 case DKIOCREADONLY: 22529 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n"); 22530 i = 0; 22531 if ((ISCD(un) && !un->un_f_mmc_writable_media) || 22532 (sr_check_wp(dev) != 0)) { 22533 i = 1; 22534 } 22535 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22536 err = EFAULT; 22537 } else { 22538 err = 0; 22539 } 22540 break; 22541 22542 case DKIOCGTEMPERATURE: 22543 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 22544 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 22545 break; 22546 22547 case MHIOCENFAILFAST: 22548 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 22549 if ((err = drv_priv(cred_p)) == 0) { 22550 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 22551 } 22552 break; 22553 22554 case MHIOCTKOWN: 22555 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 22556 if ((err = drv_priv(cred_p)) == 0) { 22557 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 22558 } 22559 break; 22560 22561 case MHIOCRELEASE: 22562 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 22563 if ((err = drv_priv(cred_p)) == 0) { 22564 err = sd_mhdioc_release(dev); 22565 } 22566 break; 22567 22568 case MHIOCSTATUS: 22569 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 22570 if ((err = drv_priv(cred_p)) == 0) { 22571 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) { 22572 case 0: 22573 err = 0; 22574 break; 22575 case EACCES: 22576 *rval_p = 1; 22577 err = 0; 22578 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22579 break; 22580 default: 22581 err = EIO; 22582 goto done_with_assess; 22583 } 22584 } 22585 break; 22586 22587 case MHIOCQRESERVE: 22588 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 22589 if ((err = drv_priv(cred_p)) == 0) { 22590 err = sd_reserve_release(dev, SD_RESERVE); 22591 } 22592 break; 22593 22594 case MHIOCREREGISTERDEVID: 22595 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 22596 if (drv_priv(cred_p) == EPERM) { 22597 err = EPERM; 22598 } else if (!un->un_f_devid_supported) { 22599 err = ENOTTY; 22600 } else { 22601 err = sd_mhdioc_register_devid(dev); 22602 } 22603 break; 22604 22605 case MHIOCGRP_INKEYS: 22606 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 22607 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22608 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22609 err = ENOTSUP; 22610 } else { 22611 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 22612 flag); 22613 } 22614 } 22615 break; 22616 22617 case MHIOCGRP_INRESV: 22618 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 22619 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22620 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22621 err = ENOTSUP; 22622 } else { 22623 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 22624 } 22625 } 22626 break; 22627 22628 case MHIOCGRP_REGISTER: 22629 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 22630 if ((err = drv_priv(cred_p)) != EPERM) { 22631 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22632 err = ENOTSUP; 22633 } else if (arg != NULL) { 22634 mhioc_register_t reg; 22635 if (ddi_copyin((void *)arg, ®, 22636 sizeof (mhioc_register_t), flag) != 0) { 22637 err = EFAULT; 22638 } else { 22639 err = 22640 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22641 ssc, SD_SCSI3_REGISTER, 22642 (uchar_t *)®); 22643 if (err != 0) 22644 goto done_with_assess; 22645 } 22646 } 22647 } 22648 break; 22649 22650 case MHIOCGRP_CLEAR: 22651 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n"); 22652 if ((err = drv_priv(cred_p)) != EPERM) { 22653 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22654 err = ENOTSUP; 22655 } else if (arg != NULL) { 22656 mhioc_register_t reg; 22657 if (ddi_copyin((void *)arg, ®, 22658 sizeof (mhioc_register_t), flag) != 0) { 22659 err = EFAULT; 22660 } else { 22661 err = 22662 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22663 ssc, SD_SCSI3_CLEAR, 22664 (uchar_t *)®); 22665 if (err != 0) 22666 goto done_with_assess; 22667 } 22668 } 22669 } 22670 break; 22671 22672 case MHIOCGRP_RESERVE: 22673 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 22674 if ((err = drv_priv(cred_p)) != EPERM) { 22675 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22676 err = ENOTSUP; 22677 } else if (arg != NULL) { 22678 mhioc_resv_desc_t resv_desc; 22679 if (ddi_copyin((void *)arg, &resv_desc, 22680 sizeof (mhioc_resv_desc_t), flag) != 0) { 22681 err = EFAULT; 22682 } else { 22683 err = 22684 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22685 ssc, SD_SCSI3_RESERVE, 22686 (uchar_t *)&resv_desc); 22687 if (err != 0) 22688 goto done_with_assess; 22689 } 22690 } 22691 } 22692 break; 22693 22694 case MHIOCGRP_PREEMPTANDABORT: 22695 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 22696 if ((err = drv_priv(cred_p)) != EPERM) { 22697 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22698 err = ENOTSUP; 22699 } else if (arg != NULL) { 22700 mhioc_preemptandabort_t preempt_abort; 22701 if (ddi_copyin((void *)arg, &preempt_abort, 22702 sizeof (mhioc_preemptandabort_t), 22703 flag) != 0) { 22704 err = EFAULT; 22705 } else { 22706 err = 22707 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22708 ssc, SD_SCSI3_PREEMPTANDABORT, 22709 (uchar_t *)&preempt_abort); 22710 if (err != 0) 22711 goto done_with_assess; 22712 } 22713 } 22714 } 22715 break; 22716 22717 case MHIOCGRP_REGISTERANDIGNOREKEY: 22718 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 22719 if ((err = drv_priv(cred_p)) != EPERM) { 22720 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22721 err = ENOTSUP; 22722 } else if (arg != NULL) { 22723 mhioc_registerandignorekey_t r_and_i; 22724 if (ddi_copyin((void *)arg, (void *)&r_and_i, 22725 sizeof (mhioc_registerandignorekey_t), 22726 flag) != 0) { 22727 err = EFAULT; 22728 } else { 22729 err = 22730 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22731 ssc, SD_SCSI3_REGISTERANDIGNOREKEY, 22732 (uchar_t *)&r_and_i); 22733 if (err != 0) 22734 goto done_with_assess; 22735 } 22736 } 22737 } 22738 break; 22739 22740 case USCSICMD: 22741 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 22742 cr = ddi_get_cred(); 22743 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 22744 err = EPERM; 22745 } else { 22746 enum uio_seg uioseg; 22747 22748 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 22749 UIO_USERSPACE; 22750 if (un->un_f_format_in_progress == TRUE) { 22751 err = EAGAIN; 22752 break; 22753 } 22754 22755 err = sd_ssc_send(ssc, 22756 (struct uscsi_cmd *)arg, 22757 flag, uioseg, SD_PATH_STANDARD); 22758 if (err != 0) 22759 goto done_with_assess; 22760 else 22761 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22762 } 22763 break; 22764 22765 case CDROMPAUSE: 22766 case CDROMRESUME: 22767 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 22768 if (!ISCD(un)) { 22769 err = ENOTTY; 22770 } else { 22771 err = sr_pause_resume(dev, cmd); 22772 } 22773 break; 22774 22775 case CDROMPLAYMSF: 22776 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 22777 if (!ISCD(un)) { 22778 err = ENOTTY; 22779 } else { 22780 err = sr_play_msf(dev, (caddr_t)arg, flag); 22781 } 22782 break; 22783 22784 case CDROMPLAYTRKIND: 22785 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 22786 #if defined(__i386) || defined(__amd64) 22787 /* 22788 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 22789 */ 22790 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22791 #else 22792 if (!ISCD(un)) { 22793 #endif 22794 err = ENOTTY; 22795 } else { 22796 err = sr_play_trkind(dev, (caddr_t)arg, flag); 22797 } 22798 break; 22799 22800 case CDROMREADTOCHDR: 22801 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 22802 if (!ISCD(un)) { 22803 err = ENOTTY; 22804 } else { 22805 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 22806 } 22807 break; 22808 22809 case CDROMREADTOCENTRY: 22810 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 22811 if (!ISCD(un)) { 22812 err = ENOTTY; 22813 } else { 22814 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 22815 } 22816 break; 22817 22818 case CDROMSTOP: 22819 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 22820 if (!ISCD(un)) { 22821 err = ENOTTY; 22822 } else { 22823 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22824 SD_TARGET_STOP, SD_PATH_STANDARD); 22825 goto done_with_assess; 22826 } 22827 break; 22828 22829 case CDROMSTART: 22830 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 22831 if (!ISCD(un)) { 22832 err = ENOTTY; 22833 } else { 22834 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22835 SD_TARGET_START, SD_PATH_STANDARD); 22836 goto done_with_assess; 22837 } 22838 break; 22839 22840 case CDROMCLOSETRAY: 22841 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 22842 if (!ISCD(un)) { 22843 err = ENOTTY; 22844 } else { 22845 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22846 SD_TARGET_CLOSE, SD_PATH_STANDARD); 22847 goto done_with_assess; 22848 } 22849 break; 22850 22851 case FDEJECT: /* for eject command */ 22852 case DKIOCEJECT: 22853 case CDROMEJECT: 22854 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 22855 if (!un->un_f_eject_media_supported) { 22856 err = ENOTTY; 22857 } else { 22858 err = sr_eject(dev); 22859 } 22860 break; 22861 22862 case CDROMVOLCTRL: 22863 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 22864 if (!ISCD(un)) { 22865 err = ENOTTY; 22866 } else { 22867 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 22868 } 22869 break; 22870 22871 case CDROMSUBCHNL: 22872 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 22873 if (!ISCD(un)) { 22874 err = ENOTTY; 22875 } else { 22876 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 22877 } 22878 break; 22879 22880 case CDROMREADMODE2: 22881 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 22882 if (!ISCD(un)) { 22883 err = ENOTTY; 22884 } else if (un->un_f_cfg_is_atapi == TRUE) { 22885 /* 22886 * If the drive supports READ CD, use that instead of 22887 * switching the LBA size via a MODE SELECT 22888 * Block Descriptor 22889 */ 22890 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 22891 } else { 22892 err = sr_read_mode2(dev, (caddr_t)arg, flag); 22893 } 22894 break; 22895 22896 case CDROMREADMODE1: 22897 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 22898 if (!ISCD(un)) { 22899 err = ENOTTY; 22900 } else { 22901 err = sr_read_mode1(dev, (caddr_t)arg, flag); 22902 } 22903 break; 22904 22905 case CDROMREADOFFSET: 22906 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 22907 if (!ISCD(un)) { 22908 err = ENOTTY; 22909 } else { 22910 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 22911 flag); 22912 } 22913 break; 22914 22915 case CDROMSBLKMODE: 22916 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 22917 /* 22918 * There is no means of changing block size in case of atapi 22919 * drives, thus return ENOTTY if drive type is atapi 22920 */ 22921 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22922 err = ENOTTY; 22923 } else if (un->un_f_mmc_cap == TRUE) { 22924 22925 /* 22926 * MMC Devices do not support changing the 22927 * logical block size 22928 * 22929 * Note: EINVAL is being returned instead of ENOTTY to 22930 * maintain consistancy with the original mmc 22931 * driver update. 22932 */ 22933 err = EINVAL; 22934 } else { 22935 mutex_enter(SD_MUTEX(un)); 22936 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 22937 (un->un_ncmds_in_transport > 0)) { 22938 mutex_exit(SD_MUTEX(un)); 22939 err = EINVAL; 22940 } else { 22941 mutex_exit(SD_MUTEX(un)); 22942 err = sr_change_blkmode(dev, cmd, arg, flag); 22943 } 22944 } 22945 break; 22946 22947 case CDROMGBLKMODE: 22948 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 22949 if (!ISCD(un)) { 22950 err = ENOTTY; 22951 } else if ((un->un_f_cfg_is_atapi != FALSE) && 22952 (un->un_f_blockcount_is_valid != FALSE)) { 22953 /* 22954 * Drive is an ATAPI drive so return target block 22955 * size for ATAPI drives since we cannot change the 22956 * blocksize on ATAPI drives. Used primarily to detect 22957 * if an ATAPI cdrom is present. 22958 */ 22959 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 22960 sizeof (int), flag) != 0) { 22961 err = EFAULT; 22962 } else { 22963 err = 0; 22964 } 22965 22966 } else { 22967 /* 22968 * Drive supports changing block sizes via a Mode 22969 * Select. 22970 */ 22971 err = sr_change_blkmode(dev, cmd, arg, flag); 22972 } 22973 break; 22974 22975 case CDROMGDRVSPEED: 22976 case CDROMSDRVSPEED: 22977 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22978 if (!ISCD(un)) { 22979 err = ENOTTY; 22980 } else if (un->un_f_mmc_cap == TRUE) { 22981 /* 22982 * Note: In the future the driver implementation 22983 * for getting and 22984 * setting cd speed should entail: 22985 * 1) If non-mmc try the Toshiba mode page 22986 * (sr_change_speed) 22987 * 2) If mmc but no support for Real Time Streaming try 22988 * the SET CD SPEED (0xBB) command 22989 * (sr_atapi_change_speed) 22990 * 3) If mmc and support for Real Time Streaming 22991 * try the GET PERFORMANCE and SET STREAMING 22992 * commands (not yet implemented, 4380808) 22993 */ 22994 /* 22995 * As per recent MMC spec, CD-ROM speed is variable 22996 * and changes with LBA. Since there is no such 22997 * things as drive speed now, fail this ioctl. 22998 * 22999 * Note: EINVAL is returned for consistancy of original 23000 * implementation which included support for getting 23001 * the drive speed of mmc devices but not setting 23002 * the drive speed. Thus EINVAL would be returned 23003 * if a set request was made for an mmc device. 23004 * We no longer support get or set speed for 23005 * mmc but need to remain consistent with regard 23006 * to the error code returned. 23007 */ 23008 err = EINVAL; 23009 } else if (un->un_f_cfg_is_atapi == TRUE) { 23010 err = sr_atapi_change_speed(dev, cmd, arg, flag); 23011 } else { 23012 err = sr_change_speed(dev, cmd, arg, flag); 23013 } 23014 break; 23015 23016 case CDROMCDDA: 23017 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 23018 if (!ISCD(un)) { 23019 err = ENOTTY; 23020 } else { 23021 err = sr_read_cdda(dev, (void *)arg, flag); 23022 } 23023 break; 23024 23025 case CDROMCDXA: 23026 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 23027 if (!ISCD(un)) { 23028 err = ENOTTY; 23029 } else { 23030 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 23031 } 23032 break; 23033 23034 case CDROMSUBCODE: 23035 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 23036 if (!ISCD(un)) { 23037 err = ENOTTY; 23038 } else { 23039 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 23040 } 23041 break; 23042 23043 23044 #ifdef SDDEBUG 23045 /* RESET/ABORTS testing ioctls */ 23046 case DKIOCRESET: { 23047 int reset_level; 23048 23049 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 23050 err = EFAULT; 23051 } else { 23052 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 23053 "reset_level = 0x%lx\n", reset_level); 23054 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 23055 err = 0; 23056 } else { 23057 err = EIO; 23058 } 23059 } 23060 break; 23061 } 23062 23063 case DKIOCABORT: 23064 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 23065 if (scsi_abort(SD_ADDRESS(un), NULL)) { 23066 err = 0; 23067 } else { 23068 err = EIO; 23069 } 23070 break; 23071 #endif 23072 23073 #ifdef SD_FAULT_INJECTION 23074 /* SDIOC FaultInjection testing ioctls */ 23075 case SDIOCSTART: 23076 case SDIOCSTOP: 23077 case SDIOCINSERTPKT: 23078 case SDIOCINSERTXB: 23079 case SDIOCINSERTUN: 23080 case SDIOCINSERTARQ: 23081 case SDIOCPUSH: 23082 case SDIOCRETRIEVE: 23083 case SDIOCRUN: 23084 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 23085 "SDIOC detected cmd:0x%X:\n", cmd); 23086 /* call error generator */ 23087 sd_faultinjection_ioctl(cmd, arg, un); 23088 err = 0; 23089 break; 23090 23091 #endif /* SD_FAULT_INJECTION */ 23092 23093 case DKIOCFLUSHWRITECACHE: 23094 { 23095 struct dk_callback *dkc = (struct dk_callback *)arg; 23096 23097 mutex_enter(SD_MUTEX(un)); 23098 if (!un->un_f_sync_cache_supported || 23099 !un->un_f_write_cache_enabled) { 23100 err = un->un_f_sync_cache_supported ? 23101 0 : ENOTSUP; 23102 mutex_exit(SD_MUTEX(un)); 23103 if ((flag & FKIOCTL) && dkc != NULL && 23104 dkc->dkc_callback != NULL) { 23105 (*dkc->dkc_callback)(dkc->dkc_cookie, 23106 err); 23107 /* 23108 * Did callback and reported error. 23109 * Since we did a callback, ioctl 23110 * should return 0. 23111 */ 23112 err = 0; 23113 } 23114 break; 23115 } 23116 mutex_exit(SD_MUTEX(un)); 23117 23118 if ((flag & FKIOCTL) && dkc != NULL && 23119 dkc->dkc_callback != NULL) { 23120 /* async SYNC CACHE request */ 23121 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 23122 } else { 23123 /* synchronous SYNC CACHE request */ 23124 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 23125 } 23126 } 23127 break; 23128 23129 case DKIOCGETWCE: { 23130 23131 int wce; 23132 23133 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) { 23134 break; 23135 } 23136 23137 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 23138 err = EFAULT; 23139 } 23140 break; 23141 } 23142 23143 case DKIOCSETWCE: { 23144 23145 int wce, sync_supported; 23146 int cur_wce = 0; 23147 23148 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 23149 err = EFAULT; 23150 break; 23151 } 23152 23153 /* 23154 * Synchronize multiple threads trying to enable 23155 * or disable the cache via the un_f_wcc_cv 23156 * condition variable. 23157 */ 23158 mutex_enter(SD_MUTEX(un)); 23159 23160 /* 23161 * Don't allow the cache to be enabled if the 23162 * config file has it disabled. 23163 */ 23164 if (un->un_f_opt_disable_cache && wce) { 23165 mutex_exit(SD_MUTEX(un)); 23166 err = EINVAL; 23167 break; 23168 } 23169 23170 /* 23171 * Wait for write cache change in progress 23172 * bit to be clear before proceeding. 23173 */ 23174 while (un->un_f_wcc_inprog) 23175 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 23176 23177 un->un_f_wcc_inprog = 1; 23178 23179 mutex_exit(SD_MUTEX(un)); 23180 23181 /* 23182 * Get the current write cache state 23183 */ 23184 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) { 23185 mutex_enter(SD_MUTEX(un)); 23186 un->un_f_wcc_inprog = 0; 23187 cv_broadcast(&un->un_wcc_cv); 23188 mutex_exit(SD_MUTEX(un)); 23189 break; 23190 } 23191 23192 mutex_enter(SD_MUTEX(un)); 23193 un->un_f_write_cache_enabled = (cur_wce != 0); 23194 23195 if (un->un_f_write_cache_enabled && wce == 0) { 23196 /* 23197 * Disable the write cache. Don't clear 23198 * un_f_write_cache_enabled until after 23199 * the mode select and flush are complete. 23200 */ 23201 sync_supported = un->un_f_sync_cache_supported; 23202 23203 /* 23204 * If cache flush is suppressed, we assume that the 23205 * controller firmware will take care of managing the 23206 * write cache for us: no need to explicitly 23207 * disable it. 23208 */ 23209 if (!un->un_f_suppress_cache_flush) { 23210 mutex_exit(SD_MUTEX(un)); 23211 if ((err = sd_cache_control(ssc, 23212 SD_CACHE_NOCHANGE, 23213 SD_CACHE_DISABLE)) == 0 && 23214 sync_supported) { 23215 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 23216 NULL); 23217 } 23218 } else { 23219 mutex_exit(SD_MUTEX(un)); 23220 } 23221 23222 mutex_enter(SD_MUTEX(un)); 23223 if (err == 0) { 23224 un->un_f_write_cache_enabled = 0; 23225 } 23226 23227 } else if (!un->un_f_write_cache_enabled && wce != 0) { 23228 /* 23229 * Set un_f_write_cache_enabled first, so there is 23230 * no window where the cache is enabled, but the 23231 * bit says it isn't. 23232 */ 23233 un->un_f_write_cache_enabled = 1; 23234 23235 /* 23236 * If cache flush is suppressed, we assume that the 23237 * controller firmware will take care of managing the 23238 * write cache for us: no need to explicitly 23239 * enable it. 23240 */ 23241 if (!un->un_f_suppress_cache_flush) { 23242 mutex_exit(SD_MUTEX(un)); 23243 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE, 23244 SD_CACHE_ENABLE); 23245 } else { 23246 mutex_exit(SD_MUTEX(un)); 23247 } 23248 23249 mutex_enter(SD_MUTEX(un)); 23250 23251 if (err) { 23252 un->un_f_write_cache_enabled = 0; 23253 } 23254 } 23255 23256 un->un_f_wcc_inprog = 0; 23257 cv_broadcast(&un->un_wcc_cv); 23258 mutex_exit(SD_MUTEX(un)); 23259 break; 23260 } 23261 23262 default: 23263 err = ENOTTY; 23264 break; 23265 } 23266 mutex_enter(SD_MUTEX(un)); 23267 un->un_ncmds_in_driver--; 23268 ASSERT(un->un_ncmds_in_driver >= 0); 23269 mutex_exit(SD_MUTEX(un)); 23270 23271 23272 done_without_assess: 23273 sd_ssc_fini(ssc); 23274 23275 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23276 return (err); 23277 23278 done_with_assess: 23279 mutex_enter(SD_MUTEX(un)); 23280 un->un_ncmds_in_driver--; 23281 ASSERT(un->un_ncmds_in_driver >= 0); 23282 mutex_exit(SD_MUTEX(un)); 23283 23284 done_quick_assess: 23285 if (err != 0) 23286 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23287 /* Uninitialize sd_ssc_t pointer */ 23288 sd_ssc_fini(ssc); 23289 23290 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23291 return (err); 23292 } 23293 23294 23295 /* 23296 * Function: sd_dkio_ctrl_info 23297 * 23298 * Description: This routine is the driver entry point for handling controller 23299 * information ioctl requests (DKIOCINFO). 23300 * 23301 * Arguments: dev - the device number 23302 * arg - pointer to user provided dk_cinfo structure 23303 * specifying the controller type and attributes. 23304 * flag - this argument is a pass through to ddi_copyxxx() 23305 * directly from the mode argument of ioctl(). 23306 * 23307 * Return Code: 0 23308 * EFAULT 23309 * ENXIO 23310 */ 23311 23312 static int 23313 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 23314 { 23315 struct sd_lun *un = NULL; 23316 struct dk_cinfo *info; 23317 dev_info_t *pdip; 23318 int lun, tgt; 23319 23320 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23321 return (ENXIO); 23322 } 23323 23324 info = (struct dk_cinfo *) 23325 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 23326 23327 switch (un->un_ctype) { 23328 case CTYPE_CDROM: 23329 info->dki_ctype = DKC_CDROM; 23330 break; 23331 default: 23332 info->dki_ctype = DKC_SCSI_CCS; 23333 break; 23334 } 23335 pdip = ddi_get_parent(SD_DEVINFO(un)); 23336 info->dki_cnum = ddi_get_instance(pdip); 23337 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 23338 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 23339 } else { 23340 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 23341 DK_DEVLEN - 1); 23342 } 23343 23344 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23345 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 23346 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23347 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 23348 23349 /* Unit Information */ 23350 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 23351 info->dki_slave = ((tgt << 3) | lun); 23352 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 23353 DK_DEVLEN - 1); 23354 info->dki_flags = DKI_FMTVOL; 23355 info->dki_partition = SDPART(dev); 23356 23357 /* Max Transfer size of this device in blocks */ 23358 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 23359 info->dki_addr = 0; 23360 info->dki_space = 0; 23361 info->dki_prio = 0; 23362 info->dki_vec = 0; 23363 23364 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 23365 kmem_free(info, sizeof (struct dk_cinfo)); 23366 return (EFAULT); 23367 } else { 23368 kmem_free(info, sizeof (struct dk_cinfo)); 23369 return (0); 23370 } 23371 } 23372 23373 /* 23374 * Function: sd_get_media_info_com 23375 * 23376 * Description: This routine returns the information required to populate 23377 * the fields for the dk_minfo/dk_minfo_ext structures. 23378 * 23379 * Arguments: dev - the device number 23380 * dki_media_type - media_type 23381 * dki_lbsize - logical block size 23382 * dki_capacity - capacity in blocks 23383 * dki_pbsize - physical block size (if requested) 23384 * 23385 * Return Code: 0 23386 * EACCESS 23387 * EFAULT 23388 * ENXIO 23389 * EIO 23390 */ 23391 static int 23392 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize, 23393 diskaddr_t *dki_capacity, uint_t *dki_pbsize) 23394 { 23395 struct sd_lun *un = NULL; 23396 struct uscsi_cmd com; 23397 struct scsi_inquiry *sinq; 23398 u_longlong_t media_capacity; 23399 uint64_t capacity; 23400 uint_t lbasize; 23401 uint_t pbsize; 23402 uchar_t *out_data; 23403 uchar_t *rqbuf; 23404 int rval = 0; 23405 int rtn; 23406 sd_ssc_t *ssc; 23407 23408 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 23409 (un->un_state == SD_STATE_OFFLINE)) { 23410 return (ENXIO); 23411 } 23412 23413 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n"); 23414 23415 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 23416 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 23417 ssc = sd_ssc_init(un); 23418 23419 /* Issue a TUR to determine if the drive is ready with media present */ 23420 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 23421 if (rval == ENXIO) { 23422 goto done; 23423 } else if (rval != 0) { 23424 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23425 } 23426 23427 /* Now get configuration data */ 23428 if (ISCD(un)) { 23429 *dki_media_type = DK_CDROM; 23430 23431 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 23432 if (un->un_f_mmc_cap == TRUE) { 23433 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 23434 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 23435 SD_PATH_STANDARD); 23436 23437 if (rtn) { 23438 /* 23439 * We ignore all failures for CD and need to 23440 * put the assessment before processing code 23441 * to avoid missing assessment for FMA. 23442 */ 23443 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23444 /* 23445 * Failed for other than an illegal request 23446 * or command not supported 23447 */ 23448 if ((com.uscsi_status == STATUS_CHECK) && 23449 (com.uscsi_rqstatus == STATUS_GOOD)) { 23450 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 23451 (rqbuf[12] != 0x20)) { 23452 rval = EIO; 23453 goto no_assessment; 23454 } 23455 } 23456 } else { 23457 /* 23458 * The GET CONFIGURATION command succeeded 23459 * so set the media type according to the 23460 * returned data 23461 */ 23462 *dki_media_type = out_data[6]; 23463 *dki_media_type <<= 8; 23464 *dki_media_type |= out_data[7]; 23465 } 23466 } 23467 } else { 23468 /* 23469 * The profile list is not available, so we attempt to identify 23470 * the media type based on the inquiry data 23471 */ 23472 sinq = un->un_sd->sd_inq; 23473 if ((sinq->inq_dtype == DTYPE_DIRECT) || 23474 (sinq->inq_dtype == DTYPE_OPTICAL)) { 23475 /* This is a direct access device or optical disk */ 23476 *dki_media_type = DK_FIXED_DISK; 23477 23478 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 23479 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 23480 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 23481 *dki_media_type = DK_ZIP; 23482 } else if ( 23483 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 23484 *dki_media_type = DK_JAZ; 23485 } 23486 } 23487 } else { 23488 /* 23489 * Not a CD, direct access or optical disk so return 23490 * unknown media 23491 */ 23492 *dki_media_type = DK_UNKNOWN; 23493 } 23494 } 23495 23496 /* 23497 * Now read the capacity so we can provide the lbasize, 23498 * pbsize and capacity. 23499 */ 23500 if (dki_pbsize && un->un_f_descr_format_supported) { 23501 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 23502 &pbsize, SD_PATH_DIRECT); 23503 23504 /* 23505 * Override the physical blocksize if the instance already 23506 * has a larger value. 23507 */ 23508 pbsize = MAX(pbsize, un->un_phy_blocksize); 23509 } 23510 23511 if (dki_pbsize == NULL || rval != 0 || 23512 !un->un_f_descr_format_supported) { 23513 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 23514 SD_PATH_DIRECT); 23515 23516 switch (rval) { 23517 case 0: 23518 if (un->un_f_enable_rmw && 23519 un->un_phy_blocksize != 0) { 23520 pbsize = un->un_phy_blocksize; 23521 } else { 23522 pbsize = lbasize; 23523 } 23524 media_capacity = capacity; 23525 23526 /* 23527 * sd_send_scsi_READ_CAPACITY() reports capacity in 23528 * un->un_sys_blocksize chunks. So we need to convert 23529 * it into cap.lbsize chunks. 23530 */ 23531 if (un->un_f_has_removable_media) { 23532 media_capacity *= un->un_sys_blocksize; 23533 media_capacity /= lbasize; 23534 } 23535 break; 23536 case EACCES: 23537 rval = EACCES; 23538 goto done; 23539 default: 23540 rval = EIO; 23541 goto done; 23542 } 23543 } else { 23544 if (un->un_f_enable_rmw && 23545 !ISP2(pbsize % DEV_BSIZE)) { 23546 pbsize = SSD_SECSIZE; 23547 } else if (!ISP2(lbasize % DEV_BSIZE) || 23548 !ISP2(pbsize % DEV_BSIZE)) { 23549 pbsize = lbasize = DEV_BSIZE; 23550 } 23551 media_capacity = capacity; 23552 } 23553 23554 /* 23555 * If lun is expanded dynamically, update the un structure. 23556 */ 23557 mutex_enter(SD_MUTEX(un)); 23558 if ((un->un_f_blockcount_is_valid == TRUE) && 23559 (un->un_f_tgt_blocksize_is_valid == TRUE) && 23560 (capacity > un->un_blockcount)) { 23561 un->un_f_expnevent = B_FALSE; 23562 sd_update_block_info(un, lbasize, capacity); 23563 } 23564 mutex_exit(SD_MUTEX(un)); 23565 23566 *dki_lbsize = lbasize; 23567 *dki_capacity = media_capacity; 23568 if (dki_pbsize) 23569 *dki_pbsize = pbsize; 23570 23571 done: 23572 if (rval != 0) { 23573 if (rval == EIO) 23574 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23575 else 23576 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23577 } 23578 no_assessment: 23579 sd_ssc_fini(ssc); 23580 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 23581 kmem_free(rqbuf, SENSE_LENGTH); 23582 return (rval); 23583 } 23584 23585 /* 23586 * Function: sd_get_media_info 23587 * 23588 * Description: This routine is the driver entry point for handling ioctl 23589 * requests for the media type or command set profile used by the 23590 * drive to operate on the media (DKIOCGMEDIAINFO). 23591 * 23592 * Arguments: dev - the device number 23593 * arg - pointer to user provided dk_minfo structure 23594 * specifying the media type, logical block size and 23595 * drive capacity. 23596 * flag - this argument is a pass through to ddi_copyxxx() 23597 * directly from the mode argument of ioctl(). 23598 * 23599 * Return Code: returns the value from sd_get_media_info_com 23600 */ 23601 static int 23602 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 23603 { 23604 struct dk_minfo mi; 23605 int rval; 23606 23607 rval = sd_get_media_info_com(dev, &mi.dki_media_type, 23608 &mi.dki_lbsize, &mi.dki_capacity, NULL); 23609 23610 if (rval) 23611 return (rval); 23612 if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag)) 23613 rval = EFAULT; 23614 return (rval); 23615 } 23616 23617 /* 23618 * Function: sd_get_media_info_ext 23619 * 23620 * Description: This routine is the driver entry point for handling ioctl 23621 * requests for the media type or command set profile used by the 23622 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The 23623 * difference this ioctl and DKIOCGMEDIAINFO is the return value 23624 * of this ioctl contains both logical block size and physical 23625 * block size. 23626 * 23627 * 23628 * Arguments: dev - the device number 23629 * arg - pointer to user provided dk_minfo_ext structure 23630 * specifying the media type, logical block size, 23631 * physical block size and disk capacity. 23632 * flag - this argument is a pass through to ddi_copyxxx() 23633 * directly from the mode argument of ioctl(). 23634 * 23635 * Return Code: returns the value from sd_get_media_info_com 23636 */ 23637 static int 23638 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag) 23639 { 23640 struct dk_minfo_ext mie; 23641 int rval = 0; 23642 23643 rval = sd_get_media_info_com(dev, &mie.dki_media_type, 23644 &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize); 23645 23646 if (rval) 23647 return (rval); 23648 if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag)) 23649 rval = EFAULT; 23650 return (rval); 23651 23652 } 23653 23654 /* 23655 * Function: sd_watch_request_submit 23656 * 23657 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit 23658 * depending on which is supported by device. 23659 */ 23660 static opaque_t 23661 sd_watch_request_submit(struct sd_lun *un) 23662 { 23663 dev_t dev; 23664 23665 /* All submissions are unified to use same device number */ 23666 dev = sd_make_device(SD_DEVINFO(un)); 23667 23668 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23669 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un), 23670 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23671 (caddr_t)dev)); 23672 } else { 23673 return (scsi_watch_request_submit(SD_SCSI_DEVP(un), 23674 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23675 (caddr_t)dev)); 23676 } 23677 } 23678 23679 23680 /* 23681 * Function: sd_check_media 23682 * 23683 * Description: This utility routine implements the functionality for the 23684 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23685 * driver state changes from that specified by the user 23686 * (inserted or ejected). For example, if the user specifies 23687 * DKIO_EJECTED and the current media state is inserted this 23688 * routine will immediately return DKIO_INSERTED. However, if the 23689 * current media state is not inserted the user thread will be 23690 * blocked until the drive state changes. If DKIO_NONE is specified 23691 * the user thread will block until a drive state change occurs. 23692 * 23693 * Arguments: dev - the device number 23694 * state - user pointer to a dkio_state, updated with the current 23695 * drive state at return. 23696 * 23697 * Return Code: ENXIO 23698 * EIO 23699 * EAGAIN 23700 * EINTR 23701 */ 23702 23703 static int 23704 sd_check_media(dev_t dev, enum dkio_state state) 23705 { 23706 struct sd_lun *un = NULL; 23707 enum dkio_state prev_state; 23708 opaque_t token = NULL; 23709 int rval = 0; 23710 sd_ssc_t *ssc; 23711 23712 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23713 return (ENXIO); 23714 } 23715 23716 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23717 23718 ssc = sd_ssc_init(un); 23719 23720 mutex_enter(SD_MUTEX(un)); 23721 23722 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23723 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23724 23725 prev_state = un->un_mediastate; 23726 23727 /* is there anything to do? */ 23728 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23729 /* 23730 * submit the request to the scsi_watch service; 23731 * scsi_media_watch_cb() does the real work 23732 */ 23733 mutex_exit(SD_MUTEX(un)); 23734 23735 /* 23736 * This change handles the case where a scsi watch request is 23737 * added to a device that is powered down. To accomplish this 23738 * we power up the device before adding the scsi watch request, 23739 * since the scsi watch sends a TUR directly to the device 23740 * which the device cannot handle if it is powered down. 23741 */ 23742 if (sd_pm_entry(un) != DDI_SUCCESS) { 23743 mutex_enter(SD_MUTEX(un)); 23744 goto done; 23745 } 23746 23747 token = sd_watch_request_submit(un); 23748 23749 sd_pm_exit(un); 23750 23751 mutex_enter(SD_MUTEX(un)); 23752 if (token == NULL) { 23753 rval = EAGAIN; 23754 goto done; 23755 } 23756 23757 /* 23758 * This is a special case IOCTL that doesn't return 23759 * until the media state changes. Routine sdpower 23760 * knows about and handles this so don't count it 23761 * as an active cmd in the driver, which would 23762 * keep the device busy to the pm framework. 23763 * If the count isn't decremented the device can't 23764 * be powered down. 23765 */ 23766 un->un_ncmds_in_driver--; 23767 ASSERT(un->un_ncmds_in_driver >= 0); 23768 23769 /* 23770 * if a prior request had been made, this will be the same 23771 * token, as scsi_watch was designed that way. 23772 */ 23773 un->un_swr_token = token; 23774 un->un_specified_mediastate = state; 23775 23776 /* 23777 * now wait for media change 23778 * we will not be signalled unless mediastate == state but it is 23779 * still better to test for this condition, since there is a 23780 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23781 */ 23782 SD_TRACE(SD_LOG_COMMON, un, 23783 "sd_check_media: waiting for media state change\n"); 23784 while (un->un_mediastate == state) { 23785 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23786 SD_TRACE(SD_LOG_COMMON, un, 23787 "sd_check_media: waiting for media state " 23788 "was interrupted\n"); 23789 un->un_ncmds_in_driver++; 23790 rval = EINTR; 23791 goto done; 23792 } 23793 SD_TRACE(SD_LOG_COMMON, un, 23794 "sd_check_media: received signal, state=%x\n", 23795 un->un_mediastate); 23796 } 23797 /* 23798 * Inc the counter to indicate the device once again 23799 * has an active outstanding cmd. 23800 */ 23801 un->un_ncmds_in_driver++; 23802 } 23803 23804 /* invalidate geometry */ 23805 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23806 sr_ejected(un); 23807 } 23808 23809 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 23810 uint64_t capacity; 23811 uint_t lbasize; 23812 23813 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 23814 mutex_exit(SD_MUTEX(un)); 23815 /* 23816 * Since the following routines use SD_PATH_DIRECT, we must 23817 * call PM directly before the upcoming disk accesses. This 23818 * may cause the disk to be power/spin up. 23819 */ 23820 23821 if (sd_pm_entry(un) == DDI_SUCCESS) { 23822 rval = sd_send_scsi_READ_CAPACITY(ssc, 23823 &capacity, &lbasize, SD_PATH_DIRECT); 23824 if (rval != 0) { 23825 sd_pm_exit(un); 23826 if (rval == EIO) 23827 sd_ssc_assessment(ssc, 23828 SD_FMT_STATUS_CHECK); 23829 else 23830 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23831 mutex_enter(SD_MUTEX(un)); 23832 goto done; 23833 } 23834 } else { 23835 rval = EIO; 23836 mutex_enter(SD_MUTEX(un)); 23837 goto done; 23838 } 23839 mutex_enter(SD_MUTEX(un)); 23840 23841 sd_update_block_info(un, lbasize, capacity); 23842 23843 /* 23844 * Check if the media in the device is writable or not 23845 */ 23846 if (ISCD(un)) { 23847 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 23848 } 23849 23850 mutex_exit(SD_MUTEX(un)); 23851 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 23852 if ((cmlb_validate(un->un_cmlbhandle, 0, 23853 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 23854 sd_set_pstats(un); 23855 SD_TRACE(SD_LOG_IO_PARTITION, un, 23856 "sd_check_media: un:0x%p pstats created and " 23857 "set\n", un); 23858 } 23859 23860 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 23861 SD_PATH_DIRECT); 23862 23863 sd_pm_exit(un); 23864 23865 if (rval != 0) { 23866 if (rval == EIO) 23867 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23868 else 23869 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23870 } 23871 23872 mutex_enter(SD_MUTEX(un)); 23873 } 23874 done: 23875 sd_ssc_fini(ssc); 23876 un->un_f_watcht_stopped = FALSE; 23877 if (token != NULL && un->un_swr_token != NULL) { 23878 /* 23879 * Use of this local token and the mutex ensures that we avoid 23880 * some race conditions associated with terminating the 23881 * scsi watch. 23882 */ 23883 token = un->un_swr_token; 23884 mutex_exit(SD_MUTEX(un)); 23885 (void) scsi_watch_request_terminate(token, 23886 SCSI_WATCH_TERMINATE_WAIT); 23887 if (scsi_watch_get_ref_count(token) == 0) { 23888 mutex_enter(SD_MUTEX(un)); 23889 un->un_swr_token = (opaque_t)NULL; 23890 } else { 23891 mutex_enter(SD_MUTEX(un)); 23892 } 23893 } 23894 23895 /* 23896 * Update the capacity kstat value, if no media previously 23897 * (capacity kstat is 0) and a media has been inserted 23898 * (un_f_blockcount_is_valid == TRUE) 23899 */ 23900 if (un->un_errstats) { 23901 struct sd_errstats *stp = NULL; 23902 23903 stp = (struct sd_errstats *)un->un_errstats->ks_data; 23904 if ((stp->sd_capacity.value.ui64 == 0) && 23905 (un->un_f_blockcount_is_valid == TRUE)) { 23906 stp->sd_capacity.value.ui64 = 23907 (uint64_t)((uint64_t)un->un_blockcount * 23908 un->un_sys_blocksize); 23909 } 23910 } 23911 mutex_exit(SD_MUTEX(un)); 23912 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 23913 return (rval); 23914 } 23915 23916 23917 /* 23918 * Function: sd_delayed_cv_broadcast 23919 * 23920 * Description: Delayed cv_broadcast to allow for target to recover from media 23921 * insertion. 23922 * 23923 * Arguments: arg - driver soft state (unit) structure 23924 */ 23925 23926 static void 23927 sd_delayed_cv_broadcast(void *arg) 23928 { 23929 struct sd_lun *un = arg; 23930 23931 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 23932 23933 mutex_enter(SD_MUTEX(un)); 23934 un->un_dcvb_timeid = NULL; 23935 cv_broadcast(&un->un_state_cv); 23936 mutex_exit(SD_MUTEX(un)); 23937 } 23938 23939 23940 /* 23941 * Function: sd_media_watch_cb 23942 * 23943 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 23944 * routine processes the TUR sense data and updates the driver 23945 * state if a transition has occurred. The user thread 23946 * (sd_check_media) is then signalled. 23947 * 23948 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23949 * among multiple watches that share this callback function 23950 * resultp - scsi watch facility result packet containing scsi 23951 * packet, status byte and sense data 23952 * 23953 * Return Code: 0 for success, -1 for failure 23954 */ 23955 23956 static int 23957 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23958 { 23959 struct sd_lun *un; 23960 struct scsi_status *statusp = resultp->statusp; 23961 uint8_t *sensep = (uint8_t *)resultp->sensep; 23962 enum dkio_state state = DKIO_NONE; 23963 dev_t dev = (dev_t)arg; 23964 uchar_t actual_sense_length; 23965 uint8_t skey, asc, ascq; 23966 23967 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23968 return (-1); 23969 } 23970 actual_sense_length = resultp->actual_sense_length; 23971 23972 mutex_enter(SD_MUTEX(un)); 23973 SD_TRACE(SD_LOG_COMMON, un, 23974 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 23975 *((char *)statusp), (void *)sensep, actual_sense_length); 23976 23977 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 23978 un->un_mediastate = DKIO_DEV_GONE; 23979 cv_broadcast(&un->un_state_cv); 23980 mutex_exit(SD_MUTEX(un)); 23981 23982 return (0); 23983 } 23984 23985 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23986 if (sd_gesn_media_data_valid(resultp->mmc_data)) { 23987 if ((resultp->mmc_data[5] & 23988 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) { 23989 state = DKIO_INSERTED; 23990 } else { 23991 state = DKIO_EJECTED; 23992 } 23993 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) == 23994 SD_GESN_MEDIA_EVENT_EJECTREQUEST) { 23995 sd_log_eject_request_event(un, KM_NOSLEEP); 23996 } 23997 } 23998 } else if (sensep != NULL) { 23999 /* 24000 * If there was a check condition then sensep points to valid 24001 * sense data. If status was not a check condition but a 24002 * reservation or busy status then the new state is DKIO_NONE. 24003 */ 24004 skey = scsi_sense_key(sensep); 24005 asc = scsi_sense_asc(sensep); 24006 ascq = scsi_sense_ascq(sensep); 24007 24008 SD_INFO(SD_LOG_COMMON, un, 24009 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24010 skey, asc, ascq); 24011 /* This routine only uses up to 13 bytes of sense data. */ 24012 if (actual_sense_length >= 13) { 24013 if (skey == KEY_UNIT_ATTENTION) { 24014 if (asc == 0x28) { 24015 state = DKIO_INSERTED; 24016 } 24017 } else if (skey == KEY_NOT_READY) { 24018 /* 24019 * Sense data of 02/06/00 means that the 24020 * drive could not read the media (No 24021 * reference position found). In this case 24022 * to prevent a hang on the DKIOCSTATE IOCTL 24023 * we set the media state to DKIO_INSERTED. 24024 */ 24025 if (asc == 0x06 && ascq == 0x00) 24026 state = DKIO_INSERTED; 24027 24028 /* 24029 * if 02/04/02 means that the host 24030 * should send start command. Explicitly 24031 * leave the media state as is 24032 * (inserted) as the media is inserted 24033 * and host has stopped device for PM 24034 * reasons. Upon next true read/write 24035 * to this media will bring the 24036 * device to the right state good for 24037 * media access. 24038 */ 24039 if (asc == 0x3a) { 24040 state = DKIO_EJECTED; 24041 } else { 24042 /* 24043 * If the drive is busy with an 24044 * operation or long write, keep the 24045 * media in an inserted state. 24046 */ 24047 24048 if ((asc == 0x04) && 24049 ((ascq == 0x02) || 24050 (ascq == 0x07) || 24051 (ascq == 0x08))) { 24052 state = DKIO_INSERTED; 24053 } 24054 } 24055 } else if (skey == KEY_NO_SENSE) { 24056 if ((asc == 0x00) && (ascq == 0x00)) { 24057 /* 24058 * Sense Data 00/00/00 does not provide 24059 * any information about the state of 24060 * the media. Ignore it. 24061 */ 24062 mutex_exit(SD_MUTEX(un)); 24063 return (0); 24064 } 24065 } 24066 } 24067 } else if ((*((char *)statusp) == STATUS_GOOD) && 24068 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24069 state = DKIO_INSERTED; 24070 } 24071 24072 SD_TRACE(SD_LOG_COMMON, un, 24073 "sd_media_watch_cb: state=%x, specified=%x\n", 24074 state, un->un_specified_mediastate); 24075 24076 /* 24077 * now signal the waiting thread if this is *not* the specified state; 24078 * delay the signal if the state is DKIO_INSERTED to allow the target 24079 * to recover 24080 */ 24081 if (state != un->un_specified_mediastate) { 24082 un->un_mediastate = state; 24083 if (state == DKIO_INSERTED) { 24084 /* 24085 * delay the signal to give the drive a chance 24086 * to do what it apparently needs to do 24087 */ 24088 SD_TRACE(SD_LOG_COMMON, un, 24089 "sd_media_watch_cb: delayed cv_broadcast\n"); 24090 if (un->un_dcvb_timeid == NULL) { 24091 un->un_dcvb_timeid = 24092 timeout(sd_delayed_cv_broadcast, un, 24093 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24094 } 24095 } else { 24096 SD_TRACE(SD_LOG_COMMON, un, 24097 "sd_media_watch_cb: immediate cv_broadcast\n"); 24098 cv_broadcast(&un->un_state_cv); 24099 } 24100 } 24101 mutex_exit(SD_MUTEX(un)); 24102 return (0); 24103 } 24104 24105 24106 /* 24107 * Function: sd_dkio_get_temp 24108 * 24109 * Description: This routine is the driver entry point for handling ioctl 24110 * requests to get the disk temperature. 24111 * 24112 * Arguments: dev - the device number 24113 * arg - pointer to user provided dk_temperature structure. 24114 * flag - this argument is a pass through to ddi_copyxxx() 24115 * directly from the mode argument of ioctl(). 24116 * 24117 * Return Code: 0 24118 * EFAULT 24119 * ENXIO 24120 * EAGAIN 24121 */ 24122 24123 static int 24124 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24125 { 24126 struct sd_lun *un = NULL; 24127 struct dk_temperature *dktemp = NULL; 24128 uchar_t *temperature_page; 24129 int rval = 0; 24130 int path_flag = SD_PATH_STANDARD; 24131 sd_ssc_t *ssc; 24132 24133 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24134 return (ENXIO); 24135 } 24136 24137 ssc = sd_ssc_init(un); 24138 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24139 24140 /* copyin the disk temp argument to get the user flags */ 24141 if (ddi_copyin((void *)arg, dktemp, 24142 sizeof (struct dk_temperature), flag) != 0) { 24143 rval = EFAULT; 24144 goto done; 24145 } 24146 24147 /* Initialize the temperature to invalid. */ 24148 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24149 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24150 24151 /* 24152 * Note: Investigate removing the "bypass pm" semantic. 24153 * Can we just bypass PM always? 24154 */ 24155 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24156 path_flag = SD_PATH_DIRECT; 24157 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24158 mutex_enter(&un->un_pm_mutex); 24159 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24160 /* 24161 * If DKT_BYPASS_PM is set, and the drive happens to be 24162 * in low power mode, we can not wake it up, Need to 24163 * return EAGAIN. 24164 */ 24165 mutex_exit(&un->un_pm_mutex); 24166 rval = EAGAIN; 24167 goto done; 24168 } else { 24169 /* 24170 * Indicate to PM the device is busy. This is required 24171 * to avoid a race - i.e. the ioctl is issuing a 24172 * command and the pm framework brings down the device 24173 * to low power mode (possible power cut-off on some 24174 * platforms). 24175 */ 24176 mutex_exit(&un->un_pm_mutex); 24177 if (sd_pm_entry(un) != DDI_SUCCESS) { 24178 rval = EAGAIN; 24179 goto done; 24180 } 24181 } 24182 } 24183 24184 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24185 24186 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page, 24187 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag); 24188 if (rval != 0) 24189 goto done2; 24190 24191 /* 24192 * For the current temperature verify that the parameter length is 0x02 24193 * and the parameter code is 0x00 24194 */ 24195 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24196 (temperature_page[5] == 0x00)) { 24197 if (temperature_page[9] == 0xFF) { 24198 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24199 } else { 24200 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24201 } 24202 } 24203 24204 /* 24205 * For the reference temperature verify that the parameter 24206 * length is 0x02 and the parameter code is 0x01 24207 */ 24208 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24209 (temperature_page[11] == 0x01)) { 24210 if (temperature_page[15] == 0xFF) { 24211 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24212 } else { 24213 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24214 } 24215 } 24216 24217 /* Do the copyout regardless of the temperature commands status. */ 24218 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24219 flag) != 0) { 24220 rval = EFAULT; 24221 goto done1; 24222 } 24223 24224 done2: 24225 if (rval != 0) { 24226 if (rval == EIO) 24227 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24228 else 24229 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24230 } 24231 done1: 24232 if (path_flag == SD_PATH_DIRECT) { 24233 sd_pm_exit(un); 24234 } 24235 24236 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24237 done: 24238 sd_ssc_fini(ssc); 24239 if (dktemp != NULL) { 24240 kmem_free(dktemp, sizeof (struct dk_temperature)); 24241 } 24242 24243 return (rval); 24244 } 24245 24246 24247 /* 24248 * Function: sd_log_page_supported 24249 * 24250 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24251 * supported log pages. 24252 * 24253 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 24254 * structure for this target. 24255 * log_page - 24256 * 24257 * Return Code: -1 - on error (log sense is optional and may not be supported). 24258 * 0 - log page not found. 24259 * 1 - log page found. 24260 */ 24261 24262 static int 24263 sd_log_page_supported(sd_ssc_t *ssc, int log_page) 24264 { 24265 uchar_t *log_page_data; 24266 int i; 24267 int match = 0; 24268 int log_size; 24269 int status = 0; 24270 struct sd_lun *un; 24271 24272 ASSERT(ssc != NULL); 24273 un = ssc->ssc_un; 24274 ASSERT(un != NULL); 24275 24276 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24277 24278 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0, 24279 SD_PATH_DIRECT); 24280 24281 if (status != 0) { 24282 if (status == EIO) { 24283 /* 24284 * Some disks do not support log sense, we 24285 * should ignore this kind of error(sense key is 24286 * 0x5 - illegal request). 24287 */ 24288 uint8_t *sensep; 24289 int senlen; 24290 24291 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 24292 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 24293 ssc->ssc_uscsi_cmd->uscsi_rqresid); 24294 24295 if (senlen > 0 && 24296 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 24297 sd_ssc_assessment(ssc, 24298 SD_FMT_IGNORE_COMPROMISE); 24299 } else { 24300 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24301 } 24302 } else { 24303 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24304 } 24305 24306 SD_ERROR(SD_LOG_COMMON, un, 24307 "sd_log_page_supported: failed log page retrieval\n"); 24308 kmem_free(log_page_data, 0xFF); 24309 return (-1); 24310 } 24311 24312 log_size = log_page_data[3]; 24313 24314 /* 24315 * The list of supported log pages start from the fourth byte. Check 24316 * until we run out of log pages or a match is found. 24317 */ 24318 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24319 if (log_page_data[i] == log_page) { 24320 match++; 24321 } 24322 } 24323 kmem_free(log_page_data, 0xFF); 24324 return (match); 24325 } 24326 24327 24328 /* 24329 * Function: sd_mhdioc_failfast 24330 * 24331 * Description: This routine is the driver entry point for handling ioctl 24332 * requests to enable/disable the multihost failfast option. 24333 * (MHIOCENFAILFAST) 24334 * 24335 * Arguments: dev - the device number 24336 * arg - user specified probing interval. 24337 * flag - this argument is a pass through to ddi_copyxxx() 24338 * directly from the mode argument of ioctl(). 24339 * 24340 * Return Code: 0 24341 * EFAULT 24342 * ENXIO 24343 */ 24344 24345 static int 24346 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24347 { 24348 struct sd_lun *un = NULL; 24349 int mh_time; 24350 int rval = 0; 24351 24352 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24353 return (ENXIO); 24354 } 24355 24356 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24357 return (EFAULT); 24358 24359 if (mh_time) { 24360 mutex_enter(SD_MUTEX(un)); 24361 un->un_resvd_status |= SD_FAILFAST; 24362 mutex_exit(SD_MUTEX(un)); 24363 /* 24364 * If mh_time is INT_MAX, then this ioctl is being used for 24365 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24366 */ 24367 if (mh_time != INT_MAX) { 24368 rval = sd_check_mhd(dev, mh_time); 24369 } 24370 } else { 24371 (void) sd_check_mhd(dev, 0); 24372 mutex_enter(SD_MUTEX(un)); 24373 un->un_resvd_status &= ~SD_FAILFAST; 24374 mutex_exit(SD_MUTEX(un)); 24375 } 24376 return (rval); 24377 } 24378 24379 24380 /* 24381 * Function: sd_mhdioc_takeown 24382 * 24383 * Description: This routine is the driver entry point for handling ioctl 24384 * requests to forcefully acquire exclusive access rights to the 24385 * multihost disk (MHIOCTKOWN). 24386 * 24387 * Arguments: dev - the device number 24388 * arg - user provided structure specifying the delay 24389 * parameters in milliseconds 24390 * flag - this argument is a pass through to ddi_copyxxx() 24391 * directly from the mode argument of ioctl(). 24392 * 24393 * Return Code: 0 24394 * EFAULT 24395 * ENXIO 24396 */ 24397 24398 static int 24399 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24400 { 24401 struct sd_lun *un = NULL; 24402 struct mhioctkown *tkown = NULL; 24403 int rval = 0; 24404 24405 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24406 return (ENXIO); 24407 } 24408 24409 if (arg != NULL) { 24410 tkown = (struct mhioctkown *) 24411 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24412 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24413 if (rval != 0) { 24414 rval = EFAULT; 24415 goto error; 24416 } 24417 } 24418 24419 rval = sd_take_ownership(dev, tkown); 24420 mutex_enter(SD_MUTEX(un)); 24421 if (rval == 0) { 24422 un->un_resvd_status |= SD_RESERVE; 24423 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24424 sd_reinstate_resv_delay = 24425 tkown->reinstate_resv_delay * 1000; 24426 } else { 24427 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24428 } 24429 /* 24430 * Give the scsi_watch routine interval set by 24431 * the MHIOCENFAILFAST ioctl precedence here. 24432 */ 24433 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24434 mutex_exit(SD_MUTEX(un)); 24435 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24436 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24437 "sd_mhdioc_takeown : %d\n", 24438 sd_reinstate_resv_delay); 24439 } else { 24440 mutex_exit(SD_MUTEX(un)); 24441 } 24442 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24443 sd_mhd_reset_notify_cb, (caddr_t)un); 24444 } else { 24445 un->un_resvd_status &= ~SD_RESERVE; 24446 mutex_exit(SD_MUTEX(un)); 24447 } 24448 24449 error: 24450 if (tkown != NULL) { 24451 kmem_free(tkown, sizeof (struct mhioctkown)); 24452 } 24453 return (rval); 24454 } 24455 24456 24457 /* 24458 * Function: sd_mhdioc_release 24459 * 24460 * Description: This routine is the driver entry point for handling ioctl 24461 * requests to release exclusive access rights to the multihost 24462 * disk (MHIOCRELEASE). 24463 * 24464 * Arguments: dev - the device number 24465 * 24466 * Return Code: 0 24467 * ENXIO 24468 */ 24469 24470 static int 24471 sd_mhdioc_release(dev_t dev) 24472 { 24473 struct sd_lun *un = NULL; 24474 timeout_id_t resvd_timeid_save; 24475 int resvd_status_save; 24476 int rval = 0; 24477 24478 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24479 return (ENXIO); 24480 } 24481 24482 mutex_enter(SD_MUTEX(un)); 24483 resvd_status_save = un->un_resvd_status; 24484 un->un_resvd_status &= 24485 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24486 if (un->un_resvd_timeid) { 24487 resvd_timeid_save = un->un_resvd_timeid; 24488 un->un_resvd_timeid = NULL; 24489 mutex_exit(SD_MUTEX(un)); 24490 (void) untimeout(resvd_timeid_save); 24491 } else { 24492 mutex_exit(SD_MUTEX(un)); 24493 } 24494 24495 /* 24496 * destroy any pending timeout thread that may be attempting to 24497 * reinstate reservation on this device. 24498 */ 24499 sd_rmv_resv_reclaim_req(dev); 24500 24501 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24502 mutex_enter(SD_MUTEX(un)); 24503 if ((un->un_mhd_token) && 24504 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24505 mutex_exit(SD_MUTEX(un)); 24506 (void) sd_check_mhd(dev, 0); 24507 } else { 24508 mutex_exit(SD_MUTEX(un)); 24509 } 24510 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24511 sd_mhd_reset_notify_cb, (caddr_t)un); 24512 } else { 24513 /* 24514 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24515 */ 24516 mutex_enter(SD_MUTEX(un)); 24517 un->un_resvd_status = resvd_status_save; 24518 mutex_exit(SD_MUTEX(un)); 24519 } 24520 return (rval); 24521 } 24522 24523 24524 /* 24525 * Function: sd_mhdioc_register_devid 24526 * 24527 * Description: This routine is the driver entry point for handling ioctl 24528 * requests to register the device id (MHIOCREREGISTERDEVID). 24529 * 24530 * Note: The implementation for this ioctl has been updated to 24531 * be consistent with the original PSARC case (1999/357) 24532 * (4375899, 4241671, 4220005) 24533 * 24534 * Arguments: dev - the device number 24535 * 24536 * Return Code: 0 24537 * ENXIO 24538 */ 24539 24540 static int 24541 sd_mhdioc_register_devid(dev_t dev) 24542 { 24543 struct sd_lun *un = NULL; 24544 int rval = 0; 24545 sd_ssc_t *ssc; 24546 24547 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24548 return (ENXIO); 24549 } 24550 24551 ASSERT(!mutex_owned(SD_MUTEX(un))); 24552 24553 mutex_enter(SD_MUTEX(un)); 24554 24555 /* If a devid already exists, de-register it */ 24556 if (un->un_devid != NULL) { 24557 ddi_devid_unregister(SD_DEVINFO(un)); 24558 /* 24559 * After unregister devid, needs to free devid memory 24560 */ 24561 ddi_devid_free(un->un_devid); 24562 un->un_devid = NULL; 24563 } 24564 24565 /* Check for reservation conflict */ 24566 mutex_exit(SD_MUTEX(un)); 24567 ssc = sd_ssc_init(un); 24568 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 24569 mutex_enter(SD_MUTEX(un)); 24570 24571 switch (rval) { 24572 case 0: 24573 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24574 break; 24575 case EACCES: 24576 break; 24577 default: 24578 rval = EIO; 24579 } 24580 24581 mutex_exit(SD_MUTEX(un)); 24582 if (rval != 0) { 24583 if (rval == EIO) 24584 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24585 else 24586 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24587 } 24588 sd_ssc_fini(ssc); 24589 return (rval); 24590 } 24591 24592 24593 /* 24594 * Function: sd_mhdioc_inkeys 24595 * 24596 * Description: This routine is the driver entry point for handling ioctl 24597 * requests to issue the SCSI-3 Persistent In Read Keys command 24598 * to the device (MHIOCGRP_INKEYS). 24599 * 24600 * Arguments: dev - the device number 24601 * arg - user provided in_keys structure 24602 * flag - this argument is a pass through to ddi_copyxxx() 24603 * directly from the mode argument of ioctl(). 24604 * 24605 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24606 * ENXIO 24607 * EFAULT 24608 */ 24609 24610 static int 24611 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24612 { 24613 struct sd_lun *un; 24614 mhioc_inkeys_t inkeys; 24615 int rval = 0; 24616 24617 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24618 return (ENXIO); 24619 } 24620 24621 #ifdef _MULTI_DATAMODEL 24622 switch (ddi_model_convert_from(flag & FMODELS)) { 24623 case DDI_MODEL_ILP32: { 24624 struct mhioc_inkeys32 inkeys32; 24625 24626 if (ddi_copyin(arg, &inkeys32, 24627 sizeof (struct mhioc_inkeys32), flag) != 0) { 24628 return (EFAULT); 24629 } 24630 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24631 if ((rval = sd_persistent_reservation_in_read_keys(un, 24632 &inkeys, flag)) != 0) { 24633 return (rval); 24634 } 24635 inkeys32.generation = inkeys.generation; 24636 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24637 flag) != 0) { 24638 return (EFAULT); 24639 } 24640 break; 24641 } 24642 case DDI_MODEL_NONE: 24643 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24644 flag) != 0) { 24645 return (EFAULT); 24646 } 24647 if ((rval = sd_persistent_reservation_in_read_keys(un, 24648 &inkeys, flag)) != 0) { 24649 return (rval); 24650 } 24651 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24652 flag) != 0) { 24653 return (EFAULT); 24654 } 24655 break; 24656 } 24657 24658 #else /* ! _MULTI_DATAMODEL */ 24659 24660 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24661 return (EFAULT); 24662 } 24663 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24664 if (rval != 0) { 24665 return (rval); 24666 } 24667 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24668 return (EFAULT); 24669 } 24670 24671 #endif /* _MULTI_DATAMODEL */ 24672 24673 return (rval); 24674 } 24675 24676 24677 /* 24678 * Function: sd_mhdioc_inresv 24679 * 24680 * Description: This routine is the driver entry point for handling ioctl 24681 * requests to issue the SCSI-3 Persistent In Read Reservations 24682 * command to the device (MHIOCGRP_INKEYS). 24683 * 24684 * Arguments: dev - the device number 24685 * arg - user provided in_resv structure 24686 * flag - this argument is a pass through to ddi_copyxxx() 24687 * directly from the mode argument of ioctl(). 24688 * 24689 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24690 * ENXIO 24691 * EFAULT 24692 */ 24693 24694 static int 24695 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24696 { 24697 struct sd_lun *un; 24698 mhioc_inresvs_t inresvs; 24699 int rval = 0; 24700 24701 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24702 return (ENXIO); 24703 } 24704 24705 #ifdef _MULTI_DATAMODEL 24706 24707 switch (ddi_model_convert_from(flag & FMODELS)) { 24708 case DDI_MODEL_ILP32: { 24709 struct mhioc_inresvs32 inresvs32; 24710 24711 if (ddi_copyin(arg, &inresvs32, 24712 sizeof (struct mhioc_inresvs32), flag) != 0) { 24713 return (EFAULT); 24714 } 24715 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24716 if ((rval = sd_persistent_reservation_in_read_resv(un, 24717 &inresvs, flag)) != 0) { 24718 return (rval); 24719 } 24720 inresvs32.generation = inresvs.generation; 24721 if (ddi_copyout(&inresvs32, arg, 24722 sizeof (struct mhioc_inresvs32), flag) != 0) { 24723 return (EFAULT); 24724 } 24725 break; 24726 } 24727 case DDI_MODEL_NONE: 24728 if (ddi_copyin(arg, &inresvs, 24729 sizeof (mhioc_inresvs_t), flag) != 0) { 24730 return (EFAULT); 24731 } 24732 if ((rval = sd_persistent_reservation_in_read_resv(un, 24733 &inresvs, flag)) != 0) { 24734 return (rval); 24735 } 24736 if (ddi_copyout(&inresvs, arg, 24737 sizeof (mhioc_inresvs_t), flag) != 0) { 24738 return (EFAULT); 24739 } 24740 break; 24741 } 24742 24743 #else /* ! _MULTI_DATAMODEL */ 24744 24745 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24746 return (EFAULT); 24747 } 24748 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24749 if (rval != 0) { 24750 return (rval); 24751 } 24752 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24753 return (EFAULT); 24754 } 24755 24756 #endif /* ! _MULTI_DATAMODEL */ 24757 24758 return (rval); 24759 } 24760 24761 24762 /* 24763 * The following routines support the clustering functionality described below 24764 * and implement lost reservation reclaim functionality. 24765 * 24766 * Clustering 24767 * ---------- 24768 * The clustering code uses two different, independent forms of SCSI 24769 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24770 * Persistent Group Reservations. For any particular disk, it will use either 24771 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24772 * 24773 * SCSI-2 24774 * The cluster software takes ownership of a multi-hosted disk by issuing the 24775 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24776 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 24777 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 24778 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 24779 * driver. The meaning of failfast is that if the driver (on this host) ever 24780 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 24781 * it should immediately panic the host. The motivation for this ioctl is that 24782 * if this host does encounter reservation conflict, the underlying cause is 24783 * that some other host of the cluster has decided that this host is no longer 24784 * in the cluster and has seized control of the disks for itself. Since this 24785 * host is no longer in the cluster, it ought to panic itself. The 24786 * MHIOCENFAILFAST ioctl does two things: 24787 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24788 * error to panic the host 24789 * (b) it sets up a periodic timer to test whether this host still has 24790 * "access" (in that no other host has reserved the device): if the 24791 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24792 * purpose of that periodic timer is to handle scenarios where the host is 24793 * otherwise temporarily quiescent, temporarily doing no real i/o. 24794 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24795 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24796 * the device itself. 24797 * 24798 * SCSI-3 PGR 24799 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24800 * facility is supported through the shared multihost disk ioctls 24801 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24802 * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR) 24803 * 24804 * Reservation Reclaim: 24805 * -------------------- 24806 * To support the lost reservation reclaim operations this driver creates a 24807 * single thread to handle reinstating reservations on all devices that have 24808 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24809 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24810 * and the reservation reclaim thread loops through the requests to regain the 24811 * lost reservations. 24812 */ 24813 24814 /* 24815 * Function: sd_check_mhd() 24816 * 24817 * Description: This function sets up and submits a scsi watch request or 24818 * terminates an existing watch request. This routine is used in 24819 * support of reservation reclaim. 24820 * 24821 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24822 * among multiple watches that share the callback function 24823 * interval - the number of microseconds specifying the watch 24824 * interval for issuing TEST UNIT READY commands. If 24825 * set to 0 the watch should be terminated. If the 24826 * interval is set to 0 and if the device is required 24827 * to hold reservation while disabling failfast, the 24828 * watch is restarted with an interval of 24829 * reinstate_resv_delay. 24830 * 24831 * Return Code: 0 - Successful submit/terminate of scsi watch request 24832 * ENXIO - Indicates an invalid device was specified 24833 * EAGAIN - Unable to submit the scsi watch request 24834 */ 24835 24836 static int 24837 sd_check_mhd(dev_t dev, int interval) 24838 { 24839 struct sd_lun *un; 24840 opaque_t token; 24841 24842 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24843 return (ENXIO); 24844 } 24845 24846 /* is this a watch termination request? */ 24847 if (interval == 0) { 24848 mutex_enter(SD_MUTEX(un)); 24849 /* if there is an existing watch task then terminate it */ 24850 if (un->un_mhd_token) { 24851 token = un->un_mhd_token; 24852 un->un_mhd_token = NULL; 24853 mutex_exit(SD_MUTEX(un)); 24854 (void) scsi_watch_request_terminate(token, 24855 SCSI_WATCH_TERMINATE_ALL_WAIT); 24856 mutex_enter(SD_MUTEX(un)); 24857 } else { 24858 mutex_exit(SD_MUTEX(un)); 24859 /* 24860 * Note: If we return here we don't check for the 24861 * failfast case. This is the original legacy 24862 * implementation but perhaps we should be checking 24863 * the failfast case. 24864 */ 24865 return (0); 24866 } 24867 /* 24868 * If the device is required to hold reservation while 24869 * disabling failfast, we need to restart the scsi_watch 24870 * routine with an interval of reinstate_resv_delay. 24871 */ 24872 if (un->un_resvd_status & SD_RESERVE) { 24873 interval = sd_reinstate_resv_delay/1000; 24874 } else { 24875 /* no failfast so bail */ 24876 mutex_exit(SD_MUTEX(un)); 24877 return (0); 24878 } 24879 mutex_exit(SD_MUTEX(un)); 24880 } 24881 24882 /* 24883 * adjust minimum time interval to 1 second, 24884 * and convert from msecs to usecs 24885 */ 24886 if (interval > 0 && interval < 1000) { 24887 interval = 1000; 24888 } 24889 interval *= 1000; 24890 24891 /* 24892 * submit the request to the scsi_watch service 24893 */ 24894 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24895 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24896 if (token == NULL) { 24897 return (EAGAIN); 24898 } 24899 24900 /* 24901 * save token for termination later on 24902 */ 24903 mutex_enter(SD_MUTEX(un)); 24904 un->un_mhd_token = token; 24905 mutex_exit(SD_MUTEX(un)); 24906 return (0); 24907 } 24908 24909 24910 /* 24911 * Function: sd_mhd_watch_cb() 24912 * 24913 * Description: This function is the call back function used by the scsi watch 24914 * facility. The scsi watch facility sends the "Test Unit Ready" 24915 * and processes the status. If applicable (i.e. a "Unit Attention" 24916 * status and automatic "Request Sense" not used) the scsi watch 24917 * facility will send a "Request Sense" and retrieve the sense data 24918 * to be passed to this callback function. In either case the 24919 * automatic "Request Sense" or the facility submitting one, this 24920 * callback is passed the status and sense data. 24921 * 24922 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24923 * among multiple watches that share this callback function 24924 * resultp - scsi watch facility result packet containing scsi 24925 * packet, status byte and sense data 24926 * 24927 * Return Code: 0 - continue the watch task 24928 * non-zero - terminate the watch task 24929 */ 24930 24931 static int 24932 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24933 { 24934 struct sd_lun *un; 24935 struct scsi_status *statusp; 24936 uint8_t *sensep; 24937 struct scsi_pkt *pkt; 24938 uchar_t actual_sense_length; 24939 dev_t dev = (dev_t)arg; 24940 24941 ASSERT(resultp != NULL); 24942 statusp = resultp->statusp; 24943 sensep = (uint8_t *)resultp->sensep; 24944 pkt = resultp->pkt; 24945 actual_sense_length = resultp->actual_sense_length; 24946 24947 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24948 return (ENXIO); 24949 } 24950 24951 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24952 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 24953 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 24954 24955 /* Begin processing of the status and/or sense data */ 24956 if (pkt->pkt_reason != CMD_CMPLT) { 24957 /* Handle the incomplete packet */ 24958 sd_mhd_watch_incomplete(un, pkt); 24959 return (0); 24960 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 24961 if (*((unsigned char *)statusp) 24962 == STATUS_RESERVATION_CONFLICT) { 24963 /* 24964 * Handle a reservation conflict by panicking if 24965 * configured for failfast or by logging the conflict 24966 * and updating the reservation status 24967 */ 24968 mutex_enter(SD_MUTEX(un)); 24969 if ((un->un_resvd_status & SD_FAILFAST) && 24970 (sd_failfast_enable)) { 24971 sd_panic_for_res_conflict(un); 24972 /*NOTREACHED*/ 24973 } 24974 SD_INFO(SD_LOG_IOCTL_MHD, un, 24975 "sd_mhd_watch_cb: Reservation Conflict\n"); 24976 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 24977 mutex_exit(SD_MUTEX(un)); 24978 } 24979 } 24980 24981 if (sensep != NULL) { 24982 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 24983 mutex_enter(SD_MUTEX(un)); 24984 if ((scsi_sense_asc(sensep) == 24985 SD_SCSI_RESET_SENSE_CODE) && 24986 (un->un_resvd_status & SD_RESERVE)) { 24987 /* 24988 * The additional sense code indicates a power 24989 * on or bus device reset has occurred; update 24990 * the reservation status. 24991 */ 24992 un->un_resvd_status |= 24993 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24994 SD_INFO(SD_LOG_IOCTL_MHD, un, 24995 "sd_mhd_watch_cb: Lost Reservation\n"); 24996 } 24997 } else { 24998 return (0); 24999 } 25000 } else { 25001 mutex_enter(SD_MUTEX(un)); 25002 } 25003 25004 if ((un->un_resvd_status & SD_RESERVE) && 25005 (un->un_resvd_status & SD_LOST_RESERVE)) { 25006 if (un->un_resvd_status & SD_WANT_RESERVE) { 25007 /* 25008 * A reset occurred in between the last probe and this 25009 * one so if a timeout is pending cancel it. 25010 */ 25011 if (un->un_resvd_timeid) { 25012 timeout_id_t temp_id = un->un_resvd_timeid; 25013 un->un_resvd_timeid = NULL; 25014 mutex_exit(SD_MUTEX(un)); 25015 (void) untimeout(temp_id); 25016 mutex_enter(SD_MUTEX(un)); 25017 } 25018 un->un_resvd_status &= ~SD_WANT_RESERVE; 25019 } 25020 if (un->un_resvd_timeid == 0) { 25021 /* Schedule a timeout to handle the lost reservation */ 25022 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25023 (void *)dev, 25024 drv_usectohz(sd_reinstate_resv_delay)); 25025 } 25026 } 25027 mutex_exit(SD_MUTEX(un)); 25028 return (0); 25029 } 25030 25031 25032 /* 25033 * Function: sd_mhd_watch_incomplete() 25034 * 25035 * Description: This function is used to find out why a scsi pkt sent by the 25036 * scsi watch facility was not completed. Under some scenarios this 25037 * routine will return. Otherwise it will send a bus reset to see 25038 * if the drive is still online. 25039 * 25040 * Arguments: un - driver soft state (unit) structure 25041 * pkt - incomplete scsi pkt 25042 */ 25043 25044 static void 25045 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25046 { 25047 int be_chatty; 25048 int perr; 25049 25050 ASSERT(pkt != NULL); 25051 ASSERT(un != NULL); 25052 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25053 perr = (pkt->pkt_statistics & STAT_PERR); 25054 25055 mutex_enter(SD_MUTEX(un)); 25056 if (un->un_state == SD_STATE_DUMPING) { 25057 mutex_exit(SD_MUTEX(un)); 25058 return; 25059 } 25060 25061 switch (pkt->pkt_reason) { 25062 case CMD_UNX_BUS_FREE: 25063 /* 25064 * If we had a parity error that caused the target to drop BSY*, 25065 * don't be chatty about it. 25066 */ 25067 if (perr && be_chatty) { 25068 be_chatty = 0; 25069 } 25070 break; 25071 case CMD_TAG_REJECT: 25072 /* 25073 * The SCSI-2 spec states that a tag reject will be sent by the 25074 * target if tagged queuing is not supported. A tag reject may 25075 * also be sent during certain initialization periods or to 25076 * control internal resources. For the latter case the target 25077 * may also return Queue Full. 25078 * 25079 * If this driver receives a tag reject from a target that is 25080 * going through an init period or controlling internal 25081 * resources tagged queuing will be disabled. This is a less 25082 * than optimal behavior but the driver is unable to determine 25083 * the target state and assumes tagged queueing is not supported 25084 */ 25085 pkt->pkt_flags = 0; 25086 un->un_tagflags = 0; 25087 25088 if (un->un_f_opt_queueing == TRUE) { 25089 un->un_throttle = min(un->un_throttle, 3); 25090 } else { 25091 un->un_throttle = 1; 25092 } 25093 mutex_exit(SD_MUTEX(un)); 25094 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25095 mutex_enter(SD_MUTEX(un)); 25096 break; 25097 case CMD_INCOMPLETE: 25098 /* 25099 * The transport stopped with an abnormal state, fallthrough and 25100 * reset the target and/or bus unless selection did not complete 25101 * (indicated by STATE_GOT_BUS) in which case we don't want to 25102 * go through a target/bus reset 25103 */ 25104 if (pkt->pkt_state == STATE_GOT_BUS) { 25105 break; 25106 } 25107 /*FALLTHROUGH*/ 25108 25109 case CMD_TIMEOUT: 25110 default: 25111 /* 25112 * The lun may still be running the command, so a lun reset 25113 * should be attempted. If the lun reset fails or cannot be 25114 * issued, than try a target reset. Lastly try a bus reset. 25115 */ 25116 if ((pkt->pkt_statistics & 25117 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25118 int reset_retval = 0; 25119 mutex_exit(SD_MUTEX(un)); 25120 if (un->un_f_allow_bus_device_reset == TRUE) { 25121 if (un->un_f_lun_reset_enabled == TRUE) { 25122 reset_retval = 25123 scsi_reset(SD_ADDRESS(un), 25124 RESET_LUN); 25125 } 25126 if (reset_retval == 0) { 25127 reset_retval = 25128 scsi_reset(SD_ADDRESS(un), 25129 RESET_TARGET); 25130 } 25131 } 25132 if (reset_retval == 0) { 25133 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25134 } 25135 mutex_enter(SD_MUTEX(un)); 25136 } 25137 break; 25138 } 25139 25140 /* A device/bus reset has occurred; update the reservation status. */ 25141 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25142 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25143 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25144 un->un_resvd_status |= 25145 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25146 SD_INFO(SD_LOG_IOCTL_MHD, un, 25147 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25148 } 25149 } 25150 25151 /* 25152 * The disk has been turned off; Update the device state. 25153 * 25154 * Note: Should we be offlining the disk here? 25155 */ 25156 if (pkt->pkt_state == STATE_GOT_BUS) { 25157 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25158 "Disk not responding to selection\n"); 25159 if (un->un_state != SD_STATE_OFFLINE) { 25160 New_state(un, SD_STATE_OFFLINE); 25161 } 25162 } else if (be_chatty) { 25163 /* 25164 * suppress messages if they are all the same pkt reason; 25165 * with TQ, many (up to 256) are returned with the same 25166 * pkt_reason 25167 */ 25168 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25169 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25170 "sd_mhd_watch_incomplete: " 25171 "SCSI transport failed: reason '%s'\n", 25172 scsi_rname(pkt->pkt_reason)); 25173 } 25174 } 25175 un->un_last_pkt_reason = pkt->pkt_reason; 25176 mutex_exit(SD_MUTEX(un)); 25177 } 25178 25179 25180 /* 25181 * Function: sd_sname() 25182 * 25183 * Description: This is a simple little routine to return a string containing 25184 * a printable description of command status byte for use in 25185 * logging. 25186 * 25187 * Arguments: status - pointer to a status byte 25188 * 25189 * Return Code: char * - string containing status description. 25190 */ 25191 25192 static char * 25193 sd_sname(uchar_t status) 25194 { 25195 switch (status & STATUS_MASK) { 25196 case STATUS_GOOD: 25197 return ("good status"); 25198 case STATUS_CHECK: 25199 return ("check condition"); 25200 case STATUS_MET: 25201 return ("condition met"); 25202 case STATUS_BUSY: 25203 return ("busy"); 25204 case STATUS_INTERMEDIATE: 25205 return ("intermediate"); 25206 case STATUS_INTERMEDIATE_MET: 25207 return ("intermediate - condition met"); 25208 case STATUS_RESERVATION_CONFLICT: 25209 return ("reservation_conflict"); 25210 case STATUS_TERMINATED: 25211 return ("command terminated"); 25212 case STATUS_QFULL: 25213 return ("queue full"); 25214 default: 25215 return ("<unknown status>"); 25216 } 25217 } 25218 25219 25220 /* 25221 * Function: sd_mhd_resvd_recover() 25222 * 25223 * Description: This function adds a reservation entry to the 25224 * sd_resv_reclaim_request list and signals the reservation 25225 * reclaim thread that there is work pending. If the reservation 25226 * reclaim thread has not been previously created this function 25227 * will kick it off. 25228 * 25229 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25230 * among multiple watches that share this callback function 25231 * 25232 * Context: This routine is called by timeout() and is run in interrupt 25233 * context. It must not sleep or call other functions which may 25234 * sleep. 25235 */ 25236 25237 static void 25238 sd_mhd_resvd_recover(void *arg) 25239 { 25240 dev_t dev = (dev_t)arg; 25241 struct sd_lun *un; 25242 struct sd_thr_request *sd_treq = NULL; 25243 struct sd_thr_request *sd_cur = NULL; 25244 struct sd_thr_request *sd_prev = NULL; 25245 int already_there = 0; 25246 25247 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25248 return; 25249 } 25250 25251 mutex_enter(SD_MUTEX(un)); 25252 un->un_resvd_timeid = NULL; 25253 if (un->un_resvd_status & SD_WANT_RESERVE) { 25254 /* 25255 * There was a reset so don't issue the reserve, allow the 25256 * sd_mhd_watch_cb callback function to notice this and 25257 * reschedule the timeout for reservation. 25258 */ 25259 mutex_exit(SD_MUTEX(un)); 25260 return; 25261 } 25262 mutex_exit(SD_MUTEX(un)); 25263 25264 /* 25265 * Add this device to the sd_resv_reclaim_request list and the 25266 * sd_resv_reclaim_thread should take care of the rest. 25267 * 25268 * Note: We can't sleep in this context so if the memory allocation 25269 * fails allow the sd_mhd_watch_cb callback function to notice this and 25270 * reschedule the timeout for reservation. (4378460) 25271 */ 25272 sd_treq = (struct sd_thr_request *) 25273 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25274 if (sd_treq == NULL) { 25275 return; 25276 } 25277 25278 sd_treq->sd_thr_req_next = NULL; 25279 sd_treq->dev = dev; 25280 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25281 if (sd_tr.srq_thr_req_head == NULL) { 25282 sd_tr.srq_thr_req_head = sd_treq; 25283 } else { 25284 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25285 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25286 if (sd_cur->dev == dev) { 25287 /* 25288 * already in Queue so don't log 25289 * another request for the device 25290 */ 25291 already_there = 1; 25292 break; 25293 } 25294 sd_prev = sd_cur; 25295 } 25296 if (!already_there) { 25297 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25298 "logging request for %lx\n", dev); 25299 sd_prev->sd_thr_req_next = sd_treq; 25300 } else { 25301 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25302 } 25303 } 25304 25305 /* 25306 * Create a kernel thread to do the reservation reclaim and free up this 25307 * thread. We cannot block this thread while we go away to do the 25308 * reservation reclaim 25309 */ 25310 if (sd_tr.srq_resv_reclaim_thread == NULL) 25311 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25312 sd_resv_reclaim_thread, NULL, 25313 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25314 25315 /* Tell the reservation reclaim thread that it has work to do */ 25316 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25317 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25318 } 25319 25320 /* 25321 * Function: sd_resv_reclaim_thread() 25322 * 25323 * Description: This function implements the reservation reclaim operations 25324 * 25325 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25326 * among multiple watches that share this callback function 25327 */ 25328 25329 static void 25330 sd_resv_reclaim_thread() 25331 { 25332 struct sd_lun *un; 25333 struct sd_thr_request *sd_mhreq; 25334 25335 /* Wait for work */ 25336 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25337 if (sd_tr.srq_thr_req_head == NULL) { 25338 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25339 &sd_tr.srq_resv_reclaim_mutex); 25340 } 25341 25342 /* Loop while we have work */ 25343 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25344 un = ddi_get_soft_state(sd_state, 25345 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25346 if (un == NULL) { 25347 /* 25348 * softstate structure is NULL so just 25349 * dequeue the request and continue 25350 */ 25351 sd_tr.srq_thr_req_head = 25352 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25353 kmem_free(sd_tr.srq_thr_cur_req, 25354 sizeof (struct sd_thr_request)); 25355 continue; 25356 } 25357 25358 /* dequeue the request */ 25359 sd_mhreq = sd_tr.srq_thr_cur_req; 25360 sd_tr.srq_thr_req_head = 25361 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25362 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25363 25364 /* 25365 * Reclaim reservation only if SD_RESERVE is still set. There 25366 * may have been a call to MHIOCRELEASE before we got here. 25367 */ 25368 mutex_enter(SD_MUTEX(un)); 25369 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25370 /* 25371 * Note: The SD_LOST_RESERVE flag is cleared before 25372 * reclaiming the reservation. If this is done after the 25373 * call to sd_reserve_release a reservation loss in the 25374 * window between pkt completion of reserve cmd and 25375 * mutex_enter below may not be recognized 25376 */ 25377 un->un_resvd_status &= ~SD_LOST_RESERVE; 25378 mutex_exit(SD_MUTEX(un)); 25379 25380 if (sd_reserve_release(sd_mhreq->dev, 25381 SD_RESERVE) == 0) { 25382 mutex_enter(SD_MUTEX(un)); 25383 un->un_resvd_status |= SD_RESERVE; 25384 mutex_exit(SD_MUTEX(un)); 25385 SD_INFO(SD_LOG_IOCTL_MHD, un, 25386 "sd_resv_reclaim_thread: " 25387 "Reservation Recovered\n"); 25388 } else { 25389 mutex_enter(SD_MUTEX(un)); 25390 un->un_resvd_status |= SD_LOST_RESERVE; 25391 mutex_exit(SD_MUTEX(un)); 25392 SD_INFO(SD_LOG_IOCTL_MHD, un, 25393 "sd_resv_reclaim_thread: Failed " 25394 "Reservation Recovery\n"); 25395 } 25396 } else { 25397 mutex_exit(SD_MUTEX(un)); 25398 } 25399 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25400 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25401 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25402 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25403 /* 25404 * wakeup the destroy thread if anyone is waiting on 25405 * us to complete. 25406 */ 25407 cv_signal(&sd_tr.srq_inprocess_cv); 25408 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25409 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25410 } 25411 25412 /* 25413 * cleanup the sd_tr structure now that this thread will not exist 25414 */ 25415 ASSERT(sd_tr.srq_thr_req_head == NULL); 25416 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25417 sd_tr.srq_resv_reclaim_thread = NULL; 25418 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25419 thread_exit(); 25420 } 25421 25422 25423 /* 25424 * Function: sd_rmv_resv_reclaim_req() 25425 * 25426 * Description: This function removes any pending reservation reclaim requests 25427 * for the specified device. 25428 * 25429 * Arguments: dev - the device 'dev_t' 25430 */ 25431 25432 static void 25433 sd_rmv_resv_reclaim_req(dev_t dev) 25434 { 25435 struct sd_thr_request *sd_mhreq; 25436 struct sd_thr_request *sd_prev; 25437 25438 /* Remove a reservation reclaim request from the list */ 25439 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25440 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25441 /* 25442 * We are attempting to reinstate reservation for 25443 * this device. We wait for sd_reserve_release() 25444 * to return before we return. 25445 */ 25446 cv_wait(&sd_tr.srq_inprocess_cv, 25447 &sd_tr.srq_resv_reclaim_mutex); 25448 } else { 25449 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25450 if (sd_mhreq && sd_mhreq->dev == dev) { 25451 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25452 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25453 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25454 return; 25455 } 25456 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25457 if (sd_mhreq && sd_mhreq->dev == dev) { 25458 break; 25459 } 25460 sd_prev = sd_mhreq; 25461 } 25462 if (sd_mhreq != NULL) { 25463 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25464 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25465 } 25466 } 25467 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25468 } 25469 25470 25471 /* 25472 * Function: sd_mhd_reset_notify_cb() 25473 * 25474 * Description: This is a call back function for scsi_reset_notify. This 25475 * function updates the softstate reserved status and logs the 25476 * reset. The driver scsi watch facility callback function 25477 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25478 * will reclaim the reservation. 25479 * 25480 * Arguments: arg - driver soft state (unit) structure 25481 */ 25482 25483 static void 25484 sd_mhd_reset_notify_cb(caddr_t arg) 25485 { 25486 struct sd_lun *un = (struct sd_lun *)arg; 25487 25488 mutex_enter(SD_MUTEX(un)); 25489 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25490 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25491 SD_INFO(SD_LOG_IOCTL_MHD, un, 25492 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25493 } 25494 mutex_exit(SD_MUTEX(un)); 25495 } 25496 25497 25498 /* 25499 * Function: sd_take_ownership() 25500 * 25501 * Description: This routine implements an algorithm to achieve a stable 25502 * reservation on disks which don't implement priority reserve, 25503 * and makes sure that other host lose re-reservation attempts. 25504 * This algorithm contains of a loop that keeps issuing the RESERVE 25505 * for some period of time (min_ownership_delay, default 6 seconds) 25506 * During that loop, it looks to see if there has been a bus device 25507 * reset or bus reset (both of which cause an existing reservation 25508 * to be lost). If the reservation is lost issue RESERVE until a 25509 * period of min_ownership_delay with no resets has gone by, or 25510 * until max_ownership_delay has expired. This loop ensures that 25511 * the host really did manage to reserve the device, in spite of 25512 * resets. The looping for min_ownership_delay (default six 25513 * seconds) is important to early generation clustering products, 25514 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25515 * MHIOCENFAILFAST periodic timer of two seconds. By having 25516 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25517 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25518 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25519 * have already noticed, via the MHIOCENFAILFAST polling, that it 25520 * no longer "owns" the disk and will have panicked itself. Thus, 25521 * the host issuing the MHIOCTKOWN is assured (with timing 25522 * dependencies) that by the time it actually starts to use the 25523 * disk for real work, the old owner is no longer accessing it. 25524 * 25525 * min_ownership_delay is the minimum amount of time for which the 25526 * disk must be reserved continuously devoid of resets before the 25527 * MHIOCTKOWN ioctl will return success. 25528 * 25529 * max_ownership_delay indicates the amount of time by which the 25530 * take ownership should succeed or timeout with an error. 25531 * 25532 * Arguments: dev - the device 'dev_t' 25533 * *p - struct containing timing info. 25534 * 25535 * Return Code: 0 for success or error code 25536 */ 25537 25538 static int 25539 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25540 { 25541 struct sd_lun *un; 25542 int rval; 25543 int err; 25544 int reservation_count = 0; 25545 int min_ownership_delay = 6000000; /* in usec */ 25546 int max_ownership_delay = 30000000; /* in usec */ 25547 clock_t start_time; /* starting time of this algorithm */ 25548 clock_t end_time; /* time limit for giving up */ 25549 clock_t ownership_time; /* time limit for stable ownership */ 25550 clock_t current_time; 25551 clock_t previous_current_time; 25552 25553 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25554 return (ENXIO); 25555 } 25556 25557 /* 25558 * Attempt a device reservation. A priority reservation is requested. 25559 */ 25560 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25561 != SD_SUCCESS) { 25562 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25563 "sd_take_ownership: return(1)=%d\n", rval); 25564 return (rval); 25565 } 25566 25567 /* Update the softstate reserved status to indicate the reservation */ 25568 mutex_enter(SD_MUTEX(un)); 25569 un->un_resvd_status |= SD_RESERVE; 25570 un->un_resvd_status &= 25571 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25572 mutex_exit(SD_MUTEX(un)); 25573 25574 if (p != NULL) { 25575 if (p->min_ownership_delay != 0) { 25576 min_ownership_delay = p->min_ownership_delay * 1000; 25577 } 25578 if (p->max_ownership_delay != 0) { 25579 max_ownership_delay = p->max_ownership_delay * 1000; 25580 } 25581 } 25582 SD_INFO(SD_LOG_IOCTL_MHD, un, 25583 "sd_take_ownership: min, max delays: %d, %d\n", 25584 min_ownership_delay, max_ownership_delay); 25585 25586 start_time = ddi_get_lbolt(); 25587 current_time = start_time; 25588 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25589 end_time = start_time + drv_usectohz(max_ownership_delay); 25590 25591 while (current_time - end_time < 0) { 25592 delay(drv_usectohz(500000)); 25593 25594 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25595 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25596 mutex_enter(SD_MUTEX(un)); 25597 rval = (un->un_resvd_status & 25598 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25599 mutex_exit(SD_MUTEX(un)); 25600 break; 25601 } 25602 } 25603 previous_current_time = current_time; 25604 current_time = ddi_get_lbolt(); 25605 mutex_enter(SD_MUTEX(un)); 25606 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25607 ownership_time = ddi_get_lbolt() + 25608 drv_usectohz(min_ownership_delay); 25609 reservation_count = 0; 25610 } else { 25611 reservation_count++; 25612 } 25613 un->un_resvd_status |= SD_RESERVE; 25614 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25615 mutex_exit(SD_MUTEX(un)); 25616 25617 SD_INFO(SD_LOG_IOCTL_MHD, un, 25618 "sd_take_ownership: ticks for loop iteration=%ld, " 25619 "reservation=%s\n", (current_time - previous_current_time), 25620 reservation_count ? "ok" : "reclaimed"); 25621 25622 if (current_time - ownership_time >= 0 && 25623 reservation_count >= 4) { 25624 rval = 0; /* Achieved a stable ownership */ 25625 break; 25626 } 25627 if (current_time - end_time >= 0) { 25628 rval = EACCES; /* No ownership in max possible time */ 25629 break; 25630 } 25631 } 25632 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25633 "sd_take_ownership: return(2)=%d\n", rval); 25634 return (rval); 25635 } 25636 25637 25638 /* 25639 * Function: sd_reserve_release() 25640 * 25641 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25642 * PRIORITY RESERVE commands based on a user specified command type 25643 * 25644 * Arguments: dev - the device 'dev_t' 25645 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25646 * SD_RESERVE, SD_RELEASE 25647 * 25648 * Return Code: 0 or Error Code 25649 */ 25650 25651 static int 25652 sd_reserve_release(dev_t dev, int cmd) 25653 { 25654 struct uscsi_cmd *com = NULL; 25655 struct sd_lun *un = NULL; 25656 char cdb[CDB_GROUP0]; 25657 int rval; 25658 25659 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25660 (cmd == SD_PRIORITY_RESERVE)); 25661 25662 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25663 return (ENXIO); 25664 } 25665 25666 /* instantiate and initialize the command and cdb */ 25667 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25668 bzero(cdb, CDB_GROUP0); 25669 com->uscsi_flags = USCSI_SILENT; 25670 com->uscsi_timeout = un->un_reserve_release_time; 25671 com->uscsi_cdblen = CDB_GROUP0; 25672 com->uscsi_cdb = cdb; 25673 if (cmd == SD_RELEASE) { 25674 cdb[0] = SCMD_RELEASE; 25675 } else { 25676 cdb[0] = SCMD_RESERVE; 25677 } 25678 25679 /* Send the command. */ 25680 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25681 SD_PATH_STANDARD); 25682 25683 /* 25684 * "break" a reservation that is held by another host, by issuing a 25685 * reset if priority reserve is desired, and we could not get the 25686 * device. 25687 */ 25688 if ((cmd == SD_PRIORITY_RESERVE) && 25689 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25690 /* 25691 * First try to reset the LUN. If we cannot, then try a target 25692 * reset, followed by a bus reset if the target reset fails. 25693 */ 25694 int reset_retval = 0; 25695 if (un->un_f_lun_reset_enabled == TRUE) { 25696 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25697 } 25698 if (reset_retval == 0) { 25699 /* The LUN reset either failed or was not issued */ 25700 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25701 } 25702 if ((reset_retval == 0) && 25703 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25704 rval = EIO; 25705 kmem_free(com, sizeof (*com)); 25706 return (rval); 25707 } 25708 25709 bzero(com, sizeof (struct uscsi_cmd)); 25710 com->uscsi_flags = USCSI_SILENT; 25711 com->uscsi_cdb = cdb; 25712 com->uscsi_cdblen = CDB_GROUP0; 25713 com->uscsi_timeout = 5; 25714 25715 /* 25716 * Reissue the last reserve command, this time without request 25717 * sense. Assume that it is just a regular reserve command. 25718 */ 25719 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25720 SD_PATH_STANDARD); 25721 } 25722 25723 /* Return an error if still getting a reservation conflict. */ 25724 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25725 rval = EACCES; 25726 } 25727 25728 kmem_free(com, sizeof (*com)); 25729 return (rval); 25730 } 25731 25732 25733 #define SD_NDUMP_RETRIES 12 25734 /* 25735 * System Crash Dump routine 25736 */ 25737 25738 static int 25739 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25740 { 25741 int instance; 25742 int partition; 25743 int i; 25744 int err; 25745 struct sd_lun *un; 25746 struct scsi_pkt *wr_pktp; 25747 struct buf *wr_bp; 25748 struct buf wr_buf; 25749 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25750 daddr_t tgt_blkno; /* rmw - blkno for target */ 25751 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25752 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25753 size_t io_start_offset; 25754 int doing_rmw = FALSE; 25755 int rval; 25756 ssize_t dma_resid; 25757 daddr_t oblkno; 25758 diskaddr_t nblks = 0; 25759 diskaddr_t start_block; 25760 25761 instance = SDUNIT(dev); 25762 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25763 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 25764 return (ENXIO); 25765 } 25766 25767 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25768 25769 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25770 25771 partition = SDPART(dev); 25772 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25773 25774 if (!(NOT_DEVBSIZE(un))) { 25775 int secmask = 0; 25776 int blknomask = 0; 25777 25778 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 25779 secmask = un->un_tgt_blocksize - 1; 25780 25781 if (blkno & blknomask) { 25782 SD_TRACE(SD_LOG_DUMP, un, 25783 "sddump: dump start block not modulo %d\n", 25784 un->un_tgt_blocksize); 25785 return (EINVAL); 25786 } 25787 25788 if ((nblk * DEV_BSIZE) & secmask) { 25789 SD_TRACE(SD_LOG_DUMP, un, 25790 "sddump: dump length not modulo %d\n", 25791 un->un_tgt_blocksize); 25792 return (EINVAL); 25793 } 25794 25795 } 25796 25797 /* Validate blocks to dump at against partition size. */ 25798 25799 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 25800 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 25801 25802 if (NOT_DEVBSIZE(un)) { 25803 if ((blkno + nblk) > nblks) { 25804 SD_TRACE(SD_LOG_DUMP, un, 25805 "sddump: dump range larger than partition: " 25806 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25807 blkno, nblk, nblks); 25808 return (EINVAL); 25809 } 25810 } else { 25811 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) + 25812 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) { 25813 SD_TRACE(SD_LOG_DUMP, un, 25814 "sddump: dump range larger than partition: " 25815 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25816 blkno, nblk, nblks); 25817 return (EINVAL); 25818 } 25819 } 25820 25821 mutex_enter(&un->un_pm_mutex); 25822 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25823 struct scsi_pkt *start_pktp; 25824 25825 mutex_exit(&un->un_pm_mutex); 25826 25827 /* 25828 * use pm framework to power on HBA 1st 25829 */ 25830 (void) pm_raise_power(SD_DEVINFO(un), 0, 25831 SD_PM_STATE_ACTIVE(un)); 25832 25833 /* 25834 * Dump no long uses sdpower to power on a device, it's 25835 * in-line here so it can be done in polled mode. 25836 */ 25837 25838 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25839 25840 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25841 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25842 25843 if (start_pktp == NULL) { 25844 /* We were not given a SCSI packet, fail. */ 25845 return (EIO); 25846 } 25847 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25848 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25849 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25850 start_pktp->pkt_flags = FLAG_NOINTR; 25851 25852 mutex_enter(SD_MUTEX(un)); 25853 SD_FILL_SCSI1_LUN(un, start_pktp); 25854 mutex_exit(SD_MUTEX(un)); 25855 /* 25856 * Scsi_poll returns 0 (success) if the command completes and 25857 * the status block is STATUS_GOOD. 25858 */ 25859 if (sd_scsi_poll(un, start_pktp) != 0) { 25860 scsi_destroy_pkt(start_pktp); 25861 return (EIO); 25862 } 25863 scsi_destroy_pkt(start_pktp); 25864 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un), 25865 SD_PM_STATE_CHANGE); 25866 } else { 25867 mutex_exit(&un->un_pm_mutex); 25868 } 25869 25870 mutex_enter(SD_MUTEX(un)); 25871 un->un_throttle = 0; 25872 25873 /* 25874 * The first time through, reset the specific target device. 25875 * However, when cpr calls sddump we know that sd is in a 25876 * a good state so no bus reset is required. 25877 * Clear sense data via Request Sense cmd. 25878 * In sddump we don't care about allow_bus_device_reset anymore 25879 */ 25880 25881 if ((un->un_state != SD_STATE_SUSPENDED) && 25882 (un->un_state != SD_STATE_DUMPING)) { 25883 25884 New_state(un, SD_STATE_DUMPING); 25885 25886 if (un->un_f_is_fibre == FALSE) { 25887 mutex_exit(SD_MUTEX(un)); 25888 /* 25889 * Attempt a bus reset for parallel scsi. 25890 * 25891 * Note: A bus reset is required because on some host 25892 * systems (i.e. E420R) a bus device reset is 25893 * insufficient to reset the state of the target. 25894 * 25895 * Note: Don't issue the reset for fibre-channel, 25896 * because this tends to hang the bus (loop) for 25897 * too long while everyone is logging out and in 25898 * and the deadman timer for dumping will fire 25899 * before the dump is complete. 25900 */ 25901 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25902 mutex_enter(SD_MUTEX(un)); 25903 Restore_state(un); 25904 mutex_exit(SD_MUTEX(un)); 25905 return (EIO); 25906 } 25907 25908 /* Delay to give the device some recovery time. */ 25909 drv_usecwait(10000); 25910 25911 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25912 SD_INFO(SD_LOG_DUMP, un, 25913 "sddump: sd_send_polled_RQS failed\n"); 25914 } 25915 mutex_enter(SD_MUTEX(un)); 25916 } 25917 } 25918 25919 /* 25920 * Convert the partition-relative block number to a 25921 * disk physical block number. 25922 */ 25923 if (NOT_DEVBSIZE(un)) { 25924 blkno += start_block; 25925 } else { 25926 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE); 25927 blkno += start_block; 25928 } 25929 25930 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25931 25932 25933 /* 25934 * Check if the device has a non-512 block size. 25935 */ 25936 wr_bp = NULL; 25937 if (NOT_DEVBSIZE(un)) { 25938 tgt_byte_offset = blkno * un->un_sys_blocksize; 25939 tgt_byte_count = nblk * un->un_sys_blocksize; 25940 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25941 (tgt_byte_count % un->un_tgt_blocksize)) { 25942 doing_rmw = TRUE; 25943 /* 25944 * Calculate the block number and number of block 25945 * in terms of the media block size. 25946 */ 25947 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25948 tgt_nblk = 25949 ((tgt_byte_offset + tgt_byte_count + 25950 (un->un_tgt_blocksize - 1)) / 25951 un->un_tgt_blocksize) - tgt_blkno; 25952 25953 /* 25954 * Invoke the routine which is going to do read part 25955 * of read-modify-write. 25956 * Note that this routine returns a pointer to 25957 * a valid bp in wr_bp. 25958 */ 25959 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25960 &wr_bp); 25961 if (err) { 25962 mutex_exit(SD_MUTEX(un)); 25963 return (err); 25964 } 25965 /* 25966 * Offset is being calculated as - 25967 * (original block # * system block size) - 25968 * (new block # * target block size) 25969 */ 25970 io_start_offset = 25971 ((uint64_t)(blkno * un->un_sys_blocksize)) - 25972 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 25973 25974 ASSERT((io_start_offset >= 0) && 25975 (io_start_offset < un->un_tgt_blocksize)); 25976 /* 25977 * Do the modify portion of read modify write. 25978 */ 25979 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 25980 (size_t)nblk * un->un_sys_blocksize); 25981 } else { 25982 doing_rmw = FALSE; 25983 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25984 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 25985 } 25986 25987 /* Convert blkno and nblk to target blocks */ 25988 blkno = tgt_blkno; 25989 nblk = tgt_nblk; 25990 } else { 25991 wr_bp = &wr_buf; 25992 bzero(wr_bp, sizeof (struct buf)); 25993 wr_bp->b_flags = B_BUSY; 25994 wr_bp->b_un.b_addr = addr; 25995 wr_bp->b_bcount = nblk << DEV_BSHIFT; 25996 wr_bp->b_resid = 0; 25997 } 25998 25999 mutex_exit(SD_MUTEX(un)); 26000 26001 /* 26002 * Obtain a SCSI packet for the write command. 26003 * It should be safe to call the allocator here without 26004 * worrying about being locked for DVMA mapping because 26005 * the address we're passed is already a DVMA mapping 26006 * 26007 * We are also not going to worry about semaphore ownership 26008 * in the dump buffer. Dumping is single threaded at present. 26009 */ 26010 26011 wr_pktp = NULL; 26012 26013 dma_resid = wr_bp->b_bcount; 26014 oblkno = blkno; 26015 26016 if (!(NOT_DEVBSIZE(un))) { 26017 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE); 26018 } 26019 26020 while (dma_resid != 0) { 26021 26022 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26023 wr_bp->b_flags &= ~B_ERROR; 26024 26025 if (un->un_partial_dma_supported == 1) { 26026 blkno = oblkno + 26027 ((wr_bp->b_bcount - dma_resid) / 26028 un->un_tgt_blocksize); 26029 nblk = dma_resid / un->un_tgt_blocksize; 26030 26031 if (wr_pktp) { 26032 /* 26033 * Partial DMA transfers after initial transfer 26034 */ 26035 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26036 blkno, nblk); 26037 } else { 26038 /* Initial transfer */ 26039 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26040 un->un_pkt_flags, NULL_FUNC, NULL, 26041 blkno, nblk); 26042 } 26043 } else { 26044 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26045 0, NULL_FUNC, NULL, blkno, nblk); 26046 } 26047 26048 if (rval == 0) { 26049 /* We were given a SCSI packet, continue. */ 26050 break; 26051 } 26052 26053 if (i == 0) { 26054 if (wr_bp->b_flags & B_ERROR) { 26055 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26056 "no resources for dumping; " 26057 "error code: 0x%x, retrying", 26058 geterror(wr_bp)); 26059 } else { 26060 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26061 "no resources for dumping; retrying"); 26062 } 26063 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26064 if (wr_bp->b_flags & B_ERROR) { 26065 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26066 "no resources for dumping; error code: " 26067 "0x%x, retrying\n", geterror(wr_bp)); 26068 } 26069 } else { 26070 if (wr_bp->b_flags & B_ERROR) { 26071 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26072 "no resources for dumping; " 26073 "error code: 0x%x, retries failed, " 26074 "giving up.\n", geterror(wr_bp)); 26075 } else { 26076 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26077 "no resources for dumping; " 26078 "retries failed, giving up.\n"); 26079 } 26080 mutex_enter(SD_MUTEX(un)); 26081 Restore_state(un); 26082 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26083 mutex_exit(SD_MUTEX(un)); 26084 scsi_free_consistent_buf(wr_bp); 26085 } else { 26086 mutex_exit(SD_MUTEX(un)); 26087 } 26088 return (EIO); 26089 } 26090 drv_usecwait(10000); 26091 } 26092 26093 if (un->un_partial_dma_supported == 1) { 26094 /* 26095 * save the resid from PARTIAL_DMA 26096 */ 26097 dma_resid = wr_pktp->pkt_resid; 26098 if (dma_resid != 0) 26099 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26100 wr_pktp->pkt_resid = 0; 26101 } else { 26102 dma_resid = 0; 26103 } 26104 26105 /* SunBug 1222170 */ 26106 wr_pktp->pkt_flags = FLAG_NOINTR; 26107 26108 err = EIO; 26109 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26110 26111 /* 26112 * Scsi_poll returns 0 (success) if the command completes and 26113 * the status block is STATUS_GOOD. We should only check 26114 * errors if this condition is not true. Even then we should 26115 * send our own request sense packet only if we have a check 26116 * condition and auto request sense has not been performed by 26117 * the hba. 26118 */ 26119 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26120 26121 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26122 (wr_pktp->pkt_resid == 0)) { 26123 err = SD_SUCCESS; 26124 break; 26125 } 26126 26127 /* 26128 * Check CMD_DEV_GONE 1st, give up if device is gone. 26129 */ 26130 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26131 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26132 "Error while dumping state...Device is gone\n"); 26133 break; 26134 } 26135 26136 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26137 SD_INFO(SD_LOG_DUMP, un, 26138 "sddump: write failed with CHECK, try # %d\n", i); 26139 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26140 (void) sd_send_polled_RQS(un); 26141 } 26142 26143 continue; 26144 } 26145 26146 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26147 int reset_retval = 0; 26148 26149 SD_INFO(SD_LOG_DUMP, un, 26150 "sddump: write failed with BUSY, try # %d\n", i); 26151 26152 if (un->un_f_lun_reset_enabled == TRUE) { 26153 reset_retval = scsi_reset(SD_ADDRESS(un), 26154 RESET_LUN); 26155 } 26156 if (reset_retval == 0) { 26157 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26158 } 26159 (void) sd_send_polled_RQS(un); 26160 26161 } else { 26162 SD_INFO(SD_LOG_DUMP, un, 26163 "sddump: write failed with 0x%x, try # %d\n", 26164 SD_GET_PKT_STATUS(wr_pktp), i); 26165 mutex_enter(SD_MUTEX(un)); 26166 sd_reset_target(un, wr_pktp); 26167 mutex_exit(SD_MUTEX(un)); 26168 } 26169 26170 /* 26171 * If we are not getting anywhere with lun/target resets, 26172 * let's reset the bus. 26173 */ 26174 if (i == SD_NDUMP_RETRIES/2) { 26175 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26176 (void) sd_send_polled_RQS(un); 26177 } 26178 } 26179 } 26180 26181 scsi_destroy_pkt(wr_pktp); 26182 mutex_enter(SD_MUTEX(un)); 26183 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26184 mutex_exit(SD_MUTEX(un)); 26185 scsi_free_consistent_buf(wr_bp); 26186 } else { 26187 mutex_exit(SD_MUTEX(un)); 26188 } 26189 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26190 return (err); 26191 } 26192 26193 /* 26194 * Function: sd_scsi_poll() 26195 * 26196 * Description: This is a wrapper for the scsi_poll call. 26197 * 26198 * Arguments: sd_lun - The unit structure 26199 * scsi_pkt - The scsi packet being sent to the device. 26200 * 26201 * Return Code: 0 - Command completed successfully with good status 26202 * -1 - Command failed. This could indicate a check condition 26203 * or other status value requiring recovery action. 26204 * 26205 * NOTE: This code is only called off sddump(). 26206 */ 26207 26208 static int 26209 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26210 { 26211 int status; 26212 26213 ASSERT(un != NULL); 26214 ASSERT(!mutex_owned(SD_MUTEX(un))); 26215 ASSERT(pktp != NULL); 26216 26217 status = SD_SUCCESS; 26218 26219 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26220 pktp->pkt_flags |= un->un_tagflags; 26221 pktp->pkt_flags &= ~FLAG_NODISCON; 26222 } 26223 26224 status = sd_ddi_scsi_poll(pktp); 26225 /* 26226 * Scsi_poll returns 0 (success) if the command completes and the 26227 * status block is STATUS_GOOD. We should only check errors if this 26228 * condition is not true. Even then we should send our own request 26229 * sense packet only if we have a check condition and auto 26230 * request sense has not been performed by the hba. 26231 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26232 */ 26233 if ((status != SD_SUCCESS) && 26234 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26235 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26236 (pktp->pkt_reason != CMD_DEV_GONE)) 26237 (void) sd_send_polled_RQS(un); 26238 26239 return (status); 26240 } 26241 26242 /* 26243 * Function: sd_send_polled_RQS() 26244 * 26245 * Description: This sends the request sense command to a device. 26246 * 26247 * Arguments: sd_lun - The unit structure 26248 * 26249 * Return Code: 0 - Command completed successfully with good status 26250 * -1 - Command failed. 26251 * 26252 */ 26253 26254 static int 26255 sd_send_polled_RQS(struct sd_lun *un) 26256 { 26257 int ret_val; 26258 struct scsi_pkt *rqs_pktp; 26259 struct buf *rqs_bp; 26260 26261 ASSERT(un != NULL); 26262 ASSERT(!mutex_owned(SD_MUTEX(un))); 26263 26264 ret_val = SD_SUCCESS; 26265 26266 rqs_pktp = un->un_rqs_pktp; 26267 rqs_bp = un->un_rqs_bp; 26268 26269 mutex_enter(SD_MUTEX(un)); 26270 26271 if (un->un_sense_isbusy) { 26272 ret_val = SD_FAILURE; 26273 mutex_exit(SD_MUTEX(un)); 26274 return (ret_val); 26275 } 26276 26277 /* 26278 * If the request sense buffer (and packet) is not in use, 26279 * let's set the un_sense_isbusy and send our packet 26280 */ 26281 un->un_sense_isbusy = 1; 26282 rqs_pktp->pkt_resid = 0; 26283 rqs_pktp->pkt_reason = 0; 26284 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26285 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26286 26287 mutex_exit(SD_MUTEX(un)); 26288 26289 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26290 " 0x%p\n", rqs_bp->b_un.b_addr); 26291 26292 /* 26293 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26294 * axle - it has a call into us! 26295 */ 26296 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26297 SD_INFO(SD_LOG_COMMON, un, 26298 "sd_send_polled_RQS: RQS failed\n"); 26299 } 26300 26301 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26302 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26303 26304 mutex_enter(SD_MUTEX(un)); 26305 un->un_sense_isbusy = 0; 26306 mutex_exit(SD_MUTEX(un)); 26307 26308 return (ret_val); 26309 } 26310 26311 /* 26312 * Defines needed for localized version of the scsi_poll routine. 26313 */ 26314 #define CSEC 10000 /* usecs */ 26315 #define SEC_TO_CSEC (1000000/CSEC) 26316 26317 /* 26318 * Function: sd_ddi_scsi_poll() 26319 * 26320 * Description: Localized version of the scsi_poll routine. The purpose is to 26321 * send a scsi_pkt to a device as a polled command. This version 26322 * is to ensure more robust handling of transport errors. 26323 * Specifically this routine cures not ready, coming ready 26324 * transition for power up and reset of sonoma's. This can take 26325 * up to 45 seconds for power-on and 20 seconds for reset of a 26326 * sonoma lun. 26327 * 26328 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26329 * 26330 * Return Code: 0 - Command completed successfully with good status 26331 * -1 - Command failed. 26332 * 26333 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 26334 * be fixed (removing this code), we need to determine how to handle the 26335 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 26336 * 26337 * NOTE: This code is only called off sddump(). 26338 */ 26339 static int 26340 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26341 { 26342 int rval = -1; 26343 int savef; 26344 long savet; 26345 void (*savec)(); 26346 int timeout; 26347 int busy_count; 26348 int poll_delay; 26349 int rc; 26350 uint8_t *sensep; 26351 struct scsi_arq_status *arqstat; 26352 extern int do_polled_io; 26353 26354 ASSERT(pkt->pkt_scbp); 26355 26356 /* 26357 * save old flags.. 26358 */ 26359 savef = pkt->pkt_flags; 26360 savec = pkt->pkt_comp; 26361 savet = pkt->pkt_time; 26362 26363 pkt->pkt_flags |= FLAG_NOINTR; 26364 26365 /* 26366 * XXX there is nothing in the SCSA spec that states that we should not 26367 * do a callback for polled cmds; however, removing this will break sd 26368 * and probably other target drivers 26369 */ 26370 pkt->pkt_comp = NULL; 26371 26372 /* 26373 * we don't like a polled command without timeout. 26374 * 60 seconds seems long enough. 26375 */ 26376 if (pkt->pkt_time == 0) 26377 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26378 26379 /* 26380 * Send polled cmd. 26381 * 26382 * We do some error recovery for various errors. Tran_busy, 26383 * queue full, and non-dispatched commands are retried every 10 msec. 26384 * as they are typically transient failures. Busy status and Not 26385 * Ready are retried every second as this status takes a while to 26386 * change. 26387 */ 26388 timeout = pkt->pkt_time * SEC_TO_CSEC; 26389 26390 for (busy_count = 0; busy_count < timeout; busy_count++) { 26391 /* 26392 * Initialize pkt status variables. 26393 */ 26394 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26395 26396 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26397 if (rc != TRAN_BUSY) { 26398 /* Transport failed - give up. */ 26399 break; 26400 } else { 26401 /* Transport busy - try again. */ 26402 poll_delay = 1 * CSEC; /* 10 msec. */ 26403 } 26404 } else { 26405 /* 26406 * Transport accepted - check pkt status. 26407 */ 26408 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26409 if ((pkt->pkt_reason == CMD_CMPLT) && 26410 (rc == STATUS_CHECK) && 26411 (pkt->pkt_state & STATE_ARQ_DONE)) { 26412 arqstat = 26413 (struct scsi_arq_status *)(pkt->pkt_scbp); 26414 sensep = (uint8_t *)&arqstat->sts_sensedata; 26415 } else { 26416 sensep = NULL; 26417 } 26418 26419 if ((pkt->pkt_reason == CMD_CMPLT) && 26420 (rc == STATUS_GOOD)) { 26421 /* No error - we're done */ 26422 rval = 0; 26423 break; 26424 26425 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26426 /* Lost connection - give up */ 26427 break; 26428 26429 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26430 (pkt->pkt_state == 0)) { 26431 /* Pkt not dispatched - try again. */ 26432 poll_delay = 1 * CSEC; /* 10 msec. */ 26433 26434 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26435 (rc == STATUS_QFULL)) { 26436 /* Queue full - try again. */ 26437 poll_delay = 1 * CSEC; /* 10 msec. */ 26438 26439 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26440 (rc == STATUS_BUSY)) { 26441 /* Busy - try again. */ 26442 poll_delay = 100 * CSEC; /* 1 sec. */ 26443 busy_count += (SEC_TO_CSEC - 1); 26444 26445 } else if ((sensep != NULL) && 26446 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 26447 /* 26448 * Unit Attention - try again. 26449 * Pretend it took 1 sec. 26450 * NOTE: 'continue' avoids poll_delay 26451 */ 26452 busy_count += (SEC_TO_CSEC - 1); 26453 continue; 26454 26455 } else if ((sensep != NULL) && 26456 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26457 (scsi_sense_asc(sensep) == 0x04) && 26458 (scsi_sense_ascq(sensep) == 0x01)) { 26459 /* 26460 * Not ready -> ready - try again. 26461 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 26462 * ...same as STATUS_BUSY 26463 */ 26464 poll_delay = 100 * CSEC; /* 1 sec. */ 26465 busy_count += (SEC_TO_CSEC - 1); 26466 26467 } else { 26468 /* BAD status - give up. */ 26469 break; 26470 } 26471 } 26472 26473 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 26474 !do_polled_io) { 26475 delay(drv_usectohz(poll_delay)); 26476 } else { 26477 /* we busy wait during cpr_dump or interrupt threads */ 26478 drv_usecwait(poll_delay); 26479 } 26480 } 26481 26482 pkt->pkt_flags = savef; 26483 pkt->pkt_comp = savec; 26484 pkt->pkt_time = savet; 26485 26486 /* return on error */ 26487 if (rval) 26488 return (rval); 26489 26490 /* 26491 * This is not a performance critical code path. 26492 * 26493 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 26494 * issues associated with looking at DMA memory prior to 26495 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 26496 */ 26497 scsi_sync_pkt(pkt); 26498 return (0); 26499 } 26500 26501 26502 26503 /* 26504 * Function: sd_persistent_reservation_in_read_keys 26505 * 26506 * Description: This routine is the driver entry point for handling CD-ROM 26507 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26508 * by sending the SCSI-3 PRIN commands to the device. 26509 * Processes the read keys command response by copying the 26510 * reservation key information into the user provided buffer. 26511 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26512 * 26513 * Arguments: un - Pointer to soft state struct for the target. 26514 * usrp - user provided pointer to multihost Persistent In Read 26515 * Keys structure (mhioc_inkeys_t) 26516 * flag - this argument is a pass through to ddi_copyxxx() 26517 * directly from the mode argument of ioctl(). 26518 * 26519 * Return Code: 0 - Success 26520 * EACCES 26521 * ENOTSUP 26522 * errno return code from sd_send_scsi_cmd() 26523 * 26524 * Context: Can sleep. Does not return until command is completed. 26525 */ 26526 26527 static int 26528 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26529 mhioc_inkeys_t *usrp, int flag) 26530 { 26531 #ifdef _MULTI_DATAMODEL 26532 struct mhioc_key_list32 li32; 26533 #endif 26534 sd_prin_readkeys_t *in; 26535 mhioc_inkeys_t *ptr; 26536 mhioc_key_list_t li; 26537 uchar_t *data_bufp; 26538 int data_len; 26539 int rval = 0; 26540 size_t copysz; 26541 sd_ssc_t *ssc; 26542 26543 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26544 return (EINVAL); 26545 } 26546 bzero(&li, sizeof (mhioc_key_list_t)); 26547 26548 ssc = sd_ssc_init(un); 26549 26550 /* 26551 * Get the listsize from user 26552 */ 26553 #ifdef _MULTI_DATAMODEL 26554 26555 switch (ddi_model_convert_from(flag & FMODELS)) { 26556 case DDI_MODEL_ILP32: 26557 copysz = sizeof (struct mhioc_key_list32); 26558 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26559 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26560 "sd_persistent_reservation_in_read_keys: " 26561 "failed ddi_copyin: mhioc_key_list32_t\n"); 26562 rval = EFAULT; 26563 goto done; 26564 } 26565 li.listsize = li32.listsize; 26566 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26567 break; 26568 26569 case DDI_MODEL_NONE: 26570 copysz = sizeof (mhioc_key_list_t); 26571 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26572 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26573 "sd_persistent_reservation_in_read_keys: " 26574 "failed ddi_copyin: mhioc_key_list_t\n"); 26575 rval = EFAULT; 26576 goto done; 26577 } 26578 break; 26579 } 26580 26581 #else /* ! _MULTI_DATAMODEL */ 26582 copysz = sizeof (mhioc_key_list_t); 26583 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26584 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26585 "sd_persistent_reservation_in_read_keys: " 26586 "failed ddi_copyin: mhioc_key_list_t\n"); 26587 rval = EFAULT; 26588 goto done; 26589 } 26590 #endif 26591 26592 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26593 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26594 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26595 26596 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 26597 data_len, data_bufp); 26598 if (rval != 0) { 26599 if (rval == EIO) 26600 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26601 else 26602 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26603 goto done; 26604 } 26605 in = (sd_prin_readkeys_t *)data_bufp; 26606 ptr->generation = BE_32(in->generation); 26607 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26608 26609 /* 26610 * Return the min(listsize, listlen) keys 26611 */ 26612 #ifdef _MULTI_DATAMODEL 26613 26614 switch (ddi_model_convert_from(flag & FMODELS)) { 26615 case DDI_MODEL_ILP32: 26616 li32.listlen = li.listlen; 26617 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26618 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26619 "sd_persistent_reservation_in_read_keys: " 26620 "failed ddi_copyout: mhioc_key_list32_t\n"); 26621 rval = EFAULT; 26622 goto done; 26623 } 26624 break; 26625 26626 case DDI_MODEL_NONE: 26627 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26628 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26629 "sd_persistent_reservation_in_read_keys: " 26630 "failed ddi_copyout: mhioc_key_list_t\n"); 26631 rval = EFAULT; 26632 goto done; 26633 } 26634 break; 26635 } 26636 26637 #else /* ! _MULTI_DATAMODEL */ 26638 26639 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26640 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26641 "sd_persistent_reservation_in_read_keys: " 26642 "failed ddi_copyout: mhioc_key_list_t\n"); 26643 rval = EFAULT; 26644 goto done; 26645 } 26646 26647 #endif /* _MULTI_DATAMODEL */ 26648 26649 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26650 li.listsize * MHIOC_RESV_KEY_SIZE); 26651 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26652 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26653 "sd_persistent_reservation_in_read_keys: " 26654 "failed ddi_copyout: keylist\n"); 26655 rval = EFAULT; 26656 } 26657 done: 26658 sd_ssc_fini(ssc); 26659 kmem_free(data_bufp, data_len); 26660 return (rval); 26661 } 26662 26663 26664 /* 26665 * Function: sd_persistent_reservation_in_read_resv 26666 * 26667 * Description: This routine is the driver entry point for handling CD-ROM 26668 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26669 * by sending the SCSI-3 PRIN commands to the device. 26670 * Process the read persistent reservations command response by 26671 * copying the reservation information into the user provided 26672 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26673 * 26674 * Arguments: un - Pointer to soft state struct for the target. 26675 * usrp - user provided pointer to multihost Persistent In Read 26676 * Keys structure (mhioc_inkeys_t) 26677 * flag - this argument is a pass through to ddi_copyxxx() 26678 * directly from the mode argument of ioctl(). 26679 * 26680 * Return Code: 0 - Success 26681 * EACCES 26682 * ENOTSUP 26683 * errno return code from sd_send_scsi_cmd() 26684 * 26685 * Context: Can sleep. Does not return until command is completed. 26686 */ 26687 26688 static int 26689 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26690 mhioc_inresvs_t *usrp, int flag) 26691 { 26692 #ifdef _MULTI_DATAMODEL 26693 struct mhioc_resv_desc_list32 resvlist32; 26694 #endif 26695 sd_prin_readresv_t *in; 26696 mhioc_inresvs_t *ptr; 26697 sd_readresv_desc_t *readresv_ptr; 26698 mhioc_resv_desc_list_t resvlist; 26699 mhioc_resv_desc_t resvdesc; 26700 uchar_t *data_bufp = NULL; 26701 int data_len; 26702 int rval = 0; 26703 int i; 26704 size_t copysz; 26705 mhioc_resv_desc_t *bufp; 26706 sd_ssc_t *ssc; 26707 26708 if ((ptr = usrp) == NULL) { 26709 return (EINVAL); 26710 } 26711 26712 ssc = sd_ssc_init(un); 26713 26714 /* 26715 * Get the listsize from user 26716 */ 26717 #ifdef _MULTI_DATAMODEL 26718 switch (ddi_model_convert_from(flag & FMODELS)) { 26719 case DDI_MODEL_ILP32: 26720 copysz = sizeof (struct mhioc_resv_desc_list32); 26721 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26722 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26723 "sd_persistent_reservation_in_read_resv: " 26724 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26725 rval = EFAULT; 26726 goto done; 26727 } 26728 resvlist.listsize = resvlist32.listsize; 26729 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26730 break; 26731 26732 case DDI_MODEL_NONE: 26733 copysz = sizeof (mhioc_resv_desc_list_t); 26734 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26735 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26736 "sd_persistent_reservation_in_read_resv: " 26737 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26738 rval = EFAULT; 26739 goto done; 26740 } 26741 break; 26742 } 26743 #else /* ! _MULTI_DATAMODEL */ 26744 copysz = sizeof (mhioc_resv_desc_list_t); 26745 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26746 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26747 "sd_persistent_reservation_in_read_resv: " 26748 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26749 rval = EFAULT; 26750 goto done; 26751 } 26752 #endif /* ! _MULTI_DATAMODEL */ 26753 26754 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26755 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26756 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26757 26758 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV, 26759 data_len, data_bufp); 26760 if (rval != 0) { 26761 if (rval == EIO) 26762 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26763 else 26764 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26765 goto done; 26766 } 26767 in = (sd_prin_readresv_t *)data_bufp; 26768 ptr->generation = BE_32(in->generation); 26769 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26770 26771 /* 26772 * Return the min(listsize, listlen( keys 26773 */ 26774 #ifdef _MULTI_DATAMODEL 26775 26776 switch (ddi_model_convert_from(flag & FMODELS)) { 26777 case DDI_MODEL_ILP32: 26778 resvlist32.listlen = resvlist.listlen; 26779 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26780 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26781 "sd_persistent_reservation_in_read_resv: " 26782 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26783 rval = EFAULT; 26784 goto done; 26785 } 26786 break; 26787 26788 case DDI_MODEL_NONE: 26789 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26790 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26791 "sd_persistent_reservation_in_read_resv: " 26792 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26793 rval = EFAULT; 26794 goto done; 26795 } 26796 break; 26797 } 26798 26799 #else /* ! _MULTI_DATAMODEL */ 26800 26801 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26802 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26803 "sd_persistent_reservation_in_read_resv: " 26804 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26805 rval = EFAULT; 26806 goto done; 26807 } 26808 26809 #endif /* ! _MULTI_DATAMODEL */ 26810 26811 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26812 bufp = resvlist.list; 26813 copysz = sizeof (mhioc_resv_desc_t); 26814 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26815 i++, readresv_ptr++, bufp++) { 26816 26817 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26818 MHIOC_RESV_KEY_SIZE); 26819 resvdesc.type = readresv_ptr->type; 26820 resvdesc.scope = readresv_ptr->scope; 26821 resvdesc.scope_specific_addr = 26822 BE_32(readresv_ptr->scope_specific_addr); 26823 26824 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26825 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26826 "sd_persistent_reservation_in_read_resv: " 26827 "failed ddi_copyout: resvlist\n"); 26828 rval = EFAULT; 26829 goto done; 26830 } 26831 } 26832 done: 26833 sd_ssc_fini(ssc); 26834 /* only if data_bufp is allocated, we need to free it */ 26835 if (data_bufp) { 26836 kmem_free(data_bufp, data_len); 26837 } 26838 return (rval); 26839 } 26840 26841 26842 /* 26843 * Function: sr_change_blkmode() 26844 * 26845 * Description: This routine is the driver entry point for handling CD-ROM 26846 * block mode ioctl requests. Support for returning and changing 26847 * the current block size in use by the device is implemented. The 26848 * LBA size is changed via a MODE SELECT Block Descriptor. 26849 * 26850 * This routine issues a mode sense with an allocation length of 26851 * 12 bytes for the mode page header and a single block descriptor. 26852 * 26853 * Arguments: dev - the device 'dev_t' 26854 * cmd - the request type; one of CDROMGBLKMODE (get) or 26855 * CDROMSBLKMODE (set) 26856 * data - current block size or requested block size 26857 * flag - this argument is a pass through to ddi_copyxxx() directly 26858 * from the mode argument of ioctl(). 26859 * 26860 * Return Code: the code returned by sd_send_scsi_cmd() 26861 * EINVAL if invalid arguments are provided 26862 * EFAULT if ddi_copyxxx() fails 26863 * ENXIO if fail ddi_get_soft_state 26864 * EIO if invalid mode sense block descriptor length 26865 * 26866 */ 26867 26868 static int 26869 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26870 { 26871 struct sd_lun *un = NULL; 26872 struct mode_header *sense_mhp, *select_mhp; 26873 struct block_descriptor *sense_desc, *select_desc; 26874 int current_bsize; 26875 int rval = EINVAL; 26876 uchar_t *sense = NULL; 26877 uchar_t *select = NULL; 26878 sd_ssc_t *ssc; 26879 26880 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26881 26882 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26883 return (ENXIO); 26884 } 26885 26886 /* 26887 * The block length is changed via the Mode Select block descriptor, the 26888 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26889 * required as part of this routine. Therefore the mode sense allocation 26890 * length is specified to be the length of a mode page header and a 26891 * block descriptor. 26892 */ 26893 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26894 26895 ssc = sd_ssc_init(un); 26896 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 26897 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 26898 sd_ssc_fini(ssc); 26899 if (rval != 0) { 26900 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26901 "sr_change_blkmode: Mode Sense Failed\n"); 26902 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26903 return (rval); 26904 } 26905 26906 /* Check the block descriptor len to handle only 1 block descriptor */ 26907 sense_mhp = (struct mode_header *)sense; 26908 if ((sense_mhp->bdesc_length == 0) || 26909 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26910 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26911 "sr_change_blkmode: Mode Sense returned invalid block" 26912 " descriptor length\n"); 26913 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26914 return (EIO); 26915 } 26916 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26917 current_bsize = ((sense_desc->blksize_hi << 16) | 26918 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26919 26920 /* Process command */ 26921 switch (cmd) { 26922 case CDROMGBLKMODE: 26923 /* Return the block size obtained during the mode sense */ 26924 if (ddi_copyout(¤t_bsize, (void *)data, 26925 sizeof (int), flag) != 0) 26926 rval = EFAULT; 26927 break; 26928 case CDROMSBLKMODE: 26929 /* Validate the requested block size */ 26930 switch (data) { 26931 case CDROM_BLK_512: 26932 case CDROM_BLK_1024: 26933 case CDROM_BLK_2048: 26934 case CDROM_BLK_2056: 26935 case CDROM_BLK_2336: 26936 case CDROM_BLK_2340: 26937 case CDROM_BLK_2352: 26938 case CDROM_BLK_2368: 26939 case CDROM_BLK_2448: 26940 case CDROM_BLK_2646: 26941 case CDROM_BLK_2647: 26942 break; 26943 default: 26944 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26945 "sr_change_blkmode: " 26946 "Block Size '%ld' Not Supported\n", data); 26947 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26948 return (EINVAL); 26949 } 26950 26951 /* 26952 * The current block size matches the requested block size so 26953 * there is no need to send the mode select to change the size 26954 */ 26955 if (current_bsize == data) { 26956 break; 26957 } 26958 26959 /* Build the select data for the requested block size */ 26960 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26961 select_mhp = (struct mode_header *)select; 26962 select_desc = 26963 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26964 /* 26965 * The LBA size is changed via the block descriptor, so the 26966 * descriptor is built according to the user data 26967 */ 26968 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26969 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26970 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26971 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26972 26973 /* Send the mode select for the requested block size */ 26974 ssc = sd_ssc_init(un); 26975 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26976 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26977 SD_PATH_STANDARD); 26978 sd_ssc_fini(ssc); 26979 if (rval != 0) { 26980 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26981 "sr_change_blkmode: Mode Select Failed\n"); 26982 /* 26983 * The mode select failed for the requested block size, 26984 * so reset the data for the original block size and 26985 * send it to the target. The error is indicated by the 26986 * return value for the failed mode select. 26987 */ 26988 select_desc->blksize_hi = sense_desc->blksize_hi; 26989 select_desc->blksize_mid = sense_desc->blksize_mid; 26990 select_desc->blksize_lo = sense_desc->blksize_lo; 26991 ssc = sd_ssc_init(un); 26992 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26993 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26994 SD_PATH_STANDARD); 26995 sd_ssc_fini(ssc); 26996 } else { 26997 ASSERT(!mutex_owned(SD_MUTEX(un))); 26998 mutex_enter(SD_MUTEX(un)); 26999 sd_update_block_info(un, (uint32_t)data, 0); 27000 mutex_exit(SD_MUTEX(un)); 27001 } 27002 break; 27003 default: 27004 /* should not reach here, but check anyway */ 27005 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27006 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27007 rval = EINVAL; 27008 break; 27009 } 27010 27011 if (select) { 27012 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27013 } 27014 if (sense) { 27015 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27016 } 27017 return (rval); 27018 } 27019 27020 27021 /* 27022 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27023 * implement driver support for getting and setting the CD speed. The command 27024 * set used will be based on the device type. If the device has not been 27025 * identified as MMC the Toshiba vendor specific mode page will be used. If 27026 * the device is MMC but does not support the Real Time Streaming feature 27027 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27028 * be used to read the speed. 27029 */ 27030 27031 /* 27032 * Function: sr_change_speed() 27033 * 27034 * Description: This routine is the driver entry point for handling CD-ROM 27035 * drive speed ioctl requests for devices supporting the Toshiba 27036 * vendor specific drive speed mode page. Support for returning 27037 * and changing the current drive speed in use by the device is 27038 * implemented. 27039 * 27040 * Arguments: dev - the device 'dev_t' 27041 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27042 * CDROMSDRVSPEED (set) 27043 * data - current drive speed or requested drive speed 27044 * flag - this argument is a pass through to ddi_copyxxx() directly 27045 * from the mode argument of ioctl(). 27046 * 27047 * Return Code: the code returned by sd_send_scsi_cmd() 27048 * EINVAL if invalid arguments are provided 27049 * EFAULT if ddi_copyxxx() fails 27050 * ENXIO if fail ddi_get_soft_state 27051 * EIO if invalid mode sense block descriptor length 27052 */ 27053 27054 static int 27055 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27056 { 27057 struct sd_lun *un = NULL; 27058 struct mode_header *sense_mhp, *select_mhp; 27059 struct mode_speed *sense_page, *select_page; 27060 int current_speed; 27061 int rval = EINVAL; 27062 int bd_len; 27063 uchar_t *sense = NULL; 27064 uchar_t *select = NULL; 27065 sd_ssc_t *ssc; 27066 27067 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27068 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27069 return (ENXIO); 27070 } 27071 27072 /* 27073 * Note: The drive speed is being modified here according to a Toshiba 27074 * vendor specific mode page (0x31). 27075 */ 27076 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27077 27078 ssc = sd_ssc_init(un); 27079 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 27080 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27081 SD_PATH_STANDARD); 27082 sd_ssc_fini(ssc); 27083 if (rval != 0) { 27084 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27085 "sr_change_speed: Mode Sense Failed\n"); 27086 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27087 return (rval); 27088 } 27089 sense_mhp = (struct mode_header *)sense; 27090 27091 /* Check the block descriptor len to handle only 1 block descriptor */ 27092 bd_len = sense_mhp->bdesc_length; 27093 if (bd_len > MODE_BLK_DESC_LENGTH) { 27094 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27095 "sr_change_speed: Mode Sense returned invalid block " 27096 "descriptor length\n"); 27097 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27098 return (EIO); 27099 } 27100 27101 sense_page = (struct mode_speed *) 27102 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27103 current_speed = sense_page->speed; 27104 27105 /* Process command */ 27106 switch (cmd) { 27107 case CDROMGDRVSPEED: 27108 /* Return the drive speed obtained during the mode sense */ 27109 if (current_speed == 0x2) { 27110 current_speed = CDROM_TWELVE_SPEED; 27111 } 27112 if (ddi_copyout(¤t_speed, (void *)data, 27113 sizeof (int), flag) != 0) { 27114 rval = EFAULT; 27115 } 27116 break; 27117 case CDROMSDRVSPEED: 27118 /* Validate the requested drive speed */ 27119 switch ((uchar_t)data) { 27120 case CDROM_TWELVE_SPEED: 27121 data = 0x2; 27122 /*FALLTHROUGH*/ 27123 case CDROM_NORMAL_SPEED: 27124 case CDROM_DOUBLE_SPEED: 27125 case CDROM_QUAD_SPEED: 27126 case CDROM_MAXIMUM_SPEED: 27127 break; 27128 default: 27129 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27130 "sr_change_speed: " 27131 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27132 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27133 return (EINVAL); 27134 } 27135 27136 /* 27137 * The current drive speed matches the requested drive speed so 27138 * there is no need to send the mode select to change the speed 27139 */ 27140 if (current_speed == data) { 27141 break; 27142 } 27143 27144 /* Build the select data for the requested drive speed */ 27145 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27146 select_mhp = (struct mode_header *)select; 27147 select_mhp->bdesc_length = 0; 27148 select_page = 27149 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27150 select_page = 27151 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27152 select_page->mode_page.code = CDROM_MODE_SPEED; 27153 select_page->mode_page.length = 2; 27154 select_page->speed = (uchar_t)data; 27155 27156 /* Send the mode select for the requested block size */ 27157 ssc = sd_ssc_init(un); 27158 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27159 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27160 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27161 sd_ssc_fini(ssc); 27162 if (rval != 0) { 27163 /* 27164 * The mode select failed for the requested drive speed, 27165 * so reset the data for the original drive speed and 27166 * send it to the target. The error is indicated by the 27167 * return value for the failed mode select. 27168 */ 27169 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27170 "sr_drive_speed: Mode Select Failed\n"); 27171 select_page->speed = sense_page->speed; 27172 ssc = sd_ssc_init(un); 27173 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27174 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27175 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27176 sd_ssc_fini(ssc); 27177 } 27178 break; 27179 default: 27180 /* should not reach here, but check anyway */ 27181 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27182 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27183 rval = EINVAL; 27184 break; 27185 } 27186 27187 if (select) { 27188 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27189 } 27190 if (sense) { 27191 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27192 } 27193 27194 return (rval); 27195 } 27196 27197 27198 /* 27199 * Function: sr_atapi_change_speed() 27200 * 27201 * Description: This routine is the driver entry point for handling CD-ROM 27202 * drive speed ioctl requests for MMC devices that do not support 27203 * the Real Time Streaming feature (0x107). 27204 * 27205 * Note: This routine will use the SET SPEED command which may not 27206 * be supported by all devices. 27207 * 27208 * Arguments: dev- the device 'dev_t' 27209 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27210 * CDROMSDRVSPEED (set) 27211 * data- current drive speed or requested drive speed 27212 * flag- this argument is a pass through to ddi_copyxxx() directly 27213 * from the mode argument of ioctl(). 27214 * 27215 * Return Code: the code returned by sd_send_scsi_cmd() 27216 * EINVAL if invalid arguments are provided 27217 * EFAULT if ddi_copyxxx() fails 27218 * ENXIO if fail ddi_get_soft_state 27219 * EIO if invalid mode sense block descriptor length 27220 */ 27221 27222 static int 27223 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27224 { 27225 struct sd_lun *un; 27226 struct uscsi_cmd *com = NULL; 27227 struct mode_header_grp2 *sense_mhp; 27228 uchar_t *sense_page; 27229 uchar_t *sense = NULL; 27230 char cdb[CDB_GROUP5]; 27231 int bd_len; 27232 int current_speed = 0; 27233 int max_speed = 0; 27234 int rval; 27235 sd_ssc_t *ssc; 27236 27237 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27238 27239 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27240 return (ENXIO); 27241 } 27242 27243 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27244 27245 ssc = sd_ssc_init(un); 27246 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 27247 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27248 SD_PATH_STANDARD); 27249 sd_ssc_fini(ssc); 27250 if (rval != 0) { 27251 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27252 "sr_atapi_change_speed: Mode Sense Failed\n"); 27253 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27254 return (rval); 27255 } 27256 27257 /* Check the block descriptor len to handle only 1 block descriptor */ 27258 sense_mhp = (struct mode_header_grp2 *)sense; 27259 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27260 if (bd_len > MODE_BLK_DESC_LENGTH) { 27261 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27262 "sr_atapi_change_speed: Mode Sense returned invalid " 27263 "block descriptor length\n"); 27264 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27265 return (EIO); 27266 } 27267 27268 /* Calculate the current and maximum drive speeds */ 27269 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27270 current_speed = (sense_page[14] << 8) | sense_page[15]; 27271 max_speed = (sense_page[8] << 8) | sense_page[9]; 27272 27273 /* Process the command */ 27274 switch (cmd) { 27275 case CDROMGDRVSPEED: 27276 current_speed /= SD_SPEED_1X; 27277 if (ddi_copyout(¤t_speed, (void *)data, 27278 sizeof (int), flag) != 0) 27279 rval = EFAULT; 27280 break; 27281 case CDROMSDRVSPEED: 27282 /* Convert the speed code to KB/sec */ 27283 switch ((uchar_t)data) { 27284 case CDROM_NORMAL_SPEED: 27285 current_speed = SD_SPEED_1X; 27286 break; 27287 case CDROM_DOUBLE_SPEED: 27288 current_speed = 2 * SD_SPEED_1X; 27289 break; 27290 case CDROM_QUAD_SPEED: 27291 current_speed = 4 * SD_SPEED_1X; 27292 break; 27293 case CDROM_TWELVE_SPEED: 27294 current_speed = 12 * SD_SPEED_1X; 27295 break; 27296 case CDROM_MAXIMUM_SPEED: 27297 current_speed = 0xffff; 27298 break; 27299 default: 27300 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27301 "sr_atapi_change_speed: invalid drive speed %d\n", 27302 (uchar_t)data); 27303 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27304 return (EINVAL); 27305 } 27306 27307 /* Check the request against the drive's max speed. */ 27308 if (current_speed != 0xffff) { 27309 if (current_speed > max_speed) { 27310 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27311 return (EINVAL); 27312 } 27313 } 27314 27315 /* 27316 * Build and send the SET SPEED command 27317 * 27318 * Note: The SET SPEED (0xBB) command used in this routine is 27319 * obsolete per the SCSI MMC spec but still supported in the 27320 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27321 * therefore the command is still implemented in this routine. 27322 */ 27323 bzero(cdb, sizeof (cdb)); 27324 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27325 cdb[2] = (uchar_t)(current_speed >> 8); 27326 cdb[3] = (uchar_t)current_speed; 27327 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27328 com->uscsi_cdb = (caddr_t)cdb; 27329 com->uscsi_cdblen = CDB_GROUP5; 27330 com->uscsi_bufaddr = NULL; 27331 com->uscsi_buflen = 0; 27332 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27333 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 27334 break; 27335 default: 27336 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27337 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27338 rval = EINVAL; 27339 } 27340 27341 if (sense) { 27342 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27343 } 27344 if (com) { 27345 kmem_free(com, sizeof (*com)); 27346 } 27347 return (rval); 27348 } 27349 27350 27351 /* 27352 * Function: sr_pause_resume() 27353 * 27354 * Description: This routine is the driver entry point for handling CD-ROM 27355 * pause/resume ioctl requests. This only affects the audio play 27356 * operation. 27357 * 27358 * Arguments: dev - the device 'dev_t' 27359 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27360 * for setting the resume bit of the cdb. 27361 * 27362 * Return Code: the code returned by sd_send_scsi_cmd() 27363 * EINVAL if invalid mode specified 27364 * 27365 */ 27366 27367 static int 27368 sr_pause_resume(dev_t dev, int cmd) 27369 { 27370 struct sd_lun *un; 27371 struct uscsi_cmd *com; 27372 char cdb[CDB_GROUP1]; 27373 int rval; 27374 27375 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27376 return (ENXIO); 27377 } 27378 27379 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27380 bzero(cdb, CDB_GROUP1); 27381 cdb[0] = SCMD_PAUSE_RESUME; 27382 switch (cmd) { 27383 case CDROMRESUME: 27384 cdb[8] = 1; 27385 break; 27386 case CDROMPAUSE: 27387 cdb[8] = 0; 27388 break; 27389 default: 27390 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27391 " Command '%x' Not Supported\n", cmd); 27392 rval = EINVAL; 27393 goto done; 27394 } 27395 27396 com->uscsi_cdb = cdb; 27397 com->uscsi_cdblen = CDB_GROUP1; 27398 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27399 27400 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27401 SD_PATH_STANDARD); 27402 27403 done: 27404 kmem_free(com, sizeof (*com)); 27405 return (rval); 27406 } 27407 27408 27409 /* 27410 * Function: sr_play_msf() 27411 * 27412 * Description: This routine is the driver entry point for handling CD-ROM 27413 * ioctl requests to output the audio signals at the specified 27414 * starting address and continue the audio play until the specified 27415 * ending address (CDROMPLAYMSF) The address is in Minute Second 27416 * Frame (MSF) format. 27417 * 27418 * Arguments: dev - the device 'dev_t' 27419 * data - pointer to user provided audio msf structure, 27420 * specifying start/end addresses. 27421 * flag - this argument is a pass through to ddi_copyxxx() 27422 * directly from the mode argument of ioctl(). 27423 * 27424 * Return Code: the code returned by sd_send_scsi_cmd() 27425 * EFAULT if ddi_copyxxx() fails 27426 * ENXIO if fail ddi_get_soft_state 27427 * EINVAL if data pointer is NULL 27428 */ 27429 27430 static int 27431 sr_play_msf(dev_t dev, caddr_t data, int flag) 27432 { 27433 struct sd_lun *un; 27434 struct uscsi_cmd *com; 27435 struct cdrom_msf msf_struct; 27436 struct cdrom_msf *msf = &msf_struct; 27437 char cdb[CDB_GROUP1]; 27438 int rval; 27439 27440 if (data == NULL) { 27441 return (EINVAL); 27442 } 27443 27444 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27445 return (ENXIO); 27446 } 27447 27448 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27449 return (EFAULT); 27450 } 27451 27452 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27453 bzero(cdb, CDB_GROUP1); 27454 cdb[0] = SCMD_PLAYAUDIO_MSF; 27455 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27456 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27457 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27458 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27459 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27460 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27461 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27462 } else { 27463 cdb[3] = msf->cdmsf_min0; 27464 cdb[4] = msf->cdmsf_sec0; 27465 cdb[5] = msf->cdmsf_frame0; 27466 cdb[6] = msf->cdmsf_min1; 27467 cdb[7] = msf->cdmsf_sec1; 27468 cdb[8] = msf->cdmsf_frame1; 27469 } 27470 com->uscsi_cdb = cdb; 27471 com->uscsi_cdblen = CDB_GROUP1; 27472 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27473 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27474 SD_PATH_STANDARD); 27475 kmem_free(com, sizeof (*com)); 27476 return (rval); 27477 } 27478 27479 27480 /* 27481 * Function: sr_play_trkind() 27482 * 27483 * Description: This routine is the driver entry point for handling CD-ROM 27484 * ioctl requests to output the audio signals at the specified 27485 * starting address and continue the audio play until the specified 27486 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27487 * format. 27488 * 27489 * Arguments: dev - the device 'dev_t' 27490 * data - pointer to user provided audio track/index structure, 27491 * specifying start/end addresses. 27492 * flag - this argument is a pass through to ddi_copyxxx() 27493 * directly from the mode argument of ioctl(). 27494 * 27495 * Return Code: the code returned by sd_send_scsi_cmd() 27496 * EFAULT if ddi_copyxxx() fails 27497 * ENXIO if fail ddi_get_soft_state 27498 * EINVAL if data pointer is NULL 27499 */ 27500 27501 static int 27502 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27503 { 27504 struct cdrom_ti ti_struct; 27505 struct cdrom_ti *ti = &ti_struct; 27506 struct uscsi_cmd *com = NULL; 27507 char cdb[CDB_GROUP1]; 27508 int rval; 27509 27510 if (data == NULL) { 27511 return (EINVAL); 27512 } 27513 27514 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27515 return (EFAULT); 27516 } 27517 27518 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27519 bzero(cdb, CDB_GROUP1); 27520 cdb[0] = SCMD_PLAYAUDIO_TI; 27521 cdb[4] = ti->cdti_trk0; 27522 cdb[5] = ti->cdti_ind0; 27523 cdb[7] = ti->cdti_trk1; 27524 cdb[8] = ti->cdti_ind1; 27525 com->uscsi_cdb = cdb; 27526 com->uscsi_cdblen = CDB_GROUP1; 27527 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27528 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27529 SD_PATH_STANDARD); 27530 kmem_free(com, sizeof (*com)); 27531 return (rval); 27532 } 27533 27534 27535 /* 27536 * Function: sr_read_all_subcodes() 27537 * 27538 * Description: This routine is the driver entry point for handling CD-ROM 27539 * ioctl requests to return raw subcode data while the target is 27540 * playing audio (CDROMSUBCODE). 27541 * 27542 * Arguments: dev - the device 'dev_t' 27543 * data - pointer to user provided cdrom subcode structure, 27544 * specifying the transfer length and address. 27545 * flag - this argument is a pass through to ddi_copyxxx() 27546 * directly from the mode argument of ioctl(). 27547 * 27548 * Return Code: the code returned by sd_send_scsi_cmd() 27549 * EFAULT if ddi_copyxxx() fails 27550 * ENXIO if fail ddi_get_soft_state 27551 * EINVAL if data pointer is NULL 27552 */ 27553 27554 static int 27555 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27556 { 27557 struct sd_lun *un = NULL; 27558 struct uscsi_cmd *com = NULL; 27559 struct cdrom_subcode *subcode = NULL; 27560 int rval; 27561 size_t buflen; 27562 char cdb[CDB_GROUP5]; 27563 27564 #ifdef _MULTI_DATAMODEL 27565 /* To support ILP32 applications in an LP64 world */ 27566 struct cdrom_subcode32 cdrom_subcode32; 27567 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27568 #endif 27569 if (data == NULL) { 27570 return (EINVAL); 27571 } 27572 27573 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27574 return (ENXIO); 27575 } 27576 27577 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27578 27579 #ifdef _MULTI_DATAMODEL 27580 switch (ddi_model_convert_from(flag & FMODELS)) { 27581 case DDI_MODEL_ILP32: 27582 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27583 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27584 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27585 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27586 return (EFAULT); 27587 } 27588 /* Convert the ILP32 uscsi data from the application to LP64 */ 27589 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27590 break; 27591 case DDI_MODEL_NONE: 27592 if (ddi_copyin(data, subcode, 27593 sizeof (struct cdrom_subcode), flag)) { 27594 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27595 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27596 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27597 return (EFAULT); 27598 } 27599 break; 27600 } 27601 #else /* ! _MULTI_DATAMODEL */ 27602 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27603 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27604 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27605 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27606 return (EFAULT); 27607 } 27608 #endif /* _MULTI_DATAMODEL */ 27609 27610 /* 27611 * Since MMC-2 expects max 3 bytes for length, check if the 27612 * length input is greater than 3 bytes 27613 */ 27614 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27615 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27616 "sr_read_all_subcodes: " 27617 "cdrom transfer length too large: %d (limit %d)\n", 27618 subcode->cdsc_length, 0xFFFFFF); 27619 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27620 return (EINVAL); 27621 } 27622 27623 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27624 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27625 bzero(cdb, CDB_GROUP5); 27626 27627 if (un->un_f_mmc_cap == TRUE) { 27628 cdb[0] = (char)SCMD_READ_CD; 27629 cdb[2] = (char)0xff; 27630 cdb[3] = (char)0xff; 27631 cdb[4] = (char)0xff; 27632 cdb[5] = (char)0xff; 27633 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27634 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27635 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27636 cdb[10] = 1; 27637 } else { 27638 /* 27639 * Note: A vendor specific command (0xDF) is being used her to 27640 * request a read of all subcodes. 27641 */ 27642 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27643 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27644 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27645 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27646 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27647 } 27648 com->uscsi_cdb = cdb; 27649 com->uscsi_cdblen = CDB_GROUP5; 27650 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27651 com->uscsi_buflen = buflen; 27652 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27653 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27654 SD_PATH_STANDARD); 27655 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27656 kmem_free(com, sizeof (*com)); 27657 return (rval); 27658 } 27659 27660 27661 /* 27662 * Function: sr_read_subchannel() 27663 * 27664 * Description: This routine is the driver entry point for handling CD-ROM 27665 * ioctl requests to return the Q sub-channel data of the CD 27666 * current position block. (CDROMSUBCHNL) The data includes the 27667 * track number, index number, absolute CD-ROM address (LBA or MSF 27668 * format per the user) , track relative CD-ROM address (LBA or MSF 27669 * format per the user), control data and audio status. 27670 * 27671 * Arguments: dev - the device 'dev_t' 27672 * data - pointer to user provided cdrom sub-channel structure 27673 * flag - this argument is a pass through to ddi_copyxxx() 27674 * directly from the mode argument of ioctl(). 27675 * 27676 * Return Code: the code returned by sd_send_scsi_cmd() 27677 * EFAULT if ddi_copyxxx() fails 27678 * ENXIO if fail ddi_get_soft_state 27679 * EINVAL if data pointer is NULL 27680 */ 27681 27682 static int 27683 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27684 { 27685 struct sd_lun *un; 27686 struct uscsi_cmd *com; 27687 struct cdrom_subchnl subchanel; 27688 struct cdrom_subchnl *subchnl = &subchanel; 27689 char cdb[CDB_GROUP1]; 27690 caddr_t buffer; 27691 int rval; 27692 27693 if (data == NULL) { 27694 return (EINVAL); 27695 } 27696 27697 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27698 (un->un_state == SD_STATE_OFFLINE)) { 27699 return (ENXIO); 27700 } 27701 27702 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27703 return (EFAULT); 27704 } 27705 27706 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27707 bzero(cdb, CDB_GROUP1); 27708 cdb[0] = SCMD_READ_SUBCHANNEL; 27709 /* Set the MSF bit based on the user requested address format */ 27710 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27711 /* 27712 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27713 * returned 27714 */ 27715 cdb[2] = 0x40; 27716 /* 27717 * Set byte 3 to specify the return data format. A value of 0x01 27718 * indicates that the CD-ROM current position should be returned. 27719 */ 27720 cdb[3] = 0x01; 27721 cdb[8] = 0x10; 27722 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27723 com->uscsi_cdb = cdb; 27724 com->uscsi_cdblen = CDB_GROUP1; 27725 com->uscsi_bufaddr = buffer; 27726 com->uscsi_buflen = 16; 27727 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27728 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27729 SD_PATH_STANDARD); 27730 if (rval != 0) { 27731 kmem_free(buffer, 16); 27732 kmem_free(com, sizeof (*com)); 27733 return (rval); 27734 } 27735 27736 /* Process the returned Q sub-channel data */ 27737 subchnl->cdsc_audiostatus = buffer[1]; 27738 subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4; 27739 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27740 subchnl->cdsc_trk = buffer[6]; 27741 subchnl->cdsc_ind = buffer[7]; 27742 if (subchnl->cdsc_format & CDROM_LBA) { 27743 subchnl->cdsc_absaddr.lba = 27744 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27745 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27746 subchnl->cdsc_reladdr.lba = 27747 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27748 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27749 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27750 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27751 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27752 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27753 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27754 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27755 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27756 } else { 27757 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27758 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27759 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27760 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27761 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27762 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27763 } 27764 kmem_free(buffer, 16); 27765 kmem_free(com, sizeof (*com)); 27766 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27767 != 0) { 27768 return (EFAULT); 27769 } 27770 return (rval); 27771 } 27772 27773 27774 /* 27775 * Function: sr_read_tocentry() 27776 * 27777 * Description: This routine is the driver entry point for handling CD-ROM 27778 * ioctl requests to read from the Table of Contents (TOC) 27779 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27780 * fields, the starting address (LBA or MSF format per the user) 27781 * and the data mode if the user specified track is a data track. 27782 * 27783 * Note: The READ HEADER (0x44) command used in this routine is 27784 * obsolete per the SCSI MMC spec but still supported in the 27785 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27786 * therefore the command is still implemented in this routine. 27787 * 27788 * Arguments: dev - the device 'dev_t' 27789 * data - pointer to user provided toc entry structure, 27790 * specifying the track # and the address format 27791 * (LBA or MSF). 27792 * flag - this argument is a pass through to ddi_copyxxx() 27793 * directly from the mode argument of ioctl(). 27794 * 27795 * Return Code: the code returned by sd_send_scsi_cmd() 27796 * EFAULT if ddi_copyxxx() fails 27797 * ENXIO if fail ddi_get_soft_state 27798 * EINVAL if data pointer is NULL 27799 */ 27800 27801 static int 27802 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27803 { 27804 struct sd_lun *un = NULL; 27805 struct uscsi_cmd *com; 27806 struct cdrom_tocentry toc_entry; 27807 struct cdrom_tocentry *entry = &toc_entry; 27808 caddr_t buffer; 27809 int rval; 27810 char cdb[CDB_GROUP1]; 27811 27812 if (data == NULL) { 27813 return (EINVAL); 27814 } 27815 27816 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27817 (un->un_state == SD_STATE_OFFLINE)) { 27818 return (ENXIO); 27819 } 27820 27821 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27822 return (EFAULT); 27823 } 27824 27825 /* Validate the requested track and address format */ 27826 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27827 return (EINVAL); 27828 } 27829 27830 if (entry->cdte_track == 0) { 27831 return (EINVAL); 27832 } 27833 27834 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27835 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27836 bzero(cdb, CDB_GROUP1); 27837 27838 cdb[0] = SCMD_READ_TOC; 27839 /* Set the MSF bit based on the user requested address format */ 27840 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27841 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27842 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27843 } else { 27844 cdb[6] = entry->cdte_track; 27845 } 27846 27847 /* 27848 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27849 * (4 byte TOC response header + 8 byte track descriptor) 27850 */ 27851 cdb[8] = 12; 27852 com->uscsi_cdb = cdb; 27853 com->uscsi_cdblen = CDB_GROUP1; 27854 com->uscsi_bufaddr = buffer; 27855 com->uscsi_buflen = 0x0C; 27856 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27857 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27858 SD_PATH_STANDARD); 27859 if (rval != 0) { 27860 kmem_free(buffer, 12); 27861 kmem_free(com, sizeof (*com)); 27862 return (rval); 27863 } 27864 27865 /* Process the toc entry */ 27866 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27867 entry->cdte_ctrl = (buffer[5] & 0x0F); 27868 if (entry->cdte_format & CDROM_LBA) { 27869 entry->cdte_addr.lba = 27870 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27871 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27872 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27873 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27874 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27875 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27876 /* 27877 * Send a READ TOC command using the LBA address format to get 27878 * the LBA for the track requested so it can be used in the 27879 * READ HEADER request 27880 * 27881 * Note: The MSF bit of the READ HEADER command specifies the 27882 * output format. The block address specified in that command 27883 * must be in LBA format. 27884 */ 27885 cdb[1] = 0; 27886 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27887 SD_PATH_STANDARD); 27888 if (rval != 0) { 27889 kmem_free(buffer, 12); 27890 kmem_free(com, sizeof (*com)); 27891 return (rval); 27892 } 27893 } else { 27894 entry->cdte_addr.msf.minute = buffer[9]; 27895 entry->cdte_addr.msf.second = buffer[10]; 27896 entry->cdte_addr.msf.frame = buffer[11]; 27897 /* 27898 * Send a READ TOC command using the LBA address format to get 27899 * the LBA for the track requested so it can be used in the 27900 * READ HEADER request 27901 * 27902 * Note: The MSF bit of the READ HEADER command specifies the 27903 * output format. The block address specified in that command 27904 * must be in LBA format. 27905 */ 27906 cdb[1] = 0; 27907 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27908 SD_PATH_STANDARD); 27909 if (rval != 0) { 27910 kmem_free(buffer, 12); 27911 kmem_free(com, sizeof (*com)); 27912 return (rval); 27913 } 27914 } 27915 27916 /* 27917 * Build and send the READ HEADER command to determine the data mode of 27918 * the user specified track. 27919 */ 27920 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27921 (entry->cdte_track != CDROM_LEADOUT)) { 27922 bzero(cdb, CDB_GROUP1); 27923 cdb[0] = SCMD_READ_HEADER; 27924 cdb[2] = buffer[8]; 27925 cdb[3] = buffer[9]; 27926 cdb[4] = buffer[10]; 27927 cdb[5] = buffer[11]; 27928 cdb[8] = 0x08; 27929 com->uscsi_buflen = 0x08; 27930 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27931 SD_PATH_STANDARD); 27932 if (rval == 0) { 27933 entry->cdte_datamode = buffer[0]; 27934 } else { 27935 /* 27936 * READ HEADER command failed, since this is 27937 * obsoleted in one spec, its better to return 27938 * -1 for an invlid track so that we can still 27939 * receive the rest of the TOC data. 27940 */ 27941 entry->cdte_datamode = (uchar_t)-1; 27942 } 27943 } else { 27944 entry->cdte_datamode = (uchar_t)-1; 27945 } 27946 27947 kmem_free(buffer, 12); 27948 kmem_free(com, sizeof (*com)); 27949 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27950 return (EFAULT); 27951 27952 return (rval); 27953 } 27954 27955 27956 /* 27957 * Function: sr_read_tochdr() 27958 * 27959 * Description: This routine is the driver entry point for handling CD-ROM 27960 * ioctl requests to read the Table of Contents (TOC) header 27961 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27962 * and ending track numbers 27963 * 27964 * Arguments: dev - the device 'dev_t' 27965 * data - pointer to user provided toc header structure, 27966 * specifying the starting and ending track numbers. 27967 * flag - this argument is a pass through to ddi_copyxxx() 27968 * directly from the mode argument of ioctl(). 27969 * 27970 * Return Code: the code returned by sd_send_scsi_cmd() 27971 * EFAULT if ddi_copyxxx() fails 27972 * ENXIO if fail ddi_get_soft_state 27973 * EINVAL if data pointer is NULL 27974 */ 27975 27976 static int 27977 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27978 { 27979 struct sd_lun *un; 27980 struct uscsi_cmd *com; 27981 struct cdrom_tochdr toc_header; 27982 struct cdrom_tochdr *hdr = &toc_header; 27983 char cdb[CDB_GROUP1]; 27984 int rval; 27985 caddr_t buffer; 27986 27987 if (data == NULL) { 27988 return (EINVAL); 27989 } 27990 27991 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27992 (un->un_state == SD_STATE_OFFLINE)) { 27993 return (ENXIO); 27994 } 27995 27996 buffer = kmem_zalloc(4, KM_SLEEP); 27997 bzero(cdb, CDB_GROUP1); 27998 cdb[0] = SCMD_READ_TOC; 27999 /* 28000 * Specifying a track number of 0x00 in the READ TOC command indicates 28001 * that the TOC header should be returned 28002 */ 28003 cdb[6] = 0x00; 28004 /* 28005 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28006 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28007 */ 28008 cdb[8] = 0x04; 28009 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28010 com->uscsi_cdb = cdb; 28011 com->uscsi_cdblen = CDB_GROUP1; 28012 com->uscsi_bufaddr = buffer; 28013 com->uscsi_buflen = 0x04; 28014 com->uscsi_timeout = 300; 28015 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28016 28017 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28018 SD_PATH_STANDARD); 28019 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28020 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28021 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28022 } else { 28023 hdr->cdth_trk0 = buffer[2]; 28024 hdr->cdth_trk1 = buffer[3]; 28025 } 28026 kmem_free(buffer, 4); 28027 kmem_free(com, sizeof (*com)); 28028 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28029 return (EFAULT); 28030 } 28031 return (rval); 28032 } 28033 28034 28035 /* 28036 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28037 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28038 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28039 * digital audio and extended architecture digital audio. These modes are 28040 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28041 * MMC specs. 28042 * 28043 * In addition to support for the various data formats these routines also 28044 * include support for devices that implement only the direct access READ 28045 * commands (0x08, 0x28), devices that implement the READ_CD commands 28046 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28047 * READ CDXA commands (0xD8, 0xDB) 28048 */ 28049 28050 /* 28051 * Function: sr_read_mode1() 28052 * 28053 * Description: This routine is the driver entry point for handling CD-ROM 28054 * ioctl read mode1 requests (CDROMREADMODE1). 28055 * 28056 * Arguments: dev - the device 'dev_t' 28057 * data - pointer to user provided cd read structure specifying 28058 * the lba buffer address and length. 28059 * flag - this argument is a pass through to ddi_copyxxx() 28060 * directly from the mode argument of ioctl(). 28061 * 28062 * Return Code: the code returned by sd_send_scsi_cmd() 28063 * EFAULT if ddi_copyxxx() fails 28064 * ENXIO if fail ddi_get_soft_state 28065 * EINVAL if data pointer is NULL 28066 */ 28067 28068 static int 28069 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28070 { 28071 struct sd_lun *un; 28072 struct cdrom_read mode1_struct; 28073 struct cdrom_read *mode1 = &mode1_struct; 28074 int rval; 28075 sd_ssc_t *ssc; 28076 28077 #ifdef _MULTI_DATAMODEL 28078 /* To support ILP32 applications in an LP64 world */ 28079 struct cdrom_read32 cdrom_read32; 28080 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28081 #endif /* _MULTI_DATAMODEL */ 28082 28083 if (data == NULL) { 28084 return (EINVAL); 28085 } 28086 28087 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28088 (un->un_state == SD_STATE_OFFLINE)) { 28089 return (ENXIO); 28090 } 28091 28092 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28093 "sd_read_mode1: entry: un:0x%p\n", un); 28094 28095 #ifdef _MULTI_DATAMODEL 28096 switch (ddi_model_convert_from(flag & FMODELS)) { 28097 case DDI_MODEL_ILP32: 28098 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28099 return (EFAULT); 28100 } 28101 /* Convert the ILP32 uscsi data from the application to LP64 */ 28102 cdrom_read32tocdrom_read(cdrd32, mode1); 28103 break; 28104 case DDI_MODEL_NONE: 28105 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28106 return (EFAULT); 28107 } 28108 } 28109 #else /* ! _MULTI_DATAMODEL */ 28110 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28111 return (EFAULT); 28112 } 28113 #endif /* _MULTI_DATAMODEL */ 28114 28115 ssc = sd_ssc_init(un); 28116 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr, 28117 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28118 sd_ssc_fini(ssc); 28119 28120 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28121 "sd_read_mode1: exit: un:0x%p\n", un); 28122 28123 return (rval); 28124 } 28125 28126 28127 /* 28128 * Function: sr_read_cd_mode2() 28129 * 28130 * Description: This routine is the driver entry point for handling CD-ROM 28131 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28132 * support the READ CD (0xBE) command or the 1st generation 28133 * READ CD (0xD4) command. 28134 * 28135 * Arguments: dev - the device 'dev_t' 28136 * data - pointer to user provided cd read structure specifying 28137 * the lba buffer address and length. 28138 * flag - this argument is a pass through to ddi_copyxxx() 28139 * directly from the mode argument of ioctl(). 28140 * 28141 * Return Code: the code returned by sd_send_scsi_cmd() 28142 * EFAULT if ddi_copyxxx() fails 28143 * ENXIO if fail ddi_get_soft_state 28144 * EINVAL if data pointer is NULL 28145 */ 28146 28147 static int 28148 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28149 { 28150 struct sd_lun *un; 28151 struct uscsi_cmd *com; 28152 struct cdrom_read mode2_struct; 28153 struct cdrom_read *mode2 = &mode2_struct; 28154 uchar_t cdb[CDB_GROUP5]; 28155 int nblocks; 28156 int rval; 28157 #ifdef _MULTI_DATAMODEL 28158 /* To support ILP32 applications in an LP64 world */ 28159 struct cdrom_read32 cdrom_read32; 28160 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28161 #endif /* _MULTI_DATAMODEL */ 28162 28163 if (data == NULL) { 28164 return (EINVAL); 28165 } 28166 28167 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28168 (un->un_state == SD_STATE_OFFLINE)) { 28169 return (ENXIO); 28170 } 28171 28172 #ifdef _MULTI_DATAMODEL 28173 switch (ddi_model_convert_from(flag & FMODELS)) { 28174 case DDI_MODEL_ILP32: 28175 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28176 return (EFAULT); 28177 } 28178 /* Convert the ILP32 uscsi data from the application to LP64 */ 28179 cdrom_read32tocdrom_read(cdrd32, mode2); 28180 break; 28181 case DDI_MODEL_NONE: 28182 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28183 return (EFAULT); 28184 } 28185 break; 28186 } 28187 28188 #else /* ! _MULTI_DATAMODEL */ 28189 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28190 return (EFAULT); 28191 } 28192 #endif /* _MULTI_DATAMODEL */ 28193 28194 bzero(cdb, sizeof (cdb)); 28195 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28196 /* Read command supported by 1st generation atapi drives */ 28197 cdb[0] = SCMD_READ_CDD4; 28198 } else { 28199 /* Universal CD Access Command */ 28200 cdb[0] = SCMD_READ_CD; 28201 } 28202 28203 /* 28204 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28205 */ 28206 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28207 28208 /* set the start address */ 28209 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28210 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28211 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28212 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28213 28214 /* set the transfer length */ 28215 nblocks = mode2->cdread_buflen / 2336; 28216 cdb[6] = (uchar_t)(nblocks >> 16); 28217 cdb[7] = (uchar_t)(nblocks >> 8); 28218 cdb[8] = (uchar_t)nblocks; 28219 28220 /* set the filter bits */ 28221 cdb[9] = CDROM_READ_CD_USERDATA; 28222 28223 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28224 com->uscsi_cdb = (caddr_t)cdb; 28225 com->uscsi_cdblen = sizeof (cdb); 28226 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28227 com->uscsi_buflen = mode2->cdread_buflen; 28228 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28229 28230 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28231 SD_PATH_STANDARD); 28232 kmem_free(com, sizeof (*com)); 28233 return (rval); 28234 } 28235 28236 28237 /* 28238 * Function: sr_read_mode2() 28239 * 28240 * Description: This routine is the driver entry point for handling CD-ROM 28241 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28242 * do not support the READ CD (0xBE) command. 28243 * 28244 * Arguments: dev - the device 'dev_t' 28245 * data - pointer to user provided cd read structure specifying 28246 * the lba buffer address and length. 28247 * flag - this argument is a pass through to ddi_copyxxx() 28248 * directly from the mode argument of ioctl(). 28249 * 28250 * Return Code: the code returned by sd_send_scsi_cmd() 28251 * EFAULT if ddi_copyxxx() fails 28252 * ENXIO if fail ddi_get_soft_state 28253 * EINVAL if data pointer is NULL 28254 * EIO if fail to reset block size 28255 * EAGAIN if commands are in progress in the driver 28256 */ 28257 28258 static int 28259 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28260 { 28261 struct sd_lun *un; 28262 struct cdrom_read mode2_struct; 28263 struct cdrom_read *mode2 = &mode2_struct; 28264 int rval; 28265 uint32_t restore_blksize; 28266 struct uscsi_cmd *com; 28267 uchar_t cdb[CDB_GROUP0]; 28268 int nblocks; 28269 28270 #ifdef _MULTI_DATAMODEL 28271 /* To support ILP32 applications in an LP64 world */ 28272 struct cdrom_read32 cdrom_read32; 28273 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28274 #endif /* _MULTI_DATAMODEL */ 28275 28276 if (data == NULL) { 28277 return (EINVAL); 28278 } 28279 28280 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28281 (un->un_state == SD_STATE_OFFLINE)) { 28282 return (ENXIO); 28283 } 28284 28285 /* 28286 * Because this routine will update the device and driver block size 28287 * being used we want to make sure there are no commands in progress. 28288 * If commands are in progress the user will have to try again. 28289 * 28290 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28291 * in sdioctl to protect commands from sdioctl through to the top of 28292 * sd_uscsi_strategy. See sdioctl for details. 28293 */ 28294 mutex_enter(SD_MUTEX(un)); 28295 if (un->un_ncmds_in_driver != 1) { 28296 mutex_exit(SD_MUTEX(un)); 28297 return (EAGAIN); 28298 } 28299 mutex_exit(SD_MUTEX(un)); 28300 28301 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28302 "sd_read_mode2: entry: un:0x%p\n", un); 28303 28304 #ifdef _MULTI_DATAMODEL 28305 switch (ddi_model_convert_from(flag & FMODELS)) { 28306 case DDI_MODEL_ILP32: 28307 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28308 return (EFAULT); 28309 } 28310 /* Convert the ILP32 uscsi data from the application to LP64 */ 28311 cdrom_read32tocdrom_read(cdrd32, mode2); 28312 break; 28313 case DDI_MODEL_NONE: 28314 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28315 return (EFAULT); 28316 } 28317 break; 28318 } 28319 #else /* ! _MULTI_DATAMODEL */ 28320 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28321 return (EFAULT); 28322 } 28323 #endif /* _MULTI_DATAMODEL */ 28324 28325 /* Store the current target block size for restoration later */ 28326 restore_blksize = un->un_tgt_blocksize; 28327 28328 /* Change the device and soft state target block size to 2336 */ 28329 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28330 rval = EIO; 28331 goto done; 28332 } 28333 28334 28335 bzero(cdb, sizeof (cdb)); 28336 28337 /* set READ operation */ 28338 cdb[0] = SCMD_READ; 28339 28340 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28341 mode2->cdread_lba >>= 2; 28342 28343 /* set the start address */ 28344 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28345 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28346 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28347 28348 /* set the transfer length */ 28349 nblocks = mode2->cdread_buflen / 2336; 28350 cdb[4] = (uchar_t)nblocks & 0xFF; 28351 28352 /* build command */ 28353 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28354 com->uscsi_cdb = (caddr_t)cdb; 28355 com->uscsi_cdblen = sizeof (cdb); 28356 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28357 com->uscsi_buflen = mode2->cdread_buflen; 28358 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28359 28360 /* 28361 * Issue SCSI command with user space address for read buffer. 28362 * 28363 * This sends the command through main channel in the driver. 28364 * 28365 * Since this is accessed via an IOCTL call, we go through the 28366 * standard path, so that if the device was powered down, then 28367 * it would be 'awakened' to handle the command. 28368 */ 28369 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28370 SD_PATH_STANDARD); 28371 28372 kmem_free(com, sizeof (*com)); 28373 28374 /* Restore the device and soft state target block size */ 28375 if (sr_sector_mode(dev, restore_blksize) != 0) { 28376 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28377 "can't do switch back to mode 1\n"); 28378 /* 28379 * If sd_send_scsi_READ succeeded we still need to report 28380 * an error because we failed to reset the block size 28381 */ 28382 if (rval == 0) { 28383 rval = EIO; 28384 } 28385 } 28386 28387 done: 28388 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28389 "sd_read_mode2: exit: un:0x%p\n", un); 28390 28391 return (rval); 28392 } 28393 28394 28395 /* 28396 * Function: sr_sector_mode() 28397 * 28398 * Description: This utility function is used by sr_read_mode2 to set the target 28399 * block size based on the user specified size. This is a legacy 28400 * implementation based upon a vendor specific mode page 28401 * 28402 * Arguments: dev - the device 'dev_t' 28403 * data - flag indicating if block size is being set to 2336 or 28404 * 512. 28405 * 28406 * Return Code: the code returned by sd_send_scsi_cmd() 28407 * EFAULT if ddi_copyxxx() fails 28408 * ENXIO if fail ddi_get_soft_state 28409 * EINVAL if data pointer is NULL 28410 */ 28411 28412 static int 28413 sr_sector_mode(dev_t dev, uint32_t blksize) 28414 { 28415 struct sd_lun *un; 28416 uchar_t *sense; 28417 uchar_t *select; 28418 int rval; 28419 sd_ssc_t *ssc; 28420 28421 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28422 (un->un_state == SD_STATE_OFFLINE)) { 28423 return (ENXIO); 28424 } 28425 28426 sense = kmem_zalloc(20, KM_SLEEP); 28427 28428 /* Note: This is a vendor specific mode page (0x81) */ 28429 ssc = sd_ssc_init(un); 28430 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81, 28431 SD_PATH_STANDARD); 28432 sd_ssc_fini(ssc); 28433 if (rval != 0) { 28434 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28435 "sr_sector_mode: Mode Sense failed\n"); 28436 kmem_free(sense, 20); 28437 return (rval); 28438 } 28439 select = kmem_zalloc(20, KM_SLEEP); 28440 select[3] = 0x08; 28441 select[10] = ((blksize >> 8) & 0xff); 28442 select[11] = (blksize & 0xff); 28443 select[12] = 0x01; 28444 select[13] = 0x06; 28445 select[14] = sense[14]; 28446 select[15] = sense[15]; 28447 if (blksize == SD_MODE2_BLKSIZE) { 28448 select[14] |= 0x01; 28449 } 28450 28451 ssc = sd_ssc_init(un); 28452 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20, 28453 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28454 sd_ssc_fini(ssc); 28455 if (rval != 0) { 28456 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28457 "sr_sector_mode: Mode Select failed\n"); 28458 } else { 28459 /* 28460 * Only update the softstate block size if we successfully 28461 * changed the device block mode. 28462 */ 28463 mutex_enter(SD_MUTEX(un)); 28464 sd_update_block_info(un, blksize, 0); 28465 mutex_exit(SD_MUTEX(un)); 28466 } 28467 kmem_free(sense, 20); 28468 kmem_free(select, 20); 28469 return (rval); 28470 } 28471 28472 28473 /* 28474 * Function: sr_read_cdda() 28475 * 28476 * Description: This routine is the driver entry point for handling CD-ROM 28477 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28478 * the target supports CDDA these requests are handled via a vendor 28479 * specific command (0xD8) If the target does not support CDDA 28480 * these requests are handled via the READ CD command (0xBE). 28481 * 28482 * Arguments: dev - the device 'dev_t' 28483 * data - pointer to user provided CD-DA structure specifying 28484 * the track starting address, transfer length, and 28485 * subcode options. 28486 * flag - this argument is a pass through to ddi_copyxxx() 28487 * directly from the mode argument of ioctl(). 28488 * 28489 * Return Code: the code returned by sd_send_scsi_cmd() 28490 * EFAULT if ddi_copyxxx() fails 28491 * ENXIO if fail ddi_get_soft_state 28492 * EINVAL if invalid arguments are provided 28493 * ENOTTY 28494 */ 28495 28496 static int 28497 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28498 { 28499 struct sd_lun *un; 28500 struct uscsi_cmd *com; 28501 struct cdrom_cdda *cdda; 28502 int rval; 28503 size_t buflen; 28504 char cdb[CDB_GROUP5]; 28505 28506 #ifdef _MULTI_DATAMODEL 28507 /* To support ILP32 applications in an LP64 world */ 28508 struct cdrom_cdda32 cdrom_cdda32; 28509 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28510 #endif /* _MULTI_DATAMODEL */ 28511 28512 if (data == NULL) { 28513 return (EINVAL); 28514 } 28515 28516 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28517 return (ENXIO); 28518 } 28519 28520 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28521 28522 #ifdef _MULTI_DATAMODEL 28523 switch (ddi_model_convert_from(flag & FMODELS)) { 28524 case DDI_MODEL_ILP32: 28525 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28526 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28527 "sr_read_cdda: ddi_copyin Failed\n"); 28528 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28529 return (EFAULT); 28530 } 28531 /* Convert the ILP32 uscsi data from the application to LP64 */ 28532 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28533 break; 28534 case DDI_MODEL_NONE: 28535 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28536 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28537 "sr_read_cdda: ddi_copyin Failed\n"); 28538 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28539 return (EFAULT); 28540 } 28541 break; 28542 } 28543 #else /* ! _MULTI_DATAMODEL */ 28544 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28545 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28546 "sr_read_cdda: ddi_copyin Failed\n"); 28547 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28548 return (EFAULT); 28549 } 28550 #endif /* _MULTI_DATAMODEL */ 28551 28552 /* 28553 * Since MMC-2 expects max 3 bytes for length, check if the 28554 * length input is greater than 3 bytes 28555 */ 28556 if ((cdda->cdda_length & 0xFF000000) != 0) { 28557 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28558 "cdrom transfer length too large: %d (limit %d)\n", 28559 cdda->cdda_length, 0xFFFFFF); 28560 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28561 return (EINVAL); 28562 } 28563 28564 switch (cdda->cdda_subcode) { 28565 case CDROM_DA_NO_SUBCODE: 28566 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28567 break; 28568 case CDROM_DA_SUBQ: 28569 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28570 break; 28571 case CDROM_DA_ALL_SUBCODE: 28572 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28573 break; 28574 case CDROM_DA_SUBCODE_ONLY: 28575 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28576 break; 28577 default: 28578 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28579 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28580 cdda->cdda_subcode); 28581 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28582 return (EINVAL); 28583 } 28584 28585 /* Build and send the command */ 28586 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28587 bzero(cdb, CDB_GROUP5); 28588 28589 if (un->un_f_cfg_cdda == TRUE) { 28590 cdb[0] = (char)SCMD_READ_CD; 28591 cdb[1] = 0x04; 28592 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28593 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28594 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28595 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28596 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28597 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28598 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28599 cdb[9] = 0x10; 28600 switch (cdda->cdda_subcode) { 28601 case CDROM_DA_NO_SUBCODE : 28602 cdb[10] = 0x0; 28603 break; 28604 case CDROM_DA_SUBQ : 28605 cdb[10] = 0x2; 28606 break; 28607 case CDROM_DA_ALL_SUBCODE : 28608 cdb[10] = 0x1; 28609 break; 28610 case CDROM_DA_SUBCODE_ONLY : 28611 /* FALLTHROUGH */ 28612 default : 28613 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28614 kmem_free(com, sizeof (*com)); 28615 return (ENOTTY); 28616 } 28617 } else { 28618 cdb[0] = (char)SCMD_READ_CDDA; 28619 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28620 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28621 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28622 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28623 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28624 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28625 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28626 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28627 cdb[10] = cdda->cdda_subcode; 28628 } 28629 28630 com->uscsi_cdb = cdb; 28631 com->uscsi_cdblen = CDB_GROUP5; 28632 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28633 com->uscsi_buflen = buflen; 28634 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28635 28636 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28637 SD_PATH_STANDARD); 28638 28639 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28640 kmem_free(com, sizeof (*com)); 28641 return (rval); 28642 } 28643 28644 28645 /* 28646 * Function: sr_read_cdxa() 28647 * 28648 * Description: This routine is the driver entry point for handling CD-ROM 28649 * ioctl requests to return CD-XA (Extended Architecture) data. 28650 * (CDROMCDXA). 28651 * 28652 * Arguments: dev - the device 'dev_t' 28653 * data - pointer to user provided CD-XA structure specifying 28654 * the data starting address, transfer length, and format 28655 * flag - this argument is a pass through to ddi_copyxxx() 28656 * directly from the mode argument of ioctl(). 28657 * 28658 * Return Code: the code returned by sd_send_scsi_cmd() 28659 * EFAULT if ddi_copyxxx() fails 28660 * ENXIO if fail ddi_get_soft_state 28661 * EINVAL if data pointer is NULL 28662 */ 28663 28664 static int 28665 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28666 { 28667 struct sd_lun *un; 28668 struct uscsi_cmd *com; 28669 struct cdrom_cdxa *cdxa; 28670 int rval; 28671 size_t buflen; 28672 char cdb[CDB_GROUP5]; 28673 uchar_t read_flags; 28674 28675 #ifdef _MULTI_DATAMODEL 28676 /* To support ILP32 applications in an LP64 world */ 28677 struct cdrom_cdxa32 cdrom_cdxa32; 28678 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28679 #endif /* _MULTI_DATAMODEL */ 28680 28681 if (data == NULL) { 28682 return (EINVAL); 28683 } 28684 28685 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28686 return (ENXIO); 28687 } 28688 28689 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28690 28691 #ifdef _MULTI_DATAMODEL 28692 switch (ddi_model_convert_from(flag & FMODELS)) { 28693 case DDI_MODEL_ILP32: 28694 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28695 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28696 return (EFAULT); 28697 } 28698 /* 28699 * Convert the ILP32 uscsi data from the 28700 * application to LP64 for internal use. 28701 */ 28702 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28703 break; 28704 case DDI_MODEL_NONE: 28705 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28706 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28707 return (EFAULT); 28708 } 28709 break; 28710 } 28711 #else /* ! _MULTI_DATAMODEL */ 28712 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28713 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28714 return (EFAULT); 28715 } 28716 #endif /* _MULTI_DATAMODEL */ 28717 28718 /* 28719 * Since MMC-2 expects max 3 bytes for length, check if the 28720 * length input is greater than 3 bytes 28721 */ 28722 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28723 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28724 "cdrom transfer length too large: %d (limit %d)\n", 28725 cdxa->cdxa_length, 0xFFFFFF); 28726 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28727 return (EINVAL); 28728 } 28729 28730 switch (cdxa->cdxa_format) { 28731 case CDROM_XA_DATA: 28732 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28733 read_flags = 0x10; 28734 break; 28735 case CDROM_XA_SECTOR_DATA: 28736 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28737 read_flags = 0xf8; 28738 break; 28739 case CDROM_XA_DATA_W_ERROR: 28740 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28741 read_flags = 0xfc; 28742 break; 28743 default: 28744 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28745 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28746 cdxa->cdxa_format); 28747 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28748 return (EINVAL); 28749 } 28750 28751 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28752 bzero(cdb, CDB_GROUP5); 28753 if (un->un_f_mmc_cap == TRUE) { 28754 cdb[0] = (char)SCMD_READ_CD; 28755 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28756 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28757 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28758 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28759 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28760 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28761 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28762 cdb[9] = (char)read_flags; 28763 } else { 28764 /* 28765 * Note: A vendor specific command (0xDB) is being used her to 28766 * request a read of all subcodes. 28767 */ 28768 cdb[0] = (char)SCMD_READ_CDXA; 28769 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28770 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28771 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28772 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28773 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28774 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28775 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28776 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28777 cdb[10] = cdxa->cdxa_format; 28778 } 28779 com->uscsi_cdb = cdb; 28780 com->uscsi_cdblen = CDB_GROUP5; 28781 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28782 com->uscsi_buflen = buflen; 28783 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28784 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28785 SD_PATH_STANDARD); 28786 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28787 kmem_free(com, sizeof (*com)); 28788 return (rval); 28789 } 28790 28791 28792 /* 28793 * Function: sr_eject() 28794 * 28795 * Description: This routine is the driver entry point for handling CD-ROM 28796 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28797 * 28798 * Arguments: dev - the device 'dev_t' 28799 * 28800 * Return Code: the code returned by sd_send_scsi_cmd() 28801 */ 28802 28803 static int 28804 sr_eject(dev_t dev) 28805 { 28806 struct sd_lun *un; 28807 int rval; 28808 sd_ssc_t *ssc; 28809 28810 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28811 (un->un_state == SD_STATE_OFFLINE)) { 28812 return (ENXIO); 28813 } 28814 28815 /* 28816 * To prevent race conditions with the eject 28817 * command, keep track of an eject command as 28818 * it progresses. If we are already handling 28819 * an eject command in the driver for the given 28820 * unit and another request to eject is received 28821 * immediately return EAGAIN so we don't lose 28822 * the command if the current eject command fails. 28823 */ 28824 mutex_enter(SD_MUTEX(un)); 28825 if (un->un_f_ejecting == TRUE) { 28826 mutex_exit(SD_MUTEX(un)); 28827 return (EAGAIN); 28828 } 28829 un->un_f_ejecting = TRUE; 28830 mutex_exit(SD_MUTEX(un)); 28831 28832 ssc = sd_ssc_init(un); 28833 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 28834 SD_PATH_STANDARD); 28835 sd_ssc_fini(ssc); 28836 28837 if (rval != 0) { 28838 mutex_enter(SD_MUTEX(un)); 28839 un->un_f_ejecting = FALSE; 28840 mutex_exit(SD_MUTEX(un)); 28841 return (rval); 28842 } 28843 28844 ssc = sd_ssc_init(un); 28845 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 28846 SD_TARGET_EJECT, SD_PATH_STANDARD); 28847 sd_ssc_fini(ssc); 28848 28849 if (rval == 0) { 28850 mutex_enter(SD_MUTEX(un)); 28851 sr_ejected(un); 28852 un->un_mediastate = DKIO_EJECTED; 28853 un->un_f_ejecting = FALSE; 28854 cv_broadcast(&un->un_state_cv); 28855 mutex_exit(SD_MUTEX(un)); 28856 } else { 28857 mutex_enter(SD_MUTEX(un)); 28858 un->un_f_ejecting = FALSE; 28859 mutex_exit(SD_MUTEX(un)); 28860 } 28861 return (rval); 28862 } 28863 28864 28865 /* 28866 * Function: sr_ejected() 28867 * 28868 * Description: This routine updates the soft state structure to invalidate the 28869 * geometry information after the media has been ejected or a 28870 * media eject has been detected. 28871 * 28872 * Arguments: un - driver soft state (unit) structure 28873 */ 28874 28875 static void 28876 sr_ejected(struct sd_lun *un) 28877 { 28878 struct sd_errstats *stp; 28879 28880 ASSERT(un != NULL); 28881 ASSERT(mutex_owned(SD_MUTEX(un))); 28882 28883 un->un_f_blockcount_is_valid = FALSE; 28884 un->un_f_tgt_blocksize_is_valid = FALSE; 28885 mutex_exit(SD_MUTEX(un)); 28886 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 28887 mutex_enter(SD_MUTEX(un)); 28888 28889 if (un->un_errstats != NULL) { 28890 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28891 stp->sd_capacity.value.ui64 = 0; 28892 } 28893 } 28894 28895 28896 /* 28897 * Function: sr_check_wp() 28898 * 28899 * Description: This routine checks the write protection of a removable 28900 * media disk and hotpluggable devices via the write protect bit of 28901 * the Mode Page Header device specific field. Some devices choke 28902 * on unsupported mode page. In order to workaround this issue, 28903 * this routine has been implemented to use 0x3f mode page(request 28904 * for all pages) for all device types. 28905 * 28906 * Arguments: dev - the device 'dev_t' 28907 * 28908 * Return Code: int indicating if the device is write protected (1) or not (0) 28909 * 28910 * Context: Kernel thread. 28911 * 28912 */ 28913 28914 static int 28915 sr_check_wp(dev_t dev) 28916 { 28917 struct sd_lun *un; 28918 uchar_t device_specific; 28919 uchar_t *sense; 28920 int hdrlen; 28921 int rval = FALSE; 28922 int status; 28923 sd_ssc_t *ssc; 28924 28925 /* 28926 * Note: The return codes for this routine should be reworked to 28927 * properly handle the case of a NULL softstate. 28928 */ 28929 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28930 return (FALSE); 28931 } 28932 28933 if (un->un_f_cfg_is_atapi == TRUE) { 28934 /* 28935 * The mode page contents are not required; set the allocation 28936 * length for the mode page header only 28937 */ 28938 hdrlen = MODE_HEADER_LENGTH_GRP2; 28939 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28940 ssc = sd_ssc_init(un); 28941 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen, 28942 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28943 sd_ssc_fini(ssc); 28944 if (status != 0) 28945 goto err_exit; 28946 device_specific = 28947 ((struct mode_header_grp2 *)sense)->device_specific; 28948 } else { 28949 hdrlen = MODE_HEADER_LENGTH; 28950 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28951 ssc = sd_ssc_init(un); 28952 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen, 28953 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28954 sd_ssc_fini(ssc); 28955 if (status != 0) 28956 goto err_exit; 28957 device_specific = 28958 ((struct mode_header *)sense)->device_specific; 28959 } 28960 28961 28962 /* 28963 * Write protect mode sense failed; not all disks 28964 * understand this query. Return FALSE assuming that 28965 * these devices are not writable. 28966 */ 28967 if (device_specific & WRITE_PROTECT) { 28968 rval = TRUE; 28969 } 28970 28971 err_exit: 28972 kmem_free(sense, hdrlen); 28973 return (rval); 28974 } 28975 28976 /* 28977 * Function: sr_volume_ctrl() 28978 * 28979 * Description: This routine is the driver entry point for handling CD-ROM 28980 * audio output volume ioctl requests. (CDROMVOLCTRL) 28981 * 28982 * Arguments: dev - the device 'dev_t' 28983 * data - pointer to user audio volume control structure 28984 * flag - this argument is a pass through to ddi_copyxxx() 28985 * directly from the mode argument of ioctl(). 28986 * 28987 * Return Code: the code returned by sd_send_scsi_cmd() 28988 * EFAULT if ddi_copyxxx() fails 28989 * ENXIO if fail ddi_get_soft_state 28990 * EINVAL if data pointer is NULL 28991 * 28992 */ 28993 28994 static int 28995 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28996 { 28997 struct sd_lun *un; 28998 struct cdrom_volctrl volume; 28999 struct cdrom_volctrl *vol = &volume; 29000 uchar_t *sense_page; 29001 uchar_t *select_page; 29002 uchar_t *sense; 29003 uchar_t *select; 29004 int sense_buflen; 29005 int select_buflen; 29006 int rval; 29007 sd_ssc_t *ssc; 29008 29009 if (data == NULL) { 29010 return (EINVAL); 29011 } 29012 29013 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29014 (un->un_state == SD_STATE_OFFLINE)) { 29015 return (ENXIO); 29016 } 29017 29018 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29019 return (EFAULT); 29020 } 29021 29022 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29023 struct mode_header_grp2 *sense_mhp; 29024 struct mode_header_grp2 *select_mhp; 29025 int bd_len; 29026 29027 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29028 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29029 MODEPAGE_AUDIO_CTRL_LEN; 29030 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29031 select = kmem_zalloc(select_buflen, KM_SLEEP); 29032 ssc = sd_ssc_init(un); 29033 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 29034 sense_buflen, MODEPAGE_AUDIO_CTRL, 29035 SD_PATH_STANDARD); 29036 sd_ssc_fini(ssc); 29037 29038 if (rval != 0) { 29039 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29040 "sr_volume_ctrl: Mode Sense Failed\n"); 29041 kmem_free(sense, sense_buflen); 29042 kmem_free(select, select_buflen); 29043 return (rval); 29044 } 29045 sense_mhp = (struct mode_header_grp2 *)sense; 29046 select_mhp = (struct mode_header_grp2 *)select; 29047 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29048 sense_mhp->bdesc_length_lo; 29049 if (bd_len > MODE_BLK_DESC_LENGTH) { 29050 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29051 "sr_volume_ctrl: Mode Sense returned invalid " 29052 "block descriptor length\n"); 29053 kmem_free(sense, sense_buflen); 29054 kmem_free(select, select_buflen); 29055 return (EIO); 29056 } 29057 sense_page = (uchar_t *) 29058 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29059 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29060 select_mhp->length_msb = 0; 29061 select_mhp->length_lsb = 0; 29062 select_mhp->bdesc_length_hi = 0; 29063 select_mhp->bdesc_length_lo = 0; 29064 } else { 29065 struct mode_header *sense_mhp, *select_mhp; 29066 29067 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29068 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29069 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29070 select = kmem_zalloc(select_buflen, KM_SLEEP); 29071 ssc = sd_ssc_init(un); 29072 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 29073 sense_buflen, MODEPAGE_AUDIO_CTRL, 29074 SD_PATH_STANDARD); 29075 sd_ssc_fini(ssc); 29076 29077 if (rval != 0) { 29078 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29079 "sr_volume_ctrl: Mode Sense Failed\n"); 29080 kmem_free(sense, sense_buflen); 29081 kmem_free(select, select_buflen); 29082 return (rval); 29083 } 29084 sense_mhp = (struct mode_header *)sense; 29085 select_mhp = (struct mode_header *)select; 29086 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29087 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29088 "sr_volume_ctrl: Mode Sense returned invalid " 29089 "block descriptor length\n"); 29090 kmem_free(sense, sense_buflen); 29091 kmem_free(select, select_buflen); 29092 return (EIO); 29093 } 29094 sense_page = (uchar_t *) 29095 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29096 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29097 select_mhp->length = 0; 29098 select_mhp->bdesc_length = 0; 29099 } 29100 /* 29101 * Note: An audio control data structure could be created and overlayed 29102 * on the following in place of the array indexing method implemented. 29103 */ 29104 29105 /* Build the select data for the user volume data */ 29106 select_page[0] = MODEPAGE_AUDIO_CTRL; 29107 select_page[1] = 0xE; 29108 /* Set the immediate bit */ 29109 select_page[2] = 0x04; 29110 /* Zero out reserved fields */ 29111 select_page[3] = 0x00; 29112 select_page[4] = 0x00; 29113 /* Return sense data for fields not to be modified */ 29114 select_page[5] = sense_page[5]; 29115 select_page[6] = sense_page[6]; 29116 select_page[7] = sense_page[7]; 29117 /* Set the user specified volume levels for channel 0 and 1 */ 29118 select_page[8] = 0x01; 29119 select_page[9] = vol->channel0; 29120 select_page[10] = 0x02; 29121 select_page[11] = vol->channel1; 29122 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29123 select_page[12] = sense_page[12]; 29124 select_page[13] = sense_page[13]; 29125 select_page[14] = sense_page[14]; 29126 select_page[15] = sense_page[15]; 29127 29128 ssc = sd_ssc_init(un); 29129 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29130 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select, 29131 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29132 } else { 29133 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 29134 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29135 } 29136 sd_ssc_fini(ssc); 29137 29138 kmem_free(sense, sense_buflen); 29139 kmem_free(select, select_buflen); 29140 return (rval); 29141 } 29142 29143 29144 /* 29145 * Function: sr_read_sony_session_offset() 29146 * 29147 * Description: This routine is the driver entry point for handling CD-ROM 29148 * ioctl requests for session offset information. (CDROMREADOFFSET) 29149 * The address of the first track in the last session of a 29150 * multi-session CD-ROM is returned 29151 * 29152 * Note: This routine uses a vendor specific key value in the 29153 * command control field without implementing any vendor check here 29154 * or in the ioctl routine. 29155 * 29156 * Arguments: dev - the device 'dev_t' 29157 * data - pointer to an int to hold the requested address 29158 * flag - this argument is a pass through to ddi_copyxxx() 29159 * directly from the mode argument of ioctl(). 29160 * 29161 * Return Code: the code returned by sd_send_scsi_cmd() 29162 * EFAULT if ddi_copyxxx() fails 29163 * ENXIO if fail ddi_get_soft_state 29164 * EINVAL if data pointer is NULL 29165 */ 29166 29167 static int 29168 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29169 { 29170 struct sd_lun *un; 29171 struct uscsi_cmd *com; 29172 caddr_t buffer; 29173 char cdb[CDB_GROUP1]; 29174 int session_offset = 0; 29175 int rval; 29176 29177 if (data == NULL) { 29178 return (EINVAL); 29179 } 29180 29181 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29182 (un->un_state == SD_STATE_OFFLINE)) { 29183 return (ENXIO); 29184 } 29185 29186 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29187 bzero(cdb, CDB_GROUP1); 29188 cdb[0] = SCMD_READ_TOC; 29189 /* 29190 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29191 * (4 byte TOC response header + 8 byte response data) 29192 */ 29193 cdb[8] = SONY_SESSION_OFFSET_LEN; 29194 /* Byte 9 is the control byte. A vendor specific value is used */ 29195 cdb[9] = SONY_SESSION_OFFSET_KEY; 29196 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29197 com->uscsi_cdb = cdb; 29198 com->uscsi_cdblen = CDB_GROUP1; 29199 com->uscsi_bufaddr = buffer; 29200 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29201 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29202 29203 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 29204 SD_PATH_STANDARD); 29205 if (rval != 0) { 29206 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29207 kmem_free(com, sizeof (*com)); 29208 return (rval); 29209 } 29210 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29211 session_offset = 29212 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29213 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29214 /* 29215 * Offset returned offset in current lbasize block's. Convert to 29216 * 2k block's to return to the user 29217 */ 29218 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29219 session_offset >>= 2; 29220 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29221 session_offset >>= 1; 29222 } 29223 } 29224 29225 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29226 rval = EFAULT; 29227 } 29228 29229 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29230 kmem_free(com, sizeof (*com)); 29231 return (rval); 29232 } 29233 29234 29235 /* 29236 * Function: sd_wm_cache_constructor() 29237 * 29238 * Description: Cache Constructor for the wmap cache for the read/modify/write 29239 * devices. 29240 * 29241 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29242 * un - sd_lun structure for the device. 29243 * flag - the km flags passed to constructor 29244 * 29245 * Return Code: 0 on success. 29246 * -1 on failure. 29247 */ 29248 29249 /*ARGSUSED*/ 29250 static int 29251 sd_wm_cache_constructor(void *wm, void *un, int flags) 29252 { 29253 bzero(wm, sizeof (struct sd_w_map)); 29254 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29255 return (0); 29256 } 29257 29258 29259 /* 29260 * Function: sd_wm_cache_destructor() 29261 * 29262 * Description: Cache destructor for the wmap cache for the read/modify/write 29263 * devices. 29264 * 29265 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29266 * un - sd_lun structure for the device. 29267 */ 29268 /*ARGSUSED*/ 29269 static void 29270 sd_wm_cache_destructor(void *wm, void *un) 29271 { 29272 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29273 } 29274 29275 29276 /* 29277 * Function: sd_range_lock() 29278 * 29279 * Description: Lock the range of blocks specified as parameter to ensure 29280 * that read, modify write is atomic and no other i/o writes 29281 * to the same location. The range is specified in terms 29282 * of start and end blocks. Block numbers are the actual 29283 * media block numbers and not system. 29284 * 29285 * Arguments: un - sd_lun structure for the device. 29286 * startb - The starting block number 29287 * endb - The end block number 29288 * typ - type of i/o - simple/read_modify_write 29289 * 29290 * Return Code: wm - pointer to the wmap structure. 29291 * 29292 * Context: This routine can sleep. 29293 */ 29294 29295 static struct sd_w_map * 29296 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29297 { 29298 struct sd_w_map *wmp = NULL; 29299 struct sd_w_map *sl_wmp = NULL; 29300 struct sd_w_map *tmp_wmp; 29301 wm_state state = SD_WM_CHK_LIST; 29302 29303 29304 ASSERT(un != NULL); 29305 ASSERT(!mutex_owned(SD_MUTEX(un))); 29306 29307 mutex_enter(SD_MUTEX(un)); 29308 29309 while (state != SD_WM_DONE) { 29310 29311 switch (state) { 29312 case SD_WM_CHK_LIST: 29313 /* 29314 * This is the starting state. Check the wmap list 29315 * to see if the range is currently available. 29316 */ 29317 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29318 /* 29319 * If this is a simple write and no rmw 29320 * i/o is pending then try to lock the 29321 * range as the range should be available. 29322 */ 29323 state = SD_WM_LOCK_RANGE; 29324 } else { 29325 tmp_wmp = sd_get_range(un, startb, endb); 29326 if (tmp_wmp != NULL) { 29327 if ((wmp != NULL) && ONLIST(un, wmp)) { 29328 /* 29329 * Should not keep onlist wmps 29330 * while waiting this macro 29331 * will also do wmp = NULL; 29332 */ 29333 FREE_ONLIST_WMAP(un, wmp); 29334 } 29335 /* 29336 * sl_wmp is the wmap on which wait 29337 * is done, since the tmp_wmp points 29338 * to the inuse wmap, set sl_wmp to 29339 * tmp_wmp and change the state to sleep 29340 */ 29341 sl_wmp = tmp_wmp; 29342 state = SD_WM_WAIT_MAP; 29343 } else { 29344 state = SD_WM_LOCK_RANGE; 29345 } 29346 29347 } 29348 break; 29349 29350 case SD_WM_LOCK_RANGE: 29351 ASSERT(un->un_wm_cache); 29352 /* 29353 * The range need to be locked, try to get a wmap. 29354 * First attempt it with NO_SLEEP, want to avoid a sleep 29355 * if possible as we will have to release the sd mutex 29356 * if we have to sleep. 29357 */ 29358 if (wmp == NULL) 29359 wmp = kmem_cache_alloc(un->un_wm_cache, 29360 KM_NOSLEEP); 29361 if (wmp == NULL) { 29362 mutex_exit(SD_MUTEX(un)); 29363 _NOTE(DATA_READABLE_WITHOUT_LOCK 29364 (sd_lun::un_wm_cache)) 29365 wmp = kmem_cache_alloc(un->un_wm_cache, 29366 KM_SLEEP); 29367 mutex_enter(SD_MUTEX(un)); 29368 /* 29369 * we released the mutex so recheck and go to 29370 * check list state. 29371 */ 29372 state = SD_WM_CHK_LIST; 29373 } else { 29374 /* 29375 * We exit out of state machine since we 29376 * have the wmap. Do the housekeeping first. 29377 * place the wmap on the wmap list if it is not 29378 * on it already and then set the state to done. 29379 */ 29380 wmp->wm_start = startb; 29381 wmp->wm_end = endb; 29382 wmp->wm_flags = typ | SD_WM_BUSY; 29383 if (typ & SD_WTYPE_RMW) { 29384 un->un_rmw_count++; 29385 } 29386 /* 29387 * If not already on the list then link 29388 */ 29389 if (!ONLIST(un, wmp)) { 29390 wmp->wm_next = un->un_wm; 29391 wmp->wm_prev = NULL; 29392 if (wmp->wm_next) 29393 wmp->wm_next->wm_prev = wmp; 29394 un->un_wm = wmp; 29395 } 29396 state = SD_WM_DONE; 29397 } 29398 break; 29399 29400 case SD_WM_WAIT_MAP: 29401 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29402 /* 29403 * Wait is done on sl_wmp, which is set in the 29404 * check_list state. 29405 */ 29406 sl_wmp->wm_wanted_count++; 29407 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29408 sl_wmp->wm_wanted_count--; 29409 /* 29410 * We can reuse the memory from the completed sl_wmp 29411 * lock range for our new lock, but only if noone is 29412 * waiting for it. 29413 */ 29414 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29415 if (sl_wmp->wm_wanted_count == 0) { 29416 if (wmp != NULL) 29417 CHK_N_FREEWMP(un, wmp); 29418 wmp = sl_wmp; 29419 } 29420 sl_wmp = NULL; 29421 /* 29422 * After waking up, need to recheck for availability of 29423 * range. 29424 */ 29425 state = SD_WM_CHK_LIST; 29426 break; 29427 29428 default: 29429 panic("sd_range_lock: " 29430 "Unknown state %d in sd_range_lock", state); 29431 /*NOTREACHED*/ 29432 } /* switch(state) */ 29433 29434 } /* while(state != SD_WM_DONE) */ 29435 29436 mutex_exit(SD_MUTEX(un)); 29437 29438 ASSERT(wmp != NULL); 29439 29440 return (wmp); 29441 } 29442 29443 29444 /* 29445 * Function: sd_get_range() 29446 * 29447 * Description: Find if there any overlapping I/O to this one 29448 * Returns the write-map of 1st such I/O, NULL otherwise. 29449 * 29450 * Arguments: un - sd_lun structure for the device. 29451 * startb - The starting block number 29452 * endb - The end block number 29453 * 29454 * Return Code: wm - pointer to the wmap structure. 29455 */ 29456 29457 static struct sd_w_map * 29458 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29459 { 29460 struct sd_w_map *wmp; 29461 29462 ASSERT(un != NULL); 29463 29464 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29465 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29466 continue; 29467 } 29468 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29469 break; 29470 } 29471 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29472 break; 29473 } 29474 } 29475 29476 return (wmp); 29477 } 29478 29479 29480 /* 29481 * Function: sd_free_inlist_wmap() 29482 * 29483 * Description: Unlink and free a write map struct. 29484 * 29485 * Arguments: un - sd_lun structure for the device. 29486 * wmp - sd_w_map which needs to be unlinked. 29487 */ 29488 29489 static void 29490 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29491 { 29492 ASSERT(un != NULL); 29493 29494 if (un->un_wm == wmp) { 29495 un->un_wm = wmp->wm_next; 29496 } else { 29497 wmp->wm_prev->wm_next = wmp->wm_next; 29498 } 29499 29500 if (wmp->wm_next) { 29501 wmp->wm_next->wm_prev = wmp->wm_prev; 29502 } 29503 29504 wmp->wm_next = wmp->wm_prev = NULL; 29505 29506 kmem_cache_free(un->un_wm_cache, wmp); 29507 } 29508 29509 29510 /* 29511 * Function: sd_range_unlock() 29512 * 29513 * Description: Unlock the range locked by wm. 29514 * Free write map if nobody else is waiting on it. 29515 * 29516 * Arguments: un - sd_lun structure for the device. 29517 * wmp - sd_w_map which needs to be unlinked. 29518 */ 29519 29520 static void 29521 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29522 { 29523 ASSERT(un != NULL); 29524 ASSERT(wm != NULL); 29525 ASSERT(!mutex_owned(SD_MUTEX(un))); 29526 29527 mutex_enter(SD_MUTEX(un)); 29528 29529 if (wm->wm_flags & SD_WTYPE_RMW) { 29530 un->un_rmw_count--; 29531 } 29532 29533 if (wm->wm_wanted_count) { 29534 wm->wm_flags = 0; 29535 /* 29536 * Broadcast that the wmap is available now. 29537 */ 29538 cv_broadcast(&wm->wm_avail); 29539 } else { 29540 /* 29541 * If no one is waiting on the map, it should be free'ed. 29542 */ 29543 sd_free_inlist_wmap(un, wm); 29544 } 29545 29546 mutex_exit(SD_MUTEX(un)); 29547 } 29548 29549 29550 /* 29551 * Function: sd_read_modify_write_task 29552 * 29553 * Description: Called from a taskq thread to initiate the write phase of 29554 * a read-modify-write request. This is used for targets where 29555 * un->un_sys_blocksize != un->un_tgt_blocksize. 29556 * 29557 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29558 * 29559 * Context: Called under taskq thread context. 29560 */ 29561 29562 static void 29563 sd_read_modify_write_task(void *arg) 29564 { 29565 struct sd_mapblocksize_info *bsp; 29566 struct buf *bp; 29567 struct sd_xbuf *xp; 29568 struct sd_lun *un; 29569 29570 bp = arg; /* The bp is given in arg */ 29571 ASSERT(bp != NULL); 29572 29573 /* Get the pointer to the layer-private data struct */ 29574 xp = SD_GET_XBUF(bp); 29575 ASSERT(xp != NULL); 29576 bsp = xp->xb_private; 29577 ASSERT(bsp != NULL); 29578 29579 un = SD_GET_UN(bp); 29580 ASSERT(un != NULL); 29581 ASSERT(!mutex_owned(SD_MUTEX(un))); 29582 29583 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29584 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29585 29586 /* 29587 * This is the write phase of a read-modify-write request, called 29588 * under the context of a taskq thread in response to the completion 29589 * of the read portion of the rmw request completing under interrupt 29590 * context. The write request must be sent from here down the iostart 29591 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29592 * we use the layer index saved in the layer-private data area. 29593 */ 29594 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29595 29596 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29597 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29598 } 29599 29600 29601 /* 29602 * Function: sddump_do_read_of_rmw() 29603 * 29604 * Description: This routine will be called from sddump, If sddump is called 29605 * with an I/O which not aligned on device blocksize boundary 29606 * then the write has to be converted to read-modify-write. 29607 * Do the read part here in order to keep sddump simple. 29608 * Note - That the sd_mutex is held across the call to this 29609 * routine. 29610 * 29611 * Arguments: un - sd_lun 29612 * blkno - block number in terms of media block size. 29613 * nblk - number of blocks. 29614 * bpp - pointer to pointer to the buf structure. On return 29615 * from this function, *bpp points to the valid buffer 29616 * to which the write has to be done. 29617 * 29618 * Return Code: 0 for success or errno-type return code 29619 */ 29620 29621 static int 29622 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29623 struct buf **bpp) 29624 { 29625 int err; 29626 int i; 29627 int rval; 29628 struct buf *bp; 29629 struct scsi_pkt *pkt = NULL; 29630 uint32_t target_blocksize; 29631 29632 ASSERT(un != NULL); 29633 ASSERT(mutex_owned(SD_MUTEX(un))); 29634 29635 target_blocksize = un->un_tgt_blocksize; 29636 29637 mutex_exit(SD_MUTEX(un)); 29638 29639 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29640 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29641 if (bp == NULL) { 29642 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29643 "no resources for dumping; giving up"); 29644 err = ENOMEM; 29645 goto done; 29646 } 29647 29648 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29649 blkno, nblk); 29650 if (rval != 0) { 29651 scsi_free_consistent_buf(bp); 29652 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29653 "no resources for dumping; giving up"); 29654 err = ENOMEM; 29655 goto done; 29656 } 29657 29658 pkt->pkt_flags |= FLAG_NOINTR; 29659 29660 err = EIO; 29661 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29662 29663 /* 29664 * Scsi_poll returns 0 (success) if the command completes and 29665 * the status block is STATUS_GOOD. We should only check 29666 * errors if this condition is not true. Even then we should 29667 * send our own request sense packet only if we have a check 29668 * condition and auto request sense has not been performed by 29669 * the hba. 29670 */ 29671 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29672 29673 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29674 err = 0; 29675 break; 29676 } 29677 29678 /* 29679 * Check CMD_DEV_GONE 1st, give up if device is gone, 29680 * no need to read RQS data. 29681 */ 29682 if (pkt->pkt_reason == CMD_DEV_GONE) { 29683 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29684 "Error while dumping state with rmw..." 29685 "Device is gone\n"); 29686 break; 29687 } 29688 29689 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29690 SD_INFO(SD_LOG_DUMP, un, 29691 "sddump: read failed with CHECK, try # %d\n", i); 29692 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29693 (void) sd_send_polled_RQS(un); 29694 } 29695 29696 continue; 29697 } 29698 29699 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29700 int reset_retval = 0; 29701 29702 SD_INFO(SD_LOG_DUMP, un, 29703 "sddump: read failed with BUSY, try # %d\n", i); 29704 29705 if (un->un_f_lun_reset_enabled == TRUE) { 29706 reset_retval = scsi_reset(SD_ADDRESS(un), 29707 RESET_LUN); 29708 } 29709 if (reset_retval == 0) { 29710 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29711 } 29712 (void) sd_send_polled_RQS(un); 29713 29714 } else { 29715 SD_INFO(SD_LOG_DUMP, un, 29716 "sddump: read failed with 0x%x, try # %d\n", 29717 SD_GET_PKT_STATUS(pkt), i); 29718 mutex_enter(SD_MUTEX(un)); 29719 sd_reset_target(un, pkt); 29720 mutex_exit(SD_MUTEX(un)); 29721 } 29722 29723 /* 29724 * If we are not getting anywhere with lun/target resets, 29725 * let's reset the bus. 29726 */ 29727 if (i > SD_NDUMP_RETRIES/2) { 29728 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29729 (void) sd_send_polled_RQS(un); 29730 } 29731 29732 } 29733 scsi_destroy_pkt(pkt); 29734 29735 if (err != 0) { 29736 scsi_free_consistent_buf(bp); 29737 *bpp = NULL; 29738 } else { 29739 *bpp = bp; 29740 } 29741 29742 done: 29743 mutex_enter(SD_MUTEX(un)); 29744 return (err); 29745 } 29746 29747 29748 /* 29749 * Function: sd_failfast_flushq 29750 * 29751 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29752 * in b_flags and move them onto the failfast queue, then kick 29753 * off a thread to return all bp's on the failfast queue to 29754 * their owners with an error set. 29755 * 29756 * Arguments: un - pointer to the soft state struct for the instance. 29757 * 29758 * Context: may execute in interrupt context. 29759 */ 29760 29761 static void 29762 sd_failfast_flushq(struct sd_lun *un) 29763 { 29764 struct buf *bp; 29765 struct buf *next_waitq_bp; 29766 struct buf *prev_waitq_bp = NULL; 29767 29768 ASSERT(un != NULL); 29769 ASSERT(mutex_owned(SD_MUTEX(un))); 29770 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29771 ASSERT(un->un_failfast_bp == NULL); 29772 29773 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29774 "sd_failfast_flushq: entry: un:0x%p\n", un); 29775 29776 /* 29777 * Check if we should flush all bufs when entering failfast state, or 29778 * just those with B_FAILFAST set. 29779 */ 29780 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29781 /* 29782 * Move *all* bp's on the wait queue to the failfast flush 29783 * queue, including those that do NOT have B_FAILFAST set. 29784 */ 29785 if (un->un_failfast_headp == NULL) { 29786 ASSERT(un->un_failfast_tailp == NULL); 29787 un->un_failfast_headp = un->un_waitq_headp; 29788 } else { 29789 ASSERT(un->un_failfast_tailp != NULL); 29790 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29791 } 29792 29793 un->un_failfast_tailp = un->un_waitq_tailp; 29794 29795 /* update kstat for each bp moved out of the waitq */ 29796 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29797 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29798 } 29799 29800 /* empty the waitq */ 29801 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29802 29803 } else { 29804 /* 29805 * Go thru the wait queue, pick off all entries with 29806 * B_FAILFAST set, and move these onto the failfast queue. 29807 */ 29808 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29809 /* 29810 * Save the pointer to the next bp on the wait queue, 29811 * so we get to it on the next iteration of this loop. 29812 */ 29813 next_waitq_bp = bp->av_forw; 29814 29815 /* 29816 * If this bp from the wait queue does NOT have 29817 * B_FAILFAST set, just move on to the next element 29818 * in the wait queue. Note, this is the only place 29819 * where it is correct to set prev_waitq_bp. 29820 */ 29821 if ((bp->b_flags & B_FAILFAST) == 0) { 29822 prev_waitq_bp = bp; 29823 continue; 29824 } 29825 29826 /* 29827 * Remove the bp from the wait queue. 29828 */ 29829 if (bp == un->un_waitq_headp) { 29830 /* The bp is the first element of the waitq. */ 29831 un->un_waitq_headp = next_waitq_bp; 29832 if (un->un_waitq_headp == NULL) { 29833 /* The wait queue is now empty */ 29834 un->un_waitq_tailp = NULL; 29835 } 29836 } else { 29837 /* 29838 * The bp is either somewhere in the middle 29839 * or at the end of the wait queue. 29840 */ 29841 ASSERT(un->un_waitq_headp != NULL); 29842 ASSERT(prev_waitq_bp != NULL); 29843 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29844 == 0); 29845 if (bp == un->un_waitq_tailp) { 29846 /* bp is the last entry on the waitq. */ 29847 ASSERT(next_waitq_bp == NULL); 29848 un->un_waitq_tailp = prev_waitq_bp; 29849 } 29850 prev_waitq_bp->av_forw = next_waitq_bp; 29851 } 29852 bp->av_forw = NULL; 29853 29854 /* 29855 * update kstat since the bp is moved out of 29856 * the waitq 29857 */ 29858 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29859 29860 /* 29861 * Now put the bp onto the failfast queue. 29862 */ 29863 if (un->un_failfast_headp == NULL) { 29864 /* failfast queue is currently empty */ 29865 ASSERT(un->un_failfast_tailp == NULL); 29866 un->un_failfast_headp = 29867 un->un_failfast_tailp = bp; 29868 } else { 29869 /* Add the bp to the end of the failfast q */ 29870 ASSERT(un->un_failfast_tailp != NULL); 29871 ASSERT(un->un_failfast_tailp->b_flags & 29872 B_FAILFAST); 29873 un->un_failfast_tailp->av_forw = bp; 29874 un->un_failfast_tailp = bp; 29875 } 29876 } 29877 } 29878 29879 /* 29880 * Now return all bp's on the failfast queue to their owners. 29881 */ 29882 while ((bp = un->un_failfast_headp) != NULL) { 29883 29884 un->un_failfast_headp = bp->av_forw; 29885 if (un->un_failfast_headp == NULL) { 29886 un->un_failfast_tailp = NULL; 29887 } 29888 29889 /* 29890 * We want to return the bp with a failure error code, but 29891 * we do not want a call to sd_start_cmds() to occur here, 29892 * so use sd_return_failed_command_no_restart() instead of 29893 * sd_return_failed_command(). 29894 */ 29895 sd_return_failed_command_no_restart(un, bp, EIO); 29896 } 29897 29898 /* Flush the xbuf queues if required. */ 29899 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29900 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29901 } 29902 29903 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29904 "sd_failfast_flushq: exit: un:0x%p\n", un); 29905 } 29906 29907 29908 /* 29909 * Function: sd_failfast_flushq_callback 29910 * 29911 * Description: Return TRUE if the given bp meets the criteria for failfast 29912 * flushing. Used with ddi_xbuf_flushq(9F). 29913 * 29914 * Arguments: bp - ptr to buf struct to be examined. 29915 * 29916 * Context: Any 29917 */ 29918 29919 static int 29920 sd_failfast_flushq_callback(struct buf *bp) 29921 { 29922 /* 29923 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29924 * state is entered; OR (2) the given bp has B_FAILFAST set. 29925 */ 29926 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29927 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29928 } 29929 29930 29931 29932 /* 29933 * Function: sd_setup_next_xfer 29934 * 29935 * Description: Prepare next I/O operation using DMA_PARTIAL 29936 * 29937 */ 29938 29939 static int 29940 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29941 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29942 { 29943 ssize_t num_blks_not_xfered; 29944 daddr_t strt_blk_num; 29945 ssize_t bytes_not_xfered; 29946 int rval; 29947 29948 ASSERT(pkt->pkt_resid == 0); 29949 29950 /* 29951 * Calculate next block number and amount to be transferred. 29952 * 29953 * How much data NOT transfered to the HBA yet. 29954 */ 29955 bytes_not_xfered = xp->xb_dma_resid; 29956 29957 /* 29958 * figure how many blocks NOT transfered to the HBA yet. 29959 */ 29960 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29961 29962 /* 29963 * set starting block number to the end of what WAS transfered. 29964 */ 29965 strt_blk_num = xp->xb_blkno + 29966 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29967 29968 /* 29969 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29970 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29971 * the disk mutex here. 29972 */ 29973 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29974 strt_blk_num, num_blks_not_xfered); 29975 29976 if (rval == 0) { 29977 29978 /* 29979 * Success. 29980 * 29981 * Adjust things if there are still more blocks to be 29982 * transfered. 29983 */ 29984 xp->xb_dma_resid = pkt->pkt_resid; 29985 pkt->pkt_resid = 0; 29986 29987 return (1); 29988 } 29989 29990 /* 29991 * There's really only one possible return value from 29992 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29993 * returns NULL. 29994 */ 29995 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29996 29997 bp->b_resid = bp->b_bcount; 29998 bp->b_flags |= B_ERROR; 29999 30000 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30001 "Error setting up next portion of DMA transfer\n"); 30002 30003 return (0); 30004 } 30005 30006 /* 30007 * Function: sd_panic_for_res_conflict 30008 * 30009 * Description: Call panic with a string formatted with "Reservation Conflict" 30010 * and a human readable identifier indicating the SD instance 30011 * that experienced the reservation conflict. 30012 * 30013 * Arguments: un - pointer to the soft state struct for the instance. 30014 * 30015 * Context: may execute in interrupt context. 30016 */ 30017 30018 #define SD_RESV_CONFLICT_FMT_LEN 40 30019 void 30020 sd_panic_for_res_conflict(struct sd_lun *un) 30021 { 30022 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30023 char path_str[MAXPATHLEN]; 30024 30025 (void) snprintf(panic_str, sizeof (panic_str), 30026 "Reservation Conflict\nDisk: %s", 30027 ddi_pathname(SD_DEVINFO(un), path_str)); 30028 30029 panic(panic_str); 30030 } 30031 30032 /* 30033 * Note: The following sd_faultinjection_ioctl( ) routines implement 30034 * driver support for handling fault injection for error analysis 30035 * causing faults in multiple layers of the driver. 30036 * 30037 */ 30038 30039 #ifdef SD_FAULT_INJECTION 30040 static uint_t sd_fault_injection_on = 0; 30041 30042 /* 30043 * Function: sd_faultinjection_ioctl() 30044 * 30045 * Description: This routine is the driver entry point for handling 30046 * faultinjection ioctls to inject errors into the 30047 * layer model 30048 * 30049 * Arguments: cmd - the ioctl cmd received 30050 * arg - the arguments from user and returns 30051 */ 30052 30053 static void 30054 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30055 30056 uint_t i = 0; 30057 uint_t rval; 30058 30059 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30060 30061 mutex_enter(SD_MUTEX(un)); 30062 30063 switch (cmd) { 30064 case SDIOCRUN: 30065 /* Allow pushed faults to be injected */ 30066 SD_INFO(SD_LOG_SDTEST, un, 30067 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30068 30069 sd_fault_injection_on = 1; 30070 30071 SD_INFO(SD_LOG_IOERR, un, 30072 "sd_faultinjection_ioctl: run finished\n"); 30073 break; 30074 30075 case SDIOCSTART: 30076 /* Start Injection Session */ 30077 SD_INFO(SD_LOG_SDTEST, un, 30078 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30079 30080 sd_fault_injection_on = 0; 30081 un->sd_injection_mask = 0xFFFFFFFF; 30082 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30083 un->sd_fi_fifo_pkt[i] = NULL; 30084 un->sd_fi_fifo_xb[i] = NULL; 30085 un->sd_fi_fifo_un[i] = NULL; 30086 un->sd_fi_fifo_arq[i] = NULL; 30087 } 30088 un->sd_fi_fifo_start = 0; 30089 un->sd_fi_fifo_end = 0; 30090 30091 mutex_enter(&(un->un_fi_mutex)); 30092 un->sd_fi_log[0] = '\0'; 30093 un->sd_fi_buf_len = 0; 30094 mutex_exit(&(un->un_fi_mutex)); 30095 30096 SD_INFO(SD_LOG_IOERR, un, 30097 "sd_faultinjection_ioctl: start finished\n"); 30098 break; 30099 30100 case SDIOCSTOP: 30101 /* Stop Injection Session */ 30102 SD_INFO(SD_LOG_SDTEST, un, 30103 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30104 sd_fault_injection_on = 0; 30105 un->sd_injection_mask = 0x0; 30106 30107 /* Empty stray or unuseds structs from fifo */ 30108 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30109 if (un->sd_fi_fifo_pkt[i] != NULL) { 30110 kmem_free(un->sd_fi_fifo_pkt[i], 30111 sizeof (struct sd_fi_pkt)); 30112 } 30113 if (un->sd_fi_fifo_xb[i] != NULL) { 30114 kmem_free(un->sd_fi_fifo_xb[i], 30115 sizeof (struct sd_fi_xb)); 30116 } 30117 if (un->sd_fi_fifo_un[i] != NULL) { 30118 kmem_free(un->sd_fi_fifo_un[i], 30119 sizeof (struct sd_fi_un)); 30120 } 30121 if (un->sd_fi_fifo_arq[i] != NULL) { 30122 kmem_free(un->sd_fi_fifo_arq[i], 30123 sizeof (struct sd_fi_arq)); 30124 } 30125 un->sd_fi_fifo_pkt[i] = NULL; 30126 un->sd_fi_fifo_un[i] = NULL; 30127 un->sd_fi_fifo_xb[i] = NULL; 30128 un->sd_fi_fifo_arq[i] = NULL; 30129 } 30130 un->sd_fi_fifo_start = 0; 30131 un->sd_fi_fifo_end = 0; 30132 30133 SD_INFO(SD_LOG_IOERR, un, 30134 "sd_faultinjection_ioctl: stop finished\n"); 30135 break; 30136 30137 case SDIOCINSERTPKT: 30138 /* Store a packet struct to be pushed onto fifo */ 30139 SD_INFO(SD_LOG_SDTEST, un, 30140 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30141 30142 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30143 30144 sd_fault_injection_on = 0; 30145 30146 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30147 if (un->sd_fi_fifo_pkt[i] != NULL) { 30148 kmem_free(un->sd_fi_fifo_pkt[i], 30149 sizeof (struct sd_fi_pkt)); 30150 } 30151 if (arg != NULL) { 30152 un->sd_fi_fifo_pkt[i] = 30153 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30154 if (un->sd_fi_fifo_pkt[i] == NULL) { 30155 /* Alloc failed don't store anything */ 30156 break; 30157 } 30158 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30159 sizeof (struct sd_fi_pkt), 0); 30160 if (rval == -1) { 30161 kmem_free(un->sd_fi_fifo_pkt[i], 30162 sizeof (struct sd_fi_pkt)); 30163 un->sd_fi_fifo_pkt[i] = NULL; 30164 } 30165 } else { 30166 SD_INFO(SD_LOG_IOERR, un, 30167 "sd_faultinjection_ioctl: pkt null\n"); 30168 } 30169 break; 30170 30171 case SDIOCINSERTXB: 30172 /* Store a xb struct to be pushed onto fifo */ 30173 SD_INFO(SD_LOG_SDTEST, un, 30174 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30175 30176 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30177 30178 sd_fault_injection_on = 0; 30179 30180 if (un->sd_fi_fifo_xb[i] != NULL) { 30181 kmem_free(un->sd_fi_fifo_xb[i], 30182 sizeof (struct sd_fi_xb)); 30183 un->sd_fi_fifo_xb[i] = NULL; 30184 } 30185 if (arg != NULL) { 30186 un->sd_fi_fifo_xb[i] = 30187 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30188 if (un->sd_fi_fifo_xb[i] == NULL) { 30189 /* Alloc failed don't store anything */ 30190 break; 30191 } 30192 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30193 sizeof (struct sd_fi_xb), 0); 30194 30195 if (rval == -1) { 30196 kmem_free(un->sd_fi_fifo_xb[i], 30197 sizeof (struct sd_fi_xb)); 30198 un->sd_fi_fifo_xb[i] = NULL; 30199 } 30200 } else { 30201 SD_INFO(SD_LOG_IOERR, un, 30202 "sd_faultinjection_ioctl: xb null\n"); 30203 } 30204 break; 30205 30206 case SDIOCINSERTUN: 30207 /* Store a un struct to be pushed onto fifo */ 30208 SD_INFO(SD_LOG_SDTEST, un, 30209 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30210 30211 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30212 30213 sd_fault_injection_on = 0; 30214 30215 if (un->sd_fi_fifo_un[i] != NULL) { 30216 kmem_free(un->sd_fi_fifo_un[i], 30217 sizeof (struct sd_fi_un)); 30218 un->sd_fi_fifo_un[i] = NULL; 30219 } 30220 if (arg != NULL) { 30221 un->sd_fi_fifo_un[i] = 30222 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30223 if (un->sd_fi_fifo_un[i] == NULL) { 30224 /* Alloc failed don't store anything */ 30225 break; 30226 } 30227 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30228 sizeof (struct sd_fi_un), 0); 30229 if (rval == -1) { 30230 kmem_free(un->sd_fi_fifo_un[i], 30231 sizeof (struct sd_fi_un)); 30232 un->sd_fi_fifo_un[i] = NULL; 30233 } 30234 30235 } else { 30236 SD_INFO(SD_LOG_IOERR, un, 30237 "sd_faultinjection_ioctl: un null\n"); 30238 } 30239 30240 break; 30241 30242 case SDIOCINSERTARQ: 30243 /* Store a arq struct to be pushed onto fifo */ 30244 SD_INFO(SD_LOG_SDTEST, un, 30245 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30246 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30247 30248 sd_fault_injection_on = 0; 30249 30250 if (un->sd_fi_fifo_arq[i] != NULL) { 30251 kmem_free(un->sd_fi_fifo_arq[i], 30252 sizeof (struct sd_fi_arq)); 30253 un->sd_fi_fifo_arq[i] = NULL; 30254 } 30255 if (arg != NULL) { 30256 un->sd_fi_fifo_arq[i] = 30257 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30258 if (un->sd_fi_fifo_arq[i] == NULL) { 30259 /* Alloc failed don't store anything */ 30260 break; 30261 } 30262 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30263 sizeof (struct sd_fi_arq), 0); 30264 if (rval == -1) { 30265 kmem_free(un->sd_fi_fifo_arq[i], 30266 sizeof (struct sd_fi_arq)); 30267 un->sd_fi_fifo_arq[i] = NULL; 30268 } 30269 30270 } else { 30271 SD_INFO(SD_LOG_IOERR, un, 30272 "sd_faultinjection_ioctl: arq null\n"); 30273 } 30274 30275 break; 30276 30277 case SDIOCPUSH: 30278 /* Push stored xb, pkt, un, and arq onto fifo */ 30279 sd_fault_injection_on = 0; 30280 30281 if (arg != NULL) { 30282 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30283 if (rval != -1 && 30284 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30285 un->sd_fi_fifo_end += i; 30286 } 30287 } else { 30288 SD_INFO(SD_LOG_IOERR, un, 30289 "sd_faultinjection_ioctl: push arg null\n"); 30290 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30291 un->sd_fi_fifo_end++; 30292 } 30293 } 30294 SD_INFO(SD_LOG_IOERR, un, 30295 "sd_faultinjection_ioctl: push to end=%d\n", 30296 un->sd_fi_fifo_end); 30297 break; 30298 30299 case SDIOCRETRIEVE: 30300 /* Return buffer of log from Injection session */ 30301 SD_INFO(SD_LOG_SDTEST, un, 30302 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30303 30304 sd_fault_injection_on = 0; 30305 30306 mutex_enter(&(un->un_fi_mutex)); 30307 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30308 un->sd_fi_buf_len+1, 0); 30309 mutex_exit(&(un->un_fi_mutex)); 30310 30311 if (rval == -1) { 30312 /* 30313 * arg is possibly invalid setting 30314 * it to NULL for return 30315 */ 30316 arg = NULL; 30317 } 30318 break; 30319 } 30320 30321 mutex_exit(SD_MUTEX(un)); 30322 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30323 " exit\n"); 30324 } 30325 30326 30327 /* 30328 * Function: sd_injection_log() 30329 * 30330 * Description: This routine adds buff to the already existing injection log 30331 * for retrieval via faultinjection_ioctl for use in fault 30332 * detection and recovery 30333 * 30334 * Arguments: buf - the string to add to the log 30335 */ 30336 30337 static void 30338 sd_injection_log(char *buf, struct sd_lun *un) 30339 { 30340 uint_t len; 30341 30342 ASSERT(un != NULL); 30343 ASSERT(buf != NULL); 30344 30345 mutex_enter(&(un->un_fi_mutex)); 30346 30347 len = min(strlen(buf), 255); 30348 /* Add logged value to Injection log to be returned later */ 30349 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30350 uint_t offset = strlen((char *)un->sd_fi_log); 30351 char *destp = (char *)un->sd_fi_log + offset; 30352 int i; 30353 for (i = 0; i < len; i++) { 30354 *destp++ = *buf++; 30355 } 30356 un->sd_fi_buf_len += len; 30357 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30358 } 30359 30360 mutex_exit(&(un->un_fi_mutex)); 30361 } 30362 30363 30364 /* 30365 * Function: sd_faultinjection() 30366 * 30367 * Description: This routine takes the pkt and changes its 30368 * content based on error injection scenerio. 30369 * 30370 * Arguments: pktp - packet to be changed 30371 */ 30372 30373 static void 30374 sd_faultinjection(struct scsi_pkt *pktp) 30375 { 30376 uint_t i; 30377 struct sd_fi_pkt *fi_pkt; 30378 struct sd_fi_xb *fi_xb; 30379 struct sd_fi_un *fi_un; 30380 struct sd_fi_arq *fi_arq; 30381 struct buf *bp; 30382 struct sd_xbuf *xb; 30383 struct sd_lun *un; 30384 30385 ASSERT(pktp != NULL); 30386 30387 /* pull bp xb and un from pktp */ 30388 bp = (struct buf *)pktp->pkt_private; 30389 xb = SD_GET_XBUF(bp); 30390 un = SD_GET_UN(bp); 30391 30392 ASSERT(un != NULL); 30393 30394 mutex_enter(SD_MUTEX(un)); 30395 30396 SD_TRACE(SD_LOG_SDTEST, un, 30397 "sd_faultinjection: entry Injection from sdintr\n"); 30398 30399 /* if injection is off return */ 30400 if (sd_fault_injection_on == 0 || 30401 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30402 mutex_exit(SD_MUTEX(un)); 30403 return; 30404 } 30405 30406 SD_INFO(SD_LOG_SDTEST, un, 30407 "sd_faultinjection: is working for copying\n"); 30408 30409 /* take next set off fifo */ 30410 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30411 30412 fi_pkt = un->sd_fi_fifo_pkt[i]; 30413 fi_xb = un->sd_fi_fifo_xb[i]; 30414 fi_un = un->sd_fi_fifo_un[i]; 30415 fi_arq = un->sd_fi_fifo_arq[i]; 30416 30417 30418 /* set variables accordingly */ 30419 /* set pkt if it was on fifo */ 30420 if (fi_pkt != NULL) { 30421 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30422 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30423 if (fi_pkt->pkt_cdbp != 0xff) 30424 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30425 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30426 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30427 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30428 30429 } 30430 /* set xb if it was on fifo */ 30431 if (fi_xb != NULL) { 30432 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30433 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30434 if (fi_xb->xb_retry_count != 0) 30435 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30436 SD_CONDSET(xb, xb, xb_victim_retry_count, 30437 "xb_victim_retry_count"); 30438 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30439 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30440 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30441 30442 /* copy in block data from sense */ 30443 /* 30444 * if (fi_xb->xb_sense_data[0] != -1) { 30445 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30446 * SENSE_LENGTH); 30447 * } 30448 */ 30449 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH); 30450 30451 /* copy in extended sense codes */ 30452 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30453 xb, es_code, "es_code"); 30454 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30455 xb, es_key, "es_key"); 30456 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30457 xb, es_add_code, "es_add_code"); 30458 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30459 xb, es_qual_code, "es_qual_code"); 30460 struct scsi_extended_sense *esp; 30461 esp = (struct scsi_extended_sense *)xb->xb_sense_data; 30462 esp->es_class = CLASS_EXTENDED_SENSE; 30463 } 30464 30465 /* set un if it was on fifo */ 30466 if (fi_un != NULL) { 30467 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30468 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30469 SD_CONDSET(un, un, un_reset_retry_count, 30470 "un_reset_retry_count"); 30471 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30472 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30473 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30474 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30475 "un_f_allow_bus_device_reset"); 30476 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30477 30478 } 30479 30480 /* copy in auto request sense if it was on fifo */ 30481 if (fi_arq != NULL) { 30482 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30483 } 30484 30485 /* free structs */ 30486 if (un->sd_fi_fifo_pkt[i] != NULL) { 30487 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30488 } 30489 if (un->sd_fi_fifo_xb[i] != NULL) { 30490 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30491 } 30492 if (un->sd_fi_fifo_un[i] != NULL) { 30493 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30494 } 30495 if (un->sd_fi_fifo_arq[i] != NULL) { 30496 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30497 } 30498 30499 /* 30500 * kmem_free does not gurantee to set to NULL 30501 * since we uses these to determine if we set 30502 * values or not lets confirm they are always 30503 * NULL after free 30504 */ 30505 un->sd_fi_fifo_pkt[i] = NULL; 30506 un->sd_fi_fifo_un[i] = NULL; 30507 un->sd_fi_fifo_xb[i] = NULL; 30508 un->sd_fi_fifo_arq[i] = NULL; 30509 30510 un->sd_fi_fifo_start++; 30511 30512 mutex_exit(SD_MUTEX(un)); 30513 30514 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30515 } 30516 30517 #endif /* SD_FAULT_INJECTION */ 30518 30519 /* 30520 * This routine is invoked in sd_unit_attach(). Before calling it, the 30521 * properties in conf file should be processed already, and "hotpluggable" 30522 * property was processed also. 30523 * 30524 * The sd driver distinguishes 3 different type of devices: removable media, 30525 * non-removable media, and hotpluggable. Below the differences are defined: 30526 * 30527 * 1. Device ID 30528 * 30529 * The device ID of a device is used to identify this device. Refer to 30530 * ddi_devid_register(9F). 30531 * 30532 * For a non-removable media disk device which can provide 0x80 or 0x83 30533 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30534 * device ID is created to identify this device. For other non-removable 30535 * media devices, a default device ID is created only if this device has 30536 * at least 2 alter cylinders. Otherwise, this device has no devid. 30537 * 30538 * ------------------------------------------------------- 30539 * removable media hotpluggable | Can Have Device ID 30540 * ------------------------------------------------------- 30541 * false false | Yes 30542 * false true | Yes 30543 * true x | No 30544 * ------------------------------------------------------ 30545 * 30546 * 30547 * 2. SCSI group 4 commands 30548 * 30549 * In SCSI specs, only some commands in group 4 command set can use 30550 * 8-byte addresses that can be used to access >2TB storage spaces. 30551 * Other commands have no such capability. Without supporting group4, 30552 * it is impossible to make full use of storage spaces of a disk with 30553 * capacity larger than 2TB. 30554 * 30555 * ----------------------------------------------- 30556 * removable media hotpluggable LP64 | Group 30557 * ----------------------------------------------- 30558 * false false false | 1 30559 * false false true | 4 30560 * false true false | 1 30561 * false true true | 4 30562 * true x x | 5 30563 * ----------------------------------------------- 30564 * 30565 * 30566 * 3. Check for VTOC Label 30567 * 30568 * If a direct-access disk has no EFI label, sd will check if it has a 30569 * valid VTOC label. Now, sd also does that check for removable media 30570 * and hotpluggable devices. 30571 * 30572 * -------------------------------------------------------------- 30573 * Direct-Access removable media hotpluggable | Check Label 30574 * ------------------------------------------------------------- 30575 * false false false | No 30576 * false false true | No 30577 * false true false | Yes 30578 * false true true | Yes 30579 * true x x | Yes 30580 * -------------------------------------------------------------- 30581 * 30582 * 30583 * 4. Building default VTOC label 30584 * 30585 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30586 * If those devices have no valid VTOC label, sd(7d) will attempt to 30587 * create default VTOC for them. Currently sd creates default VTOC label 30588 * for all devices on x86 platform (VTOC_16), but only for removable 30589 * media devices on SPARC (VTOC_8). 30590 * 30591 * ----------------------------------------------------------- 30592 * removable media hotpluggable platform | Default Label 30593 * ----------------------------------------------------------- 30594 * false false sparc | No 30595 * false true x86 | Yes 30596 * false true sparc | Yes 30597 * true x x | Yes 30598 * ---------------------------------------------------------- 30599 * 30600 * 30601 * 5. Supported blocksizes of target devices 30602 * 30603 * Sd supports non-512-byte blocksize for removable media devices only. 30604 * For other devices, only 512-byte blocksize is supported. This may be 30605 * changed in near future because some RAID devices require non-512-byte 30606 * blocksize 30607 * 30608 * ----------------------------------------------------------- 30609 * removable media hotpluggable | non-512-byte blocksize 30610 * ----------------------------------------------------------- 30611 * false false | No 30612 * false true | No 30613 * true x | Yes 30614 * ----------------------------------------------------------- 30615 * 30616 * 30617 * 6. Automatic mount & unmount 30618 * 30619 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30620 * if a device is removable media device. It return 1 for removable media 30621 * devices, and 0 for others. 30622 * 30623 * The automatic mounting subsystem should distinguish between the types 30624 * of devices and apply automounting policies to each. 30625 * 30626 * 30627 * 7. fdisk partition management 30628 * 30629 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30630 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30631 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30632 * fdisk partitions on both x86 and SPARC platform. 30633 * 30634 * ----------------------------------------------------------- 30635 * platform removable media USB/1394 | fdisk supported 30636 * ----------------------------------------------------------- 30637 * x86 X X | true 30638 * ------------------------------------------------------------ 30639 * sparc X X | false 30640 * ------------------------------------------------------------ 30641 * 30642 * 30643 * 8. MBOOT/MBR 30644 * 30645 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30646 * read/write mboot for removable media devices on sparc platform. 30647 * 30648 * ----------------------------------------------------------- 30649 * platform removable media USB/1394 | mboot supported 30650 * ----------------------------------------------------------- 30651 * x86 X X | true 30652 * ------------------------------------------------------------ 30653 * sparc false false | false 30654 * sparc false true | true 30655 * sparc true false | true 30656 * sparc true true | true 30657 * ------------------------------------------------------------ 30658 * 30659 * 30660 * 9. error handling during opening device 30661 * 30662 * If failed to open a disk device, an errno is returned. For some kinds 30663 * of errors, different errno is returned depending on if this device is 30664 * a removable media device. This brings USB/1394 hard disks in line with 30665 * expected hard disk behavior. It is not expected that this breaks any 30666 * application. 30667 * 30668 * ------------------------------------------------------ 30669 * removable media hotpluggable | errno 30670 * ------------------------------------------------------ 30671 * false false | EIO 30672 * false true | EIO 30673 * true x | ENXIO 30674 * ------------------------------------------------------ 30675 * 30676 * 30677 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30678 * 30679 * These IOCTLs are applicable only to removable media devices. 30680 * 30681 * ----------------------------------------------------------- 30682 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30683 * ----------------------------------------------------------- 30684 * false false | No 30685 * false true | No 30686 * true x | Yes 30687 * ----------------------------------------------------------- 30688 * 30689 * 30690 * 12. Kstats for partitions 30691 * 30692 * sd creates partition kstat for non-removable media devices. USB and 30693 * Firewire hard disks now have partition kstats 30694 * 30695 * ------------------------------------------------------ 30696 * removable media hotpluggable | kstat 30697 * ------------------------------------------------------ 30698 * false false | Yes 30699 * false true | Yes 30700 * true x | No 30701 * ------------------------------------------------------ 30702 * 30703 * 30704 * 13. Removable media & hotpluggable properties 30705 * 30706 * Sd driver creates a "removable-media" property for removable media 30707 * devices. Parent nexus drivers create a "hotpluggable" property if 30708 * it supports hotplugging. 30709 * 30710 * --------------------------------------------------------------------- 30711 * removable media hotpluggable | "removable-media" " hotpluggable" 30712 * --------------------------------------------------------------------- 30713 * false false | No No 30714 * false true | No Yes 30715 * true false | Yes No 30716 * true true | Yes Yes 30717 * --------------------------------------------------------------------- 30718 * 30719 * 30720 * 14. Power Management 30721 * 30722 * sd only power manages removable media devices or devices that support 30723 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30724 * 30725 * A parent nexus that supports hotplugging can also set "pm-capable" 30726 * if the disk can be power managed. 30727 * 30728 * ------------------------------------------------------------ 30729 * removable media hotpluggable pm-capable | power manage 30730 * ------------------------------------------------------------ 30731 * false false false | No 30732 * false false true | Yes 30733 * false true false | No 30734 * false true true | Yes 30735 * true x x | Yes 30736 * ------------------------------------------------------------ 30737 * 30738 * USB and firewire hard disks can now be power managed independently 30739 * of the framebuffer 30740 * 30741 * 30742 * 15. Support for USB disks with capacity larger than 1TB 30743 * 30744 * Currently, sd doesn't permit a fixed disk device with capacity 30745 * larger than 1TB to be used in a 32-bit operating system environment. 30746 * However, sd doesn't do that for removable media devices. Instead, it 30747 * assumes that removable media devices cannot have a capacity larger 30748 * than 1TB. Therefore, using those devices on 32-bit system is partially 30749 * supported, which can cause some unexpected results. 30750 * 30751 * --------------------------------------------------------------------- 30752 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30753 * --------------------------------------------------------------------- 30754 * false false | true | no 30755 * false true | true | no 30756 * true false | true | Yes 30757 * true true | true | Yes 30758 * --------------------------------------------------------------------- 30759 * 30760 * 30761 * 16. Check write-protection at open time 30762 * 30763 * When a removable media device is being opened for writing without NDELAY 30764 * flag, sd will check if this device is writable. If attempting to open 30765 * without NDELAY flag a write-protected device, this operation will abort. 30766 * 30767 * ------------------------------------------------------------ 30768 * removable media USB/1394 | WP Check 30769 * ------------------------------------------------------------ 30770 * false false | No 30771 * false true | No 30772 * true false | Yes 30773 * true true | Yes 30774 * ------------------------------------------------------------ 30775 * 30776 * 30777 * 17. syslog when corrupted VTOC is encountered 30778 * 30779 * Currently, if an invalid VTOC is encountered, sd only print syslog 30780 * for fixed SCSI disks. 30781 * ------------------------------------------------------------ 30782 * removable media USB/1394 | print syslog 30783 * ------------------------------------------------------------ 30784 * false false | Yes 30785 * false true | No 30786 * true false | No 30787 * true true | No 30788 * ------------------------------------------------------------ 30789 */ 30790 static void 30791 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30792 { 30793 int pm_cap; 30794 30795 ASSERT(un->un_sd); 30796 ASSERT(un->un_sd->sd_inq); 30797 30798 /* 30799 * Enable SYNC CACHE support for all devices. 30800 */ 30801 un->un_f_sync_cache_supported = TRUE; 30802 30803 /* 30804 * Set the sync cache required flag to false. 30805 * This would ensure that there is no SYNC CACHE 30806 * sent when there are no writes 30807 */ 30808 un->un_f_sync_cache_required = FALSE; 30809 30810 if (un->un_sd->sd_inq->inq_rmb) { 30811 /* 30812 * The media of this device is removable. And for this kind 30813 * of devices, it is possible to change medium after opening 30814 * devices. Thus we should support this operation. 30815 */ 30816 un->un_f_has_removable_media = TRUE; 30817 30818 /* 30819 * support non-512-byte blocksize of removable media devices 30820 */ 30821 un->un_f_non_devbsize_supported = TRUE; 30822 30823 /* 30824 * Assume that all removable media devices support DOOR_LOCK 30825 */ 30826 un->un_f_doorlock_supported = TRUE; 30827 30828 /* 30829 * For a removable media device, it is possible to be opened 30830 * with NDELAY flag when there is no media in drive, in this 30831 * case we don't care if device is writable. But if without 30832 * NDELAY flag, we need to check if media is write-protected. 30833 */ 30834 un->un_f_chk_wp_open = TRUE; 30835 30836 /* 30837 * need to start a SCSI watch thread to monitor media state, 30838 * when media is being inserted or ejected, notify syseventd. 30839 */ 30840 un->un_f_monitor_media_state = TRUE; 30841 30842 /* 30843 * Some devices don't support START_STOP_UNIT command. 30844 * Therefore, we'd better check if a device supports it 30845 * before sending it. 30846 */ 30847 un->un_f_check_start_stop = TRUE; 30848 30849 /* 30850 * support eject media ioctl: 30851 * FDEJECT, DKIOCEJECT, CDROMEJECT 30852 */ 30853 un->un_f_eject_media_supported = TRUE; 30854 30855 /* 30856 * Because many removable-media devices don't support 30857 * LOG_SENSE, we couldn't use this command to check if 30858 * a removable media device support power-management. 30859 * We assume that they support power-management via 30860 * START_STOP_UNIT command and can be spun up and down 30861 * without limitations. 30862 */ 30863 un->un_f_pm_supported = TRUE; 30864 30865 /* 30866 * Need to create a zero length (Boolean) property 30867 * removable-media for the removable media devices. 30868 * Note that the return value of the property is not being 30869 * checked, since if unable to create the property 30870 * then do not want the attach to fail altogether. Consistent 30871 * with other property creation in attach. 30872 */ 30873 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30874 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30875 30876 } else { 30877 /* 30878 * create device ID for device 30879 */ 30880 un->un_f_devid_supported = TRUE; 30881 30882 /* 30883 * Spin up non-removable-media devices once it is attached 30884 */ 30885 un->un_f_attach_spinup = TRUE; 30886 30887 /* 30888 * According to SCSI specification, Sense data has two kinds of 30889 * format: fixed format, and descriptor format. At present, we 30890 * don't support descriptor format sense data for removable 30891 * media. 30892 */ 30893 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30894 un->un_f_descr_format_supported = TRUE; 30895 } 30896 30897 /* 30898 * kstats are created only for non-removable media devices. 30899 * 30900 * Set this in sd.conf to 0 in order to disable kstats. The 30901 * default is 1, so they are enabled by default. 30902 */ 30903 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30904 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30905 "enable-partition-kstats", 1)); 30906 30907 /* 30908 * Check if HBA has set the "pm-capable" property. 30909 * If "pm-capable" exists and is non-zero then we can 30910 * power manage the device without checking the start/stop 30911 * cycle count log sense page. 30912 * 30913 * If "pm-capable" exists and is set to be false (0), 30914 * then we should not power manage the device. 30915 * 30916 * If "pm-capable" doesn't exist then pm_cap will 30917 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 30918 * sd will check the start/stop cycle count log sense page 30919 * and power manage the device if the cycle count limit has 30920 * not been exceeded. 30921 */ 30922 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 30923 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 30924 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) { 30925 un->un_f_log_sense_supported = TRUE; 30926 if (!un->un_f_power_condition_disabled && 30927 SD_INQUIRY(un)->inq_ansi == 6) { 30928 un->un_f_power_condition_supported = TRUE; 30929 } 30930 } else { 30931 /* 30932 * pm-capable property exists. 30933 * 30934 * Convert "TRUE" values for pm_cap to 30935 * SD_PM_CAPABLE_IS_TRUE to make it easier to check 30936 * later. "TRUE" values are any values defined in 30937 * inquiry.h. 30938 */ 30939 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) { 30940 un->un_f_log_sense_supported = FALSE; 30941 } else { 30942 /* SD_PM_CAPABLE_IS_TRUE case */ 30943 un->un_f_pm_supported = TRUE; 30944 if (!un->un_f_power_condition_disabled && 30945 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) { 30946 un->un_f_power_condition_supported = 30947 TRUE; 30948 } 30949 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) { 30950 un->un_f_log_sense_supported = TRUE; 30951 un->un_f_pm_log_sense_smart = 30952 SD_PM_CAP_SMART_LOG(pm_cap); 30953 } 30954 } 30955 30956 SD_INFO(SD_LOG_ATTACH_DETACH, un, 30957 "sd_unit_attach: un:0x%p pm-capable " 30958 "property set to %d.\n", un, un->un_f_pm_supported); 30959 } 30960 } 30961 30962 if (un->un_f_is_hotpluggable) { 30963 30964 /* 30965 * Have to watch hotpluggable devices as well, since 30966 * that's the only way for userland applications to 30967 * detect hot removal while device is busy/mounted. 30968 */ 30969 un->un_f_monitor_media_state = TRUE; 30970 30971 un->un_f_check_start_stop = TRUE; 30972 30973 } 30974 } 30975 30976 /* 30977 * sd_tg_rdwr: 30978 * Provides rdwr access for cmlb via sd_tgops. The start_block is 30979 * in sys block size, req_length in bytes. 30980 * 30981 */ 30982 static int 30983 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 30984 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 30985 { 30986 struct sd_lun *un; 30987 int path_flag = (int)(uintptr_t)tg_cookie; 30988 char *dkl = NULL; 30989 diskaddr_t real_addr = start_block; 30990 diskaddr_t first_byte, end_block; 30991 30992 size_t buffer_size = reqlength; 30993 int rval = 0; 30994 diskaddr_t cap; 30995 uint32_t lbasize; 30996 sd_ssc_t *ssc; 30997 30998 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 30999 if (un == NULL) 31000 return (ENXIO); 31001 31002 if (cmd != TG_READ && cmd != TG_WRITE) 31003 return (EINVAL); 31004 31005 ssc = sd_ssc_init(un); 31006 mutex_enter(SD_MUTEX(un)); 31007 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 31008 mutex_exit(SD_MUTEX(un)); 31009 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 31010 &lbasize, path_flag); 31011 if (rval != 0) 31012 goto done1; 31013 mutex_enter(SD_MUTEX(un)); 31014 sd_update_block_info(un, lbasize, cap); 31015 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 31016 mutex_exit(SD_MUTEX(un)); 31017 rval = EIO; 31018 goto done; 31019 } 31020 } 31021 31022 if (NOT_DEVBSIZE(un)) { 31023 /* 31024 * sys_blocksize != tgt_blocksize, need to re-adjust 31025 * blkno and save the index to beginning of dk_label 31026 */ 31027 first_byte = SD_SYSBLOCKS2BYTES(start_block); 31028 real_addr = first_byte / un->un_tgt_blocksize; 31029 31030 end_block = (first_byte + reqlength + 31031 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 31032 31033 /* round up buffer size to multiple of target block size */ 31034 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 31035 31036 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 31037 "label_addr: 0x%x allocation size: 0x%x\n", 31038 real_addr, buffer_size); 31039 31040 if (((first_byte % un->un_tgt_blocksize) != 0) || 31041 (reqlength % un->un_tgt_blocksize) != 0) 31042 /* the request is not aligned */ 31043 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 31044 } 31045 31046 /* 31047 * The MMC standard allows READ CAPACITY to be 31048 * inaccurate by a bounded amount (in the interest of 31049 * response latency). As a result, failed READs are 31050 * commonplace (due to the reading of metadata and not 31051 * data). Depending on the per-Vendor/drive Sense data, 31052 * the failed READ can cause many (unnecessary) retries. 31053 */ 31054 31055 if (ISCD(un) && (cmd == TG_READ) && 31056 (un->un_f_blockcount_is_valid == TRUE) && 31057 ((start_block == (un->un_blockcount - 1))|| 31058 (start_block == (un->un_blockcount - 2)))) { 31059 path_flag = SD_PATH_DIRECT_PRIORITY; 31060 } 31061 31062 mutex_exit(SD_MUTEX(un)); 31063 if (cmd == TG_READ) { 31064 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr, 31065 buffer_size, real_addr, path_flag); 31066 if (dkl != NULL) 31067 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 31068 real_addr), bufaddr, reqlength); 31069 } else { 31070 if (dkl) { 31071 rval = sd_send_scsi_READ(ssc, dkl, buffer_size, 31072 real_addr, path_flag); 31073 if (rval) { 31074 goto done1; 31075 } 31076 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 31077 real_addr), reqlength); 31078 } 31079 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr, 31080 buffer_size, real_addr, path_flag); 31081 } 31082 31083 done1: 31084 if (dkl != NULL) 31085 kmem_free(dkl, buffer_size); 31086 31087 if (rval != 0) { 31088 if (rval == EIO) 31089 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 31090 else 31091 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31092 } 31093 done: 31094 sd_ssc_fini(ssc); 31095 return (rval); 31096 } 31097 31098 31099 static int 31100 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 31101 { 31102 31103 struct sd_lun *un; 31104 diskaddr_t cap; 31105 uint32_t lbasize; 31106 int path_flag = (int)(uintptr_t)tg_cookie; 31107 int ret = 0; 31108 31109 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 31110 if (un == NULL) 31111 return (ENXIO); 31112 31113 switch (cmd) { 31114 case TG_GETPHYGEOM: 31115 case TG_GETVIRTGEOM: 31116 case TG_GETCAPACITY: 31117 case TG_GETBLOCKSIZE: 31118 mutex_enter(SD_MUTEX(un)); 31119 31120 if ((un->un_f_blockcount_is_valid == TRUE) && 31121 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 31122 cap = un->un_blockcount; 31123 lbasize = un->un_tgt_blocksize; 31124 mutex_exit(SD_MUTEX(un)); 31125 } else { 31126 sd_ssc_t *ssc; 31127 mutex_exit(SD_MUTEX(un)); 31128 ssc = sd_ssc_init(un); 31129 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 31130 &lbasize, path_flag); 31131 if (ret != 0) { 31132 if (ret == EIO) 31133 sd_ssc_assessment(ssc, 31134 SD_FMT_STATUS_CHECK); 31135 else 31136 sd_ssc_assessment(ssc, 31137 SD_FMT_IGNORE); 31138 sd_ssc_fini(ssc); 31139 return (ret); 31140 } 31141 sd_ssc_fini(ssc); 31142 mutex_enter(SD_MUTEX(un)); 31143 sd_update_block_info(un, lbasize, cap); 31144 if ((un->un_f_blockcount_is_valid == FALSE) || 31145 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 31146 mutex_exit(SD_MUTEX(un)); 31147 return (EIO); 31148 } 31149 mutex_exit(SD_MUTEX(un)); 31150 } 31151 31152 if (cmd == TG_GETCAPACITY) { 31153 *(diskaddr_t *)arg = cap; 31154 return (0); 31155 } 31156 31157 if (cmd == TG_GETBLOCKSIZE) { 31158 *(uint32_t *)arg = lbasize; 31159 return (0); 31160 } 31161 31162 if (cmd == TG_GETPHYGEOM) 31163 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 31164 cap, lbasize, path_flag); 31165 else 31166 /* TG_GETVIRTGEOM */ 31167 ret = sd_get_virtual_geometry(un, 31168 (cmlb_geom_t *)arg, cap, lbasize); 31169 31170 return (ret); 31171 31172 case TG_GETATTR: 31173 mutex_enter(SD_MUTEX(un)); 31174 ((tg_attribute_t *)arg)->media_is_writable = 31175 un->un_f_mmc_writable_media; 31176 ((tg_attribute_t *)arg)->media_is_solid_state = 31177 un->un_f_is_solid_state; 31178 mutex_exit(SD_MUTEX(un)); 31179 return (0); 31180 default: 31181 return (ENOTTY); 31182 31183 } 31184 } 31185 31186 /* 31187 * Function: sd_ssc_ereport_post 31188 * 31189 * Description: Will be called when SD driver need to post an ereport. 31190 * 31191 * Context: Kernel thread or interrupt context. 31192 */ 31193 31194 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown" 31195 31196 static void 31197 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess) 31198 { 31199 int uscsi_path_instance = 0; 31200 uchar_t uscsi_pkt_reason; 31201 uint32_t uscsi_pkt_state; 31202 uint32_t uscsi_pkt_statistics; 31203 uint64_t uscsi_ena; 31204 uchar_t op_code; 31205 uint8_t *sensep; 31206 union scsi_cdb *cdbp; 31207 uint_t cdblen = 0; 31208 uint_t senlen = 0; 31209 struct sd_lun *un; 31210 dev_info_t *dip; 31211 char *devid; 31212 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON | 31213 SSC_FLAGS_INVALID_STATUS | 31214 SSC_FLAGS_INVALID_SENSE | 31215 SSC_FLAGS_INVALID_DATA; 31216 char assessment[16]; 31217 31218 ASSERT(ssc != NULL); 31219 ASSERT(ssc->ssc_uscsi_cmd != NULL); 31220 ASSERT(ssc->ssc_uscsi_info != NULL); 31221 31222 un = ssc->ssc_un; 31223 ASSERT(un != NULL); 31224 31225 dip = un->un_sd->sd_dev; 31226 31227 /* 31228 * Get the devid: 31229 * devid will only be passed to non-transport error reports. 31230 */ 31231 devid = DEVI(dip)->devi_devid_str; 31232 31233 /* 31234 * If we are syncing or dumping, the command will not be executed 31235 * so we bypass this situation. 31236 */ 31237 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 31238 (un->un_state == SD_STATE_DUMPING)) 31239 return; 31240 31241 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason; 31242 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance; 31243 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state; 31244 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics; 31245 uscsi_ena = ssc->ssc_uscsi_info->ui_ena; 31246 31247 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 31248 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb; 31249 31250 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */ 31251 if (cdbp == NULL) { 31252 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 31253 "sd_ssc_ereport_post meet empty cdb\n"); 31254 return; 31255 } 31256 31257 op_code = cdbp->scc_cmd; 31258 31259 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen; 31260 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 31261 ssc->ssc_uscsi_cmd->uscsi_rqresid); 31262 31263 if (senlen > 0) 31264 ASSERT(sensep != NULL); 31265 31266 /* 31267 * Initialize drv_assess to corresponding values. 31268 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending 31269 * on the sense-key returned back. 31270 */ 31271 switch (drv_assess) { 31272 case SD_FM_DRV_RECOVERY: 31273 (void) sprintf(assessment, "%s", "recovered"); 31274 break; 31275 case SD_FM_DRV_RETRY: 31276 (void) sprintf(assessment, "%s", "retry"); 31277 break; 31278 case SD_FM_DRV_NOTICE: 31279 (void) sprintf(assessment, "%s", "info"); 31280 break; 31281 case SD_FM_DRV_FATAL: 31282 default: 31283 (void) sprintf(assessment, "%s", "unknown"); 31284 } 31285 /* 31286 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered 31287 * command, we will post ereport.io.scsi.cmd.disk.recovered. 31288 * driver-assessment will always be "recovered" here. 31289 */ 31290 if (drv_assess == SD_FM_DRV_RECOVERY) { 31291 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31292 "cmd.disk.recovered", uscsi_ena, devid, NULL, 31293 DDI_NOSLEEP, NULL, 31294 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31295 DEVID_IF_KNOWN(devid), 31296 "driver-assessment", DATA_TYPE_STRING, assessment, 31297 "op-code", DATA_TYPE_UINT8, op_code, 31298 "cdb", DATA_TYPE_UINT8_ARRAY, 31299 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31300 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31301 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31302 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31303 NULL); 31304 return; 31305 } 31306 31307 /* 31308 * If there is un-expected/un-decodable data, we should post 31309 * ereport.io.scsi.cmd.disk.dev.uderr. 31310 * driver-assessment will be set based on parameter drv_assess. 31311 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back. 31312 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered. 31313 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered. 31314 * SSC_FLAGS_INVALID_DATA - invalid data sent back. 31315 */ 31316 if (ssc->ssc_flags & ssc_invalid_flags) { 31317 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) { 31318 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31319 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid, 31320 NULL, DDI_NOSLEEP, NULL, 31321 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31322 DEVID_IF_KNOWN(devid), 31323 "driver-assessment", DATA_TYPE_STRING, 31324 drv_assess == SD_FM_DRV_FATAL ? 31325 "fail" : assessment, 31326 "op-code", DATA_TYPE_UINT8, op_code, 31327 "cdb", DATA_TYPE_UINT8_ARRAY, 31328 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31329 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31330 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31331 "pkt-stats", DATA_TYPE_UINT32, 31332 uscsi_pkt_statistics, 31333 "stat-code", DATA_TYPE_UINT8, 31334 ssc->ssc_uscsi_cmd->uscsi_status, 31335 "un-decode-info", DATA_TYPE_STRING, 31336 ssc->ssc_info, 31337 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31338 senlen, sensep, 31339 NULL); 31340 } else { 31341 /* 31342 * For other type of invalid data, the 31343 * un-decode-value field would be empty because the 31344 * un-decodable content could be seen from upper 31345 * level payload or inside un-decode-info. 31346 */ 31347 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31348 NULL, 31349 "cmd.disk.dev.uderr", uscsi_ena, devid, 31350 NULL, DDI_NOSLEEP, NULL, 31351 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31352 DEVID_IF_KNOWN(devid), 31353 "driver-assessment", DATA_TYPE_STRING, 31354 drv_assess == SD_FM_DRV_FATAL ? 31355 "fail" : assessment, 31356 "op-code", DATA_TYPE_UINT8, op_code, 31357 "cdb", DATA_TYPE_UINT8_ARRAY, 31358 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31359 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31360 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31361 "pkt-stats", DATA_TYPE_UINT32, 31362 uscsi_pkt_statistics, 31363 "stat-code", DATA_TYPE_UINT8, 31364 ssc->ssc_uscsi_cmd->uscsi_status, 31365 "un-decode-info", DATA_TYPE_STRING, 31366 ssc->ssc_info, 31367 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31368 0, NULL, 31369 NULL); 31370 } 31371 ssc->ssc_flags &= ~ssc_invalid_flags; 31372 return; 31373 } 31374 31375 if (uscsi_pkt_reason != CMD_CMPLT || 31376 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) { 31377 /* 31378 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was 31379 * set inside sd_start_cmds due to errors(bad packet or 31380 * fatal transport error), we should take it as a 31381 * transport error, so we post ereport.io.scsi.cmd.disk.tran. 31382 * driver-assessment will be set based on drv_assess. 31383 * We will set devid to NULL because it is a transport 31384 * error. 31385 */ 31386 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT) 31387 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT; 31388 31389 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31390 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL, 31391 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31392 DEVID_IF_KNOWN(devid), 31393 "driver-assessment", DATA_TYPE_STRING, 31394 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31395 "op-code", DATA_TYPE_UINT8, op_code, 31396 "cdb", DATA_TYPE_UINT8_ARRAY, 31397 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31398 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31399 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state, 31400 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31401 NULL); 31402 } else { 31403 /* 31404 * If we got here, we have a completed command, and we need 31405 * to further investigate the sense data to see what kind 31406 * of ereport we should post. 31407 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr 31408 * if sense-key == 0x3. 31409 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise. 31410 * driver-assessment will be set based on the parameter 31411 * drv_assess. 31412 */ 31413 if (senlen > 0) { 31414 /* 31415 * Here we have sense data available. 31416 */ 31417 uint8_t sense_key; 31418 sense_key = scsi_sense_key(sensep); 31419 if (sense_key == 0x3) { 31420 /* 31421 * sense-key == 0x3(medium error), 31422 * driver-assessment should be "fatal" if 31423 * drv_assess is SD_FM_DRV_FATAL. 31424 */ 31425 scsi_fm_ereport_post(un->un_sd, 31426 uscsi_path_instance, NULL, 31427 "cmd.disk.dev.rqs.merr", 31428 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL, 31429 FM_VERSION, DATA_TYPE_UINT8, 31430 FM_EREPORT_VERS0, 31431 DEVID_IF_KNOWN(devid), 31432 "driver-assessment", 31433 DATA_TYPE_STRING, 31434 drv_assess == SD_FM_DRV_FATAL ? 31435 "fatal" : assessment, 31436 "op-code", 31437 DATA_TYPE_UINT8, op_code, 31438 "cdb", 31439 DATA_TYPE_UINT8_ARRAY, cdblen, 31440 ssc->ssc_uscsi_cmd->uscsi_cdb, 31441 "pkt-reason", 31442 DATA_TYPE_UINT8, uscsi_pkt_reason, 31443 "pkt-state", 31444 DATA_TYPE_UINT8, uscsi_pkt_state, 31445 "pkt-stats", 31446 DATA_TYPE_UINT32, 31447 uscsi_pkt_statistics, 31448 "stat-code", 31449 DATA_TYPE_UINT8, 31450 ssc->ssc_uscsi_cmd->uscsi_status, 31451 "key", 31452 DATA_TYPE_UINT8, 31453 scsi_sense_key(sensep), 31454 "asc", 31455 DATA_TYPE_UINT8, 31456 scsi_sense_asc(sensep), 31457 "ascq", 31458 DATA_TYPE_UINT8, 31459 scsi_sense_ascq(sensep), 31460 "sense-data", 31461 DATA_TYPE_UINT8_ARRAY, 31462 senlen, sensep, 31463 "lba", 31464 DATA_TYPE_UINT64, 31465 ssc->ssc_uscsi_info->ui_lba, 31466 NULL); 31467 } else { 31468 /* 31469 * if sense-key == 0x4(hardware 31470 * error), driver-assessment should 31471 * be "fatal" if drv_assess is 31472 * SD_FM_DRV_FATAL. 31473 */ 31474 scsi_fm_ereport_post(un->un_sd, 31475 uscsi_path_instance, NULL, 31476 "cmd.disk.dev.rqs.derr", 31477 uscsi_ena, devid, 31478 NULL, DDI_NOSLEEP, NULL, 31479 FM_VERSION, 31480 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31481 DEVID_IF_KNOWN(devid), 31482 "driver-assessment", 31483 DATA_TYPE_STRING, 31484 drv_assess == SD_FM_DRV_FATAL ? 31485 (sense_key == 0x4 ? 31486 "fatal" : "fail") : assessment, 31487 "op-code", 31488 DATA_TYPE_UINT8, op_code, 31489 "cdb", 31490 DATA_TYPE_UINT8_ARRAY, cdblen, 31491 ssc->ssc_uscsi_cmd->uscsi_cdb, 31492 "pkt-reason", 31493 DATA_TYPE_UINT8, uscsi_pkt_reason, 31494 "pkt-state", 31495 DATA_TYPE_UINT8, uscsi_pkt_state, 31496 "pkt-stats", 31497 DATA_TYPE_UINT32, 31498 uscsi_pkt_statistics, 31499 "stat-code", 31500 DATA_TYPE_UINT8, 31501 ssc->ssc_uscsi_cmd->uscsi_status, 31502 "key", 31503 DATA_TYPE_UINT8, 31504 scsi_sense_key(sensep), 31505 "asc", 31506 DATA_TYPE_UINT8, 31507 scsi_sense_asc(sensep), 31508 "ascq", 31509 DATA_TYPE_UINT8, 31510 scsi_sense_ascq(sensep), 31511 "sense-data", 31512 DATA_TYPE_UINT8_ARRAY, 31513 senlen, sensep, 31514 NULL); 31515 } 31516 } else { 31517 /* 31518 * For stat_code == STATUS_GOOD, this is not a 31519 * hardware error. 31520 */ 31521 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) 31522 return; 31523 31524 /* 31525 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the 31526 * stat-code but with sense data unavailable. 31527 * driver-assessment will be set based on parameter 31528 * drv_assess. 31529 */ 31530 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31531 NULL, 31532 "cmd.disk.dev.serr", uscsi_ena, 31533 devid, NULL, DDI_NOSLEEP, NULL, 31534 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31535 DEVID_IF_KNOWN(devid), 31536 "driver-assessment", DATA_TYPE_STRING, 31537 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31538 "op-code", DATA_TYPE_UINT8, op_code, 31539 "cdb", 31540 DATA_TYPE_UINT8_ARRAY, 31541 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31542 "pkt-reason", 31543 DATA_TYPE_UINT8, uscsi_pkt_reason, 31544 "pkt-state", 31545 DATA_TYPE_UINT8, uscsi_pkt_state, 31546 "pkt-stats", 31547 DATA_TYPE_UINT32, uscsi_pkt_statistics, 31548 "stat-code", 31549 DATA_TYPE_UINT8, 31550 ssc->ssc_uscsi_cmd->uscsi_status, 31551 NULL); 31552 } 31553 } 31554 } 31555 31556 /* 31557 * Function: sd_ssc_extract_info 31558 * 31559 * Description: Extract information available to help generate ereport. 31560 * 31561 * Context: Kernel thread or interrupt context. 31562 */ 31563 static void 31564 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp, 31565 struct buf *bp, struct sd_xbuf *xp) 31566 { 31567 size_t senlen = 0; 31568 union scsi_cdb *cdbp; 31569 int path_instance; 31570 /* 31571 * Need scsi_cdb_size array to determine the cdb length. 31572 */ 31573 extern uchar_t scsi_cdb_size[]; 31574 31575 ASSERT(un != NULL); 31576 ASSERT(pktp != NULL); 31577 ASSERT(bp != NULL); 31578 ASSERT(xp != NULL); 31579 ASSERT(ssc != NULL); 31580 ASSERT(mutex_owned(SD_MUTEX(un))); 31581 31582 /* 31583 * Transfer the cdb buffer pointer here. 31584 */ 31585 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 31586 31587 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 31588 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp; 31589 31590 /* 31591 * Transfer the sense data buffer pointer if sense data is available, 31592 * calculate the sense data length first. 31593 */ 31594 if ((xp->xb_sense_state & STATE_XARQ_DONE) || 31595 (xp->xb_sense_state & STATE_ARQ_DONE)) { 31596 /* 31597 * For arq case, we will enter here. 31598 */ 31599 if (xp->xb_sense_state & STATE_XARQ_DONE) { 31600 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid; 31601 } else { 31602 senlen = SENSE_LENGTH; 31603 } 31604 } else { 31605 /* 31606 * For non-arq case, we will enter this branch. 31607 */ 31608 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK && 31609 (xp->xb_sense_state & STATE_XFERRED_DATA)) { 31610 senlen = SENSE_LENGTH - xp->xb_sense_resid; 31611 } 31612 31613 } 31614 31615 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff); 31616 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0; 31617 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data; 31618 31619 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 31620 31621 /* 31622 * Only transfer path_instance when scsi_pkt was properly allocated. 31623 */ 31624 path_instance = pktp->pkt_path_instance; 31625 if (scsi_pkt_allocated_correctly(pktp) && path_instance) 31626 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance; 31627 else 31628 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0; 31629 31630 /* 31631 * Copy in the other fields we may need when posting ereport. 31632 */ 31633 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason; 31634 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state; 31635 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics; 31636 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 31637 31638 /* 31639 * For partially read/write command, we will not create ena 31640 * in case of a successful command be reconized as recovered. 31641 */ 31642 if ((pktp->pkt_reason == CMD_CMPLT) && 31643 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) && 31644 (senlen == 0)) { 31645 return; 31646 } 31647 31648 /* 31649 * To associate ereports of a single command execution flow, we 31650 * need a shared ena for a specific command. 31651 */ 31652 if (xp->xb_ena == 0) 31653 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1); 31654 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena; 31655 } 31656 31657 31658 /* 31659 * Function: sd_check_solid_state 31660 * 31661 * Description: Query the optional INQUIRY VPD page 0xb1. If the device 31662 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION 31663 * RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the 31664 * device is a solid state drive. 31665 * 31666 * Context: Kernel thread or interrupt context. 31667 */ 31668 31669 static void 31670 sd_check_solid_state(sd_ssc_t *ssc) 31671 { 31672 int rval = 0; 31673 uchar_t *inqb1 = NULL; 31674 size_t inqb1_len = MAX_INQUIRY_SIZE; 31675 size_t inqb1_resid = 0; 31676 struct sd_lun *un; 31677 31678 ASSERT(ssc != NULL); 31679 un = ssc->ssc_un; 31680 ASSERT(un != NULL); 31681 ASSERT(!mutex_owned(SD_MUTEX(un))); 31682 31683 mutex_enter(SD_MUTEX(un)); 31684 un->un_f_is_solid_state = FALSE; 31685 31686 if (ISCD(un)) { 31687 mutex_exit(SD_MUTEX(un)); 31688 return; 31689 } 31690 31691 if (sd_check_vpd_page_support(ssc) == 0 && 31692 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) { 31693 mutex_exit(SD_MUTEX(un)); 31694 /* collect page b1 data */ 31695 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP); 31696 31697 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len, 31698 0x01, 0xB1, &inqb1_resid); 31699 31700 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) { 31701 SD_TRACE(SD_LOG_COMMON, un, 31702 "sd_check_solid_state: \ 31703 successfully get VPD page: %x \ 31704 PAGE LENGTH: %x BYTE 4: %x \ 31705 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4], 31706 inqb1[5]); 31707 31708 mutex_enter(SD_MUTEX(un)); 31709 /* 31710 * Check the MEDIUM ROTATION RATE. If it is set 31711 * to 1, the device is a solid state drive. 31712 */ 31713 if (inqb1[4] == 0 && inqb1[5] == 1) { 31714 un->un_f_is_solid_state = TRUE; 31715 /* solid state drives don't need disksort */ 31716 un->un_f_disksort_disabled = TRUE; 31717 } 31718 mutex_exit(SD_MUTEX(un)); 31719 } else if (rval != 0) { 31720 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31721 } 31722 31723 kmem_free(inqb1, inqb1_len); 31724 } else { 31725 mutex_exit(SD_MUTEX(un)); 31726 } 31727 } 31728 31729 /* 31730 * Function: sd_check_emulation_mode 31731 * 31732 * Description: Check whether the SSD is at emulation mode 31733 * by issuing READ_CAPACITY_16 to see whether 31734 * we can get physical block size of the drive. 31735 * 31736 * Context: Kernel thread or interrupt context. 31737 */ 31738 31739 static void 31740 sd_check_emulation_mode(sd_ssc_t *ssc) 31741 { 31742 int rval = 0; 31743 uint64_t capacity; 31744 uint_t lbasize; 31745 uint_t pbsize; 31746 int i; 31747 int devid_len; 31748 struct sd_lun *un; 31749 31750 ASSERT(ssc != NULL); 31751 un = ssc->ssc_un; 31752 ASSERT(un != NULL); 31753 ASSERT(!mutex_owned(SD_MUTEX(un))); 31754 31755 mutex_enter(SD_MUTEX(un)); 31756 if (ISCD(un)) { 31757 mutex_exit(SD_MUTEX(un)); 31758 return; 31759 } 31760 31761 if (un->un_f_descr_format_supported) { 31762 mutex_exit(SD_MUTEX(un)); 31763 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 31764 &pbsize, SD_PATH_DIRECT); 31765 mutex_enter(SD_MUTEX(un)); 31766 31767 if (rval != 0) { 31768 un->un_phy_blocksize = DEV_BSIZE; 31769 } else { 31770 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) { 31771 un->un_phy_blocksize = DEV_BSIZE; 31772 } else if (pbsize > un->un_phy_blocksize) { 31773 /* 31774 * Don't reset the physical blocksize 31775 * unless we've detected a larger value. 31776 */ 31777 un->un_phy_blocksize = pbsize; 31778 } 31779 } 31780 } 31781 31782 for (i = 0; i < sd_flash_dev_table_size; i++) { 31783 devid_len = (int)strlen(sd_flash_dev_table[i]); 31784 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len) 31785 == SD_SUCCESS) { 31786 un->un_phy_blocksize = SSD_SECSIZE; 31787 if (un->un_f_is_solid_state && 31788 un->un_phy_blocksize != un->un_tgt_blocksize) 31789 un->un_f_enable_rmw = TRUE; 31790 } 31791 } 31792 31793 mutex_exit(SD_MUTEX(un)); 31794 } 31795